nine: Add drirc options (v2)
[mesa.git] / src / gallium / targets / d3dadapter9 / drm.c
1 /*
2 * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the 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, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22
23 #include "loader.h"
24
25 #include "adapter9.h"
26
27 #include "pipe-loader/pipe_loader.h"
28
29 #include "pipe/p_screen.h"
30 #include "pipe/p_state.h"
31
32 #include "target-helpers/inline_drm_helper.h"
33 #include "target-helpers/inline_sw_helper.h"
34 #include "state_tracker/drm_driver.h"
35
36 #include "d3dadapter/d3dadapter9.h"
37 #include "d3dadapter/drm.h"
38
39 #include "xmlconfig.h"
40 #include "xmlpool.h"
41
42 #include <libdrm/drm.h>
43 #include <sys/ioctl.h>
44 #include <fcntl.h>
45 #include <stdio.h>
46
47 #define DBG_CHANNEL DBG_ADAPTER
48
49 #define VERSION_DWORD(hi, lo) \
50 ((DWORD)( \
51 ((DWORD)((hi) & 0xFFFF) << 16) | \
52 (DWORD)((lo) & 0xFFFF) \
53 ))
54
55 const char __driConfigOptionsNine[] =
56 DRI_CONF_BEGIN
57 DRI_CONF_SECTION_PERFORMANCE
58 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_1)
59 DRI_CONF_SECTION_END
60 DRI_CONF_SECTION_NINE
61 DRI_CONF_NINE_THROTTLE(-2)
62 DRI_CONF_SECTION_END
63 DRI_CONF_END;
64
65 /* Regarding os versions, we should not define our own as that would simply be
66 * weird. Defaulting to Win2k/XP seems sane considering the origin of D3D9. The
67 * driver also defaults to being a generic D3D9 driver, which of course only
68 * matters if you're actually using the DDI. */
69 #define VERSION_HIGH VERSION_DWORD(0x0006, 0x000E) /* winxp, d3d9 */
70 #define VERSION_LOW VERSION_DWORD(0x0000, 0x0001) /* version, build */
71
72 struct d3dadapter9drm_context
73 {
74 struct d3dadapter9_context base;
75 struct pipe_loader_device *dev, *swdev;
76 };
77
78 static void
79 drm_destroy( struct d3dadapter9_context *ctx )
80 {
81 #if !GALLIUM_STATIC_TARGETS
82 struct d3dadapter9drm_context *drm = (struct d3dadapter9drm_context *)ctx;
83
84 /* pipe_loader_sw destroys the context */
85 if (drm->swdev)
86 pipe_loader_release(&drm->swdev, 1);
87 if (drm->dev)
88 pipe_loader_release(&drm->dev, 1);
89 #endif
90
91 FREE(ctx);
92 }
93
94 /* read a DWORD in the form 0xnnnnnnnn, which is how sysfs pci id stuff is
95 * formatted. */
96 static INLINE DWORD
97 read_file_dword( const char *name )
98 {
99 char buf[32];
100 int fd, r;
101
102 fd = open(name, O_RDONLY);
103 if (fd < 0) {
104 DBG("Unable to get PCI information from `%s'\n", name);
105 return 0;
106 }
107
108 r = read(fd, buf, 32);
109 close(fd);
110
111 return (r > 0) ? (DWORD)strtol(buf, NULL, 0) : 0;
112 }
113
114 /* sysfs doesn't expose the revision as its own file, so this function grabs a
115 * dword at an offset in the raw PCI header. The reason this isn't used for all
116 * data is that the kernel will make corrections but not expose them in the raw
117 * header bytes. */
118 static INLINE DWORD
119 read_config_dword( int fd,
120 unsigned offset )
121 {
122 DWORD r = 0;
123
124 if (lseek(fd, offset, SEEK_SET) != offset) { return 0; }
125 if (read(fd, &r, 4) != 4) { return 0; }
126
127 return r;
128 }
129
130 static INLINE void
131 get_bus_info( int fd,
132 DWORD *vendorid,
133 DWORD *deviceid,
134 DWORD *subsysid,
135 DWORD *revision )
136 {
137 drm_unique_t u;
138
139 u.unique_len = 0;
140 u.unique = NULL;
141
142 if (ioctl(fd, DRM_IOCTL_GET_UNIQUE, &u)) { return; }
143 u.unique = CALLOC(u.unique_len+1, 1);
144
145 if (ioctl(fd, DRM_IOCTL_GET_UNIQUE, &u)) { return; }
146 u.unique[u.unique_len] = '\0';
147
148 DBG("DRM Device BusID: %s\n", u.unique);
149 if (strncmp("pci:", u.unique, 4) == 0) {
150 char fname[512]; /* this ought to be enough */
151 int l = snprintf(fname, 512, "/sys/bus/pci/devices/%s/", u.unique+4);
152
153 /* VendorId */
154 snprintf(fname+l, 512-l, "vendor");
155 *vendorid = read_file_dword(fname);
156 /* DeviceId */
157 snprintf(fname+l, 512-l, "device");
158 *deviceid = read_file_dword(fname);
159 /* SubSysId */
160 snprintf(fname+l, 512-l, "subsystem_device");
161 *subsysid = (read_file_dword(fname) << 16) & 0xFFFF0000;
162 snprintf(fname+l, 512-l, "subsystem_vendor");
163 *subsysid |= read_file_dword(fname) & 0x0000FFFF;
164 /* Revision */
165 {
166 int cfgfd;
167
168 snprintf(fname+l, 512-l, "config");
169 cfgfd = open(fname, O_RDONLY);
170 if (cfgfd >= 0) {
171 *revision = read_config_dword(cfgfd, 0x8) & 0x000000FF;
172 close(cfgfd);
173 } else {
174 DBG("Unable to get raw PCI information from `%s'\n", fname);
175 }
176 }
177 DBG("PCI info: vendor=0x%04x, device=0x%04x, subsys=0x%08x, rev=%d\n",
178 *vendorid, *deviceid, *subsysid, *revision);
179 } else {
180 DBG("Unsupported BusID type.\n");
181 }
182
183 FREE(u.unique);
184 }
185
186 static INLINE void
187 read_descriptor( struct d3dadapter9_context *ctx,
188 int fd )
189 {
190 D3DADAPTER_IDENTIFIER9 *drvid = &ctx->identifier;
191
192 memset(drvid, 0, sizeof(*drvid));
193 get_bus_info(fd, &drvid->VendorId, &drvid->DeviceId,
194 &drvid->SubSysId, &drvid->Revision);
195
196 strncpy(drvid->Driver, "libd3dadapter9.so", sizeof(drvid->Driver));
197 strncpy(drvid->DeviceName, ctx->hal->get_name(ctx->hal), 32);
198 snprintf(drvid->Description, sizeof(drvid->Description),
199 "Gallium 0.4 with %s", ctx->hal->get_vendor(ctx->hal));
200
201 drvid->DriverVersionLowPart = VERSION_LOW;
202 drvid->DriverVersionHighPart = VERSION_HIGH;
203
204 /* To make a pseudo-real GUID we use the PCI bus data and some string */
205 drvid->DeviceIdentifier.Data1 = drvid->VendorId;
206 drvid->DeviceIdentifier.Data2 = drvid->DeviceId;
207 drvid->DeviceIdentifier.Data3 = drvid->SubSysId;
208 memcpy(drvid->DeviceIdentifier.Data4, "Gallium3D", 8);
209
210 drvid->WHQLLevel = 1; /* This fakes WHQL validaion */
211
212 /* XXX Fake NVIDIA binary driver on Windows.
213 *
214 * OS version: 4=95/98/NT4, 5=2000, 6=2000/XP, 7=Vista, 8=Win7
215 */
216 strncpy(drvid->Driver, "nvd3dum.dll", sizeof(drvid->Driver));
217 strncpy(drvid->Description, "NVIDIA GeForce GTX 680", sizeof(drvid->Description));
218 drvid->DriverVersionLowPart = VERSION_DWORD(12, 6658); /* minor, build */
219 drvid->DriverVersionHighPart = VERSION_DWORD(6, 15); /* OS, major */
220 drvid->SubSysId = 0;
221 drvid->Revision = 0;
222 drvid->DeviceIdentifier.Data1 = 0xaeb2cdd4;
223 drvid->DeviceIdentifier.Data2 = 0x6e41;
224 drvid->DeviceIdentifier.Data3 = 0x43ea;
225 drvid->DeviceIdentifier.Data4[0] = 0x94;
226 drvid->DeviceIdentifier.Data4[1] = 0x1c;
227 drvid->DeviceIdentifier.Data4[2] = 0x83;
228 drvid->DeviceIdentifier.Data4[3] = 0x61;
229 drvid->DeviceIdentifier.Data4[4] = 0xcc;
230 drvid->DeviceIdentifier.Data4[5] = 0x76;
231 drvid->DeviceIdentifier.Data4[6] = 0x07;
232 drvid->DeviceIdentifier.Data4[7] = 0x81;
233 drvid->WHQLLevel = 0;
234 }
235
236 static HRESULT WINAPI
237 drm_create_adapter( int fd,
238 ID3DAdapter9 **ppAdapter )
239 {
240 struct d3dadapter9drm_context *ctx = CALLOC_STRUCT(d3dadapter9drm_context);
241 HRESULT hr;
242 int i, different_device;
243 const struct drm_conf_ret *throttle_ret = NULL;
244 const struct drm_conf_ret *dmabuf_ret = NULL;
245 driOptionCache defaultInitOptions;
246 driOptionCache userInitOptions;
247 int throttling_value_user;
248
249 #if !GALLIUM_STATIC_TARGETS
250 const char *paths[] = {
251 getenv("D3D9_DRIVERS_PATH"),
252 getenv("D3D9_DRIVERS_DIR"),
253 PIPE_SEARCH_DIR
254 };
255 #endif
256
257 if (!ctx) { return E_OUTOFMEMORY; }
258
259 ctx->base.destroy = drm_destroy;
260
261 fd = loader_get_user_preferred_fd(fd, &different_device);
262 ctx->base.linear_framebuffer = !!different_device;
263
264 #if GALLIUM_STATIC_TARGETS
265 ctx->base.hal = dd_create_screen(fd);
266 #else
267 /* use pipe-loader to dlopen appropriate drm driver */
268 if (!pipe_loader_drm_probe_fd(&ctx->dev, fd, FALSE)) {
269 ERR("Failed to probe drm fd %d.\n", fd);
270 FREE(ctx);
271 close(fd);
272 return D3DERR_DRIVERINTERNALERROR;
273 }
274
275 /* use pipe-loader to create a drm screen (hal) */
276 ctx->base.hal = NULL;
277 for (i = 0; !ctx->base.hal && i < Elements(paths); ++i) {
278 if (!paths[i]) { continue; }
279 ctx->base.hal = pipe_loader_create_screen(ctx->dev, paths[i]);
280 }
281 #endif
282 if (!ctx->base.hal) {
283 ERR("Unable to load requested driver.\n");
284 drm_destroy(&ctx->base);
285 return D3DERR_DRIVERINTERNALERROR;
286 }
287
288 #if GALLIUM_STATIC_TARGETS
289 dmabuf_ret = dd_configuration(DRM_CONF_SHARE_FD);
290 throttle_ret = dd_configuration(DRM_CONF_THROTTLE);
291 #else
292 dmabuf_ret = pipe_loader_configuration(ctx->dev, DRM_CONF_SHARE_FD);
293 throttle_ret = pipe_loader_configuration(ctx->dev, DRM_CONF_THROTTLE);
294 #endif // GALLIUM_STATIC_TARGETS
295 if (!dmabuf_ret || !dmabuf_ret->val.val_bool) {
296 ERR("The driver is not capable of dma-buf sharing."
297 "Abandon to load nine state tracker\n");
298 drm_destroy(&ctx->base);
299 return D3DERR_DRIVERINTERNALERROR;
300 }
301
302 if (throttle_ret && throttle_ret->val.val_int != -1) {
303 ctx->base.throttling = TRUE;
304 ctx->base.throttling_value = throttle_ret->val.val_int;
305 } else
306 ctx->base.throttling = FALSE;
307
308 driParseOptionInfo(&defaultInitOptions, __driConfigOptionsNine);
309 driParseConfigFiles(&userInitOptions, &defaultInitOptions, 0, "nine");
310 if (driCheckOption(&userInitOptions, "throttle_value", DRI_INT)) {
311 throttling_value_user = driQueryOptioni(&userInitOptions, "throttle_value");
312 if (throttling_value_user == -1)
313 ctx->base.throttling = FALSE;
314 else if (throttling_value_user >= 0) {
315 ctx->base.throttling = TRUE;
316 ctx->base.throttling_value = throttling_value_user;
317 }
318 }
319
320 if (driCheckOption(&userInitOptions, "vblank_mode", DRI_ENUM))
321 ctx->base.vblank_mode = driQueryOptioni(&userInitOptions, "vblank_mode");
322 else
323 ctx->base.vblank_mode = 1;
324
325 driDestroyOptionCache(&userInitOptions);
326 driDestroyOptionInfo(&defaultInitOptions);
327
328 #if GALLIUM_STATIC_TARGETS
329 ctx->base.ref = ninesw_create_screen(ctx->base.hal);
330 #else
331 /* wrap it to create a software screen that can share resources */
332 if (pipe_loader_sw_probe_wrapped(&ctx->swdev, ctx->base.hal)) {
333 ctx->base.ref = NULL;
334 for (i = 0; !ctx->base.ref && i < Elements(paths); ++i) {
335 if (!paths[i]) { continue; }
336 ctx->base.ref = pipe_loader_create_screen(ctx->swdev, paths[i]);
337 }
338 }
339 #endif
340 if (!ctx->base.ref) {
341 ERR("Couldn't wrap drm screen to swrast screen. Software devices "
342 "will be unavailable.\n");
343 }
344
345 /* read out PCI info */
346 read_descriptor(&ctx->base, fd);
347
348 /* create and return new ID3DAdapter9 */
349 hr = NineAdapter9_new(&ctx->base, (struct NineAdapter9 **)ppAdapter);
350 if (FAILED(hr)) {
351 drm_destroy(&ctx->base);
352 return hr;
353 }
354
355 return D3D_OK;
356 }
357
358 const struct D3DAdapter9DRM drm9_desc = {
359 .major_version = D3DADAPTER9DRM_MAJOR,
360 .minor_version = D3DADAPTER9DRM_MINOR,
361 .create_adapter = drm_create_adapter
362 };