Merge branch 'xa_branch'
[mesa.git] / src / gallium / targets / gbm / pipe_loader.c
1 /*
2 * Copyright © 2011 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Kristian Høgsberg <krh@bitplanet.net>
26 * Benjamin Franzke <benjaminfranzke@googlemail.com>
27 */
28
29 #include <stdio.h>
30 #include "util/u_string.h"
31 #include "util/u_memory.h"
32
33 #include <libudev.h>
34
35 #include "gbm_gallium_drmint.h"
36 #include "pipe_loader.h"
37 #define DRIVER_MAP_GALLIUM_ONLY
38 #include "pci_ids/pci_id_driver_map.h"
39
40 static struct pipe_module pipe_modules[16];
41
42 static INLINE char *
43 loader_strdup(const char *str)
44 {
45 return mem_dup(str, strlen(str) + 1);
46 }
47
48 char *
49 drm_fd_get_screen_name(int fd)
50 {
51 struct udev *udev;
52 struct udev_device *device, *parent;
53 const char *pci_id;
54 char *driver = NULL;
55 int vendor_id, chip_id, i, j;
56
57 udev = udev_new();
58 device = _gbm_udev_device_new_from_fd(udev, fd);
59 if (device == NULL)
60 return NULL;
61
62 parent = udev_device_get_parent(device);
63 if (parent == NULL) {
64 fprintf(stderr, "gbm: could not get parent device");
65 goto out;
66 }
67
68 pci_id = udev_device_get_property_value(parent, "PCI_ID");
69 if (pci_id == NULL ||
70 sscanf(pci_id, "%x:%x", &vendor_id, &chip_id) != 2) {
71 fprintf(stderr, "gbm: malformed or no PCI ID");
72 goto out;
73 }
74
75 for (i = 0; driver_map[i].driver; i++) {
76 if (vendor_id != driver_map[i].vendor_id)
77 continue;
78 if (driver_map[i].num_chips_ids == -1) {
79 driver = loader_strdup(driver_map[i].driver);
80 _gbm_log("pci id for %d: %04x:%04x, driver %s",
81 fd, vendor_id, chip_id, driver);
82 goto out;
83 }
84
85 for (j = 0; j < driver_map[i].num_chips_ids; j++)
86 if (driver_map[i].chip_ids[j] == chip_id) {
87 driver = loader_strdup(driver_map[i].driver);
88 _gbm_log("pci id for %d: %04x:%04x, driver %s",
89 fd, vendor_id, chip_id, driver);
90 goto out;
91 }
92 }
93
94 out:
95 udev_device_unref(device);
96 udev_unref(udev);
97
98 return driver;
99 }
100
101 static void
102 find_pipe_module(struct pipe_module *pmod, const char *name)
103 {
104 char *search_paths, *end, *next, *p;
105 char path[PATH_MAX];
106 int ret;
107
108 search_paths = NULL;
109 if (geteuid() == getuid()) {
110 /* don't allow setuid apps to use GBM_BACKENDS_PATH */
111 search_paths = getenv("GBM_BACKENDS_PATH");
112 }
113 if (search_paths == NULL)
114 search_paths = GBM_BACKEND_SEARCH_DIR;
115
116 end = search_paths + strlen(search_paths);
117 for (p = search_paths; p < end && pmod->lib == NULL; p = next + 1) {
118 int len;
119 next = strchr(p, ':');
120 if (next == NULL)
121 next = end;
122
123 len = next - p;
124
125 if (len) {
126 ret = util_snprintf(path, sizeof(path),
127 "%.*s/" PIPE_PREFIX "%s" UTIL_DL_EXT, len, p, pmod->name);
128 }
129 else {
130 ret = util_snprintf(path, sizeof(path),
131 PIPE_PREFIX "%s" UTIL_DL_EXT, pmod->name);
132 }
133 if (ret > 0 && ret < sizeof(path)) {
134 pmod->lib = util_dl_open(path);
135 debug_printf("loaded %s\n", path);
136 }
137
138 }
139 }
140
141 static boolean
142 load_pipe_module(struct pipe_module *pmod, const char *name)
143 {
144 pmod->name = loader_strdup(name);
145 if (!pmod->name)
146 return FALSE;
147
148 find_pipe_module(pmod, name);
149
150 if (pmod->lib) {
151 pmod->drmdd = (const struct drm_driver_descriptor *)
152 util_dl_get_proc_address(pmod->lib, "driver_descriptor");
153
154 /* sanity check on the name */
155 if (pmod->drmdd && strcmp(pmod->drmdd->name, pmod->name) != 0)
156 pmod->drmdd = NULL;
157
158 if (!pmod->drmdd) {
159 util_dl_close(pmod->lib);
160 pmod->lib = NULL;
161 }
162 }
163
164 return (pmod->drmdd != NULL);
165 }
166
167 struct pipe_module *
168 get_pipe_module(const char *name)
169 {
170 struct pipe_module *pmod = NULL;
171 int i;
172
173 if (!name)
174 return NULL;
175
176 for (i = 0; i < Elements(pipe_modules); i++) {
177 if (!pipe_modules[i].initialized ||
178 strcmp(pipe_modules[i].name, name) == 0) {
179 pmod = &pipe_modules[i];
180 break;
181 }
182 }
183 if (!pmod)
184 return NULL;
185
186 if (!pmod->initialized) {
187 load_pipe_module(pmod, name);
188 pmod->initialized = TRUE;
189 }
190
191 return pmod;
192 }