blob: 3b6c4f92b10c129e056af569647b24ecb20327c7 [file] [log] [blame]
Raul E Rangel789aefc2020-05-11 16:26:35 -06001/* SPDX-License-Identifier: GPL-2.0-only */
2
Furquan Shaikh0c707d42020-07-08 16:54:40 -07003#include <acpi/acpi_device.h>
Felix Helddba3fe72021-02-13 01:05:56 +01004#include <amdblocks/data_fabric.h>
Raul E Rangel789aefc2020-05-11 16:26:35 -06005#include <console/console.h>
6#include <cpu/x86/lapic_def.h>
Furquan Shaikh0c707d42020-07-08 16:54:40 -07007#include <device/device.h>
8#include <device/pci.h>
9#include <device/pci_ids.h>
Raul E Rangel789aefc2020-05-11 16:26:35 -060010#include <soc/data_fabric.h>
11#include <soc/iomap.h>
Felix Heldbf90b142020-09-15 16:19:54 +020012#include <types.h>
Raul E Rangel789aefc2020-05-11 16:26:35 -060013
Felix Helda79e01b2020-09-15 16:21:46 +020014static void disable_mmio_reg(unsigned int reg)
Raul E Rangel789aefc2020-05-11 16:26:35 -060015{
Felix Held0a149132021-02-13 01:22:39 +010016 data_fabric_broadcast_write32(0, NB_MMIO_CONTROL(reg),
Raul E Rangel789aefc2020-05-11 16:26:35 -060017 IOMS0_FABRIC_ID << MMIO_DST_FABRIC_ID_SHIFT);
Felix Held0a149132021-02-13 01:22:39 +010018 data_fabric_broadcast_write32(0, NB_MMIO_BASE(reg), 0);
19 data_fabric_broadcast_write32(0, NB_MMIO_LIMIT(reg), 0);
Raul E Rangel789aefc2020-05-11 16:26:35 -060020}
21
Felix Helda79e01b2020-09-15 16:21:46 +020022static bool is_mmio_reg_disabled(unsigned int reg)
Raul E Rangel789aefc2020-05-11 16:26:35 -060023{
Felix Held0a149132021-02-13 01:22:39 +010024 uint32_t val = data_fabric_broadcast_read32(0, NB_MMIO_CONTROL(reg));
Raul E Rangel789aefc2020-05-11 16:26:35 -060025 return !(val & ((MMIO_WE | MMIO_RE)));
26}
27
28static int find_unused_mmio_reg(void)
29{
30 unsigned int i;
31
32 for (i = 0; i < NUM_NB_MMIO_REGS; i++) {
33 if (is_mmio_reg_disabled(i))
34 return i;
35 }
36 return -1;
37}
38
39void data_fabric_set_mmio_np(void)
40{
41 /*
42 * Mark region from HPET-LAPIC or 0xfed00000-0xfee00000-1 as NP.
43 *
44 * AGESA has already programmed the NB MMIO routing, however nothing
45 * is yet marked as non-posted.
46 *
47 * If there exists an overlapping routing base/limit pair, trim its
48 * base or limit to avoid the new NP region. If any pair exists
49 * completely within HPET-LAPIC range, remove it. If any pair surrounds
50 * HPET-LAPIC, it must be split into two regions.
51 *
52 * TODO(b/156296146): Remove the settings from AGESA and allow coreboot
53 * to own everything. If not practical, consider erasing all settings
54 * and have coreboot reprogram them. At that time, make the source
55 * below more flexible.
56 * * Note that the code relies on the granularity of the HPET and
57 * LAPIC addresses being sufficiently large that the shifted limits
58 * +/-1 are always equivalent to the non-shifted values +/-1.
59 */
60
61 unsigned int i;
62 int reg;
63 uint32_t base, limit, ctrl;
64 const uint32_t np_bot = HPET_BASE_ADDRESS >> D18F0_MMIO_SHIFT;
65 const uint32_t np_top = (LOCAL_APIC_ADDR - 1) >> D18F0_MMIO_SHIFT;
66
67 for (i = 0; i < NUM_NB_MMIO_REGS; i++) {
68 /* Adjust all registers that overlap */
Felix Held0a149132021-02-13 01:22:39 +010069 ctrl = data_fabric_broadcast_read32(0, NB_MMIO_CONTROL(i));
Raul E Rangel789aefc2020-05-11 16:26:35 -060070 if (!(ctrl & (MMIO_WE | MMIO_RE)))
71 continue; /* not enabled */
72
Felix Held0a149132021-02-13 01:22:39 +010073 base = data_fabric_broadcast_read32(0, NB_MMIO_BASE(i));
74 limit = data_fabric_broadcast_read32(0, NB_MMIO_LIMIT(i));
Raul E Rangel789aefc2020-05-11 16:26:35 -060075
76 if (base > np_top || limit < np_bot)
77 continue; /* no overlap at all */
78
79 if (base >= np_bot && limit <= np_top) {
80 disable_mmio_reg(i); /* 100% within, so remove */
81 continue;
82 }
83
84 if (base < np_bot && limit > np_top) {
85 /* Split the configured region */
Felix Held0a149132021-02-13 01:22:39 +010086 data_fabric_broadcast_write32(0, NB_MMIO_LIMIT(i), np_bot - 1);
Raul E Rangel789aefc2020-05-11 16:26:35 -060087 reg = find_unused_mmio_reg();
88 if (reg < 0) {
89 /* Although a pair could be freed later, this condition is
90 * very unusual and deserves analysis. Flag an error and
91 * leave the topmost part unconfigured. */
92 printk(BIOS_ERR,
93 "Error: Not enough NB MMIO routing registers\n");
94 continue;
95 }
Felix Held0a149132021-02-13 01:22:39 +010096 data_fabric_broadcast_write32(0, NB_MMIO_BASE(reg), np_top + 1);
97 data_fabric_broadcast_write32(0, NB_MMIO_LIMIT(reg), limit);
98 data_fabric_broadcast_write32(0, NB_MMIO_CONTROL(reg), ctrl);
Raul E Rangel789aefc2020-05-11 16:26:35 -060099 continue;
100 }
101
102 /* If still here, adjust only the base or limit */
103 if (base <= np_bot)
Felix Held0a149132021-02-13 01:22:39 +0100104 data_fabric_broadcast_write32(0, NB_MMIO_LIMIT(i), np_bot - 1);
Raul E Rangel789aefc2020-05-11 16:26:35 -0600105 else
Felix Held0a149132021-02-13 01:22:39 +0100106 data_fabric_broadcast_write32(0, NB_MMIO_BASE(i), np_top + 1);
Raul E Rangel789aefc2020-05-11 16:26:35 -0600107 }
108
109 reg = find_unused_mmio_reg();
110 if (reg < 0) {
111 printk(BIOS_ERR, "Error: cannot configure region as NP\n");
112 return;
113 }
114
Felix Held0a149132021-02-13 01:22:39 +0100115 data_fabric_broadcast_write32(0, NB_MMIO_BASE(reg), np_bot);
116 data_fabric_broadcast_write32(0, NB_MMIO_LIMIT(reg), np_top);
117 data_fabric_broadcast_write32(0, NB_MMIO_CONTROL(reg),
Raul E Rangel789aefc2020-05-11 16:26:35 -0600118 (IOMS0_FABRIC_ID << MMIO_DST_FABRIC_ID_SHIFT) | MMIO_NP | MMIO_WE
119 | MMIO_RE);
120}
Furquan Shaikh0c707d42020-07-08 16:54:40 -0700121
122static const char *data_fabric_acpi_name(const struct device *dev)
123{
124 switch (dev->device) {
Felix Held0e5dde5d2020-11-17 14:57:40 +0100125 case PCI_DEVICE_ID_AMD_FAM17H_MODEL18H_DF0:
Furquan Shaikh0c707d42020-07-08 16:54:40 -0700126 return "DFD0";
Felix Held0e5dde5d2020-11-17 14:57:40 +0100127 case PCI_DEVICE_ID_AMD_FAM17H_MODEL18H_DF1:
Furquan Shaikh0c707d42020-07-08 16:54:40 -0700128 return "DFD1";
Felix Held0e5dde5d2020-11-17 14:57:40 +0100129 case PCI_DEVICE_ID_AMD_FAM17H_MODEL18H_DF2:
Furquan Shaikh0c707d42020-07-08 16:54:40 -0700130 return "DFD2";
Felix Held0e5dde5d2020-11-17 14:57:40 +0100131 case PCI_DEVICE_ID_AMD_FAM17H_MODEL18H_DF3:
Furquan Shaikh0c707d42020-07-08 16:54:40 -0700132 return "DFD3";
Felix Held0e5dde5d2020-11-17 14:57:40 +0100133 case PCI_DEVICE_ID_AMD_FAM17H_MODEL18H_DF4:
Furquan Shaikh0c707d42020-07-08 16:54:40 -0700134 return "DFD4";
Felix Held0e5dde5d2020-11-17 14:57:40 +0100135 case PCI_DEVICE_ID_AMD_FAM17H_MODEL18H_DF5:
Furquan Shaikh0c707d42020-07-08 16:54:40 -0700136 return "DFD5";
Felix Held0e5dde5d2020-11-17 14:57:40 +0100137 case PCI_DEVICE_ID_AMD_FAM17H_MODEL18H_DF6:
Furquan Shaikh0c707d42020-07-08 16:54:40 -0700138 return "DFD6";
139 default:
140 printk(BIOS_ERR, "%s: Unhandled device id 0x%x\n", __func__, dev->device);
141 }
142
143 return NULL;
144}
145
146static struct device_operations data_fabric_ops = {
147 .read_resources = noop_read_resources,
148 .set_resources = noop_set_resources,
149 .acpi_name = data_fabric_acpi_name,
150 .acpi_fill_ssdt = acpi_device_write_pci_dev,
151};
152
153static const unsigned short pci_device_ids[] = {
Felix Held0e5dde5d2020-11-17 14:57:40 +0100154 PCI_DEVICE_ID_AMD_FAM17H_MODEL18H_DF0,
155 PCI_DEVICE_ID_AMD_FAM17H_MODEL18H_DF1,
156 PCI_DEVICE_ID_AMD_FAM17H_MODEL18H_DF2,
157 PCI_DEVICE_ID_AMD_FAM17H_MODEL18H_DF3,
158 PCI_DEVICE_ID_AMD_FAM17H_MODEL18H_DF4,
159 PCI_DEVICE_ID_AMD_FAM17H_MODEL18H_DF5,
160 PCI_DEVICE_ID_AMD_FAM17H_MODEL18H_DF6,
Furquan Shaikh0c707d42020-07-08 16:54:40 -0700161 0
162};
163
164static const struct pci_driver data_fabric_driver __pci_driver = {
165 .ops = &data_fabric_ops,
166 .vendor = PCI_VENDOR_ID_AMD,
167 .devices = pci_device_ids,
168};