targets/nine: remove the custom pipe-driver path management
[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 (!ctx) { return E_OUTOFMEMORY; }
219
220 ctx->base.destroy = drm_destroy;
221
222 fd = loader_get_user_preferred_fd(fd, &different_device);
223 ctx->fd = fd;
224 ctx->base.linear_framebuffer = !!different_device;
225
226 #if GALLIUM_STATIC_TARGETS
227 ctx->base.hal = dd_create_screen(fd);
228 #else
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);
232 FREE(ctx);
233 close(fd);
234 return D3DERR_DRIVERINTERNALERROR;
235 }
236
237 /* use pipe-loader to create a drm screen (hal) */
238 ctx->base.hal = pipe_loader_create_screen(ctx->dev, PIPE_SEARCH_DIR);
239 #endif
240 if (!ctx->base.hal) {
241 ERR("Unable to load requested driver.\n");
242 drm_destroy(&ctx->base);
243 return D3DERR_DRIVERINTERNALERROR;
244 }
245
246 #if GALLIUM_STATIC_TARGETS
247 dmabuf_ret = dd_configuration(DRM_CONF_SHARE_FD);
248 throttle_ret = dd_configuration(DRM_CONF_THROTTLE);
249 #else
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;
258 }
259
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;
263 } else
264 ctx->base.throttling = FALSE;
265
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;
275 }
276 }
277
278 if (driCheckOption(&userInitOptions, "vblank_mode", DRI_ENUM))
279 ctx->base.vblank_mode = driQueryOptioni(&userInitOptions, "vblank_mode");
280 else
281 ctx->base.vblank_mode = 1;
282
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");
290 }
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.");
294 }
295
296 driDestroyOptionCache(&userInitOptions);
297 driDestroyOptionInfo(&defaultInitOptions);
298
299 #if GALLIUM_STATIC_TARGETS
300 ctx->base.ref = sw_screen_wrap(ctx->base.hal);
301 #else
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);
305 }
306 #endif
307 if (!ctx->base.ref) {
308 ERR("Couldn't wrap drm screen to swrast screen. Software devices "
309 "will be unavailable.\n");
310 }
311
312 /* read out PCI info */
313 read_descriptor(&ctx->base, fd);
314
315 /* create and return new ID3DAdapter9 */
316 hr = NineAdapter9_new(&ctx->base, (struct NineAdapter9 **)ppAdapter);
317 if (FAILED(hr)) {
318 drm_destroy(&ctx->base);
319 return hr;
320 }
321
322 return D3D_OK;
323 }
324
325 const struct D3DAdapter9DRM drm9_desc = {
326 .major_version = D3DADAPTER9DRM_MAJOR,
327 .minor_version = D3DADAPTER9DRM_MINOR,
328 .create_adapter = drm_create_adapter
329 };