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 
50 static void interrupt_wq(struct work_struct *);
51 
52 int 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 	spin_lock_init(&kfd->interrupt_lock);
65 
66 	INIT_WORK(&kfd->interrupt_work, interrupt_wq);
67 
68 	kfd->interrupts_active = true;
69 
70 	/*
71 	 * After this function returns, the interrupt will be enabled. This
72 	 * barrier ensures that the interrupt running on a different processor
73 	 * sees all the above writes.
74 	 */
75 	smp_wmb();
76 
77 	return 0;
78 }
79 
80 void kfd_interrupt_exit(struct kfd_dev *kfd)
81 {
82 	/*
83 	 * Stop the interrupt handler from writing to the ring and scheduling
84 	 * workqueue items. The spinlock ensures that any interrupt running
85 	 * after we have unlocked sees interrupts_active = false.
86 	 */
87 	unsigned long flags;
88 
89 	spin_lock_irqsave(&kfd->interrupt_lock, flags);
90 	kfd->interrupts_active = false;
91 	spin_unlock_irqrestore(&kfd->interrupt_lock, flags);
92 
93 	/*
94 	 * Flush_scheduled_work ensures that there are no outstanding
95 	 * work-queue items that will access interrupt_ring. New work items
96 	 * can't be created because we stopped interrupt handling above.
97 	 */
98 	flush_scheduled_work();
99 
100 	kfifo_free(&kfd->ih_fifo);
101 }
102 
103 /*
104  * Assumption: single reader/writer. This function is not re-entrant
105  */
106 bool enqueue_ih_ring_entry(struct kfd_dev *kfd,	const void *ih_ring_entry)
107 {
108 	int count;
109 
110 	count = kfifo_in(&kfd->ih_fifo, ih_ring_entry,
111 				kfd->device_info->ih_ring_entry_size);
112 	if (count != kfd->device_info->ih_ring_entry_size) {
113 		dev_err_ratelimited(kfd_chardev(),
114 			"Interrupt ring overflow, dropping interrupt %d\n",
115 			count);
116 		return false;
117 	}
118 
119 	return true;
120 }
121 
122 /*
123  * Assumption: single reader/writer. This function is not re-entrant
124  */
125 static bool dequeue_ih_ring_entry(struct kfd_dev *kfd, void *ih_ring_entry)
126 {
127 	int count;
128 
129 	count = kfifo_out(&kfd->ih_fifo, ih_ring_entry,
130 				kfd->device_info->ih_ring_entry_size);
131 
132 	WARN_ON(count && count != kfd->device_info->ih_ring_entry_size);
133 
134 	return count == kfd->device_info->ih_ring_entry_size;
135 }
136 
137 static void interrupt_wq(struct work_struct *work)
138 {
139 	struct kfd_dev *dev = container_of(work, struct kfd_dev,
140 						interrupt_work);
141 
142 	uint32_t ih_ring_entry[DIV_ROUND_UP(
143 				dev->device_info->ih_ring_entry_size,
144 				sizeof(uint32_t))];
145 
146 	while (dequeue_ih_ring_entry(dev, ih_ring_entry))
147 		dev->device_info->event_interrupt_class->interrupt_wq(dev,
148 								ih_ring_entry);
149 }
150 
151 bool interrupt_is_wanted(struct kfd_dev *dev, const uint32_t *ih_ring_entry)
152 {
153 	/* integer and bitwise OR so there is no boolean short-circuiting */
154 	unsigned int wanted = 0;
155 
156 	wanted |= dev->device_info->event_interrupt_class->interrupt_isr(dev,
157 								ih_ring_entry);
158 
159 	return wanted != 0;
160 }
161