Loading...
1/*
2 * Copyright 2014 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/*
24 * KFD Interrupts.
25 *
26 * AMD GPUs deliver interrupts by pushing an interrupt description onto the
27 * interrupt ring and then sending an interrupt. KGD receives the interrupt
28 * in ISR and sends us a pointer to each new entry on the interrupt ring.
29 *
30 * We generally can't process interrupt-signaled events from ISR, so we call
31 * out to each interrupt client module (currently only the scheduler) to ask if
32 * each interrupt is interesting. If they return true, then it requires further
33 * processing so we copy it to an internal interrupt ring and call each
34 * interrupt client again from a work-queue.
35 *
36 * There's no acknowledgment for the interrupts we use. The hardware simply
37 * queues a new interrupt each time without waiting.
38 *
39 * The fixed-size internal queue means that it's possible for us to lose
40 * interrupts because we have no back-pressure to the hardware.
41 */
42
43#include <linux/slab.h>
44#include <linux/device.h>
45#include <linux/kfifo.h>
46#include "kfd_priv.h"
47
48#define KFD_IH_NUM_ENTRIES 8192
49
50static void interrupt_wq(struct work_struct *);
51
52int kfd_interrupt_init(struct kfd_dev *kfd)
53{
54 int r;
55
56 r = kfifo_alloc(&kfd->ih_fifo,
57 KFD_IH_NUM_ENTRIES * kfd->device_info->ih_ring_entry_size,
58 GFP_KERNEL);
59 if (r) {
60 dev_err(kfd_chardev(), "Failed to allocate IH fifo\n");
61 return r;
62 }
63
64 kfd->ih_wq = alloc_workqueue("KFD IH", WQ_HIGHPRI, 1);
65 if (unlikely(!kfd->ih_wq)) {
66 kfifo_free(&kfd->ih_fifo);
67 dev_err(kfd_chardev(), "Failed to allocate KFD IH workqueue\n");
68 return -ENOMEM;
69 }
70 spin_lock_init(&kfd->interrupt_lock);
71
72 INIT_WORK(&kfd->interrupt_work, interrupt_wq);
73
74 kfd->interrupts_active = true;
75
76 /*
77 * After this function returns, the interrupt will be enabled. This
78 * barrier ensures that the interrupt running on a different processor
79 * sees all the above writes.
80 */
81 smp_wmb();
82
83 return 0;
84}
85
86void kfd_interrupt_exit(struct kfd_dev *kfd)
87{
88 /*
89 * Stop the interrupt handler from writing to the ring and scheduling
90 * workqueue items. The spinlock ensures that any interrupt running
91 * after we have unlocked sees interrupts_active = false.
92 */
93 unsigned long flags;
94
95 spin_lock_irqsave(&kfd->interrupt_lock, flags);
96 kfd->interrupts_active = false;
97 spin_unlock_irqrestore(&kfd->interrupt_lock, flags);
98
99 /*
100 * flush_work ensures that there are no outstanding
101 * work-queue items that will access interrupt_ring. New work items
102 * can't be created because we stopped interrupt handling above.
103 */
104 flush_workqueue(kfd->ih_wq);
105
106 kfifo_free(&kfd->ih_fifo);
107}
108
109/*
110 * Assumption: single reader/writer. This function is not re-entrant
111 */
112bool enqueue_ih_ring_entry(struct kfd_dev *kfd, const void *ih_ring_entry)
113{
114 int count;
115
116 count = kfifo_in(&kfd->ih_fifo, ih_ring_entry,
117 kfd->device_info->ih_ring_entry_size);
118 if (count != kfd->device_info->ih_ring_entry_size) {
119 dev_err_ratelimited(kfd_chardev(),
120 "Interrupt ring overflow, dropping interrupt %d\n",
121 count);
122 return false;
123 }
124
125 return true;
126}
127
128/*
129 * Assumption: single reader/writer. This function is not re-entrant
130 */
131static bool dequeue_ih_ring_entry(struct kfd_dev *kfd, void *ih_ring_entry)
132{
133 int count;
134
135 count = kfifo_out(&kfd->ih_fifo, ih_ring_entry,
136 kfd->device_info->ih_ring_entry_size);
137
138 WARN_ON(count && count != kfd->device_info->ih_ring_entry_size);
139
140 return count == kfd->device_info->ih_ring_entry_size;
141}
142
143static void interrupt_wq(struct work_struct *work)
144{
145 struct kfd_dev *dev = container_of(work, struct kfd_dev,
146 interrupt_work);
147 uint32_t ih_ring_entry[KFD_MAX_RING_ENTRY_SIZE];
148
149 if (dev->device_info->ih_ring_entry_size > sizeof(ih_ring_entry)) {
150 dev_err_once(kfd_chardev(), "Ring entry too small\n");
151 return;
152 }
153
154 while (dequeue_ih_ring_entry(dev, ih_ring_entry))
155 dev->device_info->event_interrupt_class->interrupt_wq(dev,
156 ih_ring_entry);
157}
158
159bool interrupt_is_wanted(struct kfd_dev *dev,
160 const uint32_t *ih_ring_entry,
161 uint32_t *patched_ihre, bool *flag)
162{
163 /* integer and bitwise OR so there is no boolean short-circuiting */
164 unsigned int wanted = 0;
165
166 wanted |= dev->device_info->event_interrupt_class->interrupt_isr(dev,
167 ih_ring_entry, patched_ihre, flag);
168
169 return wanted != 0;
170}
1// SPDX-License-Identifier: GPL-2.0 OR MIT
2/*
3 * Copyright 2014-2022 Advanced Micro Devices, Inc.
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>
46#include <linux/kfifo.h>
47#include "kfd_priv.h"
48
49#define KFD_IH_NUM_ENTRIES 8192
50
51static void interrupt_wq(struct work_struct *);
52
53int kfd_interrupt_init(struct kfd_node *node)
54{
55 int r;
56
57 r = kfifo_alloc(&node->ih_fifo,
58 KFD_IH_NUM_ENTRIES * node->kfd->device_info.ih_ring_entry_size,
59 GFP_KERNEL);
60 if (r) {
61 dev_err(node->adev->dev, "Failed to allocate IH fifo\n");
62 return r;
63 }
64
65 if (!node->kfd->ih_wq) {
66 node->kfd->ih_wq = alloc_workqueue("KFD IH", WQ_HIGHPRI | WQ_UNBOUND,
67 node->kfd->num_nodes);
68 if (unlikely(!node->kfd->ih_wq)) {
69 kfifo_free(&node->ih_fifo);
70 dev_err(node->adev->dev, "Failed to allocate KFD IH workqueue\n");
71 return -ENOMEM;
72 }
73 }
74 spin_lock_init(&node->interrupt_lock);
75
76 INIT_WORK(&node->interrupt_work, interrupt_wq);
77
78 node->interrupts_active = true;
79
80 /*
81 * After this function returns, the interrupt will be enabled. This
82 * barrier ensures that the interrupt running on a different processor
83 * sees all the above writes.
84 */
85 smp_wmb();
86
87 return 0;
88}
89
90void kfd_interrupt_exit(struct kfd_node *node)
91{
92 /*
93 * Stop the interrupt handler from writing to the ring and scheduling
94 * workqueue items. The spinlock ensures that any interrupt running
95 * after we have unlocked sees interrupts_active = false.
96 */
97 unsigned long flags;
98
99 spin_lock_irqsave(&node->interrupt_lock, flags);
100 node->interrupts_active = false;
101 spin_unlock_irqrestore(&node->interrupt_lock, flags);
102 kfifo_free(&node->ih_fifo);
103}
104
105/*
106 * Assumption: single reader/writer. This function is not re-entrant
107 */
108bool enqueue_ih_ring_entry(struct kfd_node *node, const void *ih_ring_entry)
109{
110 int count;
111
112 count = kfifo_in(&node->ih_fifo, ih_ring_entry,
113 node->kfd->device_info.ih_ring_entry_size);
114 if (count != node->kfd->device_info.ih_ring_entry_size) {
115 dev_dbg_ratelimited(node->adev->dev,
116 "Interrupt ring overflow, dropping interrupt %d\n",
117 count);
118 return false;
119 }
120
121 return true;
122}
123
124/*
125 * Assumption: single reader/writer. This function is not re-entrant
126 */
127static bool dequeue_ih_ring_entry(struct kfd_node *node, void *ih_ring_entry)
128{
129 int count;
130
131 count = kfifo_out(&node->ih_fifo, ih_ring_entry,
132 node->kfd->device_info.ih_ring_entry_size);
133
134 WARN_ON(count && count != node->kfd->device_info.ih_ring_entry_size);
135
136 return count == node->kfd->device_info.ih_ring_entry_size;
137}
138
139static void interrupt_wq(struct work_struct *work)
140{
141 struct kfd_node *dev = container_of(work, struct kfd_node,
142 interrupt_work);
143 uint32_t ih_ring_entry[KFD_MAX_RING_ENTRY_SIZE];
144 unsigned long start_jiffies = jiffies;
145
146 if (dev->kfd->device_info.ih_ring_entry_size > sizeof(ih_ring_entry)) {
147 dev_err_once(dev->adev->dev, "Ring entry too small\n");
148 return;
149 }
150
151 while (dequeue_ih_ring_entry(dev, ih_ring_entry)) {
152 dev->kfd->device_info.event_interrupt_class->interrupt_wq(dev,
153 ih_ring_entry);
154 if (time_is_before_jiffies(start_jiffies + HZ)) {
155 /* If we spent more than a second processing signals,
156 * reschedule the worker to avoid soft-lockup warnings
157 */
158 queue_work(dev->kfd->ih_wq, &dev->interrupt_work);
159 break;
160 }
161 }
162}
163
164bool interrupt_is_wanted(struct kfd_node *dev,
165 const uint32_t *ih_ring_entry,
166 uint32_t *patched_ihre, bool *flag)
167{
168 /* integer and bitwise OR so there is no boolean short-circuiting */
169 unsigned int wanted = 0;
170
171 wanted |= dev->kfd->device_info.event_interrupt_class->interrupt_isr(dev,
172 ih_ring_entry, patched_ihre, flag);
173
174 return wanted != 0;
175}