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 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 21 | static int ioapic_interrupt_count(void *ioapic_base) |
Patrick Georgi | d1de45e | 2013-01-23 13:45:23 +0100 | [diff] [blame] | 22 | { |
| 23 | /* Read the available number of interrupts. */ |
| 24 | int ioapic_interrupts = (io_apic_read(ioapic_base, 0x01) >> 16) & 0xff; |
Kyösti Mälkki | ea2fb8d | 2021-06-03 23:12:09 +0300 | [diff] [blame^] | 25 | if (!ioapic_interrupts || ioapic_interrupts == 0xff) |
Patrick Georgi | d1de45e | 2013-01-23 13:45:23 +0100 | [diff] [blame] | 26 | ioapic_interrupts = 23; |
| 27 | ioapic_interrupts += 1; /* Bits 23-16 specify the maximum redirection |
| 28 | entry, which is the number of interrupts |
| 29 | minus 1. */ |
| 30 | printk(BIOS_DEBUG, "IOAPIC: %d interrupts\n", ioapic_interrupts); |
| 31 | |
| 32 | return ioapic_interrupts; |
| 33 | } |
| 34 | |
Kyösti Mälkki | ea2fb8d | 2021-06-03 23:12:09 +0300 | [diff] [blame^] | 35 | static void clear_vectors(void *ioapic_base, u8 first, u8 last) |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 36 | { |
| 37 | u32 low, high; |
Kyösti Mälkki | ea2fb8d | 2021-06-03 23:12:09 +0300 | [diff] [blame^] | 38 | u8 i; |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 39 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 40 | printk(BIOS_DEBUG, "IOAPIC: Clearing IOAPIC at %p\n", ioapic_base); |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 41 | |
Marc Jones | f7dc972 | 2018-03-31 22:45:35 -0600 | [diff] [blame] | 42 | low = INT_DISABLED; |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 43 | high = NONE; |
| 44 | |
Kyösti Mälkki | ea2fb8d | 2021-06-03 23:12:09 +0300 | [diff] [blame^] | 45 | for (i = first; i <= last; i++) { |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 46 | io_apic_write(ioapic_base, i * 2 + 0x10, low); |
| 47 | io_apic_write(ioapic_base, i * 2 + 0x11, high); |
| 48 | |
Kyösti Mälkki | ea2fb8d | 2021-06-03 23:12:09 +0300 | [diff] [blame^] | 49 | printk(BIOS_SPEW, "IOAPIC: vector 0x%02x value 0x%08x 0x%08x\n", |
Uwe Hermann | e4990365 | 2010-10-14 23:40:10 +0000 | [diff] [blame] | 50 | i, high, low); |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | if (io_apic_read(ioapic_base, 0x10) == 0xffffffff) { |
Uwe Hermann | e4990365 | 2010-10-14 23:40:10 +0000 | [diff] [blame] | 54 | printk(BIOS_WARNING, "IOAPIC not responding.\n"); |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 55 | return; |
| 56 | } |
| 57 | } |
| 58 | |
Kyösti Mälkki | ea2fb8d | 2021-06-03 23:12:09 +0300 | [diff] [blame^] | 59 | void clear_ioapic(void *ioapic_base) |
| 60 | { |
| 61 | clear_vectors(ioapic_base, 0, ioapic_interrupt_count(ioapic_base) - 1); |
| 62 | } |
| 63 | |
| 64 | static void route_i8259_irq0(void *ioapic_base) |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 65 | { |
| 66 | u32 bsp_lapicid = lapicid(); |
Kyösti Mälkki | ea2fb8d | 2021-06-03 23:12:09 +0300 | [diff] [blame^] | 67 | u32 low, high; |
| 68 | |
| 69 | ASSERT(bsp_lapicid < 255); |
| 70 | |
| 71 | printk(BIOS_DEBUG, "IOAPIC: Bootstrap Processor Local APIC = 0x%02x\n", |
| 72 | bsp_lapicid); |
| 73 | |
| 74 | /* Enable Virtual Wire Mode. Should this be LOGICAL_DEST instead? */ |
| 75 | low = INT_ENABLED | TRIGGER_EDGE | POLARITY_HIGH | PHYSICAL_DEST | ExtINT; |
| 76 | high = bsp_lapicid << (56 - 32); |
| 77 | |
| 78 | io_apic_write(ioapic_base, 0x10, low); |
| 79 | io_apic_write(ioapic_base, 0x11, high); |
| 80 | |
| 81 | if (io_apic_read(ioapic_base, 0x10) == 0xffffffff) { |
| 82 | printk(BIOS_WARNING, "IOAPIC not responding.\n"); |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | printk(BIOS_SPEW, "IOAPIC: reg 0x%08x value 0x%08x 0x%08x\n", 0, high, low); |
| 87 | } |
| 88 | |
| 89 | void set_ioapic_id(void *ioapic_base, u8 ioapic_id) |
| 90 | { |
Paul Menzel | 1b3e176 | 2013-04-23 14:49:41 +0200 | [diff] [blame] | 91 | int i; |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 92 | |
Julius Werner | 540a980 | 2019-12-09 13:03:29 -0800 | [diff] [blame] | 93 | printk(BIOS_DEBUG, "IOAPIC: Initializing IOAPIC at %p\n", |
Uwe Hermann | e4990365 | 2010-10-14 23:40:10 +0000 | [diff] [blame] | 94 | ioapic_base); |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 95 | |
| 96 | if (ioapic_id) { |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 97 | printk(BIOS_DEBUG, "IOAPIC: ID = 0x%02x\n", ioapic_id); |
Uwe Hermann | e4990365 | 2010-10-14 23:40:10 +0000 | [diff] [blame] | 98 | /* Set IOAPIC ID if it has been specified. */ |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 99 | io_apic_write(ioapic_base, 0x00, |
Kyösti Mälkki | 939103c | 2011-10-19 07:23:51 +0300 | [diff] [blame] | 100 | (io_apic_read(ioapic_base, 0x00) & 0xf0ffffff) | |
Uwe Hermann | e4990365 | 2010-10-14 23:40:10 +0000 | [diff] [blame] | 101 | (ioapic_id << 24)); |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 102 | } |
Paul Menzel | 1b3e176 | 2013-04-23 14:49:41 +0200 | [diff] [blame] | 103 | |
| 104 | printk(BIOS_SPEW, "IOAPIC: Dumping registers\n"); |
| 105 | for (i = 0; i < 3; i++) |
| 106 | printk(BIOS_SPEW, " reg 0x%04x: 0x%08x\n", i, |
| 107 | io_apic_read(ioapic_base, i)); |
| 108 | |
Kyösti Mälkki | db4f875 | 2012-01-31 17:24:12 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 111 | static void load_vectors(void *ioapic_base) |
Kyösti Mälkki | db4f875 | 2012-01-31 17:24:12 +0200 | [diff] [blame] | 112 | { |
Kyösti Mälkki | ea2fb8d | 2021-06-03 23:12:09 +0300 | [diff] [blame^] | 113 | int first = 1, last; |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 114 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 115 | if (CONFIG(IOAPIC_INTERRUPTS_ON_FSB)) { |
Martin Roth | 898a775 | 2017-06-01 11:39:59 -0600 | [diff] [blame] | 116 | /* |
| 117 | * For the Pentium 4 and above APICs deliver their interrupts |
| 118 | * on the front side bus, enable that. |
| 119 | */ |
| 120 | printk(BIOS_DEBUG, "IOAPIC: Enabling interrupts on FSB\n"); |
| 121 | io_apic_write(ioapic_base, 0x03, |
| 122 | io_apic_read(ioapic_base, 0x03) | (1 << 0)); |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 123 | } else if (CONFIG(IOAPIC_INTERRUPTS_ON_APIC_SERIAL_BUS)) { |
Martin Roth | 898a775 | 2017-06-01 11:39:59 -0600 | [diff] [blame] | 124 | printk(BIOS_DEBUG, |
| 125 | "IOAPIC: Enabling interrupts on APIC serial bus\n"); |
| 126 | io_apic_write(ioapic_base, 0x03, 0); |
| 127 | } |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 128 | |
Kyösti Mälkki | ea2fb8d | 2021-06-03 23:12:09 +0300 | [diff] [blame^] | 129 | route_i8259_irq0(ioapic_base); |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 130 | |
Kyösti Mälkki | ea2fb8d | 2021-06-03 23:12:09 +0300 | [diff] [blame^] | 131 | last = ioapic_interrupt_count(ioapic_base) - 1; |
| 132 | clear_vectors(ioapic_base, first, last); |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 133 | } |
Kyösti Mälkki | db4f875 | 2012-01-31 17:24:12 +0200 | [diff] [blame] | 134 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 135 | void setup_ioapic(void *ioapic_base, u8 ioapic_id) |
Kyösti Mälkki | db4f875 | 2012-01-31 17:24:12 +0200 | [diff] [blame] | 136 | { |
| 137 | set_ioapic_id(ioapic_base, ioapic_id); |
| 138 | load_vectors(ioapic_base); |
| 139 | } |