blob: df97a50cf5f7a47c3fdc0669cdf2b73c14cedd7c [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>
8
Arthur Heymansf1e78a12022-12-06 18:23:30 +01009#define ALL (0xff << 24)
10#define NONE (0)
11#define INT_DISABLED (1 << 16)
12#define INT_ENABLED (0 << 16)
13#define TRIGGER_EDGE (0 << 15)
14#define TRIGGER_LEVEL (1 << 15)
15#define POLARITY_HIGH (0 << 13)
16#define POLARITY_LOW (1 << 13)
17#define PHYSICAL_DEST (0 << 11)
18#define LOGICAL_DEST (1 << 11)
19#define ExtINT (7 << 8)
20#define NMI (4 << 8)
21#define SMI (2 << 8)
22#define INT (1 << 8)
23
Kyösti Mälkki1d3c2e62021-06-08 06:23:39 +030024static u32 io_apic_read(void *ioapic_base, u32 reg)
Eric Biederman5899fd82003-04-24 06:25:08 +000025{
Stefan Reinauer0401bd82010-01-16 18:31:34 +000026 write32(ioapic_base, reg);
27 return read32(ioapic_base + 0x10);
28}
Eric Biederman5899fd82003-04-24 06:25:08 +000029
Kyösti Mälkki1d3c2e62021-06-08 06:23:39 +030030static void io_apic_write(void *ioapic_base, u32 reg, u32 value)
Stefan Reinauer0401bd82010-01-16 18:31:34 +000031{
32 write32(ioapic_base, reg);
33 write32(ioapic_base + 0x10, value);
34}
35
Kyösti Mälkki93c1eef2021-06-03 23:14:05 +030036static void write_vector(void *ioapic_base, u8 vector, u32 high, u32 low)
37{
38 io_apic_write(ioapic_base, vector * 2 + 0x10, low);
39 io_apic_write(ioapic_base, vector * 2 + 0x11, high);
40
41 printk(BIOS_SPEW, "IOAPIC: vector 0x%02x value 0x%08x 0x%08x\n",
42 vector, high, low);
43}
44
Kyösti Mälkki04a40372021-06-06 08:04:28 +030045/* Bits 23-16 of register 0x01 specify the maximum redirection entry, which
46 * is the number of interrupts minus 1. */
47unsigned int ioapic_get_max_vectors(void *ioapic_base)
Patrick Georgid1de45e2013-01-23 13:45:23 +010048{
Kyösti Mälkki04a40372021-06-06 08:04:28 +030049 u32 reg;
50 u8 count;
Patrick Georgid1de45e2013-01-23 13:45:23 +010051
Kyösti Mälkki04a40372021-06-06 08:04:28 +030052 reg = io_apic_read(ioapic_base, 0x01);
Kyösti Mälkki6139ff92021-10-18 19:43:49 +030053 count = (reg >> 16) & 0xff;
Kyösti Mälkki04a40372021-06-06 08:04:28 +030054
Kyösti Mälkki6139ff92021-10-18 19:43:49 +030055 if (count == 0xff)
Kyösti Mälkki04a40372021-06-06 08:04:28 +030056 count = 23;
57 count++;
58
59 printk(BIOS_DEBUG, "IOAPIC: %d interrupts\n", count);
60 return count;
61}
62
63/* Set maximum number of redirection entries (MRE). It is write-once register
64 * for some chipsets, and a negative mre_count will lock it to the number
65 * of vectors read from the register. */
66void ioapic_set_max_vectors(void *ioapic_base, int mre_count)
67{
68 u32 reg;
69 u8 count;
70
71 reg = io_apic_read(ioapic_base, 0x01);
Kyösti Mälkki6139ff92021-10-18 19:43:49 +030072 count = (reg >> 16) & 0xff;
Kyösti Mälkki04a40372021-06-06 08:04:28 +030073 if (mre_count > 0)
74 count = mre_count - 1;
75 reg &= ~(0xff << 16);
76 reg |= count << 16;
Iru Cai308a5b92021-10-23 21:44:02 +080077 io_apic_write(ioapic_base, 0x01, reg);
Kyösti Mälkki04a40372021-06-06 08:04:28 +030078}
79
80void ioapic_lock_max_vectors(void *ioapic_base)
81{
82 ioapic_set_max_vectors(ioapic_base, -1);
Patrick Georgid1de45e2013-01-23 13:45:23 +010083}
84
Kyösti Mälkkiea2fb8d2021-06-03 23:12:09 +030085static void clear_vectors(void *ioapic_base, u8 first, u8 last)
Stefan Reinauer0401bd82010-01-16 18:31:34 +000086{
87 u32 low, high;
Kyösti Mälkkiea2fb8d2021-06-03 23:12:09 +030088 u8 i;
Stefan Reinauer0401bd82010-01-16 18:31:34 +000089
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080090 printk(BIOS_DEBUG, "IOAPIC: Clearing IOAPIC at %p\n", ioapic_base);
Stefan Reinauer0401bd82010-01-16 18:31:34 +000091
Marc Jonesf7dc9722018-03-31 22:45:35 -060092 low = INT_DISABLED;
Stefan Reinauer0401bd82010-01-16 18:31:34 +000093 high = NONE;
94
Kyösti Mälkki93c1eef2021-06-03 23:14:05 +030095 for (i = first; i <= last; i++)
96 write_vector(ioapic_base, i, high, low);
Stefan Reinauer0401bd82010-01-16 18:31:34 +000097
98 if (io_apic_read(ioapic_base, 0x10) == 0xffffffff) {
Uwe Hermanne49903652010-10-14 23:40:10 +000099 printk(BIOS_WARNING, "IOAPIC not responding.\n");
Stefan Reinauer0401bd82010-01-16 18:31:34 +0000100 return;
101 }
102}
103
Kyösti Mälkkiea2fb8d2021-06-03 23:12:09 +0300104static void route_i8259_irq0(void *ioapic_base)
Stefan Reinauer0401bd82010-01-16 18:31:34 +0000105{
106 u32 bsp_lapicid = lapicid();
Kyösti Mälkkiea2fb8d2021-06-03 23:12:09 +0300107 u32 low, high;
108
109 ASSERT(bsp_lapicid < 255);
110
111 printk(BIOS_DEBUG, "IOAPIC: Bootstrap Processor Local APIC = 0x%02x\n",
112 bsp_lapicid);
113
114 /* Enable Virtual Wire Mode. Should this be LOGICAL_DEST instead? */
115 low = INT_ENABLED | TRIGGER_EDGE | POLARITY_HIGH | PHYSICAL_DEST | ExtINT;
116 high = bsp_lapicid << (56 - 32);
117
Kyösti Mälkki93c1eef2021-06-03 23:14:05 +0300118 write_vector(ioapic_base, 0, high, low);
Kyösti Mälkkiea2fb8d2021-06-03 23:12:09 +0300119
120 if (io_apic_read(ioapic_base, 0x10) == 0xffffffff) {
121 printk(BIOS_WARNING, "IOAPIC not responding.\n");
122 return;
123 }
Kyösti Mälkkiea2fb8d2021-06-03 23:12:09 +0300124}
125
Kyösti Mälkki1d3c2e62021-06-08 06:23:39 +0300126static void set_ioapic_id(void *ioapic_base, u8 ioapic_id)
Kyösti Mälkkiea2fb8d2021-06-03 23:12:09 +0300127{
Paul Menzel1b3e1762013-04-23 14:49:41 +0200128 int i;
Stefan Reinauer0401bd82010-01-16 18:31:34 +0000129
Julius Werner540a9802019-12-09 13:03:29 -0800130 printk(BIOS_DEBUG, "IOAPIC: Initializing IOAPIC at %p\n",
Uwe Hermanne49903652010-10-14 23:40:10 +0000131 ioapic_base);
Jay Patelc7728522023-03-13 08:36:28 -0700132 printk(BIOS_DEBUG, "IOAPIC: ID = 0x%02x\n", ioapic_id);
Stefan Reinauer0401bd82010-01-16 18:31:34 +0000133
Felix Heldb5d244c2024-02-02 11:02:40 +0100134 io_apic_write(ioapic_base, 0x00,
135 (io_apic_read(ioapic_base, 0x00) & 0xf0ffffff) | (ioapic_id << 24));
Paul Menzel1b3e1762013-04-23 14:49:41 +0200136
137 printk(BIOS_SPEW, "IOAPIC: Dumping registers\n");
138 for (i = 0; i < 3; i++)
139 printk(BIOS_SPEW, " reg 0x%04x: 0x%08x\n", i,
140 io_apic_read(ioapic_base, i));
141
Kyösti Mälkkidb4f8752012-01-31 17:24:12 +0200142}
143
Kyösti Mälkki401ec982021-06-06 08:27:15 +0300144u8 get_ioapic_id(void *ioapic_base)
145{
Arthur Heymansd1c61a82022-12-08 14:25:11 +0100146 /*
147 * According to 82093AA I/O ADVANCED PROGRAMMABLE INTERRUPT CONTROLLER (IOAPIC)
148 * only 4 bits (24:27) are used for the ID. In practice the upper bits are either
149 * always 0 or used for larger IDs.
150 */
151 return (io_apic_read(ioapic_base, 0x00) >> 24) & 0xff;
Kyösti Mälkki401ec982021-06-06 08:27:15 +0300152}
153
154u8 get_ioapic_version(void *ioapic_base)
155{
156 return io_apic_read(ioapic_base, 0x01) & 0xff;
157}
158
Kyösti Mälkki8c9a89d2021-06-06 08:14:57 +0300159void ioapic_set_boot_config(void *ioapic_base, bool irq_on_fsb)
Kyösti Mälkkidb4f8752012-01-31 17:24:12 +0200160{
Kyösti Mälkki8cc25d22021-06-03 18:36:05 +0300161 if (irq_on_fsb) {
Martin Roth898a7752017-06-01 11:39:59 -0600162 /*
163 * For the Pentium 4 and above APICs deliver their interrupts
164 * on the front side bus, enable that.
165 */
166 printk(BIOS_DEBUG, "IOAPIC: Enabling interrupts on FSB\n");
167 io_apic_write(ioapic_base, 0x03,
168 io_apic_read(ioapic_base, 0x03) | (1 << 0));
Kyösti Mälkki8cc25d22021-06-03 18:36:05 +0300169 } else {
Martin Roth898a7752017-06-01 11:39:59 -0600170 printk(BIOS_DEBUG,
171 "IOAPIC: Enabling interrupts on APIC serial bus\n");
172 io_apic_write(ioapic_base, 0x03, 0);
173 }
Kyösti Mälkki8c9a89d2021-06-06 08:14:57 +0300174}
175
Kyösti Mälkki6644d7c2021-06-06 08:10:05 +0300176void setup_ioapic(void *ioapic_base, u8 ioapic_id)
Kyösti Mälkki8c9a89d2021-06-06 08:14:57 +0300177{
Kyösti Mälkki8c9a89d2021-06-06 08:14:57 +0300178 set_ioapic_id(ioapic_base, ioapic_id);
Kyösti Mälkki04a40372021-06-06 08:04:28 +0300179 clear_vectors(ioapic_base, 0, ioapic_get_max_vectors(ioapic_base) - 1);
Kyösti Mälkki6644d7c2021-06-06 08:10:05 +0300180 route_i8259_irq0(ioapic_base);
Kyösti Mälkkidb4f8752012-01-31 17:24:12 +0200181}
Kyösti Mälkki0ea8f892021-06-08 11:28:25 +0300182
183void register_new_ioapic_gsi0(void *ioapic_base)
184{
185 setup_ioapic(ioapic_base, 0);
186}
187
188void register_new_ioapic(void *ioapic_base)
189{
190 static u8 ioapic_id;
191 ioapic_id++;
192 set_ioapic_id(ioapic_base, ioapic_id);
193 clear_vectors(ioapic_base, 0, ioapic_get_max_vectors(ioapic_base) - 1);
194}