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 "frontend/drm_driver.h"
36
37 #include "d3dadapter/d3dadapter9.h"
38 #include "d3dadapter/drm.h"
39
40 #include "util/xmlconfig.h"
41 #include "util/driconf.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 /* On non-x86 archs, Box86 has issues with thread_submit. */
51 #if DETECT_ARCH_X86 || DETECT_ARCH_X86_64
52 #define DEFAULT_THREADSUBMIT true
53 #else
54 #define DEFAULT_THREADSUBMIT false
55 #endif
56
57 const driOptionDescription __driConfigOptionsNine[] = {
58 DRI_CONF_SECTION_PERFORMANCE
59 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_1)
60 DRI_CONF_SECTION_END
61 DRI_CONF_SECTION_NINE
62 DRI_CONF_NINE_OVERRIDEVENDOR(-1)
63 DRI_CONF_NINE_THROTTLE(-2)
64 DRI_CONF_NINE_THREADSUBMIT(DEFAULT_THREADSUBMIT)
65 DRI_CONF_NINE_ALLOWDISCARDDELAYEDRELEASE(true)
66 DRI_CONF_NINE_TEARFREEDISCARD(true)
67 DRI_CONF_NINE_CSMT(-1)
68 DRI_CONF_NINE_DYNAMICTEXTUREWORKAROUND(true)
69 DRI_CONF_NINE_SHADERINLINECONSTANTS(false)
70 DRI_CONF_NINE_SHMEM_LIMIT()
71 DRI_CONF_NINE_FORCESWRENDERINGONCPU(false)
72 DRI_CONF_NINE_FORCEFEATURESEMULATION(false)
73 DRI_CONF_SECTION_END
74 DRI_CONF_SECTION_DEBUG
75 DRI_CONF_OVERRIDE_VRAM_SIZE()
76 DRI_CONF_SECTION_END
77 };
78
79 struct fallback_card_config {
80 const char *name;
81 unsigned vendor_id;
82 unsigned device_id;
83 } fallback_cards[] = {
84 {"NV124", 0x10de, 0x13C2}, /* NVIDIA GeForce GTX 970 */
85 {"HAWAII", 0x1002, 0x67b1}, /* AMD Radeon R9 290 */
86 {"Haswell Mobile", 0x8086, 0x13C2}, /* Intel Haswell Mobile */
87 {"SVGA3D", 0x15ad, 0x0405}, /* VMware SVGA 3D */
88 };
89
90 /* prototypes */
91 void
92 d3d_match_vendor_id( D3DADAPTER_IDENTIFIER9* drvid,
93 unsigned fallback_ven,
94 unsigned fallback_dev,
95 const char* fallback_name );
96
97 void d3d_fill_driver_version(D3DADAPTER_IDENTIFIER9* drvid);
98
99 void d3d_fill_cardname(D3DADAPTER_IDENTIFIER9* drvid);
100
101 struct d3dadapter9drm_context
102 {
103 struct d3dadapter9_context base;
104 struct pipe_loader_device *dev, *swdev;
105 int fd;
106 };
107
108 static void
drm_destroy(struct d3dadapter9_context * ctx)109 drm_destroy( struct d3dadapter9_context *ctx )
110 {
111 struct d3dadapter9drm_context *drm = (struct d3dadapter9drm_context *)ctx;
112
113 if (ctx->ref && ctx->hal != ctx->ref)
114 ctx->ref->destroy(ctx->ref);
115 /* because ref is a wrapper around hal, freeing ref frees hal too. */
116 else if (ctx->hal)
117 ctx->hal->destroy(ctx->hal);
118
119 if (drm->swdev && drm->swdev != drm->dev)
120 pipe_loader_release(&drm->swdev, 1);
121 if (drm->dev)
122 pipe_loader_release(&drm->dev, 1);
123
124 close(drm->fd);
125 FREE(ctx);
126 }
127
128 static inline void
get_bus_info(int fd,DWORD * vendorid,DWORD * deviceid,DWORD * subsysid,DWORD * revision)129 get_bus_info( int fd,
130 DWORD *vendorid,
131 DWORD *deviceid,
132 DWORD *subsysid,
133 DWORD *revision )
134 {
135 int vid, did;
136
137 if (loader_get_pci_id_for_fd(fd, &vid, &did)) {
138 DBG("PCI info: vendor=0x%04x, device=0x%04x\n",
139 vid, did);
140 *vendorid = vid;
141 *deviceid = did;
142 *subsysid = 0;
143 *revision = 0;
144 } else {
145 DBG("Unable to detect card. Faking %s\n", fallback_cards[0].name);
146 *vendorid = fallback_cards[0].vendor_id;
147 *deviceid = fallback_cards[0].device_id;
148 *subsysid = 0;
149 *revision = 0;
150 }
151 }
152
153 static inline void
read_descriptor(struct d3dadapter9_context * ctx,int fd,int override_vendorid)154 read_descriptor( struct d3dadapter9_context *ctx,
155 int fd, int override_vendorid )
156 {
157 unsigned i;
158 BOOL found;
159 D3DADAPTER_IDENTIFIER9 *drvid = &ctx->identifier;
160
161 memset(drvid, 0, sizeof(*drvid));
162 get_bus_info(fd, &drvid->VendorId, &drvid->DeviceId,
163 &drvid->SubSysId, &drvid->Revision);
164 snprintf(drvid->DeviceName, sizeof(drvid->DeviceName),
165 "Gallium 0.4 with %s", ctx->hal->get_vendor(ctx->hal));
166 snprintf(drvid->Description, sizeof(drvid->Description),
167 "%s", ctx->hal->get_name(ctx->hal));
168
169 if (override_vendorid > 0) {
170 found = false;
171 /* fill in device_id and card name for fake vendor */
172 for (i = 0; i < sizeof(fallback_cards)/sizeof(fallback_cards[0]); i++) {
173 if (fallback_cards[i].vendor_id == override_vendorid) {
174 DBG("Faking card '%s' vendor 0x%04x, device 0x%04x\n",
175 fallback_cards[i].name,
176 fallback_cards[i].vendor_id,
177 fallback_cards[i].device_id);
178 drvid->VendorId = fallback_cards[i].vendor_id;
179 drvid->DeviceId = fallback_cards[i].device_id;
180 snprintf(drvid->Description, sizeof(drvid->Description),
181 "%s", fallback_cards[i].name);
182 found = true;
183 break;
184 }
185 }
186 if (!found) {
187 DBG("Unknown fake vendor 0x%04x! Using detected vendor !\n", override_vendorid);
188 }
189 }
190 /* choose fall-back vendor if necessary to allow
191 * the following functions to return sane results */
192 d3d_match_vendor_id(drvid, fallback_cards[0].vendor_id, fallback_cards[0].device_id, fallback_cards[0].name);
193 /* fill in driver name and version info */
194 d3d_fill_driver_version(drvid);
195 /* override Description field with Windows like names */
196 d3d_fill_cardname(drvid);
197
198 /* this driver isn't WHQL certified */
199 drvid->WHQLLevel = 0;
200
201 /* this value is fixed */
202 drvid->DeviceIdentifier.Data1 = 0xaeb2cdd4;
203 drvid->DeviceIdentifier.Data2 = 0x6e41;
204 drvid->DeviceIdentifier.Data3 = 0x43ea;
205 drvid->DeviceIdentifier.Data4[0] = 0x94;
206 drvid->DeviceIdentifier.Data4[1] = 0x1c;
207 drvid->DeviceIdentifier.Data4[2] = 0x83;
208 drvid->DeviceIdentifier.Data4[3] = 0x61;
209 drvid->DeviceIdentifier.Data4[4] = 0xcc;
210 drvid->DeviceIdentifier.Data4[5] = 0x76;
211 drvid->DeviceIdentifier.Data4[6] = 0x07;
212 drvid->DeviceIdentifier.Data4[7] = 0x81;
213 }
214
215 static HRESULT WINAPI
drm_create_adapter(int fd,ID3DAdapter9 ** ppAdapter)216 drm_create_adapter( int fd,
217 ID3DAdapter9 **ppAdapter )
218 {
219 struct d3dadapter9drm_context *ctx = CALLOC_STRUCT(d3dadapter9drm_context);
220 HRESULT hr;
221 bool different_device;
222 driOptionCache defaultInitOptions;
223 driOptionCache userInitOptions;
224 int throttling_value_user = -2;
225 int override_vendorid = -1;
226 bool sw_rendering;
227
228 if (!ctx) { return E_OUTOFMEMORY; }
229
230 ctx->base.destroy = drm_destroy;
231
232 /* Although the fd is provided from external source, mesa/nine
233 * takes ownership of it. */
234 different_device = loader_get_user_preferred_fd(&fd, NULL);
235 ctx->fd = fd;
236 ctx->base.linear_framebuffer = different_device;
237
238 if (!pipe_loader_drm_probe_fd(&ctx->dev, fd, false)) {
239 ERR("Failed to probe drm fd %d.\n", fd);
240 FREE(ctx);
241 close(fd);
242 return D3DERR_DRIVERINTERNALERROR;
243 }
244
245 ctx->base.hal = pipe_loader_create_screen(ctx->dev);
246 if (!ctx->base.hal) {
247 ERR("Unable to load requested driver.\n");
248 drm_destroy(&ctx->base);
249 return D3DERR_DRIVERINTERNALERROR;
250 }
251
252 if (!ctx->base.hal->get_param(ctx->base.hal, PIPE_CAP_DMABUF)) {
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 /* Previously was set to PIPE_CAP_MAX_FRAMES_IN_FLIGHT,
260 * but the change of value of this cap to 1 seems to cause
261 * regressions. */
262 ctx->base.throttling_value = 2;
263 ctx->base.throttling = ctx->base.throttling_value > 0;
264
265 driParseOptionInfo(&defaultInitOptions, __driConfigOptionsNine,
266 ARRAY_SIZE(__driConfigOptionsNine));
267 driParseConfigFiles(&userInitOptions, &defaultInitOptions, 0,
268 "nine", NULL, NULL, NULL, 0, NULL, 0);
269 if (driCheckOption(&userInitOptions, "throttle_value", DRI_INT)) {
270 throttling_value_user = driQueryOptioni(&userInitOptions, "throttle_value");
271 if (throttling_value_user == -1)
272 ctx->base.throttling = false;
273 else if (throttling_value_user >= 0) {
274 ctx->base.throttling = true;
275 ctx->base.throttling_value = throttling_value_user;
276 }
277 }
278
279 ctx->base.vblank_mode = driQueryOptioni(&userInitOptions, "vblank_mode");
280 ctx->base.thread_submit = driQueryOptionb(&userInitOptions, "thread_submit"); /* TODO: default to TRUE if different_device */
281 override_vendorid = driQueryOptioni(&userInitOptions, "override_vendorid");
282
283 ctx->base.discard_delayed_release = driQueryOptionb(&userInitOptions, "discard_delayed_release");
284 ctx->base.tearfree_discard = driQueryOptionb(&userInitOptions, "tearfree_discard");
285
286 if (ctx->base.tearfree_discard && !ctx->base.discard_delayed_release) {
287 ERR("tearfree_discard requires discard_delayed_release\n");
288 ctx->base.tearfree_discard = false;
289 }
290
291 ctx->base.csmt_force = driQueryOptioni(&userInitOptions, "csmt_force");
292 ctx->base.dynamic_texture_workaround = driQueryOptionb(&userInitOptions, "dynamic_texture_workaround");
293 ctx->base.shader_inline_constants = driQueryOptionb(&userInitOptions, "shader_inline_constants");
294 ctx->base.memfd_virtualsizelimit = driQueryOptioni(&userInitOptions, "texture_memory_limit");
295 ctx->base.override_vram_size = driQueryOptioni(&userInitOptions, "override_vram_size");
296 ctx->base.force_emulation = driQueryOptionb(&userInitOptions, "force_features_emulation");
297 sw_rendering = driQueryOptionb(&userInitOptions, "force_sw_rendering_on_cpu");
298
299 driDestroyOptionCache(&userInitOptions);
300 driDestroyOptionInfo(&defaultInitOptions);
301
302 sw_rendering |= debug_get_bool_option("D3D_ALWAYS_SOFTWARE", false);
303 /* wrap it to create a software screen that can share resources */
304 if (sw_rendering && pipe_loader_sw_probe_wrapped(&ctx->swdev, ctx->base.hal))
305 ctx->base.ref = pipe_loader_create_screen(ctx->swdev);
306 else {
307 /* Use the hardware for sw rendering */
308 ctx->swdev = ctx->dev;
309 ctx->base.ref = ctx->base.hal;
310 }
311
312 /* read out PCI info */
313 read_descriptor(&ctx->base, fd, override_vendorid);
314
315 /* create and return new ID3DAdapter9 */
316 hr = NineAdapter9_new(&ctx->base, (struct NineAdapter9 **)ppAdapter);
317 if (FAILED(hr)) {
318 drm_destroy(&ctx->base);
319 return hr;
320 }
321
322 return D3D_OK;
323 }
324
325 const struct D3DAdapter9DRM drm9_desc = {
326 .major_version = D3DADAPTER9DRM_MAJOR,
327 .minor_version = D3DADAPTER9DRM_MINOR,
328 .create_adapter = drm_create_adapter
329 };
330