blob: 5e3e2afb11d83952c9f0f51cdcf6994575f024f2 [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älkki13f66502019-03-03 08:01:05 +02003#include <device/mmio.h>
Stefan Reinauer0401bd82010-01-16 18:31:34 +00004#include <arch/ioapic.h>
5#include <console/console.h>
6#include <cpu/x86/lapic.h>
7
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -08008u32 io_apic_read(void *ioapic_base, u32 reg)
Eric Biederman5899fd82003-04-24 06:25:08 +00009{
Stefan Reinauer0401bd82010-01-16 18:31:34 +000010 write32(ioapic_base, reg);
11 return read32(ioapic_base + 0x10);
12}
Eric Biederman5899fd82003-04-24 06:25:08 +000013
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080014void io_apic_write(void *ioapic_base, u32 reg, u32 value)
Stefan Reinauer0401bd82010-01-16 18:31:34 +000015{
16 write32(ioapic_base, reg);
17 write32(ioapic_base + 0x10, value);
18}
19
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080020static int ioapic_interrupt_count(void *ioapic_base)
Patrick Georgid1de45e2013-01-23 13:45:23 +010021{
22 /* Read the available number of interrupts. */
23 int ioapic_interrupts = (io_apic_read(ioapic_base, 0x01) >> 16) & 0xff;
24 if (ioapic_interrupts == 0xff)
25 ioapic_interrupts = 23;
26 ioapic_interrupts += 1; /* Bits 23-16 specify the maximum redirection
27 entry, which is the number of interrupts
28 minus 1. */
29 printk(BIOS_DEBUG, "IOAPIC: %d interrupts\n", ioapic_interrupts);
30
31 return ioapic_interrupts;
32}
33
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080034void clear_ioapic(void *ioapic_base)
Stefan Reinauer0401bd82010-01-16 18:31:34 +000035{
36 u32 low, high;
37 u32 i, ioapic_interrupts;
38
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080039 printk(BIOS_DEBUG, "IOAPIC: Clearing IOAPIC at %p\n", ioapic_base);
Stefan Reinauer0401bd82010-01-16 18:31:34 +000040
Patrick Georgid1de45e2013-01-23 13:45:23 +010041 ioapic_interrupts = ioapic_interrupt_count(ioapic_base);
Stefan Reinauer0401bd82010-01-16 18:31:34 +000042
Marc Jonesf7dc9722018-03-31 22:45:35 -060043 low = INT_DISABLED;
Stefan Reinauer0401bd82010-01-16 18:31:34 +000044 high = NONE;
45
46 for (i = 0; i < ioapic_interrupts; i++) {
47 io_apic_write(ioapic_base, i * 2 + 0x10, low);
48 io_apic_write(ioapic_base, i * 2 + 0x11, high);
49
Uwe Hermanne49903652010-10-14 23:40:10 +000050 printk(BIOS_SPEW, "IOAPIC: reg 0x%08x value 0x%08x 0x%08x\n",
51 i, high, low);
Stefan Reinauer0401bd82010-01-16 18:31:34 +000052 }
53
54 if (io_apic_read(ioapic_base, 0x10) == 0xffffffff) {
Uwe Hermanne49903652010-10-14 23:40:10 +000055 printk(BIOS_WARNING, "IOAPIC not responding.\n");
Stefan Reinauer0401bd82010-01-16 18:31:34 +000056 return;
57 }
58}
59
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080060void set_ioapic_id(void *ioapic_base, u8 ioapic_id)
Stefan Reinauer0401bd82010-01-16 18:31:34 +000061{
62 u32 bsp_lapicid = lapicid();
Paul Menzel1b3e1762013-04-23 14:49:41 +020063 int i;
Stefan Reinauer0401bd82010-01-16 18:31:34 +000064
Julius Werner540a9802019-12-09 13:03:29 -080065 printk(BIOS_DEBUG, "IOAPIC: Initializing IOAPIC at %p\n",
Uwe Hermanne49903652010-10-14 23:40:10 +000066 ioapic_base);
67 printk(BIOS_DEBUG, "IOAPIC: Bootstrap Processor Local APIC = 0x%02x\n",
68 bsp_lapicid);
Stefan Reinauer0401bd82010-01-16 18:31:34 +000069
70 if (ioapic_id) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000071 printk(BIOS_DEBUG, "IOAPIC: ID = 0x%02x\n", ioapic_id);
Uwe Hermanne49903652010-10-14 23:40:10 +000072 /* Set IOAPIC ID if it has been specified. */
Stefan Reinauer14e22772010-04-27 06:56:47 +000073 io_apic_write(ioapic_base, 0x00,
Kyösti Mälkki939103c2011-10-19 07:23:51 +030074 (io_apic_read(ioapic_base, 0x00) & 0xf0ffffff) |
Uwe Hermanne49903652010-10-14 23:40:10 +000075 (ioapic_id << 24));
Stefan Reinauer0401bd82010-01-16 18:31:34 +000076 }
Paul Menzel1b3e1762013-04-23 14:49:41 +020077
78 printk(BIOS_SPEW, "IOAPIC: Dumping registers\n");
79 for (i = 0; i < 3; i++)
80 printk(BIOS_SPEW, " reg 0x%04x: 0x%08x\n", i,
81 io_apic_read(ioapic_base, i));
82
Kyösti Mälkkidb4f8752012-01-31 17:24:12 +020083}
84
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080085static void load_vectors(void *ioapic_base)
Kyösti Mälkkidb4f8752012-01-31 17:24:12 +020086{
87 u32 bsp_lapicid = lapicid();
88 u32 low, high;
89 u32 i, ioapic_interrupts;
Stefan Reinauer0401bd82010-01-16 18:31:34 +000090
Patrick Georgid1de45e2013-01-23 13:45:23 +010091 ioapic_interrupts = ioapic_interrupt_count(ioapic_base);
Stefan Reinauer0401bd82010-01-16 18:31:34 +000092
Julius Wernercd49cce2019-03-05 16:53:33 -080093 if (CONFIG(IOAPIC_INTERRUPTS_ON_FSB)) {
Martin Roth898a7752017-06-01 11:39:59 -060094 /*
95 * For the Pentium 4 and above APICs deliver their interrupts
96 * on the front side bus, enable that.
97 */
98 printk(BIOS_DEBUG, "IOAPIC: Enabling interrupts on FSB\n");
99 io_apic_write(ioapic_base, 0x03,
100 io_apic_read(ioapic_base, 0x03) | (1 << 0));
Julius Wernercd49cce2019-03-05 16:53:33 -0800101 } else if (CONFIG(IOAPIC_INTERRUPTS_ON_APIC_SERIAL_BUS)) {
Martin Roth898a7752017-06-01 11:39:59 -0600102 printk(BIOS_DEBUG,
103 "IOAPIC: Enabling interrupts on APIC serial bus\n");
104 io_apic_write(ioapic_base, 0x03, 0);
105 }
Stefan Reinauer0401bd82010-01-16 18:31:34 +0000106
Uwe Hermanne49903652010-10-14 23:40:10 +0000107 /* Enable Virtual Wire Mode. */
Marc Jonesf7dc9722018-03-31 22:45:35 -0600108 low = INT_ENABLED | TRIGGER_EDGE | POLARITY_HIGH | PHYSICAL_DEST | ExtINT;
Stefan Reinauer0401bd82010-01-16 18:31:34 +0000109 high = bsp_lapicid << (56 - 32);
110
111 io_apic_write(ioapic_base, 0x10, low);
112 io_apic_write(ioapic_base, 0x11, high);
113
114 if (io_apic_read(ioapic_base, 0x10) == 0xffffffff) {
Uwe Hermanne49903652010-10-14 23:40:10 +0000115 printk(BIOS_WARNING, "IOAPIC not responding.\n");
Stefan Reinauer0401bd82010-01-16 18:31:34 +0000116 return;
117 }
118
Uwe Hermanne49903652010-10-14 23:40:10 +0000119 printk(BIOS_SPEW, "IOAPIC: reg 0x%08x value 0x%08x 0x%08x\n",
120 0, high, low);
Marc Jonesf7dc9722018-03-31 22:45:35 -0600121 low = INT_DISABLED;
Stefan Reinauer0401bd82010-01-16 18:31:34 +0000122 high = NONE;
Stefan Reinauer0401bd82010-01-16 18:31:34 +0000123 for (i = 1; i < ioapic_interrupts; i++) {
124 io_apic_write(ioapic_base, i * 2 + 0x10, low);
125 io_apic_write(ioapic_base, i * 2 + 0x11, high);
126
Uwe Hermanne49903652010-10-14 23:40:10 +0000127 printk(BIOS_SPEW, "IOAPIC: reg 0x%08x value 0x%08x 0x%08x\n",
128 i, high, low);
Eric Biederman5899fd82003-04-24 06:25:08 +0000129 }
130}
Kyösti Mälkkidb4f8752012-01-31 17:24:12 +0200131
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800132void setup_ioapic(void *ioapic_base, u8 ioapic_id)
Kyösti Mälkkidb4f8752012-01-31 17:24:12 +0200133{
134 set_ioapic_id(ioapic_base, ioapic_id);
135 load_vectors(ioapic_base);
136}