blob: 5bb2adb584f67d03a819742f546e3b59527f8f9a [file] [log] [blame]
Patrick Georgi11f00792020-03-04 15:10:45 +01001/* SPDX-License-Identifier: GPL-2.0-only */
Eric Biederman5899fd82003-04-24 06:25:08 +00002
Kyösti Mälkkiea2fb8d2021-06-03 23:12:09 +03003#include <assert.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +02004#include <device/mmio.h>
Stefan Reinauer0401bd82010-01-16 18:31:34 +00005#include <arch/ioapic.h>
6#include <console/console.h>
7#include <cpu/x86/lapic.h>
Felix Held0d192892024-02-06 16:55:29 +01008#include <inttypes.h>
9#include <types.h>
Stefan Reinauer0401bd82010-01-16 18:31:34 +000010
Arthur Heymansf1e78a12022-12-06 18:23:30 +010011#define ALL (0xff << 24)
12#define NONE (0)
13#define INT_DISABLED (1 << 16)
14#define INT_ENABLED (0 << 16)
15#define TRIGGER_EDGE (0 << 15)
16#define TRIGGER_LEVEL (1 << 15)
17#define POLARITY_HIGH (0 << 13)
18#define POLARITY_LOW (1 << 13)
19#define PHYSICAL_DEST (0 << 11)
20#define LOGICAL_DEST (1 << 11)
21#define ExtINT (7 << 8)
22#define NMI (4 << 8)
23#define SMI (2 << 8)
24#define INT (1 << 8)
25
Felix Held0d192892024-02-06 16:55:29 +010026static u32 io_apic_read(uintptr_t ioapic_base, u32 reg)
Eric Biederman5899fd82003-04-24 06:25:08 +000027{
Felix Held0d192892024-02-06 16:55:29 +010028 write32p(ioapic_base, reg);
29 return read32p(ioapic_base + 0x10);
Stefan Reinauer0401bd82010-01-16 18:31:34 +000030}
Eric Biederman5899fd82003-04-24 06:25:08 +000031
Felix Held0d192892024-02-06 16:55:29 +010032static void io_apic_write(uintptr_t ioapic_base, u32 reg, u32 value)
Stefan Reinauer0401bd82010-01-16 18:31:34 +000033{
Felix Held0d192892024-02-06 16:55:29 +010034 write32p(ioapic_base, reg);
35 write32p(ioapic_base + 0x10, value);
Stefan Reinauer0401bd82010-01-16 18:31:34 +000036}
37
Felix Held0d192892024-02-06 16:55:29 +010038static void write_vector(uintptr_t ioapic_base, u8 vector, u32 high, u32 low)
Kyösti Mälkki93c1eef2021-06-03 23:14:05 +030039{
40 io_apic_write(ioapic_base, vector * 2 + 0x10, low);
41 io_apic_write(ioapic_base, vector * 2 + 0x11, high);
42
43 printk(BIOS_SPEW, "IOAPIC: vector 0x%02x value 0x%08x 0x%08x\n",
44 vector, high, low);
45}
46
Kyösti Mälkki04a40372021-06-06 08:04:28 +030047/* Bits 23-16 of register 0x01 specify the maximum redirection entry, which
48 * is the number of interrupts minus 1. */
Felix Held0d192892024-02-06 16:55:29 +010049unsigned int ioapic_get_max_vectors(uintptr_t ioapic_base)
Patrick Georgid1de45e2013-01-23 13:45:23 +010050{
Kyösti Mälkki04a40372021-06-06 08:04:28 +030051 u32 reg;
52 u8 count;
Patrick Georgid1de45e2013-01-23 13:45:23 +010053
Kyösti Mälkki04a40372021-06-06 08:04:28 +030054 reg = io_apic_read(ioapic_base, 0x01);
Kyösti Mälkki6139ff92021-10-18 19:43:49 +030055 count = (reg >> 16) & 0xff;
Kyösti Mälkki04a40372021-06-06 08:04:28 +030056
Kyösti Mälkki6139ff92021-10-18 19:43:49 +030057 if (count == 0xff)
Kyösti Mälkki04a40372021-06-06 08:04:28 +030058 count = 23;
59 count++;
60
61 printk(BIOS_DEBUG, "IOAPIC: %d interrupts\n", count);
62 return count;
63}
64
65/* Set maximum number of redirection entries (MRE). It is write-once register
66 * for some chipsets, and a negative mre_count will lock it to the number
67 * of vectors read from the register. */
Felix Held0d192892024-02-06 16:55:29 +010068void ioapic_set_max_vectors(uintptr_t ioapic_base, int mre_count)
Kyösti Mälkki04a40372021-06-06 08:04:28 +030069{
70 u32 reg;
71 u8 count;
72
73 reg = io_apic_read(ioapic_base, 0x01);
Kyösti Mälkki6139ff92021-10-18 19:43:49 +030074 count = (reg >> 16) & 0xff;
Kyösti Mälkki04a40372021-06-06 08:04:28 +030075 if (mre_count > 0)
76 count = mre_count - 1;
77 reg &= ~(0xff << 16);
78 reg |= count << 16;
Iru Cai308a5b92021-10-23 21:44:02 +080079 io_apic_write(ioapic_base, 0x01, reg);
Kyösti Mälkki04a40372021-06-06 08:04:28 +030080}
81
Felix Held0d192892024-02-06 16:55:29 +010082void ioapic_lock_max_vectors(uintptr_t ioapic_base)
Kyösti Mälkki04a40372021-06-06 08:04:28 +030083{
84 ioapic_set_max_vectors(ioapic_base, -1);
Patrick Georgid1de45e2013-01-23 13:45:23 +010085}
86
Felix Held0d192892024-02-06 16:55:29 +010087static void clear_vectors(uintptr_t ioapic_base, u8 first, u8 last)
Stefan Reinauer0401bd82010-01-16 18:31:34 +000088{
89 u32 low, high;
Kyösti Mälkkiea2fb8d2021-06-03 23:12:09 +030090 u8 i;
Stefan Reinauer0401bd82010-01-16 18:31:34 +000091
Felix Held0d192892024-02-06 16:55:29 +010092 printk(BIOS_DEBUG, "IOAPIC: Clearing IOAPIC at %" PRIxPTR "\n", ioapic_base);
Stefan Reinauer0401bd82010-01-16 18:31:34 +000093
Marc Jonesf7dc9722018-03-31 22:45:35 -060094 low = INT_DISABLED;
Stefan Reinauer0401bd82010-01-16 18:31:34 +000095 high = NONE;
96
Kyösti Mälkki93c1eef2021-06-03 23:14:05 +030097 for (i = first; i <= last; i++)
98 write_vector(ioapic_base, i, high, low);
Stefan Reinauer0401bd82010-01-16 18:31:34 +000099
100 if (io_apic_read(ioapic_base, 0x10) == 0xffffffff) {
Uwe Hermanne49903652010-10-14 23:40:10 +0000101 printk(BIOS_WARNING, "IOAPIC not responding.\n");
Stefan Reinauer0401bd82010-01-16 18:31:34 +0000102 return;
103 }
104}
105
Felix Held0d192892024-02-06 16:55:29 +0100106static void route_i8259_irq0(uintptr_t ioapic_base)
Stefan Reinauer0401bd82010-01-16 18:31:34 +0000107{
108 u32 bsp_lapicid = lapicid();
Kyösti Mälkkiea2fb8d2021-06-03 23:12:09 +0300109 u32 low, high;
110
111 ASSERT(bsp_lapicid < 255);
112
113 printk(BIOS_DEBUG, "IOAPIC: Bootstrap Processor Local APIC = 0x%02x\n",
114 bsp_lapicid);
115
116 /* Enable Virtual Wire Mode. Should this be LOGICAL_DEST instead? */
117 low = INT_ENABLED | TRIGGER_EDGE | POLARITY_HIGH | PHYSICAL_DEST | ExtINT;
118 high = bsp_lapicid << (56 - 32);
119
Kyösti Mälkki93c1eef2021-06-03 23:14:05 +0300120 write_vector(ioapic_base, 0, high, low);
Kyösti Mälkkiea2fb8d2021-06-03 23:12:09 +0300121
122 if (io_apic_read(ioapic_base, 0x10) == 0xffffffff) {
123 printk(BIOS_WARNING, "IOAPIC not responding.\n");
124 return;
125 }
Kyösti Mälkkiea2fb8d2021-06-03 23:12:09 +0300126}
127
Felix Held0d192892024-02-06 16:55:29 +0100128static void set_ioapic_id(uintptr_t ioapic_base, u8 ioapic_id)
Kyösti Mälkkiea2fb8d2021-06-03 23:12:09 +0300129{
Paul Menzel1b3e1762013-04-23 14:49:41 +0200130 int i;
Stefan Reinauer0401bd82010-01-16 18:31:34 +0000131
Felix Held0d192892024-02-06 16:55:29 +0100132 printk(BIOS_DEBUG, "IOAPIC: Initializing IOAPIC at %" PRIxPTR "\n",
Uwe Hermanne49903652010-10-14 23:40:10 +0000133 ioapic_base);
Jay Patelc7728522023-03-13 08:36:28 -0700134 printk(BIOS_DEBUG, "IOAPIC: ID = 0x%02x\n", ioapic_id);
Stefan Reinauer0401bd82010-01-16 18:31:34 +0000135
Felix Heldb5d244c2024-02-02 11:02:40 +0100136 io_apic_write(ioapic_base, 0x00,
137 (io_apic_read(ioapic_base, 0x00) & 0xf0ffffff) | (ioapic_id << 24));
Paul Menzel1b3e1762013-04-23 14:49:41 +0200138
139 printk(BIOS_SPEW, "IOAPIC: Dumping registers\n");
140 for (i = 0; i < 3; i++)
141 printk(BIOS_SPEW, " reg 0x%04x: 0x%08x\n", i,
142 io_apic_read(ioapic_base, i));
Kyösti Mälkkidb4f8752012-01-31 17:24:12 +0200143}
144
Felix Held0d192892024-02-06 16:55:29 +0100145u8 get_ioapic_id(uintptr_t ioapic_base)
Kyösti Mälkki401ec982021-06-06 08:27:15 +0300146{
Arthur Heymansd1c61a82022-12-08 14:25:11 +0100147 /*
148 * According to 82093AA I/O ADVANCED PROGRAMMABLE INTERRUPT CONTROLLER (IOAPIC)
149 * only 4 bits (24:27) are used for the ID. In practice the upper bits are either
150 * always 0 or used for larger IDs.
151 */
152 return (io_apic_read(ioapic_base, 0x00) >> 24) & 0xff;
Kyösti Mälkki401ec982021-06-06 08:27:15 +0300153}
154
Felix Held0d192892024-02-06 16:55:29 +0100155u8 get_ioapic_version(uintptr_t ioapic_base)
Kyösti Mälkki401ec982021-06-06 08:27:15 +0300156{
157 return io_apic_read(ioapic_base, 0x01) & 0xff;
158}
159
Felix Held0d192892024-02-06 16:55:29 +0100160void ioapic_set_boot_config(uintptr_t ioapic_base, bool irq_on_fsb)
Kyösti Mälkkidb4f8752012-01-31 17:24:12 +0200161{
Kyösti Mälkki8cc25d22021-06-03 18:36:05 +0300162 if (irq_on_fsb) {
Martin Roth898a7752017-06-01 11:39:59 -0600163 /*
164 * For the Pentium 4 and above APICs deliver their interrupts
165 * on the front side bus, enable that.
166 */
167 printk(BIOS_DEBUG, "IOAPIC: Enabling interrupts on FSB\n");
168 io_apic_write(ioapic_base, 0x03,
169 io_apic_read(ioapic_base, 0x03) | (1 << 0));
Kyösti Mälkki8cc25d22021-06-03 18:36:05 +0300170 } else {
Martin Roth898a7752017-06-01 11:39:59 -0600171 printk(BIOS_DEBUG,
172 "IOAPIC: Enabling interrupts on APIC serial bus\n");
173 io_apic_write(ioapic_base, 0x03, 0);
174 }
Kyösti Mälkki8c9a89d2021-06-06 08:14:57 +0300175}
176
Felix Held0d192892024-02-06 16:55:29 +0100177void setup_ioapic(uintptr_t ioapic_base, u8 ioapic_id)
Kyösti Mälkki8c9a89d2021-06-06 08:14:57 +0300178{
Kyösti Mälkki8c9a89d2021-06-06 08:14:57 +0300179 set_ioapic_id(ioapic_base, ioapic_id);
Kyösti Mälkki04a40372021-06-06 08:04:28 +0300180 clear_vectors(ioapic_base, 0, ioapic_get_max_vectors(ioapic_base) - 1);
Kyösti Mälkki6644d7c2021-06-06 08:10:05 +0300181 route_i8259_irq0(ioapic_base);
Kyösti Mälkkidb4f8752012-01-31 17:24:12 +0200182}
Kyösti Mälkki0ea8f892021-06-08 11:28:25 +0300183
Felix Held0d192892024-02-06 16:55:29 +0100184void register_new_ioapic_gsi0(uintptr_t ioapic_base)
Kyösti Mälkki0ea8f892021-06-08 11:28:25 +0300185{
186 setup_ioapic(ioapic_base, 0);
187}
188
Felix Held0d192892024-02-06 16:55:29 +0100189void register_new_ioapic(uintptr_t ioapic_base)
Kyösti Mälkki0ea8f892021-06-08 11:28:25 +0300190{
191 static u8 ioapic_id;
192 ioapic_id++;
193 set_ioapic_id(ioapic_base, ioapic_id);
194 clear_vectors(ioapic_base, 0, ioapic_get_max_vectors(ioapic_base) - 1);
195}