blob: bcf5752f69073579f5aa7b745ca67e91e73ba06a [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;
36 unsigned long flags;
37};
38
Ram Pai094732a2011-02-14 17:43:18 -080039#define free_list(type, head) do { \
40 struct type *list, *tmp; \
41 for (list = (head)->next; list;) { \
42 tmp = list; \
43 list = list->next; \
44 kfree(tmp); \
45 } \
46 (head)->next = NULL; \
47} while (0)
48
Yinghai Lu568ddef2010-01-22 01:02:21 -080049static void add_to_failed_list(struct resource_list_x *head,
50 struct pci_dev *dev, struct resource *res)
51{
52 struct resource_list_x *list = head;
53 struct resource_list_x *ln = list->next;
54 struct resource_list_x *tmp;
55
56 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
57 if (!tmp) {
58 pr_warning("add_to_failed_list: kmalloc() failed!\n");
59 return;
60 }
61
62 tmp->next = ln;
63 tmp->res = res;
64 tmp->dev = dev;
65 tmp->start = res->start;
66 tmp->end = res->end;
67 tmp->flags = res->flags;
68 list->next = tmp;
69}
70
Yinghai Lu6841ec62010-01-22 01:02:25 -080071static void __dev_sort_resources(struct pci_dev *dev,
72 struct resource_list *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Yinghai Lu6841ec62010-01-22 01:02:25 -080074 u16 class = dev->class >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Yinghai Lu6841ec62010-01-22 01:02:25 -080076 /* Don't touch classless devices or host bridges or ioapics. */
77 if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST)
78 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Yinghai Lu6841ec62010-01-22 01:02:25 -080080 /* Don't touch ioapic devices already enabled by firmware */
81 if (class == PCI_CLASS_SYSTEM_PIC) {
82 u16 command;
83 pci_read_config_word(dev, PCI_COMMAND, &command);
84 if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
85 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 }
87
Yinghai Lu6841ec62010-01-22 01:02:25 -080088 pdev_sort_resources(dev, head);
89}
90
91static void __assign_resources_sorted(struct resource_list *head,
92 struct resource_list_x *fail_head)
93{
94 struct resource *res;
95 struct resource_list *list, *tmp;
96 int idx;
97
98 for (list = head->next; list;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 res = list->res;
100 idx = res - &list->dev->resource[0];
Yinghai Lu9a928662010-02-28 15:49:39 -0800101
Rajesh Shah542df5d2005-04-28 00:25:50 -0700102 if (pci_assign_resource(list->dev, idx)) {
Yinghai Lu9a928662010-02-28 15:49:39 -0800103 if (fail_head && !pci_is_root_bus(list->dev->bus)) {
104 /*
105 * if the failed res is for ROM BAR, and it will
106 * be enabled later, don't add it to the list
107 */
108 if (!((idx == PCI_ROM_RESOURCE) &&
109 (!(res->flags & IORESOURCE_ROM_ENABLE))))
110 add_to_failed_list(fail_head, list->dev, res);
111 }
Rajesh Shah542df5d2005-04-28 00:25:50 -0700112 res->start = 0;
Ivan Kokshaysky960b8462005-07-07 03:07:56 +0400113 res->end = 0;
Rajesh Shah542df5d2005-04-28 00:25:50 -0700114 res->flags = 0;
115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 tmp = list;
117 list = list->next;
118 kfree(tmp);
119 }
120}
121
Yinghai Lu6841ec62010-01-22 01:02:25 -0800122static void pdev_assign_resources_sorted(struct pci_dev *dev,
123 struct resource_list_x *fail_head)
124{
125 struct resource_list head;
126
127 head.next = NULL;
128 __dev_sort_resources(dev, &head);
129 __assign_resources_sorted(&head, fail_head);
130
131}
132
133static void pbus_assign_resources_sorted(const struct pci_bus *bus,
134 struct resource_list_x *fail_head)
135{
136 struct pci_dev *dev;
137 struct resource_list head;
138
139 head.next = NULL;
140 list_for_each_entry(dev, &bus->devices, bus_list)
141 __dev_sort_resources(dev, &head);
142
143 __assign_resources_sorted(&head, fail_head);
144}
145
Dominik Brodowskib3743fa2005-09-09 13:03:23 -0700146void pci_setup_cardbus(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 struct pci_dev *bridge = bus->self;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600149 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 struct pci_bus_region region;
151
Bjorn Helgaas865df572009-11-04 10:32:57 -0700152 dev_info(&bridge->dev, "CardBus bridge to [bus %02x-%02x]\n",
153 bus->secondary, bus->subordinate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600155 res = bus->resource[0];
156 pcibios_resource_to_bus(bridge, &region, res);
157 if (res->flags & IORESOURCE_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 /*
159 * The IO resource is allocated a range twice as large as it
160 * would normally need. This allows us to set both IO regs.
161 */
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600162 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
164 region.start);
165 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
166 region.end);
167 }
168
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600169 res = bus->resource[1];
170 pcibios_resource_to_bus(bridge, &region, res);
171 if (res->flags & IORESOURCE_IO) {
172 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
174 region.start);
175 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
176 region.end);
177 }
178
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600179 res = bus->resource[2];
180 pcibios_resource_to_bus(bridge, &region, res);
181 if (res->flags & IORESOURCE_MEM) {
182 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
184 region.start);
185 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
186 region.end);
187 }
188
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600189 res = bus->resource[3];
190 pcibios_resource_to_bus(bridge, &region, res);
191 if (res->flags & IORESOURCE_MEM) {
192 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
194 region.start);
195 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
196 region.end);
197 }
198}
Dominik Brodowskib3743fa2005-09-09 13:03:23 -0700199EXPORT_SYMBOL(pci_setup_cardbus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201/* Initialize bridges with base/limit values we have collected.
202 PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
203 requires that if there is no I/O ports or memory behind the
204 bridge, corresponding range must be turned off by writing base
205 value greater than limit to the bridge's base/limit registers.
206
207 Note: care must be taken when updating I/O base/limit registers
208 of bridges which support 32-bit I/O. This update requires two
209 config space writes, so it's quite possible that an I/O window of
210 the bridge will have some undesirable address (e.g. 0) after the
211 first write. Ditto 64-bit prefetchable MMIO. */
Yinghai Lu7cc59972009-12-22 15:02:21 -0800212static void pci_setup_bridge_io(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 struct pci_dev *bridge = bus->self;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600215 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 struct pci_bus_region region;
Yinghai Lu7cc59972009-12-22 15:02:21 -0800217 u32 l, io_upper16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 /* Set up the top and bottom of the PCI I/O segment for this bus. */
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600220 res = bus->resource[0];
221 pcibios_resource_to_bus(bridge, &region, res);
222 if (res->flags & IORESOURCE_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 pci_read_config_dword(bridge, PCI_IO_BASE, &l);
224 l &= 0xffff0000;
225 l |= (region.start >> 8) & 0x00f0;
226 l |= region.end & 0xf000;
227 /* Set up upper 16 bits of I/O base/limit. */
228 io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600229 dev_info(&bridge->dev, " bridge window %pR\n", res);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800230 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 /* Clear upper 16 bits of I/O base/limit. */
232 io_upper16 = 0;
233 l = 0x00f0;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600234 dev_info(&bridge->dev, " bridge window [io disabled]\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236 /* Temporarily disable the I/O range before updating PCI_IO_BASE. */
237 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
238 /* Update lower 16 bits of I/O base/limit. */
239 pci_write_config_dword(bridge, PCI_IO_BASE, l);
240 /* Update upper 16 bits of I/O base/limit. */
241 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800242}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Yinghai Lu7cc59972009-12-22 15:02:21 -0800244static void pci_setup_bridge_mmio(struct pci_bus *bus)
245{
246 struct pci_dev *bridge = bus->self;
247 struct resource *res;
248 struct pci_bus_region region;
249 u32 l;
250
251 /* Set up the top and bottom of the PCI Memory segment for this bus. */
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600252 res = bus->resource[1];
253 pcibios_resource_to_bus(bridge, &region, res);
254 if (res->flags & IORESOURCE_MEM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 l = (region.start >> 16) & 0xfff0;
256 l |= region.end & 0xfff00000;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600257 dev_info(&bridge->dev, " bridge window %pR\n", res);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800258 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 l = 0x0000fff0;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600260 dev_info(&bridge->dev, " bridge window [mem disabled]\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
262 pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800263}
264
265static void pci_setup_bridge_mmio_pref(struct pci_bus *bus)
266{
267 struct pci_dev *bridge = bus->self;
268 struct resource *res;
269 struct pci_bus_region region;
270 u32 l, bu, lu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 /* Clear out the upper 32 bits of PREF limit.
273 If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
274 disables PREF range, which is ok. */
275 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
276
277 /* Set up PREF base/limit. */
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100278 bu = lu = 0;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600279 res = bus->resource[2];
280 pcibios_resource_to_bus(bridge, &region, res);
281 if (res->flags & IORESOURCE_PREFETCH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 l = (region.start >> 16) & 0xfff0;
283 l |= region.end & 0xfff00000;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600284 if (res->flags & IORESOURCE_MEM_64) {
Yinghai Lu1f82de12009-04-23 20:48:32 -0700285 bu = upper_32_bits(region.start);
286 lu = upper_32_bits(region.end);
Yinghai Lu1f82de12009-04-23 20:48:32 -0700287 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600288 dev_info(&bridge->dev, " bridge window %pR\n", res);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800289 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 l = 0x0000fff0;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600291 dev_info(&bridge->dev, " bridge window [mem pref disabled]\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
293 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
294
Alex Williamson59353ea2009-11-30 14:51:44 -0700295 /* Set the upper 32 bits of PREF base & limit. */
296 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
297 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800298}
299
300static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type)
301{
302 struct pci_dev *bridge = bus->self;
303
Yinghai Lu7cc59972009-12-22 15:02:21 -0800304 dev_info(&bridge->dev, "PCI bridge to [bus %02x-%02x]\n",
305 bus->secondary, bus->subordinate);
306
307 if (type & IORESOURCE_IO)
308 pci_setup_bridge_io(bus);
309
310 if (type & IORESOURCE_MEM)
311 pci_setup_bridge_mmio(bus);
312
313 if (type & IORESOURCE_PREFETCH)
314 pci_setup_bridge_mmio_pref(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
317}
318
Yinghai Lu7cc59972009-12-22 15:02:21 -0800319static void pci_setup_bridge(struct pci_bus *bus)
320{
321 unsigned long type = IORESOURCE_IO | IORESOURCE_MEM |
322 IORESOURCE_PREFETCH;
323
324 __pci_setup_bridge(bus, type);
325}
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327/* Check whether the bridge supports optional I/O and
328 prefetchable memory ranges. If not, the respective
329 base/limit registers must be read-only and read as 0. */
Sam Ravnborg96bde062007-03-26 21:53:30 -0800330static void pci_bridge_check_ranges(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 u16 io;
333 u32 pmem;
334 struct pci_dev *bridge = bus->self;
335 struct resource *b_res;
336
337 b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
338 b_res[1].flags |= IORESOURCE_MEM;
339
340 pci_read_config_word(bridge, PCI_IO_BASE, &io);
341 if (!io) {
342 pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0);
343 pci_read_config_word(bridge, PCI_IO_BASE, &io);
344 pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
345 }
346 if (io)
347 b_res[0].flags |= IORESOURCE_IO;
348 /* DECchip 21050 pass 2 errata: the bridge may miss an address
349 disconnect boundary by one PCI data phase.
350 Workaround: do not use prefetching on this device. */
351 if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001)
352 return;
353 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
354 if (!pmem) {
355 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE,
356 0xfff0fff0);
357 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
358 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0);
359 }
Yinghai Lu1f82de12009-04-23 20:48:32 -0700360 if (pmem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
Yinghai Lu99586102010-01-22 01:02:28 -0800362 if ((pmem & PCI_PREF_RANGE_TYPE_MASK) ==
363 PCI_PREF_RANGE_TYPE_64) {
Yinghai Lu1f82de12009-04-23 20:48:32 -0700364 b_res[2].flags |= IORESOURCE_MEM_64;
Yinghai Lu99586102010-01-22 01:02:28 -0800365 b_res[2].flags |= PCI_PREF_RANGE_TYPE_64;
366 }
Yinghai Lu1f82de12009-04-23 20:48:32 -0700367 }
368
369 /* double check if bridge does support 64 bit pref */
370 if (b_res[2].flags & IORESOURCE_MEM_64) {
371 u32 mem_base_hi, tmp;
372 pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32,
373 &mem_base_hi);
374 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
375 0xffffffff);
376 pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp);
377 if (!tmp)
378 b_res[2].flags &= ~IORESOURCE_MEM_64;
379 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
380 mem_base_hi);
381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
384/* Helper function for sizing routines: find first available
385 bus resource of a given type. Note: we intentionally skip
386 the bus resources which have already been assigned (that is,
387 have non-NULL parent resource). */
Sam Ravnborg96bde062007-03-26 21:53:30 -0800388static struct resource *find_free_bus_resource(struct pci_bus *bus, unsigned long type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
390 int i;
391 struct resource *r;
392 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
393 IORESOURCE_PREFETCH;
394
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -0700395 pci_bus_for_each_resource(bus, r, i) {
Ivan Kokshaysky299de032005-06-15 18:59:27 +0400396 if (r == &ioport_resource || r == &iomem_resource)
397 continue;
Jesse Barnes55a10982009-10-27 09:39:18 -0700398 if (r && (r->flags & type_mask) == type && !r->parent)
399 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
401 return NULL;
402}
403
Ram Pai13583b12011-02-14 17:43:17 -0800404static resource_size_t calculate_iosize(resource_size_t size,
405 resource_size_t min_size,
406 resource_size_t size1,
407 resource_size_t old_size,
408 resource_size_t align)
409{
410 if (size < min_size)
411 size = min_size;
412 if (old_size == 1 )
413 old_size = 0;
414 /* To be fixed in 2.5: we should have sort of HAVE_ISA
415 flag in the struct pci_bus. */
416#if defined(CONFIG_ISA) || defined(CONFIG_EISA)
417 size = (size & 0xff) + ((size & ~0xffUL) << 2);
418#endif
419 size = ALIGN(size + size1, align);
420 if (size < old_size)
421 size = old_size;
422 return size;
423}
424
425static resource_size_t calculate_memsize(resource_size_t size,
426 resource_size_t min_size,
427 resource_size_t size1,
428 resource_size_t old_size,
429 resource_size_t align)
430{
431 if (size < min_size)
432 size = min_size;
433 if (old_size == 1 )
434 old_size = 0;
435 if (size < old_size)
436 size = old_size;
437 size = ALIGN(size + size1, align);
438 return size;
439}
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441/* Sizing the IO windows of the PCI-PCI bridge is trivial,
442 since these windows have 4K granularity and the IO ranges
443 of non-bridge PCI devices are limited to 256 bytes.
444 We must be careful with the ISA aliasing though. */
Eric W. Biederman28760482009-09-09 14:09:24 -0700445static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 struct pci_dev *dev;
448 struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO);
Ram Pai13583b12011-02-14 17:43:17 -0800449 unsigned long size = 0, size1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 if (!b_res)
452 return;
453
454 list_for_each_entry(dev, &bus->devices, bus_list) {
455 int i;
456
457 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
458 struct resource *r = &dev->resource[i];
459 unsigned long r_size;
460
461 if (r->parent || !(r->flags & IORESOURCE_IO))
462 continue;
Zhao, Yu022edd82008-10-13 19:24:28 +0800463 r_size = resource_size(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 if (r_size < 0x400)
466 /* Might be re-aligned for ISA */
467 size += r_size;
468 else
469 size1 += r_size;
470 }
471 }
Ram Pai13583b12011-02-14 17:43:17 -0800472 size = calculate_iosize(size, min_size, size1,
473 resource_size(b_res), 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (!size) {
Bjorn Helgaas865df572009-11-04 10:32:57 -0700475 if (b_res->start || b_res->end)
476 dev_info(&bus->self->dev, "disabling bridge window "
477 "%pR to [bus %02x-%02x] (unused)\n", b_res,
478 bus->secondary, bus->subordinate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 b_res->flags = 0;
480 return;
481 }
482 /* Alignment of the IO window is always 4K */
483 b_res->start = 4096;
484 b_res->end = b_res->start + size - 1;
Ivan Kokshaysky88452562008-03-30 19:50:14 +0400485 b_res->flags |= IORESOURCE_STARTALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
488/* Calculate the size of the bus and minimal alignment which
489 guarantees that all child resources fit in this size. */
Eric W. Biederman28760482009-09-09 14:09:24 -0700490static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
491 unsigned long type, resource_size_t min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 struct pci_dev *dev;
Ram Pai13583b12011-02-14 17:43:17 -0800494 resource_size_t min_align, align, size;
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100495 resource_size_t aligns[12]; /* Alignments from 1Mb to 2Gb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 int order, max_order;
497 struct resource *b_res = find_free_bus_resource(bus, type);
Yinghai Lu1f82de12009-04-23 20:48:32 -0700498 unsigned int mem64_mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 if (!b_res)
501 return 0;
502
503 memset(aligns, 0, sizeof(aligns));
504 max_order = 0;
505 size = 0;
506
Yinghai Lu1f82de12009-04-23 20:48:32 -0700507 mem64_mask = b_res->flags & IORESOURCE_MEM_64;
508 b_res->flags &= ~IORESOURCE_MEM_64;
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 list_for_each_entry(dev, &bus->devices, bus_list) {
511 int i;
Yinghai Lu1f82de12009-04-23 20:48:32 -0700512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
514 struct resource *r = &dev->resource[i];
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100515 resource_size_t r_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 if (r->parent || (r->flags & mask) != type)
518 continue;
Zhao, Yu022edd82008-10-13 19:24:28 +0800519 r_size = resource_size(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 /* For bridges size != alignment */
Chris Wright6faf17f2009-08-28 13:00:06 -0700521 align = pci_resource_alignment(dev, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 order = __ffs(align) - 20;
523 if (order > 11) {
Bjorn Helgaas865df572009-11-04 10:32:57 -0700524 dev_warn(&dev->dev, "disabling BAR %d: %pR "
525 "(bad alignment %#llx)\n", i, r,
526 (unsigned long long) align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 r->flags = 0;
528 continue;
529 }
530 size += r_size;
531 if (order < 0)
532 order = 0;
533 /* Exclude ranges with size > align from
534 calculation of the alignment. */
535 if (r_size == align)
536 aligns[order] += align;
537 if (order > max_order)
538 max_order = order;
Yinghai Lu1f82de12009-04-23 20:48:32 -0700539 mem64_mask &= r->flags & IORESOURCE_MEM_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 align = 0;
543 min_align = 0;
544 for (order = 0; order <= max_order; order++) {
Jeremy Fitzhardinge8308c542008-09-11 01:31:50 -0700545 resource_size_t align1 = 1;
546
547 align1 <<= (order + 20);
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (!align)
550 min_align = align1;
Milind Arun Choudhary6f6f8c22007-07-09 11:55:51 -0700551 else if (ALIGN(align + min_align, min_align) < align1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 min_align = align1 >> 1;
553 align += aligns[order];
554 }
Ram Pai13583b12011-02-14 17:43:17 -0800555 size = calculate_memsize(size, min_size, 0, resource_size(b_res), align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (!size) {
Bjorn Helgaas865df572009-11-04 10:32:57 -0700557 if (b_res->start || b_res->end)
558 dev_info(&bus->self->dev, "disabling bridge window "
559 "%pR to [bus %02x-%02x] (unused)\n", b_res,
560 bus->secondary, bus->subordinate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 b_res->flags = 0;
562 return 1;
563 }
564 b_res->start = min_align;
565 b_res->end = size + min_align - 1;
Ivan Kokshaysky88452562008-03-30 19:50:14 +0400566 b_res->flags |= IORESOURCE_STARTALIGN;
Yinghai Lu1f82de12009-04-23 20:48:32 -0700567 b_res->flags |= mem64_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return 1;
569}
570
Adrian Bunk5468ae62008-04-18 13:53:56 -0700571static void pci_bus_size_cardbus(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
573 struct pci_dev *bridge = bus->self;
574 struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
575 u16 ctrl;
576
577 /*
578 * Reserve some resources for CardBus. We reserve
579 * a fixed amount of bus space for CardBus bridges.
580 */
Linus Torvalds934b7022008-04-22 18:16:30 -0700581 b_res[0].start = 0;
582 b_res[0].end = pci_cardbus_io_size - 1;
583 b_res[0].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Linus Torvalds934b7022008-04-22 18:16:30 -0700585 b_res[1].start = 0;
586 b_res[1].end = pci_cardbus_io_size - 1;
587 b_res[1].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 /*
590 * Check whether prefetchable memory is supported
591 * by this bridge.
592 */
593 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
594 if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
595 ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
596 pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
597 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
598 }
599
600 /*
601 * If we have prefetchable memory support, allocate
602 * two regions. Otherwise, allocate one region of
603 * twice the size.
604 */
605 if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
Linus Torvalds934b7022008-04-22 18:16:30 -0700606 b_res[2].start = 0;
607 b_res[2].end = pci_cardbus_mem_size - 1;
608 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_SIZEALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Linus Torvalds934b7022008-04-22 18:16:30 -0700610 b_res[3].start = 0;
611 b_res[3].end = pci_cardbus_mem_size - 1;
612 b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 } else {
Linus Torvalds934b7022008-04-22 18:16:30 -0700614 b_res[3].start = 0;
615 b_res[3].end = pci_cardbus_mem_size * 2 - 1;
616 b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
618}
619
Sam Ravnborg451124a2008-02-02 22:33:43 +0100620void __ref pci_bus_size_bridges(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
622 struct pci_dev *dev;
623 unsigned long mask, prefmask;
Eric W. Biederman28760482009-09-09 14:09:24 -0700624 resource_size_t min_mem_size = 0, min_io_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 list_for_each_entry(dev, &bus->devices, bus_list) {
627 struct pci_bus *b = dev->subordinate;
628 if (!b)
629 continue;
630
631 switch (dev->class >> 8) {
632 case PCI_CLASS_BRIDGE_CARDBUS:
633 pci_bus_size_cardbus(b);
634 break;
635
636 case PCI_CLASS_BRIDGE_PCI:
637 default:
638 pci_bus_size_bridges(b);
639 break;
640 }
641 }
642
643 /* The root bus? */
644 if (!bus->self)
645 return;
646
647 switch (bus->self->class >> 8) {
648 case PCI_CLASS_BRIDGE_CARDBUS:
649 /* don't size cardbuses yet. */
650 break;
651
652 case PCI_CLASS_BRIDGE_PCI:
653 pci_bridge_check_ranges(bus);
Eric W. Biederman28760482009-09-09 14:09:24 -0700654 if (bus->self->is_hotplug_bridge) {
655 min_io_size = pci_hotplug_io_size;
656 min_mem_size = pci_hotplug_mem_size;
657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 default:
Eric W. Biederman28760482009-09-09 14:09:24 -0700659 pbus_size_io(bus, min_io_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 /* If the bridge supports prefetchable range, size it
661 separately. If it doesn't, or its prefetchable window
662 has already been allocated by arch code, try
663 non-prefetchable range for both types of PCI memory
664 resources. */
665 mask = IORESOURCE_MEM;
666 prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
Eric W. Biederman28760482009-09-09 14:09:24 -0700667 if (pbus_size_mem(bus, prefmask, prefmask, min_mem_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 mask = prefmask; /* Success, size non-prefetch only. */
Eric W. Biederman28760482009-09-09 14:09:24 -0700669 else
670 min_mem_size += min_mem_size;
671 pbus_size_mem(bus, mask, IORESOURCE_MEM, min_mem_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 break;
673 }
674}
675EXPORT_SYMBOL(pci_bus_size_bridges);
676
Yinghai Lu568ddef2010-01-22 01:02:21 -0800677static void __ref __pci_bus_assign_resources(const struct pci_bus *bus,
678 struct resource_list_x *fail_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
680 struct pci_bus *b;
681 struct pci_dev *dev;
682
Yinghai Lu568ddef2010-01-22 01:02:21 -0800683 pbus_assign_resources_sorted(bus, fail_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 list_for_each_entry(dev, &bus->devices, bus_list) {
686 b = dev->subordinate;
687 if (!b)
688 continue;
689
Yinghai Lu568ddef2010-01-22 01:02:21 -0800690 __pci_bus_assign_resources(b, fail_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 switch (dev->class >> 8) {
693 case PCI_CLASS_BRIDGE_PCI:
Yinghai Lu6841ec62010-01-22 01:02:25 -0800694 if (!pci_is_enabled(dev))
695 pci_setup_bridge(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 break;
697
698 case PCI_CLASS_BRIDGE_CARDBUS:
699 pci_setup_cardbus(b);
700 break;
701
702 default:
Bjorn Helgaas80ccba12008-06-13 10:52:11 -0600703 dev_info(&dev->dev, "not setting up bridge for bus "
704 "%04x:%02x\n", pci_domain_nr(b), b->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 break;
706 }
707 }
708}
Yinghai Lu568ddef2010-01-22 01:02:21 -0800709
710void __ref pci_bus_assign_resources(const struct pci_bus *bus)
711{
712 __pci_bus_assign_resources(bus, NULL);
713}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714EXPORT_SYMBOL(pci_bus_assign_resources);
715
Yinghai Lu6841ec62010-01-22 01:02:25 -0800716static void __ref __pci_bridge_assign_resources(const struct pci_dev *bridge,
717 struct resource_list_x *fail_head)
718{
719 struct pci_bus *b;
720
721 pdev_assign_resources_sorted((struct pci_dev *)bridge, fail_head);
722
723 b = bridge->subordinate;
724 if (!b)
725 return;
726
727 __pci_bus_assign_resources(b, fail_head);
728
729 switch (bridge->class >> 8) {
730 case PCI_CLASS_BRIDGE_PCI:
731 pci_setup_bridge(b);
732 break;
733
734 case PCI_CLASS_BRIDGE_CARDBUS:
735 pci_setup_cardbus(b);
736 break;
737
738 default:
739 dev_info(&bridge->dev, "not setting up bridge for bus "
740 "%04x:%02x\n", pci_domain_nr(b), b->number);
741 break;
742 }
743}
Yinghai Lu5009b462010-01-22 01:02:20 -0800744static void pci_bridge_release_resources(struct pci_bus *bus,
745 unsigned long type)
746{
747 int idx;
748 bool changed = false;
749 struct pci_dev *dev;
750 struct resource *r;
751 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
752 IORESOURCE_PREFETCH;
753
754 dev = bus->self;
755 for (idx = PCI_BRIDGE_RESOURCES; idx <= PCI_BRIDGE_RESOURCE_END;
756 idx++) {
757 r = &dev->resource[idx];
758 if ((r->flags & type_mask) != type)
759 continue;
760 if (!r->parent)
761 continue;
762 /*
763 * if there are children under that, we should release them
764 * all
765 */
766 release_child_resources(r);
767 if (!release_resource(r)) {
768 dev_printk(KERN_DEBUG, &dev->dev,
769 "resource %d %pR released\n", idx, r);
770 /* keep the old size */
771 r->end = resource_size(r) - 1;
772 r->start = 0;
773 r->flags = 0;
774 changed = true;
775 }
776 }
777
778 if (changed) {
779 /* avoiding touch the one without PREF */
780 if (type & IORESOURCE_PREFETCH)
781 type = IORESOURCE_PREFETCH;
782 __pci_setup_bridge(bus, type);
783 }
784}
785
786enum release_type {
787 leaf_only,
788 whole_subtree,
789};
790/*
791 * try to release pci bridge resources that is from leaf bridge,
792 * so we can allocate big new one later
793 */
794static void __ref pci_bus_release_bridge_resources(struct pci_bus *bus,
795 unsigned long type,
796 enum release_type rel_type)
797{
798 struct pci_dev *dev;
799 bool is_leaf_bridge = true;
800
801 list_for_each_entry(dev, &bus->devices, bus_list) {
802 struct pci_bus *b = dev->subordinate;
803 if (!b)
804 continue;
805
806 is_leaf_bridge = false;
807
808 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
809 continue;
810
811 if (rel_type == whole_subtree)
812 pci_bus_release_bridge_resources(b, type,
813 whole_subtree);
814 }
815
816 if (pci_is_root_bus(bus))
817 return;
818
819 if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
820 return;
821
822 if ((rel_type == whole_subtree) || is_leaf_bridge)
823 pci_bridge_release_resources(bus, type);
824}
825
Yinghai Lu76fbc262008-06-23 20:33:06 +0200826static void pci_bus_dump_res(struct pci_bus *bus)
827{
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -0700828 struct resource *res;
829 int i;
Yinghai Lu76fbc262008-06-23 20:33:06 +0200830
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -0700831 pci_bus_for_each_resource(bus, res, i) {
Yinghai Lu7c9342b2009-12-22 15:02:24 -0800832 if (!res || !res->end || !res->flags)
Yinghai Lu76fbc262008-06-23 20:33:06 +0200833 continue;
834
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600835 dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res);
Yinghai Lu76fbc262008-06-23 20:33:06 +0200836 }
837}
838
839static void pci_bus_dump_resources(struct pci_bus *bus)
840{
841 struct pci_bus *b;
842 struct pci_dev *dev;
843
844
845 pci_bus_dump_res(bus);
846
847 list_for_each_entry(dev, &bus->devices, bus_list) {
848 b = dev->subordinate;
849 if (!b)
850 continue;
851
852 pci_bus_dump_resources(b);
853 }
854}
855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856void __init
857pci_assign_unassigned_resources(void)
858{
859 struct pci_bus *bus;
860
861 /* Depth first, calculate sizes and alignments of all
862 subordinate buses. */
863 list_for_each_entry(bus, &pci_root_buses, node) {
864 pci_bus_size_bridges(bus);
865 }
866 /* Depth last, allocate resources and update the hardware. */
867 list_for_each_entry(bus, &pci_root_buses, node) {
Linus Torvalds769d9962010-05-12 18:39:45 -0700868 pci_bus_assign_resources(bus);
Yinghai Lu977d17b2010-01-22 01:02:24 -0800869 pci_enable_bridges(bus);
Linus Torvalds769d9962010-05-12 18:39:45 -0700870 }
Yinghai Lu76fbc262008-06-23 20:33:06 +0200871
872 /* dump the resource on buses */
873 list_for_each_entry(bus, &pci_root_buses, node) {
874 pci_bus_dump_resources(bus);
875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
Yinghai Lu6841ec62010-01-22 01:02:25 -0800877
878void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
879{
880 struct pci_bus *parent = bridge->subordinate;
Yinghai Lu32180e402010-01-22 01:02:27 -0800881 int tried_times = 0;
882 struct resource_list_x head, *list;
Yinghai Lu6841ec62010-01-22 01:02:25 -0800883 int retval;
Yinghai Lu32180e402010-01-22 01:02:27 -0800884 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
885 IORESOURCE_PREFETCH;
Yinghai Lu6841ec62010-01-22 01:02:25 -0800886
Yinghai Lu32180e402010-01-22 01:02:27 -0800887 head.next = NULL;
888
889again:
Yinghai Lu6841ec62010-01-22 01:02:25 -0800890 pci_bus_size_bridges(parent);
Yinghai Lu32180e402010-01-22 01:02:27 -0800891 __pci_bridge_assign_resources(bridge, &head);
Yinghai Lu32180e402010-01-22 01:02:27 -0800892
893 tried_times++;
894
895 if (!head.next)
Yinghai Lu3f579c32010-05-21 14:35:06 -0700896 goto enable_all;
Yinghai Lu32180e402010-01-22 01:02:27 -0800897
898 if (tried_times >= 2) {
899 /* still fail, don't need to try more */
Ram Pai094732a2011-02-14 17:43:18 -0800900 free_list(resource_list_x, &head);
Yinghai Lu3f579c32010-05-21 14:35:06 -0700901 goto enable_all;
Yinghai Lu32180e402010-01-22 01:02:27 -0800902 }
903
904 printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
905 tried_times + 1);
906
907 /*
908 * Try to release leaf bridge's resources that doesn't fit resource of
909 * child device under that bridge
910 */
911 for (list = head.next; list;) {
912 struct pci_bus *bus = list->dev->bus;
913 unsigned long flags = list->flags;
914
915 pci_bus_release_bridge_resources(bus, flags & type_mask,
916 whole_subtree);
917 list = list->next;
918 }
919 /* restore size and flags */
920 for (list = head.next; list;) {
921 struct resource *res = list->res;
922
923 res->start = list->start;
924 res->end = list->end;
925 res->flags = list->flags;
926 if (list->dev->subordinate)
927 res->flags = 0;
928
929 list = list->next;
930 }
Ram Pai094732a2011-02-14 17:43:18 -0800931 free_list(resource_list_x, &head);
Yinghai Lu32180e402010-01-22 01:02:27 -0800932
933 goto again;
Yinghai Lu3f579c32010-05-21 14:35:06 -0700934
935enable_all:
936 retval = pci_reenable_device(bridge);
937 pci_set_master(bridge);
938 pci_enable_bridges(parent);
Yinghai Lu6841ec62010-01-22 01:02:25 -0800939}
940EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);