xref: /openbmc/linux/drivers/gpu/drm/amd/amdgpu/tonga_ih.c (revision c595db6d7c8bcf87ef42204391fa890e5950e566)
1aaa36a97SAlex Deucher /*
2aaa36a97SAlex Deucher  * Copyright 2014 Advanced Micro Devices, Inc.
3aaa36a97SAlex Deucher  *
4aaa36a97SAlex Deucher  * Permission is hereby granted, free of charge, to any person obtaining a
5aaa36a97SAlex Deucher  * copy of this software and associated documentation files (the "Software"),
6aaa36a97SAlex Deucher  * to deal in the Software without restriction, including without limitation
7aaa36a97SAlex Deucher  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8aaa36a97SAlex Deucher  * and/or sell copies of the Software, and to permit persons to whom the
9aaa36a97SAlex Deucher  * Software is furnished to do so, subject to the following conditions:
10aaa36a97SAlex Deucher  *
11aaa36a97SAlex Deucher  * The above copyright notice and this permission notice shall be included in
12aaa36a97SAlex Deucher  * all copies or substantial portions of the Software.
13aaa36a97SAlex Deucher  *
14aaa36a97SAlex Deucher  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15aaa36a97SAlex Deucher  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16aaa36a97SAlex Deucher  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17aaa36a97SAlex Deucher  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18aaa36a97SAlex Deucher  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19aaa36a97SAlex Deucher  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20aaa36a97SAlex Deucher  * OTHER DEALINGS IN THE SOFTWARE.
21aaa36a97SAlex Deucher  *
22aaa36a97SAlex Deucher  */
2347b757fbSSam Ravnborg 
2447b757fbSSam Ravnborg #include <linux/pci.h>
2547b757fbSSam Ravnborg 
26aaa36a97SAlex Deucher #include "amdgpu.h"
27aaa36a97SAlex Deucher #include "amdgpu_ih.h"
28aaa36a97SAlex Deucher #include "vid.h"
29aaa36a97SAlex Deucher 
30aaa36a97SAlex Deucher #include "oss/oss_3_0_d.h"
31aaa36a97SAlex Deucher #include "oss/oss_3_0_sh_mask.h"
32aaa36a97SAlex Deucher 
33aaa36a97SAlex Deucher #include "bif/bif_5_1_d.h"
34aaa36a97SAlex Deucher #include "bif/bif_5_1_sh_mask.h"
35aaa36a97SAlex Deucher 
36aaa36a97SAlex Deucher /*
37aaa36a97SAlex Deucher  * Interrupts
38aaa36a97SAlex Deucher  * Starting with r6xx, interrupts are handled via a ring buffer.
39aaa36a97SAlex Deucher  * Ring buffers are areas of GPU accessible memory that the GPU
40aaa36a97SAlex Deucher  * writes interrupt vectors into and the host reads vectors out of.
41aaa36a97SAlex Deucher  * There is a rptr (read pointer) that determines where the
42aaa36a97SAlex Deucher  * host is currently reading, and a wptr (write pointer)
43aaa36a97SAlex Deucher  * which determines where the GPU has written.  When the
44aaa36a97SAlex Deucher  * pointers are equal, the ring is idle.  When the GPU
45aaa36a97SAlex Deucher  * writes vectors to the ring buffer, it increments the
46aaa36a97SAlex Deucher  * wptr.  When there is an interrupt, the host then starts
47aaa36a97SAlex Deucher  * fetching commands and processing them until the pointers are
48aaa36a97SAlex Deucher  * equal again at which point it updates the rptr.
49aaa36a97SAlex Deucher  */
50aaa36a97SAlex Deucher 
51aaa36a97SAlex Deucher static void tonga_ih_set_interrupt_funcs(struct amdgpu_device *adev);
52aaa36a97SAlex Deucher 
53aaa36a97SAlex Deucher /**
54aaa36a97SAlex Deucher  * tonga_ih_enable_interrupts - Enable the interrupt ring buffer
55aaa36a97SAlex Deucher  *
56aaa36a97SAlex Deucher  * @adev: amdgpu_device pointer
57aaa36a97SAlex Deucher  *
58aaa36a97SAlex Deucher  * Enable the interrupt ring buffer (VI).
59aaa36a97SAlex Deucher  */
tonga_ih_enable_interrupts(struct amdgpu_device * adev)60aaa36a97SAlex Deucher static void tonga_ih_enable_interrupts(struct amdgpu_device *adev)
61aaa36a97SAlex Deucher {
62aaa36a97SAlex Deucher 	u32 ih_rb_cntl = RREG32(mmIH_RB_CNTL);
63aaa36a97SAlex Deucher 
64aaa36a97SAlex Deucher 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, RB_ENABLE, 1);
65aaa36a97SAlex Deucher 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, ENABLE_INTR, 1);
66aaa36a97SAlex Deucher 	WREG32(mmIH_RB_CNTL, ih_rb_cntl);
67aaa36a97SAlex Deucher 	adev->irq.ih.enabled = true;
68aaa36a97SAlex Deucher }
69aaa36a97SAlex Deucher 
70aaa36a97SAlex Deucher /**
71aaa36a97SAlex Deucher  * tonga_ih_disable_interrupts - Disable the interrupt ring buffer
72aaa36a97SAlex Deucher  *
73aaa36a97SAlex Deucher  * @adev: amdgpu_device pointer
74aaa36a97SAlex Deucher  *
75aaa36a97SAlex Deucher  * Disable the interrupt ring buffer (VI).
76aaa36a97SAlex Deucher  */
tonga_ih_disable_interrupts(struct amdgpu_device * adev)77aaa36a97SAlex Deucher static void tonga_ih_disable_interrupts(struct amdgpu_device *adev)
78aaa36a97SAlex Deucher {
79aaa36a97SAlex Deucher 	u32 ih_rb_cntl = RREG32(mmIH_RB_CNTL);
80aaa36a97SAlex Deucher 
81aaa36a97SAlex Deucher 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, RB_ENABLE, 0);
82aaa36a97SAlex Deucher 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, ENABLE_INTR, 0);
83aaa36a97SAlex Deucher 	WREG32(mmIH_RB_CNTL, ih_rb_cntl);
84aaa36a97SAlex Deucher 	/* set rptr, wptr to 0 */
85aaa36a97SAlex Deucher 	WREG32(mmIH_RB_RPTR, 0);
86aaa36a97SAlex Deucher 	WREG32(mmIH_RB_WPTR, 0);
87aaa36a97SAlex Deucher 	adev->irq.ih.enabled = false;
88aaa36a97SAlex Deucher 	adev->irq.ih.rptr = 0;
89aaa36a97SAlex Deucher }
90aaa36a97SAlex Deucher 
91aaa36a97SAlex Deucher /**
92aaa36a97SAlex Deucher  * tonga_ih_irq_init - init and enable the interrupt ring
93aaa36a97SAlex Deucher  *
94aaa36a97SAlex Deucher  * @adev: amdgpu_device pointer
95aaa36a97SAlex Deucher  *
96aaa36a97SAlex Deucher  * Allocate a ring buffer for the interrupt controller,
97aaa36a97SAlex Deucher  * enable the RLC, disable interrupts, enable the IH
98aaa36a97SAlex Deucher  * ring buffer and enable it (VI).
99aaa36a97SAlex Deucher  * Called at device load and reume.
100aaa36a97SAlex Deucher  * Returns 0 for success, errors for failure.
101aaa36a97SAlex Deucher  */
tonga_ih_irq_init(struct amdgpu_device * adev)102aaa36a97SAlex Deucher static int tonga_ih_irq_init(struct amdgpu_device *adev)
103aaa36a97SAlex Deucher {
104aaa36a97SAlex Deucher 	u32 interrupt_cntl, ih_rb_cntl, ih_doorbell_rtpr;
105d81f78b4SChristian König 	struct amdgpu_ih_ring *ih = &adev->irq.ih;
106d81f78b4SChristian König 	int rb_bufsz;
107aaa36a97SAlex Deucher 
108aaa36a97SAlex Deucher 	/* disable irqs */
109aaa36a97SAlex Deucher 	tonga_ih_disable_interrupts(adev);
110aaa36a97SAlex Deucher 
111aaa36a97SAlex Deucher 	/* setup interrupt control */
11292e71b06SChristian König 	WREG32(mmINTERRUPT_CNTL2, adev->dummy_page_addr >> 8);
113aaa36a97SAlex Deucher 	interrupt_cntl = RREG32(mmINTERRUPT_CNTL);
114aaa36a97SAlex Deucher 	/* INTERRUPT_CNTL__IH_DUMMY_RD_OVERRIDE_MASK=0 - dummy read disabled with msi, enabled without msi
115aaa36a97SAlex Deucher 	 * INTERRUPT_CNTL__IH_DUMMY_RD_OVERRIDE_MASK=1 - dummy read controlled by IH_DUMMY_RD_EN
116aaa36a97SAlex Deucher 	 */
117aaa36a97SAlex Deucher 	interrupt_cntl = REG_SET_FIELD(interrupt_cntl, INTERRUPT_CNTL, IH_DUMMY_RD_OVERRIDE, 0);
118aaa36a97SAlex Deucher 	/* INTERRUPT_CNTL__IH_REQ_NONSNOOP_EN_MASK=1 if ring is in non-cacheable memory, e.g., vram */
119aaa36a97SAlex Deucher 	interrupt_cntl = REG_SET_FIELD(interrupt_cntl, INTERRUPT_CNTL, IH_REQ_NONSNOOP_EN, 0);
120aaa36a97SAlex Deucher 	WREG32(mmINTERRUPT_CNTL, interrupt_cntl);
121aaa36a97SAlex Deucher 
122aaa36a97SAlex Deucher 	/* Ring Buffer base. [39:8] of 40-bit address of the beginning of the ring buffer*/
123d81f78b4SChristian König 	WREG32(mmIH_RB_BASE, ih->gpu_addr >> 8);
124aaa36a97SAlex Deucher 
125aaa36a97SAlex Deucher 	rb_bufsz = order_base_2(adev->irq.ih.ring_size / 4);
126aaa36a97SAlex Deucher 	ih_rb_cntl = REG_SET_FIELD(0, IH_RB_CNTL, WPTR_OVERFLOW_CLEAR, 1);
127aaa36a97SAlex Deucher 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, RB_SIZE, rb_bufsz);
128aaa36a97SAlex Deucher 	/* Ring Buffer write pointer writeback. If enabled, IH_RB_WPTR register value is written to memory */
129aaa36a97SAlex Deucher 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, WPTR_WRITEBACK_ENABLE, 1);
130aaa36a97SAlex Deucher 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, MC_VMID, 0);
131aaa36a97SAlex Deucher 
132aaa36a97SAlex Deucher 	if (adev->irq.msi_enabled)
133aaa36a97SAlex Deucher 		ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, RPTR_REARM, 1);
134aaa36a97SAlex Deucher 
135aaa36a97SAlex Deucher 	WREG32(mmIH_RB_CNTL, ih_rb_cntl);
136aaa36a97SAlex Deucher 
137aaa36a97SAlex Deucher 	/* set the writeback address whether it's enabled or not */
138d81f78b4SChristian König 	WREG32(mmIH_RB_WPTR_ADDR_LO, lower_32_bits(ih->wptr_addr));
139d81f78b4SChristian König 	WREG32(mmIH_RB_WPTR_ADDR_HI, upper_32_bits(ih->wptr_addr) & 0xFF);
140aaa36a97SAlex Deucher 
141aaa36a97SAlex Deucher 	/* set rptr, wptr to 0 */
142aaa36a97SAlex Deucher 	WREG32(mmIH_RB_RPTR, 0);
143aaa36a97SAlex Deucher 	WREG32(mmIH_RB_WPTR, 0);
144aaa36a97SAlex Deucher 
145aaa36a97SAlex Deucher 	ih_doorbell_rtpr = RREG32(mmIH_DOORBELL_RPTR);
146aaa36a97SAlex Deucher 	if (adev->irq.ih.use_doorbell) {
147aaa36a97SAlex Deucher 		ih_doorbell_rtpr = REG_SET_FIELD(ih_doorbell_rtpr, IH_DOORBELL_RPTR,
148aaa36a97SAlex Deucher 						 OFFSET, adev->irq.ih.doorbell_index);
149aaa36a97SAlex Deucher 		ih_doorbell_rtpr = REG_SET_FIELD(ih_doorbell_rtpr, IH_DOORBELL_RPTR,
150aaa36a97SAlex Deucher 						 ENABLE, 1);
151aaa36a97SAlex Deucher 	} else {
152aaa36a97SAlex Deucher 		ih_doorbell_rtpr = REG_SET_FIELD(ih_doorbell_rtpr, IH_DOORBELL_RPTR,
153aaa36a97SAlex Deucher 						 ENABLE, 0);
154aaa36a97SAlex Deucher 	}
155aaa36a97SAlex Deucher 	WREG32(mmIH_DOORBELL_RPTR, ih_doorbell_rtpr);
156aaa36a97SAlex Deucher 
157aaa36a97SAlex Deucher 	pci_set_master(adev->pdev);
158aaa36a97SAlex Deucher 
159aaa36a97SAlex Deucher 	/* enable interrupts */
160aaa36a97SAlex Deucher 	tonga_ih_enable_interrupts(adev);
161aaa36a97SAlex Deucher 
1622f46b2a5SMuhammad Falak R Wani 	return 0;
163aaa36a97SAlex Deucher }
164aaa36a97SAlex Deucher 
165aaa36a97SAlex Deucher /**
166aaa36a97SAlex Deucher  * tonga_ih_irq_disable - disable interrupts
167aaa36a97SAlex Deucher  *
168aaa36a97SAlex Deucher  * @adev: amdgpu_device pointer
169aaa36a97SAlex Deucher  *
170aaa36a97SAlex Deucher  * Disable interrupts on the hw (VI).
171aaa36a97SAlex Deucher  */
tonga_ih_irq_disable(struct amdgpu_device * adev)172aaa36a97SAlex Deucher static void tonga_ih_irq_disable(struct amdgpu_device *adev)
173aaa36a97SAlex Deucher {
174aaa36a97SAlex Deucher 	tonga_ih_disable_interrupts(adev);
175aaa36a97SAlex Deucher 
176aaa36a97SAlex Deucher 	/* Wait and acknowledge irq */
177aaa36a97SAlex Deucher 	mdelay(1);
178aaa36a97SAlex Deucher }
179aaa36a97SAlex Deucher 
180aaa36a97SAlex Deucher /**
181aaa36a97SAlex Deucher  * tonga_ih_get_wptr - get the IH ring buffer wptr
182aaa36a97SAlex Deucher  *
183aaa36a97SAlex Deucher  * @adev: amdgpu_device pointer
18439902109SLee Jones  * @ih: IH ring buffer to fetch wptr
185aaa36a97SAlex Deucher  *
186aaa36a97SAlex Deucher  * Get the IH ring buffer wptr from either the register
187aaa36a97SAlex Deucher  * or the writeback memory buffer (VI).  Also check for
188aaa36a97SAlex Deucher  * ring buffer overflow and deal with it.
189aaa36a97SAlex Deucher  * Used by cz_irq_process(VI).
190aaa36a97SAlex Deucher  * Returns the value of the wptr.
191aaa36a97SAlex Deucher  */
tonga_ih_get_wptr(struct amdgpu_device * adev,struct amdgpu_ih_ring * ih)1928bb9eb48SChristian König static u32 tonga_ih_get_wptr(struct amdgpu_device *adev,
1938bb9eb48SChristian König 			     struct amdgpu_ih_ring *ih)
194aaa36a97SAlex Deucher {
195aaa36a97SAlex Deucher 	u32 wptr, tmp;
196aaa36a97SAlex Deucher 
197d81f78b4SChristian König 	wptr = le32_to_cpu(*ih->wptr_cpu);
198aaa36a97SAlex Deucher 
199e4180c42SDefang Bo 	if (!REG_GET_FIELD(wptr, IH_RB_WPTR, RB_OVERFLOW))
200e4180c42SDefang Bo 		goto out;
201e4180c42SDefang Bo 
202e4180c42SDefang Bo 	/* Double check that the overflow wasn't already cleared. */
203e4180c42SDefang Bo 	wptr = RREG32(mmIH_RB_WPTR);
204e4180c42SDefang Bo 
205e4180c42SDefang Bo 	if (!REG_GET_FIELD(wptr, IH_RB_WPTR, RB_OVERFLOW))
206e4180c42SDefang Bo 		goto out;
207e4180c42SDefang Bo 
208aaa36a97SAlex Deucher 	wptr = REG_SET_FIELD(wptr, IH_RB_WPTR, RB_OVERFLOW, 0);
209e4180c42SDefang Bo 
210aaa36a97SAlex Deucher 	/* When a ring buffer overflow happen start parsing interrupt
211aaa36a97SAlex Deucher 	 * from the last not overwritten vector (wptr + 16). Hopefully
212aaa36a97SAlex Deucher 	 * this should allow us to catchup.
213aaa36a97SAlex Deucher 	 */
214e4180c42SDefang Bo 
215aaa36a97SAlex Deucher 	dev_warn(adev->dev, "IH ring buffer overflow (0x%08X, 0x%08X, 0x%08X)\n",
2168bb9eb48SChristian König 		wptr, ih->rptr, (wptr + 16) & ih->ptr_mask);
2178bb9eb48SChristian König 	ih->rptr = (wptr + 16) & ih->ptr_mask;
218aaa36a97SAlex Deucher 	tmp = RREG32(mmIH_RB_CNTL);
219aaa36a97SAlex Deucher 	tmp = REG_SET_FIELD(tmp, IH_RB_CNTL, WPTR_OVERFLOW_CLEAR, 1);
220aaa36a97SAlex Deucher 	WREG32(mmIH_RB_CNTL, tmp);
221e4180c42SDefang Bo 
222*89833979SFriedrich Vock 	/* Unset the CLEAR_OVERFLOW bit immediately so new overflows
223*89833979SFriedrich Vock 	 * can be detected.
224*89833979SFriedrich Vock 	 */
225*89833979SFriedrich Vock 	tmp = REG_SET_FIELD(tmp, IH_RB_CNTL, WPTR_OVERFLOW_CLEAR, 0);
226*89833979SFriedrich Vock 	WREG32(mmIH_RB_CNTL, tmp);
227*89833979SFriedrich Vock 
228e4180c42SDefang Bo out:
2298bb9eb48SChristian König 	return (wptr & ih->ptr_mask);
230aaa36a97SAlex Deucher }
231aaa36a97SAlex Deucher 
232aaa36a97SAlex Deucher /**
233aaa36a97SAlex Deucher  * tonga_ih_decode_iv - decode an interrupt vector
234aaa36a97SAlex Deucher  *
235aaa36a97SAlex Deucher  * @adev: amdgpu_device pointer
23639902109SLee Jones  * @ih: IH ring buffer to decode
23739902109SLee Jones  * @entry: IV entry to place decoded information into
238aaa36a97SAlex Deucher  *
239aaa36a97SAlex Deucher  * Decodes the interrupt vector at the current rptr
240aaa36a97SAlex Deucher  * position and also advance the position.
241aaa36a97SAlex Deucher  */
tonga_ih_decode_iv(struct amdgpu_device * adev,struct amdgpu_ih_ring * ih,struct amdgpu_iv_entry * entry)242aaa36a97SAlex Deucher static void tonga_ih_decode_iv(struct amdgpu_device *adev,
2438bb9eb48SChristian König 			       struct amdgpu_ih_ring *ih,
244aaa36a97SAlex Deucher 			       struct amdgpu_iv_entry *entry)
245aaa36a97SAlex Deucher {
246aaa36a97SAlex Deucher 	/* wptr/rptr are in bytes! */
2478bb9eb48SChristian König 	u32 ring_index = ih->rptr >> 2;
248aaa36a97SAlex Deucher 	uint32_t dw[4];
249aaa36a97SAlex Deucher 
2508bb9eb48SChristian König 	dw[0] = le32_to_cpu(ih->ring[ring_index + 0]);
2518bb9eb48SChristian König 	dw[1] = le32_to_cpu(ih->ring[ring_index + 1]);
2528bb9eb48SChristian König 	dw[2] = le32_to_cpu(ih->ring[ring_index + 2]);
2538bb9eb48SChristian König 	dw[3] = le32_to_cpu(ih->ring[ring_index + 3]);
254aaa36a97SAlex Deucher 
2551ffdeca6SChristian König 	entry->client_id = AMDGPU_IRQ_CLIENTID_LEGACY;
256aaa36a97SAlex Deucher 	entry->src_id = dw[0] & 0xff;
2577ccf5aa8SAlex Deucher 	entry->src_data[0] = dw[1] & 0xfffffff;
258aaa36a97SAlex Deucher 	entry->ring_id = dw[2] & 0xff;
259c4f46f22SChristian König 	entry->vmid = (dw[2] >> 8) & 0xff;
2603816e42fSChristian König 	entry->pasid = (dw[2] >> 16) & 0xffff;
261aaa36a97SAlex Deucher 
262aaa36a97SAlex Deucher 	/* wptr/rptr are in bytes! */
2638bb9eb48SChristian König 	ih->rptr += 16;
264aaa36a97SAlex Deucher }
265aaa36a97SAlex Deucher 
266aaa36a97SAlex Deucher /**
267aaa36a97SAlex Deucher  * tonga_ih_set_rptr - set the IH ring buffer rptr
268aaa36a97SAlex Deucher  *
269aaa36a97SAlex Deucher  * @adev: amdgpu_device pointer
27039902109SLee Jones  * @ih: IH ring buffer to set rptr
271aaa36a97SAlex Deucher  *
272aaa36a97SAlex Deucher  * Set the IH ring buffer rptr.
273aaa36a97SAlex Deucher  */
tonga_ih_set_rptr(struct amdgpu_device * adev,struct amdgpu_ih_ring * ih)2748bb9eb48SChristian König static void tonga_ih_set_rptr(struct amdgpu_device *adev,
2758bb9eb48SChristian König 			      struct amdgpu_ih_ring *ih)
276aaa36a97SAlex Deucher {
2778bb9eb48SChristian König 	if (ih->use_doorbell) {
278aaa36a97SAlex Deucher 		/* XXX check if swapping is necessary on BE */
279d81f78b4SChristian König 		*ih->rptr_cpu = ih->rptr;
2808bb9eb48SChristian König 		WDOORBELL32(ih->doorbell_index, ih->rptr);
281aaa36a97SAlex Deucher 	} else {
2828bb9eb48SChristian König 		WREG32(mmIH_RB_RPTR, ih->rptr);
283aaa36a97SAlex Deucher 	}
284aaa36a97SAlex Deucher }
285aaa36a97SAlex Deucher 
tonga_ih_early_init(void * handle)2865fc3aeebSyanyang1 static int tonga_ih_early_init(void *handle)
287aaa36a97SAlex Deucher {
2885fc3aeebSyanyang1 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2895f232365SAlex Deucher 	int ret;
2905f232365SAlex Deucher 
2915f232365SAlex Deucher 	ret = amdgpu_irq_add_domain(adev);
2925f232365SAlex Deucher 	if (ret)
2935f232365SAlex Deucher 		return ret;
2945fc3aeebSyanyang1 
295aaa36a97SAlex Deucher 	tonga_ih_set_interrupt_funcs(adev);
2965f232365SAlex Deucher 
297aaa36a97SAlex Deucher 	return 0;
298aaa36a97SAlex Deucher }
299aaa36a97SAlex Deucher 
tonga_ih_sw_init(void * handle)3005fc3aeebSyanyang1 static int tonga_ih_sw_init(void *handle)
301aaa36a97SAlex Deucher {
302aaa36a97SAlex Deucher 	int r;
3035fc3aeebSyanyang1 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
304aaa36a97SAlex Deucher 
305425c3143SChristian König 	r = amdgpu_ih_ring_init(adev, &adev->irq.ih, 64 * 1024, true);
306aaa36a97SAlex Deucher 	if (r)
307aaa36a97SAlex Deucher 		return r;
308aaa36a97SAlex Deucher 
309aaa36a97SAlex Deucher 	adev->irq.ih.use_doorbell = true;
3109564f192SOak Zeng 	adev->irq.ih.doorbell_index = adev->doorbell_index.ih;
311aaa36a97SAlex Deucher 
312aaa36a97SAlex Deucher 	r = amdgpu_irq_init(adev);
313aaa36a97SAlex Deucher 
314aaa36a97SAlex Deucher 	return r;
315aaa36a97SAlex Deucher }
316aaa36a97SAlex Deucher 
tonga_ih_sw_fini(void * handle)3175fc3aeebSyanyang1 static int tonga_ih_sw_fini(void *handle)
318aaa36a97SAlex Deucher {
3195fc3aeebSyanyang1 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3205fc3aeebSyanyang1 
32172c8c97bSAndrey Grodzovsky 	amdgpu_irq_fini_sw(adev);
322303f551cSJunwei Zhang 	amdgpu_irq_remove_domain(adev);
323aaa36a97SAlex Deucher 
324aaa36a97SAlex Deucher 	return 0;
325aaa36a97SAlex Deucher }
326aaa36a97SAlex Deucher 
tonga_ih_hw_init(void * handle)3275fc3aeebSyanyang1 static int tonga_ih_hw_init(void *handle)
328aaa36a97SAlex Deucher {
329aaa36a97SAlex Deucher 	int r;
3305fc3aeebSyanyang1 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
331aaa36a97SAlex Deucher 
332aaa36a97SAlex Deucher 	r = tonga_ih_irq_init(adev);
333aaa36a97SAlex Deucher 	if (r)
334aaa36a97SAlex Deucher 		return r;
335aaa36a97SAlex Deucher 
336aaa36a97SAlex Deucher 	return 0;
337aaa36a97SAlex Deucher }
338aaa36a97SAlex Deucher 
tonga_ih_hw_fini(void * handle)3395fc3aeebSyanyang1 static int tonga_ih_hw_fini(void *handle)
340aaa36a97SAlex Deucher {
3415fc3aeebSyanyang1 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3425fc3aeebSyanyang1 
343aaa36a97SAlex Deucher 	tonga_ih_irq_disable(adev);
344aaa36a97SAlex Deucher 
345aaa36a97SAlex Deucher 	return 0;
346aaa36a97SAlex Deucher }
347aaa36a97SAlex Deucher 
tonga_ih_suspend(void * handle)3485fc3aeebSyanyang1 static int tonga_ih_suspend(void *handle)
349aaa36a97SAlex Deucher {
3505fc3aeebSyanyang1 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3515fc3aeebSyanyang1 
352aaa36a97SAlex Deucher 	return tonga_ih_hw_fini(adev);
353aaa36a97SAlex Deucher }
354aaa36a97SAlex Deucher 
tonga_ih_resume(void * handle)3555fc3aeebSyanyang1 static int tonga_ih_resume(void *handle)
356aaa36a97SAlex Deucher {
3575fc3aeebSyanyang1 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3585fc3aeebSyanyang1 
359aaa36a97SAlex Deucher 	return tonga_ih_hw_init(adev);
360aaa36a97SAlex Deucher }
361aaa36a97SAlex Deucher 
tonga_ih_is_idle(void * handle)3625fc3aeebSyanyang1 static bool tonga_ih_is_idle(void *handle)
363aaa36a97SAlex Deucher {
3645fc3aeebSyanyang1 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
365aaa36a97SAlex Deucher 	u32 tmp = RREG32(mmSRBM_STATUS);
366aaa36a97SAlex Deucher 
367aaa36a97SAlex Deucher 	if (REG_GET_FIELD(tmp, SRBM_STATUS, IH_BUSY))
368aaa36a97SAlex Deucher 		return false;
369aaa36a97SAlex Deucher 
370aaa36a97SAlex Deucher 	return true;
371aaa36a97SAlex Deucher }
372aaa36a97SAlex Deucher 
tonga_ih_wait_for_idle(void * handle)3735fc3aeebSyanyang1 static int tonga_ih_wait_for_idle(void *handle)
374aaa36a97SAlex Deucher {
375aaa36a97SAlex Deucher 	unsigned i;
376aaa36a97SAlex Deucher 	u32 tmp;
3775fc3aeebSyanyang1 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
378aaa36a97SAlex Deucher 
379aaa36a97SAlex Deucher 	for (i = 0; i < adev->usec_timeout; i++) {
380aaa36a97SAlex Deucher 		/* read MC_STATUS */
381aaa36a97SAlex Deucher 		tmp = RREG32(mmSRBM_STATUS);
382aaa36a97SAlex Deucher 		if (!REG_GET_FIELD(tmp, SRBM_STATUS, IH_BUSY))
383aaa36a97SAlex Deucher 			return 0;
384aaa36a97SAlex Deucher 		udelay(1);
385aaa36a97SAlex Deucher 	}
386aaa36a97SAlex Deucher 	return -ETIMEDOUT;
387aaa36a97SAlex Deucher }
388aaa36a97SAlex Deucher 
tonga_ih_check_soft_reset(void * handle)389da146d3bSAlex Deucher static bool tonga_ih_check_soft_reset(void *handle)
390aaa36a97SAlex Deucher {
3915fc3aeebSyanyang1 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3921015a1b1SChunming Zhou 	u32 srbm_soft_reset = 0;
393aaa36a97SAlex Deucher 	u32 tmp = RREG32(mmSRBM_STATUS);
394aaa36a97SAlex Deucher 
395aaa36a97SAlex Deucher 	if (tmp & SRBM_STATUS__IH_BUSY_MASK)
396aaa36a97SAlex Deucher 		srbm_soft_reset = REG_SET_FIELD(srbm_soft_reset, SRBM_SOFT_RESET,
397aaa36a97SAlex Deucher 						SOFT_RESET_IH, 1);
398aaa36a97SAlex Deucher 
399aaa36a97SAlex Deucher 	if (srbm_soft_reset) {
4001015a1b1SChunming Zhou 		adev->irq.srbm_soft_reset = srbm_soft_reset;
401da146d3bSAlex Deucher 		return true;
4021015a1b1SChunming Zhou 	} else {
4031015a1b1SChunming Zhou 		adev->irq.srbm_soft_reset = 0;
404da146d3bSAlex Deucher 		return false;
4051015a1b1SChunming Zhou 	}
4061015a1b1SChunming Zhou }
4071015a1b1SChunming Zhou 
tonga_ih_pre_soft_reset(void * handle)4081015a1b1SChunming Zhou static int tonga_ih_pre_soft_reset(void *handle)
4091015a1b1SChunming Zhou {
4101015a1b1SChunming Zhou 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
4111015a1b1SChunming Zhou 
412da146d3bSAlex Deucher 	if (!adev->irq.srbm_soft_reset)
4131015a1b1SChunming Zhou 		return 0;
4141015a1b1SChunming Zhou 
4151015a1b1SChunming Zhou 	return tonga_ih_hw_fini(adev);
4161015a1b1SChunming Zhou }
4171015a1b1SChunming Zhou 
tonga_ih_post_soft_reset(void * handle)4181015a1b1SChunming Zhou static int tonga_ih_post_soft_reset(void *handle)
4191015a1b1SChunming Zhou {
4201015a1b1SChunming Zhou 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
4211015a1b1SChunming Zhou 
422da146d3bSAlex Deucher 	if (!adev->irq.srbm_soft_reset)
4231015a1b1SChunming Zhou 		return 0;
4241015a1b1SChunming Zhou 
4251015a1b1SChunming Zhou 	return tonga_ih_hw_init(adev);
4261015a1b1SChunming Zhou }
4271015a1b1SChunming Zhou 
tonga_ih_soft_reset(void * handle)4281015a1b1SChunming Zhou static int tonga_ih_soft_reset(void *handle)
4291015a1b1SChunming Zhou {
4301015a1b1SChunming Zhou 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
4311015a1b1SChunming Zhou 	u32 srbm_soft_reset;
4321015a1b1SChunming Zhou 
433da146d3bSAlex Deucher 	if (!adev->irq.srbm_soft_reset)
4341015a1b1SChunming Zhou 		return 0;
4351015a1b1SChunming Zhou 	srbm_soft_reset = adev->irq.srbm_soft_reset;
4361015a1b1SChunming Zhou 
4371015a1b1SChunming Zhou 	if (srbm_soft_reset) {
4381015a1b1SChunming Zhou 		u32 tmp;
4391015a1b1SChunming Zhou 
440aaa36a97SAlex Deucher 		tmp = RREG32(mmSRBM_SOFT_RESET);
441aaa36a97SAlex Deucher 		tmp |= srbm_soft_reset;
442aaa36a97SAlex Deucher 		dev_info(adev->dev, "SRBM_SOFT_RESET=0x%08X\n", tmp);
443aaa36a97SAlex Deucher 		WREG32(mmSRBM_SOFT_RESET, tmp);
444aaa36a97SAlex Deucher 		tmp = RREG32(mmSRBM_SOFT_RESET);
445aaa36a97SAlex Deucher 
446aaa36a97SAlex Deucher 		udelay(50);
447aaa36a97SAlex Deucher 
448aaa36a97SAlex Deucher 		tmp &= ~srbm_soft_reset;
449aaa36a97SAlex Deucher 		WREG32(mmSRBM_SOFT_RESET, tmp);
450aaa36a97SAlex Deucher 		tmp = RREG32(mmSRBM_SOFT_RESET);
451aaa36a97SAlex Deucher 
452aaa36a97SAlex Deucher 		/* Wait a little for things to settle down */
453aaa36a97SAlex Deucher 		udelay(50);
454aaa36a97SAlex Deucher 	}
455aaa36a97SAlex Deucher 
456aaa36a97SAlex Deucher 	return 0;
457aaa36a97SAlex Deucher }
458aaa36a97SAlex Deucher 
tonga_ih_set_clockgating_state(void * handle,enum amd_clockgating_state state)4595fc3aeebSyanyang1 static int tonga_ih_set_clockgating_state(void *handle,
4605fc3aeebSyanyang1 					  enum amd_clockgating_state state)
461aaa36a97SAlex Deucher {
462aaa36a97SAlex Deucher 	return 0;
463aaa36a97SAlex Deucher }
464aaa36a97SAlex Deucher 
tonga_ih_set_powergating_state(void * handle,enum amd_powergating_state state)4655fc3aeebSyanyang1 static int tonga_ih_set_powergating_state(void *handle,
4665fc3aeebSyanyang1 					  enum amd_powergating_state state)
467aaa36a97SAlex Deucher {
468aaa36a97SAlex Deucher 	return 0;
469aaa36a97SAlex Deucher }
470aaa36a97SAlex Deucher 
471a1255107SAlex Deucher static const struct amd_ip_funcs tonga_ih_ip_funcs = {
47288a907d6STom St Denis 	.name = "tonga_ih",
473aaa36a97SAlex Deucher 	.early_init = tonga_ih_early_init,
474aaa36a97SAlex Deucher 	.late_init = NULL,
475aaa36a97SAlex Deucher 	.sw_init = tonga_ih_sw_init,
476aaa36a97SAlex Deucher 	.sw_fini = tonga_ih_sw_fini,
477aaa36a97SAlex Deucher 	.hw_init = tonga_ih_hw_init,
478aaa36a97SAlex Deucher 	.hw_fini = tonga_ih_hw_fini,
479aaa36a97SAlex Deucher 	.suspend = tonga_ih_suspend,
480aaa36a97SAlex Deucher 	.resume = tonga_ih_resume,
481aaa36a97SAlex Deucher 	.is_idle = tonga_ih_is_idle,
482aaa36a97SAlex Deucher 	.wait_for_idle = tonga_ih_wait_for_idle,
4831015a1b1SChunming Zhou 	.check_soft_reset = tonga_ih_check_soft_reset,
4841015a1b1SChunming Zhou 	.pre_soft_reset = tonga_ih_pre_soft_reset,
485aaa36a97SAlex Deucher 	.soft_reset = tonga_ih_soft_reset,
4861015a1b1SChunming Zhou 	.post_soft_reset = tonga_ih_post_soft_reset,
487aaa36a97SAlex Deucher 	.set_clockgating_state = tonga_ih_set_clockgating_state,
488aaa36a97SAlex Deucher 	.set_powergating_state = tonga_ih_set_powergating_state,
489aaa36a97SAlex Deucher };
490aaa36a97SAlex Deucher 
491aaa36a97SAlex Deucher static const struct amdgpu_ih_funcs tonga_ih_funcs = {
492aaa36a97SAlex Deucher 	.get_wptr = tonga_ih_get_wptr,
493aaa36a97SAlex Deucher 	.decode_iv = tonga_ih_decode_iv,
494aaa36a97SAlex Deucher 	.set_rptr = tonga_ih_set_rptr
495aaa36a97SAlex Deucher };
496aaa36a97SAlex Deucher 
tonga_ih_set_interrupt_funcs(struct amdgpu_device * adev)497aaa36a97SAlex Deucher static void tonga_ih_set_interrupt_funcs(struct amdgpu_device *adev)
498aaa36a97SAlex Deucher {
499aaa36a97SAlex Deucher 	adev->irq.ih_funcs = &tonga_ih_funcs;
500aaa36a97SAlex Deucher }
501aaa36a97SAlex Deucher 
5027bb8c4f6SRan Sun const struct amdgpu_ip_block_version tonga_ih_ip_block = {
503a1255107SAlex Deucher 	.type = AMD_IP_BLOCK_TYPE_IH,
504a1255107SAlex Deucher 	.major = 3,
505a1255107SAlex Deucher 	.minor = 0,
506a1255107SAlex Deucher 	.rev = 0,
507a1255107SAlex Deucher 	.funcs = &tonga_ih_ip_funcs,
508a1255107SAlex Deucher };
509