ia64/linux-2.6.18-xen.hg

view drivers/pci/setup-res.c @ 702:8fbf8e2fd087

dom0 linux: Fix issue on reassigning resources to PCI-PCI bridge.

This patch fixes the issue on reassigning resources to PCI-PCI bridge,
which was found by Zhao, Yu.

Current "quirk_align_mem_resources" updates Base/Limit register of
PCI-PCI bridge, if IORESOURCE_MEM is set in "dev->resource[i].flags".
But, when "quirk_align_mem_resources" is called,
dev->resource[i].flags
is not initialized, because "quirk_align_mem_resources" is called
before "pci_read_bridge_bases". As a result, current code does not
update Base/Limit register.

This patch sets All F to Base register and sets 0 to Limit register,
regardless of "dev->resource[i].flags". After that,
"pci_assign_unassigned_resources" calculates resource window size and
assigns resource to PCI-PCI bridge.

Signed-off-by: Yuji Shimada <shimada-yxb@necst.nec.co.jp>
author Keir Fraser <keir.fraser@citrix.com>
date Fri Oct 17 12:01:56 2008 +0100 (2008-10-17)
parents 16c549b3ba59
children a29df0e2bfe2
line source
1 /*
2 * drivers/pci/setup-res.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 */
12 /* fixed for multiple pci buses, 1999 Andrea Arcangeli <andrea@suse.de> */
14 /*
15 * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
16 * Resource sorting
17 */
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/pci.h>
22 #include <linux/errno.h>
23 #include <linux/ioport.h>
24 #include <linux/cache.h>
25 #include <linux/slab.h>
26 #include "pci.h"
29 void
30 pci_update_resource(struct pci_dev *dev, struct resource *res, int resno)
31 {
32 struct pci_bus_region region;
33 u32 new, check, mask;
34 int reg;
36 /* Ignore resources for unimplemented BARs and unused resource slots
37 for 64 bit BARs. */
38 if (!res->flags)
39 return;
41 pcibios_resource_to_bus(dev, &region, res);
43 pr_debug(" got res [%llx:%llx] bus [%lx:%lx] flags %lx for "
44 "BAR %d of %s\n", (unsigned long long)res->start,
45 (unsigned long long)res->end,
46 region.start, region.end, res->flags, resno, pci_name(dev));
48 new = region.start | (res->flags & PCI_REGION_FLAG_MASK);
49 if (res->flags & IORESOURCE_IO)
50 mask = (u32)PCI_BASE_ADDRESS_IO_MASK;
51 else
52 mask = (u32)PCI_BASE_ADDRESS_MEM_MASK;
54 if (resno < 6) {
55 reg = PCI_BASE_ADDRESS_0 + 4 * resno;
56 } else if (resno == PCI_ROM_RESOURCE) {
57 if (!(res->flags & IORESOURCE_ROM_ENABLE))
58 return;
59 new |= PCI_ROM_ADDRESS_ENABLE;
60 reg = dev->rom_base_reg;
61 } else {
62 /* Hmm, non-standard resource. */
64 return; /* kill uninitialised var warning */
65 }
67 pci_write_config_dword(dev, reg, new);
68 pci_read_config_dword(dev, reg, &check);
70 if ((new ^ check) & mask) {
71 printk(KERN_ERR "PCI: Error while updating region "
72 "%s/%d (%08x != %08x)\n", pci_name(dev), resno,
73 new, check);
74 }
76 if ((new & (PCI_BASE_ADDRESS_SPACE|PCI_BASE_ADDRESS_MEM_TYPE_MASK)) ==
77 (PCI_BASE_ADDRESS_SPACE_MEMORY|PCI_BASE_ADDRESS_MEM_TYPE_64)) {
78 new = region.start >> 16 >> 16;
79 pci_write_config_dword(dev, reg + 4, new);
80 pci_read_config_dword(dev, reg + 4, &check);
81 if (check != new) {
82 printk(KERN_ERR "PCI: Error updating region "
83 "%s/%d (high %08x != %08x)\n",
84 pci_name(dev), resno, new, check);
85 }
86 }
87 res->flags &= ~IORESOURCE_UNSET;
88 pr_debug("PCI: moved device %s resource %d (%lx) to %x\n",
89 pci_name(dev), resno, res->flags,
90 new & ~PCI_REGION_FLAG_MASK);
91 }
93 int __devinit
94 pci_claim_resource(struct pci_dev *dev, int resource)
95 {
96 struct resource *res = &dev->resource[resource];
97 struct resource *root = NULL;
98 char *dtype = resource < PCI_BRIDGE_RESOURCES ? "device" : "bridge";
99 int err;
101 root = pcibios_select_root(dev, res);
103 err = -EINVAL;
104 if (root != NULL)
105 err = insert_resource(root, res);
107 if (err) {
108 printk(KERN_ERR "PCI: %s region %d of %s %s [%llx:%llx]\n",
109 root ? "Address space collision on" :
110 "No parent found for",
111 resource, dtype, pci_name(dev),
112 (unsigned long long)res->start,
113 (unsigned long long)res->end);
114 }
116 return err;
117 }
118 EXPORT_SYMBOL_GPL(pci_claim_resource);
120 #ifdef CONFIG_PCI_REASSIGN
121 void pci_disable_bridge_window(struct pci_dev *dev)
122 {
123 printk(KERN_DEBUG "PCI: Disable bridge window on %s\n", pci_name(dev));
125 /* MMIO Base/Limit */
126 pci_write_config_dword(dev, PCI_MEMORY_BASE, 0x0000fff0);
128 /* Prefetchable MMIO Base/Limit */
129 pci_write_config_dword(dev, PCI_PREF_LIMIT_UPPER32, 0);
130 pci_write_config_dword(dev, PCI_PREF_MEMORY_BASE, 0x0000fff0);
131 pci_write_config_dword(dev, PCI_PREF_BASE_UPPER32, 0xffffffff);
132 }
133 #endif
135 int pci_assign_resource(struct pci_dev *dev, int resno)
136 {
137 struct pci_bus *bus = dev->bus;
138 struct resource *res = dev->resource + resno;
139 resource_size_t size, min, align;
140 int ret;
141 int reassigndev = reassign_resources ? is_reassigndev(dev) : 0;
143 size = res->end - res->start + 1;
144 min = (res->flags & IORESOURCE_IO) ? PCIBIOS_MIN_IO : PCIBIOS_MIN_MEM;
145 /* The bridge resources are special, as their
146 size != alignment. Sizing routines return
147 required alignment in the "start" field. */
148 if (resno < PCI_BRIDGE_RESOURCES) {
149 align = size;
150 if ((reassigndev) &&
151 (res->flags & IORESOURCE_MEM)) {
152 align = ALIGN(align, PAGE_SIZE);
153 }
154 } else {
155 align = res->start;
156 }
158 /* First, try exact prefetching match.. */
159 ret = pci_bus_alloc_resource(bus, res, size, align, min,
160 IORESOURCE_PREFETCH,
161 pcibios_align_resource, dev);
163 if (ret < 0 && (res->flags & IORESOURCE_PREFETCH)) {
164 /*
165 * That failed.
166 *
167 * But a prefetching area can handle a non-prefetching
168 * window (it will just not perform as well).
169 */
170 ret = pci_bus_alloc_resource(bus, res, size, align, min, 0,
171 pcibios_align_resource, dev);
172 }
174 if (ret) {
175 printk(KERN_ERR "PCI: Failed to allocate %s resource "
176 "#%d:%llx@%llx for %s\n",
177 res->flags & IORESOURCE_IO ? "I/O" : "mem",
178 resno, (unsigned long long)size,
179 (unsigned long long)res->start, pci_name(dev));
180 } else if (resno < PCI_BRIDGE_RESOURCES) {
181 if (reassign_resources && is_reassigndev(dev))
182 printk(KERN_DEBUG "PCI: Assign resource(%d) on %s "
183 "%016llx - %016llx\n", resno, pci_name(dev),
184 (unsigned long long)res->start,
185 (unsigned long long)res->end);
186 pci_update_resource(dev, res, resno);
187 }
189 return ret;
190 }
192 #ifdef CONFIG_EMBEDDED
193 int pci_assign_resource_fixed(struct pci_dev *dev, int resno)
194 {
195 struct pci_bus *bus = dev->bus;
196 struct resource *res = dev->resource + resno;
197 unsigned int type_mask;
198 int i, ret = -EBUSY;
200 type_mask = IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH;
202 for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
203 struct resource *r = bus->resource[i];
204 if (!r)
205 continue;
207 /* type_mask must match */
208 if ((res->flags ^ r->flags) & type_mask)
209 continue;
211 ret = request_resource(r, res);
213 if (ret == 0)
214 break;
215 }
217 if (ret) {
218 printk(KERN_ERR "PCI: Failed to allocate %s resource "
219 "#%d:%llx@%llx for %s\n",
220 res->flags & IORESOURCE_IO ? "I/O" : "mem",
221 resno, (unsigned long long)(res->end - res->start + 1),
222 (unsigned long long)res->start, pci_name(dev));
223 } else if (resno < PCI_BRIDGE_RESOURCES) {
224 pci_update_resource(dev, res, resno);
225 }
227 return ret;
228 }
229 EXPORT_SYMBOL_GPL(pci_assign_resource_fixed);
230 #endif
232 /* Sort resources by alignment */
233 void __devinit
234 pdev_sort_resources(struct pci_dev *dev, struct resource_list *head)
235 {
236 int i;
238 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
239 struct resource *r;
240 struct resource_list *list, *tmp;
241 resource_size_t r_align;
243 r = &dev->resource[i];
244 r_align = r->end - r->start;
246 if (!(r->flags) || r->parent)
247 continue;
248 if (!r_align) {
249 printk(KERN_WARNING "PCI: Ignore bogus resource %d "
250 "[%llx:%llx] of %s\n",
251 i, (unsigned long long)r->start,
252 (unsigned long long)r->end, pci_name(dev));
253 continue;
254 }
255 r_align = (i < PCI_BRIDGE_RESOURCES) ? r_align + 1 : r->start;
256 for (list = head; ; list = list->next) {
257 resource_size_t align = 0;
258 struct resource_list *ln = list->next;
259 int idx;
261 if (ln) {
262 idx = ln->res - &ln->dev->resource[0];
263 align = (idx < PCI_BRIDGE_RESOURCES) ?
264 ln->res->end - ln->res->start + 1 :
265 ln->res->start;
266 }
267 if (r_align > align) {
268 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
269 if (!tmp)
270 panic("pdev_sort_resources(): "
271 "kmalloc() failed!\n");
272 tmp->next = ln;
273 tmp->res = r;
274 tmp->dev = dev;
275 list->next = tmp;
276 break;
277 }
278 }
279 }
280 }