Felix Held | 407bd58 | 2023-04-24 17:58:24 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | |
Felix Held | 7a5dd78 | 2023-04-28 22:47:33 +0200 | [diff] [blame] | 3 | #include <acpi/acpigen.h> |
Felix Held | 407bd58 | 2023-04-24 17:58:24 +0200 | [diff] [blame] | 4 | #include <amdblocks/data_fabric.h> |
Felix Held | 3216972 | 2023-07-14 19:41:06 +0200 | [diff] [blame] | 5 | #include <amdblocks/root_complex.h> |
Felix Held | 407bd58 | 2023-04-24 17:58:24 +0200 | [diff] [blame] | 6 | #include <arch/ioapic.h> |
Felix Held | 7a5dd78 | 2023-04-28 22:47:33 +0200 | [diff] [blame] | 7 | #include <arch/vga.h> |
Felix Held | 407bd58 | 2023-04-24 17:58:24 +0200 | [diff] [blame] | 8 | #include <console/console.h> |
| 9 | #include <cpu/amd/mtrr.h> |
Felix Held | 89ca478 | 2023-09-12 15:01:02 +0200 | [diff] [blame] | 10 | #include <cpu/cpu.h> |
Felix Held | 407bd58 | 2023-04-24 17:58:24 +0200 | [diff] [blame] | 11 | #include <device/device.h> |
Felix Held | 3b5b66d | 2024-01-11 22:26:18 +0100 | [diff] [blame] | 12 | #include <device/pci.h> |
Felix Held | 407bd58 | 2023-04-24 17:58:24 +0200 | [diff] [blame] | 13 | #include <device/pci_ops.h> |
| 14 | #include <types.h> |
| 15 | |
| 16 | void amd_pci_domain_scan_bus(struct device *domain) |
| 17 | { |
Felix Held | 6c00a6a | 2023-12-06 23:40:42 +0100 | [diff] [blame] | 18 | uint8_t segment_group, bus, limit; |
Felix Held | 407bd58 | 2023-04-24 17:58:24 +0200 | [diff] [blame] | 19 | |
Felix Held | 6c00a6a | 2023-12-06 23:40:42 +0100 | [diff] [blame] | 20 | if (data_fabric_get_pci_bus_numbers(domain, &segment_group, &bus, &limit) != CB_SUCCESS) { |
Felix Held | ea83139 | 2023-08-08 02:55:09 +0200 | [diff] [blame] | 21 | printk(BIOS_ERR, "No PCI bus numbers decoded to PCI root.\n"); |
| 22 | return; |
| 23 | } |
| 24 | |
Felix Held | 3b5b66d | 2024-01-11 22:26:18 +0100 | [diff] [blame] | 25 | if (segment_group >= PCI_SEGMENT_GROUP_COUNT) { |
| 26 | printk(BIOS_ERR, "Skipping domain %u due to too large segment group %u.\n", |
| 27 | domain->path.domain.domain, segment_group); |
Felix Held | 6c00a6a | 2023-12-06 23:40:42 +0100 | [diff] [blame] | 28 | return; |
| 29 | } |
| 30 | |
Felix Held | 3b5b66d | 2024-01-11 22:26:18 +0100 | [diff] [blame] | 31 | /* TODO: Check if bus >= PCI_BUSES_PER_SEGMENT_GROUP and return in that case */ |
Felix Held | ea83139 | 2023-08-08 02:55:09 +0200 | [diff] [blame] | 32 | |
Felix Held | 3b5b66d | 2024-01-11 22:26:18 +0100 | [diff] [blame] | 33 | /* Make sure to not report more than PCI_BUSES_PER_SEGMENT_GROUP PCI buses */ |
| 34 | limit = MIN(limit, PCI_BUSES_PER_SEGMENT_GROUP - 1); |
Felix Held | 407bd58 | 2023-04-24 17:58:24 +0200 | [diff] [blame] | 35 | |
| 36 | /* Set bus first number of PCI root */ |
Arthur Heymans | 7fcd4d5 | 2023-08-24 15:12:19 +0200 | [diff] [blame] | 37 | domain->downstream->secondary = bus; |
Arthur Heymans | 0b0113f | 2023-08-31 17:09:28 +0200 | [diff] [blame] | 38 | /* subordinate needs to be the same as secondary before pci_host_bridge_scan_bus call. */ |
Arthur Heymans | 7fcd4d5 | 2023-08-24 15:12:19 +0200 | [diff] [blame] | 39 | domain->downstream->subordinate = bus; |
Felix Held | 9dcdec5 | 2023-08-08 21:38:43 +0200 | [diff] [blame] | 40 | /* Tell allocator about maximum PCI bus number in domain */ |
Arthur Heymans | 7fcd4d5 | 2023-08-24 15:12:19 +0200 | [diff] [blame] | 41 | domain->downstream->max_subordinate = limit; |
| 42 | domain->downstream->segment_group = segment_group; |
Felix Held | 407bd58 | 2023-04-24 17:58:24 +0200 | [diff] [blame] | 43 | |
Arthur Heymans | 0b0113f | 2023-08-31 17:09:28 +0200 | [diff] [blame] | 44 | pci_host_bridge_scan_bus(domain); |
Felix Held | 407bd58 | 2023-04-24 17:58:24 +0200 | [diff] [blame] | 45 | } |
| 46 | |
Felix Held | 407bd58 | 2023-04-24 17:58:24 +0200 | [diff] [blame] | 47 | static void print_df_mmio_outside_of_cpu_mmio_error(unsigned int reg) |
| 48 | { |
| 49 | printk(BIOS_WARNING, "DF MMIO register %u outside of CPU MMIO region.\n", reg); |
| 50 | } |
| 51 | |
| 52 | static bool is_mmio_region_valid(unsigned int reg, resource_t mmio_base, resource_t mmio_limit) |
| 53 | { |
| 54 | if (mmio_base > mmio_limit) { |
| 55 | printk(BIOS_WARNING, "DF MMIO register %u's base is above its limit.\n", reg); |
| 56 | return false; |
| 57 | } |
| 58 | if (mmio_base >= 4ULL * GiB) { |
| 59 | /* MMIO region above 4GB needs to be above TOP_MEM2 MSR value */ |
| 60 | if (mmio_base < get_top_of_mem_above_4gb()) { |
| 61 | print_df_mmio_outside_of_cpu_mmio_error(reg); |
| 62 | return false; |
| 63 | } |
| 64 | } else { |
| 65 | /* MMIO region below 4GB needs to be above TOP_MEM MSR value */ |
| 66 | if (mmio_base < get_top_of_mem_below_4gb()) { |
| 67 | print_df_mmio_outside_of_cpu_mmio_error(reg); |
| 68 | return false; |
| 69 | } |
| 70 | /* MMIO region below 4GB mustn't cross the 4GB boundary. */ |
| 71 | if (mmio_limit >= 4ULL * GiB) { |
| 72 | printk(BIOS_WARNING, "DF MMIO register %u crosses 4GB boundary.\n", |
| 73 | reg); |
| 74 | return false; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | return true; |
| 79 | } |
| 80 | |
Felix Held | e4594f1 | 2024-01-30 22:04:34 +0100 | [diff] [blame^] | 81 | static void report_data_fabric_mmio(struct device *domain, unsigned long idx, |
Felix Held | 407bd58 | 2023-04-24 17:58:24 +0200 | [diff] [blame] | 82 | resource_t mmio_base, resource_t mmio_limit) |
| 83 | { |
| 84 | struct resource *res; |
| 85 | res = new_resource(domain, idx); |
| 86 | res->base = mmio_base; |
| 87 | res->limit = mmio_limit; |
| 88 | res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED; |
| 89 | } |
| 90 | |
| 91 | /* Tell the resource allocator about the usable MMIO ranges configured in the data fabric */ |
Felix Held | e4594f1 | 2024-01-30 22:04:34 +0100 | [diff] [blame^] | 92 | static void add_data_fabric_mmio_regions(struct device *domain, unsigned long *idx) |
Felix Held | 407bd58 | 2023-04-24 17:58:24 +0200 | [diff] [blame] | 93 | { |
Felix Held | 9cbdc8f | 2023-08-11 22:30:06 +0200 | [diff] [blame] | 94 | const signed int iohc_dest_fabric_id = get_iohc_fabric_id(domain); |
Felix Held | 407bd58 | 2023-04-24 17:58:24 +0200 | [diff] [blame] | 95 | union df_mmio_control ctrl; |
| 96 | resource_t mmio_base; |
| 97 | resource_t mmio_limit; |
| 98 | |
| 99 | /* The last 12GB of the usable address space are reserved and can't be used for MMIO */ |
| 100 | const resource_t reserved_upper_mmio_base = |
Felix Held | 89ca478 | 2023-09-12 15:01:02 +0200 | [diff] [blame] | 101 | (1ULL << cpu_phys_address_size()) - DF_RESERVED_TOP_12GB_MMIO_SIZE; |
Felix Held | 407bd58 | 2023-04-24 17:58:24 +0200 | [diff] [blame] | 102 | |
| 103 | for (unsigned int i = 0; i < DF_MMIO_REG_SET_COUNT; i++) { |
Felix Held | 18a3c23 | 2023-08-03 00:10:03 +0200 | [diff] [blame] | 104 | ctrl.raw = data_fabric_broadcast_read32(DF_MMIO_CONTROL(i)); |
Felix Held | 407bd58 | 2023-04-24 17:58:24 +0200 | [diff] [blame] | 105 | |
| 106 | /* Relevant MMIO regions need to have both reads and writes enabled */ |
| 107 | if (!ctrl.we || !ctrl.re) |
| 108 | continue; |
| 109 | |
| 110 | /* Non-posted region contains fixed FCH MMIO devices */ |
| 111 | if (ctrl.np) |
| 112 | continue; |
| 113 | |
Felix Held | 55822d9 | 2023-08-08 21:38:07 +0200 | [diff] [blame] | 114 | /* Only look at MMIO regions that are decoded to the right PCI root */ |
Felix Held | 9cbdc8f | 2023-08-11 22:30:06 +0200 | [diff] [blame] | 115 | if (ctrl.dst_fabric_id != iohc_dest_fabric_id) |
Felix Held | 55822d9 | 2023-08-08 21:38:07 +0200 | [diff] [blame] | 116 | continue; |
Felix Held | 407bd58 | 2023-04-24 17:58:24 +0200 | [diff] [blame] | 117 | |
| 118 | data_fabric_get_mmio_base_size(i, &mmio_base, &mmio_limit); |
| 119 | |
| 120 | if (!is_mmio_region_valid(i, mmio_base, mmio_limit)) |
| 121 | continue; |
| 122 | |
| 123 | /* Make sure to not report a region overlapping with the fixed MMIO resources |
| 124 | below 4GB or the reserved MMIO range in the last 12GB of the addressable |
| 125 | address range. The code assumes that the fixed MMIO resources below 4GB |
| 126 | are between IO_APIC_ADDR and the 4GB boundary. */ |
| 127 | if (mmio_base < 4ULL * GiB) { |
| 128 | if (mmio_base >= IO_APIC_ADDR) |
| 129 | continue; |
| 130 | if (mmio_limit >= IO_APIC_ADDR) |
| 131 | mmio_limit = IO_APIC_ADDR - 1; |
| 132 | } else { |
| 133 | if (mmio_base >= reserved_upper_mmio_base) |
| 134 | continue; |
| 135 | if (mmio_limit >= reserved_upper_mmio_base) |
| 136 | mmio_limit = reserved_upper_mmio_base - 1; |
| 137 | } |
| 138 | |
| 139 | report_data_fabric_mmio(domain, (*idx)++, mmio_base, mmio_limit); |
| 140 | } |
| 141 | } |
| 142 | |
Felix Held | e4594f1 | 2024-01-30 22:04:34 +0100 | [diff] [blame^] | 143 | static void report_data_fabric_io(struct device *domain, unsigned long idx, |
Felix Held | 2dfd48b | 2023-08-04 19:22:54 +0200 | [diff] [blame] | 144 | resource_t io_base, resource_t io_limit) |
| 145 | { |
| 146 | struct resource *res; |
| 147 | res = new_resource(domain, idx); |
| 148 | res->base = io_base; |
| 149 | res->limit = io_limit; |
| 150 | res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED; |
| 151 | } |
| 152 | |
Felix Held | 407bd58 | 2023-04-24 17:58:24 +0200 | [diff] [blame] | 153 | /* Tell the resource allocator about the usable I/O space */ |
Felix Held | e4594f1 | 2024-01-30 22:04:34 +0100 | [diff] [blame^] | 154 | static void add_data_fabric_io_regions(struct device *domain, unsigned long *idx) |
Felix Held | 407bd58 | 2023-04-24 17:58:24 +0200 | [diff] [blame] | 155 | { |
Felix Held | 9cbdc8f | 2023-08-11 22:30:06 +0200 | [diff] [blame] | 156 | const signed int iohc_dest_fabric_id = get_iohc_fabric_id(domain); |
Felix Held | 4eac0d4 | 2023-08-04 19:40:02 +0200 | [diff] [blame] | 157 | union df_io_base base_reg; |
| 158 | union df_io_limit limit_reg; |
| 159 | resource_t io_base; |
| 160 | resource_t io_limit; |
Felix Held | 407bd58 | 2023-04-24 17:58:24 +0200 | [diff] [blame] | 161 | |
Felix Held | 4eac0d4 | 2023-08-04 19:40:02 +0200 | [diff] [blame] | 162 | for (unsigned int i = 0; i < DF_IO_REG_COUNT; i++) { |
| 163 | base_reg.raw = data_fabric_broadcast_read32(DF_IO_BASE(i)); |
| 164 | |
| 165 | /* Relevant IO regions need to have both reads and writes enabled */ |
| 166 | if (!base_reg.we || !base_reg.re) |
| 167 | continue; |
| 168 | |
| 169 | limit_reg.raw = data_fabric_broadcast_read32(DF_IO_LIMIT(i)); |
| 170 | |
Felix Held | 55822d9 | 2023-08-08 21:38:07 +0200 | [diff] [blame] | 171 | /* Only look at IO regions that are decoded to the right PCI root */ |
Felix Held | 9cbdc8f | 2023-08-11 22:30:06 +0200 | [diff] [blame] | 172 | if (limit_reg.dst_fabric_id != iohc_dest_fabric_id) |
Felix Held | 55822d9 | 2023-08-08 21:38:07 +0200 | [diff] [blame] | 173 | continue; |
Felix Held | 4eac0d4 | 2023-08-04 19:40:02 +0200 | [diff] [blame] | 174 | |
| 175 | io_base = base_reg.io_base << DF_IO_ADDR_SHIFT; |
| 176 | io_limit = ((limit_reg.io_limit + 1) << DF_IO_ADDR_SHIFT) - 1; |
| 177 | |
| 178 | /* Beware that the lower 25 bits of io_base and io_limit can be non-zero |
| 179 | despite there only being 16 bits worth of IO port address space. */ |
| 180 | if (io_base > 0xffff) { |
| 181 | printk(BIOS_WARNING, "DF IO base register %d value outside of valid " |
| 182 | "IO port address range.\n", i); |
| 183 | continue; |
| 184 | } |
| 185 | /* If only the IO limit is outside of the valid 16 bit IO port range, report |
| 186 | the limit as 0xffff, so that the resource allcator won't put IO BARs outside |
| 187 | of the 16 bit IO port address range. */ |
| 188 | io_limit = MIN(io_limit, 0xffff); |
| 189 | |
| 190 | report_data_fabric_io(domain, (*idx)++, io_base, io_limit); |
| 191 | } |
Felix Held | 407bd58 | 2023-04-24 17:58:24 +0200 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | void amd_pci_domain_read_resources(struct device *domain) |
| 195 | { |
Felix Held | e4594f1 | 2024-01-30 22:04:34 +0100 | [diff] [blame^] | 196 | unsigned long idx = 0; |
Felix Held | 407bd58 | 2023-04-24 17:58:24 +0200 | [diff] [blame] | 197 | |
Felix Held | 3f3f93b | 2023-08-04 22:14:40 +0200 | [diff] [blame] | 198 | add_data_fabric_io_regions(domain, &idx); |
Felix Held | 407bd58 | 2023-04-24 17:58:24 +0200 | [diff] [blame] | 199 | |
| 200 | add_data_fabric_mmio_regions(domain, &idx); |
Felix Held | 3216972 | 2023-07-14 19:41:06 +0200 | [diff] [blame] | 201 | |
| 202 | read_non_pci_resources(domain, &idx); |
Felix Held | 407bd58 | 2023-04-24 17:58:24 +0200 | [diff] [blame] | 203 | } |
Felix Held | 7a5dd78 | 2023-04-28 22:47:33 +0200 | [diff] [blame] | 204 | |
| 205 | static void write_ssdt_domain_io_producer_range_helper(const char *domain_name, |
| 206 | resource_t base, resource_t limit) |
| 207 | { |
| 208 | printk(BIOS_DEBUG, "%s _CRS: adding IO range [%llx-%llx]\n", domain_name, base, limit); |
| 209 | acpigen_resource_producer_io(base, limit); |
| 210 | } |
| 211 | |
| 212 | static void write_ssdt_domain_io_producer_range(const char *domain_name, |
| 213 | resource_t base, resource_t limit) |
| 214 | { |
| 215 | /* |
| 216 | * Split the IO region at the PCI config IO ports so that the IO resource producer |
| 217 | * won't cover the same IO ports that the IO resource consumer for the PCI config IO |
| 218 | * ports in the same ACPI device already covers. |
| 219 | */ |
| 220 | if (base < PCI_IO_CONFIG_INDEX) { |
| 221 | write_ssdt_domain_io_producer_range_helper(domain_name, |
| 222 | base, |
| 223 | MIN(limit, PCI_IO_CONFIG_INDEX - 1)); |
| 224 | } |
| 225 | if (limit > PCI_IO_CONFIG_LAST_PORT) { |
| 226 | write_ssdt_domain_io_producer_range_helper(domain_name, |
| 227 | MAX(base, PCI_IO_CONFIG_LAST_PORT + 1), |
| 228 | limit); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | static void write_ssdt_domain_mmio_producer_range(const char *domain_name, |
| 233 | resource_t base, resource_t limit) |
| 234 | { |
| 235 | printk(BIOS_DEBUG, "%s _CRS: adding MMIO range [%llx-%llx]\n", |
| 236 | domain_name, base, limit); |
| 237 | acpigen_resource_producer_mmio(base, limit, |
| 238 | MEM_RSRC_FLAG_MEM_READ_WRITE | MEM_RSRC_FLAG_MEM_ATTR_NON_CACHE); |
| 239 | } |
| 240 | |
| 241 | void amd_pci_domain_fill_ssdt(const struct device *domain) |
| 242 | { |
| 243 | const char *acpi_scope = acpi_device_path(domain); |
| 244 | printk(BIOS_DEBUG, "%s ACPI scope: '%s'\n", __func__, acpi_scope); |
| 245 | acpigen_write_scope(acpi_device_path(domain)); |
| 246 | |
| 247 | acpigen_write_name("_CRS"); |
| 248 | acpigen_write_resourcetemplate_header(); |
| 249 | |
| 250 | /* PCI bus number range in domain */ |
Felix Held | 3b5b66d | 2024-01-11 22:26:18 +0100 | [diff] [blame] | 251 | printk(BIOS_DEBUG, "%s _CRS: adding busses [%x-%x] in segment group %x\n", |
Arthur Heymans | 7fcd4d5 | 2023-08-24 15:12:19 +0200 | [diff] [blame] | 252 | acpi_device_name(domain), domain->downstream->secondary, |
| 253 | domain->downstream->max_subordinate, domain->downstream->segment_group); |
| 254 | acpigen_resource_producer_bus_number(domain->downstream->secondary, |
| 255 | domain->downstream->max_subordinate); |
Felix Held | 7a5dd78 | 2023-04-28 22:47:33 +0200 | [diff] [blame] | 256 | |
Felix Held | 5881f8a | 2024-01-17 22:00:13 +0100 | [diff] [blame] | 257 | if (domain->path.domain.domain == 0) { |
Felix Held | 7a5dd78 | 2023-04-28 22:47:33 +0200 | [diff] [blame] | 258 | /* ACPI 6.4.2.5 I/O Port Descriptor */ |
| 259 | acpigen_write_io16(PCI_IO_CONFIG_INDEX, PCI_IO_CONFIG_LAST_PORT, 1, |
| 260 | PCI_IO_CONFIG_PORT_COUNT, 1); |
| 261 | } |
| 262 | |
| 263 | struct resource *res; |
| 264 | for (res = domain->resource_list; res != NULL; res = res->next) { |
| 265 | if (!(res->flags & IORESOURCE_ASSIGNED)) |
Felix Held | a239cf4 | 2023-07-29 01:45:31 +0200 | [diff] [blame] | 266 | continue; |
Felix Held | 0df754b | 2023-07-29 01:49:15 +0200 | [diff] [blame] | 267 | /* Don't add MMIO producer ranges for reserved MMIO regions from non-PCI |
| 268 | devices */ |
| 269 | if ((res->flags & IORESOURCE_RESERVE)) |
| 270 | continue; |
Felix Held | 7a5dd78 | 2023-04-28 22:47:33 +0200 | [diff] [blame] | 271 | switch (res->flags & IORESOURCE_TYPE_MASK) { |
| 272 | case IORESOURCE_IO: |
| 273 | write_ssdt_domain_io_producer_range(acpi_device_name(domain), |
| 274 | res->base, res->limit); |
| 275 | break; |
| 276 | case IORESOURCE_MEM: |
| 277 | write_ssdt_domain_mmio_producer_range(acpi_device_name(domain), |
| 278 | res->base, res->limit); |
| 279 | break; |
| 280 | default: |
| 281 | break; |
| 282 | } |
| 283 | } |
| 284 | |
Arthur Heymans | 7fcd4d5 | 2023-08-24 15:12:19 +0200 | [diff] [blame] | 285 | if (domain->downstream->bridge_ctrl & PCI_BRIDGE_CTL_VGA) { |
Felix Held | 7a5dd78 | 2023-04-28 22:47:33 +0200 | [diff] [blame] | 286 | printk(BIOS_DEBUG, "%s _CRS: adding VGA resource\n", acpi_device_name(domain)); |
| 287 | acpigen_resource_producer_mmio(VGA_MMIO_BASE, VGA_MMIO_LIMIT, |
| 288 | MEM_RSRC_FLAG_MEM_READ_WRITE | MEM_RSRC_FLAG_MEM_ATTR_CACHE); |
| 289 | } |
| 290 | |
| 291 | acpigen_write_resourcetemplate_footer(); |
Felix Held | e4b65cc | 2023-05-05 20:46:11 +0200 | [diff] [blame] | 292 | |
Arthur Heymans | 7fcd4d5 | 2023-08-24 15:12:19 +0200 | [diff] [blame] | 293 | acpigen_write_SEG(domain->downstream->segment_group); |
| 294 | acpigen_write_BBN(domain->downstream->secondary); |
Felix Held | 7a5dd78 | 2023-04-28 22:47:33 +0200 | [diff] [blame] | 295 | /* Scope */ |
| 296 | acpigen_pop_len(); |
| 297 | } |