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>
17 #include <linux/slab.h>
21 void pci_bus_add_resource(struct pci_bus
*bus
, struct resource
*res
,
24 struct pci_bus_resource
*bus_res
;
26 bus_res
= kzalloc(sizeof(struct pci_bus_resource
), GFP_KERNEL
);
28 dev_err(&bus
->dev
, "can't add %pR resource\n", res
);
33 bus_res
->flags
= flags
;
34 list_add_tail(&bus_res
->list
, &bus
->resources
);
37 struct resource
*pci_bus_resource_n(const struct pci_bus
*bus
, int n
)
39 struct pci_bus_resource
*bus_res
;
41 if (n
< PCI_BRIDGE_RESOURCE_NUM
)
42 return bus
->resource
[n
];
44 n
-= PCI_BRIDGE_RESOURCE_NUM
;
45 list_for_each_entry(bus_res
, &bus
->resources
, list
) {
51 EXPORT_SYMBOL_GPL(pci_bus_resource_n
);
53 void pci_bus_remove_resources(struct pci_bus
*bus
)
55 struct pci_bus_resource
*bus_res
, *tmp
;
58 for (i
= 0; i
< PCI_BRIDGE_RESOURCE_NUM
; i
++)
59 bus
->resource
[i
] = NULL
;
61 list_for_each_entry_safe(bus_res
, tmp
, &bus
->resources
, list
) {
62 list_del(&bus_res
->list
);
67 static bool pci_bus_resource_better(struct resource
*res1
, bool pos1
,
68 struct resource
*res2
, bool pos2
)
70 /* If exactly one is positive decode, always prefer that one */
72 return pos1
? true : false;
74 /* Prefer the one that contains the highest address */
75 if (res1
->end
!= res2
->end
)
76 return (res1
->end
> res2
->end
) ? true : false;
78 /* Otherwise, prefer the one with highest "center of gravity" */
79 if (res1
->start
!= res2
->start
)
80 return (res1
->start
> res2
->start
) ? true : false;
82 /* Otherwise, choose one arbitrarily (but consistently) */
83 return (res1
> res2
) ? true : false;
86 static bool pci_bus_resource_positive(struct pci_bus
*bus
, struct resource
*res
)
88 struct pci_bus_resource
*bus_res
;
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.
96 list_for_each_entry(bus_res
, &bus
->resources
, list
) {
97 if (bus_res
->res
== res
)
98 return (bus_res
->flags
& PCI_SUBTRACTIVE_DECODE
) ?
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.
109 static struct resource
*pci_bus_find_resource_prev(struct pci_bus
*bus
,
111 struct resource
*res
)
113 bool res_pos
, r_pos
, prev_pos
= false;
114 struct resource
*r
, *prev
= NULL
;
117 res_pos
= pci_bus_resource_positive(bus
, res
);
118 pci_bus_for_each_resource(bus
, r
, i
) {
122 if ((r
->flags
& IORESOURCE_TYPE_BITS
) != type
)
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
,
139 * pci_bus_alloc_resource - allocate a resource from a parent 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
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.
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
*,
165 resource_size_t max
= -1;
166 unsigned int type
= res
->flags
& IORESOURCE_TYPE_BITS
;
168 type_mask
|= IORESOURCE_IO
| IORESOURCE_MEM
;
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
;
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
)
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
))
187 /* Ok, try it out.. */
188 ret
= allocate_resource(r
, res
, size
,
191 alignf
, alignf_data
);
199 * pci_bus_add_device - add a single device
200 * @dev: device to add
202 * This adds a single pci device to the global
203 * device list and adds sysfs and procfs entries
205 int pci_bus_add_device(struct pci_dev
*dev
)
208 retval
= device_add(&dev
->dev
);
213 pci_proc_attach_device(dev
);
214 pci_create_sysfs_dev_files(dev
);
219 * pci_bus_add_child - add a child bus
222 * This adds sysfs entries for a single bus
224 int pci_bus_add_child(struct pci_bus
*bus
)
229 bus
->dev
.parent
= bus
->bridge
;
231 retval
= device_register(&bus
->dev
);
237 retval
= device_create_file(&bus
->dev
, &dev_attr_cpuaffinity
);
241 retval
= device_create_file(&bus
->dev
, &dev_attr_cpulistaffinity
);
243 /* Create legacy_io and legacy_mem files for this bus */
244 pci_create_legacy_files(bus
);
250 * pci_bus_add_devices - insert newly discovered PCI devices
251 * @bus: bus to check for new devices
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)
259 * Call hotplug for each new devices.
261 void pci_bus_add_devices(const struct pci_bus
*bus
)
264 struct pci_bus
*child
;
267 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
268 /* Skip already-added devices */
271 retval
= pci_bus_add_device(dev
);
273 dev_err(&dev
->dev
, "Error adding device, continuing\n");
276 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
277 BUG_ON(!dev
->is_added
);
279 child
= dev
->subordinate
;
281 * If there is an unattached subordinate bus, attach
282 * it and then scan for unattached PCI devices.
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
);
291 pci_bus_add_devices(child
);
294 * register the bus with sysfs as the parent is now
295 * properly registered.
299 retval
= pci_bus_add_child(child
);
301 dev_err(&dev
->dev
, "Error adding bus, continuing\n");
305 void pci_enable_bridges(struct pci_bus
*bus
)
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
);
315 dev_err(&dev
->dev
, "Error enabling bridge (%d), continuing\n", retval
);
318 pci_enable_bridges(dev
->subordinate
);
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.
328 * Walk the given bus, including any bridged devices
329 * on buses under this bus. Call the provided callback
330 * on each device found.
332 * We check the return of @cb each time. If it returns anything
333 * other than 0, we break out.
336 void pci_walk_bus(struct pci_bus
*top
, int (*cb
)(struct pci_dev
*, void *),
341 struct list_head
*next
;
345 down_read(&pci_bus_sem
);
346 next
= top
->devices
.next
;
348 if (next
== &bus
->devices
) {
349 /* end of this bus, go up or finish */
352 next
= bus
->self
->bus_list
.next
;
353 bus
= bus
->self
->bus
;
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
;
362 next
= dev
->bus_list
.next
;
364 /* Run device routines with the device locked */
365 device_lock(&dev
->dev
);
366 retval
= cb(dev
, userdata
);
367 device_unlock(&dev
->dev
);
371 up_read(&pci_bus_sem
);
373 EXPORT_SYMBOL_GPL(pci_walk_bus
);
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
);
This page took 0.03858 seconds and 6 git commands to generate.