Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
David Daney | 7149b9f | 2016-03-11 15:18:38 -0600 | [diff] [blame] | 28 | #include "pci-host-common.h" |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 29 | |
| 30 | static void __iomem *gen_pci_map_cfg_bus_cam(struct pci_bus *bus, |
| 31 | unsigned int devfn, |
| 32 | int where) |
| 33 | { |
Jayachandran C | 499733e | 2015-08-05 02:23:38 +0530 | [diff] [blame] | 34 | struct gen_pci *pci = bus->sysdata; |
Lorenzo Pieralisi | dbf9826 | 2014-10-23 16:23:07 +0100 | [diff] [blame] | 35 | resource_size_t idx = bus->number - pci->cfg.bus_range->start; |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 36 | |
| 37 | return pci->cfg.win[idx] + ((devfn << 8) | where); |
| 38 | } |
| 39 | |
| 40 | static struct gen_pci_cfg_bus_ops gen_pci_cfg_cam_bus_ops = { |
| 41 | .bus_shift = 16, |
David Daney | 9a28033 | 2015-10-08 10:15:10 -0500 | [diff] [blame] | 42 | .ops = { |
| 43 | .map_bus = gen_pci_map_cfg_bus_cam, |
| 44 | .read = pci_generic_config_read, |
| 45 | .write = pci_generic_config_write, |
| 46 | } |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | static void __iomem *gen_pci_map_cfg_bus_ecam(struct pci_bus *bus, |
| 50 | unsigned int devfn, |
| 51 | int where) |
| 52 | { |
Jayachandran C | 499733e | 2015-08-05 02:23:38 +0530 | [diff] [blame] | 53 | struct gen_pci *pci = bus->sysdata; |
Lorenzo Pieralisi | dbf9826 | 2014-10-23 16:23:07 +0100 | [diff] [blame] | 54 | resource_size_t idx = bus->number - pci->cfg.bus_range->start; |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 55 | |
| 56 | return pci->cfg.win[idx] + ((devfn << 12) | where); |
| 57 | } |
| 58 | |
| 59 | static struct gen_pci_cfg_bus_ops gen_pci_cfg_ecam_bus_ops = { |
| 60 | .bus_shift = 20, |
David Daney | 9a28033 | 2015-10-08 10:15:10 -0500 | [diff] [blame] | 61 | .ops = { |
| 62 | .map_bus = gen_pci_map_cfg_bus_ecam, |
| 63 | .read = pci_generic_config_read, |
| 64 | .write = pci_generic_config_write, |
| 65 | } |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | static const struct of_device_id gen_pci_of_match[] = { |
| 69 | { .compatible = "pci-host-cam-generic", |
| 70 | .data = &gen_pci_cfg_cam_bus_ops }, |
| 71 | |
| 72 | { .compatible = "pci-host-ecam-generic", |
| 73 | .data = &gen_pci_cfg_ecam_bus_ops }, |
| 74 | |
| 75 | { }, |
| 76 | }; |
| 77 | MODULE_DEVICE_TABLE(of, gen_pci_of_match); |
| 78 | |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 79 | static void gen_pci_release_of_pci_ranges(struct gen_pci *pci) |
| 80 | { |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 81 | pci_free_resource_list(&pci->resources); |
| 82 | } |
| 83 | |
| 84 | static int gen_pci_parse_request_of_pci_ranges(struct gen_pci *pci) |
| 85 | { |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 86 | int err, res_valid = 0; |
| 87 | struct device *dev = pci->host.dev.parent; |
| 88 | struct device_node *np = dev->of_node; |
Lorenzo Pieralisi | dbf9826 | 2014-10-23 16:23:07 +0100 | [diff] [blame] | 89 | resource_size_t iobase; |
Jiang Liu | 14d76b6 | 2015-02-05 13:44:44 +0800 | [diff] [blame] | 90 | struct resource_entry *win; |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 91 | |
Lorenzo Pieralisi | dbf9826 | 2014-10-23 16:23:07 +0100 | [diff] [blame] | 92 | err = of_pci_get_host_bridge_resources(np, 0, 0xff, &pci->resources, |
| 93 | &iobase); |
| 94 | if (err) |
| 95 | return err; |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 96 | |
Jiang Liu | 14d76b6 | 2015-02-05 13:44:44 +0800 | [diff] [blame] | 97 | resource_list_for_each_entry(win, &pci->resources) { |
Lorenzo Pieralisi | dbf9826 | 2014-10-23 16:23:07 +0100 | [diff] [blame] | 98 | struct resource *parent, *res = win->res; |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 99 | |
Lorenzo Pieralisi | dbf9826 | 2014-10-23 16:23:07 +0100 | [diff] [blame] | 100 | switch (resource_type(res)) { |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 101 | case IORESOURCE_IO: |
| 102 | parent = &ioport_resource; |
Lorenzo Pieralisi | dbf9826 | 2014-10-23 16:23:07 +0100 | [diff] [blame] | 103 | err = pci_remap_iospace(res, iobase); |
| 104 | if (err) { |
| 105 | dev_warn(dev, "error %d: failed to map resource %pR\n", |
| 106 | err, res); |
| 107 | continue; |
| 108 | } |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 109 | break; |
| 110 | case IORESOURCE_MEM: |
| 111 | parent = &iomem_resource; |
Lorenzo Pieralisi | dbf9826 | 2014-10-23 16:23:07 +0100 | [diff] [blame] | 112 | res_valid |= !(res->flags & IORESOURCE_PREFETCH); |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 113 | break; |
Lorenzo Pieralisi | dbf9826 | 2014-10-23 16:23:07 +0100 | [diff] [blame] | 114 | case IORESOURCE_BUS: |
| 115 | pci->cfg.bus_range = res; |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 116 | default: |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 117 | continue; |
| 118 | } |
| 119 | |
Lorenzo Pieralisi | dbf9826 | 2014-10-23 16:23:07 +0100 | [diff] [blame] | 120 | err = devm_request_resource(dev, parent, res); |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 121 | if (err) |
| 122 | goto out_release_res; |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | if (!res_valid) { |
| 126 | dev_err(dev, "non-prefetchable memory resource required\n"); |
| 127 | err = -EINVAL; |
| 128 | goto out_release_res; |
| 129 | } |
| 130 | |
| 131 | return 0; |
| 132 | |
| 133 | out_release_res: |
| 134 | gen_pci_release_of_pci_ranges(pci); |
| 135 | return err; |
| 136 | } |
| 137 | |
| 138 | static int gen_pci_parse_map_cfg_windows(struct gen_pci *pci) |
| 139 | { |
| 140 | int err; |
| 141 | u8 bus_max; |
| 142 | resource_size_t busn; |
| 143 | struct resource *bus_range; |
| 144 | struct device *dev = pci->host.dev.parent; |
| 145 | struct device_node *np = dev->of_node; |
David Daney | f6225c3 | 2015-10-08 12:54:16 -0700 | [diff] [blame] | 146 | u32 sz = 1 << pci->cfg.ops->bus_shift; |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 147 | |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 148 | err = of_address_to_resource(np, 0, &pci->cfg.res); |
| 149 | if (err) { |
| 150 | dev_err(dev, "missing \"reg\" property\n"); |
| 151 | return err; |
| 152 | } |
| 153 | |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 154 | /* Limit the bus-range to fit within reg */ |
Lorenzo Pieralisi | dbf9826 | 2014-10-23 16:23:07 +0100 | [diff] [blame] | 155 | bus_max = pci->cfg.bus_range->start + |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 156 | (resource_size(&pci->cfg.res) >> pci->cfg.ops->bus_shift) - 1; |
Lorenzo Pieralisi | dbf9826 | 2014-10-23 16:23:07 +0100 | [diff] [blame] | 157 | pci->cfg.bus_range->end = min_t(resource_size_t, |
| 158 | pci->cfg.bus_range->end, bus_max); |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 159 | |
Lorenzo Pieralisi | dbf9826 | 2014-10-23 16:23:07 +0100 | [diff] [blame] | 160 | pci->cfg.win = devm_kcalloc(dev, resource_size(pci->cfg.bus_range), |
Lorenzo Pieralisi | a5525b2 | 2014-10-23 16:23:06 +0100 | [diff] [blame] | 161 | sizeof(*pci->cfg.win), GFP_KERNEL); |
| 162 | if (!pci->cfg.win) |
| 163 | return -ENOMEM; |
| 164 | |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 165 | /* Map our Configuration Space windows */ |
| 166 | if (!devm_request_mem_region(dev, pci->cfg.res.start, |
| 167 | resource_size(&pci->cfg.res), |
| 168 | "Configuration Space")) |
| 169 | return -ENOMEM; |
| 170 | |
Lorenzo Pieralisi | dbf9826 | 2014-10-23 16:23:07 +0100 | [diff] [blame] | 171 | bus_range = pci->cfg.bus_range; |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 172 | for (busn = bus_range->start; busn <= bus_range->end; ++busn) { |
| 173 | u32 idx = busn - bus_range->start; |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 174 | |
| 175 | pci->cfg.win[idx] = devm_ioremap(dev, |
David Daney | f6225c3 | 2015-10-08 12:54:16 -0700 | [diff] [blame] | 176 | pci->cfg.res.start + idx * sz, |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 177 | sz); |
| 178 | if (!pci->cfg.win[idx]) |
| 179 | return -ENOMEM; |
| 180 | } |
| 181 | |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 182 | return 0; |
| 183 | } |
| 184 | |
David Daney | d51b3710 | 2016-03-11 15:25:13 -0600 | [diff] [blame^] | 185 | static int pci_host_common_probe(struct platform_device *pdev, |
| 186 | struct gen_pci *pci) |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 187 | { |
| 188 | int err; |
| 189 | const char *type; |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 190 | struct device *dev = &pdev->dev; |
| 191 | struct device_node *np = dev->of_node; |
Jayachandran C | 499733e | 2015-08-05 02:23:38 +0530 | [diff] [blame] | 192 | struct pci_bus *bus, *child; |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 193 | |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 194 | type = of_get_property(np, "device_type", NULL); |
| 195 | if (!type || strcmp(type, "pci")) { |
| 196 | dev_err(dev, "invalid \"device_type\" %s\n", type); |
| 197 | return -EINVAL; |
| 198 | } |
| 199 | |
Marc Zyngier | a07245d | 2015-09-04 17:50:09 +0100 | [diff] [blame] | 200 | of_pci_check_probe_only(); |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 201 | |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 202 | pci->host.dev.parent = dev; |
| 203 | INIT_LIST_HEAD(&pci->host.windows); |
| 204 | INIT_LIST_HEAD(&pci->resources); |
| 205 | |
| 206 | /* Parse our PCI ranges and request their resources */ |
| 207 | err = gen_pci_parse_request_of_pci_ranges(pci); |
| 208 | if (err) |
| 209 | return err; |
| 210 | |
| 211 | /* Parse and map our Configuration Space windows */ |
| 212 | err = gen_pci_parse_map_cfg_windows(pci); |
| 213 | if (err) { |
| 214 | gen_pci_release_of_pci_ranges(pci); |
| 215 | return err; |
| 216 | } |
| 217 | |
Jayachandran C | 499733e | 2015-08-05 02:23:38 +0530 | [diff] [blame] | 218 | /* Do not reassign resources if probe only */ |
| 219 | if (!pci_has_flag(PCI_PROBE_ONLY)) |
| 220 | pci_add_flags(PCI_REASSIGN_ALL_RSRC | PCI_REASSIGN_ALL_BUS); |
| 221 | |
David Daney | 47ddb94 | 2015-10-08 10:24:41 -0500 | [diff] [blame] | 222 | |
| 223 | bus = pci_scan_root_bus(dev, pci->cfg.bus_range->start, |
David Daney | 9a28033 | 2015-10-08 10:15:10 -0500 | [diff] [blame] | 224 | &pci->cfg.ops->ops, pci, &pci->resources); |
Jayachandran C | 499733e | 2015-08-05 02:23:38 +0530 | [diff] [blame] | 225 | if (!bus) { |
| 226 | dev_err(dev, "Scanning rootbus failed"); |
| 227 | return -ENODEV; |
| 228 | } |
| 229 | |
| 230 | pci_fixup_irqs(pci_common_swizzle, of_irq_parse_and_map_pci); |
| 231 | |
| 232 | if (!pci_has_flag(PCI_PROBE_ONLY)) { |
| 233 | pci_bus_size_bridges(bus); |
| 234 | pci_bus_assign_resources(bus); |
| 235 | |
| 236 | list_for_each_entry(child, &bus->children, node) |
| 237 | pcie_bus_configure_settings(child); |
| 238 | } |
| 239 | |
| 240 | pci_bus_add_devices(bus); |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 241 | return 0; |
| 242 | } |
| 243 | |
David Daney | d51b3710 | 2016-03-11 15:25:13 -0600 | [diff] [blame^] | 244 | static int gen_pci_probe(struct platform_device *pdev) |
| 245 | { |
| 246 | struct device *dev = &pdev->dev; |
| 247 | const struct of_device_id *of_id; |
| 248 | struct gen_pci *pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL); |
| 249 | |
| 250 | if (!pci) |
| 251 | return -ENOMEM; |
| 252 | |
| 253 | of_id = of_match_node(gen_pci_of_match, dev->of_node); |
| 254 | pci->cfg.ops = (struct gen_pci_cfg_bus_ops *)of_id->data; |
| 255 | |
| 256 | return pci_host_common_probe(pdev, pci); |
| 257 | } |
| 258 | |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 259 | static struct platform_driver gen_pci_driver = { |
| 260 | .driver = { |
| 261 | .name = "pci-host-generic", |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 262 | .of_match_table = gen_pci_of_match, |
| 263 | }, |
| 264 | .probe = gen_pci_probe, |
| 265 | }; |
| 266 | module_platform_driver(gen_pci_driver); |
| 267 | |
| 268 | MODULE_DESCRIPTION("Generic PCI host driver"); |
| 269 | MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>"); |
Bjorn Helgaas | eed6542 | 2014-07-15 15:07:46 -0600 | [diff] [blame] | 270 | MODULE_LICENSE("GPL v2"); |