2 * IRQ offload/bypass manager
4 * Copyright (C) 2015 Red Hat, Inc.
5 * Copyright (c) 2015 Linaro Ltd.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * Various virtualization hardware acceleration techniques allow bypassing or
12 * offloading interrupts received from devices around the host kernel. Posted
13 * Interrupts on Intel VT-d systems can allow interrupts to be received
14 * directly by a virtual machine. ARM IRQ Forwarding allows forwarded physical
15 * interrupts to be directly deactivated by the guest. This manager allows
16 * interrupt producers and consumers to find each other to enable this sort of
20 #include <linux/irqbypass.h>
21 #include <linux/list.h>
22 #include <linux/module.h>
23 #include <linux/mutex.h>
25 MODULE_LICENSE("GPL v2");
26 MODULE_DESCRIPTION("IRQ bypass manager utility module");
28 static LIST_HEAD(producers
);
29 static LIST_HEAD(consumers
);
30 static DEFINE_MUTEX(lock
);
32 /* @lock must be held when calling connect */
33 static int __connect(struct irq_bypass_producer
*prod
,
34 struct irq_bypass_consumer
*cons
)
43 if (prod
->add_consumer
)
44 ret
= prod
->add_consumer(prod
, cons
);
47 ret
= cons
->add_producer(cons
, prod
);
48 if (ret
&& prod
->del_consumer
)
49 prod
->del_consumer(prod
, cons
);
60 /* @lock must be held when calling disconnect */
61 static void __disconnect(struct irq_bypass_producer
*prod
,
62 struct irq_bypass_consumer
*cons
)
69 cons
->del_producer(cons
, prod
);
71 if (prod
->del_consumer
)
72 prod
->del_consumer(prod
, cons
);
81 * irq_bypass_register_producer - register IRQ bypass producer
82 * @producer: pointer to producer structure
84 * Add the provided IRQ producer to the list of producers and connect
85 * with any matching token found on the IRQ consumers list.
87 int irq_bypass_register_producer(struct irq_bypass_producer
*producer
)
89 struct irq_bypass_producer
*tmp
;
90 struct irq_bypass_consumer
*consumer
;
97 if (!try_module_get(THIS_MODULE
))
102 list_for_each_entry(tmp
, &producers
, node
) {
103 if (tmp
->token
== producer
->token
) {
105 module_put(THIS_MODULE
);
110 list_for_each_entry(consumer
, &consumers
, node
) {
111 if (consumer
->token
== producer
->token
) {
112 int ret
= __connect(producer
, consumer
);
115 module_put(THIS_MODULE
);
122 list_add(&producer
->node
, &producers
);
128 EXPORT_SYMBOL_GPL(irq_bypass_register_producer
);
131 * irq_bypass_unregister_producer - unregister IRQ bypass producer
132 * @producer: pointer to producer structure
134 * Remove a previously registered IRQ producer from the list of producers
135 * and disconnect it from any connected IRQ consumer.
137 void irq_bypass_unregister_producer(struct irq_bypass_producer
*producer
)
139 struct irq_bypass_producer
*tmp
;
140 struct irq_bypass_consumer
*consumer
;
142 if (!producer
->token
)
147 if (!try_module_get(THIS_MODULE
))
148 return; /* nothing in the list anyway */
152 list_for_each_entry(tmp
, &producers
, node
) {
153 if (tmp
->token
!= producer
->token
)
156 list_for_each_entry(consumer
, &consumers
, node
) {
157 if (consumer
->token
== producer
->token
) {
158 __disconnect(producer
, consumer
);
163 list_del(&producer
->node
);
164 module_put(THIS_MODULE
);
170 module_put(THIS_MODULE
);
172 EXPORT_SYMBOL_GPL(irq_bypass_unregister_producer
);
175 * irq_bypass_register_consumer - register IRQ bypass consumer
176 * @consumer: pointer to consumer structure
178 * Add the provided IRQ consumer to the list of consumers and connect
179 * with any matching token found on the IRQ producer list.
181 int irq_bypass_register_consumer(struct irq_bypass_consumer
*consumer
)
183 struct irq_bypass_consumer
*tmp
;
184 struct irq_bypass_producer
*producer
;
186 if (!consumer
->token
||
187 !consumer
->add_producer
|| !consumer
->del_producer
)
192 if (!try_module_get(THIS_MODULE
))
197 list_for_each_entry(tmp
, &consumers
, node
) {
198 if (tmp
->token
== consumer
->token
|| tmp
== consumer
) {
200 module_put(THIS_MODULE
);
205 list_for_each_entry(producer
, &producers
, node
) {
206 if (producer
->token
== consumer
->token
) {
207 int ret
= __connect(producer
, consumer
);
210 module_put(THIS_MODULE
);
217 list_add(&consumer
->node
, &consumers
);
223 EXPORT_SYMBOL_GPL(irq_bypass_register_consumer
);
226 * irq_bypass_unregister_consumer - unregister IRQ bypass consumer
227 * @consumer: pointer to consumer structure
229 * Remove a previously registered IRQ consumer from the list of consumers
230 * and disconnect it from any connected IRQ producer.
232 void irq_bypass_unregister_consumer(struct irq_bypass_consumer
*consumer
)
234 struct irq_bypass_consumer
*tmp
;
235 struct irq_bypass_producer
*producer
;
237 if (!consumer
->token
)
242 if (!try_module_get(THIS_MODULE
))
243 return; /* nothing in the list anyway */
247 list_for_each_entry(tmp
, &consumers
, node
) {
251 list_for_each_entry(producer
, &producers
, node
) {
252 if (producer
->token
== consumer
->token
) {
253 __disconnect(producer
, consumer
);
258 list_del(&consumer
->node
);
259 module_put(THIS_MODULE
);
265 module_put(THIS_MODULE
);
267 EXPORT_SYMBOL_GPL(irq_bypass_unregister_consumer
);