1 /* 2 * Copyright 2018 Advanced Micro Devices, Inc. 3 * All Rights Reserved. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a 6 * copy of this software and associated documentation files (the 7 * "Software"), to deal in the Software without restriction, including 8 * without limitation the rights to use, copy, modify, merge, publish, 9 * distribute, sub license, and/or sell copies of the Software, and to 10 * permit persons to whom the Software is furnished to do so, subject to 11 * the following conditions: 12 * 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 16 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 17 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 18 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 19 * USE OR OTHER DEALINGS IN THE SOFTWARE. 20 * 21 * The above copyright notice and this permission notice (including the 22 * next paragraph) shall be included in all copies or substantial portions 23 * of the Software. 24 * 25 */ 26 27 #include <linux/io-64-nonatomic-lo-hi.h> 28 #ifdef CONFIG_X86 29 #include <asm/hypervisor.h> 30 #endif 31 32 #include "amdgpu.h" 33 #include "amdgpu_gmc.h" 34 #include "amdgpu_ras.h" 35 #include "amdgpu_reset.h" 36 #include "amdgpu_xgmi.h" 37 38 #include <drm/drm_drv.h> 39 #include <drm/ttm/ttm_tt.h> 40 41 static const u64 four_gb = 0x100000000ULL; 42 43 bool amdgpu_gmc_is_pdb0_enabled(struct amdgpu_device *adev) 44 { 45 return adev->gmc.xgmi.connected_to_cpu || amdgpu_virt_xgmi_migrate_enabled(adev); 46 } 47 48 /** 49 * amdgpu_gmc_pdb0_alloc - allocate vram for pdb0 50 * 51 * @adev: amdgpu_device pointer 52 * 53 * Allocate video memory for pdb0 and map it for CPU access 54 * Returns 0 for success, error for failure. 55 */ 56 int amdgpu_gmc_pdb0_alloc(struct amdgpu_device *adev) 57 { 58 int r; 59 struct amdgpu_bo_param bp; 60 u64 vram_size = adev->gmc.xgmi.node_segment_size * adev->gmc.xgmi.num_physical_nodes; 61 uint32_t pde0_page_shift = adev->gmc.vmid0_page_table_block_size + 21; 62 uint32_t npdes = (vram_size + (1ULL << pde0_page_shift) - 1) >> pde0_page_shift; 63 64 memset(&bp, 0, sizeof(bp)); 65 bp.size = PAGE_ALIGN((npdes + 1) * 8); 66 bp.byte_align = PAGE_SIZE; 67 bp.domain = AMDGPU_GEM_DOMAIN_VRAM; 68 bp.flags = AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED | 69 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS; 70 bp.type = ttm_bo_type_kernel; 71 bp.resv = NULL; 72 bp.bo_ptr_size = sizeof(struct amdgpu_bo); 73 74 r = amdgpu_bo_create(adev, &bp, &adev->gmc.pdb0_bo); 75 if (r) 76 return r; 77 78 r = amdgpu_bo_reserve(adev->gmc.pdb0_bo, false); 79 if (unlikely(r != 0)) 80 goto bo_reserve_failure; 81 82 r = amdgpu_bo_pin(adev->gmc.pdb0_bo, AMDGPU_GEM_DOMAIN_VRAM); 83 if (r) 84 goto bo_pin_failure; 85 r = amdgpu_bo_kmap(adev->gmc.pdb0_bo, &adev->gmc.ptr_pdb0); 86 if (r) 87 goto bo_kmap_failure; 88 89 amdgpu_bo_unreserve(adev->gmc.pdb0_bo); 90 return 0; 91 92 bo_kmap_failure: 93 amdgpu_bo_unpin(adev->gmc.pdb0_bo); 94 bo_pin_failure: 95 amdgpu_bo_unreserve(adev->gmc.pdb0_bo); 96 bo_reserve_failure: 97 amdgpu_bo_unref(&adev->gmc.pdb0_bo); 98 return r; 99 } 100 101 /** 102 * amdgpu_gmc_get_pde_for_bo - get the PDE for a BO 103 * 104 * @bo: the BO to get the PDE for 105 * @level: the level in the PD hirarchy 106 * @addr: resulting addr 107 * @flags: resulting flags 108 * 109 * Get the address and flags to be used for a PDE (Page Directory Entry). 110 */ 111 void amdgpu_gmc_get_pde_for_bo(struct amdgpu_bo *bo, int level, 112 uint64_t *addr, uint64_t *flags) 113 { 114 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev); 115 116 switch (bo->tbo.resource->mem_type) { 117 case TTM_PL_TT: 118 *addr = bo->tbo.ttm->dma_address[0]; 119 break; 120 case TTM_PL_VRAM: 121 *addr = amdgpu_bo_gpu_offset(bo); 122 break; 123 default: 124 *addr = 0; 125 break; 126 } 127 *flags = amdgpu_ttm_tt_pde_flags(bo->tbo.ttm, bo->tbo.resource); 128 amdgpu_gmc_get_vm_pde(adev, level, addr, flags); 129 } 130 131 /* 132 * amdgpu_gmc_pd_addr - return the address of the root directory 133 */ 134 uint64_t amdgpu_gmc_pd_addr(struct amdgpu_bo *bo) 135 { 136 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev); 137 uint64_t pd_addr; 138 139 /* TODO: move that into ASIC specific code */ 140 if (adev->asic_type >= CHIP_VEGA10) { 141 uint64_t flags = AMDGPU_PTE_VALID; 142 143 amdgpu_gmc_get_pde_for_bo(bo, -1, &pd_addr, &flags); 144 pd_addr |= flags; 145 } else { 146 pd_addr = amdgpu_bo_gpu_offset(bo); 147 } 148 return pd_addr; 149 } 150 151 /** 152 * amdgpu_gmc_set_pte_pde - update the page tables using CPU 153 * 154 * @adev: amdgpu_device pointer 155 * @cpu_pt_addr: cpu address of the page table 156 * @gpu_page_idx: entry in the page table to update 157 * @addr: dst addr to write into pte/pde 158 * @flags: access flags 159 * 160 * Update the page tables using CPU. 161 */ 162 int amdgpu_gmc_set_pte_pde(struct amdgpu_device *adev, void *cpu_pt_addr, 163 uint32_t gpu_page_idx, uint64_t addr, 164 uint64_t flags) 165 { 166 void __iomem *ptr = (void *)cpu_pt_addr; 167 uint64_t value; 168 169 /* 170 * The following is for PTE only. GART does not have PDEs. 171 */ 172 value = addr & 0x0000FFFFFFFFF000ULL; 173 value |= flags; 174 writeq(value, ptr + (gpu_page_idx * 8)); 175 176 return 0; 177 } 178 179 /** 180 * amdgpu_gmc_agp_addr - return the address in the AGP address space 181 * 182 * @bo: TTM BO which needs the address, must be in GTT domain 183 * 184 * Tries to figure out how to access the BO through the AGP aperture. Returns 185 * AMDGPU_BO_INVALID_OFFSET if that is not possible. 186 */ 187 uint64_t amdgpu_gmc_agp_addr(struct ttm_buffer_object *bo) 188 { 189 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev); 190 191 if (!bo->ttm) 192 return AMDGPU_BO_INVALID_OFFSET; 193 194 if (bo->ttm->num_pages != 1 || bo->ttm->caching == ttm_cached) 195 return AMDGPU_BO_INVALID_OFFSET; 196 197 if (bo->ttm->dma_address[0] + PAGE_SIZE >= adev->gmc.agp_size) 198 return AMDGPU_BO_INVALID_OFFSET; 199 200 return adev->gmc.agp_start + bo->ttm->dma_address[0]; 201 } 202 203 /** 204 * amdgpu_gmc_vram_location - try to find VRAM location 205 * 206 * @adev: amdgpu device structure holding all necessary information 207 * @mc: memory controller structure holding memory information 208 * @base: base address at which to put VRAM 209 * 210 * Function will try to place VRAM at base address provided 211 * as parameter. 212 */ 213 void amdgpu_gmc_vram_location(struct amdgpu_device *adev, struct amdgpu_gmc *mc, 214 u64 base) 215 { 216 uint64_t vis_limit = (uint64_t)amdgpu_vis_vram_limit << 20; 217 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20; 218 219 mc->vram_start = base; 220 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1; 221 if (limit < mc->real_vram_size) 222 mc->real_vram_size = limit; 223 224 if (vis_limit && vis_limit < mc->visible_vram_size) 225 mc->visible_vram_size = vis_limit; 226 227 if (mc->real_vram_size < mc->visible_vram_size) 228 mc->visible_vram_size = mc->real_vram_size; 229 230 if (mc->xgmi.num_physical_nodes == 0) { 231 mc->fb_start = mc->vram_start; 232 mc->fb_end = mc->vram_end; 233 } 234 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n", 235 mc->mc_vram_size >> 20, mc->vram_start, 236 mc->vram_end, mc->real_vram_size >> 20); 237 } 238 239 /** amdgpu_gmc_sysvm_location - place vram and gart in sysvm aperture 240 * 241 * @adev: amdgpu device structure holding all necessary information 242 * @mc: memory controller structure holding memory information 243 * 244 * This function is only used if use GART for FB translation. In such 245 * case, we use sysvm aperture (vmid0 page tables) for both vram 246 * and gart (aka system memory) access. 247 * 248 * GPUVM (and our organization of vmid0 page tables) require sysvm 249 * aperture to be placed at a location aligned with 8 times of native 250 * page size. For example, if vm_context0_cntl.page_table_block_size 251 * is 12, then native page size is 8G (2M*2^12), sysvm should start 252 * with a 64G aligned address. For simplicity, we just put sysvm at 253 * address 0. So vram start at address 0 and gart is right after vram. 254 */ 255 void amdgpu_gmc_sysvm_location(struct amdgpu_device *adev, struct amdgpu_gmc *mc) 256 { 257 u64 hive_vram_start = 0; 258 u64 hive_vram_end = mc->xgmi.node_segment_size * mc->xgmi.num_physical_nodes - 1; 259 mc->vram_start = mc->xgmi.node_segment_size * mc->xgmi.physical_node_id; 260 mc->vram_end = mc->vram_start + mc->xgmi.node_segment_size - 1; 261 /* node_segment_size may not 4GB aligned on SRIOV, align up is needed. */ 262 mc->gart_start = ALIGN(hive_vram_end + 1, four_gb); 263 mc->gart_end = mc->gart_start + mc->gart_size - 1; 264 if (amdgpu_virt_xgmi_migrate_enabled(adev)) { 265 /* set mc->vram_start to 0 to switch the returned GPU address of 266 * amdgpu_bo_create_reserved() from FB aperture to GART aperture. 267 */ 268 mc->vram_start = 0; 269 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1; 270 mc->visible_vram_size = min(mc->visible_vram_size, mc->real_vram_size); 271 } else { 272 mc->fb_start = hive_vram_start; 273 mc->fb_end = hive_vram_end; 274 } 275 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n", 276 mc->mc_vram_size >> 20, mc->vram_start, 277 mc->vram_end, mc->real_vram_size >> 20); 278 dev_info(adev->dev, "GART: %lluM 0x%016llX - 0x%016llX\n", 279 mc->gart_size >> 20, mc->gart_start, mc->gart_end); 280 } 281 282 /** 283 * amdgpu_gmc_gart_location - try to find GART location 284 * 285 * @adev: amdgpu device structure holding all necessary information 286 * @mc: memory controller structure holding memory information 287 * @gart_placement: GART placement policy with respect to VRAM 288 * 289 * Function will try to place GART before or after VRAM. 290 * If GART size is bigger than space left then we ajust GART size. 291 * Thus function will never fails. 292 */ 293 void amdgpu_gmc_gart_location(struct amdgpu_device *adev, struct amdgpu_gmc *mc, 294 enum amdgpu_gart_placement gart_placement) 295 { 296 u64 size_af, size_bf; 297 /*To avoid the hole, limit the max mc address to AMDGPU_GMC_HOLE_START*/ 298 u64 max_mc_address = min(adev->gmc.mc_mask, AMDGPU_GMC_HOLE_START - 1); 299 300 /* VCE doesn't like it when BOs cross a 4GB segment, so align 301 * the GART base on a 4GB boundary as well. 302 */ 303 size_bf = mc->fb_start; 304 size_af = max_mc_address + 1 - ALIGN(mc->fb_end + 1, four_gb); 305 306 if (mc->gart_size > max(size_bf, size_af)) { 307 dev_warn(adev->dev, "limiting GART\n"); 308 mc->gart_size = max(size_bf, size_af); 309 } 310 311 switch (gart_placement) { 312 case AMDGPU_GART_PLACEMENT_HIGH: 313 mc->gart_start = max_mc_address - mc->gart_size + 1; 314 break; 315 case AMDGPU_GART_PLACEMENT_LOW: 316 mc->gart_start = 0; 317 break; 318 case AMDGPU_GART_PLACEMENT_BEST_FIT: 319 default: 320 if ((size_bf >= mc->gart_size && size_bf < size_af) || 321 (size_af < mc->gart_size)) 322 mc->gart_start = 0; 323 else 324 mc->gart_start = max_mc_address - mc->gart_size + 1; 325 break; 326 } 327 328 mc->gart_start &= ~(four_gb - 1); 329 mc->gart_end = mc->gart_start + mc->gart_size - 1; 330 dev_info(adev->dev, "GART: %lluM 0x%016llX - 0x%016llX\n", 331 mc->gart_size >> 20, mc->gart_start, mc->gart_end); 332 } 333 334 /** 335 * amdgpu_gmc_agp_location - try to find AGP location 336 * @adev: amdgpu device structure holding all necessary information 337 * @mc: memory controller structure holding memory information 338 * 339 * Function will place try to find a place for the AGP BAR in the MC address 340 * space. 341 * 342 * AGP BAR will be assigned the largest available hole in the address space. 343 * Should be called after VRAM and GART locations are setup. 344 */ 345 void amdgpu_gmc_agp_location(struct amdgpu_device *adev, struct amdgpu_gmc *mc) 346 { 347 const uint64_t sixteen_gb = 1ULL << 34; 348 const uint64_t sixteen_gb_mask = ~(sixteen_gb - 1); 349 u64 size_af, size_bf; 350 351 if (mc->fb_start > mc->gart_start) { 352 size_bf = (mc->fb_start & sixteen_gb_mask) - 353 ALIGN(mc->gart_end + 1, sixteen_gb); 354 size_af = mc->mc_mask + 1 - ALIGN(mc->fb_end + 1, sixteen_gb); 355 } else { 356 size_bf = mc->fb_start & sixteen_gb_mask; 357 size_af = (mc->gart_start & sixteen_gb_mask) - 358 ALIGN(mc->fb_end + 1, sixteen_gb); 359 } 360 361 if (size_bf > size_af) { 362 mc->agp_start = (mc->fb_start - size_bf) & sixteen_gb_mask; 363 mc->agp_size = size_bf; 364 } else { 365 mc->agp_start = ALIGN(mc->fb_end + 1, sixteen_gb); 366 mc->agp_size = size_af; 367 } 368 369 mc->agp_end = mc->agp_start + mc->agp_size - 1; 370 dev_info(adev->dev, "AGP: %lluM 0x%016llX - 0x%016llX\n", 371 mc->agp_size >> 20, mc->agp_start, mc->agp_end); 372 } 373 374 /** 375 * amdgpu_gmc_set_agp_default - Set the default AGP aperture value. 376 * @adev: amdgpu device structure holding all necessary information 377 * @mc: memory controller structure holding memory information 378 * 379 * To disable the AGP aperture, you need to set the start to a larger 380 * value than the end. This function sets the default value which 381 * can then be overridden using amdgpu_gmc_agp_location() if you want 382 * to enable the AGP aperture on a specific chip. 383 * 384 */ 385 void amdgpu_gmc_set_agp_default(struct amdgpu_device *adev, 386 struct amdgpu_gmc *mc) 387 { 388 mc->agp_start = 0xffffffffffff; 389 mc->agp_end = 0; 390 mc->agp_size = 0; 391 } 392 393 /** 394 * amdgpu_gmc_fault_key - get hask key from vm fault address and pasid 395 * 396 * @addr: 48 bit physical address, page aligned (36 significant bits) 397 * @pasid: 16 bit process address space identifier 398 */ 399 static inline uint64_t amdgpu_gmc_fault_key(uint64_t addr, uint16_t pasid) 400 { 401 return addr << 4 | pasid; 402 } 403 404 /** 405 * amdgpu_gmc_filter_faults - filter VM faults 406 * 407 * @adev: amdgpu device structure 408 * @ih: interrupt ring that the fault received from 409 * @addr: address of the VM fault 410 * @pasid: PASID of the process causing the fault 411 * @timestamp: timestamp of the fault 412 * 413 * Returns: 414 * True if the fault was filtered and should not be processed further. 415 * False if the fault is a new one and needs to be handled. 416 */ 417 bool amdgpu_gmc_filter_faults(struct amdgpu_device *adev, 418 struct amdgpu_ih_ring *ih, uint64_t addr, 419 uint16_t pasid, uint64_t timestamp) 420 { 421 struct amdgpu_gmc *gmc = &adev->gmc; 422 uint64_t stamp, key = amdgpu_gmc_fault_key(addr, pasid); 423 struct amdgpu_gmc_fault *fault; 424 uint32_t hash; 425 426 /* Stale retry fault if timestamp goes backward */ 427 if (amdgpu_ih_ts_after(timestamp, ih->processed_timestamp)) 428 return true; 429 430 /* If we don't have space left in the ring buffer return immediately */ 431 stamp = max(timestamp, AMDGPU_GMC_FAULT_TIMEOUT + 1) - 432 AMDGPU_GMC_FAULT_TIMEOUT; 433 if (gmc->fault_ring[gmc->last_fault].timestamp >= stamp) 434 return true; 435 436 /* Try to find the fault in the hash */ 437 hash = hash_64(key, AMDGPU_GMC_FAULT_HASH_ORDER); 438 fault = &gmc->fault_ring[gmc->fault_hash[hash].idx]; 439 while (fault->timestamp >= stamp) { 440 uint64_t tmp; 441 442 if (atomic64_read(&fault->key) == key) { 443 /* 444 * if we get a fault which is already present in 445 * the fault_ring and the timestamp of 446 * the fault is after the expired timestamp, 447 * then this is a new fault that needs to be added 448 * into the fault ring. 449 */ 450 if (fault->timestamp_expiry != 0 && 451 amdgpu_ih_ts_after(fault->timestamp_expiry, 452 timestamp)) 453 break; 454 else 455 return true; 456 } 457 458 tmp = fault->timestamp; 459 fault = &gmc->fault_ring[fault->next]; 460 461 /* Check if the entry was reused */ 462 if (fault->timestamp >= tmp) 463 break; 464 } 465 466 /* Add the fault to the ring */ 467 fault = &gmc->fault_ring[gmc->last_fault]; 468 atomic64_set(&fault->key, key); 469 fault->timestamp = timestamp; 470 471 /* And update the hash */ 472 fault->next = gmc->fault_hash[hash].idx; 473 gmc->fault_hash[hash].idx = gmc->last_fault++; 474 return false; 475 } 476 477 /** 478 * amdgpu_gmc_filter_faults_remove - remove address from VM faults filter 479 * 480 * @adev: amdgpu device structure 481 * @addr: address of the VM fault 482 * @pasid: PASID of the process causing the fault 483 * 484 * Remove the address from fault filter, then future vm fault on this address 485 * will pass to retry fault handler to recover. 486 */ 487 void amdgpu_gmc_filter_faults_remove(struct amdgpu_device *adev, uint64_t addr, 488 uint16_t pasid) 489 { 490 struct amdgpu_gmc *gmc = &adev->gmc; 491 uint64_t key = amdgpu_gmc_fault_key(addr, pasid); 492 struct amdgpu_ih_ring *ih; 493 struct amdgpu_gmc_fault *fault; 494 uint32_t last_wptr; 495 uint64_t last_ts; 496 uint32_t hash; 497 uint64_t tmp; 498 499 if (adev->irq.retry_cam_enabled) 500 return; 501 502 ih = &adev->irq.ih1; 503 /* Get the WPTR of the last entry in IH ring */ 504 last_wptr = amdgpu_ih_get_wptr(adev, ih); 505 /* Order wptr with ring data. */ 506 rmb(); 507 /* Get the timetamp of the last entry in IH ring */ 508 last_ts = amdgpu_ih_decode_iv_ts(adev, ih, last_wptr, -1); 509 510 hash = hash_64(key, AMDGPU_GMC_FAULT_HASH_ORDER); 511 fault = &gmc->fault_ring[gmc->fault_hash[hash].idx]; 512 do { 513 if (atomic64_read(&fault->key) == key) { 514 /* 515 * Update the timestamp when this fault 516 * expired. 517 */ 518 fault->timestamp_expiry = last_ts; 519 break; 520 } 521 522 tmp = fault->timestamp; 523 fault = &gmc->fault_ring[fault->next]; 524 } while (fault->timestamp < tmp); 525 } 526 527 int amdgpu_gmc_ras_sw_init(struct amdgpu_device *adev) 528 { 529 int r; 530 531 /* umc ras block */ 532 r = amdgpu_umc_ras_sw_init(adev); 533 if (r) 534 return r; 535 536 /* mmhub ras block */ 537 r = amdgpu_mmhub_ras_sw_init(adev); 538 if (r) 539 return r; 540 541 /* hdp ras block */ 542 r = amdgpu_hdp_ras_sw_init(adev); 543 if (r) 544 return r; 545 546 /* mca.x ras block */ 547 r = amdgpu_mca_mp0_ras_sw_init(adev); 548 if (r) 549 return r; 550 551 r = amdgpu_mca_mp1_ras_sw_init(adev); 552 if (r) 553 return r; 554 555 r = amdgpu_mca_mpio_ras_sw_init(adev); 556 if (r) 557 return r; 558 559 /* xgmi ras block */ 560 r = amdgpu_xgmi_ras_sw_init(adev); 561 if (r) 562 return r; 563 564 return 0; 565 } 566 567 int amdgpu_gmc_ras_late_init(struct amdgpu_device *adev) 568 { 569 return 0; 570 } 571 572 void amdgpu_gmc_ras_fini(struct amdgpu_device *adev) 573 { 574 575 } 576 577 /* 578 * The latest engine allocation on gfx9/10 is: 579 * Engine 2, 3: firmware 580 * Engine 0, 1, 4~16: amdgpu ring, 581 * subject to change when ring number changes 582 * Engine 17: Gart flushes 583 */ 584 #define AMDGPU_VMHUB_INV_ENG_BITMAP 0x1FFF3 585 586 int amdgpu_gmc_allocate_vm_inv_eng(struct amdgpu_device *adev) 587 { 588 struct amdgpu_ring *ring; 589 unsigned vm_inv_engs[AMDGPU_MAX_VMHUBS] = {0}; 590 unsigned i; 591 unsigned vmhub, inv_eng; 592 struct amdgpu_ring *shared_ring; 593 594 /* init the vm inv eng for all vmhubs */ 595 for_each_set_bit(i, adev->vmhubs_mask, AMDGPU_MAX_VMHUBS) { 596 vm_inv_engs[i] = AMDGPU_VMHUB_INV_ENG_BITMAP; 597 /* reserve engine 5 for firmware */ 598 if (adev->enable_mes) 599 vm_inv_engs[i] &= ~(1 << 5); 600 /* reserve engine 6 for uni mes */ 601 if (adev->enable_uni_mes) 602 vm_inv_engs[i] &= ~(1 << 6); 603 /* reserve mmhub engine 3 for firmware */ 604 if (adev->enable_umsch_mm) 605 vm_inv_engs[i] &= ~(1 << 3); 606 } 607 608 for (i = 0; i < adev->num_rings; ++i) { 609 ring = adev->rings[i]; 610 vmhub = ring->vm_hub; 611 612 if (ring == &adev->mes.ring[0] || 613 ring == &adev->mes.ring[1] || 614 ring == &adev->umsch_mm.ring || 615 ring == &adev->cper.ring_buf) 616 continue; 617 618 /* Skip if the ring is a shared ring */ 619 if (amdgpu_sdma_is_shared_inv_eng(adev, ring)) 620 continue; 621 622 inv_eng = ffs(vm_inv_engs[vmhub]); 623 if (!inv_eng) { 624 dev_err(adev->dev, "no VM inv eng for ring %s\n", 625 ring->name); 626 return -EINVAL; 627 } 628 629 ring->vm_inv_eng = inv_eng - 1; 630 vm_inv_engs[vmhub] &= ~(1 << ring->vm_inv_eng); 631 632 dev_info(adev->dev, "ring %s uses VM inv eng %u on hub %u\n", 633 ring->name, ring->vm_inv_eng, ring->vm_hub); 634 /* SDMA has a special packet which allows it to use the same 635 * invalidation engine for all the rings in one instance. 636 * Therefore, we do not allocate a separate VM invalidation engine 637 * for SDMA page rings. Instead, they share the VM invalidation 638 * engine with the SDMA gfx ring. This change ensures efficient 639 * resource management and avoids the issue of insufficient VM 640 * invalidation engines. 641 */ 642 shared_ring = amdgpu_sdma_get_shared_ring(adev, ring); 643 if (shared_ring) { 644 shared_ring->vm_inv_eng = ring->vm_inv_eng; 645 dev_info(adev->dev, "ring %s shares VM invalidation engine %u with ring %s on hub %u\n", 646 ring->name, ring->vm_inv_eng, shared_ring->name, ring->vm_hub); 647 continue; 648 } 649 } 650 651 return 0; 652 } 653 654 void amdgpu_gmc_flush_gpu_tlb(struct amdgpu_device *adev, uint32_t vmid, 655 uint32_t vmhub, uint32_t flush_type) 656 { 657 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring; 658 struct amdgpu_vmhub *hub = &adev->vmhub[vmhub]; 659 struct dma_fence *fence; 660 struct amdgpu_job *job; 661 int r; 662 663 if (!hub->sdma_invalidation_workaround || vmid || 664 !adev->mman.buffer_funcs_enabled || !adev->ib_pool_ready || 665 !ring->sched.ready) { 666 /* 667 * A GPU reset should flush all TLBs anyway, so no need to do 668 * this while one is ongoing. 669 */ 670 if (!down_read_trylock(&adev->reset_domain->sem)) 671 return; 672 673 if (adev->gmc.flush_tlb_needs_extra_type_2) 674 adev->gmc.gmc_funcs->flush_gpu_tlb(adev, vmid, 675 vmhub, 2); 676 677 if (adev->gmc.flush_tlb_needs_extra_type_0 && flush_type == 2) 678 adev->gmc.gmc_funcs->flush_gpu_tlb(adev, vmid, 679 vmhub, 0); 680 681 adev->gmc.gmc_funcs->flush_gpu_tlb(adev, vmid, vmhub, 682 flush_type); 683 up_read(&adev->reset_domain->sem); 684 return; 685 } 686 687 /* The SDMA on Navi 1x has a bug which can theoretically result in memory 688 * corruption if an invalidation happens at the same time as an VA 689 * translation. Avoid this by doing the invalidation from the SDMA 690 * itself at least for GART. 691 */ 692 mutex_lock(&adev->mman.gtt_window_lock); 693 r = amdgpu_job_alloc_with_ib(ring->adev, &adev->mman.high_pr, 694 AMDGPU_FENCE_OWNER_UNDEFINED, 695 16 * 4, AMDGPU_IB_POOL_IMMEDIATE, 696 &job, AMDGPU_KERNEL_JOB_ID_FLUSH_GPU_TLB); 697 if (r) 698 goto error_alloc; 699 700 job->vm_pd_addr = amdgpu_gmc_pd_addr(adev->gart.bo); 701 job->vm_needs_flush = true; 702 job->ibs->ptr[job->ibs->length_dw++] = ring->funcs->nop; 703 amdgpu_ring_pad_ib(ring, &job->ibs[0]); 704 fence = amdgpu_job_submit(job); 705 mutex_unlock(&adev->mman.gtt_window_lock); 706 707 dma_fence_wait(fence, false); 708 dma_fence_put(fence); 709 710 return; 711 712 error_alloc: 713 mutex_unlock(&adev->mman.gtt_window_lock); 714 dev_err(adev->dev, "Error flushing GPU TLB using the SDMA (%d)!\n", r); 715 } 716 717 int amdgpu_gmc_flush_gpu_tlb_pasid(struct amdgpu_device *adev, uint16_t pasid, 718 uint32_t flush_type, bool all_hub, 719 uint32_t inst) 720 { 721 struct amdgpu_ring *ring = &adev->gfx.kiq[inst].ring; 722 struct amdgpu_kiq *kiq = &adev->gfx.kiq[inst]; 723 unsigned int ndw; 724 int r, cnt = 0; 725 uint32_t seq; 726 727 /* 728 * A GPU reset should flush all TLBs anyway, so no need to do 729 * this while one is ongoing. 730 */ 731 if (!down_read_trylock(&adev->reset_domain->sem)) 732 return 0; 733 734 if (!adev->gmc.flush_pasid_uses_kiq || !ring->sched.ready) { 735 736 if (!adev->gmc.gmc_funcs->flush_gpu_tlb_pasid) 737 return 0; 738 739 if (adev->gmc.flush_tlb_needs_extra_type_2) 740 adev->gmc.gmc_funcs->flush_gpu_tlb_pasid(adev, pasid, 741 2, all_hub, 742 inst); 743 744 if (adev->gmc.flush_tlb_needs_extra_type_0 && flush_type == 2) 745 adev->gmc.gmc_funcs->flush_gpu_tlb_pasid(adev, pasid, 746 0, all_hub, 747 inst); 748 749 adev->gmc.gmc_funcs->flush_gpu_tlb_pasid(adev, pasid, 750 flush_type, all_hub, 751 inst); 752 r = 0; 753 } else { 754 /* 2 dwords flush + 8 dwords fence */ 755 ndw = kiq->pmf->invalidate_tlbs_size + 8; 756 757 if (adev->gmc.flush_tlb_needs_extra_type_2) 758 ndw += kiq->pmf->invalidate_tlbs_size; 759 760 if (adev->gmc.flush_tlb_needs_extra_type_0) 761 ndw += kiq->pmf->invalidate_tlbs_size; 762 763 spin_lock(&adev->gfx.kiq[inst].ring_lock); 764 r = amdgpu_ring_alloc(ring, ndw); 765 if (r) { 766 spin_unlock(&adev->gfx.kiq[inst].ring_lock); 767 goto error_unlock_reset; 768 } 769 if (adev->gmc.flush_tlb_needs_extra_type_2) 770 kiq->pmf->kiq_invalidate_tlbs(ring, pasid, 2, all_hub); 771 772 if (flush_type == 2 && adev->gmc.flush_tlb_needs_extra_type_0) 773 kiq->pmf->kiq_invalidate_tlbs(ring, pasid, 0, all_hub); 774 775 kiq->pmf->kiq_invalidate_tlbs(ring, pasid, flush_type, all_hub); 776 r = amdgpu_fence_emit_polling(ring, &seq, MAX_KIQ_REG_WAIT); 777 if (r) { 778 amdgpu_ring_undo(ring); 779 spin_unlock(&adev->gfx.kiq[inst].ring_lock); 780 goto error_unlock_reset; 781 } 782 783 amdgpu_ring_commit(ring); 784 spin_unlock(&adev->gfx.kiq[inst].ring_lock); 785 786 r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT); 787 788 might_sleep(); 789 while (r < 1 && cnt++ < MAX_KIQ_REG_TRY && 790 !amdgpu_reset_pending(adev->reset_domain)) { 791 msleep(MAX_KIQ_REG_BAILOUT_INTERVAL); 792 r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT); 793 } 794 795 if (cnt > MAX_KIQ_REG_TRY) { 796 dev_err(adev->dev, "timeout waiting for kiq fence\n"); 797 r = -ETIME; 798 } else 799 r = 0; 800 } 801 802 error_unlock_reset: 803 up_read(&adev->reset_domain->sem); 804 return r; 805 } 806 807 void amdgpu_gmc_fw_reg_write_reg_wait(struct amdgpu_device *adev, 808 uint32_t reg0, uint32_t reg1, 809 uint32_t ref, uint32_t mask, 810 uint32_t xcc_inst) 811 { 812 struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_inst]; 813 struct amdgpu_ring *ring = &kiq->ring; 814 signed long r, cnt = 0; 815 unsigned long flags; 816 uint32_t seq; 817 818 if (adev->mes.ring[0].sched.ready) { 819 amdgpu_mes_reg_write_reg_wait(adev, reg0, reg1, 820 ref, mask); 821 return; 822 } 823 824 spin_lock_irqsave(&kiq->ring_lock, flags); 825 amdgpu_ring_alloc(ring, 32); 826 amdgpu_ring_emit_reg_write_reg_wait(ring, reg0, reg1, 827 ref, mask); 828 r = amdgpu_fence_emit_polling(ring, &seq, MAX_KIQ_REG_WAIT); 829 if (r) 830 goto failed_undo; 831 832 amdgpu_ring_commit(ring); 833 spin_unlock_irqrestore(&kiq->ring_lock, flags); 834 835 r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT); 836 837 /* don't wait anymore for IRQ context */ 838 if (r < 1 && in_interrupt()) 839 goto failed_kiq; 840 841 might_sleep(); 842 while (r < 1 && cnt++ < MAX_KIQ_REG_TRY && 843 !amdgpu_reset_pending(adev->reset_domain)) { 844 845 msleep(MAX_KIQ_REG_BAILOUT_INTERVAL); 846 r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT); 847 } 848 849 if (cnt > MAX_KIQ_REG_TRY) 850 goto failed_kiq; 851 852 return; 853 854 failed_undo: 855 amdgpu_ring_undo(ring); 856 spin_unlock_irqrestore(&kiq->ring_lock, flags); 857 failed_kiq: 858 dev_err(adev->dev, "failed to write reg %x wait reg %x\n", reg0, reg1); 859 } 860 861 /** 862 * amdgpu_gmc_tmz_set -- check and set if a device supports TMZ 863 * @adev: amdgpu_device pointer 864 * 865 * Check and set if an the device @adev supports Trusted Memory 866 * Zones (TMZ). 867 */ 868 void amdgpu_gmc_tmz_set(struct amdgpu_device *adev) 869 { 870 switch (amdgpu_ip_version(adev, GC_HWIP, 0)) { 871 /* RAVEN */ 872 case IP_VERSION(9, 2, 2): 873 case IP_VERSION(9, 1, 0): 874 /* RENOIR looks like RAVEN */ 875 case IP_VERSION(9, 3, 0): 876 /* GC 10.3.7 */ 877 case IP_VERSION(10, 3, 7): 878 /* GC 11.0.1 */ 879 case IP_VERSION(11, 0, 1): 880 if (amdgpu_tmz == 0) { 881 adev->gmc.tmz_enabled = false; 882 dev_info(adev->dev, 883 "Trusted Memory Zone (TMZ) feature disabled (cmd line)\n"); 884 } else { 885 adev->gmc.tmz_enabled = true; 886 dev_info(adev->dev, 887 "Trusted Memory Zone (TMZ) feature enabled\n"); 888 } 889 break; 890 case IP_VERSION(10, 1, 10): 891 case IP_VERSION(10, 1, 1): 892 case IP_VERSION(10, 1, 2): 893 case IP_VERSION(10, 1, 3): 894 case IP_VERSION(10, 3, 0): 895 case IP_VERSION(10, 3, 2): 896 case IP_VERSION(10, 3, 4): 897 case IP_VERSION(10, 3, 5): 898 case IP_VERSION(10, 3, 6): 899 /* VANGOGH */ 900 case IP_VERSION(10, 3, 1): 901 /* YELLOW_CARP*/ 902 case IP_VERSION(10, 3, 3): 903 case IP_VERSION(11, 0, 4): 904 case IP_VERSION(11, 5, 0): 905 case IP_VERSION(11, 5, 1): 906 case IP_VERSION(11, 5, 2): 907 case IP_VERSION(11, 5, 3): 908 /* Don't enable it by default yet. 909 */ 910 if (amdgpu_tmz < 1) { 911 adev->gmc.tmz_enabled = false; 912 dev_info(adev->dev, 913 "Trusted Memory Zone (TMZ) feature disabled as experimental (default)\n"); 914 } else { 915 adev->gmc.tmz_enabled = true; 916 dev_info(adev->dev, 917 "Trusted Memory Zone (TMZ) feature enabled as experimental (cmd line)\n"); 918 } 919 break; 920 default: 921 adev->gmc.tmz_enabled = false; 922 dev_info(adev->dev, 923 "Trusted Memory Zone (TMZ) feature not supported\n"); 924 break; 925 } 926 } 927 928 /** 929 * amdgpu_gmc_noretry_set -- set per asic noretry defaults 930 * @adev: amdgpu_device pointer 931 * 932 * Set a per asic default for the no-retry parameter. 933 * 934 */ 935 void amdgpu_gmc_noretry_set(struct amdgpu_device *adev) 936 { 937 struct amdgpu_gmc *gmc = &adev->gmc; 938 uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0); 939 bool noretry_default = (gc_ver == IP_VERSION(9, 0, 1) || 940 gc_ver == IP_VERSION(9, 4, 0) || 941 gc_ver == IP_VERSION(9, 4, 1) || 942 gc_ver == IP_VERSION(9, 4, 2) || 943 gc_ver == IP_VERSION(9, 4, 3) || 944 gc_ver == IP_VERSION(9, 4, 4) || 945 gc_ver == IP_VERSION(9, 5, 0) || 946 gc_ver >= IP_VERSION(10, 3, 0)); 947 948 if (!amdgpu_sriov_xnack_support(adev)) 949 gmc->noretry = 1; 950 else 951 gmc->noretry = (amdgpu_noretry == -1) ? noretry_default : amdgpu_noretry; 952 } 953 954 void amdgpu_gmc_set_vm_fault_masks(struct amdgpu_device *adev, int hub_type, 955 bool enable) 956 { 957 struct amdgpu_vmhub *hub; 958 u32 tmp, reg, i; 959 960 hub = &adev->vmhub[hub_type]; 961 for (i = 0; i < 16; i++) { 962 reg = hub->vm_context0_cntl + hub->ctx_distance * i; 963 964 tmp = (hub_type == AMDGPU_GFXHUB(0)) ? 965 RREG32_SOC15_IP(GC, reg) : 966 RREG32_SOC15_IP(MMHUB, reg); 967 968 if (enable) 969 tmp |= hub->vm_cntx_cntl_vm_fault; 970 else 971 tmp &= ~hub->vm_cntx_cntl_vm_fault; 972 973 (hub_type == AMDGPU_GFXHUB(0)) ? 974 WREG32_SOC15_IP(GC, reg, tmp) : 975 WREG32_SOC15_IP(MMHUB, reg, tmp); 976 } 977 } 978 979 void amdgpu_gmc_get_vbios_allocations(struct amdgpu_device *adev) 980 { 981 unsigned size; 982 983 /* 984 * Some ASICs need to reserve a region of video memory to avoid access 985 * from driver 986 */ 987 adev->mman.stolen_reserved_offset = 0; 988 adev->mman.stolen_reserved_size = 0; 989 990 /* 991 * TODO: 992 * Currently there is a bug where some memory client outside 993 * of the driver writes to first 8M of VRAM on S3 resume, 994 * this overrides GART which by default gets placed in first 8M and 995 * causes VM_FAULTS once GTT is accessed. 996 * Keep the stolen memory reservation until the while this is not solved. 997 */ 998 switch (adev->asic_type) { 999 case CHIP_VEGA10: 1000 adev->mman.keep_stolen_vga_memory = true; 1001 /* 1002 * VEGA10 SRIOV VF with MS_HYPERV host needs some firmware reserved area. 1003 */ 1004 #ifdef CONFIG_X86 1005 if (amdgpu_sriov_vf(adev) && hypervisor_is_type(X86_HYPER_MS_HYPERV)) { 1006 adev->mman.stolen_reserved_offset = 0x500000; 1007 adev->mman.stolen_reserved_size = 0x200000; 1008 } 1009 #endif 1010 break; 1011 case CHIP_RAVEN: 1012 case CHIP_RENOIR: 1013 adev->mman.keep_stolen_vga_memory = true; 1014 break; 1015 default: 1016 adev->mman.keep_stolen_vga_memory = false; 1017 break; 1018 } 1019 1020 if (amdgpu_sriov_vf(adev) || 1021 !amdgpu_device_has_display_hardware(adev)) { 1022 size = 0; 1023 } else { 1024 size = amdgpu_gmc_get_vbios_fb_size(adev); 1025 1026 if (adev->mman.keep_stolen_vga_memory) 1027 size = max(size, (unsigned)AMDGPU_VBIOS_VGA_ALLOCATION); 1028 } 1029 1030 /* set to 0 if the pre-OS buffer uses up most of vram */ 1031 if ((adev->gmc.real_vram_size - size) < (8 * 1024 * 1024)) 1032 size = 0; 1033 1034 if (size > AMDGPU_VBIOS_VGA_ALLOCATION) { 1035 adev->mman.stolen_vga_size = AMDGPU_VBIOS_VGA_ALLOCATION; 1036 adev->mman.stolen_extended_size = size - adev->mman.stolen_vga_size; 1037 } else { 1038 adev->mman.stolen_vga_size = size; 1039 adev->mman.stolen_extended_size = 0; 1040 } 1041 } 1042 1043 /** 1044 * amdgpu_gmc_init_pdb0 - initialize PDB0 1045 * 1046 * @adev: amdgpu_device pointer 1047 * 1048 * This function is only used when GART page table is used 1049 * for FB address translatioin. In such a case, we construct 1050 * a 2-level system VM page table: PDB0->PTB, to cover both 1051 * VRAM of the hive and system memory. 1052 * 1053 * PDB0 is static, initialized once on driver initialization. 1054 * The first n entries of PDB0 are used as PTE by setting 1055 * P bit to 1, pointing to VRAM. The n+1'th entry points 1056 * to a big PTB covering system memory. 1057 * 1058 */ 1059 void amdgpu_gmc_init_pdb0(struct amdgpu_device *adev) 1060 { 1061 int i; 1062 uint64_t flags = adev->gart.gart_pte_flags; //TODO it is UC. explore NC/RW? 1063 /* Each PDE0 (used as PTE) covers (2^vmid0_page_table_block_size)*2M 1064 */ 1065 u64 vram_size = adev->gmc.xgmi.node_segment_size * adev->gmc.xgmi.num_physical_nodes; 1066 u64 pde0_page_size = (1ULL<<adev->gmc.vmid0_page_table_block_size)<<21; 1067 u64 vram_addr, vram_end; 1068 u64 gart_ptb_gpu_pa = amdgpu_gmc_vram_pa(adev, adev->gart.bo); 1069 int idx; 1070 1071 if (!drm_dev_enter(adev_to_drm(adev), &idx)) 1072 return; 1073 1074 flags |= AMDGPU_PTE_VALID | AMDGPU_PTE_READABLE; 1075 flags |= AMDGPU_PTE_WRITEABLE; 1076 flags |= AMDGPU_PTE_SNOOPED; 1077 flags |= AMDGPU_PTE_FRAG((adev->gmc.vmid0_page_table_block_size + 9*1)); 1078 flags |= AMDGPU_PDE_PTE_FLAG(adev); 1079 1080 vram_addr = adev->vm_manager.vram_base_offset; 1081 if (!amdgpu_virt_xgmi_migrate_enabled(adev)) 1082 vram_addr -= adev->gmc.xgmi.physical_node_id * adev->gmc.xgmi.node_segment_size; 1083 vram_end = vram_addr + vram_size; 1084 1085 /* The first n PDE0 entries are used as PTE, 1086 * pointing to vram 1087 */ 1088 for (i = 0; vram_addr < vram_end; i++, vram_addr += pde0_page_size) 1089 amdgpu_gmc_set_pte_pde(adev, adev->gmc.ptr_pdb0, i, vram_addr, flags); 1090 1091 /* The n+1'th PDE0 entry points to a huge 1092 * PTB who has more than 512 entries each 1093 * pointing to a 4K system page 1094 */ 1095 flags = AMDGPU_PTE_VALID; 1096 flags |= AMDGPU_PTE_SNOOPED | AMDGPU_PDE_BFS_FLAG(adev, 0); 1097 /* Requires gart_ptb_gpu_pa to be 4K aligned */ 1098 amdgpu_gmc_set_pte_pde(adev, adev->gmc.ptr_pdb0, i, gart_ptb_gpu_pa, flags); 1099 drm_dev_exit(idx); 1100 } 1101 1102 /** 1103 * amdgpu_gmc_vram_mc2pa - calculate vram buffer's physical address from MC 1104 * address 1105 * 1106 * @adev: amdgpu_device pointer 1107 * @mc_addr: MC address of buffer 1108 */ 1109 uint64_t amdgpu_gmc_vram_mc2pa(struct amdgpu_device *adev, uint64_t mc_addr) 1110 { 1111 return mc_addr - adev->gmc.vram_start + adev->vm_manager.vram_base_offset; 1112 } 1113 1114 /** 1115 * amdgpu_gmc_vram_pa - calculate vram buffer object's physical address from 1116 * GPU's view 1117 * 1118 * @adev: amdgpu_device pointer 1119 * @bo: amdgpu buffer object 1120 */ 1121 uint64_t amdgpu_gmc_vram_pa(struct amdgpu_device *adev, struct amdgpu_bo *bo) 1122 { 1123 return amdgpu_gmc_vram_mc2pa(adev, amdgpu_bo_gpu_offset(bo)); 1124 } 1125 1126 int amdgpu_gmc_vram_checking(struct amdgpu_device *adev) 1127 { 1128 struct amdgpu_bo *vram_bo = NULL; 1129 uint64_t vram_gpu = 0; 1130 void *vram_ptr = NULL; 1131 1132 int ret, size = 0x100000; 1133 uint8_t cptr[10]; 1134 1135 ret = amdgpu_bo_create_kernel(adev, size, PAGE_SIZE, 1136 AMDGPU_GEM_DOMAIN_VRAM, 1137 &vram_bo, 1138 &vram_gpu, 1139 &vram_ptr); 1140 if (ret) 1141 return ret; 1142 1143 memset(vram_ptr, 0x86, size); 1144 memset(cptr, 0x86, 10); 1145 1146 /** 1147 * Check the start, the mid, and the end of the memory if the content of 1148 * each byte is the pattern "0x86". If yes, we suppose the vram bo is 1149 * workable. 1150 * 1151 * Note: If check the each byte of whole 1M bo, it will cost too many 1152 * seconds, so here, we just pick up three parts for emulation. 1153 */ 1154 ret = memcmp(vram_ptr, cptr, 10); 1155 if (ret) { 1156 ret = -EIO; 1157 goto release_buffer; 1158 } 1159 1160 ret = memcmp(vram_ptr + (size / 2), cptr, 10); 1161 if (ret) { 1162 ret = -EIO; 1163 goto release_buffer; 1164 } 1165 1166 ret = memcmp(vram_ptr + size - 10, cptr, 10); 1167 if (ret) { 1168 ret = -EIO; 1169 goto release_buffer; 1170 } 1171 1172 release_buffer: 1173 amdgpu_bo_free_kernel(&vram_bo, &vram_gpu, 1174 &vram_ptr); 1175 1176 return ret; 1177 } 1178 1179 static const char *nps_desc[] = { 1180 [AMDGPU_NPS1_PARTITION_MODE] = "NPS1", 1181 [AMDGPU_NPS2_PARTITION_MODE] = "NPS2", 1182 [AMDGPU_NPS3_PARTITION_MODE] = "NPS3", 1183 [AMDGPU_NPS4_PARTITION_MODE] = "NPS4", 1184 [AMDGPU_NPS6_PARTITION_MODE] = "NPS6", 1185 [AMDGPU_NPS8_PARTITION_MODE] = "NPS8", 1186 }; 1187 1188 static ssize_t available_memory_partition_show(struct device *dev, 1189 struct device_attribute *addr, 1190 char *buf) 1191 { 1192 struct drm_device *ddev = dev_get_drvdata(dev); 1193 struct amdgpu_device *adev = drm_to_adev(ddev); 1194 int size = 0, mode; 1195 char *sep = ""; 1196 1197 for_each_inst(mode, adev->gmc.supported_nps_modes) { 1198 size += sysfs_emit_at(buf, size, "%s%s", sep, nps_desc[mode]); 1199 sep = ", "; 1200 } 1201 size += sysfs_emit_at(buf, size, "\n"); 1202 1203 return size; 1204 } 1205 1206 static ssize_t current_memory_partition_store(struct device *dev, 1207 struct device_attribute *attr, 1208 const char *buf, size_t count) 1209 { 1210 struct drm_device *ddev = dev_get_drvdata(dev); 1211 struct amdgpu_device *adev = drm_to_adev(ddev); 1212 enum amdgpu_memory_partition mode; 1213 struct amdgpu_hive_info *hive; 1214 int i; 1215 1216 mode = UNKNOWN_MEMORY_PARTITION_MODE; 1217 for_each_inst(i, adev->gmc.supported_nps_modes) { 1218 if (!strncasecmp(nps_desc[i], buf, strlen(nps_desc[i]))) { 1219 mode = i; 1220 break; 1221 } 1222 } 1223 1224 if (mode == UNKNOWN_MEMORY_PARTITION_MODE) 1225 return -EINVAL; 1226 1227 if (mode == adev->gmc.gmc_funcs->query_mem_partition_mode(adev)) { 1228 dev_info( 1229 adev->dev, 1230 "requested NPS mode is same as current NPS mode, skipping\n"); 1231 return count; 1232 } 1233 1234 /* If device is part of hive, all devices in the hive should request the 1235 * same mode. Hence store the requested mode in hive. 1236 */ 1237 hive = amdgpu_get_xgmi_hive(adev); 1238 if (hive) { 1239 atomic_set(&hive->requested_nps_mode, mode); 1240 amdgpu_put_xgmi_hive(hive); 1241 } else { 1242 adev->gmc.requested_nps_mode = mode; 1243 } 1244 1245 dev_info( 1246 adev->dev, 1247 "NPS mode change requested, please remove and reload the driver\n"); 1248 1249 return count; 1250 } 1251 1252 static ssize_t current_memory_partition_show( 1253 struct device *dev, struct device_attribute *addr, char *buf) 1254 { 1255 struct drm_device *ddev = dev_get_drvdata(dev); 1256 struct amdgpu_device *adev = drm_to_adev(ddev); 1257 enum amdgpu_memory_partition mode; 1258 1259 /* Only minimal precaution taken to reject requests while in reset */ 1260 if (amdgpu_in_reset(adev)) 1261 return -EPERM; 1262 1263 mode = adev->gmc.gmc_funcs->query_mem_partition_mode(adev); 1264 if ((mode >= ARRAY_SIZE(nps_desc)) || 1265 (BIT(mode) & AMDGPU_ALL_NPS_MASK) != BIT(mode)) 1266 return sysfs_emit(buf, "UNKNOWN\n"); 1267 1268 return sysfs_emit(buf, "%s\n", nps_desc[mode]); 1269 } 1270 1271 static DEVICE_ATTR_RW(current_memory_partition); 1272 static DEVICE_ATTR_RO(available_memory_partition); 1273 1274 int amdgpu_gmc_sysfs_init(struct amdgpu_device *adev) 1275 { 1276 bool nps_switch_support; 1277 int r = 0; 1278 1279 if (!adev->gmc.gmc_funcs->query_mem_partition_mode) 1280 return 0; 1281 1282 nps_switch_support = (hweight32(adev->gmc.supported_nps_modes & 1283 AMDGPU_ALL_NPS_MASK) > 1); 1284 if (!nps_switch_support) 1285 dev_attr_current_memory_partition.attr.mode &= 1286 ~(S_IWUSR | S_IWGRP | S_IWOTH); 1287 else 1288 r = device_create_file(adev->dev, 1289 &dev_attr_available_memory_partition); 1290 1291 if (r) 1292 return r; 1293 1294 return device_create_file(adev->dev, 1295 &dev_attr_current_memory_partition); 1296 } 1297 1298 void amdgpu_gmc_sysfs_fini(struct amdgpu_device *adev) 1299 { 1300 if (!adev->gmc.gmc_funcs->query_mem_partition_mode) 1301 return; 1302 1303 device_remove_file(adev->dev, &dev_attr_current_memory_partition); 1304 device_remove_file(adev->dev, &dev_attr_available_memory_partition); 1305 } 1306 1307 int amdgpu_gmc_get_nps_memranges(struct amdgpu_device *adev, 1308 struct amdgpu_mem_partition_info *mem_ranges, 1309 uint8_t *exp_ranges) 1310 { 1311 struct amdgpu_gmc_memrange *ranges; 1312 int range_cnt, ret, i, j; 1313 uint32_t nps_type; 1314 bool refresh; 1315 1316 if (!mem_ranges || !exp_ranges) 1317 return -EINVAL; 1318 1319 refresh = (adev->init_lvl->level != AMDGPU_INIT_LEVEL_MINIMAL_XGMI) && 1320 (adev->gmc.reset_flags & AMDGPU_GMC_INIT_RESET_NPS); 1321 ret = amdgpu_discovery_get_nps_info(adev, &nps_type, &ranges, 1322 &range_cnt, refresh); 1323 1324 if (ret) 1325 return ret; 1326 1327 /* TODO: For now, expect ranges and partition count to be the same. 1328 * Adjust if there are holes expected in any NPS domain. 1329 */ 1330 if (*exp_ranges && (range_cnt != *exp_ranges)) { 1331 dev_warn( 1332 adev->dev, 1333 "NPS config mismatch - expected ranges: %d discovery - nps mode: %d, nps ranges: %d", 1334 *exp_ranges, nps_type, range_cnt); 1335 ret = -EINVAL; 1336 goto err; 1337 } 1338 1339 for (i = 0; i < range_cnt; ++i) { 1340 if (ranges[i].base_address >= ranges[i].limit_address) { 1341 dev_warn( 1342 adev->dev, 1343 "Invalid NPS range - nps mode: %d, range[%d]: base: %llx limit: %llx", 1344 nps_type, i, ranges[i].base_address, 1345 ranges[i].limit_address); 1346 ret = -EINVAL; 1347 goto err; 1348 } 1349 1350 /* Check for overlaps, not expecting any now */ 1351 for (j = i - 1; j >= 0; j--) { 1352 if (max(ranges[j].base_address, 1353 ranges[i].base_address) <= 1354 min(ranges[j].limit_address, 1355 ranges[i].limit_address)) { 1356 dev_warn( 1357 adev->dev, 1358 "overlapping ranges detected [ %llx - %llx ] | [%llx - %llx]", 1359 ranges[j].base_address, 1360 ranges[j].limit_address, 1361 ranges[i].base_address, 1362 ranges[i].limit_address); 1363 ret = -EINVAL; 1364 goto err; 1365 } 1366 } 1367 1368 mem_ranges[i].range.fpfn = 1369 (ranges[i].base_address - 1370 adev->vm_manager.vram_base_offset) >> 1371 AMDGPU_GPU_PAGE_SHIFT; 1372 mem_ranges[i].range.lpfn = 1373 (ranges[i].limit_address - 1374 adev->vm_manager.vram_base_offset) >> 1375 AMDGPU_GPU_PAGE_SHIFT; 1376 mem_ranges[i].size = 1377 ranges[i].limit_address - ranges[i].base_address + 1; 1378 } 1379 1380 if (!*exp_ranges) 1381 *exp_ranges = range_cnt; 1382 err: 1383 kfree(ranges); 1384 1385 return ret; 1386 } 1387 1388 int amdgpu_gmc_request_memory_partition(struct amdgpu_device *adev, 1389 int nps_mode) 1390 { 1391 /* Not supported on VF devices and APUs */ 1392 if (amdgpu_sriov_vf(adev) || (adev->flags & AMD_IS_APU)) 1393 return -EOPNOTSUPP; 1394 1395 if (!adev->psp.funcs) { 1396 dev_err(adev->dev, 1397 "PSP interface not available for nps mode change request"); 1398 return -EINVAL; 1399 } 1400 1401 return psp_memory_partition(&adev->psp, nps_mode); 1402 } 1403 1404 static inline bool amdgpu_gmc_need_nps_switch_req(struct amdgpu_device *adev, 1405 int req_nps_mode, 1406 int cur_nps_mode) 1407 { 1408 return (((BIT(req_nps_mode) & adev->gmc.supported_nps_modes) == 1409 BIT(req_nps_mode)) && 1410 req_nps_mode != cur_nps_mode); 1411 } 1412 1413 void amdgpu_gmc_prepare_nps_mode_change(struct amdgpu_device *adev) 1414 { 1415 int req_nps_mode, cur_nps_mode, r; 1416 struct amdgpu_hive_info *hive; 1417 1418 if (amdgpu_sriov_vf(adev) || !adev->gmc.supported_nps_modes || 1419 !adev->gmc.gmc_funcs->request_mem_partition_mode) 1420 return; 1421 1422 cur_nps_mode = adev->gmc.gmc_funcs->query_mem_partition_mode(adev); 1423 hive = amdgpu_get_xgmi_hive(adev); 1424 if (hive) { 1425 req_nps_mode = atomic_read(&hive->requested_nps_mode); 1426 if (!amdgpu_gmc_need_nps_switch_req(adev, req_nps_mode, 1427 cur_nps_mode)) { 1428 amdgpu_put_xgmi_hive(hive); 1429 return; 1430 } 1431 r = amdgpu_xgmi_request_nps_change(adev, hive, req_nps_mode); 1432 amdgpu_put_xgmi_hive(hive); 1433 goto out; 1434 } 1435 1436 req_nps_mode = adev->gmc.requested_nps_mode; 1437 if (!amdgpu_gmc_need_nps_switch_req(adev, req_nps_mode, cur_nps_mode)) 1438 return; 1439 1440 /* even if this fails, we should let driver unload w/o blocking */ 1441 r = adev->gmc.gmc_funcs->request_mem_partition_mode(adev, req_nps_mode); 1442 out: 1443 if (r) 1444 dev_err(adev->dev, "NPS mode change request failed\n"); 1445 else 1446 dev_info( 1447 adev->dev, 1448 "NPS mode change request done, reload driver to complete the change\n"); 1449 } 1450 1451 bool amdgpu_gmc_need_reset_on_init(struct amdgpu_device *adev) 1452 { 1453 if (adev->gmc.gmc_funcs->need_reset_on_init) 1454 return adev->gmc.gmc_funcs->need_reset_on_init(adev); 1455 1456 return false; 1457 } 1458 1459 enum amdgpu_memory_partition 1460 amdgpu_gmc_get_vf_memory_partition(struct amdgpu_device *adev) 1461 { 1462 switch (adev->gmc.num_mem_partitions) { 1463 case 0: 1464 return UNKNOWN_MEMORY_PARTITION_MODE; 1465 case 1: 1466 return AMDGPU_NPS1_PARTITION_MODE; 1467 case 2: 1468 return AMDGPU_NPS2_PARTITION_MODE; 1469 case 4: 1470 return AMDGPU_NPS4_PARTITION_MODE; 1471 case 8: 1472 return AMDGPU_NPS8_PARTITION_MODE; 1473 default: 1474 return AMDGPU_NPS1_PARTITION_MODE; 1475 } 1476 } 1477 1478 enum amdgpu_memory_partition 1479 amdgpu_gmc_get_memory_partition(struct amdgpu_device *adev, u32 *supp_modes) 1480 { 1481 enum amdgpu_memory_partition mode = UNKNOWN_MEMORY_PARTITION_MODE; 1482 1483 if (adev->nbio.funcs && 1484 adev->nbio.funcs->get_memory_partition_mode) 1485 mode = adev->nbio.funcs->get_memory_partition_mode(adev, 1486 supp_modes); 1487 else 1488 dev_warn(adev->dev, "memory partition mode query is not supported\n"); 1489 1490 return mode; 1491 } 1492 1493 enum amdgpu_memory_partition 1494 amdgpu_gmc_query_memory_partition(struct amdgpu_device *adev) 1495 { 1496 if (amdgpu_sriov_vf(adev)) 1497 return amdgpu_gmc_get_vf_memory_partition(adev); 1498 else 1499 return amdgpu_gmc_get_memory_partition(adev, NULL); 1500 } 1501 1502 static bool amdgpu_gmc_validate_partition_info(struct amdgpu_device *adev) 1503 { 1504 enum amdgpu_memory_partition mode; 1505 u32 supp_modes; 1506 bool valid; 1507 1508 mode = amdgpu_gmc_get_memory_partition(adev, &supp_modes); 1509 1510 /* Mode detected by hardware not present in supported modes */ 1511 if ((mode != UNKNOWN_MEMORY_PARTITION_MODE) && 1512 !(BIT(mode - 1) & supp_modes)) 1513 return false; 1514 1515 switch (mode) { 1516 case UNKNOWN_MEMORY_PARTITION_MODE: 1517 case AMDGPU_NPS1_PARTITION_MODE: 1518 valid = (adev->gmc.num_mem_partitions == 1); 1519 break; 1520 case AMDGPU_NPS2_PARTITION_MODE: 1521 valid = (adev->gmc.num_mem_partitions == 2); 1522 break; 1523 case AMDGPU_NPS4_PARTITION_MODE: 1524 valid = (adev->gmc.num_mem_partitions == 3 || 1525 adev->gmc.num_mem_partitions == 4); 1526 break; 1527 case AMDGPU_NPS8_PARTITION_MODE: 1528 valid = (adev->gmc.num_mem_partitions == 8); 1529 break; 1530 default: 1531 valid = false; 1532 } 1533 1534 return valid; 1535 } 1536 1537 static bool amdgpu_gmc_is_node_present(int *node_ids, int num_ids, int nid) 1538 { 1539 int i; 1540 1541 /* Check if node with id 'nid' is present in 'node_ids' array */ 1542 for (i = 0; i < num_ids; ++i) 1543 if (node_ids[i] == nid) 1544 return true; 1545 1546 return false; 1547 } 1548 1549 static void 1550 amdgpu_gmc_init_acpi_mem_ranges(struct amdgpu_device *adev, 1551 struct amdgpu_mem_partition_info *mem_ranges) 1552 { 1553 struct amdgpu_numa_info numa_info; 1554 int node_ids[AMDGPU_MAX_MEM_RANGES]; 1555 int num_ranges = 0, ret; 1556 int num_xcc, xcc_id; 1557 uint32_t xcc_mask; 1558 1559 num_xcc = NUM_XCC(adev->gfx.xcc_mask); 1560 xcc_mask = (1U << num_xcc) - 1; 1561 1562 for_each_inst(xcc_id, xcc_mask) { 1563 ret = amdgpu_acpi_get_mem_info(adev, xcc_id, &numa_info); 1564 if (ret) 1565 continue; 1566 1567 if (numa_info.nid == NUMA_NO_NODE) { 1568 mem_ranges[0].size = numa_info.size; 1569 mem_ranges[0].numa.node = numa_info.nid; 1570 num_ranges = 1; 1571 break; 1572 } 1573 1574 if (amdgpu_gmc_is_node_present(node_ids, num_ranges, 1575 numa_info.nid)) 1576 continue; 1577 1578 node_ids[num_ranges] = numa_info.nid; 1579 mem_ranges[num_ranges].numa.node = numa_info.nid; 1580 mem_ranges[num_ranges].size = numa_info.size; 1581 ++num_ranges; 1582 } 1583 1584 adev->gmc.num_mem_partitions = num_ranges; 1585 } 1586 1587 void amdgpu_gmc_init_sw_mem_ranges(struct amdgpu_device *adev, 1588 struct amdgpu_mem_partition_info *mem_ranges) 1589 { 1590 enum amdgpu_memory_partition mode; 1591 u32 start_addr = 0, size; 1592 int i, r, l; 1593 1594 mode = amdgpu_gmc_query_memory_partition(adev); 1595 1596 switch (mode) { 1597 case UNKNOWN_MEMORY_PARTITION_MODE: 1598 adev->gmc.num_mem_partitions = 0; 1599 break; 1600 case AMDGPU_NPS1_PARTITION_MODE: 1601 adev->gmc.num_mem_partitions = 1; 1602 break; 1603 case AMDGPU_NPS2_PARTITION_MODE: 1604 adev->gmc.num_mem_partitions = 2; 1605 break; 1606 case AMDGPU_NPS4_PARTITION_MODE: 1607 if (adev->flags & AMD_IS_APU) 1608 adev->gmc.num_mem_partitions = 3; 1609 else 1610 adev->gmc.num_mem_partitions = 4; 1611 break; 1612 case AMDGPU_NPS8_PARTITION_MODE: 1613 adev->gmc.num_mem_partitions = 8; 1614 break; 1615 default: 1616 adev->gmc.num_mem_partitions = 1; 1617 break; 1618 } 1619 1620 /* Use NPS range info, if populated */ 1621 r = amdgpu_gmc_get_nps_memranges(adev, mem_ranges, 1622 &adev->gmc.num_mem_partitions); 1623 if (!r) { 1624 l = 0; 1625 for (i = 1; i < adev->gmc.num_mem_partitions; ++i) { 1626 if (mem_ranges[i].range.lpfn > 1627 mem_ranges[i - 1].range.lpfn) 1628 l = i; 1629 } 1630 1631 } else { 1632 if (!adev->gmc.num_mem_partitions) { 1633 dev_warn(adev->dev, 1634 "Not able to detect NPS mode, fall back to NPS1\n"); 1635 adev->gmc.num_mem_partitions = 1; 1636 } 1637 /* Fallback to sw based calculation */ 1638 size = (adev->gmc.real_vram_size + SZ_16M) >> AMDGPU_GPU_PAGE_SHIFT; 1639 size /= adev->gmc.num_mem_partitions; 1640 1641 for (i = 0; i < adev->gmc.num_mem_partitions; ++i) { 1642 mem_ranges[i].range.fpfn = start_addr; 1643 mem_ranges[i].size = 1644 ((u64)size << AMDGPU_GPU_PAGE_SHIFT); 1645 mem_ranges[i].range.lpfn = start_addr + size - 1; 1646 start_addr += size; 1647 } 1648 1649 l = adev->gmc.num_mem_partitions - 1; 1650 } 1651 1652 /* Adjust the last one */ 1653 mem_ranges[l].range.lpfn = 1654 (adev->gmc.real_vram_size >> AMDGPU_GPU_PAGE_SHIFT) - 1; 1655 mem_ranges[l].size = 1656 adev->gmc.real_vram_size - 1657 ((u64)mem_ranges[l].range.fpfn << AMDGPU_GPU_PAGE_SHIFT); 1658 } 1659 1660 int amdgpu_gmc_init_mem_ranges(struct amdgpu_device *adev) 1661 { 1662 bool valid; 1663 1664 adev->gmc.mem_partitions = kcalloc(AMDGPU_MAX_MEM_RANGES, 1665 sizeof(struct amdgpu_mem_partition_info), 1666 GFP_KERNEL); 1667 if (!adev->gmc.mem_partitions) 1668 return -ENOMEM; 1669 1670 if (adev->gmc.is_app_apu) 1671 amdgpu_gmc_init_acpi_mem_ranges(adev, adev->gmc.mem_partitions); 1672 else 1673 amdgpu_gmc_init_sw_mem_ranges(adev, adev->gmc.mem_partitions); 1674 1675 if (amdgpu_sriov_vf(adev)) 1676 valid = true; 1677 else 1678 valid = amdgpu_gmc_validate_partition_info(adev); 1679 if (!valid) { 1680 /* TODO: handle invalid case */ 1681 dev_warn(adev->dev, 1682 "Mem ranges not matching with hardware config\n"); 1683 } 1684 1685 return 0; 1686 } 1687