]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/pci/bus.c
d29d69a37940a5a72393dbe1cfc239cfc905fe50
[net-next-2.6.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
18 #include "pci.h"
19
20 /**
21  * pci_bus_alloc_resource - allocate a resource from a parent bus
22  * @bus: PCI bus
23  * @res: resource to allocate
24  * @size: size of resource to allocate
25  * @align: alignment of resource to allocate
26  * @min: minimum /proc/iomem address to allocate
27  * @type_mask: IORESOURCE_* type flags
28  * @alignf: resource alignment function
29  * @alignf_data: data argument for resource alignment function
30  *
31  * Given the PCI bus a device resides on, the size, minimum address,
32  * alignment and type, try to find an acceptable resource allocation
33  * for a specific device resource.
34  */
35 int
36 pci_bus_alloc_resource(struct pci_bus *bus, struct resource *res,
37                 resource_size_t size, resource_size_t align,
38                 resource_size_t min, unsigned int type_mask,
39                 resource_size_t (*alignf)(void *,
40                                           struct resource *,
41                                           resource_size_t,
42                                           resource_size_t),
43                 void *alignf_data)
44 {
45         int i, ret = -ENOMEM;
46         resource_size_t max = -1;
47
48         type_mask |= IORESOURCE_IO | IORESOURCE_MEM;
49
50         /* don't allocate too high if the pref mem doesn't support 64bit*/
51         if (!(res->flags & IORESOURCE_MEM_64))
52                 max = PCIBIOS_MAX_MEM_32;
53
54         for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
55                 struct resource *r = bus->resource[i];
56                 if (!r)
57                         continue;
58
59                 /* type_mask must match */
60                 if ((res->flags ^ r->flags) & type_mask)
61                         continue;
62
63                 /* We cannot allocate a non-prefetching resource
64                    from a pre-fetching area */
65                 if ((r->flags & IORESOURCE_PREFETCH) &&
66                     !(res->flags & IORESOURCE_PREFETCH))
67                         continue;
68
69                 /* Ok, try it out.. */
70                 ret = allocate_resource(r, res, size,
71                                         r->start ? : min,
72                                         max, align,
73                                         alignf, alignf_data);
74                 if (ret == 0)
75                         break;
76         }
77         return ret;
78 }
79
80 /**
81  * pci_bus_add_device - add a single device
82  * @dev: device to add
83  *
84  * This adds a single pci device to the global
85  * device list and adds sysfs and procfs entries
86  */
87 int pci_bus_add_device(struct pci_dev *dev)
88 {
89         int retval;
90         retval = device_add(&dev->dev);
91         if (retval)
92                 return retval;
93
94         dev->is_added = 1;
95         pci_proc_attach_device(dev);
96         pci_create_sysfs_dev_files(dev);
97         return 0;
98 }
99
100 /**
101  * pci_bus_add_child - add a child bus
102  * @bus: bus to add
103  *
104  * This adds sysfs entries for a single bus
105  */
106 int pci_bus_add_child(struct pci_bus *bus)
107 {
108         int retval;
109
110         if (bus->bridge)
111                 bus->dev.parent = bus->bridge;
112
113         retval = device_register(&bus->dev);
114         if (retval)
115                 return retval;
116
117         bus->is_added = 1;
118
119         retval = device_create_file(&bus->dev, &dev_attr_cpuaffinity);
120         if (retval)
121                 return retval;
122
123         retval = device_create_file(&bus->dev, &dev_attr_cpulistaffinity);
124
125         /* Create legacy_io and legacy_mem files for this bus */
126         pci_create_legacy_files(bus);
127
128         return retval;
129 }
130
131 /**
132  * pci_bus_add_devices - insert newly discovered PCI devices
133  * @bus: bus to check for new devices
134  *
135  * Add newly discovered PCI devices (which are on the bus->devices
136  * list) to the global PCI device list, add the sysfs and procfs
137  * entries.  Where a bridge is found, add the discovered bus to
138  * the parents list of child buses, and recurse (breadth-first
139  * to be compatible with 2.4)
140  *
141  * Call hotplug for each new devices.
142  */
143 void pci_bus_add_devices(const struct pci_bus *bus)
144 {
145         struct pci_dev *dev;
146         struct pci_bus *child;
147         int retval;
148
149         list_for_each_entry(dev, &bus->devices, bus_list) {
150                 /* Skip already-added devices */
151                 if (dev->is_added)
152                         continue;
153                 retval = pci_bus_add_device(dev);
154                 if (retval)
155                         dev_err(&dev->dev, "Error adding device, continuing\n");
156         }
157
158         list_for_each_entry(dev, &bus->devices, bus_list) {
159                 BUG_ON(!dev->is_added);
160
161                 child = dev->subordinate;
162                 /*
163                  * If there is an unattached subordinate bus, attach
164                  * it and then scan for unattached PCI devices.
165                  */
166                 if (!child)
167                         continue;
168                 if (list_empty(&child->node)) {
169                         down_write(&pci_bus_sem);
170                         list_add_tail(&child->node, &dev->bus->children);
171                         up_write(&pci_bus_sem);
172                 }
173                 pci_bus_add_devices(child);
174
175                 /*
176                  * register the bus with sysfs as the parent is now
177                  * properly registered.
178                  */
179                 if (child->is_added)
180                         continue;
181                 retval = pci_bus_add_child(child);
182                 if (retval)
183                         dev_err(&dev->dev, "Error adding bus, continuing\n");
184         }
185 }
186
187 void pci_enable_bridges(struct pci_bus *bus)
188 {
189         struct pci_dev *dev;
190         int retval;
191
192         list_for_each_entry(dev, &bus->devices, bus_list) {
193                 if (dev->subordinate) {
194                         if (!pci_is_enabled(dev)) {
195                                 retval = pci_enable_device(dev);
196                                 pci_set_master(dev);
197                         }
198                         pci_enable_bridges(dev->subordinate);
199                 }
200         }
201 }
202
203 /** pci_walk_bus - walk devices on/under bus, calling callback.
204  *  @top      bus whose devices should be walked
205  *  @cb       callback to be called for each device found
206  *  @userdata arbitrary pointer to be passed to callback.
207  *
208  *  Walk the given bus, including any bridged devices
209  *  on buses under this bus.  Call the provided callback
210  *  on each device found.
211  *
212  *  We check the return of @cb each time. If it returns anything
213  *  other than 0, we break out.
214  *
215  */
216 void pci_walk_bus(struct pci_bus *top, int (*cb)(struct pci_dev *, void *),
217                   void *userdata)
218 {
219         struct pci_dev *dev;
220         struct pci_bus *bus;
221         struct list_head *next;
222         int retval;
223
224         bus = top;
225         down_read(&pci_bus_sem);
226         next = top->devices.next;
227         for (;;) {
228                 if (next == &bus->devices) {
229                         /* end of this bus, go up or finish */
230                         if (bus == top)
231                                 break;
232                         next = bus->self->bus_list.next;
233                         bus = bus->self->bus;
234                         continue;
235                 }
236                 dev = list_entry(next, struct pci_dev, bus_list);
237                 if (dev->subordinate) {
238                         /* this is a pci-pci bridge, do its devices next */
239                         next = dev->subordinate->devices.next;
240                         bus = dev->subordinate;
241                 } else
242                         next = dev->bus_list.next;
243
244                 /* Run device routines with the device locked */
245                 down(&dev->dev.sem);
246                 retval = cb(dev, userdata);
247                 up(&dev->dev.sem);
248                 if (retval)
249                         break;
250         }
251         up_read(&pci_bus_sem);
252 }
253
254 EXPORT_SYMBOL(pci_bus_alloc_resource);
255 EXPORT_SYMBOL_GPL(pci_bus_add_device);
256 EXPORT_SYMBOL(pci_bus_add_devices);
257 EXPORT_SYMBOL(pci_enable_bridges);