Linux 4.18.10
[linux/fpc-iii.git] / arch / arm64 / kernel / pci.c
blob0e2ea1c785427849b68435eb1c188e8b7d65eb64
1 /*
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>
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>
20 #include <linux/pci.h>
21 #include <linux/pci-acpi.h>
22 #include <linux/pci-ecam.h>
23 #include <linux/slab.h>
25 #ifdef CONFIG_ACPI
27 * Try to assign the IRQ number when probing a new device
29 int pcibios_alloc_irq(struct pci_dev *dev)
31 if (!acpi_disabled)
32 acpi_pci_irq_enable(dev);
34 return 0;
36 #endif
39 * raw_pci_read/write - Platform-specific PCI config space access.
41 int raw_pci_read(unsigned int domain, unsigned int bus,
42 unsigned int devfn, int reg, int len, u32 *val)
44 struct pci_bus *b = pci_find_bus(domain, bus);
46 if (!b)
47 return PCIBIOS_DEVICE_NOT_FOUND;
48 return b->ops->read(b, devfn, reg, len, val);
51 int raw_pci_write(unsigned int domain, unsigned int bus,
52 unsigned int devfn, int reg, int len, u32 val)
54 struct pci_bus *b = pci_find_bus(domain, bus);
56 if (!b)
57 return PCIBIOS_DEVICE_NOT_FOUND;
58 return b->ops->write(b, devfn, reg, len, val);
61 #ifdef CONFIG_NUMA
63 int pcibus_to_node(struct pci_bus *bus)
65 return dev_to_node(&bus->dev);
67 EXPORT_SYMBOL(pcibus_to_node);
69 #endif
71 #ifdef CONFIG_ACPI
73 struct acpi_pci_generic_root_info {
74 struct acpi_pci_root_info common;
75 struct pci_config_window *cfg; /* config space mapping */
78 int acpi_pci_bus_find_domain_nr(struct pci_bus *bus)
80 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);
84 return root->segment;
87 int pcibios_root_bridge_prepare(struct pci_host_bridge *bridge)
89 if (!acpi_disabled) {
90 struct pci_config_window *cfg = bridge->bus->sysdata;
91 struct acpi_device *adev = to_acpi_device(cfg->parent);
92 struct device *bus_dev = &bridge->bus->dev;
94 ACPI_COMPANION_SET(&bridge->dev, adev);
95 set_dev_node(bus_dev, acpi_get_node(acpi_device_handle(adev)));
98 return 0;
101 static int pci_acpi_root_prepare_resources(struct acpi_pci_root_info *ci)
103 struct resource_entry *entry, *tmp;
104 int status;
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);
111 return status;
115 * Lookup the bus range for the domain in MCFG, and set up config space
116 * mapping.
118 static struct pci_config_window *
119 pci_acpi_setup_ecam_mapping(struct acpi_pci_root *root)
121 struct device *dev = &root->device->dev;
122 struct resource *bus_res = &root->secondary;
123 u16 seg = root->segment;
124 struct pci_ecam_ops *ecam_ops;
125 struct resource cfgres;
126 struct acpi_device *adev;
127 struct pci_config_window *cfg;
128 int ret;
130 ret = pci_mcfg_lookup(root, &cfgres, &ecam_ops);
131 if (ret) {
132 dev_err(dev, "%04x:%pR ECAM region not found\n", seg, bus_res);
133 return NULL;
136 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);
144 cfg = pci_ecam_create(dev, &cfgres, bus_res, ecam_ops);
145 if (IS_ERR(cfg)) {
146 dev_err(dev, "%04x:%pR error %ld mapping ECAM\n", seg, bus_res,
147 PTR_ERR(cfg));
148 return NULL;
151 return cfg;
154 /* release_info: free resources allocated by init_info */
155 static void pci_acpi_generic_release_info(struct acpi_pci_root_info *ci)
157 struct acpi_pci_generic_root_info *ri;
159 ri = container_of(ci, struct acpi_pci_generic_root_info, common);
160 pci_ecam_free(ri->cfg);
161 kfree(ci->ops);
162 kfree(ri);
165 /* Interface called from ACPI code to setup PCI host controller */
166 struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root)
168 int node = acpi_get_node(root->device->handle);
169 struct acpi_pci_generic_root_info *ri;
170 struct pci_bus *bus, *child;
171 struct acpi_pci_root_ops *root_ops;
173 ri = kzalloc_node(sizeof(*ri), GFP_KERNEL, node);
174 if (!ri)
175 return NULL;
177 root_ops = kzalloc_node(sizeof(*root_ops), GFP_KERNEL, node);
178 if (!root_ops) {
179 kfree(ri);
180 return NULL;
183 ri->cfg = pci_acpi_setup_ecam_mapping(root);
184 if (!ri->cfg) {
185 kfree(ri);
186 kfree(root_ops);
187 return NULL;
190 root_ops->release_info = pci_acpi_generic_release_info;
191 root_ops->prepare_resources = pci_acpi_root_prepare_resources;
192 root_ops->pci_ops = &ri->cfg->ops->pci_ops;
193 bus = acpi_pci_root_create(root, root_ops, &ri->common, ri->cfg);
194 if (!bus)
195 return NULL;
197 pci_bus_size_bridges(bus);
198 pci_bus_assign_resources(bus);
200 list_for_each_entry(child, &bus->children, node)
201 pcie_bus_configure_settings(child);
203 return bus;
206 void pcibios_add_bus(struct pci_bus *bus)
208 acpi_pci_add_bus(bus);
211 void pcibios_remove_bus(struct pci_bus *bus)
213 acpi_pci_remove_bus(bus);
216 #endif