xref: /dflybsd-src/sys/dev/drm/amd/amdgpu/vega10_ih.c (revision 789731325bde747251c28a37e0a00ed4efb88c46)
1b843c749SSergey Zigachev /*
2b843c749SSergey Zigachev  * Copyright 2016 Advanced Micro Devices, Inc.
3b843c749SSergey Zigachev  *
4b843c749SSergey Zigachev  * Permission is hereby granted, free of charge, to any person obtaining a
5b843c749SSergey Zigachev  * copy of this software and associated documentation files (the "Software"),
6b843c749SSergey Zigachev  * to deal in the Software without restriction, including without limitation
7b843c749SSergey Zigachev  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b843c749SSergey Zigachev  * and/or sell copies of the Software, and to permit persons to whom the
9b843c749SSergey Zigachev  * Software is furnished to do so, subject to the following conditions:
10b843c749SSergey Zigachev  *
11b843c749SSergey Zigachev  * The above copyright notice and this permission notice shall be included in
12b843c749SSergey Zigachev  * all copies or substantial portions of the Software.
13b843c749SSergey Zigachev  *
14b843c749SSergey Zigachev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15b843c749SSergey Zigachev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16b843c749SSergey Zigachev  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17b843c749SSergey Zigachev  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18b843c749SSergey Zigachev  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19b843c749SSergey Zigachev  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20b843c749SSergey Zigachev  * OTHER DEALINGS IN THE SOFTWARE.
21b843c749SSergey Zigachev  *
22b843c749SSergey Zigachev  */
23b843c749SSergey Zigachev #include <drm/drmP.h>
24b843c749SSergey Zigachev #include "amdgpu.h"
25b843c749SSergey Zigachev #include "amdgpu_ih.h"
26b843c749SSergey Zigachev #include "soc15.h"
27b843c749SSergey Zigachev 
28b843c749SSergey Zigachev #include "oss/osssys_4_0_offset.h"
29b843c749SSergey Zigachev #include "oss/osssys_4_0_sh_mask.h"
30b843c749SSergey Zigachev 
31b843c749SSergey Zigachev #include "soc15_common.h"
32b843c749SSergey Zigachev #include "vega10_ih.h"
33b843c749SSergey Zigachev 
34b843c749SSergey Zigachev 
35b843c749SSergey Zigachev 
36b843c749SSergey Zigachev static void vega10_ih_set_interrupt_funcs(struct amdgpu_device *adev);
37b843c749SSergey Zigachev 
38b843c749SSergey Zigachev /**
39b843c749SSergey Zigachev  * vega10_ih_enable_interrupts - Enable the interrupt ring buffer
40b843c749SSergey Zigachev  *
41b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
42b843c749SSergey Zigachev  *
43b843c749SSergey Zigachev  * Enable the interrupt ring buffer (VEGA10).
44b843c749SSergey Zigachev  */
vega10_ih_enable_interrupts(struct amdgpu_device * adev)45b843c749SSergey Zigachev static void vega10_ih_enable_interrupts(struct amdgpu_device *adev)
46b843c749SSergey Zigachev {
47b843c749SSergey Zigachev 	u32 ih_rb_cntl = RREG32_SOC15(OSSSYS, 0, mmIH_RB_CNTL);
48b843c749SSergey Zigachev 
49b843c749SSergey Zigachev 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, RB_ENABLE, 1);
50b843c749SSergey Zigachev 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, ENABLE_INTR, 1);
51b843c749SSergey Zigachev 	WREG32_SOC15(OSSSYS, 0, mmIH_RB_CNTL, ih_rb_cntl);
52b843c749SSergey Zigachev 	adev->irq.ih.enabled = true;
53b843c749SSergey Zigachev }
54b843c749SSergey Zigachev 
55b843c749SSergey Zigachev /**
56b843c749SSergey Zigachev  * vega10_ih_disable_interrupts - Disable the interrupt ring buffer
57b843c749SSergey Zigachev  *
58b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
59b843c749SSergey Zigachev  *
60b843c749SSergey Zigachev  * Disable the interrupt ring buffer (VEGA10).
61b843c749SSergey Zigachev  */
vega10_ih_disable_interrupts(struct amdgpu_device * adev)62b843c749SSergey Zigachev static void vega10_ih_disable_interrupts(struct amdgpu_device *adev)
63b843c749SSergey Zigachev {
64b843c749SSergey Zigachev 	u32 ih_rb_cntl = RREG32_SOC15(OSSSYS, 0, mmIH_RB_CNTL);
65b843c749SSergey Zigachev 
66b843c749SSergey Zigachev 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, RB_ENABLE, 0);
67b843c749SSergey Zigachev 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, ENABLE_INTR, 0);
68b843c749SSergey Zigachev 	WREG32_SOC15(OSSSYS, 0, mmIH_RB_CNTL, ih_rb_cntl);
69b843c749SSergey Zigachev 	/* set rptr, wptr to 0 */
70b843c749SSergey Zigachev 	WREG32_SOC15(OSSSYS, 0, mmIH_RB_RPTR, 0);
71b843c749SSergey Zigachev 	WREG32_SOC15(OSSSYS, 0, mmIH_RB_WPTR, 0);
72b843c749SSergey Zigachev 	adev->irq.ih.enabled = false;
73b843c749SSergey Zigachev 	adev->irq.ih.rptr = 0;
74b843c749SSergey Zigachev }
75b843c749SSergey Zigachev 
76b843c749SSergey Zigachev /**
77b843c749SSergey Zigachev  * vega10_ih_irq_init - init and enable the interrupt ring
78b843c749SSergey Zigachev  *
79b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
80b843c749SSergey Zigachev  *
81b843c749SSergey Zigachev  * Allocate a ring buffer for the interrupt controller,
82b843c749SSergey Zigachev  * enable the RLC, disable interrupts, enable the IH
83b843c749SSergey Zigachev  * ring buffer and enable it (VI).
84b843c749SSergey Zigachev  * Called at device load and reume.
85b843c749SSergey Zigachev  * Returns 0 for success, errors for failure.
86b843c749SSergey Zigachev  */
vega10_ih_irq_init(struct amdgpu_device * adev)87b843c749SSergey Zigachev static int vega10_ih_irq_init(struct amdgpu_device *adev)
88b843c749SSergey Zigachev {
89b843c749SSergey Zigachev 	int ret = 0;
90b843c749SSergey Zigachev 	int rb_bufsz;
91b843c749SSergey Zigachev 	u32 ih_rb_cntl, ih_doorbell_rtpr;
92b843c749SSergey Zigachev 	u32 tmp;
93b843c749SSergey Zigachev 	u64 wptr_off;
94b843c749SSergey Zigachev 
95b843c749SSergey Zigachev 	/* disable irqs */
96b843c749SSergey Zigachev 	vega10_ih_disable_interrupts(adev);
97b843c749SSergey Zigachev 
98b843c749SSergey Zigachev 	adev->nbio_funcs->ih_control(adev);
99b843c749SSergey Zigachev 
100b843c749SSergey Zigachev 	ih_rb_cntl = RREG32_SOC15(OSSSYS, 0, mmIH_RB_CNTL);
101b843c749SSergey Zigachev 	/* Ring Buffer base. [39:8] of 40-bit address of the beginning of the ring buffer*/
102b843c749SSergey Zigachev 	if (adev->irq.ih.use_bus_addr) {
103b843c749SSergey Zigachev 		WREG32_SOC15(OSSSYS, 0, mmIH_RB_BASE, adev->irq.ih.rb_dma_addr >> 8);
104b843c749SSergey Zigachev 		WREG32_SOC15(OSSSYS, 0, mmIH_RB_BASE_HI, ((u64)adev->irq.ih.rb_dma_addr >> 40) & 0xff);
105b843c749SSergey Zigachev 		ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, MC_SPACE, 1);
106b843c749SSergey Zigachev 	} else {
107b843c749SSergey Zigachev 		WREG32_SOC15(OSSSYS, 0, mmIH_RB_BASE, adev->irq.ih.gpu_addr >> 8);
108b843c749SSergey Zigachev 		WREG32_SOC15(OSSSYS, 0, mmIH_RB_BASE_HI, (adev->irq.ih.gpu_addr >> 40) & 0xff);
109b843c749SSergey Zigachev 		ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, MC_SPACE, 4);
110b843c749SSergey Zigachev 	}
111b843c749SSergey Zigachev 	rb_bufsz = order_base_2(adev->irq.ih.ring_size / 4);
112b843c749SSergey Zigachev 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, WPTR_OVERFLOW_CLEAR, 1);
113b843c749SSergey Zigachev 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, WPTR_OVERFLOW_ENABLE, 1);
114b843c749SSergey Zigachev 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, RB_SIZE, rb_bufsz);
115b843c749SSergey Zigachev 	/* Ring Buffer write pointer writeback. If enabled, IH_RB_WPTR register value is written to memory */
116b843c749SSergey Zigachev 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, WPTR_WRITEBACK_ENABLE, 1);
117b843c749SSergey Zigachev 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, MC_SNOOP, 1);
118b843c749SSergey Zigachev 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, MC_RO, 0);
119b843c749SSergey Zigachev 	ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, MC_VMID, 0);
120b843c749SSergey Zigachev 
121b843c749SSergey Zigachev 	if (adev->irq.msi_enabled)
122b843c749SSergey Zigachev 		ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, RPTR_REARM, 1);
123b843c749SSergey Zigachev 
124b843c749SSergey Zigachev 	WREG32_SOC15(OSSSYS, 0, mmIH_RB_CNTL, ih_rb_cntl);
125b843c749SSergey Zigachev 
126b843c749SSergey Zigachev 	/* set the writeback address whether it's enabled or not */
127b843c749SSergey Zigachev 	if (adev->irq.ih.use_bus_addr)
128b843c749SSergey Zigachev 		wptr_off = adev->irq.ih.rb_dma_addr + (adev->irq.ih.wptr_offs * 4);
129b843c749SSergey Zigachev 	else
130b843c749SSergey Zigachev 		wptr_off = adev->wb.gpu_addr + (adev->irq.ih.wptr_offs * 4);
131b843c749SSergey Zigachev 	WREG32_SOC15(OSSSYS, 0, mmIH_RB_WPTR_ADDR_LO, lower_32_bits(wptr_off));
132b843c749SSergey Zigachev 	WREG32_SOC15(OSSSYS, 0, mmIH_RB_WPTR_ADDR_HI, upper_32_bits(wptr_off) & 0xFFFF);
133b843c749SSergey Zigachev 
134b843c749SSergey Zigachev 	/* set rptr, wptr to 0 */
135b843c749SSergey Zigachev 	WREG32_SOC15(OSSSYS, 0, mmIH_RB_RPTR, 0);
136b843c749SSergey Zigachev 	WREG32_SOC15(OSSSYS, 0, mmIH_RB_WPTR, 0);
137b843c749SSergey Zigachev 
138b843c749SSergey Zigachev 	ih_doorbell_rtpr = RREG32_SOC15(OSSSYS, 0, mmIH_DOORBELL_RPTR);
139b843c749SSergey Zigachev 	if (adev->irq.ih.use_doorbell) {
140b843c749SSergey Zigachev 		ih_doorbell_rtpr = REG_SET_FIELD(ih_doorbell_rtpr, IH_DOORBELL_RPTR,
141b843c749SSergey Zigachev 						 OFFSET, adev->irq.ih.doorbell_index);
142b843c749SSergey Zigachev 		ih_doorbell_rtpr = REG_SET_FIELD(ih_doorbell_rtpr, IH_DOORBELL_RPTR,
143b843c749SSergey Zigachev 						 ENABLE, 1);
144b843c749SSergey Zigachev 	} else {
145b843c749SSergey Zigachev 		ih_doorbell_rtpr = REG_SET_FIELD(ih_doorbell_rtpr, IH_DOORBELL_RPTR,
146b843c749SSergey Zigachev 						 ENABLE, 0);
147b843c749SSergey Zigachev 	}
148b843c749SSergey Zigachev 	WREG32_SOC15(OSSSYS, 0, mmIH_DOORBELL_RPTR, ih_doorbell_rtpr);
149b843c749SSergey Zigachev 	adev->nbio_funcs->ih_doorbell_range(adev, adev->irq.ih.use_doorbell,
150b843c749SSergey Zigachev 					    adev->irq.ih.doorbell_index);
151b843c749SSergey Zigachev 
152b843c749SSergey Zigachev 	tmp = RREG32_SOC15(OSSSYS, 0, mmIH_STORM_CLIENT_LIST_CNTL);
153b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, IH_STORM_CLIENT_LIST_CNTL,
154b843c749SSergey Zigachev 			    CLIENT18_IS_STORM_CLIENT, 1);
155b843c749SSergey Zigachev 	WREG32_SOC15(OSSSYS, 0, mmIH_STORM_CLIENT_LIST_CNTL, tmp);
156b843c749SSergey Zigachev 
157b843c749SSergey Zigachev 	tmp = RREG32_SOC15(OSSSYS, 0, mmIH_INT_FLOOD_CNTL);
158b843c749SSergey Zigachev 	tmp = REG_SET_FIELD(tmp, IH_INT_FLOOD_CNTL, FLOOD_CNTL_ENABLE, 1);
159b843c749SSergey Zigachev 	WREG32_SOC15(OSSSYS, 0, mmIH_INT_FLOOD_CNTL, tmp);
160b843c749SSergey Zigachev 
161b843c749SSergey Zigachev 	pci_set_master(adev->pdev);
162b843c749SSergey Zigachev 
163b843c749SSergey Zigachev 	/* enable interrupts */
164b843c749SSergey Zigachev 	vega10_ih_enable_interrupts(adev);
165b843c749SSergey Zigachev 
166b843c749SSergey Zigachev 	return ret;
167b843c749SSergey Zigachev }
168b843c749SSergey Zigachev 
169b843c749SSergey Zigachev /**
170b843c749SSergey Zigachev  * vega10_ih_irq_disable - disable interrupts
171b843c749SSergey Zigachev  *
172b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
173b843c749SSergey Zigachev  *
174b843c749SSergey Zigachev  * Disable interrupts on the hw (VEGA10).
175b843c749SSergey Zigachev  */
vega10_ih_irq_disable(struct amdgpu_device * adev)176b843c749SSergey Zigachev static void vega10_ih_irq_disable(struct amdgpu_device *adev)
177b843c749SSergey Zigachev {
178b843c749SSergey Zigachev 	vega10_ih_disable_interrupts(adev);
179b843c749SSergey Zigachev 
180b843c749SSergey Zigachev 	/* Wait and acknowledge irq */
181b843c749SSergey Zigachev 	mdelay(1);
182b843c749SSergey Zigachev }
183b843c749SSergey Zigachev 
184b843c749SSergey Zigachev /**
185b843c749SSergey Zigachev  * vega10_ih_get_wptr - get the IH ring buffer wptr
186b843c749SSergey Zigachev  *
187b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
188b843c749SSergey Zigachev  *
189b843c749SSergey Zigachev  * Get the IH ring buffer wptr from either the register
190b843c749SSergey Zigachev  * or the writeback memory buffer (VEGA10).  Also check for
191b843c749SSergey Zigachev  * ring buffer overflow and deal with it.
192b843c749SSergey Zigachev  * Returns the value of the wptr.
193b843c749SSergey Zigachev  */
vega10_ih_get_wptr(struct amdgpu_device * adev)194b843c749SSergey Zigachev static u32 vega10_ih_get_wptr(struct amdgpu_device *adev)
195b843c749SSergey Zigachev {
196b843c749SSergey Zigachev 	u32 wptr, tmp;
197b843c749SSergey Zigachev 
198b843c749SSergey Zigachev 	if (adev->irq.ih.use_bus_addr)
199b843c749SSergey Zigachev 		wptr = le32_to_cpu(adev->irq.ih.ring[adev->irq.ih.wptr_offs]);
200b843c749SSergey Zigachev 	else
201b843c749SSergey Zigachev 		wptr = le32_to_cpu(adev->wb.wb[adev->irq.ih.wptr_offs]);
202b843c749SSergey Zigachev 
203b843c749SSergey Zigachev 	if (REG_GET_FIELD(wptr, IH_RB_WPTR, RB_OVERFLOW)) {
204b843c749SSergey Zigachev 		wptr = REG_SET_FIELD(wptr, IH_RB_WPTR, RB_OVERFLOW, 0);
205b843c749SSergey Zigachev 
206b843c749SSergey Zigachev 		/* When a ring buffer overflow happen start parsing interrupt
207b843c749SSergey Zigachev 		 * from the last not overwritten vector (wptr + 32). Hopefully
208b843c749SSergey Zigachev 		 * this should allow us to catchup.
209b843c749SSergey Zigachev 		 */
210b843c749SSergey Zigachev 		tmp = (wptr + 32) & adev->irq.ih.ptr_mask;
211b843c749SSergey Zigachev 		dev_warn(adev->dev, "IH ring buffer overflow (0x%08X, 0x%08X, 0x%08X)\n",
212b843c749SSergey Zigachev 			wptr, adev->irq.ih.rptr, tmp);
213b843c749SSergey Zigachev 		adev->irq.ih.rptr = tmp;
214b843c749SSergey Zigachev 
215b843c749SSergey Zigachev 		tmp = RREG32_NO_KIQ(SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_CNTL));
216b843c749SSergey Zigachev 		tmp = REG_SET_FIELD(tmp, IH_RB_CNTL, WPTR_OVERFLOW_CLEAR, 1);
217b843c749SSergey Zigachev 		WREG32_NO_KIQ(SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_CNTL), tmp);
218b843c749SSergey Zigachev 	}
219b843c749SSergey Zigachev 	return (wptr & adev->irq.ih.ptr_mask);
220b843c749SSergey Zigachev }
221b843c749SSergey Zigachev 
222b843c749SSergey Zigachev /**
223b843c749SSergey Zigachev  * vega10_ih_prescreen_iv - prescreen an interrupt vector
224b843c749SSergey Zigachev  *
225b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
226b843c749SSergey Zigachev  *
227b843c749SSergey Zigachev  * Returns true if the interrupt vector should be further processed.
228b843c749SSergey Zigachev  */
vega10_ih_prescreen_iv(struct amdgpu_device * adev)229b843c749SSergey Zigachev static bool vega10_ih_prescreen_iv(struct amdgpu_device *adev)
230b843c749SSergey Zigachev {
231b843c749SSergey Zigachev 	u32 ring_index = adev->irq.ih.rptr >> 2;
232b843c749SSergey Zigachev 	u32 dw0, dw3, dw4, dw5;
233b843c749SSergey Zigachev 	u16 pasid;
234b843c749SSergey Zigachev 	u64 addr, key;
235b843c749SSergey Zigachev 	struct amdgpu_vm *vm;
236b843c749SSergey Zigachev 	int r;
237b843c749SSergey Zigachev 
238b843c749SSergey Zigachev 	dw0 = le32_to_cpu(adev->irq.ih.ring[ring_index + 0]);
239b843c749SSergey Zigachev 	dw3 = le32_to_cpu(adev->irq.ih.ring[ring_index + 3]);
240b843c749SSergey Zigachev 	dw4 = le32_to_cpu(adev->irq.ih.ring[ring_index + 4]);
241b843c749SSergey Zigachev 	dw5 = le32_to_cpu(adev->irq.ih.ring[ring_index + 5]);
242b843c749SSergey Zigachev 
243b843c749SSergey Zigachev 	/* Filter retry page faults, let only the first one pass. If
244b843c749SSergey Zigachev 	 * there are too many outstanding faults, ignore them until
245b843c749SSergey Zigachev 	 * some faults get cleared.
246b843c749SSergey Zigachev 	 */
247b843c749SSergey Zigachev 	switch (dw0 & 0xff) {
248b843c749SSergey Zigachev 	case SOC15_IH_CLIENTID_VMC:
249b843c749SSergey Zigachev 	case SOC15_IH_CLIENTID_UTCL2:
250b843c749SSergey Zigachev 		break;
251b843c749SSergey Zigachev 	default:
252b843c749SSergey Zigachev 		/* Not a VM fault */
253b843c749SSergey Zigachev 		return true;
254b843c749SSergey Zigachev 	}
255b843c749SSergey Zigachev 
256b843c749SSergey Zigachev 	pasid = dw3 & 0xffff;
257b843c749SSergey Zigachev 	/* No PASID, can't identify faulting process */
258b843c749SSergey Zigachev 	if (!pasid)
259b843c749SSergey Zigachev 		return true;
260b843c749SSergey Zigachev 
261b843c749SSergey Zigachev 	/* Not a retry fault, check fault credit */
262b843c749SSergey Zigachev 	if (!(dw5 & 0x80)) {
263b843c749SSergey Zigachev 		if (!amdgpu_vm_pasid_fault_credit(adev, pasid))
264b843c749SSergey Zigachev 			goto ignore_iv;
265b843c749SSergey Zigachev 		return true;
266b843c749SSergey Zigachev 	}
267b843c749SSergey Zigachev 
268b843c749SSergey Zigachev 	addr = ((u64)(dw5 & 0xf) << 44) | ((u64)dw4 << 12);
269b843c749SSergey Zigachev 	key = AMDGPU_VM_FAULT(pasid, addr);
270b843c749SSergey Zigachev 	r = amdgpu_ih_add_fault(adev, key);
271b843c749SSergey Zigachev 
272b843c749SSergey Zigachev 	/* Hash table is full or the fault is already being processed,
273b843c749SSergey Zigachev 	 * ignore further page faults
274b843c749SSergey Zigachev 	 */
275b843c749SSergey Zigachev 	if (r != 0)
276b843c749SSergey Zigachev 		goto ignore_iv;
277b843c749SSergey Zigachev 
278b843c749SSergey Zigachev 	/* Track retry faults in per-VM fault FIFO. */
279*78973132SSergey Zigachev 	lockmgr(&adev->vm_manager.pasid_lock, LK_EXCLUSIVE);
280b843c749SSergey Zigachev 	vm = idr_find(&adev->vm_manager.pasid_idr, pasid);
281b843c749SSergey Zigachev 	if (!vm) {
282b843c749SSergey Zigachev 		/* VM not found, process it normally */
283*78973132SSergey Zigachev 		lockmgr(&adev->vm_manager.pasid_lock, LK_RELEASE);
284b843c749SSergey Zigachev 		amdgpu_ih_clear_fault(adev, key);
285b843c749SSergey Zigachev 		return true;
286b843c749SSergey Zigachev 	}
287*78973132SSergey Zigachev #if 0
288b843c749SSergey Zigachev 	/* No locking required with single writer and single reader */
289b843c749SSergey Zigachev 	r = kfifo_put(&vm->faults, key);
290b843c749SSergey Zigachev 	if (!r) {
291b843c749SSergey Zigachev 		/* FIFO is full. Ignore it until there is space */
292b843c749SSergey Zigachev 		amdgpu_ih_clear_fault(adev, key);
293b843c749SSergey Zigachev 		goto ignore_iv;
294b843c749SSergey Zigachev 	}
295*78973132SSergey Zigachev #else
296*78973132SSergey Zigachev 	kprintf("vega10_ih.c: kfifo_put(&vm->faults, key); is not implemented\n");
297*78973132SSergey Zigachev 	lockmgr(&adev->vm_manager.pasid_lock, LK_RELEASE);
298*78973132SSergey Zigachev 	goto ignore_iv;
299*78973132SSergey Zigachev #endif
300*78973132SSergey Zigachev 	lockmgr(&adev->vm_manager.pasid_lock, LK_RELEASE);
301b843c749SSergey Zigachev 
302b843c749SSergey Zigachev 	/* It's the first fault for this address, process it normally */
303b843c749SSergey Zigachev 	return true;
304b843c749SSergey Zigachev 
305b843c749SSergey Zigachev ignore_iv:
306b843c749SSergey Zigachev 	adev->irq.ih.rptr += 32;
307b843c749SSergey Zigachev 	return false;
308b843c749SSergey Zigachev }
309b843c749SSergey Zigachev 
310b843c749SSergey Zigachev /**
311b843c749SSergey Zigachev  * vega10_ih_decode_iv - decode an interrupt vector
312b843c749SSergey Zigachev  *
313b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
314b843c749SSergey Zigachev  *
315b843c749SSergey Zigachev  * Decodes the interrupt vector at the current rptr
316b843c749SSergey Zigachev  * position and also advance the position.
317b843c749SSergey Zigachev  */
vega10_ih_decode_iv(struct amdgpu_device * adev,struct amdgpu_iv_entry * entry)318b843c749SSergey Zigachev static void vega10_ih_decode_iv(struct amdgpu_device *adev,
319b843c749SSergey Zigachev 				 struct amdgpu_iv_entry *entry)
320b843c749SSergey Zigachev {
321b843c749SSergey Zigachev 	/* wptr/rptr are in bytes! */
322b843c749SSergey Zigachev 	u32 ring_index = adev->irq.ih.rptr >> 2;
323b843c749SSergey Zigachev 	uint32_t dw[8];
324b843c749SSergey Zigachev 
325b843c749SSergey Zigachev 	dw[0] = le32_to_cpu(adev->irq.ih.ring[ring_index + 0]);
326b843c749SSergey Zigachev 	dw[1] = le32_to_cpu(adev->irq.ih.ring[ring_index + 1]);
327b843c749SSergey Zigachev 	dw[2] = le32_to_cpu(adev->irq.ih.ring[ring_index + 2]);
328b843c749SSergey Zigachev 	dw[3] = le32_to_cpu(adev->irq.ih.ring[ring_index + 3]);
329b843c749SSergey Zigachev 	dw[4] = le32_to_cpu(adev->irq.ih.ring[ring_index + 4]);
330b843c749SSergey Zigachev 	dw[5] = le32_to_cpu(adev->irq.ih.ring[ring_index + 5]);
331b843c749SSergey Zigachev 	dw[6] = le32_to_cpu(adev->irq.ih.ring[ring_index + 6]);
332b843c749SSergey Zigachev 	dw[7] = le32_to_cpu(adev->irq.ih.ring[ring_index + 7]);
333b843c749SSergey Zigachev 
334b843c749SSergey Zigachev 	entry->client_id = dw[0] & 0xff;
335b843c749SSergey Zigachev 	entry->src_id = (dw[0] >> 8) & 0xff;
336b843c749SSergey Zigachev 	entry->ring_id = (dw[0] >> 16) & 0xff;
337b843c749SSergey Zigachev 	entry->vmid = (dw[0] >> 24) & 0xf;
338b843c749SSergey Zigachev 	entry->vmid_src = (dw[0] >> 31);
339b843c749SSergey Zigachev 	entry->timestamp = dw[1] | ((u64)(dw[2] & 0xffff) << 32);
340b843c749SSergey Zigachev 	entry->timestamp_src = dw[2] >> 31;
341b843c749SSergey Zigachev 	entry->pasid = dw[3] & 0xffff;
342b843c749SSergey Zigachev 	entry->pasid_src = dw[3] >> 31;
343b843c749SSergey Zigachev 	entry->src_data[0] = dw[4];
344b843c749SSergey Zigachev 	entry->src_data[1] = dw[5];
345b843c749SSergey Zigachev 	entry->src_data[2] = dw[6];
346b843c749SSergey Zigachev 	entry->src_data[3] = dw[7];
347b843c749SSergey Zigachev 
348b843c749SSergey Zigachev 
349b843c749SSergey Zigachev 	/* wptr/rptr are in bytes! */
350b843c749SSergey Zigachev 	adev->irq.ih.rptr += 32;
351b843c749SSergey Zigachev }
352b843c749SSergey Zigachev 
353b843c749SSergey Zigachev /**
354b843c749SSergey Zigachev  * vega10_ih_set_rptr - set the IH ring buffer rptr
355b843c749SSergey Zigachev  *
356b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
357b843c749SSergey Zigachev  *
358b843c749SSergey Zigachev  * Set the IH ring buffer rptr.
359b843c749SSergey Zigachev  */
vega10_ih_set_rptr(struct amdgpu_device * adev)360b843c749SSergey Zigachev static void vega10_ih_set_rptr(struct amdgpu_device *adev)
361b843c749SSergey Zigachev {
362b843c749SSergey Zigachev 	if (adev->irq.ih.use_doorbell) {
363b843c749SSergey Zigachev 		/* XXX check if swapping is necessary on BE */
364b843c749SSergey Zigachev 		if (adev->irq.ih.use_bus_addr)
365b843c749SSergey Zigachev 			adev->irq.ih.ring[adev->irq.ih.rptr_offs] = adev->irq.ih.rptr;
366b843c749SSergey Zigachev 		else
367b843c749SSergey Zigachev 			adev->wb.wb[adev->irq.ih.rptr_offs] = adev->irq.ih.rptr;
368b843c749SSergey Zigachev 		WDOORBELL32(adev->irq.ih.doorbell_index, adev->irq.ih.rptr);
369b843c749SSergey Zigachev 	} else {
370b843c749SSergey Zigachev 		WREG32_SOC15(OSSSYS, 0, mmIH_RB_RPTR, adev->irq.ih.rptr);
371b843c749SSergey Zigachev 	}
372b843c749SSergey Zigachev }
373b843c749SSergey Zigachev 
vega10_ih_early_init(void * handle)374b843c749SSergey Zigachev static int vega10_ih_early_init(void *handle)
375b843c749SSergey Zigachev {
376b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
377b843c749SSergey Zigachev 
378b843c749SSergey Zigachev 	vega10_ih_set_interrupt_funcs(adev);
379b843c749SSergey Zigachev 	return 0;
380b843c749SSergey Zigachev }
381b843c749SSergey Zigachev 
vega10_ih_sw_init(void * handle)382b843c749SSergey Zigachev static int vega10_ih_sw_init(void *handle)
383b843c749SSergey Zigachev {
384b843c749SSergey Zigachev 	int r;
385b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
386b843c749SSergey Zigachev 
387b843c749SSergey Zigachev 	r = amdgpu_ih_ring_init(adev, 256 * 1024, true);
388b843c749SSergey Zigachev 	if (r)
389b843c749SSergey Zigachev 		return r;
390b843c749SSergey Zigachev 
391b843c749SSergey Zigachev 	adev->irq.ih.use_doorbell = true;
392b843c749SSergey Zigachev 	adev->irq.ih.doorbell_index = AMDGPU_DOORBELL64_IH << 1;
393b843c749SSergey Zigachev 
394*78973132SSergey Zigachev 	adev->irq.ih.faults = kmalloc(sizeof(*adev->irq.ih.faults), M_DRM, GFP_KERNEL);
395b843c749SSergey Zigachev 	if (!adev->irq.ih.faults)
396b843c749SSergey Zigachev 		return -ENOMEM;
397b843c749SSergey Zigachev 	INIT_CHASH_TABLE(adev->irq.ih.faults->hash,
398b843c749SSergey Zigachev 			 AMDGPU_PAGEFAULT_HASH_BITS, 8, 0);
399*78973132SSergey Zigachev 	lockinit(&adev->irq.ih.faults->lock, "agdiihfl", 0, LK_CANRECURSE);
400b843c749SSergey Zigachev 	adev->irq.ih.faults->count = 0;
401b843c749SSergey Zigachev 
402b843c749SSergey Zigachev 	r = amdgpu_irq_init(adev);
403b843c749SSergey Zigachev 
404b843c749SSergey Zigachev 	return r;
405b843c749SSergey Zigachev }
406b843c749SSergey Zigachev 
vega10_ih_sw_fini(void * handle)407b843c749SSergey Zigachev static int vega10_ih_sw_fini(void *handle)
408b843c749SSergey Zigachev {
409b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
410b843c749SSergey Zigachev 
411b843c749SSergey Zigachev 	amdgpu_irq_fini(adev);
412b843c749SSergey Zigachev 	amdgpu_ih_ring_fini(adev);
413b843c749SSergey Zigachev 
414b843c749SSergey Zigachev 	kfree(adev->irq.ih.faults);
415b843c749SSergey Zigachev 	adev->irq.ih.faults = NULL;
416b843c749SSergey Zigachev 
417b843c749SSergey Zigachev 	return 0;
418b843c749SSergey Zigachev }
419b843c749SSergey Zigachev 
vega10_ih_hw_init(void * handle)420b843c749SSergey Zigachev static int vega10_ih_hw_init(void *handle)
421b843c749SSergey Zigachev {
422b843c749SSergey Zigachev 	int r;
423b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
424b843c749SSergey Zigachev 
425b843c749SSergey Zigachev 	r = vega10_ih_irq_init(adev);
426b843c749SSergey Zigachev 	if (r)
427b843c749SSergey Zigachev 		return r;
428b843c749SSergey Zigachev 
429b843c749SSergey Zigachev 	return 0;
430b843c749SSergey Zigachev }
431b843c749SSergey Zigachev 
vega10_ih_hw_fini(void * handle)432b843c749SSergey Zigachev static int vega10_ih_hw_fini(void *handle)
433b843c749SSergey Zigachev {
434b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
435b843c749SSergey Zigachev 
436b843c749SSergey Zigachev 	vega10_ih_irq_disable(adev);
437b843c749SSergey Zigachev 
438b843c749SSergey Zigachev 	return 0;
439b843c749SSergey Zigachev }
440b843c749SSergey Zigachev 
vega10_ih_suspend(void * handle)441b843c749SSergey Zigachev static int vega10_ih_suspend(void *handle)
442b843c749SSergey Zigachev {
443b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
444b843c749SSergey Zigachev 
445b843c749SSergey Zigachev 	return vega10_ih_hw_fini(adev);
446b843c749SSergey Zigachev }
447b843c749SSergey Zigachev 
vega10_ih_resume(void * handle)448b843c749SSergey Zigachev static int vega10_ih_resume(void *handle)
449b843c749SSergey Zigachev {
450b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
451b843c749SSergey Zigachev 
452b843c749SSergey Zigachev 	return vega10_ih_hw_init(adev);
453b843c749SSergey Zigachev }
454b843c749SSergey Zigachev 
vega10_ih_is_idle(void * handle)455b843c749SSergey Zigachev static bool vega10_ih_is_idle(void *handle)
456b843c749SSergey Zigachev {
457b843c749SSergey Zigachev 	/* todo */
458b843c749SSergey Zigachev 	return true;
459b843c749SSergey Zigachev }
460b843c749SSergey Zigachev 
vega10_ih_wait_for_idle(void * handle)461b843c749SSergey Zigachev static int vega10_ih_wait_for_idle(void *handle)
462b843c749SSergey Zigachev {
463b843c749SSergey Zigachev 	/* todo */
464b843c749SSergey Zigachev 	return -ETIMEDOUT;
465b843c749SSergey Zigachev }
466b843c749SSergey Zigachev 
vega10_ih_soft_reset(void * handle)467b843c749SSergey Zigachev static int vega10_ih_soft_reset(void *handle)
468b843c749SSergey Zigachev {
469b843c749SSergey Zigachev 	/* todo */
470b843c749SSergey Zigachev 
471b843c749SSergey Zigachev 	return 0;
472b843c749SSergey Zigachev }
473b843c749SSergey Zigachev 
vega10_ih_set_clockgating_state(void * handle,enum amd_clockgating_state state)474b843c749SSergey Zigachev static int vega10_ih_set_clockgating_state(void *handle,
475b843c749SSergey Zigachev 					  enum amd_clockgating_state state)
476b843c749SSergey Zigachev {
477b843c749SSergey Zigachev 	return 0;
478b843c749SSergey Zigachev }
479b843c749SSergey Zigachev 
vega10_ih_set_powergating_state(void * handle,enum amd_powergating_state state)480b843c749SSergey Zigachev static int vega10_ih_set_powergating_state(void *handle,
481b843c749SSergey Zigachev 					  enum amd_powergating_state state)
482b843c749SSergey Zigachev {
483b843c749SSergey Zigachev 	return 0;
484b843c749SSergey Zigachev }
485b843c749SSergey Zigachev 
486b843c749SSergey Zigachev const struct amd_ip_funcs vega10_ih_ip_funcs = {
487b843c749SSergey Zigachev 	.name = "vega10_ih",
488b843c749SSergey Zigachev 	.early_init = vega10_ih_early_init,
489b843c749SSergey Zigachev 	.late_init = NULL,
490b843c749SSergey Zigachev 	.sw_init = vega10_ih_sw_init,
491b843c749SSergey Zigachev 	.sw_fini = vega10_ih_sw_fini,
492b843c749SSergey Zigachev 	.hw_init = vega10_ih_hw_init,
493b843c749SSergey Zigachev 	.hw_fini = vega10_ih_hw_fini,
494b843c749SSergey Zigachev 	.suspend = vega10_ih_suspend,
495b843c749SSergey Zigachev 	.resume = vega10_ih_resume,
496b843c749SSergey Zigachev 	.is_idle = vega10_ih_is_idle,
497b843c749SSergey Zigachev 	.wait_for_idle = vega10_ih_wait_for_idle,
498b843c749SSergey Zigachev 	.soft_reset = vega10_ih_soft_reset,
499b843c749SSergey Zigachev 	.set_clockgating_state = vega10_ih_set_clockgating_state,
500b843c749SSergey Zigachev 	.set_powergating_state = vega10_ih_set_powergating_state,
501b843c749SSergey Zigachev };
502b843c749SSergey Zigachev 
503b843c749SSergey Zigachev static const struct amdgpu_ih_funcs vega10_ih_funcs = {
504b843c749SSergey Zigachev 	.get_wptr = vega10_ih_get_wptr,
505b843c749SSergey Zigachev 	.prescreen_iv = vega10_ih_prescreen_iv,
506b843c749SSergey Zigachev 	.decode_iv = vega10_ih_decode_iv,
507b843c749SSergey Zigachev 	.set_rptr = vega10_ih_set_rptr
508b843c749SSergey Zigachev };
509b843c749SSergey Zigachev 
vega10_ih_set_interrupt_funcs(struct amdgpu_device * adev)510b843c749SSergey Zigachev static void vega10_ih_set_interrupt_funcs(struct amdgpu_device *adev)
511b843c749SSergey Zigachev {
512b843c749SSergey Zigachev 	if (adev->irq.ih_funcs == NULL)
513b843c749SSergey Zigachev 		adev->irq.ih_funcs = &vega10_ih_funcs;
514b843c749SSergey Zigachev }
515b843c749SSergey Zigachev 
516b843c749SSergey Zigachev const struct amdgpu_ip_block_version vega10_ih_ip_block =
517b843c749SSergey Zigachev {
518b843c749SSergey Zigachev 	.type = AMD_IP_BLOCK_TYPE_IH,
519b843c749SSergey Zigachev 	.major = 4,
520b843c749SSergey Zigachev 	.minor = 0,
521b843c749SSergey Zigachev 	.rev = 0,
522b843c749SSergey Zigachev 	.funcs = &vega10_ih_ip_funcs,
523b843c749SSergey Zigachev };
524