1b843c749SSergey Zigachev /*
2b843c749SSergey Zigachev * Copyright 2014 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
24b843c749SSergey Zigachev #include <drm/drmP.h>
25b843c749SSergey Zigachev #include "amdgpu.h"
26b843c749SSergey Zigachev #include "amdgpu_ih.h"
27b843c749SSergey Zigachev #include "amdgpu_amdkfd.h"
28b843c749SSergey Zigachev
29b843c749SSergey Zigachev /**
30b843c749SSergey Zigachev * amdgpu_ih_ring_alloc - allocate memory for the IH ring
31b843c749SSergey Zigachev *
32b843c749SSergey Zigachev * @adev: amdgpu_device pointer
33b843c749SSergey Zigachev *
34b843c749SSergey Zigachev * Allocate a ring buffer for the interrupt controller.
35b843c749SSergey Zigachev * Returns 0 for success, errors for failure.
36b843c749SSergey Zigachev */
amdgpu_ih_ring_alloc(struct amdgpu_device * adev)37b843c749SSergey Zigachev static int amdgpu_ih_ring_alloc(struct amdgpu_device *adev)
38b843c749SSergey Zigachev {
39b843c749SSergey Zigachev int r;
40b843c749SSergey Zigachev
41b843c749SSergey Zigachev /* Allocate ring buffer */
42b843c749SSergey Zigachev if (adev->irq.ih.ring_obj == NULL) {
43b843c749SSergey Zigachev r = amdgpu_bo_create_kernel(adev, adev->irq.ih.ring_size,
44b843c749SSergey Zigachev PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
45b843c749SSergey Zigachev &adev->irq.ih.ring_obj,
46*78973132SSergey Zigachev (u64 *)&adev->irq.ih.gpu_addr,
47b843c749SSergey Zigachev (void **)&adev->irq.ih.ring);
48b843c749SSergey Zigachev if (r) {
49b843c749SSergey Zigachev DRM_ERROR("amdgpu: failed to create ih ring buffer (%d).\n", r);
50b843c749SSergey Zigachev return r;
51b843c749SSergey Zigachev }
52b843c749SSergey Zigachev }
53b843c749SSergey Zigachev return 0;
54b843c749SSergey Zigachev }
55b843c749SSergey Zigachev
56b843c749SSergey Zigachev /**
57b843c749SSergey Zigachev * amdgpu_ih_ring_init - initialize the IH state
58b843c749SSergey Zigachev *
59b843c749SSergey Zigachev * @adev: amdgpu_device pointer
60b843c749SSergey Zigachev *
61b843c749SSergey Zigachev * Initializes the IH state and allocates a buffer
62b843c749SSergey Zigachev * for the IH ring buffer.
63b843c749SSergey Zigachev * Returns 0 for success, errors for failure.
64b843c749SSergey Zigachev */
amdgpu_ih_ring_init(struct amdgpu_device * adev,unsigned ring_size,bool use_bus_addr)65b843c749SSergey Zigachev int amdgpu_ih_ring_init(struct amdgpu_device *adev, unsigned ring_size,
66b843c749SSergey Zigachev bool use_bus_addr)
67b843c749SSergey Zigachev {
68b843c749SSergey Zigachev u32 rb_bufsz;
69b843c749SSergey Zigachev int r;
70b843c749SSergey Zigachev
71b843c749SSergey Zigachev /* Align ring size */
72b843c749SSergey Zigachev rb_bufsz = order_base_2(ring_size / 4);
73b843c749SSergey Zigachev ring_size = (1 << rb_bufsz) * 4;
74b843c749SSergey Zigachev adev->irq.ih.ring_size = ring_size;
75b843c749SSergey Zigachev adev->irq.ih.ptr_mask = adev->irq.ih.ring_size - 1;
76b843c749SSergey Zigachev adev->irq.ih.rptr = 0;
77b843c749SSergey Zigachev adev->irq.ih.use_bus_addr = use_bus_addr;
78b843c749SSergey Zigachev
79b843c749SSergey Zigachev if (adev->irq.ih.use_bus_addr) {
80b843c749SSergey Zigachev if (!adev->irq.ih.ring) {
81b843c749SSergey Zigachev /* add 8 bytes for the rptr/wptr shadows and
82b843c749SSergey Zigachev * add them to the end of the ring allocation.
83b843c749SSergey Zigachev */
84b843c749SSergey Zigachev adev->irq.ih.ring = pci_alloc_consistent(adev->pdev,
85b843c749SSergey Zigachev adev->irq.ih.ring_size + 8,
86b843c749SSergey Zigachev &adev->irq.ih.rb_dma_addr);
87b843c749SSergey Zigachev if (adev->irq.ih.ring == NULL)
88b843c749SSergey Zigachev return -ENOMEM;
89b843c749SSergey Zigachev memset((void *)adev->irq.ih.ring, 0, adev->irq.ih.ring_size + 8);
90b843c749SSergey Zigachev adev->irq.ih.wptr_offs = (adev->irq.ih.ring_size / 4) + 0;
91b843c749SSergey Zigachev adev->irq.ih.rptr_offs = (adev->irq.ih.ring_size / 4) + 1;
92b843c749SSergey Zigachev }
93b843c749SSergey Zigachev return 0;
94b843c749SSergey Zigachev } else {
95b843c749SSergey Zigachev r = amdgpu_device_wb_get(adev, &adev->irq.ih.wptr_offs);
96b843c749SSergey Zigachev if (r) {
97b843c749SSergey Zigachev dev_err(adev->dev, "(%d) ih wptr_offs wb alloc failed\n", r);
98b843c749SSergey Zigachev return r;
99b843c749SSergey Zigachev }
100b843c749SSergey Zigachev
101b843c749SSergey Zigachev r = amdgpu_device_wb_get(adev, &adev->irq.ih.rptr_offs);
102b843c749SSergey Zigachev if (r) {
103b843c749SSergey Zigachev amdgpu_device_wb_free(adev, adev->irq.ih.wptr_offs);
104b843c749SSergey Zigachev dev_err(adev->dev, "(%d) ih rptr_offs wb alloc failed\n", r);
105b843c749SSergey Zigachev return r;
106b843c749SSergey Zigachev }
107b843c749SSergey Zigachev
108b843c749SSergey Zigachev return amdgpu_ih_ring_alloc(adev);
109b843c749SSergey Zigachev }
110b843c749SSergey Zigachev }
111b843c749SSergey Zigachev
112b843c749SSergey Zigachev /**
113b843c749SSergey Zigachev * amdgpu_ih_ring_fini - tear down the IH state
114b843c749SSergey Zigachev *
115b843c749SSergey Zigachev * @adev: amdgpu_device pointer
116b843c749SSergey Zigachev *
117b843c749SSergey Zigachev * Tears down the IH state and frees buffer
118b843c749SSergey Zigachev * used for the IH ring buffer.
119b843c749SSergey Zigachev */
amdgpu_ih_ring_fini(struct amdgpu_device * adev)120b843c749SSergey Zigachev void amdgpu_ih_ring_fini(struct amdgpu_device *adev)
121b843c749SSergey Zigachev {
122b843c749SSergey Zigachev if (adev->irq.ih.use_bus_addr) {
123b843c749SSergey Zigachev if (adev->irq.ih.ring) {
124b843c749SSergey Zigachev /* add 8 bytes for the rptr/wptr shadows and
125b843c749SSergey Zigachev * add them to the end of the ring allocation.
126b843c749SSergey Zigachev */
127b843c749SSergey Zigachev pci_free_consistent(adev->pdev, adev->irq.ih.ring_size + 8,
128b843c749SSergey Zigachev (void *)adev->irq.ih.ring,
129b843c749SSergey Zigachev adev->irq.ih.rb_dma_addr);
130b843c749SSergey Zigachev adev->irq.ih.ring = NULL;
131b843c749SSergey Zigachev }
132b843c749SSergey Zigachev } else {
133b843c749SSergey Zigachev amdgpu_bo_free_kernel(&adev->irq.ih.ring_obj,
134*78973132SSergey Zigachev (u64 *)&adev->irq.ih.gpu_addr,
135b843c749SSergey Zigachev (void **)&adev->irq.ih.ring);
136b843c749SSergey Zigachev amdgpu_device_wb_free(adev, adev->irq.ih.wptr_offs);
137b843c749SSergey Zigachev amdgpu_device_wb_free(adev, adev->irq.ih.rptr_offs);
138b843c749SSergey Zigachev }
139b843c749SSergey Zigachev }
140b843c749SSergey Zigachev
141b843c749SSergey Zigachev /**
142b843c749SSergey Zigachev * amdgpu_ih_process - interrupt handler
143b843c749SSergey Zigachev *
144b843c749SSergey Zigachev * @adev: amdgpu_device pointer
145b843c749SSergey Zigachev *
146b843c749SSergey Zigachev * Interrupt hander (VI), walk the IH ring.
147b843c749SSergey Zigachev * Returns irq process return code.
148b843c749SSergey Zigachev */
amdgpu_ih_process(struct amdgpu_device * adev)149b843c749SSergey Zigachev int amdgpu_ih_process(struct amdgpu_device *adev)
150b843c749SSergey Zigachev {
151b843c749SSergey Zigachev struct amdgpu_iv_entry entry;
152b843c749SSergey Zigachev u32 wptr;
153b843c749SSergey Zigachev
154b843c749SSergey Zigachev if (!adev->irq.ih.enabled || adev->shutdown)
155b843c749SSergey Zigachev return IRQ_NONE;
156b843c749SSergey Zigachev
157b843c749SSergey Zigachev wptr = amdgpu_ih_get_wptr(adev);
158b843c749SSergey Zigachev
159b843c749SSergey Zigachev restart_ih:
160b843c749SSergey Zigachev /* is somebody else already processing irqs? */
161b843c749SSergey Zigachev if (atomic_xchg(&adev->irq.ih.lock, 1))
162b843c749SSergey Zigachev return IRQ_NONE;
163b843c749SSergey Zigachev
164b843c749SSergey Zigachev DRM_DEBUG("%s: rptr %d, wptr %d\n", __func__, adev->irq.ih.rptr, wptr);
165b843c749SSergey Zigachev
166b843c749SSergey Zigachev /* Order reading of wptr vs. reading of IH ring data */
167b843c749SSergey Zigachev rmb();
168b843c749SSergey Zigachev
169b843c749SSergey Zigachev while (adev->irq.ih.rptr != wptr) {
170b843c749SSergey Zigachev u32 ring_index = adev->irq.ih.rptr >> 2;
171b843c749SSergey Zigachev
172b843c749SSergey Zigachev /* Prescreening of high-frequency interrupts */
173b843c749SSergey Zigachev if (!amdgpu_ih_prescreen_iv(adev)) {
174b843c749SSergey Zigachev adev->irq.ih.rptr &= adev->irq.ih.ptr_mask;
175b843c749SSergey Zigachev continue;
176b843c749SSergey Zigachev }
177b843c749SSergey Zigachev
178b843c749SSergey Zigachev /* Before dispatching irq to IP blocks, send it to amdkfd */
179b843c749SSergey Zigachev amdgpu_amdkfd_interrupt(adev,
180b843c749SSergey Zigachev (const void *) &adev->irq.ih.ring[ring_index]);
181b843c749SSergey Zigachev
182b843c749SSergey Zigachev entry.iv_entry = (const uint32_t *)
183b843c749SSergey Zigachev &adev->irq.ih.ring[ring_index];
184b843c749SSergey Zigachev amdgpu_ih_decode_iv(adev, &entry);
185b843c749SSergey Zigachev adev->irq.ih.rptr &= adev->irq.ih.ptr_mask;
186b843c749SSergey Zigachev
187b843c749SSergey Zigachev amdgpu_irq_dispatch(adev, &entry);
188b843c749SSergey Zigachev }
189b843c749SSergey Zigachev amdgpu_ih_set_rptr(adev);
190b843c749SSergey Zigachev atomic_set(&adev->irq.ih.lock, 0);
191b843c749SSergey Zigachev
192b843c749SSergey Zigachev /* make sure wptr hasn't changed while processing */
193b843c749SSergey Zigachev wptr = amdgpu_ih_get_wptr(adev);
194b843c749SSergey Zigachev if (wptr != adev->irq.ih.rptr)
195b843c749SSergey Zigachev goto restart_ih;
196b843c749SSergey Zigachev
197b843c749SSergey Zigachev return IRQ_HANDLED;
198b843c749SSergey Zigachev }
199b843c749SSergey Zigachev
200b843c749SSergey Zigachev /**
201b843c749SSergey Zigachev * amdgpu_ih_add_fault - Add a page fault record
202b843c749SSergey Zigachev *
203b843c749SSergey Zigachev * @adev: amdgpu device pointer
204b843c749SSergey Zigachev * @key: 64-bit encoding of PASID and address
205b843c749SSergey Zigachev *
206b843c749SSergey Zigachev * This should be called when a retry page fault interrupt is
207b843c749SSergey Zigachev * received. If this is a new page fault, it will be added to a hash
208b843c749SSergey Zigachev * table. The return value indicates whether this is a new fault, or
209b843c749SSergey Zigachev * a fault that was already known and is already being handled.
210b843c749SSergey Zigachev *
211b843c749SSergey Zigachev * If there are too many pending page faults, this will fail. Retry
212b843c749SSergey Zigachev * interrupts should be ignored in this case until there is enough
213b843c749SSergey Zigachev * free space.
214b843c749SSergey Zigachev *
215b843c749SSergey Zigachev * Returns 0 if the fault was added, 1 if the fault was already known,
216b843c749SSergey Zigachev * -ENOSPC if there are too many pending faults.
217b843c749SSergey Zigachev */
amdgpu_ih_add_fault(struct amdgpu_device * adev,u64 key)218b843c749SSergey Zigachev int amdgpu_ih_add_fault(struct amdgpu_device *adev, u64 key)
219b843c749SSergey Zigachev {
220b843c749SSergey Zigachev unsigned long flags;
221b843c749SSergey Zigachev int r = -ENOSPC;
222b843c749SSergey Zigachev
223b843c749SSergey Zigachev if (WARN_ON_ONCE(!adev->irq.ih.faults))
224b843c749SSergey Zigachev /* Should be allocated in <IP>_ih_sw_init on GPUs that
225b843c749SSergey Zigachev * support retry faults and require retry filtering.
226b843c749SSergey Zigachev */
227b843c749SSergey Zigachev return r;
228b843c749SSergey Zigachev
229b843c749SSergey Zigachev spin_lock_irqsave(&adev->irq.ih.faults->lock, flags);
230b843c749SSergey Zigachev
231b843c749SSergey Zigachev /* Only let the hash table fill up to 50% for best performance */
232b843c749SSergey Zigachev if (adev->irq.ih.faults->count >= (1 << (AMDGPU_PAGEFAULT_HASH_BITS-1)))
233b843c749SSergey Zigachev goto unlock_out;
234b843c749SSergey Zigachev
235b843c749SSergey Zigachev r = chash_table_copy_in(&adev->irq.ih.faults->hash, key, NULL);
236b843c749SSergey Zigachev if (!r)
237b843c749SSergey Zigachev adev->irq.ih.faults->count++;
238b843c749SSergey Zigachev
239b843c749SSergey Zigachev /* chash_table_copy_in should never fail unless we're losing count */
240b843c749SSergey Zigachev WARN_ON_ONCE(r < 0);
241b843c749SSergey Zigachev
242b843c749SSergey Zigachev unlock_out:
243b843c749SSergey Zigachev spin_unlock_irqrestore(&adev->irq.ih.faults->lock, flags);
244b843c749SSergey Zigachev return r;
245b843c749SSergey Zigachev }
246b843c749SSergey Zigachev
247b843c749SSergey Zigachev /**
248b843c749SSergey Zigachev * amdgpu_ih_clear_fault - Remove a page fault record
249b843c749SSergey Zigachev *
250b843c749SSergey Zigachev * @adev: amdgpu device pointer
251b843c749SSergey Zigachev * @key: 64-bit encoding of PASID and address
252b843c749SSergey Zigachev *
253b843c749SSergey Zigachev * This should be called when a page fault has been handled. Any
254b843c749SSergey Zigachev * future interrupt with this key will be processed as a new
255b843c749SSergey Zigachev * page fault.
256b843c749SSergey Zigachev */
amdgpu_ih_clear_fault(struct amdgpu_device * adev,u64 key)257b843c749SSergey Zigachev void amdgpu_ih_clear_fault(struct amdgpu_device *adev, u64 key)
258b843c749SSergey Zigachev {
259b843c749SSergey Zigachev unsigned long flags;
260b843c749SSergey Zigachev int r;
261b843c749SSergey Zigachev
262b843c749SSergey Zigachev if (!adev->irq.ih.faults)
263b843c749SSergey Zigachev return;
264b843c749SSergey Zigachev
265b843c749SSergey Zigachev spin_lock_irqsave(&adev->irq.ih.faults->lock, flags);
266b843c749SSergey Zigachev
267b843c749SSergey Zigachev r = chash_table_remove(&adev->irq.ih.faults->hash, key, NULL);
268b843c749SSergey Zigachev if (!WARN_ON_ONCE(r < 0)) {
269b843c749SSergey Zigachev adev->irq.ih.faults->count--;
270b843c749SSergey Zigachev WARN_ON_ONCE(adev->irq.ih.faults->count < 0);
271b843c749SSergey Zigachev }
272b843c749SSergey Zigachev
273b843c749SSergey Zigachev spin_unlock_irqrestore(&adev->irq.ih.faults->lock, flags);
274b843c749SSergey Zigachev }
275