2 * Copyright 2018 Advanced Micro Devices, Inc.
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:
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.
21 * The above copyright notice and this permission notice (including the
22 * next paragraph) shall be included in all copies or substantial portions
27 #include <linux/io-64-nonatomic-lo-hi.h>
29 #include <asm/hypervisor.h>
33 #include "amdgpu_gmc.h"
34 #include "amdgpu_ras.h"
35 #include "amdgpu_xgmi.h"
37 #include <drm/drm_drv.h>
38 #include <drm/ttm/ttm_tt.h>
41 * amdgpu_gmc_pdb0_alloc - allocate vram for pdb0
43 * @adev: amdgpu_device pointer
45 * Allocate video memory for pdb0 and map it for CPU access
46 * Returns 0 for success, error for failure.
48 int amdgpu_gmc_pdb0_alloc(struct amdgpu_device *adev)
51 struct amdgpu_bo_param bp;
52 u64 vram_size = adev->gmc.xgmi.node_segment_size * adev->gmc.xgmi.num_physical_nodes;
53 uint32_t pde0_page_shift = adev->gmc.vmid0_page_table_block_size + 21;
54 uint32_t npdes = (vram_size + (1ULL << pde0_page_shift) -1) >> pde0_page_shift;
56 memset(&bp, 0, sizeof(bp));
57 bp.size = PAGE_ALIGN((npdes + 1) * 8);
58 bp.byte_align = PAGE_SIZE;
59 bp.domain = AMDGPU_GEM_DOMAIN_VRAM;
60 bp.flags = AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
61 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
62 bp.type = ttm_bo_type_kernel;
64 bp.bo_ptr_size = sizeof(struct amdgpu_bo);
66 r = amdgpu_bo_create(adev, &bp, &adev->gmc.pdb0_bo);
70 r = amdgpu_bo_reserve(adev->gmc.pdb0_bo, false);
72 goto bo_reserve_failure;
74 r = amdgpu_bo_pin(adev->gmc.pdb0_bo, AMDGPU_GEM_DOMAIN_VRAM);
77 r = amdgpu_bo_kmap(adev->gmc.pdb0_bo, &adev->gmc.ptr_pdb0);
81 amdgpu_bo_unreserve(adev->gmc.pdb0_bo);
85 amdgpu_bo_unpin(adev->gmc.pdb0_bo);
87 amdgpu_bo_unreserve(adev->gmc.pdb0_bo);
89 amdgpu_bo_unref(&adev->gmc.pdb0_bo);
94 * amdgpu_gmc_get_pde_for_bo - get the PDE for a BO
96 * @bo: the BO to get the PDE for
97 * @level: the level in the PD hirarchy
98 * @addr: resulting addr
99 * @flags: resulting flags
101 * Get the address and flags to be used for a PDE (Page Directory Entry).
103 void amdgpu_gmc_get_pde_for_bo(struct amdgpu_bo *bo, int level,
104 uint64_t *addr, uint64_t *flags)
106 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
108 switch (bo->tbo.resource->mem_type) {
110 *addr = bo->tbo.ttm->dma_address[0];
113 *addr = amdgpu_bo_gpu_offset(bo);
119 *flags = amdgpu_ttm_tt_pde_flags(bo->tbo.ttm, bo->tbo.resource);
120 amdgpu_gmc_get_vm_pde(adev, level, addr, flags);
124 * amdgpu_gmc_pd_addr - return the address of the root directory
126 uint64_t amdgpu_gmc_pd_addr(struct amdgpu_bo *bo)
128 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
131 /* TODO: move that into ASIC specific code */
132 if (adev->asic_type >= CHIP_VEGA10) {
133 uint64_t flags = AMDGPU_PTE_VALID;
135 amdgpu_gmc_get_pde_for_bo(bo, -1, &pd_addr, &flags);
138 pd_addr = amdgpu_bo_gpu_offset(bo);
144 * amdgpu_gmc_set_pte_pde - update the page tables using CPU
146 * @adev: amdgpu_device pointer
147 * @cpu_pt_addr: cpu address of the page table
148 * @gpu_page_idx: entry in the page table to update
149 * @addr: dst addr to write into pte/pde
150 * @flags: access flags
152 * Update the page tables using CPU.
154 int amdgpu_gmc_set_pte_pde(struct amdgpu_device *adev, void *cpu_pt_addr,
155 uint32_t gpu_page_idx, uint64_t addr,
158 void __iomem *ptr = (void *)cpu_pt_addr;
162 * The following is for PTE only. GART does not have PDEs.
164 value = addr & 0x0000FFFFFFFFF000ULL;
166 writeq(value, ptr + (gpu_page_idx * 8));
172 * amdgpu_gmc_agp_addr - return the address in the AGP address space
174 * @bo: TTM BO which needs the address, must be in GTT domain
176 * Tries to figure out how to access the BO through the AGP aperture. Returns
177 * AMDGPU_BO_INVALID_OFFSET if that is not possible.
179 uint64_t amdgpu_gmc_agp_addr(struct ttm_buffer_object *bo)
181 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
183 if (bo->ttm->num_pages != 1 || bo->ttm->caching == ttm_cached)
184 return AMDGPU_BO_INVALID_OFFSET;
186 if (bo->ttm->dma_address[0] + PAGE_SIZE >= adev->gmc.agp_size)
187 return AMDGPU_BO_INVALID_OFFSET;
189 return adev->gmc.agp_start + bo->ttm->dma_address[0];
193 * amdgpu_gmc_vram_location - try to find VRAM location
195 * @adev: amdgpu device structure holding all necessary information
196 * @mc: memory controller structure holding memory information
197 * @base: base address at which to put VRAM
199 * Function will try to place VRAM at base address provided
202 void amdgpu_gmc_vram_location(struct amdgpu_device *adev, struct amdgpu_gmc *mc,
205 uint64_t vis_limit = (uint64_t)amdgpu_vis_vram_limit << 20;
206 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
208 mc->vram_start = base;
209 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
210 if (limit < mc->real_vram_size)
211 mc->real_vram_size = limit;
213 if (vis_limit && vis_limit < mc->visible_vram_size)
214 mc->visible_vram_size = vis_limit;
216 if (mc->real_vram_size < mc->visible_vram_size)
217 mc->visible_vram_size = mc->real_vram_size;
219 if (mc->xgmi.num_physical_nodes == 0) {
220 mc->fb_start = mc->vram_start;
221 mc->fb_end = mc->vram_end;
223 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
224 mc->mc_vram_size >> 20, mc->vram_start,
225 mc->vram_end, mc->real_vram_size >> 20);
228 /** amdgpu_gmc_sysvm_location - place vram and gart in sysvm aperture
230 * @adev: amdgpu device structure holding all necessary information
231 * @mc: memory controller structure holding memory information
233 * This function is only used if use GART for FB translation. In such
234 * case, we use sysvm aperture (vmid0 page tables) for both vram
235 * and gart (aka system memory) access.
237 * GPUVM (and our organization of vmid0 page tables) require sysvm
238 * aperture to be placed at a location aligned with 8 times of native
239 * page size. For example, if vm_context0_cntl.page_table_block_size
240 * is 12, then native page size is 8G (2M*2^12), sysvm should start
241 * with a 64G aligned address. For simplicity, we just put sysvm at
242 * address 0. So vram start at address 0 and gart is right after vram.
244 void amdgpu_gmc_sysvm_location(struct amdgpu_device *adev, struct amdgpu_gmc *mc)
246 u64 hive_vram_start = 0;
247 u64 hive_vram_end = mc->xgmi.node_segment_size * mc->xgmi.num_physical_nodes - 1;
248 mc->vram_start = mc->xgmi.node_segment_size * mc->xgmi.physical_node_id;
249 mc->vram_end = mc->vram_start + mc->xgmi.node_segment_size - 1;
250 mc->gart_start = hive_vram_end + 1;
251 mc->gart_end = mc->gart_start + mc->gart_size - 1;
252 mc->fb_start = hive_vram_start;
253 mc->fb_end = hive_vram_end;
254 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
255 mc->mc_vram_size >> 20, mc->vram_start,
256 mc->vram_end, mc->real_vram_size >> 20);
257 dev_info(adev->dev, "GART: %lluM 0x%016llX - 0x%016llX\n",
258 mc->gart_size >> 20, mc->gart_start, mc->gart_end);
262 * amdgpu_gmc_gart_location - try to find GART location
264 * @adev: amdgpu device structure holding all necessary information
265 * @mc: memory controller structure holding memory information
267 * Function will place try to place GART before or after VRAM.
268 * If GART size is bigger than space left then we ajust GART size.
269 * Thus function will never fails.
271 void amdgpu_gmc_gart_location(struct amdgpu_device *adev, struct amdgpu_gmc *mc)
273 const uint64_t four_gb = 0x100000000ULL;
274 u64 size_af, size_bf;
275 /*To avoid the hole, limit the max mc address to AMDGPU_GMC_HOLE_START*/
276 u64 max_mc_address = min(adev->gmc.mc_mask, AMDGPU_GMC_HOLE_START - 1);
278 /* VCE doesn't like it when BOs cross a 4GB segment, so align
279 * the GART base on a 4GB boundary as well.
281 size_bf = mc->fb_start;
282 size_af = max_mc_address + 1 - ALIGN(mc->fb_end + 1, four_gb);
284 if (mc->gart_size > max(size_bf, size_af)) {
285 dev_warn(adev->dev, "limiting GART\n");
286 mc->gart_size = max(size_bf, size_af);
289 if ((size_bf >= mc->gart_size && size_bf < size_af) ||
290 (size_af < mc->gart_size))
293 mc->gart_start = max_mc_address - mc->gart_size + 1;
295 mc->gart_start &= ~(four_gb - 1);
296 mc->gart_end = mc->gart_start + mc->gart_size - 1;
297 dev_info(adev->dev, "GART: %lluM 0x%016llX - 0x%016llX\n",
298 mc->gart_size >> 20, mc->gart_start, mc->gart_end);
302 * amdgpu_gmc_agp_location - try to find AGP location
303 * @adev: amdgpu device structure holding all necessary information
304 * @mc: memory controller structure holding memory information
306 * Function will place try to find a place for the AGP BAR in the MC address
309 * AGP BAR will be assigned the largest available hole in the address space.
310 * Should be called after VRAM and GART locations are setup.
312 void amdgpu_gmc_agp_location(struct amdgpu_device *adev, struct amdgpu_gmc *mc)
314 const uint64_t sixteen_gb = 1ULL << 34;
315 const uint64_t sixteen_gb_mask = ~(sixteen_gb - 1);
316 u64 size_af, size_bf;
318 if (amdgpu_sriov_vf(adev)) {
319 mc->agp_start = 0xffffffffffff;
326 if (mc->fb_start > mc->gart_start) {
327 size_bf = (mc->fb_start & sixteen_gb_mask) -
328 ALIGN(mc->gart_end + 1, sixteen_gb);
329 size_af = mc->mc_mask + 1 - ALIGN(mc->fb_end + 1, sixteen_gb);
331 size_bf = mc->fb_start & sixteen_gb_mask;
332 size_af = (mc->gart_start & sixteen_gb_mask) -
333 ALIGN(mc->fb_end + 1, sixteen_gb);
336 if (size_bf > size_af) {
337 mc->agp_start = (mc->fb_start - size_bf) & sixteen_gb_mask;
338 mc->agp_size = size_bf;
340 mc->agp_start = ALIGN(mc->fb_end + 1, sixteen_gb);
341 mc->agp_size = size_af;
344 mc->agp_end = mc->agp_start + mc->agp_size - 1;
345 dev_info(adev->dev, "AGP: %lluM 0x%016llX - 0x%016llX\n",
346 mc->agp_size >> 20, mc->agp_start, mc->agp_end);
350 * amdgpu_gmc_fault_key - get hask key from vm fault address and pasid
352 * @addr: 48 bit physical address, page aligned (36 significant bits)
353 * @pasid: 16 bit process address space identifier
355 static inline uint64_t amdgpu_gmc_fault_key(uint64_t addr, uint16_t pasid)
357 return addr << 4 | pasid;
361 * amdgpu_gmc_filter_faults - filter VM faults
363 * @adev: amdgpu device structure
364 * @ih: interrupt ring that the fault received from
365 * @addr: address of the VM fault
366 * @pasid: PASID of the process causing the fault
367 * @timestamp: timestamp of the fault
370 * True if the fault was filtered and should not be processed further.
371 * False if the fault is a new one and needs to be handled.
373 bool amdgpu_gmc_filter_faults(struct amdgpu_device *adev,
374 struct amdgpu_ih_ring *ih, uint64_t addr,
375 uint16_t pasid, uint64_t timestamp)
377 struct amdgpu_gmc *gmc = &adev->gmc;
378 uint64_t stamp, key = amdgpu_gmc_fault_key(addr, pasid);
379 struct amdgpu_gmc_fault *fault;
382 /* Stale retry fault if timestamp goes backward */
383 if (amdgpu_ih_ts_after(timestamp, ih->processed_timestamp))
386 /* If we don't have space left in the ring buffer return immediately */
387 stamp = max(timestamp, AMDGPU_GMC_FAULT_TIMEOUT + 1) -
388 AMDGPU_GMC_FAULT_TIMEOUT;
389 if (gmc->fault_ring[gmc->last_fault].timestamp >= stamp)
392 /* Try to find the fault in the hash */
393 hash = hash_64(key, AMDGPU_GMC_FAULT_HASH_ORDER);
394 fault = &gmc->fault_ring[gmc->fault_hash[hash].idx];
395 while (fault->timestamp >= stamp) {
398 if (atomic64_read(&fault->key) == key)
401 tmp = fault->timestamp;
402 fault = &gmc->fault_ring[fault->next];
404 /* Check if the entry was reused */
405 if (fault->timestamp >= tmp)
409 /* Add the fault to the ring */
410 fault = &gmc->fault_ring[gmc->last_fault];
411 atomic64_set(&fault->key, key);
412 fault->timestamp = timestamp;
414 /* And update the hash */
415 fault->next = gmc->fault_hash[hash].idx;
416 gmc->fault_hash[hash].idx = gmc->last_fault++;
421 * amdgpu_gmc_filter_faults_remove - remove address from VM faults filter
423 * @adev: amdgpu device structure
424 * @addr: address of the VM fault
425 * @pasid: PASID of the process causing the fault
427 * Remove the address from fault filter, then future vm fault on this address
428 * will pass to retry fault handler to recover.
430 void amdgpu_gmc_filter_faults_remove(struct amdgpu_device *adev, uint64_t addr,
433 struct amdgpu_gmc *gmc = &adev->gmc;
434 uint64_t key = amdgpu_gmc_fault_key(addr, pasid);
435 struct amdgpu_gmc_fault *fault;
439 hash = hash_64(key, AMDGPU_GMC_FAULT_HASH_ORDER);
440 fault = &gmc->fault_ring[gmc->fault_hash[hash].idx];
442 if (atomic64_cmpxchg(&fault->key, key, 0) == key)
445 tmp = fault->timestamp;
446 fault = &gmc->fault_ring[fault->next];
447 } while (fault->timestamp < tmp);
450 int amdgpu_gmc_ras_early_init(struct amdgpu_device *adev)
452 if (!adev->gmc.xgmi.connected_to_cpu) {
453 adev->gmc.xgmi.ras = &xgmi_ras;
454 amdgpu_ras_register_ras_block(adev, &adev->gmc.xgmi.ras->ras_block);
455 adev->gmc.xgmi.ras_if = &adev->gmc.xgmi.ras->ras_block.ras_comm;
461 int amdgpu_gmc_ras_late_init(struct amdgpu_device *adev)
466 void amdgpu_gmc_ras_fini(struct amdgpu_device *adev)
472 * The latest engine allocation on gfx9/10 is:
473 * Engine 2, 3: firmware
474 * Engine 0, 1, 4~16: amdgpu ring,
475 * subject to change when ring number changes
476 * Engine 17: Gart flushes
478 #define GFXHUB_FREE_VM_INV_ENGS_BITMAP 0x1FFF3
479 #define MMHUB_FREE_VM_INV_ENGS_BITMAP 0x1FFF3
481 int amdgpu_gmc_allocate_vm_inv_eng(struct amdgpu_device *adev)
483 struct amdgpu_ring *ring;
484 unsigned vm_inv_engs[AMDGPU_MAX_VMHUBS] =
485 {GFXHUB_FREE_VM_INV_ENGS_BITMAP, MMHUB_FREE_VM_INV_ENGS_BITMAP,
486 GFXHUB_FREE_VM_INV_ENGS_BITMAP};
488 unsigned vmhub, inv_eng;
490 if (adev->enable_mes) {
491 /* reserve engine 5 for firmware */
492 for (vmhub = 0; vmhub < AMDGPU_MAX_VMHUBS; vmhub++)
493 vm_inv_engs[vmhub] &= ~(1 << 5);
496 for (i = 0; i < adev->num_rings; ++i) {
497 ring = adev->rings[i];
498 vmhub = ring->funcs->vmhub;
500 if (ring == &adev->mes.ring)
503 inv_eng = ffs(vm_inv_engs[vmhub]);
505 dev_err(adev->dev, "no VM inv eng for ring %s\n",
510 ring->vm_inv_eng = inv_eng - 1;
511 vm_inv_engs[vmhub] &= ~(1 << ring->vm_inv_eng);
513 dev_info(adev->dev, "ring %s uses VM inv eng %u on hub %u\n",
514 ring->name, ring->vm_inv_eng, ring->funcs->vmhub);
521 * amdgpu_gmc_tmz_set -- check and set if a device supports TMZ
522 * @adev: amdgpu_device pointer
524 * Check and set if an the device @adev supports Trusted Memory
527 void amdgpu_gmc_tmz_set(struct amdgpu_device *adev)
529 switch (adev->ip_versions[GC_HWIP][0]) {
531 case IP_VERSION(9, 2, 2):
532 case IP_VERSION(9, 1, 0):
533 /* RENOIR looks like RAVEN */
534 case IP_VERSION(9, 3, 0):
536 case IP_VERSION(10, 3, 7):
537 if (amdgpu_tmz == 0) {
538 adev->gmc.tmz_enabled = false;
540 "Trusted Memory Zone (TMZ) feature disabled (cmd line)\n");
542 adev->gmc.tmz_enabled = true;
544 "Trusted Memory Zone (TMZ) feature enabled\n");
547 case IP_VERSION(10, 1, 10):
548 case IP_VERSION(10, 1, 1):
549 case IP_VERSION(10, 1, 2):
550 case IP_VERSION(10, 1, 3):
551 case IP_VERSION(10, 3, 0):
552 case IP_VERSION(10, 3, 2):
553 case IP_VERSION(10, 3, 4):
554 case IP_VERSION(10, 3, 5):
556 case IP_VERSION(10, 3, 1):
558 case IP_VERSION(10, 3, 3):
559 case IP_VERSION(11, 0, 1):
560 case IP_VERSION(11, 0, 4):
561 /* Don't enable it by default yet.
563 if (amdgpu_tmz < 1) {
564 adev->gmc.tmz_enabled = false;
566 "Trusted Memory Zone (TMZ) feature disabled as experimental (default)\n");
568 adev->gmc.tmz_enabled = true;
570 "Trusted Memory Zone (TMZ) feature enabled as experimental (cmd line)\n");
574 adev->gmc.tmz_enabled = false;
576 "Trusted Memory Zone (TMZ) feature not supported\n");
582 * amdgpu_gmc_noretry_set -- set per asic noretry defaults
583 * @adev: amdgpu_device pointer
585 * Set a per asic default for the no-retry parameter.
588 void amdgpu_gmc_noretry_set(struct amdgpu_device *adev)
590 struct amdgpu_gmc *gmc = &adev->gmc;
591 uint32_t gc_ver = adev->ip_versions[GC_HWIP][0];
592 bool noretry_default = (gc_ver == IP_VERSION(9, 0, 1) ||
593 gc_ver == IP_VERSION(9, 3, 0) ||
594 gc_ver == IP_VERSION(9, 4, 0) ||
595 gc_ver == IP_VERSION(9, 4, 1) ||
596 gc_ver == IP_VERSION(9, 4, 2) ||
597 gc_ver >= IP_VERSION(10, 3, 0));
599 gmc->noretry = (amdgpu_noretry == -1) ? noretry_default : amdgpu_noretry;
602 void amdgpu_gmc_set_vm_fault_masks(struct amdgpu_device *adev, int hub_type,
605 struct amdgpu_vmhub *hub;
608 hub = &adev->vmhub[hub_type];
609 for (i = 0; i < 16; i++) {
610 reg = hub->vm_context0_cntl + hub->ctx_distance * i;
612 tmp = (hub_type == AMDGPU_GFXHUB_0) ?
613 RREG32_SOC15_IP(GC, reg) :
614 RREG32_SOC15_IP(MMHUB, reg);
617 tmp |= hub->vm_cntx_cntl_vm_fault;
619 tmp &= ~hub->vm_cntx_cntl_vm_fault;
621 (hub_type == AMDGPU_GFXHUB_0) ?
622 WREG32_SOC15_IP(GC, reg, tmp) :
623 WREG32_SOC15_IP(MMHUB, reg, tmp);
627 void amdgpu_gmc_get_vbios_allocations(struct amdgpu_device *adev)
632 * Some ASICs need to reserve a region of video memory to avoid access
635 adev->mman.stolen_reserved_offset = 0;
636 adev->mman.stolen_reserved_size = 0;
640 * Currently there is a bug where some memory client outside
641 * of the driver writes to first 8M of VRAM on S3 resume,
642 * this overrides GART which by default gets placed in first 8M and
643 * causes VM_FAULTS once GTT is accessed.
644 * Keep the stolen memory reservation until the while this is not solved.
646 switch (adev->asic_type) {
648 adev->mman.keep_stolen_vga_memory = true;
650 * VEGA10 SRIOV VF with MS_HYPERV host needs some firmware reserved area.
653 if (amdgpu_sriov_vf(adev) && hypervisor_is_type(X86_HYPER_MS_HYPERV)) {
654 adev->mman.stolen_reserved_offset = 0x500000;
655 adev->mman.stolen_reserved_size = 0x200000;
661 adev->mman.keep_stolen_vga_memory = true;
663 case CHIP_YELLOW_CARP:
664 if (amdgpu_discovery == 0) {
665 adev->mman.stolen_reserved_offset = 0x1ffb0000;
666 adev->mman.stolen_reserved_size = 64 * PAGE_SIZE;
670 adev->mman.keep_stolen_vga_memory = false;
674 if (amdgpu_sriov_vf(adev) ||
675 !amdgpu_device_has_display_hardware(adev)) {
678 size = amdgpu_gmc_get_vbios_fb_size(adev);
680 if (adev->mman.keep_stolen_vga_memory)
681 size = max(size, (unsigned)AMDGPU_VBIOS_VGA_ALLOCATION);
684 /* set to 0 if the pre-OS buffer uses up most of vram */
685 if ((adev->gmc.real_vram_size - size) < (8 * 1024 * 1024))
688 if (size > AMDGPU_VBIOS_VGA_ALLOCATION) {
689 adev->mman.stolen_vga_size = AMDGPU_VBIOS_VGA_ALLOCATION;
690 adev->mman.stolen_extended_size = size - adev->mman.stolen_vga_size;
692 adev->mman.stolen_vga_size = size;
693 adev->mman.stolen_extended_size = 0;
698 * amdgpu_gmc_init_pdb0 - initialize PDB0
700 * @adev: amdgpu_device pointer
702 * This function is only used when GART page table is used
703 * for FB address translatioin. In such a case, we construct
704 * a 2-level system VM page table: PDB0->PTB, to cover both
705 * VRAM of the hive and system memory.
707 * PDB0 is static, initialized once on driver initialization.
708 * The first n entries of PDB0 are used as PTE by setting
709 * P bit to 1, pointing to VRAM. The n+1'th entry points
710 * to a big PTB covering system memory.
713 void amdgpu_gmc_init_pdb0(struct amdgpu_device *adev)
716 uint64_t flags = adev->gart.gart_pte_flags; //TODO it is UC. explore NC/RW?
717 /* Each PDE0 (used as PTE) covers (2^vmid0_page_table_block_size)*2M
719 u64 vram_size = adev->gmc.xgmi.node_segment_size * adev->gmc.xgmi.num_physical_nodes;
720 u64 pde0_page_size = (1ULL<<adev->gmc.vmid0_page_table_block_size)<<21;
721 u64 vram_addr = adev->vm_manager.vram_base_offset -
722 adev->gmc.xgmi.physical_node_id * adev->gmc.xgmi.node_segment_size;
723 u64 vram_end = vram_addr + vram_size;
724 u64 gart_ptb_gpu_pa = amdgpu_gmc_vram_pa(adev, adev->gart.bo);
727 if (!drm_dev_enter(adev_to_drm(adev), &idx))
730 flags |= AMDGPU_PTE_VALID | AMDGPU_PTE_READABLE;
731 flags |= AMDGPU_PTE_WRITEABLE;
732 flags |= AMDGPU_PTE_SNOOPED;
733 flags |= AMDGPU_PTE_FRAG((adev->gmc.vmid0_page_table_block_size + 9*1));
734 flags |= AMDGPU_PDE_PTE;
736 /* The first n PDE0 entries are used as PTE,
739 for (i = 0; vram_addr < vram_end; i++, vram_addr += pde0_page_size)
740 amdgpu_gmc_set_pte_pde(adev, adev->gmc.ptr_pdb0, i, vram_addr, flags);
742 /* The n+1'th PDE0 entry points to a huge
743 * PTB who has more than 512 entries each
744 * pointing to a 4K system page
746 flags = AMDGPU_PTE_VALID;
747 flags |= AMDGPU_PDE_BFS(0) | AMDGPU_PTE_SNOOPED;
748 /* Requires gart_ptb_gpu_pa to be 4K aligned */
749 amdgpu_gmc_set_pte_pde(adev, adev->gmc.ptr_pdb0, i, gart_ptb_gpu_pa, flags);
754 * amdgpu_gmc_vram_mc2pa - calculate vram buffer's physical address from MC
757 * @adev: amdgpu_device pointer
758 * @mc_addr: MC address of buffer
760 uint64_t amdgpu_gmc_vram_mc2pa(struct amdgpu_device *adev, uint64_t mc_addr)
762 return mc_addr - adev->gmc.vram_start + adev->vm_manager.vram_base_offset;
766 * amdgpu_gmc_vram_pa - calculate vram buffer object's physical address from
769 * @adev: amdgpu_device pointer
770 * @bo: amdgpu buffer object
772 uint64_t amdgpu_gmc_vram_pa(struct amdgpu_device *adev, struct amdgpu_bo *bo)
774 return amdgpu_gmc_vram_mc2pa(adev, amdgpu_bo_gpu_offset(bo));
778 * amdgpu_gmc_vram_cpu_pa - calculate vram buffer object's physical address
781 * @adev: amdgpu_device pointer
782 * @bo: amdgpu buffer object
784 uint64_t amdgpu_gmc_vram_cpu_pa(struct amdgpu_device *adev, struct amdgpu_bo *bo)
786 return amdgpu_bo_gpu_offset(bo) - adev->gmc.vram_start + adev->gmc.aper_base;
789 int amdgpu_gmc_vram_checking(struct amdgpu_device *adev)
791 struct amdgpu_bo *vram_bo = NULL;
792 uint64_t vram_gpu = 0;
793 void *vram_ptr = NULL;
795 int ret, size = 0x100000;
798 ret = amdgpu_bo_create_kernel(adev, size, PAGE_SIZE,
799 AMDGPU_GEM_DOMAIN_VRAM,
806 memset(vram_ptr, 0x86, size);
807 memset(cptr, 0x86, 10);
810 * Check the start, the mid, and the end of the memory if the content of
811 * each byte is the pattern "0x86". If yes, we suppose the vram bo is
814 * Note: If check the each byte of whole 1M bo, it will cost too many
815 * seconds, so here, we just pick up three parts for emulation.
817 ret = memcmp(vram_ptr, cptr, 10);
821 ret = memcmp(vram_ptr + (size / 2), cptr, 10);
825 ret = memcmp(vram_ptr + size - 10, cptr, 10);
829 amdgpu_bo_free_kernel(&vram_bo, &vram_gpu,