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