OSDN Git Service

freedreno: move bo-cache to it's own file
[android-x86/external-libdrm.git] / libkms / linux.c
1 /**************************************************************************
2  *
3  * Copyright © 2009 VMware, Inc., Palo Alto, CA., USA
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 /*
28  * Thanks to krh and jcristau for the tips on
29  * going from fd to pci id via fstat and udev.
30  */
31
32
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36 #include <errno.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <xf86drm.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include <sys/stat.h>
43 #include <sys/types.h>
44 #ifdef MAJOR_IN_MKDEV
45 #include <sys/mkdev.h>
46 #endif
47 #ifdef MAJOR_IN_SYSMACROS
48 #include <sys/sysmacros.h>
49 #endif
50
51 #include "libdrm_macros.h"
52 #include "internal.h"
53
54 #define PATH_SIZE 512
55
56 static int
57 linux_name_from_sysfs(int fd, char **out)
58 {
59         char path[PATH_SIZE+1] = ""; /* initialize to please valgrind */
60         char link[PATH_SIZE+1] = "";
61         struct stat buffer;
62         unsigned maj, min;
63         char* slash_name;
64         int ret;
65
66         /* 
67          * Inside the sysfs directory for the device there is a symlink
68          * to the directory representing the driver module, that path
69          * happens to hold the name of the driver.
70          *
71          * So lets get the symlink for the drm device. Then read the link
72          * and filter out the last directory which happens to be the name
73          * of the driver, which we can use to load the correct interface.
74          *
75          * Thanks to Ray Strode of Plymouth for the code.
76          */
77
78         ret = fstat(fd, &buffer);
79         if (ret)
80                 return -EINVAL;
81
82         if (!S_ISCHR(buffer.st_mode))
83                 return -EINVAL;
84
85         maj = major(buffer.st_rdev);
86         min = minor(buffer.st_rdev);
87
88         snprintf(path, PATH_SIZE, "/sys/dev/char/%d:%d/device/driver", maj, min);
89
90         if (readlink(path, link, PATH_SIZE) < 0)
91                 return -EINVAL;
92
93         /* link looks something like this: ../../../bus/pci/drivers/intel */
94         slash_name = strrchr(link, '/');
95         if (!slash_name)
96                 return -EINVAL;
97
98         /* copy name and at the same time remove the slash */
99         *out = strdup(slash_name + 1);
100         return 0;
101 }
102
103 static int
104 linux_from_sysfs(int fd, struct kms_driver **out)
105 {
106         char *name;
107         int ret;
108
109         ret = linux_name_from_sysfs(fd, &name);
110         if (ret)
111                 return ret;
112
113 #ifdef HAVE_INTEL
114         if (!strcmp(name, "intel"))
115                 ret = intel_create(fd, out);
116         else
117 #endif
118 #ifdef HAVE_VMWGFX
119         if (!strcmp(name, "vmwgfx"))
120                 ret = vmwgfx_create(fd, out);
121         else
122 #endif
123 #ifdef HAVE_NOUVEAU
124         if (!strcmp(name, "nouveau"))
125                 ret = nouveau_create(fd, out);
126         else
127 #endif
128 #ifdef HAVE_RADEON
129         if (!strcmp(name, "radeon"))
130                 ret = radeon_create(fd, out);
131         else
132 #endif
133 #ifdef HAVE_EXYNOS
134         if (!strcmp(name, "exynos"))
135                 ret = exynos_create(fd, out);
136         else
137 #endif
138                 ret = -ENOSYS;
139
140         free(name);
141         return ret;
142 }
143
144 #if 0
145 #define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE
146 #include <libudev.h>
147
148 struct create_record
149 {
150         unsigned vendor;
151         unsigned chip;
152         int (*func)(int fd, struct kms_driver **out);
153 };
154
155 static const struct create_record table[] = {
156         { 0x8086, 0x2a42, intel_create }, /* i965 */
157 #ifdef HAVE_VMWGFX
158         { 0x15ad, 0x0405, vmwgfx_create }, /* VMware vGPU */
159 #endif
160         { 0, 0, NULL },
161 };
162
163 static int
164 linux_get_pciid_from_fd(int fd, unsigned *vendor_id, unsigned *chip_id)
165 {
166         struct udev *udev;
167         struct udev_device *device;
168         struct udev_device *parent;
169         const char *pci_id;
170         struct stat buffer;
171         int ret;
172
173         ret = fstat(fd, &buffer);
174         if (ret)
175                 return -EINVAL;
176
177         if (!S_ISCHR(buffer.st_mode))
178                 return -EINVAL;
179
180         udev = udev_new();
181         if (!udev)
182                 return -ENOMEM;
183
184         device = udev_device_new_from_devnum(udev, 'c', buffer.st_rdev);
185         if (!device)
186                 goto err_free_udev;
187
188         parent = udev_device_get_parent(device);
189         if (!parent)
190                 goto err_free_device;
191
192         pci_id = udev_device_get_property_value(parent, "PCI_ID");
193         if (!pci_id)
194                 goto err_free_device;
195
196         if (sscanf(pci_id, "%x:%x", vendor_id, chip_id) != 2)
197                 goto err_free_device;
198
199         udev_device_unref(device);
200         udev_unref(udev);
201
202         return 0;
203
204 err_free_device:
205         udev_device_unref(device);
206 err_free_udev:
207         udev_unref(udev);
208         return -EINVAL;
209 }
210
211 static int
212 linux_from_udev(int fd, struct kms_driver **out)
213 {
214         unsigned vendor_id, chip_id;
215         int ret, i;
216
217         ret = linux_get_pciid_from_fd(fd, &vendor_id, &chip_id);
218         if (ret)
219                 return ret;
220
221         for (i = 0; table[i].func; i++)
222                 if (table[i].vendor == vendor_id && table[i].chip == chip_id)
223                         return table[i].func(fd, out);
224
225         return -ENOSYS;
226 }
227 #else
228 static int
229 linux_from_udev(int fd, struct kms_driver **out)
230 {
231         return -ENOSYS;
232 }
233 #endif
234
235 drm_private int
236 linux_create(int fd, struct kms_driver **out)
237 {
238         if (!dumb_create(fd, out))
239                 return 0;
240
241         if (!linux_from_udev(fd, out))
242                 return 0;
243
244         return linux_from_sysfs(fd, out);
245 }