Add DRI3+Present loader
[mesa.git] / src / glx / dri3_common.c
1 /*
2 * Copyright © 2013 Keith Packard
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
23 /*
24 * This code is derived from src/egl/drivers/dri2/common.c which
25 * carries the following copyright:
26 *
27 * Copyright © 2011 Intel Corporation
28 *
29 * Permission is hereby granted, free of charge, to any person obtaining a
30 * copy of this software and associated documentation files (the "Software"),
31 * to deal in the Software without restriction, including without limitation
32 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
33 * and/or sell copies of the Software, and to permit persons to whom the
34 * Software is furnished to do so, subject to the following conditions:
35 *
36 * The above copyright notice and this permission notice (including the next
37 * paragraph) shall be included in all copies or substantial portions of the
38 * Software.
39 *
40 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
41 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
42 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
43 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
44 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
45 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
46 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
47 * DEALINGS IN THE SOFTWARE.
48 *
49 * Authors:
50 * Kristian Høgsberg <krh@bitplanet.net>
51 * Benjamin Franzke <benjaminfranzke@googlemail.com>
52 */
53
54 #include <stdio.h>
55 #include <string.h>
56
57 #include <sys/types.h>
58 #include <sys/stat.h>
59 #include <unistd.h>
60 #include <GL/gl.h>
61 #include "glapi.h"
62 #include "glxclient.h"
63 #include "xf86dri.h"
64 #include <dlfcn.h>
65 #include <fcntl.h>
66 #include <unistd.h>
67 #include <sys/types.h>
68 #include <sys/mman.h>
69 #include <sys/time.h>
70 #include "xf86drm.h"
71 #include "dri_common.h"
72 #include "dri3_priv.h"
73
74 #define DRIVER_MAP_DRI3_ONLY
75 #include "pci_ids/pci_id_driver_map.h"
76
77 #include <libudev.h>
78
79 static struct udev_device *
80 dri3_udev_device_new_from_fd(struct udev *udev, int fd)
81 {
82 struct udev_device *device;
83 struct stat buf;
84
85 if (fstat(fd, &buf) < 0) {
86 ErrorMessageF("DRI3: failed to stat fd %d", fd);
87 return NULL;
88 }
89
90 device = udev_device_new_from_devnum(udev, 'c', buf.st_rdev);
91 if (device == NULL) {
92 ErrorMessageF("DRI3: could not create udev device for fd %d", fd);
93 return NULL;
94 }
95
96 return device;
97 }
98
99 char *
100 dri3_get_driver_for_fd(int fd)
101 {
102 struct udev *udev;
103 struct udev_device *device, *parent;
104 const char *pci_id;
105 char *driver = NULL;
106 int vendor_id, chip_id, i, j;
107
108 udev = udev_new();
109 device = dri3_udev_device_new_from_fd(udev, fd);
110 if (device == NULL)
111 return NULL;
112
113 parent = udev_device_get_parent(device);
114 if (parent == NULL) {
115 ErrorMessageF("DRI3: could not get parent device");
116 goto out;
117 }
118
119 pci_id = udev_device_get_property_value(parent, "PCI_ID");
120 if (pci_id == NULL ||
121 sscanf(pci_id, "%x:%x", &vendor_id, &chip_id) != 2) {
122 ErrorMessageF("DRI3: malformed or no PCI ID");
123 goto out;
124 }
125
126 for (i = 0; driver_map[i].driver; i++) {
127 if (vendor_id != driver_map[i].vendor_id)
128 continue;
129 if (driver_map[i].num_chips_ids == -1) {
130 driver = strdup(driver_map[i].driver);
131 goto out;
132 }
133
134 for (j = 0; j < driver_map[i].num_chips_ids; j++)
135 if (driver_map[i].chip_ids[j] == chip_id) {
136 driver = strdup(driver_map[i].driver);
137 goto out;
138 }
139 }
140
141 out:
142 udev_device_unref(device);
143 udev_unref(udev);
144
145 return driver;
146 }