Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
Greg Kroah-Hartman | e31dd6e | 2006-06-12 17:06:02 -0700 | [diff] [blame] | 37 | resource_size_t size, resource_size_t align, |
| 38 | resource_size_t min, unsigned int type_mask, |
Dominik Brodowski | b26b2d4 | 2010-01-01 17:40:49 +0100 | [diff] [blame] | 39 | resource_size_t (*alignf)(void *, |
Dominik Brodowski | 3b7a17f | 2010-01-01 17:40:50 +0100 | [diff] [blame] | 40 | const struct resource *, |
Dominik Brodowski | b26b2d4 | 2010-01-01 17:40:49 +0100 | [diff] [blame] | 41 | resource_size_t, |
| 42 | resource_size_t), |
Greg Kroah-Hartman | e31dd6e | 2006-06-12 17:06:02 -0700 | [diff] [blame] | 43 | void *alignf_data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | { |
| 45 | int i, ret = -ENOMEM; |
Bjorn Helgaas | 89a74ec | 2010-02-23 10:24:31 -0700 | [diff] [blame^] | 46 | struct resource *r; |
Yinghai Lu | 1f82de1 | 2009-04-23 20:48:32 -0700 | [diff] [blame] | 47 | resource_size_t max = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
| 49 | type_mask |= IORESOURCE_IO | IORESOURCE_MEM; |
| 50 | |
Yinghai Lu | 1f82de1 | 2009-04-23 20:48:32 -0700 | [diff] [blame] | 51 | /* don't allocate too high if the pref mem doesn't support 64bit*/ |
| 52 | if (!(res->flags & IORESOURCE_MEM_64)) |
| 53 | max = PCIBIOS_MAX_MEM_32; |
| 54 | |
Bjorn Helgaas | 89a74ec | 2010-02-23 10:24:31 -0700 | [diff] [blame^] | 55 | pci_bus_for_each_resource(bus, r, i) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 Torvalds | 688d1918 | 2005-08-02 14:55:40 -0700 | [diff] [blame] | 70 | ret = allocate_resource(r, res, size, |
| 71 | r->start ? : min, |
Yinghai Lu | 1f82de1 | 2009-04-23 20:48:32 -0700 | [diff] [blame] | 72 | max, align, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | alignf, alignf_data); |
| 74 | if (ret == 0) |
| 75 | break; |
| 76 | } |
| 77 | return ret; |
| 78 | } |
| 79 | |
| 80 | /** |
Yu Zhao | 3fa16fd | 2008-11-22 02:41:45 +0800 | [diff] [blame] | 81 | * pci_bus_add_device - add a single device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Sam Ravnborg | 96bde06 | 2007-03-26 21:53:30 -0800 | [diff] [blame] | 87 | int pci_bus_add_device(struct pci_dev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | { |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 89 | int retval; |
| 90 | retval = device_add(&dev->dev); |
| 91 | if (retval) |
| 92 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
Greg Kroah-Hartman | 8a1bc90 | 2008-02-14 14:56:56 -0800 | [diff] [blame] | 94 | dev->is_added = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | pci_proc_attach_device(dev); |
| 96 | pci_create_sysfs_dev_files(dev); |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 97 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /** |
Yu Zhao | 876e501 | 2008-11-22 02:42:35 +0800 | [diff] [blame] | 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 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
akpm@linux-foundation.org | c48f167 | 2009-02-03 15:45:26 -0800 | [diff] [blame] | 143 | void pci_bus_add_devices(const struct pci_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | { |
| 145 | struct pci_dev *dev; |
Yu Zhao | 3fa16fd | 2008-11-22 02:41:45 +0800 | [diff] [blame] | 146 | struct pci_bus *child; |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 147 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
| 149 | list_for_each_entry(dev, &bus->devices, bus_list) { |
Greg Kroah-Hartman | 8a1bc90 | 2008-02-14 14:56:56 -0800 | [diff] [blame] | 150 | /* Skip already-added devices */ |
| 151 | if (dev->is_added) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | continue; |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 153 | retval = pci_bus_add_device(dev); |
| 154 | if (retval) |
| 155 | dev_err(&dev->dev, "Error adding device, continuing\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | list_for_each_entry(dev, &bus->devices, bus_list) { |
Greg Kroah-Hartman | 8a1bc90 | 2008-02-14 14:56:56 -0800 | [diff] [blame] | 159 | BUG_ON(!dev->is_added); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
Yu Zhao | 3fa16fd | 2008-11-22 02:41:45 +0800 | [diff] [blame] | 161 | child = dev->subordinate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | /* |
| 163 | * If there is an unattached subordinate bus, attach |
| 164 | * it and then scan for unattached PCI devices. |
| 165 | */ |
Yu Zhao | 3fa16fd | 2008-11-22 02:41:45 +0800 | [diff] [blame] | 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); |
Greg Kroah-Hartman | fd7d1ce | 2007-05-22 22:47:54 -0400 | [diff] [blame] | 174 | |
Yu Zhao | 3fa16fd | 2008-11-22 02:41:45 +0800 | [diff] [blame] | 175 | /* |
| 176 | * register the bus with sysfs as the parent is now |
| 177 | * properly registered. |
| 178 | */ |
| 179 | if (child->is_added) |
| 180 | continue; |
Yu Zhao | 876e501 | 2008-11-22 02:42:35 +0800 | [diff] [blame] | 181 | retval = pci_bus_add_child(child); |
Yu Zhao | 3fa16fd | 2008-11-22 02:41:45 +0800 | [diff] [blame] | 182 | if (retval) |
Yu Zhao | 876e501 | 2008-11-22 02:42:35 +0800 | [diff] [blame] | 183 | dev_err(&dev->dev, "Error adding bus, continuing\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
| 187 | void pci_enable_bridges(struct pci_bus *bus) |
| 188 | { |
| 189 | struct pci_dev *dev; |
Greg Kroah-Hartman | 95a6296 | 2005-07-28 11:37:33 -0700 | [diff] [blame] | 190 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
| 192 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 193 | if (dev->subordinate) { |
Yuji Shimada | 296ccb0 | 2009-04-03 16:41:46 +0900 | [diff] [blame] | 194 | if (!pci_is_enabled(dev)) { |
Alex Chiang | 9dd90ca | 2009-03-20 14:56:20 -0600 | [diff] [blame] | 195 | retval = pci_enable_device(dev); |
| 196 | pci_set_master(dev); |
| 197 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | pci_enable_bridges(dev->subordinate); |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
Paul Mackerras | cecf486 | 2005-08-18 14:33:01 +1000 | [diff] [blame] | 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. |
Zhang, Yanmin | 70298c6 | 2009-06-16 13:34:38 +0800 | [diff] [blame] | 211 | * |
| 212 | * We check the return of @cb each time. If it returns anything |
| 213 | * other than 0, we break out. |
| 214 | * |
Paul Mackerras | cecf486 | 2005-08-18 14:33:01 +1000 | [diff] [blame] | 215 | */ |
Zhang, Yanmin | 70298c6 | 2009-06-16 13:34:38 +0800 | [diff] [blame] | 216 | void pci_walk_bus(struct pci_bus *top, int (*cb)(struct pci_dev *, void *), |
Paul Mackerras | cecf486 | 2005-08-18 14:33:01 +1000 | [diff] [blame] | 217 | void *userdata) |
| 218 | { |
| 219 | struct pci_dev *dev; |
| 220 | struct pci_bus *bus; |
| 221 | struct list_head *next; |
Zhang, Yanmin | 70298c6 | 2009-06-16 13:34:38 +0800 | [diff] [blame] | 222 | int retval; |
Paul Mackerras | cecf486 | 2005-08-18 14:33:01 +1000 | [diff] [blame] | 223 | |
| 224 | bus = top; |
Zhang Yanmin | d71374d | 2006-06-02 12:35:43 +0800 | [diff] [blame] | 225 | down_read(&pci_bus_sem); |
Paul Mackerras | cecf486 | 2005-08-18 14:33:01 +1000 | [diff] [blame] | 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); |
Paul Mackerras | cecf486 | 2005-08-18 14:33:01 +1000 | [diff] [blame] | 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; |
Paul Mackerras | cecf486 | 2005-08-18 14:33:01 +1000 | [diff] [blame] | 243 | |
Zhang Yanmin | d71374d | 2006-06-02 12:35:43 +0800 | [diff] [blame] | 244 | /* Run device routines with the device locked */ |
| 245 | down(&dev->dev.sem); |
Zhang, Yanmin | 70298c6 | 2009-06-16 13:34:38 +0800 | [diff] [blame] | 246 | retval = cb(dev, userdata); |
Zhang Yanmin | d71374d | 2006-06-02 12:35:43 +0800 | [diff] [blame] | 247 | up(&dev->dev.sem); |
Zhang, Yanmin | 70298c6 | 2009-06-16 13:34:38 +0800 | [diff] [blame] | 248 | if (retval) |
| 249 | break; |
Paul Mackerras | cecf486 | 2005-08-18 14:33:01 +1000 | [diff] [blame] | 250 | } |
Zhang Yanmin | d71374d | 2006-06-02 12:35:43 +0800 | [diff] [blame] | 251 | up_read(&pci_bus_sem); |
Paul Mackerras | cecf486 | 2005-08-18 14:33:01 +1000 | [diff] [blame] | 252 | } |
Paul Mackerras | cecf486 | 2005-08-18 14:33:01 +1000 | [diff] [blame] | 253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |