Yinghai Lu | 70093f7 | 2004-07-01 03:55:03 +0000 | [diff] [blame] | 1 | #include <console/console.h> |
| 2 | #include <device/device.h> |
| 3 | #include <device/pci.h> |
| 4 | #include <device/pci_ids.h> |
| 5 | #include <device/pci_ops.h> |
| 6 | #include <pc80/mc146818rtc.h> |
Steven J. Magnani | 09e4ef6 | 2005-09-14 13:56:25 +0000 | [diff] [blame] | 7 | #include <assert.h> |
Yinghai Lu | 70093f7 | 2004-07-01 03:55:03 +0000 | [diff] [blame] | 8 | #include "82870.h" |
| 9 | |
Steven J. Magnani | 09e4ef6 | 2005-09-14 13:56:25 +0000 | [diff] [blame] | 10 | static int num_p64h2_ioapics = 0; |
Yinghai Lu | 70093f7 | 2004-07-01 03:55:03 +0000 | [diff] [blame] | 11 | |
| 12 | static void p64h2_ioapic_enable(device_t dev) |
| 13 | { |
Steven J. Magnani | 09e4ef6 | 2005-09-14 13:56:25 +0000 | [diff] [blame] | 14 | /* We have to enable MEM and Bus Master for IOAPIC */ |
| 15 | uint16_t command = PCI_COMMAND_SERR | PCI_COMMAND_PARITY | PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY; |
Yinghai Lu | 70093f7 | 2004-07-01 03:55:03 +0000 | [diff] [blame] | 16 | |
| 17 | |
Steven J. Magnani | 09e4ef6 | 2005-09-14 13:56:25 +0000 | [diff] [blame] | 18 | pci_write_config16(dev, PCI_COMMAND, command); |
Yinghai Lu | 70093f7 | 2004-07-01 03:55:03 +0000 | [diff] [blame] | 19 | } |
| 20 | |
Uwe Hermann | b69cb5a | 2010-10-26 22:46:43 +0000 | [diff] [blame] | 21 | /** |
| 22 | * Configure one of the IOAPICs in a P64H2. |
| 23 | * |
| 24 | * Note that a PCI bus scan will detect both IOAPICs, so this function |
| 25 | * will be called twice for each P64H2 in the system. |
| 26 | * |
| 27 | * @param dev PCI bus/device/function of P64H2 IOAPIC. |
| 28 | * NOTE: There are two IOAPICs per P64H2, at D28:F0 and D30:F0. |
| 29 | */ |
Yinghai Lu | 70093f7 | 2004-07-01 03:55:03 +0000 | [diff] [blame] | 30 | static void p64h2_ioapic_init(device_t dev) |
| 31 | { |
Steven J. Magnani | 09e4ef6 | 2005-09-14 13:56:25 +0000 | [diff] [blame] | 32 | uint32_t memoryBase; |
| 33 | int apic_index, apic_id; |
Yinghai Lu | 70093f7 | 2004-07-01 03:55:03 +0000 | [diff] [blame] | 34 | |
Steven J. Magnani | 09e4ef6 | 2005-09-14 13:56:25 +0000 | [diff] [blame] | 35 | volatile uint32_t* pIndexRegister; /* io apic io memory space command address */ |
| 36 | volatile uint32_t* pWindowRegister; /* io apic io memory space data address */ |
Yinghai Lu | 70093f7 | 2004-07-01 03:55:03 +0000 | [diff] [blame] | 37 | |
Steven J. Magnani | 09e4ef6 | 2005-09-14 13:56:25 +0000 | [diff] [blame] | 38 | apic_index = num_p64h2_ioapics; |
| 39 | num_p64h2_ioapics++; |
Yinghai Lu | 70093f7 | 2004-07-01 03:55:03 +0000 | [diff] [blame] | 40 | |
Steven J. Magnani | 09e4ef6 | 2005-09-14 13:56:25 +0000 | [diff] [blame] | 41 | // A note on IOAPIC addresses: |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 42 | // 0 and 1 are used for the local APICs of the dual virtual |
Uwe Hermann | 607614d | 2010-11-18 20:12:13 +0000 | [diff] [blame] | 43 | // (hyper-threaded) CPUs of physical CPU 0 (devicetree.cb). |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 44 | // 6 and 7 are used for the local APICs of the dual virtual |
Uwe Hermann | 607614d | 2010-11-18 20:12:13 +0000 | [diff] [blame] | 45 | // (hyper-threaded) CPUs of physical CPU 1 (devicetree.cb). |
| 46 | // 2 is used for the IOAPIC in the 82801 southbridge (hard-coded in i82801xx_lpc.c) |
Yinghai Lu | 70093f7 | 2004-07-01 03:55:03 +0000 | [diff] [blame] | 47 | |
Steven J. Magnani | 09e4ef6 | 2005-09-14 13:56:25 +0000 | [diff] [blame] | 48 | // Map APIC index into APIC ID |
| 49 | // IDs 3, 4, 5, and 8+ are available (see above note) |
Yinghai Lu | 70093f7 | 2004-07-01 03:55:03 +0000 | [diff] [blame] | 50 | |
Steven J. Magnani | 09e4ef6 | 2005-09-14 13:56:25 +0000 | [diff] [blame] | 51 | if (apic_index < 3) |
| 52 | apic_id = apic_index + 3; |
| 53 | else |
| 54 | apic_id = apic_index + 5; |
Yinghai Lu | 70093f7 | 2004-07-01 03:55:03 +0000 | [diff] [blame] | 55 | |
Steven J. Magnani | 09e4ef6 | 2005-09-14 13:56:25 +0000 | [diff] [blame] | 56 | ASSERT(apic_id < 16); // ID is only 4 bits |
Yinghai Lu | 70093f7 | 2004-07-01 03:55:03 +0000 | [diff] [blame] | 57 | |
Steven J. Magnani | 09e4ef6 | 2005-09-14 13:56:25 +0000 | [diff] [blame] | 58 | // Read the MBAR address for setting up the IOAPIC in memory space |
| 59 | // NOTE: this address was assigned during enumeration of the bus |
| 60 | |
| 61 | memoryBase = pci_read_config32(dev, PCI_BASE_ADDRESS_0); |
| 62 | pIndexRegister = (volatile uint32_t*) memoryBase; |
| 63 | pWindowRegister = (volatile uint32_t*)(memoryBase + 0x10); |
| 64 | |
Myles Watson | 08e0fb8 | 2010-03-22 16:33:25 +0000 | [diff] [blame] | 65 | printk(BIOS_DEBUG, "IOAPIC %d at %02x:%02x.%01x MBAR = %p DataAddr = %p\n", |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 66 | apic_id, dev->bus->secondary, PCI_SLOT(dev->path.pci.devfn), |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 67 | PCI_FUNC(dev->path.pci.devfn), pIndexRegister, pWindowRegister); |
Steven J. Magnani | 09e4ef6 | 2005-09-14 13:56:25 +0000 | [diff] [blame] | 68 | |
| 69 | apic_id <<= 24; // Convert ID to bitmask |
| 70 | |
| 71 | *pIndexRegister = 0; // Select APIC ID register |
| 72 | *pWindowRegister = (*pWindowRegister & ~(0xF<<24)) | apic_id; // Set the ID |
| 73 | |
| 74 | if ((*pWindowRegister & (0xF<<24)) != apic_id) |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 75 | die("p64h2_ioapic_init failed"); |
Steven J. Magnani | 09e4ef6 | 2005-09-14 13:56:25 +0000 | [diff] [blame] | 76 | |
| 77 | *pIndexRegister = 3; // Select Boot Configuration register |
| 78 | *pWindowRegister |= 1; // Use Processor System Bus to deliver interrupts |
| 79 | |
| 80 | if (!(*pWindowRegister & 1)) |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 81 | die("p64h2_ioapic_init failed"); |
Yinghai Lu | 70093f7 | 2004-07-01 03:55:03 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static struct device_operations ioapic_ops = { |
| 85 | .read_resources = pci_dev_read_resources, |
| 86 | .set_resources = pci_dev_set_resources, |
| 87 | .enable_resources = pci_dev_enable_resources, |
| 88 | .init = p64h2_ioapic_init, |
| 89 | .scan_bus = 0, |
| 90 | .enable = p64h2_ioapic_enable, |
| 91 | }; |
| 92 | |
Stefan Reinauer | f1cf1f7 | 2007-10-24 09:08:58 +0000 | [diff] [blame] | 93 | static const struct pci_driver ioapic_driver __pci_driver = { |
Yinghai Lu | 70093f7 | 2004-07-01 03:55:03 +0000 | [diff] [blame] | 94 | .ops = &ioapic_ops, |
| 95 | .vendor = PCI_VENDOR_ID_INTEL, |
| 96 | .device = PCI_DEVICE_ID_INTEL_82870_1E0, |
| 97 | |
| 98 | }; |