blob: ed6916bac67567d43d158ad1751355aa1274dbca [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
Andrew Mortonea741552009-02-18 10:44:29 -080030static void pbus_assign_resources_sorted(const struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
32 struct pci_dev *dev;
33 struct resource *res;
34 struct resource_list head, *list, *tmp;
35 int idx;
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 head.next = NULL;
38 list_for_each_entry(dev, &bus->devices, bus_list) {
39 u16 class = dev->class >> 8;
40
Kenji Kaneshige9bded002006-10-04 02:15:34 -070041 /* Don't touch classless devices or host bridges or ioapics. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 if (class == PCI_CLASS_NOT_DEFINED ||
Satoru Takeuchi23186272006-09-12 10:21:44 -070043 class == PCI_CLASS_BRIDGE_HOST)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 continue;
45
Kenji Kaneshige9bded002006-10-04 02:15:34 -070046 /* Don't touch ioapic devices already enabled by firmware */
Satoru Takeuchi23186272006-09-12 10:21:44 -070047 if (class == PCI_CLASS_SYSTEM_PIC) {
Kenji Kaneshige9bded002006-10-04 02:15:34 -070048 u16 command;
49 pci_read_config_word(dev, PCI_COMMAND, &command);
50 if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
Satoru Takeuchi23186272006-09-12 10:21:44 -070051 continue;
52 }
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 pdev_sort_resources(dev, &head);
55 }
56
57 for (list = head.next; list;) {
58 res = list->res;
59 idx = res - &list->dev->resource[0];
Rajesh Shah542df5d2005-04-28 00:25:50 -070060 if (pci_assign_resource(list->dev, idx)) {
61 res->start = 0;
Ivan Kokshaysky960b8462005-07-07 03:07:56 +040062 res->end = 0;
Rajesh Shah542df5d2005-04-28 00:25:50 -070063 res->flags = 0;
64 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 tmp = list;
66 list = list->next;
67 kfree(tmp);
68 }
69}
70
Dominik Brodowskib3743fa2005-09-09 13:03:23 -070071void pci_setup_cardbus(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
73 struct pci_dev *bridge = bus->self;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -060074 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 struct pci_bus_region region;
76
Bjorn Helgaas80ccba12008-06-13 10:52:11 -060077 dev_info(&bridge->dev, "CardBus bridge, secondary bus %04x:%02x\n",
78 pci_domain_nr(bus), bus->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -060080 res = bus->resource[0];
81 pcibios_resource_to_bus(bridge, &region, res);
82 if (res->flags & IORESOURCE_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 /*
84 * The IO resource is allocated a range twice as large as it
85 * would normally need. This allows us to set both IO regs.
86 */
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -060087 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
89 region.start);
90 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
91 region.end);
92 }
93
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -060094 res = bus->resource[1];
95 pcibios_resource_to_bus(bridge, &region, res);
96 if (res->flags & IORESOURCE_IO) {
97 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
99 region.start);
100 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
101 region.end);
102 }
103
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600104 res = bus->resource[2];
105 pcibios_resource_to_bus(bridge, &region, res);
106 if (res->flags & IORESOURCE_MEM) {
107 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
109 region.start);
110 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
111 region.end);
112 }
113
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600114 res = bus->resource[3];
115 pcibios_resource_to_bus(bridge, &region, res);
116 if (res->flags & IORESOURCE_MEM) {
117 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
119 region.start);
120 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
121 region.end);
122 }
123}
Dominik Brodowskib3743fa2005-09-09 13:03:23 -0700124EXPORT_SYMBOL(pci_setup_cardbus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126/* Initialize bridges with base/limit values we have collected.
127 PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
128 requires that if there is no I/O ports or memory behind the
129 bridge, corresponding range must be turned off by writing base
130 value greater than limit to the bridge's base/limit registers.
131
132 Note: care must be taken when updating I/O base/limit registers
133 of bridges which support 32-bit I/O. This update requires two
134 config space writes, so it's quite possible that an I/O window of
135 the bridge will have some undesirable address (e.g. 0) after the
136 first write. Ditto 64-bit prefetchable MMIO. */
Adrian Bunka391f192008-04-18 13:53:57 -0700137static void pci_setup_bridge(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
139 struct pci_dev *bridge = bus->self;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600140 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 struct pci_bus_region region;
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100142 u32 l, bu, lu, io_upper16;
Yinghai Lu1f82de12009-04-23 20:48:32 -0700143 int pref_mem64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Yuji Shimada296ccb02009-04-03 16:41:46 +0900145 if (pci_is_enabled(bridge))
Alex Chiangb73e97d2009-03-20 14:56:15 -0600146 return;
147
Bjorn Helgaas80ccba12008-06-13 10:52:11 -0600148 dev_info(&bridge->dev, "PCI bridge, secondary bus %04x:%02x\n",
149 pci_domain_nr(bus), bus->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 /* Set up the top and bottom of the PCI I/O segment for this bus. */
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600152 res = bus->resource[0];
153 pcibios_resource_to_bus(bridge, &region, res);
154 if (res->flags & IORESOURCE_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 pci_read_config_dword(bridge, PCI_IO_BASE, &l);
156 l &= 0xffff0000;
157 l |= (region.start >> 8) & 0x00f0;
158 l |= region.end & 0xf000;
159 /* Set up upper 16 bits of I/O base/limit. */
160 io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600161 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 }
163 else {
164 /* Clear upper 16 bits of I/O base/limit. */
165 io_upper16 = 0;
166 l = 0x00f0;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600167 dev_info(&bridge->dev, " bridge window [io disabled]\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
169 /* Temporarily disable the I/O range before updating PCI_IO_BASE. */
170 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
171 /* Update lower 16 bits of I/O base/limit. */
172 pci_write_config_dword(bridge, PCI_IO_BASE, l);
173 /* Update upper 16 bits of I/O base/limit. */
174 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
175
176 /* Set up the top and bottom of the PCI Memory segment
177 for this bus. */
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600178 res = bus->resource[1];
179 pcibios_resource_to_bus(bridge, &region, res);
180 if (res->flags & IORESOURCE_MEM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 l = (region.start >> 16) & 0xfff0;
182 l |= region.end & 0xfff00000;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600183 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 }
185 else {
186 l = 0x0000fff0;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600187 dev_info(&bridge->dev, " bridge window [mem disabled]\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
189 pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
190
191 /* Clear out the upper 32 bits of PREF limit.
192 If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
193 disables PREF range, which is ok. */
194 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
195
196 /* Set up PREF base/limit. */
Yinghai Lu1f82de12009-04-23 20:48:32 -0700197 pref_mem64 = 0;
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100198 bu = lu = 0;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600199 res = bus->resource[2];
200 pcibios_resource_to_bus(bridge, &region, res);
201 if (res->flags & IORESOURCE_PREFETCH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 l = (region.start >> 16) & 0xfff0;
203 l |= region.end & 0xfff00000;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600204 if (res->flags & IORESOURCE_MEM_64) {
Yinghai Lu1f82de12009-04-23 20:48:32 -0700205 pref_mem64 = 1;
206 bu = upper_32_bits(region.start);
207 lu = upper_32_bits(region.end);
Yinghai Lu1f82de12009-04-23 20:48:32 -0700208 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600209 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
211 else {
212 l = 0x0000fff0;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600213 dev_info(&bridge->dev, " bridge window [mem pref disabled]\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
216
Yinghai Lu1f82de12009-04-23 20:48:32 -0700217 if (pref_mem64) {
218 /* Set the upper 32 bits of PREF base & limit. */
219 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
220 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
224}
225
226/* Check whether the bridge supports optional I/O and
227 prefetchable memory ranges. If not, the respective
228 base/limit registers must be read-only and read as 0. */
Sam Ravnborg96bde062007-03-26 21:53:30 -0800229static void pci_bridge_check_ranges(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 u16 io;
232 u32 pmem;
233 struct pci_dev *bridge = bus->self;
234 struct resource *b_res;
235
236 b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
237 b_res[1].flags |= IORESOURCE_MEM;
238
239 pci_read_config_word(bridge, PCI_IO_BASE, &io);
240 if (!io) {
241 pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0);
242 pci_read_config_word(bridge, PCI_IO_BASE, &io);
243 pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
244 }
245 if (io)
246 b_res[0].flags |= IORESOURCE_IO;
247 /* DECchip 21050 pass 2 errata: the bridge may miss an address
248 disconnect boundary by one PCI data phase.
249 Workaround: do not use prefetching on this device. */
250 if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001)
251 return;
252 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
253 if (!pmem) {
254 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE,
255 0xfff0fff0);
256 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
257 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0);
258 }
Yinghai Lu1f82de12009-04-23 20:48:32 -0700259 if (pmem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
Yinghai Lu1f82de12009-04-23 20:48:32 -0700261 if ((pmem & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64)
262 b_res[2].flags |= IORESOURCE_MEM_64;
263 }
264
265 /* double check if bridge does support 64 bit pref */
266 if (b_res[2].flags & IORESOURCE_MEM_64) {
267 u32 mem_base_hi, tmp;
268 pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32,
269 &mem_base_hi);
270 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
271 0xffffffff);
272 pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp);
273 if (!tmp)
274 b_res[2].flags &= ~IORESOURCE_MEM_64;
275 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
276 mem_base_hi);
277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
280/* Helper function for sizing routines: find first available
281 bus resource of a given type. Note: we intentionally skip
282 the bus resources which have already been assigned (that is,
283 have non-NULL parent resource). */
Sam Ravnborg96bde062007-03-26 21:53:30 -0800284static struct resource *find_free_bus_resource(struct pci_bus *bus, unsigned long type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 int i;
287 struct resource *r;
288 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
289 IORESOURCE_PREFETCH;
290
291 for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
292 r = bus->resource[i];
Ivan Kokshaysky299de032005-06-15 18:59:27 +0400293 if (r == &ioport_resource || r == &iomem_resource)
294 continue;
Jesse Barnes55a10982009-10-27 09:39:18 -0700295 if (r && (r->flags & type_mask) == type && !r->parent)
296 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
298 return NULL;
299}
300
301/* Sizing the IO windows of the PCI-PCI bridge is trivial,
302 since these windows have 4K granularity and the IO ranges
303 of non-bridge PCI devices are limited to 256 bytes.
304 We must be careful with the ISA aliasing though. */
Eric W. Biederman28760482009-09-09 14:09:24 -0700305static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 struct pci_dev *dev;
308 struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO);
309 unsigned long size = 0, size1 = 0;
310
311 if (!b_res)
312 return;
313
314 list_for_each_entry(dev, &bus->devices, bus_list) {
315 int i;
316
317 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
318 struct resource *r = &dev->resource[i];
319 unsigned long r_size;
320
321 if (r->parent || !(r->flags & IORESOURCE_IO))
322 continue;
Zhao, Yu022edd82008-10-13 19:24:28 +0800323 r_size = resource_size(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 if (r_size < 0x400)
326 /* Might be re-aligned for ISA */
327 size += r_size;
328 else
329 size1 += r_size;
330 }
331 }
Eric W. Biederman28760482009-09-09 14:09:24 -0700332 if (size < min_size)
333 size = min_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334/* To be fixed in 2.5: we should have sort of HAVE_ISA
335 flag in the struct pci_bus. */
336#if defined(CONFIG_ISA) || defined(CONFIG_EISA)
337 size = (size & 0xff) + ((size & ~0xffUL) << 2);
338#endif
Milind Arun Choudhary6f6f8c22007-07-09 11:55:51 -0700339 size = ALIGN(size + size1, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (!size) {
341 b_res->flags = 0;
342 return;
343 }
344 /* Alignment of the IO window is always 4K */
345 b_res->start = 4096;
346 b_res->end = b_res->start + size - 1;
Ivan Kokshaysky88452562008-03-30 19:50:14 +0400347 b_res->flags |= IORESOURCE_STARTALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
350/* Calculate the size of the bus and minimal alignment which
351 guarantees that all child resources fit in this size. */
Eric W. Biederman28760482009-09-09 14:09:24 -0700352static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
353 unsigned long type, resource_size_t min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 struct pci_dev *dev;
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100356 resource_size_t min_align, align, size;
357 resource_size_t aligns[12]; /* Alignments from 1Mb to 2Gb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 int order, max_order;
359 struct resource *b_res = find_free_bus_resource(bus, type);
Yinghai Lu1f82de12009-04-23 20:48:32 -0700360 unsigned int mem64_mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 if (!b_res)
363 return 0;
364
365 memset(aligns, 0, sizeof(aligns));
366 max_order = 0;
367 size = 0;
368
Yinghai Lu1f82de12009-04-23 20:48:32 -0700369 mem64_mask = b_res->flags & IORESOURCE_MEM_64;
370 b_res->flags &= ~IORESOURCE_MEM_64;
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 list_for_each_entry(dev, &bus->devices, bus_list) {
373 int i;
Yinghai Lu1f82de12009-04-23 20:48:32 -0700374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
376 struct resource *r = &dev->resource[i];
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100377 resource_size_t r_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 if (r->parent || (r->flags & mask) != type)
380 continue;
Zhao, Yu022edd82008-10-13 19:24:28 +0800381 r_size = resource_size(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 /* For bridges size != alignment */
Chris Wright6faf17f2009-08-28 13:00:06 -0700383 align = pci_resource_alignment(dev, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 order = __ffs(align) - 20;
385 if (order > 11) {
Bjorn Helgaasa369c792009-10-06 15:33:44 -0600386 dev_warn(&dev->dev, "BAR %d: bad alignment %llx: "
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600387 "%pR\n", i, (unsigned long long)align, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 r->flags = 0;
389 continue;
390 }
391 size += r_size;
392 if (order < 0)
393 order = 0;
394 /* Exclude ranges with size > align from
395 calculation of the alignment. */
396 if (r_size == align)
397 aligns[order] += align;
398 if (order > max_order)
399 max_order = order;
Yinghai Lu1f82de12009-04-23 20:48:32 -0700400 mem64_mask &= r->flags & IORESOURCE_MEM_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402 }
Eric W. Biederman28760482009-09-09 14:09:24 -0700403 if (size < min_size)
404 size = min_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 align = 0;
407 min_align = 0;
408 for (order = 0; order <= max_order; order++) {
Jeremy Fitzhardinge8308c542008-09-11 01:31:50 -0700409 resource_size_t align1 = 1;
410
411 align1 <<= (order + 20);
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (!align)
414 min_align = align1;
Milind Arun Choudhary6f6f8c22007-07-09 11:55:51 -0700415 else if (ALIGN(align + min_align, min_align) < align1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 min_align = align1 >> 1;
417 align += aligns[order];
418 }
Milind Arun Choudhary6f6f8c22007-07-09 11:55:51 -0700419 size = ALIGN(size, min_align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if (!size) {
421 b_res->flags = 0;
422 return 1;
423 }
424 b_res->start = min_align;
425 b_res->end = size + min_align - 1;
Ivan Kokshaysky88452562008-03-30 19:50:14 +0400426 b_res->flags |= IORESOURCE_STARTALIGN;
Yinghai Lu1f82de12009-04-23 20:48:32 -0700427 b_res->flags |= mem64_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 return 1;
429}
430
Adrian Bunk5468ae62008-04-18 13:53:56 -0700431static void pci_bus_size_cardbus(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
433 struct pci_dev *bridge = bus->self;
434 struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
435 u16 ctrl;
436
437 /*
438 * Reserve some resources for CardBus. We reserve
439 * a fixed amount of bus space for CardBus bridges.
440 */
Linus Torvalds934b7022008-04-22 18:16:30 -0700441 b_res[0].start = 0;
442 b_res[0].end = pci_cardbus_io_size - 1;
443 b_res[0].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Linus Torvalds934b7022008-04-22 18:16:30 -0700445 b_res[1].start = 0;
446 b_res[1].end = pci_cardbus_io_size - 1;
447 b_res[1].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 /*
450 * Check whether prefetchable memory is supported
451 * by this bridge.
452 */
453 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
454 if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
455 ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
456 pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
457 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
458 }
459
460 /*
461 * If we have prefetchable memory support, allocate
462 * two regions. Otherwise, allocate one region of
463 * twice the size.
464 */
465 if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
Linus Torvalds934b7022008-04-22 18:16:30 -0700466 b_res[2].start = 0;
467 b_res[2].end = pci_cardbus_mem_size - 1;
468 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_SIZEALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Linus Torvalds934b7022008-04-22 18:16:30 -0700470 b_res[3].start = 0;
471 b_res[3].end = pci_cardbus_mem_size - 1;
472 b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 } else {
Linus Torvalds934b7022008-04-22 18:16:30 -0700474 b_res[3].start = 0;
475 b_res[3].end = pci_cardbus_mem_size * 2 - 1;
476 b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
478}
479
Sam Ravnborg451124a2008-02-02 22:33:43 +0100480void __ref pci_bus_size_bridges(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
482 struct pci_dev *dev;
483 unsigned long mask, prefmask;
Eric W. Biederman28760482009-09-09 14:09:24 -0700484 resource_size_t min_mem_size = 0, min_io_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 list_for_each_entry(dev, &bus->devices, bus_list) {
487 struct pci_bus *b = dev->subordinate;
488 if (!b)
489 continue;
490
491 switch (dev->class >> 8) {
492 case PCI_CLASS_BRIDGE_CARDBUS:
493 pci_bus_size_cardbus(b);
494 break;
495
496 case PCI_CLASS_BRIDGE_PCI:
497 default:
498 pci_bus_size_bridges(b);
499 break;
500 }
501 }
502
503 /* The root bus? */
504 if (!bus->self)
505 return;
506
507 switch (bus->self->class >> 8) {
508 case PCI_CLASS_BRIDGE_CARDBUS:
509 /* don't size cardbuses yet. */
510 break;
511
512 case PCI_CLASS_BRIDGE_PCI:
513 pci_bridge_check_ranges(bus);
Eric W. Biederman28760482009-09-09 14:09:24 -0700514 if (bus->self->is_hotplug_bridge) {
515 min_io_size = pci_hotplug_io_size;
516 min_mem_size = pci_hotplug_mem_size;
517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 default:
Eric W. Biederman28760482009-09-09 14:09:24 -0700519 pbus_size_io(bus, min_io_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 /* If the bridge supports prefetchable range, size it
521 separately. If it doesn't, or its prefetchable window
522 has already been allocated by arch code, try
523 non-prefetchable range for both types of PCI memory
524 resources. */
525 mask = IORESOURCE_MEM;
526 prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
Eric W. Biederman28760482009-09-09 14:09:24 -0700527 if (pbus_size_mem(bus, prefmask, prefmask, min_mem_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 mask = prefmask; /* Success, size non-prefetch only. */
Eric W. Biederman28760482009-09-09 14:09:24 -0700529 else
530 min_mem_size += min_mem_size;
531 pbus_size_mem(bus, mask, IORESOURCE_MEM, min_mem_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 break;
533 }
534}
535EXPORT_SYMBOL(pci_bus_size_bridges);
536
Andrew Mortonea741552009-02-18 10:44:29 -0800537void __ref pci_bus_assign_resources(const struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 struct pci_bus *b;
540 struct pci_dev *dev;
541
542 pbus_assign_resources_sorted(bus);
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 list_for_each_entry(dev, &bus->devices, bus_list) {
545 b = dev->subordinate;
546 if (!b)
547 continue;
548
549 pci_bus_assign_resources(b);
550
551 switch (dev->class >> 8) {
552 case PCI_CLASS_BRIDGE_PCI:
553 pci_setup_bridge(b);
554 break;
555
556 case PCI_CLASS_BRIDGE_CARDBUS:
557 pci_setup_cardbus(b);
558 break;
559
560 default:
Bjorn Helgaas80ccba12008-06-13 10:52:11 -0600561 dev_info(&dev->dev, "not setting up bridge for bus "
562 "%04x:%02x\n", pci_domain_nr(b), b->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 break;
564 }
565 }
566}
567EXPORT_SYMBOL(pci_bus_assign_resources);
568
Yinghai Lu76fbc262008-06-23 20:33:06 +0200569static void pci_bus_dump_res(struct pci_bus *bus)
570{
571 int i;
572
573 for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
574 struct resource *res = bus->resource[i];
Yinghai Lu681bf592009-04-13 18:28:54 -0700575 if (!res || !res->end)
Yinghai Lu76fbc262008-06-23 20:33:06 +0200576 continue;
577
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600578 dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res);
Yinghai Lu76fbc262008-06-23 20:33:06 +0200579 }
580}
581
582static void pci_bus_dump_resources(struct pci_bus *bus)
583{
584 struct pci_bus *b;
585 struct pci_dev *dev;
586
587
588 pci_bus_dump_res(bus);
589
590 list_for_each_entry(dev, &bus->devices, bus_list) {
591 b = dev->subordinate;
592 if (!b)
593 continue;
594
595 pci_bus_dump_resources(b);
596 }
597}
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599void __init
600pci_assign_unassigned_resources(void)
601{
602 struct pci_bus *bus;
603
604 /* Depth first, calculate sizes and alignments of all
605 subordinate buses. */
606 list_for_each_entry(bus, &pci_root_buses, node) {
607 pci_bus_size_bridges(bus);
608 }
609 /* Depth last, allocate resources and update the hardware. */
610 list_for_each_entry(bus, &pci_root_buses, node) {
611 pci_bus_assign_resources(bus);
612 pci_enable_bridges(bus);
613 }
Yinghai Lu76fbc262008-06-23 20:33:06 +0200614
615 /* dump the resource on buses */
616 list_for_each_entry(bus, &pci_root_buses, node) {
617 pci_bus_dump_resources(bus);
618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}