blob: 9c8ec231daa2679e898d7819155fd4a6bb867698 [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Martin Rothebace9f2018-05-26 18:56:17 -06002
Kyösti Mälkki5c2594e2021-06-08 10:47:24 +03003#include <arch/ioapic.h>
Yinghai Lu70093f72004-07-01 03:55:03 +00004#include <device/device.h>
5#include <device/pci.h>
6#include <device/pci_ids.h>
7#include <device/pci_ops.h>
Steven J. Magnani09e4ef62005-09-14 13:56:25 +00008#include <assert.h>
Yinghai Lu70093f72004-07-01 03:55:03 +00009#include "82870.h"
10
Steven J. Magnani09e4ef62005-09-14 13:56:25 +000011static int num_p64h2_ioapics = 0;
Yinghai Lu70093f72004-07-01 03:55:03 +000012
Elyes HAOUAS17c59f52018-05-13 13:38:38 +020013static void p64h2_ioapic_enable(struct device *dev)
Yinghai Lu70093f72004-07-01 03:55:03 +000014{
Elyes HAOUASba28e8d2016-08-31 19:22:16 +020015 /* We have to enable MEM and Bus Master for IOAPIC */
16 uint16_t command = PCI_COMMAND_SERR | PCI_COMMAND_PARITY | PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
Yinghai Lu70093f72004-07-01 03:55:03 +000017
Elyes HAOUASba28e8d2016-08-31 19:22:16 +020018 pci_write_config16(dev, PCI_COMMAND, command);
Yinghai Lu70093f72004-07-01 03:55:03 +000019}
20
Uwe Hermannb69cb5a2010-10-26 22:46:43 +000021/**
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 */
Elyes HAOUAS17c59f52018-05-13 13:38:38 +020030static void p64h2_ioapic_init(struct device *dev)
Yinghai Lu70093f72004-07-01 03:55:03 +000031{
Elyes HAOUASba28e8d2016-08-31 19:22:16 +020032 uint32_t memoryBase;
33 int apic_index, apic_id;
Yinghai Lu70093f72004-07-01 03:55:03 +000034
Elyes HAOUASba28e8d2016-08-31 19:22:16 +020035 apic_index = num_p64h2_ioapics;
36 num_p64h2_ioapics++;
Yinghai Lu70093f72004-07-01 03:55:03 +000037
Elyes HAOUASba28e8d2016-08-31 19:22:16 +020038 // A note on IOAPIC addresses:
39 // 0 and 1 are used for the local APICs of the dual virtual
40 // (hyper-threaded) CPUs of physical CPU 0 (devicetree.cb).
41 // 6 and 7 are used for the local APICs of the dual virtual
42 // (hyper-threaded) CPUs of physical CPU 1 (devicetree.cb).
43 // 2 is used for the IOAPIC in the 82801 southbridge (hard-coded in i82801xx_lpc.c)
Yinghai Lu70093f72004-07-01 03:55:03 +000044
Elyes HAOUASba28e8d2016-08-31 19:22:16 +020045 // Map APIC index into APIC ID
46 // IDs 3, 4, 5, and 8+ are available (see above note)
Yinghai Lu70093f72004-07-01 03:55:03 +000047
Elyes HAOUASba28e8d2016-08-31 19:22:16 +020048 if (apic_index < 3)
49 apic_id = apic_index + 3;
50 else
51 apic_id = apic_index + 5;
Yinghai Lu70093f72004-07-01 03:55:03 +000052
Elyes HAOUASba28e8d2016-08-31 19:22:16 +020053 ASSERT(apic_id < 16); // ID is only 4 bits
Yinghai Lu70093f72004-07-01 03:55:03 +000054
Elyes HAOUASba28e8d2016-08-31 19:22:16 +020055 // Read the MBAR address for setting up the IOAPIC in memory space
56 // NOTE: this address was assigned during enumeration of the bus
Steven J. Magnani09e4ef62005-09-14 13:56:25 +000057
Elyes HAOUASba28e8d2016-08-31 19:22:16 +020058 memoryBase = pci_read_config32(dev, PCI_BASE_ADDRESS_0);
Steven J. Magnani09e4ef62005-09-14 13:56:25 +000059
Kyösti Mälkki5c2594e2021-06-08 10:47:24 +030060 set_ioapic_id((void *)memoryBase, apic_id);
Steven J. Magnani09e4ef62005-09-14 13:56:25 +000061
Kyösti Mälkki5c2594e2021-06-08 10:47:24 +030062 // Use Processor System Bus to deliver interrupts
63 ioapic_set_boot_config((void *)memoryBase, true);
Yinghai Lu70093f72004-07-01 03:55:03 +000064}
65
66static struct device_operations ioapic_ops = {
Elyes HAOUASba28e8d2016-08-31 19:22:16 +020067 .read_resources = pci_dev_read_resources,
68 .set_resources = pci_dev_set_resources,
69 .enable_resources = pci_dev_enable_resources,
70 .init = p64h2_ioapic_init,
Elyes HAOUASba28e8d2016-08-31 19:22:16 +020071 .enable = p64h2_ioapic_enable,
Yinghai Lu70093f72004-07-01 03:55:03 +000072};
73
Stefan Reinauerf1cf1f72007-10-24 09:08:58 +000074static const struct pci_driver ioapic_driver __pci_driver = {
Elyes HAOUASba28e8d2016-08-31 19:22:16 +020075 .ops = &ioapic_ops,
76 .vendor = PCI_VENDOR_ID_INTEL,
77 .device = PCI_DEVICE_ID_INTEL_82870_1E0,
Yinghai Lu70093f72004-07-01 03:55:03 +000078
79};