blob: 92bd8b924c07dc5ec9467d02f63d1420724dbd59 [file] [log] [blame]
Ashutosh Joshi29a28c42016-01-04 12:21:06 -08001/*
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
Greg Hackmannda3c42a2015-06-08 12:54:49 -070017#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>
Alexey Polyudovc36763d2016-05-05 10:39:01 -070026#include <seos.h>
Greg Hackmannda3c42a2015-06-08 12:54:49 -070027
28struct ChainedInterrupt {
29 link_t isrs;
30
31 void (*const enable)(struct ChainedInterrupt *);
32 void (*const disable)(struct ChainedInterrupt *);
33};
34
35struct ChainedIsr {
36 link_t node;
Ben Fennema8efc9b92016-09-14 15:51:06 -070037
38 uint32_t maxLatencyNs;
39
Greg Hackmann0189e632015-06-09 10:45:55 -070040 bool (*func)(struct ChainedIsr *);
Alexey Polyudovc36763d2016-05-05 10:39:01 -070041 uint16_t tid;
Greg Hackmannda3c42a2015-06-08 12:54:49 -070042};
43
44static inline void chainIsr(struct ChainedInterrupt *interrupt, struct ChainedIsr *isr)
45{
46 interrupt->disable(interrupt);
Ben Fennemaf62fa5a2017-02-15 00:28:24 -080047 isr->tid = osGetCurrentTid();
Greg Hackmannda3c42a2015-06-08 12:54:49 -070048 list_add_tail(&interrupt->isrs, &isr->node);
49 interrupt->enable(interrupt);
50}
51
52static inline void unchainIsr(struct ChainedInterrupt *interrupt, struct ChainedIsr *isr)
53{
54 interrupt->disable(interrupt);
Alexey Polyudovc36763d2016-05-05 10:39:01 -070055 isr->tid = 0;
Greg Hackmannda3c42a2015-06-08 12:54:49 -070056 list_delete(&isr->node);
57 if (!list_is_empty(&interrupt->isrs))
58 interrupt->enable(interrupt);
59}
60
61static inline bool dispatchIsr(struct ChainedInterrupt *interrupt)
62{
63 struct link_t *cur, *tmp;
64 bool handled = false;
Alexey Polyudovc36763d2016-05-05 10:39:01 -070065 uint16_t oldTid = osGetCurrentTid();
Greg Hackmannda3c42a2015-06-08 12:54:49 -070066
67 list_iterate(&interrupt->isrs, cur, tmp) {
68 struct ChainedIsr *curIsr = container_of(cur, struct ChainedIsr, node);
Alexey Polyudovc36763d2016-05-05 10:39:01 -070069 osSetCurrentTid(curIsr->tid);
70 handled = curIsr->func(curIsr);
71 if (handled)
72 break;
Greg Hackmannda3c42a2015-06-08 12:54:49 -070073 }
Alexey Polyudovc36763d2016-05-05 10:39:01 -070074 osSetCurrentTid(oldTid);
Greg Hackmannda3c42a2015-06-08 12:54:49 -070075
76 return handled;
77}
78
Alexey Polyudovc36763d2016-05-05 10:39:01 -070079static 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
Ben Fennema8efc9b92016-09-14 15:51:06 -070095static 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
Greg Hackmannda3c42a2015-06-08 12:54:49 -0700109#endif /* __ISR_H */