1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2020 Intel Corporation 4 */ 5 6 #include <linux/slab.h> /* fault-inject.h is not standalone! */ 7 8 #include <linux/fault-inject.h> 9 #include <linux/sched/mm.h> 10 11 #include <drm/drm_cache.h> 12 13 #include "gem/i915_gem_internal.h" 14 #include "gem/i915_gem_lmem.h" 15 #include "i915_trace.h" 16 #include "intel_gt.h" 17 #include "intel_gt_regs.h" 18 #include "intel_gtt.h" 19 20 struct drm_i915_gem_object *alloc_pt_lmem(struct i915_address_space *vm, int sz) 21 { 22 struct drm_i915_gem_object *obj; 23 24 /* 25 * To avoid severe over-allocation when dealing with min_page_size 26 * restrictions, we override that behaviour here by allowing an object 27 * size and page layout which can be smaller. In practice this should be 28 * totally fine, since GTT paging structures are not typically inserted 29 * into the GTT. 30 * 31 * Note that we also hit this path for the scratch page, and for this 32 * case it might need to be 64K, but that should work fine here since we 33 * used the passed in size for the page size, which should ensure it 34 * also has the same alignment. 35 */ 36 obj = __i915_gem_object_create_lmem_with_ps(vm->i915, sz, sz, 37 vm->lmem_pt_obj_flags); 38 /* 39 * Ensure all paging structures for this vm share the same dma-resv 40 * object underneath, with the idea that one object_lock() will lock 41 * them all at once. 42 */ 43 if (!IS_ERR(obj)) { 44 obj->base.resv = i915_vm_resv_get(vm); 45 obj->shares_resv_from = vm; 46 } 47 48 return obj; 49 } 50 51 struct drm_i915_gem_object *alloc_pt_dma(struct i915_address_space *vm, int sz) 52 { 53 struct drm_i915_gem_object *obj; 54 55 if (I915_SELFTEST_ONLY(should_fail(&vm->fault_attr, 1))) 56 i915_gem_shrink_all(vm->i915); 57 58 obj = i915_gem_object_create_internal(vm->i915, sz); 59 /* 60 * Ensure all paging structures for this vm share the same dma-resv 61 * object underneath, with the idea that one object_lock() will lock 62 * them all at once. 63 */ 64 if (!IS_ERR(obj)) { 65 obj->base.resv = i915_vm_resv_get(vm); 66 obj->shares_resv_from = vm; 67 } 68 69 return obj; 70 } 71 72 int map_pt_dma(struct i915_address_space *vm, struct drm_i915_gem_object *obj) 73 { 74 enum i915_map_type type; 75 void *vaddr; 76 77 type = i915_coherent_map_type(vm->i915, obj, true); 78 vaddr = i915_gem_object_pin_map_unlocked(obj, type); 79 if (IS_ERR(vaddr)) 80 return PTR_ERR(vaddr); 81 82 i915_gem_object_make_unshrinkable(obj); 83 return 0; 84 } 85 86 int map_pt_dma_locked(struct i915_address_space *vm, struct drm_i915_gem_object *obj) 87 { 88 enum i915_map_type type; 89 void *vaddr; 90 91 type = i915_coherent_map_type(vm->i915, obj, true); 92 vaddr = i915_gem_object_pin_map(obj, type); 93 if (IS_ERR(vaddr)) 94 return PTR_ERR(vaddr); 95 96 i915_gem_object_make_unshrinkable(obj); 97 return 0; 98 } 99 100 void __i915_vm_close(struct i915_address_space *vm) 101 { 102 struct i915_vma *vma, *vn; 103 104 if (!atomic_dec_and_mutex_lock(&vm->open, &vm->mutex)) 105 return; 106 107 list_for_each_entry_safe(vma, vn, &vm->bound_list, vm_link) { 108 struct drm_i915_gem_object *obj = vma->obj; 109 110 /* Keep the obj (and hence the vma) alive as _we_ destroy it */ 111 if (!kref_get_unless_zero(&obj->base.refcount)) 112 continue; 113 114 atomic_and(~I915_VMA_PIN_MASK, &vma->flags); 115 WARN_ON(__i915_vma_unbind(vma)); 116 __i915_vma_put(vma); 117 118 i915_gem_object_put(obj); 119 } 120 GEM_BUG_ON(!list_empty(&vm->bound_list)); 121 122 mutex_unlock(&vm->mutex); 123 } 124 125 /* lock the vm into the current ww, if we lock one, we lock all */ 126 int i915_vm_lock_objects(struct i915_address_space *vm, 127 struct i915_gem_ww_ctx *ww) 128 { 129 if (vm->scratch[0]->base.resv == &vm->_resv) { 130 return i915_gem_object_lock(vm->scratch[0], ww); 131 } else { 132 struct i915_ppgtt *ppgtt = i915_vm_to_ppgtt(vm); 133 134 /* We borrowed the scratch page from ggtt, take the top level object */ 135 return i915_gem_object_lock(ppgtt->pd->pt.base, ww); 136 } 137 } 138 139 void i915_address_space_fini(struct i915_address_space *vm) 140 { 141 drm_mm_takedown(&vm->mm); 142 mutex_destroy(&vm->mutex); 143 } 144 145 /** 146 * i915_vm_resv_release - Final struct i915_address_space destructor 147 * @kref: Pointer to the &i915_address_space.resv_ref member. 148 * 149 * This function is called when the last lock sharer no longer shares the 150 * &i915_address_space._resv lock. 151 */ 152 void i915_vm_resv_release(struct kref *kref) 153 { 154 struct i915_address_space *vm = 155 container_of(kref, typeof(*vm), resv_ref); 156 157 dma_resv_fini(&vm->_resv); 158 kfree(vm); 159 } 160 161 static void __i915_vm_release(struct work_struct *work) 162 { 163 struct i915_address_space *vm = 164 container_of(work, struct i915_address_space, release_work); 165 166 /* Synchronize async unbinds. */ 167 i915_vma_resource_bind_dep_sync_all(vm); 168 169 vm->cleanup(vm); 170 i915_address_space_fini(vm); 171 172 i915_vm_resv_put(vm); 173 } 174 175 void i915_vm_release(struct kref *kref) 176 { 177 struct i915_address_space *vm = 178 container_of(kref, struct i915_address_space, ref); 179 180 GEM_BUG_ON(i915_is_ggtt(vm)); 181 trace_i915_ppgtt_release(vm); 182 183 queue_work(vm->i915->wq, &vm->release_work); 184 } 185 186 void i915_address_space_init(struct i915_address_space *vm, int subclass) 187 { 188 kref_init(&vm->ref); 189 190 /* 191 * Special case for GGTT that has already done an early 192 * kref_init here. 193 */ 194 if (!kref_read(&vm->resv_ref)) 195 kref_init(&vm->resv_ref); 196 197 vm->pending_unbind = RB_ROOT_CACHED; 198 INIT_WORK(&vm->release_work, __i915_vm_release); 199 atomic_set(&vm->open, 1); 200 201 /* 202 * The vm->mutex must be reclaim safe (for use in the shrinker). 203 * Do a dummy acquire now under fs_reclaim so that any allocation 204 * attempt holding the lock is immediately reported by lockdep. 205 */ 206 mutex_init(&vm->mutex); 207 lockdep_set_subclass(&vm->mutex, subclass); 208 209 if (!intel_vm_no_concurrent_access_wa(vm->i915)) { 210 i915_gem_shrinker_taints_mutex(vm->i915, &vm->mutex); 211 } else { 212 /* 213 * CHV + BXT VTD workaround use stop_machine(), 214 * which is allowed to allocate memory. This means &vm->mutex 215 * is the outer lock, and in theory we can allocate memory inside 216 * it through stop_machine(). 217 * 218 * Add the annotation for this, we use trylock in shrinker. 219 */ 220 mutex_acquire(&vm->mutex.dep_map, 0, 0, _THIS_IP_); 221 might_alloc(GFP_KERNEL); 222 mutex_release(&vm->mutex.dep_map, _THIS_IP_); 223 } 224 dma_resv_init(&vm->_resv); 225 226 GEM_BUG_ON(!vm->total); 227 drm_mm_init(&vm->mm, 0, vm->total); 228 vm->mm.head_node.color = I915_COLOR_UNEVICTABLE; 229 230 INIT_LIST_HEAD(&vm->bound_list); 231 } 232 233 void *__px_vaddr(struct drm_i915_gem_object *p) 234 { 235 enum i915_map_type type; 236 237 GEM_BUG_ON(!i915_gem_object_has_pages(p)); 238 return page_unpack_bits(p->mm.mapping, &type); 239 } 240 241 dma_addr_t __px_dma(struct drm_i915_gem_object *p) 242 { 243 GEM_BUG_ON(!i915_gem_object_has_pages(p)); 244 return sg_dma_address(p->mm.pages->sgl); 245 } 246 247 struct page *__px_page(struct drm_i915_gem_object *p) 248 { 249 GEM_BUG_ON(!i915_gem_object_has_pages(p)); 250 return sg_page(p->mm.pages->sgl); 251 } 252 253 void 254 fill_page_dma(struct drm_i915_gem_object *p, const u64 val, unsigned int count) 255 { 256 void *vaddr = __px_vaddr(p); 257 258 memset64(vaddr, val, count); 259 clflush_cache_range(vaddr, PAGE_SIZE); 260 } 261 262 static void poison_scratch_page(struct drm_i915_gem_object *scratch) 263 { 264 void *vaddr = __px_vaddr(scratch); 265 u8 val; 266 267 val = 0; 268 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) 269 val = POISON_FREE; 270 271 memset(vaddr, val, scratch->base.size); 272 drm_clflush_virt_range(vaddr, scratch->base.size); 273 } 274 275 int setup_scratch_page(struct i915_address_space *vm) 276 { 277 unsigned long size; 278 279 /* 280 * In order to utilize 64K pages for an object with a size < 2M, we will 281 * need to support a 64K scratch page, given that every 16th entry for a 282 * page-table operating in 64K mode must point to a properly aligned 64K 283 * region, including any PTEs which happen to point to scratch. 284 * 285 * This is only relevant for the 48b PPGTT where we support 286 * huge-gtt-pages, see also i915_vma_insert(). However, as we share the 287 * scratch (read-only) between all vm, we create one 64k scratch page 288 * for all. 289 */ 290 size = I915_GTT_PAGE_SIZE_4K; 291 if (i915_vm_is_4lvl(vm) && 292 HAS_PAGE_SIZES(vm->i915, I915_GTT_PAGE_SIZE_64K)) 293 size = I915_GTT_PAGE_SIZE_64K; 294 295 do { 296 struct drm_i915_gem_object *obj; 297 298 obj = vm->alloc_scratch_dma(vm, size); 299 if (IS_ERR(obj)) 300 goto skip; 301 302 if (map_pt_dma(vm, obj)) 303 goto skip_obj; 304 305 /* We need a single contiguous page for our scratch */ 306 if (obj->mm.page_sizes.sg < size) 307 goto skip_obj; 308 309 /* And it needs to be correspondingly aligned */ 310 if (__px_dma(obj) & (size - 1)) 311 goto skip_obj; 312 313 /* 314 * Use a non-zero scratch page for debugging. 315 * 316 * We want a value that should be reasonably obvious 317 * to spot in the error state, while also causing a GPU hang 318 * if executed. We prefer using a clear page in production, so 319 * should it ever be accidentally used, the effect should be 320 * fairly benign. 321 */ 322 poison_scratch_page(obj); 323 324 vm->scratch[0] = obj; 325 vm->scratch_order = get_order(size); 326 return 0; 327 328 skip_obj: 329 i915_gem_object_put(obj); 330 skip: 331 if (size == I915_GTT_PAGE_SIZE_4K) 332 return -ENOMEM; 333 334 /* 335 * If we need 64K minimum GTT pages for device local-memory, 336 * like on XEHPSDV, then we need to fail the allocation here, 337 * otherwise we can't safely support the insertion of 338 * local-memory pages for this vm, since the HW expects the 339 * correct physical alignment and size when the page-table is 340 * operating in 64K GTT mode, which includes any scratch PTEs, 341 * since userspace can still touch them. 342 */ 343 if (HAS_64K_PAGES(vm->i915)) 344 return -ENOMEM; 345 346 size = I915_GTT_PAGE_SIZE_4K; 347 } while (1); 348 } 349 350 void free_scratch(struct i915_address_space *vm) 351 { 352 int i; 353 354 for (i = 0; i <= vm->top; i++) 355 i915_gem_object_put(vm->scratch[i]); 356 } 357 358 void gtt_write_workarounds(struct intel_gt *gt) 359 { 360 struct drm_i915_private *i915 = gt->i915; 361 struct intel_uncore *uncore = gt->uncore; 362 363 /* 364 * This function is for gtt related workarounds. This function is 365 * called on driver load and after a GPU reset, so you can place 366 * workarounds here even if they get overwritten by GPU reset. 367 */ 368 /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt,kbl,glk,cfl,cnl,icl */ 369 if (IS_BROADWELL(i915)) 370 intel_uncore_write(uncore, 371 GEN8_L3_LRA_1_GPGPU, 372 GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW); 373 else if (IS_CHERRYVIEW(i915)) 374 intel_uncore_write(uncore, 375 GEN8_L3_LRA_1_GPGPU, 376 GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV); 377 else if (IS_GEN9_LP(i915)) 378 intel_uncore_write(uncore, 379 GEN8_L3_LRA_1_GPGPU, 380 GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT); 381 else if (GRAPHICS_VER(i915) >= 9 && GRAPHICS_VER(i915) <= 11) 382 intel_uncore_write(uncore, 383 GEN8_L3_LRA_1_GPGPU, 384 GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL); 385 386 /* 387 * To support 64K PTEs we need to first enable the use of the 388 * Intermediate-Page-Size(IPS) bit of the PDE field via some magical 389 * mmio, otherwise the page-walker will simply ignore the IPS bit. This 390 * shouldn't be needed after GEN10. 391 * 392 * 64K pages were first introduced from BDW+, although technically they 393 * only *work* from gen9+. For pre-BDW we instead have the option for 394 * 32K pages, but we don't currently have any support for it in our 395 * driver. 396 */ 397 if (HAS_PAGE_SIZES(i915, I915_GTT_PAGE_SIZE_64K) && 398 GRAPHICS_VER(i915) <= 10) 399 intel_uncore_rmw(uncore, 400 GEN8_GAMW_ECO_DEV_RW_IA, 401 0, 402 GAMW_ECO_ENABLE_64K_IPS_FIELD); 403 404 if (IS_GRAPHICS_VER(i915, 8, 11)) { 405 bool can_use_gtt_cache = true; 406 407 /* 408 * According to the BSpec if we use 2M/1G pages then we also 409 * need to disable the GTT cache. At least on BDW we can see 410 * visual corruption when using 2M pages, and not disabling the 411 * GTT cache. 412 */ 413 if (HAS_PAGE_SIZES(i915, I915_GTT_PAGE_SIZE_2M)) 414 can_use_gtt_cache = false; 415 416 /* WaGttCachingOffByDefault */ 417 intel_uncore_write(uncore, 418 HSW_GTT_CACHE_EN, 419 can_use_gtt_cache ? GTT_CACHE_EN_ALL : 0); 420 drm_WARN_ON_ONCE(&i915->drm, can_use_gtt_cache && 421 intel_uncore_read(uncore, 422 HSW_GTT_CACHE_EN) == 0); 423 } 424 } 425 426 static void tgl_setup_private_ppat(struct intel_uncore *uncore) 427 { 428 /* TGL doesn't support LLC or AGE settings */ 429 intel_uncore_write(uncore, GEN12_PAT_INDEX(0), GEN8_PPAT_WB); 430 intel_uncore_write(uncore, GEN12_PAT_INDEX(1), GEN8_PPAT_WC); 431 intel_uncore_write(uncore, GEN12_PAT_INDEX(2), GEN8_PPAT_WT); 432 intel_uncore_write(uncore, GEN12_PAT_INDEX(3), GEN8_PPAT_UC); 433 intel_uncore_write(uncore, GEN12_PAT_INDEX(4), GEN8_PPAT_WB); 434 intel_uncore_write(uncore, GEN12_PAT_INDEX(5), GEN8_PPAT_WB); 435 intel_uncore_write(uncore, GEN12_PAT_INDEX(6), GEN8_PPAT_WB); 436 intel_uncore_write(uncore, GEN12_PAT_INDEX(7), GEN8_PPAT_WB); 437 } 438 439 static void icl_setup_private_ppat(struct intel_uncore *uncore) 440 { 441 intel_uncore_write(uncore, 442 GEN10_PAT_INDEX(0), 443 GEN8_PPAT_WB | GEN8_PPAT_LLC); 444 intel_uncore_write(uncore, 445 GEN10_PAT_INDEX(1), 446 GEN8_PPAT_WC | GEN8_PPAT_LLCELLC); 447 intel_uncore_write(uncore, 448 GEN10_PAT_INDEX(2), 449 GEN8_PPAT_WB | GEN8_PPAT_ELLC_OVERRIDE); 450 intel_uncore_write(uncore, 451 GEN10_PAT_INDEX(3), 452 GEN8_PPAT_UC); 453 intel_uncore_write(uncore, 454 GEN10_PAT_INDEX(4), 455 GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)); 456 intel_uncore_write(uncore, 457 GEN10_PAT_INDEX(5), 458 GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)); 459 intel_uncore_write(uncore, 460 GEN10_PAT_INDEX(6), 461 GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)); 462 intel_uncore_write(uncore, 463 GEN10_PAT_INDEX(7), 464 GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3)); 465 } 466 467 /* 468 * The GGTT and PPGTT need a private PPAT setup in order to handle cacheability 469 * bits. When using advanced contexts each context stores its own PAT, but 470 * writing this data shouldn't be harmful even in those cases. 471 */ 472 static void bdw_setup_private_ppat(struct intel_uncore *uncore) 473 { 474 struct drm_i915_private *i915 = uncore->i915; 475 u64 pat; 476 477 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */ 478 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */ 479 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */ 480 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) | 481 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) | 482 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) | 483 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3)); 484 485 /* for scanout with eLLC */ 486 if (GRAPHICS_VER(i915) >= 9) 487 pat |= GEN8_PPAT(2, GEN8_PPAT_WB | GEN8_PPAT_ELLC_OVERRIDE); 488 else 489 pat |= GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC); 490 491 intel_uncore_write(uncore, GEN8_PRIVATE_PAT_LO, lower_32_bits(pat)); 492 intel_uncore_write(uncore, GEN8_PRIVATE_PAT_HI, upper_32_bits(pat)); 493 } 494 495 static void chv_setup_private_ppat(struct intel_uncore *uncore) 496 { 497 u64 pat; 498 499 /* 500 * Map WB on BDW to snooped on CHV. 501 * 502 * Only the snoop bit has meaning for CHV, the rest is 503 * ignored. 504 * 505 * The hardware will never snoop for certain types of accesses: 506 * - CPU GTT (GMADR->GGTT->no snoop->memory) 507 * - PPGTT page tables 508 * - some other special cycles 509 * 510 * As with BDW, we also need to consider the following for GT accesses: 511 * "For GGTT, there is NO pat_sel[2:0] from the entry, 512 * so RTL will always use the value corresponding to 513 * pat_sel = 000". 514 * Which means we must set the snoop bit in PAT entry 0 515 * in order to keep the global status page working. 516 */ 517 518 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) | 519 GEN8_PPAT(1, 0) | 520 GEN8_PPAT(2, 0) | 521 GEN8_PPAT(3, 0) | 522 GEN8_PPAT(4, CHV_PPAT_SNOOP) | 523 GEN8_PPAT(5, CHV_PPAT_SNOOP) | 524 GEN8_PPAT(6, CHV_PPAT_SNOOP) | 525 GEN8_PPAT(7, CHV_PPAT_SNOOP); 526 527 intel_uncore_write(uncore, GEN8_PRIVATE_PAT_LO, lower_32_bits(pat)); 528 intel_uncore_write(uncore, GEN8_PRIVATE_PAT_HI, upper_32_bits(pat)); 529 } 530 531 void setup_private_pat(struct intel_uncore *uncore) 532 { 533 struct drm_i915_private *i915 = uncore->i915; 534 535 GEM_BUG_ON(GRAPHICS_VER(i915) < 8); 536 537 if (GRAPHICS_VER(i915) >= 12) 538 tgl_setup_private_ppat(uncore); 539 else if (GRAPHICS_VER(i915) >= 11) 540 icl_setup_private_ppat(uncore); 541 else if (IS_CHERRYVIEW(i915) || IS_GEN9_LP(i915)) 542 chv_setup_private_ppat(uncore); 543 else 544 bdw_setup_private_ppat(uncore); 545 } 546 547 struct i915_vma * 548 __vm_create_scratch_for_read(struct i915_address_space *vm, unsigned long size) 549 { 550 struct drm_i915_gem_object *obj; 551 struct i915_vma *vma; 552 553 obj = i915_gem_object_create_internal(vm->i915, PAGE_ALIGN(size)); 554 if (IS_ERR(obj)) 555 return ERR_CAST(obj); 556 557 i915_gem_object_set_cache_coherency(obj, I915_CACHING_CACHED); 558 559 vma = i915_vma_instance(obj, vm, NULL); 560 if (IS_ERR(vma)) { 561 i915_gem_object_put(obj); 562 return vma; 563 } 564 565 return vma; 566 } 567 568 struct i915_vma * 569 __vm_create_scratch_for_read_pinned(struct i915_address_space *vm, unsigned long size) 570 { 571 struct i915_vma *vma; 572 int err; 573 574 vma = __vm_create_scratch_for_read(vm, size); 575 if (IS_ERR(vma)) 576 return vma; 577 578 err = i915_vma_pin(vma, 0, 0, 579 i915_vma_is_ggtt(vma) ? PIN_GLOBAL : PIN_USER); 580 if (err) { 581 i915_vma_put(vma); 582 return ERR_PTR(err); 583 } 584 585 return vma; 586 } 587 588 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) 589 #include "selftests/mock_gtt.c" 590 #endif 591