[PATCH] kprobes: Fix return probes on sys_execve
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / pci / setup-res.c
blob50d6685dcbcce801682c9600a81be2a98f90f8a1
1 /*
2 * drivers/pci/setup-res.c
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)
9 * Support routines for initializing a PCI subsystem.
12 /* fixed for multiple pci buses, 1999 Andrea Arcangeli <andrea@suse.de> */
15 * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
16 * Resource sorting
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)
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 [%lx:%lx] bus [%lx:%lx] flags %lx for "
44 "BAR %d of %s\n", res->start, res->end,
45 region.start, region.end, res->flags, resno, pci_name(dev));
47 new = region.start | (res->flags & PCI_REGION_FLAG_MASK);
48 if (res->flags & IORESOURCE_IO)
49 mask = (u32)PCI_BASE_ADDRESS_IO_MASK;
50 else
51 mask = (u32)PCI_BASE_ADDRESS_MEM_MASK;
53 if (resno < 6) {
54 reg = PCI_BASE_ADDRESS_0 + 4 * resno;
55 } else if (resno == PCI_ROM_RESOURCE) {
56 if (!(res->flags & IORESOURCE_ROM_ENABLE))
57 return;
58 new |= PCI_ROM_ADDRESS_ENABLE;
59 reg = dev->rom_base_reg;
60 } else {
61 /* Hmm, non-standard resource. */
63 return; /* kill uninitialised var warning */
66 pci_write_config_dword(dev, reg, new);
67 pci_read_config_dword(dev, reg, &check);
69 if ((new ^ check) & mask) {
70 printk(KERN_ERR "PCI: Error while updating region "
71 "%s/%d (%08x != %08x)\n", pci_name(dev), resno,
72 new, check);
75 if ((new & (PCI_BASE_ADDRESS_SPACE|PCI_BASE_ADDRESS_MEM_TYPE_MASK)) ==
76 (PCI_BASE_ADDRESS_SPACE_MEMORY|PCI_BASE_ADDRESS_MEM_TYPE_64)) {
77 new = region.start >> 16 >> 16;
78 pci_write_config_dword(dev, reg + 4, new);
79 pci_read_config_dword(dev, reg + 4, &check);
80 if (check != new) {
81 printk(KERN_ERR "PCI: Error updating region "
82 "%s/%d (high %08x != %08x)\n",
83 pci_name(dev), resno, new, check);
86 res->flags &= ~IORESOURCE_UNSET;
87 pr_debug("PCI: moved device %s resource %d (%lx) to %x\n",
88 pci_name(dev), resno, res->flags,
89 new & ~PCI_REGION_FLAG_MASK);
92 int __devinit
93 pci_claim_resource(struct pci_dev *dev, int resource)
95 struct resource *res = &dev->resource[resource];
96 struct resource *root = NULL;
97 char *dtype = resource < PCI_BRIDGE_RESOURCES ? "device" : "bridge";
98 int err;
100 root = pcibios_select_root(dev, res);
102 err = -EINVAL;
103 if (root != NULL)
104 err = insert_resource(root, res);
106 if (err) {
107 printk(KERN_ERR "PCI: %s region %d of %s %s [%lx:%lx]\n",
108 root ? "Address space collision on" :
109 "No parent found for",
110 resource, dtype, pci_name(dev), res->start, res->end);
113 return err;
116 int pci_assign_resource(struct pci_dev *dev, int resno)
118 struct pci_bus *bus = dev->bus;
119 struct resource *res = dev->resource + resno;
120 unsigned long size, min, align;
121 int ret;
123 size = res->end - res->start + 1;
124 min = (res->flags & IORESOURCE_IO) ? PCIBIOS_MIN_IO : PCIBIOS_MIN_MEM;
125 /* The bridge resources are special, as their
126 size != alignment. Sizing routines return
127 required alignment in the "start" field. */
128 align = (resno < PCI_BRIDGE_RESOURCES) ? size : res->start;
130 /* First, try exact prefetching match.. */
131 ret = pci_bus_alloc_resource(bus, res, size, align, min,
132 IORESOURCE_PREFETCH,
133 pcibios_align_resource, dev);
135 if (ret < 0 && (res->flags & IORESOURCE_PREFETCH)) {
137 * That failed.
139 * But a prefetching area can handle a non-prefetching
140 * window (it will just not perform as well).
142 ret = pci_bus_alloc_resource(bus, res, size, align, min, 0,
143 pcibios_align_resource, dev);
146 if (ret) {
147 printk(KERN_ERR "PCI: Failed to allocate %s resource #%d:%lx@%lx for %s\n",
148 res->flags & IORESOURCE_IO ? "I/O" : "mem",
149 resno, size, res->start, pci_name(dev));
150 } else if (resno < PCI_BRIDGE_RESOURCES) {
151 pci_update_resource(dev, res, resno);
154 return ret;
157 /* Sort resources by alignment */
158 void __devinit
159 pdev_sort_resources(struct pci_dev *dev, struct resource_list *head)
161 int i;
163 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
164 struct resource *r;
165 struct resource_list *list, *tmp;
166 unsigned long r_align;
168 r = &dev->resource[i];
169 r_align = r->end - r->start;
171 if (!(r->flags) || r->parent)
172 continue;
173 if (!r_align) {
174 printk(KERN_WARNING "PCI: Ignore bogus resource %d "
175 "[%lx:%lx] of %s\n",
176 i, r->start, r->end, pci_name(dev));
177 continue;
179 r_align = (i < PCI_BRIDGE_RESOURCES) ? r_align + 1 : r->start;
180 for (list = head; ; list = list->next) {
181 unsigned long align = 0;
182 struct resource_list *ln = list->next;
183 int idx;
185 if (ln) {
186 idx = ln->res - &ln->dev->resource[0];
187 align = (idx < PCI_BRIDGE_RESOURCES) ?
188 ln->res->end - ln->res->start + 1 :
189 ln->res->start;
191 if (r_align > align) {
192 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
193 if (!tmp)
194 panic("pdev_sort_resources(): "
195 "kmalloc() failed!\n");
196 tmp->next = ln;
197 tmp->res = r;
198 tmp->dev = dev;
199 list->next = tmp;
200 break;