blob: 820e26b473f4aa220b5e52b7f0f10ee0afbe7f8d [file] [log] [blame]
Jayachandran C35ff9472016-05-10 17:19:51 +02001/*
2 * Copyright 2016 Broadcom
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2, as
6 * published by the Free Software Foundation (the "GPL").
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License version 2 (GPLv2) for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * version 2 (GPLv2) along with this source code.
15 */
16
17#include <linux/device.h>
18#include <linux/io.h>
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/pci.h>
Jayachandran C80955f92016-06-10 21:55:09 +020022#include <linux/pci-ecam.h>
Jayachandran C35ff9472016-05-10 17:19:51 +020023#include <linux/slab.h>
24
Jayachandran C35ff9472016-05-10 17:19:51 +020025/*
26 * On 64-bit systems, we do a single ioremap for the whole config space
27 * since we have enough virtual address range available. On 32-bit, we
28 * ioremap the config space for each bus individually.
29 */
30static const bool per_bus_mapping = !config_enabled(CONFIG_64BIT);
31
32/*
33 * Create a PCI config space window
34 * - reserve mem region
35 * - alloc struct pci_config_window with space for all mappings
36 * - ioremap the config space
37 */
38struct pci_config_window *pci_ecam_create(struct device *dev,
39 struct resource *cfgres, struct resource *busr,
40 struct pci_ecam_ops *ops)
41{
42 struct pci_config_window *cfg;
43 unsigned int bus_range, bus_range_max, bsz;
44 struct resource *conflict;
45 int i, err;
46
47 if (busr->start > busr->end)
48 return ERR_PTR(-EINVAL);
49
50 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
51 if (!cfg)
52 return ERR_PTR(-ENOMEM);
53
54 cfg->ops = ops;
55 cfg->busr.start = busr->start;
56 cfg->busr.end = busr->end;
57 cfg->busr.flags = IORESOURCE_BUS;
58 bus_range = resource_size(&cfg->busr);
59 bus_range_max = resource_size(cfgres) >> ops->bus_shift;
60 if (bus_range > bus_range_max) {
61 bus_range = bus_range_max;
62 cfg->busr.end = busr->start + bus_range - 1;
63 dev_warn(dev, "ECAM area %pR can only accommodate %pR (reduced from %pR desired)\n",
64 cfgres, &cfg->busr, busr);
65 }
66 bsz = 1 << ops->bus_shift;
67
68 cfg->res.start = cfgres->start;
69 cfg->res.end = cfgres->end;
70 cfg->res.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
71 cfg->res.name = "PCI ECAM";
72
73 conflict = request_resource_conflict(&iomem_resource, &cfg->res);
74 if (conflict) {
75 err = -EBUSY;
76 dev_err(dev, "can't claim ECAM area %pR: address conflict with %s %pR\n",
77 &cfg->res, conflict->name, conflict);
78 goto err_exit;
79 }
80
81 if (per_bus_mapping) {
82 cfg->winp = kcalloc(bus_range, sizeof(*cfg->winp), GFP_KERNEL);
83 if (!cfg->winp)
84 goto err_exit_malloc;
85 for (i = 0; i < bus_range; i++) {
86 cfg->winp[i] = ioremap(cfgres->start + i * bsz, bsz);
87 if (!cfg->winp[i])
88 goto err_exit_iomap;
89 }
90 } else {
91 cfg->win = ioremap(cfgres->start, bus_range * bsz);
92 if (!cfg->win)
93 goto err_exit_iomap;
94 }
95
96 if (ops->init) {
97 err = ops->init(dev, cfg);
98 if (err)
99 goto err_exit;
100 }
101 dev_info(dev, "ECAM at %pR for %pR\n", &cfg->res, &cfg->busr);
102 return cfg;
103
104err_exit_iomap:
105 dev_err(dev, "ECAM ioremap failed\n");
106err_exit_malloc:
107 err = -ENOMEM;
108err_exit:
109 pci_ecam_free(cfg);
110 return ERR_PTR(err);
111}
112
113void pci_ecam_free(struct pci_config_window *cfg)
114{
115 int i;
116
117 if (per_bus_mapping) {
118 if (cfg->winp) {
119 for (i = 0; i < resource_size(&cfg->busr); i++)
120 if (cfg->winp[i])
121 iounmap(cfg->winp[i]);
122 kfree(cfg->winp);
123 }
124 } else {
125 if (cfg->win)
126 iounmap(cfg->win);
127 }
128 if (cfg->res.parent)
129 release_resource(&cfg->res);
130 kfree(cfg);
131}
132
133/*
134 * Function to implement the pci_ops ->map_bus method
135 */
136void __iomem *pci_ecam_map_bus(struct pci_bus *bus, unsigned int devfn,
137 int where)
138{
139 struct pci_config_window *cfg = bus->sysdata;
140 unsigned int devfn_shift = cfg->ops->bus_shift - 8;
141 unsigned int busn = bus->number;
142 void __iomem *base;
143
144 if (busn < cfg->busr.start || busn > cfg->busr.end)
145 return NULL;
146
147 busn -= cfg->busr.start;
148 if (per_bus_mapping)
149 base = cfg->winp[busn];
150 else
151 base = cfg->win + (busn << cfg->ops->bus_shift);
152 return base + (devfn << devfn_shift) + where;
153}
154
155/* ECAM ops */
156struct pci_ecam_ops pci_generic_ecam_ops = {
157 .bus_shift = 20,
158 .pci_ops = {
159 .map_bus = pci_ecam_map_bus,
160 .read = pci_generic_config_read,
161 .write = pci_generic_config_write,
162 }
163};