icd: add an API to scan Linux DRM subsystem
authorChia-I Wu <olvaffe@gmail.com>
Mon, 15 Sep 2014 05:08:49 +0000 (13:08 +0800)
committerChia-I Wu <olvaffe@gmail.com>
Mon, 15 Sep 2014 05:48:50 +0000 (13:48 +0800)
It differs from the scanner in intel ICD in that both legacy (primary) nodes
and render nodes are supported.  And it does not open() any of them.

icd/common/CMakeLists.txt
icd/common/icd-enumerate-drm.c [new file with mode: 0644]
icd/common/icd-enumerate-drm.h [new file with mode: 0644]

index ba49a23..dc6e3bd 100644 (file)
@@ -9,5 +9,5 @@ add_custom_command(OUTPUT icd-dispatch-entrypoints.c
                   DEPENDS ${PROJECT_SOURCE_DIR}/xgl-generate.py
                           ${PROJECT_SOURCE_DIR}/xgl.py)
 
-add_library(icd OBJECT icd.c icd-dispatch-entrypoints.c icd-dispatch-table.h icd-format.c icd-utils.c)
+add_library(icd OBJECT icd.c icd-dispatch-entrypoints.c icd-dispatch-table.h icd-enumerate-drm.c icd-format.c icd-utils.c)
 set_target_properties(icd PROPERTIES POSITION_INDEPENDENT_CODE ON)
diff --git a/icd/common/icd-enumerate-drm.c b/icd/common/icd-enumerate-drm.c
new file mode 100644 (file)
index 0000000..e3ceea8
--- /dev/null
@@ -0,0 +1,201 @@
+/*
+ * XGL
+ *
+ * Copyright (C) 2014 LunarG, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Authors:
+ *   Chia-I Wu <olv@lunarg.com>
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <libudev.h>
+
+#include "icd-utils.h"
+#include "icd-enumerate-drm.h"
+
+static enum icd_drm_minor_type get_minor_type(struct udev_device *minor_dev)
+{
+    const char *minor;
+
+    minor = udev_device_get_property_value(minor_dev, "MINOR");
+    if (!minor)
+        return ICD_DRM_MINOR_INVALID;
+
+    switch (atoi(minor) >> 6) {
+    case 0:
+        return ICD_DRM_MINOR_LEGACY;
+    case 2:
+        return ICD_DRM_MINOR_RENDER;
+    default:
+        return ICD_DRM_MINOR_INVALID;
+    }
+}
+
+static void get_pci_id(struct udev_device *pci_dev, int *vendor, int *devid)
+{
+    const char *pci_id;
+
+    pci_id = udev_device_get_property_value(pci_dev, "PCI_ID");
+    if (sscanf(pci_id, "%x:%x", vendor, devid) != 2) {
+        *vendor = 0;
+        *devid = 0;
+    }
+}
+
+static struct icd_drm_device *find_dev(struct icd_drm_device *devices,
+                                       const char *parent_syspath)
+{
+    struct icd_drm_device *dev = devices;
+
+    while (dev) {
+        if (!strcmp((const char *) dev->id, parent_syspath))
+            break;
+        dev = dev->next;
+    }
+
+    return dev;
+}
+
+struct icd_drm_device *probe_syspath(struct icd_drm_device *devices,
+                                     struct udev *udev, const char *syspath,
+                                     int vendor_id_match)
+{
+    struct udev_device *minor, *parent;
+    enum icd_drm_minor_type type;
+    const char *parent_syspath;
+    struct icd_drm_device *dev;
+    int vendor, devid;
+
+    minor = udev_device_new_from_syspath(udev, syspath);
+    if (!minor)
+        return devices;
+
+    type = get_minor_type(minor);
+    if (type == ICD_DRM_MINOR_INVALID) {
+        udev_device_unref(minor);
+        return devices;
+    }
+
+    parent = udev_device_get_parent(minor);
+    if (!parent) {
+        udev_device_unref(minor);
+        return devices;
+    }
+
+    get_pci_id(parent, &vendor, &devid);
+    if (vendor_id_match && vendor != vendor_id_match) {
+        udev_device_unref(minor);
+        return devices;
+    }
+
+    parent_syspath = udev_device_get_syspath(parent);
+
+    dev = find_dev(devices, parent_syspath);
+    if (dev) {
+        assert(dev->devid == devid);
+
+        assert(!dev->minors[type]);
+        if (dev->minors[type])
+            udev_device_unref((struct udev_device *) dev->minors[type]);
+
+        dev->minors[type] = (void *) minor;
+
+        return devices;
+    } else {
+        dev = icd_alloc(sizeof(*dev), 0, XGL_SYSTEM_ALLOC_INTERNAL_TEMP);
+        if (!dev)
+            return devices;
+
+        memset(dev, 0, sizeof(*dev));
+
+        dev->id = (const void *) parent_syspath;
+        dev->devid = devid;
+        dev->minors[type] = (void *) minor;
+
+        dev->next = devices;
+
+        return dev;
+    }
+}
+
+struct icd_drm_device *icd_drm_enumerate(int vendor_id)
+{
+    struct icd_drm_device *devices = NULL;
+    struct udev *udev;
+    struct udev_enumerate *e;
+    struct udev_list_entry *entry;
+
+    udev = udev_new();
+    if (udev == NULL) {
+        icd_log(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0,
+                XGL_NULL_HANDLE, 0, 0, "failed to initialize udev context");
+
+        return NULL;
+    }
+
+    e = udev_enumerate_new(udev);
+    if (e == NULL) {
+        icd_log(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0,
+                XGL_NULL_HANDLE, 0, 0,
+                "failed to initialize udev enumerate context");
+        udev_unref(udev);
+
+        return NULL;
+    }
+
+    /* we are interested in DRM minors */
+    udev_enumerate_add_match_subsystem(e, "drm");
+    udev_enumerate_add_match_property(e, "DEVTYPE", "drm_minor");
+    udev_enumerate_scan_devices(e);
+
+    udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
+        devices = probe_syspath(devices, udev,
+                udev_list_entry_get_name(entry), vendor_id);
+    }
+
+    return devices;
+}
+
+void icd_drm_release(struct icd_drm_device *devices)
+{
+    struct icd_drm_device *dev = devices;
+
+    while (dev) {
+        struct icd_drm_device *next = dev->next;
+        int i;
+
+        for (i = 0; i < ARRAY_SIZE(dev->minors); i++)
+            udev_device_unref((struct udev_device *) dev->minors[i]);
+
+        icd_free(dev);
+        dev = next;
+    }
+}
+
+const char *icd_drm_get_devnode(struct icd_drm_device *dev,
+                                enum icd_drm_minor_type minor)
+{
+    return (dev->minors[minor]) ?
+        udev_device_get_devnode((struct udev_device *) dev->minors[minor]) :
+        NULL;
+}
diff --git a/icd/common/icd-enumerate-drm.h b/icd/common/icd-enumerate-drm.h
new file mode 100644 (file)
index 0000000..45883f8
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * XGL
+ *
+ * Copyright (C) 2014 LunarG, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Authors:
+ *   Chia-I Wu <olv@lunarg.com>
+ */
+
+#ifndef ICD_ENUMERATE_DRM_H
+#define ICD_ENUMERATE_DRM_H
+
+enum icd_drm_minor_type {
+    ICD_DRM_MINOR_LEGACY,
+    ICD_DRM_MINOR_RENDER,
+
+    ICD_DRM_MINOR_COUNT,
+    ICD_DRM_MINOR_INVALID,
+};
+
+struct icd_drm_device {
+    const void *id;
+    int devid;
+
+    void *minors[ICD_DRM_MINOR_COUNT];
+
+    struct icd_drm_device *next;
+};
+
+struct icd_drm_device *icd_drm_enumerate(int vendor_id);
+void icd_drm_release(struct icd_drm_device *devices);
+
+const char *icd_drm_get_devnode(struct icd_drm_device *dev,
+                                enum icd_drm_minor_type minor);
+
+#endif /* ICD_ENUMERATE_DRM_H */