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 else if (adev->irq.ih1.ring_size) 502 ih = &adev->irq.ih1; 503 else if (adev->irq.ih_soft.enabled) 504 ih = &adev->irq.ih_soft; 505 else 506 return; 507 508 /* Get the WPTR of the last entry in IH ring */ 509 last_wptr = amdgpu_ih_get_wptr(adev, ih); 510 /* Order wptr with ring data. */ 511 rmb(); 512 /* Get the timetamp of the last entry in IH ring */ 513 last_ts = amdgpu_ih_decode_iv_ts(adev, ih, last_wptr, -1); 514 515 hash = hash_64(key, AMDGPU_GMC_FAULT_HASH_ORDER); 516 fault = &gmc->fault_ring[gmc->fault_hash[hash].idx]; 517 do { 518 if (atomic64_read(&fault->key) == key) { 519 /* 520 * Update the timestamp when this fault 521 * expired. 522 */ 523 fault->timestamp_expiry = last_ts; 524 break; 525 } 526 527 tmp = fault->timestamp; 528 fault = &gmc->fault_ring[fault->next]; 529 } while (fault->timestamp < tmp); 530 } 531 532 int amdgpu_gmc_handle_retry_fault(struct amdgpu_device *adev, 533 struct amdgpu_iv_entry *entry, 534 u64 addr, 535 u32 cam_index, 536 u32 node_id, 537 bool write_fault) 538 { 539 int ret; 540 541 if (adev->irq.retry_cam_enabled) { 542 /* Delegate it to a different ring if the hardware hasn't 543 * already done it. 544 */ 545 if (entry->ih == &adev->irq.ih) { 546 amdgpu_irq_delegate(adev, entry, 8); 547 return 1; 548 } 549 550 ret = amdgpu_vm_handle_fault(adev, entry->pasid, entry->vmid, node_id, 551 addr, entry->timestamp, write_fault); 552 WDOORBELL32(adev->irq.retry_cam_doorbell_index, cam_index); 553 if (ret) 554 return 1; 555 } else { 556 /* Process it only if it's the first fault for this address */ 557 if (entry->ih != &adev->irq.ih_soft && 558 amdgpu_gmc_filter_faults(adev, entry->ih, addr, entry->pasid, 559 entry->timestamp)) 560 return 1; 561 562 /* Delegate it to a different ring if the hardware hasn't 563 * already done it. 564 */ 565 if (entry->ih == &adev->irq.ih) { 566 amdgpu_irq_delegate(adev, entry, 8); 567 return 1; 568 } 569 570 /* Try to handle the recoverable page faults by filling page 571 * tables 572 */ 573 if (amdgpu_vm_handle_fault(adev, entry->pasid, entry->vmid, node_id, 574 addr, entry->timestamp, write_fault)) 575 return 1; 576 } 577 return 0; 578 } 579 580 int amdgpu_gmc_ras_sw_init(struct amdgpu_device *adev) 581 { 582 int r; 583 584 /* umc ras block */ 585 r = amdgpu_umc_ras_sw_init(adev); 586 if (r) 587 return r; 588 589 /* mmhub ras block */ 590 r = amdgpu_mmhub_ras_sw_init(adev); 591 if (r) 592 return r; 593 594 /* hdp ras block */ 595 r = amdgpu_hdp_ras_sw_init(adev); 596 if (r) 597 return r; 598 599 /* mca.x ras block */ 600 r = amdgpu_mca_mp0_ras_sw_init(adev); 601 if (r) 602 return r; 603 604 r = amdgpu_mca_mp1_ras_sw_init(adev); 605 if (r) 606 return r; 607 608 r = amdgpu_mca_mpio_ras_sw_init(adev); 609 if (r) 610 return r; 611 612 /* xgmi ras block */ 613 r = amdgpu_xgmi_ras_sw_init(adev); 614 if (r) 615 return r; 616 617 return 0; 618 } 619 620 int amdgpu_gmc_ras_late_init(struct amdgpu_device *adev) 621 { 622 return 0; 623 } 624 625 void amdgpu_gmc_ras_fini(struct amdgpu_device *adev) 626 { 627 628 } 629 630 /* 631 * The latest engine allocation on gfx9/10 is: 632 * Engine 2, 3: firmware 633 * Engine 0, 1, 4~16: amdgpu ring, 634 * subject to change when ring number changes 635 * Engine 17: Gart flushes 636 */ 637 #define AMDGPU_VMHUB_INV_ENG_BITMAP 0x1FFF3 638 639 int amdgpu_gmc_allocate_vm_inv_eng(struct amdgpu_device *adev) 640 { 641 struct amdgpu_ring *ring; 642 unsigned vm_inv_engs[AMDGPU_MAX_VMHUBS] = {0}; 643 unsigned i; 644 unsigned vmhub, inv_eng; 645 struct amdgpu_ring *shared_ring; 646 647 /* init the vm inv eng for all vmhubs */ 648 for_each_set_bit(i, adev->vmhubs_mask, AMDGPU_MAX_VMHUBS) { 649 vm_inv_engs[i] = AMDGPU_VMHUB_INV_ENG_BITMAP; 650 /* reserve engine 5 for firmware */ 651 if (adev->enable_mes) 652 vm_inv_engs[i] &= ~(1 << 5); 653 /* reserve engine 6 for uni mes */ 654 if (adev->enable_uni_mes) 655 vm_inv_engs[i] &= ~(1 << 6); 656 /* reserve mmhub engine 3 for firmware */ 657 if (adev->enable_umsch_mm) 658 vm_inv_engs[i] &= ~(1 << 3); 659 } 660 661 for (i = 0; i < adev->num_rings; ++i) { 662 ring = adev->rings[i]; 663 vmhub = ring->vm_hub; 664 665 if (ring == &adev->mes.ring[0] || 666 ring == &adev->mes.ring[1] || 667 ring == &adev->umsch_mm.ring || 668 ring == &adev->cper.ring_buf) 669 continue; 670 671 /* Skip if the ring is a shared ring */ 672 if (amdgpu_sdma_is_shared_inv_eng(adev, ring)) 673 continue; 674 675 inv_eng = ffs(vm_inv_engs[vmhub]); 676 if (!inv_eng) { 677 dev_err(adev->dev, "no VM inv eng for ring %s\n", 678 ring->name); 679 return -EINVAL; 680 } 681 682 ring->vm_inv_eng = inv_eng - 1; 683 vm_inv_engs[vmhub] &= ~(1 << ring->vm_inv_eng); 684 685 dev_info(adev->dev, "ring %s uses VM inv eng %u on hub %u\n", 686 ring->name, ring->vm_inv_eng, ring->vm_hub); 687 /* SDMA has a special packet which allows it to use the same 688 * invalidation engine for all the rings in one instance. 689 * Therefore, we do not allocate a separate VM invalidation engine 690 * for SDMA page rings. Instead, they share the VM invalidation 691 * engine with the SDMA gfx ring. This change ensures efficient 692 * resource management and avoids the issue of insufficient VM 693 * invalidation engines. 694 */ 695 shared_ring = amdgpu_sdma_get_shared_ring(adev, ring); 696 if (shared_ring) { 697 shared_ring->vm_inv_eng = ring->vm_inv_eng; 698 dev_info(adev->dev, "ring %s shares VM invalidation engine %u with ring %s on hub %u\n", 699 ring->name, ring->vm_inv_eng, shared_ring->name, ring->vm_hub); 700 continue; 701 } 702 } 703 704 return 0; 705 } 706 707 void amdgpu_gmc_flush_gpu_tlb(struct amdgpu_device *adev, uint32_t vmid, 708 uint32_t vmhub, uint32_t flush_type) 709 { 710 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring; 711 struct amdgpu_vmhub *hub = &adev->vmhub[vmhub]; 712 struct dma_fence *fence; 713 struct amdgpu_job *job; 714 int r; 715 716 if (!hub->sdma_invalidation_workaround || vmid || 717 !adev->mman.buffer_funcs_enabled || !adev->ib_pool_ready || 718 !ring->sched.ready) { 719 /* 720 * A GPU reset should flush all TLBs anyway, so no need to do 721 * this while one is ongoing. 722 */ 723 if (!down_read_trylock(&adev->reset_domain->sem)) 724 return; 725 726 if (adev->gmc.flush_tlb_needs_extra_type_2) 727 adev->gmc.gmc_funcs->flush_gpu_tlb(adev, vmid, 728 vmhub, 2); 729 730 if (adev->gmc.flush_tlb_needs_extra_type_0 && flush_type == 2) 731 adev->gmc.gmc_funcs->flush_gpu_tlb(adev, vmid, 732 vmhub, 0); 733 734 adev->gmc.gmc_funcs->flush_gpu_tlb(adev, vmid, vmhub, 735 flush_type); 736 up_read(&adev->reset_domain->sem); 737 return; 738 } 739 740 /* The SDMA on Navi 1x has a bug which can theoretically result in memory 741 * corruption if an invalidation happens at the same time as an VA 742 * translation. Avoid this by doing the invalidation from the SDMA 743 * itself at least for GART. 744 */ 745 mutex_lock(&adev->mman.gtt_window_lock); 746 r = amdgpu_job_alloc_with_ib(ring->adev, &adev->mman.default_entity.base, 747 AMDGPU_FENCE_OWNER_UNDEFINED, 748 16 * 4, AMDGPU_IB_POOL_IMMEDIATE, 749 &job, AMDGPU_KERNEL_JOB_ID_FLUSH_GPU_TLB); 750 if (r) 751 goto error_alloc; 752 753 job->vm_pd_addr = amdgpu_gmc_pd_addr(adev->gart.bo); 754 job->vm_needs_flush = true; 755 job->ibs->ptr[job->ibs->length_dw++] = ring->funcs->nop; 756 amdgpu_ring_pad_ib(ring, &job->ibs[0]); 757 fence = amdgpu_job_submit(job); 758 mutex_unlock(&adev->mman.gtt_window_lock); 759 760 dma_fence_wait(fence, false); 761 dma_fence_put(fence); 762 763 return; 764 765 error_alloc: 766 mutex_unlock(&adev->mman.gtt_window_lock); 767 dev_err(adev->dev, "Error flushing GPU TLB using the SDMA (%d)!\n", r); 768 } 769 770 int amdgpu_gmc_flush_gpu_tlb_pasid(struct amdgpu_device *adev, uint16_t pasid, 771 uint32_t flush_type, bool all_hub, 772 uint32_t inst) 773 { 774 struct amdgpu_ring *ring = &adev->gfx.kiq[inst].ring; 775 struct amdgpu_kiq *kiq = &adev->gfx.kiq[inst]; 776 unsigned int ndw; 777 int r, cnt = 0; 778 uint32_t seq; 779 780 /* 781 * A GPU reset should flush all TLBs anyway, so no need to do 782 * this while one is ongoing. 783 */ 784 if (!down_read_trylock(&adev->reset_domain->sem)) 785 return 0; 786 787 if (!adev->gmc.flush_pasid_uses_kiq || !ring->sched.ready) { 788 789 if (!adev->gmc.gmc_funcs->flush_gpu_tlb_pasid) { 790 r = 0; 791 goto error_unlock_reset; 792 } 793 794 if (adev->gmc.flush_tlb_needs_extra_type_2) 795 adev->gmc.gmc_funcs->flush_gpu_tlb_pasid(adev, pasid, 796 2, all_hub, 797 inst); 798 799 if (adev->gmc.flush_tlb_needs_extra_type_0 && flush_type == 2) 800 adev->gmc.gmc_funcs->flush_gpu_tlb_pasid(adev, pasid, 801 0, all_hub, 802 inst); 803 804 adev->gmc.gmc_funcs->flush_gpu_tlb_pasid(adev, pasid, 805 flush_type, all_hub, 806 inst); 807 r = 0; 808 } else { 809 /* 2 dwords flush + 8 dwords fence */ 810 ndw = kiq->pmf->invalidate_tlbs_size + 8; 811 812 if (adev->gmc.flush_tlb_needs_extra_type_2) 813 ndw += kiq->pmf->invalidate_tlbs_size; 814 815 if (adev->gmc.flush_tlb_needs_extra_type_0) 816 ndw += kiq->pmf->invalidate_tlbs_size; 817 818 spin_lock(&adev->gfx.kiq[inst].ring_lock); 819 r = amdgpu_ring_alloc(ring, ndw); 820 if (r) { 821 spin_unlock(&adev->gfx.kiq[inst].ring_lock); 822 goto error_unlock_reset; 823 } 824 if (adev->gmc.flush_tlb_needs_extra_type_2) 825 kiq->pmf->kiq_invalidate_tlbs(ring, pasid, 2, all_hub); 826 827 if (flush_type == 2 && adev->gmc.flush_tlb_needs_extra_type_0) 828 kiq->pmf->kiq_invalidate_tlbs(ring, pasid, 0, all_hub); 829 830 kiq->pmf->kiq_invalidate_tlbs(ring, pasid, flush_type, all_hub); 831 r = amdgpu_fence_emit_polling(ring, &seq, MAX_KIQ_REG_WAIT); 832 if (r) { 833 amdgpu_ring_undo(ring); 834 spin_unlock(&adev->gfx.kiq[inst].ring_lock); 835 goto error_unlock_reset; 836 } 837 838 amdgpu_ring_commit(ring); 839 spin_unlock(&adev->gfx.kiq[inst].ring_lock); 840 841 r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT); 842 843 might_sleep(); 844 while (r < 1 && cnt++ < MAX_KIQ_REG_TRY && 845 !amdgpu_reset_pending(adev->reset_domain)) { 846 msleep(MAX_KIQ_REG_BAILOUT_INTERVAL); 847 r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT); 848 } 849 850 if (cnt > MAX_KIQ_REG_TRY) { 851 dev_err(adev->dev, "timeout waiting for kiq fence\n"); 852 r = -ETIME; 853 } else 854 r = 0; 855 } 856 857 error_unlock_reset: 858 up_read(&adev->reset_domain->sem); 859 return r; 860 } 861 862 void amdgpu_gmc_fw_reg_write_reg_wait(struct amdgpu_device *adev, 863 uint32_t reg0, uint32_t reg1, 864 uint32_t ref, uint32_t mask, 865 uint32_t xcc_inst) 866 { 867 struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_inst]; 868 struct amdgpu_ring *ring = &kiq->ring; 869 signed long r, cnt = 0; 870 unsigned long flags; 871 uint32_t seq; 872 873 if (adev->mes.ring[MES_PIPE_INST(xcc_inst, 0)].sched.ready) { 874 amdgpu_mes_reg_write_reg_wait(adev, reg0, reg1, 875 ref, mask, xcc_inst); 876 return; 877 } 878 879 spin_lock_irqsave(&kiq->ring_lock, flags); 880 amdgpu_ring_alloc(ring, 32); 881 amdgpu_ring_emit_reg_write_reg_wait(ring, reg0, reg1, 882 ref, mask); 883 r = amdgpu_fence_emit_polling(ring, &seq, MAX_KIQ_REG_WAIT); 884 if (r) 885 goto failed_undo; 886 887 amdgpu_ring_commit(ring); 888 spin_unlock_irqrestore(&kiq->ring_lock, flags); 889 890 r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT); 891 892 /* don't wait anymore for IRQ context */ 893 if (r < 1 && in_interrupt()) 894 goto failed_kiq; 895 896 might_sleep(); 897 while (r < 1 && cnt++ < MAX_KIQ_REG_TRY && 898 !amdgpu_reset_pending(adev->reset_domain)) { 899 900 msleep(MAX_KIQ_REG_BAILOUT_INTERVAL); 901 r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT); 902 } 903 904 if (cnt > MAX_KIQ_REG_TRY) 905 goto failed_kiq; 906 907 return; 908 909 failed_undo: 910 amdgpu_ring_undo(ring); 911 spin_unlock_irqrestore(&kiq->ring_lock, flags); 912 failed_kiq: 913 dev_err(adev->dev, "failed to write reg %x wait reg %x\n", reg0, reg1); 914 } 915 916 /** 917 * amdgpu_gmc_tmz_set -- check and set if a device supports TMZ 918 * @adev: amdgpu_device pointer 919 * 920 * Check and set if an the device @adev supports Trusted Memory 921 * Zones (TMZ). 922 */ 923 void amdgpu_gmc_tmz_set(struct amdgpu_device *adev) 924 { 925 switch (amdgpu_ip_version(adev, GC_HWIP, 0)) { 926 /* RAVEN */ 927 case IP_VERSION(9, 2, 2): 928 case IP_VERSION(9, 1, 0): 929 /* RENOIR looks like RAVEN */ 930 case IP_VERSION(9, 3, 0): 931 /* GC 10.3.7 */ 932 case IP_VERSION(10, 3, 7): 933 /* GC 11.0.1 */ 934 case IP_VERSION(11, 0, 1): 935 if (amdgpu_tmz == 0) { 936 adev->gmc.tmz_enabled = false; 937 dev_info(adev->dev, 938 "Trusted Memory Zone (TMZ) feature disabled (cmd line)\n"); 939 } else { 940 adev->gmc.tmz_enabled = true; 941 dev_info(adev->dev, 942 "Trusted Memory Zone (TMZ) feature enabled\n"); 943 } 944 break; 945 case IP_VERSION(10, 1, 10): 946 case IP_VERSION(10, 1, 1): 947 case IP_VERSION(10, 1, 2): 948 case IP_VERSION(10, 1, 3): 949 case IP_VERSION(10, 3, 0): 950 case IP_VERSION(10, 3, 2): 951 case IP_VERSION(10, 3, 4): 952 case IP_VERSION(10, 3, 5): 953 case IP_VERSION(10, 3, 6): 954 /* VANGOGH */ 955 case IP_VERSION(10, 3, 1): 956 /* YELLOW_CARP*/ 957 case IP_VERSION(10, 3, 3): 958 case IP_VERSION(11, 0, 4): 959 case IP_VERSION(11, 5, 0): 960 case IP_VERSION(11, 5, 1): 961 case IP_VERSION(11, 5, 2): 962 case IP_VERSION(11, 5, 3): 963 case IP_VERSION(11, 5, 4): 964 /* Don't enable it by default yet. 965 */ 966 if (amdgpu_tmz < 1) { 967 adev->gmc.tmz_enabled = false; 968 dev_info(adev->dev, 969 "Trusted Memory Zone (TMZ) feature disabled as experimental (default)\n"); 970 } else { 971 adev->gmc.tmz_enabled = true; 972 dev_info(adev->dev, 973 "Trusted Memory Zone (TMZ) feature enabled as experimental (cmd line)\n"); 974 } 975 break; 976 default: 977 adev->gmc.tmz_enabled = false; 978 dev_info(adev->dev, 979 "Trusted Memory Zone (TMZ) feature not supported\n"); 980 break; 981 } 982 } 983 984 /** 985 * amdgpu_gmc_noretry_set -- set per asic noretry defaults 986 * @adev: amdgpu_device pointer 987 * 988 * Set a per asic default for the no-retry parameter. 989 * 990 */ 991 void amdgpu_gmc_noretry_set(struct amdgpu_device *adev) 992 { 993 struct amdgpu_gmc *gmc = &adev->gmc; 994 uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0); 995 bool noretry_default = (gc_ver == IP_VERSION(9, 0, 1) || 996 gc_ver == IP_VERSION(9, 4, 0) || 997 gc_ver == IP_VERSION(9, 4, 1) || 998 gc_ver == IP_VERSION(9, 4, 2) || 999 gc_ver == IP_VERSION(9, 4, 3) || 1000 gc_ver == IP_VERSION(9, 4, 4) || 1001 gc_ver == IP_VERSION(9, 5, 0) || 1002 gc_ver >= IP_VERSION(10, 3, 0)); 1003 1004 if (!amdgpu_sriov_xnack_support(adev)) 1005 gmc->noretry = 1; 1006 else 1007 gmc->noretry = (amdgpu_noretry == -1) ? noretry_default : amdgpu_noretry; 1008 } 1009 1010 void amdgpu_gmc_set_vm_fault_masks(struct amdgpu_device *adev, int hub_type, 1011 bool enable) 1012 { 1013 struct amdgpu_vmhub *hub; 1014 u32 tmp, reg, i; 1015 1016 hub = &adev->vmhub[hub_type]; 1017 for (i = 0; i < 16; i++) { 1018 reg = hub->vm_context0_cntl + hub->ctx_distance * i; 1019 1020 tmp = (hub_type == AMDGPU_GFXHUB(0)) ? 1021 RREG32_SOC15_IP(GC, reg) : 1022 RREG32_SOC15_IP(MMHUB, reg); 1023 1024 if (enable) 1025 tmp |= hub->vm_cntx_cntl_vm_fault; 1026 else 1027 tmp &= ~hub->vm_cntx_cntl_vm_fault; 1028 1029 (hub_type == AMDGPU_GFXHUB(0)) ? 1030 WREG32_SOC15_IP(GC, reg, tmp) : 1031 WREG32_SOC15_IP(MMHUB, reg, tmp); 1032 } 1033 } 1034 1035 void amdgpu_gmc_get_vbios_allocations(struct amdgpu_device *adev) 1036 { 1037 unsigned size; 1038 1039 /* 1040 * Some ASICs need to reserve a region of video memory to avoid access 1041 * from driver 1042 */ 1043 adev->mman.stolen_reserved_offset = 0; 1044 adev->mman.stolen_reserved_size = 0; 1045 1046 /* 1047 * TODO: 1048 * Currently there is a bug where some memory client outside 1049 * of the driver writes to first 8M of VRAM on S3 resume, 1050 * this overrides GART which by default gets placed in first 8M and 1051 * causes VM_FAULTS once GTT is accessed. 1052 * Keep the stolen memory reservation until the while this is not solved. 1053 */ 1054 switch (adev->asic_type) { 1055 case CHIP_VEGA10: 1056 adev->mman.keep_stolen_vga_memory = true; 1057 /* 1058 * VEGA10 SRIOV VF with MS_HYPERV host needs some firmware reserved area. 1059 */ 1060 #ifdef CONFIG_X86 1061 if (amdgpu_sriov_vf(adev) && hypervisor_is_type(X86_HYPER_MS_HYPERV)) { 1062 adev->mman.stolen_reserved_offset = 0x500000; 1063 adev->mman.stolen_reserved_size = 0x200000; 1064 } 1065 #endif 1066 break; 1067 case CHIP_RAVEN: 1068 case CHIP_RENOIR: 1069 adev->mman.keep_stolen_vga_memory = true; 1070 break; 1071 default: 1072 adev->mman.keep_stolen_vga_memory = false; 1073 break; 1074 } 1075 1076 if (amdgpu_sriov_vf(adev) || 1077 !amdgpu_device_has_display_hardware(adev)) { 1078 size = 0; 1079 } else { 1080 size = amdgpu_gmc_get_vbios_fb_size(adev); 1081 1082 if (adev->mman.keep_stolen_vga_memory) 1083 size = max(size, (unsigned)AMDGPU_VBIOS_VGA_ALLOCATION); 1084 } 1085 1086 /* set to 0 if the pre-OS buffer uses up most of vram */ 1087 if ((adev->gmc.real_vram_size - size) < (8 * 1024 * 1024)) 1088 size = 0; 1089 1090 if (size > AMDGPU_VBIOS_VGA_ALLOCATION) { 1091 adev->mman.stolen_vga_size = AMDGPU_VBIOS_VGA_ALLOCATION; 1092 adev->mman.stolen_extended_size = size - adev->mman.stolen_vga_size; 1093 } else { 1094 adev->mman.stolen_vga_size = size; 1095 adev->mman.stolen_extended_size = 0; 1096 } 1097 } 1098 1099 /** 1100 * amdgpu_gmc_init_pdb0 - initialize PDB0 1101 * 1102 * @adev: amdgpu_device pointer 1103 * 1104 * This function is only used when GART page table is used 1105 * for FB address translatioin. In such a case, we construct 1106 * a 2-level system VM page table: PDB0->PTB, to cover both 1107 * VRAM of the hive and system memory. 1108 * 1109 * PDB0 is static, initialized once on driver initialization. 1110 * The first n entries of PDB0 are used as PTE by setting 1111 * P bit to 1, pointing to VRAM. The n+1'th entry points 1112 * to a big PTB covering system memory. 1113 * 1114 */ 1115 void amdgpu_gmc_init_pdb0(struct amdgpu_device *adev) 1116 { 1117 int i; 1118 uint64_t flags = adev->gart.gart_pte_flags; //TODO it is UC. explore NC/RW? 1119 /* Each PDE0 (used as PTE) covers (2^vmid0_page_table_block_size)*2M 1120 */ 1121 u64 vram_size = adev->gmc.xgmi.node_segment_size * adev->gmc.xgmi.num_physical_nodes; 1122 u64 pde0_page_size = (1ULL<<adev->gmc.vmid0_page_table_block_size)<<21; 1123 u64 vram_addr, vram_end; 1124 u64 gart_ptb_gpu_pa = amdgpu_gmc_vram_pa(adev, adev->gart.bo); 1125 int idx; 1126 1127 if (!drm_dev_enter(adev_to_drm(adev), &idx)) 1128 return; 1129 1130 flags |= AMDGPU_PTE_VALID | AMDGPU_PTE_READABLE; 1131 flags |= AMDGPU_PTE_WRITEABLE; 1132 flags |= AMDGPU_PTE_SNOOPED; 1133 flags |= AMDGPU_PTE_FRAG((adev->gmc.vmid0_page_table_block_size + 9*1)); 1134 flags |= AMDGPU_PDE_PTE_FLAG(adev); 1135 1136 vram_addr = adev->vm_manager.vram_base_offset; 1137 if (!amdgpu_virt_xgmi_migrate_enabled(adev)) 1138 vram_addr -= adev->gmc.xgmi.physical_node_id * adev->gmc.xgmi.node_segment_size; 1139 vram_end = vram_addr + vram_size; 1140 1141 /* The first n PDE0 entries are used as PTE, 1142 * pointing to vram 1143 */ 1144 for (i = 0; vram_addr < vram_end; i++, vram_addr += pde0_page_size) 1145 amdgpu_gmc_set_pte_pde(adev, adev->gmc.ptr_pdb0, i, vram_addr, flags); 1146 1147 /* The n+1'th PDE0 entry points to a huge 1148 * PTB who has more than 512 entries each 1149 * pointing to a 4K system page 1150 */ 1151 flags = AMDGPU_PTE_VALID; 1152 flags |= AMDGPU_PTE_SNOOPED | AMDGPU_PDE_BFS_FLAG(adev, 0); 1153 /* Requires gart_ptb_gpu_pa to be 4K aligned */ 1154 amdgpu_gmc_set_pte_pde(adev, adev->gmc.ptr_pdb0, i, gart_ptb_gpu_pa, flags); 1155 drm_dev_exit(idx); 1156 } 1157 1158 /** 1159 * amdgpu_gmc_vram_mc2pa - calculate vram buffer's physical address from MC 1160 * address 1161 * 1162 * @adev: amdgpu_device pointer 1163 * @mc_addr: MC address of buffer 1164 */ 1165 uint64_t amdgpu_gmc_vram_mc2pa(struct amdgpu_device *adev, uint64_t mc_addr) 1166 { 1167 return mc_addr - adev->gmc.vram_start + adev->vm_manager.vram_base_offset; 1168 } 1169 1170 /** 1171 * amdgpu_gmc_vram_pa - calculate vram buffer object's physical address from 1172 * GPU's view 1173 * 1174 * @adev: amdgpu_device pointer 1175 * @bo: amdgpu buffer object 1176 */ 1177 uint64_t amdgpu_gmc_vram_pa(struct amdgpu_device *adev, struct amdgpu_bo *bo) 1178 { 1179 return amdgpu_gmc_vram_mc2pa(adev, amdgpu_bo_gpu_offset(bo)); 1180 } 1181 1182 int amdgpu_gmc_vram_checking(struct amdgpu_device *adev) 1183 { 1184 struct amdgpu_bo *vram_bo = NULL; 1185 uint64_t vram_gpu = 0; 1186 void *vram_ptr = NULL; 1187 1188 int ret, size = 0x100000; 1189 uint8_t cptr[10]; 1190 1191 ret = amdgpu_bo_create_kernel(adev, size, PAGE_SIZE, 1192 AMDGPU_GEM_DOMAIN_VRAM, 1193 &vram_bo, 1194 &vram_gpu, 1195 &vram_ptr); 1196 if (ret) 1197 return ret; 1198 1199 memset(vram_ptr, 0x86, size); 1200 memset(cptr, 0x86, 10); 1201 1202 /** 1203 * Check the start, the mid, and the end of the memory if the content of 1204 * each byte is the pattern "0x86". If yes, we suppose the vram bo is 1205 * workable. 1206 * 1207 * Note: If check the each byte of whole 1M bo, it will cost too many 1208 * seconds, so here, we just pick up three parts for emulation. 1209 */ 1210 ret = memcmp(vram_ptr, cptr, 10); 1211 if (ret) { 1212 ret = -EIO; 1213 goto release_buffer; 1214 } 1215 1216 ret = memcmp(vram_ptr + (size / 2), cptr, 10); 1217 if (ret) { 1218 ret = -EIO; 1219 goto release_buffer; 1220 } 1221 1222 ret = memcmp(vram_ptr + size - 10, cptr, 10); 1223 if (ret) { 1224 ret = -EIO; 1225 goto release_buffer; 1226 } 1227 1228 release_buffer: 1229 amdgpu_bo_free_kernel(&vram_bo, &vram_gpu, 1230 &vram_ptr); 1231 1232 return ret; 1233 } 1234 1235 static const char *nps_desc[] = { 1236 [AMDGPU_NPS1_PARTITION_MODE] = "NPS1", 1237 [AMDGPU_NPS2_PARTITION_MODE] = "NPS2", 1238 [AMDGPU_NPS3_PARTITION_MODE] = "NPS3", 1239 [AMDGPU_NPS4_PARTITION_MODE] = "NPS4", 1240 [AMDGPU_NPS6_PARTITION_MODE] = "NPS6", 1241 [AMDGPU_NPS8_PARTITION_MODE] = "NPS8", 1242 }; 1243 1244 static ssize_t available_memory_partition_show(struct device *dev, 1245 struct device_attribute *addr, 1246 char *buf) 1247 { 1248 struct drm_device *ddev = dev_get_drvdata(dev); 1249 struct amdgpu_device *adev = drm_to_adev(ddev); 1250 int size = 0, mode; 1251 char *sep = ""; 1252 1253 for_each_inst(mode, adev->gmc.supported_nps_modes) { 1254 size += sysfs_emit_at(buf, size, "%s%s", sep, nps_desc[mode]); 1255 sep = ", "; 1256 } 1257 size += sysfs_emit_at(buf, size, "\n"); 1258 1259 return size; 1260 } 1261 1262 static ssize_t current_memory_partition_store(struct device *dev, 1263 struct device_attribute *attr, 1264 const char *buf, size_t count) 1265 { 1266 struct drm_device *ddev = dev_get_drvdata(dev); 1267 struct amdgpu_device *adev = drm_to_adev(ddev); 1268 enum amdgpu_memory_partition mode; 1269 struct amdgpu_hive_info *hive; 1270 int i; 1271 1272 mode = UNKNOWN_MEMORY_PARTITION_MODE; 1273 for_each_inst(i, adev->gmc.supported_nps_modes) { 1274 if (!strncasecmp(nps_desc[i], buf, strlen(nps_desc[i]))) { 1275 mode = i; 1276 break; 1277 } 1278 } 1279 1280 if (mode == UNKNOWN_MEMORY_PARTITION_MODE) 1281 return -EINVAL; 1282 1283 if (mode == adev->gmc.gmc_funcs->query_mem_partition_mode(adev)) { 1284 dev_info( 1285 adev->dev, 1286 "requested NPS mode is same as current NPS mode, skipping\n"); 1287 return count; 1288 } 1289 1290 /* If device is part of hive, all devices in the hive should request the 1291 * same mode. Hence store the requested mode in hive. 1292 */ 1293 hive = amdgpu_get_xgmi_hive(adev); 1294 if (hive) { 1295 atomic_set(&hive->requested_nps_mode, mode); 1296 amdgpu_put_xgmi_hive(hive); 1297 } else { 1298 adev->gmc.requested_nps_mode = mode; 1299 } 1300 1301 dev_info( 1302 adev->dev, 1303 "NPS mode change requested, please remove and reload the driver\n"); 1304 1305 return count; 1306 } 1307 1308 static ssize_t current_memory_partition_show( 1309 struct device *dev, struct device_attribute *addr, char *buf) 1310 { 1311 struct drm_device *ddev = dev_get_drvdata(dev); 1312 struct amdgpu_device *adev = drm_to_adev(ddev); 1313 enum amdgpu_memory_partition mode; 1314 1315 /* Only minimal precaution taken to reject requests while in reset */ 1316 if (amdgpu_in_reset(adev)) 1317 return -EPERM; 1318 1319 mode = adev->gmc.gmc_funcs->query_mem_partition_mode(adev); 1320 if ((mode >= ARRAY_SIZE(nps_desc)) || 1321 (BIT(mode) & AMDGPU_ALL_NPS_MASK) != BIT(mode)) 1322 return sysfs_emit(buf, "UNKNOWN\n"); 1323 1324 return sysfs_emit(buf, "%s\n", nps_desc[mode]); 1325 } 1326 1327 static DEVICE_ATTR_RW(current_memory_partition); 1328 static DEVICE_ATTR_RO(available_memory_partition); 1329 1330 int amdgpu_gmc_sysfs_init(struct amdgpu_device *adev) 1331 { 1332 bool nps_switch_support; 1333 int r = 0; 1334 1335 if (!adev->gmc.gmc_funcs->query_mem_partition_mode) 1336 return 0; 1337 1338 nps_switch_support = (hweight32(adev->gmc.supported_nps_modes & 1339 AMDGPU_ALL_NPS_MASK) > 1); 1340 if (!nps_switch_support) 1341 dev_attr_current_memory_partition.attr.mode &= 1342 ~(S_IWUSR | S_IWGRP | S_IWOTH); 1343 else 1344 r = device_create_file(adev->dev, 1345 &dev_attr_available_memory_partition); 1346 1347 if (r) 1348 return r; 1349 1350 return device_create_file(adev->dev, 1351 &dev_attr_current_memory_partition); 1352 } 1353 1354 void amdgpu_gmc_sysfs_fini(struct amdgpu_device *adev) 1355 { 1356 if (!adev->gmc.gmc_funcs->query_mem_partition_mode) 1357 return; 1358 1359 device_remove_file(adev->dev, &dev_attr_current_memory_partition); 1360 device_remove_file(adev->dev, &dev_attr_available_memory_partition); 1361 } 1362 1363 int amdgpu_gmc_get_nps_memranges(struct amdgpu_device *adev, 1364 struct amdgpu_mem_partition_info *mem_ranges, 1365 uint8_t *exp_ranges) 1366 { 1367 struct amdgpu_gmc_memrange *ranges; 1368 int range_cnt, ret, i, j; 1369 uint32_t nps_type; 1370 bool refresh; 1371 1372 if (!mem_ranges || !exp_ranges) 1373 return -EINVAL; 1374 1375 refresh = (adev->init_lvl->level != AMDGPU_INIT_LEVEL_MINIMAL_XGMI) && 1376 (adev->gmc.reset_flags & AMDGPU_GMC_INIT_RESET_NPS); 1377 ret = amdgpu_discovery_get_nps_info(adev, &nps_type, &ranges, 1378 &range_cnt, refresh); 1379 1380 if (ret) 1381 return ret; 1382 1383 /* TODO: For now, expect ranges and partition count to be the same. 1384 * Adjust if there are holes expected in any NPS domain. 1385 */ 1386 if (*exp_ranges && (range_cnt != *exp_ranges)) { 1387 dev_warn( 1388 adev->dev, 1389 "NPS config mismatch - expected ranges: %d discovery - nps mode: %d, nps ranges: %d", 1390 *exp_ranges, nps_type, range_cnt); 1391 ret = -EINVAL; 1392 goto err; 1393 } 1394 1395 for (i = 0; i < range_cnt; ++i) { 1396 if (ranges[i].base_address >= ranges[i].limit_address) { 1397 dev_warn( 1398 adev->dev, 1399 "Invalid NPS range - nps mode: %d, range[%d]: base: %llx limit: %llx", 1400 nps_type, i, ranges[i].base_address, 1401 ranges[i].limit_address); 1402 ret = -EINVAL; 1403 goto err; 1404 } 1405 1406 /* Check for overlaps, not expecting any now */ 1407 for (j = i - 1; j >= 0; j--) { 1408 if (max(ranges[j].base_address, 1409 ranges[i].base_address) <= 1410 min(ranges[j].limit_address, 1411 ranges[i].limit_address)) { 1412 dev_warn( 1413 adev->dev, 1414 "overlapping ranges detected [ %llx - %llx ] | [%llx - %llx]", 1415 ranges[j].base_address, 1416 ranges[j].limit_address, 1417 ranges[i].base_address, 1418 ranges[i].limit_address); 1419 ret = -EINVAL; 1420 goto err; 1421 } 1422 } 1423 1424 mem_ranges[i].range.fpfn = 1425 (ranges[i].base_address - 1426 adev->vm_manager.vram_base_offset) >> 1427 AMDGPU_GPU_PAGE_SHIFT; 1428 mem_ranges[i].range.lpfn = 1429 (ranges[i].limit_address - 1430 adev->vm_manager.vram_base_offset) >> 1431 AMDGPU_GPU_PAGE_SHIFT; 1432 mem_ranges[i].size = 1433 ranges[i].limit_address - ranges[i].base_address + 1; 1434 } 1435 1436 if (!*exp_ranges) 1437 *exp_ranges = range_cnt; 1438 err: 1439 kfree(ranges); 1440 1441 return ret; 1442 } 1443 1444 int amdgpu_gmc_request_memory_partition(struct amdgpu_device *adev, 1445 int nps_mode) 1446 { 1447 /* Not supported on VF devices and APUs */ 1448 if (amdgpu_sriov_vf(adev) || (adev->flags & AMD_IS_APU)) 1449 return -EOPNOTSUPP; 1450 1451 if (!adev->psp.funcs) { 1452 dev_err(adev->dev, 1453 "PSP interface not available for nps mode change request"); 1454 return -EINVAL; 1455 } 1456 1457 return psp_memory_partition(&adev->psp, nps_mode); 1458 } 1459 1460 static inline bool amdgpu_gmc_need_nps_switch_req(struct amdgpu_device *adev, 1461 int req_nps_mode, 1462 int cur_nps_mode) 1463 { 1464 return (((BIT(req_nps_mode) & adev->gmc.supported_nps_modes) == 1465 BIT(req_nps_mode)) && 1466 req_nps_mode != cur_nps_mode); 1467 } 1468 1469 void amdgpu_gmc_prepare_nps_mode_change(struct amdgpu_device *adev) 1470 { 1471 int req_nps_mode, cur_nps_mode, r; 1472 struct amdgpu_hive_info *hive; 1473 1474 if (amdgpu_sriov_vf(adev) || !adev->gmc.supported_nps_modes || 1475 !adev->gmc.gmc_funcs->request_mem_partition_mode) 1476 return; 1477 1478 cur_nps_mode = adev->gmc.gmc_funcs->query_mem_partition_mode(adev); 1479 hive = amdgpu_get_xgmi_hive(adev); 1480 if (hive) { 1481 req_nps_mode = atomic_read(&hive->requested_nps_mode); 1482 if (!amdgpu_gmc_need_nps_switch_req(adev, req_nps_mode, 1483 cur_nps_mode)) { 1484 amdgpu_put_xgmi_hive(hive); 1485 return; 1486 } 1487 r = amdgpu_xgmi_request_nps_change(adev, hive, req_nps_mode); 1488 amdgpu_put_xgmi_hive(hive); 1489 goto out; 1490 } 1491 1492 req_nps_mode = adev->gmc.requested_nps_mode; 1493 if (!amdgpu_gmc_need_nps_switch_req(adev, req_nps_mode, cur_nps_mode)) 1494 return; 1495 1496 /* even if this fails, we should let driver unload w/o blocking */ 1497 r = adev->gmc.gmc_funcs->request_mem_partition_mode(adev, req_nps_mode); 1498 out: 1499 if (r) 1500 dev_err(adev->dev, "NPS mode change request failed\n"); 1501 else 1502 dev_info( 1503 adev->dev, 1504 "NPS mode change request done, reload driver to complete the change\n"); 1505 } 1506 1507 bool amdgpu_gmc_need_reset_on_init(struct amdgpu_device *adev) 1508 { 1509 if (adev->gmc.gmc_funcs->need_reset_on_init) 1510 return adev->gmc.gmc_funcs->need_reset_on_init(adev); 1511 1512 return false; 1513 } 1514 1515 enum amdgpu_memory_partition 1516 amdgpu_gmc_get_vf_memory_partition(struct amdgpu_device *adev) 1517 { 1518 switch (adev->gmc.num_mem_partitions) { 1519 case 0: 1520 return UNKNOWN_MEMORY_PARTITION_MODE; 1521 case 1: 1522 return AMDGPU_NPS1_PARTITION_MODE; 1523 case 2: 1524 return AMDGPU_NPS2_PARTITION_MODE; 1525 case 4: 1526 return AMDGPU_NPS4_PARTITION_MODE; 1527 case 8: 1528 return AMDGPU_NPS8_PARTITION_MODE; 1529 default: 1530 return AMDGPU_NPS1_PARTITION_MODE; 1531 } 1532 } 1533 1534 enum amdgpu_memory_partition 1535 amdgpu_gmc_get_memory_partition(struct amdgpu_device *adev, u32 *supp_modes) 1536 { 1537 enum amdgpu_memory_partition mode = UNKNOWN_MEMORY_PARTITION_MODE; 1538 1539 if (adev->nbio.funcs && 1540 adev->nbio.funcs->get_memory_partition_mode) 1541 mode = adev->nbio.funcs->get_memory_partition_mode(adev, 1542 supp_modes); 1543 else 1544 dev_warn(adev->dev, "memory partition mode query is not supported\n"); 1545 1546 return mode; 1547 } 1548 1549 enum amdgpu_memory_partition 1550 amdgpu_gmc_query_memory_partition(struct amdgpu_device *adev) 1551 { 1552 if (amdgpu_sriov_vf(adev)) 1553 return amdgpu_gmc_get_vf_memory_partition(adev); 1554 else 1555 return amdgpu_gmc_get_memory_partition(adev, NULL); 1556 } 1557 1558 static bool amdgpu_gmc_validate_partition_info(struct amdgpu_device *adev) 1559 { 1560 enum amdgpu_memory_partition mode; 1561 u32 supp_modes; 1562 bool valid; 1563 1564 mode = amdgpu_gmc_get_memory_partition(adev, &supp_modes); 1565 1566 /* Mode detected by hardware not present in supported modes */ 1567 if ((mode != UNKNOWN_MEMORY_PARTITION_MODE) && 1568 !(BIT(mode - 1) & supp_modes)) 1569 return false; 1570 1571 switch (mode) { 1572 case UNKNOWN_MEMORY_PARTITION_MODE: 1573 case AMDGPU_NPS1_PARTITION_MODE: 1574 valid = (adev->gmc.num_mem_partitions == 1); 1575 break; 1576 case AMDGPU_NPS2_PARTITION_MODE: 1577 valid = (adev->gmc.num_mem_partitions == 2); 1578 break; 1579 case AMDGPU_NPS4_PARTITION_MODE: 1580 valid = (adev->gmc.num_mem_partitions == 3 || 1581 adev->gmc.num_mem_partitions == 4); 1582 break; 1583 case AMDGPU_NPS8_PARTITION_MODE: 1584 valid = (adev->gmc.num_mem_partitions == 8); 1585 break; 1586 default: 1587 valid = false; 1588 } 1589 1590 return valid; 1591 } 1592 1593 static bool amdgpu_gmc_is_node_present(int *node_ids, int num_ids, int nid) 1594 { 1595 int i; 1596 1597 /* Check if node with id 'nid' is present in 'node_ids' array */ 1598 for (i = 0; i < num_ids; ++i) 1599 if (node_ids[i] == nid) 1600 return true; 1601 1602 return false; 1603 } 1604 1605 static void 1606 amdgpu_gmc_init_acpi_mem_ranges(struct amdgpu_device *adev, 1607 struct amdgpu_mem_partition_info *mem_ranges) 1608 { 1609 struct amdgpu_numa_info numa_info; 1610 int node_ids[AMDGPU_MAX_MEM_RANGES]; 1611 int num_ranges = 0, ret; 1612 int num_xcc, xcc_id; 1613 uint32_t xcc_mask; 1614 1615 num_xcc = NUM_XCC(adev->gfx.xcc_mask); 1616 xcc_mask = (1U << num_xcc) - 1; 1617 1618 for_each_inst(xcc_id, xcc_mask) { 1619 ret = amdgpu_acpi_get_mem_info(adev, xcc_id, &numa_info); 1620 if (ret) 1621 continue; 1622 1623 if (numa_info.nid == NUMA_NO_NODE) { 1624 mem_ranges[0].size = numa_info.size; 1625 mem_ranges[0].numa.node = numa_info.nid; 1626 num_ranges = 1; 1627 break; 1628 } 1629 1630 if (amdgpu_gmc_is_node_present(node_ids, num_ranges, 1631 numa_info.nid)) 1632 continue; 1633 1634 node_ids[num_ranges] = numa_info.nid; 1635 mem_ranges[num_ranges].numa.node = numa_info.nid; 1636 mem_ranges[num_ranges].size = numa_info.size; 1637 ++num_ranges; 1638 } 1639 1640 adev->gmc.num_mem_partitions = num_ranges; 1641 } 1642 1643 void amdgpu_gmc_init_sw_mem_ranges(struct amdgpu_device *adev, 1644 struct amdgpu_mem_partition_info *mem_ranges) 1645 { 1646 enum amdgpu_memory_partition mode; 1647 u32 start_addr = 0, size; 1648 int i, r, l; 1649 1650 mode = amdgpu_gmc_query_memory_partition(adev); 1651 1652 switch (mode) { 1653 case UNKNOWN_MEMORY_PARTITION_MODE: 1654 adev->gmc.num_mem_partitions = 0; 1655 break; 1656 case AMDGPU_NPS1_PARTITION_MODE: 1657 adev->gmc.num_mem_partitions = 1; 1658 break; 1659 case AMDGPU_NPS2_PARTITION_MODE: 1660 adev->gmc.num_mem_partitions = 2; 1661 break; 1662 case AMDGPU_NPS4_PARTITION_MODE: 1663 if (adev->flags & AMD_IS_APU) 1664 adev->gmc.num_mem_partitions = 3; 1665 else 1666 adev->gmc.num_mem_partitions = 4; 1667 break; 1668 case AMDGPU_NPS8_PARTITION_MODE: 1669 adev->gmc.num_mem_partitions = 8; 1670 break; 1671 default: 1672 adev->gmc.num_mem_partitions = 1; 1673 break; 1674 } 1675 1676 /* Use NPS range info, if populated */ 1677 r = amdgpu_gmc_get_nps_memranges(adev, mem_ranges, 1678 &adev->gmc.num_mem_partitions); 1679 if (!r) { 1680 l = 0; 1681 for (i = 1; i < adev->gmc.num_mem_partitions; ++i) { 1682 if (mem_ranges[i].range.lpfn > 1683 mem_ranges[i - 1].range.lpfn) 1684 l = i; 1685 } 1686 1687 } else { 1688 if (!adev->gmc.num_mem_partitions) { 1689 dev_warn(adev->dev, 1690 "Not able to detect NPS mode, fall back to NPS1\n"); 1691 adev->gmc.num_mem_partitions = 1; 1692 } 1693 /* Fallback to sw based calculation */ 1694 size = (adev->gmc.real_vram_size + SZ_16M) >> AMDGPU_GPU_PAGE_SHIFT; 1695 size /= adev->gmc.num_mem_partitions; 1696 1697 for (i = 0; i < adev->gmc.num_mem_partitions; ++i) { 1698 mem_ranges[i].range.fpfn = start_addr; 1699 mem_ranges[i].size = 1700 ((u64)size << AMDGPU_GPU_PAGE_SHIFT); 1701 mem_ranges[i].range.lpfn = start_addr + size - 1; 1702 start_addr += size; 1703 } 1704 1705 l = adev->gmc.num_mem_partitions - 1; 1706 } 1707 1708 /* Adjust the last one */ 1709 mem_ranges[l].range.lpfn = 1710 (adev->gmc.real_vram_size >> AMDGPU_GPU_PAGE_SHIFT) - 1; 1711 mem_ranges[l].size = 1712 adev->gmc.real_vram_size - 1713 ((u64)mem_ranges[l].range.fpfn << AMDGPU_GPU_PAGE_SHIFT); 1714 } 1715 1716 int amdgpu_gmc_init_mem_ranges(struct amdgpu_device *adev) 1717 { 1718 bool valid; 1719 1720 adev->gmc.mem_partitions = kcalloc(AMDGPU_MAX_MEM_RANGES, 1721 sizeof(struct amdgpu_mem_partition_info), 1722 GFP_KERNEL); 1723 if (!adev->gmc.mem_partitions) 1724 return -ENOMEM; 1725 1726 if (adev->gmc.is_app_apu) 1727 amdgpu_gmc_init_acpi_mem_ranges(adev, adev->gmc.mem_partitions); 1728 else 1729 amdgpu_gmc_init_sw_mem_ranges(adev, adev->gmc.mem_partitions); 1730 1731 if (amdgpu_sriov_vf(adev)) 1732 valid = true; 1733 else 1734 valid = amdgpu_gmc_validate_partition_info(adev); 1735 if (!valid) { 1736 /* TODO: handle invalid case */ 1737 dev_warn(adev->dev, 1738 "Mem ranges not matching with hardware config\n"); 1739 } 1740 1741 return 0; 1742 } 1743