1e60f8db5SAlex Xie /*
2e60f8db5SAlex Xie * Copyright 2016 Advanced Micro Devices, Inc.
3e60f8db5SAlex Xie *
4e60f8db5SAlex Xie * Permission is hereby granted, free of charge, to any person obtaining a
5e60f8db5SAlex Xie * copy of this software and associated documentation files (the "Software"),
6e60f8db5SAlex Xie * to deal in the Software without restriction, including without limitation
7e60f8db5SAlex Xie * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8e60f8db5SAlex Xie * and/or sell copies of the Software, and to permit persons to whom the
9e60f8db5SAlex Xie * Software is furnished to do so, subject to the following conditions:
10e60f8db5SAlex Xie *
11e60f8db5SAlex Xie * The above copyright notice and this permission notice shall be included in
12e60f8db5SAlex Xie * all copies or substantial portions of the Software.
13e60f8db5SAlex Xie *
14e60f8db5SAlex Xie * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15e60f8db5SAlex Xie * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16e60f8db5SAlex Xie * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17e60f8db5SAlex Xie * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18e60f8db5SAlex Xie * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19e60f8db5SAlex Xie * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20e60f8db5SAlex Xie * OTHER DEALINGS IN THE SOFTWARE.
21e60f8db5SAlex Xie *
22e60f8db5SAlex Xie */
23e60f8db5SAlex Xie #include "amdgpu.h"
24e60f8db5SAlex Xie #include "gfxhub_v1_0.h"
2521470d97SKevin Wang #include "gfxhub_v1_1.h"
26e60f8db5SAlex Xie
27cde5c34fSFeifei Xu #include "gc/gc_9_0_offset.h"
28cde5c34fSFeifei Xu #include "gc/gc_9_0_sh_mask.h"
29cde5c34fSFeifei Xu #include "gc/gc_9_0_default.h"
30fb960bd2SFeifei Xu #include "vega10_enum.h"
31e60f8db5SAlex Xie
32e60f8db5SAlex Xie #include "soc15_common.h"
33e60f8db5SAlex Xie
gfxhub_v1_0_get_mc_fb_offset(struct amdgpu_device * adev)340d4d9512SHawking Zhang static u64 gfxhub_v1_0_get_mc_fb_offset(struct amdgpu_device *adev)
352d8e898eSChunming Zhou {
36f7047402STom St Denis return (u64)RREG32_SOC15(GC, 0, mmMC_VM_FB_OFFSET) << 24;
372d8e898eSChunming Zhou }
382d8e898eSChunming Zhou
gfxhub_v1_0_setup_vm_pt_regs(struct amdgpu_device * adev,uint32_t vmid,uint64_t page_table_base)390d4d9512SHawking Zhang static void gfxhub_v1_0_setup_vm_pt_regs(struct amdgpu_device *adev,
400d4d9512SHawking Zhang uint32_t vmid,
41c7ff7be6SYong Zhao uint64_t page_table_base)
42a51dca4fSHuang Rui {
43f4caf584SHawking Zhang struct amdgpu_vmhub *hub = &adev->vmhub[AMDGPU_GFXHUB(0)];
44a51dca4fSHuang Rui
45c7ff7be6SYong Zhao WREG32_SOC15_OFFSET(GC, 0, mmVM_CONTEXT0_PAGE_TABLE_BASE_ADDR_LO32,
468c471360SHuang Rui hub->ctx_addr_distance * vmid,
478c471360SHuang Rui lower_32_bits(page_table_base));
48a51dca4fSHuang Rui
49c7ff7be6SYong Zhao WREG32_SOC15_OFFSET(GC, 0, mmVM_CONTEXT0_PAGE_TABLE_BASE_ADDR_HI32,
508c471360SHuang Rui hub->ctx_addr_distance * vmid,
518c471360SHuang Rui upper_32_bits(page_table_base));
52a51dca4fSHuang Rui }
53a51dca4fSHuang Rui
gfxhub_v1_0_init_gart_aperture_regs(struct amdgpu_device * adev)549bbad6fdSHuang Rui static void gfxhub_v1_0_init_gart_aperture_regs(struct amdgpu_device *adev)
559bbad6fdSHuang Rui {
560c19cab5SOak Zeng uint64_t pt_base;
570c19cab5SOak Zeng
580c19cab5SOak Zeng if (adev->gmc.pdb0_bo)
590c19cab5SOak Zeng pt_base = amdgpu_gmc_pd_addr(adev->gmc.pdb0_bo);
600c19cab5SOak Zeng else
610c19cab5SOak Zeng pt_base = amdgpu_gmc_pd_addr(adev->gart.bo);
62c7ff7be6SYong Zhao
63c7ff7be6SYong Zhao gfxhub_v1_0_setup_vm_pt_regs(adev, 0, pt_base);
649bbad6fdSHuang Rui
650c19cab5SOak Zeng /* If use GART for FB translation, vmid0 page table covers both
660c19cab5SOak Zeng * vram and system memory (gart)
670c19cab5SOak Zeng */
680c19cab5SOak Zeng if (adev->gmc.pdb0_bo) {
690c19cab5SOak Zeng WREG32_SOC15(GC, 0, mmVM_CONTEXT0_PAGE_TABLE_START_ADDR_LO32,
700c19cab5SOak Zeng (u32)(adev->gmc.fb_start >> 12));
710c19cab5SOak Zeng WREG32_SOC15(GC, 0, mmVM_CONTEXT0_PAGE_TABLE_START_ADDR_HI32,
720c19cab5SOak Zeng (u32)(adev->gmc.fb_start >> 44));
730c19cab5SOak Zeng
740c19cab5SOak Zeng WREG32_SOC15(GC, 0, mmVM_CONTEXT0_PAGE_TABLE_END_ADDR_LO32,
750c19cab5SOak Zeng (u32)(adev->gmc.gart_end >> 12));
760c19cab5SOak Zeng WREG32_SOC15(GC, 0, mmVM_CONTEXT0_PAGE_TABLE_END_ADDR_HI32,
770c19cab5SOak Zeng (u32)(adev->gmc.gart_end >> 44));
780c19cab5SOak Zeng } else {
7989f99cebSHuang Rui WREG32_SOC15(GC, 0, mmVM_CONTEXT0_PAGE_TABLE_START_ADDR_LO32,
80770d13b1SChristian König (u32)(adev->gmc.gart_start >> 12));
8189f99cebSHuang Rui WREG32_SOC15(GC, 0, mmVM_CONTEXT0_PAGE_TABLE_START_ADDR_HI32,
82770d13b1SChristian König (u32)(adev->gmc.gart_start >> 44));
839bbad6fdSHuang Rui
8489f99cebSHuang Rui WREG32_SOC15(GC, 0, mmVM_CONTEXT0_PAGE_TABLE_END_ADDR_LO32,
85770d13b1SChristian König (u32)(adev->gmc.gart_end >> 12));
8689f99cebSHuang Rui WREG32_SOC15(GC, 0, mmVM_CONTEXT0_PAGE_TABLE_END_ADDR_HI32,
87770d13b1SChristian König (u32)(adev->gmc.gart_end >> 44));
889bbad6fdSHuang Rui }
890c19cab5SOak Zeng }
909bbad6fdSHuang Rui
gfxhub_v1_0_init_system_aperture_regs(struct amdgpu_device * adev)91fc4b884bSHuang Rui static void gfxhub_v1_0_init_system_aperture_regs(struct amdgpu_device *adev)
92e60f8db5SAlex Xie {
93fc4b884bSHuang Rui uint64_t value;
94e60f8db5SAlex Xie
95c3e1b43cSChristian König /* Program the AGP BAR */
961bff7f6cSTrigger Huang WREG32_SOC15_RLC(GC, 0, mmMC_VM_AGP_BASE, 0);
971bff7f6cSTrigger Huang WREG32_SOC15_RLC(GC, 0, mmMC_VM_AGP_BOT, adev->gmc.agp_start >> 24);
981bff7f6cSTrigger Huang WREG32_SOC15_RLC(GC, 0, mmMC_VM_AGP_TOP, adev->gmc.agp_end >> 24);
99a51dca4fSHuang Rui
10008546895SZhigang Luo if (!amdgpu_sriov_vf(adev) || adev->asic_type <= CHIP_VEGA10) {
101fc4b884bSHuang Rui /* Program the system aperture low logical page number. */
1021bff7f6cSTrigger Huang WREG32_SOC15_RLC(GC, 0, mmMC_VM_SYSTEM_APERTURE_LOW_ADDR,
1035581c670Sshaoyunl min(adev->gmc.fb_start, adev->gmc.agp_start) >> 18);
10476006776SHuang Rui
105*08dde830SAlex Deucher if (adev->apu_flags & (AMD_APU_IS_RAVEN2 |
106*08dde830SAlex Deucher AMD_APU_IS_RENOIR |
107*08dde830SAlex Deucher AMD_APU_IS_GREEN_SARDINE))
10876006776SHuang Rui /*
10908546895SZhigang Luo * Raven2 has a HW issue that it is unable to use the
11008546895SZhigang Luo * vram which is out of MC_VM_SYSTEM_APERTURE_HIGH_ADDR.
11108546895SZhigang Luo * So here is the workaround that increase system
11208546895SZhigang Luo * aperture high address (add 1) to get rid of the VM
11308546895SZhigang Luo * fault and hardware hang.
11476006776SHuang Rui */
11508546895SZhigang Luo WREG32_SOC15_RLC(GC, 0,
11608546895SZhigang Luo mmMC_VM_SYSTEM_APERTURE_HIGH_ADDR,
1175581c670Sshaoyunl max((adev->gmc.fb_end >> 18) + 0x1,
11875986276SHuang Rui adev->gmc.agp_end >> 18));
11976006776SHuang Rui else
12008546895SZhigang Luo WREG32_SOC15_RLC(
12108546895SZhigang Luo GC, 0, mmMC_VM_SYSTEM_APERTURE_HIGH_ADDR,
1225581c670Sshaoyunl max(adev->gmc.fb_end, adev->gmc.agp_end) >> 18);
123e60f8db5SAlex Xie
124fc4b884bSHuang Rui /* Set default page address. */
1257ccfd79fSChristian König value = amdgpu_gmc_vram_mc2pa(adev, adev->mem_scratch.gpu_addr);
12689f99cebSHuang Rui WREG32_SOC15(GC, 0, mmMC_VM_SYSTEM_APERTURE_DEFAULT_ADDR_LSB,
127e60f8db5SAlex Xie (u32)(value >> 12));
12889f99cebSHuang Rui WREG32_SOC15(GC, 0, mmMC_VM_SYSTEM_APERTURE_DEFAULT_ADDR_MSB,
129e60f8db5SAlex Xie (u32)(value >> 44));
130e60f8db5SAlex Xie
131fc4b884bSHuang Rui /* Program "protection fault". */
13289f99cebSHuang Rui WREG32_SOC15(GC, 0, mmVM_L2_PROTECTION_FAULT_DEFAULT_ADDR_LO32,
13392e71b06SChristian König (u32)(adev->dummy_page_addr >> 12));
13489f99cebSHuang Rui WREG32_SOC15(GC, 0, mmVM_L2_PROTECTION_FAULT_DEFAULT_ADDR_HI32,
13592e71b06SChristian König (u32)((u64)adev->dummy_page_addr >> 44));
136fc4b884bSHuang Rui
137805cb75cSTom St Denis WREG32_FIELD15(GC, 0, VM_L2_PROTECTION_FAULT_CNTL2,
138fc4b884bSHuang Rui ACTIVE_PAGE_MIGRATION_PTE_READ_RETRY, 1);
139fc4b884bSHuang Rui }
1400c19cab5SOak Zeng
1410c19cab5SOak Zeng /* In the case squeezing vram into GART aperture, we don't use
1420c19cab5SOak Zeng * FB aperture and AGP aperture. Disable them.
1430c19cab5SOak Zeng */
1440c19cab5SOak Zeng if (adev->gmc.pdb0_bo) {
1450c19cab5SOak Zeng WREG32_SOC15(GC, 0, mmMC_VM_FB_LOCATION_TOP, 0);
1460c19cab5SOak Zeng WREG32_SOC15(GC, 0, mmMC_VM_FB_LOCATION_BASE, 0x00FFFFFF);
1470c19cab5SOak Zeng WREG32_SOC15(GC, 0, mmMC_VM_AGP_TOP, 0);
1480c19cab5SOak Zeng WREG32_SOC15(GC, 0, mmMC_VM_AGP_BOT, 0xFFFFFF);
1490c19cab5SOak Zeng WREG32_SOC15(GC, 0, mmMC_VM_SYSTEM_APERTURE_LOW_ADDR, 0x3FFFFFFF);
1500c19cab5SOak Zeng WREG32_SOC15(GC, 0, mmMC_VM_SYSTEM_APERTURE_HIGH_ADDR, 0);
1510c19cab5SOak Zeng }
15208546895SZhigang Luo }
153fc4b884bSHuang Rui
gfxhub_v1_0_init_tlb_regs(struct amdgpu_device * adev)15434269839SHuang Rui static void gfxhub_v1_0_init_tlb_regs(struct amdgpu_device *adev)
15534269839SHuang Rui {
15634269839SHuang Rui uint32_t tmp;
15734269839SHuang Rui
15834269839SHuang Rui /* Setup TLB control */
15989f99cebSHuang Rui tmp = RREG32_SOC15(GC, 0, mmMC_VM_MX_L1_TLB_CNTL);
16034269839SHuang Rui
16134269839SHuang Rui tmp = REG_SET_FIELD(tmp, MC_VM_MX_L1_TLB_CNTL, ENABLE_L1_TLB, 1);
16234269839SHuang Rui tmp = REG_SET_FIELD(tmp, MC_VM_MX_L1_TLB_CNTL, SYSTEM_ACCESS_MODE, 3);
16334269839SHuang Rui tmp = REG_SET_FIELD(tmp, MC_VM_MX_L1_TLB_CNTL,
16434269839SHuang Rui ENABLE_ADVANCED_DRIVER_MODEL, 1);
16534269839SHuang Rui tmp = REG_SET_FIELD(tmp, MC_VM_MX_L1_TLB_CNTL,
16634269839SHuang Rui SYSTEM_APERTURE_UNMAPPED_ACCESS, 0);
16734269839SHuang Rui tmp = REG_SET_FIELD(tmp, MC_VM_MX_L1_TLB_CNTL,
16834269839SHuang Rui MTYPE, MTYPE_UC);/* XXX for emulation. */
16934269839SHuang Rui tmp = REG_SET_FIELD(tmp, MC_VM_MX_L1_TLB_CNTL, ATC_EN, 1);
17034269839SHuang Rui
1711bff7f6cSTrigger Huang WREG32_SOC15_RLC(GC, 0, mmMC_VM_MX_L1_TLB_CNTL, tmp);
17234269839SHuang Rui }
17334269839SHuang Rui
gfxhub_v1_0_init_cache_regs(struct amdgpu_device * adev)17441f6f311SHuang Rui static void gfxhub_v1_0_init_cache_regs(struct amdgpu_device *adev)
17541f6f311SHuang Rui {
176a3ce3645SRoger He uint32_t tmp;
17741f6f311SHuang Rui
17841f6f311SHuang Rui /* Setup L2 cache */
17989f99cebSHuang Rui tmp = RREG32_SOC15(GC, 0, mmVM_L2_CNTL);
18041f6f311SHuang Rui tmp = REG_SET_FIELD(tmp, VM_L2_CNTL, ENABLE_L2_CACHE, 1);
1816be7adb3SChristian König tmp = REG_SET_FIELD(tmp, VM_L2_CNTL, ENABLE_L2_FRAGMENT_PROCESSING, 1);
18241f6f311SHuang Rui /* XXX for emulation, Refer to closed source code.*/
18341f6f311SHuang Rui tmp = REG_SET_FIELD(tmp, VM_L2_CNTL, L2_PDE0_CACHE_TAG_GENERATION_MODE,
18441f6f311SHuang Rui 0);
1850cd57eecSYong Zhao tmp = REG_SET_FIELD(tmp, VM_L2_CNTL, PDE_FAULT_CLASSIFICATION, 0);
18641f6f311SHuang Rui tmp = REG_SET_FIELD(tmp, VM_L2_CNTL, CONTEXT1_IDENTITY_ACCESS_MODE, 1);
18741f6f311SHuang Rui tmp = REG_SET_FIELD(tmp, VM_L2_CNTL, IDENTITY_MODE_FRAGMENT_SIZE, 0);
188bdb50274SEmily Deng WREG32_SOC15_RLC(GC, 0, mmVM_L2_CNTL, tmp);
18941f6f311SHuang Rui
19089f99cebSHuang Rui tmp = RREG32_SOC15(GC, 0, mmVM_L2_CNTL2);
19141f6f311SHuang Rui tmp = REG_SET_FIELD(tmp, VM_L2_CNTL2, INVALIDATE_ALL_L1_TLBS, 1);
19241f6f311SHuang Rui tmp = REG_SET_FIELD(tmp, VM_L2_CNTL2, INVALIDATE_L2_CACHE, 1);
193bdb50274SEmily Deng WREG32_SOC15_RLC(GC, 0, mmVM_L2_CNTL2, tmp);
19441f6f311SHuang Rui
19541f6f311SHuang Rui tmp = mmVM_L2_CNTL3_DEFAULT;
196770d13b1SChristian König if (adev->gmc.translate_further) {
1976a42fd6fSChristian König tmp = REG_SET_FIELD(tmp, VM_L2_CNTL3, BANK_SELECT, 12);
1986a42fd6fSChristian König tmp = REG_SET_FIELD(tmp, VM_L2_CNTL3,
1996a42fd6fSChristian König L2_CACHE_BIGK_FRAGMENT_SIZE, 9);
2006a42fd6fSChristian König } else {
201a3ce3645SRoger He tmp = REG_SET_FIELD(tmp, VM_L2_CNTL3, BANK_SELECT, 9);
2026a42fd6fSChristian König tmp = REG_SET_FIELD(tmp, VM_L2_CNTL3,
2036a42fd6fSChristian König L2_CACHE_BIGK_FRAGMENT_SIZE, 6);
2046a42fd6fSChristian König }
205bdb50274SEmily Deng WREG32_SOC15_RLC(GC, 0, mmVM_L2_CNTL3, tmp);
20641f6f311SHuang Rui
20741f6f311SHuang Rui tmp = mmVM_L2_CNTL4_DEFAULT;
2081f928f51SOak Zeng if (adev->gmc.xgmi.connected_to_cpu) {
2091f928f51SOak Zeng tmp = REG_SET_FIELD(tmp, VM_L2_CNTL4, VMC_TAP_PDE_REQUEST_PHYSICAL, 1);
2101f928f51SOak Zeng tmp = REG_SET_FIELD(tmp, VM_L2_CNTL4, VMC_TAP_PTE_REQUEST_PHYSICAL, 1);
2111f928f51SOak Zeng } else {
21241f6f311SHuang Rui tmp = REG_SET_FIELD(tmp, VM_L2_CNTL4, VMC_TAP_PDE_REQUEST_PHYSICAL, 0);
21341f6f311SHuang Rui tmp = REG_SET_FIELD(tmp, VM_L2_CNTL4, VMC_TAP_PTE_REQUEST_PHYSICAL, 0);
2141f928f51SOak Zeng }
215bdb50274SEmily Deng WREG32_SOC15_RLC(GC, 0, mmVM_L2_CNTL4, tmp);
21641f6f311SHuang Rui }
21741f6f311SHuang Rui
gfxhub_v1_0_enable_system_domain(struct amdgpu_device * adev)21802c4704bSHuang Rui static void gfxhub_v1_0_enable_system_domain(struct amdgpu_device *adev)
21902c4704bSHuang Rui {
22002c4704bSHuang Rui uint32_t tmp;
22102c4704bSHuang Rui
22289f99cebSHuang Rui tmp = RREG32_SOC15(GC, 0, mmVM_CONTEXT0_CNTL);
22302c4704bSHuang Rui tmp = REG_SET_FIELD(tmp, VM_CONTEXT0_CNTL, ENABLE_CONTEXT, 1);
2247b454b3aSOak Zeng tmp = REG_SET_FIELD(tmp, VM_CONTEXT0_CNTL, PAGE_TABLE_DEPTH,
2257b454b3aSOak Zeng adev->gmc.vmid0_page_table_depth);
2267b454b3aSOak Zeng tmp = REG_SET_FIELD(tmp, VM_CONTEXT0_CNTL, PAGE_TABLE_BLOCK_SIZE,
2277b454b3aSOak Zeng adev->gmc.vmid0_page_table_block_size);
2287cae7061SFelix Kuehling tmp = REG_SET_FIELD(tmp, VM_CONTEXT0_CNTL,
2297cae7061SFelix Kuehling RETRY_PERMISSION_OR_INVALID_PAGE_FAULT, 0);
23089f99cebSHuang Rui WREG32_SOC15(GC, 0, mmVM_CONTEXT0_CNTL, tmp);
23102c4704bSHuang Rui }
23202c4704bSHuang Rui
gfxhub_v1_0_disable_identity_aperture(struct amdgpu_device * adev)233d5c87390SHuang Rui static void gfxhub_v1_0_disable_identity_aperture(struct amdgpu_device *adev)
234d5c87390SHuang Rui {
23589f99cebSHuang Rui WREG32_SOC15(GC, 0, mmVM_L2_CONTEXT1_IDENTITY_APERTURE_LOW_ADDR_LO32,
236d5c87390SHuang Rui 0XFFFFFFFF);
23789f99cebSHuang Rui WREG32_SOC15(GC, 0, mmVM_L2_CONTEXT1_IDENTITY_APERTURE_LOW_ADDR_HI32,
23889f99cebSHuang Rui 0x0000000F);
239d5c87390SHuang Rui
24089f99cebSHuang Rui WREG32_SOC15(GC, 0, mmVM_L2_CONTEXT1_IDENTITY_APERTURE_HIGH_ADDR_LO32,
24189f99cebSHuang Rui 0);
24289f99cebSHuang Rui WREG32_SOC15(GC, 0, mmVM_L2_CONTEXT1_IDENTITY_APERTURE_HIGH_ADDR_HI32,
24389f99cebSHuang Rui 0);
244d5c87390SHuang Rui
24589f99cebSHuang Rui WREG32_SOC15(GC, 0, mmVM_L2_CONTEXT_IDENTITY_PHYSICAL_OFFSET_LO32, 0);
24689f99cebSHuang Rui WREG32_SOC15(GC, 0, mmVM_L2_CONTEXT_IDENTITY_PHYSICAL_OFFSET_HI32, 0);
247d5c87390SHuang Rui
248d5c87390SHuang Rui }
249d5c87390SHuang Rui
gfxhub_v1_0_setup_vmid_config(struct amdgpu_device * adev)2503dff4cc4SHuang Rui static void gfxhub_v1_0_setup_vmid_config(struct amdgpu_device *adev)
251fc4b884bSHuang Rui {
252f4caf584SHawking Zhang struct amdgpu_vmhub *hub = &adev->vmhub[AMDGPU_GFXHUB(0)];
25362e6771aSSrinivasan Shanmugam unsigned int num_level, block_size;
2543dff4cc4SHuang Rui uint32_t tmp;
2556a42fd6fSChristian König int i;
2566a42fd6fSChristian König
2576a42fd6fSChristian König num_level = adev->vm_manager.num_level;
2586a42fd6fSChristian König block_size = adev->vm_manager.block_size;
259770d13b1SChristian König if (adev->gmc.translate_further)
2606a42fd6fSChristian König num_level -= 1;
2616a42fd6fSChristian König else
2626a42fd6fSChristian König block_size -= 9;
263e60f8db5SAlex Xie
264e60f8db5SAlex Xie for (i = 0; i <= 14; i++) {
265f7047402STom St Denis tmp = RREG32_SOC15_OFFSET(GC, 0, mmVM_CONTEXT1_CNTL, i);
266e60f8db5SAlex Xie tmp = REG_SET_FIELD(tmp, VM_CONTEXT1_CNTL, ENABLE_CONTEXT, 1);
2674fb1cf3aSChunming Zhou tmp = REG_SET_FIELD(tmp, VM_CONTEXT1_CNTL, PAGE_TABLE_DEPTH,
2686a42fd6fSChristian König num_level);
269e60f8db5SAlex Xie tmp = REG_SET_FIELD(tmp, VM_CONTEXT1_CNTL,
270e60f8db5SAlex Xie RANGE_PROTECTION_FAULT_ENABLE_DEFAULT, 1);
271e60f8db5SAlex Xie tmp = REG_SET_FIELD(tmp, VM_CONTEXT1_CNTL,
2726a42fd6fSChristian König DUMMY_PAGE_PROTECTION_FAULT_ENABLE_DEFAULT,
2736a42fd6fSChristian König 1);
274e60f8db5SAlex Xie tmp = REG_SET_FIELD(tmp, VM_CONTEXT1_CNTL,
275e60f8db5SAlex Xie PDE0_PROTECTION_FAULT_ENABLE_DEFAULT, 1);
276e60f8db5SAlex Xie tmp = REG_SET_FIELD(tmp, VM_CONTEXT1_CNTL,
277e60f8db5SAlex Xie VALID_PROTECTION_FAULT_ENABLE_DEFAULT, 1);
278e60f8db5SAlex Xie tmp = REG_SET_FIELD(tmp, VM_CONTEXT1_CNTL,
279e60f8db5SAlex Xie READ_PROTECTION_FAULT_ENABLE_DEFAULT, 1);
280e60f8db5SAlex Xie tmp = REG_SET_FIELD(tmp, VM_CONTEXT1_CNTL,
281e60f8db5SAlex Xie WRITE_PROTECTION_FAULT_ENABLE_DEFAULT, 1);
282e60f8db5SAlex Xie tmp = REG_SET_FIELD(tmp, VM_CONTEXT1_CNTL,
283e60f8db5SAlex Xie EXECUTE_PROTECTION_FAULT_ENABLE_DEFAULT, 1);
284e60f8db5SAlex Xie tmp = REG_SET_FIELD(tmp, VM_CONTEXT1_CNTL,
285e60f8db5SAlex Xie PAGE_TABLE_BLOCK_SIZE,
2866a42fd6fSChristian König block_size);
2879705c85fSFelix Kuehling /* Send no-retry XNACK on fault to suppress VM fault storm.
2889705c85fSFelix Kuehling * On Aldebaran, XNACK can be enabled in the SQ per-process.
2899705c85fSFelix Kuehling * Retry faults need to be enabled for that to work.
2909705c85fSFelix Kuehling */
2919f57f7b4SJay Cornwall tmp = REG_SET_FIELD(tmp, VM_CONTEXT1_CNTL,
29275ee6487SFelix Kuehling RETRY_PERMISSION_OR_INVALID_PAGE_FAULT,
2939705c85fSFelix Kuehling !adev->gmc.noretry ||
2949705c85fSFelix Kuehling adev->asic_type == CHIP_ALDEBARAN);
2958c471360SHuang Rui WREG32_SOC15_OFFSET(GC, 0, mmVM_CONTEXT1_CNTL,
2968c471360SHuang Rui i * hub->ctx_distance, tmp);
2978c471360SHuang Rui WREG32_SOC15_OFFSET(GC, 0, mmVM_CONTEXT1_PAGE_TABLE_START_ADDR_LO32,
2988c471360SHuang Rui i * hub->ctx_addr_distance, 0);
2998c471360SHuang Rui WREG32_SOC15_OFFSET(GC, 0, mmVM_CONTEXT1_PAGE_TABLE_START_ADDR_HI32,
3008c471360SHuang Rui i * hub->ctx_addr_distance, 0);
3018c471360SHuang Rui WREG32_SOC15_OFFSET(GC, 0, mmVM_CONTEXT1_PAGE_TABLE_END_ADDR_LO32,
3028c471360SHuang Rui i * hub->ctx_addr_distance,
30322770e5aSFelix Kuehling lower_32_bits(adev->vm_manager.max_pfn - 1));
3048c471360SHuang Rui WREG32_SOC15_OFFSET(GC, 0, mmVM_CONTEXT1_PAGE_TABLE_END_ADDR_HI32,
3058c471360SHuang Rui i * hub->ctx_addr_distance,
30622770e5aSFelix Kuehling upper_32_bits(adev->vm_manager.max_pfn - 1));
307e60f8db5SAlex Xie }
3083dff4cc4SHuang Rui }
309e60f8db5SAlex Xie
gfxhub_v1_0_program_invalidation(struct amdgpu_device * adev)3101e4eccdaSHuang Rui static void gfxhub_v1_0_program_invalidation(struct amdgpu_device *adev)
3111e4eccdaSHuang Rui {
312f4caf584SHawking Zhang struct amdgpu_vmhub *hub = &adev->vmhub[AMDGPU_GFXHUB(0)];
31362e6771aSSrinivasan Shanmugam unsigned int i;
3141e4eccdaSHuang Rui
3151e4eccdaSHuang Rui for (i = 0 ; i < 18; ++i) {
316f7047402STom St Denis WREG32_SOC15_OFFSET(GC, 0, mmVM_INVALIDATE_ENG0_ADDR_RANGE_LO32,
3178c471360SHuang Rui i * hub->eng_addr_distance, 0xffffffff);
318f7047402STom St Denis WREG32_SOC15_OFFSET(GC, 0, mmVM_INVALIDATE_ENG0_ADDR_RANGE_HI32,
3198c471360SHuang Rui i * hub->eng_addr_distance, 0x1f);
3201e4eccdaSHuang Rui }
3211e4eccdaSHuang Rui }
3221e4eccdaSHuang Rui
gfxhub_v1_0_gart_enable(struct amdgpu_device * adev)3230d4d9512SHawking Zhang static int gfxhub_v1_0_gart_enable(struct amdgpu_device *adev)
3243dff4cc4SHuang Rui {
3253dff4cc4SHuang Rui /* GART Enable. */
3263dff4cc4SHuang Rui gfxhub_v1_0_init_gart_aperture_regs(adev);
3273dff4cc4SHuang Rui gfxhub_v1_0_init_system_aperture_regs(adev);
3283dff4cc4SHuang Rui gfxhub_v1_0_init_tlb_regs(adev);
32908546895SZhigang Luo if (!amdgpu_sriov_vf(adev))
3303dff4cc4SHuang Rui gfxhub_v1_0_init_cache_regs(adev);
3313dff4cc4SHuang Rui
3323dff4cc4SHuang Rui gfxhub_v1_0_enable_system_domain(adev);
33308546895SZhigang Luo if (!amdgpu_sriov_vf(adev))
3343dff4cc4SHuang Rui gfxhub_v1_0_disable_identity_aperture(adev);
3353dff4cc4SHuang Rui gfxhub_v1_0_setup_vmid_config(adev);
3361e4eccdaSHuang Rui gfxhub_v1_0_program_invalidation(adev);
337e60f8db5SAlex Xie
338e60f8db5SAlex Xie return 0;
339e60f8db5SAlex Xie }
340e60f8db5SAlex Xie
gfxhub_v1_0_gart_disable(struct amdgpu_device * adev)3410d4d9512SHawking Zhang static void gfxhub_v1_0_gart_disable(struct amdgpu_device *adev)
342e60f8db5SAlex Xie {
343f4caf584SHawking Zhang struct amdgpu_vmhub *hub = &adev->vmhub[AMDGPU_GFXHUB(0)];
344e60f8db5SAlex Xie u32 tmp;
345e60f8db5SAlex Xie u32 i;
346e60f8db5SAlex Xie
347e60f8db5SAlex Xie /* Disable all tables */
348e60f8db5SAlex Xie for (i = 0; i < 16; i++)
3498c471360SHuang Rui WREG32_SOC15_OFFSET(GC, 0, mmVM_CONTEXT0_CNTL,
3508c471360SHuang Rui i * hub->ctx_distance, 0);
351e60f8db5SAlex Xie
352c4fc13b5SYuBiao Wang if (amdgpu_sriov_vf(adev))
353c4fc13b5SYuBiao Wang /* Avoid write to GMC registers */
354c4fc13b5SYuBiao Wang return;
355c4fc13b5SYuBiao Wang
356e60f8db5SAlex Xie /* Setup TLB control */
35789f99cebSHuang Rui tmp = RREG32_SOC15(GC, 0, mmMC_VM_MX_L1_TLB_CNTL);
358e60f8db5SAlex Xie tmp = REG_SET_FIELD(tmp, MC_VM_MX_L1_TLB_CNTL, ENABLE_L1_TLB, 0);
359e60f8db5SAlex Xie tmp = REG_SET_FIELD(tmp,
360e60f8db5SAlex Xie MC_VM_MX_L1_TLB_CNTL,
361e60f8db5SAlex Xie ENABLE_ADVANCED_DRIVER_MODEL,
362e60f8db5SAlex Xie 0);
3631bff7f6cSTrigger Huang WREG32_SOC15_RLC(GC, 0, mmMC_VM_MX_L1_TLB_CNTL, tmp);
364e60f8db5SAlex Xie
365e60f8db5SAlex Xie /* Setup L2 cache */
366805cb75cSTom St Denis WREG32_FIELD15(GC, 0, VM_L2_CNTL, ENABLE_L2_CACHE, 0);
36789f99cebSHuang Rui WREG32_SOC15(GC, 0, mmVM_L2_CNTL3, 0);
368e60f8db5SAlex Xie }
369e60f8db5SAlex Xie
370e60f8db5SAlex Xie /**
371e60f8db5SAlex Xie * gfxhub_v1_0_set_fault_enable_default - update GART/VM fault handling
372e60f8db5SAlex Xie *
373e60f8db5SAlex Xie * @adev: amdgpu_device pointer
374e60f8db5SAlex Xie * @value: true redirects VM faults to the default page
375e60f8db5SAlex Xie */
gfxhub_v1_0_set_fault_enable_default(struct amdgpu_device * adev,bool value)3760d4d9512SHawking Zhang static void gfxhub_v1_0_set_fault_enable_default(struct amdgpu_device *adev,
377e60f8db5SAlex Xie bool value)
378e60f8db5SAlex Xie {
379e60f8db5SAlex Xie u32 tmp;
38062e6771aSSrinivasan Shanmugam
38189f99cebSHuang Rui tmp = RREG32_SOC15(GC, 0, mmVM_L2_PROTECTION_FAULT_CNTL);
382e60f8db5SAlex Xie tmp = REG_SET_FIELD(tmp, VM_L2_PROTECTION_FAULT_CNTL,
383e60f8db5SAlex Xie RANGE_PROTECTION_FAULT_ENABLE_DEFAULT, value);
384e60f8db5SAlex Xie tmp = REG_SET_FIELD(tmp, VM_L2_PROTECTION_FAULT_CNTL,
385e60f8db5SAlex Xie PDE0_PROTECTION_FAULT_ENABLE_DEFAULT, value);
386e60f8db5SAlex Xie tmp = REG_SET_FIELD(tmp, VM_L2_PROTECTION_FAULT_CNTL,
387e60f8db5SAlex Xie PDE1_PROTECTION_FAULT_ENABLE_DEFAULT, value);
388e60f8db5SAlex Xie tmp = REG_SET_FIELD(tmp, VM_L2_PROTECTION_FAULT_CNTL,
389e60f8db5SAlex Xie PDE2_PROTECTION_FAULT_ENABLE_DEFAULT, value);
390e60f8db5SAlex Xie tmp = REG_SET_FIELD(tmp,
391e60f8db5SAlex Xie VM_L2_PROTECTION_FAULT_CNTL,
392e60f8db5SAlex Xie TRANSLATE_FURTHER_PROTECTION_FAULT_ENABLE_DEFAULT,
393e60f8db5SAlex Xie value);
394e60f8db5SAlex Xie tmp = REG_SET_FIELD(tmp, VM_L2_PROTECTION_FAULT_CNTL,
395e60f8db5SAlex Xie NACK_PROTECTION_FAULT_ENABLE_DEFAULT, value);
396e60f8db5SAlex Xie tmp = REG_SET_FIELD(tmp, VM_L2_PROTECTION_FAULT_CNTL,
397e60f8db5SAlex Xie DUMMY_PAGE_PROTECTION_FAULT_ENABLE_DEFAULT, value);
398e60f8db5SAlex Xie tmp = REG_SET_FIELD(tmp, VM_L2_PROTECTION_FAULT_CNTL,
399e60f8db5SAlex Xie VALID_PROTECTION_FAULT_ENABLE_DEFAULT, value);
400e60f8db5SAlex Xie tmp = REG_SET_FIELD(tmp, VM_L2_PROTECTION_FAULT_CNTL,
401e60f8db5SAlex Xie READ_PROTECTION_FAULT_ENABLE_DEFAULT, value);
402e60f8db5SAlex Xie tmp = REG_SET_FIELD(tmp, VM_L2_PROTECTION_FAULT_CNTL,
403e60f8db5SAlex Xie WRITE_PROTECTION_FAULT_ENABLE_DEFAULT, value);
404e60f8db5SAlex Xie tmp = REG_SET_FIELD(tmp, VM_L2_PROTECTION_FAULT_CNTL,
405e60f8db5SAlex Xie EXECUTE_PROTECTION_FAULT_ENABLE_DEFAULT, value);
4064bd9a67eSMonk Liu if (!value) {
4074bd9a67eSMonk Liu tmp = REG_SET_FIELD(tmp, VM_L2_PROTECTION_FAULT_CNTL,
4084bd9a67eSMonk Liu CRASH_ON_NO_RETRY_FAULT, 1);
4094bd9a67eSMonk Liu tmp = REG_SET_FIELD(tmp, VM_L2_PROTECTION_FAULT_CNTL,
4104bd9a67eSMonk Liu CRASH_ON_RETRY_FAULT, 1);
4114bd9a67eSMonk Liu }
41289f99cebSHuang Rui WREG32_SOC15(GC, 0, mmVM_L2_PROTECTION_FAULT_CNTL, tmp);
413e60f8db5SAlex Xie }
414e60f8db5SAlex Xie
gfxhub_v1_0_init(struct amdgpu_device * adev)4150d4d9512SHawking Zhang static void gfxhub_v1_0_init(struct amdgpu_device *adev)
416e60f8db5SAlex Xie {
417f4caf584SHawking Zhang struct amdgpu_vmhub *hub = &adev->vmhub[AMDGPU_GFXHUB(0)];
418e60f8db5SAlex Xie
419e60f8db5SAlex Xie hub->ctx0_ptb_addr_lo32 =
420e60f8db5SAlex Xie SOC15_REG_OFFSET(GC, 0,
421e60f8db5SAlex Xie mmVM_CONTEXT0_PAGE_TABLE_BASE_ADDR_LO32);
422e60f8db5SAlex Xie hub->ctx0_ptb_addr_hi32 =
423e60f8db5SAlex Xie SOC15_REG_OFFSET(GC, 0,
424e60f8db5SAlex Xie mmVM_CONTEXT0_PAGE_TABLE_BASE_ADDR_HI32);
4256c2c8972Schangzhu hub->vm_inv_eng0_sem =
4266c2c8972Schangzhu SOC15_REG_OFFSET(GC, 0, mmVM_INVALIDATE_ENG0_SEM);
427e60f8db5SAlex Xie hub->vm_inv_eng0_req =
428e60f8db5SAlex Xie SOC15_REG_OFFSET(GC, 0, mmVM_INVALIDATE_ENG0_REQ);
429e60f8db5SAlex Xie hub->vm_inv_eng0_ack =
430e60f8db5SAlex Xie SOC15_REG_OFFSET(GC, 0, mmVM_INVALIDATE_ENG0_ACK);
431e60f8db5SAlex Xie hub->vm_context0_cntl =
432e60f8db5SAlex Xie SOC15_REG_OFFSET(GC, 0, mmVM_CONTEXT0_CNTL);
433e60f8db5SAlex Xie hub->vm_l2_pro_fault_status =
434e60f8db5SAlex Xie SOC15_REG_OFFSET(GC, 0, mmVM_L2_PROTECTION_FAULT_STATUS);
435e60f8db5SAlex Xie hub->vm_l2_pro_fault_cntl =
436e60f8db5SAlex Xie SOC15_REG_OFFSET(GC, 0, mmVM_L2_PROTECTION_FAULT_CNTL);
4371f9d56c3SHuang Rui
4381f9d56c3SHuang Rui hub->ctx_distance = mmVM_CONTEXT1_CNTL - mmVM_CONTEXT0_CNTL;
4391f9d56c3SHuang Rui hub->ctx_addr_distance = mmVM_CONTEXT1_PAGE_TABLE_BASE_ADDR_LO32 -
4401f9d56c3SHuang Rui mmVM_CONTEXT0_PAGE_TABLE_BASE_ADDR_LO32;
4411f9d56c3SHuang Rui hub->eng_distance = mmVM_INVALIDATE_ENG1_REQ - mmVM_INVALIDATE_ENG0_REQ;
4421f9d56c3SHuang Rui hub->eng_addr_distance = mmVM_INVALIDATE_ENG1_ADDR_RANGE_LO32 -
4431f9d56c3SHuang Rui mmVM_INVALIDATE_ENG0_ADDR_RANGE_LO32;
4440c8c0847SHuang Rui }
4458ffff9b4SOak Zeng
4468ffff9b4SOak Zeng
4478ffff9b4SOak Zeng const struct amdgpu_gfxhub_funcs gfxhub_v1_0_funcs = {
4488ffff9b4SOak Zeng .get_mc_fb_offset = gfxhub_v1_0_get_mc_fb_offset,
4498ffff9b4SOak Zeng .setup_vm_pt_regs = gfxhub_v1_0_setup_vm_pt_regs,
4508ffff9b4SOak Zeng .gart_enable = gfxhub_v1_0_gart_enable,
4518ffff9b4SOak Zeng .gart_disable = gfxhub_v1_0_gart_disable,
4528ffff9b4SOak Zeng .set_fault_enable_default = gfxhub_v1_0_set_fault_enable_default,
4538ffff9b4SOak Zeng .init = gfxhub_v1_0_init,
45421470d97SKevin Wang .get_xgmi_info = gfxhub_v1_1_get_xgmi_info,
4558ffff9b4SOak Zeng };
456