1 /*
2  * Copyright 2019 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_5_0_0_offset.h"
30 #include "oss/osssys_5_0_0_sh_mask.h"
31 
32 #include "soc15_common.h"
33 #include "navi10_ih.h"
34 
35 #define MAX_REARM_RETRY 10
36 
37 #define mmIH_CHICKEN_Sienna_Cichlid                 0x018d
38 #define mmIH_CHICKEN_Sienna_Cichlid_BASE_IDX        0
39 
40 static void navi10_ih_set_interrupt_funcs(struct amdgpu_device *adev);
41 
42 /**
43  * navi10_ih_init_register_offset - Initialize register offset for ih rings
44  *
45  * @adev: amdgpu_device pointer
46  *
47  * Initialize register offset ih rings (NAVI10).
48  */
49 static void navi10_ih_init_register_offset(struct amdgpu_device *adev)
50 {
51 	struct amdgpu_ih_regs *ih_regs;
52 
53 	if (adev->irq.ih.ring_size) {
54 		ih_regs = &adev->irq.ih.ih_regs;
55 		ih_regs->ih_rb_base = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_BASE);
56 		ih_regs->ih_rb_base_hi = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_BASE_HI);
57 		ih_regs->ih_rb_cntl = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_CNTL);
58 		ih_regs->ih_rb_wptr = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_WPTR);
59 		ih_regs->ih_rb_rptr = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_RPTR);
60 		ih_regs->ih_doorbell_rptr = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_DOORBELL_RPTR);
61 		ih_regs->ih_rb_wptr_addr_lo = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_WPTR_ADDR_LO);
62 		ih_regs->ih_rb_wptr_addr_hi = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_WPTR_ADDR_HI);
63 		ih_regs->psp_reg_id = PSP_REG_IH_RB_CNTL;
64 	}
65 
66 	if (adev->irq.ih1.ring_size) {
67 		ih_regs = &adev->irq.ih1.ih_regs;
68 		ih_regs->ih_rb_base = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_BASE_RING1);
69 		ih_regs->ih_rb_base_hi = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_BASE_HI_RING1);
70 		ih_regs->ih_rb_cntl = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_CNTL_RING1);
71 		ih_regs->ih_rb_wptr = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_WPTR_RING1);
72 		ih_regs->ih_rb_rptr = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_RPTR_RING1);
73 		ih_regs->ih_doorbell_rptr = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_DOORBELL_RPTR_RING1);
74 		ih_regs->psp_reg_id = PSP_REG_IH_RB_CNTL_RING1;
75 	}
76 
77 	if (adev->irq.ih2.ring_size) {
78 		ih_regs = &adev->irq.ih2.ih_regs;
79 		ih_regs->ih_rb_base = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_BASE_RING2);
80 		ih_regs->ih_rb_base_hi = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_BASE_HI_RING2);
81 		ih_regs->ih_rb_cntl = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_CNTL_RING2);
82 		ih_regs->ih_rb_wptr = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_WPTR_RING2);
83 		ih_regs->ih_rb_rptr = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_RPTR_RING2);
84 		ih_regs->ih_doorbell_rptr = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_DOORBELL_RPTR_RING2);
85 		ih_regs->psp_reg_id = PSP_REG_IH_RB_CNTL_RING2;
86 	}
87 }
88 
89 /**
90  * force_update_wptr_for_self_int - Force update the wptr for self interrupt
91  *
92  * @adev: amdgpu_device pointer
93  * @threshold: threshold to trigger the wptr reporting
94  * @timeout: timeout to trigger the wptr reporting
95  * @enabled: Enable/disable timeout flush mechanism
96  *
97  * threshold input range: 0 ~ 15, default 0,
98  * real_threshold = 2^threshold
99  * timeout input range: 0 ~ 20, default 8,
100  * real_timeout = (2^timeout) * 1024 / (socclk_freq)
101  *
102  * Force update wptr for self interrupt ( >= SIENNA_CICHLID).
103  */
104 static void
105 force_update_wptr_for_self_int(struct amdgpu_device *adev,
106 			       u32 threshold, u32 timeout, bool enabled)
107 {
108 	u32 ih_cntl, ih_rb_cntl;
109 
110 	if (adev->ip_versions[OSSSYS_HWIP][0] < IP_VERSION(5, 0, 3))
111 		return;
112 
113 	ih_cntl = RREG32_SOC15(OSSSYS, 0, mmIH_CNTL2);
114 	ih_rb_cntl = RREG32_SOC15(OSSSYS, 0, mmIH_RB_CNTL_RING1);
115 
116 	ih_cntl = REG_SET_FIELD(ih_cntl, IH_CNTL2,
117 				SELF_IV_FORCE_WPTR_UPDATE_TIMEOUT, timeout);
118 	ih_cntl = REG_SET_FIELD(ih_cntl, IH_CNTL2,
119 				SELF_IV_FORCE_WPTR_UPDATE_ENABLE, enabled);
120 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL_RING1,
121 				   RB_USED_INT_THRESHOLD, threshold);
122 
123 	if (amdgpu_sriov_vf(adev) && amdgpu_sriov_reg_indirect_ih(adev)) {
124 		if (psp_reg_program(&adev->psp, PSP_REG_IH_RB_CNTL_RING1, ih_rb_cntl))
125 			return;
126 	} else {
127 		WREG32_SOC15(OSSSYS, 0, mmIH_RB_CNTL_RING1, ih_rb_cntl);
128 	}
129 
130 	ih_rb_cntl = RREG32_SOC15(OSSSYS, 0, mmIH_RB_CNTL_RING2);
131 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL_RING2,
132 				   RB_USED_INT_THRESHOLD, threshold);
133 	if (amdgpu_sriov_vf(adev) && amdgpu_sriov_reg_indirect_ih(adev)) {
134 		if (psp_reg_program(&adev->psp, PSP_REG_IH_RB_CNTL_RING2, ih_rb_cntl))
135 			return;
136 	} else {
137 		WREG32_SOC15(OSSSYS, 0, mmIH_RB_CNTL_RING2, ih_rb_cntl);
138 	}
139 
140 	WREG32_SOC15(OSSSYS, 0, mmIH_CNTL2, ih_cntl);
141 }
142 
143 /**
144  * navi10_ih_toggle_ring_interrupts - toggle the interrupt ring buffer
145  *
146  * @adev: amdgpu_device pointer
147  * @ih: amdgpu_ih_ring pointet
148  * @enable: true - enable the interrupts, false - disable the interrupts
149  *
150  * Toggle the interrupt ring buffer (NAVI10)
151  */
152 static int navi10_ih_toggle_ring_interrupts(struct amdgpu_device *adev,
153 					    struct amdgpu_ih_ring *ih,
154 					    bool enable)
155 {
156 	struct amdgpu_ih_regs *ih_regs;
157 	uint32_t tmp;
158 
159 	ih_regs = &ih->ih_regs;
160 
161 	tmp = RREG32(ih_regs->ih_rb_cntl);
162 	tmp = REG_SET_FIELD(tmp, IH_RB_CNTL, RB_ENABLE, (enable ? 1 : 0));
163 	tmp = REG_SET_FIELD(tmp, IH_RB_CNTL, RB_GPU_TS_ENABLE, 1);
164 	/* enable_intr field is only valid in ring0 */
165 	if (ih == &adev->irq.ih)
166 		tmp = REG_SET_FIELD(tmp, IH_RB_CNTL, ENABLE_INTR, (enable ? 1 : 0));
167 
168 	if (amdgpu_sriov_vf(adev) && amdgpu_sriov_reg_indirect_ih(adev)) {
169 		if (psp_reg_program(&adev->psp, ih_regs->psp_reg_id, tmp))
170 			return -ETIMEDOUT;
171 	} else {
172 		WREG32(ih_regs->ih_rb_cntl, tmp);
173 	}
174 
175 	if (enable) {
176 		ih->enabled = true;
177 	} else {
178 		/* set rptr, wptr to 0 */
179 		WREG32(ih_regs->ih_rb_rptr, 0);
180 		WREG32(ih_regs->ih_rb_wptr, 0);
181 		ih->enabled = false;
182 		ih->rptr = 0;
183 	}
184 
185 	return 0;
186 }
187 
188 /**
189  * navi10_ih_toggle_interrupts - Toggle all the available interrupt ring buffers
190  *
191  * @adev: amdgpu_device pointer
192  * @enable: enable or disable interrupt ring buffers
193  *
194  * Toggle all the available interrupt ring buffers (NAVI10).
195  */
196 static int navi10_ih_toggle_interrupts(struct amdgpu_device *adev, bool enable)
197 {
198 	struct amdgpu_ih_ring *ih[] = {&adev->irq.ih, &adev->irq.ih1, &adev->irq.ih2};
199 	int i;
200 	int r;
201 
202 	for (i = 0; i < ARRAY_SIZE(ih); i++) {
203 		if (ih[i]->ring_size) {
204 			r = navi10_ih_toggle_ring_interrupts(adev, ih[i], enable);
205 			if (r)
206 				return r;
207 		}
208 	}
209 
210 	return 0;
211 }
212 
213 static uint32_t navi10_ih_rb_cntl(struct amdgpu_ih_ring *ih, uint32_t ih_rb_cntl)
214 {
215 	int rb_bufsz = order_base_2(ih->ring_size / 4);
216 
217 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL,
218 				   MC_SPACE, ih->use_bus_addr ? 1 : 4);
219 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL,
220 				   WPTR_OVERFLOW_CLEAR, 1);
221 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL,
222 				   WPTR_OVERFLOW_ENABLE, 1);
223 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, RB_SIZE, rb_bufsz);
224 	/* Ring Buffer write pointer writeback. If enabled, IH_RB_WPTR register
225 	 * value is written to memory
226 	 */
227 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL,
228 				   WPTR_WRITEBACK_ENABLE, 1);
229 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, MC_SNOOP, 1);
230 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, MC_RO, 0);
231 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, MC_VMID, 0);
232 
233 	return ih_rb_cntl;
234 }
235 
236 static uint32_t navi10_ih_doorbell_rptr(struct amdgpu_ih_ring *ih)
237 {
238 	u32 ih_doorbell_rtpr = 0;
239 
240 	if (ih->use_doorbell) {
241 		ih_doorbell_rtpr = REG_SET_FIELD(ih_doorbell_rtpr,
242 						 IH_DOORBELL_RPTR, OFFSET,
243 						 ih->doorbell_index);
244 		ih_doorbell_rtpr = REG_SET_FIELD(ih_doorbell_rtpr,
245 						 IH_DOORBELL_RPTR,
246 						 ENABLE, 1);
247 	} else {
248 		ih_doorbell_rtpr = REG_SET_FIELD(ih_doorbell_rtpr,
249 						 IH_DOORBELL_RPTR,
250 						 ENABLE, 0);
251 	}
252 	return ih_doorbell_rtpr;
253 }
254 
255 /**
256  * navi10_ih_enable_ring - enable an ih ring buffer
257  *
258  * @adev: amdgpu_device pointer
259  * @ih: amdgpu_ih_ring pointer
260  *
261  * Enable an ih ring buffer (NAVI10)
262  */
263 static int navi10_ih_enable_ring(struct amdgpu_device *adev,
264 				 struct amdgpu_ih_ring *ih)
265 {
266 	struct amdgpu_ih_regs *ih_regs;
267 	uint32_t tmp;
268 
269 	ih_regs = &ih->ih_regs;
270 
271 	/* Ring Buffer base. [39:8] of 40-bit address of the beginning of the ring buffer*/
272 	WREG32(ih_regs->ih_rb_base, ih->gpu_addr >> 8);
273 	WREG32(ih_regs->ih_rb_base_hi, (ih->gpu_addr >> 40) & 0xff);
274 
275 	tmp = RREG32(ih_regs->ih_rb_cntl);
276 	tmp = navi10_ih_rb_cntl(ih, tmp);
277 	if (ih == &adev->irq.ih)
278 		tmp = REG_SET_FIELD(tmp, IH_RB_CNTL, RPTR_REARM, !!adev->irq.msi_enabled);
279 	if (ih == &adev->irq.ih1) {
280 		tmp = REG_SET_FIELD(tmp, IH_RB_CNTL, WPTR_OVERFLOW_ENABLE, 0);
281 		tmp = REG_SET_FIELD(tmp, IH_RB_CNTL, RB_FULL_DRAIN_ENABLE, 1);
282 	}
283 
284 	if (amdgpu_sriov_vf(adev) && amdgpu_sriov_reg_indirect_ih(adev)) {
285 		if (psp_reg_program(&adev->psp, ih_regs->psp_reg_id, tmp)) {
286 			DRM_ERROR("PSP program IH_RB_CNTL failed!\n");
287 			return -ETIMEDOUT;
288 		}
289 	} else {
290 		WREG32(ih_regs->ih_rb_cntl, tmp);
291 	}
292 
293 	if (ih == &adev->irq.ih) {
294 		/* set the ih ring 0 writeback address whether it's enabled or not */
295 		WREG32(ih_regs->ih_rb_wptr_addr_lo, lower_32_bits(ih->wptr_addr));
296 		WREG32(ih_regs->ih_rb_wptr_addr_hi, upper_32_bits(ih->wptr_addr) & 0xFFFF);
297 	}
298 
299 	/* set rptr, wptr to 0 */
300 	WREG32(ih_regs->ih_rb_wptr, 0);
301 	WREG32(ih_regs->ih_rb_rptr, 0);
302 
303 	WREG32(ih_regs->ih_doorbell_rptr, navi10_ih_doorbell_rptr(ih));
304 
305 	return 0;
306 }
307 
308 /**
309  * navi10_ih_irq_init - init and enable the interrupt ring
310  *
311  * @adev: amdgpu_device pointer
312  *
313  * Allocate a ring buffer for the interrupt controller,
314  * enable the RLC, disable interrupts, enable the IH
315  * ring buffer and enable it (NAVI).
316  * Called at device load and reume.
317  * Returns 0 for success, errors for failure.
318  */
319 static int navi10_ih_irq_init(struct amdgpu_device *adev)
320 {
321 	struct amdgpu_ih_ring *ih[] = {&adev->irq.ih, &adev->irq.ih1, &adev->irq.ih2};
322 	u32 ih_chicken;
323 	u32 tmp;
324 	int ret;
325 	int i;
326 
327 	/* disable irqs */
328 	ret = navi10_ih_toggle_interrupts(adev, false);
329 	if (ret)
330 		return ret;
331 
332 	adev->nbio.funcs->ih_control(adev);
333 
334 	if (unlikely(adev->firmware.load_type == AMDGPU_FW_LOAD_DIRECT)) {
335 		if (ih[0]->use_bus_addr) {
336 			switch (adev->ip_versions[OSSSYS_HWIP][0]) {
337 			case IP_VERSION(5, 0, 3):
338 			case IP_VERSION(5, 2, 0):
339 			case IP_VERSION(5, 2, 1):
340 				ih_chicken = RREG32_SOC15(OSSSYS, 0, mmIH_CHICKEN_Sienna_Cichlid);
341 				ih_chicken = REG_SET_FIELD(ih_chicken,
342 						IH_CHICKEN, MC_SPACE_GPA_ENABLE, 1);
343 				WREG32_SOC15(OSSSYS, 0, mmIH_CHICKEN_Sienna_Cichlid, ih_chicken);
344 				break;
345 			default:
346 				ih_chicken = RREG32_SOC15(OSSSYS, 0, mmIH_CHICKEN);
347 				ih_chicken = REG_SET_FIELD(ih_chicken,
348 						IH_CHICKEN, MC_SPACE_GPA_ENABLE, 1);
349 				WREG32_SOC15(OSSSYS, 0, mmIH_CHICKEN, ih_chicken);
350 				break;
351 			}
352 		}
353 	}
354 
355 	for (i = 0; i < ARRAY_SIZE(ih); i++) {
356 		if (ih[i]->ring_size) {
357 			ret = navi10_ih_enable_ring(adev, ih[i]);
358 			if (ret)
359 				return ret;
360 		}
361 	}
362 
363 	/* update doorbell range for ih ring 0*/
364 	adev->nbio.funcs->ih_doorbell_range(adev, ih[0]->use_doorbell,
365 					    ih[0]->doorbell_index);
366 
367 	tmp = RREG32_SOC15(OSSSYS, 0, mmIH_STORM_CLIENT_LIST_CNTL);
368 	tmp = REG_SET_FIELD(tmp, IH_STORM_CLIENT_LIST_CNTL,
369 			    CLIENT18_IS_STORM_CLIENT, 1);
370 	WREG32_SOC15(OSSSYS, 0, mmIH_STORM_CLIENT_LIST_CNTL, tmp);
371 
372 	tmp = RREG32_SOC15(OSSSYS, 0, mmIH_INT_FLOOD_CNTL);
373 	tmp = REG_SET_FIELD(tmp, IH_INT_FLOOD_CNTL, FLOOD_CNTL_ENABLE, 1);
374 	WREG32_SOC15(OSSSYS, 0, mmIH_INT_FLOOD_CNTL, tmp);
375 
376 	pci_set_master(adev->pdev);
377 
378 	/* enable interrupts */
379 	ret = navi10_ih_toggle_interrupts(adev, true);
380 	if (ret)
381 		return ret;
382 	/* enable wptr force update for self int */
383 	force_update_wptr_for_self_int(adev, 0, 8, true);
384 
385 	if (adev->irq.ih_soft.ring_size)
386 		adev->irq.ih_soft.enabled = true;
387 
388 	return 0;
389 }
390 
391 /**
392  * navi10_ih_irq_disable - disable interrupts
393  *
394  * @adev: amdgpu_device pointer
395  *
396  * Disable interrupts on the hw (NAVI10).
397  */
398 static void navi10_ih_irq_disable(struct amdgpu_device *adev)
399 {
400 	force_update_wptr_for_self_int(adev, 0, 8, false);
401 	navi10_ih_toggle_interrupts(adev, false);
402 
403 	/* Wait and acknowledge irq */
404 	mdelay(1);
405 }
406 
407 /**
408  * navi10_ih_get_wptr - get the IH ring buffer wptr
409  *
410  * @adev: amdgpu_device pointer
411  * @ih: IH ring buffer to fetch wptr
412  *
413  * Get the IH ring buffer wptr from either the register
414  * or the writeback memory buffer (NAVI10).  Also check for
415  * ring buffer overflow and deal with it.
416  * Returns the value of the wptr.
417  */
418 static u32 navi10_ih_get_wptr(struct amdgpu_device *adev,
419 			      struct amdgpu_ih_ring *ih)
420 {
421 	u32 wptr, tmp;
422 	struct amdgpu_ih_regs *ih_regs;
423 
424 	wptr = le32_to_cpu(*ih->wptr_cpu);
425 	ih_regs = &ih->ih_regs;
426 
427 	if (!REG_GET_FIELD(wptr, IH_RB_WPTR, RB_OVERFLOW))
428 		goto out;
429 
430 	wptr = RREG32_NO_KIQ(ih_regs->ih_rb_wptr);
431 	if (!REG_GET_FIELD(wptr, IH_RB_WPTR, RB_OVERFLOW))
432 		goto out;
433 	wptr = REG_SET_FIELD(wptr, IH_RB_WPTR, RB_OVERFLOW, 0);
434 
435 	/* When a ring buffer overflow happen start parsing interrupt
436 	 * from the last not overwritten vector (wptr + 32). Hopefully
437 	 * this should allow us to catch up.
438 	 */
439 	tmp = (wptr + 32) & ih->ptr_mask;
440 	dev_warn(adev->dev, "IH ring buffer overflow "
441 		 "(0x%08X, 0x%08X, 0x%08X)\n",
442 		 wptr, ih->rptr, tmp);
443 	ih->rptr = tmp;
444 
445 	tmp = RREG32_NO_KIQ(ih_regs->ih_rb_cntl);
446 	tmp = REG_SET_FIELD(tmp, IH_RB_CNTL, WPTR_OVERFLOW_CLEAR, 1);
447 	WREG32_NO_KIQ(ih_regs->ih_rb_cntl, tmp);
448 out:
449 	return (wptr & ih->ptr_mask);
450 }
451 
452 /**
453  * navi10_ih_irq_rearm - rearm IRQ if lost
454  *
455  * @adev: amdgpu_device pointer
456  * @ih: IH ring to match
457  *
458  */
459 static void navi10_ih_irq_rearm(struct amdgpu_device *adev,
460 			       struct amdgpu_ih_ring *ih)
461 {
462 	uint32_t v = 0;
463 	uint32_t i = 0;
464 	struct amdgpu_ih_regs *ih_regs;
465 
466 	ih_regs = &ih->ih_regs;
467 
468 	/* Rearm IRQ / re-write doorbell if doorbell write is lost */
469 	for (i = 0; i < MAX_REARM_RETRY; i++) {
470 		v = RREG32_NO_KIQ(ih_regs->ih_rb_rptr);
471 		if ((v < ih->ring_size) && (v != ih->rptr))
472 			WDOORBELL32(ih->doorbell_index, ih->rptr);
473 		else
474 			break;
475 	}
476 }
477 
478 /**
479  * navi10_ih_set_rptr - set the IH ring buffer rptr
480  *
481  * @adev: amdgpu_device pointer
482  *
483  * @ih: IH ring buffer to set rptr
484  * Set the IH ring buffer rptr.
485  */
486 static void navi10_ih_set_rptr(struct amdgpu_device *adev,
487 			       struct amdgpu_ih_ring *ih)
488 {
489 	struct amdgpu_ih_regs *ih_regs;
490 
491 	if (ih->use_doorbell) {
492 		/* XXX check if swapping is necessary on BE */
493 		*ih->rptr_cpu = ih->rptr;
494 		WDOORBELL32(ih->doorbell_index, ih->rptr);
495 
496 		if (amdgpu_sriov_vf(adev))
497 			navi10_ih_irq_rearm(adev, ih);
498 	} else {
499 		ih_regs = &ih->ih_regs;
500 		WREG32(ih_regs->ih_rb_rptr, ih->rptr);
501 	}
502 }
503 
504 /**
505  * navi10_ih_self_irq - dispatch work for ring 1 and 2
506  *
507  * @adev: amdgpu_device pointer
508  * @source: irq source
509  * @entry: IV with WPTR update
510  *
511  * Update the WPTR from the IV and schedule work to handle the entries.
512  */
513 static int navi10_ih_self_irq(struct amdgpu_device *adev,
514 			      struct amdgpu_irq_src *source,
515 			      struct amdgpu_iv_entry *entry)
516 {
517 	uint32_t wptr = cpu_to_le32(entry->src_data[0]);
518 
519 	switch (entry->ring_id) {
520 	case 1:
521 		*adev->irq.ih1.wptr_cpu = wptr;
522 		schedule_work(&adev->irq.ih1_work);
523 		break;
524 	case 2:
525 		*adev->irq.ih2.wptr_cpu = wptr;
526 		schedule_work(&adev->irq.ih2_work);
527 		break;
528 	default: break;
529 	}
530 	return 0;
531 }
532 
533 static const struct amdgpu_irq_src_funcs navi10_ih_self_irq_funcs = {
534 	.process = navi10_ih_self_irq,
535 };
536 
537 static void navi10_ih_set_self_irq_funcs(struct amdgpu_device *adev)
538 {
539 	adev->irq.self_irq.num_types = 0;
540 	adev->irq.self_irq.funcs = &navi10_ih_self_irq_funcs;
541 }
542 
543 static int navi10_ih_early_init(void *handle)
544 {
545 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
546 
547 	navi10_ih_set_interrupt_funcs(adev);
548 	navi10_ih_set_self_irq_funcs(adev);
549 	return 0;
550 }
551 
552 static int navi10_ih_sw_init(void *handle)
553 {
554 	int r;
555 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
556 	bool use_bus_addr;
557 
558 	r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_IH, 0,
559 				&adev->irq.self_irq);
560 
561 	if (r)
562 		return r;
563 
564 	/* use gpu virtual address for ih ring
565 	 * until ih_checken is programmed to allow
566 	 * use bus address for ih ring by psp bl */
567 	if ((adev->flags & AMD_IS_APU) ||
568 	    (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP))
569 		use_bus_addr = false;
570 	else
571 		use_bus_addr = true;
572 	r = amdgpu_ih_ring_init(adev, &adev->irq.ih, 256 * 1024, use_bus_addr);
573 	if (r)
574 		return r;
575 
576 	adev->irq.ih.use_doorbell = true;
577 	adev->irq.ih.doorbell_index = adev->doorbell_index.ih << 1;
578 
579 	adev->irq.ih1.ring_size = 0;
580 	adev->irq.ih2.ring_size = 0;
581 
582 	/* initialize ih control registers offset */
583 	navi10_ih_init_register_offset(adev);
584 
585 	r = amdgpu_ih_ring_init(adev, &adev->irq.ih_soft, PAGE_SIZE, true);
586 	if (r)
587 		return r;
588 
589 	r = amdgpu_irq_init(adev);
590 
591 	return r;
592 }
593 
594 static int navi10_ih_sw_fini(void *handle)
595 {
596 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
597 
598 	amdgpu_irq_fini_sw(adev);
599 
600 	return 0;
601 }
602 
603 static int navi10_ih_hw_init(void *handle)
604 {
605 	int r;
606 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
607 
608 	r = navi10_ih_irq_init(adev);
609 	if (r)
610 		return r;
611 
612 	return 0;
613 }
614 
615 static int navi10_ih_hw_fini(void *handle)
616 {
617 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
618 
619 	navi10_ih_irq_disable(adev);
620 
621 	return 0;
622 }
623 
624 static int navi10_ih_suspend(void *handle)
625 {
626 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
627 
628 	return navi10_ih_hw_fini(adev);
629 }
630 
631 static int navi10_ih_resume(void *handle)
632 {
633 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
634 
635 	return navi10_ih_hw_init(adev);
636 }
637 
638 static bool navi10_ih_is_idle(void *handle)
639 {
640 	/* todo */
641 	return true;
642 }
643 
644 static int navi10_ih_wait_for_idle(void *handle)
645 {
646 	/* todo */
647 	return -ETIMEDOUT;
648 }
649 
650 static int navi10_ih_soft_reset(void *handle)
651 {
652 	/* todo */
653 	return 0;
654 }
655 
656 static void navi10_ih_update_clockgating_state(struct amdgpu_device *adev,
657 					       bool enable)
658 {
659 	uint32_t data, def, field_val;
660 
661 	if (adev->cg_flags & AMD_CG_SUPPORT_IH_CG) {
662 		def = data = RREG32_SOC15(OSSSYS, 0, mmIH_CLK_CTRL);
663 		field_val = enable ? 0 : 1;
664 		data = REG_SET_FIELD(data, IH_CLK_CTRL,
665 				     DBUS_MUX_CLK_SOFT_OVERRIDE, field_val);
666 		data = REG_SET_FIELD(data, IH_CLK_CTRL,
667 				     OSSSYS_SHARE_CLK_SOFT_OVERRIDE, field_val);
668 		data = REG_SET_FIELD(data, IH_CLK_CTRL,
669 				     LIMIT_SMN_CLK_SOFT_OVERRIDE, field_val);
670 		data = REG_SET_FIELD(data, IH_CLK_CTRL,
671 				     DYN_CLK_SOFT_OVERRIDE, field_val);
672 		data = REG_SET_FIELD(data, IH_CLK_CTRL,
673 				     REG_CLK_SOFT_OVERRIDE, field_val);
674 		if (def != data)
675 			WREG32_SOC15(OSSSYS, 0, mmIH_CLK_CTRL, data);
676 	}
677 
678 	return;
679 }
680 
681 static int navi10_ih_set_clockgating_state(void *handle,
682 					   enum amd_clockgating_state state)
683 {
684 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
685 
686 	navi10_ih_update_clockgating_state(adev,
687 				state == AMD_CG_STATE_GATE);
688 	return 0;
689 }
690 
691 static int navi10_ih_set_powergating_state(void *handle,
692 					   enum amd_powergating_state state)
693 {
694 	return 0;
695 }
696 
697 static void navi10_ih_get_clockgating_state(void *handle, u32 *flags)
698 {
699 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
700 
701 	if (!RREG32_SOC15(OSSSYS, 0, mmIH_CLK_CTRL))
702 		*flags |= AMD_CG_SUPPORT_IH_CG;
703 
704 	return;
705 }
706 
707 static const struct amd_ip_funcs navi10_ih_ip_funcs = {
708 	.name = "navi10_ih",
709 	.early_init = navi10_ih_early_init,
710 	.late_init = NULL,
711 	.sw_init = navi10_ih_sw_init,
712 	.sw_fini = navi10_ih_sw_fini,
713 	.hw_init = navi10_ih_hw_init,
714 	.hw_fini = navi10_ih_hw_fini,
715 	.suspend = navi10_ih_suspend,
716 	.resume = navi10_ih_resume,
717 	.is_idle = navi10_ih_is_idle,
718 	.wait_for_idle = navi10_ih_wait_for_idle,
719 	.soft_reset = navi10_ih_soft_reset,
720 	.set_clockgating_state = navi10_ih_set_clockgating_state,
721 	.set_powergating_state = navi10_ih_set_powergating_state,
722 	.get_clockgating_state = navi10_ih_get_clockgating_state,
723 };
724 
725 static const struct amdgpu_ih_funcs navi10_ih_funcs = {
726 	.get_wptr = navi10_ih_get_wptr,
727 	.decode_iv = amdgpu_ih_decode_iv_helper,
728 	.set_rptr = navi10_ih_set_rptr
729 };
730 
731 static void navi10_ih_set_interrupt_funcs(struct amdgpu_device *adev)
732 {
733 	if (adev->irq.ih_funcs == NULL)
734 		adev->irq.ih_funcs = &navi10_ih_funcs;
735 }
736 
737 const struct amdgpu_ip_block_version navi10_ih_ip_block =
738 {
739 	.type = AMD_IP_BLOCK_TYPE_IH,
740 	.major = 5,
741 	.minor = 0,
742 	.rev = 0,
743 	.funcs = &navi10_ih_ip_funcs,
744 };
745