Revert "tty: hvc: Fix data abort due to race in hvc_open"
[linux/fpc-iii.git] / virt / lib / irqbypass.c
blob28fda42e471bbdabfcf3a4ebad772cf9317e320f
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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
14 * bypass.
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)
33 int ret = 0;
35 if (prod->stop)
36 prod->stop(prod);
37 if (cons->stop)
38 cons->stop(cons);
40 if (prod->add_consumer)
41 ret = prod->add_consumer(prod, cons);
43 if (!ret) {
44 ret = cons->add_producer(cons, prod);
45 if (ret && prod->del_consumer)
46 prod->del_consumer(prod, cons);
49 if (cons->start)
50 cons->start(cons);
51 if (prod->start)
52 prod->start(prod);
54 return ret;
57 /* @lock must be held when calling disconnect */
58 static void __disconnect(struct irq_bypass_producer *prod,
59 struct irq_bypass_consumer *cons)
61 if (prod->stop)
62 prod->stop(prod);
63 if (cons->stop)
64 cons->stop(cons);
66 cons->del_producer(cons, prod);
68 if (prod->del_consumer)
69 prod->del_consumer(prod, cons);
71 if (cons->start)
72 cons->start(cons);
73 if (prod->start)
74 prod->start(prod);
77 /**
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;
88 int ret;
90 if (!producer->token)
91 return -EINVAL;
93 might_sleep();
95 if (!try_module_get(THIS_MODULE))
96 return -ENODEV;
98 mutex_lock(&lock);
100 list_for_each_entry(tmp, &producers, node) {
101 if (tmp->token == producer->token) {
102 ret = -EBUSY;
103 goto out_err;
107 list_for_each_entry(consumer, &consumers, node) {
108 if (consumer->token == producer->token) {
109 ret = __connect(producer, consumer);
110 if (ret)
111 goto out_err;
112 break;
116 list_add(&producer->node, &producers);
118 mutex_unlock(&lock);
120 return 0;
121 out_err:
122 mutex_unlock(&lock);
123 module_put(THIS_MODULE);
124 return ret;
126 EXPORT_SYMBOL_GPL(irq_bypass_register_producer);
129 * irq_bypass_unregister_producer - unregister IRQ bypass producer
130 * @producer: pointer to producer structure
132 * Remove a previously registered IRQ producer from the list of producers
133 * and disconnect it from any connected IRQ consumer.
135 void irq_bypass_unregister_producer(struct irq_bypass_producer *producer)
137 struct irq_bypass_producer *tmp;
138 struct irq_bypass_consumer *consumer;
140 if (!producer->token)
141 return;
143 might_sleep();
145 if (!try_module_get(THIS_MODULE))
146 return; /* nothing in the list anyway */
148 mutex_lock(&lock);
150 list_for_each_entry(tmp, &producers, node) {
151 if (tmp->token != producer->token)
152 continue;
154 list_for_each_entry(consumer, &consumers, node) {
155 if (consumer->token == producer->token) {
156 __disconnect(producer, consumer);
157 break;
161 list_del(&producer->node);
162 module_put(THIS_MODULE);
163 break;
166 mutex_unlock(&lock);
168 module_put(THIS_MODULE);
170 EXPORT_SYMBOL_GPL(irq_bypass_unregister_producer);
173 * irq_bypass_register_consumer - register IRQ bypass consumer
174 * @consumer: pointer to consumer structure
176 * Add the provided IRQ consumer to the list of consumers and connect
177 * with any matching token found on the IRQ producer list.
179 int irq_bypass_register_consumer(struct irq_bypass_consumer *consumer)
181 struct irq_bypass_consumer *tmp;
182 struct irq_bypass_producer *producer;
183 int ret;
185 if (!consumer->token ||
186 !consumer->add_producer || !consumer->del_producer)
187 return -EINVAL;
189 might_sleep();
191 if (!try_module_get(THIS_MODULE))
192 return -ENODEV;
194 mutex_lock(&lock);
196 list_for_each_entry(tmp, &consumers, node) {
197 if (tmp->token == consumer->token || tmp == consumer) {
198 ret = -EBUSY;
199 goto out_err;
203 list_for_each_entry(producer, &producers, node) {
204 if (producer->token == consumer->token) {
205 ret = __connect(producer, consumer);
206 if (ret)
207 goto out_err;
208 break;
212 list_add(&consumer->node, &consumers);
214 mutex_unlock(&lock);
216 return 0;
217 out_err:
218 mutex_unlock(&lock);
219 module_put(THIS_MODULE);
220 return ret;
222 EXPORT_SYMBOL_GPL(irq_bypass_register_consumer);
225 * irq_bypass_unregister_consumer - unregister IRQ bypass consumer
226 * @consumer: pointer to consumer structure
228 * Remove a previously registered IRQ consumer from the list of consumers
229 * and disconnect it from any connected IRQ producer.
231 void irq_bypass_unregister_consumer(struct irq_bypass_consumer *consumer)
233 struct irq_bypass_consumer *tmp;
234 struct irq_bypass_producer *producer;
236 if (!consumer->token)
237 return;
239 might_sleep();
241 if (!try_module_get(THIS_MODULE))
242 return; /* nothing in the list anyway */
244 mutex_lock(&lock);
246 list_for_each_entry(tmp, &consumers, node) {
247 if (tmp != consumer)
248 continue;
250 list_for_each_entry(producer, &producers, node) {
251 if (producer->token == consumer->token) {
252 __disconnect(producer, consumer);
253 break;
257 list_del(&consumer->node);
258 module_put(THIS_MODULE);
259 break;
262 mutex_unlock(&lock);
264 module_put(THIS_MODULE);
266 EXPORT_SYMBOL_GPL(irq_bypass_unregister_consumer);