1 /*
2  * Copyright 2019 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  */
23 
24 #include "amdgpu.h"
25 #include "mmhub_v2_0.h"
26 
27 #include "mmhub/mmhub_2_0_0_offset.h"
28 #include "mmhub/mmhub_2_0_0_sh_mask.h"
29 #include "mmhub/mmhub_2_0_0_default.h"
30 #include "navi10_enum.h"
31 
32 #include "soc15_common.h"
33 
34 #define mmMM_ATC_L2_MISC_CG_Sienna_Cichlid                      0x064d
35 #define mmMM_ATC_L2_MISC_CG_Sienna_Cichlid_BASE_IDX             0
36 #define mmDAGB0_CNTL_MISC2_Sienna_Cichlid                       0x0070
37 #define mmDAGB0_CNTL_MISC2_Sienna_Cichlid_BASE_IDX              0
38 
39 static const char *mmhub_client_ids_navi1x[][2] = {
40 	[3][0] = "DCEDMC",
41 	[4][0] = "DCEVGA",
42 	[5][0] = "MP0",
43 	[6][0] = "MP1",
44 	[13][0] = "VMC",
45 	[14][0] = "HDP",
46 	[15][0] = "OSS",
47 	[16][0] = "VCNU",
48 	[17][0] = "JPEG",
49 	[18][0] = "VCN",
50 	[3][1] = "DCEDMC",
51 	[4][1] = "DCEXFC",
52 	[5][1] = "DCEVGA",
53 	[6][1] = "DCEDWB",
54 	[7][1] = "MP0",
55 	[8][1] = "MP1",
56 	[9][1] = "DBGU1",
57 	[10][1] = "DBGU0",
58 	[11][1] = "XDP",
59 	[14][1] = "HDP",
60 	[15][1] = "OSS",
61 	[16][1] = "VCNU",
62 	[17][1] = "JPEG",
63 	[18][1] = "VCN",
64 };
65 
66 static const char *mmhub_client_ids_sienna_cichlid[][2] = {
67 	[3][0] = "DCEDMC",
68 	[4][0] = "DCEVGA",
69 	[5][0] = "MP0",
70 	[6][0] = "MP1",
71 	[8][0] = "VMC",
72 	[9][0] = "VCNU0",
73 	[10][0] = "JPEG",
74 	[12][0] = "VCNU1",
75 	[13][0] = "VCN1",
76 	[14][0] = "HDP",
77 	[15][0] = "OSS",
78 	[32+11][0] = "VCN0",
79 	[0][1] = "DBGU0",
80 	[1][1] = "DBGU1",
81 	[2][1] = "DCEDWB",
82 	[3][1] = "DCEDMC",
83 	[4][1] = "DCEVGA",
84 	[5][1] = "MP0",
85 	[6][1] = "MP1",
86 	[7][1] = "XDP",
87 	[9][1] = "VCNU0",
88 	[10][1] = "JPEG",
89 	[11][1] = "VCN0",
90 	[12][1] = "VCNU1",
91 	[13][1] = "VCN1",
92 	[14][1] = "HDP",
93 	[15][1] = "OSS",
94 };
95 
96 static uint32_t mmhub_v2_0_get_invalidate_req(unsigned int vmid,
97 					      uint32_t flush_type)
98 {
99 	u32 req = 0;
100 
101 	/* invalidate using legacy mode on vmid*/
102 	req = REG_SET_FIELD(req, MMVM_INVALIDATE_ENG0_REQ,
103 			    PER_VMID_INVALIDATE_REQ, 1 << vmid);
104 	req = REG_SET_FIELD(req, MMVM_INVALIDATE_ENG0_REQ, FLUSH_TYPE, flush_type);
105 	req = REG_SET_FIELD(req, MMVM_INVALIDATE_ENG0_REQ, INVALIDATE_L2_PTES, 1);
106 	req = REG_SET_FIELD(req, MMVM_INVALIDATE_ENG0_REQ, INVALIDATE_L2_PDE0, 1);
107 	req = REG_SET_FIELD(req, MMVM_INVALIDATE_ENG0_REQ, INVALIDATE_L2_PDE1, 1);
108 	req = REG_SET_FIELD(req, MMVM_INVALIDATE_ENG0_REQ, INVALIDATE_L2_PDE2, 1);
109 	req = REG_SET_FIELD(req, MMVM_INVALIDATE_ENG0_REQ, INVALIDATE_L1_PTES, 1);
110 	req = REG_SET_FIELD(req, MMVM_INVALIDATE_ENG0_REQ,
111 			    CLEAR_PROTECTION_FAULT_STATUS_ADDR,	0);
112 
113 	return req;
114 }
115 
116 static void
117 mmhub_v2_0_print_l2_protection_fault_status(struct amdgpu_device *adev,
118 					     uint32_t status)
119 {
120 	uint32_t cid, rw;
121 	const char *mmhub_cid = NULL;
122 
123 	cid = REG_GET_FIELD(status,
124 			    MMVM_L2_PROTECTION_FAULT_STATUS, CID);
125 	rw = REG_GET_FIELD(status,
126 			   MMVM_L2_PROTECTION_FAULT_STATUS, RW);
127 
128 	dev_err(adev->dev,
129 		"MMVM_L2_PROTECTION_FAULT_STATUS:0x%08X\n",
130 		status);
131 	switch (adev->asic_type) {
132 	case CHIP_NAVI10:
133 	case CHIP_NAVI12:
134 	case CHIP_NAVI14:
135 		mmhub_cid = mmhub_client_ids_navi1x[cid][rw];
136 		break;
137 	case CHIP_SIENNA_CICHLID:
138 	case CHIP_NAVY_FLOUNDER:
139 	case CHIP_DIMGREY_CAVEFISH:
140 		mmhub_cid = mmhub_client_ids_sienna_cichlid[cid][rw];
141 		break;
142 	default:
143 		mmhub_cid = NULL;
144 		break;
145 	}
146 	dev_err(adev->dev, "\t Faulty UTCL2 client ID: %s (0x%x)\n",
147 		mmhub_cid ? mmhub_cid : "unknown", cid);
148 	dev_err(adev->dev, "\t MORE_FAULTS: 0x%lx\n",
149 		REG_GET_FIELD(status,
150 		MMVM_L2_PROTECTION_FAULT_STATUS, MORE_FAULTS));
151 	dev_err(adev->dev, "\t WALKER_ERROR: 0x%lx\n",
152 		REG_GET_FIELD(status,
153 		MMVM_L2_PROTECTION_FAULT_STATUS, WALKER_ERROR));
154 	dev_err(adev->dev, "\t PERMISSION_FAULTS: 0x%lx\n",
155 		REG_GET_FIELD(status,
156 		MMVM_L2_PROTECTION_FAULT_STATUS, PERMISSION_FAULTS));
157 	dev_err(adev->dev, "\t MAPPING_ERROR: 0x%lx\n",
158 		REG_GET_FIELD(status,
159 		MMVM_L2_PROTECTION_FAULT_STATUS, MAPPING_ERROR));
160 	dev_err(adev->dev, "\t RW: 0x%x\n", rw);
161 }
162 
163 static void mmhub_v2_0_setup_vm_pt_regs(struct amdgpu_device *adev, uint32_t vmid,
164 				uint64_t page_table_base)
165 {
166 	struct amdgpu_vmhub *hub = &adev->vmhub[AMDGPU_MMHUB_0];
167 
168 	WREG32_SOC15_OFFSET(MMHUB, 0, mmMMVM_CONTEXT0_PAGE_TABLE_BASE_ADDR_LO32,
169 			    hub->ctx_addr_distance * vmid,
170 			    lower_32_bits(page_table_base));
171 
172 	WREG32_SOC15_OFFSET(MMHUB, 0, mmMMVM_CONTEXT0_PAGE_TABLE_BASE_ADDR_HI32,
173 			    hub->ctx_addr_distance * vmid,
174 			    upper_32_bits(page_table_base));
175 }
176 
177 static void mmhub_v2_0_init_gart_aperture_regs(struct amdgpu_device *adev)
178 {
179 	uint64_t pt_base = amdgpu_gmc_pd_addr(adev->gart.bo);
180 
181 	mmhub_v2_0_setup_vm_pt_regs(adev, 0, pt_base);
182 
183 	WREG32_SOC15(MMHUB, 0, mmMMVM_CONTEXT0_PAGE_TABLE_START_ADDR_LO32,
184 		     (u32)(adev->gmc.gart_start >> 12));
185 	WREG32_SOC15(MMHUB, 0, mmMMVM_CONTEXT0_PAGE_TABLE_START_ADDR_HI32,
186 		     (u32)(adev->gmc.gart_start >> 44));
187 
188 	WREG32_SOC15(MMHUB, 0, mmMMVM_CONTEXT0_PAGE_TABLE_END_ADDR_LO32,
189 		     (u32)(adev->gmc.gart_end >> 12));
190 	WREG32_SOC15(MMHUB, 0, mmMMVM_CONTEXT0_PAGE_TABLE_END_ADDR_HI32,
191 		     (u32)(adev->gmc.gart_end >> 44));
192 }
193 
194 static void mmhub_v2_0_init_system_aperture_regs(struct amdgpu_device *adev)
195 {
196 	uint64_t value;
197 	uint32_t tmp;
198 
199 	/* Program the AGP BAR */
200 	WREG32_SOC15(MMHUB, 0, mmMMMC_VM_AGP_BASE, 0);
201 	WREG32_SOC15(MMHUB, 0, mmMMMC_VM_AGP_BOT, adev->gmc.agp_start >> 24);
202 	WREG32_SOC15(MMHUB, 0, mmMMMC_VM_AGP_TOP, adev->gmc.agp_end >> 24);
203 
204 	if (!amdgpu_sriov_vf(adev)) {
205 		/* Program the system aperture low logical page number. */
206 		WREG32_SOC15(MMHUB, 0, mmMMMC_VM_SYSTEM_APERTURE_LOW_ADDR,
207 			     min(adev->gmc.fb_start, adev->gmc.agp_start) >> 18);
208 		WREG32_SOC15(MMHUB, 0, mmMMMC_VM_SYSTEM_APERTURE_HIGH_ADDR,
209 			     max(adev->gmc.fb_end, adev->gmc.agp_end) >> 18);
210 	}
211 
212 	/* Set default page address. */
213 	value = amdgpu_gmc_vram_mc2pa(adev, adev->vram_scratch.gpu_addr);
214 	WREG32_SOC15(MMHUB, 0, mmMMMC_VM_SYSTEM_APERTURE_DEFAULT_ADDR_LSB,
215 		     (u32)(value >> 12));
216 	WREG32_SOC15(MMHUB, 0, mmMMMC_VM_SYSTEM_APERTURE_DEFAULT_ADDR_MSB,
217 		     (u32)(value >> 44));
218 
219 	/* Program "protection fault". */
220 	WREG32_SOC15(MMHUB, 0, mmMMVM_L2_PROTECTION_FAULT_DEFAULT_ADDR_LO32,
221 		     (u32)(adev->dummy_page_addr >> 12));
222 	WREG32_SOC15(MMHUB, 0, mmMMVM_L2_PROTECTION_FAULT_DEFAULT_ADDR_HI32,
223 		     (u32)((u64)adev->dummy_page_addr >> 44));
224 
225 	tmp = RREG32_SOC15(MMHUB, 0, mmMMVM_L2_PROTECTION_FAULT_CNTL2);
226 	tmp = REG_SET_FIELD(tmp, MMVM_L2_PROTECTION_FAULT_CNTL2,
227 			    ACTIVE_PAGE_MIGRATION_PTE_READ_RETRY, 1);
228 	WREG32_SOC15(MMHUB, 0, mmMMVM_L2_PROTECTION_FAULT_CNTL2, tmp);
229 }
230 
231 static void mmhub_v2_0_init_tlb_regs(struct amdgpu_device *adev)
232 {
233 	uint32_t tmp;
234 
235 	/* Setup TLB control */
236 	tmp = RREG32_SOC15(MMHUB, 0, mmMMMC_VM_MX_L1_TLB_CNTL);
237 
238 	tmp = REG_SET_FIELD(tmp, MMMC_VM_MX_L1_TLB_CNTL, ENABLE_L1_TLB, 1);
239 	tmp = REG_SET_FIELD(tmp, MMMC_VM_MX_L1_TLB_CNTL, SYSTEM_ACCESS_MODE, 3);
240 	tmp = REG_SET_FIELD(tmp, MMMC_VM_MX_L1_TLB_CNTL,
241 			    ENABLE_ADVANCED_DRIVER_MODEL, 1);
242 	tmp = REG_SET_FIELD(tmp, MMMC_VM_MX_L1_TLB_CNTL,
243 			    SYSTEM_APERTURE_UNMAPPED_ACCESS, 0);
244 	tmp = REG_SET_FIELD(tmp, MMMC_VM_MX_L1_TLB_CNTL, ECO_BITS, 0);
245 	tmp = REG_SET_FIELD(tmp, MMMC_VM_MX_L1_TLB_CNTL,
246 			    MTYPE, MTYPE_UC); /* UC, uncached */
247 
248 	WREG32_SOC15(MMHUB, 0, mmMMMC_VM_MX_L1_TLB_CNTL, tmp);
249 }
250 
251 static void mmhub_v2_0_init_cache_regs(struct amdgpu_device *adev)
252 {
253 	uint32_t tmp;
254 
255 	/* These registers are not accessible to VF-SRIOV.
256 	 * The PF will program them instead.
257 	 */
258 	if (amdgpu_sriov_vf(adev))
259 		return;
260 
261 	/* Setup L2 cache */
262 	tmp = RREG32_SOC15(MMHUB, 0, mmMMVM_L2_CNTL);
263 	tmp = REG_SET_FIELD(tmp, MMVM_L2_CNTL, ENABLE_L2_CACHE, 1);
264 	tmp = REG_SET_FIELD(tmp, MMVM_L2_CNTL, ENABLE_L2_FRAGMENT_PROCESSING, 0);
265 	tmp = REG_SET_FIELD(tmp, MMVM_L2_CNTL,
266 			    ENABLE_DEFAULT_PAGE_OUT_TO_SYSTEM_MEMORY, 1);
267 	/* XXX for emulation, Refer to closed source code.*/
268 	tmp = REG_SET_FIELD(tmp, MMVM_L2_CNTL, L2_PDE0_CACHE_TAG_GENERATION_MODE,
269 			    0);
270 	tmp = REG_SET_FIELD(tmp, MMVM_L2_CNTL, PDE_FAULT_CLASSIFICATION, 0);
271 	tmp = REG_SET_FIELD(tmp, MMVM_L2_CNTL, CONTEXT1_IDENTITY_ACCESS_MODE, 1);
272 	tmp = REG_SET_FIELD(tmp, MMVM_L2_CNTL, IDENTITY_MODE_FRAGMENT_SIZE, 0);
273 	WREG32_SOC15(MMHUB, 0, mmMMVM_L2_CNTL, tmp);
274 
275 	tmp = RREG32_SOC15(MMHUB, 0, mmMMVM_L2_CNTL2);
276 	tmp = REG_SET_FIELD(tmp, MMVM_L2_CNTL2, INVALIDATE_ALL_L1_TLBS, 1);
277 	tmp = REG_SET_FIELD(tmp, MMVM_L2_CNTL2, INVALIDATE_L2_CACHE, 1);
278 	WREG32_SOC15(MMHUB, 0, mmMMVM_L2_CNTL2, tmp);
279 
280 	tmp = mmMMVM_L2_CNTL3_DEFAULT;
281 	if (adev->gmc.translate_further) {
282 		tmp = REG_SET_FIELD(tmp, MMVM_L2_CNTL3, BANK_SELECT, 12);
283 		tmp = REG_SET_FIELD(tmp, MMVM_L2_CNTL3,
284 				    L2_CACHE_BIGK_FRAGMENT_SIZE, 9);
285 	} else {
286 		tmp = REG_SET_FIELD(tmp, MMVM_L2_CNTL3, BANK_SELECT, 9);
287 		tmp = REG_SET_FIELD(tmp, MMVM_L2_CNTL3,
288 				    L2_CACHE_BIGK_FRAGMENT_SIZE, 6);
289 	}
290 	WREG32_SOC15(MMHUB, 0, mmMMVM_L2_CNTL3, tmp);
291 
292 	tmp = mmMMVM_L2_CNTL4_DEFAULT;
293 	tmp = REG_SET_FIELD(tmp, MMVM_L2_CNTL4, VMC_TAP_PDE_REQUEST_PHYSICAL, 0);
294 	tmp = REG_SET_FIELD(tmp, MMVM_L2_CNTL4, VMC_TAP_PTE_REQUEST_PHYSICAL, 0);
295 	WREG32_SOC15(MMHUB, 0, mmMMVM_L2_CNTL4, tmp);
296 
297 	tmp = mmMMVM_L2_CNTL5_DEFAULT;
298 	tmp = REG_SET_FIELD(tmp, MMVM_L2_CNTL5, L2_CACHE_SMALLK_FRAGMENT_SIZE, 0);
299 	WREG32_SOC15(GC, 0, mmMMVM_L2_CNTL5, tmp);
300 }
301 
302 static void mmhub_v2_0_enable_system_domain(struct amdgpu_device *adev)
303 {
304 	uint32_t tmp;
305 
306 	tmp = RREG32_SOC15(MMHUB, 0, mmMMVM_CONTEXT0_CNTL);
307 	tmp = REG_SET_FIELD(tmp, MMVM_CONTEXT0_CNTL, ENABLE_CONTEXT, 1);
308 	tmp = REG_SET_FIELD(tmp, MMVM_CONTEXT0_CNTL, PAGE_TABLE_DEPTH, 0);
309 	tmp = REG_SET_FIELD(tmp, MMVM_CONTEXT0_CNTL,
310 			    RETRY_PERMISSION_OR_INVALID_PAGE_FAULT, 0);
311 	WREG32_SOC15(MMHUB, 0, mmMMVM_CONTEXT0_CNTL, tmp);
312 }
313 
314 static void mmhub_v2_0_disable_identity_aperture(struct amdgpu_device *adev)
315 {
316 	/* These registers are not accessible to VF-SRIOV.
317 	 * The PF will program them instead.
318 	 */
319 	if (amdgpu_sriov_vf(adev))
320 		return;
321 
322 	WREG32_SOC15(MMHUB, 0,
323 		     mmMMVM_L2_CONTEXT1_IDENTITY_APERTURE_LOW_ADDR_LO32,
324 		     0xFFFFFFFF);
325 	WREG32_SOC15(MMHUB, 0,
326 		     mmMMVM_L2_CONTEXT1_IDENTITY_APERTURE_LOW_ADDR_HI32,
327 		     0x0000000F);
328 
329 	WREG32_SOC15(MMHUB, 0,
330 		     mmMMVM_L2_CONTEXT1_IDENTITY_APERTURE_HIGH_ADDR_LO32, 0);
331 	WREG32_SOC15(MMHUB, 0,
332 		     mmMMVM_L2_CONTEXT1_IDENTITY_APERTURE_HIGH_ADDR_HI32, 0);
333 
334 	WREG32_SOC15(MMHUB, 0, mmMMVM_L2_CONTEXT_IDENTITY_PHYSICAL_OFFSET_LO32,
335 		     0);
336 	WREG32_SOC15(MMHUB, 0, mmMMVM_L2_CONTEXT_IDENTITY_PHYSICAL_OFFSET_HI32,
337 		     0);
338 }
339 
340 static void mmhub_v2_0_setup_vmid_config(struct amdgpu_device *adev)
341 {
342 	struct amdgpu_vmhub *hub = &adev->vmhub[AMDGPU_MMHUB_0];
343 	int i;
344 	uint32_t tmp;
345 
346 	for (i = 0; i <= 14; i++) {
347 		tmp = RREG32_SOC15_OFFSET(MMHUB, 0, mmMMVM_CONTEXT1_CNTL, i);
348 		tmp = REG_SET_FIELD(tmp, MMVM_CONTEXT1_CNTL, ENABLE_CONTEXT, 1);
349 		tmp = REG_SET_FIELD(tmp, MMVM_CONTEXT1_CNTL, PAGE_TABLE_DEPTH,
350 				    adev->vm_manager.num_level);
351 		tmp = REG_SET_FIELD(tmp, MMVM_CONTEXT1_CNTL,
352 				    RANGE_PROTECTION_FAULT_ENABLE_DEFAULT, 1);
353 		tmp = REG_SET_FIELD(tmp, MMVM_CONTEXT1_CNTL,
354 				    DUMMY_PAGE_PROTECTION_FAULT_ENABLE_DEFAULT,
355 				    1);
356 		tmp = REG_SET_FIELD(tmp, MMVM_CONTEXT1_CNTL,
357 				    PDE0_PROTECTION_FAULT_ENABLE_DEFAULT, 1);
358 		tmp = REG_SET_FIELD(tmp, MMVM_CONTEXT1_CNTL,
359 				    VALID_PROTECTION_FAULT_ENABLE_DEFAULT, 1);
360 		tmp = REG_SET_FIELD(tmp, MMVM_CONTEXT1_CNTL,
361 				    READ_PROTECTION_FAULT_ENABLE_DEFAULT, 1);
362 		tmp = REG_SET_FIELD(tmp, MMVM_CONTEXT1_CNTL,
363 				    WRITE_PROTECTION_FAULT_ENABLE_DEFAULT, 1);
364 		tmp = REG_SET_FIELD(tmp, MMVM_CONTEXT1_CNTL,
365 				    EXECUTE_PROTECTION_FAULT_ENABLE_DEFAULT, 1);
366 		tmp = REG_SET_FIELD(tmp, MMVM_CONTEXT1_CNTL,
367 				    PAGE_TABLE_BLOCK_SIZE,
368 				    adev->vm_manager.block_size - 9);
369 		/* Send no-retry XNACK on fault to suppress VM fault storm. */
370 		tmp = REG_SET_FIELD(tmp, MMVM_CONTEXT1_CNTL,
371 				    RETRY_PERMISSION_OR_INVALID_PAGE_FAULT,
372 				    !adev->gmc.noretry);
373 		WREG32_SOC15_OFFSET(MMHUB, 0, mmMMVM_CONTEXT1_CNTL,
374 				    i * hub->ctx_distance, tmp);
375 		WREG32_SOC15_OFFSET(MMHUB, 0, mmMMVM_CONTEXT1_PAGE_TABLE_START_ADDR_LO32,
376 				    i * hub->ctx_addr_distance, 0);
377 		WREG32_SOC15_OFFSET(MMHUB, 0, mmMMVM_CONTEXT1_PAGE_TABLE_START_ADDR_HI32,
378 				    i * hub->ctx_addr_distance, 0);
379 		WREG32_SOC15_OFFSET(MMHUB, 0, mmMMVM_CONTEXT1_PAGE_TABLE_END_ADDR_LO32,
380 				    i * hub->ctx_addr_distance,
381 				    lower_32_bits(adev->vm_manager.max_pfn - 1));
382 		WREG32_SOC15_OFFSET(MMHUB, 0, mmMMVM_CONTEXT1_PAGE_TABLE_END_ADDR_HI32,
383 				    i * hub->ctx_addr_distance,
384 				    upper_32_bits(adev->vm_manager.max_pfn - 1));
385 	}
386 }
387 
388 static void mmhub_v2_0_program_invalidation(struct amdgpu_device *adev)
389 {
390 	struct amdgpu_vmhub *hub = &adev->vmhub[AMDGPU_MMHUB_0];
391 	unsigned i;
392 
393 	for (i = 0; i < 18; ++i) {
394 		WREG32_SOC15_OFFSET(MMHUB, 0, mmMMVM_INVALIDATE_ENG0_ADDR_RANGE_LO32,
395 				    i * hub->eng_addr_distance, 0xffffffff);
396 		WREG32_SOC15_OFFSET(MMHUB, 0, mmMMVM_INVALIDATE_ENG0_ADDR_RANGE_HI32,
397 				    i * hub->eng_addr_distance, 0x1f);
398 	}
399 }
400 
401 static int mmhub_v2_0_gart_enable(struct amdgpu_device *adev)
402 {
403 	/* GART Enable. */
404 	mmhub_v2_0_init_gart_aperture_regs(adev);
405 	mmhub_v2_0_init_system_aperture_regs(adev);
406 	mmhub_v2_0_init_tlb_regs(adev);
407 	mmhub_v2_0_init_cache_regs(adev);
408 
409 	mmhub_v2_0_enable_system_domain(adev);
410 	mmhub_v2_0_disable_identity_aperture(adev);
411 	mmhub_v2_0_setup_vmid_config(adev);
412 	mmhub_v2_0_program_invalidation(adev);
413 
414 	return 0;
415 }
416 
417 static void mmhub_v2_0_gart_disable(struct amdgpu_device *adev)
418 {
419 	struct amdgpu_vmhub *hub = &adev->vmhub[AMDGPU_MMHUB_0];
420 	u32 tmp;
421 	u32 i;
422 
423 	/* Disable all tables */
424 	for (i = 0; i < AMDGPU_NUM_VMID; i++)
425 		WREG32_SOC15_OFFSET(MMHUB, 0, mmMMVM_CONTEXT0_CNTL,
426 				    i * hub->ctx_distance, 0);
427 
428 	/* Setup TLB control */
429 	tmp = RREG32_SOC15(MMHUB, 0, mmMMMC_VM_MX_L1_TLB_CNTL);
430 	tmp = REG_SET_FIELD(tmp, MMMC_VM_MX_L1_TLB_CNTL, ENABLE_L1_TLB, 0);
431 	tmp = REG_SET_FIELD(tmp, MMMC_VM_MX_L1_TLB_CNTL,
432 			    ENABLE_ADVANCED_DRIVER_MODEL, 0);
433 	WREG32_SOC15(MMHUB, 0, mmMMMC_VM_MX_L1_TLB_CNTL, tmp);
434 
435 	/* Setup L2 cache */
436 	tmp = RREG32_SOC15(MMHUB, 0, mmMMVM_L2_CNTL);
437 	tmp = REG_SET_FIELD(tmp, MMVM_L2_CNTL, ENABLE_L2_CACHE, 0);
438 	WREG32_SOC15(MMHUB, 0, mmMMVM_L2_CNTL, tmp);
439 	WREG32_SOC15(MMHUB, 0, mmMMVM_L2_CNTL3, 0);
440 }
441 
442 /**
443  * mmhub_v2_0_set_fault_enable_default - update GART/VM fault handling
444  *
445  * @adev: amdgpu_device pointer
446  * @value: true redirects VM faults to the default page
447  */
448 static void mmhub_v2_0_set_fault_enable_default(struct amdgpu_device *adev, bool value)
449 {
450 	u32 tmp;
451 
452 	/* These registers are not accessible to VF-SRIOV.
453 	 * The PF will program them instead.
454 	 */
455 	if (amdgpu_sriov_vf(adev))
456 		return;
457 
458 	tmp = RREG32_SOC15(MMHUB, 0, mmMMVM_L2_PROTECTION_FAULT_CNTL);
459 	tmp = REG_SET_FIELD(tmp, MMVM_L2_PROTECTION_FAULT_CNTL,
460 			    RANGE_PROTECTION_FAULT_ENABLE_DEFAULT, value);
461 	tmp = REG_SET_FIELD(tmp, MMVM_L2_PROTECTION_FAULT_CNTL,
462 			    PDE0_PROTECTION_FAULT_ENABLE_DEFAULT, value);
463 	tmp = REG_SET_FIELD(tmp, MMVM_L2_PROTECTION_FAULT_CNTL,
464 			    PDE1_PROTECTION_FAULT_ENABLE_DEFAULT, value);
465 	tmp = REG_SET_FIELD(tmp, MMVM_L2_PROTECTION_FAULT_CNTL,
466 			    PDE2_PROTECTION_FAULT_ENABLE_DEFAULT, value);
467 	tmp = REG_SET_FIELD(tmp, MMVM_L2_PROTECTION_FAULT_CNTL,
468 			    TRANSLATE_FURTHER_PROTECTION_FAULT_ENABLE_DEFAULT,
469 			    value);
470 	tmp = REG_SET_FIELD(tmp, MMVM_L2_PROTECTION_FAULT_CNTL,
471 			    NACK_PROTECTION_FAULT_ENABLE_DEFAULT, value);
472 	tmp = REG_SET_FIELD(tmp, MMVM_L2_PROTECTION_FAULT_CNTL,
473 			    DUMMY_PAGE_PROTECTION_FAULT_ENABLE_DEFAULT, value);
474 	tmp = REG_SET_FIELD(tmp, MMVM_L2_PROTECTION_FAULT_CNTL,
475 			    VALID_PROTECTION_FAULT_ENABLE_DEFAULT, value);
476 	tmp = REG_SET_FIELD(tmp, MMVM_L2_PROTECTION_FAULT_CNTL,
477 			    READ_PROTECTION_FAULT_ENABLE_DEFAULT, value);
478 	tmp = REG_SET_FIELD(tmp, MMVM_L2_PROTECTION_FAULT_CNTL,
479 			    WRITE_PROTECTION_FAULT_ENABLE_DEFAULT, value);
480 	tmp = REG_SET_FIELD(tmp, MMVM_L2_PROTECTION_FAULT_CNTL,
481 			    EXECUTE_PROTECTION_FAULT_ENABLE_DEFAULT, value);
482 	if (!value) {
483 		tmp = REG_SET_FIELD(tmp, MMVM_L2_PROTECTION_FAULT_CNTL,
484 				CRASH_ON_NO_RETRY_FAULT, 1);
485 		tmp = REG_SET_FIELD(tmp, MMVM_L2_PROTECTION_FAULT_CNTL,
486 				CRASH_ON_RETRY_FAULT, 1);
487 	}
488 	WREG32_SOC15(MMHUB, 0, mmMMVM_L2_PROTECTION_FAULT_CNTL, tmp);
489 }
490 
491 static const struct amdgpu_vmhub_funcs mmhub_v2_0_vmhub_funcs = {
492 	.print_l2_protection_fault_status = mmhub_v2_0_print_l2_protection_fault_status,
493 	.get_invalidate_req = mmhub_v2_0_get_invalidate_req,
494 };
495 
496 static void mmhub_v2_0_init(struct amdgpu_device *adev)
497 {
498 	struct amdgpu_vmhub *hub = &adev->vmhub[AMDGPU_MMHUB_0];
499 
500 	hub->ctx0_ptb_addr_lo32 =
501 		SOC15_REG_OFFSET(MMHUB, 0,
502 				 mmMMVM_CONTEXT0_PAGE_TABLE_BASE_ADDR_LO32);
503 	hub->ctx0_ptb_addr_hi32 =
504 		SOC15_REG_OFFSET(MMHUB, 0,
505 				 mmMMVM_CONTEXT0_PAGE_TABLE_BASE_ADDR_HI32);
506 	hub->vm_inv_eng0_sem =
507 		SOC15_REG_OFFSET(MMHUB, 0, mmMMVM_INVALIDATE_ENG0_SEM);
508 	hub->vm_inv_eng0_req =
509 		SOC15_REG_OFFSET(MMHUB, 0, mmMMVM_INVALIDATE_ENG0_REQ);
510 	hub->vm_inv_eng0_ack =
511 		SOC15_REG_OFFSET(MMHUB, 0, mmMMVM_INVALIDATE_ENG0_ACK);
512 	hub->vm_context0_cntl =
513 		SOC15_REG_OFFSET(MMHUB, 0, mmMMVM_CONTEXT0_CNTL);
514 	hub->vm_l2_pro_fault_status =
515 		SOC15_REG_OFFSET(MMHUB, 0, mmMMVM_L2_PROTECTION_FAULT_STATUS);
516 	hub->vm_l2_pro_fault_cntl =
517 		SOC15_REG_OFFSET(MMHUB, 0, mmMMVM_L2_PROTECTION_FAULT_CNTL);
518 
519 	hub->ctx_distance = mmMMVM_CONTEXT1_CNTL - mmMMVM_CONTEXT0_CNTL;
520 	hub->ctx_addr_distance = mmMMVM_CONTEXT1_PAGE_TABLE_BASE_ADDR_LO32 -
521 		mmMMVM_CONTEXT0_PAGE_TABLE_BASE_ADDR_LO32;
522 	hub->eng_distance = mmMMVM_INVALIDATE_ENG1_REQ -
523 		mmMMVM_INVALIDATE_ENG0_REQ;
524 	hub->eng_addr_distance = mmMMVM_INVALIDATE_ENG1_ADDR_RANGE_LO32 -
525 		mmMMVM_INVALIDATE_ENG0_ADDR_RANGE_LO32;
526 
527 	hub->vm_cntx_cntl_vm_fault = MMVM_CONTEXT1_CNTL__RANGE_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
528 		MMVM_CONTEXT1_CNTL__DUMMY_PAGE_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
529 		MMVM_CONTEXT1_CNTL__PDE0_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
530 		MMVM_CONTEXT1_CNTL__VALID_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
531 		MMVM_CONTEXT1_CNTL__READ_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
532 		MMVM_CONTEXT1_CNTL__WRITE_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
533 		MMVM_CONTEXT1_CNTL__EXECUTE_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK;
534 
535 	hub->vmhub_funcs = &mmhub_v2_0_vmhub_funcs;
536 }
537 
538 static void mmhub_v2_0_update_medium_grain_clock_gating(struct amdgpu_device *adev,
539 							bool enable)
540 {
541 	uint32_t def, data, def1, data1;
542 
543 	switch (adev->asic_type) {
544 	case CHIP_SIENNA_CICHLID:
545 	case CHIP_NAVY_FLOUNDER:
546 	case CHIP_DIMGREY_CAVEFISH:
547 		def  = data  = RREG32_SOC15(MMHUB, 0, mmMM_ATC_L2_MISC_CG_Sienna_Cichlid);
548 		def1 = data1 = RREG32_SOC15(MMHUB, 0, mmDAGB0_CNTL_MISC2_Sienna_Cichlid);
549 		break;
550 	default:
551 		def  = data  = RREG32_SOC15(MMHUB, 0, mmMM_ATC_L2_MISC_CG);
552 		def1 = data1 = RREG32_SOC15(MMHUB, 0, mmDAGB0_CNTL_MISC2);
553 		break;
554 	}
555 
556 	if (enable && (adev->cg_flags & AMD_CG_SUPPORT_MC_MGCG)) {
557 		data |= MM_ATC_L2_MISC_CG__ENABLE_MASK;
558 
559 		data1 &= ~(DAGB0_CNTL_MISC2__DISABLE_WRREQ_CG_MASK |
560 		           DAGB0_CNTL_MISC2__DISABLE_WRRET_CG_MASK |
561 		           DAGB0_CNTL_MISC2__DISABLE_RDREQ_CG_MASK |
562 		           DAGB0_CNTL_MISC2__DISABLE_RDRET_CG_MASK |
563 		           DAGB0_CNTL_MISC2__DISABLE_TLBWR_CG_MASK |
564 		           DAGB0_CNTL_MISC2__DISABLE_TLBRD_CG_MASK);
565 
566 	} else {
567 		data &= ~MM_ATC_L2_MISC_CG__ENABLE_MASK;
568 
569 		data1 |= (DAGB0_CNTL_MISC2__DISABLE_WRREQ_CG_MASK |
570 			  DAGB0_CNTL_MISC2__DISABLE_WRRET_CG_MASK |
571 			  DAGB0_CNTL_MISC2__DISABLE_RDREQ_CG_MASK |
572 			  DAGB0_CNTL_MISC2__DISABLE_RDRET_CG_MASK |
573 			  DAGB0_CNTL_MISC2__DISABLE_TLBWR_CG_MASK |
574 			  DAGB0_CNTL_MISC2__DISABLE_TLBRD_CG_MASK);
575 	}
576 
577 	switch (adev->asic_type) {
578 	case CHIP_SIENNA_CICHLID:
579 	case CHIP_NAVY_FLOUNDER:
580 	case CHIP_DIMGREY_CAVEFISH:
581 		if (def != data)
582 			WREG32_SOC15(MMHUB, 0, mmMM_ATC_L2_MISC_CG_Sienna_Cichlid, data);
583 		if (def1 != data1)
584 			WREG32_SOC15(MMHUB, 0, mmDAGB0_CNTL_MISC2_Sienna_Cichlid, data1);
585 		break;
586 	default:
587 		if (def != data)
588 			WREG32_SOC15(MMHUB, 0, mmMM_ATC_L2_MISC_CG, data);
589 		if (def1 != data1)
590 			WREG32_SOC15(MMHUB, 0, mmDAGB0_CNTL_MISC2, data1);
591 		break;
592 	}
593 }
594 
595 static void mmhub_v2_0_update_medium_grain_light_sleep(struct amdgpu_device *adev,
596 						       bool enable)
597 {
598 	uint32_t def, data;
599 
600 	switch (adev->asic_type) {
601 	case CHIP_SIENNA_CICHLID:
602 	case CHIP_NAVY_FLOUNDER:
603 	case CHIP_DIMGREY_CAVEFISH:
604 		def  = data  = RREG32_SOC15(MMHUB, 0, mmMM_ATC_L2_MISC_CG_Sienna_Cichlid);
605 		break;
606 	default:
607 		def  = data  = RREG32_SOC15(MMHUB, 0, mmMM_ATC_L2_MISC_CG);
608 		break;
609 	}
610 
611 	if (enable && (adev->cg_flags & AMD_CG_SUPPORT_MC_LS))
612 		data |= MM_ATC_L2_MISC_CG__MEM_LS_ENABLE_MASK;
613 	else
614 		data &= ~MM_ATC_L2_MISC_CG__MEM_LS_ENABLE_MASK;
615 
616 	if (def != data) {
617 		switch (adev->asic_type) {
618 		case CHIP_SIENNA_CICHLID:
619 		case CHIP_NAVY_FLOUNDER:
620 		case CHIP_DIMGREY_CAVEFISH:
621 			WREG32_SOC15(MMHUB, 0, mmMM_ATC_L2_MISC_CG_Sienna_Cichlid, data);
622 			break;
623 		default:
624 			WREG32_SOC15(MMHUB, 0, mmMM_ATC_L2_MISC_CG, data);
625 			break;
626 		}
627 	}
628 }
629 
630 static int mmhub_v2_0_set_clockgating(struct amdgpu_device *adev,
631 			       enum amd_clockgating_state state)
632 {
633 	if (amdgpu_sriov_vf(adev))
634 		return 0;
635 
636 	switch (adev->asic_type) {
637 	case CHIP_NAVI10:
638 	case CHIP_NAVI14:
639 	case CHIP_NAVI12:
640 	case CHIP_SIENNA_CICHLID:
641 	case CHIP_NAVY_FLOUNDER:
642 	case CHIP_DIMGREY_CAVEFISH:
643 		mmhub_v2_0_update_medium_grain_clock_gating(adev,
644 				state == AMD_CG_STATE_GATE);
645 		mmhub_v2_0_update_medium_grain_light_sleep(adev,
646 				state == AMD_CG_STATE_GATE);
647 		break;
648 	default:
649 		break;
650 	}
651 
652 	return 0;
653 }
654 
655 static void mmhub_v2_0_get_clockgating(struct amdgpu_device *adev, u32 *flags)
656 {
657 	int data, data1;
658 
659 	if (amdgpu_sriov_vf(adev))
660 		*flags = 0;
661 
662 	switch (adev->asic_type) {
663 	case CHIP_SIENNA_CICHLID:
664 	case CHIP_NAVY_FLOUNDER:
665 	case CHIP_DIMGREY_CAVEFISH:
666 		data  = RREG32_SOC15(MMHUB, 0, mmMM_ATC_L2_MISC_CG_Sienna_Cichlid);
667 		data1 = RREG32_SOC15(MMHUB, 0, mmDAGB0_CNTL_MISC2_Sienna_Cichlid);
668 		break;
669 	default:
670 		data  = RREG32_SOC15(MMHUB, 0, mmMM_ATC_L2_MISC_CG);
671 		data1 = RREG32_SOC15(MMHUB, 0, mmDAGB0_CNTL_MISC2);
672 		break;
673 	}
674 
675 	/* AMD_CG_SUPPORT_MC_MGCG */
676 	if ((data & MM_ATC_L2_MISC_CG__ENABLE_MASK) &&
677 	    !(data1 & (DAGB0_CNTL_MISC2__DISABLE_WRREQ_CG_MASK |
678 		       DAGB0_CNTL_MISC2__DISABLE_WRRET_CG_MASK |
679 		       DAGB0_CNTL_MISC2__DISABLE_RDREQ_CG_MASK |
680 		       DAGB0_CNTL_MISC2__DISABLE_RDRET_CG_MASK |
681 		       DAGB0_CNTL_MISC2__DISABLE_TLBWR_CG_MASK |
682 		       DAGB0_CNTL_MISC2__DISABLE_TLBRD_CG_MASK)))
683 		*flags |= AMD_CG_SUPPORT_MC_MGCG;
684 
685 	/* AMD_CG_SUPPORT_MC_LS */
686 	if (data & MM_ATC_L2_MISC_CG__MEM_LS_ENABLE_MASK)
687 		*flags |= AMD_CG_SUPPORT_MC_LS;
688 }
689 
690 const struct amdgpu_mmhub_funcs mmhub_v2_0_funcs = {
691 	.init = mmhub_v2_0_init,
692 	.gart_enable = mmhub_v2_0_gart_enable,
693 	.set_fault_enable_default = mmhub_v2_0_set_fault_enable_default,
694 	.gart_disable = mmhub_v2_0_gart_disable,
695 	.set_clockgating = mmhub_v2_0_set_clockgating,
696 	.get_clockgating = mmhub_v2_0_get_clockgating,
697 	.setup_vm_pt_regs = mmhub_v2_0_setup_vm_pt_regs,
698 };
699