Subversion Repositories HelenOS

Rev

Rev 2101 | Rev 2218 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2101 Rev 2217
1
/*
1
/*
2
 * Copyright (c) 2001-2004 Jakub Jermar
2
 * Copyright (c) 2001-2004 Jakub Jermar
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup ia32   
29
/** @addtogroup ia32   
30
 * @{
30
 * @{
31
 */
31
 */
32
/** @file
32
/** @file
33
 */
33
 */
34
 
34
 
35
#include <arch/types.h>
35
#include <arch/types.h>
36
#include <arch/smp/apic.h>
36
#include <arch/smp/apic.h>
37
#include <arch/smp/ap.h>
37
#include <arch/smp/ap.h>
38
#include <arch/smp/mps.h>
38
#include <arch/smp/mps.h>
39
#include <arch/boot/boot.h>
39
#include <arch/boot/boot.h>
40
#include <mm/page.h>
40
#include <mm/page.h>
41
#include <time/delay.h>
41
#include <time/delay.h>
42
#include <interrupt.h>
42
#include <interrupt.h>
43
#include <arch/interrupt.h>
43
#include <arch/interrupt.h>
44
#include <print.h>
44
#include <print.h>
45
#include <arch/asm.h>
45
#include <arch/asm.h>
46
#include <arch.h>
46
#include <arch.h>
47
#include <ddi/irq.h>
47
#include <ddi/irq.h>
48
#include <ddi/device.h>
48
#include <ddi/device.h>
49
 
49
 
50
#ifdef CONFIG_SMP
50
#ifdef CONFIG_SMP
51
 
51
 
52
/*
52
/*
53
 * Advanced Programmable Interrupt Controller for SMP systems.
53
 * Advanced Programmable Interrupt Controller for SMP systems.
54
 * Tested on:
54
 * Tested on:
55
 *  Bochs 2.0.2 - Bochs 2.2.6 with 2-8 CPUs
55
 *  Bochs 2.0.2 - Bochs 2.2.6 with 2-8 CPUs
56
 *  Simics 2.0.28 - Simics 2.2.19 2-15 CPUs
56
 *  Simics 2.0.28 - Simics 2.2.19 2-15 CPUs
57
 *  VMware Workstation 5.5 with 2 CPUs
57
 *  VMware Workstation 5.5 with 2 CPUs
58
 *  QEMU 0.8.0 with 2-15 CPUs
58
 *  QEMU 0.8.0 with 2-15 CPUs
59
 *  ASUS P/I-P65UP5 + ASUS C-P55T2D REV. 1.41 with 2x 200Mhz Pentium CPUs
59
 *  ASUS P/I-P65UP5 + ASUS C-P55T2D REV. 1.41 with 2x 200Mhz Pentium CPUs
60
 *  ASUS PCH-DL with 2x 3000Mhz Pentium 4 Xeon (HT) CPUs
60
 *  ASUS PCH-DL with 2x 3000Mhz Pentium 4 Xeon (HT) CPUs
61
 *  MSI K7D Master-L with 2x 2100MHz Athlon MP CPUs
61
 *  MSI K7D Master-L with 2x 2100MHz Athlon MP CPUs
62
 */
62
 */
63
 
63
 
64
/*
64
/*
65
 * These variables either stay configured as initilalized, or are changed by
65
 * These variables either stay configured as initilalized, or are changed by
66
 * the MP configuration code.
66
 * the MP configuration code.
67
 *
67
 *
68
 * Pay special attention to the volatile keyword. Without it, gcc -O2 would
68
 * Pay special attention to the volatile keyword. Without it, gcc -O2 would
69
 * optimize the code too much and accesses to l_apic and io_apic, that must
69
 * optimize the code too much and accesses to l_apic and io_apic, that must
70
 * always be 32-bit, would use byte oriented instructions.
70
 * always be 32-bit, would use byte oriented instructions.
71
 */
71
 */
72
volatile uint32_t *l_apic = (uint32_t *) 0xfee00000;
72
volatile uint32_t *l_apic = (uint32_t *) 0xfee00000;
73
volatile uint32_t *io_apic = (uint32_t *) 0xfec00000;
73
volatile uint32_t *io_apic = (uint32_t *) 0xfec00000;
74
 
74
 
75
uint32_t apic_id_mask = 0;
75
uint32_t apic_id_mask = 0;
76
static irq_t l_apic_timer_irq;
76
static irq_t l_apic_timer_irq;
77
 
77
 
78
static int apic_poll_errors(void);
78
static int apic_poll_errors(void);
79
 
79
 
80
#ifdef LAPIC_VERBOSE
80
#ifdef LAPIC_VERBOSE
81
static char *delmod_str[] = {
81
static char *delmod_str[] = {
82
    "Fixed",
82
    "Fixed",
83
    "Lowest Priority",
83
    "Lowest Priority",
84
    "SMI",
84
    "SMI",
85
    "Reserved",
85
    "Reserved",
86
    "NMI",
86
    "NMI",
87
    "INIT",
87
    "INIT",
88
    "STARTUP",
88
    "STARTUP",
89
    "ExtInt"
89
    "ExtInt"
90
};
90
};
91
 
91
 
92
static char *destmod_str[] = {
92
static char *destmod_str[] = {
93
    "Physical",
93
    "Physical",
94
    "Logical"
94
    "Logical"
95
};
95
};
96
 
96
 
97
static char *trigmod_str[] = {
97
static char *trigmod_str[] = {
98
    "Edge",
98
    "Edge",
99
    "Level"
99
    "Level"
100
};
100
};
101
 
101
 
102
static char *mask_str[] = {
102
static char *mask_str[] = {
103
    "Unmasked",
103
    "Unmasked",
104
    "Masked"
104
    "Masked"
105
};
105
};
106
 
106
 
107
static char *delivs_str[] = {
107
static char *delivs_str[] = {
108
    "Idle",
108
    "Idle",
109
    "Send Pending"
109
    "Send Pending"
110
};
110
};
111
 
111
 
112
static char *tm_mode_str[] = {
112
static char *tm_mode_str[] = {
113
    "One-shot",
113
    "One-shot",
114
    "Periodic"
114
    "Periodic"
115
};
115
};
116
 
116
 
117
static char *intpol_str[] = {
117
static char *intpol_str[] = {
118
    "Polarity High",
118
    "Polarity High",
119
    "Polarity Low"
119
    "Polarity Low"
120
};
120
};
121
#endif /* LAPIC_VERBOSE */
121
#endif /* LAPIC_VERBOSE */
122
 
122
 
123
/** APIC spurious interrupt handler.
123
/** APIC spurious interrupt handler.
124
 *
124
 *
125
 * @param n Interrupt vector.
125
 * @param n Interrupt vector.
126
 * @param istate Interrupted state.
126
 * @param istate Interrupted state.
127
 */
127
 */
128
static void apic_spurious(int n, istate_t *istate)
128
static void apic_spurious(int n, istate_t *istate)
129
{
129
{
130
#ifdef CONFIG_DEBUG
130
#ifdef CONFIG_DEBUG
131
    printf("cpu%d: APIC spurious interrupt\n", CPU->id);
131
    printf("cpu%d: APIC spurious interrupt\n", CPU->id);
132
#endif
132
#endif
133
}
133
}
134
 
134
 
135
static irq_ownership_t l_apic_timer_claim(void)
135
static irq_ownership_t l_apic_timer_claim(void)
136
{
136
{
137
    return IRQ_ACCEPT;
137
    return IRQ_ACCEPT;
138
}
138
}
139
 
139
 
140
static void l_apic_timer_irq_handler(irq_t *irq, void *arg, ...)
140
static void l_apic_timer_irq_handler(irq_t *irq, void *arg, ...)
141
{
141
{
-
 
142
    /*
-
 
143
     * Holding a spinlock could prevent clock() from preempting
-
 
144
     * the current thread. In this case, we don't need to hold the
-
 
145
     * irq->lock so we just unlock it and then lock it again.
-
 
146
     */
-
 
147
    spinlock_unlock(&irq->lock);
142
    clock();
148
    clock();
-
 
149
    spinlock_lock(&irq->lock);
143
}
150
}
144
 
151
 
145
/** Initialize APIC on BSP. */
152
/** Initialize APIC on BSP. */
146
void apic_init(void)
153
void apic_init(void)
147
{
154
{
148
    io_apic_id_t idreg;
155
    io_apic_id_t idreg;
149
    unsigned int i;
156
    unsigned int i;
150
 
157
 
151
    exc_register(VECTOR_APIC_SPUR, "apic_spurious", (iroutine) apic_spurious);
158
    exc_register(VECTOR_APIC_SPUR, "apic_spurious", (iroutine) apic_spurious);
152
 
159
 
153
    enable_irqs_function = io_apic_enable_irqs;
160
    enable_irqs_function = io_apic_enable_irqs;
154
    disable_irqs_function = io_apic_disable_irqs;
161
    disable_irqs_function = io_apic_disable_irqs;
155
    eoi_function = l_apic_eoi;
162
    eoi_function = l_apic_eoi;
156
   
163
   
157
    /*
164
    /*
158
     * Configure interrupt routing.
165
     * Configure interrupt routing.
159
     * IRQ 0 remains masked as the time signal is generated by l_apic's themselves.
166
     * IRQ 0 remains masked as the time signal is generated by l_apic's themselves.
160
     * Other interrupts will be forwarded to the lowest priority CPU.
167
     * Other interrupts will be forwarded to the lowest priority CPU.
161
     */
168
     */
162
    io_apic_disable_irqs(0xffff);
169
    io_apic_disable_irqs(0xffff);
163
   
170
   
164
    irq_initialize(&l_apic_timer_irq);
171
    irq_initialize(&l_apic_timer_irq);
165
    l_apic_timer_irq.devno = device_assign_devno();
172
    l_apic_timer_irq.devno = device_assign_devno();
166
    l_apic_timer_irq.inr = IRQ_CLK;
173
    l_apic_timer_irq.inr = IRQ_CLK;
167
    l_apic_timer_irq.claim = l_apic_timer_claim;
174
    l_apic_timer_irq.claim = l_apic_timer_claim;
168
    l_apic_timer_irq.handler = l_apic_timer_irq_handler;
175
    l_apic_timer_irq.handler = l_apic_timer_irq_handler;
169
    irq_register(&l_apic_timer_irq);
176
    irq_register(&l_apic_timer_irq);
170
   
177
   
171
    for (i = 0; i < IRQ_COUNT; i++) {
178
    for (i = 0; i < IRQ_COUNT; i++) {
172
        int pin;
179
        int pin;
173
   
180
   
174
        if ((pin = smp_irq_to_pin(i)) != -1)
181
        if ((pin = smp_irq_to_pin(i)) != -1)
175
            io_apic_change_ioredtbl(pin, DEST_ALL, IVT_IRQBASE + i, LOPRI);
182
            io_apic_change_ioredtbl(pin, DEST_ALL, IVT_IRQBASE + i, LOPRI);
176
    }
183
    }
177
   
184
   
178
    /*
185
    /*
179
     * Ensure that io_apic has unique ID.
186
     * Ensure that io_apic has unique ID.
180
     */
187
     */
181
    idreg.value = io_apic_read(IOAPICID);
188
    idreg.value = io_apic_read(IOAPICID);
182
    if ((1 << idreg.apic_id) & apic_id_mask) {  /* see if IO APIC ID is used already */
189
    if ((1 << idreg.apic_id) & apic_id_mask) {  /* see if IO APIC ID is used already */
183
        for (i = 0; i < APIC_ID_COUNT; i++) {
190
        for (i = 0; i < APIC_ID_COUNT; i++) {
184
            if (!((1 << i) & apic_id_mask)) {
191
            if (!((1 << i) & apic_id_mask)) {
185
                idreg.apic_id = i;
192
                idreg.apic_id = i;
186
                io_apic_write(IOAPICID, idreg.value);
193
                io_apic_write(IOAPICID, idreg.value);
187
                break;
194
                break;
188
            }
195
            }
189
        }
196
        }
190
    }
197
    }
191
 
198
 
192
    /*
199
    /*
193
     * Configure the BSP's lapic.
200
     * Configure the BSP's lapic.
194
     */
201
     */
195
    l_apic_init();
202
    l_apic_init();
196
 
203
 
197
    l_apic_debug();
204
    l_apic_debug();
198
}
205
}
199
 
206
 
200
/** Poll for APIC errors.
207
/** Poll for APIC errors.
201
 *
208
 *
202
 * Examine Error Status Register and report all errors found.
209
 * Examine Error Status Register and report all errors found.
203
 *
210
 *
204
 * @return 0 on error, 1 on success.
211
 * @return 0 on error, 1 on success.
205
 */
212
 */
206
int apic_poll_errors(void)
213
int apic_poll_errors(void)
207
{
214
{
208
    esr_t esr;
215
    esr_t esr;
209
   
216
   
210
    esr.value = l_apic[ESR];
217
    esr.value = l_apic[ESR];
211
   
218
   
212
    if (esr.send_checksum_error)
219
    if (esr.send_checksum_error)
213
        printf("Send Checksum Error\n");
220
        printf("Send Checksum Error\n");
214
    if (esr.receive_checksum_error)
221
    if (esr.receive_checksum_error)
215
        printf("Receive Checksum Error\n");
222
        printf("Receive Checksum Error\n");
216
    if (esr.send_accept_error)
223
    if (esr.send_accept_error)
217
        printf("Send Accept Error\n");
224
        printf("Send Accept Error\n");
218
    if (esr.receive_accept_error)
225
    if (esr.receive_accept_error)
219
        printf("Receive Accept Error\n");
226
        printf("Receive Accept Error\n");
220
    if (esr.send_illegal_vector)
227
    if (esr.send_illegal_vector)
221
        printf("Send Illegal Vector\n");
228
        printf("Send Illegal Vector\n");
222
    if (esr.received_illegal_vector)
229
    if (esr.received_illegal_vector)
223
        printf("Received Illegal Vector\n");
230
        printf("Received Illegal Vector\n");
224
    if (esr.illegal_register_address)
231
    if (esr.illegal_register_address)
225
        printf("Illegal Register Address\n");
232
        printf("Illegal Register Address\n");
226
 
233
 
227
    return !esr.err_bitmap;
234
    return !esr.err_bitmap;
228
}
235
}
229
 
236
 
230
/** Send all CPUs excluding CPU IPI vector.
237
/** Send all CPUs excluding CPU IPI vector.
231
 *
238
 *
232
 * @param vector Interrupt vector to be sent.
239
 * @param vector Interrupt vector to be sent.
233
 *
240
 *
234
 * @return 0 on failure, 1 on success.
241
 * @return 0 on failure, 1 on success.
235
 */
242
 */
236
int l_apic_broadcast_custom_ipi(uint8_t vector)
243
int l_apic_broadcast_custom_ipi(uint8_t vector)
237
{
244
{
238
    icr_t icr;
245
    icr_t icr;
239
 
246
 
240
    icr.lo = l_apic[ICRlo];
247
    icr.lo = l_apic[ICRlo];
241
    icr.delmod = DELMOD_FIXED;
248
    icr.delmod = DELMOD_FIXED;
242
    icr.destmod = DESTMOD_LOGIC;
249
    icr.destmod = DESTMOD_LOGIC;
243
    icr.level = LEVEL_ASSERT;
250
    icr.level = LEVEL_ASSERT;
244
    icr.shorthand = SHORTHAND_ALL_EXCL;
251
    icr.shorthand = SHORTHAND_ALL_EXCL;
245
    icr.trigger_mode = TRIGMOD_LEVEL;
252
    icr.trigger_mode = TRIGMOD_LEVEL;
246
    icr.vector = vector;
253
    icr.vector = vector;
247
 
254
 
248
    l_apic[ICRlo] = icr.lo;
255
    l_apic[ICRlo] = icr.lo;
249
 
256
 
250
    icr.lo = l_apic[ICRlo];
257
    icr.lo = l_apic[ICRlo];
251
    if (icr.delivs == DELIVS_PENDING) {
258
    if (icr.delivs == DELIVS_PENDING) {
252
#ifdef CONFIG_DEBUG
259
#ifdef CONFIG_DEBUG
253
        printf("IPI is pending.\n");
260
        printf("IPI is pending.\n");
254
#endif
261
#endif
255
    }
262
    }
256
 
263
 
257
    return apic_poll_errors();
264
    return apic_poll_errors();
258
}
265
}
259
 
266
 
260
/** Universal Start-up Algorithm for bringing up the AP processors.
267
/** Universal Start-up Algorithm for bringing up the AP processors.
261
 *
268
 *
262
 * @param apicid APIC ID of the processor to be brought up.
269
 * @param apicid APIC ID of the processor to be brought up.
263
 *
270
 *
264
 * @return 0 on failure, 1 on success.
271
 * @return 0 on failure, 1 on success.
265
 */
272
 */
266
int l_apic_send_init_ipi(uint8_t apicid)
273
int l_apic_send_init_ipi(uint8_t apicid)
267
{
274
{
268
    icr_t icr;
275
    icr_t icr;
269
    int i;
276
    int i;
270
 
277
 
271
    /*
278
    /*
272
     * Read the ICR register in and zero all non-reserved fields.
279
     * Read the ICR register in and zero all non-reserved fields.
273
     */
280
     */
274
    icr.lo = l_apic[ICRlo];
281
    icr.lo = l_apic[ICRlo];
275
    icr.hi = l_apic[ICRhi];
282
    icr.hi = l_apic[ICRhi];
276
   
283
   
277
    icr.delmod = DELMOD_INIT;
284
    icr.delmod = DELMOD_INIT;
278
    icr.destmod = DESTMOD_PHYS;
285
    icr.destmod = DESTMOD_PHYS;
279
    icr.level = LEVEL_ASSERT;
286
    icr.level = LEVEL_ASSERT;
280
    icr.trigger_mode = TRIGMOD_LEVEL;
287
    icr.trigger_mode = TRIGMOD_LEVEL;
281
    icr.shorthand = SHORTHAND_NONE;
288
    icr.shorthand = SHORTHAND_NONE;
282
    icr.vector = 0;
289
    icr.vector = 0;
283
    icr.dest = apicid;
290
    icr.dest = apicid;
284
   
291
   
285
    l_apic[ICRhi] = icr.hi;
292
    l_apic[ICRhi] = icr.hi;
286
    l_apic[ICRlo] = icr.lo;
293
    l_apic[ICRlo] = icr.lo;
287
 
294
 
288
    /*
295
    /*
289
     * According to MP Specification, 20us should be enough to
296
     * According to MP Specification, 20us should be enough to
290
     * deliver the IPI.
297
     * deliver the IPI.
291
     */
298
     */
292
    delay(20);
299
    delay(20);
293
 
300
 
294
    if (!apic_poll_errors())
301
    if (!apic_poll_errors())
295
        return 0;
302
        return 0;
296
 
303
 
297
    icr.lo = l_apic[ICRlo];
304
    icr.lo = l_apic[ICRlo];
298
    if (icr.delivs == DELIVS_PENDING) {
305
    if (icr.delivs == DELIVS_PENDING) {
299
#ifdef CONFIG_DEBUG
306
#ifdef CONFIG_DEBUG
300
        printf("IPI is pending.\n");
307
        printf("IPI is pending.\n");
301
#endif
308
#endif
302
    }
309
    }
303
 
310
 
304
    icr.delmod = DELMOD_INIT;
311
    icr.delmod = DELMOD_INIT;
305
    icr.destmod = DESTMOD_PHYS;
312
    icr.destmod = DESTMOD_PHYS;
306
    icr.level = LEVEL_DEASSERT;
313
    icr.level = LEVEL_DEASSERT;
307
    icr.shorthand = SHORTHAND_NONE;
314
    icr.shorthand = SHORTHAND_NONE;
308
    icr.trigger_mode = TRIGMOD_LEVEL;
315
    icr.trigger_mode = TRIGMOD_LEVEL;
309
    icr.vector = 0;
316
    icr.vector = 0;
310
    l_apic[ICRlo] = icr.lo;
317
    l_apic[ICRlo] = icr.lo;
311
 
318
 
312
    /*
319
    /*
313
     * Wait 10ms as MP Specification specifies.
320
     * Wait 10ms as MP Specification specifies.
314
     */
321
     */
315
    delay(10000);
322
    delay(10000);
316
 
323
 
317
    if (!is_82489DX_apic(l_apic[LAVR])) {
324
    if (!is_82489DX_apic(l_apic[LAVR])) {
318
        /*
325
        /*
319
         * If this is not 82489DX-based l_apic we must send two STARTUP IPI's.
326
         * If this is not 82489DX-based l_apic we must send two STARTUP IPI's.
320
         */
327
         */
321
        for (i = 0; i<2; i++) {
328
        for (i = 0; i<2; i++) {
322
            icr.lo = l_apic[ICRlo];
329
            icr.lo = l_apic[ICRlo];
323
            icr.vector = ((uintptr_t) ap_boot) / 4096; /* calculate the reset vector */
330
            icr.vector = ((uintptr_t) ap_boot) / 4096; /* calculate the reset vector */
324
            icr.delmod = DELMOD_STARTUP;
331
            icr.delmod = DELMOD_STARTUP;
325
            icr.destmod = DESTMOD_PHYS;
332
            icr.destmod = DESTMOD_PHYS;
326
            icr.level = LEVEL_ASSERT;
333
            icr.level = LEVEL_ASSERT;
327
            icr.shorthand = SHORTHAND_NONE;
334
            icr.shorthand = SHORTHAND_NONE;
328
            icr.trigger_mode = TRIGMOD_LEVEL;
335
            icr.trigger_mode = TRIGMOD_LEVEL;
329
            l_apic[ICRlo] = icr.lo;
336
            l_apic[ICRlo] = icr.lo;
330
            delay(200);
337
            delay(200);
331
        }
338
        }
332
    }
339
    }
333
   
340
   
334
    return apic_poll_errors();
341
    return apic_poll_errors();
335
}
342
}
336
 
343
 
337
/** Initialize Local APIC. */
344
/** Initialize Local APIC. */
338
void l_apic_init(void)
345
void l_apic_init(void)
339
{
346
{
340
    lvt_error_t error;
347
    lvt_error_t error;
341
    lvt_lint_t lint;
348
    lvt_lint_t lint;
342
    tpr_t tpr;
349
    tpr_t tpr;
343
    svr_t svr;
350
    svr_t svr;
344
    icr_t icr;
351
    icr_t icr;
345
    tdcr_t tdcr;
352
    tdcr_t tdcr;
346
    lvt_tm_t tm;
353
    lvt_tm_t tm;
347
    ldr_t ldr;
354
    ldr_t ldr;
348
    dfr_t dfr;
355
    dfr_t dfr;
349
    uint32_t t1, t2;
356
    uint32_t t1, t2;
350
 
357
 
351
    /* Initialize LVT Error register. */
358
    /* Initialize LVT Error register. */
352
    error.value = l_apic[LVT_Err];
359
    error.value = l_apic[LVT_Err];
353
    error.masked = true;
360
    error.masked = true;
354
    l_apic[LVT_Err] = error.value;
361
    l_apic[LVT_Err] = error.value;
355
 
362
 
356
    /* Initialize LVT LINT0 register. */
363
    /* Initialize LVT LINT0 register. */
357
    lint.value = l_apic[LVT_LINT0];
364
    lint.value = l_apic[LVT_LINT0];
358
    lint.masked = true;
365
    lint.masked = true;
359
    l_apic[LVT_LINT0] = lint.value;
366
    l_apic[LVT_LINT0] = lint.value;
360
 
367
 
361
    /* Initialize LVT LINT1 register. */
368
    /* Initialize LVT LINT1 register. */
362
    lint.value = l_apic[LVT_LINT1];
369
    lint.value = l_apic[LVT_LINT1];
363
    lint.masked = true;
370
    lint.masked = true;
364
    l_apic[LVT_LINT1] = lint.value;
371
    l_apic[LVT_LINT1] = lint.value;
365
 
372
 
366
    /* Task Priority Register initialization. */
373
    /* Task Priority Register initialization. */
367
    tpr.value = l_apic[TPR];
374
    tpr.value = l_apic[TPR];
368
    tpr.pri_sc = 0;
375
    tpr.pri_sc = 0;
369
    tpr.pri = 0;
376
    tpr.pri = 0;
370
    l_apic[TPR] = tpr.value;
377
    l_apic[TPR] = tpr.value;
371
   
378
   
372
    /* Spurious-Interrupt Vector Register initialization. */
379
    /* Spurious-Interrupt Vector Register initialization. */
373
    svr.value = l_apic[SVR];
380
    svr.value = l_apic[SVR];
374
    svr.vector = VECTOR_APIC_SPUR;
381
    svr.vector = VECTOR_APIC_SPUR;
375
    svr.lapic_enabled = true;
382
    svr.lapic_enabled = true;
376
    svr.focus_checking = true;
383
    svr.focus_checking = true;
377
    l_apic[SVR] = svr.value;
384
    l_apic[SVR] = svr.value;
378
 
385
 
379
    if (CPU->arch.family >= 6)
386
    if (CPU->arch.family >= 6)
380
        enable_l_apic_in_msr();
387
        enable_l_apic_in_msr();
381
   
388
   
382
    /* Interrupt Command Register initialization. */
389
    /* Interrupt Command Register initialization. */
383
    icr.lo = l_apic[ICRlo];
390
    icr.lo = l_apic[ICRlo];
384
    icr.delmod = DELMOD_INIT;
391
    icr.delmod = DELMOD_INIT;
385
    icr.destmod = DESTMOD_PHYS;
392
    icr.destmod = DESTMOD_PHYS;
386
    icr.level = LEVEL_DEASSERT;
393
    icr.level = LEVEL_DEASSERT;
387
    icr.shorthand = SHORTHAND_ALL_INCL;
394
    icr.shorthand = SHORTHAND_ALL_INCL;
388
    icr.trigger_mode = TRIGMOD_LEVEL;
395
    icr.trigger_mode = TRIGMOD_LEVEL;
389
    l_apic[ICRlo] = icr.lo;
396
    l_apic[ICRlo] = icr.lo;
390
   
397
   
391
    /* Timer Divide Configuration Register initialization. */
398
    /* Timer Divide Configuration Register initialization. */
392
    tdcr.value = l_apic[TDCR];
399
    tdcr.value = l_apic[TDCR];
393
    tdcr.div_value = DIVIDE_1;
400
    tdcr.div_value = DIVIDE_1;
394
    l_apic[TDCR] = tdcr.value;
401
    l_apic[TDCR] = tdcr.value;
395
 
402
 
396
    /* Program local timer. */
403
    /* Program local timer. */
397
    tm.value = l_apic[LVT_Tm];
404
    tm.value = l_apic[LVT_Tm];
398
    tm.vector = VECTOR_CLK;
405
    tm.vector = VECTOR_CLK;
399
    tm.mode = TIMER_PERIODIC;
406
    tm.mode = TIMER_PERIODIC;
400
    tm.masked = false;
407
    tm.masked = false;
401
    l_apic[LVT_Tm] = tm.value;
408
    l_apic[LVT_Tm] = tm.value;
402
 
409
 
403
    /*
410
    /*
404
     * Measure and configure the timer to generate timer
411
     * Measure and configure the timer to generate timer
405
     * interrupt with period 1s/HZ seconds.
412
     * interrupt with period 1s/HZ seconds.
406
     */
413
     */
407
    t1 = l_apic[CCRT];
414
    t1 = l_apic[CCRT];
408
    l_apic[ICRT] = 0xffffffff;
415
    l_apic[ICRT] = 0xffffffff;
409
 
416
 
410
    while (l_apic[CCRT] == t1)
417
    while (l_apic[CCRT] == t1)
411
        ;
418
        ;
412
       
419
       
413
    t1 = l_apic[CCRT];
420
    t1 = l_apic[CCRT];
414
    delay(1000000/HZ);
421
    delay(1000000/HZ);
415
    t2 = l_apic[CCRT];
422
    t2 = l_apic[CCRT];
416
   
423
   
417
    l_apic[ICRT] = t1-t2;
424
    l_apic[ICRT] = t1-t2;
418
   
425
   
419
    /* Program Logical Destination Register. */
426
    /* Program Logical Destination Register. */
420
    ldr.value = l_apic[LDR];
427
    ldr.value = l_apic[LDR];
421
    if (CPU->id < sizeof(CPU->id)*8)    /* size in bits */
428
    if (CPU->id < sizeof(CPU->id)*8)    /* size in bits */
422
        ldr.id = (1<<CPU->id);
429
        ldr.id = (1<<CPU->id);
423
    l_apic[LDR] = ldr.value;
430
    l_apic[LDR] = ldr.value;
424
   
431
   
425
    /* Program Destination Format Register for Flat mode. */
432
    /* Program Destination Format Register for Flat mode. */
426
    dfr.value = l_apic[DFR];
433
    dfr.value = l_apic[DFR];
427
    dfr.model = MODEL_FLAT;
434
    dfr.model = MODEL_FLAT;
428
    l_apic[DFR] = dfr.value;
435
    l_apic[DFR] = dfr.value;
429
}
436
}
430
 
437
 
431
/** Local APIC End of Interrupt. */
438
/** Local APIC End of Interrupt. */
432
void l_apic_eoi(void)
439
void l_apic_eoi(void)
433
{
440
{
434
    l_apic[EOI] = 0;
441
    l_apic[EOI] = 0;
435
}
442
}
436
 
443
 
437
/** Dump content of Local APIC registers. */
444
/** Dump content of Local APIC registers. */
438
void l_apic_debug(void)
445
void l_apic_debug(void)
439
{
446
{
440
#ifdef LAPIC_VERBOSE
447
#ifdef LAPIC_VERBOSE
441
    lvt_tm_t tm;
448
    lvt_tm_t tm;
442
    lvt_lint_t lint;
449
    lvt_lint_t lint;
443
    lvt_error_t error; 
450
    lvt_error_t error; 
444
   
451
   
445
    printf("LVT on cpu%d, LAPIC ID: %d\n", CPU->id, l_apic_id());
452
    printf("LVT on cpu%d, LAPIC ID: %d\n", CPU->id, l_apic_id());
446
 
453
 
447
    tm.value = l_apic[LVT_Tm];
454
    tm.value = l_apic[LVT_Tm];
448
    printf("LVT Tm: vector=%hhd, %s, %s, %s\n", tm.vector, delivs_str[tm.delivs], mask_str[tm.masked], tm_mode_str[tm.mode]);
455
    printf("LVT Tm: vector=%hhd, %s, %s, %s\n", tm.vector, delivs_str[tm.delivs], mask_str[tm.masked], tm_mode_str[tm.mode]);
449
    lint.value = l_apic[LVT_LINT0];
456
    lint.value = l_apic[LVT_LINT0];
450
    printf("LVT LINT0: vector=%hhd, %s, %s, %s, irr=%d, %s, %s\n", tm.vector, delmod_str[lint.delmod], delivs_str[lint.delivs], intpol_str[lint.intpol], lint.irr, trigmod_str[lint.trigger_mode], mask_str[lint.masked]);
457
    printf("LVT LINT0: vector=%hhd, %s, %s, %s, irr=%d, %s, %s\n", tm.vector, delmod_str[lint.delmod], delivs_str[lint.delivs], intpol_str[lint.intpol], lint.irr, trigmod_str[lint.trigger_mode], mask_str[lint.masked]);
451
    lint.value = l_apic[LVT_LINT1];
458
    lint.value = l_apic[LVT_LINT1];
452
    printf("LVT LINT1: vector=%hhd, %s, %s, %s, irr=%d, %s, %s\n", tm.vector, delmod_str[lint.delmod], delivs_str[lint.delivs], intpol_str[lint.intpol], lint.irr, trigmod_str[lint.trigger_mode], mask_str[lint.masked]); 
459
    printf("LVT LINT1: vector=%hhd, %s, %s, %s, irr=%d, %s, %s\n", tm.vector, delmod_str[lint.delmod], delivs_str[lint.delivs], intpol_str[lint.intpol], lint.irr, trigmod_str[lint.trigger_mode], mask_str[lint.masked]); 
453
    error.value = l_apic[LVT_Err];
460
    error.value = l_apic[LVT_Err];
454
    printf("LVT Err: vector=%hhd, %s, %s\n", error.vector, delivs_str[error.delivs], mask_str[error.masked]);
461
    printf("LVT Err: vector=%hhd, %s, %s\n", error.vector, delivs_str[error.delivs], mask_str[error.masked]);
455
#endif
462
#endif
456
}
463
}
457
 
464
 
458
/** Get Local APIC ID.
465
/** Get Local APIC ID.
459
 *
466
 *
460
 * @return Local APIC ID.
467
 * @return Local APIC ID.
461
 */
468
 */
462
uint8_t l_apic_id(void)
469
uint8_t l_apic_id(void)
463
{
470
{
464
    l_apic_id_t idreg;
471
    l_apic_id_t idreg;
465
   
472
   
466
    idreg.value = l_apic[L_APIC_ID];
473
    idreg.value = l_apic[L_APIC_ID];
467
    return idreg.apic_id;
474
    return idreg.apic_id;
468
}
475
}
469
 
476
 
470
/** Read from IO APIC register.
477
/** Read from IO APIC register.
471
 *
478
 *
472
 * @param address IO APIC register address.
479
 * @param address IO APIC register address.
473
 *
480
 *
474
 * @return Content of the addressed IO APIC register.
481
 * @return Content of the addressed IO APIC register.
475
 */
482
 */
476
uint32_t io_apic_read(uint8_t address)
483
uint32_t io_apic_read(uint8_t address)
477
{
484
{
478
    io_regsel_t regsel;
485
    io_regsel_t regsel;
479
   
486
   
480
    regsel.value = io_apic[IOREGSEL];
487
    regsel.value = io_apic[IOREGSEL];
481
    regsel.reg_addr = address;
488
    regsel.reg_addr = address;
482
    io_apic[IOREGSEL] = regsel.value;
489
    io_apic[IOREGSEL] = regsel.value;
483
    return io_apic[IOWIN];
490
    return io_apic[IOWIN];
484
}
491
}
485
 
492
 
486
/** Write to IO APIC register.
493
/** Write to IO APIC register.
487
 *
494
 *
488
 * @param address IO APIC register address.
495
 * @param address IO APIC register address.
489
 * @param x Content to be written to the addressed IO APIC register.
496
 * @param x Content to be written to the addressed IO APIC register.
490
 */
497
 */
491
void io_apic_write(uint8_t address, uint32_t x)
498
void io_apic_write(uint8_t address, uint32_t x)
492
{
499
{
493
    io_regsel_t regsel;
500
    io_regsel_t regsel;
494
   
501
   
495
    regsel.value = io_apic[IOREGSEL];
502
    regsel.value = io_apic[IOREGSEL];
496
    regsel.reg_addr = address;
503
    regsel.reg_addr = address;
497
    io_apic[IOREGSEL] = regsel.value;
504
    io_apic[IOREGSEL] = regsel.value;
498
    io_apic[IOWIN] = x;
505
    io_apic[IOWIN] = x;
499
}
506
}
500
 
507
 
501
/** Change some attributes of one item in I/O Redirection Table.
508
/** Change some attributes of one item in I/O Redirection Table.
502
 *
509
 *
503
 * @param pin IO APIC pin number.
510
 * @param pin IO APIC pin number.
504
 * @param dest Interrupt destination address.
511
 * @param dest Interrupt destination address.
505
 * @param v Interrupt vector to trigger.
512
 * @param v Interrupt vector to trigger.
506
 * @param flags Flags.
513
 * @param flags Flags.
507
 */
514
 */
508
void io_apic_change_ioredtbl(int pin, int dest, uint8_t v, int flags)
515
void io_apic_change_ioredtbl(int pin, int dest, uint8_t v, int flags)
509
{
516
{
510
    io_redirection_reg_t reg;
517
    io_redirection_reg_t reg;
511
    int dlvr = DELMOD_FIXED;
518
    int dlvr = DELMOD_FIXED;
512
   
519
   
513
    if (flags & LOPRI)
520
    if (flags & LOPRI)
514
        dlvr = DELMOD_LOWPRI;
521
        dlvr = DELMOD_LOWPRI;
515
 
522
 
516
    reg.lo = io_apic_read(IOREDTBL + pin*2);
523
    reg.lo = io_apic_read(IOREDTBL + pin*2);
517
    reg.hi = io_apic_read(IOREDTBL + pin*2 + 1);
524
    reg.hi = io_apic_read(IOREDTBL + pin*2 + 1);
518
   
525
   
519
    reg.dest = dest;
526
    reg.dest = dest;
520
    reg.destmod = DESTMOD_LOGIC;
527
    reg.destmod = DESTMOD_LOGIC;
521
    reg.trigger_mode = TRIGMOD_EDGE;
528
    reg.trigger_mode = TRIGMOD_EDGE;
522
    reg.intpol = POLARITY_HIGH;
529
    reg.intpol = POLARITY_HIGH;
523
    reg.delmod = dlvr;
530
    reg.delmod = dlvr;
524
    reg.intvec = v;
531
    reg.intvec = v;
525
 
532
 
526
    io_apic_write(IOREDTBL + pin*2, reg.lo);
533
    io_apic_write(IOREDTBL + pin*2, reg.lo);
527
    io_apic_write(IOREDTBL + pin*2 + 1, reg.hi);
534
    io_apic_write(IOREDTBL + pin*2 + 1, reg.hi);
528
}
535
}
529
 
536
 
530
/** Mask IRQs in IO APIC.
537
/** Mask IRQs in IO APIC.
531
 *
538
 *
532
 * @param irqmask Bitmask of IRQs to be masked (0 = do not mask, 1 = mask).
539
 * @param irqmask Bitmask of IRQs to be masked (0 = do not mask, 1 = mask).
533
 */
540
 */
534
void io_apic_disable_irqs(uint16_t irqmask)
541
void io_apic_disable_irqs(uint16_t irqmask)
535
{
542
{
536
    io_redirection_reg_t reg;
543
    io_redirection_reg_t reg;
537
    unsigned int i;
544
    unsigned int i;
538
    int pin;
545
    int pin;
539
   
546
   
540
    for (i = 0; i < 16; i++) {
547
    for (i = 0; i < 16; i++) {
541
        if (irqmask & (1 << i)) {
548
        if (irqmask & (1 << i)) {
542
            /*
549
            /*
543
             * Mask the signal input in IO APIC if there is a
550
             * Mask the signal input in IO APIC if there is a
544
             * mapping for the respective IRQ number.
551
             * mapping for the respective IRQ number.
545
             */
552
             */
546
            pin = smp_irq_to_pin(i);
553
            pin = smp_irq_to_pin(i);
547
            if (pin != -1) {
554
            if (pin != -1) {
548
                reg.lo = io_apic_read(IOREDTBL + pin * 2);
555
                reg.lo = io_apic_read(IOREDTBL + pin * 2);
549
                reg.masked = true;
556
                reg.masked = true;
550
                io_apic_write(IOREDTBL + pin * 2, reg.lo);
557
                io_apic_write(IOREDTBL + pin * 2, reg.lo);
551
            }
558
            }
552
           
559
           
553
        }
560
        }
554
    }
561
    }
555
}
562
}
556
 
563
 
557
/** Unmask IRQs in IO APIC.
564
/** Unmask IRQs in IO APIC.
558
 *
565
 *
559
 * @param irqmask Bitmask of IRQs to be unmasked (0 = do not unmask, 1 = unmask).
566
 * @param irqmask Bitmask of IRQs to be unmasked (0 = do not unmask, 1 = unmask).
560
 */
567
 */
561
void io_apic_enable_irqs(uint16_t irqmask)
568
void io_apic_enable_irqs(uint16_t irqmask)
562
{
569
{
563
    unsigned int i;
570
    unsigned int i;
564
    int pin;
571
    int pin;
565
    io_redirection_reg_t reg;  
572
    io_redirection_reg_t reg;  
566
   
573
   
567
    for (i = 0;i < 16; i++) {
574
    for (i = 0;i < 16; i++) {
568
        if (irqmask & (1 << i)) {
575
        if (irqmask & (1 << i)) {
569
            /*
576
            /*
570
             * Unmask the signal input in IO APIC if there is a
577
             * Unmask the signal input in IO APIC if there is a
571
             * mapping for the respective IRQ number.
578
             * mapping for the respective IRQ number.
572
             */
579
             */
573
            pin = smp_irq_to_pin(i);
580
            pin = smp_irq_to_pin(i);
574
            if (pin != -1) {
581
            if (pin != -1) {
575
                reg.lo = io_apic_read(IOREDTBL + pin * 2);
582
                reg.lo = io_apic_read(IOREDTBL + pin * 2);
576
                reg.masked = false;
583
                reg.masked = false;
577
                io_apic_write(IOREDTBL + pin * 2, reg.lo);
584
                io_apic_write(IOREDTBL + pin * 2, reg.lo);
578
            }
585
            }
579
           
586
           
580
        }
587
        }
581
    }
588
    }
582
}
589
}
583
 
590
 
584
#endif /* CONFIG_SMP */
591
#endif /* CONFIG_SMP */
585
 
592
 
586
/** @}
593
/** @}
587
 */
594
 */
588
 
595