Ashutosh Joshi | 29a28c4 | 2016-01-04 12:21:06 -0800 | [diff] [blame] | 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 | |
Greg Hackmann | da3c42a | 2015-06-08 12:54:49 -0700 | [diff] [blame] | 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> |
Alexey Polyudov | c36763d | 2016-05-05 10:39:01 -0700 | [diff] [blame] | 26 | #include <seos.h> |
Greg Hackmann | da3c42a | 2015-06-08 12:54:49 -0700 | [diff] [blame] | 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; |
Ben Fennema | 8efc9b9 | 2016-09-14 15:51:06 -0700 | [diff] [blame] | 37 | |
| 38 | uint32_t maxLatencyNs; |
| 39 | |
Greg Hackmann | 0189e63 | 2015-06-09 10:45:55 -0700 | [diff] [blame] | 40 | bool (*func)(struct ChainedIsr *); |
Alexey Polyudov | c36763d | 2016-05-05 10:39:01 -0700 | [diff] [blame] | 41 | uint16_t tid; |
Greg Hackmann | da3c42a | 2015-06-08 12:54:49 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | static inline void chainIsr(struct ChainedInterrupt *interrupt, struct ChainedIsr *isr) |
| 45 | { |
| 46 | interrupt->disable(interrupt); |
Ben Fennema | f62fa5a | 2017-02-15 00:28:24 -0800 | [diff] [blame] | 47 | isr->tid = osGetCurrentTid(); |
Greg Hackmann | da3c42a | 2015-06-08 12:54:49 -0700 | [diff] [blame] | 48 | list_add_tail(&interrupt->isrs, &isr->node); |
| 49 | interrupt->enable(interrupt); |
| 50 | } |
| 51 | |
| 52 | static inline void unchainIsr(struct ChainedInterrupt *interrupt, struct ChainedIsr *isr) |
| 53 | { |
| 54 | interrupt->disable(interrupt); |
Alexey Polyudov | c36763d | 2016-05-05 10:39:01 -0700 | [diff] [blame] | 55 | isr->tid = 0; |
Greg Hackmann | da3c42a | 2015-06-08 12:54:49 -0700 | [diff] [blame] | 56 | list_delete(&isr->node); |
| 57 | if (!list_is_empty(&interrupt->isrs)) |
| 58 | interrupt->enable(interrupt); |
| 59 | } |
| 60 | |
| 61 | static inline bool dispatchIsr(struct ChainedInterrupt *interrupt) |
| 62 | { |
| 63 | struct link_t *cur, *tmp; |
| 64 | bool handled = false; |
Alexey Polyudov | c36763d | 2016-05-05 10:39:01 -0700 | [diff] [blame] | 65 | uint16_t oldTid = osGetCurrentTid(); |
Greg Hackmann | da3c42a | 2015-06-08 12:54:49 -0700 | [diff] [blame] | 66 | |
| 67 | list_iterate(&interrupt->isrs, cur, tmp) { |
| 68 | struct ChainedIsr *curIsr = container_of(cur, struct ChainedIsr, node); |
Alexey Polyudov | c36763d | 2016-05-05 10:39:01 -0700 | [diff] [blame] | 69 | osSetCurrentTid(curIsr->tid); |
| 70 | handled = curIsr->func(curIsr); |
| 71 | if (handled) |
| 72 | break; |
Greg Hackmann | da3c42a | 2015-06-08 12:54:49 -0700 | [diff] [blame] | 73 | } |
Alexey Polyudov | c36763d | 2016-05-05 10:39:01 -0700 | [diff] [blame] | 74 | osSetCurrentTid(oldTid); |
Greg Hackmann | da3c42a | 2015-06-08 12:54:49 -0700 | [diff] [blame] | 75 | |
| 76 | return handled; |
| 77 | } |
| 78 | |
Alexey Polyudov | c36763d | 2016-05-05 10:39:01 -0700 | [diff] [blame] | 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 | |
Ben Fennema | 8efc9b9 | 2016-09-14 15:51:06 -0700 | [diff] [blame] | 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 | |
Greg Hackmann | da3c42a | 2015-06-08 12:54:49 -0700 | [diff] [blame] | 109 | #endif /* __ISR_H */ |