pipe-loader: use loader_open_device() rather than open()
[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 <xf86drm.h>
36 #include <unistd.h>
37
38 #include "loader.h"
39 #include "state_tracker/drm_driver.h"
40 #include "pipe_loader_priv.h"
41
42 #include "util/u_memory.h"
43 #include "util/u_dl.h"
44 #include "util/u_debug.h"
45
46 #define DRM_RENDER_NODE_DEV_NAME_FORMAT "%s/renderD%d"
47 #define DRM_RENDER_NODE_MAX_NODES 63
48 #define DRM_RENDER_NODE_MIN_MINOR 128
49 #define DRM_RENDER_NODE_MAX_MINOR (DRM_RENDER_NODE_MIN_MINOR + DRM_RENDER_NODE_MAX_NODES)
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 bool
62 pipe_loader_drm_probe_fd(struct pipe_loader_device **dev, int fd)
63 {
64 struct pipe_loader_drm_device *ddev = CALLOC_STRUCT(pipe_loader_drm_device);
65 int vendor_id, chip_id;
66
67 if (!ddev)
68 return false;
69
70 if (loader_get_pci_id_for_fd(fd, &vendor_id, &chip_id)) {
71 ddev->base.type = PIPE_LOADER_DEVICE_PCI;
72 ddev->base.u.pci.vendor_id = vendor_id;
73 ddev->base.u.pci.chip_id = chip_id;
74 } else {
75 ddev->base.type = PIPE_LOADER_DEVICE_PLATFORM;
76 }
77 ddev->base.ops = &pipe_loader_drm_ops;
78 ddev->fd = fd;
79
80 ddev->base.driver_name = loader_get_driver_for_fd(fd, _LOADER_GALLIUM);
81 if (!ddev->base.driver_name)
82 goto fail;
83
84 *dev = &ddev->base;
85 return true;
86
87 fail:
88 FREE(ddev);
89 return false;
90 }
91
92 static int
93 open_drm_render_node_minor(int minor)
94 {
95 char path[PATH_MAX];
96 snprintf(path, sizeof(path), DRM_RENDER_NODE_DEV_NAME_FORMAT, DRM_DIR_NAME,
97 minor);
98 return loader_open_device(path);
99 }
100
101 int
102 pipe_loader_drm_probe(struct pipe_loader_device **devs, int ndev)
103 {
104 struct pipe_loader_device *dev;
105 int i, j, fd;
106
107 for (i = DRM_RENDER_NODE_MIN_MINOR, j = 0;
108 i <= DRM_RENDER_NODE_MAX_MINOR && j < ndev; i++) {
109
110 fd = open_drm_render_node_minor(i);
111 if (fd < 0)
112 continue;
113
114 if (!pipe_loader_drm_probe_fd(&dev, fd)) {
115 close(fd);
116 continue;
117 }
118
119 devs[j++] = dev;
120 }
121
122 return j;
123 }
124
125 static void
126 pipe_loader_drm_release(struct pipe_loader_device **dev)
127 {
128 struct pipe_loader_drm_device *ddev = pipe_loader_drm_device(*dev);
129
130 if (ddev->lib)
131 util_dl_close(ddev->lib);
132
133 close(ddev->fd);
134 FREE(ddev->base.driver_name);
135 FREE(ddev);
136 *dev = NULL;
137 }
138
139 static const struct drm_conf_ret *
140 pipe_loader_drm_configuration(struct pipe_loader_device *dev,
141 enum drm_conf conf)
142 {
143 struct pipe_loader_drm_device *ddev = pipe_loader_drm_device(dev);
144 const struct drm_driver_descriptor *dd;
145
146 if (!ddev->lib)
147 return NULL;
148
149 dd = (const struct drm_driver_descriptor *)
150 util_dl_get_proc_address(ddev->lib, "driver_descriptor");
151
152 /* sanity check on the name */
153 if (!dd || strcmp(dd->name, ddev->base.driver_name) != 0)
154 return NULL;
155
156 if (!dd->configuration)
157 return NULL;
158
159 return dd->configuration(conf);
160 }
161
162 static struct pipe_screen *
163 pipe_loader_drm_create_screen(struct pipe_loader_device *dev,
164 const char *library_paths)
165 {
166 struct pipe_loader_drm_device *ddev = pipe_loader_drm_device(dev);
167 const struct drm_driver_descriptor *dd;
168
169 if (!ddev->lib)
170 ddev->lib = pipe_loader_find_module(dev, library_paths);
171 if (!ddev->lib)
172 return NULL;
173
174 dd = (const struct drm_driver_descriptor *)
175 util_dl_get_proc_address(ddev->lib, "driver_descriptor");
176
177 /* sanity check on the name */
178 if (!dd || strcmp(dd->name, ddev->base.driver_name) != 0)
179 return NULL;
180
181 return dd->create_screen(ddev->fd);
182 }
183
184 static struct pipe_loader_ops pipe_loader_drm_ops = {
185 .create_screen = pipe_loader_drm_create_screen,
186 .configuration = pipe_loader_drm_configuration,
187 .release = pipe_loader_drm_release
188 };