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