1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
|
/****************************************************************************
* arch/x86/src/i486/up_irq.c
* arch/x86/src/chip/up_irq.c
*
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <string.h>
#include <debug.h>
#include <nuttx/irq.h>
#include <nuttx/arch.h>
#include <arch/irq.h>
#include <arch/io.h>
#include "up_arch.h"
#include "os_internal.h"
#include "up_internal.h"
#include "qemu_internal.h"
/****************************************************************************
* Definitions
****************************************************************************/
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
static void idt_outb(uint8_t val, uint16_t addr) __attribute__((noinline));
static void up_remappic(void);
static void up_idtentry(unsigned int index, uint32_t base, uint16_t sel,
uint8_t flags);
static inline void up_idtinit(void);
/****************************************************************************
* Public Data
****************************************************************************/
volatile uint32_t *current_regs;
/****************************************************************************
* Private Data
****************************************************************************/
static struct idt_entry_s idt_entries[256];
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name idt_outb
*
* Description:
* A slightly slower version of outb
*
****************************************************************************/
static void idt_outb(uint8_t val, uint16_t addr)
{
outb(val, addr);
}
/****************************************************************************
* Name up_remappic
*
* Description:
* Remap the PIC. The Programmable Interrupt Controller (PIC) is used to
* combine several sources of interrupt onto one or more CPU lines, while
* allowing priority levels to be assigned to its interrupt outputs. When
* the device has multiple interrupt outputs to assert, it will assert them
* in the order of their relative priority.
*
****************************************************************************/
static void up_remappic(void)
{
/* Mask interrupts from PIC */
idt_outb(PIC1_IMR_ALL, PIC1_IMR);
idt_outb(PIC2_IMR_ALL, PIC2_IMR);
/* If the PIC has been reset, it must be initialized with 2 to 4 Initialization
* Command Words (ICW) before it will accept and process Interrupt Requests. The
* following outlines the four possible Initialization Command Words.
*/
/* Remap the irq table for primary:
*
* ICW1 - We will be sending ICW4
* ICW2 - Address
* ICW3 */
idt_outb(PIC_ICW1_ICW4|PIC_ICW1_ICW1, PIC1_ICW1);
idt_outb(0x20, PIC1_ICW2);
idt_outb(PIC1_ICW3_IRQ2, PIC1_ICW3);
idt_outb(PIC_ICW4_808xMODE, PIC1_ICW4);
/* Remap irq for slave */
idt_outb(PIC_ICW1_ICW4|PIC_ICW1_ICW1, PIC2_ICW1);
idt_outb(0x28, PIC2_ICW2);
idt_outb(PIC_ICW3_SID2, PIC2_ICW3);
idt_outb(PIC_ICW4_808xMODE, PIC2_ICW4);
/* Mask interrupts from PIC */
idt_outb(PIC1_IMR_ALL, PIC1_IMR);
idt_outb(PIC2_IMR_ALL, PIC2_IMR);
}
/****************************************************************************
* Name up_idtentry
*
* Description:
* Initialize one IDT entry.
*
****************************************************************************/
static void up_idtentry(unsigned int index, uint32_t base, uint16_t sel,
uint8_t flags)
{
struct idt_entry_s *entry = &idt_entries[index];
entry->lobase = base & 0xffff;
entry->hibase = (base >> 16) & 0xffff;
entry->sel = sel;
entry->zero = 0;
/* We must uncomment the OR below when we get to using user-mode. It sets the
* interrupt gate's privilege level to 3.
*/
entry->flags = flags /* | 0x60 */;
}
/****************************************************************************
* Name up_idtinit
*
* Description:
* Initialize the IDT. The Interrupt Descriptor Table (IDT) is a data
* structure used by the x86 architecture to implement an interrupt vector
* table. The IDT is used by the processor to determine the correct
* response to interrupts and exceptions.
*
****************************************************************************/
static inline void up_idtinit(void)
{
struct idt_ptr_s idt_ptr;
idt_ptr.limit = sizeof(struct idt_entry_s) * 256 - 1;
idt_ptr.base = (uint32_t)&idt_entries;
memset(&idt_entries, 0, sizeof(struct idt_entry_s)*256);
/* Re-map the PIC */
up_remappic();
/* Set each ISR/IRQ to the appropriate vector with selector=8 and with
* 32-bit interrupt gate. Interrupt gate (vs. trap gate) will leave
* interrupts enabled when the IRS/IRQ handler is entered.
*/
up_idtentry(ISR0, (uint32_t)vector_isr0 , 0x08, 0x8e);
up_idtentry(ISR1, (uint32_t)vector_isr1 , 0x08, 0x8e);
up_idtentry(ISR2, (uint32_t)vector_isr2 , 0x08, 0x8e);
up_idtentry(ISR3, (uint32_t)vector_isr3 , 0x08, 0x8e);
up_idtentry(ISR4, (uint32_t)vector_isr4 , 0x08, 0x8e);
up_idtentry(ISR5, (uint32_t)vector_isr5 , 0x08, 0x8e);
up_idtentry(ISR6, (uint32_t)vector_isr6 , 0x08, 0x8e);
up_idtentry(ISR7, (uint32_t)vector_isr7 , 0x08, 0x8e);
up_idtentry(ISR8, (uint32_t)vector_isr8 , 0x08, 0x8e);
up_idtentry(ISR9, (uint32_t)vector_isr9 , 0x08, 0x8e);
up_idtentry(ISR10, (uint32_t)vector_isr10, 0x08, 0x8e);
up_idtentry(ISR11, (uint32_t)vector_isr11, 0x08, 0x8e);
up_idtentry(ISR12, (uint32_t)vector_isr12, 0x08, 0x8e);
up_idtentry(ISR13, (uint32_t)vector_isr13, 0x08, 0x8e);
up_idtentry(ISR14, (uint32_t)vector_isr14, 0x08, 0x8e);
up_idtentry(ISR15, (uint32_t)vector_isr15, 0x08, 0x8e);
up_idtentry(ISR16, (uint32_t)vector_isr16, 0x08, 0x8e);
up_idtentry(ISR17, (uint32_t)vector_isr17, 0x08, 0x8e);
up_idtentry(ISR18, (uint32_t)vector_isr18, 0x08, 0x8e);
up_idtentry(ISR19, (uint32_t)vector_isr19, 0x08, 0x8e);
up_idtentry(ISR20, (uint32_t)vector_isr20, 0x08, 0x8e);
up_idtentry(ISR21, (uint32_t)vector_isr21, 0x08, 0x8e);
up_idtentry(ISR22, (uint32_t)vector_isr22, 0x08, 0x8e);
up_idtentry(ISR23, (uint32_t)vector_isr23, 0x08, 0x8e);
up_idtentry(ISR24, (uint32_t)vector_isr24, 0x08, 0x8e);
up_idtentry(ISR25, (uint32_t)vector_isr25, 0x08, 0x8e);
up_idtentry(ISR26, (uint32_t)vector_isr26, 0x08, 0x8e);
up_idtentry(ISR27, (uint32_t)vector_isr27, 0x08, 0x8e);
up_idtentry(ISR28, (uint32_t)vector_isr28, 0x08, 0x8e);
up_idtentry(ISR29, (uint32_t)vector_isr29, 0x08, 0x8e);
up_idtentry(ISR30, (uint32_t)vector_isr30, 0x08, 0x8e);
up_idtentry(ISR31, (uint32_t)vector_isr31, 0x08, 0x8e);
up_idtentry(IRQ0, (uint32_t)vector_irq0, 0x08, 0x8e);
up_idtentry(IRQ1, (uint32_t)vector_irq1, 0x08, 0x8e);
up_idtentry(IRQ2, (uint32_t)vector_irq2, 0x08, 0x8e);
up_idtentry(IRQ3, (uint32_t)vector_irq3, 0x08, 0x8e);
up_idtentry(IRQ4, (uint32_t)vector_irq4, 0x08, 0x8e);
up_idtentry(IRQ5, (uint32_t)vector_irq5, 0x08, 0x8e);
up_idtentry(IRQ6, (uint32_t)vector_irq6, 0x08, 0x8e);
up_idtentry(IRQ7, (uint32_t)vector_irq7, 0x08, 0x8e);
up_idtentry(IRQ8, (uint32_t)vector_irq8, 0x08, 0x8e);
up_idtentry(IRQ9, (uint32_t)vector_irq9, 0x08, 0x8e);
up_idtentry(IRQ10, (uint32_t)vector_irq10, 0x08, 0x8e);
up_idtentry(IRQ11, (uint32_t)vector_irq11, 0x08, 0x8e);
up_idtentry(IRQ12, (uint32_t)vector_irq12, 0x08, 0x8e);
up_idtentry(IRQ13, (uint32_t)vector_irq13, 0x08, 0x8e);
up_idtentry(IRQ14, (uint32_t)vector_irq14, 0x08, 0x8e);
up_idtentry(IRQ15, (uint32_t)vector_irq15, 0x08, 0x8e);
/* Then program the IDT */
idt_flush((uint32_t)&idt_ptr);
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: up_irqinitialize
****************************************************************************/
void up_irqinitialize(void)
{
/* currents_regs is non-NULL only while processing an interrupt */
current_regs = NULL;
/* Initialize the IDT */
up_idtinit();
/* And finally, enable interrupts */
#ifndef CONFIG_SUPPRESS_INTERRUPTS
irqrestore(X86_FLAGS_IF);
#endif
}
/****************************************************************************
* Name: up_disable_irq
*
* Description:
* Disable the IRQ specified by 'irq'
*
****************************************************************************/
void up_disable_irq(int irq)
{
unsigned int regaddr;
uint8_t regbit;
if (irq >= IRQ0)
{
/* Map the IRQ IMR regiser to a PIC and a bit number */
if (irq <= IRQ7)
{
regaddr = PIC1_IMR;
regbit = (1 << (irq - IRQ0));
}
else if (irq <= IRQ15)
{
regaddr = PIC2_IMR;
regbit = (1 << (irq - IRQ8));
}
else
{
return;
}
/* Disable (mask) the interrupt */
modifyreg8(regaddr, 0, regbit);
}
}
/****************************************************************************
* Name: up_enable_irq
*
* Description:
* Enable the IRQ specified by 'irq'
*
****************************************************************************/
void up_enable_irq(int irq)
{
unsigned int regaddr;
uint8_t regbit;
if (irq >= IRQ0)
{
/* Map the IRQ IMR regiser to a PIC and a bit number */
if (irq <= IRQ7)
{
regaddr = PIC1_IMR;
regbit = (1 << (irq - IRQ0));
}
else if (irq <= IRQ15)
{
regaddr = PIC2_IMR;
regbit = (1 << (irq - IRQ8));
}
else
{
return;
}
/* Enable (unmask) the interrupt */
modifyreg8(regaddr, regbit, 0);
}
}
/****************************************************************************
* Name: up_prioritize_irq
*
* Description:
* Set the priority of an IRQ.
*
* Since this API is not supported on all architectures, it should be
* avoided in common implementations where possible.
*
****************************************************************************/
#ifdef CONFIG_ARCH_IRQPRIO
int up_prioritize_irq(int irq, int priority)
{
#warning "Missing Logic"
return OK;
}
#endif
|