xref: /openbmc/linux/arch/powerpc/kernel/eeh_cache.c (revision 03ab8e6297acd1bc0eedaa050e2a1635c576fd11)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2317f06deSGavin Shan /*
3317f06deSGavin Shan  * PCI address cache; allows the lookup of PCI devices based on I/O address
4317f06deSGavin Shan  *
5317f06deSGavin Shan  * Copyright IBM Corporation 2004
6317f06deSGavin Shan  * Copyright Linas Vepstas <linas@austin.ibm.com> 2004
7317f06deSGavin Shan  */
8317f06deSGavin Shan 
9317f06deSGavin Shan #include <linux/list.h>
10317f06deSGavin Shan #include <linux/pci.h>
11317f06deSGavin Shan #include <linux/rbtree.h>
12317f06deSGavin Shan #include <linux/slab.h>
13317f06deSGavin Shan #include <linux/spinlock.h>
14317f06deSGavin Shan #include <linux/atomic.h>
15dbf77fedSAneesh Kumar K.V #include <linux/debugfs.h>
16317f06deSGavin Shan #include <asm/pci-bridge.h>
17317f06deSGavin Shan #include <asm/ppc-pci.h>
18317f06deSGavin Shan 
19317f06deSGavin Shan 
20317f06deSGavin Shan /**
213becd11dSQian Cai  * DOC: Overview
223becd11dSQian Cai  *
23317f06deSGavin Shan  * The pci address cache subsystem.  This subsystem places
24317f06deSGavin Shan  * PCI device address resources into a red-black tree, sorted
25317f06deSGavin Shan  * according to the address range, so that given only an i/o
26317f06deSGavin Shan  * address, the corresponding PCI device can be **quickly**
27317f06deSGavin Shan  * found. It is safe to perform an address lookup in an interrupt
28317f06deSGavin Shan  * context; this ability is an important feature.
29317f06deSGavin Shan  *
30317f06deSGavin Shan  * Currently, the only customer of this code is the EEH subsystem;
31317f06deSGavin Shan  * thus, this code has been somewhat tailored to suit EEH better.
32317f06deSGavin Shan  * In particular, the cache does *not* hold the addresses of devices
33317f06deSGavin Shan  * for which EEH is not enabled.
34317f06deSGavin Shan  *
35317f06deSGavin Shan  * (Implementation Note: The RB tree seems to be better/faster
36317f06deSGavin Shan  * than any hash algo I could think of for this problem, even
37317f06deSGavin Shan  * with the penalty of slow pointer chases for d-cache misses).
38317f06deSGavin Shan  */
393becd11dSQian Cai 
40317f06deSGavin Shan struct pci_io_addr_range {
41317f06deSGavin Shan 	struct rb_node rb_node;
4237213529SWei Yang 	resource_size_t addr_lo;
4337213529SWei Yang 	resource_size_t addr_hi;
44317f06deSGavin Shan 	struct eeh_dev *edev;
45317f06deSGavin Shan 	struct pci_dev *pcidev;
4637213529SWei Yang 	unsigned long flags;
47317f06deSGavin Shan };
48317f06deSGavin Shan 
49317f06deSGavin Shan static struct pci_io_addr_cache {
50317f06deSGavin Shan 	struct rb_root rb_root;
51317f06deSGavin Shan 	spinlock_t piar_lock;
52317f06deSGavin Shan } pci_io_addr_cache_root;
53317f06deSGavin Shan 
__eeh_addr_cache_get_device(unsigned long addr)54317f06deSGavin Shan static inline struct eeh_dev *__eeh_addr_cache_get_device(unsigned long addr)
55317f06deSGavin Shan {
56317f06deSGavin Shan 	struct rb_node *n = pci_io_addr_cache_root.rb_root.rb_node;
57317f06deSGavin Shan 
58317f06deSGavin Shan 	while (n) {
59317f06deSGavin Shan 		struct pci_io_addr_range *piar;
60317f06deSGavin Shan 		piar = rb_entry(n, struct pci_io_addr_range, rb_node);
61317f06deSGavin Shan 
620ba17888SGavin Shan 		if (addr < piar->addr_lo)
63317f06deSGavin Shan 			n = n->rb_left;
640ba17888SGavin Shan 		else if (addr > piar->addr_hi)
65317f06deSGavin Shan 			n = n->rb_right;
660ba17888SGavin Shan 		else
67317f06deSGavin Shan 			return piar->edev;
68317f06deSGavin Shan 	}
69317f06deSGavin Shan 
70317f06deSGavin Shan 	return NULL;
71317f06deSGavin Shan }
72317f06deSGavin Shan 
73317f06deSGavin Shan /**
74317f06deSGavin Shan  * eeh_addr_cache_get_dev - Get device, given only address
75317f06deSGavin Shan  * @addr: mmio (PIO) phys address or i/o port number
76317f06deSGavin Shan  *
77317f06deSGavin Shan  * Given an mmio phys address, or a port number, find a pci device
7863457b14SSam Bobroff  * that implements this address.  I/O port numbers are assumed to be offset
79317f06deSGavin Shan  * from zero (that is, they do *not* have pci_io_addr added in).
80317f06deSGavin Shan  * It is safe to call this function within an interrupt.
81317f06deSGavin Shan  */
eeh_addr_cache_get_dev(unsigned long addr)82317f06deSGavin Shan struct eeh_dev *eeh_addr_cache_get_dev(unsigned long addr)
83317f06deSGavin Shan {
84317f06deSGavin Shan 	struct eeh_dev *edev;
85317f06deSGavin Shan 	unsigned long flags;
86317f06deSGavin Shan 
87317f06deSGavin Shan 	spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
88317f06deSGavin Shan 	edev = __eeh_addr_cache_get_device(addr);
89317f06deSGavin Shan 	spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
90317f06deSGavin Shan 	return edev;
91317f06deSGavin Shan }
92317f06deSGavin Shan 
93317f06deSGavin Shan #ifdef DEBUG
94317f06deSGavin Shan /*
95317f06deSGavin Shan  * Handy-dandy debug print routine, does nothing more
96317f06deSGavin Shan  * than print out the contents of our addr cache.
97317f06deSGavin Shan  */
eeh_addr_cache_print(struct pci_io_addr_cache * cache)98317f06deSGavin Shan static void eeh_addr_cache_print(struct pci_io_addr_cache *cache)
99317f06deSGavin Shan {
100317f06deSGavin Shan 	struct rb_node *n;
101317f06deSGavin Shan 	int cnt = 0;
102317f06deSGavin Shan 
103317f06deSGavin Shan 	n = rb_first(&cache->rb_root);
104317f06deSGavin Shan 	while (n) {
105317f06deSGavin Shan 		struct pci_io_addr_range *piar;
106317f06deSGavin Shan 		piar = rb_entry(n, struct pci_io_addr_range, rb_node);
107c8f02f21SOliver O'Halloran 		pr_info("PCI: %s addr range %d [%pap-%pap]: %s\n",
108317f06deSGavin Shan 		       (piar->flags & IORESOURCE_IO) ? "i/o" : "mem", cnt,
10991dc0682SAndrew Donnellan 		       &piar->addr_lo, &piar->addr_hi, pci_name(piar->pcidev));
110317f06deSGavin Shan 		cnt++;
111317f06deSGavin Shan 		n = rb_next(n);
112317f06deSGavin Shan 	}
113317f06deSGavin Shan }
114317f06deSGavin Shan #endif
115317f06deSGavin Shan 
116317f06deSGavin Shan /* Insert address range into the rb tree. */
117317f06deSGavin Shan static struct pci_io_addr_range *
eeh_addr_cache_insert(struct pci_dev * dev,resource_size_t alo,resource_size_t ahi,unsigned long flags)11837213529SWei Yang eeh_addr_cache_insert(struct pci_dev *dev, resource_size_t alo,
11937213529SWei Yang 		      resource_size_t ahi, unsigned long flags)
120317f06deSGavin Shan {
121317f06deSGavin Shan 	struct rb_node **p = &pci_io_addr_cache_root.rb_root.rb_node;
122317f06deSGavin Shan 	struct rb_node *parent = NULL;
123317f06deSGavin Shan 	struct pci_io_addr_range *piar;
124317f06deSGavin Shan 
125317f06deSGavin Shan 	/* Walk tree, find a place to insert into tree */
126317f06deSGavin Shan 	while (*p) {
127317f06deSGavin Shan 		parent = *p;
128317f06deSGavin Shan 		piar = rb_entry(parent, struct pci_io_addr_range, rb_node);
129317f06deSGavin Shan 		if (ahi < piar->addr_lo) {
130317f06deSGavin Shan 			p = &parent->rb_left;
131317f06deSGavin Shan 		} else if (alo > piar->addr_hi) {
132317f06deSGavin Shan 			p = &parent->rb_right;
133317f06deSGavin Shan 		} else {
134317f06deSGavin Shan 			if (dev != piar->pcidev ||
135317f06deSGavin Shan 			    alo != piar->addr_lo || ahi != piar->addr_hi) {
1360dae2743SGavin Shan 				pr_warn("PIAR: overlapping address range\n");
137317f06deSGavin Shan 			}
138317f06deSGavin Shan 			return piar;
139317f06deSGavin Shan 		}
140317f06deSGavin Shan 	}
141317f06deSGavin Shan 	piar = kzalloc(sizeof(struct pci_io_addr_range), GFP_ATOMIC);
142317f06deSGavin Shan 	if (!piar)
143317f06deSGavin Shan 		return NULL;
144317f06deSGavin Shan 
145317f06deSGavin Shan 	piar->addr_lo = alo;
146317f06deSGavin Shan 	piar->addr_hi = ahi;
147317f06deSGavin Shan 	piar->edev = pci_dev_to_eeh_dev(dev);
148317f06deSGavin Shan 	piar->pcidev = dev;
149317f06deSGavin Shan 	piar->flags = flags;
150317f06deSGavin Shan 
1511ff8f36fSSam Bobroff 	eeh_edev_dbg(piar->edev, "PIAR: insert range=[%pap:%pap]\n",
1521ff8f36fSSam Bobroff 		 &alo, &ahi);
153317f06deSGavin Shan 
154317f06deSGavin Shan 	rb_link_node(&piar->rb_node, parent, p);
155317f06deSGavin Shan 	rb_insert_color(&piar->rb_node, &pci_io_addr_cache_root.rb_root);
156317f06deSGavin Shan 
157317f06deSGavin Shan 	return piar;
158317f06deSGavin Shan }
159317f06deSGavin Shan 
__eeh_addr_cache_insert_dev(struct pci_dev * dev)160317f06deSGavin Shan static void __eeh_addr_cache_insert_dev(struct pci_dev *dev)
161317f06deSGavin Shan {
162317f06deSGavin Shan 	struct eeh_dev *edev;
163317f06deSGavin Shan 	int i;
164317f06deSGavin Shan 
165b1268f4cSOliver O'Halloran 	edev = pci_dev_to_eeh_dev(dev);
166317f06deSGavin Shan 	if (!edev) {
167c6406d8fSGavin Shan 		pr_warn("PCI: no EEH dev found for %s\n",
168c6406d8fSGavin Shan 			pci_name(dev));
169317f06deSGavin Shan 		return;
170317f06deSGavin Shan 	}
171317f06deSGavin Shan 
172317f06deSGavin Shan 	/* Skip any devices for which EEH is not enabled. */
17305b1721dSGavin Shan 	if (!edev->pe) {
174c6406d8fSGavin Shan 		dev_dbg(&dev->dev, "EEH: Skip building address cache\n");
175317f06deSGavin Shan 		return;
176317f06deSGavin Shan 	}
177317f06deSGavin Shan 
17851c0e87eSWei Yang 	/*
17951c0e87eSWei Yang 	 * Walk resources on this device, poke the first 7 (6 normal BAR and 1
18051c0e87eSWei Yang 	 * ROM BAR) into the tree.
18151c0e87eSWei Yang 	 */
18251c0e87eSWei Yang 	for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
18337213529SWei Yang 		resource_size_t start = pci_resource_start(dev,i);
18437213529SWei Yang 		resource_size_t end = pci_resource_end(dev,i);
18537213529SWei Yang 		unsigned long flags = pci_resource_flags(dev,i);
186317f06deSGavin Shan 
187317f06deSGavin Shan 		/* We are interested only bus addresses, not dma or other stuff */
188317f06deSGavin Shan 		if (0 == (flags & (IORESOURCE_IO | IORESOURCE_MEM)))
189317f06deSGavin Shan 			continue;
190317f06deSGavin Shan 		if (start == 0 || ~start == 0 || end == 0 || ~end == 0)
191317f06deSGavin Shan 			 continue;
192317f06deSGavin Shan 		eeh_addr_cache_insert(dev, start, end, flags);
193317f06deSGavin Shan 	}
194317f06deSGavin Shan }
195317f06deSGavin Shan 
196317f06deSGavin Shan /**
197317f06deSGavin Shan  * eeh_addr_cache_insert_dev - Add a device to the address cache
198317f06deSGavin Shan  * @dev: PCI device whose I/O addresses we are interested in.
199317f06deSGavin Shan  *
200317f06deSGavin Shan  * In order to support the fast lookup of devices based on addresses,
201317f06deSGavin Shan  * we maintain a cache of devices that can be quickly searched.
202317f06deSGavin Shan  * This routine adds a device to that cache.
203317f06deSGavin Shan  */
eeh_addr_cache_insert_dev(struct pci_dev * dev)204317f06deSGavin Shan void eeh_addr_cache_insert_dev(struct pci_dev *dev)
205317f06deSGavin Shan {
206317f06deSGavin Shan 	unsigned long flags;
207317f06deSGavin Shan 
208317f06deSGavin Shan 	spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
209317f06deSGavin Shan 	__eeh_addr_cache_insert_dev(dev);
210317f06deSGavin Shan 	spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
211317f06deSGavin Shan }
212317f06deSGavin Shan 
__eeh_addr_cache_rmv_dev(struct pci_dev * dev)213317f06deSGavin Shan static inline void __eeh_addr_cache_rmv_dev(struct pci_dev *dev)
214317f06deSGavin Shan {
215317f06deSGavin Shan 	struct rb_node *n;
216317f06deSGavin Shan 
217317f06deSGavin Shan restart:
218317f06deSGavin Shan 	n = rb_first(&pci_io_addr_cache_root.rb_root);
219317f06deSGavin Shan 	while (n) {
220317f06deSGavin Shan 		struct pci_io_addr_range *piar;
221317f06deSGavin Shan 		piar = rb_entry(n, struct pci_io_addr_range, rb_node);
222317f06deSGavin Shan 
223317f06deSGavin Shan 		if (piar->pcidev == dev) {
2241ff8f36fSSam Bobroff 			eeh_edev_dbg(piar->edev, "PIAR: remove range=[%pap:%pap]\n",
2251ff8f36fSSam Bobroff 				 &piar->addr_lo, &piar->addr_hi);
226317f06deSGavin Shan 			rb_erase(n, &pci_io_addr_cache_root.rb_root);
227317f06deSGavin Shan 			kfree(piar);
228317f06deSGavin Shan 			goto restart;
229317f06deSGavin Shan 		}
230317f06deSGavin Shan 		n = rb_next(n);
231317f06deSGavin Shan 	}
232317f06deSGavin Shan }
233317f06deSGavin Shan 
234317f06deSGavin Shan /**
235317f06deSGavin Shan  * eeh_addr_cache_rmv_dev - remove pci device from addr cache
236317f06deSGavin Shan  * @dev: device to remove
237317f06deSGavin Shan  *
238317f06deSGavin Shan  * Remove a device from the addr-cache tree.
239317f06deSGavin Shan  * This is potentially expensive, since it will walk
240317f06deSGavin Shan  * the tree multiple times (once per resource).
241317f06deSGavin Shan  * But so what; device removal doesn't need to be that fast.
242317f06deSGavin Shan  */
eeh_addr_cache_rmv_dev(struct pci_dev * dev)243317f06deSGavin Shan void eeh_addr_cache_rmv_dev(struct pci_dev *dev)
244317f06deSGavin Shan {
245317f06deSGavin Shan 	unsigned long flags;
246317f06deSGavin Shan 
247317f06deSGavin Shan 	spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
248317f06deSGavin Shan 	__eeh_addr_cache_rmv_dev(dev);
249317f06deSGavin Shan 	spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
250317f06deSGavin Shan }
251317f06deSGavin Shan 
252317f06deSGavin Shan /**
253685a0bc0SSam Bobroff  * eeh_addr_cache_init - Initialize a cache of I/O addresses
254685a0bc0SSam Bobroff  *
255685a0bc0SSam Bobroff  * Initialize a cache of pci i/o addresses.  This cache will be used to
256685a0bc0SSam Bobroff  * find the pci device that corresponds to a given address.
257685a0bc0SSam Bobroff  */
eeh_addr_cache_init(void)258685a0bc0SSam Bobroff void eeh_addr_cache_init(void)
259685a0bc0SSam Bobroff {
260685a0bc0SSam Bobroff 	spin_lock_init(&pci_io_addr_cache_root.piar_lock);
261685a0bc0SSam Bobroff }
262685a0bc0SSam Bobroff 
eeh_addr_cache_show(struct seq_file * s,void * v)2635ca85ae6SOliver O'Halloran static int eeh_addr_cache_show(struct seq_file *s, void *v)
2645ca85ae6SOliver O'Halloran {
2655ca85ae6SOliver O'Halloran 	struct pci_io_addr_range *piar;
2665ca85ae6SOliver O'Halloran 	struct rb_node *n;
267fd552e05SQian Cai 	unsigned long flags;
2685ca85ae6SOliver O'Halloran 
269fd552e05SQian Cai 	spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
2705ca85ae6SOliver O'Halloran 	for (n = rb_first(&pci_io_addr_cache_root.rb_root); n; n = rb_next(n)) {
2715ca85ae6SOliver O'Halloran 		piar = rb_entry(n, struct pci_io_addr_range, rb_node);
2725ca85ae6SOliver O'Halloran 
2735ca85ae6SOliver O'Halloran 		seq_printf(s, "%s addr range [%pap-%pap]: %s\n",
2745ca85ae6SOliver O'Halloran 		       (piar->flags & IORESOURCE_IO) ? "i/o" : "mem",
2755ca85ae6SOliver O'Halloran 		       &piar->addr_lo, &piar->addr_hi, pci_name(piar->pcidev));
2765ca85ae6SOliver O'Halloran 	}
277fd552e05SQian Cai 	spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
2785ca85ae6SOliver O'Halloran 
2795ca85ae6SOliver O'Halloran 	return 0;
2805ca85ae6SOliver O'Halloran }
2815ca85ae6SOliver O'Halloran DEFINE_SHOW_ATTRIBUTE(eeh_addr_cache);
2825ca85ae6SOliver O'Halloran 
eeh_cache_debugfs_init(void)283*d276960dSNick Child void __init eeh_cache_debugfs_init(void)
2845ca85ae6SOliver O'Halloran {
2855ca85ae6SOliver O'Halloran 	debugfs_create_file_unsafe("eeh_address_cache", 0400,
286dbf77fedSAneesh Kumar K.V 			arch_debugfs_dir, NULL,
2875ca85ae6SOliver O'Halloran 			&eeh_addr_cache_fops);
288317f06deSGavin Shan }
289