From patchwork Wed Sep 9 17:21:31 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emil Velikov X-Patchwork-Id: 7147691 Return-Path: X-Original-To: patchwork-dri-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id C9B469F326 for ; Wed, 9 Sep 2015 17:17:38 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id B941820567 for ; Wed, 9 Sep 2015 17:17:37 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by mail.kernel.org (Postfix) with ESMTP id 9C47C20A06 for ; Wed, 9 Sep 2015 17:17:36 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id EAE856EC8A; Wed, 9 Sep 2015 10:17:35 -0700 (PDT) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wi0-f171.google.com (mail-wi0-f171.google.com [209.85.212.171]) by gabe.freedesktop.org (Postfix) with ESMTPS id 113AE6EC8A for ; Wed, 9 Sep 2015 10:17:34 -0700 (PDT) Received: by wiclk2 with SMTP id lk2so29786305wic.1 for ; Wed, 09 Sep 2015 10:17:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=U2uf88/wxj7/lDH2dhlKrFpoqW11Bu7NRYBy+NB4GXY=; b=Ys2ew5yanRXtBI1xOa72mEje9FAMAqskI2Is0g09nfrlshWzO7MuVx5Lq2hfJSkv2S DP2K+Y/hPo22eHrb5l/i5EQxCZVujfN7CL38LAIRbqD8+BiBvsbgKjYv9FU/lf2b3hxM cNnG/nwL6glN/Kxp4qYMH7md4RDg32yvU7cDIBFoDwWU6LnBYbdtmez+QFN5CSlztltA 94itmmYZqyvq5unPLGgeL0eM1/TgsCjFsMFvKC3jooMZvjtgIiW6vHa2xyBcktcpsWaN rT2LgFrubbwgLIQMx3FMVPPZOA0q9bQfX/FKZ90vE65AkTKyU0FNR8LAh6j7n14U1Zyg uhPQ== X-Received: by 10.194.250.100 with SMTP id zb4mr13821556wjc.126.1441819052838; Wed, 09 Sep 2015 10:17:32 -0700 (PDT) Received: from arch-x220.localdomain (cpc12-croy20-2-0-cust52.croy.cable.virginm.net. [82.44.54.53]) by smtp.gmail.com with ESMTPSA id j7sm11257042wjz.11.2015.09.09.10.17.31 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 09 Sep 2015 10:17:32 -0700 (PDT) From: Emil Velikov To: dri-devel@lists.freedesktop.org Subject: [PATCH libdrm 10/12] xf86drm: split out drmProcessPciDevice and drmFoldDuplicatedDevices Date: Wed, 9 Sep 2015 18:21:31 +0100 Message-Id: <1441819293-25567-11-git-send-email-emil.l.velikov@gmail.com> X-Mailer: git-send-email 2.5.0 In-Reply-To: <1441819293-25567-1-git-send-email-emil.l.velikov@gmail.com> References: <1441819293-25567-1-git-send-email-emil.l.velikov@gmail.com> Cc: emil.l.velikov@gmail.com X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Spam-Status: No, score=-4.1 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_MED, T_DKIM_INVALID, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Will be reused in the next commit. Signed-off-by: Emil Velikov --- xf86drm.c | 123 +++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 74 insertions(+), 49 deletions(-) diff --git a/xf86drm.c b/xf86drm.c index a783a28..aa0fbe4 100644 --- a/xf86drm.c +++ b/xf86drm.c @@ -36,6 +36,7 @@ #endif #include #include +#include #include #include #include @@ -3001,6 +3002,75 @@ void drmFreeDevices(drmDevicePtr devices[], int count) drmFreeDevice(&devices[i]); } +static int drmProcessPciDevice(drmDevicePtr *device, const char *d_name, + const char *node, int node_type, + int maj, int min, bool fetch_deviceinfo) +{ + const int max_node_str = drmGetMaxNodeName(); + int ret, i; + void *addr; + + addr = *device = calloc(1, sizeof(drmDevice) + + (DRM_NODE_MAX * + (sizeof(void *) + max_node_str)) + + sizeof(drmPciBusInfo) + + sizeof(drmPciDeviceInfo)); + if (!*device) + return -ENOMEM; + + (*device)->bustype = DRM_BUS_PCI; + (*device)->available_nodes = 1 << node_type; + + addr += sizeof(drmDevice); + (*device)->nodes = addr; + + addr += DRM_NODE_MAX * sizeof(void *); + for (i = 0; i < DRM_NODE_MAX; i++) { + (*device)->nodes[i] = addr; + addr += max_node_str; + } + memcpy((*device)->nodes[node_type], node, max_node_str); + + (*device)->businfo.pci = addr; + + ret = drmParsePciBusInfo(maj, min, (*device)->businfo.pci); + if (ret) + goto free_device; + + // Fetch the device info if the user has requested it + if (fetch_deviceinfo) { + addr += sizeof(drmPciBusInfo); + (*device)->deviceinfo.pci = addr; + + ret = drmParsePciDeviceInfo(d_name, (*device)->deviceinfo.pci); + if (ret) + goto free_device; + } + return 0; + +free_device: + free(*device); + *device = NULL; + return ret; +} + +static void drmFoldDuplicatedDevices(drmDevicePtr local_devices[], int count) +{ + int node_type, i, j; + + for (i = 0; i < count; i++) { + for (j = i + 1; j < count; j++) { + if (drmCompareBusInfo(local_devices[i], local_devices[j]) == 0) { + local_devices[i]->available_nodes |= local_devices[j]->available_nodes; + node_type = log2(local_devices[j]->available_nodes); + memcpy(local_devices[i]->nodes[node_type], + local_devices[j]->nodes[node_type], drmGetMaxNodeName()); + drmFreeDevice(&local_devices[j]); + } + } + } +} + /** * Get drm devices on the system * @@ -3021,12 +3091,10 @@ int drmGetDevices(drmDevicePtr devices[], int max_devices) struct dirent *dent; struct stat sbuf; char node[PATH_MAX + 1]; - const int max_node_str = drmGetMaxNodeName(); int node_type, subsystem_type; int maj, min; - int ret, i, j, node_count, device_count; + int ret, i, node_count, device_count; int max_count = 16; - void *addr; local_devices = calloc(max_count, sizeof(drmDevicePtr)); if (local_devices == NULL) @@ -3061,44 +3129,11 @@ int drmGetDevices(drmDevicePtr devices[], int max_devices) switch (subsystem_type) { case DRM_BUS_PCI: - addr = device = calloc(1, sizeof(drmDevice) + - (DRM_NODE_MAX * - (sizeof(void *) + max_node_str)) + - sizeof(drmPciBusInfo) + - sizeof(drmPciDeviceInfo)); - if (!device) - goto free_devices; - - device->bustype = subsystem_type; - device->available_nodes = 1 << node_type; - - addr += sizeof(drmDevice); - device->nodes = addr; - - addr += DRM_NODE_MAX * sizeof(void *); - for (j = 0; j < DRM_NODE_MAX; j++) { - device->nodes[j] = addr; - addr += max_node_str; - } - memcpy(device->nodes[node_type], node, max_node_str); - - device->businfo.pci = addr; - - ret = drmParsePciBusInfo(maj, min, device->businfo.pci); + ret = drmProcessPciDevice(&device, dent->d_name, node, node_type, + maj, min, devices != NULL); if (ret) goto free_devices; - // Fetch the device info if the user has requested it - if (devices != NULL) { - addr += sizeof(drmPciBusInfo); - device->deviceinfo.pci = addr; - - ret = drmParsePciDeviceInfo(dent->d_name, - device->deviceinfo.pci); - if (ret) - goto free_devices; - } - break; default: fprintf(stderr, "The subsystem type is not supported yet\n"); @@ -3121,17 +3156,7 @@ int drmGetDevices(drmDevicePtr devices[], int max_devices) node_count = i; /* Fold nodes into a single device if they share the same bus info */ - for (i = 0; i < node_count; i++) { - for (j = i + 1; j < node_count; j++) { - if (drmCompareBusInfo(local_devices[i], local_devices[j]) == 0) { - local_devices[i]->available_nodes |= local_devices[j]->available_nodes; - node_type = log2(local_devices[j]->available_nodes); - memcpy(local_devices[i]->nodes[node_type], - local_devices[j]->nodes[node_type], max_node_str); - drmFreeDevice(&local_devices[j]); - } - } - } + drmFoldDuplicatedDevices(local_devices, node_count); device_count = 0; for (i = 0; i < node_count && local_devices[i]; i++) {