1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __ISR_H
18 #define __ISR_H
19 
20 #include <stdbool.h>
21 #include <stdint.h>
22 
23 #include <cpu.h>
24 #include <list.h>
25 #include <util.h>
26 #include <seos.h>
27 
28 struct ChainedInterrupt {
29     link_t isrs;
30 
31     void (*const enable)(struct ChainedInterrupt *);
32     void (*const disable)(struct ChainedInterrupt *);
33 };
34 
35 struct ChainedIsr {
36     link_t node;
37 
38     uint32_t maxLatencyNs;
39 
40     bool (*func)(struct ChainedIsr *);
41     uint16_t tid;
42 };
43 
chainIsr(struct ChainedInterrupt * interrupt,struct ChainedIsr * isr)44 static inline void chainIsr(struct ChainedInterrupt *interrupt, struct ChainedIsr *isr)
45 {
46     interrupt->disable(interrupt);
47     isr->tid = osGetCurrentTid();
48     list_add_tail(&interrupt->isrs, &isr->node);
49     interrupt->enable(interrupt);
50 }
51 
unchainIsr(struct ChainedInterrupt * interrupt,struct ChainedIsr * isr)52 static inline void unchainIsr(struct ChainedInterrupt *interrupt, struct ChainedIsr *isr)
53 {
54     interrupt->disable(interrupt);
55     isr->tid = 0;
56     list_delete(&isr->node);
57     if (!list_is_empty(&interrupt->isrs))
58         interrupt->enable(interrupt);
59 }
60 
dispatchIsr(struct ChainedInterrupt * interrupt)61 static inline bool dispatchIsr(struct ChainedInterrupt *interrupt)
62 {
63     struct link_t *cur, *tmp;
64     bool handled = false;
65     uint16_t oldTid = osGetCurrentTid();
66 
67     list_iterate(&interrupt->isrs, cur, tmp) {
68         struct ChainedIsr *curIsr = container_of(cur, struct ChainedIsr, node);
69         osSetCurrentTid(curIsr->tid);
70         handled = curIsr->func(curIsr);
71         if (handled)
72             break;
73     }
74     osSetCurrentTid(oldTid);
75 
76     return handled;
77 }
78 
unchainIsrAll(struct ChainedInterrupt * interrupt,uint32_t tid)79 static inline int unchainIsrAll(struct ChainedInterrupt *interrupt, uint32_t tid)
80 {
81     int count = 0;
82     struct link_t *cur, *tmp;
83 
84     list_iterate(&interrupt->isrs, cur, tmp) {
85         struct ChainedIsr *curIsr = container_of(cur, struct ChainedIsr, node);
86         if (curIsr->tid == tid) {
87             unchainIsr(interrupt, curIsr);
88             count++;
89         }
90     }
91 
92     return count;
93 }
94 
maxLatencyIsr(struct ChainedInterrupt * interrupt)95 static inline uint32_t maxLatencyIsr(struct ChainedInterrupt *interrupt)
96 {
97     struct link_t *cur, *tmp;
98     uint32_t latency = 0;
99 
100     list_iterate(&interrupt->isrs, cur, tmp) {
101         struct ChainedIsr *curIsr = container_of(cur, struct ChainedIsr, node);
102         if (!latency || (curIsr->maxLatencyNs && curIsr->maxLatencyNs < latency))
103             latency = curIsr->maxLatencyNs;
104     }
105 
106     return latency;
107 }
108 
109 #endif /* __ISR_H */
110