blob: ae0b1f2c9e0a29bb3f98c04a8ed1d63190ad915f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/pci/setup-bus.c
3 *
4 * Extruded from code written by
5 * Dave Rusling (david.rusling@reo.mts.dec.com)
6 * David Mosberger (davidm@cs.arizona.edu)
7 * David Miller (davem@redhat.com)
8 *
9 * Support routines for initializing a PCI subsystem.
10 */
11
12/*
13 * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
14 * PCI-PCI bridges cleanup, sorted resource allocation.
15 * Feb 2002, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
16 * Converted to allocation in 3 passes, which gives
17 * tighter packing. Prefetchable range support.
18 */
19
20#include <linux/init.h>
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/pci.h>
24#include <linux/errno.h>
25#include <linux/ioport.h>
26#include <linux/cache.h>
27#include <linux/slab.h>
Chris Wright6faf17f2009-08-28 13:00:06 -070028#include "pci.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Yinghai Lubdc4abe2012-01-21 02:08:27 -080030struct pci_dev_resource {
31 struct list_head list;
Yinghai Lu2934a0d2012-01-21 02:08:26 -080032 struct resource *res;
33 struct pci_dev *dev;
Yinghai Lu568ddef2010-01-22 01:02:21 -080034 resource_size_t start;
35 resource_size_t end;
Ram Paic8adf9a2011-02-14 17:43:20 -080036 resource_size_t add_size;
Ram Pai2bbc6942011-07-25 13:08:39 -070037 resource_size_t min_align;
Yinghai Lu568ddef2010-01-22 01:02:21 -080038 unsigned long flags;
39};
40
Yinghai Lubffc56d2012-01-21 02:08:30 -080041static void free_list(struct list_head *head)
42{
43 struct pci_dev_resource *dev_res, *tmp;
44
45 list_for_each_entry_safe(dev_res, tmp, head, list) {
46 list_del(&dev_res->list);
47 kfree(dev_res);
48 }
49}
Ram Pai094732a2011-02-14 17:43:18 -080050
Ram Paif483d392011-07-07 11:19:10 -070051int pci_realloc_enable = 0;
52#define pci_realloc_enabled() pci_realloc_enable
53void pci_realloc(void)
54{
55 pci_realloc_enable = 1;
56}
57
Ram Paic8adf9a2011-02-14 17:43:20 -080058/**
59 * add_to_list() - add a new resource tracker to the list
60 * @head: Head of the list
61 * @dev: device corresponding to which the resource
62 * belongs
63 * @res: The resource to be tracked
64 * @add_size: additional size to be optionally added
65 * to the resource
66 */
Yinghai Lubdc4abe2012-01-21 02:08:27 -080067static int add_to_list(struct list_head *head,
Ram Paic8adf9a2011-02-14 17:43:20 -080068 struct pci_dev *dev, struct resource *res,
Ram Pai2bbc6942011-07-25 13:08:39 -070069 resource_size_t add_size, resource_size_t min_align)
Yinghai Lu568ddef2010-01-22 01:02:21 -080070{
Yinghai Lu764242a2012-01-21 02:08:28 -080071 struct pci_dev_resource *tmp;
Yinghai Lu568ddef2010-01-22 01:02:21 -080072
Yinghai Lubdc4abe2012-01-21 02:08:27 -080073 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
Yinghai Lu568ddef2010-01-22 01:02:21 -080074 if (!tmp) {
Ram Paic8adf9a2011-02-14 17:43:20 -080075 pr_warning("add_to_list: kmalloc() failed!\n");
Yinghai Luef62dfe2012-01-21 02:08:18 -080076 return -ENOMEM;
Yinghai Lu568ddef2010-01-22 01:02:21 -080077 }
78
Yinghai Lu568ddef2010-01-22 01:02:21 -080079 tmp->res = res;
80 tmp->dev = dev;
81 tmp->start = res->start;
82 tmp->end = res->end;
83 tmp->flags = res->flags;
Ram Paic8adf9a2011-02-14 17:43:20 -080084 tmp->add_size = add_size;
Ram Pai2bbc6942011-07-25 13:08:39 -070085 tmp->min_align = min_align;
Yinghai Lubdc4abe2012-01-21 02:08:27 -080086
87 list_add(&tmp->list, head);
Yinghai Luef62dfe2012-01-21 02:08:18 -080088
89 return 0;
Yinghai Lu568ddef2010-01-22 01:02:21 -080090}
91
Yinghai Lubdc4abe2012-01-21 02:08:27 -080092static void add_to_failed_list(struct list_head *head,
Ram Paic8adf9a2011-02-14 17:43:20 -080093 struct pci_dev *dev, struct resource *res)
94{
Ram Pai2bbc6942011-07-25 13:08:39 -070095 add_to_list(head, dev, res,
96 0 /* dont care */,
97 0 /* dont care */);
Ram Paic8adf9a2011-02-14 17:43:20 -080098}
99
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800100static void remove_from_list(struct list_head *head,
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800101 struct resource *res)
102{
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800103 struct pci_dev_resource *dev_res, *tmp;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800104
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800105 list_for_each_entry_safe(dev_res, tmp, head, list) {
106 if (dev_res->res == res) {
107 list_del(&dev_res->list);
108 kfree(dev_res);
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800109 break;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800110 }
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800111 }
112}
113
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800114static resource_size_t get_res_add_size(struct list_head *head,
Yinghai Lu1c372352012-01-21 02:08:19 -0800115 struct resource *res)
116{
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800117 struct pci_dev_resource *dev_res;
Yinghai Lu1c372352012-01-21 02:08:19 -0800118
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800119 list_for_each_entry(dev_res, head, list) {
120 if (dev_res->res == res) {
Yinghai Lub5924432012-01-21 02:08:31 -0800121 int idx = res - &dev_res->dev->resource[0];
122
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800123 dev_printk(KERN_DEBUG, &dev_res->dev->dev,
Yinghai Lub5924432012-01-21 02:08:31 -0800124 "res[%d]=%pR get_res_add_size add_size %llx\n",
125 idx, dev_res->res,
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800126 (unsigned long long)dev_res->add_size);
Yinghai Lub5924432012-01-21 02:08:31 -0800127
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800128 return dev_res->add_size;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800129 }
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800130 }
Yinghai Lu1c372352012-01-21 02:08:19 -0800131
132 return 0;
133}
134
Yinghai Lu78c3b322012-01-21 02:08:25 -0800135/* Sort resources by alignment */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800136static void pdev_sort_resources(struct pci_dev *dev, struct list_head *head)
Yinghai Lu78c3b322012-01-21 02:08:25 -0800137{
138 int i;
139
140 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
141 struct resource *r;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800142 struct pci_dev_resource *dev_res, *tmp;
Yinghai Lu78c3b322012-01-21 02:08:25 -0800143 resource_size_t r_align;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800144 struct list_head *n;
Yinghai Lu78c3b322012-01-21 02:08:25 -0800145
146 r = &dev->resource[i];
147
148 if (r->flags & IORESOURCE_PCI_FIXED)
149 continue;
150
151 if (!(r->flags) || r->parent)
152 continue;
153
154 r_align = pci_resource_alignment(dev, r);
155 if (!r_align) {
156 dev_warn(&dev->dev, "BAR %d: %pR has bogus alignment\n",
157 i, r);
158 continue;
159 }
Yinghai Lu78c3b322012-01-21 02:08:25 -0800160
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800161 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
162 if (!tmp)
163 panic("pdev_sort_resources(): "
164 "kmalloc() failed!\n");
165 tmp->res = r;
166 tmp->dev = dev;
167
168 /* fallback is smallest one or list is empty*/
169 n = head;
170 list_for_each_entry(dev_res, head, list) {
171 resource_size_t align;
172
173 align = pci_resource_alignment(dev_res->dev,
174 dev_res->res);
Yinghai Lu78c3b322012-01-21 02:08:25 -0800175
176 if (r_align > align) {
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800177 n = &dev_res->list;
Yinghai Lu78c3b322012-01-21 02:08:25 -0800178 break;
179 }
180 }
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800181 /* Insert it just before n*/
182 list_add_tail(&tmp->list, n);
Yinghai Lu78c3b322012-01-21 02:08:25 -0800183 }
184}
185
Yinghai Lu6841ec62010-01-22 01:02:25 -0800186static void __dev_sort_resources(struct pci_dev *dev,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800187 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Yinghai Lu6841ec62010-01-22 01:02:25 -0800189 u16 class = dev->class >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Yinghai Lu6841ec62010-01-22 01:02:25 -0800191 /* Don't touch classless devices or host bridges or ioapics. */
192 if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST)
193 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Yinghai Lu6841ec62010-01-22 01:02:25 -0800195 /* Don't touch ioapic devices already enabled by firmware */
196 if (class == PCI_CLASS_SYSTEM_PIC) {
197 u16 command;
198 pci_read_config_word(dev, PCI_COMMAND, &command);
199 if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
200 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
202
Yinghai Lu6841ec62010-01-22 01:02:25 -0800203 pdev_sort_resources(dev, head);
204}
205
Ram Paifc075e12011-02-14 17:43:19 -0800206static inline void reset_resource(struct resource *res)
207{
208 res->start = 0;
209 res->end = 0;
210 res->flags = 0;
211}
212
Ram Paic8adf9a2011-02-14 17:43:20 -0800213/**
Ram Pai9e8bf932011-07-25 13:08:42 -0700214 * reassign_resources_sorted() - satisfy any additional resource requests
Ram Paic8adf9a2011-02-14 17:43:20 -0800215 *
Ram Pai9e8bf932011-07-25 13:08:42 -0700216 * @realloc_head : head of the list tracking requests requiring additional
Ram Paic8adf9a2011-02-14 17:43:20 -0800217 * resources
218 * @head : head of the list tracking requests with allocated
219 * resources
220 *
Ram Pai9e8bf932011-07-25 13:08:42 -0700221 * Walk through each element of the realloc_head and try to procure
Ram Paic8adf9a2011-02-14 17:43:20 -0800222 * additional resources for the element, provided the element
223 * is in the head list.
224 */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800225static void reassign_resources_sorted(struct list_head *realloc_head,
226 struct list_head *head)
Ram Paic8adf9a2011-02-14 17:43:20 -0800227{
228 struct resource *res;
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800229 struct pci_dev_resource *add_res, *tmp;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800230 struct pci_dev_resource *dev_res;
Ram Paic8adf9a2011-02-14 17:43:20 -0800231 resource_size_t add_size;
232 int idx;
233
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800234 list_for_each_entry_safe(add_res, tmp, realloc_head, list) {
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800235 bool found_match = false;
236
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800237 res = add_res->res;
Ram Paic8adf9a2011-02-14 17:43:20 -0800238 /* skip resource that has been reset */
239 if (!res->flags)
240 goto out;
241
242 /* skip this resource if not found in head list */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800243 list_for_each_entry(dev_res, head, list) {
244 if (dev_res->res == res) {
245 found_match = true;
246 break;
247 }
Ram Paic8adf9a2011-02-14 17:43:20 -0800248 }
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800249 if (!found_match)/* just skip */
250 continue;
Ram Paic8adf9a2011-02-14 17:43:20 -0800251
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800252 idx = res - &add_res->dev->resource[0];
253 add_size = add_res->add_size;
Ram Pai2bbc6942011-07-25 13:08:39 -0700254 if (!resource_size(res)) {
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800255 res->start = add_res->start;
Ram Pai2bbc6942011-07-25 13:08:39 -0700256 res->end = res->start + add_size - 1;
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800257 if (pci_assign_resource(add_res->dev, idx))
Ram Paic8adf9a2011-02-14 17:43:20 -0800258 reset_resource(res);
Ram Pai2bbc6942011-07-25 13:08:39 -0700259 } else {
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800260 resource_size_t align = add_res->min_align;
261 res->flags |= add_res->flags &
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800262 (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN);
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800263 if (pci_reassign_resource(add_res->dev, idx,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800264 add_size, align))
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800265 dev_printk(KERN_DEBUG, &add_res->dev->dev,
Yinghai Lub5924432012-01-21 02:08:31 -0800266 "failed to add %llx res[%d]=%pR\n",
267 (unsigned long long)add_size,
268 idx, res);
Ram Paic8adf9a2011-02-14 17:43:20 -0800269 }
270out:
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800271 list_del(&add_res->list);
272 kfree(add_res);
Ram Paic8adf9a2011-02-14 17:43:20 -0800273 }
274}
275
276/**
277 * assign_requested_resources_sorted() - satisfy resource requests
278 *
279 * @head : head of the list tracking requests for resources
280 * @failed_list : head of the list tracking requests that could
281 * not be allocated
282 *
283 * Satisfy resource requests of each element in the list. Add
284 * requests that could not satisfied to the failed_list.
285 */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800286static void assign_requested_resources_sorted(struct list_head *head,
287 struct list_head *fail_head)
Yinghai Lu6841ec62010-01-22 01:02:25 -0800288{
289 struct resource *res;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800290 struct pci_dev_resource *dev_res;
Yinghai Lu6841ec62010-01-22 01:02:25 -0800291 int idx;
292
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800293 list_for_each_entry(dev_res, head, list) {
294 res = dev_res->res;
295 idx = res - &dev_res->dev->resource[0];
296 if (resource_size(res) &&
297 pci_assign_resource(dev_res->dev, idx)) {
298 if (fail_head && !pci_is_root_bus(dev_res->dev->bus)) {
Yinghai Lu9a928662010-02-28 15:49:39 -0800299 /*
300 * if the failed res is for ROM BAR, and it will
301 * be enabled later, don't add it to the list
302 */
303 if (!((idx == PCI_ROM_RESOURCE) &&
304 (!(res->flags & IORESOURCE_ROM_ENABLE))))
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800305 add_to_failed_list(fail_head,
306 dev_res->dev, res);
Yinghai Lu9a928662010-02-28 15:49:39 -0800307 }
Ram Paifc075e12011-02-14 17:43:19 -0800308 reset_resource(res);
Rajesh Shah542df5d2005-04-28 00:25:50 -0700309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
311}
312
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800313static void __assign_resources_sorted(struct list_head *head,
314 struct list_head *realloc_head,
315 struct list_head *fail_head)
Ram Paic8adf9a2011-02-14 17:43:20 -0800316{
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800317 /*
318 * Should not assign requested resources at first.
319 * they could be adjacent, so later reassign can not reallocate
320 * them one by one in parent resource window.
321 * Try to assign requested + add_size at begining
322 * if could do that, could get out early.
323 * if could not do that, we still try to assign requested at first,
324 * then try to reassign add_size for some resources.
325 */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800326 LIST_HEAD(save_head);
327 LIST_HEAD(local_fail_head);
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800328 struct pci_dev_resource *save_res;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800329 struct pci_dev_resource *dev_res;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800330
331 /* Check if optional add_size is there */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800332 if (!realloc_head || list_empty(realloc_head))
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800333 goto requested_and_reassign;
334
335 /* Save original start, end, flags etc at first */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800336 list_for_each_entry(dev_res, head, list) {
337 if (add_to_list(&save_head, dev_res->dev, dev_res->res, 0, 0)) {
Yinghai Lubffc56d2012-01-21 02:08:30 -0800338 free_list(&save_head);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800339 goto requested_and_reassign;
340 }
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800341 }
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800342
343 /* Update res in head list with add_size in realloc_head list */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800344 list_for_each_entry(dev_res, head, list)
345 dev_res->res->end += get_res_add_size(realloc_head,
346 dev_res->res);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800347
348 /* Try updated head list with add_size added */
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800349 assign_requested_resources_sorted(head, &local_fail_head);
350
351 /* all assigned with add_size ? */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800352 if (list_empty(&local_fail_head)) {
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800353 /* Remove head list from realloc_head list */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800354 list_for_each_entry(dev_res, head, list)
355 remove_from_list(realloc_head, dev_res->res);
Yinghai Lubffc56d2012-01-21 02:08:30 -0800356 free_list(&save_head);
357 free_list(head);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800358 return;
359 }
360
Yinghai Lubffc56d2012-01-21 02:08:30 -0800361 free_list(&local_fail_head);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800362 /* Release assigned resource */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800363 list_for_each_entry(dev_res, head, list)
364 if (dev_res->res->parent)
365 release_resource(dev_res->res);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800366 /* Restore start/end/flags from saved list */
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800367 list_for_each_entry(save_res, &save_head, list) {
368 struct resource *res = save_res->res;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800369
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800370 res->start = save_res->start;
371 res->end = save_res->end;
372 res->flags = save_res->flags;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800373 }
Yinghai Lubffc56d2012-01-21 02:08:30 -0800374 free_list(&save_head);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800375
376requested_and_reassign:
Ram Paic8adf9a2011-02-14 17:43:20 -0800377 /* Satisfy the must-have resource requests */
378 assign_requested_resources_sorted(head, fail_head);
379
Ram Pai0a2daa12011-07-25 13:08:41 -0700380 /* Try to satisfy any additional optional resource
Ram Paic8adf9a2011-02-14 17:43:20 -0800381 requests */
Ram Pai9e8bf932011-07-25 13:08:42 -0700382 if (realloc_head)
383 reassign_resources_sorted(realloc_head, head);
Yinghai Lubffc56d2012-01-21 02:08:30 -0800384 free_list(head);
Ram Paic8adf9a2011-02-14 17:43:20 -0800385}
386
Yinghai Lu6841ec62010-01-22 01:02:25 -0800387static void pdev_assign_resources_sorted(struct pci_dev *dev,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800388 struct list_head *add_head,
389 struct list_head *fail_head)
Yinghai Lu6841ec62010-01-22 01:02:25 -0800390{
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800391 LIST_HEAD(head);
Yinghai Lu6841ec62010-01-22 01:02:25 -0800392
Yinghai Lu6841ec62010-01-22 01:02:25 -0800393 __dev_sort_resources(dev, &head);
Yinghai Lu8424d752012-01-21 02:08:21 -0800394 __assign_resources_sorted(&head, add_head, fail_head);
Yinghai Lu6841ec62010-01-22 01:02:25 -0800395
396}
397
398static void pbus_assign_resources_sorted(const struct pci_bus *bus,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800399 struct list_head *realloc_head,
400 struct list_head *fail_head)
Yinghai Lu6841ec62010-01-22 01:02:25 -0800401{
402 struct pci_dev *dev;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800403 LIST_HEAD(head);
Yinghai Lu6841ec62010-01-22 01:02:25 -0800404
Yinghai Lu6841ec62010-01-22 01:02:25 -0800405 list_for_each_entry(dev, &bus->devices, bus_list)
406 __dev_sort_resources(dev, &head);
407
Ram Pai9e8bf932011-07-25 13:08:42 -0700408 __assign_resources_sorted(&head, realloc_head, fail_head);
Yinghai Lu6841ec62010-01-22 01:02:25 -0800409}
410
Dominik Brodowskib3743fa2005-09-09 13:03:23 -0700411void pci_setup_cardbus(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
413 struct pci_dev *bridge = bus->self;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600414 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 struct pci_bus_region region;
416
Bjorn Helgaas865df572009-11-04 10:32:57 -0700417 dev_info(&bridge->dev, "CardBus bridge to [bus %02x-%02x]\n",
418 bus->secondary, bus->subordinate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600420 res = bus->resource[0];
421 pcibios_resource_to_bus(bridge, &region, res);
422 if (res->flags & IORESOURCE_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 /*
424 * The IO resource is allocated a range twice as large as it
425 * would normally need. This allows us to set both IO regs.
426 */
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600427 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
429 region.start);
430 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
431 region.end);
432 }
433
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600434 res = bus->resource[1];
435 pcibios_resource_to_bus(bridge, &region, res);
436 if (res->flags & IORESOURCE_IO) {
437 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
439 region.start);
440 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
441 region.end);
442 }
443
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600444 res = bus->resource[2];
445 pcibios_resource_to_bus(bridge, &region, res);
446 if (res->flags & IORESOURCE_MEM) {
447 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
449 region.start);
450 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
451 region.end);
452 }
453
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600454 res = bus->resource[3];
455 pcibios_resource_to_bus(bridge, &region, res);
456 if (res->flags & IORESOURCE_MEM) {
457 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
459 region.start);
460 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
461 region.end);
462 }
463}
Dominik Brodowskib3743fa2005-09-09 13:03:23 -0700464EXPORT_SYMBOL(pci_setup_cardbus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466/* Initialize bridges with base/limit values we have collected.
467 PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
468 requires that if there is no I/O ports or memory behind the
469 bridge, corresponding range must be turned off by writing base
470 value greater than limit to the bridge's base/limit registers.
471
472 Note: care must be taken when updating I/O base/limit registers
473 of bridges which support 32-bit I/O. This update requires two
474 config space writes, so it's quite possible that an I/O window of
475 the bridge will have some undesirable address (e.g. 0) after the
476 first write. Ditto 64-bit prefetchable MMIO. */
Yinghai Lu7cc59972009-12-22 15:02:21 -0800477static void pci_setup_bridge_io(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 struct pci_dev *bridge = bus->self;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600480 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 struct pci_bus_region region;
Yinghai Lu7cc59972009-12-22 15:02:21 -0800482 u32 l, io_upper16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 /* Set up the top and bottom of the PCI I/O segment for this bus. */
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600485 res = bus->resource[0];
486 pcibios_resource_to_bus(bridge, &region, res);
487 if (res->flags & IORESOURCE_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 pci_read_config_dword(bridge, PCI_IO_BASE, &l);
489 l &= 0xffff0000;
490 l |= (region.start >> 8) & 0x00f0;
491 l |= region.end & 0xf000;
492 /* Set up upper 16 bits of I/O base/limit. */
493 io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600494 dev_info(&bridge->dev, " bridge window %pR\n", res);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800495 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 /* Clear upper 16 bits of I/O base/limit. */
497 io_upper16 = 0;
498 l = 0x00f0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
500 /* Temporarily disable the I/O range before updating PCI_IO_BASE. */
501 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
502 /* Update lower 16 bits of I/O base/limit. */
503 pci_write_config_dword(bridge, PCI_IO_BASE, l);
504 /* Update upper 16 bits of I/O base/limit. */
505 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800506}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Yinghai Lu7cc59972009-12-22 15:02:21 -0800508static void pci_setup_bridge_mmio(struct pci_bus *bus)
509{
510 struct pci_dev *bridge = bus->self;
511 struct resource *res;
512 struct pci_bus_region region;
513 u32 l;
514
515 /* Set up the top and bottom of the PCI Memory segment for this bus. */
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600516 res = bus->resource[1];
517 pcibios_resource_to_bus(bridge, &region, res);
518 if (res->flags & IORESOURCE_MEM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 l = (region.start >> 16) & 0xfff0;
520 l |= region.end & 0xfff00000;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600521 dev_info(&bridge->dev, " bridge window %pR\n", res);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800522 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 l = 0x0000fff0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
525 pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800526}
527
528static void pci_setup_bridge_mmio_pref(struct pci_bus *bus)
529{
530 struct pci_dev *bridge = bus->self;
531 struct resource *res;
532 struct pci_bus_region region;
533 u32 l, bu, lu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 /* Clear out the upper 32 bits of PREF limit.
536 If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
537 disables PREF range, which is ok. */
538 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
539
540 /* Set up PREF base/limit. */
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100541 bu = lu = 0;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600542 res = bus->resource[2];
543 pcibios_resource_to_bus(bridge, &region, res);
544 if (res->flags & IORESOURCE_PREFETCH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 l = (region.start >> 16) & 0xfff0;
546 l |= region.end & 0xfff00000;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600547 if (res->flags & IORESOURCE_MEM_64) {
Yinghai Lu1f82de12009-04-23 20:48:32 -0700548 bu = upper_32_bits(region.start);
549 lu = upper_32_bits(region.end);
Yinghai Lu1f82de12009-04-23 20:48:32 -0700550 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600551 dev_info(&bridge->dev, " bridge window %pR\n", res);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800552 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 l = 0x0000fff0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
555 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
556
Alex Williamson59353ea2009-11-30 14:51:44 -0700557 /* Set the upper 32 bits of PREF base & limit. */
558 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
559 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800560}
561
562static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type)
563{
564 struct pci_dev *bridge = bus->self;
565
Yinghai Lu7cc59972009-12-22 15:02:21 -0800566 dev_info(&bridge->dev, "PCI bridge to [bus %02x-%02x]\n",
567 bus->secondary, bus->subordinate);
568
569 if (type & IORESOURCE_IO)
570 pci_setup_bridge_io(bus);
571
572 if (type & IORESOURCE_MEM)
573 pci_setup_bridge_mmio(bus);
574
575 if (type & IORESOURCE_PREFETCH)
576 pci_setup_bridge_mmio_pref(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
579}
580
Benjamin Herrenschmidte2444272011-09-11 14:08:38 -0300581void pci_setup_bridge(struct pci_bus *bus)
Yinghai Lu7cc59972009-12-22 15:02:21 -0800582{
583 unsigned long type = IORESOURCE_IO | IORESOURCE_MEM |
584 IORESOURCE_PREFETCH;
585
586 __pci_setup_bridge(bus, type);
587}
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589/* Check whether the bridge supports optional I/O and
590 prefetchable memory ranges. If not, the respective
591 base/limit registers must be read-only and read as 0. */
Sam Ravnborg96bde062007-03-26 21:53:30 -0800592static void pci_bridge_check_ranges(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
594 u16 io;
595 u32 pmem;
596 struct pci_dev *bridge = bus->self;
597 struct resource *b_res;
598
599 b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
600 b_res[1].flags |= IORESOURCE_MEM;
601
602 pci_read_config_word(bridge, PCI_IO_BASE, &io);
603 if (!io) {
604 pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0);
605 pci_read_config_word(bridge, PCI_IO_BASE, &io);
606 pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
607 }
608 if (io)
609 b_res[0].flags |= IORESOURCE_IO;
610 /* DECchip 21050 pass 2 errata: the bridge may miss an address
611 disconnect boundary by one PCI data phase.
612 Workaround: do not use prefetching on this device. */
613 if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001)
614 return;
615 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
616 if (!pmem) {
617 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE,
618 0xfff0fff0);
619 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
620 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0);
621 }
Yinghai Lu1f82de12009-04-23 20:48:32 -0700622 if (pmem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
Yinghai Lu99586102010-01-22 01:02:28 -0800624 if ((pmem & PCI_PREF_RANGE_TYPE_MASK) ==
625 PCI_PREF_RANGE_TYPE_64) {
Yinghai Lu1f82de12009-04-23 20:48:32 -0700626 b_res[2].flags |= IORESOURCE_MEM_64;
Yinghai Lu99586102010-01-22 01:02:28 -0800627 b_res[2].flags |= PCI_PREF_RANGE_TYPE_64;
628 }
Yinghai Lu1f82de12009-04-23 20:48:32 -0700629 }
630
631 /* double check if bridge does support 64 bit pref */
632 if (b_res[2].flags & IORESOURCE_MEM_64) {
633 u32 mem_base_hi, tmp;
634 pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32,
635 &mem_base_hi);
636 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
637 0xffffffff);
638 pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp);
639 if (!tmp)
640 b_res[2].flags &= ~IORESOURCE_MEM_64;
641 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
642 mem_base_hi);
643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
646/* Helper function for sizing routines: find first available
647 bus resource of a given type. Note: we intentionally skip
648 the bus resources which have already been assigned (that is,
649 have non-NULL parent resource). */
Sam Ravnborg96bde062007-03-26 21:53:30 -0800650static struct resource *find_free_bus_resource(struct pci_bus *bus, unsigned long type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
652 int i;
653 struct resource *r;
654 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
655 IORESOURCE_PREFETCH;
656
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -0700657 pci_bus_for_each_resource(bus, r, i) {
Ivan Kokshaysky299de032005-06-15 18:59:27 +0400658 if (r == &ioport_resource || r == &iomem_resource)
659 continue;
Jesse Barnes55a10982009-10-27 09:39:18 -0700660 if (r && (r->flags & type_mask) == type && !r->parent)
661 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
663 return NULL;
664}
665
Ram Pai13583b12011-02-14 17:43:17 -0800666static resource_size_t calculate_iosize(resource_size_t size,
667 resource_size_t min_size,
668 resource_size_t size1,
669 resource_size_t old_size,
670 resource_size_t align)
671{
672 if (size < min_size)
673 size = min_size;
674 if (old_size == 1 )
675 old_size = 0;
676 /* To be fixed in 2.5: we should have sort of HAVE_ISA
677 flag in the struct pci_bus. */
678#if defined(CONFIG_ISA) || defined(CONFIG_EISA)
679 size = (size & 0xff) + ((size & ~0xffUL) << 2);
680#endif
681 size = ALIGN(size + size1, align);
682 if (size < old_size)
683 size = old_size;
684 return size;
685}
686
687static resource_size_t calculate_memsize(resource_size_t size,
688 resource_size_t min_size,
689 resource_size_t size1,
690 resource_size_t old_size,
691 resource_size_t align)
692{
693 if (size < min_size)
694 size = min_size;
695 if (old_size == 1 )
696 old_size = 0;
697 if (size < old_size)
698 size = old_size;
699 size = ALIGN(size + size1, align);
700 return size;
701}
702
Ram Paic8adf9a2011-02-14 17:43:20 -0800703/**
704 * pbus_size_io() - size the io window of a given bus
705 *
706 * @bus : the bus
707 * @min_size : the minimum io window that must to be allocated
708 * @add_size : additional optional io window
Ram Pai9e8bf932011-07-25 13:08:42 -0700709 * @realloc_head : track the additional io window on this list
Ram Paic8adf9a2011-02-14 17:43:20 -0800710 *
711 * Sizing the IO windows of the PCI-PCI bridge is trivial,
712 * since these windows have 4K granularity and the IO ranges
713 * of non-bridge PCI devices are limited to 256 bytes.
714 * We must be careful with the ISA aliasing though.
715 */
716static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800717 resource_size_t add_size, struct list_head *realloc_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
719 struct pci_dev *dev;
720 struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO);
Ram Paic8adf9a2011-02-14 17:43:20 -0800721 unsigned long size = 0, size0 = 0, size1 = 0;
Yinghai Lube768912011-07-25 13:08:38 -0700722 resource_size_t children_add_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 if (!b_res)
725 return;
726
727 list_for_each_entry(dev, &bus->devices, bus_list) {
728 int i;
729
730 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
731 struct resource *r = &dev->resource[i];
732 unsigned long r_size;
733
734 if (r->parent || !(r->flags & IORESOURCE_IO))
735 continue;
Zhao, Yu022edd82008-10-13 19:24:28 +0800736 r_size = resource_size(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 if (r_size < 0x400)
739 /* Might be re-aligned for ISA */
740 size += r_size;
741 else
742 size1 += r_size;
Yinghai Lube768912011-07-25 13:08:38 -0700743
Ram Pai9e8bf932011-07-25 13:08:42 -0700744 if (realloc_head)
745 children_add_size += get_res_add_size(realloc_head, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
747 }
Ram Paic8adf9a2011-02-14 17:43:20 -0800748 size0 = calculate_iosize(size, min_size, size1,
Ram Pai13583b12011-02-14 17:43:17 -0800749 resource_size(b_res), 4096);
Yinghai Lube768912011-07-25 13:08:38 -0700750 if (children_add_size > add_size)
751 add_size = children_add_size;
Ram Pai9e8bf932011-07-25 13:08:42 -0700752 size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
Yinghai Lua4ac9fe2012-01-21 02:08:17 -0800753 calculate_iosize(size, min_size, add_size + size1,
Ram Paic8adf9a2011-02-14 17:43:20 -0800754 resource_size(b_res), 4096);
755 if (!size0 && !size1) {
Bjorn Helgaas865df572009-11-04 10:32:57 -0700756 if (b_res->start || b_res->end)
757 dev_info(&bus->self->dev, "disabling bridge window "
758 "%pR to [bus %02x-%02x] (unused)\n", b_res,
759 bus->secondary, bus->subordinate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 b_res->flags = 0;
761 return;
762 }
763 /* Alignment of the IO window is always 4K */
764 b_res->start = 4096;
Ram Paic8adf9a2011-02-14 17:43:20 -0800765 b_res->end = b_res->start + size0 - 1;
Ivan Kokshaysky88452562008-03-30 19:50:14 +0400766 b_res->flags |= IORESOURCE_STARTALIGN;
Yinghai Lub5924432012-01-21 02:08:31 -0800767 if (size1 > size0 && realloc_head) {
Ram Pai9e8bf932011-07-25 13:08:42 -0700768 add_to_list(realloc_head, bus->self, b_res, size1-size0, 4096);
Yinghai Lub5924432012-01-21 02:08:31 -0800769 dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window "
770 "%pR to [bus %02x-%02x] add_size %lx\n", b_res,
771 bus->secondary, bus->subordinate, size1-size0);
772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
Ram Paic8adf9a2011-02-14 17:43:20 -0800775/**
776 * pbus_size_mem() - size the memory window of a given bus
777 *
778 * @bus : the bus
779 * @min_size : the minimum memory window that must to be allocated
780 * @add_size : additional optional memory window
Ram Pai9e8bf932011-07-25 13:08:42 -0700781 * @realloc_head : track the additional memory window on this list
Ram Paic8adf9a2011-02-14 17:43:20 -0800782 *
783 * Calculate the size of the bus and minimal alignment which
784 * guarantees that all child resources fit in this size.
785 */
Eric W. Biederman28760482009-09-09 14:09:24 -0700786static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
Ram Paic8adf9a2011-02-14 17:43:20 -0800787 unsigned long type, resource_size_t min_size,
788 resource_size_t add_size,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800789 struct list_head *realloc_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
791 struct pci_dev *dev;
Ram Paic8adf9a2011-02-14 17:43:20 -0800792 resource_size_t min_align, align, size, size0, size1;
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100793 resource_size_t aligns[12]; /* Alignments from 1Mb to 2Gb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 int order, max_order;
795 struct resource *b_res = find_free_bus_resource(bus, type);
Yinghai Lu1f82de12009-04-23 20:48:32 -0700796 unsigned int mem64_mask = 0;
Yinghai Lube768912011-07-25 13:08:38 -0700797 resource_size_t children_add_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 if (!b_res)
800 return 0;
801
802 memset(aligns, 0, sizeof(aligns));
803 max_order = 0;
804 size = 0;
805
Yinghai Lu1f82de12009-04-23 20:48:32 -0700806 mem64_mask = b_res->flags & IORESOURCE_MEM_64;
807 b_res->flags &= ~IORESOURCE_MEM_64;
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 list_for_each_entry(dev, &bus->devices, bus_list) {
810 int i;
Yinghai Lu1f82de12009-04-23 20:48:32 -0700811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
813 struct resource *r = &dev->resource[i];
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100814 resource_size_t r_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 if (r->parent || (r->flags & mask) != type)
817 continue;
Zhao, Yu022edd82008-10-13 19:24:28 +0800818 r_size = resource_size(r);
Yinghai Lu2aceefc2011-07-25 13:08:40 -0700819#ifdef CONFIG_PCI_IOV
820 /* put SRIOV requested res to the optional list */
Ram Pai9e8bf932011-07-25 13:08:42 -0700821 if (realloc_head && i >= PCI_IOV_RESOURCES &&
Yinghai Lu2aceefc2011-07-25 13:08:40 -0700822 i <= PCI_IOV_RESOURCE_END) {
823 r->end = r->start - 1;
Ram Pai9e8bf932011-07-25 13:08:42 -0700824 add_to_list(realloc_head, dev, r, r_size, 0/* dont' care */);
Yinghai Lu2aceefc2011-07-25 13:08:40 -0700825 children_add_size += r_size;
826 continue;
827 }
828#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 /* For bridges size != alignment */
Chris Wright6faf17f2009-08-28 13:00:06 -0700830 align = pci_resource_alignment(dev, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 order = __ffs(align) - 20;
832 if (order > 11) {
Bjorn Helgaas865df572009-11-04 10:32:57 -0700833 dev_warn(&dev->dev, "disabling BAR %d: %pR "
834 "(bad alignment %#llx)\n", i, r,
835 (unsigned long long) align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 r->flags = 0;
837 continue;
838 }
839 size += r_size;
840 if (order < 0)
841 order = 0;
842 /* Exclude ranges with size > align from
843 calculation of the alignment. */
844 if (r_size == align)
845 aligns[order] += align;
846 if (order > max_order)
847 max_order = order;
Yinghai Lu1f82de12009-04-23 20:48:32 -0700848 mem64_mask &= r->flags & IORESOURCE_MEM_64;
Yinghai Lube768912011-07-25 13:08:38 -0700849
Ram Pai9e8bf932011-07-25 13:08:42 -0700850 if (realloc_head)
851 children_add_size += get_res_add_size(realloc_head, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 align = 0;
855 min_align = 0;
856 for (order = 0; order <= max_order; order++) {
Jeremy Fitzhardinge8308c542008-09-11 01:31:50 -0700857 resource_size_t align1 = 1;
858
859 align1 <<= (order + 20);
860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 if (!align)
862 min_align = align1;
Milind Arun Choudhary6f6f8c22007-07-09 11:55:51 -0700863 else if (ALIGN(align + min_align, min_align) < align1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 min_align = align1 >> 1;
865 align += aligns[order];
866 }
Linus Torvaldsb42282e2011-04-11 10:53:11 -0700867 size0 = calculate_memsize(size, min_size, 0, resource_size(b_res), min_align);
Yinghai Lube768912011-07-25 13:08:38 -0700868 if (children_add_size > add_size)
869 add_size = children_add_size;
Ram Pai9e8bf932011-07-25 13:08:42 -0700870 size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
Yinghai Lua4ac9fe2012-01-21 02:08:17 -0800871 calculate_memsize(size, min_size, add_size,
Linus Torvaldsb42282e2011-04-11 10:53:11 -0700872 resource_size(b_res), min_align);
Ram Paic8adf9a2011-02-14 17:43:20 -0800873 if (!size0 && !size1) {
Bjorn Helgaas865df572009-11-04 10:32:57 -0700874 if (b_res->start || b_res->end)
875 dev_info(&bus->self->dev, "disabling bridge window "
876 "%pR to [bus %02x-%02x] (unused)\n", b_res,
877 bus->secondary, bus->subordinate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 b_res->flags = 0;
879 return 1;
880 }
881 b_res->start = min_align;
Ram Paic8adf9a2011-02-14 17:43:20 -0800882 b_res->end = size0 + min_align - 1;
883 b_res->flags |= IORESOURCE_STARTALIGN | mem64_mask;
Yinghai Lub5924432012-01-21 02:08:31 -0800884 if (size1 > size0 && realloc_head) {
Ram Pai9e8bf932011-07-25 13:08:42 -0700885 add_to_list(realloc_head, bus->self, b_res, size1-size0, min_align);
Yinghai Lub5924432012-01-21 02:08:31 -0800886 dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window "
887 "%pR to [bus %02x-%02x] add_size %llx\n", b_res,
888 bus->secondary, bus->subordinate, (unsigned long long)size1-size0);
889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 return 1;
891}
892
Ram Pai0a2daa12011-07-25 13:08:41 -0700893unsigned long pci_cardbus_resource_alignment(struct resource *res)
894{
895 if (res->flags & IORESOURCE_IO)
896 return pci_cardbus_io_size;
897 if (res->flags & IORESOURCE_MEM)
898 return pci_cardbus_mem_size;
899 return 0;
900}
901
902static void pci_bus_size_cardbus(struct pci_bus *bus,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800903 struct list_head *realloc_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
905 struct pci_dev *bridge = bus->self;
906 struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
907 u16 ctrl;
908
909 /*
910 * Reserve some resources for CardBus. We reserve
911 * a fixed amount of bus space for CardBus bridges.
912 */
Linus Torvalds934b7022008-04-22 18:16:30 -0700913 b_res[0].start = 0;
Linus Torvalds934b7022008-04-22 18:16:30 -0700914 b_res[0].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
Ram Pai9e8bf932011-07-25 13:08:42 -0700915 if (realloc_head)
916 add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size, 0 /* dont care */);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Linus Torvalds934b7022008-04-22 18:16:30 -0700918 b_res[1].start = 0;
Linus Torvalds934b7022008-04-22 18:16:30 -0700919 b_res[1].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
Ram Pai9e8bf932011-07-25 13:08:42 -0700920 if (realloc_head)
921 add_to_list(realloc_head, bridge, b_res+1, pci_cardbus_io_size, 0 /* dont care */);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 /*
924 * Check whether prefetchable memory is supported
925 * by this bridge.
926 */
927 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
928 if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
929 ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
930 pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
931 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
932 }
933
934 /*
935 * If we have prefetchable memory support, allocate
936 * two regions. Otherwise, allocate one region of
937 * twice the size.
938 */
939 if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
Linus Torvalds934b7022008-04-22 18:16:30 -0700940 b_res[2].start = 0;
Linus Torvalds934b7022008-04-22 18:16:30 -0700941 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_SIZEALIGN;
Ram Pai9e8bf932011-07-25 13:08:42 -0700942 if (realloc_head)
943 add_to_list(realloc_head, bridge, b_res+2, pci_cardbus_mem_size, 0 /* dont care */);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Linus Torvalds934b7022008-04-22 18:16:30 -0700945 b_res[3].start = 0;
Linus Torvalds934b7022008-04-22 18:16:30 -0700946 b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
Ram Pai9e8bf932011-07-25 13:08:42 -0700947 if (realloc_head)
948 add_to_list(realloc_head, bridge, b_res+3, pci_cardbus_mem_size, 0 /* dont care */);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 } else {
Linus Torvalds934b7022008-04-22 18:16:30 -0700950 b_res[3].start = 0;
Linus Torvalds934b7022008-04-22 18:16:30 -0700951 b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
Ram Pai9e8bf932011-07-25 13:08:42 -0700952 if (realloc_head)
953 add_to_list(realloc_head, bridge, b_res+3, pci_cardbus_mem_size * 2, 0 /* dont care */);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 }
Ram Pai0a2daa12011-07-25 13:08:41 -0700955
956 /* set the size of the resource to zero, so that the resource does not
957 * get assigned during required-resource allocation cycle but gets assigned
958 * during the optional-resource allocation cycle.
959 */
960 b_res[0].start = b_res[1].start = b_res[2].start = b_res[3].start = 1;
961 b_res[0].end = b_res[1].end = b_res[2].end = b_res[3].end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962}
963
Ram Paic8adf9a2011-02-14 17:43:20 -0800964void __ref __pci_bus_size_bridges(struct pci_bus *bus,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800965 struct list_head *realloc_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
967 struct pci_dev *dev;
968 unsigned long mask, prefmask;
Ram Paic8adf9a2011-02-14 17:43:20 -0800969 resource_size_t additional_mem_size = 0, additional_io_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 list_for_each_entry(dev, &bus->devices, bus_list) {
972 struct pci_bus *b = dev->subordinate;
973 if (!b)
974 continue;
975
976 switch (dev->class >> 8) {
977 case PCI_CLASS_BRIDGE_CARDBUS:
Ram Pai9e8bf932011-07-25 13:08:42 -0700978 pci_bus_size_cardbus(b, realloc_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 break;
980
981 case PCI_CLASS_BRIDGE_PCI:
982 default:
Ram Pai9e8bf932011-07-25 13:08:42 -0700983 __pci_bus_size_bridges(b, realloc_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 break;
985 }
986 }
987
988 /* The root bus? */
989 if (!bus->self)
990 return;
991
992 switch (bus->self->class >> 8) {
993 case PCI_CLASS_BRIDGE_CARDBUS:
994 /* don't size cardbuses yet. */
995 break;
996
997 case PCI_CLASS_BRIDGE_PCI:
998 pci_bridge_check_ranges(bus);
Eric W. Biederman28760482009-09-09 14:09:24 -0700999 if (bus->self->is_hotplug_bridge) {
Ram Paic8adf9a2011-02-14 17:43:20 -08001000 additional_io_size = pci_hotplug_io_size;
1001 additional_mem_size = pci_hotplug_mem_size;
Eric W. Biederman28760482009-09-09 14:09:24 -07001002 }
Ram Paic8adf9a2011-02-14 17:43:20 -08001003 /*
1004 * Follow thru
1005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 default:
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001007 pbus_size_io(bus, realloc_head ? 0 : additional_io_size,
1008 additional_io_size, realloc_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 /* If the bridge supports prefetchable range, size it
1010 separately. If it doesn't, or its prefetchable window
1011 has already been allocated by arch code, try
1012 non-prefetchable range for both types of PCI memory
1013 resources. */
1014 mask = IORESOURCE_MEM;
1015 prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001016 if (pbus_size_mem(bus, prefmask, prefmask,
1017 realloc_head ? 0 : additional_mem_size,
1018 additional_mem_size, realloc_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 mask = prefmask; /* Success, size non-prefetch only. */
Eric W. Biederman28760482009-09-09 14:09:24 -07001020 else
Ram Paic8adf9a2011-02-14 17:43:20 -08001021 additional_mem_size += additional_mem_size;
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001022 pbus_size_mem(bus, mask, IORESOURCE_MEM,
1023 realloc_head ? 0 : additional_mem_size,
1024 additional_mem_size, realloc_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 break;
1026 }
1027}
Ram Paic8adf9a2011-02-14 17:43:20 -08001028
1029void __ref pci_bus_size_bridges(struct pci_bus *bus)
1030{
1031 __pci_bus_size_bridges(bus, NULL);
1032}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033EXPORT_SYMBOL(pci_bus_size_bridges);
1034
Yinghai Lu568ddef2010-01-22 01:02:21 -08001035static void __ref __pci_bus_assign_resources(const struct pci_bus *bus,
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001036 struct list_head *realloc_head,
1037 struct list_head *fail_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
1039 struct pci_bus *b;
1040 struct pci_dev *dev;
1041
Ram Pai9e8bf932011-07-25 13:08:42 -07001042 pbus_assign_resources_sorted(bus, realloc_head, fail_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 list_for_each_entry(dev, &bus->devices, bus_list) {
1045 b = dev->subordinate;
1046 if (!b)
1047 continue;
1048
Ram Pai9e8bf932011-07-25 13:08:42 -07001049 __pci_bus_assign_resources(b, realloc_head, fail_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 switch (dev->class >> 8) {
1052 case PCI_CLASS_BRIDGE_PCI:
Yinghai Lu6841ec62010-01-22 01:02:25 -08001053 if (!pci_is_enabled(dev))
1054 pci_setup_bridge(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 break;
1056
1057 case PCI_CLASS_BRIDGE_CARDBUS:
1058 pci_setup_cardbus(b);
1059 break;
1060
1061 default:
Bjorn Helgaas80ccba12008-06-13 10:52:11 -06001062 dev_info(&dev->dev, "not setting up bridge for bus "
1063 "%04x:%02x\n", pci_domain_nr(b), b->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 break;
1065 }
1066 }
1067}
Yinghai Lu568ddef2010-01-22 01:02:21 -08001068
1069void __ref pci_bus_assign_resources(const struct pci_bus *bus)
1070{
Ram Paic8adf9a2011-02-14 17:43:20 -08001071 __pci_bus_assign_resources(bus, NULL, NULL);
Yinghai Lu568ddef2010-01-22 01:02:21 -08001072}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073EXPORT_SYMBOL(pci_bus_assign_resources);
1074
Yinghai Lu6841ec62010-01-22 01:02:25 -08001075static void __ref __pci_bridge_assign_resources(const struct pci_dev *bridge,
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001076 struct list_head *add_head,
1077 struct list_head *fail_head)
Yinghai Lu6841ec62010-01-22 01:02:25 -08001078{
1079 struct pci_bus *b;
1080
Yinghai Lu8424d752012-01-21 02:08:21 -08001081 pdev_assign_resources_sorted((struct pci_dev *)bridge,
1082 add_head, fail_head);
Yinghai Lu6841ec62010-01-22 01:02:25 -08001083
1084 b = bridge->subordinate;
1085 if (!b)
1086 return;
1087
Yinghai Lu8424d752012-01-21 02:08:21 -08001088 __pci_bus_assign_resources(b, add_head, fail_head);
Yinghai Lu6841ec62010-01-22 01:02:25 -08001089
1090 switch (bridge->class >> 8) {
1091 case PCI_CLASS_BRIDGE_PCI:
1092 pci_setup_bridge(b);
1093 break;
1094
1095 case PCI_CLASS_BRIDGE_CARDBUS:
1096 pci_setup_cardbus(b);
1097 break;
1098
1099 default:
1100 dev_info(&bridge->dev, "not setting up bridge for bus "
1101 "%04x:%02x\n", pci_domain_nr(b), b->number);
1102 break;
1103 }
1104}
Yinghai Lu5009b462010-01-22 01:02:20 -08001105static void pci_bridge_release_resources(struct pci_bus *bus,
1106 unsigned long type)
1107{
1108 int idx;
1109 bool changed = false;
1110 struct pci_dev *dev;
1111 struct resource *r;
1112 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1113 IORESOURCE_PREFETCH;
1114
1115 dev = bus->self;
1116 for (idx = PCI_BRIDGE_RESOURCES; idx <= PCI_BRIDGE_RESOURCE_END;
1117 idx++) {
1118 r = &dev->resource[idx];
1119 if ((r->flags & type_mask) != type)
1120 continue;
1121 if (!r->parent)
1122 continue;
1123 /*
1124 * if there are children under that, we should release them
1125 * all
1126 */
1127 release_child_resources(r);
1128 if (!release_resource(r)) {
1129 dev_printk(KERN_DEBUG, &dev->dev,
1130 "resource %d %pR released\n", idx, r);
1131 /* keep the old size */
1132 r->end = resource_size(r) - 1;
1133 r->start = 0;
1134 r->flags = 0;
1135 changed = true;
1136 }
1137 }
1138
1139 if (changed) {
1140 /* avoiding touch the one without PREF */
1141 if (type & IORESOURCE_PREFETCH)
1142 type = IORESOURCE_PREFETCH;
1143 __pci_setup_bridge(bus, type);
1144 }
1145}
1146
1147enum release_type {
1148 leaf_only,
1149 whole_subtree,
1150};
1151/*
1152 * try to release pci bridge resources that is from leaf bridge,
1153 * so we can allocate big new one later
1154 */
1155static void __ref pci_bus_release_bridge_resources(struct pci_bus *bus,
1156 unsigned long type,
1157 enum release_type rel_type)
1158{
1159 struct pci_dev *dev;
1160 bool is_leaf_bridge = true;
1161
1162 list_for_each_entry(dev, &bus->devices, bus_list) {
1163 struct pci_bus *b = dev->subordinate;
1164 if (!b)
1165 continue;
1166
1167 is_leaf_bridge = false;
1168
1169 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
1170 continue;
1171
1172 if (rel_type == whole_subtree)
1173 pci_bus_release_bridge_resources(b, type,
1174 whole_subtree);
1175 }
1176
1177 if (pci_is_root_bus(bus))
1178 return;
1179
1180 if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
1181 return;
1182
1183 if ((rel_type == whole_subtree) || is_leaf_bridge)
1184 pci_bridge_release_resources(bus, type);
1185}
1186
Yinghai Lu76fbc262008-06-23 20:33:06 +02001187static void pci_bus_dump_res(struct pci_bus *bus)
1188{
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -07001189 struct resource *res;
1190 int i;
Yinghai Lu76fbc262008-06-23 20:33:06 +02001191
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -07001192 pci_bus_for_each_resource(bus, res, i) {
Yinghai Lu7c9342b2009-12-22 15:02:24 -08001193 if (!res || !res->end || !res->flags)
Yinghai Lu76fbc262008-06-23 20:33:06 +02001194 continue;
1195
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001196 dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res);
Yinghai Lu76fbc262008-06-23 20:33:06 +02001197 }
1198}
1199
1200static void pci_bus_dump_resources(struct pci_bus *bus)
1201{
1202 struct pci_bus *b;
1203 struct pci_dev *dev;
1204
1205
1206 pci_bus_dump_res(bus);
1207
1208 list_for_each_entry(dev, &bus->devices, bus_list) {
1209 b = dev->subordinate;
1210 if (!b)
1211 continue;
1212
1213 pci_bus_dump_resources(b);
1214 }
1215}
1216
Yinghai Luda7822e2011-05-12 17:11:37 -07001217static int __init pci_bus_get_depth(struct pci_bus *bus)
1218{
1219 int depth = 0;
1220 struct pci_dev *dev;
1221
1222 list_for_each_entry(dev, &bus->devices, bus_list) {
1223 int ret;
1224 struct pci_bus *b = dev->subordinate;
1225 if (!b)
1226 continue;
1227
1228 ret = pci_bus_get_depth(b);
1229 if (ret + 1 > depth)
1230 depth = ret + 1;
1231 }
1232
1233 return depth;
1234}
1235static int __init pci_get_max_depth(void)
1236{
1237 int depth = 0;
1238 struct pci_bus *bus;
1239
1240 list_for_each_entry(bus, &pci_root_buses, node) {
1241 int ret;
1242
1243 ret = pci_bus_get_depth(bus);
1244 if (ret > depth)
1245 depth = ret;
1246 }
1247
1248 return depth;
1249}
1250
Ram Paif483d392011-07-07 11:19:10 -07001251
Yinghai Luda7822e2011-05-12 17:11:37 -07001252/*
1253 * first try will not touch pci bridge res
1254 * second and later try will clear small leaf bridge res
1255 * will stop till to the max deepth if can not find good one
1256 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257void __init
1258pci_assign_unassigned_resources(void)
1259{
1260 struct pci_bus *bus;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001261 LIST_HEAD(realloc_head); /* list of resources that
Ram Paic8adf9a2011-02-14 17:43:20 -08001262 want additional resources */
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001263 struct list_head *add_list = NULL;
Yinghai Luda7822e2011-05-12 17:11:37 -07001264 int tried_times = 0;
1265 enum release_type rel_type = leaf_only;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001266 LIST_HEAD(fail_head);
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001267 struct pci_dev_resource *fail_res;
Yinghai Luda7822e2011-05-12 17:11:37 -07001268 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1269 IORESOURCE_PREFETCH;
1270 unsigned long failed_type;
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001271 int pci_try_num = 1;
Yinghai Luda7822e2011-05-12 17:11:37 -07001272
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001273 /* don't realloc if asked to do so */
1274 if (pci_realloc_enabled()) {
1275 int max_depth = pci_get_max_depth();
1276
1277 pci_try_num = max_depth + 1;
1278 printk(KERN_DEBUG "PCI: max bus depth: %d pci_try_num: %d\n",
1279 max_depth, pci_try_num);
1280 }
Yinghai Luda7822e2011-05-12 17:11:37 -07001281
1282again:
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001283 /*
1284 * last try will use add_list, otherwise will try good to have as
1285 * must have, so can realloc parent bridge resource
1286 */
1287 if (tried_times + 1 == pci_try_num)
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001288 add_list = &realloc_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 /* Depth first, calculate sizes and alignments of all
1290 subordinate buses. */
Yinghai Luda7822e2011-05-12 17:11:37 -07001291 list_for_each_entry(bus, &pci_root_buses, node)
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001292 __pci_bus_size_bridges(bus, add_list);
Ram Paic8adf9a2011-02-14 17:43:20 -08001293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 /* Depth last, allocate resources and update the hardware. */
Yinghai Luda7822e2011-05-12 17:11:37 -07001295 list_for_each_entry(bus, &pci_root_buses, node)
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001296 __pci_bus_assign_resources(bus, add_list, &fail_head);
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001297 if (add_list)
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001298 BUG_ON(!list_empty(add_list));
Yinghai Luda7822e2011-05-12 17:11:37 -07001299 tried_times++;
1300
1301 /* any device complain? */
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001302 if (list_empty(&fail_head))
Yinghai Luda7822e2011-05-12 17:11:37 -07001303 goto enable_and_dump;
Ram Paif483d392011-07-07 11:19:10 -07001304
Yinghai Luda7822e2011-05-12 17:11:37 -07001305 failed_type = 0;
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001306 list_for_each_entry(fail_res, &fail_head, list)
1307 failed_type |= fail_res->flags;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001308
Yinghai Luda7822e2011-05-12 17:11:37 -07001309 /*
1310 * io port are tight, don't try extra
1311 * or if reach the limit, don't want to try more
1312 */
1313 failed_type &= type_mask;
1314 if ((failed_type == IORESOURCE_IO) || (tried_times >= pci_try_num)) {
Yinghai Lubffc56d2012-01-21 02:08:30 -08001315 free_list(&fail_head);
Yinghai Luda7822e2011-05-12 17:11:37 -07001316 goto enable_and_dump;
1317 }
1318
1319 printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
1320 tried_times + 1);
1321
1322 /* third times and later will not check if it is leaf */
1323 if ((tried_times + 1) > 2)
1324 rel_type = whole_subtree;
1325
1326 /*
1327 * Try to release leaf bridge's resources that doesn't fit resource of
1328 * child device under that bridge
1329 */
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001330 list_for_each_entry(fail_res, &fail_head, list) {
1331 bus = fail_res->dev->bus;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001332 pci_bus_release_bridge_resources(bus,
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001333 fail_res->flags & type_mask,
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001334 rel_type);
Yinghai Luda7822e2011-05-12 17:11:37 -07001335 }
1336 /* restore size and flags */
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001337 list_for_each_entry(fail_res, &fail_head, list) {
1338 struct resource *res = fail_res->res;
Yinghai Luda7822e2011-05-12 17:11:37 -07001339
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001340 res->start = fail_res->start;
1341 res->end = fail_res->end;
1342 res->flags = fail_res->flags;
1343 if (fail_res->dev->subordinate)
Yinghai Luda7822e2011-05-12 17:11:37 -07001344 res->flags = 0;
Yinghai Luda7822e2011-05-12 17:11:37 -07001345 }
Yinghai Lubffc56d2012-01-21 02:08:30 -08001346 free_list(&fail_head);
Yinghai Luda7822e2011-05-12 17:11:37 -07001347
1348 goto again;
1349
1350enable_and_dump:
1351 /* Depth last, update the hardware. */
1352 list_for_each_entry(bus, &pci_root_buses, node)
1353 pci_enable_bridges(bus);
Yinghai Lu76fbc262008-06-23 20:33:06 +02001354
1355 /* dump the resource on buses */
Yinghai Luda7822e2011-05-12 17:11:37 -07001356 list_for_each_entry(bus, &pci_root_buses, node)
Yinghai Lu76fbc262008-06-23 20:33:06 +02001357 pci_bus_dump_resources(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358}
Yinghai Lu6841ec62010-01-22 01:02:25 -08001359
1360void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
1361{
1362 struct pci_bus *parent = bridge->subordinate;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001363 LIST_HEAD(add_list); /* list of resources that
Yinghai Lu8424d752012-01-21 02:08:21 -08001364 want additional resources */
Yinghai Lu32180e42010-01-22 01:02:27 -08001365 int tried_times = 0;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001366 LIST_HEAD(fail_head);
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001367 struct pci_dev_resource *fail_res;
Yinghai Lu6841ec62010-01-22 01:02:25 -08001368 int retval;
Yinghai Lu32180e42010-01-22 01:02:27 -08001369 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1370 IORESOURCE_PREFETCH;
Yinghai Lu6841ec62010-01-22 01:02:25 -08001371
Yinghai Lu32180e42010-01-22 01:02:27 -08001372again:
Yinghai Lu8424d752012-01-21 02:08:21 -08001373 __pci_bus_size_bridges(parent, &add_list);
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001374 __pci_bridge_assign_resources(bridge, &add_list, &fail_head);
1375 BUG_ON(!list_empty(&add_list));
Yinghai Lu32180e42010-01-22 01:02:27 -08001376 tried_times++;
1377
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001378 if (list_empty(&fail_head))
Yinghai Lu3f579c32010-05-21 14:35:06 -07001379 goto enable_all;
Yinghai Lu32180e42010-01-22 01:02:27 -08001380
1381 if (tried_times >= 2) {
1382 /* still fail, don't need to try more */
Yinghai Lubffc56d2012-01-21 02:08:30 -08001383 free_list(&fail_head);
Yinghai Lu3f579c32010-05-21 14:35:06 -07001384 goto enable_all;
Yinghai Lu32180e42010-01-22 01:02:27 -08001385 }
1386
1387 printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
1388 tried_times + 1);
1389
1390 /*
1391 * Try to release leaf bridge's resources that doesn't fit resource of
1392 * child device under that bridge
1393 */
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001394 list_for_each_entry(fail_res, &fail_head, list) {
1395 struct pci_bus *bus = fail_res->dev->bus;
1396 unsigned long flags = fail_res->flags;
Yinghai Lu32180e42010-01-22 01:02:27 -08001397
1398 pci_bus_release_bridge_resources(bus, flags & type_mask,
1399 whole_subtree);
Yinghai Lu32180e42010-01-22 01:02:27 -08001400 }
1401 /* restore size and flags */
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001402 list_for_each_entry(fail_res, &fail_head, list) {
1403 struct resource *res = fail_res->res;
Yinghai Lu32180e42010-01-22 01:02:27 -08001404
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001405 res->start = fail_res->start;
1406 res->end = fail_res->end;
1407 res->flags = fail_res->flags;
1408 if (fail_res->dev->subordinate)
Yinghai Lu32180e42010-01-22 01:02:27 -08001409 res->flags = 0;
Yinghai Lu32180e42010-01-22 01:02:27 -08001410 }
Yinghai Lubffc56d2012-01-21 02:08:30 -08001411 free_list(&fail_head);
Yinghai Lu32180e42010-01-22 01:02:27 -08001412
1413 goto again;
Yinghai Lu3f579c32010-05-21 14:35:06 -07001414
1415enable_all:
1416 retval = pci_reenable_device(bridge);
1417 pci_set_master(bridge);
1418 pci_enable_bridges(parent);
Yinghai Lu6841ec62010-01-22 01:02:25 -08001419}
1420EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
Yinghai Lu9b030882012-01-21 02:08:23 -08001421
1422#ifdef CONFIG_HOTPLUG
1423/**
1424 * pci_rescan_bus - scan a PCI bus for devices.
1425 * @bus: PCI bus to scan
1426 *
1427 * Scan a PCI bus and child buses for new devices, adds them,
1428 * and enables them.
1429 *
1430 * Returns the max number of subordinate bus discovered.
1431 */
1432unsigned int __ref pci_rescan_bus(struct pci_bus *bus)
1433{
1434 unsigned int max;
1435 struct pci_dev *dev;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001436 LIST_HEAD(add_list); /* list of resources that
Yinghai Lu9b030882012-01-21 02:08:23 -08001437 want additional resources */
1438
1439 max = pci_scan_child_bus(bus);
1440
Yinghai Lu9b030882012-01-21 02:08:23 -08001441 down_read(&pci_bus_sem);
1442 list_for_each_entry(dev, &bus->devices, bus_list)
1443 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
1444 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
1445 if (dev->subordinate)
1446 __pci_bus_size_bridges(dev->subordinate,
1447 &add_list);
1448 up_read(&pci_bus_sem);
1449 __pci_bus_assign_resources(bus, &add_list, NULL);
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001450 BUG_ON(!list_empty(&add_list));
Yinghai Lu9b030882012-01-21 02:08:23 -08001451
1452 pci_enable_bridges(bus);
1453 pci_bus_add_devices(bus);
1454
1455 return max;
1456}
1457EXPORT_SYMBOL_GPL(pci_rescan_bus);
1458#endif