blob: d131d29c4fdbb455323416a6ab6298e770cda2a3 [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>
Rui Wang584c5c42016-08-17 16:00:34 +080028#include <linux/acpi.h>
Chris Wright6faf17f2009-08-28 13:00:06 -070029#include "pci.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Bjorn Helgaas844393f2012-02-23 20:18:59 -070031unsigned int pci_flags;
Bjorn Helgaas47087702012-02-23 14:29:23 -070032
Yinghai Lubdc4abe2012-01-21 02:08:27 -080033struct pci_dev_resource {
34 struct list_head list;
Yinghai Lu2934a0d2012-01-21 02:08:26 -080035 struct resource *res;
36 struct pci_dev *dev;
Yinghai Lu568ddef2010-01-22 01:02:21 -080037 resource_size_t start;
38 resource_size_t end;
Ram Paic8adf9a2011-02-14 17:43:20 -080039 resource_size_t add_size;
Ram Pai2bbc6942011-07-25 13:08:39 -070040 resource_size_t min_align;
Yinghai Lu568ddef2010-01-22 01:02:21 -080041 unsigned long flags;
42};
43
Yinghai Lubffc56d2012-01-21 02:08:30 -080044static void free_list(struct list_head *head)
45{
46 struct pci_dev_resource *dev_res, *tmp;
47
48 list_for_each_entry_safe(dev_res, tmp, head, list) {
49 list_del(&dev_res->list);
50 kfree(dev_res);
51 }
52}
Ram Pai094732a2011-02-14 17:43:18 -080053
Ram Paic8adf9a2011-02-14 17:43:20 -080054/**
55 * add_to_list() - add a new resource tracker to the list
56 * @head: Head of the list
57 * @dev: device corresponding to which the resource
58 * belongs
59 * @res: The resource to be tracked
60 * @add_size: additional size to be optionally added
61 * to the resource
62 */
Yinghai Lubdc4abe2012-01-21 02:08:27 -080063static int add_to_list(struct list_head *head,
Ram Paic8adf9a2011-02-14 17:43:20 -080064 struct pci_dev *dev, struct resource *res,
Ram Pai2bbc6942011-07-25 13:08:39 -070065 resource_size_t add_size, resource_size_t min_align)
Yinghai Lu568ddef2010-01-22 01:02:21 -080066{
Yinghai Lu764242a2012-01-21 02:08:28 -080067 struct pci_dev_resource *tmp;
Yinghai Lu568ddef2010-01-22 01:02:21 -080068
Yinghai Lubdc4abe2012-01-21 02:08:27 -080069 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
Markus Elfringc7abb232017-12-29 12:15:16 +010070 if (!tmp)
Yinghai Luef62dfe2012-01-21 02:08:18 -080071 return -ENOMEM;
Yinghai Lu568ddef2010-01-22 01:02:21 -080072
Yinghai Lu568ddef2010-01-22 01:02:21 -080073 tmp->res = res;
74 tmp->dev = dev;
75 tmp->start = res->start;
76 tmp->end = res->end;
77 tmp->flags = res->flags;
Ram Paic8adf9a2011-02-14 17:43:20 -080078 tmp->add_size = add_size;
Ram Pai2bbc6942011-07-25 13:08:39 -070079 tmp->min_align = min_align;
Yinghai Lubdc4abe2012-01-21 02:08:27 -080080
81 list_add(&tmp->list, head);
Yinghai Luef62dfe2012-01-21 02:08:18 -080082
83 return 0;
Yinghai Lu568ddef2010-01-22 01:02:21 -080084}
85
Yinghai Lub9b0bba2012-01-21 02:08:29 -080086static void remove_from_list(struct list_head *head,
Yinghai Lu3e6e0d82012-01-21 02:08:20 -080087 struct resource *res)
88{
Yinghai Lub9b0bba2012-01-21 02:08:29 -080089 struct pci_dev_resource *dev_res, *tmp;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -080090
Yinghai Lub9b0bba2012-01-21 02:08:29 -080091 list_for_each_entry_safe(dev_res, tmp, head, list) {
92 if (dev_res->res == res) {
93 list_del(&dev_res->list);
94 kfree(dev_res);
Yinghai Lubdc4abe2012-01-21 02:08:27 -080095 break;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -080096 }
Yinghai Lu3e6e0d82012-01-21 02:08:20 -080097 }
98}
99
Wei Yangd74b9022015-03-25 16:23:51 +0800100static struct pci_dev_resource *res_to_dev_res(struct list_head *head,
101 struct resource *res)
Yinghai Lu1c372352012-01-21 02:08:19 -0800102{
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800103 struct pci_dev_resource *dev_res;
Yinghai Lu1c372352012-01-21 02:08:19 -0800104
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800105 list_for_each_entry(dev_res, head, list) {
Bjorn Helgaas25e77382016-12-29 11:27:52 -0600106 if (dev_res->res == res)
Wei Yangd74b9022015-03-25 16:23:51 +0800107 return dev_res;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800108 }
Yinghai Lu1c372352012-01-21 02:08:19 -0800109
Wei Yangd74b9022015-03-25 16:23:51 +0800110 return NULL;
Yinghai Lu1c372352012-01-21 02:08:19 -0800111}
112
Wei Yangd74b9022015-03-25 16:23:51 +0800113static resource_size_t get_res_add_size(struct list_head *head,
114 struct resource *res)
115{
116 struct pci_dev_resource *dev_res;
117
118 dev_res = res_to_dev_res(head, res);
119 return dev_res ? dev_res->add_size : 0;
120}
121
122static resource_size_t get_res_add_align(struct list_head *head,
123 struct resource *res)
124{
125 struct pci_dev_resource *dev_res;
126
127 dev_res = res_to_dev_res(head, res);
128 return dev_res ? dev_res->min_align : 0;
129}
130
131
Yinghai Lu78c3b322012-01-21 02:08:25 -0800132/* Sort resources by alignment */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800133static void pdev_sort_resources(struct pci_dev *dev, struct list_head *head)
Yinghai Lu78c3b322012-01-21 02:08:25 -0800134{
135 int i;
136
137 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
138 struct resource *r;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800139 struct pci_dev_resource *dev_res, *tmp;
Yinghai Lu78c3b322012-01-21 02:08:25 -0800140 resource_size_t r_align;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800141 struct list_head *n;
Yinghai Lu78c3b322012-01-21 02:08:25 -0800142
143 r = &dev->resource[i];
144
145 if (r->flags & IORESOURCE_PCI_FIXED)
146 continue;
147
148 if (!(r->flags) || r->parent)
149 continue;
150
151 r_align = pci_resource_alignment(dev, r);
152 if (!r_align) {
153 dev_warn(&dev->dev, "BAR %d: %pR has bogus alignment\n",
154 i, r);
155 continue;
156 }
Yinghai Lu78c3b322012-01-21 02:08:25 -0800157
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800158 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
159 if (!tmp)
Ryan Desfosses227f0642014-04-18 20:13:50 -0400160 panic("pdev_sort_resources(): kmalloc() failed!\n");
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800161 tmp->res = r;
162 tmp->dev = dev;
163
164 /* fallback is smallest one or list is empty*/
165 n = head;
166 list_for_each_entry(dev_res, head, list) {
167 resource_size_t align;
168
169 align = pci_resource_alignment(dev_res->dev,
170 dev_res->res);
Yinghai Lu78c3b322012-01-21 02:08:25 -0800171
172 if (r_align > align) {
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800173 n = &dev_res->list;
Yinghai Lu78c3b322012-01-21 02:08:25 -0800174 break;
175 }
176 }
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800177 /* Insert it just before n*/
178 list_add_tail(&tmp->list, n);
Yinghai Lu78c3b322012-01-21 02:08:25 -0800179 }
180}
181
Yinghai Lu6841ec62010-01-22 01:02:25 -0800182static void __dev_sort_resources(struct pci_dev *dev,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800183 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Yinghai Lu6841ec62010-01-22 01:02:25 -0800185 u16 class = dev->class >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Yinghai Lu6841ec62010-01-22 01:02:25 -0800187 /* Don't touch classless devices or host bridges or ioapics. */
188 if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST)
189 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Yinghai Lu6841ec62010-01-22 01:02:25 -0800191 /* Don't touch ioapic devices already enabled by firmware */
192 if (class == PCI_CLASS_SYSTEM_PIC) {
193 u16 command;
194 pci_read_config_word(dev, PCI_COMMAND, &command);
195 if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
196 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
198
Yinghai Lu6841ec62010-01-22 01:02:25 -0800199 pdev_sort_resources(dev, head);
200}
201
Ram Paifc075e12011-02-14 17:43:19 -0800202static inline void reset_resource(struct resource *res)
203{
204 res->start = 0;
205 res->end = 0;
206 res->flags = 0;
207}
208
Ram Paic8adf9a2011-02-14 17:43:20 -0800209/**
Ram Pai9e8bf932011-07-25 13:08:42 -0700210 * reassign_resources_sorted() - satisfy any additional resource requests
Ram Paic8adf9a2011-02-14 17:43:20 -0800211 *
Ram Pai9e8bf932011-07-25 13:08:42 -0700212 * @realloc_head : head of the list tracking requests requiring additional
Ram Paic8adf9a2011-02-14 17:43:20 -0800213 * resources
214 * @head : head of the list tracking requests with allocated
215 * resources
216 *
Ram Pai9e8bf932011-07-25 13:08:42 -0700217 * Walk through each element of the realloc_head and try to procure
Ram Paic8adf9a2011-02-14 17:43:20 -0800218 * additional resources for the element, provided the element
219 * is in the head list.
220 */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800221static void reassign_resources_sorted(struct list_head *realloc_head,
222 struct list_head *head)
Ram Paic8adf9a2011-02-14 17:43:20 -0800223{
224 struct resource *res;
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800225 struct pci_dev_resource *add_res, *tmp;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800226 struct pci_dev_resource *dev_res;
Wei Yangd74b9022015-03-25 16:23:51 +0800227 resource_size_t add_size, align;
Ram Paic8adf9a2011-02-14 17:43:20 -0800228 int idx;
229
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800230 list_for_each_entry_safe(add_res, tmp, realloc_head, list) {
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800231 bool found_match = false;
232
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800233 res = add_res->res;
Ram Paic8adf9a2011-02-14 17:43:20 -0800234 /* skip resource that has been reset */
235 if (!res->flags)
236 goto out;
237
238 /* skip this resource if not found in head list */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800239 list_for_each_entry(dev_res, head, list) {
240 if (dev_res->res == res) {
241 found_match = true;
242 break;
243 }
Ram Paic8adf9a2011-02-14 17:43:20 -0800244 }
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800245 if (!found_match)/* just skip */
246 continue;
Ram Paic8adf9a2011-02-14 17:43:20 -0800247
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800248 idx = res - &add_res->dev->resource[0];
249 add_size = add_res->add_size;
Wei Yangd74b9022015-03-25 16:23:51 +0800250 align = add_res->min_align;
Ram Pai2bbc6942011-07-25 13:08:39 -0700251 if (!resource_size(res)) {
Wei Yangd74b9022015-03-25 16:23:51 +0800252 res->start = align;
Ram Pai2bbc6942011-07-25 13:08:39 -0700253 res->end = res->start + add_size - 1;
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800254 if (pci_assign_resource(add_res->dev, idx))
Ram Paic8adf9a2011-02-14 17:43:20 -0800255 reset_resource(res);
Ram Pai2bbc6942011-07-25 13:08:39 -0700256 } else {
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800257 res->flags |= add_res->flags &
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800258 (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN);
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800259 if (pci_reassign_resource(add_res->dev, idx,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800260 add_size, align))
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800261 dev_printk(KERN_DEBUG, &add_res->dev->dev,
Yinghai Lub5924432012-01-21 02:08:31 -0800262 "failed to add %llx res[%d]=%pR\n",
263 (unsigned long long)add_size,
264 idx, res);
Ram Paic8adf9a2011-02-14 17:43:20 -0800265 }
266out:
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800267 list_del(&add_res->list);
268 kfree(add_res);
Ram Paic8adf9a2011-02-14 17:43:20 -0800269 }
270}
271
272/**
273 * assign_requested_resources_sorted() - satisfy resource requests
274 *
275 * @head : head of the list tracking requests for resources
Wanpeng Li8356aad2012-06-15 21:15:49 +0800276 * @fail_head : head of the list tracking requests that could
Ram Paic8adf9a2011-02-14 17:43:20 -0800277 * not be allocated
278 *
279 * Satisfy resource requests of each element in the list. Add
280 * requests that could not satisfied to the failed_list.
281 */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800282static void assign_requested_resources_sorted(struct list_head *head,
283 struct list_head *fail_head)
Yinghai Lu6841ec62010-01-22 01:02:25 -0800284{
285 struct resource *res;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800286 struct pci_dev_resource *dev_res;
Yinghai Lu6841ec62010-01-22 01:02:25 -0800287 int idx;
288
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800289 list_for_each_entry(dev_res, head, list) {
290 res = dev_res->res;
291 idx = res - &dev_res->dev->resource[0];
292 if (resource_size(res) &&
293 pci_assign_resource(dev_res->dev, idx)) {
Yinghai Lua3cb9992013-01-21 13:20:43 -0800294 if (fail_head) {
Yinghai Lu9a928662010-02-28 15:49:39 -0800295 /*
296 * if the failed res is for ROM BAR, and it will
297 * be enabled later, don't add it to the list
298 */
299 if (!((idx == PCI_ROM_RESOURCE) &&
300 (!(res->flags & IORESOURCE_ROM_ENABLE))))
Yinghai Lu67cc7e22012-01-21 02:08:32 -0800301 add_to_list(fail_head,
302 dev_res->dev, res,
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700303 0 /* don't care */,
304 0 /* don't care */);
Yinghai Lu9a928662010-02-28 15:49:39 -0800305 }
Ram Paifc075e12011-02-14 17:43:19 -0800306 reset_resource(res);
Rajesh Shah542df5d2005-04-28 00:25:50 -0700307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
309}
310
Yinghai Luaa914f52013-07-25 06:31:38 -0700311static unsigned long pci_fail_res_type_mask(struct list_head *fail_head)
312{
313 struct pci_dev_resource *fail_res;
314 unsigned long mask = 0;
315
316 /* check failed type */
317 list_for_each_entry(fail_res, fail_head, list)
318 mask |= fail_res->flags;
319
320 /*
321 * one pref failed resource will set IORESOURCE_MEM,
322 * as we can allocate pref in non-pref range.
323 * Will release all assigned non-pref sibling resources
324 * according to that bit.
325 */
326 return mask & (IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH);
327}
328
329static bool pci_need_to_release(unsigned long mask, struct resource *res)
330{
331 if (res->flags & IORESOURCE_IO)
332 return !!(mask & IORESOURCE_IO);
333
334 /* check pref at first */
335 if (res->flags & IORESOURCE_PREFETCH) {
336 if (mask & IORESOURCE_PREFETCH)
337 return true;
338 /* count pref if its parent is non-pref */
339 else if ((mask & IORESOURCE_MEM) &&
340 !(res->parent->flags & IORESOURCE_PREFETCH))
341 return true;
342 else
343 return false;
344 }
345
346 if (res->flags & IORESOURCE_MEM)
347 return !!(mask & IORESOURCE_MEM);
348
349 return false; /* should not get here */
350}
351
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800352static void __assign_resources_sorted(struct list_head *head,
353 struct list_head *realloc_head,
354 struct list_head *fail_head)
Ram Paic8adf9a2011-02-14 17:43:20 -0800355{
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800356 /*
357 * Should not assign requested resources at first.
358 * they could be adjacent, so later reassign can not reallocate
359 * them one by one in parent resource window.
Masanari Iida367fa982012-07-23 22:39:51 +0900360 * Try to assign requested + add_size at beginning
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800361 * if could do that, could get out early.
362 * if could not do that, we still try to assign requested at first,
363 * then try to reassign add_size for some resources.
Yinghai Luaa914f52013-07-25 06:31:38 -0700364 *
365 * Separate three resource type checking if we need to release
366 * assigned resource after requested + add_size try.
367 * 1. if there is io port assign fail, will release assigned
368 * io port.
369 * 2. if there is pref mmio assign fail, release assigned
370 * pref mmio.
371 * if assigned pref mmio's parent is non-pref mmio and there
372 * is non-pref mmio assign fail, will release that assigned
373 * pref mmio.
374 * 3. if there is non-pref mmio assign fail or pref mmio
375 * assigned fail, will release assigned non-pref mmio.
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800376 */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800377 LIST_HEAD(save_head);
378 LIST_HEAD(local_fail_head);
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800379 struct pci_dev_resource *save_res;
Wei Yangd74b9022015-03-25 16:23:51 +0800380 struct pci_dev_resource *dev_res, *tmp_res, *dev_res2;
Yinghai Luaa914f52013-07-25 06:31:38 -0700381 unsigned long fail_type;
Wei Yangd74b9022015-03-25 16:23:51 +0800382 resource_size_t add_align, align;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800383
384 /* Check if optional add_size is there */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800385 if (!realloc_head || list_empty(realloc_head))
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800386 goto requested_and_reassign;
387
388 /* Save original start, end, flags etc at first */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800389 list_for_each_entry(dev_res, head, list) {
390 if (add_to_list(&save_head, dev_res->dev, dev_res->res, 0, 0)) {
Yinghai Lubffc56d2012-01-21 02:08:30 -0800391 free_list(&save_head);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800392 goto requested_and_reassign;
393 }
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800394 }
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800395
396 /* Update res in head list with add_size in realloc_head list */
Wei Yangd74b9022015-03-25 16:23:51 +0800397 list_for_each_entry_safe(dev_res, tmp_res, head, list) {
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800398 dev_res->res->end += get_res_add_size(realloc_head,
399 dev_res->res);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800400
Wei Yangd74b9022015-03-25 16:23:51 +0800401 /*
402 * There are two kinds of additional resources in the list:
403 * 1. bridge resource -- IORESOURCE_STARTALIGN
404 * 2. SR-IOV resource -- IORESOURCE_SIZEALIGN
405 * Here just fix the additional alignment for bridge
406 */
407 if (!(dev_res->res->flags & IORESOURCE_STARTALIGN))
408 continue;
409
410 add_align = get_res_add_align(realloc_head, dev_res->res);
411
412 /*
413 * The "head" list is sorted by the alignment to make sure
414 * resources with bigger alignment will be assigned first.
415 * After we change the alignment of a dev_res in "head" list,
416 * we need to reorder the list by alignment to make it
417 * consistent.
418 */
419 if (add_align > dev_res->res->start) {
Yinghai Lu552bc942015-05-28 22:40:00 -0700420 resource_size_t r_size = resource_size(dev_res->res);
421
Wei Yangd74b9022015-03-25 16:23:51 +0800422 dev_res->res->start = add_align;
Yinghai Lu552bc942015-05-28 22:40:00 -0700423 dev_res->res->end = add_align + r_size - 1;
Wei Yangd74b9022015-03-25 16:23:51 +0800424
425 list_for_each_entry(dev_res2, head, list) {
426 align = pci_resource_alignment(dev_res2->dev,
427 dev_res2->res);
Wei Yanga6b65982015-05-19 14:24:17 +0800428 if (add_align > align) {
Wei Yangd74b9022015-03-25 16:23:51 +0800429 list_move_tail(&dev_res->list,
430 &dev_res2->list);
Wei Yanga6b65982015-05-19 14:24:17 +0800431 break;
432 }
Wei Yangd74b9022015-03-25 16:23:51 +0800433 }
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800434 }
Wei Yangd74b9022015-03-25 16:23:51 +0800435
436 }
437
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800438 /* Try updated head list with add_size added */
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800439 assign_requested_resources_sorted(head, &local_fail_head);
440
441 /* all assigned with add_size ? */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800442 if (list_empty(&local_fail_head)) {
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800443 /* Remove head list from realloc_head list */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800444 list_for_each_entry(dev_res, head, list)
445 remove_from_list(realloc_head, dev_res->res);
Yinghai Lubffc56d2012-01-21 02:08:30 -0800446 free_list(&save_head);
447 free_list(head);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800448 return;
449 }
450
Yinghai Luaa914f52013-07-25 06:31:38 -0700451 /* check failed type */
452 fail_type = pci_fail_res_type_mask(&local_fail_head);
453 /* remove not need to be released assigned res from head list etc */
454 list_for_each_entry_safe(dev_res, tmp_res, head, list)
455 if (dev_res->res->parent &&
456 !pci_need_to_release(fail_type, dev_res->res)) {
457 /* remove it from realloc_head list */
458 remove_from_list(realloc_head, dev_res->res);
459 remove_from_list(&save_head, dev_res->res);
460 list_del(&dev_res->list);
461 kfree(dev_res);
462 }
463
Yinghai Lubffc56d2012-01-21 02:08:30 -0800464 free_list(&local_fail_head);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800465 /* Release assigned resource */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800466 list_for_each_entry(dev_res, head, list)
467 if (dev_res->res->parent)
468 release_resource(dev_res->res);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800469 /* Restore start/end/flags from saved list */
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800470 list_for_each_entry(save_res, &save_head, list) {
471 struct resource *res = save_res->res;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800472
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800473 res->start = save_res->start;
474 res->end = save_res->end;
475 res->flags = save_res->flags;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800476 }
Yinghai Lubffc56d2012-01-21 02:08:30 -0800477 free_list(&save_head);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800478
479requested_and_reassign:
Ram Paic8adf9a2011-02-14 17:43:20 -0800480 /* Satisfy the must-have resource requests */
481 assign_requested_resources_sorted(head, fail_head);
482
Ram Pai0a2daa12011-07-25 13:08:41 -0700483 /* Try to satisfy any additional optional resource
Ram Paic8adf9a2011-02-14 17:43:20 -0800484 requests */
Ram Pai9e8bf932011-07-25 13:08:42 -0700485 if (realloc_head)
486 reassign_resources_sorted(realloc_head, head);
Yinghai Lubffc56d2012-01-21 02:08:30 -0800487 free_list(head);
Ram Paic8adf9a2011-02-14 17:43:20 -0800488}
489
Yinghai Lu6841ec62010-01-22 01:02:25 -0800490static void pdev_assign_resources_sorted(struct pci_dev *dev,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800491 struct list_head *add_head,
492 struct list_head *fail_head)
Yinghai Lu6841ec62010-01-22 01:02:25 -0800493{
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800494 LIST_HEAD(head);
Yinghai Lu6841ec62010-01-22 01:02:25 -0800495
Yinghai Lu6841ec62010-01-22 01:02:25 -0800496 __dev_sort_resources(dev, &head);
Yinghai Lu8424d752012-01-21 02:08:21 -0800497 __assign_resources_sorted(&head, add_head, fail_head);
Yinghai Lu6841ec62010-01-22 01:02:25 -0800498
499}
500
501static void pbus_assign_resources_sorted(const struct pci_bus *bus,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800502 struct list_head *realloc_head,
503 struct list_head *fail_head)
Yinghai Lu6841ec62010-01-22 01:02:25 -0800504{
505 struct pci_dev *dev;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800506 LIST_HEAD(head);
Yinghai Lu6841ec62010-01-22 01:02:25 -0800507
Yinghai Lu6841ec62010-01-22 01:02:25 -0800508 list_for_each_entry(dev, &bus->devices, bus_list)
509 __dev_sort_resources(dev, &head);
510
Ram Pai9e8bf932011-07-25 13:08:42 -0700511 __assign_resources_sorted(&head, realloc_head, fail_head);
Yinghai Lu6841ec62010-01-22 01:02:25 -0800512}
513
Dominik Brodowskib3743fa2005-09-09 13:03:23 -0700514void pci_setup_cardbus(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
516 struct pci_dev *bridge = bus->self;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600517 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 struct pci_bus_region region;
519
Yinghai Lub918c622012-05-17 18:51:11 -0700520 dev_info(&bridge->dev, "CardBus bridge to %pR\n",
521 &bus->busn_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600523 res = bus->resource[0];
Yinghai Lufc279852013-12-09 22:54:40 -0800524 pcibios_resource_to_bus(bridge->bus, &region, res);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600525 if (res->flags & IORESOURCE_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 /*
527 * The IO resource is allocated a range twice as large as it
528 * would normally need. This allows us to set both IO regs.
529 */
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600530 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
532 region.start);
533 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
534 region.end);
535 }
536
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600537 res = bus->resource[1];
Yinghai Lufc279852013-12-09 22:54:40 -0800538 pcibios_resource_to_bus(bridge->bus, &region, res);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600539 if (res->flags & IORESOURCE_IO) {
540 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
542 region.start);
543 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
544 region.end);
545 }
546
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600547 res = bus->resource[2];
Yinghai Lufc279852013-12-09 22:54:40 -0800548 pcibios_resource_to_bus(bridge->bus, &region, res);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600549 if (res->flags & IORESOURCE_MEM) {
550 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
552 region.start);
553 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
554 region.end);
555 }
556
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600557 res = bus->resource[3];
Yinghai Lufc279852013-12-09 22:54:40 -0800558 pcibios_resource_to_bus(bridge->bus, &region, res);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600559 if (res->flags & IORESOURCE_MEM) {
560 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
562 region.start);
563 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
564 region.end);
565 }
566}
Dominik Brodowskib3743fa2005-09-09 13:03:23 -0700567EXPORT_SYMBOL(pci_setup_cardbus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569/* Initialize bridges with base/limit values we have collected.
570 PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
571 requires that if there is no I/O ports or memory behind the
572 bridge, corresponding range must be turned off by writing base
573 value greater than limit to the bridge's base/limit registers.
574
575 Note: care must be taken when updating I/O base/limit registers
576 of bridges which support 32-bit I/O. This update requires two
577 config space writes, so it's quite possible that an I/O window of
578 the bridge will have some undesirable address (e.g. 0) after the
579 first write. Ditto 64-bit prefetchable MMIO. */
Yinghai Lu3f2f4dc2015-01-15 10:22:31 -0600580static void pci_setup_bridge_io(struct pci_dev *bridge)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600582 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 struct pci_bus_region region;
Bjorn Helgaas2b28ae12012-07-09 13:38:57 -0600584 unsigned long io_mask;
585 u8 io_base_lo, io_limit_lo;
Bjorn Helgaas5b764b82013-11-27 17:24:50 -0700586 u16 l;
587 u32 io_upper16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Bjorn Helgaas2b28ae12012-07-09 13:38:57 -0600589 io_mask = PCI_IO_RANGE_MASK;
590 if (bridge->io_window_1k)
591 io_mask = PCI_IO_1K_RANGE_MASK;
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 /* Set up the top and bottom of the PCI I/O segment for this bus. */
Yinghai Lu3f2f4dc2015-01-15 10:22:31 -0600594 res = &bridge->resource[PCI_BRIDGE_RESOURCES + 0];
Yinghai Lufc279852013-12-09 22:54:40 -0800595 pcibios_resource_to_bus(bridge->bus, &region, res);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600596 if (res->flags & IORESOURCE_IO) {
Bjorn Helgaas5b764b82013-11-27 17:24:50 -0700597 pci_read_config_word(bridge, PCI_IO_BASE, &l);
Bjorn Helgaas2b28ae12012-07-09 13:38:57 -0600598 io_base_lo = (region.start >> 8) & io_mask;
599 io_limit_lo = (region.end >> 8) & io_mask;
Bjorn Helgaas5b764b82013-11-27 17:24:50 -0700600 l = ((u16) io_limit_lo << 8) | io_base_lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 /* Set up upper 16 bits of I/O base/limit. */
602 io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600603 dev_info(&bridge->dev, " bridge window %pR\n", res);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800604 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 /* Clear upper 16 bits of I/O base/limit. */
606 io_upper16 = 0;
607 l = 0x00f0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
609 /* Temporarily disable the I/O range before updating PCI_IO_BASE. */
610 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
611 /* Update lower 16 bits of I/O base/limit. */
Bjorn Helgaas5b764b82013-11-27 17:24:50 -0700612 pci_write_config_word(bridge, PCI_IO_BASE, l);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 /* Update upper 16 bits of I/O base/limit. */
614 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800615}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Yinghai Lu3f2f4dc2015-01-15 10:22:31 -0600617static void pci_setup_bridge_mmio(struct pci_dev *bridge)
Yinghai Lu7cc59972009-12-22 15:02:21 -0800618{
Yinghai Lu7cc59972009-12-22 15:02:21 -0800619 struct resource *res;
620 struct pci_bus_region region;
621 u32 l;
622
623 /* Set up the top and bottom of the PCI Memory segment for this bus. */
Yinghai Lu3f2f4dc2015-01-15 10:22:31 -0600624 res = &bridge->resource[PCI_BRIDGE_RESOURCES + 1];
Yinghai Lufc279852013-12-09 22:54:40 -0800625 pcibios_resource_to_bus(bridge->bus, &region, res);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600626 if (res->flags & IORESOURCE_MEM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 l = (region.start >> 16) & 0xfff0;
628 l |= region.end & 0xfff00000;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600629 dev_info(&bridge->dev, " bridge window %pR\n", res);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800630 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 l = 0x0000fff0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
633 pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800634}
635
Yinghai Lu3f2f4dc2015-01-15 10:22:31 -0600636static void pci_setup_bridge_mmio_pref(struct pci_dev *bridge)
Yinghai Lu7cc59972009-12-22 15:02:21 -0800637{
Yinghai Lu7cc59972009-12-22 15:02:21 -0800638 struct resource *res;
639 struct pci_bus_region region;
640 u32 l, bu, lu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 /* Clear out the upper 32 bits of PREF limit.
643 If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
644 disables PREF range, which is ok. */
645 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
646
647 /* Set up PREF base/limit. */
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100648 bu = lu = 0;
Yinghai Lu3f2f4dc2015-01-15 10:22:31 -0600649 res = &bridge->resource[PCI_BRIDGE_RESOURCES + 2];
Yinghai Lufc279852013-12-09 22:54:40 -0800650 pcibios_resource_to_bus(bridge->bus, &region, res);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600651 if (res->flags & IORESOURCE_PREFETCH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 l = (region.start >> 16) & 0xfff0;
653 l |= region.end & 0xfff00000;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600654 if (res->flags & IORESOURCE_MEM_64) {
Yinghai Lu1f82de12009-04-23 20:48:32 -0700655 bu = upper_32_bits(region.start);
656 lu = upper_32_bits(region.end);
Yinghai Lu1f82de12009-04-23 20:48:32 -0700657 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600658 dev_info(&bridge->dev, " bridge window %pR\n", res);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800659 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 l = 0x0000fff0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
663
Alex Williamson59353ea2009-11-30 14:51:44 -0700664 /* Set the upper 32 bits of PREF base & limit. */
665 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
666 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800667}
668
669static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type)
670{
671 struct pci_dev *bridge = bus->self;
672
Yinghai Lub918c622012-05-17 18:51:11 -0700673 dev_info(&bridge->dev, "PCI bridge to %pR\n",
674 &bus->busn_res);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800675
676 if (type & IORESOURCE_IO)
Yinghai Lu3f2f4dc2015-01-15 10:22:31 -0600677 pci_setup_bridge_io(bridge);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800678
679 if (type & IORESOURCE_MEM)
Yinghai Lu3f2f4dc2015-01-15 10:22:31 -0600680 pci_setup_bridge_mmio(bridge);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800681
682 if (type & IORESOURCE_PREFETCH)
Yinghai Lu3f2f4dc2015-01-15 10:22:31 -0600683 pci_setup_bridge_mmio_pref(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
686}
687
Gavin Shand366d282016-05-20 16:41:25 +1000688void __weak pcibios_setup_bridge(struct pci_bus *bus, unsigned long type)
689{
690}
691
Benjamin Herrenschmidte2444272011-09-11 14:08:38 -0300692void pci_setup_bridge(struct pci_bus *bus)
Yinghai Lu7cc59972009-12-22 15:02:21 -0800693{
694 unsigned long type = IORESOURCE_IO | IORESOURCE_MEM |
695 IORESOURCE_PREFETCH;
696
Gavin Shand366d282016-05-20 16:41:25 +1000697 pcibios_setup_bridge(bus, type);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800698 __pci_setup_bridge(bus, type);
699}
700
Yinghai Lu8505e722015-01-15 16:21:49 -0600701
702int pci_claim_bridge_resource(struct pci_dev *bridge, int i)
703{
704 if (i < PCI_BRIDGE_RESOURCES || i > PCI_BRIDGE_RESOURCE_END)
705 return 0;
706
707 if (pci_claim_resource(bridge, i) == 0)
708 return 0; /* claimed the window */
709
710 if ((bridge->class >> 8) != PCI_CLASS_BRIDGE_PCI)
711 return 0;
712
713 if (!pci_bus_clip_resource(bridge, i))
714 return -EINVAL; /* clipping didn't change anything */
715
716 switch (i - PCI_BRIDGE_RESOURCES) {
717 case 0:
718 pci_setup_bridge_io(bridge);
719 break;
720 case 1:
721 pci_setup_bridge_mmio(bridge);
722 break;
723 case 2:
724 pci_setup_bridge_mmio_pref(bridge);
725 break;
726 default:
727 return -EINVAL;
728 }
729
730 if (pci_claim_resource(bridge, i) == 0)
731 return 0; /* claimed a smaller window */
732
733 return -EINVAL;
734}
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736/* Check whether the bridge supports optional I/O and
737 prefetchable memory ranges. If not, the respective
738 base/limit registers must be read-only and read as 0. */
Sam Ravnborg96bde062007-03-26 21:53:30 -0800739static void pci_bridge_check_ranges(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
741 u16 io;
742 u32 pmem;
743 struct pci_dev *bridge = bus->self;
744 struct resource *b_res;
745
746 b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
747 b_res[1].flags |= IORESOURCE_MEM;
748
749 pci_read_config_word(bridge, PCI_IO_BASE, &io);
750 if (!io) {
Bjorn Helgaasd2f54d92013-11-27 15:31:07 -0700751 pci_write_config_word(bridge, PCI_IO_BASE, 0xe0f0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 pci_read_config_word(bridge, PCI_IO_BASE, &io);
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700753 pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
754 }
755 if (io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 b_res[0].flags |= IORESOURCE_IO;
Bjorn Helgaasd2f54d92013-11-27 15:31:07 -0700757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 /* DECchip 21050 pass 2 errata: the bridge may miss an address
759 disconnect boundary by one PCI data phase.
760 Workaround: do not use prefetching on this device. */
761 if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001)
762 return;
Bjorn Helgaasd2f54d92013-11-27 15:31:07 -0700763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
765 if (!pmem) {
766 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE,
Bjorn Helgaasd2f54d92013-11-27 15:31:07 -0700767 0xffe0fff0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
769 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0);
770 }
Yinghai Lu1f82de12009-04-23 20:48:32 -0700771 if (pmem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
Yinghai Lu99586102010-01-22 01:02:28 -0800773 if ((pmem & PCI_PREF_RANGE_TYPE_MASK) ==
774 PCI_PREF_RANGE_TYPE_64) {
Yinghai Lu1f82de12009-04-23 20:48:32 -0700775 b_res[2].flags |= IORESOURCE_MEM_64;
Yinghai Lu99586102010-01-22 01:02:28 -0800776 b_res[2].flags |= PCI_PREF_RANGE_TYPE_64;
777 }
Yinghai Lu1f82de12009-04-23 20:48:32 -0700778 }
779
780 /* double check if bridge does support 64 bit pref */
781 if (b_res[2].flags & IORESOURCE_MEM_64) {
782 u32 mem_base_hi, tmp;
783 pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32,
784 &mem_base_hi);
785 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
786 0xffffffff);
787 pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp);
788 if (!tmp)
789 b_res[2].flags &= ~IORESOURCE_MEM_64;
790 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
791 mem_base_hi);
792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793}
794
795/* Helper function for sizing routines: find first available
796 bus resource of a given type. Note: we intentionally skip
797 the bus resources which have already been assigned (that is,
798 have non-NULL parent resource). */
Yinghai Lu5b285412014-05-19 17:01:55 -0600799static struct resource *find_free_bus_resource(struct pci_bus *bus,
800 unsigned long type_mask, unsigned long type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
802 int i;
803 struct resource *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -0700805 pci_bus_for_each_resource(bus, r, i) {
Ivan Kokshaysky299de032005-06-15 18:59:27 +0400806 if (r == &ioport_resource || r == &iomem_resource)
807 continue;
Jesse Barnes55a10982009-10-27 09:39:18 -0700808 if (r && (r->flags & type_mask) == type && !r->parent)
809 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
811 return NULL;
812}
813
Ram Pai13583b12011-02-14 17:43:17 -0800814static resource_size_t calculate_iosize(resource_size_t size,
815 resource_size_t min_size,
816 resource_size_t size1,
817 resource_size_t old_size,
818 resource_size_t align)
819{
820 if (size < min_size)
821 size = min_size;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400822 if (old_size == 1)
Ram Pai13583b12011-02-14 17:43:17 -0800823 old_size = 0;
824 /* To be fixed in 2.5: we should have sort of HAVE_ISA
825 flag in the struct pci_bus. */
826#if defined(CONFIG_ISA) || defined(CONFIG_EISA)
827 size = (size & 0xff) + ((size & ~0xffUL) << 2);
828#endif
829 size = ALIGN(size + size1, align);
830 if (size < old_size)
831 size = old_size;
832 return size;
833}
834
835static resource_size_t calculate_memsize(resource_size_t size,
836 resource_size_t min_size,
837 resource_size_t size1,
838 resource_size_t old_size,
839 resource_size_t align)
840{
841 if (size < min_size)
842 size = min_size;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400843 if (old_size == 1)
Ram Pai13583b12011-02-14 17:43:17 -0800844 old_size = 0;
845 if (size < old_size)
846 size = old_size;
847 size = ALIGN(size + size1, align);
848 return size;
849}
850
Gavin Shanac5ad932012-09-11 16:59:45 -0600851resource_size_t __weak pcibios_window_alignment(struct pci_bus *bus,
852 unsigned long type)
853{
854 return 1;
855}
856
857#define PCI_P2P_DEFAULT_MEM_ALIGN 0x100000 /* 1MiB */
858#define PCI_P2P_DEFAULT_IO_ALIGN 0x1000 /* 4KiB */
859#define PCI_P2P_DEFAULT_IO_ALIGN_1K 0x400 /* 1KiB */
860
861static resource_size_t window_alignment(struct pci_bus *bus,
862 unsigned long type)
863{
864 resource_size_t align = 1, arch_align;
865
866 if (type & IORESOURCE_MEM)
867 align = PCI_P2P_DEFAULT_MEM_ALIGN;
868 else if (type & IORESOURCE_IO) {
869 /*
870 * Per spec, I/O windows are 4K-aligned, but some
871 * bridges have an extension to support 1K alignment.
872 */
873 if (bus->self->io_window_1k)
874 align = PCI_P2P_DEFAULT_IO_ALIGN_1K;
875 else
876 align = PCI_P2P_DEFAULT_IO_ALIGN;
877 }
878
879 arch_align = pcibios_window_alignment(bus, type);
880 return max(align, arch_align);
881}
882
Ram Paic8adf9a2011-02-14 17:43:20 -0800883/**
884 * pbus_size_io() - size the io window of a given bus
885 *
886 * @bus : the bus
887 * @min_size : the minimum io window that must to be allocated
888 * @add_size : additional optional io window
Ram Pai9e8bf932011-07-25 13:08:42 -0700889 * @realloc_head : track the additional io window on this list
Ram Paic8adf9a2011-02-14 17:43:20 -0800890 *
891 * Sizing the IO windows of the PCI-PCI bridge is trivial,
Yinghai Lufd591342012-07-09 19:55:29 -0600892 * since these windows have 1K or 4K granularity and the IO ranges
Ram Paic8adf9a2011-02-14 17:43:20 -0800893 * of non-bridge PCI devices are limited to 256 bytes.
894 * We must be careful with the ISA aliasing though.
895 */
896static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800897 resource_size_t add_size, struct list_head *realloc_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
899 struct pci_dev *dev;
Yinghai Lu5b285412014-05-19 17:01:55 -0600900 struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO,
901 IORESOURCE_IO);
Wei Yang11251a82013-08-02 17:31:05 +0800902 resource_size_t size = 0, size0 = 0, size1 = 0;
Yinghai Lube768912011-07-25 13:08:38 -0700903 resource_size_t children_add_size = 0;
Bjorn Helgaas2d1d6672013-08-05 16:15:10 -0600904 resource_size_t min_align, align;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 if (!b_res)
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700907 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Bjorn Helgaas2d1d6672013-08-05 16:15:10 -0600909 min_align = window_alignment(bus, IORESOURCE_IO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 list_for_each_entry(dev, &bus->devices, bus_list) {
911 int i;
912
913 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
914 struct resource *r = &dev->resource[i];
915 unsigned long r_size;
916
917 if (r->parent || !(r->flags & IORESOURCE_IO))
918 continue;
Zhao, Yu022edd82008-10-13 19:24:28 +0800919 r_size = resource_size(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 if (r_size < 0x400)
922 /* Might be re-aligned for ISA */
923 size += r_size;
924 else
925 size1 += r_size;
Yinghai Lube768912011-07-25 13:08:38 -0700926
Yinghai Lufd591342012-07-09 19:55:29 -0600927 align = pci_resource_alignment(dev, r);
928 if (align > min_align)
929 min_align = align;
930
Ram Pai9e8bf932011-07-25 13:08:42 -0700931 if (realloc_head)
932 children_add_size += get_res_add_size(realloc_head, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
934 }
Yinghai Lufd591342012-07-09 19:55:29 -0600935
Ram Paic8adf9a2011-02-14 17:43:20 -0800936 size0 = calculate_iosize(size, min_size, size1,
Yinghai Lufd591342012-07-09 19:55:29 -0600937 resource_size(b_res), min_align);
Yinghai Lube768912011-07-25 13:08:38 -0700938 if (children_add_size > add_size)
939 add_size = children_add_size;
Ram Pai9e8bf932011-07-25 13:08:42 -0700940 size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
Yinghai Lua4ac9fe2012-01-21 02:08:17 -0800941 calculate_iosize(size, min_size, add_size + size1,
Yinghai Lufd591342012-07-09 19:55:29 -0600942 resource_size(b_res), min_align);
Ram Paic8adf9a2011-02-14 17:43:20 -0800943 if (!size0 && !size1) {
Bjorn Helgaas865df572009-11-04 10:32:57 -0700944 if (b_res->start || b_res->end)
Ryan Desfosses227f0642014-04-18 20:13:50 -0400945 dev_info(&bus->self->dev, "disabling bridge window %pR to %pR (unused)\n",
946 b_res, &bus->busn_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 b_res->flags = 0;
948 return;
949 }
Yinghai Lufd591342012-07-09 19:55:29 -0600950
951 b_res->start = min_align;
Ram Paic8adf9a2011-02-14 17:43:20 -0800952 b_res->end = b_res->start + size0 - 1;
Ivan Kokshaysky88452562008-03-30 19:50:14 +0400953 b_res->flags |= IORESOURCE_STARTALIGN;
Yinghai Lub5924432012-01-21 02:08:31 -0800954 if (size1 > size0 && realloc_head) {
Yinghai Lufd591342012-07-09 19:55:29 -0600955 add_to_list(realloc_head, bus->self, b_res, size1-size0,
956 min_align);
Ryan Desfosses227f0642014-04-18 20:13:50 -0400957 dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window %pR to %pR add_size %llx\n",
958 b_res, &bus->busn_res,
959 (unsigned long long)size1-size0);
Yinghai Lub5924432012-01-21 02:08:31 -0800960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
Gavin Shanc1215042012-09-11 16:59:46 -0600963static inline resource_size_t calculate_mem_align(resource_size_t *aligns,
964 int max_order)
965{
966 resource_size_t align = 0;
967 resource_size_t min_align = 0;
968 int order;
969
970 for (order = 0; order <= max_order; order++) {
971 resource_size_t align1 = 1;
972
973 align1 <<= (order + 20);
974
975 if (!align)
976 min_align = align1;
977 else if (ALIGN(align + min_align, min_align) < align1)
978 min_align = align1 >> 1;
979 align += aligns[order];
980 }
981
982 return min_align;
983}
984
Ram Paic8adf9a2011-02-14 17:43:20 -0800985/**
986 * pbus_size_mem() - size the memory window of a given bus
987 *
988 * @bus : the bus
Wei Yang496f70c2013-08-02 17:31:04 +0800989 * @mask: mask the resource flag, then compare it with type
990 * @type: the type of free resource from bridge
Yinghai Lu5b285412014-05-19 17:01:55 -0600991 * @type2: second match type
992 * @type3: third match type
Ram Paic8adf9a2011-02-14 17:43:20 -0800993 * @min_size : the minimum memory window that must to be allocated
994 * @add_size : additional optional memory window
Ram Pai9e8bf932011-07-25 13:08:42 -0700995 * @realloc_head : track the additional memory window on this list
Ram Paic8adf9a2011-02-14 17:43:20 -0800996 *
997 * Calculate the size of the bus and minimal alignment which
998 * guarantees that all child resources fit in this size.
Bjorn Helgaas30afe8d2014-05-19 18:28:37 -0600999 *
1000 * Returns -ENOSPC if there's no available bus resource of the desired type.
1001 * Otherwise, sets the bus resource start/end to indicate the required
1002 * size, adds things to realloc_head (if supplied), and returns 0.
Ram Paic8adf9a2011-02-14 17:43:20 -08001003 */
Eric W. Biederman28760482009-09-09 14:09:24 -07001004static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
Yinghai Lu5b285412014-05-19 17:01:55 -06001005 unsigned long type, unsigned long type2,
1006 unsigned long type3,
1007 resource_size_t min_size, resource_size_t add_size,
1008 struct list_head *realloc_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
1010 struct pci_dev *dev;
Ram Paic8adf9a2011-02-14 17:43:20 -08001011 resource_size_t min_align, align, size, size0, size1;
Yinghai Lu096d4222014-07-03 13:46:17 -07001012 resource_size_t aligns[18]; /* Alignments from 1Mb to 128Gb */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 int order, max_order;
Yinghai Lu5b285412014-05-19 17:01:55 -06001014 struct resource *b_res = find_free_bus_resource(bus,
1015 mask | IORESOURCE_PREFETCH, type);
Yinghai Lube768912011-07-25 13:08:38 -07001016 resource_size_t children_add_size = 0;
Wei Yangd74b9022015-03-25 16:23:51 +08001017 resource_size_t children_add_align = 0;
1018 resource_size_t add_align = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 if (!b_res)
Bjorn Helgaas30afe8d2014-05-19 18:28:37 -06001021 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 memset(aligns, 0, sizeof(aligns));
1024 max_order = 0;
1025 size = 0;
1026
1027 list_for_each_entry(dev, &bus->devices, bus_list) {
1028 int i;
Yinghai Lu1f82de12009-04-23 20:48:32 -07001029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
1031 struct resource *r = &dev->resource[i];
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +11001032 resource_size_t r_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
David Daneya2220d82015-10-29 17:35:39 -05001034 if (r->parent || (r->flags & IORESOURCE_PCI_FIXED) ||
1035 ((r->flags & mask) != type &&
1036 (r->flags & mask) != type2 &&
1037 (r->flags & mask) != type3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 continue;
Zhao, Yu022edd82008-10-13 19:24:28 +08001039 r_size = resource_size(r);
Yinghai Lu2aceefc2011-07-25 13:08:40 -07001040#ifdef CONFIG_PCI_IOV
1041 /* put SRIOV requested res to the optional list */
Ram Pai9e8bf932011-07-25 13:08:42 -07001042 if (realloc_head && i >= PCI_IOV_RESOURCES &&
Yinghai Lu2aceefc2011-07-25 13:08:40 -07001043 i <= PCI_IOV_RESOURCE_END) {
Wei Yangd74b9022015-03-25 16:23:51 +08001044 add_align = max(pci_resource_alignment(dev, r), add_align);
Yinghai Lu2aceefc2011-07-25 13:08:40 -07001045 r->end = r->start - 1;
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001046 add_to_list(realloc_head, dev, r, r_size, 0/* don't care */);
Yinghai Lu2aceefc2011-07-25 13:08:40 -07001047 children_add_size += r_size;
1048 continue;
1049 }
1050#endif
Alan14c85302014-05-19 14:03:14 +01001051 /*
1052 * aligns[0] is for 1MB (since bridge memory
1053 * windows are always at least 1MB aligned), so
1054 * keep "order" from being negative for smaller
1055 * resources.
1056 */
Chris Wright6faf17f2009-08-28 13:00:06 -07001057 align = pci_resource_alignment(dev, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 order = __ffs(align) - 20;
Alan14c85302014-05-19 14:03:14 +01001059 if (order < 0)
1060 order = 0;
1061 if (order >= ARRAY_SIZE(aligns)) {
Ryan Desfosses227f0642014-04-18 20:13:50 -04001062 dev_warn(&dev->dev, "disabling BAR %d: %pR (bad alignment %#llx)\n",
1063 i, r, (unsigned long long) align);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 r->flags = 0;
1065 continue;
1066 }
Yongji Xiec9c75142017-04-10 19:58:11 +08001067 size += max(r_size, align);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 /* Exclude ranges with size > align from
1069 calculation of the alignment. */
Yongji Xiec9c75142017-04-10 19:58:11 +08001070 if (r_size <= align)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 aligns[order] += align;
1072 if (order > max_order)
1073 max_order = order;
Yinghai Lube768912011-07-25 13:08:38 -07001074
Wei Yangd74b9022015-03-25 16:23:51 +08001075 if (realloc_head) {
Ram Pai9e8bf932011-07-25 13:08:42 -07001076 children_add_size += get_res_add_size(realloc_head, r);
Wei Yangd74b9022015-03-25 16:23:51 +08001077 children_add_align = get_res_add_align(realloc_head, r);
1078 add_align = max(add_align, children_add_align);
1079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 }
1081 }
Jeremy Fitzhardinge8308c542008-09-11 01:31:50 -07001082
Gavin Shanc1215042012-09-11 16:59:46 -06001083 min_align = calculate_mem_align(aligns, max_order);
Wei Yang3ad94b02013-09-06 09:45:58 +08001084 min_align = max(min_align, window_alignment(bus, b_res->flags));
Linus Torvaldsb42282e2011-04-11 10:53:11 -07001085 size0 = calculate_memsize(size, min_size, 0, resource_size(b_res), min_align);
Wei Yangd74b9022015-03-25 16:23:51 +08001086 add_align = max(min_align, add_align);
Yinghai Lube768912011-07-25 13:08:38 -07001087 if (children_add_size > add_size)
1088 add_size = children_add_size;
Ram Pai9e8bf932011-07-25 13:08:42 -07001089 size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
Yinghai Lua4ac9fe2012-01-21 02:08:17 -08001090 calculate_memsize(size, min_size, add_size,
Wei Yangd74b9022015-03-25 16:23:51 +08001091 resource_size(b_res), add_align);
Ram Paic8adf9a2011-02-14 17:43:20 -08001092 if (!size0 && !size1) {
Bjorn Helgaas865df572009-11-04 10:32:57 -07001093 if (b_res->start || b_res->end)
Ryan Desfosses227f0642014-04-18 20:13:50 -04001094 dev_info(&bus->self->dev, "disabling bridge window %pR to %pR (unused)\n",
1095 b_res, &bus->busn_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 b_res->flags = 0;
Bjorn Helgaas30afe8d2014-05-19 18:28:37 -06001097 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 }
1099 b_res->start = min_align;
Ram Paic8adf9a2011-02-14 17:43:20 -08001100 b_res->end = size0 + min_align - 1;
Yinghai Lu5b285412014-05-19 17:01:55 -06001101 b_res->flags |= IORESOURCE_STARTALIGN;
Yinghai Lub5924432012-01-21 02:08:31 -08001102 if (size1 > size0 && realloc_head) {
Wei Yangd74b9022015-03-25 16:23:51 +08001103 add_to_list(realloc_head, bus->self, b_res, size1-size0, add_align);
1104 dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window %pR to %pR add_size %llx add_align %llx\n",
Ryan Desfosses227f0642014-04-18 20:13:50 -04001105 b_res, &bus->busn_res,
Wei Yangd74b9022015-03-25 16:23:51 +08001106 (unsigned long long) (size1 - size0),
1107 (unsigned long long) add_align);
Yinghai Lub5924432012-01-21 02:08:31 -08001108 }
Bjorn Helgaas30afe8d2014-05-19 18:28:37 -06001109 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110}
1111
Ram Pai0a2daa12011-07-25 13:08:41 -07001112unsigned long pci_cardbus_resource_alignment(struct resource *res)
1113{
1114 if (res->flags & IORESOURCE_IO)
1115 return pci_cardbus_io_size;
1116 if (res->flags & IORESOURCE_MEM)
1117 return pci_cardbus_mem_size;
1118 return 0;
1119}
1120
1121static void pci_bus_size_cardbus(struct pci_bus *bus,
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001122 struct list_head *realloc_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
1124 struct pci_dev *bridge = bus->self;
1125 struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
Yinghai Lu11848932012-02-10 15:33:47 -08001126 resource_size_t b_res_3_size = pci_cardbus_mem_size * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 u16 ctrl;
1128
Yinghai Lu3796f1e2012-02-10 15:33:48 -08001129 if (b_res[0].parent)
1130 goto handle_b_res_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 /*
1132 * Reserve some resources for CardBus. We reserve
1133 * a fixed amount of bus space for CardBus bridges.
1134 */
Yinghai Lu11848932012-02-10 15:33:47 -08001135 b_res[0].start = pci_cardbus_io_size;
1136 b_res[0].end = b_res[0].start + pci_cardbus_io_size - 1;
1137 b_res[0].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
1138 if (realloc_head) {
1139 b_res[0].end -= pci_cardbus_io_size;
1140 add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size,
1141 pci_cardbus_io_size);
1142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Yinghai Lu3796f1e2012-02-10 15:33:48 -08001144handle_b_res_1:
1145 if (b_res[1].parent)
1146 goto handle_b_res_2;
Yinghai Lu11848932012-02-10 15:33:47 -08001147 b_res[1].start = pci_cardbus_io_size;
1148 b_res[1].end = b_res[1].start + pci_cardbus_io_size - 1;
1149 b_res[1].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
1150 if (realloc_head) {
1151 b_res[1].end -= pci_cardbus_io_size;
1152 add_to_list(realloc_head, bridge, b_res+1, pci_cardbus_io_size,
1153 pci_cardbus_io_size);
1154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Yinghai Lu3796f1e2012-02-10 15:33:48 -08001156handle_b_res_2:
Yinghai Ludcef0d02012-02-10 15:33:46 -08001157 /* MEM1 must not be pref mmio */
1158 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
1159 if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM1) {
1160 ctrl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1;
1161 pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
1162 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
1163 }
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 /*
1166 * Check whether prefetchable memory is supported
1167 * by this bridge.
1168 */
1169 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
1170 if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
1171 ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
1172 pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
1173 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
1174 }
1175
Yinghai Lu3796f1e2012-02-10 15:33:48 -08001176 if (b_res[2].parent)
1177 goto handle_b_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 /*
1179 * If we have prefetchable memory support, allocate
1180 * two regions. Otherwise, allocate one region of
1181 * twice the size.
1182 */
1183 if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
Yinghai Lu11848932012-02-10 15:33:47 -08001184 b_res[2].start = pci_cardbus_mem_size;
1185 b_res[2].end = b_res[2].start + pci_cardbus_mem_size - 1;
1186 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH |
1187 IORESOURCE_STARTALIGN;
1188 if (realloc_head) {
1189 b_res[2].end -= pci_cardbus_mem_size;
1190 add_to_list(realloc_head, bridge, b_res+2,
1191 pci_cardbus_mem_size, pci_cardbus_mem_size);
1192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Yinghai Lu11848932012-02-10 15:33:47 -08001194 /* reduce that to half */
1195 b_res_3_size = pci_cardbus_mem_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 }
Ram Pai0a2daa12011-07-25 13:08:41 -07001197
Yinghai Lu3796f1e2012-02-10 15:33:48 -08001198handle_b_res_3:
1199 if (b_res[3].parent)
1200 goto handle_done;
Yinghai Lu11848932012-02-10 15:33:47 -08001201 b_res[3].start = pci_cardbus_mem_size;
1202 b_res[3].end = b_res[3].start + b_res_3_size - 1;
1203 b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_STARTALIGN;
1204 if (realloc_head) {
1205 b_res[3].end -= b_res_3_size;
1206 add_to_list(realloc_head, bridge, b_res+3, b_res_3_size,
1207 pci_cardbus_mem_size);
1208 }
Yinghai Lu3796f1e2012-02-10 15:33:48 -08001209
1210handle_done:
1211 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
Bjorn Helgaas10874f52014-04-14 16:11:40 -06001214void __pci_bus_size_bridges(struct pci_bus *bus, struct list_head *realloc_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
1216 struct pci_dev *dev;
Yinghai Lu5b285412014-05-19 17:01:55 -06001217 unsigned long mask, prefmask, type2 = 0, type3 = 0;
Ram Paic8adf9a2011-02-14 17:43:20 -08001218 resource_size_t additional_mem_size = 0, additional_io_size = 0;
Yinghai Lu5b285412014-05-19 17:01:55 -06001219 struct resource *b_res;
Bjorn Helgaas30afe8d2014-05-19 18:28:37 -06001220 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222 list_for_each_entry(dev, &bus->devices, bus_list) {
1223 struct pci_bus *b = dev->subordinate;
1224 if (!b)
1225 continue;
1226
1227 switch (dev->class >> 8) {
1228 case PCI_CLASS_BRIDGE_CARDBUS:
Ram Pai9e8bf932011-07-25 13:08:42 -07001229 pci_bus_size_cardbus(b, realloc_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 break;
1231
1232 case PCI_CLASS_BRIDGE_PCI:
1233 default:
Ram Pai9e8bf932011-07-25 13:08:42 -07001234 __pci_bus_size_bridges(b, realloc_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 break;
1236 }
1237 }
1238
1239 /* The root bus? */
Wei Yang2ba29e22013-09-06 09:45:56 +08001240 if (pci_is_root_bus(bus))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 return;
1242
1243 switch (bus->self->class >> 8) {
1244 case PCI_CLASS_BRIDGE_CARDBUS:
1245 /* don't size cardbuses yet. */
1246 break;
1247
1248 case PCI_CLASS_BRIDGE_PCI:
1249 pci_bridge_check_ranges(bus);
Eric W. Biederman28760482009-09-09 14:09:24 -07001250 if (bus->self->is_hotplug_bridge) {
Ram Paic8adf9a2011-02-14 17:43:20 -08001251 additional_io_size = pci_hotplug_io_size;
1252 additional_mem_size = pci_hotplug_mem_size;
Eric W. Biederman28760482009-09-09 14:09:24 -07001253 }
Bjorn Helgaas67d29b52014-05-19 18:32:18 -06001254 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 default:
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001256 pbus_size_io(bus, realloc_head ? 0 : additional_io_size,
1257 additional_io_size, realloc_head);
Bjorn Helgaas67d29b52014-05-19 18:32:18 -06001258
1259 /*
1260 * If there's a 64-bit prefetchable MMIO window, compute
1261 * the size required to put all 64-bit prefetchable
1262 * resources in it.
1263 */
Yinghai Lu5b285412014-05-19 17:01:55 -06001264 b_res = &bus->self->resource[PCI_BRIDGE_RESOURCES];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 mask = IORESOURCE_MEM;
1266 prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
Yinghai Lu5b285412014-05-19 17:01:55 -06001267 if (b_res[2].flags & IORESOURCE_MEM_64) {
1268 prefmask |= IORESOURCE_MEM_64;
Bjorn Helgaas30afe8d2014-05-19 18:28:37 -06001269 ret = pbus_size_mem(bus, prefmask, prefmask,
Yinghai Lu5b285412014-05-19 17:01:55 -06001270 prefmask, prefmask,
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001271 realloc_head ? 0 : additional_mem_size,
Bjorn Helgaas30afe8d2014-05-19 18:28:37 -06001272 additional_mem_size, realloc_head);
Bjorn Helgaas67d29b52014-05-19 18:32:18 -06001273
1274 /*
1275 * If successful, all non-prefetchable resources
1276 * and any 32-bit prefetchable resources will go in
1277 * the non-prefetchable window.
1278 */
Bjorn Helgaas30afe8d2014-05-19 18:28:37 -06001279 if (ret == 0) {
Bjorn Helgaas30afe8d2014-05-19 18:28:37 -06001280 mask = prefmask;
1281 type2 = prefmask & ~IORESOURCE_MEM_64;
1282 type3 = prefmask & ~IORESOURCE_PREFETCH;
Yinghai Lu5b285412014-05-19 17:01:55 -06001283 }
1284 }
Bjorn Helgaas67d29b52014-05-19 18:32:18 -06001285
1286 /*
1287 * If there is no 64-bit prefetchable window, compute the
1288 * size required to put all prefetchable resources in the
1289 * 32-bit prefetchable window (if there is one).
1290 */
Yinghai Lu5b285412014-05-19 17:01:55 -06001291 if (!type2) {
1292 prefmask &= ~IORESOURCE_MEM_64;
Bjorn Helgaas30afe8d2014-05-19 18:28:37 -06001293 ret = pbus_size_mem(bus, prefmask, prefmask,
Yinghai Lu5b285412014-05-19 17:01:55 -06001294 prefmask, prefmask,
1295 realloc_head ? 0 : additional_mem_size,
Bjorn Helgaas30afe8d2014-05-19 18:28:37 -06001296 additional_mem_size, realloc_head);
Bjorn Helgaas67d29b52014-05-19 18:32:18 -06001297
1298 /*
1299 * If successful, only non-prefetchable resources
1300 * will go in the non-prefetchable window.
1301 */
1302 if (ret == 0)
Yinghai Lu5b285412014-05-19 17:01:55 -06001303 mask = prefmask;
Bjorn Helgaas67d29b52014-05-19 18:32:18 -06001304 else
Yinghai Lu5b285412014-05-19 17:01:55 -06001305 additional_mem_size += additional_mem_size;
Bjorn Helgaas67d29b52014-05-19 18:32:18 -06001306
Yinghai Lu5b285412014-05-19 17:01:55 -06001307 type2 = type3 = IORESOURCE_MEM;
1308 }
Bjorn Helgaas67d29b52014-05-19 18:32:18 -06001309
1310 /*
1311 * Compute the size required to put everything else in the
1312 * non-prefetchable window. This includes:
1313 *
1314 * - all non-prefetchable resources
1315 * - 32-bit prefetchable resources if there's a 64-bit
1316 * prefetchable window or no prefetchable window at all
1317 * - 64-bit prefetchable resources if there's no
1318 * prefetchable window at all
1319 *
1320 * Note that the strategy in __pci_assign_resource() must
1321 * match that used here. Specifically, we cannot put a
1322 * 32-bit prefetchable resource in a 64-bit prefetchable
1323 * window.
1324 */
Yinghai Lu5b285412014-05-19 17:01:55 -06001325 pbus_size_mem(bus, mask, IORESOURCE_MEM, type2, type3,
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001326 realloc_head ? 0 : additional_mem_size,
1327 additional_mem_size, realloc_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 break;
1329 }
1330}
Ram Paic8adf9a2011-02-14 17:43:20 -08001331
Bjorn Helgaas10874f52014-04-14 16:11:40 -06001332void pci_bus_size_bridges(struct pci_bus *bus)
Ram Paic8adf9a2011-02-14 17:43:20 -08001333{
1334 __pci_bus_size_bridges(bus, NULL);
1335}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336EXPORT_SYMBOL(pci_bus_size_bridges);
1337
David Daneyd04d0112015-10-29 17:35:39 -05001338static void assign_fixed_resource_on_bus(struct pci_bus *b, struct resource *r)
1339{
1340 int i;
1341 struct resource *parent_r;
1342 unsigned long mask = IORESOURCE_IO | IORESOURCE_MEM |
1343 IORESOURCE_PREFETCH;
1344
1345 pci_bus_for_each_resource(b, parent_r, i) {
1346 if (!parent_r)
1347 continue;
1348
1349 if ((r->flags & mask) == (parent_r->flags & mask) &&
1350 resource_contains(parent_r, r))
1351 request_resource(parent_r, r);
1352 }
1353}
1354
1355/*
1356 * Try to assign any resources marked as IORESOURCE_PCI_FIXED, as they
1357 * are skipped by pbus_assign_resources_sorted().
1358 */
1359static void pdev_assign_fixed_resources(struct pci_dev *dev)
1360{
1361 int i;
1362
1363 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
1364 struct pci_bus *b;
1365 struct resource *r = &dev->resource[i];
1366
1367 if (r->parent || !(r->flags & IORESOURCE_PCI_FIXED) ||
1368 !(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
1369 continue;
1370
1371 b = dev->bus;
1372 while (b && !r->parent) {
1373 assign_fixed_resource_on_bus(b, r);
1374 b = b->parent;
1375 }
1376 }
1377}
1378
Bjorn Helgaas10874f52014-04-14 16:11:40 -06001379void __pci_bus_assign_resources(const struct pci_bus *bus,
1380 struct list_head *realloc_head,
1381 struct list_head *fail_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
1383 struct pci_bus *b;
1384 struct pci_dev *dev;
1385
Ram Pai9e8bf932011-07-25 13:08:42 -07001386 pbus_assign_resources_sorted(bus, realloc_head, fail_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 list_for_each_entry(dev, &bus->devices, bus_list) {
David Daneyd04d0112015-10-29 17:35:39 -05001389 pdev_assign_fixed_resources(dev);
1390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 b = dev->subordinate;
1392 if (!b)
1393 continue;
1394
Ram Pai9e8bf932011-07-25 13:08:42 -07001395 __pci_bus_assign_resources(b, realloc_head, fail_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 switch (dev->class >> 8) {
1398 case PCI_CLASS_BRIDGE_PCI:
Yinghai Lu6841ec62010-01-22 01:02:25 -08001399 if (!pci_is_enabled(dev))
1400 pci_setup_bridge(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 break;
1402
1403 case PCI_CLASS_BRIDGE_CARDBUS:
1404 pci_setup_cardbus(b);
1405 break;
1406
1407 default:
Ryan Desfosses227f0642014-04-18 20:13:50 -04001408 dev_info(&dev->dev, "not setting up bridge for bus %04x:%02x\n",
1409 pci_domain_nr(b), b->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 break;
1411 }
1412 }
1413}
Yinghai Lu568ddef2010-01-22 01:02:21 -08001414
Bjorn Helgaas10874f52014-04-14 16:11:40 -06001415void pci_bus_assign_resources(const struct pci_bus *bus)
Yinghai Lu568ddef2010-01-22 01:02:21 -08001416{
Ram Paic8adf9a2011-02-14 17:43:20 -08001417 __pci_bus_assign_resources(bus, NULL, NULL);
Yinghai Lu568ddef2010-01-22 01:02:21 -08001418}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419EXPORT_SYMBOL(pci_bus_assign_resources);
1420
Lorenzo Pieralisi765bf9b2016-06-08 12:04:47 +01001421static void pci_claim_device_resources(struct pci_dev *dev)
1422{
1423 int i;
1424
1425 for (i = 0; i < PCI_BRIDGE_RESOURCES; i++) {
1426 struct resource *r = &dev->resource[i];
1427
1428 if (!r->flags || r->parent)
1429 continue;
1430
1431 pci_claim_resource(dev, i);
1432 }
1433}
1434
1435static void pci_claim_bridge_resources(struct pci_dev *dev)
1436{
1437 int i;
1438
1439 for (i = PCI_BRIDGE_RESOURCES; i < PCI_NUM_RESOURCES; i++) {
1440 struct resource *r = &dev->resource[i];
1441
1442 if (!r->flags || r->parent)
1443 continue;
1444
1445 pci_claim_bridge_resource(dev, i);
1446 }
1447}
1448
1449static void pci_bus_allocate_dev_resources(struct pci_bus *b)
1450{
1451 struct pci_dev *dev;
1452 struct pci_bus *child;
1453
1454 list_for_each_entry(dev, &b->devices, bus_list) {
1455 pci_claim_device_resources(dev);
1456
1457 child = dev->subordinate;
1458 if (child)
1459 pci_bus_allocate_dev_resources(child);
1460 }
1461}
1462
1463static void pci_bus_allocate_resources(struct pci_bus *b)
1464{
1465 struct pci_bus *child;
1466
1467 /*
1468 * Carry out a depth-first search on the PCI bus
1469 * tree to allocate bridge apertures. Read the
1470 * programmed bridge bases and recursively claim
1471 * the respective bridge resources.
1472 */
1473 if (b->self) {
1474 pci_read_bridge_bases(b);
1475 pci_claim_bridge_resources(b->self);
1476 }
1477
1478 list_for_each_entry(child, &b->children, node)
1479 pci_bus_allocate_resources(child);
1480}
1481
1482void pci_bus_claim_resources(struct pci_bus *b)
1483{
1484 pci_bus_allocate_resources(b);
1485 pci_bus_allocate_dev_resources(b);
1486}
1487EXPORT_SYMBOL(pci_bus_claim_resources);
1488
Bjorn Helgaas10874f52014-04-14 16:11:40 -06001489static void __pci_bridge_assign_resources(const struct pci_dev *bridge,
1490 struct list_head *add_head,
1491 struct list_head *fail_head)
Yinghai Lu6841ec62010-01-22 01:02:25 -08001492{
1493 struct pci_bus *b;
1494
Yinghai Lu8424d752012-01-21 02:08:21 -08001495 pdev_assign_resources_sorted((struct pci_dev *)bridge,
1496 add_head, fail_head);
Yinghai Lu6841ec62010-01-22 01:02:25 -08001497
1498 b = bridge->subordinate;
1499 if (!b)
1500 return;
1501
Yinghai Lu8424d752012-01-21 02:08:21 -08001502 __pci_bus_assign_resources(b, add_head, fail_head);
Yinghai Lu6841ec62010-01-22 01:02:25 -08001503
1504 switch (bridge->class >> 8) {
1505 case PCI_CLASS_BRIDGE_PCI:
1506 pci_setup_bridge(b);
1507 break;
1508
1509 case PCI_CLASS_BRIDGE_CARDBUS:
1510 pci_setup_cardbus(b);
1511 break;
1512
1513 default:
Ryan Desfosses227f0642014-04-18 20:13:50 -04001514 dev_info(&bridge->dev, "not setting up bridge for bus %04x:%02x\n",
1515 pci_domain_nr(b), b->number);
Yinghai Lu6841ec62010-01-22 01:02:25 -08001516 break;
1517 }
1518}
Christian Königcb21bc92017-10-18 15:58:17 +02001519
1520#define PCI_RES_TYPE_MASK \
1521 (IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH |\
1522 IORESOURCE_MEM_64)
1523
Yinghai Lu5009b462010-01-22 01:02:20 -08001524static void pci_bridge_release_resources(struct pci_bus *bus,
1525 unsigned long type)
1526{
Yinghai Lu5b285412014-05-19 17:01:55 -06001527 struct pci_dev *dev = bus->self;
Yinghai Lu5009b462010-01-22 01:02:20 -08001528 struct resource *r;
Yinghai Lu5b285412014-05-19 17:01:55 -06001529 unsigned old_flags = 0;
1530 struct resource *b_res;
1531 int idx = 1;
Yinghai Lu5009b462010-01-22 01:02:20 -08001532
Yinghai Lu5b285412014-05-19 17:01:55 -06001533 b_res = &dev->resource[PCI_BRIDGE_RESOURCES];
Yinghai Lu5009b462010-01-22 01:02:20 -08001534
Yinghai Lu5b285412014-05-19 17:01:55 -06001535 /*
1536 * 1. if there is io port assign fail, will release bridge
1537 * io port.
1538 * 2. if there is non pref mmio assign fail, release bridge
1539 * nonpref mmio.
1540 * 3. if there is 64bit pref mmio assign fail, and bridge pref
1541 * is 64bit, release bridge pref mmio.
1542 * 4. if there is pref mmio assign fail, and bridge pref is
1543 * 32bit mmio, release bridge pref mmio
1544 * 5. if there is pref mmio assign fail, and bridge pref is not
1545 * assigned, release bridge nonpref mmio.
1546 */
1547 if (type & IORESOURCE_IO)
1548 idx = 0;
1549 else if (!(type & IORESOURCE_PREFETCH))
1550 idx = 1;
1551 else if ((type & IORESOURCE_MEM_64) &&
1552 (b_res[2].flags & IORESOURCE_MEM_64))
1553 idx = 2;
1554 else if (!(b_res[2].flags & IORESOURCE_MEM_64) &&
1555 (b_res[2].flags & IORESOURCE_PREFETCH))
1556 idx = 2;
1557 else
1558 idx = 1;
1559
1560 r = &b_res[idx];
1561
1562 if (!r->parent)
1563 return;
1564
1565 /*
1566 * if there are children under that, we should release them
1567 * all
1568 */
1569 release_child_resources(r);
1570 if (!release_resource(r)) {
Christian Königcb21bc92017-10-18 15:58:17 +02001571 type = old_flags = r->flags & PCI_RES_TYPE_MASK;
Yinghai Lu5b285412014-05-19 17:01:55 -06001572 dev_printk(KERN_DEBUG, &dev->dev, "resource %d %pR released\n",
1573 PCI_BRIDGE_RESOURCES + idx, r);
1574 /* keep the old size */
1575 r->end = resource_size(r) - 1;
1576 r->start = 0;
1577 r->flags = 0;
1578
Yinghai Lu5009b462010-01-22 01:02:20 -08001579 /* avoiding touch the one without PREF */
1580 if (type & IORESOURCE_PREFETCH)
1581 type = IORESOURCE_PREFETCH;
1582 __pci_setup_bridge(bus, type);
Yinghai Lu5b285412014-05-19 17:01:55 -06001583 /* for next child res under same bridge */
1584 r->flags = old_flags;
Yinghai Lu5009b462010-01-22 01:02:20 -08001585 }
1586}
1587
1588enum release_type {
1589 leaf_only,
1590 whole_subtree,
1591};
1592/*
1593 * try to release pci bridge resources that is from leaf bridge,
1594 * so we can allocate big new one later
1595 */
Bjorn Helgaas10874f52014-04-14 16:11:40 -06001596static void pci_bus_release_bridge_resources(struct pci_bus *bus,
1597 unsigned long type,
1598 enum release_type rel_type)
Yinghai Lu5009b462010-01-22 01:02:20 -08001599{
1600 struct pci_dev *dev;
1601 bool is_leaf_bridge = true;
1602
1603 list_for_each_entry(dev, &bus->devices, bus_list) {
1604 struct pci_bus *b = dev->subordinate;
1605 if (!b)
1606 continue;
1607
1608 is_leaf_bridge = false;
1609
1610 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
1611 continue;
1612
1613 if (rel_type == whole_subtree)
1614 pci_bus_release_bridge_resources(b, type,
1615 whole_subtree);
1616 }
1617
1618 if (pci_is_root_bus(bus))
1619 return;
1620
1621 if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
1622 return;
1623
1624 if ((rel_type == whole_subtree) || is_leaf_bridge)
1625 pci_bridge_release_resources(bus, type);
1626}
1627
Yinghai Lu76fbc262008-06-23 20:33:06 +02001628static void pci_bus_dump_res(struct pci_bus *bus)
1629{
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -07001630 struct resource *res;
1631 int i;
Yinghai Lu76fbc262008-06-23 20:33:06 +02001632
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -07001633 pci_bus_for_each_resource(bus, res, i) {
Yinghai Lu7c9342b2009-12-22 15:02:24 -08001634 if (!res || !res->end || !res->flags)
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001635 continue;
Yinghai Lu76fbc262008-06-23 20:33:06 +02001636
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001637 dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res);
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001638 }
Yinghai Lu76fbc262008-06-23 20:33:06 +02001639}
1640
1641static void pci_bus_dump_resources(struct pci_bus *bus)
1642{
1643 struct pci_bus *b;
1644 struct pci_dev *dev;
1645
1646
1647 pci_bus_dump_res(bus);
1648
1649 list_for_each_entry(dev, &bus->devices, bus_list) {
1650 b = dev->subordinate;
1651 if (!b)
1652 continue;
1653
1654 pci_bus_dump_resources(b);
1655 }
1656}
1657
Yinghai Luff351472013-07-24 15:37:13 -06001658static int pci_bus_get_depth(struct pci_bus *bus)
Yinghai Luda7822e2011-05-12 17:11:37 -07001659{
1660 int depth = 0;
Wei Yangf2a230b2013-08-02 17:31:03 +08001661 struct pci_bus *child_bus;
Yinghai Luda7822e2011-05-12 17:11:37 -07001662
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001663 list_for_each_entry(child_bus, &bus->children, node) {
Yinghai Luda7822e2011-05-12 17:11:37 -07001664 int ret;
Yinghai Luda7822e2011-05-12 17:11:37 -07001665
Wei Yangf2a230b2013-08-02 17:31:03 +08001666 ret = pci_bus_get_depth(child_bus);
Yinghai Luda7822e2011-05-12 17:11:37 -07001667 if (ret + 1 > depth)
1668 depth = ret + 1;
1669 }
1670
1671 return depth;
1672}
Yinghai Luda7822e2011-05-12 17:11:37 -07001673
Yinghai Lub55438f2012-02-23 19:23:30 -08001674/*
1675 * -1: undefined, will auto detect later
1676 * 0: disabled by user
1677 * 1: disabled by auto detect
1678 * 2: enabled by user
1679 * 3: enabled by auto detect
1680 */
1681enum enable_type {
1682 undefined = -1,
1683 user_disabled,
1684 auto_disabled,
1685 user_enabled,
1686 auto_enabled,
1687};
1688
Yinghai Luff351472013-07-24 15:37:13 -06001689static enum enable_type pci_realloc_enable = undefined;
Yinghai Lub55438f2012-02-23 19:23:30 -08001690void __init pci_realloc_get_opt(char *str)
1691{
1692 if (!strncmp(str, "off", 3))
1693 pci_realloc_enable = user_disabled;
1694 else if (!strncmp(str, "on", 2))
1695 pci_realloc_enable = user_enabled;
1696}
Yinghai Luff351472013-07-24 15:37:13 -06001697static bool pci_realloc_enabled(enum enable_type enable)
Yinghai Lub55438f2012-02-23 19:23:30 -08001698{
Yinghai Lu967260c2013-07-22 14:37:15 -07001699 return enable >= user_enabled;
Yinghai Lub55438f2012-02-23 19:23:30 -08001700}
Ram Paif483d392011-07-07 11:19:10 -07001701
Yinghai Lub07f2eb2012-02-23 19:23:32 -08001702#if defined(CONFIG_PCI_IOV) && defined(CONFIG_PCI_REALLOC_ENABLE_AUTO)
Yinghai Luff351472013-07-24 15:37:13 -06001703static int iov_resources_unassigned(struct pci_dev *dev, void *data)
Yinghai Lu223d96f2013-07-22 14:37:13 -07001704{
1705 int i;
1706 bool *unassigned = data;
Yinghai Lub07f2eb2012-02-23 19:23:32 -08001707
Yinghai Lu223d96f2013-07-22 14:37:13 -07001708 for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++) {
1709 struct resource *r = &dev->resource[i];
Yinghai Lufa216bf2013-07-22 14:37:14 -07001710 struct pci_bus_region region;
Yinghai Lub07f2eb2012-02-23 19:23:32 -08001711
Yinghai Lu223d96f2013-07-22 14:37:13 -07001712 /* Not assigned or rejected by kernel? */
Yinghai Lufa216bf2013-07-22 14:37:14 -07001713 if (!r->flags)
1714 continue;
Yinghai Lub07f2eb2012-02-23 19:23:32 -08001715
Yinghai Lufc279852013-12-09 22:54:40 -08001716 pcibios_resource_to_bus(dev->bus, &region, r);
Yinghai Lufa216bf2013-07-22 14:37:14 -07001717 if (!region.start) {
Yinghai Lu223d96f2013-07-22 14:37:13 -07001718 *unassigned = true;
1719 return 1; /* return early from pci_walk_bus() */
Yinghai Lub07f2eb2012-02-23 19:23:32 -08001720 }
1721 }
Yinghai Lu223d96f2013-07-22 14:37:13 -07001722
1723 return 0;
Yinghai Lub07f2eb2012-02-23 19:23:32 -08001724}
1725
Yinghai Luff351472013-07-24 15:37:13 -06001726static enum enable_type pci_realloc_detect(struct pci_bus *bus,
Yinghai Lu967260c2013-07-22 14:37:15 -07001727 enum enable_type enable_local)
Yinghai Lu223d96f2013-07-22 14:37:13 -07001728{
1729 bool unassigned = false;
Yinghai Luda7822e2011-05-12 17:11:37 -07001730
Yinghai Lu967260c2013-07-22 14:37:15 -07001731 if (enable_local != undefined)
1732 return enable_local;
Yinghai Luda7822e2011-05-12 17:11:37 -07001733
Yinghai Lu967260c2013-07-22 14:37:15 -07001734 pci_walk_bus(bus, iov_resources_unassigned, &unassigned);
1735 if (unassigned)
1736 return auto_enabled;
1737
1738 return enable_local;
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001739}
Yinghai Lu223d96f2013-07-22 14:37:13 -07001740#else
Yinghai Luff351472013-07-24 15:37:13 -06001741static enum enable_type pci_realloc_detect(struct pci_bus *bus,
Yinghai Lu967260c2013-07-22 14:37:15 -07001742 enum enable_type enable_local)
1743{
1744 return enable_local;
1745}
Yinghai Lu223d96f2013-07-22 14:37:13 -07001746#endif
Yinghai Luda7822e2011-05-12 17:11:37 -07001747
1748/*
1749 * first try will not touch pci bridge res
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001750 * second and later try will clear small leaf bridge res
1751 * will stop till to the max depth if can not find good one
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 */
Yinghai Lu39772032013-07-22 14:37:18 -07001753void pci_assign_unassigned_root_bus_resources(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754{
Ram Paic8adf9a2011-02-14 17:43:20 -08001755 LIST_HEAD(realloc_head); /* list of resources that
Yinghai Luda7822e2011-05-12 17:11:37 -07001756 want additional resources */
1757 struct list_head *add_list = NULL;
1758 int tried_times = 0;
1759 enum release_type rel_type = leaf_only;
1760 LIST_HEAD(fail_head);
1761 struct pci_dev_resource *fail_res;
Yinghai Luda7822e2011-05-12 17:11:37 -07001762 int pci_try_num = 1;
Yinghai Lu55ed83a2013-07-22 14:37:16 -07001763 enum enable_type enable_local;
Yinghai Luda7822e2011-05-12 17:11:37 -07001764
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001765 /* don't realloc if asked to do so */
Yinghai Lu55ed83a2013-07-22 14:37:16 -07001766 enable_local = pci_realloc_detect(bus, pci_realloc_enable);
Yinghai Lu967260c2013-07-22 14:37:15 -07001767 if (pci_realloc_enabled(enable_local)) {
Yinghai Lu55ed83a2013-07-22 14:37:16 -07001768 int max_depth = pci_bus_get_depth(bus);
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001769
1770 pci_try_num = max_depth + 1;
Yinghai Lu55ed83a2013-07-22 14:37:16 -07001771 dev_printk(KERN_DEBUG, &bus->dev,
1772 "max bus depth: %d pci_try_num: %d\n",
1773 max_depth, pci_try_num);
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001774 }
Yinghai Luda7822e2011-05-12 17:11:37 -07001775
1776again:
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001777 /*
1778 * last try will use add_list, otherwise will try good to have as
1779 * must have, so can realloc parent bridge resource
1780 */
1781 if (tried_times + 1 == pci_try_num)
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001782 add_list = &realloc_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 /* Depth first, calculate sizes and alignments of all
1784 subordinate buses. */
Yinghai Lu55ed83a2013-07-22 14:37:16 -07001785 __pci_bus_size_bridges(bus, add_list);
Ram Paic8adf9a2011-02-14 17:43:20 -08001786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 /* Depth last, allocate resources and update the hardware. */
Yinghai Lu55ed83a2013-07-22 14:37:16 -07001788 __pci_bus_assign_resources(bus, add_list, &fail_head);
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001789 if (add_list)
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001790 BUG_ON(!list_empty(add_list));
Yinghai Luda7822e2011-05-12 17:11:37 -07001791 tried_times++;
1792
1793 /* any device complain? */
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001794 if (list_empty(&fail_head))
Yinghai Lu928bea92013-07-22 14:37:17 -07001795 goto dump;
Ram Paif483d392011-07-07 11:19:10 -07001796
Yinghai Lu0c5be0c2012-02-23 19:23:29 -08001797 if (tried_times >= pci_try_num) {
Yinghai Lu967260c2013-07-22 14:37:15 -07001798 if (enable_local == undefined)
Yinghai Lu55ed83a2013-07-22 14:37:16 -07001799 dev_info(&bus->dev, "Some PCI device resources are unassigned, try booting with pci=realloc\n");
Yinghai Lu967260c2013-07-22 14:37:15 -07001800 else if (enable_local == auto_enabled)
Yinghai Lu55ed83a2013-07-22 14:37:16 -07001801 dev_info(&bus->dev, "Automatically enabled pci realloc, if you have problem, try booting with pci=realloc=off\n");
Yinghai Lueb572e72012-02-23 19:23:31 -08001802
Yinghai Lubffc56d2012-01-21 02:08:30 -08001803 free_list(&fail_head);
Yinghai Lu928bea92013-07-22 14:37:17 -07001804 goto dump;
Yinghai Luda7822e2011-05-12 17:11:37 -07001805 }
1806
Yinghai Lu55ed83a2013-07-22 14:37:16 -07001807 dev_printk(KERN_DEBUG, &bus->dev,
1808 "No. %d try to assign unassigned res\n", tried_times + 1);
Yinghai Luda7822e2011-05-12 17:11:37 -07001809
1810 /* third times and later will not check if it is leaf */
1811 if ((tried_times + 1) > 2)
1812 rel_type = whole_subtree;
1813
1814 /*
1815 * Try to release leaf bridge's resources that doesn't fit resource of
1816 * child device under that bridge
1817 */
Yinghai Lu61e83cd2013-07-22 14:37:12 -07001818 list_for_each_entry(fail_res, &fail_head, list)
1819 pci_bus_release_bridge_resources(fail_res->dev->bus,
Christian Königcb21bc92017-10-18 15:58:17 +02001820 fail_res->flags & PCI_RES_TYPE_MASK,
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001821 rel_type);
Yinghai Lu61e83cd2013-07-22 14:37:12 -07001822
Yinghai Luda7822e2011-05-12 17:11:37 -07001823 /* restore size and flags */
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001824 list_for_each_entry(fail_res, &fail_head, list) {
1825 struct resource *res = fail_res->res;
Yinghai Luda7822e2011-05-12 17:11:37 -07001826
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001827 res->start = fail_res->start;
1828 res->end = fail_res->end;
1829 res->flags = fail_res->flags;
1830 if (fail_res->dev->subordinate)
Yinghai Luda7822e2011-05-12 17:11:37 -07001831 res->flags = 0;
Yinghai Luda7822e2011-05-12 17:11:37 -07001832 }
Yinghai Lubffc56d2012-01-21 02:08:30 -08001833 free_list(&fail_head);
Yinghai Luda7822e2011-05-12 17:11:37 -07001834
1835 goto again;
1836
Yinghai Lu928bea92013-07-22 14:37:17 -07001837dump:
Yinghai Lu76fbc262008-06-23 20:33:06 +02001838 /* dump the resource on buses */
Yinghai Lu55ed83a2013-07-22 14:37:16 -07001839 pci_bus_dump_resources(bus);
1840}
1841
1842void __init pci_assign_unassigned_resources(void)
1843{
1844 struct pci_bus *root_bus;
1845
Rui Wang584c5c42016-08-17 16:00:34 +08001846 list_for_each_entry(root_bus, &pci_root_buses, node) {
Yinghai Lu55ed83a2013-07-22 14:37:16 -07001847 pci_assign_unassigned_root_bus_resources(root_bus);
Rui Wangd9c149d2016-09-10 23:40:45 +08001848
1849 /* Make sure the root bridge has a companion ACPI device: */
1850 if (ACPI_HANDLE(root_bus->bridge))
1851 acpi_ioapic_add(ACPI_HANDLE(root_bus->bridge));
Rui Wang584c5c42016-08-17 16:00:34 +08001852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853}
Yinghai Lu6841ec62010-01-22 01:02:25 -08001854
Mika Westerberg1a576772017-10-13 21:35:45 +03001855static void extend_bridge_window(struct pci_dev *bridge, struct resource *res,
1856 struct list_head *add_list, resource_size_t available)
1857{
1858 struct pci_dev_resource *dev_res;
1859
1860 if (res->parent)
1861 return;
1862
1863 if (resource_size(res) >= available)
1864 return;
1865
1866 dev_res = res_to_dev_res(add_list, res);
1867 if (!dev_res)
1868 return;
1869
1870 /* Is there room to extend the window? */
1871 if (available - resource_size(res) <= dev_res->add_size)
1872 return;
1873
1874 dev_res->add_size = available - resource_size(res);
1875 dev_dbg(&bridge->dev, "bridge window %pR extended by %pa\n", res,
1876 &dev_res->add_size);
1877}
1878
1879static void pci_bus_distribute_available_resources(struct pci_bus *bus,
1880 struct list_head *add_list, resource_size_t available_io,
1881 resource_size_t available_mmio, resource_size_t available_mmio_pref)
1882{
1883 resource_size_t remaining_io, remaining_mmio, remaining_mmio_pref;
1884 unsigned int normal_bridges = 0, hotplug_bridges = 0;
1885 struct resource *io_res, *mmio_res, *mmio_pref_res;
1886 struct pci_dev *dev, *bridge = bus->self;
1887
1888 io_res = &bridge->resource[PCI_BRIDGE_RESOURCES + 0];
1889 mmio_res = &bridge->resource[PCI_BRIDGE_RESOURCES + 1];
1890 mmio_pref_res = &bridge->resource[PCI_BRIDGE_RESOURCES + 2];
1891
1892 /*
1893 * Update additional resource list (add_list) to fill all the
1894 * extra resource space available for this port except the space
1895 * calculated in __pci_bus_size_bridges() which covers all the
1896 * devices currently connected to the port and below.
1897 */
1898 extend_bridge_window(bridge, io_res, add_list, available_io);
1899 extend_bridge_window(bridge, mmio_res, add_list, available_mmio);
1900 extend_bridge_window(bridge, mmio_pref_res, add_list,
1901 available_mmio_pref);
1902
1903 /*
1904 * Calculate the total amount of extra resource space we can
1905 * pass to bridges below this one. This is basically the
1906 * extra space reduced by the minimal required space for the
1907 * non-hotplug bridges.
1908 */
1909 remaining_io = available_io;
1910 remaining_mmio = available_mmio;
1911 remaining_mmio_pref = available_mmio_pref;
1912
1913 /*
1914 * Calculate how many hotplug bridges and normal bridges there
1915 * are on this bus. We will distribute the additional available
1916 * resources between hotplug bridges.
1917 */
1918 for_each_pci_bridge(dev, bus) {
1919 if (dev->is_hotplug_bridge)
1920 hotplug_bridges++;
1921 else
1922 normal_bridges++;
1923 }
1924
1925 for_each_pci_bridge(dev, bus) {
1926 const struct resource *res;
1927
1928 if (dev->is_hotplug_bridge)
1929 continue;
1930
1931 /*
1932 * Reduce the available resource space by what the
1933 * bridge and devices below it occupy.
1934 */
1935 res = &dev->resource[PCI_BRIDGE_RESOURCES + 0];
1936 if (!res->parent && available_io > resource_size(res))
1937 remaining_io -= resource_size(res);
1938
1939 res = &dev->resource[PCI_BRIDGE_RESOURCES + 1];
1940 if (!res->parent && available_mmio > resource_size(res))
1941 remaining_mmio -= resource_size(res);
1942
1943 res = &dev->resource[PCI_BRIDGE_RESOURCES + 2];
1944 if (!res->parent && available_mmio_pref > resource_size(res))
1945 remaining_mmio_pref -= resource_size(res);
1946 }
1947
1948 /*
1949 * Go over devices on this bus and distribute the remaining
1950 * resource space between hotplug bridges.
1951 */
1952 for_each_pci_bridge(dev, bus) {
1953 struct pci_bus *b;
1954
1955 b = dev->subordinate;
1956 if (!b)
1957 continue;
1958
1959 if (!hotplug_bridges && normal_bridges == 1) {
1960 /*
1961 * There is only one bridge on the bus (upstream
1962 * port) so it gets all available resources
1963 * which it can then distribute to the possible
1964 * hotplug bridges below.
1965 */
1966 pci_bus_distribute_available_resources(b, add_list,
1967 available_io, available_mmio,
1968 available_mmio_pref);
1969 } else if (dev->is_hotplug_bridge) {
1970 resource_size_t align, io, mmio, mmio_pref;
1971
1972 /*
1973 * Distribute available extra resources equally
1974 * between hotplug-capable downstream ports
1975 * taking alignment into account.
1976 *
1977 * Here hotplug_bridges is always != 0.
1978 */
1979 align = pci_resource_alignment(bridge, io_res);
1980 io = div64_ul(available_io, hotplug_bridges);
1981 io = min(ALIGN(io, align), remaining_io);
1982 remaining_io -= io;
1983
1984 align = pci_resource_alignment(bridge, mmio_res);
1985 mmio = div64_ul(available_mmio, hotplug_bridges);
1986 mmio = min(ALIGN(mmio, align), remaining_mmio);
1987 remaining_mmio -= mmio;
1988
1989 align = pci_resource_alignment(bridge, mmio_pref_res);
1990 mmio_pref = div64_ul(available_mmio_pref,
1991 hotplug_bridges);
1992 mmio_pref = min(ALIGN(mmio_pref, align),
1993 remaining_mmio_pref);
1994 remaining_mmio_pref -= mmio_pref;
1995
1996 pci_bus_distribute_available_resources(b, add_list, io,
1997 mmio, mmio_pref);
1998 }
1999 }
2000}
2001
2002static void
2003pci_bridge_distribute_available_resources(struct pci_dev *bridge,
2004 struct list_head *add_list)
2005{
2006 resource_size_t available_io, available_mmio, available_mmio_pref;
2007 const struct resource *res;
2008
2009 if (!bridge->is_hotplug_bridge)
2010 return;
2011
2012 /* Take the initial extra resources from the hotplug port */
2013 res = &bridge->resource[PCI_BRIDGE_RESOURCES + 0];
2014 available_io = resource_size(res);
2015 res = &bridge->resource[PCI_BRIDGE_RESOURCES + 1];
2016 available_mmio = resource_size(res);
2017 res = &bridge->resource[PCI_BRIDGE_RESOURCES + 2];
2018 available_mmio_pref = resource_size(res);
2019
2020 pci_bus_distribute_available_resources(bridge->subordinate,
2021 add_list, available_io, available_mmio, available_mmio_pref);
2022}
2023
Yinghai Lu6841ec62010-01-22 01:02:25 -08002024void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
2025{
2026 struct pci_bus *parent = bridge->subordinate;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08002027 LIST_HEAD(add_list); /* list of resources that
Yinghai Lu8424d752012-01-21 02:08:21 -08002028 want additional resources */
Yinghai Lu32180e42010-01-22 01:02:27 -08002029 int tried_times = 0;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08002030 LIST_HEAD(fail_head);
Yinghai Lub9b0bba2012-01-21 02:08:29 -08002031 struct pci_dev_resource *fail_res;
Yinghai Lu6841ec62010-01-22 01:02:25 -08002032 int retval;
2033
Yinghai Lu32180e42010-01-22 01:02:27 -08002034again:
Yinghai Lu8424d752012-01-21 02:08:21 -08002035 __pci_bus_size_bridges(parent, &add_list);
Mika Westerberg1a576772017-10-13 21:35:45 +03002036
2037 /*
2038 * Distribute remaining resources (if any) equally between
2039 * hotplug bridges below. This makes it possible to extend the
2040 * hierarchy later without running out of resources.
2041 */
2042 pci_bridge_distribute_available_resources(bridge, &add_list);
2043
Yinghai Lubdc4abe2012-01-21 02:08:27 -08002044 __pci_bridge_assign_resources(bridge, &add_list, &fail_head);
2045 BUG_ON(!list_empty(&add_list));
Yinghai Lu32180e42010-01-22 01:02:27 -08002046 tried_times++;
2047
Yinghai Lubdc4abe2012-01-21 02:08:27 -08002048 if (list_empty(&fail_head))
Yinghai Lu3f579c32010-05-21 14:35:06 -07002049 goto enable_all;
Yinghai Lu32180e42010-01-22 01:02:27 -08002050
2051 if (tried_times >= 2) {
2052 /* still fail, don't need to try more */
Yinghai Lubffc56d2012-01-21 02:08:30 -08002053 free_list(&fail_head);
Yinghai Lu3f579c32010-05-21 14:35:06 -07002054 goto enable_all;
Yinghai Lu32180e42010-01-22 01:02:27 -08002055 }
2056
2057 printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
2058 tried_times + 1);
2059
2060 /*
2061 * Try to release leaf bridge's resources that doesn't fit resource of
2062 * child device under that bridge
2063 */
Yinghai Lu61e83cd2013-07-22 14:37:12 -07002064 list_for_each_entry(fail_res, &fail_head, list)
2065 pci_bus_release_bridge_resources(fail_res->dev->bus,
Christian Königcb21bc92017-10-18 15:58:17 +02002066 fail_res->flags & PCI_RES_TYPE_MASK,
Yinghai Lu32180e42010-01-22 01:02:27 -08002067 whole_subtree);
Yinghai Lu61e83cd2013-07-22 14:37:12 -07002068
Yinghai Lu32180e42010-01-22 01:02:27 -08002069 /* restore size and flags */
Yinghai Lub9b0bba2012-01-21 02:08:29 -08002070 list_for_each_entry(fail_res, &fail_head, list) {
2071 struct resource *res = fail_res->res;
Yinghai Lu32180e42010-01-22 01:02:27 -08002072
Yinghai Lub9b0bba2012-01-21 02:08:29 -08002073 res->start = fail_res->start;
2074 res->end = fail_res->end;
2075 res->flags = fail_res->flags;
2076 if (fail_res->dev->subordinate)
Yinghai Lu32180e42010-01-22 01:02:27 -08002077 res->flags = 0;
Yinghai Lu32180e42010-01-22 01:02:27 -08002078 }
Yinghai Lubffc56d2012-01-21 02:08:30 -08002079 free_list(&fail_head);
Yinghai Lu32180e42010-01-22 01:02:27 -08002080
2081 goto again;
Yinghai Lu3f579c32010-05-21 14:35:06 -07002082
2083enable_all:
2084 retval = pci_reenable_device(bridge);
Bjorn Helgaas9fc9eea2013-04-12 11:35:40 -06002085 if (retval)
2086 dev_err(&bridge->dev, "Error reenabling bridge (%d)\n", retval);
Yinghai Lu3f579c32010-05-21 14:35:06 -07002087 pci_set_master(bridge);
Yinghai Lu6841ec62010-01-22 01:02:25 -08002088}
2089EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
Yinghai Lu9b030882012-01-21 02:08:23 -08002090
Christian König8bb705e2017-10-24 14:40:26 -05002091int pci_reassign_bridge_resources(struct pci_dev *bridge, unsigned long type)
2092{
2093 struct pci_dev_resource *dev_res;
2094 struct pci_dev *next;
2095 LIST_HEAD(saved);
2096 LIST_HEAD(added);
2097 LIST_HEAD(failed);
2098 unsigned int i;
2099 int ret;
2100
2101 /* Walk to the root hub, releasing bridge BARs when possible */
2102 next = bridge;
2103 do {
2104 bridge = next;
2105 for (i = PCI_BRIDGE_RESOURCES; i < PCI_BRIDGE_RESOURCE_END;
2106 i++) {
2107 struct resource *res = &bridge->resource[i];
2108
2109 if ((res->flags ^ type) & PCI_RES_TYPE_MASK)
2110 continue;
2111
2112 /* Ignore BARs which are still in use */
2113 if (res->child)
2114 continue;
2115
2116 ret = add_to_list(&saved, bridge, res, 0, 0);
2117 if (ret)
2118 goto cleanup;
2119
2120 dev_info(&bridge->dev, "BAR %d: releasing %pR\n",
2121 i, res);
2122
2123 if (res->parent)
2124 release_resource(res);
2125 res->start = 0;
2126 res->end = 0;
2127 break;
2128 }
2129 if (i == PCI_BRIDGE_RESOURCE_END)
2130 break;
2131
2132 next = bridge->bus ? bridge->bus->self : NULL;
2133 } while (next);
2134
2135 if (list_empty(&saved))
2136 return -ENOENT;
2137
2138 __pci_bus_size_bridges(bridge->subordinate, &added);
2139 __pci_bridge_assign_resources(bridge, &added, &failed);
2140 BUG_ON(!list_empty(&added));
2141
2142 if (!list_empty(&failed)) {
2143 ret = -ENOSPC;
2144 goto cleanup;
2145 }
2146
2147 list_for_each_entry(dev_res, &saved, list) {
2148 /* Skip the bridge we just assigned resources for. */
2149 if (bridge == dev_res->dev)
2150 continue;
2151
2152 bridge = dev_res->dev;
2153 pci_setup_bridge(bridge->subordinate);
2154 }
2155
2156 free_list(&saved);
2157 return 0;
2158
2159cleanup:
2160 /* restore size and flags */
2161 list_for_each_entry(dev_res, &failed, list) {
2162 struct resource *res = dev_res->res;
2163
2164 res->start = dev_res->start;
2165 res->end = dev_res->end;
2166 res->flags = dev_res->flags;
2167 }
2168 free_list(&failed);
2169
2170 /* Revert to the old configuration */
2171 list_for_each_entry(dev_res, &saved, list) {
2172 struct resource *res = dev_res->res;
2173
2174 bridge = dev_res->dev;
2175 i = res - bridge->resource;
2176
2177 res->start = dev_res->start;
2178 res->end = dev_res->end;
2179 res->flags = dev_res->flags;
2180
2181 pci_claim_resource(bridge, i);
2182 pci_setup_bridge(bridge->subordinate);
2183 }
2184 free_list(&saved);
2185
2186 return ret;
2187}
2188
Yinghai Lu17787942012-10-30 14:31:10 -06002189void pci_assign_unassigned_bus_resources(struct pci_bus *bus)
Yinghai Lu9b030882012-01-21 02:08:23 -08002190{
Yinghai Lu9b030882012-01-21 02:08:23 -08002191 struct pci_dev *dev;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08002192 LIST_HEAD(add_list); /* list of resources that
Yinghai Lu9b030882012-01-21 02:08:23 -08002193 want additional resources */
2194
Yinghai Lu9b030882012-01-21 02:08:23 -08002195 down_read(&pci_bus_sem);
Andy Shevchenko24a0c652017-10-20 15:38:54 -05002196 for_each_pci_bridge(dev, bus)
2197 if (pci_has_subordinate(dev))
2198 __pci_bus_size_bridges(dev->subordinate, &add_list);
Yinghai Lu9b030882012-01-21 02:08:23 -08002199 up_read(&pci_bus_sem);
2200 __pci_bus_assign_resources(bus, &add_list, NULL);
Yinghai Lubdc4abe2012-01-21 02:08:27 -08002201 BUG_ON(!list_empty(&add_list));
Yinghai Lu17787942012-10-30 14:31:10 -06002202}
Ray Juie6b29de2015-04-08 11:21:33 -07002203EXPORT_SYMBOL_GPL(pci_assign_unassigned_bus_resources);