blob: 9d0ece1d6ca99fb38c4efe09f1eb7e9e30a47c18 [file] [log] [blame]
Jason Gleneskf934fae2021-07-20 02:19:58 -07001/* SPDX-License-Identifier: GPL-2.0-only */
2
3#include <acpi/acpi_ivrs.h>
4#include <amdblocks/acpi.h>
5#include <amdblocks/cpu.h>
Jason Gleneskf934fae2021-07-20 02:19:58 -07006#include <amdblocks/ioapic.h>
Felix Heldc32df9a2023-07-03 18:52:36 +02007#include <amdblocks/iommu.h>
Felix Held96fa6a22022-12-06 01:25:13 +01008#include <arch/ioapic.h>
Jason Gleneskf934fae2021-07-20 02:19:58 -07009#include <console/console.h>
10#include <cpu/amd/cpuid.h>
Jason Gleneskf934fae2021-07-20 02:19:58 -070011#include <device/device.h>
Elyes Haouas8823ba12022-12-05 08:48:50 +010012#include <device/mmio.h>
Jason Gleneskf934fae2021-07-20 02:19:58 -070013#include <device/pci_def.h>
14#include <device/pci_ops.h>
Jason Gleneskf934fae2021-07-20 02:19:58 -070015#include <soc/data_fabric.h>
Felix Held96fa6a22022-12-06 01:25:13 +010016#include <soc/iomap.h>
Jason Gleneskf934fae2021-07-20 02:19:58 -070017#include <soc/pci_devs.h>
Jason Gleneskf934fae2021-07-20 02:19:58 -070018
Arthur Heymansf9ee87f2023-06-07 15:29:02 +020019static unsigned long acpi_fill_ivrs_ioapic(unsigned long current, void *ioapic_base,
Naresh Solanki4ef89f72023-05-25 17:37:50 +020020 uint16_t src_devid, uint8_t dte_setting)
Jason Gleneskf934fae2021-07-20 02:19:58 -070021{
22 ivrs_ivhd_special_t *ivhd_ioapic = (ivrs_ivhd_special_t *)current;
23 memset(ivhd_ioapic, 0, sizeof(*ivhd_ioapic));
24
25 ivhd_ioapic->type = IVHD_DEV_8_BYTE_EXT_SPECIAL_DEV;
Naresh Solanki4ef89f72023-05-25 17:37:50 +020026 ivhd_ioapic->dte_setting = dte_setting;
27 ivhd_ioapic->handle = get_ioapic_id(ioapic_base);
28 ivhd_ioapic->source_dev_id = src_devid;
Jason Gleneskf934fae2021-07-20 02:19:58 -070029 ivhd_ioapic->variety = IVHD_SPECIAL_DEV_IOAPIC;
30 current += sizeof(ivrs_ivhd_special_t);
31
32 return current;
33}
34
Naresh Solanki4ef89f72023-05-25 17:37:50 +020035static unsigned long ivhd_describe_hpet(unsigned long current, uint8_t hndl, uint16_t src_devid)
Jason Gleneskf934fae2021-07-20 02:19:58 -070036{
37 ivrs_ivhd_special_t *ivhd_hpet = (ivrs_ivhd_special_t *)current;
Felix Held56167c52023-06-22 23:22:19 +020038 memset(ivhd_hpet, 0, sizeof(*ivhd_hpet));
Jason Gleneskf934fae2021-07-20 02:19:58 -070039
40 ivhd_hpet->type = IVHD_DEV_8_BYTE_EXT_SPECIAL_DEV;
Naresh Solanki4ef89f72023-05-25 17:37:50 +020041 ivhd_hpet->handle = hndl;
42 ivhd_hpet->source_dev_id = src_devid; /* function 0 of FCH PCI device */
Jason Gleneskf934fae2021-07-20 02:19:58 -070043 ivhd_hpet->variety = IVHD_SPECIAL_DEV_HPET;
44 current += sizeof(ivrs_ivhd_special_t);
45
46 return current;
47}
48
Felix Held534cce32023-06-22 23:09:23 +020049static unsigned long ivhd_describe_f0_device(unsigned long current, uint16_t dev_id,
50 const char acpi_hid[8], uint8_t datasetting)
Jason Gleneskf934fae2021-07-20 02:19:58 -070051{
Elyes Haouas68fc51f2022-07-16 09:48:27 +020052 ivrs_ivhd_f0_entry_t *ivhd_f0 = (ivrs_ivhd_f0_entry_t *)current;
Felix Held63a4e6bd2023-06-22 23:04:19 +020053 memset(ivhd_f0, 0, sizeof(*ivhd_f0));
Jason Gleneskf934fae2021-07-20 02:19:58 -070054
55 ivhd_f0->type = IVHD_DEV_VARIABLE;
56 ivhd_f0->dev_id = dev_id;
57 ivhd_f0->dte_setting = datasetting;
Felix Held534cce32023-06-22 23:09:23 +020058
59 memcpy(ivhd_f0->hardware_id, acpi_hid, sizeof(ivhd_f0->hardware_id));
Jason Gleneskf934fae2021-07-20 02:19:58 -070060
Jason Gleneskf934fae2021-07-20 02:19:58 -070061 current += sizeof(ivrs_ivhd_f0_entry_t);
62 return current;
63}
64
65static unsigned long ivhd_dev_range(unsigned long current, uint16_t start_devid,
66 uint16_t end_devid, uint8_t setting)
67{
68 /* 4-byte IVHD structures must be aligned to the 4-byte boundary. */
69 current = ALIGN_UP(current, 4);
70 ivrs_ivhd_generic_t *ivhd_range = (ivrs_ivhd_generic_t *)current;
Felix Heldfe242ce2023-06-23 19:03:57 +020071 memset(ivhd_range, 0, sizeof(*ivhd_range));
Jason Gleneskf934fae2021-07-20 02:19:58 -070072
73 /* Create the start range IVHD entry */
74 ivhd_range->type = IVHD_DEV_4_BYTE_START_RANGE;
75 ivhd_range->dev_id = start_devid;
76 ivhd_range->dte_setting = setting;
77 current += sizeof(ivrs_ivhd_generic_t);
78
79 /* Create the end range IVHD entry */
80 ivhd_range = (ivrs_ivhd_generic_t *)current;
81 ivhd_range->type = IVHD_DEV_4_BYTE_END_RANGE;
82 ivhd_range->dev_id = end_devid;
83 ivhd_range->dte_setting = setting;
84 current += sizeof(ivrs_ivhd_generic_t);
85
86 return current;
87}
88
89static unsigned long add_ivhd_dev_entry(struct device *parent, struct device *dev,
90 unsigned long *current, uint8_t type, uint8_t data)
91{
92 if (type == IVHD_DEV_4_BYTE_SELECT) {
93 /* 4-byte IVHD structures must be aligned to the 4-byte boundary. */
94 *current = ALIGN_UP(*current, 4);
95 ivrs_ivhd_generic_t *ivhd_entry = (ivrs_ivhd_generic_t *)*current;
Felix Heldfe242ce2023-06-23 19:03:57 +020096 memset(ivhd_entry, 0, sizeof(*ivhd_entry));
Jason Gleneskf934fae2021-07-20 02:19:58 -070097
98 ivhd_entry->type = type;
99 ivhd_entry->dev_id = dev->path.pci.devfn | (dev->bus->secondary << 8);
100 ivhd_entry->dte_setting = data;
101 *current += sizeof(ivrs_ivhd_generic_t);
102 } else if (type == IVHD_DEV_8_BYTE_ALIAS_SELECT) {
103 ivrs_ivhd_alias_t *ivhd_entry = (ivrs_ivhd_alias_t *)*current;
Felix Heldfe242ce2023-06-23 19:03:57 +0200104 memset(ivhd_entry, 0, sizeof(*ivhd_entry));
Jason Gleneskf934fae2021-07-20 02:19:58 -0700105
106 ivhd_entry->type = type;
107 ivhd_entry->dev_id = dev->path.pci.devfn | (dev->bus->secondary << 8);
108 ivhd_entry->dte_setting = data;
109 ivhd_entry->reserved1 = 0;
110 ivhd_entry->reserved2 = 0;
111 ivhd_entry->source_dev_id = parent->path.pci.devfn |
112 (parent->bus->secondary << 8);
113 *current += sizeof(ivrs_ivhd_alias_t);
114 }
115
116 return *current;
117}
118
119static void ivrs_add_device_or_bridge(struct device *parent, struct device *dev,
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200120 unsigned long *current)
Jason Gleneskf934fae2021-07-20 02:19:58 -0700121{
122 unsigned int header_type, is_pcie;
Jason Gleneskf934fae2021-07-20 02:19:58 -0700123
124 header_type = dev->hdr_type & 0x7f;
125 is_pcie = pci_find_capability(dev, PCI_CAP_ID_PCIE);
126
127 if (((header_type == PCI_HEADER_TYPE_NORMAL) ||
128 (header_type == PCI_HEADER_TYPE_BRIDGE)) && is_pcie) {
129 /* Device or Bridge is PCIe */
Jason Gleneskf934fae2021-07-20 02:19:58 -0700130 add_ivhd_dev_entry(parent, dev, current, IVHD_DEV_4_BYTE_SELECT, 0x0);
Jason Gleneskf934fae2021-07-20 02:19:58 -0700131 } else if ((header_type == PCI_HEADER_TYPE_NORMAL) && !is_pcie) {
132 /* Device is legacy PCI or PCI-X */
Jason Gleneskf934fae2021-07-20 02:19:58 -0700133 add_ivhd_dev_entry(parent, dev, current, IVHD_DEV_8_BYTE_ALIAS_SELECT, 0x0);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200134
Jason Gleneskf934fae2021-07-20 02:19:58 -0700135 }
136}
137
138static void add_ivhd_device_entries(struct device *parent, struct device *dev,
139 unsigned int depth, int linknum, int8_t *root_level,
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200140 unsigned long *current, uint16_t nb_bus)
Jason Gleneskf934fae2021-07-20 02:19:58 -0700141{
142 struct device *sibling;
143 struct bus *link;
144
145 if (!root_level)
146 return;
147
148 if (dev->path.type == DEVICE_PATH_PCI) {
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200149 if ((dev->bus->secondary == nb_bus) &&
Jason Gleneskf934fae2021-07-20 02:19:58 -0700150 (dev->path.pci.devfn == 0x0))
151 *root_level = depth;
152
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200153 if ((*root_level != -1) && (dev->enabled))
Jason Gleneskf934fae2021-07-20 02:19:58 -0700154 if (depth != *root_level)
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200155 ivrs_add_device_or_bridge(parent, dev, current);
Jason Gleneskf934fae2021-07-20 02:19:58 -0700156 }
157
158 for (link = dev->link_list; link; link = link->next)
159 for (sibling = link->children; sibling; sibling =
160 sibling->sibling)
161 add_ivhd_device_entries(dev, sibling, depth + 1, depth, root_level,
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200162 current, nb_bus);
Jason Gleneskf934fae2021-07-20 02:19:58 -0700163}
164
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200165static unsigned long acpi_ivhd_misc(unsigned long current, struct device *dev)
166{
167 u8 dte_setting = IVHD_DTE_LINT_1_PASS | IVHD_DTE_LINT_0_PASS |
168 IVHD_DTE_SYS_MGT_NO_TRANS | IVHD_DTE_NMI_PASS |
169 IVHD_DTE_EXT_INT_PASS | IVHD_DTE_INIT_PASS;
170 int8_t root_level = -1;
171 struct resource *res;
172
173 /*
174 * Add all possible PCI devices in the domain that can generate transactions
175 * processed by IOMMU. Start with device <bus>:01.0
176 */
177 current = ivhd_dev_range(current, PCI_DEVFN(0, 3) | (dev->link_list->secondary << 8),
178 0xff | (dev->link_list->subordinate << 8), 0);
179
180 add_ivhd_device_entries(NULL, dev, 0, -1, &root_level,
181 &current, dev->link_list->secondary);
182
Felix Heldb8b0c662023-07-20 20:43:41 +0200183 res = probe_resource(dev, IOMMU_IOAPIC_IDX);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200184 if (res) {
185 /* Describe IOAPIC associated with the IOMMU */
Arthur Heymansf9ee87f2023-06-07 15:29:02 +0200186 current = acpi_fill_ivrs_ioapic(current, (u8 *)(uintptr_t)res->base,
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200187 PCI_DEVFN(0, 1) | (dev->link_list->secondary << 8), 0);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200188 }
189
190 /* If the domain has secondary bus as zero then associate HPET & FCH IOAPIC */
191 if (dev->link_list->secondary == 0) {
192 /* Describe HPET */
193 current = ivhd_describe_hpet(current, 0x00, SMBUS_DEVFN);
194 /* Describe FCH IOAPICs */
Arthur Heymansf9ee87f2023-06-07 15:29:02 +0200195 current = acpi_fill_ivrs_ioapic(current, VIO_APIC_VADDR,
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200196 SMBUS_DEVFN, dte_setting);
197 }
198
199 return current;
200}
201
202static unsigned long acpi_fill_ivrs40(unsigned long current, acpi_ivrs_ivhd_t *ivhd,
203 struct device *nb_dev, struct device *iommu_dev)
Jason Gleneskf934fae2021-07-20 02:19:58 -0700204{
Felix Held50cbb932023-06-23 18:49:26 +0200205 acpi_ivrs_ivhd40_t *ivhd_40 = (acpi_ivrs_ivhd40_t *)current;
Jason Gleneskf934fae2021-07-20 02:19:58 -0700206 unsigned long current_backup;
Jason Gleneskf934fae2021-07-20 02:19:58 -0700207
Felix Held8cbafe82023-06-23 18:55:34 +0200208 memset(ivhd_40, 0, sizeof(*ivhd_40));
Jason Gleneskf934fae2021-07-20 02:19:58 -0700209
210 /* Enable EFR */
211 ivhd_40->type = IVHD_BLOCK_TYPE_FULL__ACPI_HID;
212 /* For type 40h bits 6 and 7 are reserved */
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200213 ivhd_40->flags = ivhd->flags & 0x3f;
Jason Gleneskf934fae2021-07-20 02:19:58 -0700214 ivhd_40->length = sizeof(struct acpi_ivrs_ivhd_40);
215 /* BDF <bus>:00.2 */
216 ivhd_40->device_id = 0x02 | (nb_dev->bus->secondary << 8);
217 ivhd_40->capability_offset = pci_find_capability(iommu_dev, IOMMU_CAP_ID);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200218 ivhd_40->iommu_base_low = ivhd->iommu_base_low;
219 ivhd_40->iommu_base_high = ivhd->iommu_base_high;
Felix Held3b5b66d2024-01-11 22:26:18 +0100220 ivhd_40->pci_segment_group = nb_dev->bus->segment_group;
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200221 ivhd_40->iommu_info = ivhd->iommu_info;
Jason Gleneskf934fae2021-07-20 02:19:58 -0700222 /* For type 40h bits 31:28 and 12:0 are reserved */
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200223 ivhd_40->iommu_attributes = ivhd->iommu_feature_info & 0xfffe000;
Jason Gleneskf934fae2021-07-20 02:19:58 -0700224
225 if (pci_read_config32(iommu_dev, ivhd_40->capability_offset) & EFR_FEATURE_SUP) {
Arthur Heymans4c684872022-04-19 21:44:22 +0200226 ivhd_40->efr_reg_image_low = read32p(ivhd_40->iommu_base_low + 0x30);
227 ivhd_40->efr_reg_image_high = read32p(ivhd_40->iommu_base_low + 0x34);
Jason Gleneskf934fae2021-07-20 02:19:58 -0700228 }
229
230 current += sizeof(acpi_ivrs_ivhd40_t);
231
232 /* Now repeat all the device entries from type 10h */
233 current_backup = current;
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200234 current = acpi_ivhd_misc(current, nb_dev->bus->dev);
Jason Gleneskf934fae2021-07-20 02:19:58 -0700235
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200236 if (nb_dev->bus->secondary == 0) {
237 /* Describe EMMC */
Felix Held47ed2712023-06-20 19:17:43 +0200238 if (CONFIG(SOC_AMD_COMMON_BLOCK_EMMC)) {
239 /* PCI_DEVFN(0x13, 1) doesn't exist in the hardware, but it's what the
240 * reference code uses. Maybe to have a unique PCI device to put into
241 * the field that doesn't collide with any existing device? */
242 current = ivhd_describe_f0_device(current, PCI_DEVFN(0x13, 1),
Felix Held534cce32023-06-22 23:09:23 +0200243 "AMDI0040",
Felix Held47ed2712023-06-20 19:17:43 +0200244 IVHD_DTE_LINT_1_PASS | IVHD_DTE_LINT_0_PASS |
245 IVHD_DTE_SYS_MGT_TRANS | IVHD_DTE_NMI_PASS |
246 IVHD_DTE_EXT_INT_PASS | IVHD_DTE_INIT_PASS);
247 }
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200248 }
Jason Gleneskf934fae2021-07-20 02:19:58 -0700249 ivhd_40->length += (current - current_backup);
250
251 return current;
252}
253
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200254static unsigned long acpi_fill_ivrs11(unsigned long current, acpi_ivrs_ivhd_t *ivhd,
255 struct device *nb_dev, struct device *iommu_dev)
Jason Gleneskf934fae2021-07-20 02:19:58 -0700256{
Felix Held50cbb932023-06-23 18:49:26 +0200257 acpi_ivrs_ivhd11_t *ivhd_11 = (acpi_ivrs_ivhd11_t *)current;
Jason Gleneskf934fae2021-07-20 02:19:58 -0700258 ivhd11_iommu_attr_t *ivhd11_attr_ptr;
259 unsigned long current_backup;
Jason Gleneskf934fae2021-07-20 02:19:58 -0700260
261 /*
262 * In order to utilize all features, firmware should expose type 11h
263 * IVHD which supersedes the type 10h.
264 */
Felix Held8cbafe82023-06-23 18:55:34 +0200265 memset(ivhd_11, 0, sizeof(*ivhd_11));
Jason Gleneskf934fae2021-07-20 02:19:58 -0700266
267 /* Enable EFR */
268 ivhd_11->type = IVHD_BLOCK_TYPE_FULL__FIXED;
269 /* For type 11h bits 6 and 7 are reserved */
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200270 ivhd_11->flags = ivhd->flags & 0x3f;
Jason Gleneskf934fae2021-07-20 02:19:58 -0700271 ivhd_11->length = sizeof(struct acpi_ivrs_ivhd_11);
272 /* BDF <bus>:00.2 */
273 ivhd_11->device_id = 0x02 | (nb_dev->bus->secondary << 8);
274 ivhd_11->capability_offset = pci_find_capability(iommu_dev, IOMMU_CAP_ID);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200275 ivhd_11->iommu_base_low = ivhd->iommu_base_low;
276 ivhd_11->iommu_base_high = ivhd->iommu_base_high;
Felix Held3b5b66d2024-01-11 22:26:18 +0100277 ivhd_11->pci_segment_group = nb_dev->bus->segment_group;
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200278 ivhd_11->iommu_info = ivhd->iommu_info;
279 ivhd11_attr_ptr = (ivhd11_iommu_attr_t *)&ivhd->iommu_feature_info;
Jason Gleneskf934fae2021-07-20 02:19:58 -0700280 ivhd_11->iommu_attributes.perf_counters = ivhd11_attr_ptr->perf_counters;
281 ivhd_11->iommu_attributes.perf_counter_banks = ivhd11_attr_ptr->perf_counter_banks;
282 ivhd_11->iommu_attributes.msi_num_ppr = ivhd11_attr_ptr->msi_num_ppr;
283
284 if (pci_read_config32(iommu_dev, ivhd_11->capability_offset) & EFR_FEATURE_SUP) {
Arthur Heymans4c684872022-04-19 21:44:22 +0200285 ivhd_11->efr_reg_image_low = read32p(ivhd_11->iommu_base_low + 0x30);
286 ivhd_11->efr_reg_image_high = read32p(ivhd_11->iommu_base_low + 0x34);
Jason Gleneskf934fae2021-07-20 02:19:58 -0700287 }
288
289 current += sizeof(acpi_ivrs_ivhd11_t);
290
291 /* Now repeat all the device entries from type 10h */
292 current_backup = current;
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200293 current = acpi_ivhd_misc(current, nb_dev->bus->dev);
Jason Gleneskf934fae2021-07-20 02:19:58 -0700294 ivhd_11->length += (current - current_backup);
295
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200296 return acpi_fill_ivrs40(current, ivhd, nb_dev, iommu_dev);
Jason Gleneskf934fae2021-07-20 02:19:58 -0700297}
298
Felix Helda4a2a4f2024-01-25 18:53:54 +0100299static unsigned long acpi_fill_ivrs(acpi_ivrs_t *ivrs, unsigned long current)
Jason Gleneskf934fae2021-07-20 02:19:58 -0700300{
301 unsigned long current_backup;
302 uint64_t mmio_x30_value;
303 uint64_t mmio_x18_value;
304 uint64_t mmio_x4000_value;
305 uint32_t cap_offset_0;
306 uint32_t cap_offset_10;
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200307 struct acpi_ivrs_ivhd *ivhd;
Jason Gleneskf934fae2021-07-20 02:19:58 -0700308 struct device *iommu_dev;
309 struct device *nb_dev;
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200310 struct device *dev = NULL;
Jason Gleneskf934fae2021-07-20 02:19:58 -0700311
Naresh Solanki08601b22023-05-31 17:13:03 +0200312 if (ivrs == NULL) {
313 printk(BIOS_WARNING, "%s: ivrs is NULL\n", __func__);
314 return current;
315 }
316
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200317 ivhd = &ivrs->ivhd;
Jason Gleneskf934fae2021-07-20 02:19:58 -0700318
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200319 while ((dev = dev_find_path(dev, DEVICE_PATH_DOMAIN)) != NULL) {
Jason Gleneskf934fae2021-07-20 02:19:58 -0700320
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200321 nb_dev = pcidev_path_behind(dev->link_list, PCI_DEVFN(0, 0));
322 iommu_dev = pcidev_path_behind(dev->link_list, PCI_DEVFN(0, 2));
323 if (!nb_dev) {
324 printk(BIOS_WARNING, "%s: Northbridge device not present!\n", __func__);
325 printk(BIOS_WARNING, "%s: IVRS table not generated...\n", __func__);
326 return (unsigned long)ivrs;
327 }
Jason Gleneskf934fae2021-07-20 02:19:58 -0700328
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200329 if (!iommu_dev) {
330 printk(BIOS_WARNING, "%s: IOMMU device not found\n", __func__);
331 return (unsigned long)ivrs;
332 }
Jason Gleneskf934fae2021-07-20 02:19:58 -0700333
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200334 ivhd->type = IVHD_BLOCK_TYPE_LEGACY__FIXED;
335 ivhd->length = sizeof(struct acpi_ivrs_ivhd);
Jason Gleneskf934fae2021-07-20 02:19:58 -0700336
337 /* BDF <bus>:00.2 */
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200338 ivhd->device_id = 0x02 | (nb_dev->bus->secondary << 8);
339 ivhd->capability_offset = pci_find_capability(iommu_dev, IOMMU_CAP_ID);
Felix Heldc32df9a2023-07-03 18:52:36 +0200340 ivhd->iommu_base_low = pci_read_config32(iommu_dev, IOMMU_CAP_BASE_LO) & 0xffffc000;
341 ivhd->iommu_base_high = pci_read_config32(iommu_dev, IOMMU_CAP_BASE_HI);
Jason Gleneskf934fae2021-07-20 02:19:58 -0700342
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200343 cap_offset_0 = pci_read_config32(iommu_dev, ivhd->capability_offset);
Jason Gleneskf934fae2021-07-20 02:19:58 -0700344 cap_offset_10 = pci_read_config32(iommu_dev,
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200345 ivhd->capability_offset + 0x10);
346 mmio_x18_value = read64p(ivhd->iommu_base_low + 0x18);
347 mmio_x30_value = read64p(ivhd->iommu_base_low + 0x30);
348 mmio_x4000_value = read64p(ivhd->iommu_base_low + 0x4000);
Jason Gleneskf934fae2021-07-20 02:19:58 -0700349
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200350 ivhd->flags |= ((mmio_x30_value & MMIO_EXT_FEATURE_PPR_SUP) ?
Jason Gleneskf934fae2021-07-20 02:19:58 -0700351 IVHD_FLAG_PPE_SUP : 0);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200352 ivhd->flags |= ((mmio_x30_value & MMIO_EXT_FEATURE_PRE_F_SUP) ?
Jason Gleneskf934fae2021-07-20 02:19:58 -0700353 IVHD_FLAG_PREF_SUP : 0);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200354 ivhd->flags |= ((mmio_x18_value & MMIO_CTRL_COHERENT) ?
Jason Gleneskf934fae2021-07-20 02:19:58 -0700355 IVHD_FLAG_COHERENT : 0);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200356 ivhd->flags |= ((cap_offset_0 & CAP_OFFSET_0_IOTLB_SP) ?
Jason Gleneskf934fae2021-07-20 02:19:58 -0700357 IVHD_FLAG_IOTLB_SUP : 0);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200358 ivhd->flags |= ((mmio_x18_value & MMIO_CTRL_ISOC) ?
Jason Gleneskf934fae2021-07-20 02:19:58 -0700359 IVHD_FLAG_ISOC : 0);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200360 ivhd->flags |= ((mmio_x18_value & MMIO_CTRL_RES_PASS_PW) ?
Jason Gleneskf934fae2021-07-20 02:19:58 -0700361 IVHD_FLAG_RES_PASS_PW : 0);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200362 ivhd->flags |= ((mmio_x18_value & MMIO_CTRL_PASS_PW) ?
Jason Gleneskf934fae2021-07-20 02:19:58 -0700363 IVHD_FLAG_PASS_PW : 0);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200364 ivhd->flags |= ((mmio_x18_value & MMIO_CTRL_HT_TUN_EN) ?
Jason Gleneskf934fae2021-07-20 02:19:58 -0700365 IVHD_FLAG_HT_TUN_EN : 0);
366
Felix Held3b5b66d2024-01-11 22:26:18 +0100367 ivhd->pci_segment_group = nb_dev->bus->segment_group;
Jason Gleneskf934fae2021-07-20 02:19:58 -0700368
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200369 ivhd->iommu_info = pci_read_config16(iommu_dev,
370 ivhd->capability_offset + 0x10) & 0x1F;
371 ivhd->iommu_info |= (pci_read_config16(iommu_dev,
372 ivhd->capability_offset + 0xC) & 0x1F) << IOMMU_INFO_UNIT_ID_SHIFT;
Jason Gleneskf934fae2021-07-20 02:19:58 -0700373
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200374 ivhd->iommu_feature_info = 0;
375 ivhd->iommu_feature_info |= (mmio_x30_value & MMIO_EXT_FEATURE_HATS_MASK)
Jason Gleneskf934fae2021-07-20 02:19:58 -0700376 << (IOMMU_FEATURE_HATS_SHIFT - MMIO_EXT_FEATURE_HATS_SHIFT);
377
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200378 ivhd->iommu_feature_info |= (mmio_x30_value & MMIO_EXT_FEATURE_GATS_MASK)
Jason Gleneskf934fae2021-07-20 02:19:58 -0700379 << (IOMMU_FEATURE_GATS_SHIFT - MMIO_EXT_FEATURE_GATS_SHIFT);
380
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200381 ivhd->iommu_feature_info |= (cap_offset_10 & CAP_OFFSET_10_MSI_NUM_PPR)
Jason Gleneskf934fae2021-07-20 02:19:58 -0700382 >> (CAP_OFFSET_10_MSI_NUM_PPR_SHIFT
383 - IOMMU_FEATURE_MSI_NUM_PPR_SHIFT);
384
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200385 ivhd->iommu_feature_info |= (mmio_x4000_value &
Jason Gleneskf934fae2021-07-20 02:19:58 -0700386 MMIO_CNT_CFG_N_COUNTER_BANKS)
387 << (IOMMU_FEATURE_PN_BANKS_SHIFT - MMIO_CNT_CFG_N_CNT_BANKS_SHIFT);
388
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200389 ivhd->iommu_feature_info |= (mmio_x4000_value & MMIO_CNT_CFG_N_COUNTER)
Jason Gleneskf934fae2021-07-20 02:19:58 -0700390 << (IOMMU_FEATURE_PN_COUNTERS_SHIFT - MMIO_CNT_CFG_N_COUNTER_SHIFT);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200391 ivhd->iommu_feature_info |= (mmio_x30_value &
Jason Gleneskf934fae2021-07-20 02:19:58 -0700392 MMIO_EXT_FEATURE_PAS_MAX_MASK)
393 >> (MMIO_EXT_FEATURE_PAS_MAX_SHIFT - IOMMU_FEATURE_PA_SMAX_SHIFT);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200394 ivhd->iommu_feature_info |= ((mmio_x30_value & MMIO_EXT_FEATURE_HE_SUP)
Jason Gleneskf934fae2021-07-20 02:19:58 -0700395 ? IOMMU_FEATURE_HE_SUP : 0);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200396 ivhd->iommu_feature_info |= ((mmio_x30_value & MMIO_EXT_FEATURE_GA_SUP)
Jason Gleneskf934fae2021-07-20 02:19:58 -0700397 ? IOMMU_FEATURE_GA_SUP : 0);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200398 ivhd->iommu_feature_info |= ((mmio_x30_value & MMIO_EXT_FEATURE_IA_SUP)
Jason Gleneskf934fae2021-07-20 02:19:58 -0700399 ? IOMMU_FEATURE_IA_SUP : 0);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200400 ivhd->iommu_feature_info |= (mmio_x30_value &
Jason Gleneskf934fae2021-07-20 02:19:58 -0700401 MMIO_EXT_FEATURE_GLX_SUP_MASK)
402 >> (MMIO_EXT_FEATURE_GLX_SHIFT - IOMMU_FEATURE_GLX_SHIFT);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200403 ivhd->iommu_feature_info |= ((mmio_x30_value & MMIO_EXT_FEATURE_GT_SUP)
Jason Gleneskf934fae2021-07-20 02:19:58 -0700404 ? IOMMU_FEATURE_GT_SUP : 0);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200405 ivhd->iommu_feature_info |= ((mmio_x30_value & MMIO_EXT_FEATURE_NX_SUP)
Jason Gleneskf934fae2021-07-20 02:19:58 -0700406 ? IOMMU_FEATURE_NX_SUP : 0);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200407 ivhd->iommu_feature_info |= ((mmio_x30_value & MMIO_EXT_FEATURE_XT_SUP)
Jason Gleneskf934fae2021-07-20 02:19:58 -0700408 ? IOMMU_FEATURE_XT_SUP : 0);
409
410 /* Enable EFR if supported */
411 ivrs->iv_info = pci_read_config32(iommu_dev,
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200412 ivhd->capability_offset + 0x10) & 0x007fffe0;
Jason Gleneskf934fae2021-07-20 02:19:58 -0700413 if (pci_read_config32(iommu_dev,
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200414 ivhd->capability_offset) & EFR_FEATURE_SUP)
Jason Gleneskf934fae2021-07-20 02:19:58 -0700415 ivrs->iv_info |= IVINFO_EFR_SUPPORTED;
416
Jason Gleneskf934fae2021-07-20 02:19:58 -0700417
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200418 current_backup = current;
419 current = acpi_ivhd_misc(current, dev);
420 ivhd->length += (current - current_backup);
421
422 /* If EFR is not supported, IVHD type 11h is reserved */
423 if (!(ivrs->iv_info & IVINFO_EFR_SUPPORTED))
424 return current;
425
426 current = acpi_fill_ivrs11(current, ivhd, nb_dev, iommu_dev);
427
428 ivhd = (struct acpi_ivrs_ivhd *)current;
429 current += sizeof(struct acpi_ivrs_ivhd);
Jason Gleneskf934fae2021-07-20 02:19:58 -0700430 }
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200431 current -= sizeof(struct acpi_ivrs_ivhd);
Jason Gleneskf934fae2021-07-20 02:19:58 -0700432
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200433 return current;
Jason Gleneskf934fae2021-07-20 02:19:58 -0700434}
Felix Helda4a2a4f2024-01-25 18:53:54 +0100435
436unsigned long acpi_add_ivrs_table(unsigned long current, acpi_rsdp_t *rsdp)
437{
438 acpi_ivrs_t *ivrs;
439
440 current = acpi_align_current(current);
441 ivrs = (acpi_ivrs_t *)current;
442 acpi_create_ivrs(ivrs, acpi_fill_ivrs);
443 current += ivrs->header.length;
444 acpi_add_table(rsdp, ivrs);
445
446 return current;
447}