blob: cb0c801b3bc2360520acdc4119f463119430f55f [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Alex Williamsonf73f8172015-09-18 22:29:39 +08002/*
3 * IRQ offload/bypass manager
4 *
5 * Copyright (C) 2015 Red Hat, Inc.
6 * Copyright (c) 2015 Linaro Ltd.
7 *
Alex Williamsonf73f8172015-09-18 22:29:39 +08008 * Various virtualization hardware acceleration techniques allow bypassing or
9 * offloading interrupts received from devices around the host kernel. Posted
10 * Interrupts on Intel VT-d systems can allow interrupts to be received
11 * directly by a virtual machine. ARM IRQ Forwarding allows forwarded physical
12 * interrupts to be directly deactivated by the guest. This manager allows
13 * interrupt producers and consumers to find each other to enable this sort of
14 * bypass.
15 */
16
17#include <linux/irqbypass.h>
18#include <linux/list.h>
19#include <linux/module.h>
20#include <linux/mutex.h>
21
22MODULE_LICENSE("GPL v2");
23MODULE_DESCRIPTION("IRQ bypass manager utility module");
24
25static LIST_HEAD(producers);
26static LIST_HEAD(consumers);
27static DEFINE_MUTEX(lock);
28
29/* @lock must be held when calling connect */
30static int __connect(struct irq_bypass_producer *prod,
31 struct irq_bypass_consumer *cons)
32{
33 int ret = 0;
34
35 if (prod->stop)
36 prod->stop(prod);
37 if (cons->stop)
38 cons->stop(cons);
39
40 if (prod->add_consumer)
41 ret = prod->add_consumer(prod, cons);
42
43 if (!ret) {
44 ret = cons->add_producer(cons, prod);
45 if (ret && prod->del_consumer)
46 prod->del_consumer(prod, cons);
47 }
48
49 if (cons->start)
50 cons->start(cons);
51 if (prod->start)
52 prod->start(prod);
53
54 return ret;
55}
56
57/* @lock must be held when calling disconnect */
58static void __disconnect(struct irq_bypass_producer *prod,
59 struct irq_bypass_consumer *cons)
60{
61 if (prod->stop)
62 prod->stop(prod);
63 if (cons->stop)
64 cons->stop(cons);
65
66 cons->del_producer(cons, prod);
67
68 if (prod->del_consumer)
69 prod->del_consumer(prod, cons);
70
71 if (cons->start)
72 cons->start(cons);
73 if (prod->start)
74 prod->start(prod);
75}
76
77/**
78 * irq_bypass_register_producer - register IRQ bypass producer
79 * @producer: pointer to producer structure
80 *
81 * Add the provided IRQ producer to the list of producers and connect
82 * with any matching token found on the IRQ consumers list.
83 */
84int irq_bypass_register_producer(struct irq_bypass_producer *producer)
85{
86 struct irq_bypass_producer *tmp;
87 struct irq_bypass_consumer *consumer;
88
Alex Williamsonb52f3ed2016-05-05 11:58:29 -060089 if (!producer->token)
90 return -EINVAL;
91
Alex Williamsonf73f8172015-09-18 22:29:39 +080092 might_sleep();
93
94 if (!try_module_get(THIS_MODULE))
95 return -ENODEV;
96
97 mutex_lock(&lock);
98
99 list_for_each_entry(tmp, &producers, node) {
100 if (tmp->token == producer->token) {
101 mutex_unlock(&lock);
102 module_put(THIS_MODULE);
103 return -EBUSY;
104 }
105 }
106
107 list_for_each_entry(consumer, &consumers, node) {
108 if (consumer->token == producer->token) {
109 int ret = __connect(producer, consumer);
110 if (ret) {
111 mutex_unlock(&lock);
112 module_put(THIS_MODULE);
113 return ret;
114 }
115 break;
116 }
117 }
118
119 list_add(&producer->node, &producers);
120
121 mutex_unlock(&lock);
122
123 return 0;
124}
125EXPORT_SYMBOL_GPL(irq_bypass_register_producer);
126
127/**
128 * irq_bypass_unregister_producer - unregister IRQ bypass producer
129 * @producer: pointer to producer structure
130 *
131 * Remove a previously registered IRQ producer from the list of producers
132 * and disconnect it from any connected IRQ consumer.
133 */
134void irq_bypass_unregister_producer(struct irq_bypass_producer *producer)
135{
136 struct irq_bypass_producer *tmp;
137 struct irq_bypass_consumer *consumer;
138
Alex Williamsonb52f3ed2016-05-05 11:58:29 -0600139 if (!producer->token)
140 return;
141
Alex Williamsonf73f8172015-09-18 22:29:39 +0800142 might_sleep();
143
144 if (!try_module_get(THIS_MODULE))
145 return; /* nothing in the list anyway */
146
147 mutex_lock(&lock);
148
149 list_for_each_entry(tmp, &producers, node) {
150 if (tmp->token != producer->token)
151 continue;
152
153 list_for_each_entry(consumer, &consumers, node) {
154 if (consumer->token == producer->token) {
155 __disconnect(producer, consumer);
156 break;
157 }
158 }
159
160 list_del(&producer->node);
161 module_put(THIS_MODULE);
162 break;
163 }
164
165 mutex_unlock(&lock);
166
167 module_put(THIS_MODULE);
168}
169EXPORT_SYMBOL_GPL(irq_bypass_unregister_producer);
170
171/**
172 * irq_bypass_register_consumer - register IRQ bypass consumer
173 * @consumer: pointer to consumer structure
174 *
175 * Add the provided IRQ consumer to the list of consumers and connect
176 * with any matching token found on the IRQ producer list.
177 */
178int irq_bypass_register_consumer(struct irq_bypass_consumer *consumer)
179{
180 struct irq_bypass_consumer *tmp;
181 struct irq_bypass_producer *producer;
Miaohe Lin8262fe82019-12-06 10:53:52 +0800182 int ret;
Alex Williamsonf73f8172015-09-18 22:29:39 +0800183
Alex Williamsonb52f3ed2016-05-05 11:58:29 -0600184 if (!consumer->token ||
185 !consumer->add_producer || !consumer->del_producer)
Alex Williamsonf73f8172015-09-18 22:29:39 +0800186 return -EINVAL;
187
188 might_sleep();
189
190 if (!try_module_get(THIS_MODULE))
191 return -ENODEV;
192
193 mutex_lock(&lock);
194
195 list_for_each_entry(tmp, &consumers, node) {
Wanpeng Li4f3dbdf2017-01-05 17:39:42 -0800196 if (tmp->token == consumer->token || tmp == consumer) {
Miaohe Lin8262fe82019-12-06 10:53:52 +0800197 ret = -EBUSY;
198 goto out_err;
Alex Williamsonf73f8172015-09-18 22:29:39 +0800199 }
200 }
201
202 list_for_each_entry(producer, &producers, node) {
203 if (producer->token == consumer->token) {
Miaohe Lin8262fe82019-12-06 10:53:52 +0800204 ret = __connect(producer, consumer);
205 if (ret)
206 goto out_err;
Alex Williamsonf73f8172015-09-18 22:29:39 +0800207 break;
208 }
209 }
210
211 list_add(&consumer->node, &consumers);
212
213 mutex_unlock(&lock);
214
215 return 0;
Miaohe Lin8262fe82019-12-06 10:53:52 +0800216out_err:
217 mutex_unlock(&lock);
218 module_put(THIS_MODULE);
219 return ret;
Alex Williamsonf73f8172015-09-18 22:29:39 +0800220}
221EXPORT_SYMBOL_GPL(irq_bypass_register_consumer);
222
223/**
224 * irq_bypass_unregister_consumer - unregister IRQ bypass consumer
225 * @consumer: pointer to consumer structure
226 *
227 * Remove a previously registered IRQ consumer from the list of consumers
228 * and disconnect it from any connected IRQ producer.
229 */
230void irq_bypass_unregister_consumer(struct irq_bypass_consumer *consumer)
231{
232 struct irq_bypass_consumer *tmp;
233 struct irq_bypass_producer *producer;
234
Alex Williamsonb52f3ed2016-05-05 11:58:29 -0600235 if (!consumer->token)
236 return;
237
Alex Williamsonf73f8172015-09-18 22:29:39 +0800238 might_sleep();
239
240 if (!try_module_get(THIS_MODULE))
241 return; /* nothing in the list anyway */
242
243 mutex_lock(&lock);
244
245 list_for_each_entry(tmp, &consumers, node) {
Wanpeng Li4f3dbdf2017-01-05 17:39:42 -0800246 if (tmp != consumer)
Alex Williamsonf73f8172015-09-18 22:29:39 +0800247 continue;
248
249 list_for_each_entry(producer, &producers, node) {
250 if (producer->token == consumer->token) {
251 __disconnect(producer, consumer);
252 break;
253 }
254 }
255
256 list_del(&consumer->node);
257 module_put(THIS_MODULE);
258 break;
259 }
260
261 mutex_unlock(&lock);
262
263 module_put(THIS_MODULE);
264}
265EXPORT_SYMBOL_GPL(irq_bypass_unregister_consumer);