From patchwork Mon May 25 12:18:02 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jammy Zhou X-Patchwork-Id: 6474181 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 47A279F1C1 for ; Mon, 25 May 2015 12:18:21 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id AE07A2041B for ; Mon, 25 May 2015 12:18:17 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by mail.kernel.org (Postfix) with ESMTP id 13DB4202B8 for ; Mon, 25 May 2015 12:18:14 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id DF3F26E3CA; Mon, 25 May 2015 05:18:12 -0700 (PDT) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from na01-by2-obe.outbound.protection.outlook.com (mail-by2on0120.outbound.protection.outlook.com [207.46.100.120]) by gabe.freedesktop.org (Postfix) with ESMTP id 5C5156E3CA for ; Mon, 25 May 2015 05:18:12 -0700 (PDT) Received: from BY2PR02CA0096.namprd02.prod.outlook.com (25.163.44.150) by BN3PR02MB1110.namprd02.prod.outlook.com (25.162.168.140) with Microsoft SMTP Server (TLS) id 15.1.172.22; Mon, 25 May 2015 12:18:10 +0000 Received: from BY2FFO11FD040.protection.gbl (2a01:111:f400:7c0c::115) by BY2PR02CA0096.outlook.office365.com (2a01:111:e400:5261::22) with Microsoft SMTP Server (TLS) id 15.1.172.22 via Frontend Transport; Mon, 25 May 2015 12:18:10 +0000 Authentication-Results: spf=none (sender IP is 165.204.84.222) smtp.mailfrom=amd.com; gmail.com; dkim=none (message not signed) header.d=none; Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) Received: from atltwp02.amd.com (165.204.84.222) by BY2FFO11FD040.mail.protection.outlook.com (10.1.14.225) with Microsoft SMTP Server id 15.1.172.14 via Frontend Transport; Mon, 25 May 2015 12:18:09 +0000 X-WSS-ID: 0NOWNI6-08-912-02 X-M-MSG: Received: from satlvexedge01.amd.com (satlvexedge01.amd.com [10.177.96.28]) (using TLSv1 with cipher AES128-SHA (128/128 bits)) (No client certificate requested) by atltwp02.amd.com (Axway MailGate 5.3.1) with ESMTPS id 25010D16020; Mon, 25 May 2015 08:18:06 -0400 (EDT) Received: from SATLEXDAG01.amd.com (10.181.40.3) by satlvexedge01.amd.com (10.177.96.28) with Microsoft SMTP Server (TLS) id 14.3.195.1; Mon, 25 May 2015 07:18:23 -0500 Received: from SCYBEXDAG01.amd.com (10.34.11.11) by SATLEXDAG01.amd.com (10.181.40.3) with Microsoft SMTP Server (TLS) id 14.3.195.1; Mon, 25 May 2015 08:18:06 -0400 Received: from SCYBEXDAG04.amd.com ([169.254.4.81]) by SCYBEXDAG01.amd.com ([169.254.1.190]) with mapi id 14.03.0195.001; Mon, 25 May 2015 20:18:02 +0800 From: "Zhou, Jammy" To: "dri-devel@lists.freedesktop.org" , "emil.l.velikov@gmail.com" Subject: RE: [PATCH] Add device enumeration interface (v3) Thread-Topic: [PATCH] Add device enumeration interface (v3) Thread-Index: AQHQkgh1n+mjZUedFkqte1HEe74Z6Z2MpXEg Date: Mon, 25 May 2015 12:18:02 +0000 Message-ID: References: <1432049465-8563-1-git-send-email-Jammy.Zhou@amd.com> In-Reply-To: <1432049465-8563-1-git-send-email-Jammy.Zhou@amd.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.224.42.200] MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-Microsoft-Exchange-Diagnostics: 1; BY2FFO11FD040; 1:6X+mGDfCd2VtgeYKuYdRc7N5YhkldWf4+Nxj/SuneUa0y4lxiBfSwnaL5Esbg7iY5FL0HMbd8aGuZQ7z5M1WBvO3Hhm0rAxzo2h2SNtdeKYT0fRwDfsFUzmRqsQ3sX13hE0Q3Q246ZiIYywKrG5kl2Q7HPqh9vZL8Uv5oEiKfVunWMOpKxSwZpIgWoR3k/kIJYURlGE/7ng0jd7cea0s+92xYb10ySUdfr0/eDm5tnObNsSdQFHlDRXClSXkzbTzv60c4M2sk3Fm0vUUoC7KBuGlWVxPQ6/uLA37pN+i55E+T1WL60lxmk5d3t/x6C71 X-Forefront-Antispam-Report: CIP:165.204.84.222; CTRY:US; IPV:NLI; EFV:NLI; SFV:NSPM; SFS:(10019020)(6009001)(428002)(199003)(13464003)(377454003)(189002)(97736004)(68736005)(5001770100001)(2900100001)(2950100001)(50466002)(15975445007)(2920100001)(54356999)(33656002)(55846006)(101416001)(106116001)(5001830100001)(1720100001)(2501003)(19580405001)(19580395003)(5001860100001)(47776003)(53416004)(76176999)(102836002)(4001540100001)(106466001)(64706001)(62966003)(77156002)(23676002)(46102003)(2656002)(85806002)(50986999)(189998001)(86362001)(105586002)(87936001)(92566002); DIR:OUT; SFP:1102; SCL:1; SRVR:BN3PR02MB1110; H:atltwp02.amd.com; FPR:; SPF:None; PTR:InfoDomainNonexistent; A:1; MX:1; LANG:en; X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:BN3PR02MB1110; X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(601004)(5005006)(520002)(3002001); SRVR:BN3PR02MB1110; BCL:0; PCL:0; RULEID:; SRVR:BN3PR02MB1110; X-Forefront-PRVS: 058707456E X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 May 2015 12:18:09.5760 (UTC) X-MS-Exchange-CrossTenant-Id: fde4dada-be84-483f-92cc-e026cbee8e96 X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=fde4dada-be84-483f-92cc-e026cbee8e96; Ip=[165.204.84.222]; Helo=[atltwp02.amd.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN3PR02MB1110 Cc: "Deucher, Alexander" , "Min, Frank" 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: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, 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 Hi Emil, Do you have chance to have a look at this new version? Thanks! Regards, Jammy -----Original Message----- From: dri-devel [mailto:dri-devel-bounces@lists.freedesktop.org] On Behalf Of Zhou, Jammy Sent: Tuesday, May 19, 2015 11:31 PM To: dri-devel@lists.freedesktop.org; emil.l.velikov@gmail.com Cc: Min, Frank Subject: [PATCH] Add device enumeration interface (v3) From: frank v3: switch to udev/sysfs for the enumeration Signed-off-by: Frank Min Reviewed-by: Christian König Reviewed-by: Alex Deucher Reviewed-by: Jammy Zhou --- Makefile.am | 7 +++-- xf86drm.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ xf86drm.h | 19 +++++++++++ 3 files changed, 127 insertions(+), 2 deletions(-) -- 1.9.1 _______________________________________________ dri-devel mailing list dri-devel@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/dri-devel diff --git a/Makefile.am b/Makefile.am index 13df80c..ffd334a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -95,12 +95,15 @@ SUBDIRS = \ libdrm_la_LTLIBRARIES = libdrm.la libdrm_ladir = $(libdir) libdrm_la_LDFLAGS = -version-number 2:4:0 -no-undefined -libdrm_la_LIBADD = @CLOCK_LIB@ +libdrm_la_LIBADD = \ + @CLOCK_LIB@ \ + @LIBUDEV_LIBS@ libdrm_la_CPPFLAGS = -I$(top_srcdir)/include/drm AM_CFLAGS = \ $(WARN_CFLAGS) \ - $(VALGRIND_CFLAGS) + $(VALGRIND_CFLAGS) \ + $(LIBUDEV_CFLAGS) libdrm_la_SOURCES = $(LIBDRM_FILES) diff --git a/xf86drm.c b/xf86drm.c index f7c45f8..d2704de 100644 --- a/xf86drm.c +++ b/xf86drm.c @@ -63,6 +63,7 @@ #include "xf86drm.h" #include "libdrm_macros.h" +#include "libudev.h" #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) #define DRM_MAJOR 145 @@ -2817,3 +2818,105 @@ char *drmGetRenderDeviceNameFromFd(int fd) { return drmGetMinorNameForFD(fd, DRM_NODE_RENDER); } + +/** +* Enumerate the GPU devices on the system +* +* \param devs device array set to return the device information +* (if NULL, the number of device is returned) +* \param vendor the vendor ID for GPU devices to list +* (optional, if not specified, all GPU devices are returned) +* +* \return the number of GPU devices +*/ +int drmGetPciDevices(drmPciDevicePtr devSet, uint16_t vendorId) { + struct udev *udev = NULL; + struct udev_enumerate *udev_enumerate; + struct udev_list_entry *list_entry; + struct udev_device *device; + int drmDevCount = 0; + int vendor = 0; + int devid = 0; + int devclass = 0; + int subvendor = 0; + int subdevid = 0; + int revid = 0xff; + int domain = 0; + int bus = 0; + int dev = 0; + int func = 0; + char config[64] = {0}; + char node[128] = {'\0'}; + char slot[] = "0000:00:00.0"; + char *info = NULL; + int minor = 0xff; + int fd = 0; + int ret = 0; + + udev = udev_new(); + if (udev == NULL) { + fprintf(stderr, "no context\n"); + return -EINVAL; + } + udev_enumerate = udev_enumerate_new(udev); + if (udev_enumerate == NULL) + return -EINVAL; + udev_enumerate_add_match_subsystem(udev_enumerate, "drm"); + udev_enumerate_add_match_property(udev_enumerate, "DEVTYPE", +"drm_minor"); + + udev_enumerate_scan_devices(udev_enumerate); + + udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(udev_enumerate)) { + device = udev_device_new_from_syspath(udev_enumerate_get_udev(udev_enumerate), + udev_list_entry_get_name(list_entry)); + if (device != NULL) { + info = udev_device_get_property_value(device, "MINOR"); + if (!strcmp(info, "0")) { + strcpy(node, udev_device_get_syspath(device)); + info = strstr(node, "/drm"); + strncpy(slot, info - strlen(slot), strlen(slot)); + if (sscanf(slot, "%4x:%2x:%2x.%1x", &domain, &bus, &dev, &func) != 4) { + domain = 0; + bus = 0; + dev = 0; + func = 0; + } + strcpy(node + strlen(node), "/device/config"); + + fd = open(node, O_RDONLY); + if (fd >= 0) { + ret = read(fd, config, 64); + if (ret == 64) { + vendor = config[0] + (config[1] << 8); + devid = config[2] + (config[3] << 8); + revid = config[8]; + devclass = config[9] + (config[10] << 8) + (config[11] << 16); + subdevid = config[44] + (config[45] << 8); + } + close(fd); + } + + if((vendorId == 0) || (vendorId == vendor)) { + if(devSet != NULL) { + devSet[drmDevCount].domain = domain; + devSet[drmDevCount].bus = bus; + devSet[drmDevCount].dev = dev; + devSet[drmDevCount].func = func; + devSet[drmDevCount].vendor_id = vendor; + devSet[drmDevCount].device_id = devid; + devSet[drmDevCount].subdevice_id = subdevid; + devSet[drmDevCount].subvendor_id = subvendor; + devSet[drmDevCount].revision_id = revid; + } + drmDevCount++; + } + } + } + udev_device_unref(device); + } + udev_enumerate_unref(udev_enumerate); + udev_unref(udev); + + return drmDevCount; +} diff --git a/xf86drm.h b/xf86drm.h index 40c55c9..2610934 100644 --- a/xf86drm.h +++ b/xf86drm.h @@ -342,6 +342,24 @@ typedef struct _drmSetVersion { int drm_dd_minor; } drmSetVersion, *drmSetVersionPtr; +/** + * Structure to a general pci gpu device + * + * \sa drmGetDevices() + * +*/ +typedef struct _drmPciDevice { + uint16_t domain; + uint8_t bus; + uint8_t dev; + uint8_t func; + uint16_t vendor_id; + uint16_t device_id; + uint16_t subvendor_id; + uint16_t subdevice_id; + uint8_t revision_id; +} drmPciDevice, *drmPciDevicePtr; + #define __drm_dummy_lock(lock) (*(__volatile__ unsigned int *)lock) #define DRM_LOCK_HELD 0x80000000U /**< Hardware lock is held */ @@ -552,6 +570,7 @@ do { register unsigned int __old __asm("o0"); \ /* General user-level programmer's API: unprivileged */ extern int drmAvailable(void); extern int drmOpen(const char *name, const char *busid); +extern int drmGetPciDevices(drmPciDevicePtr devSet, uint16_t vendorId); #define DRM_NODE_PRIMARY 0 #define DRM_NODE_CONTROL 1