blob: 1440bb43313683f8c1d9f498bf8b145969f3870b [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
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -08009u32 io_apic_read(void *ioapic_base, u32 reg)
Eric Biederman5899fd82003-04-24 06:25:08 +000010{
Stefan Reinauer0401bd82010-01-16 18:31:34 +000011 write32(ioapic_base, reg);
12 return read32(ioapic_base + 0x10);
13}
Eric Biederman5899fd82003-04-24 06:25:08 +000014
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080015void io_apic_write(void *ioapic_base, u32 reg, u32 value)
Stefan Reinauer0401bd82010-01-16 18:31:34 +000016{
17 write32(ioapic_base, reg);
18 write32(ioapic_base + 0x10, value);
19}
20
Kyösti Mälkki93c1eef2021-06-03 23:14:05 +030021static 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älkki04a40372021-06-06 08:04:28 +030030/* Bits 23-16 of register 0x01 specify the maximum redirection entry, which
31 * is the number of interrupts minus 1. */
32unsigned int ioapic_get_max_vectors(void *ioapic_base)
Patrick Georgid1de45e2013-01-23 13:45:23 +010033{
Kyösti Mälkki04a40372021-06-06 08:04:28 +030034 u32 reg;
35 u8 count;
Patrick Georgid1de45e2013-01-23 13:45:23 +010036
Kyösti Mälkki04a40372021-06-06 08:04:28 +030037 reg = io_apic_read(ioapic_base, 0x01);
Kyösti Mälkki6139ff92021-10-18 19:43:49 +030038 count = (reg >> 16) & 0xff;
Kyösti Mälkki04a40372021-06-06 08:04:28 +030039
Kyösti Mälkki6139ff92021-10-18 19:43:49 +030040 if (count == 0xff)
Kyösti Mälkki04a40372021-06-06 08:04:28 +030041 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. */
51void 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älkki6139ff92021-10-18 19:43:49 +030057 count = (reg >> 16) & 0xff;
Kyösti Mälkki04a40372021-06-06 08:04:28 +030058 if (mre_count > 0)
59 count = mre_count - 1;
60 reg &= ~(0xff << 16);
61 reg |= count << 16;
Iru Cai308a5b92021-10-23 21:44:02 +080062 io_apic_write(ioapic_base, 0x01, reg);
Kyösti Mälkki04a40372021-06-06 08:04:28 +030063}
64
65void ioapic_lock_max_vectors(void *ioapic_base)
66{
67 ioapic_set_max_vectors(ioapic_base, -1);
Patrick Georgid1de45e2013-01-23 13:45:23 +010068}
69
Kyösti Mälkkiea2fb8d2021-06-03 23:12:09 +030070static void clear_vectors(void *ioapic_base, u8 first, u8 last)
Stefan Reinauer0401bd82010-01-16 18:31:34 +000071{
72 u32 low, high;
Kyösti Mälkkiea2fb8d2021-06-03 23:12:09 +030073 u8 i;
Stefan Reinauer0401bd82010-01-16 18:31:34 +000074
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080075 printk(BIOS_DEBUG, "IOAPIC: Clearing IOAPIC at %p\n", ioapic_base);
Stefan Reinauer0401bd82010-01-16 18:31:34 +000076
Marc Jonesf7dc9722018-03-31 22:45:35 -060077 low = INT_DISABLED;
Stefan Reinauer0401bd82010-01-16 18:31:34 +000078 high = NONE;
79
Kyösti Mälkki93c1eef2021-06-03 23:14:05 +030080 for (i = first; i <= last; i++)
81 write_vector(ioapic_base, i, high, low);
Stefan Reinauer0401bd82010-01-16 18:31:34 +000082
83 if (io_apic_read(ioapic_base, 0x10) == 0xffffffff) {
Uwe Hermanne49903652010-10-14 23:40:10 +000084 printk(BIOS_WARNING, "IOAPIC not responding.\n");
Stefan Reinauer0401bd82010-01-16 18:31:34 +000085 return;
86 }
87}
88
Kyösti Mälkkiea2fb8d2021-06-03 23:12:09 +030089static void route_i8259_irq0(void *ioapic_base)
Stefan Reinauer0401bd82010-01-16 18:31:34 +000090{
91 u32 bsp_lapicid = lapicid();
Kyösti Mälkkiea2fb8d2021-06-03 23:12:09 +030092 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älkki93c1eef2021-06-03 23:14:05 +0300103 write_vector(ioapic_base, 0, high, low);
Kyösti Mälkkiea2fb8d2021-06-03 23:12:09 +0300104
105 if (io_apic_read(ioapic_base, 0x10) == 0xffffffff) {
106 printk(BIOS_WARNING, "IOAPIC not responding.\n");
107 return;
108 }
Kyösti Mälkkiea2fb8d2021-06-03 23:12:09 +0300109}
110
111void set_ioapic_id(void *ioapic_base, u8 ioapic_id)
112{
Paul Menzel1b3e1762013-04-23 14:49:41 +0200113 int i;
Stefan Reinauer0401bd82010-01-16 18:31:34 +0000114
Julius Werner540a9802019-12-09 13:03:29 -0800115 printk(BIOS_DEBUG, "IOAPIC: Initializing IOAPIC at %p\n",
Uwe Hermanne49903652010-10-14 23:40:10 +0000116 ioapic_base);
Stefan Reinauer0401bd82010-01-16 18:31:34 +0000117
118 if (ioapic_id) {
Stefan Reinauer14e22772010-04-27 06:56:47 +0000119 printk(BIOS_DEBUG, "IOAPIC: ID = 0x%02x\n", ioapic_id);
Uwe Hermanne49903652010-10-14 23:40:10 +0000120 /* Set IOAPIC ID if it has been specified. */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000121 io_apic_write(ioapic_base, 0x00,
Kyösti Mälkki939103c2011-10-19 07:23:51 +0300122 (io_apic_read(ioapic_base, 0x00) & 0xf0ffffff) |
Uwe Hermanne49903652010-10-14 23:40:10 +0000123 (ioapic_id << 24));
Stefan Reinauer0401bd82010-01-16 18:31:34 +0000124 }
Paul Menzel1b3e1762013-04-23 14:49:41 +0200125
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älkkidb4f8752012-01-31 17:24:12 +0200131}
132
Kyösti Mälkki401ec982021-06-06 08:27:15 +0300133u8 get_ioapic_id(void *ioapic_base)
134{
135 return (io_apic_read(ioapic_base, 0x00) >> 24) & 0x0f;
136}
137
138u8 get_ioapic_version(void *ioapic_base)
139{
140 return io_apic_read(ioapic_base, 0x01) & 0xff;
141}
142
Kyösti Mälkki8c9a89d2021-06-06 08:14:57 +0300143void ioapic_set_boot_config(void *ioapic_base, bool irq_on_fsb)
Kyösti Mälkkidb4f8752012-01-31 17:24:12 +0200144{
Kyösti Mälkki8cc25d22021-06-03 18:36:05 +0300145 if (irq_on_fsb) {
Martin Roth898a7752017-06-01 11:39:59 -0600146 /*
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älkki8cc25d22021-06-03 18:36:05 +0300153 } else {
Martin Roth898a7752017-06-01 11:39:59 -0600154 printk(BIOS_DEBUG,
155 "IOAPIC: Enabling interrupts on APIC serial bus\n");
156 io_apic_write(ioapic_base, 0x03, 0);
157 }
Kyösti Mälkki8c9a89d2021-06-06 08:14:57 +0300158}
159
Kyösti Mälkki6644d7c2021-06-06 08:10:05 +0300160void setup_ioapic(void *ioapic_base, u8 ioapic_id)
Kyösti Mälkki8c9a89d2021-06-06 08:14:57 +0300161{
Kyösti Mälkki8c9a89d2021-06-06 08:14:57 +0300162 set_ioapic_id(ioapic_base, ioapic_id);
Kyösti Mälkki04a40372021-06-06 08:04:28 +0300163 clear_vectors(ioapic_base, 0, ioapic_get_max_vectors(ioapic_base) - 1);
Kyösti Mälkki6644d7c2021-06-06 08:10:05 +0300164 route_i8259_irq0(ioapic_base);
Kyösti Mälkkidb4f8752012-01-31 17:24:12 +0200165}
Kyösti Mälkki0ea8f892021-06-08 11:28:25 +0300166
167void register_new_ioapic_gsi0(void *ioapic_base)
168{
169 setup_ioapic(ioapic_base, 0);
170}
171
172void 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}