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
91struct eeh_dev *eeh_addr_cache_get_dev(unsigned long addr)
92{
93 struct eeh_dev *edev;
94 unsigned long flags;
95
96 spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
97 edev = __eeh_addr_cache_get_device(addr);
98 spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
99 return edev;
100}
101
102#ifdef DEBUG
103
104
105
106
107static void eeh_addr_cache_print(struct pci_io_addr_cache *cache)
108{
109 struct rb_node *n;
110 int cnt = 0;
111
112 n = rb_first(&cache->rb_root);
113 while (n) {
114 struct pci_io_addr_range *piar;
115 piar = rb_entry(n, struct pci_io_addr_range, rb_node);
116 pr_info("PCI: %s addr range %d [%pap-%pap]: %s\n",
117 (piar->flags & IORESOURCE_IO) ? "i/o" : "mem", cnt,
118 &piar->addr_lo, &piar->addr_hi, pci_name(piar->pcidev));
119 cnt++;
120 n = rb_next(n);
121 }
122}
123#endif
124
125
126static struct pci_io_addr_range *
127eeh_addr_cache_insert(struct pci_dev *dev, resource_size_t alo,
128 resource_size_t ahi, unsigned long flags)
129{
130 struct rb_node **p = &pci_io_addr_cache_root.rb_root.rb_node;
131 struct rb_node *parent = NULL;
132 struct pci_io_addr_range *piar;
133
134
135 while (*p) {
136 parent = *p;
137 piar = rb_entry(parent, struct pci_io_addr_range, rb_node);
138 if (ahi < piar->addr_lo) {
139 p = &parent->rb_left;
140 } else if (alo > piar->addr_hi) {
141 p = &parent->rb_right;
142 } else {
143 if (dev != piar->pcidev ||
144 alo != piar->addr_lo || ahi != piar->addr_hi) {
145 pr_warn("PIAR: overlapping address range\n");
146 }
147 return piar;
148 }
149 }
150 piar = kzalloc(sizeof(struct pci_io_addr_range), GFP_ATOMIC);
151 if (!piar)
152 return NULL;
153
154 piar->addr_lo = alo;
155 piar->addr_hi = ahi;
156 piar->edev = pci_dev_to_eeh_dev(dev);
157 piar->pcidev = dev;
158 piar->flags = flags;
159
160 eeh_edev_dbg(piar->edev, "PIAR: insert range=[%pap:%pap]\n",
161 &alo, &ahi);
162
163 rb_link_node(&piar->rb_node, parent, p);
164 rb_insert_color(&piar->rb_node, &pci_io_addr_cache_root.rb_root);
165
166 return piar;
167}
168
169static void __eeh_addr_cache_insert_dev(struct pci_dev *dev)
170{
171 struct pci_dn *pdn;
172 struct eeh_dev *edev;
173 int i;
174
175 pdn = pci_get_pdn_by_devfn(dev->bus, dev->devfn);
176 if (!pdn) {
177 pr_warn("PCI: no pci dn found for dev=%s\n",
178 pci_name(dev));
179 return;
180 }
181
182 edev = pdn_to_eeh_dev(pdn);
183 if (!edev) {
184 pr_warn("PCI: no EEH dev found for %s\n",
185 pci_name(dev));
186 return;
187 }
188
189
190 if (!edev->pe) {
191 dev_dbg(&dev->dev, "EEH: Skip building address cache\n");
192 return;
193 }
194
195
196
197
198
199 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
200 resource_size_t start = pci_resource_start(dev,i);
201 resource_size_t end = pci_resource_end(dev,i);
202 unsigned long flags = pci_resource_flags(dev,i);
203
204
205 if (0 == (flags & (IORESOURCE_IO | IORESOURCE_MEM)))
206 continue;
207 if (start == 0 || ~start == 0 || end == 0 || ~end == 0)
208 continue;
209 eeh_addr_cache_insert(dev, start, end, flags);
210 }
211}
212
213
214
215
216
217
218
219
220
221void eeh_addr_cache_insert_dev(struct pci_dev *dev)
222{
223 unsigned long flags;
224
225 spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
226 __eeh_addr_cache_insert_dev(dev);
227 spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
228}
229
230static inline void __eeh_addr_cache_rmv_dev(struct pci_dev *dev)
231{
232 struct rb_node *n;
233
234restart:
235 n = rb_first(&pci_io_addr_cache_root.rb_root);
236 while (n) {
237 struct pci_io_addr_range *piar;
238 piar = rb_entry(n, struct pci_io_addr_range, rb_node);
239
240 if (piar->pcidev == dev) {
241 eeh_edev_dbg(piar->edev, "PIAR: remove range=[%pap:%pap]\n",
242 &piar->addr_lo, &piar->addr_hi);
243 rb_erase(n, &pci_io_addr_cache_root.rb_root);
244 kfree(piar);
245 goto restart;
246 }
247 n = rb_next(n);
248 }
249}
250
251
252
253
254
255
256
257
258
259
260void eeh_addr_cache_rmv_dev(struct pci_dev *dev)
261{
262 unsigned long flags;
263
264 spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
265 __eeh_addr_cache_rmv_dev(dev);
266 spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
267}
268
269
270
271
272
273
274
275void eeh_addr_cache_init(void)
276{
277 spin_lock_init(&pci_io_addr_cache_root.piar_lock);
278}
279