xref: /dragonfly/sys/dev/drm/amd/amdgpu/cz_ih.c (revision b843c749)
1*b843c749SSergey Zigachev /*
2*b843c749SSergey Zigachev  * Copyright 2014 Advanced Micro Devices, Inc.
3*b843c749SSergey Zigachev  *
4*b843c749SSergey Zigachev  * Permission is hereby granted, free of charge, to any person obtaining a
5*b843c749SSergey Zigachev  * copy of this software and associated documentation files (the "Software"),
6*b843c749SSergey Zigachev  * to deal in the Software without restriction, including without limitation
7*b843c749SSergey Zigachev  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*b843c749SSergey Zigachev  * and/or sell copies of the Software, and to permit persons to whom the
9*b843c749SSergey Zigachev  * Software is furnished to do so, subject to the following conditions:
10*b843c749SSergey Zigachev  *
11*b843c749SSergey Zigachev  * The above copyright notice and this permission notice shall be included in
12*b843c749SSergey Zigachev  * all copies or substantial portions of the Software.
13*b843c749SSergey Zigachev  *
14*b843c749SSergey Zigachev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15*b843c749SSergey Zigachev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16*b843c749SSergey Zigachev  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17*b843c749SSergey Zigachev  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18*b843c749SSergey Zigachev  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19*b843c749SSergey Zigachev  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20*b843c749SSergey Zigachev  * OTHER DEALINGS IN THE SOFTWARE.
21*b843c749SSergey Zigachev  *
22*b843c749SSergey Zigachev  */
23*b843c749SSergey Zigachev #include <drm/drmP.h>
24*b843c749SSergey Zigachev #include "amdgpu.h"
25*b843c749SSergey Zigachev #include "amdgpu_ih.h"
26*b843c749SSergey Zigachev #include "vid.h"
27*b843c749SSergey Zigachev 
28*b843c749SSergey Zigachev #include "oss/oss_3_0_1_d.h"
29*b843c749SSergey Zigachev #include "oss/oss_3_0_1_sh_mask.h"
30*b843c749SSergey Zigachev 
31*b843c749SSergey Zigachev #include "bif/bif_5_1_d.h"
32*b843c749SSergey Zigachev #include "bif/bif_5_1_sh_mask.h"
33*b843c749SSergey Zigachev 
34*b843c749SSergey Zigachev /*
35*b843c749SSergey Zigachev  * Interrupts
36*b843c749SSergey Zigachev  * Starting with r6xx, interrupts are handled via a ring buffer.
37*b843c749SSergey Zigachev  * Ring buffers are areas of GPU accessible memory that the GPU
38*b843c749SSergey Zigachev  * writes interrupt vectors into and the host reads vectors out of.
39*b843c749SSergey Zigachev  * There is a rptr (read pointer) that determines where the
40*b843c749SSergey Zigachev  * host is currently reading, and a wptr (write pointer)
41*b843c749SSergey Zigachev  * which determines where the GPU has written.  When the
42*b843c749SSergey Zigachev  * pointers are equal, the ring is idle.  When the GPU
43*b843c749SSergey Zigachev  * writes vectors to the ring buffer, it increments the
44*b843c749SSergey Zigachev  * wptr.  When there is an interrupt, the host then starts
45*b843c749SSergey Zigachev  * fetching commands and processing them until the pointers are
46*b843c749SSergey Zigachev  * equal again at which point it updates the rptr.
47*b843c749SSergey Zigachev  */
48*b843c749SSergey Zigachev 
49*b843c749SSergey Zigachev static void cz_ih_set_interrupt_funcs(struct amdgpu_device *adev);
50*b843c749SSergey Zigachev 
51*b843c749SSergey Zigachev /**
52*b843c749SSergey Zigachev  * cz_ih_enable_interrupts - Enable the interrupt ring buffer
53*b843c749SSergey Zigachev  *
54*b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
55*b843c749SSergey Zigachev  *
56*b843c749SSergey Zigachev  * Enable the interrupt ring buffer (VI).
57*b843c749SSergey Zigachev  */
cz_ih_enable_interrupts(struct amdgpu_device * adev)58*b843c749SSergey Zigachev static void cz_ih_enable_interrupts(struct amdgpu_device *adev)
59*b843c749SSergey Zigachev {
60*b843c749SSergey Zigachev 	u32 ih_cntl = RREG32(mmIH_CNTL);
61*b843c749SSergey Zigachev 	u32 ih_rb_cntl = RREG32(mmIH_RB_CNTL);
62*b843c749SSergey Zigachev 
63*b843c749SSergey Zigachev 	ih_cntl = REG_SET_FIELD(ih_cntl, IH_CNTL, ENABLE_INTR, 1);
64*b843c749SSergey Zigachev 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, RB_ENABLE, 1);
65*b843c749SSergey Zigachev 	WREG32(mmIH_CNTL, ih_cntl);
66*b843c749SSergey Zigachev 	WREG32(mmIH_RB_CNTL, ih_rb_cntl);
67*b843c749SSergey Zigachev 	adev->irq.ih.enabled = true;
68*b843c749SSergey Zigachev }
69*b843c749SSergey Zigachev 
70*b843c749SSergey Zigachev /**
71*b843c749SSergey Zigachev  * cz_ih_disable_interrupts - Disable the interrupt ring buffer
72*b843c749SSergey Zigachev  *
73*b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
74*b843c749SSergey Zigachev  *
75*b843c749SSergey Zigachev  * Disable the interrupt ring buffer (VI).
76*b843c749SSergey Zigachev  */
cz_ih_disable_interrupts(struct amdgpu_device * adev)77*b843c749SSergey Zigachev static void cz_ih_disable_interrupts(struct amdgpu_device *adev)
78*b843c749SSergey Zigachev {
79*b843c749SSergey Zigachev 	u32 ih_rb_cntl = RREG32(mmIH_RB_CNTL);
80*b843c749SSergey Zigachev 	u32 ih_cntl = RREG32(mmIH_CNTL);
81*b843c749SSergey Zigachev 
82*b843c749SSergey Zigachev 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, RB_ENABLE, 0);
83*b843c749SSergey Zigachev 	ih_cntl = REG_SET_FIELD(ih_cntl, IH_CNTL, ENABLE_INTR, 0);
84*b843c749SSergey Zigachev 	WREG32(mmIH_RB_CNTL, ih_rb_cntl);
85*b843c749SSergey Zigachev 	WREG32(mmIH_CNTL, ih_cntl);
86*b843c749SSergey Zigachev 	/* set rptr, wptr to 0 */
87*b843c749SSergey Zigachev 	WREG32(mmIH_RB_RPTR, 0);
88*b843c749SSergey Zigachev 	WREG32(mmIH_RB_WPTR, 0);
89*b843c749SSergey Zigachev 	adev->irq.ih.enabled = false;
90*b843c749SSergey Zigachev 	adev->irq.ih.rptr = 0;
91*b843c749SSergey Zigachev }
92*b843c749SSergey Zigachev 
93*b843c749SSergey Zigachev /**
94*b843c749SSergey Zigachev  * cz_ih_irq_init - init and enable the interrupt ring
95*b843c749SSergey Zigachev  *
96*b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
97*b843c749SSergey Zigachev  *
98*b843c749SSergey Zigachev  * Allocate a ring buffer for the interrupt controller,
99*b843c749SSergey Zigachev  * enable the RLC, disable interrupts, enable the IH
100*b843c749SSergey Zigachev  * ring buffer and enable it (VI).
101*b843c749SSergey Zigachev  * Called at device load and reume.
102*b843c749SSergey Zigachev  * Returns 0 for success, errors for failure.
103*b843c749SSergey Zigachev  */
cz_ih_irq_init(struct amdgpu_device * adev)104*b843c749SSergey Zigachev static int cz_ih_irq_init(struct amdgpu_device *adev)
105*b843c749SSergey Zigachev {
106*b843c749SSergey Zigachev 	int rb_bufsz;
107*b843c749SSergey Zigachev 	u32 interrupt_cntl, ih_cntl, ih_rb_cntl;
108*b843c749SSergey Zigachev 	u64 wptr_off;
109*b843c749SSergey Zigachev 
110*b843c749SSergey Zigachev 	/* disable irqs */
111*b843c749SSergey Zigachev 	cz_ih_disable_interrupts(adev);
112*b843c749SSergey Zigachev 
113*b843c749SSergey Zigachev 	/* setup interrupt control */
114*b843c749SSergey Zigachev 	WREG32(mmINTERRUPT_CNTL2, adev->dummy_page_addr >> 8);
115*b843c749SSergey Zigachev 	interrupt_cntl = RREG32(mmINTERRUPT_CNTL);
116*b843c749SSergey Zigachev 	/* INTERRUPT_CNTL__IH_DUMMY_RD_OVERRIDE_MASK=0 - dummy read disabled with msi, enabled without msi
117*b843c749SSergey Zigachev 	 * INTERRUPT_CNTL__IH_DUMMY_RD_OVERRIDE_MASK=1 - dummy read controlled by IH_DUMMY_RD_EN
118*b843c749SSergey Zigachev 	 */
119*b843c749SSergey Zigachev 	interrupt_cntl = REG_SET_FIELD(interrupt_cntl, INTERRUPT_CNTL, IH_DUMMY_RD_OVERRIDE, 0);
120*b843c749SSergey Zigachev 	/* INTERRUPT_CNTL__IH_REQ_NONSNOOP_EN_MASK=1 if ring is in non-cacheable memory, e.g., vram */
121*b843c749SSergey Zigachev 	interrupt_cntl = REG_SET_FIELD(interrupt_cntl, INTERRUPT_CNTL, IH_REQ_NONSNOOP_EN, 0);
122*b843c749SSergey Zigachev 	WREG32(mmINTERRUPT_CNTL, interrupt_cntl);
123*b843c749SSergey Zigachev 
124*b843c749SSergey Zigachev 	/* Ring Buffer base. [39:8] of 40-bit address of the beginning of the ring buffer*/
125*b843c749SSergey Zigachev 	WREG32(mmIH_RB_BASE, adev->irq.ih.gpu_addr >> 8);
126*b843c749SSergey Zigachev 
127*b843c749SSergey Zigachev 	rb_bufsz = order_base_2(adev->irq.ih.ring_size / 4);
128*b843c749SSergey Zigachev 	ih_rb_cntl = REG_SET_FIELD(0, IH_RB_CNTL, WPTR_OVERFLOW_ENABLE, 1);
129*b843c749SSergey Zigachev 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, WPTR_OVERFLOW_CLEAR, 1);
130*b843c749SSergey Zigachev 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, RB_SIZE, rb_bufsz);
131*b843c749SSergey Zigachev 
132*b843c749SSergey Zigachev 	/* Ring Buffer write pointer writeback. If enabled, IH_RB_WPTR register value is written to memory */
133*b843c749SSergey Zigachev 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, WPTR_WRITEBACK_ENABLE, 1);
134*b843c749SSergey Zigachev 
135*b843c749SSergey Zigachev 	/* set the writeback address whether it's enabled or not */
136*b843c749SSergey Zigachev 	wptr_off = adev->wb.gpu_addr + (adev->irq.ih.wptr_offs * 4);
137*b843c749SSergey Zigachev 	WREG32(mmIH_RB_WPTR_ADDR_LO, lower_32_bits(wptr_off));
138*b843c749SSergey Zigachev 	WREG32(mmIH_RB_WPTR_ADDR_HI, upper_32_bits(wptr_off) & 0xFF);
139*b843c749SSergey Zigachev 
140*b843c749SSergey Zigachev 	WREG32(mmIH_RB_CNTL, ih_rb_cntl);
141*b843c749SSergey Zigachev 
142*b843c749SSergey Zigachev 	/* set rptr, wptr to 0 */
143*b843c749SSergey Zigachev 	WREG32(mmIH_RB_RPTR, 0);
144*b843c749SSergey Zigachev 	WREG32(mmIH_RB_WPTR, 0);
145*b843c749SSergey Zigachev 
146*b843c749SSergey Zigachev 	/* Default settings for IH_CNTL (disabled at first) */
147*b843c749SSergey Zigachev 	ih_cntl = RREG32(mmIH_CNTL);
148*b843c749SSergey Zigachev 	ih_cntl = REG_SET_FIELD(ih_cntl, IH_CNTL, MC_VMID, 0);
149*b843c749SSergey Zigachev 
150*b843c749SSergey Zigachev 	if (adev->irq.msi_enabled)
151*b843c749SSergey Zigachev 		ih_cntl = REG_SET_FIELD(ih_cntl, IH_CNTL, RPTR_REARM, 1);
152*b843c749SSergey Zigachev 	WREG32(mmIH_CNTL, ih_cntl);
153*b843c749SSergey Zigachev 
154*b843c749SSergey Zigachev 	pci_set_master(adev->pdev);
155*b843c749SSergey Zigachev 
156*b843c749SSergey Zigachev 	/* enable interrupts */
157*b843c749SSergey Zigachev 	cz_ih_enable_interrupts(adev);
158*b843c749SSergey Zigachev 
159*b843c749SSergey Zigachev 	return 0;
160*b843c749SSergey Zigachev }
161*b843c749SSergey Zigachev 
162*b843c749SSergey Zigachev /**
163*b843c749SSergey Zigachev  * cz_ih_irq_disable - disable interrupts
164*b843c749SSergey Zigachev  *
165*b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
166*b843c749SSergey Zigachev  *
167*b843c749SSergey Zigachev  * Disable interrupts on the hw (VI).
168*b843c749SSergey Zigachev  */
cz_ih_irq_disable(struct amdgpu_device * adev)169*b843c749SSergey Zigachev static void cz_ih_irq_disable(struct amdgpu_device *adev)
170*b843c749SSergey Zigachev {
171*b843c749SSergey Zigachev 	cz_ih_disable_interrupts(adev);
172*b843c749SSergey Zigachev 
173*b843c749SSergey Zigachev 	/* Wait and acknowledge irq */
174*b843c749SSergey Zigachev 	mdelay(1);
175*b843c749SSergey Zigachev }
176*b843c749SSergey Zigachev 
177*b843c749SSergey Zigachev /**
178*b843c749SSergey Zigachev  * cz_ih_get_wptr - get the IH ring buffer wptr
179*b843c749SSergey Zigachev  *
180*b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
181*b843c749SSergey Zigachev  *
182*b843c749SSergey Zigachev  * Get the IH ring buffer wptr from either the register
183*b843c749SSergey Zigachev  * or the writeback memory buffer (VI).  Also check for
184*b843c749SSergey Zigachev  * ring buffer overflow and deal with it.
185*b843c749SSergey Zigachev  * Used by cz_irq_process(VI).
186*b843c749SSergey Zigachev  * Returns the value of the wptr.
187*b843c749SSergey Zigachev  */
cz_ih_get_wptr(struct amdgpu_device * adev)188*b843c749SSergey Zigachev static u32 cz_ih_get_wptr(struct amdgpu_device *adev)
189*b843c749SSergey Zigachev {
190*b843c749SSergey Zigachev 	u32 wptr, tmp;
191*b843c749SSergey Zigachev 
192*b843c749SSergey Zigachev 	wptr = le32_to_cpu(adev->wb.wb[adev->irq.ih.wptr_offs]);
193*b843c749SSergey Zigachev 
194*b843c749SSergey Zigachev 	if (REG_GET_FIELD(wptr, IH_RB_WPTR, RB_OVERFLOW)) {
195*b843c749SSergey Zigachev 		wptr = REG_SET_FIELD(wptr, IH_RB_WPTR, RB_OVERFLOW, 0);
196*b843c749SSergey Zigachev 		/* When a ring buffer overflow happen start parsing interrupt
197*b843c749SSergey Zigachev 		 * from the last not overwritten vector (wptr + 16). Hopefully
198*b843c749SSergey Zigachev 		 * this should allow us to catchup.
199*b843c749SSergey Zigachev 		 */
200*b843c749SSergey Zigachev 		dev_warn(adev->dev, "IH ring buffer overflow (0x%08X, 0x%08X, 0x%08X)\n",
201*b843c749SSergey Zigachev 			wptr, adev->irq.ih.rptr, (wptr + 16) & adev->irq.ih.ptr_mask);
202*b843c749SSergey Zigachev 		adev->irq.ih.rptr = (wptr + 16) & adev->irq.ih.ptr_mask;
203*b843c749SSergey Zigachev 		tmp = RREG32(mmIH_RB_CNTL);
204*b843c749SSergey Zigachev 		tmp = REG_SET_FIELD(tmp, IH_RB_CNTL, WPTR_OVERFLOW_CLEAR, 1);
205*b843c749SSergey Zigachev 		WREG32(mmIH_RB_CNTL, tmp);
206*b843c749SSergey Zigachev 	}
207*b843c749SSergey Zigachev 	return (wptr & adev->irq.ih.ptr_mask);
208*b843c749SSergey Zigachev }
209*b843c749SSergey Zigachev 
210*b843c749SSergey Zigachev /**
211*b843c749SSergey Zigachev  * cz_ih_prescreen_iv - prescreen an interrupt vector
212*b843c749SSergey Zigachev  *
213*b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
214*b843c749SSergey Zigachev  *
215*b843c749SSergey Zigachev  * Returns true if the interrupt vector should be further processed.
216*b843c749SSergey Zigachev  */
cz_ih_prescreen_iv(struct amdgpu_device * adev)217*b843c749SSergey Zigachev static bool cz_ih_prescreen_iv(struct amdgpu_device *adev)
218*b843c749SSergey Zigachev {
219*b843c749SSergey Zigachev 	u32 ring_index = adev->irq.ih.rptr >> 2;
220*b843c749SSergey Zigachev 	u16 pasid;
221*b843c749SSergey Zigachev 
222*b843c749SSergey Zigachev 	switch (le32_to_cpu(adev->irq.ih.ring[ring_index]) & 0xff) {
223*b843c749SSergey Zigachev 	case 146:
224*b843c749SSergey Zigachev 	case 147:
225*b843c749SSergey Zigachev 		pasid = le32_to_cpu(adev->irq.ih.ring[ring_index + 2]) >> 16;
226*b843c749SSergey Zigachev 		if (!pasid || amdgpu_vm_pasid_fault_credit(adev, pasid))
227*b843c749SSergey Zigachev 			return true;
228*b843c749SSergey Zigachev 		break;
229*b843c749SSergey Zigachev 	default:
230*b843c749SSergey Zigachev 		/* Not a VM fault */
231*b843c749SSergey Zigachev 		return true;
232*b843c749SSergey Zigachev 	}
233*b843c749SSergey Zigachev 
234*b843c749SSergey Zigachev 	adev->irq.ih.rptr += 16;
235*b843c749SSergey Zigachev 	return false;
236*b843c749SSergey Zigachev }
237*b843c749SSergey Zigachev 
238*b843c749SSergey Zigachev /**
239*b843c749SSergey Zigachev  * cz_ih_decode_iv - decode an interrupt vector
240*b843c749SSergey Zigachev  *
241*b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
242*b843c749SSergey Zigachev  *
243*b843c749SSergey Zigachev  * Decodes the interrupt vector at the current rptr
244*b843c749SSergey Zigachev  * position and also advance the position.
245*b843c749SSergey Zigachev  */
cz_ih_decode_iv(struct amdgpu_device * adev,struct amdgpu_iv_entry * entry)246*b843c749SSergey Zigachev static void cz_ih_decode_iv(struct amdgpu_device *adev,
247*b843c749SSergey Zigachev 				 struct amdgpu_iv_entry *entry)
248*b843c749SSergey Zigachev {
249*b843c749SSergey Zigachev 	/* wptr/rptr are in bytes! */
250*b843c749SSergey Zigachev 	u32 ring_index = adev->irq.ih.rptr >> 2;
251*b843c749SSergey Zigachev 	uint32_t dw[4];
252*b843c749SSergey Zigachev 
253*b843c749SSergey Zigachev 	dw[0] = le32_to_cpu(adev->irq.ih.ring[ring_index + 0]);
254*b843c749SSergey Zigachev 	dw[1] = le32_to_cpu(adev->irq.ih.ring[ring_index + 1]);
255*b843c749SSergey Zigachev 	dw[2] = le32_to_cpu(adev->irq.ih.ring[ring_index + 2]);
256*b843c749SSergey Zigachev 	dw[3] = le32_to_cpu(adev->irq.ih.ring[ring_index + 3]);
257*b843c749SSergey Zigachev 
258*b843c749SSergey Zigachev 	entry->client_id = AMDGPU_IH_CLIENTID_LEGACY;
259*b843c749SSergey Zigachev 	entry->src_id = dw[0] & 0xff;
260*b843c749SSergey Zigachev 	entry->src_data[0] = dw[1] & 0xfffffff;
261*b843c749SSergey Zigachev 	entry->ring_id = dw[2] & 0xff;
262*b843c749SSergey Zigachev 	entry->vmid = (dw[2] >> 8) & 0xff;
263*b843c749SSergey Zigachev 	entry->pasid = (dw[2] >> 16) & 0xffff;
264*b843c749SSergey Zigachev 
265*b843c749SSergey Zigachev 	/* wptr/rptr are in bytes! */
266*b843c749SSergey Zigachev 	adev->irq.ih.rptr += 16;
267*b843c749SSergey Zigachev }
268*b843c749SSergey Zigachev 
269*b843c749SSergey Zigachev /**
270*b843c749SSergey Zigachev  * cz_ih_set_rptr - set the IH ring buffer rptr
271*b843c749SSergey Zigachev  *
272*b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
273*b843c749SSergey Zigachev  *
274*b843c749SSergey Zigachev  * Set the IH ring buffer rptr.
275*b843c749SSergey Zigachev  */
cz_ih_set_rptr(struct amdgpu_device * adev)276*b843c749SSergey Zigachev static void cz_ih_set_rptr(struct amdgpu_device *adev)
277*b843c749SSergey Zigachev {
278*b843c749SSergey Zigachev 	WREG32(mmIH_RB_RPTR, adev->irq.ih.rptr);
279*b843c749SSergey Zigachev }
280*b843c749SSergey Zigachev 
cz_ih_early_init(void * handle)281*b843c749SSergey Zigachev static int cz_ih_early_init(void *handle)
282*b843c749SSergey Zigachev {
283*b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
284*b843c749SSergey Zigachev 	int ret;
285*b843c749SSergey Zigachev 
286*b843c749SSergey Zigachev 	ret = amdgpu_irq_add_domain(adev);
287*b843c749SSergey Zigachev 	if (ret)
288*b843c749SSergey Zigachev 		return ret;
289*b843c749SSergey Zigachev 
290*b843c749SSergey Zigachev 	cz_ih_set_interrupt_funcs(adev);
291*b843c749SSergey Zigachev 
292*b843c749SSergey Zigachev 	return 0;
293*b843c749SSergey Zigachev }
294*b843c749SSergey Zigachev 
cz_ih_sw_init(void * handle)295*b843c749SSergey Zigachev static int cz_ih_sw_init(void *handle)
296*b843c749SSergey Zigachev {
297*b843c749SSergey Zigachev 	int r;
298*b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
299*b843c749SSergey Zigachev 
300*b843c749SSergey Zigachev 	r = amdgpu_ih_ring_init(adev, 64 * 1024, false);
301*b843c749SSergey Zigachev 	if (r)
302*b843c749SSergey Zigachev 		return r;
303*b843c749SSergey Zigachev 
304*b843c749SSergey Zigachev 	r = amdgpu_irq_init(adev);
305*b843c749SSergey Zigachev 
306*b843c749SSergey Zigachev 	return r;
307*b843c749SSergey Zigachev }
308*b843c749SSergey Zigachev 
cz_ih_sw_fini(void * handle)309*b843c749SSergey Zigachev static int cz_ih_sw_fini(void *handle)
310*b843c749SSergey Zigachev {
311*b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
312*b843c749SSergey Zigachev 
313*b843c749SSergey Zigachev 	amdgpu_irq_fini(adev);
314*b843c749SSergey Zigachev 	amdgpu_ih_ring_fini(adev);
315*b843c749SSergey Zigachev 	amdgpu_irq_remove_domain(adev);
316*b843c749SSergey Zigachev 
317*b843c749SSergey Zigachev 	return 0;
318*b843c749SSergey Zigachev }
319*b843c749SSergey Zigachev 
cz_ih_hw_init(void * handle)320*b843c749SSergey Zigachev static int cz_ih_hw_init(void *handle)
321*b843c749SSergey Zigachev {
322*b843c749SSergey Zigachev 	int r;
323*b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
324*b843c749SSergey Zigachev 
325*b843c749SSergey Zigachev 	r = cz_ih_irq_init(adev);
326*b843c749SSergey Zigachev 	if (r)
327*b843c749SSergey Zigachev 		return r;
328*b843c749SSergey Zigachev 
329*b843c749SSergey Zigachev 	return 0;
330*b843c749SSergey Zigachev }
331*b843c749SSergey Zigachev 
cz_ih_hw_fini(void * handle)332*b843c749SSergey Zigachev static int cz_ih_hw_fini(void *handle)
333*b843c749SSergey Zigachev {
334*b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
335*b843c749SSergey Zigachev 
336*b843c749SSergey Zigachev 	cz_ih_irq_disable(adev);
337*b843c749SSergey Zigachev 
338*b843c749SSergey Zigachev 	return 0;
339*b843c749SSergey Zigachev }
340*b843c749SSergey Zigachev 
cz_ih_suspend(void * handle)341*b843c749SSergey Zigachev static int cz_ih_suspend(void *handle)
342*b843c749SSergey Zigachev {
343*b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
344*b843c749SSergey Zigachev 
345*b843c749SSergey Zigachev 	return cz_ih_hw_fini(adev);
346*b843c749SSergey Zigachev }
347*b843c749SSergey Zigachev 
cz_ih_resume(void * handle)348*b843c749SSergey Zigachev static int cz_ih_resume(void *handle)
349*b843c749SSergey Zigachev {
350*b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
351*b843c749SSergey Zigachev 
352*b843c749SSergey Zigachev 	return cz_ih_hw_init(adev);
353*b843c749SSergey Zigachev }
354*b843c749SSergey Zigachev 
cz_ih_is_idle(void * handle)355*b843c749SSergey Zigachev static bool cz_ih_is_idle(void *handle)
356*b843c749SSergey Zigachev {
357*b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
358*b843c749SSergey Zigachev 	u32 tmp = RREG32(mmSRBM_STATUS);
359*b843c749SSergey Zigachev 
360*b843c749SSergey Zigachev 	if (REG_GET_FIELD(tmp, SRBM_STATUS, IH_BUSY))
361*b843c749SSergey Zigachev 		return false;
362*b843c749SSergey Zigachev 
363*b843c749SSergey Zigachev 	return true;
364*b843c749SSergey Zigachev }
365*b843c749SSergey Zigachev 
cz_ih_wait_for_idle(void * handle)366*b843c749SSergey Zigachev static int cz_ih_wait_for_idle(void *handle)
367*b843c749SSergey Zigachev {
368*b843c749SSergey Zigachev 	unsigned i;
369*b843c749SSergey Zigachev 	u32 tmp;
370*b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
371*b843c749SSergey Zigachev 
372*b843c749SSergey Zigachev 	for (i = 0; i < adev->usec_timeout; i++) {
373*b843c749SSergey Zigachev 		/* read MC_STATUS */
374*b843c749SSergey Zigachev 		tmp = RREG32(mmSRBM_STATUS);
375*b843c749SSergey Zigachev 		if (!REG_GET_FIELD(tmp, SRBM_STATUS, IH_BUSY))
376*b843c749SSergey Zigachev 			return 0;
377*b843c749SSergey Zigachev 		udelay(1);
378*b843c749SSergey Zigachev 	}
379*b843c749SSergey Zigachev 	return -ETIMEDOUT;
380*b843c749SSergey Zigachev }
381*b843c749SSergey Zigachev 
cz_ih_soft_reset(void * handle)382*b843c749SSergey Zigachev static int cz_ih_soft_reset(void *handle)
383*b843c749SSergey Zigachev {
384*b843c749SSergey Zigachev 	u32 srbm_soft_reset = 0;
385*b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
386*b843c749SSergey Zigachev 	u32 tmp = RREG32(mmSRBM_STATUS);
387*b843c749SSergey Zigachev 
388*b843c749SSergey Zigachev 	if (tmp & SRBM_STATUS__IH_BUSY_MASK)
389*b843c749SSergey Zigachev 		srbm_soft_reset = REG_SET_FIELD(srbm_soft_reset, SRBM_SOFT_RESET,
390*b843c749SSergey Zigachev 						SOFT_RESET_IH, 1);
391*b843c749SSergey Zigachev 
392*b843c749SSergey Zigachev 	if (srbm_soft_reset) {
393*b843c749SSergey Zigachev 		tmp = RREG32(mmSRBM_SOFT_RESET);
394*b843c749SSergey Zigachev 		tmp |= srbm_soft_reset;
395*b843c749SSergey Zigachev 		dev_info(adev->dev, "SRBM_SOFT_RESET=0x%08X\n", tmp);
396*b843c749SSergey Zigachev 		WREG32(mmSRBM_SOFT_RESET, tmp);
397*b843c749SSergey Zigachev 		tmp = RREG32(mmSRBM_SOFT_RESET);
398*b843c749SSergey Zigachev 
399*b843c749SSergey Zigachev 		udelay(50);
400*b843c749SSergey Zigachev 
401*b843c749SSergey Zigachev 		tmp &= ~srbm_soft_reset;
402*b843c749SSergey Zigachev 		WREG32(mmSRBM_SOFT_RESET, tmp);
403*b843c749SSergey Zigachev 		tmp = RREG32(mmSRBM_SOFT_RESET);
404*b843c749SSergey Zigachev 
405*b843c749SSergey Zigachev 		/* Wait a little for things to settle down */
406*b843c749SSergey Zigachev 		udelay(50);
407*b843c749SSergey Zigachev 	}
408*b843c749SSergey Zigachev 
409*b843c749SSergey Zigachev 	return 0;
410*b843c749SSergey Zigachev }
411*b843c749SSergey Zigachev 
cz_ih_set_clockgating_state(void * handle,enum amd_clockgating_state state)412*b843c749SSergey Zigachev static int cz_ih_set_clockgating_state(void *handle,
413*b843c749SSergey Zigachev 					  enum amd_clockgating_state state)
414*b843c749SSergey Zigachev {
415*b843c749SSergey Zigachev 	// TODO
416*b843c749SSergey Zigachev 	return 0;
417*b843c749SSergey Zigachev }
418*b843c749SSergey Zigachev 
cz_ih_set_powergating_state(void * handle,enum amd_powergating_state state)419*b843c749SSergey Zigachev static int cz_ih_set_powergating_state(void *handle,
420*b843c749SSergey Zigachev 					  enum amd_powergating_state state)
421*b843c749SSergey Zigachev {
422*b843c749SSergey Zigachev 	// TODO
423*b843c749SSergey Zigachev 	return 0;
424*b843c749SSergey Zigachev }
425*b843c749SSergey Zigachev 
426*b843c749SSergey Zigachev static const struct amd_ip_funcs cz_ih_ip_funcs = {
427*b843c749SSergey Zigachev 	.name = "cz_ih",
428*b843c749SSergey Zigachev 	.early_init = cz_ih_early_init,
429*b843c749SSergey Zigachev 	.late_init = NULL,
430*b843c749SSergey Zigachev 	.sw_init = cz_ih_sw_init,
431*b843c749SSergey Zigachev 	.sw_fini = cz_ih_sw_fini,
432*b843c749SSergey Zigachev 	.hw_init = cz_ih_hw_init,
433*b843c749SSergey Zigachev 	.hw_fini = cz_ih_hw_fini,
434*b843c749SSergey Zigachev 	.suspend = cz_ih_suspend,
435*b843c749SSergey Zigachev 	.resume = cz_ih_resume,
436*b843c749SSergey Zigachev 	.is_idle = cz_ih_is_idle,
437*b843c749SSergey Zigachev 	.wait_for_idle = cz_ih_wait_for_idle,
438*b843c749SSergey Zigachev 	.soft_reset = cz_ih_soft_reset,
439*b843c749SSergey Zigachev 	.set_clockgating_state = cz_ih_set_clockgating_state,
440*b843c749SSergey Zigachev 	.set_powergating_state = cz_ih_set_powergating_state,
441*b843c749SSergey Zigachev };
442*b843c749SSergey Zigachev 
443*b843c749SSergey Zigachev static const struct amdgpu_ih_funcs cz_ih_funcs = {
444*b843c749SSergey Zigachev 	.get_wptr = cz_ih_get_wptr,
445*b843c749SSergey Zigachev 	.prescreen_iv = cz_ih_prescreen_iv,
446*b843c749SSergey Zigachev 	.decode_iv = cz_ih_decode_iv,
447*b843c749SSergey Zigachev 	.set_rptr = cz_ih_set_rptr
448*b843c749SSergey Zigachev };
449*b843c749SSergey Zigachev 
cz_ih_set_interrupt_funcs(struct amdgpu_device * adev)450*b843c749SSergey Zigachev static void cz_ih_set_interrupt_funcs(struct amdgpu_device *adev)
451*b843c749SSergey Zigachev {
452*b843c749SSergey Zigachev 	if (adev->irq.ih_funcs == NULL)
453*b843c749SSergey Zigachev 		adev->irq.ih_funcs = &cz_ih_funcs;
454*b843c749SSergey Zigachev }
455*b843c749SSergey Zigachev 
456*b843c749SSergey Zigachev const struct amdgpu_ip_block_version cz_ih_ip_block =
457*b843c749SSergey Zigachev {
458*b843c749SSergey Zigachev 	.type = AMD_IP_BLOCK_TYPE_IH,
459*b843c749SSergey Zigachev 	.major = 3,
460*b843c749SSergey Zigachev 	.minor = 0,
461*b843c749SSergey Zigachev 	.rev = 0,
462*b843c749SSergey Zigachev 	.funcs = &cz_ih_ip_funcs,
463*b843c749SSergey Zigachev };
464