blob: 994ff02db013ee8f8e669d4f5251161cd4a7538f [file] [log] [blame]
Ken Wang27ae1062016-01-19 14:02:14 +08001/*
2 * Copyright 2015 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 */
23#include "drmP.h"
24#include "amdgpu.h"
25#include "amdgpu_ih.h"
26#include "si/sid.h"
27#include "si_ih.h"
28
29static void si_ih_set_interrupt_funcs(struct amdgpu_device *adev);
30
31static void si_ih_enable_interrupts(struct amdgpu_device *adev)
32{
33 u32 ih_cntl = RREG32(IH_CNTL);
34 u32 ih_rb_cntl = RREG32(IH_RB_CNTL);
35
36 ih_cntl |= ENABLE_INTR;
37 ih_rb_cntl |= IH_RB_ENABLE;
38 WREG32(IH_CNTL, ih_cntl);
39 WREG32(IH_RB_CNTL, ih_rb_cntl);
40 adev->irq.ih.enabled = true;
41}
42
43static void si_ih_disable_interrupts(struct amdgpu_device *adev)
44{
45 u32 ih_rb_cntl = RREG32(IH_RB_CNTL);
46 u32 ih_cntl = RREG32(IH_CNTL);
47
48 ih_rb_cntl &= ~IH_RB_ENABLE;
49 ih_cntl &= ~ENABLE_INTR;
50 WREG32(IH_RB_CNTL, ih_rb_cntl);
51 WREG32(IH_CNTL, ih_cntl);
52 WREG32(IH_RB_RPTR, 0);
53 WREG32(IH_RB_WPTR, 0);
54 adev->irq.ih.enabled = false;
55 adev->irq.ih.rptr = 0;
56}
57
58static int si_ih_irq_init(struct amdgpu_device *adev)
59{
60 int ret = 0;
61 int rb_bufsz;
62 u32 interrupt_cntl, ih_cntl, ih_rb_cntl;
63 u64 wptr_off;
64
65 si_ih_disable_interrupts(adev);
66 WREG32(INTERRUPT_CNTL2, adev->irq.ih.gpu_addr >> 8);
67 interrupt_cntl = RREG32(INTERRUPT_CNTL);
68 interrupt_cntl &= ~IH_DUMMY_RD_OVERRIDE;
69 interrupt_cntl &= ~IH_REQ_NONSNOOP_EN;
70 WREG32(INTERRUPT_CNTL, interrupt_cntl);
71
72 WREG32(IH_RB_BASE, adev->irq.ih.gpu_addr >> 8);
73 rb_bufsz = order_base_2(adev->irq.ih.ring_size / 4);
74
75 ih_rb_cntl = (IH_WPTR_OVERFLOW_ENABLE |
76 IH_WPTR_OVERFLOW_CLEAR |
77 (rb_bufsz << 1));
78
79 ih_rb_cntl |= IH_WPTR_WRITEBACK_ENABLE;
80
81 wptr_off = adev->wb.gpu_addr + (adev->irq.ih.wptr_offs * 4);
82 WREG32(IH_RB_WPTR_ADDR_LO, lower_32_bits(wptr_off));
83 WREG32(IH_RB_WPTR_ADDR_HI, upper_32_bits(wptr_off) & 0xFF);
84
85 WREG32(IH_RB_CNTL, ih_rb_cntl);
86
87 WREG32(IH_RB_RPTR, 0);
88 WREG32(IH_RB_WPTR, 0);
89
90 ih_cntl = MC_WRREQ_CREDIT(0x10) | MC_WR_CLEAN_CNT(0x10) | MC_VMID(0);
91 if (adev->irq.msi_enabled)
92 ih_cntl |= RPTR_REARM;
93 WREG32(IH_CNTL, ih_cntl);
94
95 pci_set_master(adev->pdev);
96
97 si_ih_enable_interrupts(adev);
98
99 return ret;
100}
101
102static void si_ih_irq_disable(struct amdgpu_device *adev)
103{
104 si_ih_disable_interrupts(adev);
105 mdelay(1);
106}
107
108static u32 si_ih_get_wptr(struct amdgpu_device *adev)
109{
110 u32 wptr, tmp;
111
112 wptr = le32_to_cpu(adev->wb.wb[adev->irq.ih.wptr_offs]);
113
114 if (wptr & IH_RB_WPTR__RB_OVERFLOW_MASK) {
115 wptr &= ~IH_RB_WPTR__RB_OVERFLOW_MASK;
116 dev_warn(adev->dev, "IH ring buffer overflow (0x%08X, 0x%08X, 0x%08X)\n",
117 wptr, adev->irq.ih.rptr, (wptr + 16) & adev->irq.ih.ptr_mask);
118 adev->irq.ih.rptr = (wptr + 16) & adev->irq.ih.ptr_mask;
119 tmp = RREG32(IH_RB_CNTL);
120 tmp |= IH_RB_CNTL__WPTR_OVERFLOW_CLEAR_MASK;
121 WREG32(IH_RB_CNTL, tmp);
122 }
123 return (wptr & adev->irq.ih.ptr_mask);
124}
125
126static void si_ih_decode_iv(struct amdgpu_device *adev,
127 struct amdgpu_iv_entry *entry)
128{
129 u32 ring_index = adev->irq.ih.rptr >> 2;
130 uint32_t dw[4];
131
132 dw[0] = le32_to_cpu(adev->irq.ih.ring[ring_index + 0]);
133 dw[1] = le32_to_cpu(adev->irq.ih.ring[ring_index + 1]);
134 dw[2] = le32_to_cpu(adev->irq.ih.ring[ring_index + 2]);
135 dw[3] = le32_to_cpu(adev->irq.ih.ring[ring_index + 3]);
136
137 entry->src_id = dw[0] & 0xff;
138 entry->src_data = dw[1] & 0xfffffff;
139 entry->ring_id = dw[2] & 0xff;
140 entry->vm_id = (dw[2] >> 8) & 0xff;
141
142 adev->irq.ih.rptr += 16;
143}
144
145static void si_ih_set_rptr(struct amdgpu_device *adev)
146{
147 WREG32(IH_RB_RPTR, adev->irq.ih.rptr);
148}
149
150static int si_ih_early_init(void *handle)
151{
152 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
153
154 si_ih_set_interrupt_funcs(adev);
155
156 return 0;
157}
158
159static int si_ih_sw_init(void *handle)
160{
161 int r;
162 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
163
164 r = amdgpu_ih_ring_init(adev, 64 * 1024, false);
165 if (r)
166 return r;
167
168 r = amdgpu_irq_init(adev);
169
170 return r;
171}
172
173static int si_ih_sw_fini(void *handle)
174{
175 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
176
177 amdgpu_irq_fini(adev);
178 amdgpu_ih_ring_fini(adev);
179
180 return 0;
181}
182
183static int si_ih_hw_init(void *handle)
184{
185 int r;
186 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
187
188 r = si_ih_irq_init(adev);
189 if (r)
190 return r;
191
192 return 0;
193}
194
195static int si_ih_hw_fini(void *handle)
196{
197 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
198
199 si_ih_irq_disable(adev);
200
201 return 0;
202}
203
204static int si_ih_suspend(void *handle)
205{
206 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
207
208 return si_ih_hw_fini(adev);
209}
210
211static int si_ih_resume(void *handle)
212{
213 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
214
215 return si_ih_hw_init(adev);
216}
217
218static bool si_ih_is_idle(void *handle)
219{
220 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
221 u32 tmp = RREG32(SRBM_STATUS);
222
223 if (tmp & SRBM_STATUS__IH_BUSY_MASK)
224 return false;
225
226 return true;
227}
228
229static int si_ih_wait_for_idle(void *handle)
230{
231 unsigned i;
232 u32 tmp;
233 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
234
235 for (i = 0; i < adev->usec_timeout; i++) {
236 tmp = RREG32(SRBM_STATUS) & SRBM_STATUS__IH_BUSY_MASK;
237 if (!tmp)
238 return 0;
239 udelay(1);
240 }
241 return -ETIMEDOUT;
242}
243
244static int si_ih_soft_reset(void *handle)
245{
246 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
247
248 u32 srbm_soft_reset = 0;
249 u32 tmp = RREG32(SRBM_STATUS);
250
251 if (tmp & SRBM_STATUS__IH_BUSY_MASK)
252 srbm_soft_reset |= SRBM_SOFT_RESET__SOFT_RESET_IH_MASK;
253
254 if (srbm_soft_reset) {
255 tmp = RREG32(SRBM_SOFT_RESET);
256 tmp |= srbm_soft_reset;
257 dev_info(adev->dev, "SRBM_SOFT_RESET=0x%08X\n", tmp);
258 WREG32(SRBM_SOFT_RESET, tmp);
259 tmp = RREG32(SRBM_SOFT_RESET);
260
261 udelay(50);
262
263 tmp &= ~srbm_soft_reset;
264 WREG32(SRBM_SOFT_RESET, tmp);
265 tmp = RREG32(SRBM_SOFT_RESET);
266
267 udelay(50);
268 }
269
270 return 0;
271}
272
273static int si_ih_set_clockgating_state(void *handle,
274 enum amd_clockgating_state state)
275{
276 return 0;
277}
278
279static int si_ih_set_powergating_state(void *handle,
280 enum amd_powergating_state state)
281{
282 return 0;
283}
284
285const struct amd_ip_funcs si_ih_ip_funcs = {
286 .name = "si_ih",
287 .early_init = si_ih_early_init,
288 .late_init = NULL,
289 .sw_init = si_ih_sw_init,
290 .sw_fini = si_ih_sw_fini,
291 .hw_init = si_ih_hw_init,
292 .hw_fini = si_ih_hw_fini,
293 .suspend = si_ih_suspend,
294 .resume = si_ih_resume,
295 .is_idle = si_ih_is_idle,
296 .wait_for_idle = si_ih_wait_for_idle,
297 .soft_reset = si_ih_soft_reset,
298 .set_clockgating_state = si_ih_set_clockgating_state,
299 .set_powergating_state = si_ih_set_powergating_state,
300};
301
302static const struct amdgpu_ih_funcs si_ih_funcs = {
303 .get_wptr = si_ih_get_wptr,
304 .decode_iv = si_ih_decode_iv,
305 .set_rptr = si_ih_set_rptr
306};
307
308static void si_ih_set_interrupt_funcs(struct amdgpu_device *adev)
309{
310 if (adev->irq.ih_funcs == NULL)
311 adev->irq.ih_funcs = &si_ih_funcs;
312}
313