1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/list.h>
23#include <linux/pci.h>
24#include <linux/rbtree.h>
25#include <linux/slab.h>
26#include <linux/spinlock.h>
27#include <linux/atomic.h>
28#include <asm/pci-bridge.h>
29#include <asm/ppc-pci.h>
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49struct pci_io_addr_range {
50 struct rb_node rb_node;
51 resource_size_t addr_lo;
52 resource_size_t addr_hi;
53 struct eeh_dev *edev;
54 struct pci_dev *pcidev;
55 unsigned long flags;
56};
57
58static struct pci_io_addr_cache {
59 struct rb_root rb_root;
60 spinlock_t piar_lock;
61} pci_io_addr_cache_root;
62
63static inline struct eeh_dev *__eeh_addr_cache_get_device(unsigned long addr)
64{
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
71 if (addr < piar->addr_lo)
72 n = n->rb_left;
73 else if (addr > piar->addr_hi)
74 n = n->rb_right;
75 else
76 return piar->edev;
77 }
78
79 return NULL;
80}
81
82
83
84
85
86
87
88
89
90
91
92struct eeh_dev *eeh_addr_cache_get_dev(unsigned long addr)
93{
94 struct eeh_dev *edev;
95 unsigned long flags;
96
97 spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
98 edev = __eeh_addr_cache_get_device(addr);
99 spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
100 return edev;
101}
102
103#ifdef DEBUG
104
105
106
107
108static void eeh_addr_cache_print(struct pci_io_addr_cache *cache)
109{
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);
117 pr_debug("PCI: %s addr range %d [%lx-%lx]: %s\n",
118 (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
127static struct pci_io_addr_range *
128eeh_addr_cache_insert(struct pci_dev *dev, resource_size_t alo,
129 resource_size_t ahi, unsigned long flags)
130{
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
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) {
146 pr_warn("PIAR: overlapping address range\n");
147 }
148 return piar;
149 }
150 }
151 piar = kzalloc(sizeof(struct pci_io_addr_range), GFP_ATOMIC);
152 if (!piar)
153 return NULL;
154
155 piar->addr_lo = alo;
156 piar->addr_hi = ahi;
157 piar->edev = pci_dev_to_eeh_dev(dev);
158 piar->pcidev = dev;
159 piar->flags = flags;
160
161#ifdef DEBUG
162 pr_debug("PIAR: insert range=[%lx:%lx] dev=%s\n",
163 alo, ahi, pci_name(dev));
164#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
172static void __eeh_addr_cache_insert_dev(struct pci_dev *dev)
173{
174 struct pci_dn *pdn;
175 struct eeh_dev *edev;
176 int i;
177
178 pdn = pci_get_pdn_by_devfn(dev->bus, dev->devfn);
179 if (!pdn) {
180 pr_warn("PCI: no pci dn found for dev=%s\n",
181 pci_name(dev));
182 return;
183 }
184
185 edev = pdn_to_eeh_dev(pdn);
186 if (!edev) {
187 pr_warn("PCI: no EEH dev found for %s\n",
188 pci_name(dev));
189 return;
190 }
191
192
193 if (!edev->pe) {
194 dev_dbg(&dev->dev, "EEH: Skip building address cache\n");
195 return;
196 }
197
198
199
200
201
202 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
203 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);
206
207
208 if (0 == (flags & (IORESOURCE_IO | IORESOURCE_MEM)))
209 continue;
210 if (start == 0 || ~start == 0 || end == 0 || ~end == 0)
211 continue;
212 eeh_addr_cache_insert(dev, start, end, flags);
213 }
214}
215
216
217
218
219
220
221
222
223
224void eeh_addr_cache_insert_dev(struct pci_dev *dev)
225{
226 unsigned long flags;
227
228 spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
229 __eeh_addr_cache_insert_dev(dev);
230 spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
231}
232
233static inline void __eeh_addr_cache_rmv_dev(struct pci_dev *dev)
234{
235 struct rb_node *n;
236
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);
245 kfree(piar);
246 goto restart;
247 }
248 n = rb_next(n);
249 }
250}
251
252
253
254
255
256
257
258
259
260
261void eeh_addr_cache_rmv_dev(struct pci_dev *dev)
262{
263 unsigned long flags;
264
265 spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
266 __eeh_addr_cache_rmv_dev(dev);
267 spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
268}
269
270
271
272
273
274
275
276
277
278
279void eeh_addr_cache_build(void)
280{
281 struct pci_dn *pdn;
282 struct eeh_dev *edev;
283 struct pci_dev *dev = NULL;
284
285 spin_lock_init(&pci_io_addr_cache_root.piar_lock);
286
287 for_each_pci_dev(dev) {
288 pdn = pci_get_pdn_by_devfn(dev->bus, dev->devfn);
289 if (!pdn)
290 continue;
291
292 edev = pdn_to_eeh_dev(pdn);
293 if (!edev)
294 continue;
295
296 dev->dev.archdata.edev = edev;
297 edev->pdev = dev;
298
299 eeh_addr_cache_insert_dev(dev);
300 eeh_sysfs_add_device(dev);
301 }
302
303#ifdef DEBUG
304
305 eeh_addr_cache_print(&pci_io_addr_cache_root);
306#endif
307}
308