linux/drivers/pci/host/pci-host-generic.c
<<
>>
Prefs
   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
  28struct gen_pci_cfg_bus_ops {
  29        u32 bus_shift;
  30        struct pci_ops ops;
  31};
  32
  33struct gen_pci_cfg_windows {
  34        struct resource                         res;
  35        struct resource                         *bus_range;
  36        void __iomem                            **win;
  37
  38        struct gen_pci_cfg_bus_ops              *ops;
  39};
  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{
  51        struct gen_pci *pci = bus->sysdata;
  52        resource_size_t idx = bus->number - pci->cfg.bus_range->start;
  53
  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,
  59        .ops            = {
  60                .map_bus        = gen_pci_map_cfg_bus_cam,
  61                .read           = pci_generic_config_read,
  62                .write          = pci_generic_config_write,
  63        }
  64};
  65
  66static void __iomem *gen_pci_map_cfg_bus_ecam(struct pci_bus *bus,
  67                                              unsigned int devfn,
  68                                              int where)
  69{
  70        struct gen_pci *pci = bus->sysdata;
  71        resource_size_t idx = bus->number - pci->cfg.bus_range->start;
  72
  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,
  78        .ops            = {
  79                .map_bus        = gen_pci_map_cfg_bus_ecam,
  80                .read           = pci_generic_config_read,
  81                .write          = pci_generic_config_write,
  82        }
  83};
  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
  96static void gen_pci_release_of_pci_ranges(struct gen_pci *pci)
  97{
  98        pci_free_resource_list(&pci->resources);
  99}
 100
 101static int gen_pci_parse_request_of_pci_ranges(struct gen_pci *pci)
 102{
 103        int err, res_valid = 0;
 104        struct device *dev = pci->host.dev.parent;
 105        struct device_node *np = dev->of_node;
 106        resource_size_t iobase;
 107        struct resource_entry *win;
 108
 109        err = of_pci_get_host_bridge_resources(np, 0, 0xff, &pci->resources,
 110                                               &iobase);
 111        if (err)
 112                return err;
 113
 114        resource_list_for_each_entry(win, &pci->resources) {
 115                struct resource *parent, *res = win->res;
 116
 117                switch (resource_type(res)) {
 118                case IORESOURCE_IO:
 119                        parent = &ioport_resource;
 120                        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                        }
 126                        break;
 127                case IORESOURCE_MEM:
 128                        parent = &iomem_resource;
 129                        res_valid |= !(res->flags & IORESOURCE_PREFETCH);
 130                        break;
 131                case IORESOURCE_BUS:
 132                        pci->cfg.bus_range = res;
 133                default:
 134                        continue;
 135                }
 136
 137                err = devm_request_resource(dev, parent, res);
 138                if (err)
 139                        goto out_release_res;
 140        }
 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;
 163        u32 sz = 1 << pci->cfg.ops->bus_shift;
 164
 165        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
 171        /* Limit the bus-range to fit within reg */
 172        bus_max = pci->cfg.bus_range->start +
 173                  (resource_size(&pci->cfg.res) >> pci->cfg.ops->bus_shift) - 1;
 174        pci->cfg.bus_range->end = min_t(resource_size_t,
 175                                        pci->cfg.bus_range->end, bus_max);
 176
 177        pci->cfg.win = devm_kcalloc(dev, resource_size(pci->cfg.bus_range),
 178                                    sizeof(*pci->cfg.win), GFP_KERNEL);
 179        if (!pci->cfg.win)
 180                return -ENOMEM;
 181
 182        /* 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
 188        bus_range = pci->cfg.bus_range;
 189        for (busn = bus_range->start; busn <= bus_range->end; ++busn) {
 190                u32 idx = busn - bus_range->start;
 191
 192                pci->cfg.win[idx] = devm_ioremap(dev,
 193                                                 pci->cfg.res.start + idx * sz,
 194                                                 sz);
 195                if (!pci->cfg.win[idx])
 196                        return -ENOMEM;
 197        }
 198
 199        return 0;
 200}
 201
 202static int gen_pci_probe(struct platform_device *pdev)
 203{
 204        int err;
 205        const char *type;
 206        const struct of_device_id *of_id;
 207        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);
 210        struct pci_bus *bus, *child;
 211
 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
 221        of_pci_check_probe_only();
 222
 223        of_id = of_match_node(gen_pci_of_match, np);
 224        pci->cfg.ops = (struct gen_pci_cfg_bus_ops *)of_id->data;
 225        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
 241        /* 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
 245
 246        bus = pci_scan_root_bus(dev, pci->cfg.bus_range->start,
 247                                &pci->cfg.ops->ops, pci, &pci->resources);
 248        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);
 264        return 0;
 265}
 266
 267static struct platform_driver gen_pci_driver = {
 268        .driver = {
 269                .name = "pci-host-generic",
 270                .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>");
 278MODULE_LICENSE("GPL v2");
 279