st/nine: Add new driconf options to control DISCARD behaviour
[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 "xmlconfig.h"
41 #include "xmlpool.h"
42
43 #include <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_SECTION_END
62 DRI_CONF_END;
63
64 struct fallback_card_config {
65 const char *name;
66 unsigned vendor_id;
67 unsigned device_id;
68 } fallback_cards[] = {
69 {"NV124", 0x10de, 0x13C2}, /* NVIDIA GeForce GTX 970 */
70 {"HAWAII", 0x1002, 0x67b1}, /* AMD Radeon R9 290 */
71 {"Haswell Mobile", 0x8086, 0x13C2}, /* Intel Haswell Mobile */
72 {"SVGA3D", 0x15ad, 0x0405}, /* VMware SVGA 3D */
73 };
74
75 /* prototypes */
76 void
77 d3d_match_vendor_id( D3DADAPTER_IDENTIFIER9* drvid,
78 unsigned fallback_ven,
79 unsigned fallback_dev,
80 const char* fallback_name );
81
82 void d3d_fill_driver_version(D3DADAPTER_IDENTIFIER9* drvid);
83
84 void d3d_fill_cardname(D3DADAPTER_IDENTIFIER9* drvid);
85
86 struct d3dadapter9drm_context
87 {
88 struct d3dadapter9_context base;
89 struct pipe_loader_device *dev, *swdev;
90 int fd;
91 };
92
93 static void
94 drm_destroy( struct d3dadapter9_context *ctx )
95 {
96 struct d3dadapter9drm_context *drm = (struct d3dadapter9drm_context *)ctx;
97
98 if (ctx->ref)
99 ctx->ref->destroy(ctx->ref);
100 /* because ref is a wrapper around hal, freeing ref frees hal too. */
101 else if (ctx->hal)
102 ctx->hal->destroy(ctx->hal);
103
104 if (drm->swdev)
105 pipe_loader_release(&drm->swdev, 1);
106 if (drm->dev)
107 pipe_loader_release(&drm->dev, 1);
108
109 close(drm->fd);
110 FREE(ctx);
111 }
112
113 static inline void
114 get_bus_info( int fd,
115 DWORD *vendorid,
116 DWORD *deviceid,
117 DWORD *subsysid,
118 DWORD *revision )
119 {
120 int vid, did;
121
122 if (loader_get_pci_id_for_fd(fd, &vid, &did)) {
123 DBG("PCI info: vendor=0x%04x, device=0x%04x\n",
124 vid, did);
125 *vendorid = vid;
126 *deviceid = did;
127 *subsysid = 0;
128 *revision = 0;
129 } else {
130 DBG("Unable to detect card. Faking %s\n", fallback_cards[0].name);
131 *vendorid = fallback_cards[0].vendor_id;
132 *deviceid = fallback_cards[0].device_id;
133 *subsysid = 0;
134 *revision = 0;
135 }
136 }
137
138 static inline void
139 read_descriptor( struct d3dadapter9_context *ctx,
140 int fd, int override_vendorid )
141 {
142 unsigned i;
143 BOOL found;
144 D3DADAPTER_IDENTIFIER9 *drvid = &ctx->identifier;
145
146 memset(drvid, 0, sizeof(*drvid));
147 get_bus_info(fd, &drvid->VendorId, &drvid->DeviceId,
148 &drvid->SubSysId, &drvid->Revision);
149 snprintf(drvid->DeviceName, sizeof(drvid->DeviceName),
150 "Gallium 0.4 with %s", ctx->hal->get_vendor(ctx->hal));
151 strncpy(drvid->Description, ctx->hal->get_name(ctx->hal),
152 sizeof(drvid->Description));
153
154 if (override_vendorid > 0) {
155 found = FALSE;
156 /* fill in device_id and card name for fake vendor */
157 for (i = 0; i < sizeof(fallback_cards)/sizeof(fallback_cards[0]); i++) {
158 if (fallback_cards[i].vendor_id == override_vendorid) {
159 DBG("Faking card '%s' vendor 0x%04x, device 0x%04x\n",
160 fallback_cards[i].name,
161 fallback_cards[i].vendor_id,
162 fallback_cards[i].device_id);
163 drvid->VendorId = fallback_cards[i].vendor_id;
164 drvid->DeviceId = fallback_cards[i].device_id;
165 strncpy(drvid->Description, fallback_cards[i].name,
166 sizeof(drvid->Description));
167 found = TRUE;
168 break;
169 }
170 }
171 if (!found) {
172 DBG("Unknown fake vendor 0x%04x! Using detected vendor !\n", override_vendorid);
173 }
174 }
175 /* choose fall-back vendor if necessary to allow
176 * the following functions to return sane results */
177 d3d_match_vendor_id(drvid, fallback_cards[0].vendor_id, fallback_cards[0].device_id, fallback_cards[0].name);
178 /* fill in driver name and version info */
179 d3d_fill_driver_version(drvid);
180 /* override Description field with Windows like names */
181 d3d_fill_cardname(drvid);
182
183 /* this driver isn't WHQL certified */
184 drvid->WHQLLevel = 0;
185
186 /* this value is fixed */
187 drvid->DeviceIdentifier.Data1 = 0xaeb2cdd4;
188 drvid->DeviceIdentifier.Data2 = 0x6e41;
189 drvid->DeviceIdentifier.Data3 = 0x43ea;
190 drvid->DeviceIdentifier.Data4[0] = 0x94;
191 drvid->DeviceIdentifier.Data4[1] = 0x1c;
192 drvid->DeviceIdentifier.Data4[2] = 0x83;
193 drvid->DeviceIdentifier.Data4[3] = 0x61;
194 drvid->DeviceIdentifier.Data4[4] = 0xcc;
195 drvid->DeviceIdentifier.Data4[5] = 0x76;
196 drvid->DeviceIdentifier.Data4[6] = 0x07;
197 drvid->DeviceIdentifier.Data4[7] = 0x81;
198 }
199
200 static HRESULT WINAPI
201 drm_create_adapter( int fd,
202 ID3DAdapter9 **ppAdapter )
203 {
204 struct d3dadapter9drm_context *ctx = CALLOC_STRUCT(d3dadapter9drm_context);
205 HRESULT hr;
206 int different_device;
207 const struct drm_conf_ret *throttle_ret = NULL;
208 const struct drm_conf_ret *dmabuf_ret = NULL;
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 dmabuf_ret = pipe_loader_configuration(ctx->dev, DRM_CONF_SHARE_FD);
239 throttle_ret = pipe_loader_configuration(ctx->dev, DRM_CONF_THROTTLE);
240 if (!dmabuf_ret || !dmabuf_ret->val.val_bool) {
241 ERR("The driver is not capable of dma-buf sharing."
242 "Abandon to load nine state tracker\n");
243 drm_destroy(&ctx->base);
244 return D3DERR_DRIVERINTERNALERROR;
245 }
246
247 if (throttle_ret && throttle_ret->val.val_int != -1) {
248 ctx->base.throttling = TRUE;
249 ctx->base.throttling_value = throttle_ret->val.val_int;
250 } else
251 ctx->base.throttling = FALSE;
252
253 driParseOptionInfo(&defaultInitOptions, __driConfigOptionsNine);
254 driParseConfigFiles(&userInitOptions, &defaultInitOptions, 0, "nine");
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 (ctx->base.thread_submit && (throttling_value_user == -2 || throttling_value_user == 0)) {
276 ctx->base.throttling_value = 0;
277 } else if (ctx->base.thread_submit) {
278 DBG("You have set a non standard throttling value in combination with thread_submit."
279 "We advise to use a throttling value of -2/0");
280 }
281 if (ctx->base.thread_submit && !different_device)
282 DBG("You have set thread_submit but do not use a different device than the server."
283 "You should not expect any benefit.");
284
285 if (driCheckOption(&userInitOptions, "override_vendorid", DRI_INT)) {
286 override_vendorid = driQueryOptioni(&userInitOptions, "override_vendorid");
287 }
288
289 if (driCheckOption(&userInitOptions, "discard_delayed_release", DRI_BOOL))
290 ctx->base.discard_delayed_release = driQueryOptionb(&userInitOptions, "discard_delayed_release");
291 else
292 ctx->base.discard_delayed_release = TRUE;
293
294 if (driCheckOption(&userInitOptions, "tearfree_discard", DRI_BOOL))
295 ctx->base.tearfree_discard = driQueryOptionb(&userInitOptions, "tearfree_discard");
296 else
297 ctx->base.tearfree_discard = FALSE;
298
299 if (ctx->base.tearfree_discard && !ctx->base.discard_delayed_release) {
300 ERR("tearfree_discard requires discard_delayed_release\n");
301 ctx->base.tearfree_discard = FALSE;
302 }
303
304 driDestroyOptionCache(&userInitOptions);
305 driDestroyOptionInfo(&defaultInitOptions);
306
307 /* wrap it to create a software screen that can share resources */
308 if (pipe_loader_sw_probe_wrapped(&ctx->swdev, ctx->base.hal))
309 ctx->base.ref = pipe_loader_create_screen(ctx->swdev);
310
311 if (!ctx->base.ref) {
312 ERR("Couldn't wrap drm screen to swrast screen. Software devices "
313 "will be unavailable.\n");
314 }
315
316 /* read out PCI info */
317 read_descriptor(&ctx->base, fd, override_vendorid);
318
319 /* create and return new ID3DAdapter9 */
320 hr = NineAdapter9_new(&ctx->base, (struct NineAdapter9 **)ppAdapter);
321 if (FAILED(hr)) {
322 drm_destroy(&ctx->base);
323 return hr;
324 }
325
326 return D3D_OK;
327 }
328
329 const struct D3DAdapter9DRM drm9_desc = {
330 .major_version = D3DADAPTER9DRM_MAJOR,
331 .minor_version = D3DADAPTER9DRM_MINOR,
332 .create_adapter = drm_create_adapter
333 };