blob: 219722df68d6f294489b981a97821bd2307bd4e6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Wright6faf17f2009-08-28 13:00:06 -070028#include "pci.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Yinghai Lubdc4abe2012-01-21 02:08:27 -080030struct pci_dev_resource {
31 struct list_head list;
Yinghai Lu2934a0d2012-01-21 02:08:26 -080032 struct resource *res;
33 struct pci_dev *dev;
Yinghai Lu568ddef2010-01-22 01:02:21 -080034 resource_size_t start;
35 resource_size_t end;
Ram Paic8adf9a2011-02-14 17:43:20 -080036 resource_size_t add_size;
Ram Pai2bbc6942011-07-25 13:08:39 -070037 resource_size_t min_align;
Yinghai Lu568ddef2010-01-22 01:02:21 -080038 unsigned long flags;
39};
40
Yinghai Lubffc56d2012-01-21 02:08:30 -080041static 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 Pai094732a2011-02-14 17:43:18 -080050
Ram Paic8adf9a2011-02-14 17:43:20 -080051/**
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 Lubdc4abe2012-01-21 02:08:27 -080060static int add_to_list(struct list_head *head,
Ram Paic8adf9a2011-02-14 17:43:20 -080061 struct pci_dev *dev, struct resource *res,
Ram Pai2bbc6942011-07-25 13:08:39 -070062 resource_size_t add_size, resource_size_t min_align)
Yinghai Lu568ddef2010-01-22 01:02:21 -080063{
Yinghai Lu764242a2012-01-21 02:08:28 -080064 struct pci_dev_resource *tmp;
Yinghai Lu568ddef2010-01-22 01:02:21 -080065
Yinghai Lubdc4abe2012-01-21 02:08:27 -080066 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
Yinghai Lu568ddef2010-01-22 01:02:21 -080067 if (!tmp) {
Ram Paic8adf9a2011-02-14 17:43:20 -080068 pr_warning("add_to_list: kmalloc() failed!\n");
Yinghai Luef62dfe2012-01-21 02:08:18 -080069 return -ENOMEM;
Yinghai Lu568ddef2010-01-22 01:02:21 -080070 }
71
Yinghai Lu568ddef2010-01-22 01:02:21 -080072 tmp->res = res;
73 tmp->dev = dev;
74 tmp->start = res->start;
75 tmp->end = res->end;
76 tmp->flags = res->flags;
Ram Paic8adf9a2011-02-14 17:43:20 -080077 tmp->add_size = add_size;
Ram Pai2bbc6942011-07-25 13:08:39 -070078 tmp->min_align = min_align;
Yinghai Lubdc4abe2012-01-21 02:08:27 -080079
80 list_add(&tmp->list, head);
Yinghai Luef62dfe2012-01-21 02:08:18 -080081
82 return 0;
Yinghai Lu568ddef2010-01-22 01:02:21 -080083}
84
Yinghai Lub9b0bba2012-01-21 02:08:29 -080085static void remove_from_list(struct list_head *head,
Yinghai Lu3e6e0d82012-01-21 02:08:20 -080086 struct resource *res)
87{
Yinghai Lub9b0bba2012-01-21 02:08:29 -080088 struct pci_dev_resource *dev_res, *tmp;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -080089
Yinghai Lub9b0bba2012-01-21 02:08:29 -080090 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 Lubdc4abe2012-01-21 02:08:27 -080094 break;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -080095 }
Yinghai Lu3e6e0d82012-01-21 02:08:20 -080096 }
97}
98
Yinghai Lub9b0bba2012-01-21 02:08:29 -080099static resource_size_t get_res_add_size(struct list_head *head,
Yinghai Lu1c372352012-01-21 02:08:19 -0800100 struct resource *res)
101{
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800102 struct pci_dev_resource *dev_res;
Yinghai Lu1c372352012-01-21 02:08:19 -0800103
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800104 list_for_each_entry(dev_res, head, list) {
105 if (dev_res->res == res) {
Yinghai Lub5924432012-01-21 02:08:31 -0800106 int idx = res - &dev_res->dev->resource[0];
107
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800108 dev_printk(KERN_DEBUG, &dev_res->dev->dev,
Yinghai Lub5924432012-01-21 02:08:31 -0800109 "res[%d]=%pR get_res_add_size add_size %llx\n",
110 idx, dev_res->res,
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800111 (unsigned long long)dev_res->add_size);
Yinghai Lub5924432012-01-21 02:08:31 -0800112
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800113 return dev_res->add_size;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800114 }
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800115 }
Yinghai Lu1c372352012-01-21 02:08:19 -0800116
117 return 0;
118}
119
Yinghai Lu78c3b322012-01-21 02:08:25 -0800120/* Sort resources by alignment */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800121static void pdev_sort_resources(struct pci_dev *dev, struct list_head *head)
Yinghai Lu78c3b322012-01-21 02:08:25 -0800122{
123 int i;
124
125 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
126 struct resource *r;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800127 struct pci_dev_resource *dev_res, *tmp;
Yinghai Lu78c3b322012-01-21 02:08:25 -0800128 resource_size_t r_align;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800129 struct list_head *n;
Yinghai Lu78c3b322012-01-21 02:08:25 -0800130
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 Lu78c3b322012-01-21 02:08:25 -0800145
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800146 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 Lu78c3b322012-01-21 02:08:25 -0800160
161 if (r_align > align) {
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800162 n = &dev_res->list;
Yinghai Lu78c3b322012-01-21 02:08:25 -0800163 break;
164 }
165 }
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800166 /* Insert it just before n*/
167 list_add_tail(&tmp->list, n);
Yinghai Lu78c3b322012-01-21 02:08:25 -0800168 }
169}
170
Yinghai Lu6841ec62010-01-22 01:02:25 -0800171static void __dev_sort_resources(struct pci_dev *dev,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800172 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Yinghai Lu6841ec62010-01-22 01:02:25 -0800174 u16 class = dev->class >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Yinghai Lu6841ec62010-01-22 01:02:25 -0800176 /* 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 Torvalds1da177e2005-04-16 15:20:36 -0700179
Yinghai Lu6841ec62010-01-22 01:02:25 -0800180 /* 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 Torvalds1da177e2005-04-16 15:20:36 -0700186 }
187
Yinghai Lu6841ec62010-01-22 01:02:25 -0800188 pdev_sort_resources(dev, head);
189}
190
Ram Paifc075e12011-02-14 17:43:19 -0800191static inline void reset_resource(struct resource *res)
192{
193 res->start = 0;
194 res->end = 0;
195 res->flags = 0;
196}
197
Ram Paic8adf9a2011-02-14 17:43:20 -0800198/**
Ram Pai9e8bf932011-07-25 13:08:42 -0700199 * reassign_resources_sorted() - satisfy any additional resource requests
Ram Paic8adf9a2011-02-14 17:43:20 -0800200 *
Ram Pai9e8bf932011-07-25 13:08:42 -0700201 * @realloc_head : head of the list tracking requests requiring additional
Ram Paic8adf9a2011-02-14 17:43:20 -0800202 * resources
203 * @head : head of the list tracking requests with allocated
204 * resources
205 *
Ram Pai9e8bf932011-07-25 13:08:42 -0700206 * Walk through each element of the realloc_head and try to procure
Ram Paic8adf9a2011-02-14 17:43:20 -0800207 * additional resources for the element, provided the element
208 * is in the head list.
209 */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800210static void reassign_resources_sorted(struct list_head *realloc_head,
211 struct list_head *head)
Ram Paic8adf9a2011-02-14 17:43:20 -0800212{
213 struct resource *res;
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800214 struct pci_dev_resource *add_res, *tmp;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800215 struct pci_dev_resource *dev_res;
Ram Paic8adf9a2011-02-14 17:43:20 -0800216 resource_size_t add_size;
217 int idx;
218
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800219 list_for_each_entry_safe(add_res, tmp, realloc_head, list) {
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800220 bool found_match = false;
221
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800222 res = add_res->res;
Ram Paic8adf9a2011-02-14 17:43:20 -0800223 /* 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 Lubdc4abe2012-01-21 02:08:27 -0800228 list_for_each_entry(dev_res, head, list) {
229 if (dev_res->res == res) {
230 found_match = true;
231 break;
232 }
Ram Paic8adf9a2011-02-14 17:43:20 -0800233 }
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800234 if (!found_match)/* just skip */
235 continue;
Ram Paic8adf9a2011-02-14 17:43:20 -0800236
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800237 idx = res - &add_res->dev->resource[0];
238 add_size = add_res->add_size;
Ram Pai2bbc6942011-07-25 13:08:39 -0700239 if (!resource_size(res)) {
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800240 res->start = add_res->start;
Ram Pai2bbc6942011-07-25 13:08:39 -0700241 res->end = res->start + add_size - 1;
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800242 if (pci_assign_resource(add_res->dev, idx))
Ram Paic8adf9a2011-02-14 17:43:20 -0800243 reset_resource(res);
Ram Pai2bbc6942011-07-25 13:08:39 -0700244 } else {
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800245 resource_size_t align = add_res->min_align;
246 res->flags |= add_res->flags &
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800247 (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN);
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800248 if (pci_reassign_resource(add_res->dev, idx,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800249 add_size, align))
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800250 dev_printk(KERN_DEBUG, &add_res->dev->dev,
Yinghai Lub5924432012-01-21 02:08:31 -0800251 "failed to add %llx res[%d]=%pR\n",
252 (unsigned long long)add_size,
253 idx, res);
Ram Paic8adf9a2011-02-14 17:43:20 -0800254 }
255out:
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800256 list_del(&add_res->list);
257 kfree(add_res);
Ram Paic8adf9a2011-02-14 17:43:20 -0800258 }
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 Lubdc4abe2012-01-21 02:08:27 -0800271static void assign_requested_resources_sorted(struct list_head *head,
272 struct list_head *fail_head)
Yinghai Lu6841ec62010-01-22 01:02:25 -0800273{
274 struct resource *res;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800275 struct pci_dev_resource *dev_res;
Yinghai Lu6841ec62010-01-22 01:02:25 -0800276 int idx;
277
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800278 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 Lu9a928662010-02-28 15:49:39 -0800284 /*
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 Lu67cc7e22012-01-21 02:08:32 -0800290 add_to_list(fail_head,
291 dev_res->dev, res,
292 0 /* dont care */,
293 0 /* dont care */);
Yinghai Lu9a928662010-02-28 15:49:39 -0800294 }
Ram Paifc075e12011-02-14 17:43:19 -0800295 reset_resource(res);
Rajesh Shah542df5d2005-04-28 00:25:50 -0700296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
298}
299
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800300static void __assign_resources_sorted(struct list_head *head,
301 struct list_head *realloc_head,
302 struct list_head *fail_head)
Ram Paic8adf9a2011-02-14 17:43:20 -0800303{
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800304 /*
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 Lubdc4abe2012-01-21 02:08:27 -0800313 LIST_HEAD(save_head);
314 LIST_HEAD(local_fail_head);
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800315 struct pci_dev_resource *save_res;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800316 struct pci_dev_resource *dev_res;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800317
318 /* Check if optional add_size is there */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800319 if (!realloc_head || list_empty(realloc_head))
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800320 goto requested_and_reassign;
321
322 /* Save original start, end, flags etc at first */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800323 list_for_each_entry(dev_res, head, list) {
324 if (add_to_list(&save_head, dev_res->dev, dev_res->res, 0, 0)) {
Yinghai Lubffc56d2012-01-21 02:08:30 -0800325 free_list(&save_head);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800326 goto requested_and_reassign;
327 }
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800328 }
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800329
330 /* Update res in head list with add_size in realloc_head list */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800331 list_for_each_entry(dev_res, head, list)
332 dev_res->res->end += get_res_add_size(realloc_head,
333 dev_res->res);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800334
335 /* Try updated head list with add_size added */
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800336 assign_requested_resources_sorted(head, &local_fail_head);
337
338 /* all assigned with add_size ? */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800339 if (list_empty(&local_fail_head)) {
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800340 /* Remove head list from realloc_head list */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800341 list_for_each_entry(dev_res, head, list)
342 remove_from_list(realloc_head, dev_res->res);
Yinghai Lubffc56d2012-01-21 02:08:30 -0800343 free_list(&save_head);
344 free_list(head);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800345 return;
346 }
347
Yinghai Lubffc56d2012-01-21 02:08:30 -0800348 free_list(&local_fail_head);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800349 /* Release assigned resource */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800350 list_for_each_entry(dev_res, head, list)
351 if (dev_res->res->parent)
352 release_resource(dev_res->res);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800353 /* Restore start/end/flags from saved list */
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800354 list_for_each_entry(save_res, &save_head, list) {
355 struct resource *res = save_res->res;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800356
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800357 res->start = save_res->start;
358 res->end = save_res->end;
359 res->flags = save_res->flags;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800360 }
Yinghai Lubffc56d2012-01-21 02:08:30 -0800361 free_list(&save_head);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800362
363requested_and_reassign:
Ram Paic8adf9a2011-02-14 17:43:20 -0800364 /* Satisfy the must-have resource requests */
365 assign_requested_resources_sorted(head, fail_head);
366
Ram Pai0a2daa12011-07-25 13:08:41 -0700367 /* Try to satisfy any additional optional resource
Ram Paic8adf9a2011-02-14 17:43:20 -0800368 requests */
Ram Pai9e8bf932011-07-25 13:08:42 -0700369 if (realloc_head)
370 reassign_resources_sorted(realloc_head, head);
Yinghai Lubffc56d2012-01-21 02:08:30 -0800371 free_list(head);
Ram Paic8adf9a2011-02-14 17:43:20 -0800372}
373
Yinghai Lu6841ec62010-01-22 01:02:25 -0800374static void pdev_assign_resources_sorted(struct pci_dev *dev,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800375 struct list_head *add_head,
376 struct list_head *fail_head)
Yinghai Lu6841ec62010-01-22 01:02:25 -0800377{
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800378 LIST_HEAD(head);
Yinghai Lu6841ec62010-01-22 01:02:25 -0800379
Yinghai Lu6841ec62010-01-22 01:02:25 -0800380 __dev_sort_resources(dev, &head);
Yinghai Lu8424d752012-01-21 02:08:21 -0800381 __assign_resources_sorted(&head, add_head, fail_head);
Yinghai Lu6841ec62010-01-22 01:02:25 -0800382
383}
384
385static void pbus_assign_resources_sorted(const struct pci_bus *bus,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800386 struct list_head *realloc_head,
387 struct list_head *fail_head)
Yinghai Lu6841ec62010-01-22 01:02:25 -0800388{
389 struct pci_dev *dev;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800390 LIST_HEAD(head);
Yinghai Lu6841ec62010-01-22 01:02:25 -0800391
Yinghai Lu6841ec62010-01-22 01:02:25 -0800392 list_for_each_entry(dev, &bus->devices, bus_list)
393 __dev_sort_resources(dev, &head);
394
Ram Pai9e8bf932011-07-25 13:08:42 -0700395 __assign_resources_sorted(&head, realloc_head, fail_head);
Yinghai Lu6841ec62010-01-22 01:02:25 -0800396}
397
Dominik Brodowskib3743fa2005-09-09 13:03:23 -0700398void pci_setup_cardbus(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
400 struct pci_dev *bridge = bus->self;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600401 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 struct pci_bus_region region;
403
Bjorn Helgaas865df572009-11-04 10:32:57 -0700404 dev_info(&bridge->dev, "CardBus bridge to [bus %02x-%02x]\n",
405 bus->secondary, bus->subordinate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600407 res = bus->resource[0];
408 pcibios_resource_to_bus(bridge, &region, res);
409 if (res->flags & IORESOURCE_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 /*
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 Helgaasc7dabef2009-10-27 13:26:47 -0600414 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 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 Helgaasc7dabef2009-10-27 13:26:47 -0600421 res = bus->resource[1];
422 pcibios_resource_to_bus(bridge, &region, res);
423 if (res->flags & IORESOURCE_IO) {
424 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 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 Helgaasc7dabef2009-10-27 13:26:47 -0600431 res = bus->resource[2];
432 pcibios_resource_to_bus(bridge, &region, res);
433 if (res->flags & IORESOURCE_MEM) {
434 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 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 Helgaasc7dabef2009-10-27 13:26:47 -0600441 res = bus->resource[3];
442 pcibios_resource_to_bus(bridge, &region, res);
443 if (res->flags & IORESOURCE_MEM) {
444 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 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 Brodowskib3743fa2005-09-09 13:03:23 -0700451EXPORT_SYMBOL(pci_setup_cardbus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
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 Lu7cc59972009-12-22 15:02:21 -0800464static void pci_setup_bridge_io(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 struct pci_dev *bridge = bus->self;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600467 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 struct pci_bus_region region;
Yinghai Lu7cc59972009-12-22 15:02:21 -0800469 u32 l, io_upper16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 /* Set up the top and bottom of the PCI I/O segment for this bus. */
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600472 res = bus->resource[0];
473 pcibios_resource_to_bus(bridge, &region, res);
474 if (res->flags & IORESOURCE_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 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 Helgaasc7dabef2009-10-27 13:26:47 -0600481 dev_info(&bridge->dev, " bridge window %pR\n", res);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800482 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 /* Clear upper 16 bits of I/O base/limit. */
484 io_upper16 = 0;
485 l = 0x00f0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
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 Lu7cc59972009-12-22 15:02:21 -0800493}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Yinghai Lu7cc59972009-12-22 15:02:21 -0800495static 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 Helgaasc7dabef2009-10-27 13:26:47 -0600503 res = bus->resource[1];
504 pcibios_resource_to_bus(bridge, &region, res);
505 if (res->flags & IORESOURCE_MEM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 l = (region.start >> 16) & 0xfff0;
507 l |= region.end & 0xfff00000;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600508 dev_info(&bridge->dev, " bridge window %pR\n", res);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800509 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 l = 0x0000fff0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512 pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800513}
514
515static 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 Torvalds1da177e2005-04-16 15:20:36 -0700521
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 Herrenschmidtc40a22e2007-12-10 17:32:15 +1100528 bu = lu = 0;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600529 res = bus->resource[2];
530 pcibios_resource_to_bus(bridge, &region, res);
531 if (res->flags & IORESOURCE_PREFETCH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 l = (region.start >> 16) & 0xfff0;
533 l |= region.end & 0xfff00000;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600534 if (res->flags & IORESOURCE_MEM_64) {
Yinghai Lu1f82de12009-04-23 20:48:32 -0700535 bu = upper_32_bits(region.start);
536 lu = upper_32_bits(region.end);
Yinghai Lu1f82de12009-04-23 20:48:32 -0700537 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600538 dev_info(&bridge->dev, " bridge window %pR\n", res);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800539 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 l = 0x0000fff0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
542 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
543
Alex Williamson59353ea2009-11-30 14:51:44 -0700544 /* 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 Lu7cc59972009-12-22 15:02:21 -0800547}
548
549static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type)
550{
551 struct pci_dev *bridge = bus->self;
552
Yinghai Lu7cc59972009-12-22 15:02:21 -0800553 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 Torvalds1da177e2005-04-16 15:20:36 -0700564
565 pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
566}
567
Benjamin Herrenschmidte2444272011-09-11 14:08:38 -0300568void pci_setup_bridge(struct pci_bus *bus)
Yinghai Lu7cc59972009-12-22 15:02:21 -0800569{
570 unsigned long type = IORESOURCE_IO | IORESOURCE_MEM |
571 IORESOURCE_PREFETCH;
572
573 __pci_setup_bridge(bus, type);
574}
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576/* 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 Ravnborg96bde062007-03-26 21:53:30 -0800579static void pci_bridge_check_ranges(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
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 Lu1f82de12009-04-23 20:48:32 -0700609 if (pmem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
Yinghai Lu99586102010-01-22 01:02:28 -0800611 if ((pmem & PCI_PREF_RANGE_TYPE_MASK) ==
612 PCI_PREF_RANGE_TYPE_64) {
Yinghai Lu1f82de12009-04-23 20:48:32 -0700613 b_res[2].flags |= IORESOURCE_MEM_64;
Yinghai Lu99586102010-01-22 01:02:28 -0800614 b_res[2].flags |= PCI_PREF_RANGE_TYPE_64;
615 }
Yinghai Lu1f82de12009-04-23 20:48:32 -0700616 }
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 Torvalds1da177e2005-04-16 15:20:36 -0700631}
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 Ravnborg96bde062007-03-26 21:53:30 -0800637static struct resource *find_free_bus_resource(struct pci_bus *bus, unsigned long type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639 int i;
640 struct resource *r;
641 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
642 IORESOURCE_PREFETCH;
643
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -0700644 pci_bus_for_each_resource(bus, r, i) {
Ivan Kokshaysky299de032005-06-15 18:59:27 +0400645 if (r == &ioport_resource || r == &iomem_resource)
646 continue;
Jesse Barnes55a10982009-10-27 09:39:18 -0700647 if (r && (r->flags & type_mask) == type && !r->parent)
648 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
650 return NULL;
651}
652
Ram Pai13583b12011-02-14 17:43:17 -0800653static 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
674static 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 Paic8adf9a2011-02-14 17:43:20 -0800690/**
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 Pai9e8bf932011-07-25 13:08:42 -0700696 * @realloc_head : track the additional io window on this list
Ram Paic8adf9a2011-02-14 17:43:20 -0800697 *
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 */
703static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800704 resource_size_t add_size, struct list_head *realloc_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
706 struct pci_dev *dev;
707 struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO);
Ram Paic8adf9a2011-02-14 17:43:20 -0800708 unsigned long size = 0, size0 = 0, size1 = 0;
Yinghai Lube768912011-07-25 13:08:38 -0700709 resource_size_t children_add_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
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, Yu022edd82008-10-13 19:24:28 +0800723 r_size = resource_size(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 if (r_size < 0x400)
726 /* Might be re-aligned for ISA */
727 size += r_size;
728 else
729 size1 += r_size;
Yinghai Lube768912011-07-25 13:08:38 -0700730
Ram Pai9e8bf932011-07-25 13:08:42 -0700731 if (realloc_head)
732 children_add_size += get_res_add_size(realloc_head, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734 }
Ram Paic8adf9a2011-02-14 17:43:20 -0800735 size0 = calculate_iosize(size, min_size, size1,
Ram Pai13583b12011-02-14 17:43:17 -0800736 resource_size(b_res), 4096);
Yinghai Lube768912011-07-25 13:08:38 -0700737 if (children_add_size > add_size)
738 add_size = children_add_size;
Ram Pai9e8bf932011-07-25 13:08:42 -0700739 size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
Yinghai Lua4ac9fe2012-01-21 02:08:17 -0800740 calculate_iosize(size, min_size, add_size + size1,
Ram Paic8adf9a2011-02-14 17:43:20 -0800741 resource_size(b_res), 4096);
742 if (!size0 && !size1) {
Bjorn Helgaas865df572009-11-04 10:32:57 -0700743 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 Torvalds1da177e2005-04-16 15:20:36 -0700747 b_res->flags = 0;
748 return;
749 }
750 /* Alignment of the IO window is always 4K */
751 b_res->start = 4096;
Ram Paic8adf9a2011-02-14 17:43:20 -0800752 b_res->end = b_res->start + size0 - 1;
Ivan Kokshaysky88452562008-03-30 19:50:14 +0400753 b_res->flags |= IORESOURCE_STARTALIGN;
Yinghai Lub5924432012-01-21 02:08:31 -0800754 if (size1 > size0 && realloc_head) {
Ram Pai9e8bf932011-07-25 13:08:42 -0700755 add_to_list(realloc_head, bus->self, b_res, size1-size0, 4096);
Yinghai Lub5924432012-01-21 02:08:31 -0800756 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 Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
Ram Paic8adf9a2011-02-14 17:43:20 -0800762/**
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 Pai9e8bf932011-07-25 13:08:42 -0700768 * @realloc_head : track the additional memory window on this list
Ram Paic8adf9a2011-02-14 17:43:20 -0800769 *
770 * Calculate the size of the bus and minimal alignment which
771 * guarantees that all child resources fit in this size.
772 */
Eric W. Biederman28760482009-09-09 14:09:24 -0700773static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
Ram Paic8adf9a2011-02-14 17:43:20 -0800774 unsigned long type, resource_size_t min_size,
775 resource_size_t add_size,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800776 struct list_head *realloc_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
778 struct pci_dev *dev;
Ram Paic8adf9a2011-02-14 17:43:20 -0800779 resource_size_t min_align, align, size, size0, size1;
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100780 resource_size_t aligns[12]; /* Alignments from 1Mb to 2Gb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 int order, max_order;
782 struct resource *b_res = find_free_bus_resource(bus, type);
Yinghai Lu1f82de12009-04-23 20:48:32 -0700783 unsigned int mem64_mask = 0;
Yinghai Lube768912011-07-25 13:08:38 -0700784 resource_size_t children_add_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 if (!b_res)
787 return 0;
788
789 memset(aligns, 0, sizeof(aligns));
790 max_order = 0;
791 size = 0;
792
Yinghai Lu1f82de12009-04-23 20:48:32 -0700793 mem64_mask = b_res->flags & IORESOURCE_MEM_64;
794 b_res->flags &= ~IORESOURCE_MEM_64;
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 list_for_each_entry(dev, &bus->devices, bus_list) {
797 int i;
Yinghai Lu1f82de12009-04-23 20:48:32 -0700798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
800 struct resource *r = &dev->resource[i];
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100801 resource_size_t r_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 if (r->parent || (r->flags & mask) != type)
804 continue;
Zhao, Yu022edd82008-10-13 19:24:28 +0800805 r_size = resource_size(r);
Yinghai Lu2aceefc2011-07-25 13:08:40 -0700806#ifdef CONFIG_PCI_IOV
807 /* put SRIOV requested res to the optional list */
Ram Pai9e8bf932011-07-25 13:08:42 -0700808 if (realloc_head && i >= PCI_IOV_RESOURCES &&
Yinghai Lu2aceefc2011-07-25 13:08:40 -0700809 i <= PCI_IOV_RESOURCE_END) {
810 r->end = r->start - 1;
Ram Pai9e8bf932011-07-25 13:08:42 -0700811 add_to_list(realloc_head, dev, r, r_size, 0/* dont' care */);
Yinghai Lu2aceefc2011-07-25 13:08:40 -0700812 children_add_size += r_size;
813 continue;
814 }
815#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 /* For bridges size != alignment */
Chris Wright6faf17f2009-08-28 13:00:06 -0700817 align = pci_resource_alignment(dev, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 order = __ffs(align) - 20;
819 if (order > 11) {
Bjorn Helgaas865df572009-11-04 10:32:57 -0700820 dev_warn(&dev->dev, "disabling BAR %d: %pR "
821 "(bad alignment %#llx)\n", i, r,
822 (unsigned long long) align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 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 Lu1f82de12009-04-23 20:48:32 -0700835 mem64_mask &= r->flags & IORESOURCE_MEM_64;
Yinghai Lube768912011-07-25 13:08:38 -0700836
Ram Pai9e8bf932011-07-25 13:08:42 -0700837 if (realloc_head)
838 children_add_size += get_res_add_size(realloc_head, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 align = 0;
842 min_align = 0;
843 for (order = 0; order <= max_order; order++) {
Jeremy Fitzhardinge8308c542008-09-11 01:31:50 -0700844 resource_size_t align1 = 1;
845
846 align1 <<= (order + 20);
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if (!align)
849 min_align = align1;
Milind Arun Choudhary6f6f8c22007-07-09 11:55:51 -0700850 else if (ALIGN(align + min_align, min_align) < align1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 min_align = align1 >> 1;
852 align += aligns[order];
853 }
Linus Torvaldsb42282e2011-04-11 10:53:11 -0700854 size0 = calculate_memsize(size, min_size, 0, resource_size(b_res), min_align);
Yinghai Lube768912011-07-25 13:08:38 -0700855 if (children_add_size > add_size)
856 add_size = children_add_size;
Ram Pai9e8bf932011-07-25 13:08:42 -0700857 size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
Yinghai Lua4ac9fe2012-01-21 02:08:17 -0800858 calculate_memsize(size, min_size, add_size,
Linus Torvaldsb42282e2011-04-11 10:53:11 -0700859 resource_size(b_res), min_align);
Ram Paic8adf9a2011-02-14 17:43:20 -0800860 if (!size0 && !size1) {
Bjorn Helgaas865df572009-11-04 10:32:57 -0700861 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 Torvalds1da177e2005-04-16 15:20:36 -0700865 b_res->flags = 0;
866 return 1;
867 }
868 b_res->start = min_align;
Ram Paic8adf9a2011-02-14 17:43:20 -0800869 b_res->end = size0 + min_align - 1;
870 b_res->flags |= IORESOURCE_STARTALIGN | mem64_mask;
Yinghai Lub5924432012-01-21 02:08:31 -0800871 if (size1 > size0 && realloc_head) {
Ram Pai9e8bf932011-07-25 13:08:42 -0700872 add_to_list(realloc_head, bus->self, b_res, size1-size0, min_align);
Yinghai Lub5924432012-01-21 02:08:31 -0800873 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 Torvalds1da177e2005-04-16 15:20:36 -0700877 return 1;
878}
879
Ram Pai0a2daa12011-07-25 13:08:41 -0700880unsigned 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
889static void pci_bus_size_cardbus(struct pci_bus *bus,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800890 struct list_head *realloc_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
892 struct pci_dev *bridge = bus->self;
893 struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
Yinghai Lu11848932012-02-10 15:33:47 -0800894 resource_size_t b_res_3_size = pci_cardbus_mem_size * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 u16 ctrl;
896
Yinghai Lu3796f1e2012-02-10 15:33:48 -0800897 if (b_res[0].parent)
898 goto handle_b_res_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 /*
900 * Reserve some resources for CardBus. We reserve
901 * a fixed amount of bus space for CardBus bridges.
902 */
Yinghai Lu11848932012-02-10 15:33:47 -0800903 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 Torvalds1da177e2005-04-16 15:20:36 -0700911
Yinghai Lu3796f1e2012-02-10 15:33:48 -0800912handle_b_res_1:
913 if (b_res[1].parent)
914 goto handle_b_res_2;
Yinghai Lu11848932012-02-10 15:33:47 -0800915 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 Torvalds1da177e2005-04-16 15:20:36 -0700923
Yinghai Lu3796f1e2012-02-10 15:33:48 -0800924handle_b_res_2:
Yinghai Ludcef0d02012-02-10 15:33:46 -0800925 /* 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 Torvalds1da177e2005-04-16 15:20:36 -0700933 /*
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 Lu3796f1e2012-02-10 15:33:48 -0800944 if (b_res[2].parent)
945 goto handle_b_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 /*
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 Lu11848932012-02-10 15:33:47 -0800952 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 Torvalds1da177e2005-04-16 15:20:36 -0700961
Yinghai Lu11848932012-02-10 15:33:47 -0800962 /* reduce that to half */
963 b_res_3_size = pci_cardbus_mem_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
Ram Pai0a2daa12011-07-25 13:08:41 -0700965
Yinghai Lu3796f1e2012-02-10 15:33:48 -0800966handle_b_res_3:
967 if (b_res[3].parent)
968 goto handle_done;
Yinghai Lu11848932012-02-10 15:33:47 -0800969 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 Lu3796f1e2012-02-10 15:33:48 -0800977
978handle_done:
979 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
981
Ram Paic8adf9a2011-02-14 17:43:20 -0800982void __ref __pci_bus_size_bridges(struct pci_bus *bus,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800983 struct list_head *realloc_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
985 struct pci_dev *dev;
986 unsigned long mask, prefmask;
Ram Paic8adf9a2011-02-14 17:43:20 -0800987 resource_size_t additional_mem_size = 0, additional_io_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
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 Pai9e8bf932011-07-25 13:08:42 -0700996 pci_bus_size_cardbus(b, realloc_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 break;
998
999 case PCI_CLASS_BRIDGE_PCI:
1000 default:
Ram Pai9e8bf932011-07-25 13:08:42 -07001001 __pci_bus_size_bridges(b, realloc_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 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. Biederman28760482009-09-09 14:09:24 -07001017 if (bus->self->is_hotplug_bridge) {
Ram Paic8adf9a2011-02-14 17:43:20 -08001018 additional_io_size = pci_hotplug_io_size;
1019 additional_mem_size = pci_hotplug_mem_size;
Eric W. Biederman28760482009-09-09 14:09:24 -07001020 }
Ram Paic8adf9a2011-02-14 17:43:20 -08001021 /*
1022 * Follow thru
1023 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 default:
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001025 pbus_size_io(bus, realloc_head ? 0 : additional_io_size,
1026 additional_io_size, realloc_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 /* 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 Lu19aa7ee2012-01-21 02:08:24 -08001034 if (pbus_size_mem(bus, prefmask, prefmask,
1035 realloc_head ? 0 : additional_mem_size,
1036 additional_mem_size, realloc_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 mask = prefmask; /* Success, size non-prefetch only. */
Eric W. Biederman28760482009-09-09 14:09:24 -07001038 else
Ram Paic8adf9a2011-02-14 17:43:20 -08001039 additional_mem_size += additional_mem_size;
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001040 pbus_size_mem(bus, mask, IORESOURCE_MEM,
1041 realloc_head ? 0 : additional_mem_size,
1042 additional_mem_size, realloc_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 break;
1044 }
1045}
Ram Paic8adf9a2011-02-14 17:43:20 -08001046
1047void __ref pci_bus_size_bridges(struct pci_bus *bus)
1048{
1049 __pci_bus_size_bridges(bus, NULL);
1050}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051EXPORT_SYMBOL(pci_bus_size_bridges);
1052
Yinghai Lu568ddef2010-01-22 01:02:21 -08001053static void __ref __pci_bus_assign_resources(const struct pci_bus *bus,
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001054 struct list_head *realloc_head,
1055 struct list_head *fail_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
1057 struct pci_bus *b;
1058 struct pci_dev *dev;
1059
Ram Pai9e8bf932011-07-25 13:08:42 -07001060 pbus_assign_resources_sorted(bus, realloc_head, fail_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 list_for_each_entry(dev, &bus->devices, bus_list) {
1063 b = dev->subordinate;
1064 if (!b)
1065 continue;
1066
Ram Pai9e8bf932011-07-25 13:08:42 -07001067 __pci_bus_assign_resources(b, realloc_head, fail_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 switch (dev->class >> 8) {
1070 case PCI_CLASS_BRIDGE_PCI:
Yinghai Lu6841ec62010-01-22 01:02:25 -08001071 if (!pci_is_enabled(dev))
1072 pci_setup_bridge(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 break;
1074
1075 case PCI_CLASS_BRIDGE_CARDBUS:
1076 pci_setup_cardbus(b);
1077 break;
1078
1079 default:
Bjorn Helgaas80ccba12008-06-13 10:52:11 -06001080 dev_info(&dev->dev, "not setting up bridge for bus "
1081 "%04x:%02x\n", pci_domain_nr(b), b->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 break;
1083 }
1084 }
1085}
Yinghai Lu568ddef2010-01-22 01:02:21 -08001086
1087void __ref pci_bus_assign_resources(const struct pci_bus *bus)
1088{
Ram Paic8adf9a2011-02-14 17:43:20 -08001089 __pci_bus_assign_resources(bus, NULL, NULL);
Yinghai Lu568ddef2010-01-22 01:02:21 -08001090}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091EXPORT_SYMBOL(pci_bus_assign_resources);
1092
Yinghai Lu6841ec62010-01-22 01:02:25 -08001093static void __ref __pci_bridge_assign_resources(const struct pci_dev *bridge,
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001094 struct list_head *add_head,
1095 struct list_head *fail_head)
Yinghai Lu6841ec62010-01-22 01:02:25 -08001096{
1097 struct pci_bus *b;
1098
Yinghai Lu8424d752012-01-21 02:08:21 -08001099 pdev_assign_resources_sorted((struct pci_dev *)bridge,
1100 add_head, fail_head);
Yinghai Lu6841ec62010-01-22 01:02:25 -08001101
1102 b = bridge->subordinate;
1103 if (!b)
1104 return;
1105
Yinghai Lu8424d752012-01-21 02:08:21 -08001106 __pci_bus_assign_resources(b, add_head, fail_head);
Yinghai Lu6841ec62010-01-22 01:02:25 -08001107
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 Lu5009b462010-01-22 01:02:20 -08001123static 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
1165enum 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 */
1173static 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 Lu76fbc262008-06-23 20:33:06 +02001205static void pci_bus_dump_res(struct pci_bus *bus)
1206{
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -07001207 struct resource *res;
1208 int i;
Yinghai Lu76fbc262008-06-23 20:33:06 +02001209
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -07001210 pci_bus_for_each_resource(bus, res, i) {
Yinghai Lu7c9342b2009-12-22 15:02:24 -08001211 if (!res || !res->end || !res->flags)
Yinghai Lu76fbc262008-06-23 20:33:06 +02001212 continue;
1213
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001214 dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res);
Yinghai Lu76fbc262008-06-23 20:33:06 +02001215 }
1216}
1217
1218static 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 Luda7822e2011-05-12 17:11:37 -07001235static 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}
1253static 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 Lub55438f2012-02-23 19:23:30 -08001269/*
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 */
1276enum enable_type {
1277 undefined = -1,
1278 user_disabled,
1279 auto_disabled,
1280 user_enabled,
1281 auto_enabled,
1282};
1283
1284static enum enable_type pci_realloc_enable __initdata = undefined;
1285void __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}
1292static bool __init pci_realloc_enabled(void)
1293{
1294 return pci_realloc_enable >= user_enabled;
1295}
Ram Paif483d392011-07-07 11:19:10 -07001296
Yinghai Luda7822e2011-05-12 17:11:37 -07001297/*
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 Torvalds1da177e2005-04-16 15:20:36 -07001302void __init
1303pci_assign_unassigned_resources(void)
1304{
1305 struct pci_bus *bus;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001306 LIST_HEAD(realloc_head); /* list of resources that
Ram Paic8adf9a2011-02-14 17:43:20 -08001307 want additional resources */
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001308 struct list_head *add_list = NULL;
Yinghai Luda7822e2011-05-12 17:11:37 -07001309 int tried_times = 0;
1310 enum release_type rel_type = leaf_only;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001311 LIST_HEAD(fail_head);
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001312 struct pci_dev_resource *fail_res;
Yinghai Luda7822e2011-05-12 17:11:37 -07001313 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1314 IORESOURCE_PREFETCH;
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001315 int pci_try_num = 1;
Yinghai Luda7822e2011-05-12 17:11:37 -07001316
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001317 /* 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 Luda7822e2011-05-12 17:11:37 -07001325
1326again:
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001327 /*
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 Lubdc4abe2012-01-21 02:08:27 -08001332 add_list = &realloc_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 /* Depth first, calculate sizes and alignments of all
1334 subordinate buses. */
Yinghai Luda7822e2011-05-12 17:11:37 -07001335 list_for_each_entry(bus, &pci_root_buses, node)
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001336 __pci_bus_size_bridges(bus, add_list);
Ram Paic8adf9a2011-02-14 17:43:20 -08001337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 /* Depth last, allocate resources and update the hardware. */
Yinghai Luda7822e2011-05-12 17:11:37 -07001339 list_for_each_entry(bus, &pci_root_buses, node)
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001340 __pci_bus_assign_resources(bus, add_list, &fail_head);
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001341 if (add_list)
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001342 BUG_ON(!list_empty(add_list));
Yinghai Luda7822e2011-05-12 17:11:37 -07001343 tried_times++;
1344
1345 /* any device complain? */
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001346 if (list_empty(&fail_head))
Yinghai Luda7822e2011-05-12 17:11:37 -07001347 goto enable_and_dump;
Ram Paif483d392011-07-07 11:19:10 -07001348
Yinghai Lu0c5be0c2012-02-23 19:23:29 -08001349 if (tried_times >= pci_try_num) {
Yinghai Lubffc56d2012-01-21 02:08:30 -08001350 free_list(&fail_head);
Yinghai Luda7822e2011-05-12 17:11:37 -07001351 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 Lub9b0bba2012-01-21 02:08:29 -08001365 list_for_each_entry(fail_res, &fail_head, list) {
1366 bus = fail_res->dev->bus;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001367 pci_bus_release_bridge_resources(bus,
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001368 fail_res->flags & type_mask,
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001369 rel_type);
Yinghai Luda7822e2011-05-12 17:11:37 -07001370 }
1371 /* restore size and flags */
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001372 list_for_each_entry(fail_res, &fail_head, list) {
1373 struct resource *res = fail_res->res;
Yinghai Luda7822e2011-05-12 17:11:37 -07001374
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001375 res->start = fail_res->start;
1376 res->end = fail_res->end;
1377 res->flags = fail_res->flags;
1378 if (fail_res->dev->subordinate)
Yinghai Luda7822e2011-05-12 17:11:37 -07001379 res->flags = 0;
Yinghai Luda7822e2011-05-12 17:11:37 -07001380 }
Yinghai Lubffc56d2012-01-21 02:08:30 -08001381 free_list(&fail_head);
Yinghai Luda7822e2011-05-12 17:11:37 -07001382
1383 goto again;
1384
1385enable_and_dump:
1386 /* Depth last, update the hardware. */
1387 list_for_each_entry(bus, &pci_root_buses, node)
1388 pci_enable_bridges(bus);
Yinghai Lu76fbc262008-06-23 20:33:06 +02001389
1390 /* dump the resource on buses */
Yinghai Luda7822e2011-05-12 17:11:37 -07001391 list_for_each_entry(bus, &pci_root_buses, node)
Yinghai Lu76fbc262008-06-23 20:33:06 +02001392 pci_bus_dump_resources(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393}
Yinghai Lu6841ec62010-01-22 01:02:25 -08001394
1395void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
1396{
1397 struct pci_bus *parent = bridge->subordinate;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001398 LIST_HEAD(add_list); /* list of resources that
Yinghai Lu8424d752012-01-21 02:08:21 -08001399 want additional resources */
Yinghai Lu32180e42010-01-22 01:02:27 -08001400 int tried_times = 0;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001401 LIST_HEAD(fail_head);
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001402 struct pci_dev_resource *fail_res;
Yinghai Lu6841ec62010-01-22 01:02:25 -08001403 int retval;
Yinghai Lu32180e42010-01-22 01:02:27 -08001404 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1405 IORESOURCE_PREFETCH;
Yinghai Lu6841ec62010-01-22 01:02:25 -08001406
Yinghai Lu32180e42010-01-22 01:02:27 -08001407again:
Yinghai Lu8424d752012-01-21 02:08:21 -08001408 __pci_bus_size_bridges(parent, &add_list);
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001409 __pci_bridge_assign_resources(bridge, &add_list, &fail_head);
1410 BUG_ON(!list_empty(&add_list));
Yinghai Lu32180e42010-01-22 01:02:27 -08001411 tried_times++;
1412
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001413 if (list_empty(&fail_head))
Yinghai Lu3f579c32010-05-21 14:35:06 -07001414 goto enable_all;
Yinghai Lu32180e42010-01-22 01:02:27 -08001415
1416 if (tried_times >= 2) {
1417 /* still fail, don't need to try more */
Yinghai Lubffc56d2012-01-21 02:08:30 -08001418 free_list(&fail_head);
Yinghai Lu3f579c32010-05-21 14:35:06 -07001419 goto enable_all;
Yinghai Lu32180e42010-01-22 01:02:27 -08001420 }
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 Lub9b0bba2012-01-21 02:08:29 -08001429 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 Lu32180e42010-01-22 01:02:27 -08001432
1433 pci_bus_release_bridge_resources(bus, flags & type_mask,
1434 whole_subtree);
Yinghai Lu32180e42010-01-22 01:02:27 -08001435 }
1436 /* restore size and flags */
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001437 list_for_each_entry(fail_res, &fail_head, list) {
1438 struct resource *res = fail_res->res;
Yinghai Lu32180e42010-01-22 01:02:27 -08001439
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001440 res->start = fail_res->start;
1441 res->end = fail_res->end;
1442 res->flags = fail_res->flags;
1443 if (fail_res->dev->subordinate)
Yinghai Lu32180e42010-01-22 01:02:27 -08001444 res->flags = 0;
Yinghai Lu32180e42010-01-22 01:02:27 -08001445 }
Yinghai Lubffc56d2012-01-21 02:08:30 -08001446 free_list(&fail_head);
Yinghai Lu32180e42010-01-22 01:02:27 -08001447
1448 goto again;
Yinghai Lu3f579c32010-05-21 14:35:06 -07001449
1450enable_all:
1451 retval = pci_reenable_device(bridge);
1452 pci_set_master(bridge);
1453 pci_enable_bridges(parent);
Yinghai Lu6841ec62010-01-22 01:02:25 -08001454}
1455EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
Yinghai Lu9b030882012-01-21 02:08:23 -08001456
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 */
1467unsigned int __ref pci_rescan_bus(struct pci_bus *bus)
1468{
1469 unsigned int max;
1470 struct pci_dev *dev;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001471 LIST_HEAD(add_list); /* list of resources that
Yinghai Lu9b030882012-01-21 02:08:23 -08001472 want additional resources */
1473
1474 max = pci_scan_child_bus(bus);
1475
Yinghai Lu9b030882012-01-21 02:08:23 -08001476 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 Lubdc4abe2012-01-21 02:08:27 -08001485 BUG_ON(!list_empty(&add_list));
Yinghai Lu9b030882012-01-21 02:08:23 -08001486
1487 pci_enable_bridges(bus);
1488 pci_bus_add_devices(bus);
1489
1490 return max;
1491}
1492EXPORT_SYMBOL_GPL(pci_rescan_bus);
1493#endif