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