1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * |
---|
6 | * @brief NIOS2 Exception and Interrupt Handler |
---|
7 | * |
---|
8 | * @note Derived from c4x/irq.c |
---|
9 | */ |
---|
10 | |
---|
11 | /* |
---|
12 | * COPYRIGHT (c) 1989-2007. |
---|
13 | * On-Line Applications Research Corporation (OAR). |
---|
14 | * |
---|
15 | * Redistribution and use in source and binary forms, with or without |
---|
16 | * modification, are permitted provided that the following conditions |
---|
17 | * are met: |
---|
18 | * 1. Redistributions of source code must retain the above copyright |
---|
19 | * notice, this list of conditions and the following disclaimer. |
---|
20 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
21 | * notice, this list of conditions and the following disclaimer in the |
---|
22 | * documentation and/or other materials provided with the distribution. |
---|
23 | * |
---|
24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
28 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
29 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
30 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
31 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
32 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
34 | * POSSIBILITY OF SUCH DAMAGE. |
---|
35 | */ |
---|
36 | |
---|
37 | #ifdef HAVE_CONFIG_H |
---|
38 | #include "config.h" |
---|
39 | #endif |
---|
40 | |
---|
41 | #include <rtems/score/cpu.h> |
---|
42 | #include <rtems/score/isr.h> |
---|
43 | #include <rtems/score/threaddispatch.h> |
---|
44 | #include <rtems/score/nios2-utility.h> |
---|
45 | #include <rtems/score/interr.h> |
---|
46 | |
---|
47 | /* |
---|
48 | * This routine provides the RTEMS interrupt management. |
---|
49 | * |
---|
50 | * Upon entry, interrupts are disabled |
---|
51 | */ |
---|
52 | |
---|
53 | unsigned long *_old_stack_ptr; |
---|
54 | |
---|
55 | /* |
---|
56 | * Prototypes |
---|
57 | */ |
---|
58 | void __ISR_Handler(void); |
---|
59 | void __Exception_Handler(CPU_Exception_frame *efr); |
---|
60 | |
---|
61 | register unsigned long *stack_ptr __asm__ ("sp"); |
---|
62 | |
---|
63 | static inline void __IIC_Handler(void) |
---|
64 | { |
---|
65 | uint32_t active; |
---|
66 | uint32_t mask; |
---|
67 | uint32_t vector; |
---|
68 | |
---|
69 | /* |
---|
70 | * Obtain from the interrupt controller a bit list of pending interrupts, |
---|
71 | * and then process the highest priority interrupt. This process loops, |
---|
72 | * loading the active interrupt list on each pass until ipending |
---|
73 | * return zero. |
---|
74 | * |
---|
75 | * The maximum interrupt latency for the highest priority interrupt is |
---|
76 | * reduced by finding out which interrupts are pending as late as possible. |
---|
77 | * Consider the case where the high priority interupt is asserted during |
---|
78 | * the interrupt entry sequence for a lower priority interrupt to see why |
---|
79 | * this is the case. |
---|
80 | */ |
---|
81 | |
---|
82 | active = _Nios2_Get_ctlreg_ipending(); |
---|
83 | |
---|
84 | while (active) |
---|
85 | { |
---|
86 | vector = 0; |
---|
87 | mask = 1; |
---|
88 | |
---|
89 | /* |
---|
90 | * Test each bit in turn looking for an active interrupt. Once one is |
---|
91 | * found call it to clear the interrupt condition. |
---|
92 | */ |
---|
93 | |
---|
94 | while (active) |
---|
95 | { |
---|
96 | if (active & mask) |
---|
97 | { |
---|
98 | if ( _ISR_Vector_table[ vector] ) |
---|
99 | (*_ISR_Vector_table[ vector ])(vector); |
---|
100 | active &= ~mask; |
---|
101 | } |
---|
102 | mask <<= 1; |
---|
103 | ++vector; |
---|
104 | }; |
---|
105 | |
---|
106 | active = _Nios2_Get_ctlreg_ipending(); |
---|
107 | } |
---|
108 | |
---|
109 | } |
---|
110 | |
---|
111 | void __ISR_Handler(void) |
---|
112 | { |
---|
113 | register uint32_t level; |
---|
114 | |
---|
115 | /* Interrupts are disabled upon entry to this Handler */ |
---|
116 | |
---|
117 | if ( _ISR_Nest_level == 0 ) { |
---|
118 | /* Install irq stack */ |
---|
119 | _old_stack_ptr = stack_ptr; |
---|
120 | stack_ptr = _CPU_Interrupt_stack_high - 4; |
---|
121 | } |
---|
122 | |
---|
123 | _ISR_Nest_level++; |
---|
124 | |
---|
125 | _Thread_Dispatch_disable(); |
---|
126 | |
---|
127 | __IIC_Handler(); |
---|
128 | |
---|
129 | /* Make sure that interrupts are disabled again */ |
---|
130 | _CPU_ISR_Disable( level ); |
---|
131 | |
---|
132 | _Thread_Dispatch_unnest( _Per_CPU_Get() ); |
---|
133 | |
---|
134 | _ISR_Nest_level--; |
---|
135 | |
---|
136 | if( _ISR_Nest_level == 0) { |
---|
137 | stack_ptr = _old_stack_ptr; |
---|
138 | |
---|
139 | if( _Thread_Dispatch_is_enabled() ) |
---|
140 | { |
---|
141 | if ( _Thread_Dispatch_necessary ) { |
---|
142 | _CPU_ISR_Enable( level ); |
---|
143 | _Thread_Dispatch(); |
---|
144 | /* may have switched to another task and not return here immed. */ |
---|
145 | _CPU_ISR_Disable( level ); /* Keep _pairs_ of Enable/Disable */ |
---|
146 | } |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | _CPU_ISR_Enable( level ); |
---|
151 | } |
---|
152 | |
---|
153 | void __Exception_Handler(CPU_Exception_frame *efr) |
---|
154 | { |
---|
155 | _CPU_Fatal_halt(RTEMS_FATAL_SOURCE_EXCEPTION, 0xECC0); /* source ignored */ |
---|
156 | } |
---|