| 1 | /*
|
|---|
| 2 | * Copyright (c) 2005 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 genarch
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file
|
|---|
| 34 | * @brief Multiple APIC Description Table (MADT) parsing.
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <typedefs.h>
|
|---|
| 38 | #include <genarch/acpi/acpi.h>
|
|---|
| 39 | #include <genarch/acpi/madt.h>
|
|---|
| 40 | #include <arch/smp/apic.h>
|
|---|
| 41 | #include <arch/smp/smp.h>
|
|---|
| 42 | #include <panic.h>
|
|---|
| 43 | #include <debug.h>
|
|---|
| 44 | #include <config.h>
|
|---|
| 45 | #include <print.h>
|
|---|
| 46 | #include <mm/slab.h>
|
|---|
| 47 | #include <memstr.h>
|
|---|
| 48 | #include <sort.h>
|
|---|
| 49 |
|
|---|
| 50 | struct acpi_madt *acpi_madt = NULL;
|
|---|
| 51 |
|
|---|
| 52 | #ifdef CONFIG_SMP
|
|---|
| 53 |
|
|---|
| 54 | /**
|
|---|
| 55 | * Standard ISA IRQ map; can be overriden by
|
|---|
| 56 | * Interrupt Source Override entries of MADT.
|
|---|
| 57 | */
|
|---|
| 58 | static int isa_irq_map[] =
|
|---|
| 59 | { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
|
|---|
| 60 |
|
|---|
| 61 | struct madt_l_apic *madt_l_apic_entries = NULL;
|
|---|
| 62 | struct madt_io_apic *madt_io_apic_entries = NULL;
|
|---|
| 63 |
|
|---|
| 64 | static size_t madt_l_apic_entry_index = 0;
|
|---|
| 65 | static size_t madt_io_apic_entry_index = 0;
|
|---|
| 66 | static size_t madt_l_apic_entry_cnt = 0;
|
|---|
| 67 | static size_t madt_io_apic_entry_cnt = 0;
|
|---|
| 68 |
|
|---|
| 69 | static struct madt_apic_header **madt_entries_index = NULL;
|
|---|
| 70 |
|
|---|
| 71 | const char *entry[] = {
|
|---|
| 72 | "L_APIC",
|
|---|
| 73 | "IO_APIC",
|
|---|
| 74 | "INTR_SRC_OVRD",
|
|---|
| 75 | "NMI_SRC",
|
|---|
| 76 | "L_APIC_NMI",
|
|---|
| 77 | "L_APIC_ADDR_OVRD",
|
|---|
| 78 | "IO_SAPIC",
|
|---|
| 79 | "L_SAPIC",
|
|---|
| 80 | "PLATFORM_INTR_SRC"
|
|---|
| 81 | };
|
|---|
| 82 |
|
|---|
| 83 | static uint8_t madt_cpu_apic_id(size_t i)
|
|---|
| 84 | {
|
|---|
| 85 | ASSERT(i < madt_l_apic_entry_cnt);
|
|---|
| 86 |
|
|---|
| 87 | return ((struct madt_l_apic *)
|
|---|
| 88 | madt_entries_index[madt_l_apic_entry_index + i])->apic_id;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | static bool madt_cpu_enabled(size_t i)
|
|---|
| 92 | {
|
|---|
| 93 | ASSERT(i < madt_l_apic_entry_cnt);
|
|---|
| 94 |
|
|---|
| 95 | /*
|
|---|
| 96 | * FIXME: The current local APIC driver limits usable
|
|---|
| 97 | * CPU IDs to 8.
|
|---|
| 98 | *
|
|---|
| 99 | */
|
|---|
| 100 | if (i > 7)
|
|---|
| 101 | return false;
|
|---|
| 102 |
|
|---|
| 103 | return ((struct madt_l_apic *)
|
|---|
| 104 | madt_entries_index[madt_l_apic_entry_index + i])->flags & 0x1;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | static bool madt_cpu_bootstrap(size_t i)
|
|---|
| 108 | {
|
|---|
| 109 | ASSERT(i < madt_l_apic_entry_cnt);
|
|---|
| 110 |
|
|---|
| 111 | return ((struct madt_l_apic *)
|
|---|
| 112 | madt_entries_index[madt_l_apic_entry_index + i])->apic_id ==
|
|---|
| 113 | bsp_l_apic;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | static int madt_irq_to_pin(unsigned int irq)
|
|---|
| 117 | {
|
|---|
| 118 | if (irq >= sizeof(isa_irq_map) / sizeof(int))
|
|---|
| 119 | return (int) irq;
|
|---|
| 120 |
|
|---|
| 121 | return isa_irq_map[irq];
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | /** ACPI MADT Implementation of SMP configuration interface.
|
|---|
| 125 | *
|
|---|
| 126 | */
|
|---|
| 127 | struct smp_config_operations madt_config_operations = {
|
|---|
| 128 | .cpu_enabled = madt_cpu_enabled,
|
|---|
| 129 | .cpu_bootstrap = madt_cpu_bootstrap,
|
|---|
| 130 | .cpu_apic_id = madt_cpu_apic_id,
|
|---|
| 131 | .irq_to_pin = madt_irq_to_pin
|
|---|
| 132 | };
|
|---|
| 133 |
|
|---|
| 134 | static int madt_cmp(void *a, void *b, void *arg)
|
|---|
| 135 | {
|
|---|
| 136 | uint8_t typea = (*((struct madt_apic_header **) a))->type;
|
|---|
| 137 | uint8_t typeb = (*((struct madt_apic_header **) b))->type;
|
|---|
| 138 |
|
|---|
| 139 | if (typea > typeb)
|
|---|
| 140 | return 1;
|
|---|
| 141 |
|
|---|
| 142 | if (typea < typeb)
|
|---|
| 143 | return -1;
|
|---|
| 144 |
|
|---|
| 145 | return 0;
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | static void madt_l_apic_entry(struct madt_l_apic *la, size_t i)
|
|---|
| 149 | {
|
|---|
| 150 | if (madt_l_apic_entry_cnt == 0)
|
|---|
| 151 | madt_l_apic_entry_index = i;
|
|---|
| 152 |
|
|---|
| 153 | madt_l_apic_entry_cnt++;
|
|---|
| 154 |
|
|---|
| 155 | if (!(la->flags & 0x1)) {
|
|---|
| 156 | /* Processor is unusable, skip it. */
|
|---|
| 157 | return;
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | apic_id_mask |= 1 << la->apic_id;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | static void madt_io_apic_entry(struct madt_io_apic *ioa, size_t i)
|
|---|
| 164 | {
|
|---|
| 165 | if (madt_io_apic_entry_cnt == 0) {
|
|---|
| 166 | /* Remember index of the first io apic entry */
|
|---|
| 167 | madt_io_apic_entry_index = i;
|
|---|
| 168 | io_apic = (uint32_t *) (sysarg_t) ioa->io_apic_address;
|
|---|
| 169 | } else {
|
|---|
| 170 | /* Currently not supported */
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | madt_io_apic_entry_cnt++;
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | static void madt_intr_src_ovrd_entry(struct madt_intr_src_ovrd *override,
|
|---|
| 177 | size_t i)
|
|---|
| 178 | {
|
|---|
| 179 | ASSERT(override->source < sizeof(isa_irq_map) / sizeof(int));
|
|---|
| 180 |
|
|---|
| 181 | isa_irq_map[override->source] = override->global_int;
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | void acpi_madt_parse(void)
|
|---|
| 185 | {
|
|---|
| 186 | struct madt_apic_header *end = (struct madt_apic_header *)
|
|---|
| 187 | (((uint8_t *) acpi_madt) + acpi_madt->header.length);
|
|---|
| 188 | struct madt_apic_header *hdr;
|
|---|
| 189 |
|
|---|
| 190 | l_apic = (uint32_t *) (sysarg_t) acpi_madt->l_apic_address;
|
|---|
| 191 |
|
|---|
| 192 | /* Count MADT entries */
|
|---|
| 193 | unsigned int madt_entries_index_cnt = 0;
|
|---|
| 194 | for (hdr = acpi_madt->apic_header; hdr < end;
|
|---|
| 195 | hdr = (struct madt_apic_header *) (((uint8_t *) hdr) + hdr->length))
|
|---|
| 196 | madt_entries_index_cnt++;
|
|---|
| 197 |
|
|---|
| 198 | /* Create MADT APIC entries index array */
|
|---|
| 199 | madt_entries_index = (struct madt_apic_header **)
|
|---|
| 200 | malloc(madt_entries_index_cnt * sizeof(struct madt_apic_header *),
|
|---|
| 201 | FRAME_ATOMIC);
|
|---|
| 202 | if (!madt_entries_index)
|
|---|
| 203 | panic("Memory allocation error.");
|
|---|
| 204 |
|
|---|
| 205 | size_t i = 0;
|
|---|
| 206 |
|
|---|
| 207 | for (hdr = acpi_madt->apic_header; hdr < end;
|
|---|
| 208 | hdr = (struct madt_apic_header *) (((uint8_t *) hdr) + hdr->length)) {
|
|---|
| 209 | madt_entries_index[i] = hdr;
|
|---|
| 210 | i++;
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | /* Sort MADT index structure */
|
|---|
| 214 | if (!gsort(madt_entries_index, madt_entries_index_cnt,
|
|---|
| 215 | sizeof(struct madt_apic_header *), madt_cmp, NULL))
|
|---|
| 216 | panic("Sorting error.");
|
|---|
| 217 |
|
|---|
| 218 | /* Parse MADT entries */
|
|---|
| 219 | for (i = 0; i < madt_entries_index_cnt; i++) {
|
|---|
| 220 | hdr = madt_entries_index[i];
|
|---|
| 221 |
|
|---|
| 222 | switch (hdr->type) {
|
|---|
| 223 | case MADT_L_APIC:
|
|---|
| 224 | madt_l_apic_entry((struct madt_l_apic *) hdr, i);
|
|---|
| 225 | break;
|
|---|
| 226 | case MADT_IO_APIC:
|
|---|
| 227 | madt_io_apic_entry((struct madt_io_apic *) hdr, i);
|
|---|
| 228 | break;
|
|---|
| 229 | case MADT_INTR_SRC_OVRD:
|
|---|
| 230 | madt_intr_src_ovrd_entry((struct madt_intr_src_ovrd *) hdr, i);
|
|---|
| 231 | break;
|
|---|
| 232 | case MADT_NMI_SRC:
|
|---|
| 233 | case MADT_L_APIC_NMI:
|
|---|
| 234 | case MADT_L_APIC_ADDR_OVRD:
|
|---|
| 235 | case MADT_IO_SAPIC:
|
|---|
| 236 | case MADT_L_SAPIC:
|
|---|
| 237 | case MADT_PLATFORM_INTR_SRC:
|
|---|
| 238 | printf("MADT: Skipping %s entry (type=%" PRIu8 ")\n",
|
|---|
| 239 | entry[hdr->type], hdr->type);
|
|---|
| 240 | break;
|
|---|
| 241 | default:
|
|---|
| 242 | if ((hdr->type >= MADT_RESERVED_SKIP_BEGIN)
|
|---|
| 243 | && (hdr->type <= MADT_RESERVED_SKIP_END))
|
|---|
| 244 | printf("MADT: Skipping reserved entry (type=%" PRIu8 ")\n",
|
|---|
| 245 | hdr->type);
|
|---|
| 246 |
|
|---|
| 247 | if (hdr->type >= MADT_RESERVED_OEM_BEGIN)
|
|---|
| 248 | printf("MADT: Skipping OEM entry (type=%" PRIu8 ")\n",
|
|---|
| 249 | hdr->type);
|
|---|
| 250 |
|
|---|
| 251 | break;
|
|---|
| 252 | }
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 | if (madt_l_apic_entry_cnt > 0)
|
|---|
| 256 | config.cpu_count = madt_l_apic_entry_cnt;
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | #endif /* CONFIG_SMP */
|
|---|
| 260 |
|
|---|
| 261 | /** @}
|
|---|
| 262 | */
|
|---|