[154049e] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2005 Jakub Jermar
|
---|
[154049e] | 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 |
|
---|
[c5429fe] | 29 | /** @addtogroup kernel_ia64
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[154049e] | 35 | /** Interval Timer driver. */
|
---|
[da1bafb] | 36 |
|
---|
[154049e] | 37 | #include <arch/drivers/it.h>
|
---|
| 38 | #include <arch/interrupt.h>
|
---|
| 39 | #include <arch/register.h>
|
---|
| 40 | #include <arch/asm.h>
|
---|
[05882233] | 41 | #include <barrier.h>
|
---|
[154049e] | 42 | #include <time/clock.h>
|
---|
[de57e060] | 43 | #include <ddi/irq.h>
|
---|
[bd571f44] | 44 | #include <arch.h>
|
---|
[154049e] | 45 |
|
---|
[da1bafb] | 46 | #define IT_SERVICE_CLOCKS 64
|
---|
[bd571f44] | 47 |
|
---|
[da1bafb] | 48 | #define FREQ_NUMERATOR_SHIFT 32
|
---|
| 49 | #define FREQ_NUMERATOR_MASK 0xffffffff00000000ULL
|
---|
[1025d28] | 50 |
|
---|
[da1bafb] | 51 | #define FREQ_DENOMINATOR_SHIFT 0
|
---|
| 52 | #define FREQ_DENOMINATOR_MASK 0xffffffffULL
|
---|
[1025d28] | 53 |
|
---|
| 54 | uint64_t it_delta;
|
---|
| 55 |
|
---|
[de57e060] | 56 | static irq_t it_irq;
|
---|
| 57 |
|
---|
[c9b550b] | 58 | static irq_ownership_t it_claim(irq_t *);
|
---|
| 59 | static void it_interrupt(irq_t *);
|
---|
[de57e060] | 60 |
|
---|
[154049e] | 61 | /** Initialize Interval Timer. */
|
---|
| 62 | void it_init(void)
|
---|
| 63 | {
|
---|
[666773c] | 64 | if (config.cpu_active == 1) {
|
---|
[59e4864] | 65 | irq_initialize(&it_irq);
|
---|
| 66 | it_irq.inr = INTERRUPT_TIMER;
|
---|
| 67 | it_irq.claim = it_claim;
|
---|
| 68 | it_irq.handler = it_interrupt;
|
---|
| 69 | irq_register(&it_irq);
|
---|
[a35b458] | 70 |
|
---|
[1025d28] | 71 | uint64_t base_freq;
|
---|
[666773c] | 72 | base_freq = ((bootinfo->freq_scale) & FREQ_NUMERATOR_MASK) >>
|
---|
| 73 | FREQ_NUMERATOR_SHIFT;
|
---|
[1025d28] | 74 | base_freq *= bootinfo->sys_freq;
|
---|
[666773c] | 75 | base_freq /= ((bootinfo->freq_scale) & FREQ_DENOMINATOR_MASK) >>
|
---|
| 76 | FREQ_DENOMINATOR_SHIFT;
|
---|
[a35b458] | 77 |
|
---|
[666773c] | 78 | it_delta = base_freq / HZ;
|
---|
[59e4864] | 79 | }
|
---|
[a35b458] | 80 |
|
---|
[da1bafb] | 81 | /* Initialize Interval Timer external interrupt vector */
|
---|
| 82 | cr_itv_t itv;
|
---|
[a35b458] | 83 |
|
---|
[154049e] | 84 | itv.value = itv_read();
|
---|
| 85 | itv.vector = INTERRUPT_TIMER;
|
---|
| 86 | itv.m = 0;
|
---|
| 87 | itv_write(itv.value);
|
---|
[a35b458] | 88 |
|
---|
[da1bafb] | 89 | /* Set Interval Timer Counter to zero */
|
---|
[154049e] | 90 | itc_write(0);
|
---|
[a35b458] | 91 |
|
---|
[da1bafb] | 92 | /* Generate first Interval Timer interrupt in IT_DELTA ticks */
|
---|
[154049e] | 93 | itm_write(IT_DELTA);
|
---|
[a35b458] | 94 |
|
---|
[da1bafb] | 95 | /* Propagate changes */
|
---|
[154049e] | 96 | srlz_d();
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[de57e060] | 99 | /** Always claim ownership for this IRQ.
|
---|
| 100 | *
|
---|
| 101 | * Other devices are responsible to avoid using INR 0.
|
---|
| 102 | *
|
---|
| 103 | * @return Always IRQ_ACCEPT.
|
---|
[da1bafb] | 104 | *
|
---|
[de57e060] | 105 | */
|
---|
[c9b550b] | 106 | irq_ownership_t it_claim(irq_t *irq)
|
---|
[de57e060] | 107 | {
|
---|
| 108 | return IRQ_ACCEPT;
|
---|
| 109 | }
|
---|
[bd571f44] | 110 |
|
---|
[154049e] | 111 | /** Process Interval Timer interrupt. */
|
---|
[6cd9aa6] | 112 | void it_interrupt(irq_t *irq)
|
---|
[154049e] | 113 | {
|
---|
| 114 | eoi_write(EOI);
|
---|
[a35b458] | 115 |
|
---|
[da1bafb] | 116 | int64_t itm = itm_read();
|
---|
[a35b458] | 117 |
|
---|
[da1bafb] | 118 | while (true) {
|
---|
| 119 | int64_t itc = itc_read();
|
---|
| 120 | itc += IT_SERVICE_CLOCKS;
|
---|
[a35b458] | 121 |
|
---|
[da1bafb] | 122 | itm += IT_DELTA;
|
---|
| 123 | if (itm - itc < 0)
|
---|
[4760793] | 124 | CPU_LOCAL->missed_clock_ticks++;
|
---|
[9a5b556] | 125 | else
|
---|
| 126 | break;
|
---|
[bd571f44] | 127 | }
|
---|
[a35b458] | 128 |
|
---|
[da1bafb] | 129 | itm_write(itm);
|
---|
| 130 | srlz_d(); /* Propagate changes */
|
---|
[a35b458] | 131 |
|
---|
[f619ec11] | 132 | /*
|
---|
| 133 | * We are holding a lock which prevents preemption.
|
---|
| 134 | * Release the lock, call clock() and reacquire the lock again.
|
---|
| 135 | */
|
---|
[da1bafb] | 136 | irq_spinlock_unlock(&irq->lock, false);
|
---|
[154049e] | 137 | clock();
|
---|
[da1bafb] | 138 | irq_spinlock_lock(&irq->lock, false);
|
---|
[154049e] | 139 | }
|
---|
[b45c443] | 140 |
|
---|
[9a5b556] | 141 | /** @}
|
---|
[b45c443] | 142 | */
|
---|