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)
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>
21 * pci_bus_alloc_resource - allocate a resource from a parent 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
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.
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 *,
46 resource_size_t max = -1;
48 type_mask |= IORESOURCE_IO | IORESOURCE_MEM;
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;
54 for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
55 struct resource *r = bus->resource[i];
59 /* type_mask must match */
60 if ((res->flags ^ r->flags) & type_mask)
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))
69 /* Ok, try it out.. */
70 ret = allocate_resource(r, res, size,
81 * pci_bus_add_device - add a single device
84 * This adds a single pci device to the global
85 * device list and adds sysfs and procfs entries
87 int pci_bus_add_device(struct pci_dev *dev)
90 retval = device_add(&dev->dev);
95 pci_proc_attach_device(dev);
96 pci_create_sysfs_dev_files(dev);
101 * pci_bus_add_child - add a child bus
104 * This adds sysfs entries for a single bus
106 int pci_bus_add_child(struct pci_bus *bus)
111 bus->dev.parent = bus->bridge;
113 retval = device_register(&bus->dev);
119 retval = device_create_file(&bus->dev, &dev_attr_cpuaffinity);
123 retval = device_create_file(&bus->dev, &dev_attr_cpulistaffinity);
125 /* Create legacy_io and legacy_mem files for this bus */
126 pci_create_legacy_files(bus);
132 * pci_bus_add_devices - insert newly discovered PCI devices
133 * @bus: bus to check for new devices
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)
141 * Call hotplug for each new devices.
143 void pci_bus_add_devices(const struct pci_bus *bus)
146 struct pci_bus *child;
149 list_for_each_entry(dev, &bus->devices, bus_list) {
150 /* Skip already-added devices */
153 retval = pci_bus_add_device(dev);
155 dev_err(&dev->dev, "Error adding device, continuing\n");
158 list_for_each_entry(dev, &bus->devices, bus_list) {
159 BUG_ON(!dev->is_added);
161 child = dev->subordinate;
163 * If there is an unattached subordinate bus, attach
164 * it and then scan for unattached PCI devices.
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);
173 pci_bus_add_devices(child);
176 * register the bus with sysfs as the parent is now
177 * properly registered.
181 retval = pci_bus_add_child(child);
183 dev_err(&dev->dev, "Error adding bus, continuing\n");
187 void pci_enable_bridges(struct pci_bus *bus)
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);
198 pci_enable_bridges(dev->subordinate);
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.
208 * Walk the given bus, including any bridged devices
209 * on buses under this bus. Call the provided callback
210 * on each device found.
212 * We check the return of @cb each time. If it returns anything
213 * other than 0, we break out.
216 void pci_walk_bus(struct pci_bus *top, int (*cb)(struct pci_dev *, void *),
221 struct list_head *next;
225 down_read(&pci_bus_sem);
226 next = top->devices.next;
228 if (next == &bus->devices) {
229 /* end of this bus, go up or finish */
232 next = bus->self->bus_list.next;
233 bus = bus->self->bus;
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;
242 next = dev->bus_list.next;
244 /* Run device routines with the device locked */
246 retval = cb(dev, userdata);
251 up_read(&pci_bus_sem);
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);