Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/pci/setup-bus.c |
| 3 | * |
| 4 | * Extruded from code written by |
| 5 | * Dave Rusling (david.rusling@reo.mts.dec.com) |
| 6 | * David Mosberger (davidm@cs.arizona.edu) |
| 7 | * David Miller (davem@redhat.com) |
| 8 | * |
| 9 | * Support routines for initializing a PCI subsystem. |
| 10 | */ |
| 11 | |
| 12 | /* |
| 13 | * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru> |
| 14 | * PCI-PCI bridges cleanup, sorted resource allocation. |
| 15 | * Feb 2002, Ivan Kokshaysky <ink@jurassic.park.msu.ru> |
| 16 | * Converted to allocation in 3 passes, which gives |
| 17 | * tighter packing. Prefetchable range support. |
| 18 | */ |
| 19 | |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/pci.h> |
| 24 | #include <linux/errno.h> |
| 25 | #include <linux/ioport.h> |
| 26 | #include <linux/cache.h> |
| 27 | #include <linux/slab.h> |
Chris Wright | 6faf17f | 2009-08-28 13:00:06 -0700 | [diff] [blame] | 28 | #include "pci.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 30 | struct pci_dev_resource { |
| 31 | struct list_head list; |
Yinghai Lu | 2934a0d | 2012-01-21 02:08:26 -0800 | [diff] [blame] | 32 | struct resource *res; |
| 33 | struct pci_dev *dev; |
Yinghai Lu | 568ddef | 2010-01-22 01:02:21 -0800 | [diff] [blame] | 34 | resource_size_t start; |
| 35 | resource_size_t end; |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 36 | resource_size_t add_size; |
Ram Pai | 2bbc694 | 2011-07-25 13:08:39 -0700 | [diff] [blame] | 37 | resource_size_t min_align; |
Yinghai Lu | 568ddef | 2010-01-22 01:02:21 -0800 | [diff] [blame] | 38 | unsigned long flags; |
| 39 | }; |
| 40 | |
Yinghai Lu | bffc56d | 2012-01-21 02:08:30 -0800 | [diff] [blame] | 41 | static void free_list(struct list_head *head) |
| 42 | { |
| 43 | struct pci_dev_resource *dev_res, *tmp; |
| 44 | |
| 45 | list_for_each_entry_safe(dev_res, tmp, head, list) { |
| 46 | list_del(&dev_res->list); |
| 47 | kfree(dev_res); |
| 48 | } |
| 49 | } |
Ram Pai | 094732a | 2011-02-14 17:43:18 -0800 | [diff] [blame] | 50 | |
Ram Pai | f483d39 | 2011-07-07 11:19:10 -0700 | [diff] [blame] | 51 | int pci_realloc_enable = 0; |
| 52 | #define pci_realloc_enabled() pci_realloc_enable |
| 53 | void pci_realloc(void) |
| 54 | { |
| 55 | pci_realloc_enable = 1; |
| 56 | } |
| 57 | |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 58 | /** |
| 59 | * add_to_list() - add a new resource tracker to the list |
| 60 | * @head: Head of the list |
| 61 | * @dev: device corresponding to which the resource |
| 62 | * belongs |
| 63 | * @res: The resource to be tracked |
| 64 | * @add_size: additional size to be optionally added |
| 65 | * to the resource |
| 66 | */ |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 67 | static int add_to_list(struct list_head *head, |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 68 | struct pci_dev *dev, struct resource *res, |
Ram Pai | 2bbc694 | 2011-07-25 13:08:39 -0700 | [diff] [blame] | 69 | resource_size_t add_size, resource_size_t min_align) |
Yinghai Lu | 568ddef | 2010-01-22 01:02:21 -0800 | [diff] [blame] | 70 | { |
Yinghai Lu | 764242a | 2012-01-21 02:08:28 -0800 | [diff] [blame] | 71 | struct pci_dev_resource *tmp; |
Yinghai Lu | 568ddef | 2010-01-22 01:02:21 -0800 | [diff] [blame] | 72 | |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 73 | tmp = kzalloc(sizeof(*tmp), GFP_KERNEL); |
Yinghai Lu | 568ddef | 2010-01-22 01:02:21 -0800 | [diff] [blame] | 74 | if (!tmp) { |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 75 | pr_warning("add_to_list: kmalloc() failed!\n"); |
Yinghai Lu | ef62dfe | 2012-01-21 02:08:18 -0800 | [diff] [blame] | 76 | return -ENOMEM; |
Yinghai Lu | 568ddef | 2010-01-22 01:02:21 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Yinghai Lu | 568ddef | 2010-01-22 01:02:21 -0800 | [diff] [blame] | 79 | tmp->res = res; |
| 80 | tmp->dev = dev; |
| 81 | tmp->start = res->start; |
| 82 | tmp->end = res->end; |
| 83 | tmp->flags = res->flags; |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 84 | tmp->add_size = add_size; |
Ram Pai | 2bbc694 | 2011-07-25 13:08:39 -0700 | [diff] [blame] | 85 | tmp->min_align = min_align; |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 86 | |
| 87 | list_add(&tmp->list, head); |
Yinghai Lu | ef62dfe | 2012-01-21 02:08:18 -0800 | [diff] [blame] | 88 | |
| 89 | return 0; |
Yinghai Lu | 568ddef | 2010-01-22 01:02:21 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Yinghai Lu | b9b0bba | 2012-01-21 02:08:29 -0800 | [diff] [blame] | 92 | static void remove_from_list(struct list_head *head, |
Yinghai Lu | 3e6e0d8 | 2012-01-21 02:08:20 -0800 | [diff] [blame] | 93 | struct resource *res) |
| 94 | { |
Yinghai Lu | b9b0bba | 2012-01-21 02:08:29 -0800 | [diff] [blame] | 95 | struct pci_dev_resource *dev_res, *tmp; |
Yinghai Lu | 3e6e0d8 | 2012-01-21 02:08:20 -0800 | [diff] [blame] | 96 | |
Yinghai Lu | b9b0bba | 2012-01-21 02:08:29 -0800 | [diff] [blame] | 97 | list_for_each_entry_safe(dev_res, tmp, head, list) { |
| 98 | if (dev_res->res == res) { |
| 99 | list_del(&dev_res->list); |
| 100 | kfree(dev_res); |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 101 | break; |
Yinghai Lu | 3e6e0d8 | 2012-01-21 02:08:20 -0800 | [diff] [blame] | 102 | } |
Yinghai Lu | 3e6e0d8 | 2012-01-21 02:08:20 -0800 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
Yinghai Lu | b9b0bba | 2012-01-21 02:08:29 -0800 | [diff] [blame] | 106 | static resource_size_t get_res_add_size(struct list_head *head, |
Yinghai Lu | 1c37235 | 2012-01-21 02:08:19 -0800 | [diff] [blame] | 107 | struct resource *res) |
| 108 | { |
Yinghai Lu | b9b0bba | 2012-01-21 02:08:29 -0800 | [diff] [blame] | 109 | struct pci_dev_resource *dev_res; |
Yinghai Lu | 1c37235 | 2012-01-21 02:08:19 -0800 | [diff] [blame] | 110 | |
Yinghai Lu | b9b0bba | 2012-01-21 02:08:29 -0800 | [diff] [blame] | 111 | list_for_each_entry(dev_res, head, list) { |
| 112 | if (dev_res->res == res) { |
Yinghai Lu | b592443 | 2012-01-21 02:08:31 -0800 | [diff] [blame] | 113 | int idx = res - &dev_res->dev->resource[0]; |
| 114 | |
Yinghai Lu | b9b0bba | 2012-01-21 02:08:29 -0800 | [diff] [blame] | 115 | dev_printk(KERN_DEBUG, &dev_res->dev->dev, |
Yinghai Lu | b592443 | 2012-01-21 02:08:31 -0800 | [diff] [blame] | 116 | "res[%d]=%pR get_res_add_size add_size %llx\n", |
| 117 | idx, dev_res->res, |
Yinghai Lu | b9b0bba | 2012-01-21 02:08:29 -0800 | [diff] [blame] | 118 | (unsigned long long)dev_res->add_size); |
Yinghai Lu | b592443 | 2012-01-21 02:08:31 -0800 | [diff] [blame] | 119 | |
Yinghai Lu | b9b0bba | 2012-01-21 02:08:29 -0800 | [diff] [blame] | 120 | return dev_res->add_size; |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 121 | } |
Yinghai Lu | 3e6e0d8 | 2012-01-21 02:08:20 -0800 | [diff] [blame] | 122 | } |
Yinghai Lu | 1c37235 | 2012-01-21 02:08:19 -0800 | [diff] [blame] | 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
Yinghai Lu | 78c3b32 | 2012-01-21 02:08:25 -0800 | [diff] [blame] | 127 | /* Sort resources by alignment */ |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 128 | static void pdev_sort_resources(struct pci_dev *dev, struct list_head *head) |
Yinghai Lu | 78c3b32 | 2012-01-21 02:08:25 -0800 | [diff] [blame] | 129 | { |
| 130 | int i; |
| 131 | |
| 132 | for (i = 0; i < PCI_NUM_RESOURCES; i++) { |
| 133 | struct resource *r; |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 134 | struct pci_dev_resource *dev_res, *tmp; |
Yinghai Lu | 78c3b32 | 2012-01-21 02:08:25 -0800 | [diff] [blame] | 135 | resource_size_t r_align; |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 136 | struct list_head *n; |
Yinghai Lu | 78c3b32 | 2012-01-21 02:08:25 -0800 | [diff] [blame] | 137 | |
| 138 | r = &dev->resource[i]; |
| 139 | |
| 140 | if (r->flags & IORESOURCE_PCI_FIXED) |
| 141 | continue; |
| 142 | |
| 143 | if (!(r->flags) || r->parent) |
| 144 | continue; |
| 145 | |
| 146 | r_align = pci_resource_alignment(dev, r); |
| 147 | if (!r_align) { |
| 148 | dev_warn(&dev->dev, "BAR %d: %pR has bogus alignment\n", |
| 149 | i, r); |
| 150 | continue; |
| 151 | } |
Yinghai Lu | 78c3b32 | 2012-01-21 02:08:25 -0800 | [diff] [blame] | 152 | |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 153 | tmp = kzalloc(sizeof(*tmp), GFP_KERNEL); |
| 154 | if (!tmp) |
| 155 | panic("pdev_sort_resources(): " |
| 156 | "kmalloc() failed!\n"); |
| 157 | tmp->res = r; |
| 158 | tmp->dev = dev; |
| 159 | |
| 160 | /* fallback is smallest one or list is empty*/ |
| 161 | n = head; |
| 162 | list_for_each_entry(dev_res, head, list) { |
| 163 | resource_size_t align; |
| 164 | |
| 165 | align = pci_resource_alignment(dev_res->dev, |
| 166 | dev_res->res); |
Yinghai Lu | 78c3b32 | 2012-01-21 02:08:25 -0800 | [diff] [blame] | 167 | |
| 168 | if (r_align > align) { |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 169 | n = &dev_res->list; |
Yinghai Lu | 78c3b32 | 2012-01-21 02:08:25 -0800 | [diff] [blame] | 170 | break; |
| 171 | } |
| 172 | } |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 173 | /* Insert it just before n*/ |
| 174 | list_add_tail(&tmp->list, n); |
Yinghai Lu | 78c3b32 | 2012-01-21 02:08:25 -0800 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
Yinghai Lu | 6841ec6 | 2010-01-22 01:02:25 -0800 | [diff] [blame] | 178 | static void __dev_sort_resources(struct pci_dev *dev, |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 179 | struct list_head *head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | { |
Yinghai Lu | 6841ec6 | 2010-01-22 01:02:25 -0800 | [diff] [blame] | 181 | u16 class = dev->class >> 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | |
Yinghai Lu | 6841ec6 | 2010-01-22 01:02:25 -0800 | [diff] [blame] | 183 | /* Don't touch classless devices or host bridges or ioapics. */ |
| 184 | if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST) |
| 185 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
Yinghai Lu | 6841ec6 | 2010-01-22 01:02:25 -0800 | [diff] [blame] | 187 | /* Don't touch ioapic devices already enabled by firmware */ |
| 188 | if (class == PCI_CLASS_SYSTEM_PIC) { |
| 189 | u16 command; |
| 190 | pci_read_config_word(dev, PCI_COMMAND, &command); |
| 191 | if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) |
| 192 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Yinghai Lu | 6841ec6 | 2010-01-22 01:02:25 -0800 | [diff] [blame] | 195 | pdev_sort_resources(dev, head); |
| 196 | } |
| 197 | |
Ram Pai | fc075e1 | 2011-02-14 17:43:19 -0800 | [diff] [blame] | 198 | static inline void reset_resource(struct resource *res) |
| 199 | { |
| 200 | res->start = 0; |
| 201 | res->end = 0; |
| 202 | res->flags = 0; |
| 203 | } |
| 204 | |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 205 | /** |
Ram Pai | 9e8bf93 | 2011-07-25 13:08:42 -0700 | [diff] [blame] | 206 | * reassign_resources_sorted() - satisfy any additional resource requests |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 207 | * |
Ram Pai | 9e8bf93 | 2011-07-25 13:08:42 -0700 | [diff] [blame] | 208 | * @realloc_head : head of the list tracking requests requiring additional |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 209 | * resources |
| 210 | * @head : head of the list tracking requests with allocated |
| 211 | * resources |
| 212 | * |
Ram Pai | 9e8bf93 | 2011-07-25 13:08:42 -0700 | [diff] [blame] | 213 | * Walk through each element of the realloc_head and try to procure |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 214 | * additional resources for the element, provided the element |
| 215 | * is in the head list. |
| 216 | */ |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 217 | static void reassign_resources_sorted(struct list_head *realloc_head, |
| 218 | struct list_head *head) |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 219 | { |
| 220 | struct resource *res; |
Yinghai Lu | b9b0bba | 2012-01-21 02:08:29 -0800 | [diff] [blame] | 221 | struct pci_dev_resource *add_res, *tmp; |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 222 | struct pci_dev_resource *dev_res; |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 223 | resource_size_t add_size; |
| 224 | int idx; |
| 225 | |
Yinghai Lu | b9b0bba | 2012-01-21 02:08:29 -0800 | [diff] [blame] | 226 | list_for_each_entry_safe(add_res, tmp, realloc_head, list) { |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 227 | bool found_match = false; |
| 228 | |
Yinghai Lu | b9b0bba | 2012-01-21 02:08:29 -0800 | [diff] [blame] | 229 | res = add_res->res; |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 230 | /* skip resource that has been reset */ |
| 231 | if (!res->flags) |
| 232 | goto out; |
| 233 | |
| 234 | /* skip this resource if not found in head list */ |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 235 | list_for_each_entry(dev_res, head, list) { |
| 236 | if (dev_res->res == res) { |
| 237 | found_match = true; |
| 238 | break; |
| 239 | } |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 240 | } |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 241 | if (!found_match)/* just skip */ |
| 242 | continue; |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 243 | |
Yinghai Lu | b9b0bba | 2012-01-21 02:08:29 -0800 | [diff] [blame] | 244 | idx = res - &add_res->dev->resource[0]; |
| 245 | add_size = add_res->add_size; |
Ram Pai | 2bbc694 | 2011-07-25 13:08:39 -0700 | [diff] [blame] | 246 | if (!resource_size(res)) { |
Yinghai Lu | b9b0bba | 2012-01-21 02:08:29 -0800 | [diff] [blame] | 247 | res->start = add_res->start; |
Ram Pai | 2bbc694 | 2011-07-25 13:08:39 -0700 | [diff] [blame] | 248 | res->end = res->start + add_size - 1; |
Yinghai Lu | b9b0bba | 2012-01-21 02:08:29 -0800 | [diff] [blame] | 249 | if (pci_assign_resource(add_res->dev, idx)) |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 250 | reset_resource(res); |
Ram Pai | 2bbc694 | 2011-07-25 13:08:39 -0700 | [diff] [blame] | 251 | } else { |
Yinghai Lu | b9b0bba | 2012-01-21 02:08:29 -0800 | [diff] [blame] | 252 | resource_size_t align = add_res->min_align; |
| 253 | res->flags |= add_res->flags & |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 254 | (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN); |
Yinghai Lu | b9b0bba | 2012-01-21 02:08:29 -0800 | [diff] [blame] | 255 | if (pci_reassign_resource(add_res->dev, idx, |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 256 | add_size, align)) |
Yinghai Lu | b9b0bba | 2012-01-21 02:08:29 -0800 | [diff] [blame] | 257 | dev_printk(KERN_DEBUG, &add_res->dev->dev, |
Yinghai Lu | b592443 | 2012-01-21 02:08:31 -0800 | [diff] [blame] | 258 | "failed to add %llx res[%d]=%pR\n", |
| 259 | (unsigned long long)add_size, |
| 260 | idx, res); |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 261 | } |
| 262 | out: |
Yinghai Lu | b9b0bba | 2012-01-21 02:08:29 -0800 | [diff] [blame] | 263 | list_del(&add_res->list); |
| 264 | kfree(add_res); |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * assign_requested_resources_sorted() - satisfy resource requests |
| 270 | * |
| 271 | * @head : head of the list tracking requests for resources |
| 272 | * @failed_list : head of the list tracking requests that could |
| 273 | * not be allocated |
| 274 | * |
| 275 | * Satisfy resource requests of each element in the list. Add |
| 276 | * requests that could not satisfied to the failed_list. |
| 277 | */ |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 278 | static void assign_requested_resources_sorted(struct list_head *head, |
| 279 | struct list_head *fail_head) |
Yinghai Lu | 6841ec6 | 2010-01-22 01:02:25 -0800 | [diff] [blame] | 280 | { |
| 281 | struct resource *res; |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 282 | struct pci_dev_resource *dev_res; |
Yinghai Lu | 6841ec6 | 2010-01-22 01:02:25 -0800 | [diff] [blame] | 283 | int idx; |
| 284 | |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 285 | list_for_each_entry(dev_res, head, list) { |
| 286 | res = dev_res->res; |
| 287 | idx = res - &dev_res->dev->resource[0]; |
| 288 | if (resource_size(res) && |
| 289 | pci_assign_resource(dev_res->dev, idx)) { |
| 290 | if (fail_head && !pci_is_root_bus(dev_res->dev->bus)) { |
Yinghai Lu | 9a92866 | 2010-02-28 15:49:39 -0800 | [diff] [blame] | 291 | /* |
| 292 | * if the failed res is for ROM BAR, and it will |
| 293 | * be enabled later, don't add it to the list |
| 294 | */ |
| 295 | if (!((idx == PCI_ROM_RESOURCE) && |
| 296 | (!(res->flags & IORESOURCE_ROM_ENABLE)))) |
Yinghai Lu | 67cc7e2 | 2012-01-21 02:08:32 -0800 | [diff] [blame] | 297 | add_to_list(fail_head, |
| 298 | dev_res->dev, res, |
| 299 | 0 /* dont care */, |
| 300 | 0 /* dont care */); |
Yinghai Lu | 9a92866 | 2010-02-28 15:49:39 -0800 | [diff] [blame] | 301 | } |
Ram Pai | fc075e1 | 2011-02-14 17:43:19 -0800 | [diff] [blame] | 302 | reset_resource(res); |
Rajesh Shah | 542df5d | 2005-04-28 00:25:50 -0700 | [diff] [blame] | 303 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 307 | static void __assign_resources_sorted(struct list_head *head, |
| 308 | struct list_head *realloc_head, |
| 309 | struct list_head *fail_head) |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 310 | { |
Yinghai Lu | 3e6e0d8 | 2012-01-21 02:08:20 -0800 | [diff] [blame] | 311 | /* |
| 312 | * Should not assign requested resources at first. |
| 313 | * they could be adjacent, so later reassign can not reallocate |
| 314 | * them one by one in parent resource window. |
| 315 | * Try to assign requested + add_size at begining |
| 316 | * if could do that, could get out early. |
| 317 | * if could not do that, we still try to assign requested at first, |
| 318 | * then try to reassign add_size for some resources. |
| 319 | */ |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 320 | LIST_HEAD(save_head); |
| 321 | LIST_HEAD(local_fail_head); |
Yinghai Lu | b9b0bba | 2012-01-21 02:08:29 -0800 | [diff] [blame] | 322 | struct pci_dev_resource *save_res; |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 323 | struct pci_dev_resource *dev_res; |
Yinghai Lu | 3e6e0d8 | 2012-01-21 02:08:20 -0800 | [diff] [blame] | 324 | |
| 325 | /* Check if optional add_size is there */ |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 326 | if (!realloc_head || list_empty(realloc_head)) |
Yinghai Lu | 3e6e0d8 | 2012-01-21 02:08:20 -0800 | [diff] [blame] | 327 | goto requested_and_reassign; |
| 328 | |
| 329 | /* Save original start, end, flags etc at first */ |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 330 | list_for_each_entry(dev_res, head, list) { |
| 331 | if (add_to_list(&save_head, dev_res->dev, dev_res->res, 0, 0)) { |
Yinghai Lu | bffc56d | 2012-01-21 02:08:30 -0800 | [diff] [blame] | 332 | free_list(&save_head); |
Yinghai Lu | 3e6e0d8 | 2012-01-21 02:08:20 -0800 | [diff] [blame] | 333 | goto requested_and_reassign; |
| 334 | } |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 335 | } |
Yinghai Lu | 3e6e0d8 | 2012-01-21 02:08:20 -0800 | [diff] [blame] | 336 | |
| 337 | /* Update res in head list with add_size in realloc_head list */ |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 338 | list_for_each_entry(dev_res, head, list) |
| 339 | dev_res->res->end += get_res_add_size(realloc_head, |
| 340 | dev_res->res); |
Yinghai Lu | 3e6e0d8 | 2012-01-21 02:08:20 -0800 | [diff] [blame] | 341 | |
| 342 | /* Try updated head list with add_size added */ |
Yinghai Lu | 3e6e0d8 | 2012-01-21 02:08:20 -0800 | [diff] [blame] | 343 | assign_requested_resources_sorted(head, &local_fail_head); |
| 344 | |
| 345 | /* all assigned with add_size ? */ |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 346 | if (list_empty(&local_fail_head)) { |
Yinghai Lu | 3e6e0d8 | 2012-01-21 02:08:20 -0800 | [diff] [blame] | 347 | /* Remove head list from realloc_head list */ |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 348 | list_for_each_entry(dev_res, head, list) |
| 349 | remove_from_list(realloc_head, dev_res->res); |
Yinghai Lu | bffc56d | 2012-01-21 02:08:30 -0800 | [diff] [blame] | 350 | free_list(&save_head); |
| 351 | free_list(head); |
Yinghai Lu | 3e6e0d8 | 2012-01-21 02:08:20 -0800 | [diff] [blame] | 352 | return; |
| 353 | } |
| 354 | |
Yinghai Lu | bffc56d | 2012-01-21 02:08:30 -0800 | [diff] [blame] | 355 | free_list(&local_fail_head); |
Yinghai Lu | 3e6e0d8 | 2012-01-21 02:08:20 -0800 | [diff] [blame] | 356 | /* Release assigned resource */ |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 357 | list_for_each_entry(dev_res, head, list) |
| 358 | if (dev_res->res->parent) |
| 359 | release_resource(dev_res->res); |
Yinghai Lu | 3e6e0d8 | 2012-01-21 02:08:20 -0800 | [diff] [blame] | 360 | /* Restore start/end/flags from saved list */ |
Yinghai Lu | b9b0bba | 2012-01-21 02:08:29 -0800 | [diff] [blame] | 361 | list_for_each_entry(save_res, &save_head, list) { |
| 362 | struct resource *res = save_res->res; |
Yinghai Lu | 3e6e0d8 | 2012-01-21 02:08:20 -0800 | [diff] [blame] | 363 | |
Yinghai Lu | b9b0bba | 2012-01-21 02:08:29 -0800 | [diff] [blame] | 364 | res->start = save_res->start; |
| 365 | res->end = save_res->end; |
| 366 | res->flags = save_res->flags; |
Yinghai Lu | 3e6e0d8 | 2012-01-21 02:08:20 -0800 | [diff] [blame] | 367 | } |
Yinghai Lu | bffc56d | 2012-01-21 02:08:30 -0800 | [diff] [blame] | 368 | free_list(&save_head); |
Yinghai Lu | 3e6e0d8 | 2012-01-21 02:08:20 -0800 | [diff] [blame] | 369 | |
| 370 | requested_and_reassign: |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 371 | /* Satisfy the must-have resource requests */ |
| 372 | assign_requested_resources_sorted(head, fail_head); |
| 373 | |
Ram Pai | 0a2daa1 | 2011-07-25 13:08:41 -0700 | [diff] [blame] | 374 | /* Try to satisfy any additional optional resource |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 375 | requests */ |
Ram Pai | 9e8bf93 | 2011-07-25 13:08:42 -0700 | [diff] [blame] | 376 | if (realloc_head) |
| 377 | reassign_resources_sorted(realloc_head, head); |
Yinghai Lu | bffc56d | 2012-01-21 02:08:30 -0800 | [diff] [blame] | 378 | free_list(head); |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 379 | } |
| 380 | |
Yinghai Lu | 6841ec6 | 2010-01-22 01:02:25 -0800 | [diff] [blame] | 381 | static void pdev_assign_resources_sorted(struct pci_dev *dev, |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 382 | struct list_head *add_head, |
| 383 | struct list_head *fail_head) |
Yinghai Lu | 6841ec6 | 2010-01-22 01:02:25 -0800 | [diff] [blame] | 384 | { |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 385 | LIST_HEAD(head); |
Yinghai Lu | 6841ec6 | 2010-01-22 01:02:25 -0800 | [diff] [blame] | 386 | |
Yinghai Lu | 6841ec6 | 2010-01-22 01:02:25 -0800 | [diff] [blame] | 387 | __dev_sort_resources(dev, &head); |
Yinghai Lu | 8424d75 | 2012-01-21 02:08:21 -0800 | [diff] [blame] | 388 | __assign_resources_sorted(&head, add_head, fail_head); |
Yinghai Lu | 6841ec6 | 2010-01-22 01:02:25 -0800 | [diff] [blame] | 389 | |
| 390 | } |
| 391 | |
| 392 | static void pbus_assign_resources_sorted(const struct pci_bus *bus, |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 393 | struct list_head *realloc_head, |
| 394 | struct list_head *fail_head) |
Yinghai Lu | 6841ec6 | 2010-01-22 01:02:25 -0800 | [diff] [blame] | 395 | { |
| 396 | struct pci_dev *dev; |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 397 | LIST_HEAD(head); |
Yinghai Lu | 6841ec6 | 2010-01-22 01:02:25 -0800 | [diff] [blame] | 398 | |
Yinghai Lu | 6841ec6 | 2010-01-22 01:02:25 -0800 | [diff] [blame] | 399 | list_for_each_entry(dev, &bus->devices, bus_list) |
| 400 | __dev_sort_resources(dev, &head); |
| 401 | |
Ram Pai | 9e8bf93 | 2011-07-25 13:08:42 -0700 | [diff] [blame] | 402 | __assign_resources_sorted(&head, realloc_head, fail_head); |
Yinghai Lu | 6841ec6 | 2010-01-22 01:02:25 -0800 | [diff] [blame] | 403 | } |
| 404 | |
Dominik Brodowski | b3743fa | 2005-09-09 13:03:23 -0700 | [diff] [blame] | 405 | void pci_setup_cardbus(struct pci_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | { |
| 407 | struct pci_dev *bridge = bus->self; |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 408 | struct resource *res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | struct pci_bus_region region; |
| 410 | |
Bjorn Helgaas | 865df57 | 2009-11-04 10:32:57 -0700 | [diff] [blame] | 411 | dev_info(&bridge->dev, "CardBus bridge to [bus %02x-%02x]\n", |
| 412 | bus->secondary, bus->subordinate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 414 | res = bus->resource[0]; |
| 415 | pcibios_resource_to_bus(bridge, ®ion, res); |
| 416 | if (res->flags & IORESOURCE_IO) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | /* |
| 418 | * The IO resource is allocated a range twice as large as it |
| 419 | * would normally need. This allows us to set both IO regs. |
| 420 | */ |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 421 | dev_info(&bridge->dev, " bridge window %pR\n", res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | pci_write_config_dword(bridge, PCI_CB_IO_BASE_0, |
| 423 | region.start); |
| 424 | pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0, |
| 425 | region.end); |
| 426 | } |
| 427 | |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 428 | res = bus->resource[1]; |
| 429 | pcibios_resource_to_bus(bridge, ®ion, res); |
| 430 | if (res->flags & IORESOURCE_IO) { |
| 431 | dev_info(&bridge->dev, " bridge window %pR\n", res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | pci_write_config_dword(bridge, PCI_CB_IO_BASE_1, |
| 433 | region.start); |
| 434 | pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1, |
| 435 | region.end); |
| 436 | } |
| 437 | |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 438 | res = bus->resource[2]; |
| 439 | pcibios_resource_to_bus(bridge, ®ion, res); |
| 440 | if (res->flags & IORESOURCE_MEM) { |
| 441 | dev_info(&bridge->dev, " bridge window %pR\n", res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0, |
| 443 | region.start); |
| 444 | pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0, |
| 445 | region.end); |
| 446 | } |
| 447 | |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 448 | res = bus->resource[3]; |
| 449 | pcibios_resource_to_bus(bridge, ®ion, res); |
| 450 | if (res->flags & IORESOURCE_MEM) { |
| 451 | dev_info(&bridge->dev, " bridge window %pR\n", res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1, |
| 453 | region.start); |
| 454 | pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1, |
| 455 | region.end); |
| 456 | } |
| 457 | } |
Dominik Brodowski | b3743fa | 2005-09-09 13:03:23 -0700 | [diff] [blame] | 458 | EXPORT_SYMBOL(pci_setup_cardbus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | |
| 460 | /* Initialize bridges with base/limit values we have collected. |
| 461 | PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998) |
| 462 | requires that if there is no I/O ports or memory behind the |
| 463 | bridge, corresponding range must be turned off by writing base |
| 464 | value greater than limit to the bridge's base/limit registers. |
| 465 | |
| 466 | Note: care must be taken when updating I/O base/limit registers |
| 467 | of bridges which support 32-bit I/O. This update requires two |
| 468 | config space writes, so it's quite possible that an I/O window of |
| 469 | the bridge will have some undesirable address (e.g. 0) after the |
| 470 | first write. Ditto 64-bit prefetchable MMIO. */ |
Yinghai Lu | 7cc5997 | 2009-12-22 15:02:21 -0800 | [diff] [blame] | 471 | static void pci_setup_bridge_io(struct pci_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | { |
| 473 | struct pci_dev *bridge = bus->self; |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 474 | struct resource *res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | struct pci_bus_region region; |
Yinghai Lu | 7cc5997 | 2009-12-22 15:02:21 -0800 | [diff] [blame] | 476 | u32 l, io_upper16; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | |
| 478 | /* Set up the top and bottom of the PCI I/O segment for this bus. */ |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 479 | res = bus->resource[0]; |
| 480 | pcibios_resource_to_bus(bridge, ®ion, res); |
| 481 | if (res->flags & IORESOURCE_IO) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | pci_read_config_dword(bridge, PCI_IO_BASE, &l); |
| 483 | l &= 0xffff0000; |
| 484 | l |= (region.start >> 8) & 0x00f0; |
| 485 | l |= region.end & 0xf000; |
| 486 | /* Set up upper 16 bits of I/O base/limit. */ |
| 487 | io_upper16 = (region.end & 0xffff0000) | (region.start >> 16); |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 488 | dev_info(&bridge->dev, " bridge window %pR\n", res); |
Yinghai Lu | 7cc5997 | 2009-12-22 15:02:21 -0800 | [diff] [blame] | 489 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | /* Clear upper 16 bits of I/O base/limit. */ |
| 491 | io_upper16 = 0; |
| 492 | l = 0x00f0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | } |
| 494 | /* Temporarily disable the I/O range before updating PCI_IO_BASE. */ |
| 495 | pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff); |
| 496 | /* Update lower 16 bits of I/O base/limit. */ |
| 497 | pci_write_config_dword(bridge, PCI_IO_BASE, l); |
| 498 | /* Update upper 16 bits of I/O base/limit. */ |
| 499 | pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16); |
Yinghai Lu | 7cc5997 | 2009-12-22 15:02:21 -0800 | [diff] [blame] | 500 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | |
Yinghai Lu | 7cc5997 | 2009-12-22 15:02:21 -0800 | [diff] [blame] | 502 | static void pci_setup_bridge_mmio(struct pci_bus *bus) |
| 503 | { |
| 504 | struct pci_dev *bridge = bus->self; |
| 505 | struct resource *res; |
| 506 | struct pci_bus_region region; |
| 507 | u32 l; |
| 508 | |
| 509 | /* Set up the top and bottom of the PCI Memory segment for this bus. */ |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 510 | res = bus->resource[1]; |
| 511 | pcibios_resource_to_bus(bridge, ®ion, res); |
| 512 | if (res->flags & IORESOURCE_MEM) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | l = (region.start >> 16) & 0xfff0; |
| 514 | l |= region.end & 0xfff00000; |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 515 | dev_info(&bridge->dev, " bridge window %pR\n", res); |
Yinghai Lu | 7cc5997 | 2009-12-22 15:02:21 -0800 | [diff] [blame] | 516 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | l = 0x0000fff0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | } |
| 519 | pci_write_config_dword(bridge, PCI_MEMORY_BASE, l); |
Yinghai Lu | 7cc5997 | 2009-12-22 15:02:21 -0800 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | static void pci_setup_bridge_mmio_pref(struct pci_bus *bus) |
| 523 | { |
| 524 | struct pci_dev *bridge = bus->self; |
| 525 | struct resource *res; |
| 526 | struct pci_bus_region region; |
| 527 | u32 l, bu, lu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | |
| 529 | /* Clear out the upper 32 bits of PREF limit. |
| 530 | If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily |
| 531 | disables PREF range, which is ok. */ |
| 532 | pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0); |
| 533 | |
| 534 | /* Set up PREF base/limit. */ |
Benjamin Herrenschmidt | c40a22e | 2007-12-10 17:32:15 +1100 | [diff] [blame] | 535 | bu = lu = 0; |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 536 | res = bus->resource[2]; |
| 537 | pcibios_resource_to_bus(bridge, ®ion, res); |
| 538 | if (res->flags & IORESOURCE_PREFETCH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | l = (region.start >> 16) & 0xfff0; |
| 540 | l |= region.end & 0xfff00000; |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 541 | if (res->flags & IORESOURCE_MEM_64) { |
Yinghai Lu | 1f82de1 | 2009-04-23 20:48:32 -0700 | [diff] [blame] | 542 | bu = upper_32_bits(region.start); |
| 543 | lu = upper_32_bits(region.end); |
Yinghai Lu | 1f82de1 | 2009-04-23 20:48:32 -0700 | [diff] [blame] | 544 | } |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 545 | dev_info(&bridge->dev, " bridge window %pR\n", res); |
Yinghai Lu | 7cc5997 | 2009-12-22 15:02:21 -0800 | [diff] [blame] | 546 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | l = 0x0000fff0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | } |
| 549 | pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l); |
| 550 | |
Alex Williamson | 59353ea | 2009-11-30 14:51:44 -0700 | [diff] [blame] | 551 | /* Set the upper 32 bits of PREF base & limit. */ |
| 552 | pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu); |
| 553 | pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu); |
Yinghai Lu | 7cc5997 | 2009-12-22 15:02:21 -0800 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type) |
| 557 | { |
| 558 | struct pci_dev *bridge = bus->self; |
| 559 | |
Yinghai Lu | 7cc5997 | 2009-12-22 15:02:21 -0800 | [diff] [blame] | 560 | dev_info(&bridge->dev, "PCI bridge to [bus %02x-%02x]\n", |
| 561 | bus->secondary, bus->subordinate); |
| 562 | |
| 563 | if (type & IORESOURCE_IO) |
| 564 | pci_setup_bridge_io(bus); |
| 565 | |
| 566 | if (type & IORESOURCE_MEM) |
| 567 | pci_setup_bridge_mmio(bus); |
| 568 | |
| 569 | if (type & IORESOURCE_PREFETCH) |
| 570 | pci_setup_bridge_mmio_pref(bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | |
| 572 | pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl); |
| 573 | } |
| 574 | |
Benjamin Herrenschmidt | e244427 | 2011-09-11 14:08:38 -0300 | [diff] [blame] | 575 | void pci_setup_bridge(struct pci_bus *bus) |
Yinghai Lu | 7cc5997 | 2009-12-22 15:02:21 -0800 | [diff] [blame] | 576 | { |
| 577 | unsigned long type = IORESOURCE_IO | IORESOURCE_MEM | |
| 578 | IORESOURCE_PREFETCH; |
| 579 | |
| 580 | __pci_setup_bridge(bus, type); |
| 581 | } |
| 582 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | /* Check whether the bridge supports optional I/O and |
| 584 | prefetchable memory ranges. If not, the respective |
| 585 | base/limit registers must be read-only and read as 0. */ |
Sam Ravnborg | 96bde06 | 2007-03-26 21:53:30 -0800 | [diff] [blame] | 586 | static void pci_bridge_check_ranges(struct pci_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | { |
| 588 | u16 io; |
| 589 | u32 pmem; |
| 590 | struct pci_dev *bridge = bus->self; |
| 591 | struct resource *b_res; |
| 592 | |
| 593 | b_res = &bridge->resource[PCI_BRIDGE_RESOURCES]; |
| 594 | b_res[1].flags |= IORESOURCE_MEM; |
| 595 | |
| 596 | pci_read_config_word(bridge, PCI_IO_BASE, &io); |
| 597 | if (!io) { |
| 598 | pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0); |
| 599 | pci_read_config_word(bridge, PCI_IO_BASE, &io); |
| 600 | pci_write_config_word(bridge, PCI_IO_BASE, 0x0); |
| 601 | } |
| 602 | if (io) |
| 603 | b_res[0].flags |= IORESOURCE_IO; |
| 604 | /* DECchip 21050 pass 2 errata: the bridge may miss an address |
| 605 | disconnect boundary by one PCI data phase. |
| 606 | Workaround: do not use prefetching on this device. */ |
| 607 | if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001) |
| 608 | return; |
| 609 | pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem); |
| 610 | if (!pmem) { |
| 611 | pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, |
| 612 | 0xfff0fff0); |
| 613 | pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem); |
| 614 | pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0); |
| 615 | } |
Yinghai Lu | 1f82de1 | 2009-04-23 20:48:32 -0700 | [diff] [blame] | 616 | if (pmem) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH; |
Yinghai Lu | 9958610 | 2010-01-22 01:02:28 -0800 | [diff] [blame] | 618 | if ((pmem & PCI_PREF_RANGE_TYPE_MASK) == |
| 619 | PCI_PREF_RANGE_TYPE_64) { |
Yinghai Lu | 1f82de1 | 2009-04-23 20:48:32 -0700 | [diff] [blame] | 620 | b_res[2].flags |= IORESOURCE_MEM_64; |
Yinghai Lu | 9958610 | 2010-01-22 01:02:28 -0800 | [diff] [blame] | 621 | b_res[2].flags |= PCI_PREF_RANGE_TYPE_64; |
| 622 | } |
Yinghai Lu | 1f82de1 | 2009-04-23 20:48:32 -0700 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | /* double check if bridge does support 64 bit pref */ |
| 626 | if (b_res[2].flags & IORESOURCE_MEM_64) { |
| 627 | u32 mem_base_hi, tmp; |
| 628 | pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, |
| 629 | &mem_base_hi); |
| 630 | pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, |
| 631 | 0xffffffff); |
| 632 | pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp); |
| 633 | if (!tmp) |
| 634 | b_res[2].flags &= ~IORESOURCE_MEM_64; |
| 635 | pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, |
| 636 | mem_base_hi); |
| 637 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | /* Helper function for sizing routines: find first available |
| 641 | bus resource of a given type. Note: we intentionally skip |
| 642 | the bus resources which have already been assigned (that is, |
| 643 | have non-NULL parent resource). */ |
Sam Ravnborg | 96bde06 | 2007-03-26 21:53:30 -0800 | [diff] [blame] | 644 | static struct resource *find_free_bus_resource(struct pci_bus *bus, unsigned long type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | { |
| 646 | int i; |
| 647 | struct resource *r; |
| 648 | unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | |
| 649 | IORESOURCE_PREFETCH; |
| 650 | |
Bjorn Helgaas | 89a74ec | 2010-02-23 10:24:31 -0700 | [diff] [blame] | 651 | pci_bus_for_each_resource(bus, r, i) { |
Ivan Kokshaysky | 299de03 | 2005-06-15 18:59:27 +0400 | [diff] [blame] | 652 | if (r == &ioport_resource || r == &iomem_resource) |
| 653 | continue; |
Jesse Barnes | 55a1098 | 2009-10-27 09:39:18 -0700 | [diff] [blame] | 654 | if (r && (r->flags & type_mask) == type && !r->parent) |
| 655 | return r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | } |
| 657 | return NULL; |
| 658 | } |
| 659 | |
Ram Pai | 13583b1 | 2011-02-14 17:43:17 -0800 | [diff] [blame] | 660 | static resource_size_t calculate_iosize(resource_size_t size, |
| 661 | resource_size_t min_size, |
| 662 | resource_size_t size1, |
| 663 | resource_size_t old_size, |
| 664 | resource_size_t align) |
| 665 | { |
| 666 | if (size < min_size) |
| 667 | size = min_size; |
| 668 | if (old_size == 1 ) |
| 669 | old_size = 0; |
| 670 | /* To be fixed in 2.5: we should have sort of HAVE_ISA |
| 671 | flag in the struct pci_bus. */ |
| 672 | #if defined(CONFIG_ISA) || defined(CONFIG_EISA) |
| 673 | size = (size & 0xff) + ((size & ~0xffUL) << 2); |
| 674 | #endif |
| 675 | size = ALIGN(size + size1, align); |
| 676 | if (size < old_size) |
| 677 | size = old_size; |
| 678 | return size; |
| 679 | } |
| 680 | |
| 681 | static resource_size_t calculate_memsize(resource_size_t size, |
| 682 | resource_size_t min_size, |
| 683 | resource_size_t size1, |
| 684 | resource_size_t old_size, |
| 685 | resource_size_t align) |
| 686 | { |
| 687 | if (size < min_size) |
| 688 | size = min_size; |
| 689 | if (old_size == 1 ) |
| 690 | old_size = 0; |
| 691 | if (size < old_size) |
| 692 | size = old_size; |
| 693 | size = ALIGN(size + size1, align); |
| 694 | return size; |
| 695 | } |
| 696 | |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 697 | /** |
| 698 | * pbus_size_io() - size the io window of a given bus |
| 699 | * |
| 700 | * @bus : the bus |
| 701 | * @min_size : the minimum io window that must to be allocated |
| 702 | * @add_size : additional optional io window |
Ram Pai | 9e8bf93 | 2011-07-25 13:08:42 -0700 | [diff] [blame] | 703 | * @realloc_head : track the additional io window on this list |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 704 | * |
| 705 | * Sizing the IO windows of the PCI-PCI bridge is trivial, |
| 706 | * since these windows have 4K granularity and the IO ranges |
| 707 | * of non-bridge PCI devices are limited to 256 bytes. |
| 708 | * We must be careful with the ISA aliasing though. |
| 709 | */ |
| 710 | static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size, |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 711 | resource_size_t add_size, struct list_head *realloc_head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | { |
| 713 | struct pci_dev *dev; |
| 714 | struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO); |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 715 | unsigned long size = 0, size0 = 0, size1 = 0; |
Yinghai Lu | be76891 | 2011-07-25 13:08:38 -0700 | [diff] [blame] | 716 | resource_size_t children_add_size = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | |
| 718 | if (!b_res) |
| 719 | return; |
| 720 | |
| 721 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 722 | int i; |
| 723 | |
| 724 | for (i = 0; i < PCI_NUM_RESOURCES; i++) { |
| 725 | struct resource *r = &dev->resource[i]; |
| 726 | unsigned long r_size; |
| 727 | |
| 728 | if (r->parent || !(r->flags & IORESOURCE_IO)) |
| 729 | continue; |
Zhao, Yu | 022edd8 | 2008-10-13 19:24:28 +0800 | [diff] [blame] | 730 | r_size = resource_size(r); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | |
| 732 | if (r_size < 0x400) |
| 733 | /* Might be re-aligned for ISA */ |
| 734 | size += r_size; |
| 735 | else |
| 736 | size1 += r_size; |
Yinghai Lu | be76891 | 2011-07-25 13:08:38 -0700 | [diff] [blame] | 737 | |
Ram Pai | 9e8bf93 | 2011-07-25 13:08:42 -0700 | [diff] [blame] | 738 | if (realloc_head) |
| 739 | children_add_size += get_res_add_size(realloc_head, r); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | } |
| 741 | } |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 742 | size0 = calculate_iosize(size, min_size, size1, |
Ram Pai | 13583b1 | 2011-02-14 17:43:17 -0800 | [diff] [blame] | 743 | resource_size(b_res), 4096); |
Yinghai Lu | be76891 | 2011-07-25 13:08:38 -0700 | [diff] [blame] | 744 | if (children_add_size > add_size) |
| 745 | add_size = children_add_size; |
Ram Pai | 9e8bf93 | 2011-07-25 13:08:42 -0700 | [diff] [blame] | 746 | size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 : |
Yinghai Lu | a4ac9fe | 2012-01-21 02:08:17 -0800 | [diff] [blame] | 747 | calculate_iosize(size, min_size, add_size + size1, |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 748 | resource_size(b_res), 4096); |
| 749 | if (!size0 && !size1) { |
Bjorn Helgaas | 865df57 | 2009-11-04 10:32:57 -0700 | [diff] [blame] | 750 | if (b_res->start || b_res->end) |
| 751 | dev_info(&bus->self->dev, "disabling bridge window " |
| 752 | "%pR to [bus %02x-%02x] (unused)\n", b_res, |
| 753 | bus->secondary, bus->subordinate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | b_res->flags = 0; |
| 755 | return; |
| 756 | } |
| 757 | /* Alignment of the IO window is always 4K */ |
| 758 | b_res->start = 4096; |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 759 | b_res->end = b_res->start + size0 - 1; |
Ivan Kokshaysky | 8845256 | 2008-03-30 19:50:14 +0400 | [diff] [blame] | 760 | b_res->flags |= IORESOURCE_STARTALIGN; |
Yinghai Lu | b592443 | 2012-01-21 02:08:31 -0800 | [diff] [blame] | 761 | if (size1 > size0 && realloc_head) { |
Ram Pai | 9e8bf93 | 2011-07-25 13:08:42 -0700 | [diff] [blame] | 762 | add_to_list(realloc_head, bus->self, b_res, size1-size0, 4096); |
Yinghai Lu | b592443 | 2012-01-21 02:08:31 -0800 | [diff] [blame] | 763 | dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window " |
| 764 | "%pR to [bus %02x-%02x] add_size %lx\n", b_res, |
| 765 | bus->secondary, bus->subordinate, size1-size0); |
| 766 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | } |
| 768 | |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 769 | /** |
| 770 | * pbus_size_mem() - size the memory window of a given bus |
| 771 | * |
| 772 | * @bus : the bus |
| 773 | * @min_size : the minimum memory window that must to be allocated |
| 774 | * @add_size : additional optional memory window |
Ram Pai | 9e8bf93 | 2011-07-25 13:08:42 -0700 | [diff] [blame] | 775 | * @realloc_head : track the additional memory window on this list |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 776 | * |
| 777 | * Calculate the size of the bus and minimal alignment which |
| 778 | * guarantees that all child resources fit in this size. |
| 779 | */ |
Eric W. Biederman | 2876048 | 2009-09-09 14:09:24 -0700 | [diff] [blame] | 780 | static int pbus_size_mem(struct pci_bus *bus, unsigned long mask, |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 781 | unsigned long type, resource_size_t min_size, |
| 782 | resource_size_t add_size, |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 783 | struct list_head *realloc_head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | { |
| 785 | struct pci_dev *dev; |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 786 | resource_size_t min_align, align, size, size0, size1; |
Benjamin Herrenschmidt | c40a22e | 2007-12-10 17:32:15 +1100 | [diff] [blame] | 787 | resource_size_t aligns[12]; /* Alignments from 1Mb to 2Gb */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | int order, max_order; |
| 789 | struct resource *b_res = find_free_bus_resource(bus, type); |
Yinghai Lu | 1f82de1 | 2009-04-23 20:48:32 -0700 | [diff] [blame] | 790 | unsigned int mem64_mask = 0; |
Yinghai Lu | be76891 | 2011-07-25 13:08:38 -0700 | [diff] [blame] | 791 | resource_size_t children_add_size = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | |
| 793 | if (!b_res) |
| 794 | return 0; |
| 795 | |
| 796 | memset(aligns, 0, sizeof(aligns)); |
| 797 | max_order = 0; |
| 798 | size = 0; |
| 799 | |
Yinghai Lu | 1f82de1 | 2009-04-23 20:48:32 -0700 | [diff] [blame] | 800 | mem64_mask = b_res->flags & IORESOURCE_MEM_64; |
| 801 | b_res->flags &= ~IORESOURCE_MEM_64; |
| 802 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 804 | int i; |
Yinghai Lu | 1f82de1 | 2009-04-23 20:48:32 -0700 | [diff] [blame] | 805 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | for (i = 0; i < PCI_NUM_RESOURCES; i++) { |
| 807 | struct resource *r = &dev->resource[i]; |
Benjamin Herrenschmidt | c40a22e | 2007-12-10 17:32:15 +1100 | [diff] [blame] | 808 | resource_size_t r_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | |
| 810 | if (r->parent || (r->flags & mask) != type) |
| 811 | continue; |
Zhao, Yu | 022edd8 | 2008-10-13 19:24:28 +0800 | [diff] [blame] | 812 | r_size = resource_size(r); |
Yinghai Lu | 2aceefc | 2011-07-25 13:08:40 -0700 | [diff] [blame] | 813 | #ifdef CONFIG_PCI_IOV |
| 814 | /* put SRIOV requested res to the optional list */ |
Ram Pai | 9e8bf93 | 2011-07-25 13:08:42 -0700 | [diff] [blame] | 815 | if (realloc_head && i >= PCI_IOV_RESOURCES && |
Yinghai Lu | 2aceefc | 2011-07-25 13:08:40 -0700 | [diff] [blame] | 816 | i <= PCI_IOV_RESOURCE_END) { |
| 817 | r->end = r->start - 1; |
Ram Pai | 9e8bf93 | 2011-07-25 13:08:42 -0700 | [diff] [blame] | 818 | add_to_list(realloc_head, dev, r, r_size, 0/* dont' care */); |
Yinghai Lu | 2aceefc | 2011-07-25 13:08:40 -0700 | [diff] [blame] | 819 | children_add_size += r_size; |
| 820 | continue; |
| 821 | } |
| 822 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | /* For bridges size != alignment */ |
Chris Wright | 6faf17f | 2009-08-28 13:00:06 -0700 | [diff] [blame] | 824 | align = pci_resource_alignment(dev, r); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | order = __ffs(align) - 20; |
| 826 | if (order > 11) { |
Bjorn Helgaas | 865df57 | 2009-11-04 10:32:57 -0700 | [diff] [blame] | 827 | dev_warn(&dev->dev, "disabling BAR %d: %pR " |
| 828 | "(bad alignment %#llx)\n", i, r, |
| 829 | (unsigned long long) align); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | r->flags = 0; |
| 831 | continue; |
| 832 | } |
| 833 | size += r_size; |
| 834 | if (order < 0) |
| 835 | order = 0; |
| 836 | /* Exclude ranges with size > align from |
| 837 | calculation of the alignment. */ |
| 838 | if (r_size == align) |
| 839 | aligns[order] += align; |
| 840 | if (order > max_order) |
| 841 | max_order = order; |
Yinghai Lu | 1f82de1 | 2009-04-23 20:48:32 -0700 | [diff] [blame] | 842 | mem64_mask &= r->flags & IORESOURCE_MEM_64; |
Yinghai Lu | be76891 | 2011-07-25 13:08:38 -0700 | [diff] [blame] | 843 | |
Ram Pai | 9e8bf93 | 2011-07-25 13:08:42 -0700 | [diff] [blame] | 844 | if (realloc_head) |
| 845 | children_add_size += get_res_add_size(realloc_head, r); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | } |
| 847 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | align = 0; |
| 849 | min_align = 0; |
| 850 | for (order = 0; order <= max_order; order++) { |
Jeremy Fitzhardinge | 8308c54 | 2008-09-11 01:31:50 -0700 | [diff] [blame] | 851 | resource_size_t align1 = 1; |
| 852 | |
| 853 | align1 <<= (order + 20); |
| 854 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | if (!align) |
| 856 | min_align = align1; |
Milind Arun Choudhary | 6f6f8c2 | 2007-07-09 11:55:51 -0700 | [diff] [blame] | 857 | else if (ALIGN(align + min_align, min_align) < align1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | min_align = align1 >> 1; |
| 859 | align += aligns[order]; |
| 860 | } |
Linus Torvalds | b42282e | 2011-04-11 10:53:11 -0700 | [diff] [blame] | 861 | size0 = calculate_memsize(size, min_size, 0, resource_size(b_res), min_align); |
Yinghai Lu | be76891 | 2011-07-25 13:08:38 -0700 | [diff] [blame] | 862 | if (children_add_size > add_size) |
| 863 | add_size = children_add_size; |
Ram Pai | 9e8bf93 | 2011-07-25 13:08:42 -0700 | [diff] [blame] | 864 | size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 : |
Yinghai Lu | a4ac9fe | 2012-01-21 02:08:17 -0800 | [diff] [blame] | 865 | calculate_memsize(size, min_size, add_size, |
Linus Torvalds | b42282e | 2011-04-11 10:53:11 -0700 | [diff] [blame] | 866 | resource_size(b_res), min_align); |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 867 | if (!size0 && !size1) { |
Bjorn Helgaas | 865df57 | 2009-11-04 10:32:57 -0700 | [diff] [blame] | 868 | if (b_res->start || b_res->end) |
| 869 | dev_info(&bus->self->dev, "disabling bridge window " |
| 870 | "%pR to [bus %02x-%02x] (unused)\n", b_res, |
| 871 | bus->secondary, bus->subordinate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | b_res->flags = 0; |
| 873 | return 1; |
| 874 | } |
| 875 | b_res->start = min_align; |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 876 | b_res->end = size0 + min_align - 1; |
| 877 | b_res->flags |= IORESOURCE_STARTALIGN | mem64_mask; |
Yinghai Lu | b592443 | 2012-01-21 02:08:31 -0800 | [diff] [blame] | 878 | if (size1 > size0 && realloc_head) { |
Ram Pai | 9e8bf93 | 2011-07-25 13:08:42 -0700 | [diff] [blame] | 879 | add_to_list(realloc_head, bus->self, b_res, size1-size0, min_align); |
Yinghai Lu | b592443 | 2012-01-21 02:08:31 -0800 | [diff] [blame] | 880 | dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window " |
| 881 | "%pR to [bus %02x-%02x] add_size %llx\n", b_res, |
| 882 | bus->secondary, bus->subordinate, (unsigned long long)size1-size0); |
| 883 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | return 1; |
| 885 | } |
| 886 | |
Ram Pai | 0a2daa1 | 2011-07-25 13:08:41 -0700 | [diff] [blame] | 887 | unsigned long pci_cardbus_resource_alignment(struct resource *res) |
| 888 | { |
| 889 | if (res->flags & IORESOURCE_IO) |
| 890 | return pci_cardbus_io_size; |
| 891 | if (res->flags & IORESOURCE_MEM) |
| 892 | return pci_cardbus_mem_size; |
| 893 | return 0; |
| 894 | } |
| 895 | |
| 896 | static void pci_bus_size_cardbus(struct pci_bus *bus, |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 897 | struct list_head *realloc_head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | { |
| 899 | struct pci_dev *bridge = bus->self; |
| 900 | struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES]; |
Yinghai Lu | 1184893 | 2012-02-10 15:33:47 -0800 | [diff] [blame] | 901 | resource_size_t b_res_3_size = pci_cardbus_mem_size * 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | u16 ctrl; |
| 903 | |
Yinghai Lu | 3796f1e | 2012-02-10 15:33:48 -0800 | [diff] [blame] | 904 | if (b_res[0].parent) |
| 905 | goto handle_b_res_1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | /* |
| 907 | * Reserve some resources for CardBus. We reserve |
| 908 | * a fixed amount of bus space for CardBus bridges. |
| 909 | */ |
Yinghai Lu | 1184893 | 2012-02-10 15:33:47 -0800 | [diff] [blame] | 910 | b_res[0].start = pci_cardbus_io_size; |
| 911 | b_res[0].end = b_res[0].start + pci_cardbus_io_size - 1; |
| 912 | b_res[0].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN; |
| 913 | if (realloc_head) { |
| 914 | b_res[0].end -= pci_cardbus_io_size; |
| 915 | add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size, |
| 916 | pci_cardbus_io_size); |
| 917 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | |
Yinghai Lu | 3796f1e | 2012-02-10 15:33:48 -0800 | [diff] [blame] | 919 | handle_b_res_1: |
| 920 | if (b_res[1].parent) |
| 921 | goto handle_b_res_2; |
Yinghai Lu | 1184893 | 2012-02-10 15:33:47 -0800 | [diff] [blame] | 922 | b_res[1].start = pci_cardbus_io_size; |
| 923 | b_res[1].end = b_res[1].start + pci_cardbus_io_size - 1; |
| 924 | b_res[1].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN; |
| 925 | if (realloc_head) { |
| 926 | b_res[1].end -= pci_cardbus_io_size; |
| 927 | add_to_list(realloc_head, bridge, b_res+1, pci_cardbus_io_size, |
| 928 | pci_cardbus_io_size); |
| 929 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | |
Yinghai Lu | 3796f1e | 2012-02-10 15:33:48 -0800 | [diff] [blame] | 931 | handle_b_res_2: |
Yinghai Lu | dcef0d0 | 2012-02-10 15:33:46 -0800 | [diff] [blame] | 932 | /* MEM1 must not be pref mmio */ |
| 933 | pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); |
| 934 | if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM1) { |
| 935 | ctrl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1; |
| 936 | pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl); |
| 937 | pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); |
| 938 | } |
| 939 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | /* |
| 941 | * Check whether prefetchable memory is supported |
| 942 | * by this bridge. |
| 943 | */ |
| 944 | pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); |
| 945 | if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) { |
| 946 | ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0; |
| 947 | pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl); |
| 948 | pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); |
| 949 | } |
| 950 | |
Yinghai Lu | 3796f1e | 2012-02-10 15:33:48 -0800 | [diff] [blame] | 951 | if (b_res[2].parent) |
| 952 | goto handle_b_res_3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | /* |
| 954 | * If we have prefetchable memory support, allocate |
| 955 | * two regions. Otherwise, allocate one region of |
| 956 | * twice the size. |
| 957 | */ |
| 958 | if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) { |
Yinghai Lu | 1184893 | 2012-02-10 15:33:47 -0800 | [diff] [blame] | 959 | b_res[2].start = pci_cardbus_mem_size; |
| 960 | b_res[2].end = b_res[2].start + pci_cardbus_mem_size - 1; |
| 961 | b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | |
| 962 | IORESOURCE_STARTALIGN; |
| 963 | if (realloc_head) { |
| 964 | b_res[2].end -= pci_cardbus_mem_size; |
| 965 | add_to_list(realloc_head, bridge, b_res+2, |
| 966 | pci_cardbus_mem_size, pci_cardbus_mem_size); |
| 967 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | |
Yinghai Lu | 1184893 | 2012-02-10 15:33:47 -0800 | [diff] [blame] | 969 | /* reduce that to half */ |
| 970 | b_res_3_size = pci_cardbus_mem_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | } |
Ram Pai | 0a2daa1 | 2011-07-25 13:08:41 -0700 | [diff] [blame] | 972 | |
Yinghai Lu | 3796f1e | 2012-02-10 15:33:48 -0800 | [diff] [blame] | 973 | handle_b_res_3: |
| 974 | if (b_res[3].parent) |
| 975 | goto handle_done; |
Yinghai Lu | 1184893 | 2012-02-10 15:33:47 -0800 | [diff] [blame] | 976 | b_res[3].start = pci_cardbus_mem_size; |
| 977 | b_res[3].end = b_res[3].start + b_res_3_size - 1; |
| 978 | b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_STARTALIGN; |
| 979 | if (realloc_head) { |
| 980 | b_res[3].end -= b_res_3_size; |
| 981 | add_to_list(realloc_head, bridge, b_res+3, b_res_3_size, |
| 982 | pci_cardbus_mem_size); |
| 983 | } |
Yinghai Lu | 3796f1e | 2012-02-10 15:33:48 -0800 | [diff] [blame] | 984 | |
| 985 | handle_done: |
| 986 | ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | } |
| 988 | |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 989 | void __ref __pci_bus_size_bridges(struct pci_bus *bus, |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 990 | struct list_head *realloc_head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | { |
| 992 | struct pci_dev *dev; |
| 993 | unsigned long mask, prefmask; |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 994 | resource_size_t additional_mem_size = 0, additional_io_size = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | |
| 996 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 997 | struct pci_bus *b = dev->subordinate; |
| 998 | if (!b) |
| 999 | continue; |
| 1000 | |
| 1001 | switch (dev->class >> 8) { |
| 1002 | case PCI_CLASS_BRIDGE_CARDBUS: |
Ram Pai | 9e8bf93 | 2011-07-25 13:08:42 -0700 | [diff] [blame] | 1003 | pci_bus_size_cardbus(b, realloc_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | break; |
| 1005 | |
| 1006 | case PCI_CLASS_BRIDGE_PCI: |
| 1007 | default: |
Ram Pai | 9e8bf93 | 2011-07-25 13:08:42 -0700 | [diff] [blame] | 1008 | __pci_bus_size_bridges(b, realloc_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | break; |
| 1010 | } |
| 1011 | } |
| 1012 | |
| 1013 | /* The root bus? */ |
| 1014 | if (!bus->self) |
| 1015 | return; |
| 1016 | |
| 1017 | switch (bus->self->class >> 8) { |
| 1018 | case PCI_CLASS_BRIDGE_CARDBUS: |
| 1019 | /* don't size cardbuses yet. */ |
| 1020 | break; |
| 1021 | |
| 1022 | case PCI_CLASS_BRIDGE_PCI: |
| 1023 | pci_bridge_check_ranges(bus); |
Eric W. Biederman | 2876048 | 2009-09-09 14:09:24 -0700 | [diff] [blame] | 1024 | if (bus->self->is_hotplug_bridge) { |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 1025 | additional_io_size = pci_hotplug_io_size; |
| 1026 | additional_mem_size = pci_hotplug_mem_size; |
Eric W. Biederman | 2876048 | 2009-09-09 14:09:24 -0700 | [diff] [blame] | 1027 | } |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 1028 | /* |
| 1029 | * Follow thru |
| 1030 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | default: |
Yinghai Lu | 19aa7ee | 2012-01-21 02:08:24 -0800 | [diff] [blame] | 1032 | pbus_size_io(bus, realloc_head ? 0 : additional_io_size, |
| 1033 | additional_io_size, realloc_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | /* If the bridge supports prefetchable range, size it |
| 1035 | separately. If it doesn't, or its prefetchable window |
| 1036 | has already been allocated by arch code, try |
| 1037 | non-prefetchable range for both types of PCI memory |
| 1038 | resources. */ |
| 1039 | mask = IORESOURCE_MEM; |
| 1040 | prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH; |
Yinghai Lu | 19aa7ee | 2012-01-21 02:08:24 -0800 | [diff] [blame] | 1041 | if (pbus_size_mem(bus, prefmask, prefmask, |
| 1042 | realloc_head ? 0 : additional_mem_size, |
| 1043 | additional_mem_size, realloc_head)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | mask = prefmask; /* Success, size non-prefetch only. */ |
Eric W. Biederman | 2876048 | 2009-09-09 14:09:24 -0700 | [diff] [blame] | 1045 | else |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 1046 | additional_mem_size += additional_mem_size; |
Yinghai Lu | 19aa7ee | 2012-01-21 02:08:24 -0800 | [diff] [blame] | 1047 | pbus_size_mem(bus, mask, IORESOURCE_MEM, |
| 1048 | realloc_head ? 0 : additional_mem_size, |
| 1049 | additional_mem_size, realloc_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | break; |
| 1051 | } |
| 1052 | } |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 1053 | |
| 1054 | void __ref pci_bus_size_bridges(struct pci_bus *bus) |
| 1055 | { |
| 1056 | __pci_bus_size_bridges(bus, NULL); |
| 1057 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | EXPORT_SYMBOL(pci_bus_size_bridges); |
| 1059 | |
Yinghai Lu | 568ddef | 2010-01-22 01:02:21 -0800 | [diff] [blame] | 1060 | static void __ref __pci_bus_assign_resources(const struct pci_bus *bus, |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 1061 | struct list_head *realloc_head, |
| 1062 | struct list_head *fail_head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | { |
| 1064 | struct pci_bus *b; |
| 1065 | struct pci_dev *dev; |
| 1066 | |
Ram Pai | 9e8bf93 | 2011-07-25 13:08:42 -0700 | [diff] [blame] | 1067 | pbus_assign_resources_sorted(bus, realloc_head, fail_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 1070 | b = dev->subordinate; |
| 1071 | if (!b) |
| 1072 | continue; |
| 1073 | |
Ram Pai | 9e8bf93 | 2011-07-25 13:08:42 -0700 | [diff] [blame] | 1074 | __pci_bus_assign_resources(b, realloc_head, fail_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | |
| 1076 | switch (dev->class >> 8) { |
| 1077 | case PCI_CLASS_BRIDGE_PCI: |
Yinghai Lu | 6841ec6 | 2010-01-22 01:02:25 -0800 | [diff] [blame] | 1078 | if (!pci_is_enabled(dev)) |
| 1079 | pci_setup_bridge(b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | break; |
| 1081 | |
| 1082 | case PCI_CLASS_BRIDGE_CARDBUS: |
| 1083 | pci_setup_cardbus(b); |
| 1084 | break; |
| 1085 | |
| 1086 | default: |
Bjorn Helgaas | 80ccba1 | 2008-06-13 10:52:11 -0600 | [diff] [blame] | 1087 | dev_info(&dev->dev, "not setting up bridge for bus " |
| 1088 | "%04x:%02x\n", pci_domain_nr(b), b->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | break; |
| 1090 | } |
| 1091 | } |
| 1092 | } |
Yinghai Lu | 568ddef | 2010-01-22 01:02:21 -0800 | [diff] [blame] | 1093 | |
| 1094 | void __ref pci_bus_assign_resources(const struct pci_bus *bus) |
| 1095 | { |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 1096 | __pci_bus_assign_resources(bus, NULL, NULL); |
Yinghai Lu | 568ddef | 2010-01-22 01:02:21 -0800 | [diff] [blame] | 1097 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | EXPORT_SYMBOL(pci_bus_assign_resources); |
| 1099 | |
Yinghai Lu | 6841ec6 | 2010-01-22 01:02:25 -0800 | [diff] [blame] | 1100 | static void __ref __pci_bridge_assign_resources(const struct pci_dev *bridge, |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 1101 | struct list_head *add_head, |
| 1102 | struct list_head *fail_head) |
Yinghai Lu | 6841ec6 | 2010-01-22 01:02:25 -0800 | [diff] [blame] | 1103 | { |
| 1104 | struct pci_bus *b; |
| 1105 | |
Yinghai Lu | 8424d75 | 2012-01-21 02:08:21 -0800 | [diff] [blame] | 1106 | pdev_assign_resources_sorted((struct pci_dev *)bridge, |
| 1107 | add_head, fail_head); |
Yinghai Lu | 6841ec6 | 2010-01-22 01:02:25 -0800 | [diff] [blame] | 1108 | |
| 1109 | b = bridge->subordinate; |
| 1110 | if (!b) |
| 1111 | return; |
| 1112 | |
Yinghai Lu | 8424d75 | 2012-01-21 02:08:21 -0800 | [diff] [blame] | 1113 | __pci_bus_assign_resources(b, add_head, fail_head); |
Yinghai Lu | 6841ec6 | 2010-01-22 01:02:25 -0800 | [diff] [blame] | 1114 | |
| 1115 | switch (bridge->class >> 8) { |
| 1116 | case PCI_CLASS_BRIDGE_PCI: |
| 1117 | pci_setup_bridge(b); |
| 1118 | break; |
| 1119 | |
| 1120 | case PCI_CLASS_BRIDGE_CARDBUS: |
| 1121 | pci_setup_cardbus(b); |
| 1122 | break; |
| 1123 | |
| 1124 | default: |
| 1125 | dev_info(&bridge->dev, "not setting up bridge for bus " |
| 1126 | "%04x:%02x\n", pci_domain_nr(b), b->number); |
| 1127 | break; |
| 1128 | } |
| 1129 | } |
Yinghai Lu | 5009b46 | 2010-01-22 01:02:20 -0800 | [diff] [blame] | 1130 | static void pci_bridge_release_resources(struct pci_bus *bus, |
| 1131 | unsigned long type) |
| 1132 | { |
| 1133 | int idx; |
| 1134 | bool changed = false; |
| 1135 | struct pci_dev *dev; |
| 1136 | struct resource *r; |
| 1137 | unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | |
| 1138 | IORESOURCE_PREFETCH; |
| 1139 | |
| 1140 | dev = bus->self; |
| 1141 | for (idx = PCI_BRIDGE_RESOURCES; idx <= PCI_BRIDGE_RESOURCE_END; |
| 1142 | idx++) { |
| 1143 | r = &dev->resource[idx]; |
| 1144 | if ((r->flags & type_mask) != type) |
| 1145 | continue; |
| 1146 | if (!r->parent) |
| 1147 | continue; |
| 1148 | /* |
| 1149 | * if there are children under that, we should release them |
| 1150 | * all |
| 1151 | */ |
| 1152 | release_child_resources(r); |
| 1153 | if (!release_resource(r)) { |
| 1154 | dev_printk(KERN_DEBUG, &dev->dev, |
| 1155 | "resource %d %pR released\n", idx, r); |
| 1156 | /* keep the old size */ |
| 1157 | r->end = resource_size(r) - 1; |
| 1158 | r->start = 0; |
| 1159 | r->flags = 0; |
| 1160 | changed = true; |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | if (changed) { |
| 1165 | /* avoiding touch the one without PREF */ |
| 1166 | if (type & IORESOURCE_PREFETCH) |
| 1167 | type = IORESOURCE_PREFETCH; |
| 1168 | __pci_setup_bridge(bus, type); |
| 1169 | } |
| 1170 | } |
| 1171 | |
| 1172 | enum release_type { |
| 1173 | leaf_only, |
| 1174 | whole_subtree, |
| 1175 | }; |
| 1176 | /* |
| 1177 | * try to release pci bridge resources that is from leaf bridge, |
| 1178 | * so we can allocate big new one later |
| 1179 | */ |
| 1180 | static void __ref pci_bus_release_bridge_resources(struct pci_bus *bus, |
| 1181 | unsigned long type, |
| 1182 | enum release_type rel_type) |
| 1183 | { |
| 1184 | struct pci_dev *dev; |
| 1185 | bool is_leaf_bridge = true; |
| 1186 | |
| 1187 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 1188 | struct pci_bus *b = dev->subordinate; |
| 1189 | if (!b) |
| 1190 | continue; |
| 1191 | |
| 1192 | is_leaf_bridge = false; |
| 1193 | |
| 1194 | if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI) |
| 1195 | continue; |
| 1196 | |
| 1197 | if (rel_type == whole_subtree) |
| 1198 | pci_bus_release_bridge_resources(b, type, |
| 1199 | whole_subtree); |
| 1200 | } |
| 1201 | |
| 1202 | if (pci_is_root_bus(bus)) |
| 1203 | return; |
| 1204 | |
| 1205 | if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI) |
| 1206 | return; |
| 1207 | |
| 1208 | if ((rel_type == whole_subtree) || is_leaf_bridge) |
| 1209 | pci_bridge_release_resources(bus, type); |
| 1210 | } |
| 1211 | |
Yinghai Lu | 76fbc26 | 2008-06-23 20:33:06 +0200 | [diff] [blame] | 1212 | static void pci_bus_dump_res(struct pci_bus *bus) |
| 1213 | { |
Bjorn Helgaas | 89a74ec | 2010-02-23 10:24:31 -0700 | [diff] [blame] | 1214 | struct resource *res; |
| 1215 | int i; |
Yinghai Lu | 76fbc26 | 2008-06-23 20:33:06 +0200 | [diff] [blame] | 1216 | |
Bjorn Helgaas | 89a74ec | 2010-02-23 10:24:31 -0700 | [diff] [blame] | 1217 | pci_bus_for_each_resource(bus, res, i) { |
Yinghai Lu | 7c9342b | 2009-12-22 15:02:24 -0800 | [diff] [blame] | 1218 | if (!res || !res->end || !res->flags) |
Yinghai Lu | 76fbc26 | 2008-06-23 20:33:06 +0200 | [diff] [blame] | 1219 | continue; |
| 1220 | |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 1221 | dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res); |
Yinghai Lu | 76fbc26 | 2008-06-23 20:33:06 +0200 | [diff] [blame] | 1222 | } |
| 1223 | } |
| 1224 | |
| 1225 | static void pci_bus_dump_resources(struct pci_bus *bus) |
| 1226 | { |
| 1227 | struct pci_bus *b; |
| 1228 | struct pci_dev *dev; |
| 1229 | |
| 1230 | |
| 1231 | pci_bus_dump_res(bus); |
| 1232 | |
| 1233 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 1234 | b = dev->subordinate; |
| 1235 | if (!b) |
| 1236 | continue; |
| 1237 | |
| 1238 | pci_bus_dump_resources(b); |
| 1239 | } |
| 1240 | } |
| 1241 | |
Yinghai Lu | da7822e | 2011-05-12 17:11:37 -0700 | [diff] [blame] | 1242 | static int __init pci_bus_get_depth(struct pci_bus *bus) |
| 1243 | { |
| 1244 | int depth = 0; |
| 1245 | struct pci_dev *dev; |
| 1246 | |
| 1247 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 1248 | int ret; |
| 1249 | struct pci_bus *b = dev->subordinate; |
| 1250 | if (!b) |
| 1251 | continue; |
| 1252 | |
| 1253 | ret = pci_bus_get_depth(b); |
| 1254 | if (ret + 1 > depth) |
| 1255 | depth = ret + 1; |
| 1256 | } |
| 1257 | |
| 1258 | return depth; |
| 1259 | } |
| 1260 | static int __init pci_get_max_depth(void) |
| 1261 | { |
| 1262 | int depth = 0; |
| 1263 | struct pci_bus *bus; |
| 1264 | |
| 1265 | list_for_each_entry(bus, &pci_root_buses, node) { |
| 1266 | int ret; |
| 1267 | |
| 1268 | ret = pci_bus_get_depth(bus); |
| 1269 | if (ret > depth) |
| 1270 | depth = ret; |
| 1271 | } |
| 1272 | |
| 1273 | return depth; |
| 1274 | } |
| 1275 | |
Ram Pai | f483d39 | 2011-07-07 11:19:10 -0700 | [diff] [blame] | 1276 | |
Yinghai Lu | da7822e | 2011-05-12 17:11:37 -0700 | [diff] [blame] | 1277 | /* |
| 1278 | * first try will not touch pci bridge res |
| 1279 | * second and later try will clear small leaf bridge res |
| 1280 | * will stop till to the max deepth if can not find good one |
| 1281 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | void __init |
| 1283 | pci_assign_unassigned_resources(void) |
| 1284 | { |
| 1285 | struct pci_bus *bus; |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 1286 | LIST_HEAD(realloc_head); /* list of resources that |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 1287 | want additional resources */ |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 1288 | struct list_head *add_list = NULL; |
Yinghai Lu | da7822e | 2011-05-12 17:11:37 -0700 | [diff] [blame] | 1289 | int tried_times = 0; |
| 1290 | enum release_type rel_type = leaf_only; |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 1291 | LIST_HEAD(fail_head); |
Yinghai Lu | b9b0bba | 2012-01-21 02:08:29 -0800 | [diff] [blame] | 1292 | struct pci_dev_resource *fail_res; |
Yinghai Lu | da7822e | 2011-05-12 17:11:37 -0700 | [diff] [blame] | 1293 | unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | |
| 1294 | IORESOURCE_PREFETCH; |
Yinghai Lu | 19aa7ee | 2012-01-21 02:08:24 -0800 | [diff] [blame] | 1295 | int pci_try_num = 1; |
Yinghai Lu | da7822e | 2011-05-12 17:11:37 -0700 | [diff] [blame] | 1296 | |
Yinghai Lu | 19aa7ee | 2012-01-21 02:08:24 -0800 | [diff] [blame] | 1297 | /* don't realloc if asked to do so */ |
| 1298 | if (pci_realloc_enabled()) { |
| 1299 | int max_depth = pci_get_max_depth(); |
| 1300 | |
| 1301 | pci_try_num = max_depth + 1; |
| 1302 | printk(KERN_DEBUG "PCI: max bus depth: %d pci_try_num: %d\n", |
| 1303 | max_depth, pci_try_num); |
| 1304 | } |
Yinghai Lu | da7822e | 2011-05-12 17:11:37 -0700 | [diff] [blame] | 1305 | |
| 1306 | again: |
Yinghai Lu | 19aa7ee | 2012-01-21 02:08:24 -0800 | [diff] [blame] | 1307 | /* |
| 1308 | * last try will use add_list, otherwise will try good to have as |
| 1309 | * must have, so can realloc parent bridge resource |
| 1310 | */ |
| 1311 | if (tried_times + 1 == pci_try_num) |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 1312 | add_list = &realloc_head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | /* Depth first, calculate sizes and alignments of all |
| 1314 | subordinate buses. */ |
Yinghai Lu | da7822e | 2011-05-12 17:11:37 -0700 | [diff] [blame] | 1315 | list_for_each_entry(bus, &pci_root_buses, node) |
Yinghai Lu | 19aa7ee | 2012-01-21 02:08:24 -0800 | [diff] [blame] | 1316 | __pci_bus_size_bridges(bus, add_list); |
Ram Pai | c8adf9a | 2011-02-14 17:43:20 -0800 | [diff] [blame] | 1317 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | /* Depth last, allocate resources and update the hardware. */ |
Yinghai Lu | da7822e | 2011-05-12 17:11:37 -0700 | [diff] [blame] | 1319 | list_for_each_entry(bus, &pci_root_buses, node) |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 1320 | __pci_bus_assign_resources(bus, add_list, &fail_head); |
Yinghai Lu | 19aa7ee | 2012-01-21 02:08:24 -0800 | [diff] [blame] | 1321 | if (add_list) |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 1322 | BUG_ON(!list_empty(add_list)); |
Yinghai Lu | da7822e | 2011-05-12 17:11:37 -0700 | [diff] [blame] | 1323 | tried_times++; |
| 1324 | |
| 1325 | /* any device complain? */ |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 1326 | if (list_empty(&fail_head)) |
Yinghai Lu | da7822e | 2011-05-12 17:11:37 -0700 | [diff] [blame] | 1327 | goto enable_and_dump; |
Ram Pai | f483d39 | 2011-07-07 11:19:10 -0700 | [diff] [blame] | 1328 | |
Yinghai Lu | 0c5be0c | 2012-02-23 19:23:29 -0800 | [diff] [blame^] | 1329 | if (tried_times >= pci_try_num) { |
Yinghai Lu | bffc56d | 2012-01-21 02:08:30 -0800 | [diff] [blame] | 1330 | free_list(&fail_head); |
Yinghai Lu | da7822e | 2011-05-12 17:11:37 -0700 | [diff] [blame] | 1331 | goto enable_and_dump; |
| 1332 | } |
| 1333 | |
| 1334 | printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n", |
| 1335 | tried_times + 1); |
| 1336 | |
| 1337 | /* third times and later will not check if it is leaf */ |
| 1338 | if ((tried_times + 1) > 2) |
| 1339 | rel_type = whole_subtree; |
| 1340 | |
| 1341 | /* |
| 1342 | * Try to release leaf bridge's resources that doesn't fit resource of |
| 1343 | * child device under that bridge |
| 1344 | */ |
Yinghai Lu | b9b0bba | 2012-01-21 02:08:29 -0800 | [diff] [blame] | 1345 | list_for_each_entry(fail_res, &fail_head, list) { |
| 1346 | bus = fail_res->dev->bus; |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 1347 | pci_bus_release_bridge_resources(bus, |
Yinghai Lu | b9b0bba | 2012-01-21 02:08:29 -0800 | [diff] [blame] | 1348 | fail_res->flags & type_mask, |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 1349 | rel_type); |
Yinghai Lu | da7822e | 2011-05-12 17:11:37 -0700 | [diff] [blame] | 1350 | } |
| 1351 | /* restore size and flags */ |
Yinghai Lu | b9b0bba | 2012-01-21 02:08:29 -0800 | [diff] [blame] | 1352 | list_for_each_entry(fail_res, &fail_head, list) { |
| 1353 | struct resource *res = fail_res->res; |
Yinghai Lu | da7822e | 2011-05-12 17:11:37 -0700 | [diff] [blame] | 1354 | |
Yinghai Lu | b9b0bba | 2012-01-21 02:08:29 -0800 | [diff] [blame] | 1355 | res->start = fail_res->start; |
| 1356 | res->end = fail_res->end; |
| 1357 | res->flags = fail_res->flags; |
| 1358 | if (fail_res->dev->subordinate) |
Yinghai Lu | da7822e | 2011-05-12 17:11:37 -0700 | [diff] [blame] | 1359 | res->flags = 0; |
Yinghai Lu | da7822e | 2011-05-12 17:11:37 -0700 | [diff] [blame] | 1360 | } |
Yinghai Lu | bffc56d | 2012-01-21 02:08:30 -0800 | [diff] [blame] | 1361 | free_list(&fail_head); |
Yinghai Lu | da7822e | 2011-05-12 17:11:37 -0700 | [diff] [blame] | 1362 | |
| 1363 | goto again; |
| 1364 | |
| 1365 | enable_and_dump: |
| 1366 | /* Depth last, update the hardware. */ |
| 1367 | list_for_each_entry(bus, &pci_root_buses, node) |
| 1368 | pci_enable_bridges(bus); |
Yinghai Lu | 76fbc26 | 2008-06-23 20:33:06 +0200 | [diff] [blame] | 1369 | |
| 1370 | /* dump the resource on buses */ |
Yinghai Lu | da7822e | 2011-05-12 17:11:37 -0700 | [diff] [blame] | 1371 | list_for_each_entry(bus, &pci_root_buses, node) |
Yinghai Lu | 76fbc26 | 2008-06-23 20:33:06 +0200 | [diff] [blame] | 1372 | pci_bus_dump_resources(bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | } |
Yinghai Lu | 6841ec6 | 2010-01-22 01:02:25 -0800 | [diff] [blame] | 1374 | |
| 1375 | void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge) |
| 1376 | { |
| 1377 | struct pci_bus *parent = bridge->subordinate; |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 1378 | LIST_HEAD(add_list); /* list of resources that |
Yinghai Lu | 8424d75 | 2012-01-21 02:08:21 -0800 | [diff] [blame] | 1379 | want additional resources */ |
Yinghai Lu | 32180e4 | 2010-01-22 01:02:27 -0800 | [diff] [blame] | 1380 | int tried_times = 0; |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 1381 | LIST_HEAD(fail_head); |
Yinghai Lu | b9b0bba | 2012-01-21 02:08:29 -0800 | [diff] [blame] | 1382 | struct pci_dev_resource *fail_res; |
Yinghai Lu | 6841ec6 | 2010-01-22 01:02:25 -0800 | [diff] [blame] | 1383 | int retval; |
Yinghai Lu | 32180e4 | 2010-01-22 01:02:27 -0800 | [diff] [blame] | 1384 | unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | |
| 1385 | IORESOURCE_PREFETCH; |
Yinghai Lu | 6841ec6 | 2010-01-22 01:02:25 -0800 | [diff] [blame] | 1386 | |
Yinghai Lu | 32180e4 | 2010-01-22 01:02:27 -0800 | [diff] [blame] | 1387 | again: |
Yinghai Lu | 8424d75 | 2012-01-21 02:08:21 -0800 | [diff] [blame] | 1388 | __pci_bus_size_bridges(parent, &add_list); |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 1389 | __pci_bridge_assign_resources(bridge, &add_list, &fail_head); |
| 1390 | BUG_ON(!list_empty(&add_list)); |
Yinghai Lu | 32180e4 | 2010-01-22 01:02:27 -0800 | [diff] [blame] | 1391 | tried_times++; |
| 1392 | |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 1393 | if (list_empty(&fail_head)) |
Yinghai Lu | 3f579c3 | 2010-05-21 14:35:06 -0700 | [diff] [blame] | 1394 | goto enable_all; |
Yinghai Lu | 32180e4 | 2010-01-22 01:02:27 -0800 | [diff] [blame] | 1395 | |
| 1396 | if (tried_times >= 2) { |
| 1397 | /* still fail, don't need to try more */ |
Yinghai Lu | bffc56d | 2012-01-21 02:08:30 -0800 | [diff] [blame] | 1398 | free_list(&fail_head); |
Yinghai Lu | 3f579c3 | 2010-05-21 14:35:06 -0700 | [diff] [blame] | 1399 | goto enable_all; |
Yinghai Lu | 32180e4 | 2010-01-22 01:02:27 -0800 | [diff] [blame] | 1400 | } |
| 1401 | |
| 1402 | printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n", |
| 1403 | tried_times + 1); |
| 1404 | |
| 1405 | /* |
| 1406 | * Try to release leaf bridge's resources that doesn't fit resource of |
| 1407 | * child device under that bridge |
| 1408 | */ |
Yinghai Lu | b9b0bba | 2012-01-21 02:08:29 -0800 | [diff] [blame] | 1409 | list_for_each_entry(fail_res, &fail_head, list) { |
| 1410 | struct pci_bus *bus = fail_res->dev->bus; |
| 1411 | unsigned long flags = fail_res->flags; |
Yinghai Lu | 32180e4 | 2010-01-22 01:02:27 -0800 | [diff] [blame] | 1412 | |
| 1413 | pci_bus_release_bridge_resources(bus, flags & type_mask, |
| 1414 | whole_subtree); |
Yinghai Lu | 32180e4 | 2010-01-22 01:02:27 -0800 | [diff] [blame] | 1415 | } |
| 1416 | /* restore size and flags */ |
Yinghai Lu | b9b0bba | 2012-01-21 02:08:29 -0800 | [diff] [blame] | 1417 | list_for_each_entry(fail_res, &fail_head, list) { |
| 1418 | struct resource *res = fail_res->res; |
Yinghai Lu | 32180e4 | 2010-01-22 01:02:27 -0800 | [diff] [blame] | 1419 | |
Yinghai Lu | b9b0bba | 2012-01-21 02:08:29 -0800 | [diff] [blame] | 1420 | res->start = fail_res->start; |
| 1421 | res->end = fail_res->end; |
| 1422 | res->flags = fail_res->flags; |
| 1423 | if (fail_res->dev->subordinate) |
Yinghai Lu | 32180e4 | 2010-01-22 01:02:27 -0800 | [diff] [blame] | 1424 | res->flags = 0; |
Yinghai Lu | 32180e4 | 2010-01-22 01:02:27 -0800 | [diff] [blame] | 1425 | } |
Yinghai Lu | bffc56d | 2012-01-21 02:08:30 -0800 | [diff] [blame] | 1426 | free_list(&fail_head); |
Yinghai Lu | 32180e4 | 2010-01-22 01:02:27 -0800 | [diff] [blame] | 1427 | |
| 1428 | goto again; |
Yinghai Lu | 3f579c3 | 2010-05-21 14:35:06 -0700 | [diff] [blame] | 1429 | |
| 1430 | enable_all: |
| 1431 | retval = pci_reenable_device(bridge); |
| 1432 | pci_set_master(bridge); |
| 1433 | pci_enable_bridges(parent); |
Yinghai Lu | 6841ec6 | 2010-01-22 01:02:25 -0800 | [diff] [blame] | 1434 | } |
| 1435 | EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources); |
Yinghai Lu | 9b03088 | 2012-01-21 02:08:23 -0800 | [diff] [blame] | 1436 | |
| 1437 | #ifdef CONFIG_HOTPLUG |
| 1438 | /** |
| 1439 | * pci_rescan_bus - scan a PCI bus for devices. |
| 1440 | * @bus: PCI bus to scan |
| 1441 | * |
| 1442 | * Scan a PCI bus and child buses for new devices, adds them, |
| 1443 | * and enables them. |
| 1444 | * |
| 1445 | * Returns the max number of subordinate bus discovered. |
| 1446 | */ |
| 1447 | unsigned int __ref pci_rescan_bus(struct pci_bus *bus) |
| 1448 | { |
| 1449 | unsigned int max; |
| 1450 | struct pci_dev *dev; |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 1451 | LIST_HEAD(add_list); /* list of resources that |
Yinghai Lu | 9b03088 | 2012-01-21 02:08:23 -0800 | [diff] [blame] | 1452 | want additional resources */ |
| 1453 | |
| 1454 | max = pci_scan_child_bus(bus); |
| 1455 | |
Yinghai Lu | 9b03088 | 2012-01-21 02:08:23 -0800 | [diff] [blame] | 1456 | down_read(&pci_bus_sem); |
| 1457 | list_for_each_entry(dev, &bus->devices, bus_list) |
| 1458 | if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE || |
| 1459 | dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) |
| 1460 | if (dev->subordinate) |
| 1461 | __pci_bus_size_bridges(dev->subordinate, |
| 1462 | &add_list); |
| 1463 | up_read(&pci_bus_sem); |
| 1464 | __pci_bus_assign_resources(bus, &add_list, NULL); |
Yinghai Lu | bdc4abe | 2012-01-21 02:08:27 -0800 | [diff] [blame] | 1465 | BUG_ON(!list_empty(&add_list)); |
Yinghai Lu | 9b03088 | 2012-01-21 02:08:23 -0800 | [diff] [blame] | 1466 | |
| 1467 | pci_enable_bridges(bus); |
| 1468 | pci_bus_add_devices(bus); |
| 1469 | |
| 1470 | return max; |
| 1471 | } |
| 1472 | EXPORT_SYMBOL_GPL(pci_rescan_bus); |
| 1473 | #endif |