[f761f1eb] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2003-2004 Jakub Jermar
|
---|
[f761f1eb] | 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 |
|
---|
[9a5b556] | 29 | /** @addtogroup mips32interrupt
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[973be64e] | 35 | #include <interrupt.h>
|
---|
[f761f1eb] | 36 | #include <arch/interrupt.h>
|
---|
[d99c1d2] | 37 | #include <typedefs.h>
|
---|
[f761f1eb] | 38 | #include <arch.h>
|
---|
| 39 | #include <arch/cp0.h>
|
---|
[2b698d8] | 40 | #include <arch/smp/dorder.h>
|
---|
[f761f1eb] | 41 | #include <time/clock.h>
|
---|
[5626277] | 42 | #include <ipc/sysipc.h>
|
---|
[7688b5d] | 43 | #include <ddi/device.h>
|
---|
| 44 |
|
---|
[7f341820] | 45 | #define IRQ_COUNT 8
|
---|
| 46 | #define TIMER_IRQ 7
|
---|
| 47 | #define DORDER_IRQ 5
|
---|
[7688b5d] | 48 |
|
---|
[8c84448] | 49 | function virtual_timer_fnc = NULL;
|
---|
[7688b5d] | 50 | static irq_t timer_irq;
|
---|
[2b698d8] | 51 | static irq_t dorder_irq;
|
---|
[5626277] | 52 |
|
---|
[da1bafb] | 53 | // TODO: This is SMP unsafe!!!
|
---|
| 54 |
|
---|
| 55 | uint32_t count_hi = 0;
|
---|
| 56 | static unsigned long nextcount;
|
---|
| 57 | static unsigned long lastcount;
|
---|
| 58 |
|
---|
[22f7769] | 59 | /** Disable interrupts.
|
---|
| 60 | *
|
---|
| 61 | * @return Old interrupt priority level.
|
---|
| 62 | */
|
---|
| 63 | ipl_t interrupts_disable(void)
|
---|
[f761f1eb] | 64 | {
|
---|
[22f7769] | 65 | ipl_t ipl = (ipl_t) cp0_status_read();
|
---|
| 66 | cp0_status_write(ipl & ~cp0_status_ie_enabled_bit);
|
---|
| 67 | return ipl;
|
---|
[f761f1eb] | 68 | }
|
---|
| 69 |
|
---|
[22f7769] | 70 | /** Enable interrupts.
|
---|
| 71 | *
|
---|
| 72 | * @return Old interrupt priority level.
|
---|
| 73 | */
|
---|
| 74 | ipl_t interrupts_enable(void)
|
---|
[f761f1eb] | 75 | {
|
---|
[22f7769] | 76 | ipl_t ipl = (ipl_t) cp0_status_read();
|
---|
| 77 | cp0_status_write(ipl | cp0_status_ie_enabled_bit);
|
---|
| 78 | return ipl;
|
---|
[f761f1eb] | 79 | }
|
---|
| 80 |
|
---|
[22f7769] | 81 | /** Restore interrupt priority level.
|
---|
| 82 | *
|
---|
| 83 | * @param ipl Saved interrupt priority level.
|
---|
| 84 | */
|
---|
| 85 | void interrupts_restore(ipl_t ipl)
|
---|
[f761f1eb] | 86 | {
|
---|
[22f7769] | 87 | cp0_status_write(cp0_status_read() | (ipl & cp0_status_ie_enabled_bit));
|
---|
[f761f1eb] | 88 | }
|
---|
| 89 |
|
---|
[22f7769] | 90 | /** Read interrupt priority level.
|
---|
| 91 | *
|
---|
| 92 | * @return Current interrupt priority level.
|
---|
| 93 | */
|
---|
| 94 | ipl_t interrupts_read(void)
|
---|
[f761f1eb] | 95 | {
|
---|
[76cec1e] | 96 | return cp0_status_read();
|
---|
[f761f1eb] | 97 | }
|
---|
| 98 |
|
---|
[b7aa7c5] | 99 | /** Check interrupts state.
|
---|
| 100 | *
|
---|
| 101 | * @return True if interrupts are disabled.
|
---|
| 102 | *
|
---|
| 103 | */
|
---|
| 104 | bool interrupts_disabled(void)
|
---|
| 105 | {
|
---|
| 106 | return !(cp0_status_read() & cp0_status_ie_enabled_bit);
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[da1bafb] | 109 | /** Start hardware clock
|
---|
| 110 | *
|
---|
| 111 | */
|
---|
[d6e5cbc] | 112 | static void timer_start(void)
|
---|
| 113 | {
|
---|
[0287820] | 114 | lastcount = cp0_count_read();
|
---|
[d6e5cbc] | 115 | nextcount = cp0_compare_value + cp0_count_read();
|
---|
| 116 | cp0_compare_write(nextcount);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[c9b550b] | 119 | static irq_ownership_t timer_claim(irq_t *irq)
|
---|
[7688b5d] | 120 | {
|
---|
| 121 | return IRQ_ACCEPT;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[6cd9aa6] | 124 | static void timer_irq_handler(irq_t *irq)
|
---|
[973be64e] | 125 | {
|
---|
[8df2eab] | 126 | if (cp0_count_read() < lastcount)
|
---|
| 127 | /* Count overflow detected */
|
---|
[0287820] | 128 | count_hi++;
|
---|
[da1bafb] | 129 |
|
---|
[8df2eab] | 130 | lastcount = cp0_count_read();
|
---|
[0287820] | 131 |
|
---|
[da1bafb] | 132 | unsigned long drift = cp0_count_read() - nextcount;
|
---|
[d6e5cbc] | 133 | while (drift > cp0_compare_value) {
|
---|
| 134 | drift -= cp0_compare_value;
|
---|
| 135 | CPU->missed_clock_ticks++;
|
---|
| 136 | }
|
---|
[da1bafb] | 137 |
|
---|
[d6e5cbc] | 138 | nextcount = cp0_count_read() + cp0_compare_value - drift;
|
---|
| 139 | cp0_compare_write(nextcount);
|
---|
[0287820] | 140 |
|
---|
[f619ec11] | 141 | /*
|
---|
| 142 | * We are holding a lock which prevents preemption.
|
---|
| 143 | * Release the lock, call clock() and reacquire the lock again.
|
---|
| 144 | */
|
---|
[da1bafb] | 145 | irq_spinlock_unlock(&irq->lock, false);
|
---|
[973be64e] | 146 | clock();
|
---|
[da1bafb] | 147 | irq_spinlock_lock(&irq->lock, false);
|
---|
[7688b5d] | 148 |
|
---|
[8c84448] | 149 | if (virtual_timer_fnc != NULL)
|
---|
| 150 | virtual_timer_fnc();
|
---|
[973be64e] | 151 | }
|
---|
| 152 |
|
---|
[2b698d8] | 153 | static irq_ownership_t dorder_claim(irq_t *irq)
|
---|
| 154 | {
|
---|
| 155 | return IRQ_ACCEPT;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | static void dorder_irq_handler(irq_t *irq)
|
---|
| 159 | {
|
---|
| 160 | dorder_ipi_ack(1 << dorder_cpuid());
|
---|
| 161 | }
|
---|
| 162 |
|
---|
[973be64e] | 163 | /* Initialize basic tables for exception dispatching */
|
---|
| 164 | void interrupt_init(void)
|
---|
| 165 | {
|
---|
[7688b5d] | 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 |
|
---|
[d6e5cbc] | 175 | timer_start();
|
---|
[7688b5d] | 176 | cp0_unmask_int(TIMER_IRQ);
|
---|
[2b698d8] | 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);
|
---|
[5626277] | 186 | }
|
---|
[b45c443] | 187 |
|
---|
[9a5b556] | 188 | /** @}
|
---|
[b45c443] | 189 | */
|
---|