2020-03-04 15:10:45 +01:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
2010-01-16 19:31:34 +01:00
|
|
|
|
2021-06-03 22:12:09 +02:00
|
|
|
#include <assert.h>
|
2019-03-03 07:01:05 +01:00
|
|
|
#include <device/mmio.h>
|
2003-04-24 08:25:08 +02:00
|
|
|
#include <arch/ioapic.h>
|
2010-01-16 19:31:34 +01:00
|
|
|
#include <console/console.h>
|
|
|
|
#include <cpu/x86/lapic.h>
|
2003-04-24 08:25:08 +02:00
|
|
|
|
2014-12-25 03:43:20 +01:00
|
|
|
u32 io_apic_read(void *ioapic_base, u32 reg)
|
2010-01-16 19:31:34 +01:00
|
|
|
{
|
|
|
|
write32(ioapic_base, reg);
|
|
|
|
return read32(ioapic_base + 0x10);
|
|
|
|
}
|
|
|
|
|
2014-12-25 03:43:20 +01:00
|
|
|
void io_apic_write(void *ioapic_base, u32 reg, u32 value)
|
2010-01-16 19:31:34 +01:00
|
|
|
{
|
|
|
|
write32(ioapic_base, reg);
|
|
|
|
write32(ioapic_base + 0x10, value);
|
|
|
|
}
|
|
|
|
|
2021-06-03 22:14:05 +02:00
|
|
|
static void write_vector(void *ioapic_base, u8 vector, u32 high, u32 low)
|
|
|
|
{
|
|
|
|
io_apic_write(ioapic_base, vector * 2 + 0x10, low);
|
|
|
|
io_apic_write(ioapic_base, vector * 2 + 0x11, high);
|
|
|
|
|
|
|
|
printk(BIOS_SPEW, "IOAPIC: vector 0x%02x value 0x%08x 0x%08x\n",
|
|
|
|
vector, high, low);
|
|
|
|
}
|
|
|
|
|
2014-12-25 03:43:20 +01:00
|
|
|
static int ioapic_interrupt_count(void *ioapic_base)
|
2010-01-16 19:31:34 +01:00
|
|
|
{
|
2010-10-15 01:40:10 +02:00
|
|
|
/* Read the available number of interrupts. */
|
2013-01-23 13:45:23 +01:00
|
|
|
int ioapic_interrupts = (io_apic_read(ioapic_base, 0x01) >> 16) & 0xff;
|
2021-06-03 22:12:09 +02:00
|
|
|
if (!ioapic_interrupts || ioapic_interrupts == 0xff)
|
2012-12-18 19:29:20 +01:00
|
|
|
ioapic_interrupts = 23;
|
|
|
|
ioapic_interrupts += 1; /* Bits 23-16 specify the maximum redirection
|
|
|
|
entry, which is the number of interrupts
|
|
|
|
minus 1. */
|
2010-04-27 08:56:47 +02:00
|
|
|
printk(BIOS_DEBUG, "IOAPIC: %d interrupts\n", ioapic_interrupts);
|
2010-01-16 19:31:34 +01:00
|
|
|
|
2013-01-23 13:45:23 +01:00
|
|
|
return ioapic_interrupts;
|
|
|
|
}
|
|
|
|
|
2021-06-03 22:12:09 +02:00
|
|
|
static void clear_vectors(void *ioapic_base, u8 first, u8 last)
|
2013-01-23 13:45:23 +01:00
|
|
|
{
|
|
|
|
u32 low, high;
|
2021-06-03 22:12:09 +02:00
|
|
|
u8 i;
|
2013-01-23 13:45:23 +01:00
|
|
|
|
2014-12-25 03:43:20 +01:00
|
|
|
printk(BIOS_DEBUG, "IOAPIC: Clearing IOAPIC at %p\n", ioapic_base);
|
2013-01-23 13:45:23 +01:00
|
|
|
|
2018-04-01 06:45:35 +02:00
|
|
|
low = INT_DISABLED;
|
2010-01-16 19:31:34 +01:00
|
|
|
high = NONE;
|
|
|
|
|
2021-06-03 22:14:05 +02:00
|
|
|
for (i = first; i <= last; i++)
|
|
|
|
write_vector(ioapic_base, i, high, low);
|
2010-01-16 19:31:34 +01:00
|
|
|
|
|
|
|
if (io_apic_read(ioapic_base, 0x10) == 0xffffffff) {
|
2010-10-15 01:40:10 +02:00
|
|
|
printk(BIOS_WARNING, "IOAPIC not responding.\n");
|
2010-01-16 19:31:34 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-03 22:12:09 +02:00
|
|
|
void clear_ioapic(void *ioapic_base)
|
|
|
|
{
|
|
|
|
clear_vectors(ioapic_base, 0, ioapic_interrupt_count(ioapic_base) - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void route_i8259_irq0(void *ioapic_base)
|
2003-04-24 08:25:08 +02:00
|
|
|
{
|
2010-01-16 19:31:34 +01:00
|
|
|
u32 bsp_lapicid = lapicid();
|
2021-06-03 22:12:09 +02:00
|
|
|
u32 low, high;
|
|
|
|
|
|
|
|
ASSERT(bsp_lapicid < 255);
|
|
|
|
|
|
|
|
printk(BIOS_DEBUG, "IOAPIC: Bootstrap Processor Local APIC = 0x%02x\n",
|
|
|
|
bsp_lapicid);
|
|
|
|
|
|
|
|
/* Enable Virtual Wire Mode. Should this be LOGICAL_DEST instead? */
|
|
|
|
low = INT_ENABLED | TRIGGER_EDGE | POLARITY_HIGH | PHYSICAL_DEST | ExtINT;
|
|
|
|
high = bsp_lapicid << (56 - 32);
|
|
|
|
|
2021-06-03 22:14:05 +02:00
|
|
|
write_vector(ioapic_base, 0, high, low);
|
2021-06-03 22:12:09 +02:00
|
|
|
|
|
|
|
if (io_apic_read(ioapic_base, 0x10) == 0xffffffff) {
|
|
|
|
printk(BIOS_WARNING, "IOAPIC not responding.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_ioapic_id(void *ioapic_base, u8 ioapic_id)
|
|
|
|
{
|
2013-04-23 14:49:41 +02:00
|
|
|
int i;
|
2010-01-16 19:31:34 +01:00
|
|
|
|
2019-12-09 22:03:29 +01:00
|
|
|
printk(BIOS_DEBUG, "IOAPIC: Initializing IOAPIC at %p\n",
|
2010-10-15 01:40:10 +02:00
|
|
|
ioapic_base);
|
2010-01-16 19:31:34 +01:00
|
|
|
|
|
|
|
if (ioapic_id) {
|
2010-04-27 08:56:47 +02:00
|
|
|
printk(BIOS_DEBUG, "IOAPIC: ID = 0x%02x\n", ioapic_id);
|
2010-10-15 01:40:10 +02:00
|
|
|
/* Set IOAPIC ID if it has been specified. */
|
2010-04-27 08:56:47 +02:00
|
|
|
io_apic_write(ioapic_base, 0x00,
|
2011-10-19 06:23:51 +02:00
|
|
|
(io_apic_read(ioapic_base, 0x00) & 0xf0ffffff) |
|
2010-10-15 01:40:10 +02:00
|
|
|
(ioapic_id << 24));
|
2010-01-16 19:31:34 +01:00
|
|
|
}
|
2013-04-23 14:49:41 +02:00
|
|
|
|
|
|
|
printk(BIOS_SPEW, "IOAPIC: Dumping registers\n");
|
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
printk(BIOS_SPEW, " reg 0x%04x: 0x%08x\n", i,
|
|
|
|
io_apic_read(ioapic_base, i));
|
|
|
|
|
2012-01-31 16:24:12 +01:00
|
|
|
}
|
|
|
|
|
2021-06-06 07:27:15 +02:00
|
|
|
u8 get_ioapic_id(void *ioapic_base)
|
|
|
|
{
|
|
|
|
return (io_apic_read(ioapic_base, 0x00) >> 24) & 0x0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
u8 get_ioapic_version(void *ioapic_base)
|
|
|
|
{
|
|
|
|
return io_apic_read(ioapic_base, 0x01) & 0xff;
|
|
|
|
}
|
|
|
|
|
2021-06-06 07:14:57 +02:00
|
|
|
void ioapic_set_boot_config(void *ioapic_base, bool irq_on_fsb)
|
2012-01-31 16:24:12 +01:00
|
|
|
{
|
2021-06-03 17:36:05 +02:00
|
|
|
if (irq_on_fsb) {
|
2017-06-01 19:39:59 +02:00
|
|
|
/*
|
|
|
|
* For the Pentium 4 and above APICs deliver their interrupts
|
|
|
|
* on the front side bus, enable that.
|
|
|
|
*/
|
|
|
|
printk(BIOS_DEBUG, "IOAPIC: Enabling interrupts on FSB\n");
|
|
|
|
io_apic_write(ioapic_base, 0x03,
|
|
|
|
io_apic_read(ioapic_base, 0x03) | (1 << 0));
|
2021-06-03 17:36:05 +02:00
|
|
|
} else {
|
2017-06-01 19:39:59 +02:00
|
|
|
printk(BIOS_DEBUG,
|
|
|
|
"IOAPIC: Enabling interrupts on APIC serial bus\n");
|
|
|
|
io_apic_write(ioapic_base, 0x03, 0);
|
|
|
|
}
|
2021-06-06 07:14:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void setup_ioapic_helper(void *ioapic_base, u8 ioapic_id, bool enable_virtual_wire)
|
|
|
|
{
|
|
|
|
set_ioapic_id(ioapic_base, ioapic_id);
|
2021-06-06 05:19:15 +02:00
|
|
|
clear_ioapic(ioapic_base);
|
2010-01-16 19:31:34 +01:00
|
|
|
|
2021-06-06 05:19:15 +02:00
|
|
|
if (enable_virtual_wire)
|
2021-06-03 17:36:05 +02:00
|
|
|
route_i8259_irq0(ioapic_base);
|
2003-04-24 08:25:08 +02:00
|
|
|
}
|
2012-01-31 16:24:12 +01:00
|
|
|
|
2021-06-03 17:36:05 +02:00
|
|
|
|
2014-12-25 03:43:20 +01:00
|
|
|
void setup_ioapic(void *ioapic_base, u8 ioapic_id)
|
2012-01-31 16:24:12 +01:00
|
|
|
{
|
2021-06-06 07:14:57 +02:00
|
|
|
setup_ioapic_helper(ioapic_base, ioapic_id, true);
|
2012-01-31 16:24:12 +01:00
|
|
|
}
|