blob: c09c67ab5612cd500ebb078018ddbf07bea8abb7 [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 Lu568ddef2010-01-22 01:02:21 -080030struct resource_list_x {
31 struct resource_list_x *next;
32 struct resource *res;
33 struct pci_dev *dev;
34 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
Ram Pai094732a2011-02-14 17:43:18 -080041#define free_list(type, head) do { \
42 struct type *list, *tmp; \
43 for (list = (head)->next; list;) { \
44 tmp = list; \
45 list = list->next; \
46 kfree(tmp); \
47 } \
48 (head)->next = NULL; \
49} while (0)
50
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 Luef62dfe2012-01-21 02:08:18 -080067static int add_to_list(struct resource_list_x *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{
71 struct resource_list_x *list = head;
72 struct resource_list_x *ln = list->next;
73 struct resource_list_x *tmp;
74
75 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
76 if (!tmp) {
Ram Paic8adf9a2011-02-14 17:43:20 -080077 pr_warning("add_to_list: kmalloc() failed!\n");
Yinghai Luef62dfe2012-01-21 02:08:18 -080078 return -ENOMEM;
Yinghai Lu568ddef2010-01-22 01:02:21 -080079 }
80
81 tmp->next = ln;
82 tmp->res = res;
83 tmp->dev = dev;
84 tmp->start = res->start;
85 tmp->end = res->end;
86 tmp->flags = res->flags;
Ram Paic8adf9a2011-02-14 17:43:20 -080087 tmp->add_size = add_size;
Ram Pai2bbc6942011-07-25 13:08:39 -070088 tmp->min_align = min_align;
Yinghai Lu568ddef2010-01-22 01:02:21 -080089 list->next = tmp;
Yinghai Luef62dfe2012-01-21 02:08:18 -080090
91 return 0;
Yinghai Lu568ddef2010-01-22 01:02:21 -080092}
93
Ram Paic8adf9a2011-02-14 17:43:20 -080094static void add_to_failed_list(struct resource_list_x *head,
95 struct pci_dev *dev, struct resource *res)
96{
Ram Pai2bbc6942011-07-25 13:08:39 -070097 add_to_list(head, dev, res,
98 0 /* dont care */,
99 0 /* dont care */);
Ram Paic8adf9a2011-02-14 17:43:20 -0800100}
101
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800102static void remove_from_list(struct resource_list_x *realloc_head,
103 struct resource *res)
104{
105 struct resource_list_x *prev, *tmp, *list;
106
107 prev = realloc_head;
108 for (list = realloc_head->next; list;) {
109 if (list->res != res) {
110 prev = list;
111 list = list->next;
112 continue;
113 }
114 tmp = list;
115 prev->next = list = list->next;
116 kfree(tmp);
117 }
118}
119
Yinghai Lu1c372352012-01-21 02:08:19 -0800120static resource_size_t get_res_add_size(struct resource_list_x *realloc_head,
121 struct resource *res)
122{
123 struct resource_list_x *list;
124
125 /* check if it is in realloc_head list */
126 for (list = realloc_head->next; list && list->res != res;
127 list = list->next)
128 ;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800129
130 if (list) {
131 dev_printk(KERN_DEBUG, &list->dev->dev,
132 "%pR get_res_add_size add_size %llx\n",
133 list->res, (unsigned long long)list->add_size);
Yinghai Lu1c372352012-01-21 02:08:19 -0800134 return list->add_size;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800135 }
Yinghai Lu1c372352012-01-21 02:08:19 -0800136
137 return 0;
138}
139
Yinghai Lu6841ec62010-01-22 01:02:25 -0800140static void __dev_sort_resources(struct pci_dev *dev,
141 struct resource_list *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Yinghai Lu6841ec62010-01-22 01:02:25 -0800143 u16 class = dev->class >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Yinghai Lu6841ec62010-01-22 01:02:25 -0800145 /* Don't touch classless devices or host bridges or ioapics. */
146 if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST)
147 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Yinghai Lu6841ec62010-01-22 01:02:25 -0800149 /* Don't touch ioapic devices already enabled by firmware */
150 if (class == PCI_CLASS_SYSTEM_PIC) {
151 u16 command;
152 pci_read_config_word(dev, PCI_COMMAND, &command);
153 if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
154 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 }
156
Yinghai Lu6841ec62010-01-22 01:02:25 -0800157 pdev_sort_resources(dev, head);
158}
159
Ram Paifc075e12011-02-14 17:43:19 -0800160static inline void reset_resource(struct resource *res)
161{
162 res->start = 0;
163 res->end = 0;
164 res->flags = 0;
165}
166
Ram Paic8adf9a2011-02-14 17:43:20 -0800167/**
Ram Pai9e8bf932011-07-25 13:08:42 -0700168 * reassign_resources_sorted() - satisfy any additional resource requests
Ram Paic8adf9a2011-02-14 17:43:20 -0800169 *
Ram Pai9e8bf932011-07-25 13:08:42 -0700170 * @realloc_head : head of the list tracking requests requiring additional
Ram Paic8adf9a2011-02-14 17:43:20 -0800171 * resources
172 * @head : head of the list tracking requests with allocated
173 * resources
174 *
Ram Pai9e8bf932011-07-25 13:08:42 -0700175 * Walk through each element of the realloc_head and try to procure
Ram Paic8adf9a2011-02-14 17:43:20 -0800176 * additional resources for the element, provided the element
177 * is in the head list.
178 */
Ram Pai9e8bf932011-07-25 13:08:42 -0700179static void reassign_resources_sorted(struct resource_list_x *realloc_head,
Ram Paic8adf9a2011-02-14 17:43:20 -0800180 struct resource_list *head)
181{
182 struct resource *res;
183 struct resource_list_x *list, *tmp, *prev;
184 struct resource_list *hlist;
185 resource_size_t add_size;
186 int idx;
187
Ram Pai9e8bf932011-07-25 13:08:42 -0700188 prev = realloc_head;
189 for (list = realloc_head->next; list;) {
Ram Paic8adf9a2011-02-14 17:43:20 -0800190 res = list->res;
191 /* skip resource that has been reset */
192 if (!res->flags)
193 goto out;
194
195 /* skip this resource if not found in head list */
196 for (hlist = head->next; hlist && hlist->res != res;
197 hlist = hlist->next);
198 if (!hlist) { /* just skip */
199 prev = list;
200 list = list->next;
201 continue;
202 }
203
204 idx = res - &list->dev->resource[0];
205 add_size=list->add_size;
Ram Pai2bbc6942011-07-25 13:08:39 -0700206 if (!resource_size(res)) {
Ram Pai0a2daa12011-07-25 13:08:41 -0700207 res->start = list->start;
Ram Pai2bbc6942011-07-25 13:08:39 -0700208 res->end = res->start + add_size - 1;
209 if(pci_assign_resource(list->dev, idx))
Ram Paic8adf9a2011-02-14 17:43:20 -0800210 reset_resource(res);
Ram Pai2bbc6942011-07-25 13:08:39 -0700211 } else {
212 resource_size_t align = list->min_align;
213 res->flags |= list->flags & (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN);
214 if (pci_reassign_resource(list->dev, idx, add_size, align))
215 dev_printk(KERN_DEBUG, &list->dev->dev, "failed to add optional resources res=%pR\n",
216 res);
Ram Paic8adf9a2011-02-14 17:43:20 -0800217 }
218out:
219 tmp = list;
220 prev->next = list = list->next;
221 kfree(tmp);
222 }
223}
224
225/**
226 * assign_requested_resources_sorted() - satisfy resource requests
227 *
228 * @head : head of the list tracking requests for resources
229 * @failed_list : head of the list tracking requests that could
230 * not be allocated
231 *
232 * Satisfy resource requests of each element in the list. Add
233 * requests that could not satisfied to the failed_list.
234 */
235static void assign_requested_resources_sorted(struct resource_list *head,
Yinghai Lu6841ec62010-01-22 01:02:25 -0800236 struct resource_list_x *fail_head)
237{
238 struct resource *res;
Ram Paic8adf9a2011-02-14 17:43:20 -0800239 struct resource_list *list;
Yinghai Lu6841ec62010-01-22 01:02:25 -0800240 int idx;
241
Ram Paic8adf9a2011-02-14 17:43:20 -0800242 for (list = head->next; list; list = list->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 res = list->res;
244 idx = res - &list->dev->resource[0];
Ram Paic8adf9a2011-02-14 17:43:20 -0800245 if (resource_size(res) && pci_assign_resource(list->dev, idx)) {
Yinghai Lu9a928662010-02-28 15:49:39 -0800246 if (fail_head && !pci_is_root_bus(list->dev->bus)) {
247 /*
248 * if the failed res is for ROM BAR, and it will
249 * be enabled later, don't add it to the list
250 */
251 if (!((idx == PCI_ROM_RESOURCE) &&
252 (!(res->flags & IORESOURCE_ROM_ENABLE))))
253 add_to_failed_list(fail_head, list->dev, res);
254 }
Ram Paifc075e12011-02-14 17:43:19 -0800255 reset_resource(res);
Rajesh Shah542df5d2005-04-28 00:25:50 -0700256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258}
259
Ram Paic8adf9a2011-02-14 17:43:20 -0800260static void __assign_resources_sorted(struct resource_list *head,
Ram Pai9e8bf932011-07-25 13:08:42 -0700261 struct resource_list_x *realloc_head,
Ram Paic8adf9a2011-02-14 17:43:20 -0800262 struct resource_list_x *fail_head)
263{
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800264 /*
265 * Should not assign requested resources at first.
266 * they could be adjacent, so later reassign can not reallocate
267 * them one by one in parent resource window.
268 * Try to assign requested + add_size at begining
269 * if could do that, could get out early.
270 * if could not do that, we still try to assign requested at first,
271 * then try to reassign add_size for some resources.
272 */
273 struct resource_list_x save_head, local_fail_head, *list;
274 struct resource_list *l;
275
276 /* Check if optional add_size is there */
277 if (!realloc_head || !realloc_head->next)
278 goto requested_and_reassign;
279
280 /* Save original start, end, flags etc at first */
281 save_head.next = NULL;
282 for (l = head->next; l; l = l->next)
283 if (add_to_list(&save_head, l->dev, l->res, 0, 0)) {
284 free_list(resource_list_x, &save_head);
285 goto requested_and_reassign;
286 }
287
288 /* Update res in head list with add_size in realloc_head list */
289 for (l = head->next; l; l = l->next)
290 l->res->end += get_res_add_size(realloc_head, l->res);
291
292 /* Try updated head list with add_size added */
293 local_fail_head.next = NULL;
294 assign_requested_resources_sorted(head, &local_fail_head);
295
296 /* all assigned with add_size ? */
297 if (!local_fail_head.next) {
298 /* Remove head list from realloc_head list */
299 for (l = head->next; l; l = l->next)
300 remove_from_list(realloc_head, l->res);
301 free_list(resource_list_x, &save_head);
302 free_list(resource_list, head);
303 return;
304 }
305
306 free_list(resource_list_x, &local_fail_head);
307 /* Release assigned resource */
308 for (l = head->next; l; l = l->next)
309 if (l->res->parent)
310 release_resource(l->res);
311 /* Restore start/end/flags from saved list */
312 for (list = save_head.next; list; list = list->next) {
313 struct resource *res = list->res;
314
315 res->start = list->start;
316 res->end = list->end;
317 res->flags = list->flags;
318 }
319 free_list(resource_list_x, &save_head);
320
321requested_and_reassign:
Ram Paic8adf9a2011-02-14 17:43:20 -0800322 /* Satisfy the must-have resource requests */
323 assign_requested_resources_sorted(head, fail_head);
324
Ram Pai0a2daa12011-07-25 13:08:41 -0700325 /* Try to satisfy any additional optional resource
Ram Paic8adf9a2011-02-14 17:43:20 -0800326 requests */
Ram Pai9e8bf932011-07-25 13:08:42 -0700327 if (realloc_head)
328 reassign_resources_sorted(realloc_head, head);
Ram Paic8adf9a2011-02-14 17:43:20 -0800329 free_list(resource_list, head);
330}
331
Yinghai Lu6841ec62010-01-22 01:02:25 -0800332static void pdev_assign_resources_sorted(struct pci_dev *dev,
Yinghai Lu8424d752012-01-21 02:08:21 -0800333 struct resource_list_x *add_head,
Yinghai Lu6841ec62010-01-22 01:02:25 -0800334 struct resource_list_x *fail_head)
335{
336 struct resource_list head;
337
338 head.next = NULL;
339 __dev_sort_resources(dev, &head);
Yinghai Lu8424d752012-01-21 02:08:21 -0800340 __assign_resources_sorted(&head, add_head, fail_head);
Yinghai Lu6841ec62010-01-22 01:02:25 -0800341
342}
343
344static void pbus_assign_resources_sorted(const struct pci_bus *bus,
Ram Pai9e8bf932011-07-25 13:08:42 -0700345 struct resource_list_x *realloc_head,
Yinghai Lu6841ec62010-01-22 01:02:25 -0800346 struct resource_list_x *fail_head)
347{
348 struct pci_dev *dev;
349 struct resource_list head;
350
351 head.next = NULL;
352 list_for_each_entry(dev, &bus->devices, bus_list)
353 __dev_sort_resources(dev, &head);
354
Ram Pai9e8bf932011-07-25 13:08:42 -0700355 __assign_resources_sorted(&head, realloc_head, fail_head);
Yinghai Lu6841ec62010-01-22 01:02:25 -0800356}
357
Dominik Brodowskib3743fa2005-09-09 13:03:23 -0700358void pci_setup_cardbus(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360 struct pci_dev *bridge = bus->self;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600361 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 struct pci_bus_region region;
363
Bjorn Helgaas865df572009-11-04 10:32:57 -0700364 dev_info(&bridge->dev, "CardBus bridge to [bus %02x-%02x]\n",
365 bus->secondary, bus->subordinate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600367 res = bus->resource[0];
368 pcibios_resource_to_bus(bridge, &region, res);
369 if (res->flags & IORESOURCE_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 /*
371 * The IO resource is allocated a range twice as large as it
372 * would normally need. This allows us to set both IO regs.
373 */
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600374 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
376 region.start);
377 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
378 region.end);
379 }
380
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600381 res = bus->resource[1];
382 pcibios_resource_to_bus(bridge, &region, res);
383 if (res->flags & IORESOURCE_IO) {
384 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
386 region.start);
387 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
388 region.end);
389 }
390
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600391 res = bus->resource[2];
392 pcibios_resource_to_bus(bridge, &region, res);
393 if (res->flags & IORESOURCE_MEM) {
394 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
396 region.start);
397 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
398 region.end);
399 }
400
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600401 res = bus->resource[3];
402 pcibios_resource_to_bus(bridge, &region, res);
403 if (res->flags & IORESOURCE_MEM) {
404 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
406 region.start);
407 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
408 region.end);
409 }
410}
Dominik Brodowskib3743fa2005-09-09 13:03:23 -0700411EXPORT_SYMBOL(pci_setup_cardbus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413/* Initialize bridges with base/limit values we have collected.
414 PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
415 requires that if there is no I/O ports or memory behind the
416 bridge, corresponding range must be turned off by writing base
417 value greater than limit to the bridge's base/limit registers.
418
419 Note: care must be taken when updating I/O base/limit registers
420 of bridges which support 32-bit I/O. This update requires two
421 config space writes, so it's quite possible that an I/O window of
422 the bridge will have some undesirable address (e.g. 0) after the
423 first write. Ditto 64-bit prefetchable MMIO. */
Yinghai Lu7cc59972009-12-22 15:02:21 -0800424static void pci_setup_bridge_io(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
426 struct pci_dev *bridge = bus->self;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600427 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 struct pci_bus_region region;
Yinghai Lu7cc59972009-12-22 15:02:21 -0800429 u32 l, io_upper16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 /* Set up the top and bottom of the PCI I/O segment for this bus. */
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600432 res = bus->resource[0];
433 pcibios_resource_to_bus(bridge, &region, res);
434 if (res->flags & IORESOURCE_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 pci_read_config_dword(bridge, PCI_IO_BASE, &l);
436 l &= 0xffff0000;
437 l |= (region.start >> 8) & 0x00f0;
438 l |= region.end & 0xf000;
439 /* Set up upper 16 bits of I/O base/limit. */
440 io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600441 dev_info(&bridge->dev, " bridge window %pR\n", res);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800442 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 /* Clear upper 16 bits of I/O base/limit. */
444 io_upper16 = 0;
445 l = 0x00f0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
447 /* Temporarily disable the I/O range before updating PCI_IO_BASE. */
448 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
449 /* Update lower 16 bits of I/O base/limit. */
450 pci_write_config_dword(bridge, PCI_IO_BASE, l);
451 /* Update upper 16 bits of I/O base/limit. */
452 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800453}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Yinghai Lu7cc59972009-12-22 15:02:21 -0800455static void pci_setup_bridge_mmio(struct pci_bus *bus)
456{
457 struct pci_dev *bridge = bus->self;
458 struct resource *res;
459 struct pci_bus_region region;
460 u32 l;
461
462 /* Set up the top and bottom of the PCI Memory segment for this bus. */
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600463 res = bus->resource[1];
464 pcibios_resource_to_bus(bridge, &region, res);
465 if (res->flags & IORESOURCE_MEM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 l = (region.start >> 16) & 0xfff0;
467 l |= region.end & 0xfff00000;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600468 dev_info(&bridge->dev, " bridge window %pR\n", res);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800469 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 l = 0x0000fff0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
472 pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800473}
474
475static void pci_setup_bridge_mmio_pref(struct pci_bus *bus)
476{
477 struct pci_dev *bridge = bus->self;
478 struct resource *res;
479 struct pci_bus_region region;
480 u32 l, bu, lu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 /* Clear out the upper 32 bits of PREF limit.
483 If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
484 disables PREF range, which is ok. */
485 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
486
487 /* Set up PREF base/limit. */
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100488 bu = lu = 0;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600489 res = bus->resource[2];
490 pcibios_resource_to_bus(bridge, &region, res);
491 if (res->flags & IORESOURCE_PREFETCH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 l = (region.start >> 16) & 0xfff0;
493 l |= region.end & 0xfff00000;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600494 if (res->flags & IORESOURCE_MEM_64) {
Yinghai Lu1f82de12009-04-23 20:48:32 -0700495 bu = upper_32_bits(region.start);
496 lu = upper_32_bits(region.end);
Yinghai Lu1f82de12009-04-23 20:48:32 -0700497 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600498 dev_info(&bridge->dev, " bridge window %pR\n", res);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800499 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 l = 0x0000fff0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
502 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
503
Alex Williamson59353ea2009-11-30 14:51:44 -0700504 /* Set the upper 32 bits of PREF base & limit. */
505 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
506 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800507}
508
509static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type)
510{
511 struct pci_dev *bridge = bus->self;
512
Yinghai Lu7cc59972009-12-22 15:02:21 -0800513 dev_info(&bridge->dev, "PCI bridge to [bus %02x-%02x]\n",
514 bus->secondary, bus->subordinate);
515
516 if (type & IORESOURCE_IO)
517 pci_setup_bridge_io(bus);
518
519 if (type & IORESOURCE_MEM)
520 pci_setup_bridge_mmio(bus);
521
522 if (type & IORESOURCE_PREFETCH)
523 pci_setup_bridge_mmio_pref(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
526}
527
Benjamin Herrenschmidte2444272011-09-11 14:08:38 -0300528void pci_setup_bridge(struct pci_bus *bus)
Yinghai Lu7cc59972009-12-22 15:02:21 -0800529{
530 unsigned long type = IORESOURCE_IO | IORESOURCE_MEM |
531 IORESOURCE_PREFETCH;
532
533 __pci_setup_bridge(bus, type);
534}
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536/* Check whether the bridge supports optional I/O and
537 prefetchable memory ranges. If not, the respective
538 base/limit registers must be read-only and read as 0. */
Sam Ravnborg96bde062007-03-26 21:53:30 -0800539static void pci_bridge_check_ranges(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
541 u16 io;
542 u32 pmem;
543 struct pci_dev *bridge = bus->self;
544 struct resource *b_res;
545
546 b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
547 b_res[1].flags |= IORESOURCE_MEM;
548
549 pci_read_config_word(bridge, PCI_IO_BASE, &io);
550 if (!io) {
551 pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0);
552 pci_read_config_word(bridge, PCI_IO_BASE, &io);
553 pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
554 }
555 if (io)
556 b_res[0].flags |= IORESOURCE_IO;
557 /* DECchip 21050 pass 2 errata: the bridge may miss an address
558 disconnect boundary by one PCI data phase.
559 Workaround: do not use prefetching on this device. */
560 if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001)
561 return;
562 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
563 if (!pmem) {
564 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE,
565 0xfff0fff0);
566 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
567 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0);
568 }
Yinghai Lu1f82de12009-04-23 20:48:32 -0700569 if (pmem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
Yinghai Lu99586102010-01-22 01:02:28 -0800571 if ((pmem & PCI_PREF_RANGE_TYPE_MASK) ==
572 PCI_PREF_RANGE_TYPE_64) {
Yinghai Lu1f82de12009-04-23 20:48:32 -0700573 b_res[2].flags |= IORESOURCE_MEM_64;
Yinghai Lu99586102010-01-22 01:02:28 -0800574 b_res[2].flags |= PCI_PREF_RANGE_TYPE_64;
575 }
Yinghai Lu1f82de12009-04-23 20:48:32 -0700576 }
577
578 /* double check if bridge does support 64 bit pref */
579 if (b_res[2].flags & IORESOURCE_MEM_64) {
580 u32 mem_base_hi, tmp;
581 pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32,
582 &mem_base_hi);
583 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
584 0xffffffff);
585 pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp);
586 if (!tmp)
587 b_res[2].flags &= ~IORESOURCE_MEM_64;
588 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
589 mem_base_hi);
590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
593/* Helper function for sizing routines: find first available
594 bus resource of a given type. Note: we intentionally skip
595 the bus resources which have already been assigned (that is,
596 have non-NULL parent resource). */
Sam Ravnborg96bde062007-03-26 21:53:30 -0800597static struct resource *find_free_bus_resource(struct pci_bus *bus, unsigned long type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
599 int i;
600 struct resource *r;
601 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
602 IORESOURCE_PREFETCH;
603
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -0700604 pci_bus_for_each_resource(bus, r, i) {
Ivan Kokshaysky299de032005-06-15 18:59:27 +0400605 if (r == &ioport_resource || r == &iomem_resource)
606 continue;
Jesse Barnes55a10982009-10-27 09:39:18 -0700607 if (r && (r->flags & type_mask) == type && !r->parent)
608 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
610 return NULL;
611}
612
Ram Pai13583b12011-02-14 17:43:17 -0800613static resource_size_t calculate_iosize(resource_size_t size,
614 resource_size_t min_size,
615 resource_size_t size1,
616 resource_size_t old_size,
617 resource_size_t align)
618{
619 if (size < min_size)
620 size = min_size;
621 if (old_size == 1 )
622 old_size = 0;
623 /* To be fixed in 2.5: we should have sort of HAVE_ISA
624 flag in the struct pci_bus. */
625#if defined(CONFIG_ISA) || defined(CONFIG_EISA)
626 size = (size & 0xff) + ((size & ~0xffUL) << 2);
627#endif
628 size = ALIGN(size + size1, align);
629 if (size < old_size)
630 size = old_size;
631 return size;
632}
633
634static resource_size_t calculate_memsize(resource_size_t size,
635 resource_size_t min_size,
636 resource_size_t size1,
637 resource_size_t old_size,
638 resource_size_t align)
639{
640 if (size < min_size)
641 size = min_size;
642 if (old_size == 1 )
643 old_size = 0;
644 if (size < old_size)
645 size = old_size;
646 size = ALIGN(size + size1, align);
647 return size;
648}
649
Ram Paic8adf9a2011-02-14 17:43:20 -0800650/**
651 * pbus_size_io() - size the io window of a given bus
652 *
653 * @bus : the bus
654 * @min_size : the minimum io window that must to be allocated
655 * @add_size : additional optional io window
Ram Pai9e8bf932011-07-25 13:08:42 -0700656 * @realloc_head : track the additional io window on this list
Ram Paic8adf9a2011-02-14 17:43:20 -0800657 *
658 * Sizing the IO windows of the PCI-PCI bridge is trivial,
659 * since these windows have 4K granularity and the IO ranges
660 * of non-bridge PCI devices are limited to 256 bytes.
661 * We must be careful with the ISA aliasing though.
662 */
663static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size,
Ram Pai9e8bf932011-07-25 13:08:42 -0700664 resource_size_t add_size, struct resource_list_x *realloc_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
666 struct pci_dev *dev;
667 struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO);
Ram Paic8adf9a2011-02-14 17:43:20 -0800668 unsigned long size = 0, size0 = 0, size1 = 0;
Yinghai Lube768912011-07-25 13:08:38 -0700669 resource_size_t children_add_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 if (!b_res)
672 return;
673
674 list_for_each_entry(dev, &bus->devices, bus_list) {
675 int i;
676
677 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
678 struct resource *r = &dev->resource[i];
679 unsigned long r_size;
680
681 if (r->parent || !(r->flags & IORESOURCE_IO))
682 continue;
Zhao, Yu022edd82008-10-13 19:24:28 +0800683 r_size = resource_size(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 if (r_size < 0x400)
686 /* Might be re-aligned for ISA */
687 size += r_size;
688 else
689 size1 += r_size;
Yinghai Lube768912011-07-25 13:08:38 -0700690
Ram Pai9e8bf932011-07-25 13:08:42 -0700691 if (realloc_head)
692 children_add_size += get_res_add_size(realloc_head, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
694 }
Ram Paic8adf9a2011-02-14 17:43:20 -0800695 size0 = calculate_iosize(size, min_size, size1,
Ram Pai13583b12011-02-14 17:43:17 -0800696 resource_size(b_res), 4096);
Yinghai Lube768912011-07-25 13:08:38 -0700697 if (children_add_size > add_size)
698 add_size = children_add_size;
Ram Pai9e8bf932011-07-25 13:08:42 -0700699 size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
Yinghai Lua4ac9fe2012-01-21 02:08:17 -0800700 calculate_iosize(size, min_size, add_size + size1,
Ram Paic8adf9a2011-02-14 17:43:20 -0800701 resource_size(b_res), 4096);
702 if (!size0 && !size1) {
Bjorn Helgaas865df572009-11-04 10:32:57 -0700703 if (b_res->start || b_res->end)
704 dev_info(&bus->self->dev, "disabling bridge window "
705 "%pR to [bus %02x-%02x] (unused)\n", b_res,
706 bus->secondary, bus->subordinate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 b_res->flags = 0;
708 return;
709 }
710 /* Alignment of the IO window is always 4K */
711 b_res->start = 4096;
Ram Paic8adf9a2011-02-14 17:43:20 -0800712 b_res->end = b_res->start + size0 - 1;
Ivan Kokshaysky88452562008-03-30 19:50:14 +0400713 b_res->flags |= IORESOURCE_STARTALIGN;
Ram Pai9e8bf932011-07-25 13:08:42 -0700714 if (size1 > size0 && realloc_head)
715 add_to_list(realloc_head, bus->self, b_res, size1-size0, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
Ram Paic8adf9a2011-02-14 17:43:20 -0800718/**
719 * pbus_size_mem() - size the memory window of a given bus
720 *
721 * @bus : the bus
722 * @min_size : the minimum memory window that must to be allocated
723 * @add_size : additional optional memory window
Ram Pai9e8bf932011-07-25 13:08:42 -0700724 * @realloc_head : track the additional memory window on this list
Ram Paic8adf9a2011-02-14 17:43:20 -0800725 *
726 * Calculate the size of the bus and minimal alignment which
727 * guarantees that all child resources fit in this size.
728 */
Eric W. Biederman28760482009-09-09 14:09:24 -0700729static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
Ram Paic8adf9a2011-02-14 17:43:20 -0800730 unsigned long type, resource_size_t min_size,
731 resource_size_t add_size,
Ram Pai9e8bf932011-07-25 13:08:42 -0700732 struct resource_list_x *realloc_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
734 struct pci_dev *dev;
Ram Paic8adf9a2011-02-14 17:43:20 -0800735 resource_size_t min_align, align, size, size0, size1;
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100736 resource_size_t aligns[12]; /* Alignments from 1Mb to 2Gb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 int order, max_order;
738 struct resource *b_res = find_free_bus_resource(bus, type);
Yinghai Lu1f82de12009-04-23 20:48:32 -0700739 unsigned int mem64_mask = 0;
Yinghai Lube768912011-07-25 13:08:38 -0700740 resource_size_t children_add_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742 if (!b_res)
743 return 0;
744
745 memset(aligns, 0, sizeof(aligns));
746 max_order = 0;
747 size = 0;
748
Yinghai Lu1f82de12009-04-23 20:48:32 -0700749 mem64_mask = b_res->flags & IORESOURCE_MEM_64;
750 b_res->flags &= ~IORESOURCE_MEM_64;
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 list_for_each_entry(dev, &bus->devices, bus_list) {
753 int i;
Yinghai Lu1f82de12009-04-23 20:48:32 -0700754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
756 struct resource *r = &dev->resource[i];
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100757 resource_size_t r_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 if (r->parent || (r->flags & mask) != type)
760 continue;
Zhao, Yu022edd82008-10-13 19:24:28 +0800761 r_size = resource_size(r);
Yinghai Lu2aceefc2011-07-25 13:08:40 -0700762#ifdef CONFIG_PCI_IOV
763 /* put SRIOV requested res to the optional list */
Ram Pai9e8bf932011-07-25 13:08:42 -0700764 if (realloc_head && i >= PCI_IOV_RESOURCES &&
Yinghai Lu2aceefc2011-07-25 13:08:40 -0700765 i <= PCI_IOV_RESOURCE_END) {
766 r->end = r->start - 1;
Ram Pai9e8bf932011-07-25 13:08:42 -0700767 add_to_list(realloc_head, dev, r, r_size, 0/* dont' care */);
Yinghai Lu2aceefc2011-07-25 13:08:40 -0700768 children_add_size += r_size;
769 continue;
770 }
771#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 /* For bridges size != alignment */
Chris Wright6faf17f2009-08-28 13:00:06 -0700773 align = pci_resource_alignment(dev, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 order = __ffs(align) - 20;
775 if (order > 11) {
Bjorn Helgaas865df572009-11-04 10:32:57 -0700776 dev_warn(&dev->dev, "disabling BAR %d: %pR "
777 "(bad alignment %#llx)\n", i, r,
778 (unsigned long long) align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 r->flags = 0;
780 continue;
781 }
782 size += r_size;
783 if (order < 0)
784 order = 0;
785 /* Exclude ranges with size > align from
786 calculation of the alignment. */
787 if (r_size == align)
788 aligns[order] += align;
789 if (order > max_order)
790 max_order = order;
Yinghai Lu1f82de12009-04-23 20:48:32 -0700791 mem64_mask &= r->flags & IORESOURCE_MEM_64;
Yinghai Lube768912011-07-25 13:08:38 -0700792
Ram Pai9e8bf932011-07-25 13:08:42 -0700793 if (realloc_head)
794 children_add_size += get_res_add_size(realloc_head, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 align = 0;
798 min_align = 0;
799 for (order = 0; order <= max_order; order++) {
Jeremy Fitzhardinge8308c542008-09-11 01:31:50 -0700800 resource_size_t align1 = 1;
801
802 align1 <<= (order + 20);
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (!align)
805 min_align = align1;
Milind Arun Choudhary6f6f8c22007-07-09 11:55:51 -0700806 else if (ALIGN(align + min_align, min_align) < align1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 min_align = align1 >> 1;
808 align += aligns[order];
809 }
Linus Torvaldsb42282e2011-04-11 10:53:11 -0700810 size0 = calculate_memsize(size, min_size, 0, resource_size(b_res), min_align);
Yinghai Lube768912011-07-25 13:08:38 -0700811 if (children_add_size > add_size)
812 add_size = children_add_size;
Ram Pai9e8bf932011-07-25 13:08:42 -0700813 size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
Yinghai Lua4ac9fe2012-01-21 02:08:17 -0800814 calculate_memsize(size, min_size, add_size,
Linus Torvaldsb42282e2011-04-11 10:53:11 -0700815 resource_size(b_res), min_align);
Ram Paic8adf9a2011-02-14 17:43:20 -0800816 if (!size0 && !size1) {
Bjorn Helgaas865df572009-11-04 10:32:57 -0700817 if (b_res->start || b_res->end)
818 dev_info(&bus->self->dev, "disabling bridge window "
819 "%pR to [bus %02x-%02x] (unused)\n", b_res,
820 bus->secondary, bus->subordinate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 b_res->flags = 0;
822 return 1;
823 }
824 b_res->start = min_align;
Ram Paic8adf9a2011-02-14 17:43:20 -0800825 b_res->end = size0 + min_align - 1;
826 b_res->flags |= IORESOURCE_STARTALIGN | mem64_mask;
Ram Pai9e8bf932011-07-25 13:08:42 -0700827 if (size1 > size0 && realloc_head)
828 add_to_list(realloc_head, bus->self, b_res, size1-size0, min_align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return 1;
830}
831
Ram Pai0a2daa12011-07-25 13:08:41 -0700832unsigned long pci_cardbus_resource_alignment(struct resource *res)
833{
834 if (res->flags & IORESOURCE_IO)
835 return pci_cardbus_io_size;
836 if (res->flags & IORESOURCE_MEM)
837 return pci_cardbus_mem_size;
838 return 0;
839}
840
841static void pci_bus_size_cardbus(struct pci_bus *bus,
Ram Pai9e8bf932011-07-25 13:08:42 -0700842 struct resource_list_x *realloc_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
844 struct pci_dev *bridge = bus->self;
845 struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
846 u16 ctrl;
847
848 /*
849 * Reserve some resources for CardBus. We reserve
850 * a fixed amount of bus space for CardBus bridges.
851 */
Linus Torvalds934b7022008-04-22 18:16:30 -0700852 b_res[0].start = 0;
Linus Torvalds934b7022008-04-22 18:16:30 -0700853 b_res[0].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
Ram Pai9e8bf932011-07-25 13:08:42 -0700854 if (realloc_head)
855 add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size, 0 /* dont care */);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Linus Torvalds934b7022008-04-22 18:16:30 -0700857 b_res[1].start = 0;
Linus Torvalds934b7022008-04-22 18:16:30 -0700858 b_res[1].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
Ram Pai9e8bf932011-07-25 13:08:42 -0700859 if (realloc_head)
860 add_to_list(realloc_head, bridge, b_res+1, pci_cardbus_io_size, 0 /* dont care */);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 /*
863 * Check whether prefetchable memory is supported
864 * by this bridge.
865 */
866 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
867 if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
868 ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
869 pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
870 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
871 }
872
873 /*
874 * If we have prefetchable memory support, allocate
875 * two regions. Otherwise, allocate one region of
876 * twice the size.
877 */
878 if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
Linus Torvalds934b7022008-04-22 18:16:30 -0700879 b_res[2].start = 0;
Linus Torvalds934b7022008-04-22 18:16:30 -0700880 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_SIZEALIGN;
Ram Pai9e8bf932011-07-25 13:08:42 -0700881 if (realloc_head)
882 add_to_list(realloc_head, bridge, b_res+2, pci_cardbus_mem_size, 0 /* dont care */);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Linus Torvalds934b7022008-04-22 18:16:30 -0700884 b_res[3].start = 0;
Linus Torvalds934b7022008-04-22 18:16:30 -0700885 b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
Ram Pai9e8bf932011-07-25 13:08:42 -0700886 if (realloc_head)
887 add_to_list(realloc_head, bridge, b_res+3, pci_cardbus_mem_size, 0 /* dont care */);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 } else {
Linus Torvalds934b7022008-04-22 18:16:30 -0700889 b_res[3].start = 0;
Linus Torvalds934b7022008-04-22 18:16:30 -0700890 b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
Ram Pai9e8bf932011-07-25 13:08:42 -0700891 if (realloc_head)
892 add_to_list(realloc_head, bridge, b_res+3, pci_cardbus_mem_size * 2, 0 /* dont care */);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 }
Ram Pai0a2daa12011-07-25 13:08:41 -0700894
895 /* set the size of the resource to zero, so that the resource does not
896 * get assigned during required-resource allocation cycle but gets assigned
897 * during the optional-resource allocation cycle.
898 */
899 b_res[0].start = b_res[1].start = b_res[2].start = b_res[3].start = 1;
900 b_res[0].end = b_res[1].end = b_res[2].end = b_res[3].end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
902
Ram Paic8adf9a2011-02-14 17:43:20 -0800903void __ref __pci_bus_size_bridges(struct pci_bus *bus,
Ram Pai9e8bf932011-07-25 13:08:42 -0700904 struct resource_list_x *realloc_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
906 struct pci_dev *dev;
907 unsigned long mask, prefmask;
Ram Paic8adf9a2011-02-14 17:43:20 -0800908 resource_size_t additional_mem_size = 0, additional_io_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 list_for_each_entry(dev, &bus->devices, bus_list) {
911 struct pci_bus *b = dev->subordinate;
912 if (!b)
913 continue;
914
915 switch (dev->class >> 8) {
916 case PCI_CLASS_BRIDGE_CARDBUS:
Ram Pai9e8bf932011-07-25 13:08:42 -0700917 pci_bus_size_cardbus(b, realloc_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 break;
919
920 case PCI_CLASS_BRIDGE_PCI:
921 default:
Ram Pai9e8bf932011-07-25 13:08:42 -0700922 __pci_bus_size_bridges(b, realloc_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 break;
924 }
925 }
926
927 /* The root bus? */
928 if (!bus->self)
929 return;
930
931 switch (bus->self->class >> 8) {
932 case PCI_CLASS_BRIDGE_CARDBUS:
933 /* don't size cardbuses yet. */
934 break;
935
936 case PCI_CLASS_BRIDGE_PCI:
937 pci_bridge_check_ranges(bus);
Eric W. Biederman28760482009-09-09 14:09:24 -0700938 if (bus->self->is_hotplug_bridge) {
Ram Paic8adf9a2011-02-14 17:43:20 -0800939 additional_io_size = pci_hotplug_io_size;
940 additional_mem_size = pci_hotplug_mem_size;
Eric W. Biederman28760482009-09-09 14:09:24 -0700941 }
Ram Paic8adf9a2011-02-14 17:43:20 -0800942 /*
943 * Follow thru
944 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 default:
Ram Pai9e8bf932011-07-25 13:08:42 -0700946 pbus_size_io(bus, 0, additional_io_size, realloc_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 /* If the bridge supports prefetchable range, size it
948 separately. If it doesn't, or its prefetchable window
949 has already been allocated by arch code, try
950 non-prefetchable range for both types of PCI memory
951 resources. */
952 mask = IORESOURCE_MEM;
953 prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
Ram Pai9e8bf932011-07-25 13:08:42 -0700954 if (pbus_size_mem(bus, prefmask, prefmask, 0, additional_mem_size, realloc_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 mask = prefmask; /* Success, size non-prefetch only. */
Eric W. Biederman28760482009-09-09 14:09:24 -0700956 else
Ram Paic8adf9a2011-02-14 17:43:20 -0800957 additional_mem_size += additional_mem_size;
Ram Pai9e8bf932011-07-25 13:08:42 -0700958 pbus_size_mem(bus, mask, IORESOURCE_MEM, 0, additional_mem_size, realloc_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 break;
960 }
961}
Ram Paic8adf9a2011-02-14 17:43:20 -0800962
963void __ref pci_bus_size_bridges(struct pci_bus *bus)
964{
965 __pci_bus_size_bridges(bus, NULL);
966}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967EXPORT_SYMBOL(pci_bus_size_bridges);
968
Yinghai Lu568ddef2010-01-22 01:02:21 -0800969static void __ref __pci_bus_assign_resources(const struct pci_bus *bus,
Ram Pai9e8bf932011-07-25 13:08:42 -0700970 struct resource_list_x *realloc_head,
Yinghai Lu568ddef2010-01-22 01:02:21 -0800971 struct resource_list_x *fail_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
973 struct pci_bus *b;
974 struct pci_dev *dev;
975
Ram Pai9e8bf932011-07-25 13:08:42 -0700976 pbus_assign_resources_sorted(bus, realloc_head, fail_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 list_for_each_entry(dev, &bus->devices, bus_list) {
979 b = dev->subordinate;
980 if (!b)
981 continue;
982
Ram Pai9e8bf932011-07-25 13:08:42 -0700983 __pci_bus_assign_resources(b, realloc_head, fail_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985 switch (dev->class >> 8) {
986 case PCI_CLASS_BRIDGE_PCI:
Yinghai Lu6841ec62010-01-22 01:02:25 -0800987 if (!pci_is_enabled(dev))
988 pci_setup_bridge(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 break;
990
991 case PCI_CLASS_BRIDGE_CARDBUS:
992 pci_setup_cardbus(b);
993 break;
994
995 default:
Bjorn Helgaas80ccba12008-06-13 10:52:11 -0600996 dev_info(&dev->dev, "not setting up bridge for bus "
997 "%04x:%02x\n", pci_domain_nr(b), b->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 break;
999 }
1000 }
1001}
Yinghai Lu568ddef2010-01-22 01:02:21 -08001002
1003void __ref pci_bus_assign_resources(const struct pci_bus *bus)
1004{
Ram Paic8adf9a2011-02-14 17:43:20 -08001005 __pci_bus_assign_resources(bus, NULL, NULL);
Yinghai Lu568ddef2010-01-22 01:02:21 -08001006}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007EXPORT_SYMBOL(pci_bus_assign_resources);
1008
Yinghai Lu6841ec62010-01-22 01:02:25 -08001009static void __ref __pci_bridge_assign_resources(const struct pci_dev *bridge,
Yinghai Lu8424d752012-01-21 02:08:21 -08001010 struct resource_list_x *add_head,
Yinghai Lu6841ec62010-01-22 01:02:25 -08001011 struct resource_list_x *fail_head)
1012{
1013 struct pci_bus *b;
1014
Yinghai Lu8424d752012-01-21 02:08:21 -08001015 pdev_assign_resources_sorted((struct pci_dev *)bridge,
1016 add_head, fail_head);
Yinghai Lu6841ec62010-01-22 01:02:25 -08001017
1018 b = bridge->subordinate;
1019 if (!b)
1020 return;
1021
Yinghai Lu8424d752012-01-21 02:08:21 -08001022 __pci_bus_assign_resources(b, add_head, fail_head);
Yinghai Lu6841ec62010-01-22 01:02:25 -08001023
1024 switch (bridge->class >> 8) {
1025 case PCI_CLASS_BRIDGE_PCI:
1026 pci_setup_bridge(b);
1027 break;
1028
1029 case PCI_CLASS_BRIDGE_CARDBUS:
1030 pci_setup_cardbus(b);
1031 break;
1032
1033 default:
1034 dev_info(&bridge->dev, "not setting up bridge for bus "
1035 "%04x:%02x\n", pci_domain_nr(b), b->number);
1036 break;
1037 }
1038}
Yinghai Lu5009b462010-01-22 01:02:20 -08001039static void pci_bridge_release_resources(struct pci_bus *bus,
1040 unsigned long type)
1041{
1042 int idx;
1043 bool changed = false;
1044 struct pci_dev *dev;
1045 struct resource *r;
1046 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1047 IORESOURCE_PREFETCH;
1048
1049 dev = bus->self;
1050 for (idx = PCI_BRIDGE_RESOURCES; idx <= PCI_BRIDGE_RESOURCE_END;
1051 idx++) {
1052 r = &dev->resource[idx];
1053 if ((r->flags & type_mask) != type)
1054 continue;
1055 if (!r->parent)
1056 continue;
1057 /*
1058 * if there are children under that, we should release them
1059 * all
1060 */
1061 release_child_resources(r);
1062 if (!release_resource(r)) {
1063 dev_printk(KERN_DEBUG, &dev->dev,
1064 "resource %d %pR released\n", idx, r);
1065 /* keep the old size */
1066 r->end = resource_size(r) - 1;
1067 r->start = 0;
1068 r->flags = 0;
1069 changed = true;
1070 }
1071 }
1072
1073 if (changed) {
1074 /* avoiding touch the one without PREF */
1075 if (type & IORESOURCE_PREFETCH)
1076 type = IORESOURCE_PREFETCH;
1077 __pci_setup_bridge(bus, type);
1078 }
1079}
1080
1081enum release_type {
1082 leaf_only,
1083 whole_subtree,
1084};
1085/*
1086 * try to release pci bridge resources that is from leaf bridge,
1087 * so we can allocate big new one later
1088 */
1089static void __ref pci_bus_release_bridge_resources(struct pci_bus *bus,
1090 unsigned long type,
1091 enum release_type rel_type)
1092{
1093 struct pci_dev *dev;
1094 bool is_leaf_bridge = true;
1095
1096 list_for_each_entry(dev, &bus->devices, bus_list) {
1097 struct pci_bus *b = dev->subordinate;
1098 if (!b)
1099 continue;
1100
1101 is_leaf_bridge = false;
1102
1103 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
1104 continue;
1105
1106 if (rel_type == whole_subtree)
1107 pci_bus_release_bridge_resources(b, type,
1108 whole_subtree);
1109 }
1110
1111 if (pci_is_root_bus(bus))
1112 return;
1113
1114 if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
1115 return;
1116
1117 if ((rel_type == whole_subtree) || is_leaf_bridge)
1118 pci_bridge_release_resources(bus, type);
1119}
1120
Yinghai Lu76fbc262008-06-23 20:33:06 +02001121static void pci_bus_dump_res(struct pci_bus *bus)
1122{
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -07001123 struct resource *res;
1124 int i;
Yinghai Lu76fbc262008-06-23 20:33:06 +02001125
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -07001126 pci_bus_for_each_resource(bus, res, i) {
Yinghai Lu7c9342b2009-12-22 15:02:24 -08001127 if (!res || !res->end || !res->flags)
Yinghai Lu76fbc262008-06-23 20:33:06 +02001128 continue;
1129
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001130 dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res);
Yinghai Lu76fbc262008-06-23 20:33:06 +02001131 }
1132}
1133
1134static void pci_bus_dump_resources(struct pci_bus *bus)
1135{
1136 struct pci_bus *b;
1137 struct pci_dev *dev;
1138
1139
1140 pci_bus_dump_res(bus);
1141
1142 list_for_each_entry(dev, &bus->devices, bus_list) {
1143 b = dev->subordinate;
1144 if (!b)
1145 continue;
1146
1147 pci_bus_dump_resources(b);
1148 }
1149}
1150
Yinghai Luda7822e2011-05-12 17:11:37 -07001151static int __init pci_bus_get_depth(struct pci_bus *bus)
1152{
1153 int depth = 0;
1154 struct pci_dev *dev;
1155
1156 list_for_each_entry(dev, &bus->devices, bus_list) {
1157 int ret;
1158 struct pci_bus *b = dev->subordinate;
1159 if (!b)
1160 continue;
1161
1162 ret = pci_bus_get_depth(b);
1163 if (ret + 1 > depth)
1164 depth = ret + 1;
1165 }
1166
1167 return depth;
1168}
1169static int __init pci_get_max_depth(void)
1170{
1171 int depth = 0;
1172 struct pci_bus *bus;
1173
1174 list_for_each_entry(bus, &pci_root_buses, node) {
1175 int ret;
1176
1177 ret = pci_bus_get_depth(bus);
1178 if (ret > depth)
1179 depth = ret;
1180 }
1181
1182 return depth;
1183}
1184
Ram Paif483d392011-07-07 11:19:10 -07001185
Yinghai Luda7822e2011-05-12 17:11:37 -07001186/*
1187 * first try will not touch pci bridge res
1188 * second and later try will clear small leaf bridge res
1189 * will stop till to the max deepth if can not find good one
1190 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191void __init
1192pci_assign_unassigned_resources(void)
1193{
1194 struct pci_bus *bus;
Ram Pai9e8bf932011-07-25 13:08:42 -07001195 struct resource_list_x realloc_list; /* list of resources that
Ram Paic8adf9a2011-02-14 17:43:20 -08001196 want additional resources */
Yinghai Luda7822e2011-05-12 17:11:37 -07001197 int tried_times = 0;
1198 enum release_type rel_type = leaf_only;
1199 struct resource_list_x head, *list;
1200 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1201 IORESOURCE_PREFETCH;
1202 unsigned long failed_type;
1203 int max_depth = pci_get_max_depth();
1204 int pci_try_num;
1205
1206
1207 head.next = NULL;
Ram Pai9e8bf932011-07-25 13:08:42 -07001208 realloc_list.next = NULL;
Yinghai Luda7822e2011-05-12 17:11:37 -07001209
1210 pci_try_num = max_depth + 1;
1211 printk(KERN_DEBUG "PCI: max bus depth: %d pci_try_num: %d\n",
1212 max_depth, pci_try_num);
1213
1214again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 /* Depth first, calculate sizes and alignments of all
1216 subordinate buses. */
Yinghai Luda7822e2011-05-12 17:11:37 -07001217 list_for_each_entry(bus, &pci_root_buses, node)
Ram Pai9e8bf932011-07-25 13:08:42 -07001218 __pci_bus_size_bridges(bus, &realloc_list);
Ram Paic8adf9a2011-02-14 17:43:20 -08001219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 /* Depth last, allocate resources and update the hardware. */
Yinghai Luda7822e2011-05-12 17:11:37 -07001221 list_for_each_entry(bus, &pci_root_buses, node)
Ram Pai9e8bf932011-07-25 13:08:42 -07001222 __pci_bus_assign_resources(bus, &realloc_list, &head);
1223 BUG_ON(realloc_list.next);
Yinghai Luda7822e2011-05-12 17:11:37 -07001224 tried_times++;
1225
1226 /* any device complain? */
1227 if (!head.next)
1228 goto enable_and_dump;
Ram Paif483d392011-07-07 11:19:10 -07001229
1230 /* don't realloc if asked to do so */
1231 if (!pci_realloc_enabled()) {
1232 free_list(resource_list_x, &head);
1233 goto enable_and_dump;
1234 }
1235
Yinghai Luda7822e2011-05-12 17:11:37 -07001236 failed_type = 0;
1237 for (list = head.next; list;) {
1238 failed_type |= list->flags;
1239 list = list->next;
1240 }
1241 /*
1242 * io port are tight, don't try extra
1243 * or if reach the limit, don't want to try more
1244 */
1245 failed_type &= type_mask;
1246 if ((failed_type == IORESOURCE_IO) || (tried_times >= pci_try_num)) {
1247 free_list(resource_list_x, &head);
1248 goto enable_and_dump;
1249 }
1250
1251 printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
1252 tried_times + 1);
1253
1254 /* third times and later will not check if it is leaf */
1255 if ((tried_times + 1) > 2)
1256 rel_type = whole_subtree;
1257
1258 /*
1259 * Try to release leaf bridge's resources that doesn't fit resource of
1260 * child device under that bridge
1261 */
1262 for (list = head.next; list;) {
1263 bus = list->dev->bus;
1264 pci_bus_release_bridge_resources(bus, list->flags & type_mask,
1265 rel_type);
1266 list = list->next;
1267 }
1268 /* restore size and flags */
1269 for (list = head.next; list;) {
1270 struct resource *res = list->res;
1271
1272 res->start = list->start;
1273 res->end = list->end;
1274 res->flags = list->flags;
1275 if (list->dev->subordinate)
1276 res->flags = 0;
1277
1278 list = list->next;
1279 }
1280 free_list(resource_list_x, &head);
1281
1282 goto again;
1283
1284enable_and_dump:
1285 /* Depth last, update the hardware. */
1286 list_for_each_entry(bus, &pci_root_buses, node)
1287 pci_enable_bridges(bus);
Yinghai Lu76fbc262008-06-23 20:33:06 +02001288
1289 /* dump the resource on buses */
Yinghai Luda7822e2011-05-12 17:11:37 -07001290 list_for_each_entry(bus, &pci_root_buses, node)
Yinghai Lu76fbc262008-06-23 20:33:06 +02001291 pci_bus_dump_resources(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
Yinghai Lu6841ec62010-01-22 01:02:25 -08001293
1294void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
1295{
1296 struct pci_bus *parent = bridge->subordinate;
Yinghai Lu8424d752012-01-21 02:08:21 -08001297 struct resource_list_x add_list; /* list of resources that
1298 want additional resources */
Yinghai Lu32180e42010-01-22 01:02:27 -08001299 int tried_times = 0;
1300 struct resource_list_x head, *list;
Yinghai Lu6841ec62010-01-22 01:02:25 -08001301 int retval;
Yinghai Lu32180e42010-01-22 01:02:27 -08001302 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1303 IORESOURCE_PREFETCH;
Yinghai Lu6841ec62010-01-22 01:02:25 -08001304
Yinghai Lu32180e42010-01-22 01:02:27 -08001305 head.next = NULL;
Yinghai Lu8424d752012-01-21 02:08:21 -08001306 add_list.next = NULL;
Yinghai Lu32180e42010-01-22 01:02:27 -08001307
1308again:
Yinghai Lu8424d752012-01-21 02:08:21 -08001309 __pci_bus_size_bridges(parent, &add_list);
1310 __pci_bridge_assign_resources(bridge, &add_list, &head);
1311 BUG_ON(add_list.next);
Yinghai Lu32180e42010-01-22 01:02:27 -08001312 tried_times++;
1313
1314 if (!head.next)
Yinghai Lu3f579c32010-05-21 14:35:06 -07001315 goto enable_all;
Yinghai Lu32180e42010-01-22 01:02:27 -08001316
1317 if (tried_times >= 2) {
1318 /* still fail, don't need to try more */
Ram Pai094732a2011-02-14 17:43:18 -08001319 free_list(resource_list_x, &head);
Yinghai Lu3f579c32010-05-21 14:35:06 -07001320 goto enable_all;
Yinghai Lu32180e42010-01-22 01:02:27 -08001321 }
1322
1323 printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
1324 tried_times + 1);
1325
1326 /*
1327 * Try to release leaf bridge's resources that doesn't fit resource of
1328 * child device under that bridge
1329 */
1330 for (list = head.next; list;) {
1331 struct pci_bus *bus = list->dev->bus;
1332 unsigned long flags = list->flags;
1333
1334 pci_bus_release_bridge_resources(bus, flags & type_mask,
1335 whole_subtree);
1336 list = list->next;
1337 }
1338 /* restore size and flags */
1339 for (list = head.next; list;) {
1340 struct resource *res = list->res;
1341
1342 res->start = list->start;
1343 res->end = list->end;
1344 res->flags = list->flags;
1345 if (list->dev->subordinate)
1346 res->flags = 0;
1347
1348 list = list->next;
1349 }
Ram Pai094732a2011-02-14 17:43:18 -08001350 free_list(resource_list_x, &head);
Yinghai Lu32180e42010-01-22 01:02:27 -08001351
1352 goto again;
Yinghai Lu3f579c32010-05-21 14:35:06 -07001353
1354enable_all:
1355 retval = pci_reenable_device(bridge);
1356 pci_set_master(bridge);
1357 pci_enable_bridges(parent);
Yinghai Lu6841ec62010-01-22 01:02:25 -08001358}
1359EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
Yinghai Lu9b030882012-01-21 02:08:23 -08001360
1361#ifdef CONFIG_HOTPLUG
1362/**
1363 * pci_rescan_bus - scan a PCI bus for devices.
1364 * @bus: PCI bus to scan
1365 *
1366 * Scan a PCI bus and child buses for new devices, adds them,
1367 * and enables them.
1368 *
1369 * Returns the max number of subordinate bus discovered.
1370 */
1371unsigned int __ref pci_rescan_bus(struct pci_bus *bus)
1372{
1373 unsigned int max;
1374 struct pci_dev *dev;
1375 struct resource_list_x add_list; /* list of resources that
1376 want additional resources */
1377
1378 max = pci_scan_child_bus(bus);
1379
1380 add_list.next = NULL;
1381 down_read(&pci_bus_sem);
1382 list_for_each_entry(dev, &bus->devices, bus_list)
1383 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
1384 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
1385 if (dev->subordinate)
1386 __pci_bus_size_bridges(dev->subordinate,
1387 &add_list);
1388 up_read(&pci_bus_sem);
1389 __pci_bus_assign_resources(bus, &add_list, NULL);
1390 BUG_ON(add_list.next);
1391
1392 pci_enable_bridges(bus);
1393 pci_bus_add_devices(bus);
1394
1395 return max;
1396}
1397EXPORT_SYMBOL_GPL(pci_rescan_bus);
1398#endif