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_d.h"
29*b843c749SSergey Zigachev #include "oss/oss_3_0_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 tonga_ih_set_interrupt_funcs(struct amdgpu_device *adev);
50*b843c749SSergey Zigachev
51*b843c749SSergey Zigachev /**
52*b843c749SSergey Zigachev * tonga_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 */
tonga_ih_enable_interrupts(struct amdgpu_device * adev)58*b843c749SSergey Zigachev static void tonga_ih_enable_interrupts(struct amdgpu_device *adev)
59*b843c749SSergey Zigachev {
60*b843c749SSergey Zigachev u32 ih_rb_cntl = RREG32(mmIH_RB_CNTL);
61*b843c749SSergey Zigachev
62*b843c749SSergey Zigachev ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, RB_ENABLE, 1);
63*b843c749SSergey Zigachev ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, ENABLE_INTR, 1);
64*b843c749SSergey Zigachev WREG32(mmIH_RB_CNTL, ih_rb_cntl);
65*b843c749SSergey Zigachev adev->irq.ih.enabled = true;
66*b843c749SSergey Zigachev }
67*b843c749SSergey Zigachev
68*b843c749SSergey Zigachev /**
69*b843c749SSergey Zigachev * tonga_ih_disable_interrupts - Disable the interrupt ring buffer
70*b843c749SSergey Zigachev *
71*b843c749SSergey Zigachev * @adev: amdgpu_device pointer
72*b843c749SSergey Zigachev *
73*b843c749SSergey Zigachev * Disable the interrupt ring buffer (VI).
74*b843c749SSergey Zigachev */
tonga_ih_disable_interrupts(struct amdgpu_device * adev)75*b843c749SSergey Zigachev static void tonga_ih_disable_interrupts(struct amdgpu_device *adev)
76*b843c749SSergey Zigachev {
77*b843c749SSergey Zigachev u32 ih_rb_cntl = RREG32(mmIH_RB_CNTL);
78*b843c749SSergey Zigachev
79*b843c749SSergey Zigachev ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, RB_ENABLE, 0);
80*b843c749SSergey Zigachev ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, ENABLE_INTR, 0);
81*b843c749SSergey Zigachev WREG32(mmIH_RB_CNTL, ih_rb_cntl);
82*b843c749SSergey Zigachev /* set rptr, wptr to 0 */
83*b843c749SSergey Zigachev WREG32(mmIH_RB_RPTR, 0);
84*b843c749SSergey Zigachev WREG32(mmIH_RB_WPTR, 0);
85*b843c749SSergey Zigachev adev->irq.ih.enabled = false;
86*b843c749SSergey Zigachev adev->irq.ih.rptr = 0;
87*b843c749SSergey Zigachev }
88*b843c749SSergey Zigachev
89*b843c749SSergey Zigachev /**
90*b843c749SSergey Zigachev * tonga_ih_irq_init - init and enable the interrupt ring
91*b843c749SSergey Zigachev *
92*b843c749SSergey Zigachev * @adev: amdgpu_device pointer
93*b843c749SSergey Zigachev *
94*b843c749SSergey Zigachev * Allocate a ring buffer for the interrupt controller,
95*b843c749SSergey Zigachev * enable the RLC, disable interrupts, enable the IH
96*b843c749SSergey Zigachev * ring buffer and enable it (VI).
97*b843c749SSergey Zigachev * Called at device load and reume.
98*b843c749SSergey Zigachev * Returns 0 for success, errors for failure.
99*b843c749SSergey Zigachev */
tonga_ih_irq_init(struct amdgpu_device * adev)100*b843c749SSergey Zigachev static int tonga_ih_irq_init(struct amdgpu_device *adev)
101*b843c749SSergey Zigachev {
102*b843c749SSergey Zigachev int rb_bufsz;
103*b843c749SSergey Zigachev u32 interrupt_cntl, ih_rb_cntl, ih_doorbell_rtpr;
104*b843c749SSergey Zigachev u64 wptr_off;
105*b843c749SSergey Zigachev
106*b843c749SSergey Zigachev /* disable irqs */
107*b843c749SSergey Zigachev tonga_ih_disable_interrupts(adev);
108*b843c749SSergey Zigachev
109*b843c749SSergey Zigachev /* setup interrupt control */
110*b843c749SSergey Zigachev WREG32(mmINTERRUPT_CNTL2, adev->dummy_page_addr >> 8);
111*b843c749SSergey Zigachev interrupt_cntl = RREG32(mmINTERRUPT_CNTL);
112*b843c749SSergey Zigachev /* INTERRUPT_CNTL__IH_DUMMY_RD_OVERRIDE_MASK=0 - dummy read disabled with msi, enabled without msi
113*b843c749SSergey Zigachev * INTERRUPT_CNTL__IH_DUMMY_RD_OVERRIDE_MASK=1 - dummy read controlled by IH_DUMMY_RD_EN
114*b843c749SSergey Zigachev */
115*b843c749SSergey Zigachev interrupt_cntl = REG_SET_FIELD(interrupt_cntl, INTERRUPT_CNTL, IH_DUMMY_RD_OVERRIDE, 0);
116*b843c749SSergey Zigachev /* INTERRUPT_CNTL__IH_REQ_NONSNOOP_EN_MASK=1 if ring is in non-cacheable memory, e.g., vram */
117*b843c749SSergey Zigachev interrupt_cntl = REG_SET_FIELD(interrupt_cntl, INTERRUPT_CNTL, IH_REQ_NONSNOOP_EN, 0);
118*b843c749SSergey Zigachev WREG32(mmINTERRUPT_CNTL, interrupt_cntl);
119*b843c749SSergey Zigachev
120*b843c749SSergey Zigachev /* Ring Buffer base. [39:8] of 40-bit address of the beginning of the ring buffer*/
121*b843c749SSergey Zigachev if (adev->irq.ih.use_bus_addr)
122*b843c749SSergey Zigachev WREG32(mmIH_RB_BASE, adev->irq.ih.rb_dma_addr >> 8);
123*b843c749SSergey Zigachev else
124*b843c749SSergey Zigachev WREG32(mmIH_RB_BASE, adev->irq.ih.gpu_addr >> 8);
125*b843c749SSergey Zigachev
126*b843c749SSergey Zigachev rb_bufsz = order_base_2(adev->irq.ih.ring_size / 4);
127*b843c749SSergey Zigachev ih_rb_cntl = REG_SET_FIELD(0, IH_RB_CNTL, WPTR_OVERFLOW_CLEAR, 1);
128*b843c749SSergey Zigachev ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, RB_SIZE, rb_bufsz);
129*b843c749SSergey Zigachev /* Ring Buffer write pointer writeback. If enabled, IH_RB_WPTR register value is written to memory */
130*b843c749SSergey Zigachev ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, WPTR_WRITEBACK_ENABLE, 1);
131*b843c749SSergey Zigachev ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, MC_VMID, 0);
132*b843c749SSergey Zigachev
133*b843c749SSergey Zigachev if (adev->irq.msi_enabled)
134*b843c749SSergey Zigachev ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, RPTR_REARM, 1);
135*b843c749SSergey Zigachev
136*b843c749SSergey Zigachev WREG32(mmIH_RB_CNTL, ih_rb_cntl);
137*b843c749SSergey Zigachev
138*b843c749SSergey Zigachev /* set the writeback address whether it's enabled or not */
139*b843c749SSergey Zigachev if (adev->irq.ih.use_bus_addr)
140*b843c749SSergey Zigachev wptr_off = adev->irq.ih.rb_dma_addr + (adev->irq.ih.wptr_offs * 4);
141*b843c749SSergey Zigachev else
142*b843c749SSergey Zigachev wptr_off = adev->wb.gpu_addr + (adev->irq.ih.wptr_offs * 4);
143*b843c749SSergey Zigachev WREG32(mmIH_RB_WPTR_ADDR_LO, lower_32_bits(wptr_off));
144*b843c749SSergey Zigachev WREG32(mmIH_RB_WPTR_ADDR_HI, upper_32_bits(wptr_off) & 0xFF);
145*b843c749SSergey Zigachev
146*b843c749SSergey Zigachev /* set rptr, wptr to 0 */
147*b843c749SSergey Zigachev WREG32(mmIH_RB_RPTR, 0);
148*b843c749SSergey Zigachev WREG32(mmIH_RB_WPTR, 0);
149*b843c749SSergey Zigachev
150*b843c749SSergey Zigachev ih_doorbell_rtpr = RREG32(mmIH_DOORBELL_RPTR);
151*b843c749SSergey Zigachev if (adev->irq.ih.use_doorbell) {
152*b843c749SSergey Zigachev ih_doorbell_rtpr = REG_SET_FIELD(ih_doorbell_rtpr, IH_DOORBELL_RPTR,
153*b843c749SSergey Zigachev OFFSET, adev->irq.ih.doorbell_index);
154*b843c749SSergey Zigachev ih_doorbell_rtpr = REG_SET_FIELD(ih_doorbell_rtpr, IH_DOORBELL_RPTR,
155*b843c749SSergey Zigachev ENABLE, 1);
156*b843c749SSergey Zigachev } else {
157*b843c749SSergey Zigachev ih_doorbell_rtpr = REG_SET_FIELD(ih_doorbell_rtpr, IH_DOORBELL_RPTR,
158*b843c749SSergey Zigachev ENABLE, 0);
159*b843c749SSergey Zigachev }
160*b843c749SSergey Zigachev WREG32(mmIH_DOORBELL_RPTR, ih_doorbell_rtpr);
161*b843c749SSergey Zigachev
162*b843c749SSergey Zigachev pci_set_master(adev->pdev);
163*b843c749SSergey Zigachev
164*b843c749SSergey Zigachev /* enable interrupts */
165*b843c749SSergey Zigachev tonga_ih_enable_interrupts(adev);
166*b843c749SSergey Zigachev
167*b843c749SSergey Zigachev return 0;
168*b843c749SSergey Zigachev }
169*b843c749SSergey Zigachev
170*b843c749SSergey Zigachev /**
171*b843c749SSergey Zigachev * tonga_ih_irq_disable - disable interrupts
172*b843c749SSergey Zigachev *
173*b843c749SSergey Zigachev * @adev: amdgpu_device pointer
174*b843c749SSergey Zigachev *
175*b843c749SSergey Zigachev * Disable interrupts on the hw (VI).
176*b843c749SSergey Zigachev */
tonga_ih_irq_disable(struct amdgpu_device * adev)177*b843c749SSergey Zigachev static void tonga_ih_irq_disable(struct amdgpu_device *adev)
178*b843c749SSergey Zigachev {
179*b843c749SSergey Zigachev tonga_ih_disable_interrupts(adev);
180*b843c749SSergey Zigachev
181*b843c749SSergey Zigachev /* Wait and acknowledge irq */
182*b843c749SSergey Zigachev mdelay(1);
183*b843c749SSergey Zigachev }
184*b843c749SSergey Zigachev
185*b843c749SSergey Zigachev /**
186*b843c749SSergey Zigachev * tonga_ih_get_wptr - get the IH ring buffer wptr
187*b843c749SSergey Zigachev *
188*b843c749SSergey Zigachev * @adev: amdgpu_device pointer
189*b843c749SSergey Zigachev *
190*b843c749SSergey Zigachev * Get the IH ring buffer wptr from either the register
191*b843c749SSergey Zigachev * or the writeback memory buffer (VI). Also check for
192*b843c749SSergey Zigachev * ring buffer overflow and deal with it.
193*b843c749SSergey Zigachev * Used by cz_irq_process(VI).
194*b843c749SSergey Zigachev * Returns the value of the wptr.
195*b843c749SSergey Zigachev */
tonga_ih_get_wptr(struct amdgpu_device * adev)196*b843c749SSergey Zigachev static u32 tonga_ih_get_wptr(struct amdgpu_device *adev)
197*b843c749SSergey Zigachev {
198*b843c749SSergey Zigachev u32 wptr, tmp;
199*b843c749SSergey Zigachev
200*b843c749SSergey Zigachev if (adev->irq.ih.use_bus_addr)
201*b843c749SSergey Zigachev wptr = le32_to_cpu(adev->irq.ih.ring[adev->irq.ih.wptr_offs]);
202*b843c749SSergey Zigachev else
203*b843c749SSergey Zigachev wptr = le32_to_cpu(adev->wb.wb[adev->irq.ih.wptr_offs]);
204*b843c749SSergey Zigachev
205*b843c749SSergey Zigachev if (REG_GET_FIELD(wptr, IH_RB_WPTR, RB_OVERFLOW)) {
206*b843c749SSergey Zigachev wptr = REG_SET_FIELD(wptr, IH_RB_WPTR, RB_OVERFLOW, 0);
207*b843c749SSergey Zigachev /* When a ring buffer overflow happen start parsing interrupt
208*b843c749SSergey Zigachev * from the last not overwritten vector (wptr + 16). Hopefully
209*b843c749SSergey Zigachev * this should allow us to catchup.
210*b843c749SSergey Zigachev */
211*b843c749SSergey Zigachev dev_warn(adev->dev, "IH ring buffer overflow (0x%08X, 0x%08X, 0x%08X)\n",
212*b843c749SSergey Zigachev wptr, adev->irq.ih.rptr, (wptr + 16) & adev->irq.ih.ptr_mask);
213*b843c749SSergey Zigachev adev->irq.ih.rptr = (wptr + 16) & adev->irq.ih.ptr_mask;
214*b843c749SSergey Zigachev tmp = RREG32(mmIH_RB_CNTL);
215*b843c749SSergey Zigachev tmp = REG_SET_FIELD(tmp, IH_RB_CNTL, WPTR_OVERFLOW_CLEAR, 1);
216*b843c749SSergey Zigachev WREG32(mmIH_RB_CNTL, tmp);
217*b843c749SSergey Zigachev }
218*b843c749SSergey Zigachev return (wptr & adev->irq.ih.ptr_mask);
219*b843c749SSergey Zigachev }
220*b843c749SSergey Zigachev
221*b843c749SSergey Zigachev /**
222*b843c749SSergey Zigachev * tonga_ih_prescreen_iv - prescreen an interrupt vector
223*b843c749SSergey Zigachev *
224*b843c749SSergey Zigachev * @adev: amdgpu_device pointer
225*b843c749SSergey Zigachev *
226*b843c749SSergey Zigachev * Returns true if the interrupt vector should be further processed.
227*b843c749SSergey Zigachev */
tonga_ih_prescreen_iv(struct amdgpu_device * adev)228*b843c749SSergey Zigachev static bool tonga_ih_prescreen_iv(struct amdgpu_device *adev)
229*b843c749SSergey Zigachev {
230*b843c749SSergey Zigachev u32 ring_index = adev->irq.ih.rptr >> 2;
231*b843c749SSergey Zigachev u16 pasid;
232*b843c749SSergey Zigachev
233*b843c749SSergey Zigachev switch (le32_to_cpu(adev->irq.ih.ring[ring_index]) & 0xff) {
234*b843c749SSergey Zigachev case 146:
235*b843c749SSergey Zigachev case 147:
236*b843c749SSergey Zigachev pasid = le32_to_cpu(adev->irq.ih.ring[ring_index + 2]) >> 16;
237*b843c749SSergey Zigachev if (!pasid || amdgpu_vm_pasid_fault_credit(adev, pasid))
238*b843c749SSergey Zigachev return true;
239*b843c749SSergey Zigachev break;
240*b843c749SSergey Zigachev default:
241*b843c749SSergey Zigachev /* Not a VM fault */
242*b843c749SSergey Zigachev return true;
243*b843c749SSergey Zigachev }
244*b843c749SSergey Zigachev
245*b843c749SSergey Zigachev adev->irq.ih.rptr += 16;
246*b843c749SSergey Zigachev return false;
247*b843c749SSergey Zigachev }
248*b843c749SSergey Zigachev
249*b843c749SSergey Zigachev /**
250*b843c749SSergey Zigachev * tonga_ih_decode_iv - decode an interrupt vector
251*b843c749SSergey Zigachev *
252*b843c749SSergey Zigachev * @adev: amdgpu_device pointer
253*b843c749SSergey Zigachev *
254*b843c749SSergey Zigachev * Decodes the interrupt vector at the current rptr
255*b843c749SSergey Zigachev * position and also advance the position.
256*b843c749SSergey Zigachev */
tonga_ih_decode_iv(struct amdgpu_device * adev,struct amdgpu_iv_entry * entry)257*b843c749SSergey Zigachev static void tonga_ih_decode_iv(struct amdgpu_device *adev,
258*b843c749SSergey Zigachev struct amdgpu_iv_entry *entry)
259*b843c749SSergey Zigachev {
260*b843c749SSergey Zigachev /* wptr/rptr are in bytes! */
261*b843c749SSergey Zigachev u32 ring_index = adev->irq.ih.rptr >> 2;
262*b843c749SSergey Zigachev uint32_t dw[4];
263*b843c749SSergey Zigachev
264*b843c749SSergey Zigachev dw[0] = le32_to_cpu(adev->irq.ih.ring[ring_index + 0]);
265*b843c749SSergey Zigachev dw[1] = le32_to_cpu(adev->irq.ih.ring[ring_index + 1]);
266*b843c749SSergey Zigachev dw[2] = le32_to_cpu(adev->irq.ih.ring[ring_index + 2]);
267*b843c749SSergey Zigachev dw[3] = le32_to_cpu(adev->irq.ih.ring[ring_index + 3]);
268*b843c749SSergey Zigachev
269*b843c749SSergey Zigachev entry->client_id = AMDGPU_IH_CLIENTID_LEGACY;
270*b843c749SSergey Zigachev entry->src_id = dw[0] & 0xff;
271*b843c749SSergey Zigachev entry->src_data[0] = dw[1] & 0xfffffff;
272*b843c749SSergey Zigachev entry->ring_id = dw[2] & 0xff;
273*b843c749SSergey Zigachev entry->vmid = (dw[2] >> 8) & 0xff;
274*b843c749SSergey Zigachev entry->pasid = (dw[2] >> 16) & 0xffff;
275*b843c749SSergey Zigachev
276*b843c749SSergey Zigachev /* wptr/rptr are in bytes! */
277*b843c749SSergey Zigachev adev->irq.ih.rptr += 16;
278*b843c749SSergey Zigachev }
279*b843c749SSergey Zigachev
280*b843c749SSergey Zigachev /**
281*b843c749SSergey Zigachev * tonga_ih_set_rptr - set the IH ring buffer rptr
282*b843c749SSergey Zigachev *
283*b843c749SSergey Zigachev * @adev: amdgpu_device pointer
284*b843c749SSergey Zigachev *
285*b843c749SSergey Zigachev * Set the IH ring buffer rptr.
286*b843c749SSergey Zigachev */
tonga_ih_set_rptr(struct amdgpu_device * adev)287*b843c749SSergey Zigachev static void tonga_ih_set_rptr(struct amdgpu_device *adev)
288*b843c749SSergey Zigachev {
289*b843c749SSergey Zigachev if (adev->irq.ih.use_doorbell) {
290*b843c749SSergey Zigachev /* XXX check if swapping is necessary on BE */
291*b843c749SSergey Zigachev if (adev->irq.ih.use_bus_addr)
292*b843c749SSergey Zigachev adev->irq.ih.ring[adev->irq.ih.rptr_offs] = adev->irq.ih.rptr;
293*b843c749SSergey Zigachev else
294*b843c749SSergey Zigachev adev->wb.wb[adev->irq.ih.rptr_offs] = adev->irq.ih.rptr;
295*b843c749SSergey Zigachev WDOORBELL32(adev->irq.ih.doorbell_index, adev->irq.ih.rptr);
296*b843c749SSergey Zigachev } else {
297*b843c749SSergey Zigachev WREG32(mmIH_RB_RPTR, adev->irq.ih.rptr);
298*b843c749SSergey Zigachev }
299*b843c749SSergey Zigachev }
300*b843c749SSergey Zigachev
tonga_ih_early_init(void * handle)301*b843c749SSergey Zigachev static int tonga_ih_early_init(void *handle)
302*b843c749SSergey Zigachev {
303*b843c749SSergey Zigachev struct amdgpu_device *adev = (struct amdgpu_device *)handle;
304*b843c749SSergey Zigachev int ret;
305*b843c749SSergey Zigachev
306*b843c749SSergey Zigachev ret = amdgpu_irq_add_domain(adev);
307*b843c749SSergey Zigachev if (ret)
308*b843c749SSergey Zigachev return ret;
309*b843c749SSergey Zigachev
310*b843c749SSergey Zigachev tonga_ih_set_interrupt_funcs(adev);
311*b843c749SSergey Zigachev
312*b843c749SSergey Zigachev return 0;
313*b843c749SSergey Zigachev }
314*b843c749SSergey Zigachev
tonga_ih_sw_init(void * handle)315*b843c749SSergey Zigachev static int tonga_ih_sw_init(void *handle)
316*b843c749SSergey Zigachev {
317*b843c749SSergey Zigachev int r;
318*b843c749SSergey Zigachev struct amdgpu_device *adev = (struct amdgpu_device *)handle;
319*b843c749SSergey Zigachev
320*b843c749SSergey Zigachev r = amdgpu_ih_ring_init(adev, 64 * 1024, true);
321*b843c749SSergey Zigachev if (r)
322*b843c749SSergey Zigachev return r;
323*b843c749SSergey Zigachev
324*b843c749SSergey Zigachev adev->irq.ih.use_doorbell = true;
325*b843c749SSergey Zigachev adev->irq.ih.doorbell_index = AMDGPU_DOORBELL_IH;
326*b843c749SSergey Zigachev
327*b843c749SSergey Zigachev r = amdgpu_irq_init(adev);
328*b843c749SSergey Zigachev
329*b843c749SSergey Zigachev return r;
330*b843c749SSergey Zigachev }
331*b843c749SSergey Zigachev
tonga_ih_sw_fini(void * handle)332*b843c749SSergey Zigachev static int tonga_ih_sw_fini(void *handle)
333*b843c749SSergey Zigachev {
334*b843c749SSergey Zigachev struct amdgpu_device *adev = (struct amdgpu_device *)handle;
335*b843c749SSergey Zigachev
336*b843c749SSergey Zigachev amdgpu_irq_fini(adev);
337*b843c749SSergey Zigachev amdgpu_ih_ring_fini(adev);
338*b843c749SSergey Zigachev amdgpu_irq_remove_domain(adev);
339*b843c749SSergey Zigachev
340*b843c749SSergey Zigachev return 0;
341*b843c749SSergey Zigachev }
342*b843c749SSergey Zigachev
tonga_ih_hw_init(void * handle)343*b843c749SSergey Zigachev static int tonga_ih_hw_init(void *handle)
344*b843c749SSergey Zigachev {
345*b843c749SSergey Zigachev int r;
346*b843c749SSergey Zigachev struct amdgpu_device *adev = (struct amdgpu_device *)handle;
347*b843c749SSergey Zigachev
348*b843c749SSergey Zigachev r = tonga_ih_irq_init(adev);
349*b843c749SSergey Zigachev if (r)
350*b843c749SSergey Zigachev return r;
351*b843c749SSergey Zigachev
352*b843c749SSergey Zigachev return 0;
353*b843c749SSergey Zigachev }
354*b843c749SSergey Zigachev
tonga_ih_hw_fini(void * handle)355*b843c749SSergey Zigachev static int tonga_ih_hw_fini(void *handle)
356*b843c749SSergey Zigachev {
357*b843c749SSergey Zigachev struct amdgpu_device *adev = (struct amdgpu_device *)handle;
358*b843c749SSergey Zigachev
359*b843c749SSergey Zigachev tonga_ih_irq_disable(adev);
360*b843c749SSergey Zigachev
361*b843c749SSergey Zigachev return 0;
362*b843c749SSergey Zigachev }
363*b843c749SSergey Zigachev
tonga_ih_suspend(void * handle)364*b843c749SSergey Zigachev static int tonga_ih_suspend(void *handle)
365*b843c749SSergey Zigachev {
366*b843c749SSergey Zigachev struct amdgpu_device *adev = (struct amdgpu_device *)handle;
367*b843c749SSergey Zigachev
368*b843c749SSergey Zigachev return tonga_ih_hw_fini(adev);
369*b843c749SSergey Zigachev }
370*b843c749SSergey Zigachev
tonga_ih_resume(void * handle)371*b843c749SSergey Zigachev static int tonga_ih_resume(void *handle)
372*b843c749SSergey Zigachev {
373*b843c749SSergey Zigachev struct amdgpu_device *adev = (struct amdgpu_device *)handle;
374*b843c749SSergey Zigachev
375*b843c749SSergey Zigachev return tonga_ih_hw_init(adev);
376*b843c749SSergey Zigachev }
377*b843c749SSergey Zigachev
tonga_ih_is_idle(void * handle)378*b843c749SSergey Zigachev static bool tonga_ih_is_idle(void *handle)
379*b843c749SSergey Zigachev {
380*b843c749SSergey Zigachev struct amdgpu_device *adev = (struct amdgpu_device *)handle;
381*b843c749SSergey Zigachev u32 tmp = RREG32(mmSRBM_STATUS);
382*b843c749SSergey Zigachev
383*b843c749SSergey Zigachev if (REG_GET_FIELD(tmp, SRBM_STATUS, IH_BUSY))
384*b843c749SSergey Zigachev return false;
385*b843c749SSergey Zigachev
386*b843c749SSergey Zigachev return true;
387*b843c749SSergey Zigachev }
388*b843c749SSergey Zigachev
tonga_ih_wait_for_idle(void * handle)389*b843c749SSergey Zigachev static int tonga_ih_wait_for_idle(void *handle)
390*b843c749SSergey Zigachev {
391*b843c749SSergey Zigachev unsigned i;
392*b843c749SSergey Zigachev u32 tmp;
393*b843c749SSergey Zigachev struct amdgpu_device *adev = (struct amdgpu_device *)handle;
394*b843c749SSergey Zigachev
395*b843c749SSergey Zigachev for (i = 0; i < adev->usec_timeout; i++) {
396*b843c749SSergey Zigachev /* read MC_STATUS */
397*b843c749SSergey Zigachev tmp = RREG32(mmSRBM_STATUS);
398*b843c749SSergey Zigachev if (!REG_GET_FIELD(tmp, SRBM_STATUS, IH_BUSY))
399*b843c749SSergey Zigachev return 0;
400*b843c749SSergey Zigachev udelay(1);
401*b843c749SSergey Zigachev }
402*b843c749SSergey Zigachev return -ETIMEDOUT;
403*b843c749SSergey Zigachev }
404*b843c749SSergey Zigachev
tonga_ih_check_soft_reset(void * handle)405*b843c749SSergey Zigachev static bool tonga_ih_check_soft_reset(void *handle)
406*b843c749SSergey Zigachev {
407*b843c749SSergey Zigachev struct amdgpu_device *adev = (struct amdgpu_device *)handle;
408*b843c749SSergey Zigachev u32 srbm_soft_reset = 0;
409*b843c749SSergey Zigachev u32 tmp = RREG32(mmSRBM_STATUS);
410*b843c749SSergey Zigachev
411*b843c749SSergey Zigachev if (tmp & SRBM_STATUS__IH_BUSY_MASK)
412*b843c749SSergey Zigachev srbm_soft_reset = REG_SET_FIELD(srbm_soft_reset, SRBM_SOFT_RESET,
413*b843c749SSergey Zigachev SOFT_RESET_IH, 1);
414*b843c749SSergey Zigachev
415*b843c749SSergey Zigachev if (srbm_soft_reset) {
416*b843c749SSergey Zigachev adev->irq.srbm_soft_reset = srbm_soft_reset;
417*b843c749SSergey Zigachev return true;
418*b843c749SSergey Zigachev } else {
419*b843c749SSergey Zigachev adev->irq.srbm_soft_reset = 0;
420*b843c749SSergey Zigachev return false;
421*b843c749SSergey Zigachev }
422*b843c749SSergey Zigachev }
423*b843c749SSergey Zigachev
tonga_ih_pre_soft_reset(void * handle)424*b843c749SSergey Zigachev static int tonga_ih_pre_soft_reset(void *handle)
425*b843c749SSergey Zigachev {
426*b843c749SSergey Zigachev struct amdgpu_device *adev = (struct amdgpu_device *)handle;
427*b843c749SSergey Zigachev
428*b843c749SSergey Zigachev if (!adev->irq.srbm_soft_reset)
429*b843c749SSergey Zigachev return 0;
430*b843c749SSergey Zigachev
431*b843c749SSergey Zigachev return tonga_ih_hw_fini(adev);
432*b843c749SSergey Zigachev }
433*b843c749SSergey Zigachev
tonga_ih_post_soft_reset(void * handle)434*b843c749SSergey Zigachev static int tonga_ih_post_soft_reset(void *handle)
435*b843c749SSergey Zigachev {
436*b843c749SSergey Zigachev struct amdgpu_device *adev = (struct amdgpu_device *)handle;
437*b843c749SSergey Zigachev
438*b843c749SSergey Zigachev if (!adev->irq.srbm_soft_reset)
439*b843c749SSergey Zigachev return 0;
440*b843c749SSergey Zigachev
441*b843c749SSergey Zigachev return tonga_ih_hw_init(adev);
442*b843c749SSergey Zigachev }
443*b843c749SSergey Zigachev
tonga_ih_soft_reset(void * handle)444*b843c749SSergey Zigachev static int tonga_ih_soft_reset(void *handle)
445*b843c749SSergey Zigachev {
446*b843c749SSergey Zigachev struct amdgpu_device *adev = (struct amdgpu_device *)handle;
447*b843c749SSergey Zigachev u32 srbm_soft_reset;
448*b843c749SSergey Zigachev
449*b843c749SSergey Zigachev if (!adev->irq.srbm_soft_reset)
450*b843c749SSergey Zigachev return 0;
451*b843c749SSergey Zigachev srbm_soft_reset = adev->irq.srbm_soft_reset;
452*b843c749SSergey Zigachev
453*b843c749SSergey Zigachev if (srbm_soft_reset) {
454*b843c749SSergey Zigachev u32 tmp;
455*b843c749SSergey Zigachev
456*b843c749SSergey Zigachev tmp = RREG32(mmSRBM_SOFT_RESET);
457*b843c749SSergey Zigachev tmp |= srbm_soft_reset;
458*b843c749SSergey Zigachev dev_info(adev->dev, "SRBM_SOFT_RESET=0x%08X\n", tmp);
459*b843c749SSergey Zigachev WREG32(mmSRBM_SOFT_RESET, tmp);
460*b843c749SSergey Zigachev tmp = RREG32(mmSRBM_SOFT_RESET);
461*b843c749SSergey Zigachev
462*b843c749SSergey Zigachev udelay(50);
463*b843c749SSergey Zigachev
464*b843c749SSergey Zigachev tmp &= ~srbm_soft_reset;
465*b843c749SSergey Zigachev WREG32(mmSRBM_SOFT_RESET, tmp);
466*b843c749SSergey Zigachev tmp = RREG32(mmSRBM_SOFT_RESET);
467*b843c749SSergey Zigachev
468*b843c749SSergey Zigachev /* Wait a little for things to settle down */
469*b843c749SSergey Zigachev udelay(50);
470*b843c749SSergey Zigachev }
471*b843c749SSergey Zigachev
472*b843c749SSergey Zigachev return 0;
473*b843c749SSergey Zigachev }
474*b843c749SSergey Zigachev
tonga_ih_set_clockgating_state(void * handle,enum amd_clockgating_state state)475*b843c749SSergey Zigachev static int tonga_ih_set_clockgating_state(void *handle,
476*b843c749SSergey Zigachev enum amd_clockgating_state state)
477*b843c749SSergey Zigachev {
478*b843c749SSergey Zigachev return 0;
479*b843c749SSergey Zigachev }
480*b843c749SSergey Zigachev
tonga_ih_set_powergating_state(void * handle,enum amd_powergating_state state)481*b843c749SSergey Zigachev static int tonga_ih_set_powergating_state(void *handle,
482*b843c749SSergey Zigachev enum amd_powergating_state state)
483*b843c749SSergey Zigachev {
484*b843c749SSergey Zigachev return 0;
485*b843c749SSergey Zigachev }
486*b843c749SSergey Zigachev
487*b843c749SSergey Zigachev static const struct amd_ip_funcs tonga_ih_ip_funcs = {
488*b843c749SSergey Zigachev .name = "tonga_ih",
489*b843c749SSergey Zigachev .early_init = tonga_ih_early_init,
490*b843c749SSergey Zigachev .late_init = NULL,
491*b843c749SSergey Zigachev .sw_init = tonga_ih_sw_init,
492*b843c749SSergey Zigachev .sw_fini = tonga_ih_sw_fini,
493*b843c749SSergey Zigachev .hw_init = tonga_ih_hw_init,
494*b843c749SSergey Zigachev .hw_fini = tonga_ih_hw_fini,
495*b843c749SSergey Zigachev .suspend = tonga_ih_suspend,
496*b843c749SSergey Zigachev .resume = tonga_ih_resume,
497*b843c749SSergey Zigachev .is_idle = tonga_ih_is_idle,
498*b843c749SSergey Zigachev .wait_for_idle = tonga_ih_wait_for_idle,
499*b843c749SSergey Zigachev .check_soft_reset = tonga_ih_check_soft_reset,
500*b843c749SSergey Zigachev .pre_soft_reset = tonga_ih_pre_soft_reset,
501*b843c749SSergey Zigachev .soft_reset = tonga_ih_soft_reset,
502*b843c749SSergey Zigachev .post_soft_reset = tonga_ih_post_soft_reset,
503*b843c749SSergey Zigachev .set_clockgating_state = tonga_ih_set_clockgating_state,
504*b843c749SSergey Zigachev .set_powergating_state = tonga_ih_set_powergating_state,
505*b843c749SSergey Zigachev };
506*b843c749SSergey Zigachev
507*b843c749SSergey Zigachev static const struct amdgpu_ih_funcs tonga_ih_funcs = {
508*b843c749SSergey Zigachev .get_wptr = tonga_ih_get_wptr,
509*b843c749SSergey Zigachev .prescreen_iv = tonga_ih_prescreen_iv,
510*b843c749SSergey Zigachev .decode_iv = tonga_ih_decode_iv,
511*b843c749SSergey Zigachev .set_rptr = tonga_ih_set_rptr
512*b843c749SSergey Zigachev };
513*b843c749SSergey Zigachev
tonga_ih_set_interrupt_funcs(struct amdgpu_device * adev)514*b843c749SSergey Zigachev static void tonga_ih_set_interrupt_funcs(struct amdgpu_device *adev)
515*b843c749SSergey Zigachev {
516*b843c749SSergey Zigachev if (adev->irq.ih_funcs == NULL)
517*b843c749SSergey Zigachev adev->irq.ih_funcs = &tonga_ih_funcs;
518*b843c749SSergey Zigachev }
519*b843c749SSergey Zigachev
520*b843c749SSergey Zigachev const struct amdgpu_ip_block_version tonga_ih_ip_block =
521*b843c749SSergey Zigachev {
522*b843c749SSergey Zigachev .type = AMD_IP_BLOCK_TYPE_IH,
523*b843c749SSergey Zigachev .major = 3,
524*b843c749SSergey Zigachev .minor = 0,
525*b843c749SSergey Zigachev .rev = 0,
526*b843c749SSergey Zigachev .funcs = &tonga_ih_ip_funcs,
527*b843c749SSergey Zigachev };
528