xref: /optee_os/core/kernel/interrupt.c (revision 51ac0e23b5c2b3c84469a0de79c9f027a46d5747)
1 /*
2  * Copyright (c) 2016, Linaro Limited
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <kernel/interrupt.h>
29 #include <trace.h>
30 
31 /*
32  * NOTE!
33  *
34  * We're assuming that there's no concurrent use of this interface, except
35  * delivery of interrupts in parallel. Synchronization will be needed when
36  * we begin to modify settings after boot initialization.
37  */
38 
39 static struct itr_chip *itr_chip;
40 static SLIST_HEAD(, itr_handler) handlers = SLIST_HEAD_INITIALIZER(handlers);
41 
42 void itr_init(struct itr_chip *chip)
43 {
44 	itr_chip = chip;
45 }
46 
47 static struct itr_handler *find_handler(size_t it)
48 {
49 	struct itr_handler *h;
50 
51 	SLIST_FOREACH(h, &handlers, link)
52 		if (h->it == it)
53 			return h;
54 	return NULL;
55 }
56 
57 void itr_handle(size_t it)
58 {
59 	struct itr_handler *h = find_handler(it);
60 
61 	if (!h) {
62 		EMSG("Disabling unhandled interrupt %zu", it);
63 		itr_chip->ops->disable(itr_chip, it);
64 		return;
65 	}
66 
67 	if (h->handler(h) != ITRR_HANDLED) {
68 		EMSG("Disabling interrupt %zu not handled by handler", it);
69 		itr_chip->ops->disable(itr_chip, it);
70 	}
71 }
72 
73 void itr_add(struct itr_handler *h)
74 {
75 	itr_chip->ops->add(itr_chip, h->it, h->flags);
76 	SLIST_INSERT_HEAD(&handlers, h, link);
77 }
78 
79 void itr_enable(struct itr_handler *h)
80 {
81 	itr_chip->ops->enable(itr_chip, h->it);
82 }
83 
84 void itr_disable(struct itr_handler *h)
85 {
86 	itr_chip->ops->disable(itr_chip, h->it);
87 }
88