Merge remote-tracking branch 'mesa-public/master' into vulkan
[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 /* XXX: header order is slightly screwy here */
24 #include "loader.h"
25
26 #include "adapter9.h"
27
28 #include "pipe-loader/pipe_loader.h"
29
30 #include "pipe/p_screen.h"
31 #include "pipe/p_state.h"
32
33 #include "target-helpers/drm_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 (drm->swdev)
95 pipe_loader_release(&drm->swdev, 1);
96 if (drm->dev)
97 pipe_loader_release(&drm->dev, 1);
98
99 close(drm->fd);
100 FREE(ctx);
101 }
102
103 static inline void
104 get_bus_info( int fd,
105 DWORD *vendorid,
106 DWORD *deviceid,
107 DWORD *subsysid,
108 DWORD *revision )
109 {
110 int vid, did;
111
112 if (loader_get_pci_id_for_fd(fd, &vid, &did)) {
113 DBG("PCI info: vendor=0x%04x, device=0x%04x\n",
114 vid, did);
115 *vendorid = vid;
116 *deviceid = did;
117 *subsysid = 0;
118 *revision = 0;
119 } else {
120 DBG("Unable to detect card. Faking %s\n", FALLBACK_NAME);
121 *vendorid = FALLBACK_VENID;
122 *deviceid = FALLBACK_DEVID;
123 *subsysid = 0;
124 *revision = 0;
125 }
126 }
127
128 static inline void
129 read_descriptor( struct d3dadapter9_context *ctx,
130 int fd )
131 {
132 D3DADAPTER_IDENTIFIER9 *drvid = &ctx->identifier;
133
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));
141
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);
149
150 /* this driver isn't WHQL certified */
151 drvid->WHQLLevel = 0;
152
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;
165 }
166
167 static HRESULT WINAPI
168 drm_create_adapter( int fd,
169 ID3DAdapter9 **ppAdapter )
170 {
171 struct d3dadapter9drm_context *ctx = CALLOC_STRUCT(d3dadapter9drm_context);
172 HRESULT hr;
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;
179
180 if (!ctx) { return E_OUTOFMEMORY; }
181
182 ctx->base.destroy = drm_destroy;
183
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);
187 ctx->fd = fd;
188 ctx->base.linear_framebuffer = !!different_device;
189
190 if (!pipe_loader_drm_probe_fd(&ctx->dev, fd)) {
191 ERR("Failed to probe drm fd %d.\n", fd);
192 FREE(ctx);
193 close(fd);
194 return D3DERR_DRIVERINTERNALERROR;
195 }
196
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;
202 }
203
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;
211 }
212
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;
216 } else
217 ctx->base.throttling = FALSE;
218
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;
228 }
229 }
230
231 if (driCheckOption(&userInitOptions, "vblank_mode", DRI_ENUM))
232 ctx->base.vblank_mode = driQueryOptioni(&userInitOptions, "vblank_mode");
233 else
234 ctx->base.vblank_mode = 1;
235
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");
243 }
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.");
247 }
248
249 driDestroyOptionCache(&userInitOptions);
250 driDestroyOptionInfo(&defaultInitOptions);
251
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);
255
256 if (!ctx->base.ref) {
257 ERR("Couldn't wrap drm screen to swrast screen. Software devices "
258 "will be unavailable.\n");
259 }
260
261 /* read out PCI info */
262 read_descriptor(&ctx->base, fd);
263
264 /* create and return new ID3DAdapter9 */
265 hr = NineAdapter9_new(&ctx->base, (struct NineAdapter9 **)ppAdapter);
266 if (FAILED(hr)) {
267 drm_destroy(&ctx->base);
268 return hr;
269 }
270
271 return D3D_OK;
272 }
273
274 const struct D3DAdapter9DRM drm9_desc = {
275 .major_version = D3DADAPTER9DRM_MAJOR,
276 .minor_version = D3DADAPTER9DRM_MINOR,
277 .create_adapter = drm_create_adapter
278 };