]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - arch/arm64/kernel/pci.c
efcc351c518f5b4ec39cf08a57eccebe2b90bf8e
[karo-tx-linux.git] / arch / arm64 / kernel / pci.c
1 /*
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
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>
24
25 /*
26  * Called after each bus is probed, but before its children are examined
27  */
28 void 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  */
36 resource_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 #ifdef CONFIG_ACPI
43 /*
44  * Try to assign the IRQ number when probing a new device
45  */
46 int pcibios_alloc_irq(struct pci_dev *dev)
47 {
48         if (!acpi_disabled)
49                 acpi_pci_irq_enable(dev);
50
51         return 0;
52 }
53 #endif
54
55 /*
56  * raw_pci_read/write - Platform-specific PCI config space access.
57  */
58 int raw_pci_read(unsigned int domain, unsigned int bus,
59                   unsigned int devfn, int reg, int len, u32 *val)
60 {
61         struct pci_bus *b = pci_find_bus(domain, bus);
62
63         if (!b)
64                 return PCIBIOS_DEVICE_NOT_FOUND;
65         return b->ops->read(b, devfn, reg, len, val);
66 }
67
68 int raw_pci_write(unsigned int domain, unsigned int bus,
69                 unsigned int devfn, int reg, int len, u32 val)
70 {
71         struct pci_bus *b = pci_find_bus(domain, bus);
72
73         if (!b)
74                 return PCIBIOS_DEVICE_NOT_FOUND;
75         return b->ops->write(b, devfn, reg, len, val);
76 }
77
78 #ifdef CONFIG_NUMA
79
80 int pcibus_to_node(struct pci_bus *bus)
81 {
82         return dev_to_node(&bus->dev);
83 }
84 EXPORT_SYMBOL(pcibus_to_node);
85
86 #endif
87
88 #ifdef CONFIG_ACPI
89
90 struct acpi_pci_generic_root_info {
91         struct acpi_pci_root_info       common;
92         struct pci_config_window        *cfg;   /* config space mapping */
93 };
94
95 int acpi_pci_bus_find_domain_nr(struct pci_bus *bus)
96 {
97         struct pci_config_window *cfg = bus->sysdata;
98         struct acpi_device *adev = to_acpi_device(cfg->parent);
99         struct acpi_pci_root *root = acpi_driver_data(adev);
100
101         return root->segment;
102 }
103
104 int pcibios_root_bridge_prepare(struct pci_host_bridge *bridge)
105 {
106         if (!acpi_disabled) {
107                 struct pci_config_window *cfg = bridge->bus->sysdata;
108                 struct acpi_device *adev = to_acpi_device(cfg->parent);
109                 ACPI_COMPANION_SET(&bridge->dev, adev);
110         }
111
112         return 0;
113 }
114
115 static int pci_acpi_root_prepare_resources(struct acpi_pci_root_info *ci)
116 {
117         struct resource_entry *entry, *tmp;
118         int status;
119
120         status = acpi_pci_probe_root_resources(ci);
121         resource_list_for_each_entry_safe(entry, tmp, &ci->resources) {
122                 if (!(entry->res->flags & IORESOURCE_WINDOW))
123                         resource_list_destroy_entry(entry);
124         }
125         return status;
126 }
127
128 /*
129  * Lookup the bus range for the domain in MCFG, and set up config space
130  * mapping.
131  */
132 static struct pci_config_window *
133 pci_acpi_setup_ecam_mapping(struct acpi_pci_root *root)
134 {
135         struct device *dev = &root->device->dev;
136         struct resource *bus_res = &root->secondary;
137         u16 seg = root->segment;
138         struct pci_ecam_ops *ecam_ops;
139         struct resource cfgres;
140         struct acpi_device *adev;
141         struct pci_config_window *cfg;
142         int ret;
143
144         ret = pci_mcfg_lookup(root, &cfgres, &ecam_ops);
145         if (ret) {
146                 dev_err(dev, "%04x:%pR ECAM region not found\n", seg, bus_res);
147                 return NULL;
148         }
149
150         adev = acpi_resource_consumer(&cfgres);
151         if (adev)
152                 dev_info(dev, "ECAM area %pR reserved by %s\n", &cfgres,
153                          dev_name(&adev->dev));
154         else
155                 dev_warn(dev, FW_BUG "ECAM area %pR not reserved in ACPI namespace\n",
156                          &cfgres);
157
158         cfg = pci_ecam_create(dev, &cfgres, bus_res, ecam_ops);
159         if (IS_ERR(cfg)) {
160                 dev_err(dev, "%04x:%pR error %ld mapping ECAM\n", seg, bus_res,
161                         PTR_ERR(cfg));
162                 return NULL;
163         }
164
165         return cfg;
166 }
167
168 /* release_info: free resources allocated by init_info */
169 static void pci_acpi_generic_release_info(struct acpi_pci_root_info *ci)
170 {
171         struct acpi_pci_generic_root_info *ri;
172
173         ri = container_of(ci, struct acpi_pci_generic_root_info, common);
174         pci_ecam_free(ri->cfg);
175         kfree(ci->ops);
176         kfree(ri);
177 }
178
179 /* Interface called from ACPI code to setup PCI host controller */
180 struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root)
181 {
182         int node = acpi_get_node(root->device->handle);
183         struct acpi_pci_generic_root_info *ri;
184         struct pci_bus *bus, *child;
185         struct acpi_pci_root_ops *root_ops;
186
187         ri = kzalloc_node(sizeof(*ri), GFP_KERNEL, node);
188         if (!ri)
189                 return NULL;
190
191         root_ops = kzalloc_node(sizeof(*root_ops), GFP_KERNEL, node);
192         if (!root_ops)
193                 return NULL;
194
195         ri->cfg = pci_acpi_setup_ecam_mapping(root);
196         if (!ri->cfg) {
197                 kfree(ri);
198                 kfree(root_ops);
199                 return NULL;
200         }
201
202         root_ops->release_info = pci_acpi_generic_release_info;
203         root_ops->prepare_resources = pci_acpi_root_prepare_resources;
204         root_ops->pci_ops = &ri->cfg->ops->pci_ops;
205         bus = acpi_pci_root_create(root, root_ops, &ri->common, ri->cfg);
206         if (!bus)
207                 return NULL;
208
209         pci_bus_size_bridges(bus);
210         pci_bus_assign_resources(bus);
211
212         list_for_each_entry(child, &bus->children, node)
213                 pcie_bus_configure_settings(child);
214
215         return bus;
216 }
217
218 void pcibios_add_bus(struct pci_bus *bus)
219 {
220         acpi_pci_add_bus(bus);
221 }
222
223 void pcibios_remove_bus(struct pci_bus *bus)
224 {
225         acpi_pci_remove_bus(bus);
226 }
227
228 #endif