xref: /linux/drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c (revision 5b723b12301272ed3c6c99c4ad8b43a520f880ea)
1 /*
2  * Copyright 2016 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  */
23 
24 #include <linux/firmware.h>
25 #include <linux/pci.h>
26 
27 #include <drm/drm_cache.h>
28 
29 #include "amdgpu.h"
30 #include "gmc_v9_0.h"
31 #include "amdgpu_atomfirmware.h"
32 #include "amdgpu_gem.h"
33 
34 #include "gc/gc_9_0_sh_mask.h"
35 #include "dce/dce_12_0_offset.h"
36 #include "dce/dce_12_0_sh_mask.h"
37 #include "vega10_enum.h"
38 #include "mmhub/mmhub_1_0_offset.h"
39 #include "athub/athub_1_0_sh_mask.h"
40 #include "athub/athub_1_0_offset.h"
41 #include "oss/osssys_4_0_offset.h"
42 
43 #include "soc15.h"
44 #include "soc15d.h"
45 #include "soc15_common.h"
46 #include "umc/umc_6_0_sh_mask.h"
47 
48 #include "gfxhub_v1_0.h"
49 #include "mmhub_v1_0.h"
50 #include "athub_v1_0.h"
51 #include "gfxhub_v1_1.h"
52 #include "mmhub_v9_4.h"
53 #include "mmhub_v1_7.h"
54 #include "umc_v6_1.h"
55 #include "umc_v6_0.h"
56 #include "umc_v6_7.h"
57 #include "hdp_v4_0.h"
58 #include "mca_v3_0.h"
59 
60 #include "ivsrcid/vmc/irqsrcs_vmc_1_0.h"
61 
62 #include "amdgpu_ras.h"
63 #include "amdgpu_xgmi.h"
64 
65 /* add these here since we already include dce12 headers and these are for DCN */
66 #define mmHUBP0_DCSURF_PRI_VIEWPORT_DIMENSION                                                          0x055d
67 #define mmHUBP0_DCSURF_PRI_VIEWPORT_DIMENSION_BASE_IDX                                                 2
68 #define HUBP0_DCSURF_PRI_VIEWPORT_DIMENSION__PRI_VIEWPORT_WIDTH__SHIFT                                        0x0
69 #define HUBP0_DCSURF_PRI_VIEWPORT_DIMENSION__PRI_VIEWPORT_HEIGHT__SHIFT                                       0x10
70 #define HUBP0_DCSURF_PRI_VIEWPORT_DIMENSION__PRI_VIEWPORT_WIDTH_MASK                                          0x00003FFFL
71 #define HUBP0_DCSURF_PRI_VIEWPORT_DIMENSION__PRI_VIEWPORT_HEIGHT_MASK                                         0x3FFF0000L
72 #define mmDCHUBBUB_SDPIF_MMIO_CNTRL_0                                                                  0x049d
73 #define mmDCHUBBUB_SDPIF_MMIO_CNTRL_0_BASE_IDX                                                         2
74 
75 #define mmHUBP0_DCSURF_PRI_VIEWPORT_DIMENSION_DCN2                                                          0x05ea
76 #define mmHUBP0_DCSURF_PRI_VIEWPORT_DIMENSION_DCN2_BASE_IDX                                                 2
77 
78 
79 static const char *gfxhub_client_ids[] = {
80 	"CB",
81 	"DB",
82 	"IA",
83 	"WD",
84 	"CPF",
85 	"CPC",
86 	"CPG",
87 	"RLC",
88 	"TCP",
89 	"SQC (inst)",
90 	"SQC (data)",
91 	"SQG",
92 	"PA",
93 };
94 
95 static const char *mmhub_client_ids_raven[][2] = {
96 	[0][0] = "MP1",
97 	[1][0] = "MP0",
98 	[2][0] = "VCN",
99 	[3][0] = "VCNU",
100 	[4][0] = "HDP",
101 	[5][0] = "DCE",
102 	[13][0] = "UTCL2",
103 	[19][0] = "TLS",
104 	[26][0] = "OSS",
105 	[27][0] = "SDMA0",
106 	[0][1] = "MP1",
107 	[1][1] = "MP0",
108 	[2][1] = "VCN",
109 	[3][1] = "VCNU",
110 	[4][1] = "HDP",
111 	[5][1] = "XDP",
112 	[6][1] = "DBGU0",
113 	[7][1] = "DCE",
114 	[8][1] = "DCEDWB0",
115 	[9][1] = "DCEDWB1",
116 	[26][1] = "OSS",
117 	[27][1] = "SDMA0",
118 };
119 
120 static const char *mmhub_client_ids_renoir[][2] = {
121 	[0][0] = "MP1",
122 	[1][0] = "MP0",
123 	[2][0] = "HDP",
124 	[4][0] = "DCEDMC",
125 	[5][0] = "DCEVGA",
126 	[13][0] = "UTCL2",
127 	[19][0] = "TLS",
128 	[26][0] = "OSS",
129 	[27][0] = "SDMA0",
130 	[28][0] = "VCN",
131 	[29][0] = "VCNU",
132 	[30][0] = "JPEG",
133 	[0][1] = "MP1",
134 	[1][1] = "MP0",
135 	[2][1] = "HDP",
136 	[3][1] = "XDP",
137 	[6][1] = "DBGU0",
138 	[7][1] = "DCEDMC",
139 	[8][1] = "DCEVGA",
140 	[9][1] = "DCEDWB",
141 	[26][1] = "OSS",
142 	[27][1] = "SDMA0",
143 	[28][1] = "VCN",
144 	[29][1] = "VCNU",
145 	[30][1] = "JPEG",
146 };
147 
148 static const char *mmhub_client_ids_vega10[][2] = {
149 	[0][0] = "MP0",
150 	[1][0] = "UVD",
151 	[2][0] = "UVDU",
152 	[3][0] = "HDP",
153 	[13][0] = "UTCL2",
154 	[14][0] = "OSS",
155 	[15][0] = "SDMA1",
156 	[32+0][0] = "VCE0",
157 	[32+1][0] = "VCE0U",
158 	[32+2][0] = "XDMA",
159 	[32+3][0] = "DCE",
160 	[32+4][0] = "MP1",
161 	[32+14][0] = "SDMA0",
162 	[0][1] = "MP0",
163 	[1][1] = "UVD",
164 	[2][1] = "UVDU",
165 	[3][1] = "DBGU0",
166 	[4][1] = "HDP",
167 	[5][1] = "XDP",
168 	[14][1] = "OSS",
169 	[15][1] = "SDMA0",
170 	[32+0][1] = "VCE0",
171 	[32+1][1] = "VCE0U",
172 	[32+2][1] = "XDMA",
173 	[32+3][1] = "DCE",
174 	[32+4][1] = "DCEDWB",
175 	[32+5][1] = "MP1",
176 	[32+6][1] = "DBGU1",
177 	[32+14][1] = "SDMA1",
178 };
179 
180 static const char *mmhub_client_ids_vega12[][2] = {
181 	[0][0] = "MP0",
182 	[1][0] = "VCE0",
183 	[2][0] = "VCE0U",
184 	[3][0] = "HDP",
185 	[13][0] = "UTCL2",
186 	[14][0] = "OSS",
187 	[15][0] = "SDMA1",
188 	[32+0][0] = "DCE",
189 	[32+1][0] = "XDMA",
190 	[32+2][0] = "UVD",
191 	[32+3][0] = "UVDU",
192 	[32+4][0] = "MP1",
193 	[32+15][0] = "SDMA0",
194 	[0][1] = "MP0",
195 	[1][1] = "VCE0",
196 	[2][1] = "VCE0U",
197 	[3][1] = "DBGU0",
198 	[4][1] = "HDP",
199 	[5][1] = "XDP",
200 	[14][1] = "OSS",
201 	[15][1] = "SDMA0",
202 	[32+0][1] = "DCE",
203 	[32+1][1] = "DCEDWB",
204 	[32+2][1] = "XDMA",
205 	[32+3][1] = "UVD",
206 	[32+4][1] = "UVDU",
207 	[32+5][1] = "MP1",
208 	[32+6][1] = "DBGU1",
209 	[32+15][1] = "SDMA1",
210 };
211 
212 static const char *mmhub_client_ids_vega20[][2] = {
213 	[0][0] = "XDMA",
214 	[1][0] = "DCE",
215 	[2][0] = "VCE0",
216 	[3][0] = "VCE0U",
217 	[4][0] = "UVD",
218 	[5][0] = "UVD1U",
219 	[13][0] = "OSS",
220 	[14][0] = "HDP",
221 	[15][0] = "SDMA0",
222 	[32+0][0] = "UVD",
223 	[32+1][0] = "UVDU",
224 	[32+2][0] = "MP1",
225 	[32+3][0] = "MP0",
226 	[32+12][0] = "UTCL2",
227 	[32+14][0] = "SDMA1",
228 	[0][1] = "XDMA",
229 	[1][1] = "DCE",
230 	[2][1] = "DCEDWB",
231 	[3][1] = "VCE0",
232 	[4][1] = "VCE0U",
233 	[5][1] = "UVD1",
234 	[6][1] = "UVD1U",
235 	[7][1] = "DBGU0",
236 	[8][1] = "XDP",
237 	[13][1] = "OSS",
238 	[14][1] = "HDP",
239 	[15][1] = "SDMA0",
240 	[32+0][1] = "UVD",
241 	[32+1][1] = "UVDU",
242 	[32+2][1] = "DBGU1",
243 	[32+3][1] = "MP1",
244 	[32+4][1] = "MP0",
245 	[32+14][1] = "SDMA1",
246 };
247 
248 static const char *mmhub_client_ids_arcturus[][2] = {
249 	[0][0] = "DBGU1",
250 	[1][0] = "XDP",
251 	[2][0] = "MP1",
252 	[14][0] = "HDP",
253 	[171][0] = "JPEG",
254 	[172][0] = "VCN",
255 	[173][0] = "VCNU",
256 	[203][0] = "JPEG1",
257 	[204][0] = "VCN1",
258 	[205][0] = "VCN1U",
259 	[256][0] = "SDMA0",
260 	[257][0] = "SDMA1",
261 	[258][0] = "SDMA2",
262 	[259][0] = "SDMA3",
263 	[260][0] = "SDMA4",
264 	[261][0] = "SDMA5",
265 	[262][0] = "SDMA6",
266 	[263][0] = "SDMA7",
267 	[384][0] = "OSS",
268 	[0][1] = "DBGU1",
269 	[1][1] = "XDP",
270 	[2][1] = "MP1",
271 	[14][1] = "HDP",
272 	[171][1] = "JPEG",
273 	[172][1] = "VCN",
274 	[173][1] = "VCNU",
275 	[203][1] = "JPEG1",
276 	[204][1] = "VCN1",
277 	[205][1] = "VCN1U",
278 	[256][1] = "SDMA0",
279 	[257][1] = "SDMA1",
280 	[258][1] = "SDMA2",
281 	[259][1] = "SDMA3",
282 	[260][1] = "SDMA4",
283 	[261][1] = "SDMA5",
284 	[262][1] = "SDMA6",
285 	[263][1] = "SDMA7",
286 	[384][1] = "OSS",
287 };
288 
289 static const char *mmhub_client_ids_aldebaran[][2] = {
290 	[2][0] = "MP1",
291 	[3][0] = "MP0",
292 	[32+1][0] = "DBGU_IO0",
293 	[32+2][0] = "DBGU_IO2",
294 	[32+4][0] = "MPIO",
295 	[96+11][0] = "JPEG0",
296 	[96+12][0] = "VCN0",
297 	[96+13][0] = "VCNU0",
298 	[128+11][0] = "JPEG1",
299 	[128+12][0] = "VCN1",
300 	[128+13][0] = "VCNU1",
301 	[160+1][0] = "XDP",
302 	[160+14][0] = "HDP",
303 	[256+0][0] = "SDMA0",
304 	[256+1][0] = "SDMA1",
305 	[256+2][0] = "SDMA2",
306 	[256+3][0] = "SDMA3",
307 	[256+4][0] = "SDMA4",
308 	[384+0][0] = "OSS",
309 	[2][1] = "MP1",
310 	[3][1] = "MP0",
311 	[32+1][1] = "DBGU_IO0",
312 	[32+2][1] = "DBGU_IO2",
313 	[32+4][1] = "MPIO",
314 	[96+11][1] = "JPEG0",
315 	[96+12][1] = "VCN0",
316 	[96+13][1] = "VCNU0",
317 	[128+11][1] = "JPEG1",
318 	[128+12][1] = "VCN1",
319 	[128+13][1] = "VCNU1",
320 	[160+1][1] = "XDP",
321 	[160+14][1] = "HDP",
322 	[256+0][1] = "SDMA0",
323 	[256+1][1] = "SDMA1",
324 	[256+2][1] = "SDMA2",
325 	[256+3][1] = "SDMA3",
326 	[256+4][1] = "SDMA4",
327 	[384+0][1] = "OSS",
328 };
329 
330 static const struct soc15_reg_golden golden_settings_mmhub_1_0_0[] =
331 {
332 	SOC15_REG_GOLDEN_VALUE(MMHUB, 0, mmDAGB1_WRCLI2, 0x00000007, 0xfe5fe0fa),
333 	SOC15_REG_GOLDEN_VALUE(MMHUB, 0, mmMMEA1_DRAM_WR_CLI2GRP_MAP0, 0x00000030, 0x55555565)
334 };
335 
336 static const struct soc15_reg_golden golden_settings_athub_1_0_0[] =
337 {
338 	SOC15_REG_GOLDEN_VALUE(ATHUB, 0, mmRPB_ARB_CNTL, 0x0000ff00, 0x00000800),
339 	SOC15_REG_GOLDEN_VALUE(ATHUB, 0, mmRPB_ARB_CNTL2, 0x00ff00ff, 0x00080008)
340 };
341 
342 static const uint32_t ecc_umc_mcumc_ctrl_addrs[] = {
343 	(0x000143c0 + 0x00000000),
344 	(0x000143c0 + 0x00000800),
345 	(0x000143c0 + 0x00001000),
346 	(0x000143c0 + 0x00001800),
347 	(0x000543c0 + 0x00000000),
348 	(0x000543c0 + 0x00000800),
349 	(0x000543c0 + 0x00001000),
350 	(0x000543c0 + 0x00001800),
351 	(0x000943c0 + 0x00000000),
352 	(0x000943c0 + 0x00000800),
353 	(0x000943c0 + 0x00001000),
354 	(0x000943c0 + 0x00001800),
355 	(0x000d43c0 + 0x00000000),
356 	(0x000d43c0 + 0x00000800),
357 	(0x000d43c0 + 0x00001000),
358 	(0x000d43c0 + 0x00001800),
359 	(0x001143c0 + 0x00000000),
360 	(0x001143c0 + 0x00000800),
361 	(0x001143c0 + 0x00001000),
362 	(0x001143c0 + 0x00001800),
363 	(0x001543c0 + 0x00000000),
364 	(0x001543c0 + 0x00000800),
365 	(0x001543c0 + 0x00001000),
366 	(0x001543c0 + 0x00001800),
367 	(0x001943c0 + 0x00000000),
368 	(0x001943c0 + 0x00000800),
369 	(0x001943c0 + 0x00001000),
370 	(0x001943c0 + 0x00001800),
371 	(0x001d43c0 + 0x00000000),
372 	(0x001d43c0 + 0x00000800),
373 	(0x001d43c0 + 0x00001000),
374 	(0x001d43c0 + 0x00001800),
375 };
376 
377 static const uint32_t ecc_umc_mcumc_ctrl_mask_addrs[] = {
378 	(0x000143e0 + 0x00000000),
379 	(0x000143e0 + 0x00000800),
380 	(0x000143e0 + 0x00001000),
381 	(0x000143e0 + 0x00001800),
382 	(0x000543e0 + 0x00000000),
383 	(0x000543e0 + 0x00000800),
384 	(0x000543e0 + 0x00001000),
385 	(0x000543e0 + 0x00001800),
386 	(0x000943e0 + 0x00000000),
387 	(0x000943e0 + 0x00000800),
388 	(0x000943e0 + 0x00001000),
389 	(0x000943e0 + 0x00001800),
390 	(0x000d43e0 + 0x00000000),
391 	(0x000d43e0 + 0x00000800),
392 	(0x000d43e0 + 0x00001000),
393 	(0x000d43e0 + 0x00001800),
394 	(0x001143e0 + 0x00000000),
395 	(0x001143e0 + 0x00000800),
396 	(0x001143e0 + 0x00001000),
397 	(0x001143e0 + 0x00001800),
398 	(0x001543e0 + 0x00000000),
399 	(0x001543e0 + 0x00000800),
400 	(0x001543e0 + 0x00001000),
401 	(0x001543e0 + 0x00001800),
402 	(0x001943e0 + 0x00000000),
403 	(0x001943e0 + 0x00000800),
404 	(0x001943e0 + 0x00001000),
405 	(0x001943e0 + 0x00001800),
406 	(0x001d43e0 + 0x00000000),
407 	(0x001d43e0 + 0x00000800),
408 	(0x001d43e0 + 0x00001000),
409 	(0x001d43e0 + 0x00001800),
410 };
411 
412 static int gmc_v9_0_ecc_interrupt_state(struct amdgpu_device *adev,
413 		struct amdgpu_irq_src *src,
414 		unsigned type,
415 		enum amdgpu_interrupt_state state)
416 {
417 	u32 bits, i, tmp, reg;
418 
419 	/* Devices newer then VEGA10/12 shall have these programming
420 	     sequences performed by PSP BL */
421 	if (adev->asic_type >= CHIP_VEGA20)
422 		return 0;
423 
424 	bits = 0x7f;
425 
426 	switch (state) {
427 	case AMDGPU_IRQ_STATE_DISABLE:
428 		for (i = 0; i < ARRAY_SIZE(ecc_umc_mcumc_ctrl_addrs); i++) {
429 			reg = ecc_umc_mcumc_ctrl_addrs[i];
430 			tmp = RREG32(reg);
431 			tmp &= ~bits;
432 			WREG32(reg, tmp);
433 		}
434 		for (i = 0; i < ARRAY_SIZE(ecc_umc_mcumc_ctrl_mask_addrs); i++) {
435 			reg = ecc_umc_mcumc_ctrl_mask_addrs[i];
436 			tmp = RREG32(reg);
437 			tmp &= ~bits;
438 			WREG32(reg, tmp);
439 		}
440 		break;
441 	case AMDGPU_IRQ_STATE_ENABLE:
442 		for (i = 0; i < ARRAY_SIZE(ecc_umc_mcumc_ctrl_addrs); i++) {
443 			reg = ecc_umc_mcumc_ctrl_addrs[i];
444 			tmp = RREG32(reg);
445 			tmp |= bits;
446 			WREG32(reg, tmp);
447 		}
448 		for (i = 0; i < ARRAY_SIZE(ecc_umc_mcumc_ctrl_mask_addrs); i++) {
449 			reg = ecc_umc_mcumc_ctrl_mask_addrs[i];
450 			tmp = RREG32(reg);
451 			tmp |= bits;
452 			WREG32(reg, tmp);
453 		}
454 		break;
455 	default:
456 		break;
457 	}
458 
459 	return 0;
460 }
461 
462 static int gmc_v9_0_vm_fault_interrupt_state(struct amdgpu_device *adev,
463 					struct amdgpu_irq_src *src,
464 					unsigned type,
465 					enum amdgpu_interrupt_state state)
466 {
467 	struct amdgpu_vmhub *hub;
468 	u32 tmp, reg, bits, i, j;
469 
470 	bits = VM_CONTEXT1_CNTL__RANGE_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
471 		VM_CONTEXT1_CNTL__DUMMY_PAGE_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
472 		VM_CONTEXT1_CNTL__PDE0_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
473 		VM_CONTEXT1_CNTL__VALID_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
474 		VM_CONTEXT1_CNTL__READ_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
475 		VM_CONTEXT1_CNTL__WRITE_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
476 		VM_CONTEXT1_CNTL__EXECUTE_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK;
477 
478 	switch (state) {
479 	case AMDGPU_IRQ_STATE_DISABLE:
480 		for (j = 0; j < adev->num_vmhubs; j++) {
481 			hub = &adev->vmhub[j];
482 			for (i = 0; i < 16; i++) {
483 				reg = hub->vm_context0_cntl + i;
484 
485 				if (j == AMDGPU_GFXHUB_0)
486 					tmp = RREG32_SOC15_IP(GC, reg);
487 				else
488 					tmp = RREG32_SOC15_IP(MMHUB, reg);
489 
490 				tmp &= ~bits;
491 
492 				if (j == AMDGPU_GFXHUB_0)
493 					WREG32_SOC15_IP(GC, reg, tmp);
494 				else
495 					WREG32_SOC15_IP(MMHUB, reg, tmp);
496 			}
497 		}
498 		break;
499 	case AMDGPU_IRQ_STATE_ENABLE:
500 		for (j = 0; j < adev->num_vmhubs; j++) {
501 			hub = &adev->vmhub[j];
502 			for (i = 0; i < 16; i++) {
503 				reg = hub->vm_context0_cntl + i;
504 
505 				if (j == AMDGPU_GFXHUB_0)
506 					tmp = RREG32_SOC15_IP(GC, reg);
507 				else
508 					tmp = RREG32_SOC15_IP(MMHUB, reg);
509 
510 				tmp |= bits;
511 
512 				if (j == AMDGPU_GFXHUB_0)
513 					WREG32_SOC15_IP(GC, reg, tmp);
514 				else
515 					WREG32_SOC15_IP(MMHUB, reg, tmp);
516 			}
517 		}
518 		break;
519 	default:
520 		break;
521 	}
522 
523 	return 0;
524 }
525 
526 static int gmc_v9_0_process_interrupt(struct amdgpu_device *adev,
527 				      struct amdgpu_irq_src *source,
528 				      struct amdgpu_iv_entry *entry)
529 {
530 	bool retry_fault = !!(entry->src_data[1] & 0x80);
531 	bool write_fault = !!(entry->src_data[1] & 0x20);
532 	uint32_t status = 0, cid = 0, rw = 0;
533 	struct amdgpu_task_info task_info;
534 	struct amdgpu_vmhub *hub;
535 	const char *mmhub_cid;
536 	const char *hub_name;
537 	u64 addr;
538 
539 	addr = (u64)entry->src_data[0] << 12;
540 	addr |= ((u64)entry->src_data[1] & 0xf) << 44;
541 
542 	if (retry_fault) {
543 		/* Returning 1 here also prevents sending the IV to the KFD */
544 
545 		/* Process it onyl if it's the first fault for this address */
546 		if (entry->ih != &adev->irq.ih_soft &&
547 		    amdgpu_gmc_filter_faults(adev, entry->ih, addr, entry->pasid,
548 					     entry->timestamp))
549 			return 1;
550 
551 		/* Delegate it to a different ring if the hardware hasn't
552 		 * already done it.
553 		 */
554 		if (entry->ih == &adev->irq.ih) {
555 			amdgpu_irq_delegate(adev, entry, 8);
556 			return 1;
557 		}
558 
559 		/* Try to handle the recoverable page faults by filling page
560 		 * tables
561 		 */
562 		if (amdgpu_vm_handle_fault(adev, entry->pasid, addr, write_fault))
563 			return 1;
564 	}
565 
566 	if (!printk_ratelimit())
567 		return 0;
568 
569 	if (entry->client_id == SOC15_IH_CLIENTID_VMC) {
570 		hub_name = "mmhub0";
571 		hub = &adev->vmhub[AMDGPU_MMHUB_0];
572 	} else if (entry->client_id == SOC15_IH_CLIENTID_VMC1) {
573 		hub_name = "mmhub1";
574 		hub = &adev->vmhub[AMDGPU_MMHUB_1];
575 	} else {
576 		hub_name = "gfxhub0";
577 		hub = &adev->vmhub[AMDGPU_GFXHUB_0];
578 	}
579 
580 	memset(&task_info, 0, sizeof(struct amdgpu_task_info));
581 	amdgpu_vm_get_task_info(adev, entry->pasid, &task_info);
582 
583 	dev_err(adev->dev,
584 		"[%s] %s page fault (src_id:%u ring:%u vmid:%u "
585 		"pasid:%u, for process %s pid %d thread %s pid %d)\n",
586 		hub_name, retry_fault ? "retry" : "no-retry",
587 		entry->src_id, entry->ring_id, entry->vmid,
588 		entry->pasid, task_info.process_name, task_info.tgid,
589 		task_info.task_name, task_info.pid);
590 	dev_err(adev->dev, "  in page starting at address 0x%016llx from IH client 0x%x (%s)\n",
591 		addr, entry->client_id,
592 		soc15_ih_clientid_name[entry->client_id]);
593 
594 	if (amdgpu_sriov_vf(adev))
595 		return 0;
596 
597 	/*
598 	 * Issue a dummy read to wait for the status register to
599 	 * be updated to avoid reading an incorrect value due to
600 	 * the new fast GRBM interface.
601 	 */
602 	if ((entry->vmid_src == AMDGPU_GFXHUB_0) &&
603 	    (adev->ip_versions[GC_HWIP][0] < IP_VERSION(9, 4, 2)))
604 		RREG32(hub->vm_l2_pro_fault_status);
605 
606 	status = RREG32(hub->vm_l2_pro_fault_status);
607 	cid = REG_GET_FIELD(status, VM_L2_PROTECTION_FAULT_STATUS, CID);
608 	rw = REG_GET_FIELD(status, VM_L2_PROTECTION_FAULT_STATUS, RW);
609 	WREG32_P(hub->vm_l2_pro_fault_cntl, 1, ~1);
610 
611 
612 	dev_err(adev->dev,
613 		"VM_L2_PROTECTION_FAULT_STATUS:0x%08X\n",
614 		status);
615 	if (hub == &adev->vmhub[AMDGPU_GFXHUB_0]) {
616 		dev_err(adev->dev, "\t Faulty UTCL2 client ID: %s (0x%x)\n",
617 			cid >= ARRAY_SIZE(gfxhub_client_ids) ? "unknown" :
618 			gfxhub_client_ids[cid],
619 			cid);
620 	} else {
621 		switch (adev->ip_versions[MMHUB_HWIP][0]) {
622 		case IP_VERSION(9, 0, 0):
623 			mmhub_cid = mmhub_client_ids_vega10[cid][rw];
624 			break;
625 		case IP_VERSION(9, 3, 0):
626 			mmhub_cid = mmhub_client_ids_vega12[cid][rw];
627 			break;
628 		case IP_VERSION(9, 4, 0):
629 			mmhub_cid = mmhub_client_ids_vega20[cid][rw];
630 			break;
631 		case IP_VERSION(9, 4, 1):
632 			mmhub_cid = mmhub_client_ids_arcturus[cid][rw];
633 			break;
634 		case IP_VERSION(9, 1, 0):
635 		case IP_VERSION(9, 2, 0):
636 			mmhub_cid = mmhub_client_ids_raven[cid][rw];
637 			break;
638 		case IP_VERSION(1, 5, 0):
639 		case IP_VERSION(2, 4, 0):
640 			mmhub_cid = mmhub_client_ids_renoir[cid][rw];
641 			break;
642 		case IP_VERSION(9, 4, 2):
643 			mmhub_cid = mmhub_client_ids_aldebaran[cid][rw];
644 			break;
645 		default:
646 			mmhub_cid = NULL;
647 			break;
648 		}
649 		dev_err(adev->dev, "\t Faulty UTCL2 client ID: %s (0x%x)\n",
650 			mmhub_cid ? mmhub_cid : "unknown", cid);
651 	}
652 	dev_err(adev->dev, "\t MORE_FAULTS: 0x%lx\n",
653 		REG_GET_FIELD(status,
654 		VM_L2_PROTECTION_FAULT_STATUS, MORE_FAULTS));
655 	dev_err(adev->dev, "\t WALKER_ERROR: 0x%lx\n",
656 		REG_GET_FIELD(status,
657 		VM_L2_PROTECTION_FAULT_STATUS, WALKER_ERROR));
658 	dev_err(adev->dev, "\t PERMISSION_FAULTS: 0x%lx\n",
659 		REG_GET_FIELD(status,
660 		VM_L2_PROTECTION_FAULT_STATUS, PERMISSION_FAULTS));
661 	dev_err(adev->dev, "\t MAPPING_ERROR: 0x%lx\n",
662 		REG_GET_FIELD(status,
663 		VM_L2_PROTECTION_FAULT_STATUS, MAPPING_ERROR));
664 	dev_err(adev->dev, "\t RW: 0x%x\n", rw);
665 	return 0;
666 }
667 
668 static const struct amdgpu_irq_src_funcs gmc_v9_0_irq_funcs = {
669 	.set = gmc_v9_0_vm_fault_interrupt_state,
670 	.process = gmc_v9_0_process_interrupt,
671 };
672 
673 
674 static const struct amdgpu_irq_src_funcs gmc_v9_0_ecc_funcs = {
675 	.set = gmc_v9_0_ecc_interrupt_state,
676 	.process = amdgpu_umc_process_ecc_irq,
677 };
678 
679 static void gmc_v9_0_set_irq_funcs(struct amdgpu_device *adev)
680 {
681 	adev->gmc.vm_fault.num_types = 1;
682 	adev->gmc.vm_fault.funcs = &gmc_v9_0_irq_funcs;
683 
684 	if (!amdgpu_sriov_vf(adev) &&
685 	    !adev->gmc.xgmi.connected_to_cpu) {
686 		adev->gmc.ecc_irq.num_types = 1;
687 		adev->gmc.ecc_irq.funcs = &gmc_v9_0_ecc_funcs;
688 	}
689 }
690 
691 static uint32_t gmc_v9_0_get_invalidate_req(unsigned int vmid,
692 					uint32_t flush_type)
693 {
694 	u32 req = 0;
695 
696 	req = REG_SET_FIELD(req, VM_INVALIDATE_ENG0_REQ,
697 			    PER_VMID_INVALIDATE_REQ, 1 << vmid);
698 	req = REG_SET_FIELD(req, VM_INVALIDATE_ENG0_REQ, FLUSH_TYPE, flush_type);
699 	req = REG_SET_FIELD(req, VM_INVALIDATE_ENG0_REQ, INVALIDATE_L2_PTES, 1);
700 	req = REG_SET_FIELD(req, VM_INVALIDATE_ENG0_REQ, INVALIDATE_L2_PDE0, 1);
701 	req = REG_SET_FIELD(req, VM_INVALIDATE_ENG0_REQ, INVALIDATE_L2_PDE1, 1);
702 	req = REG_SET_FIELD(req, VM_INVALIDATE_ENG0_REQ, INVALIDATE_L2_PDE2, 1);
703 	req = REG_SET_FIELD(req, VM_INVALIDATE_ENG0_REQ, INVALIDATE_L1_PTES, 1);
704 	req = REG_SET_FIELD(req, VM_INVALIDATE_ENG0_REQ,
705 			    CLEAR_PROTECTION_FAULT_STATUS_ADDR,	0);
706 
707 	return req;
708 }
709 
710 /**
711  * gmc_v9_0_use_invalidate_semaphore - judge whether to use semaphore
712  *
713  * @adev: amdgpu_device pointer
714  * @vmhub: vmhub type
715  *
716  */
717 static bool gmc_v9_0_use_invalidate_semaphore(struct amdgpu_device *adev,
718 				       uint32_t vmhub)
719 {
720 	if (adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 2))
721 		return false;
722 
723 	return ((vmhub == AMDGPU_MMHUB_0 ||
724 		 vmhub == AMDGPU_MMHUB_1) &&
725 		(!amdgpu_sriov_vf(adev)) &&
726 		(!(!(adev->apu_flags & AMD_APU_IS_RAVEN2) &&
727 		   (adev->apu_flags & AMD_APU_IS_PICASSO))));
728 }
729 
730 static bool gmc_v9_0_get_atc_vmid_pasid_mapping_info(struct amdgpu_device *adev,
731 					uint8_t vmid, uint16_t *p_pasid)
732 {
733 	uint32_t value;
734 
735 	value = RREG32(SOC15_REG_OFFSET(ATHUB, 0, mmATC_VMID0_PASID_MAPPING)
736 		     + vmid);
737 	*p_pasid = value & ATC_VMID0_PASID_MAPPING__PASID_MASK;
738 
739 	return !!(value & ATC_VMID0_PASID_MAPPING__VALID_MASK);
740 }
741 
742 /*
743  * GART
744  * VMID 0 is the physical GPU addresses as used by the kernel.
745  * VMIDs 1-15 are used for userspace clients and are handled
746  * by the amdgpu vm/hsa code.
747  */
748 
749 /**
750  * gmc_v9_0_flush_gpu_tlb - tlb flush with certain type
751  *
752  * @adev: amdgpu_device pointer
753  * @vmid: vm instance to flush
754  * @vmhub: which hub to flush
755  * @flush_type: the flush type
756  *
757  * Flush the TLB for the requested page table using certain type.
758  */
759 static void gmc_v9_0_flush_gpu_tlb(struct amdgpu_device *adev, uint32_t vmid,
760 					uint32_t vmhub, uint32_t flush_type)
761 {
762 	bool use_semaphore = gmc_v9_0_use_invalidate_semaphore(adev, vmhub);
763 	const unsigned eng = 17;
764 	u32 j, inv_req, inv_req2, tmp;
765 	struct amdgpu_vmhub *hub;
766 
767 	BUG_ON(vmhub >= adev->num_vmhubs);
768 
769 	hub = &adev->vmhub[vmhub];
770 	if (adev->gmc.xgmi.num_physical_nodes &&
771 	    adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 0)) {
772 		/* Vega20+XGMI caches PTEs in TC and TLB. Add a
773 		 * heavy-weight TLB flush (type 2), which flushes
774 		 * both. Due to a race condition with concurrent
775 		 * memory accesses using the same TLB cache line, we
776 		 * still need a second TLB flush after this.
777 		 */
778 		inv_req = gmc_v9_0_get_invalidate_req(vmid, 2);
779 		inv_req2 = gmc_v9_0_get_invalidate_req(vmid, flush_type);
780 	} else {
781 		inv_req = gmc_v9_0_get_invalidate_req(vmid, flush_type);
782 		inv_req2 = 0;
783 	}
784 
785 	/* This is necessary for a HW workaround under SRIOV as well
786 	 * as GFXOFF under bare metal
787 	 */
788 	if (adev->gfx.kiq.ring.sched.ready &&
789 	    (amdgpu_sriov_runtime(adev) || !amdgpu_sriov_vf(adev)) &&
790 	    down_read_trylock(&adev->reset_sem)) {
791 		uint32_t req = hub->vm_inv_eng0_req + hub->eng_distance * eng;
792 		uint32_t ack = hub->vm_inv_eng0_ack + hub->eng_distance * eng;
793 
794 		amdgpu_virt_kiq_reg_write_reg_wait(adev, req, ack, inv_req,
795 						   1 << vmid);
796 		up_read(&adev->reset_sem);
797 		return;
798 	}
799 
800 	spin_lock(&adev->gmc.invalidate_lock);
801 
802 	/*
803 	 * It may lose gpuvm invalidate acknowldege state across power-gating
804 	 * off cycle, add semaphore acquire before invalidation and semaphore
805 	 * release after invalidation to avoid entering power gated state
806 	 * to WA the Issue
807 	 */
808 
809 	/* TODO: It needs to continue working on debugging with semaphore for GFXHUB as well. */
810 	if (use_semaphore) {
811 		for (j = 0; j < adev->usec_timeout; j++) {
812 			/* a read return value of 1 means semaphore acquire */
813 			if (vmhub == AMDGPU_GFXHUB_0)
814 				tmp = RREG32_SOC15_IP_NO_KIQ(GC, hub->vm_inv_eng0_sem + hub->eng_distance * eng);
815 			else
816 				tmp = RREG32_SOC15_IP_NO_KIQ(MMHUB, hub->vm_inv_eng0_sem + hub->eng_distance * eng);
817 
818 			if (tmp & 0x1)
819 				break;
820 			udelay(1);
821 		}
822 
823 		if (j >= adev->usec_timeout)
824 			DRM_ERROR("Timeout waiting for sem acquire in VM flush!\n");
825 	}
826 
827 	do {
828 		if (vmhub == AMDGPU_GFXHUB_0)
829 			WREG32_SOC15_IP_NO_KIQ(GC, hub->vm_inv_eng0_req + hub->eng_distance * eng, inv_req);
830 		else
831 			WREG32_SOC15_IP_NO_KIQ(MMHUB, hub->vm_inv_eng0_req + hub->eng_distance * eng, inv_req);
832 
833 		/*
834 		 * Issue a dummy read to wait for the ACK register to
835 		 * be cleared to avoid a false ACK due to the new fast
836 		 * GRBM interface.
837 		 */
838 		if ((vmhub == AMDGPU_GFXHUB_0) &&
839 		    (adev->ip_versions[GC_HWIP][0] < IP_VERSION(9, 4, 2)))
840 			RREG32_NO_KIQ(hub->vm_inv_eng0_req +
841 				      hub->eng_distance * eng);
842 
843 		for (j = 0; j < adev->usec_timeout; j++) {
844 			if (vmhub == AMDGPU_GFXHUB_0)
845 				tmp = RREG32_SOC15_IP_NO_KIQ(GC, hub->vm_inv_eng0_ack + hub->eng_distance * eng);
846 			else
847 				tmp = RREG32_SOC15_IP_NO_KIQ(MMHUB, hub->vm_inv_eng0_ack + hub->eng_distance * eng);
848 
849 			if (tmp & (1 << vmid))
850 				break;
851 			udelay(1);
852 		}
853 
854 		inv_req = inv_req2;
855 		inv_req2 = 0;
856 	} while (inv_req);
857 
858 	/* TODO: It needs to continue working on debugging with semaphore for GFXHUB as well. */
859 	if (use_semaphore) {
860 		/*
861 		 * add semaphore release after invalidation,
862 		 * write with 0 means semaphore release
863 		 */
864 		if (vmhub == AMDGPU_GFXHUB_0)
865 			WREG32_SOC15_IP_NO_KIQ(GC, hub->vm_inv_eng0_sem + hub->eng_distance * eng, 0);
866 		else
867 			WREG32_SOC15_IP_NO_KIQ(MMHUB, hub->vm_inv_eng0_sem + hub->eng_distance * eng, 0);
868 	}
869 
870 	spin_unlock(&adev->gmc.invalidate_lock);
871 
872 	if (j < adev->usec_timeout)
873 		return;
874 
875 	DRM_ERROR("Timeout waiting for VM flush ACK!\n");
876 }
877 
878 /**
879  * gmc_v9_0_flush_gpu_tlb_pasid - tlb flush via pasid
880  *
881  * @adev: amdgpu_device pointer
882  * @pasid: pasid to be flush
883  * @flush_type: the flush type
884  * @all_hub: flush all hubs
885  *
886  * Flush the TLB for the requested pasid.
887  */
888 static int gmc_v9_0_flush_gpu_tlb_pasid(struct amdgpu_device *adev,
889 					uint16_t pasid, uint32_t flush_type,
890 					bool all_hub)
891 {
892 	int vmid, i;
893 	signed long r;
894 	uint32_t seq;
895 	uint16_t queried_pasid;
896 	bool ret;
897 	struct amdgpu_ring *ring = &adev->gfx.kiq.ring;
898 	struct amdgpu_kiq *kiq = &adev->gfx.kiq;
899 
900 	if (amdgpu_in_reset(adev))
901 		return -EIO;
902 
903 	if (ring->sched.ready && down_read_trylock(&adev->reset_sem)) {
904 		/* Vega20+XGMI caches PTEs in TC and TLB. Add a
905 		 * heavy-weight TLB flush (type 2), which flushes
906 		 * both. Due to a race condition with concurrent
907 		 * memory accesses using the same TLB cache line, we
908 		 * still need a second TLB flush after this.
909 		 */
910 		bool vega20_xgmi_wa = (adev->gmc.xgmi.num_physical_nodes &&
911 				       adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 0));
912 		/* 2 dwords flush + 8 dwords fence */
913 		unsigned int ndw = kiq->pmf->invalidate_tlbs_size + 8;
914 
915 		if (vega20_xgmi_wa)
916 			ndw += kiq->pmf->invalidate_tlbs_size;
917 
918 		spin_lock(&adev->gfx.kiq.ring_lock);
919 		/* 2 dwords flush + 8 dwords fence */
920 		amdgpu_ring_alloc(ring, ndw);
921 		if (vega20_xgmi_wa)
922 			kiq->pmf->kiq_invalidate_tlbs(ring,
923 						      pasid, 2, all_hub);
924 		kiq->pmf->kiq_invalidate_tlbs(ring,
925 					pasid, flush_type, all_hub);
926 		r = amdgpu_fence_emit_polling(ring, &seq, MAX_KIQ_REG_WAIT);
927 		if (r) {
928 			amdgpu_ring_undo(ring);
929 			spin_unlock(&adev->gfx.kiq.ring_lock);
930 			up_read(&adev->reset_sem);
931 			return -ETIME;
932 		}
933 
934 		amdgpu_ring_commit(ring);
935 		spin_unlock(&adev->gfx.kiq.ring_lock);
936 		r = amdgpu_fence_wait_polling(ring, seq, adev->usec_timeout);
937 		if (r < 1) {
938 			dev_err(adev->dev, "wait for kiq fence error: %ld.\n", r);
939 			up_read(&adev->reset_sem);
940 			return -ETIME;
941 		}
942 		up_read(&adev->reset_sem);
943 		return 0;
944 	}
945 
946 	for (vmid = 1; vmid < 16; vmid++) {
947 
948 		ret = gmc_v9_0_get_atc_vmid_pasid_mapping_info(adev, vmid,
949 				&queried_pasid);
950 		if (ret && queried_pasid == pasid) {
951 			if (all_hub) {
952 				for (i = 0; i < adev->num_vmhubs; i++)
953 					gmc_v9_0_flush_gpu_tlb(adev, vmid,
954 							i, flush_type);
955 			} else {
956 				gmc_v9_0_flush_gpu_tlb(adev, vmid,
957 						AMDGPU_GFXHUB_0, flush_type);
958 			}
959 			break;
960 		}
961 	}
962 
963 	return 0;
964 
965 }
966 
967 static uint64_t gmc_v9_0_emit_flush_gpu_tlb(struct amdgpu_ring *ring,
968 					    unsigned vmid, uint64_t pd_addr)
969 {
970 	bool use_semaphore = gmc_v9_0_use_invalidate_semaphore(ring->adev, ring->funcs->vmhub);
971 	struct amdgpu_device *adev = ring->adev;
972 	struct amdgpu_vmhub *hub = &adev->vmhub[ring->funcs->vmhub];
973 	uint32_t req = gmc_v9_0_get_invalidate_req(vmid, 0);
974 	unsigned eng = ring->vm_inv_eng;
975 
976 	/*
977 	 * It may lose gpuvm invalidate acknowldege state across power-gating
978 	 * off cycle, add semaphore acquire before invalidation and semaphore
979 	 * release after invalidation to avoid entering power gated state
980 	 * to WA the Issue
981 	 */
982 
983 	/* TODO: It needs to continue working on debugging with semaphore for GFXHUB as well. */
984 	if (use_semaphore)
985 		/* a read return value of 1 means semaphore acuqire */
986 		amdgpu_ring_emit_reg_wait(ring,
987 					  hub->vm_inv_eng0_sem +
988 					  hub->eng_distance * eng, 0x1, 0x1);
989 
990 	amdgpu_ring_emit_wreg(ring, hub->ctx0_ptb_addr_lo32 +
991 			      (hub->ctx_addr_distance * vmid),
992 			      lower_32_bits(pd_addr));
993 
994 	amdgpu_ring_emit_wreg(ring, hub->ctx0_ptb_addr_hi32 +
995 			      (hub->ctx_addr_distance * vmid),
996 			      upper_32_bits(pd_addr));
997 
998 	amdgpu_ring_emit_reg_write_reg_wait(ring, hub->vm_inv_eng0_req +
999 					    hub->eng_distance * eng,
1000 					    hub->vm_inv_eng0_ack +
1001 					    hub->eng_distance * eng,
1002 					    req, 1 << vmid);
1003 
1004 	/* TODO: It needs to continue working on debugging with semaphore for GFXHUB as well. */
1005 	if (use_semaphore)
1006 		/*
1007 		 * add semaphore release after invalidation,
1008 		 * write with 0 means semaphore release
1009 		 */
1010 		amdgpu_ring_emit_wreg(ring, hub->vm_inv_eng0_sem +
1011 				      hub->eng_distance * eng, 0);
1012 
1013 	return pd_addr;
1014 }
1015 
1016 static void gmc_v9_0_emit_pasid_mapping(struct amdgpu_ring *ring, unsigned vmid,
1017 					unsigned pasid)
1018 {
1019 	struct amdgpu_device *adev = ring->adev;
1020 	uint32_t reg;
1021 
1022 	/* Do nothing because there's no lut register for mmhub1. */
1023 	if (ring->funcs->vmhub == AMDGPU_MMHUB_1)
1024 		return;
1025 
1026 	if (ring->funcs->vmhub == AMDGPU_GFXHUB_0)
1027 		reg = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_VMID_0_LUT) + vmid;
1028 	else
1029 		reg = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_VMID_0_LUT_MM) + vmid;
1030 
1031 	amdgpu_ring_emit_wreg(ring, reg, pasid);
1032 }
1033 
1034 /*
1035  * PTE format on VEGA 10:
1036  * 63:59 reserved
1037  * 58:57 mtype
1038  * 56 F
1039  * 55 L
1040  * 54 P
1041  * 53 SW
1042  * 52 T
1043  * 50:48 reserved
1044  * 47:12 4k physical page base address
1045  * 11:7 fragment
1046  * 6 write
1047  * 5 read
1048  * 4 exe
1049  * 3 Z
1050  * 2 snooped
1051  * 1 system
1052  * 0 valid
1053  *
1054  * PDE format on VEGA 10:
1055  * 63:59 block fragment size
1056  * 58:55 reserved
1057  * 54 P
1058  * 53:48 reserved
1059  * 47:6 physical base address of PD or PTE
1060  * 5:3 reserved
1061  * 2 C
1062  * 1 system
1063  * 0 valid
1064  */
1065 
1066 static uint64_t gmc_v9_0_map_mtype(struct amdgpu_device *adev, uint32_t flags)
1067 
1068 {
1069 	switch (flags) {
1070 	case AMDGPU_VM_MTYPE_DEFAULT:
1071 		return AMDGPU_PTE_MTYPE_VG10(MTYPE_NC);
1072 	case AMDGPU_VM_MTYPE_NC:
1073 		return AMDGPU_PTE_MTYPE_VG10(MTYPE_NC);
1074 	case AMDGPU_VM_MTYPE_WC:
1075 		return AMDGPU_PTE_MTYPE_VG10(MTYPE_WC);
1076 	case AMDGPU_VM_MTYPE_RW:
1077 		return AMDGPU_PTE_MTYPE_VG10(MTYPE_RW);
1078 	case AMDGPU_VM_MTYPE_CC:
1079 		return AMDGPU_PTE_MTYPE_VG10(MTYPE_CC);
1080 	case AMDGPU_VM_MTYPE_UC:
1081 		return AMDGPU_PTE_MTYPE_VG10(MTYPE_UC);
1082 	default:
1083 		return AMDGPU_PTE_MTYPE_VG10(MTYPE_NC);
1084 	}
1085 }
1086 
1087 static void gmc_v9_0_get_vm_pde(struct amdgpu_device *adev, int level,
1088 				uint64_t *addr, uint64_t *flags)
1089 {
1090 	if (!(*flags & AMDGPU_PDE_PTE) && !(*flags & AMDGPU_PTE_SYSTEM))
1091 		*addr = amdgpu_gmc_vram_mc2pa(adev, *addr);
1092 	BUG_ON(*addr & 0xFFFF00000000003FULL);
1093 
1094 	if (!adev->gmc.translate_further)
1095 		return;
1096 
1097 	if (level == AMDGPU_VM_PDB1) {
1098 		/* Set the block fragment size */
1099 		if (!(*flags & AMDGPU_PDE_PTE))
1100 			*flags |= AMDGPU_PDE_BFS(0x9);
1101 
1102 	} else if (level == AMDGPU_VM_PDB0) {
1103 		if (*flags & AMDGPU_PDE_PTE)
1104 			*flags &= ~AMDGPU_PDE_PTE;
1105 		else
1106 			*flags |= AMDGPU_PTE_TF;
1107 	}
1108 }
1109 
1110 static void gmc_v9_0_get_vm_pte(struct amdgpu_device *adev,
1111 				struct amdgpu_bo_va_mapping *mapping,
1112 				uint64_t *flags)
1113 {
1114 	*flags &= ~AMDGPU_PTE_EXECUTABLE;
1115 	*flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE;
1116 
1117 	*flags &= ~AMDGPU_PTE_MTYPE_VG10_MASK;
1118 	*flags |= mapping->flags & AMDGPU_PTE_MTYPE_VG10_MASK;
1119 
1120 	if (mapping->flags & AMDGPU_PTE_PRT) {
1121 		*flags |= AMDGPU_PTE_PRT;
1122 		*flags &= ~AMDGPU_PTE_VALID;
1123 	}
1124 
1125 	if ((adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 1) ||
1126 	     adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 2)) &&
1127 	    !(*flags & AMDGPU_PTE_SYSTEM) &&
1128 	    mapping->bo_va->is_xgmi)
1129 		*flags |= AMDGPU_PTE_SNOOPED;
1130 
1131 	if (adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 2))
1132 		*flags |= mapping->flags & AMDGPU_PTE_SNOOPED;
1133 }
1134 
1135 static unsigned gmc_v9_0_get_vbios_fb_size(struct amdgpu_device *adev)
1136 {
1137 	u32 d1vga_control = RREG32_SOC15(DCE, 0, mmD1VGA_CONTROL);
1138 	unsigned size;
1139 
1140 	/* TODO move to DC so GMC doesn't need to hard-code DCN registers */
1141 
1142 	if (REG_GET_FIELD(d1vga_control, D1VGA_CONTROL, D1VGA_MODE_ENABLE)) {
1143 		size = AMDGPU_VBIOS_VGA_ALLOCATION;
1144 	} else {
1145 		u32 viewport;
1146 
1147 		switch (adev->ip_versions[DCE_HWIP][0]) {
1148 		case IP_VERSION(1, 0, 0):
1149 		case IP_VERSION(1, 0, 1):
1150 			viewport = RREG32_SOC15(DCE, 0, mmHUBP0_DCSURF_PRI_VIEWPORT_DIMENSION);
1151 			size = (REG_GET_FIELD(viewport,
1152 					      HUBP0_DCSURF_PRI_VIEWPORT_DIMENSION, PRI_VIEWPORT_HEIGHT) *
1153 				REG_GET_FIELD(viewport,
1154 					      HUBP0_DCSURF_PRI_VIEWPORT_DIMENSION, PRI_VIEWPORT_WIDTH) *
1155 				4);
1156 			break;
1157 		case IP_VERSION(2, 1, 0):
1158 			viewport = RREG32_SOC15(DCE, 0, mmHUBP0_DCSURF_PRI_VIEWPORT_DIMENSION_DCN2);
1159 			size = (REG_GET_FIELD(viewport,
1160 					      HUBP0_DCSURF_PRI_VIEWPORT_DIMENSION, PRI_VIEWPORT_HEIGHT) *
1161 				REG_GET_FIELD(viewport,
1162 					      HUBP0_DCSURF_PRI_VIEWPORT_DIMENSION, PRI_VIEWPORT_WIDTH) *
1163 				4);
1164 			break;
1165 		default:
1166 			viewport = RREG32_SOC15(DCE, 0, mmSCL0_VIEWPORT_SIZE);
1167 			size = (REG_GET_FIELD(viewport, SCL0_VIEWPORT_SIZE, VIEWPORT_HEIGHT) *
1168 				REG_GET_FIELD(viewport, SCL0_VIEWPORT_SIZE, VIEWPORT_WIDTH) *
1169 				4);
1170 			break;
1171 		}
1172 	}
1173 
1174 	return size;
1175 }
1176 
1177 static const struct amdgpu_gmc_funcs gmc_v9_0_gmc_funcs = {
1178 	.flush_gpu_tlb = gmc_v9_0_flush_gpu_tlb,
1179 	.flush_gpu_tlb_pasid = gmc_v9_0_flush_gpu_tlb_pasid,
1180 	.emit_flush_gpu_tlb = gmc_v9_0_emit_flush_gpu_tlb,
1181 	.emit_pasid_mapping = gmc_v9_0_emit_pasid_mapping,
1182 	.map_mtype = gmc_v9_0_map_mtype,
1183 	.get_vm_pde = gmc_v9_0_get_vm_pde,
1184 	.get_vm_pte = gmc_v9_0_get_vm_pte,
1185 	.get_vbios_fb_size = gmc_v9_0_get_vbios_fb_size,
1186 };
1187 
1188 static void gmc_v9_0_set_gmc_funcs(struct amdgpu_device *adev)
1189 {
1190 	adev->gmc.gmc_funcs = &gmc_v9_0_gmc_funcs;
1191 }
1192 
1193 static void gmc_v9_0_set_umc_funcs(struct amdgpu_device *adev)
1194 {
1195 	switch (adev->ip_versions[UMC_HWIP][0]) {
1196 	case IP_VERSION(6, 0, 0):
1197 		adev->umc.funcs = &umc_v6_0_funcs;
1198 		break;
1199 	case IP_VERSION(6, 1, 1):
1200 		adev->umc.max_ras_err_cnt_per_query = UMC_V6_1_TOTAL_CHANNEL_NUM;
1201 		adev->umc.channel_inst_num = UMC_V6_1_CHANNEL_INSTANCE_NUM;
1202 		adev->umc.umc_inst_num = UMC_V6_1_UMC_INSTANCE_NUM;
1203 		adev->umc.channel_offs = UMC_V6_1_PER_CHANNEL_OFFSET_VG20;
1204 		adev->umc.channel_idx_tbl = &umc_v6_1_channel_idx_tbl[0][0];
1205 		adev->umc.ras = &umc_v6_1_ras;
1206 		break;
1207 	case IP_VERSION(6, 1, 2):
1208 		adev->umc.max_ras_err_cnt_per_query = UMC_V6_1_TOTAL_CHANNEL_NUM;
1209 		adev->umc.channel_inst_num = UMC_V6_1_CHANNEL_INSTANCE_NUM;
1210 		adev->umc.umc_inst_num = UMC_V6_1_UMC_INSTANCE_NUM;
1211 		adev->umc.channel_offs = UMC_V6_1_PER_CHANNEL_OFFSET_ARCT;
1212 		adev->umc.channel_idx_tbl = &umc_v6_1_channel_idx_tbl[0][0];
1213 		adev->umc.ras = &umc_v6_1_ras;
1214 		break;
1215 	case IP_VERSION(6, 7, 0):
1216 		adev->umc.max_ras_err_cnt_per_query =
1217 			UMC_V6_7_TOTAL_CHANNEL_NUM * UMC_V6_7_BAD_PAGE_NUM_PER_CHANNEL;
1218 		adev->umc.channel_inst_num = UMC_V6_7_CHANNEL_INSTANCE_NUM;
1219 		adev->umc.umc_inst_num = UMC_V6_7_UMC_INSTANCE_NUM;
1220 		adev->umc.channel_offs = UMC_V6_7_PER_CHANNEL_OFFSET;
1221 		if (!adev->gmc.xgmi.connected_to_cpu)
1222 			adev->umc.ras = &umc_v6_7_ras;
1223 		if (1 & adev->smuio.funcs->get_die_id(adev))
1224 			adev->umc.channel_idx_tbl = &umc_v6_7_channel_idx_tbl_first[0][0];
1225 		else
1226 			adev->umc.channel_idx_tbl = &umc_v6_7_channel_idx_tbl_second[0][0];
1227 		break;
1228 	default:
1229 		break;
1230 	}
1231 
1232 	if (adev->umc.ras) {
1233 		amdgpu_ras_register_ras_block(adev, &adev->umc.ras->ras_block);
1234 
1235 		strcpy(adev->umc.ras->ras_block.ras_comm.name, "umc");
1236 		adev->umc.ras->ras_block.ras_comm.block = AMDGPU_RAS_BLOCK__UMC;
1237 		adev->umc.ras->ras_block.ras_comm.type = AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE;
1238 		adev->umc.ras_if = &adev->umc.ras->ras_block.ras_comm;
1239 
1240 		/* If don't define special ras_late_init function, use default ras_late_init */
1241 		if (!adev->umc.ras->ras_block.ras_late_init)
1242 				adev->umc.ras->ras_block.ras_late_init = amdgpu_umc_ras_late_init;
1243 
1244 		/* If don't define special ras_fini function, use default ras_fini */
1245 		if (!adev->umc.ras->ras_block.ras_fini)
1246 				adev->umc.ras->ras_block.ras_fini = amdgpu_umc_ras_fini;
1247 
1248 		/* If not defined special ras_cb function, use default ras_cb */
1249 		if (!adev->umc.ras->ras_block.ras_cb)
1250 			adev->umc.ras->ras_block.ras_cb = amdgpu_umc_process_ras_data_cb;
1251 	}
1252 }
1253 
1254 static void gmc_v9_0_set_mmhub_funcs(struct amdgpu_device *adev)
1255 {
1256 	switch (adev->ip_versions[MMHUB_HWIP][0]) {
1257 	case IP_VERSION(9, 4, 1):
1258 		adev->mmhub.funcs = &mmhub_v9_4_funcs;
1259 		break;
1260 	case IP_VERSION(9, 4, 2):
1261 		adev->mmhub.funcs = &mmhub_v1_7_funcs;
1262 		break;
1263 	default:
1264 		adev->mmhub.funcs = &mmhub_v1_0_funcs;
1265 		break;
1266 	}
1267 }
1268 
1269 static void gmc_v9_0_set_mmhub_ras_funcs(struct amdgpu_device *adev)
1270 {
1271 	switch (adev->ip_versions[MMHUB_HWIP][0]) {
1272 	case IP_VERSION(9, 4, 0):
1273 		adev->mmhub.ras = &mmhub_v1_0_ras;
1274 		break;
1275 	case IP_VERSION(9, 4, 1):
1276 		adev->mmhub.ras = &mmhub_v9_4_ras;
1277 		break;
1278 	case IP_VERSION(9, 4, 2):
1279 		adev->mmhub.ras = &mmhub_v1_7_ras;
1280 		break;
1281 	default:
1282 		/* mmhub ras is not available */
1283 		break;
1284 	}
1285 
1286 	if (adev->mmhub.ras) {
1287 		amdgpu_ras_register_ras_block(adev, &adev->mmhub.ras->ras_block);
1288 
1289 		strcpy(adev->mmhub.ras->ras_block.ras_comm.name, "mmhub");
1290 		adev->mmhub.ras->ras_block.ras_comm.block = AMDGPU_RAS_BLOCK__MMHUB;
1291 		adev->mmhub.ras->ras_block.ras_comm.type = AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE;
1292 		adev->mmhub.ras_if = &adev->mmhub.ras->ras_block.ras_comm;
1293 
1294 		/* If don't define special ras_fini function, use default ras_fini */
1295 		if (!adev->mmhub.ras->ras_block.ras_fini)
1296 			adev->mmhub.ras->ras_block.ras_fini = amdgpu_mmhub_ras_fini;
1297 	}
1298 }
1299 
1300 static void gmc_v9_0_set_gfxhub_funcs(struct amdgpu_device *adev)
1301 {
1302 	adev->gfxhub.funcs = &gfxhub_v1_0_funcs;
1303 }
1304 
1305 static void gmc_v9_0_set_hdp_ras_funcs(struct amdgpu_device *adev)
1306 {
1307 	adev->hdp.ras = &hdp_v4_0_ras;
1308 	amdgpu_ras_register_ras_block(adev, &adev->hdp.ras->ras_block);
1309 	adev->hdp.ras_if = &adev->hdp.ras->ras_block.ras_comm;
1310 }
1311 
1312 static void gmc_v9_0_set_mca_funcs(struct amdgpu_device *adev)
1313 {
1314 	/* is UMC the right IP to check for MCA?  Maybe DF? */
1315 	switch (adev->ip_versions[UMC_HWIP][0]) {
1316 	case IP_VERSION(6, 7, 0):
1317 		if (!adev->gmc.xgmi.connected_to_cpu)
1318 			adev->mca.funcs = &mca_v3_0_funcs;
1319 		break;
1320 	default:
1321 		break;
1322 	}
1323 }
1324 
1325 static int gmc_v9_0_early_init(void *handle)
1326 {
1327 	int r;
1328 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1329 
1330 	/* ARCT and VEGA20 don't have XGMI defined in their IP discovery tables */
1331 	if (adev->asic_type == CHIP_VEGA20 ||
1332 	    adev->asic_type == CHIP_ARCTURUS)
1333 		adev->gmc.xgmi.supported = true;
1334 
1335 	if (adev->ip_versions[XGMI_HWIP][0] == IP_VERSION(6, 1, 0)) {
1336 		adev->gmc.xgmi.supported = true;
1337 		adev->gmc.xgmi.connected_to_cpu =
1338 			adev->smuio.funcs->is_host_gpu_xgmi_supported(adev);
1339 	}
1340 
1341 	gmc_v9_0_set_gmc_funcs(adev);
1342 	gmc_v9_0_set_irq_funcs(adev);
1343 	gmc_v9_0_set_umc_funcs(adev);
1344 	gmc_v9_0_set_mmhub_funcs(adev);
1345 	gmc_v9_0_set_mmhub_ras_funcs(adev);
1346 	gmc_v9_0_set_gfxhub_funcs(adev);
1347 	gmc_v9_0_set_hdp_ras_funcs(adev);
1348 	gmc_v9_0_set_mca_funcs(adev);
1349 
1350 	adev->gmc.shared_aperture_start = 0x2000000000000000ULL;
1351 	adev->gmc.shared_aperture_end =
1352 		adev->gmc.shared_aperture_start + (4ULL << 30) - 1;
1353 	adev->gmc.private_aperture_start = 0x1000000000000000ULL;
1354 	adev->gmc.private_aperture_end =
1355 		adev->gmc.private_aperture_start + (4ULL << 30) - 1;
1356 
1357 	r = amdgpu_gmc_ras_early_init(adev);
1358 	if (r)
1359 		return r;
1360 
1361 	return 0;
1362 }
1363 
1364 static int gmc_v9_0_late_init(void *handle)
1365 {
1366 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1367 	int r;
1368 
1369 	r = amdgpu_gmc_allocate_vm_inv_eng(adev);
1370 	if (r)
1371 		return r;
1372 
1373 	/*
1374 	 * Workaround performance drop issue with VBIOS enables partial
1375 	 * writes, while disables HBM ECC for vega10.
1376 	 */
1377 	if (!amdgpu_sriov_vf(adev) &&
1378 	    (adev->ip_versions[UMC_HWIP][0] == IP_VERSION(6, 0, 0))) {
1379 		if (!(adev->ras_enabled & (1 << AMDGPU_RAS_BLOCK__UMC))) {
1380 			if (adev->df.funcs &&
1381 			    adev->df.funcs->enable_ecc_force_par_wr_rmw)
1382 				adev->df.funcs->enable_ecc_force_par_wr_rmw(adev, false);
1383 		}
1384 	}
1385 
1386 	if (!amdgpu_persistent_edc_harvesting_supported(adev)) {
1387 		if (adev->mmhub.ras && adev->mmhub.ras->ras_block.hw_ops &&
1388 		    adev->mmhub.ras->ras_block.hw_ops->reset_ras_error_count)
1389 			adev->mmhub.ras->ras_block.hw_ops->reset_ras_error_count(adev);
1390 
1391 		if (adev->hdp.ras && adev->hdp.ras->ras_block.hw_ops &&
1392 		    adev->hdp.ras->ras_block.hw_ops->reset_ras_error_count)
1393 			adev->hdp.ras->ras_block.hw_ops->reset_ras_error_count(adev);
1394 	}
1395 
1396 	r = amdgpu_gmc_ras_late_init(adev);
1397 	if (r)
1398 		return r;
1399 
1400 	return amdgpu_irq_get(adev, &adev->gmc.vm_fault, 0);
1401 }
1402 
1403 static void gmc_v9_0_vram_gtt_location(struct amdgpu_device *adev,
1404 					struct amdgpu_gmc *mc)
1405 {
1406 	u64 base = adev->mmhub.funcs->get_fb_location(adev);
1407 
1408 	/* add the xgmi offset of the physical node */
1409 	base += adev->gmc.xgmi.physical_node_id * adev->gmc.xgmi.node_segment_size;
1410 	if (adev->gmc.xgmi.connected_to_cpu) {
1411 		amdgpu_gmc_sysvm_location(adev, mc);
1412 	} else {
1413 		amdgpu_gmc_vram_location(adev, mc, base);
1414 		amdgpu_gmc_gart_location(adev, mc);
1415 		amdgpu_gmc_agp_location(adev, mc);
1416 	}
1417 	/* base offset of vram pages */
1418 	adev->vm_manager.vram_base_offset = adev->gfxhub.funcs->get_mc_fb_offset(adev);
1419 
1420 	/* XXX: add the xgmi offset of the physical node? */
1421 	adev->vm_manager.vram_base_offset +=
1422 		adev->gmc.xgmi.physical_node_id * adev->gmc.xgmi.node_segment_size;
1423 }
1424 
1425 /**
1426  * gmc_v9_0_mc_init - initialize the memory controller driver params
1427  *
1428  * @adev: amdgpu_device pointer
1429  *
1430  * Look up the amount of vram, vram width, and decide how to place
1431  * vram and gart within the GPU's physical address space.
1432  * Returns 0 for success.
1433  */
1434 static int gmc_v9_0_mc_init(struct amdgpu_device *adev)
1435 {
1436 	int r;
1437 
1438 	/* size in MB on si */
1439 	adev->gmc.mc_vram_size =
1440 		adev->nbio.funcs->get_memsize(adev) * 1024ULL * 1024ULL;
1441 	adev->gmc.real_vram_size = adev->gmc.mc_vram_size;
1442 
1443 	if (!(adev->flags & AMD_IS_APU) &&
1444 	    !adev->gmc.xgmi.connected_to_cpu) {
1445 		r = amdgpu_device_resize_fb_bar(adev);
1446 		if (r)
1447 			return r;
1448 	}
1449 	adev->gmc.aper_base = pci_resource_start(adev->pdev, 0);
1450 	adev->gmc.aper_size = pci_resource_len(adev->pdev, 0);
1451 
1452 #ifdef CONFIG_X86_64
1453 	/*
1454 	 * AMD Accelerated Processing Platform (APP) supporting GPU-HOST xgmi
1455 	 * interface can use VRAM through here as it appears system reserved
1456 	 * memory in host address space.
1457 	 *
1458 	 * For APUs, VRAM is just the stolen system memory and can be accessed
1459 	 * directly.
1460 	 *
1461 	 * Otherwise, use the legacy Host Data Path (HDP) through PCIe BAR.
1462 	 */
1463 
1464 	/* check whether both host-gpu and gpu-gpu xgmi links exist */
1465 	if ((adev->flags & AMD_IS_APU) ||
1466 	    (adev->gmc.xgmi.supported &&
1467 	     adev->gmc.xgmi.connected_to_cpu)) {
1468 		adev->gmc.aper_base =
1469 			adev->gfxhub.funcs->get_mc_fb_offset(adev) +
1470 			adev->gmc.xgmi.physical_node_id *
1471 			adev->gmc.xgmi.node_segment_size;
1472 		adev->gmc.aper_size = adev->gmc.real_vram_size;
1473 	}
1474 
1475 #endif
1476 	/* In case the PCI BAR is larger than the actual amount of vram */
1477 	adev->gmc.visible_vram_size = adev->gmc.aper_size;
1478 	if (adev->gmc.visible_vram_size > adev->gmc.real_vram_size)
1479 		adev->gmc.visible_vram_size = adev->gmc.real_vram_size;
1480 
1481 	/* set the gart size */
1482 	if (amdgpu_gart_size == -1) {
1483 		switch (adev->ip_versions[GC_HWIP][0]) {
1484 		case IP_VERSION(9, 0, 1):  /* all engines support GPUVM */
1485 		case IP_VERSION(9, 2, 1):  /* all engines support GPUVM */
1486 		case IP_VERSION(9, 4, 0):
1487 		case IP_VERSION(9, 4, 1):
1488 		case IP_VERSION(9, 4, 2):
1489 		default:
1490 			adev->gmc.gart_size = 512ULL << 20;
1491 			break;
1492 		case IP_VERSION(9, 1, 0):   /* DCE SG support */
1493 		case IP_VERSION(9, 2, 2):   /* DCE SG support */
1494 		case IP_VERSION(9, 3, 0):
1495 			adev->gmc.gart_size = 1024ULL << 20;
1496 			break;
1497 		}
1498 	} else {
1499 		adev->gmc.gart_size = (u64)amdgpu_gart_size << 20;
1500 	}
1501 
1502 	adev->gmc.gart_size += adev->pm.smu_prv_buffer_size;
1503 
1504 	gmc_v9_0_vram_gtt_location(adev, &adev->gmc);
1505 
1506 	return 0;
1507 }
1508 
1509 static int gmc_v9_0_gart_init(struct amdgpu_device *adev)
1510 {
1511 	int r;
1512 
1513 	if (adev->gart.bo) {
1514 		WARN(1, "VEGA10 PCIE GART already initialized\n");
1515 		return 0;
1516 	}
1517 
1518 	if (adev->gmc.xgmi.connected_to_cpu) {
1519 		adev->gmc.vmid0_page_table_depth = 1;
1520 		adev->gmc.vmid0_page_table_block_size = 12;
1521 	} else {
1522 		adev->gmc.vmid0_page_table_depth = 0;
1523 		adev->gmc.vmid0_page_table_block_size = 0;
1524 	}
1525 
1526 	/* Initialize common gart structure */
1527 	r = amdgpu_gart_init(adev);
1528 	if (r)
1529 		return r;
1530 	adev->gart.table_size = adev->gart.num_gpu_pages * 8;
1531 	adev->gart.gart_pte_flags = AMDGPU_PTE_MTYPE_VG10(MTYPE_UC) |
1532 				 AMDGPU_PTE_EXECUTABLE;
1533 
1534 	r = amdgpu_gart_table_vram_alloc(adev);
1535 	if (r)
1536 		return r;
1537 
1538 	if (adev->gmc.xgmi.connected_to_cpu) {
1539 		r = amdgpu_gmc_pdb0_alloc(adev);
1540 	}
1541 
1542 	return r;
1543 }
1544 
1545 /**
1546  * gmc_v9_0_save_registers - saves regs
1547  *
1548  * @adev: amdgpu_device pointer
1549  *
1550  * This saves potential register values that should be
1551  * restored upon resume
1552  */
1553 static void gmc_v9_0_save_registers(struct amdgpu_device *adev)
1554 {
1555 	if ((adev->ip_versions[DCE_HWIP][0] == IP_VERSION(1, 0, 0)) ||
1556 	    (adev->ip_versions[DCE_HWIP][0] == IP_VERSION(1, 0, 1)))
1557 		adev->gmc.sdpif_register = RREG32_SOC15(DCE, 0, mmDCHUBBUB_SDPIF_MMIO_CNTRL_0);
1558 }
1559 
1560 static int gmc_v9_0_sw_init(void *handle)
1561 {
1562 	int r, vram_width = 0, vram_type = 0, vram_vendor = 0;
1563 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1564 
1565 	adev->gfxhub.funcs->init(adev);
1566 
1567 	adev->mmhub.funcs->init(adev);
1568 	if (adev->mca.funcs)
1569 		adev->mca.funcs->init(adev);
1570 
1571 	spin_lock_init(&adev->gmc.invalidate_lock);
1572 
1573 	r = amdgpu_atomfirmware_get_vram_info(adev,
1574 		&vram_width, &vram_type, &vram_vendor);
1575 	if (amdgpu_sriov_vf(adev))
1576 		/* For Vega10 SR-IOV, vram_width can't be read from ATOM as RAVEN,
1577 		 * and DF related registers is not readable, seems hardcord is the
1578 		 * only way to set the correct vram_width
1579 		 */
1580 		adev->gmc.vram_width = 2048;
1581 	else if (amdgpu_emu_mode != 1)
1582 		adev->gmc.vram_width = vram_width;
1583 
1584 	if (!adev->gmc.vram_width) {
1585 		int chansize, numchan;
1586 
1587 		/* hbm memory channel size */
1588 		if (adev->flags & AMD_IS_APU)
1589 			chansize = 64;
1590 		else
1591 			chansize = 128;
1592 		if (adev->df.funcs &&
1593 		    adev->df.funcs->get_hbm_channel_number) {
1594 			numchan = adev->df.funcs->get_hbm_channel_number(adev);
1595 			adev->gmc.vram_width = numchan * chansize;
1596 		}
1597 	}
1598 
1599 	adev->gmc.vram_type = vram_type;
1600 	adev->gmc.vram_vendor = vram_vendor;
1601 	switch (adev->ip_versions[GC_HWIP][0]) {
1602 	case IP_VERSION(9, 1, 0):
1603 	case IP_VERSION(9, 2, 2):
1604 		adev->num_vmhubs = 2;
1605 
1606 		if (adev->rev_id == 0x0 || adev->rev_id == 0x1) {
1607 			amdgpu_vm_adjust_size(adev, 256 * 1024, 9, 3, 48);
1608 		} else {
1609 			/* vm_size is 128TB + 512GB for legacy 3-level page support */
1610 			amdgpu_vm_adjust_size(adev, 128 * 1024 + 512, 9, 2, 48);
1611 			adev->gmc.translate_further =
1612 				adev->vm_manager.num_level > 1;
1613 		}
1614 		break;
1615 	case IP_VERSION(9, 0, 1):
1616 	case IP_VERSION(9, 2, 1):
1617 	case IP_VERSION(9, 4, 0):
1618 	case IP_VERSION(9, 3, 0):
1619 	case IP_VERSION(9, 4, 2):
1620 		adev->num_vmhubs = 2;
1621 
1622 
1623 		/*
1624 		 * To fulfill 4-level page support,
1625 		 * vm size is 256TB (48bit), maximum size of Vega10,
1626 		 * block size 512 (9bit)
1627 		 */
1628 		/* sriov restrict max_pfn below AMDGPU_GMC_HOLE */
1629 		if (amdgpu_sriov_vf(adev))
1630 			amdgpu_vm_adjust_size(adev, 256 * 1024, 9, 3, 47);
1631 		else
1632 			amdgpu_vm_adjust_size(adev, 256 * 1024, 9, 3, 48);
1633 		break;
1634 	case IP_VERSION(9, 4, 1):
1635 		adev->num_vmhubs = 3;
1636 
1637 		/* Keep the vm size same with Vega20 */
1638 		amdgpu_vm_adjust_size(adev, 256 * 1024, 9, 3, 48);
1639 		break;
1640 	default:
1641 		break;
1642 	}
1643 
1644 	/* This interrupt is VMC page fault.*/
1645 	r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_VMC, VMC_1_0__SRCID__VM_FAULT,
1646 				&adev->gmc.vm_fault);
1647 	if (r)
1648 		return r;
1649 
1650 	if (adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 1)) {
1651 		r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_VMC1, VMC_1_0__SRCID__VM_FAULT,
1652 					&adev->gmc.vm_fault);
1653 		if (r)
1654 			return r;
1655 	}
1656 
1657 	r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_UTCL2, UTCL2_1_0__SRCID__FAULT,
1658 				&adev->gmc.vm_fault);
1659 
1660 	if (r)
1661 		return r;
1662 
1663 	if (!amdgpu_sriov_vf(adev) &&
1664 	    !adev->gmc.xgmi.connected_to_cpu) {
1665 		/* interrupt sent to DF. */
1666 		r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DF, 0,
1667 				      &adev->gmc.ecc_irq);
1668 		if (r)
1669 			return r;
1670 	}
1671 
1672 	/* Set the internal MC address mask
1673 	 * This is the max address of the GPU's
1674 	 * internal address space.
1675 	 */
1676 	adev->gmc.mc_mask = 0xffffffffffffULL; /* 48 bit MC */
1677 
1678 	r = dma_set_mask_and_coherent(adev->dev, DMA_BIT_MASK(44));
1679 	if (r) {
1680 		printk(KERN_WARNING "amdgpu: No suitable DMA available.\n");
1681 		return r;
1682 	}
1683 	adev->need_swiotlb = drm_need_swiotlb(44);
1684 
1685 	r = gmc_v9_0_mc_init(adev);
1686 	if (r)
1687 		return r;
1688 
1689 	amdgpu_gmc_get_vbios_allocations(adev);
1690 
1691 	/* Memory manager */
1692 	r = amdgpu_bo_init(adev);
1693 	if (r)
1694 		return r;
1695 
1696 	r = gmc_v9_0_gart_init(adev);
1697 	if (r)
1698 		return r;
1699 
1700 	/*
1701 	 * number of VMs
1702 	 * VMID 0 is reserved for System
1703 	 * amdgpu graphics/compute will use VMIDs 1..n-1
1704 	 * amdkfd will use VMIDs n..15
1705 	 *
1706 	 * The first KFD VMID is 8 for GPUs with graphics, 3 for
1707 	 * compute-only GPUs. On compute-only GPUs that leaves 2 VMIDs
1708 	 * for video processing.
1709 	 */
1710 	adev->vm_manager.first_kfd_vmid =
1711 		(adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 1) ||
1712 		 adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 2)) ? 3 : 8;
1713 
1714 	amdgpu_vm_manager_init(adev);
1715 
1716 	gmc_v9_0_save_registers(adev);
1717 
1718 	return 0;
1719 }
1720 
1721 static int gmc_v9_0_sw_fini(void *handle)
1722 {
1723 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1724 
1725 	amdgpu_gmc_ras_fini(adev);
1726 	amdgpu_gem_force_release(adev);
1727 	amdgpu_vm_manager_fini(adev);
1728 	amdgpu_gart_table_vram_free(adev);
1729 	amdgpu_bo_unref(&adev->gmc.pdb0_bo);
1730 	amdgpu_bo_fini(adev);
1731 
1732 	return 0;
1733 }
1734 
1735 static void gmc_v9_0_init_golden_registers(struct amdgpu_device *adev)
1736 {
1737 
1738 	switch (adev->ip_versions[MMHUB_HWIP][0]) {
1739 	case IP_VERSION(9, 0, 0):
1740 		if (amdgpu_sriov_vf(adev))
1741 			break;
1742 		fallthrough;
1743 	case IP_VERSION(9, 4, 0):
1744 		soc15_program_register_sequence(adev,
1745 						golden_settings_mmhub_1_0_0,
1746 						ARRAY_SIZE(golden_settings_mmhub_1_0_0));
1747 		soc15_program_register_sequence(adev,
1748 						golden_settings_athub_1_0_0,
1749 						ARRAY_SIZE(golden_settings_athub_1_0_0));
1750 		break;
1751 	case IP_VERSION(9, 1, 0):
1752 	case IP_VERSION(9, 2, 0):
1753 		/* TODO for renoir */
1754 		soc15_program_register_sequence(adev,
1755 						golden_settings_athub_1_0_0,
1756 						ARRAY_SIZE(golden_settings_athub_1_0_0));
1757 		break;
1758 	default:
1759 		break;
1760 	}
1761 }
1762 
1763 /**
1764  * gmc_v9_0_restore_registers - restores regs
1765  *
1766  * @adev: amdgpu_device pointer
1767  *
1768  * This restores register values, saved at suspend.
1769  */
1770 void gmc_v9_0_restore_registers(struct amdgpu_device *adev)
1771 {
1772 	if ((adev->ip_versions[DCE_HWIP][0] == IP_VERSION(1, 0, 0)) ||
1773 	    (adev->ip_versions[DCE_HWIP][0] == IP_VERSION(1, 0, 1))) {
1774 		WREG32_SOC15(DCE, 0, mmDCHUBBUB_SDPIF_MMIO_CNTRL_0, adev->gmc.sdpif_register);
1775 		WARN_ON(adev->gmc.sdpif_register !=
1776 			RREG32_SOC15(DCE, 0, mmDCHUBBUB_SDPIF_MMIO_CNTRL_0));
1777 	}
1778 }
1779 
1780 /**
1781  * gmc_v9_0_gart_enable - gart enable
1782  *
1783  * @adev: amdgpu_device pointer
1784  */
1785 static int gmc_v9_0_gart_enable(struct amdgpu_device *adev)
1786 {
1787 	int r;
1788 
1789 	if (adev->gmc.xgmi.connected_to_cpu)
1790 		amdgpu_gmc_init_pdb0(adev);
1791 
1792 	if (adev->gart.bo == NULL) {
1793 		dev_err(adev->dev, "No VRAM object for PCIE GART.\n");
1794 		return -EINVAL;
1795 	}
1796 
1797 	amdgpu_gtt_mgr_recover(&adev->mman.gtt_mgr);
1798 	r = adev->gfxhub.funcs->gart_enable(adev);
1799 	if (r)
1800 		return r;
1801 
1802 	r = adev->mmhub.funcs->gart_enable(adev);
1803 	if (r)
1804 		return r;
1805 
1806 	DRM_INFO("PCIE GART of %uM enabled.\n",
1807 		 (unsigned)(adev->gmc.gart_size >> 20));
1808 	if (adev->gmc.pdb0_bo)
1809 		DRM_INFO("PDB0 located at 0x%016llX\n",
1810 				(unsigned long long)amdgpu_bo_gpu_offset(adev->gmc.pdb0_bo));
1811 	DRM_INFO("PTB located at 0x%016llX\n",
1812 			(unsigned long long)amdgpu_bo_gpu_offset(adev->gart.bo));
1813 
1814 	return 0;
1815 }
1816 
1817 static int gmc_v9_0_hw_init(void *handle)
1818 {
1819 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1820 	bool value;
1821 	int i, r;
1822 
1823 	/* The sequence of these two function calls matters.*/
1824 	gmc_v9_0_init_golden_registers(adev);
1825 
1826 	if (adev->mode_info.num_crtc) {
1827 		/* Lockout access through VGA aperture*/
1828 		WREG32_FIELD15(DCE, 0, VGA_HDP_CONTROL, VGA_MEMORY_DISABLE, 1);
1829 		/* disable VGA render */
1830 		WREG32_FIELD15(DCE, 0, VGA_RENDER_CONTROL, VGA_VSTATUS_CNTL, 0);
1831 	}
1832 
1833 	if (adev->mmhub.funcs->update_power_gating)
1834 		adev->mmhub.funcs->update_power_gating(adev, true);
1835 
1836 	adev->hdp.funcs->init_registers(adev);
1837 
1838 	/* After HDP is initialized, flush HDP.*/
1839 	adev->hdp.funcs->flush_hdp(adev, NULL);
1840 
1841 	if (amdgpu_vm_fault_stop == AMDGPU_VM_FAULT_STOP_ALWAYS)
1842 		value = false;
1843 	else
1844 		value = true;
1845 
1846 	if (!amdgpu_sriov_vf(adev)) {
1847 		adev->gfxhub.funcs->set_fault_enable_default(adev, value);
1848 		adev->mmhub.funcs->set_fault_enable_default(adev, value);
1849 	}
1850 	for (i = 0; i < adev->num_vmhubs; ++i)
1851 		gmc_v9_0_flush_gpu_tlb(adev, 0, i, 0);
1852 
1853 	if (adev->umc.funcs && adev->umc.funcs->init_registers)
1854 		adev->umc.funcs->init_registers(adev);
1855 
1856 	r = gmc_v9_0_gart_enable(adev);
1857 	if (r)
1858 		return r;
1859 
1860 	if (amdgpu_emu_mode == 1)
1861 		return amdgpu_gmc_vram_checking(adev);
1862 	else
1863 		return r;
1864 }
1865 
1866 /**
1867  * gmc_v9_0_gart_disable - gart disable
1868  *
1869  * @adev: amdgpu_device pointer
1870  *
1871  * This disables all VM page table.
1872  */
1873 static void gmc_v9_0_gart_disable(struct amdgpu_device *adev)
1874 {
1875 	adev->gfxhub.funcs->gart_disable(adev);
1876 	adev->mmhub.funcs->gart_disable(adev);
1877 }
1878 
1879 static int gmc_v9_0_hw_fini(void *handle)
1880 {
1881 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1882 
1883 	gmc_v9_0_gart_disable(adev);
1884 
1885 	if (amdgpu_sriov_vf(adev)) {
1886 		/* full access mode, so don't touch any GMC register */
1887 		DRM_DEBUG("For SRIOV client, shouldn't do anything.\n");
1888 		return 0;
1889 	}
1890 
1891 	/*
1892 	 * Pair the operations did in gmc_v9_0_hw_init and thus maintain
1893 	 * a correct cached state for GMC. Otherwise, the "gate" again
1894 	 * operation on S3 resuming will fail due to wrong cached state.
1895 	 */
1896 	if (adev->mmhub.funcs->update_power_gating)
1897 		adev->mmhub.funcs->update_power_gating(adev, false);
1898 
1899 	amdgpu_irq_put(adev, &adev->gmc.ecc_irq, 0);
1900 	amdgpu_irq_put(adev, &adev->gmc.vm_fault, 0);
1901 
1902 	return 0;
1903 }
1904 
1905 static int gmc_v9_0_suspend(void *handle)
1906 {
1907 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1908 
1909 	return gmc_v9_0_hw_fini(adev);
1910 }
1911 
1912 static int gmc_v9_0_resume(void *handle)
1913 {
1914 	int r;
1915 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1916 
1917 	r = gmc_v9_0_hw_init(adev);
1918 	if (r)
1919 		return r;
1920 
1921 	amdgpu_vmid_reset_all(adev);
1922 
1923 	return 0;
1924 }
1925 
1926 static bool gmc_v9_0_is_idle(void *handle)
1927 {
1928 	/* MC is always ready in GMC v9.*/
1929 	return true;
1930 }
1931 
1932 static int gmc_v9_0_wait_for_idle(void *handle)
1933 {
1934 	/* There is no need to wait for MC idle in GMC v9.*/
1935 	return 0;
1936 }
1937 
1938 static int gmc_v9_0_soft_reset(void *handle)
1939 {
1940 	/* XXX for emulation.*/
1941 	return 0;
1942 }
1943 
1944 static int gmc_v9_0_set_clockgating_state(void *handle,
1945 					enum amd_clockgating_state state)
1946 {
1947 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1948 
1949 	adev->mmhub.funcs->set_clockgating(adev, state);
1950 
1951 	athub_v1_0_set_clockgating(adev, state);
1952 
1953 	return 0;
1954 }
1955 
1956 static void gmc_v9_0_get_clockgating_state(void *handle, u32 *flags)
1957 {
1958 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1959 
1960 	adev->mmhub.funcs->get_clockgating(adev, flags);
1961 
1962 	athub_v1_0_get_clockgating(adev, flags);
1963 }
1964 
1965 static int gmc_v9_0_set_powergating_state(void *handle,
1966 					enum amd_powergating_state state)
1967 {
1968 	return 0;
1969 }
1970 
1971 const struct amd_ip_funcs gmc_v9_0_ip_funcs = {
1972 	.name = "gmc_v9_0",
1973 	.early_init = gmc_v9_0_early_init,
1974 	.late_init = gmc_v9_0_late_init,
1975 	.sw_init = gmc_v9_0_sw_init,
1976 	.sw_fini = gmc_v9_0_sw_fini,
1977 	.hw_init = gmc_v9_0_hw_init,
1978 	.hw_fini = gmc_v9_0_hw_fini,
1979 	.suspend = gmc_v9_0_suspend,
1980 	.resume = gmc_v9_0_resume,
1981 	.is_idle = gmc_v9_0_is_idle,
1982 	.wait_for_idle = gmc_v9_0_wait_for_idle,
1983 	.soft_reset = gmc_v9_0_soft_reset,
1984 	.set_clockgating_state = gmc_v9_0_set_clockgating_state,
1985 	.set_powergating_state = gmc_v9_0_set_powergating_state,
1986 	.get_clockgating_state = gmc_v9_0_get_clockgating_state,
1987 };
1988 
1989 const struct amdgpu_ip_block_version gmc_v9_0_ip_block =
1990 {
1991 	.type = AMD_IP_BLOCK_TYPE_GMC,
1992 	.major = 9,
1993 	.minor = 0,
1994 	.rev = 0,
1995 	.funcs = &gmc_v9_0_ip_funcs,
1996 };
1997