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