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