blob: 1652bc70b1453663cc4a7614d68a3e7c5ea12524 [file] [log] [blame]
Will Deaconce292992013-11-22 16:14:41 +00001/*
2 * Simple, generic PCI host controller driver targetting firmware-initialised
3 * systems and virtual machines (e.g. the PCI emulation provided by kvmtool).
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 * Copyright (C) 2014 ARM Limited
18 *
19 * Author: Will Deacon <will.deacon@arm.com>
20 */
21
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/of_address.h>
25#include <linux/of_pci.h>
26#include <linux/platform_device.h>
27
28struct gen_pci_cfg_bus_ops {
29 u32 bus_shift;
David Daney9a280332015-10-08 10:15:10 -050030 struct pci_ops ops;
Will Deaconce292992013-11-22 16:14:41 +000031};
32
33struct gen_pci_cfg_windows {
34 struct resource res;
Lorenzo Pieralisidbf98262014-10-23 16:23:07 +010035 struct resource *bus_range;
Will Deaconce292992013-11-22 16:14:41 +000036 void __iomem **win;
37
David Daney9a280332015-10-08 10:15:10 -050038 struct gen_pci_cfg_bus_ops *ops;
Will Deaconce292992013-11-22 16:14:41 +000039};
40
41struct gen_pci {
42 struct pci_host_bridge host;
43 struct gen_pci_cfg_windows cfg;
44 struct list_head resources;
45};
46
47static void __iomem *gen_pci_map_cfg_bus_cam(struct pci_bus *bus,
48 unsigned int devfn,
49 int where)
50{
Jayachandran C499733e2015-08-05 02:23:38 +053051 struct gen_pci *pci = bus->sysdata;
Lorenzo Pieralisidbf98262014-10-23 16:23:07 +010052 resource_size_t idx = bus->number - pci->cfg.bus_range->start;
Will Deaconce292992013-11-22 16:14:41 +000053
54 return pci->cfg.win[idx] + ((devfn << 8) | where);
55}
56
57static struct gen_pci_cfg_bus_ops gen_pci_cfg_cam_bus_ops = {
58 .bus_shift = 16,
David Daney9a280332015-10-08 10:15:10 -050059 .ops = {
60 .map_bus = gen_pci_map_cfg_bus_cam,
61 .read = pci_generic_config_read,
62 .write = pci_generic_config_write,
63 }
Will Deaconce292992013-11-22 16:14:41 +000064};
65
66static void __iomem *gen_pci_map_cfg_bus_ecam(struct pci_bus *bus,
67 unsigned int devfn,
68 int where)
69{
Jayachandran C499733e2015-08-05 02:23:38 +053070 struct gen_pci *pci = bus->sysdata;
Lorenzo Pieralisidbf98262014-10-23 16:23:07 +010071 resource_size_t idx = bus->number - pci->cfg.bus_range->start;
Will Deaconce292992013-11-22 16:14:41 +000072
73 return pci->cfg.win[idx] + ((devfn << 12) | where);
74}
75
76static struct gen_pci_cfg_bus_ops gen_pci_cfg_ecam_bus_ops = {
77 .bus_shift = 20,
David Daney9a280332015-10-08 10:15:10 -050078 .ops = {
79 .map_bus = gen_pci_map_cfg_bus_ecam,
80 .read = pci_generic_config_read,
81 .write = pci_generic_config_write,
82 }
Will Deaconce292992013-11-22 16:14:41 +000083};
84
85static const struct of_device_id gen_pci_of_match[] = {
86 { .compatible = "pci-host-cam-generic",
87 .data = &gen_pci_cfg_cam_bus_ops },
88
89 { .compatible = "pci-host-ecam-generic",
90 .data = &gen_pci_cfg_ecam_bus_ops },
91
92 { },
93};
94MODULE_DEVICE_TABLE(of, gen_pci_of_match);
95
Will Deaconce292992013-11-22 16:14:41 +000096static void gen_pci_release_of_pci_ranges(struct gen_pci *pci)
97{
Will Deaconce292992013-11-22 16:14:41 +000098 pci_free_resource_list(&pci->resources);
99}
100
101static int gen_pci_parse_request_of_pci_ranges(struct gen_pci *pci)
102{
Will Deaconce292992013-11-22 16:14:41 +0000103 int err, res_valid = 0;
104 struct device *dev = pci->host.dev.parent;
105 struct device_node *np = dev->of_node;
Lorenzo Pieralisidbf98262014-10-23 16:23:07 +0100106 resource_size_t iobase;
Jiang Liu14d76b62015-02-05 13:44:44 +0800107 struct resource_entry *win;
Will Deaconce292992013-11-22 16:14:41 +0000108
Lorenzo Pieralisidbf98262014-10-23 16:23:07 +0100109 err = of_pci_get_host_bridge_resources(np, 0, 0xff, &pci->resources,
110 &iobase);
111 if (err)
112 return err;
Will Deaconce292992013-11-22 16:14:41 +0000113
Jiang Liu14d76b62015-02-05 13:44:44 +0800114 resource_list_for_each_entry(win, &pci->resources) {
Lorenzo Pieralisidbf98262014-10-23 16:23:07 +0100115 struct resource *parent, *res = win->res;
Will Deaconce292992013-11-22 16:14:41 +0000116
Lorenzo Pieralisidbf98262014-10-23 16:23:07 +0100117 switch (resource_type(res)) {
Will Deaconce292992013-11-22 16:14:41 +0000118 case IORESOURCE_IO:
119 parent = &ioport_resource;
Lorenzo Pieralisidbf98262014-10-23 16:23:07 +0100120 err = pci_remap_iospace(res, iobase);
121 if (err) {
122 dev_warn(dev, "error %d: failed to map resource %pR\n",
123 err, res);
124 continue;
125 }
Will Deaconce292992013-11-22 16:14:41 +0000126 break;
127 case IORESOURCE_MEM:
128 parent = &iomem_resource;
Lorenzo Pieralisidbf98262014-10-23 16:23:07 +0100129 res_valid |= !(res->flags & IORESOURCE_PREFETCH);
Will Deaconce292992013-11-22 16:14:41 +0000130 break;
Lorenzo Pieralisidbf98262014-10-23 16:23:07 +0100131 case IORESOURCE_BUS:
132 pci->cfg.bus_range = res;
Will Deaconce292992013-11-22 16:14:41 +0000133 default:
Will Deaconce292992013-11-22 16:14:41 +0000134 continue;
135 }
136
Lorenzo Pieralisidbf98262014-10-23 16:23:07 +0100137 err = devm_request_resource(dev, parent, res);
Will Deaconce292992013-11-22 16:14:41 +0000138 if (err)
139 goto out_release_res;
Will Deaconce292992013-11-22 16:14:41 +0000140 }
141
142 if (!res_valid) {
143 dev_err(dev, "non-prefetchable memory resource required\n");
144 err = -EINVAL;
145 goto out_release_res;
146 }
147
148 return 0;
149
150out_release_res:
151 gen_pci_release_of_pci_ranges(pci);
152 return err;
153}
154
155static int gen_pci_parse_map_cfg_windows(struct gen_pci *pci)
156{
157 int err;
158 u8 bus_max;
159 resource_size_t busn;
160 struct resource *bus_range;
161 struct device *dev = pci->host.dev.parent;
162 struct device_node *np = dev->of_node;
David Daneyf6225c32015-10-08 12:54:16 -0700163 u32 sz = 1 << pci->cfg.ops->bus_shift;
Will Deaconce292992013-11-22 16:14:41 +0000164
Will Deaconce292992013-11-22 16:14:41 +0000165 err = of_address_to_resource(np, 0, &pci->cfg.res);
166 if (err) {
167 dev_err(dev, "missing \"reg\" property\n");
168 return err;
169 }
170
Will Deaconce292992013-11-22 16:14:41 +0000171 /* Limit the bus-range to fit within reg */
Lorenzo Pieralisidbf98262014-10-23 16:23:07 +0100172 bus_max = pci->cfg.bus_range->start +
Will Deaconce292992013-11-22 16:14:41 +0000173 (resource_size(&pci->cfg.res) >> pci->cfg.ops->bus_shift) - 1;
Lorenzo Pieralisidbf98262014-10-23 16:23:07 +0100174 pci->cfg.bus_range->end = min_t(resource_size_t,
175 pci->cfg.bus_range->end, bus_max);
Will Deaconce292992013-11-22 16:14:41 +0000176
Lorenzo Pieralisidbf98262014-10-23 16:23:07 +0100177 pci->cfg.win = devm_kcalloc(dev, resource_size(pci->cfg.bus_range),
Lorenzo Pieralisia5525b22014-10-23 16:23:06 +0100178 sizeof(*pci->cfg.win), GFP_KERNEL);
179 if (!pci->cfg.win)
180 return -ENOMEM;
181
Will Deaconce292992013-11-22 16:14:41 +0000182 /* Map our Configuration Space windows */
183 if (!devm_request_mem_region(dev, pci->cfg.res.start,
184 resource_size(&pci->cfg.res),
185 "Configuration Space"))
186 return -ENOMEM;
187
Lorenzo Pieralisidbf98262014-10-23 16:23:07 +0100188 bus_range = pci->cfg.bus_range;
Will Deaconce292992013-11-22 16:14:41 +0000189 for (busn = bus_range->start; busn <= bus_range->end; ++busn) {
190 u32 idx = busn - bus_range->start;
Will Deaconce292992013-11-22 16:14:41 +0000191
192 pci->cfg.win[idx] = devm_ioremap(dev,
David Daneyf6225c32015-10-08 12:54:16 -0700193 pci->cfg.res.start + idx * sz,
Will Deaconce292992013-11-22 16:14:41 +0000194 sz);
195 if (!pci->cfg.win[idx])
196 return -ENOMEM;
197 }
198
Will Deaconce292992013-11-22 16:14:41 +0000199 return 0;
200}
201
Will Deaconce292992013-11-22 16:14:41 +0000202static int gen_pci_probe(struct platform_device *pdev)
203{
204 int err;
205 const char *type;
206 const struct of_device_id *of_id;
Will Deaconce292992013-11-22 16:14:41 +0000207 struct device *dev = &pdev->dev;
208 struct device_node *np = dev->of_node;
209 struct gen_pci *pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
Jayachandran C499733e2015-08-05 02:23:38 +0530210 struct pci_bus *bus, *child;
Will Deaconce292992013-11-22 16:14:41 +0000211
212 if (!pci)
213 return -ENOMEM;
214
215 type = of_get_property(np, "device_type", NULL);
216 if (!type || strcmp(type, "pci")) {
217 dev_err(dev, "invalid \"device_type\" %s\n", type);
218 return -EINVAL;
219 }
220
Marc Zyngiera07245d2015-09-04 17:50:09 +0100221 of_pci_check_probe_only();
Will Deaconce292992013-11-22 16:14:41 +0000222
223 of_id = of_match_node(gen_pci_of_match, np);
David Daney9a280332015-10-08 10:15:10 -0500224 pci->cfg.ops = (struct gen_pci_cfg_bus_ops *)of_id->data;
Will Deaconce292992013-11-22 16:14:41 +0000225 pci->host.dev.parent = dev;
226 INIT_LIST_HEAD(&pci->host.windows);
227 INIT_LIST_HEAD(&pci->resources);
228
229 /* Parse our PCI ranges and request their resources */
230 err = gen_pci_parse_request_of_pci_ranges(pci);
231 if (err)
232 return err;
233
234 /* Parse and map our Configuration Space windows */
235 err = gen_pci_parse_map_cfg_windows(pci);
236 if (err) {
237 gen_pci_release_of_pci_ranges(pci);
238 return err;
239 }
240
Jayachandran C499733e2015-08-05 02:23:38 +0530241 /* Do not reassign resources if probe only */
242 if (!pci_has_flag(PCI_PROBE_ONLY))
243 pci_add_flags(PCI_REASSIGN_ALL_RSRC | PCI_REASSIGN_ALL_BUS);
244
David Daney47ddb942015-10-08 10:24:41 -0500245
246 bus = pci_scan_root_bus(dev, pci->cfg.bus_range->start,
David Daney9a280332015-10-08 10:15:10 -0500247 &pci->cfg.ops->ops, pci, &pci->resources);
Jayachandran C499733e2015-08-05 02:23:38 +0530248 if (!bus) {
249 dev_err(dev, "Scanning rootbus failed");
250 return -ENODEV;
251 }
252
253 pci_fixup_irqs(pci_common_swizzle, of_irq_parse_and_map_pci);
254
255 if (!pci_has_flag(PCI_PROBE_ONLY)) {
256 pci_bus_size_bridges(bus);
257 pci_bus_assign_resources(bus);
258
259 list_for_each_entry(child, &bus->children, node)
260 pcie_bus_configure_settings(child);
261 }
262
263 pci_bus_add_devices(bus);
Will Deaconce292992013-11-22 16:14:41 +0000264 return 0;
265}
266
267static struct platform_driver gen_pci_driver = {
268 .driver = {
269 .name = "pci-host-generic",
Will Deaconce292992013-11-22 16:14:41 +0000270 .of_match_table = gen_pci_of_match,
271 },
272 .probe = gen_pci_probe,
273};
274module_platform_driver(gen_pci_driver);
275
276MODULE_DESCRIPTION("Generic PCI host driver");
277MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
Bjorn Helgaaseed65422014-07-15 15:07:46 -0600278MODULE_LICENSE("GPL v2");