blob: c79ce4ee634b72b56881d98e2a7a68a9b4382570 [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:
Yinghai Lu19aa7ee2012-01-21 02:08:24 -0800946 pbus_size_io(bus, realloc_head ? 0 : additional_io_size,
947 additional_io_size, realloc_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 /* If the bridge supports prefetchable range, size it
949 separately. If it doesn't, or its prefetchable window
950 has already been allocated by arch code, try
951 non-prefetchable range for both types of PCI memory
952 resources. */
953 mask = IORESOURCE_MEM;
954 prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
Yinghai Lu19aa7ee2012-01-21 02:08:24 -0800955 if (pbus_size_mem(bus, prefmask, prefmask,
956 realloc_head ? 0 : additional_mem_size,
957 additional_mem_size, realloc_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 mask = prefmask; /* Success, size non-prefetch only. */
Eric W. Biederman28760482009-09-09 14:09:24 -0700959 else
Ram Paic8adf9a2011-02-14 17:43:20 -0800960 additional_mem_size += additional_mem_size;
Yinghai Lu19aa7ee2012-01-21 02:08:24 -0800961 pbus_size_mem(bus, mask, IORESOURCE_MEM,
962 realloc_head ? 0 : additional_mem_size,
963 additional_mem_size, realloc_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 break;
965 }
966}
Ram Paic8adf9a2011-02-14 17:43:20 -0800967
968void __ref pci_bus_size_bridges(struct pci_bus *bus)
969{
970 __pci_bus_size_bridges(bus, NULL);
971}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972EXPORT_SYMBOL(pci_bus_size_bridges);
973
Yinghai Lu568ddef2010-01-22 01:02:21 -0800974static void __ref __pci_bus_assign_resources(const struct pci_bus *bus,
Ram Pai9e8bf932011-07-25 13:08:42 -0700975 struct resource_list_x *realloc_head,
Yinghai Lu568ddef2010-01-22 01:02:21 -0800976 struct resource_list_x *fail_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
978 struct pci_bus *b;
979 struct pci_dev *dev;
980
Ram Pai9e8bf932011-07-25 13:08:42 -0700981 pbus_assign_resources_sorted(bus, realloc_head, fail_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 list_for_each_entry(dev, &bus->devices, bus_list) {
984 b = dev->subordinate;
985 if (!b)
986 continue;
987
Ram Pai9e8bf932011-07-25 13:08:42 -0700988 __pci_bus_assign_resources(b, realloc_head, fail_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 switch (dev->class >> 8) {
991 case PCI_CLASS_BRIDGE_PCI:
Yinghai Lu6841ec62010-01-22 01:02:25 -0800992 if (!pci_is_enabled(dev))
993 pci_setup_bridge(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 break;
995
996 case PCI_CLASS_BRIDGE_CARDBUS:
997 pci_setup_cardbus(b);
998 break;
999
1000 default:
Bjorn Helgaas80ccba12008-06-13 10:52:11 -06001001 dev_info(&dev->dev, "not setting up bridge for bus "
1002 "%04x:%02x\n", pci_domain_nr(b), b->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 break;
1004 }
1005 }
1006}
Yinghai Lu568ddef2010-01-22 01:02:21 -08001007
1008void __ref pci_bus_assign_resources(const struct pci_bus *bus)
1009{
Ram Paic8adf9a2011-02-14 17:43:20 -08001010 __pci_bus_assign_resources(bus, NULL, NULL);
Yinghai Lu568ddef2010-01-22 01:02:21 -08001011}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012EXPORT_SYMBOL(pci_bus_assign_resources);
1013
Yinghai Lu6841ec62010-01-22 01:02:25 -08001014static void __ref __pci_bridge_assign_resources(const struct pci_dev *bridge,
Yinghai Lu8424d752012-01-21 02:08:21 -08001015 struct resource_list_x *add_head,
Yinghai Lu6841ec62010-01-22 01:02:25 -08001016 struct resource_list_x *fail_head)
1017{
1018 struct pci_bus *b;
1019
Yinghai Lu8424d752012-01-21 02:08:21 -08001020 pdev_assign_resources_sorted((struct pci_dev *)bridge,
1021 add_head, fail_head);
Yinghai Lu6841ec62010-01-22 01:02:25 -08001022
1023 b = bridge->subordinate;
1024 if (!b)
1025 return;
1026
Yinghai Lu8424d752012-01-21 02:08:21 -08001027 __pci_bus_assign_resources(b, add_head, fail_head);
Yinghai Lu6841ec62010-01-22 01:02:25 -08001028
1029 switch (bridge->class >> 8) {
1030 case PCI_CLASS_BRIDGE_PCI:
1031 pci_setup_bridge(b);
1032 break;
1033
1034 case PCI_CLASS_BRIDGE_CARDBUS:
1035 pci_setup_cardbus(b);
1036 break;
1037
1038 default:
1039 dev_info(&bridge->dev, "not setting up bridge for bus "
1040 "%04x:%02x\n", pci_domain_nr(b), b->number);
1041 break;
1042 }
1043}
Yinghai Lu5009b462010-01-22 01:02:20 -08001044static void pci_bridge_release_resources(struct pci_bus *bus,
1045 unsigned long type)
1046{
1047 int idx;
1048 bool changed = false;
1049 struct pci_dev *dev;
1050 struct resource *r;
1051 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1052 IORESOURCE_PREFETCH;
1053
1054 dev = bus->self;
1055 for (idx = PCI_BRIDGE_RESOURCES; idx <= PCI_BRIDGE_RESOURCE_END;
1056 idx++) {
1057 r = &dev->resource[idx];
1058 if ((r->flags & type_mask) != type)
1059 continue;
1060 if (!r->parent)
1061 continue;
1062 /*
1063 * if there are children under that, we should release them
1064 * all
1065 */
1066 release_child_resources(r);
1067 if (!release_resource(r)) {
1068 dev_printk(KERN_DEBUG, &dev->dev,
1069 "resource %d %pR released\n", idx, r);
1070 /* keep the old size */
1071 r->end = resource_size(r) - 1;
1072 r->start = 0;
1073 r->flags = 0;
1074 changed = true;
1075 }
1076 }
1077
1078 if (changed) {
1079 /* avoiding touch the one without PREF */
1080 if (type & IORESOURCE_PREFETCH)
1081 type = IORESOURCE_PREFETCH;
1082 __pci_setup_bridge(bus, type);
1083 }
1084}
1085
1086enum release_type {
1087 leaf_only,
1088 whole_subtree,
1089};
1090/*
1091 * try to release pci bridge resources that is from leaf bridge,
1092 * so we can allocate big new one later
1093 */
1094static void __ref pci_bus_release_bridge_resources(struct pci_bus *bus,
1095 unsigned long type,
1096 enum release_type rel_type)
1097{
1098 struct pci_dev *dev;
1099 bool is_leaf_bridge = true;
1100
1101 list_for_each_entry(dev, &bus->devices, bus_list) {
1102 struct pci_bus *b = dev->subordinate;
1103 if (!b)
1104 continue;
1105
1106 is_leaf_bridge = false;
1107
1108 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
1109 continue;
1110
1111 if (rel_type == whole_subtree)
1112 pci_bus_release_bridge_resources(b, type,
1113 whole_subtree);
1114 }
1115
1116 if (pci_is_root_bus(bus))
1117 return;
1118
1119 if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
1120 return;
1121
1122 if ((rel_type == whole_subtree) || is_leaf_bridge)
1123 pci_bridge_release_resources(bus, type);
1124}
1125
Yinghai Lu76fbc262008-06-23 20:33:06 +02001126static void pci_bus_dump_res(struct pci_bus *bus)
1127{
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -07001128 struct resource *res;
1129 int i;
Yinghai Lu76fbc262008-06-23 20:33:06 +02001130
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -07001131 pci_bus_for_each_resource(bus, res, i) {
Yinghai Lu7c9342b2009-12-22 15:02:24 -08001132 if (!res || !res->end || !res->flags)
Yinghai Lu76fbc262008-06-23 20:33:06 +02001133 continue;
1134
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001135 dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res);
Yinghai Lu76fbc262008-06-23 20:33:06 +02001136 }
1137}
1138
1139static void pci_bus_dump_resources(struct pci_bus *bus)
1140{
1141 struct pci_bus *b;
1142 struct pci_dev *dev;
1143
1144
1145 pci_bus_dump_res(bus);
1146
1147 list_for_each_entry(dev, &bus->devices, bus_list) {
1148 b = dev->subordinate;
1149 if (!b)
1150 continue;
1151
1152 pci_bus_dump_resources(b);
1153 }
1154}
1155
Yinghai Luda7822e2011-05-12 17:11:37 -07001156static int __init pci_bus_get_depth(struct pci_bus *bus)
1157{
1158 int depth = 0;
1159 struct pci_dev *dev;
1160
1161 list_for_each_entry(dev, &bus->devices, bus_list) {
1162 int ret;
1163 struct pci_bus *b = dev->subordinate;
1164 if (!b)
1165 continue;
1166
1167 ret = pci_bus_get_depth(b);
1168 if (ret + 1 > depth)
1169 depth = ret + 1;
1170 }
1171
1172 return depth;
1173}
1174static int __init pci_get_max_depth(void)
1175{
1176 int depth = 0;
1177 struct pci_bus *bus;
1178
1179 list_for_each_entry(bus, &pci_root_buses, node) {
1180 int ret;
1181
1182 ret = pci_bus_get_depth(bus);
1183 if (ret > depth)
1184 depth = ret;
1185 }
1186
1187 return depth;
1188}
1189
Ram Paif483d392011-07-07 11:19:10 -07001190
Yinghai Luda7822e2011-05-12 17:11:37 -07001191/*
1192 * first try will not touch pci bridge res
1193 * second and later try will clear small leaf bridge res
1194 * will stop till to the max deepth if can not find good one
1195 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196void __init
1197pci_assign_unassigned_resources(void)
1198{
1199 struct pci_bus *bus;
Ram Pai9e8bf932011-07-25 13:08:42 -07001200 struct resource_list_x realloc_list; /* list of resources that
Ram Paic8adf9a2011-02-14 17:43:20 -08001201 want additional resources */
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001202 struct resource_list_x *add_list = NULL;
Yinghai Luda7822e2011-05-12 17:11:37 -07001203 int tried_times = 0;
1204 enum release_type rel_type = leaf_only;
1205 struct resource_list_x head, *list;
1206 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1207 IORESOURCE_PREFETCH;
1208 unsigned long failed_type;
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001209 int pci_try_num = 1;
Yinghai Luda7822e2011-05-12 17:11:37 -07001210
1211 head.next = NULL;
Ram Pai9e8bf932011-07-25 13:08:42 -07001212 realloc_list.next = NULL;
Yinghai Luda7822e2011-05-12 17:11:37 -07001213
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001214 /* don't realloc if asked to do so */
1215 if (pci_realloc_enabled()) {
1216 int max_depth = pci_get_max_depth();
1217
1218 pci_try_num = max_depth + 1;
1219 printk(KERN_DEBUG "PCI: max bus depth: %d pci_try_num: %d\n",
1220 max_depth, pci_try_num);
1221 }
Yinghai Luda7822e2011-05-12 17:11:37 -07001222
1223again:
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001224 /*
1225 * last try will use add_list, otherwise will try good to have as
1226 * must have, so can realloc parent bridge resource
1227 */
1228 if (tried_times + 1 == pci_try_num)
1229 add_list = &realloc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 /* Depth first, calculate sizes and alignments of all
1231 subordinate buses. */
Yinghai Luda7822e2011-05-12 17:11:37 -07001232 list_for_each_entry(bus, &pci_root_buses, node)
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001233 __pci_bus_size_bridges(bus, add_list);
Ram Paic8adf9a2011-02-14 17:43:20 -08001234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 /* Depth last, allocate resources and update the hardware. */
Yinghai Luda7822e2011-05-12 17:11:37 -07001236 list_for_each_entry(bus, &pci_root_buses, node)
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001237 __pci_bus_assign_resources(bus, add_list, &head);
1238 if (add_list)
1239 BUG_ON(add_list->next);
Yinghai Luda7822e2011-05-12 17:11:37 -07001240 tried_times++;
1241
1242 /* any device complain? */
1243 if (!head.next)
1244 goto enable_and_dump;
Ram Paif483d392011-07-07 11:19:10 -07001245
Yinghai Luda7822e2011-05-12 17:11:37 -07001246 failed_type = 0;
1247 for (list = head.next; list;) {
1248 failed_type |= list->flags;
1249 list = list->next;
1250 }
1251 /*
1252 * io port are tight, don't try extra
1253 * or if reach the limit, don't want to try more
1254 */
1255 failed_type &= type_mask;
1256 if ((failed_type == IORESOURCE_IO) || (tried_times >= pci_try_num)) {
1257 free_list(resource_list_x, &head);
1258 goto enable_and_dump;
1259 }
1260
1261 printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
1262 tried_times + 1);
1263
1264 /* third times and later will not check if it is leaf */
1265 if ((tried_times + 1) > 2)
1266 rel_type = whole_subtree;
1267
1268 /*
1269 * Try to release leaf bridge's resources that doesn't fit resource of
1270 * child device under that bridge
1271 */
1272 for (list = head.next; list;) {
1273 bus = list->dev->bus;
1274 pci_bus_release_bridge_resources(bus, list->flags & type_mask,
1275 rel_type);
1276 list = list->next;
1277 }
1278 /* restore size and flags */
1279 for (list = head.next; list;) {
1280 struct resource *res = list->res;
1281
1282 res->start = list->start;
1283 res->end = list->end;
1284 res->flags = list->flags;
1285 if (list->dev->subordinate)
1286 res->flags = 0;
1287
1288 list = list->next;
1289 }
1290 free_list(resource_list_x, &head);
1291
1292 goto again;
1293
1294enable_and_dump:
1295 /* Depth last, update the hardware. */
1296 list_for_each_entry(bus, &pci_root_buses, node)
1297 pci_enable_bridges(bus);
Yinghai Lu76fbc262008-06-23 20:33:06 +02001298
1299 /* dump the resource on buses */
Yinghai Luda7822e2011-05-12 17:11:37 -07001300 list_for_each_entry(bus, &pci_root_buses, node)
Yinghai Lu76fbc262008-06-23 20:33:06 +02001301 pci_bus_dump_resources(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302}
Yinghai Lu6841ec62010-01-22 01:02:25 -08001303
1304void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
1305{
1306 struct pci_bus *parent = bridge->subordinate;
Yinghai Lu8424d752012-01-21 02:08:21 -08001307 struct resource_list_x add_list; /* list of resources that
1308 want additional resources */
Yinghai Lu32180e402010-01-22 01:02:27 -08001309 int tried_times = 0;
1310 struct resource_list_x head, *list;
Yinghai Lu6841ec62010-01-22 01:02:25 -08001311 int retval;
Yinghai Lu32180e402010-01-22 01:02:27 -08001312 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1313 IORESOURCE_PREFETCH;
Yinghai Lu6841ec62010-01-22 01:02:25 -08001314
Yinghai Lu32180e402010-01-22 01:02:27 -08001315 head.next = NULL;
Yinghai Lu8424d752012-01-21 02:08:21 -08001316 add_list.next = NULL;
Yinghai Lu32180e402010-01-22 01:02:27 -08001317
1318again:
Yinghai Lu8424d752012-01-21 02:08:21 -08001319 __pci_bus_size_bridges(parent, &add_list);
1320 __pci_bridge_assign_resources(bridge, &add_list, &head);
1321 BUG_ON(add_list.next);
Yinghai Lu32180e402010-01-22 01:02:27 -08001322 tried_times++;
1323
1324 if (!head.next)
Yinghai Lu3f579c32010-05-21 14:35:06 -07001325 goto enable_all;
Yinghai Lu32180e402010-01-22 01:02:27 -08001326
1327 if (tried_times >= 2) {
1328 /* still fail, don't need to try more */
Ram Pai094732a2011-02-14 17:43:18 -08001329 free_list(resource_list_x, &head);
Yinghai Lu3f579c32010-05-21 14:35:06 -07001330 goto enable_all;
Yinghai Lu32180e402010-01-22 01:02:27 -08001331 }
1332
1333 printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
1334 tried_times + 1);
1335
1336 /*
1337 * Try to release leaf bridge's resources that doesn't fit resource of
1338 * child device under that bridge
1339 */
1340 for (list = head.next; list;) {
1341 struct pci_bus *bus = list->dev->bus;
1342 unsigned long flags = list->flags;
1343
1344 pci_bus_release_bridge_resources(bus, flags & type_mask,
1345 whole_subtree);
1346 list = list->next;
1347 }
1348 /* restore size and flags */
1349 for (list = head.next; list;) {
1350 struct resource *res = list->res;
1351
1352 res->start = list->start;
1353 res->end = list->end;
1354 res->flags = list->flags;
1355 if (list->dev->subordinate)
1356 res->flags = 0;
1357
1358 list = list->next;
1359 }
Ram Pai094732a2011-02-14 17:43:18 -08001360 free_list(resource_list_x, &head);
Yinghai Lu32180e402010-01-22 01:02:27 -08001361
1362 goto again;
Yinghai Lu3f579c32010-05-21 14:35:06 -07001363
1364enable_all:
1365 retval = pci_reenable_device(bridge);
1366 pci_set_master(bridge);
1367 pci_enable_bridges(parent);
Yinghai Lu6841ec62010-01-22 01:02:25 -08001368}
1369EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
Yinghai Lu9b030882012-01-21 02:08:23 -08001370
1371#ifdef CONFIG_HOTPLUG
1372/**
1373 * pci_rescan_bus - scan a PCI bus for devices.
1374 * @bus: PCI bus to scan
1375 *
1376 * Scan a PCI bus and child buses for new devices, adds them,
1377 * and enables them.
1378 *
1379 * Returns the max number of subordinate bus discovered.
1380 */
1381unsigned int __ref pci_rescan_bus(struct pci_bus *bus)
1382{
1383 unsigned int max;
1384 struct pci_dev *dev;
1385 struct resource_list_x add_list; /* list of resources that
1386 want additional resources */
1387
1388 max = pci_scan_child_bus(bus);
1389
1390 add_list.next = NULL;
1391 down_read(&pci_bus_sem);
1392 list_for_each_entry(dev, &bus->devices, bus_list)
1393 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
1394 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
1395 if (dev->subordinate)
1396 __pci_bus_size_bridges(dev->subordinate,
1397 &add_list);
1398 up_read(&pci_bus_sem);
1399 __pci_bus_assign_resources(bus, &add_list, NULL);
1400 BUG_ON(add_list.next);
1401
1402 pci_enable_bridges(bus);
1403 pci_bus_add_devices(bus);
1404
1405 return max;
1406}
1407EXPORT_SYMBOL_GPL(pci_rescan_bus);
1408#endif