blob: 7e87ea8f320006cd39e8be30c8fc71aa5ccc3728 [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
39static void add_to_failed_list(struct resource_list_x *head,
40 struct pci_dev *dev, struct resource *res)
41{
42 struct resource_list_x *list = head;
43 struct resource_list_x *ln = list->next;
44 struct resource_list_x *tmp;
45
46 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
47 if (!tmp) {
48 pr_warning("add_to_failed_list: kmalloc() failed!\n");
49 return;
50 }
51
52 tmp->next = ln;
53 tmp->res = res;
54 tmp->dev = dev;
55 tmp->start = res->start;
56 tmp->end = res->end;
57 tmp->flags = res->flags;
58 list->next = tmp;
59}
60
61static void free_failed_list(struct resource_list_x *head)
62{
63 struct resource_list_x *list, *tmp;
64
65 for (list = head->next; list;) {
66 tmp = list;
67 list = list->next;
68 kfree(tmp);
69 }
70
71 head->next = NULL;
72}
73
74static void pbus_assign_resources_sorted(const struct pci_bus *bus,
75 struct resource_list_x *fail_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
77 struct pci_dev *dev;
78 struct resource *res;
79 struct resource_list head, *list, *tmp;
80 int idx;
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 head.next = NULL;
83 list_for_each_entry(dev, &bus->devices, bus_list) {
84 u16 class = dev->class >> 8;
85
Kenji Kaneshige9bded002006-10-04 02:15:34 -070086 /* Don't touch classless devices or host bridges or ioapics. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 if (class == PCI_CLASS_NOT_DEFINED ||
Satoru Takeuchi23186272006-09-12 10:21:44 -070088 class == PCI_CLASS_BRIDGE_HOST)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 continue;
90
Kenji Kaneshige9bded002006-10-04 02:15:34 -070091 /* Don't touch ioapic devices already enabled by firmware */
Satoru Takeuchi23186272006-09-12 10:21:44 -070092 if (class == PCI_CLASS_SYSTEM_PIC) {
Kenji Kaneshige9bded002006-10-04 02:15:34 -070093 u16 command;
94 pci_read_config_word(dev, PCI_COMMAND, &command);
95 if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
Satoru Takeuchi23186272006-09-12 10:21:44 -070096 continue;
97 }
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 pdev_sort_resources(dev, &head);
100 }
101
102 for (list = head.next; list;) {
103 res = list->res;
104 idx = res - &list->dev->resource[0];
Rajesh Shah542df5d2005-04-28 00:25:50 -0700105 if (pci_assign_resource(list->dev, idx)) {
Yinghai Lu568ddef2010-01-22 01:02:21 -0800106 if (fail_head && !pci_is_root_bus(list->dev->bus))
107 add_to_failed_list(fail_head, list->dev, res);
Rajesh Shah542df5d2005-04-28 00:25:50 -0700108 res->start = 0;
Ivan Kokshaysky960b8462005-07-07 03:07:56 +0400109 res->end = 0;
Rajesh Shah542df5d2005-04-28 00:25:50 -0700110 res->flags = 0;
111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 tmp = list;
113 list = list->next;
114 kfree(tmp);
115 }
116}
117
Dominik Brodowskib3743fa2005-09-09 13:03:23 -0700118void pci_setup_cardbus(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
120 struct pci_dev *bridge = bus->self;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600121 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 struct pci_bus_region region;
123
Bjorn Helgaas865df572009-11-04 10:32:57 -0700124 dev_info(&bridge->dev, "CardBus bridge to [bus %02x-%02x]\n",
125 bus->secondary, bus->subordinate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600127 res = bus->resource[0];
128 pcibios_resource_to_bus(bridge, &region, res);
129 if (res->flags & IORESOURCE_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 /*
131 * The IO resource is allocated a range twice as large as it
132 * would normally need. This allows us to set both IO regs.
133 */
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600134 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
136 region.start);
137 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
138 region.end);
139 }
140
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600141 res = bus->resource[1];
142 pcibios_resource_to_bus(bridge, &region, res);
143 if (res->flags & IORESOURCE_IO) {
144 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
146 region.start);
147 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
148 region.end);
149 }
150
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600151 res = bus->resource[2];
152 pcibios_resource_to_bus(bridge, &region, res);
153 if (res->flags & IORESOURCE_MEM) {
154 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
156 region.start);
157 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
158 region.end);
159 }
160
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600161 res = bus->resource[3];
162 pcibios_resource_to_bus(bridge, &region, res);
163 if (res->flags & IORESOURCE_MEM) {
164 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
166 region.start);
167 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
168 region.end);
169 }
170}
Dominik Brodowskib3743fa2005-09-09 13:03:23 -0700171EXPORT_SYMBOL(pci_setup_cardbus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173/* Initialize bridges with base/limit values we have collected.
174 PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
175 requires that if there is no I/O ports or memory behind the
176 bridge, corresponding range must be turned off by writing base
177 value greater than limit to the bridge's base/limit registers.
178
179 Note: care must be taken when updating I/O base/limit registers
180 of bridges which support 32-bit I/O. This update requires two
181 config space writes, so it's quite possible that an I/O window of
182 the bridge will have some undesirable address (e.g. 0) after the
183 first write. Ditto 64-bit prefetchable MMIO. */
Yinghai Lu7cc59972009-12-22 15:02:21 -0800184static void pci_setup_bridge_io(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
186 struct pci_dev *bridge = bus->self;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600187 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 struct pci_bus_region region;
Yinghai Lu7cc59972009-12-22 15:02:21 -0800189 u32 l, io_upper16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 /* Set up the top and bottom of the PCI I/O segment for this bus. */
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600192 res = bus->resource[0];
193 pcibios_resource_to_bus(bridge, &region, res);
194 if (res->flags & IORESOURCE_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 pci_read_config_dword(bridge, PCI_IO_BASE, &l);
196 l &= 0xffff0000;
197 l |= (region.start >> 8) & 0x00f0;
198 l |= region.end & 0xf000;
199 /* Set up upper 16 bits of I/O base/limit. */
200 io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600201 dev_info(&bridge->dev, " bridge window %pR\n", res);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800202 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 /* Clear upper 16 bits of I/O base/limit. */
204 io_upper16 = 0;
205 l = 0x00f0;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600206 dev_info(&bridge->dev, " bridge window [io disabled]\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
208 /* Temporarily disable the I/O range before updating PCI_IO_BASE. */
209 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
210 /* Update lower 16 bits of I/O base/limit. */
211 pci_write_config_dword(bridge, PCI_IO_BASE, l);
212 /* Update upper 16 bits of I/O base/limit. */
213 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800214}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Yinghai Lu7cc59972009-12-22 15:02:21 -0800216static void pci_setup_bridge_mmio(struct pci_bus *bus)
217{
218 struct pci_dev *bridge = bus->self;
219 struct resource *res;
220 struct pci_bus_region region;
221 u32 l;
222
223 /* Set up the top and bottom of the PCI Memory segment for this bus. */
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600224 res = bus->resource[1];
225 pcibios_resource_to_bus(bridge, &region, res);
226 if (res->flags & IORESOURCE_MEM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 l = (region.start >> 16) & 0xfff0;
228 l |= region.end & 0xfff00000;
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 l = 0x0000fff0;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600232 dev_info(&bridge->dev, " bridge window [mem disabled]\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
234 pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800235}
236
237static void pci_setup_bridge_mmio_pref(struct pci_bus *bus)
238{
239 struct pci_dev *bridge = bus->self;
240 struct resource *res;
241 struct pci_bus_region region;
242 u32 l, bu, lu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 /* Clear out the upper 32 bits of PREF limit.
245 If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
246 disables PREF range, which is ok. */
247 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
248
249 /* Set up PREF base/limit. */
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100250 bu = lu = 0;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600251 res = bus->resource[2];
252 pcibios_resource_to_bus(bridge, &region, res);
253 if (res->flags & IORESOURCE_PREFETCH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 l = (region.start >> 16) & 0xfff0;
255 l |= region.end & 0xfff00000;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600256 if (res->flags & IORESOURCE_MEM_64) {
Yinghai Lu1f82de12009-04-23 20:48:32 -0700257 bu = upper_32_bits(region.start);
258 lu = upper_32_bits(region.end);
Yinghai Lu1f82de12009-04-23 20:48:32 -0700259 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600260 dev_info(&bridge->dev, " bridge window %pR\n", res);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800261 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 l = 0x0000fff0;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600263 dev_info(&bridge->dev, " bridge window [mem pref disabled]\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
265 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
266
Alex Williamson59353ea2009-11-30 14:51:44 -0700267 /* Set the upper 32 bits of PREF base & limit. */
268 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
269 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800270}
271
272static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type)
273{
274 struct pci_dev *bridge = bus->self;
275
276 if (pci_is_enabled(bridge))
277 return;
278
279 dev_info(&bridge->dev, "PCI bridge to [bus %02x-%02x]\n",
280 bus->secondary, bus->subordinate);
281
282 if (type & IORESOURCE_IO)
283 pci_setup_bridge_io(bus);
284
285 if (type & IORESOURCE_MEM)
286 pci_setup_bridge_mmio(bus);
287
288 if (type & IORESOURCE_PREFETCH)
289 pci_setup_bridge_mmio_pref(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
292}
293
Yinghai Lu7cc59972009-12-22 15:02:21 -0800294static void pci_setup_bridge(struct pci_bus *bus)
295{
296 unsigned long type = IORESOURCE_IO | IORESOURCE_MEM |
297 IORESOURCE_PREFETCH;
298
299 __pci_setup_bridge(bus, type);
300}
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302/* Check whether the bridge supports optional I/O and
303 prefetchable memory ranges. If not, the respective
304 base/limit registers must be read-only and read as 0. */
Sam Ravnborg96bde062007-03-26 21:53:30 -0800305static void pci_bridge_check_ranges(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 u16 io;
308 u32 pmem;
309 struct pci_dev *bridge = bus->self;
310 struct resource *b_res;
311
312 b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
313 b_res[1].flags |= IORESOURCE_MEM;
314
315 pci_read_config_word(bridge, PCI_IO_BASE, &io);
316 if (!io) {
317 pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0);
318 pci_read_config_word(bridge, PCI_IO_BASE, &io);
319 pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
320 }
321 if (io)
322 b_res[0].flags |= IORESOURCE_IO;
323 /* DECchip 21050 pass 2 errata: the bridge may miss an address
324 disconnect boundary by one PCI data phase.
325 Workaround: do not use prefetching on this device. */
326 if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001)
327 return;
328 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
329 if (!pmem) {
330 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE,
331 0xfff0fff0);
332 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
333 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0);
334 }
Yinghai Lu1f82de12009-04-23 20:48:32 -0700335 if (pmem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
Yinghai Lu1f82de12009-04-23 20:48:32 -0700337 if ((pmem & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64)
338 b_res[2].flags |= IORESOURCE_MEM_64;
339 }
340
341 /* double check if bridge does support 64 bit pref */
342 if (b_res[2].flags & IORESOURCE_MEM_64) {
343 u32 mem_base_hi, tmp;
344 pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32,
345 &mem_base_hi);
346 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
347 0xffffffff);
348 pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp);
349 if (!tmp)
350 b_res[2].flags &= ~IORESOURCE_MEM_64;
351 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
352 mem_base_hi);
353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
356/* Helper function for sizing routines: find first available
357 bus resource of a given type. Note: we intentionally skip
358 the bus resources which have already been assigned (that is,
359 have non-NULL parent resource). */
Sam Ravnborg96bde062007-03-26 21:53:30 -0800360static struct resource *find_free_bus_resource(struct pci_bus *bus, unsigned long type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
362 int i;
363 struct resource *r;
364 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
365 IORESOURCE_PREFETCH;
366
367 for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
368 r = bus->resource[i];
Ivan Kokshaysky299de032005-06-15 18:59:27 +0400369 if (r == &ioport_resource || r == &iomem_resource)
370 continue;
Jesse Barnes55a10982009-10-27 09:39:18 -0700371 if (r && (r->flags & type_mask) == type && !r->parent)
372 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
374 return NULL;
375}
376
377/* Sizing the IO windows of the PCI-PCI bridge is trivial,
378 since these windows have 4K granularity and the IO ranges
379 of non-bridge PCI devices are limited to 256 bytes.
380 We must be careful with the ISA aliasing though. */
Eric W. Biederman28760482009-09-09 14:09:24 -0700381static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
383 struct pci_dev *dev;
384 struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO);
385 unsigned long size = 0, size1 = 0;
386
387 if (!b_res)
388 return;
389
390 list_for_each_entry(dev, &bus->devices, bus_list) {
391 int i;
392
393 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
394 struct resource *r = &dev->resource[i];
395 unsigned long r_size;
396
397 if (r->parent || !(r->flags & IORESOURCE_IO))
398 continue;
Zhao, Yu022edd82008-10-13 19:24:28 +0800399 r_size = resource_size(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 if (r_size < 0x400)
402 /* Might be re-aligned for ISA */
403 size += r_size;
404 else
405 size1 += r_size;
406 }
407 }
Eric W. Biederman28760482009-09-09 14:09:24 -0700408 if (size < min_size)
409 size = min_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410/* To be fixed in 2.5: we should have sort of HAVE_ISA
411 flag in the struct pci_bus. */
412#if defined(CONFIG_ISA) || defined(CONFIG_EISA)
413 size = (size & 0xff) + ((size & ~0xffUL) << 2);
414#endif
Milind Arun Choudhary6f6f8c22007-07-09 11:55:51 -0700415 size = ALIGN(size + size1, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if (!size) {
Bjorn Helgaas865df572009-11-04 10:32:57 -0700417 if (b_res->start || b_res->end)
418 dev_info(&bus->self->dev, "disabling bridge window "
419 "%pR to [bus %02x-%02x] (unused)\n", b_res,
420 bus->secondary, bus->subordinate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 b_res->flags = 0;
422 return;
423 }
424 /* Alignment of the IO window is always 4K */
425 b_res->start = 4096;
426 b_res->end = b_res->start + size - 1;
Ivan Kokshaysky88452562008-03-30 19:50:14 +0400427 b_res->flags |= IORESOURCE_STARTALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
430/* Calculate the size of the bus and minimal alignment which
431 guarantees that all child resources fit in this size. */
Eric W. Biederman28760482009-09-09 14:09:24 -0700432static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
433 unsigned long type, resource_size_t min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
435 struct pci_dev *dev;
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100436 resource_size_t min_align, align, size;
437 resource_size_t aligns[12]; /* Alignments from 1Mb to 2Gb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 int order, max_order;
439 struct resource *b_res = find_free_bus_resource(bus, type);
Yinghai Lu1f82de12009-04-23 20:48:32 -0700440 unsigned int mem64_mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 if (!b_res)
443 return 0;
444
445 memset(aligns, 0, sizeof(aligns));
446 max_order = 0;
447 size = 0;
448
Yinghai Lu1f82de12009-04-23 20:48:32 -0700449 mem64_mask = b_res->flags & IORESOURCE_MEM_64;
450 b_res->flags &= ~IORESOURCE_MEM_64;
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 list_for_each_entry(dev, &bus->devices, bus_list) {
453 int i;
Yinghai Lu1f82de12009-04-23 20:48:32 -0700454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
456 struct resource *r = &dev->resource[i];
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100457 resource_size_t r_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 if (r->parent || (r->flags & mask) != type)
460 continue;
Zhao, Yu022edd82008-10-13 19:24:28 +0800461 r_size = resource_size(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 /* For bridges size != alignment */
Chris Wright6faf17f2009-08-28 13:00:06 -0700463 align = pci_resource_alignment(dev, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 order = __ffs(align) - 20;
465 if (order > 11) {
Bjorn Helgaas865df572009-11-04 10:32:57 -0700466 dev_warn(&dev->dev, "disabling BAR %d: %pR "
467 "(bad alignment %#llx)\n", i, r,
468 (unsigned long long) align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 r->flags = 0;
470 continue;
471 }
472 size += r_size;
473 if (order < 0)
474 order = 0;
475 /* Exclude ranges with size > align from
476 calculation of the alignment. */
477 if (r_size == align)
478 aligns[order] += align;
479 if (order > max_order)
480 max_order = order;
Yinghai Lu1f82de12009-04-23 20:48:32 -0700481 mem64_mask &= r->flags & IORESOURCE_MEM_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
483 }
Eric W. Biederman28760482009-09-09 14:09:24 -0700484 if (size < min_size)
485 size = min_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 align = 0;
488 min_align = 0;
489 for (order = 0; order <= max_order; order++) {
Jeremy Fitzhardinge8308c542008-09-11 01:31:50 -0700490 resource_size_t align1 = 1;
491
492 align1 <<= (order + 20);
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (!align)
495 min_align = align1;
Milind Arun Choudhary6f6f8c22007-07-09 11:55:51 -0700496 else if (ALIGN(align + min_align, min_align) < align1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 min_align = align1 >> 1;
498 align += aligns[order];
499 }
Milind Arun Choudhary6f6f8c22007-07-09 11:55:51 -0700500 size = ALIGN(size, min_align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (!size) {
Bjorn Helgaas865df572009-11-04 10:32:57 -0700502 if (b_res->start || b_res->end)
503 dev_info(&bus->self->dev, "disabling bridge window "
504 "%pR to [bus %02x-%02x] (unused)\n", b_res,
505 bus->secondary, bus->subordinate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 b_res->flags = 0;
507 return 1;
508 }
509 b_res->start = min_align;
510 b_res->end = size + min_align - 1;
Ivan Kokshaysky88452562008-03-30 19:50:14 +0400511 b_res->flags |= IORESOURCE_STARTALIGN;
Yinghai Lu1f82de12009-04-23 20:48:32 -0700512 b_res->flags |= mem64_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return 1;
514}
515
Adrian Bunk5468ae62008-04-18 13:53:56 -0700516static void pci_bus_size_cardbus(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 struct pci_dev *bridge = bus->self;
519 struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
520 u16 ctrl;
521
522 /*
523 * Reserve some resources for CardBus. We reserve
524 * a fixed amount of bus space for CardBus bridges.
525 */
Linus Torvalds934b7022008-04-22 18:16:30 -0700526 b_res[0].start = 0;
527 b_res[0].end = pci_cardbus_io_size - 1;
528 b_res[0].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Linus Torvalds934b7022008-04-22 18:16:30 -0700530 b_res[1].start = 0;
531 b_res[1].end = pci_cardbus_io_size - 1;
532 b_res[1].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 /*
535 * Check whether prefetchable memory is supported
536 * by this bridge.
537 */
538 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
539 if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
540 ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
541 pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
542 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
543 }
544
545 /*
546 * If we have prefetchable memory support, allocate
547 * two regions. Otherwise, allocate one region of
548 * twice the size.
549 */
550 if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
Linus Torvalds934b7022008-04-22 18:16:30 -0700551 b_res[2].start = 0;
552 b_res[2].end = pci_cardbus_mem_size - 1;
553 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_SIZEALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Linus Torvalds934b7022008-04-22 18:16:30 -0700555 b_res[3].start = 0;
556 b_res[3].end = pci_cardbus_mem_size - 1;
557 b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 } else {
Linus Torvalds934b7022008-04-22 18:16:30 -0700559 b_res[3].start = 0;
560 b_res[3].end = pci_cardbus_mem_size * 2 - 1;
561 b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563}
564
Sam Ravnborg451124a2008-02-02 22:33:43 +0100565void __ref pci_bus_size_bridges(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
567 struct pci_dev *dev;
568 unsigned long mask, prefmask;
Eric W. Biederman28760482009-09-09 14:09:24 -0700569 resource_size_t min_mem_size = 0, min_io_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 list_for_each_entry(dev, &bus->devices, bus_list) {
572 struct pci_bus *b = dev->subordinate;
573 if (!b)
574 continue;
575
576 switch (dev->class >> 8) {
577 case PCI_CLASS_BRIDGE_CARDBUS:
578 pci_bus_size_cardbus(b);
579 break;
580
581 case PCI_CLASS_BRIDGE_PCI:
582 default:
583 pci_bus_size_bridges(b);
584 break;
585 }
586 }
587
588 /* The root bus? */
589 if (!bus->self)
590 return;
591
592 switch (bus->self->class >> 8) {
593 case PCI_CLASS_BRIDGE_CARDBUS:
594 /* don't size cardbuses yet. */
595 break;
596
597 case PCI_CLASS_BRIDGE_PCI:
598 pci_bridge_check_ranges(bus);
Eric W. Biederman28760482009-09-09 14:09:24 -0700599 if (bus->self->is_hotplug_bridge) {
600 min_io_size = pci_hotplug_io_size;
601 min_mem_size = pci_hotplug_mem_size;
602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 default:
Eric W. Biederman28760482009-09-09 14:09:24 -0700604 pbus_size_io(bus, min_io_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 /* If the bridge supports prefetchable range, size it
606 separately. If it doesn't, or its prefetchable window
607 has already been allocated by arch code, try
608 non-prefetchable range for both types of PCI memory
609 resources. */
610 mask = IORESOURCE_MEM;
611 prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
Eric W. Biederman28760482009-09-09 14:09:24 -0700612 if (pbus_size_mem(bus, prefmask, prefmask, min_mem_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 mask = prefmask; /* Success, size non-prefetch only. */
Eric W. Biederman28760482009-09-09 14:09:24 -0700614 else
615 min_mem_size += min_mem_size;
616 pbus_size_mem(bus, mask, IORESOURCE_MEM, min_mem_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 break;
618 }
619}
620EXPORT_SYMBOL(pci_bus_size_bridges);
621
Yinghai Lu568ddef2010-01-22 01:02:21 -0800622static void __ref __pci_bus_assign_resources(const struct pci_bus *bus,
623 struct resource_list_x *fail_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
625 struct pci_bus *b;
626 struct pci_dev *dev;
627
Yinghai Lu568ddef2010-01-22 01:02:21 -0800628 pbus_assign_resources_sorted(bus, fail_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 list_for_each_entry(dev, &bus->devices, bus_list) {
631 b = dev->subordinate;
632 if (!b)
633 continue;
634
Yinghai Lu568ddef2010-01-22 01:02:21 -0800635 __pci_bus_assign_resources(b, fail_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 switch (dev->class >> 8) {
638 case PCI_CLASS_BRIDGE_PCI:
639 pci_setup_bridge(b);
640 break;
641
642 case PCI_CLASS_BRIDGE_CARDBUS:
643 pci_setup_cardbus(b);
644 break;
645
646 default:
Bjorn Helgaas80ccba12008-06-13 10:52:11 -0600647 dev_info(&dev->dev, "not setting up bridge for bus "
648 "%04x:%02x\n", pci_domain_nr(b), b->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 break;
650 }
651 }
652}
Yinghai Lu568ddef2010-01-22 01:02:21 -0800653
654void __ref pci_bus_assign_resources(const struct pci_bus *bus)
655{
656 __pci_bus_assign_resources(bus, NULL);
657}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658EXPORT_SYMBOL(pci_bus_assign_resources);
659
Yinghai Lu5009b462010-01-22 01:02:20 -0800660static void pci_bridge_release_resources(struct pci_bus *bus,
661 unsigned long type)
662{
663 int idx;
664 bool changed = false;
665 struct pci_dev *dev;
666 struct resource *r;
667 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
668 IORESOURCE_PREFETCH;
669
670 dev = bus->self;
671 for (idx = PCI_BRIDGE_RESOURCES; idx <= PCI_BRIDGE_RESOURCE_END;
672 idx++) {
673 r = &dev->resource[idx];
674 if ((r->flags & type_mask) != type)
675 continue;
676 if (!r->parent)
677 continue;
678 /*
679 * if there are children under that, we should release them
680 * all
681 */
682 release_child_resources(r);
683 if (!release_resource(r)) {
684 dev_printk(KERN_DEBUG, &dev->dev,
685 "resource %d %pR released\n", idx, r);
686 /* keep the old size */
687 r->end = resource_size(r) - 1;
688 r->start = 0;
689 r->flags = 0;
690 changed = true;
691 }
692 }
693
694 if (changed) {
695 /* avoiding touch the one without PREF */
696 if (type & IORESOURCE_PREFETCH)
697 type = IORESOURCE_PREFETCH;
698 __pci_setup_bridge(bus, type);
699 }
700}
701
702enum release_type {
703 leaf_only,
704 whole_subtree,
705};
706/*
707 * try to release pci bridge resources that is from leaf bridge,
708 * so we can allocate big new one later
709 */
710static void __ref pci_bus_release_bridge_resources(struct pci_bus *bus,
711 unsigned long type,
712 enum release_type rel_type)
713{
714 struct pci_dev *dev;
715 bool is_leaf_bridge = true;
716
717 list_for_each_entry(dev, &bus->devices, bus_list) {
718 struct pci_bus *b = dev->subordinate;
719 if (!b)
720 continue;
721
722 is_leaf_bridge = false;
723
724 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
725 continue;
726
727 if (rel_type == whole_subtree)
728 pci_bus_release_bridge_resources(b, type,
729 whole_subtree);
730 }
731
732 if (pci_is_root_bus(bus))
733 return;
734
735 if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
736 return;
737
738 if ((rel_type == whole_subtree) || is_leaf_bridge)
739 pci_bridge_release_resources(bus, type);
740}
741
Yinghai Lu76fbc262008-06-23 20:33:06 +0200742static void pci_bus_dump_res(struct pci_bus *bus)
743{
744 int i;
745
746 for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
747 struct resource *res = bus->resource[i];
Yinghai Lu7c9342b2009-12-22 15:02:24 -0800748
749 if (!res || !res->end || !res->flags)
Yinghai Lu76fbc262008-06-23 20:33:06 +0200750 continue;
751
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600752 dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res);
Yinghai Lu76fbc262008-06-23 20:33:06 +0200753 }
754}
755
756static void pci_bus_dump_resources(struct pci_bus *bus)
757{
758 struct pci_bus *b;
759 struct pci_dev *dev;
760
761
762 pci_bus_dump_res(bus);
763
764 list_for_each_entry(dev, &bus->devices, bus_list) {
765 b = dev->subordinate;
766 if (!b)
767 continue;
768
769 pci_bus_dump_resources(b);
770 }
771}
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773void __init
774pci_assign_unassigned_resources(void)
775{
776 struct pci_bus *bus;
777
778 /* Depth first, calculate sizes and alignments of all
779 subordinate buses. */
780 list_for_each_entry(bus, &pci_root_buses, node) {
781 pci_bus_size_bridges(bus);
782 }
783 /* Depth last, allocate resources and update the hardware. */
784 list_for_each_entry(bus, &pci_root_buses, node) {
785 pci_bus_assign_resources(bus);
786 pci_enable_bridges(bus);
787 }
Yinghai Lu76fbc262008-06-23 20:33:06 +0200788
789 /* dump the resource on buses */
790 list_for_each_entry(bus, &pci_root_buses, node) {
791 pci_bus_dump_resources(bus);
792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793}