1 // SPDX-License-Identifier: GPL-2.0-only
3 * IRQ offload/bypass manager
5 * Copyright (C) 2015 Red Hat, Inc.
6 * Copyright (c) 2015 Linaro Ltd.
8 * 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
17 #include <linux/irqbypass.h>
18 #include <linux/list.h>
19 #include <linux/module.h>
20 #include <linux/mutex.h>
22 MODULE_LICENSE("GPL v2");
23 MODULE_DESCRIPTION("IRQ bypass manager utility module");
25 static LIST_HEAD(producers
);
26 static LIST_HEAD(consumers
);
27 static DEFINE_MUTEX(lock
);
29 /* @lock must be held when calling connect */
30 static int __connect(struct irq_bypass_producer
*prod
,
31 struct irq_bypass_consumer
*cons
)
40 if (prod
->add_consumer
)
41 ret
= prod
->add_consumer(prod
, cons
);
44 ret
= cons
->add_producer(cons
, prod
);
45 if (ret
&& prod
->del_consumer
)
46 prod
->del_consumer(prod
, cons
);
57 /* @lock must be held when calling disconnect */
58 static void __disconnect(struct irq_bypass_producer
*prod
,
59 struct irq_bypass_consumer
*cons
)
66 cons
->del_producer(cons
, prod
);
68 if (prod
->del_consumer
)
69 prod
->del_consumer(prod
, cons
);
78 * irq_bypass_register_producer - register IRQ bypass producer
79 * @producer: pointer to producer structure
81 * Add the provided IRQ producer to the list of producers and connect
82 * with any matching token found on the IRQ consumers list.
84 int irq_bypass_register_producer(struct irq_bypass_producer
*producer
)
86 struct irq_bypass_producer
*tmp
;
87 struct irq_bypass_consumer
*consumer
;
94 if (!try_module_get(THIS_MODULE
))
99 list_for_each_entry(tmp
, &producers
, node
) {
100 if (tmp
->token
== producer
->token
) {
102 module_put(THIS_MODULE
);
107 list_for_each_entry(consumer
, &consumers
, node
) {
108 if (consumer
->token
== producer
->token
) {
109 int ret
= __connect(producer
, consumer
);
112 module_put(THIS_MODULE
);
119 list_add(&producer
->node
, &producers
);
125 EXPORT_SYMBOL_GPL(irq_bypass_register_producer
);
128 * irq_bypass_unregister_producer - unregister IRQ bypass producer
129 * @producer: pointer to producer structure
131 * Remove a previously registered IRQ producer from the list of producers
132 * and disconnect it from any connected IRQ consumer.
134 void irq_bypass_unregister_producer(struct irq_bypass_producer
*producer
)
136 struct irq_bypass_producer
*tmp
;
137 struct irq_bypass_consumer
*consumer
;
139 if (!producer
->token
)
144 if (!try_module_get(THIS_MODULE
))
145 return; /* nothing in the list anyway */
149 list_for_each_entry(tmp
, &producers
, node
) {
150 if (tmp
->token
!= producer
->token
)
153 list_for_each_entry(consumer
, &consumers
, node
) {
154 if (consumer
->token
== producer
->token
) {
155 __disconnect(producer
, consumer
);
160 list_del(&producer
->node
);
161 module_put(THIS_MODULE
);
167 module_put(THIS_MODULE
);
169 EXPORT_SYMBOL_GPL(irq_bypass_unregister_producer
);
172 * irq_bypass_register_consumer - register IRQ bypass consumer
173 * @consumer: pointer to consumer structure
175 * Add the provided IRQ consumer to the list of consumers and connect
176 * with any matching token found on the IRQ producer list.
178 int irq_bypass_register_consumer(struct irq_bypass_consumer
*consumer
)
180 struct irq_bypass_consumer
*tmp
;
181 struct irq_bypass_producer
*producer
;
183 if (!consumer
->token
||
184 !consumer
->add_producer
|| !consumer
->del_producer
)
189 if (!try_module_get(THIS_MODULE
))
194 list_for_each_entry(tmp
, &consumers
, node
) {
195 if (tmp
->token
== consumer
->token
|| tmp
== consumer
) {
197 module_put(THIS_MODULE
);
202 list_for_each_entry(producer
, &producers
, node
) {
203 if (producer
->token
== consumer
->token
) {
204 int ret
= __connect(producer
, consumer
);
207 module_put(THIS_MODULE
);
214 list_add(&consumer
->node
, &consumers
);
220 EXPORT_SYMBOL_GPL(irq_bypass_register_consumer
);
223 * irq_bypass_unregister_consumer - unregister IRQ bypass consumer
224 * @consumer: pointer to consumer structure
226 * Remove a previously registered IRQ consumer from the list of consumers
227 * and disconnect it from any connected IRQ producer.
229 void irq_bypass_unregister_consumer(struct irq_bypass_consumer
*consumer
)
231 struct irq_bypass_consumer
*tmp
;
232 struct irq_bypass_producer
*producer
;
234 if (!consumer
->token
)
239 if (!try_module_get(THIS_MODULE
))
240 return; /* nothing in the list anyway */
244 list_for_each_entry(tmp
, &consumers
, node
) {
248 list_for_each_entry(producer
, &producers
, node
) {
249 if (producer
->token
== consumer
->token
) {
250 __disconnect(producer
, consumer
);
255 list_del(&consumer
->node
);
256 module_put(THIS_MODULE
);
262 module_put(THIS_MODULE
);
264 EXPORT_SYMBOL_GPL(irq_bypass_unregister_consumer
);