xref: /linux/drivers/gpu/drm/amd/amdgpu/ih_v6_0.c (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1 /*
2  * Copyright 2021 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  */
23 
24 #include <linux/pci.h>
25 
26 #include "amdgpu.h"
27 #include "amdgpu_ih.h"
28 
29 #include "oss/osssys_6_0_0_offset.h"
30 #include "oss/osssys_6_0_0_sh_mask.h"
31 
32 #include "soc15_common.h"
33 #include "ih_v6_0.h"
34 
35 #define MAX_REARM_RETRY 10
36 
37 static void ih_v6_0_set_interrupt_funcs(struct amdgpu_device *adev);
38 
39 /**
40  * ih_v6_0_init_register_offset - Initialize register offset for ih rings
41  *
42  * @adev: amdgpu_device pointer
43  *
44  * Initialize register offset ih rings (IH_V6_0).
45  */
46 static void ih_v6_0_init_register_offset(struct amdgpu_device *adev)
47 {
48 	struct amdgpu_ih_regs *ih_regs;
49 
50 	/* ih ring 2 is removed
51 	 * ih ring and ih ring 1 are available */
52 	if (adev->irq.ih.ring_size) {
53 		ih_regs = &adev->irq.ih.ih_regs;
54 		ih_regs->ih_rb_base = SOC15_REG_OFFSET(OSSSYS, 0, regIH_RB_BASE);
55 		ih_regs->ih_rb_base_hi = SOC15_REG_OFFSET(OSSSYS, 0, regIH_RB_BASE_HI);
56 		ih_regs->ih_rb_cntl = SOC15_REG_OFFSET(OSSSYS, 0, regIH_RB_CNTL);
57 		ih_regs->ih_rb_wptr = SOC15_REG_OFFSET(OSSSYS, 0, regIH_RB_WPTR);
58 		ih_regs->ih_rb_rptr = SOC15_REG_OFFSET(OSSSYS, 0, regIH_RB_RPTR);
59 		ih_regs->ih_doorbell_rptr = SOC15_REG_OFFSET(OSSSYS, 0, regIH_DOORBELL_RPTR);
60 		ih_regs->ih_rb_wptr_addr_lo = SOC15_REG_OFFSET(OSSSYS, 0, regIH_RB_WPTR_ADDR_LO);
61 		ih_regs->ih_rb_wptr_addr_hi = SOC15_REG_OFFSET(OSSSYS, 0, regIH_RB_WPTR_ADDR_HI);
62 		ih_regs->psp_reg_id = PSP_REG_IH_RB_CNTL;
63 	}
64 
65 	if (adev->irq.ih1.ring_size) {
66 		ih_regs = &adev->irq.ih1.ih_regs;
67 		ih_regs->ih_rb_base = SOC15_REG_OFFSET(OSSSYS, 0, regIH_RB_BASE_RING1);
68 		ih_regs->ih_rb_base_hi = SOC15_REG_OFFSET(OSSSYS, 0, regIH_RB_BASE_HI_RING1);
69 		ih_regs->ih_rb_cntl = SOC15_REG_OFFSET(OSSSYS, 0, regIH_RB_CNTL_RING1);
70 		ih_regs->ih_rb_wptr = SOC15_REG_OFFSET(OSSSYS, 0, regIH_RB_WPTR_RING1);
71 		ih_regs->ih_rb_rptr = SOC15_REG_OFFSET(OSSSYS, 0, regIH_RB_RPTR_RING1);
72 		ih_regs->ih_doorbell_rptr = SOC15_REG_OFFSET(OSSSYS, 0, regIH_DOORBELL_RPTR_RING1);
73 		ih_regs->psp_reg_id = PSP_REG_IH_RB_CNTL_RING1;
74 	}
75 }
76 
77 /**
78  * force_update_wptr_for_self_int - Force update the wptr for self interrupt
79  *
80  * @adev: amdgpu_device pointer
81  * @threshold: threshold to trigger the wptr reporting
82  * @timeout: timeout to trigger the wptr reporting
83  * @enabled: Enable/disable timeout flush mechanism
84  *
85  * threshold input range: 0 ~ 15, default 0,
86  * real_threshold = 2^threshold
87  * timeout input range: 0 ~ 20, default 8,
88  * real_timeout = (2^timeout) * 1024 / (socclk_freq)
89  *
90  * Force update wptr for self interrupt ( >= SIENNA_CICHLID).
91  */
92 static void
93 force_update_wptr_for_self_int(struct amdgpu_device *adev,
94 			       u32 threshold, u32 timeout, bool enabled)
95 {
96 	u32 ih_cntl, ih_rb_cntl;
97 
98 	ih_cntl = RREG32_SOC15(OSSSYS, 0, regIH_CNTL2);
99 	ih_rb_cntl = RREG32_SOC15(OSSSYS, 0, regIH_RB_CNTL_RING1);
100 
101 	ih_cntl = REG_SET_FIELD(ih_cntl, IH_CNTL2,
102 				SELF_IV_FORCE_WPTR_UPDATE_TIMEOUT, timeout);
103 	ih_cntl = REG_SET_FIELD(ih_cntl, IH_CNTL2,
104 				SELF_IV_FORCE_WPTR_UPDATE_ENABLE, enabled);
105 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL_RING1,
106 				   RB_USED_INT_THRESHOLD, threshold);
107 
108 	if (amdgpu_sriov_vf(adev) && amdgpu_sriov_reg_indirect_ih(adev)) {
109 		if (psp_reg_program(&adev->psp, PSP_REG_IH_RB_CNTL_RING1, ih_rb_cntl))
110 			return;
111 	} else {
112 		WREG32_SOC15(OSSSYS, 0, regIH_RB_CNTL_RING1, ih_rb_cntl);
113 	}
114 
115 	WREG32_SOC15(OSSSYS, 0, regIH_CNTL2, ih_cntl);
116 }
117 
118 /**
119  * ih_v6_0_toggle_ring_interrupts - toggle the interrupt ring buffer
120  *
121  * @adev: amdgpu_device pointer
122  * @ih: amdgpu_ih_ring pointer
123  * @enable: true - enable the interrupts, false - disable the interrupts
124  *
125  * Toggle the interrupt ring buffer (IH_V6_0)
126  */
127 static int ih_v6_0_toggle_ring_interrupts(struct amdgpu_device *adev,
128 					  struct amdgpu_ih_ring *ih,
129 					  bool enable)
130 {
131 	struct amdgpu_ih_regs *ih_regs;
132 	uint32_t tmp;
133 
134 	ih_regs = &ih->ih_regs;
135 
136 	tmp = RREG32(ih_regs->ih_rb_cntl);
137 	tmp = REG_SET_FIELD(tmp, IH_RB_CNTL, RB_ENABLE, (enable ? 1 : 0));
138 
139 	if (enable) {
140 		/* Unset the CLEAR_OVERFLOW bit to make sure the next step
141 		 * is switching the bit from 0 to 1
142 		 */
143 		tmp = REG_SET_FIELD(tmp, IH_RB_CNTL, WPTR_OVERFLOW_CLEAR, 0);
144 		if (amdgpu_sriov_vf(adev) && amdgpu_sriov_reg_indirect_ih(adev)) {
145 			if (psp_reg_program(&adev->psp, ih_regs->psp_reg_id, tmp))
146 				return -ETIMEDOUT;
147 		} else {
148 			WREG32_NO_KIQ(ih_regs->ih_rb_cntl, tmp);
149 		}
150 
151 		/* Clear RB_OVERFLOW bit */
152 		tmp = REG_SET_FIELD(tmp, IH_RB_CNTL, WPTR_OVERFLOW_CLEAR, 1);
153 		if (amdgpu_sriov_vf(adev) && amdgpu_sriov_reg_indirect_ih(adev)) {
154 			if (psp_reg_program(&adev->psp, ih_regs->psp_reg_id, tmp))
155 				return -ETIMEDOUT;
156 		} else {
157 			WREG32_NO_KIQ(ih_regs->ih_rb_cntl, tmp);
158 		}
159 
160 		/* Unset the CLEAR_OVERFLOW bit immediately so new overflows
161 		 * can be detected.
162 		 */
163 		tmp = REG_SET_FIELD(tmp, IH_RB_CNTL, WPTR_OVERFLOW_CLEAR, 0);
164 	}
165 
166 	/* enable_intr field is only valid in ring0 */
167 	if (ih == &adev->irq.ih)
168 		tmp = REG_SET_FIELD(tmp, IH_RB_CNTL, ENABLE_INTR, (enable ? 1 : 0));
169 
170 	if (amdgpu_sriov_vf(adev) && amdgpu_sriov_reg_indirect_ih(adev)) {
171 		/*
172 		 * PSP programming of IH_RB_CNTL is slow enough for MSI to
173 		 * arrive before this call returns. Publish ih->enabled first
174 		 * so amdgpu_ih_process() consumes that IV and rearms.
175 		 */
176 		if (enable) {
177 			ih->enabled = true;
178 			smp_wmb();
179 		}
180 		if (psp_reg_program(&adev->psp, ih_regs->psp_reg_id, tmp)) {
181 			ih->enabled = false;
182 			return -ETIMEDOUT;
183 		}
184 	} else {
185 		WREG32(ih_regs->ih_rb_cntl, tmp);
186 	}
187 
188 	if (enable) {
189 		ih->enabled = true;
190 	} else {
191 		/* set rptr, wptr to 0 */
192 		WREG32(ih_regs->ih_rb_rptr, 0);
193 		WREG32(ih_regs->ih_rb_wptr, 0);
194 		ih->enabled = false;
195 		ih->rptr = 0;
196 	}
197 
198 	return 0;
199 }
200 
201 /**
202  * ih_v6_0_toggle_interrupts - Toggle all the available interrupt ring buffers
203  *
204  * @adev: amdgpu_device pointer
205  * @enable: enable or disable interrupt ring buffers
206  *
207  * Toggle all the available interrupt ring buffers (IH_V6_0).
208  */
209 static int ih_v6_0_toggle_interrupts(struct amdgpu_device *adev, bool enable)
210 {
211 	struct amdgpu_ih_ring *ih[] = {&adev->irq.ih, &adev->irq.ih1};
212 	int i;
213 	int r;
214 
215 	for (i = 0; i < ARRAY_SIZE(ih); i++) {
216 		if (ih[i]->ring_size) {
217 			r = ih_v6_0_toggle_ring_interrupts(adev, ih[i], enable);
218 			if (r)
219 				return r;
220 		}
221 	}
222 
223 	return 0;
224 }
225 
226 static uint32_t ih_v6_0_rb_cntl(struct amdgpu_ih_ring *ih, uint32_t ih_rb_cntl)
227 {
228 	int rb_bufsz = order_base_2(ih->ring_size / 4);
229 
230 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL,
231 				   MC_SPACE, ih->use_bus_addr ? 2 : 4);
232 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL,
233 				   WPTR_OVERFLOW_CLEAR, 1);
234 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL,
235 				   WPTR_OVERFLOW_ENABLE, 1);
236 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, RB_SIZE, rb_bufsz);
237 	/* Ring Buffer write pointer writeback. If enabled, IH_RB_WPTR register
238 	 * value is written to memory
239 	 */
240 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL,
241 				   WPTR_WRITEBACK_ENABLE, 1);
242 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, MC_SNOOP, 1);
243 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, MC_RO, 0);
244 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, MC_VMID, 0);
245 
246 	return ih_rb_cntl;
247 }
248 
249 static uint32_t ih_v6_0_doorbell_rptr(struct amdgpu_ih_ring *ih)
250 {
251 	u32 ih_doorbell_rtpr = 0;
252 
253 	if (ih->use_doorbell) {
254 		ih_doorbell_rtpr = REG_SET_FIELD(ih_doorbell_rtpr,
255 						 IH_DOORBELL_RPTR, OFFSET,
256 						 ih->doorbell_index);
257 		ih_doorbell_rtpr = REG_SET_FIELD(ih_doorbell_rtpr,
258 						 IH_DOORBELL_RPTR,
259 						 ENABLE, 1);
260 	} else {
261 		ih_doorbell_rtpr = REG_SET_FIELD(ih_doorbell_rtpr,
262 						 IH_DOORBELL_RPTR,
263 						 ENABLE, 0);
264 	}
265 	return ih_doorbell_rtpr;
266 }
267 
268 /**
269  * ih_v6_0_enable_ring - enable an ih ring buffer
270  *
271  * @adev: amdgpu_device pointer
272  * @ih: amdgpu_ih_ring pointer
273  *
274  * Enable an ih ring buffer (IH_V6_0)
275  */
276 static int ih_v6_0_enable_ring(struct amdgpu_device *adev,
277 				      struct amdgpu_ih_ring *ih)
278 {
279 	struct amdgpu_ih_regs *ih_regs;
280 	uint32_t tmp;
281 
282 	ih_regs = &ih->ih_regs;
283 
284 	/* Ring Buffer base. [39:8] of 40-bit address of the beginning of the ring buffer*/
285 	WREG32(ih_regs->ih_rb_base, ih->gpu_addr >> 8);
286 	WREG32(ih_regs->ih_rb_base_hi, (ih->gpu_addr >> 40) & 0xff);
287 
288 	tmp = RREG32(ih_regs->ih_rb_cntl);
289 	tmp = ih_v6_0_rb_cntl(ih, tmp);
290 	if (ih == &adev->irq.ih)
291 		tmp = REG_SET_FIELD(tmp, IH_RB_CNTL, RPTR_REARM, !!adev->irq.msi_enabled);
292 	if (ih == &adev->irq.ih1) {
293 		tmp = REG_SET_FIELD(tmp, IH_RB_CNTL, WPTR_OVERFLOW_ENABLE, 0);
294 		tmp = REG_SET_FIELD(tmp, IH_RB_CNTL, RB_FULL_DRAIN_ENABLE, 1);
295 	}
296 
297 	if (amdgpu_sriov_vf(adev) && amdgpu_sriov_reg_indirect_ih(adev)) {
298 		if (psp_reg_program(&adev->psp, ih_regs->psp_reg_id, tmp)) {
299 			DRM_ERROR("PSP program IH_RB_CNTL failed!\n");
300 			return -ETIMEDOUT;
301 		}
302 	} else {
303 		WREG32(ih_regs->ih_rb_cntl, tmp);
304 	}
305 
306 	if (ih == &adev->irq.ih) {
307 		/* set the ih ring 0 writeback address whether it's enabled or not */
308 		WREG32(ih_regs->ih_rb_wptr_addr_lo, lower_32_bits(ih->wptr_addr));
309 		WREG32(ih_regs->ih_rb_wptr_addr_hi, upper_32_bits(ih->wptr_addr) & 0xFFFF);
310 	}
311 
312 	/* set rptr, wptr to 0 */
313 	WREG32(ih_regs->ih_rb_wptr, 0);
314 	WREG32(ih_regs->ih_rb_rptr, 0);
315 
316 	WREG32(ih_regs->ih_doorbell_rptr, ih_v6_0_doorbell_rptr(ih));
317 
318 	return 0;
319 }
320 
321 static uint32_t ih_v6_0_setup_retry_doorbell(u32 doorbell_index)
322 {
323 	u32 val = 0;
324 
325 	val = REG_SET_FIELD(val, IH_DOORBELL_RPTR, OFFSET, doorbell_index);
326 	val = REG_SET_FIELD(val, IH_DOORBELL_RPTR, ENABLE, 1);
327 
328 	return val;
329 }
330 
331 /**
332  * ih_v6_0_irq_init - init and enable the interrupt ring
333  *
334  * @adev: amdgpu_device pointer
335  *
336  * Allocate a ring buffer for the interrupt controller,
337  * enable the RLC, disable interrupts, enable the IH
338  * ring buffer and enable it.
339  * Called at device load and reume.
340  * Returns 0 for success, errors for failure.
341  */
342 static int ih_v6_0_irq_init(struct amdgpu_device *adev)
343 {
344 	struct amdgpu_ih_ring *ih[] = {&adev->irq.ih, &adev->irq.ih1};
345 	u32 ih_chicken;
346 	u32 tmp;
347 	int ret;
348 	int i;
349 
350 	/* disable irqs */
351 	ret = ih_v6_0_toggle_interrupts(adev, false);
352 	if (ret)
353 		return ret;
354 
355 	adev->nbio.funcs->ih_control(adev);
356 
357 	if (unlikely((adev->firmware.load_type == AMDGPU_FW_LOAD_DIRECT) ||
358 		     (adev->firmware.load_type == AMDGPU_FW_LOAD_RLC_BACKDOOR_AUTO))) {
359 		if (ih[0]->use_bus_addr) {
360 			ih_chicken = RREG32_SOC15(OSSSYS, 0, regIH_CHICKEN);
361 			ih_chicken = REG_SET_FIELD(ih_chicken,
362 					IH_CHICKEN, MC_SPACE_GPA_ENABLE, 1);
363 			WREG32_SOC15(OSSSYS, 0, regIH_CHICKEN, ih_chicken);
364 		}
365 	}
366 
367 	for (i = 0; i < ARRAY_SIZE(ih); i++) {
368 		if (ih[i]->ring_size) {
369 			ret = ih_v6_0_enable_ring(adev, ih[i]);
370 			if (ret)
371 				return ret;
372 		}
373 		ih[i]->overflow = false;
374 	}
375 
376 	/* update doorbell range for ih ring 0 */
377 	adev->nbio.funcs->ih_doorbell_range(adev, ih[0]->use_doorbell,
378 					    ih[0]->doorbell_index);
379 
380 	tmp = RREG32_SOC15(OSSSYS, 0, regIH_STORM_CLIENT_LIST_CNTL);
381 	tmp = REG_SET_FIELD(tmp, IH_STORM_CLIENT_LIST_CNTL,
382 			    CLIENT18_IS_STORM_CLIENT, 1);
383 	WREG32_SOC15(OSSSYS, 0, regIH_STORM_CLIENT_LIST_CNTL, tmp);
384 
385 	tmp = RREG32_SOC15(OSSSYS, 0, regIH_INT_FLOOD_CNTL);
386 	tmp = REG_SET_FIELD(tmp, IH_INT_FLOOD_CNTL, FLOOD_CNTL_ENABLE, 1);
387 	WREG32_SOC15(OSSSYS, 0, regIH_INT_FLOOD_CNTL, tmp);
388 
389 	/* GC/MMHUB UTCL2 page fault interrupts are configured as
390 	 * MSI storm capable interrupts by deafult. The delay is
391 	 * used to avoid ISR being called too frequently
392 	 * when page fault happens on several continuous page
393 	 * and thus avoid MSI storm */
394 	tmp = RREG32_SOC15(OSSSYS, 0, regIH_MSI_STORM_CTRL);
395 	tmp = REG_SET_FIELD(tmp, IH_MSI_STORM_CTRL,
396 			    DELAY, 3);
397 	WREG32_SOC15(OSSSYS, 0, regIH_MSI_STORM_CTRL, tmp);
398 
399 	/* Redirect the interrupts to IH RB1 for dGPU */
400 	if (adev->irq.ih1.ring_size) {
401 		tmp = RREG32_SOC15(OSSSYS, 0, regIH_RING1_CLIENT_CFG_INDEX);
402 		tmp = REG_SET_FIELD(tmp, IH_RING1_CLIENT_CFG_INDEX, INDEX, 0);
403 		WREG32_SOC15(OSSSYS, 0, regIH_RING1_CLIENT_CFG_INDEX, tmp);
404 
405 		tmp = RREG32_SOC15(OSSSYS, 0, regIH_RING1_CLIENT_CFG_DATA);
406 		tmp = REG_SET_FIELD(tmp, IH_RING1_CLIENT_CFG_DATA, CLIENT_ID, 0xa);
407 		tmp = REG_SET_FIELD(tmp, IH_RING1_CLIENT_CFG_DATA, SOURCE_ID, 0x0);
408 		tmp = REG_SET_FIELD(tmp, IH_RING1_CLIENT_CFG_DATA,
409 				    SOURCE_ID_MATCH_ENABLE, 0x1);
410 
411 		WREG32_SOC15(OSSSYS, 0, regIH_RING1_CLIENT_CFG_DATA, tmp);
412 	}
413 
414 	pci_set_master(adev->pdev);
415 
416 	if (!(adev->flags & AMD_IS_APU)) {
417 		/* Allocate the doorbell for IH Retry CAM */
418 		adev->irq.retry_cam_doorbell_index = (adev->doorbell_index.ih + 2) << 1;
419 		WREG32_SOC15(OSSSYS, 0, regIH_DOORBELL_RETRY_CAM,
420 			     ih_v6_0_setup_retry_doorbell(adev->irq.retry_cam_doorbell_index));
421 
422 		/* Enable IH Retry CAM */
423 		tmp = RREG32_SOC15(OSSSYS, 0, regIH_RETRY_INT_CAM_CNTL);
424 		tmp = REG_SET_FIELD(tmp, IH_RETRY_INT_CAM_CNTL, ENABLE, 1);
425 		tmp = REG_SET_FIELD(tmp, IH_RETRY_INT_CAM_CNTL, CAM_SIZE, 0xF);
426 		WREG32_SOC15(OSSSYS, 0, regIH_RETRY_INT_CAM_CNTL, tmp);
427 
428 		adev->irq.retry_cam_enabled = true;
429 	}
430 
431 	/* enable interrupts */
432 	ret = ih_v6_0_toggle_interrupts(adev, true);
433 	if (ret)
434 		return ret;
435 	/* enable wptr force update for self int */
436 	force_update_wptr_for_self_int(adev, 0, 8, true);
437 
438 	if (adev->irq.ih_soft.ring_size)
439 		adev->irq.ih_soft.enabled = true;
440 
441 	return 0;
442 }
443 
444 /**
445  * ih_v6_0_irq_disable - disable interrupts
446  *
447  * @adev: amdgpu_device pointer
448  *
449  * Disable interrupts on the hw.
450  */
451 static void ih_v6_0_irq_disable(struct amdgpu_device *adev)
452 {
453 	force_update_wptr_for_self_int(adev, 0, 8, false);
454 	ih_v6_0_toggle_interrupts(adev, false);
455 
456 	/* Wait and acknowledge irq */
457 	mdelay(1);
458 }
459 
460 /**
461  * ih_v6_0_get_wptr - get the IH ring buffer wptr
462  *
463  * @adev: amdgpu_device pointer
464  * @ih: amdgpu_ih_ring pointer
465  *
466  * Get the IH ring buffer wptr from either the register
467  * or the writeback memory buffer.  Also check for
468  * ring buffer overflow and deal with it.
469  * Returns the value of the wptr.
470  */
471 static u32 ih_v6_0_get_wptr(struct amdgpu_device *adev,
472 			      struct amdgpu_ih_ring *ih)
473 {
474 	u32 wptr, tmp;
475 	struct amdgpu_ih_regs *ih_regs;
476 
477 	wptr = le32_to_cpu(*ih->wptr_cpu);
478 
479 	if (ih == &adev->irq.ih_soft)
480 		goto out;
481 
482 	ih_regs = &ih->ih_regs;
483 
484 	if (!REG_GET_FIELD(wptr, IH_RB_WPTR, RB_OVERFLOW))
485 		goto out;
486 
487 	wptr = RREG32_NO_KIQ(ih_regs->ih_rb_wptr);
488 	if (!REG_GET_FIELD(wptr, IH_RB_WPTR, RB_OVERFLOW))
489 		goto out;
490 	if (!amdgpu_sriov_vf(adev))
491 		wptr = REG_SET_FIELD(wptr, IH_RB_WPTR, RB_OVERFLOW, 0);
492 	else
493 		ih->overflow = true;
494 
495 	/* When a ring buffer overflow happen start parsing interrupt
496 	 * from the last not overwritten vector (wptr + 32). Hopefully
497 	 * this should allow us to catch up.
498 	 */
499 	tmp = (wptr + 32) & ih->ptr_mask;
500 	dev_warn(adev->dev, "IH ring buffer overflow "
501 		 "(0x%08X, 0x%08X, 0x%08X)\n",
502 		 wptr, ih->rptr, tmp);
503 	ih->rptr = tmp;
504 
505 	tmp = RREG32_NO_KIQ(ih_regs->ih_rb_cntl);
506 	tmp = REG_SET_FIELD(tmp, IH_RB_CNTL, WPTR_OVERFLOW_CLEAR, 1);
507 	WREG32_NO_KIQ(ih_regs->ih_rb_cntl, tmp);
508 
509 	/* Unset the CLEAR_OVERFLOW bit immediately so new overflows
510 	 * can be detected.
511 	 */
512 	tmp = REG_SET_FIELD(tmp, IH_RB_CNTL, WPTR_OVERFLOW_CLEAR, 0);
513 	WREG32_NO_KIQ(ih_regs->ih_rb_cntl, tmp);
514 out:
515 	return (wptr & ih->ptr_mask);
516 }
517 
518 /**
519  * ih_v6_0_irq_rearm - rearm IRQ if lost
520  *
521  * @adev: amdgpu_device pointer
522  * @ih: amdgpu_ih_ring pointer
523  *
524  */
525 static void ih_v6_0_irq_rearm(struct amdgpu_device *adev,
526 			       struct amdgpu_ih_ring *ih)
527 {
528 	uint32_t v = 0;
529 	uint32_t i = 0;
530 	struct amdgpu_ih_regs *ih_regs;
531 
532 	ih_regs = &ih->ih_regs;
533 
534 	/* Rearm IRQ / re-write doorbell if doorbell write is lost */
535 	for (i = 0; i < MAX_REARM_RETRY; i++) {
536 		v = RREG32_NO_KIQ(ih_regs->ih_rb_rptr);
537 		if ((v < ih->ring_size) && (v != ih->rptr))
538 			WDOORBELL32(ih->doorbell_index, ih->rptr);
539 		else
540 			break;
541 	}
542 }
543 
544 /**
545  * ih_v6_0_set_rptr - set the IH ring buffer rptr
546  *
547  * @adev: amdgpu_device pointer
548  * @ih: amdgpu_ih_ring pointer
549  *
550  * Set the IH ring buffer rptr.
551  */
552 static void ih_v6_0_set_rptr(struct amdgpu_device *adev,
553 			       struct amdgpu_ih_ring *ih)
554 {
555 	struct amdgpu_ih_regs *ih_regs;
556 
557 	if (ih == &adev->irq.ih_soft)
558 		return;
559 
560 	if (ih->use_doorbell) {
561 		/* XXX check if swapping is necessary on BE */
562 		*ih->rptr_cpu = ih->rptr;
563 		WDOORBELL32(ih->doorbell_index, ih->rptr);
564 
565 		if (amdgpu_sriov_vf(adev))
566 			ih_v6_0_irq_rearm(adev, ih);
567 	} else {
568 		ih_regs = &ih->ih_regs;
569 		WREG32(ih_regs->ih_rb_rptr, ih->rptr);
570 	}
571 }
572 
573 /**
574  * ih_v6_0_self_irq - dispatch work for ring 1
575  *
576  * @adev: amdgpu_device pointer
577  * @source: irq source
578  * @entry: IV with WPTR update
579  *
580  * Update the WPTR from the IV and schedule work to handle the entries.
581  */
582 static int ih_v6_0_self_irq(struct amdgpu_device *adev,
583 			      struct amdgpu_irq_src *source,
584 			      struct amdgpu_iv_entry *entry)
585 {
586 	uint32_t wptr = cpu_to_le32(entry->src_data[0]);
587 
588 	switch (entry->ring_id) {
589 	case 1:
590 		*adev->irq.ih1.wptr_cpu = wptr;
591 		schedule_work(&adev->irq.ih1_work);
592 		break;
593 	default:
594 		break;
595 	}
596 	return 0;
597 }
598 
599 static const struct amdgpu_irq_src_funcs ih_v6_0_self_irq_funcs = {
600 	.process = ih_v6_0_self_irq,
601 };
602 
603 static void ih_v6_0_set_self_irq_funcs(struct amdgpu_device *adev)
604 {
605 	adev->irq.self_irq.num_types = 0;
606 	adev->irq.self_irq.funcs = &ih_v6_0_self_irq_funcs;
607 }
608 
609 static int ih_v6_0_early_init(struct amdgpu_ip_block *ip_block)
610 {
611 	struct amdgpu_device *adev = ip_block->adev;
612 
613 	ih_v6_0_set_interrupt_funcs(adev);
614 	ih_v6_0_set_self_irq_funcs(adev);
615 	return 0;
616 }
617 
618 static int ih_v6_0_sw_init(struct amdgpu_ip_block *ip_block)
619 {
620 	int r;
621 	struct amdgpu_device *adev = ip_block->adev;
622 	bool use_bus_addr;
623 
624 	r = amdgpu_irq_add_id(adev, SOC21_IH_CLIENTID_IH, 0,
625 			      &adev->irq.self_irq);
626 
627 	if (r)
628 		return r;
629 
630 	/* use gpu virtual address for ih ring
631 	 * until ih_checken is programmed to allow
632 	 * use bus address for ih ring by psp bl */
633 	use_bus_addr = adev->firmware.load_type != AMDGPU_FW_LOAD_PSP;
634 	r = amdgpu_ih_ring_init(adev, &adev->irq.ih, IH_RING_SIZE, use_bus_addr);
635 	if (r)
636 		return r;
637 
638 	adev->irq.ih.use_doorbell = true;
639 	adev->irq.ih.doorbell_index = adev->doorbell_index.ih << 1;
640 
641 	if (!(adev->flags & AMD_IS_APU)) {
642 		r = amdgpu_ih_ring_init(adev, &adev->irq.ih1, IH_RING_SIZE,
643 					use_bus_addr);
644 		if (r)
645 			return r;
646 
647 		adev->irq.ih1.use_doorbell = true;
648 		adev->irq.ih1.doorbell_index = (adev->doorbell_index.ih + 1) << 1;
649 	}
650 
651 	/* initialize ih control register offset */
652 	ih_v6_0_init_register_offset(adev);
653 
654 	r = amdgpu_ih_ring_init(adev, &adev->irq.ih_soft, IH_SW_RING_SIZE, true);
655 	if (r)
656 		return r;
657 
658 	r = amdgpu_irq_init(adev);
659 
660 	return r;
661 }
662 
663 static int ih_v6_0_sw_fini(struct amdgpu_ip_block *ip_block)
664 {
665 	struct amdgpu_device *adev = ip_block->adev;
666 
667 	amdgpu_irq_fini_sw(adev);
668 
669 	return 0;
670 }
671 
672 static int ih_v6_0_hw_init(struct amdgpu_ip_block *ip_block)
673 {
674 	int r;
675 	struct amdgpu_device *adev = ip_block->adev;
676 
677 	r = ih_v6_0_irq_init(adev);
678 	if (r)
679 		return r;
680 
681 	return 0;
682 }
683 
684 static int ih_v6_0_hw_fini(struct amdgpu_ip_block *ip_block)
685 {
686 	ih_v6_0_irq_disable(ip_block->adev);
687 
688 	return 0;
689 }
690 
691 static int ih_v6_0_suspend(struct amdgpu_ip_block *ip_block)
692 {
693 	return ih_v6_0_hw_fini(ip_block);
694 }
695 
696 static int ih_v6_0_resume(struct amdgpu_ip_block *ip_block)
697 {
698 	return ih_v6_0_hw_init(ip_block);
699 }
700 
701 static int ih_v6_0_wait_for_idle(struct amdgpu_ip_block *ip_block)
702 {
703 	/* todo */
704 	return -ETIMEDOUT;
705 }
706 
707 static void ih_v6_0_update_clockgating_state(struct amdgpu_device *adev,
708 					       bool enable)
709 {
710 	uint32_t data, def, field_val;
711 
712 	if (adev->cg_flags & AMD_CG_SUPPORT_IH_CG) {
713 		def = data = RREG32_SOC15(OSSSYS, 0, regIH_CLK_CTRL);
714 		field_val = enable ? 0 : 1;
715 		data = REG_SET_FIELD(data, IH_CLK_CTRL,
716 				     DBUS_MUX_CLK_SOFT_OVERRIDE, field_val);
717 		data = REG_SET_FIELD(data, IH_CLK_CTRL,
718 				     OSSSYS_SHARE_CLK_SOFT_OVERRIDE, field_val);
719 		data = REG_SET_FIELD(data, IH_CLK_CTRL,
720 				     LIMIT_SMN_CLK_SOFT_OVERRIDE, field_val);
721 		data = REG_SET_FIELD(data, IH_CLK_CTRL,
722 				     DYN_CLK_SOFT_OVERRIDE, field_val);
723 		data = REG_SET_FIELD(data, IH_CLK_CTRL,
724 				     REG_CLK_SOFT_OVERRIDE, field_val);
725 		if (def != data)
726 			WREG32_SOC15(OSSSYS, 0, regIH_CLK_CTRL, data);
727 	}
728 }
729 
730 static int ih_v6_0_set_clockgating_state(struct amdgpu_ip_block *ip_block,
731 					   enum amd_clockgating_state state)
732 {
733 	struct amdgpu_device *adev = ip_block->adev;
734 
735 	ih_v6_0_update_clockgating_state(adev,
736 				state == AMD_CG_STATE_GATE);
737 	return 0;
738 }
739 
740 static void ih_v6_0_update_ih_mem_power_gating(struct amdgpu_device *adev,
741 					       bool enable)
742 {
743 	uint32_t ih_mem_pwr_cntl;
744 
745 	/* Disable ih sram power cntl before switch powergating mode */
746 	ih_mem_pwr_cntl = RREG32_SOC15(OSSSYS, 0, regIH_MEM_POWER_CTRL);
747 	ih_mem_pwr_cntl = REG_SET_FIELD(ih_mem_pwr_cntl, IH_MEM_POWER_CTRL,
748 					IH_BUFFER_MEM_POWER_CTRL_EN, 0);
749 	WREG32_SOC15(OSSSYS, 0, regIH_MEM_POWER_CTRL, ih_mem_pwr_cntl);
750 
751 	/* It is recommended to set mem powergating mode to DS mode */
752 	if (enable) {
753 		/* mem power mode */
754 		ih_mem_pwr_cntl = REG_SET_FIELD(ih_mem_pwr_cntl, IH_MEM_POWER_CTRL,
755 						IH_BUFFER_MEM_POWER_LS_EN, 0);
756 		ih_mem_pwr_cntl = REG_SET_FIELD(ih_mem_pwr_cntl, IH_MEM_POWER_CTRL,
757 						IH_BUFFER_MEM_POWER_DS_EN, 1);
758 		ih_mem_pwr_cntl = REG_SET_FIELD(ih_mem_pwr_cntl, IH_MEM_POWER_CTRL,
759 						IH_BUFFER_MEM_POWER_SD_EN, 0);
760 		/* cam mem power mode */
761 		ih_mem_pwr_cntl = REG_SET_FIELD(ih_mem_pwr_cntl, IH_MEM_POWER_CTRL,
762 						IH_RETRY_INT_CAM_MEM_POWER_LS_EN, 0);
763 		ih_mem_pwr_cntl = REG_SET_FIELD(ih_mem_pwr_cntl, IH_MEM_POWER_CTRL,
764 						IH_RETRY_INT_CAM_MEM_POWER_DS_EN, 1);
765 		ih_mem_pwr_cntl = REG_SET_FIELD(ih_mem_pwr_cntl, IH_MEM_POWER_CTRL,
766 						IH_RETRY_INT_CAM_MEM_POWER_SD_EN, 0);
767 		/* re-enable power cntl */
768 		ih_mem_pwr_cntl = REG_SET_FIELD(ih_mem_pwr_cntl, IH_MEM_POWER_CTRL,
769 						IH_BUFFER_MEM_POWER_CTRL_EN, 1);
770 	} else {
771 		/* mem power mode */
772 		ih_mem_pwr_cntl = REG_SET_FIELD(ih_mem_pwr_cntl, IH_MEM_POWER_CTRL,
773 						IH_BUFFER_MEM_POWER_LS_EN, 0);
774 		ih_mem_pwr_cntl = REG_SET_FIELD(ih_mem_pwr_cntl, IH_MEM_POWER_CTRL,
775 						IH_BUFFER_MEM_POWER_DS_EN, 0);
776 		ih_mem_pwr_cntl = REG_SET_FIELD(ih_mem_pwr_cntl, IH_MEM_POWER_CTRL,
777 						IH_BUFFER_MEM_POWER_SD_EN, 0);
778 		/* cam mem power mode */
779 		ih_mem_pwr_cntl = REG_SET_FIELD(ih_mem_pwr_cntl, IH_MEM_POWER_CTRL,
780 						IH_RETRY_INT_CAM_MEM_POWER_LS_EN, 0);
781 		ih_mem_pwr_cntl = REG_SET_FIELD(ih_mem_pwr_cntl, IH_MEM_POWER_CTRL,
782 						IH_RETRY_INT_CAM_MEM_POWER_DS_EN, 0);
783 		ih_mem_pwr_cntl = REG_SET_FIELD(ih_mem_pwr_cntl, IH_MEM_POWER_CTRL,
784 						IH_RETRY_INT_CAM_MEM_POWER_SD_EN, 0);
785 		/* re-enable power cntl*/
786 		ih_mem_pwr_cntl = REG_SET_FIELD(ih_mem_pwr_cntl, IH_MEM_POWER_CTRL,
787 						IH_BUFFER_MEM_POWER_CTRL_EN, 1);
788 	}
789 
790 	WREG32_SOC15(OSSSYS, 0, regIH_MEM_POWER_CTRL, ih_mem_pwr_cntl);
791 }
792 
793 static int ih_v6_0_set_powergating_state(struct amdgpu_ip_block *ip_block,
794 					 enum amd_powergating_state state)
795 {
796 	struct amdgpu_device *adev = ip_block->adev;
797 	bool enable = (state == AMD_PG_STATE_GATE);
798 
799 	if (adev->pg_flags & AMD_PG_SUPPORT_IH_SRAM_PG)
800 		ih_v6_0_update_ih_mem_power_gating(adev, enable);
801 
802 	return 0;
803 }
804 
805 static void ih_v6_0_get_clockgating_state(struct amdgpu_ip_block *ip_block, u64 *flags)
806 {
807 	struct amdgpu_device *adev = ip_block->adev;
808 
809 	if (!RREG32_SOC15(OSSSYS, 0, regIH_CLK_CTRL))
810 		*flags |= AMD_CG_SUPPORT_IH_CG;
811 }
812 
813 static const struct amd_ip_funcs ih_v6_0_ip_funcs = {
814 	.name = "ih_v6_0",
815 	.early_init = ih_v6_0_early_init,
816 	.sw_init = ih_v6_0_sw_init,
817 	.sw_fini = ih_v6_0_sw_fini,
818 	.hw_init = ih_v6_0_hw_init,
819 	.hw_fini = ih_v6_0_hw_fini,
820 	.suspend = ih_v6_0_suspend,
821 	.resume = ih_v6_0_resume,
822 	.wait_for_idle = ih_v6_0_wait_for_idle,
823 	.set_clockgating_state = ih_v6_0_set_clockgating_state,
824 	.set_powergating_state = ih_v6_0_set_powergating_state,
825 	.get_clockgating_state = ih_v6_0_get_clockgating_state,
826 };
827 
828 static const struct amdgpu_ih_funcs ih_v6_0_funcs = {
829 	.get_wptr = ih_v6_0_get_wptr,
830 	.decode_iv = amdgpu_ih_decode_iv_helper,
831 	.decode_iv_ts = amdgpu_ih_decode_iv_ts_helper,
832 	.set_rptr = ih_v6_0_set_rptr
833 };
834 
835 static void ih_v6_0_set_interrupt_funcs(struct amdgpu_device *adev)
836 {
837 	adev->irq.ih_funcs = &ih_v6_0_funcs;
838 }
839 
840 const struct amdgpu_ip_block_version ih_v6_0_ip_block = {
841 	.type = AMD_IP_BLOCK_TYPE_IH,
842 	.major = 6,
843 	.minor = 0,
844 	.rev = 0,
845 	.funcs = &ih_v6_0_ip_funcs,
846 };
847