[PATCH 1/2] Add device enumeration interface (v2)

Jammy Zhou Jammy.Zhou at amd.com
Thu Apr 23 20:44:05 PDT 2015


drmGetDevices interface is added to enumernate GPU devices on the system

v2: rebase the code and some improvement for the coding style

Signed-off-by: Frank Min <Frank.Min at amd.com>
Signed-off-by: Jammy Zhou <Jammy.Zhou at amd.com> (v2)
---
 Makefile.am |  3 ++-
 xf86drm.c   | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 xf86drm.h   | 18 ++++++++++++++++++
 3 files changed, 68 insertions(+), 1 deletion(-)

diff --git a/Makefile.am b/Makefile.am
index 42d3d7f..8236ed8 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -89,7 +89,8 @@ 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@ \
+		@PCIACCESS_LIBS@
 
 libdrm_la_CPPFLAGS = -I$(top_srcdir)/include/drm
 AM_CFLAGS = \
diff --git a/xf86drm.c b/xf86drm.c
index ffc53b8..4d67861 100644
--- a/xf86drm.c
+++ b/xf86drm.c
@@ -63,6 +63,7 @@
 
 #include "xf86drm.h"
 #include "libdrm.h"
+#include <pciaccess.h>
 
 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
 #define DRM_MAJOR 145
@@ -2817,3 +2818,50 @@ 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 drmGetDevices(drmDevicePtr devs, uint16_t vendor)
+{
+	struct pci_device_iterator * iter;
+	struct pci_device * dev;
+	uint32_t count = 0;
+
+	if (pci_system_init())
+		return -EINVAL;
+
+	iter = pci_slot_match_iterator_create(NULL);
+	if (!iter)
+		return -EINVAL;
+
+	while ((dev = pci_device_next(iter))) {
+		if (((dev->device_class == 0x30000) ||
+			(dev->device_class == 0x38000)) &&
+			((vendor == 0) || (dev->vendor_id == vendor))){
+			if (devs) {
+				devs[count].domain = dev->domain;
+				devs[count].bus = dev->bus;
+				devs[count].dev = dev->dev;
+				devs[count].func = dev->func;
+				devs[count].vendor_id = dev->vendor_id;
+				devs[count].device_id = dev->device_id;
+				devs[count].subvendor_id = dev->subvendor_id;
+				devs[count].subdevice_id = dev->subdevice_id;
+				devs[count].revision_id = dev->revision;
+			}
+			count++;
+		}
+	}
+
+	pci_iterator_destroy(iter);
+	pci_system_cleanup();
+	return count;
+}
diff --git a/xf86drm.h b/xf86drm.h
index 40c55c9..6150e71 100644
--- a/xf86drm.h
+++ b/xf86drm.h
@@ -752,6 +752,24 @@ extern int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle);
 extern char *drmGetPrimaryDeviceNameFromFd(int fd);
 extern char *drmGetRenderDeviceNameFromFd(int fd);
 
+/**
+ * Structure for a general pcie gpu device
+ */
+typedef struct _drmDevice {
+	uint16_t domain;
+	uint8_t bus;
+	uint8_t dev;
+	uint8_t func;
+	uint8_t revision_id;
+	uint16_t vendor_id;
+	uint16_t device_id;
+	uint16_t subvendor_id;
+	uint16_t subdevice_id;
+} drmDevice, *drmDevicePtr;
+
+
+extern int drmGetDevices(drmDevicePtr devs, uint16_t vendor);
+
 #if defined(__cplusplus) || defined(c_plusplus)
 }
 #endif
-- 
1.9.1



More information about the dri-devel mailing list