Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2010 coresystems GmbH |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 14 | */ |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 15 | |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 16 | #include <device/mmio.h> |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 17 | #include <arch/ioapic.h> |
| 18 | #include <console/console.h> |
| 19 | #include <cpu/x86/lapic.h> |
| 20 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 21 | u32 io_apic_read(void *ioapic_base, u32 reg) |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 22 | { |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 23 | write32(ioapic_base, reg); |
| 24 | return read32(ioapic_base + 0x10); |
| 25 | } |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 26 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 27 | void io_apic_write(void *ioapic_base, u32 reg, u32 value) |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 28 | { |
| 29 | write32(ioapic_base, reg); |
| 30 | write32(ioapic_base + 0x10, value); |
| 31 | } |
| 32 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 33 | static int ioapic_interrupt_count(void *ioapic_base) |
Patrick Georgi | d1de45e | 2013-01-23 13:45:23 +0100 | [diff] [blame] | 34 | { |
| 35 | /* Read the available number of interrupts. */ |
| 36 | int ioapic_interrupts = (io_apic_read(ioapic_base, 0x01) >> 16) & 0xff; |
| 37 | if (ioapic_interrupts == 0xff) |
| 38 | ioapic_interrupts = 23; |
| 39 | ioapic_interrupts += 1; /* Bits 23-16 specify the maximum redirection |
| 40 | entry, which is the number of interrupts |
| 41 | minus 1. */ |
| 42 | printk(BIOS_DEBUG, "IOAPIC: %d interrupts\n", ioapic_interrupts); |
| 43 | |
| 44 | return ioapic_interrupts; |
| 45 | } |
| 46 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 47 | void clear_ioapic(void *ioapic_base) |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 48 | { |
| 49 | u32 low, high; |
| 50 | u32 i, ioapic_interrupts; |
| 51 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 52 | printk(BIOS_DEBUG, "IOAPIC: Clearing IOAPIC at %p\n", ioapic_base); |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 53 | |
Patrick Georgi | d1de45e | 2013-01-23 13:45:23 +0100 | [diff] [blame] | 54 | ioapic_interrupts = ioapic_interrupt_count(ioapic_base); |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 55 | |
Marc Jones | f7dc972 | 2018-03-31 22:45:35 -0600 | [diff] [blame] | 56 | low = INT_DISABLED; |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 57 | high = NONE; |
| 58 | |
| 59 | for (i = 0; i < ioapic_interrupts; i++) { |
| 60 | io_apic_write(ioapic_base, i * 2 + 0x10, low); |
| 61 | io_apic_write(ioapic_base, i * 2 + 0x11, high); |
| 62 | |
Uwe Hermann | e4990365 | 2010-10-14 23:40:10 +0000 | [diff] [blame] | 63 | printk(BIOS_SPEW, "IOAPIC: reg 0x%08x value 0x%08x 0x%08x\n", |
| 64 | i, high, low); |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | if (io_apic_read(ioapic_base, 0x10) == 0xffffffff) { |
Uwe Hermann | e4990365 | 2010-10-14 23:40:10 +0000 | [diff] [blame] | 68 | printk(BIOS_WARNING, "IOAPIC not responding.\n"); |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 69 | return; |
| 70 | } |
| 71 | } |
| 72 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 73 | void set_ioapic_id(void *ioapic_base, u8 ioapic_id) |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 74 | { |
| 75 | u32 bsp_lapicid = lapicid(); |
Paul Menzel | 1b3e176 | 2013-04-23 14:49:41 +0200 | [diff] [blame] | 76 | int i; |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 77 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 78 | printk(BIOS_DEBUG, "IOAPIC: Initializing IOAPIC at 0x%p\n", |
Uwe Hermann | e4990365 | 2010-10-14 23:40:10 +0000 | [diff] [blame] | 79 | ioapic_base); |
| 80 | printk(BIOS_DEBUG, "IOAPIC: Bootstrap Processor Local APIC = 0x%02x\n", |
| 81 | bsp_lapicid); |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 82 | |
| 83 | if (ioapic_id) { |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 84 | printk(BIOS_DEBUG, "IOAPIC: ID = 0x%02x\n", ioapic_id); |
Uwe Hermann | e4990365 | 2010-10-14 23:40:10 +0000 | [diff] [blame] | 85 | /* Set IOAPIC ID if it has been specified. */ |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 86 | io_apic_write(ioapic_base, 0x00, |
Kyösti Mälkki | 939103c | 2011-10-19 07:23:51 +0300 | [diff] [blame] | 87 | (io_apic_read(ioapic_base, 0x00) & 0xf0ffffff) | |
Uwe Hermann | e4990365 | 2010-10-14 23:40:10 +0000 | [diff] [blame] | 88 | (ioapic_id << 24)); |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 89 | } |
Paul Menzel | 1b3e176 | 2013-04-23 14:49:41 +0200 | [diff] [blame] | 90 | |
| 91 | printk(BIOS_SPEW, "IOAPIC: Dumping registers\n"); |
| 92 | for (i = 0; i < 3; i++) |
| 93 | printk(BIOS_SPEW, " reg 0x%04x: 0x%08x\n", i, |
| 94 | io_apic_read(ioapic_base, i)); |
| 95 | |
Kyösti Mälkki | db4f875 | 2012-01-31 17:24:12 +0200 | [diff] [blame] | 96 | } |
| 97 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 98 | static void load_vectors(void *ioapic_base) |
Kyösti Mälkki | db4f875 | 2012-01-31 17:24:12 +0200 | [diff] [blame] | 99 | { |
| 100 | u32 bsp_lapicid = lapicid(); |
| 101 | u32 low, high; |
| 102 | u32 i, ioapic_interrupts; |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 103 | |
Patrick Georgi | d1de45e | 2013-01-23 13:45:23 +0100 | [diff] [blame] | 104 | ioapic_interrupts = ioapic_interrupt_count(ioapic_base); |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 105 | |
Martin Roth | 898a775 | 2017-06-01 11:39:59 -0600 | [diff] [blame] | 106 | if (IS_ENABLED(CONFIG_IOAPIC_INTERRUPTS_ON_FSB)) { |
| 107 | /* |
| 108 | * For the Pentium 4 and above APICs deliver their interrupts |
| 109 | * on the front side bus, enable that. |
| 110 | */ |
| 111 | printk(BIOS_DEBUG, "IOAPIC: Enabling interrupts on FSB\n"); |
| 112 | io_apic_write(ioapic_base, 0x03, |
| 113 | io_apic_read(ioapic_base, 0x03) | (1 << 0)); |
| 114 | } else if (IS_ENABLED(CONFIG_IOAPIC_INTERRUPTS_ON_APIC_SERIAL_BUS)) { |
| 115 | printk(BIOS_DEBUG, |
| 116 | "IOAPIC: Enabling interrupts on APIC serial bus\n"); |
| 117 | io_apic_write(ioapic_base, 0x03, 0); |
| 118 | } |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 119 | |
Uwe Hermann | e4990365 | 2010-10-14 23:40:10 +0000 | [diff] [blame] | 120 | /* Enable Virtual Wire Mode. */ |
Marc Jones | f7dc972 | 2018-03-31 22:45:35 -0600 | [diff] [blame] | 121 | low = INT_ENABLED | TRIGGER_EDGE | POLARITY_HIGH | PHYSICAL_DEST | ExtINT; |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 122 | high = bsp_lapicid << (56 - 32); |
| 123 | |
| 124 | io_apic_write(ioapic_base, 0x10, low); |
| 125 | io_apic_write(ioapic_base, 0x11, high); |
| 126 | |
| 127 | if (io_apic_read(ioapic_base, 0x10) == 0xffffffff) { |
Uwe Hermann | e4990365 | 2010-10-14 23:40:10 +0000 | [diff] [blame] | 128 | printk(BIOS_WARNING, "IOAPIC not responding.\n"); |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 129 | return; |
| 130 | } |
| 131 | |
Uwe Hermann | e4990365 | 2010-10-14 23:40:10 +0000 | [diff] [blame] | 132 | printk(BIOS_SPEW, "IOAPIC: reg 0x%08x value 0x%08x 0x%08x\n", |
| 133 | 0, high, low); |
Marc Jones | f7dc972 | 2018-03-31 22:45:35 -0600 | [diff] [blame] | 134 | low = INT_DISABLED; |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 135 | high = NONE; |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 136 | for (i = 1; i < ioapic_interrupts; i++) { |
| 137 | io_apic_write(ioapic_base, i * 2 + 0x10, low); |
| 138 | io_apic_write(ioapic_base, i * 2 + 0x11, high); |
| 139 | |
Uwe Hermann | e4990365 | 2010-10-14 23:40:10 +0000 | [diff] [blame] | 140 | printk(BIOS_SPEW, "IOAPIC: reg 0x%08x value 0x%08x 0x%08x\n", |
| 141 | i, high, low); |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
Kyösti Mälkki | db4f875 | 2012-01-31 17:24:12 +0200 | [diff] [blame] | 144 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 145 | void setup_ioapic(void *ioapic_base, u8 ioapic_id) |
Kyösti Mälkki | db4f875 | 2012-01-31 17:24:12 +0200 | [diff] [blame] | 146 | { |
| 147 | set_ioapic_id(ioapic_base, ioapic_id); |
| 148 | load_vectors(ioapic_base); |
| 149 | } |