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 "target-helpers/sw_helper.h" 35#include "frontend/drm_driver.h" 36 37#include "d3dadapter/d3dadapter9.h" 38#include "d3dadapter/drm.h" 39 40#include "util/xmlconfig.h" 41#include "util/driconf.h" 42 43#include "drm-uapi/drm.h" 44#include <sys/ioctl.h> 45#include <fcntl.h> 46#include <stdio.h> 47 48#define DBG_CHANNEL DBG_ADAPTER 49 50/* On non-x86 archs, Box86 has issues with thread_submit. */ 51#if defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64) 52#define DEFAULT_THREADSUBMIT true 53#else 54#define DEFAULT_THREADSUBMIT false 55#endif 56 57const driOptionDescription __driConfigOptionsNine[] = { 58 DRI_CONF_SECTION_PERFORMANCE 59 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_1) 60 DRI_CONF_SECTION_END 61 DRI_CONF_SECTION_NINE 62 DRI_CONF_NINE_OVERRIDEVENDOR(-1) 63 DRI_CONF_NINE_THROTTLE(-2) 64 DRI_CONF_NINE_THREADSUBMIT(DEFAULT_THREADSUBMIT) 65 DRI_CONF_NINE_ALLOWDISCARDDELAYEDRELEASE(true) 66 DRI_CONF_NINE_TEARFREEDISCARD(true) 67 DRI_CONF_NINE_CSMT(-1) 68 DRI_CONF_NINE_DYNAMICTEXTUREWORKAROUND(true) 69 DRI_CONF_NINE_SHADERINLINECONSTANTS(false) 70 DRI_CONF_NINE_SHMEM_LIMIT() 71 DRI_CONF_NINE_FORCESWRENDERINGONCPU(false) 72 DRI_CONF_SECTION_END 73 DRI_CONF_SECTION_DEBUG 74 DRI_CONF_OVERRIDE_VRAM_SIZE() 75 DRI_CONF_SECTION_END 76}; 77 78struct fallback_card_config { 79 const char *name; 80 unsigned vendor_id; 81 unsigned device_id; 82} fallback_cards[] = { 83 {"NV124", 0x10de, 0x13C2}, /* NVIDIA GeForce GTX 970 */ 84 {"HAWAII", 0x1002, 0x67b1}, /* AMD Radeon R9 290 */ 85 {"Haswell Mobile", 0x8086, 0x13C2}, /* Intel Haswell Mobile */ 86 {"SVGA3D", 0x15ad, 0x0405}, /* VMware SVGA 3D */ 87}; 88 89/* prototypes */ 90void 91d3d_match_vendor_id( D3DADAPTER_IDENTIFIER9* drvid, 92 unsigned fallback_ven, 93 unsigned fallback_dev, 94 const char* fallback_name ); 95 96void d3d_fill_driver_version(D3DADAPTER_IDENTIFIER9* drvid); 97 98void d3d_fill_cardname(D3DADAPTER_IDENTIFIER9* drvid); 99 100struct d3dadapter9drm_context 101{ 102 struct d3dadapter9_context base; 103 struct pipe_loader_device *dev, *swdev; 104 int fd; 105}; 106 107static void 108drm_destroy( struct d3dadapter9_context *ctx ) 109{ 110 struct d3dadapter9drm_context *drm = (struct d3dadapter9drm_context *)ctx; 111 112 if (ctx->ref && ctx->hal != ctx->ref) 113 ctx->ref->destroy(ctx->ref); 114 /* because ref is a wrapper around hal, freeing ref frees hal too. */ 115 else if (ctx->hal) 116 ctx->hal->destroy(ctx->hal); 117 118 if (drm->swdev && drm->swdev != drm->dev) 119 pipe_loader_release(&drm->swdev, 1); 120 if (drm->dev) 121 pipe_loader_release(&drm->dev, 1); 122 123 close(drm->fd); 124 FREE(ctx); 125} 126 127static inline void 128get_bus_info( int fd, 129 DWORD *vendorid, 130 DWORD *deviceid, 131 DWORD *subsysid, 132 DWORD *revision ) 133{ 134 int vid, did; 135 136 if (loader_get_pci_id_for_fd(fd, &vid, &did)) { 137 DBG("PCI info: vendor=0x%04x, device=0x%04x\n", 138 vid, did); 139 *vendorid = vid; 140 *deviceid = did; 141 *subsysid = 0; 142 *revision = 0; 143 } else { 144 DBG("Unable to detect card. Faking %s\n", fallback_cards[0].name); 145 *vendorid = fallback_cards[0].vendor_id; 146 *deviceid = fallback_cards[0].device_id; 147 *subsysid = 0; 148 *revision = 0; 149 } 150} 151 152static inline void 153read_descriptor( struct d3dadapter9_context *ctx, 154 int fd, int override_vendorid ) 155{ 156 unsigned i; 157 BOOL found; 158 D3DADAPTER_IDENTIFIER9 *drvid = &ctx->identifier; 159 160 memset(drvid, 0, sizeof(*drvid)); 161 get_bus_info(fd, &drvid->VendorId, &drvid->DeviceId, 162 &drvid->SubSysId, &drvid->Revision); 163 snprintf(drvid->DeviceName, sizeof(drvid->DeviceName), 164 "Gallium 0.4 with %s", ctx->hal->get_vendor(ctx->hal)); 165 snprintf(drvid->Description, sizeof(drvid->Description), 166 "%s", ctx->hal->get_name(ctx->hal)); 167 168 if (override_vendorid > 0) { 169 found = FALSE; 170 /* fill in device_id and card name for fake vendor */ 171 for (i = 0; i < sizeof(fallback_cards)/sizeof(fallback_cards[0]); i++) { 172 if (fallback_cards[i].vendor_id == override_vendorid) { 173 DBG("Faking card '%s' vendor 0x%04x, device 0x%04x\n", 174 fallback_cards[i].name, 175 fallback_cards[i].vendor_id, 176 fallback_cards[i].device_id); 177 drvid->VendorId = fallback_cards[i].vendor_id; 178 drvid->DeviceId = fallback_cards[i].device_id; 179 snprintf(drvid->Description, sizeof(drvid->Description), 180 "%s", fallback_cards[i].name); 181 found = TRUE; 182 break; 183 } 184 } 185 if (!found) { 186 DBG("Unknown fake vendor 0x%04x! Using detected vendor !\n", override_vendorid); 187 } 188 } 189 /* choose fall-back vendor if necessary to allow 190 * the following functions to return sane results */ 191 d3d_match_vendor_id(drvid, fallback_cards[0].vendor_id, fallback_cards[0].device_id, fallback_cards[0].name); 192 /* fill in driver name and version info */ 193 d3d_fill_driver_version(drvid); 194 /* override Description field with Windows like names */ 195 d3d_fill_cardname(drvid); 196 197 /* this driver isn't WHQL certified */ 198 drvid->WHQLLevel = 0; 199 200 /* this value is fixed */ 201 drvid->DeviceIdentifier.Data1 = 0xaeb2cdd4; 202 drvid->DeviceIdentifier.Data2 = 0x6e41; 203 drvid->DeviceIdentifier.Data3 = 0x43ea; 204 drvid->DeviceIdentifier.Data4[0] = 0x94; 205 drvid->DeviceIdentifier.Data4[1] = 0x1c; 206 drvid->DeviceIdentifier.Data4[2] = 0x83; 207 drvid->DeviceIdentifier.Data4[3] = 0x61; 208 drvid->DeviceIdentifier.Data4[4] = 0xcc; 209 drvid->DeviceIdentifier.Data4[5] = 0x76; 210 drvid->DeviceIdentifier.Data4[6] = 0x07; 211 drvid->DeviceIdentifier.Data4[7] = 0x81; 212} 213 214static HRESULT WINAPI 215drm_create_adapter( int fd, 216 ID3DAdapter9 **ppAdapter ) 217{ 218 struct d3dadapter9drm_context *ctx = CALLOC_STRUCT(d3dadapter9drm_context); 219 HRESULT hr; 220 bool different_device; 221 driOptionCache defaultInitOptions; 222 driOptionCache userInitOptions; 223 int throttling_value_user = -2; 224 int override_vendorid = -1; 225 bool sw_rendering; 226 227 if (!ctx) { return E_OUTOFMEMORY; } 228 229 ctx->base.destroy = drm_destroy; 230 231 /* Although the fd is provided from external source, mesa/nine 232 * takes ownership of it. */ 233 fd = loader_get_user_preferred_fd(fd, &different_device); 234 ctx->fd = fd; 235 ctx->base.linear_framebuffer = different_device; 236 237 if (!pipe_loader_drm_probe_fd(&ctx->dev, fd)) { 238 ERR("Failed to probe drm fd %d.\n", fd); 239 FREE(ctx); 240 close(fd); 241 return D3DERR_DRIVERINTERNALERROR; 242 } 243 244 ctx->base.hal = pipe_loader_create_screen(ctx->dev); 245 if (!ctx->base.hal) { 246 ERR("Unable to load requested driver.\n"); 247 drm_destroy(&ctx->base); 248 return D3DERR_DRIVERINTERNALERROR; 249 } 250 251 if (!ctx->base.hal->get_param(ctx->base.hal, PIPE_CAP_DMABUF)) { 252 ERR("The driver is not capable of dma-buf sharing." 253 "Abandon to load nine state tracker\n"); 254 drm_destroy(&ctx->base); 255 return D3DERR_DRIVERINTERNALERROR; 256 } 257 258 /* Previously was set to PIPE_CAP_MAX_FRAMES_IN_FLIGHT, 259 * but the change of value of this cap to 1 seems to cause 260 * regressions. */ 261 ctx->base.throttling_value = 2; 262 ctx->base.throttling = ctx->base.throttling_value > 0; 263 264 driParseOptionInfo(&defaultInitOptions, __driConfigOptionsNine, 265 ARRAY_SIZE(__driConfigOptionsNine)); 266 driParseConfigFiles(&userInitOptions, &defaultInitOptions, 0, 267 "nine", NULL, NULL, NULL, 0, NULL, 0); 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 ctx->base.vblank_mode = driQueryOptioni(&userInitOptions, "vblank_mode"); 279 ctx->base.thread_submit = driQueryOptionb(&userInitOptions, "thread_submit"); /* TODO: default to TRUE if different_device */ 280 override_vendorid = driQueryOptioni(&userInitOptions, "override_vendorid"); 281 282 ctx->base.discard_delayed_release = driQueryOptionb(&userInitOptions, "discard_delayed_release"); 283 ctx->base.tearfree_discard = driQueryOptionb(&userInitOptions, "tearfree_discard"); 284 285 if (ctx->base.tearfree_discard && !ctx->base.discard_delayed_release) { 286 ERR("tearfree_discard requires discard_delayed_release\n"); 287 ctx->base.tearfree_discard = FALSE; 288 } 289 290 ctx->base.csmt_force = driQueryOptioni(&userInitOptions, "csmt_force"); 291 ctx->base.dynamic_texture_workaround = driQueryOptionb(&userInitOptions, "dynamic_texture_workaround"); 292 ctx->base.shader_inline_constants = driQueryOptionb(&userInitOptions, "shader_inline_constants"); 293 ctx->base.memfd_virtualsizelimit = driQueryOptioni(&userInitOptions, "texture_memory_limit"); 294 ctx->base.override_vram_size = driQueryOptioni(&userInitOptions, "override_vram_size"); 295 sw_rendering = driQueryOptionb(&userInitOptions, "force_sw_rendering_on_cpu"); 296 297 driDestroyOptionCache(&userInitOptions); 298 driDestroyOptionInfo(&defaultInitOptions); 299 300 sw_rendering |= debug_get_bool_option("D3D_ALWAYS_SOFTWARE", false); 301 /* wrap it to create a software screen that can share resources */ 302 if (sw_rendering && pipe_loader_sw_probe_wrapped(&ctx->swdev, ctx->base.hal)) 303 ctx->base.ref = pipe_loader_create_screen(ctx->swdev); 304 else { 305 /* Use the hardware for sw rendering */ 306 ctx->swdev = ctx->dev; 307 ctx->base.ref = ctx->base.hal; 308 } 309 310 /* read out PCI info */ 311 read_descriptor(&ctx->base, fd, override_vendorid); 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 323const struct D3DAdapter9DRM drm9_desc = { 324 .major_version = D3DADAPTER9DRM_MAJOR, 325 .minor_version = D3DADAPTER9DRM_MINOR, 326 .create_adapter = drm_create_adapter 327}; 328