]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/pci/bus.c
resource/PCI: align functions now return start of resource
[net-next-2.6.git] / drivers / pci / bus.c
CommitLineData
1da177e4
LT
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 */
35int
36pci_bus_alloc_resource(struct pci_bus *bus, struct resource *res,
e31dd6e4
GKH
37 resource_size_t size, resource_size_t align,
38 resource_size_t min, unsigned int type_mask,
b26b2d49
DB
39 resource_size_t (*alignf)(void *,
40 struct resource *,
41 resource_size_t,
42 resource_size_t),
e31dd6e4 43 void *alignf_data)
1da177e4
LT
44{
45 int i, ret = -ENOMEM;
1f82de10 46 resource_size_t max = -1;
1da177e4
LT
47
48 type_mask |= IORESOURCE_IO | IORESOURCE_MEM;
49
1f82de10
YL
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
1da177e4
LT
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.. */
688d1918
LT
70 ret = allocate_resource(r, res, size,
71 r->start ? : min,
1f82de10 72 max, align,
1da177e4
LT
73 alignf, alignf_data);
74 if (ret == 0)
75 break;
76 }
77 return ret;
78}
79
80/**
3fa16fdb 81 * pci_bus_add_device - add a single device
1da177e4
LT
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 */
96bde06a 87int pci_bus_add_device(struct pci_dev *dev)
1da177e4 88{
b19441af
GKH
89 int retval;
90 retval = device_add(&dev->dev);
91 if (retval)
92 return retval;
1da177e4 93
8a1bc901 94 dev->is_added = 1;
1da177e4
LT
95 pci_proc_attach_device(dev);
96 pci_create_sysfs_dev_files(dev);
b19441af 97 return 0;
1da177e4
LT
98}
99
876e501a
YZ
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 */
106int 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
1da177e4
LT
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 */
c48f1670 143void pci_bus_add_devices(const struct pci_bus *bus)
1da177e4
LT
144{
145 struct pci_dev *dev;
3fa16fdb 146 struct pci_bus *child;
b19441af 147 int retval;
1da177e4
LT
148
149 list_for_each_entry(dev, &bus->devices, bus_list) {
8a1bc901
GKH
150 /* Skip already-added devices */
151 if (dev->is_added)
1da177e4 152 continue;
b19441af
GKH
153 retval = pci_bus_add_device(dev);
154 if (retval)
155 dev_err(&dev->dev, "Error adding device, continuing\n");
1da177e4
LT
156 }
157
158 list_for_each_entry(dev, &bus->devices, bus_list) {
8a1bc901 159 BUG_ON(!dev->is_added);
1da177e4 160
3fa16fdb 161 child = dev->subordinate;
1da177e4
LT
162 /*
163 * If there is an unattached subordinate bus, attach
164 * it and then scan for unattached PCI devices.
165 */
3fa16fdb
YZ
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;
876e501a 181 retval = pci_bus_add_child(child);
3fa16fdb 182 if (retval)
876e501a 183 dev_err(&dev->dev, "Error adding bus, continuing\n");
1da177e4
LT
184 }
185}
186
187void pci_enable_bridges(struct pci_bus *bus)
188{
189 struct pci_dev *dev;
95a62965 190 int retval;
1da177e4
LT
191
192 list_for_each_entry(dev, &bus->devices, bus_list) {
193 if (dev->subordinate) {
296ccb08 194 if (!pci_is_enabled(dev)) {
9dd90caf
AC
195 retval = pci_enable_device(dev);
196 pci_set_master(dev);
197 }
1da177e4
LT
198 pci_enable_bridges(dev->subordinate);
199 }
200 }
201}
202
cecf4864
PM
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.
70298c6e
ZY
211 *
212 * We check the return of @cb each time. If it returns anything
213 * other than 0, we break out.
214 *
cecf4864 215 */
70298c6e 216void pci_walk_bus(struct pci_bus *top, int (*cb)(struct pci_dev *, void *),
cecf4864
PM
217 void *userdata)
218{
219 struct pci_dev *dev;
220 struct pci_bus *bus;
221 struct list_head *next;
70298c6e 222 int retval;
cecf4864
PM
223
224 bus = top;
d71374da 225 down_read(&pci_bus_sem);
cecf4864
PM
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);
cecf4864
PM
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;
cecf4864 243
d71374da
ZY
244 /* Run device routines with the device locked */
245 down(&dev->dev.sem);
70298c6e 246 retval = cb(dev, userdata);
d71374da 247 up(&dev->dev.sem);
70298c6e
ZY
248 if (retval)
249 break;
cecf4864 250 }
d71374da 251 up_read(&pci_bus_sem);
cecf4864 252}
cecf4864 253
1da177e4
LT
254EXPORT_SYMBOL(pci_bus_alloc_resource);
255EXPORT_SYMBOL_GPL(pci_bus_add_device);
256EXPORT_SYMBOL(pci_bus_add_devices);
257EXPORT_SYMBOL(pci_enable_bridges);