blob: e241f2fd6cbf72cee1c15f4db0d3286af9e986a8 [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>
Bjorn Helgaas47087702012-02-23 14:29:23 -070028#include <asm-generic/pci-bridge.h>
Chris Wright6faf17f2009-08-28 13:00:06 -070029#include "pci.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Bjorn Helgaas844393f2012-02-23 20:18:59 -070031unsigned int pci_flags;
Bjorn Helgaas47087702012-02-23 14:29:23 -070032
Yinghai Lubdc4abe2012-01-21 02:08:27 -080033struct pci_dev_resource {
34 struct list_head list;
Yinghai Lu2934a0d2012-01-21 02:08:26 -080035 struct resource *res;
36 struct pci_dev *dev;
Yinghai Lu568ddef2010-01-22 01:02:21 -080037 resource_size_t start;
38 resource_size_t end;
Ram Paic8adf9a2011-02-14 17:43:20 -080039 resource_size_t add_size;
Ram Pai2bbc6942011-07-25 13:08:39 -070040 resource_size_t min_align;
Yinghai Lu568ddef2010-01-22 01:02:21 -080041 unsigned long flags;
42};
43
Yinghai Lubffc56d2012-01-21 02:08:30 -080044static void free_list(struct list_head *head)
45{
46 struct pci_dev_resource *dev_res, *tmp;
47
48 list_for_each_entry_safe(dev_res, tmp, head, list) {
49 list_del(&dev_res->list);
50 kfree(dev_res);
51 }
52}
Ram Pai094732a2011-02-14 17:43:18 -080053
Ram Paif483d392011-07-07 11:19:10 -070054int pci_realloc_enable = 0;
55#define pci_realloc_enabled() pci_realloc_enable
56void pci_realloc(void)
57{
58 pci_realloc_enable = 1;
59}
60
Ram Paic8adf9a2011-02-14 17:43:20 -080061/**
62 * add_to_list() - add a new resource tracker to the list
63 * @head: Head of the list
64 * @dev: device corresponding to which the resource
65 * belongs
66 * @res: The resource to be tracked
67 * @add_size: additional size to be optionally added
68 * to the resource
69 */
Yinghai Lubdc4abe2012-01-21 02:08:27 -080070static int add_to_list(struct list_head *head,
Ram Paic8adf9a2011-02-14 17:43:20 -080071 struct pci_dev *dev, struct resource *res,
Ram Pai2bbc6942011-07-25 13:08:39 -070072 resource_size_t add_size, resource_size_t min_align)
Yinghai Lu568ddef2010-01-22 01:02:21 -080073{
Yinghai Lu764242a2012-01-21 02:08:28 -080074 struct pci_dev_resource *tmp;
Yinghai Lu568ddef2010-01-22 01:02:21 -080075
Yinghai Lubdc4abe2012-01-21 02:08:27 -080076 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
Yinghai Lu568ddef2010-01-22 01:02:21 -080077 if (!tmp) {
Ram Paic8adf9a2011-02-14 17:43:20 -080078 pr_warning("add_to_list: kmalloc() failed!\n");
Yinghai Luef62dfe2012-01-21 02:08:18 -080079 return -ENOMEM;
Yinghai Lu568ddef2010-01-22 01:02:21 -080080 }
81
Yinghai Lu568ddef2010-01-22 01:02:21 -080082 tmp->res = res;
83 tmp->dev = dev;
84 tmp->start = res->start;
85 tmp->end = res->end;
86 tmp->flags = res->flags;
Ram Paic8adf9a2011-02-14 17:43:20 -080087 tmp->add_size = add_size;
Ram Pai2bbc6942011-07-25 13:08:39 -070088 tmp->min_align = min_align;
Yinghai Lubdc4abe2012-01-21 02:08:27 -080089
90 list_add(&tmp->list, head);
Yinghai Luef62dfe2012-01-21 02:08:18 -080091
92 return 0;
Yinghai Lu568ddef2010-01-22 01:02:21 -080093}
94
Yinghai Lub9b0bba2012-01-21 02:08:29 -080095static void remove_from_list(struct list_head *head,
Yinghai Lu3e6e0d82012-01-21 02:08:20 -080096 struct resource *res)
97{
Yinghai Lub9b0bba2012-01-21 02:08:29 -080098 struct pci_dev_resource *dev_res, *tmp;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -080099
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800100 list_for_each_entry_safe(dev_res, tmp, head, list) {
101 if (dev_res->res == res) {
102 list_del(&dev_res->list);
103 kfree(dev_res);
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800104 break;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800105 }
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800106 }
107}
108
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800109static resource_size_t get_res_add_size(struct list_head *head,
Yinghai Lu1c372352012-01-21 02:08:19 -0800110 struct resource *res)
111{
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800112 struct pci_dev_resource *dev_res;
Yinghai Lu1c372352012-01-21 02:08:19 -0800113
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800114 list_for_each_entry(dev_res, head, list) {
115 if (dev_res->res == res) {
Yinghai Lub5924432012-01-21 02:08:31 -0800116 int idx = res - &dev_res->dev->resource[0];
117
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800118 dev_printk(KERN_DEBUG, &dev_res->dev->dev,
Yinghai Lub5924432012-01-21 02:08:31 -0800119 "res[%d]=%pR get_res_add_size add_size %llx\n",
120 idx, dev_res->res,
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800121 (unsigned long long)dev_res->add_size);
Yinghai Lub5924432012-01-21 02:08:31 -0800122
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800123 return dev_res->add_size;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800124 }
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 Lub9b0bba2012-01-21 02:08:29 -0800224 struct pci_dev_resource *add_res, *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 Lub9b0bba2012-01-21 02:08:29 -0800229 list_for_each_entry_safe(add_res, tmp, realloc_head, list) {
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800230 bool found_match = false;
231
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800232 res = add_res->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 Lub9b0bba2012-01-21 02:08:29 -0800247 idx = res - &add_res->dev->resource[0];
248 add_size = add_res->add_size;
Ram Pai2bbc6942011-07-25 13:08:39 -0700249 if (!resource_size(res)) {
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800250 res->start = add_res->start;
Ram Pai2bbc6942011-07-25 13:08:39 -0700251 res->end = res->start + add_size - 1;
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800252 if (pci_assign_resource(add_res->dev, idx))
Ram Paic8adf9a2011-02-14 17:43:20 -0800253 reset_resource(res);
Ram Pai2bbc6942011-07-25 13:08:39 -0700254 } else {
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800255 resource_size_t align = add_res->min_align;
256 res->flags |= add_res->flags &
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800257 (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN);
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800258 if (pci_reassign_resource(add_res->dev, idx,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800259 add_size, align))
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800260 dev_printk(KERN_DEBUG, &add_res->dev->dev,
Yinghai Lub5924432012-01-21 02:08:31 -0800261 "failed to add %llx res[%d]=%pR\n",
262 (unsigned long long)add_size,
263 idx, res);
Ram Paic8adf9a2011-02-14 17:43:20 -0800264 }
265out:
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800266 list_del(&add_res->list);
267 kfree(add_res);
Ram Paic8adf9a2011-02-14 17:43:20 -0800268 }
269}
270
271/**
272 * assign_requested_resources_sorted() - satisfy resource requests
273 *
274 * @head : head of the list tracking requests for resources
275 * @failed_list : head of the list tracking requests that could
276 * not be allocated
277 *
278 * Satisfy resource requests of each element in the list. Add
279 * requests that could not satisfied to the failed_list.
280 */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800281static void assign_requested_resources_sorted(struct list_head *head,
282 struct list_head *fail_head)
Yinghai Lu6841ec62010-01-22 01:02:25 -0800283{
284 struct resource *res;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800285 struct pci_dev_resource *dev_res;
Yinghai Lu6841ec62010-01-22 01:02:25 -0800286 int idx;
287
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800288 list_for_each_entry(dev_res, head, list) {
289 res = dev_res->res;
290 idx = res - &dev_res->dev->resource[0];
291 if (resource_size(res) &&
292 pci_assign_resource(dev_res->dev, idx)) {
293 if (fail_head && !pci_is_root_bus(dev_res->dev->bus)) {
Yinghai Lu9a928662010-02-28 15:49:39 -0800294 /*
295 * if the failed res is for ROM BAR, and it will
296 * be enabled later, don't add it to the list
297 */
298 if (!((idx == PCI_ROM_RESOURCE) &&
299 (!(res->flags & IORESOURCE_ROM_ENABLE))))
Yinghai Lu67cc7e22012-01-21 02:08:32 -0800300 add_to_list(fail_head,
301 dev_res->dev, res,
302 0 /* dont care */,
303 0 /* dont care */);
Yinghai Lu9a928662010-02-28 15:49:39 -0800304 }
Ram Paifc075e12011-02-14 17:43:19 -0800305 reset_resource(res);
Rajesh Shah542df5d2005-04-28 00:25:50 -0700306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308}
309
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800310static void __assign_resources_sorted(struct list_head *head,
311 struct list_head *realloc_head,
312 struct list_head *fail_head)
Ram Paic8adf9a2011-02-14 17:43:20 -0800313{
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800314 /*
315 * Should not assign requested resources at first.
316 * they could be adjacent, so later reassign can not reallocate
317 * them one by one in parent resource window.
318 * Try to assign requested + add_size at begining
319 * if could do that, could get out early.
320 * if could not do that, we still try to assign requested at first,
321 * then try to reassign add_size for some resources.
322 */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800323 LIST_HEAD(save_head);
324 LIST_HEAD(local_fail_head);
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800325 struct pci_dev_resource *save_res;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800326 struct pci_dev_resource *dev_res;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800327
328 /* Check if optional add_size is there */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800329 if (!realloc_head || list_empty(realloc_head))
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800330 goto requested_and_reassign;
331
332 /* Save original start, end, flags etc at first */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800333 list_for_each_entry(dev_res, head, list) {
334 if (add_to_list(&save_head, dev_res->dev, dev_res->res, 0, 0)) {
Yinghai Lubffc56d2012-01-21 02:08:30 -0800335 free_list(&save_head);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800336 goto requested_and_reassign;
337 }
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800338 }
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800339
340 /* Update res in head list with add_size in realloc_head list */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800341 list_for_each_entry(dev_res, head, list)
342 dev_res->res->end += get_res_add_size(realloc_head,
343 dev_res->res);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800344
345 /* Try updated head list with add_size added */
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800346 assign_requested_resources_sorted(head, &local_fail_head);
347
348 /* all assigned with add_size ? */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800349 if (list_empty(&local_fail_head)) {
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800350 /* Remove head list from realloc_head list */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800351 list_for_each_entry(dev_res, head, list)
352 remove_from_list(realloc_head, dev_res->res);
Yinghai Lubffc56d2012-01-21 02:08:30 -0800353 free_list(&save_head);
354 free_list(head);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800355 return;
356 }
357
Yinghai Lubffc56d2012-01-21 02:08:30 -0800358 free_list(&local_fail_head);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800359 /* Release assigned resource */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800360 list_for_each_entry(dev_res, head, list)
361 if (dev_res->res->parent)
362 release_resource(dev_res->res);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800363 /* Restore start/end/flags from saved list */
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800364 list_for_each_entry(save_res, &save_head, list) {
365 struct resource *res = save_res->res;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800366
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800367 res->start = save_res->start;
368 res->end = save_res->end;
369 res->flags = save_res->flags;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800370 }
Yinghai Lubffc56d2012-01-21 02:08:30 -0800371 free_list(&save_head);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800372
373requested_and_reassign:
Ram Paic8adf9a2011-02-14 17:43:20 -0800374 /* Satisfy the must-have resource requests */
375 assign_requested_resources_sorted(head, fail_head);
376
Ram Pai0a2daa12011-07-25 13:08:41 -0700377 /* Try to satisfy any additional optional resource
Ram Paic8adf9a2011-02-14 17:43:20 -0800378 requests */
Ram Pai9e8bf932011-07-25 13:08:42 -0700379 if (realloc_head)
380 reassign_resources_sorted(realloc_head, head);
Yinghai Lubffc56d2012-01-21 02:08:30 -0800381 free_list(head);
Ram Paic8adf9a2011-02-14 17:43:20 -0800382}
383
Yinghai Lu6841ec62010-01-22 01:02:25 -0800384static void pdev_assign_resources_sorted(struct pci_dev *dev,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800385 struct list_head *add_head,
386 struct list_head *fail_head)
Yinghai Lu6841ec62010-01-22 01:02:25 -0800387{
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800388 LIST_HEAD(head);
Yinghai Lu6841ec62010-01-22 01:02:25 -0800389
Yinghai Lu6841ec62010-01-22 01:02:25 -0800390 __dev_sort_resources(dev, &head);
Yinghai Lu8424d752012-01-21 02:08:21 -0800391 __assign_resources_sorted(&head, add_head, fail_head);
Yinghai Lu6841ec62010-01-22 01:02:25 -0800392
393}
394
395static void pbus_assign_resources_sorted(const struct pci_bus *bus,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800396 struct list_head *realloc_head,
397 struct list_head *fail_head)
Yinghai Lu6841ec62010-01-22 01:02:25 -0800398{
399 struct pci_dev *dev;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800400 LIST_HEAD(head);
Yinghai Lu6841ec62010-01-22 01:02:25 -0800401
Yinghai Lu6841ec62010-01-22 01:02:25 -0800402 list_for_each_entry(dev, &bus->devices, bus_list)
403 __dev_sort_resources(dev, &head);
404
Ram Pai9e8bf932011-07-25 13:08:42 -0700405 __assign_resources_sorted(&head, realloc_head, fail_head);
Yinghai Lu6841ec62010-01-22 01:02:25 -0800406}
407
Dominik Brodowskib3743fa2005-09-09 13:03:23 -0700408void pci_setup_cardbus(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
410 struct pci_dev *bridge = bus->self;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600411 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 struct pci_bus_region region;
413
Bjorn Helgaas865df572009-11-04 10:32:57 -0700414 dev_info(&bridge->dev, "CardBus bridge to [bus %02x-%02x]\n",
415 bus->secondary, bus->subordinate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600417 res = bus->resource[0];
418 pcibios_resource_to_bus(bridge, &region, res);
419 if (res->flags & IORESOURCE_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 /*
421 * The IO resource is allocated a range twice as large as it
422 * would normally need. This allows us to set both IO regs.
423 */
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600424 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
426 region.start);
427 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
428 region.end);
429 }
430
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600431 res = bus->resource[1];
432 pcibios_resource_to_bus(bridge, &region, res);
433 if (res->flags & IORESOURCE_IO) {
434 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
436 region.start);
437 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
438 region.end);
439 }
440
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600441 res = bus->resource[2];
442 pcibios_resource_to_bus(bridge, &region, res);
443 if (res->flags & IORESOURCE_MEM) {
444 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
446 region.start);
447 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
448 region.end);
449 }
450
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600451 res = bus->resource[3];
452 pcibios_resource_to_bus(bridge, &region, res);
453 if (res->flags & IORESOURCE_MEM) {
454 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
456 region.start);
457 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
458 region.end);
459 }
460}
Dominik Brodowskib3743fa2005-09-09 13:03:23 -0700461EXPORT_SYMBOL(pci_setup_cardbus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463/* Initialize bridges with base/limit values we have collected.
464 PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
465 requires that if there is no I/O ports or memory behind the
466 bridge, corresponding range must be turned off by writing base
467 value greater than limit to the bridge's base/limit registers.
468
469 Note: care must be taken when updating I/O base/limit registers
470 of bridges which support 32-bit I/O. This update requires two
471 config space writes, so it's quite possible that an I/O window of
472 the bridge will have some undesirable address (e.g. 0) after the
473 first write. Ditto 64-bit prefetchable MMIO. */
Yinghai Lu7cc59972009-12-22 15:02:21 -0800474static void pci_setup_bridge_io(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
476 struct pci_dev *bridge = bus->self;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600477 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 struct pci_bus_region region;
Yinghai Lu7cc59972009-12-22 15:02:21 -0800479 u32 l, io_upper16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 /* Set up the top and bottom of the PCI I/O segment for this bus. */
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600482 res = bus->resource[0];
483 pcibios_resource_to_bus(bridge, &region, res);
484 if (res->flags & IORESOURCE_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 pci_read_config_dword(bridge, PCI_IO_BASE, &l);
486 l &= 0xffff0000;
487 l |= (region.start >> 8) & 0x00f0;
488 l |= region.end & 0xf000;
489 /* Set up upper 16 bits of I/O base/limit. */
490 io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600491 dev_info(&bridge->dev, " bridge window %pR\n", res);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800492 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 /* Clear upper 16 bits of I/O base/limit. */
494 io_upper16 = 0;
495 l = 0x00f0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
497 /* Temporarily disable the I/O range before updating PCI_IO_BASE. */
498 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
499 /* Update lower 16 bits of I/O base/limit. */
500 pci_write_config_dword(bridge, PCI_IO_BASE, l);
501 /* Update upper 16 bits of I/O base/limit. */
502 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800503}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Yinghai Lu7cc59972009-12-22 15:02:21 -0800505static void pci_setup_bridge_mmio(struct pci_bus *bus)
506{
507 struct pci_dev *bridge = bus->self;
508 struct resource *res;
509 struct pci_bus_region region;
510 u32 l;
511
512 /* Set up the top and bottom of the PCI Memory segment for this bus. */
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600513 res = bus->resource[1];
514 pcibios_resource_to_bus(bridge, &region, res);
515 if (res->flags & IORESOURCE_MEM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 l = (region.start >> 16) & 0xfff0;
517 l |= region.end & 0xfff00000;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600518 dev_info(&bridge->dev, " bridge window %pR\n", res);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800519 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 l = 0x0000fff0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
522 pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800523}
524
525static void pci_setup_bridge_mmio_pref(struct pci_bus *bus)
526{
527 struct pci_dev *bridge = bus->self;
528 struct resource *res;
529 struct pci_bus_region region;
530 u32 l, bu, lu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 /* Clear out the upper 32 bits of PREF limit.
533 If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
534 disables PREF range, which is ok. */
535 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
536
537 /* Set up PREF base/limit. */
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100538 bu = lu = 0;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600539 res = bus->resource[2];
540 pcibios_resource_to_bus(bridge, &region, res);
541 if (res->flags & IORESOURCE_PREFETCH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 l = (region.start >> 16) & 0xfff0;
543 l |= region.end & 0xfff00000;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600544 if (res->flags & IORESOURCE_MEM_64) {
Yinghai Lu1f82de12009-04-23 20:48:32 -0700545 bu = upper_32_bits(region.start);
546 lu = upper_32_bits(region.end);
Yinghai Lu1f82de12009-04-23 20:48:32 -0700547 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600548 dev_info(&bridge->dev, " bridge window %pR\n", res);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800549 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 l = 0x0000fff0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
552 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
553
Alex Williamson59353ea2009-11-30 14:51:44 -0700554 /* Set the upper 32 bits of PREF base & limit. */
555 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
556 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800557}
558
559static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type)
560{
561 struct pci_dev *bridge = bus->self;
562
Yinghai Lu7cc59972009-12-22 15:02:21 -0800563 dev_info(&bridge->dev, "PCI bridge to [bus %02x-%02x]\n",
564 bus->secondary, bus->subordinate);
565
566 if (type & IORESOURCE_IO)
567 pci_setup_bridge_io(bus);
568
569 if (type & IORESOURCE_MEM)
570 pci_setup_bridge_mmio(bus);
571
572 if (type & IORESOURCE_PREFETCH)
573 pci_setup_bridge_mmio_pref(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
576}
577
Benjamin Herrenschmidte2444272011-09-11 14:08:38 -0300578void pci_setup_bridge(struct pci_bus *bus)
Yinghai Lu7cc59972009-12-22 15:02:21 -0800579{
580 unsigned long type = IORESOURCE_IO | IORESOURCE_MEM |
581 IORESOURCE_PREFETCH;
582
583 __pci_setup_bridge(bus, type);
584}
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586/* Check whether the bridge supports optional I/O and
587 prefetchable memory ranges. If not, the respective
588 base/limit registers must be read-only and read as 0. */
Sam Ravnborg96bde062007-03-26 21:53:30 -0800589static void pci_bridge_check_ranges(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
591 u16 io;
592 u32 pmem;
593 struct pci_dev *bridge = bus->self;
594 struct resource *b_res;
595
596 b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
597 b_res[1].flags |= IORESOURCE_MEM;
598
599 pci_read_config_word(bridge, PCI_IO_BASE, &io);
600 if (!io) {
601 pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0);
602 pci_read_config_word(bridge, PCI_IO_BASE, &io);
603 pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
604 }
605 if (io)
606 b_res[0].flags |= IORESOURCE_IO;
607 /* DECchip 21050 pass 2 errata: the bridge may miss an address
608 disconnect boundary by one PCI data phase.
609 Workaround: do not use prefetching on this device. */
610 if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001)
611 return;
612 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
613 if (!pmem) {
614 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE,
615 0xfff0fff0);
616 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
617 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0);
618 }
Yinghai Lu1f82de12009-04-23 20:48:32 -0700619 if (pmem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
Yinghai Lu99586102010-01-22 01:02:28 -0800621 if ((pmem & PCI_PREF_RANGE_TYPE_MASK) ==
622 PCI_PREF_RANGE_TYPE_64) {
Yinghai Lu1f82de12009-04-23 20:48:32 -0700623 b_res[2].flags |= IORESOURCE_MEM_64;
Yinghai Lu99586102010-01-22 01:02:28 -0800624 b_res[2].flags |= PCI_PREF_RANGE_TYPE_64;
625 }
Yinghai Lu1f82de12009-04-23 20:48:32 -0700626 }
627
628 /* double check if bridge does support 64 bit pref */
629 if (b_res[2].flags & IORESOURCE_MEM_64) {
630 u32 mem_base_hi, tmp;
631 pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32,
632 &mem_base_hi);
633 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
634 0xffffffff);
635 pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp);
636 if (!tmp)
637 b_res[2].flags &= ~IORESOURCE_MEM_64;
638 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
639 mem_base_hi);
640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
643/* Helper function for sizing routines: find first available
644 bus resource of a given type. Note: we intentionally skip
645 the bus resources which have already been assigned (that is,
646 have non-NULL parent resource). */
Sam Ravnborg96bde062007-03-26 21:53:30 -0800647static struct resource *find_free_bus_resource(struct pci_bus *bus, unsigned long type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 int i;
650 struct resource *r;
651 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
652 IORESOURCE_PREFETCH;
653
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -0700654 pci_bus_for_each_resource(bus, r, i) {
Ivan Kokshaysky299de032005-06-15 18:59:27 +0400655 if (r == &ioport_resource || r == &iomem_resource)
656 continue;
Jesse Barnes55a10982009-10-27 09:39:18 -0700657 if (r && (r->flags & type_mask) == type && !r->parent)
658 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660 return NULL;
661}
662
Ram Pai13583b12011-02-14 17:43:17 -0800663static resource_size_t calculate_iosize(resource_size_t size,
664 resource_size_t min_size,
665 resource_size_t size1,
666 resource_size_t old_size,
667 resource_size_t align)
668{
669 if (size < min_size)
670 size = min_size;
671 if (old_size == 1 )
672 old_size = 0;
673 /* To be fixed in 2.5: we should have sort of HAVE_ISA
674 flag in the struct pci_bus. */
675#if defined(CONFIG_ISA) || defined(CONFIG_EISA)
676 size = (size & 0xff) + ((size & ~0xffUL) << 2);
677#endif
678 size = ALIGN(size + size1, align);
679 if (size < old_size)
680 size = old_size;
681 return size;
682}
683
684static resource_size_t calculate_memsize(resource_size_t size,
685 resource_size_t min_size,
686 resource_size_t size1,
687 resource_size_t old_size,
688 resource_size_t align)
689{
690 if (size < min_size)
691 size = min_size;
692 if (old_size == 1 )
693 old_size = 0;
694 if (size < old_size)
695 size = old_size;
696 size = ALIGN(size + size1, align);
697 return size;
698}
699
Ram Paic8adf9a2011-02-14 17:43:20 -0800700/**
701 * pbus_size_io() - size the io window of a given bus
702 *
703 * @bus : the bus
704 * @min_size : the minimum io window that must to be allocated
705 * @add_size : additional optional io window
Ram Pai9e8bf932011-07-25 13:08:42 -0700706 * @realloc_head : track the additional io window on this list
Ram Paic8adf9a2011-02-14 17:43:20 -0800707 *
708 * Sizing the IO windows of the PCI-PCI bridge is trivial,
709 * since these windows have 4K granularity and the IO ranges
710 * of non-bridge PCI devices are limited to 256 bytes.
711 * We must be careful with the ISA aliasing though.
712 */
713static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800714 resource_size_t add_size, struct list_head *realloc_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
716 struct pci_dev *dev;
717 struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO);
Ram Paic8adf9a2011-02-14 17:43:20 -0800718 unsigned long size = 0, size0 = 0, size1 = 0;
Yinghai Lube768912011-07-25 13:08:38 -0700719 resource_size_t children_add_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 if (!b_res)
722 return;
723
724 list_for_each_entry(dev, &bus->devices, bus_list) {
725 int i;
726
727 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
728 struct resource *r = &dev->resource[i];
729 unsigned long r_size;
730
731 if (r->parent || !(r->flags & IORESOURCE_IO))
732 continue;
Zhao, Yu022edd82008-10-13 19:24:28 +0800733 r_size = resource_size(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 if (r_size < 0x400)
736 /* Might be re-aligned for ISA */
737 size += r_size;
738 else
739 size1 += r_size;
Yinghai Lube768912011-07-25 13:08:38 -0700740
Ram Pai9e8bf932011-07-25 13:08:42 -0700741 if (realloc_head)
742 children_add_size += get_res_add_size(realloc_head, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
744 }
Ram Paic8adf9a2011-02-14 17:43:20 -0800745 size0 = calculate_iosize(size, min_size, size1,
Ram Pai13583b12011-02-14 17:43:17 -0800746 resource_size(b_res), 4096);
Yinghai Lube768912011-07-25 13:08:38 -0700747 if (children_add_size > add_size)
748 add_size = children_add_size;
Ram Pai9e8bf932011-07-25 13:08:42 -0700749 size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
Yinghai Lua4ac9fe2012-01-21 02:08:17 -0800750 calculate_iosize(size, min_size, add_size + size1,
Ram Paic8adf9a2011-02-14 17:43:20 -0800751 resource_size(b_res), 4096);
752 if (!size0 && !size1) {
Bjorn Helgaas865df572009-11-04 10:32:57 -0700753 if (b_res->start || b_res->end)
754 dev_info(&bus->self->dev, "disabling bridge window "
755 "%pR to [bus %02x-%02x] (unused)\n", b_res,
756 bus->secondary, bus->subordinate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 b_res->flags = 0;
758 return;
759 }
760 /* Alignment of the IO window is always 4K */
761 b_res->start = 4096;
Ram Paic8adf9a2011-02-14 17:43:20 -0800762 b_res->end = b_res->start + size0 - 1;
Ivan Kokshaysky88452562008-03-30 19:50:14 +0400763 b_res->flags |= IORESOURCE_STARTALIGN;
Yinghai Lub5924432012-01-21 02:08:31 -0800764 if (size1 > size0 && realloc_head) {
Ram Pai9e8bf932011-07-25 13:08:42 -0700765 add_to_list(realloc_head, bus->self, b_res, size1-size0, 4096);
Yinghai Lub5924432012-01-21 02:08:31 -0800766 dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window "
767 "%pR to [bus %02x-%02x] add_size %lx\n", b_res,
768 bus->secondary, bus->subordinate, size1-size0);
769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
771
Ram Paic8adf9a2011-02-14 17:43:20 -0800772/**
773 * pbus_size_mem() - size the memory window of a given bus
774 *
775 * @bus : the bus
776 * @min_size : the minimum memory window that must to be allocated
777 * @add_size : additional optional memory window
Ram Pai9e8bf932011-07-25 13:08:42 -0700778 * @realloc_head : track the additional memory window on this list
Ram Paic8adf9a2011-02-14 17:43:20 -0800779 *
780 * Calculate the size of the bus and minimal alignment which
781 * guarantees that all child resources fit in this size.
782 */
Eric W. Biederman28760482009-09-09 14:09:24 -0700783static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
Ram Paic8adf9a2011-02-14 17:43:20 -0800784 unsigned long type, resource_size_t min_size,
785 resource_size_t add_size,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800786 struct list_head *realloc_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 struct pci_dev *dev;
Ram Paic8adf9a2011-02-14 17:43:20 -0800789 resource_size_t min_align, align, size, size0, size1;
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100790 resource_size_t aligns[12]; /* Alignments from 1Mb to 2Gb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 int order, max_order;
792 struct resource *b_res = find_free_bus_resource(bus, type);
Yinghai Lu1f82de12009-04-23 20:48:32 -0700793 unsigned int mem64_mask = 0;
Yinghai Lube768912011-07-25 13:08:38 -0700794 resource_size_t children_add_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 if (!b_res)
797 return 0;
798
799 memset(aligns, 0, sizeof(aligns));
800 max_order = 0;
801 size = 0;
802
Yinghai Lu1f82de12009-04-23 20:48:32 -0700803 mem64_mask = b_res->flags & IORESOURCE_MEM_64;
804 b_res->flags &= ~IORESOURCE_MEM_64;
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 list_for_each_entry(dev, &bus->devices, bus_list) {
807 int i;
Yinghai Lu1f82de12009-04-23 20:48:32 -0700808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
810 struct resource *r = &dev->resource[i];
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100811 resource_size_t r_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813 if (r->parent || (r->flags & mask) != type)
814 continue;
Zhao, Yu022edd82008-10-13 19:24:28 +0800815 r_size = resource_size(r);
Yinghai Lu2aceefc2011-07-25 13:08:40 -0700816#ifdef CONFIG_PCI_IOV
817 /* put SRIOV requested res to the optional list */
Ram Pai9e8bf932011-07-25 13:08:42 -0700818 if (realloc_head && i >= PCI_IOV_RESOURCES &&
Yinghai Lu2aceefc2011-07-25 13:08:40 -0700819 i <= PCI_IOV_RESOURCE_END) {
820 r->end = r->start - 1;
Ram Pai9e8bf932011-07-25 13:08:42 -0700821 add_to_list(realloc_head, dev, r, r_size, 0/* dont' care */);
Yinghai Lu2aceefc2011-07-25 13:08:40 -0700822 children_add_size += r_size;
823 continue;
824 }
825#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 /* For bridges size != alignment */
Chris Wright6faf17f2009-08-28 13:00:06 -0700827 align = pci_resource_alignment(dev, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 order = __ffs(align) - 20;
829 if (order > 11) {
Bjorn Helgaas865df572009-11-04 10:32:57 -0700830 dev_warn(&dev->dev, "disabling BAR %d: %pR "
831 "(bad alignment %#llx)\n", i, r,
832 (unsigned long long) align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 r->flags = 0;
834 continue;
835 }
836 size += r_size;
837 if (order < 0)
838 order = 0;
839 /* Exclude ranges with size > align from
840 calculation of the alignment. */
841 if (r_size == align)
842 aligns[order] += align;
843 if (order > max_order)
844 max_order = order;
Yinghai Lu1f82de12009-04-23 20:48:32 -0700845 mem64_mask &= r->flags & IORESOURCE_MEM_64;
Yinghai Lube768912011-07-25 13:08:38 -0700846
Ram Pai9e8bf932011-07-25 13:08:42 -0700847 if (realloc_head)
848 children_add_size += get_res_add_size(realloc_head, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 align = 0;
852 min_align = 0;
853 for (order = 0; order <= max_order; order++) {
Jeremy Fitzhardinge8308c542008-09-11 01:31:50 -0700854 resource_size_t align1 = 1;
855
856 align1 <<= (order + 20);
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (!align)
859 min_align = align1;
Milind Arun Choudhary6f6f8c22007-07-09 11:55:51 -0700860 else if (ALIGN(align + min_align, min_align) < align1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 min_align = align1 >> 1;
862 align += aligns[order];
863 }
Linus Torvaldsb42282e2011-04-11 10:53:11 -0700864 size0 = calculate_memsize(size, min_size, 0, resource_size(b_res), min_align);
Yinghai Lube768912011-07-25 13:08:38 -0700865 if (children_add_size > add_size)
866 add_size = children_add_size;
Ram Pai9e8bf932011-07-25 13:08:42 -0700867 size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
Yinghai Lua4ac9fe2012-01-21 02:08:17 -0800868 calculate_memsize(size, min_size, add_size,
Linus Torvaldsb42282e2011-04-11 10:53:11 -0700869 resource_size(b_res), min_align);
Ram Paic8adf9a2011-02-14 17:43:20 -0800870 if (!size0 && !size1) {
Bjorn Helgaas865df572009-11-04 10:32:57 -0700871 if (b_res->start || b_res->end)
872 dev_info(&bus->self->dev, "disabling bridge window "
873 "%pR to [bus %02x-%02x] (unused)\n", b_res,
874 bus->secondary, bus->subordinate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 b_res->flags = 0;
876 return 1;
877 }
878 b_res->start = min_align;
Ram Paic8adf9a2011-02-14 17:43:20 -0800879 b_res->end = size0 + min_align - 1;
880 b_res->flags |= IORESOURCE_STARTALIGN | mem64_mask;
Yinghai Lub5924432012-01-21 02:08:31 -0800881 if (size1 > size0 && realloc_head) {
Ram Pai9e8bf932011-07-25 13:08:42 -0700882 add_to_list(realloc_head, bus->self, b_res, size1-size0, min_align);
Yinghai Lub5924432012-01-21 02:08:31 -0800883 dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window "
884 "%pR to [bus %02x-%02x] add_size %llx\n", b_res,
885 bus->secondary, bus->subordinate, (unsigned long long)size1-size0);
886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return 1;
888}
889
Ram Pai0a2daa12011-07-25 13:08:41 -0700890unsigned long pci_cardbus_resource_alignment(struct resource *res)
891{
892 if (res->flags & IORESOURCE_IO)
893 return pci_cardbus_io_size;
894 if (res->flags & IORESOURCE_MEM)
895 return pci_cardbus_mem_size;
896 return 0;
897}
898
899static void pci_bus_size_cardbus(struct pci_bus *bus,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800900 struct list_head *realloc_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
902 struct pci_dev *bridge = bus->self;
903 struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
Yinghai Lu11848932012-02-10 15:33:47 -0800904 resource_size_t b_res_3_size = pci_cardbus_mem_size * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 u16 ctrl;
906
Yinghai Lu3796f1e2012-02-10 15:33:48 -0800907 if (b_res[0].parent)
908 goto handle_b_res_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 /*
910 * Reserve some resources for CardBus. We reserve
911 * a fixed amount of bus space for CardBus bridges.
912 */
Yinghai Lu11848932012-02-10 15:33:47 -0800913 b_res[0].start = pci_cardbus_io_size;
914 b_res[0].end = b_res[0].start + pci_cardbus_io_size - 1;
915 b_res[0].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
916 if (realloc_head) {
917 b_res[0].end -= pci_cardbus_io_size;
918 add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size,
919 pci_cardbus_io_size);
920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Yinghai Lu3796f1e2012-02-10 15:33:48 -0800922handle_b_res_1:
923 if (b_res[1].parent)
924 goto handle_b_res_2;
Yinghai Lu11848932012-02-10 15:33:47 -0800925 b_res[1].start = pci_cardbus_io_size;
926 b_res[1].end = b_res[1].start + pci_cardbus_io_size - 1;
927 b_res[1].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
928 if (realloc_head) {
929 b_res[1].end -= pci_cardbus_io_size;
930 add_to_list(realloc_head, bridge, b_res+1, pci_cardbus_io_size,
931 pci_cardbus_io_size);
932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Yinghai Lu3796f1e2012-02-10 15:33:48 -0800934handle_b_res_2:
Yinghai Ludcef0d02012-02-10 15:33:46 -0800935 /* MEM1 must not be pref mmio */
936 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
937 if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM1) {
938 ctrl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1;
939 pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
940 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
941 }
942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 /*
944 * Check whether prefetchable memory is supported
945 * by this bridge.
946 */
947 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
948 if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
949 ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
950 pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
951 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
952 }
953
Yinghai Lu3796f1e2012-02-10 15:33:48 -0800954 if (b_res[2].parent)
955 goto handle_b_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 /*
957 * If we have prefetchable memory support, allocate
958 * two regions. Otherwise, allocate one region of
959 * twice the size.
960 */
961 if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
Yinghai Lu11848932012-02-10 15:33:47 -0800962 b_res[2].start = pci_cardbus_mem_size;
963 b_res[2].end = b_res[2].start + pci_cardbus_mem_size - 1;
964 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH |
965 IORESOURCE_STARTALIGN;
966 if (realloc_head) {
967 b_res[2].end -= pci_cardbus_mem_size;
968 add_to_list(realloc_head, bridge, b_res+2,
969 pci_cardbus_mem_size, pci_cardbus_mem_size);
970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Yinghai Lu11848932012-02-10 15:33:47 -0800972 /* reduce that to half */
973 b_res_3_size = pci_cardbus_mem_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 }
Ram Pai0a2daa12011-07-25 13:08:41 -0700975
Yinghai Lu3796f1e2012-02-10 15:33:48 -0800976handle_b_res_3:
977 if (b_res[3].parent)
978 goto handle_done;
Yinghai Lu11848932012-02-10 15:33:47 -0800979 b_res[3].start = pci_cardbus_mem_size;
980 b_res[3].end = b_res[3].start + b_res_3_size - 1;
981 b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_STARTALIGN;
982 if (realloc_head) {
983 b_res[3].end -= b_res_3_size;
984 add_to_list(realloc_head, bridge, b_res+3, b_res_3_size,
985 pci_cardbus_mem_size);
986 }
Yinghai Lu3796f1e2012-02-10 15:33:48 -0800987
988handle_done:
989 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
991
Ram Paic8adf9a2011-02-14 17:43:20 -0800992void __ref __pci_bus_size_bridges(struct pci_bus *bus,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800993 struct list_head *realloc_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
995 struct pci_dev *dev;
996 unsigned long mask, prefmask;
Ram Paic8adf9a2011-02-14 17:43:20 -0800997 resource_size_t additional_mem_size = 0, additional_io_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 list_for_each_entry(dev, &bus->devices, bus_list) {
1000 struct pci_bus *b = dev->subordinate;
1001 if (!b)
1002 continue;
1003
1004 switch (dev->class >> 8) {
1005 case PCI_CLASS_BRIDGE_CARDBUS:
Ram Pai9e8bf932011-07-25 13:08:42 -07001006 pci_bus_size_cardbus(b, realloc_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 break;
1008
1009 case PCI_CLASS_BRIDGE_PCI:
1010 default:
Ram Pai9e8bf932011-07-25 13:08:42 -07001011 __pci_bus_size_bridges(b, realloc_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 break;
1013 }
1014 }
1015
1016 /* The root bus? */
1017 if (!bus->self)
1018 return;
1019
1020 switch (bus->self->class >> 8) {
1021 case PCI_CLASS_BRIDGE_CARDBUS:
1022 /* don't size cardbuses yet. */
1023 break;
1024
1025 case PCI_CLASS_BRIDGE_PCI:
1026 pci_bridge_check_ranges(bus);
Eric W. Biederman28760482009-09-09 14:09:24 -07001027 if (bus->self->is_hotplug_bridge) {
Ram Paic8adf9a2011-02-14 17:43:20 -08001028 additional_io_size = pci_hotplug_io_size;
1029 additional_mem_size = pci_hotplug_mem_size;
Eric W. Biederman28760482009-09-09 14:09:24 -07001030 }
Ram Paic8adf9a2011-02-14 17:43:20 -08001031 /*
1032 * Follow thru
1033 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 default:
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001035 pbus_size_io(bus, realloc_head ? 0 : additional_io_size,
1036 additional_io_size, realloc_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 /* If the bridge supports prefetchable range, size it
1038 separately. If it doesn't, or its prefetchable window
1039 has already been allocated by arch code, try
1040 non-prefetchable range for both types of PCI memory
1041 resources. */
1042 mask = IORESOURCE_MEM;
1043 prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001044 if (pbus_size_mem(bus, prefmask, prefmask,
1045 realloc_head ? 0 : additional_mem_size,
1046 additional_mem_size, realloc_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 mask = prefmask; /* Success, size non-prefetch only. */
Eric W. Biederman28760482009-09-09 14:09:24 -07001048 else
Ram Paic8adf9a2011-02-14 17:43:20 -08001049 additional_mem_size += additional_mem_size;
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001050 pbus_size_mem(bus, mask, IORESOURCE_MEM,
1051 realloc_head ? 0 : additional_mem_size,
1052 additional_mem_size, realloc_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 break;
1054 }
1055}
Ram Paic8adf9a2011-02-14 17:43:20 -08001056
1057void __ref pci_bus_size_bridges(struct pci_bus *bus)
1058{
1059 __pci_bus_size_bridges(bus, NULL);
1060}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061EXPORT_SYMBOL(pci_bus_size_bridges);
1062
Yinghai Lu568ddef2010-01-22 01:02:21 -08001063static void __ref __pci_bus_assign_resources(const struct pci_bus *bus,
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001064 struct list_head *realloc_head,
1065 struct list_head *fail_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
1067 struct pci_bus *b;
1068 struct pci_dev *dev;
1069
Ram Pai9e8bf932011-07-25 13:08:42 -07001070 pbus_assign_resources_sorted(bus, realloc_head, fail_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 list_for_each_entry(dev, &bus->devices, bus_list) {
1073 b = dev->subordinate;
1074 if (!b)
1075 continue;
1076
Ram Pai9e8bf932011-07-25 13:08:42 -07001077 __pci_bus_assign_resources(b, realloc_head, fail_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 switch (dev->class >> 8) {
1080 case PCI_CLASS_BRIDGE_PCI:
Yinghai Lu6841ec62010-01-22 01:02:25 -08001081 if (!pci_is_enabled(dev))
1082 pci_setup_bridge(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 break;
1084
1085 case PCI_CLASS_BRIDGE_CARDBUS:
1086 pci_setup_cardbus(b);
1087 break;
1088
1089 default:
Bjorn Helgaas80ccba12008-06-13 10:52:11 -06001090 dev_info(&dev->dev, "not setting up bridge for bus "
1091 "%04x:%02x\n", pci_domain_nr(b), b->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 break;
1093 }
1094 }
1095}
Yinghai Lu568ddef2010-01-22 01:02:21 -08001096
1097void __ref pci_bus_assign_resources(const struct pci_bus *bus)
1098{
Ram Paic8adf9a2011-02-14 17:43:20 -08001099 __pci_bus_assign_resources(bus, NULL, NULL);
Yinghai Lu568ddef2010-01-22 01:02:21 -08001100}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101EXPORT_SYMBOL(pci_bus_assign_resources);
1102
Yinghai Lu6841ec62010-01-22 01:02:25 -08001103static void __ref __pci_bridge_assign_resources(const struct pci_dev *bridge,
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001104 struct list_head *add_head,
1105 struct list_head *fail_head)
Yinghai Lu6841ec62010-01-22 01:02:25 -08001106{
1107 struct pci_bus *b;
1108
Yinghai Lu8424d752012-01-21 02:08:21 -08001109 pdev_assign_resources_sorted((struct pci_dev *)bridge,
1110 add_head, fail_head);
Yinghai Lu6841ec62010-01-22 01:02:25 -08001111
1112 b = bridge->subordinate;
1113 if (!b)
1114 return;
1115
Yinghai Lu8424d752012-01-21 02:08:21 -08001116 __pci_bus_assign_resources(b, add_head, fail_head);
Yinghai Lu6841ec62010-01-22 01:02:25 -08001117
1118 switch (bridge->class >> 8) {
1119 case PCI_CLASS_BRIDGE_PCI:
1120 pci_setup_bridge(b);
1121 break;
1122
1123 case PCI_CLASS_BRIDGE_CARDBUS:
1124 pci_setup_cardbus(b);
1125 break;
1126
1127 default:
1128 dev_info(&bridge->dev, "not setting up bridge for bus "
1129 "%04x:%02x\n", pci_domain_nr(b), b->number);
1130 break;
1131 }
1132}
Yinghai Lu5009b462010-01-22 01:02:20 -08001133static void pci_bridge_release_resources(struct pci_bus *bus,
1134 unsigned long type)
1135{
1136 int idx;
1137 bool changed = false;
1138 struct pci_dev *dev;
1139 struct resource *r;
1140 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1141 IORESOURCE_PREFETCH;
1142
1143 dev = bus->self;
1144 for (idx = PCI_BRIDGE_RESOURCES; idx <= PCI_BRIDGE_RESOURCE_END;
1145 idx++) {
1146 r = &dev->resource[idx];
1147 if ((r->flags & type_mask) != type)
1148 continue;
1149 if (!r->parent)
1150 continue;
1151 /*
1152 * if there are children under that, we should release them
1153 * all
1154 */
1155 release_child_resources(r);
1156 if (!release_resource(r)) {
1157 dev_printk(KERN_DEBUG, &dev->dev,
1158 "resource %d %pR released\n", idx, r);
1159 /* keep the old size */
1160 r->end = resource_size(r) - 1;
1161 r->start = 0;
1162 r->flags = 0;
1163 changed = true;
1164 }
1165 }
1166
1167 if (changed) {
1168 /* avoiding touch the one without PREF */
1169 if (type & IORESOURCE_PREFETCH)
1170 type = IORESOURCE_PREFETCH;
1171 __pci_setup_bridge(bus, type);
1172 }
1173}
1174
1175enum release_type {
1176 leaf_only,
1177 whole_subtree,
1178};
1179/*
1180 * try to release pci bridge resources that is from leaf bridge,
1181 * so we can allocate big new one later
1182 */
1183static void __ref pci_bus_release_bridge_resources(struct pci_bus *bus,
1184 unsigned long type,
1185 enum release_type rel_type)
1186{
1187 struct pci_dev *dev;
1188 bool is_leaf_bridge = true;
1189
1190 list_for_each_entry(dev, &bus->devices, bus_list) {
1191 struct pci_bus *b = dev->subordinate;
1192 if (!b)
1193 continue;
1194
1195 is_leaf_bridge = false;
1196
1197 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
1198 continue;
1199
1200 if (rel_type == whole_subtree)
1201 pci_bus_release_bridge_resources(b, type,
1202 whole_subtree);
1203 }
1204
1205 if (pci_is_root_bus(bus))
1206 return;
1207
1208 if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
1209 return;
1210
1211 if ((rel_type == whole_subtree) || is_leaf_bridge)
1212 pci_bridge_release_resources(bus, type);
1213}
1214
Yinghai Lu76fbc262008-06-23 20:33:06 +02001215static void pci_bus_dump_res(struct pci_bus *bus)
1216{
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -07001217 struct resource *res;
1218 int i;
Yinghai Lu76fbc262008-06-23 20:33:06 +02001219
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -07001220 pci_bus_for_each_resource(bus, res, i) {
Yinghai Lu7c9342b2009-12-22 15:02:24 -08001221 if (!res || !res->end || !res->flags)
Yinghai Lu76fbc262008-06-23 20:33:06 +02001222 continue;
1223
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001224 dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res);
Yinghai Lu76fbc262008-06-23 20:33:06 +02001225 }
1226}
1227
1228static void pci_bus_dump_resources(struct pci_bus *bus)
1229{
1230 struct pci_bus *b;
1231 struct pci_dev *dev;
1232
1233
1234 pci_bus_dump_res(bus);
1235
1236 list_for_each_entry(dev, &bus->devices, bus_list) {
1237 b = dev->subordinate;
1238 if (!b)
1239 continue;
1240
1241 pci_bus_dump_resources(b);
1242 }
1243}
1244
Yinghai Luda7822e2011-05-12 17:11:37 -07001245static int __init pci_bus_get_depth(struct pci_bus *bus)
1246{
1247 int depth = 0;
1248 struct pci_dev *dev;
1249
1250 list_for_each_entry(dev, &bus->devices, bus_list) {
1251 int ret;
1252 struct pci_bus *b = dev->subordinate;
1253 if (!b)
1254 continue;
1255
1256 ret = pci_bus_get_depth(b);
1257 if (ret + 1 > depth)
1258 depth = ret + 1;
1259 }
1260
1261 return depth;
1262}
1263static int __init pci_get_max_depth(void)
1264{
1265 int depth = 0;
1266 struct pci_bus *bus;
1267
1268 list_for_each_entry(bus, &pci_root_buses, node) {
1269 int ret;
1270
1271 ret = pci_bus_get_depth(bus);
1272 if (ret > depth)
1273 depth = ret;
1274 }
1275
1276 return depth;
1277}
1278
Ram Paif483d392011-07-07 11:19:10 -07001279
Yinghai Luda7822e2011-05-12 17:11:37 -07001280/*
1281 * first try will not touch pci bridge res
1282 * second and later try will clear small leaf bridge res
1283 * will stop till to the max deepth if can not find good one
1284 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285void __init
1286pci_assign_unassigned_resources(void)
1287{
1288 struct pci_bus *bus;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001289 LIST_HEAD(realloc_head); /* list of resources that
Ram Paic8adf9a2011-02-14 17:43:20 -08001290 want additional resources */
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001291 struct list_head *add_list = NULL;
Yinghai Luda7822e2011-05-12 17:11:37 -07001292 int tried_times = 0;
1293 enum release_type rel_type = leaf_only;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001294 LIST_HEAD(fail_head);
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001295 struct pci_dev_resource *fail_res;
Yinghai Luda7822e2011-05-12 17:11:37 -07001296 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1297 IORESOURCE_PREFETCH;
1298 unsigned long failed_type;
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001299 int pci_try_num = 1;
Yinghai Luda7822e2011-05-12 17:11:37 -07001300
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001301 /* don't realloc if asked to do so */
1302 if (pci_realloc_enabled()) {
1303 int max_depth = pci_get_max_depth();
1304
1305 pci_try_num = max_depth + 1;
1306 printk(KERN_DEBUG "PCI: max bus depth: %d pci_try_num: %d\n",
1307 max_depth, pci_try_num);
1308 }
Yinghai Luda7822e2011-05-12 17:11:37 -07001309
1310again:
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001311 /*
1312 * last try will use add_list, otherwise will try good to have as
1313 * must have, so can realloc parent bridge resource
1314 */
1315 if (tried_times + 1 == pci_try_num)
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001316 add_list = &realloc_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 /* Depth first, calculate sizes and alignments of all
1318 subordinate buses. */
Yinghai Luda7822e2011-05-12 17:11:37 -07001319 list_for_each_entry(bus, &pci_root_buses, node)
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001320 __pci_bus_size_bridges(bus, add_list);
Ram Paic8adf9a2011-02-14 17:43:20 -08001321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 /* Depth last, allocate resources and update the hardware. */
Yinghai Luda7822e2011-05-12 17:11:37 -07001323 list_for_each_entry(bus, &pci_root_buses, node)
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001324 __pci_bus_assign_resources(bus, add_list, &fail_head);
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001325 if (add_list)
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001326 BUG_ON(!list_empty(add_list));
Yinghai Luda7822e2011-05-12 17:11:37 -07001327 tried_times++;
1328
1329 /* any device complain? */
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001330 if (list_empty(&fail_head))
Yinghai Luda7822e2011-05-12 17:11:37 -07001331 goto enable_and_dump;
Ram Paif483d392011-07-07 11:19:10 -07001332
Yinghai Luda7822e2011-05-12 17:11:37 -07001333 failed_type = 0;
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001334 list_for_each_entry(fail_res, &fail_head, list)
1335 failed_type |= fail_res->flags;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001336
Yinghai Luda7822e2011-05-12 17:11:37 -07001337 /*
1338 * io port are tight, don't try extra
1339 * or if reach the limit, don't want to try more
1340 */
1341 failed_type &= type_mask;
1342 if ((failed_type == IORESOURCE_IO) || (tried_times >= pci_try_num)) {
Yinghai Lubffc56d2012-01-21 02:08:30 -08001343 free_list(&fail_head);
Yinghai Luda7822e2011-05-12 17:11:37 -07001344 goto enable_and_dump;
1345 }
1346
1347 printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
1348 tried_times + 1);
1349
1350 /* third times and later will not check if it is leaf */
1351 if ((tried_times + 1) > 2)
1352 rel_type = whole_subtree;
1353
1354 /*
1355 * Try to release leaf bridge's resources that doesn't fit resource of
1356 * child device under that bridge
1357 */
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001358 list_for_each_entry(fail_res, &fail_head, list) {
1359 bus = fail_res->dev->bus;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001360 pci_bus_release_bridge_resources(bus,
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001361 fail_res->flags & type_mask,
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001362 rel_type);
Yinghai Luda7822e2011-05-12 17:11:37 -07001363 }
1364 /* restore size and flags */
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001365 list_for_each_entry(fail_res, &fail_head, list) {
1366 struct resource *res = fail_res->res;
Yinghai Luda7822e2011-05-12 17:11:37 -07001367
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001368 res->start = fail_res->start;
1369 res->end = fail_res->end;
1370 res->flags = fail_res->flags;
1371 if (fail_res->dev->subordinate)
Yinghai Luda7822e2011-05-12 17:11:37 -07001372 res->flags = 0;
Yinghai Luda7822e2011-05-12 17:11:37 -07001373 }
Yinghai Lubffc56d2012-01-21 02:08:30 -08001374 free_list(&fail_head);
Yinghai Luda7822e2011-05-12 17:11:37 -07001375
1376 goto again;
1377
1378enable_and_dump:
1379 /* Depth last, update the hardware. */
1380 list_for_each_entry(bus, &pci_root_buses, node)
1381 pci_enable_bridges(bus);
Yinghai Lu76fbc262008-06-23 20:33:06 +02001382
1383 /* dump the resource on buses */
Yinghai Luda7822e2011-05-12 17:11:37 -07001384 list_for_each_entry(bus, &pci_root_buses, node)
Yinghai Lu76fbc262008-06-23 20:33:06 +02001385 pci_bus_dump_resources(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
Yinghai Lu6841ec62010-01-22 01:02:25 -08001387
1388void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
1389{
1390 struct pci_bus *parent = bridge->subordinate;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001391 LIST_HEAD(add_list); /* list of resources that
Yinghai Lu8424d752012-01-21 02:08:21 -08001392 want additional resources */
Yinghai Lu32180e402010-01-22 01:02:27 -08001393 int tried_times = 0;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001394 LIST_HEAD(fail_head);
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001395 struct pci_dev_resource *fail_res;
Yinghai Lu6841ec62010-01-22 01:02:25 -08001396 int retval;
Yinghai Lu32180e402010-01-22 01:02:27 -08001397 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1398 IORESOURCE_PREFETCH;
Yinghai Lu6841ec62010-01-22 01:02:25 -08001399
Yinghai Lu32180e402010-01-22 01:02:27 -08001400again:
Yinghai Lu8424d752012-01-21 02:08:21 -08001401 __pci_bus_size_bridges(parent, &add_list);
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001402 __pci_bridge_assign_resources(bridge, &add_list, &fail_head);
1403 BUG_ON(!list_empty(&add_list));
Yinghai Lu32180e402010-01-22 01:02:27 -08001404 tried_times++;
1405
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001406 if (list_empty(&fail_head))
Yinghai Lu3f579c32010-05-21 14:35:06 -07001407 goto enable_all;
Yinghai Lu32180e402010-01-22 01:02:27 -08001408
1409 if (tried_times >= 2) {
1410 /* still fail, don't need to try more */
Yinghai Lubffc56d2012-01-21 02:08:30 -08001411 free_list(&fail_head);
Yinghai Lu3f579c32010-05-21 14:35:06 -07001412 goto enable_all;
Yinghai Lu32180e402010-01-22 01:02:27 -08001413 }
1414
1415 printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
1416 tried_times + 1);
1417
1418 /*
1419 * Try to release leaf bridge's resources that doesn't fit resource of
1420 * child device under that bridge
1421 */
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001422 list_for_each_entry(fail_res, &fail_head, list) {
1423 struct pci_bus *bus = fail_res->dev->bus;
1424 unsigned long flags = fail_res->flags;
Yinghai Lu32180e402010-01-22 01:02:27 -08001425
1426 pci_bus_release_bridge_resources(bus, flags & type_mask,
1427 whole_subtree);
Yinghai Lu32180e402010-01-22 01:02:27 -08001428 }
1429 /* restore size and flags */
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001430 list_for_each_entry(fail_res, &fail_head, list) {
1431 struct resource *res = fail_res->res;
Yinghai Lu32180e402010-01-22 01:02:27 -08001432
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001433 res->start = fail_res->start;
1434 res->end = fail_res->end;
1435 res->flags = fail_res->flags;
1436 if (fail_res->dev->subordinate)
Yinghai Lu32180e402010-01-22 01:02:27 -08001437 res->flags = 0;
Yinghai Lu32180e402010-01-22 01:02:27 -08001438 }
Yinghai Lubffc56d2012-01-21 02:08:30 -08001439 free_list(&fail_head);
Yinghai Lu32180e402010-01-22 01:02:27 -08001440
1441 goto again;
Yinghai Lu3f579c32010-05-21 14:35:06 -07001442
1443enable_all:
1444 retval = pci_reenable_device(bridge);
1445 pci_set_master(bridge);
1446 pci_enable_bridges(parent);
Yinghai Lu6841ec62010-01-22 01:02:25 -08001447}
1448EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
Yinghai Lu9b030882012-01-21 02:08:23 -08001449
1450#ifdef CONFIG_HOTPLUG
1451/**
1452 * pci_rescan_bus - scan a PCI bus for devices.
1453 * @bus: PCI bus to scan
1454 *
1455 * Scan a PCI bus and child buses for new devices, adds them,
1456 * and enables them.
1457 *
1458 * Returns the max number of subordinate bus discovered.
1459 */
1460unsigned int __ref pci_rescan_bus(struct pci_bus *bus)
1461{
1462 unsigned int max;
1463 struct pci_dev *dev;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001464 LIST_HEAD(add_list); /* list of resources that
Yinghai Lu9b030882012-01-21 02:08:23 -08001465 want additional resources */
1466
1467 max = pci_scan_child_bus(bus);
1468
Yinghai Lu9b030882012-01-21 02:08:23 -08001469 down_read(&pci_bus_sem);
1470 list_for_each_entry(dev, &bus->devices, bus_list)
1471 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
1472 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
1473 if (dev->subordinate)
1474 __pci_bus_size_bridges(dev->subordinate,
1475 &add_list);
1476 up_read(&pci_bus_sem);
1477 __pci_bus_assign_resources(bus, &add_list, NULL);
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001478 BUG_ON(!list_empty(&add_list));
Yinghai Lu9b030882012-01-21 02:08:23 -08001479
1480 pci_enable_bridges(bus);
1481 pci_bus_add_devices(bus);
1482
1483 return max;
1484}
1485EXPORT_SYMBOL_GPL(pci_rescan_bus);
1486#endif