source: mainline/kernel/arch/mips32/src/interrupt.c@ 41a7f62

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 41a7f62 was 2b698d8, checked in by Martin Decky <martin@…>, 15 years ago

basic processing of the IPI interrupts from the dorder device

  • Property mode set to 100644
File size: 4.6 KB
Line 
1/*
2 * Copyright (c) 2003-2004 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup mips32interrupt
30 * @{
31 */
32/** @file
33 */
34
35#include <interrupt.h>
36#include <arch/interrupt.h>
37#include <typedefs.h>
38#include <arch.h>
39#include <arch/cp0.h>
40#include <arch/smp/dorder.h>
41#include <time/clock.h>
42#include <ipc/sysipc.h>
43#include <ddi/device.h>
44
45#define IRQ_COUNT 8
46#define TIMER_IRQ 7
47#define DORDER_IRQ 5
48
49function virtual_timer_fnc = NULL;
50static irq_t timer_irq;
51static irq_t dorder_irq;
52
53// TODO: This is SMP unsafe!!!
54
55uint32_t count_hi = 0;
56static unsigned long nextcount;
57static unsigned long lastcount;
58
59/** Disable interrupts.
60 *
61 * @return Old interrupt priority level.
62 */
63ipl_t interrupts_disable(void)
64{
65 ipl_t ipl = (ipl_t) cp0_status_read();
66 cp0_status_write(ipl & ~cp0_status_ie_enabled_bit);
67 return ipl;
68}
69
70/** Enable interrupts.
71 *
72 * @return Old interrupt priority level.
73 */
74ipl_t interrupts_enable(void)
75{
76 ipl_t ipl = (ipl_t) cp0_status_read();
77 cp0_status_write(ipl | cp0_status_ie_enabled_bit);
78 return ipl;
79}
80
81/** Restore interrupt priority level.
82 *
83 * @param ipl Saved interrupt priority level.
84 */
85void interrupts_restore(ipl_t ipl)
86{
87 cp0_status_write(cp0_status_read() | (ipl & cp0_status_ie_enabled_bit));
88}
89
90/** Read interrupt priority level.
91 *
92 * @return Current interrupt priority level.
93 */
94ipl_t interrupts_read(void)
95{
96 return cp0_status_read();
97}
98
99/** Check interrupts state.
100 *
101 * @return True if interrupts are disabled.
102 *
103 */
104bool interrupts_disabled(void)
105{
106 return !(cp0_status_read() & cp0_status_ie_enabled_bit);
107}
108
109/** Start hardware clock
110 *
111 */
112static void timer_start(void)
113{
114 lastcount = cp0_count_read();
115 nextcount = cp0_compare_value + cp0_count_read();
116 cp0_compare_write(nextcount);
117}
118
119static irq_ownership_t timer_claim(irq_t *irq)
120{
121 return IRQ_ACCEPT;
122}
123
124static void timer_irq_handler(irq_t *irq)
125{
126 if (cp0_count_read() < lastcount)
127 /* Count overflow detected */
128 count_hi++;
129
130 lastcount = cp0_count_read();
131
132 unsigned long drift = cp0_count_read() - nextcount;
133 while (drift > cp0_compare_value) {
134 drift -= cp0_compare_value;
135 CPU->missed_clock_ticks++;
136 }
137
138 nextcount = cp0_count_read() + cp0_compare_value - drift;
139 cp0_compare_write(nextcount);
140
141 /*
142 * We are holding a lock which prevents preemption.
143 * Release the lock, call clock() and reacquire the lock again.
144 */
145 irq_spinlock_unlock(&irq->lock, false);
146 clock();
147 irq_spinlock_lock(&irq->lock, false);
148
149 if (virtual_timer_fnc != NULL)
150 virtual_timer_fnc();
151}
152
153static irq_ownership_t dorder_claim(irq_t *irq)
154{
155 return IRQ_ACCEPT;
156}
157
158static void dorder_irq_handler(irq_t *irq)
159{
160 dorder_ipi_ack(1 << dorder_cpuid());
161}
162
163/* Initialize basic tables for exception dispatching */
164void interrupt_init(void)
165{
166 irq_init(IRQ_COUNT, IRQ_COUNT);
167
168 irq_initialize(&timer_irq);
169 timer_irq.devno = device_assign_devno();
170 timer_irq.inr = TIMER_IRQ;
171 timer_irq.claim = timer_claim;
172 timer_irq.handler = timer_irq_handler;
173 irq_register(&timer_irq);
174
175 timer_start();
176 cp0_unmask_int(TIMER_IRQ);
177
178 irq_initialize(&dorder_irq);
179 dorder_irq.devno = device_assign_devno();
180 dorder_irq.inr = DORDER_IRQ;
181 dorder_irq.claim = dorder_claim;
182 dorder_irq.handler = dorder_irq_handler;
183 irq_register(&dorder_irq);
184
185 cp0_unmask_int(DORDER_IRQ);
186}
187
188/** @}
189 */
Note: See TracBrowser for help on using the repository browser.