xref: /linux/drivers/gpu/drm/amd/amdgpu/gmc_v11_0.c (revision ab475966455ce285c2c9978a3e3bfe97d75ff8d4)
1 /*
2  * Copyright 2021 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 #include <linux/firmware.h>
24 #include <linux/pci.h>
25 
26 #include <drm/drm_cache.h>
27 
28 #include "amdgpu.h"
29 #include "amdgpu_atomfirmware.h"
30 #include "gmc_v11_0.h"
31 #include "umc_v8_10.h"
32 #include "athub/athub_3_0_0_sh_mask.h"
33 #include "athub/athub_3_0_0_offset.h"
34 #include "dcn/dcn_3_2_0_offset.h"
35 #include "dcn/dcn_3_2_0_sh_mask.h"
36 #include "oss/osssys_6_0_0_offset.h"
37 #include "ivsrcid/vmc/irqsrcs_vmc_1_0.h"
38 #include "navi10_enum.h"
39 #include "soc15.h"
40 #include "soc15d.h"
41 #include "soc15_common.h"
42 #include "nbio_v4_3.h"
43 #include "gfxhub_v3_0.h"
44 #include "gfxhub_v3_0_3.h"
45 #include "gfxhub_v11_5_0.h"
46 #include "mmhub_v3_0.h"
47 #include "mmhub_v3_0_1.h"
48 #include "mmhub_v3_0_2.h"
49 #include "mmhub_v3_3.h"
50 #include "athub_v3_0.h"
51 
52 
53 static int gmc_v11_0_ecc_interrupt_state(struct amdgpu_device *adev,
54 					 struct amdgpu_irq_src *src,
55 					 unsigned int type,
56 					 enum amdgpu_interrupt_state state)
57 {
58 	return 0;
59 }
60 
61 static int
62 gmc_v11_0_vm_fault_interrupt_state(struct amdgpu_device *adev,
63 				   struct amdgpu_irq_src *src, unsigned int type,
64 				   enum amdgpu_interrupt_state state)
65 {
66 	switch (state) {
67 	case AMDGPU_IRQ_STATE_DISABLE:
68 		/* MM HUB */
69 		amdgpu_gmc_set_vm_fault_masks(adev, AMDGPU_MMHUB0(0), false);
70 		/* GFX HUB */
71 		/* This works because this interrupt is only
72 		 * enabled at init/resume and disabled in
73 		 * fini/suspend, so the overall state doesn't
74 		 * change over the course of suspend/resume.
75 		 */
76 		if (!adev->in_s0ix && (adev->in_runpm || adev->in_suspend ||
77 							   amdgpu_in_reset(adev)))
78 			amdgpu_gmc_set_vm_fault_masks(adev, AMDGPU_GFXHUB(0), false);
79 		break;
80 	case AMDGPU_IRQ_STATE_ENABLE:
81 		/* MM HUB */
82 		amdgpu_gmc_set_vm_fault_masks(adev, AMDGPU_MMHUB0(0), true);
83 		/* GFX HUB */
84 		/* This works because this interrupt is only
85 		 * enabled at init/resume and disabled in
86 		 * fini/suspend, so the overall state doesn't
87 		 * change over the course of suspend/resume.
88 		 */
89 		if (!adev->in_s0ix)
90 			amdgpu_gmc_set_vm_fault_masks(adev, AMDGPU_GFXHUB(0), true);
91 		break;
92 	default:
93 		break;
94 	}
95 
96 	return 0;
97 }
98 
99 static int gmc_v11_0_process_interrupt(struct amdgpu_device *adev,
100 				       struct amdgpu_irq_src *source,
101 				       struct amdgpu_iv_entry *entry)
102 {
103 	uint32_t vmhub_index = entry->client_id == SOC21_IH_CLIENTID_VMC ?
104 			       AMDGPU_MMHUB0(0) : AMDGPU_GFXHUB(0);
105 	struct amdgpu_vmhub *hub = &adev->vmhub[vmhub_index];
106 	uint32_t status = 0;
107 	u64 addr;
108 
109 	addr = (u64)entry->src_data[0] << 12;
110 	addr |= ((u64)entry->src_data[1] & 0xf) << 44;
111 
112 	if (!amdgpu_sriov_vf(adev)) {
113 		/*
114 		 * Issue a dummy read to wait for the status register to
115 		 * be updated to avoid reading an incorrect value due to
116 		 * the new fast GRBM interface.
117 		 */
118 		if (entry->vmid_src == AMDGPU_GFXHUB(0))
119 			RREG32(hub->vm_l2_pro_fault_status);
120 
121 		status = RREG32(hub->vm_l2_pro_fault_status);
122 		WREG32_P(hub->vm_l2_pro_fault_cntl, 1, ~1);
123 
124 		amdgpu_vm_update_fault_cache(adev, entry->pasid, addr, status,
125 					     entry->vmid_src ? AMDGPU_MMHUB0(0) : AMDGPU_GFXHUB(0));
126 	}
127 
128 	if (printk_ratelimit()) {
129 		struct amdgpu_task_info task_info;
130 
131 		memset(&task_info, 0, sizeof(struct amdgpu_task_info));
132 		amdgpu_vm_get_task_info(adev, entry->pasid, &task_info);
133 
134 		dev_err(adev->dev,
135 			"[%s] page fault (src_id:%u ring:%u vmid:%u pasid:%u, for process %s pid %d thread %s pid %d)\n",
136 			entry->vmid_src ? "mmhub" : "gfxhub",
137 			entry->src_id, entry->ring_id, entry->vmid,
138 			entry->pasid, task_info.process_name, task_info.tgid,
139 			task_info.task_name, task_info.pid);
140 		dev_err(adev->dev, "  in page starting at address 0x%016llx from client %d\n",
141 			addr, entry->client_id);
142 		if (!amdgpu_sriov_vf(adev))
143 			hub->vmhub_funcs->print_l2_protection_fault_status(adev, status);
144 	}
145 
146 	return 0;
147 }
148 
149 static const struct amdgpu_irq_src_funcs gmc_v11_0_irq_funcs = {
150 	.set = gmc_v11_0_vm_fault_interrupt_state,
151 	.process = gmc_v11_0_process_interrupt,
152 };
153 
154 static const struct amdgpu_irq_src_funcs gmc_v11_0_ecc_funcs = {
155 	.set = gmc_v11_0_ecc_interrupt_state,
156 	.process = amdgpu_umc_process_ecc_irq,
157 };
158 
159 static void gmc_v11_0_set_irq_funcs(struct amdgpu_device *adev)
160 {
161 	adev->gmc.vm_fault.num_types = 1;
162 	adev->gmc.vm_fault.funcs = &gmc_v11_0_irq_funcs;
163 
164 	if (!amdgpu_sriov_vf(adev)) {
165 		adev->gmc.ecc_irq.num_types = 1;
166 		adev->gmc.ecc_irq.funcs = &gmc_v11_0_ecc_funcs;
167 	}
168 }
169 
170 /**
171  * gmc_v11_0_use_invalidate_semaphore - judge whether to use semaphore
172  *
173  * @adev: amdgpu_device pointer
174  * @vmhub: vmhub type
175  *
176  */
177 static bool gmc_v11_0_use_invalidate_semaphore(struct amdgpu_device *adev,
178 				       uint32_t vmhub)
179 {
180 	return ((vmhub == AMDGPU_MMHUB0(0)) &&
181 		(!amdgpu_sriov_vf(adev)));
182 }
183 
184 static bool gmc_v11_0_get_vmid_pasid_mapping_info(
185 					struct amdgpu_device *adev,
186 					uint8_t vmid, uint16_t *p_pasid)
187 {
188 	*p_pasid = RREG32(SOC15_REG_OFFSET(OSSSYS, 0, regIH_VMID_0_LUT) + vmid) & 0xffff;
189 
190 	return !!(*p_pasid);
191 }
192 
193 /**
194  * gmc_v11_0_flush_gpu_tlb - gart tlb flush callback
195  *
196  * @adev: amdgpu_device pointer
197  * @vmid: vm instance to flush
198  * @vmhub: which hub to flush
199  * @flush_type: the flush type
200  *
201  * Flush the TLB for the requested page table.
202  */
203 static void gmc_v11_0_flush_gpu_tlb(struct amdgpu_device *adev, uint32_t vmid,
204 					uint32_t vmhub, uint32_t flush_type)
205 {
206 	bool use_semaphore = gmc_v11_0_use_invalidate_semaphore(adev, vmhub);
207 	struct amdgpu_vmhub *hub = &adev->vmhub[vmhub];
208 	u32 inv_req = hub->vmhub_funcs->get_invalidate_req(vmid, flush_type);
209 	/* Use register 17 for GART */
210 	const unsigned int eng = 17;
211 	unsigned char hub_ip;
212 	u32 sem, req, ack;
213 	unsigned int i;
214 	u32 tmp;
215 
216 	if ((vmhub == AMDGPU_GFXHUB(0)) && !adev->gfx.is_poweron)
217 		return;
218 
219 	sem = hub->vm_inv_eng0_sem + hub->eng_distance * eng;
220 	req = hub->vm_inv_eng0_req + hub->eng_distance * eng;
221 	ack = hub->vm_inv_eng0_ack + hub->eng_distance * eng;
222 
223 	/* flush hdp cache */
224 	adev->hdp.funcs->flush_hdp(adev, NULL);
225 
226 	/* For SRIOV run time, driver shouldn't access the register through MMIO
227 	 * Directly use kiq to do the vm invalidation instead
228 	 */
229 	if ((adev->gfx.kiq[0].ring.sched.ready || adev->mes.ring.sched.ready) &&
230 	    (amdgpu_sriov_runtime(adev) || !amdgpu_sriov_vf(adev))) {
231 		amdgpu_virt_kiq_reg_write_reg_wait(adev, req, ack, inv_req,
232 				1 << vmid, GET_INST(GC, 0));
233 		return;
234 	}
235 
236 	hub_ip = (vmhub == AMDGPU_GFXHUB(0)) ? GC_HWIP : MMHUB_HWIP;
237 
238 	spin_lock(&adev->gmc.invalidate_lock);
239 	/*
240 	 * It may lose gpuvm invalidate acknowldege state across power-gating
241 	 * off cycle, add semaphore acquire before invalidation and semaphore
242 	 * release after invalidation to avoid entering power gated state
243 	 * to WA the Issue
244 	 */
245 
246 	/* TODO: It needs to continue working on debugging with semaphore for GFXHUB as well. */
247 	if (use_semaphore) {
248 		for (i = 0; i < adev->usec_timeout; i++) {
249 			/* a read return value of 1 means semaphore acuqire */
250 			tmp = RREG32_RLC_NO_KIQ(sem, hub_ip);
251 			if (tmp & 0x1)
252 				break;
253 			udelay(1);
254 		}
255 
256 		if (i >= adev->usec_timeout)
257 			DRM_ERROR("Timeout waiting for sem acquire in VM flush!\n");
258 	}
259 
260 	WREG32_RLC_NO_KIQ(req, inv_req, hub_ip);
261 
262 	/* Wait for ACK with a delay.*/
263 	for (i = 0; i < adev->usec_timeout; i++) {
264 		tmp = RREG32_RLC_NO_KIQ(ack, hub_ip);
265 		tmp &= 1 << vmid;
266 		if (tmp)
267 			break;
268 
269 		udelay(1);
270 	}
271 
272 	/* TODO: It needs to continue working on debugging with semaphore for GFXHUB as well. */
273 	if (use_semaphore)
274 		WREG32_RLC_NO_KIQ(sem, 0, hub_ip);
275 
276 	/* Issue additional private vm invalidation to MMHUB */
277 	if ((vmhub != AMDGPU_GFXHUB(0)) &&
278 	    (hub->vm_l2_bank_select_reserved_cid2) &&
279 		!amdgpu_sriov_vf(adev)) {
280 		inv_req = RREG32_NO_KIQ(hub->vm_l2_bank_select_reserved_cid2);
281 		/* bit 25: RSERVED_CACHE_PRIVATE_INVALIDATION */
282 		inv_req |= (1 << 25);
283 		/* Issue private invalidation */
284 		WREG32_NO_KIQ(hub->vm_l2_bank_select_reserved_cid2, inv_req);
285 		/* Read back to ensure invalidation is done*/
286 		RREG32_NO_KIQ(hub->vm_l2_bank_select_reserved_cid2);
287 	}
288 
289 	spin_unlock(&adev->gmc.invalidate_lock);
290 
291 	if (i >= adev->usec_timeout)
292 		dev_err(adev->dev, "Timeout waiting for VM flush ACK!\n");
293 }
294 
295 /**
296  * gmc_v11_0_flush_gpu_tlb_pasid - tlb flush via pasid
297  *
298  * @adev: amdgpu_device pointer
299  * @pasid: pasid to be flush
300  * @flush_type: the flush type
301  * @all_hub: flush all hubs
302  * @inst: is used to select which instance of KIQ to use for the invalidation
303  *
304  * Flush the TLB for the requested pasid.
305  */
306 static void gmc_v11_0_flush_gpu_tlb_pasid(struct amdgpu_device *adev,
307 					  uint16_t pasid, uint32_t flush_type,
308 					  bool all_hub, uint32_t inst)
309 {
310 	uint16_t queried;
311 	int vmid, i;
312 
313 	for (vmid = 1; vmid < 16; vmid++) {
314 		bool valid;
315 
316 		valid = gmc_v11_0_get_vmid_pasid_mapping_info(adev, vmid,
317 							      &queried);
318 		if (!valid || queried != pasid)
319 			continue;
320 
321 		if (all_hub) {
322 			for_each_set_bit(i, adev->vmhubs_mask,
323 					 AMDGPU_MAX_VMHUBS)
324 				gmc_v11_0_flush_gpu_tlb(adev, vmid, i,
325 							flush_type);
326 		} else {
327 			gmc_v11_0_flush_gpu_tlb(adev, vmid, AMDGPU_GFXHUB(0),
328 						flush_type);
329 		}
330 	}
331 }
332 
333 static uint64_t gmc_v11_0_emit_flush_gpu_tlb(struct amdgpu_ring *ring,
334 					     unsigned int vmid, uint64_t pd_addr)
335 {
336 	bool use_semaphore = gmc_v11_0_use_invalidate_semaphore(ring->adev, ring->vm_hub);
337 	struct amdgpu_vmhub *hub = &ring->adev->vmhub[ring->vm_hub];
338 	uint32_t req = hub->vmhub_funcs->get_invalidate_req(vmid, 0);
339 	unsigned int eng = ring->vm_inv_eng;
340 
341 	/*
342 	 * It may lose gpuvm invalidate acknowldege state across power-gating
343 	 * off cycle, add semaphore acquire before invalidation and semaphore
344 	 * release after invalidation to avoid entering power gated state
345 	 * to WA the Issue
346 	 */
347 
348 	/* TODO: It needs to continue working on debugging with semaphore for GFXHUB as well. */
349 	if (use_semaphore)
350 		/* a read return value of 1 means semaphore acuqire */
351 		amdgpu_ring_emit_reg_wait(ring,
352 					  hub->vm_inv_eng0_sem +
353 					  hub->eng_distance * eng, 0x1, 0x1);
354 
355 	amdgpu_ring_emit_wreg(ring, hub->ctx0_ptb_addr_lo32 +
356 			      (hub->ctx_addr_distance * vmid),
357 			      lower_32_bits(pd_addr));
358 
359 	amdgpu_ring_emit_wreg(ring, hub->ctx0_ptb_addr_hi32 +
360 			      (hub->ctx_addr_distance * vmid),
361 			      upper_32_bits(pd_addr));
362 
363 	amdgpu_ring_emit_reg_write_reg_wait(ring, hub->vm_inv_eng0_req +
364 					    hub->eng_distance * eng,
365 					    hub->vm_inv_eng0_ack +
366 					    hub->eng_distance * eng,
367 					    req, 1 << vmid);
368 
369 	/* TODO: It needs to continue working on debugging with semaphore for GFXHUB as well. */
370 	if (use_semaphore)
371 		/*
372 		 * add semaphore release after invalidation,
373 		 * write with 0 means semaphore release
374 		 */
375 		amdgpu_ring_emit_wreg(ring, hub->vm_inv_eng0_sem +
376 				      hub->eng_distance * eng, 0);
377 
378 	return pd_addr;
379 }
380 
381 static void gmc_v11_0_emit_pasid_mapping(struct amdgpu_ring *ring, unsigned int vmid,
382 					 unsigned int pasid)
383 {
384 	struct amdgpu_device *adev = ring->adev;
385 	uint32_t reg;
386 
387 	/* MES fw manages IH_VMID_x_LUT updating */
388 	if (ring->is_mes_queue)
389 		return;
390 
391 	if (ring->vm_hub == AMDGPU_GFXHUB(0))
392 		reg = SOC15_REG_OFFSET(OSSSYS, 0, regIH_VMID_0_LUT) + vmid;
393 	else
394 		reg = SOC15_REG_OFFSET(OSSSYS, 0, regIH_VMID_0_LUT_MM) + vmid;
395 
396 	amdgpu_ring_emit_wreg(ring, reg, pasid);
397 }
398 
399 /*
400  * PTE format:
401  * 63:59 reserved
402  * 58:57 reserved
403  * 56 F
404  * 55 L
405  * 54 reserved
406  * 53:52 SW
407  * 51 T
408  * 50:48 mtype
409  * 47:12 4k physical page base address
410  * 11:7 fragment
411  * 6 write
412  * 5 read
413  * 4 exe
414  * 3 Z
415  * 2 snooped
416  * 1 system
417  * 0 valid
418  *
419  * PDE format:
420  * 63:59 block fragment size
421  * 58:55 reserved
422  * 54 P
423  * 53:48 reserved
424  * 47:6 physical base address of PD or PTE
425  * 5:3 reserved
426  * 2 C
427  * 1 system
428  * 0 valid
429  */
430 
431 static uint64_t gmc_v11_0_map_mtype(struct amdgpu_device *adev, uint32_t flags)
432 {
433 	switch (flags) {
434 	case AMDGPU_VM_MTYPE_DEFAULT:
435 		return AMDGPU_PTE_MTYPE_NV10(MTYPE_NC);
436 	case AMDGPU_VM_MTYPE_NC:
437 		return AMDGPU_PTE_MTYPE_NV10(MTYPE_NC);
438 	case AMDGPU_VM_MTYPE_WC:
439 		return AMDGPU_PTE_MTYPE_NV10(MTYPE_WC);
440 	case AMDGPU_VM_MTYPE_CC:
441 		return AMDGPU_PTE_MTYPE_NV10(MTYPE_CC);
442 	case AMDGPU_VM_MTYPE_UC:
443 		return AMDGPU_PTE_MTYPE_NV10(MTYPE_UC);
444 	default:
445 		return AMDGPU_PTE_MTYPE_NV10(MTYPE_NC);
446 	}
447 }
448 
449 static void gmc_v11_0_get_vm_pde(struct amdgpu_device *adev, int level,
450 				 uint64_t *addr, uint64_t *flags)
451 {
452 	if (!(*flags & AMDGPU_PDE_PTE) && !(*flags & AMDGPU_PTE_SYSTEM))
453 		*addr = amdgpu_gmc_vram_mc2pa(adev, *addr);
454 	BUG_ON(*addr & 0xFFFF00000000003FULL);
455 
456 	if (!adev->gmc.translate_further)
457 		return;
458 
459 	if (level == AMDGPU_VM_PDB1) {
460 		/* Set the block fragment size */
461 		if (!(*flags & AMDGPU_PDE_PTE))
462 			*flags |= AMDGPU_PDE_BFS(0x9);
463 
464 	} else if (level == AMDGPU_VM_PDB0) {
465 		if (*flags & AMDGPU_PDE_PTE)
466 			*flags &= ~AMDGPU_PDE_PTE;
467 		else
468 			*flags |= AMDGPU_PTE_TF;
469 	}
470 }
471 
472 static void gmc_v11_0_get_vm_pte(struct amdgpu_device *adev,
473 				 struct amdgpu_bo_va_mapping *mapping,
474 				 uint64_t *flags)
475 {
476 	struct amdgpu_bo *bo = mapping->bo_va->base.bo;
477 
478 	*flags &= ~AMDGPU_PTE_EXECUTABLE;
479 	*flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE;
480 
481 	*flags &= ~AMDGPU_PTE_MTYPE_NV10_MASK;
482 	*flags |= (mapping->flags & AMDGPU_PTE_MTYPE_NV10_MASK);
483 
484 	*flags &= ~AMDGPU_PTE_NOALLOC;
485 	*flags |= (mapping->flags & AMDGPU_PTE_NOALLOC);
486 
487 	if (mapping->flags & AMDGPU_PTE_PRT) {
488 		*flags |= AMDGPU_PTE_PRT;
489 		*flags |= AMDGPU_PTE_SNOOPED;
490 		*flags |= AMDGPU_PTE_LOG;
491 		*flags |= AMDGPU_PTE_SYSTEM;
492 		*flags &= ~AMDGPU_PTE_VALID;
493 	}
494 
495 	if (bo && bo->flags & (AMDGPU_GEM_CREATE_COHERENT |
496 			       AMDGPU_GEM_CREATE_EXT_COHERENT |
497 			       AMDGPU_GEM_CREATE_UNCACHED))
498 		*flags = (*flags & ~AMDGPU_PTE_MTYPE_NV10_MASK) |
499 			 AMDGPU_PTE_MTYPE_NV10(MTYPE_UC);
500 }
501 
502 static unsigned int gmc_v11_0_get_vbios_fb_size(struct amdgpu_device *adev)
503 {
504 	u32 d1vga_control = RREG32_SOC15(DCE, 0, regD1VGA_CONTROL);
505 	unsigned int size;
506 
507 	if (REG_GET_FIELD(d1vga_control, D1VGA_CONTROL, D1VGA_MODE_ENABLE)) {
508 		size = AMDGPU_VBIOS_VGA_ALLOCATION;
509 	} else {
510 		u32 viewport;
511 		u32 pitch;
512 
513 		viewport = RREG32_SOC15(DCE, 0, regHUBP0_DCSURF_PRI_VIEWPORT_DIMENSION);
514 		pitch = RREG32_SOC15(DCE, 0, regHUBPREQ0_DCSURF_SURFACE_PITCH);
515 		size = (REG_GET_FIELD(viewport,
516 					HUBP0_DCSURF_PRI_VIEWPORT_DIMENSION, PRI_VIEWPORT_HEIGHT) *
517 				REG_GET_FIELD(pitch, HUBPREQ0_DCSURF_SURFACE_PITCH, PITCH) *
518 				4);
519 	}
520 
521 	return size;
522 }
523 
524 static const struct amdgpu_gmc_funcs gmc_v11_0_gmc_funcs = {
525 	.flush_gpu_tlb = gmc_v11_0_flush_gpu_tlb,
526 	.flush_gpu_tlb_pasid = gmc_v11_0_flush_gpu_tlb_pasid,
527 	.emit_flush_gpu_tlb = gmc_v11_0_emit_flush_gpu_tlb,
528 	.emit_pasid_mapping = gmc_v11_0_emit_pasid_mapping,
529 	.map_mtype = gmc_v11_0_map_mtype,
530 	.get_vm_pde = gmc_v11_0_get_vm_pde,
531 	.get_vm_pte = gmc_v11_0_get_vm_pte,
532 	.get_vbios_fb_size = gmc_v11_0_get_vbios_fb_size,
533 };
534 
535 static void gmc_v11_0_set_gmc_funcs(struct amdgpu_device *adev)
536 {
537 	adev->gmc.gmc_funcs = &gmc_v11_0_gmc_funcs;
538 }
539 
540 static void gmc_v11_0_set_umc_funcs(struct amdgpu_device *adev)
541 {
542 	switch (amdgpu_ip_version(adev, UMC_HWIP, 0)) {
543 	case IP_VERSION(8, 10, 0):
544 		adev->umc.channel_inst_num = UMC_V8_10_CHANNEL_INSTANCE_NUM;
545 		adev->umc.umc_inst_num = UMC_V8_10_UMC_INSTANCE_NUM;
546 		adev->umc.max_ras_err_cnt_per_query = UMC_V8_10_TOTAL_CHANNEL_NUM(adev);
547 		adev->umc.channel_offs = UMC_V8_10_PER_CHANNEL_OFFSET;
548 		adev->umc.retire_unit = UMC_V8_10_NA_COL_2BITS_POWER_OF_2_NUM;
549 		if (adev->umc.node_inst_num == 4)
550 			adev->umc.channel_idx_tbl = &umc_v8_10_channel_idx_tbl_ext0[0][0][0];
551 		else
552 			adev->umc.channel_idx_tbl = &umc_v8_10_channel_idx_tbl[0][0][0];
553 		adev->umc.ras = &umc_v8_10_ras;
554 		break;
555 	case IP_VERSION(8, 11, 0):
556 		break;
557 	default:
558 		break;
559 	}
560 }
561 
562 
563 static void gmc_v11_0_set_mmhub_funcs(struct amdgpu_device *adev)
564 {
565 	switch (amdgpu_ip_version(adev, MMHUB_HWIP, 0)) {
566 	case IP_VERSION(3, 0, 1):
567 		adev->mmhub.funcs = &mmhub_v3_0_1_funcs;
568 		break;
569 	case IP_VERSION(3, 0, 2):
570 		adev->mmhub.funcs = &mmhub_v3_0_2_funcs;
571 		break;
572 	case IP_VERSION(3, 3, 0):
573 		adev->mmhub.funcs = &mmhub_v3_3_funcs;
574 		break;
575 	default:
576 		adev->mmhub.funcs = &mmhub_v3_0_funcs;
577 		break;
578 	}
579 }
580 
581 static void gmc_v11_0_set_gfxhub_funcs(struct amdgpu_device *adev)
582 {
583 	switch (amdgpu_ip_version(adev, GC_HWIP, 0)) {
584 	case IP_VERSION(11, 0, 3):
585 		adev->gfxhub.funcs = &gfxhub_v3_0_3_funcs;
586 		break;
587 	case IP_VERSION(11, 5, 0):
588 		adev->gfxhub.funcs = &gfxhub_v11_5_0_funcs;
589 		break;
590 	default:
591 		adev->gfxhub.funcs = &gfxhub_v3_0_funcs;
592 		break;
593 	}
594 }
595 
596 static int gmc_v11_0_early_init(void *handle)
597 {
598 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
599 
600 	gmc_v11_0_set_gfxhub_funcs(adev);
601 	gmc_v11_0_set_mmhub_funcs(adev);
602 	gmc_v11_0_set_gmc_funcs(adev);
603 	gmc_v11_0_set_irq_funcs(adev);
604 	gmc_v11_0_set_umc_funcs(adev);
605 
606 	adev->gmc.shared_aperture_start = 0x2000000000000000ULL;
607 	adev->gmc.shared_aperture_end =
608 		adev->gmc.shared_aperture_start + (4ULL << 30) - 1;
609 	adev->gmc.private_aperture_start = 0x1000000000000000ULL;
610 	adev->gmc.private_aperture_end =
611 		adev->gmc.private_aperture_start + (4ULL << 30) - 1;
612 	adev->gmc.noretry_flags = AMDGPU_VM_NORETRY_FLAGS_TF;
613 
614 	return 0;
615 }
616 
617 static int gmc_v11_0_late_init(void *handle)
618 {
619 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
620 	int r;
621 
622 	r = amdgpu_gmc_allocate_vm_inv_eng(adev);
623 	if (r)
624 		return r;
625 
626 	r = amdgpu_gmc_ras_late_init(adev);
627 	if (r)
628 		return r;
629 
630 	return amdgpu_irq_get(adev, &adev->gmc.vm_fault, 0);
631 }
632 
633 static void gmc_v11_0_vram_gtt_location(struct amdgpu_device *adev,
634 					struct amdgpu_gmc *mc)
635 {
636 	u64 base = 0;
637 
638 	base = adev->mmhub.funcs->get_fb_location(adev);
639 
640 	amdgpu_gmc_set_agp_default(adev, mc);
641 	amdgpu_gmc_vram_location(adev, &adev->gmc, base);
642 	amdgpu_gmc_gart_location(adev, mc, AMDGPU_GART_PLACEMENT_HIGH);
643 	if (!amdgpu_sriov_vf(adev) ||
644 	    (amdgpu_ip_version(adev, GC_HWIP, 0) < IP_VERSION(11, 5, 0)))
645 		amdgpu_gmc_agp_location(adev, mc);
646 
647 	/* base offset of vram pages */
648 	if (amdgpu_sriov_vf(adev))
649 		adev->vm_manager.vram_base_offset = 0;
650 	else
651 		adev->vm_manager.vram_base_offset = adev->mmhub.funcs->get_mc_fb_offset(adev);
652 }
653 
654 /**
655  * gmc_v11_0_mc_init - initialize the memory controller driver params
656  *
657  * @adev: amdgpu_device pointer
658  *
659  * Look up the amount of vram, vram width, and decide how to place
660  * vram and gart within the GPU's physical address space.
661  * Returns 0 for success.
662  */
663 static int gmc_v11_0_mc_init(struct amdgpu_device *adev)
664 {
665 	int r;
666 
667 	/* size in MB on si */
668 	adev->gmc.mc_vram_size =
669 		adev->nbio.funcs->get_memsize(adev) * 1024ULL * 1024ULL;
670 	adev->gmc.real_vram_size = adev->gmc.mc_vram_size;
671 
672 	if (!(adev->flags & AMD_IS_APU)) {
673 		r = amdgpu_device_resize_fb_bar(adev);
674 		if (r)
675 			return r;
676 	}
677 	adev->gmc.aper_base = pci_resource_start(adev->pdev, 0);
678 	adev->gmc.aper_size = pci_resource_len(adev->pdev, 0);
679 
680 #ifdef CONFIG_X86_64
681 	if ((adev->flags & AMD_IS_APU) && !amdgpu_passthrough(adev)) {
682 		adev->gmc.aper_base = adev->mmhub.funcs->get_mc_fb_offset(adev);
683 		adev->gmc.aper_size = adev->gmc.real_vram_size;
684 	}
685 #endif
686 	/* In case the PCI BAR is larger than the actual amount of vram */
687 	adev->gmc.visible_vram_size = adev->gmc.aper_size;
688 	if (adev->gmc.visible_vram_size > adev->gmc.real_vram_size)
689 		adev->gmc.visible_vram_size = adev->gmc.real_vram_size;
690 
691 	/* set the gart size */
692 	if (amdgpu_gart_size == -1)
693 		adev->gmc.gart_size = 512ULL << 20;
694 	else
695 		adev->gmc.gart_size = (u64)amdgpu_gart_size << 20;
696 
697 	gmc_v11_0_vram_gtt_location(adev, &adev->gmc);
698 
699 	return 0;
700 }
701 
702 static int gmc_v11_0_gart_init(struct amdgpu_device *adev)
703 {
704 	int r;
705 
706 	if (adev->gart.bo) {
707 		WARN(1, "PCIE GART already initialized\n");
708 		return 0;
709 	}
710 
711 	/* Initialize common gart structure */
712 	r = amdgpu_gart_init(adev);
713 	if (r)
714 		return r;
715 
716 	adev->gart.table_size = adev->gart.num_gpu_pages * 8;
717 	adev->gart.gart_pte_flags = AMDGPU_PTE_MTYPE_NV10(MTYPE_UC) |
718 				 AMDGPU_PTE_EXECUTABLE;
719 
720 	return amdgpu_gart_table_vram_alloc(adev);
721 }
722 
723 static int gmc_v11_0_sw_init(void *handle)
724 {
725 	int r, vram_width = 0, vram_type = 0, vram_vendor = 0;
726 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
727 
728 	adev->mmhub.funcs->init(adev);
729 
730 	adev->gfxhub.funcs->init(adev);
731 
732 	spin_lock_init(&adev->gmc.invalidate_lock);
733 
734 	r = amdgpu_atomfirmware_get_vram_info(adev,
735 					      &vram_width, &vram_type, &vram_vendor);
736 	adev->gmc.vram_width = vram_width;
737 
738 	adev->gmc.vram_type = vram_type;
739 	adev->gmc.vram_vendor = vram_vendor;
740 
741 	switch (amdgpu_ip_version(adev, GC_HWIP, 0)) {
742 	case IP_VERSION(11, 0, 0):
743 	case IP_VERSION(11, 0, 1):
744 	case IP_VERSION(11, 0, 2):
745 	case IP_VERSION(11, 0, 3):
746 	case IP_VERSION(11, 0, 4):
747 	case IP_VERSION(11, 5, 0):
748 		set_bit(AMDGPU_GFXHUB(0), adev->vmhubs_mask);
749 		set_bit(AMDGPU_MMHUB0(0), adev->vmhubs_mask);
750 		/*
751 		 * To fulfill 4-level page support,
752 		 * vm size is 256TB (48bit), maximum size,
753 		 * block size 512 (9bit)
754 		 */
755 		amdgpu_vm_adjust_size(adev, 256 * 1024, 9, 3, 48);
756 		break;
757 	default:
758 		break;
759 	}
760 
761 	/* This interrupt is VMC page fault.*/
762 	r = amdgpu_irq_add_id(adev, SOC21_IH_CLIENTID_VMC,
763 			      VMC_1_0__SRCID__VM_FAULT,
764 			      &adev->gmc.vm_fault);
765 
766 	if (r)
767 		return r;
768 
769 	r = amdgpu_irq_add_id(adev, SOC21_IH_CLIENTID_GFX,
770 			      UTCL2_1_0__SRCID__FAULT,
771 			      &adev->gmc.vm_fault);
772 	if (r)
773 		return r;
774 
775 	if (!amdgpu_sriov_vf(adev)) {
776 		/* interrupt sent to DF. */
777 		r = amdgpu_irq_add_id(adev, SOC21_IH_CLIENTID_DF, 0,
778 				      &adev->gmc.ecc_irq);
779 		if (r)
780 			return r;
781 	}
782 
783 	/*
784 	 * Set the internal MC address mask This is the max address of the GPU's
785 	 * internal address space.
786 	 */
787 	adev->gmc.mc_mask = 0xffffffffffffULL; /* 48 bit MC */
788 
789 	r = dma_set_mask_and_coherent(adev->dev, DMA_BIT_MASK(44));
790 	if (r) {
791 		dev_warn(adev->dev, "amdgpu: No suitable DMA available.\n");
792 		return r;
793 	}
794 
795 	adev->need_swiotlb = drm_need_swiotlb(44);
796 
797 	r = gmc_v11_0_mc_init(adev);
798 	if (r)
799 		return r;
800 
801 	amdgpu_gmc_get_vbios_allocations(adev);
802 
803 	/* Memory manager */
804 	r = amdgpu_bo_init(adev);
805 	if (r)
806 		return r;
807 
808 	r = gmc_v11_0_gart_init(adev);
809 	if (r)
810 		return r;
811 
812 	/*
813 	 * number of VMs
814 	 * VMID 0 is reserved for System
815 	 * amdgpu graphics/compute will use VMIDs 1-7
816 	 * amdkfd will use VMIDs 8-15
817 	 */
818 	adev->vm_manager.first_kfd_vmid = 8;
819 
820 	amdgpu_vm_manager_init(adev);
821 
822 	r = amdgpu_gmc_ras_sw_init(adev);
823 	if (r)
824 		return r;
825 
826 	return 0;
827 }
828 
829 /**
830  * gmc_v11_0_gart_fini - vm fini callback
831  *
832  * @adev: amdgpu_device pointer
833  *
834  * Tears down the driver GART/VM setup (CIK).
835  */
836 static void gmc_v11_0_gart_fini(struct amdgpu_device *adev)
837 {
838 	amdgpu_gart_table_vram_free(adev);
839 }
840 
841 static int gmc_v11_0_sw_fini(void *handle)
842 {
843 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
844 
845 	amdgpu_vm_manager_fini(adev);
846 	gmc_v11_0_gart_fini(adev);
847 	amdgpu_gem_force_release(adev);
848 	amdgpu_bo_fini(adev);
849 
850 	return 0;
851 }
852 
853 static void gmc_v11_0_init_golden_registers(struct amdgpu_device *adev)
854 {
855 	if (amdgpu_sriov_vf(adev)) {
856 		struct amdgpu_vmhub *hub = &adev->vmhub[AMDGPU_MMHUB0(0)];
857 
858 		WREG32(hub->vm_contexts_disable, 0);
859 		return;
860 	}
861 }
862 
863 /**
864  * gmc_v11_0_gart_enable - gart enable
865  *
866  * @adev: amdgpu_device pointer
867  */
868 static int gmc_v11_0_gart_enable(struct amdgpu_device *adev)
869 {
870 	int r;
871 	bool value;
872 
873 	if (adev->gart.bo == NULL) {
874 		dev_err(adev->dev, "No VRAM object for PCIE GART.\n");
875 		return -EINVAL;
876 	}
877 
878 	amdgpu_gtt_mgr_recover(&adev->mman.gtt_mgr);
879 
880 	r = adev->mmhub.funcs->gart_enable(adev);
881 	if (r)
882 		return r;
883 
884 	/* Flush HDP after it is initialized */
885 	adev->hdp.funcs->flush_hdp(adev, NULL);
886 
887 	value = (amdgpu_vm_fault_stop == AMDGPU_VM_FAULT_STOP_ALWAYS) ?
888 		false : true;
889 
890 	adev->mmhub.funcs->set_fault_enable_default(adev, value);
891 	gmc_v11_0_flush_gpu_tlb(adev, 0, AMDGPU_MMHUB0(0), 0);
892 
893 	DRM_INFO("PCIE GART of %uM enabled (table at 0x%016llX).\n",
894 		 (unsigned int)(adev->gmc.gart_size >> 20),
895 		 (unsigned long long)amdgpu_bo_gpu_offset(adev->gart.bo));
896 
897 	return 0;
898 }
899 
900 static int gmc_v11_0_hw_init(void *handle)
901 {
902 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
903 	int r;
904 
905 	adev->gmc.flush_pasid_uses_kiq = !amdgpu_emu_mode;
906 
907 	/* The sequence of these two function calls matters.*/
908 	gmc_v11_0_init_golden_registers(adev);
909 
910 	r = gmc_v11_0_gart_enable(adev);
911 	if (r)
912 		return r;
913 
914 	if (adev->umc.funcs && adev->umc.funcs->init_registers)
915 		adev->umc.funcs->init_registers(adev);
916 
917 	return 0;
918 }
919 
920 /**
921  * gmc_v11_0_gart_disable - gart disable
922  *
923  * @adev: amdgpu_device pointer
924  *
925  * This disables all VM page table.
926  */
927 static void gmc_v11_0_gart_disable(struct amdgpu_device *adev)
928 {
929 	adev->mmhub.funcs->gart_disable(adev);
930 }
931 
932 static int gmc_v11_0_hw_fini(void *handle)
933 {
934 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
935 
936 	if (amdgpu_sriov_vf(adev)) {
937 		/* full access mode, so don't touch any GMC register */
938 		DRM_DEBUG("For SRIOV client, shouldn't do anything.\n");
939 		return 0;
940 	}
941 
942 	amdgpu_irq_put(adev, &adev->gmc.vm_fault, 0);
943 	gmc_v11_0_gart_disable(adev);
944 
945 	return 0;
946 }
947 
948 static int gmc_v11_0_suspend(void *handle)
949 {
950 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
951 
952 	gmc_v11_0_hw_fini(adev);
953 
954 	return 0;
955 }
956 
957 static int gmc_v11_0_resume(void *handle)
958 {
959 	int r;
960 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
961 
962 	r = gmc_v11_0_hw_init(adev);
963 	if (r)
964 		return r;
965 
966 	amdgpu_vmid_reset_all(adev);
967 
968 	return 0;
969 }
970 
971 static bool gmc_v11_0_is_idle(void *handle)
972 {
973 	/* MC is always ready in GMC v11.*/
974 	return true;
975 }
976 
977 static int gmc_v11_0_wait_for_idle(void *handle)
978 {
979 	/* There is no need to wait for MC idle in GMC v11.*/
980 	return 0;
981 }
982 
983 static int gmc_v11_0_soft_reset(void *handle)
984 {
985 	return 0;
986 }
987 
988 static int gmc_v11_0_set_clockgating_state(void *handle,
989 					   enum amd_clockgating_state state)
990 {
991 	int r;
992 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
993 
994 	r = adev->mmhub.funcs->set_clockgating(adev, state);
995 	if (r)
996 		return r;
997 
998 	return athub_v3_0_set_clockgating(adev, state);
999 }
1000 
1001 static void gmc_v11_0_get_clockgating_state(void *handle, u64 *flags)
1002 {
1003 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1004 
1005 	adev->mmhub.funcs->get_clockgating(adev, flags);
1006 
1007 	athub_v3_0_get_clockgating(adev, flags);
1008 }
1009 
1010 static int gmc_v11_0_set_powergating_state(void *handle,
1011 					   enum amd_powergating_state state)
1012 {
1013 	return 0;
1014 }
1015 
1016 const struct amd_ip_funcs gmc_v11_0_ip_funcs = {
1017 	.name = "gmc_v11_0",
1018 	.early_init = gmc_v11_0_early_init,
1019 	.sw_init = gmc_v11_0_sw_init,
1020 	.hw_init = gmc_v11_0_hw_init,
1021 	.late_init = gmc_v11_0_late_init,
1022 	.sw_fini = gmc_v11_0_sw_fini,
1023 	.hw_fini = gmc_v11_0_hw_fini,
1024 	.suspend = gmc_v11_0_suspend,
1025 	.resume = gmc_v11_0_resume,
1026 	.is_idle = gmc_v11_0_is_idle,
1027 	.wait_for_idle = gmc_v11_0_wait_for_idle,
1028 	.soft_reset = gmc_v11_0_soft_reset,
1029 	.set_clockgating_state = gmc_v11_0_set_clockgating_state,
1030 	.set_powergating_state = gmc_v11_0_set_powergating_state,
1031 	.get_clockgating_state = gmc_v11_0_get_clockgating_state,
1032 };
1033 
1034 const struct amdgpu_ip_block_version gmc_v11_0_ip_block = {
1035 	.type = AMD_IP_BLOCK_TYPE_GMC,
1036 	.major = 11,
1037 	.minor = 0,
1038 	.rev = 0,
1039 	.funcs = &gmc_v11_0_ip_funcs,
1040 };
1041