Liviu Dudau | d1e6dc9 | 2014-09-29 15:29:31 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Code borrowed from powerpc/kernel/pci-common.c |
| 3 | * |
| 4 | * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM |
| 5 | * Copyright (C) 2014 ARM Ltd. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * version 2 as published by the Free Software Foundation. |
| 10 | * |
| 11 | */ |
| 12 | |
Hanjun Guo | a9cb97f | 2015-03-24 14:02:40 +0000 | [diff] [blame] | 13 | #include <linux/acpi.h> |
Liviu Dudau | d1e6dc9 | 2014-09-29 15:29:31 +0100 | [diff] [blame] | 14 | #include <linux/init.h> |
| 15 | #include <linux/io.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/mm.h> |
| 18 | #include <linux/of_pci.h> |
| 19 | #include <linux/of_platform.h> |
Tomasz Nowicki | 2ab51dd | 2016-06-10 15:36:26 -0500 | [diff] [blame] | 20 | #include <linux/pci.h> |
Tomasz Nowicki | 0cb0786 | 2016-06-10 21:55:19 +0200 | [diff] [blame^] | 21 | #include <linux/pci-acpi.h> |
| 22 | #include <linux/pci-ecam.h> |
Liviu Dudau | d1e6dc9 | 2014-09-29 15:29:31 +0100 | [diff] [blame] | 23 | #include <linux/slab.h> |
| 24 | |
Liviu Dudau | d1e6dc9 | 2014-09-29 15:29:31 +0100 | [diff] [blame] | 25 | /* |
| 26 | * Called after each bus is probed, but before its children are examined |
| 27 | */ |
| 28 | void pcibios_fixup_bus(struct pci_bus *bus) |
| 29 | { |
| 30 | /* nothing to do, expected to be removed in the future */ |
| 31 | } |
| 32 | |
| 33 | /* |
| 34 | * We don't have to worry about legacy ISA devices, so nothing to do here |
| 35 | */ |
| 36 | resource_size_t pcibios_align_resource(void *data, const struct resource *res, |
| 37 | resource_size_t size, resource_size_t align) |
| 38 | { |
| 39 | return res->start; |
| 40 | } |
| 41 | |
Lorenzo Pieralisi | 7240751 | 2015-07-30 14:13:59 +0100 | [diff] [blame] | 42 | /** |
| 43 | * pcibios_enable_device - Enable I/O and memory. |
| 44 | * @dev: PCI device to be enabled |
| 45 | * @mask: bitmask of BARs to enable |
| 46 | */ |
| 47 | int pcibios_enable_device(struct pci_dev *dev, int mask) |
| 48 | { |
| 49 | if (pci_has_flag(PCI_PROBE_ONLY)) |
| 50 | return 0; |
| 51 | |
| 52 | return pci_enable_resources(dev, mask); |
| 53 | } |
| 54 | |
Liviu Dudau | d1e6dc9 | 2014-09-29 15:29:31 +0100 | [diff] [blame] | 55 | /* |
Tomasz Nowicki | d8ed75d | 2016-06-10 21:55:17 +0200 | [diff] [blame] | 56 | * Try to assign the IRQ number when probing a new device |
Liviu Dudau | d1e6dc9 | 2014-09-29 15:29:31 +0100 | [diff] [blame] | 57 | */ |
Tomasz Nowicki | d8ed75d | 2016-06-10 21:55:17 +0200 | [diff] [blame] | 58 | int pcibios_alloc_irq(struct pci_dev *dev) |
Liviu Dudau | d1e6dc9 | 2014-09-29 15:29:31 +0100 | [diff] [blame] | 59 | { |
Tomasz Nowicki | d8ed75d | 2016-06-10 21:55:17 +0200 | [diff] [blame] | 60 | if (acpi_disabled) |
| 61 | dev->irq = of_irq_parse_and_map_pci(dev, 0, 0); |
| 62 | #ifdef CONFIG_ACPI |
| 63 | else |
| 64 | return acpi_pci_irq_enable(dev); |
| 65 | #endif |
Liviu Dudau | d1e6dc9 | 2014-09-29 15:29:31 +0100 | [diff] [blame] | 66 | |
| 67 | return 0; |
| 68 | } |
Hanjun Guo | a9cb97f | 2015-03-24 14:02:40 +0000 | [diff] [blame] | 69 | |
| 70 | /* |
| 71 | * raw_pci_read/write - Platform-specific PCI config space access. |
| 72 | */ |
| 73 | int raw_pci_read(unsigned int domain, unsigned int bus, |
| 74 | unsigned int devfn, int reg, int len, u32 *val) |
| 75 | { |
Tomasz Nowicki | f058f4f | 2016-06-10 21:55:18 +0200 | [diff] [blame] | 76 | struct pci_bus *b = pci_find_bus(domain, bus); |
| 77 | |
| 78 | if (!b) |
| 79 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 80 | return b->ops->read(b, devfn, reg, len, val); |
Hanjun Guo | a9cb97f | 2015-03-24 14:02:40 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | int raw_pci_write(unsigned int domain, unsigned int bus, |
| 84 | unsigned int devfn, int reg, int len, u32 val) |
| 85 | { |
Tomasz Nowicki | f058f4f | 2016-06-10 21:55:18 +0200 | [diff] [blame] | 86 | struct pci_bus *b = pci_find_bus(domain, bus); |
| 87 | |
| 88 | if (!b) |
| 89 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 90 | return b->ops->write(b, devfn, reg, len, val); |
Hanjun Guo | a9cb97f | 2015-03-24 14:02:40 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 93 | #ifdef CONFIG_NUMA |
| 94 | |
| 95 | int pcibus_to_node(struct pci_bus *bus) |
| 96 | { |
| 97 | return dev_to_node(&bus->dev); |
| 98 | } |
| 99 | EXPORT_SYMBOL(pcibus_to_node); |
| 100 | |
| 101 | #endif |
| 102 | |
Hanjun Guo | a9cb97f | 2015-03-24 14:02:40 +0000 | [diff] [blame] | 103 | #ifdef CONFIG_ACPI |
Tomasz Nowicki | 2ab51dd | 2016-06-10 15:36:26 -0500 | [diff] [blame] | 104 | |
Tomasz Nowicki | 0cb0786 | 2016-06-10 21:55:19 +0200 | [diff] [blame^] | 105 | struct acpi_pci_generic_root_info { |
| 106 | struct acpi_pci_root_info common; |
| 107 | struct pci_config_window *cfg; /* config space mapping */ |
| 108 | }; |
| 109 | |
Tomasz Nowicki | 2ab51dd | 2016-06-10 15:36:26 -0500 | [diff] [blame] | 110 | int acpi_pci_bus_find_domain_nr(struct pci_bus *bus) |
| 111 | { |
Tomasz Nowicki | 0cb0786 | 2016-06-10 21:55:19 +0200 | [diff] [blame^] | 112 | struct pci_config_window *cfg = bus->sysdata; |
| 113 | struct acpi_device *adev = to_acpi_device(cfg->parent); |
| 114 | struct acpi_pci_root *root = acpi_driver_data(adev); |
| 115 | |
| 116 | return root->segment; |
| 117 | } |
| 118 | |
| 119 | int pcibios_root_bridge_prepare(struct pci_host_bridge *bridge) |
| 120 | { |
| 121 | if (!acpi_disabled) { |
| 122 | struct pci_config_window *cfg = bridge->bus->sysdata; |
| 123 | struct acpi_device *adev = to_acpi_device(cfg->parent); |
| 124 | ACPI_COMPANION_SET(&bridge->dev, adev); |
| 125 | } |
| 126 | |
Tomasz Nowicki | 2ab51dd | 2016-06-10 15:36:26 -0500 | [diff] [blame] | 127 | return 0; |
| 128 | } |
| 129 | |
Tomasz Nowicki | 0cb0786 | 2016-06-10 21:55:19 +0200 | [diff] [blame^] | 130 | /* |
| 131 | * Lookup the bus range for the domain in MCFG, and set up config space |
| 132 | * mapping. |
| 133 | */ |
| 134 | static struct pci_config_window * |
| 135 | pci_acpi_setup_ecam_mapping(struct acpi_pci_root *root) |
| 136 | { |
| 137 | struct resource *bus_res = &root->secondary; |
| 138 | u16 seg = root->segment; |
| 139 | struct pci_config_window *cfg; |
| 140 | struct resource cfgres; |
| 141 | unsigned int bsz; |
| 142 | |
| 143 | /* Use address from _CBA if present, otherwise lookup MCFG */ |
| 144 | if (!root->mcfg_addr) |
| 145 | root->mcfg_addr = pci_mcfg_lookup(seg, bus_res); |
| 146 | |
| 147 | if (!root->mcfg_addr) { |
| 148 | dev_err(&root->device->dev, "%04x:%pR ECAM region not found\n", |
| 149 | seg, bus_res); |
| 150 | return NULL; |
| 151 | } |
| 152 | |
| 153 | bsz = 1 << pci_generic_ecam_ops.bus_shift; |
| 154 | cfgres.start = root->mcfg_addr + bus_res->start * bsz; |
| 155 | cfgres.end = cfgres.start + resource_size(bus_res) * bsz - 1; |
| 156 | cfgres.flags = IORESOURCE_MEM; |
| 157 | cfg = pci_ecam_create(&root->device->dev, &cfgres, bus_res, |
| 158 | &pci_generic_ecam_ops); |
| 159 | if (IS_ERR(cfg)) { |
| 160 | dev_err(&root->device->dev, "%04x:%pR error %ld mapping ECAM\n", |
| 161 | seg, bus_res, PTR_ERR(cfg)); |
| 162 | return NULL; |
| 163 | } |
| 164 | |
| 165 | return cfg; |
| 166 | } |
| 167 | |
| 168 | /* release_info: free resources allocated by init_info */ |
| 169 | static void pci_acpi_generic_release_info(struct acpi_pci_root_info *ci) |
| 170 | { |
| 171 | struct acpi_pci_generic_root_info *ri; |
| 172 | |
| 173 | ri = container_of(ci, struct acpi_pci_generic_root_info, common); |
| 174 | pci_ecam_free(ri->cfg); |
| 175 | kfree(ri); |
| 176 | } |
| 177 | |
| 178 | static struct acpi_pci_root_ops acpi_pci_root_ops = { |
| 179 | .release_info = pci_acpi_generic_release_info, |
| 180 | }; |
| 181 | |
| 182 | /* Interface called from ACPI code to setup PCI host controller */ |
Hanjun Guo | a9cb97f | 2015-03-24 14:02:40 +0000 | [diff] [blame] | 183 | struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root) |
| 184 | { |
Tomasz Nowicki | 0cb0786 | 2016-06-10 21:55:19 +0200 | [diff] [blame^] | 185 | int node = acpi_get_node(root->device->handle); |
| 186 | struct acpi_pci_generic_root_info *ri; |
| 187 | struct pci_bus *bus, *child; |
| 188 | |
| 189 | ri = kzalloc_node(sizeof(*ri), GFP_KERNEL, node); |
| 190 | if (!ri) |
| 191 | return NULL; |
| 192 | |
| 193 | ri->cfg = pci_acpi_setup_ecam_mapping(root); |
| 194 | if (!ri->cfg) { |
| 195 | kfree(ri); |
| 196 | return NULL; |
| 197 | } |
| 198 | |
| 199 | acpi_pci_root_ops.pci_ops = &ri->cfg->ops->pci_ops; |
| 200 | bus = acpi_pci_root_create(root, &acpi_pci_root_ops, &ri->common, |
| 201 | ri->cfg); |
| 202 | if (!bus) |
| 203 | return NULL; |
| 204 | |
| 205 | pci_bus_size_bridges(bus); |
| 206 | pci_bus_assign_resources(bus); |
| 207 | |
| 208 | list_for_each_entry(child, &bus->children, node) |
| 209 | pcie_bus_configure_settings(child); |
| 210 | |
| 211 | return bus; |
Hanjun Guo | a9cb97f | 2015-03-24 14:02:40 +0000 | [diff] [blame] | 212 | } |
Tomasz Nowicki | 0cb0786 | 2016-06-10 21:55:19 +0200 | [diff] [blame^] | 213 | |
| 214 | void pcibios_add_bus(struct pci_bus *bus) |
| 215 | { |
| 216 | acpi_pci_add_bus(bus); |
| 217 | } |
| 218 | |
| 219 | void pcibios_remove_bus(struct pci_bus *bus) |
| 220 | { |
| 221 | acpi_pci_remove_bus(bus); |
| 222 | } |
| 223 | |
Hanjun Guo | a9cb97f | 2015-03-24 14:02:40 +0000 | [diff] [blame] | 224 | #endif |