targets/nine: use the existing sw_screen_wrap() over our custom version
[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 #include "loader.h"
24
25 #include "adapter9.h"
26
27 #include "pipe-loader/pipe_loader.h"
28
29 #include "pipe/p_screen.h"
30 #include "pipe/p_state.h"
31
32 #include "target-helpers/inline_drm_helper.h"
33 #include "target-helpers/inline_wrapper_sw_helper.h"
34 #include "state_tracker/drm_driver.h"
35
36 #include "d3dadapter/d3dadapter9.h"
37 #include "d3dadapter/drm.h"
38
39 #include "xmlconfig.h"
40 #include "xmlpool.h"
41
42 #include <drm.h>
43 #include <sys/ioctl.h>
44 #include <fcntl.h>
45 #include <stdio.h>
46
47 #define DBG_CHANNEL DBG_ADAPTER
48
49 const char __driConfigOptionsNine[] =
50 DRI_CONF_BEGIN
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_THROTTLE(-2)
56 DRI_CONF_NINE_THREADSUBMIT("false")
57 DRI_CONF_SECTION_END
58 DRI_CONF_END;
59
60 /* define fallback value here: NVIDIA GeForce GTX 970 */
61 #define FALLBACK_NAME "NV124"
62 #define FALLBACK_DEVID 0x13C2
63 #define FALLBACK_VENID 0x10de
64
65 /* prototypes */
66 void
67 d3d_match_vendor_id( D3DADAPTER_IDENTIFIER9* drvid,
68 unsigned fallback_ven,
69 unsigned fallback_dev,
70 const char* fallback_name );
71
72 void d3d_fill_driver_version(D3DADAPTER_IDENTIFIER9* drvid);
73
74 void d3d_fill_cardname(D3DADAPTER_IDENTIFIER9* drvid);
75
76 struct d3dadapter9drm_context
77 {
78 struct d3dadapter9_context base;
79 struct pipe_loader_device *dev, *swdev;
80 int fd;
81 };
82
83 static void
84 drm_destroy( struct d3dadapter9_context *ctx )
85 {
86 struct d3dadapter9drm_context *drm = (struct d3dadapter9drm_context *)ctx;
87
88 if (ctx->ref)
89 ctx->ref->destroy(ctx->ref);
90 /* because ref is a wrapper around hal, freeing ref frees hal too. */
91 else if (ctx->hal)
92 ctx->hal->destroy(ctx->hal);
93
94 #if !GALLIUM_STATIC_TARGETS
95 if (drm->swdev)
96 pipe_loader_release(&drm->swdev, 1);
97 if (drm->dev)
98 pipe_loader_release(&drm->dev, 1);
99 #endif
100
101 close(drm->fd);
102 FREE(ctx);
103 }
104
105 /* read a DWORD in the form 0xnnnnnnnn, which is how sysfs pci id stuff is
106 * formatted. */
107 static inline DWORD
108 read_file_dword( const char *name )
109 {
110 char buf[32];
111 int fd, r;
112
113 fd = open(name, O_RDONLY);
114 if (fd < 0) {
115 DBG("Unable to get PCI information from `%s'\n", name);
116 return 0;
117 }
118
119 r = read(fd, buf, 32);
120 close(fd);
121
122 return (r > 0) ? (DWORD)strtol(buf, NULL, 0) : 0;
123 }
124
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
128 * header bytes. */
129 static inline DWORD
130 read_config_dword( int fd,
131 unsigned offset )
132 {
133 DWORD r = 0;
134
135 if (lseek(fd, offset, SEEK_SET) != offset) { return 0; }
136 if (read(fd, &r, 4) != 4) { return 0; }
137
138 return r;
139 }
140
141 static inline void
142 get_bus_info( int fd,
143 DWORD *vendorid,
144 DWORD *deviceid,
145 DWORD *subsysid,
146 DWORD *revision )
147 {
148 int vid, did;
149
150 if (loader_get_pci_id_for_fd(fd, &vid, &did)) {
151 DBG("PCI info: vendor=0x%04x, device=0x%04x\n",
152 vid, did);
153 *vendorid = vid;
154 *deviceid = did;
155 *subsysid = 0;
156 *revision = 0;
157 } else {
158 DBG("Unable to detect card. Faking %s\n", FALLBACK_NAME);
159 *vendorid = FALLBACK_VENID;
160 *deviceid = FALLBACK_DEVID;
161 *subsysid = 0;
162 *revision = 0;
163 }
164 }
165
166 static inline void
167 read_descriptor( struct d3dadapter9_context *ctx,
168 int fd )
169 {
170 D3DADAPTER_IDENTIFIER9 *drvid = &ctx->identifier;
171
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));
179
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);
187
188 /* this driver isn't WHQL certified */
189 drvid->WHQLLevel = 0;
190
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;
203 }
204
205 static HRESULT WINAPI
206 drm_create_adapter( int fd,
207 ID3DAdapter9 **ppAdapter )
208 {
209 struct d3dadapter9drm_context *ctx = CALLOC_STRUCT(d3dadapter9drm_context);
210 HRESULT hr;
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;
217
218 #if !GALLIUM_STATIC_TARGETS
219 const char *paths[] = {
220 getenv("D3D9_DRIVERS_PATH"),
221 getenv("D3D9_DRIVERS_DIR"),
222 PIPE_SEARCH_DIR
223 };
224 #endif
225
226 if (!ctx) { return E_OUTOFMEMORY; }
227
228 ctx->base.destroy = drm_destroy;
229
230 fd = loader_get_user_preferred_fd(fd, &different_device);
231 ctx->fd = fd;
232 ctx->base.linear_framebuffer = !!different_device;
233
234 #if GALLIUM_STATIC_TARGETS
235 ctx->base.hal = dd_create_screen(fd);
236 #else
237 /* use pipe-loader to dlopen appropriate drm driver */
238 if (!pipe_loader_drm_probe_fd(&ctx->dev, fd)) {
239 ERR("Failed to probe drm fd %d.\n", fd);
240 FREE(ctx);
241 close(fd);
242 return D3DERR_DRIVERINTERNALERROR;
243 }
244
245 /* use pipe-loader to create a drm screen (hal) */
246 ctx->base.hal = NULL;
247 for (i = 0; !ctx->base.hal && i < Elements(paths); ++i) {
248 if (!paths[i]) { continue; }
249 ctx->base.hal = pipe_loader_create_screen(ctx->dev, paths[i]);
250 }
251 #endif
252 if (!ctx->base.hal) {
253 ERR("Unable to load requested driver.\n");
254 drm_destroy(&ctx->base);
255 return D3DERR_DRIVERINTERNALERROR;
256 }
257
258 #if GALLIUM_STATIC_TARGETS
259 dmabuf_ret = dd_configuration(DRM_CONF_SHARE_FD);
260 throttle_ret = dd_configuration(DRM_CONF_THROTTLE);
261 #else
262 dmabuf_ret = pipe_loader_configuration(ctx->dev, DRM_CONF_SHARE_FD);
263 throttle_ret = pipe_loader_configuration(ctx->dev, DRM_CONF_THROTTLE);
264 #endif // GALLIUM_STATIC_TARGETS
265 if (!dmabuf_ret || !dmabuf_ret->val.val_bool) {
266 ERR("The driver is not capable of dma-buf sharing."
267 "Abandon to load nine state tracker\n");
268 drm_destroy(&ctx->base);
269 return D3DERR_DRIVERINTERNALERROR;
270 }
271
272 if (throttle_ret && throttle_ret->val.val_int != -1) {
273 ctx->base.throttling = TRUE;
274 ctx->base.throttling_value = throttle_ret->val.val_int;
275 } else
276 ctx->base.throttling = FALSE;
277
278 driParseOptionInfo(&defaultInitOptions, __driConfigOptionsNine);
279 driParseConfigFiles(&userInitOptions, &defaultInitOptions, 0, "nine");
280 if (driCheckOption(&userInitOptions, "throttle_value", DRI_INT)) {
281 throttling_value_user = driQueryOptioni(&userInitOptions, "throttle_value");
282 if (throttling_value_user == -1)
283 ctx->base.throttling = FALSE;
284 else if (throttling_value_user >= 0) {
285 ctx->base.throttling = TRUE;
286 ctx->base.throttling_value = throttling_value_user;
287 }
288 }
289
290 if (driCheckOption(&userInitOptions, "vblank_mode", DRI_ENUM))
291 ctx->base.vblank_mode = driQueryOptioni(&userInitOptions, "vblank_mode");
292 else
293 ctx->base.vblank_mode = 1;
294
295 if (driCheckOption(&userInitOptions, "thread_submit", DRI_BOOL)) {
296 ctx->base.thread_submit = driQueryOptionb(&userInitOptions, "thread_submit");
297 if (ctx->base.thread_submit && (throttling_value_user == -2 || throttling_value_user == 0)) {
298 ctx->base.throttling_value = 0;
299 } else if (ctx->base.thread_submit) {
300 DBG("You have set a non standard throttling value in combination with thread_submit."
301 "We advise to use a throttling value of -2/0");
302 }
303 if (ctx->base.thread_submit && !different_device)
304 DBG("You have set thread_submit but do not use a different device than the server."
305 "You should not expect any benefit.");
306 }
307
308 driDestroyOptionCache(&userInitOptions);
309 driDestroyOptionInfo(&defaultInitOptions);
310
311 #if GALLIUM_STATIC_TARGETS
312 ctx->base.ref = sw_screen_wrap(ctx->base.hal);
313 #else
314 /* wrap it to create a software screen that can share resources */
315 if (pipe_loader_sw_probe_wrapped(&ctx->swdev, ctx->base.hal)) {
316 ctx->base.ref = NULL;
317 for (i = 0; !ctx->base.ref && i < Elements(paths); ++i) {
318 if (!paths[i]) { continue; }
319 ctx->base.ref = pipe_loader_create_screen(ctx->swdev, paths[i]);
320 }
321 }
322 #endif
323 if (!ctx->base.ref) {
324 ERR("Couldn't wrap drm screen to swrast screen. Software devices "
325 "will be unavailable.\n");
326 }
327
328 /* read out PCI info */
329 read_descriptor(&ctx->base, fd);
330
331 /* create and return new ID3DAdapter9 */
332 hr = NineAdapter9_new(&ctx->base, (struct NineAdapter9 **)ppAdapter);
333 if (FAILED(hr)) {
334 drm_destroy(&ctx->base);
335 return hr;
336 }
337
338 return D3D_OK;
339 }
340
341 const struct D3DAdapter9DRM drm9_desc = {
342 .major_version = D3DADAPTER9DRM_MAJOR,
343 .minor_version = D3DADAPTER9DRM_MINOR,
344 .create_adapter = drm_create_adapter
345 };