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 | |
| 19 | /* TODO: Systems with more than one PCI root need to read the data fabric registers to |
| 20 | see which PCI bus numbers get decoded to which PCI root. */ |
| 21 | bus = 0; |
| 22 | limit = CONFIG_ECAM_MMCONF_BUS_NUMBER - 1; |
| 23 | |
| 24 | /* Set bus first number of PCI root */ |
| 25 | domain->link_list->secondary = bus; |
| 26 | /* subordinate needs to be the same as secondary before pci_domain_scan_bus call. */ |
| 27 | domain->link_list->subordinate = bus; |
| 28 | |
| 29 | pci_domain_scan_bus(domain); |
| 30 | |
| 31 | /* pci_domain_scan_bus will modify subordinate, so change it back to the maximum |
| 32 | bus number decoded to this PCI root for the acpigen_resource_producer_bus_number |
| 33 | call to write the correct ACPI code. */ |
| 34 | domain->link_list->subordinate = limit; |
| 35 | } |
| 36 | |
| 37 | /* Read the registers and return normalized values */ |
| 38 | static void data_fabric_get_mmio_base_size(unsigned int reg, |
| 39 | resource_t *mmio_base, resource_t *mmio_limit) |
| 40 | { |
Felix Held | 18a3c23 | 2023-08-03 00:10:03 +0200 | [diff] [blame^] | 41 | const uint32_t base_reg = data_fabric_broadcast_read32(DF_MMIO_BASE(reg)); |
| 42 | 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] | 43 | /* The raw register values are bits 47..16 of the actual address */ |
| 44 | *mmio_base = (resource_t)base_reg << D18F0_MMIO_SHIFT; |
| 45 | *mmio_limit = (((resource_t)limit_reg + 1) << D18F0_MMIO_SHIFT) - 1; |
| 46 | } |
| 47 | |
| 48 | static void print_df_mmio_outside_of_cpu_mmio_error(unsigned int reg) |
| 49 | { |
| 50 | printk(BIOS_WARNING, "DF MMIO register %u outside of CPU MMIO region.\n", reg); |
| 51 | } |
| 52 | |
| 53 | static bool is_mmio_region_valid(unsigned int reg, resource_t mmio_base, resource_t mmio_limit) |
| 54 | { |
| 55 | if (mmio_base > mmio_limit) { |
| 56 | printk(BIOS_WARNING, "DF MMIO register %u's base is above its limit.\n", reg); |
| 57 | return false; |
| 58 | } |
| 59 | if (mmio_base >= 4ULL * GiB) { |
| 60 | /* MMIO region above 4GB needs to be above TOP_MEM2 MSR value */ |
| 61 | if (mmio_base < get_top_of_mem_above_4gb()) { |
| 62 | print_df_mmio_outside_of_cpu_mmio_error(reg); |
| 63 | return false; |
| 64 | } |
| 65 | } else { |
| 66 | /* MMIO region below 4GB needs to be above TOP_MEM MSR value */ |
| 67 | if (mmio_base < get_top_of_mem_below_4gb()) { |
| 68 | print_df_mmio_outside_of_cpu_mmio_error(reg); |
| 69 | return false; |
| 70 | } |
| 71 | /* MMIO region below 4GB mustn't cross the 4GB boundary. */ |
| 72 | if (mmio_limit >= 4ULL * GiB) { |
| 73 | printk(BIOS_WARNING, "DF MMIO register %u crosses 4GB boundary.\n", |
| 74 | reg); |
| 75 | return false; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | static void report_data_fabric_mmio(struct device *domain, unsigned int idx, |
| 83 | resource_t mmio_base, resource_t mmio_limit) |
| 84 | { |
| 85 | struct resource *res; |
| 86 | res = new_resource(domain, idx); |
| 87 | res->base = mmio_base; |
| 88 | res->limit = mmio_limit; |
| 89 | res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED; |
| 90 | } |
| 91 | |
| 92 | /* Tell the resource allocator about the usable MMIO ranges configured in the data fabric */ |
| 93 | static void add_data_fabric_mmio_regions(struct device *domain, unsigned int *idx) |
| 94 | { |
| 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 = |
| 101 | (1ULL << get_usable_physical_address_bits()) - DF_RESERVED_TOP_12GB_MMIO_SIZE; |
| 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 | |
| 114 | /* TODO: Systems with more than one PCI root need to check to which PCI root |
| 115 | the MMIO range gets decoded to. */ |
| 116 | |
| 117 | data_fabric_get_mmio_base_size(i, &mmio_base, &mmio_limit); |
| 118 | |
| 119 | if (!is_mmio_region_valid(i, mmio_base, mmio_limit)) |
| 120 | continue; |
| 121 | |
| 122 | /* Make sure to not report a region overlapping with the fixed MMIO resources |
| 123 | below 4GB or the reserved MMIO range in the last 12GB of the addressable |
| 124 | address range. The code assumes that the fixed MMIO resources below 4GB |
| 125 | are between IO_APIC_ADDR and the 4GB boundary. */ |
| 126 | if (mmio_base < 4ULL * GiB) { |
| 127 | if (mmio_base >= IO_APIC_ADDR) |
| 128 | continue; |
| 129 | if (mmio_limit >= IO_APIC_ADDR) |
| 130 | mmio_limit = IO_APIC_ADDR - 1; |
| 131 | } else { |
| 132 | if (mmio_base >= reserved_upper_mmio_base) |
| 133 | continue; |
| 134 | if (mmio_limit >= reserved_upper_mmio_base) |
| 135 | mmio_limit = reserved_upper_mmio_base - 1; |
| 136 | } |
| 137 | |
| 138 | report_data_fabric_mmio(domain, (*idx)++, mmio_base, mmio_limit); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /* Tell the resource allocator about the usable I/O space */ |
| 143 | static void add_io_regions(struct device *domain, unsigned int *idx) |
| 144 | { |
| 145 | struct resource *res; |
| 146 | |
| 147 | /* TODO: Systems with more than one PCI root need to read the data fabric registers to |
| 148 | see which IO ranges get decoded to which PCI root. */ |
| 149 | |
| 150 | res = new_resource(domain, (*idx)++); |
| 151 | res->base = 0; |
| 152 | res->limit = 0xffff; |
| 153 | res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED; |
| 154 | } |
| 155 | |
| 156 | void amd_pci_domain_read_resources(struct device *domain) |
| 157 | { |
| 158 | unsigned int idx = 0; |
| 159 | |
| 160 | add_io_regions(domain, &idx); |
| 161 | |
| 162 | add_data_fabric_mmio_regions(domain, &idx); |
Felix Held | 3216972 | 2023-07-14 19:41:06 +0200 | [diff] [blame] | 163 | |
| 164 | read_non_pci_resources(domain, &idx); |
Felix Held | 407bd58 | 2023-04-24 17:58:24 +0200 | [diff] [blame] | 165 | } |
Felix Held | 7a5dd78 | 2023-04-28 22:47:33 +0200 | [diff] [blame] | 166 | |
| 167 | static void write_ssdt_domain_io_producer_range_helper(const char *domain_name, |
| 168 | resource_t base, resource_t limit) |
| 169 | { |
| 170 | printk(BIOS_DEBUG, "%s _CRS: adding IO range [%llx-%llx]\n", domain_name, base, limit); |
| 171 | acpigen_resource_producer_io(base, limit); |
| 172 | } |
| 173 | |
| 174 | static void write_ssdt_domain_io_producer_range(const char *domain_name, |
| 175 | resource_t base, resource_t limit) |
| 176 | { |
| 177 | /* |
| 178 | * Split the IO region at the PCI config IO ports so that the IO resource producer |
| 179 | * won't cover the same IO ports that the IO resource consumer for the PCI config IO |
| 180 | * ports in the same ACPI device already covers. |
| 181 | */ |
| 182 | if (base < PCI_IO_CONFIG_INDEX) { |
| 183 | write_ssdt_domain_io_producer_range_helper(domain_name, |
| 184 | base, |
| 185 | MIN(limit, PCI_IO_CONFIG_INDEX - 1)); |
| 186 | } |
| 187 | if (limit > PCI_IO_CONFIG_LAST_PORT) { |
| 188 | write_ssdt_domain_io_producer_range_helper(domain_name, |
| 189 | MAX(base, PCI_IO_CONFIG_LAST_PORT + 1), |
| 190 | limit); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | static void write_ssdt_domain_mmio_producer_range(const char *domain_name, |
| 195 | resource_t base, resource_t limit) |
| 196 | { |
| 197 | printk(BIOS_DEBUG, "%s _CRS: adding MMIO range [%llx-%llx]\n", |
| 198 | domain_name, base, limit); |
| 199 | acpigen_resource_producer_mmio(base, limit, |
| 200 | MEM_RSRC_FLAG_MEM_READ_WRITE | MEM_RSRC_FLAG_MEM_ATTR_NON_CACHE); |
| 201 | } |
| 202 | |
| 203 | void amd_pci_domain_fill_ssdt(const struct device *domain) |
| 204 | { |
| 205 | const char *acpi_scope = acpi_device_path(domain); |
| 206 | printk(BIOS_DEBUG, "%s ACPI scope: '%s'\n", __func__, acpi_scope); |
| 207 | acpigen_write_scope(acpi_device_path(domain)); |
| 208 | |
| 209 | acpigen_write_name("_CRS"); |
| 210 | acpigen_write_resourcetemplate_header(); |
| 211 | |
| 212 | /* PCI bus number range in domain */ |
| 213 | printk(BIOS_DEBUG, "%s _CRS: adding busses [%x-%x]\n", acpi_device_name(domain), |
| 214 | domain->link_list->secondary, domain->link_list->subordinate); |
| 215 | acpigen_resource_producer_bus_number(domain->link_list->secondary, |
| 216 | domain->link_list->subordinate); |
| 217 | |
| 218 | if (domain->link_list->secondary == 0) { |
| 219 | /* ACPI 6.4.2.5 I/O Port Descriptor */ |
| 220 | acpigen_write_io16(PCI_IO_CONFIG_INDEX, PCI_IO_CONFIG_LAST_PORT, 1, |
| 221 | PCI_IO_CONFIG_PORT_COUNT, 1); |
| 222 | } |
| 223 | |
| 224 | struct resource *res; |
| 225 | for (res = domain->resource_list; res != NULL; res = res->next) { |
| 226 | if (!(res->flags & IORESOURCE_ASSIGNED)) |
Felix Held | a239cf4 | 2023-07-29 01:45:31 +0200 | [diff] [blame] | 227 | continue; |
Felix Held | 0df754b | 2023-07-29 01:49:15 +0200 | [diff] [blame] | 228 | /* Don't add MMIO producer ranges for reserved MMIO regions from non-PCI |
| 229 | devices */ |
| 230 | if ((res->flags & IORESOURCE_RESERVE)) |
| 231 | continue; |
Felix Held | 7a5dd78 | 2023-04-28 22:47:33 +0200 | [diff] [blame] | 232 | switch (res->flags & IORESOURCE_TYPE_MASK) { |
| 233 | case IORESOURCE_IO: |
| 234 | write_ssdt_domain_io_producer_range(acpi_device_name(domain), |
| 235 | res->base, res->limit); |
| 236 | break; |
| 237 | case IORESOURCE_MEM: |
| 238 | write_ssdt_domain_mmio_producer_range(acpi_device_name(domain), |
| 239 | res->base, res->limit); |
| 240 | break; |
| 241 | default: |
| 242 | break; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | if (domain->link_list->bridge_ctrl & PCI_BRIDGE_CTL_VGA) { |
| 247 | printk(BIOS_DEBUG, "%s _CRS: adding VGA resource\n", acpi_device_name(domain)); |
| 248 | acpigen_resource_producer_mmio(VGA_MMIO_BASE, VGA_MMIO_LIMIT, |
| 249 | MEM_RSRC_FLAG_MEM_READ_WRITE | MEM_RSRC_FLAG_MEM_ATTR_CACHE); |
| 250 | } |
| 251 | |
| 252 | acpigen_write_resourcetemplate_footer(); |
Felix Held | e4b65cc | 2023-05-05 20:46:11 +0200 | [diff] [blame] | 253 | |
| 254 | acpigen_write_BBN(domain->link_list->secondary); |
| 255 | |
Felix Held | 7a5dd78 | 2023-04-28 22:47:33 +0200 | [diff] [blame] | 256 | /* Scope */ |
| 257 | acpigen_pop_len(); |
| 258 | } |