blob: 21212155eabae5c632ec6d562248778082354091 [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
Yinghai Lu6841ec62010-01-22 01:02:25 -080074static void __dev_sort_resources(struct pci_dev *dev,
75 struct resource_list *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Yinghai Lu6841ec62010-01-22 01:02:25 -080077 u16 class = dev->class >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Yinghai Lu6841ec62010-01-22 01:02:25 -080079 /* Don't touch classless devices or host bridges or ioapics. */
80 if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST)
81 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Yinghai Lu6841ec62010-01-22 01:02:25 -080083 /* Don't touch ioapic devices already enabled by firmware */
84 if (class == PCI_CLASS_SYSTEM_PIC) {
85 u16 command;
86 pci_read_config_word(dev, PCI_COMMAND, &command);
87 if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
88 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 }
90
Yinghai Lu6841ec62010-01-22 01:02:25 -080091 pdev_sort_resources(dev, head);
92}
93
94static void __assign_resources_sorted(struct resource_list *head,
95 struct resource_list_x *fail_head)
96{
97 struct resource *res;
98 struct resource_list *list, *tmp;
99 int idx;
100
101 for (list = head->next; list;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 res = list->res;
103 idx = res - &list->dev->resource[0];
Yinghai Lu9a928662010-02-28 15:49:39 -0800104
Rajesh Shah542df5d2005-04-28 00:25:50 -0700105 if (pci_assign_resource(list->dev, idx)) {
Yinghai Lu9a928662010-02-28 15:49:39 -0800106 if (fail_head && !pci_is_root_bus(list->dev->bus)) {
107 /*
108 * if the failed res is for ROM BAR, and it will
109 * be enabled later, don't add it to the list
110 */
111 if (!((idx == PCI_ROM_RESOURCE) &&
112 (!(res->flags & IORESOURCE_ROM_ENABLE))))
113 add_to_failed_list(fail_head, list->dev, res);
114 }
Rajesh Shah542df5d2005-04-28 00:25:50 -0700115 res->start = 0;
Ivan Kokshaysky960b8462005-07-07 03:07:56 +0400116 res->end = 0;
Rajesh Shah542df5d2005-04-28 00:25:50 -0700117 res->flags = 0;
118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 tmp = list;
120 list = list->next;
121 kfree(tmp);
122 }
123}
124
Yinghai Lu6841ec62010-01-22 01:02:25 -0800125static void pdev_assign_resources_sorted(struct pci_dev *dev,
126 struct resource_list_x *fail_head)
127{
128 struct resource_list head;
129
130 head.next = NULL;
131 __dev_sort_resources(dev, &head);
132 __assign_resources_sorted(&head, fail_head);
133
134}
135
136static void pbus_assign_resources_sorted(const struct pci_bus *bus,
137 struct resource_list_x *fail_head)
138{
139 struct pci_dev *dev;
140 struct resource_list head;
141
142 head.next = NULL;
143 list_for_each_entry(dev, &bus->devices, bus_list)
144 __dev_sort_resources(dev, &head);
145
146 __assign_resources_sorted(&head, fail_head);
147}
148
Dominik Brodowskib3743fa2005-09-09 13:03:23 -0700149void pci_setup_cardbus(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
151 struct pci_dev *bridge = bus->self;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600152 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 struct pci_bus_region region;
154
Bjorn Helgaas865df572009-11-04 10:32:57 -0700155 dev_info(&bridge->dev, "CardBus bridge to [bus %02x-%02x]\n",
156 bus->secondary, bus->subordinate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600158 res = bus->resource[0];
159 pcibios_resource_to_bus(bridge, &region, res);
160 if (res->flags & IORESOURCE_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 /*
162 * The IO resource is allocated a range twice as large as it
163 * would normally need. This allows us to set both IO regs.
164 */
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600165 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
167 region.start);
168 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
169 region.end);
170 }
171
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600172 res = bus->resource[1];
173 pcibios_resource_to_bus(bridge, &region, res);
174 if (res->flags & IORESOURCE_IO) {
175 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
177 region.start);
178 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
179 region.end);
180 }
181
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600182 res = bus->resource[2];
183 pcibios_resource_to_bus(bridge, &region, res);
184 if (res->flags & IORESOURCE_MEM) {
185 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
187 region.start);
188 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
189 region.end);
190 }
191
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600192 res = bus->resource[3];
193 pcibios_resource_to_bus(bridge, &region, res);
194 if (res->flags & IORESOURCE_MEM) {
195 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
197 region.start);
198 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
199 region.end);
200 }
201}
Dominik Brodowskib3743fa2005-09-09 13:03:23 -0700202EXPORT_SYMBOL(pci_setup_cardbus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204/* Initialize bridges with base/limit values we have collected.
205 PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
206 requires that if there is no I/O ports or memory behind the
207 bridge, corresponding range must be turned off by writing base
208 value greater than limit to the bridge's base/limit registers.
209
210 Note: care must be taken when updating I/O base/limit registers
211 of bridges which support 32-bit I/O. This update requires two
212 config space writes, so it's quite possible that an I/O window of
213 the bridge will have some undesirable address (e.g. 0) after the
214 first write. Ditto 64-bit prefetchable MMIO. */
Yinghai Lu7cc59972009-12-22 15:02:21 -0800215static void pci_setup_bridge_io(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 struct pci_dev *bridge = bus->self;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600218 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 struct pci_bus_region region;
Yinghai Lu7cc59972009-12-22 15:02:21 -0800220 u32 l, io_upper16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 /* Set up the top and bottom of the PCI I/O segment for this bus. */
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600223 res = bus->resource[0];
224 pcibios_resource_to_bus(bridge, &region, res);
225 if (res->flags & IORESOURCE_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 pci_read_config_dword(bridge, PCI_IO_BASE, &l);
227 l &= 0xffff0000;
228 l |= (region.start >> 8) & 0x00f0;
229 l |= region.end & 0xf000;
230 /* Set up upper 16 bits of I/O base/limit. */
231 io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600232 dev_info(&bridge->dev, " bridge window %pR\n", res);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800233 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 /* Clear upper 16 bits of I/O base/limit. */
235 io_upper16 = 0;
236 l = 0x00f0;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600237 dev_info(&bridge->dev, " bridge window [io disabled]\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
239 /* Temporarily disable the I/O range before updating PCI_IO_BASE. */
240 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
241 /* Update lower 16 bits of I/O base/limit. */
242 pci_write_config_dword(bridge, PCI_IO_BASE, l);
243 /* Update upper 16 bits of I/O base/limit. */
244 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800245}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Yinghai Lu7cc59972009-12-22 15:02:21 -0800247static void pci_setup_bridge_mmio(struct pci_bus *bus)
248{
249 struct pci_dev *bridge = bus->self;
250 struct resource *res;
251 struct pci_bus_region region;
252 u32 l;
253
254 /* Set up the top and bottom of the PCI Memory segment for this bus. */
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600255 res = bus->resource[1];
256 pcibios_resource_to_bus(bridge, &region, res);
257 if (res->flags & IORESOURCE_MEM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 l = (region.start >> 16) & 0xfff0;
259 l |= region.end & 0xfff00000;
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 disabled]\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
265 pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800266}
267
268static void pci_setup_bridge_mmio_pref(struct pci_bus *bus)
269{
270 struct pci_dev *bridge = bus->self;
271 struct resource *res;
272 struct pci_bus_region region;
273 u32 l, bu, lu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 /* Clear out the upper 32 bits of PREF limit.
276 If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
277 disables PREF range, which is ok. */
278 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
279
280 /* Set up PREF base/limit. */
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100281 bu = lu = 0;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600282 res = bus->resource[2];
283 pcibios_resource_to_bus(bridge, &region, res);
284 if (res->flags & IORESOURCE_PREFETCH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 l = (region.start >> 16) & 0xfff0;
286 l |= region.end & 0xfff00000;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600287 if (res->flags & IORESOURCE_MEM_64) {
Yinghai Lu1f82de12009-04-23 20:48:32 -0700288 bu = upper_32_bits(region.start);
289 lu = upper_32_bits(region.end);
Yinghai Lu1f82de12009-04-23 20:48:32 -0700290 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600291 dev_info(&bridge->dev, " bridge window %pR\n", res);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800292 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 l = 0x0000fff0;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600294 dev_info(&bridge->dev, " bridge window [mem pref disabled]\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
296 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
297
Alex Williamson59353ea2009-11-30 14:51:44 -0700298 /* Set the upper 32 bits of PREF base & limit. */
299 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
300 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800301}
302
303static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type)
304{
305 struct pci_dev *bridge = bus->self;
306
Yinghai Lu7cc59972009-12-22 15:02:21 -0800307 dev_info(&bridge->dev, "PCI bridge to [bus %02x-%02x]\n",
308 bus->secondary, bus->subordinate);
309
310 if (type & IORESOURCE_IO)
311 pci_setup_bridge_io(bus);
312
313 if (type & IORESOURCE_MEM)
314 pci_setup_bridge_mmio(bus);
315
316 if (type & IORESOURCE_PREFETCH)
317 pci_setup_bridge_mmio_pref(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
320}
321
Yinghai Lu7cc59972009-12-22 15:02:21 -0800322static void pci_setup_bridge(struct pci_bus *bus)
323{
324 unsigned long type = IORESOURCE_IO | IORESOURCE_MEM |
325 IORESOURCE_PREFETCH;
326
327 __pci_setup_bridge(bus, type);
328}
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330/* Check whether the bridge supports optional I/O and
331 prefetchable memory ranges. If not, the respective
332 base/limit registers must be read-only and read as 0. */
Sam Ravnborg96bde062007-03-26 21:53:30 -0800333static void pci_bridge_check_ranges(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 u16 io;
336 u32 pmem;
337 struct pci_dev *bridge = bus->self;
338 struct resource *b_res;
339
340 b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
341 b_res[1].flags |= IORESOURCE_MEM;
342
343 pci_read_config_word(bridge, PCI_IO_BASE, &io);
344 if (!io) {
345 pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0);
346 pci_read_config_word(bridge, PCI_IO_BASE, &io);
347 pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
348 }
349 if (io)
350 b_res[0].flags |= IORESOURCE_IO;
351 /* DECchip 21050 pass 2 errata: the bridge may miss an address
352 disconnect boundary by one PCI data phase.
353 Workaround: do not use prefetching on this device. */
354 if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001)
355 return;
356 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
357 if (!pmem) {
358 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE,
359 0xfff0fff0);
360 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
361 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0);
362 }
Yinghai Lu1f82de12009-04-23 20:48:32 -0700363 if (pmem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
Yinghai Lu99586102010-01-22 01:02:28 -0800365 if ((pmem & PCI_PREF_RANGE_TYPE_MASK) ==
366 PCI_PREF_RANGE_TYPE_64) {
Yinghai Lu1f82de12009-04-23 20:48:32 -0700367 b_res[2].flags |= IORESOURCE_MEM_64;
Yinghai Lu99586102010-01-22 01:02:28 -0800368 b_res[2].flags |= PCI_PREF_RANGE_TYPE_64;
369 }
Yinghai Lu1f82de12009-04-23 20:48:32 -0700370 }
371
372 /* double check if bridge does support 64 bit pref */
373 if (b_res[2].flags & IORESOURCE_MEM_64) {
374 u32 mem_base_hi, tmp;
375 pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32,
376 &mem_base_hi);
377 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
378 0xffffffff);
379 pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp);
380 if (!tmp)
381 b_res[2].flags &= ~IORESOURCE_MEM_64;
382 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
383 mem_base_hi);
384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
387/* Helper function for sizing routines: find first available
388 bus resource of a given type. Note: we intentionally skip
389 the bus resources which have already been assigned (that is,
390 have non-NULL parent resource). */
Sam Ravnborg96bde062007-03-26 21:53:30 -0800391static struct resource *find_free_bus_resource(struct pci_bus *bus, unsigned long type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 int i;
394 struct resource *r;
395 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
396 IORESOURCE_PREFETCH;
397
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -0700398 pci_bus_for_each_resource(bus, r, i) {
Ivan Kokshaysky299de032005-06-15 18:59:27 +0400399 if (r == &ioport_resource || r == &iomem_resource)
400 continue;
Jesse Barnes55a10982009-10-27 09:39:18 -0700401 if (r && (r->flags & type_mask) == type && !r->parent)
402 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
404 return NULL;
405}
406
Ram Pai13583b12011-02-14 17:43:17 -0800407static resource_size_t calculate_iosize(resource_size_t size,
408 resource_size_t min_size,
409 resource_size_t size1,
410 resource_size_t old_size,
411 resource_size_t align)
412{
413 if (size < min_size)
414 size = min_size;
415 if (old_size == 1 )
416 old_size = 0;
417 /* To be fixed in 2.5: we should have sort of HAVE_ISA
418 flag in the struct pci_bus. */
419#if defined(CONFIG_ISA) || defined(CONFIG_EISA)
420 size = (size & 0xff) + ((size & ~0xffUL) << 2);
421#endif
422 size = ALIGN(size + size1, align);
423 if (size < old_size)
424 size = old_size;
425 return size;
426}
427
428static resource_size_t calculate_memsize(resource_size_t size,
429 resource_size_t min_size,
430 resource_size_t size1,
431 resource_size_t old_size,
432 resource_size_t align)
433{
434 if (size < min_size)
435 size = min_size;
436 if (old_size == 1 )
437 old_size = 0;
438 if (size < old_size)
439 size = old_size;
440 size = ALIGN(size + size1, align);
441 return size;
442}
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444/* Sizing the IO windows of the PCI-PCI bridge is trivial,
445 since these windows have 4K granularity and the IO ranges
446 of non-bridge PCI devices are limited to 256 bytes.
447 We must be careful with the ISA aliasing though. */
Eric W. Biederman28760482009-09-09 14:09:24 -0700448static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
450 struct pci_dev *dev;
451 struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO);
Ram Pai13583b12011-02-14 17:43:17 -0800452 unsigned long size = 0, size1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 if (!b_res)
455 return;
456
457 list_for_each_entry(dev, &bus->devices, bus_list) {
458 int i;
459
460 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
461 struct resource *r = &dev->resource[i];
462 unsigned long r_size;
463
464 if (r->parent || !(r->flags & IORESOURCE_IO))
465 continue;
Zhao, Yu022edd82008-10-13 19:24:28 +0800466 r_size = resource_size(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 if (r_size < 0x400)
469 /* Might be re-aligned for ISA */
470 size += r_size;
471 else
472 size1 += r_size;
473 }
474 }
Ram Pai13583b12011-02-14 17:43:17 -0800475 size = calculate_iosize(size, min_size, size1,
476 resource_size(b_res), 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if (!size) {
Bjorn Helgaas865df572009-11-04 10:32:57 -0700478 if (b_res->start || b_res->end)
479 dev_info(&bus->self->dev, "disabling bridge window "
480 "%pR to [bus %02x-%02x] (unused)\n", b_res,
481 bus->secondary, bus->subordinate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 b_res->flags = 0;
483 return;
484 }
485 /* Alignment of the IO window is always 4K */
486 b_res->start = 4096;
487 b_res->end = b_res->start + size - 1;
Ivan Kokshaysky88452562008-03-30 19:50:14 +0400488 b_res->flags |= IORESOURCE_STARTALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
491/* Calculate the size of the bus and minimal alignment which
492 guarantees that all child resources fit in this size. */
Eric W. Biederman28760482009-09-09 14:09:24 -0700493static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
494 unsigned long type, resource_size_t min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 struct pci_dev *dev;
Ram Pai13583b12011-02-14 17:43:17 -0800497 resource_size_t min_align, align, size;
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100498 resource_size_t aligns[12]; /* Alignments from 1Mb to 2Gb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 int order, max_order;
500 struct resource *b_res = find_free_bus_resource(bus, type);
Yinghai Lu1f82de12009-04-23 20:48:32 -0700501 unsigned int mem64_mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 if (!b_res)
504 return 0;
505
506 memset(aligns, 0, sizeof(aligns));
507 max_order = 0;
508 size = 0;
509
Yinghai Lu1f82de12009-04-23 20:48:32 -0700510 mem64_mask = b_res->flags & IORESOURCE_MEM_64;
511 b_res->flags &= ~IORESOURCE_MEM_64;
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 list_for_each_entry(dev, &bus->devices, bus_list) {
514 int i;
Yinghai Lu1f82de12009-04-23 20:48:32 -0700515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
517 struct resource *r = &dev->resource[i];
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100518 resource_size_t r_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 if (r->parent || (r->flags & mask) != type)
521 continue;
Zhao, Yu022edd82008-10-13 19:24:28 +0800522 r_size = resource_size(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 /* For bridges size != alignment */
Chris Wright6faf17f2009-08-28 13:00:06 -0700524 align = pci_resource_alignment(dev, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 order = __ffs(align) - 20;
526 if (order > 11) {
Bjorn Helgaas865df572009-11-04 10:32:57 -0700527 dev_warn(&dev->dev, "disabling BAR %d: %pR "
528 "(bad alignment %#llx)\n", i, r,
529 (unsigned long long) align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 r->flags = 0;
531 continue;
532 }
533 size += r_size;
534 if (order < 0)
535 order = 0;
536 /* Exclude ranges with size > align from
537 calculation of the alignment. */
538 if (r_size == align)
539 aligns[order] += align;
540 if (order > max_order)
541 max_order = order;
Yinghai Lu1f82de12009-04-23 20:48:32 -0700542 mem64_mask &= r->flags & IORESOURCE_MEM_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 align = 0;
546 min_align = 0;
547 for (order = 0; order <= max_order; order++) {
Jeremy Fitzhardinge8308c542008-09-11 01:31:50 -0700548 resource_size_t align1 = 1;
549
550 align1 <<= (order + 20);
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (!align)
553 min_align = align1;
Milind Arun Choudhary6f6f8c22007-07-09 11:55:51 -0700554 else if (ALIGN(align + min_align, min_align) < align1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 min_align = align1 >> 1;
556 align += aligns[order];
557 }
Ram Pai13583b12011-02-14 17:43:17 -0800558 size = calculate_memsize(size, min_size, 0, resource_size(b_res), align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (!size) {
Bjorn Helgaas865df572009-11-04 10:32:57 -0700560 if (b_res->start || b_res->end)
561 dev_info(&bus->self->dev, "disabling bridge window "
562 "%pR to [bus %02x-%02x] (unused)\n", b_res,
563 bus->secondary, bus->subordinate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 b_res->flags = 0;
565 return 1;
566 }
567 b_res->start = min_align;
568 b_res->end = size + min_align - 1;
Ivan Kokshaysky88452562008-03-30 19:50:14 +0400569 b_res->flags |= IORESOURCE_STARTALIGN;
Yinghai Lu1f82de12009-04-23 20:48:32 -0700570 b_res->flags |= mem64_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return 1;
572}
573
Adrian Bunk5468ae62008-04-18 13:53:56 -0700574static void pci_bus_size_cardbus(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
576 struct pci_dev *bridge = bus->self;
577 struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
578 u16 ctrl;
579
580 /*
581 * Reserve some resources for CardBus. We reserve
582 * a fixed amount of bus space for CardBus bridges.
583 */
Linus Torvalds934b7022008-04-22 18:16:30 -0700584 b_res[0].start = 0;
585 b_res[0].end = pci_cardbus_io_size - 1;
586 b_res[0].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Linus Torvalds934b7022008-04-22 18:16:30 -0700588 b_res[1].start = 0;
589 b_res[1].end = pci_cardbus_io_size - 1;
590 b_res[1].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 /*
593 * Check whether prefetchable memory is supported
594 * by this bridge.
595 */
596 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
597 if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
598 ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
599 pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
600 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
601 }
602
603 /*
604 * If we have prefetchable memory support, allocate
605 * two regions. Otherwise, allocate one region of
606 * twice the size.
607 */
608 if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
Linus Torvalds934b7022008-04-22 18:16:30 -0700609 b_res[2].start = 0;
610 b_res[2].end = pci_cardbus_mem_size - 1;
611 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_SIZEALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Linus Torvalds934b7022008-04-22 18:16:30 -0700613 b_res[3].start = 0;
614 b_res[3].end = pci_cardbus_mem_size - 1;
615 b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 } else {
Linus Torvalds934b7022008-04-22 18:16:30 -0700617 b_res[3].start = 0;
618 b_res[3].end = pci_cardbus_mem_size * 2 - 1;
619 b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
621}
622
Sam Ravnborg451124a2008-02-02 22:33:43 +0100623void __ref pci_bus_size_bridges(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
625 struct pci_dev *dev;
626 unsigned long mask, prefmask;
Eric W. Biederman28760482009-09-09 14:09:24 -0700627 resource_size_t min_mem_size = 0, min_io_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 list_for_each_entry(dev, &bus->devices, bus_list) {
630 struct pci_bus *b = dev->subordinate;
631 if (!b)
632 continue;
633
634 switch (dev->class >> 8) {
635 case PCI_CLASS_BRIDGE_CARDBUS:
636 pci_bus_size_cardbus(b);
637 break;
638
639 case PCI_CLASS_BRIDGE_PCI:
640 default:
641 pci_bus_size_bridges(b);
642 break;
643 }
644 }
645
646 /* The root bus? */
647 if (!bus->self)
648 return;
649
650 switch (bus->self->class >> 8) {
651 case PCI_CLASS_BRIDGE_CARDBUS:
652 /* don't size cardbuses yet. */
653 break;
654
655 case PCI_CLASS_BRIDGE_PCI:
656 pci_bridge_check_ranges(bus);
Eric W. Biederman28760482009-09-09 14:09:24 -0700657 if (bus->self->is_hotplug_bridge) {
658 min_io_size = pci_hotplug_io_size;
659 min_mem_size = pci_hotplug_mem_size;
660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 default:
Eric W. Biederman28760482009-09-09 14:09:24 -0700662 pbus_size_io(bus, min_io_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 /* If the bridge supports prefetchable range, size it
664 separately. If it doesn't, or its prefetchable window
665 has already been allocated by arch code, try
666 non-prefetchable range for both types of PCI memory
667 resources. */
668 mask = IORESOURCE_MEM;
669 prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
Eric W. Biederman28760482009-09-09 14:09:24 -0700670 if (pbus_size_mem(bus, prefmask, prefmask, min_mem_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 mask = prefmask; /* Success, size non-prefetch only. */
Eric W. Biederman28760482009-09-09 14:09:24 -0700672 else
673 min_mem_size += min_mem_size;
674 pbus_size_mem(bus, mask, IORESOURCE_MEM, min_mem_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 break;
676 }
677}
678EXPORT_SYMBOL(pci_bus_size_bridges);
679
Yinghai Lu568ddef2010-01-22 01:02:21 -0800680static void __ref __pci_bus_assign_resources(const struct pci_bus *bus,
681 struct resource_list_x *fail_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
683 struct pci_bus *b;
684 struct pci_dev *dev;
685
Yinghai Lu568ddef2010-01-22 01:02:21 -0800686 pbus_assign_resources_sorted(bus, fail_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 list_for_each_entry(dev, &bus->devices, bus_list) {
689 b = dev->subordinate;
690 if (!b)
691 continue;
692
Yinghai Lu568ddef2010-01-22 01:02:21 -0800693 __pci_bus_assign_resources(b, fail_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 switch (dev->class >> 8) {
696 case PCI_CLASS_BRIDGE_PCI:
Yinghai Lu6841ec62010-01-22 01:02:25 -0800697 if (!pci_is_enabled(dev))
698 pci_setup_bridge(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 break;
700
701 case PCI_CLASS_BRIDGE_CARDBUS:
702 pci_setup_cardbus(b);
703 break;
704
705 default:
Bjorn Helgaas80ccba12008-06-13 10:52:11 -0600706 dev_info(&dev->dev, "not setting up bridge for bus "
707 "%04x:%02x\n", pci_domain_nr(b), b->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 break;
709 }
710 }
711}
Yinghai Lu568ddef2010-01-22 01:02:21 -0800712
713void __ref pci_bus_assign_resources(const struct pci_bus *bus)
714{
715 __pci_bus_assign_resources(bus, NULL);
716}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717EXPORT_SYMBOL(pci_bus_assign_resources);
718
Yinghai Lu6841ec62010-01-22 01:02:25 -0800719static void __ref __pci_bridge_assign_resources(const struct pci_dev *bridge,
720 struct resource_list_x *fail_head)
721{
722 struct pci_bus *b;
723
724 pdev_assign_resources_sorted((struct pci_dev *)bridge, fail_head);
725
726 b = bridge->subordinate;
727 if (!b)
728 return;
729
730 __pci_bus_assign_resources(b, fail_head);
731
732 switch (bridge->class >> 8) {
733 case PCI_CLASS_BRIDGE_PCI:
734 pci_setup_bridge(b);
735 break;
736
737 case PCI_CLASS_BRIDGE_CARDBUS:
738 pci_setup_cardbus(b);
739 break;
740
741 default:
742 dev_info(&bridge->dev, "not setting up bridge for bus "
743 "%04x:%02x\n", pci_domain_nr(b), b->number);
744 break;
745 }
746}
Yinghai Lu5009b462010-01-22 01:02:20 -0800747static void pci_bridge_release_resources(struct pci_bus *bus,
748 unsigned long type)
749{
750 int idx;
751 bool changed = false;
752 struct pci_dev *dev;
753 struct resource *r;
754 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
755 IORESOURCE_PREFETCH;
756
757 dev = bus->self;
758 for (idx = PCI_BRIDGE_RESOURCES; idx <= PCI_BRIDGE_RESOURCE_END;
759 idx++) {
760 r = &dev->resource[idx];
761 if ((r->flags & type_mask) != type)
762 continue;
763 if (!r->parent)
764 continue;
765 /*
766 * if there are children under that, we should release them
767 * all
768 */
769 release_child_resources(r);
770 if (!release_resource(r)) {
771 dev_printk(KERN_DEBUG, &dev->dev,
772 "resource %d %pR released\n", idx, r);
773 /* keep the old size */
774 r->end = resource_size(r) - 1;
775 r->start = 0;
776 r->flags = 0;
777 changed = true;
778 }
779 }
780
781 if (changed) {
782 /* avoiding touch the one without PREF */
783 if (type & IORESOURCE_PREFETCH)
784 type = IORESOURCE_PREFETCH;
785 __pci_setup_bridge(bus, type);
786 }
787}
788
789enum release_type {
790 leaf_only,
791 whole_subtree,
792};
793/*
794 * try to release pci bridge resources that is from leaf bridge,
795 * so we can allocate big new one later
796 */
797static void __ref pci_bus_release_bridge_resources(struct pci_bus *bus,
798 unsigned long type,
799 enum release_type rel_type)
800{
801 struct pci_dev *dev;
802 bool is_leaf_bridge = true;
803
804 list_for_each_entry(dev, &bus->devices, bus_list) {
805 struct pci_bus *b = dev->subordinate;
806 if (!b)
807 continue;
808
809 is_leaf_bridge = false;
810
811 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
812 continue;
813
814 if (rel_type == whole_subtree)
815 pci_bus_release_bridge_resources(b, type,
816 whole_subtree);
817 }
818
819 if (pci_is_root_bus(bus))
820 return;
821
822 if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
823 return;
824
825 if ((rel_type == whole_subtree) || is_leaf_bridge)
826 pci_bridge_release_resources(bus, type);
827}
828
Yinghai Lu76fbc262008-06-23 20:33:06 +0200829static void pci_bus_dump_res(struct pci_bus *bus)
830{
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -0700831 struct resource *res;
832 int i;
Yinghai Lu76fbc262008-06-23 20:33:06 +0200833
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -0700834 pci_bus_for_each_resource(bus, res, i) {
Yinghai Lu7c9342b2009-12-22 15:02:24 -0800835 if (!res || !res->end || !res->flags)
Yinghai Lu76fbc262008-06-23 20:33:06 +0200836 continue;
837
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600838 dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res);
Yinghai Lu76fbc262008-06-23 20:33:06 +0200839 }
840}
841
842static void pci_bus_dump_resources(struct pci_bus *bus)
843{
844 struct pci_bus *b;
845 struct pci_dev *dev;
846
847
848 pci_bus_dump_res(bus);
849
850 list_for_each_entry(dev, &bus->devices, bus_list) {
851 b = dev->subordinate;
852 if (!b)
853 continue;
854
855 pci_bus_dump_resources(b);
856 }
857}
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859void __init
860pci_assign_unassigned_resources(void)
861{
862 struct pci_bus *bus;
863
864 /* Depth first, calculate sizes and alignments of all
865 subordinate buses. */
866 list_for_each_entry(bus, &pci_root_buses, node) {
867 pci_bus_size_bridges(bus);
868 }
869 /* Depth last, allocate resources and update the hardware. */
870 list_for_each_entry(bus, &pci_root_buses, node) {
Linus Torvalds769d9962010-05-12 18:39:45 -0700871 pci_bus_assign_resources(bus);
Yinghai Lu977d17b2010-01-22 01:02:24 -0800872 pci_enable_bridges(bus);
Linus Torvalds769d9962010-05-12 18:39:45 -0700873 }
Yinghai Lu76fbc262008-06-23 20:33:06 +0200874
875 /* dump the resource on buses */
876 list_for_each_entry(bus, &pci_root_buses, node) {
877 pci_bus_dump_resources(bus);
878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
Yinghai Lu6841ec62010-01-22 01:02:25 -0800880
881void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
882{
883 struct pci_bus *parent = bridge->subordinate;
Yinghai Lu32180e42010-01-22 01:02:27 -0800884 int tried_times = 0;
885 struct resource_list_x head, *list;
Yinghai Lu6841ec62010-01-22 01:02:25 -0800886 int retval;
Yinghai Lu32180e42010-01-22 01:02:27 -0800887 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
888 IORESOURCE_PREFETCH;
Yinghai Lu6841ec62010-01-22 01:02:25 -0800889
Yinghai Lu32180e42010-01-22 01:02:27 -0800890 head.next = NULL;
891
892again:
Yinghai Lu6841ec62010-01-22 01:02:25 -0800893 pci_bus_size_bridges(parent);
Yinghai Lu32180e42010-01-22 01:02:27 -0800894 __pci_bridge_assign_resources(bridge, &head);
Yinghai Lu32180e42010-01-22 01:02:27 -0800895
896 tried_times++;
897
898 if (!head.next)
Yinghai Lu3f579c32010-05-21 14:35:06 -0700899 goto enable_all;
Yinghai Lu32180e42010-01-22 01:02:27 -0800900
901 if (tried_times >= 2) {
902 /* still fail, don't need to try more */
903 free_failed_list(&head);
Yinghai Lu3f579c32010-05-21 14:35:06 -0700904 goto enable_all;
Yinghai Lu32180e42010-01-22 01:02:27 -0800905 }
906
907 printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
908 tried_times + 1);
909
910 /*
911 * Try to release leaf bridge's resources that doesn't fit resource of
912 * child device under that bridge
913 */
914 for (list = head.next; list;) {
915 struct pci_bus *bus = list->dev->bus;
916 unsigned long flags = list->flags;
917
918 pci_bus_release_bridge_resources(bus, flags & type_mask,
919 whole_subtree);
920 list = list->next;
921 }
922 /* restore size and flags */
923 for (list = head.next; list;) {
924 struct resource *res = list->res;
925
926 res->start = list->start;
927 res->end = list->end;
928 res->flags = list->flags;
929 if (list->dev->subordinate)
930 res->flags = 0;
931
932 list = list->next;
933 }
934 free_failed_list(&head);
935
936 goto again;
Yinghai Lu3f579c32010-05-21 14:35:06 -0700937
938enable_all:
939 retval = pci_reenable_device(bridge);
940 pci_set_master(bridge);
941 pci_enable_bridges(parent);
Yinghai Lu6841ec62010-01-22 01:02:25 -0800942}
943EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);