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 | |
| 36 | /* List to save MCFG entries */ |
| 37 | static LIST_HEAD(pci_mcfg_list); |
| 38 | |
Tomasz Nowicki | 13983eb | 2016-09-09 21:24:03 +0200 | [diff] [blame^] | 39 | int pci_mcfg_lookup(struct acpi_pci_root *root, struct resource *cfgres, |
| 40 | struct pci_ecam_ops **ecam_ops) |
Tomasz Nowicki | 935c760 | 2016-06-10 21:55:13 +0200 | [diff] [blame] | 41 | { |
Tomasz Nowicki | 13983eb | 2016-09-09 21:24:03 +0200 | [diff] [blame^] | 42 | struct pci_ecam_ops *ops = &pci_generic_ecam_ops; |
| 43 | struct resource *bus_res = &root->secondary; |
| 44 | u16 seg = root->segment; |
Tomasz Nowicki | 935c760 | 2016-06-10 21:55:13 +0200 | [diff] [blame] | 45 | struct mcfg_entry *e; |
Tomasz Nowicki | 13983eb | 2016-09-09 21:24:03 +0200 | [diff] [blame^] | 46 | struct resource res; |
| 47 | |
| 48 | /* Use address from _CBA if present, otherwise lookup MCFG */ |
| 49 | if (root->mcfg_addr) |
| 50 | goto skip_lookup; |
Tomasz Nowicki | 935c760 | 2016-06-10 21:55:13 +0200 | [diff] [blame] | 51 | |
| 52 | /* |
| 53 | * We expect exact match, unless MCFG entry end bus covers more than |
| 54 | * specified by caller. |
| 55 | */ |
| 56 | list_for_each_entry(e, &pci_mcfg_list, list) { |
| 57 | if (e->segment == seg && e->bus_start == bus_res->start && |
Tomasz Nowicki | 13983eb | 2016-09-09 21:24:03 +0200 | [diff] [blame^] | 58 | e->bus_end >= bus_res->end) { |
| 59 | root->mcfg_addr = e->addr; |
| 60 | } |
| 61 | |
Tomasz Nowicki | 935c760 | 2016-06-10 21:55:13 +0200 | [diff] [blame] | 62 | } |
| 63 | |
Tomasz Nowicki | 13983eb | 2016-09-09 21:24:03 +0200 | [diff] [blame^] | 64 | if (!root->mcfg_addr) |
| 65 | return -ENXIO; |
| 66 | |
| 67 | skip_lookup: |
| 68 | memset(&res, 0, sizeof(res)); |
| 69 | res.start = root->mcfg_addr + (bus_res->start << 20); |
| 70 | res.end = res.start + (resource_size(bus_res) << 20) - 1; |
| 71 | res.flags = IORESOURCE_MEM; |
| 72 | *cfgres = res; |
| 73 | *ecam_ops = ops; |
Tomasz Nowicki | 935c760 | 2016-06-10 21:55:13 +0200 | [diff] [blame] | 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | static __init int pci_mcfg_parse(struct acpi_table_header *header) |
| 78 | { |
| 79 | struct acpi_table_mcfg *mcfg; |
| 80 | struct acpi_mcfg_allocation *mptr; |
| 81 | struct mcfg_entry *e, *arr; |
| 82 | int i, n; |
| 83 | |
| 84 | if (header->length < sizeof(struct acpi_table_mcfg)) |
| 85 | return -EINVAL; |
| 86 | |
| 87 | n = (header->length - sizeof(struct acpi_table_mcfg)) / |
| 88 | sizeof(struct acpi_mcfg_allocation); |
| 89 | mcfg = (struct acpi_table_mcfg *)header; |
| 90 | mptr = (struct acpi_mcfg_allocation *) &mcfg[1]; |
| 91 | |
| 92 | arr = kcalloc(n, sizeof(*arr), GFP_KERNEL); |
| 93 | if (!arr) |
| 94 | return -ENOMEM; |
| 95 | |
| 96 | for (i = 0, e = arr; i < n; i++, mptr++, e++) { |
| 97 | e->segment = mptr->pci_segment; |
| 98 | e->addr = mptr->address; |
| 99 | e->bus_start = mptr->start_bus_number; |
| 100 | e->bus_end = mptr->end_bus_number; |
| 101 | list_add(&e->list, &pci_mcfg_list); |
| 102 | } |
| 103 | |
| 104 | pr_info("MCFG table detected, %d entries\n", n); |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | /* Interface called by ACPI - parse and save MCFG table */ |
| 109 | void __init pci_mmcfg_late_init(void) |
| 110 | { |
| 111 | int err = acpi_table_parse(ACPI_SIG_MCFG, pci_mcfg_parse); |
| 112 | if (err) |
| 113 | pr_err("Failed to parse MCFG (%d)\n", err); |
| 114 | } |