Re: [Mesa-dev] [PATCH v2 05/16] loader: reimplement loader_get_user_preferred_fd via libdrm

2016-10-18 Thread Nicolai Hähnle

On 18.10.2016 18:01, Emil Velikov wrote:

On 18 October 2016 at 09:49, Nicolai Hähnle  wrote:

On 14.10.2016 20:21, Emil Velikov wrote:


From: Emil Velikov 

Currently not everyone has libudev and with follow-up patches we'll
completely remove the divergent codepaths.

Use the libdrm drm device API to construct the required ID_PATH_TAG-like
string, to preserve the current functionality for libudev users and
allow others to benefit from it as well.

v2: Drop ranty comments, pick the correct device

Cc: Axel Davy 
Signed-off-by: Emil Velikov 
---
 src/loader/loader.c | 247
++--
 1 file changed, 106 insertions(+), 141 deletions(-)

diff --git a/src/loader/loader.c b/src/loader/loader.c
index ad4f946..06df05b 100644
--- a/src/loader/loader.c
+++ b/src/loader/loader.c


[snip]


@@ -321,17 +232,60 @@ static char *loader_get_dri_config_device_id(void)
 }
 #endif

+static char *drm_construct_id_path_tag(drmDevicePtr device)
+{
+/* Length of "pci-_xx_xx_x\n" */
+#define PCI_ID_PATH_TAG_LENGTH 17
+   char *tag = NULL;
+
+   if (device->bustype == DRM_BUS_PCI) {
+tag = calloc(PCI_ID_PATH_TAG_LENGTH, sizeof(char));
+if (tag == NULL)
+return NULL;
+
+sprintf(tag, "pci-%04x_%02x_%02x_%1u",
device->businfo.pci->domain,
+device->businfo.pci->bus, device->businfo.pci->dev,
+device->businfo.pci->func);



Defensive programming would suggest to use snprintf.


Correct. It's more like extra defensive in this case but will fix.


Thanks :)



[snip]


@@ -345,55 +299,66 @@ int loader_get_user_preferred_fd(int default_fd, int
*different_device)
   return default_fd;
}

-   udev = udev_new();
-   if (!udev)
-  goto prime_clean;
+   default_tag = drm_get_id_path_tag_for_fd(default_fd);
+   if (default_tag == NULL)
+  goto err;

-   default_device_id_path_tag = get_id_path_tag_from_fd(udev,
default_fd);
-   if (!default_device_id_path_tag)
-  goto udev_clean;
+   num_devices = drmGetDevices(devices, MAX_DRM_DEVICES);
+   if (num_devices < 0)
+  goto err;

-   is_different_device = 1;
/* two format are supported:
 * "1": choose any other card than the card used by default.
 * id_path_tag: (for example "pci-_02_00_0") choose the card
 * with this id_path_tag.
 */
if (!strcmp(prime,"1")) {
-  free(prime);
-  prime = strdup(default_device_id_path_tag);
-  /* request a card with a different card than the default card */
-  another_tag = 1;
-   } else if (!strcmp(default_device_id_path_tag, prime))
-  /* we are to get a new fd (render-node) of the same device */
-  is_different_device = 0;
-
-   device_name = get_render_node_from_id_path_tag(udev,
-  prime,
-  another_tag);
-   if (device_name == NULL) {
-  is_different_device = 0;
-  goto default_device_clean;
+  /* Hmm... detection for 2-7 seems to be broken. Oh well ...
+   * Pick the first render device that is not our own.
+   */
+  for (i = 0; i < num_devices; i++) {
+ if (devices[i]->available_nodes & 1 << DRM_NODE_RENDER &&
+ !drm_device_matches_tag(devices[i], default_tag)) {
+
+found = true;
+break;
+ }
+  }
+   } else {
+  for (i = 0; i < num_devices; i++) {
+ if (devices[i]->available_nodes & 1 << DRM_NODE_RENDER &&
+drm_device_matches_tag(devices[i], prime)) {
+
+found = true;
+break;
+ }
+  }



I feel like it would be helpful to have a warning here if the device was not
found. This could avoid some confusion when people inevitably typo their
prime setting.


Original code does not have such a message, so let's add it as follow-up ?


Fine by me.

Cheers,
Nicolai



Emil


___
mesa-dev mailing list
mesa-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/mesa-dev


Re: [Mesa-dev] [PATCH v2 05/16] loader: reimplement loader_get_user_preferred_fd via libdrm

2016-10-18 Thread Emil Velikov
On 18 October 2016 at 09:49, Nicolai Hähnle  wrote:
> On 14.10.2016 20:21, Emil Velikov wrote:
>>
>> From: Emil Velikov 
>>
>> Currently not everyone has libudev and with follow-up patches we'll
>> completely remove the divergent codepaths.
>>
>> Use the libdrm drm device API to construct the required ID_PATH_TAG-like
>> string, to preserve the current functionality for libudev users and
>> allow others to benefit from it as well.
>>
>> v2: Drop ranty comments, pick the correct device
>>
>> Cc: Axel Davy 
>> Signed-off-by: Emil Velikov 
>> ---
>>  src/loader/loader.c | 247
>> ++--
>>  1 file changed, 106 insertions(+), 141 deletions(-)
>>
>> diff --git a/src/loader/loader.c b/src/loader/loader.c
>> index ad4f946..06df05b 100644
>> --- a/src/loader/loader.c
>> +++ b/src/loader/loader.c
>
> [snip]
>>
>> @@ -321,17 +232,60 @@ static char *loader_get_dri_config_device_id(void)
>>  }
>>  #endif
>>
>> +static char *drm_construct_id_path_tag(drmDevicePtr device)
>> +{
>> +/* Length of "pci-_xx_xx_x\n" */
>> +#define PCI_ID_PATH_TAG_LENGTH 17
>> +   char *tag = NULL;
>> +
>> +   if (device->bustype == DRM_BUS_PCI) {
>> +tag = calloc(PCI_ID_PATH_TAG_LENGTH, sizeof(char));
>> +if (tag == NULL)
>> +return NULL;
>> +
>> +sprintf(tag, "pci-%04x_%02x_%02x_%1u",
>> device->businfo.pci->domain,
>> +device->businfo.pci->bus, device->businfo.pci->dev,
>> +device->businfo.pci->func);
>
>
> Defensive programming would suggest to use snprintf.
>
Correct. It's more like extra defensive in this case but will fix.

> [snip]
>
>> @@ -345,55 +299,66 @@ int loader_get_user_preferred_fd(int default_fd, int
>> *different_device)
>>return default_fd;
>> }
>>
>> -   udev = udev_new();
>> -   if (!udev)
>> -  goto prime_clean;
>> +   default_tag = drm_get_id_path_tag_for_fd(default_fd);
>> +   if (default_tag == NULL)
>> +  goto err;
>>
>> -   default_device_id_path_tag = get_id_path_tag_from_fd(udev,
>> default_fd);
>> -   if (!default_device_id_path_tag)
>> -  goto udev_clean;
>> +   num_devices = drmGetDevices(devices, MAX_DRM_DEVICES);
>> +   if (num_devices < 0)
>> +  goto err;
>>
>> -   is_different_device = 1;
>> /* two format are supported:
>>  * "1": choose any other card than the card used by default.
>>  * id_path_tag: (for example "pci-_02_00_0") choose the card
>>  * with this id_path_tag.
>>  */
>> if (!strcmp(prime,"1")) {
>> -  free(prime);
>> -  prime = strdup(default_device_id_path_tag);
>> -  /* request a card with a different card than the default card */
>> -  another_tag = 1;
>> -   } else if (!strcmp(default_device_id_path_tag, prime))
>> -  /* we are to get a new fd (render-node) of the same device */
>> -  is_different_device = 0;
>> -
>> -   device_name = get_render_node_from_id_path_tag(udev,
>> -  prime,
>> -  another_tag);
>> -   if (device_name == NULL) {
>> -  is_different_device = 0;
>> -  goto default_device_clean;
>> +  /* Hmm... detection for 2-7 seems to be broken. Oh well ...
>> +   * Pick the first render device that is not our own.
>> +   */
>> +  for (i = 0; i < num_devices; i++) {
>> + if (devices[i]->available_nodes & 1 << DRM_NODE_RENDER &&
>> + !drm_device_matches_tag(devices[i], default_tag)) {
>> +
>> +found = true;
>> +break;
>> + }
>> +  }
>> +   } else {
>> +  for (i = 0; i < num_devices; i++) {
>> + if (devices[i]->available_nodes & 1 << DRM_NODE_RENDER &&
>> +drm_device_matches_tag(devices[i], prime)) {
>> +
>> +found = true;
>> +break;
>> + }
>> +  }
>
>
> I feel like it would be helpful to have a warning here if the device was not
> found. This could avoid some confusion when people inevitably typo their
> prime setting.
>
Original code does not have such a message, so let's add it as follow-up ?

Emil
___
mesa-dev mailing list
mesa-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/mesa-dev


Re: [Mesa-dev] [PATCH v2 05/16] loader: reimplement loader_get_user_preferred_fd via libdrm

2016-10-18 Thread Nicolai Hähnle

On 14.10.2016 20:21, Emil Velikov wrote:

From: Emil Velikov 

Currently not everyone has libudev and with follow-up patches we'll
completely remove the divergent codepaths.

Use the libdrm drm device API to construct the required ID_PATH_TAG-like
string, to preserve the current functionality for libudev users and
allow others to benefit from it as well.

v2: Drop ranty comments, pick the correct device

Cc: Axel Davy 
Signed-off-by: Emil Velikov 
---
 src/loader/loader.c | 247 ++--
 1 file changed, 106 insertions(+), 141 deletions(-)

diff --git a/src/loader/loader.c b/src/loader/loader.c
index ad4f946..06df05b 100644
--- a/src/loader/loader.c
+++ b/src/loader/loader.c

[snip]

@@ -321,17 +232,60 @@ static char *loader_get_dri_config_device_id(void)
 }
 #endif

+static char *drm_construct_id_path_tag(drmDevicePtr device)
+{
+/* Length of "pci-_xx_xx_x\n" */
+#define PCI_ID_PATH_TAG_LENGTH 17
+   char *tag = NULL;
+
+   if (device->bustype == DRM_BUS_PCI) {
+tag = calloc(PCI_ID_PATH_TAG_LENGTH, sizeof(char));
+if (tag == NULL)
+return NULL;
+
+sprintf(tag, "pci-%04x_%02x_%02x_%1u", device->businfo.pci->domain,
+device->businfo.pci->bus, device->businfo.pci->dev,
+device->businfo.pci->func);


Defensive programming would suggest to use snprintf.

[snip]

@@ -345,55 +299,66 @@ int loader_get_user_preferred_fd(int default_fd, int 
*different_device)
   return default_fd;
}

-   udev = udev_new();
-   if (!udev)
-  goto prime_clean;
+   default_tag = drm_get_id_path_tag_for_fd(default_fd);
+   if (default_tag == NULL)
+  goto err;

-   default_device_id_path_tag = get_id_path_tag_from_fd(udev, default_fd);
-   if (!default_device_id_path_tag)
-  goto udev_clean;
+   num_devices = drmGetDevices(devices, MAX_DRM_DEVICES);
+   if (num_devices < 0)
+  goto err;

-   is_different_device = 1;
/* two format are supported:
 * "1": choose any other card than the card used by default.
 * id_path_tag: (for example "pci-_02_00_0") choose the card
 * with this id_path_tag.
 */
if (!strcmp(prime,"1")) {
-  free(prime);
-  prime = strdup(default_device_id_path_tag);
-  /* request a card with a different card than the default card */
-  another_tag = 1;
-   } else if (!strcmp(default_device_id_path_tag, prime))
-  /* we are to get a new fd (render-node) of the same device */
-  is_different_device = 0;
-
-   device_name = get_render_node_from_id_path_tag(udev,
-  prime,
-  another_tag);
-   if (device_name == NULL) {
-  is_different_device = 0;
-  goto default_device_clean;
+  /* Hmm... detection for 2-7 seems to be broken. Oh well ...
+   * Pick the first render device that is not our own.
+   */
+  for (i = 0; i < num_devices; i++) {
+ if (devices[i]->available_nodes & 1 << DRM_NODE_RENDER &&
+ !drm_device_matches_tag(devices[i], default_tag)) {
+
+found = true;
+break;
+ }
+  }
+   } else {
+  for (i = 0; i < num_devices; i++) {
+ if (devices[i]->available_nodes & 1 << DRM_NODE_RENDER &&
+drm_device_matches_tag(devices[i], prime)) {
+
+found = true;
+break;
+ }
+  }


I feel like it would be helpful to have a warning here if the device was 
not found. This could avoid some confusion when people inevitably typo 
their prime setting.


Nicolai
___
mesa-dev mailing list
mesa-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/mesa-dev


Re: [Mesa-dev] [PATCH v2 05/16] loader: reimplement loader_get_user_preferred_fd via libdrm

2016-10-17 Thread Emil Velikov
On 15 October 2016 at 08:59, Axel Davy  wrote:
> On 14/10/2016 22:33, Emil Velikov wrote:
>>
>> On 14 October 2016 at 20:21, Axel Davy  wrote:
>>
>>> The code looks good. With the minor nitpick fixed, this patch is:
>>> Reviewed-by: Axel Davy 
>>>
>> Thanks. If you can skim through any of the other patches that'll be
>> appreciated.
>> Emil
>>
> I've looked through the entire serie, and the patches look good to me
> (except the small typo in the commit title of the 11th patch). You can add
> my r-b.
>
Thanks !
Emil
___
mesa-dev mailing list
mesa-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/mesa-dev


Re: [Mesa-dev] [PATCH v2 05/16] loader: reimplement loader_get_user_preferred_fd via libdrm

2016-10-15 Thread Axel Davy

On 14/10/2016 22:33, Emil Velikov wrote:

On 14 October 2016 at 20:21, Axel Davy  wrote:


The code looks good. With the minor nitpick fixed, this patch is:
Reviewed-by: Axel Davy 


Thanks. If you can skim through any of the other patches that'll be appreciated.
Emil

I've looked through the entire serie, and the patches look good to me 
(except the small typo in the commit title of the 11th patch). You can 
add my r-b.



Axel

___
mesa-dev mailing list
mesa-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/mesa-dev


Re: [Mesa-dev] [PATCH v2 05/16] loader: reimplement loader_get_user_preferred_fd via libdrm

2016-10-14 Thread Emil Velikov
On 14 October 2016 at 20:21, Axel Davy  wrote:
> On 14/10/2016 20:21, Emil Velikov wrote:
>>
>> From: Emil Velikov 
>>
>> Currently not everyone has libudev and with follow-up patches we'll
>> completely remove the divergent codepaths.
>>
>> Use the libdrm drm device API to construct the required ID_PATH_TAG-like
>> string, to preserve the current functionality for libudev users and
>> allow others to benefit from it as well.
>>
>> v2: Drop ranty comments, pick the correct device
>>
>> Cc: Axel Davy 
>> Signed-off-by: Emil Velikov 
>> ---
>>   src/loader/loader.c | 247
>> ++--
>>   1 file changed, 106 insertions(+), 141 deletions(-)
>>
>> diff --git a/src/loader/loader.c b/src/loader/loader.c
>> index ad4f946..06df05b 100644
>> --- a/src/loader/loader.c
>> +++ b/src/loader/loader.c
>> @@ -69,16 +69,11 @@
>>   #include 
>>   #include 
>>   #include 
>> +#include 
>>   #include 
>>   #ifdef HAVE_LIBUDEV
>>   #include 
>>   #include 
>> -#include 
>> -#include 
>> -#ifdef USE_DRICONF
>> -#include "xmlconfig.h"
>> -#include "xmlpool.h"
>> -#endif
>>   #endif
>>   #ifdef MAJOR_IN_MKDEV
>>   #include 
>> @@ -89,7 +84,13 @@
>>   #include "loader.h"
>> #ifdef HAVE_LIBDRM
>> +#include 
>> +#include 
>>   #include 
>> +#ifdef USE_DRICONF
>> +#include "xmlconfig.h"
>> +#include "xmlpool.h"
>> +#endif
>>   #endif
>> #define __IS_LOADER
>> @@ -203,99 +204,9 @@ udev_device_new_from_fd(struct udev *udev, int fd)
>>return device;
>>   }
>> +#endif
>>   -static char *
>> -get_render_node_from_id_path_tag(struct udev *udev,
>> - char *id_path_tag,
>> - char another_tag)
>> -{
>> -   struct udev_device *device;
>> -   struct udev_enumerate *e;
>> -   struct udev_list_entry *entry;
>> -   const char *path, *id_path_tag_tmp;
>> -   char *path_res;
>> -   char found = 0;
>> -   UDEV_SYMBOL(struct udev_enumerate *, udev_enumerate_new,
>> -   (struct udev *));
>> -   UDEV_SYMBOL(int, udev_enumerate_add_match_subsystem,
>> -   (struct udev_enumerate *, const char *));
>> -   UDEV_SYMBOL(int, udev_enumerate_add_match_sysname,
>> -   (struct udev_enumerate *, const char *));
>> -   UDEV_SYMBOL(int, udev_enumerate_scan_devices,
>> -   (struct udev_enumerate *));
>> -   UDEV_SYMBOL(struct udev_list_entry *, udev_enumerate_get_list_entry,
>> -   (struct udev_enumerate *));
>> -   UDEV_SYMBOL(void, udev_enumerate_unref,
>> -   (struct udev_enumerate *));
>> -   UDEV_SYMBOL(struct udev_list_entry *, udev_list_entry_get_next,
>> -   (struct udev_list_entry *));
>> -   UDEV_SYMBOL(const char *, udev_list_entry_get_name,
>> -   (struct udev_list_entry *));
>> -   UDEV_SYMBOL(struct udev_device *, udev_device_new_from_syspath,
>> -   (struct udev *, const char *));
>> -   UDEV_SYMBOL(const char *, udev_device_get_property_value,
>> -   (struct udev_device *, const char *));
>> -   UDEV_SYMBOL(const char *, udev_device_get_devnode,
>> -   (struct udev_device *));
>> -   UDEV_SYMBOL(struct udev_device *, udev_device_unref,
>> -   (struct udev_device *));
>> -
>> -   e = udev_enumerate_new(udev);
>> -   udev_enumerate_add_match_subsystem(e, "drm");
>> -   udev_enumerate_add_match_sysname(e, "render*");
>> -
>> -   udev_enumerate_scan_devices(e);
>> -   udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
>> -  path = udev_list_entry_get_name(entry);
>> -  device = udev_device_new_from_syspath(udev, path);
>> -  if (!device)
>> - continue;
>> -  id_path_tag_tmp = udev_device_get_property_value(device,
>> "ID_PATH_TAG");
>> -  if (id_path_tag_tmp) {
>> - if ((!another_tag && !strcmp(id_path_tag, id_path_tag_tmp)) ||
>> - (another_tag && strcmp(id_path_tag, id_path_tag_tmp))) {
>> -found = 1;
>> -break;
>> - }
>> -  }
>> -  udev_device_unref(device);
>> -   }
>> -
>> -   udev_enumerate_unref(e);
>> -
>> -   if (found) {
>> -  path_res = strdup(udev_device_get_devnode(device));
>> -  udev_device_unref(device);
>> -  return path_res;
>> -   }
>> -   return NULL;
>> -}
>> -
>> -static char *
>> -get_id_path_tag_from_fd(struct udev *udev, int fd)
>> -{
>> -   struct udev_device *device;
>> -   const char *id_path_tag_tmp;
>> -   char *id_path_tag;
>> -   UDEV_SYMBOL(const char *, udev_device_get_property_value,
>> -   (struct udev_device *, const char *));
>> -   UDEV_SYMBOL(struct udev_device *, udev_device_unref,
>> -   (struct udev_device *));
>> -
>> -   device = udev_device_new_from_fd(udev, fd);
>> -   if (!device)
>> -  return NULL;
>> -
>> -   id_path_tag_tmp = udev_device_get_property_value(device,
>> "ID_PATH_TAG");
>> -   if (!id_path_tag_tmp)

Re: [Mesa-dev] [PATCH v2 05/16] loader: reimplement loader_get_user_preferred_fd via libdrm

2016-10-14 Thread Axel Davy

On 14/10/2016 20:21, Emil Velikov wrote:

From: Emil Velikov 

Currently not everyone has libudev and with follow-up patches we'll
completely remove the divergent codepaths.

Use the libdrm drm device API to construct the required ID_PATH_TAG-like
string, to preserve the current functionality for libudev users and
allow others to benefit from it as well.

v2: Drop ranty comments, pick the correct device

Cc: Axel Davy 
Signed-off-by: Emil Velikov 
---
  src/loader/loader.c | 247 ++--
  1 file changed, 106 insertions(+), 141 deletions(-)

diff --git a/src/loader/loader.c b/src/loader/loader.c
index ad4f946..06df05b 100644
--- a/src/loader/loader.c
+++ b/src/loader/loader.c
@@ -69,16 +69,11 @@
  #include 
  #include 
  #include 
+#include 
  #include 
  #ifdef HAVE_LIBUDEV
  #include 
  #include 
-#include 
-#include 
-#ifdef USE_DRICONF
-#include "xmlconfig.h"
-#include "xmlpool.h"
-#endif
  #endif
  #ifdef MAJOR_IN_MKDEV
  #include 
@@ -89,7 +84,13 @@
  #include "loader.h"
  
  #ifdef HAVE_LIBDRM

+#include 
+#include 
  #include 
+#ifdef USE_DRICONF
+#include "xmlconfig.h"
+#include "xmlpool.h"
+#endif
  #endif
  
  #define __IS_LOADER

@@ -203,99 +204,9 @@ udev_device_new_from_fd(struct udev *udev, int fd)
  
 return device;

  }
+#endif
  
-static char *

-get_render_node_from_id_path_tag(struct udev *udev,
- char *id_path_tag,
- char another_tag)
-{
-   struct udev_device *device;
-   struct udev_enumerate *e;
-   struct udev_list_entry *entry;
-   const char *path, *id_path_tag_tmp;
-   char *path_res;
-   char found = 0;
-   UDEV_SYMBOL(struct udev_enumerate *, udev_enumerate_new,
-   (struct udev *));
-   UDEV_SYMBOL(int, udev_enumerate_add_match_subsystem,
-   (struct udev_enumerate *, const char *));
-   UDEV_SYMBOL(int, udev_enumerate_add_match_sysname,
-   (struct udev_enumerate *, const char *));
-   UDEV_SYMBOL(int, udev_enumerate_scan_devices,
-   (struct udev_enumerate *));
-   UDEV_SYMBOL(struct udev_list_entry *, udev_enumerate_get_list_entry,
-   (struct udev_enumerate *));
-   UDEV_SYMBOL(void, udev_enumerate_unref,
-   (struct udev_enumerate *));
-   UDEV_SYMBOL(struct udev_list_entry *, udev_list_entry_get_next,
-   (struct udev_list_entry *));
-   UDEV_SYMBOL(const char *, udev_list_entry_get_name,
-   (struct udev_list_entry *));
-   UDEV_SYMBOL(struct udev_device *, udev_device_new_from_syspath,
-   (struct udev *, const char *));
-   UDEV_SYMBOL(const char *, udev_device_get_property_value,
-   (struct udev_device *, const char *));
-   UDEV_SYMBOL(const char *, udev_device_get_devnode,
-   (struct udev_device *));
-   UDEV_SYMBOL(struct udev_device *, udev_device_unref,
-   (struct udev_device *));
-
-   e = udev_enumerate_new(udev);
-   udev_enumerate_add_match_subsystem(e, "drm");
-   udev_enumerate_add_match_sysname(e, "render*");
-
-   udev_enumerate_scan_devices(e);
-   udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
-  path = udev_list_entry_get_name(entry);
-  device = udev_device_new_from_syspath(udev, path);
-  if (!device)
- continue;
-  id_path_tag_tmp = udev_device_get_property_value(device, "ID_PATH_TAG");
-  if (id_path_tag_tmp) {
- if ((!another_tag && !strcmp(id_path_tag, id_path_tag_tmp)) ||
- (another_tag && strcmp(id_path_tag, id_path_tag_tmp))) {
-found = 1;
-break;
- }
-  }
-  udev_device_unref(device);
-   }
-
-   udev_enumerate_unref(e);
-
-   if (found) {
-  path_res = strdup(udev_device_get_devnode(device));
-  udev_device_unref(device);
-  return path_res;
-   }
-   return NULL;
-}
-
-static char *
-get_id_path_tag_from_fd(struct udev *udev, int fd)
-{
-   struct udev_device *device;
-   const char *id_path_tag_tmp;
-   char *id_path_tag;
-   UDEV_SYMBOL(const char *, udev_device_get_property_value,
-   (struct udev_device *, const char *));
-   UDEV_SYMBOL(struct udev_device *, udev_device_unref,
-   (struct udev_device *));
-
-   device = udev_device_new_from_fd(udev, fd);
-   if (!device)
-  return NULL;
-
-   id_path_tag_tmp = udev_device_get_property_value(device, "ID_PATH_TAG");
-   if (!id_path_tag_tmp)
-  return NULL;
-
-   id_path_tag = strdup(id_path_tag_tmp);
-
-   udev_device_unref(device);
-   return id_path_tag;
-}
-
+#if defined(HAVE_LIBDRM)
  #ifdef USE_DRICONF
  static const char __driConfigOptionsLoader[] =
  DRI_CONF_BEGIN
@@ -321,17 +232,60 @@ static char *loader_get_dri_config_device_id(void)
  }
  #endif
  
+static char *drm_construct_id_path_tag(drmDevicePtr device)

+{
+/* Length of "pci-_xx_xx_x\n" */

I assume you want to say:

"pci-_xx_xx_x" + 

[Mesa-dev] [PATCH v2 05/16] loader: reimplement loader_get_user_preferred_fd via libdrm

2016-10-14 Thread Emil Velikov
From: Emil Velikov 

Currently not everyone has libudev and with follow-up patches we'll
completely remove the divergent codepaths.

Use the libdrm drm device API to construct the required ID_PATH_TAG-like
string, to preserve the current functionality for libudev users and
allow others to benefit from it as well.

v2: Drop ranty comments, pick the correct device

Cc: Axel Davy 
Signed-off-by: Emil Velikov 
---
 src/loader/loader.c | 247 ++--
 1 file changed, 106 insertions(+), 141 deletions(-)

diff --git a/src/loader/loader.c b/src/loader/loader.c
index ad4f946..06df05b 100644
--- a/src/loader/loader.c
+++ b/src/loader/loader.c
@@ -69,16 +69,11 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #ifdef HAVE_LIBUDEV
 #include 
 #include 
-#include 
-#include 
-#ifdef USE_DRICONF
-#include "xmlconfig.h"
-#include "xmlpool.h"
-#endif
 #endif
 #ifdef MAJOR_IN_MKDEV
 #include 
@@ -89,7 +84,13 @@
 #include "loader.h"
 
 #ifdef HAVE_LIBDRM
+#include 
+#include 
 #include 
+#ifdef USE_DRICONF
+#include "xmlconfig.h"
+#include "xmlpool.h"
+#endif
 #endif
 
 #define __IS_LOADER
@@ -203,99 +204,9 @@ udev_device_new_from_fd(struct udev *udev, int fd)
 
return device;
 }
+#endif
 
-static char *
-get_render_node_from_id_path_tag(struct udev *udev,
- char *id_path_tag,
- char another_tag)
-{
-   struct udev_device *device;
-   struct udev_enumerate *e;
-   struct udev_list_entry *entry;
-   const char *path, *id_path_tag_tmp;
-   char *path_res;
-   char found = 0;
-   UDEV_SYMBOL(struct udev_enumerate *, udev_enumerate_new,
-   (struct udev *));
-   UDEV_SYMBOL(int, udev_enumerate_add_match_subsystem,
-   (struct udev_enumerate *, const char *));
-   UDEV_SYMBOL(int, udev_enumerate_add_match_sysname,
-   (struct udev_enumerate *, const char *));
-   UDEV_SYMBOL(int, udev_enumerate_scan_devices,
-   (struct udev_enumerate *));
-   UDEV_SYMBOL(struct udev_list_entry *, udev_enumerate_get_list_entry,
-   (struct udev_enumerate *));
-   UDEV_SYMBOL(void, udev_enumerate_unref,
-   (struct udev_enumerate *));
-   UDEV_SYMBOL(struct udev_list_entry *, udev_list_entry_get_next,
-   (struct udev_list_entry *));
-   UDEV_SYMBOL(const char *, udev_list_entry_get_name,
-   (struct udev_list_entry *));
-   UDEV_SYMBOL(struct udev_device *, udev_device_new_from_syspath,
-   (struct udev *, const char *));
-   UDEV_SYMBOL(const char *, udev_device_get_property_value,
-   (struct udev_device *, const char *));
-   UDEV_SYMBOL(const char *, udev_device_get_devnode,
-   (struct udev_device *));
-   UDEV_SYMBOL(struct udev_device *, udev_device_unref,
-   (struct udev_device *));
-
-   e = udev_enumerate_new(udev);
-   udev_enumerate_add_match_subsystem(e, "drm");
-   udev_enumerate_add_match_sysname(e, "render*");
-
-   udev_enumerate_scan_devices(e);
-   udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
-  path = udev_list_entry_get_name(entry);
-  device = udev_device_new_from_syspath(udev, path);
-  if (!device)
- continue;
-  id_path_tag_tmp = udev_device_get_property_value(device, "ID_PATH_TAG");
-  if (id_path_tag_tmp) {
- if ((!another_tag && !strcmp(id_path_tag, id_path_tag_tmp)) ||
- (another_tag && strcmp(id_path_tag, id_path_tag_tmp))) {
-found = 1;
-break;
- }
-  }
-  udev_device_unref(device);
-   }
-
-   udev_enumerate_unref(e);
-
-   if (found) {
-  path_res = strdup(udev_device_get_devnode(device));
-  udev_device_unref(device);
-  return path_res;
-   }
-   return NULL;
-}
-
-static char *
-get_id_path_tag_from_fd(struct udev *udev, int fd)
-{
-   struct udev_device *device;
-   const char *id_path_tag_tmp;
-   char *id_path_tag;
-   UDEV_SYMBOL(const char *, udev_device_get_property_value,
-   (struct udev_device *, const char *));
-   UDEV_SYMBOL(struct udev_device *, udev_device_unref,
-   (struct udev_device *));
-
-   device = udev_device_new_from_fd(udev, fd);
-   if (!device)
-  return NULL;
-
-   id_path_tag_tmp = udev_device_get_property_value(device, "ID_PATH_TAG");
-   if (!id_path_tag_tmp)
-  return NULL;
-
-   id_path_tag = strdup(id_path_tag_tmp);
-
-   udev_device_unref(device);
-   return id_path_tag;
-}
-
+#if defined(HAVE_LIBDRM)
 #ifdef USE_DRICONF
 static const char __driConfigOptionsLoader[] =
 DRI_CONF_BEGIN
@@ -321,17 +232,60 @@ static char *loader_get_dri_config_device_id(void)
 }
 #endif
 
+static char *drm_construct_id_path_tag(drmDevicePtr device)
+{
+/* Length of "pci-_xx_xx_x\n" */
+#define PCI_ID_PATH_TAG_LENGTH 17
+   char *tag = NULL;
+
+   if (device->bustype == DRM_BUS_PCI) {
+tag =