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;
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;
141 if (!try_module_get(THIS_MODULE))
142 return; /* nothing in the list anyway */
146 list_for_each_entry(tmp, &producers, node) {
147 if (tmp->token != producer->token)
150 list_for_each_entry(consumer, &consumers, node) {
151 if (consumer->token == producer->token) {
152 __disconnect(producer, consumer);
157 list_del(&producer->node);
158 module_put(THIS_MODULE);
164 module_put(THIS_MODULE);
166 EXPORT_SYMBOL_GPL(irq_bypass_unregister_producer);
169 * irq_bypass_register_consumer - register IRQ bypass consumer
170 * @consumer: pointer to consumer structure
172 * Add the provided IRQ consumer to the list of consumers and connect
173 * with any matching token found on the IRQ producer list.
175 int irq_bypass_register_consumer(struct irq_bypass_consumer *consumer)
177 struct irq_bypass_consumer *tmp;
178 struct irq_bypass_producer *producer;
180 if (!consumer->add_producer || !consumer->del_producer)
185 if (!try_module_get(THIS_MODULE))
190 list_for_each_entry(tmp, &consumers, node) {
191 if (tmp->token == consumer->token) {
193 module_put(THIS_MODULE);
198 list_for_each_entry(producer, &producers, node) {
199 if (producer->token == consumer->token) {
200 int ret = __connect(producer, consumer);
203 module_put(THIS_MODULE);
210 list_add(&consumer->node, &consumers);
216 EXPORT_SYMBOL_GPL(irq_bypass_register_consumer);
219 * irq_bypass_unregister_consumer - unregister IRQ bypass consumer
220 * @consumer: pointer to consumer structure
222 * Remove a previously registered IRQ consumer from the list of consumers
223 * and disconnect it from any connected IRQ producer.
225 void irq_bypass_unregister_consumer(struct irq_bypass_consumer *consumer)
227 struct irq_bypass_consumer *tmp;
228 struct irq_bypass_producer *producer;
232 if (!try_module_get(THIS_MODULE))
233 return; /* nothing in the list anyway */
237 list_for_each_entry(tmp, &consumers, node) {
238 if (tmp->token != consumer->token)
241 list_for_each_entry(producer, &producers, node) {
242 if (producer->token == consumer->token) {
243 __disconnect(producer, consumer);
248 list_del(&consumer->node);
249 module_put(THIS_MODULE);
255 module_put(THIS_MODULE);
257 EXPORT_SYMBOL_GPL(irq_bypass_unregister_consumer);