blob: df6480f8cd6a053b0ea10d80cd7e6918600f5434 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (c) 2008 Travis Geiselbrecht
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23#include <err.h>
24#include <sys/types.h>
25#include <debug.h>
26#include <reg.h>
27#include <kernel/thread.h>
28#include <kernel/debug.h>
29#include <platform/interrupts.h>
30#include <platform/armemu.h>
31#include <arch/ops.h>
32#include <arch/arm.h>
33#include "platform_p.h"
34
35struct int_handler_struct {
36 int_handler handler;
37 void *arg;
38};
39
40static struct int_handler_struct int_handler_table[PIC_MAX_INT];
41
42void platform_init_interrupts(void)
43{
44 // mask all the interrupts
45 *REG32(PIC_MASK_LATCH) = 0xffffffff;
46}
47
48status_t mask_interrupt(unsigned int vector)
49{
50 if (vector >= PIC_MAX_INT)
51 return ERR_INVALID_ARGS;
52
53// dprintf("%s: vector %d\n", __PRETTY_FUNCTION__, vector);
54
55 *REG32(PIC_MASK_LATCH) = 1 << vector;
56
57 return NO_ERROR;
58}
59
60status_t unmask_interrupt(unsigned int vector)
61{
62 if (vector >= PIC_MAX_INT)
63 return ERR_INVALID_ARGS;
64
65// dprintf("%s: vector %d\n", __PRETTY_FUNCTION__, vector);
66
67 *REG32(PIC_UNMASK_LATCH) = 1 << vector;
68
69 return NO_ERROR;
70}
71
72enum handler_return platform_irq(struct arm_iframe *frame)
73{
74 // get the current vector
75 unsigned int vector = *REG32(PIC_CURRENT_NUM);
76 if (vector == 0xffffffff)
77 return INT_NO_RESCHEDULE;
78
79 THREAD_STATS_INC(interrupts);
80 KEVLOG_IRQ_ENTER(vector);
81
82// printf("platform_irq: spsr 0x%x, pc 0x%x, currthread %p, vector %d\n", frame->spsr, frame->pc, current_thread, vector);
83
84 // deliver the interrupt
85 enum handler_return ret;
86
87 ret = INT_NO_RESCHEDULE;
88 if (int_handler_table[vector].handler)
89 ret = int_handler_table[vector].handler(int_handler_table[vector].arg);
90
91// dprintf("platform_irq: exit %d\n", ret);
92
93 KEVLOG_IRQ_EXIT(vector);
94
95 return ret;
96}
97
98void platform_fiq(struct arm_iframe *frame)
99{
100 panic("FIQ: unimplemented\n");
101}
102
103void register_int_handler(unsigned int vector, int_handler handler, void *arg)
104{
105 if (vector >= PIC_MAX_INT)
106 panic("register_int_handler: vector out of range %d\n", vector);
107
108 int_handler_table[vector].handler = handler;
109 int_handler_table[vector].arg = arg;
110}
111