Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Liviu Dudau | d1e6dc9 | 2014-09-29 15:29:31 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Code borrowed from powerpc/kernel/pci-common.c |
| 4 | * |
| 5 | * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM |
| 6 | * Copyright (C) 2014 ARM Ltd. |
Liviu Dudau | d1e6dc9 | 2014-09-29 15:29:31 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
Hanjun Guo | a9cb97f | 2015-03-24 14:02:40 +0000 | [diff] [blame] | 9 | #include <linux/acpi.h> |
Liviu Dudau | d1e6dc9 | 2014-09-29 15:29:31 +0100 | [diff] [blame] | 10 | #include <linux/init.h> |
| 11 | #include <linux/io.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/mm.h> |
| 14 | #include <linux/of_pci.h> |
| 15 | #include <linux/of_platform.h> |
Tomasz Nowicki | 2ab51dd | 2016-06-10 15:36:26 -0500 | [diff] [blame] | 16 | #include <linux/pci.h> |
Tomasz Nowicki | 0cb0786 | 2016-06-10 21:55:19 +0200 | [diff] [blame] | 17 | #include <linux/pci-acpi.h> |
| 18 | #include <linux/pci-ecam.h> |
Liviu Dudau | d1e6dc9 | 2014-09-29 15:29:31 +0100 | [diff] [blame] | 19 | #include <linux/slab.h> |
| 20 | |
Lorenzo Pieralisi | 769b461 | 2017-06-28 15:14:12 -0500 | [diff] [blame] | 21 | #ifdef CONFIG_ACPI |
Liviu Dudau | d1e6dc9 | 2014-09-29 15:29:31 +0100 | [diff] [blame] | 22 | /* |
Tomasz Nowicki | d8ed75d | 2016-06-10 21:55:17 +0200 | [diff] [blame] | 23 | * Try to assign the IRQ number when probing a new device |
Liviu Dudau | d1e6dc9 | 2014-09-29 15:29:31 +0100 | [diff] [blame] | 24 | */ |
Tomasz Nowicki | d8ed75d | 2016-06-10 21:55:17 +0200 | [diff] [blame] | 25 | int pcibios_alloc_irq(struct pci_dev *dev) |
Liviu Dudau | d1e6dc9 | 2014-09-29 15:29:31 +0100 | [diff] [blame] | 26 | { |
Lorenzo Pieralisi | 769b461 | 2017-06-28 15:14:12 -0500 | [diff] [blame] | 27 | if (!acpi_disabled) |
| 28 | acpi_pci_irq_enable(dev); |
Liviu Dudau | d1e6dc9 | 2014-09-29 15:29:31 +0100 | [diff] [blame] | 29 | |
| 30 | return 0; |
| 31 | } |
Lorenzo Pieralisi | 769b461 | 2017-06-28 15:14:12 -0500 | [diff] [blame] | 32 | #endif |
Hanjun Guo | a9cb97f | 2015-03-24 14:02:40 +0000 | [diff] [blame] | 33 | |
| 34 | /* |
| 35 | * raw_pci_read/write - Platform-specific PCI config space access. |
| 36 | */ |
| 37 | int raw_pci_read(unsigned int domain, unsigned int bus, |
| 38 | unsigned int devfn, int reg, int len, u32 *val) |
| 39 | { |
Tomasz Nowicki | f058f4f | 2016-06-10 21:55:18 +0200 | [diff] [blame] | 40 | struct pci_bus *b = pci_find_bus(domain, bus); |
| 41 | |
| 42 | if (!b) |
| 43 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 44 | return b->ops->read(b, devfn, reg, len, val); |
Hanjun Guo | a9cb97f | 2015-03-24 14:02:40 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | int raw_pci_write(unsigned int domain, unsigned int bus, |
| 48 | unsigned int devfn, int reg, int len, u32 val) |
| 49 | { |
Tomasz Nowicki | f058f4f | 2016-06-10 21:55:18 +0200 | [diff] [blame] | 50 | struct pci_bus *b = pci_find_bus(domain, bus); |
| 51 | |
| 52 | if (!b) |
| 53 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 54 | return b->ops->write(b, devfn, reg, len, val); |
Hanjun Guo | a9cb97f | 2015-03-24 14:02:40 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 57 | #ifdef CONFIG_NUMA |
| 58 | |
| 59 | int pcibus_to_node(struct pci_bus *bus) |
| 60 | { |
| 61 | return dev_to_node(&bus->dev); |
| 62 | } |
| 63 | EXPORT_SYMBOL(pcibus_to_node); |
| 64 | |
| 65 | #endif |
| 66 | |
Hanjun Guo | a9cb97f | 2015-03-24 14:02:40 +0000 | [diff] [blame] | 67 | #ifdef CONFIG_ACPI |
Tomasz Nowicki | 2ab51dd | 2016-06-10 15:36:26 -0500 | [diff] [blame] | 68 | |
Tomasz Nowicki | 0cb0786 | 2016-06-10 21:55:19 +0200 | [diff] [blame] | 69 | struct acpi_pci_generic_root_info { |
| 70 | struct acpi_pci_root_info common; |
| 71 | struct pci_config_window *cfg; /* config space mapping */ |
| 72 | }; |
| 73 | |
Tomasz Nowicki | 2ab51dd | 2016-06-10 15:36:26 -0500 | [diff] [blame] | 74 | int acpi_pci_bus_find_domain_nr(struct pci_bus *bus) |
| 75 | { |
Tomasz Nowicki | 0cb0786 | 2016-06-10 21:55:19 +0200 | [diff] [blame] | 76 | struct pci_config_window *cfg = bus->sysdata; |
| 77 | struct acpi_device *adev = to_acpi_device(cfg->parent); |
| 78 | struct acpi_pci_root *root = acpi_driver_data(adev); |
| 79 | |
| 80 | return root->segment; |
| 81 | } |
| 82 | |
| 83 | int pcibios_root_bridge_prepare(struct pci_host_bridge *bridge) |
| 84 | { |
| 85 | if (!acpi_disabled) { |
| 86 | struct pci_config_window *cfg = bridge->bus->sysdata; |
| 87 | struct acpi_device *adev = to_acpi_device(cfg->parent); |
Lorenzo Pieralisi | db46a72 | 2017-05-24 18:22:19 +0100 | [diff] [blame] | 88 | struct device *bus_dev = &bridge->bus->dev; |
| 89 | |
Tomasz Nowicki | 0cb0786 | 2016-06-10 21:55:19 +0200 | [diff] [blame] | 90 | ACPI_COMPANION_SET(&bridge->dev, adev); |
Lorenzo Pieralisi | db46a72 | 2017-05-24 18:22:19 +0100 | [diff] [blame] | 91 | set_dev_node(bus_dev, acpi_get_node(acpi_device_handle(adev))); |
Tomasz Nowicki | 0cb0786 | 2016-06-10 21:55:19 +0200 | [diff] [blame] | 92 | } |
| 93 | |
Tomasz Nowicki | 2ab51dd | 2016-06-10 15:36:26 -0500 | [diff] [blame] | 94 | return 0; |
| 95 | } |
| 96 | |
Bjorn Helgaas | 8fd4391 | 2016-12-02 17:25:54 -0600 | [diff] [blame] | 97 | static int pci_acpi_root_prepare_resources(struct acpi_pci_root_info *ci) |
| 98 | { |
| 99 | struct resource_entry *entry, *tmp; |
| 100 | int status; |
| 101 | |
| 102 | status = acpi_pci_probe_root_resources(ci); |
| 103 | resource_list_for_each_entry_safe(entry, tmp, &ci->resources) { |
| 104 | if (!(entry->res->flags & IORESOURCE_WINDOW)) |
| 105 | resource_list_destroy_entry(entry); |
| 106 | } |
| 107 | return status; |
| 108 | } |
| 109 | |
Tomasz Nowicki | 0cb0786 | 2016-06-10 21:55:19 +0200 | [diff] [blame] | 110 | /* |
| 111 | * Lookup the bus range for the domain in MCFG, and set up config space |
| 112 | * mapping. |
| 113 | */ |
| 114 | static struct pci_config_window * |
| 115 | pci_acpi_setup_ecam_mapping(struct acpi_pci_root *root) |
| 116 | { |
Bjorn Helgaas | dfd1972 | 2016-12-01 11:33:57 -0600 | [diff] [blame] | 117 | struct device *dev = &root->device->dev; |
Tomasz Nowicki | 0cb0786 | 2016-06-10 21:55:19 +0200 | [diff] [blame] | 118 | struct resource *bus_res = &root->secondary; |
| 119 | u16 seg = root->segment; |
Tomasz Nowicki | 13983eb | 2016-09-09 21:24:03 +0200 | [diff] [blame] | 120 | struct pci_ecam_ops *ecam_ops; |
Tomasz Nowicki | 0cb0786 | 2016-06-10 21:55:19 +0200 | [diff] [blame] | 121 | struct resource cfgres; |
Bjorn Helgaas | 08b1c19 | 2016-11-30 14:48:33 -0600 | [diff] [blame] | 122 | struct acpi_device *adev; |
| 123 | struct pci_config_window *cfg; |
Tomasz Nowicki | 13983eb | 2016-09-09 21:24:03 +0200 | [diff] [blame] | 124 | int ret; |
Tomasz Nowicki | 0cb0786 | 2016-06-10 21:55:19 +0200 | [diff] [blame] | 125 | |
Tomasz Nowicki | 13983eb | 2016-09-09 21:24:03 +0200 | [diff] [blame] | 126 | ret = pci_mcfg_lookup(root, &cfgres, &ecam_ops); |
| 127 | if (ret) { |
Bjorn Helgaas | dfd1972 | 2016-12-01 11:33:57 -0600 | [diff] [blame] | 128 | dev_err(dev, "%04x:%pR ECAM region not found\n", seg, bus_res); |
Tomasz Nowicki | 0cb0786 | 2016-06-10 21:55:19 +0200 | [diff] [blame] | 129 | return NULL; |
| 130 | } |
| 131 | |
Bjorn Helgaas | 08b1c19 | 2016-11-30 14:48:33 -0600 | [diff] [blame] | 132 | adev = acpi_resource_consumer(&cfgres); |
| 133 | if (adev) |
| 134 | dev_info(dev, "ECAM area %pR reserved by %s\n", &cfgres, |
| 135 | dev_name(&adev->dev)); |
| 136 | else |
| 137 | dev_warn(dev, FW_BUG "ECAM area %pR not reserved in ACPI namespace\n", |
| 138 | &cfgres); |
| 139 | |
Tomasz Nowicki | 13983eb | 2016-09-09 21:24:03 +0200 | [diff] [blame] | 140 | cfg = pci_ecam_create(dev, &cfgres, bus_res, ecam_ops); |
Tomasz Nowicki | 0cb0786 | 2016-06-10 21:55:19 +0200 | [diff] [blame] | 141 | if (IS_ERR(cfg)) { |
Bjorn Helgaas | dfd1972 | 2016-12-01 11:33:57 -0600 | [diff] [blame] | 142 | dev_err(dev, "%04x:%pR error %ld mapping ECAM\n", seg, bus_res, |
| 143 | PTR_ERR(cfg)); |
Tomasz Nowicki | 0cb0786 | 2016-06-10 21:55:19 +0200 | [diff] [blame] | 144 | return NULL; |
| 145 | } |
| 146 | |
| 147 | return cfg; |
| 148 | } |
| 149 | |
| 150 | /* release_info: free resources allocated by init_info */ |
| 151 | static void pci_acpi_generic_release_info(struct acpi_pci_root_info *ci) |
| 152 | { |
| 153 | struct acpi_pci_generic_root_info *ri; |
| 154 | |
| 155 | ri = container_of(ci, struct acpi_pci_generic_root_info, common); |
| 156 | pci_ecam_free(ri->cfg); |
Tomasz Nowicki | 093d24a | 2016-11-24 12:05:23 +0100 | [diff] [blame] | 157 | kfree(ci->ops); |
Tomasz Nowicki | 0cb0786 | 2016-06-10 21:55:19 +0200 | [diff] [blame] | 158 | kfree(ri); |
| 159 | } |
| 160 | |
Tomasz Nowicki | 0cb0786 | 2016-06-10 21:55:19 +0200 | [diff] [blame] | 161 | /* Interface called from ACPI code to setup PCI host controller */ |
Hanjun Guo | a9cb97f | 2015-03-24 14:02:40 +0000 | [diff] [blame] | 162 | struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root) |
| 163 | { |
Tomasz Nowicki | 0cb0786 | 2016-06-10 21:55:19 +0200 | [diff] [blame] | 164 | struct acpi_pci_generic_root_info *ri; |
| 165 | struct pci_bus *bus, *child; |
Tomasz Nowicki | 093d24a | 2016-11-24 12:05:23 +0100 | [diff] [blame] | 166 | struct acpi_pci_root_ops *root_ops; |
Tomasz Nowicki | 0cb0786 | 2016-06-10 21:55:19 +0200 | [diff] [blame] | 167 | |
Punit Agrawal | 9c314a4 | 2018-08-28 16:05:12 +0100 | [diff] [blame] | 168 | ri = kzalloc(sizeof(*ri), GFP_KERNEL); |
Tomasz Nowicki | 0cb0786 | 2016-06-10 21:55:19 +0200 | [diff] [blame] | 169 | if (!ri) |
| 170 | return NULL; |
| 171 | |
Punit Agrawal | 9c314a4 | 2018-08-28 16:05:12 +0100 | [diff] [blame] | 172 | root_ops = kzalloc(sizeof(*root_ops), GFP_KERNEL); |
Timmy Li | 717902c | 2017-05-22 16:48:28 +0100 | [diff] [blame] | 173 | if (!root_ops) { |
| 174 | kfree(ri); |
Tomasz Nowicki | 093d24a | 2016-11-24 12:05:23 +0100 | [diff] [blame] | 175 | return NULL; |
Timmy Li | 717902c | 2017-05-22 16:48:28 +0100 | [diff] [blame] | 176 | } |
Tomasz Nowicki | 093d24a | 2016-11-24 12:05:23 +0100 | [diff] [blame] | 177 | |
Tomasz Nowicki | 0cb0786 | 2016-06-10 21:55:19 +0200 | [diff] [blame] | 178 | ri->cfg = pci_acpi_setup_ecam_mapping(root); |
| 179 | if (!ri->cfg) { |
| 180 | kfree(ri); |
Tomasz Nowicki | 093d24a | 2016-11-24 12:05:23 +0100 | [diff] [blame] | 181 | kfree(root_ops); |
Tomasz Nowicki | 0cb0786 | 2016-06-10 21:55:19 +0200 | [diff] [blame] | 182 | return NULL; |
| 183 | } |
| 184 | |
Tomasz Nowicki | 093d24a | 2016-11-24 12:05:23 +0100 | [diff] [blame] | 185 | root_ops->release_info = pci_acpi_generic_release_info; |
Bjorn Helgaas | 8fd4391 | 2016-12-02 17:25:54 -0600 | [diff] [blame] | 186 | root_ops->prepare_resources = pci_acpi_root_prepare_resources; |
Tomasz Nowicki | 093d24a | 2016-11-24 12:05:23 +0100 | [diff] [blame] | 187 | root_ops->pci_ops = &ri->cfg->ops->pci_ops; |
| 188 | bus = acpi_pci_root_create(root, root_ops, &ri->common, ri->cfg); |
Tomasz Nowicki | 0cb0786 | 2016-06-10 21:55:19 +0200 | [diff] [blame] | 189 | if (!bus) |
| 190 | return NULL; |
| 191 | |
| 192 | pci_bus_size_bridges(bus); |
| 193 | pci_bus_assign_resources(bus); |
| 194 | |
| 195 | list_for_each_entry(child, &bus->children, node) |
| 196 | pcie_bus_configure_settings(child); |
| 197 | |
| 198 | return bus; |
Hanjun Guo | a9cb97f | 2015-03-24 14:02:40 +0000 | [diff] [blame] | 199 | } |
Tomasz Nowicki | 0cb0786 | 2016-06-10 21:55:19 +0200 | [diff] [blame] | 200 | |
| 201 | void pcibios_add_bus(struct pci_bus *bus) |
| 202 | { |
| 203 | acpi_pci_add_bus(bus); |
| 204 | } |
| 205 | |
| 206 | void pcibios_remove_bus(struct pci_bus *bus) |
| 207 | { |
| 208 | acpi_pci_remove_bus(bus); |
| 209 | } |
| 210 | |
Hanjun Guo | a9cb97f | 2015-03-24 14:02:40 +0000 | [diff] [blame] | 211 | #endif |