]>
Commit | Line | Data |
---|---|---|
7328c8f4 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 | 2 | /* |
df62ab5e | 3 | * Support routines for initializing a PCI subsystem |
1da177e4 LT |
4 | * |
5 | * Extruded from code written by | |
6 | * Dave Rusling ([email protected]) | |
7 | * David Mosberger ([email protected]) | |
8 | * David Miller ([email protected]) | |
9 | * | |
1da177e4 LT |
10 | * Nov 2000, Ivan Kokshaysky <[email protected]> |
11 | * PCI-PCI bridges cleanup, sorted resource allocation. | |
12 | * Feb 2002, Ivan Kokshaysky <[email protected]> | |
13 | * Converted to allocation in 3 passes, which gives | |
14 | * tighter packing. Prefetchable range support. | |
15 | */ | |
16 | ||
17 | #include <linux/init.h> | |
18 | #include <linux/kernel.h> | |
19 | #include <linux/module.h> | |
20 | #include <linux/pci.h> | |
21 | #include <linux/errno.h> | |
22 | #include <linux/ioport.h> | |
23 | #include <linux/cache.h> | |
24 | #include <linux/slab.h> | |
584c5c42 | 25 | #include <linux/acpi.h> |
6faf17f6 | 26 | #include "pci.h" |
1da177e4 | 27 | |
844393f4 | 28 | unsigned int pci_flags; |
0c59c06a | 29 | EXPORT_SYMBOL_GPL(pci_flags); |
47087700 | 30 | |
bdc4abec YL |
31 | struct pci_dev_resource { |
32 | struct list_head list; | |
2934a0de YL |
33 | struct resource *res; |
34 | struct pci_dev *dev; | |
568ddef8 YL |
35 | resource_size_t start; |
36 | resource_size_t end; | |
c8adf9a3 | 37 | resource_size_t add_size; |
2bbc6942 | 38 | resource_size_t min_align; |
568ddef8 YL |
39 | unsigned long flags; |
40 | }; | |
41 | ||
bffc56d4 YL |
42 | static void free_list(struct list_head *head) |
43 | { | |
44 | struct pci_dev_resource *dev_res, *tmp; | |
45 | ||
46 | list_for_each_entry_safe(dev_res, tmp, head, list) { | |
47 | list_del(&dev_res->list); | |
48 | kfree(dev_res); | |
49 | } | |
50 | } | |
094732a5 | 51 | |
c8adf9a3 | 52 | /** |
0d607618 | 53 | * add_to_list() - Add a new resource tracker to the list |
c8adf9a3 | 54 | * @head: Head of the list |
0d607618 NJ |
55 | * @dev: Device to which the resource belongs |
56 | * @res: Resource to be tracked | |
57 | * @add_size: Additional size to be optionally added to the resource | |
9b41d19a | 58 | * @min_align: Minimum memory window alignment |
c8adf9a3 | 59 | */ |
0d607618 NJ |
60 | static int add_to_list(struct list_head *head, struct pci_dev *dev, |
61 | struct resource *res, resource_size_t add_size, | |
62 | resource_size_t min_align) | |
568ddef8 | 63 | { |
764242a0 | 64 | struct pci_dev_resource *tmp; |
568ddef8 | 65 | |
bdc4abec | 66 | tmp = kzalloc(sizeof(*tmp), GFP_KERNEL); |
c7abb235 | 67 | if (!tmp) |
ef62dfef | 68 | return -ENOMEM; |
568ddef8 | 69 | |
568ddef8 YL |
70 | tmp->res = res; |
71 | tmp->dev = dev; | |
72 | tmp->start = res->start; | |
73 | tmp->end = res->end; | |
74 | tmp->flags = res->flags; | |
c8adf9a3 | 75 | tmp->add_size = add_size; |
2bbc6942 | 76 | tmp->min_align = min_align; |
bdc4abec YL |
77 | |
78 | list_add(&tmp->list, head); | |
ef62dfef YL |
79 | |
80 | return 0; | |
568ddef8 YL |
81 | } |
82 | ||
0d607618 | 83 | static void remove_from_list(struct list_head *head, struct resource *res) |
3e6e0d80 | 84 | { |
b9b0bba9 | 85 | struct pci_dev_resource *dev_res, *tmp; |
3e6e0d80 | 86 | |
b9b0bba9 YL |
87 | list_for_each_entry_safe(dev_res, tmp, head, list) { |
88 | if (dev_res->res == res) { | |
89 | list_del(&dev_res->list); | |
90 | kfree(dev_res); | |
bdc4abec | 91 | break; |
3e6e0d80 | 92 | } |
3e6e0d80 YL |
93 | } |
94 | } | |
95 | ||
d74b9027 WY |
96 | static struct pci_dev_resource *res_to_dev_res(struct list_head *head, |
97 | struct resource *res) | |
1c372353 | 98 | { |
b9b0bba9 | 99 | struct pci_dev_resource *dev_res; |
bdc4abec | 100 | |
b9b0bba9 | 101 | list_for_each_entry(dev_res, head, list) { |
25e77388 | 102 | if (dev_res->res == res) |
d74b9027 | 103 | return dev_res; |
3e6e0d80 | 104 | } |
1c372353 | 105 | |
d74b9027 | 106 | return NULL; |
1c372353 YL |
107 | } |
108 | ||
d74b9027 WY |
109 | static resource_size_t get_res_add_size(struct list_head *head, |
110 | struct resource *res) | |
111 | { | |
112 | struct pci_dev_resource *dev_res; | |
113 | ||
114 | dev_res = res_to_dev_res(head, res); | |
115 | return dev_res ? dev_res->add_size : 0; | |
116 | } | |
117 | ||
118 | static resource_size_t get_res_add_align(struct list_head *head, | |
119 | struct resource *res) | |
120 | { | |
121 | struct pci_dev_resource *dev_res; | |
122 | ||
123 | dev_res = res_to_dev_res(head, res); | |
124 | return dev_res ? dev_res->min_align : 0; | |
125 | } | |
126 | ||
127 | ||
78c3b329 | 128 | /* Sort resources by alignment */ |
bdc4abec | 129 | static void pdev_sort_resources(struct pci_dev *dev, struct list_head *head) |
78c3b329 YL |
130 | { |
131 | int i; | |
132 | ||
133 | for (i = 0; i < PCI_NUM_RESOURCES; i++) { | |
134 | struct resource *r; | |
bdc4abec | 135 | struct pci_dev_resource *dev_res, *tmp; |
78c3b329 | 136 | resource_size_t r_align; |
bdc4abec | 137 | struct list_head *n; |
78c3b329 YL |
138 | |
139 | r = &dev->resource[i]; | |
140 | ||
141 | if (r->flags & IORESOURCE_PCI_FIXED) | |
142 | continue; | |
143 | ||
144 | if (!(r->flags) || r->parent) | |
145 | continue; | |
146 | ||
147 | r_align = pci_resource_alignment(dev, r); | |
148 | if (!r_align) { | |
7506dc79 | 149 | pci_warn(dev, "BAR %d: %pR has bogus alignment\n", |
78c3b329 YL |
150 | i, r); |
151 | continue; | |
152 | } | |
78c3b329 | 153 | |
bdc4abec YL |
154 | tmp = kzalloc(sizeof(*tmp), GFP_KERNEL); |
155 | if (!tmp) | |
c7c337c5 | 156 | panic("%s: kzalloc() failed!\n", __func__); |
bdc4abec YL |
157 | tmp->res = r; |
158 | tmp->dev = dev; | |
159 | ||
0d607618 | 160 | /* Fallback is smallest one or list is empty */ |
bdc4abec YL |
161 | n = head; |
162 | list_for_each_entry(dev_res, head, list) { | |
163 | resource_size_t align; | |
164 | ||
165 | align = pci_resource_alignment(dev_res->dev, | |
166 | dev_res->res); | |
78c3b329 YL |
167 | |
168 | if (r_align > align) { | |
bdc4abec | 169 | n = &dev_res->list; |
78c3b329 YL |
170 | break; |
171 | } | |
172 | } | |
0d607618 | 173 | /* Insert it just before n */ |
bdc4abec | 174 | list_add_tail(&tmp->list, n); |
78c3b329 YL |
175 | } |
176 | } | |
177 | ||
0d607618 | 178 | static void __dev_sort_resources(struct pci_dev *dev, struct list_head *head) |
1da177e4 | 179 | { |
6841ec68 | 180 | u16 class = dev->class >> 8; |
1da177e4 | 181 | |
0d607618 | 182 | /* Don't touch classless devices or host bridges or IOAPICs */ |
6841ec68 YL |
183 | if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST) |
184 | return; | |
1da177e4 | 185 | |
0d607618 | 186 | /* Don't touch IOAPIC devices already enabled by firmware */ |
6841ec68 YL |
187 | if (class == PCI_CLASS_SYSTEM_PIC) { |
188 | u16 command; | |
189 | pci_read_config_word(dev, PCI_COMMAND, &command); | |
190 | if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) | |
191 | return; | |
192 | } | |
1da177e4 | 193 | |
6841ec68 YL |
194 | pdev_sort_resources(dev, head); |
195 | } | |
23186279 | 196 | |
fc075e1d RP |
197 | static inline void reset_resource(struct resource *res) |
198 | { | |
199 | res->start = 0; | |
200 | res->end = 0; | |
201 | res->flags = 0; | |
202 | } | |
203 | ||
c8adf9a3 | 204 | /** |
0d607618 | 205 | * reassign_resources_sorted() - Satisfy any additional resource requests |
c8adf9a3 | 206 | * |
0d607618 NJ |
207 | * @realloc_head: Head of the list tracking requests requiring |
208 | * additional resources | |
209 | * @head: Head of the list tracking requests with allocated | |
210 | * resources | |
c8adf9a3 | 211 | * |
0d607618 NJ |
212 | * Walk through each element of the realloc_head and try to procure additional |
213 | * resources for the element, provided the element is in the head list. | |
c8adf9a3 | 214 | */ |
bdc4abec | 215 | static void reassign_resources_sorted(struct list_head *realloc_head, |
0d607618 | 216 | struct list_head *head) |
6841ec68 YL |
217 | { |
218 | struct resource *res; | |
b9b0bba9 | 219 | struct pci_dev_resource *add_res, *tmp; |
bdc4abec | 220 | struct pci_dev_resource *dev_res; |
d74b9027 | 221 | resource_size_t add_size, align; |
6841ec68 | 222 | int idx; |
1da177e4 | 223 | |
b9b0bba9 | 224 | list_for_each_entry_safe(add_res, tmp, realloc_head, list) { |
bdc4abec YL |
225 | bool found_match = false; |
226 | ||
b9b0bba9 | 227 | res = add_res->res; |
0d607618 | 228 | /* Skip resource that has been reset */ |
c8adf9a3 RP |
229 | if (!res->flags) |
230 | goto out; | |
231 | ||
0d607618 | 232 | /* Skip this resource if not found in head list */ |
bdc4abec YL |
233 | list_for_each_entry(dev_res, head, list) { |
234 | if (dev_res->res == res) { | |
235 | found_match = true; | |
236 | break; | |
237 | } | |
c8adf9a3 | 238 | } |
0d607618 | 239 | if (!found_match) /* Just skip */ |
bdc4abec | 240 | continue; |
c8adf9a3 | 241 | |
b9b0bba9 YL |
242 | idx = res - &add_res->dev->resource[0]; |
243 | add_size = add_res->add_size; | |
d74b9027 | 244 | align = add_res->min_align; |
2bbc6942 | 245 | if (!resource_size(res)) { |
d74b9027 | 246 | res->start = align; |
2bbc6942 | 247 | res->end = res->start + add_size - 1; |
b9b0bba9 | 248 | if (pci_assign_resource(add_res->dev, idx)) |
c8adf9a3 | 249 | reset_resource(res); |
2bbc6942 | 250 | } else { |
b9b0bba9 | 251 | res->flags |= add_res->flags & |
bdc4abec | 252 | (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN); |
b9b0bba9 | 253 | if (pci_reassign_resource(add_res->dev, idx, |
bdc4abec | 254 | add_size, align)) |
34c6b710 MK |
255 | pci_info(add_res->dev, "failed to add %llx res[%d]=%pR\n", |
256 | (unsigned long long) add_size, idx, | |
257 | res); | |
c8adf9a3 RP |
258 | } |
259 | out: | |
b9b0bba9 YL |
260 | list_del(&add_res->list); |
261 | kfree(add_res); | |
c8adf9a3 RP |
262 | } |
263 | } | |
264 | ||
265 | /** | |
0d607618 | 266 | * assign_requested_resources_sorted() - Satisfy resource requests |
c8adf9a3 | 267 | * |
0d607618 NJ |
268 | * @head: Head of the list tracking requests for resources |
269 | * @fail_head: Head of the list tracking requests that could not be | |
270 | * allocated | |
c8adf9a3 | 271 | * |
0d607618 NJ |
272 | * Satisfy resource requests of each element in the list. Add requests that |
273 | * could not be satisfied to the failed_list. | |
c8adf9a3 | 274 | */ |
bdc4abec YL |
275 | static void assign_requested_resources_sorted(struct list_head *head, |
276 | struct list_head *fail_head) | |
c8adf9a3 RP |
277 | { |
278 | struct resource *res; | |
bdc4abec | 279 | struct pci_dev_resource *dev_res; |
c8adf9a3 | 280 | int idx; |
9a928660 | 281 | |
bdc4abec YL |
282 | list_for_each_entry(dev_res, head, list) { |
283 | res = dev_res->res; | |
284 | idx = res - &dev_res->dev->resource[0]; | |
285 | if (resource_size(res) && | |
286 | pci_assign_resource(dev_res->dev, idx)) { | |
a3cb999d | 287 | if (fail_head) { |
9a928660 | 288 | /* |
0d607618 NJ |
289 | * If the failed resource is a ROM BAR and |
290 | * it will be enabled later, don't add it | |
291 | * to the list. | |
9a928660 YL |
292 | */ |
293 | if (!((idx == PCI_ROM_RESOURCE) && | |
294 | (!(res->flags & IORESOURCE_ROM_ENABLE)))) | |
67cc7e26 YL |
295 | add_to_list(fail_head, |
296 | dev_res->dev, res, | |
f7625980 BH |
297 | 0 /* don't care */, |
298 | 0 /* don't care */); | |
9a928660 | 299 | } |
fc075e1d | 300 | reset_resource(res); |
542df5de | 301 | } |
1da177e4 LT |
302 | } |
303 | } | |
304 | ||
aa914f5e YL |
305 | static unsigned long pci_fail_res_type_mask(struct list_head *fail_head) |
306 | { | |
307 | struct pci_dev_resource *fail_res; | |
308 | unsigned long mask = 0; | |
309 | ||
0d607618 | 310 | /* Check failed type */ |
aa914f5e YL |
311 | list_for_each_entry(fail_res, fail_head, list) |
312 | mask |= fail_res->flags; | |
313 | ||
314 | /* | |
0d607618 NJ |
315 | * One pref failed resource will set IORESOURCE_MEM, as we can |
316 | * allocate pref in non-pref range. Will release all assigned | |
317 | * non-pref sibling resources according to that bit. | |
aa914f5e YL |
318 | */ |
319 | return mask & (IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH); | |
320 | } | |
321 | ||
322 | static bool pci_need_to_release(unsigned long mask, struct resource *res) | |
323 | { | |
324 | if (res->flags & IORESOURCE_IO) | |
325 | return !!(mask & IORESOURCE_IO); | |
326 | ||
0d607618 | 327 | /* Check pref at first */ |
aa914f5e YL |
328 | if (res->flags & IORESOURCE_PREFETCH) { |
329 | if (mask & IORESOURCE_PREFETCH) | |
330 | return true; | |
0d607618 | 331 | /* Count pref if its parent is non-pref */ |
aa914f5e YL |
332 | else if ((mask & IORESOURCE_MEM) && |
333 | !(res->parent->flags & IORESOURCE_PREFETCH)) | |
334 | return true; | |
335 | else | |
336 | return false; | |
337 | } | |
338 | ||
339 | if (res->flags & IORESOURCE_MEM) | |
340 | return !!(mask & IORESOURCE_MEM); | |
341 | ||
0d607618 | 342 | return false; /* Should not get here */ |
aa914f5e YL |
343 | } |
344 | ||
bdc4abec | 345 | static void __assign_resources_sorted(struct list_head *head, |
0d607618 NJ |
346 | struct list_head *realloc_head, |
347 | struct list_head *fail_head) | |
c8adf9a3 | 348 | { |
3e6e0d80 | 349 | /* |
0d607618 NJ |
350 | * Should not assign requested resources at first. They could be |
351 | * adjacent, so later reassign can not reallocate them one by one in | |
352 | * parent resource window. | |
353 | * | |
354 | * Try to assign requested + add_size at beginning. If could do that, | |
355 | * could get out early. If could not do that, we still try to assign | |
356 | * requested at first, then try to reassign add_size for some resources. | |
aa914f5e YL |
357 | * |
358 | * Separate three resource type checking if we need to release | |
359 | * assigned resource after requested + add_size try. | |
0d607618 NJ |
360 | * |
361 | * 1. If IO port assignment fails, will release assigned IO | |
362 | * port. | |
363 | * 2. If pref MMIO assignment fails, release assigned pref | |
364 | * MMIO. If assigned pref MMIO's parent is non-pref MMIO | |
365 | * and non-pref MMIO assignment fails, will release that | |
366 | * assigned pref MMIO. | |
367 | * 3. If non-pref MMIO assignment fails or pref MMIO | |
368 | * assignment fails, will release assigned non-pref MMIO. | |
3e6e0d80 | 369 | */ |
bdc4abec YL |
370 | LIST_HEAD(save_head); |
371 | LIST_HEAD(local_fail_head); | |
b9b0bba9 | 372 | struct pci_dev_resource *save_res; |
d74b9027 | 373 | struct pci_dev_resource *dev_res, *tmp_res, *dev_res2; |
aa914f5e | 374 | unsigned long fail_type; |
d74b9027 | 375 | resource_size_t add_align, align; |
3e6e0d80 YL |
376 | |
377 | /* Check if optional add_size is there */ | |
bdc4abec | 378 | if (!realloc_head || list_empty(realloc_head)) |
3e6e0d80 YL |
379 | goto requested_and_reassign; |
380 | ||
381 | /* Save original start, end, flags etc at first */ | |
bdc4abec YL |
382 | list_for_each_entry(dev_res, head, list) { |
383 | if (add_to_list(&save_head, dev_res->dev, dev_res->res, 0, 0)) { | |
bffc56d4 | 384 | free_list(&save_head); |
3e6e0d80 YL |
385 | goto requested_and_reassign; |
386 | } | |
bdc4abec | 387 | } |
3e6e0d80 YL |
388 | |
389 | /* Update res in head list with add_size in realloc_head list */ | |
d74b9027 | 390 | list_for_each_entry_safe(dev_res, tmp_res, head, list) { |
bdc4abec YL |
391 | dev_res->res->end += get_res_add_size(realloc_head, |
392 | dev_res->res); | |
3e6e0d80 | 393 | |
d74b9027 WY |
394 | /* |
395 | * There are two kinds of additional resources in the list: | |
396 | * 1. bridge resource -- IORESOURCE_STARTALIGN | |
0d607618 | 397 | * 2. SR-IOV resource -- IORESOURCE_SIZEALIGN |
d74b9027 WY |
398 | * Here just fix the additional alignment for bridge |
399 | */ | |
400 | if (!(dev_res->res->flags & IORESOURCE_STARTALIGN)) | |
401 | continue; | |
402 | ||
403 | add_align = get_res_add_align(realloc_head, dev_res->res); | |
404 | ||
405 | /* | |
0d607618 NJ |
406 | * The "head" list is sorted by alignment so resources with |
407 | * bigger alignment will be assigned first. After we | |
408 | * change the alignment of a dev_res in "head" list, we | |
409 | * need to reorder the list by alignment to make it | |
d74b9027 WY |
410 | * consistent. |
411 | */ | |
412 | if (add_align > dev_res->res->start) { | |
552bc94e YL |
413 | resource_size_t r_size = resource_size(dev_res->res); |
414 | ||
d74b9027 | 415 | dev_res->res->start = add_align; |
552bc94e | 416 | dev_res->res->end = add_align + r_size - 1; |
d74b9027 WY |
417 | |
418 | list_for_each_entry(dev_res2, head, list) { | |
419 | align = pci_resource_alignment(dev_res2->dev, | |
420 | dev_res2->res); | |
a6b65983 | 421 | if (add_align > align) { |
d74b9027 WY |
422 | list_move_tail(&dev_res->list, |
423 | &dev_res2->list); | |
a6b65983 WY |
424 | break; |
425 | } | |
d74b9027 | 426 | } |
ff3ce480 | 427 | } |
d74b9027 WY |
428 | |
429 | } | |
430 | ||
3e6e0d80 | 431 | /* Try updated head list with add_size added */ |
3e6e0d80 YL |
432 | assign_requested_resources_sorted(head, &local_fail_head); |
433 | ||
0d607618 | 434 | /* All assigned with add_size? */ |
bdc4abec | 435 | if (list_empty(&local_fail_head)) { |
3e6e0d80 | 436 | /* Remove head list from realloc_head list */ |
bdc4abec YL |
437 | list_for_each_entry(dev_res, head, list) |
438 | remove_from_list(realloc_head, dev_res->res); | |
bffc56d4 YL |
439 | free_list(&save_head); |
440 | free_list(head); | |
3e6e0d80 YL |
441 | return; |
442 | } | |
443 | ||
0d607618 | 444 | /* Check failed type */ |
aa914f5e | 445 | fail_type = pci_fail_res_type_mask(&local_fail_head); |
0d607618 | 446 | /* Remove not need to be released assigned res from head list etc */ |
aa914f5e YL |
447 | list_for_each_entry_safe(dev_res, tmp_res, head, list) |
448 | if (dev_res->res->parent && | |
449 | !pci_need_to_release(fail_type, dev_res->res)) { | |
0d607618 | 450 | /* Remove it from realloc_head list */ |
aa914f5e YL |
451 | remove_from_list(realloc_head, dev_res->res); |
452 | remove_from_list(&save_head, dev_res->res); | |
453 | list_del(&dev_res->list); | |
454 | kfree(dev_res); | |
455 | } | |
456 | ||
bffc56d4 | 457 | free_list(&local_fail_head); |
3e6e0d80 | 458 | /* Release assigned resource */ |
bdc4abec YL |
459 | list_for_each_entry(dev_res, head, list) |
460 | if (dev_res->res->parent) | |
461 | release_resource(dev_res->res); | |
3e6e0d80 | 462 | /* Restore start/end/flags from saved list */ |
b9b0bba9 YL |
463 | list_for_each_entry(save_res, &save_head, list) { |
464 | struct resource *res = save_res->res; | |
3e6e0d80 | 465 | |
b9b0bba9 YL |
466 | res->start = save_res->start; |
467 | res->end = save_res->end; | |
468 | res->flags = save_res->flags; | |
3e6e0d80 | 469 | } |
bffc56d4 | 470 | free_list(&save_head); |
3e6e0d80 YL |
471 | |
472 | requested_and_reassign: | |
c8adf9a3 RP |
473 | /* Satisfy the must-have resource requests */ |
474 | assign_requested_resources_sorted(head, fail_head); | |
475 | ||
0d607618 | 476 | /* Try to satisfy any additional optional resource requests */ |
9e8bf93a RP |
477 | if (realloc_head) |
478 | reassign_resources_sorted(realloc_head, head); | |
bffc56d4 | 479 | free_list(head); |
c8adf9a3 RP |
480 | } |
481 | ||
6841ec68 | 482 | static void pdev_assign_resources_sorted(struct pci_dev *dev, |
0d607618 NJ |
483 | struct list_head *add_head, |
484 | struct list_head *fail_head) | |
6841ec68 | 485 | { |
bdc4abec | 486 | LIST_HEAD(head); |
6841ec68 | 487 | |
6841ec68 | 488 | __dev_sort_resources(dev, &head); |
8424d759 | 489 | __assign_resources_sorted(&head, add_head, fail_head); |
6841ec68 YL |
490 | |
491 | } | |
492 | ||
493 | static void pbus_assign_resources_sorted(const struct pci_bus *bus, | |
bdc4abec YL |
494 | struct list_head *realloc_head, |
495 | struct list_head *fail_head) | |
6841ec68 YL |
496 | { |
497 | struct pci_dev *dev; | |
bdc4abec | 498 | LIST_HEAD(head); |
6841ec68 | 499 | |
6841ec68 YL |
500 | list_for_each_entry(dev, &bus->devices, bus_list) |
501 | __dev_sort_resources(dev, &head); | |
502 | ||
9e8bf93a | 503 | __assign_resources_sorted(&head, realloc_head, fail_head); |
6841ec68 YL |
504 | } |
505 | ||
b3743fa4 | 506 | void pci_setup_cardbus(struct pci_bus *bus) |
1da177e4 LT |
507 | { |
508 | struct pci_dev *bridge = bus->self; | |
c7dabef8 | 509 | struct resource *res; |
1da177e4 LT |
510 | struct pci_bus_region region; |
511 | ||
7506dc79 | 512 | pci_info(bridge, "CardBus bridge to %pR\n", |
b918c62e | 513 | &bus->busn_res); |
1da177e4 | 514 | |
c7dabef8 | 515 | res = bus->resource[0]; |
fc279850 | 516 | pcibios_resource_to_bus(bridge->bus, ®ion, res); |
c7dabef8 | 517 | if (res->flags & IORESOURCE_IO) { |
1da177e4 LT |
518 | /* |
519 | * The IO resource is allocated a range twice as large as it | |
520 | * would normally need. This allows us to set both IO regs. | |
521 | */ | |
7506dc79 | 522 | pci_info(bridge, " bridge window %pR\n", res); |
1da177e4 LT |
523 | pci_write_config_dword(bridge, PCI_CB_IO_BASE_0, |
524 | region.start); | |
525 | pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0, | |
526 | region.end); | |
527 | } | |
528 | ||
c7dabef8 | 529 | res = bus->resource[1]; |
fc279850 | 530 | pcibios_resource_to_bus(bridge->bus, ®ion, res); |
c7dabef8 | 531 | if (res->flags & IORESOURCE_IO) { |
7506dc79 | 532 | pci_info(bridge, " bridge window %pR\n", res); |
1da177e4 LT |
533 | pci_write_config_dword(bridge, PCI_CB_IO_BASE_1, |
534 | region.start); | |
535 | pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1, | |
536 | region.end); | |
537 | } | |
538 | ||
c7dabef8 | 539 | res = bus->resource[2]; |
fc279850 | 540 | pcibios_resource_to_bus(bridge->bus, ®ion, res); |
c7dabef8 | 541 | if (res->flags & IORESOURCE_MEM) { |
7506dc79 | 542 | pci_info(bridge, " bridge window %pR\n", res); |
1da177e4 LT |
543 | pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0, |
544 | region.start); | |
545 | pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0, | |
546 | region.end); | |
547 | } | |
548 | ||
c7dabef8 | 549 | res = bus->resource[3]; |
fc279850 | 550 | pcibios_resource_to_bus(bridge->bus, ®ion, res); |
c7dabef8 | 551 | if (res->flags & IORESOURCE_MEM) { |
7506dc79 | 552 | pci_info(bridge, " bridge window %pR\n", res); |
1da177e4 LT |
553 | pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1, |
554 | region.start); | |
555 | pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1, | |
556 | region.end); | |
557 | } | |
558 | } | |
b3743fa4 | 559 | EXPORT_SYMBOL(pci_setup_cardbus); |
1da177e4 | 560 | |
0d607618 NJ |
561 | /* |
562 | * Initialize bridges with base/limit values we have collected. PCI-to-PCI | |
563 | * Bridge Architecture Specification rev. 1.1 (1998) requires that if there | |
564 | * are no I/O ports or memory behind the bridge, the corresponding range | |
565 | * must be turned off by writing base value greater than limit to the | |
566 | * bridge's base/limit registers. | |
567 | * | |
568 | * Note: care must be taken when updating I/O base/limit registers of | |
569 | * bridges which support 32-bit I/O. This update requires two config space | |
570 | * writes, so it's quite possible that an I/O window of the bridge will | |
571 | * have some undesirable address (e.g. 0) after the first write. Ditto | |
572 | * 64-bit prefetchable MMIO. | |
573 | */ | |
3f2f4dc4 | 574 | static void pci_setup_bridge_io(struct pci_dev *bridge) |
1da177e4 | 575 | { |
c7dabef8 | 576 | struct resource *res; |
1da177e4 | 577 | struct pci_bus_region region; |
2b28ae19 BH |
578 | unsigned long io_mask; |
579 | u8 io_base_lo, io_limit_lo; | |
5b764b83 BH |
580 | u16 l; |
581 | u32 io_upper16; | |
1da177e4 | 582 | |
2b28ae19 BH |
583 | io_mask = PCI_IO_RANGE_MASK; |
584 | if (bridge->io_window_1k) | |
585 | io_mask = PCI_IO_1K_RANGE_MASK; | |
586 | ||
0d607618 | 587 | /* Set up the top and bottom of the PCI I/O segment for this bus */ |
6e0688db | 588 | res = &bridge->resource[PCI_BRIDGE_IO_WINDOW]; |
fc279850 | 589 | pcibios_resource_to_bus(bridge->bus, ®ion, res); |
c7dabef8 | 590 | if (res->flags & IORESOURCE_IO) { |
5b764b83 | 591 | pci_read_config_word(bridge, PCI_IO_BASE, &l); |
2b28ae19 BH |
592 | io_base_lo = (region.start >> 8) & io_mask; |
593 | io_limit_lo = (region.end >> 8) & io_mask; | |
5b764b83 | 594 | l = ((u16) io_limit_lo << 8) | io_base_lo; |
0d607618 | 595 | /* Set up upper 16 bits of I/O base/limit */ |
1da177e4 | 596 | io_upper16 = (region.end & 0xffff0000) | (region.start >> 16); |
7506dc79 | 597 | pci_info(bridge, " bridge window %pR\n", res); |
7cc5997d | 598 | } else { |
0d607618 | 599 | /* Clear upper 16 bits of I/O base/limit */ |
1da177e4 LT |
600 | io_upper16 = 0; |
601 | l = 0x00f0; | |
1da177e4 | 602 | } |
0d607618 | 603 | /* Temporarily disable the I/O range before updating PCI_IO_BASE */ |
1da177e4 | 604 | pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff); |
0d607618 | 605 | /* Update lower 16 bits of I/O base/limit */ |
5b764b83 | 606 | pci_write_config_word(bridge, PCI_IO_BASE, l); |
0d607618 | 607 | /* Update upper 16 bits of I/O base/limit */ |
1da177e4 | 608 | pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16); |
7cc5997d YL |
609 | } |
610 | ||
3f2f4dc4 | 611 | static void pci_setup_bridge_mmio(struct pci_dev *bridge) |
7cc5997d | 612 | { |
7cc5997d YL |
613 | struct resource *res; |
614 | struct pci_bus_region region; | |
615 | u32 l; | |
1da177e4 | 616 | |
0d607618 | 617 | /* Set up the top and bottom of the PCI Memory segment for this bus */ |
6e0688db | 618 | res = &bridge->resource[PCI_BRIDGE_MEM_WINDOW]; |
fc279850 | 619 | pcibios_resource_to_bus(bridge->bus, ®ion, res); |
c7dabef8 | 620 | if (res->flags & IORESOURCE_MEM) { |
1da177e4 LT |
621 | l = (region.start >> 16) & 0xfff0; |
622 | l |= region.end & 0xfff00000; | |
7506dc79 | 623 | pci_info(bridge, " bridge window %pR\n", res); |
7cc5997d | 624 | } else { |
1da177e4 | 625 | l = 0x0000fff0; |
1da177e4 LT |
626 | } |
627 | pci_write_config_dword(bridge, PCI_MEMORY_BASE, l); | |
7cc5997d YL |
628 | } |
629 | ||
3f2f4dc4 | 630 | static void pci_setup_bridge_mmio_pref(struct pci_dev *bridge) |
7cc5997d | 631 | { |
7cc5997d YL |
632 | struct resource *res; |
633 | struct pci_bus_region region; | |
634 | u32 l, bu, lu; | |
1da177e4 | 635 | |
0d607618 NJ |
636 | /* |
637 | * Clear out the upper 32 bits of PREF limit. If | |
638 | * PCI_PREF_BASE_UPPER32 was non-zero, this temporarily disables | |
639 | * PREF range, which is ok. | |
640 | */ | |
1da177e4 LT |
641 | pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0); |
642 | ||
0d607618 | 643 | /* Set up PREF base/limit */ |
c40a22e0 | 644 | bu = lu = 0; |
6e0688db | 645 | res = &bridge->resource[PCI_BRIDGE_PREF_MEM_WINDOW]; |
fc279850 | 646 | pcibios_resource_to_bus(bridge->bus, ®ion, res); |
c7dabef8 | 647 | if (res->flags & IORESOURCE_PREFETCH) { |
1da177e4 LT |
648 | l = (region.start >> 16) & 0xfff0; |
649 | l |= region.end & 0xfff00000; | |
c7dabef8 | 650 | if (res->flags & IORESOURCE_MEM_64) { |
1f82de10 YL |
651 | bu = upper_32_bits(region.start); |
652 | lu = upper_32_bits(region.end); | |
1f82de10 | 653 | } |
7506dc79 | 654 | pci_info(bridge, " bridge window %pR\n", res); |
7cc5997d | 655 | } else { |
1da177e4 | 656 | l = 0x0000fff0; |
1da177e4 LT |
657 | } |
658 | pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l); | |
659 | ||
0d607618 | 660 | /* Set the upper 32 bits of PREF base & limit */ |
59353ea3 AW |
661 | pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu); |
662 | pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu); | |
7cc5997d YL |
663 | } |
664 | ||
665 | static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type) | |
666 | { | |
667 | struct pci_dev *bridge = bus->self; | |
668 | ||
7506dc79 | 669 | pci_info(bridge, "PCI bridge to %pR\n", |
b918c62e | 670 | &bus->busn_res); |
7cc5997d YL |
671 | |
672 | if (type & IORESOURCE_IO) | |
3f2f4dc4 | 673 | pci_setup_bridge_io(bridge); |
7cc5997d YL |
674 | |
675 | if (type & IORESOURCE_MEM) | |
3f2f4dc4 | 676 | pci_setup_bridge_mmio(bridge); |
7cc5997d YL |
677 | |
678 | if (type & IORESOURCE_PREFETCH) | |
3f2f4dc4 | 679 | pci_setup_bridge_mmio_pref(bridge); |
1da177e4 LT |
680 | |
681 | pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl); | |
682 | } | |
683 | ||
d366d28c GS |
684 | void __weak pcibios_setup_bridge(struct pci_bus *bus, unsigned long type) |
685 | { | |
686 | } | |
687 | ||
e2444273 | 688 | void pci_setup_bridge(struct pci_bus *bus) |
7cc5997d YL |
689 | { |
690 | unsigned long type = IORESOURCE_IO | IORESOURCE_MEM | | |
691 | IORESOURCE_PREFETCH; | |
692 | ||
d366d28c | 693 | pcibios_setup_bridge(bus, type); |
7cc5997d YL |
694 | __pci_setup_bridge(bus, type); |
695 | } | |
696 | ||
8505e729 YL |
697 | |
698 | int pci_claim_bridge_resource(struct pci_dev *bridge, int i) | |
699 | { | |
700 | if (i < PCI_BRIDGE_RESOURCES || i > PCI_BRIDGE_RESOURCE_END) | |
701 | return 0; | |
702 | ||
703 | if (pci_claim_resource(bridge, i) == 0) | |
0d607618 | 704 | return 0; /* Claimed the window */ |
8505e729 YL |
705 | |
706 | if ((bridge->class >> 8) != PCI_CLASS_BRIDGE_PCI) | |
707 | return 0; | |
708 | ||
709 | if (!pci_bus_clip_resource(bridge, i)) | |
0d607618 | 710 | return -EINVAL; /* Clipping didn't change anything */ |
8505e729 | 711 | |
6e0688db KW |
712 | switch (i) { |
713 | case PCI_BRIDGE_IO_WINDOW: | |
8505e729 YL |
714 | pci_setup_bridge_io(bridge); |
715 | break; | |
6e0688db | 716 | case PCI_BRIDGE_MEM_WINDOW: |
8505e729 YL |
717 | pci_setup_bridge_mmio(bridge); |
718 | break; | |
6e0688db | 719 | case PCI_BRIDGE_PREF_MEM_WINDOW: |
8505e729 YL |
720 | pci_setup_bridge_mmio_pref(bridge); |
721 | break; | |
722 | default: | |
723 | return -EINVAL; | |
724 | } | |
725 | ||
726 | if (pci_claim_resource(bridge, i) == 0) | |
0d607618 | 727 | return 0; /* Claimed a smaller window */ |
8505e729 YL |
728 | |
729 | return -EINVAL; | |
730 | } | |
731 | ||
0d607618 NJ |
732 | /* |
733 | * Check whether the bridge supports optional I/O and prefetchable memory | |
734 | * ranges. If not, the respective base/limit registers must be read-only | |
735 | * and read as 0. | |
736 | */ | |
96bde06a | 737 | static void pci_bridge_check_ranges(struct pci_bus *bus) |
1da177e4 | 738 | { |
1da177e4 | 739 | struct pci_dev *bridge = bus->self; |
6e0688db | 740 | struct resource *b_res; |
1da177e4 | 741 | |
6e0688db KW |
742 | b_res = &bridge->resource[PCI_BRIDGE_MEM_WINDOW]; |
743 | b_res->flags |= IORESOURCE_MEM; | |
1da177e4 | 744 | |
6e0688db KW |
745 | if (bridge->io_window) { |
746 | b_res = &bridge->resource[PCI_BRIDGE_IO_WINDOW]; | |
747 | b_res->flags |= IORESOURCE_IO; | |
748 | } | |
d2f54d9b | 749 | |
51c48b31 | 750 | if (bridge->pref_window) { |
6e0688db KW |
751 | b_res = &bridge->resource[PCI_BRIDGE_PREF_MEM_WINDOW]; |
752 | b_res->flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH; | |
51c48b31 | 753 | if (bridge->pref_64_window) { |
6e0688db KW |
754 | b_res->flags |= IORESOURCE_MEM_64 | |
755 | PCI_PREF_RANGE_TYPE_64; | |
99586105 | 756 | } |
1f82de10 | 757 | } |
1da177e4 LT |
758 | } |
759 | ||
0d607618 | 760 | /* |
c13704f5 NJ |
761 | * Helper function for sizing routines. Assigned resources have non-NULL |
762 | * parent resource. | |
763 | * | |
764 | * Return first unassigned resource of the correct type. If there is none, | |
765 | * return first assigned resource of the correct type. If none of the | |
766 | * above, return NULL. | |
767 | * | |
768 | * Returning an assigned resource of the correct type allows the caller to | |
769 | * distinguish between already assigned and no resource of the correct type. | |
0d607618 | 770 | */ |
c13704f5 NJ |
771 | static struct resource *find_bus_resource_of_type(struct pci_bus *bus, |
772 | unsigned long type_mask, | |
773 | unsigned long type) | |
1da177e4 | 774 | { |
c13704f5 | 775 | struct resource *r, *r_assigned = NULL; |
1da177e4 | 776 | int i; |
1da177e4 | 777 | |
89a74ecc | 778 | pci_bus_for_each_resource(bus, r, i) { |
299de034 IK |
779 | if (r == &ioport_resource || r == &iomem_resource) |
780 | continue; | |
55a10984 JB |
781 | if (r && (r->flags & type_mask) == type && !r->parent) |
782 | return r; | |
c13704f5 NJ |
783 | if (r && (r->flags & type_mask) == type && !r_assigned) |
784 | r_assigned = r; | |
1da177e4 | 785 | } |
c13704f5 | 786 | return r_assigned; |
1da177e4 LT |
787 | } |
788 | ||
13583b16 | 789 | static resource_size_t calculate_iosize(resource_size_t size, |
0d607618 NJ |
790 | resource_size_t min_size, |
791 | resource_size_t size1, | |
792 | resource_size_t add_size, | |
793 | resource_size_t children_add_size, | |
794 | resource_size_t old_size, | |
795 | resource_size_t align) | |
13583b16 RP |
796 | { |
797 | if (size < min_size) | |
798 | size = min_size; | |
3c78bc61 | 799 | if (old_size == 1) |
13583b16 | 800 | old_size = 0; |
0d607618 NJ |
801 | /* |
802 | * To be fixed in 2.5: we should have sort of HAVE_ISA flag in the | |
803 | * struct pci_bus. | |
804 | */ | |
13583b16 RP |
805 | #if defined(CONFIG_ISA) || defined(CONFIG_EISA) |
806 | size = (size & 0xff) + ((size & ~0xffUL) << 2); | |
807 | #endif | |
de3ffa30 | 808 | size = size + size1; |
13583b16 RP |
809 | if (size < old_size) |
810 | size = old_size; | |
de3ffa30 JD |
811 | |
812 | size = ALIGN(max(size, add_size) + children_add_size, align); | |
13583b16 RP |
813 | return size; |
814 | } | |
815 | ||
816 | static resource_size_t calculate_memsize(resource_size_t size, | |
0d607618 NJ |
817 | resource_size_t min_size, |
818 | resource_size_t add_size, | |
819 | resource_size_t children_add_size, | |
820 | resource_size_t old_size, | |
821 | resource_size_t align) | |
13583b16 RP |
822 | { |
823 | if (size < min_size) | |
824 | size = min_size; | |
3c78bc61 | 825 | if (old_size == 1) |
13583b16 RP |
826 | old_size = 0; |
827 | if (size < old_size) | |
828 | size = old_size; | |
de3ffa30 JD |
829 | |
830 | size = ALIGN(max(size, add_size) + children_add_size, align); | |
13583b16 RP |
831 | return size; |
832 | } | |
833 | ||
ac5ad93e GS |
834 | resource_size_t __weak pcibios_window_alignment(struct pci_bus *bus, |
835 | unsigned long type) | |
836 | { | |
837 | return 1; | |
838 | } | |
839 | ||
840 | #define PCI_P2P_DEFAULT_MEM_ALIGN 0x100000 /* 1MiB */ | |
841 | #define PCI_P2P_DEFAULT_IO_ALIGN 0x1000 /* 4KiB */ | |
842 | #define PCI_P2P_DEFAULT_IO_ALIGN_1K 0x400 /* 1KiB */ | |
843 | ||
0d607618 | 844 | static resource_size_t window_alignment(struct pci_bus *bus, unsigned long type) |
ac5ad93e GS |
845 | { |
846 | resource_size_t align = 1, arch_align; | |
847 | ||
848 | if (type & IORESOURCE_MEM) | |
849 | align = PCI_P2P_DEFAULT_MEM_ALIGN; | |
850 | else if (type & IORESOURCE_IO) { | |
851 | /* | |
0d607618 NJ |
852 | * Per spec, I/O windows are 4K-aligned, but some bridges have |
853 | * an extension to support 1K alignment. | |
ac5ad93e | 854 | */ |
2c8d5a2d | 855 | if (bus->self && bus->self->io_window_1k) |
ac5ad93e GS |
856 | align = PCI_P2P_DEFAULT_IO_ALIGN_1K; |
857 | else | |
858 | align = PCI_P2P_DEFAULT_IO_ALIGN; | |
859 | } | |
860 | ||
861 | arch_align = pcibios_window_alignment(bus, type); | |
862 | return max(align, arch_align); | |
863 | } | |
864 | ||
c8adf9a3 | 865 | /** |
0d607618 | 866 | * pbus_size_io() - Size the I/O window of a given bus |
c8adf9a3 | 867 | * |
0d607618 NJ |
868 | * @bus: The bus |
869 | * @min_size: The minimum I/O window that must be allocated | |
870 | * @add_size: Additional optional I/O window | |
871 | * @realloc_head: Track the additional I/O window on this list | |
c8adf9a3 | 872 | * |
0d607618 NJ |
873 | * Sizing the I/O windows of the PCI-PCI bridge is trivial, since these |
874 | * windows have 1K or 4K granularity and the I/O ranges of non-bridge PCI | |
875 | * devices are limited to 256 bytes. We must be careful with the ISA | |
876 | * aliasing though. | |
c8adf9a3 RP |
877 | */ |
878 | static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size, | |
0d607618 NJ |
879 | resource_size_t add_size, |
880 | struct list_head *realloc_head) | |
1da177e4 LT |
881 | { |
882 | struct pci_dev *dev; | |
c13704f5 NJ |
883 | struct resource *b_res = find_bus_resource_of_type(bus, IORESOURCE_IO, |
884 | IORESOURCE_IO); | |
11251a86 | 885 | resource_size_t size = 0, size0 = 0, size1 = 0; |
be768912 | 886 | resource_size_t children_add_size = 0; |
2d1d6678 | 887 | resource_size_t min_align, align; |
1da177e4 LT |
888 | |
889 | if (!b_res) | |
f7625980 | 890 | return; |
1da177e4 | 891 | |
c13704f5 NJ |
892 | /* If resource is already assigned, nothing more to do */ |
893 | if (b_res->parent) | |
894 | return; | |
895 | ||
2d1d6678 | 896 | min_align = window_alignment(bus, IORESOURCE_IO); |
1da177e4 LT |
897 | list_for_each_entry(dev, &bus->devices, bus_list) { |
898 | int i; | |
899 | ||
900 | for (i = 0; i < PCI_NUM_RESOURCES; i++) { | |
901 | struct resource *r = &dev->resource[i]; | |
902 | unsigned long r_size; | |
903 | ||
904 | if (r->parent || !(r->flags & IORESOURCE_IO)) | |
905 | continue; | |
022edd86 | 906 | r_size = resource_size(r); |
1da177e4 LT |
907 | |
908 | if (r_size < 0x400) | |
909 | /* Might be re-aligned for ISA */ | |
910 | size += r_size; | |
911 | else | |
912 | size1 += r_size; | |
be768912 | 913 | |
fd591341 YL |
914 | align = pci_resource_alignment(dev, r); |
915 | if (align > min_align) | |
916 | min_align = align; | |
917 | ||
9e8bf93a RP |
918 | if (realloc_head) |
919 | children_add_size += get_res_add_size(realloc_head, r); | |
1da177e4 LT |
920 | } |
921 | } | |
fd591341 | 922 | |
de3ffa30 | 923 | size0 = calculate_iosize(size, min_size, size1, 0, 0, |
fd591341 | 924 | resource_size(b_res), min_align); |
de3ffa30 JD |
925 | size1 = (!realloc_head || (realloc_head && !add_size && !children_add_size)) ? size0 : |
926 | calculate_iosize(size, min_size, size1, add_size, children_add_size, | |
fd591341 | 927 | resource_size(b_res), min_align); |
c8adf9a3 | 928 | if (!size0 && !size1) { |
2c8d5a2d | 929 | if (bus->self && (b_res->start || b_res->end)) |
7506dc79 | 930 | pci_info(bus->self, "disabling bridge window %pR to %pR (unused)\n", |
227f0647 | 931 | b_res, &bus->busn_res); |
1da177e4 LT |
932 | b_res->flags = 0; |
933 | return; | |
934 | } | |
fd591341 YL |
935 | |
936 | b_res->start = min_align; | |
c8adf9a3 | 937 | b_res->end = b_res->start + size0 - 1; |
88452565 | 938 | b_res->flags |= IORESOURCE_STARTALIGN; |
2c8d5a2d | 939 | if (bus->self && size1 > size0 && realloc_head) { |
fd591341 YL |
940 | add_to_list(realloc_head, bus->self, b_res, size1-size0, |
941 | min_align); | |
34c6b710 MK |
942 | pci_info(bus->self, "bridge window %pR to %pR add_size %llx\n", |
943 | b_res, &bus->busn_res, | |
944 | (unsigned long long) size1 - size0); | |
b592443d | 945 | } |
1da177e4 LT |
946 | } |
947 | ||
c121504e GS |
948 | static inline resource_size_t calculate_mem_align(resource_size_t *aligns, |
949 | int max_order) | |
950 | { | |
951 | resource_size_t align = 0; | |
952 | resource_size_t min_align = 0; | |
953 | int order; | |
954 | ||
955 | for (order = 0; order <= max_order; order++) { | |
956 | resource_size_t align1 = 1; | |
957 | ||
958 | align1 <<= (order + 20); | |
959 | ||
960 | if (!align) | |
961 | min_align = align1; | |
962 | else if (ALIGN(align + min_align, min_align) < align1) | |
963 | min_align = align1 >> 1; | |
964 | align += aligns[order]; | |
965 | } | |
966 | ||
967 | return min_align; | |
968 | } | |
969 | ||
c8adf9a3 | 970 | /** |
0d607618 | 971 | * pbus_size_mem() - Size the memory window of a given bus |
c8adf9a3 | 972 | * |
0d607618 NJ |
973 | * @bus: The bus |
974 | * @mask: Mask the resource flag, then compare it with type | |
975 | * @type: The type of free resource from bridge | |
976 | * @type2: Second match type | |
977 | * @type3: Third match type | |
978 | * @min_size: The minimum memory window that must be allocated | |
979 | * @add_size: Additional optional memory window | |
980 | * @realloc_head: Track the additional memory window on this list | |
c8adf9a3 | 981 | * |
0d607618 NJ |
982 | * Calculate the size of the bus and minimal alignment which guarantees |
983 | * that all child resources fit in this size. | |
30afe8d0 | 984 | * |
0d607618 NJ |
985 | * Return -ENOSPC if there's no available bus resource of the desired |
986 | * type. Otherwise, set the bus resource start/end to indicate the | |
987 | * required size, add things to realloc_head (if supplied), and return 0. | |
c8adf9a3 | 988 | */ |
28760489 | 989 | static int pbus_size_mem(struct pci_bus *bus, unsigned long mask, |
5b285415 | 990 | unsigned long type, unsigned long type2, |
0d607618 NJ |
991 | unsigned long type3, resource_size_t min_size, |
992 | resource_size_t add_size, | |
5b285415 | 993 | struct list_head *realloc_head) |
1da177e4 LT |
994 | { |
995 | struct pci_dev *dev; | |
c8adf9a3 | 996 | resource_size_t min_align, align, size, size0, size1; |
3dc8a1f6 | 997 | resource_size_t aligns[24]; /* Alignments from 1MB to 8TB */ |
1da177e4 | 998 | int order, max_order; |
c13704f5 | 999 | struct resource *b_res = find_bus_resource_of_type(bus, |
5b285415 | 1000 | mask | IORESOURCE_PREFETCH, type); |
be768912 | 1001 | resource_size_t children_add_size = 0; |
d74b9027 WY |
1002 | resource_size_t children_add_align = 0; |
1003 | resource_size_t add_align = 0; | |
1da177e4 LT |
1004 | |
1005 | if (!b_res) | |
30afe8d0 | 1006 | return -ENOSPC; |
1da177e4 | 1007 | |
c13704f5 NJ |
1008 | /* If resource is already assigned, nothing more to do */ |
1009 | if (b_res->parent) | |
1010 | return 0; | |
1011 | ||
1da177e4 LT |
1012 | memset(aligns, 0, sizeof(aligns)); |
1013 | max_order = 0; | |
1014 | size = 0; | |
1015 | ||
1016 | list_for_each_entry(dev, &bus->devices, bus_list) { | |
1017 | int i; | |
1f82de10 | 1018 | |
1da177e4 LT |
1019 | for (i = 0; i < PCI_NUM_RESOURCES; i++) { |
1020 | struct resource *r = &dev->resource[i]; | |
c40a22e0 | 1021 | resource_size_t r_size; |
1da177e4 | 1022 | |
a2220d80 DD |
1023 | if (r->parent || (r->flags & IORESOURCE_PCI_FIXED) || |
1024 | ((r->flags & mask) != type && | |
1025 | (r->flags & mask) != type2 && | |
1026 | (r->flags & mask) != type3)) | |
1da177e4 | 1027 | continue; |
022edd86 | 1028 | r_size = resource_size(r); |
2aceefcb | 1029 | #ifdef CONFIG_PCI_IOV |
0d607618 | 1030 | /* Put SRIOV requested res to the optional list */ |
9e8bf93a | 1031 | if (realloc_head && i >= PCI_IOV_RESOURCES && |
2aceefcb | 1032 | i <= PCI_IOV_RESOURCE_END) { |
d74b9027 | 1033 | add_align = max(pci_resource_alignment(dev, r), add_align); |
2aceefcb | 1034 | r->end = r->start - 1; |
0d607618 | 1035 | add_to_list(realloc_head, dev, r, r_size, 0 /* Don't care */); |
2aceefcb YL |
1036 | children_add_size += r_size; |
1037 | continue; | |
1038 | } | |
1039 | #endif | |
14c8530d A |
1040 | /* |
1041 | * aligns[0] is for 1MB (since bridge memory | |
1042 | * windows are always at least 1MB aligned), so | |
1043 | * keep "order" from being negative for smaller | |
1044 | * resources. | |
1045 | */ | |
6faf17f6 | 1046 | align = pci_resource_alignment(dev, r); |
1da177e4 | 1047 | order = __ffs(align) - 20; |
14c8530d A |
1048 | if (order < 0) |
1049 | order = 0; | |
1050 | if (order >= ARRAY_SIZE(aligns)) { | |
7506dc79 | 1051 | pci_warn(dev, "disabling BAR %d: %pR (bad alignment %#llx)\n", |
227f0647 | 1052 | i, r, (unsigned long long) align); |
1da177e4 LT |
1053 | r->flags = 0; |
1054 | continue; | |
1055 | } | |
c9c75143 | 1056 | size += max(r_size, align); |
0d607618 NJ |
1057 | /* |
1058 | * Exclude ranges with size > align from calculation of | |
1059 | * the alignment. | |
1060 | */ | |
c9c75143 | 1061 | if (r_size <= align) |
1da177e4 LT |
1062 | aligns[order] += align; |
1063 | if (order > max_order) | |
1064 | max_order = order; | |
be768912 | 1065 | |
d74b9027 | 1066 | if (realloc_head) { |
9e8bf93a | 1067 | children_add_size += get_res_add_size(realloc_head, r); |
d74b9027 WY |
1068 | children_add_align = get_res_add_align(realloc_head, r); |
1069 | add_align = max(add_align, children_add_align); | |
1070 | } | |
1da177e4 LT |
1071 | } |
1072 | } | |
462d9303 | 1073 | |
c121504e | 1074 | min_align = calculate_mem_align(aligns, max_order); |
3ad94b0d | 1075 | min_align = max(min_align, window_alignment(bus, b_res->flags)); |
de3ffa30 | 1076 | size0 = calculate_memsize(size, min_size, 0, 0, resource_size(b_res), min_align); |
d74b9027 | 1077 | add_align = max(min_align, add_align); |
de3ffa30 JD |
1078 | size1 = (!realloc_head || (realloc_head && !add_size && !children_add_size)) ? size0 : |
1079 | calculate_memsize(size, min_size, add_size, children_add_size, | |
d74b9027 | 1080 | resource_size(b_res), add_align); |
c8adf9a3 | 1081 | if (!size0 && !size1) { |
2c8d5a2d | 1082 | if (bus->self && (b_res->start || b_res->end)) |
7506dc79 | 1083 | pci_info(bus->self, "disabling bridge window %pR to %pR (unused)\n", |
227f0647 | 1084 | b_res, &bus->busn_res); |
1da177e4 | 1085 | b_res->flags = 0; |
30afe8d0 | 1086 | return 0; |
1da177e4 LT |
1087 | } |
1088 | b_res->start = min_align; | |
c8adf9a3 | 1089 | b_res->end = size0 + min_align - 1; |
5b285415 | 1090 | b_res->flags |= IORESOURCE_STARTALIGN; |
2c8d5a2d | 1091 | if (bus->self && size1 > size0 && realloc_head) { |
d74b9027 | 1092 | add_to_list(realloc_head, bus->self, b_res, size1-size0, add_align); |
34c6b710 | 1093 | pci_info(bus->self, "bridge window %pR to %pR add_size %llx add_align %llx\n", |
227f0647 | 1094 | b_res, &bus->busn_res, |
d74b9027 WY |
1095 | (unsigned long long) (size1 - size0), |
1096 | (unsigned long long) add_align); | |
b592443d | 1097 | } |
30afe8d0 | 1098 | return 0; |
1da177e4 LT |
1099 | } |
1100 | ||
0a2daa1c RP |
1101 | unsigned long pci_cardbus_resource_alignment(struct resource *res) |
1102 | { | |
1103 | if (res->flags & IORESOURCE_IO) | |
1104 | return pci_cardbus_io_size; | |
1105 | if (res->flags & IORESOURCE_MEM) | |
1106 | return pci_cardbus_mem_size; | |
1107 | return 0; | |
1108 | } | |
1109 | ||
1110 | static void pci_bus_size_cardbus(struct pci_bus *bus, | |
0d607618 | 1111 | struct list_head *realloc_head) |
1da177e4 LT |
1112 | { |
1113 | struct pci_dev *bridge = bus->self; | |
6e0688db | 1114 | struct resource *b_res; |
11848934 | 1115 | resource_size_t b_res_3_size = pci_cardbus_mem_size * 2; |
1da177e4 LT |
1116 | u16 ctrl; |
1117 | ||
6e0688db KW |
1118 | b_res = &bridge->resource[PCI_CB_BRIDGE_IO_0_WINDOW]; |
1119 | if (b_res->parent) | |
3796f1e2 | 1120 | goto handle_b_res_1; |
1da177e4 | 1121 | /* |
0d607618 NJ |
1122 | * Reserve some resources for CardBus. We reserve a fixed amount |
1123 | * of bus space for CardBus bridges. | |
1da177e4 | 1124 | */ |
6e0688db KW |
1125 | b_res->start = pci_cardbus_io_size; |
1126 | b_res->end = b_res->start + pci_cardbus_io_size - 1; | |
1127 | b_res->flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN; | |
11848934 | 1128 | if (realloc_head) { |
6e0688db | 1129 | b_res->end -= pci_cardbus_io_size; |
11848934 | 1130 | add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size, |
6e0688db | 1131 | pci_cardbus_io_size); |
11848934 | 1132 | } |
1da177e4 | 1133 | |
3796f1e2 | 1134 | handle_b_res_1: |
6e0688db KW |
1135 | b_res = &bridge->resource[PCI_CB_BRIDGE_IO_1_WINDOW]; |
1136 | if (b_res->parent) | |
3796f1e2 | 1137 | goto handle_b_res_2; |
6e0688db KW |
1138 | b_res->start = pci_cardbus_io_size; |
1139 | b_res->end = b_res->start + pci_cardbus_io_size - 1; | |
1140 | b_res->flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN; | |
11848934 | 1141 | if (realloc_head) { |
6e0688db KW |
1142 | b_res->end -= pci_cardbus_io_size; |
1143 | add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size, | |
1144 | pci_cardbus_io_size); | |
11848934 | 1145 | } |
1da177e4 | 1146 | |
3796f1e2 | 1147 | handle_b_res_2: |
0d607618 | 1148 | /* MEM1 must not be pref MMIO */ |
dcef0d06 YL |
1149 | pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); |
1150 | if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM1) { | |
1151 | ctrl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1; | |
1152 | pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl); | |
1153 | pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); | |
1154 | } | |
1155 | ||
0d607618 | 1156 | /* Check whether prefetchable memory is supported by this bridge. */ |
1da177e4 LT |
1157 | pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); |
1158 | if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) { | |
1159 | ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0; | |
1160 | pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl); | |
1161 | pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); | |
1162 | } | |
1163 | ||
6e0688db KW |
1164 | b_res = &bridge->resource[PCI_CB_BRIDGE_MEM_0_WINDOW]; |
1165 | if (b_res->parent) | |
3796f1e2 | 1166 | goto handle_b_res_3; |
1da177e4 | 1167 | /* |
0d607618 NJ |
1168 | * If we have prefetchable memory support, allocate two regions. |
1169 | * Otherwise, allocate one region of twice the size. | |
1da177e4 LT |
1170 | */ |
1171 | if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) { | |
6e0688db KW |
1172 | b_res->start = pci_cardbus_mem_size; |
1173 | b_res->end = b_res->start + pci_cardbus_mem_size - 1; | |
1174 | b_res->flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | | |
1175 | IORESOURCE_STARTALIGN; | |
11848934 | 1176 | if (realloc_head) { |
6e0688db KW |
1177 | b_res->end -= pci_cardbus_mem_size; |
1178 | add_to_list(realloc_head, bridge, b_res, | |
1179 | pci_cardbus_mem_size, pci_cardbus_mem_size); | |
11848934 YL |
1180 | } |
1181 | ||
0d607618 | 1182 | /* Reduce that to half */ |
11848934 YL |
1183 | b_res_3_size = pci_cardbus_mem_size; |
1184 | } | |
1185 | ||
3796f1e2 | 1186 | handle_b_res_3: |
6e0688db KW |
1187 | b_res = &bridge->resource[PCI_CB_BRIDGE_MEM_1_WINDOW]; |
1188 | if (b_res->parent) | |
3796f1e2 | 1189 | goto handle_done; |
6e0688db KW |
1190 | b_res->start = pci_cardbus_mem_size; |
1191 | b_res->end = b_res->start + b_res_3_size - 1; | |
1192 | b_res->flags |= IORESOURCE_MEM | IORESOURCE_STARTALIGN; | |
11848934 | 1193 | if (realloc_head) { |
6e0688db KW |
1194 | b_res->end -= b_res_3_size; |
1195 | add_to_list(realloc_head, bridge, b_res, b_res_3_size, | |
1196 | pci_cardbus_mem_size); | |
11848934 | 1197 | } |
3796f1e2 YL |
1198 | |
1199 | handle_done: | |
1200 | ; | |
1da177e4 LT |
1201 | } |
1202 | ||
10874f5a | 1203 | void __pci_bus_size_bridges(struct pci_bus *bus, struct list_head *realloc_head) |
1da177e4 LT |
1204 | { |
1205 | struct pci_dev *dev; | |
5b285415 | 1206 | unsigned long mask, prefmask, type2 = 0, type3 = 0; |
d7b8a217 NJ |
1207 | resource_size_t additional_io_size = 0, additional_mmio_size = 0, |
1208 | additional_mmio_pref_size = 0; | |
2c8d5a2d IK |
1209 | struct resource *pref; |
1210 | struct pci_host_bridge *host; | |
1211 | int hdr_type, i, ret; | |
1da177e4 LT |
1212 | |
1213 | list_for_each_entry(dev, &bus->devices, bus_list) { | |
1214 | struct pci_bus *b = dev->subordinate; | |
1215 | if (!b) | |
1216 | continue; | |
1217 | ||
b2fb5cc5 HZ |
1218 | switch (dev->hdr_type) { |
1219 | case PCI_HEADER_TYPE_CARDBUS: | |
9e8bf93a | 1220 | pci_bus_size_cardbus(b, realloc_head); |
1da177e4 LT |
1221 | break; |
1222 | ||
b2fb5cc5 | 1223 | case PCI_HEADER_TYPE_BRIDGE: |
1da177e4 | 1224 | default: |
9e8bf93a | 1225 | __pci_bus_size_bridges(b, realloc_head); |
1da177e4 LT |
1226 | break; |
1227 | } | |
1228 | } | |
1229 | ||
1230 | /* The root bus? */ | |
2c8d5a2d IK |
1231 | if (pci_is_root_bus(bus)) { |
1232 | host = to_pci_host_bridge(bus->bridge); | |
1233 | if (!host->size_windows) | |
1234 | return; | |
1235 | pci_bus_for_each_resource(bus, pref, i) | |
1236 | if (pref && (pref->flags & IORESOURCE_PREFETCH)) | |
1237 | break; | |
1238 | hdr_type = -1; /* Intentionally invalid - not a PCI device. */ | |
1239 | } else { | |
6e0688db | 1240 | pref = &bus->self->resource[PCI_BRIDGE_PREF_MEM_WINDOW]; |
2c8d5a2d IK |
1241 | hdr_type = bus->self->hdr_type; |
1242 | } | |
1da177e4 | 1243 | |
2c8d5a2d | 1244 | switch (hdr_type) { |
b2fb5cc5 | 1245 | case PCI_HEADER_TYPE_CARDBUS: |
0d607618 | 1246 | /* Don't size CardBuses yet */ |
1da177e4 LT |
1247 | break; |
1248 | ||
b2fb5cc5 | 1249 | case PCI_HEADER_TYPE_BRIDGE: |
1da177e4 | 1250 | pci_bridge_check_ranges(bus); |
28760489 | 1251 | if (bus->self->is_hotplug_bridge) { |
c8adf9a3 | 1252 | additional_io_size = pci_hotplug_io_size; |
d7b8a217 NJ |
1253 | additional_mmio_size = pci_hotplug_mmio_size; |
1254 | additional_mmio_pref_size = pci_hotplug_mmio_pref_size; | |
28760489 | 1255 | } |
df561f66 | 1256 | fallthrough; |
1da177e4 | 1257 | default: |
19aa7ee4 YL |
1258 | pbus_size_io(bus, realloc_head ? 0 : additional_io_size, |
1259 | additional_io_size, realloc_head); | |
67d29b5c BH |
1260 | |
1261 | /* | |
1262 | * If there's a 64-bit prefetchable MMIO window, compute | |
1263 | * the size required to put all 64-bit prefetchable | |
1264 | * resources in it. | |
1265 | */ | |
1da177e4 LT |
1266 | mask = IORESOURCE_MEM; |
1267 | prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH; | |
2c8d5a2d | 1268 | if (pref && (pref->flags & IORESOURCE_MEM_64)) { |
5b285415 | 1269 | prefmask |= IORESOURCE_MEM_64; |
30afe8d0 | 1270 | ret = pbus_size_mem(bus, prefmask, prefmask, |
d7b8a217 NJ |
1271 | prefmask, prefmask, |
1272 | realloc_head ? 0 : additional_mmio_pref_size, | |
1273 | additional_mmio_pref_size, realloc_head); | |
67d29b5c BH |
1274 | |
1275 | /* | |
1276 | * If successful, all non-prefetchable resources | |
1277 | * and any 32-bit prefetchable resources will go in | |
1278 | * the non-prefetchable window. | |
1279 | */ | |
30afe8d0 | 1280 | if (ret == 0) { |
30afe8d0 BH |
1281 | mask = prefmask; |
1282 | type2 = prefmask & ~IORESOURCE_MEM_64; | |
1283 | type3 = prefmask & ~IORESOURCE_PREFETCH; | |
5b285415 YL |
1284 | } |
1285 | } | |
67d29b5c BH |
1286 | |
1287 | /* | |
1288 | * If there is no 64-bit prefetchable window, compute the | |
1289 | * size required to put all prefetchable resources in the | |
1290 | * 32-bit prefetchable window (if there is one). | |
1291 | */ | |
5b285415 YL |
1292 | if (!type2) { |
1293 | prefmask &= ~IORESOURCE_MEM_64; | |
30afe8d0 | 1294 | ret = pbus_size_mem(bus, prefmask, prefmask, |
d7b8a217 NJ |
1295 | prefmask, prefmask, |
1296 | realloc_head ? 0 : additional_mmio_pref_size, | |
1297 | additional_mmio_pref_size, realloc_head); | |
67d29b5c BH |
1298 | |
1299 | /* | |
1300 | * If successful, only non-prefetchable resources | |
1301 | * will go in the non-prefetchable window. | |
1302 | */ | |
1303 | if (ret == 0) | |
5b285415 | 1304 | mask = prefmask; |
67d29b5c | 1305 | else |
d7b8a217 | 1306 | additional_mmio_size += additional_mmio_pref_size; |
67d29b5c | 1307 | |
5b285415 YL |
1308 | type2 = type3 = IORESOURCE_MEM; |
1309 | } | |
67d29b5c BH |
1310 | |
1311 | /* | |
1312 | * Compute the size required to put everything else in the | |
0d607618 | 1313 | * non-prefetchable window. This includes: |
67d29b5c BH |
1314 | * |
1315 | * - all non-prefetchable resources | |
1316 | * - 32-bit prefetchable resources if there's a 64-bit | |
1317 | * prefetchable window or no prefetchable window at all | |
0d607618 NJ |
1318 | * - 64-bit prefetchable resources if there's no prefetchable |
1319 | * window at all | |
67d29b5c | 1320 | * |
0d607618 NJ |
1321 | * Note that the strategy in __pci_assign_resource() must match |
1322 | * that used here. Specifically, we cannot put a 32-bit | |
1323 | * prefetchable resource in a 64-bit prefetchable window. | |
67d29b5c | 1324 | */ |
5b285415 | 1325 | pbus_size_mem(bus, mask, IORESOURCE_MEM, type2, type3, |
d7b8a217 NJ |
1326 | realloc_head ? 0 : additional_mmio_size, |
1327 | additional_mmio_size, realloc_head); | |
1da177e4 LT |
1328 | break; |
1329 | } | |
1330 | } | |
c8adf9a3 | 1331 | |
10874f5a | 1332 | void pci_bus_size_bridges(struct pci_bus *bus) |
c8adf9a3 RP |
1333 | { |
1334 | __pci_bus_size_bridges(bus, NULL); | |
1335 | } | |
1da177e4 LT |
1336 | EXPORT_SYMBOL(pci_bus_size_bridges); |
1337 | ||
d04d0111 DD |
1338 | static void assign_fixed_resource_on_bus(struct pci_bus *b, struct resource *r) |
1339 | { | |
1340 | int i; | |
1341 | struct resource *parent_r; | |
1342 | unsigned long mask = IORESOURCE_IO | IORESOURCE_MEM | | |
1343 | IORESOURCE_PREFETCH; | |
1344 | ||
1345 | pci_bus_for_each_resource(b, parent_r, i) { | |
1346 | if (!parent_r) | |
1347 | continue; | |
1348 | ||
1349 | if ((r->flags & mask) == (parent_r->flags & mask) && | |
1350 | resource_contains(parent_r, r)) | |
1351 | request_resource(parent_r, r); | |
1352 | } | |
1353 | } | |
1354 | ||
1355 | /* | |
0d607618 NJ |
1356 | * Try to assign any resources marked as IORESOURCE_PCI_FIXED, as they are |
1357 | * skipped by pbus_assign_resources_sorted(). | |
d04d0111 DD |
1358 | */ |
1359 | static void pdev_assign_fixed_resources(struct pci_dev *dev) | |
1360 | { | |
1361 | int i; | |
1362 | ||
1363 | for (i = 0; i < PCI_NUM_RESOURCES; i++) { | |
1364 | struct pci_bus *b; | |
1365 | struct resource *r = &dev->resource[i]; | |
1366 | ||
1367 | if (r->parent || !(r->flags & IORESOURCE_PCI_FIXED) || | |
1368 | !(r->flags & (IORESOURCE_IO | IORESOURCE_MEM))) | |
1369 | continue; | |
1370 | ||
1371 | b = dev->bus; | |
1372 | while (b && !r->parent) { | |
1373 | assign_fixed_resource_on_bus(b, r); | |
1374 | b = b->parent; | |
1375 | } | |
1376 | } | |
1377 | } | |
1378 | ||
10874f5a BH |
1379 | void __pci_bus_assign_resources(const struct pci_bus *bus, |
1380 | struct list_head *realloc_head, | |
1381 | struct list_head *fail_head) | |
1da177e4 LT |
1382 | { |
1383 | struct pci_bus *b; | |
1384 | struct pci_dev *dev; | |
1385 | ||
9e8bf93a | 1386 | pbus_assign_resources_sorted(bus, realloc_head, fail_head); |
1da177e4 | 1387 | |
1da177e4 | 1388 | list_for_each_entry(dev, &bus->devices, bus_list) { |
d04d0111 DD |
1389 | pdev_assign_fixed_resources(dev); |
1390 | ||
1da177e4 LT |
1391 | b = dev->subordinate; |
1392 | if (!b) | |
1393 | continue; | |
1394 | ||
9e8bf93a | 1395 | __pci_bus_assign_resources(b, realloc_head, fail_head); |
1da177e4 | 1396 | |
b2fb5cc5 HZ |
1397 | switch (dev->hdr_type) { |
1398 | case PCI_HEADER_TYPE_BRIDGE: | |
6841ec68 YL |
1399 | if (!pci_is_enabled(dev)) |
1400 | pci_setup_bridge(b); | |
1da177e4 LT |
1401 | break; |
1402 | ||
b2fb5cc5 | 1403 | case PCI_HEADER_TYPE_CARDBUS: |
1da177e4 LT |
1404 | pci_setup_cardbus(b); |
1405 | break; | |
1406 | ||
1407 | default: | |
7506dc79 | 1408 | pci_info(dev, "not setting up bridge for bus %04x:%02x\n", |
227f0647 | 1409 | pci_domain_nr(b), b->number); |
1da177e4 LT |
1410 | break; |
1411 | } | |
1412 | } | |
1413 | } | |
568ddef8 | 1414 | |
10874f5a | 1415 | void pci_bus_assign_resources(const struct pci_bus *bus) |
568ddef8 | 1416 | { |
c8adf9a3 | 1417 | __pci_bus_assign_resources(bus, NULL, NULL); |
568ddef8 | 1418 | } |
1da177e4 LT |
1419 | EXPORT_SYMBOL(pci_bus_assign_resources); |
1420 | ||
765bf9b7 LP |
1421 | static void pci_claim_device_resources(struct pci_dev *dev) |
1422 | { | |
1423 | int i; | |
1424 | ||
1425 | for (i = 0; i < PCI_BRIDGE_RESOURCES; i++) { | |
1426 | struct resource *r = &dev->resource[i]; | |
1427 | ||
1428 | if (!r->flags || r->parent) | |
1429 | continue; | |
1430 | ||
1431 | pci_claim_resource(dev, i); | |
1432 | } | |
1433 | } | |
1434 | ||
1435 | static void pci_claim_bridge_resources(struct pci_dev *dev) | |
1436 | { | |
1437 | int i; | |
1438 | ||
1439 | for (i = PCI_BRIDGE_RESOURCES; i < PCI_NUM_RESOURCES; i++) { | |
1440 | struct resource *r = &dev->resource[i]; | |
1441 | ||
1442 | if (!r->flags || r->parent) | |
1443 | continue; | |
1444 | ||
1445 | pci_claim_bridge_resource(dev, i); | |
1446 | } | |
1447 | } | |
1448 | ||
1449 | static void pci_bus_allocate_dev_resources(struct pci_bus *b) | |
1450 | { | |
1451 | struct pci_dev *dev; | |
1452 | struct pci_bus *child; | |
1453 | ||
1454 | list_for_each_entry(dev, &b->devices, bus_list) { | |
1455 | pci_claim_device_resources(dev); | |
1456 | ||
1457 | child = dev->subordinate; | |
1458 | if (child) | |
1459 | pci_bus_allocate_dev_resources(child); | |
1460 | } | |
1461 | } | |
1462 | ||
1463 | static void pci_bus_allocate_resources(struct pci_bus *b) | |
1464 | { | |
1465 | struct pci_bus *child; | |
1466 | ||
1467 | /* | |
0d607618 NJ |
1468 | * Carry out a depth-first search on the PCI bus tree to allocate |
1469 | * bridge apertures. Read the programmed bridge bases and | |
1470 | * recursively claim the respective bridge resources. | |
765bf9b7 LP |
1471 | */ |
1472 | if (b->self) { | |
1473 | pci_read_bridge_bases(b); | |
1474 | pci_claim_bridge_resources(b->self); | |
1475 | } | |
1476 | ||
1477 | list_for_each_entry(child, &b->children, node) | |
1478 | pci_bus_allocate_resources(child); | |
1479 | } | |
1480 | ||
1481 | void pci_bus_claim_resources(struct pci_bus *b) | |
1482 | { | |
1483 | pci_bus_allocate_resources(b); | |
1484 | pci_bus_allocate_dev_resources(b); | |
1485 | } | |
1486 | EXPORT_SYMBOL(pci_bus_claim_resources); | |
1487 | ||
10874f5a BH |
1488 | static void __pci_bridge_assign_resources(const struct pci_dev *bridge, |
1489 | struct list_head *add_head, | |
1490 | struct list_head *fail_head) | |
6841ec68 YL |
1491 | { |
1492 | struct pci_bus *b; | |
1493 | ||
8424d759 YL |
1494 | pdev_assign_resources_sorted((struct pci_dev *)bridge, |
1495 | add_head, fail_head); | |
6841ec68 YL |
1496 | |
1497 | b = bridge->subordinate; | |
1498 | if (!b) | |
1499 | return; | |
1500 | ||
8424d759 | 1501 | __pci_bus_assign_resources(b, add_head, fail_head); |
6841ec68 YL |
1502 | |
1503 | switch (bridge->class >> 8) { | |
1504 | case PCI_CLASS_BRIDGE_PCI: | |
1505 | pci_setup_bridge(b); | |
1506 | break; | |
1507 | ||
1508 | case PCI_CLASS_BRIDGE_CARDBUS: | |
1509 | pci_setup_cardbus(b); | |
1510 | break; | |
1511 | ||
1512 | default: | |
7506dc79 | 1513 | pci_info(bridge, "not setting up bridge for bus %04x:%02x\n", |
227f0647 | 1514 | pci_domain_nr(b), b->number); |
6841ec68 YL |
1515 | break; |
1516 | } | |
1517 | } | |
cb21bc94 CK |
1518 | |
1519 | #define PCI_RES_TYPE_MASK \ | |
1520 | (IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH |\ | |
1521 | IORESOURCE_MEM_64) | |
1522 | ||
5009b460 | 1523 | static void pci_bridge_release_resources(struct pci_bus *bus, |
0d607618 | 1524 | unsigned long type) |
5009b460 | 1525 | { |
5b285415 | 1526 | struct pci_dev *dev = bus->self; |
5009b460 | 1527 | struct resource *r; |
c50762a8 | 1528 | unsigned int old_flags; |
5b285415 YL |
1529 | struct resource *b_res; |
1530 | int idx = 1; | |
5009b460 | 1531 | |
5b285415 YL |
1532 | b_res = &dev->resource[PCI_BRIDGE_RESOURCES]; |
1533 | ||
1534 | /* | |
0d607618 NJ |
1535 | * 1. If IO port assignment fails, release bridge IO port. |
1536 | * 2. If non pref MMIO assignment fails, release bridge nonpref MMIO. | |
1537 | * 3. If 64bit pref MMIO assignment fails, and bridge pref is 64bit, | |
1538 | * release bridge pref MMIO. | |
1539 | * 4. If pref MMIO assignment fails, and bridge pref is 32bit, | |
1540 | * release bridge pref MMIO. | |
1541 | * 5. If pref MMIO assignment fails, and bridge pref is not | |
1542 | * assigned, release bridge nonpref MMIO. | |
5b285415 YL |
1543 | */ |
1544 | if (type & IORESOURCE_IO) | |
1545 | idx = 0; | |
1546 | else if (!(type & IORESOURCE_PREFETCH)) | |
1547 | idx = 1; | |
1548 | else if ((type & IORESOURCE_MEM_64) && | |
1549 | (b_res[2].flags & IORESOURCE_MEM_64)) | |
1550 | idx = 2; | |
1551 | else if (!(b_res[2].flags & IORESOURCE_MEM_64) && | |
1552 | (b_res[2].flags & IORESOURCE_PREFETCH)) | |
1553 | idx = 2; | |
1554 | else | |
1555 | idx = 1; | |
1556 | ||
1557 | r = &b_res[idx]; | |
1558 | ||
1559 | if (!r->parent) | |
1560 | return; | |
1561 | ||
0d607618 | 1562 | /* If there are children, release them all */ |
5b285415 YL |
1563 | release_child_resources(r); |
1564 | if (!release_resource(r)) { | |
cb21bc94 | 1565 | type = old_flags = r->flags & PCI_RES_TYPE_MASK; |
34c6b710 MK |
1566 | pci_info(dev, "resource %d %pR released\n", |
1567 | PCI_BRIDGE_RESOURCES + idx, r); | |
0d607618 | 1568 | /* Keep the old size */ |
5b285415 YL |
1569 | r->end = resource_size(r) - 1; |
1570 | r->start = 0; | |
1571 | r->flags = 0; | |
5009b460 | 1572 | |
0d607618 | 1573 | /* Avoiding touch the one without PREF */ |
5009b460 YL |
1574 | if (type & IORESOURCE_PREFETCH) |
1575 | type = IORESOURCE_PREFETCH; | |
1576 | __pci_setup_bridge(bus, type); | |
0d607618 | 1577 | /* For next child res under same bridge */ |
5b285415 | 1578 | r->flags = old_flags; |
5009b460 YL |
1579 | } |
1580 | } | |
1581 | ||
1582 | enum release_type { | |
1583 | leaf_only, | |
1584 | whole_subtree, | |
1585 | }; | |
0d607618 | 1586 | |
5009b460 | 1587 | /* |
0d607618 NJ |
1588 | * Try to release PCI bridge resources from leaf bridge, so we can allocate |
1589 | * a larger window later. | |
5009b460 | 1590 | */ |
10874f5a BH |
1591 | static void pci_bus_release_bridge_resources(struct pci_bus *bus, |
1592 | unsigned long type, | |
1593 | enum release_type rel_type) | |
5009b460 YL |
1594 | { |
1595 | struct pci_dev *dev; | |
1596 | bool is_leaf_bridge = true; | |
1597 | ||
1598 | list_for_each_entry(dev, &bus->devices, bus_list) { | |
1599 | struct pci_bus *b = dev->subordinate; | |
1600 | if (!b) | |
1601 | continue; | |
1602 | ||
1603 | is_leaf_bridge = false; | |
1604 | ||
1605 | if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI) | |
1606 | continue; | |
1607 | ||
1608 | if (rel_type == whole_subtree) | |
1609 | pci_bus_release_bridge_resources(b, type, | |
1610 | whole_subtree); | |
1611 | } | |
1612 | ||
1613 | if (pci_is_root_bus(bus)) | |
1614 | return; | |
1615 | ||
1616 | if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI) | |
1617 | return; | |
1618 | ||
1619 | if ((rel_type == whole_subtree) || is_leaf_bridge) | |
1620 | pci_bridge_release_resources(bus, type); | |
1621 | } | |
1622 | ||
76fbc263 YL |
1623 | static void pci_bus_dump_res(struct pci_bus *bus) |
1624 | { | |
89a74ecc BH |
1625 | struct resource *res; |
1626 | int i; | |
7c9342b8 | 1627 | |
89a74ecc | 1628 | pci_bus_for_each_resource(bus, res, i) { |
7c9342b8 | 1629 | if (!res || !res->end || !res->flags) |
3c78bc61 | 1630 | continue; |
76fbc263 | 1631 | |
34c6b710 | 1632 | dev_info(&bus->dev, "resource %d %pR\n", i, res); |
3c78bc61 | 1633 | } |
76fbc263 YL |
1634 | } |
1635 | ||
1636 | static void pci_bus_dump_resources(struct pci_bus *bus) | |
1637 | { | |
1638 | struct pci_bus *b; | |
1639 | struct pci_dev *dev; | |
1640 | ||
1641 | ||
1642 | pci_bus_dump_res(bus); | |
1643 | ||
1644 | list_for_each_entry(dev, &bus->devices, bus_list) { | |
1645 | b = dev->subordinate; | |
1646 | if (!b) | |
1647 | continue; | |
1648 | ||
1649 | pci_bus_dump_resources(b); | |
1650 | } | |
1651 | } | |
1652 | ||
ff35147c | 1653 | static int pci_bus_get_depth(struct pci_bus *bus) |
da7822e5 YL |
1654 | { |
1655 | int depth = 0; | |
f2a230bd | 1656 | struct pci_bus *child_bus; |
da7822e5 | 1657 | |
3c78bc61 | 1658 | list_for_each_entry(child_bus, &bus->children, node) { |
da7822e5 | 1659 | int ret; |
da7822e5 | 1660 | |
f2a230bd | 1661 | ret = pci_bus_get_depth(child_bus); |
da7822e5 YL |
1662 | if (ret + 1 > depth) |
1663 | depth = ret + 1; | |
1664 | } | |
1665 | ||
1666 | return depth; | |
1667 | } | |
da7822e5 | 1668 | |
b55438fd YL |
1669 | /* |
1670 | * -1: undefined, will auto detect later | |
1671 | * 0: disabled by user | |
1672 | * 1: disabled by auto detect | |
1673 | * 2: enabled by user | |
1674 | * 3: enabled by auto detect | |
1675 | */ | |
1676 | enum enable_type { | |
1677 | undefined = -1, | |
1678 | user_disabled, | |
1679 | auto_disabled, | |
1680 | user_enabled, | |
1681 | auto_enabled, | |
1682 | }; | |
1683 | ||
ff35147c | 1684 | static enum enable_type pci_realloc_enable = undefined; |
b55438fd YL |
1685 | void __init pci_realloc_get_opt(char *str) |
1686 | { | |
1687 | if (!strncmp(str, "off", 3)) | |
1688 | pci_realloc_enable = user_disabled; | |
1689 | else if (!strncmp(str, "on", 2)) | |
1690 | pci_realloc_enable = user_enabled; | |
1691 | } | |
ff35147c | 1692 | static bool pci_realloc_enabled(enum enable_type enable) |
b55438fd | 1693 | { |
967260cd | 1694 | return enable >= user_enabled; |
b55438fd | 1695 | } |
f483d392 | 1696 | |
b07f2ebc | 1697 | #if defined(CONFIG_PCI_IOV) && defined(CONFIG_PCI_REALLOC_ENABLE_AUTO) |
ff35147c | 1698 | static int iov_resources_unassigned(struct pci_dev *dev, void *data) |
223d96fc YL |
1699 | { |
1700 | int i; | |
1701 | bool *unassigned = data; | |
b07f2ebc | 1702 | |
39098edb DE |
1703 | for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { |
1704 | struct resource *r = &dev->resource[i + PCI_IOV_RESOURCES]; | |
fa216bf4 | 1705 | struct pci_bus_region region; |
b07f2ebc | 1706 | |
223d96fc | 1707 | /* Not assigned or rejected by kernel? */ |
fa216bf4 YL |
1708 | if (!r->flags) |
1709 | continue; | |
b07f2ebc | 1710 | |
fc279850 | 1711 | pcibios_resource_to_bus(dev->bus, ®ion, r); |
fa216bf4 | 1712 | if (!region.start) { |
223d96fc | 1713 | *unassigned = true; |
0d607618 | 1714 | return 1; /* Return early from pci_walk_bus() */ |
b07f2ebc YL |
1715 | } |
1716 | } | |
b07f2ebc | 1717 | |
223d96fc | 1718 | return 0; |
b07f2ebc YL |
1719 | } |
1720 | ||
ff35147c | 1721 | static enum enable_type pci_realloc_detect(struct pci_bus *bus, |
0d607618 | 1722 | enum enable_type enable_local) |
223d96fc YL |
1723 | { |
1724 | bool unassigned = false; | |
7ac0d094 | 1725 | struct pci_host_bridge *host; |
b07f2ebc | 1726 | |
967260cd YL |
1727 | if (enable_local != undefined) |
1728 | return enable_local; | |
223d96fc | 1729 | |
7ac0d094 BH |
1730 | host = pci_find_host_bridge(bus); |
1731 | if (host->preserve_config) | |
1732 | return auto_disabled; | |
1733 | ||
967260cd YL |
1734 | pci_walk_bus(bus, iov_resources_unassigned, &unassigned); |
1735 | if (unassigned) | |
1736 | return auto_enabled; | |
1737 | ||
1738 | return enable_local; | |
b07f2ebc | 1739 | } |
223d96fc | 1740 | #else |
ff35147c | 1741 | static enum enable_type pci_realloc_detect(struct pci_bus *bus, |
0d607618 | 1742 | enum enable_type enable_local) |
967260cd YL |
1743 | { |
1744 | return enable_local; | |
b07f2ebc | 1745 | } |
223d96fc | 1746 | #endif |
b07f2ebc | 1747 | |
1e58f4e1 | 1748 | static void adjust_bridge_window(struct pci_dev *bridge, struct resource *res, |
0d607618 | 1749 | struct list_head *add_list, |
3d264da9 | 1750 | resource_size_t new_size) |
1a576772 | 1751 | { |
94867573 | 1752 | resource_size_t add_size, size = resource_size(res); |
1a576772 MW |
1753 | |
1754 | if (res->parent) | |
1755 | return; | |
1756 | ||
94867573 | 1757 | if (!new_size) |
1a576772 MW |
1758 | return; |
1759 | ||
94867573 NJ |
1760 | if (new_size > size) { |
1761 | add_size = new_size - size; | |
1762 | pci_dbg(bridge, "bridge window %pR extended by %pa\n", res, | |
1763 | &add_size); | |
1764 | } else if (new_size < size) { | |
1765 | add_size = size - new_size; | |
1766 | pci_dbg(bridge, "bridge window %pR shrunken by %pa\n", res, | |
1767 | &add_size); | |
9db0b9b6 MW |
1768 | } else { |
1769 | return; | |
94867573 NJ |
1770 | } |
1771 | ||
ae4611f1 | 1772 | res->end = res->start + new_size - 1; |
5632e2be | 1773 | remove_from_list(add_list, res); |
1a576772 MW |
1774 | } |
1775 | ||
9db0b9b6 MW |
1776 | static void remove_dev_resource(struct resource *avail, struct pci_dev *dev, |
1777 | struct resource *res) | |
1778 | { | |
1779 | resource_size_t size, align, tmp; | |
1780 | ||
1781 | size = resource_size(res); | |
1782 | if (!size) | |
1783 | return; | |
1784 | ||
1785 | align = pci_resource_alignment(dev, res); | |
1786 | align = align ? ALIGN(avail->start, align) - avail->start : 0; | |
1787 | tmp = align + size; | |
1788 | avail->start = min(avail->start + tmp, avail->end + 1); | |
1789 | } | |
1790 | ||
1791 | static void remove_dev_resources(struct pci_dev *dev, struct resource *io, | |
1792 | struct resource *mmio, | |
1793 | struct resource *mmio_pref) | |
1794 | { | |
1795 | int i; | |
1796 | ||
1797 | for (i = 0; i < PCI_NUM_RESOURCES; i++) { | |
1798 | struct resource *res = &dev->resource[i]; | |
1799 | ||
1800 | if (resource_type(res) == IORESOURCE_IO) { | |
1801 | remove_dev_resource(io, dev, res); | |
1802 | } else if (resource_type(res) == IORESOURCE_MEM) { | |
1803 | ||
1804 | /* | |
1805 | * Make sure prefetchable memory is reduced from | |
1806 | * the correct resource. Specifically we put 32-bit | |
1807 | * prefetchable memory in non-prefetchable window | |
1808 | * if there is an 64-bit pretchable window. | |
1809 | * | |
1810 | * See comments in __pci_bus_size_bridges() for | |
1811 | * more information. | |
1812 | */ | |
1813 | if ((res->flags & IORESOURCE_PREFETCH) && | |
1814 | ((res->flags & IORESOURCE_MEM_64) == | |
1815 | (mmio_pref->flags & IORESOURCE_MEM_64))) | |
1816 | remove_dev_resource(mmio_pref, dev, res); | |
1817 | else | |
1818 | remove_dev_resource(mmio, dev, res); | |
1819 | } | |
1820 | } | |
1821 | } | |
1822 | ||
1823 | /* | |
1824 | * io, mmio and mmio_pref contain the total amount of bridge window space | |
1825 | * available. This includes the minimal space needed to cover all the | |
1826 | * existing devices on the bus and the possible extra space that can be | |
1827 | * shared with the bridges. | |
1828 | */ | |
1a576772 | 1829 | static void pci_bus_distribute_available_resources(struct pci_bus *bus, |
0d607618 | 1830 | struct list_head *add_list, |
d555a50f NJ |
1831 | struct resource io, |
1832 | struct resource mmio, | |
1833 | struct resource mmio_pref) | |
1a576772 | 1834 | { |
1a576772 MW |
1835 | unsigned int normal_bridges = 0, hotplug_bridges = 0; |
1836 | struct resource *io_res, *mmio_res, *mmio_pref_res; | |
1837 | struct pci_dev *dev, *bridge = bus->self; | |
9db0b9b6 | 1838 | resource_size_t io_per_b, mmio_per_b, mmio_pref_per_b, align; |
1a576772 | 1839 | |
6e0688db KW |
1840 | io_res = &bridge->resource[PCI_BRIDGE_IO_WINDOW]; |
1841 | mmio_res = &bridge->resource[PCI_BRIDGE_MEM_WINDOW]; | |
1842 | mmio_pref_res = &bridge->resource[PCI_BRIDGE_PREF_MEM_WINDOW]; | |
1a576772 | 1843 | |
f924c26e NJ |
1844 | /* |
1845 | * The alignment of this bridge is yet to be considered, hence it must | |
1846 | * be done now before extending its bridge window. | |
1847 | */ | |
1848 | align = pci_resource_alignment(bridge, io_res); | |
1849 | if (!io_res->parent && align) | |
1850 | io.start = min(ALIGN(io.start, align), io.end + 1); | |
1851 | ||
1852 | align = pci_resource_alignment(bridge, mmio_res); | |
1853 | if (!mmio_res->parent && align) | |
1854 | mmio.start = min(ALIGN(mmio.start, align), mmio.end + 1); | |
1855 | ||
1856 | align = pci_resource_alignment(bridge, mmio_pref_res); | |
1857 | if (!mmio_pref_res->parent && align) | |
1858 | mmio_pref.start = min(ALIGN(mmio_pref.start, align), | |
1859 | mmio_pref.end + 1); | |
1860 | ||
1a576772 | 1861 | /* |
ae4611f1 NJ |
1862 | * Now that we have adjusted for alignment, update the bridge window |
1863 | * resources to fill as much remaining resource space as possible. | |
1a576772 | 1864 | */ |
1e58f4e1 NJ |
1865 | adjust_bridge_window(bridge, io_res, add_list, resource_size(&io)); |
1866 | adjust_bridge_window(bridge, mmio_res, add_list, resource_size(&mmio)); | |
1867 | adjust_bridge_window(bridge, mmio_pref_res, add_list, | |
77793854 | 1868 | resource_size(&mmio_pref)); |
1a576772 | 1869 | |
1a576772 MW |
1870 | /* |
1871 | * Calculate how many hotplug bridges and normal bridges there | |
0d607618 | 1872 | * are on this bus. We will distribute the additional available |
1a576772 MW |
1873 | * resources between hotplug bridges. |
1874 | */ | |
1875 | for_each_pci_bridge(dev, bus) { | |
1876 | if (dev->is_hotplug_bridge) | |
1877 | hotplug_bridges++; | |
1878 | else | |
1879 | normal_bridges++; | |
1880 | } | |
1881 | ||
9db0b9b6 | 1882 | if (!(hotplug_bridges + normal_bridges)) |
6a381ea6 NJ |
1883 | return; |
1884 | ||
5c6bcc34 | 1885 | /* |
9db0b9b6 MW |
1886 | * Calculate the amount of space we can forward from "bus" to any |
1887 | * downstream buses, i.e., the space left over after assigning the | |
1888 | * BARs and windows on "bus". | |
5c6bcc34 | 1889 | */ |
9db0b9b6 MW |
1890 | list_for_each_entry(dev, &bus->devices, bus_list) { |
1891 | if (!dev->is_virtfn) | |
1892 | remove_dev_resources(dev, &io, &mmio, &mmio_pref); | |
1a576772 MW |
1893 | } |
1894 | ||
1895 | /* | |
9db0b9b6 MW |
1896 | * If there is at least one hotplug bridge on this bus it gets all |
1897 | * the extra resource space that was left after the reductions | |
1898 | * above. | |
1899 | * | |
1900 | * If there are no hotplug bridges the extra resource space is | |
1901 | * split between non-hotplug bridges. This is to allow possible | |
1902 | * hotplug bridges below them to get the extra space as well. | |
1a576772 | 1903 | */ |
9db0b9b6 MW |
1904 | if (hotplug_bridges) { |
1905 | io_per_b = div64_ul(resource_size(&io), hotplug_bridges); | |
1906 | mmio_per_b = div64_ul(resource_size(&mmio), hotplug_bridges); | |
1907 | mmio_pref_per_b = div64_ul(resource_size(&mmio_pref), | |
1908 | hotplug_bridges); | |
1909 | } else { | |
1910 | io_per_b = div64_ul(resource_size(&io), normal_bridges); | |
1911 | mmio_per_b = div64_ul(resource_size(&mmio), normal_bridges); | |
1912 | mmio_pref_per_b = div64_ul(resource_size(&mmio_pref), | |
1913 | normal_bridges); | |
1914 | } | |
1915 | ||
1a576772 | 1916 | for_each_pci_bridge(dev, bus) { |
08f0a15e | 1917 | struct resource *res; |
1a576772 MW |
1918 | struct pci_bus *b; |
1919 | ||
1920 | b = dev->subordinate; | |
9db0b9b6 | 1921 | if (!b) |
1a576772 | 1922 | continue; |
9db0b9b6 MW |
1923 | if (hotplug_bridges && !dev->is_hotplug_bridge) |
1924 | continue; | |
1925 | ||
1926 | res = &dev->resource[PCI_BRIDGE_IO_WINDOW]; | |
1a576772 | 1927 | |
14fe5951 | 1928 | /* |
9db0b9b6 MW |
1929 | * Make sure the split resource space is properly aligned |
1930 | * for bridge windows (align it down to avoid going above | |
1931 | * what is available). | |
14fe5951 | 1932 | */ |
08f0a15e | 1933 | align = pci_resource_alignment(dev, res); |
9db0b9b6 MW |
1934 | io.end = align ? io.start + ALIGN_DOWN(io_per_b, align) - 1 |
1935 | : io.start + io_per_b - 1; | |
1936 | ||
1937 | /* | |
1938 | * The x_per_b holds the extra resource space that can be | |
1939 | * added for each bridge but there is the minimal already | |
1940 | * reserved as well so adjust x.start down accordingly to | |
1941 | * cover the whole space. | |
1942 | */ | |
1943 | io.start -= resource_size(res); | |
08f0a15e MW |
1944 | |
1945 | res = &dev->resource[PCI_BRIDGE_MEM_WINDOW]; | |
1946 | align = pci_resource_alignment(dev, res); | |
9db0b9b6 MW |
1947 | mmio.end = align ? mmio.start + ALIGN_DOWN(mmio_per_b, align) - 1 |
1948 | : mmio.start + mmio_per_b - 1; | |
1949 | mmio.start -= resource_size(res); | |
08f0a15e MW |
1950 | |
1951 | res = &dev->resource[PCI_BRIDGE_PREF_MEM_WINDOW]; | |
1952 | align = pci_resource_alignment(dev, res); | |
1953 | mmio_pref.end = align ? mmio_pref.start + | |
9db0b9b6 MW |
1954 | ALIGN_DOWN(mmio_pref_per_b, align) - 1 |
1955 | : mmio_pref.start + mmio_pref_per_b - 1; | |
1956 | mmio_pref.start -= resource_size(res); | |
d555a50f NJ |
1957 | |
1958 | pci_bus_distribute_available_resources(b, add_list, io, mmio, | |
1959 | mmio_pref); | |
f924c26e | 1960 | |
08f0a15e MW |
1961 | io.start += io.end + 1; |
1962 | mmio.start += mmio.end + 1; | |
1963 | mmio_pref.start += mmio_pref.end + 1; | |
1a576772 MW |
1964 | } |
1965 | } | |
1966 | ||
0d607618 | 1967 | static void pci_bridge_distribute_available_resources(struct pci_dev *bridge, |
17d2d67d | 1968 | struct list_head *add_list) |
1a576772 | 1969 | { |
d555a50f | 1970 | struct resource available_io, available_mmio, available_mmio_pref; |
1a576772 MW |
1971 | |
1972 | if (!bridge->is_hotplug_bridge) | |
1973 | return; | |
1974 | ||
1975 | /* Take the initial extra resources from the hotplug port */ | |
6e0688db KW |
1976 | available_io = bridge->resource[PCI_BRIDGE_IO_WINDOW]; |
1977 | available_mmio = bridge->resource[PCI_BRIDGE_MEM_WINDOW]; | |
1978 | available_mmio_pref = bridge->resource[PCI_BRIDGE_PREF_MEM_WINDOW]; | |
1a576772 MW |
1979 | |
1980 | pci_bus_distribute_available_resources(bridge->subordinate, | |
0d607618 NJ |
1981 | add_list, available_io, |
1982 | available_mmio, | |
1983 | available_mmio_pref); | |
1a576772 MW |
1984 | } |
1985 | ||
d1caf229 MW |
1986 | /* |
1987 | * First try will not touch PCI bridge res. | |
1988 | * Second and later try will clear small leaf bridge res. | |
1989 | * Will stop till to the max depth if can not find good one. | |
1990 | */ | |
1991 | void pci_assign_unassigned_root_bus_resources(struct pci_bus *bus) | |
1992 | { | |
1993 | LIST_HEAD(realloc_head); | |
1994 | /* List of resources that want additional resources */ | |
1995 | struct list_head *add_list = NULL; | |
1996 | int tried_times = 0; | |
1997 | enum release_type rel_type = leaf_only; | |
1998 | LIST_HEAD(fail_head); | |
1999 | struct pci_dev_resource *fail_res; | |
2000 | int pci_try_num = 1; | |
2001 | enum enable_type enable_local; | |
2002 | ||
2003 | /* Don't realloc if asked to do so */ | |
2004 | enable_local = pci_realloc_detect(bus, pci_realloc_enable); | |
2005 | if (pci_realloc_enabled(enable_local)) { | |
2006 | int max_depth = pci_bus_get_depth(bus); | |
2007 | ||
2008 | pci_try_num = max_depth + 1; | |
2009 | dev_info(&bus->dev, "max bus depth: %d pci_try_num: %d\n", | |
2010 | max_depth, pci_try_num); | |
2011 | } | |
2012 | ||
2013 | again: | |
2014 | /* | |
2015 | * Last try will use add_list, otherwise will try good to have as must | |
2016 | * have, so can realloc parent bridge resource | |
2017 | */ | |
2018 | if (tried_times + 1 == pci_try_num) | |
2019 | add_list = &realloc_head; | |
2020 | /* | |
2021 | * Depth first, calculate sizes and alignments of all subordinate buses. | |
2022 | */ | |
2023 | __pci_bus_size_bridges(bus, add_list); | |
2024 | ||
2025 | /* Depth last, allocate resources and update the hardware. */ | |
2026 | __pci_bus_assign_resources(bus, add_list, &fail_head); | |
2027 | if (add_list) | |
2028 | BUG_ON(!list_empty(add_list)); | |
2029 | tried_times++; | |
2030 | ||
2031 | /* Any device complain? */ | |
2032 | if (list_empty(&fail_head)) | |
2033 | goto dump; | |
2034 | ||
2035 | if (tried_times >= pci_try_num) { | |
2036 | if (enable_local == undefined) | |
2037 | dev_info(&bus->dev, "Some PCI device resources are unassigned, try booting with pci=realloc\n"); | |
2038 | else if (enable_local == auto_enabled) | |
2039 | dev_info(&bus->dev, "Automatically enabled pci realloc, if you have problem, try booting with pci=realloc=off\n"); | |
2040 | ||
2041 | free_list(&fail_head); | |
2042 | goto dump; | |
2043 | } | |
2044 | ||
2045 | dev_info(&bus->dev, "No. %d try to assign unassigned res\n", | |
2046 | tried_times + 1); | |
2047 | ||
2048 | /* Third times and later will not check if it is leaf */ | |
2049 | if ((tried_times + 1) > 2) | |
2050 | rel_type = whole_subtree; | |
2051 | ||
2052 | /* | |
2053 | * Try to release leaf bridge's resources that doesn't fit resource of | |
2054 | * child device under that bridge. | |
2055 | */ | |
2056 | list_for_each_entry(fail_res, &fail_head, list) | |
2057 | pci_bus_release_bridge_resources(fail_res->dev->bus, | |
2058 | fail_res->flags & PCI_RES_TYPE_MASK, | |
2059 | rel_type); | |
2060 | ||
2061 | /* Restore size and flags */ | |
2062 | list_for_each_entry(fail_res, &fail_head, list) { | |
2063 | struct resource *res = fail_res->res; | |
2064 | int idx; | |
2065 | ||
2066 | res->start = fail_res->start; | |
2067 | res->end = fail_res->end; | |
2068 | res->flags = fail_res->flags; | |
2069 | ||
2070 | if (pci_is_bridge(fail_res->dev)) { | |
2071 | idx = res - &fail_res->dev->resource[0]; | |
2072 | if (idx >= PCI_BRIDGE_RESOURCES && | |
2073 | idx <= PCI_BRIDGE_RESOURCE_END) | |
2074 | res->flags = 0; | |
2075 | } | |
2076 | } | |
2077 | free_list(&fail_head); | |
2078 | ||
2079 | goto again; | |
2080 | ||
2081 | dump: | |
2082 | /* Dump the resource on buses */ | |
2083 | pci_bus_dump_resources(bus); | |
2084 | } | |
2085 | ||
2086 | void __init pci_assign_unassigned_resources(void) | |
2087 | { | |
2088 | struct pci_bus *root_bus; | |
2089 | ||
2090 | list_for_each_entry(root_bus, &pci_root_buses, node) { | |
2091 | pci_assign_unassigned_root_bus_resources(root_bus); | |
2092 | ||
2093 | /* Make sure the root bridge has a companion ACPI device */ | |
2094 | if (ACPI_HANDLE(root_bus->bridge)) | |
2095 | acpi_ioapic_add(ACPI_HANDLE(root_bus->bridge)); | |
2096 | } | |
2097 | } | |
2098 | ||
6841ec68 YL |
2099 | void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge) |
2100 | { | |
2101 | struct pci_bus *parent = bridge->subordinate; | |
0d607618 NJ |
2102 | /* List of resources that want additional resources */ |
2103 | LIST_HEAD(add_list); | |
2104 | ||
32180e40 | 2105 | int tried_times = 0; |
bdc4abec | 2106 | LIST_HEAD(fail_head); |
b9b0bba9 | 2107 | struct pci_dev_resource *fail_res; |
6841ec68 | 2108 | int retval; |
32180e40 | 2109 | |
32180e40 | 2110 | again: |
8424d759 | 2111 | __pci_bus_size_bridges(parent, &add_list); |
1a576772 MW |
2112 | |
2113 | /* | |
0d607618 NJ |
2114 | * Distribute remaining resources (if any) equally between hotplug |
2115 | * bridges below. This makes it possible to extend the hierarchy | |
2116 | * later without running out of resources. | |
1a576772 MW |
2117 | */ |
2118 | pci_bridge_distribute_available_resources(bridge, &add_list); | |
2119 | ||
bdc4abec YL |
2120 | __pci_bridge_assign_resources(bridge, &add_list, &fail_head); |
2121 | BUG_ON(!list_empty(&add_list)); | |
32180e40 YL |
2122 | tried_times++; |
2123 | ||
bdc4abec | 2124 | if (list_empty(&fail_head)) |
3f579c34 | 2125 | goto enable_all; |
32180e40 YL |
2126 | |
2127 | if (tried_times >= 2) { | |
0d607618 | 2128 | /* Still fail, don't need to try more */ |
bffc56d4 | 2129 | free_list(&fail_head); |
3f579c34 | 2130 | goto enable_all; |
32180e40 YL |
2131 | } |
2132 | ||
2133 | printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n", | |
2134 | tried_times + 1); | |
2135 | ||
2136 | /* | |
0d607618 NJ |
2137 | * Try to release leaf bridge's resources that aren't big enough |
2138 | * to contain child device resources. | |
32180e40 | 2139 | */ |
61e83cdd YL |
2140 | list_for_each_entry(fail_res, &fail_head, list) |
2141 | pci_bus_release_bridge_resources(fail_res->dev->bus, | |
cb21bc94 | 2142 | fail_res->flags & PCI_RES_TYPE_MASK, |
32180e40 | 2143 | whole_subtree); |
61e83cdd | 2144 | |
0d607618 | 2145 | /* Restore size and flags */ |
b9b0bba9 YL |
2146 | list_for_each_entry(fail_res, &fail_head, list) { |
2147 | struct resource *res = fail_res->res; | |
9db8dc6d | 2148 | int idx; |
32180e40 | 2149 | |
b9b0bba9 YL |
2150 | res->start = fail_res->start; |
2151 | res->end = fail_res->end; | |
2152 | res->flags = fail_res->flags; | |
9db8dc6d LG |
2153 | |
2154 | if (pci_is_bridge(fail_res->dev)) { | |
2155 | idx = res - &fail_res->dev->resource[0]; | |
2156 | if (idx >= PCI_BRIDGE_RESOURCES && | |
2157 | idx <= PCI_BRIDGE_RESOURCE_END) | |
2158 | res->flags = 0; | |
2159 | } | |
32180e40 | 2160 | } |
bffc56d4 | 2161 | free_list(&fail_head); |
32180e40 YL |
2162 | |
2163 | goto again; | |
3f579c34 YL |
2164 | |
2165 | enable_all: | |
2166 | retval = pci_reenable_device(bridge); | |
9fc9eea0 | 2167 | if (retval) |
7506dc79 | 2168 | pci_err(bridge, "Error reenabling bridge (%d)\n", retval); |
3f579c34 | 2169 | pci_set_master(bridge); |
6841ec68 YL |
2170 | } |
2171 | EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources); | |
9b03088f | 2172 | |
8bb705e3 CK |
2173 | int pci_reassign_bridge_resources(struct pci_dev *bridge, unsigned long type) |
2174 | { | |
2175 | struct pci_dev_resource *dev_res; | |
2176 | struct pci_dev *next; | |
2177 | LIST_HEAD(saved); | |
2178 | LIST_HEAD(added); | |
2179 | LIST_HEAD(failed); | |
2180 | unsigned int i; | |
2181 | int ret; | |
2182 | ||
fb794a70 BH |
2183 | down_read(&pci_bus_sem); |
2184 | ||
8bb705e3 CK |
2185 | /* Walk to the root hub, releasing bridge BARs when possible */ |
2186 | next = bridge; | |
2187 | do { | |
2188 | bridge = next; | |
2189 | for (i = PCI_BRIDGE_RESOURCES; i < PCI_BRIDGE_RESOURCE_END; | |
2190 | i++) { | |
2191 | struct resource *res = &bridge->resource[i]; | |
2192 | ||
2193 | if ((res->flags ^ type) & PCI_RES_TYPE_MASK) | |
2194 | continue; | |
2195 | ||
2196 | /* Ignore BARs which are still in use */ | |
2197 | if (res->child) | |
2198 | continue; | |
2199 | ||
2200 | ret = add_to_list(&saved, bridge, res, 0, 0); | |
2201 | if (ret) | |
2202 | goto cleanup; | |
2203 | ||
7506dc79 | 2204 | pci_info(bridge, "BAR %d: releasing %pR\n", |
8bb705e3 CK |
2205 | i, res); |
2206 | ||
2207 | if (res->parent) | |
2208 | release_resource(res); | |
2209 | res->start = 0; | |
2210 | res->end = 0; | |
2211 | break; | |
2212 | } | |
2213 | if (i == PCI_BRIDGE_RESOURCE_END) | |
2214 | break; | |
2215 | ||
2216 | next = bridge->bus ? bridge->bus->self : NULL; | |
2217 | } while (next); | |
2218 | ||
fb794a70 BH |
2219 | if (list_empty(&saved)) { |
2220 | up_read(&pci_bus_sem); | |
8bb705e3 | 2221 | return -ENOENT; |
fb794a70 | 2222 | } |
8bb705e3 CK |
2223 | |
2224 | __pci_bus_size_bridges(bridge->subordinate, &added); | |
2225 | __pci_bridge_assign_resources(bridge, &added, &failed); | |
2226 | BUG_ON(!list_empty(&added)); | |
2227 | ||
2228 | if (!list_empty(&failed)) { | |
2229 | ret = -ENOSPC; | |
2230 | goto cleanup; | |
2231 | } | |
2232 | ||
2233 | list_for_each_entry(dev_res, &saved, list) { | |
0d607618 | 2234 | /* Skip the bridge we just assigned resources for */ |
8bb705e3 CK |
2235 | if (bridge == dev_res->dev) |
2236 | continue; | |
2237 | ||
2238 | bridge = dev_res->dev; | |
2239 | pci_setup_bridge(bridge->subordinate); | |
2240 | } | |
2241 | ||
2242 | free_list(&saved); | |
fb794a70 | 2243 | up_read(&pci_bus_sem); |
8bb705e3 CK |
2244 | return 0; |
2245 | ||
2246 | cleanup: | |
0d607618 | 2247 | /* Restore size and flags */ |
8bb705e3 CK |
2248 | list_for_each_entry(dev_res, &failed, list) { |
2249 | struct resource *res = dev_res->res; | |
2250 | ||
2251 | res->start = dev_res->start; | |
2252 | res->end = dev_res->end; | |
2253 | res->flags = dev_res->flags; | |
2254 | } | |
2255 | free_list(&failed); | |
2256 | ||
2257 | /* Revert to the old configuration */ | |
2258 | list_for_each_entry(dev_res, &saved, list) { | |
2259 | struct resource *res = dev_res->res; | |
2260 | ||
2261 | bridge = dev_res->dev; | |
2262 | i = res - bridge->resource; | |
2263 | ||
2264 | res->start = dev_res->start; | |
2265 | res->end = dev_res->end; | |
2266 | res->flags = dev_res->flags; | |
2267 | ||
2268 | pci_claim_resource(bridge, i); | |
2269 | pci_setup_bridge(bridge->subordinate); | |
2270 | } | |
2271 | free_list(&saved); | |
fb794a70 | 2272 | up_read(&pci_bus_sem); |
8bb705e3 CK |
2273 | |
2274 | return ret; | |
2275 | } | |
2276 | ||
17787940 | 2277 | void pci_assign_unassigned_bus_resources(struct pci_bus *bus) |
9b03088f | 2278 | { |
9b03088f | 2279 | struct pci_dev *dev; |
0d607618 NJ |
2280 | /* List of resources that want additional resources */ |
2281 | LIST_HEAD(add_list); | |
9b03088f | 2282 | |
9b03088f | 2283 | down_read(&pci_bus_sem); |
24a0c654 AS |
2284 | for_each_pci_bridge(dev, bus) |
2285 | if (pci_has_subordinate(dev)) | |
2286 | __pci_bus_size_bridges(dev->subordinate, &add_list); | |
9b03088f YL |
2287 | up_read(&pci_bus_sem); |
2288 | __pci_bus_assign_resources(bus, &add_list, NULL); | |
bdc4abec | 2289 | BUG_ON(!list_empty(&add_list)); |
17787940 | 2290 | } |
e6b29dea | 2291 | EXPORT_SYMBOL_GPL(pci_assign_unassigned_bus_resources); |