blob: c845d18bb12652e6b55283272b2f73d5d19bbfa7 [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) {
121 dev_printk(KERN_DEBUG, &dev_res->dev->dev,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800122 "%pR get_res_add_size add_size %llx\n",
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800123 dev_res->res,
124 (unsigned long long)dev_res->add_size);
125 return dev_res->add_size;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800126 }
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800127 }
Yinghai Lu1c372352012-01-21 02:08:19 -0800128
129 return 0;
130}
131
Yinghai Lu78c3b322012-01-21 02:08:25 -0800132/* Sort resources by alignment */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800133static void pdev_sort_resources(struct pci_dev *dev, struct list_head *head)
Yinghai Lu78c3b322012-01-21 02:08:25 -0800134{
135 int i;
136
137 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
138 struct resource *r;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800139 struct pci_dev_resource *dev_res, *tmp;
Yinghai Lu78c3b322012-01-21 02:08:25 -0800140 resource_size_t r_align;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800141 struct list_head *n;
Yinghai Lu78c3b322012-01-21 02:08:25 -0800142
143 r = &dev->resource[i];
144
145 if (r->flags & IORESOURCE_PCI_FIXED)
146 continue;
147
148 if (!(r->flags) || r->parent)
149 continue;
150
151 r_align = pci_resource_alignment(dev, r);
152 if (!r_align) {
153 dev_warn(&dev->dev, "BAR %d: %pR has bogus alignment\n",
154 i, r);
155 continue;
156 }
Yinghai Lu78c3b322012-01-21 02:08:25 -0800157
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800158 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
159 if (!tmp)
160 panic("pdev_sort_resources(): "
161 "kmalloc() failed!\n");
162 tmp->res = r;
163 tmp->dev = dev;
164
165 /* fallback is smallest one or list is empty*/
166 n = head;
167 list_for_each_entry(dev_res, head, list) {
168 resource_size_t align;
169
170 align = pci_resource_alignment(dev_res->dev,
171 dev_res->res);
Yinghai Lu78c3b322012-01-21 02:08:25 -0800172
173 if (r_align > align) {
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800174 n = &dev_res->list;
Yinghai Lu78c3b322012-01-21 02:08:25 -0800175 break;
176 }
177 }
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800178 /* Insert it just before n*/
179 list_add_tail(&tmp->list, n);
Yinghai Lu78c3b322012-01-21 02:08:25 -0800180 }
181}
182
Yinghai Lu6841ec62010-01-22 01:02:25 -0800183static void __dev_sort_resources(struct pci_dev *dev,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800184 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Yinghai Lu6841ec62010-01-22 01:02:25 -0800186 u16 class = dev->class >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Yinghai Lu6841ec62010-01-22 01:02:25 -0800188 /* Don't touch classless devices or host bridges or ioapics. */
189 if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST)
190 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Yinghai Lu6841ec62010-01-22 01:02:25 -0800192 /* Don't touch ioapic devices already enabled by firmware */
193 if (class == PCI_CLASS_SYSTEM_PIC) {
194 u16 command;
195 pci_read_config_word(dev, PCI_COMMAND, &command);
196 if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
197 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
199
Yinghai Lu6841ec62010-01-22 01:02:25 -0800200 pdev_sort_resources(dev, head);
201}
202
Ram Paifc075e12011-02-14 17:43:19 -0800203static inline void reset_resource(struct resource *res)
204{
205 res->start = 0;
206 res->end = 0;
207 res->flags = 0;
208}
209
Ram Paic8adf9a2011-02-14 17:43:20 -0800210/**
Ram Pai9e8bf932011-07-25 13:08:42 -0700211 * reassign_resources_sorted() - satisfy any additional resource requests
Ram Paic8adf9a2011-02-14 17:43:20 -0800212 *
Ram Pai9e8bf932011-07-25 13:08:42 -0700213 * @realloc_head : head of the list tracking requests requiring additional
Ram Paic8adf9a2011-02-14 17:43:20 -0800214 * resources
215 * @head : head of the list tracking requests with allocated
216 * resources
217 *
Ram Pai9e8bf932011-07-25 13:08:42 -0700218 * Walk through each element of the realloc_head and try to procure
Ram Paic8adf9a2011-02-14 17:43:20 -0800219 * additional resources for the element, provided the element
220 * is in the head list.
221 */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800222static void reassign_resources_sorted(struct list_head *realloc_head,
223 struct list_head *head)
Ram Paic8adf9a2011-02-14 17:43:20 -0800224{
225 struct resource *res;
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800226 struct pci_dev_resource *add_res, *tmp;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800227 struct pci_dev_resource *dev_res;
Ram Paic8adf9a2011-02-14 17:43:20 -0800228 resource_size_t add_size;
229 int idx;
230
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800231 list_for_each_entry_safe(add_res, tmp, realloc_head, list) {
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800232 bool found_match = false;
233
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800234 res = add_res->res;
Ram Paic8adf9a2011-02-14 17:43:20 -0800235 /* skip resource that has been reset */
236 if (!res->flags)
237 goto out;
238
239 /* skip this resource if not found in head list */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800240 list_for_each_entry(dev_res, head, list) {
241 if (dev_res->res == res) {
242 found_match = true;
243 break;
244 }
Ram Paic8adf9a2011-02-14 17:43:20 -0800245 }
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800246 if (!found_match)/* just skip */
247 continue;
Ram Paic8adf9a2011-02-14 17:43:20 -0800248
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800249 idx = res - &add_res->dev->resource[0];
250 add_size = add_res->add_size;
Ram Pai2bbc6942011-07-25 13:08:39 -0700251 if (!resource_size(res)) {
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800252 res->start = add_res->start;
Ram Pai2bbc6942011-07-25 13:08:39 -0700253 res->end = res->start + add_size - 1;
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800254 if (pci_assign_resource(add_res->dev, idx))
Ram Paic8adf9a2011-02-14 17:43:20 -0800255 reset_resource(res);
Ram Pai2bbc6942011-07-25 13:08:39 -0700256 } else {
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800257 resource_size_t align = add_res->min_align;
258 res->flags |= add_res->flags &
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800259 (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN);
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800260 if (pci_reassign_resource(add_res->dev, idx,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800261 add_size, align))
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800262 dev_printk(KERN_DEBUG, &add_res->dev->dev,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800263 "failed to add optional resources res=%pR\n",
Ram Pai2bbc6942011-07-25 13:08:39 -0700264 res);
Ram Paic8adf9a2011-02-14 17:43:20 -0800265 }
266out:
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800267 list_del(&add_res->list);
268 kfree(add_res);
Ram Paic8adf9a2011-02-14 17:43:20 -0800269 }
270}
271
272/**
273 * assign_requested_resources_sorted() - satisfy resource requests
274 *
275 * @head : head of the list tracking requests for resources
276 * @failed_list : head of the list tracking requests that could
277 * not be allocated
278 *
279 * Satisfy resource requests of each element in the list. Add
280 * requests that could not satisfied to the failed_list.
281 */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800282static void assign_requested_resources_sorted(struct list_head *head,
283 struct list_head *fail_head)
Yinghai Lu6841ec62010-01-22 01:02:25 -0800284{
285 struct resource *res;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800286 struct pci_dev_resource *dev_res;
Yinghai Lu6841ec62010-01-22 01:02:25 -0800287 int idx;
288
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800289 list_for_each_entry(dev_res, head, list) {
290 res = dev_res->res;
291 idx = res - &dev_res->dev->resource[0];
292 if (resource_size(res) &&
293 pci_assign_resource(dev_res->dev, idx)) {
294 if (fail_head && !pci_is_root_bus(dev_res->dev->bus)) {
Yinghai Lu9a928662010-02-28 15:49:39 -0800295 /*
296 * if the failed res is for ROM BAR, and it will
297 * be enabled later, don't add it to the list
298 */
299 if (!((idx == PCI_ROM_RESOURCE) &&
300 (!(res->flags & IORESOURCE_ROM_ENABLE))))
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800301 add_to_failed_list(fail_head,
302 dev_res->dev, res);
Yinghai Lu9a928662010-02-28 15:49:39 -0800303 }
Ram Paifc075e12011-02-14 17:43:19 -0800304 reset_resource(res);
Rajesh Shah542df5d2005-04-28 00:25:50 -0700305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
307}
308
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800309static void __assign_resources_sorted(struct list_head *head,
310 struct list_head *realloc_head,
311 struct list_head *fail_head)
Ram Paic8adf9a2011-02-14 17:43:20 -0800312{
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800313 /*
314 * Should not assign requested resources at first.
315 * they could be adjacent, so later reassign can not reallocate
316 * them one by one in parent resource window.
317 * Try to assign requested + add_size at begining
318 * if could do that, could get out early.
319 * if could not do that, we still try to assign requested at first,
320 * then try to reassign add_size for some resources.
321 */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800322 LIST_HEAD(save_head);
323 LIST_HEAD(local_fail_head);
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800324 struct pci_dev_resource *save_res;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800325 struct pci_dev_resource *dev_res;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800326
327 /* Check if optional add_size is there */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800328 if (!realloc_head || list_empty(realloc_head))
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800329 goto requested_and_reassign;
330
331 /* Save original start, end, flags etc at first */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800332 list_for_each_entry(dev_res, head, list) {
333 if (add_to_list(&save_head, dev_res->dev, dev_res->res, 0, 0)) {
Yinghai Lubffc56d2012-01-21 02:08:30 -0800334 free_list(&save_head);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800335 goto requested_and_reassign;
336 }
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800337 }
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800338
339 /* Update res in head list with add_size in realloc_head list */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800340 list_for_each_entry(dev_res, head, list)
341 dev_res->res->end += get_res_add_size(realloc_head,
342 dev_res->res);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800343
344 /* Try updated head list with add_size added */
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800345 assign_requested_resources_sorted(head, &local_fail_head);
346
347 /* all assigned with add_size ? */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800348 if (list_empty(&local_fail_head)) {
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800349 /* Remove head list from realloc_head list */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800350 list_for_each_entry(dev_res, head, list)
351 remove_from_list(realloc_head, dev_res->res);
Yinghai Lubffc56d2012-01-21 02:08:30 -0800352 free_list(&save_head);
353 free_list(head);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800354 return;
355 }
356
Yinghai Lubffc56d2012-01-21 02:08:30 -0800357 free_list(&local_fail_head);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800358 /* Release assigned resource */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800359 list_for_each_entry(dev_res, head, list)
360 if (dev_res->res->parent)
361 release_resource(dev_res->res);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800362 /* Restore start/end/flags from saved list */
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800363 list_for_each_entry(save_res, &save_head, list) {
364 struct resource *res = save_res->res;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800365
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800366 res->start = save_res->start;
367 res->end = save_res->end;
368 res->flags = save_res->flags;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800369 }
Yinghai Lubffc56d2012-01-21 02:08:30 -0800370 free_list(&save_head);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800371
372requested_and_reassign:
Ram Paic8adf9a2011-02-14 17:43:20 -0800373 /* Satisfy the must-have resource requests */
374 assign_requested_resources_sorted(head, fail_head);
375
Ram Pai0a2daa12011-07-25 13:08:41 -0700376 /* Try to satisfy any additional optional resource
Ram Paic8adf9a2011-02-14 17:43:20 -0800377 requests */
Ram Pai9e8bf932011-07-25 13:08:42 -0700378 if (realloc_head)
379 reassign_resources_sorted(realloc_head, head);
Yinghai Lubffc56d2012-01-21 02:08:30 -0800380 free_list(head);
Ram Paic8adf9a2011-02-14 17:43:20 -0800381}
382
Yinghai Lu6841ec62010-01-22 01:02:25 -0800383static void pdev_assign_resources_sorted(struct pci_dev *dev,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800384 struct list_head *add_head,
385 struct list_head *fail_head)
Yinghai Lu6841ec62010-01-22 01:02:25 -0800386{
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800387 LIST_HEAD(head);
Yinghai Lu6841ec62010-01-22 01:02:25 -0800388
Yinghai Lu6841ec62010-01-22 01:02:25 -0800389 __dev_sort_resources(dev, &head);
Yinghai Lu8424d752012-01-21 02:08:21 -0800390 __assign_resources_sorted(&head, add_head, fail_head);
Yinghai Lu6841ec62010-01-22 01:02:25 -0800391
392}
393
394static void pbus_assign_resources_sorted(const struct pci_bus *bus,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800395 struct list_head *realloc_head,
396 struct list_head *fail_head)
Yinghai Lu6841ec62010-01-22 01:02:25 -0800397{
398 struct pci_dev *dev;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800399 LIST_HEAD(head);
Yinghai Lu6841ec62010-01-22 01:02:25 -0800400
Yinghai Lu6841ec62010-01-22 01:02:25 -0800401 list_for_each_entry(dev, &bus->devices, bus_list)
402 __dev_sort_resources(dev, &head);
403
Ram Pai9e8bf932011-07-25 13:08:42 -0700404 __assign_resources_sorted(&head, realloc_head, fail_head);
Yinghai Lu6841ec62010-01-22 01:02:25 -0800405}
406
Dominik Brodowskib3743fa2005-09-09 13:03:23 -0700407void pci_setup_cardbus(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
409 struct pci_dev *bridge = bus->self;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600410 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 struct pci_bus_region region;
412
Bjorn Helgaas865df572009-11-04 10:32:57 -0700413 dev_info(&bridge->dev, "CardBus bridge to [bus %02x-%02x]\n",
414 bus->secondary, bus->subordinate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600416 res = bus->resource[0];
417 pcibios_resource_to_bus(bridge, &region, res);
418 if (res->flags & IORESOURCE_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 /*
420 * The IO resource is allocated a range twice as large as it
421 * would normally need. This allows us to set both IO regs.
422 */
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600423 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
425 region.start);
426 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
427 region.end);
428 }
429
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600430 res = bus->resource[1];
431 pcibios_resource_to_bus(bridge, &region, res);
432 if (res->flags & IORESOURCE_IO) {
433 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
435 region.start);
436 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
437 region.end);
438 }
439
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600440 res = bus->resource[2];
441 pcibios_resource_to_bus(bridge, &region, res);
442 if (res->flags & IORESOURCE_MEM) {
443 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
445 region.start);
446 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
447 region.end);
448 }
449
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600450 res = bus->resource[3];
451 pcibios_resource_to_bus(bridge, &region, res);
452 if (res->flags & IORESOURCE_MEM) {
453 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
455 region.start);
456 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
457 region.end);
458 }
459}
Dominik Brodowskib3743fa2005-09-09 13:03:23 -0700460EXPORT_SYMBOL(pci_setup_cardbus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462/* Initialize bridges with base/limit values we have collected.
463 PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
464 requires that if there is no I/O ports or memory behind the
465 bridge, corresponding range must be turned off by writing base
466 value greater than limit to the bridge's base/limit registers.
467
468 Note: care must be taken when updating I/O base/limit registers
469 of bridges which support 32-bit I/O. This update requires two
470 config space writes, so it's quite possible that an I/O window of
471 the bridge will have some undesirable address (e.g. 0) after the
472 first write. Ditto 64-bit prefetchable MMIO. */
Yinghai Lu7cc59972009-12-22 15:02:21 -0800473static void pci_setup_bridge_io(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
475 struct pci_dev *bridge = bus->self;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600476 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 struct pci_bus_region region;
Yinghai Lu7cc59972009-12-22 15:02:21 -0800478 u32 l, io_upper16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 /* Set up the top and bottom of the PCI I/O segment for this bus. */
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600481 res = bus->resource[0];
482 pcibios_resource_to_bus(bridge, &region, res);
483 if (res->flags & IORESOURCE_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 pci_read_config_dword(bridge, PCI_IO_BASE, &l);
485 l &= 0xffff0000;
486 l |= (region.start >> 8) & 0x00f0;
487 l |= region.end & 0xf000;
488 /* Set up upper 16 bits of I/O base/limit. */
489 io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600490 dev_info(&bridge->dev, " bridge window %pR\n", res);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800491 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 /* Clear upper 16 bits of I/O base/limit. */
493 io_upper16 = 0;
494 l = 0x00f0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
496 /* Temporarily disable the I/O range before updating PCI_IO_BASE. */
497 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
498 /* Update lower 16 bits of I/O base/limit. */
499 pci_write_config_dword(bridge, PCI_IO_BASE, l);
500 /* Update upper 16 bits of I/O base/limit. */
501 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800502}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Yinghai Lu7cc59972009-12-22 15:02:21 -0800504static void pci_setup_bridge_mmio(struct pci_bus *bus)
505{
506 struct pci_dev *bridge = bus->self;
507 struct resource *res;
508 struct pci_bus_region region;
509 u32 l;
510
511 /* Set up the top and bottom of the PCI Memory segment for this bus. */
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600512 res = bus->resource[1];
513 pcibios_resource_to_bus(bridge, &region, res);
514 if (res->flags & IORESOURCE_MEM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 l = (region.start >> 16) & 0xfff0;
516 l |= region.end & 0xfff00000;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600517 dev_info(&bridge->dev, " bridge window %pR\n", res);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800518 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 l = 0x0000fff0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
521 pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800522}
523
524static void pci_setup_bridge_mmio_pref(struct pci_bus *bus)
525{
526 struct pci_dev *bridge = bus->self;
527 struct resource *res;
528 struct pci_bus_region region;
529 u32 l, bu, lu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 /* Clear out the upper 32 bits of PREF limit.
532 If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
533 disables PREF range, which is ok. */
534 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
535
536 /* Set up PREF base/limit. */
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100537 bu = lu = 0;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600538 res = bus->resource[2];
539 pcibios_resource_to_bus(bridge, &region, res);
540 if (res->flags & IORESOURCE_PREFETCH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 l = (region.start >> 16) & 0xfff0;
542 l |= region.end & 0xfff00000;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600543 if (res->flags & IORESOURCE_MEM_64) {
Yinghai Lu1f82de12009-04-23 20:48:32 -0700544 bu = upper_32_bits(region.start);
545 lu = upper_32_bits(region.end);
Yinghai Lu1f82de12009-04-23 20:48:32 -0700546 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600547 dev_info(&bridge->dev, " bridge window %pR\n", res);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800548 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 l = 0x0000fff0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
551 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
552
Alex Williamson59353ea2009-11-30 14:51:44 -0700553 /* Set the upper 32 bits of PREF base & limit. */
554 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
555 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800556}
557
558static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type)
559{
560 struct pci_dev *bridge = bus->self;
561
Yinghai Lu7cc59972009-12-22 15:02:21 -0800562 dev_info(&bridge->dev, "PCI bridge to [bus %02x-%02x]\n",
563 bus->secondary, bus->subordinate);
564
565 if (type & IORESOURCE_IO)
566 pci_setup_bridge_io(bus);
567
568 if (type & IORESOURCE_MEM)
569 pci_setup_bridge_mmio(bus);
570
571 if (type & IORESOURCE_PREFETCH)
572 pci_setup_bridge_mmio_pref(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
575}
576
Benjamin Herrenschmidte2444272011-09-11 14:08:38 -0300577void pci_setup_bridge(struct pci_bus *bus)
Yinghai Lu7cc59972009-12-22 15:02:21 -0800578{
579 unsigned long type = IORESOURCE_IO | IORESOURCE_MEM |
580 IORESOURCE_PREFETCH;
581
582 __pci_setup_bridge(bus, type);
583}
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585/* Check whether the bridge supports optional I/O and
586 prefetchable memory ranges. If not, the respective
587 base/limit registers must be read-only and read as 0. */
Sam Ravnborg96bde062007-03-26 21:53:30 -0800588static void pci_bridge_check_ranges(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
590 u16 io;
591 u32 pmem;
592 struct pci_dev *bridge = bus->self;
593 struct resource *b_res;
594
595 b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
596 b_res[1].flags |= IORESOURCE_MEM;
597
598 pci_read_config_word(bridge, PCI_IO_BASE, &io);
599 if (!io) {
600 pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0);
601 pci_read_config_word(bridge, PCI_IO_BASE, &io);
602 pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
603 }
604 if (io)
605 b_res[0].flags |= IORESOURCE_IO;
606 /* DECchip 21050 pass 2 errata: the bridge may miss an address
607 disconnect boundary by one PCI data phase.
608 Workaround: do not use prefetching on this device. */
609 if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001)
610 return;
611 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
612 if (!pmem) {
613 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE,
614 0xfff0fff0);
615 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
616 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0);
617 }
Yinghai Lu1f82de12009-04-23 20:48:32 -0700618 if (pmem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
Yinghai Lu99586102010-01-22 01:02:28 -0800620 if ((pmem & PCI_PREF_RANGE_TYPE_MASK) ==
621 PCI_PREF_RANGE_TYPE_64) {
Yinghai Lu1f82de12009-04-23 20:48:32 -0700622 b_res[2].flags |= IORESOURCE_MEM_64;
Yinghai Lu99586102010-01-22 01:02:28 -0800623 b_res[2].flags |= PCI_PREF_RANGE_TYPE_64;
624 }
Yinghai Lu1f82de12009-04-23 20:48:32 -0700625 }
626
627 /* double check if bridge does support 64 bit pref */
628 if (b_res[2].flags & IORESOURCE_MEM_64) {
629 u32 mem_base_hi, tmp;
630 pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32,
631 &mem_base_hi);
632 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
633 0xffffffff);
634 pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp);
635 if (!tmp)
636 b_res[2].flags &= ~IORESOURCE_MEM_64;
637 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
638 mem_base_hi);
639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
642/* Helper function for sizing routines: find first available
643 bus resource of a given type. Note: we intentionally skip
644 the bus resources which have already been assigned (that is,
645 have non-NULL parent resource). */
Sam Ravnborg96bde062007-03-26 21:53:30 -0800646static struct resource *find_free_bus_resource(struct pci_bus *bus, unsigned long type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
648 int i;
649 struct resource *r;
650 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
651 IORESOURCE_PREFETCH;
652
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -0700653 pci_bus_for_each_resource(bus, r, i) {
Ivan Kokshaysky299de032005-06-15 18:59:27 +0400654 if (r == &ioport_resource || r == &iomem_resource)
655 continue;
Jesse Barnes55a10982009-10-27 09:39:18 -0700656 if (r && (r->flags & type_mask) == type && !r->parent)
657 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
659 return NULL;
660}
661
Ram Pai13583b12011-02-14 17:43:17 -0800662static resource_size_t calculate_iosize(resource_size_t size,
663 resource_size_t min_size,
664 resource_size_t size1,
665 resource_size_t old_size,
666 resource_size_t align)
667{
668 if (size < min_size)
669 size = min_size;
670 if (old_size == 1 )
671 old_size = 0;
672 /* To be fixed in 2.5: we should have sort of HAVE_ISA
673 flag in the struct pci_bus. */
674#if defined(CONFIG_ISA) || defined(CONFIG_EISA)
675 size = (size & 0xff) + ((size & ~0xffUL) << 2);
676#endif
677 size = ALIGN(size + size1, align);
678 if (size < old_size)
679 size = old_size;
680 return size;
681}
682
683static resource_size_t calculate_memsize(resource_size_t size,
684 resource_size_t min_size,
685 resource_size_t size1,
686 resource_size_t old_size,
687 resource_size_t align)
688{
689 if (size < min_size)
690 size = min_size;
691 if (old_size == 1 )
692 old_size = 0;
693 if (size < old_size)
694 size = old_size;
695 size = ALIGN(size + size1, align);
696 return size;
697}
698
Ram Paic8adf9a2011-02-14 17:43:20 -0800699/**
700 * pbus_size_io() - size the io window of a given bus
701 *
702 * @bus : the bus
703 * @min_size : the minimum io window that must to be allocated
704 * @add_size : additional optional io window
Ram Pai9e8bf932011-07-25 13:08:42 -0700705 * @realloc_head : track the additional io window on this list
Ram Paic8adf9a2011-02-14 17:43:20 -0800706 *
707 * Sizing the IO windows of the PCI-PCI bridge is trivial,
708 * since these windows have 4K granularity and the IO ranges
709 * of non-bridge PCI devices are limited to 256 bytes.
710 * We must be careful with the ISA aliasing though.
711 */
712static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800713 resource_size_t add_size, struct list_head *realloc_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
715 struct pci_dev *dev;
716 struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO);
Ram Paic8adf9a2011-02-14 17:43:20 -0800717 unsigned long size = 0, size0 = 0, size1 = 0;
Yinghai Lube768912011-07-25 13:08:38 -0700718 resource_size_t children_add_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 if (!b_res)
721 return;
722
723 list_for_each_entry(dev, &bus->devices, bus_list) {
724 int i;
725
726 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
727 struct resource *r = &dev->resource[i];
728 unsigned long r_size;
729
730 if (r->parent || !(r->flags & IORESOURCE_IO))
731 continue;
Zhao, Yu022edd82008-10-13 19:24:28 +0800732 r_size = resource_size(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 if (r_size < 0x400)
735 /* Might be re-aligned for ISA */
736 size += r_size;
737 else
738 size1 += r_size;
Yinghai Lube768912011-07-25 13:08:38 -0700739
Ram Pai9e8bf932011-07-25 13:08:42 -0700740 if (realloc_head)
741 children_add_size += get_res_add_size(realloc_head, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
743 }
Ram Paic8adf9a2011-02-14 17:43:20 -0800744 size0 = calculate_iosize(size, min_size, size1,
Ram Pai13583b12011-02-14 17:43:17 -0800745 resource_size(b_res), 4096);
Yinghai Lube768912011-07-25 13:08:38 -0700746 if (children_add_size > add_size)
747 add_size = children_add_size;
Ram Pai9e8bf932011-07-25 13:08:42 -0700748 size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
Yinghai Lua4ac9fe2012-01-21 02:08:17 -0800749 calculate_iosize(size, min_size, add_size + size1,
Ram Paic8adf9a2011-02-14 17:43:20 -0800750 resource_size(b_res), 4096);
751 if (!size0 && !size1) {
Bjorn Helgaas865df572009-11-04 10:32:57 -0700752 if (b_res->start || b_res->end)
753 dev_info(&bus->self->dev, "disabling bridge window "
754 "%pR to [bus %02x-%02x] (unused)\n", b_res,
755 bus->secondary, bus->subordinate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 b_res->flags = 0;
757 return;
758 }
759 /* Alignment of the IO window is always 4K */
760 b_res->start = 4096;
Ram Paic8adf9a2011-02-14 17:43:20 -0800761 b_res->end = b_res->start + size0 - 1;
Ivan Kokshaysky88452562008-03-30 19:50:14 +0400762 b_res->flags |= IORESOURCE_STARTALIGN;
Ram Pai9e8bf932011-07-25 13:08:42 -0700763 if (size1 > size0 && realloc_head)
764 add_to_list(realloc_head, bus->self, b_res, size1-size0, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
Ram Paic8adf9a2011-02-14 17:43:20 -0800767/**
768 * pbus_size_mem() - size the memory window of a given bus
769 *
770 * @bus : the bus
771 * @min_size : the minimum memory window that must to be allocated
772 * @add_size : additional optional memory window
Ram Pai9e8bf932011-07-25 13:08:42 -0700773 * @realloc_head : track the additional memory window on this list
Ram Paic8adf9a2011-02-14 17:43:20 -0800774 *
775 * Calculate the size of the bus and minimal alignment which
776 * guarantees that all child resources fit in this size.
777 */
Eric W. Biederman28760482009-09-09 14:09:24 -0700778static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
Ram Paic8adf9a2011-02-14 17:43:20 -0800779 unsigned long type, resource_size_t min_size,
780 resource_size_t add_size,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800781 struct list_head *realloc_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
783 struct pci_dev *dev;
Ram Paic8adf9a2011-02-14 17:43:20 -0800784 resource_size_t min_align, align, size, size0, size1;
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100785 resource_size_t aligns[12]; /* Alignments from 1Mb to 2Gb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 int order, max_order;
787 struct resource *b_res = find_free_bus_resource(bus, type);
Yinghai Lu1f82de12009-04-23 20:48:32 -0700788 unsigned int mem64_mask = 0;
Yinghai Lube768912011-07-25 13:08:38 -0700789 resource_size_t children_add_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 if (!b_res)
792 return 0;
793
794 memset(aligns, 0, sizeof(aligns));
795 max_order = 0;
796 size = 0;
797
Yinghai Lu1f82de12009-04-23 20:48:32 -0700798 mem64_mask = b_res->flags & IORESOURCE_MEM_64;
799 b_res->flags &= ~IORESOURCE_MEM_64;
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 list_for_each_entry(dev, &bus->devices, bus_list) {
802 int i;
Yinghai Lu1f82de12009-04-23 20:48:32 -0700803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
805 struct resource *r = &dev->resource[i];
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100806 resource_size_t r_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 if (r->parent || (r->flags & mask) != type)
809 continue;
Zhao, Yu022edd82008-10-13 19:24:28 +0800810 r_size = resource_size(r);
Yinghai Lu2aceefc2011-07-25 13:08:40 -0700811#ifdef CONFIG_PCI_IOV
812 /* put SRIOV requested res to the optional list */
Ram Pai9e8bf932011-07-25 13:08:42 -0700813 if (realloc_head && i >= PCI_IOV_RESOURCES &&
Yinghai Lu2aceefc2011-07-25 13:08:40 -0700814 i <= PCI_IOV_RESOURCE_END) {
815 r->end = r->start - 1;
Ram Pai9e8bf932011-07-25 13:08:42 -0700816 add_to_list(realloc_head, dev, r, r_size, 0/* dont' care */);
Yinghai Lu2aceefc2011-07-25 13:08:40 -0700817 children_add_size += r_size;
818 continue;
819 }
820#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 /* For bridges size != alignment */
Chris Wright6faf17f2009-08-28 13:00:06 -0700822 align = pci_resource_alignment(dev, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 order = __ffs(align) - 20;
824 if (order > 11) {
Bjorn Helgaas865df572009-11-04 10:32:57 -0700825 dev_warn(&dev->dev, "disabling BAR %d: %pR "
826 "(bad alignment %#llx)\n", i, r,
827 (unsigned long long) align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 r->flags = 0;
829 continue;
830 }
831 size += r_size;
832 if (order < 0)
833 order = 0;
834 /* Exclude ranges with size > align from
835 calculation of the alignment. */
836 if (r_size == align)
837 aligns[order] += align;
838 if (order > max_order)
839 max_order = order;
Yinghai Lu1f82de12009-04-23 20:48:32 -0700840 mem64_mask &= r->flags & IORESOURCE_MEM_64;
Yinghai Lube768912011-07-25 13:08:38 -0700841
Ram Pai9e8bf932011-07-25 13:08:42 -0700842 if (realloc_head)
843 children_add_size += get_res_add_size(realloc_head, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 align = 0;
847 min_align = 0;
848 for (order = 0; order <= max_order; order++) {
Jeremy Fitzhardinge8308c542008-09-11 01:31:50 -0700849 resource_size_t align1 = 1;
850
851 align1 <<= (order + 20);
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 if (!align)
854 min_align = align1;
Milind Arun Choudhary6f6f8c22007-07-09 11:55:51 -0700855 else if (ALIGN(align + min_align, min_align) < align1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 min_align = align1 >> 1;
857 align += aligns[order];
858 }
Linus Torvaldsb42282e2011-04-11 10:53:11 -0700859 size0 = calculate_memsize(size, min_size, 0, resource_size(b_res), min_align);
Yinghai Lube768912011-07-25 13:08:38 -0700860 if (children_add_size > add_size)
861 add_size = children_add_size;
Ram Pai9e8bf932011-07-25 13:08:42 -0700862 size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
Yinghai Lua4ac9fe2012-01-21 02:08:17 -0800863 calculate_memsize(size, min_size, add_size,
Linus Torvaldsb42282e2011-04-11 10:53:11 -0700864 resource_size(b_res), min_align);
Ram Paic8adf9a2011-02-14 17:43:20 -0800865 if (!size0 && !size1) {
Bjorn Helgaas865df572009-11-04 10:32:57 -0700866 if (b_res->start || b_res->end)
867 dev_info(&bus->self->dev, "disabling bridge window "
868 "%pR to [bus %02x-%02x] (unused)\n", b_res,
869 bus->secondary, bus->subordinate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 b_res->flags = 0;
871 return 1;
872 }
873 b_res->start = min_align;
Ram Paic8adf9a2011-02-14 17:43:20 -0800874 b_res->end = size0 + min_align - 1;
875 b_res->flags |= IORESOURCE_STARTALIGN | mem64_mask;
Ram Pai9e8bf932011-07-25 13:08:42 -0700876 if (size1 > size0 && realloc_head)
877 add_to_list(realloc_head, bus->self, b_res, size1-size0, min_align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 return 1;
879}
880
Ram Pai0a2daa12011-07-25 13:08:41 -0700881unsigned long pci_cardbus_resource_alignment(struct resource *res)
882{
883 if (res->flags & IORESOURCE_IO)
884 return pci_cardbus_io_size;
885 if (res->flags & IORESOURCE_MEM)
886 return pci_cardbus_mem_size;
887 return 0;
888}
889
890static void pci_bus_size_cardbus(struct pci_bus *bus,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800891 struct list_head *realloc_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
893 struct pci_dev *bridge = bus->self;
894 struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
895 u16 ctrl;
896
897 /*
898 * Reserve some resources for CardBus. We reserve
899 * a fixed amount of bus space for CardBus bridges.
900 */
Linus Torvalds934b7022008-04-22 18:16:30 -0700901 b_res[0].start = 0;
Linus Torvalds934b7022008-04-22 18:16:30 -0700902 b_res[0].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
Ram Pai9e8bf932011-07-25 13:08:42 -0700903 if (realloc_head)
904 add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size, 0 /* dont care */);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Linus Torvalds934b7022008-04-22 18:16:30 -0700906 b_res[1].start = 0;
Linus Torvalds934b7022008-04-22 18:16:30 -0700907 b_res[1].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
Ram Pai9e8bf932011-07-25 13:08:42 -0700908 if (realloc_head)
909 add_to_list(realloc_head, bridge, b_res+1, pci_cardbus_io_size, 0 /* dont care */);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 /*
912 * Check whether prefetchable memory is supported
913 * by this bridge.
914 */
915 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
916 if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
917 ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
918 pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
919 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
920 }
921
922 /*
923 * If we have prefetchable memory support, allocate
924 * two regions. Otherwise, allocate one region of
925 * twice the size.
926 */
927 if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
Linus Torvalds934b7022008-04-22 18:16:30 -0700928 b_res[2].start = 0;
Linus Torvalds934b7022008-04-22 18:16:30 -0700929 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_SIZEALIGN;
Ram Pai9e8bf932011-07-25 13:08:42 -0700930 if (realloc_head)
931 add_to_list(realloc_head, bridge, b_res+2, pci_cardbus_mem_size, 0 /* dont care */);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Linus Torvalds934b7022008-04-22 18:16:30 -0700933 b_res[3].start = 0;
Linus Torvalds934b7022008-04-22 18:16:30 -0700934 b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
Ram Pai9e8bf932011-07-25 13:08:42 -0700935 if (realloc_head)
936 add_to_list(realloc_head, bridge, b_res+3, pci_cardbus_mem_size, 0 /* dont care */);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 } else {
Linus Torvalds934b7022008-04-22 18:16:30 -0700938 b_res[3].start = 0;
Linus Torvalds934b7022008-04-22 18:16:30 -0700939 b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
Ram Pai9e8bf932011-07-25 13:08:42 -0700940 if (realloc_head)
941 add_to_list(realloc_head, bridge, b_res+3, pci_cardbus_mem_size * 2, 0 /* dont care */);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
Ram Pai0a2daa12011-07-25 13:08:41 -0700943
944 /* set the size of the resource to zero, so that the resource does not
945 * get assigned during required-resource allocation cycle but gets assigned
946 * during the optional-resource allocation cycle.
947 */
948 b_res[0].start = b_res[1].start = b_res[2].start = b_res[3].start = 1;
949 b_res[0].end = b_res[1].end = b_res[2].end = b_res[3].end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950}
951
Ram Paic8adf9a2011-02-14 17:43:20 -0800952void __ref __pci_bus_size_bridges(struct pci_bus *bus,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800953 struct list_head *realloc_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
955 struct pci_dev *dev;
956 unsigned long mask, prefmask;
Ram Paic8adf9a2011-02-14 17:43:20 -0800957 resource_size_t additional_mem_size = 0, additional_io_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 list_for_each_entry(dev, &bus->devices, bus_list) {
960 struct pci_bus *b = dev->subordinate;
961 if (!b)
962 continue;
963
964 switch (dev->class >> 8) {
965 case PCI_CLASS_BRIDGE_CARDBUS:
Ram Pai9e8bf932011-07-25 13:08:42 -0700966 pci_bus_size_cardbus(b, realloc_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 break;
968
969 case PCI_CLASS_BRIDGE_PCI:
970 default:
Ram Pai9e8bf932011-07-25 13:08:42 -0700971 __pci_bus_size_bridges(b, realloc_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 break;
973 }
974 }
975
976 /* The root bus? */
977 if (!bus->self)
978 return;
979
980 switch (bus->self->class >> 8) {
981 case PCI_CLASS_BRIDGE_CARDBUS:
982 /* don't size cardbuses yet. */
983 break;
984
985 case PCI_CLASS_BRIDGE_PCI:
986 pci_bridge_check_ranges(bus);
Eric W. Biederman28760482009-09-09 14:09:24 -0700987 if (bus->self->is_hotplug_bridge) {
Ram Paic8adf9a2011-02-14 17:43:20 -0800988 additional_io_size = pci_hotplug_io_size;
989 additional_mem_size = pci_hotplug_mem_size;
Eric W. Biederman28760482009-09-09 14:09:24 -0700990 }
Ram Paic8adf9a2011-02-14 17:43:20 -0800991 /*
992 * Follow thru
993 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 default:
Yinghai Lu19aa7ee2012-01-21 02:08:24 -0800995 pbus_size_io(bus, realloc_head ? 0 : additional_io_size,
996 additional_io_size, realloc_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 /* If the bridge supports prefetchable range, size it
998 separately. If it doesn't, or its prefetchable window
999 has already been allocated by arch code, try
1000 non-prefetchable range for both types of PCI memory
1001 resources. */
1002 mask = IORESOURCE_MEM;
1003 prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001004 if (pbus_size_mem(bus, prefmask, prefmask,
1005 realloc_head ? 0 : additional_mem_size,
1006 additional_mem_size, realloc_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 mask = prefmask; /* Success, size non-prefetch only. */
Eric W. Biederman28760482009-09-09 14:09:24 -07001008 else
Ram Paic8adf9a2011-02-14 17:43:20 -08001009 additional_mem_size += additional_mem_size;
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001010 pbus_size_mem(bus, mask, IORESOURCE_MEM,
1011 realloc_head ? 0 : additional_mem_size,
1012 additional_mem_size, realloc_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 break;
1014 }
1015}
Ram Paic8adf9a2011-02-14 17:43:20 -08001016
1017void __ref pci_bus_size_bridges(struct pci_bus *bus)
1018{
1019 __pci_bus_size_bridges(bus, NULL);
1020}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021EXPORT_SYMBOL(pci_bus_size_bridges);
1022
Yinghai Lu568ddef2010-01-22 01:02:21 -08001023static void __ref __pci_bus_assign_resources(const struct pci_bus *bus,
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001024 struct list_head *realloc_head,
1025 struct list_head *fail_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
1027 struct pci_bus *b;
1028 struct pci_dev *dev;
1029
Ram Pai9e8bf932011-07-25 13:08:42 -07001030 pbus_assign_resources_sorted(bus, realloc_head, fail_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 list_for_each_entry(dev, &bus->devices, bus_list) {
1033 b = dev->subordinate;
1034 if (!b)
1035 continue;
1036
Ram Pai9e8bf932011-07-25 13:08:42 -07001037 __pci_bus_assign_resources(b, realloc_head, fail_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 switch (dev->class >> 8) {
1040 case PCI_CLASS_BRIDGE_PCI:
Yinghai Lu6841ec62010-01-22 01:02:25 -08001041 if (!pci_is_enabled(dev))
1042 pci_setup_bridge(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 break;
1044
1045 case PCI_CLASS_BRIDGE_CARDBUS:
1046 pci_setup_cardbus(b);
1047 break;
1048
1049 default:
Bjorn Helgaas80ccba12008-06-13 10:52:11 -06001050 dev_info(&dev->dev, "not setting up bridge for bus "
1051 "%04x:%02x\n", pci_domain_nr(b), b->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 break;
1053 }
1054 }
1055}
Yinghai Lu568ddef2010-01-22 01:02:21 -08001056
1057void __ref pci_bus_assign_resources(const struct pci_bus *bus)
1058{
Ram Paic8adf9a2011-02-14 17:43:20 -08001059 __pci_bus_assign_resources(bus, NULL, NULL);
Yinghai Lu568ddef2010-01-22 01:02:21 -08001060}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061EXPORT_SYMBOL(pci_bus_assign_resources);
1062
Yinghai Lu6841ec62010-01-22 01:02:25 -08001063static void __ref __pci_bridge_assign_resources(const struct pci_dev *bridge,
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001064 struct list_head *add_head,
1065 struct list_head *fail_head)
Yinghai Lu6841ec62010-01-22 01:02:25 -08001066{
1067 struct pci_bus *b;
1068
Yinghai Lu8424d752012-01-21 02:08:21 -08001069 pdev_assign_resources_sorted((struct pci_dev *)bridge,
1070 add_head, fail_head);
Yinghai Lu6841ec62010-01-22 01:02:25 -08001071
1072 b = bridge->subordinate;
1073 if (!b)
1074 return;
1075
Yinghai Lu8424d752012-01-21 02:08:21 -08001076 __pci_bus_assign_resources(b, add_head, fail_head);
Yinghai Lu6841ec62010-01-22 01:02:25 -08001077
1078 switch (bridge->class >> 8) {
1079 case PCI_CLASS_BRIDGE_PCI:
1080 pci_setup_bridge(b);
1081 break;
1082
1083 case PCI_CLASS_BRIDGE_CARDBUS:
1084 pci_setup_cardbus(b);
1085 break;
1086
1087 default:
1088 dev_info(&bridge->dev, "not setting up bridge for bus "
1089 "%04x:%02x\n", pci_domain_nr(b), b->number);
1090 break;
1091 }
1092}
Yinghai Lu5009b462010-01-22 01:02:20 -08001093static void pci_bridge_release_resources(struct pci_bus *bus,
1094 unsigned long type)
1095{
1096 int idx;
1097 bool changed = false;
1098 struct pci_dev *dev;
1099 struct resource *r;
1100 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1101 IORESOURCE_PREFETCH;
1102
1103 dev = bus->self;
1104 for (idx = PCI_BRIDGE_RESOURCES; idx <= PCI_BRIDGE_RESOURCE_END;
1105 idx++) {
1106 r = &dev->resource[idx];
1107 if ((r->flags & type_mask) != type)
1108 continue;
1109 if (!r->parent)
1110 continue;
1111 /*
1112 * if there are children under that, we should release them
1113 * all
1114 */
1115 release_child_resources(r);
1116 if (!release_resource(r)) {
1117 dev_printk(KERN_DEBUG, &dev->dev,
1118 "resource %d %pR released\n", idx, r);
1119 /* keep the old size */
1120 r->end = resource_size(r) - 1;
1121 r->start = 0;
1122 r->flags = 0;
1123 changed = true;
1124 }
1125 }
1126
1127 if (changed) {
1128 /* avoiding touch the one without PREF */
1129 if (type & IORESOURCE_PREFETCH)
1130 type = IORESOURCE_PREFETCH;
1131 __pci_setup_bridge(bus, type);
1132 }
1133}
1134
1135enum release_type {
1136 leaf_only,
1137 whole_subtree,
1138};
1139/*
1140 * try to release pci bridge resources that is from leaf bridge,
1141 * so we can allocate big new one later
1142 */
1143static void __ref pci_bus_release_bridge_resources(struct pci_bus *bus,
1144 unsigned long type,
1145 enum release_type rel_type)
1146{
1147 struct pci_dev *dev;
1148 bool is_leaf_bridge = true;
1149
1150 list_for_each_entry(dev, &bus->devices, bus_list) {
1151 struct pci_bus *b = dev->subordinate;
1152 if (!b)
1153 continue;
1154
1155 is_leaf_bridge = false;
1156
1157 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
1158 continue;
1159
1160 if (rel_type == whole_subtree)
1161 pci_bus_release_bridge_resources(b, type,
1162 whole_subtree);
1163 }
1164
1165 if (pci_is_root_bus(bus))
1166 return;
1167
1168 if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
1169 return;
1170
1171 if ((rel_type == whole_subtree) || is_leaf_bridge)
1172 pci_bridge_release_resources(bus, type);
1173}
1174
Yinghai Lu76fbc262008-06-23 20:33:06 +02001175static void pci_bus_dump_res(struct pci_bus *bus)
1176{
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -07001177 struct resource *res;
1178 int i;
Yinghai Lu76fbc262008-06-23 20:33:06 +02001179
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -07001180 pci_bus_for_each_resource(bus, res, i) {
Yinghai Lu7c9342b2009-12-22 15:02:24 -08001181 if (!res || !res->end || !res->flags)
Yinghai Lu76fbc262008-06-23 20:33:06 +02001182 continue;
1183
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001184 dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res);
Yinghai Lu76fbc262008-06-23 20:33:06 +02001185 }
1186}
1187
1188static void pci_bus_dump_resources(struct pci_bus *bus)
1189{
1190 struct pci_bus *b;
1191 struct pci_dev *dev;
1192
1193
1194 pci_bus_dump_res(bus);
1195
1196 list_for_each_entry(dev, &bus->devices, bus_list) {
1197 b = dev->subordinate;
1198 if (!b)
1199 continue;
1200
1201 pci_bus_dump_resources(b);
1202 }
1203}
1204
Yinghai Luda7822e2011-05-12 17:11:37 -07001205static int __init pci_bus_get_depth(struct pci_bus *bus)
1206{
1207 int depth = 0;
1208 struct pci_dev *dev;
1209
1210 list_for_each_entry(dev, &bus->devices, bus_list) {
1211 int ret;
1212 struct pci_bus *b = dev->subordinate;
1213 if (!b)
1214 continue;
1215
1216 ret = pci_bus_get_depth(b);
1217 if (ret + 1 > depth)
1218 depth = ret + 1;
1219 }
1220
1221 return depth;
1222}
1223static int __init pci_get_max_depth(void)
1224{
1225 int depth = 0;
1226 struct pci_bus *bus;
1227
1228 list_for_each_entry(bus, &pci_root_buses, node) {
1229 int ret;
1230
1231 ret = pci_bus_get_depth(bus);
1232 if (ret > depth)
1233 depth = ret;
1234 }
1235
1236 return depth;
1237}
1238
Ram Paif483d392011-07-07 11:19:10 -07001239
Yinghai Luda7822e2011-05-12 17:11:37 -07001240/*
1241 * first try will not touch pci bridge res
1242 * second and later try will clear small leaf bridge res
1243 * will stop till to the max deepth if can not find good one
1244 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245void __init
1246pci_assign_unassigned_resources(void)
1247{
1248 struct pci_bus *bus;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001249 LIST_HEAD(realloc_head); /* list of resources that
Ram Paic8adf9a2011-02-14 17:43:20 -08001250 want additional resources */
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001251 struct list_head *add_list = NULL;
Yinghai Luda7822e2011-05-12 17:11:37 -07001252 int tried_times = 0;
1253 enum release_type rel_type = leaf_only;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001254 LIST_HEAD(fail_head);
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001255 struct pci_dev_resource *fail_res;
Yinghai Luda7822e2011-05-12 17:11:37 -07001256 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1257 IORESOURCE_PREFETCH;
1258 unsigned long failed_type;
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001259 int pci_try_num = 1;
Yinghai Luda7822e2011-05-12 17:11:37 -07001260
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001261 /* don't realloc if asked to do so */
1262 if (pci_realloc_enabled()) {
1263 int max_depth = pci_get_max_depth();
1264
1265 pci_try_num = max_depth + 1;
1266 printk(KERN_DEBUG "PCI: max bus depth: %d pci_try_num: %d\n",
1267 max_depth, pci_try_num);
1268 }
Yinghai Luda7822e2011-05-12 17:11:37 -07001269
1270again:
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001271 /*
1272 * last try will use add_list, otherwise will try good to have as
1273 * must have, so can realloc parent bridge resource
1274 */
1275 if (tried_times + 1 == pci_try_num)
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001276 add_list = &realloc_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 /* Depth first, calculate sizes and alignments of all
1278 subordinate buses. */
Yinghai Luda7822e2011-05-12 17:11:37 -07001279 list_for_each_entry(bus, &pci_root_buses, node)
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001280 __pci_bus_size_bridges(bus, add_list);
Ram Paic8adf9a2011-02-14 17:43:20 -08001281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 /* Depth last, allocate resources and update the hardware. */
Yinghai Luda7822e2011-05-12 17:11:37 -07001283 list_for_each_entry(bus, &pci_root_buses, node)
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001284 __pci_bus_assign_resources(bus, add_list, &fail_head);
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001285 if (add_list)
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001286 BUG_ON(!list_empty(add_list));
Yinghai Luda7822e2011-05-12 17:11:37 -07001287 tried_times++;
1288
1289 /* any device complain? */
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001290 if (list_empty(&fail_head))
Yinghai Luda7822e2011-05-12 17:11:37 -07001291 goto enable_and_dump;
Ram Paif483d392011-07-07 11:19:10 -07001292
Yinghai Luda7822e2011-05-12 17:11:37 -07001293 failed_type = 0;
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001294 list_for_each_entry(fail_res, &fail_head, list)
1295 failed_type |= fail_res->flags;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001296
Yinghai Luda7822e2011-05-12 17:11:37 -07001297 /*
1298 * io port are tight, don't try extra
1299 * or if reach the limit, don't want to try more
1300 */
1301 failed_type &= type_mask;
1302 if ((failed_type == IORESOURCE_IO) || (tried_times >= pci_try_num)) {
Yinghai Lubffc56d2012-01-21 02:08:30 -08001303 free_list(&fail_head);
Yinghai Luda7822e2011-05-12 17:11:37 -07001304 goto enable_and_dump;
1305 }
1306
1307 printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
1308 tried_times + 1);
1309
1310 /* third times and later will not check if it is leaf */
1311 if ((tried_times + 1) > 2)
1312 rel_type = whole_subtree;
1313
1314 /*
1315 * Try to release leaf bridge's resources that doesn't fit resource of
1316 * child device under that bridge
1317 */
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001318 list_for_each_entry(fail_res, &fail_head, list) {
1319 bus = fail_res->dev->bus;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001320 pci_bus_release_bridge_resources(bus,
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001321 fail_res->flags & type_mask,
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001322 rel_type);
Yinghai Luda7822e2011-05-12 17:11:37 -07001323 }
1324 /* restore size and flags */
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001325 list_for_each_entry(fail_res, &fail_head, list) {
1326 struct resource *res = fail_res->res;
Yinghai Luda7822e2011-05-12 17:11:37 -07001327
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001328 res->start = fail_res->start;
1329 res->end = fail_res->end;
1330 res->flags = fail_res->flags;
1331 if (fail_res->dev->subordinate)
Yinghai Luda7822e2011-05-12 17:11:37 -07001332 res->flags = 0;
Yinghai Luda7822e2011-05-12 17:11:37 -07001333 }
Yinghai Lubffc56d2012-01-21 02:08:30 -08001334 free_list(&fail_head);
Yinghai Luda7822e2011-05-12 17:11:37 -07001335
1336 goto again;
1337
1338enable_and_dump:
1339 /* Depth last, update the hardware. */
1340 list_for_each_entry(bus, &pci_root_buses, node)
1341 pci_enable_bridges(bus);
Yinghai Lu76fbc262008-06-23 20:33:06 +02001342
1343 /* dump the resource on buses */
Yinghai Luda7822e2011-05-12 17:11:37 -07001344 list_for_each_entry(bus, &pci_root_buses, node)
Yinghai Lu76fbc262008-06-23 20:33:06 +02001345 pci_bus_dump_resources(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346}
Yinghai Lu6841ec62010-01-22 01:02:25 -08001347
1348void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
1349{
1350 struct pci_bus *parent = bridge->subordinate;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001351 LIST_HEAD(add_list); /* list of resources that
Yinghai Lu8424d752012-01-21 02:08:21 -08001352 want additional resources */
Yinghai Lu32180e42010-01-22 01:02:27 -08001353 int tried_times = 0;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001354 LIST_HEAD(fail_head);
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001355 struct pci_dev_resource *fail_res;
Yinghai Lu6841ec62010-01-22 01:02:25 -08001356 int retval;
Yinghai Lu32180e42010-01-22 01:02:27 -08001357 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1358 IORESOURCE_PREFETCH;
Yinghai Lu6841ec62010-01-22 01:02:25 -08001359
Yinghai Lu32180e42010-01-22 01:02:27 -08001360again:
Yinghai Lu8424d752012-01-21 02:08:21 -08001361 __pci_bus_size_bridges(parent, &add_list);
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001362 __pci_bridge_assign_resources(bridge, &add_list, &fail_head);
1363 BUG_ON(!list_empty(&add_list));
Yinghai Lu32180e42010-01-22 01:02:27 -08001364 tried_times++;
1365
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001366 if (list_empty(&fail_head))
Yinghai Lu3f579c32010-05-21 14:35:06 -07001367 goto enable_all;
Yinghai Lu32180e42010-01-22 01:02:27 -08001368
1369 if (tried_times >= 2) {
1370 /* still fail, don't need to try more */
Yinghai Lubffc56d2012-01-21 02:08:30 -08001371 free_list(&fail_head);
Yinghai Lu3f579c32010-05-21 14:35:06 -07001372 goto enable_all;
Yinghai Lu32180e42010-01-22 01:02:27 -08001373 }
1374
1375 printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
1376 tried_times + 1);
1377
1378 /*
1379 * Try to release leaf bridge's resources that doesn't fit resource of
1380 * child device under that bridge
1381 */
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001382 list_for_each_entry(fail_res, &fail_head, list) {
1383 struct pci_bus *bus = fail_res->dev->bus;
1384 unsigned long flags = fail_res->flags;
Yinghai Lu32180e42010-01-22 01:02:27 -08001385
1386 pci_bus_release_bridge_resources(bus, flags & type_mask,
1387 whole_subtree);
Yinghai Lu32180e42010-01-22 01:02:27 -08001388 }
1389 /* restore size and flags */
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001390 list_for_each_entry(fail_res, &fail_head, list) {
1391 struct resource *res = fail_res->res;
Yinghai Lu32180e42010-01-22 01:02:27 -08001392
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001393 res->start = fail_res->start;
1394 res->end = fail_res->end;
1395 res->flags = fail_res->flags;
1396 if (fail_res->dev->subordinate)
Yinghai Lu32180e42010-01-22 01:02:27 -08001397 res->flags = 0;
Yinghai Lu32180e42010-01-22 01:02:27 -08001398 }
Yinghai Lubffc56d2012-01-21 02:08:30 -08001399 free_list(&fail_head);
Yinghai Lu32180e42010-01-22 01:02:27 -08001400
1401 goto again;
Yinghai Lu3f579c32010-05-21 14:35:06 -07001402
1403enable_all:
1404 retval = pci_reenable_device(bridge);
1405 pci_set_master(bridge);
1406 pci_enable_bridges(parent);
Yinghai Lu6841ec62010-01-22 01:02:25 -08001407}
1408EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
Yinghai Lu9b030882012-01-21 02:08:23 -08001409
1410#ifdef CONFIG_HOTPLUG
1411/**
1412 * pci_rescan_bus - scan a PCI bus for devices.
1413 * @bus: PCI bus to scan
1414 *
1415 * Scan a PCI bus and child buses for new devices, adds them,
1416 * and enables them.
1417 *
1418 * Returns the max number of subordinate bus discovered.
1419 */
1420unsigned int __ref pci_rescan_bus(struct pci_bus *bus)
1421{
1422 unsigned int max;
1423 struct pci_dev *dev;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001424 LIST_HEAD(add_list); /* list of resources that
Yinghai Lu9b030882012-01-21 02:08:23 -08001425 want additional resources */
1426
1427 max = pci_scan_child_bus(bus);
1428
Yinghai Lu9b030882012-01-21 02:08:23 -08001429 down_read(&pci_bus_sem);
1430 list_for_each_entry(dev, &bus->devices, bus_list)
1431 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
1432 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
1433 if (dev->subordinate)
1434 __pci_bus_size_bridges(dev->subordinate,
1435 &add_list);
1436 up_read(&pci_bus_sem);
1437 __pci_bus_assign_resources(bus, &add_list, NULL);
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001438 BUG_ON(!list_empty(&add_list));
Yinghai Lu9b030882012-01-21 02:08:23 -08001439
1440 pci_enable_bridges(bus);
1441 pci_bus_add_devices(bus);
1442
1443 return max;
1444}
1445EXPORT_SYMBOL_GPL(pci_rescan_bus);
1446#endif