28dbd2ef9db2636cd5d727c426ce71c1873cbc85
[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 /* XXX: header order is slightly screwy here */
24 #include "loader.h"
25
26 #include "adapter9.h"
27
28 #include "pipe-loader/pipe_loader.h"
29
30 #include "pipe/p_screen.h"
31 #include "pipe/p_state.h"
32
33 #include "target-helpers/drm_helper.h"
34 #include "target-helpers/sw_helper.h"
35 #include "state_tracker/drm_driver.h"
36
37 #include "d3dadapter/d3dadapter9.h"
38 #include "d3dadapter/drm.h"
39
40 #include "util/xmlconfig.h"
41 #include "util/xmlpool.h"
42
43 #include "drm-uapi/drm.h"
44 #include <sys/ioctl.h>
45 #include <fcntl.h>
46 #include <stdio.h>
47
48 #define DBG_CHANNEL DBG_ADAPTER
49
50 const char __driConfigOptionsNine[] =
51 DRI_CONF_BEGIN
52 DRI_CONF_SECTION_PERFORMANCE
53 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_1)
54 DRI_CONF_SECTION_END
55 DRI_CONF_SECTION_NINE
56 DRI_CONF_NINE_OVERRIDEVENDOR(-1)
57 DRI_CONF_NINE_THROTTLE(-2)
58 DRI_CONF_NINE_THREADSUBMIT("false")
59 DRI_CONF_NINE_ALLOWDISCARDDELAYEDRELEASE("true")
60 DRI_CONF_NINE_TEARFREEDISCARD("false")
61 DRI_CONF_NINE_CSMT(-1)
62 DRI_CONF_SECTION_END
63 DRI_CONF_END;
64
65 struct fallback_card_config {
66 const char *name;
67 unsigned vendor_id;
68 unsigned device_id;
69 } fallback_cards[] = {
70 {"NV124", 0x10de, 0x13C2}, /* NVIDIA GeForce GTX 970 */
71 {"HAWAII", 0x1002, 0x67b1}, /* AMD Radeon R9 290 */
72 {"Haswell Mobile", 0x8086, 0x13C2}, /* Intel Haswell Mobile */
73 {"SVGA3D", 0x15ad, 0x0405}, /* VMware SVGA 3D */
74 };
75
76 /* prototypes */
77 void
78 d3d_match_vendor_id( D3DADAPTER_IDENTIFIER9* drvid,
79 unsigned fallback_ven,
80 unsigned fallback_dev,
81 const char* fallback_name );
82
83 void d3d_fill_driver_version(D3DADAPTER_IDENTIFIER9* drvid);
84
85 void d3d_fill_cardname(D3DADAPTER_IDENTIFIER9* drvid);
86
87 struct d3dadapter9drm_context
88 {
89 struct d3dadapter9_context base;
90 struct pipe_loader_device *dev, *swdev;
91 int fd;
92 };
93
94 static void
95 drm_destroy( struct d3dadapter9_context *ctx )
96 {
97 struct d3dadapter9drm_context *drm = (struct d3dadapter9drm_context *)ctx;
98
99 if (ctx->ref)
100 ctx->ref->destroy(ctx->ref);
101 /* because ref is a wrapper around hal, freeing ref frees hal too. */
102 else if (ctx->hal)
103 ctx->hal->destroy(ctx->hal);
104
105 if (drm->swdev)
106 pipe_loader_release(&drm->swdev, 1);
107 if (drm->dev)
108 pipe_loader_release(&drm->dev, 1);
109
110 close(drm->fd);
111 FREE(ctx);
112 }
113
114 static inline void
115 get_bus_info( int fd,
116 DWORD *vendorid,
117 DWORD *deviceid,
118 DWORD *subsysid,
119 DWORD *revision )
120 {
121 int vid, did;
122
123 if (loader_get_pci_id_for_fd(fd, &vid, &did)) {
124 DBG("PCI info: vendor=0x%04x, device=0x%04x\n",
125 vid, did);
126 *vendorid = vid;
127 *deviceid = did;
128 *subsysid = 0;
129 *revision = 0;
130 } else {
131 DBG("Unable to detect card. Faking %s\n", fallback_cards[0].name);
132 *vendorid = fallback_cards[0].vendor_id;
133 *deviceid = fallback_cards[0].device_id;
134 *subsysid = 0;
135 *revision = 0;
136 }
137 }
138
139 static inline void
140 read_descriptor( struct d3dadapter9_context *ctx,
141 int fd, int override_vendorid )
142 {
143 unsigned i;
144 BOOL found;
145 D3DADAPTER_IDENTIFIER9 *drvid = &ctx->identifier;
146
147 memset(drvid, 0, sizeof(*drvid));
148 get_bus_info(fd, &drvid->VendorId, &drvid->DeviceId,
149 &drvid->SubSysId, &drvid->Revision);
150 snprintf(drvid->DeviceName, sizeof(drvid->DeviceName),
151 "Gallium 0.4 with %s", ctx->hal->get_vendor(ctx->hal));
152 snprintf(drvid->Description, sizeof(drvid->Description),
153 "%s", ctx->hal->get_name(ctx->hal));
154
155 if (override_vendorid > 0) {
156 found = FALSE;
157 /* fill in device_id and card name for fake vendor */
158 for (i = 0; i < sizeof(fallback_cards)/sizeof(fallback_cards[0]); i++) {
159 if (fallback_cards[i].vendor_id == override_vendorid) {
160 DBG("Faking card '%s' vendor 0x%04x, device 0x%04x\n",
161 fallback_cards[i].name,
162 fallback_cards[i].vendor_id,
163 fallback_cards[i].device_id);
164 drvid->VendorId = fallback_cards[i].vendor_id;
165 drvid->DeviceId = fallback_cards[i].device_id;
166 snprintf(drvid->Description, sizeof(drvid->Description),
167 "%s", fallback_cards[i].name);
168 found = TRUE;
169 break;
170 }
171 }
172 if (!found) {
173 DBG("Unknown fake vendor 0x%04x! Using detected vendor !\n", override_vendorid);
174 }
175 }
176 /* choose fall-back vendor if necessary to allow
177 * the following functions to return sane results */
178 d3d_match_vendor_id(drvid, fallback_cards[0].vendor_id, fallback_cards[0].device_id, fallback_cards[0].name);
179 /* fill in driver name and version info */
180 d3d_fill_driver_version(drvid);
181 /* override Description field with Windows like names */
182 d3d_fill_cardname(drvid);
183
184 /* this driver isn't WHQL certified */
185 drvid->WHQLLevel = 0;
186
187 /* this value is fixed */
188 drvid->DeviceIdentifier.Data1 = 0xaeb2cdd4;
189 drvid->DeviceIdentifier.Data2 = 0x6e41;
190 drvid->DeviceIdentifier.Data3 = 0x43ea;
191 drvid->DeviceIdentifier.Data4[0] = 0x94;
192 drvid->DeviceIdentifier.Data4[1] = 0x1c;
193 drvid->DeviceIdentifier.Data4[2] = 0x83;
194 drvid->DeviceIdentifier.Data4[3] = 0x61;
195 drvid->DeviceIdentifier.Data4[4] = 0xcc;
196 drvid->DeviceIdentifier.Data4[5] = 0x76;
197 drvid->DeviceIdentifier.Data4[6] = 0x07;
198 drvid->DeviceIdentifier.Data4[7] = 0x81;
199 }
200
201 static HRESULT WINAPI
202 drm_create_adapter( int fd,
203 ID3DAdapter9 **ppAdapter )
204 {
205 struct d3dadapter9drm_context *ctx = CALLOC_STRUCT(d3dadapter9drm_context);
206 HRESULT hr;
207 bool different_device;
208 bool software_device;
209 const struct drm_conf_ret *throttle_ret = NULL;
210 const struct drm_conf_ret *dmabuf_ret = NULL;
211 driOptionCache defaultInitOptions;
212 driOptionCache userInitOptions;
213 int throttling_value_user = -2;
214 int override_vendorid = -1;
215
216 if (!ctx) { return E_OUTOFMEMORY; }
217
218 ctx->base.destroy = drm_destroy;
219
220 /* Although the fd is provided from external source, mesa/nine
221 * takes ownership of it. */
222 fd = loader_get_user_preferred_fd(fd, &different_device);
223 ctx->fd = fd;
224 ctx->base.linear_framebuffer = different_device;
225
226 const char *force_sw = getenv("D3D_ALWAYS_SOFTWARE");
227 software_device = force_sw && !strcmp(force_sw, "1");
228
229 if ((software_device && !pipe_loader_sw_probe_kms(&ctx->dev, fd)) ||
230 (!software_device && !pipe_loader_drm_probe_fd(&ctx->dev, fd))) {
231 ERR("Failed to probe drm fd %d.\n", fd);
232 FREE(ctx);
233 close(fd);
234 return D3DERR_DRIVERINTERNALERROR;
235 }
236
237 ctx->base.hal = pipe_loader_create_screen(ctx->dev);
238 if (!ctx->base.hal) {
239 ERR("Unable to load requested driver.\n");
240 drm_destroy(&ctx->base);
241 return D3DERR_DRIVERINTERNALERROR;
242 }
243
244 if (!software_device) {
245 /*
246 * The software renderer isn't a DRM device and doesn't support
247 * pipe_loader_configuration.
248 * The KMS winsys supports SHARE_FD, so skip this check.
249 */
250 dmabuf_ret = pipe_loader_configuration(ctx->dev, DRM_CONF_SHARE_FD);
251 throttle_ret = pipe_loader_configuration(ctx->dev, DRM_CONF_THROTTLE);
252 if (!dmabuf_ret || !dmabuf_ret->val.val_bool) {
253 ERR("The driver is not capable of dma-buf sharing."
254 "Abandon to load nine state tracker\n");
255 drm_destroy(&ctx->base);
256 return D3DERR_DRIVERINTERNALERROR;
257 }
258 }
259
260 if (throttle_ret && throttle_ret->val.val_int != -1) {
261 ctx->base.throttling = TRUE;
262 ctx->base.throttling_value = throttle_ret->val.val_int;
263 } else
264 ctx->base.throttling = FALSE;
265
266 driParseOptionInfo(&defaultInitOptions, __driConfigOptionsNine);
267 driParseConfigFiles(&userInitOptions, &defaultInitOptions, 0, "nine", NULL);
268 if (driCheckOption(&userInitOptions, "throttle_value", DRI_INT)) {
269 throttling_value_user = driQueryOptioni(&userInitOptions, "throttle_value");
270 if (throttling_value_user == -1)
271 ctx->base.throttling = FALSE;
272 else if (throttling_value_user >= 0) {
273 ctx->base.throttling = TRUE;
274 ctx->base.throttling_value = throttling_value_user;
275 }
276 }
277
278 if (driCheckOption(&userInitOptions, "vblank_mode", DRI_ENUM))
279 ctx->base.vblank_mode = driQueryOptioni(&userInitOptions, "vblank_mode");
280 else
281 ctx->base.vblank_mode = 1;
282
283 if (driCheckOption(&userInitOptions, "thread_submit", DRI_BOOL))
284 ctx->base.thread_submit = driQueryOptionb(&userInitOptions, "thread_submit");
285 else
286 ctx->base.thread_submit = different_device;
287
288 if (ctx->base.thread_submit && (throttling_value_user == -2 || throttling_value_user == 0)) {
289 ctx->base.throttling_value = 0;
290 } else if (ctx->base.thread_submit) {
291 DBG("You have set a non standard throttling value in combination with thread_submit."
292 "We advise to use a throttling value of -2/0");
293 }
294
295 if (driCheckOption(&userInitOptions, "override_vendorid", DRI_INT)) {
296 override_vendorid = driQueryOptioni(&userInitOptions, "override_vendorid");
297 }
298
299 if (driCheckOption(&userInitOptions, "discard_delayed_release", DRI_BOOL))
300 ctx->base.discard_delayed_release = driQueryOptionb(&userInitOptions, "discard_delayed_release");
301 else
302 ctx->base.discard_delayed_release = TRUE;
303
304 if (driCheckOption(&userInitOptions, "tearfree_discard", DRI_BOOL))
305 ctx->base.tearfree_discard = driQueryOptionb(&userInitOptions, "tearfree_discard");
306 else
307 ctx->base.tearfree_discard = FALSE;
308
309 if (ctx->base.tearfree_discard && !ctx->base.discard_delayed_release) {
310 ERR("tearfree_discard requires discard_delayed_release\n");
311 ctx->base.tearfree_discard = FALSE;
312 }
313
314 if (driCheckOption(&userInitOptions, "csmt_force", DRI_INT))
315 ctx->base.csmt_force = driQueryOptioni(&userInitOptions, "csmt_force");
316 else
317 ctx->base.csmt_force = -1;
318
319 driDestroyOptionCache(&userInitOptions);
320 driDestroyOptionInfo(&defaultInitOptions);
321
322 /* wrap it to create a software screen that can share resources */
323 if (pipe_loader_sw_probe_wrapped(&ctx->swdev, ctx->base.hal))
324 ctx->base.ref = pipe_loader_create_screen(ctx->swdev);
325
326 if (!ctx->base.ref) {
327 ERR("Couldn't wrap drm screen to swrast screen. Software devices "
328 "will be unavailable.\n");
329 }
330
331 /* read out PCI info */
332 read_descriptor(&ctx->base, fd, override_vendorid);
333
334 /* create and return new ID3DAdapter9 */
335 hr = NineAdapter9_new(&ctx->base, (struct NineAdapter9 **)ppAdapter);
336 if (FAILED(hr)) {
337 drm_destroy(&ctx->base);
338 return hr;
339 }
340
341 return D3D_OK;
342 }
343
344 const struct D3DAdapter9DRM drm9_desc = {
345 .major_version = D3DADAPTER9DRM_MAJOR,
346 .minor_version = D3DADAPTER9DRM_MINOR,
347 .create_adapter = drm_create_adapter
348 };