]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * drivers/pci/setup-res.c | |
3 | * | |
4 | * Extruded from code written by | |
5 | * Dave Rusling ([email protected]) | |
6 | * David Mosberger ([email protected]) | |
7 | * David Miller ([email protected]) | |
8 | * | |
9 | * Support routines for initializing a PCI subsystem. | |
10 | */ | |
11 | ||
12 | /* fixed for multiple pci buses, 1999 Andrea Arcangeli <[email protected]> */ | |
13 | ||
14 | /* | |
15 | * Nov 2000, Ivan Kokshaysky <[email protected]> | |
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 | ||
064b53db | 29 | void |
1da177e4 LT |
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; | |
35 | ||
fb0f2b40 RB |
36 | /* |
37 | * Ignore resources for unimplemented BARs and unused resource slots | |
38 | * for 64 bit BARs. | |
39 | */ | |
cf7bee5a IK |
40 | if (!res->flags) |
41 | return; | |
42 | ||
fb0f2b40 RB |
43 | /* |
44 | * Ignore non-moveable resources. This might be legacy resources for | |
45 | * which no functional BAR register exists or another important | |
46 | * system resource we should better not move around in system address | |
47 | * space. | |
48 | */ | |
49 | if (res->flags & IORESOURCE_PCI_FIXED) | |
50 | return; | |
51 | ||
1da177e4 LT |
52 | pcibios_resource_to_bus(dev, ®ion, res); |
53 | ||
1396a8c3 GKH |
54 | pr_debug(" got res [%llx:%llx] bus [%lx:%lx] flags %lx for " |
55 | "BAR %d of %s\n", (unsigned long long)res->start, | |
56 | (unsigned long long)res->end, | |
1da177e4 LT |
57 | region.start, region.end, res->flags, resno, pci_name(dev)); |
58 | ||
59 | new = region.start | (res->flags & PCI_REGION_FLAG_MASK); | |
60 | if (res->flags & IORESOURCE_IO) | |
61 | mask = (u32)PCI_BASE_ADDRESS_IO_MASK; | |
62 | else | |
63 | mask = (u32)PCI_BASE_ADDRESS_MEM_MASK; | |
64 | ||
65 | if (resno < 6) { | |
66 | reg = PCI_BASE_ADDRESS_0 + 4 * resno; | |
67 | } else if (resno == PCI_ROM_RESOURCE) { | |
755528c8 LT |
68 | if (!(res->flags & IORESOURCE_ROM_ENABLE)) |
69 | return; | |
70 | new |= PCI_ROM_ADDRESS_ENABLE; | |
1da177e4 LT |
71 | reg = dev->rom_base_reg; |
72 | } else { | |
73 | /* Hmm, non-standard resource. */ | |
74 | ||
75 | return; /* kill uninitialised var warning */ | |
76 | } | |
77 | ||
78 | pci_write_config_dword(dev, reg, new); | |
79 | pci_read_config_dword(dev, reg, &check); | |
80 | ||
81 | if ((new ^ check) & mask) { | |
82 | printk(KERN_ERR "PCI: Error while updating region " | |
83 | "%s/%d (%08x != %08x)\n", pci_name(dev), resno, | |
84 | new, check); | |
85 | } | |
86 | ||
87 | if ((new & (PCI_BASE_ADDRESS_SPACE|PCI_BASE_ADDRESS_MEM_TYPE_MASK)) == | |
88 | (PCI_BASE_ADDRESS_SPACE_MEMORY|PCI_BASE_ADDRESS_MEM_TYPE_64)) { | |
cf7bee5a | 89 | new = region.start >> 16 >> 16; |
1da177e4 LT |
90 | pci_write_config_dword(dev, reg + 4, new); |
91 | pci_read_config_dword(dev, reg + 4, &check); | |
92 | if (check != new) { | |
93 | printk(KERN_ERR "PCI: Error updating region " | |
94 | "%s/%d (high %08x != %08x)\n", | |
95 | pci_name(dev), resno, new, check); | |
96 | } | |
97 | } | |
98 | res->flags &= ~IORESOURCE_UNSET; | |
99 | pr_debug("PCI: moved device %s resource %d (%lx) to %x\n", | |
100 | pci_name(dev), resno, res->flags, | |
101 | new & ~PCI_REGION_FLAG_MASK); | |
102 | } | |
103 | ||
104 | int __devinit | |
105 | pci_claim_resource(struct pci_dev *dev, int resource) | |
106 | { | |
107 | struct resource *res = &dev->resource[resource]; | |
108 | struct resource *root = NULL; | |
109 | char *dtype = resource < PCI_BRIDGE_RESOURCES ? "device" : "bridge"; | |
110 | int err; | |
111 | ||
085ae41f | 112 | root = pcibios_select_root(dev, res); |
1da177e4 LT |
113 | |
114 | err = -EINVAL; | |
115 | if (root != NULL) | |
116 | err = insert_resource(root, res); | |
117 | ||
118 | if (err) { | |
1396a8c3 GKH |
119 | printk(KERN_ERR "PCI: %s region %d of %s %s [%llx:%llx]\n", |
120 | root ? "Address space collision on" : | |
121 | "No parent found for", | |
122 | resource, dtype, pci_name(dev), | |
123 | (unsigned long long)res->start, | |
124 | (unsigned long long)res->end); | |
1da177e4 LT |
125 | } |
126 | ||
127 | return err; | |
128 | } | |
dd8c4996 | 129 | EXPORT_SYMBOL_GPL(pci_claim_resource); |
1da177e4 LT |
130 | |
131 | int pci_assign_resource(struct pci_dev *dev, int resno) | |
132 | { | |
133 | struct pci_bus *bus = dev->bus; | |
134 | struct resource *res = dev->resource + resno; | |
e31dd6e4 | 135 | resource_size_t size, min, align; |
1da177e4 LT |
136 | int ret; |
137 | ||
138 | size = res->end - res->start + 1; | |
139 | min = (res->flags & IORESOURCE_IO) ? PCIBIOS_MIN_IO : PCIBIOS_MIN_MEM; | |
140 | /* The bridge resources are special, as their | |
141 | size != alignment. Sizing routines return | |
142 | required alignment in the "start" field. */ | |
143 | align = (resno < PCI_BRIDGE_RESOURCES) ? size : res->start; | |
144 | ||
145 | /* First, try exact prefetching match.. */ | |
146 | ret = pci_bus_alloc_resource(bus, res, size, align, min, | |
147 | IORESOURCE_PREFETCH, | |
148 | pcibios_align_resource, dev); | |
149 | ||
150 | if (ret < 0 && (res->flags & IORESOURCE_PREFETCH)) { | |
151 | /* | |
152 | * That failed. | |
153 | * | |
154 | * But a prefetching area can handle a non-prefetching | |
155 | * window (it will just not perform as well). | |
156 | */ | |
157 | ret = pci_bus_alloc_resource(bus, res, size, align, min, 0, | |
158 | pcibios_align_resource, dev); | |
159 | } | |
160 | ||
161 | if (ret) { | |
1396a8c3 GKH |
162 | printk(KERN_ERR "PCI: Failed to allocate %s resource " |
163 | "#%d:%llx@%llx for %s\n", | |
164 | res->flags & IORESOURCE_IO ? "I/O" : "mem", | |
165 | resno, (unsigned long long)size, | |
166 | (unsigned long long)res->start, pci_name(dev)); | |
1da177e4 LT |
167 | } else if (resno < PCI_BRIDGE_RESOURCES) { |
168 | pci_update_resource(dev, res, resno); | |
169 | } | |
170 | ||
171 | return ret; | |
172 | } | |
173 | ||
75acfeca KG |
174 | #ifdef CONFIG_EMBEDDED |
175 | int pci_assign_resource_fixed(struct pci_dev *dev, int resno) | |
176 | { | |
177 | struct pci_bus *bus = dev->bus; | |
178 | struct resource *res = dev->resource + resno; | |
179 | unsigned int type_mask; | |
180 | int i, ret = -EBUSY; | |
181 | ||
182 | type_mask = IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH; | |
183 | ||
184 | for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) { | |
185 | struct resource *r = bus->resource[i]; | |
186 | if (!r) | |
187 | continue; | |
188 | ||
189 | /* type_mask must match */ | |
190 | if ((res->flags ^ r->flags) & type_mask) | |
191 | continue; | |
192 | ||
193 | ret = request_resource(r, res); | |
194 | ||
195 | if (ret == 0) | |
196 | break; | |
197 | } | |
198 | ||
199 | if (ret) { | |
200 | printk(KERN_ERR "PCI: Failed to allocate %s resource " | |
201 | "#%d:%llx@%llx for %s\n", | |
202 | res->flags & IORESOURCE_IO ? "I/O" : "mem", | |
203 | resno, (unsigned long long)(res->end - res->start + 1), | |
204 | (unsigned long long)res->start, pci_name(dev)); | |
205 | } else if (resno < PCI_BRIDGE_RESOURCES) { | |
206 | pci_update_resource(dev, res, resno); | |
207 | } | |
208 | ||
209 | return ret; | |
210 | } | |
211 | EXPORT_SYMBOL_GPL(pci_assign_resource_fixed); | |
212 | #endif | |
213 | ||
1da177e4 LT |
214 | /* Sort resources by alignment */ |
215 | void __devinit | |
216 | pdev_sort_resources(struct pci_dev *dev, struct resource_list *head) | |
217 | { | |
218 | int i; | |
219 | ||
220 | for (i = 0; i < PCI_NUM_RESOURCES; i++) { | |
221 | struct resource *r; | |
222 | struct resource_list *list, *tmp; | |
e31dd6e4 | 223 | resource_size_t r_align; |
1da177e4 LT |
224 | |
225 | r = &dev->resource[i]; | |
fb0f2b40 RB |
226 | |
227 | if (r->flags & IORESOURCE_PCI_FIXED) | |
228 | continue; | |
229 | ||
1da177e4 LT |
230 | r_align = r->end - r->start; |
231 | ||
232 | if (!(r->flags) || r->parent) | |
233 | continue; | |
234 | if (!r_align) { | |
235 | printk(KERN_WARNING "PCI: Ignore bogus resource %d " | |
1396a8c3 GKH |
236 | "[%llx:%llx] of %s\n", |
237 | i, (unsigned long long)r->start, | |
238 | (unsigned long long)r->end, pci_name(dev)); | |
1da177e4 LT |
239 | continue; |
240 | } | |
241 | r_align = (i < PCI_BRIDGE_RESOURCES) ? r_align + 1 : r->start; | |
242 | for (list = head; ; list = list->next) { | |
e31dd6e4 | 243 | resource_size_t align = 0; |
1da177e4 LT |
244 | struct resource_list *ln = list->next; |
245 | int idx; | |
246 | ||
247 | if (ln) { | |
248 | idx = ln->res - &ln->dev->resource[0]; | |
249 | align = (idx < PCI_BRIDGE_RESOURCES) ? | |
250 | ln->res->end - ln->res->start + 1 : | |
251 | ln->res->start; | |
252 | } | |
253 | if (r_align > align) { | |
254 | tmp = kmalloc(sizeof(*tmp), GFP_KERNEL); | |
255 | if (!tmp) | |
256 | panic("pdev_sort_resources(): " | |
257 | "kmalloc() failed!\n"); | |
258 | tmp->next = ln; | |
259 | tmp->res = r; | |
260 | tmp->dev = dev; | |
261 | list->next = tmp; | |
262 | break; | |
263 | } | |
264 | } | |
265 | } | |
266 | } |