ia64/linux-2.6.18-xen.hg

view drivers/pci/setup-res.c @ 701:16c549b3ba59

xen: conditionalize PCI reassign code by a config option, selected
only from privileged Xen configurations.

Also eliminate the pointless new macro ROUND_UP_TO_PAGESIZE().

Signed-off-by: Jan Beulich <jbeulich@novell.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Oct 14 14:58:08 2008 +0100 (2008-10-14)
parents 2b5cc22ab406
children 8fbf8e2fd087
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
122 pci_update_bridge(struct pci_dev *dev, int resno)
123 {
124 struct resource *res = &dev->resource[resno];
125 struct pci_bus_region region;
126 u32 l, dw, base_up32, limit_up32;
128 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE ||
129 (dev->class >> 8) != PCI_CLASS_BRIDGE_PCI) {
130 return;
131 }
133 if (!res->flags)
134 return;
136 switch (resno) {
137 case 8 : /* MMIO Base/Limit */
138 pcibios_resource_to_bus(dev, &region, res);
139 if (res->flags & IORESOURCE_MEM &&
140 !(res->flags & IORESOURCE_PREFETCH)) {
141 l = (region.start >> 16) & 0xfff0;
142 l |= region.end & 0xfff00000;
143 } else {
144 l = 0x0000fff0;
145 }
146 pci_write_config_dword(dev, PCI_MEMORY_BASE, l);
148 break;
150 case 9 : /* Prefetchable MMIO Base/Limit */
151 /* Clear out the upper 32 bits of PREF limit.
152 * If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
153 * disables PREF range, which is ok.
154 */
155 pci_write_config_dword(dev, PCI_PREF_LIMIT_UPPER32, 0);
157 /* Get PREF 32/64 bits Addressing mode */
158 pci_read_config_dword(dev, PCI_PREF_MEMORY_BASE, &dw);
160 pcibios_resource_to_bus(dev, &region, res);
161 if (res->flags & IORESOURCE_MEM &&
162 res->flags & IORESOURCE_PREFETCH) {
163 l = (region.start >> 16) & 0xfff0;
164 l |= region.end & 0xfff00000;
166 if (dw & PCI_PREF_RANGE_TYPE_64) {
167 base_up32 = (region.start >> 32) & 0xffffffff;
168 limit_up32 = (region.end >> 32) & 0xffffffff;
169 } else {
170 base_up32 = 0;
171 limit_up32 = 0;
172 }
173 } else {
174 l = 0x0000fff0;
175 base_up32 = 0xffffffff;
176 limit_up32 = 0;
177 }
178 pci_write_config_dword(dev, PCI_PREF_MEMORY_BASE, l);
179 /* Set up the upper 32 bits of PREF base/limit. */
180 pci_write_config_dword(dev, PCI_PREF_BASE_UPPER32, base_up32);
181 pci_write_config_dword(dev, PCI_PREF_LIMIT_UPPER32, limit_up32);
182 break;
183 default :
184 BUG();
185 break;
186 }
187 }
188 #endif
190 int pci_assign_resource(struct pci_dev *dev, int resno)
191 {
192 struct pci_bus *bus = dev->bus;
193 struct resource *res = dev->resource + resno;
194 resource_size_t size, min, align;
195 int ret;
196 int reassigndev = reassign_resources ? is_reassigndev(dev) : 0;
198 size = res->end - res->start + 1;
199 min = (res->flags & IORESOURCE_IO) ? PCIBIOS_MIN_IO : PCIBIOS_MIN_MEM;
200 /* The bridge resources are special, as their
201 size != alignment. Sizing routines return
202 required alignment in the "start" field. */
203 if (resno < PCI_BRIDGE_RESOURCES) {
204 align = size;
205 if ((reassigndev) &&
206 (res->flags & IORESOURCE_MEM)) {
207 align = ALIGN(align, PAGE_SIZE);
208 }
209 } else {
210 align = res->start;
211 }
213 /* First, try exact prefetching match.. */
214 ret = pci_bus_alloc_resource(bus, res, size, align, min,
215 IORESOURCE_PREFETCH,
216 pcibios_align_resource, dev);
218 if (ret < 0 && (res->flags & IORESOURCE_PREFETCH)) {
219 /*
220 * That failed.
221 *
222 * But a prefetching area can handle a non-prefetching
223 * window (it will just not perform as well).
224 */
225 ret = pci_bus_alloc_resource(bus, res, size, align, min, 0,
226 pcibios_align_resource, dev);
227 }
229 if (ret) {
230 printk(KERN_ERR "PCI: Failed to allocate %s resource "
231 "#%d:%llx@%llx for %s\n",
232 res->flags & IORESOURCE_IO ? "I/O" : "mem",
233 resno, (unsigned long long)size,
234 (unsigned long long)res->start, pci_name(dev));
235 } else if (resno < PCI_BRIDGE_RESOURCES) {
236 if (reassign_resources && is_reassigndev(dev))
237 printk(KERN_DEBUG "PCI: Assign resource(%d) on %s "
238 "%016llx - %016llx\n", resno, pci_name(dev),
239 (unsigned long long)res->start,
240 (unsigned long long)res->end);
241 pci_update_resource(dev, res, resno);
242 }
244 return ret;
245 }
247 #ifdef CONFIG_EMBEDDED
248 int pci_assign_resource_fixed(struct pci_dev *dev, int resno)
249 {
250 struct pci_bus *bus = dev->bus;
251 struct resource *res = dev->resource + resno;
252 unsigned int type_mask;
253 int i, ret = -EBUSY;
255 type_mask = IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH;
257 for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
258 struct resource *r = bus->resource[i];
259 if (!r)
260 continue;
262 /* type_mask must match */
263 if ((res->flags ^ r->flags) & type_mask)
264 continue;
266 ret = request_resource(r, res);
268 if (ret == 0)
269 break;
270 }
272 if (ret) {
273 printk(KERN_ERR "PCI: Failed to allocate %s resource "
274 "#%d:%llx@%llx for %s\n",
275 res->flags & IORESOURCE_IO ? "I/O" : "mem",
276 resno, (unsigned long long)(res->end - res->start + 1),
277 (unsigned long long)res->start, pci_name(dev));
278 } else if (resno < PCI_BRIDGE_RESOURCES) {
279 pci_update_resource(dev, res, resno);
280 }
282 return ret;
283 }
284 EXPORT_SYMBOL_GPL(pci_assign_resource_fixed);
285 #endif
287 /* Sort resources by alignment */
288 void __devinit
289 pdev_sort_resources(struct pci_dev *dev, struct resource_list *head)
290 {
291 int i;
293 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
294 struct resource *r;
295 struct resource_list *list, *tmp;
296 resource_size_t r_align;
298 r = &dev->resource[i];
299 r_align = r->end - r->start;
301 if (!(r->flags) || r->parent)
302 continue;
303 if (!r_align) {
304 printk(KERN_WARNING "PCI: Ignore bogus resource %d "
305 "[%llx:%llx] of %s\n",
306 i, (unsigned long long)r->start,
307 (unsigned long long)r->end, pci_name(dev));
308 continue;
309 }
310 r_align = (i < PCI_BRIDGE_RESOURCES) ? r_align + 1 : r->start;
311 for (list = head; ; list = list->next) {
312 resource_size_t align = 0;
313 struct resource_list *ln = list->next;
314 int idx;
316 if (ln) {
317 idx = ln->res - &ln->dev->resource[0];
318 align = (idx < PCI_BRIDGE_RESOURCES) ?
319 ln->res->end - ln->res->start + 1 :
320 ln->res->start;
321 }
322 if (r_align > align) {
323 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
324 if (!tmp)
325 panic("pdev_sort_resources(): "
326 "kmalloc() failed!\n");
327 tmp->next = ln;
328 tmp->res = r;
329 tmp->dev = dev;
330 list->next = tmp;
331 break;
332 }
333 }
334 }
335 }