xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c (revision 35e86e6a54e82e3624e9abdad61c8d4b0f764396)
1 /*
2  * Copyright 2018 Advanced Micro Devices, Inc.
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sub license, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
16  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
17  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19  * USE OR OTHER DEALINGS IN THE SOFTWARE.
20  *
21  * The above copyright notice and this permission notice (including the
22  * next paragraph) shall be included in all copies or substantial portions
23  * of the Software.
24  *
25  */
26 
27 #include <linux/io-64-nonatomic-lo-hi.h>
28 #ifdef CONFIG_X86
29 #include <asm/hypervisor.h>
30 #endif
31 
32 #include "amdgpu.h"
33 #include "amdgpu_gmc.h"
34 #include "amdgpu_ras.h"
35 #include "amdgpu_reset.h"
36 #include "amdgpu_xgmi.h"
37 #include "amdgpu_atomfirmware.h"
38 
39 #include <drm/drm_drv.h>
40 #include <drm/ttm/ttm_tt.h>
41 
42 static const u64 four_gb = 0x100000000ULL;
43 
44 bool amdgpu_gmc_is_pdb0_enabled(struct amdgpu_device *adev)
45 {
46 	return adev->gmc.xgmi.connected_to_cpu || amdgpu_virt_xgmi_migrate_enabled(adev);
47 }
48 
49 /**
50  * amdgpu_gmc_pdb0_alloc - allocate vram for pdb0
51  *
52  * @adev: amdgpu_device pointer
53  *
54  * Allocate video memory for pdb0 and map it for CPU access
55  * Returns 0 for success, error for failure.
56  */
57 int amdgpu_gmc_pdb0_alloc(struct amdgpu_device *adev)
58 {
59 	int r;
60 	struct amdgpu_bo_param bp;
61 	u64 vram_size = adev->gmc.xgmi.node_segment_size * adev->gmc.xgmi.num_physical_nodes;
62 	uint32_t pde0_page_shift = adev->gmc.vmid0_page_table_block_size + 21;
63 	uint32_t npdes = (vram_size + (1ULL << pde0_page_shift) - 1) >> pde0_page_shift;
64 
65 	memset(&bp, 0, sizeof(bp));
66 	bp.size = PAGE_ALIGN((npdes + 1) * 8);
67 	bp.byte_align = PAGE_SIZE;
68 	bp.domain = AMDGPU_GEM_DOMAIN_VRAM;
69 	bp.flags = AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
70 		AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
71 	bp.type = ttm_bo_type_kernel;
72 	bp.resv = NULL;
73 	bp.bo_ptr_size = sizeof(struct amdgpu_bo);
74 
75 	r = amdgpu_bo_create(adev, &bp, &adev->gmc.pdb0_bo);
76 	if (r)
77 		return r;
78 
79 	r = amdgpu_bo_reserve(adev->gmc.pdb0_bo, false);
80 	if (unlikely(r != 0))
81 		goto bo_reserve_failure;
82 
83 	r = amdgpu_bo_pin(adev->gmc.pdb0_bo, AMDGPU_GEM_DOMAIN_VRAM);
84 	if (r)
85 		goto bo_pin_failure;
86 	r = amdgpu_bo_kmap(adev->gmc.pdb0_bo, &adev->gmc.ptr_pdb0);
87 	if (r)
88 		goto bo_kmap_failure;
89 
90 	amdgpu_bo_unreserve(adev->gmc.pdb0_bo);
91 	return 0;
92 
93 bo_kmap_failure:
94 	amdgpu_bo_unpin(adev->gmc.pdb0_bo);
95 bo_pin_failure:
96 	amdgpu_bo_unreserve(adev->gmc.pdb0_bo);
97 bo_reserve_failure:
98 	amdgpu_bo_unref(&adev->gmc.pdb0_bo);
99 	return r;
100 }
101 
102 /**
103  * amdgpu_gmc_get_pde_for_bo - get the PDE for a BO
104  *
105  * @bo: the BO to get the PDE for
106  * @level: the level in the PD hirarchy
107  * @addr: resulting addr
108  * @flags: resulting flags
109  *
110  * Get the address and flags to be used for a PDE (Page Directory Entry).
111  */
112 void amdgpu_gmc_get_pde_for_bo(struct amdgpu_bo *bo, int level,
113 			       uint64_t *addr, uint64_t *flags)
114 {
115 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
116 
117 	switch (bo->tbo.resource->mem_type) {
118 	case TTM_PL_TT:
119 		*addr = bo->tbo.ttm->dma_address[0];
120 		break;
121 	case TTM_PL_VRAM:
122 		*addr = amdgpu_bo_gpu_offset(bo);
123 		break;
124 	default:
125 		*addr = 0;
126 		break;
127 	}
128 	*flags = amdgpu_ttm_tt_pde_flags(bo->tbo.ttm, bo->tbo.resource);
129 	amdgpu_gmc_get_vm_pde(adev, level, addr, flags);
130 }
131 
132 /*
133  * amdgpu_gmc_pd_addr - return the address of the root directory
134  */
135 uint64_t amdgpu_gmc_pd_addr(struct amdgpu_bo *bo)
136 {
137 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
138 	uint64_t pd_addr;
139 
140 	/* TODO: move that into ASIC specific code */
141 	if (adev->asic_type >= CHIP_VEGA10) {
142 		uint64_t flags = AMDGPU_PTE_VALID;
143 
144 		amdgpu_gmc_get_pde_for_bo(bo, -1, &pd_addr, &flags);
145 		pd_addr |= flags;
146 	} else {
147 		pd_addr = amdgpu_bo_gpu_offset(bo);
148 	}
149 	return pd_addr;
150 }
151 
152 /**
153  * amdgpu_gmc_set_pte_pde - update the page tables using CPU
154  *
155  * @adev: amdgpu_device pointer
156  * @cpu_pt_addr: cpu address of the page table
157  * @gpu_page_idx: entry in the page table to update
158  * @addr: dst addr to write into pte/pde
159  * @flags: access flags
160  *
161  * Update the page tables using CPU.
162  */
163 int amdgpu_gmc_set_pte_pde(struct amdgpu_device *adev, void *cpu_pt_addr,
164 				uint32_t gpu_page_idx, uint64_t addr,
165 				uint64_t flags)
166 {
167 	void __iomem *ptr = (void *)cpu_pt_addr;
168 	uint64_t value;
169 
170 	/*
171 	 * The following is for PTE only. GART does not have PDEs.
172 	*/
173 	value = addr & 0x0000FFFFFFFFF000ULL;
174 	value |= flags;
175 	writeq(value, ptr + (gpu_page_idx * 8));
176 
177 	return 0;
178 }
179 
180 /**
181  * amdgpu_gmc_agp_addr - return the address in the AGP address space
182  *
183  * @bo: TTM BO which needs the address, must be in GTT domain
184  *
185  * Tries to figure out how to access the BO through the AGP aperture. Returns
186  * AMDGPU_BO_INVALID_OFFSET if that is not possible.
187  */
188 uint64_t amdgpu_gmc_agp_addr(struct ttm_buffer_object *bo)
189 {
190 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
191 
192 	if (!bo->ttm)
193 		return AMDGPU_BO_INVALID_OFFSET;
194 
195 	if (bo->ttm->num_pages != 1 || bo->ttm->caching == ttm_cached)
196 		return AMDGPU_BO_INVALID_OFFSET;
197 
198 	if (bo->ttm->dma_address[0] + PAGE_SIZE >= adev->gmc.agp_size)
199 		return AMDGPU_BO_INVALID_OFFSET;
200 
201 	return adev->gmc.agp_start + bo->ttm->dma_address[0];
202 }
203 
204 /**
205  * amdgpu_gmc_vram_location - try to find VRAM location
206  *
207  * @adev: amdgpu device structure holding all necessary information
208  * @mc: memory controller structure holding memory information
209  * @base: base address at which to put VRAM
210  *
211  * Function will try to place VRAM at base address provided
212  * as parameter.
213  */
214 void amdgpu_gmc_vram_location(struct amdgpu_device *adev, struct amdgpu_gmc *mc,
215 			      u64 base)
216 {
217 	uint64_t vis_limit = (uint64_t)amdgpu_vis_vram_limit << 20;
218 	uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
219 
220 	mc->vram_start = base;
221 	mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
222 	if (limit < mc->real_vram_size)
223 		mc->real_vram_size = limit;
224 
225 	if (vis_limit && vis_limit < mc->visible_vram_size)
226 		mc->visible_vram_size = vis_limit;
227 
228 	if (mc->real_vram_size < mc->visible_vram_size)
229 		mc->visible_vram_size = mc->real_vram_size;
230 
231 	if (mc->xgmi.num_physical_nodes == 0) {
232 		mc->fb_start = mc->vram_start;
233 		mc->fb_end = mc->vram_end;
234 	}
235 	dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
236 			mc->mc_vram_size >> 20, mc->vram_start,
237 			mc->vram_end, mc->real_vram_size >> 20);
238 }
239 
240 /** amdgpu_gmc_sysvm_location - place vram and gart in sysvm aperture
241  *
242  * @adev: amdgpu device structure holding all necessary information
243  * @mc: memory controller structure holding memory information
244  *
245  * This function is only used if use GART for FB translation. In such
246  * case, we use sysvm aperture (vmid0 page tables) for both vram
247  * and gart (aka system memory) access.
248  *
249  * GPUVM (and our organization of vmid0 page tables) require sysvm
250  * aperture to be placed at a location aligned with 8 times of native
251  * page size. For example, if vm_context0_cntl.page_table_block_size
252  * is 12, then native page size is 8G (2M*2^12), sysvm should start
253  * with a 64G aligned address. For simplicity, we just put sysvm at
254  * address 0. So vram start at address 0 and gart is right after vram.
255  */
256 void amdgpu_gmc_sysvm_location(struct amdgpu_device *adev, struct amdgpu_gmc *mc)
257 {
258 	u64 hive_vram_start = 0;
259 	u64 hive_vram_end = mc->xgmi.node_segment_size * mc->xgmi.num_physical_nodes - 1;
260 	mc->vram_start = mc->xgmi.node_segment_size * mc->xgmi.physical_node_id;
261 	mc->vram_end = mc->vram_start + mc->xgmi.node_segment_size - 1;
262 	/* node_segment_size may not 4GB aligned on SRIOV, align up is needed. */
263 	mc->gart_start = ALIGN(hive_vram_end + 1, four_gb);
264 	mc->gart_end = mc->gart_start + mc->gart_size - 1;
265 	if (amdgpu_virt_xgmi_migrate_enabled(adev)) {
266 		/* set mc->vram_start to 0 to switch the returned GPU address of
267 		 * amdgpu_bo_create_reserved() from FB aperture to GART aperture.
268 		 */
269 		mc->vram_start = 0;
270 		mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
271 		mc->visible_vram_size = min(mc->visible_vram_size, mc->real_vram_size);
272 	} else {
273 		mc->fb_start = hive_vram_start;
274 		mc->fb_end = hive_vram_end;
275 	}
276 	dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
277 			mc->mc_vram_size >> 20, mc->vram_start,
278 			mc->vram_end, mc->real_vram_size >> 20);
279 	dev_info(adev->dev, "GART: %lluM 0x%016llX - 0x%016llX\n",
280 			mc->gart_size >> 20, mc->gart_start, mc->gart_end);
281 }
282 
283 /**
284  * amdgpu_gmc_gart_location - try to find GART location
285  *
286  * @adev: amdgpu device structure holding all necessary information
287  * @mc: memory controller structure holding memory information
288  * @gart_placement: GART placement policy with respect to VRAM
289  *
290  * Function will try to place GART before or after VRAM.
291  * If GART size is bigger than space left then we ajust GART size.
292  * Thus function will never fails.
293  */
294 void amdgpu_gmc_gart_location(struct amdgpu_device *adev, struct amdgpu_gmc *mc,
295 			      enum amdgpu_gart_placement gart_placement)
296 {
297 	u64 size_af, size_bf;
298 	/*To avoid the hole, limit the max mc address to AMDGPU_GMC_HOLE_START*/
299 	u64 max_mc_address = min(adev->gmc.mc_mask, AMDGPU_GMC_HOLE_START - 1);
300 
301 	/* VCE doesn't like it when BOs cross a 4GB segment, so align
302 	 * the GART base on a 4GB boundary as well.
303 	 */
304 	size_bf = mc->fb_start;
305 	size_af = max_mc_address + 1 - ALIGN(mc->fb_end + 1, four_gb);
306 
307 	if (mc->gart_size > max(size_bf, size_af)) {
308 		dev_warn(adev->dev, "limiting GART\n");
309 		mc->gart_size = max(size_bf, size_af);
310 	}
311 
312 	switch (gart_placement) {
313 	case AMDGPU_GART_PLACEMENT_HIGH:
314 		mc->gart_start = max_mc_address - mc->gart_size + 1;
315 		break;
316 	case AMDGPU_GART_PLACEMENT_LOW:
317 		mc->gart_start = 0;
318 		break;
319 	case AMDGPU_GART_PLACEMENT_BEST_FIT:
320 	default:
321 		if ((size_bf >= mc->gart_size && size_bf < size_af) ||
322 		    (size_af < mc->gart_size))
323 			mc->gart_start = 0;
324 		else
325 			mc->gart_start = max_mc_address - mc->gart_size + 1;
326 		break;
327 	}
328 
329 	mc->gart_start &= ~(four_gb - 1);
330 	mc->gart_end = mc->gart_start + mc->gart_size - 1;
331 	dev_info(adev->dev, "GART: %lluM 0x%016llX - 0x%016llX\n",
332 			mc->gart_size >> 20, mc->gart_start, mc->gart_end);
333 }
334 
335 /**
336  * amdgpu_gmc_agp_location - try to find AGP location
337  * @adev: amdgpu device structure holding all necessary information
338  * @mc: memory controller structure holding memory information
339  *
340  * Function will place try to find a place for the AGP BAR in the MC address
341  * space.
342  *
343  * AGP BAR will be assigned the largest available hole in the address space.
344  * Should be called after VRAM and GART locations are setup.
345  */
346 void amdgpu_gmc_agp_location(struct amdgpu_device *adev, struct amdgpu_gmc *mc)
347 {
348 	const uint64_t sixteen_gb = 1ULL << 34;
349 	const uint64_t sixteen_gb_mask = ~(sixteen_gb - 1);
350 	u64 size_af, size_bf;
351 
352 	if (mc->fb_start > mc->gart_start) {
353 		size_bf = (mc->fb_start & sixteen_gb_mask) -
354 			ALIGN(mc->gart_end + 1, sixteen_gb);
355 		size_af = mc->mc_mask + 1 - ALIGN(mc->fb_end + 1, sixteen_gb);
356 	} else {
357 		size_bf = mc->fb_start & sixteen_gb_mask;
358 		size_af = (mc->gart_start & sixteen_gb_mask) -
359 			ALIGN(mc->fb_end + 1, sixteen_gb);
360 	}
361 
362 	if (size_bf > size_af) {
363 		mc->agp_start = (mc->fb_start - size_bf) & sixteen_gb_mask;
364 		mc->agp_size = size_bf;
365 	} else {
366 		mc->agp_start = ALIGN(mc->fb_end + 1, sixteen_gb);
367 		mc->agp_size = size_af;
368 	}
369 
370 	mc->agp_end = mc->agp_start + mc->agp_size - 1;
371 	dev_info(adev->dev, "AGP: %lluM 0x%016llX - 0x%016llX\n",
372 			mc->agp_size >> 20, mc->agp_start, mc->agp_end);
373 }
374 
375 /**
376  * amdgpu_gmc_set_agp_default - Set the default AGP aperture value.
377  * @adev: amdgpu device structure holding all necessary information
378  * @mc: memory controller structure holding memory information
379  *
380  * To disable the AGP aperture, you need to set the start to a larger
381  * value than the end.  This function sets the default value which
382  * can then be overridden using amdgpu_gmc_agp_location() if you want
383  * to enable the AGP aperture on a specific chip.
384  *
385  */
386 void amdgpu_gmc_set_agp_default(struct amdgpu_device *adev,
387 				struct amdgpu_gmc *mc)
388 {
389 	mc->agp_start = 0xffffffffffff;
390 	mc->agp_end = 0;
391 	mc->agp_size = 0;
392 }
393 
394 /**
395  * amdgpu_gmc_fault_key - get hask key from vm fault address and pasid
396  *
397  * @addr: 48 bit physical address, page aligned (36 significant bits)
398  * @pasid: 16 bit process address space identifier
399  */
400 static inline uint64_t amdgpu_gmc_fault_key(uint64_t addr, uint16_t pasid)
401 {
402 	return addr << 4 | pasid;
403 }
404 
405 /**
406  * amdgpu_gmc_filter_faults - filter VM faults
407  *
408  * @adev: amdgpu device structure
409  * @ih: interrupt ring that the fault received from
410  * @addr: address of the VM fault
411  * @pasid: PASID of the process causing the fault
412  * @timestamp: timestamp of the fault
413  *
414  * Returns:
415  * True if the fault was filtered and should not be processed further.
416  * False if the fault is a new one and needs to be handled.
417  */
418 bool amdgpu_gmc_filter_faults(struct amdgpu_device *adev,
419 			      struct amdgpu_ih_ring *ih, uint64_t addr,
420 			      uint16_t pasid, uint64_t timestamp)
421 {
422 	struct amdgpu_gmc *gmc = &adev->gmc;
423 	uint64_t stamp, key = amdgpu_gmc_fault_key(addr, pasid);
424 	struct amdgpu_gmc_fault *fault;
425 	uint32_t hash;
426 
427 	/* Stale retry fault if timestamp goes backward */
428 	if (amdgpu_ih_ts_after(timestamp, ih->processed_timestamp))
429 		return true;
430 
431 	/* If we don't have space left in the ring buffer return immediately */
432 	stamp = max(timestamp, AMDGPU_GMC_FAULT_TIMEOUT + 1) -
433 		AMDGPU_GMC_FAULT_TIMEOUT;
434 	if (gmc->fault_ring[gmc->last_fault].timestamp >= stamp)
435 		return true;
436 
437 	/* Try to find the fault in the hash */
438 	hash = hash_64(key, AMDGPU_GMC_FAULT_HASH_ORDER);
439 	fault = &gmc->fault_ring[gmc->fault_hash[hash].idx];
440 	while (fault->timestamp >= stamp) {
441 		uint64_t tmp;
442 
443 		if (atomic64_read(&fault->key) == key) {
444 			/*
445 			 * if we get a fault which is already present in
446 			 * the fault_ring and the timestamp of
447 			 * the fault is after the expired timestamp,
448 			 * then this is a new fault that needs to be added
449 			 * into the fault ring.
450 			 */
451 			if (fault->timestamp_expiry != 0 &&
452 			    amdgpu_ih_ts_after(fault->timestamp_expiry,
453 					       timestamp))
454 				break;
455 			else
456 				return true;
457 		}
458 
459 		tmp = fault->timestamp;
460 		fault = &gmc->fault_ring[fault->next];
461 
462 		/* Check if the entry was reused */
463 		if (fault->timestamp >= tmp)
464 			break;
465 	}
466 
467 	/* Add the fault to the ring */
468 	fault = &gmc->fault_ring[gmc->last_fault];
469 	atomic64_set(&fault->key, key);
470 	fault->timestamp = timestamp;
471 
472 	/* And update the hash */
473 	fault->next = gmc->fault_hash[hash].idx;
474 	gmc->fault_hash[hash].idx = gmc->last_fault++;
475 	return false;
476 }
477 
478 /**
479  * amdgpu_gmc_filter_faults_remove - remove address from VM faults filter
480  *
481  * @adev: amdgpu device structure
482  * @addr: address of the VM fault
483  * @pasid: PASID of the process causing the fault
484  *
485  * Remove the address from fault filter, then future vm fault on this address
486  * will pass to retry fault handler to recover.
487  */
488 void amdgpu_gmc_filter_faults_remove(struct amdgpu_device *adev, uint64_t addr,
489 				     uint16_t pasid)
490 {
491 	struct amdgpu_gmc *gmc = &adev->gmc;
492 	uint64_t key = amdgpu_gmc_fault_key(addr, pasid);
493 	struct amdgpu_ih_ring *ih;
494 	struct amdgpu_gmc_fault *fault;
495 	uint32_t last_wptr;
496 	uint64_t last_ts;
497 	uint32_t hash;
498 	uint64_t tmp;
499 
500 	if (adev->irq.retry_cam_enabled)
501 		return;
502 	else if (adev->irq.ih1.ring_size)
503 		ih = &adev->irq.ih1;
504 	else if (adev->irq.ih_soft.enabled)
505 		ih = &adev->irq.ih_soft;
506 	else
507 		return;
508 
509 	/* Get the WPTR of the last entry in IH ring */
510 	last_wptr = amdgpu_ih_get_wptr(adev, ih);
511 	/* Order wptr with ring data. */
512 	rmb();
513 	/* Get the timetamp of the last entry in IH ring */
514 	last_ts = amdgpu_ih_decode_iv_ts(adev, ih, last_wptr, -1);
515 
516 	hash = hash_64(key, AMDGPU_GMC_FAULT_HASH_ORDER);
517 	fault = &gmc->fault_ring[gmc->fault_hash[hash].idx];
518 	do {
519 		if (atomic64_read(&fault->key) == key) {
520 			/*
521 			 * Update the timestamp when this fault
522 			 * expired.
523 			 */
524 			fault->timestamp_expiry = last_ts;
525 			break;
526 		}
527 
528 		tmp = fault->timestamp;
529 		fault = &gmc->fault_ring[fault->next];
530 	} while (fault->timestamp < tmp);
531 }
532 
533 int amdgpu_gmc_handle_retry_fault(struct amdgpu_device *adev,
534 				  struct amdgpu_iv_entry *entry,
535 				  u64 addr,
536 				  u32 cam_index,
537 				  u32 node_id,
538 				  bool write_fault)
539 {
540 	int ret;
541 
542 	if (adev->irq.retry_cam_enabled) {
543 		/* Delegate it to a different ring if the hardware hasn't
544 		 * already done it.
545 		 */
546 		if (entry->ih == &adev->irq.ih) {
547 			amdgpu_irq_delegate(adev, entry, 8);
548 			return 1;
549 		}
550 
551 		ret = amdgpu_vm_handle_fault(adev, entry->pasid, entry->vmid, node_id,
552 					     addr, entry->timestamp, write_fault);
553 		WDOORBELL32(adev->irq.retry_cam_doorbell_index, cam_index);
554 		if (ret)
555 			return 1;
556 	} else {
557 		/* Process it only if it's the first fault for this address */
558 		if (entry->ih != &adev->irq.ih_soft &&
559 		    amdgpu_gmc_filter_faults(adev, entry->ih, addr, entry->pasid,
560 					     entry->timestamp))
561 			return 1;
562 
563 		/* Delegate it to a different ring if the hardware hasn't
564 		 * already done it.
565 		 */
566 		if (entry->ih == &adev->irq.ih) {
567 			amdgpu_irq_delegate(adev, entry, 8);
568 			return 1;
569 		}
570 
571 		/* Try to handle the recoverable page faults by filling page
572 		 * tables
573 		 */
574 		if (amdgpu_vm_handle_fault(adev, entry->pasid, entry->vmid, node_id,
575 					   addr, entry->timestamp, write_fault))
576 			return 1;
577 	}
578 	return 0;
579 }
580 
581 int amdgpu_gmc_ras_sw_init(struct amdgpu_device *adev)
582 {
583 	int r;
584 
585 	/* umc ras block */
586 	r = amdgpu_umc_ras_sw_init(adev);
587 	if (r)
588 		return r;
589 
590 	/* mmhub ras block */
591 	r = amdgpu_mmhub_ras_sw_init(adev);
592 	if (r)
593 		return r;
594 
595 	/* hdp ras block */
596 	r = amdgpu_hdp_ras_sw_init(adev);
597 	if (r)
598 		return r;
599 
600 	/* mca.x ras block */
601 	r = amdgpu_mca_mp0_ras_sw_init(adev);
602 	if (r)
603 		return r;
604 
605 	r = amdgpu_mca_mp1_ras_sw_init(adev);
606 	if (r)
607 		return r;
608 
609 	r = amdgpu_mca_mpio_ras_sw_init(adev);
610 	if (r)
611 		return r;
612 
613 	/* xgmi ras block */
614 	r = amdgpu_xgmi_ras_sw_init(adev);
615 	if (r)
616 		return r;
617 
618 	return 0;
619 }
620 
621 int amdgpu_gmc_ras_late_init(struct amdgpu_device *adev)
622 {
623 	return 0;
624 }
625 
626 void amdgpu_gmc_ras_fini(struct amdgpu_device *adev)
627 {
628 
629 }
630 
631 	/*
632 	 * The latest engine allocation on gfx9/10 is:
633 	 * Engine 2, 3: firmware
634 	 * Engine 0, 1, 4~16: amdgpu ring,
635 	 *                    subject to change when ring number changes
636 	 * Engine 17: Gart flushes
637 	 */
638 #define AMDGPU_VMHUB_INV_ENG_BITMAP		0x1FFF3
639 
640 int amdgpu_gmc_allocate_vm_inv_eng(struct amdgpu_device *adev)
641 {
642 	struct amdgpu_ring *ring;
643 	unsigned vm_inv_engs[AMDGPU_MAX_VMHUBS] = {0};
644 	unsigned i;
645 	unsigned vmhub, inv_eng;
646 	struct amdgpu_ring *shared_ring;
647 
648 	/* init the vm inv eng for all vmhubs */
649 	for_each_set_bit(i, adev->vmhubs_mask, AMDGPU_MAX_VMHUBS) {
650 		vm_inv_engs[i] = AMDGPU_VMHUB_INV_ENG_BITMAP;
651 		/* reserve engine 5 for firmware */
652 		if (adev->enable_mes)
653 			vm_inv_engs[i] &= ~(1 << 5);
654 		/* reserve engine 6 for uni mes */
655 		if (adev->enable_uni_mes)
656 			vm_inv_engs[i] &= ~(1 << 6);
657 		/* reserve mmhub engine 3 for firmware */
658 		if (adev->enable_umsch_mm)
659 			vm_inv_engs[i] &= ~(1 << 3);
660 	}
661 
662 	for (i = 0; i < adev->num_rings; ++i) {
663 		ring = adev->rings[i];
664 		vmhub = ring->vm_hub;
665 
666 		if (ring == &adev->mes.ring[0] ||
667 		    ring == &adev->mes.ring[1] ||
668 		    ring == &adev->umsch_mm.ring ||
669 		    ring == &adev->cper.ring_buf)
670 			continue;
671 
672 		/* Skip if the ring is a shared ring */
673 		if (amdgpu_sdma_is_shared_inv_eng(adev, ring))
674 			continue;
675 
676 		inv_eng = ffs(vm_inv_engs[vmhub]);
677 		if (!inv_eng) {
678 			dev_err(adev->dev, "no VM inv eng for ring %s\n",
679 				ring->name);
680 			return -EINVAL;
681 		}
682 
683 		ring->vm_inv_eng = inv_eng - 1;
684 		vm_inv_engs[vmhub] &= ~(1 << ring->vm_inv_eng);
685 
686 		dev_info(adev->dev, "ring %s uses VM inv eng %u on hub %u\n",
687 			 ring->name, ring->vm_inv_eng, ring->vm_hub);
688 		/* SDMA has a special packet which allows it to use the same
689 		 * invalidation engine for all the rings in one instance.
690 		 * Therefore, we do not allocate a separate VM invalidation engine
691 		 * for SDMA page rings. Instead, they share the VM invalidation
692 		 * engine with the SDMA gfx ring. This change ensures efficient
693 		 * resource management and avoids the issue of insufficient VM
694 		 * invalidation engines.
695 		 */
696 		shared_ring = amdgpu_sdma_get_shared_ring(adev, ring);
697 		if (shared_ring) {
698 			shared_ring->vm_inv_eng = ring->vm_inv_eng;
699 			dev_info(adev->dev, "ring %s shares VM invalidation engine %u with ring %s on hub %u\n",
700 					ring->name, ring->vm_inv_eng, shared_ring->name, ring->vm_hub);
701 			continue;
702 		}
703 	}
704 
705 	return 0;
706 }
707 
708 void amdgpu_gmc_flush_gpu_tlb(struct amdgpu_device *adev, uint32_t vmid,
709 			      uint32_t vmhub, uint32_t flush_type)
710 {
711 	struct amdgpu_ring *ring;
712 	struct amdgpu_vmhub *hub = &adev->vmhub[vmhub];
713 	struct dma_fence *fence;
714 	struct amdgpu_job *job;
715 	int r;
716 
717 	ring = to_amdgpu_ring(adev->mman.buffer_funcs_scheds[0]);
718 
719 	if (!hub->sdma_invalidation_workaround || vmid ||
720 	    !adev->mman.buffer_funcs_enabled || !adev->ib_pool_ready ||
721 	    !ring->sched.ready) {
722 		/*
723 		 * A GPU reset should flush all TLBs anyway, so no need to do
724 		 * this while one is ongoing.
725 		 */
726 		if (!down_read_trylock(&adev->reset_domain->sem))
727 			return;
728 
729 		if (adev->gmc.flush_tlb_needs_extra_type_2)
730 			adev->gmc.gmc_funcs->flush_gpu_tlb(adev, vmid,
731 							   vmhub, 2);
732 
733 		if (adev->gmc.flush_tlb_needs_extra_type_0 && flush_type == 2)
734 			adev->gmc.gmc_funcs->flush_gpu_tlb(adev, vmid,
735 							   vmhub, 0);
736 
737 		adev->gmc.gmc_funcs->flush_gpu_tlb(adev, vmid, vmhub,
738 						   flush_type);
739 		up_read(&adev->reset_domain->sem);
740 		return;
741 	}
742 
743 	/* The SDMA on Navi 1x has a bug which can theoretically result in memory
744 	 * corruption if an invalidation happens at the same time as an VA
745 	 * translation. Avoid this by doing the invalidation from the SDMA
746 	 * itself at least for GART.
747 	 */
748 	mutex_lock(&adev->mman.default_entity.lock);
749 	r = amdgpu_job_alloc_with_ib(ring->adev, &adev->mman.default_entity.base,
750 				     AMDGPU_FENCE_OWNER_UNDEFINED,
751 				     16 * 4, AMDGPU_IB_POOL_IMMEDIATE,
752 				     &job, AMDGPU_KERNEL_JOB_ID_FLUSH_GPU_TLB);
753 	if (r)
754 		goto error_alloc;
755 
756 	job->vm_pd_addr = amdgpu_gmc_pd_addr(adev->gart.bo);
757 	job->vm_needs_flush = true;
758 	job->ibs->ptr[job->ibs->length_dw++] = ring->funcs->nop;
759 	amdgpu_ring_pad_ib(ring, &job->ibs[0]);
760 	fence = amdgpu_job_submit(job);
761 	mutex_unlock(&adev->mman.default_entity.lock);
762 
763 	dma_fence_wait(fence, false);
764 	dma_fence_put(fence);
765 
766 	return;
767 
768 error_alloc:
769 	mutex_unlock(&adev->mman.default_entity.lock);
770 	dev_err(adev->dev, "Error flushing GPU TLB using the SDMA (%d)!\n", r);
771 }
772 
773 int amdgpu_gmc_flush_gpu_tlb_pasid(struct amdgpu_device *adev, uint16_t pasid,
774 				   uint32_t flush_type, bool all_hub,
775 				   uint32_t inst)
776 {
777 	struct amdgpu_ring *ring = &adev->gfx.kiq[inst].ring;
778 	struct amdgpu_kiq *kiq = &adev->gfx.kiq[inst];
779 	unsigned int ndw;
780 	int r, cnt = 0;
781 	uint32_t seq;
782 
783 	/*
784 	 * A GPU reset should flush all TLBs anyway, so no need to do
785 	 * this while one is ongoing.
786 	 */
787 	if (!down_read_trylock(&adev->reset_domain->sem))
788 		return 0;
789 
790 	if (!adev->gmc.flush_pasid_uses_kiq || !ring->sched.ready) {
791 
792 		if (!adev->gmc.gmc_funcs->flush_gpu_tlb_pasid) {
793 			r = 0;
794 			goto error_unlock_reset;
795 		}
796 
797 		if (adev->gmc.flush_tlb_needs_extra_type_2)
798 			adev->gmc.gmc_funcs->flush_gpu_tlb_pasid(adev, pasid,
799 								 2, all_hub,
800 								 inst);
801 
802 		if (adev->gmc.flush_tlb_needs_extra_type_0 && flush_type == 2)
803 			adev->gmc.gmc_funcs->flush_gpu_tlb_pasid(adev, pasid,
804 								 0, all_hub,
805 								 inst);
806 
807 		adev->gmc.gmc_funcs->flush_gpu_tlb_pasid(adev, pasid,
808 							 flush_type, all_hub,
809 							 inst);
810 		r = 0;
811 	} else {
812 		/* 2 dwords flush + 8 dwords fence */
813 		ndw = kiq->pmf->invalidate_tlbs_size + 8;
814 
815 		if (adev->gmc.flush_tlb_needs_extra_type_2)
816 			ndw += kiq->pmf->invalidate_tlbs_size;
817 
818 		if (adev->gmc.flush_tlb_needs_extra_type_0)
819 			ndw += kiq->pmf->invalidate_tlbs_size;
820 
821 		spin_lock(&adev->gfx.kiq[inst].ring_lock);
822 		r = amdgpu_ring_alloc(ring, ndw);
823 		if (r) {
824 			spin_unlock(&adev->gfx.kiq[inst].ring_lock);
825 			goto error_unlock_reset;
826 		}
827 		if (adev->gmc.flush_tlb_needs_extra_type_2)
828 			kiq->pmf->kiq_invalidate_tlbs(ring, pasid, 2, all_hub);
829 
830 		if (flush_type == 2 && adev->gmc.flush_tlb_needs_extra_type_0)
831 			kiq->pmf->kiq_invalidate_tlbs(ring, pasid, 0, all_hub);
832 
833 		kiq->pmf->kiq_invalidate_tlbs(ring, pasid, flush_type, all_hub);
834 		r = amdgpu_fence_emit_polling(ring, &seq, MAX_KIQ_REG_WAIT);
835 		if (r) {
836 			amdgpu_ring_undo(ring);
837 			spin_unlock(&adev->gfx.kiq[inst].ring_lock);
838 			goto error_unlock_reset;
839 		}
840 
841 		amdgpu_ring_commit(ring);
842 		spin_unlock(&adev->gfx.kiq[inst].ring_lock);
843 
844 		r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT);
845 
846 		might_sleep();
847 		while (r < 1 && cnt++ < MAX_KIQ_REG_TRY &&
848 		       !amdgpu_reset_pending(adev->reset_domain)) {
849 			msleep(MAX_KIQ_REG_BAILOUT_INTERVAL);
850 			r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT);
851 		}
852 
853 		if (cnt > MAX_KIQ_REG_TRY) {
854 			dev_err(adev->dev, "timeout waiting for kiq fence\n");
855 			r = -ETIME;
856 		} else
857 			r = 0;
858 	}
859 
860 error_unlock_reset:
861 	up_read(&adev->reset_domain->sem);
862 	return r;
863 }
864 
865 void amdgpu_gmc_fw_reg_write_reg_wait(struct amdgpu_device *adev,
866 				      uint32_t reg0, uint32_t reg1,
867 				      uint32_t ref, uint32_t mask,
868 				      uint32_t xcc_inst)
869 {
870 	struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_inst];
871 	struct amdgpu_ring *ring = &kiq->ring;
872 	signed long r, cnt = 0;
873 	unsigned long flags;
874 	uint32_t seq;
875 
876 	if (adev->mes.ring[MES_PIPE_INST(xcc_inst, 0)].sched.ready) {
877 		amdgpu_mes_reg_write_reg_wait(adev, reg0, reg1,
878 					      ref, mask, xcc_inst);
879 		return;
880 	}
881 
882 	spin_lock_irqsave(&kiq->ring_lock, flags);
883 	amdgpu_ring_alloc(ring, 32);
884 	amdgpu_ring_emit_reg_write_reg_wait(ring, reg0, reg1,
885 					    ref, mask);
886 	r = amdgpu_fence_emit_polling(ring, &seq, MAX_KIQ_REG_WAIT);
887 	if (r)
888 		goto failed_undo;
889 
890 	amdgpu_ring_commit(ring);
891 	spin_unlock_irqrestore(&kiq->ring_lock, flags);
892 
893 	r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT);
894 
895 	/* don't wait anymore for IRQ context */
896 	if (r < 1 && in_interrupt())
897 		goto failed_kiq;
898 
899 	might_sleep();
900 	while (r < 1 && cnt++ < MAX_KIQ_REG_TRY &&
901 	       !amdgpu_reset_pending(adev->reset_domain)) {
902 
903 		msleep(MAX_KIQ_REG_BAILOUT_INTERVAL);
904 		r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT);
905 	}
906 
907 	if (cnt > MAX_KIQ_REG_TRY)
908 		goto failed_kiq;
909 
910 	return;
911 
912 failed_undo:
913 	amdgpu_ring_undo(ring);
914 	spin_unlock_irqrestore(&kiq->ring_lock, flags);
915 failed_kiq:
916 	dev_err(adev->dev, "failed to write reg %x wait reg %x\n", reg0, reg1);
917 }
918 
919 /**
920  * amdgpu_gmc_tmz_set -- check and set if a device supports TMZ
921  * @adev: amdgpu_device pointer
922  *
923  * Check and set if an the device @adev supports Trusted Memory
924  * Zones (TMZ).
925  */
926 void amdgpu_gmc_tmz_set(struct amdgpu_device *adev)
927 {
928 	switch (amdgpu_ip_version(adev, GC_HWIP, 0)) {
929 	/* RAVEN */
930 	case IP_VERSION(9, 2, 2):
931 	case IP_VERSION(9, 1, 0):
932 	/* RENOIR looks like RAVEN */
933 	case IP_VERSION(9, 3, 0):
934 	/* GC 10.3.7 */
935 	case IP_VERSION(10, 3, 7):
936 	/* GC 11.0.1 */
937 	case IP_VERSION(11, 0, 1):
938 		if (amdgpu_tmz == 0) {
939 			adev->gmc.tmz_enabled = false;
940 			dev_info(adev->dev,
941 				 "Trusted Memory Zone (TMZ) feature disabled (cmd line)\n");
942 		} else {
943 			adev->gmc.tmz_enabled = true;
944 			dev_info(adev->dev,
945 				 "Trusted Memory Zone (TMZ) feature enabled\n");
946 		}
947 		break;
948 	case IP_VERSION(10, 1, 10):
949 	case IP_VERSION(10, 1, 1):
950 	case IP_VERSION(10, 1, 2):
951 	case IP_VERSION(10, 1, 3):
952 	case IP_VERSION(10, 3, 0):
953 	case IP_VERSION(10, 3, 2):
954 	case IP_VERSION(10, 3, 4):
955 	case IP_VERSION(10, 3, 5):
956 	case IP_VERSION(10, 3, 6):
957 	/* VANGOGH */
958 	case IP_VERSION(10, 3, 1):
959 	/* YELLOW_CARP*/
960 	case IP_VERSION(10, 3, 3):
961 	case IP_VERSION(11, 0, 4):
962 	case IP_VERSION(11, 5, 0):
963 	case IP_VERSION(11, 5, 1):
964 	case IP_VERSION(11, 5, 2):
965 	case IP_VERSION(11, 5, 3):
966 	case IP_VERSION(11, 5, 4):
967 		/* Don't enable it by default yet.
968 		 */
969 		if (amdgpu_tmz < 1) {
970 			adev->gmc.tmz_enabled = false;
971 			dev_info(adev->dev,
972 				 "Trusted Memory Zone (TMZ) feature disabled as experimental (default)\n");
973 		} else {
974 			adev->gmc.tmz_enabled = true;
975 			dev_info(adev->dev,
976 				 "Trusted Memory Zone (TMZ) feature enabled as experimental (cmd line)\n");
977 		}
978 		break;
979 	default:
980 		adev->gmc.tmz_enabled = false;
981 		dev_info(adev->dev,
982 			 "Trusted Memory Zone (TMZ) feature not supported\n");
983 		break;
984 	}
985 }
986 
987 /**
988  * amdgpu_gmc_noretry_set -- set per asic noretry defaults
989  * @adev: amdgpu_device pointer
990  *
991  * Set a per asic default for the no-retry parameter.
992  *
993  */
994 void amdgpu_gmc_noretry_set(struct amdgpu_device *adev)
995 {
996 	struct amdgpu_gmc *gmc = &adev->gmc;
997 	uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
998 	bool noretry_default = (gc_ver == IP_VERSION(9, 0, 1) ||
999 				gc_ver == IP_VERSION(9, 4, 0) ||
1000 				gc_ver == IP_VERSION(9, 4, 1) ||
1001 				gc_ver == IP_VERSION(9, 4, 2) ||
1002 				gc_ver == IP_VERSION(9, 4, 3) ||
1003 				gc_ver == IP_VERSION(9, 4, 4) ||
1004 				gc_ver == IP_VERSION(9, 5, 0) ||
1005 				gc_ver >= IP_VERSION(10, 3, 0));
1006 
1007 	if (!amdgpu_sriov_xnack_support(adev))
1008 		gmc->noretry = 1;
1009 	else
1010 		gmc->noretry = (amdgpu_noretry == -1) ? noretry_default : amdgpu_noretry;
1011 }
1012 
1013 void amdgpu_gmc_set_vm_fault_masks(struct amdgpu_device *adev, int hub_type,
1014 				   bool enable)
1015 {
1016 	struct amdgpu_vmhub *hub;
1017 	u32 tmp, reg, i;
1018 
1019 	hub = &adev->vmhub[hub_type];
1020 	for (i = 0; i < 16; i++) {
1021 		reg = hub->vm_context0_cntl + hub->ctx_distance * i;
1022 
1023 		tmp = (hub_type == AMDGPU_GFXHUB(0)) ?
1024 			RREG32_SOC15_IP(GC, reg) :
1025 			RREG32_SOC15_IP(MMHUB, reg);
1026 
1027 		if (enable)
1028 			tmp |= hub->vm_cntx_cntl_vm_fault;
1029 		else
1030 			tmp &= ~hub->vm_cntx_cntl_vm_fault;
1031 
1032 		(hub_type == AMDGPU_GFXHUB(0)) ?
1033 			WREG32_SOC15_IP(GC, reg, tmp) :
1034 			WREG32_SOC15_IP(MMHUB, reg, tmp);
1035 	}
1036 }
1037 
1038 void amdgpu_gmc_init_vga_resv_regions(struct amdgpu_device *adev)
1039 {
1040 	unsigned size;
1041 
1042 	if (adev->gmc.is_app_apu)
1043 		return;
1044 
1045 	/*
1046 	 * Some ASICs need to reserve a region of video memory to avoid access
1047 	 * from driver
1048 	 */
1049 	/*
1050 	 * TODO:
1051 	 * Currently there is a bug where some memory client outside
1052 	 * of the driver writes to first 8M of VRAM on S3 resume,
1053 	 * this overrides GART which by default gets placed in first 8M and
1054 	 * causes VM_FAULTS once GTT is accessed.
1055 	 * Keep the stolen memory reservation until the while this is not solved.
1056 	 */
1057 	switch (adev->asic_type) {
1058 	case CHIP_VEGA10:
1059 		adev->mman.keep_stolen_vga_memory = true;
1060 		/*
1061 		 * VEGA10 SRIOV VF with MS_HYPERV host needs some firmware reserved area.
1062 		 */
1063 #ifdef CONFIG_X86
1064 		if (amdgpu_sriov_vf(adev) && hypervisor_is_type(X86_HYPER_MS_HYPERV)) {
1065 			amdgpu_ttm_init_vram_resv(adev, AMDGPU_RESV_STOLEN_RESERVED,
1066 						  0x500000, 0x200000, false);
1067 		}
1068 #endif
1069 		break;
1070 	case CHIP_RAVEN:
1071 	case CHIP_RENOIR:
1072 		adev->mman.keep_stolen_vga_memory = true;
1073 		break;
1074 	case CHIP_POLARIS10:
1075 	case CHIP_POLARIS11:
1076 	case CHIP_POLARIS12:
1077 		/* MacBookPros with switchable graphics put VRAM at 0 when
1078 		 * the iGPU is enabled which results in cursor issues if
1079 		 * the cursor ends up at 0.  Reserve vram at 0 in that case.
1080 		 */
1081 		if (adev->gmc.vram_start == 0)
1082 			adev->mman.keep_stolen_vga_memory = true;
1083 		break;
1084 	default:
1085 		adev->mman.keep_stolen_vga_memory = false;
1086 		break;
1087 	}
1088 
1089 	if (amdgpu_sriov_vf(adev) ||
1090 	    !amdgpu_device_has_display_hardware(adev)) {
1091 		size = 0;
1092 	} else {
1093 		size = amdgpu_gmc_get_vbios_fb_size(adev);
1094 
1095 		if (adev->mman.keep_stolen_vga_memory)
1096 			size = max(size, (unsigned)AMDGPU_VBIOS_VGA_ALLOCATION);
1097 	}
1098 
1099 	/* set to 0 if the pre-OS buffer uses up most of vram */
1100 	if ((adev->gmc.real_vram_size - size) < (8 * 1024 * 1024))
1101 		size = 0;
1102 
1103 	if (size > AMDGPU_VBIOS_VGA_ALLOCATION) {
1104 		amdgpu_ttm_init_vram_resv(adev, AMDGPU_RESV_STOLEN_VGA,
1105 					  0, AMDGPU_VBIOS_VGA_ALLOCATION, false);
1106 		amdgpu_ttm_init_vram_resv(adev, AMDGPU_RESV_STOLEN_EXTENDED,
1107 					  AMDGPU_VBIOS_VGA_ALLOCATION,
1108 					  size - AMDGPU_VBIOS_VGA_ALLOCATION, false);
1109 	} else {
1110 		amdgpu_ttm_init_vram_resv(adev, AMDGPU_RESV_STOLEN_VGA,
1111 					  0, size, false);
1112 	}
1113 }
1114 
1115 /**
1116  * amdgpu_gmc_init_pdb0 - initialize PDB0
1117  *
1118  * @adev: amdgpu_device pointer
1119  *
1120  * This function is only used when GART page table is used
1121  * for FB address translatioin. In such a case, we construct
1122  * a 2-level system VM page table: PDB0->PTB, to cover both
1123  * VRAM of the hive and system memory.
1124  *
1125  * PDB0 is static, initialized once on driver initialization.
1126  * The first n entries of PDB0 are used as PTE by setting
1127  * P bit to 1, pointing to VRAM. The n+1'th entry points
1128  * to a big PTB covering system memory.
1129  *
1130  */
1131 void amdgpu_gmc_init_pdb0(struct amdgpu_device *adev)
1132 {
1133 	int i;
1134 	uint64_t flags = adev->gart.gart_pte_flags; //TODO it is UC. explore NC/RW?
1135 	/* Each PDE0 (used as PTE) covers (2^vmid0_page_table_block_size)*2M
1136 	 */
1137 	u64 vram_size = adev->gmc.xgmi.node_segment_size * adev->gmc.xgmi.num_physical_nodes;
1138 	u64 pde0_page_size = (1ULL<<adev->gmc.vmid0_page_table_block_size)<<21;
1139 	u64 vram_addr, vram_end;
1140 	u64 gart_ptb_gpu_pa = amdgpu_gmc_vram_pa(adev, adev->gart.bo);
1141 	int idx;
1142 
1143 	if (!drm_dev_enter(adev_to_drm(adev), &idx))
1144 		return;
1145 
1146 	flags |= AMDGPU_PTE_VALID | AMDGPU_PTE_READABLE;
1147 	flags |= AMDGPU_PTE_WRITEABLE;
1148 	flags |= AMDGPU_PTE_SNOOPED;
1149 	flags |= AMDGPU_PTE_FRAG((adev->gmc.vmid0_page_table_block_size + 9*1));
1150 	flags |= AMDGPU_PDE_PTE_FLAG(adev);
1151 
1152 	vram_addr = adev->vm_manager.vram_base_offset;
1153 	if (!amdgpu_virt_xgmi_migrate_enabled(adev))
1154 		vram_addr -= adev->gmc.xgmi.physical_node_id * adev->gmc.xgmi.node_segment_size;
1155 	vram_end = vram_addr + vram_size;
1156 
1157 	/* The first n PDE0 entries are used as PTE,
1158 	 * pointing to vram
1159 	 */
1160 	for (i = 0; vram_addr < vram_end; i++, vram_addr += pde0_page_size)
1161 		amdgpu_gmc_set_pte_pde(adev, adev->gmc.ptr_pdb0, i, vram_addr, flags);
1162 
1163 	/* The n+1'th PDE0 entry points to a huge
1164 	 * PTB who has more than 512 entries each
1165 	 * pointing to a 4K system page
1166 	 */
1167 	flags = AMDGPU_PTE_VALID;
1168 	flags |= AMDGPU_PTE_SNOOPED | AMDGPU_PDE_BFS_FLAG(adev, 0);
1169 	/* Requires gart_ptb_gpu_pa to be 4K aligned */
1170 	amdgpu_gmc_set_pte_pde(adev, adev->gmc.ptr_pdb0, i, gart_ptb_gpu_pa, flags);
1171 	drm_dev_exit(idx);
1172 }
1173 
1174 /**
1175  * amdgpu_gmc_vram_mc2pa - calculate vram buffer's physical address from MC
1176  * address
1177  *
1178  * @adev: amdgpu_device pointer
1179  * @mc_addr: MC address of buffer
1180  */
1181 uint64_t amdgpu_gmc_vram_mc2pa(struct amdgpu_device *adev, uint64_t mc_addr)
1182 {
1183 	return mc_addr - adev->gmc.vram_start + adev->vm_manager.vram_base_offset;
1184 }
1185 
1186 /**
1187  * amdgpu_gmc_vram_pa - calculate vram buffer object's physical address from
1188  * GPU's view
1189  *
1190  * @adev: amdgpu_device pointer
1191  * @bo: amdgpu buffer object
1192  */
1193 uint64_t amdgpu_gmc_vram_pa(struct amdgpu_device *adev, struct amdgpu_bo *bo)
1194 {
1195 	return amdgpu_gmc_vram_mc2pa(adev, amdgpu_bo_gpu_offset(bo));
1196 }
1197 
1198 int amdgpu_gmc_vram_checking(struct amdgpu_device *adev)
1199 {
1200 	struct amdgpu_bo *vram_bo = NULL;
1201 	uint64_t vram_gpu = 0;
1202 	void *vram_ptr = NULL;
1203 
1204 	int ret, size = 0x100000;
1205 	uint8_t cptr[10];
1206 
1207 	ret = amdgpu_bo_create_kernel(adev, size, PAGE_SIZE,
1208 				AMDGPU_GEM_DOMAIN_VRAM,
1209 				&vram_bo,
1210 				&vram_gpu,
1211 				&vram_ptr);
1212 	if (ret)
1213 		return ret;
1214 
1215 	memset(vram_ptr, 0x86, size);
1216 	memset(cptr, 0x86, 10);
1217 
1218 	/**
1219 	 * Check the start, the mid, and the end of the memory if the content of
1220 	 * each byte is the pattern "0x86". If yes, we suppose the vram bo is
1221 	 * workable.
1222 	 *
1223 	 * Note: If check the each byte of whole 1M bo, it will cost too many
1224 	 * seconds, so here, we just pick up three parts for emulation.
1225 	 */
1226 	ret = memcmp(vram_ptr, cptr, 10);
1227 	if (ret) {
1228 		ret = -EIO;
1229 		goto release_buffer;
1230 	}
1231 
1232 	ret = memcmp(vram_ptr + (size / 2), cptr, 10);
1233 	if (ret) {
1234 		ret = -EIO;
1235 		goto release_buffer;
1236 	}
1237 
1238 	ret = memcmp(vram_ptr + size - 10, cptr, 10);
1239 	if (ret) {
1240 		ret = -EIO;
1241 		goto release_buffer;
1242 	}
1243 
1244 release_buffer:
1245 	amdgpu_bo_free_kernel(&vram_bo, &vram_gpu,
1246 			&vram_ptr);
1247 
1248 	return ret;
1249 }
1250 
1251 static const char *nps_desc[] = {
1252 	[AMDGPU_NPS1_PARTITION_MODE] = "NPS1",
1253 	[AMDGPU_NPS2_PARTITION_MODE] = "NPS2",
1254 	[AMDGPU_NPS3_PARTITION_MODE] = "NPS3",
1255 	[AMDGPU_NPS4_PARTITION_MODE] = "NPS4",
1256 	[AMDGPU_NPS6_PARTITION_MODE] = "NPS6",
1257 	[AMDGPU_NPS8_PARTITION_MODE] = "NPS8",
1258 };
1259 
1260 static ssize_t available_memory_partition_show(struct device *dev,
1261 					       struct device_attribute *addr,
1262 					       char *buf)
1263 {
1264 	struct drm_device *ddev = dev_get_drvdata(dev);
1265 	struct amdgpu_device *adev = drm_to_adev(ddev);
1266 	int size = 0, mode;
1267 	char *sep = "";
1268 
1269 	for_each_inst(mode, adev->gmc.supported_nps_modes) {
1270 		size += sysfs_emit_at(buf, size, "%s%s", sep, nps_desc[mode]);
1271 		sep = ", ";
1272 	}
1273 	size += sysfs_emit_at(buf, size, "\n");
1274 
1275 	return size;
1276 }
1277 
1278 static ssize_t current_memory_partition_store(struct device *dev,
1279 					      struct device_attribute *attr,
1280 					      const char *buf, size_t count)
1281 {
1282 	struct drm_device *ddev = dev_get_drvdata(dev);
1283 	struct amdgpu_device *adev = drm_to_adev(ddev);
1284 	enum amdgpu_memory_partition mode;
1285 	struct amdgpu_hive_info *hive;
1286 	int i;
1287 
1288 	mode = UNKNOWN_MEMORY_PARTITION_MODE;
1289 	for_each_inst(i, adev->gmc.supported_nps_modes) {
1290 		if (!strncasecmp(nps_desc[i], buf, strlen(nps_desc[i]))) {
1291 			mode = i;
1292 			break;
1293 		}
1294 	}
1295 
1296 	if (mode == UNKNOWN_MEMORY_PARTITION_MODE)
1297 		return -EINVAL;
1298 
1299 	if (mode == adev->gmc.gmc_funcs->query_mem_partition_mode(adev)) {
1300 		dev_info(
1301 			adev->dev,
1302 			"requested NPS mode is same as current NPS mode, skipping\n");
1303 		return count;
1304 	}
1305 
1306 	/* If device is part of hive, all devices in the hive should request the
1307 	 * same mode. Hence store the requested mode in hive.
1308 	 */
1309 	hive = amdgpu_get_xgmi_hive(adev);
1310 	if (hive) {
1311 		atomic_set(&hive->requested_nps_mode, mode);
1312 		amdgpu_put_xgmi_hive(hive);
1313 	} else {
1314 		adev->gmc.requested_nps_mode = mode;
1315 	}
1316 
1317 	dev_info(
1318 		adev->dev,
1319 		"NPS mode change requested, please remove and reload the driver\n");
1320 
1321 	return count;
1322 }
1323 
1324 static ssize_t current_memory_partition_show(
1325 	struct device *dev, struct device_attribute *addr, char *buf)
1326 {
1327 	struct drm_device *ddev = dev_get_drvdata(dev);
1328 	struct amdgpu_device *adev = drm_to_adev(ddev);
1329 	enum amdgpu_memory_partition mode;
1330 
1331 	/* Only minimal precaution taken to reject requests while in reset */
1332 	if (amdgpu_in_reset(adev))
1333 		return -EPERM;
1334 
1335 	mode = adev->gmc.gmc_funcs->query_mem_partition_mode(adev);
1336 	if ((mode >= ARRAY_SIZE(nps_desc)) ||
1337 	    (BIT(mode) & AMDGPU_ALL_NPS_MASK) != BIT(mode))
1338 		return sysfs_emit(buf, "UNKNOWN\n");
1339 
1340 	return sysfs_emit(buf, "%s\n", nps_desc[mode]);
1341 }
1342 
1343 static DEVICE_ATTR_RW(current_memory_partition);
1344 static DEVICE_ATTR_RO(available_memory_partition);
1345 
1346 int amdgpu_gmc_sysfs_init(struct amdgpu_device *adev)
1347 {
1348 	bool nps_switch_support;
1349 	int r = 0;
1350 
1351 	if (!adev->gmc.gmc_funcs->query_mem_partition_mode)
1352 		return 0;
1353 
1354 	nps_switch_support = (hweight32(adev->gmc.supported_nps_modes &
1355 					AMDGPU_ALL_NPS_MASK) > 1);
1356 	if (!nps_switch_support)
1357 		dev_attr_current_memory_partition.attr.mode &=
1358 			~(S_IWUSR | S_IWGRP | S_IWOTH);
1359 	else
1360 		r = device_create_file(adev->dev,
1361 				       &dev_attr_available_memory_partition);
1362 
1363 	if (r)
1364 		return r;
1365 
1366 	return device_create_file(adev->dev,
1367 				  &dev_attr_current_memory_partition);
1368 }
1369 
1370 void amdgpu_gmc_sysfs_fini(struct amdgpu_device *adev)
1371 {
1372 	if (!adev->gmc.gmc_funcs->query_mem_partition_mode)
1373 		return;
1374 
1375 	device_remove_file(adev->dev, &dev_attr_current_memory_partition);
1376 	device_remove_file(adev->dev, &dev_attr_available_memory_partition);
1377 }
1378 
1379 int amdgpu_gmc_get_nps_memranges(struct amdgpu_device *adev,
1380 				 struct amdgpu_mem_partition_info *mem_ranges,
1381 				 uint8_t *exp_ranges)
1382 {
1383 	struct amdgpu_gmc_memrange ranges[AMDGPU_MAX_MEM_RANGES];
1384 	int range_cnt, ret, i, j;
1385 	uint32_t nps_type;
1386 	bool refresh;
1387 
1388 	if (!mem_ranges || !exp_ranges)
1389 		return -EINVAL;
1390 	range_cnt = AMDGPU_MAX_MEM_RANGES;
1391 	refresh = (adev->init_lvl->level != AMDGPU_INIT_LEVEL_MINIMAL_XGMI) &&
1392 		  (adev->gmc.reset_flags & AMDGPU_GMC_INIT_RESET_NPS);
1393 	ret = amdgpu_discovery_get_nps_info(adev, &nps_type, ranges, &range_cnt,
1394 					    refresh);
1395 
1396 	if (ret)
1397 		return ret;
1398 
1399 	/* TODO: For now, expect ranges and partition count to be the same.
1400 	 * Adjust if there are holes expected in any NPS domain.
1401 	 */
1402 	if (*exp_ranges && (range_cnt != *exp_ranges)) {
1403 		dev_warn(
1404 			adev->dev,
1405 			"NPS config mismatch - expected ranges: %d discovery - nps mode: %d, nps ranges: %d",
1406 			*exp_ranges, nps_type, range_cnt);
1407 		ret = -EINVAL;
1408 		goto err;
1409 	}
1410 
1411 	for (i = 0; i < range_cnt; ++i) {
1412 		if (ranges[i].base_address >= ranges[i].limit_address) {
1413 			dev_warn(
1414 				adev->dev,
1415 				"Invalid NPS range - nps mode: %d, range[%d]: base: %llx limit: %llx",
1416 				nps_type, i, ranges[i].base_address,
1417 				ranges[i].limit_address);
1418 			ret = -EINVAL;
1419 			goto err;
1420 		}
1421 
1422 		/* Check for overlaps, not expecting any now */
1423 		for (j = i - 1; j >= 0; j--) {
1424 			if (max(ranges[j].base_address,
1425 				ranges[i].base_address) <=
1426 			    min(ranges[j].limit_address,
1427 				ranges[i].limit_address)) {
1428 				dev_warn(
1429 					adev->dev,
1430 					"overlapping ranges detected [ %llx - %llx ] | [%llx - %llx]",
1431 					ranges[j].base_address,
1432 					ranges[j].limit_address,
1433 					ranges[i].base_address,
1434 					ranges[i].limit_address);
1435 				ret = -EINVAL;
1436 				goto err;
1437 			}
1438 		}
1439 
1440 		mem_ranges[i].range.fpfn =
1441 			(ranges[i].base_address -
1442 			 adev->vm_manager.vram_base_offset) >>
1443 			AMDGPU_GPU_PAGE_SHIFT;
1444 		mem_ranges[i].range.lpfn =
1445 			(ranges[i].limit_address -
1446 			 adev->vm_manager.vram_base_offset) >>
1447 			AMDGPU_GPU_PAGE_SHIFT;
1448 		mem_ranges[i].size =
1449 			ranges[i].limit_address - ranges[i].base_address + 1;
1450 	}
1451 
1452 	if (!*exp_ranges)
1453 		*exp_ranges = range_cnt;
1454 err:
1455 	return ret;
1456 }
1457 
1458 int amdgpu_gmc_request_memory_partition(struct amdgpu_device *adev,
1459 					int nps_mode)
1460 {
1461 	/* Not supported on VF devices and APUs */
1462 	if (amdgpu_sriov_vf(adev) || (adev->flags & AMD_IS_APU))
1463 		return -EOPNOTSUPP;
1464 
1465 	if (!adev->psp.funcs) {
1466 		dev_err(adev->dev,
1467 			"PSP interface not available for nps mode change request");
1468 		return -EINVAL;
1469 	}
1470 
1471 	return psp_memory_partition(&adev->psp, nps_mode);
1472 }
1473 
1474 static inline bool amdgpu_gmc_need_nps_switch_req(struct amdgpu_device *adev,
1475 						  int req_nps_mode,
1476 						  int cur_nps_mode)
1477 {
1478 	return (((BIT(req_nps_mode) & adev->gmc.supported_nps_modes) ==
1479 			BIT(req_nps_mode)) &&
1480 		req_nps_mode != cur_nps_mode);
1481 }
1482 
1483 void amdgpu_gmc_prepare_nps_mode_change(struct amdgpu_device *adev)
1484 {
1485 	int req_nps_mode, cur_nps_mode, r;
1486 	struct amdgpu_hive_info *hive;
1487 
1488 	if (amdgpu_sriov_vf(adev) || !adev->gmc.supported_nps_modes ||
1489 	    !adev->gmc.gmc_funcs->request_mem_partition_mode)
1490 		return;
1491 
1492 	cur_nps_mode = adev->gmc.gmc_funcs->query_mem_partition_mode(adev);
1493 	hive = amdgpu_get_xgmi_hive(adev);
1494 	if (hive) {
1495 		req_nps_mode = atomic_read(&hive->requested_nps_mode);
1496 		if (!amdgpu_gmc_need_nps_switch_req(adev, req_nps_mode,
1497 						    cur_nps_mode)) {
1498 			amdgpu_put_xgmi_hive(hive);
1499 			return;
1500 		}
1501 		r = amdgpu_xgmi_request_nps_change(adev, hive, req_nps_mode);
1502 		amdgpu_put_xgmi_hive(hive);
1503 		goto out;
1504 	}
1505 
1506 	req_nps_mode = adev->gmc.requested_nps_mode;
1507 	if (!amdgpu_gmc_need_nps_switch_req(adev, req_nps_mode, cur_nps_mode))
1508 		return;
1509 
1510 	/* even if this fails, we should let driver unload w/o blocking */
1511 	r = adev->gmc.gmc_funcs->request_mem_partition_mode(adev, req_nps_mode);
1512 out:
1513 	if (r)
1514 		dev_err(adev->dev, "NPS mode change request failed\n");
1515 	else
1516 		dev_info(
1517 			adev->dev,
1518 			"NPS mode change request done, reload driver to complete the change\n");
1519 }
1520 
1521 bool amdgpu_gmc_need_reset_on_init(struct amdgpu_device *adev)
1522 {
1523 	if (adev->gmc.gmc_funcs->need_reset_on_init)
1524 		return adev->gmc.gmc_funcs->need_reset_on_init(adev);
1525 
1526 	return false;
1527 }
1528 
1529 enum amdgpu_memory_partition
1530 amdgpu_gmc_get_vf_memory_partition(struct amdgpu_device *adev)
1531 {
1532 	switch (adev->gmc.num_mem_partitions) {
1533 	case 0:
1534 		return UNKNOWN_MEMORY_PARTITION_MODE;
1535 	case 1:
1536 		return AMDGPU_NPS1_PARTITION_MODE;
1537 	case 2:
1538 		return AMDGPU_NPS2_PARTITION_MODE;
1539 	case 4:
1540 		return AMDGPU_NPS4_PARTITION_MODE;
1541 	case 8:
1542 		return AMDGPU_NPS8_PARTITION_MODE;
1543 	default:
1544 		return AMDGPU_NPS1_PARTITION_MODE;
1545 	}
1546 }
1547 
1548 enum amdgpu_memory_partition
1549 amdgpu_gmc_get_memory_partition(struct amdgpu_device *adev, u32 *supp_modes)
1550 {
1551 	enum amdgpu_memory_partition mode = UNKNOWN_MEMORY_PARTITION_MODE;
1552 
1553 	if (adev->nbio.funcs &&
1554 	    adev->nbio.funcs->get_memory_partition_mode)
1555 		mode = adev->nbio.funcs->get_memory_partition_mode(adev,
1556 								   supp_modes);
1557 	else
1558 		dev_warn(adev->dev, "memory partition mode query is not supported\n");
1559 
1560 	return mode;
1561 }
1562 
1563 enum amdgpu_memory_partition
1564 amdgpu_gmc_query_memory_partition(struct amdgpu_device *adev)
1565 {
1566 	if (amdgpu_sriov_vf(adev))
1567 		return amdgpu_gmc_get_vf_memory_partition(adev);
1568 	else
1569 		return amdgpu_gmc_get_memory_partition(adev, NULL);
1570 }
1571 
1572 static bool amdgpu_gmc_validate_partition_info(struct amdgpu_device *adev)
1573 {
1574 	enum amdgpu_memory_partition mode;
1575 	u32 supp_modes;
1576 	bool valid;
1577 
1578 	mode = amdgpu_gmc_get_memory_partition(adev, &supp_modes);
1579 
1580 	/* Mode detected by hardware not present in supported modes */
1581 	if ((mode != UNKNOWN_MEMORY_PARTITION_MODE) &&
1582 	    !(BIT(mode - 1) & supp_modes))
1583 		return false;
1584 
1585 	switch (mode) {
1586 	case UNKNOWN_MEMORY_PARTITION_MODE:
1587 	case AMDGPU_NPS1_PARTITION_MODE:
1588 		valid = (adev->gmc.num_mem_partitions == 1);
1589 		break;
1590 	case AMDGPU_NPS2_PARTITION_MODE:
1591 		valid = (adev->gmc.num_mem_partitions == 2);
1592 		break;
1593 	case AMDGPU_NPS4_PARTITION_MODE:
1594 		valid = (adev->gmc.num_mem_partitions == 3 ||
1595 			 adev->gmc.num_mem_partitions == 4);
1596 		break;
1597 	case AMDGPU_NPS8_PARTITION_MODE:
1598 		valid = (adev->gmc.num_mem_partitions == 8);
1599 		break;
1600 	default:
1601 		valid = false;
1602 	}
1603 
1604 	return valid;
1605 }
1606 
1607 static bool amdgpu_gmc_is_node_present(int *node_ids, int num_ids, int nid)
1608 {
1609 	int i;
1610 
1611 	/* Check if node with id 'nid' is present in 'node_ids' array */
1612 	for (i = 0; i < num_ids; ++i)
1613 		if (node_ids[i] == nid)
1614 			return true;
1615 
1616 	return false;
1617 }
1618 
1619 static void
1620 amdgpu_gmc_init_acpi_mem_ranges(struct amdgpu_device *adev,
1621 				struct amdgpu_mem_partition_info *mem_ranges)
1622 {
1623 	struct amdgpu_numa_info numa_info;
1624 	int node_ids[AMDGPU_MAX_MEM_RANGES];
1625 	int num_ranges = 0, ret;
1626 	int num_xcc, xcc_id;
1627 	uint32_t xcc_mask;
1628 
1629 	num_xcc = NUM_XCC(adev->gfx.xcc_mask);
1630 	xcc_mask = (1U << num_xcc) - 1;
1631 
1632 	for_each_inst(xcc_id, xcc_mask)	{
1633 		ret = amdgpu_acpi_get_mem_info(adev, xcc_id, &numa_info);
1634 		if (ret)
1635 			continue;
1636 
1637 		if (numa_info.nid == NUMA_NO_NODE) {
1638 			mem_ranges[0].size = numa_info.size;
1639 			mem_ranges[0].numa.node = numa_info.nid;
1640 			num_ranges = 1;
1641 			break;
1642 		}
1643 
1644 		if (amdgpu_gmc_is_node_present(node_ids, num_ranges,
1645 					     numa_info.nid))
1646 			continue;
1647 
1648 		node_ids[num_ranges] = numa_info.nid;
1649 		mem_ranges[num_ranges].numa.node = numa_info.nid;
1650 		mem_ranges[num_ranges].size = numa_info.size;
1651 		++num_ranges;
1652 	}
1653 
1654 	adev->gmc.num_mem_partitions = num_ranges;
1655 }
1656 
1657 void amdgpu_gmc_init_sw_mem_ranges(struct amdgpu_device *adev,
1658 				   struct amdgpu_mem_partition_info *mem_ranges)
1659 {
1660 	enum amdgpu_memory_partition mode;
1661 	u32 start_addr = 0, size;
1662 	int i, r, l;
1663 
1664 	mode = amdgpu_gmc_query_memory_partition(adev);
1665 
1666 	switch (mode) {
1667 	case UNKNOWN_MEMORY_PARTITION_MODE:
1668 		adev->gmc.num_mem_partitions = 0;
1669 		break;
1670 	case AMDGPU_NPS1_PARTITION_MODE:
1671 		adev->gmc.num_mem_partitions = 1;
1672 		break;
1673 	case AMDGPU_NPS2_PARTITION_MODE:
1674 		adev->gmc.num_mem_partitions = 2;
1675 		break;
1676 	case AMDGPU_NPS4_PARTITION_MODE:
1677 		if (adev->flags & AMD_IS_APU)
1678 			adev->gmc.num_mem_partitions = 3;
1679 		else
1680 			adev->gmc.num_mem_partitions = 4;
1681 		break;
1682 	case AMDGPU_NPS8_PARTITION_MODE:
1683 		adev->gmc.num_mem_partitions = 8;
1684 		break;
1685 	default:
1686 		adev->gmc.num_mem_partitions = 1;
1687 		break;
1688 	}
1689 
1690 	/* Use NPS range info, if populated */
1691 	r = amdgpu_gmc_get_nps_memranges(adev, mem_ranges,
1692 					 &adev->gmc.num_mem_partitions);
1693 	if (!r) {
1694 		l = 0;
1695 		for (i = 1; i < adev->gmc.num_mem_partitions; ++i) {
1696 			if (mem_ranges[i].range.lpfn >
1697 			    mem_ranges[i - 1].range.lpfn)
1698 				l = i;
1699 		}
1700 
1701 	} else {
1702 		if (!adev->gmc.num_mem_partitions) {
1703 			dev_warn(adev->dev,
1704 				 "Not able to detect NPS mode, fall back to NPS1\n");
1705 			adev->gmc.num_mem_partitions = 1;
1706 		}
1707 		/* Fallback to sw based calculation */
1708 		size = (adev->gmc.real_vram_size + SZ_16M) >> AMDGPU_GPU_PAGE_SHIFT;
1709 		size /= adev->gmc.num_mem_partitions;
1710 
1711 		for (i = 0; i < adev->gmc.num_mem_partitions; ++i) {
1712 			mem_ranges[i].range.fpfn = start_addr;
1713 			mem_ranges[i].size =
1714 				((u64)size << AMDGPU_GPU_PAGE_SHIFT);
1715 			mem_ranges[i].range.lpfn = start_addr + size - 1;
1716 			start_addr += size;
1717 		}
1718 
1719 		l = adev->gmc.num_mem_partitions - 1;
1720 	}
1721 
1722 	/* Adjust the last one */
1723 	mem_ranges[l].range.lpfn =
1724 		(adev->gmc.real_vram_size >> AMDGPU_GPU_PAGE_SHIFT) - 1;
1725 	mem_ranges[l].size =
1726 		adev->gmc.real_vram_size -
1727 		((u64)mem_ranges[l].range.fpfn << AMDGPU_GPU_PAGE_SHIFT);
1728 }
1729 
1730 int amdgpu_gmc_init_mem_ranges(struct amdgpu_device *adev)
1731 {
1732 	bool valid;
1733 
1734 	adev->gmc.mem_partitions = kzalloc_objs(struct amdgpu_mem_partition_info,
1735 						AMDGPU_MAX_MEM_RANGES);
1736 	if (!adev->gmc.mem_partitions)
1737 		return -ENOMEM;
1738 
1739 	if (adev->gmc.is_app_apu)
1740 		amdgpu_gmc_init_acpi_mem_ranges(adev, adev->gmc.mem_partitions);
1741 	else
1742 		amdgpu_gmc_init_sw_mem_ranges(adev, adev->gmc.mem_partitions);
1743 
1744 	if (amdgpu_sriov_vf(adev))
1745 		valid = true;
1746 	else
1747 		valid = amdgpu_gmc_validate_partition_info(adev);
1748 	if (!valid) {
1749 		/* TODO: handle invalid case */
1750 		dev_warn(adev->dev,
1751 			 "Mem ranges not matching with hardware config\n");
1752 	}
1753 
1754 	return 0;
1755 }
1756 
1757 int amdgpu_gmc_get_vram_info(struct amdgpu_device *adev,
1758 		int *vram_width, int *vram_type, int *vram_vendor)
1759 {
1760 	int ret = 0;
1761 
1762 	if (adev->flags & AMD_IS_APU)
1763 		return amdgpu_atomfirmware_get_integrated_system_info(adev,
1764 							vram_width, vram_type, vram_vendor);
1765 	switch (amdgpu_ip_version(adev, GC_HWIP, 0)) {
1766 	case IP_VERSION(12, 0, 0):
1767 	case IP_VERSION(12, 0, 1):
1768 		return amdgpu_atomfirmware_get_umc_info(adev,
1769 								vram_width, vram_type, vram_vendor);
1770 	case IP_VERSION(9, 5, 0):
1771 	case IP_VERSION(9, 4, 4):
1772 	case IP_VERSION(9, 4, 3):
1773 		ret = amdgpu_atomfirmware_get_umc_info(adev,
1774 								vram_width, vram_type, vram_vendor);
1775 		if (vram_width && !ret)
1776 			*vram_width *= hweight32(adev->aid_mask);
1777 		return ret;
1778 	default:
1779 		return amdgpu_atomfirmware_get_vram_info(adev,
1780 								vram_width, vram_type, vram_vendor);
1781 	}
1782 	return 0;
1783 }
1784