xref: /dragonfly/sys/dev/drm/amd/amdgpu/gmc_v7_0.c (revision 809f3802)
1b843c749SSergey Zigachev /*
2b843c749SSergey Zigachev  * Copyright 2014 Advanced Micro Devices, Inc.
3b843c749SSergey Zigachev  *
4b843c749SSergey Zigachev  * Permission is hereby granted, free of charge, to any person obtaining a
5b843c749SSergey Zigachev  * copy of this software and associated documentation files (the "Software"),
6b843c749SSergey Zigachev  * to deal in the Software without restriction, including without limitation
7b843c749SSergey Zigachev  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b843c749SSergey Zigachev  * and/or sell copies of the Software, and to permit persons to whom the
9b843c749SSergey Zigachev  * Software is furnished to do so, subject to the following conditions:
10b843c749SSergey Zigachev  *
11b843c749SSergey Zigachev  * The above copyright notice and this permission notice shall be included in
12b843c749SSergey Zigachev  * all copies or substantial portions of the Software.
13b843c749SSergey Zigachev  *
14b843c749SSergey Zigachev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15b843c749SSergey Zigachev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16b843c749SSergey Zigachev  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17b843c749SSergey Zigachev  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18b843c749SSergey Zigachev  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19b843c749SSergey Zigachev  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20b843c749SSergey Zigachev  * OTHER DEALINGS IN THE SOFTWARE.
21b843c749SSergey Zigachev  *
22b843c749SSergey Zigachev  */
23b843c749SSergey Zigachev #include <linux/firmware.h>
24b843c749SSergey Zigachev #include <drm/drmP.h>
25b843c749SSergey Zigachev #include <drm/drm_cache.h>
26b843c749SSergey Zigachev #include "amdgpu.h"
27b843c749SSergey Zigachev #include "cikd.h"
28b843c749SSergey Zigachev #include "cik.h"
29b843c749SSergey Zigachev #include "gmc_v7_0.h"
30b843c749SSergey Zigachev #include "amdgpu_ucode.h"
31b843c749SSergey Zigachev #include "amdgpu_amdkfd.h"
32b843c749SSergey Zigachev 
33b843c749SSergey Zigachev #include "bif/bif_4_1_d.h"
34b843c749SSergey Zigachev #include "bif/bif_4_1_sh_mask.h"
35b843c749SSergey Zigachev 
36b843c749SSergey Zigachev #include "gmc/gmc_7_1_d.h"
37b843c749SSergey Zigachev #include "gmc/gmc_7_1_sh_mask.h"
38b843c749SSergey Zigachev 
39b843c749SSergey Zigachev #include "oss/oss_2_0_d.h"
40b843c749SSergey Zigachev #include "oss/oss_2_0_sh_mask.h"
41b843c749SSergey Zigachev 
42b843c749SSergey Zigachev #include "dce/dce_8_0_d.h"
43b843c749SSergey Zigachev #include "dce/dce_8_0_sh_mask.h"
44b843c749SSergey Zigachev 
45b843c749SSergey Zigachev #include "amdgpu_atombios.h"
46b843c749SSergey Zigachev 
47b843c749SSergey Zigachev #include "ivsrcid/ivsrcid_vislands30.h"
48b843c749SSergey Zigachev 
49b843c749SSergey Zigachev static void gmc_v7_0_set_gmc_funcs(struct amdgpu_device *adev);
50b843c749SSergey Zigachev static void gmc_v7_0_set_irq_funcs(struct amdgpu_device *adev);
51b843c749SSergey Zigachev static int gmc_v7_0_wait_for_idle(void *handle);
52b843c749SSergey Zigachev 
53*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_bonaire_mc");
54*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_hawaii_mc");
55*809f3802SSergey Zigachev MODULE_FIRMWARE("amdgpufw_topaz_mc");
56b843c749SSergey Zigachev 
57b843c749SSergey Zigachev static const u32 golden_settings_iceland_a11[] =
58b843c749SSergey Zigachev {
59b843c749SSergey Zigachev 	mmVM_PRT_APERTURE0_LOW_ADDR, 0x0fffffff, 0x0fffffff,
60b843c749SSergey Zigachev 	mmVM_PRT_APERTURE1_LOW_ADDR, 0x0fffffff, 0x0fffffff,
61b843c749SSergey Zigachev 	mmVM_PRT_APERTURE2_LOW_ADDR, 0x0fffffff, 0x0fffffff,
62b843c749SSergey Zigachev 	mmVM_PRT_APERTURE3_LOW_ADDR, 0x0fffffff, 0x0fffffff
63b843c749SSergey Zigachev };
64b843c749SSergey Zigachev 
65b843c749SSergey Zigachev static const u32 iceland_mgcg_cgcg_init[] =
66b843c749SSergey Zigachev {
67b843c749SSergey Zigachev 	mmMC_MEM_POWER_LS, 0xffffffff, 0x00000104
68b843c749SSergey Zigachev };
69b843c749SSergey Zigachev 
gmc_v7_0_init_golden_registers(struct amdgpu_device * adev)70b843c749SSergey Zigachev static void gmc_v7_0_init_golden_registers(struct amdgpu_device *adev)
71b843c749SSergey Zigachev {
72b843c749SSergey Zigachev 	switch (adev->asic_type) {
73b843c749SSergey Zigachev 	case CHIP_TOPAZ:
74b843c749SSergey Zigachev 		amdgpu_device_program_register_sequence(adev,
75b843c749SSergey Zigachev 							iceland_mgcg_cgcg_init,
76b843c749SSergey Zigachev 							ARRAY_SIZE(iceland_mgcg_cgcg_init));
77b843c749SSergey Zigachev 		amdgpu_device_program_register_sequence(adev,
78b843c749SSergey Zigachev 							golden_settings_iceland_a11,
79b843c749SSergey Zigachev 							ARRAY_SIZE(golden_settings_iceland_a11));
80b843c749SSergey Zigachev 		break;
81b843c749SSergey Zigachev 	default:
82b843c749SSergey Zigachev 		break;
83b843c749SSergey Zigachev 	}
84b843c749SSergey Zigachev }
85b843c749SSergey Zigachev 
gmc_v7_0_mc_stop(struct amdgpu_device * adev)86b843c749SSergey Zigachev static void gmc_v7_0_mc_stop(struct amdgpu_device *adev)
87b843c749SSergey Zigachev {
88b843c749SSergey Zigachev 	u32 blackout;
89b843c749SSergey Zigachev 
90b843c749SSergey Zigachev 	gmc_v7_0_wait_for_idle((void *)adev);
91b843c749SSergey Zigachev 
92b843c749SSergey Zigachev 	blackout = RREG32(mmMC_SHARED_BLACKOUT_CNTL);
93b843c749SSergey Zigachev 	if (REG_GET_FIELD(blackout, MC_SHARED_BLACKOUT_CNTL, BLACKOUT_MODE) != 1) {
94b843c749SSergey Zigachev 		/* Block CPU access */
95b843c749SSergey Zigachev 		WREG32(mmBIF_FB_EN, 0);
96b843c749SSergey Zigachev 		/* blackout the MC */
97b843c749SSergey Zigachev 		blackout = REG_SET_FIELD(blackout,
98b843c749SSergey Zigachev 					 MC_SHARED_BLACKOUT_CNTL, BLACKOUT_MODE, 0);
99b843c749SSergey Zigachev 		WREG32(mmMC_SHARED_BLACKOUT_CNTL, blackout | 1);
100b843c749SSergey Zigachev 	}
101b843c749SSergey Zigachev 	/* wait for the MC to settle */
102b843c749SSergey Zigachev 	udelay(100);
103b843c749SSergey Zigachev }
104b843c749SSergey Zigachev 
gmc_v7_0_mc_resume(struct amdgpu_device * adev)105b843c749SSergey Zigachev static void gmc_v7_0_mc_resume(struct amdgpu_device *adev)
106b843c749SSergey Zigachev {
107b843c749SSergey Zigachev 	u32 tmp;
108b843c749SSergey Zigachev 
109b843c749SSergey Zigachev 	/* unblackout the MC */
110b843c749SSergey Zigachev 	tmp = RREG32(mmMC_SHARED_BLACKOUT_CNTL);
111b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, MC_SHARED_BLACKOUT_CNTL, BLACKOUT_MODE, 0);
112b843c749SSergey Zigachev 	WREG32(mmMC_SHARED_BLACKOUT_CNTL, tmp);
113b843c749SSergey Zigachev 	/* allow CPU access */
114b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(0, BIF_FB_EN, FB_READ_EN, 1);
115b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, BIF_FB_EN, FB_WRITE_EN, 1);
116b843c749SSergey Zigachev 	WREG32(mmBIF_FB_EN, tmp);
117b843c749SSergey Zigachev }
118b843c749SSergey Zigachev 
119b843c749SSergey Zigachev /**
120b843c749SSergey Zigachev  * gmc_v7_0_init_microcode - load ucode images from disk
121b843c749SSergey Zigachev  *
122b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
123b843c749SSergey Zigachev  *
124b843c749SSergey Zigachev  * Use the firmware interface to load the ucode images into
125b843c749SSergey Zigachev  * the driver (not loaded into hw).
126b843c749SSergey Zigachev  * Returns 0 on success, error on failure.
127b843c749SSergey Zigachev  */
gmc_v7_0_init_microcode(struct amdgpu_device * adev)128b843c749SSergey Zigachev static int gmc_v7_0_init_microcode(struct amdgpu_device *adev)
129b843c749SSergey Zigachev {
130b843c749SSergey Zigachev 	const char *chip_name;
131b843c749SSergey Zigachev 	char fw_name[30];
132b843c749SSergey Zigachev 	int err;
133b843c749SSergey Zigachev 
134b843c749SSergey Zigachev 	DRM_DEBUG("\n");
135b843c749SSergey Zigachev 
136b843c749SSergey Zigachev 	switch (adev->asic_type) {
137b843c749SSergey Zigachev 	case CHIP_BONAIRE:
138b843c749SSergey Zigachev 		chip_name = "bonaire";
139b843c749SSergey Zigachev 		break;
140b843c749SSergey Zigachev 	case CHIP_HAWAII:
141b843c749SSergey Zigachev 		chip_name = "hawaii";
142b843c749SSergey Zigachev 		break;
143b843c749SSergey Zigachev 	case CHIP_TOPAZ:
144b843c749SSergey Zigachev 		chip_name = "topaz";
145b843c749SSergey Zigachev 		break;
146b843c749SSergey Zigachev 	case CHIP_KAVERI:
147b843c749SSergey Zigachev 	case CHIP_KABINI:
148b843c749SSergey Zigachev 	case CHIP_MULLINS:
149b843c749SSergey Zigachev 		return 0;
150b843c749SSergey Zigachev 	default: BUG();
151b843c749SSergey Zigachev 	}
152b843c749SSergey Zigachev 
153*809f3802SSergey Zigachev 	snprintf(fw_name, sizeof(fw_name), "amdgpufw_%s_mc", chip_name);
154b843c749SSergey Zigachev 
155b843c749SSergey Zigachev 	err = request_firmware(&adev->gmc.fw, fw_name, adev->dev);
156b843c749SSergey Zigachev 	if (err)
157b843c749SSergey Zigachev 		goto out;
158b843c749SSergey Zigachev 	err = amdgpu_ucode_validate(adev->gmc.fw);
159b843c749SSergey Zigachev 
160b843c749SSergey Zigachev out:
161b843c749SSergey Zigachev 	if (err) {
162b843c749SSergey Zigachev 		pr_err("cik_mc: Failed to load firmware \"%s\"\n", fw_name);
163b843c749SSergey Zigachev 		release_firmware(adev->gmc.fw);
164b843c749SSergey Zigachev 		adev->gmc.fw = NULL;
165b843c749SSergey Zigachev 	}
166b843c749SSergey Zigachev 	return err;
167b843c749SSergey Zigachev }
168b843c749SSergey Zigachev 
169b843c749SSergey Zigachev /**
170b843c749SSergey Zigachev  * gmc_v7_0_mc_load_microcode - load MC ucode into the hw
171b843c749SSergey Zigachev  *
172b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
173b843c749SSergey Zigachev  *
174b843c749SSergey Zigachev  * Load the GDDR MC ucode into the hw (CIK).
175b843c749SSergey Zigachev  * Returns 0 on success, error on failure.
176b843c749SSergey Zigachev  */
gmc_v7_0_mc_load_microcode(struct amdgpu_device * adev)177b843c749SSergey Zigachev static int gmc_v7_0_mc_load_microcode(struct amdgpu_device *adev)
178b843c749SSergey Zigachev {
179b843c749SSergey Zigachev 	const struct mc_firmware_header_v1_0 *hdr;
180b843c749SSergey Zigachev 	const __le32 *fw_data = NULL;
181b843c749SSergey Zigachev 	const __le32 *io_mc_regs = NULL;
182b843c749SSergey Zigachev 	u32 running;
183b843c749SSergey Zigachev 	int i, ucode_size, regs_size;
184b843c749SSergey Zigachev 
185b843c749SSergey Zigachev 	if (!adev->gmc.fw)
186b843c749SSergey Zigachev 		return -EINVAL;
187b843c749SSergey Zigachev 
188b843c749SSergey Zigachev 	hdr = (const struct mc_firmware_header_v1_0 *)adev->gmc.fw->data;
189b843c749SSergey Zigachev 	amdgpu_ucode_print_mc_hdr(&hdr->header);
190b843c749SSergey Zigachev 
191b843c749SSergey Zigachev 	adev->gmc.fw_version = le32_to_cpu(hdr->header.ucode_version);
192b843c749SSergey Zigachev 	regs_size = le32_to_cpu(hdr->io_debug_size_bytes) / (4 * 2);
193b843c749SSergey Zigachev 	io_mc_regs = (const __le32 *)
194b843c749SSergey Zigachev 		(adev->gmc.fw->data + le32_to_cpu(hdr->io_debug_array_offset_bytes));
195b843c749SSergey Zigachev 	ucode_size = le32_to_cpu(hdr->header.ucode_size_bytes) / 4;
196b843c749SSergey Zigachev 	fw_data = (const __le32 *)
197b843c749SSergey Zigachev 		(adev->gmc.fw->data + le32_to_cpu(hdr->header.ucode_array_offset_bytes));
198b843c749SSergey Zigachev 
199b843c749SSergey Zigachev 	running = REG_GET_FIELD(RREG32(mmMC_SEQ_SUP_CNTL), MC_SEQ_SUP_CNTL, RUN);
200b843c749SSergey Zigachev 
201b843c749SSergey Zigachev 	if (running == 0) {
202b843c749SSergey Zigachev 		/* reset the engine and set to writable */
203b843c749SSergey Zigachev 		WREG32(mmMC_SEQ_SUP_CNTL, 0x00000008);
204b843c749SSergey Zigachev 		WREG32(mmMC_SEQ_SUP_CNTL, 0x00000010);
205b843c749SSergey Zigachev 
206b843c749SSergey Zigachev 		/* load mc io regs */
207b843c749SSergey Zigachev 		for (i = 0; i < regs_size; i++) {
208b843c749SSergey Zigachev 			WREG32(mmMC_SEQ_IO_DEBUG_INDEX, le32_to_cpup(io_mc_regs++));
209b843c749SSergey Zigachev 			WREG32(mmMC_SEQ_IO_DEBUG_DATA, le32_to_cpup(io_mc_regs++));
210b843c749SSergey Zigachev 		}
211b843c749SSergey Zigachev 		/* load the MC ucode */
212b843c749SSergey Zigachev 		for (i = 0; i < ucode_size; i++)
213b843c749SSergey Zigachev 			WREG32(mmMC_SEQ_SUP_PGM, le32_to_cpup(fw_data++));
214b843c749SSergey Zigachev 
215b843c749SSergey Zigachev 		/* put the engine back into the active state */
216b843c749SSergey Zigachev 		WREG32(mmMC_SEQ_SUP_CNTL, 0x00000008);
217b843c749SSergey Zigachev 		WREG32(mmMC_SEQ_SUP_CNTL, 0x00000004);
218b843c749SSergey Zigachev 		WREG32(mmMC_SEQ_SUP_CNTL, 0x00000001);
219b843c749SSergey Zigachev 
220b843c749SSergey Zigachev 		/* wait for training to complete */
221b843c749SSergey Zigachev 		for (i = 0; i < adev->usec_timeout; i++) {
222b843c749SSergey Zigachev 			if (REG_GET_FIELD(RREG32(mmMC_SEQ_TRAIN_WAKEUP_CNTL),
223b843c749SSergey Zigachev 					  MC_SEQ_TRAIN_WAKEUP_CNTL, TRAIN_DONE_D0))
224b843c749SSergey Zigachev 				break;
225b843c749SSergey Zigachev 			udelay(1);
226b843c749SSergey Zigachev 		}
227b843c749SSergey Zigachev 		for (i = 0; i < adev->usec_timeout; i++) {
228b843c749SSergey Zigachev 			if (REG_GET_FIELD(RREG32(mmMC_SEQ_TRAIN_WAKEUP_CNTL),
229b843c749SSergey Zigachev 					  MC_SEQ_TRAIN_WAKEUP_CNTL, TRAIN_DONE_D1))
230b843c749SSergey Zigachev 				break;
231b843c749SSergey Zigachev 			udelay(1);
232b843c749SSergey Zigachev 		}
233b843c749SSergey Zigachev 	}
234b843c749SSergey Zigachev 
235b843c749SSergey Zigachev 	return 0;
236b843c749SSergey Zigachev }
237b843c749SSergey Zigachev 
gmc_v7_0_vram_gtt_location(struct amdgpu_device * adev,struct amdgpu_gmc * mc)238b843c749SSergey Zigachev static void gmc_v7_0_vram_gtt_location(struct amdgpu_device *adev,
239b843c749SSergey Zigachev 				       struct amdgpu_gmc *mc)
240b843c749SSergey Zigachev {
241b843c749SSergey Zigachev 	u64 base = RREG32(mmMC_VM_FB_LOCATION) & 0xFFFF;
242b843c749SSergey Zigachev 	base <<= 24;
243b843c749SSergey Zigachev 
244b843c749SSergey Zigachev 	amdgpu_device_vram_location(adev, &adev->gmc, base);
245b843c749SSergey Zigachev 	amdgpu_device_gart_location(adev, mc);
246b843c749SSergey Zigachev }
247b843c749SSergey Zigachev 
248b843c749SSergey Zigachev /**
249b843c749SSergey Zigachev  * gmc_v7_0_mc_program - program the GPU memory controller
250b843c749SSergey Zigachev  *
251b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
252b843c749SSergey Zigachev  *
253b843c749SSergey Zigachev  * Set the location of vram, gart, and AGP in the GPU's
254b843c749SSergey Zigachev  * physical address space (CIK).
255b843c749SSergey Zigachev  */
gmc_v7_0_mc_program(struct amdgpu_device * adev)256b843c749SSergey Zigachev static void gmc_v7_0_mc_program(struct amdgpu_device *adev)
257b843c749SSergey Zigachev {
258b843c749SSergey Zigachev 	u32 tmp;
259b843c749SSergey Zigachev 	int i, j;
260b843c749SSergey Zigachev 
261b843c749SSergey Zigachev 	/* Initialize HDP */
262b843c749SSergey Zigachev 	for (i = 0, j = 0; i < 32; i++, j += 0x6) {
263b843c749SSergey Zigachev 		WREG32((0xb05 + j), 0x00000000);
264b843c749SSergey Zigachev 		WREG32((0xb06 + j), 0x00000000);
265b843c749SSergey Zigachev 		WREG32((0xb07 + j), 0x00000000);
266b843c749SSergey Zigachev 		WREG32((0xb08 + j), 0x00000000);
267b843c749SSergey Zigachev 		WREG32((0xb09 + j), 0x00000000);
268b843c749SSergey Zigachev 	}
269b843c749SSergey Zigachev 	WREG32(mmHDP_REG_COHERENCY_FLUSH_CNTL, 0);
270b843c749SSergey Zigachev 
271b843c749SSergey Zigachev 	if (gmc_v7_0_wait_for_idle((void *)adev)) {
272b843c749SSergey Zigachev 		dev_warn(adev->dev, "Wait for MC idle timedout !\n");
273b843c749SSergey Zigachev 	}
274b843c749SSergey Zigachev 	if (adev->mode_info.num_crtc) {
275b843c749SSergey Zigachev 		/* Lockout access through VGA aperture*/
276b843c749SSergey Zigachev 		tmp = RREG32(mmVGA_HDP_CONTROL);
277b843c749SSergey Zigachev 		tmp = REG_SET_FIELD(tmp, VGA_HDP_CONTROL, VGA_MEMORY_DISABLE, 1);
278b843c749SSergey Zigachev 		WREG32(mmVGA_HDP_CONTROL, tmp);
279b843c749SSergey Zigachev 
280b843c749SSergey Zigachev 		/* disable VGA render */
281b843c749SSergey Zigachev 		tmp = RREG32(mmVGA_RENDER_CONTROL);
282b843c749SSergey Zigachev 		tmp = REG_SET_FIELD(tmp, VGA_RENDER_CONTROL, VGA_VSTATUS_CNTL, 0);
283b843c749SSergey Zigachev 		WREG32(mmVGA_RENDER_CONTROL, tmp);
284b843c749SSergey Zigachev 	}
285b843c749SSergey Zigachev 	/* Update configuration */
286b843c749SSergey Zigachev 	WREG32(mmMC_VM_SYSTEM_APERTURE_LOW_ADDR,
287b843c749SSergey Zigachev 	       adev->gmc.vram_start >> 12);
288b843c749SSergey Zigachev 	WREG32(mmMC_VM_SYSTEM_APERTURE_HIGH_ADDR,
289b843c749SSergey Zigachev 	       adev->gmc.vram_end >> 12);
290b843c749SSergey Zigachev 	WREG32(mmMC_VM_SYSTEM_APERTURE_DEFAULT_ADDR,
291b843c749SSergey Zigachev 	       adev->vram_scratch.gpu_addr >> 12);
292b843c749SSergey Zigachev 	WREG32(mmMC_VM_AGP_BASE, 0);
293b843c749SSergey Zigachev 	WREG32(mmMC_VM_AGP_TOP, 0x0FFFFFFF);
294b843c749SSergey Zigachev 	WREG32(mmMC_VM_AGP_BOT, 0x0FFFFFFF);
295b843c749SSergey Zigachev 	if (gmc_v7_0_wait_for_idle((void *)adev)) {
296b843c749SSergey Zigachev 		dev_warn(adev->dev, "Wait for MC idle timedout !\n");
297b843c749SSergey Zigachev 	}
298b843c749SSergey Zigachev 
299b843c749SSergey Zigachev 	WREG32(mmBIF_FB_EN, BIF_FB_EN__FB_READ_EN_MASK | BIF_FB_EN__FB_WRITE_EN_MASK);
300b843c749SSergey Zigachev 
301b843c749SSergey Zigachev 	tmp = RREG32(mmHDP_MISC_CNTL);
302b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, HDP_MISC_CNTL, FLUSH_INVALIDATE_CACHE, 0);
303b843c749SSergey Zigachev 	WREG32(mmHDP_MISC_CNTL, tmp);
304b843c749SSergey Zigachev 
305b843c749SSergey Zigachev 	tmp = RREG32(mmHDP_HOST_PATH_CNTL);
306b843c749SSergey Zigachev 	WREG32(mmHDP_HOST_PATH_CNTL, tmp);
307b843c749SSergey Zigachev }
308b843c749SSergey Zigachev 
309b843c749SSergey Zigachev /**
310b843c749SSergey Zigachev  * gmc_v7_0_mc_init - initialize the memory controller driver params
311b843c749SSergey Zigachev  *
312b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
313b843c749SSergey Zigachev  *
314b843c749SSergey Zigachev  * Look up the amount of vram, vram width, and decide how to place
315b843c749SSergey Zigachev  * vram and gart within the GPU's physical address space (CIK).
316b843c749SSergey Zigachev  * Returns 0 for success.
317b843c749SSergey Zigachev  */
gmc_v7_0_mc_init(struct amdgpu_device * adev)318b843c749SSergey Zigachev static int gmc_v7_0_mc_init(struct amdgpu_device *adev)
319b843c749SSergey Zigachev {
320b843c749SSergey Zigachev 	int r;
321b843c749SSergey Zigachev 
322b843c749SSergey Zigachev 	adev->gmc.vram_width = amdgpu_atombios_get_vram_width(adev);
323b843c749SSergey Zigachev 	if (!adev->gmc.vram_width) {
324b843c749SSergey Zigachev 		u32 tmp;
325b843c749SSergey Zigachev 		int chansize, numchan;
326b843c749SSergey Zigachev 
327b843c749SSergey Zigachev 		/* Get VRAM informations */
328b843c749SSergey Zigachev 		tmp = RREG32(mmMC_ARB_RAMCFG);
329b843c749SSergey Zigachev 		if (REG_GET_FIELD(tmp, MC_ARB_RAMCFG, CHANSIZE)) {
330b843c749SSergey Zigachev 			chansize = 64;
331b843c749SSergey Zigachev 		} else {
332b843c749SSergey Zigachev 			chansize = 32;
333b843c749SSergey Zigachev 		}
334b843c749SSergey Zigachev 		tmp = RREG32(mmMC_SHARED_CHMAP);
335b843c749SSergey Zigachev 		switch (REG_GET_FIELD(tmp, MC_SHARED_CHMAP, NOOFCHAN)) {
336b843c749SSergey Zigachev 		case 0:
337b843c749SSergey Zigachev 		default:
338b843c749SSergey Zigachev 			numchan = 1;
339b843c749SSergey Zigachev 			break;
340b843c749SSergey Zigachev 		case 1:
341b843c749SSergey Zigachev 			numchan = 2;
342b843c749SSergey Zigachev 			break;
343b843c749SSergey Zigachev 		case 2:
344b843c749SSergey Zigachev 			numchan = 4;
345b843c749SSergey Zigachev 			break;
346b843c749SSergey Zigachev 		case 3:
347b843c749SSergey Zigachev 			numchan = 8;
348b843c749SSergey Zigachev 			break;
349b843c749SSergey Zigachev 		case 4:
350b843c749SSergey Zigachev 			numchan = 3;
351b843c749SSergey Zigachev 			break;
352b843c749SSergey Zigachev 		case 5:
353b843c749SSergey Zigachev 			numchan = 6;
354b843c749SSergey Zigachev 			break;
355b843c749SSergey Zigachev 		case 6:
356b843c749SSergey Zigachev 			numchan = 10;
357b843c749SSergey Zigachev 			break;
358b843c749SSergey Zigachev 		case 7:
359b843c749SSergey Zigachev 			numchan = 12;
360b843c749SSergey Zigachev 			break;
361b843c749SSergey Zigachev 		case 8:
362b843c749SSergey Zigachev 			numchan = 16;
363b843c749SSergey Zigachev 			break;
364b843c749SSergey Zigachev 		}
365b843c749SSergey Zigachev 		adev->gmc.vram_width = numchan * chansize;
366b843c749SSergey Zigachev 	}
367b843c749SSergey Zigachev 	/* size in MB on si */
368b843c749SSergey Zigachev 	adev->gmc.mc_vram_size = RREG32(mmCONFIG_MEMSIZE) * 1024ULL * 1024ULL;
369b843c749SSergey Zigachev 	adev->gmc.real_vram_size = RREG32(mmCONFIG_MEMSIZE) * 1024ULL * 1024ULL;
370b843c749SSergey Zigachev 
371b843c749SSergey Zigachev 	if (!(adev->flags & AMD_IS_APU)) {
372b843c749SSergey Zigachev 		r = amdgpu_device_resize_fb_bar(adev);
373b843c749SSergey Zigachev 		if (r)
374b843c749SSergey Zigachev 			return r;
375b843c749SSergey Zigachev 	}
376b843c749SSergey Zigachev 	adev->gmc.aper_base = pci_resource_start(adev->pdev, 0);
377b843c749SSergey Zigachev 	adev->gmc.aper_size = pci_resource_len(adev->pdev, 0);
378b843c749SSergey Zigachev 
379b843c749SSergey Zigachev #ifdef CONFIG_X86_64
380b843c749SSergey Zigachev 	if (adev->flags & AMD_IS_APU) {
381b843c749SSergey Zigachev 		adev->gmc.aper_base = ((u64)RREG32(mmMC_VM_FB_OFFSET)) << 22;
382b843c749SSergey Zigachev 		adev->gmc.aper_size = adev->gmc.real_vram_size;
383b843c749SSergey Zigachev 	}
384b843c749SSergey Zigachev #endif
385b843c749SSergey Zigachev 
386b843c749SSergey Zigachev 	/* In case the PCI BAR is larger than the actual amount of vram */
387b843c749SSergey Zigachev 	adev->gmc.visible_vram_size = adev->gmc.aper_size;
388b843c749SSergey Zigachev 	if (adev->gmc.visible_vram_size > adev->gmc.real_vram_size)
389b843c749SSergey Zigachev 		adev->gmc.visible_vram_size = adev->gmc.real_vram_size;
390b843c749SSergey Zigachev 
391b843c749SSergey Zigachev 	/* set the gart size */
392b843c749SSergey Zigachev 	if (amdgpu_gart_size == -1) {
393b843c749SSergey Zigachev 		switch (adev->asic_type) {
394b843c749SSergey Zigachev 		case CHIP_TOPAZ:     /* no MM engines */
395b843c749SSergey Zigachev 		default:
396b843c749SSergey Zigachev 			adev->gmc.gart_size = 256ULL << 20;
397b843c749SSergey Zigachev 			break;
398b843c749SSergey Zigachev #ifdef CONFIG_DRM_AMDGPU_CIK
399b843c749SSergey Zigachev 		case CHIP_BONAIRE: /* UVD, VCE do not support GPUVM */
400b843c749SSergey Zigachev 		case CHIP_HAWAII:  /* UVD, VCE do not support GPUVM */
401b843c749SSergey Zigachev 		case CHIP_KAVERI:  /* UVD, VCE do not support GPUVM */
402b843c749SSergey Zigachev 		case CHIP_KABINI:  /* UVD, VCE do not support GPUVM */
403b843c749SSergey Zigachev 		case CHIP_MULLINS: /* UVD, VCE do not support GPUVM */
404b843c749SSergey Zigachev 			adev->gmc.gart_size = 1024ULL << 20;
405b843c749SSergey Zigachev 			break;
406b843c749SSergey Zigachev #endif
407b843c749SSergey Zigachev 		}
408b843c749SSergey Zigachev 	} else {
409b843c749SSergey Zigachev 		adev->gmc.gart_size = (u64)amdgpu_gart_size << 20;
410b843c749SSergey Zigachev 	}
411b843c749SSergey Zigachev 
412b843c749SSergey Zigachev 	gmc_v7_0_vram_gtt_location(adev, &adev->gmc);
413b843c749SSergey Zigachev 
414b843c749SSergey Zigachev 	return 0;
415b843c749SSergey Zigachev }
416b843c749SSergey Zigachev 
417b843c749SSergey Zigachev /*
418b843c749SSergey Zigachev  * GART
419b843c749SSergey Zigachev  * VMID 0 is the physical GPU addresses as used by the kernel.
420b843c749SSergey Zigachev  * VMIDs 1-15 are used for userspace clients and are handled
421b843c749SSergey Zigachev  * by the amdgpu vm/hsa code.
422b843c749SSergey Zigachev  */
423b843c749SSergey Zigachev 
424b843c749SSergey Zigachev /**
425b843c749SSergey Zigachev  * gmc_v7_0_flush_gpu_tlb - gart tlb flush callback
426b843c749SSergey Zigachev  *
427b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
428b843c749SSergey Zigachev  * @vmid: vm instance to flush
429b843c749SSergey Zigachev  *
430b843c749SSergey Zigachev  * Flush the TLB for the requested page table (CIK).
431b843c749SSergey Zigachev  */
gmc_v7_0_flush_gpu_tlb(struct amdgpu_device * adev,uint32_t vmid)432b843c749SSergey Zigachev static void gmc_v7_0_flush_gpu_tlb(struct amdgpu_device *adev, uint32_t vmid)
433b843c749SSergey Zigachev {
434b843c749SSergey Zigachev 	/* bits 0-15 are the VM contexts0-15 */
435b843c749SSergey Zigachev 	WREG32(mmVM_INVALIDATE_REQUEST, 1 << vmid);
436b843c749SSergey Zigachev }
437b843c749SSergey Zigachev 
gmc_v7_0_emit_flush_gpu_tlb(struct amdgpu_ring * ring,unsigned vmid,uint64_t pd_addr)438b843c749SSergey Zigachev static uint64_t gmc_v7_0_emit_flush_gpu_tlb(struct amdgpu_ring *ring,
439b843c749SSergey Zigachev 					    unsigned vmid, uint64_t pd_addr)
440b843c749SSergey Zigachev {
441b843c749SSergey Zigachev 	uint32_t reg;
442b843c749SSergey Zigachev 
443b843c749SSergey Zigachev 	if (vmid < 8)
444b843c749SSergey Zigachev 		reg = mmVM_CONTEXT0_PAGE_TABLE_BASE_ADDR + vmid;
445b843c749SSergey Zigachev 	else
446b843c749SSergey Zigachev 		reg = mmVM_CONTEXT8_PAGE_TABLE_BASE_ADDR + vmid - 8;
447b843c749SSergey Zigachev 	amdgpu_ring_emit_wreg(ring, reg, pd_addr >> 12);
448b843c749SSergey Zigachev 
449b843c749SSergey Zigachev 	/* bits 0-15 are the VM contexts0-15 */
450b843c749SSergey Zigachev 	amdgpu_ring_emit_wreg(ring, mmVM_INVALIDATE_REQUEST, 1 << vmid);
451b843c749SSergey Zigachev 
452b843c749SSergey Zigachev 	return pd_addr;
453b843c749SSergey Zigachev }
454b843c749SSergey Zigachev 
gmc_v7_0_emit_pasid_mapping(struct amdgpu_ring * ring,unsigned vmid,unsigned pasid)455b843c749SSergey Zigachev static void gmc_v7_0_emit_pasid_mapping(struct amdgpu_ring *ring, unsigned vmid,
456b843c749SSergey Zigachev 					unsigned pasid)
457b843c749SSergey Zigachev {
458b843c749SSergey Zigachev 	amdgpu_ring_emit_wreg(ring, mmIH_VMID_0_LUT + vmid, pasid);
459b843c749SSergey Zigachev }
460b843c749SSergey Zigachev 
461b843c749SSergey Zigachev /**
462b843c749SSergey Zigachev  * gmc_v7_0_set_pte_pde - update the page tables using MMIO
463b843c749SSergey Zigachev  *
464b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
465b843c749SSergey Zigachev  * @cpu_pt_addr: cpu address of the page table
466b843c749SSergey Zigachev  * @gpu_page_idx: entry in the page table to update
467b843c749SSergey Zigachev  * @addr: dst addr to write into pte/pde
468b843c749SSergey Zigachev  * @flags: access flags
469b843c749SSergey Zigachev  *
470b843c749SSergey Zigachev  * Update the page tables using the CPU.
471b843c749SSergey Zigachev  */
gmc_v7_0_set_pte_pde(struct amdgpu_device * adev,void * cpu_pt_addr,uint32_t gpu_page_idx,uint64_t addr,uint64_t flags)472b843c749SSergey Zigachev static int gmc_v7_0_set_pte_pde(struct amdgpu_device *adev, void *cpu_pt_addr,
473b843c749SSergey Zigachev 				 uint32_t gpu_page_idx, uint64_t addr,
474b843c749SSergey Zigachev 				 uint64_t flags)
475b843c749SSergey Zigachev {
476b843c749SSergey Zigachev 	void __iomem *ptr = (void *)cpu_pt_addr;
477b843c749SSergey Zigachev 	uint64_t value;
478b843c749SSergey Zigachev 
479b843c749SSergey Zigachev 	value = addr & 0xFFFFFFFFFFFFF000ULL;
480b843c749SSergey Zigachev 	value |= flags;
481b843c749SSergey Zigachev 	writeq(value, ptr + (gpu_page_idx * 8));
482b843c749SSergey Zigachev 
483b843c749SSergey Zigachev 	return 0;
484b843c749SSergey Zigachev }
485b843c749SSergey Zigachev 
gmc_v7_0_get_vm_pte_flags(struct amdgpu_device * adev,uint32_t flags)486b843c749SSergey Zigachev static uint64_t gmc_v7_0_get_vm_pte_flags(struct amdgpu_device *adev,
487b843c749SSergey Zigachev 					  uint32_t flags)
488b843c749SSergey Zigachev {
489b843c749SSergey Zigachev 	uint64_t pte_flag = 0;
490b843c749SSergey Zigachev 
491b843c749SSergey Zigachev 	if (flags & AMDGPU_VM_PAGE_READABLE)
492b843c749SSergey Zigachev 		pte_flag |= AMDGPU_PTE_READABLE;
493b843c749SSergey Zigachev 	if (flags & AMDGPU_VM_PAGE_WRITEABLE)
494b843c749SSergey Zigachev 		pte_flag |= AMDGPU_PTE_WRITEABLE;
495b843c749SSergey Zigachev 	if (flags & AMDGPU_VM_PAGE_PRT)
496b843c749SSergey Zigachev 		pte_flag |= AMDGPU_PTE_PRT;
497b843c749SSergey Zigachev 
498b843c749SSergey Zigachev 	return pte_flag;
499b843c749SSergey Zigachev }
500b843c749SSergey Zigachev 
gmc_v7_0_get_vm_pde(struct amdgpu_device * adev,int level,uint64_t * addr,uint64_t * flags)501b843c749SSergey Zigachev static void gmc_v7_0_get_vm_pde(struct amdgpu_device *adev, int level,
502b843c749SSergey Zigachev 				uint64_t *addr, uint64_t *flags)
503b843c749SSergey Zigachev {
504b843c749SSergey Zigachev 	BUG_ON(*addr & 0xFFFFFF0000000FFFULL);
505b843c749SSergey Zigachev }
506b843c749SSergey Zigachev 
507b843c749SSergey Zigachev /**
508b843c749SSergey Zigachev  * gmc_v8_0_set_fault_enable_default - update VM fault handling
509b843c749SSergey Zigachev  *
510b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
511b843c749SSergey Zigachev  * @value: true redirects VM faults to the default page
512b843c749SSergey Zigachev  */
gmc_v7_0_set_fault_enable_default(struct amdgpu_device * adev,bool value)513b843c749SSergey Zigachev static void gmc_v7_0_set_fault_enable_default(struct amdgpu_device *adev,
514b843c749SSergey Zigachev 					      bool value)
515b843c749SSergey Zigachev {
516b843c749SSergey Zigachev 	u32 tmp;
517b843c749SSergey Zigachev 
518b843c749SSergey Zigachev 	tmp = RREG32(mmVM_CONTEXT1_CNTL);
519b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, VM_CONTEXT1_CNTL,
520b843c749SSergey Zigachev 			    RANGE_PROTECTION_FAULT_ENABLE_DEFAULT, value);
521b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, VM_CONTEXT1_CNTL,
522b843c749SSergey Zigachev 			    DUMMY_PAGE_PROTECTION_FAULT_ENABLE_DEFAULT, value);
523b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, VM_CONTEXT1_CNTL,
524b843c749SSergey Zigachev 			    PDE0_PROTECTION_FAULT_ENABLE_DEFAULT, value);
525b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, VM_CONTEXT1_CNTL,
526b843c749SSergey Zigachev 			    VALID_PROTECTION_FAULT_ENABLE_DEFAULT, value);
527b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, VM_CONTEXT1_CNTL,
528b843c749SSergey Zigachev 			    READ_PROTECTION_FAULT_ENABLE_DEFAULT, value);
529b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, VM_CONTEXT1_CNTL,
530b843c749SSergey Zigachev 			    WRITE_PROTECTION_FAULT_ENABLE_DEFAULT, value);
531b843c749SSergey Zigachev 	WREG32(mmVM_CONTEXT1_CNTL, tmp);
532b843c749SSergey Zigachev }
533b843c749SSergey Zigachev 
534b843c749SSergey Zigachev /**
535b843c749SSergey Zigachev  * gmc_v7_0_set_prt - set PRT VM fault
536b843c749SSergey Zigachev  *
537b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
538b843c749SSergey Zigachev  * @enable: enable/disable VM fault handling for PRT
539b843c749SSergey Zigachev  */
gmc_v7_0_set_prt(struct amdgpu_device * adev,bool enable)540b843c749SSergey Zigachev static void gmc_v7_0_set_prt(struct amdgpu_device *adev, bool enable)
541b843c749SSergey Zigachev {
542b843c749SSergey Zigachev 	uint32_t tmp;
543b843c749SSergey Zigachev 
544b843c749SSergey Zigachev 	if (enable && !adev->gmc.prt_warning) {
545b843c749SSergey Zigachev 		dev_warn(adev->dev, "Disabling VM faults because of PRT request!\n");
546b843c749SSergey Zigachev 		adev->gmc.prt_warning = true;
547b843c749SSergey Zigachev 	}
548b843c749SSergey Zigachev 
549b843c749SSergey Zigachev 	tmp = RREG32(mmVM_PRT_CNTL);
550b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, VM_PRT_CNTL,
551b843c749SSergey Zigachev 			    CB_DISABLE_READ_FAULT_ON_UNMAPPED_ACCESS, enable);
552b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, VM_PRT_CNTL,
553b843c749SSergey Zigachev 			    CB_DISABLE_WRITE_FAULT_ON_UNMAPPED_ACCESS, enable);
554b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, VM_PRT_CNTL,
555b843c749SSergey Zigachev 			    TC_DISABLE_READ_FAULT_ON_UNMAPPED_ACCESS, enable);
556b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, VM_PRT_CNTL,
557b843c749SSergey Zigachev 			    TC_DISABLE_WRITE_FAULT_ON_UNMAPPED_ACCESS, enable);
558b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, VM_PRT_CNTL,
559b843c749SSergey Zigachev 			    L2_CACHE_STORE_INVALID_ENTRIES, enable);
560b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, VM_PRT_CNTL,
561b843c749SSergey Zigachev 			    L1_TLB_STORE_INVALID_ENTRIES, enable);
562b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, VM_PRT_CNTL,
563b843c749SSergey Zigachev 			    MASK_PDE0_FAULT, enable);
564b843c749SSergey Zigachev 	WREG32(mmVM_PRT_CNTL, tmp);
565b843c749SSergey Zigachev 
566b843c749SSergey Zigachev 	if (enable) {
567b843c749SSergey Zigachev 		uint32_t low = AMDGPU_VA_RESERVED_SIZE >> AMDGPU_GPU_PAGE_SHIFT;
568b843c749SSergey Zigachev 		uint32_t high = adev->vm_manager.max_pfn -
569b843c749SSergey Zigachev 			(AMDGPU_VA_RESERVED_SIZE >> AMDGPU_GPU_PAGE_SHIFT);
570b843c749SSergey Zigachev 
571b843c749SSergey Zigachev 		WREG32(mmVM_PRT_APERTURE0_LOW_ADDR, low);
572b843c749SSergey Zigachev 		WREG32(mmVM_PRT_APERTURE1_LOW_ADDR, low);
573b843c749SSergey Zigachev 		WREG32(mmVM_PRT_APERTURE2_LOW_ADDR, low);
574b843c749SSergey Zigachev 		WREG32(mmVM_PRT_APERTURE3_LOW_ADDR, low);
575b843c749SSergey Zigachev 		WREG32(mmVM_PRT_APERTURE0_HIGH_ADDR, high);
576b843c749SSergey Zigachev 		WREG32(mmVM_PRT_APERTURE1_HIGH_ADDR, high);
577b843c749SSergey Zigachev 		WREG32(mmVM_PRT_APERTURE2_HIGH_ADDR, high);
578b843c749SSergey Zigachev 		WREG32(mmVM_PRT_APERTURE3_HIGH_ADDR, high);
579b843c749SSergey Zigachev 	} else {
580b843c749SSergey Zigachev 		WREG32(mmVM_PRT_APERTURE0_LOW_ADDR, 0xfffffff);
581b843c749SSergey Zigachev 		WREG32(mmVM_PRT_APERTURE1_LOW_ADDR, 0xfffffff);
582b843c749SSergey Zigachev 		WREG32(mmVM_PRT_APERTURE2_LOW_ADDR, 0xfffffff);
583b843c749SSergey Zigachev 		WREG32(mmVM_PRT_APERTURE3_LOW_ADDR, 0xfffffff);
584b843c749SSergey Zigachev 		WREG32(mmVM_PRT_APERTURE0_HIGH_ADDR, 0x0);
585b843c749SSergey Zigachev 		WREG32(mmVM_PRT_APERTURE1_HIGH_ADDR, 0x0);
586b843c749SSergey Zigachev 		WREG32(mmVM_PRT_APERTURE2_HIGH_ADDR, 0x0);
587b843c749SSergey Zigachev 		WREG32(mmVM_PRT_APERTURE3_HIGH_ADDR, 0x0);
588b843c749SSergey Zigachev 	}
589b843c749SSergey Zigachev }
590b843c749SSergey Zigachev 
591b843c749SSergey Zigachev /**
592b843c749SSergey Zigachev  * gmc_v7_0_gart_enable - gart enable
593b843c749SSergey Zigachev  *
594b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
595b843c749SSergey Zigachev  *
596b843c749SSergey Zigachev  * This sets up the TLBs, programs the page tables for VMID0,
597b843c749SSergey Zigachev  * sets up the hw for VMIDs 1-15 which are allocated on
598b843c749SSergey Zigachev  * demand, and sets up the global locations for the LDS, GDS,
599b843c749SSergey Zigachev  * and GPUVM for FSA64 clients (CIK).
600b843c749SSergey Zigachev  * Returns 0 for success, errors for failure.
601b843c749SSergey Zigachev  */
gmc_v7_0_gart_enable(struct amdgpu_device * adev)602b843c749SSergey Zigachev static int gmc_v7_0_gart_enable(struct amdgpu_device *adev)
603b843c749SSergey Zigachev {
604b843c749SSergey Zigachev 	int r, i;
605b843c749SSergey Zigachev 	u32 tmp, field;
606b843c749SSergey Zigachev 
607b843c749SSergey Zigachev 	if (adev->gart.robj == NULL) {
608b843c749SSergey Zigachev 		dev_err(adev->dev, "No VRAM object for PCIE GART.\n");
609b843c749SSergey Zigachev 		return -EINVAL;
610b843c749SSergey Zigachev 	}
611b843c749SSergey Zigachev 	r = amdgpu_gart_table_vram_pin(adev);
612b843c749SSergey Zigachev 	if (r)
613b843c749SSergey Zigachev 		return r;
614b843c749SSergey Zigachev 	/* Setup TLB control */
615b843c749SSergey Zigachev 	tmp = RREG32(mmMC_VM_MX_L1_TLB_CNTL);
616b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, MC_VM_MX_L1_TLB_CNTL, ENABLE_L1_TLB, 1);
617b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, MC_VM_MX_L1_TLB_CNTL, ENABLE_L1_FRAGMENT_PROCESSING, 1);
618b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, MC_VM_MX_L1_TLB_CNTL, SYSTEM_ACCESS_MODE, 3);
619b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, MC_VM_MX_L1_TLB_CNTL, ENABLE_ADVANCED_DRIVER_MODEL, 1);
620b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, MC_VM_MX_L1_TLB_CNTL, SYSTEM_APERTURE_UNMAPPED_ACCESS, 0);
621b843c749SSergey Zigachev 	WREG32(mmMC_VM_MX_L1_TLB_CNTL, tmp);
622b843c749SSergey Zigachev 	/* Setup L2 cache */
623b843c749SSergey Zigachev 	tmp = RREG32(mmVM_L2_CNTL);
624b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, VM_L2_CNTL, ENABLE_L2_CACHE, 1);
625b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, VM_L2_CNTL, ENABLE_L2_FRAGMENT_PROCESSING, 1);
626b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, VM_L2_CNTL, ENABLE_L2_PTE_CACHE_LRU_UPDATE_BY_WRITE, 1);
627b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, VM_L2_CNTL, ENABLE_L2_PDE0_CACHE_LRU_UPDATE_BY_WRITE, 1);
628b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, VM_L2_CNTL, EFFECTIVE_L2_QUEUE_SIZE, 7);
629b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, VM_L2_CNTL, CONTEXT1_IDENTITY_ACCESS_MODE, 1);
630b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, VM_L2_CNTL, ENABLE_DEFAULT_PAGE_OUT_TO_SYSTEM_MEMORY, 1);
631b843c749SSergey Zigachev 	WREG32(mmVM_L2_CNTL, tmp);
632b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(0, VM_L2_CNTL2, INVALIDATE_ALL_L1_TLBS, 1);
633b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, VM_L2_CNTL2, INVALIDATE_L2_CACHE, 1);
634b843c749SSergey Zigachev 	WREG32(mmVM_L2_CNTL2, tmp);
635b843c749SSergey Zigachev 
636b843c749SSergey Zigachev 	field = adev->vm_manager.fragment_size;
637b843c749SSergey Zigachev 	tmp = RREG32(mmVM_L2_CNTL3);
638b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, VM_L2_CNTL3, L2_CACHE_BIGK_ASSOCIATIVITY, 1);
639b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, VM_L2_CNTL3, BANK_SELECT, field);
640b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, VM_L2_CNTL3, L2_CACHE_BIGK_FRAGMENT_SIZE, field);
641b843c749SSergey Zigachev 	WREG32(mmVM_L2_CNTL3, tmp);
642b843c749SSergey Zigachev 	/* setup context0 */
643b843c749SSergey Zigachev 	WREG32(mmVM_CONTEXT0_PAGE_TABLE_START_ADDR, adev->gmc.gart_start >> 12);
644b843c749SSergey Zigachev 	WREG32(mmVM_CONTEXT0_PAGE_TABLE_END_ADDR, adev->gmc.gart_end >> 12);
645b843c749SSergey Zigachev 	WREG32(mmVM_CONTEXT0_PAGE_TABLE_BASE_ADDR, adev->gart.table_addr >> 12);
646b843c749SSergey Zigachev 	WREG32(mmVM_CONTEXT0_PROTECTION_FAULT_DEFAULT_ADDR,
647b843c749SSergey Zigachev 			(u32)(adev->dummy_page_addr >> 12));
648b843c749SSergey Zigachev 	WREG32(mmVM_CONTEXT0_CNTL2, 0);
649b843c749SSergey Zigachev 	tmp = RREG32(mmVM_CONTEXT0_CNTL);
650b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, VM_CONTEXT0_CNTL, ENABLE_CONTEXT, 1);
651b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, VM_CONTEXT0_CNTL, PAGE_TABLE_DEPTH, 0);
652b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, VM_CONTEXT0_CNTL, RANGE_PROTECTION_FAULT_ENABLE_DEFAULT, 1);
653b843c749SSergey Zigachev 	WREG32(mmVM_CONTEXT0_CNTL, tmp);
654b843c749SSergey Zigachev 
655b843c749SSergey Zigachev 	WREG32(0x575, 0);
656b843c749SSergey Zigachev 	WREG32(0x576, 0);
657b843c749SSergey Zigachev 	WREG32(0x577, 0);
658b843c749SSergey Zigachev 
659b843c749SSergey Zigachev 	/* empty context1-15 */
660b843c749SSergey Zigachev 	/* FIXME start with 4G, once using 2 level pt switch to full
661b843c749SSergey Zigachev 	 * vm size space
662b843c749SSergey Zigachev 	 */
663b843c749SSergey Zigachev 	/* set vm size, must be a multiple of 4 */
664b843c749SSergey Zigachev 	WREG32(mmVM_CONTEXT1_PAGE_TABLE_START_ADDR, 0);
665b843c749SSergey Zigachev 	WREG32(mmVM_CONTEXT1_PAGE_TABLE_END_ADDR, adev->vm_manager.max_pfn - 1);
666b843c749SSergey Zigachev 	for (i = 1; i < 16; i++) {
667b843c749SSergey Zigachev 		if (i < 8)
668b843c749SSergey Zigachev 			WREG32(mmVM_CONTEXT0_PAGE_TABLE_BASE_ADDR + i,
669b843c749SSergey Zigachev 			       adev->gart.table_addr >> 12);
670b843c749SSergey Zigachev 		else
671b843c749SSergey Zigachev 			WREG32(mmVM_CONTEXT8_PAGE_TABLE_BASE_ADDR + i - 8,
672b843c749SSergey Zigachev 			       adev->gart.table_addr >> 12);
673b843c749SSergey Zigachev 	}
674b843c749SSergey Zigachev 
675b843c749SSergey Zigachev 	/* enable context1-15 */
676b843c749SSergey Zigachev 	WREG32(mmVM_CONTEXT1_PROTECTION_FAULT_DEFAULT_ADDR,
677b843c749SSergey Zigachev 	       (u32)(adev->dummy_page_addr >> 12));
678b843c749SSergey Zigachev 	WREG32(mmVM_CONTEXT1_CNTL2, 4);
679b843c749SSergey Zigachev 	tmp = RREG32(mmVM_CONTEXT1_CNTL);
680b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, VM_CONTEXT1_CNTL, ENABLE_CONTEXT, 1);
681b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, VM_CONTEXT1_CNTL, PAGE_TABLE_DEPTH, 1);
682b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, VM_CONTEXT1_CNTL, PAGE_TABLE_BLOCK_SIZE,
683b843c749SSergey Zigachev 			    adev->vm_manager.block_size - 9);
684b843c749SSergey Zigachev 	WREG32(mmVM_CONTEXT1_CNTL, tmp);
685b843c749SSergey Zigachev 	if (amdgpu_vm_fault_stop == AMDGPU_VM_FAULT_STOP_ALWAYS)
686b843c749SSergey Zigachev 		gmc_v7_0_set_fault_enable_default(adev, false);
687b843c749SSergey Zigachev 	else
688b843c749SSergey Zigachev 		gmc_v7_0_set_fault_enable_default(adev, true);
689b843c749SSergey Zigachev 
690b843c749SSergey Zigachev 	if (adev->asic_type == CHIP_KAVERI) {
691b843c749SSergey Zigachev 		tmp = RREG32(mmCHUB_CONTROL);
692b843c749SSergey Zigachev 		tmp &= ~BYPASS_VM;
693b843c749SSergey Zigachev 		WREG32(mmCHUB_CONTROL, tmp);
694b843c749SSergey Zigachev 	}
695b843c749SSergey Zigachev 
696b843c749SSergey Zigachev 	gmc_v7_0_flush_gpu_tlb(adev, 0);
697b843c749SSergey Zigachev 	DRM_INFO("PCIE GART of %uM enabled (table at 0x%016llX).\n",
698b843c749SSergey Zigachev 		 (unsigned)(adev->gmc.gart_size >> 20),
699b843c749SSergey Zigachev 		 (unsigned long long)adev->gart.table_addr);
700b843c749SSergey Zigachev 	adev->gart.ready = true;
701b843c749SSergey Zigachev 	return 0;
702b843c749SSergey Zigachev }
703b843c749SSergey Zigachev 
gmc_v7_0_gart_init(struct amdgpu_device * adev)704b843c749SSergey Zigachev static int gmc_v7_0_gart_init(struct amdgpu_device *adev)
705b843c749SSergey Zigachev {
706b843c749SSergey Zigachev 	int r;
707b843c749SSergey Zigachev 
708b843c749SSergey Zigachev 	if (adev->gart.robj) {
709b843c749SSergey Zigachev 		WARN(1, "R600 PCIE GART already initialized\n");
710b843c749SSergey Zigachev 		return 0;
711b843c749SSergey Zigachev 	}
712b843c749SSergey Zigachev 	/* Initialize common gart structure */
713b843c749SSergey Zigachev 	r = amdgpu_gart_init(adev);
714b843c749SSergey Zigachev 	if (r)
715b843c749SSergey Zigachev 		return r;
716b843c749SSergey Zigachev 	adev->gart.table_size = adev->gart.num_gpu_pages * 8;
717b843c749SSergey Zigachev 	adev->gart.gart_pte_flags = 0;
718b843c749SSergey Zigachev 	return amdgpu_gart_table_vram_alloc(adev);
719b843c749SSergey Zigachev }
720b843c749SSergey Zigachev 
721b843c749SSergey Zigachev /**
722b843c749SSergey Zigachev  * gmc_v7_0_gart_disable - gart disable
723b843c749SSergey Zigachev  *
724b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
725b843c749SSergey Zigachev  *
726b843c749SSergey Zigachev  * This disables all VM page table (CIK).
727b843c749SSergey Zigachev  */
gmc_v7_0_gart_disable(struct amdgpu_device * adev)728b843c749SSergey Zigachev static void gmc_v7_0_gart_disable(struct amdgpu_device *adev)
729b843c749SSergey Zigachev {
730b843c749SSergey Zigachev 	u32 tmp;
731b843c749SSergey Zigachev 
732b843c749SSergey Zigachev 	/* Disable all tables */
733b843c749SSergey Zigachev 	WREG32(mmVM_CONTEXT0_CNTL, 0);
734b843c749SSergey Zigachev 	WREG32(mmVM_CONTEXT1_CNTL, 0);
735b843c749SSergey Zigachev 	/* Setup TLB control */
736b843c749SSergey Zigachev 	tmp = RREG32(mmMC_VM_MX_L1_TLB_CNTL);
737b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, MC_VM_MX_L1_TLB_CNTL, ENABLE_L1_TLB, 0);
738b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, MC_VM_MX_L1_TLB_CNTL, ENABLE_L1_FRAGMENT_PROCESSING, 0);
739b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, MC_VM_MX_L1_TLB_CNTL, ENABLE_ADVANCED_DRIVER_MODEL, 0);
740b843c749SSergey Zigachev 	WREG32(mmMC_VM_MX_L1_TLB_CNTL, tmp);
741b843c749SSergey Zigachev 	/* Setup L2 cache */
742b843c749SSergey Zigachev 	tmp = RREG32(mmVM_L2_CNTL);
743b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, VM_L2_CNTL, ENABLE_L2_CACHE, 0);
744b843c749SSergey Zigachev 	WREG32(mmVM_L2_CNTL, tmp);
745b843c749SSergey Zigachev 	WREG32(mmVM_L2_CNTL2, 0);
746b843c749SSergey Zigachev 	amdgpu_gart_table_vram_unpin(adev);
747b843c749SSergey Zigachev }
748b843c749SSergey Zigachev 
749b843c749SSergey Zigachev /**
750b843c749SSergey Zigachev  * gmc_v7_0_vm_decode_fault - print human readable fault info
751b843c749SSergey Zigachev  *
752b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
753b843c749SSergey Zigachev  * @status: VM_CONTEXT1_PROTECTION_FAULT_STATUS register value
754b843c749SSergey Zigachev  * @addr: VM_CONTEXT1_PROTECTION_FAULT_ADDR register value
755b843c749SSergey Zigachev  *
756b843c749SSergey Zigachev  * Print human readable fault information (CIK).
757b843c749SSergey Zigachev  */
gmc_v7_0_vm_decode_fault(struct amdgpu_device * adev,u32 status,u32 addr,u32 mc_client,unsigned pasid)758b843c749SSergey Zigachev static void gmc_v7_0_vm_decode_fault(struct amdgpu_device *adev, u32 status,
759b843c749SSergey Zigachev 				     u32 addr, u32 mc_client, unsigned pasid)
760b843c749SSergey Zigachev {
761b843c749SSergey Zigachev 	u32 vmid = REG_GET_FIELD(status, VM_CONTEXT1_PROTECTION_FAULT_STATUS, VMID);
762b843c749SSergey Zigachev 	u32 protections = REG_GET_FIELD(status, VM_CONTEXT1_PROTECTION_FAULT_STATUS,
763b843c749SSergey Zigachev 					PROTECTIONS);
764b843c749SSergey Zigachev 	char block[5] = { mc_client >> 24, (mc_client >> 16) & 0xff,
765b843c749SSergey Zigachev 		(mc_client >> 8) & 0xff, mc_client & 0xff, 0 };
766b843c749SSergey Zigachev 	u32 mc_id;
767b843c749SSergey Zigachev 
768b843c749SSergey Zigachev 	mc_id = REG_GET_FIELD(status, VM_CONTEXT1_PROTECTION_FAULT_STATUS,
769b843c749SSergey Zigachev 			      MEMORY_CLIENT_ID);
770b843c749SSergey Zigachev 
771b843c749SSergey Zigachev 	dev_err(adev->dev, "VM fault (0x%02x, vmid %d, pasid %d) at page %u, %s from '%s' (0x%08x) (%d)\n",
772b843c749SSergey Zigachev 	       protections, vmid, pasid, addr,
773b843c749SSergey Zigachev 	       REG_GET_FIELD(status, VM_CONTEXT1_PROTECTION_FAULT_STATUS,
774b843c749SSergey Zigachev 			     MEMORY_CLIENT_RW) ?
775b843c749SSergey Zigachev 	       "write" : "read", block, mc_client, mc_id);
776b843c749SSergey Zigachev }
777b843c749SSergey Zigachev 
778b843c749SSergey Zigachev 
779b843c749SSergey Zigachev static const u32 mc_cg_registers[] = {
780b843c749SSergey Zigachev 	mmMC_HUB_MISC_HUB_CG,
781b843c749SSergey Zigachev 	mmMC_HUB_MISC_SIP_CG,
782b843c749SSergey Zigachev 	mmMC_HUB_MISC_VM_CG,
783b843c749SSergey Zigachev 	mmMC_XPB_CLK_GAT,
784b843c749SSergey Zigachev 	mmATC_MISC_CG,
785b843c749SSergey Zigachev 	mmMC_CITF_MISC_WR_CG,
786b843c749SSergey Zigachev 	mmMC_CITF_MISC_RD_CG,
787b843c749SSergey Zigachev 	mmMC_CITF_MISC_VM_CG,
788b843c749SSergey Zigachev 	mmVM_L2_CG,
789b843c749SSergey Zigachev };
790b843c749SSergey Zigachev 
791b843c749SSergey Zigachev static const u32 mc_cg_ls_en[] = {
792b843c749SSergey Zigachev 	MC_HUB_MISC_HUB_CG__MEM_LS_ENABLE_MASK,
793b843c749SSergey Zigachev 	MC_HUB_MISC_SIP_CG__MEM_LS_ENABLE_MASK,
794b843c749SSergey Zigachev 	MC_HUB_MISC_VM_CG__MEM_LS_ENABLE_MASK,
795b843c749SSergey Zigachev 	MC_XPB_CLK_GAT__MEM_LS_ENABLE_MASK,
796b843c749SSergey Zigachev 	ATC_MISC_CG__MEM_LS_ENABLE_MASK,
797b843c749SSergey Zigachev 	MC_CITF_MISC_WR_CG__MEM_LS_ENABLE_MASK,
798b843c749SSergey Zigachev 	MC_CITF_MISC_RD_CG__MEM_LS_ENABLE_MASK,
799b843c749SSergey Zigachev 	MC_CITF_MISC_VM_CG__MEM_LS_ENABLE_MASK,
800b843c749SSergey Zigachev 	VM_L2_CG__MEM_LS_ENABLE_MASK,
801b843c749SSergey Zigachev };
802b843c749SSergey Zigachev 
803b843c749SSergey Zigachev static const u32 mc_cg_en[] = {
804b843c749SSergey Zigachev 	MC_HUB_MISC_HUB_CG__ENABLE_MASK,
805b843c749SSergey Zigachev 	MC_HUB_MISC_SIP_CG__ENABLE_MASK,
806b843c749SSergey Zigachev 	MC_HUB_MISC_VM_CG__ENABLE_MASK,
807b843c749SSergey Zigachev 	MC_XPB_CLK_GAT__ENABLE_MASK,
808b843c749SSergey Zigachev 	ATC_MISC_CG__ENABLE_MASK,
809b843c749SSergey Zigachev 	MC_CITF_MISC_WR_CG__ENABLE_MASK,
810b843c749SSergey Zigachev 	MC_CITF_MISC_RD_CG__ENABLE_MASK,
811b843c749SSergey Zigachev 	MC_CITF_MISC_VM_CG__ENABLE_MASK,
812b843c749SSergey Zigachev 	VM_L2_CG__ENABLE_MASK,
813b843c749SSergey Zigachev };
814b843c749SSergey Zigachev 
gmc_v7_0_enable_mc_ls(struct amdgpu_device * adev,bool enable)815b843c749SSergey Zigachev static void gmc_v7_0_enable_mc_ls(struct amdgpu_device *adev,
816b843c749SSergey Zigachev 				  bool enable)
817b843c749SSergey Zigachev {
818b843c749SSergey Zigachev 	int i;
819b843c749SSergey Zigachev 	u32 orig, data;
820b843c749SSergey Zigachev 
821b843c749SSergey Zigachev 	for (i = 0; i < ARRAY_SIZE(mc_cg_registers); i++) {
822b843c749SSergey Zigachev 		orig = data = RREG32(mc_cg_registers[i]);
823b843c749SSergey Zigachev 		if (enable && (adev->cg_flags & AMD_CG_SUPPORT_MC_LS))
824b843c749SSergey Zigachev 			data |= mc_cg_ls_en[i];
825b843c749SSergey Zigachev 		else
826b843c749SSergey Zigachev 			data &= ~mc_cg_ls_en[i];
827b843c749SSergey Zigachev 		if (data != orig)
828b843c749SSergey Zigachev 			WREG32(mc_cg_registers[i], data);
829b843c749SSergey Zigachev 	}
830b843c749SSergey Zigachev }
831b843c749SSergey Zigachev 
gmc_v7_0_enable_mc_mgcg(struct amdgpu_device * adev,bool enable)832b843c749SSergey Zigachev static void gmc_v7_0_enable_mc_mgcg(struct amdgpu_device *adev,
833b843c749SSergey Zigachev 				    bool enable)
834b843c749SSergey Zigachev {
835b843c749SSergey Zigachev 	int i;
836b843c749SSergey Zigachev 	u32 orig, data;
837b843c749SSergey Zigachev 
838b843c749SSergey Zigachev 	for (i = 0; i < ARRAY_SIZE(mc_cg_registers); i++) {
839b843c749SSergey Zigachev 		orig = data = RREG32(mc_cg_registers[i]);
840b843c749SSergey Zigachev 		if (enable && (adev->cg_flags & AMD_CG_SUPPORT_MC_MGCG))
841b843c749SSergey Zigachev 			data |= mc_cg_en[i];
842b843c749SSergey Zigachev 		else
843b843c749SSergey Zigachev 			data &= ~mc_cg_en[i];
844b843c749SSergey Zigachev 		if (data != orig)
845b843c749SSergey Zigachev 			WREG32(mc_cg_registers[i], data);
846b843c749SSergey Zigachev 	}
847b843c749SSergey Zigachev }
848b843c749SSergey Zigachev 
gmc_v7_0_enable_bif_mgls(struct amdgpu_device * adev,bool enable)849b843c749SSergey Zigachev static void gmc_v7_0_enable_bif_mgls(struct amdgpu_device *adev,
850b843c749SSergey Zigachev 				     bool enable)
851b843c749SSergey Zigachev {
852b843c749SSergey Zigachev 	u32 orig, data;
853b843c749SSergey Zigachev 
854b843c749SSergey Zigachev 	orig = data = RREG32_PCIE(ixPCIE_CNTL2);
855b843c749SSergey Zigachev 
856b843c749SSergey Zigachev 	if (enable && (adev->cg_flags & AMD_CG_SUPPORT_BIF_LS)) {
857b843c749SSergey Zigachev 		data = REG_SET_FIELD(data, PCIE_CNTL2, SLV_MEM_LS_EN, 1);
858b843c749SSergey Zigachev 		data = REG_SET_FIELD(data, PCIE_CNTL2, MST_MEM_LS_EN, 1);
859b843c749SSergey Zigachev 		data = REG_SET_FIELD(data, PCIE_CNTL2, REPLAY_MEM_LS_EN, 1);
860b843c749SSergey Zigachev 		data = REG_SET_FIELD(data, PCIE_CNTL2, SLV_MEM_AGGRESSIVE_LS_EN, 1);
861b843c749SSergey Zigachev 	} else {
862b843c749SSergey Zigachev 		data = REG_SET_FIELD(data, PCIE_CNTL2, SLV_MEM_LS_EN, 0);
863b843c749SSergey Zigachev 		data = REG_SET_FIELD(data, PCIE_CNTL2, MST_MEM_LS_EN, 0);
864b843c749SSergey Zigachev 		data = REG_SET_FIELD(data, PCIE_CNTL2, REPLAY_MEM_LS_EN, 0);
865b843c749SSergey Zigachev 		data = REG_SET_FIELD(data, PCIE_CNTL2, SLV_MEM_AGGRESSIVE_LS_EN, 0);
866b843c749SSergey Zigachev 	}
867b843c749SSergey Zigachev 
868b843c749SSergey Zigachev 	if (orig != data)
869b843c749SSergey Zigachev 		WREG32_PCIE(ixPCIE_CNTL2, data);
870b843c749SSergey Zigachev }
871b843c749SSergey Zigachev 
gmc_v7_0_enable_hdp_mgcg(struct amdgpu_device * adev,bool enable)872b843c749SSergey Zigachev static void gmc_v7_0_enable_hdp_mgcg(struct amdgpu_device *adev,
873b843c749SSergey Zigachev 				     bool enable)
874b843c749SSergey Zigachev {
875b843c749SSergey Zigachev 	u32 orig, data;
876b843c749SSergey Zigachev 
877b843c749SSergey Zigachev 	orig = data = RREG32(mmHDP_HOST_PATH_CNTL);
878b843c749SSergey Zigachev 
879b843c749SSergey Zigachev 	if (enable && (adev->cg_flags & AMD_CG_SUPPORT_HDP_MGCG))
880b843c749SSergey Zigachev 		data = REG_SET_FIELD(data, HDP_HOST_PATH_CNTL, CLOCK_GATING_DIS, 0);
881b843c749SSergey Zigachev 	else
882b843c749SSergey Zigachev 		data = REG_SET_FIELD(data, HDP_HOST_PATH_CNTL, CLOCK_GATING_DIS, 1);
883b843c749SSergey Zigachev 
884b843c749SSergey Zigachev 	if (orig != data)
885b843c749SSergey Zigachev 		WREG32(mmHDP_HOST_PATH_CNTL, data);
886b843c749SSergey Zigachev }
887b843c749SSergey Zigachev 
gmc_v7_0_enable_hdp_ls(struct amdgpu_device * adev,bool enable)888b843c749SSergey Zigachev static void gmc_v7_0_enable_hdp_ls(struct amdgpu_device *adev,
889b843c749SSergey Zigachev 				   bool enable)
890b843c749SSergey Zigachev {
891b843c749SSergey Zigachev 	u32 orig, data;
892b843c749SSergey Zigachev 
893b843c749SSergey Zigachev 	orig = data = RREG32(mmHDP_MEM_POWER_LS);
894b843c749SSergey Zigachev 
895b843c749SSergey Zigachev 	if (enable && (adev->cg_flags & AMD_CG_SUPPORT_HDP_LS))
896b843c749SSergey Zigachev 		data = REG_SET_FIELD(data, HDP_MEM_POWER_LS, LS_ENABLE, 1);
897b843c749SSergey Zigachev 	else
898b843c749SSergey Zigachev 		data = REG_SET_FIELD(data, HDP_MEM_POWER_LS, LS_ENABLE, 0);
899b843c749SSergey Zigachev 
900b843c749SSergey Zigachev 	if (orig != data)
901b843c749SSergey Zigachev 		WREG32(mmHDP_MEM_POWER_LS, data);
902b843c749SSergey Zigachev }
903b843c749SSergey Zigachev 
gmc_v7_0_convert_vram_type(int mc_seq_vram_type)904b843c749SSergey Zigachev static int gmc_v7_0_convert_vram_type(int mc_seq_vram_type)
905b843c749SSergey Zigachev {
906b843c749SSergey Zigachev 	switch (mc_seq_vram_type) {
907b843c749SSergey Zigachev 	case MC_SEQ_MISC0__MT__GDDR1:
908b843c749SSergey Zigachev 		return AMDGPU_VRAM_TYPE_GDDR1;
909b843c749SSergey Zigachev 	case MC_SEQ_MISC0__MT__DDR2:
910b843c749SSergey Zigachev 		return AMDGPU_VRAM_TYPE_DDR2;
911b843c749SSergey Zigachev 	case MC_SEQ_MISC0__MT__GDDR3:
912b843c749SSergey Zigachev 		return AMDGPU_VRAM_TYPE_GDDR3;
913b843c749SSergey Zigachev 	case MC_SEQ_MISC0__MT__GDDR4:
914b843c749SSergey Zigachev 		return AMDGPU_VRAM_TYPE_GDDR4;
915b843c749SSergey Zigachev 	case MC_SEQ_MISC0__MT__GDDR5:
916b843c749SSergey Zigachev 		return AMDGPU_VRAM_TYPE_GDDR5;
917b843c749SSergey Zigachev 	case MC_SEQ_MISC0__MT__HBM:
918b843c749SSergey Zigachev 		return AMDGPU_VRAM_TYPE_HBM;
919b843c749SSergey Zigachev 	case MC_SEQ_MISC0__MT__DDR3:
920b843c749SSergey Zigachev 		return AMDGPU_VRAM_TYPE_DDR3;
921b843c749SSergey Zigachev 	default:
922b843c749SSergey Zigachev 		return AMDGPU_VRAM_TYPE_UNKNOWN;
923b843c749SSergey Zigachev 	}
924b843c749SSergey Zigachev }
925b843c749SSergey Zigachev 
gmc_v7_0_early_init(void * handle)926b843c749SSergey Zigachev static int gmc_v7_0_early_init(void *handle)
927b843c749SSergey Zigachev {
928b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
929b843c749SSergey Zigachev 
930b843c749SSergey Zigachev 	gmc_v7_0_set_gmc_funcs(adev);
931b843c749SSergey Zigachev 	gmc_v7_0_set_irq_funcs(adev);
932b843c749SSergey Zigachev 
933b843c749SSergey Zigachev 	adev->gmc.shared_aperture_start = 0x2000000000000000ULL;
934b843c749SSergey Zigachev 	adev->gmc.shared_aperture_end =
935b843c749SSergey Zigachev 		adev->gmc.shared_aperture_start + (4ULL << 30) - 1;
936b843c749SSergey Zigachev 	adev->gmc.private_aperture_start =
937b843c749SSergey Zigachev 		adev->gmc.shared_aperture_end + 1;
938b843c749SSergey Zigachev 	adev->gmc.private_aperture_end =
939b843c749SSergey Zigachev 		adev->gmc.private_aperture_start + (4ULL << 30) - 1;
940b843c749SSergey Zigachev 
941b843c749SSergey Zigachev 	return 0;
942b843c749SSergey Zigachev }
943b843c749SSergey Zigachev 
gmc_v7_0_late_init(void * handle)944b843c749SSergey Zigachev static int gmc_v7_0_late_init(void *handle)
945b843c749SSergey Zigachev {
946b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
947b843c749SSergey Zigachev 
948b843c749SSergey Zigachev 	amdgpu_bo_late_init(adev);
949b843c749SSergey Zigachev 
950b843c749SSergey Zigachev 	if (amdgpu_vm_fault_stop != AMDGPU_VM_FAULT_STOP_ALWAYS)
951b843c749SSergey Zigachev 		return amdgpu_irq_get(adev, &adev->gmc.vm_fault, 0);
952b843c749SSergey Zigachev 	else
953b843c749SSergey Zigachev 		return 0;
954b843c749SSergey Zigachev }
955b843c749SSergey Zigachev 
gmc_v7_0_get_vbios_fb_size(struct amdgpu_device * adev)956b843c749SSergey Zigachev static unsigned gmc_v7_0_get_vbios_fb_size(struct amdgpu_device *adev)
957b843c749SSergey Zigachev {
958b843c749SSergey Zigachev 	u32 d1vga_control = RREG32(mmD1VGA_CONTROL);
959b843c749SSergey Zigachev 	unsigned size;
960b843c749SSergey Zigachev 
961b843c749SSergey Zigachev 	if (REG_GET_FIELD(d1vga_control, D1VGA_CONTROL, D1VGA_MODE_ENABLE)) {
962b843c749SSergey Zigachev 		size = 9 * 1024 * 1024; /* reserve 8MB for vga emulator and 1 MB for FB */
963b843c749SSergey Zigachev 	} else {
964b843c749SSergey Zigachev 		u32 viewport = RREG32(mmVIEWPORT_SIZE);
965b843c749SSergey Zigachev 		size = (REG_GET_FIELD(viewport, VIEWPORT_SIZE, VIEWPORT_HEIGHT) *
966b843c749SSergey Zigachev 			REG_GET_FIELD(viewport, VIEWPORT_SIZE, VIEWPORT_WIDTH) *
967b843c749SSergey Zigachev 			4);
968b843c749SSergey Zigachev 	}
969b843c749SSergey Zigachev 	/* return 0 if the pre-OS buffer uses up most of vram */
970b843c749SSergey Zigachev 	if ((adev->gmc.real_vram_size - size) < (8 * 1024 * 1024))
971b843c749SSergey Zigachev 		return 0;
972b843c749SSergey Zigachev 	return size;
973b843c749SSergey Zigachev }
974b843c749SSergey Zigachev 
gmc_v7_0_sw_init(void * handle)975b843c749SSergey Zigachev static int gmc_v7_0_sw_init(void *handle)
976b843c749SSergey Zigachev {
977b843c749SSergey Zigachev 	int r;
978b843c749SSergey Zigachev 	int dma_bits;
979b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
980b843c749SSergey Zigachev 
981b843c749SSergey Zigachev 	if (adev->flags & AMD_IS_APU) {
982b843c749SSergey Zigachev 		adev->gmc.vram_type = AMDGPU_VRAM_TYPE_UNKNOWN;
983b843c749SSergey Zigachev 	} else {
984b843c749SSergey Zigachev 		u32 tmp = RREG32(mmMC_SEQ_MISC0);
985b843c749SSergey Zigachev 		tmp &= MC_SEQ_MISC0__MT__MASK;
986b843c749SSergey Zigachev 		adev->gmc.vram_type = gmc_v7_0_convert_vram_type(tmp);
987b843c749SSergey Zigachev 	}
988b843c749SSergey Zigachev 
989b843c749SSergey Zigachev 	r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_LEGACY, VISLANDS30_IV_SRCID_GFX_PAGE_INV_FAULT, &adev->gmc.vm_fault);
990b843c749SSergey Zigachev 	if (r)
991b843c749SSergey Zigachev 		return r;
992b843c749SSergey Zigachev 
993b843c749SSergey Zigachev 	r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_LEGACY, VISLANDS30_IV_SRCID_GFX_MEM_PROT_FAULT, &adev->gmc.vm_fault);
994b843c749SSergey Zigachev 	if (r)
995b843c749SSergey Zigachev 		return r;
996b843c749SSergey Zigachev 
997b843c749SSergey Zigachev 	/* Adjust VM size here.
998b843c749SSergey Zigachev 	 * Currently set to 4GB ((1 << 20) 4k pages).
999b843c749SSergey Zigachev 	 * Max GPUVM size for cayman and SI is 40 bits.
1000b843c749SSergey Zigachev 	 */
1001b843c749SSergey Zigachev 	amdgpu_vm_adjust_size(adev, 64, 9, 1, 40);
1002b843c749SSergey Zigachev 
1003b843c749SSergey Zigachev 	/* Set the internal MC address mask
1004b843c749SSergey Zigachev 	 * This is the max address of the GPU's
1005b843c749SSergey Zigachev 	 * internal address space.
1006b843c749SSergey Zigachev 	 */
1007b843c749SSergey Zigachev 	adev->gmc.mc_mask = 0xffffffffffULL; /* 40 bit MC */
1008b843c749SSergey Zigachev 
1009b843c749SSergey Zigachev 	/* set DMA mask + need_dma32 flags.
1010b843c749SSergey Zigachev 	 * PCIE - can handle 40-bits.
1011b843c749SSergey Zigachev 	 * IGP - can handle 40-bits
1012b843c749SSergey Zigachev 	 * PCI - dma32 for legacy pci gart, 40 bits on newer asics
1013b843c749SSergey Zigachev 	 */
1014b843c749SSergey Zigachev 	adev->need_dma32 = false;
1015b843c749SSergey Zigachev 	dma_bits = adev->need_dma32 ? 32 : 40;
1016b843c749SSergey Zigachev 	r = pci_set_dma_mask(adev->pdev, DMA_BIT_MASK(dma_bits));
1017b843c749SSergey Zigachev 	if (r) {
1018b843c749SSergey Zigachev 		adev->need_dma32 = true;
1019b843c749SSergey Zigachev 		dma_bits = 32;
1020b843c749SSergey Zigachev 		pr_warn("amdgpu: No suitable DMA available\n");
1021b843c749SSergey Zigachev 	}
1022b843c749SSergey Zigachev 	r = pci_set_consistent_dma_mask(adev->pdev, DMA_BIT_MASK(dma_bits));
1023b843c749SSergey Zigachev 	if (r) {
1024b843c749SSergey Zigachev 		pci_set_consistent_dma_mask(adev->pdev, DMA_BIT_MASK(32));
1025b843c749SSergey Zigachev 		pr_warn("amdgpu: No coherent DMA available\n");
1026b843c749SSergey Zigachev 	}
102778973132SSergey Zigachev #if 0
1028b843c749SSergey Zigachev 	adev->need_swiotlb = drm_get_max_iomem() > ((u64)1 << dma_bits);
102978973132SSergey Zigachev #endif
103078973132SSergey Zigachev 	/* XXX DragonFly: FreeBSD implementation returns false
103178973132SSergey Zigachev 	 * drm-kmod excerpt:
103278973132SSergey Zigachev 	 * Only used in combination with CONFIG_SWIOTLB in v4.17
103378973132SSergey Zigachev 	 * BSDFIXME: Let's say we can dma all physical memory...
103478973132SSergey Zigachev 	 */
103578973132SSergey Zigachev 	adev->need_swiotlb = false;
1036b843c749SSergey Zigachev 
1037b843c749SSergey Zigachev 	r = gmc_v7_0_init_microcode(adev);
1038b843c749SSergey Zigachev 	if (r) {
1039b843c749SSergey Zigachev 		DRM_ERROR("Failed to load mc firmware!\n");
1040b843c749SSergey Zigachev 		return r;
1041b843c749SSergey Zigachev 	}
1042b843c749SSergey Zigachev 
1043b843c749SSergey Zigachev 	r = gmc_v7_0_mc_init(adev);
1044b843c749SSergey Zigachev 	if (r)
1045b843c749SSergey Zigachev 		return r;
1046b843c749SSergey Zigachev 
1047b843c749SSergey Zigachev 	adev->gmc.stolen_size = gmc_v7_0_get_vbios_fb_size(adev);
1048b843c749SSergey Zigachev 
1049b843c749SSergey Zigachev 	/* Memory manager */
1050b843c749SSergey Zigachev 	r = amdgpu_bo_init(adev);
1051b843c749SSergey Zigachev 	if (r)
1052b843c749SSergey Zigachev 		return r;
1053b843c749SSergey Zigachev 
1054b843c749SSergey Zigachev 	r = gmc_v7_0_gart_init(adev);
1055b843c749SSergey Zigachev 	if (r)
1056b843c749SSergey Zigachev 		return r;
1057b843c749SSergey Zigachev 
1058b843c749SSergey Zigachev 	/*
1059b843c749SSergey Zigachev 	 * number of VMs
1060b843c749SSergey Zigachev 	 * VMID 0 is reserved for System
1061b843c749SSergey Zigachev 	 * amdgpu graphics/compute will use VMIDs 1-7
1062b843c749SSergey Zigachev 	 * amdkfd will use VMIDs 8-15
1063b843c749SSergey Zigachev 	 */
1064b843c749SSergey Zigachev 	adev->vm_manager.id_mgr[0].num_ids = AMDGPU_NUM_OF_VMIDS;
1065b843c749SSergey Zigachev 	amdgpu_vm_manager_init(adev);
1066b843c749SSergey Zigachev 
1067b843c749SSergey Zigachev 	/* base offset of vram pages */
1068b843c749SSergey Zigachev 	if (adev->flags & AMD_IS_APU) {
1069b843c749SSergey Zigachev 		u64 tmp = RREG32(mmMC_VM_FB_OFFSET);
1070b843c749SSergey Zigachev 
1071b843c749SSergey Zigachev 		tmp <<= 22;
1072b843c749SSergey Zigachev 		adev->vm_manager.vram_base_offset = tmp;
1073b843c749SSergey Zigachev 	} else {
1074b843c749SSergey Zigachev 		adev->vm_manager.vram_base_offset = 0;
1075b843c749SSergey Zigachev 	}
1076b843c749SSergey Zigachev 
1077b843c749SSergey Zigachev 	adev->gmc.vm_fault_info = kmalloc(sizeof(struct kfd_vm_fault_info),
107878973132SSergey Zigachev 					M_DRM,
1079b843c749SSergey Zigachev 					GFP_KERNEL);
1080b843c749SSergey Zigachev 	if (!adev->gmc.vm_fault_info)
1081b843c749SSergey Zigachev 		return -ENOMEM;
1082b843c749SSergey Zigachev 	atomic_set(&adev->gmc.vm_fault_info_updated, 0);
1083b843c749SSergey Zigachev 
1084b843c749SSergey Zigachev 	return 0;
1085b843c749SSergey Zigachev }
1086b843c749SSergey Zigachev 
gmc_v7_0_sw_fini(void * handle)1087b843c749SSergey Zigachev static int gmc_v7_0_sw_fini(void *handle)
1088b843c749SSergey Zigachev {
1089b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1090b843c749SSergey Zigachev 
1091b843c749SSergey Zigachev 	amdgpu_gem_force_release(adev);
1092b843c749SSergey Zigachev 	amdgpu_vm_manager_fini(adev);
1093b843c749SSergey Zigachev 	kfree(adev->gmc.vm_fault_info);
1094b843c749SSergey Zigachev 	amdgpu_gart_table_vram_free(adev);
1095b843c749SSergey Zigachev 	amdgpu_bo_fini(adev);
1096b843c749SSergey Zigachev 	amdgpu_gart_fini(adev);
1097b843c749SSergey Zigachev 	release_firmware(adev->gmc.fw);
1098b843c749SSergey Zigachev 	adev->gmc.fw = NULL;
1099b843c749SSergey Zigachev 
1100b843c749SSergey Zigachev 	return 0;
1101b843c749SSergey Zigachev }
1102b843c749SSergey Zigachev 
gmc_v7_0_hw_init(void * handle)1103b843c749SSergey Zigachev static int gmc_v7_0_hw_init(void *handle)
1104b843c749SSergey Zigachev {
1105b843c749SSergey Zigachev 	int r;
1106b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1107b843c749SSergey Zigachev 
1108b843c749SSergey Zigachev 	gmc_v7_0_init_golden_registers(adev);
1109b843c749SSergey Zigachev 
1110b843c749SSergey Zigachev 	gmc_v7_0_mc_program(adev);
1111b843c749SSergey Zigachev 
1112b843c749SSergey Zigachev 	if (!(adev->flags & AMD_IS_APU)) {
1113b843c749SSergey Zigachev 		r = gmc_v7_0_mc_load_microcode(adev);
1114b843c749SSergey Zigachev 		if (r) {
1115b843c749SSergey Zigachev 			DRM_ERROR("Failed to load MC firmware!\n");
1116b843c749SSergey Zigachev 			return r;
1117b843c749SSergey Zigachev 		}
1118b843c749SSergey Zigachev 	}
1119b843c749SSergey Zigachev 
1120b843c749SSergey Zigachev 	r = gmc_v7_0_gart_enable(adev);
1121b843c749SSergey Zigachev 	if (r)
1122b843c749SSergey Zigachev 		return r;
1123b843c749SSergey Zigachev 
1124b843c749SSergey Zigachev 	return r;
1125b843c749SSergey Zigachev }
1126b843c749SSergey Zigachev 
gmc_v7_0_hw_fini(void * handle)1127b843c749SSergey Zigachev static int gmc_v7_0_hw_fini(void *handle)
1128b843c749SSergey Zigachev {
1129b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1130b843c749SSergey Zigachev 
1131b843c749SSergey Zigachev 	amdgpu_irq_put(adev, &adev->gmc.vm_fault, 0);
1132b843c749SSergey Zigachev 	gmc_v7_0_gart_disable(adev);
1133b843c749SSergey Zigachev 
1134b843c749SSergey Zigachev 	return 0;
1135b843c749SSergey Zigachev }
1136b843c749SSergey Zigachev 
gmc_v7_0_suspend(void * handle)1137b843c749SSergey Zigachev static int gmc_v7_0_suspend(void *handle)
1138b843c749SSergey Zigachev {
1139b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1140b843c749SSergey Zigachev 
1141b843c749SSergey Zigachev 	gmc_v7_0_hw_fini(adev);
1142b843c749SSergey Zigachev 
1143b843c749SSergey Zigachev 	return 0;
1144b843c749SSergey Zigachev }
1145b843c749SSergey Zigachev 
gmc_v7_0_resume(void * handle)1146b843c749SSergey Zigachev static int gmc_v7_0_resume(void *handle)
1147b843c749SSergey Zigachev {
1148b843c749SSergey Zigachev 	int r;
1149b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1150b843c749SSergey Zigachev 
1151b843c749SSergey Zigachev 	r = gmc_v7_0_hw_init(adev);
1152b843c749SSergey Zigachev 	if (r)
1153b843c749SSergey Zigachev 		return r;
1154b843c749SSergey Zigachev 
1155b843c749SSergey Zigachev 	amdgpu_vmid_reset_all(adev);
1156b843c749SSergey Zigachev 
1157b843c749SSergey Zigachev 	return 0;
1158b843c749SSergey Zigachev }
1159b843c749SSergey Zigachev 
gmc_v7_0_is_idle(void * handle)1160b843c749SSergey Zigachev static bool gmc_v7_0_is_idle(void *handle)
1161b843c749SSergey Zigachev {
1162b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1163b843c749SSergey Zigachev 	u32 tmp = RREG32(mmSRBM_STATUS);
1164b843c749SSergey Zigachev 
1165b843c749SSergey Zigachev 	if (tmp & (SRBM_STATUS__MCB_BUSY_MASK | SRBM_STATUS__MCB_NON_DISPLAY_BUSY_MASK |
1166b843c749SSergey Zigachev 		   SRBM_STATUS__MCC_BUSY_MASK | SRBM_STATUS__MCD_BUSY_MASK | SRBM_STATUS__VMC_BUSY_MASK))
1167b843c749SSergey Zigachev 		return false;
1168b843c749SSergey Zigachev 
1169b843c749SSergey Zigachev 	return true;
1170b843c749SSergey Zigachev }
1171b843c749SSergey Zigachev 
gmc_v7_0_wait_for_idle(void * handle)1172b843c749SSergey Zigachev static int gmc_v7_0_wait_for_idle(void *handle)
1173b843c749SSergey Zigachev {
1174b843c749SSergey Zigachev 	unsigned i;
1175b843c749SSergey Zigachev 	u32 tmp;
1176b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1177b843c749SSergey Zigachev 
1178b843c749SSergey Zigachev 	for (i = 0; i < adev->usec_timeout; i++) {
1179b843c749SSergey Zigachev 		/* read MC_STATUS */
1180b843c749SSergey Zigachev 		tmp = RREG32(mmSRBM_STATUS) & (SRBM_STATUS__MCB_BUSY_MASK |
1181b843c749SSergey Zigachev 					       SRBM_STATUS__MCB_NON_DISPLAY_BUSY_MASK |
1182b843c749SSergey Zigachev 					       SRBM_STATUS__MCC_BUSY_MASK |
1183b843c749SSergey Zigachev 					       SRBM_STATUS__MCD_BUSY_MASK |
1184b843c749SSergey Zigachev 					       SRBM_STATUS__VMC_BUSY_MASK);
1185b843c749SSergey Zigachev 		if (!tmp)
1186b843c749SSergey Zigachev 			return 0;
1187b843c749SSergey Zigachev 		udelay(1);
1188b843c749SSergey Zigachev 	}
1189b843c749SSergey Zigachev 	return -ETIMEDOUT;
1190b843c749SSergey Zigachev 
1191b843c749SSergey Zigachev }
1192b843c749SSergey Zigachev 
gmc_v7_0_soft_reset(void * handle)1193b843c749SSergey Zigachev static int gmc_v7_0_soft_reset(void *handle)
1194b843c749SSergey Zigachev {
1195b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1196b843c749SSergey Zigachev 	u32 srbm_soft_reset = 0;
1197b843c749SSergey Zigachev 	u32 tmp = RREG32(mmSRBM_STATUS);
1198b843c749SSergey Zigachev 
1199b843c749SSergey Zigachev 	if (tmp & SRBM_STATUS__VMC_BUSY_MASK)
1200b843c749SSergey Zigachev 		srbm_soft_reset = REG_SET_FIELD(srbm_soft_reset,
1201b843c749SSergey Zigachev 						SRBM_SOFT_RESET, SOFT_RESET_VMC, 1);
1202b843c749SSergey Zigachev 
1203b843c749SSergey Zigachev 	if (tmp & (SRBM_STATUS__MCB_BUSY_MASK | SRBM_STATUS__MCB_NON_DISPLAY_BUSY_MASK |
1204b843c749SSergey Zigachev 		   SRBM_STATUS__MCC_BUSY_MASK | SRBM_STATUS__MCD_BUSY_MASK)) {
1205b843c749SSergey Zigachev 		if (!(adev->flags & AMD_IS_APU))
1206b843c749SSergey Zigachev 			srbm_soft_reset = REG_SET_FIELD(srbm_soft_reset,
1207b843c749SSergey Zigachev 							SRBM_SOFT_RESET, SOFT_RESET_MC, 1);
1208b843c749SSergey Zigachev 	}
1209b843c749SSergey Zigachev 
1210b843c749SSergey Zigachev 	if (srbm_soft_reset) {
1211b843c749SSergey Zigachev 		gmc_v7_0_mc_stop(adev);
1212b843c749SSergey Zigachev 		if (gmc_v7_0_wait_for_idle((void *)adev)) {
1213b843c749SSergey Zigachev 			dev_warn(adev->dev, "Wait for GMC idle timed out !\n");
1214b843c749SSergey Zigachev 		}
1215b843c749SSergey Zigachev 
1216b843c749SSergey Zigachev 
1217b843c749SSergey Zigachev 		tmp = RREG32(mmSRBM_SOFT_RESET);
1218b843c749SSergey Zigachev 		tmp |= srbm_soft_reset;
1219b843c749SSergey Zigachev 		dev_info(adev->dev, "SRBM_SOFT_RESET=0x%08X\n", tmp);
1220b843c749SSergey Zigachev 		WREG32(mmSRBM_SOFT_RESET, tmp);
1221b843c749SSergey Zigachev 		tmp = RREG32(mmSRBM_SOFT_RESET);
1222b843c749SSergey Zigachev 
1223b843c749SSergey Zigachev 		udelay(50);
1224b843c749SSergey Zigachev 
1225b843c749SSergey Zigachev 		tmp &= ~srbm_soft_reset;
1226b843c749SSergey Zigachev 		WREG32(mmSRBM_SOFT_RESET, tmp);
1227b843c749SSergey Zigachev 		tmp = RREG32(mmSRBM_SOFT_RESET);
1228b843c749SSergey Zigachev 
1229b843c749SSergey Zigachev 		/* Wait a little for things to settle down */
1230b843c749SSergey Zigachev 		udelay(50);
1231b843c749SSergey Zigachev 
1232b843c749SSergey Zigachev 		gmc_v7_0_mc_resume(adev);
1233b843c749SSergey Zigachev 		udelay(50);
1234b843c749SSergey Zigachev 	}
1235b843c749SSergey Zigachev 
1236b843c749SSergey Zigachev 	return 0;
1237b843c749SSergey Zigachev }
1238b843c749SSergey Zigachev 
gmc_v7_0_vm_fault_interrupt_state(struct amdgpu_device * adev,struct amdgpu_irq_src * src,unsigned type,enum amdgpu_interrupt_state state)1239b843c749SSergey Zigachev static int gmc_v7_0_vm_fault_interrupt_state(struct amdgpu_device *adev,
1240b843c749SSergey Zigachev 					     struct amdgpu_irq_src *src,
1241b843c749SSergey Zigachev 					     unsigned type,
1242b843c749SSergey Zigachev 					     enum amdgpu_interrupt_state state)
1243b843c749SSergey Zigachev {
1244b843c749SSergey Zigachev 	u32 tmp;
1245b843c749SSergey Zigachev 	u32 bits = (VM_CONTEXT1_CNTL__RANGE_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
1246b843c749SSergey Zigachev 		    VM_CONTEXT1_CNTL__DUMMY_PAGE_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
1247b843c749SSergey Zigachev 		    VM_CONTEXT1_CNTL__PDE0_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
1248b843c749SSergey Zigachev 		    VM_CONTEXT1_CNTL__VALID_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
1249b843c749SSergey Zigachev 		    VM_CONTEXT1_CNTL__READ_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
1250b843c749SSergey Zigachev 		    VM_CONTEXT1_CNTL__WRITE_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK);
1251b843c749SSergey Zigachev 
1252b843c749SSergey Zigachev 	switch (state) {
1253b843c749SSergey Zigachev 	case AMDGPU_IRQ_STATE_DISABLE:
1254b843c749SSergey Zigachev 		/* system context */
1255b843c749SSergey Zigachev 		tmp = RREG32(mmVM_CONTEXT0_CNTL);
1256b843c749SSergey Zigachev 		tmp &= ~bits;
1257b843c749SSergey Zigachev 		WREG32(mmVM_CONTEXT0_CNTL, tmp);
1258b843c749SSergey Zigachev 		/* VMs */
1259b843c749SSergey Zigachev 		tmp = RREG32(mmVM_CONTEXT1_CNTL);
1260b843c749SSergey Zigachev 		tmp &= ~bits;
1261b843c749SSergey Zigachev 		WREG32(mmVM_CONTEXT1_CNTL, tmp);
1262b843c749SSergey Zigachev 		break;
1263b843c749SSergey Zigachev 	case AMDGPU_IRQ_STATE_ENABLE:
1264b843c749SSergey Zigachev 		/* system context */
1265b843c749SSergey Zigachev 		tmp = RREG32(mmVM_CONTEXT0_CNTL);
1266b843c749SSergey Zigachev 		tmp |= bits;
1267b843c749SSergey Zigachev 		WREG32(mmVM_CONTEXT0_CNTL, tmp);
1268b843c749SSergey Zigachev 		/* VMs */
1269b843c749SSergey Zigachev 		tmp = RREG32(mmVM_CONTEXT1_CNTL);
1270b843c749SSergey Zigachev 		tmp |= bits;
1271b843c749SSergey Zigachev 		WREG32(mmVM_CONTEXT1_CNTL, tmp);
1272b843c749SSergey Zigachev 		break;
1273b843c749SSergey Zigachev 	default:
1274b843c749SSergey Zigachev 		break;
1275b843c749SSergey Zigachev 	}
1276b843c749SSergey Zigachev 
1277b843c749SSergey Zigachev 	return 0;
1278b843c749SSergey Zigachev }
1279b843c749SSergey Zigachev 
gmc_v7_0_process_interrupt(struct amdgpu_device * adev,struct amdgpu_irq_src * source,struct amdgpu_iv_entry * entry)1280b843c749SSergey Zigachev static int gmc_v7_0_process_interrupt(struct amdgpu_device *adev,
1281b843c749SSergey Zigachev 				      struct amdgpu_irq_src *source,
1282b843c749SSergey Zigachev 				      struct amdgpu_iv_entry *entry)
1283b843c749SSergey Zigachev {
1284b843c749SSergey Zigachev 	u32 addr, status, mc_client, vmid;
1285b843c749SSergey Zigachev 
1286b843c749SSergey Zigachev 	addr = RREG32(mmVM_CONTEXT1_PROTECTION_FAULT_ADDR);
1287b843c749SSergey Zigachev 	status = RREG32(mmVM_CONTEXT1_PROTECTION_FAULT_STATUS);
1288b843c749SSergey Zigachev 	mc_client = RREG32(mmVM_CONTEXT1_PROTECTION_FAULT_MCCLIENT);
1289b843c749SSergey Zigachev 	/* reset addr and status */
1290b843c749SSergey Zigachev 	WREG32_P(mmVM_CONTEXT1_CNTL2, 1, ~1);
1291b843c749SSergey Zigachev 
1292b843c749SSergey Zigachev 	if (!addr && !status)
1293b843c749SSergey Zigachev 		return 0;
1294b843c749SSergey Zigachev 
1295b843c749SSergey Zigachev 	if (amdgpu_vm_fault_stop == AMDGPU_VM_FAULT_STOP_FIRST)
1296b843c749SSergey Zigachev 		gmc_v7_0_set_fault_enable_default(adev, false);
1297b843c749SSergey Zigachev 
1298b843c749SSergey Zigachev 	if (printk_ratelimit()) {
1299b843c749SSergey Zigachev 		dev_err(adev->dev, "GPU fault detected: %d 0x%08x\n",
1300b843c749SSergey Zigachev 			entry->src_id, entry->src_data[0]);
1301b843c749SSergey Zigachev 		dev_err(adev->dev, "  VM_CONTEXT1_PROTECTION_FAULT_ADDR   0x%08X\n",
1302b843c749SSergey Zigachev 			addr);
1303b843c749SSergey Zigachev 		dev_err(adev->dev, "  VM_CONTEXT1_PROTECTION_FAULT_STATUS 0x%08X\n",
1304b843c749SSergey Zigachev 			status);
1305b843c749SSergey Zigachev 		gmc_v7_0_vm_decode_fault(adev, status, addr, mc_client,
1306b843c749SSergey Zigachev 					 entry->pasid);
1307b843c749SSergey Zigachev 	}
1308b843c749SSergey Zigachev 
1309b843c749SSergey Zigachev 	vmid = REG_GET_FIELD(status, VM_CONTEXT1_PROTECTION_FAULT_STATUS,
1310b843c749SSergey Zigachev 			     VMID);
1311b843c749SSergey Zigachev 	if (amdgpu_amdkfd_is_kfd_vmid(adev, vmid)
1312b843c749SSergey Zigachev 		&& !atomic_read(&adev->gmc.vm_fault_info_updated)) {
1313b843c749SSergey Zigachev 		struct kfd_vm_fault_info *info = adev->gmc.vm_fault_info;
1314b843c749SSergey Zigachev 		u32 protections = REG_GET_FIELD(status,
1315b843c749SSergey Zigachev 					VM_CONTEXT1_PROTECTION_FAULT_STATUS,
1316b843c749SSergey Zigachev 					PROTECTIONS);
1317b843c749SSergey Zigachev 
1318b843c749SSergey Zigachev 		info->vmid = vmid;
1319b843c749SSergey Zigachev 		info->mc_id = REG_GET_FIELD(status,
1320b843c749SSergey Zigachev 					    VM_CONTEXT1_PROTECTION_FAULT_STATUS,
1321b843c749SSergey Zigachev 					    MEMORY_CLIENT_ID);
1322b843c749SSergey Zigachev 		info->status = status;
1323b843c749SSergey Zigachev 		info->page_addr = addr;
1324b843c749SSergey Zigachev 		info->prot_valid = protections & 0x7 ? true : false;
1325b843c749SSergey Zigachev 		info->prot_read = protections & 0x8 ? true : false;
1326b843c749SSergey Zigachev 		info->prot_write = protections & 0x10 ? true : false;
1327b843c749SSergey Zigachev 		info->prot_exec = protections & 0x20 ? true : false;
1328b843c749SSergey Zigachev 		mb();
1329b843c749SSergey Zigachev 		atomic_set(&adev->gmc.vm_fault_info_updated, 1);
1330b843c749SSergey Zigachev 	}
1331b843c749SSergey Zigachev 
1332b843c749SSergey Zigachev 	return 0;
1333b843c749SSergey Zigachev }
1334b843c749SSergey Zigachev 
gmc_v7_0_set_clockgating_state(void * handle,enum amd_clockgating_state state)1335b843c749SSergey Zigachev static int gmc_v7_0_set_clockgating_state(void *handle,
1336b843c749SSergey Zigachev 					  enum amd_clockgating_state state)
1337b843c749SSergey Zigachev {
1338b843c749SSergey Zigachev 	bool gate = false;
1339b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1340b843c749SSergey Zigachev 
1341b843c749SSergey Zigachev 	if (state == AMD_CG_STATE_GATE)
1342b843c749SSergey Zigachev 		gate = true;
1343b843c749SSergey Zigachev 
1344b843c749SSergey Zigachev 	if (!(adev->flags & AMD_IS_APU)) {
1345b843c749SSergey Zigachev 		gmc_v7_0_enable_mc_mgcg(adev, gate);
1346b843c749SSergey Zigachev 		gmc_v7_0_enable_mc_ls(adev, gate);
1347b843c749SSergey Zigachev 	}
1348b843c749SSergey Zigachev 	gmc_v7_0_enable_bif_mgls(adev, gate);
1349b843c749SSergey Zigachev 	gmc_v7_0_enable_hdp_mgcg(adev, gate);
1350b843c749SSergey Zigachev 	gmc_v7_0_enable_hdp_ls(adev, gate);
1351b843c749SSergey Zigachev 
1352b843c749SSergey Zigachev 	return 0;
1353b843c749SSergey Zigachev }
1354b843c749SSergey Zigachev 
gmc_v7_0_set_powergating_state(void * handle,enum amd_powergating_state state)1355b843c749SSergey Zigachev static int gmc_v7_0_set_powergating_state(void *handle,
1356b843c749SSergey Zigachev 					  enum amd_powergating_state state)
1357b843c749SSergey Zigachev {
1358b843c749SSergey Zigachev 	return 0;
1359b843c749SSergey Zigachev }
1360b843c749SSergey Zigachev 
1361b843c749SSergey Zigachev static const struct amd_ip_funcs gmc_v7_0_ip_funcs = {
1362b843c749SSergey Zigachev 	.name = "gmc_v7_0",
1363b843c749SSergey Zigachev 	.early_init = gmc_v7_0_early_init,
1364b843c749SSergey Zigachev 	.late_init = gmc_v7_0_late_init,
1365b843c749SSergey Zigachev 	.sw_init = gmc_v7_0_sw_init,
1366b843c749SSergey Zigachev 	.sw_fini = gmc_v7_0_sw_fini,
1367b843c749SSergey Zigachev 	.hw_init = gmc_v7_0_hw_init,
1368b843c749SSergey Zigachev 	.hw_fini = gmc_v7_0_hw_fini,
1369b843c749SSergey Zigachev 	.suspend = gmc_v7_0_suspend,
1370b843c749SSergey Zigachev 	.resume = gmc_v7_0_resume,
1371b843c749SSergey Zigachev 	.is_idle = gmc_v7_0_is_idle,
1372b843c749SSergey Zigachev 	.wait_for_idle = gmc_v7_0_wait_for_idle,
1373b843c749SSergey Zigachev 	.soft_reset = gmc_v7_0_soft_reset,
1374b843c749SSergey Zigachev 	.set_clockgating_state = gmc_v7_0_set_clockgating_state,
1375b843c749SSergey Zigachev 	.set_powergating_state = gmc_v7_0_set_powergating_state,
1376b843c749SSergey Zigachev };
1377b843c749SSergey Zigachev 
1378b843c749SSergey Zigachev static const struct amdgpu_gmc_funcs gmc_v7_0_gmc_funcs = {
1379b843c749SSergey Zigachev 	.flush_gpu_tlb = gmc_v7_0_flush_gpu_tlb,
1380b843c749SSergey Zigachev 	.emit_flush_gpu_tlb = gmc_v7_0_emit_flush_gpu_tlb,
1381b843c749SSergey Zigachev 	.emit_pasid_mapping = gmc_v7_0_emit_pasid_mapping,
1382b843c749SSergey Zigachev 	.set_pte_pde = gmc_v7_0_set_pte_pde,
1383b843c749SSergey Zigachev 	.set_prt = gmc_v7_0_set_prt,
1384b843c749SSergey Zigachev 	.get_vm_pte_flags = gmc_v7_0_get_vm_pte_flags,
1385b843c749SSergey Zigachev 	.get_vm_pde = gmc_v7_0_get_vm_pde
1386b843c749SSergey Zigachev };
1387b843c749SSergey Zigachev 
1388b843c749SSergey Zigachev static const struct amdgpu_irq_src_funcs gmc_v7_0_irq_funcs = {
1389b843c749SSergey Zigachev 	.set = gmc_v7_0_vm_fault_interrupt_state,
1390b843c749SSergey Zigachev 	.process = gmc_v7_0_process_interrupt,
1391b843c749SSergey Zigachev };
1392b843c749SSergey Zigachev 
gmc_v7_0_set_gmc_funcs(struct amdgpu_device * adev)1393b843c749SSergey Zigachev static void gmc_v7_0_set_gmc_funcs(struct amdgpu_device *adev)
1394b843c749SSergey Zigachev {
1395b843c749SSergey Zigachev 	if (adev->gmc.gmc_funcs == NULL)
1396b843c749SSergey Zigachev 		adev->gmc.gmc_funcs = &gmc_v7_0_gmc_funcs;
1397b843c749SSergey Zigachev }
1398b843c749SSergey Zigachev 
gmc_v7_0_set_irq_funcs(struct amdgpu_device * adev)1399b843c749SSergey Zigachev static void gmc_v7_0_set_irq_funcs(struct amdgpu_device *adev)
1400b843c749SSergey Zigachev {
1401b843c749SSergey Zigachev 	adev->gmc.vm_fault.num_types = 1;
1402b843c749SSergey Zigachev 	adev->gmc.vm_fault.funcs = &gmc_v7_0_irq_funcs;
1403b843c749SSergey Zigachev }
1404b843c749SSergey Zigachev 
1405b843c749SSergey Zigachev const struct amdgpu_ip_block_version gmc_v7_0_ip_block =
1406b843c749SSergey Zigachev {
1407b843c749SSergey Zigachev 	.type = AMD_IP_BLOCK_TYPE_GMC,
1408b843c749SSergey Zigachev 	.major = 7,
1409b843c749SSergey Zigachev 	.minor = 0,
1410b843c749SSergey Zigachev 	.rev = 0,
1411b843c749SSergey Zigachev 	.funcs = &gmc_v7_0_ip_funcs,
1412b843c749SSergey Zigachev };
1413b843c749SSergey Zigachev 
1414b843c749SSergey Zigachev const struct amdgpu_ip_block_version gmc_v7_4_ip_block =
1415b843c749SSergey Zigachev {
1416b843c749SSergey Zigachev 	.type = AMD_IP_BLOCK_TYPE_GMC,
1417b843c749SSergey Zigachev 	.major = 7,
1418b843c749SSergey Zigachev 	.minor = 4,
1419b843c749SSergey Zigachev 	.rev = 0,
1420b843c749SSergey Zigachev 	.funcs = &gmc_v7_0_ip_funcs,
1421b843c749SSergey Zigachev };
1422