blob: 4f0e3ebfea4b4f6496a783bd172abc05e7ec1d4a [file] [log] [blame]
Liviu Dudaud1e6dc92014-09-29 15:29:31 +01001/*
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 Guoa9cb97f2015-03-24 14:02:40 +000013#include <linux/acpi.h>
Liviu Dudaud1e6dc92014-09-29 15:29:31 +010014#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 Nowicki2ab51dd2016-06-10 15:36:26 -050020#include <linux/pci.h>
Tomasz Nowicki0cb07862016-06-10 21:55:19 +020021#include <linux/pci-acpi.h>
22#include <linux/pci-ecam.h>
Liviu Dudaud1e6dc92014-09-29 15:29:31 +010023#include <linux/slab.h>
24
Liviu Dudaud1e6dc92014-09-29 15:29:31 +010025/*
26 * Called after each bus is probed, but before its children are examined
27 */
28void 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 */
36resource_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
42/*
Tomasz Nowickid8ed75d2016-06-10 21:55:17 +020043 * Try to assign the IRQ number when probing a new device
Liviu Dudaud1e6dc92014-09-29 15:29:31 +010044 */
Tomasz Nowickid8ed75d2016-06-10 21:55:17 +020045int pcibios_alloc_irq(struct pci_dev *dev)
Liviu Dudaud1e6dc92014-09-29 15:29:31 +010046{
Tomasz Nowickid8ed75d2016-06-10 21:55:17 +020047 if (acpi_disabled)
48 dev->irq = of_irq_parse_and_map_pci(dev, 0, 0);
49#ifdef CONFIG_ACPI
50 else
51 return acpi_pci_irq_enable(dev);
52#endif
Liviu Dudaud1e6dc92014-09-29 15:29:31 +010053
54 return 0;
55}
Hanjun Guoa9cb97f2015-03-24 14:02:40 +000056
57/*
58 * raw_pci_read/write - Platform-specific PCI config space access.
59 */
60int raw_pci_read(unsigned int domain, unsigned int bus,
61 unsigned int devfn, int reg, int len, u32 *val)
62{
Tomasz Nowickif058f4f2016-06-10 21:55:18 +020063 struct pci_bus *b = pci_find_bus(domain, bus);
64
65 if (!b)
66 return PCIBIOS_DEVICE_NOT_FOUND;
67 return b->ops->read(b, devfn, reg, len, val);
Hanjun Guoa9cb97f2015-03-24 14:02:40 +000068}
69
70int raw_pci_write(unsigned int domain, unsigned int bus,
71 unsigned int devfn, int reg, int len, u32 val)
72{
Tomasz Nowickif058f4f2016-06-10 21:55:18 +020073 struct pci_bus *b = pci_find_bus(domain, bus);
74
75 if (!b)
76 return PCIBIOS_DEVICE_NOT_FOUND;
77 return b->ops->write(b, devfn, reg, len, val);
Hanjun Guoa9cb97f2015-03-24 14:02:40 +000078}
79
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -070080#ifdef CONFIG_NUMA
81
82int pcibus_to_node(struct pci_bus *bus)
83{
84 return dev_to_node(&bus->dev);
85}
86EXPORT_SYMBOL(pcibus_to_node);
87
88#endif
89
Hanjun Guoa9cb97f2015-03-24 14:02:40 +000090#ifdef CONFIG_ACPI
Tomasz Nowicki2ab51dd2016-06-10 15:36:26 -050091
Tomasz Nowicki0cb07862016-06-10 21:55:19 +020092struct acpi_pci_generic_root_info {
93 struct acpi_pci_root_info common;
94 struct pci_config_window *cfg; /* config space mapping */
95};
96
Tomasz Nowicki2ab51dd2016-06-10 15:36:26 -050097int acpi_pci_bus_find_domain_nr(struct pci_bus *bus)
98{
Tomasz Nowicki0cb07862016-06-10 21:55:19 +020099 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);
102
103 return root->segment;
104}
105
106int pcibios_root_bridge_prepare(struct pci_host_bridge *bridge)
107{
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);
112 }
113
Tomasz Nowicki2ab51dd2016-06-10 15:36:26 -0500114 return 0;
115}
116
Bjorn Helgaas8fd43912016-12-02 17:25:54 -0600117static int pci_acpi_root_prepare_resources(struct acpi_pci_root_info *ci)
118{
119 struct resource_entry *entry, *tmp;
120 int status;
121
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);
126 }
127 return status;
128}
129
Tomasz Nowicki0cb07862016-06-10 21:55:19 +0200130/*
131 * Lookup the bus range for the domain in MCFG, and set up config space
132 * mapping.
133 */
134static struct pci_config_window *
135pci_acpi_setup_ecam_mapping(struct acpi_pci_root *root)
136{
Bjorn Helgaasdfd19722016-12-01 11:33:57 -0600137 struct device *dev = &root->device->dev;
Tomasz Nowicki0cb07862016-06-10 21:55:19 +0200138 struct resource *bus_res = &root->secondary;
139 u16 seg = root->segment;
Tomasz Nowicki13983eb2016-09-09 21:24:03 +0200140 struct pci_ecam_ops *ecam_ops;
Tomasz Nowicki0cb07862016-06-10 21:55:19 +0200141 struct resource cfgres;
Bjorn Helgaas08b1c192016-11-30 14:48:33 -0600142 struct acpi_device *adev;
143 struct pci_config_window *cfg;
Tomasz Nowicki13983eb2016-09-09 21:24:03 +0200144 int ret;
Tomasz Nowicki0cb07862016-06-10 21:55:19 +0200145
Tomasz Nowicki13983eb2016-09-09 21:24:03 +0200146 ret = pci_mcfg_lookup(root, &cfgres, &ecam_ops);
147 if (ret) {
Bjorn Helgaasdfd19722016-12-01 11:33:57 -0600148 dev_err(dev, "%04x:%pR ECAM region not found\n", seg, bus_res);
Tomasz Nowicki0cb07862016-06-10 21:55:19 +0200149 return NULL;
150 }
151
Bjorn Helgaas08b1c192016-11-30 14:48:33 -0600152 adev = acpi_resource_consumer(&cfgres);
153 if (adev)
154 dev_info(dev, "ECAM area %pR reserved by %s\n", &cfgres,
155 dev_name(&adev->dev));
156 else
157 dev_warn(dev, FW_BUG "ECAM area %pR not reserved in ACPI namespace\n",
158 &cfgres);
159
Tomasz Nowicki13983eb2016-09-09 21:24:03 +0200160 cfg = pci_ecam_create(dev, &cfgres, bus_res, ecam_ops);
Tomasz Nowicki0cb07862016-06-10 21:55:19 +0200161 if (IS_ERR(cfg)) {
Bjorn Helgaasdfd19722016-12-01 11:33:57 -0600162 dev_err(dev, "%04x:%pR error %ld mapping ECAM\n", seg, bus_res,
163 PTR_ERR(cfg));
Tomasz Nowicki0cb07862016-06-10 21:55:19 +0200164 return NULL;
165 }
166
167 return cfg;
168}
169
170/* release_info: free resources allocated by init_info */
171static void pci_acpi_generic_release_info(struct acpi_pci_root_info *ci)
172{
173 struct acpi_pci_generic_root_info *ri;
174
175 ri = container_of(ci, struct acpi_pci_generic_root_info, common);
176 pci_ecam_free(ri->cfg);
Tomasz Nowicki093d24a2016-11-24 12:05:23 +0100177 kfree(ci->ops);
Tomasz Nowicki0cb07862016-06-10 21:55:19 +0200178 kfree(ri);
179}
180
Tomasz Nowicki0cb07862016-06-10 21:55:19 +0200181/* Interface called from ACPI code to setup PCI host controller */
Hanjun Guoa9cb97f2015-03-24 14:02:40 +0000182struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root)
183{
Tomasz Nowicki0cb07862016-06-10 21:55:19 +0200184 int node = acpi_get_node(root->device->handle);
185 struct acpi_pci_generic_root_info *ri;
186 struct pci_bus *bus, *child;
Tomasz Nowicki093d24a2016-11-24 12:05:23 +0100187 struct acpi_pci_root_ops *root_ops;
Tomasz Nowicki0cb07862016-06-10 21:55:19 +0200188
189 ri = kzalloc_node(sizeof(*ri), GFP_KERNEL, node);
190 if (!ri)
191 return NULL;
192
Tomasz Nowicki093d24a2016-11-24 12:05:23 +0100193 root_ops = kzalloc_node(sizeof(*root_ops), GFP_KERNEL, node);
194 if (!root_ops)
195 return NULL;
196
Tomasz Nowicki0cb07862016-06-10 21:55:19 +0200197 ri->cfg = pci_acpi_setup_ecam_mapping(root);
198 if (!ri->cfg) {
199 kfree(ri);
Tomasz Nowicki093d24a2016-11-24 12:05:23 +0100200 kfree(root_ops);
Tomasz Nowicki0cb07862016-06-10 21:55:19 +0200201 return NULL;
202 }
203
Tomasz Nowicki093d24a2016-11-24 12:05:23 +0100204 root_ops->release_info = pci_acpi_generic_release_info;
Bjorn Helgaas8fd43912016-12-02 17:25:54 -0600205 root_ops->prepare_resources = pci_acpi_root_prepare_resources;
Tomasz Nowicki093d24a2016-11-24 12:05:23 +0100206 root_ops->pci_ops = &ri->cfg->ops->pci_ops;
207 bus = acpi_pci_root_create(root, root_ops, &ri->common, ri->cfg);
Tomasz Nowicki0cb07862016-06-10 21:55:19 +0200208 if (!bus)
209 return NULL;
210
211 pci_bus_size_bridges(bus);
212 pci_bus_assign_resources(bus);
213
214 list_for_each_entry(child, &bus->children, node)
215 pcie_bus_configure_settings(child);
216
217 return bus;
Hanjun Guoa9cb97f2015-03-24 14:02:40 +0000218}
Tomasz Nowicki0cb07862016-06-10 21:55:19 +0200219
220void pcibios_add_bus(struct pci_bus *bus)
221{
222 acpi_pci_add_bus(bus);
223}
224
225void pcibios_remove_bus(struct pci_bus *bus)
226{
227 acpi_pci_remove_bus(bus);
228}
229
Hanjun Guoa9cb97f2015-03-24 14:02:40 +0000230#endif