blob: a26135bb0ffd050929d52e33302b522398f24244 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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,
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -070037 resource_size_t size, resource_size_t align,
38 resource_size_t min, unsigned int type_mask,
Dominik Brodowskib26b2d42010-01-01 17:40:49 +010039 resource_size_t (*alignf)(void *,
Dominik Brodowski3b7a17f2010-01-01 17:40:50 +010040 const struct resource *,
Dominik Brodowskib26b2d42010-01-01 17:40:49 +010041 resource_size_t,
42 resource_size_t),
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -070043 void *alignf_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
45 int i, ret = -ENOMEM;
Yinghai Lu1f82de12009-04-23 20:48:32 -070046 resource_size_t max = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48 type_mask |= IORESOURCE_IO | IORESOURCE_MEM;
49
Yinghai Lu1f82de12009-04-23 20:48:32 -070050 /* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 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.. */
Linus Torvalds688d1912005-08-02 14:55:40 -070070 ret = allocate_resource(r, res, size,
71 r->start ? : min,
Yinghai Lu1f82de12009-04-23 20:48:32 -070072 max, align,
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 alignf, alignf_data);
74 if (ret == 0)
75 break;
76 }
77 return ret;
78}
79
80/**
Yu Zhao3fa16fd2008-11-22 02:41:45 +080081 * pci_bus_add_device - add a single device
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * @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 */
Sam Ravnborg96bde062007-03-26 21:53:30 -080087int pci_bus_add_device(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -070089 int retval;
90 retval = device_add(&dev->dev);
91 if (retval)
92 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Greg Kroah-Hartman8a1bc902008-02-14 14:56:56 -080094 dev->is_added = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 pci_proc_attach_device(dev);
96 pci_create_sysfs_dev_files(dev);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -070097 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
100/**
Yu Zhao876e5012008-11-22 02:42:35 +0800101 * 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
131/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 * 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 */
akpm@linux-foundation.orgc48f1672009-02-03 15:45:26 -0800143void pci_bus_add_devices(const struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 struct pci_dev *dev;
Yu Zhao3fa16fd2008-11-22 02:41:45 +0800146 struct pci_bus *child;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700147 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 list_for_each_entry(dev, &bus->devices, bus_list) {
Greg Kroah-Hartman8a1bc902008-02-14 14:56:56 -0800150 /* Skip already-added devices */
151 if (dev->is_added)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 continue;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700153 retval = pci_bus_add_device(dev);
154 if (retval)
155 dev_err(&dev->dev, "Error adding device, continuing\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 }
157
158 list_for_each_entry(dev, &bus->devices, bus_list) {
Greg Kroah-Hartman8a1bc902008-02-14 14:56:56 -0800159 BUG_ON(!dev->is_added);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Yu Zhao3fa16fd2008-11-22 02:41:45 +0800161 child = dev->subordinate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 /*
163 * If there is an unattached subordinate bus, attach
164 * it and then scan for unattached PCI devices.
165 */
Yu Zhao3fa16fd2008-11-22 02:41:45 +0800166 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);
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400174
Yu Zhao3fa16fd2008-11-22 02:41:45 +0800175 /*
176 * register the bus with sysfs as the parent is now
177 * properly registered.
178 */
179 if (child->is_added)
180 continue;
Yu Zhao876e5012008-11-22 02:42:35 +0800181 retval = pci_bus_add_child(child);
Yu Zhao3fa16fd2008-11-22 02:41:45 +0800182 if (retval)
Yu Zhao876e5012008-11-22 02:42:35 +0800183 dev_err(&dev->dev, "Error adding bus, continuing\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 }
185}
186
187void pci_enable_bridges(struct pci_bus *bus)
188{
189 struct pci_dev *dev;
Greg Kroah-Hartman95a62962005-07-28 11:37:33 -0700190 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 list_for_each_entry(dev, &bus->devices, bus_list) {
193 if (dev->subordinate) {
Yuji Shimada296ccb02009-04-03 16:41:46 +0900194 if (!pci_is_enabled(dev)) {
Alex Chiang9dd90ca2009-03-20 14:56:20 -0600195 retval = pci_enable_device(dev);
196 pci_set_master(dev);
197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 pci_enable_bridges(dev->subordinate);
199 }
200 }
201}
202
Paul Mackerrascecf4862005-08-18 14:33:01 +1000203/** 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.
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800211 *
212 * We check the return of @cb each time. If it returns anything
213 * other than 0, we break out.
214 *
Paul Mackerrascecf4862005-08-18 14:33:01 +1000215 */
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800216void pci_walk_bus(struct pci_bus *top, int (*cb)(struct pci_dev *, void *),
Paul Mackerrascecf4862005-08-18 14:33:01 +1000217 void *userdata)
218{
219 struct pci_dev *dev;
220 struct pci_bus *bus;
221 struct list_head *next;
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800222 int retval;
Paul Mackerrascecf4862005-08-18 14:33:01 +1000223
224 bus = top;
Zhang Yanmind71374d2006-06-02 12:35:43 +0800225 down_read(&pci_bus_sem);
Paul Mackerrascecf4862005-08-18 14:33:01 +1000226 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);
Paul Mackerrascecf4862005-08-18 14:33:01 +1000237 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;
Paul Mackerrascecf4862005-08-18 14:33:01 +1000243
Zhang Yanmind71374d2006-06-02 12:35:43 +0800244 /* Run device routines with the device locked */
245 down(&dev->dev.sem);
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800246 retval = cb(dev, userdata);
Zhang Yanmind71374d2006-06-02 12:35:43 +0800247 up(&dev->dev.sem);
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800248 if (retval)
249 break;
Paul Mackerrascecf4862005-08-18 14:33:01 +1000250 }
Zhang Yanmind71374d2006-06-02 12:35:43 +0800251 up_read(&pci_bus_sem);
Paul Mackerrascecf4862005-08-18 14:33:01 +1000252}
Paul Mackerrascecf4862005-08-18 14:33:01 +1000253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254EXPORT_SYMBOL(pci_bus_alloc_resource);
255EXPORT_SYMBOL_GPL(pci_bus_add_device);
256EXPORT_SYMBOL(pci_bus_add_devices);
257EXPORT_SYMBOL(pci_enable_bridges);