blob: af2a98a0ae7caf7af54207f651fadffb30d0b92a [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 Lubdc4abe2012-01-21 02:08:27 -080041#define free_list(type, head) do { \
42 struct type *dev_res, *tmp; \
43 list_for_each_entry_safe(dev_res, tmp, head, list) { \
44 list_del(&dev_res->list); \
45 kfree(dev_res); \
46 } \
Ram Pai094732a2011-02-14 17:43:18 -080047} while (0)
48
Ram Paif483d392011-07-07 11:19:10 -070049int pci_realloc_enable = 0;
50#define pci_realloc_enabled() pci_realloc_enable
51void pci_realloc(void)
52{
53 pci_realloc_enable = 1;
54}
55
Ram Paic8adf9a2011-02-14 17:43:20 -080056/**
57 * add_to_list() - add a new resource tracker to the list
58 * @head: Head of the list
59 * @dev: device corresponding to which the resource
60 * belongs
61 * @res: The resource to be tracked
62 * @add_size: additional size to be optionally added
63 * to the resource
64 */
Yinghai Lubdc4abe2012-01-21 02:08:27 -080065static int add_to_list(struct list_head *head,
Ram Paic8adf9a2011-02-14 17:43:20 -080066 struct pci_dev *dev, struct resource *res,
Ram Pai2bbc6942011-07-25 13:08:39 -070067 resource_size_t add_size, resource_size_t min_align)
Yinghai Lu568ddef2010-01-22 01:02:21 -080068{
Yinghai Lu764242a2012-01-21 02:08:28 -080069 struct pci_dev_resource *tmp;
Yinghai Lu568ddef2010-01-22 01:02:21 -080070
Yinghai Lubdc4abe2012-01-21 02:08:27 -080071 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
Yinghai Lu568ddef2010-01-22 01:02:21 -080072 if (!tmp) {
Ram Paic8adf9a2011-02-14 17:43:20 -080073 pr_warning("add_to_list: kmalloc() failed!\n");
Yinghai Luef62dfe2012-01-21 02:08:18 -080074 return -ENOMEM;
Yinghai Lu568ddef2010-01-22 01:02:21 -080075 }
76
Yinghai Lu568ddef2010-01-22 01:02:21 -080077 tmp->res = res;
78 tmp->dev = dev;
79 tmp->start = res->start;
80 tmp->end = res->end;
81 tmp->flags = res->flags;
Ram Paic8adf9a2011-02-14 17:43:20 -080082 tmp->add_size = add_size;
Ram Pai2bbc6942011-07-25 13:08:39 -070083 tmp->min_align = min_align;
Yinghai Lubdc4abe2012-01-21 02:08:27 -080084
85 list_add(&tmp->list, head);
Yinghai Luef62dfe2012-01-21 02:08:18 -080086
87 return 0;
Yinghai Lu568ddef2010-01-22 01:02:21 -080088}
89
Yinghai Lubdc4abe2012-01-21 02:08:27 -080090static void add_to_failed_list(struct list_head *head,
Ram Paic8adf9a2011-02-14 17:43:20 -080091 struct pci_dev *dev, struct resource *res)
92{
Ram Pai2bbc6942011-07-25 13:08:39 -070093 add_to_list(head, dev, res,
94 0 /* dont care */,
95 0 /* dont care */);
Ram Paic8adf9a2011-02-14 17:43:20 -080096}
97
Yinghai Lubdc4abe2012-01-21 02:08:27 -080098static void remove_from_list(struct list_head *realloc_head,
Yinghai Lu3e6e0d82012-01-21 02:08:20 -080099 struct resource *res)
100{
Yinghai Lu764242a2012-01-21 02:08:28 -0800101 struct pci_dev_resource *dev_res_x, *tmp;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800102
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800103 list_for_each_entry_safe(dev_res_x, tmp, realloc_head, list) {
104 if (dev_res_x->res == res) {
105 list_del(&dev_res_x->list);
106 kfree(dev_res_x);
107 break;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800108 }
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800109 }
110}
111
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800112static resource_size_t get_res_add_size(struct list_head *realloc_head,
Yinghai Lu1c372352012-01-21 02:08:19 -0800113 struct resource *res)
114{
Yinghai Lu764242a2012-01-21 02:08:28 -0800115 struct pci_dev_resource *dev_res_x;
Yinghai Lu1c372352012-01-21 02:08:19 -0800116
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800117 list_for_each_entry(dev_res_x, realloc_head, list) {
118 if (dev_res_x->res == res) {
119 dev_printk(KERN_DEBUG, &dev_res_x->dev->dev,
120 "%pR get_res_add_size add_size %llx\n",
121 dev_res_x->res,
122 (unsigned long long)dev_res_x->add_size);
123 return dev_res_x->add_size;
124 }
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800125 }
Yinghai Lu1c372352012-01-21 02:08:19 -0800126
127 return 0;
128}
129
Yinghai Lu78c3b322012-01-21 02:08:25 -0800130/* Sort resources by alignment */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800131static void pdev_sort_resources(struct pci_dev *dev, struct list_head *head)
Yinghai Lu78c3b322012-01-21 02:08:25 -0800132{
133 int i;
134
135 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
136 struct resource *r;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800137 struct pci_dev_resource *dev_res, *tmp;
Yinghai Lu78c3b322012-01-21 02:08:25 -0800138 resource_size_t r_align;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800139 struct list_head *n;
Yinghai Lu78c3b322012-01-21 02:08:25 -0800140
141 r = &dev->resource[i];
142
143 if (r->flags & IORESOURCE_PCI_FIXED)
144 continue;
145
146 if (!(r->flags) || r->parent)
147 continue;
148
149 r_align = pci_resource_alignment(dev, r);
150 if (!r_align) {
151 dev_warn(&dev->dev, "BAR %d: %pR has bogus alignment\n",
152 i, r);
153 continue;
154 }
Yinghai Lu78c3b322012-01-21 02:08:25 -0800155
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800156 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
157 if (!tmp)
158 panic("pdev_sort_resources(): "
159 "kmalloc() failed!\n");
160 tmp->res = r;
161 tmp->dev = dev;
162
163 /* fallback is smallest one or list is empty*/
164 n = head;
165 list_for_each_entry(dev_res, head, list) {
166 resource_size_t align;
167
168 align = pci_resource_alignment(dev_res->dev,
169 dev_res->res);
Yinghai Lu78c3b322012-01-21 02:08:25 -0800170
171 if (r_align > align) {
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800172 n = &dev_res->list;
Yinghai Lu78c3b322012-01-21 02:08:25 -0800173 break;
174 }
175 }
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800176 /* Insert it just before n*/
177 list_add_tail(&tmp->list, n);
Yinghai Lu78c3b322012-01-21 02:08:25 -0800178 }
179}
180
Yinghai Lu6841ec62010-01-22 01:02:25 -0800181static void __dev_sort_resources(struct pci_dev *dev,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800182 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Yinghai Lu6841ec62010-01-22 01:02:25 -0800184 u16 class = dev->class >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Yinghai Lu6841ec62010-01-22 01:02:25 -0800186 /* Don't touch classless devices or host bridges or ioapics. */
187 if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST)
188 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Yinghai Lu6841ec62010-01-22 01:02:25 -0800190 /* Don't touch ioapic devices already enabled by firmware */
191 if (class == PCI_CLASS_SYSTEM_PIC) {
192 u16 command;
193 pci_read_config_word(dev, PCI_COMMAND, &command);
194 if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
195 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
197
Yinghai Lu6841ec62010-01-22 01:02:25 -0800198 pdev_sort_resources(dev, head);
199}
200
Ram Paifc075e12011-02-14 17:43:19 -0800201static inline void reset_resource(struct resource *res)
202{
203 res->start = 0;
204 res->end = 0;
205 res->flags = 0;
206}
207
Ram Paic8adf9a2011-02-14 17:43:20 -0800208/**
Ram Pai9e8bf932011-07-25 13:08:42 -0700209 * reassign_resources_sorted() - satisfy any additional resource requests
Ram Paic8adf9a2011-02-14 17:43:20 -0800210 *
Ram Pai9e8bf932011-07-25 13:08:42 -0700211 * @realloc_head : head of the list tracking requests requiring additional
Ram Paic8adf9a2011-02-14 17:43:20 -0800212 * resources
213 * @head : head of the list tracking requests with allocated
214 * resources
215 *
Ram Pai9e8bf932011-07-25 13:08:42 -0700216 * Walk through each element of the realloc_head and try to procure
Ram Paic8adf9a2011-02-14 17:43:20 -0800217 * additional resources for the element, provided the element
218 * is in the head list.
219 */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800220static void reassign_resources_sorted(struct list_head *realloc_head,
221 struct list_head *head)
Ram Paic8adf9a2011-02-14 17:43:20 -0800222{
223 struct resource *res;
Yinghai Lu764242a2012-01-21 02:08:28 -0800224 struct pci_dev_resource *dev_res_x, *tmp;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800225 struct pci_dev_resource *dev_res;
Ram Paic8adf9a2011-02-14 17:43:20 -0800226 resource_size_t add_size;
227 int idx;
228
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800229 list_for_each_entry_safe(dev_res_x, tmp, realloc_head, list) {
230 bool found_match = false;
231
232 res = dev_res_x->res;
Ram Paic8adf9a2011-02-14 17:43:20 -0800233 /* skip resource that has been reset */
234 if (!res->flags)
235 goto out;
236
237 /* skip this resource if not found in head list */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800238 list_for_each_entry(dev_res, head, list) {
239 if (dev_res->res == res) {
240 found_match = true;
241 break;
242 }
Ram Paic8adf9a2011-02-14 17:43:20 -0800243 }
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800244 if (!found_match)/* just skip */
245 continue;
Ram Paic8adf9a2011-02-14 17:43:20 -0800246
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800247 idx = res - &dev_res_x->dev->resource[0];
248 add_size = dev_res_x->add_size;
Ram Pai2bbc6942011-07-25 13:08:39 -0700249 if (!resource_size(res)) {
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800250 res->start = dev_res_x->start;
Ram Pai2bbc6942011-07-25 13:08:39 -0700251 res->end = res->start + add_size - 1;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800252 if (pci_assign_resource(dev_res_x->dev, idx))
Ram Paic8adf9a2011-02-14 17:43:20 -0800253 reset_resource(res);
Ram Pai2bbc6942011-07-25 13:08:39 -0700254 } else {
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800255 resource_size_t align = dev_res_x->min_align;
256 res->flags |= dev_res_x->flags &
257 (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN);
258 if (pci_reassign_resource(dev_res_x->dev, idx,
259 add_size, align))
260 dev_printk(KERN_DEBUG, &dev_res_x->dev->dev,
261 "failed to add optional resources res=%pR\n",
Ram Pai2bbc6942011-07-25 13:08:39 -0700262 res);
Ram Paic8adf9a2011-02-14 17:43:20 -0800263 }
264out:
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800265 list_del(&dev_res_x->list);
266 kfree(dev_res_x);
Ram Paic8adf9a2011-02-14 17:43:20 -0800267 }
268}
269
270/**
271 * assign_requested_resources_sorted() - satisfy resource requests
272 *
273 * @head : head of the list tracking requests for resources
274 * @failed_list : head of the list tracking requests that could
275 * not be allocated
276 *
277 * Satisfy resource requests of each element in the list. Add
278 * requests that could not satisfied to the failed_list.
279 */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800280static void assign_requested_resources_sorted(struct list_head *head,
281 struct list_head *fail_head)
Yinghai Lu6841ec62010-01-22 01:02:25 -0800282{
283 struct resource *res;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800284 struct pci_dev_resource *dev_res;
Yinghai Lu6841ec62010-01-22 01:02:25 -0800285 int idx;
286
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800287 list_for_each_entry(dev_res, head, list) {
288 res = dev_res->res;
289 idx = res - &dev_res->dev->resource[0];
290 if (resource_size(res) &&
291 pci_assign_resource(dev_res->dev, idx)) {
292 if (fail_head && !pci_is_root_bus(dev_res->dev->bus)) {
Yinghai Lu9a928662010-02-28 15:49:39 -0800293 /*
294 * if the failed res is for ROM BAR, and it will
295 * be enabled later, don't add it to the list
296 */
297 if (!((idx == PCI_ROM_RESOURCE) &&
298 (!(res->flags & IORESOURCE_ROM_ENABLE))))
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800299 add_to_failed_list(fail_head,
300 dev_res->dev, res);
Yinghai Lu9a928662010-02-28 15:49:39 -0800301 }
Ram Paifc075e12011-02-14 17:43:19 -0800302 reset_resource(res);
Rajesh Shah542df5d2005-04-28 00:25:50 -0700303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
305}
306
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800307static void __assign_resources_sorted(struct list_head *head,
308 struct list_head *realloc_head,
309 struct list_head *fail_head)
Ram Paic8adf9a2011-02-14 17:43:20 -0800310{
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800311 /*
312 * Should not assign requested resources at first.
313 * they could be adjacent, so later reassign can not reallocate
314 * them one by one in parent resource window.
315 * Try to assign requested + add_size at begining
316 * if could do that, could get out early.
317 * if could not do that, we still try to assign requested at first,
318 * then try to reassign add_size for some resources.
319 */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800320 LIST_HEAD(save_head);
321 LIST_HEAD(local_fail_head);
Yinghai Lu764242a2012-01-21 02:08:28 -0800322 struct pci_dev_resource *dev_res_x;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800323 struct pci_dev_resource *dev_res;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800324
325 /* Check if optional add_size is there */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800326 if (!realloc_head || list_empty(realloc_head))
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800327 goto requested_and_reassign;
328
329 /* Save original start, end, flags etc at first */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800330 list_for_each_entry(dev_res, head, list) {
331 if (add_to_list(&save_head, dev_res->dev, dev_res->res, 0, 0)) {
Yinghai Lu764242a2012-01-21 02:08:28 -0800332 free_list(pci_dev_resource, &save_head);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800333 goto requested_and_reassign;
334 }
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800335 }
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800336
337 /* Update res in head list with add_size in realloc_head list */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800338 list_for_each_entry(dev_res, head, list)
339 dev_res->res->end += get_res_add_size(realloc_head,
340 dev_res->res);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800341
342 /* Try updated head list with add_size added */
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800343 assign_requested_resources_sorted(head, &local_fail_head);
344
345 /* all assigned with add_size ? */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800346 if (list_empty(&local_fail_head)) {
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800347 /* Remove head list from realloc_head list */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800348 list_for_each_entry(dev_res, head, list)
349 remove_from_list(realloc_head, dev_res->res);
Yinghai Lu764242a2012-01-21 02:08:28 -0800350 free_list(pci_dev_resource, &save_head);
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800351 free_list(pci_dev_resource, head);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800352 return;
353 }
354
Yinghai Lu764242a2012-01-21 02:08:28 -0800355 free_list(pci_dev_resource, &local_fail_head);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800356 /* Release assigned resource */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800357 list_for_each_entry(dev_res, head, list)
358 if (dev_res->res->parent)
359 release_resource(dev_res->res);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800360 /* Restore start/end/flags from saved list */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800361 list_for_each_entry(dev_res_x, &save_head, list) {
362 struct resource *res = dev_res_x->res;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800363
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800364 res->start = dev_res_x->start;
365 res->end = dev_res_x->end;
366 res->flags = dev_res_x->flags;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800367 }
Yinghai Lu764242a2012-01-21 02:08:28 -0800368 free_list(pci_dev_resource, &save_head);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800369
370requested_and_reassign:
Ram Paic8adf9a2011-02-14 17:43:20 -0800371 /* Satisfy the must-have resource requests */
372 assign_requested_resources_sorted(head, fail_head);
373
Ram Pai0a2daa12011-07-25 13:08:41 -0700374 /* Try to satisfy any additional optional resource
Ram Paic8adf9a2011-02-14 17:43:20 -0800375 requests */
Ram Pai9e8bf932011-07-25 13:08:42 -0700376 if (realloc_head)
377 reassign_resources_sorted(realloc_head, head);
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800378 free_list(pci_dev_resource, head);
Ram Paic8adf9a2011-02-14 17:43:20 -0800379}
380
Yinghai Lu6841ec62010-01-22 01:02:25 -0800381static void pdev_assign_resources_sorted(struct pci_dev *dev,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800382 struct list_head *add_head,
383 struct list_head *fail_head)
Yinghai Lu6841ec62010-01-22 01:02:25 -0800384{
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800385 LIST_HEAD(head);
Yinghai Lu6841ec62010-01-22 01:02:25 -0800386
Yinghai Lu6841ec62010-01-22 01:02:25 -0800387 __dev_sort_resources(dev, &head);
Yinghai Lu8424d752012-01-21 02:08:21 -0800388 __assign_resources_sorted(&head, add_head, fail_head);
Yinghai Lu6841ec62010-01-22 01:02:25 -0800389
390}
391
392static void pbus_assign_resources_sorted(const struct pci_bus *bus,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800393 struct list_head *realloc_head,
394 struct list_head *fail_head)
Yinghai Lu6841ec62010-01-22 01:02:25 -0800395{
396 struct pci_dev *dev;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800397 LIST_HEAD(head);
Yinghai Lu6841ec62010-01-22 01:02:25 -0800398
Yinghai Lu6841ec62010-01-22 01:02:25 -0800399 list_for_each_entry(dev, &bus->devices, bus_list)
400 __dev_sort_resources(dev, &head);
401
Ram Pai9e8bf932011-07-25 13:08:42 -0700402 __assign_resources_sorted(&head, realloc_head, fail_head);
Yinghai Lu6841ec62010-01-22 01:02:25 -0800403}
404
Dominik Brodowskib3743fa2005-09-09 13:03:23 -0700405void pci_setup_cardbus(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
407 struct pci_dev *bridge = bus->self;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600408 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 struct pci_bus_region region;
410
Bjorn Helgaas865df572009-11-04 10:32:57 -0700411 dev_info(&bridge->dev, "CardBus bridge to [bus %02x-%02x]\n",
412 bus->secondary, bus->subordinate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600414 res = bus->resource[0];
415 pcibios_resource_to_bus(bridge, &region, res);
416 if (res->flags & IORESOURCE_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /*
418 * The IO resource is allocated a range twice as large as it
419 * would normally need. This allows us to set both IO regs.
420 */
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600421 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
423 region.start);
424 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
425 region.end);
426 }
427
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600428 res = bus->resource[1];
429 pcibios_resource_to_bus(bridge, &region, res);
430 if (res->flags & IORESOURCE_IO) {
431 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
433 region.start);
434 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
435 region.end);
436 }
437
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600438 res = bus->resource[2];
439 pcibios_resource_to_bus(bridge, &region, res);
440 if (res->flags & IORESOURCE_MEM) {
441 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
443 region.start);
444 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
445 region.end);
446 }
447
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600448 res = bus->resource[3];
449 pcibios_resource_to_bus(bridge, &region, res);
450 if (res->flags & IORESOURCE_MEM) {
451 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
453 region.start);
454 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
455 region.end);
456 }
457}
Dominik Brodowskib3743fa2005-09-09 13:03:23 -0700458EXPORT_SYMBOL(pci_setup_cardbus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460/* Initialize bridges with base/limit values we have collected.
461 PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
462 requires that if there is no I/O ports or memory behind the
463 bridge, corresponding range must be turned off by writing base
464 value greater than limit to the bridge's base/limit registers.
465
466 Note: care must be taken when updating I/O base/limit registers
467 of bridges which support 32-bit I/O. This update requires two
468 config space writes, so it's quite possible that an I/O window of
469 the bridge will have some undesirable address (e.g. 0) after the
470 first write. Ditto 64-bit prefetchable MMIO. */
Yinghai Lu7cc59972009-12-22 15:02:21 -0800471static void pci_setup_bridge_io(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
473 struct pci_dev *bridge = bus->self;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600474 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 struct pci_bus_region region;
Yinghai Lu7cc59972009-12-22 15:02:21 -0800476 u32 l, io_upper16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 /* Set up the top and bottom of the PCI I/O segment for this bus. */
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600479 res = bus->resource[0];
480 pcibios_resource_to_bus(bridge, &region, res);
481 if (res->flags & IORESOURCE_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 pci_read_config_dword(bridge, PCI_IO_BASE, &l);
483 l &= 0xffff0000;
484 l |= (region.start >> 8) & 0x00f0;
485 l |= region.end & 0xf000;
486 /* Set up upper 16 bits of I/O base/limit. */
487 io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600488 dev_info(&bridge->dev, " bridge window %pR\n", res);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800489 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 /* Clear upper 16 bits of I/O base/limit. */
491 io_upper16 = 0;
492 l = 0x00f0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
494 /* Temporarily disable the I/O range before updating PCI_IO_BASE. */
495 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
496 /* Update lower 16 bits of I/O base/limit. */
497 pci_write_config_dword(bridge, PCI_IO_BASE, l);
498 /* Update upper 16 bits of I/O base/limit. */
499 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800500}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Yinghai Lu7cc59972009-12-22 15:02:21 -0800502static void pci_setup_bridge_mmio(struct pci_bus *bus)
503{
504 struct pci_dev *bridge = bus->self;
505 struct resource *res;
506 struct pci_bus_region region;
507 u32 l;
508
509 /* Set up the top and bottom of the PCI Memory segment for this bus. */
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600510 res = bus->resource[1];
511 pcibios_resource_to_bus(bridge, &region, res);
512 if (res->flags & IORESOURCE_MEM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 l = (region.start >> 16) & 0xfff0;
514 l |= region.end & 0xfff00000;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600515 dev_info(&bridge->dev, " bridge window %pR\n", res);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800516 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 l = 0x0000fff0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519 pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800520}
521
522static void pci_setup_bridge_mmio_pref(struct pci_bus *bus)
523{
524 struct pci_dev *bridge = bus->self;
525 struct resource *res;
526 struct pci_bus_region region;
527 u32 l, bu, lu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 /* Clear out the upper 32 bits of PREF limit.
530 If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
531 disables PREF range, which is ok. */
532 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
533
534 /* Set up PREF base/limit. */
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100535 bu = lu = 0;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600536 res = bus->resource[2];
537 pcibios_resource_to_bus(bridge, &region, res);
538 if (res->flags & IORESOURCE_PREFETCH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 l = (region.start >> 16) & 0xfff0;
540 l |= region.end & 0xfff00000;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600541 if (res->flags & IORESOURCE_MEM_64) {
Yinghai Lu1f82de12009-04-23 20:48:32 -0700542 bu = upper_32_bits(region.start);
543 lu = upper_32_bits(region.end);
Yinghai Lu1f82de12009-04-23 20:48:32 -0700544 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600545 dev_info(&bridge->dev, " bridge window %pR\n", res);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800546 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 l = 0x0000fff0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
550
Alex Williamson59353ea2009-11-30 14:51:44 -0700551 /* Set the upper 32 bits of PREF base & limit. */
552 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
553 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800554}
555
556static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type)
557{
558 struct pci_dev *bridge = bus->self;
559
Yinghai Lu7cc59972009-12-22 15:02:21 -0800560 dev_info(&bridge->dev, "PCI bridge to [bus %02x-%02x]\n",
561 bus->secondary, bus->subordinate);
562
563 if (type & IORESOURCE_IO)
564 pci_setup_bridge_io(bus);
565
566 if (type & IORESOURCE_MEM)
567 pci_setup_bridge_mmio(bus);
568
569 if (type & IORESOURCE_PREFETCH)
570 pci_setup_bridge_mmio_pref(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
573}
574
Benjamin Herrenschmidte2444272011-09-11 14:08:38 -0300575void pci_setup_bridge(struct pci_bus *bus)
Yinghai Lu7cc59972009-12-22 15:02:21 -0800576{
577 unsigned long type = IORESOURCE_IO | IORESOURCE_MEM |
578 IORESOURCE_PREFETCH;
579
580 __pci_setup_bridge(bus, type);
581}
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583/* Check whether the bridge supports optional I/O and
584 prefetchable memory ranges. If not, the respective
585 base/limit registers must be read-only and read as 0. */
Sam Ravnborg96bde062007-03-26 21:53:30 -0800586static void pci_bridge_check_ranges(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
588 u16 io;
589 u32 pmem;
590 struct pci_dev *bridge = bus->self;
591 struct resource *b_res;
592
593 b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
594 b_res[1].flags |= IORESOURCE_MEM;
595
596 pci_read_config_word(bridge, PCI_IO_BASE, &io);
597 if (!io) {
598 pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0);
599 pci_read_config_word(bridge, PCI_IO_BASE, &io);
600 pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
601 }
602 if (io)
603 b_res[0].flags |= IORESOURCE_IO;
604 /* DECchip 21050 pass 2 errata: the bridge may miss an address
605 disconnect boundary by one PCI data phase.
606 Workaround: do not use prefetching on this device. */
607 if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001)
608 return;
609 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
610 if (!pmem) {
611 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE,
612 0xfff0fff0);
613 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
614 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0);
615 }
Yinghai Lu1f82de12009-04-23 20:48:32 -0700616 if (pmem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
Yinghai Lu99586102010-01-22 01:02:28 -0800618 if ((pmem & PCI_PREF_RANGE_TYPE_MASK) ==
619 PCI_PREF_RANGE_TYPE_64) {
Yinghai Lu1f82de12009-04-23 20:48:32 -0700620 b_res[2].flags |= IORESOURCE_MEM_64;
Yinghai Lu99586102010-01-22 01:02:28 -0800621 b_res[2].flags |= PCI_PREF_RANGE_TYPE_64;
622 }
Yinghai Lu1f82de12009-04-23 20:48:32 -0700623 }
624
625 /* double check if bridge does support 64 bit pref */
626 if (b_res[2].flags & IORESOURCE_MEM_64) {
627 u32 mem_base_hi, tmp;
628 pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32,
629 &mem_base_hi);
630 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
631 0xffffffff);
632 pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp);
633 if (!tmp)
634 b_res[2].flags &= ~IORESOURCE_MEM_64;
635 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
636 mem_base_hi);
637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
639
640/* Helper function for sizing routines: find first available
641 bus resource of a given type. Note: we intentionally skip
642 the bus resources which have already been assigned (that is,
643 have non-NULL parent resource). */
Sam Ravnborg96bde062007-03-26 21:53:30 -0800644static struct resource *find_free_bus_resource(struct pci_bus *bus, unsigned long type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
646 int i;
647 struct resource *r;
648 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
649 IORESOURCE_PREFETCH;
650
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -0700651 pci_bus_for_each_resource(bus, r, i) {
Ivan Kokshaysky299de032005-06-15 18:59:27 +0400652 if (r == &ioport_resource || r == &iomem_resource)
653 continue;
Jesse Barnes55a10982009-10-27 09:39:18 -0700654 if (r && (r->flags & type_mask) == type && !r->parent)
655 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
657 return NULL;
658}
659
Ram Pai13583b12011-02-14 17:43:17 -0800660static resource_size_t calculate_iosize(resource_size_t size,
661 resource_size_t min_size,
662 resource_size_t size1,
663 resource_size_t old_size,
664 resource_size_t align)
665{
666 if (size < min_size)
667 size = min_size;
668 if (old_size == 1 )
669 old_size = 0;
670 /* To be fixed in 2.5: we should have sort of HAVE_ISA
671 flag in the struct pci_bus. */
672#if defined(CONFIG_ISA) || defined(CONFIG_EISA)
673 size = (size & 0xff) + ((size & ~0xffUL) << 2);
674#endif
675 size = ALIGN(size + size1, align);
676 if (size < old_size)
677 size = old_size;
678 return size;
679}
680
681static resource_size_t calculate_memsize(resource_size_t size,
682 resource_size_t min_size,
683 resource_size_t size1,
684 resource_size_t old_size,
685 resource_size_t align)
686{
687 if (size < min_size)
688 size = min_size;
689 if (old_size == 1 )
690 old_size = 0;
691 if (size < old_size)
692 size = old_size;
693 size = ALIGN(size + size1, align);
694 return size;
695}
696
Ram Paic8adf9a2011-02-14 17:43:20 -0800697/**
698 * pbus_size_io() - size the io window of a given bus
699 *
700 * @bus : the bus
701 * @min_size : the minimum io window that must to be allocated
702 * @add_size : additional optional io window
Ram Pai9e8bf932011-07-25 13:08:42 -0700703 * @realloc_head : track the additional io window on this list
Ram Paic8adf9a2011-02-14 17:43:20 -0800704 *
705 * Sizing the IO windows of the PCI-PCI bridge is trivial,
706 * since these windows have 4K granularity and the IO ranges
707 * of non-bridge PCI devices are limited to 256 bytes.
708 * We must be careful with the ISA aliasing though.
709 */
710static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800711 resource_size_t add_size, struct list_head *realloc_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
713 struct pci_dev *dev;
714 struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO);
Ram Paic8adf9a2011-02-14 17:43:20 -0800715 unsigned long size = 0, size0 = 0, size1 = 0;
Yinghai Lube768912011-07-25 13:08:38 -0700716 resource_size_t children_add_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 if (!b_res)
719 return;
720
721 list_for_each_entry(dev, &bus->devices, bus_list) {
722 int i;
723
724 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
725 struct resource *r = &dev->resource[i];
726 unsigned long r_size;
727
728 if (r->parent || !(r->flags & IORESOURCE_IO))
729 continue;
Zhao, Yu022edd82008-10-13 19:24:28 +0800730 r_size = resource_size(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 if (r_size < 0x400)
733 /* Might be re-aligned for ISA */
734 size += r_size;
735 else
736 size1 += r_size;
Yinghai Lube768912011-07-25 13:08:38 -0700737
Ram Pai9e8bf932011-07-25 13:08:42 -0700738 if (realloc_head)
739 children_add_size += get_res_add_size(realloc_head, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
741 }
Ram Paic8adf9a2011-02-14 17:43:20 -0800742 size0 = calculate_iosize(size, min_size, size1,
Ram Pai13583b12011-02-14 17:43:17 -0800743 resource_size(b_res), 4096);
Yinghai Lube768912011-07-25 13:08:38 -0700744 if (children_add_size > add_size)
745 add_size = children_add_size;
Ram Pai9e8bf932011-07-25 13:08:42 -0700746 size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
Yinghai Lua4ac9fe2012-01-21 02:08:17 -0800747 calculate_iosize(size, min_size, add_size + size1,
Ram Paic8adf9a2011-02-14 17:43:20 -0800748 resource_size(b_res), 4096);
749 if (!size0 && !size1) {
Bjorn Helgaas865df572009-11-04 10:32:57 -0700750 if (b_res->start || b_res->end)
751 dev_info(&bus->self->dev, "disabling bridge window "
752 "%pR to [bus %02x-%02x] (unused)\n", b_res,
753 bus->secondary, bus->subordinate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 b_res->flags = 0;
755 return;
756 }
757 /* Alignment of the IO window is always 4K */
758 b_res->start = 4096;
Ram Paic8adf9a2011-02-14 17:43:20 -0800759 b_res->end = b_res->start + size0 - 1;
Ivan Kokshaysky88452562008-03-30 19:50:14 +0400760 b_res->flags |= IORESOURCE_STARTALIGN;
Ram Pai9e8bf932011-07-25 13:08:42 -0700761 if (size1 > size0 && realloc_head)
762 add_to_list(realloc_head, bus->self, b_res, size1-size0, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
Ram Paic8adf9a2011-02-14 17:43:20 -0800765/**
766 * pbus_size_mem() - size the memory window of a given bus
767 *
768 * @bus : the bus
769 * @min_size : the minimum memory window that must to be allocated
770 * @add_size : additional optional memory window
Ram Pai9e8bf932011-07-25 13:08:42 -0700771 * @realloc_head : track the additional memory window on this list
Ram Paic8adf9a2011-02-14 17:43:20 -0800772 *
773 * Calculate the size of the bus and minimal alignment which
774 * guarantees that all child resources fit in this size.
775 */
Eric W. Biederman28760482009-09-09 14:09:24 -0700776static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
Ram Paic8adf9a2011-02-14 17:43:20 -0800777 unsigned long type, resource_size_t min_size,
778 resource_size_t add_size,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800779 struct list_head *realloc_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
781 struct pci_dev *dev;
Ram Paic8adf9a2011-02-14 17:43:20 -0800782 resource_size_t min_align, align, size, size0, size1;
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100783 resource_size_t aligns[12]; /* Alignments from 1Mb to 2Gb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 int order, max_order;
785 struct resource *b_res = find_free_bus_resource(bus, type);
Yinghai Lu1f82de12009-04-23 20:48:32 -0700786 unsigned int mem64_mask = 0;
Yinghai Lube768912011-07-25 13:08:38 -0700787 resource_size_t children_add_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 if (!b_res)
790 return 0;
791
792 memset(aligns, 0, sizeof(aligns));
793 max_order = 0;
794 size = 0;
795
Yinghai Lu1f82de12009-04-23 20:48:32 -0700796 mem64_mask = b_res->flags & IORESOURCE_MEM_64;
797 b_res->flags &= ~IORESOURCE_MEM_64;
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 list_for_each_entry(dev, &bus->devices, bus_list) {
800 int i;
Yinghai Lu1f82de12009-04-23 20:48:32 -0700801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
803 struct resource *r = &dev->resource[i];
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100804 resource_size_t r_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806 if (r->parent || (r->flags & mask) != type)
807 continue;
Zhao, Yu022edd82008-10-13 19:24:28 +0800808 r_size = resource_size(r);
Yinghai Lu2aceefc2011-07-25 13:08:40 -0700809#ifdef CONFIG_PCI_IOV
810 /* put SRIOV requested res to the optional list */
Ram Pai9e8bf932011-07-25 13:08:42 -0700811 if (realloc_head && i >= PCI_IOV_RESOURCES &&
Yinghai Lu2aceefc2011-07-25 13:08:40 -0700812 i <= PCI_IOV_RESOURCE_END) {
813 r->end = r->start - 1;
Ram Pai9e8bf932011-07-25 13:08:42 -0700814 add_to_list(realloc_head, dev, r, r_size, 0/* dont' care */);
Yinghai Lu2aceefc2011-07-25 13:08:40 -0700815 children_add_size += r_size;
816 continue;
817 }
818#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 /* For bridges size != alignment */
Chris Wright6faf17f2009-08-28 13:00:06 -0700820 align = pci_resource_alignment(dev, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 order = __ffs(align) - 20;
822 if (order > 11) {
Bjorn Helgaas865df572009-11-04 10:32:57 -0700823 dev_warn(&dev->dev, "disabling BAR %d: %pR "
824 "(bad alignment %#llx)\n", i, r,
825 (unsigned long long) align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 r->flags = 0;
827 continue;
828 }
829 size += r_size;
830 if (order < 0)
831 order = 0;
832 /* Exclude ranges with size > align from
833 calculation of the alignment. */
834 if (r_size == align)
835 aligns[order] += align;
836 if (order > max_order)
837 max_order = order;
Yinghai Lu1f82de12009-04-23 20:48:32 -0700838 mem64_mask &= r->flags & IORESOURCE_MEM_64;
Yinghai Lube768912011-07-25 13:08:38 -0700839
Ram Pai9e8bf932011-07-25 13:08:42 -0700840 if (realloc_head)
841 children_add_size += get_res_add_size(realloc_head, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 align = 0;
845 min_align = 0;
846 for (order = 0; order <= max_order; order++) {
Jeremy Fitzhardinge8308c542008-09-11 01:31:50 -0700847 resource_size_t align1 = 1;
848
849 align1 <<= (order + 20);
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if (!align)
852 min_align = align1;
Milind Arun Choudhary6f6f8c22007-07-09 11:55:51 -0700853 else if (ALIGN(align + min_align, min_align) < align1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 min_align = align1 >> 1;
855 align += aligns[order];
856 }
Linus Torvaldsb42282e2011-04-11 10:53:11 -0700857 size0 = calculate_memsize(size, min_size, 0, resource_size(b_res), min_align);
Yinghai Lube768912011-07-25 13:08:38 -0700858 if (children_add_size > add_size)
859 add_size = children_add_size;
Ram Pai9e8bf932011-07-25 13:08:42 -0700860 size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
Yinghai Lua4ac9fe2012-01-21 02:08:17 -0800861 calculate_memsize(size, min_size, add_size,
Linus Torvaldsb42282e2011-04-11 10:53:11 -0700862 resource_size(b_res), min_align);
Ram Paic8adf9a2011-02-14 17:43:20 -0800863 if (!size0 && !size1) {
Bjorn Helgaas865df572009-11-04 10:32:57 -0700864 if (b_res->start || b_res->end)
865 dev_info(&bus->self->dev, "disabling bridge window "
866 "%pR to [bus %02x-%02x] (unused)\n", b_res,
867 bus->secondary, bus->subordinate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 b_res->flags = 0;
869 return 1;
870 }
871 b_res->start = min_align;
Ram Paic8adf9a2011-02-14 17:43:20 -0800872 b_res->end = size0 + min_align - 1;
873 b_res->flags |= IORESOURCE_STARTALIGN | mem64_mask;
Ram Pai9e8bf932011-07-25 13:08:42 -0700874 if (size1 > size0 && realloc_head)
875 add_to_list(realloc_head, bus->self, b_res, size1-size0, min_align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return 1;
877}
878
Ram Pai0a2daa12011-07-25 13:08:41 -0700879unsigned long pci_cardbus_resource_alignment(struct resource *res)
880{
881 if (res->flags & IORESOURCE_IO)
882 return pci_cardbus_io_size;
883 if (res->flags & IORESOURCE_MEM)
884 return pci_cardbus_mem_size;
885 return 0;
886}
887
888static void pci_bus_size_cardbus(struct pci_bus *bus,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800889 struct list_head *realloc_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
891 struct pci_dev *bridge = bus->self;
892 struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
893 u16 ctrl;
894
895 /*
896 * Reserve some resources for CardBus. We reserve
897 * a fixed amount of bus space for CardBus bridges.
898 */
Linus Torvalds934b7022008-04-22 18:16:30 -0700899 b_res[0].start = 0;
Linus Torvalds934b7022008-04-22 18:16:30 -0700900 b_res[0].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
Ram Pai9e8bf932011-07-25 13:08:42 -0700901 if (realloc_head)
902 add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size, 0 /* dont care */);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Linus Torvalds934b7022008-04-22 18:16:30 -0700904 b_res[1].start = 0;
Linus Torvalds934b7022008-04-22 18:16:30 -0700905 b_res[1].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
Ram Pai9e8bf932011-07-25 13:08:42 -0700906 if (realloc_head)
907 add_to_list(realloc_head, bridge, b_res+1, pci_cardbus_io_size, 0 /* dont care */);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 /*
910 * Check whether prefetchable memory is supported
911 * by this bridge.
912 */
913 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
914 if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
915 ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
916 pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
917 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
918 }
919
920 /*
921 * If we have prefetchable memory support, allocate
922 * two regions. Otherwise, allocate one region of
923 * twice the size.
924 */
925 if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
Linus Torvalds934b7022008-04-22 18:16:30 -0700926 b_res[2].start = 0;
Linus Torvalds934b7022008-04-22 18:16:30 -0700927 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_SIZEALIGN;
Ram Pai9e8bf932011-07-25 13:08:42 -0700928 if (realloc_head)
929 add_to_list(realloc_head, bridge, b_res+2, pci_cardbus_mem_size, 0 /* dont care */);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Linus Torvalds934b7022008-04-22 18:16:30 -0700931 b_res[3].start = 0;
Linus Torvalds934b7022008-04-22 18:16:30 -0700932 b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
Ram Pai9e8bf932011-07-25 13:08:42 -0700933 if (realloc_head)
934 add_to_list(realloc_head, bridge, b_res+3, pci_cardbus_mem_size, 0 /* dont care */);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 } else {
Linus Torvalds934b7022008-04-22 18:16:30 -0700936 b_res[3].start = 0;
Linus Torvalds934b7022008-04-22 18:16:30 -0700937 b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
Ram Pai9e8bf932011-07-25 13:08:42 -0700938 if (realloc_head)
939 add_to_list(realloc_head, bridge, b_res+3, pci_cardbus_mem_size * 2, 0 /* dont care */);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
Ram Pai0a2daa12011-07-25 13:08:41 -0700941
942 /* set the size of the resource to zero, so that the resource does not
943 * get assigned during required-resource allocation cycle but gets assigned
944 * during the optional-resource allocation cycle.
945 */
946 b_res[0].start = b_res[1].start = b_res[2].start = b_res[3].start = 1;
947 b_res[0].end = b_res[1].end = b_res[2].end = b_res[3].end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948}
949
Ram Paic8adf9a2011-02-14 17:43:20 -0800950void __ref __pci_bus_size_bridges(struct pci_bus *bus,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800951 struct list_head *realloc_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
953 struct pci_dev *dev;
954 unsigned long mask, prefmask;
Ram Paic8adf9a2011-02-14 17:43:20 -0800955 resource_size_t additional_mem_size = 0, additional_io_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 list_for_each_entry(dev, &bus->devices, bus_list) {
958 struct pci_bus *b = dev->subordinate;
959 if (!b)
960 continue;
961
962 switch (dev->class >> 8) {
963 case PCI_CLASS_BRIDGE_CARDBUS:
Ram Pai9e8bf932011-07-25 13:08:42 -0700964 pci_bus_size_cardbus(b, realloc_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 break;
966
967 case PCI_CLASS_BRIDGE_PCI:
968 default:
Ram Pai9e8bf932011-07-25 13:08:42 -0700969 __pci_bus_size_bridges(b, realloc_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 break;
971 }
972 }
973
974 /* The root bus? */
975 if (!bus->self)
976 return;
977
978 switch (bus->self->class >> 8) {
979 case PCI_CLASS_BRIDGE_CARDBUS:
980 /* don't size cardbuses yet. */
981 break;
982
983 case PCI_CLASS_BRIDGE_PCI:
984 pci_bridge_check_ranges(bus);
Eric W. Biederman28760482009-09-09 14:09:24 -0700985 if (bus->self->is_hotplug_bridge) {
Ram Paic8adf9a2011-02-14 17:43:20 -0800986 additional_io_size = pci_hotplug_io_size;
987 additional_mem_size = pci_hotplug_mem_size;
Eric W. Biederman28760482009-09-09 14:09:24 -0700988 }
Ram Paic8adf9a2011-02-14 17:43:20 -0800989 /*
990 * Follow thru
991 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 default:
Yinghai Lu19aa7ee2012-01-21 02:08:24 -0800993 pbus_size_io(bus, realloc_head ? 0 : additional_io_size,
994 additional_io_size, realloc_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 /* If the bridge supports prefetchable range, size it
996 separately. If it doesn't, or its prefetchable window
997 has already been allocated by arch code, try
998 non-prefetchable range for both types of PCI memory
999 resources. */
1000 mask = IORESOURCE_MEM;
1001 prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001002 if (pbus_size_mem(bus, prefmask, prefmask,
1003 realloc_head ? 0 : additional_mem_size,
1004 additional_mem_size, realloc_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 mask = prefmask; /* Success, size non-prefetch only. */
Eric W. Biederman28760482009-09-09 14:09:24 -07001006 else
Ram Paic8adf9a2011-02-14 17:43:20 -08001007 additional_mem_size += additional_mem_size;
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001008 pbus_size_mem(bus, mask, IORESOURCE_MEM,
1009 realloc_head ? 0 : additional_mem_size,
1010 additional_mem_size, realloc_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 break;
1012 }
1013}
Ram Paic8adf9a2011-02-14 17:43:20 -08001014
1015void __ref pci_bus_size_bridges(struct pci_bus *bus)
1016{
1017 __pci_bus_size_bridges(bus, NULL);
1018}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019EXPORT_SYMBOL(pci_bus_size_bridges);
1020
Yinghai Lu568ddef2010-01-22 01:02:21 -08001021static void __ref __pci_bus_assign_resources(const struct pci_bus *bus,
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001022 struct list_head *realloc_head,
1023 struct list_head *fail_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
1025 struct pci_bus *b;
1026 struct pci_dev *dev;
1027
Ram Pai9e8bf932011-07-25 13:08:42 -07001028 pbus_assign_resources_sorted(bus, realloc_head, fail_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 list_for_each_entry(dev, &bus->devices, bus_list) {
1031 b = dev->subordinate;
1032 if (!b)
1033 continue;
1034
Ram Pai9e8bf932011-07-25 13:08:42 -07001035 __pci_bus_assign_resources(b, realloc_head, fail_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037 switch (dev->class >> 8) {
1038 case PCI_CLASS_BRIDGE_PCI:
Yinghai Lu6841ec62010-01-22 01:02:25 -08001039 if (!pci_is_enabled(dev))
1040 pci_setup_bridge(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 break;
1042
1043 case PCI_CLASS_BRIDGE_CARDBUS:
1044 pci_setup_cardbus(b);
1045 break;
1046
1047 default:
Bjorn Helgaas80ccba12008-06-13 10:52:11 -06001048 dev_info(&dev->dev, "not setting up bridge for bus "
1049 "%04x:%02x\n", pci_domain_nr(b), b->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 break;
1051 }
1052 }
1053}
Yinghai Lu568ddef2010-01-22 01:02:21 -08001054
1055void __ref pci_bus_assign_resources(const struct pci_bus *bus)
1056{
Ram Paic8adf9a2011-02-14 17:43:20 -08001057 __pci_bus_assign_resources(bus, NULL, NULL);
Yinghai Lu568ddef2010-01-22 01:02:21 -08001058}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059EXPORT_SYMBOL(pci_bus_assign_resources);
1060
Yinghai Lu6841ec62010-01-22 01:02:25 -08001061static void __ref __pci_bridge_assign_resources(const struct pci_dev *bridge,
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001062 struct list_head *add_head,
1063 struct list_head *fail_head)
Yinghai Lu6841ec62010-01-22 01:02:25 -08001064{
1065 struct pci_bus *b;
1066
Yinghai Lu8424d752012-01-21 02:08:21 -08001067 pdev_assign_resources_sorted((struct pci_dev *)bridge,
1068 add_head, fail_head);
Yinghai Lu6841ec62010-01-22 01:02:25 -08001069
1070 b = bridge->subordinate;
1071 if (!b)
1072 return;
1073
Yinghai Lu8424d752012-01-21 02:08:21 -08001074 __pci_bus_assign_resources(b, add_head, fail_head);
Yinghai Lu6841ec62010-01-22 01:02:25 -08001075
1076 switch (bridge->class >> 8) {
1077 case PCI_CLASS_BRIDGE_PCI:
1078 pci_setup_bridge(b);
1079 break;
1080
1081 case PCI_CLASS_BRIDGE_CARDBUS:
1082 pci_setup_cardbus(b);
1083 break;
1084
1085 default:
1086 dev_info(&bridge->dev, "not setting up bridge for bus "
1087 "%04x:%02x\n", pci_domain_nr(b), b->number);
1088 break;
1089 }
1090}
Yinghai Lu5009b462010-01-22 01:02:20 -08001091static void pci_bridge_release_resources(struct pci_bus *bus,
1092 unsigned long type)
1093{
1094 int idx;
1095 bool changed = false;
1096 struct pci_dev *dev;
1097 struct resource *r;
1098 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1099 IORESOURCE_PREFETCH;
1100
1101 dev = bus->self;
1102 for (idx = PCI_BRIDGE_RESOURCES; idx <= PCI_BRIDGE_RESOURCE_END;
1103 idx++) {
1104 r = &dev->resource[idx];
1105 if ((r->flags & type_mask) != type)
1106 continue;
1107 if (!r->parent)
1108 continue;
1109 /*
1110 * if there are children under that, we should release them
1111 * all
1112 */
1113 release_child_resources(r);
1114 if (!release_resource(r)) {
1115 dev_printk(KERN_DEBUG, &dev->dev,
1116 "resource %d %pR released\n", idx, r);
1117 /* keep the old size */
1118 r->end = resource_size(r) - 1;
1119 r->start = 0;
1120 r->flags = 0;
1121 changed = true;
1122 }
1123 }
1124
1125 if (changed) {
1126 /* avoiding touch the one without PREF */
1127 if (type & IORESOURCE_PREFETCH)
1128 type = IORESOURCE_PREFETCH;
1129 __pci_setup_bridge(bus, type);
1130 }
1131}
1132
1133enum release_type {
1134 leaf_only,
1135 whole_subtree,
1136};
1137/*
1138 * try to release pci bridge resources that is from leaf bridge,
1139 * so we can allocate big new one later
1140 */
1141static void __ref pci_bus_release_bridge_resources(struct pci_bus *bus,
1142 unsigned long type,
1143 enum release_type rel_type)
1144{
1145 struct pci_dev *dev;
1146 bool is_leaf_bridge = true;
1147
1148 list_for_each_entry(dev, &bus->devices, bus_list) {
1149 struct pci_bus *b = dev->subordinate;
1150 if (!b)
1151 continue;
1152
1153 is_leaf_bridge = false;
1154
1155 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
1156 continue;
1157
1158 if (rel_type == whole_subtree)
1159 pci_bus_release_bridge_resources(b, type,
1160 whole_subtree);
1161 }
1162
1163 if (pci_is_root_bus(bus))
1164 return;
1165
1166 if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
1167 return;
1168
1169 if ((rel_type == whole_subtree) || is_leaf_bridge)
1170 pci_bridge_release_resources(bus, type);
1171}
1172
Yinghai Lu76fbc262008-06-23 20:33:06 +02001173static void pci_bus_dump_res(struct pci_bus *bus)
1174{
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -07001175 struct resource *res;
1176 int i;
Yinghai Lu76fbc262008-06-23 20:33:06 +02001177
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -07001178 pci_bus_for_each_resource(bus, res, i) {
Yinghai Lu7c9342b2009-12-22 15:02:24 -08001179 if (!res || !res->end || !res->flags)
Yinghai Lu76fbc262008-06-23 20:33:06 +02001180 continue;
1181
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001182 dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res);
Yinghai Lu76fbc262008-06-23 20:33:06 +02001183 }
1184}
1185
1186static void pci_bus_dump_resources(struct pci_bus *bus)
1187{
1188 struct pci_bus *b;
1189 struct pci_dev *dev;
1190
1191
1192 pci_bus_dump_res(bus);
1193
1194 list_for_each_entry(dev, &bus->devices, bus_list) {
1195 b = dev->subordinate;
1196 if (!b)
1197 continue;
1198
1199 pci_bus_dump_resources(b);
1200 }
1201}
1202
Yinghai Luda7822e2011-05-12 17:11:37 -07001203static int __init pci_bus_get_depth(struct pci_bus *bus)
1204{
1205 int depth = 0;
1206 struct pci_dev *dev;
1207
1208 list_for_each_entry(dev, &bus->devices, bus_list) {
1209 int ret;
1210 struct pci_bus *b = dev->subordinate;
1211 if (!b)
1212 continue;
1213
1214 ret = pci_bus_get_depth(b);
1215 if (ret + 1 > depth)
1216 depth = ret + 1;
1217 }
1218
1219 return depth;
1220}
1221static int __init pci_get_max_depth(void)
1222{
1223 int depth = 0;
1224 struct pci_bus *bus;
1225
1226 list_for_each_entry(bus, &pci_root_buses, node) {
1227 int ret;
1228
1229 ret = pci_bus_get_depth(bus);
1230 if (ret > depth)
1231 depth = ret;
1232 }
1233
1234 return depth;
1235}
1236
Ram Paif483d392011-07-07 11:19:10 -07001237
Yinghai Luda7822e2011-05-12 17:11:37 -07001238/*
1239 * first try will not touch pci bridge res
1240 * second and later try will clear small leaf bridge res
1241 * will stop till to the max deepth if can not find good one
1242 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243void __init
1244pci_assign_unassigned_resources(void)
1245{
1246 struct pci_bus *bus;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001247 LIST_HEAD(realloc_head); /* list of resources that
Ram Paic8adf9a2011-02-14 17:43:20 -08001248 want additional resources */
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001249 struct list_head *add_list = NULL;
Yinghai Luda7822e2011-05-12 17:11:37 -07001250 int tried_times = 0;
1251 enum release_type rel_type = leaf_only;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001252 LIST_HEAD(fail_head);
Yinghai Lu764242a2012-01-21 02:08:28 -08001253 struct pci_dev_resource *dev_res_x;
Yinghai Luda7822e2011-05-12 17:11:37 -07001254 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1255 IORESOURCE_PREFETCH;
1256 unsigned long failed_type;
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001257 int pci_try_num = 1;
Yinghai Luda7822e2011-05-12 17:11:37 -07001258
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001259 /* don't realloc if asked to do so */
1260 if (pci_realloc_enabled()) {
1261 int max_depth = pci_get_max_depth();
1262
1263 pci_try_num = max_depth + 1;
1264 printk(KERN_DEBUG "PCI: max bus depth: %d pci_try_num: %d\n",
1265 max_depth, pci_try_num);
1266 }
Yinghai Luda7822e2011-05-12 17:11:37 -07001267
1268again:
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001269 /*
1270 * last try will use add_list, otherwise will try good to have as
1271 * must have, so can realloc parent bridge resource
1272 */
1273 if (tried_times + 1 == pci_try_num)
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001274 add_list = &realloc_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 /* Depth first, calculate sizes and alignments of all
1276 subordinate buses. */
Yinghai Luda7822e2011-05-12 17:11:37 -07001277 list_for_each_entry(bus, &pci_root_buses, node)
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001278 __pci_bus_size_bridges(bus, add_list);
Ram Paic8adf9a2011-02-14 17:43:20 -08001279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 /* Depth last, allocate resources and update the hardware. */
Yinghai Luda7822e2011-05-12 17:11:37 -07001281 list_for_each_entry(bus, &pci_root_buses, node)
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001282 __pci_bus_assign_resources(bus, add_list, &fail_head);
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001283 if (add_list)
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001284 BUG_ON(!list_empty(add_list));
Yinghai Luda7822e2011-05-12 17:11:37 -07001285 tried_times++;
1286
1287 /* any device complain? */
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001288 if (list_empty(&fail_head))
Yinghai Luda7822e2011-05-12 17:11:37 -07001289 goto enable_and_dump;
Ram Paif483d392011-07-07 11:19:10 -07001290
Yinghai Luda7822e2011-05-12 17:11:37 -07001291 failed_type = 0;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001292 list_for_each_entry(dev_res_x, &fail_head, list)
1293 failed_type |= dev_res_x->flags;
1294
Yinghai Luda7822e2011-05-12 17:11:37 -07001295 /*
1296 * io port are tight, don't try extra
1297 * or if reach the limit, don't want to try more
1298 */
1299 failed_type &= type_mask;
1300 if ((failed_type == IORESOURCE_IO) || (tried_times >= pci_try_num)) {
Yinghai Lu764242a2012-01-21 02:08:28 -08001301 free_list(pci_dev_resource, &fail_head);
Yinghai Luda7822e2011-05-12 17:11:37 -07001302 goto enable_and_dump;
1303 }
1304
1305 printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
1306 tried_times + 1);
1307
1308 /* third times and later will not check if it is leaf */
1309 if ((tried_times + 1) > 2)
1310 rel_type = whole_subtree;
1311
1312 /*
1313 * Try to release leaf bridge's resources that doesn't fit resource of
1314 * child device under that bridge
1315 */
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001316 list_for_each_entry(dev_res_x, &fail_head, list) {
1317 bus = dev_res_x->dev->bus;
1318 pci_bus_release_bridge_resources(bus,
1319 dev_res_x->flags & type_mask,
1320 rel_type);
Yinghai Luda7822e2011-05-12 17:11:37 -07001321 }
1322 /* restore size and flags */
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001323 list_for_each_entry(dev_res_x, &fail_head, list) {
1324 struct resource *res = dev_res_x->res;
Yinghai Luda7822e2011-05-12 17:11:37 -07001325
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001326 res->start = dev_res_x->start;
1327 res->end = dev_res_x->end;
1328 res->flags = dev_res_x->flags;
1329 if (dev_res_x->dev->subordinate)
Yinghai Luda7822e2011-05-12 17:11:37 -07001330 res->flags = 0;
Yinghai Luda7822e2011-05-12 17:11:37 -07001331 }
Yinghai Lu764242a2012-01-21 02:08:28 -08001332 free_list(pci_dev_resource, &fail_head);
Yinghai Luda7822e2011-05-12 17:11:37 -07001333
1334 goto again;
1335
1336enable_and_dump:
1337 /* Depth last, update the hardware. */
1338 list_for_each_entry(bus, &pci_root_buses, node)
1339 pci_enable_bridges(bus);
Yinghai Lu76fbc262008-06-23 20:33:06 +02001340
1341 /* dump the resource on buses */
Yinghai Luda7822e2011-05-12 17:11:37 -07001342 list_for_each_entry(bus, &pci_root_buses, node)
Yinghai Lu76fbc262008-06-23 20:33:06 +02001343 pci_bus_dump_resources(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
Yinghai Lu6841ec62010-01-22 01:02:25 -08001345
1346void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
1347{
1348 struct pci_bus *parent = bridge->subordinate;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001349 LIST_HEAD(add_list); /* list of resources that
Yinghai Lu8424d752012-01-21 02:08:21 -08001350 want additional resources */
Yinghai Lu32180e42010-01-22 01:02:27 -08001351 int tried_times = 0;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001352 LIST_HEAD(fail_head);
Yinghai Lu764242a2012-01-21 02:08:28 -08001353 struct pci_dev_resource *dev_res_x;
Yinghai Lu6841ec62010-01-22 01:02:25 -08001354 int retval;
Yinghai Lu32180e42010-01-22 01:02:27 -08001355 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1356 IORESOURCE_PREFETCH;
Yinghai Lu6841ec62010-01-22 01:02:25 -08001357
Yinghai Lu32180e42010-01-22 01:02:27 -08001358again:
Yinghai Lu8424d752012-01-21 02:08:21 -08001359 __pci_bus_size_bridges(parent, &add_list);
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001360 __pci_bridge_assign_resources(bridge, &add_list, &fail_head);
1361 BUG_ON(!list_empty(&add_list));
Yinghai Lu32180e42010-01-22 01:02:27 -08001362 tried_times++;
1363
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001364 if (list_empty(&fail_head))
Yinghai Lu3f579c32010-05-21 14:35:06 -07001365 goto enable_all;
Yinghai Lu32180e42010-01-22 01:02:27 -08001366
1367 if (tried_times >= 2) {
1368 /* still fail, don't need to try more */
Yinghai Lu764242a2012-01-21 02:08:28 -08001369 free_list(pci_dev_resource, &fail_head);
Yinghai Lu3f579c32010-05-21 14:35:06 -07001370 goto enable_all;
Yinghai Lu32180e42010-01-22 01:02:27 -08001371 }
1372
1373 printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
1374 tried_times + 1);
1375
1376 /*
1377 * Try to release leaf bridge's resources that doesn't fit resource of
1378 * child device under that bridge
1379 */
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001380 list_for_each_entry(dev_res_x, &fail_head, list) {
1381 struct pci_bus *bus = dev_res_x->dev->bus;
1382 unsigned long flags = dev_res_x->flags;
Yinghai Lu32180e42010-01-22 01:02:27 -08001383
1384 pci_bus_release_bridge_resources(bus, flags & type_mask,
1385 whole_subtree);
Yinghai Lu32180e42010-01-22 01:02:27 -08001386 }
1387 /* restore size and flags */
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001388 list_for_each_entry(dev_res_x, &fail_head, list) {
1389 struct resource *res = dev_res_x->res;
Yinghai Lu32180e42010-01-22 01:02:27 -08001390
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001391 res->start = dev_res_x->start;
1392 res->end = dev_res_x->end;
1393 res->flags = dev_res_x->flags;
1394 if (dev_res_x->dev->subordinate)
Yinghai Lu32180e42010-01-22 01:02:27 -08001395 res->flags = 0;
Yinghai Lu32180e42010-01-22 01:02:27 -08001396 }
Yinghai Lu764242a2012-01-21 02:08:28 -08001397 free_list(pci_dev_resource, &fail_head);
Yinghai Lu32180e42010-01-22 01:02:27 -08001398
1399 goto again;
Yinghai Lu3f579c32010-05-21 14:35:06 -07001400
1401enable_all:
1402 retval = pci_reenable_device(bridge);
1403 pci_set_master(bridge);
1404 pci_enable_bridges(parent);
Yinghai Lu6841ec62010-01-22 01:02:25 -08001405}
1406EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
Yinghai Lu9b030882012-01-21 02:08:23 -08001407
1408#ifdef CONFIG_HOTPLUG
1409/**
1410 * pci_rescan_bus - scan a PCI bus for devices.
1411 * @bus: PCI bus to scan
1412 *
1413 * Scan a PCI bus and child buses for new devices, adds them,
1414 * and enables them.
1415 *
1416 * Returns the max number of subordinate bus discovered.
1417 */
1418unsigned int __ref pci_rescan_bus(struct pci_bus *bus)
1419{
1420 unsigned int max;
1421 struct pci_dev *dev;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001422 LIST_HEAD(add_list); /* list of resources that
Yinghai Lu9b030882012-01-21 02:08:23 -08001423 want additional resources */
1424
1425 max = pci_scan_child_bus(bus);
1426
Yinghai Lu9b030882012-01-21 02:08:23 -08001427 down_read(&pci_bus_sem);
1428 list_for_each_entry(dev, &bus->devices, bus_list)
1429 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
1430 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
1431 if (dev->subordinate)
1432 __pci_bus_size_bridges(dev->subordinate,
1433 &add_list);
1434 up_read(&pci_bus_sem);
1435 __pci_bus_assign_resources(bus, &add_list, NULL);
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001436 BUG_ON(!list_empty(&add_list));
Yinghai Lu9b030882012-01-21 02:08:23 -08001437
1438 pci_enable_bridges(bus);
1439 pci_bus_add_devices(bus);
1440
1441 return max;
1442}
1443EXPORT_SYMBOL_GPL(pci_rescan_bus);
1444#endif