1 /**************************************************************************
2 *
3 * Copyright 2011 Intel Corporation
4 * Copyright 2012 Francisco Jerez
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 * Authors:
28 * Kristian Høgsberg <krh@bitplanet.net>
29 * Benjamin Franzke <benjaminfranzke@googlemail.com>
30 *
31 **************************************************************************/
32
33 #include <fcntl.h>
34 #include <stdio.h>
35 #include <libudev.h>
36 #include <xf86drm.h>
37
38 #ifdef PIPE_LOADER_HAVE_XCB
39
40 #include <xcb/dri2.h>
41
42 #endif
43
44 #include "state_tracker/drm_driver.h"
45 #include "pipe_loader_priv.h"
46
47 #include "util/u_memory.h"
48 #include "util/u_dl.h"
49 #include "util/u_debug.h"
50
51 #define DRIVER_MAP_GALLIUM_ONLY
52 #include "pci_ids/pci_id_driver_map.h"
53
54 struct pipe_loader_drm_device {
55 struct pipe_loader_device base;
56 struct util_dl_library *lib;
57 int fd;
58 };
59
60 #define pipe_loader_drm_device(dev) ((struct pipe_loader_drm_device *)dev)
61
62 static boolean
find_drm_pci_id(struct pipe_loader_drm_device * ddev)63 find_drm_pci_id(struct pipe_loader_drm_device *ddev)
64 {
65 struct udev *udev = NULL;
66 struct udev_device *parent, *device = NULL;
67 struct stat stat;
68 const char *pci_id;
69
70 if (fstat(ddev->fd, &stat) < 0)
71 goto fail;
72
73 udev = udev_new();
74 if (!udev)
75 goto fail;
76
77 device = udev_device_new_from_devnum(udev, 'c', stat.st_rdev);
78 if (!device)
79 goto fail;
80
81 parent = udev_device_get_parent(device);
82 if (!parent)
83 goto fail;
84
85 pci_id = udev_device_get_property_value(parent, "PCI_ID");
86 if (!pci_id ||
87 sscanf(pci_id, "%x:%x", &ddev->base.u.pci.vendor_id,
88 &ddev->base.u.pci.chip_id) != 2)
89 goto fail;
90
91 return TRUE;
92
93 fail:
94 if (device)
95 udev_device_unref(device);
96 if (udev)
97 udev_unref(udev);
98
99 debug_printf("pci id for fd %d not found\n", ddev->fd);
100 return FALSE;
101 }
102
103 static boolean
find_drm_driver_name(struct pipe_loader_drm_device * ddev)104 find_drm_driver_name(struct pipe_loader_drm_device *ddev)
105 {
106 struct pipe_loader_device *dev = &ddev->base;
107 int i, j;
108
109 for (i = 0; driver_map[i].driver; i++) {
110 if (dev->u.pci.vendor_id != driver_map[i].vendor_id)
111 continue;
112
113 if (driver_map[i].num_chips_ids == -1) {
114 dev->driver_name = driver_map[i].driver;
115 goto found;
116 }
117
118 for (j = 0; j < driver_map[i].num_chips_ids; j++) {
119 if (dev->u.pci.chip_id == driver_map[i].chip_ids[j]) {
120 dev->driver_name = driver_map[i].driver;
121 goto found;
122 }
123 }
124 }
125
126 return FALSE;
127
128 found:
129 debug_printf("driver for %04x:%04x: %s\n", dev->u.pci.vendor_id,
130 dev->u.pci.chip_id, dev->driver_name);
131 return TRUE;
132 }
133
134 static struct pipe_loader_ops pipe_loader_drm_ops;
135
136 static void
pipe_loader_drm_x_auth(int fd)137 pipe_loader_drm_x_auth(int fd)
138 {
139 #if PIPE_LOADER_HAVE_XCB
140 /* Try authenticate with the X server to give us access to devices that X
141 * is running on. */
142 xcb_connection_t *xcb_conn;
143 const xcb_setup_t *xcb_setup;
144 xcb_screen_iterator_t s;
145 xcb_dri2_connect_cookie_t connect_cookie;
146 xcb_dri2_connect_reply_t *connect;
147 drm_magic_t magic;
148 xcb_dri2_authenticate_cookie_t authenticate_cookie;
149 xcb_dri2_authenticate_reply_t *authenticate;
150
151 xcb_conn = xcb_connect(NULL, NULL);
152
153 if(!xcb_conn)
154 return;
155
156 xcb_setup = xcb_get_setup(xcb_conn);
157
158 if (!xcb_setup)
159 goto disconnect;
160
161 s = xcb_setup_roots_iterator(xcb_setup);
162 connect_cookie = xcb_dri2_connect_unchecked(xcb_conn, s.data->root,
163 XCB_DRI2_DRIVER_TYPE_DRI);
164 connect = xcb_dri2_connect_reply(xcb_conn, connect_cookie, NULL);
165
166 if (!connect || connect->driver_name_length
167 + connect->device_name_length == 0) {
168
169 goto disconnect;
170 }
171
172 if (drmGetMagic(fd, &magic))
173 goto disconnect;
174
175 authenticate_cookie = xcb_dri2_authenticate_unchecked(xcb_conn,
176 s.data->root,
177 magic);
178 authenticate = xcb_dri2_authenticate_reply(xcb_conn,
179 authenticate_cookie,
180 NULL);
181 FREE(authenticate);
182
183 disconnect:
184 xcb_disconnect(xcb_conn);
185
186 #endif
187 }
188
189 boolean
pipe_loader_drm_probe_fd(struct pipe_loader_device ** dev,int fd)190 pipe_loader_drm_probe_fd(struct pipe_loader_device **dev, int fd)
191 {
192 struct pipe_loader_drm_device *ddev = CALLOC_STRUCT(pipe_loader_drm_device);
193
194 ddev->base.type = PIPE_LOADER_DEVICE_PCI;
195 ddev->base.ops = &pipe_loader_drm_ops;
196 ddev->fd = fd;
197
198 pipe_loader_drm_x_auth(fd);
199
200 if (!find_drm_pci_id(ddev))
201 goto fail;
202
203 if (!find_drm_driver_name(ddev))
204 goto fail;
205
206 *dev = &ddev->base;
207 return TRUE;
208
209 fail:
210 FREE(ddev);
211 return FALSE;
212 }
213
214 static int
open_drm_minor(int minor)215 open_drm_minor(int minor)
216 {
217 char path[PATH_MAX];
218 snprintf(path, sizeof(path), DRM_DEV_NAME, DRM_DIR_NAME, minor);
219 return open(path, O_RDWR, 0);
220 }
221
222 int
pipe_loader_drm_probe(struct pipe_loader_device ** devs,int ndev)223 pipe_loader_drm_probe(struct pipe_loader_device **devs, int ndev)
224 {
225 int i, j, fd;
226
227 for (i = 0, j = 0; i < DRM_MAX_MINOR; i++) {
228 fd = open_drm_minor(i);
229 if (fd < 0)
230 continue;
231
232 if (j >= ndev || !pipe_loader_drm_probe_fd(&devs[j], fd))
233 close(fd);
234
235 j++;
236 }
237
238 return j;
239 }
240
241 static void
pipe_loader_drm_release(struct pipe_loader_device ** dev)242 pipe_loader_drm_release(struct pipe_loader_device **dev)
243 {
244 struct pipe_loader_drm_device *ddev = pipe_loader_drm_device(*dev);
245
246 if (ddev->lib)
247 util_dl_close(ddev->lib);
248
249 close(ddev->fd);
250 FREE(ddev);
251 *dev = NULL;
252 }
253
254 static struct pipe_screen *
pipe_loader_drm_create_screen(struct pipe_loader_device * dev,const char * library_paths)255 pipe_loader_drm_create_screen(struct pipe_loader_device *dev,
256 const char *library_paths)
257 {
258 struct pipe_loader_drm_device *ddev = pipe_loader_drm_device(dev);
259 const struct drm_driver_descriptor *dd;
260
261 if (!ddev->lib)
262 ddev->lib = pipe_loader_find_module(dev, library_paths);
263 if (!ddev->lib)
264 return NULL;
265
266 dd = (const struct drm_driver_descriptor *)
267 util_dl_get_proc_address(ddev->lib, "driver_descriptor");
268
269 /* sanity check on the name */
270 if (!dd || strcmp(dd->name, ddev->base.driver_name) != 0)
271 return NULL;
272
273 return dd->create_screen(ddev->fd);
274 }
275
276 static struct pipe_loader_ops pipe_loader_drm_ops = {
277 .create_screen = pipe_loader_drm_create_screen,
278 .release = pipe_loader_drm_release
279 };
280