1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Copyright 2017 Gordon W. Ross
*/
/*
* Helper functions for sun_xf86drm.c to find DRM device nodes
* given a pair of device major/minor numbers.
*
* Some of this code was cribbed from the "prtconf" command.
*/
/*
* Device major numbers on illumos and Solaris are dynamic.
*
* Device node naming is also different. See:
* uts/common/io/drm/drm_stub.c drm_get_minor()
* uts/common/io/drm/drm_sysfs.c drm_sysfs_device_add()
*
* Device "types" (encoded in some of the minor bits)
* Typical device setup with intel graphics:
*
* DRM_MINOR_LEGACY /devices/pci@0,0/display@2:drm0
* DRM_MINOR_CONTROL /devices/pci@0,0/display@2:controlD0
* DRM_MINOR_RENDER /devices/pci@0,0/display@2:renderD0
* DRM_MINOR_VGATEXT /devices/pci@0,0/display@2:gfx0
* DRM_MINOR_AGPMASTER /devices/pci@0,0/display@2:agpmaster0
*
* The "devlinks" system also makes links under /dev
* which normally look something like this:
*
* /dev/fbs/gfx0 -> .../display@2:gfx0
* /dev/fb -> .../display@2:gfx0
* /dev/fb0 -> fbs/text-0
* /dev/fb1 -> fbs/gfx0
* /dev/agp/agpmaster0 -> .../display@2:agpmaster0
* /dev/dri/card1 -> .../display@2:controlD0
*
* (i916 driver) /devices/pci@0,0/pci1179,1@0:agptarget
* /dev/agp/agptarget0 -> /devices/pci@0,0/pci1179,1@0:agptarget
*
* (agpgart driver) /devices/agpgart:agpgart
* /dev/agpgart -> /devices/agpgart:agpgart
*/
#include <string.h>
#include <fcntl.h>
#include <fnmatch.h>
#include <libdevinfo.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/mkdev.h>
#include "xf86drm.h"
struct search_args {
char *s_path;
int s_minor;
};
static int _sun_drm_major; /* cache */
/*
* Search callback function called for each minor node under
* some device that was found to be of possible interest.
* Return non-zero if match found.
*/
static int
find_minor(di_node_t node, di_minor_t minor, struct search_args *sargs)
{
char *path;
dev_t devt;
int ret;
devt = di_minor_devt(minor);
/* Does the caller want a specific minor number? */
if (sargs->s_minor >= 0 &&
sargs->s_minor != minor(devt))
return (0);
/*
* get device minor node path
* Note: allocates path
*/
if ((path = di_devfs_minor_path(minor)) == NULL)
return (0);
ret = asprintf(&sargs->s_path, "/devices%s", path);
di_devfs_path_free(path);
if (ret < 0) {
free(sargs->s_path);
return (0);
}
return (1);
}
/*
* Call back function for di_walk_node, called for every device node.
*/
static int
find_dev(di_node_t node, void *vargs)
{
struct search_args *sargs = vargs;
const char *node_name;
di_minor_t minor_node;
node_name = di_node_name(node);
if (strcmp(node_name, "pseudo") == 0)
return (DI_WALK_PRUNECHILD);
/*
* Had: udev_enumerate_add_match_subsystem(e, "drm");
* so skip anything outside "drm".
* For illumos or Solaris, I think we want to skip
* anything that's not named "display".
*/
if (strcmp(node_name, "display") != 0)
return (DI_WALK_CONTINUE);
/*
* Walk the minor node paths searching...
*/
minor_node = DI_MINOR_NIL;
while ((minor_node = di_minor_next(node, minor_node)) != DI_MINOR_NIL) {
if (find_minor(node, minor_node, sargs)) {
/* Found it! */
return (DI_WALK_TERMINATE);
}
}
return (DI_WALK_CONTINUE);
}
/*
* Helper function for xf86drm.c
* drmGetMinorNameForFD()
* drmParseSubsystemType()
* drmParsePciBusInfo()
* drmParsePciDeviceInfo()
*
* Given a device minor number, find the /devices path.
* Returns malloc'ed memory at *pathp, caller frees.
*/
int
_sun_drm_find_device(int min, char **pathp)
{
struct search_args sargs;
di_node_t root_node;
root_node = di_init("/", DINFOCPYALL);
if (root_node == DI_NODE_NIL)
return (-errno);
memset(&sargs, 0, sizeof (sargs));
di_walk_node(root_node, DI_WALK_CLDFIRST, &sargs, find_dev);
di_fini(root_node);
if (sargs.s_path == NULL)
return (-ENOENT);
if (pathp != NULL)
*pathp = sargs.s_path;
else
free(sargs.s_path);
return (0);
}
/*
* Helper function for DRM_MAJOR in xf86drm.c
* Return the major number assigned to the drm driver.
*/
int
_sun_drm_get_major(void)
{
struct stat sbuf;
dev_t dev = 0;
char *path;
int i, ret;
if (_sun_drm_major != 0)
return (_sun_drm_major);
for (i = 0; i < DRM_MAX_MINOR; i++) {
ret = _sun_drm_find_device(i, &path);
if (ret != 0)
continue;
ret = stat(path, &sbuf);
free(path);
if (ret != 0)
continue;
if (!S_ISCHR(sbuf.st_mode))
continue;
dev = major(sbuf.st_rdev);
if (dev != 0) {
_sun_drm_major = dev;
return (dev);
}
}
/*
* No devices found? No way to return errors here,
* so just return an impossible value, and let
* later calls like open fail.
*/
return (MAXMAJ32);
}
/*
* Helper function for drmParseSubsystemType()
* Returns one of: DRM_BUS_PCI, ... or -EINVAL.
* Our only driver implementations currently
* are on PCI. Others todo.
*/
int
_sun_drm_get_subsystem(char *path)
{
char *p;
int err;
p = path;
if (strncmp(p, "/devices/", 9) == 0)
p += 8;
if (strncmp(p, "/pci", 4) == 0)
err = DRM_BUS_PCI;
else
err = -EINVAL;
return (err);
}
/*
* Helper function for drmParsePciBusInfo()
*
* Get PCI bus info for the give device path.
*/
int
_sun_drm_get_pci_bus_info(char *path, drmPciBusInfo *info)
{
int n, bus, slot, unit;
/* Skip the /devices prefix, if present. */
if (strncmp(path, "/devices/", 9) == 0)
path += 8; /* the next slash */
n = sscanf(path, "/pci@%d,%d/display@%d:",
&bus, &slot, &unit);
if (n != 3)
return (-EINVAL);
info->domain = 0;
info->bus = bus;
info->dev = slot;
info->func = unit;
return (0);
}
/*
* Helper function for drmParsePciDeviceInfo()
*
* Get PCI data for the give device path.
*
* Note path given is a full minor under /devices i.e.
* /devices/pci@0,0/display@2:drm
* and libdevinfo wants just:
* /pci@0,0/display@2
*/
int
_sun_drm_get_pci_dev_info(char *path, drmPciDeviceInfo *pcii)
{
char pathbuf[MAXPATHLEN];
di_node_t node;
char *s;
int *propval = NULL;
int ret = -EINVAL;
/* Skip the /devices prefix, if present. */
if (strncmp(path, "/devices/", 9) == 0)
path += 8; /* the next slash */
strlcpy(pathbuf, path, sizeof (pathbuf));
/* Strip :drm0 or whatever */
if ((s = strrchr(pathbuf, ':')) != NULL)
*s = '\0';
/*
* Ask libdevinfo about this device
*/
node = di_init(pathbuf, DINFOCPYALL);
if (node == DI_NODE_NIL)
return (-EINVAL);
/*
* Get the various PCI properties.
* Only the first two are required.
*/
memset(pcii, 0, sizeof (*pcii));
if (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
"vendor-id", &propval) > 0)
pcii->vendor_id = (uint16_t)*propval;
else
goto out;
if (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
"device-id", &propval) > 0)
pcii->device_id = (uint16_t)*propval;
else
goto out;
if (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
"subsystem-vendor-id", &propval) > 0)
pcii->subvendor_id = (uint16_t)*propval;
if (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
"subsystem-id", &propval) > 0)
pcii->subdevice_id = (uint16_t)*propval;
if (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
"revision-id", &propval) > 0)
pcii->revision_id = (uint16_t)*propval;
ret = 0;
out:
di_fini(node);
return (ret);
}
|