Rework drm{Get, Free}Devices and add drm{Get, Free}Device
cover Rework drm{Get, Free}Devices and add drm{Get, Free}Device
Hello all,
With this series, we add a couple of new functions drm{Get,Free}Device, which will be used to query information about the opened device.
To do that some refactoring was done, plus an extra test is added to demonstrate how the drm{Get,Free}Device{,s} can be used.
Any and all input will be appreciated.
Emil
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
PATCH 1/12 tests/drmdevice: add new 'test'
A simple example of how to use/what is the new drm{Get,Free}Devices() interface.
--- tests/Makefile.am | 1 + tests/drmdevice.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 tests/drmdevice.c
tests/Makefile.am +1 -0
@@ -31,6 +31,7 @@ LDADD = $(top_builddir)/libdrm.la check_PROGRAMS = \ dristat \ + drmdevice \ drmstat dristat_LDADD = -lm
tests/drmdevice.c new file +93 -0
93 lines not on this page · raw message
Signature · 6 lines
2.5.0 _______________________________________________ dri-devel mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/dri-devel
PATCH 2/12 xf86drm: flex platform specifics into drmParsePciBusInfo
In reply to Emil VelikovThis will allow one to reuse the core drmGetDevices implementation on other platforms. Keeping all the platform specifics in ParseFoo.
On the plus side this saves a bit of code :)
--- xf86drm.c | 46 ++++++++++++++++++++-------------------------- 1 file changed, 20 insertions(+), 26 deletions(-)
xf86drm.c +20 -26
@@ -2855,23 +2855,33 @@ static int drmParseSubsystemType(const char *str)return -EINVAL ; } -static int drmParsePciBusInfo (const char *str,drmPciBusInfoPtr info) +static int drmParsePciBusInfo (int maj,int min,drmPciBusInfoPtr info) { +char path[PATH_MAX +1 ]; +char data[128 ]; +char *str;int domain, bus, dev, func; -char *value; +int fd, ret; -if (str ==NULL ) -return -EINVAL ; +snprintf (path,PATH_MAX ,"/sys/dev/char/%d:%d/device/uevent" , maj, min); + fd =open (path,O_RDONLY ); +if (fd <0 ) +return -errno; - value =strstr (str,"PCI_SLOT_NAME=" ); -if (value ==NULL ) -return -EINVAL ; + ret =read (fd, data,sizeof (data)); +close (fd); +if (ret <0 ) +return -errno; - value +=strlen ("PCI_SLOT_NAME=" ); +#define TAG "PCI_SLOT_NAME=" + str =strstr (data,TAG ); +if (str ==NULL ) +return -EINVAL ; -if (sscanf (value,"%04x:%02x:%02x.%1u" , +if (sscanf (str,TAG "%04x:%02x:%02x.%1u" , &domain, &bus, &dev, &func) !=4 )return -EINVAL ; +#undef TAG info->domain = domain; info->bus = bus;
@@ -2981,7 +2991,6 @@ int drmGetDevices(drmDevicePtr devices[], int max_devices)struct stat sbuf = {0 };char node[PATH_MAX +1 ] ="" ;char path[PATH_MAX +1 ] ="" ; -char data[128 ] ="" ;unsigned char config[64 ] ="" ;int node_type, subsystem_type;int maj, min;
@@ -3030,22 +3039,7 @@ int drmGetDevices(drmDevicePtr devices[], int max_devices) goto free_locals; } -snprintf (path,PATH_MAX ,"/sys/dev/char/%d:%d/device/uevent" , - maj, min); - fd =open (path,O_RDONLY ); -if (fd <0 ) { - ret = -errno; - goto free_locals; - } - ret =read (fd, data,sizeof (data)); -if (ret <0 ) { - ret = -errno; -close (fd); - goto free_locals; - } - - ret =drmParsePciBusInfo (data, pcibus); -close (fd); + ret =drmParsePciBusInfo (maj, min, pcibus);if (ret) goto free_locals;
Signature · 6 lines
2.5.0 _______________________________________________ dri-devel mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/dri-devel
PATCH 3/12 xf86drm: move platform details to drmParsePciDeviceInfo()
In reply to Emil VelikovAs with previous commit let's try to keep drmGetDevices clean of linux specifics.
--- xf86drm.c | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-)
xf86drm.c +15 -21
@@ -2924,11 +2924,22 @@ static int drmGetNodeType(const char *name)return -EINVAL ; } -static int drmParsePciDeviceInfo (const unsigned char *config, +static int drmParsePciDeviceInfo (const char *d_name,drmPciDeviceInfoPtr device) { -if (config ==NULL ) -return -EINVAL ; +char path[PATH_MAX +1 ]; +unsigned char config[64 ]; +int fd, ret; + +snprintf (path,PATH_MAX ,"/sys/class/drm/%s/device/config" , d_name); + fd =open (path,O_RDONLY ); +if (fd <0 ) +return -errno; + + ret =read (fd, config,sizeof (config)); +close (fd); +if (ret <0 ) +return -errno; device->vendor_id = config[0 ] | (config[1 ] <<8 ); device->device_id = config[2 ] | (config[3 ] <<8 );
@@ -2991,10 +3002,8 @@ int drmGetDevices(drmDevicePtr devices[], int max_devices)struct stat sbuf = {0 };char node[PATH_MAX +1 ] ="" ;char path[PATH_MAX +1 ] ="" ; -unsigned char config[64 ] ="" ;int node_type, subsystem_type;int maj, min; -int fd;int ret, i =0 , j, node_count, device_count =0 ;int max_count =16 ;int *duplicated =NULL ;
@@ -3063,32 +3072,17 @@ int drmGetDevices(drmDevicePtr devices[], int max_devices) devs[i].available_nodes =1 << node_type;if (devices !=NULL ) { -snprintf (path,PATH_MAX ,"/sys/class/drm/%s/device/config" , - dent->d_name); - fd =open (path,O_RDONLY ); -if (fd <0 ) { - ret = -errno; - goto free_locals; - } - ret =read (fd, config,64 ); -if (ret <0 ) { - ret = -errno; -close (fd); - goto free_locals; - } - pcidevice =calloc (1 ,sizeof (*pcidevice));if (pcidevice ==NULL ) { ret = -ENOMEM ; goto free_locals; } - ret =drmParsePciDeviceInfo (config, pcidevice); + ret =drmParsePciDeviceInfo (dent->d_name, pcidevice);if (ret) goto free_locals; devs[i].deviceinfo.pci = pcidevice; -close (fd); }break ;default :
Signature · 6 lines
2.5.0 _______________________________________________ dri-devel mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/dri-devel
PATCH 4/12 xf86drm: move the final linux specific bits out of drmGetDevices
In reply to Emil VelikovThird and final piece of making drmGetDevices less crazy/ugly.
--- xf86drm.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-)
xf86drm.c +9 -10
@@ -2835,21 +2835,23 @@ char *drmGetRenderDeviceNameFromFd(int fd) }#ifdef __linux__ -static int drmParseSubsystemType (const char *str) +static int drmParseSubsystemType (int maj,int min) { +char path[PATH_MAX +1 ];char link[PATH_MAX +1 ] ="" ;char *name; -if (readlink (str, link,PATH_MAX ) <0 ) -return -EINVAL ; +snprintf (path,PATH_MAX ,"/sys/dev/char/%d:%d/device/subsystem" , + maj, min); + +if (readlink (path, link,PATH_MAX ) <0 ) +return -errno; name =strrchr (link,'/' );if (!name)return -EINVAL ; - name++; - -if (strncmp (name,"pci" ,3 ) ==0 ) +if (strncmp (name,"/pci" ,4 ) ==0 )return DRM_BUS_PCI ;return -EINVAL ;
@@ -3001,7 +3003,6 @@ int drmGetDevices(drmDevicePtr devices[], int max_devices)struct dirent *dent =NULL ;struct stat sbuf = {0 };char node[PATH_MAX +1 ] ="" ; -char path[PATH_MAX +1 ] ="" ;int node_type, subsystem_type;int maj, min;int ret, i =0 , j, node_count, device_count =0 ;
@@ -3033,9 +3034,7 @@ int drmGetDevices(drmDevicePtr devices[], int max_devices)if (maj !=DRM_MAJOR || !S_ISCHR (sbuf.st_mode))continue ; -snprintf (path,PATH_MAX ,"/sys/dev/char/%d:%d/device/subsystem" , - maj, min); - subsystem_type =drmParseSubsystemType (path); + subsystem_type =drmParseSubsystemType (maj, min);if (subsystem_type <0 )continue ;
Signature · 6 lines
2.5.0 _______________________________________________ dri-devel mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/dri-devel
PATCH 5/12 xf86drm: rename drmSameDevice to drmCompareBusInfo
In reply to Emil VelikovMove away form the boolean name, change the return value appropriately and check if either argument is NULL.
--- xf86drm.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
xf86drm.c +8 -6
@@ -2893,20 +2893,22 @@ static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info)return 0 ; } -static int drmSameDevice (drmDevicePtr a,drmDevicePtr b) +static int drmCompareBusInfo (drmDevicePtr a,drmDevicePtr b) { +if (a ==NULL || b ==NULL ) +return -1 ; +if (a->bustype != b->bustype) -return 0 ; +return -1 ;switch (a->bustype) {case DRM_BUS_PCI : -if (memcmp (a->businfo.pci, b->businfo.pci,sizeof (drmPciBusInfo)) ==0 ) -return 1 ; +return memcmp (a->businfo.pci, b->businfo.pci,sizeof (drmPciBusInfo));default :break ; } -return 0 ; +return -1 ; }static int drmGetNodeType (const char *name)
@@ -3104,7 +3106,7 @@ int drmGetDevices(drmDevicePtr devices[], int max_devices)for (j = i+1 ; j < node_count; j++) {if (duplicated[i] || duplicated[j])continue ; -if (drmSameDevice (&devs[i], &devs[j])) { +if (drmCompareBusInfo (&devs[i], &devs[j]) ==0 ) { duplicated[j] =1 ; devs[i].available_nodes |= devs[j].available_nodes; node_type =log2 (devs[j].available_nodes);
Signature · 6 lines
2.5.0 _______________________________________________ dri-devel mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/dri-devel
PATCH 6/12 util_math: add MAX3 macro
In reply to Emil Velikov--- util_math.h | 1 + 1 file changed, 1 insertion(+)
util_math.h +1 -0
@@ -26,6 +26,7 @@#define MIN2 (A ,B ) ( (A)<(B) ? (A) : (B) )#define MAX2 (A ,B ) ( (A)>(B) ? (A) : (B) ) +#define MAX3 (A ,B ,C ) ((A) > (B) ? MAX2(A, C) : MAX2(B, C))#define __align_mask (value, mask) (((value) + (mask)) & ~(mask))#define ALIGN (value, alignment) __align_mask(value, (typeof(value))((alignment) - 1))
Signature · 6 lines
2.5.0 _______________________________________________ dri-devel mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/dri-devel
PATCH 7/12 xf86drm: rework drmGetDevices()
In reply to Emil VelikovDo a once off memory allocation for each drmDevice.
This allows us to ease the error handling and simplify the de-duplication loop. As part of this we need to rework drmFreeDevice() such so that it frees the relevant hunks, rather than leaving that to the caller.
Some memory stats from the drmdevice test
before: 22 allocs, 22 frees, 66,922 bytes allocated
after: 9 allocs, 9 frees, 66,436 bytes allocated
--- xf86drm.c | 204 +++++++++++++++++++++++++++++--------------------------------- 1 file changed, 94 insertions(+), 110 deletions(-)
xf86drm.c +94 -110
@@ -65,6 +65,8 @@#include "xf86drm.h" #include "libdrm_macros.h" +#include "util_math.h" +#ifdef __OpenBSD__#define DRM_PRIMARY_MINOR_NAME "drm"#define DRM_CONTROL_MINOR_NAME "drmC"
@@ -2928,6 +2930,15 @@ static int drmGetNodeType(const char *name)return -EINVAL ; } +static int drmGetMaxNodeName (void ) +{ +return sizeof (DRM_DIR_NAME ) + +MAX3 (sizeof (DRM_PRIMARY_MINOR_NAME ), +sizeof (DRM_CONTROL_MINOR_NAME ), +sizeof (DRM_RENDER_MINOR_NAME )) + +3 /* lenght of the node number */ ; +} +static int drmParsePciDeviceInfo (const char *d_name,drmPciDeviceInfoPtr device) {
@@ -2954,20 +2965,13 @@ static int drmParsePciDeviceInfo(const char *d_name,return 0 ; } -static void drmFreeDevice (drmDevicePtr device) +static void drmFreeDevice (drmDevicePtr *device) { -int i; -if (device ==NULL )return ; -if (device->nodes !=NULL ) -for (i =0 ; i <DRM_NODE_MAX ; i++) -free (device->nodes[i]); - -free (device->nodes); -free (device->businfo.pci); -free (device->deviceinfo.pci); +free (*device); + *device =NULL ; }void drmFreeDevices (drmDevicePtr devices[],int count)
@@ -2977,11 +2981,8 @@ void drmFreeDevices(drmDevicePtr devices[], int count)if (devices ==NULL )return ; -for (i =0 ; i < count; i++) { -drmFreeDevice (devices[i]); -free (devices[i]); - devices[i] =NULL ; - } +for (i =0 ; i < count && devices[i] !=NULL ; i++) +drmFreeDevice (&devices[i]); }/**
@@ -2998,29 +2999,30 @@ void drmFreeDevices(drmDevicePtr devices[], int count)*/ int drmGetDevices (drmDevicePtr devices[],int max_devices) { -drmDevicePtr devs =NULL ; -drmPciBusInfoPtr pcibus =NULL ; -drmPciDeviceInfoPtr pcidevice =NULL ; -DIR *sysdir =NULL ; -struct dirent *dent =NULL ; -struct stat sbuf = {0 }; -char node[PATH_MAX +1 ] ="" ; +drmDevicePtr *local_devices; +drmDevicePtr device; +DIR *sysdir; +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 =0 , j, node_count, device_count =0 ; +int ret, i, j, node_count, device_count;int max_count =16 ; -int *duplicated =NULL ; +void *addr; - devs =calloc (max_count,sizeof (*devs)); -if (devs ==NULL ) + local_devices =calloc (max_count,sizeof (drmDevicePtr)); +if (local_devices ==NULL )return -ENOMEM ; sysdir =opendir (DRM_DIR_NAME );if (!sysdir) { ret = -errno; - goto free_locals; + goto close_sysdir; } + i =0 ;while ((dent =readdir (sysdir))) { node_type =drmGetNodeType (dent->d_name);if (node_type <0 )
@@ -3043,115 +3045,97 @@ int drmGetDevices(drmDevicePtr devices[], int max_devices)switch (subsystem_type) {case DRM_BUS_PCI : - pcibus =calloc (1 ,sizeof (*pcibus)); -if (pcibus ==NULL ) { - ret = -ENOMEM ; - goto free_locals; + 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); - ret =drmParsePciBusInfo (maj, min, pcibus); -if (ret) - goto free_locals; - -if (i >= max_count) { - max_count +=16 ; - devs =realloc (devs, max_count *sizeof (*devs)); - } + device->businfo.pci = addr; - devs[i].businfo.pci = pcibus; - devs[i].bustype = subsystem_type; - devs[i].nodes =calloc (DRM_NODE_MAX ,sizeof (char *)); -if (devs[i].nodes ==NULL ) { - ret = -ENOMEM ; - goto free_locals; - } - devs[i].nodes[node_type] =strdup (node); -if (devs[i].nodes[node_type] ==NULL ) { - ret = -ENOMEM ; - goto free_locals; - } - devs[i].available_nodes =1 << node_type; + ret =drmParsePciBusInfo (maj, min, device->businfo.pci); +if (ret) + goto free_devices; +// Fetch the device info if the user has requested it if (devices !=NULL ) { - pcidevice =calloc (1 ,sizeof (*pcidevice)); -if (pcidevice ==NULL ) { - ret = -ENOMEM ; - goto free_locals; - } + addr +=sizeof (drmPciBusInfo); + device->deviceinfo.pci = addr; - ret =drmParsePciDeviceInfo (dent->d_name, pcidevice); + ret =drmParsePciDeviceInfo (dent->d_name, + device->deviceinfo.pci);if (ret) - goto free_locals; - - devs[i].deviceinfo.pci = pcidevice; + goto free_devices; } +break ;default :fprintf (stderr,"The subsystem type is not supported yet\n" );break ; } - i++; - } - node_count = i; +if (i >= max_count) { +drmDevicePtr *temp; -/* merge duplicated devices with same domain/bus/device/func IDs */ - duplicated =calloc (node_count,sizeof (*duplicated)); -if (duplicated ==NULL ) { - ret = -ENOMEM ; - goto free_locals; + max_count +=16 ; + temp =realloc (local_devices, max_count *sizeof (drmDevicePtr)); +if (!temp) + goto free_devices; + local_devices = temp; + } + + local_devices[i] = device; + i++; } + 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 (duplicated[i] || duplicated[j]) -continue ; -if (drmCompareBusInfo (&devs[i], &devs[j]) ==0 ) { - duplicated[j] =1 ; - devs[i].available_nodes |= devs[j].available_nodes; - node_type =log2 (devs[j].available_nodes); - devs[i].nodes[node_type] = devs[j].nodes[node_type]; -free (devs[j].nodes); -free (devs[j].businfo.pci); -free (devs[j].deviceinfo.pci); +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]); } } } -for (i =0 ; i < node_count; i++) { -if (duplicated[i] ==0 ) { -if ((devices !=NULL ) && (device_count < max_devices)) { - devices[device_count] =calloc (1 ,sizeof (drmDevice)); -if (devices[device_count] ==NULL ) { - ret = -ENOMEM ; -break ; - } -memcpy (devices[device_count], &devs[i],sizeof (drmDevice)); - }else -drmFreeDevice (&devs[i]); - device_count++; - } - } + device_count =0 ; +for (i =0 ; i < node_count && local_devices[i]; i++) { +if ((devices !=NULL ) && (device_count < max_devices)) + devices[device_count] = local_devices[i]; +else +drmFreeDevice (&local_devices[i]); -if (i < node_count) { -drmFreeDevices (devices, device_count); -for ( ; i < node_count; i++) -if (duplicated[i] ==0 ) -drmFreeDevice (&devs[i]); - }else - ret = device_count; + device_count++; + } -free (duplicated); -free (devs); +free (local_devices);closedir (sysdir); -return ret; +return device_count; + +free_devices: +drmFreeDevices (local_devices, i); +free (local_devices); -free_locals: -for (j =0 ; j < i; j++) -drmFreeDevice (&devs[j]); -free (pcidevice); -free (pcibus); -free (devs); +close_sysdir:closedir (sysdir);return ret; }
Signature · 6 lines
2.5.0 _______________________________________________ dri-devel mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/dri-devel
PATCH 8/12 xf86drm: move ifdef __linux__ guards where needed
In reply to Emil Velikov--- xf86drm.c | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-)
xf86drm.c +15 -18
@@ -2836,9 +2836,9 @@ char *drmGetRenderDeviceNameFromFd(int fd)return drmGetMinorNameForFD (fd,DRM_NODE_RENDER ); } -#ifdef __linux__static int drmParseSubsystemType (int maj,int min) { +#ifdef __linux__char path[PATH_MAX +1 ];char link[PATH_MAX +1 ] ="" ;char *name;
@@ -2857,10 +2857,15 @@ static int drmParseSubsystemType(int maj, int min)return DRM_BUS_PCI ;return -EINVAL ; +#else +#warning "Missing implementation of drmParseSubsystemType" +return -EINVAL ; +#endif }static int drmParsePciBusInfo (int maj,int min,drmPciBusInfoPtr info) { +#ifdef __linux__char path[PATH_MAX +1 ];char data[128 ];char *str;
@@ -2893,6 +2898,10 @@ static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info) info->func = func;return 0 ; +#else +#warning "Missing implementation of drmParsePciBusInfo" +return -EINVAL ; +#endif }static int drmCompareBusInfo (drmDevicePtr a,drmDevicePtr b)
@@ -2942,6 +2951,7 @@ static int drmGetMaxNodeName(void)static int drmParsePciDeviceInfo (const char *d_name,drmPciDeviceInfoPtr device) { +#ifdef __linux__char path[PATH_MAX +1 ];unsigned char config[64 ];int fd, ret;
@@ -2963,6 +2973,10 @@ static int drmParsePciDeviceInfo(const char *d_name, device->subdevice_id = config[46 ] | (config[47 ] <<8 );return 0 ; +#else +#warning "Missing implementation of drmParsePciDeviceInfo" +return -EINVAL ; +#endif }static void drmFreeDevice (drmDevicePtr *device)
@@ -3139,20 +3153,3 @@ close_sysdir:closedir (sysdir);return ret; } -#else -void drmFreeDevices (drmDevicePtr devices[],int count) -{ - (void )devices; - (void )count; -} - -int drmGetDevices (drmDevicePtr devices[],int max_devices) -{ - (void )devices; - (void )max_devices; -return -EINVAL ; -} - -#warning "Missing implementation of drmGetDevices/drmFreeDevices" - -#endif
Signature · 6 lines
2.5.0 _______________________________________________ dri-devel mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/dri-devel
PATCH 9/12 xf86drm: warn on missing drmGetMinorNameForFD implementation
In reply to Emil Velikov--- xf86drm.c | 2 ++ 1 file changed, 2 insertions(+)
xf86drm.c +2 -0
@@ -2822,6 +2822,8 @@ static char *drmGetMinorNameForFD(int fd, int type) out_close_dir:closedir (sysdir); +#else +#warning "Missing implementation of drmGetMinorNameForFD"#endif return NULL ; }
Signature · 6 lines
2.5.0 _______________________________________________ dri-devel mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/dri-devel
PATCH 10/12 xf86drm: split out drmProcessPciDevice and drmFoldDuplicatedDevices
In reply to Emil VelikovWill be reused in the next commit.
--- xf86drm.c | 123 +++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 74 insertions(+), 49 deletions(-)
xf86drm.c +74 -49
@@ -36,6 +36,7 @@#endif #include <stdio.h> #include <stdlib.h> +#include <stdbool.h> #include <unistd.h> #include <string.h> #include <strings.h>
@@ -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++) {
Signature · 6 lines
2.5.0 _______________________________________________ dri-devel mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/dri-devel
PATCH 11/12 xf86drm: add drm{Get,Free}Device
In reply to Emil VelikovSimilar interface to the *Devices() ones but they obtain/free the information of the opened device (as given by its fd).
Note there is a fair bit of duplication between the two Get functions, and anyone interested is more than welcome to consolidate it.
--- xf86drm.c | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- xf86drm.h | 3 ++ 2 files changed, 117 insertions(+), 1 deletion(-)
xf86drm.c +114 -1
@@ -2982,7 +2982,7 @@ static int drmParsePciDeviceInfo(const char *d_name,#endif } -static void drmFreeDevice (drmDevicePtr *device) +void drmFreeDevice (drmDevicePtr *device) {if (device ==NULL )return ;
@@ -3072,6 +3072,119 @@ static void drmFoldDuplicatedDevices(drmDevicePtr local_devices[], int count) }/** +* Get information about the opened drm device +* +* \param fd file descriptor of the drm device +* \param device the address of a drmDevicePtr where the information +* will be allocated in stored +* +* \return zero on success, negative error code otherwise. +*/ +int drmGetDevice (int fd,drmDevicePtr *device) +{ +drmDevicePtr *local_devices; +drmDevicePtr d; +DIR *sysdir; +struct dirent *dent; +struct stat sbuf; +char node[PATH_MAX +1 ]; +int node_type, subsystem_type; +int maj, min; +int ret, i, node_count; +int max_count =16 ; + +if (fd ==-1 || device ==NULL ) +return -EINVAL ; + +if (fstat (fd, &sbuf)) +return -errno; + + maj =major (sbuf.st_rdev); + min =minor (sbuf.st_rdev); + +if (maj !=DRM_MAJOR || !S_ISCHR (sbuf.st_mode)) +return -EINVAL ; + + subsystem_type =drmParseSubsystemType (maj, min); + + local_devices =calloc (max_count,sizeof (drmDevicePtr)); +if (local_devices ==NULL ) +return -ENOMEM ; + + sysdir =opendir (DRM_DIR_NAME ); +if (!sysdir) { + ret = -errno; + goto close_sysdir; + } + + i =0 ; +while ((dent =readdir (sysdir))) { + node_type =drmGetNodeType (dent->d_name); +if (node_type <0 ) +continue ; + +snprintf (node,PATH_MAX ,"%s/%s" ,DRM_DIR_NAME , dent->d_name); +if (stat (node, &sbuf)) +continue ; + + maj =major (sbuf.st_rdev); + min =minor (sbuf.st_rdev); + +if (maj !=DRM_MAJOR || !S_ISCHR (sbuf.st_mode)) +continue ; + +if (drmParseSubsystemType (maj, min) != subsystem_type) +continue ; + +switch (subsystem_type) { +case DRM_BUS_PCI : + ret =drmProcessPciDevice (&d, dent->d_name, node, node_type, + maj, min, true); +if (ret) + goto free_devices; + +break ; +default : +fprintf (stderr,"The subsystem type is not supported yet\n" ); +break ; + } + +if (i >= max_count) { +drmDevicePtr *temp; + + max_count +=16 ; + temp =realloc (local_devices, max_count *sizeof (drmDevicePtr)); +if (!temp) + goto free_devices; + local_devices = temp; + } + + local_devices[i] = d; + i++; + } + node_count = i; + +/* Fold nodes into a single device if they share the same bus info */ +drmFoldDuplicatedDevices (local_devices, node_count); + + *device = local_devices[0 ]; +for (i =1 ; i < node_count && local_devices[i]; i++) +drmFreeDevice (&local_devices[i]); + +free (local_devices); +closedir (sysdir); +return 0 ; + +free_devices: +drmFreeDevices (local_devices, i); +free (local_devices); + +close_sysdir: +closedir (sysdir); +return ret; +} + +/** * Get drm devices on the system * * \param devices the array of devices with drmDevicePtr elements
xf86drm.h +3 -0
@@ -790,6 +790,9 @@ typedef struct _drmDevice { } deviceinfo; } drmDevice, *drmDevicePtr; +extern int drmGetDevice (int fd,drmDevicePtr *device); +extern void drmFreeDevice (drmDevicePtr *device); +extern int drmGetDevices (drmDevicePtr devices[],int max_devices);extern void drmFreeDevices (drmDevicePtr devices[],int count);
Signature · 6 lines
2.5.0 _______________________________________________ dri-devel mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/dri-devel
PATCH 12/12 tests/drmdevice: add drm{Get, Free}Device() example
In reply to Emil Velikov--- tests/drmdevice.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-)
tests/drmdevice.c +21 -2
@@ -23,6 +23,9 @@#include <stdio.h> #include <stdlib.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <unistd.h> #include <xf86drm.h>
@@ -62,7 +65,8 @@ intmain (void) {drmDevicePtr *devices; -int ret, max_devices; +drmDevicePtr device; +int fd, ret, max_devices; max_devices =drmGetDevices (NULL ,0 );
@@ -84,9 +88,24 @@ main(void)return -1 ; } -for (int i =0 ; i < ret; i++) +for (int i =0 ; i < ret; i++) {print_device_info (devices[i], i); +for (int j =0 ; j <DRM_NODE_MAX ; j++) { +if (devices[i]->available_nodes &1 << j) { + fd =open (devices[i]->nodes[j],O_RDONLY |O_CLOEXEC ,0 ); +if (fd <0 ) +continue ; + +if (drmGetDevice (fd, &device) ==0 ) { +print_device_info (device,-1 ); +drmFreeDevice (&device); + } +close (fd); + } + } + } +drmFreeDevices (devices, ret);free (devices);return 0 ;
Signature · 6 lines
2.5.0 _______________________________________________ dri-devel mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/dri-devel
Quoted 9 lines · Emil Velikov
Hello all,
With this series, we add a couple of new functions drm{Get,Free}Device, which will be used to query information about the opened device.
To do that some refactoring was done, plus an extra test is added to demonstrate how the drm{Get,Free}Device{,s} can be used.
Any and all input will be appreciated.
Looks like a nice clean up to me.
Quoted 7 lines
Emil
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
Quoted 6 lines · Alex Deucher
Quoted 5 lines · Emil Velikov
Hello all,
With this series, we add a couple of new functions drm{Get,Free}Device, which will be used to query information about the opened device.
Quoted 9 lines
Quoted 4 lines
To do that some refactoring was done, plus an extra test is added to demonstrate how the drm{Get,Free}Device{,s} can be used.
Any and all input will be appreciated.
Looks like a nice clean up to me.
Acked-by Alex Deucher <[email protected]>
Thanks Alex. It's all in master now.
-Emil
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel