xref: /linux/drivers/gpu/drm/amd/amdgpu/gmc_v12_1.c (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1 /*
2  * Copyright 2025 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 "amdgpu.h"
24 #include "gmc_v12_1.h"
25 #include "soc15_common.h"
26 #include "soc_v1_0.h"
27 #include "soc_v1_0_enum.h"
28 #include "oss/osssys_7_1_0_offset.h"
29 #include "oss/osssys_7_1_0_sh_mask.h"
30 #include "ivsrcid/vmc/irqsrcs_vmc_1_0.h"
31 
32 static void gmc_v12_1_get_mtypes(struct amdgpu_device *adev,
33 				 unsigned int *mtype_local,
34 				 unsigned int *mtype_remote);
35 
36 static int gmc_v12_1_vm_fault_interrupt_state(struct amdgpu_device *adev,
37 					      struct amdgpu_irq_src *src,
38 					      unsigned int type,
39 					      enum amdgpu_interrupt_state state)
40 {
41 	struct amdgpu_vmhub *hub;
42 	u32 tmp, reg, i, j;
43 
44 	switch (state) {
45 	case AMDGPU_IRQ_STATE_DISABLE:
46 		for_each_set_bit(j, adev->vmhubs_mask, AMDGPU_MAX_VMHUBS) {
47 			hub = &adev->vmhub[j];
48 			for (i = 0; i < 16; i++) {
49 				reg = hub->vm_context0_cntl + i;
50 
51 				/* This works because this interrupt is only
52 				 * enabled at init/resume and disabled in
53 				 * fini/suspend, so the overall state doesn't
54 				 * change over the course of suspend/resume.
55 				 */
56 				if (adev->in_s0ix && (j == AMDGPU_GFXHUB(0)))
57 					continue;
58 
59 				if (j >= AMDGPU_MMHUB0(0))
60 					tmp = RREG32_SOC15_IP(MMHUB, reg);
61 				else
62 					tmp = RREG32_XCC(reg, j);
63 
64 				tmp &= ~hub->vm_cntx_cntl_vm_fault;
65 
66 				if (j >= AMDGPU_MMHUB0(0))
67 					WREG32_SOC15_IP(MMHUB, reg, tmp);
68 				else
69 					WREG32_XCC(reg, tmp, j);
70 			}
71 		}
72 		break;
73 	case AMDGPU_IRQ_STATE_ENABLE:
74 		for_each_set_bit(j, adev->vmhubs_mask, AMDGPU_MAX_VMHUBS) {
75 			hub = &adev->vmhub[j];
76 			for (i = 0; i < 16; i++) {
77 				reg = hub->vm_context0_cntl + i;
78 
79 				/* This works because this interrupt is only
80 				 * enabled at init/resume and disabled in
81 				 * fini/suspend, so the overall state doesn't
82 				 * change over the course of suspend/resume.
83 				 */
84 				if (adev->in_s0ix && (j == AMDGPU_GFXHUB(0)))
85 					continue;
86 
87 				if (j >= AMDGPU_MMHUB0(0))
88 					tmp = RREG32_SOC15_IP(MMHUB, reg);
89 				else
90 					tmp = RREG32_XCC(reg, j);
91 
92 				tmp |= hub->vm_cntx_cntl_vm_fault;
93 
94 				if (j >= AMDGPU_MMHUB0(0))
95 					WREG32_SOC15_IP(MMHUB, reg, tmp);
96 				else
97 					WREG32_XCC(reg, tmp, j);
98 			}
99 		}
100 		break;
101 	default:
102 		break;
103 	}
104 
105 	return 0;
106 }
107 
108 /*
109  * NodeID definition in interrupt cookie for gmc v12.1.0
110  *
111  * DIE		IH Cookie NodeID
112  * MID0		0x0
113  * AID0		0x1
114  * AID0.XCD0	0x2
115  * AID0.XCD1	0x3
116  * AID0.XCD2	0x4
117  * AID0.XCD3	0x5
118  * MID1		0x8
119  * AID1		0x9
120  * AID1.XCD0	0xA
121  * AID1.XCD1	0xB
122  * AID1.XCD2	0xC
123  * AID1.XCD3	0xD
124  */
125 static int gmc_v12_1_process_interrupt(struct amdgpu_device *adev,
126 				       struct amdgpu_irq_src *source,
127 				       struct amdgpu_iv_entry *entry)
128 {
129 	struct amdgpu_task_info *task_info;
130 	bool retry_fault = false, write_fault = false;
131 	unsigned int vmhub, node_id;
132 	struct amdgpu_vmhub *hub;
133 	uint32_t cam_index = 0;
134 	const char *hub_name;
135 	int xcc_id = 0;
136 	uint32_t status = 0, status_hi = 0;
137 	const char *die_name;
138 	char die_name_buf[32];
139 	u64 addr;
140 
141 	node_id = entry->node_id;
142 
143 	addr = (u64)entry->src_data[0] << 12;
144 	addr |= ((u64)entry->src_data[1] & 0x1fff) << 44;
145 
146 	if (entry->src_id == UTCL2_1_0__SRCID__RETRY) {
147 		retry_fault = true;
148 		write_fault = !!(entry->src_data[1] & AMDGPU_GMC121_FAULT_SOURCE_DATA_WRITE);
149 	}
150 
151 	if (entry->client_id == SOC_V1_0_IH_CLIENTID_VMC) {
152 		hub_name = "mmhub0";
153 		vmhub = AMDGPU_MMHUB0(node_id / 8);
154 	} else {
155 		hub_name = "gfxhub0";
156 		if (adev->gfx.funcs->ih_node_to_logical_xcc) {
157 			xcc_id = adev->gfx.funcs->ih_node_to_logical_xcc(adev,
158 								node_id);
159 			if (xcc_id < 0)
160 				xcc_id = 0;
161 		}
162 		vmhub = xcc_id;
163 	}
164 
165 	hub = &adev->vmhub[vmhub];
166 
167 	if (retry_fault) {
168 		cam_index = entry->src_data[3] & 0x3ff;
169 
170 		int ret = amdgpu_gmc_handle_retry_fault(adev, entry, addr, cam_index, node_id,
171 							write_fault);
172 		/* Returning 1 here also prevents sending the IV to the KFD */
173 		if (ret == 1)
174 			return 1;
175 	}
176 
177 	if (kgd2kfd_vmfault_fast_path(adev, entry, retry_fault))
178 		return 1;
179 
180 	if (!printk_ratelimit())
181 		return 0;
182 
183 	dev_err(adev->dev,
184 		"[%s] %s page fault (src_id:%u ring:%u vmid:%u pasid:%u)\n", hub_name,
185 		retry_fault ? "retry" : "no-retry",
186 		entry->src_id, entry->ring_id, entry->vmid, entry->pasid);
187 
188 	task_info = amdgpu_vm_get_task_info_pasid(adev, entry->pasid);
189 	if (task_info) {
190 		amdgpu_vm_print_task_info(adev, task_info);
191 		amdgpu_vm_put_task_info(task_info);
192 	}
193 
194 	dev_err(adev->dev, "  in page starting at address 0x%016llx from IH client %d (%s)\n",
195 		addr, entry->client_id, soc_v1_0_ih_clientid_name[entry->client_id]);
196 
197 	if (adev->irq.ih_funcs &&
198 	    adev->irq.ih_funcs->node_id_to_die_name) {
199 		die_name = adev->irq.ih_funcs->node_id_to_die_name(adev, node_id,
200 								   die_name_buf,
201 								   sizeof(die_name_buf));
202 		if (die_name)
203 			dev_err(adev->dev,
204 				"  cookie node_id %d fault from die %s\n",
205 				node_id, die_name);
206 	}
207 
208 	if (amdgpu_sriov_vf(adev))
209 		return 0;
210 
211 	/*
212 	 * Issue a dummy read to wait for the status register to
213 	 * be updated to avoid reading an incorrect value due to
214 	 * the new fast GRBM interface.
215 	 */
216 	if (entry->vmid_src == AMDGPU_GFXHUB(0))
217 		RREG32(hub->vm_l2_pro_fault_status);
218 
219 	status = RREG32(hub->vm_l2_pro_fault_status);
220 	if (hub->vmhub_funcs &&
221 	    hub->vmhub_funcs->print_l2_protection_fault_status_hi)
222 		status_hi = RREG32(hub->vm_l2_pro_fault_status_hi);
223 
224 	/* Only print L2 fault status if the status register could be read and
225 	 * contains useful information
226 	 */
227 	if (!status && !status_hi)
228 		return 0;
229 
230 	WREG32_P(hub->vm_l2_pro_fault_cntl, 1, ~1);
231 
232 	amdgpu_vm_update_fault_cache(adev, entry->pasid, addr, status, vmhub);
233 
234 	if (!hub->vmhub_funcs || !hub->vmhub_funcs->print_l2_protection_fault_status) {
235 		dev_warn_once(adev->dev, "vmhub %d print fault status func not defined\n", vmhub);
236 		return 0;
237 	}
238 
239 	hub->vmhub_funcs->print_l2_protection_fault_status(adev, status);
240 	if (hub->vmhub_funcs->print_l2_protection_fault_status_hi)
241 		hub->vmhub_funcs->print_l2_protection_fault_status_hi(adev,
242 								     status_hi);
243 
244 	return 0;
245 }
246 
247 static bool gmc_v12_1_get_vmid_pasid_mapping_info(struct amdgpu_device *adev,
248 						  uint8_t vmid, uint8_t inst,
249 						  uint16_t *p_pasid)
250 {
251 	uint16_t index;
252 
253 	if (inst/4)
254 		index = 0xA + inst%4;
255 	else
256 		index = 0x2 + inst%4;
257 
258 	WREG32(SOC15_REG_OFFSET(OSSSYS, 0, regIH_VMID_LUT_INDEX), index);
259 
260 	*p_pasid = RREG32(SOC15_REG_OFFSET(OSSSYS, 0, regIH_VMID_0_LUT) + vmid) & 0xffff;
261 
262 	return !!(*p_pasid);
263 }
264 
265 /*
266  * GART
267  * VMID 0 is the physical GPU addresses as used by the kernel.
268  * VMIDs 1-15 are used for userspace clients and are handled
269  * by the amdgpu vm/hsa code.
270  */
271 
272 /**
273  * gmc_v12_1_use_invalidate_semaphore - judge whether to use semaphore
274  *
275  * @adev: amdgpu_device pointer
276  * @vmhub: vmhub type
277  *
278  */
279 static bool gmc_v12_1_use_invalidate_semaphore(struct amdgpu_device *adev,
280 				       uint32_t vmhub)
281 {
282 	return ((!AMDGPU_IS_GFXHUB(vmhub)) &&
283 		(!amdgpu_sriov_vf(adev)));
284 }
285 
286 static void gmc_v12_1_flush_vm_hub(struct amdgpu_device *adev, uint32_t vmid,
287 				   unsigned int vmhub, uint32_t flush_type)
288 {
289 	bool use_semaphore = gmc_v12_1_use_invalidate_semaphore(adev, vmhub);
290 	struct amdgpu_vmhub *hub = &adev->vmhub[vmhub];
291 	u32 inv_req = hub->vmhub_funcs->get_invalidate_req(vmid, flush_type);
292 	u32 tmp;
293 	/* Use register 17 for GART */
294 	const unsigned eng = 17;
295 	unsigned int i;
296 	u32 inst;
297 
298 	inst = AMDGPU_IS_GFXHUB(vmhub) ? vmhub - AMDGPU_GFXHUB_START : 0;
299 
300 	spin_lock(&adev->gmc.invalidate_lock);
301 
302 	if (use_semaphore) {
303 		for (i = 0; i < adev->usec_timeout; i++) {
304 			/* a read return value of 1 means semaphore acuqire */
305 			if (AMDGPU_IS_GFXHUB(vmhub))
306 				tmp = RREG32_SOC15_IP_NO_KIQ(GC,
307 							     hub->vm_inv_eng0_sem +
308 							     hub->eng_distance * eng,
309 							     inst);
310 			else
311 				tmp = RREG32_SOC15_IP_NO_KIQ(MMHUB,
312 							     hub->vm_inv_eng0_sem +
313 							     hub->eng_distance * eng,
314 							     inst);
315 			if (tmp & 0x1)
316 				break;
317 			udelay(1);
318 		}
319 
320 		if (i >= adev->usec_timeout)
321 			DRM_ERROR("Timeout waiting for sem acquire in VM flush!\n");
322 	}
323 
324 	if (AMDGPU_IS_GFXHUB(vmhub))
325 		WREG32_SOC15_IP_NO_KIQ(GC,
326 				       hub->vm_inv_eng0_req + hub->eng_distance * eng,
327 				       inv_req, inst);
328 	else
329 		WREG32_SOC15_IP_NO_KIQ(MMHUB,
330 				       hub->vm_inv_eng0_req + hub->eng_distance * eng,
331 				       inv_req, inst);
332 
333 	/* Wait for ACK with a delay.*/
334 	for (i = 0; i < adev->usec_timeout; i++) {
335 		if (AMDGPU_IS_GFXHUB(vmhub))
336 			tmp = RREG32_SOC15_IP_NO_KIQ(GC,
337 						     hub->vm_inv_eng0_ack +
338 						     hub->eng_distance * eng,
339 						     inst);
340 		else
341 			tmp = RREG32_SOC15_IP_NO_KIQ(MMHUB,
342 						     hub->vm_inv_eng0_ack +
343 						     hub->eng_distance * eng,
344 						     inst);
345 		tmp &= 1 << vmid;
346 		if (tmp)
347 			break;
348 
349 		udelay(1);
350 	}
351 
352 	if (use_semaphore) {
353 		if (AMDGPU_IS_GFXHUB(vmhub))
354 			WREG32_SOC15_IP_NO_KIQ(GC,
355 					       hub->vm_inv_eng0_sem + hub->eng_distance * eng,
356 					       0, inst);
357 		else
358 			WREG32_SOC15_IP_NO_KIQ(MMHUB,
359 					       hub->vm_inv_eng0_sem + hub->eng_distance * eng,
360 					       0, inst);
361 	}
362 
363 	/* Issue additional private vm invalidation to MMHUB */
364 	if (!AMDGPU_IS_GFXHUB(vmhub) &&
365 	    (hub->vm_l2_bank_select_reserved_cid2) &&
366 		!amdgpu_sriov_vf(adev)) {
367 		inv_req = RREG32_NO_KIQ(hub->vm_l2_bank_select_reserved_cid2);
368 		/* bit 25: RSERVED_CACHE_PRIVATE_INVALIDATION */
369 		inv_req |= (1 << 25);
370 		/* Issue private invalidation */
371 		WREG32_NO_KIQ(hub->vm_l2_bank_select_reserved_cid2, inv_req);
372 		/* Read back to ensure invalidation is done*/
373 		RREG32_NO_KIQ(hub->vm_l2_bank_select_reserved_cid2);
374 	}
375 
376 	spin_unlock(&adev->gmc.invalidate_lock);
377 
378 	if (i < adev->usec_timeout)
379 		return;
380 
381 	dev_err(adev->dev, "Timeout waiting for VM flush ACK!\n");
382 }
383 
384 /**
385  * gmc_v12_1_flush_gpu_tlb - gart tlb flush callback
386  *
387  * @adev: amdgpu_device pointer
388  * @vmid: vm instance to flush
389  * @vmhub: which hub to flush
390  * @flush_type: the flush type
391  *
392  * Flush the TLB for the requested page table.
393  */
394 static void gmc_v12_1_flush_gpu_tlb(struct amdgpu_device *adev, uint32_t vmid,
395 				    uint32_t vmhub, uint32_t flush_type)
396 {
397 	u32 inst;
398 
399 	if (AMDGPU_IS_GFXHUB(vmhub) &&
400 	    !adev->gfx.is_poweron)
401 		return;
402 
403 	if (vmhub >= AMDGPU_MMHUB0(0))
404 		inst = 0;
405 	else
406 		inst = vmhub;
407 
408 	/* This is necessary for SRIOV as well as for GFXOFF to function
409 	 * properly under bare metal
410 	 */
411 	if (adev->mes.ring[MES_PIPE_INST(inst, 0)].sched.ready &&
412 	    (amdgpu_sriov_runtime(adev) || !amdgpu_sriov_vf(adev))) {
413 		struct amdgpu_vmhub *hub = &adev->vmhub[vmhub];
414 		const unsigned eng = 17;
415 		u32 inv_req = hub->vmhub_funcs->get_invalidate_req(vmid, flush_type);
416 		u32 req = hub->vm_inv_eng0_req + hub->eng_distance * eng;
417 		u32 ack = hub->vm_inv_eng0_ack + hub->eng_distance * eng;
418 
419 		amdgpu_gmc_fw_reg_write_reg_wait(adev, req, ack, inv_req,
420 				1 << vmid, inst);
421 		return;
422 	}
423 
424 	gmc_v12_1_flush_vm_hub(adev, vmid, vmhub, flush_type);
425 	return;
426 }
427 
428 /**
429  * gmc_v12_1_flush_gpu_tlb_pasid - tlb flush via pasid
430  *
431  * @adev: amdgpu_device pointer
432  * @pasid: pasid to be flush
433  * @flush_type: the flush type
434  * @all_hub: flush all hubs
435  * @inst: XCC instance to use for the invalidation
436  *
437  * Flush the TLB for the requested pasid.
438  */
439 static void gmc_v12_1_flush_gpu_tlb_pasid(struct amdgpu_device *adev,
440 					  uint16_t pasid, uint32_t flush_type,
441 					  bool all_hub, uint32_t inst)
442 {
443 	uint16_t queried;
444 	int vmid, i;
445 
446 	if (adev->mes.ring[0].sched.ready &&
447 	    (adev->mes.sched_version & AMDGPU_MES_VERSION_MASK) >= 0x6f) {
448 		struct mes_inv_tlbs_pasid_input input = {0};
449 		input.xcc_id = inst;
450 		input.pasid = pasid;
451 		input.flush_type = flush_type;
452 
453 		/* MES will invalidate hubs for the device(including slave xcc) from master, ignore request from slave */
454 		if (!amdgpu_gfx_is_master_xcc(adev, inst))
455 			return;
456 
457 		input.hub_id = AMDGPU_GFXHUB(0);
458 		adev->mes.funcs->invalidate_tlbs_pasid(&adev->mes, &input);
459 
460 		if (all_hub) {
461 			/* invalidate mm_hub */
462 			if (test_bit(AMDGPU_MMHUB0(0), adev->vmhubs_mask)) {
463 				input.hub_id = AMDGPU_MMHUB0(0);
464 				adev->mes.funcs->invalidate_tlbs_pasid(&adev->mes, &input);
465 			}
466 			if (test_bit(AMDGPU_MMHUB1(0), adev->vmhubs_mask)) {
467 				input.hub_id = AMDGPU_MMHUB1(0);
468 				adev->mes.funcs->invalidate_tlbs_pasid(&adev->mes, &input);
469 			}
470 		}
471 		return;
472 	}
473 
474 	for (vmid = 1; vmid < 16; vmid++) {
475 		bool valid;
476 
477 		valid = gmc_v12_1_get_vmid_pasid_mapping_info(adev, vmid, inst,
478 							      &queried);
479 		if (!valid || queried != pasid)
480 			continue;
481 
482 		if (all_hub) {
483 			for_each_set_bit(i, adev->vmhubs_mask,
484 					 AMDGPU_MAX_VMHUBS)
485 				gmc_v12_1_flush_gpu_tlb(adev, vmid, i,
486 							flush_type);
487 		} else {
488 			gmc_v12_1_flush_gpu_tlb(adev, vmid, AMDGPU_GFXHUB(inst),
489 						flush_type);
490 		}
491 	}
492 }
493 
494 static uint64_t gmc_v12_1_emit_flush_gpu_tlb(struct amdgpu_ring *ring,
495 					     unsigned vmid, uint64_t pd_addr)
496 {
497 	bool use_semaphore = gmc_v12_1_use_invalidate_semaphore(ring->adev, ring->vm_hub);
498 	struct amdgpu_vmhub *hub = &ring->adev->vmhub[ring->vm_hub];
499 	uint32_t req = hub->vmhub_funcs->get_invalidate_req(vmid, 0);
500 	unsigned eng = ring->vm_inv_eng;
501 
502 	if (use_semaphore)
503 		/* a read return value of 1 means semaphore acuqire */
504 		amdgpu_ring_emit_reg_wait(ring,
505 					  hub->vm_inv_eng0_sem +
506 					  hub->eng_distance * eng, 0x1, 0x1);
507 
508 	amdgpu_ring_emit_wreg(ring, hub->ctx0_ptb_addr_lo32 +
509 			      (hub->ctx_addr_distance * vmid),
510 			      lower_32_bits(pd_addr));
511 
512 	amdgpu_ring_emit_wreg(ring, hub->ctx0_ptb_addr_hi32 +
513 			      (hub->ctx_addr_distance * vmid),
514 			      upper_32_bits(pd_addr));
515 
516 	amdgpu_ring_emit_reg_write_reg_wait(ring, hub->vm_inv_eng0_req +
517 					    hub->eng_distance * eng,
518 					    hub->vm_inv_eng0_ack +
519 					    hub->eng_distance * eng,
520 					    req, 1 << vmid);
521 
522 	if (use_semaphore)
523 		/*
524 		 * add semaphore release after invalidation,
525 		 * write with 0 means semaphore release
526 		 */
527 		amdgpu_ring_emit_wreg(ring, hub->vm_inv_eng0_sem +
528 				      hub->eng_distance * eng, 0);
529 
530 	return pd_addr;
531 }
532 
533 static void gmc_v12_1_emit_pasid_mapping(struct amdgpu_ring *ring,
534 					 unsigned vmid, unsigned pasid)
535 {
536 	struct amdgpu_device *adev = ring->adev;
537 	uint32_t reg;
538 
539 	if (ring->vm_hub == AMDGPU_GFXHUB(0))
540 		reg = SOC15_REG_OFFSET(OSSSYS, 0, regIH_VMID_0_LUT) + vmid;
541 	else
542 		reg = SOC15_REG_OFFSET(OSSSYS, 0, regIH_VMID_0_LUT_MM) + vmid;
543 
544 	amdgpu_ring_emit_wreg(ring, reg, pasid);
545 }
546 
547 /*
548  * PTE format:
549  * 63 P
550  * 62:59 reserved
551  * 58 D
552  * 57 G
553  * 56 T
554  * 55:54 M
555  * 53:52 SW
556  * 51:48 reserved for future
557  * 47:12 4k physical page base address
558  * 11:7 fragment
559  * 6 write
560  * 5 read
561  * 4 exe
562  * 3 Z
563  * 2 snooped
564  * 1 system
565  * 0 valid
566  *
567  * PDE format:
568  * 63 P
569  * 62:58 block fragment size
570  * 57 reserved
571  * 56 A
572  * 55:54 M
573  * 53:52 reserved
574  * 51:48 reserved for future
575  * 47:6 physical base address of PD or PTE
576  * 5:3 reserved
577  * 2 C
578  * 1 system
579  * 0 valid
580  */
581 
582 static void gmc_v12_1_get_vm_pde(struct amdgpu_device *adev, int level,
583 				 uint64_t *addr, uint64_t *flags)
584 {
585 	if (!(*flags & AMDGPU_PDE_PTE_GFX12) && !(*flags & AMDGPU_PTE_SYSTEM))
586 		*addr = adev->vm_manager.vram_base_offset + *addr -
587 			adev->gmc.vram_start;
588 
589 	if (*addr & ~adev->gmc.pte_addr_mask) {
590 		dev_err(adev->dev, "Invalid PDE address: %llx\n", *addr);
591 		*addr = 0;
592 		*flags &= ~AMDGPU_PTE_VALID;
593 	}
594 
595 	*flags |= AMDGPU_PTE_SNOOPED;
596 
597 	if (!adev->gmc.translate_further)
598 		return;
599 
600 	if (level == AMDGPU_VM_PDB1) {
601 		/* Set the block fragment size */
602 		if (!(*flags & AMDGPU_PDE_PTE_GFX12))
603 			*flags |= AMDGPU_PDE_BFS_GFX12(0x9);
604 	}
605 }
606 
607 static void gmc_v12_1_get_npa_flags(struct amdgpu_device *adev,
608 				    uint64_t *flags)
609 {
610 	unsigned int mtype_local, mtype_remote;
611 
612 	gmc_v12_1_get_mtypes(adev, &mtype_local, &mtype_remote);
613 
614 	*flags = AMDGPU_PTE_MTYPE_GFX12(*flags, mtype_remote);
615 	/* VSCT = 0011 to identify NPA. Additionally PTE.B = 1 */
616 	*flags |= AMDGPU_PTE_SNOOPED | AMDGPU_PTE_PRT_GFX12 |
617 		   AMDGPU_PTE_BUS_ATOMICS;
618 	*flags &= ~AMDGPU_PTE_VALID;
619 	*flags &= ~AMDGPU_PTE_SYSTEM;
620 	*flags &= ~AMDGPU_PTE_EXECUTABLE;
621 }
622 
623 /*
624  * Resolve the MTYPEs used for local and remote memory accesses on GFX 12.1.
625  * Both default to an ASIC-dependent value that can be overridden through the
626  * amdgpu_mtype_local and amdgpu_mtype_remote module parameters.
627  */
628 static void gmc_v12_1_get_mtypes(struct amdgpu_device *adev,
629 				 unsigned int *mtype_local,
630 				 unsigned int *mtype_remote)
631 {
632 	bool is_aid_a1 = SOC_V1_0_DIE_REV_AID(adev->rev_id) == 1;
633 
634 	/* Local memory: ASIC default depends on the AID stepping. */
635 	*mtype_local = is_aid_a1 ? MTYPE_RW : MTYPE_NC;
636 	if (amdgpu_mtype_local == 0)
637 		*mtype_local = MTYPE_RW;
638 	else if (amdgpu_mtype_local == 1)
639 		*mtype_local = MTYPE_NC;
640 	else if (amdgpu_mtype_local == 2)
641 		DRM_INFO_ONCE("MTYPE_CC not supported for local memory\n");
642 
643 	/* Remote memory defaults to MTYPE_UC on GFX 12.1. */
644 	*mtype_remote = MTYPE_UC;
645 	if (amdgpu_mtype_remote == 0)
646 		*mtype_remote = MTYPE_NC;
647 	else if (amdgpu_mtype_remote == 1)
648 		*mtype_remote = MTYPE_UC;
649 
650 	DRM_INFO_ONCE("Using %s for local memory and %s for remote memory\n",
651 		      *mtype_local == MTYPE_RW ? "MTYPE_RW" : "MTYPE_NC",
652 		      *mtype_remote == MTYPE_NC ? "MTYPE_NC" : "MTYPE_UC");
653 }
654 
655 /*
656  * The compute MQD coherent_aql_mtype field (offset 509) must be programmed to
657  * 0 whenever the driver maps local or remote memory as MTYPE_NC, and to 1 in
658  * all other cases.
659  */
660 u32 gmc_v12_1_get_coherent_aql_mtype(struct amdgpu_device *adev)
661 {
662 	unsigned int mtype_local, mtype_remote;
663 
664 	gmc_v12_1_get_mtypes(adev, &mtype_local, &mtype_remote);
665 
666 	return (mtype_local == MTYPE_NC || mtype_remote == MTYPE_NC) ? 0 : 1;
667 }
668 
669 static void gmc_v12_1_get_coherence_flags(struct amdgpu_device *adev,
670 					  struct amdgpu_bo *bo,
671 					  uint64_t *flags)
672 {
673 	struct amdgpu_device *bo_adev = amdgpu_ttm_adev(bo->tbo.bdev);
674 	bool is_vram = bo->tbo.resource &&
675 		       bo->tbo.resource->mem_type == TTM_PL_VRAM;
676 	bool coherent = bo->flags & (AMDGPU_GEM_CREATE_COHERENT |
677 				     AMDGPU_GEM_CREATE_EXT_COHERENT);
678 	bool ext_coherent = bo->flags & AMDGPU_GEM_CREATE_EXT_COHERENT;
679 	uint32_t gc_ip_version = amdgpu_ip_version(adev, GC_HWIP, 0);
680 	bool uncached = bo->flags & AMDGPU_GEM_CREATE_UNCACHED;
681 	unsigned int mtype, mtype_local, mtype_remote;
682 	bool snoop = false;
683 	bool is_local = false;
684 	bool is_aid_a1, is_spx;
685 
686 	switch (gc_ip_version) {
687 	case IP_VERSION(12, 1, 0):
688 		gmc_v12_1_get_mtypes(adev, &mtype_local, &mtype_remote);
689 
690 		is_local = (is_vram && adev == bo_adev);
691 		snoop = true;
692 		if (uncached) {
693 			mtype = MTYPE_UC;
694 		} else if (ext_coherent) {
695 			is_aid_a1 = SOC_V1_0_DIE_REV_AID(adev->rev_id) == 1;
696 			is_spx = amdgpu_xcp_query_partition_mode(adev->xcp_mgr,
697 								 AMDGPU_XCP_FL_NONE) ==
698 				 AMDGPU_SPX_PARTITION_MODE;
699 			/* AID A0 requires MTYPE_UC for extended-scope coherent
700 			 * local memory in DPX/QPX/CPX modes.
701 			 */
702 			if (is_local && (is_aid_a1 || is_spx))
703 				mtype = mtype_local;
704 			else
705 				mtype = MTYPE_UC;
706 		} else {
707 			mtype = is_local ? mtype_local : mtype_remote;
708 		}
709 		break;
710 	default:
711 		if (uncached || coherent)
712 			mtype = MTYPE_UC;
713 		else
714 			mtype = MTYPE_NC;
715 	}
716 
717 	if (mtype != MTYPE_NC)
718 		*flags = AMDGPU_PTE_MTYPE_GFX12(*flags, mtype);
719 
720 	if (is_local || adev->have_atomics_support)
721 		*flags |= AMDGPU_PTE_BUS_ATOMICS;
722 
723 	*flags |= snoop ? AMDGPU_PTE_SNOOPED : 0;
724 }
725 
726 static void gmc_v12_1_get_vm_pte(struct amdgpu_device *adev,
727 				 struct amdgpu_vm *vm,
728 				 struct amdgpu_bo *bo,
729 				 uint32_t vm_flags,
730 				 uint64_t *flags)
731 {
732 	struct ttm_resource *mem;
733 
734 	if (vm_flags & AMDGPU_VM_PAGE_EXECUTABLE)
735 		*flags |= AMDGPU_PTE_EXECUTABLE;
736 	else
737 		*flags &= ~AMDGPU_PTE_EXECUTABLE;
738 
739 	switch (vm_flags & AMDGPU_VM_MTYPE_MASK) {
740 	case AMDGPU_VM_MTYPE_DEFAULT:
741 		*flags = AMDGPU_PTE_MTYPE_GFX12(*flags, MTYPE_NC);
742 		break;
743 	case AMDGPU_VM_MTYPE_NC:
744 	default:
745 		*flags = AMDGPU_PTE_MTYPE_GFX12(*flags, MTYPE_NC);
746 		break;
747 	case AMDGPU_VM_MTYPE_RW:
748 		*flags = AMDGPU_PTE_MTYPE_GFX12(*flags, MTYPE_RW);
749 		break;
750 	case AMDGPU_VM_MTYPE_UC:
751 		*flags = AMDGPU_PTE_MTYPE_GFX12(*flags, MTYPE_UC);
752 		break;
753 	}
754 
755 	if (bo) {
756 		mem = bo->tbo.resource;
757 		if (mem && mem->mem_type == AMDGPU_PL_NPA) {
758 			dev_dbg(adev->dev,
759 				"Setting PTE for NPA BO, mem->type: %d, mem->start: %lx, mem->size: %u, cur_flags: %llx\n",
760 				mem->mem_type, mem->start, (u32)mem->size, *flags);
761 			gmc_v12_1_get_npa_flags(adev, flags);
762 		} else if (*flags & AMDGPU_PTE_VALID) {
763 			gmc_v12_1_get_coherence_flags(adev, bo, flags);
764 		}
765 	}
766 }
767 
768 static const struct amdgpu_gmc_funcs gmc_v12_1_gmc_funcs = {
769 	.flush_gpu_tlb = gmc_v12_1_flush_gpu_tlb,
770 	.flush_gpu_tlb_pasid = gmc_v12_1_flush_gpu_tlb_pasid,
771 	.emit_flush_gpu_tlb = gmc_v12_1_emit_flush_gpu_tlb,
772 	.emit_pasid_mapping = gmc_v12_1_emit_pasid_mapping,
773 	.get_vm_pde = gmc_v12_1_get_vm_pde,
774 	.get_vm_pte = gmc_v12_1_get_vm_pte,
775 	.query_mem_partition_mode = &amdgpu_gmc_query_memory_partition,
776 	.request_mem_partition_mode = &amdgpu_gmc_request_memory_partition,
777 };
778 
779 void gmc_v12_1_set_gmc_funcs(struct amdgpu_device *adev)
780 {
781 	adev->gmc.gmc_funcs = &gmc_v12_1_gmc_funcs;
782 }
783 
784 static const struct amdgpu_irq_src_funcs gmc_v12_1_irq_funcs = {
785 	.set = gmc_v12_1_vm_fault_interrupt_state,
786 	.process = gmc_v12_1_process_interrupt,
787 };
788 
789 static const struct amdgpu_irq_src_funcs gmc_v12_1_ecc_funcs = {
790 	.process = amdgpu_umc_uniras_process_ecc_irq,
791 };
792 
793 void gmc_v12_1_set_irq_funcs(struct amdgpu_device *adev)
794 {
795 	adev->gmc.vm_fault.num_types = 1;
796 	adev->gmc.vm_fault.funcs = &gmc_v12_1_irq_funcs;
797 
798 	adev->gmc.ecc_irq.num_types = 1;
799 	adev->gmc.ecc_irq.funcs = &gmc_v12_1_ecc_funcs;
800 }
801 
802 void gmc_v12_1_init_vram_info(struct amdgpu_device *adev)
803 {
804 	/* TODO: query vram_info from ip discovery binary */
805 	adev->gmc.vram_type = AMDGPU_VRAM_TYPE_HBM4;
806 	adev->gmc.vram_width = 384 * 64;
807 }
808 
809 void gmc_v12_1_init_nps_details(struct amdgpu_device *adev)
810 {
811 	enum amdgpu_memory_partition mode;
812 	uint32_t supp_modes;
813 	int i;
814 
815 	adev->gmc.supported_nps_modes = 0;
816 
817 	if (amdgpu_sriov_vf(adev) || (adev->flags & AMD_IS_APU))
818 		return;
819 
820 	mode = amdgpu_gmc_get_memory_partition(adev, &supp_modes);
821 
822 	/* Mode detected by hardware and supported modes available */
823 	if ((mode != UNKNOWN_MEMORY_PARTITION_MODE) && supp_modes) {
824 		while ((i = ffs(supp_modes))) {
825 			if (AMDGPU_ALL_NPS_MASK & BIT(i))
826 				adev->gmc.supported_nps_modes |= BIT(i);
827 			supp_modes &= supp_modes - 1;
828 		}
829 	} else {
830 		switch (amdgpu_ip_version(adev, GC_HWIP, 0)) {
831 		case IP_VERSION(12, 1, 0):
832 			adev->gmc.supported_nps_modes =
833 				BIT(AMDGPU_NPS1_PARTITION_MODE) |
834 				BIT(AMDGPU_NPS2_PARTITION_MODE);
835 			break;
836 		default:
837 			break;
838 		}
839 	}
840 }
841