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