blob: ddbcfab7efdfbe4f03136427f517f9f0ce4d2046 [file] [log] [blame]
Linas Vepstas5d5a0932005-11-03 18:53:07 -06001/*
Linas Vepstas5d5a0932005-11-03 18:53:07 -06002 * PCI address cache; allows the lookup of PCI devices based on I/O address
3 *
Linas Vepstas3c8c90a2007-05-24 03:28:01 +10004 * Copyright IBM Corporation 2004
5 * Copyright Linas Vepstas <linas@austin.ibm.com> 2004
Linas Vepstas5d5a0932005-11-03 18:53:07 -06006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#include <linux/list.h>
23#include <linux/pci.h>
24#include <linux/rbtree.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Linas Vepstas5d5a0932005-11-03 18:53:07 -060026#include <linux/spinlock.h>
Arun Sharma600634972011-07-26 16:09:06 -070027#include <linux/atomic.h>
Linas Vepstas5d5a0932005-11-03 18:53:07 -060028#include <asm/pci-bridge.h>
29#include <asm/ppc-pci.h>
Linas Vepstas5d5a0932005-11-03 18:53:07 -060030
Linas Vepstas5d5a0932005-11-03 18:53:07 -060031
32/**
33 * The pci address cache subsystem. This subsystem places
34 * PCI device address resources into a red-black tree, sorted
35 * according to the address range, so that given only an i/o
36 * address, the corresponding PCI device can be **quickly**
37 * found. It is safe to perform an address lookup in an interrupt
38 * context; this ability is an important feature.
39 *
40 * Currently, the only customer of this code is the EEH subsystem;
41 * thus, this code has been somewhat tailored to suit EEH better.
42 * In particular, the cache does *not* hold the addresses of devices
43 * for which EEH is not enabled.
44 *
45 * (Implementation Note: The RB tree seems to be better/faster
46 * than any hash algo I could think of for this problem, even
47 * with the penalty of slow pointer chases for d-cache misses).
48 */
Gavin Shan29f8bf12012-02-27 20:04:02 +000049struct pci_io_addr_range {
Linas Vepstas5d5a0932005-11-03 18:53:07 -060050 struct rb_node rb_node;
Wei Yang37213522015-04-27 09:25:09 +080051 resource_size_t addr_lo;
52 resource_size_t addr_hi;
Gavin Shanf8f7d632012-09-07 22:44:22 +000053 struct eeh_dev *edev;
Linas Vepstas5d5a0932005-11-03 18:53:07 -060054 struct pci_dev *pcidev;
Wei Yang37213522015-04-27 09:25:09 +080055 unsigned long flags;
Linas Vepstas5d5a0932005-11-03 18:53:07 -060056};
57
Gavin Shan29f8bf12012-02-27 20:04:02 +000058static struct pci_io_addr_cache {
Linas Vepstas5d5a0932005-11-03 18:53:07 -060059 struct rb_root rb_root;
60 spinlock_t piar_lock;
61} pci_io_addr_cache_root;
62
Gavin Shan3ab96a02012-09-07 22:44:23 +000063static inline struct eeh_dev *__eeh_addr_cache_get_device(unsigned long addr)
Linas Vepstas5d5a0932005-11-03 18:53:07 -060064{
65 struct rb_node *n = pci_io_addr_cache_root.rb_root.rb_node;
66
67 while (n) {
68 struct pci_io_addr_range *piar;
69 piar = rb_entry(n, struct pci_io_addr_range, rb_node);
70
Gavin Shan0ba17882013-07-24 10:24:51 +080071 if (addr < piar->addr_lo)
Linas Vepstas5d5a0932005-11-03 18:53:07 -060072 n = n->rb_left;
Gavin Shan0ba17882013-07-24 10:24:51 +080073 else if (addr > piar->addr_hi)
74 n = n->rb_right;
75 else
76 return piar->edev;
Linas Vepstas5d5a0932005-11-03 18:53:07 -060077 }
78
79 return NULL;
80}
81
82/**
Gavin Shan3ab96a02012-09-07 22:44:23 +000083 * eeh_addr_cache_get_dev - Get device, given only address
Linas Vepstas5d5a0932005-11-03 18:53:07 -060084 * @addr: mmio (PIO) phys address or i/o port number
85 *
86 * Given an mmio phys address, or a port number, find a pci device
87 * that implements this address. Be sure to pci_dev_put the device
88 * when finished. I/O port numbers are assumed to be offset
89 * from zero (that is, they do *not* have pci_io_addr added in).
90 * It is safe to call this function within an interrupt.
91 */
Gavin Shan3ab96a02012-09-07 22:44:23 +000092struct eeh_dev *eeh_addr_cache_get_dev(unsigned long addr)
Linas Vepstas5d5a0932005-11-03 18:53:07 -060093{
Gavin Shanf8f7d632012-09-07 22:44:22 +000094 struct eeh_dev *edev;
Linas Vepstas5d5a0932005-11-03 18:53:07 -060095 unsigned long flags;
96
97 spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
Gavin Shan3ab96a02012-09-07 22:44:23 +000098 edev = __eeh_addr_cache_get_device(addr);
Linas Vepstas5d5a0932005-11-03 18:53:07 -060099 spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
Gavin Shanf8f7d632012-09-07 22:44:22 +0000100 return edev;
Linas Vepstas5d5a0932005-11-03 18:53:07 -0600101}
102
103#ifdef DEBUG
104/*
105 * Handy-dandy debug print routine, does nothing more
106 * than print out the contents of our addr cache.
107 */
Gavin Shan3ab96a02012-09-07 22:44:23 +0000108static void eeh_addr_cache_print(struct pci_io_addr_cache *cache)
Linas Vepstas5d5a0932005-11-03 18:53:07 -0600109{
110 struct rb_node *n;
111 int cnt = 0;
112
113 n = rb_first(&cache->rb_root);
114 while (n) {
115 struct pci_io_addr_range *piar;
116 piar = rb_entry(n, struct pci_io_addr_range, rb_node);
Gavin Shan3ab96a02012-09-07 22:44:23 +0000117 pr_debug("PCI: %s addr range %d [%lx-%lx]: %s\n",
Linas Vepstas5d5a0932005-11-03 18:53:07 -0600118 (piar->flags & IORESOURCE_IO) ? "i/o" : "mem", cnt,
119 piar->addr_lo, piar->addr_hi, pci_name(piar->pcidev));
120 cnt++;
121 n = rb_next(n);
122 }
123}
124#endif
125
126/* Insert address range into the rb tree. */
127static struct pci_io_addr_range *
Wei Yang37213522015-04-27 09:25:09 +0800128eeh_addr_cache_insert(struct pci_dev *dev, resource_size_t alo,
129 resource_size_t ahi, unsigned long flags)
Linas Vepstas5d5a0932005-11-03 18:53:07 -0600130{
131 struct rb_node **p = &pci_io_addr_cache_root.rb_root.rb_node;
132 struct rb_node *parent = NULL;
133 struct pci_io_addr_range *piar;
134
135 /* Walk tree, find a place to insert into tree */
136 while (*p) {
137 parent = *p;
138 piar = rb_entry(parent, struct pci_io_addr_range, rb_node);
139 if (ahi < piar->addr_lo) {
140 p = &parent->rb_left;
141 } else if (alo > piar->addr_hi) {
142 p = &parent->rb_right;
143 } else {
144 if (dev != piar->pcidev ||
145 alo != piar->addr_lo || ahi != piar->addr_hi) {
Gavin Shan0dae2742014-07-17 14:41:41 +1000146 pr_warn("PIAR: overlapping address range\n");
Linas Vepstas5d5a0932005-11-03 18:53:07 -0600147 }
148 return piar;
149 }
150 }
Gavin Shan7e4bbaf2012-09-07 22:44:03 +0000151 piar = kzalloc(sizeof(struct pci_io_addr_range), GFP_ATOMIC);
Linas Vepstas5d5a0932005-11-03 18:53:07 -0600152 if (!piar)
153 return NULL;
154
155 piar->addr_lo = alo;
156 piar->addr_hi = ahi;
Gavin Shanf8f7d632012-09-07 22:44:22 +0000157 piar->edev = pci_dev_to_eeh_dev(dev);
Linas Vepstas5d5a0932005-11-03 18:53:07 -0600158 piar->pcidev = dev;
159 piar->flags = flags;
160
161#ifdef DEBUG
Gavin Shan3ab96a02012-09-07 22:44:23 +0000162 pr_debug("PIAR: insert range=[%lx:%lx] dev=%s\n",
Gavin Shan29f8bf12012-02-27 20:04:02 +0000163 alo, ahi, pci_name(dev));
Linas Vepstas5d5a0932005-11-03 18:53:07 -0600164#endif
165
166 rb_link_node(&piar->rb_node, parent, p);
167 rb_insert_color(&piar->rb_node, &pci_io_addr_cache_root.rb_root);
168
169 return piar;
170}
171
Gavin Shan3ab96a02012-09-07 22:44:23 +0000172static void __eeh_addr_cache_insert_dev(struct pci_dev *dev)
Linas Vepstas5d5a0932005-11-03 18:53:07 -0600173{
Gavin Shanc6406d82015-03-17 16:15:08 +1100174 struct pci_dn *pdn;
Gavin Shand50a7d42012-02-27 20:04:06 +0000175 struct eeh_dev *edev;
Linas Vepstas5d5a0932005-11-03 18:53:07 -0600176 int i;
Linas Vepstas5d5a0932005-11-03 18:53:07 -0600177
Gavin Shanc6406d82015-03-17 16:15:08 +1100178 pdn = pci_get_pdn_by_devfn(dev->bus, dev->devfn);
179 if (!pdn) {
Gavin Shan0dae2742014-07-17 14:41:41 +1000180 pr_warn("PCI: no pci dn found for dev=%s\n",
181 pci_name(dev));
Linas Vepstas5d5a0932005-11-03 18:53:07 -0600182 return;
183 }
184
Gavin Shanc6406d82015-03-17 16:15:08 +1100185 edev = pdn_to_eeh_dev(pdn);
Gavin Shand50a7d42012-02-27 20:04:06 +0000186 if (!edev) {
Gavin Shanc6406d82015-03-17 16:15:08 +1100187 pr_warn("PCI: no EEH dev found for %s\n",
188 pci_name(dev));
Gavin Shand50a7d42012-02-27 20:04:06 +0000189 return;
190 }
191
Linas Vepstas5d5a0932005-11-03 18:53:07 -0600192 /* Skip any devices for which EEH is not enabled. */
Gavin Shan05b17212014-07-17 14:41:38 +1000193 if (!edev->pe) {
Gavin Shanc6406d82015-03-17 16:15:08 +1100194 dev_dbg(&dev->dev, "EEH: Skip building address cache\n");
Linas Vepstas5d5a0932005-11-03 18:53:07 -0600195 return;
196 }
197
Wei Yang51c0e872016-03-04 10:53:06 +1100198 /*
199 * Walk resources on this device, poke the first 7 (6 normal BAR and 1
200 * ROM BAR) into the tree.
201 */
202 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
Wei Yang37213522015-04-27 09:25:09 +0800203 resource_size_t start = pci_resource_start(dev,i);
204 resource_size_t end = pci_resource_end(dev,i);
205 unsigned long flags = pci_resource_flags(dev,i);
Linas Vepstas5d5a0932005-11-03 18:53:07 -0600206
207 /* We are interested only bus addresses, not dma or other stuff */
208 if (0 == (flags & (IORESOURCE_IO | IORESOURCE_MEM)))
209 continue;
210 if (start == 0 || ~start == 0 || end == 0 || ~end == 0)
211 continue;
Gavin Shan3ab96a02012-09-07 22:44:23 +0000212 eeh_addr_cache_insert(dev, start, end, flags);
Linas Vepstas5d5a0932005-11-03 18:53:07 -0600213 }
Linas Vepstas5d5a0932005-11-03 18:53:07 -0600214}
215
216/**
Gavin Shan3ab96a02012-09-07 22:44:23 +0000217 * eeh_addr_cache_insert_dev - Add a device to the address cache
Linas Vepstas5d5a0932005-11-03 18:53:07 -0600218 * @dev: PCI device whose I/O addresses we are interested in.
219 *
220 * In order to support the fast lookup of devices based on addresses,
221 * we maintain a cache of devices that can be quickly searched.
222 * This routine adds a device to that cache.
223 */
Gavin Shan3ab96a02012-09-07 22:44:23 +0000224void eeh_addr_cache_insert_dev(struct pci_dev *dev)
Linas Vepstas5d5a0932005-11-03 18:53:07 -0600225{
226 unsigned long flags;
227
228 spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
Gavin Shan3ab96a02012-09-07 22:44:23 +0000229 __eeh_addr_cache_insert_dev(dev);
Linas Vepstas5d5a0932005-11-03 18:53:07 -0600230 spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
231}
232
Gavin Shan3ab96a02012-09-07 22:44:23 +0000233static inline void __eeh_addr_cache_rmv_dev(struct pci_dev *dev)
Linas Vepstas5d5a0932005-11-03 18:53:07 -0600234{
235 struct rb_node *n;
Linas Vepstas5d5a0932005-11-03 18:53:07 -0600236
237restart:
238 n = rb_first(&pci_io_addr_cache_root.rb_root);
239 while (n) {
240 struct pci_io_addr_range *piar;
241 piar = rb_entry(n, struct pci_io_addr_range, rb_node);
242
243 if (piar->pcidev == dev) {
244 rb_erase(n, &pci_io_addr_cache_root.rb_root);
Linas Vepstas5d5a0932005-11-03 18:53:07 -0600245 kfree(piar);
246 goto restart;
247 }
248 n = rb_next(n);
249 }
Linas Vepstas5d5a0932005-11-03 18:53:07 -0600250}
251
252/**
Gavin Shan3ab96a02012-09-07 22:44:23 +0000253 * eeh_addr_cache_rmv_dev - remove pci device from addr cache
Linas Vepstas5d5a0932005-11-03 18:53:07 -0600254 * @dev: device to remove
255 *
256 * Remove a device from the addr-cache tree.
257 * This is potentially expensive, since it will walk
258 * the tree multiple times (once per resource).
259 * But so what; device removal doesn't need to be that fast.
260 */
Gavin Shan3ab96a02012-09-07 22:44:23 +0000261void eeh_addr_cache_rmv_dev(struct pci_dev *dev)
Linas Vepstas5d5a0932005-11-03 18:53:07 -0600262{
263 unsigned long flags;
264
265 spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
Gavin Shan3ab96a02012-09-07 22:44:23 +0000266 __eeh_addr_cache_rmv_dev(dev);
Linas Vepstas5d5a0932005-11-03 18:53:07 -0600267 spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
268}
269
270/**
Gavin Shan3ab96a02012-09-07 22:44:23 +0000271 * eeh_addr_cache_build - Build a cache of I/O addresses
Linas Vepstas5d5a0932005-11-03 18:53:07 -0600272 *
273 * Build a cache of pci i/o addresses. This cache will be used to
274 * find the pci device that corresponds to a given address.
275 * This routine scans all pci busses to build the cache.
276 * Must be run late in boot process, after the pci controllers
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200277 * have been scanned for devices (after all device resources are known).
Linas Vepstas5d5a0932005-11-03 18:53:07 -0600278 */
Gavin Shaneeb63612013-06-27 13:46:47 +0800279void eeh_addr_cache_build(void)
Linas Vepstas5d5a0932005-11-03 18:53:07 -0600280{
Gavin Shanc6406d82015-03-17 16:15:08 +1100281 struct pci_dn *pdn;
Gavin Shand50a7d42012-02-27 20:04:06 +0000282 struct eeh_dev *edev;
Linas Vepstas5d5a0932005-11-03 18:53:07 -0600283 struct pci_dev *dev = NULL;
284
285 spin_lock_init(&pci_io_addr_cache_root.piar_lock);
286
Kulikov Vasiliy6901c6c2010-07-03 06:03:48 +0000287 for_each_pci_dev(dev) {
Gavin Shanc6406d82015-03-17 16:15:08 +1100288 pdn = pci_get_pdn_by_devfn(dev->bus, dev->devfn);
289 if (!pdn)
Nathan Lynchccba0512006-06-20 18:01:58 +1000290 continue;
Gavin Shand50a7d42012-02-27 20:04:06 +0000291
Gavin Shanc6406d82015-03-17 16:15:08 +1100292 edev = pdn_to_eeh_dev(pdn);
Gavin Shand50a7d42012-02-27 20:04:06 +0000293 if (!edev)
294 continue;
295
Gavin Shand50a7d42012-02-27 20:04:06 +0000296 dev->dev.archdata.edev = edev;
297 edev->pdev = dev;
Linas Vepstase1d04c92007-05-24 03:16:46 +1000298
Thadeu Lima de Souza Cascardo1abd6012013-06-27 18:00:10 -0300299 eeh_addr_cache_insert_dev(dev);
Linas Vepstase1d04c92007-05-24 03:16:46 +1000300 eeh_sysfs_add_device(dev);
Linas Vepstas5d5a0932005-11-03 18:53:07 -0600301 }
302
303#ifdef DEBUG
304 /* Verify tree built up above, echo back the list of addrs. */
Gavin Shan3ab96a02012-09-07 22:44:23 +0000305 eeh_addr_cache_print(&pci_io_addr_cache_root);
Linas Vepstas5d5a0932005-11-03 18:53:07 -0600306#endif
307}