blob: 9941b65f09d9195ec93d846e4b28142ef67f34cc [file] [log] [blame]
Chia-I Wu412ce1b2014-09-15 13:08:49 +08001/*
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002 * Vulkan
Chia-I Wu412ce1b2014-09-15 13:08:49 +08003 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
26 */
27
28#include <stdlib.h>
29#include <stdio.h>
30#include <string.h>
31#include <libudev.h>
32
Chia-I Wu15517d82015-02-20 13:41:57 -070033#include "icd-instance.h"
Chia-I Wu412ce1b2014-09-15 13:08:49 +080034#include "icd-utils.h"
35#include "icd-enumerate-drm.h"
36
37static enum icd_drm_minor_type get_minor_type(struct udev_device *minor_dev)
38{
39 const char *minor;
40
41 minor = udev_device_get_property_value(minor_dev, "MINOR");
42 if (!minor)
43 return ICD_DRM_MINOR_INVALID;
44
45 switch (atoi(minor) >> 6) {
46 case 0:
47 return ICD_DRM_MINOR_LEGACY;
48 case 2:
49 return ICD_DRM_MINOR_RENDER;
50 default:
51 return ICD_DRM_MINOR_INVALID;
52 }
53}
54
55static void get_pci_id(struct udev_device *pci_dev, int *vendor, int *devid)
56{
57 const char *pci_id;
58
59 pci_id = udev_device_get_property_value(pci_dev, "PCI_ID");
60 if (sscanf(pci_id, "%x:%x", vendor, devid) != 2) {
61 *vendor = 0;
62 *devid = 0;
63 }
64}
65
66static struct icd_drm_device *find_dev(struct icd_drm_device *devices,
67 const char *parent_syspath)
68{
69 struct icd_drm_device *dev = devices;
70
71 while (dev) {
72 if (!strcmp((const char *) dev->id, parent_syspath))
73 break;
74 dev = dev->next;
75 }
76
77 return dev;
78}
79
Chia-I Wu15517d82015-02-20 13:41:57 -070080static struct icd_drm_device *probe_syspath(const struct icd_instance *instance,
81 struct icd_drm_device *devices,
Chia-I Wu776ed422015-01-02 22:56:56 +080082 struct udev *udev, const char *syspath,
83 int vendor_id_match)
Chia-I Wu412ce1b2014-09-15 13:08:49 +080084{
85 struct udev_device *minor, *parent;
86 enum icd_drm_minor_type type;
87 const char *parent_syspath;
88 struct icd_drm_device *dev;
89 int vendor, devid;
90
91 minor = udev_device_new_from_syspath(udev, syspath);
92 if (!minor)
93 return devices;
94
95 type = get_minor_type(minor);
96 if (type == ICD_DRM_MINOR_INVALID) {
97 udev_device_unref(minor);
98 return devices;
99 }
100
101 parent = udev_device_get_parent(minor);
102 if (!parent) {
103 udev_device_unref(minor);
104 return devices;
105 }
106
107 get_pci_id(parent, &vendor, &devid);
108 if (vendor_id_match && vendor != vendor_id_match) {
109 udev_device_unref(minor);
110 return devices;
111 }
112
113 parent_syspath = udev_device_get_syspath(parent);
114
115 dev = find_dev(devices, parent_syspath);
116 if (dev) {
117 assert(dev->devid == devid);
118
119 assert(!dev->minors[type]);
120 if (dev->minors[type])
121 udev_device_unref((struct udev_device *) dev->minors[type]);
122
123 dev->minors[type] = (void *) minor;
124
125 return devices;
126 } else {
Chia-I Wu15517d82015-02-20 13:41:57 -0700127 dev = icd_instance_alloc(instance, sizeof(*dev), 0,
Tony Barbour8205d902015-04-16 15:59:00 -0600128 VK_SYSTEM_ALLOC_TYPE_INTERNAL_TEMP);
Chia-I Wu412ce1b2014-09-15 13:08:49 +0800129 if (!dev)
130 return devices;
131
132 memset(dev, 0, sizeof(*dev));
133
134 dev->id = (const void *) parent_syspath;
135 dev->devid = devid;
136 dev->minors[type] = (void *) minor;
137
138 dev->next = devices;
139
140 return dev;
141 }
142}
143
Chia-I Wu15517d82015-02-20 13:41:57 -0700144struct icd_drm_device *icd_drm_enumerate(const struct icd_instance *instance,
145 int vendor_id)
Chia-I Wu412ce1b2014-09-15 13:08:49 +0800146{
147 struct icd_drm_device *devices = NULL;
148 struct udev *udev;
149 struct udev_enumerate *e;
150 struct udev_list_entry *entry;
151
152 udev = udev_new();
153 if (udev == NULL) {
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600154 icd_instance_log(instance, VK_DBG_REPORT_ERROR_BIT,
155 0, VK_NULL_HANDLE, /* obj_type, object */
156 0, 0, /* location, msg_code */
157 "failed to initialize udev context");
Chia-I Wu412ce1b2014-09-15 13:08:49 +0800158
159 return NULL;
160 }
161
162 e = udev_enumerate_new(udev);
163 if (e == NULL) {
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600164 icd_instance_log(instance, VK_DBG_REPORT_ERROR_BIT,
165 0, VK_NULL_HANDLE, /* obj_type, object */
166 0, 0, /* location, msg_code */
167 "failed to initialize udev enumerate context");
Chia-I Wu412ce1b2014-09-15 13:08:49 +0800168 udev_unref(udev);
169
170 return NULL;
171 }
172
173 /* we are interested in DRM minors */
174 udev_enumerate_add_match_subsystem(e, "drm");
175 udev_enumerate_add_match_property(e, "DEVTYPE", "drm_minor");
176 udev_enumerate_scan_devices(e);
177
178 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
Chia-I Wu15517d82015-02-20 13:41:57 -0700179 devices = probe_syspath(instance, devices, udev,
Chia-I Wu412ce1b2014-09-15 13:08:49 +0800180 udev_list_entry_get_name(entry), vendor_id);
181 }
182
Courtney Goeltzenleuchtere0f35a72014-09-25 18:13:21 -0600183 free(e);
184 free(udev);
Chia-I Wu412ce1b2014-09-15 13:08:49 +0800185 return devices;
186}
187
Chia-I Wu15517d82015-02-20 13:41:57 -0700188void icd_drm_release(const struct icd_instance *instance,
189 struct icd_drm_device *devices)
Chia-I Wu412ce1b2014-09-15 13:08:49 +0800190{
191 struct icd_drm_device *dev = devices;
192
193 while (dev) {
194 struct icd_drm_device *next = dev->next;
Ian Elliott642f8922015-02-13 14:29:21 -0700195 size_t i;
Chia-I Wu412ce1b2014-09-15 13:08:49 +0800196
197 for (i = 0; i < ARRAY_SIZE(dev->minors); i++)
198 udev_device_unref((struct udev_device *) dev->minors[i]);
199
Chia-I Wu15517d82015-02-20 13:41:57 -0700200 icd_instance_free(instance, dev);
Chia-I Wu412ce1b2014-09-15 13:08:49 +0800201 dev = next;
202 }
203}
204
205const char *icd_drm_get_devnode(struct icd_drm_device *dev,
206 enum icd_drm_minor_type minor)
207{
208 return (dev->minors[minor]) ?
209 udev_device_get_devnode((struct udev_device *) dev->minors[minor]) :
210 NULL;
211}