| 1 | /*
|
|---|
| 2 | * Copyright (c) 2008 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 ia32
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #include <smp/smp.h>
|
|---|
| 36 | #include <arch/smp/smp.h>
|
|---|
| 37 | #include <arch/smp/mps.h>
|
|---|
| 38 | #include <arch/smp/ap.h>
|
|---|
| 39 | #include <arch/boot/boot.h>
|
|---|
| 40 | #include <genarch/acpi/acpi.h>
|
|---|
| 41 | #include <genarch/acpi/madt.h>
|
|---|
| 42 | #include <config.h>
|
|---|
| 43 | #include <synch/waitq.h>
|
|---|
| 44 | #include <arch/pm.h>
|
|---|
| 45 | #include <func.h>
|
|---|
| 46 | #include <panic.h>
|
|---|
| 47 | #include <debug.h>
|
|---|
| 48 | #include <arch/asm.h>
|
|---|
| 49 | #include <mm/page.h>
|
|---|
| 50 | #include <mm/frame.h>
|
|---|
| 51 | #include <mm/km.h>
|
|---|
| 52 | #include <mm/slab.h>
|
|---|
| 53 | #include <mm/as.h>
|
|---|
| 54 | #include <log.h>
|
|---|
| 55 | #include <memstr.h>
|
|---|
| 56 | #include <arch/drivers/i8259.h>
|
|---|
| 57 | #include <cpu.h>
|
|---|
| 58 |
|
|---|
| 59 | #ifdef CONFIG_SMP
|
|---|
| 60 |
|
|---|
| 61 | static struct smp_config_operations *ops = NULL;
|
|---|
| 62 |
|
|---|
| 63 | void smp_init(void)
|
|---|
| 64 | {
|
|---|
| 65 | if (acpi_madt) {
|
|---|
| 66 | acpi_madt_parse();
|
|---|
| 67 | ops = &madt_config_operations;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | if (config.cpu_count == 1) {
|
|---|
| 71 | mps_init();
|
|---|
| 72 | ops = &mps_config_operations;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | if (config.cpu_count > 1) {
|
|---|
| 76 | l_apic = (uint32_t *) km_map((uintptr_t) l_apic, PAGE_SIZE,
|
|---|
| 77 | PAGE_WRITE | PAGE_NOT_CACHEABLE);
|
|---|
| 78 | io_apic = (uint32_t *) km_map((uintptr_t) io_apic, PAGE_SIZE,
|
|---|
| 79 | PAGE_WRITE | PAGE_NOT_CACHEABLE);
|
|---|
| 80 | }
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | static void cpu_arch_id_init(void)
|
|---|
| 84 | {
|
|---|
| 85 | ASSERT(ops != NULL);
|
|---|
| 86 | ASSERT(cpus != NULL);
|
|---|
| 87 |
|
|---|
| 88 | for (unsigned int i = 0; i < config.cpu_count; ++i) {
|
|---|
| 89 | cpus[i].arch.id = ops->cpu_apic_id(i);
|
|---|
| 90 | }
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | /*
|
|---|
| 94 | * Kernel thread for bringing up application processors. It becomes clear
|
|---|
| 95 | * that we need an arrangement like this (AP's being initialized by a kernel
|
|---|
| 96 | * thread), for a thread has its dedicated stack. (The stack used during the
|
|---|
| 97 | * BSP initialization (prior the very first call to scheduler()) will be used
|
|---|
| 98 | * as an initialization stack for each AP.)
|
|---|
| 99 | */
|
|---|
| 100 | void kmp(void *arg __attribute__((unused)))
|
|---|
| 101 | {
|
|---|
| 102 | unsigned int i;
|
|---|
| 103 |
|
|---|
| 104 | ASSERT(ops != NULL);
|
|---|
| 105 |
|
|---|
| 106 | /*
|
|---|
| 107 | * SMP initialized, cpus array allocated. Assign each CPU its
|
|---|
| 108 | * physical APIC ID.
|
|---|
| 109 | */
|
|---|
| 110 | cpu_arch_id_init();
|
|---|
| 111 |
|
|---|
| 112 | /*
|
|---|
| 113 | * We need to access data in frame 0.
|
|---|
| 114 | * We boldly make use of kernel address space mapping.
|
|---|
| 115 | */
|
|---|
| 116 |
|
|---|
| 117 | /*
|
|---|
| 118 | * Set the warm-reset vector to the real-mode address of 4K-aligned ap_boot()
|
|---|
| 119 | */
|
|---|
| 120 | *((uint16_t *) (PA2KA(0x467 + 0))) =
|
|---|
| 121 | (uint16_t) (((uintptr_t) ap_boot) >> 4); /* segment */
|
|---|
| 122 | *((uint16_t *) (PA2KA(0x467 + 2))) = 0; /* offset */
|
|---|
| 123 |
|
|---|
| 124 | /*
|
|---|
| 125 | * Save 0xa to address 0xf of the CMOS RAM.
|
|---|
| 126 | * BIOS will not do the POST after the INIT signal.
|
|---|
| 127 | */
|
|---|
| 128 | pio_write_8((ioport8_t *) 0x70, 0xf);
|
|---|
| 129 | pio_write_8((ioport8_t *) 0x71, 0xa);
|
|---|
| 130 |
|
|---|
| 131 | pic_disable_irqs(0xffff);
|
|---|
| 132 | apic_init();
|
|---|
| 133 |
|
|---|
| 134 | for (i = 0; i < config.cpu_count; i++) {
|
|---|
| 135 | /*
|
|---|
| 136 | * Skip processors marked unusable.
|
|---|
| 137 | */
|
|---|
| 138 | if (!ops->cpu_enabled(i))
|
|---|
| 139 | continue;
|
|---|
| 140 |
|
|---|
| 141 | /*
|
|---|
| 142 | * The bootstrap processor is already up.
|
|---|
| 143 | */
|
|---|
| 144 | if (ops->cpu_bootstrap(i))
|
|---|
| 145 | continue;
|
|---|
| 146 |
|
|---|
| 147 | if (ops->cpu_apic_id(i) == bsp_l_apic) {
|
|---|
| 148 | log(LF_ARCH, LVL_ERROR, "kmp: bad processor entry #%u, "
|
|---|
| 149 | "will not send IPI to myself", i);
|
|---|
| 150 | continue;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | /*
|
|---|
| 154 | * Prepare new GDT for CPU in question.
|
|---|
| 155 | */
|
|---|
| 156 |
|
|---|
| 157 | /* XXX Flag FRAME_LOW_4_GiB was removed temporarily,
|
|---|
| 158 | * it needs to be replaced by a generic fuctionality of
|
|---|
| 159 | * the memory subsystem
|
|---|
| 160 | */
|
|---|
| 161 | descriptor_t *gdt_new =
|
|---|
| 162 | (descriptor_t *) malloc(GDT_ITEMS * sizeof(descriptor_t),
|
|---|
| 163 | FRAME_ATOMIC);
|
|---|
| 164 | if (!gdt_new)
|
|---|
| 165 | panic("Cannot allocate memory for GDT.");
|
|---|
| 166 |
|
|---|
| 167 | memcpy(gdt_new, gdt, GDT_ITEMS * sizeof(descriptor_t));
|
|---|
| 168 | memsetb(&gdt_new[TSS_DES], sizeof(descriptor_t), 0);
|
|---|
| 169 | protected_ap_gdtr.limit = GDT_ITEMS * sizeof(descriptor_t);
|
|---|
| 170 | protected_ap_gdtr.base = KA2PA((uintptr_t) gdt_new);
|
|---|
| 171 | gdtr.base = (uintptr_t) gdt_new;
|
|---|
| 172 |
|
|---|
| 173 | if (l_apic_send_init_ipi(ops->cpu_apic_id(i))) {
|
|---|
| 174 | /*
|
|---|
| 175 | * There may be just one AP being initialized at
|
|---|
| 176 | * the time. After it comes completely up, it is
|
|---|
| 177 | * supposed to wake us up.
|
|---|
| 178 | */
|
|---|
| 179 | if (waitq_sleep_timeout(&ap_completion_wq, 1000000,
|
|---|
| 180 | SYNCH_FLAGS_NONE) == ESYNCH_TIMEOUT) {
|
|---|
| 181 | log(LF_ARCH, LVL_NOTE, "%s: waiting for cpu%u "
|
|---|
| 182 | "(APIC ID = %d) timed out", __FUNCTION__,
|
|---|
| 183 | i, ops->cpu_apic_id(i));
|
|---|
| 184 | }
|
|---|
| 185 | } else
|
|---|
| 186 | log(LF_ARCH, LVL_ERROR, "INIT IPI for l_apic%d failed",
|
|---|
| 187 | ops->cpu_apic_id(i));
|
|---|
| 188 | }
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | int smp_irq_to_pin(unsigned int irq)
|
|---|
| 192 | {
|
|---|
| 193 | ASSERT(ops != NULL);
|
|---|
| 194 | return ops->irq_to_pin(irq);
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | #endif /* CONFIG_SMP */
|
|---|
| 198 |
|
|---|
| 199 | /** @}
|
|---|
| 200 | */
|
|---|