2 * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
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:
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
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. */
27 #include "pipe-loader/pipe_loader.h"
29 #include "pipe/p_screen.h"
30 #include "pipe/p_state.h"
32 #include "target-helpers/inline_drm_helper.h"
33 #include "target-helpers/inline_wrapper_sw_helper.h"
34 #include "state_tracker/drm_driver.h"
36 #include "d3dadapter/d3dadapter9.h"
37 #include "d3dadapter/drm.h"
39 #include "xmlconfig.h"
43 #include <sys/ioctl.h>
47 #define DBG_CHANNEL DBG_ADAPTER
49 const char __driConfigOptionsNine
[] =
51 DRI_CONF_SECTION_PERFORMANCE
52 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_1
)
55 DRI_CONF_NINE_THROTTLE(-2)
56 DRI_CONF_NINE_THREADSUBMIT("false")
60 /* define fallback value here: NVIDIA GeForce GTX 970 */
61 #define FALLBACK_NAME "NV124"
62 #define FALLBACK_DEVID 0x13C2
63 #define FALLBACK_VENID 0x10de
67 d3d_match_vendor_id( D3DADAPTER_IDENTIFIER9
* drvid
,
68 unsigned fallback_ven
,
69 unsigned fallback_dev
,
70 const char* fallback_name
);
72 void d3d_fill_driver_version(D3DADAPTER_IDENTIFIER9
* drvid
);
74 void d3d_fill_cardname(D3DADAPTER_IDENTIFIER9
* drvid
);
76 struct d3dadapter9drm_context
78 struct d3dadapter9_context base
;
79 struct pipe_loader_device
*dev
, *swdev
;
84 drm_destroy( struct d3dadapter9_context
*ctx
)
86 struct d3dadapter9drm_context
*drm
= (struct d3dadapter9drm_context
*)ctx
;
89 ctx
->ref
->destroy(ctx
->ref
);
90 /* because ref is a wrapper around hal, freeing ref frees hal too. */
92 ctx
->hal
->destroy(ctx
->hal
);
94 #if !GALLIUM_STATIC_TARGETS
96 pipe_loader_release(&drm
->swdev
, 1);
98 pipe_loader_release(&drm
->dev
, 1);
105 /* read a DWORD in the form 0xnnnnnnnn, which is how sysfs pci id stuff is
108 read_file_dword( const char *name
)
113 fd
= open(name
, O_RDONLY
);
115 DBG("Unable to get PCI information from `%s'\n", name
);
119 r
= read(fd
, buf
, 32);
122 return (r
> 0) ? (DWORD
)strtol(buf
, NULL
, 0) : 0;
125 /* sysfs doesn't expose the revision as its own file, so this function grabs a
126 * dword at an offset in the raw PCI header. The reason this isn't used for all
127 * data is that the kernel will make corrections but not expose them in the raw
130 read_config_dword( int fd
,
135 if (lseek(fd
, offset
, SEEK_SET
) != offset
) { return 0; }
136 if (read(fd
, &r
, 4) != 4) { return 0; }
142 get_bus_info( int fd
,
150 if (loader_get_pci_id_for_fd(fd
, &vid
, &did
)) {
151 DBG("PCI info: vendor=0x%04x, device=0x%04x\n",
158 DBG("Unable to detect card. Faking %s\n", FALLBACK_NAME
);
159 *vendorid
= FALLBACK_VENID
;
160 *deviceid
= FALLBACK_DEVID
;
167 read_descriptor( struct d3dadapter9_context
*ctx
,
170 D3DADAPTER_IDENTIFIER9
*drvid
= &ctx
->identifier
;
172 memset(drvid
, 0, sizeof(*drvid
));
173 get_bus_info(fd
, &drvid
->VendorId
, &drvid
->DeviceId
,
174 &drvid
->SubSysId
, &drvid
->Revision
);
175 snprintf(drvid
->DeviceName
, sizeof(drvid
->DeviceName
),
176 "Gallium 0.4 with %s", ctx
->hal
->get_vendor(ctx
->hal
));
177 strncpy(drvid
->Description
, ctx
->hal
->get_name(ctx
->hal
),
178 sizeof(drvid
->Description
));
180 /* choose fall-back vendor if necessary to allow
181 * the following functions to return sane results */
182 d3d_match_vendor_id(drvid
, FALLBACK_VENID
, FALLBACK_DEVID
, FALLBACK_NAME
);
183 /* fill in driver name and version info */
184 d3d_fill_driver_version(drvid
);
185 /* override Description field with Windows like names */
186 d3d_fill_cardname(drvid
);
188 /* this driver isn't WHQL certified */
189 drvid
->WHQLLevel
= 0;
191 /* this value is fixed */
192 drvid
->DeviceIdentifier
.Data1
= 0xaeb2cdd4;
193 drvid
->DeviceIdentifier
.Data2
= 0x6e41;
194 drvid
->DeviceIdentifier
.Data3
= 0x43ea;
195 drvid
->DeviceIdentifier
.Data4
[0] = 0x94;
196 drvid
->DeviceIdentifier
.Data4
[1] = 0x1c;
197 drvid
->DeviceIdentifier
.Data4
[2] = 0x83;
198 drvid
->DeviceIdentifier
.Data4
[3] = 0x61;
199 drvid
->DeviceIdentifier
.Data4
[4] = 0xcc;
200 drvid
->DeviceIdentifier
.Data4
[5] = 0x76;
201 drvid
->DeviceIdentifier
.Data4
[6] = 0x07;
202 drvid
->DeviceIdentifier
.Data4
[7] = 0x81;
205 static HRESULT WINAPI
206 drm_create_adapter( int fd
,
207 ID3DAdapter9
**ppAdapter
)
209 struct d3dadapter9drm_context
*ctx
= CALLOC_STRUCT(d3dadapter9drm_context
);
211 int different_device
;
212 const struct drm_conf_ret
*throttle_ret
= NULL
;
213 const struct drm_conf_ret
*dmabuf_ret
= NULL
;
214 driOptionCache defaultInitOptions
;
215 driOptionCache userInitOptions
;
216 int throttling_value_user
= -2;
218 if (!ctx
) { return E_OUTOFMEMORY
; }
220 ctx
->base
.destroy
= drm_destroy
;
222 fd
= loader_get_user_preferred_fd(fd
, &different_device
);
224 ctx
->base
.linear_framebuffer
= !!different_device
;
226 #if GALLIUM_STATIC_TARGETS
227 ctx
->base
.hal
= dd_create_screen(fd
);
229 /* use pipe-loader to dlopen appropriate drm driver */
230 if (!pipe_loader_drm_probe_fd(&ctx
->dev
, fd
)) {
231 ERR("Failed to probe drm fd %d.\n", fd
);
234 return D3DERR_DRIVERINTERNALERROR
;
237 /* use pipe-loader to create a drm screen (hal) */
238 ctx
->base
.hal
= pipe_loader_create_screen(ctx
->dev
, PIPE_SEARCH_DIR
);
240 if (!ctx
->base
.hal
) {
241 ERR("Unable to load requested driver.\n");
242 drm_destroy(&ctx
->base
);
243 return D3DERR_DRIVERINTERNALERROR
;
246 #if GALLIUM_STATIC_TARGETS
247 dmabuf_ret
= dd_configuration(DRM_CONF_SHARE_FD
);
248 throttle_ret
= dd_configuration(DRM_CONF_THROTTLE
);
250 dmabuf_ret
= pipe_loader_configuration(ctx
->dev
, DRM_CONF_SHARE_FD
);
251 throttle_ret
= pipe_loader_configuration(ctx
->dev
, DRM_CONF_THROTTLE
);
252 #endif // GALLIUM_STATIC_TARGETS
253 if (!dmabuf_ret
|| !dmabuf_ret
->val
.val_bool
) {
254 ERR("The driver is not capable of dma-buf sharing."
255 "Abandon to load nine state tracker\n");
256 drm_destroy(&ctx
->base
);
257 return D3DERR_DRIVERINTERNALERROR
;
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
;
264 ctx
->base
.throttling
= FALSE
;
266 driParseOptionInfo(&defaultInitOptions
, __driConfigOptionsNine
);
267 driParseConfigFiles(&userInitOptions
, &defaultInitOptions
, 0, "nine");
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
;
278 if (driCheckOption(&userInitOptions
, "vblank_mode", DRI_ENUM
))
279 ctx
->base
.vblank_mode
= driQueryOptioni(&userInitOptions
, "vblank_mode");
281 ctx
->base
.vblank_mode
= 1;
283 if (driCheckOption(&userInitOptions
, "thread_submit", DRI_BOOL
)) {
284 ctx
->base
.thread_submit
= driQueryOptionb(&userInitOptions
, "thread_submit");
285 if (ctx
->base
.thread_submit
&& (throttling_value_user
== -2 || throttling_value_user
== 0)) {
286 ctx
->base
.throttling_value
= 0;
287 } else if (ctx
->base
.thread_submit
) {
288 DBG("You have set a non standard throttling value in combination with thread_submit."
289 "We advise to use a throttling value of -2/0");
291 if (ctx
->base
.thread_submit
&& !different_device
)
292 DBG("You have set thread_submit but do not use a different device than the server."
293 "You should not expect any benefit.");
296 driDestroyOptionCache(&userInitOptions
);
297 driDestroyOptionInfo(&defaultInitOptions
);
299 #if GALLIUM_STATIC_TARGETS
300 ctx
->base
.ref
= sw_screen_wrap(ctx
->base
.hal
);
302 /* wrap it to create a software screen that can share resources */
303 if (pipe_loader_sw_probe_wrapped(&ctx
->swdev
, ctx
->base
.hal
)) {
304 ctx
->base
.ref
= pipe_loader_create_screen(ctx
->swdev
, PIPE_SEARCH_DIR
);
307 if (!ctx
->base
.ref
) {
308 ERR("Couldn't wrap drm screen to swrast screen. Software devices "
309 "will be unavailable.\n");
312 /* read out PCI info */
313 read_descriptor(&ctx
->base
, fd
);
315 /* create and return new ID3DAdapter9 */
316 hr
= NineAdapter9_new(&ctx
->base
, (struct NineAdapter9
**)ppAdapter
);
318 drm_destroy(&ctx
->base
);
325 const struct D3DAdapter9DRM drm9_desc
= {
326 .major_version
= D3DADAPTER9DRM_MAJOR
,
327 .minor_version
= D3DADAPTER9DRM_MINOR
,
328 .create_adapter
= drm_create_adapter