Patrick Georgi | 11f0079 | 2020-03-04 15:10:45 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 2 | |
Kyösti Mälkki | ea2fb8d | 2021-06-03 23:12:09 +0300 | [diff] [blame] | 3 | #include <assert.h> |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 4 | #include <device/mmio.h> |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 5 | #include <arch/ioapic.h> |
| 6 | #include <console/console.h> |
| 7 | #include <cpu/x86/lapic.h> |
| 8 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 9 | u32 io_apic_read(void *ioapic_base, u32 reg) |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 10 | { |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 11 | write32(ioapic_base, reg); |
| 12 | return read32(ioapic_base + 0x10); |
| 13 | } |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 14 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 15 | void io_apic_write(void *ioapic_base, u32 reg, u32 value) |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 16 | { |
| 17 | write32(ioapic_base, reg); |
| 18 | write32(ioapic_base + 0x10, value); |
| 19 | } |
| 20 | |
Kyösti Mälkki | 93c1eef | 2021-06-03 23:14:05 +0300 | [diff] [blame] | 21 | static void write_vector(void *ioapic_base, u8 vector, u32 high, u32 low) |
| 22 | { |
| 23 | io_apic_write(ioapic_base, vector * 2 + 0x10, low); |
| 24 | io_apic_write(ioapic_base, vector * 2 + 0x11, high); |
| 25 | |
| 26 | printk(BIOS_SPEW, "IOAPIC: vector 0x%02x value 0x%08x 0x%08x\n", |
| 27 | vector, high, low); |
| 28 | } |
| 29 | |
Kyösti Mälkki | 04a4037 | 2021-06-06 08:04:28 +0300 | [diff] [blame] | 30 | /* Bits 23-16 of register 0x01 specify the maximum redirection entry, which |
| 31 | * is the number of interrupts minus 1. */ |
| 32 | unsigned int ioapic_get_max_vectors(void *ioapic_base) |
Patrick Georgi | d1de45e | 2013-01-23 13:45:23 +0100 | [diff] [blame] | 33 | { |
Kyösti Mälkki | 04a4037 | 2021-06-06 08:04:28 +0300 | [diff] [blame] | 34 | u32 reg; |
| 35 | u8 count; |
Patrick Georgi | d1de45e | 2013-01-23 13:45:23 +0100 | [diff] [blame] | 36 | |
Kyösti Mälkki | 04a4037 | 2021-06-06 08:04:28 +0300 | [diff] [blame] | 37 | reg = io_apic_read(ioapic_base, 0x01); |
Kyösti Mälkki | 6139ff9 | 2021-10-18 19:43:49 +0300 | [diff] [blame] | 38 | count = (reg >> 16) & 0xff; |
Kyösti Mälkki | 04a4037 | 2021-06-06 08:04:28 +0300 | [diff] [blame] | 39 | |
Kyösti Mälkki | 6139ff9 | 2021-10-18 19:43:49 +0300 | [diff] [blame] | 40 | if (count == 0xff) |
Kyösti Mälkki | 04a4037 | 2021-06-06 08:04:28 +0300 | [diff] [blame] | 41 | count = 23; |
| 42 | count++; |
| 43 | |
| 44 | printk(BIOS_DEBUG, "IOAPIC: %d interrupts\n", count); |
| 45 | return count; |
| 46 | } |
| 47 | |
| 48 | /* Set maximum number of redirection entries (MRE). It is write-once register |
| 49 | * for some chipsets, and a negative mre_count will lock it to the number |
| 50 | * of vectors read from the register. */ |
| 51 | void ioapic_set_max_vectors(void *ioapic_base, int mre_count) |
| 52 | { |
| 53 | u32 reg; |
| 54 | u8 count; |
| 55 | |
| 56 | reg = io_apic_read(ioapic_base, 0x01); |
Kyösti Mälkki | 6139ff9 | 2021-10-18 19:43:49 +0300 | [diff] [blame] | 57 | count = (reg >> 16) & 0xff; |
Kyösti Mälkki | 04a4037 | 2021-06-06 08:04:28 +0300 | [diff] [blame] | 58 | if (mre_count > 0) |
| 59 | count = mre_count - 1; |
| 60 | reg &= ~(0xff << 16); |
| 61 | reg |= count << 16; |
Iru Cai | 308a5b9 | 2021-10-23 21:44:02 +0800 | [diff] [blame] | 62 | io_apic_write(ioapic_base, 0x01, reg); |
Kyösti Mälkki | 04a4037 | 2021-06-06 08:04:28 +0300 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void ioapic_lock_max_vectors(void *ioapic_base) |
| 66 | { |
| 67 | ioapic_set_max_vectors(ioapic_base, -1); |
Patrick Georgi | d1de45e | 2013-01-23 13:45:23 +0100 | [diff] [blame] | 68 | } |
| 69 | |
Kyösti Mälkki | ea2fb8d | 2021-06-03 23:12:09 +0300 | [diff] [blame] | 70 | static void clear_vectors(void *ioapic_base, u8 first, u8 last) |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 71 | { |
| 72 | u32 low, high; |
Kyösti Mälkki | ea2fb8d | 2021-06-03 23:12:09 +0300 | [diff] [blame] | 73 | u8 i; |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 74 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 75 | printk(BIOS_DEBUG, "IOAPIC: Clearing IOAPIC at %p\n", ioapic_base); |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 76 | |
Marc Jones | f7dc972 | 2018-03-31 22:45:35 -0600 | [diff] [blame] | 77 | low = INT_DISABLED; |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 78 | high = NONE; |
| 79 | |
Kyösti Mälkki | 93c1eef | 2021-06-03 23:14:05 +0300 | [diff] [blame] | 80 | for (i = first; i <= last; i++) |
| 81 | write_vector(ioapic_base, i, high, low); |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 82 | |
| 83 | if (io_apic_read(ioapic_base, 0x10) == 0xffffffff) { |
Uwe Hermann | e4990365 | 2010-10-14 23:40:10 +0000 | [diff] [blame] | 84 | printk(BIOS_WARNING, "IOAPIC not responding.\n"); |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 85 | return; |
| 86 | } |
| 87 | } |
| 88 | |
Kyösti Mälkki | ea2fb8d | 2021-06-03 23:12:09 +0300 | [diff] [blame] | 89 | static void route_i8259_irq0(void *ioapic_base) |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 90 | { |
| 91 | u32 bsp_lapicid = lapicid(); |
Kyösti Mälkki | ea2fb8d | 2021-06-03 23:12:09 +0300 | [diff] [blame] | 92 | u32 low, high; |
| 93 | |
| 94 | ASSERT(bsp_lapicid < 255); |
| 95 | |
| 96 | printk(BIOS_DEBUG, "IOAPIC: Bootstrap Processor Local APIC = 0x%02x\n", |
| 97 | bsp_lapicid); |
| 98 | |
| 99 | /* Enable Virtual Wire Mode. Should this be LOGICAL_DEST instead? */ |
| 100 | low = INT_ENABLED | TRIGGER_EDGE | POLARITY_HIGH | PHYSICAL_DEST | ExtINT; |
| 101 | high = bsp_lapicid << (56 - 32); |
| 102 | |
Kyösti Mälkki | 93c1eef | 2021-06-03 23:14:05 +0300 | [diff] [blame] | 103 | write_vector(ioapic_base, 0, high, low); |
Kyösti Mälkki | ea2fb8d | 2021-06-03 23:12:09 +0300 | [diff] [blame] | 104 | |
| 105 | if (io_apic_read(ioapic_base, 0x10) == 0xffffffff) { |
| 106 | printk(BIOS_WARNING, "IOAPIC not responding.\n"); |
| 107 | return; |
| 108 | } |
Kyösti Mälkki | ea2fb8d | 2021-06-03 23:12:09 +0300 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void set_ioapic_id(void *ioapic_base, u8 ioapic_id) |
| 112 | { |
Paul Menzel | 1b3e176 | 2013-04-23 14:49:41 +0200 | [diff] [blame] | 113 | int i; |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 114 | |
Julius Werner | 540a980 | 2019-12-09 13:03:29 -0800 | [diff] [blame] | 115 | printk(BIOS_DEBUG, "IOAPIC: Initializing IOAPIC at %p\n", |
Uwe Hermann | e4990365 | 2010-10-14 23:40:10 +0000 | [diff] [blame] | 116 | ioapic_base); |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 117 | |
| 118 | if (ioapic_id) { |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 119 | printk(BIOS_DEBUG, "IOAPIC: ID = 0x%02x\n", ioapic_id); |
Uwe Hermann | e4990365 | 2010-10-14 23:40:10 +0000 | [diff] [blame] | 120 | /* Set IOAPIC ID if it has been specified. */ |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 121 | io_apic_write(ioapic_base, 0x00, |
Kyösti Mälkki | 939103c | 2011-10-19 07:23:51 +0300 | [diff] [blame] | 122 | (io_apic_read(ioapic_base, 0x00) & 0xf0ffffff) | |
Uwe Hermann | e4990365 | 2010-10-14 23:40:10 +0000 | [diff] [blame] | 123 | (ioapic_id << 24)); |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 124 | } |
Paul Menzel | 1b3e176 | 2013-04-23 14:49:41 +0200 | [diff] [blame] | 125 | |
| 126 | printk(BIOS_SPEW, "IOAPIC: Dumping registers\n"); |
| 127 | for (i = 0; i < 3; i++) |
| 128 | printk(BIOS_SPEW, " reg 0x%04x: 0x%08x\n", i, |
| 129 | io_apic_read(ioapic_base, i)); |
| 130 | |
Kyösti Mälkki | db4f875 | 2012-01-31 17:24:12 +0200 | [diff] [blame] | 131 | } |
| 132 | |
Kyösti Mälkki | 401ec98 | 2021-06-06 08:27:15 +0300 | [diff] [blame] | 133 | u8 get_ioapic_id(void *ioapic_base) |
| 134 | { |
| 135 | return (io_apic_read(ioapic_base, 0x00) >> 24) & 0x0f; |
| 136 | } |
| 137 | |
| 138 | u8 get_ioapic_version(void *ioapic_base) |
| 139 | { |
| 140 | return io_apic_read(ioapic_base, 0x01) & 0xff; |
| 141 | } |
| 142 | |
Kyösti Mälkki | 8c9a89d | 2021-06-06 08:14:57 +0300 | [diff] [blame] | 143 | void ioapic_set_boot_config(void *ioapic_base, bool irq_on_fsb) |
Kyösti Mälkki | db4f875 | 2012-01-31 17:24:12 +0200 | [diff] [blame] | 144 | { |
Kyösti Mälkki | 8cc25d2 | 2021-06-03 18:36:05 +0300 | [diff] [blame] | 145 | if (irq_on_fsb) { |
Martin Roth | 898a775 | 2017-06-01 11:39:59 -0600 | [diff] [blame] | 146 | /* |
| 147 | * For the Pentium 4 and above APICs deliver their interrupts |
| 148 | * on the front side bus, enable that. |
| 149 | */ |
| 150 | printk(BIOS_DEBUG, "IOAPIC: Enabling interrupts on FSB\n"); |
| 151 | io_apic_write(ioapic_base, 0x03, |
| 152 | io_apic_read(ioapic_base, 0x03) | (1 << 0)); |
Kyösti Mälkki | 8cc25d2 | 2021-06-03 18:36:05 +0300 | [diff] [blame] | 153 | } else { |
Martin Roth | 898a775 | 2017-06-01 11:39:59 -0600 | [diff] [blame] | 154 | printk(BIOS_DEBUG, |
| 155 | "IOAPIC: Enabling interrupts on APIC serial bus\n"); |
| 156 | io_apic_write(ioapic_base, 0x03, 0); |
| 157 | } |
Kyösti Mälkki | 8c9a89d | 2021-06-06 08:14:57 +0300 | [diff] [blame] | 158 | } |
| 159 | |
Kyösti Mälkki | 6644d7c | 2021-06-06 08:10:05 +0300 | [diff] [blame] | 160 | void setup_ioapic(void *ioapic_base, u8 ioapic_id) |
Kyösti Mälkki | 8c9a89d | 2021-06-06 08:14:57 +0300 | [diff] [blame] | 161 | { |
Kyösti Mälkki | 8c9a89d | 2021-06-06 08:14:57 +0300 | [diff] [blame] | 162 | set_ioapic_id(ioapic_base, ioapic_id); |
Kyösti Mälkki | 04a4037 | 2021-06-06 08:04:28 +0300 | [diff] [blame] | 163 | clear_vectors(ioapic_base, 0, ioapic_get_max_vectors(ioapic_base) - 1); |
Kyösti Mälkki | 6644d7c | 2021-06-06 08:10:05 +0300 | [diff] [blame] | 164 | route_i8259_irq0(ioapic_base); |
Kyösti Mälkki | db4f875 | 2012-01-31 17:24:12 +0200 | [diff] [blame] | 165 | } |
Kyösti Mälkki | 0ea8f89 | 2021-06-08 11:28:25 +0300 | [diff] [blame^] | 166 | |
| 167 | void register_new_ioapic_gsi0(void *ioapic_base) |
| 168 | { |
| 169 | setup_ioapic(ioapic_base, 0); |
| 170 | } |
| 171 | |
| 172 | void register_new_ioapic(void *ioapic_base) |
| 173 | { |
| 174 | static u8 ioapic_id; |
| 175 | ioapic_id++; |
| 176 | set_ioapic_id(ioapic_base, ioapic_id); |
| 177 | clear_vectors(ioapic_base, 0, ioapic_get_max_vectors(ioapic_base) - 1); |
| 178 | } |