blob: bb85e2f4603f88d527a97da32c6e29c662053a89 [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
Lorenzo Pieralisi769b4612017-06-28 15:14:12 -050025#ifdef CONFIG_ACPI
Liviu Dudaud1e6dc92014-09-29 15:29:31 +010026/*
Tomasz Nowickid8ed75d2016-06-10 21:55:17 +020027 * Try to assign the IRQ number when probing a new device
Liviu Dudaud1e6dc92014-09-29 15:29:31 +010028 */
Tomasz Nowickid8ed75d2016-06-10 21:55:17 +020029int pcibios_alloc_irq(struct pci_dev *dev)
Liviu Dudaud1e6dc92014-09-29 15:29:31 +010030{
Lorenzo Pieralisi769b4612017-06-28 15:14:12 -050031 if (!acpi_disabled)
32 acpi_pci_irq_enable(dev);
Liviu Dudaud1e6dc92014-09-29 15:29:31 +010033
34 return 0;
35}
Lorenzo Pieralisi769b4612017-06-28 15:14:12 -050036#endif
Hanjun Guoa9cb97f2015-03-24 14:02:40 +000037
38/*
39 * raw_pci_read/write - Platform-specific PCI config space access.
40 */
41int raw_pci_read(unsigned int domain, unsigned int bus,
42 unsigned int devfn, int reg, int len, u32 *val)
43{
Tomasz Nowickif058f4f2016-06-10 21:55:18 +020044 struct pci_bus *b = pci_find_bus(domain, bus);
45
46 if (!b)
47 return PCIBIOS_DEVICE_NOT_FOUND;
48 return b->ops->read(b, devfn, reg, len, val);
Hanjun Guoa9cb97f2015-03-24 14:02:40 +000049}
50
51int raw_pci_write(unsigned int domain, unsigned int bus,
52 unsigned int devfn, int reg, int len, u32 val)
53{
Tomasz Nowickif058f4f2016-06-10 21:55:18 +020054 struct pci_bus *b = pci_find_bus(domain, bus);
55
56 if (!b)
57 return PCIBIOS_DEVICE_NOT_FOUND;
58 return b->ops->write(b, devfn, reg, len, val);
Hanjun Guoa9cb97f2015-03-24 14:02:40 +000059}
60
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -070061#ifdef CONFIG_NUMA
62
63int pcibus_to_node(struct pci_bus *bus)
64{
65 return dev_to_node(&bus->dev);
66}
67EXPORT_SYMBOL(pcibus_to_node);
68
69#endif
70
Hanjun Guoa9cb97f2015-03-24 14:02:40 +000071#ifdef CONFIG_ACPI
Tomasz Nowicki2ab51dd2016-06-10 15:36:26 -050072
Tomasz Nowicki0cb07862016-06-10 21:55:19 +020073struct acpi_pci_generic_root_info {
74 struct acpi_pci_root_info common;
75 struct pci_config_window *cfg; /* config space mapping */
76};
77
Tomasz Nowicki2ab51dd2016-06-10 15:36:26 -050078int acpi_pci_bus_find_domain_nr(struct pci_bus *bus)
79{
Tomasz Nowicki0cb07862016-06-10 21:55:19 +020080 struct pci_config_window *cfg = bus->sysdata;
81 struct acpi_device *adev = to_acpi_device(cfg->parent);
82 struct acpi_pci_root *root = acpi_driver_data(adev);
83
84 return root->segment;
85}
86
87int pcibios_root_bridge_prepare(struct pci_host_bridge *bridge)
88{
89 if (!acpi_disabled) {
90 struct pci_config_window *cfg = bridge->bus->sysdata;
91 struct acpi_device *adev = to_acpi_device(cfg->parent);
Lorenzo Pieralisidb46a722017-05-24 18:22:19 +010092 struct device *bus_dev = &bridge->bus->dev;
93
Tomasz Nowicki0cb07862016-06-10 21:55:19 +020094 ACPI_COMPANION_SET(&bridge->dev, adev);
Lorenzo Pieralisidb46a722017-05-24 18:22:19 +010095 set_dev_node(bus_dev, acpi_get_node(acpi_device_handle(adev)));
Tomasz Nowicki0cb07862016-06-10 21:55:19 +020096 }
97
Tomasz Nowicki2ab51dd2016-06-10 15:36:26 -050098 return 0;
99}
100
Bjorn Helgaas8fd43912016-12-02 17:25:54 -0600101static int pci_acpi_root_prepare_resources(struct acpi_pci_root_info *ci)
102{
103 struct resource_entry *entry, *tmp;
104 int status;
105
106 status = acpi_pci_probe_root_resources(ci);
107 resource_list_for_each_entry_safe(entry, tmp, &ci->resources) {
108 if (!(entry->res->flags & IORESOURCE_WINDOW))
109 resource_list_destroy_entry(entry);
110 }
111 return status;
112}
113
Tomasz Nowicki0cb07862016-06-10 21:55:19 +0200114/*
115 * Lookup the bus range for the domain in MCFG, and set up config space
116 * mapping.
117 */
118static struct pci_config_window *
119pci_acpi_setup_ecam_mapping(struct acpi_pci_root *root)
120{
Bjorn Helgaasdfd19722016-12-01 11:33:57 -0600121 struct device *dev = &root->device->dev;
Tomasz Nowicki0cb07862016-06-10 21:55:19 +0200122 struct resource *bus_res = &root->secondary;
123 u16 seg = root->segment;
Tomasz Nowicki13983eb2016-09-09 21:24:03 +0200124 struct pci_ecam_ops *ecam_ops;
Tomasz Nowicki0cb07862016-06-10 21:55:19 +0200125 struct resource cfgres;
Bjorn Helgaas08b1c192016-11-30 14:48:33 -0600126 struct acpi_device *adev;
127 struct pci_config_window *cfg;
Tomasz Nowicki13983eb2016-09-09 21:24:03 +0200128 int ret;
Tomasz Nowicki0cb07862016-06-10 21:55:19 +0200129
Tomasz Nowicki13983eb2016-09-09 21:24:03 +0200130 ret = pci_mcfg_lookup(root, &cfgres, &ecam_ops);
131 if (ret) {
Bjorn Helgaasdfd19722016-12-01 11:33:57 -0600132 dev_err(dev, "%04x:%pR ECAM region not found\n", seg, bus_res);
Tomasz Nowicki0cb07862016-06-10 21:55:19 +0200133 return NULL;
134 }
135
Bjorn Helgaas08b1c192016-11-30 14:48:33 -0600136 adev = acpi_resource_consumer(&cfgres);
137 if (adev)
138 dev_info(dev, "ECAM area %pR reserved by %s\n", &cfgres,
139 dev_name(&adev->dev));
140 else
141 dev_warn(dev, FW_BUG "ECAM area %pR not reserved in ACPI namespace\n",
142 &cfgres);
143
Tomasz Nowicki13983eb2016-09-09 21:24:03 +0200144 cfg = pci_ecam_create(dev, &cfgres, bus_res, ecam_ops);
Tomasz Nowicki0cb07862016-06-10 21:55:19 +0200145 if (IS_ERR(cfg)) {
Bjorn Helgaasdfd19722016-12-01 11:33:57 -0600146 dev_err(dev, "%04x:%pR error %ld mapping ECAM\n", seg, bus_res,
147 PTR_ERR(cfg));
Tomasz Nowicki0cb07862016-06-10 21:55:19 +0200148 return NULL;
149 }
150
151 return cfg;
152}
153
154/* release_info: free resources allocated by init_info */
155static void pci_acpi_generic_release_info(struct acpi_pci_root_info *ci)
156{
157 struct acpi_pci_generic_root_info *ri;
158
159 ri = container_of(ci, struct acpi_pci_generic_root_info, common);
160 pci_ecam_free(ri->cfg);
Tomasz Nowicki093d24a2016-11-24 12:05:23 +0100161 kfree(ci->ops);
Tomasz Nowicki0cb07862016-06-10 21:55:19 +0200162 kfree(ri);
163}
164
Tomasz Nowicki0cb07862016-06-10 21:55:19 +0200165/* Interface called from ACPI code to setup PCI host controller */
Hanjun Guoa9cb97f2015-03-24 14:02:40 +0000166struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root)
167{
Tomasz Nowicki0cb07862016-06-10 21:55:19 +0200168 struct acpi_pci_generic_root_info *ri;
169 struct pci_bus *bus, *child;
Tomasz Nowicki093d24a2016-11-24 12:05:23 +0100170 struct acpi_pci_root_ops *root_ops;
Tomasz Nowicki0cb07862016-06-10 21:55:19 +0200171
Punit Agrawal9c314a42018-08-28 16:05:12 +0100172 ri = kzalloc(sizeof(*ri), GFP_KERNEL);
Tomasz Nowicki0cb07862016-06-10 21:55:19 +0200173 if (!ri)
174 return NULL;
175
Punit Agrawal9c314a42018-08-28 16:05:12 +0100176 root_ops = kzalloc(sizeof(*root_ops), GFP_KERNEL);
Timmy Li717902c2017-05-22 16:48:28 +0100177 if (!root_ops) {
178 kfree(ri);
Tomasz Nowicki093d24a2016-11-24 12:05:23 +0100179 return NULL;
Timmy Li717902c2017-05-22 16:48:28 +0100180 }
Tomasz Nowicki093d24a2016-11-24 12:05:23 +0100181
Tomasz Nowicki0cb07862016-06-10 21:55:19 +0200182 ri->cfg = pci_acpi_setup_ecam_mapping(root);
183 if (!ri->cfg) {
184 kfree(ri);
Tomasz Nowicki093d24a2016-11-24 12:05:23 +0100185 kfree(root_ops);
Tomasz Nowicki0cb07862016-06-10 21:55:19 +0200186 return NULL;
187 }
188
Tomasz Nowicki093d24a2016-11-24 12:05:23 +0100189 root_ops->release_info = pci_acpi_generic_release_info;
Bjorn Helgaas8fd43912016-12-02 17:25:54 -0600190 root_ops->prepare_resources = pci_acpi_root_prepare_resources;
Tomasz Nowicki093d24a2016-11-24 12:05:23 +0100191 root_ops->pci_ops = &ri->cfg->ops->pci_ops;
192 bus = acpi_pci_root_create(root, root_ops, &ri->common, ri->cfg);
Tomasz Nowicki0cb07862016-06-10 21:55:19 +0200193 if (!bus)
194 return NULL;
195
196 pci_bus_size_bridges(bus);
197 pci_bus_assign_resources(bus);
198
199 list_for_each_entry(child, &bus->children, node)
200 pcie_bus_configure_settings(child);
201
202 return bus;
Hanjun Guoa9cb97f2015-03-24 14:02:40 +0000203}
Tomasz Nowicki0cb07862016-06-10 21:55:19 +0200204
205void pcibios_add_bus(struct pci_bus *bus)
206{
207 acpi_pci_add_bus(bus);
208}
209
210void pcibios_remove_bus(struct pci_bus *bus)
211{
212 acpi_pci_remove_bus(bus);
213}
214
Hanjun Guoa9cb97f2015-03-24 14:02:40 +0000215#endif