Merge branch 'perf-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[pandora-kernel.git] / drivers / pci / bus.c
1 /*
2  *      drivers/pci/bus.c
3  *
4  * From setup-res.c, by:
5  *      Dave Rusling (david.rusling@reo.mts.dec.com)
6  *      David Mosberger (davidm@cs.arizona.edu)
7  *      David Miller (davem@redhat.com)
8  *      Ivan Kokshaysky (ink@jurassic.park.msu.ru)
9  */
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/pci.h>
13 #include <linux/errno.h>
14 #include <linux/ioport.h>
15 #include <linux/proc_fs.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18
19 #include "pci.h"
20
21 void pci_bus_add_resource(struct pci_bus *bus, struct resource *res,
22                           unsigned int flags)
23 {
24         struct pci_bus_resource *bus_res;
25
26         bus_res = kzalloc(sizeof(struct pci_bus_resource), GFP_KERNEL);
27         if (!bus_res) {
28                 dev_err(&bus->dev, "can't add %pR resource\n", res);
29                 return;
30         }
31
32         bus_res->res = res;
33         bus_res->flags = flags;
34         list_add_tail(&bus_res->list, &bus->resources);
35 }
36
37 struct resource *pci_bus_resource_n(const struct pci_bus *bus, int n)
38 {
39         struct pci_bus_resource *bus_res;
40
41         if (n < PCI_BRIDGE_RESOURCE_NUM)
42                 return bus->resource[n];
43
44         n -= PCI_BRIDGE_RESOURCE_NUM;
45         list_for_each_entry(bus_res, &bus->resources, list) {
46                 if (n-- == 0)
47                         return bus_res->res;
48         }
49         return NULL;
50 }
51 EXPORT_SYMBOL_GPL(pci_bus_resource_n);
52
53 void pci_bus_remove_resources(struct pci_bus *bus)
54 {
55         struct pci_bus_resource *bus_res, *tmp;
56         int i;
57
58         for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
59                 bus->resource[i] = NULL;
60
61         list_for_each_entry_safe(bus_res, tmp, &bus->resources, list) {
62                 list_del(&bus_res->list);
63                 kfree(bus_res);
64         }
65 }
66
67 static bool pci_bus_resource_better(struct resource *res1, bool pos1,
68                                     struct resource *res2, bool pos2)
69 {
70         /* If exactly one is positive decode, always prefer that one */
71         if (pos1 != pos2)
72                 return pos1 ? true : false;
73
74         /* Prefer the one that contains the highest address */
75         if (res1->end != res2->end)
76                 return (res1->end > res2->end) ? true : false;
77
78         /* Otherwise, prefer the one with highest "center of gravity" */
79         if (res1->start != res2->start)
80                 return (res1->start > res2->start) ? true : false;
81
82         /* Otherwise, choose one arbitrarily (but consistently) */
83         return (res1 > res2) ? true : false;
84 }
85
86 static bool pci_bus_resource_positive(struct pci_bus *bus, struct resource *res)
87 {
88         struct pci_bus_resource *bus_res;
89
90         /*
91          * This relies on the fact that pci_bus.resource[] refers to P2P or
92          * CardBus bridge base/limit registers, which are always positively
93          * decoded.  The pci_bus.resources list contains host bridge or
94          * subtractively decoded resources.
95          */
96         list_for_each_entry(bus_res, &bus->resources, list) {
97                 if (bus_res->res == res)
98                         return (bus_res->flags & PCI_SUBTRACTIVE_DECODE) ?
99                                 false : true;
100         }
101         return true;
102 }
103
104 /*
105  * Find the next-best bus resource after the cursor "res".  If the cursor is
106  * NULL, return the best resource.  "Best" means that we prefer positive
107  * decode regions over subtractive decode, then those at higher addresses.
108  */
109 static struct resource *pci_bus_find_resource_prev(struct pci_bus *bus,
110                                                    unsigned int type,
111                                                    struct resource *res)
112 {
113         bool res_pos, r_pos, prev_pos = false;
114         struct resource *r, *prev = NULL;
115         int i;
116
117         res_pos = pci_bus_resource_positive(bus, res);
118         pci_bus_for_each_resource(bus, r, i) {
119                 if (!r)
120                         continue;
121
122                 if ((r->flags & IORESOURCE_TYPE_BITS) != type)
123                         continue;
124
125                 r_pos = pci_bus_resource_positive(bus, r);
126                 if (!res || pci_bus_resource_better(res, res_pos, r, r_pos)) {
127                         if (!prev || pci_bus_resource_better(r, r_pos,
128                                                              prev, prev_pos)) {
129                                 prev = r;
130                                 prev_pos = r_pos;
131                         }
132                 }
133         }
134
135         return prev;
136 }
137
138 /**
139  * pci_bus_alloc_resource - allocate a resource from a parent bus
140  * @bus: PCI bus
141  * @res: resource to allocate
142  * @size: size of resource to allocate
143  * @align: alignment of resource to allocate
144  * @min: minimum /proc/iomem address to allocate
145  * @type_mask: IORESOURCE_* type flags
146  * @alignf: resource alignment function
147  * @alignf_data: data argument for resource alignment function
148  *
149  * Given the PCI bus a device resides on, the size, minimum address,
150  * alignment and type, try to find an acceptable resource allocation
151  * for a specific device resource.
152  */
153 int
154 pci_bus_alloc_resource(struct pci_bus *bus, struct resource *res,
155                 resource_size_t size, resource_size_t align,
156                 resource_size_t min, unsigned int type_mask,
157                 resource_size_t (*alignf)(void *,
158                                           const struct resource *,
159                                           resource_size_t,
160                                           resource_size_t),
161                 void *alignf_data)
162 {
163         int ret = -ENOMEM;
164         struct resource *r;
165         resource_size_t max = -1;
166         unsigned int type = res->flags & IORESOURCE_TYPE_BITS;
167
168         type_mask |= IORESOURCE_IO | IORESOURCE_MEM;
169
170         /* don't allocate too high if the pref mem doesn't support 64bit*/
171         if (!(res->flags & IORESOURCE_MEM_64))
172                 max = PCIBIOS_MAX_MEM_32;
173
174         /* Look for space at highest addresses first */
175         r = pci_bus_find_resource_prev(bus, type, NULL);
176         for ( ; r; r = pci_bus_find_resource_prev(bus, type, r)) {
177                 /* type_mask must match */
178                 if ((res->flags ^ r->flags) & type_mask)
179                         continue;
180
181                 /* We cannot allocate a non-prefetching resource
182                    from a pre-fetching area */
183                 if ((r->flags & IORESOURCE_PREFETCH) &&
184                     !(res->flags & IORESOURCE_PREFETCH))
185                         continue;
186
187                 /* Ok, try it out.. */
188                 ret = allocate_resource(r, res, size,
189                                         r->start ? : min,
190                                         max, align,
191                                         alignf, alignf_data);
192                 if (ret == 0)
193                         break;
194         }
195         return ret;
196 }
197
198 /**
199  * pci_bus_add_device - add a single device
200  * @dev: device to add
201  *
202  * This adds a single pci device to the global
203  * device list and adds sysfs and procfs entries
204  */
205 int pci_bus_add_device(struct pci_dev *dev)
206 {
207         int retval;
208         retval = device_add(&dev->dev);
209         if (retval)
210                 return retval;
211
212         dev->is_added = 1;
213         pci_proc_attach_device(dev);
214         pci_create_sysfs_dev_files(dev);
215         return 0;
216 }
217
218 /**
219  * pci_bus_add_child - add a child bus
220  * @bus: bus to add
221  *
222  * This adds sysfs entries for a single bus
223  */
224 int pci_bus_add_child(struct pci_bus *bus)
225 {
226         int retval;
227
228         if (bus->bridge)
229                 bus->dev.parent = bus->bridge;
230
231         retval = device_register(&bus->dev);
232         if (retval)
233                 return retval;
234
235         bus->is_added = 1;
236
237         retval = device_create_file(&bus->dev, &dev_attr_cpuaffinity);
238         if (retval)
239                 return retval;
240
241         retval = device_create_file(&bus->dev, &dev_attr_cpulistaffinity);
242
243         /* Create legacy_io and legacy_mem files for this bus */
244         pci_create_legacy_files(bus);
245
246         return retval;
247 }
248
249 /**
250  * pci_bus_add_devices - insert newly discovered PCI devices
251  * @bus: bus to check for new devices
252  *
253  * Add newly discovered PCI devices (which are on the bus->devices
254  * list) to the global PCI device list, add the sysfs and procfs
255  * entries.  Where a bridge is found, add the discovered bus to
256  * the parents list of child buses, and recurse (breadth-first
257  * to be compatible with 2.4)
258  *
259  * Call hotplug for each new devices.
260  */
261 void pci_bus_add_devices(const struct pci_bus *bus)
262 {
263         struct pci_dev *dev;
264         struct pci_bus *child;
265         int retval;
266
267         list_for_each_entry(dev, &bus->devices, bus_list) {
268                 /* Skip already-added devices */
269                 if (dev->is_added)
270                         continue;
271                 retval = pci_bus_add_device(dev);
272                 if (retval)
273                         dev_err(&dev->dev, "Error adding device, continuing\n");
274         }
275
276         list_for_each_entry(dev, &bus->devices, bus_list) {
277                 BUG_ON(!dev->is_added);
278
279                 child = dev->subordinate;
280                 /*
281                  * If there is an unattached subordinate bus, attach
282                  * it and then scan for unattached PCI devices.
283                  */
284                 if (!child)
285                         continue;
286                 if (list_empty(&child->node)) {
287                         down_write(&pci_bus_sem);
288                         list_add_tail(&child->node, &dev->bus->children);
289                         up_write(&pci_bus_sem);
290                 }
291                 pci_bus_add_devices(child);
292
293                 /*
294                  * register the bus with sysfs as the parent is now
295                  * properly registered.
296                  */
297                 if (child->is_added)
298                         continue;
299                 retval = pci_bus_add_child(child);
300                 if (retval)
301                         dev_err(&dev->dev, "Error adding bus, continuing\n");
302         }
303 }
304
305 void pci_enable_bridges(struct pci_bus *bus)
306 {
307         struct pci_dev *dev;
308         int retval;
309
310         list_for_each_entry(dev, &bus->devices, bus_list) {
311                 if (dev->subordinate) {
312                         if (!pci_is_enabled(dev)) {
313                                 retval = pci_enable_device(dev);
314                                 if (retval)
315                                         dev_err(&dev->dev, "Error enabling bridge (%d), continuing\n", retval);
316                                 pci_set_master(dev);
317                         }
318                         pci_enable_bridges(dev->subordinate);
319                 }
320         }
321 }
322
323 /** pci_walk_bus - walk devices on/under bus, calling callback.
324  *  @top      bus whose devices should be walked
325  *  @cb       callback to be called for each device found
326  *  @userdata arbitrary pointer to be passed to callback.
327  *
328  *  Walk the given bus, including any bridged devices
329  *  on buses under this bus.  Call the provided callback
330  *  on each device found.
331  *
332  *  We check the return of @cb each time. If it returns anything
333  *  other than 0, we break out.
334  *
335  */
336 void pci_walk_bus(struct pci_bus *top, int (*cb)(struct pci_dev *, void *),
337                   void *userdata)
338 {
339         struct pci_dev *dev;
340         struct pci_bus *bus;
341         struct list_head *next;
342         int retval;
343
344         bus = top;
345         down_read(&pci_bus_sem);
346         next = top->devices.next;
347         for (;;) {
348                 if (next == &bus->devices) {
349                         /* end of this bus, go up or finish */
350                         if (bus == top)
351                                 break;
352                         next = bus->self->bus_list.next;
353                         bus = bus->self->bus;
354                         continue;
355                 }
356                 dev = list_entry(next, struct pci_dev, bus_list);
357                 if (dev->subordinate) {
358                         /* this is a pci-pci bridge, do its devices next */
359                         next = dev->subordinate->devices.next;
360                         bus = dev->subordinate;
361                 } else
362                         next = dev->bus_list.next;
363
364                 /* Run device routines with the device locked */
365                 device_lock(&dev->dev);
366                 retval = cb(dev, userdata);
367                 device_unlock(&dev->dev);
368                 if (retval)
369                         break;
370         }
371         up_read(&pci_bus_sem);
372 }
373 EXPORT_SYMBOL_GPL(pci_walk_bus);
374
375 EXPORT_SYMBOL(pci_bus_alloc_resource);
376 EXPORT_SYMBOL_GPL(pci_bus_add_device);
377 EXPORT_SYMBOL(pci_bus_add_devices);
378 EXPORT_SYMBOL(pci_enable_bridges);