Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | ||
14add80b | 29 | void pci_update_resource(struct pci_dev *dev, int resno) |
1da177e4 LT |
30 | { |
31 | struct pci_bus_region region; | |
32 | u32 new, check, mask; | |
33 | int reg; | |
613e7ed6 | 34 | enum pci_bar_type type; |
14add80b | 35 | struct resource *res = dev->resource + resno; |
1da177e4 | 36 | |
fb0f2b40 RB |
37 | /* |
38 | * Ignore resources for unimplemented BARs and unused resource slots | |
39 | * for 64 bit BARs. | |
40 | */ | |
cf7bee5a IK |
41 | if (!res->flags) |
42 | return; | |
43 | ||
fb0f2b40 RB |
44 | /* |
45 | * Ignore non-moveable resources. This might be legacy resources for | |
46 | * which no functional BAR register exists or another important | |
80ccba11 | 47 | * system resource we shouldn't move around. |
fb0f2b40 RB |
48 | */ |
49 | if (res->flags & IORESOURCE_PCI_FIXED) | |
50 | return; | |
51 | ||
1da177e4 LT |
52 | pcibios_resource_to_bus(dev, ®ion, res); |
53 | ||
096e6f67 BH |
54 | dev_dbg(&dev->dev, "BAR %d: got res %pR bus [%#llx-%#llx] " |
55 | "flags %#lx\n", resno, res, | |
6015fbef BH |
56 | (unsigned long long)region.start, |
57 | (unsigned long long)region.end, | |
80ccba11 | 58 | (unsigned long)res->flags); |
1da177e4 LT |
59 | |
60 | new = region.start | (res->flags & PCI_REGION_FLAG_MASK); | |
61 | if (res->flags & IORESOURCE_IO) | |
62 | mask = (u32)PCI_BASE_ADDRESS_IO_MASK; | |
63 | else | |
64 | mask = (u32)PCI_BASE_ADDRESS_MEM_MASK; | |
65 | ||
613e7ed6 YZ |
66 | reg = pci_resource_bar(dev, resno, &type); |
67 | if (!reg) | |
68 | return; | |
69 | if (type != pci_bar_unknown) { | |
755528c8 LT |
70 | if (!(res->flags & IORESOURCE_ROM_ENABLE)) |
71 | return; | |
72 | new |= PCI_ROM_ADDRESS_ENABLE; | |
1da177e4 LT |
73 | } |
74 | ||
75 | pci_write_config_dword(dev, reg, new); | |
76 | pci_read_config_dword(dev, reg, &check); | |
77 | ||
78 | if ((new ^ check) & mask) { | |
80ccba11 BH |
79 | dev_err(&dev->dev, "BAR %d: error updating (%#08x != %#08x)\n", |
80 | resno, new, check); | |
1da177e4 LT |
81 | } |
82 | ||
83 | if ((new & (PCI_BASE_ADDRESS_SPACE|PCI_BASE_ADDRESS_MEM_TYPE_MASK)) == | |
84 | (PCI_BASE_ADDRESS_SPACE_MEMORY|PCI_BASE_ADDRESS_MEM_TYPE_64)) { | |
cf7bee5a | 85 | new = region.start >> 16 >> 16; |
1da177e4 LT |
86 | pci_write_config_dword(dev, reg + 4, new); |
87 | pci_read_config_dword(dev, reg + 4, &check); | |
88 | if (check != new) { | |
80ccba11 BH |
89 | dev_err(&dev->dev, "BAR %d: error updating " |
90 | "(high %#08x != %#08x)\n", resno, new, check); | |
1da177e4 LT |
91 | } |
92 | } | |
93 | res->flags &= ~IORESOURCE_UNSET; | |
80ccba11 BH |
94 | dev_dbg(&dev->dev, "BAR %d: moved to bus [%#llx-%#llx] flags %#lx\n", |
95 | resno, (unsigned long long)region.start, | |
96 | (unsigned long long)region.end, res->flags); | |
1da177e4 LT |
97 | } |
98 | ||
96bde06a | 99 | int pci_claim_resource(struct pci_dev *dev, int resource) |
1da177e4 LT |
100 | { |
101 | struct resource *res = &dev->resource[resource]; | |
102 | struct resource *root = NULL; | |
103 | char *dtype = resource < PCI_BRIDGE_RESOURCES ? "device" : "bridge"; | |
104 | int err; | |
105 | ||
085ae41f | 106 | root = pcibios_select_root(dev, res); |
1da177e4 LT |
107 | |
108 | err = -EINVAL; | |
109 | if (root != NULL) | |
110 | err = insert_resource(root, res); | |
111 | ||
112 | if (err) { | |
096e6f67 | 113 | dev_err(&dev->dev, "BAR %d: %s of %s %pR\n", |
80ccba11 BH |
114 | resource, |
115 | root ? "address space collision on" : | |
116 | "no parent found for", | |
096e6f67 | 117 | dtype, res); |
1da177e4 LT |
118 | } |
119 | ||
120 | return err; | |
121 | } | |
122 | ||
123 | int pci_assign_resource(struct pci_dev *dev, int resno) | |
124 | { | |
125 | struct pci_bus *bus = dev->bus; | |
126 | struct resource *res = dev->resource + resno; | |
e31dd6e4 | 127 | resource_size_t size, min, align; |
1da177e4 LT |
128 | int ret; |
129 | ||
022edd86 | 130 | size = resource_size(res); |
1da177e4 | 131 | min = (res->flags & IORESOURCE_IO) ? PCIBIOS_MIN_IO : PCIBIOS_MIN_MEM; |
88452565 IK |
132 | |
133 | align = resource_alignment(res); | |
134 | if (!align) { | |
104bafcf | 135 | dev_info(&dev->dev, "BAR %d: can't allocate resource (bogus " |
096e6f67 BH |
136 | "alignment) %pR flags %#lx\n", |
137 | resno, res, res->flags); | |
88452565 IK |
138 | return -EINVAL; |
139 | } | |
1da177e4 LT |
140 | |
141 | /* First, try exact prefetching match.. */ | |
142 | ret = pci_bus_alloc_resource(bus, res, size, align, min, | |
143 | IORESOURCE_PREFETCH, | |
144 | pcibios_align_resource, dev); | |
145 | ||
146 | if (ret < 0 && (res->flags & IORESOURCE_PREFETCH)) { | |
147 | /* | |
148 | * That failed. | |
149 | * | |
150 | * But a prefetching area can handle a non-prefetching | |
151 | * window (it will just not perform as well). | |
152 | */ | |
153 | ret = pci_bus_alloc_resource(bus, res, size, align, min, 0, | |
154 | pcibios_align_resource, dev); | |
155 | } | |
156 | ||
157 | if (ret) { | |
104bafcf | 158 | dev_info(&dev->dev, "BAR %d: can't allocate %s resource %pR\n", |
096e6f67 | 159 | resno, res->flags & IORESOURCE_IO ? "I/O" : "mem", res); |
88452565 IK |
160 | } else { |
161 | res->flags &= ~IORESOURCE_STARTALIGN; | |
162 | if (resno < PCI_BRIDGE_RESOURCES) | |
14add80b | 163 | pci_update_resource(dev, resno); |
1da177e4 LT |
164 | } |
165 | ||
166 | return ret; | |
167 | } | |
168 | ||
2baad5f9 | 169 | #if 0 |
75acfeca KG |
170 | int pci_assign_resource_fixed(struct pci_dev *dev, int resno) |
171 | { | |
172 | struct pci_bus *bus = dev->bus; | |
173 | struct resource *res = dev->resource + resno; | |
174 | unsigned int type_mask; | |
175 | int i, ret = -EBUSY; | |
176 | ||
177 | type_mask = IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH; | |
178 | ||
179 | for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) { | |
180 | struct resource *r = bus->resource[i]; | |
181 | if (!r) | |
182 | continue; | |
183 | ||
184 | /* type_mask must match */ | |
185 | if ((res->flags ^ r->flags) & type_mask) | |
186 | continue; | |
187 | ||
188 | ret = request_resource(r, res); | |
189 | ||
190 | if (ret == 0) | |
191 | break; | |
192 | } | |
193 | ||
194 | if (ret) { | |
096e6f67 BH |
195 | dev_err(&dev->dev, "BAR %d: can't allocate %s resource %pR\n", |
196 | resno, res->flags & IORESOURCE_IO ? "I/O" : "mem", res); | |
75acfeca | 197 | } else if (resno < PCI_BRIDGE_RESOURCES) { |
14add80b | 198 | pci_update_resource(dev, resno); |
75acfeca KG |
199 | } |
200 | ||
201 | return ret; | |
202 | } | |
203 | EXPORT_SYMBOL_GPL(pci_assign_resource_fixed); | |
204 | #endif | |
205 | ||
1da177e4 | 206 | /* Sort resources by alignment */ |
96bde06a | 207 | void pdev_sort_resources(struct pci_dev *dev, struct resource_list *head) |
1da177e4 LT |
208 | { |
209 | int i; | |
210 | ||
211 | for (i = 0; i < PCI_NUM_RESOURCES; i++) { | |
212 | struct resource *r; | |
213 | struct resource_list *list, *tmp; | |
e31dd6e4 | 214 | resource_size_t r_align; |
1da177e4 LT |
215 | |
216 | r = &dev->resource[i]; | |
fb0f2b40 RB |
217 | |
218 | if (r->flags & IORESOURCE_PCI_FIXED) | |
219 | continue; | |
220 | ||
1da177e4 LT |
221 | if (!(r->flags) || r->parent) |
222 | continue; | |
88452565 IK |
223 | |
224 | r_align = resource_alignment(r); | |
1da177e4 | 225 | if (!r_align) { |
80ccba11 | 226 | dev_warn(&dev->dev, "BAR %d: bogus alignment " |
096e6f67 BH |
227 | "%pR flags %#lx\n", |
228 | i, r, r->flags); | |
1da177e4 LT |
229 | continue; |
230 | } | |
1da177e4 | 231 | for (list = head; ; list = list->next) { |
e31dd6e4 | 232 | resource_size_t align = 0; |
1da177e4 | 233 | struct resource_list *ln = list->next; |
88452565 IK |
234 | |
235 | if (ln) | |
236 | align = resource_alignment(ln->res); | |
237 | ||
1da177e4 LT |
238 | if (r_align > align) { |
239 | tmp = kmalloc(sizeof(*tmp), GFP_KERNEL); | |
240 | if (!tmp) | |
241 | panic("pdev_sort_resources(): " | |
242 | "kmalloc() failed!\n"); | |
243 | tmp->next = ln; | |
244 | tmp->res = r; | |
245 | tmp->dev = dev; | |
246 | list->next = tmp; | |
247 | break; | |
248 | } | |
249 | } | |
250 | } | |
251 | } | |
842de40d BH |
252 | |
253 | int pci_enable_resources(struct pci_dev *dev, int mask) | |
254 | { | |
255 | u16 cmd, old_cmd; | |
256 | int i; | |
257 | struct resource *r; | |
258 | ||
259 | pci_read_config_word(dev, PCI_COMMAND, &cmd); | |
260 | old_cmd = cmd; | |
261 | ||
262 | for (i = 0; i < PCI_NUM_RESOURCES; i++) { | |
263 | if (!(mask & (1 << i))) | |
264 | continue; | |
265 | ||
266 | r = &dev->resource[i]; | |
267 | ||
268 | if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM))) | |
269 | continue; | |
270 | if ((i == PCI_ROM_RESOURCE) && | |
271 | (!(r->flags & IORESOURCE_ROM_ENABLE))) | |
272 | continue; | |
273 | ||
274 | if (!r->parent) { | |
275 | dev_err(&dev->dev, "device not available because of " | |
096e6f67 | 276 | "BAR %d %pR collisions\n", i, r); |
842de40d BH |
277 | return -EINVAL; |
278 | } | |
279 | ||
280 | if (r->flags & IORESOURCE_IO) | |
281 | cmd |= PCI_COMMAND_IO; | |
282 | if (r->flags & IORESOURCE_MEM) | |
283 | cmd |= PCI_COMMAND_MEMORY; | |
284 | } | |
285 | ||
286 | if (cmd != old_cmd) { | |
287 | dev_info(&dev->dev, "enabling device (%04x -> %04x)\n", | |
288 | old_cmd, cmd); | |
289 | pci_write_config_word(dev, PCI_COMMAND, cmd); | |
290 | } | |
291 | return 0; | |
292 | } |