]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - arch/arm64/kernel/pci.c
UBUNTU: Ubuntu-5.4.0-117.132
[mirror_ubuntu-focal-kernel.git] / arch / arm64 / kernel / pci.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
d1e6dc91
LD
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.
d1e6dc91
LD
7 */
8
a9cb97fe 9#include <linux/acpi.h>
d1e6dc91
LD
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>
2ab51dde 16#include <linux/pci.h>
0cb0786b
TN
17#include <linux/pci-acpi.h>
18#include <linux/pci-ecam.h>
d1e6dc91
LD
19#include <linux/slab.h>
20
769b461f 21#ifdef CONFIG_ACPI
d1e6dc91 22/*
d8ed75d5 23 * Try to assign the IRQ number when probing a new device
d1e6dc91 24 */
d8ed75d5 25int pcibios_alloc_irq(struct pci_dev *dev)
d1e6dc91 26{
769b461f
LP
27 if (!acpi_disabled)
28 acpi_pci_irq_enable(dev);
d1e6dc91
LD
29
30 return 0;
31}
769b461f 32#endif
a9cb97fe
HG
33
34/*
35 * raw_pci_read/write - Platform-specific PCI config space access.
36 */
37int raw_pci_read(unsigned int domain, unsigned int bus,
38 unsigned int devfn, int reg, int len, u32 *val)
39{
f058f4fb
TN
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);
a9cb97fe
HG
45}
46
47int raw_pci_write(unsigned int domain, unsigned int bus,
48 unsigned int devfn, int reg, int len, u32 val)
49{
f058f4fb
TN
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);
a9cb97fe
HG
55}
56
1a2db300
GK
57#ifdef CONFIG_NUMA
58
59int pcibus_to_node(struct pci_bus *bus)
60{
61 return dev_to_node(&bus->dev);
62}
63EXPORT_SYMBOL(pcibus_to_node);
64
65#endif
66
a9cb97fe 67#ifdef CONFIG_ACPI
2ab51dde 68
0cb0786b
TN
69struct acpi_pci_generic_root_info {
70 struct acpi_pci_root_info common;
71 struct pci_config_window *cfg; /* config space mapping */
72};
73
2ab51dde
TN
74int acpi_pci_bus_find_domain_nr(struct pci_bus *bus)
75{
0cb0786b
TN
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
83int 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);
db46a72b
LP
88 struct device *bus_dev = &bridge->bus->dev;
89
0cb0786b 90 ACPI_COMPANION_SET(&bridge->dev, adev);
db46a72b 91 set_dev_node(bus_dev, acpi_get_node(acpi_device_handle(adev)));
0cb0786b
TN
92 }
93
2ab51dde
TN
94 return 0;
95}
96
8fd4391e
BH
97static 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
0cb0786b
TN
110/*
111 * Lookup the bus range for the domain in MCFG, and set up config space
112 * mapping.
113 */
114static struct pci_config_window *
115pci_acpi_setup_ecam_mapping(struct acpi_pci_root *root)
116{
dfd1972c 117 struct device *dev = &root->device->dev;
0cb0786b
TN
118 struct resource *bus_res = &root->secondary;
119 u16 seg = root->segment;
13983eb8 120 struct pci_ecam_ops *ecam_ops;
0cb0786b 121 struct resource cfgres;
08b1c196
BH
122 struct acpi_device *adev;
123 struct pci_config_window *cfg;
13983eb8 124 int ret;
0cb0786b 125
13983eb8
TN
126 ret = pci_mcfg_lookup(root, &cfgres, &ecam_ops);
127 if (ret) {
dfd1972c 128 dev_err(dev, "%04x:%pR ECAM region not found\n", seg, bus_res);
0cb0786b
TN
129 return NULL;
130 }
131
08b1c196
BH
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
13983eb8 140 cfg = pci_ecam_create(dev, &cfgres, bus_res, ecam_ops);
0cb0786b 141 if (IS_ERR(cfg)) {
dfd1972c
BH
142 dev_err(dev, "%04x:%pR error %ld mapping ECAM\n", seg, bus_res,
143 PTR_ERR(cfg));
0cb0786b
TN
144 return NULL;
145 }
146
147 return cfg;
148}
149
150/* release_info: free resources allocated by init_info */
151static 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);
093d24a2 157 kfree(ci->ops);
0cb0786b
TN
158 kfree(ri);
159}
160
0cb0786b 161/* Interface called from ACPI code to setup PCI host controller */
a9cb97fe
HG
162struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root)
163{
0cb0786b
TN
164 struct acpi_pci_generic_root_info *ri;
165 struct pci_bus *bus, *child;
093d24a2 166 struct acpi_pci_root_ops *root_ops;
85dc0413 167 struct pci_host_bridge *host;
0cb0786b 168
9c314a48 169 ri = kzalloc(sizeof(*ri), GFP_KERNEL);
0cb0786b
TN
170 if (!ri)
171 return NULL;
172
9c314a48 173 root_ops = kzalloc(sizeof(*root_ops), GFP_KERNEL);
717902cc
TL
174 if (!root_ops) {
175 kfree(ri);
093d24a2 176 return NULL;
717902cc 177 }
093d24a2 178
0cb0786b
TN
179 ri->cfg = pci_acpi_setup_ecam_mapping(root);
180 if (!ri->cfg) {
181 kfree(ri);
093d24a2 182 kfree(root_ops);
0cb0786b
TN
183 return NULL;
184 }
185
093d24a2 186 root_ops->release_info = pci_acpi_generic_release_info;
8fd4391e 187 root_ops->prepare_resources = pci_acpi_root_prepare_resources;
093d24a2
TN
188 root_ops->pci_ops = &ri->cfg->ops->pci_ops;
189 bus = acpi_pci_root_create(root, root_ops, &ri->common, ri->cfg);
0cb0786b
TN
190 if (!bus)
191 return NULL;
192
85dc0413
BH
193 /* If we must preserve the resource configuration, claim now */
194 host = pci_find_host_bridge(bus);
195 if (host->preserve_config)
196 pci_bus_claim_resources(bus);
197
198 /*
199 * Assign whatever was left unassigned. If we didn't claim above,
200 * this will reassign everything.
201 */
3e8ba968 202 pci_assign_unassigned_root_bus_resources(bus);
0cb0786b
TN
203
204 list_for_each_entry(child, &bus->children, node)
205 pcie_bus_configure_settings(child);
206
207 return bus;
208}
209
210void pcibios_add_bus(struct pci_bus *bus)
211{
212 acpi_pci_add_bus(bus);
a9cb97fe 213}
0cb0786b
TN
214
215void pcibios_remove_bus(struct pci_bus *bus)
216{
217 acpi_pci_remove_bus(bus);
218}
219
a9cb97fe 220#endif