pipe-loader: use loader util lib
[mesa.git] / src / gallium / auxiliary / pipe-loader / pipe_loader_drm.c
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 VMWARE 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 HAVE_PIPE_LOADER_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 struct pipe_loader_drm_device {
52 struct pipe_loader_device base;
53 struct util_dl_library *lib;
54 int fd;
55 };
56
57 #define pipe_loader_drm_device(dev) ((struct pipe_loader_drm_device *)dev)
58
59 static struct pipe_loader_ops pipe_loader_drm_ops;
60
61 static void
62 pipe_loader_drm_x_auth(int fd)
63 {
64 #if HAVE_PIPE_LOADER_XCB
65 /* Try authenticate with the X server to give us access to devices that X
66 * is running on. */
67 xcb_connection_t *xcb_conn;
68 const xcb_setup_t *xcb_setup;
69 xcb_screen_iterator_t s;
70 xcb_dri2_connect_cookie_t connect_cookie;
71 xcb_dri2_connect_reply_t *connect;
72 drm_magic_t magic;
73 xcb_dri2_authenticate_cookie_t authenticate_cookie;
74 xcb_dri2_authenticate_reply_t *authenticate;
75
76 xcb_conn = xcb_connect(NULL, NULL);
77
78 if(!xcb_conn)
79 return;
80
81 xcb_setup = xcb_get_setup(xcb_conn);
82
83 if (!xcb_setup)
84 goto disconnect;
85
86 s = xcb_setup_roots_iterator(xcb_setup);
87 connect_cookie = xcb_dri2_connect_unchecked(xcb_conn, s.data->root,
88 XCB_DRI2_DRIVER_TYPE_DRI);
89 connect = xcb_dri2_connect_reply(xcb_conn, connect_cookie, NULL);
90
91 if (!connect || connect->driver_name_length
92 + connect->device_name_length == 0) {
93
94 goto disconnect;
95 }
96
97 if (drmGetMagic(fd, &magic))
98 goto disconnect;
99
100 authenticate_cookie = xcb_dri2_authenticate_unchecked(xcb_conn,
101 s.data->root,
102 magic);
103 authenticate = xcb_dri2_authenticate_reply(xcb_conn,
104 authenticate_cookie,
105 NULL);
106 FREE(authenticate);
107
108 disconnect:
109 xcb_disconnect(xcb_conn);
110
111 #endif
112 }
113
114 boolean
115 pipe_loader_drm_probe_fd(struct pipe_loader_device **dev, int fd)
116 {
117 struct pipe_loader_drm_device *ddev = CALLOC_STRUCT(pipe_loader_drm_device);
118 int vendor_id, chip_id;
119
120 if (loader_get_pci_id_for_fd(fd, &vendor_id, &chip_id)) {
121 ddev->base.type = PIPE_LOADER_DEVICE_PCI;
122 ddev->base.u.pci.vendor_id = vendor_id;
123 ddev->base.u.pci.chip_id = chip_id;
124 }
125 ddev->base.ops = &pipe_loader_drm_ops;
126 ddev->fd = fd;
127
128 pipe_loader_drm_x_auth(fd);
129
130 ddev->base.driver_name = loader_get_driver_for_fd(fd, _LOADER_GALLIUM);
131 if (!ddev->base.driver_name)
132 goto fail;
133
134 *dev = &ddev->base;
135 return TRUE;
136
137 fail:
138 FREE(ddev);
139 return FALSE;
140 }
141
142 static int
143 open_drm_minor(int minor)
144 {
145 char path[PATH_MAX];
146 snprintf(path, sizeof(path), DRM_DEV_NAME, DRM_DIR_NAME, minor);
147 return open(path, O_RDWR, 0);
148 }
149
150 int
151 pipe_loader_drm_probe(struct pipe_loader_device **devs, int ndev)
152 {
153 int i, j, fd;
154
155 for (i = 0, j = 0; i < DRM_MAX_MINOR; i++) {
156 fd = open_drm_minor(i);
157 if (fd < 0)
158 continue;
159
160 if (j >= ndev || !pipe_loader_drm_probe_fd(&devs[j], fd))
161 close(fd);
162
163 j++;
164 }
165
166 return j;
167 }
168
169 static void
170 pipe_loader_drm_release(struct pipe_loader_device **dev)
171 {
172 struct pipe_loader_drm_device *ddev = pipe_loader_drm_device(*dev);
173
174 if (ddev->lib)
175 util_dl_close(ddev->lib);
176
177 close(ddev->fd);
178 FREE(ddev);
179 *dev = NULL;
180 }
181
182 static struct pipe_screen *
183 pipe_loader_drm_create_screen(struct pipe_loader_device *dev,
184 const char *library_paths)
185 {
186 struct pipe_loader_drm_device *ddev = pipe_loader_drm_device(dev);
187 const struct drm_driver_descriptor *dd;
188
189 if (!ddev->lib)
190 ddev->lib = pipe_loader_find_module(dev, library_paths);
191 if (!ddev->lib)
192 return NULL;
193
194 dd = (const struct drm_driver_descriptor *)
195 util_dl_get_proc_address(ddev->lib, "driver_descriptor");
196
197 /* sanity check on the name */
198 if (!dd || strcmp(dd->name, ddev->base.driver_name) != 0)
199 return NULL;
200
201 return dd->create_screen(ddev->fd);
202 }
203
204 static struct pipe_loader_ops pipe_loader_drm_ops = {
205 .create_screen = pipe_loader_drm_create_screen,
206 .release = pipe_loader_drm_release
207 };