aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/pci/setup-res.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/pci/setup-res.c')
-rw-r--r--drivers/pci/setup-res.c200
1 files changed, 200 insertions, 0 deletions
diff --git a/drivers/pci/setup-res.c b/drivers/pci/setup-res.c
new file mode 100644
index 000000000000..1ca21d2ba11c
--- /dev/null
+++ b/drivers/pci/setup-res.c
@@ -0,0 +1,200 @@
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 */
11
12/* fixed for multiple pci buses, 1999 Andrea Arcangeli <andrea@suse.de> */
13
14/*
15 * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
16 * Resource sorting
17 */
18
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"
27
28
29static void
30pci_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;
35
36 pcibios_resource_to_bus(dev, &region, res);
37
38 pr_debug(" got res [%lx:%lx] bus [%lx:%lx] flags %lx for "
39 "BAR %d of %s\n", res->start, res->end,
40 region.start, region.end, res->flags, resno, pci_name(dev));
41
42 new = region.start | (res->flags & PCI_REGION_FLAG_MASK);
43 if (res->flags & IORESOURCE_IO)
44 mask = (u32)PCI_BASE_ADDRESS_IO_MASK;
45 else
46 mask = (u32)PCI_BASE_ADDRESS_MEM_MASK;
47
48 if (resno < 6) {
49 reg = PCI_BASE_ADDRESS_0 + 4 * resno;
50 } else if (resno == PCI_ROM_RESOURCE) {
51 new |= res->flags & IORESOURCE_ROM_ENABLE;
52 reg = dev->rom_base_reg;
53 } else {
54 /* Hmm, non-standard resource. */
55
56 return; /* kill uninitialised var warning */
57 }
58
59 pci_write_config_dword(dev, reg, new);
60 pci_read_config_dword(dev, reg, &check);
61
62 if ((new ^ check) & mask) {
63 printk(KERN_ERR "PCI: Error while updating region "
64 "%s/%d (%08x != %08x)\n", pci_name(dev), resno,
65 new, check);
66 }
67
68 if ((new & (PCI_BASE_ADDRESS_SPACE|PCI_BASE_ADDRESS_MEM_TYPE_MASK)) ==
69 (PCI_BASE_ADDRESS_SPACE_MEMORY|PCI_BASE_ADDRESS_MEM_TYPE_64)) {
70 new = 0; /* currently everyone zeros the high address */
71 pci_write_config_dword(dev, reg + 4, new);
72 pci_read_config_dword(dev, reg + 4, &check);
73 if (check != new) {
74 printk(KERN_ERR "PCI: Error updating region "
75 "%s/%d (high %08x != %08x)\n",
76 pci_name(dev), resno, new, check);
77 }
78 }
79 res->flags &= ~IORESOURCE_UNSET;
80 pr_debug("PCI: moved device %s resource %d (%lx) to %x\n",
81 pci_name(dev), resno, res->flags,
82 new & ~PCI_REGION_FLAG_MASK);
83}
84
85int __devinit
86pci_claim_resource(struct pci_dev *dev, int resource)
87{
88 struct resource *res = &dev->resource[resource];
89 struct resource *root = NULL;
90 char *dtype = resource < PCI_BRIDGE_RESOURCES ? "device" : "bridge";
91 int err;
92
93 if (res->flags & IORESOURCE_IO)
94 root = &ioport_resource;
95 if (res->flags & IORESOURCE_MEM)
96 root = &iomem_resource;
97
98 err = -EINVAL;
99 if (root != NULL)
100 err = insert_resource(root, res);
101
102 if (err) {
103 printk(KERN_ERR "PCI: %s region %d of %s %s [%lx:%lx]\n",
104 root ? "Address space collision on" :
105 "No parent found for",
106 resource, dtype, pci_name(dev), res->start, res->end);
107 }
108
109 return err;
110}
111
112int pci_assign_resource(struct pci_dev *dev, int resno)
113{
114 struct pci_bus *bus = dev->bus;
115 struct resource *res = dev->resource + resno;
116 unsigned long size, min, align;
117 int ret;
118
119 size = res->end - res->start + 1;
120 min = (res->flags & IORESOURCE_IO) ? PCIBIOS_MIN_IO : PCIBIOS_MIN_MEM;
121 /* The bridge resources are special, as their
122 size != alignment. Sizing routines return
123 required alignment in the "start" field. */
124 align = (resno < PCI_BRIDGE_RESOURCES) ? size : res->start;
125
126 /* First, try exact prefetching match.. */
127 ret = pci_bus_alloc_resource(bus, res, size, align, min,
128 IORESOURCE_PREFETCH,
129 pcibios_align_resource, dev);
130
131 if (ret < 0 && (res->flags & IORESOURCE_PREFETCH)) {
132 /*
133 * That failed.
134 *
135 * But a prefetching area can handle a non-prefetching
136 * window (it will just not perform as well).
137 */
138 ret = pci_bus_alloc_resource(bus, res, size, align, min, 0,
139 pcibios_align_resource, dev);
140 }
141
142 if (ret) {
143 printk(KERN_ERR "PCI: Failed to allocate %s resource #%d:%lx@%lx for %s\n",
144 res->flags & IORESOURCE_IO ? "I/O" : "mem",
145 resno, size, res->start, pci_name(dev));
146 } else if (resno < PCI_BRIDGE_RESOURCES) {
147 pci_update_resource(dev, res, resno);
148 }
149
150 return ret;
151}
152
153/* Sort resources by alignment */
154void __devinit
155pdev_sort_resources(struct pci_dev *dev, struct resource_list *head)
156{
157 int i;
158
159 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
160 struct resource *r;
161 struct resource_list *list, *tmp;
162 unsigned long r_align;
163
164 r = &dev->resource[i];
165 r_align = r->end - r->start;
166
167 if (!(r->flags) || r->parent)
168 continue;
169 if (!r_align) {
170 printk(KERN_WARNING "PCI: Ignore bogus resource %d "
171 "[%lx:%lx] of %s\n",
172 i, r->start, r->end, pci_name(dev));
173 continue;
174 }
175 r_align = (i < PCI_BRIDGE_RESOURCES) ? r_align + 1 : r->start;
176 for (list = head; ; list = list->next) {
177 unsigned long align = 0;
178 struct resource_list *ln = list->next;
179 int idx;
180
181 if (ln) {
182 idx = ln->res - &ln->dev->resource[0];
183 align = (idx < PCI_BRIDGE_RESOURCES) ?
184 ln->res->end - ln->res->start + 1 :
185 ln->res->start;
186 }
187 if (r_align > align) {
188 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
189 if (!tmp)
190 panic("pdev_sort_resources(): "
191 "kmalloc() failed!\n");
192 tmp->next = ln;
193 tmp->res = r;
194 tmp->dev = dev;
195 list->next = tmp;
196 break;
197 }
198 }
199 }
200}