nine: Add state tracker nine for Direct3D9 (v3)
[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_sw_helper.h"
34 #include "state_tracker/drm_driver.h"
35
36 #include "d3dadapter/d3dadapter9.h"
37 #include "d3dadapter/drm.h"
38
39 #include <libdrm/drm.h>
40 #include <sys/ioctl.h>
41 #include <fcntl.h>
42 #include <stdio.h>
43
44 #define DBG_CHANNEL DBG_ADAPTER
45
46 #define VERSION_DWORD(hi, lo) \
47 ((DWORD)( \
48 ((DWORD)((hi) & 0xFFFF) << 16) | \
49 (DWORD)((lo) & 0xFFFF) \
50 ))
51
52 /* Regarding os versions, we should not define our own as that would simply be
53 * weird. Defaulting to Win2k/XP seems sane considering the origin of D3D9. The
54 * driver also defaults to being a generic D3D9 driver, which of course only
55 * matters if you're actually using the DDI. */
56 #define VERSION_HIGH VERSION_DWORD(0x0006, 0x000E) /* winxp, d3d9 */
57 #define VERSION_LOW VERSION_DWORD(0x0000, 0x0001) /* version, build */
58
59 struct d3dadapter9drm_context
60 {
61 struct d3dadapter9_context base;
62 struct pipe_loader_device *dev, *swdev;
63 };
64
65 static void
66 drm_destroy( struct d3dadapter9_context *ctx )
67 {
68 #if !GALLIUM_STATIC_TARGETS
69 struct d3dadapter9drm_context *drm = (struct d3dadapter9drm_context *)ctx;
70
71 /* pipe_loader_sw destroys the context */
72 if (drm->swdev)
73 pipe_loader_release(&drm->swdev, 1);
74 if (drm->dev)
75 pipe_loader_release(&drm->dev, 1);
76 #endif
77
78 FREE(ctx);
79 }
80
81 /* read a DWORD in the form 0xnnnnnnnn, which is how sysfs pci id stuff is
82 * formatted. */
83 static INLINE DWORD
84 read_file_dword( const char *name )
85 {
86 char buf[32];
87 int fd, r;
88
89 fd = open(name, O_RDONLY);
90 if (fd < 0) {
91 DBG("Unable to get PCI information from `%s'\n", name);
92 return 0;
93 }
94
95 r = read(fd, buf, 32);
96 close(fd);
97
98 return (r > 0) ? (DWORD)strtol(buf, NULL, 0) : 0;
99 }
100
101 /* sysfs doesn't expose the revision as its own file, so this function grabs a
102 * dword at an offset in the raw PCI header. The reason this isn't used for all
103 * data is that the kernel will make corrections but not expose them in the raw
104 * header bytes. */
105 static INLINE DWORD
106 read_config_dword( int fd,
107 unsigned offset )
108 {
109 DWORD r = 0;
110
111 if (lseek(fd, offset, SEEK_SET) != offset) { return 0; }
112 if (read(fd, &r, 4) != 4) { return 0; }
113
114 return r;
115 }
116
117 static INLINE void
118 get_bus_info( int fd,
119 DWORD *vendorid,
120 DWORD *deviceid,
121 DWORD *subsysid,
122 DWORD *revision )
123 {
124 drm_unique_t u;
125
126 u.unique_len = 0;
127 u.unique = NULL;
128
129 if (ioctl(fd, DRM_IOCTL_GET_UNIQUE, &u)) { return; }
130 u.unique = CALLOC(u.unique_len+1, 1);
131
132 if (ioctl(fd, DRM_IOCTL_GET_UNIQUE, &u)) { return; }
133 u.unique[u.unique_len] = '\0';
134
135 DBG("DRM Device BusID: %s\n", u.unique);
136 if (strncmp("pci:", u.unique, 4) == 0) {
137 char fname[512]; /* this ought to be enough */
138 int l = snprintf(fname, 512, "/sys/bus/pci/devices/%s/", u.unique+4);
139
140 /* VendorId */
141 snprintf(fname+l, 512-l, "vendor");
142 *vendorid = read_file_dword(fname);
143 /* DeviceId */
144 snprintf(fname+l, 512-l, "device");
145 *deviceid = read_file_dword(fname);
146 /* SubSysId */
147 snprintf(fname+l, 512-l, "subsystem_device");
148 *subsysid = (read_file_dword(fname) << 16) & 0xFFFF0000;
149 snprintf(fname+l, 512-l, "subsystem_vendor");
150 *subsysid |= read_file_dword(fname) & 0x0000FFFF;
151 /* Revision */
152 {
153 int cfgfd;
154
155 snprintf(fname+l, 512-l, "config");
156 cfgfd = open(fname, O_RDONLY);
157 if (cfgfd >= 0) {
158 *revision = read_config_dword(cfgfd, 0x8) & 0x000000FF;
159 close(cfgfd);
160 } else {
161 DBG("Unable to get raw PCI information from `%s'\n", fname);
162 }
163 }
164 DBG("PCI info: vendor=0x%04x, device=0x%04x, subsys=0x%08x, rev=%d\n",
165 *vendorid, *deviceid, *subsysid, *revision);
166 } else {
167 DBG("Unsupported BusID type.\n");
168 }
169
170 FREE(u.unique);
171 }
172
173 static INLINE void
174 read_descriptor( struct d3dadapter9_context *ctx,
175 int fd )
176 {
177 D3DADAPTER_IDENTIFIER9 *drvid = &ctx->identifier;
178
179 memset(drvid, 0, sizeof(*drvid));
180 get_bus_info(fd, &drvid->VendorId, &drvid->DeviceId,
181 &drvid->SubSysId, &drvid->Revision);
182
183 strncpy(drvid->Driver, "libd3dadapter9.so", sizeof(drvid->Driver));
184 strncpy(drvid->DeviceName, ctx->hal->get_name(ctx->hal), 32);
185 snprintf(drvid->Description, sizeof(drvid->Description),
186 "Gallium 0.4 with %s", ctx->hal->get_vendor(ctx->hal));
187
188 drvid->DriverVersionLowPart = VERSION_LOW;
189 drvid->DriverVersionHighPart = VERSION_HIGH;
190
191 /* To make a pseudo-real GUID we use the PCI bus data and some string */
192 drvid->DeviceIdentifier.Data1 = drvid->VendorId;
193 drvid->DeviceIdentifier.Data2 = drvid->DeviceId;
194 drvid->DeviceIdentifier.Data3 = drvid->SubSysId;
195 memcpy(drvid->DeviceIdentifier.Data4, "Gallium3D", 8);
196
197 drvid->WHQLLevel = 1; /* This fakes WHQL validaion */
198
199 /* XXX Fake NVIDIA binary driver on Windows.
200 *
201 * OS version: 4=95/98/NT4, 5=2000, 6=2000/XP, 7=Vista, 8=Win7
202 */
203 strncpy(drvid->Driver, "nvd3dum.dll", sizeof(drvid->Driver));
204 strncpy(drvid->Description, "NVIDIA GeForce GTX 680", sizeof(drvid->Description));
205 drvid->DriverVersionLowPart = VERSION_DWORD(12, 6658); /* minor, build */
206 drvid->DriverVersionHighPart = VERSION_DWORD(6, 15); /* OS, major */
207 drvid->SubSysId = 0;
208 drvid->Revision = 0;
209 drvid->DeviceIdentifier.Data1 = 0xaeb2cdd4;
210 drvid->DeviceIdentifier.Data2 = 0x6e41;
211 drvid->DeviceIdentifier.Data3 = 0x43ea;
212 drvid->DeviceIdentifier.Data4[0] = 0x94;
213 drvid->DeviceIdentifier.Data4[1] = 0x1c;
214 drvid->DeviceIdentifier.Data4[2] = 0x83;
215 drvid->DeviceIdentifier.Data4[3] = 0x61;
216 drvid->DeviceIdentifier.Data4[4] = 0xcc;
217 drvid->DeviceIdentifier.Data4[5] = 0x76;
218 drvid->DeviceIdentifier.Data4[6] = 0x07;
219 drvid->DeviceIdentifier.Data4[7] = 0x81;
220 drvid->WHQLLevel = 0;
221 }
222
223 static HRESULT WINAPI
224 drm_create_adapter( int fd,
225 ID3DAdapter9 **ppAdapter )
226 {
227 struct d3dadapter9drm_context *ctx = CALLOC_STRUCT(d3dadapter9drm_context);
228 HRESULT hr;
229 int i, different_device;
230 const struct drm_conf_ret *throttle_ret = NULL;
231 const struct drm_conf_ret *dmabuf_ret = NULL;
232
233 #if !GALLIUM_STATIC_TARGETS
234 const char *paths[] = {
235 getenv("D3D9_DRIVERS_PATH"),
236 getenv("D3D9_DRIVERS_DIR"),
237 PIPE_SEARCH_DIR
238 };
239 #endif
240
241 if (!ctx) { return E_OUTOFMEMORY; }
242
243 ctx->base.destroy = drm_destroy;
244
245 fd = loader_get_user_preferred_fd(fd, &different_device);
246 ctx->base.linear_framebuffer = !!different_device;
247
248 #if GALLIUM_STATIC_TARGETS
249 ctx->base.hal = dd_create_screen(fd);
250 #else
251 /* use pipe-loader to dlopen appropriate drm driver */
252 if (!pipe_loader_drm_probe_fd(&ctx->dev, fd, FALSE)) {
253 ERR("Failed to probe drm fd %d.\n", fd);
254 FREE(ctx);
255 close(fd);
256 return D3DERR_DRIVERINTERNALERROR;
257 }
258
259 /* use pipe-loader to create a drm screen (hal) */
260 ctx->base.hal = NULL;
261 for (i = 0; !ctx->base.hal && i < Elements(paths); ++i) {
262 if (!paths[i]) { continue; }
263 ctx->base.hal = pipe_loader_create_screen(ctx->dev, paths[i]);
264 }
265 #endif
266 if (!ctx->base.hal) {
267 ERR("Unable to load requested driver.\n");
268 drm_destroy(&ctx->base);
269 return D3DERR_DRIVERINTERNALERROR;
270 }
271
272 #if GALLIUM_STATIC_TARGETS
273 dmabuf_ret = dd_configuration(DRM_CONF_SHARE_FD);
274 throttle_ret = dd_configuration(DRM_CONF_THROTTLE);
275 #else
276 dmabuf_ret = pipe_loader_configuration(ctx->dev, DRM_CONF_SHARE_FD);
277 throttle_ret = pipe_loader_configuration(ctx->dev, DRM_CONF_THROTTLE);
278 #endif // GALLIUM_STATIC_TARGETS
279 if (!dmabuf_ret || !dmabuf_ret->val.val_bool) {
280 ERR("The driver is not capable of dma-buf sharing."
281 "Abandon to load nine state tracker\n");
282 drm_destroy(&ctx->base);
283 return D3DERR_DRIVERINTERNALERROR;
284 }
285
286 if (throttle_ret && throttle_ret->val.val_int != -1) {
287 ctx->base.throttling = TRUE;
288 ctx->base.throttling_value = throttle_ret->val.val_int;
289 } else
290 ctx->base.throttling = FALSE;
291
292
293 #if GALLIUM_STATIC_TARGETS
294 ctx->base.ref = ninesw_create_screen(ctx->base.hal);
295 #else
296 /* wrap it to create a software screen that can share resources */
297 if (pipe_loader_sw_probe_wrapped(&ctx->swdev, ctx->base.hal)) {
298 ctx->base.ref = NULL;
299 for (i = 0; !ctx->base.ref && i < Elements(paths); ++i) {
300 if (!paths[i]) { continue; }
301 ctx->base.ref = pipe_loader_create_screen(ctx->swdev, paths[i]);
302 }
303 }
304 #endif
305 if (!ctx->base.ref) {
306 ERR("Couldn't wrap drm screen to swrast screen. Software devices "
307 "will be unavailable.\n");
308 }
309
310 /* read out PCI info */
311 read_descriptor(&ctx->base, fd);
312
313 /* create and return new ID3DAdapter9 */
314 hr = NineAdapter9_new(&ctx->base, (struct NineAdapter9 **)ppAdapter);
315 if (FAILED(hr)) {
316 drm_destroy(&ctx->base);
317 return hr;
318 }
319
320 return D3D_OK;
321 }
322
323 const struct D3DAdapter9DRM drm9_desc = {
324 .major_version = D3DADAPTER9DRM_MAJOR,
325 .minor_version = D3DADAPTER9DRM_MINOR,
326 .create_adapter = drm_create_adapter
327 };