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. */
23 /* XXX: header order is slightly screwy here */
28 #include "pipe-loader/pipe_loader.h"
30 #include "pipe/p_screen.h"
31 #include "pipe/p_state.h"
33 #include "target-helpers/drm_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
);
95 pipe_loader_release(&drm
->swdev
, 1);
97 pipe_loader_release(&drm
->dev
, 1);
104 get_bus_info( int fd
,
112 if (loader_get_pci_id_for_fd(fd
, &vid
, &did
)) {
113 DBG("PCI info: vendor=0x%04x, device=0x%04x\n",
120 DBG("Unable to detect card. Faking %s\n", FALLBACK_NAME
);
121 *vendorid
= FALLBACK_VENID
;
122 *deviceid
= FALLBACK_DEVID
;
129 read_descriptor( struct d3dadapter9_context
*ctx
,
132 D3DADAPTER_IDENTIFIER9
*drvid
= &ctx
->identifier
;
134 memset(drvid
, 0, sizeof(*drvid
));
135 get_bus_info(fd
, &drvid
->VendorId
, &drvid
->DeviceId
,
136 &drvid
->SubSysId
, &drvid
->Revision
);
137 snprintf(drvid
->DeviceName
, sizeof(drvid
->DeviceName
),
138 "Gallium 0.4 with %s", ctx
->hal
->get_vendor(ctx
->hal
));
139 strncpy(drvid
->Description
, ctx
->hal
->get_name(ctx
->hal
),
140 sizeof(drvid
->Description
));
142 /* choose fall-back vendor if necessary to allow
143 * the following functions to return sane results */
144 d3d_match_vendor_id(drvid
, FALLBACK_VENID
, FALLBACK_DEVID
, FALLBACK_NAME
);
145 /* fill in driver name and version info */
146 d3d_fill_driver_version(drvid
);
147 /* override Description field with Windows like names */
148 d3d_fill_cardname(drvid
);
150 /* this driver isn't WHQL certified */
151 drvid
->WHQLLevel
= 0;
153 /* this value is fixed */
154 drvid
->DeviceIdentifier
.Data1
= 0xaeb2cdd4;
155 drvid
->DeviceIdentifier
.Data2
= 0x6e41;
156 drvid
->DeviceIdentifier
.Data3
= 0x43ea;
157 drvid
->DeviceIdentifier
.Data4
[0] = 0x94;
158 drvid
->DeviceIdentifier
.Data4
[1] = 0x1c;
159 drvid
->DeviceIdentifier
.Data4
[2] = 0x83;
160 drvid
->DeviceIdentifier
.Data4
[3] = 0x61;
161 drvid
->DeviceIdentifier
.Data4
[4] = 0xcc;
162 drvid
->DeviceIdentifier
.Data4
[5] = 0x76;
163 drvid
->DeviceIdentifier
.Data4
[6] = 0x07;
164 drvid
->DeviceIdentifier
.Data4
[7] = 0x81;
167 static HRESULT WINAPI
168 drm_create_adapter( int fd
,
169 ID3DAdapter9
**ppAdapter
)
171 struct d3dadapter9drm_context
*ctx
= CALLOC_STRUCT(d3dadapter9drm_context
);
173 int different_device
;
174 const struct drm_conf_ret
*throttle_ret
= NULL
;
175 const struct drm_conf_ret
*dmabuf_ret
= NULL
;
176 driOptionCache defaultInitOptions
;
177 driOptionCache userInitOptions
;
178 int throttling_value_user
= -2;
180 if (!ctx
) { return E_OUTOFMEMORY
; }
182 ctx
->base
.destroy
= drm_destroy
;
184 /* Although the fd is provided from external source, mesa/nine
185 * takes ownership of it. */
186 fd
= loader_get_user_preferred_fd(fd
, &different_device
);
188 ctx
->base
.linear_framebuffer
= !!different_device
;
190 if (!pipe_loader_drm_probe_fd(&ctx
->dev
, fd
)) {
191 ERR("Failed to probe drm fd %d.\n", fd
);
194 return D3DERR_DRIVERINTERNALERROR
;
197 ctx
->base
.hal
= pipe_loader_create_screen(ctx
->dev
);
198 if (!ctx
->base
.hal
) {
199 ERR("Unable to load requested driver.\n");
200 drm_destroy(&ctx
->base
);
201 return D3DERR_DRIVERINTERNALERROR
;
204 dmabuf_ret
= pipe_loader_configuration(ctx
->dev
, DRM_CONF_SHARE_FD
);
205 throttle_ret
= pipe_loader_configuration(ctx
->dev
, DRM_CONF_THROTTLE
);
206 if (!dmabuf_ret
|| !dmabuf_ret
->val
.val_bool
) {
207 ERR("The driver is not capable of dma-buf sharing."
208 "Abandon to load nine state tracker\n");
209 drm_destroy(&ctx
->base
);
210 return D3DERR_DRIVERINTERNALERROR
;
213 if (throttle_ret
&& throttle_ret
->val
.val_int
!= -1) {
214 ctx
->base
.throttling
= TRUE
;
215 ctx
->base
.throttling_value
= throttle_ret
->val
.val_int
;
217 ctx
->base
.throttling
= FALSE
;
219 driParseOptionInfo(&defaultInitOptions
, __driConfigOptionsNine
);
220 driParseConfigFiles(&userInitOptions
, &defaultInitOptions
, 0, "nine");
221 if (driCheckOption(&userInitOptions
, "throttle_value", DRI_INT
)) {
222 throttling_value_user
= driQueryOptioni(&userInitOptions
, "throttle_value");
223 if (throttling_value_user
== -1)
224 ctx
->base
.throttling
= FALSE
;
225 else if (throttling_value_user
>= 0) {
226 ctx
->base
.throttling
= TRUE
;
227 ctx
->base
.throttling_value
= throttling_value_user
;
231 if (driCheckOption(&userInitOptions
, "vblank_mode", DRI_ENUM
))
232 ctx
->base
.vblank_mode
= driQueryOptioni(&userInitOptions
, "vblank_mode");
234 ctx
->base
.vblank_mode
= 1;
236 if (driCheckOption(&userInitOptions
, "thread_submit", DRI_BOOL
)) {
237 ctx
->base
.thread_submit
= driQueryOptionb(&userInitOptions
, "thread_submit");
238 if (ctx
->base
.thread_submit
&& (throttling_value_user
== -2 || throttling_value_user
== 0)) {
239 ctx
->base
.throttling_value
= 0;
240 } else if (ctx
->base
.thread_submit
) {
241 DBG("You have set a non standard throttling value in combination with thread_submit."
242 "We advise to use a throttling value of -2/0");
244 if (ctx
->base
.thread_submit
&& !different_device
)
245 DBG("You have set thread_submit but do not use a different device than the server."
246 "You should not expect any benefit.");
249 driDestroyOptionCache(&userInitOptions
);
250 driDestroyOptionInfo(&defaultInitOptions
);
252 /* wrap it to create a software screen that can share resources */
253 if (pipe_loader_sw_probe_wrapped(&ctx
->swdev
, ctx
->base
.hal
))
254 ctx
->base
.ref
= pipe_loader_create_screen(ctx
->swdev
);
256 if (!ctx
->base
.ref
) {
257 ERR("Couldn't wrap drm screen to swrast screen. Software devices "
258 "will be unavailable.\n");
261 /* read out PCI info */
262 read_descriptor(&ctx
->base
, fd
);
264 /* create and return new ID3DAdapter9 */
265 hr
= NineAdapter9_new(&ctx
->base
, (struct NineAdapter9
**)ppAdapter
);
267 drm_destroy(&ctx
->base
);
274 const struct D3DAdapter9DRM drm9_desc
= {
275 .major_version
= D3DADAPTER9DRM_MAJOR
,
276 .minor_version
= D3DADAPTER9DRM_MINOR
,
277 .create_adapter
= drm_create_adapter