nine: Implement threadpool
[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_NINE_THREADSUBMIT("false")
63 DRI_CONF_SECTION_END
64 DRI_CONF_END;
65
66 /* Regarding os versions, we should not define our own as that would simply be
67 * weird. Defaulting to Win2k/XP seems sane considering the origin of D3D9. The
68 * driver also defaults to being a generic D3D9 driver, which of course only
69 * matters if you're actually using the DDI. */
70 #define VERSION_HIGH VERSION_DWORD(0x0006, 0x000E) /* winxp, d3d9 */
71 #define VERSION_LOW VERSION_DWORD(0x0000, 0x0001) /* version, build */
72
73 struct d3dadapter9drm_context
74 {
75 struct d3dadapter9_context base;
76 struct pipe_loader_device *dev, *swdev;
77 };
78
79 static void
80 drm_destroy( struct d3dadapter9_context *ctx )
81 {
82 #if !GALLIUM_STATIC_TARGETS
83 struct d3dadapter9drm_context *drm = (struct d3dadapter9drm_context *)ctx;
84
85 /* pipe_loader_sw destroys the context */
86 if (drm->swdev)
87 pipe_loader_release(&drm->swdev, 1);
88 if (drm->dev)
89 pipe_loader_release(&drm->dev, 1);
90 #endif
91
92 FREE(ctx);
93 }
94
95 /* read a DWORD in the form 0xnnnnnnnn, which is how sysfs pci id stuff is
96 * formatted. */
97 static INLINE DWORD
98 read_file_dword( const char *name )
99 {
100 char buf[32];
101 int fd, r;
102
103 fd = open(name, O_RDONLY);
104 if (fd < 0) {
105 DBG("Unable to get PCI information from `%s'\n", name);
106 return 0;
107 }
108
109 r = read(fd, buf, 32);
110 close(fd);
111
112 return (r > 0) ? (DWORD)strtol(buf, NULL, 0) : 0;
113 }
114
115 /* sysfs doesn't expose the revision as its own file, so this function grabs a
116 * dword at an offset in the raw PCI header. The reason this isn't used for all
117 * data is that the kernel will make corrections but not expose them in the raw
118 * header bytes. */
119 static INLINE DWORD
120 read_config_dword( int fd,
121 unsigned offset )
122 {
123 DWORD r = 0;
124
125 if (lseek(fd, offset, SEEK_SET) != offset) { return 0; }
126 if (read(fd, &r, 4) != 4) { return 0; }
127
128 return r;
129 }
130
131 static INLINE void
132 get_bus_info( int fd,
133 DWORD *vendorid,
134 DWORD *deviceid,
135 DWORD *subsysid,
136 DWORD *revision )
137 {
138 drm_unique_t u;
139
140 u.unique_len = 0;
141 u.unique = NULL;
142
143 if (ioctl(fd, DRM_IOCTL_GET_UNIQUE, &u)) { return; }
144 u.unique = CALLOC(u.unique_len+1, 1);
145
146 if (ioctl(fd, DRM_IOCTL_GET_UNIQUE, &u)) { return; }
147 u.unique[u.unique_len] = '\0';
148
149 DBG("DRM Device BusID: %s\n", u.unique);
150 if (strncmp("pci:", u.unique, 4) == 0) {
151 char fname[512]; /* this ought to be enough */
152 int l = snprintf(fname, 512, "/sys/bus/pci/devices/%s/", u.unique+4);
153
154 /* VendorId */
155 snprintf(fname+l, 512-l, "vendor");
156 *vendorid = read_file_dword(fname);
157 /* DeviceId */
158 snprintf(fname+l, 512-l, "device");
159 *deviceid = read_file_dword(fname);
160 /* SubSysId */
161 snprintf(fname+l, 512-l, "subsystem_device");
162 *subsysid = (read_file_dword(fname) << 16) & 0xFFFF0000;
163 snprintf(fname+l, 512-l, "subsystem_vendor");
164 *subsysid |= read_file_dword(fname) & 0x0000FFFF;
165 /* Revision */
166 {
167 int cfgfd;
168
169 snprintf(fname+l, 512-l, "config");
170 cfgfd = open(fname, O_RDONLY);
171 if (cfgfd >= 0) {
172 *revision = read_config_dword(cfgfd, 0x8) & 0x000000FF;
173 close(cfgfd);
174 } else {
175 DBG("Unable to get raw PCI information from `%s'\n", fname);
176 }
177 }
178 DBG("PCI info: vendor=0x%04x, device=0x%04x, subsys=0x%08x, rev=%d\n",
179 *vendorid, *deviceid, *subsysid, *revision);
180 } else {
181 DBG("Unsupported BusID type.\n");
182 }
183
184 FREE(u.unique);
185 }
186
187 static INLINE void
188 read_descriptor( struct d3dadapter9_context *ctx,
189 int fd )
190 {
191 D3DADAPTER_IDENTIFIER9 *drvid = &ctx->identifier;
192
193 memset(drvid, 0, sizeof(*drvid));
194 get_bus_info(fd, &drvid->VendorId, &drvid->DeviceId,
195 &drvid->SubSysId, &drvid->Revision);
196
197 strncpy(drvid->Driver, "libd3dadapter9.so", sizeof(drvid->Driver));
198 strncpy(drvid->DeviceName, ctx->hal->get_name(ctx->hal), 32);
199 snprintf(drvid->Description, sizeof(drvid->Description),
200 "Gallium 0.4 with %s", ctx->hal->get_vendor(ctx->hal));
201
202 drvid->DriverVersionLowPart = VERSION_LOW;
203 drvid->DriverVersionHighPart = VERSION_HIGH;
204
205 /* To make a pseudo-real GUID we use the PCI bus data and some string */
206 drvid->DeviceIdentifier.Data1 = drvid->VendorId;
207 drvid->DeviceIdentifier.Data2 = drvid->DeviceId;
208 drvid->DeviceIdentifier.Data3 = drvid->SubSysId;
209 memcpy(drvid->DeviceIdentifier.Data4, "Gallium3D", 8);
210
211 drvid->WHQLLevel = 1; /* This fakes WHQL validaion */
212
213 /* XXX Fake NVIDIA binary driver on Windows.
214 *
215 * OS version: 4=95/98/NT4, 5=2000, 6=2000/XP, 7=Vista, 8=Win7
216 */
217 strncpy(drvid->Driver, "nvd3dum.dll", sizeof(drvid->Driver));
218 strncpy(drvid->Description, "NVIDIA GeForce GTX 680", sizeof(drvid->Description));
219 drvid->DriverVersionLowPart = VERSION_DWORD(12, 6658); /* minor, build */
220 drvid->DriverVersionHighPart = VERSION_DWORD(6, 15); /* OS, major */
221 drvid->SubSysId = 0;
222 drvid->Revision = 0;
223 drvid->DeviceIdentifier.Data1 = 0xaeb2cdd4;
224 drvid->DeviceIdentifier.Data2 = 0x6e41;
225 drvid->DeviceIdentifier.Data3 = 0x43ea;
226 drvid->DeviceIdentifier.Data4[0] = 0x94;
227 drvid->DeviceIdentifier.Data4[1] = 0x1c;
228 drvid->DeviceIdentifier.Data4[2] = 0x83;
229 drvid->DeviceIdentifier.Data4[3] = 0x61;
230 drvid->DeviceIdentifier.Data4[4] = 0xcc;
231 drvid->DeviceIdentifier.Data4[5] = 0x76;
232 drvid->DeviceIdentifier.Data4[6] = 0x07;
233 drvid->DeviceIdentifier.Data4[7] = 0x81;
234 drvid->WHQLLevel = 0;
235 }
236
237 static HRESULT WINAPI
238 drm_create_adapter( int fd,
239 ID3DAdapter9 **ppAdapter )
240 {
241 struct d3dadapter9drm_context *ctx = CALLOC_STRUCT(d3dadapter9drm_context);
242 HRESULT hr;
243 int i, different_device;
244 const struct drm_conf_ret *throttle_ret = NULL;
245 const struct drm_conf_ret *dmabuf_ret = NULL;
246 driOptionCache defaultInitOptions;
247 driOptionCache userInitOptions;
248 int throttling_value_user = -2;
249
250 #if !GALLIUM_STATIC_TARGETS
251 const char *paths[] = {
252 getenv("D3D9_DRIVERS_PATH"),
253 getenv("D3D9_DRIVERS_DIR"),
254 PIPE_SEARCH_DIR
255 };
256 #endif
257
258 if (!ctx) { return E_OUTOFMEMORY; }
259
260 ctx->base.destroy = drm_destroy;
261
262 fd = loader_get_user_preferred_fd(fd, &different_device);
263 ctx->base.linear_framebuffer = !!different_device;
264
265 #if GALLIUM_STATIC_TARGETS
266 ctx->base.hal = dd_create_screen(fd);
267 #else
268 /* use pipe-loader to dlopen appropriate drm driver */
269 if (!pipe_loader_drm_probe_fd(&ctx->dev, fd, FALSE)) {
270 ERR("Failed to probe drm fd %d.\n", fd);
271 FREE(ctx);
272 close(fd);
273 return D3DERR_DRIVERINTERNALERROR;
274 }
275
276 /* use pipe-loader to create a drm screen (hal) */
277 ctx->base.hal = NULL;
278 for (i = 0; !ctx->base.hal && i < Elements(paths); ++i) {
279 if (!paths[i]) { continue; }
280 ctx->base.hal = pipe_loader_create_screen(ctx->dev, paths[i]);
281 }
282 #endif
283 if (!ctx->base.hal) {
284 ERR("Unable to load requested driver.\n");
285 drm_destroy(&ctx->base);
286 return D3DERR_DRIVERINTERNALERROR;
287 }
288
289 #if GALLIUM_STATIC_TARGETS
290 dmabuf_ret = dd_configuration(DRM_CONF_SHARE_FD);
291 throttle_ret = dd_configuration(DRM_CONF_THROTTLE);
292 #else
293 dmabuf_ret = pipe_loader_configuration(ctx->dev, DRM_CONF_SHARE_FD);
294 throttle_ret = pipe_loader_configuration(ctx->dev, DRM_CONF_THROTTLE);
295 #endif // GALLIUM_STATIC_TARGETS
296 if (!dmabuf_ret || !dmabuf_ret->val.val_bool) {
297 ERR("The driver is not capable of dma-buf sharing."
298 "Abandon to load nine state tracker\n");
299 drm_destroy(&ctx->base);
300 return D3DERR_DRIVERINTERNALERROR;
301 }
302
303 if (throttle_ret && throttle_ret->val.val_int != -1) {
304 ctx->base.throttling = TRUE;
305 ctx->base.throttling_value = throttle_ret->val.val_int;
306 } else
307 ctx->base.throttling = FALSE;
308
309 driParseOptionInfo(&defaultInitOptions, __driConfigOptionsNine);
310 driParseConfigFiles(&userInitOptions, &defaultInitOptions, 0, "nine");
311 if (driCheckOption(&userInitOptions, "throttle_value", DRI_INT)) {
312 throttling_value_user = driQueryOptioni(&userInitOptions, "throttle_value");
313 if (throttling_value_user == -1)
314 ctx->base.throttling = FALSE;
315 else if (throttling_value_user >= 0) {
316 ctx->base.throttling = TRUE;
317 ctx->base.throttling_value = throttling_value_user;
318 }
319 }
320
321 if (driCheckOption(&userInitOptions, "vblank_mode", DRI_ENUM))
322 ctx->base.vblank_mode = driQueryOptioni(&userInitOptions, "vblank_mode");
323 else
324 ctx->base.vblank_mode = 1;
325
326 if (driCheckOption(&userInitOptions, "thread_submit", DRI_BOOL)) {
327 ctx->base.thread_submit = driQueryOptionb(&userInitOptions, "thread_submit");
328 if (ctx->base.thread_submit && (throttling_value_user == -2 || throttling_value_user == 0)) {
329 ctx->base.throttling_value = 0;
330 } else if (ctx->base.thread_submit) {
331 DBG("You have set a non standard throttling value in combination with thread_submit."
332 "We advise to use a throttling value of -2/0");
333 }
334 if (ctx->base.thread_submit && !different_device)
335 DBG("You have set thread_submit but do not use a different device than the server."
336 "You should not expect any benefit.");
337 }
338
339 driDestroyOptionCache(&userInitOptions);
340 driDestroyOptionInfo(&defaultInitOptions);
341
342 #if GALLIUM_STATIC_TARGETS
343 ctx->base.ref = ninesw_create_screen(ctx->base.hal);
344 #else
345 /* wrap it to create a software screen that can share resources */
346 if (pipe_loader_sw_probe_wrapped(&ctx->swdev, ctx->base.hal)) {
347 ctx->base.ref = NULL;
348 for (i = 0; !ctx->base.ref && i < Elements(paths); ++i) {
349 if (!paths[i]) { continue; }
350 ctx->base.ref = pipe_loader_create_screen(ctx->swdev, paths[i]);
351 }
352 }
353 #endif
354 if (!ctx->base.ref) {
355 ERR("Couldn't wrap drm screen to swrast screen. Software devices "
356 "will be unavailable.\n");
357 }
358
359 /* read out PCI info */
360 read_descriptor(&ctx->base, fd);
361
362 /* create and return new ID3DAdapter9 */
363 hr = NineAdapter9_new(&ctx->base, (struct NineAdapter9 **)ppAdapter);
364 if (FAILED(hr)) {
365 drm_destroy(&ctx->base);
366 return hr;
367 }
368
369 return D3D_OK;
370 }
371
372 const struct D3DAdapter9DRM drm9_desc = {
373 .major_version = D3DADAPTER9DRM_MAJOR,
374 .minor_version = D3DADAPTER9DRM_MINOR,
375 .create_adapter = drm_create_adapter
376 };