2 * Code borrowed from powerpc/kernel/pci-common.c
4 * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
5 * Copyright (C) 2014 ARM Ltd.
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.
13 #include <linux/acpi.h>
14 #include <linux/init.h>
16 #include <linux/kernel.h>
18 #include <linux/of_pci.h>
19 #include <linux/of_platform.h>
20 #include <linux/pci.h>
21 #include <linux/pci-acpi.h>
22 #include <linux/pci-ecam.h>
23 #include <linux/slab.h>
26 * Called after each bus is probed, but before its children are examined
28 void pcibios_fixup_bus(struct pci_bus *bus)
30 /* nothing to do, expected to be removed in the future */
34 * We don't have to worry about legacy ISA devices, so nothing to do here
36 resource_size_t pcibios_align_resource(void *data, const struct resource *res,
37 resource_size_t size, resource_size_t align)
43 * Try to assign the IRQ number when probing a new device
45 int pcibios_alloc_irq(struct pci_dev *dev)
48 dev->irq = of_irq_parse_and_map_pci(dev, 0, 0);
51 return acpi_pci_irq_enable(dev);
58 * raw_pci_read/write - Platform-specific PCI config space access.
60 int raw_pci_read(unsigned int domain, unsigned int bus,
61 unsigned int devfn, int reg, int len, u32 *val)
63 struct pci_bus *b = pci_find_bus(domain, bus);
66 return PCIBIOS_DEVICE_NOT_FOUND;
67 return b->ops->read(b, devfn, reg, len, val);
70 int raw_pci_write(unsigned int domain, unsigned int bus,
71 unsigned int devfn, int reg, int len, u32 val)
73 struct pci_bus *b = pci_find_bus(domain, bus);
76 return PCIBIOS_DEVICE_NOT_FOUND;
77 return b->ops->write(b, devfn, reg, len, val);
82 int pcibus_to_node(struct pci_bus *bus)
84 return dev_to_node(&bus->dev);
86 EXPORT_SYMBOL(pcibus_to_node);
92 struct acpi_pci_generic_root_info {
93 struct acpi_pci_root_info common;
94 struct pci_config_window *cfg; /* config space mapping */
97 int acpi_pci_bus_find_domain_nr(struct pci_bus *bus)
99 struct pci_config_window *cfg = bus->sysdata;
100 struct acpi_device *adev = to_acpi_device(cfg->parent);
101 struct acpi_pci_root *root = acpi_driver_data(adev);
103 return root->segment;
106 int pcibios_root_bridge_prepare(struct pci_host_bridge *bridge)
108 if (!acpi_disabled) {
109 struct pci_config_window *cfg = bridge->bus->sysdata;
110 struct acpi_device *adev = to_acpi_device(cfg->parent);
111 ACPI_COMPANION_SET(&bridge->dev, adev);
117 static int pci_acpi_root_prepare_resources(struct acpi_pci_root_info *ci)
119 struct resource_entry *entry, *tmp;
122 status = acpi_pci_probe_root_resources(ci);
123 resource_list_for_each_entry_safe(entry, tmp, &ci->resources) {
124 if (!(entry->res->flags & IORESOURCE_WINDOW))
125 resource_list_destroy_entry(entry);
131 * Lookup the bus range for the domain in MCFG, and set up config space
134 static struct pci_config_window *
135 pci_acpi_setup_ecam_mapping(struct acpi_pci_root *root)
137 struct device *dev = &root->device->dev;
138 struct resource *bus_res = &root->secondary;
139 u16 seg = root->segment;
140 struct resource cfgres;
141 struct acpi_device *adev;
142 struct pci_config_window *cfg;
145 /* Use address from _CBA if present, otherwise lookup MCFG */
146 if (!root->mcfg_addr)
147 root->mcfg_addr = pci_mcfg_lookup(seg, bus_res);
149 if (!root->mcfg_addr) {
150 dev_err(dev, "%04x:%pR ECAM region not found\n", seg, bus_res);
154 bsz = 1 << pci_generic_ecam_ops.bus_shift;
155 cfgres.start = root->mcfg_addr + bus_res->start * bsz;
156 cfgres.end = cfgres.start + resource_size(bus_res) * bsz - 1;
157 cfgres.flags = IORESOURCE_MEM;
159 adev = acpi_resource_consumer(&cfgres);
161 dev_info(dev, "ECAM area %pR reserved by %s\n", &cfgres,
162 dev_name(&adev->dev));
164 dev_warn(dev, FW_BUG "ECAM area %pR not reserved in ACPI namespace\n",
167 cfg = pci_ecam_create(dev, &cfgres, bus_res, &pci_generic_ecam_ops);
169 dev_err(dev, "%04x:%pR error %ld mapping ECAM\n", seg, bus_res,
177 /* release_info: free resources allocated by init_info */
178 static void pci_acpi_generic_release_info(struct acpi_pci_root_info *ci)
180 struct acpi_pci_generic_root_info *ri;
182 ri = container_of(ci, struct acpi_pci_generic_root_info, common);
183 pci_ecam_free(ri->cfg);
188 /* Interface called from ACPI code to setup PCI host controller */
189 struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root)
191 int node = acpi_get_node(root->device->handle);
192 struct acpi_pci_generic_root_info *ri;
193 struct pci_bus *bus, *child;
194 struct acpi_pci_root_ops *root_ops;
196 ri = kzalloc_node(sizeof(*ri), GFP_KERNEL, node);
200 root_ops = kzalloc_node(sizeof(*root_ops), GFP_KERNEL, node);
204 ri->cfg = pci_acpi_setup_ecam_mapping(root);
211 root_ops->release_info = pci_acpi_generic_release_info;
212 root_ops->prepare_resources = pci_acpi_root_prepare_resources;
213 root_ops->pci_ops = &ri->cfg->ops->pci_ops;
214 bus = acpi_pci_root_create(root, root_ops, &ri->common, ri->cfg);
218 pci_bus_size_bridges(bus);
219 pci_bus_assign_resources(bus);
221 list_for_each_entry(child, &bus->children, node)
222 pcie_bus_configure_settings(child);
227 void pcibios_add_bus(struct pci_bus *bus)
229 acpi_pci_add_bus(bus);
232 void pcibios_remove_bus(struct pci_bus *bus)
234 acpi_pci_remove_bus(bus);