blob: 814f514eef2ca5acb1762a446b4b8defb2052ef4 [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>
6#include <amdblocks/data_fabric.h>
7#include <amdblocks/ioapic.h>
Felix Heldc32df9a2023-07-03 18:52:36 +02008#include <amdblocks/iommu.h>
Felix Held96fa6a22022-12-06 01:25:13 +01009#include <arch/ioapic.h>
Jason Gleneskf934fae2021-07-20 02:19:58 -070010#include <console/console.h>
11#include <cpu/amd/cpuid.h>
Jason Gleneskf934fae2021-07-20 02:19:58 -070012#include <device/device.h>
Elyes Haouas8823ba12022-12-05 08:48:50 +010013#include <device/mmio.h>
Jason Gleneskf934fae2021-07-20 02:19:58 -070014#include <device/pci_def.h>
15#include <device/pci_ops.h>
16#include <soc/acpi.h>
17#include <soc/data_fabric.h>
Felix Held96fa6a22022-12-06 01:25:13 +010018#include <soc/iomap.h>
Jason Gleneskf934fae2021-07-20 02:19:58 -070019#include <soc/pci_devs.h>
Jason Gleneskf934fae2021-07-20 02:19:58 -070020
Arthur Heymansf9ee87f2023-06-07 15:29:02 +020021static unsigned long acpi_fill_ivrs_ioapic(unsigned long current, void *ioapic_base,
Naresh Solanki4ef89f72023-05-25 17:37:50 +020022 uint16_t src_devid, uint8_t dte_setting)
Jason Gleneskf934fae2021-07-20 02:19:58 -070023{
24 ivrs_ivhd_special_t *ivhd_ioapic = (ivrs_ivhd_special_t *)current;
25 memset(ivhd_ioapic, 0, sizeof(*ivhd_ioapic));
26
27 ivhd_ioapic->type = IVHD_DEV_8_BYTE_EXT_SPECIAL_DEV;
Naresh Solanki4ef89f72023-05-25 17:37:50 +020028 ivhd_ioapic->dte_setting = dte_setting;
29 ivhd_ioapic->handle = get_ioapic_id(ioapic_base);
30 ivhd_ioapic->source_dev_id = src_devid;
Jason Gleneskf934fae2021-07-20 02:19:58 -070031 ivhd_ioapic->variety = IVHD_SPECIAL_DEV_IOAPIC;
32 current += sizeof(ivrs_ivhd_special_t);
33
34 return current;
35}
36
Naresh Solanki4ef89f72023-05-25 17:37:50 +020037static unsigned long ivhd_describe_hpet(unsigned long current, uint8_t hndl, uint16_t src_devid)
Jason Gleneskf934fae2021-07-20 02:19:58 -070038{
39 ivrs_ivhd_special_t *ivhd_hpet = (ivrs_ivhd_special_t *)current;
Felix Held56167c52023-06-22 23:22:19 +020040 memset(ivhd_hpet, 0, sizeof(*ivhd_hpet));
Jason Gleneskf934fae2021-07-20 02:19:58 -070041
42 ivhd_hpet->type = IVHD_DEV_8_BYTE_EXT_SPECIAL_DEV;
Naresh Solanki4ef89f72023-05-25 17:37:50 +020043 ivhd_hpet->handle = hndl;
44 ivhd_hpet->source_dev_id = src_devid; /* function 0 of FCH PCI device */
Jason Gleneskf934fae2021-07-20 02:19:58 -070045 ivhd_hpet->variety = IVHD_SPECIAL_DEV_HPET;
46 current += sizeof(ivrs_ivhd_special_t);
47
48 return current;
49}
50
Felix Held534cce32023-06-22 23:09:23 +020051static unsigned long ivhd_describe_f0_device(unsigned long current, uint16_t dev_id,
52 const char acpi_hid[8], uint8_t datasetting)
Jason Gleneskf934fae2021-07-20 02:19:58 -070053{
Elyes Haouas68fc51f2022-07-16 09:48:27 +020054 ivrs_ivhd_f0_entry_t *ivhd_f0 = (ivrs_ivhd_f0_entry_t *)current;
Felix Held63a4e6bd2023-06-22 23:04:19 +020055 memset(ivhd_f0, 0, sizeof(*ivhd_f0));
Jason Gleneskf934fae2021-07-20 02:19:58 -070056
57 ivhd_f0->type = IVHD_DEV_VARIABLE;
58 ivhd_f0->dev_id = dev_id;
59 ivhd_f0->dte_setting = datasetting;
Felix Held534cce32023-06-22 23:09:23 +020060
61 memcpy(ivhd_f0->hardware_id, acpi_hid, sizeof(ivhd_f0->hardware_id));
Jason Gleneskf934fae2021-07-20 02:19:58 -070062
Jason Gleneskf934fae2021-07-20 02:19:58 -070063 current += sizeof(ivrs_ivhd_f0_entry_t);
64 return current;
65}
66
67static unsigned long ivhd_dev_range(unsigned long current, uint16_t start_devid,
68 uint16_t end_devid, uint8_t setting)
69{
70 /* 4-byte IVHD structures must be aligned to the 4-byte boundary. */
71 current = ALIGN_UP(current, 4);
72 ivrs_ivhd_generic_t *ivhd_range = (ivrs_ivhd_generic_t *)current;
Felix Heldfe242ce2023-06-23 19:03:57 +020073 memset(ivhd_range, 0, sizeof(*ivhd_range));
Jason Gleneskf934fae2021-07-20 02:19:58 -070074
75 /* Create the start range IVHD entry */
76 ivhd_range->type = IVHD_DEV_4_BYTE_START_RANGE;
77 ivhd_range->dev_id = start_devid;
78 ivhd_range->dte_setting = setting;
79 current += sizeof(ivrs_ivhd_generic_t);
80
81 /* Create the end range IVHD entry */
82 ivhd_range = (ivrs_ivhd_generic_t *)current;
83 ivhd_range->type = IVHD_DEV_4_BYTE_END_RANGE;
84 ivhd_range->dev_id = end_devid;
85 ivhd_range->dte_setting = setting;
86 current += sizeof(ivrs_ivhd_generic_t);
87
88 return current;
89}
90
91static unsigned long add_ivhd_dev_entry(struct device *parent, struct device *dev,
92 unsigned long *current, uint8_t type, uint8_t data)
93{
94 if (type == IVHD_DEV_4_BYTE_SELECT) {
95 /* 4-byte IVHD structures must be aligned to the 4-byte boundary. */
96 *current = ALIGN_UP(*current, 4);
97 ivrs_ivhd_generic_t *ivhd_entry = (ivrs_ivhd_generic_t *)*current;
Felix Heldfe242ce2023-06-23 19:03:57 +020098 memset(ivhd_entry, 0, sizeof(*ivhd_entry));
Jason Gleneskf934fae2021-07-20 02:19:58 -070099
100 ivhd_entry->type = type;
101 ivhd_entry->dev_id = dev->path.pci.devfn | (dev->bus->secondary << 8);
102 ivhd_entry->dte_setting = data;
103 *current += sizeof(ivrs_ivhd_generic_t);
104 } else if (type == IVHD_DEV_8_BYTE_ALIAS_SELECT) {
105 ivrs_ivhd_alias_t *ivhd_entry = (ivrs_ivhd_alias_t *)*current;
Felix Heldfe242ce2023-06-23 19:03:57 +0200106 memset(ivhd_entry, 0, sizeof(*ivhd_entry));
Jason Gleneskf934fae2021-07-20 02:19:58 -0700107
108 ivhd_entry->type = type;
109 ivhd_entry->dev_id = dev->path.pci.devfn | (dev->bus->secondary << 8);
110 ivhd_entry->dte_setting = data;
111 ivhd_entry->reserved1 = 0;
112 ivhd_entry->reserved2 = 0;
113 ivhd_entry->source_dev_id = parent->path.pci.devfn |
114 (parent->bus->secondary << 8);
115 *current += sizeof(ivrs_ivhd_alias_t);
116 }
117
118 return *current;
119}
120
121static void ivrs_add_device_or_bridge(struct device *parent, struct device *dev,
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200122 unsigned long *current)
Jason Gleneskf934fae2021-07-20 02:19:58 -0700123{
124 unsigned int header_type, is_pcie;
Jason Gleneskf934fae2021-07-20 02:19:58 -0700125
126 header_type = dev->hdr_type & 0x7f;
127 is_pcie = pci_find_capability(dev, PCI_CAP_ID_PCIE);
128
129 if (((header_type == PCI_HEADER_TYPE_NORMAL) ||
130 (header_type == PCI_HEADER_TYPE_BRIDGE)) && is_pcie) {
131 /* Device or Bridge is PCIe */
Jason Gleneskf934fae2021-07-20 02:19:58 -0700132 add_ivhd_dev_entry(parent, dev, current, IVHD_DEV_4_BYTE_SELECT, 0x0);
Jason Gleneskf934fae2021-07-20 02:19:58 -0700133 } else if ((header_type == PCI_HEADER_TYPE_NORMAL) && !is_pcie) {
134 /* Device is legacy PCI or PCI-X */
Jason Gleneskf934fae2021-07-20 02:19:58 -0700135 add_ivhd_dev_entry(parent, dev, current, IVHD_DEV_8_BYTE_ALIAS_SELECT, 0x0);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200136
Jason Gleneskf934fae2021-07-20 02:19:58 -0700137 }
138}
139
140static void add_ivhd_device_entries(struct device *parent, struct device *dev,
141 unsigned int depth, int linknum, int8_t *root_level,
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200142 unsigned long *current, uint16_t nb_bus)
Jason Gleneskf934fae2021-07-20 02:19:58 -0700143{
144 struct device *sibling;
145 struct bus *link;
146
147 if (!root_level)
148 return;
149
150 if (dev->path.type == DEVICE_PATH_PCI) {
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200151 if ((dev->bus->secondary == nb_bus) &&
Jason Gleneskf934fae2021-07-20 02:19:58 -0700152 (dev->path.pci.devfn == 0x0))
153 *root_level = depth;
154
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200155 if ((*root_level != -1) && (dev->enabled))
Jason Gleneskf934fae2021-07-20 02:19:58 -0700156 if (depth != *root_level)
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200157 ivrs_add_device_or_bridge(parent, dev, current);
Jason Gleneskf934fae2021-07-20 02:19:58 -0700158 }
159
160 for (link = dev->link_list; link; link = link->next)
161 for (sibling = link->children; sibling; sibling =
162 sibling->sibling)
163 add_ivhd_device_entries(dev, sibling, depth + 1, depth, root_level,
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200164 current, nb_bus);
Jason Gleneskf934fae2021-07-20 02:19:58 -0700165}
166
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200167static unsigned long acpi_ivhd_misc(unsigned long current, struct device *dev)
168{
169 u8 dte_setting = IVHD_DTE_LINT_1_PASS | IVHD_DTE_LINT_0_PASS |
170 IVHD_DTE_SYS_MGT_NO_TRANS | IVHD_DTE_NMI_PASS |
171 IVHD_DTE_EXT_INT_PASS | IVHD_DTE_INIT_PASS;
172 int8_t root_level = -1;
173 struct resource *res;
174
175 /*
176 * Add all possible PCI devices in the domain that can generate transactions
177 * processed by IOMMU. Start with device <bus>:01.0
178 */
179 current = ivhd_dev_range(current, PCI_DEVFN(0, 3) | (dev->link_list->secondary << 8),
180 0xff | (dev->link_list->subordinate << 8), 0);
181
182 add_ivhd_device_entries(NULL, dev, 0, -1, &root_level,
183 &current, dev->link_list->secondary);
184
Felix Heldb8b0c662023-07-20 20:43:41 +0200185 res = probe_resource(dev, IOMMU_IOAPIC_IDX);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200186 if (res) {
187 /* Describe IOAPIC associated with the IOMMU */
Arthur Heymansf9ee87f2023-06-07 15:29:02 +0200188 current = acpi_fill_ivrs_ioapic(current, (u8 *)(uintptr_t)res->base,
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200189 PCI_DEVFN(0, 1) | (dev->link_list->secondary << 8), 0);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200190 }
191
192 /* If the domain has secondary bus as zero then associate HPET & FCH IOAPIC */
193 if (dev->link_list->secondary == 0) {
194 /* Describe HPET */
195 current = ivhd_describe_hpet(current, 0x00, SMBUS_DEVFN);
196 /* Describe FCH IOAPICs */
Arthur Heymansf9ee87f2023-06-07 15:29:02 +0200197 current = acpi_fill_ivrs_ioapic(current, VIO_APIC_VADDR,
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200198 SMBUS_DEVFN, dte_setting);
199 }
200
201 return current;
202}
203
204static unsigned long acpi_fill_ivrs40(unsigned long current, acpi_ivrs_ivhd_t *ivhd,
205 struct device *nb_dev, struct device *iommu_dev)
Jason Gleneskf934fae2021-07-20 02:19:58 -0700206{
Felix Held50cbb932023-06-23 18:49:26 +0200207 acpi_ivrs_ivhd40_t *ivhd_40 = (acpi_ivrs_ivhd40_t *)current;
Jason Gleneskf934fae2021-07-20 02:19:58 -0700208 unsigned long current_backup;
Jason Gleneskf934fae2021-07-20 02:19:58 -0700209
Felix Held8cbafe82023-06-23 18:55:34 +0200210 memset(ivhd_40, 0, sizeof(*ivhd_40));
Jason Gleneskf934fae2021-07-20 02:19:58 -0700211
212 /* Enable EFR */
213 ivhd_40->type = IVHD_BLOCK_TYPE_FULL__ACPI_HID;
214 /* For type 40h bits 6 and 7 are reserved */
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200215 ivhd_40->flags = ivhd->flags & 0x3f;
Jason Gleneskf934fae2021-07-20 02:19:58 -0700216 ivhd_40->length = sizeof(struct acpi_ivrs_ivhd_40);
217 /* BDF <bus>:00.2 */
218 ivhd_40->device_id = 0x02 | (nb_dev->bus->secondary << 8);
219 ivhd_40->capability_offset = pci_find_capability(iommu_dev, IOMMU_CAP_ID);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200220 ivhd_40->iommu_base_low = ivhd->iommu_base_low;
221 ivhd_40->iommu_base_high = ivhd->iommu_base_high;
Jason Gleneskf934fae2021-07-20 02:19:58 -0700222 ivhd_40->pci_segment_group = 0x0000;
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200223 ivhd_40->iommu_info = ivhd->iommu_info;
Jason Gleneskf934fae2021-07-20 02:19:58 -0700224 /* For type 40h bits 31:28 and 12:0 are reserved */
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200225 ivhd_40->iommu_attributes = ivhd->iommu_feature_info & 0xfffe000;
Jason Gleneskf934fae2021-07-20 02:19:58 -0700226
227 if (pci_read_config32(iommu_dev, ivhd_40->capability_offset) & EFR_FEATURE_SUP) {
Arthur Heymans4c684872022-04-19 21:44:22 +0200228 ivhd_40->efr_reg_image_low = read32p(ivhd_40->iommu_base_low + 0x30);
229 ivhd_40->efr_reg_image_high = read32p(ivhd_40->iommu_base_low + 0x34);
Jason Gleneskf934fae2021-07-20 02:19:58 -0700230 }
231
232 current += sizeof(acpi_ivrs_ivhd40_t);
233
234 /* Now repeat all the device entries from type 10h */
235 current_backup = current;
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200236 current = acpi_ivhd_misc(current, nb_dev->bus->dev);
Jason Gleneskf934fae2021-07-20 02:19:58 -0700237
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200238 if (nb_dev->bus->secondary == 0) {
239 /* Describe EMMC */
Felix Held47ed2712023-06-20 19:17:43 +0200240 if (CONFIG(SOC_AMD_COMMON_BLOCK_EMMC)) {
241 /* PCI_DEVFN(0x13, 1) doesn't exist in the hardware, but it's what the
242 * reference code uses. Maybe to have a unique PCI device to put into
243 * the field that doesn't collide with any existing device? */
244 current = ivhd_describe_f0_device(current, PCI_DEVFN(0x13, 1),
Felix Held534cce32023-06-22 23:09:23 +0200245 "AMDI0040",
Felix Held47ed2712023-06-20 19:17:43 +0200246 IVHD_DTE_LINT_1_PASS | IVHD_DTE_LINT_0_PASS |
247 IVHD_DTE_SYS_MGT_TRANS | IVHD_DTE_NMI_PASS |
248 IVHD_DTE_EXT_INT_PASS | IVHD_DTE_INIT_PASS);
249 }
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200250 }
Jason Gleneskf934fae2021-07-20 02:19:58 -0700251 ivhd_40->length += (current - current_backup);
252
253 return current;
254}
255
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200256static unsigned long acpi_fill_ivrs11(unsigned long current, acpi_ivrs_ivhd_t *ivhd,
257 struct device *nb_dev, struct device *iommu_dev)
Jason Gleneskf934fae2021-07-20 02:19:58 -0700258{
Felix Held50cbb932023-06-23 18:49:26 +0200259 acpi_ivrs_ivhd11_t *ivhd_11 = (acpi_ivrs_ivhd11_t *)current;
Jason Gleneskf934fae2021-07-20 02:19:58 -0700260 ivhd11_iommu_attr_t *ivhd11_attr_ptr;
261 unsigned long current_backup;
Jason Gleneskf934fae2021-07-20 02:19:58 -0700262
263 /*
264 * In order to utilize all features, firmware should expose type 11h
265 * IVHD which supersedes the type 10h.
266 */
Felix Held8cbafe82023-06-23 18:55:34 +0200267 memset(ivhd_11, 0, sizeof(*ivhd_11));
Jason Gleneskf934fae2021-07-20 02:19:58 -0700268
269 /* Enable EFR */
270 ivhd_11->type = IVHD_BLOCK_TYPE_FULL__FIXED;
271 /* For type 11h bits 6 and 7 are reserved */
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200272 ivhd_11->flags = ivhd->flags & 0x3f;
Jason Gleneskf934fae2021-07-20 02:19:58 -0700273 ivhd_11->length = sizeof(struct acpi_ivrs_ivhd_11);
274 /* BDF <bus>:00.2 */
275 ivhd_11->device_id = 0x02 | (nb_dev->bus->secondary << 8);
276 ivhd_11->capability_offset = pci_find_capability(iommu_dev, IOMMU_CAP_ID);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200277 ivhd_11->iommu_base_low = ivhd->iommu_base_low;
278 ivhd_11->iommu_base_high = ivhd->iommu_base_high;
Jason Gleneskf934fae2021-07-20 02:19:58 -0700279 ivhd_11->pci_segment_group = 0x0000;
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200280 ivhd_11->iommu_info = ivhd->iommu_info;
281 ivhd11_attr_ptr = (ivhd11_iommu_attr_t *)&ivhd->iommu_feature_info;
Jason Gleneskf934fae2021-07-20 02:19:58 -0700282 ivhd_11->iommu_attributes.perf_counters = ivhd11_attr_ptr->perf_counters;
283 ivhd_11->iommu_attributes.perf_counter_banks = ivhd11_attr_ptr->perf_counter_banks;
284 ivhd_11->iommu_attributes.msi_num_ppr = ivhd11_attr_ptr->msi_num_ppr;
285
286 if (pci_read_config32(iommu_dev, ivhd_11->capability_offset) & EFR_FEATURE_SUP) {
Arthur Heymans4c684872022-04-19 21:44:22 +0200287 ivhd_11->efr_reg_image_low = read32p(ivhd_11->iommu_base_low + 0x30);
288 ivhd_11->efr_reg_image_high = read32p(ivhd_11->iommu_base_low + 0x34);
Jason Gleneskf934fae2021-07-20 02:19:58 -0700289 }
290
291 current += sizeof(acpi_ivrs_ivhd11_t);
292
293 /* Now repeat all the device entries from type 10h */
294 current_backup = current;
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200295 current = acpi_ivhd_misc(current, nb_dev->bus->dev);
Jason Gleneskf934fae2021-07-20 02:19:58 -0700296 ivhd_11->length += (current - current_backup);
297
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200298 return acpi_fill_ivrs40(current, ivhd, nb_dev, iommu_dev);
Jason Gleneskf934fae2021-07-20 02:19:58 -0700299}
300
301unsigned long acpi_fill_ivrs(acpi_ivrs_t *ivrs, unsigned long current)
302{
303 unsigned long current_backup;
304 uint64_t mmio_x30_value;
305 uint64_t mmio_x18_value;
306 uint64_t mmio_x4000_value;
307 uint32_t cap_offset_0;
308 uint32_t cap_offset_10;
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200309 struct acpi_ivrs_ivhd *ivhd;
Jason Gleneskf934fae2021-07-20 02:19:58 -0700310 struct device *iommu_dev;
311 struct device *nb_dev;
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200312 struct device *dev = NULL;
Jason Gleneskf934fae2021-07-20 02:19:58 -0700313
Naresh Solanki08601b22023-05-31 17:13:03 +0200314 if (ivrs == NULL) {
315 printk(BIOS_WARNING, "%s: ivrs is NULL\n", __func__);
316 return current;
317 }
318
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200319 ivhd = &ivrs->ivhd;
Jason Gleneskf934fae2021-07-20 02:19:58 -0700320
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200321 while ((dev = dev_find_path(dev, DEVICE_PATH_DOMAIN)) != NULL) {
Jason Gleneskf934fae2021-07-20 02:19:58 -0700322
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200323 nb_dev = pcidev_path_behind(dev->link_list, PCI_DEVFN(0, 0));
324 iommu_dev = pcidev_path_behind(dev->link_list, PCI_DEVFN(0, 2));
325 if (!nb_dev) {
326 printk(BIOS_WARNING, "%s: Northbridge device not present!\n", __func__);
327 printk(BIOS_WARNING, "%s: IVRS table not generated...\n", __func__);
328 return (unsigned long)ivrs;
329 }
Jason Gleneskf934fae2021-07-20 02:19:58 -0700330
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200331 if (!iommu_dev) {
332 printk(BIOS_WARNING, "%s: IOMMU device not found\n", __func__);
333 return (unsigned long)ivrs;
334 }
Jason Gleneskf934fae2021-07-20 02:19:58 -0700335
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200336 ivhd->type = IVHD_BLOCK_TYPE_LEGACY__FIXED;
337 ivhd->length = sizeof(struct acpi_ivrs_ivhd);
Jason Gleneskf934fae2021-07-20 02:19:58 -0700338
339 /* BDF <bus>:00.2 */
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200340 ivhd->device_id = 0x02 | (nb_dev->bus->secondary << 8);
341 ivhd->capability_offset = pci_find_capability(iommu_dev, IOMMU_CAP_ID);
Felix Heldc32df9a2023-07-03 18:52:36 +0200342 ivhd->iommu_base_low = pci_read_config32(iommu_dev, IOMMU_CAP_BASE_LO) & 0xffffc000;
343 ivhd->iommu_base_high = pci_read_config32(iommu_dev, IOMMU_CAP_BASE_HI);
Jason Gleneskf934fae2021-07-20 02:19:58 -0700344
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200345 cap_offset_0 = pci_read_config32(iommu_dev, ivhd->capability_offset);
Jason Gleneskf934fae2021-07-20 02:19:58 -0700346 cap_offset_10 = pci_read_config32(iommu_dev,
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200347 ivhd->capability_offset + 0x10);
348 mmio_x18_value = read64p(ivhd->iommu_base_low + 0x18);
349 mmio_x30_value = read64p(ivhd->iommu_base_low + 0x30);
350 mmio_x4000_value = read64p(ivhd->iommu_base_low + 0x4000);
Jason Gleneskf934fae2021-07-20 02:19:58 -0700351
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200352 ivhd->flags |= ((mmio_x30_value & MMIO_EXT_FEATURE_PPR_SUP) ?
Jason Gleneskf934fae2021-07-20 02:19:58 -0700353 IVHD_FLAG_PPE_SUP : 0);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200354 ivhd->flags |= ((mmio_x30_value & MMIO_EXT_FEATURE_PRE_F_SUP) ?
Jason Gleneskf934fae2021-07-20 02:19:58 -0700355 IVHD_FLAG_PREF_SUP : 0);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200356 ivhd->flags |= ((mmio_x18_value & MMIO_CTRL_COHERENT) ?
Jason Gleneskf934fae2021-07-20 02:19:58 -0700357 IVHD_FLAG_COHERENT : 0);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200358 ivhd->flags |= ((cap_offset_0 & CAP_OFFSET_0_IOTLB_SP) ?
Jason Gleneskf934fae2021-07-20 02:19:58 -0700359 IVHD_FLAG_IOTLB_SUP : 0);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200360 ivhd->flags |= ((mmio_x18_value & MMIO_CTRL_ISOC) ?
Jason Gleneskf934fae2021-07-20 02:19:58 -0700361 IVHD_FLAG_ISOC : 0);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200362 ivhd->flags |= ((mmio_x18_value & MMIO_CTRL_RES_PASS_PW) ?
Jason Gleneskf934fae2021-07-20 02:19:58 -0700363 IVHD_FLAG_RES_PASS_PW : 0);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200364 ivhd->flags |= ((mmio_x18_value & MMIO_CTRL_PASS_PW) ?
Jason Gleneskf934fae2021-07-20 02:19:58 -0700365 IVHD_FLAG_PASS_PW : 0);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200366 ivhd->flags |= ((mmio_x18_value & MMIO_CTRL_HT_TUN_EN) ?
Jason Gleneskf934fae2021-07-20 02:19:58 -0700367 IVHD_FLAG_HT_TUN_EN : 0);
368
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200369 ivhd->pci_segment_group = 0x0000;
Jason Gleneskf934fae2021-07-20 02:19:58 -0700370
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200371 ivhd->iommu_info = pci_read_config16(iommu_dev,
372 ivhd->capability_offset + 0x10) & 0x1F;
373 ivhd->iommu_info |= (pci_read_config16(iommu_dev,
374 ivhd->capability_offset + 0xC) & 0x1F) << IOMMU_INFO_UNIT_ID_SHIFT;
Jason Gleneskf934fae2021-07-20 02:19:58 -0700375
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200376 ivhd->iommu_feature_info = 0;
377 ivhd->iommu_feature_info |= (mmio_x30_value & MMIO_EXT_FEATURE_HATS_MASK)
Jason Gleneskf934fae2021-07-20 02:19:58 -0700378 << (IOMMU_FEATURE_HATS_SHIFT - MMIO_EXT_FEATURE_HATS_SHIFT);
379
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200380 ivhd->iommu_feature_info |= (mmio_x30_value & MMIO_EXT_FEATURE_GATS_MASK)
Jason Gleneskf934fae2021-07-20 02:19:58 -0700381 << (IOMMU_FEATURE_GATS_SHIFT - MMIO_EXT_FEATURE_GATS_SHIFT);
382
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200383 ivhd->iommu_feature_info |= (cap_offset_10 & CAP_OFFSET_10_MSI_NUM_PPR)
Jason Gleneskf934fae2021-07-20 02:19:58 -0700384 >> (CAP_OFFSET_10_MSI_NUM_PPR_SHIFT
385 - IOMMU_FEATURE_MSI_NUM_PPR_SHIFT);
386
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200387 ivhd->iommu_feature_info |= (mmio_x4000_value &
Jason Gleneskf934fae2021-07-20 02:19:58 -0700388 MMIO_CNT_CFG_N_COUNTER_BANKS)
389 << (IOMMU_FEATURE_PN_BANKS_SHIFT - MMIO_CNT_CFG_N_CNT_BANKS_SHIFT);
390
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200391 ivhd->iommu_feature_info |= (mmio_x4000_value & MMIO_CNT_CFG_N_COUNTER)
Jason Gleneskf934fae2021-07-20 02:19:58 -0700392 << (IOMMU_FEATURE_PN_COUNTERS_SHIFT - MMIO_CNT_CFG_N_COUNTER_SHIFT);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200393 ivhd->iommu_feature_info |= (mmio_x30_value &
Jason Gleneskf934fae2021-07-20 02:19:58 -0700394 MMIO_EXT_FEATURE_PAS_MAX_MASK)
395 >> (MMIO_EXT_FEATURE_PAS_MAX_SHIFT - IOMMU_FEATURE_PA_SMAX_SHIFT);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200396 ivhd->iommu_feature_info |= ((mmio_x30_value & MMIO_EXT_FEATURE_HE_SUP)
Jason Gleneskf934fae2021-07-20 02:19:58 -0700397 ? IOMMU_FEATURE_HE_SUP : 0);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200398 ivhd->iommu_feature_info |= ((mmio_x30_value & MMIO_EXT_FEATURE_GA_SUP)
Jason Gleneskf934fae2021-07-20 02:19:58 -0700399 ? IOMMU_FEATURE_GA_SUP : 0);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200400 ivhd->iommu_feature_info |= ((mmio_x30_value & MMIO_EXT_FEATURE_IA_SUP)
Jason Gleneskf934fae2021-07-20 02:19:58 -0700401 ? IOMMU_FEATURE_IA_SUP : 0);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200402 ivhd->iommu_feature_info |= (mmio_x30_value &
Jason Gleneskf934fae2021-07-20 02:19:58 -0700403 MMIO_EXT_FEATURE_GLX_SUP_MASK)
404 >> (MMIO_EXT_FEATURE_GLX_SHIFT - IOMMU_FEATURE_GLX_SHIFT);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200405 ivhd->iommu_feature_info |= ((mmio_x30_value & MMIO_EXT_FEATURE_GT_SUP)
Jason Gleneskf934fae2021-07-20 02:19:58 -0700406 ? IOMMU_FEATURE_GT_SUP : 0);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200407 ivhd->iommu_feature_info |= ((mmio_x30_value & MMIO_EXT_FEATURE_NX_SUP)
Jason Gleneskf934fae2021-07-20 02:19:58 -0700408 ? IOMMU_FEATURE_NX_SUP : 0);
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200409 ivhd->iommu_feature_info |= ((mmio_x30_value & MMIO_EXT_FEATURE_XT_SUP)
Jason Gleneskf934fae2021-07-20 02:19:58 -0700410 ? IOMMU_FEATURE_XT_SUP : 0);
411
412 /* Enable EFR if supported */
413 ivrs->iv_info = pci_read_config32(iommu_dev,
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200414 ivhd->capability_offset + 0x10) & 0x007fffe0;
Jason Gleneskf934fae2021-07-20 02:19:58 -0700415 if (pci_read_config32(iommu_dev,
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200416 ivhd->capability_offset) & EFR_FEATURE_SUP)
Jason Gleneskf934fae2021-07-20 02:19:58 -0700417 ivrs->iv_info |= IVINFO_EFR_SUPPORTED;
418
Jason Gleneskf934fae2021-07-20 02:19:58 -0700419
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200420 current_backup = current;
421 current = acpi_ivhd_misc(current, dev);
422 ivhd->length += (current - current_backup);
423
424 /* If EFR is not supported, IVHD type 11h is reserved */
425 if (!(ivrs->iv_info & IVINFO_EFR_SUPPORTED))
426 return current;
427
428 current = acpi_fill_ivrs11(current, ivhd, nb_dev, iommu_dev);
429
430 ivhd = (struct acpi_ivrs_ivhd *)current;
431 current += sizeof(struct acpi_ivrs_ivhd);
Jason Gleneskf934fae2021-07-20 02:19:58 -0700432 }
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200433 current -= sizeof(struct acpi_ivrs_ivhd);
Jason Gleneskf934fae2021-07-20 02:19:58 -0700434
Naresh Solanki4ef89f72023-05-25 17:37:50 +0200435 return current;
Jason Gleneskf934fae2021-07-20 02:19:58 -0700436}