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 44 static inline void chainIsr(struct ChainedInterrupt *interrupt, struct ChainedIsr *isr) 45 { 46 interrupt->disable(interrupt); 47 list_add_tail(&interrupt->isrs, &isr->node); 48 interrupt->enable(interrupt); 49 } 50 51 static inline void unchainIsr(struct ChainedInterrupt *interrupt, struct ChainedIsr *isr) 52 { 53 interrupt->disable(interrupt); 54 isr->tid = 0; 55 list_delete(&isr->node); 56 if (!list_is_empty(&interrupt->isrs)) 57 interrupt->enable(interrupt); 58 } 59 60 static inline bool dispatchIsr(struct ChainedInterrupt *interrupt) 61 { 62 struct link_t *cur, *tmp; 63 bool handled = false; 64 uint16_t oldTid = osGetCurrentTid(); 65 66 list_iterate(&interrupt->isrs, cur, tmp) { 67 struct ChainedIsr *curIsr = container_of(cur, struct ChainedIsr, node); 68 osSetCurrentTid(curIsr->tid); 69 handled = curIsr->func(curIsr); 70 if (handled) 71 break; 72 } 73 osSetCurrentTid(oldTid); 74 75 return handled; 76 } 77 78 static inline int unchainIsrAll(struct ChainedInterrupt *interrupt, uint32_t tid) 79 { 80 int count = 0; 81 struct link_t *cur, *tmp; 82 83 list_iterate(&interrupt->isrs, cur, tmp) { 84 struct ChainedIsr *curIsr = container_of(cur, struct ChainedIsr, node); 85 if (curIsr->tid == tid) { 86 unchainIsr(interrupt, curIsr); 87 count++; 88 } 89 } 90 91 return count; 92 } 93 94 static inline uint32_t maxLatencyIsr(struct ChainedInterrupt *interrupt) 95 { 96 struct link_t *cur, *tmp; 97 uint32_t latency = 0; 98 99 list_iterate(&interrupt->isrs, cur, tmp) { 100 struct ChainedIsr *curIsr = container_of(cur, struct ChainedIsr, node); 101 if (!latency || (curIsr->maxLatencyNs && curIsr->maxLatencyNs < latency)) 102 latency = curIsr->maxLatencyNs; 103 } 104 105 return latency; 106 } 107 108 #endif /* __ISR_H */ 109