ia64/linux-2.6.18-xen.hg

view drivers/pci/setup-res.c @ 696:2b5cc22ab406

xen/dom0: Reassign memory resources to device for pci passthrough.

This patch adds the function that reassign page-aligned memory
resources, to dom0 linux. The function is useful when we assign I/O
device to HVM domain using pci passthrough.

When we assign a device to HVM domain using pci passthrough,
the device needs to be assigned page-aligned memory resources. If the
memory resource is not page-aligned, following error occurs.

Error: pci: 0000:00:1d.7: non-page-aligned MMIO BAR found.

On many system, BIOS assigns memory resources to the device and
enables it. So my patch disables the device, and releases resources,
Then it assigns page-aligned memory resource to the device.

To reassign resources, please add boot parameters of dom0 linux as
follows.

reassign_resources reassigndev=00:1d.7,01:00.0

reassign_resources
Enables reassigning resources.

reassigndev= Specifies devices include I/O device and
PCI-PCI
bridge to reassign resources. PCI-PCI bridge
can be specified, if resource windows need to
be expanded.

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