blob: d5897c32f6697ea343f7c0ff45d4d5c30cfeb5e1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/pci/setup-bus.c
3 *
4 * Extruded from code written by
5 * Dave Rusling (david.rusling@reo.mts.dec.com)
6 * David Mosberger (davidm@cs.arizona.edu)
7 * David Miller (davem@redhat.com)
8 *
9 * Support routines for initializing a PCI subsystem.
10 */
11
12/*
13 * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
14 * PCI-PCI bridges cleanup, sorted resource allocation.
15 * Feb 2002, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
16 * Converted to allocation in 3 passes, which gives
17 * tighter packing. Prefetchable range support.
18 */
19
20#include <linux/init.h>
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/pci.h>
24#include <linux/errno.h>
25#include <linux/ioport.h>
26#include <linux/cache.h>
27#include <linux/slab.h>
Chris Wright6faf17f2009-08-28 13:00:06 -070028#include "pci.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Yinghai Lubdc4abe2012-01-21 02:08:27 -080030struct pci_dev_resource {
31 struct list_head list;
Yinghai Lu2934a0d2012-01-21 02:08:26 -080032 struct resource *res;
33 struct pci_dev *dev;
Yinghai Lu568ddef2010-01-22 01:02:21 -080034 resource_size_t start;
35 resource_size_t end;
Ram Paic8adf9a2011-02-14 17:43:20 -080036 resource_size_t add_size;
Ram Pai2bbc6942011-07-25 13:08:39 -070037 resource_size_t min_align;
Yinghai Lu568ddef2010-01-22 01:02:21 -080038 unsigned long flags;
39};
40
Yinghai Lubffc56d2012-01-21 02:08:30 -080041static void free_list(struct list_head *head)
42{
43 struct pci_dev_resource *dev_res, *tmp;
44
45 list_for_each_entry_safe(dev_res, tmp, head, list) {
46 list_del(&dev_res->list);
47 kfree(dev_res);
48 }
49}
Ram Pai094732a2011-02-14 17:43:18 -080050
Ram Paif483d392011-07-07 11:19:10 -070051int pci_realloc_enable = 0;
52#define pci_realloc_enabled() pci_realloc_enable
53void pci_realloc(void)
54{
55 pci_realloc_enable = 1;
56}
57
Ram Paic8adf9a2011-02-14 17:43:20 -080058/**
59 * add_to_list() - add a new resource tracker to the list
60 * @head: Head of the list
61 * @dev: device corresponding to which the resource
62 * belongs
63 * @res: The resource to be tracked
64 * @add_size: additional size to be optionally added
65 * to the resource
66 */
Yinghai Lubdc4abe2012-01-21 02:08:27 -080067static int add_to_list(struct list_head *head,
Ram Paic8adf9a2011-02-14 17:43:20 -080068 struct pci_dev *dev, struct resource *res,
Ram Pai2bbc6942011-07-25 13:08:39 -070069 resource_size_t add_size, resource_size_t min_align)
Yinghai Lu568ddef2010-01-22 01:02:21 -080070{
Yinghai Lu764242a2012-01-21 02:08:28 -080071 struct pci_dev_resource *tmp;
Yinghai Lu568ddef2010-01-22 01:02:21 -080072
Yinghai Lubdc4abe2012-01-21 02:08:27 -080073 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
Yinghai Lu568ddef2010-01-22 01:02:21 -080074 if (!tmp) {
Ram Paic8adf9a2011-02-14 17:43:20 -080075 pr_warning("add_to_list: kmalloc() failed!\n");
Yinghai Luef62dfe2012-01-21 02:08:18 -080076 return -ENOMEM;
Yinghai Lu568ddef2010-01-22 01:02:21 -080077 }
78
Yinghai Lu568ddef2010-01-22 01:02:21 -080079 tmp->res = res;
80 tmp->dev = dev;
81 tmp->start = res->start;
82 tmp->end = res->end;
83 tmp->flags = res->flags;
Ram Paic8adf9a2011-02-14 17:43:20 -080084 tmp->add_size = add_size;
Ram Pai2bbc6942011-07-25 13:08:39 -070085 tmp->min_align = min_align;
Yinghai Lubdc4abe2012-01-21 02:08:27 -080086
87 list_add(&tmp->list, head);
Yinghai Luef62dfe2012-01-21 02:08:18 -080088
89 return 0;
Yinghai Lu568ddef2010-01-22 01:02:21 -080090}
91
Yinghai Lub9b0bba2012-01-21 02:08:29 -080092static void remove_from_list(struct list_head *head,
Yinghai Lu3e6e0d82012-01-21 02:08:20 -080093 struct resource *res)
94{
Yinghai Lub9b0bba2012-01-21 02:08:29 -080095 struct pci_dev_resource *dev_res, *tmp;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -080096
Yinghai Lub9b0bba2012-01-21 02:08:29 -080097 list_for_each_entry_safe(dev_res, tmp, head, list) {
98 if (dev_res->res == res) {
99 list_del(&dev_res->list);
100 kfree(dev_res);
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800101 break;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800102 }
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800103 }
104}
105
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800106static resource_size_t get_res_add_size(struct list_head *head,
Yinghai Lu1c372352012-01-21 02:08:19 -0800107 struct resource *res)
108{
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800109 struct pci_dev_resource *dev_res;
Yinghai Lu1c372352012-01-21 02:08:19 -0800110
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800111 list_for_each_entry(dev_res, head, list) {
112 if (dev_res->res == res) {
Yinghai Lub5924432012-01-21 02:08:31 -0800113 int idx = res - &dev_res->dev->resource[0];
114
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800115 dev_printk(KERN_DEBUG, &dev_res->dev->dev,
Yinghai Lub5924432012-01-21 02:08:31 -0800116 "res[%d]=%pR get_res_add_size add_size %llx\n",
117 idx, dev_res->res,
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800118 (unsigned long long)dev_res->add_size);
Yinghai Lub5924432012-01-21 02:08:31 -0800119
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800120 return dev_res->add_size;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800121 }
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800122 }
Yinghai Lu1c372352012-01-21 02:08:19 -0800123
124 return 0;
125}
126
Yinghai Lu78c3b322012-01-21 02:08:25 -0800127/* Sort resources by alignment */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800128static void pdev_sort_resources(struct pci_dev *dev, struct list_head *head)
Yinghai Lu78c3b322012-01-21 02:08:25 -0800129{
130 int i;
131
132 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
133 struct resource *r;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800134 struct pci_dev_resource *dev_res, *tmp;
Yinghai Lu78c3b322012-01-21 02:08:25 -0800135 resource_size_t r_align;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800136 struct list_head *n;
Yinghai Lu78c3b322012-01-21 02:08:25 -0800137
138 r = &dev->resource[i];
139
140 if (r->flags & IORESOURCE_PCI_FIXED)
141 continue;
142
143 if (!(r->flags) || r->parent)
144 continue;
145
146 r_align = pci_resource_alignment(dev, r);
147 if (!r_align) {
148 dev_warn(&dev->dev, "BAR %d: %pR has bogus alignment\n",
149 i, r);
150 continue;
151 }
Yinghai Lu78c3b322012-01-21 02:08:25 -0800152
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800153 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
154 if (!tmp)
155 panic("pdev_sort_resources(): "
156 "kmalloc() failed!\n");
157 tmp->res = r;
158 tmp->dev = dev;
159
160 /* fallback is smallest one or list is empty*/
161 n = head;
162 list_for_each_entry(dev_res, head, list) {
163 resource_size_t align;
164
165 align = pci_resource_alignment(dev_res->dev,
166 dev_res->res);
Yinghai Lu78c3b322012-01-21 02:08:25 -0800167
168 if (r_align > align) {
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800169 n = &dev_res->list;
Yinghai Lu78c3b322012-01-21 02:08:25 -0800170 break;
171 }
172 }
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800173 /* Insert it just before n*/
174 list_add_tail(&tmp->list, n);
Yinghai Lu78c3b322012-01-21 02:08:25 -0800175 }
176}
177
Yinghai Lu6841ec62010-01-22 01:02:25 -0800178static void __dev_sort_resources(struct pci_dev *dev,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800179 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Yinghai Lu6841ec62010-01-22 01:02:25 -0800181 u16 class = dev->class >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Yinghai Lu6841ec62010-01-22 01:02:25 -0800183 /* Don't touch classless devices or host bridges or ioapics. */
184 if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST)
185 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Yinghai Lu6841ec62010-01-22 01:02:25 -0800187 /* Don't touch ioapic devices already enabled by firmware */
188 if (class == PCI_CLASS_SYSTEM_PIC) {
189 u16 command;
190 pci_read_config_word(dev, PCI_COMMAND, &command);
191 if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
192 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 }
194
Yinghai Lu6841ec62010-01-22 01:02:25 -0800195 pdev_sort_resources(dev, head);
196}
197
Ram Paifc075e12011-02-14 17:43:19 -0800198static inline void reset_resource(struct resource *res)
199{
200 res->start = 0;
201 res->end = 0;
202 res->flags = 0;
203}
204
Ram Paic8adf9a2011-02-14 17:43:20 -0800205/**
Ram Pai9e8bf932011-07-25 13:08:42 -0700206 * reassign_resources_sorted() - satisfy any additional resource requests
Ram Paic8adf9a2011-02-14 17:43:20 -0800207 *
Ram Pai9e8bf932011-07-25 13:08:42 -0700208 * @realloc_head : head of the list tracking requests requiring additional
Ram Paic8adf9a2011-02-14 17:43:20 -0800209 * resources
210 * @head : head of the list tracking requests with allocated
211 * resources
212 *
Ram Pai9e8bf932011-07-25 13:08:42 -0700213 * Walk through each element of the realloc_head and try to procure
Ram Paic8adf9a2011-02-14 17:43:20 -0800214 * additional resources for the element, provided the element
215 * is in the head list.
216 */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800217static void reassign_resources_sorted(struct list_head *realloc_head,
218 struct list_head *head)
Ram Paic8adf9a2011-02-14 17:43:20 -0800219{
220 struct resource *res;
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800221 struct pci_dev_resource *add_res, *tmp;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800222 struct pci_dev_resource *dev_res;
Ram Paic8adf9a2011-02-14 17:43:20 -0800223 resource_size_t add_size;
224 int idx;
225
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800226 list_for_each_entry_safe(add_res, tmp, realloc_head, list) {
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800227 bool found_match = false;
228
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800229 res = add_res->res;
Ram Paic8adf9a2011-02-14 17:43:20 -0800230 /* skip resource that has been reset */
231 if (!res->flags)
232 goto out;
233
234 /* skip this resource if not found in head list */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800235 list_for_each_entry(dev_res, head, list) {
236 if (dev_res->res == res) {
237 found_match = true;
238 break;
239 }
Ram Paic8adf9a2011-02-14 17:43:20 -0800240 }
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800241 if (!found_match)/* just skip */
242 continue;
Ram Paic8adf9a2011-02-14 17:43:20 -0800243
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800244 idx = res - &add_res->dev->resource[0];
245 add_size = add_res->add_size;
Ram Pai2bbc6942011-07-25 13:08:39 -0700246 if (!resource_size(res)) {
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800247 res->start = add_res->start;
Ram Pai2bbc6942011-07-25 13:08:39 -0700248 res->end = res->start + add_size - 1;
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800249 if (pci_assign_resource(add_res->dev, idx))
Ram Paic8adf9a2011-02-14 17:43:20 -0800250 reset_resource(res);
Ram Pai2bbc6942011-07-25 13:08:39 -0700251 } else {
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800252 resource_size_t align = add_res->min_align;
253 res->flags |= add_res->flags &
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800254 (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN);
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800255 if (pci_reassign_resource(add_res->dev, idx,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800256 add_size, align))
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800257 dev_printk(KERN_DEBUG, &add_res->dev->dev,
Yinghai Lub5924432012-01-21 02:08:31 -0800258 "failed to add %llx res[%d]=%pR\n",
259 (unsigned long long)add_size,
260 idx, res);
Ram Paic8adf9a2011-02-14 17:43:20 -0800261 }
262out:
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800263 list_del(&add_res->list);
264 kfree(add_res);
Ram Paic8adf9a2011-02-14 17:43:20 -0800265 }
266}
267
268/**
269 * assign_requested_resources_sorted() - satisfy resource requests
270 *
271 * @head : head of the list tracking requests for resources
272 * @failed_list : head of the list tracking requests that could
273 * not be allocated
274 *
275 * Satisfy resource requests of each element in the list. Add
276 * requests that could not satisfied to the failed_list.
277 */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800278static void assign_requested_resources_sorted(struct list_head *head,
279 struct list_head *fail_head)
Yinghai Lu6841ec62010-01-22 01:02:25 -0800280{
281 struct resource *res;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800282 struct pci_dev_resource *dev_res;
Yinghai Lu6841ec62010-01-22 01:02:25 -0800283 int idx;
284
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800285 list_for_each_entry(dev_res, head, list) {
286 res = dev_res->res;
287 idx = res - &dev_res->dev->resource[0];
288 if (resource_size(res) &&
289 pci_assign_resource(dev_res->dev, idx)) {
290 if (fail_head && !pci_is_root_bus(dev_res->dev->bus)) {
Yinghai Lu9a928662010-02-28 15:49:39 -0800291 /*
292 * if the failed res is for ROM BAR, and it will
293 * be enabled later, don't add it to the list
294 */
295 if (!((idx == PCI_ROM_RESOURCE) &&
296 (!(res->flags & IORESOURCE_ROM_ENABLE))))
Yinghai Lu67cc7e22012-01-21 02:08:32 -0800297 add_to_list(fail_head,
298 dev_res->dev, res,
299 0 /* dont care */,
300 0 /* dont care */);
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 Lub9b0bba2012-01-21 02:08:29 -0800322 struct pci_dev_resource *save_res;
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 Lubffc56d2012-01-21 02:08:30 -0800332 free_list(&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 Lubffc56d2012-01-21 02:08:30 -0800350 free_list(&save_head);
351 free_list(head);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800352 return;
353 }
354
Yinghai Lubffc56d2012-01-21 02:08:30 -0800355 free_list(&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 Lub9b0bba2012-01-21 02:08:29 -0800361 list_for_each_entry(save_res, &save_head, list) {
362 struct resource *res = save_res->res;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800363
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800364 res->start = save_res->start;
365 res->end = save_res->end;
366 res->flags = save_res->flags;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800367 }
Yinghai Lubffc56d2012-01-21 02:08:30 -0800368 free_list(&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 Lubffc56d2012-01-21 02:08:30 -0800378 free_list(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;
Yinghai Lub5924432012-01-21 02:08:31 -0800761 if (size1 > size0 && realloc_head) {
Ram Pai9e8bf932011-07-25 13:08:42 -0700762 add_to_list(realloc_head, bus->self, b_res, size1-size0, 4096);
Yinghai Lub5924432012-01-21 02:08:31 -0800763 dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window "
764 "%pR to [bus %02x-%02x] add_size %lx\n", b_res,
765 bus->secondary, bus->subordinate, size1-size0);
766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
Ram Paic8adf9a2011-02-14 17:43:20 -0800769/**
770 * pbus_size_mem() - size the memory window of a given bus
771 *
772 * @bus : the bus
773 * @min_size : the minimum memory window that must to be allocated
774 * @add_size : additional optional memory window
Ram Pai9e8bf932011-07-25 13:08:42 -0700775 * @realloc_head : track the additional memory window on this list
Ram Paic8adf9a2011-02-14 17:43:20 -0800776 *
777 * Calculate the size of the bus and minimal alignment which
778 * guarantees that all child resources fit in this size.
779 */
Eric W. Biederman28760482009-09-09 14:09:24 -0700780static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
Ram Paic8adf9a2011-02-14 17:43:20 -0800781 unsigned long type, resource_size_t min_size,
782 resource_size_t add_size,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800783 struct list_head *realloc_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
785 struct pci_dev *dev;
Ram Paic8adf9a2011-02-14 17:43:20 -0800786 resource_size_t min_align, align, size, size0, size1;
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100787 resource_size_t aligns[12]; /* Alignments from 1Mb to 2Gb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 int order, max_order;
789 struct resource *b_res = find_free_bus_resource(bus, type);
Yinghai Lu1f82de12009-04-23 20:48:32 -0700790 unsigned int mem64_mask = 0;
Yinghai Lube768912011-07-25 13:08:38 -0700791 resource_size_t children_add_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 if (!b_res)
794 return 0;
795
796 memset(aligns, 0, sizeof(aligns));
797 max_order = 0;
798 size = 0;
799
Yinghai Lu1f82de12009-04-23 20:48:32 -0700800 mem64_mask = b_res->flags & IORESOURCE_MEM_64;
801 b_res->flags &= ~IORESOURCE_MEM_64;
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 list_for_each_entry(dev, &bus->devices, bus_list) {
804 int i;
Yinghai Lu1f82de12009-04-23 20:48:32 -0700805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
807 struct resource *r = &dev->resource[i];
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100808 resource_size_t r_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 if (r->parent || (r->flags & mask) != type)
811 continue;
Zhao, Yu022edd82008-10-13 19:24:28 +0800812 r_size = resource_size(r);
Yinghai Lu2aceefc2011-07-25 13:08:40 -0700813#ifdef CONFIG_PCI_IOV
814 /* put SRIOV requested res to the optional list */
Ram Pai9e8bf932011-07-25 13:08:42 -0700815 if (realloc_head && i >= PCI_IOV_RESOURCES &&
Yinghai Lu2aceefc2011-07-25 13:08:40 -0700816 i <= PCI_IOV_RESOURCE_END) {
817 r->end = r->start - 1;
Ram Pai9e8bf932011-07-25 13:08:42 -0700818 add_to_list(realloc_head, dev, r, r_size, 0/* dont' care */);
Yinghai Lu2aceefc2011-07-25 13:08:40 -0700819 children_add_size += r_size;
820 continue;
821 }
822#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 /* For bridges size != alignment */
Chris Wright6faf17f2009-08-28 13:00:06 -0700824 align = pci_resource_alignment(dev, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 order = __ffs(align) - 20;
826 if (order > 11) {
Bjorn Helgaas865df572009-11-04 10:32:57 -0700827 dev_warn(&dev->dev, "disabling BAR %d: %pR "
828 "(bad alignment %#llx)\n", i, r,
829 (unsigned long long) align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 r->flags = 0;
831 continue;
832 }
833 size += r_size;
834 if (order < 0)
835 order = 0;
836 /* Exclude ranges with size > align from
837 calculation of the alignment. */
838 if (r_size == align)
839 aligns[order] += align;
840 if (order > max_order)
841 max_order = order;
Yinghai Lu1f82de12009-04-23 20:48:32 -0700842 mem64_mask &= r->flags & IORESOURCE_MEM_64;
Yinghai Lube768912011-07-25 13:08:38 -0700843
Ram Pai9e8bf932011-07-25 13:08:42 -0700844 if (realloc_head)
845 children_add_size += get_res_add_size(realloc_head, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 align = 0;
849 min_align = 0;
850 for (order = 0; order <= max_order; order++) {
Jeremy Fitzhardinge8308c542008-09-11 01:31:50 -0700851 resource_size_t align1 = 1;
852
853 align1 <<= (order + 20);
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (!align)
856 min_align = align1;
Milind Arun Choudhary6f6f8c22007-07-09 11:55:51 -0700857 else if (ALIGN(align + min_align, min_align) < align1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 min_align = align1 >> 1;
859 align += aligns[order];
860 }
Linus Torvaldsb42282e2011-04-11 10:53:11 -0700861 size0 = calculate_memsize(size, min_size, 0, resource_size(b_res), min_align);
Yinghai Lube768912011-07-25 13:08:38 -0700862 if (children_add_size > add_size)
863 add_size = children_add_size;
Ram Pai9e8bf932011-07-25 13:08:42 -0700864 size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
Yinghai Lua4ac9fe2012-01-21 02:08:17 -0800865 calculate_memsize(size, min_size, add_size,
Linus Torvaldsb42282e2011-04-11 10:53:11 -0700866 resource_size(b_res), min_align);
Ram Paic8adf9a2011-02-14 17:43:20 -0800867 if (!size0 && !size1) {
Bjorn Helgaas865df572009-11-04 10:32:57 -0700868 if (b_res->start || b_res->end)
869 dev_info(&bus->self->dev, "disabling bridge window "
870 "%pR to [bus %02x-%02x] (unused)\n", b_res,
871 bus->secondary, bus->subordinate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 b_res->flags = 0;
873 return 1;
874 }
875 b_res->start = min_align;
Ram Paic8adf9a2011-02-14 17:43:20 -0800876 b_res->end = size0 + min_align - 1;
877 b_res->flags |= IORESOURCE_STARTALIGN | mem64_mask;
Yinghai Lub5924432012-01-21 02:08:31 -0800878 if (size1 > size0 && realloc_head) {
Ram Pai9e8bf932011-07-25 13:08:42 -0700879 add_to_list(realloc_head, bus->self, b_res, size1-size0, min_align);
Yinghai Lub5924432012-01-21 02:08:31 -0800880 dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window "
881 "%pR to [bus %02x-%02x] add_size %llx\n", b_res,
882 bus->secondary, bus->subordinate, (unsigned long long)size1-size0);
883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return 1;
885}
886
Ram Pai0a2daa12011-07-25 13:08:41 -0700887unsigned long pci_cardbus_resource_alignment(struct resource *res)
888{
889 if (res->flags & IORESOURCE_IO)
890 return pci_cardbus_io_size;
891 if (res->flags & IORESOURCE_MEM)
892 return pci_cardbus_mem_size;
893 return 0;
894}
895
896static void pci_bus_size_cardbus(struct pci_bus *bus,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800897 struct list_head *realloc_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
899 struct pci_dev *bridge = bus->self;
900 struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
901 u16 ctrl;
902
903 /*
904 * Reserve some resources for CardBus. We reserve
905 * a fixed amount of bus space for CardBus bridges.
906 */
Linus Torvalds934b7022008-04-22 18:16:30 -0700907 b_res[0].start = 0;
Linus Torvalds934b7022008-04-22 18:16:30 -0700908 b_res[0].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
Ram Pai9e8bf932011-07-25 13:08:42 -0700909 if (realloc_head)
910 add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size, 0 /* dont care */);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Linus Torvalds934b7022008-04-22 18:16:30 -0700912 b_res[1].start = 0;
Linus Torvalds934b7022008-04-22 18:16:30 -0700913 b_res[1].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
Ram Pai9e8bf932011-07-25 13:08:42 -0700914 if (realloc_head)
915 add_to_list(realloc_head, bridge, b_res+1, pci_cardbus_io_size, 0 /* dont care */);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Yinghai Ludcef0d02012-02-10 15:33:46 -0800917 /* MEM1 must not be pref mmio */
918 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
919 if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM1) {
920 ctrl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1;
921 pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
922 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
923 }
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 /*
926 * Check whether prefetchable memory is supported
927 * by this bridge.
928 */
929 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
930 if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
931 ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
932 pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
933 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
934 }
935
936 /*
937 * If we have prefetchable memory support, allocate
938 * two regions. Otherwise, allocate one region of
939 * twice the size.
940 */
941 if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
Linus Torvalds934b7022008-04-22 18:16:30 -0700942 b_res[2].start = 0;
Linus Torvalds934b7022008-04-22 18:16:30 -0700943 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_SIZEALIGN;
Ram Pai9e8bf932011-07-25 13:08:42 -0700944 if (realloc_head)
945 add_to_list(realloc_head, bridge, b_res+2, pci_cardbus_mem_size, 0 /* dont care */);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Linus Torvalds934b7022008-04-22 18:16:30 -0700947 b_res[3].start = 0;
Linus Torvalds934b7022008-04-22 18:16:30 -0700948 b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
Ram Pai9e8bf932011-07-25 13:08:42 -0700949 if (realloc_head)
950 add_to_list(realloc_head, bridge, b_res+3, pci_cardbus_mem_size, 0 /* dont care */);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 } else {
Linus Torvalds934b7022008-04-22 18:16:30 -0700952 b_res[3].start = 0;
Linus Torvalds934b7022008-04-22 18:16:30 -0700953 b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
Ram Pai9e8bf932011-07-25 13:08:42 -0700954 if (realloc_head)
955 add_to_list(realloc_head, bridge, b_res+3, pci_cardbus_mem_size * 2, 0 /* dont care */);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 }
Ram Pai0a2daa12011-07-25 13:08:41 -0700957
958 /* set the size of the resource to zero, so that the resource does not
959 * get assigned during required-resource allocation cycle but gets assigned
960 * during the optional-resource allocation cycle.
961 */
962 b_res[0].start = b_res[1].start = b_res[2].start = b_res[3].start = 1;
963 b_res[0].end = b_res[1].end = b_res[2].end = b_res[3].end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
965
Ram Paic8adf9a2011-02-14 17:43:20 -0800966void __ref __pci_bus_size_bridges(struct pci_bus *bus,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800967 struct list_head *realloc_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
969 struct pci_dev *dev;
970 unsigned long mask, prefmask;
Ram Paic8adf9a2011-02-14 17:43:20 -0800971 resource_size_t additional_mem_size = 0, additional_io_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 list_for_each_entry(dev, &bus->devices, bus_list) {
974 struct pci_bus *b = dev->subordinate;
975 if (!b)
976 continue;
977
978 switch (dev->class >> 8) {
979 case PCI_CLASS_BRIDGE_CARDBUS:
Ram Pai9e8bf932011-07-25 13:08:42 -0700980 pci_bus_size_cardbus(b, realloc_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 break;
982
983 case PCI_CLASS_BRIDGE_PCI:
984 default:
Ram Pai9e8bf932011-07-25 13:08:42 -0700985 __pci_bus_size_bridges(b, realloc_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 break;
987 }
988 }
989
990 /* The root bus? */
991 if (!bus->self)
992 return;
993
994 switch (bus->self->class >> 8) {
995 case PCI_CLASS_BRIDGE_CARDBUS:
996 /* don't size cardbuses yet. */
997 break;
998
999 case PCI_CLASS_BRIDGE_PCI:
1000 pci_bridge_check_ranges(bus);
Eric W. Biederman28760482009-09-09 14:09:24 -07001001 if (bus->self->is_hotplug_bridge) {
Ram Paic8adf9a2011-02-14 17:43:20 -08001002 additional_io_size = pci_hotplug_io_size;
1003 additional_mem_size = pci_hotplug_mem_size;
Eric W. Biederman28760482009-09-09 14:09:24 -07001004 }
Ram Paic8adf9a2011-02-14 17:43:20 -08001005 /*
1006 * Follow thru
1007 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 default:
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001009 pbus_size_io(bus, realloc_head ? 0 : additional_io_size,
1010 additional_io_size, realloc_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 /* If the bridge supports prefetchable range, size it
1012 separately. If it doesn't, or its prefetchable window
1013 has already been allocated by arch code, try
1014 non-prefetchable range for both types of PCI memory
1015 resources. */
1016 mask = IORESOURCE_MEM;
1017 prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001018 if (pbus_size_mem(bus, prefmask, prefmask,
1019 realloc_head ? 0 : additional_mem_size,
1020 additional_mem_size, realloc_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 mask = prefmask; /* Success, size non-prefetch only. */
Eric W. Biederman28760482009-09-09 14:09:24 -07001022 else
Ram Paic8adf9a2011-02-14 17:43:20 -08001023 additional_mem_size += additional_mem_size;
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001024 pbus_size_mem(bus, mask, IORESOURCE_MEM,
1025 realloc_head ? 0 : additional_mem_size,
1026 additional_mem_size, realloc_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 break;
1028 }
1029}
Ram Paic8adf9a2011-02-14 17:43:20 -08001030
1031void __ref pci_bus_size_bridges(struct pci_bus *bus)
1032{
1033 __pci_bus_size_bridges(bus, NULL);
1034}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035EXPORT_SYMBOL(pci_bus_size_bridges);
1036
Yinghai Lu568ddef2010-01-22 01:02:21 -08001037static void __ref __pci_bus_assign_resources(const struct pci_bus *bus,
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001038 struct list_head *realloc_head,
1039 struct list_head *fail_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
1041 struct pci_bus *b;
1042 struct pci_dev *dev;
1043
Ram Pai9e8bf932011-07-25 13:08:42 -07001044 pbus_assign_resources_sorted(bus, realloc_head, fail_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 list_for_each_entry(dev, &bus->devices, bus_list) {
1047 b = dev->subordinate;
1048 if (!b)
1049 continue;
1050
Ram Pai9e8bf932011-07-25 13:08:42 -07001051 __pci_bus_assign_resources(b, realloc_head, fail_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 switch (dev->class >> 8) {
1054 case PCI_CLASS_BRIDGE_PCI:
Yinghai Lu6841ec62010-01-22 01:02:25 -08001055 if (!pci_is_enabled(dev))
1056 pci_setup_bridge(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 break;
1058
1059 case PCI_CLASS_BRIDGE_CARDBUS:
1060 pci_setup_cardbus(b);
1061 break;
1062
1063 default:
Bjorn Helgaas80ccba12008-06-13 10:52:11 -06001064 dev_info(&dev->dev, "not setting up bridge for bus "
1065 "%04x:%02x\n", pci_domain_nr(b), b->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 break;
1067 }
1068 }
1069}
Yinghai Lu568ddef2010-01-22 01:02:21 -08001070
1071void __ref pci_bus_assign_resources(const struct pci_bus *bus)
1072{
Ram Paic8adf9a2011-02-14 17:43:20 -08001073 __pci_bus_assign_resources(bus, NULL, NULL);
Yinghai Lu568ddef2010-01-22 01:02:21 -08001074}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075EXPORT_SYMBOL(pci_bus_assign_resources);
1076
Yinghai Lu6841ec62010-01-22 01:02:25 -08001077static void __ref __pci_bridge_assign_resources(const struct pci_dev *bridge,
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001078 struct list_head *add_head,
1079 struct list_head *fail_head)
Yinghai Lu6841ec62010-01-22 01:02:25 -08001080{
1081 struct pci_bus *b;
1082
Yinghai Lu8424d752012-01-21 02:08:21 -08001083 pdev_assign_resources_sorted((struct pci_dev *)bridge,
1084 add_head, fail_head);
Yinghai Lu6841ec62010-01-22 01:02:25 -08001085
1086 b = bridge->subordinate;
1087 if (!b)
1088 return;
1089
Yinghai Lu8424d752012-01-21 02:08:21 -08001090 __pci_bus_assign_resources(b, add_head, fail_head);
Yinghai Lu6841ec62010-01-22 01:02:25 -08001091
1092 switch (bridge->class >> 8) {
1093 case PCI_CLASS_BRIDGE_PCI:
1094 pci_setup_bridge(b);
1095 break;
1096
1097 case PCI_CLASS_BRIDGE_CARDBUS:
1098 pci_setup_cardbus(b);
1099 break;
1100
1101 default:
1102 dev_info(&bridge->dev, "not setting up bridge for bus "
1103 "%04x:%02x\n", pci_domain_nr(b), b->number);
1104 break;
1105 }
1106}
Yinghai Lu5009b462010-01-22 01:02:20 -08001107static void pci_bridge_release_resources(struct pci_bus *bus,
1108 unsigned long type)
1109{
1110 int idx;
1111 bool changed = false;
1112 struct pci_dev *dev;
1113 struct resource *r;
1114 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1115 IORESOURCE_PREFETCH;
1116
1117 dev = bus->self;
1118 for (idx = PCI_BRIDGE_RESOURCES; idx <= PCI_BRIDGE_RESOURCE_END;
1119 idx++) {
1120 r = &dev->resource[idx];
1121 if ((r->flags & type_mask) != type)
1122 continue;
1123 if (!r->parent)
1124 continue;
1125 /*
1126 * if there are children under that, we should release them
1127 * all
1128 */
1129 release_child_resources(r);
1130 if (!release_resource(r)) {
1131 dev_printk(KERN_DEBUG, &dev->dev,
1132 "resource %d %pR released\n", idx, r);
1133 /* keep the old size */
1134 r->end = resource_size(r) - 1;
1135 r->start = 0;
1136 r->flags = 0;
1137 changed = true;
1138 }
1139 }
1140
1141 if (changed) {
1142 /* avoiding touch the one without PREF */
1143 if (type & IORESOURCE_PREFETCH)
1144 type = IORESOURCE_PREFETCH;
1145 __pci_setup_bridge(bus, type);
1146 }
1147}
1148
1149enum release_type {
1150 leaf_only,
1151 whole_subtree,
1152};
1153/*
1154 * try to release pci bridge resources that is from leaf bridge,
1155 * so we can allocate big new one later
1156 */
1157static void __ref pci_bus_release_bridge_resources(struct pci_bus *bus,
1158 unsigned long type,
1159 enum release_type rel_type)
1160{
1161 struct pci_dev *dev;
1162 bool is_leaf_bridge = true;
1163
1164 list_for_each_entry(dev, &bus->devices, bus_list) {
1165 struct pci_bus *b = dev->subordinate;
1166 if (!b)
1167 continue;
1168
1169 is_leaf_bridge = false;
1170
1171 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
1172 continue;
1173
1174 if (rel_type == whole_subtree)
1175 pci_bus_release_bridge_resources(b, type,
1176 whole_subtree);
1177 }
1178
1179 if (pci_is_root_bus(bus))
1180 return;
1181
1182 if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
1183 return;
1184
1185 if ((rel_type == whole_subtree) || is_leaf_bridge)
1186 pci_bridge_release_resources(bus, type);
1187}
1188
Yinghai Lu76fbc262008-06-23 20:33:06 +02001189static void pci_bus_dump_res(struct pci_bus *bus)
1190{
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -07001191 struct resource *res;
1192 int i;
Yinghai Lu76fbc262008-06-23 20:33:06 +02001193
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -07001194 pci_bus_for_each_resource(bus, res, i) {
Yinghai Lu7c9342b2009-12-22 15:02:24 -08001195 if (!res || !res->end || !res->flags)
Yinghai Lu76fbc262008-06-23 20:33:06 +02001196 continue;
1197
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001198 dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res);
Yinghai Lu76fbc262008-06-23 20:33:06 +02001199 }
1200}
1201
1202static void pci_bus_dump_resources(struct pci_bus *bus)
1203{
1204 struct pci_bus *b;
1205 struct pci_dev *dev;
1206
1207
1208 pci_bus_dump_res(bus);
1209
1210 list_for_each_entry(dev, &bus->devices, bus_list) {
1211 b = dev->subordinate;
1212 if (!b)
1213 continue;
1214
1215 pci_bus_dump_resources(b);
1216 }
1217}
1218
Yinghai Luda7822e2011-05-12 17:11:37 -07001219static int __init pci_bus_get_depth(struct pci_bus *bus)
1220{
1221 int depth = 0;
1222 struct pci_dev *dev;
1223
1224 list_for_each_entry(dev, &bus->devices, bus_list) {
1225 int ret;
1226 struct pci_bus *b = dev->subordinate;
1227 if (!b)
1228 continue;
1229
1230 ret = pci_bus_get_depth(b);
1231 if (ret + 1 > depth)
1232 depth = ret + 1;
1233 }
1234
1235 return depth;
1236}
1237static int __init pci_get_max_depth(void)
1238{
1239 int depth = 0;
1240 struct pci_bus *bus;
1241
1242 list_for_each_entry(bus, &pci_root_buses, node) {
1243 int ret;
1244
1245 ret = pci_bus_get_depth(bus);
1246 if (ret > depth)
1247 depth = ret;
1248 }
1249
1250 return depth;
1251}
1252
Ram Paif483d392011-07-07 11:19:10 -07001253
Yinghai Luda7822e2011-05-12 17:11:37 -07001254/*
1255 * first try will not touch pci bridge res
1256 * second and later try will clear small leaf bridge res
1257 * will stop till to the max deepth if can not find good one
1258 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259void __init
1260pci_assign_unassigned_resources(void)
1261{
1262 struct pci_bus *bus;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001263 LIST_HEAD(realloc_head); /* list of resources that
Ram Paic8adf9a2011-02-14 17:43:20 -08001264 want additional resources */
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001265 struct list_head *add_list = NULL;
Yinghai Luda7822e2011-05-12 17:11:37 -07001266 int tried_times = 0;
1267 enum release_type rel_type = leaf_only;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001268 LIST_HEAD(fail_head);
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001269 struct pci_dev_resource *fail_res;
Yinghai Luda7822e2011-05-12 17:11:37 -07001270 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1271 IORESOURCE_PREFETCH;
1272 unsigned long failed_type;
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001273 int pci_try_num = 1;
Yinghai Luda7822e2011-05-12 17:11:37 -07001274
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001275 /* don't realloc if asked to do so */
1276 if (pci_realloc_enabled()) {
1277 int max_depth = pci_get_max_depth();
1278
1279 pci_try_num = max_depth + 1;
1280 printk(KERN_DEBUG "PCI: max bus depth: %d pci_try_num: %d\n",
1281 max_depth, pci_try_num);
1282 }
Yinghai Luda7822e2011-05-12 17:11:37 -07001283
1284again:
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001285 /*
1286 * last try will use add_list, otherwise will try good to have as
1287 * must have, so can realloc parent bridge resource
1288 */
1289 if (tried_times + 1 == pci_try_num)
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001290 add_list = &realloc_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 /* Depth first, calculate sizes and alignments of all
1292 subordinate buses. */
Yinghai Luda7822e2011-05-12 17:11:37 -07001293 list_for_each_entry(bus, &pci_root_buses, node)
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001294 __pci_bus_size_bridges(bus, add_list);
Ram Paic8adf9a2011-02-14 17:43:20 -08001295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 /* Depth last, allocate resources and update the hardware. */
Yinghai Luda7822e2011-05-12 17:11:37 -07001297 list_for_each_entry(bus, &pci_root_buses, node)
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001298 __pci_bus_assign_resources(bus, add_list, &fail_head);
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001299 if (add_list)
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001300 BUG_ON(!list_empty(add_list));
Yinghai Luda7822e2011-05-12 17:11:37 -07001301 tried_times++;
1302
1303 /* any device complain? */
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001304 if (list_empty(&fail_head))
Yinghai Luda7822e2011-05-12 17:11:37 -07001305 goto enable_and_dump;
Ram Paif483d392011-07-07 11:19:10 -07001306
Yinghai Luda7822e2011-05-12 17:11:37 -07001307 failed_type = 0;
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001308 list_for_each_entry(fail_res, &fail_head, list)
1309 failed_type |= fail_res->flags;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001310
Yinghai Luda7822e2011-05-12 17:11:37 -07001311 /*
1312 * io port are tight, don't try extra
1313 * or if reach the limit, don't want to try more
1314 */
1315 failed_type &= type_mask;
1316 if ((failed_type == IORESOURCE_IO) || (tried_times >= pci_try_num)) {
Yinghai Lubffc56d2012-01-21 02:08:30 -08001317 free_list(&fail_head);
Yinghai Luda7822e2011-05-12 17:11:37 -07001318 goto enable_and_dump;
1319 }
1320
1321 printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
1322 tried_times + 1);
1323
1324 /* third times and later will not check if it is leaf */
1325 if ((tried_times + 1) > 2)
1326 rel_type = whole_subtree;
1327
1328 /*
1329 * Try to release leaf bridge's resources that doesn't fit resource of
1330 * child device under that bridge
1331 */
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001332 list_for_each_entry(fail_res, &fail_head, list) {
1333 bus = fail_res->dev->bus;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001334 pci_bus_release_bridge_resources(bus,
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001335 fail_res->flags & type_mask,
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001336 rel_type);
Yinghai Luda7822e2011-05-12 17:11:37 -07001337 }
1338 /* restore size and flags */
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001339 list_for_each_entry(fail_res, &fail_head, list) {
1340 struct resource *res = fail_res->res;
Yinghai Luda7822e2011-05-12 17:11:37 -07001341
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001342 res->start = fail_res->start;
1343 res->end = fail_res->end;
1344 res->flags = fail_res->flags;
1345 if (fail_res->dev->subordinate)
Yinghai Luda7822e2011-05-12 17:11:37 -07001346 res->flags = 0;
Yinghai Luda7822e2011-05-12 17:11:37 -07001347 }
Yinghai Lubffc56d2012-01-21 02:08:30 -08001348 free_list(&fail_head);
Yinghai Luda7822e2011-05-12 17:11:37 -07001349
1350 goto again;
1351
1352enable_and_dump:
1353 /* Depth last, update the hardware. */
1354 list_for_each_entry(bus, &pci_root_buses, node)
1355 pci_enable_bridges(bus);
Yinghai Lu76fbc262008-06-23 20:33:06 +02001356
1357 /* dump the resource on buses */
Yinghai Luda7822e2011-05-12 17:11:37 -07001358 list_for_each_entry(bus, &pci_root_buses, node)
Yinghai Lu76fbc262008-06-23 20:33:06 +02001359 pci_bus_dump_resources(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360}
Yinghai Lu6841ec62010-01-22 01:02:25 -08001361
1362void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
1363{
1364 struct pci_bus *parent = bridge->subordinate;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001365 LIST_HEAD(add_list); /* list of resources that
Yinghai Lu8424d752012-01-21 02:08:21 -08001366 want additional resources */
Yinghai Lu32180e402010-01-22 01:02:27 -08001367 int tried_times = 0;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001368 LIST_HEAD(fail_head);
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001369 struct pci_dev_resource *fail_res;
Yinghai Lu6841ec62010-01-22 01:02:25 -08001370 int retval;
Yinghai Lu32180e402010-01-22 01:02:27 -08001371 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1372 IORESOURCE_PREFETCH;
Yinghai Lu6841ec62010-01-22 01:02:25 -08001373
Yinghai Lu32180e402010-01-22 01:02:27 -08001374again:
Yinghai Lu8424d752012-01-21 02:08:21 -08001375 __pci_bus_size_bridges(parent, &add_list);
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001376 __pci_bridge_assign_resources(bridge, &add_list, &fail_head);
1377 BUG_ON(!list_empty(&add_list));
Yinghai Lu32180e402010-01-22 01:02:27 -08001378 tried_times++;
1379
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001380 if (list_empty(&fail_head))
Yinghai Lu3f579c32010-05-21 14:35:06 -07001381 goto enable_all;
Yinghai Lu32180e402010-01-22 01:02:27 -08001382
1383 if (tried_times >= 2) {
1384 /* still fail, don't need to try more */
Yinghai Lubffc56d2012-01-21 02:08:30 -08001385 free_list(&fail_head);
Yinghai Lu3f579c32010-05-21 14:35:06 -07001386 goto enable_all;
Yinghai Lu32180e402010-01-22 01:02:27 -08001387 }
1388
1389 printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
1390 tried_times + 1);
1391
1392 /*
1393 * Try to release leaf bridge's resources that doesn't fit resource of
1394 * child device under that bridge
1395 */
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001396 list_for_each_entry(fail_res, &fail_head, list) {
1397 struct pci_bus *bus = fail_res->dev->bus;
1398 unsigned long flags = fail_res->flags;
Yinghai Lu32180e402010-01-22 01:02:27 -08001399
1400 pci_bus_release_bridge_resources(bus, flags & type_mask,
1401 whole_subtree);
Yinghai Lu32180e402010-01-22 01:02:27 -08001402 }
1403 /* restore size and flags */
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001404 list_for_each_entry(fail_res, &fail_head, list) {
1405 struct resource *res = fail_res->res;
Yinghai Lu32180e402010-01-22 01:02:27 -08001406
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001407 res->start = fail_res->start;
1408 res->end = fail_res->end;
1409 res->flags = fail_res->flags;
1410 if (fail_res->dev->subordinate)
Yinghai Lu32180e402010-01-22 01:02:27 -08001411 res->flags = 0;
Yinghai Lu32180e402010-01-22 01:02:27 -08001412 }
Yinghai Lubffc56d2012-01-21 02:08:30 -08001413 free_list(&fail_head);
Yinghai Lu32180e402010-01-22 01:02:27 -08001414
1415 goto again;
Yinghai Lu3f579c32010-05-21 14:35:06 -07001416
1417enable_all:
1418 retval = pci_reenable_device(bridge);
1419 pci_set_master(bridge);
1420 pci_enable_bridges(parent);
Yinghai Lu6841ec62010-01-22 01:02:25 -08001421}
1422EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
Yinghai Lu9b030882012-01-21 02:08:23 -08001423
1424#ifdef CONFIG_HOTPLUG
1425/**
1426 * pci_rescan_bus - scan a PCI bus for devices.
1427 * @bus: PCI bus to scan
1428 *
1429 * Scan a PCI bus and child buses for new devices, adds them,
1430 * and enables them.
1431 *
1432 * Returns the max number of subordinate bus discovered.
1433 */
1434unsigned int __ref pci_rescan_bus(struct pci_bus *bus)
1435{
1436 unsigned int max;
1437 struct pci_dev *dev;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001438 LIST_HEAD(add_list); /* list of resources that
Yinghai Lu9b030882012-01-21 02:08:23 -08001439 want additional resources */
1440
1441 max = pci_scan_child_bus(bus);
1442
Yinghai Lu9b030882012-01-21 02:08:23 -08001443 down_read(&pci_bus_sem);
1444 list_for_each_entry(dev, &bus->devices, bus_list)
1445 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
1446 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
1447 if (dev->subordinate)
1448 __pci_bus_size_bridges(dev->subordinate,
1449 &add_list);
1450 up_read(&pci_bus_sem);
1451 __pci_bus_assign_resources(bus, &add_list, NULL);
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001452 BUG_ON(!list_empty(&add_list));
Yinghai Lu9b030882012-01-21 02:08:23 -08001453
1454 pci_enable_bridges(bus);
1455 pci_bus_add_devices(bus);
1456
1457 return max;
1458}
1459EXPORT_SYMBOL_GPL(pci_rescan_bus);
1460#endif