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 goto err_add_consumer;
46 ret = cons->add_producer(cons, prod);
48 goto err_add_producer;
55 if (prod->del_consumer)
56 prod->del_consumer(prod, cons);
61 /* @lock must be held when calling disconnect */
62 static void __disconnect(struct irq_bypass_producer *prod,
63 struct irq_bypass_consumer *cons)
70 cons->del_producer(cons, prod);
72 if (prod->del_consumer)
73 prod->del_consumer(prod, cons);
82 * irq_bypass_register_producer - register IRQ bypass producer
83 * @producer: pointer to producer structure
85 * Add the provided IRQ producer to the list of producers and connect
86 * with any matching token found on the IRQ consumers list.
88 int irq_bypass_register_producer(struct irq_bypass_producer *producer)
90 struct irq_bypass_producer *tmp;
91 struct irq_bypass_consumer *consumer;
99 if (!try_module_get(THIS_MODULE))
104 list_for_each_entry(tmp, &producers, node) {
105 if (tmp->token == producer->token) {
111 list_for_each_entry(consumer, &consumers, node) {
112 if (consumer->token == producer->token) {
113 ret = __connect(producer, consumer);
120 list_add(&producer->node, &producers);
127 module_put(THIS_MODULE);
130 EXPORT_SYMBOL_GPL(irq_bypass_register_producer);
133 * irq_bypass_unregister_producer - unregister IRQ bypass producer
134 * @producer: pointer to producer structure
136 * Remove a previously registered IRQ producer from the list of producers
137 * and disconnect it from any connected IRQ consumer.
139 void irq_bypass_unregister_producer(struct irq_bypass_producer *producer)
141 struct irq_bypass_producer *tmp;
142 struct irq_bypass_consumer *consumer;
144 if (!producer->token)
149 if (!try_module_get(THIS_MODULE))
150 return; /* nothing in the list anyway */
154 list_for_each_entry(tmp, &producers, node) {
155 if (tmp->token != producer->token)
158 list_for_each_entry(consumer, &consumers, node) {
159 if (consumer->token == producer->token) {
160 __disconnect(producer, consumer);
165 list_del(&producer->node);
166 module_put(THIS_MODULE);
172 module_put(THIS_MODULE);
174 EXPORT_SYMBOL_GPL(irq_bypass_unregister_producer);
177 * irq_bypass_register_consumer - register IRQ bypass consumer
178 * @consumer: pointer to consumer structure
180 * Add the provided IRQ consumer to the list of consumers and connect
181 * with any matching token found on the IRQ producer list.
183 int irq_bypass_register_consumer(struct irq_bypass_consumer *consumer)
185 struct irq_bypass_consumer *tmp;
186 struct irq_bypass_producer *producer;
189 if (!consumer->token ||
190 !consumer->add_producer || !consumer->del_producer)
195 if (!try_module_get(THIS_MODULE))
200 list_for_each_entry(tmp, &consumers, node) {
201 if (tmp->token == consumer->token || tmp == consumer) {
207 list_for_each_entry(producer, &producers, node) {
208 if (producer->token == consumer->token) {
209 ret = __connect(producer, consumer);
216 list_add(&consumer->node, &consumers);
223 module_put(THIS_MODULE);
226 EXPORT_SYMBOL_GPL(irq_bypass_register_consumer);
229 * irq_bypass_unregister_consumer - unregister IRQ bypass consumer
230 * @consumer: pointer to consumer structure
232 * Remove a previously registered IRQ consumer from the list of consumers
233 * and disconnect it from any connected IRQ producer.
235 void irq_bypass_unregister_consumer(struct irq_bypass_consumer *consumer)
237 struct irq_bypass_consumer *tmp;
238 struct irq_bypass_producer *producer;
240 if (!consumer->token)
245 if (!try_module_get(THIS_MODULE))
246 return; /* nothing in the list anyway */
250 list_for_each_entry(tmp, &consumers, node) {
254 list_for_each_entry(producer, &producers, node) {
255 if (producer->token == consumer->token) {
256 __disconnect(producer, consumer);
261 list_del(&consumer->node);
262 module_put(THIS_MODULE);
268 module_put(THIS_MODULE);
270 EXPORT_SYMBOL_GPL(irq_bypass_unregister_consumer);