Tomasz Nowicki | 935c760 | 2016-06-10 21:55:13 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 Broadcom |
| 3 | * Author: Jayachandran C <jchandra@broadcom.com> |
| 4 | * Copyright (C) 2016 Semihalf |
| 5 | * Author: Tomasz Nowicki <tn@semihalf.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License, version 2, as |
| 9 | * published by the Free Software Foundation (the "GPL"). |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License version 2 (GPLv2) for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * version 2 (GPLv2) along with this source code. |
| 18 | */ |
| 19 | |
| 20 | #define pr_fmt(fmt) "ACPI: " fmt |
| 21 | |
| 22 | #include <linux/kernel.h> |
| 23 | #include <linux/pci.h> |
| 24 | #include <linux/pci-acpi.h> |
Tomasz Nowicki | 13983eb | 2016-09-09 21:24:03 +0200 | [diff] [blame] | 25 | #include <linux/pci-ecam.h> |
Tomasz Nowicki | 935c760 | 2016-06-10 21:55:13 +0200 | [diff] [blame] | 26 | |
| 27 | /* Structure to hold entries from the MCFG table */ |
| 28 | struct mcfg_entry { |
| 29 | struct list_head list; |
| 30 | phys_addr_t addr; |
| 31 | u16 segment; |
| 32 | u8 bus_start; |
| 33 | u8 bus_end; |
| 34 | }; |
| 35 | |
Tomasz Nowicki | 5b69b85 | 2016-09-09 21:24:04 +0200 | [diff] [blame] | 36 | #ifdef CONFIG_PCI_QUIRKS |
| 37 | struct mcfg_fixup { |
| 38 | char oem_id[ACPI_OEM_ID_SIZE + 1]; |
| 39 | char oem_table_id[ACPI_OEM_TABLE_ID_SIZE + 1]; |
| 40 | u32 oem_revision; |
| 41 | u16 segment; |
| 42 | struct resource bus_range; |
| 43 | struct pci_ecam_ops *ops; |
| 44 | struct resource cfgres; |
| 45 | }; |
| 46 | |
| 47 | #define MCFG_BUS_RANGE(start, end) DEFINE_RES_NAMED((start), \ |
| 48 | ((end) - (start) + 1), \ |
| 49 | NULL, IORESOURCE_BUS) |
| 50 | #define MCFG_BUS_ANY MCFG_BUS_RANGE(0x0, 0xff) |
| 51 | |
| 52 | static struct mcfg_fixup mcfg_quirks[] = { |
| 53 | /* { OEM_ID, OEM_TABLE_ID, REV, SEGMENT, BUS_RANGE, ops, cfgres }, */ |
Christopher Covington | 2ca5b8d | 2016-11-02 11:11:27 -0500 | [diff] [blame^] | 54 | |
| 55 | #define QCOM_ECAM32(seg) \ |
| 56 | { "QCOM ", "QDF2432 ", 1, seg, MCFG_BUS_ANY, &pci_32b_ops } |
| 57 | QCOM_ECAM32(0), |
| 58 | QCOM_ECAM32(1), |
| 59 | QCOM_ECAM32(2), |
| 60 | QCOM_ECAM32(3), |
| 61 | QCOM_ECAM32(4), |
| 62 | QCOM_ECAM32(5), |
| 63 | QCOM_ECAM32(6), |
| 64 | QCOM_ECAM32(7), |
Tomasz Nowicki | 5b69b85 | 2016-09-09 21:24:04 +0200 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | static char mcfg_oem_id[ACPI_OEM_ID_SIZE]; |
| 68 | static char mcfg_oem_table_id[ACPI_OEM_TABLE_ID_SIZE]; |
| 69 | static u32 mcfg_oem_revision; |
| 70 | |
| 71 | static int pci_mcfg_quirk_matches(struct mcfg_fixup *f, u16 segment, |
| 72 | struct resource *bus_range) |
| 73 | { |
| 74 | if (!memcmp(f->oem_id, mcfg_oem_id, ACPI_OEM_ID_SIZE) && |
| 75 | !memcmp(f->oem_table_id, mcfg_oem_table_id, |
| 76 | ACPI_OEM_TABLE_ID_SIZE) && |
| 77 | f->oem_revision == mcfg_oem_revision && |
| 78 | f->segment == segment && |
| 79 | resource_contains(&f->bus_range, bus_range)) |
| 80 | return 1; |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | #endif |
| 85 | |
| 86 | static void pci_mcfg_apply_quirks(struct acpi_pci_root *root, |
| 87 | struct resource *cfgres, |
| 88 | struct pci_ecam_ops **ecam_ops) |
| 89 | { |
| 90 | #ifdef CONFIG_PCI_QUIRKS |
| 91 | u16 segment = root->segment; |
| 92 | struct resource *bus_range = &root->secondary; |
| 93 | struct mcfg_fixup *f; |
| 94 | int i; |
| 95 | |
| 96 | for (i = 0, f = mcfg_quirks; i < ARRAY_SIZE(mcfg_quirks); i++, f++) { |
| 97 | if (pci_mcfg_quirk_matches(f, segment, bus_range)) { |
| 98 | if (f->cfgres.start) |
| 99 | *cfgres = f->cfgres; |
| 100 | if (f->ops) |
| 101 | *ecam_ops = f->ops; |
| 102 | dev_info(&root->device->dev, "MCFG quirk: ECAM at %pR for %pR with %ps\n", |
| 103 | cfgres, bus_range, *ecam_ops); |
| 104 | return; |
| 105 | } |
| 106 | } |
| 107 | #endif |
| 108 | } |
| 109 | |
Tomasz Nowicki | 935c760 | 2016-06-10 21:55:13 +0200 | [diff] [blame] | 110 | /* List to save MCFG entries */ |
| 111 | static LIST_HEAD(pci_mcfg_list); |
| 112 | |
Tomasz Nowicki | 13983eb | 2016-09-09 21:24:03 +0200 | [diff] [blame] | 113 | int pci_mcfg_lookup(struct acpi_pci_root *root, struct resource *cfgres, |
| 114 | struct pci_ecam_ops **ecam_ops) |
Tomasz Nowicki | 935c760 | 2016-06-10 21:55:13 +0200 | [diff] [blame] | 115 | { |
Tomasz Nowicki | 13983eb | 2016-09-09 21:24:03 +0200 | [diff] [blame] | 116 | struct pci_ecam_ops *ops = &pci_generic_ecam_ops; |
| 117 | struct resource *bus_res = &root->secondary; |
| 118 | u16 seg = root->segment; |
Tomasz Nowicki | 935c760 | 2016-06-10 21:55:13 +0200 | [diff] [blame] | 119 | struct mcfg_entry *e; |
Tomasz Nowicki | 13983eb | 2016-09-09 21:24:03 +0200 | [diff] [blame] | 120 | struct resource res; |
| 121 | |
| 122 | /* Use address from _CBA if present, otherwise lookup MCFG */ |
| 123 | if (root->mcfg_addr) |
| 124 | goto skip_lookup; |
Tomasz Nowicki | 935c760 | 2016-06-10 21:55:13 +0200 | [diff] [blame] | 125 | |
| 126 | /* |
| 127 | * We expect exact match, unless MCFG entry end bus covers more than |
| 128 | * specified by caller. |
| 129 | */ |
| 130 | list_for_each_entry(e, &pci_mcfg_list, list) { |
| 131 | if (e->segment == seg && e->bus_start == bus_res->start && |
Tomasz Nowicki | 13983eb | 2016-09-09 21:24:03 +0200 | [diff] [blame] | 132 | e->bus_end >= bus_res->end) { |
| 133 | root->mcfg_addr = e->addr; |
| 134 | } |
| 135 | |
Tomasz Nowicki | 935c760 | 2016-06-10 21:55:13 +0200 | [diff] [blame] | 136 | } |
| 137 | |
Tomasz Nowicki | 13983eb | 2016-09-09 21:24:03 +0200 | [diff] [blame] | 138 | skip_lookup: |
| 139 | memset(&res, 0, sizeof(res)); |
Tomasz Nowicki | 5b69b85 | 2016-09-09 21:24:04 +0200 | [diff] [blame] | 140 | if (root->mcfg_addr) { |
| 141 | res.start = root->mcfg_addr + (bus_res->start << 20); |
| 142 | res.end = res.start + (resource_size(bus_res) << 20) - 1; |
| 143 | res.flags = IORESOURCE_MEM; |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * Allow quirks to override default ECAM ops and CFG resource |
| 148 | * range. This may even fabricate a CFG resource range in case |
| 149 | * MCFG does not have it. Invalid CFG start address means MCFG |
| 150 | * firmware bug or we need another quirk in array. |
| 151 | */ |
| 152 | pci_mcfg_apply_quirks(root, &res, &ops); |
| 153 | if (!res.start) |
| 154 | return -ENXIO; |
| 155 | |
Tomasz Nowicki | 13983eb | 2016-09-09 21:24:03 +0200 | [diff] [blame] | 156 | *cfgres = res; |
| 157 | *ecam_ops = ops; |
Tomasz Nowicki | 935c760 | 2016-06-10 21:55:13 +0200 | [diff] [blame] | 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | static __init int pci_mcfg_parse(struct acpi_table_header *header) |
| 162 | { |
| 163 | struct acpi_table_mcfg *mcfg; |
| 164 | struct acpi_mcfg_allocation *mptr; |
| 165 | struct mcfg_entry *e, *arr; |
| 166 | int i, n; |
| 167 | |
| 168 | if (header->length < sizeof(struct acpi_table_mcfg)) |
| 169 | return -EINVAL; |
| 170 | |
| 171 | n = (header->length - sizeof(struct acpi_table_mcfg)) / |
| 172 | sizeof(struct acpi_mcfg_allocation); |
| 173 | mcfg = (struct acpi_table_mcfg *)header; |
| 174 | mptr = (struct acpi_mcfg_allocation *) &mcfg[1]; |
| 175 | |
| 176 | arr = kcalloc(n, sizeof(*arr), GFP_KERNEL); |
| 177 | if (!arr) |
| 178 | return -ENOMEM; |
| 179 | |
| 180 | for (i = 0, e = arr; i < n; i++, mptr++, e++) { |
| 181 | e->segment = mptr->pci_segment; |
| 182 | e->addr = mptr->address; |
| 183 | e->bus_start = mptr->start_bus_number; |
| 184 | e->bus_end = mptr->end_bus_number; |
| 185 | list_add(&e->list, &pci_mcfg_list); |
| 186 | } |
| 187 | |
Tomasz Nowicki | 5b69b85 | 2016-09-09 21:24:04 +0200 | [diff] [blame] | 188 | #ifdef CONFIG_PCI_QUIRKS |
| 189 | /* Save MCFG IDs and revision for quirks matching */ |
| 190 | memcpy(mcfg_oem_id, header->oem_id, ACPI_OEM_ID_SIZE); |
| 191 | memcpy(mcfg_oem_table_id, header->oem_table_id, ACPI_OEM_TABLE_ID_SIZE); |
| 192 | mcfg_oem_revision = header->oem_revision; |
| 193 | #endif |
| 194 | |
Tomasz Nowicki | 935c760 | 2016-06-10 21:55:13 +0200 | [diff] [blame] | 195 | pr_info("MCFG table detected, %d entries\n", n); |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | /* Interface called by ACPI - parse and save MCFG table */ |
| 200 | void __init pci_mmcfg_late_init(void) |
| 201 | { |
| 202 | int err = acpi_table_parse(ACPI_SIG_MCFG, pci_mcfg_parse); |
| 203 | if (err) |
| 204 | pr_err("Failed to parse MCFG (%d)\n", err); |
| 205 | } |