blob: dad5425f1f09399fd7fa0ab30863182285093653 [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 Paic8adf9a2011-02-14 17:43:20 -080054/**
55 * add_to_list() - add a new resource tracker to the list
56 * @head: Head of the list
57 * @dev: device corresponding to which the resource
58 * belongs
59 * @res: The resource to be tracked
60 * @add_size: additional size to be optionally added
61 * to the resource
62 */
Yinghai Lubdc4abe2012-01-21 02:08:27 -080063static int add_to_list(struct list_head *head,
Ram Paic8adf9a2011-02-14 17:43:20 -080064 struct pci_dev *dev, struct resource *res,
Ram Pai2bbc6942011-07-25 13:08:39 -070065 resource_size_t add_size, resource_size_t min_align)
Yinghai Lu568ddef2010-01-22 01:02:21 -080066{
Yinghai Lu764242a2012-01-21 02:08:28 -080067 struct pci_dev_resource *tmp;
Yinghai Lu568ddef2010-01-22 01:02:21 -080068
Yinghai Lubdc4abe2012-01-21 02:08:27 -080069 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
Yinghai Lu568ddef2010-01-22 01:02:21 -080070 if (!tmp) {
Ram Paic8adf9a2011-02-14 17:43:20 -080071 pr_warning("add_to_list: kmalloc() failed!\n");
Yinghai Luef62dfe2012-01-21 02:08:18 -080072 return -ENOMEM;
Yinghai Lu568ddef2010-01-22 01:02:21 -080073 }
74
Yinghai Lu568ddef2010-01-22 01:02:21 -080075 tmp->res = res;
76 tmp->dev = dev;
77 tmp->start = res->start;
78 tmp->end = res->end;
79 tmp->flags = res->flags;
Ram Paic8adf9a2011-02-14 17:43:20 -080080 tmp->add_size = add_size;
Ram Pai2bbc6942011-07-25 13:08:39 -070081 tmp->min_align = min_align;
Yinghai Lubdc4abe2012-01-21 02:08:27 -080082
83 list_add(&tmp->list, head);
Yinghai Luef62dfe2012-01-21 02:08:18 -080084
85 return 0;
Yinghai Lu568ddef2010-01-22 01:02:21 -080086}
87
Yinghai Lub9b0bba2012-01-21 02:08:29 -080088static void remove_from_list(struct list_head *head,
Yinghai Lu3e6e0d82012-01-21 02:08:20 -080089 struct resource *res)
90{
Yinghai Lub9b0bba2012-01-21 02:08:29 -080091 struct pci_dev_resource *dev_res, *tmp;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -080092
Yinghai Lub9b0bba2012-01-21 02:08:29 -080093 list_for_each_entry_safe(dev_res, tmp, head, list) {
94 if (dev_res->res == res) {
95 list_del(&dev_res->list);
96 kfree(dev_res);
Yinghai Lubdc4abe2012-01-21 02:08:27 -080097 break;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -080098 }
Yinghai Lu3e6e0d82012-01-21 02:08:20 -080099 }
100}
101
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800102static resource_size_t get_res_add_size(struct list_head *head,
Yinghai Lu1c372352012-01-21 02:08:19 -0800103 struct resource *res)
104{
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800105 struct pci_dev_resource *dev_res;
Yinghai Lu1c372352012-01-21 02:08:19 -0800106
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800107 list_for_each_entry(dev_res, head, list) {
108 if (dev_res->res == res) {
Yinghai Lub5924432012-01-21 02:08:31 -0800109 int idx = res - &dev_res->dev->resource[0];
110
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800111 dev_printk(KERN_DEBUG, &dev_res->dev->dev,
Yinghai Lub5924432012-01-21 02:08:31 -0800112 "res[%d]=%pR get_res_add_size add_size %llx\n",
113 idx, dev_res->res,
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800114 (unsigned long long)dev_res->add_size);
Yinghai Lub5924432012-01-21 02:08:31 -0800115
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800116 return dev_res->add_size;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800117 }
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800118 }
Yinghai Lu1c372352012-01-21 02:08:19 -0800119
120 return 0;
121}
122
Yinghai Lu78c3b322012-01-21 02:08:25 -0800123/* Sort resources by alignment */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800124static void pdev_sort_resources(struct pci_dev *dev, struct list_head *head)
Yinghai Lu78c3b322012-01-21 02:08:25 -0800125{
126 int i;
127
128 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
129 struct resource *r;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800130 struct pci_dev_resource *dev_res, *tmp;
Yinghai Lu78c3b322012-01-21 02:08:25 -0800131 resource_size_t r_align;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800132 struct list_head *n;
Yinghai Lu78c3b322012-01-21 02:08:25 -0800133
134 r = &dev->resource[i];
135
136 if (r->flags & IORESOURCE_PCI_FIXED)
137 continue;
138
139 if (!(r->flags) || r->parent)
140 continue;
141
142 r_align = pci_resource_alignment(dev, r);
143 if (!r_align) {
144 dev_warn(&dev->dev, "BAR %d: %pR has bogus alignment\n",
145 i, r);
146 continue;
147 }
Yinghai Lu78c3b322012-01-21 02:08:25 -0800148
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800149 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
150 if (!tmp)
151 panic("pdev_sort_resources(): "
152 "kmalloc() failed!\n");
153 tmp->res = r;
154 tmp->dev = dev;
155
156 /* fallback is smallest one or list is empty*/
157 n = head;
158 list_for_each_entry(dev_res, head, list) {
159 resource_size_t align;
160
161 align = pci_resource_alignment(dev_res->dev,
162 dev_res->res);
Yinghai Lu78c3b322012-01-21 02:08:25 -0800163
164 if (r_align > align) {
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800165 n = &dev_res->list;
Yinghai Lu78c3b322012-01-21 02:08:25 -0800166 break;
167 }
168 }
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800169 /* Insert it just before n*/
170 list_add_tail(&tmp->list, n);
Yinghai Lu78c3b322012-01-21 02:08:25 -0800171 }
172}
173
Yinghai Lu6841ec62010-01-22 01:02:25 -0800174static void __dev_sort_resources(struct pci_dev *dev,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800175 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Yinghai Lu6841ec62010-01-22 01:02:25 -0800177 u16 class = dev->class >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Yinghai Lu6841ec62010-01-22 01:02:25 -0800179 /* Don't touch classless devices or host bridges or ioapics. */
180 if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST)
181 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Yinghai Lu6841ec62010-01-22 01:02:25 -0800183 /* Don't touch ioapic devices already enabled by firmware */
184 if (class == PCI_CLASS_SYSTEM_PIC) {
185 u16 command;
186 pci_read_config_word(dev, PCI_COMMAND, &command);
187 if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
188 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
190
Yinghai Lu6841ec62010-01-22 01:02:25 -0800191 pdev_sort_resources(dev, head);
192}
193
Ram Paifc075e12011-02-14 17:43:19 -0800194static inline void reset_resource(struct resource *res)
195{
196 res->start = 0;
197 res->end = 0;
198 res->flags = 0;
199}
200
Ram Paic8adf9a2011-02-14 17:43:20 -0800201/**
Ram Pai9e8bf932011-07-25 13:08:42 -0700202 * reassign_resources_sorted() - satisfy any additional resource requests
Ram Paic8adf9a2011-02-14 17:43:20 -0800203 *
Ram Pai9e8bf932011-07-25 13:08:42 -0700204 * @realloc_head : head of the list tracking requests requiring additional
Ram Paic8adf9a2011-02-14 17:43:20 -0800205 * resources
206 * @head : head of the list tracking requests with allocated
207 * resources
208 *
Ram Pai9e8bf932011-07-25 13:08:42 -0700209 * Walk through each element of the realloc_head and try to procure
Ram Paic8adf9a2011-02-14 17:43:20 -0800210 * additional resources for the element, provided the element
211 * is in the head list.
212 */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800213static void reassign_resources_sorted(struct list_head *realloc_head,
214 struct list_head *head)
Ram Paic8adf9a2011-02-14 17:43:20 -0800215{
216 struct resource *res;
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800217 struct pci_dev_resource *add_res, *tmp;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800218 struct pci_dev_resource *dev_res;
Ram Paic8adf9a2011-02-14 17:43:20 -0800219 resource_size_t add_size;
220 int idx;
221
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800222 list_for_each_entry_safe(add_res, tmp, realloc_head, list) {
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800223 bool found_match = false;
224
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800225 res = add_res->res;
Ram Paic8adf9a2011-02-14 17:43:20 -0800226 /* skip resource that has been reset */
227 if (!res->flags)
228 goto out;
229
230 /* skip this resource if not found in head list */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800231 list_for_each_entry(dev_res, head, list) {
232 if (dev_res->res == res) {
233 found_match = true;
234 break;
235 }
Ram Paic8adf9a2011-02-14 17:43:20 -0800236 }
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800237 if (!found_match)/* just skip */
238 continue;
Ram Paic8adf9a2011-02-14 17:43:20 -0800239
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800240 idx = res - &add_res->dev->resource[0];
241 add_size = add_res->add_size;
Ram Pai2bbc6942011-07-25 13:08:39 -0700242 if (!resource_size(res)) {
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800243 res->start = add_res->start;
Ram Pai2bbc6942011-07-25 13:08:39 -0700244 res->end = res->start + add_size - 1;
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800245 if (pci_assign_resource(add_res->dev, idx))
Ram Paic8adf9a2011-02-14 17:43:20 -0800246 reset_resource(res);
Ram Pai2bbc6942011-07-25 13:08:39 -0700247 } else {
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800248 resource_size_t align = add_res->min_align;
249 res->flags |= add_res->flags &
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800250 (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN);
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800251 if (pci_reassign_resource(add_res->dev, idx,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800252 add_size, align))
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800253 dev_printk(KERN_DEBUG, &add_res->dev->dev,
Yinghai Lub5924432012-01-21 02:08:31 -0800254 "failed to add %llx res[%d]=%pR\n",
255 (unsigned long long)add_size,
256 idx, res);
Ram Paic8adf9a2011-02-14 17:43:20 -0800257 }
258out:
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800259 list_del(&add_res->list);
260 kfree(add_res);
Ram Paic8adf9a2011-02-14 17:43:20 -0800261 }
262}
263
264/**
265 * assign_requested_resources_sorted() - satisfy resource requests
266 *
267 * @head : head of the list tracking requests for resources
268 * @failed_list : head of the list tracking requests that could
269 * not be allocated
270 *
271 * Satisfy resource requests of each element in the list. Add
272 * requests that could not satisfied to the failed_list.
273 */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800274static void assign_requested_resources_sorted(struct list_head *head,
275 struct list_head *fail_head)
Yinghai Lu6841ec62010-01-22 01:02:25 -0800276{
277 struct resource *res;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800278 struct pci_dev_resource *dev_res;
Yinghai Lu6841ec62010-01-22 01:02:25 -0800279 int idx;
280
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800281 list_for_each_entry(dev_res, head, list) {
282 res = dev_res->res;
283 idx = res - &dev_res->dev->resource[0];
284 if (resource_size(res) &&
285 pci_assign_resource(dev_res->dev, idx)) {
286 if (fail_head && !pci_is_root_bus(dev_res->dev->bus)) {
Yinghai Lu9a928662010-02-28 15:49:39 -0800287 /*
288 * if the failed res is for ROM BAR, and it will
289 * be enabled later, don't add it to the list
290 */
291 if (!((idx == PCI_ROM_RESOURCE) &&
292 (!(res->flags & IORESOURCE_ROM_ENABLE))))
Yinghai Lu67cc7e22012-01-21 02:08:32 -0800293 add_to_list(fail_head,
294 dev_res->dev, res,
295 0 /* dont care */,
296 0 /* dont care */);
Yinghai Lu9a928662010-02-28 15:49:39 -0800297 }
Ram Paifc075e12011-02-14 17:43:19 -0800298 reset_resource(res);
Rajesh Shah542df5d2005-04-28 00:25:50 -0700299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
301}
302
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800303static void __assign_resources_sorted(struct list_head *head,
304 struct list_head *realloc_head,
305 struct list_head *fail_head)
Ram Paic8adf9a2011-02-14 17:43:20 -0800306{
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800307 /*
308 * Should not assign requested resources at first.
309 * they could be adjacent, so later reassign can not reallocate
310 * them one by one in parent resource window.
311 * Try to assign requested + add_size at begining
312 * if could do that, could get out early.
313 * if could not do that, we still try to assign requested at first,
314 * then try to reassign add_size for some resources.
315 */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800316 LIST_HEAD(save_head);
317 LIST_HEAD(local_fail_head);
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800318 struct pci_dev_resource *save_res;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800319 struct pci_dev_resource *dev_res;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800320
321 /* Check if optional add_size is there */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800322 if (!realloc_head || list_empty(realloc_head))
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800323 goto requested_and_reassign;
324
325 /* Save original start, end, flags etc at first */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800326 list_for_each_entry(dev_res, head, list) {
327 if (add_to_list(&save_head, dev_res->dev, dev_res->res, 0, 0)) {
Yinghai Lubffc56d2012-01-21 02:08:30 -0800328 free_list(&save_head);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800329 goto requested_and_reassign;
330 }
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800331 }
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800332
333 /* Update res in head list with add_size in realloc_head list */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800334 list_for_each_entry(dev_res, head, list)
335 dev_res->res->end += get_res_add_size(realloc_head,
336 dev_res->res);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800337
338 /* Try updated head list with add_size added */
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800339 assign_requested_resources_sorted(head, &local_fail_head);
340
341 /* all assigned with add_size ? */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800342 if (list_empty(&local_fail_head)) {
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800343 /* Remove head list from realloc_head list */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800344 list_for_each_entry(dev_res, head, list)
345 remove_from_list(realloc_head, dev_res->res);
Yinghai Lubffc56d2012-01-21 02:08:30 -0800346 free_list(&save_head);
347 free_list(head);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800348 return;
349 }
350
Yinghai Lubffc56d2012-01-21 02:08:30 -0800351 free_list(&local_fail_head);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800352 /* Release assigned resource */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800353 list_for_each_entry(dev_res, head, list)
354 if (dev_res->res->parent)
355 release_resource(dev_res->res);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800356 /* Restore start/end/flags from saved list */
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800357 list_for_each_entry(save_res, &save_head, list) {
358 struct resource *res = save_res->res;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800359
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800360 res->start = save_res->start;
361 res->end = save_res->end;
362 res->flags = save_res->flags;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800363 }
Yinghai Lubffc56d2012-01-21 02:08:30 -0800364 free_list(&save_head);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800365
366requested_and_reassign:
Ram Paic8adf9a2011-02-14 17:43:20 -0800367 /* Satisfy the must-have resource requests */
368 assign_requested_resources_sorted(head, fail_head);
369
Ram Pai0a2daa12011-07-25 13:08:41 -0700370 /* Try to satisfy any additional optional resource
Ram Paic8adf9a2011-02-14 17:43:20 -0800371 requests */
Ram Pai9e8bf932011-07-25 13:08:42 -0700372 if (realloc_head)
373 reassign_resources_sorted(realloc_head, head);
Yinghai Lubffc56d2012-01-21 02:08:30 -0800374 free_list(head);
Ram Paic8adf9a2011-02-14 17:43:20 -0800375}
376
Yinghai Lu6841ec62010-01-22 01:02:25 -0800377static void pdev_assign_resources_sorted(struct pci_dev *dev,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800378 struct list_head *add_head,
379 struct list_head *fail_head)
Yinghai Lu6841ec62010-01-22 01:02:25 -0800380{
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800381 LIST_HEAD(head);
Yinghai Lu6841ec62010-01-22 01:02:25 -0800382
Yinghai Lu6841ec62010-01-22 01:02:25 -0800383 __dev_sort_resources(dev, &head);
Yinghai Lu8424d752012-01-21 02:08:21 -0800384 __assign_resources_sorted(&head, add_head, fail_head);
Yinghai Lu6841ec62010-01-22 01:02:25 -0800385
386}
387
388static void pbus_assign_resources_sorted(const struct pci_bus *bus,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800389 struct list_head *realloc_head,
390 struct list_head *fail_head)
Yinghai Lu6841ec62010-01-22 01:02:25 -0800391{
392 struct pci_dev *dev;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800393 LIST_HEAD(head);
Yinghai Lu6841ec62010-01-22 01:02:25 -0800394
Yinghai Lu6841ec62010-01-22 01:02:25 -0800395 list_for_each_entry(dev, &bus->devices, bus_list)
396 __dev_sort_resources(dev, &head);
397
Ram Pai9e8bf932011-07-25 13:08:42 -0700398 __assign_resources_sorted(&head, realloc_head, fail_head);
Yinghai Lu6841ec62010-01-22 01:02:25 -0800399}
400
Dominik Brodowskib3743fa2005-09-09 13:03:23 -0700401void pci_setup_cardbus(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
403 struct pci_dev *bridge = bus->self;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600404 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 struct pci_bus_region region;
406
Bjorn Helgaas865df572009-11-04 10:32:57 -0700407 dev_info(&bridge->dev, "CardBus bridge to [bus %02x-%02x]\n",
408 bus->secondary, bus->subordinate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600410 res = bus->resource[0];
411 pcibios_resource_to_bus(bridge, &region, res);
412 if (res->flags & IORESOURCE_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 /*
414 * The IO resource is allocated a range twice as large as it
415 * would normally need. This allows us to set both IO regs.
416 */
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600417 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
419 region.start);
420 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
421 region.end);
422 }
423
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600424 res = bus->resource[1];
425 pcibios_resource_to_bus(bridge, &region, res);
426 if (res->flags & IORESOURCE_IO) {
427 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
429 region.start);
430 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
431 region.end);
432 }
433
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600434 res = bus->resource[2];
435 pcibios_resource_to_bus(bridge, &region, res);
436 if (res->flags & IORESOURCE_MEM) {
437 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
439 region.start);
440 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
441 region.end);
442 }
443
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600444 res = bus->resource[3];
445 pcibios_resource_to_bus(bridge, &region, res);
446 if (res->flags & IORESOURCE_MEM) {
447 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
449 region.start);
450 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
451 region.end);
452 }
453}
Dominik Brodowskib3743fa2005-09-09 13:03:23 -0700454EXPORT_SYMBOL(pci_setup_cardbus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456/* Initialize bridges with base/limit values we have collected.
457 PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
458 requires that if there is no I/O ports or memory behind the
459 bridge, corresponding range must be turned off by writing base
460 value greater than limit to the bridge's base/limit registers.
461
462 Note: care must be taken when updating I/O base/limit registers
463 of bridges which support 32-bit I/O. This update requires two
464 config space writes, so it's quite possible that an I/O window of
465 the bridge will have some undesirable address (e.g. 0) after the
466 first write. Ditto 64-bit prefetchable MMIO. */
Yinghai Lu7cc59972009-12-22 15:02:21 -0800467static void pci_setup_bridge_io(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 struct pci_dev *bridge = bus->self;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600470 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 struct pci_bus_region region;
Bjorn Helgaas2b28ae12012-07-09 13:38:57 -0600472 unsigned long io_mask;
473 u8 io_base_lo, io_limit_lo;
Yinghai Lu7cc59972009-12-22 15:02:21 -0800474 u32 l, io_upper16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Bjorn Helgaas2b28ae12012-07-09 13:38:57 -0600476 io_mask = PCI_IO_RANGE_MASK;
477 if (bridge->io_window_1k)
478 io_mask = PCI_IO_1K_RANGE_MASK;
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 /* Set up the top and bottom of the PCI I/O segment for this bus. */
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600481 res = bus->resource[0];
482 pcibios_resource_to_bus(bridge, &region, res);
483 if (res->flags & IORESOURCE_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 pci_read_config_dword(bridge, PCI_IO_BASE, &l);
485 l &= 0xffff0000;
Bjorn Helgaas2b28ae12012-07-09 13:38:57 -0600486 io_base_lo = (region.start >> 8) & io_mask;
487 io_limit_lo = (region.end >> 8) & io_mask;
488 l |= ((u32) io_limit_lo << 8) | io_base_lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 /* 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
Yinghai Lub55438f2012-02-23 19:23:30 -08001279/*
1280 * -1: undefined, will auto detect later
1281 * 0: disabled by user
1282 * 1: disabled by auto detect
1283 * 2: enabled by user
1284 * 3: enabled by auto detect
1285 */
1286enum enable_type {
1287 undefined = -1,
1288 user_disabled,
1289 auto_disabled,
1290 user_enabled,
1291 auto_enabled,
1292};
1293
1294static enum enable_type pci_realloc_enable __initdata = undefined;
1295void __init pci_realloc_get_opt(char *str)
1296{
1297 if (!strncmp(str, "off", 3))
1298 pci_realloc_enable = user_disabled;
1299 else if (!strncmp(str, "on", 2))
1300 pci_realloc_enable = user_enabled;
1301}
1302static bool __init pci_realloc_enabled(void)
1303{
1304 return pci_realloc_enable >= user_enabled;
1305}
Ram Paif483d392011-07-07 11:19:10 -07001306
Yinghai Lub07f2eb2012-02-23 19:23:32 -08001307static void __init pci_realloc_detect(void)
1308{
1309#if defined(CONFIG_PCI_IOV) && defined(CONFIG_PCI_REALLOC_ENABLE_AUTO)
1310 struct pci_dev *dev = NULL;
1311
1312 if (pci_realloc_enable != undefined)
1313 return;
1314
1315 for_each_pci_dev(dev) {
1316 int i;
1317
1318 for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++) {
1319 struct resource *r = &dev->resource[i];
1320
1321 /* Not assigned, or rejected by kernel ? */
1322 if (r->flags && !r->start) {
1323 pci_realloc_enable = auto_enabled;
1324
1325 return;
1326 }
1327 }
1328 }
1329#endif
1330}
1331
Yinghai Luda7822e2011-05-12 17:11:37 -07001332/*
1333 * first try will not touch pci bridge res
1334 * second and later try will clear small leaf bridge res
1335 * will stop till to the max deepth if can not find good one
1336 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337void __init
1338pci_assign_unassigned_resources(void)
1339{
1340 struct pci_bus *bus;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001341 LIST_HEAD(realloc_head); /* list of resources that
Ram Paic8adf9a2011-02-14 17:43:20 -08001342 want additional resources */
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001343 struct list_head *add_list = NULL;
Yinghai Luda7822e2011-05-12 17:11:37 -07001344 int tried_times = 0;
1345 enum release_type rel_type = leaf_only;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001346 LIST_HEAD(fail_head);
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001347 struct pci_dev_resource *fail_res;
Yinghai Luda7822e2011-05-12 17:11:37 -07001348 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1349 IORESOURCE_PREFETCH;
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001350 int pci_try_num = 1;
Yinghai Luda7822e2011-05-12 17:11:37 -07001351
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001352 /* don't realloc if asked to do so */
Yinghai Lub07f2eb2012-02-23 19:23:32 -08001353 pci_realloc_detect();
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001354 if (pci_realloc_enabled()) {
1355 int max_depth = pci_get_max_depth();
1356
1357 pci_try_num = max_depth + 1;
1358 printk(KERN_DEBUG "PCI: max bus depth: %d pci_try_num: %d\n",
1359 max_depth, pci_try_num);
1360 }
Yinghai Luda7822e2011-05-12 17:11:37 -07001361
1362again:
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001363 /*
1364 * last try will use add_list, otherwise will try good to have as
1365 * must have, so can realloc parent bridge resource
1366 */
1367 if (tried_times + 1 == pci_try_num)
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001368 add_list = &realloc_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 /* Depth first, calculate sizes and alignments of all
1370 subordinate buses. */
Yinghai Luda7822e2011-05-12 17:11:37 -07001371 list_for_each_entry(bus, &pci_root_buses, node)
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001372 __pci_bus_size_bridges(bus, add_list);
Ram Paic8adf9a2011-02-14 17:43:20 -08001373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 /* Depth last, allocate resources and update the hardware. */
Yinghai Luda7822e2011-05-12 17:11:37 -07001375 list_for_each_entry(bus, &pci_root_buses, node)
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001376 __pci_bus_assign_resources(bus, add_list, &fail_head);
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001377 if (add_list)
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001378 BUG_ON(!list_empty(add_list));
Yinghai Luda7822e2011-05-12 17:11:37 -07001379 tried_times++;
1380
1381 /* any device complain? */
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001382 if (list_empty(&fail_head))
Yinghai Luda7822e2011-05-12 17:11:37 -07001383 goto enable_and_dump;
Ram Paif483d392011-07-07 11:19:10 -07001384
Yinghai Lu0c5be0c2012-02-23 19:23:29 -08001385 if (tried_times >= pci_try_num) {
Yinghai Lueb572e72012-02-23 19:23:31 -08001386 if (pci_realloc_enable == undefined)
1387 printk(KERN_INFO "Some PCI device resources are unassigned, try booting with pci=realloc\n");
Yinghai Lub07f2eb2012-02-23 19:23:32 -08001388 else if (pci_realloc_enable == auto_enabled)
1389 printk(KERN_INFO "Automatically enabled pci realloc, if you have problem, try booting with pci=realloc=off\n");
Yinghai Lueb572e72012-02-23 19:23:31 -08001390
Yinghai Lubffc56d2012-01-21 02:08:30 -08001391 free_list(&fail_head);
Yinghai Luda7822e2011-05-12 17:11:37 -07001392 goto enable_and_dump;
1393 }
1394
1395 printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
1396 tried_times + 1);
1397
1398 /* third times and later will not check if it is leaf */
1399 if ((tried_times + 1) > 2)
1400 rel_type = whole_subtree;
1401
1402 /*
1403 * Try to release leaf bridge's resources that doesn't fit resource of
1404 * child device under that bridge
1405 */
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001406 list_for_each_entry(fail_res, &fail_head, list) {
1407 bus = fail_res->dev->bus;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001408 pci_bus_release_bridge_resources(bus,
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001409 fail_res->flags & type_mask,
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001410 rel_type);
Yinghai Luda7822e2011-05-12 17:11:37 -07001411 }
1412 /* restore size and flags */
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001413 list_for_each_entry(fail_res, &fail_head, list) {
1414 struct resource *res = fail_res->res;
Yinghai Luda7822e2011-05-12 17:11:37 -07001415
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001416 res->start = fail_res->start;
1417 res->end = fail_res->end;
1418 res->flags = fail_res->flags;
1419 if (fail_res->dev->subordinate)
Yinghai Luda7822e2011-05-12 17:11:37 -07001420 res->flags = 0;
Yinghai Luda7822e2011-05-12 17:11:37 -07001421 }
Yinghai Lubffc56d2012-01-21 02:08:30 -08001422 free_list(&fail_head);
Yinghai Luda7822e2011-05-12 17:11:37 -07001423
1424 goto again;
1425
1426enable_and_dump:
1427 /* Depth last, update the hardware. */
1428 list_for_each_entry(bus, &pci_root_buses, node)
1429 pci_enable_bridges(bus);
Yinghai Lu76fbc262008-06-23 20:33:06 +02001430
1431 /* dump the resource on buses */
Yinghai Luda7822e2011-05-12 17:11:37 -07001432 list_for_each_entry(bus, &pci_root_buses, node)
Yinghai Lu76fbc262008-06-23 20:33:06 +02001433 pci_bus_dump_resources(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434}
Yinghai Lu6841ec62010-01-22 01:02:25 -08001435
1436void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
1437{
1438 struct pci_bus *parent = bridge->subordinate;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001439 LIST_HEAD(add_list); /* list of resources that
Yinghai Lu8424d752012-01-21 02:08:21 -08001440 want additional resources */
Yinghai Lu32180e402010-01-22 01:02:27 -08001441 int tried_times = 0;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001442 LIST_HEAD(fail_head);
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001443 struct pci_dev_resource *fail_res;
Yinghai Lu6841ec62010-01-22 01:02:25 -08001444 int retval;
Yinghai Lu32180e402010-01-22 01:02:27 -08001445 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1446 IORESOURCE_PREFETCH;
Yinghai Lu6841ec62010-01-22 01:02:25 -08001447
Yinghai Lu32180e402010-01-22 01:02:27 -08001448again:
Yinghai Lu8424d752012-01-21 02:08:21 -08001449 __pci_bus_size_bridges(parent, &add_list);
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001450 __pci_bridge_assign_resources(bridge, &add_list, &fail_head);
1451 BUG_ON(!list_empty(&add_list));
Yinghai Lu32180e402010-01-22 01:02:27 -08001452 tried_times++;
1453
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001454 if (list_empty(&fail_head))
Yinghai Lu3f579c32010-05-21 14:35:06 -07001455 goto enable_all;
Yinghai Lu32180e402010-01-22 01:02:27 -08001456
1457 if (tried_times >= 2) {
1458 /* still fail, don't need to try more */
Yinghai Lubffc56d2012-01-21 02:08:30 -08001459 free_list(&fail_head);
Yinghai Lu3f579c32010-05-21 14:35:06 -07001460 goto enable_all;
Yinghai Lu32180e402010-01-22 01:02:27 -08001461 }
1462
1463 printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
1464 tried_times + 1);
1465
1466 /*
1467 * Try to release leaf bridge's resources that doesn't fit resource of
1468 * child device under that bridge
1469 */
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001470 list_for_each_entry(fail_res, &fail_head, list) {
1471 struct pci_bus *bus = fail_res->dev->bus;
1472 unsigned long flags = fail_res->flags;
Yinghai Lu32180e402010-01-22 01:02:27 -08001473
1474 pci_bus_release_bridge_resources(bus, flags & type_mask,
1475 whole_subtree);
Yinghai Lu32180e402010-01-22 01:02:27 -08001476 }
1477 /* restore size and flags */
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001478 list_for_each_entry(fail_res, &fail_head, list) {
1479 struct resource *res = fail_res->res;
Yinghai Lu32180e402010-01-22 01:02:27 -08001480
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001481 res->start = fail_res->start;
1482 res->end = fail_res->end;
1483 res->flags = fail_res->flags;
1484 if (fail_res->dev->subordinate)
Yinghai Lu32180e402010-01-22 01:02:27 -08001485 res->flags = 0;
Yinghai Lu32180e402010-01-22 01:02:27 -08001486 }
Yinghai Lubffc56d2012-01-21 02:08:30 -08001487 free_list(&fail_head);
Yinghai Lu32180e402010-01-22 01:02:27 -08001488
1489 goto again;
Yinghai Lu3f579c32010-05-21 14:35:06 -07001490
1491enable_all:
1492 retval = pci_reenable_device(bridge);
1493 pci_set_master(bridge);
1494 pci_enable_bridges(parent);
Yinghai Lu6841ec62010-01-22 01:02:25 -08001495}
1496EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
Yinghai Lu9b030882012-01-21 02:08:23 -08001497
1498#ifdef CONFIG_HOTPLUG
1499/**
1500 * pci_rescan_bus - scan a PCI bus for devices.
1501 * @bus: PCI bus to scan
1502 *
1503 * Scan a PCI bus and child buses for new devices, adds them,
1504 * and enables them.
1505 *
1506 * Returns the max number of subordinate bus discovered.
1507 */
1508unsigned int __ref pci_rescan_bus(struct pci_bus *bus)
1509{
1510 unsigned int max;
1511 struct pci_dev *dev;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001512 LIST_HEAD(add_list); /* list of resources that
Yinghai Lu9b030882012-01-21 02:08:23 -08001513 want additional resources */
1514
1515 max = pci_scan_child_bus(bus);
1516
Yinghai Lu9b030882012-01-21 02:08:23 -08001517 down_read(&pci_bus_sem);
1518 list_for_each_entry(dev, &bus->devices, bus_list)
1519 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
1520 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
1521 if (dev->subordinate)
1522 __pci_bus_size_bridges(dev->subordinate,
1523 &add_list);
1524 up_read(&pci_bus_sem);
1525 __pci_bus_assign_resources(bus, &add_list, NULL);
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001526 BUG_ON(!list_empty(&add_list));
Yinghai Lu9b030882012-01-21 02:08:23 -08001527
1528 pci_enable_bridges(bus);
1529 pci_bus_add_devices(bus);
1530
1531 return max;
1532}
1533EXPORT_SYMBOL_GPL(pci_rescan_bus);
1534#endif