drm/amdkfd: Introduce kfd_node struct (v5)
[linux-block.git] / drivers / gpu / drm / amd / amdkfd / kfd_interrupt.c
CommitLineData
d87f36a0 1// SPDX-License-Identifier: GPL-2.0 OR MIT
2249d558 2/*
d87f36a0 3 * Copyright 2014-2022 Advanced Micro Devices, Inc.
2249d558
AL
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24/*
25 * KFD Interrupts.
26 *
27 * AMD GPUs deliver interrupts by pushing an interrupt description onto the
28 * interrupt ring and then sending an interrupt. KGD receives the interrupt
29 * in ISR and sends us a pointer to each new entry on the interrupt ring.
30 *
31 * We generally can't process interrupt-signaled events from ISR, so we call
32 * out to each interrupt client module (currently only the scheduler) to ask if
33 * each interrupt is interesting. If they return true, then it requires further
34 * processing so we copy it to an internal interrupt ring and call each
35 * interrupt client again from a work-queue.
36 *
37 * There's no acknowledgment for the interrupts we use. The hardware simply
38 * queues a new interrupt each time without waiting.
39 *
40 * The fixed-size internal queue means that it's possible for us to lose
41 * interrupts because we have no back-pressure to the hardware.
42 */
43
44#include <linux/slab.h>
45#include <linux/device.h>
04ad47bd 46#include <linux/kfifo.h>
2249d558
AL
47#include "kfd_priv.h"
48
27232055 49#define KFD_IH_NUM_ENTRIES 8192
2249d558
AL
50
51static void interrupt_wq(struct work_struct *);
52
8dc1db31 53int kfd_interrupt_init(struct kfd_node *node)
2249d558 54{
04ad47bd
AR
55 int r;
56
8dc1db31
MJ
57 r = kfifo_alloc(&node->ih_fifo,
58 KFD_IH_NUM_ENTRIES * node->kfd->device_info.ih_ring_entry_size,
04ad47bd
AR
59 GFP_KERNEL);
60 if (r) {
8dc1db31 61 dev_err(node->adev->dev, "Failed to allocate IH fifo\n");
04ad47bd
AR
62 return r;
63 }
2249d558 64
8dc1db31
MJ
65 node->ih_wq = alloc_workqueue("KFD IH", WQ_HIGHPRI, 1);
66 if (unlikely(!node->ih_wq)) {
67 kfifo_free(&node->ih_fifo);
68 dev_err(node->adev->dev, "Failed to allocate KFD IH workqueue\n");
81de29d8
AP
69 return -ENOMEM;
70 }
8dc1db31 71 spin_lock_init(&node->interrupt_lock);
2249d558 72
8dc1db31 73 INIT_WORK(&node->interrupt_work, interrupt_wq);
2249d558 74
8dc1db31 75 node->interrupts_active = true;
2249d558
AL
76
77 /*
78 * After this function returns, the interrupt will be enabled. This
79 * barrier ensures that the interrupt running on a different processor
80 * sees all the above writes.
81 */
82 smp_wmb();
83
84 return 0;
85}
86
8dc1db31 87void kfd_interrupt_exit(struct kfd_node *node)
2249d558
AL
88{
89 /*
90 * Stop the interrupt handler from writing to the ring and scheduling
91 * workqueue items. The spinlock ensures that any interrupt running
92 * after we have unlocked sees interrupts_active = false.
93 */
94 unsigned long flags;
95
8dc1db31
MJ
96 spin_lock_irqsave(&node->interrupt_lock, flags);
97 node->interrupts_active = false;
98 spin_unlock_irqrestore(&node->interrupt_lock, flags);
2249d558
AL
99
100 /*
0f875e3f 101 * flush_work ensures that there are no outstanding
2249d558
AL
102 * work-queue items that will access interrupt_ring. New work items
103 * can't be created because we stopped interrupt handling above.
104 */
8dc1db31 105 flush_workqueue(node->ih_wq);
2249d558 106
8dc1db31 107 kfifo_free(&node->ih_fifo);
2249d558
AL
108}
109
110/*
04ad47bd 111 * Assumption: single reader/writer. This function is not re-entrant
2249d558 112 */
8dc1db31 113bool enqueue_ih_ring_entry(struct kfd_node *node, const void *ih_ring_entry)
2249d558 114{
04ad47bd 115 int count;
2249d558 116
8dc1db31
MJ
117 count = kfifo_in(&node->ih_fifo, ih_ring_entry,
118 node->kfd->device_info.ih_ring_entry_size);
119 if (count != node->kfd->device_info.ih_ring_entry_size) {
120 dev_dbg_ratelimited(node->adev->dev,
04ad47bd
AR
121 "Interrupt ring overflow, dropping interrupt %d\n",
122 count);
2249d558
AL
123 return false;
124 }
125
2249d558
AL
126 return true;
127}
128
129/*
04ad47bd 130 * Assumption: single reader/writer. This function is not re-entrant
2249d558 131 */
8dc1db31 132static bool dequeue_ih_ring_entry(struct kfd_node *node, void *ih_ring_entry)
2249d558 133{
04ad47bd 134 int count;
2249d558 135
8dc1db31
MJ
136 count = kfifo_out(&node->ih_fifo, ih_ring_entry,
137 node->kfd->device_info.ih_ring_entry_size);
2249d558 138
8dc1db31 139 WARN_ON(count && count != node->kfd->device_info.ih_ring_entry_size);
2249d558 140
8dc1db31 141 return count == node->kfd->device_info.ih_ring_entry_size;
2249d558
AL
142}
143
144static void interrupt_wq(struct work_struct *work)
145{
8dc1db31 146 struct kfd_node *dev = container_of(work, struct kfd_node,
2249d558 147 interrupt_work);
af47b390 148 uint32_t ih_ring_entry[KFD_MAX_RING_ENTRY_SIZE];
dcfe584b 149 unsigned long start_jiffies = jiffies;
2249d558 150
8dc1db31 151 if (dev->kfd->device_info.ih_ring_entry_size > sizeof(ih_ring_entry)) {
a0c5fd46 152 dev_err_once(dev->adev->dev, "Ring entry too small\n");
af47b390
LA
153 return;
154 }
2249d558 155
5273e82c 156 while (dequeue_ih_ring_entry(dev, ih_ring_entry)) {
8dc1db31 157 dev->kfd->device_info.event_interrupt_class->interrupt_wq(dev,
f3a39818 158 ih_ring_entry);
dcfe584b 159 if (time_is_before_jiffies(start_jiffies + HZ)) {
5273e82c
FK
160 /* If we spent more than a second processing signals,
161 * reschedule the worker to avoid soft-lockup warnings
162 */
163 queue_work(dev->ih_wq, &dev->interrupt_work);
164 break;
165 }
166 }
2249d558
AL
167}
168
8dc1db31 169bool interrupt_is_wanted(struct kfd_node *dev,
58e69886
LX
170 const uint32_t *ih_ring_entry,
171 uint32_t *patched_ihre, bool *flag)
2249d558 172{
f3a39818 173 /* integer and bitwise OR so there is no boolean short-circuiting */
8eabaf54 174 unsigned int wanted = 0;
f3a39818 175
8dc1db31 176 wanted |= dev->kfd->device_info.event_interrupt_class->interrupt_isr(dev,
58e69886 177 ih_ring_entry, patched_ihre, flag);
f3a39818
AL
178
179 return wanted != 0;
2249d558 180}