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.
26 #include "amdgpu_ih.h"
27 #include "amdgpu_amdkfd.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;
59 /* add 8 bytes for the rptr/wptr shadows and
60 * add them to the end of the ring allocation.
62 ih->ring = dma_alloc_coherent(adev->dev, ih->ring_size + 8,
63 &ih->rb_dma_addr, GFP_KERNEL);
67 memset((void *)ih->ring, 0, ih->ring_size + 8);
68 ih->wptr_offs = (ih->ring_size / 4) + 0;
69 ih->rptr_offs = (ih->ring_size / 4) + 1;
71 r = amdgpu_device_wb_get(adev, &ih->wptr_offs);
75 r = amdgpu_device_wb_get(adev, &ih->rptr_offs);
77 amdgpu_device_wb_free(adev, ih->wptr_offs);
81 r = amdgpu_bo_create_kernel(adev, ih->ring_size, PAGE_SIZE,
82 AMDGPU_GEM_DOMAIN_GTT,
83 &ih->ring_obj, &ih->gpu_addr,
86 amdgpu_device_wb_free(adev, ih->rptr_offs);
87 amdgpu_device_wb_free(adev, ih->wptr_offs);
95 * amdgpu_ih_ring_fini - tear down the IH state
97 * @adev: amdgpu_device pointer
98 * @ih: ih ring to tear down
100 * Tears down the IH state and frees buffer
101 * used for the IH ring buffer.
103 void amdgpu_ih_ring_fini(struct amdgpu_device *adev, struct amdgpu_ih_ring *ih)
105 if (ih->use_bus_addr) {
109 /* add 8 bytes for the rptr/wptr shadows and
110 * add them to the end of the ring allocation.
112 dma_free_coherent(adev->dev, ih->ring_size + 8,
113 (void *)ih->ring, ih->rb_dma_addr);
116 amdgpu_bo_free_kernel(&ih->ring_obj, &ih->gpu_addr,
118 amdgpu_device_wb_free(adev, ih->wptr_offs);
119 amdgpu_device_wb_free(adev, ih->rptr_offs);
124 * amdgpu_ih_process - interrupt handler
126 * @adev: amdgpu_device pointer
127 * @ih: ih ring to process
129 * Interrupt hander (VI), walk the IH ring.
130 * Returns irq process return code.
132 int amdgpu_ih_process(struct amdgpu_device *adev, struct amdgpu_ih_ring *ih)
134 struct amdgpu_iv_entry entry;
137 if (!ih->enabled || adev->shutdown)
140 wptr = amdgpu_ih_get_wptr(adev);
143 /* is somebody else already processing irqs? */
144 if (atomic_xchg(&ih->lock, 1))
147 DRM_DEBUG("%s: rptr %d, wptr %d\n", __func__, ih->rptr, wptr);
149 /* Order reading of wptr vs. reading of IH ring data */
152 while (ih->rptr != wptr) {
153 u32 ring_index = ih->rptr >> 2;
155 /* Prescreening of high-frequency interrupts */
156 if (!amdgpu_ih_prescreen_iv(adev)) {
157 ih->rptr &= ih->ptr_mask;
161 /* Before dispatching irq to IP blocks, send it to amdkfd */
162 amdgpu_amdkfd_interrupt(adev,
163 (const void *) &ih->ring[ring_index]);
165 entry.iv_entry = (const uint32_t *)&ih->ring[ring_index];
166 amdgpu_ih_decode_iv(adev, &entry);
167 ih->rptr &= ih->ptr_mask;
169 amdgpu_irq_dispatch(adev, &entry);
171 amdgpu_ih_set_rptr(adev);
172 atomic_set(&ih->lock, 0);
174 /* make sure wptr hasn't changed while processing */
175 wptr = amdgpu_ih_get_wptr(adev);
176 if (wptr != ih->rptr)