2 * Copyright 2014 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
24 #include <linux/dma-mapping.h>
27 #include "amdgpu_ih.h"
30 * amdgpu_ih_ring_init - initialize the IH state
32 * @adev: amdgpu_device pointer
33 * @ih: ih ring to initialize
34 * @ring_size: ring size to allocate
35 * @use_bus_addr: true when we can use dma_alloc_coherent
37 * Initializes the IH state and allocates a buffer
38 * for the IH ring buffer.
39 * Returns 0 for success, errors for failure.
41 int amdgpu_ih_ring_init(struct amdgpu_device *adev, struct amdgpu_ih_ring *ih,
42 unsigned ring_size, bool use_bus_addr)
48 rb_bufsz = order_base_2(ring_size / 4);
49 ring_size = (1 << rb_bufsz) * 4;
50 ih->ring_size = ring_size;
51 ih->ptr_mask = ih->ring_size - 1;
53 ih->use_bus_addr = use_bus_addr;
61 /* add 8 bytes for the rptr/wptr shadows and
62 * add them to the end of the ring allocation.
64 ih->ring = dma_alloc_coherent(adev->dev, ih->ring_size + 8,
65 &dma_addr, GFP_KERNEL);
69 ih->gpu_addr = dma_addr;
70 ih->wptr_addr = dma_addr + ih->ring_size;
71 ih->wptr_cpu = &ih->ring[ih->ring_size / 4];
72 ih->rptr_addr = dma_addr + ih->ring_size + 4;
73 ih->rptr_cpu = &ih->ring[(ih->ring_size / 4) + 1];
75 unsigned wptr_offs, rptr_offs;
77 r = amdgpu_device_wb_get(adev, &wptr_offs);
81 r = amdgpu_device_wb_get(adev, &rptr_offs);
83 amdgpu_device_wb_free(adev, wptr_offs);
87 r = amdgpu_bo_create_kernel(adev, ih->ring_size, PAGE_SIZE,
88 AMDGPU_GEM_DOMAIN_GTT,
89 &ih->ring_obj, &ih->gpu_addr,
92 amdgpu_device_wb_free(adev, rptr_offs);
93 amdgpu_device_wb_free(adev, wptr_offs);
97 ih->wptr_addr = adev->wb.gpu_addr + wptr_offs * 4;
98 ih->wptr_cpu = &adev->wb.wb[wptr_offs];
99 ih->rptr_addr = adev->wb.gpu_addr + rptr_offs * 4;
100 ih->rptr_cpu = &adev->wb.wb[rptr_offs];
106 * amdgpu_ih_ring_fini - tear down the IH state
108 * @adev: amdgpu_device pointer
109 * @ih: ih ring to tear down
111 * Tears down the IH state and frees buffer
112 * used for the IH ring buffer.
114 void amdgpu_ih_ring_fini(struct amdgpu_device *adev, struct amdgpu_ih_ring *ih)
116 if (ih->use_bus_addr) {
120 /* add 8 bytes for the rptr/wptr shadows and
121 * add them to the end of the ring allocation.
123 dma_free_coherent(adev->dev, ih->ring_size + 8,
124 (void *)ih->ring, ih->gpu_addr);
127 amdgpu_bo_free_kernel(&ih->ring_obj, &ih->gpu_addr,
129 amdgpu_device_wb_free(adev, (ih->wptr_addr - ih->gpu_addr) / 4);
130 amdgpu_device_wb_free(adev, (ih->rptr_addr - ih->gpu_addr) / 4);
135 * amdgpu_ih_ring_write - write IV to the ring buffer
137 * @ih: ih ring to write to
138 * @iv: the iv to write
139 * @num_dw: size of the iv in dw
141 * Writes an IV to the ring buffer using the CPU and increment the wptr.
142 * Used for testing and delegating IVs to a software ring.
144 void amdgpu_ih_ring_write(struct amdgpu_ih_ring *ih, const uint32_t *iv,
147 uint32_t wptr = le32_to_cpu(*ih->wptr_cpu) >> 2;
150 for (i = 0; i < num_dw; ++i)
151 ih->ring[wptr++] = cpu_to_le32(iv[i]);
154 wptr &= ih->ptr_mask;
156 /* Only commit the new wptr if we don't overflow */
157 if (wptr != READ_ONCE(ih->rptr)) {
159 WRITE_ONCE(*ih->wptr_cpu, cpu_to_le32(wptr));
164 * amdgpu_ih_process - interrupt handler
166 * @adev: amdgpu_device pointer
167 * @ih: ih ring to process
169 * Interrupt hander (VI), walk the IH ring.
170 * Returns irq process return code.
172 int amdgpu_ih_process(struct amdgpu_device *adev, struct amdgpu_ih_ring *ih)
174 unsigned int count = AMDGPU_IH_MAX_NUM_IVS;
177 if (!ih->enabled || adev->shutdown)
180 wptr = amdgpu_ih_get_wptr(adev, ih);
183 /* is somebody else already processing irqs? */
184 if (atomic_xchg(&ih->lock, 1))
187 DRM_DEBUG("%s: rptr %d, wptr %d\n", __func__, ih->rptr, wptr);
189 /* Order reading of wptr vs. reading of IH ring data */
192 while (ih->rptr != wptr && --count) {
193 amdgpu_irq_dispatch(adev, ih);
194 ih->rptr &= ih->ptr_mask;
197 amdgpu_ih_set_rptr(adev, ih);
198 atomic_set(&ih->lock, 0);
200 /* make sure wptr hasn't changed while processing */
201 wptr = amdgpu_ih_get_wptr(adev, ih);
202 if (wptr != ih->rptr)
209 * amdgpu_ih_decode_iv_helper - decode an interrupt vector
211 * @adev: amdgpu_device pointer
213 * Decodes the interrupt vector at the current rptr
214 * position and also advance the position for for Vega10
217 void amdgpu_ih_decode_iv_helper(struct amdgpu_device *adev,
218 struct amdgpu_ih_ring *ih,
219 struct amdgpu_iv_entry *entry)
221 /* wptr/rptr are in bytes! */
222 u32 ring_index = ih->rptr >> 2;
225 dw[0] = le32_to_cpu(ih->ring[ring_index + 0]);
226 dw[1] = le32_to_cpu(ih->ring[ring_index + 1]);
227 dw[2] = le32_to_cpu(ih->ring[ring_index + 2]);
228 dw[3] = le32_to_cpu(ih->ring[ring_index + 3]);
229 dw[4] = le32_to_cpu(ih->ring[ring_index + 4]);
230 dw[5] = le32_to_cpu(ih->ring[ring_index + 5]);
231 dw[6] = le32_to_cpu(ih->ring[ring_index + 6]);
232 dw[7] = le32_to_cpu(ih->ring[ring_index + 7]);
234 entry->client_id = dw[0] & 0xff;
235 entry->src_id = (dw[0] >> 8) & 0xff;
236 entry->ring_id = (dw[0] >> 16) & 0xff;
237 entry->vmid = (dw[0] >> 24) & 0xf;
238 entry->vmid_src = (dw[0] >> 31);
239 entry->timestamp = dw[1] | ((u64)(dw[2] & 0xffff) << 32);
240 entry->timestamp_src = dw[2] >> 31;
241 entry->pasid = dw[3] & 0xffff;
242 entry->pasid_src = dw[3] >> 31;
243 entry->src_data[0] = dw[4];
244 entry->src_data[1] = dw[5];
245 entry->src_data[2] = dw[6];
246 entry->src_data[3] = dw[7];
248 /* wptr/rptr are in bytes! */