blob: 0a26648f1712ee2095c4271e70c72995a1c8530c [file] [log] [blame]
Bjorn Helgaas7328c8f2018-01-26 11:45:16 -06001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * drivers/pci/setup-bus.c
4 *
5 * Extruded from code written by
6 * Dave Rusling (david.rusling@reo.mts.dec.com)
7 * David Mosberger (davidm@cs.arizona.edu)
8 * David Miller (davem@redhat.com)
9 *
10 * Support routines for initializing a PCI subsystem.
11 */
12
13/*
14 * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
15 * PCI-PCI bridges cleanup, sorted resource allocation.
16 * Feb 2002, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
17 * Converted to allocation in 3 passes, which gives
18 * tighter packing. Prefetchable range support.
19 */
20
21#include <linux/init.h>
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/pci.h>
25#include <linux/errno.h>
26#include <linux/ioport.h>
27#include <linux/cache.h>
28#include <linux/slab.h>
Rui Wang584c5c42016-08-17 16:00:34 +080029#include <linux/acpi.h>
Chris Wright6faf17f2009-08-28 13:00:06 -070030#include "pci.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Bjorn Helgaas844393f2012-02-23 20:18:59 -070032unsigned int pci_flags;
Bjorn Helgaas47087702012-02-23 14:29:23 -070033
Yinghai Lubdc4abe2012-01-21 02:08:27 -080034struct pci_dev_resource {
35 struct list_head list;
Yinghai Lu2934a0d2012-01-21 02:08:26 -080036 struct resource *res;
37 struct pci_dev *dev;
Yinghai Lu568ddef2010-01-22 01:02:21 -080038 resource_size_t start;
39 resource_size_t end;
Ram Paic8adf9a2011-02-14 17:43:20 -080040 resource_size_t add_size;
Ram Pai2bbc6942011-07-25 13:08:39 -070041 resource_size_t min_align;
Yinghai Lu568ddef2010-01-22 01:02:21 -080042 unsigned long flags;
43};
44
Yinghai Lubffc56d2012-01-21 02:08:30 -080045static void free_list(struct list_head *head)
46{
47 struct pci_dev_resource *dev_res, *tmp;
48
49 list_for_each_entry_safe(dev_res, tmp, head, list) {
50 list_del(&dev_res->list);
51 kfree(dev_res);
52 }
53}
Ram Pai094732a2011-02-14 17:43:18 -080054
Ram Paic8adf9a2011-02-14 17:43:20 -080055/**
56 * add_to_list() - add a new resource tracker to the list
57 * @head: Head of the list
58 * @dev: device corresponding to which the resource
59 * belongs
60 * @res: The resource to be tracked
61 * @add_size: additional size to be optionally added
62 * to the resource
63 */
Yinghai Lubdc4abe2012-01-21 02:08:27 -080064static int add_to_list(struct list_head *head,
Ram Paic8adf9a2011-02-14 17:43:20 -080065 struct pci_dev *dev, struct resource *res,
Ram Pai2bbc6942011-07-25 13:08:39 -070066 resource_size_t add_size, resource_size_t min_align)
Yinghai Lu568ddef2010-01-22 01:02:21 -080067{
Yinghai Lu764242a2012-01-21 02:08:28 -080068 struct pci_dev_resource *tmp;
Yinghai Lu568ddef2010-01-22 01:02:21 -080069
Yinghai Lubdc4abe2012-01-21 02:08:27 -080070 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
Yinghai Lu568ddef2010-01-22 01:02:21 -080071 if (!tmp) {
Ryan Desfosses3c78bc62014-04-18 20:13:49 -040072 pr_warn("add_to_list: kmalloc() failed!\n");
Yinghai Luef62dfe2012-01-21 02:08:18 -080073 return -ENOMEM;
Yinghai Lu568ddef2010-01-22 01:02:21 -080074 }
75
Yinghai Lu568ddef2010-01-22 01:02:21 -080076 tmp->res = res;
77 tmp->dev = dev;
78 tmp->start = res->start;
79 tmp->end = res->end;
80 tmp->flags = res->flags;
Ram Paic8adf9a2011-02-14 17:43:20 -080081 tmp->add_size = add_size;
Ram Pai2bbc6942011-07-25 13:08:39 -070082 tmp->min_align = min_align;
Yinghai Lubdc4abe2012-01-21 02:08:27 -080083
84 list_add(&tmp->list, head);
Yinghai Luef62dfe2012-01-21 02:08:18 -080085
86 return 0;
Yinghai Lu568ddef2010-01-22 01:02:21 -080087}
88
Yinghai Lub9b0bba2012-01-21 02:08:29 -080089static void remove_from_list(struct list_head *head,
Yinghai Lu3e6e0d82012-01-21 02:08:20 -080090 struct resource *res)
91{
Yinghai Lub9b0bba2012-01-21 02:08:29 -080092 struct pci_dev_resource *dev_res, *tmp;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -080093
Yinghai Lub9b0bba2012-01-21 02:08:29 -080094 list_for_each_entry_safe(dev_res, tmp, head, list) {
95 if (dev_res->res == res) {
96 list_del(&dev_res->list);
97 kfree(dev_res);
Yinghai Lubdc4abe2012-01-21 02:08:27 -080098 break;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -080099 }
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800100 }
101}
102
Wei Yangd74b9022015-03-25 16:23:51 +0800103static struct pci_dev_resource *res_to_dev_res(struct list_head *head,
104 struct resource *res)
Yinghai Lu1c372352012-01-21 02:08:19 -0800105{
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800106 struct pci_dev_resource *dev_res;
Yinghai Lu1c372352012-01-21 02:08:19 -0800107
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800108 list_for_each_entry(dev_res, head, list) {
Bjorn Helgaas25e77382016-12-29 11:27:52 -0600109 if (dev_res->res == res)
Wei Yangd74b9022015-03-25 16:23:51 +0800110 return dev_res;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800111 }
Yinghai Lu1c372352012-01-21 02:08:19 -0800112
Wei Yangd74b9022015-03-25 16:23:51 +0800113 return NULL;
Yinghai Lu1c372352012-01-21 02:08:19 -0800114}
115
Wei Yangd74b9022015-03-25 16:23:51 +0800116static resource_size_t get_res_add_size(struct list_head *head,
117 struct resource *res)
118{
119 struct pci_dev_resource *dev_res;
120
121 dev_res = res_to_dev_res(head, res);
122 return dev_res ? dev_res->add_size : 0;
123}
124
125static resource_size_t get_res_add_align(struct list_head *head,
126 struct resource *res)
127{
128 struct pci_dev_resource *dev_res;
129
130 dev_res = res_to_dev_res(head, res);
131 return dev_res ? dev_res->min_align : 0;
132}
133
134
Yinghai Lu78c3b322012-01-21 02:08:25 -0800135/* Sort resources by alignment */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800136static void pdev_sort_resources(struct pci_dev *dev, struct list_head *head)
Yinghai Lu78c3b322012-01-21 02:08:25 -0800137{
138 int i;
139
140 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
141 struct resource *r;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800142 struct pci_dev_resource *dev_res, *tmp;
Yinghai Lu78c3b322012-01-21 02:08:25 -0800143 resource_size_t r_align;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800144 struct list_head *n;
Yinghai Lu78c3b322012-01-21 02:08:25 -0800145
146 r = &dev->resource[i];
147
148 if (r->flags & IORESOURCE_PCI_FIXED)
149 continue;
150
151 if (!(r->flags) || r->parent)
152 continue;
153
154 r_align = pci_resource_alignment(dev, r);
155 if (!r_align) {
156 dev_warn(&dev->dev, "BAR %d: %pR has bogus alignment\n",
157 i, r);
158 continue;
159 }
Yinghai Lu78c3b322012-01-21 02:08:25 -0800160
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800161 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
162 if (!tmp)
Ryan Desfosses227f0642014-04-18 20:13:50 -0400163 panic("pdev_sort_resources(): kmalloc() failed!\n");
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800164 tmp->res = r;
165 tmp->dev = dev;
166
167 /* fallback is smallest one or list is empty*/
168 n = head;
169 list_for_each_entry(dev_res, head, list) {
170 resource_size_t align;
171
172 align = pci_resource_alignment(dev_res->dev,
173 dev_res->res);
Yinghai Lu78c3b322012-01-21 02:08:25 -0800174
175 if (r_align > align) {
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800176 n = &dev_res->list;
Yinghai Lu78c3b322012-01-21 02:08:25 -0800177 break;
178 }
179 }
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800180 /* Insert it just before n*/
181 list_add_tail(&tmp->list, n);
Yinghai Lu78c3b322012-01-21 02:08:25 -0800182 }
183}
184
Yinghai Lu6841ec62010-01-22 01:02:25 -0800185static void __dev_sort_resources(struct pci_dev *dev,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800186 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Yinghai Lu6841ec62010-01-22 01:02:25 -0800188 u16 class = dev->class >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Yinghai Lu6841ec62010-01-22 01:02:25 -0800190 /* Don't touch classless devices or host bridges or ioapics. */
191 if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST)
192 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Yinghai Lu6841ec62010-01-22 01:02:25 -0800194 /* Don't touch ioapic devices already enabled by firmware */
195 if (class == PCI_CLASS_SYSTEM_PIC) {
196 u16 command;
197 pci_read_config_word(dev, PCI_COMMAND, &command);
198 if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
199 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
201
Yinghai Lu6841ec62010-01-22 01:02:25 -0800202 pdev_sort_resources(dev, head);
203}
204
Ram Paifc075e12011-02-14 17:43:19 -0800205static inline void reset_resource(struct resource *res)
206{
207 res->start = 0;
208 res->end = 0;
209 res->flags = 0;
210}
211
Ram Paic8adf9a2011-02-14 17:43:20 -0800212/**
Ram Pai9e8bf932011-07-25 13:08:42 -0700213 * reassign_resources_sorted() - satisfy any additional resource requests
Ram Paic8adf9a2011-02-14 17:43:20 -0800214 *
Ram Pai9e8bf932011-07-25 13:08:42 -0700215 * @realloc_head : head of the list tracking requests requiring additional
Ram Paic8adf9a2011-02-14 17:43:20 -0800216 * resources
217 * @head : head of the list tracking requests with allocated
218 * resources
219 *
Ram Pai9e8bf932011-07-25 13:08:42 -0700220 * Walk through each element of the realloc_head and try to procure
Ram Paic8adf9a2011-02-14 17:43:20 -0800221 * additional resources for the element, provided the element
222 * is in the head list.
223 */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800224static void reassign_resources_sorted(struct list_head *realloc_head,
225 struct list_head *head)
Ram Paic8adf9a2011-02-14 17:43:20 -0800226{
227 struct resource *res;
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800228 struct pci_dev_resource *add_res, *tmp;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800229 struct pci_dev_resource *dev_res;
Wei Yangd74b9022015-03-25 16:23:51 +0800230 resource_size_t add_size, align;
Ram Paic8adf9a2011-02-14 17:43:20 -0800231 int idx;
232
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800233 list_for_each_entry_safe(add_res, tmp, realloc_head, list) {
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800234 bool found_match = false;
235
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800236 res = add_res->res;
Ram Paic8adf9a2011-02-14 17:43:20 -0800237 /* skip resource that has been reset */
238 if (!res->flags)
239 goto out;
240
241 /* skip this resource if not found in head list */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800242 list_for_each_entry(dev_res, head, list) {
243 if (dev_res->res == res) {
244 found_match = true;
245 break;
246 }
Ram Paic8adf9a2011-02-14 17:43:20 -0800247 }
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800248 if (!found_match)/* just skip */
249 continue;
Ram Paic8adf9a2011-02-14 17:43:20 -0800250
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800251 idx = res - &add_res->dev->resource[0];
252 add_size = add_res->add_size;
Wei Yangd74b9022015-03-25 16:23:51 +0800253 align = add_res->min_align;
Ram Pai2bbc6942011-07-25 13:08:39 -0700254 if (!resource_size(res)) {
Wei Yangd74b9022015-03-25 16:23:51 +0800255 res->start = align;
Ram Pai2bbc6942011-07-25 13:08:39 -0700256 res->end = res->start + add_size - 1;
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800257 if (pci_assign_resource(add_res->dev, idx))
Ram Paic8adf9a2011-02-14 17:43:20 -0800258 reset_resource(res);
Ram Pai2bbc6942011-07-25 13:08:39 -0700259 } else {
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800260 res->flags |= add_res->flags &
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800261 (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN);
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800262 if (pci_reassign_resource(add_res->dev, idx,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800263 add_size, align))
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800264 dev_printk(KERN_DEBUG, &add_res->dev->dev,
Yinghai Lub5924432012-01-21 02:08:31 -0800265 "failed to add %llx res[%d]=%pR\n",
266 (unsigned long long)add_size,
267 idx, res);
Ram Paic8adf9a2011-02-14 17:43:20 -0800268 }
269out:
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800270 list_del(&add_res->list);
271 kfree(add_res);
Ram Paic8adf9a2011-02-14 17:43:20 -0800272 }
273}
274
275/**
276 * assign_requested_resources_sorted() - satisfy resource requests
277 *
278 * @head : head of the list tracking requests for resources
Wanpeng Li8356aad2012-06-15 21:15:49 +0800279 * @fail_head : head of the list tracking requests that could
Ram Paic8adf9a2011-02-14 17:43:20 -0800280 * not be allocated
281 *
282 * Satisfy resource requests of each element in the list. Add
283 * requests that could not satisfied to the failed_list.
284 */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800285static void assign_requested_resources_sorted(struct list_head *head,
286 struct list_head *fail_head)
Yinghai Lu6841ec62010-01-22 01:02:25 -0800287{
288 struct resource *res;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800289 struct pci_dev_resource *dev_res;
Yinghai Lu6841ec62010-01-22 01:02:25 -0800290 int idx;
291
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800292 list_for_each_entry(dev_res, head, list) {
293 res = dev_res->res;
294 idx = res - &dev_res->dev->resource[0];
295 if (resource_size(res) &&
296 pci_assign_resource(dev_res->dev, idx)) {
Yinghai Lua3cb9992013-01-21 13:20:43 -0800297 if (fail_head) {
Yinghai Lu9a928662010-02-28 15:49:39 -0800298 /*
299 * if the failed res is for ROM BAR, and it will
300 * be enabled later, don't add it to the list
301 */
302 if (!((idx == PCI_ROM_RESOURCE) &&
303 (!(res->flags & IORESOURCE_ROM_ENABLE))))
Yinghai Lu67cc7e22012-01-21 02:08:32 -0800304 add_to_list(fail_head,
305 dev_res->dev, res,
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700306 0 /* don't care */,
307 0 /* don't care */);
Yinghai Lu9a928662010-02-28 15:49:39 -0800308 }
Ram Paifc075e12011-02-14 17:43:19 -0800309 reset_resource(res);
Rajesh Shah542df5d2005-04-28 00:25:50 -0700310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
312}
313
Yinghai Luaa914f52013-07-25 06:31:38 -0700314static unsigned long pci_fail_res_type_mask(struct list_head *fail_head)
315{
316 struct pci_dev_resource *fail_res;
317 unsigned long mask = 0;
318
319 /* check failed type */
320 list_for_each_entry(fail_res, fail_head, list)
321 mask |= fail_res->flags;
322
323 /*
324 * one pref failed resource will set IORESOURCE_MEM,
325 * as we can allocate pref in non-pref range.
326 * Will release all assigned non-pref sibling resources
327 * according to that bit.
328 */
329 return mask & (IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH);
330}
331
332static bool pci_need_to_release(unsigned long mask, struct resource *res)
333{
334 if (res->flags & IORESOURCE_IO)
335 return !!(mask & IORESOURCE_IO);
336
337 /* check pref at first */
338 if (res->flags & IORESOURCE_PREFETCH) {
339 if (mask & IORESOURCE_PREFETCH)
340 return true;
341 /* count pref if its parent is non-pref */
342 else if ((mask & IORESOURCE_MEM) &&
343 !(res->parent->flags & IORESOURCE_PREFETCH))
344 return true;
345 else
346 return false;
347 }
348
349 if (res->flags & IORESOURCE_MEM)
350 return !!(mask & IORESOURCE_MEM);
351
352 return false; /* should not get here */
353}
354
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800355static void __assign_resources_sorted(struct list_head *head,
356 struct list_head *realloc_head,
357 struct list_head *fail_head)
Ram Paic8adf9a2011-02-14 17:43:20 -0800358{
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800359 /*
360 * Should not assign requested resources at first.
361 * they could be adjacent, so later reassign can not reallocate
362 * them one by one in parent resource window.
Masanari Iida367fa982012-07-23 22:39:51 +0900363 * Try to assign requested + add_size at beginning
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800364 * if could do that, could get out early.
365 * if could not do that, we still try to assign requested at first,
366 * then try to reassign add_size for some resources.
Yinghai Luaa914f52013-07-25 06:31:38 -0700367 *
368 * Separate three resource type checking if we need to release
369 * assigned resource after requested + add_size try.
370 * 1. if there is io port assign fail, will release assigned
371 * io port.
372 * 2. if there is pref mmio assign fail, release assigned
373 * pref mmio.
374 * if assigned pref mmio's parent is non-pref mmio and there
375 * is non-pref mmio assign fail, will release that assigned
376 * pref mmio.
377 * 3. if there is non-pref mmio assign fail or pref mmio
378 * assigned fail, will release assigned non-pref mmio.
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800379 */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800380 LIST_HEAD(save_head);
381 LIST_HEAD(local_fail_head);
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800382 struct pci_dev_resource *save_res;
Wei Yangd74b9022015-03-25 16:23:51 +0800383 struct pci_dev_resource *dev_res, *tmp_res, *dev_res2;
Yinghai Luaa914f52013-07-25 06:31:38 -0700384 unsigned long fail_type;
Wei Yangd74b9022015-03-25 16:23:51 +0800385 resource_size_t add_align, align;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800386
387 /* Check if optional add_size is there */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800388 if (!realloc_head || list_empty(realloc_head))
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800389 goto requested_and_reassign;
390
391 /* Save original start, end, flags etc at first */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800392 list_for_each_entry(dev_res, head, list) {
393 if (add_to_list(&save_head, dev_res->dev, dev_res->res, 0, 0)) {
Yinghai Lubffc56d2012-01-21 02:08:30 -0800394 free_list(&save_head);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800395 goto requested_and_reassign;
396 }
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800397 }
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800398
399 /* Update res in head list with add_size in realloc_head list */
Wei Yangd74b9022015-03-25 16:23:51 +0800400 list_for_each_entry_safe(dev_res, tmp_res, head, list) {
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800401 dev_res->res->end += get_res_add_size(realloc_head,
402 dev_res->res);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800403
Wei Yangd74b9022015-03-25 16:23:51 +0800404 /*
405 * There are two kinds of additional resources in the list:
406 * 1. bridge resource -- IORESOURCE_STARTALIGN
407 * 2. SR-IOV resource -- IORESOURCE_SIZEALIGN
408 * Here just fix the additional alignment for bridge
409 */
410 if (!(dev_res->res->flags & IORESOURCE_STARTALIGN))
411 continue;
412
413 add_align = get_res_add_align(realloc_head, dev_res->res);
414
415 /*
416 * The "head" list is sorted by the alignment to make sure
417 * resources with bigger alignment will be assigned first.
418 * After we change the alignment of a dev_res in "head" list,
419 * we need to reorder the list by alignment to make it
420 * consistent.
421 */
422 if (add_align > dev_res->res->start) {
Yinghai Lu552bc942015-05-28 22:40:00 -0700423 resource_size_t r_size = resource_size(dev_res->res);
424
Wei Yangd74b9022015-03-25 16:23:51 +0800425 dev_res->res->start = add_align;
Yinghai Lu552bc942015-05-28 22:40:00 -0700426 dev_res->res->end = add_align + r_size - 1;
Wei Yangd74b9022015-03-25 16:23:51 +0800427
428 list_for_each_entry(dev_res2, head, list) {
429 align = pci_resource_alignment(dev_res2->dev,
430 dev_res2->res);
Wei Yanga6b65982015-05-19 14:24:17 +0800431 if (add_align > align) {
Wei Yangd74b9022015-03-25 16:23:51 +0800432 list_move_tail(&dev_res->list,
433 &dev_res2->list);
Wei Yanga6b65982015-05-19 14:24:17 +0800434 break;
435 }
Wei Yangd74b9022015-03-25 16:23:51 +0800436 }
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800437 }
Wei Yangd74b9022015-03-25 16:23:51 +0800438
439 }
440
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800441 /* Try updated head list with add_size added */
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800442 assign_requested_resources_sorted(head, &local_fail_head);
443
444 /* all assigned with add_size ? */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800445 if (list_empty(&local_fail_head)) {
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800446 /* Remove head list from realloc_head list */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800447 list_for_each_entry(dev_res, head, list)
448 remove_from_list(realloc_head, dev_res->res);
Yinghai Lubffc56d2012-01-21 02:08:30 -0800449 free_list(&save_head);
450 free_list(head);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800451 return;
452 }
453
Yinghai Luaa914f52013-07-25 06:31:38 -0700454 /* check failed type */
455 fail_type = pci_fail_res_type_mask(&local_fail_head);
456 /* remove not need to be released assigned res from head list etc */
457 list_for_each_entry_safe(dev_res, tmp_res, head, list)
458 if (dev_res->res->parent &&
459 !pci_need_to_release(fail_type, dev_res->res)) {
460 /* remove it from realloc_head list */
461 remove_from_list(realloc_head, dev_res->res);
462 remove_from_list(&save_head, dev_res->res);
463 list_del(&dev_res->list);
464 kfree(dev_res);
465 }
466
Yinghai Lubffc56d2012-01-21 02:08:30 -0800467 free_list(&local_fail_head);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800468 /* Release assigned resource */
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800469 list_for_each_entry(dev_res, head, list)
470 if (dev_res->res->parent)
471 release_resource(dev_res->res);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800472 /* Restore start/end/flags from saved list */
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800473 list_for_each_entry(save_res, &save_head, list) {
474 struct resource *res = save_res->res;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800475
Yinghai Lub9b0bba2012-01-21 02:08:29 -0800476 res->start = save_res->start;
477 res->end = save_res->end;
478 res->flags = save_res->flags;
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800479 }
Yinghai Lubffc56d2012-01-21 02:08:30 -0800480 free_list(&save_head);
Yinghai Lu3e6e0d82012-01-21 02:08:20 -0800481
482requested_and_reassign:
Ram Paic8adf9a2011-02-14 17:43:20 -0800483 /* Satisfy the must-have resource requests */
484 assign_requested_resources_sorted(head, fail_head);
485
Ram Pai0a2daa12011-07-25 13:08:41 -0700486 /* Try to satisfy any additional optional resource
Ram Paic8adf9a2011-02-14 17:43:20 -0800487 requests */
Ram Pai9e8bf932011-07-25 13:08:42 -0700488 if (realloc_head)
489 reassign_resources_sorted(realloc_head, head);
Yinghai Lubffc56d2012-01-21 02:08:30 -0800490 free_list(head);
Ram Paic8adf9a2011-02-14 17:43:20 -0800491}
492
Yinghai Lu6841ec62010-01-22 01:02:25 -0800493static void pdev_assign_resources_sorted(struct pci_dev *dev,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800494 struct list_head *add_head,
495 struct list_head *fail_head)
Yinghai Lu6841ec62010-01-22 01:02:25 -0800496{
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800497 LIST_HEAD(head);
Yinghai Lu6841ec62010-01-22 01:02:25 -0800498
Yinghai Lu6841ec62010-01-22 01:02:25 -0800499 __dev_sort_resources(dev, &head);
Yinghai Lu8424d752012-01-21 02:08:21 -0800500 __assign_resources_sorted(&head, add_head, fail_head);
Yinghai Lu6841ec62010-01-22 01:02:25 -0800501
502}
503
504static void pbus_assign_resources_sorted(const struct pci_bus *bus,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800505 struct list_head *realloc_head,
506 struct list_head *fail_head)
Yinghai Lu6841ec62010-01-22 01:02:25 -0800507{
508 struct pci_dev *dev;
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800509 LIST_HEAD(head);
Yinghai Lu6841ec62010-01-22 01:02:25 -0800510
Yinghai Lu6841ec62010-01-22 01:02:25 -0800511 list_for_each_entry(dev, &bus->devices, bus_list)
512 __dev_sort_resources(dev, &head);
513
Ram Pai9e8bf932011-07-25 13:08:42 -0700514 __assign_resources_sorted(&head, realloc_head, fail_head);
Yinghai Lu6841ec62010-01-22 01:02:25 -0800515}
516
Dominik Brodowskib3743fa2005-09-09 13:03:23 -0700517void pci_setup_cardbus(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
519 struct pci_dev *bridge = bus->self;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600520 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 struct pci_bus_region region;
522
Yinghai Lub918c622012-05-17 18:51:11 -0700523 dev_info(&bridge->dev, "CardBus bridge to %pR\n",
524 &bus->busn_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600526 res = bus->resource[0];
Yinghai Lufc279852013-12-09 22:54:40 -0800527 pcibios_resource_to_bus(bridge->bus, &region, res);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600528 if (res->flags & IORESOURCE_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 /*
530 * The IO resource is allocated a range twice as large as it
531 * would normally need. This allows us to set both IO regs.
532 */
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600533 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
535 region.start);
536 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
537 region.end);
538 }
539
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600540 res = bus->resource[1];
Yinghai Lufc279852013-12-09 22:54:40 -0800541 pcibios_resource_to_bus(bridge->bus, &region, res);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600542 if (res->flags & IORESOURCE_IO) {
543 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
545 region.start);
546 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
547 region.end);
548 }
549
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600550 res = bus->resource[2];
Yinghai Lufc279852013-12-09 22:54:40 -0800551 pcibios_resource_to_bus(bridge->bus, &region, res);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600552 if (res->flags & IORESOURCE_MEM) {
553 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
555 region.start);
556 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
557 region.end);
558 }
559
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600560 res = bus->resource[3];
Yinghai Lufc279852013-12-09 22:54:40 -0800561 pcibios_resource_to_bus(bridge->bus, &region, res);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600562 if (res->flags & IORESOURCE_MEM) {
563 dev_info(&bridge->dev, " bridge window %pR\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
565 region.start);
566 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
567 region.end);
568 }
569}
Dominik Brodowskib3743fa2005-09-09 13:03:23 -0700570EXPORT_SYMBOL(pci_setup_cardbus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572/* Initialize bridges with base/limit values we have collected.
573 PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
574 requires that if there is no I/O ports or memory behind the
575 bridge, corresponding range must be turned off by writing base
576 value greater than limit to the bridge's base/limit registers.
577
578 Note: care must be taken when updating I/O base/limit registers
579 of bridges which support 32-bit I/O. This update requires two
580 config space writes, so it's quite possible that an I/O window of
581 the bridge will have some undesirable address (e.g. 0) after the
582 first write. Ditto 64-bit prefetchable MMIO. */
Yinghai Lu3f2f4dc2015-01-15 10:22:31 -0600583static void pci_setup_bridge_io(struct pci_dev *bridge)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600585 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 struct pci_bus_region region;
Bjorn Helgaas2b28ae12012-07-09 13:38:57 -0600587 unsigned long io_mask;
588 u8 io_base_lo, io_limit_lo;
Bjorn Helgaas5b764b82013-11-27 17:24:50 -0700589 u16 l;
590 u32 io_upper16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Bjorn Helgaas2b28ae12012-07-09 13:38:57 -0600592 io_mask = PCI_IO_RANGE_MASK;
593 if (bridge->io_window_1k)
594 io_mask = PCI_IO_1K_RANGE_MASK;
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 /* Set up the top and bottom of the PCI I/O segment for this bus. */
Yinghai Lu3f2f4dc2015-01-15 10:22:31 -0600597 res = &bridge->resource[PCI_BRIDGE_RESOURCES + 0];
Yinghai Lufc279852013-12-09 22:54:40 -0800598 pcibios_resource_to_bus(bridge->bus, &region, res);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600599 if (res->flags & IORESOURCE_IO) {
Bjorn Helgaas5b764b82013-11-27 17:24:50 -0700600 pci_read_config_word(bridge, PCI_IO_BASE, &l);
Bjorn Helgaas2b28ae12012-07-09 13:38:57 -0600601 io_base_lo = (region.start >> 8) & io_mask;
602 io_limit_lo = (region.end >> 8) & io_mask;
Bjorn Helgaas5b764b82013-11-27 17:24:50 -0700603 l = ((u16) io_limit_lo << 8) | io_base_lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 /* Set up upper 16 bits of I/O base/limit. */
605 io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600606 dev_info(&bridge->dev, " bridge window %pR\n", res);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800607 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 /* Clear upper 16 bits of I/O base/limit. */
609 io_upper16 = 0;
610 l = 0x00f0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
612 /* Temporarily disable the I/O range before updating PCI_IO_BASE. */
613 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
614 /* Update lower 16 bits of I/O base/limit. */
Bjorn Helgaas5b764b82013-11-27 17:24:50 -0700615 pci_write_config_word(bridge, PCI_IO_BASE, l);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 /* Update upper 16 bits of I/O base/limit. */
617 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800618}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Yinghai Lu3f2f4dc2015-01-15 10:22:31 -0600620static void pci_setup_bridge_mmio(struct pci_dev *bridge)
Yinghai Lu7cc59972009-12-22 15:02:21 -0800621{
Yinghai Lu7cc59972009-12-22 15:02:21 -0800622 struct resource *res;
623 struct pci_bus_region region;
624 u32 l;
625
626 /* Set up the top and bottom of the PCI Memory segment for this bus. */
Yinghai Lu3f2f4dc2015-01-15 10:22:31 -0600627 res = &bridge->resource[PCI_BRIDGE_RESOURCES + 1];
Yinghai Lufc279852013-12-09 22:54:40 -0800628 pcibios_resource_to_bus(bridge->bus, &region, res);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600629 if (res->flags & IORESOURCE_MEM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 l = (region.start >> 16) & 0xfff0;
631 l |= region.end & 0xfff00000;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600632 dev_info(&bridge->dev, " bridge window %pR\n", res);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800633 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 l = 0x0000fff0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
636 pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800637}
638
Yinghai Lu3f2f4dc2015-01-15 10:22:31 -0600639static void pci_setup_bridge_mmio_pref(struct pci_dev *bridge)
Yinghai Lu7cc59972009-12-22 15:02:21 -0800640{
Yinghai Lu7cc59972009-12-22 15:02:21 -0800641 struct resource *res;
642 struct pci_bus_region region;
643 u32 l, bu, lu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 /* Clear out the upper 32 bits of PREF limit.
646 If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
647 disables PREF range, which is ok. */
648 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
649
650 /* Set up PREF base/limit. */
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100651 bu = lu = 0;
Yinghai Lu3f2f4dc2015-01-15 10:22:31 -0600652 res = &bridge->resource[PCI_BRIDGE_RESOURCES + 2];
Yinghai Lufc279852013-12-09 22:54:40 -0800653 pcibios_resource_to_bus(bridge->bus, &region, res);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600654 if (res->flags & IORESOURCE_PREFETCH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 l = (region.start >> 16) & 0xfff0;
656 l |= region.end & 0xfff00000;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600657 if (res->flags & IORESOURCE_MEM_64) {
Yinghai Lu1f82de12009-04-23 20:48:32 -0700658 bu = upper_32_bits(region.start);
659 lu = upper_32_bits(region.end);
Yinghai Lu1f82de12009-04-23 20:48:32 -0700660 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600661 dev_info(&bridge->dev, " bridge window %pR\n", res);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800662 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 l = 0x0000fff0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
665 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
666
Alex Williamson59353ea2009-11-30 14:51:44 -0700667 /* Set the upper 32 bits of PREF base & limit. */
668 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
669 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800670}
671
672static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type)
673{
674 struct pci_dev *bridge = bus->self;
675
Yinghai Lub918c622012-05-17 18:51:11 -0700676 dev_info(&bridge->dev, "PCI bridge to %pR\n",
677 &bus->busn_res);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800678
679 if (type & IORESOURCE_IO)
Yinghai Lu3f2f4dc2015-01-15 10:22:31 -0600680 pci_setup_bridge_io(bridge);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800681
682 if (type & IORESOURCE_MEM)
Yinghai Lu3f2f4dc2015-01-15 10:22:31 -0600683 pci_setup_bridge_mmio(bridge);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800684
685 if (type & IORESOURCE_PREFETCH)
Yinghai Lu3f2f4dc2015-01-15 10:22:31 -0600686 pci_setup_bridge_mmio_pref(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
689}
690
Gavin Shand366d282016-05-20 16:41:25 +1000691void __weak pcibios_setup_bridge(struct pci_bus *bus, unsigned long type)
692{
693}
694
Benjamin Herrenschmidte2444272011-09-11 14:08:38 -0300695void pci_setup_bridge(struct pci_bus *bus)
Yinghai Lu7cc59972009-12-22 15:02:21 -0800696{
697 unsigned long type = IORESOURCE_IO | IORESOURCE_MEM |
698 IORESOURCE_PREFETCH;
699
Gavin Shand366d282016-05-20 16:41:25 +1000700 pcibios_setup_bridge(bus, type);
Yinghai Lu7cc59972009-12-22 15:02:21 -0800701 __pci_setup_bridge(bus, type);
702}
703
Yinghai Lu8505e722015-01-15 16:21:49 -0600704
705int pci_claim_bridge_resource(struct pci_dev *bridge, int i)
706{
707 if (i < PCI_BRIDGE_RESOURCES || i > PCI_BRIDGE_RESOURCE_END)
708 return 0;
709
710 if (pci_claim_resource(bridge, i) == 0)
711 return 0; /* claimed the window */
712
713 if ((bridge->class >> 8) != PCI_CLASS_BRIDGE_PCI)
714 return 0;
715
716 if (!pci_bus_clip_resource(bridge, i))
717 return -EINVAL; /* clipping didn't change anything */
718
719 switch (i - PCI_BRIDGE_RESOURCES) {
720 case 0:
721 pci_setup_bridge_io(bridge);
722 break;
723 case 1:
724 pci_setup_bridge_mmio(bridge);
725 break;
726 case 2:
727 pci_setup_bridge_mmio_pref(bridge);
728 break;
729 default:
730 return -EINVAL;
731 }
732
733 if (pci_claim_resource(bridge, i) == 0)
734 return 0; /* claimed a smaller window */
735
736 return -EINVAL;
737}
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739/* Check whether the bridge supports optional I/O and
740 prefetchable memory ranges. If not, the respective
741 base/limit registers must be read-only and read as 0. */
Sam Ravnborg96bde062007-03-26 21:53:30 -0800742static void pci_bridge_check_ranges(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
744 u16 io;
745 u32 pmem;
746 struct pci_dev *bridge = bus->self;
747 struct resource *b_res;
748
749 b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
750 b_res[1].flags |= IORESOURCE_MEM;
751
752 pci_read_config_word(bridge, PCI_IO_BASE, &io);
753 if (!io) {
Bjorn Helgaasd2f54d92013-11-27 15:31:07 -0700754 pci_write_config_word(bridge, PCI_IO_BASE, 0xe0f0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 pci_read_config_word(bridge, PCI_IO_BASE, &io);
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700756 pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
757 }
758 if (io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 b_res[0].flags |= IORESOURCE_IO;
Bjorn Helgaasd2f54d92013-11-27 15:31:07 -0700760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 /* DECchip 21050 pass 2 errata: the bridge may miss an address
762 disconnect boundary by one PCI data phase.
763 Workaround: do not use prefetching on this device. */
764 if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001)
765 return;
Bjorn Helgaasd2f54d92013-11-27 15:31:07 -0700766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
768 if (!pmem) {
769 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE,
Bjorn Helgaasd2f54d92013-11-27 15:31:07 -0700770 0xffe0fff0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
772 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0);
773 }
Yinghai Lu1f82de12009-04-23 20:48:32 -0700774 if (pmem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
Yinghai Lu99586102010-01-22 01:02:28 -0800776 if ((pmem & PCI_PREF_RANGE_TYPE_MASK) ==
777 PCI_PREF_RANGE_TYPE_64) {
Yinghai Lu1f82de12009-04-23 20:48:32 -0700778 b_res[2].flags |= IORESOURCE_MEM_64;
Yinghai Lu99586102010-01-22 01:02:28 -0800779 b_res[2].flags |= PCI_PREF_RANGE_TYPE_64;
780 }
Yinghai Lu1f82de12009-04-23 20:48:32 -0700781 }
782
783 /* double check if bridge does support 64 bit pref */
784 if (b_res[2].flags & IORESOURCE_MEM_64) {
785 u32 mem_base_hi, tmp;
786 pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32,
787 &mem_base_hi);
788 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
789 0xffffffff);
790 pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp);
791 if (!tmp)
792 b_res[2].flags &= ~IORESOURCE_MEM_64;
793 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
794 mem_base_hi);
795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
797
798/* Helper function for sizing routines: find first available
799 bus resource of a given type. Note: we intentionally skip
800 the bus resources which have already been assigned (that is,
801 have non-NULL parent resource). */
Yinghai Lu5b285412014-05-19 17:01:55 -0600802static struct resource *find_free_bus_resource(struct pci_bus *bus,
803 unsigned long type_mask, unsigned long type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
805 int i;
806 struct resource *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -0700808 pci_bus_for_each_resource(bus, r, i) {
Ivan Kokshaysky299de032005-06-15 18:59:27 +0400809 if (r == &ioport_resource || r == &iomem_resource)
810 continue;
Jesse Barnes55a10982009-10-27 09:39:18 -0700811 if (r && (r->flags & type_mask) == type && !r->parent)
812 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
814 return NULL;
815}
816
Ram Pai13583b12011-02-14 17:43:17 -0800817static resource_size_t calculate_iosize(resource_size_t size,
818 resource_size_t min_size,
819 resource_size_t size1,
820 resource_size_t old_size,
821 resource_size_t align)
822{
823 if (size < min_size)
824 size = min_size;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400825 if (old_size == 1)
Ram Pai13583b12011-02-14 17:43:17 -0800826 old_size = 0;
827 /* To be fixed in 2.5: we should have sort of HAVE_ISA
828 flag in the struct pci_bus. */
829#if defined(CONFIG_ISA) || defined(CONFIG_EISA)
830 size = (size & 0xff) + ((size & ~0xffUL) << 2);
831#endif
832 size = ALIGN(size + size1, align);
833 if (size < old_size)
834 size = old_size;
835 return size;
836}
837
838static resource_size_t calculate_memsize(resource_size_t size,
839 resource_size_t min_size,
840 resource_size_t size1,
841 resource_size_t old_size,
842 resource_size_t align)
843{
844 if (size < min_size)
845 size = min_size;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400846 if (old_size == 1)
Ram Pai13583b12011-02-14 17:43:17 -0800847 old_size = 0;
848 if (size < old_size)
849 size = old_size;
850 size = ALIGN(size + size1, align);
851 return size;
852}
853
Gavin Shanac5ad932012-09-11 16:59:45 -0600854resource_size_t __weak pcibios_window_alignment(struct pci_bus *bus,
855 unsigned long type)
856{
857 return 1;
858}
859
860#define PCI_P2P_DEFAULT_MEM_ALIGN 0x100000 /* 1MiB */
861#define PCI_P2P_DEFAULT_IO_ALIGN 0x1000 /* 4KiB */
862#define PCI_P2P_DEFAULT_IO_ALIGN_1K 0x400 /* 1KiB */
863
864static resource_size_t window_alignment(struct pci_bus *bus,
865 unsigned long type)
866{
867 resource_size_t align = 1, arch_align;
868
869 if (type & IORESOURCE_MEM)
870 align = PCI_P2P_DEFAULT_MEM_ALIGN;
871 else if (type & IORESOURCE_IO) {
872 /*
873 * Per spec, I/O windows are 4K-aligned, but some
874 * bridges have an extension to support 1K alignment.
875 */
876 if (bus->self->io_window_1k)
877 align = PCI_P2P_DEFAULT_IO_ALIGN_1K;
878 else
879 align = PCI_P2P_DEFAULT_IO_ALIGN;
880 }
881
882 arch_align = pcibios_window_alignment(bus, type);
883 return max(align, arch_align);
884}
885
Ram Paic8adf9a2011-02-14 17:43:20 -0800886/**
887 * pbus_size_io() - size the io window of a given bus
888 *
889 * @bus : the bus
890 * @min_size : the minimum io window that must to be allocated
891 * @add_size : additional optional io window
Ram Pai9e8bf932011-07-25 13:08:42 -0700892 * @realloc_head : track the additional io window on this list
Ram Paic8adf9a2011-02-14 17:43:20 -0800893 *
894 * Sizing the IO windows of the PCI-PCI bridge is trivial,
Yinghai Lufd591342012-07-09 19:55:29 -0600895 * since these windows have 1K or 4K granularity and the IO ranges
Ram Paic8adf9a2011-02-14 17:43:20 -0800896 * of non-bridge PCI devices are limited to 256 bytes.
897 * We must be careful with the ISA aliasing though.
898 */
899static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size,
Yinghai Lubdc4abe2012-01-21 02:08:27 -0800900 resource_size_t add_size, struct list_head *realloc_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
902 struct pci_dev *dev;
Yinghai Lu5b285412014-05-19 17:01:55 -0600903 struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO,
904 IORESOURCE_IO);
Wei Yang11251a82013-08-02 17:31:05 +0800905 resource_size_t size = 0, size0 = 0, size1 = 0;
Yinghai Lube768912011-07-25 13:08:38 -0700906 resource_size_t children_add_size = 0;
Bjorn Helgaas2d1d6672013-08-05 16:15:10 -0600907 resource_size_t min_align, align;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 if (!b_res)
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700910 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Bjorn Helgaas2d1d6672013-08-05 16:15:10 -0600912 min_align = window_alignment(bus, IORESOURCE_IO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 list_for_each_entry(dev, &bus->devices, bus_list) {
914 int i;
915
916 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
917 struct resource *r = &dev->resource[i];
918 unsigned long r_size;
919
920 if (r->parent || !(r->flags & IORESOURCE_IO))
921 continue;
Zhao, Yu022edd82008-10-13 19:24:28 +0800922 r_size = resource_size(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 if (r_size < 0x400)
925 /* Might be re-aligned for ISA */
926 size += r_size;
927 else
928 size1 += r_size;
Yinghai Lube768912011-07-25 13:08:38 -0700929
Yinghai Lufd591342012-07-09 19:55:29 -0600930 align = pci_resource_alignment(dev, r);
931 if (align > min_align)
932 min_align = align;
933
Ram Pai9e8bf932011-07-25 13:08:42 -0700934 if (realloc_head)
935 children_add_size += get_res_add_size(realloc_head, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
937 }
Yinghai Lufd591342012-07-09 19:55:29 -0600938
Ram Paic8adf9a2011-02-14 17:43:20 -0800939 size0 = calculate_iosize(size, min_size, size1,
Yinghai Lufd591342012-07-09 19:55:29 -0600940 resource_size(b_res), min_align);
Yinghai Lube768912011-07-25 13:08:38 -0700941 if (children_add_size > add_size)
942 add_size = children_add_size;
Ram Pai9e8bf932011-07-25 13:08:42 -0700943 size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
Yinghai Lua4ac9fe2012-01-21 02:08:17 -0800944 calculate_iosize(size, min_size, add_size + size1,
Yinghai Lufd591342012-07-09 19:55:29 -0600945 resource_size(b_res), min_align);
Ram Paic8adf9a2011-02-14 17:43:20 -0800946 if (!size0 && !size1) {
Bjorn Helgaas865df572009-11-04 10:32:57 -0700947 if (b_res->start || b_res->end)
Ryan Desfosses227f0642014-04-18 20:13:50 -0400948 dev_info(&bus->self->dev, "disabling bridge window %pR to %pR (unused)\n",
949 b_res, &bus->busn_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 b_res->flags = 0;
951 return;
952 }
Yinghai Lufd591342012-07-09 19:55:29 -0600953
954 b_res->start = min_align;
Ram Paic8adf9a2011-02-14 17:43:20 -0800955 b_res->end = b_res->start + size0 - 1;
Ivan Kokshaysky88452562008-03-30 19:50:14 +0400956 b_res->flags |= IORESOURCE_STARTALIGN;
Yinghai Lub5924432012-01-21 02:08:31 -0800957 if (size1 > size0 && realloc_head) {
Yinghai Lufd591342012-07-09 19:55:29 -0600958 add_to_list(realloc_head, bus->self, b_res, size1-size0,
959 min_align);
Ryan Desfosses227f0642014-04-18 20:13:50 -0400960 dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window %pR to %pR add_size %llx\n",
961 b_res, &bus->busn_res,
962 (unsigned long long)size1-size0);
Yinghai Lub5924432012-01-21 02:08:31 -0800963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
965
Gavin Shanc1215042012-09-11 16:59:46 -0600966static inline resource_size_t calculate_mem_align(resource_size_t *aligns,
967 int max_order)
968{
969 resource_size_t align = 0;
970 resource_size_t min_align = 0;
971 int order;
972
973 for (order = 0; order <= max_order; order++) {
974 resource_size_t align1 = 1;
975
976 align1 <<= (order + 20);
977
978 if (!align)
979 min_align = align1;
980 else if (ALIGN(align + min_align, min_align) < align1)
981 min_align = align1 >> 1;
982 align += aligns[order];
983 }
984
985 return min_align;
986}
987
Ram Paic8adf9a2011-02-14 17:43:20 -0800988/**
989 * pbus_size_mem() - size the memory window of a given bus
990 *
991 * @bus : the bus
Wei Yang496f70c2013-08-02 17:31:04 +0800992 * @mask: mask the resource flag, then compare it with type
993 * @type: the type of free resource from bridge
Yinghai Lu5b285412014-05-19 17:01:55 -0600994 * @type2: second match type
995 * @type3: third match type
Ram Paic8adf9a2011-02-14 17:43:20 -0800996 * @min_size : the minimum memory window that must to be allocated
997 * @add_size : additional optional memory window
Ram Pai9e8bf932011-07-25 13:08:42 -0700998 * @realloc_head : track the additional memory window on this list
Ram Paic8adf9a2011-02-14 17:43:20 -0800999 *
1000 * Calculate the size of the bus and minimal alignment which
1001 * guarantees that all child resources fit in this size.
Bjorn Helgaas30afe8d2014-05-19 18:28:37 -06001002 *
1003 * Returns -ENOSPC if there's no available bus resource of the desired type.
1004 * Otherwise, sets the bus resource start/end to indicate the required
1005 * size, adds things to realloc_head (if supplied), and returns 0.
Ram Paic8adf9a2011-02-14 17:43:20 -08001006 */
Eric W. Biederman28760482009-09-09 14:09:24 -07001007static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
Yinghai Lu5b285412014-05-19 17:01:55 -06001008 unsigned long type, unsigned long type2,
1009 unsigned long type3,
1010 resource_size_t min_size, resource_size_t add_size,
1011 struct list_head *realloc_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
1013 struct pci_dev *dev;
Ram Paic8adf9a2011-02-14 17:43:20 -08001014 resource_size_t min_align, align, size, size0, size1;
Yinghai Lu096d4222014-07-03 13:46:17 -07001015 resource_size_t aligns[18]; /* Alignments from 1Mb to 128Gb */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 int order, max_order;
Yinghai Lu5b285412014-05-19 17:01:55 -06001017 struct resource *b_res = find_free_bus_resource(bus,
1018 mask | IORESOURCE_PREFETCH, type);
Yinghai Lube768912011-07-25 13:08:38 -07001019 resource_size_t children_add_size = 0;
Wei Yangd74b9022015-03-25 16:23:51 +08001020 resource_size_t children_add_align = 0;
1021 resource_size_t add_align = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 if (!b_res)
Bjorn Helgaas30afe8d2014-05-19 18:28:37 -06001024 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 memset(aligns, 0, sizeof(aligns));
1027 max_order = 0;
1028 size = 0;
1029
1030 list_for_each_entry(dev, &bus->devices, bus_list) {
1031 int i;
Yinghai Lu1f82de12009-04-23 20:48:32 -07001032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
1034 struct resource *r = &dev->resource[i];
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +11001035 resource_size_t r_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
David Daneya2220d82015-10-29 17:35:39 -05001037 if (r->parent || (r->flags & IORESOURCE_PCI_FIXED) ||
1038 ((r->flags & mask) != type &&
1039 (r->flags & mask) != type2 &&
1040 (r->flags & mask) != type3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 continue;
Zhao, Yu022edd82008-10-13 19:24:28 +08001042 r_size = resource_size(r);
Yinghai Lu2aceefc2011-07-25 13:08:40 -07001043#ifdef CONFIG_PCI_IOV
1044 /* put SRIOV requested res to the optional list */
Ram Pai9e8bf932011-07-25 13:08:42 -07001045 if (realloc_head && i >= PCI_IOV_RESOURCES &&
Yinghai Lu2aceefc2011-07-25 13:08:40 -07001046 i <= PCI_IOV_RESOURCE_END) {
Wei Yangd74b9022015-03-25 16:23:51 +08001047 add_align = max(pci_resource_alignment(dev, r), add_align);
Yinghai Lu2aceefc2011-07-25 13:08:40 -07001048 r->end = r->start - 1;
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001049 add_to_list(realloc_head, dev, r, r_size, 0/* don't care */);
Yinghai Lu2aceefc2011-07-25 13:08:40 -07001050 children_add_size += r_size;
1051 continue;
1052 }
1053#endif
Alan14c85302014-05-19 14:03:14 +01001054 /*
1055 * aligns[0] is for 1MB (since bridge memory
1056 * windows are always at least 1MB aligned), so
1057 * keep "order" from being negative for smaller
1058 * resources.
1059 */
Chris Wright6faf17f2009-08-28 13:00:06 -07001060 align = pci_resource_alignment(dev, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 order = __ffs(align) - 20;
Alan14c85302014-05-19 14:03:14 +01001062 if (order < 0)
1063 order = 0;
1064 if (order >= ARRAY_SIZE(aligns)) {
Ryan Desfosses227f0642014-04-18 20:13:50 -04001065 dev_warn(&dev->dev, "disabling BAR %d: %pR (bad alignment %#llx)\n",
1066 i, r, (unsigned long long) align);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 r->flags = 0;
1068 continue;
1069 }
Yongji Xiec9c75142017-04-10 19:58:11 +08001070 size += max(r_size, align);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 /* Exclude ranges with size > align from
1072 calculation of the alignment. */
Yongji Xiec9c75142017-04-10 19:58:11 +08001073 if (r_size <= align)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 aligns[order] += align;
1075 if (order > max_order)
1076 max_order = order;
Yinghai Lube768912011-07-25 13:08:38 -07001077
Wei Yangd74b9022015-03-25 16:23:51 +08001078 if (realloc_head) {
Ram Pai9e8bf932011-07-25 13:08:42 -07001079 children_add_size += get_res_add_size(realloc_head, r);
Wei Yangd74b9022015-03-25 16:23:51 +08001080 children_add_align = get_res_add_align(realloc_head, r);
1081 add_align = max(add_align, children_add_align);
1082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 }
1084 }
Jeremy Fitzhardinge8308c542008-09-11 01:31:50 -07001085
Gavin Shanc1215042012-09-11 16:59:46 -06001086 min_align = calculate_mem_align(aligns, max_order);
Wei Yang3ad94b02013-09-06 09:45:58 +08001087 min_align = max(min_align, window_alignment(bus, b_res->flags));
Linus Torvaldsb42282e2011-04-11 10:53:11 -07001088 size0 = calculate_memsize(size, min_size, 0, resource_size(b_res), min_align);
Wei Yangd74b9022015-03-25 16:23:51 +08001089 add_align = max(min_align, add_align);
Yinghai Lube768912011-07-25 13:08:38 -07001090 if (children_add_size > add_size)
1091 add_size = children_add_size;
Ram Pai9e8bf932011-07-25 13:08:42 -07001092 size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
Yinghai Lua4ac9fe2012-01-21 02:08:17 -08001093 calculate_memsize(size, min_size, add_size,
Wei Yangd74b9022015-03-25 16:23:51 +08001094 resource_size(b_res), add_align);
Ram Paic8adf9a2011-02-14 17:43:20 -08001095 if (!size0 && !size1) {
Bjorn Helgaas865df572009-11-04 10:32:57 -07001096 if (b_res->start || b_res->end)
Ryan Desfosses227f0642014-04-18 20:13:50 -04001097 dev_info(&bus->self->dev, "disabling bridge window %pR to %pR (unused)\n",
1098 b_res, &bus->busn_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 b_res->flags = 0;
Bjorn Helgaas30afe8d2014-05-19 18:28:37 -06001100 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 }
1102 b_res->start = min_align;
Ram Paic8adf9a2011-02-14 17:43:20 -08001103 b_res->end = size0 + min_align - 1;
Yinghai Lu5b285412014-05-19 17:01:55 -06001104 b_res->flags |= IORESOURCE_STARTALIGN;
Yinghai Lub5924432012-01-21 02:08:31 -08001105 if (size1 > size0 && realloc_head) {
Wei Yangd74b9022015-03-25 16:23:51 +08001106 add_to_list(realloc_head, bus->self, b_res, size1-size0, add_align);
1107 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 -04001108 b_res, &bus->busn_res,
Wei Yangd74b9022015-03-25 16:23:51 +08001109 (unsigned long long) (size1 - size0),
1110 (unsigned long long) add_align);
Yinghai Lub5924432012-01-21 02:08:31 -08001111 }
Bjorn Helgaas30afe8d2014-05-19 18:28:37 -06001112 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113}
1114
Ram Pai0a2daa12011-07-25 13:08:41 -07001115unsigned long pci_cardbus_resource_alignment(struct resource *res)
1116{
1117 if (res->flags & IORESOURCE_IO)
1118 return pci_cardbus_io_size;
1119 if (res->flags & IORESOURCE_MEM)
1120 return pci_cardbus_mem_size;
1121 return 0;
1122}
1123
1124static void pci_bus_size_cardbus(struct pci_bus *bus,
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001125 struct list_head *realloc_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
1127 struct pci_dev *bridge = bus->self;
1128 struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
Yinghai Lu11848932012-02-10 15:33:47 -08001129 resource_size_t b_res_3_size = pci_cardbus_mem_size * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 u16 ctrl;
1131
Yinghai Lu3796f1e2012-02-10 15:33:48 -08001132 if (b_res[0].parent)
1133 goto handle_b_res_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 /*
1135 * Reserve some resources for CardBus. We reserve
1136 * a fixed amount of bus space for CardBus bridges.
1137 */
Yinghai Lu11848932012-02-10 15:33:47 -08001138 b_res[0].start = pci_cardbus_io_size;
1139 b_res[0].end = b_res[0].start + pci_cardbus_io_size - 1;
1140 b_res[0].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
1141 if (realloc_head) {
1142 b_res[0].end -= pci_cardbus_io_size;
1143 add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size,
1144 pci_cardbus_io_size);
1145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Yinghai Lu3796f1e2012-02-10 15:33:48 -08001147handle_b_res_1:
1148 if (b_res[1].parent)
1149 goto handle_b_res_2;
Yinghai Lu11848932012-02-10 15:33:47 -08001150 b_res[1].start = pci_cardbus_io_size;
1151 b_res[1].end = b_res[1].start + pci_cardbus_io_size - 1;
1152 b_res[1].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
1153 if (realloc_head) {
1154 b_res[1].end -= pci_cardbus_io_size;
1155 add_to_list(realloc_head, bridge, b_res+1, pci_cardbus_io_size,
1156 pci_cardbus_io_size);
1157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Yinghai Lu3796f1e2012-02-10 15:33:48 -08001159handle_b_res_2:
Yinghai Ludcef0d02012-02-10 15:33:46 -08001160 /* MEM1 must not be pref mmio */
1161 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
1162 if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM1) {
1163 ctrl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1;
1164 pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
1165 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
1166 }
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 /*
1169 * Check whether prefetchable memory is supported
1170 * by this bridge.
1171 */
1172 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
1173 if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
1174 ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
1175 pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
1176 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
1177 }
1178
Yinghai Lu3796f1e2012-02-10 15:33:48 -08001179 if (b_res[2].parent)
1180 goto handle_b_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 /*
1182 * If we have prefetchable memory support, allocate
1183 * two regions. Otherwise, allocate one region of
1184 * twice the size.
1185 */
1186 if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
Yinghai Lu11848932012-02-10 15:33:47 -08001187 b_res[2].start = pci_cardbus_mem_size;
1188 b_res[2].end = b_res[2].start + pci_cardbus_mem_size - 1;
1189 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH |
1190 IORESOURCE_STARTALIGN;
1191 if (realloc_head) {
1192 b_res[2].end -= pci_cardbus_mem_size;
1193 add_to_list(realloc_head, bridge, b_res+2,
1194 pci_cardbus_mem_size, pci_cardbus_mem_size);
1195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Yinghai Lu11848932012-02-10 15:33:47 -08001197 /* reduce that to half */
1198 b_res_3_size = pci_cardbus_mem_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 }
Ram Pai0a2daa12011-07-25 13:08:41 -07001200
Yinghai Lu3796f1e2012-02-10 15:33:48 -08001201handle_b_res_3:
1202 if (b_res[3].parent)
1203 goto handle_done;
Yinghai Lu11848932012-02-10 15:33:47 -08001204 b_res[3].start = pci_cardbus_mem_size;
1205 b_res[3].end = b_res[3].start + b_res_3_size - 1;
1206 b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_STARTALIGN;
1207 if (realloc_head) {
1208 b_res[3].end -= b_res_3_size;
1209 add_to_list(realloc_head, bridge, b_res+3, b_res_3_size,
1210 pci_cardbus_mem_size);
1211 }
Yinghai Lu3796f1e2012-02-10 15:33:48 -08001212
1213handle_done:
1214 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215}
1216
Bjorn Helgaas10874f52014-04-14 16:11:40 -06001217void __pci_bus_size_bridges(struct pci_bus *bus, struct list_head *realloc_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
1219 struct pci_dev *dev;
Yinghai Lu5b285412014-05-19 17:01:55 -06001220 unsigned long mask, prefmask, type2 = 0, type3 = 0;
Ram Paic8adf9a2011-02-14 17:43:20 -08001221 resource_size_t additional_mem_size = 0, additional_io_size = 0;
Yinghai Lu5b285412014-05-19 17:01:55 -06001222 struct resource *b_res;
Bjorn Helgaas30afe8d2014-05-19 18:28:37 -06001223 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225 list_for_each_entry(dev, &bus->devices, bus_list) {
1226 struct pci_bus *b = dev->subordinate;
1227 if (!b)
1228 continue;
1229
1230 switch (dev->class >> 8) {
1231 case PCI_CLASS_BRIDGE_CARDBUS:
Ram Pai9e8bf932011-07-25 13:08:42 -07001232 pci_bus_size_cardbus(b, realloc_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 break;
1234
1235 case PCI_CLASS_BRIDGE_PCI:
1236 default:
Ram Pai9e8bf932011-07-25 13:08:42 -07001237 __pci_bus_size_bridges(b, realloc_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 break;
1239 }
1240 }
1241
1242 /* The root bus? */
Wei Yang2ba29e22013-09-06 09:45:56 +08001243 if (pci_is_root_bus(bus))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return;
1245
1246 switch (bus->self->class >> 8) {
1247 case PCI_CLASS_BRIDGE_CARDBUS:
1248 /* don't size cardbuses yet. */
1249 break;
1250
1251 case PCI_CLASS_BRIDGE_PCI:
1252 pci_bridge_check_ranges(bus);
Eric W. Biederman28760482009-09-09 14:09:24 -07001253 if (bus->self->is_hotplug_bridge) {
Ram Paic8adf9a2011-02-14 17:43:20 -08001254 additional_io_size = pci_hotplug_io_size;
1255 additional_mem_size = pci_hotplug_mem_size;
Eric W. Biederman28760482009-09-09 14:09:24 -07001256 }
Bjorn Helgaas67d29b52014-05-19 18:32:18 -06001257 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 default:
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001259 pbus_size_io(bus, realloc_head ? 0 : additional_io_size,
1260 additional_io_size, realloc_head);
Bjorn Helgaas67d29b52014-05-19 18:32:18 -06001261
1262 /*
1263 * If there's a 64-bit prefetchable MMIO window, compute
1264 * the size required to put all 64-bit prefetchable
1265 * resources in it.
1266 */
Yinghai Lu5b285412014-05-19 17:01:55 -06001267 b_res = &bus->self->resource[PCI_BRIDGE_RESOURCES];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 mask = IORESOURCE_MEM;
1269 prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
Yinghai Lu5b285412014-05-19 17:01:55 -06001270 if (b_res[2].flags & IORESOURCE_MEM_64) {
1271 prefmask |= IORESOURCE_MEM_64;
Bjorn Helgaas30afe8d2014-05-19 18:28:37 -06001272 ret = pbus_size_mem(bus, prefmask, prefmask,
Yinghai Lu5b285412014-05-19 17:01:55 -06001273 prefmask, prefmask,
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001274 realloc_head ? 0 : additional_mem_size,
Bjorn Helgaas30afe8d2014-05-19 18:28:37 -06001275 additional_mem_size, realloc_head);
Bjorn Helgaas67d29b52014-05-19 18:32:18 -06001276
1277 /*
1278 * If successful, all non-prefetchable resources
1279 * and any 32-bit prefetchable resources will go in
1280 * the non-prefetchable window.
1281 */
Bjorn Helgaas30afe8d2014-05-19 18:28:37 -06001282 if (ret == 0) {
Bjorn Helgaas30afe8d2014-05-19 18:28:37 -06001283 mask = prefmask;
1284 type2 = prefmask & ~IORESOURCE_MEM_64;
1285 type3 = prefmask & ~IORESOURCE_PREFETCH;
Yinghai Lu5b285412014-05-19 17:01:55 -06001286 }
1287 }
Bjorn Helgaas67d29b52014-05-19 18:32:18 -06001288
1289 /*
1290 * If there is no 64-bit prefetchable window, compute the
1291 * size required to put all prefetchable resources in the
1292 * 32-bit prefetchable window (if there is one).
1293 */
Yinghai Lu5b285412014-05-19 17:01:55 -06001294 if (!type2) {
1295 prefmask &= ~IORESOURCE_MEM_64;
Bjorn Helgaas30afe8d2014-05-19 18:28:37 -06001296 ret = pbus_size_mem(bus, prefmask, prefmask,
Yinghai Lu5b285412014-05-19 17:01:55 -06001297 prefmask, prefmask,
1298 realloc_head ? 0 : additional_mem_size,
Bjorn Helgaas30afe8d2014-05-19 18:28:37 -06001299 additional_mem_size, realloc_head);
Bjorn Helgaas67d29b52014-05-19 18:32:18 -06001300
1301 /*
1302 * If successful, only non-prefetchable resources
1303 * will go in the non-prefetchable window.
1304 */
1305 if (ret == 0)
Yinghai Lu5b285412014-05-19 17:01:55 -06001306 mask = prefmask;
Bjorn Helgaas67d29b52014-05-19 18:32:18 -06001307 else
Yinghai Lu5b285412014-05-19 17:01:55 -06001308 additional_mem_size += additional_mem_size;
Bjorn Helgaas67d29b52014-05-19 18:32:18 -06001309
Yinghai Lu5b285412014-05-19 17:01:55 -06001310 type2 = type3 = IORESOURCE_MEM;
1311 }
Bjorn Helgaas67d29b52014-05-19 18:32:18 -06001312
1313 /*
1314 * Compute the size required to put everything else in the
1315 * non-prefetchable window. This includes:
1316 *
1317 * - all non-prefetchable resources
1318 * - 32-bit prefetchable resources if there's a 64-bit
1319 * prefetchable window or no prefetchable window at all
1320 * - 64-bit prefetchable resources if there's no
1321 * prefetchable window at all
1322 *
1323 * Note that the strategy in __pci_assign_resource() must
1324 * match that used here. Specifically, we cannot put a
1325 * 32-bit prefetchable resource in a 64-bit prefetchable
1326 * window.
1327 */
Yinghai Lu5b285412014-05-19 17:01:55 -06001328 pbus_size_mem(bus, mask, IORESOURCE_MEM, type2, type3,
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001329 realloc_head ? 0 : additional_mem_size,
1330 additional_mem_size, realloc_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 break;
1332 }
1333}
Ram Paic8adf9a2011-02-14 17:43:20 -08001334
Bjorn Helgaas10874f52014-04-14 16:11:40 -06001335void pci_bus_size_bridges(struct pci_bus *bus)
Ram Paic8adf9a2011-02-14 17:43:20 -08001336{
1337 __pci_bus_size_bridges(bus, NULL);
1338}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339EXPORT_SYMBOL(pci_bus_size_bridges);
1340
David Daneyd04d0112015-10-29 17:35:39 -05001341static void assign_fixed_resource_on_bus(struct pci_bus *b, struct resource *r)
1342{
1343 int i;
1344 struct resource *parent_r;
1345 unsigned long mask = IORESOURCE_IO | IORESOURCE_MEM |
1346 IORESOURCE_PREFETCH;
1347
1348 pci_bus_for_each_resource(b, parent_r, i) {
1349 if (!parent_r)
1350 continue;
1351
1352 if ((r->flags & mask) == (parent_r->flags & mask) &&
1353 resource_contains(parent_r, r))
1354 request_resource(parent_r, r);
1355 }
1356}
1357
1358/*
1359 * Try to assign any resources marked as IORESOURCE_PCI_FIXED, as they
1360 * are skipped by pbus_assign_resources_sorted().
1361 */
1362static void pdev_assign_fixed_resources(struct pci_dev *dev)
1363{
1364 int i;
1365
1366 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
1367 struct pci_bus *b;
1368 struct resource *r = &dev->resource[i];
1369
1370 if (r->parent || !(r->flags & IORESOURCE_PCI_FIXED) ||
1371 !(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
1372 continue;
1373
1374 b = dev->bus;
1375 while (b && !r->parent) {
1376 assign_fixed_resource_on_bus(b, r);
1377 b = b->parent;
1378 }
1379 }
1380}
1381
Bjorn Helgaas10874f52014-04-14 16:11:40 -06001382void __pci_bus_assign_resources(const struct pci_bus *bus,
1383 struct list_head *realloc_head,
1384 struct list_head *fail_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
1386 struct pci_bus *b;
1387 struct pci_dev *dev;
1388
Ram Pai9e8bf932011-07-25 13:08:42 -07001389 pbus_assign_resources_sorted(bus, realloc_head, fail_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 list_for_each_entry(dev, &bus->devices, bus_list) {
David Daneyd04d0112015-10-29 17:35:39 -05001392 pdev_assign_fixed_resources(dev);
1393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 b = dev->subordinate;
1395 if (!b)
1396 continue;
1397
Ram Pai9e8bf932011-07-25 13:08:42 -07001398 __pci_bus_assign_resources(b, realloc_head, fail_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400 switch (dev->class >> 8) {
1401 case PCI_CLASS_BRIDGE_PCI:
Yinghai Lu6841ec62010-01-22 01:02:25 -08001402 if (!pci_is_enabled(dev))
1403 pci_setup_bridge(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 break;
1405
1406 case PCI_CLASS_BRIDGE_CARDBUS:
1407 pci_setup_cardbus(b);
1408 break;
1409
1410 default:
Ryan Desfosses227f0642014-04-18 20:13:50 -04001411 dev_info(&dev->dev, "not setting up bridge for bus %04x:%02x\n",
1412 pci_domain_nr(b), b->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 break;
1414 }
1415 }
1416}
Yinghai Lu568ddef2010-01-22 01:02:21 -08001417
Bjorn Helgaas10874f52014-04-14 16:11:40 -06001418void pci_bus_assign_resources(const struct pci_bus *bus)
Yinghai Lu568ddef2010-01-22 01:02:21 -08001419{
Ram Paic8adf9a2011-02-14 17:43:20 -08001420 __pci_bus_assign_resources(bus, NULL, NULL);
Yinghai Lu568ddef2010-01-22 01:02:21 -08001421}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422EXPORT_SYMBOL(pci_bus_assign_resources);
1423
Lorenzo Pieralisi765bf9b2016-06-08 12:04:47 +01001424static void pci_claim_device_resources(struct pci_dev *dev)
1425{
1426 int i;
1427
1428 for (i = 0; i < PCI_BRIDGE_RESOURCES; i++) {
1429 struct resource *r = &dev->resource[i];
1430
1431 if (!r->flags || r->parent)
1432 continue;
1433
1434 pci_claim_resource(dev, i);
1435 }
1436}
1437
1438static void pci_claim_bridge_resources(struct pci_dev *dev)
1439{
1440 int i;
1441
1442 for (i = PCI_BRIDGE_RESOURCES; i < PCI_NUM_RESOURCES; i++) {
1443 struct resource *r = &dev->resource[i];
1444
1445 if (!r->flags || r->parent)
1446 continue;
1447
1448 pci_claim_bridge_resource(dev, i);
1449 }
1450}
1451
1452static void pci_bus_allocate_dev_resources(struct pci_bus *b)
1453{
1454 struct pci_dev *dev;
1455 struct pci_bus *child;
1456
1457 list_for_each_entry(dev, &b->devices, bus_list) {
1458 pci_claim_device_resources(dev);
1459
1460 child = dev->subordinate;
1461 if (child)
1462 pci_bus_allocate_dev_resources(child);
1463 }
1464}
1465
1466static void pci_bus_allocate_resources(struct pci_bus *b)
1467{
1468 struct pci_bus *child;
1469
1470 /*
1471 * Carry out a depth-first search on the PCI bus
1472 * tree to allocate bridge apertures. Read the
1473 * programmed bridge bases and recursively claim
1474 * the respective bridge resources.
1475 */
1476 if (b->self) {
1477 pci_read_bridge_bases(b);
1478 pci_claim_bridge_resources(b->self);
1479 }
1480
1481 list_for_each_entry(child, &b->children, node)
1482 pci_bus_allocate_resources(child);
1483}
1484
1485void pci_bus_claim_resources(struct pci_bus *b)
1486{
1487 pci_bus_allocate_resources(b);
1488 pci_bus_allocate_dev_resources(b);
1489}
1490EXPORT_SYMBOL(pci_bus_claim_resources);
1491
Bjorn Helgaas10874f52014-04-14 16:11:40 -06001492static void __pci_bridge_assign_resources(const struct pci_dev *bridge,
1493 struct list_head *add_head,
1494 struct list_head *fail_head)
Yinghai Lu6841ec62010-01-22 01:02:25 -08001495{
1496 struct pci_bus *b;
1497
Yinghai Lu8424d752012-01-21 02:08:21 -08001498 pdev_assign_resources_sorted((struct pci_dev *)bridge,
1499 add_head, fail_head);
Yinghai Lu6841ec62010-01-22 01:02:25 -08001500
1501 b = bridge->subordinate;
1502 if (!b)
1503 return;
1504
Yinghai Lu8424d752012-01-21 02:08:21 -08001505 __pci_bus_assign_resources(b, add_head, fail_head);
Yinghai Lu6841ec62010-01-22 01:02:25 -08001506
1507 switch (bridge->class >> 8) {
1508 case PCI_CLASS_BRIDGE_PCI:
1509 pci_setup_bridge(b);
1510 break;
1511
1512 case PCI_CLASS_BRIDGE_CARDBUS:
1513 pci_setup_cardbus(b);
1514 break;
1515
1516 default:
Ryan Desfosses227f0642014-04-18 20:13:50 -04001517 dev_info(&bridge->dev, "not setting up bridge for bus %04x:%02x\n",
1518 pci_domain_nr(b), b->number);
Yinghai Lu6841ec62010-01-22 01:02:25 -08001519 break;
1520 }
1521}
Christian Königcb21bc92017-10-18 15:58:17 +02001522
1523#define PCI_RES_TYPE_MASK \
1524 (IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH |\
1525 IORESOURCE_MEM_64)
1526
Yinghai Lu5009b462010-01-22 01:02:20 -08001527static void pci_bridge_release_resources(struct pci_bus *bus,
1528 unsigned long type)
1529{
Yinghai Lu5b285412014-05-19 17:01:55 -06001530 struct pci_dev *dev = bus->self;
Yinghai Lu5009b462010-01-22 01:02:20 -08001531 struct resource *r;
Yinghai Lu5b285412014-05-19 17:01:55 -06001532 unsigned old_flags = 0;
1533 struct resource *b_res;
1534 int idx = 1;
Yinghai Lu5009b462010-01-22 01:02:20 -08001535
Yinghai Lu5b285412014-05-19 17:01:55 -06001536 b_res = &dev->resource[PCI_BRIDGE_RESOURCES];
Yinghai Lu5009b462010-01-22 01:02:20 -08001537
Yinghai Lu5b285412014-05-19 17:01:55 -06001538 /*
1539 * 1. if there is io port assign fail, will release bridge
1540 * io port.
1541 * 2. if there is non pref mmio assign fail, release bridge
1542 * nonpref mmio.
1543 * 3. if there is 64bit pref mmio assign fail, and bridge pref
1544 * is 64bit, release bridge pref mmio.
1545 * 4. if there is pref mmio assign fail, and bridge pref is
1546 * 32bit mmio, release bridge pref mmio
1547 * 5. if there is pref mmio assign fail, and bridge pref is not
1548 * assigned, release bridge nonpref mmio.
1549 */
1550 if (type & IORESOURCE_IO)
1551 idx = 0;
1552 else if (!(type & IORESOURCE_PREFETCH))
1553 idx = 1;
1554 else if ((type & IORESOURCE_MEM_64) &&
1555 (b_res[2].flags & IORESOURCE_MEM_64))
1556 idx = 2;
1557 else if (!(b_res[2].flags & IORESOURCE_MEM_64) &&
1558 (b_res[2].flags & IORESOURCE_PREFETCH))
1559 idx = 2;
1560 else
1561 idx = 1;
1562
1563 r = &b_res[idx];
1564
1565 if (!r->parent)
1566 return;
1567
1568 /*
1569 * if there are children under that, we should release them
1570 * all
1571 */
1572 release_child_resources(r);
1573 if (!release_resource(r)) {
Christian Königcb21bc92017-10-18 15:58:17 +02001574 type = old_flags = r->flags & PCI_RES_TYPE_MASK;
Yinghai Lu5b285412014-05-19 17:01:55 -06001575 dev_printk(KERN_DEBUG, &dev->dev, "resource %d %pR released\n",
1576 PCI_BRIDGE_RESOURCES + idx, r);
1577 /* keep the old size */
1578 r->end = resource_size(r) - 1;
1579 r->start = 0;
1580 r->flags = 0;
1581
Yinghai Lu5009b462010-01-22 01:02:20 -08001582 /* avoiding touch the one without PREF */
1583 if (type & IORESOURCE_PREFETCH)
1584 type = IORESOURCE_PREFETCH;
1585 __pci_setup_bridge(bus, type);
Yinghai Lu5b285412014-05-19 17:01:55 -06001586 /* for next child res under same bridge */
1587 r->flags = old_flags;
Yinghai Lu5009b462010-01-22 01:02:20 -08001588 }
1589}
1590
1591enum release_type {
1592 leaf_only,
1593 whole_subtree,
1594};
1595/*
1596 * try to release pci bridge resources that is from leaf bridge,
1597 * so we can allocate big new one later
1598 */
Bjorn Helgaas10874f52014-04-14 16:11:40 -06001599static void pci_bus_release_bridge_resources(struct pci_bus *bus,
1600 unsigned long type,
1601 enum release_type rel_type)
Yinghai Lu5009b462010-01-22 01:02:20 -08001602{
1603 struct pci_dev *dev;
1604 bool is_leaf_bridge = true;
1605
1606 list_for_each_entry(dev, &bus->devices, bus_list) {
1607 struct pci_bus *b = dev->subordinate;
1608 if (!b)
1609 continue;
1610
1611 is_leaf_bridge = false;
1612
1613 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
1614 continue;
1615
1616 if (rel_type == whole_subtree)
1617 pci_bus_release_bridge_resources(b, type,
1618 whole_subtree);
1619 }
1620
1621 if (pci_is_root_bus(bus))
1622 return;
1623
1624 if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
1625 return;
1626
1627 if ((rel_type == whole_subtree) || is_leaf_bridge)
1628 pci_bridge_release_resources(bus, type);
1629}
1630
Yinghai Lu76fbc262008-06-23 20:33:06 +02001631static void pci_bus_dump_res(struct pci_bus *bus)
1632{
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -07001633 struct resource *res;
1634 int i;
Yinghai Lu76fbc262008-06-23 20:33:06 +02001635
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -07001636 pci_bus_for_each_resource(bus, res, i) {
Yinghai Lu7c9342b2009-12-22 15:02:24 -08001637 if (!res || !res->end || !res->flags)
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001638 continue;
Yinghai Lu76fbc262008-06-23 20:33:06 +02001639
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001640 dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res);
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001641 }
Yinghai Lu76fbc262008-06-23 20:33:06 +02001642}
1643
1644static void pci_bus_dump_resources(struct pci_bus *bus)
1645{
1646 struct pci_bus *b;
1647 struct pci_dev *dev;
1648
1649
1650 pci_bus_dump_res(bus);
1651
1652 list_for_each_entry(dev, &bus->devices, bus_list) {
1653 b = dev->subordinate;
1654 if (!b)
1655 continue;
1656
1657 pci_bus_dump_resources(b);
1658 }
1659}
1660
Yinghai Luff351472013-07-24 15:37:13 -06001661static int pci_bus_get_depth(struct pci_bus *bus)
Yinghai Luda7822e2011-05-12 17:11:37 -07001662{
1663 int depth = 0;
Wei Yangf2a230b2013-08-02 17:31:03 +08001664 struct pci_bus *child_bus;
Yinghai Luda7822e2011-05-12 17:11:37 -07001665
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001666 list_for_each_entry(child_bus, &bus->children, node) {
Yinghai Luda7822e2011-05-12 17:11:37 -07001667 int ret;
Yinghai Luda7822e2011-05-12 17:11:37 -07001668
Wei Yangf2a230b2013-08-02 17:31:03 +08001669 ret = pci_bus_get_depth(child_bus);
Yinghai Luda7822e2011-05-12 17:11:37 -07001670 if (ret + 1 > depth)
1671 depth = ret + 1;
1672 }
1673
1674 return depth;
1675}
Yinghai Luda7822e2011-05-12 17:11:37 -07001676
Yinghai Lub55438f2012-02-23 19:23:30 -08001677/*
1678 * -1: undefined, will auto detect later
1679 * 0: disabled by user
1680 * 1: disabled by auto detect
1681 * 2: enabled by user
1682 * 3: enabled by auto detect
1683 */
1684enum enable_type {
1685 undefined = -1,
1686 user_disabled,
1687 auto_disabled,
1688 user_enabled,
1689 auto_enabled,
1690};
1691
Yinghai Luff351472013-07-24 15:37:13 -06001692static enum enable_type pci_realloc_enable = undefined;
Yinghai Lub55438f2012-02-23 19:23:30 -08001693void __init pci_realloc_get_opt(char *str)
1694{
1695 if (!strncmp(str, "off", 3))
1696 pci_realloc_enable = user_disabled;
1697 else if (!strncmp(str, "on", 2))
1698 pci_realloc_enable = user_enabled;
1699}
Yinghai Luff351472013-07-24 15:37:13 -06001700static bool pci_realloc_enabled(enum enable_type enable)
Yinghai Lub55438f2012-02-23 19:23:30 -08001701{
Yinghai Lu967260c2013-07-22 14:37:15 -07001702 return enable >= user_enabled;
Yinghai Lub55438f2012-02-23 19:23:30 -08001703}
Ram Paif483d392011-07-07 11:19:10 -07001704
Yinghai Lub07f2eb2012-02-23 19:23:32 -08001705#if defined(CONFIG_PCI_IOV) && defined(CONFIG_PCI_REALLOC_ENABLE_AUTO)
Yinghai Luff351472013-07-24 15:37:13 -06001706static int iov_resources_unassigned(struct pci_dev *dev, void *data)
Yinghai Lu223d96f2013-07-22 14:37:13 -07001707{
1708 int i;
1709 bool *unassigned = data;
Yinghai Lub07f2eb2012-02-23 19:23:32 -08001710
Yinghai Lu223d96f2013-07-22 14:37:13 -07001711 for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++) {
1712 struct resource *r = &dev->resource[i];
Yinghai Lufa216bf2013-07-22 14:37:14 -07001713 struct pci_bus_region region;
Yinghai Lub07f2eb2012-02-23 19:23:32 -08001714
Yinghai Lu223d96f2013-07-22 14:37:13 -07001715 /* Not assigned or rejected by kernel? */
Yinghai Lufa216bf2013-07-22 14:37:14 -07001716 if (!r->flags)
1717 continue;
Yinghai Lub07f2eb2012-02-23 19:23:32 -08001718
Yinghai Lufc279852013-12-09 22:54:40 -08001719 pcibios_resource_to_bus(dev->bus, &region, r);
Yinghai Lufa216bf2013-07-22 14:37:14 -07001720 if (!region.start) {
Yinghai Lu223d96f2013-07-22 14:37:13 -07001721 *unassigned = true;
1722 return 1; /* return early from pci_walk_bus() */
Yinghai Lub07f2eb2012-02-23 19:23:32 -08001723 }
1724 }
Yinghai Lu223d96f2013-07-22 14:37:13 -07001725
1726 return 0;
Yinghai Lub07f2eb2012-02-23 19:23:32 -08001727}
1728
Yinghai Luff351472013-07-24 15:37:13 -06001729static enum enable_type pci_realloc_detect(struct pci_bus *bus,
Yinghai Lu967260c2013-07-22 14:37:15 -07001730 enum enable_type enable_local)
Yinghai Lu223d96f2013-07-22 14:37:13 -07001731{
1732 bool unassigned = false;
Yinghai Luda7822e2011-05-12 17:11:37 -07001733
Yinghai Lu967260c2013-07-22 14:37:15 -07001734 if (enable_local != undefined)
1735 return enable_local;
Yinghai Luda7822e2011-05-12 17:11:37 -07001736
Yinghai Lu967260c2013-07-22 14:37:15 -07001737 pci_walk_bus(bus, iov_resources_unassigned, &unassigned);
1738 if (unassigned)
1739 return auto_enabled;
1740
1741 return enable_local;
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001742}
Yinghai Lu223d96f2013-07-22 14:37:13 -07001743#else
Yinghai Luff351472013-07-24 15:37:13 -06001744static enum enable_type pci_realloc_detect(struct pci_bus *bus,
Yinghai Lu967260c2013-07-22 14:37:15 -07001745 enum enable_type enable_local)
1746{
1747 return enable_local;
1748}
Yinghai Lu223d96f2013-07-22 14:37:13 -07001749#endif
Yinghai Luda7822e2011-05-12 17:11:37 -07001750
1751/*
1752 * first try will not touch pci bridge res
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001753 * second and later try will clear small leaf bridge res
1754 * will stop till to the max depth if can not find good one
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 */
Yinghai Lu39772032013-07-22 14:37:18 -07001756void pci_assign_unassigned_root_bus_resources(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757{
Ram Paic8adf9a2011-02-14 17:43:20 -08001758 LIST_HEAD(realloc_head); /* list of resources that
Yinghai Luda7822e2011-05-12 17:11:37 -07001759 want additional resources */
1760 struct list_head *add_list = NULL;
1761 int tried_times = 0;
1762 enum release_type rel_type = leaf_only;
1763 LIST_HEAD(fail_head);
1764 struct pci_dev_resource *fail_res;
Yinghai Luda7822e2011-05-12 17:11:37 -07001765 int pci_try_num = 1;
Yinghai Lu55ed83a2013-07-22 14:37:16 -07001766 enum enable_type enable_local;
Yinghai Luda7822e2011-05-12 17:11:37 -07001767
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001768 /* don't realloc if asked to do so */
Yinghai Lu55ed83a2013-07-22 14:37:16 -07001769 enable_local = pci_realloc_detect(bus, pci_realloc_enable);
Yinghai Lu967260c2013-07-22 14:37:15 -07001770 if (pci_realloc_enabled(enable_local)) {
Yinghai Lu55ed83a2013-07-22 14:37:16 -07001771 int max_depth = pci_bus_get_depth(bus);
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001772
1773 pci_try_num = max_depth + 1;
Yinghai Lu55ed83a2013-07-22 14:37:16 -07001774 dev_printk(KERN_DEBUG, &bus->dev,
1775 "max bus depth: %d pci_try_num: %d\n",
1776 max_depth, pci_try_num);
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001777 }
Yinghai Luda7822e2011-05-12 17:11:37 -07001778
1779again:
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001780 /*
1781 * last try will use add_list, otherwise will try good to have as
1782 * must have, so can realloc parent bridge resource
1783 */
1784 if (tried_times + 1 == pci_try_num)
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001785 add_list = &realloc_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 /* Depth first, calculate sizes and alignments of all
1787 subordinate buses. */
Yinghai Lu55ed83a2013-07-22 14:37:16 -07001788 __pci_bus_size_bridges(bus, add_list);
Ram Paic8adf9a2011-02-14 17:43:20 -08001789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 /* Depth last, allocate resources and update the hardware. */
Yinghai Lu55ed83a2013-07-22 14:37:16 -07001791 __pci_bus_assign_resources(bus, add_list, &fail_head);
Yinghai Lu19aa7ee2012-01-21 02:08:24 -08001792 if (add_list)
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001793 BUG_ON(!list_empty(add_list));
Yinghai Luda7822e2011-05-12 17:11:37 -07001794 tried_times++;
1795
1796 /* any device complain? */
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001797 if (list_empty(&fail_head))
Yinghai Lu928bea92013-07-22 14:37:17 -07001798 goto dump;
Ram Paif483d392011-07-07 11:19:10 -07001799
Yinghai Lu0c5be0c2012-02-23 19:23:29 -08001800 if (tried_times >= pci_try_num) {
Yinghai Lu967260c2013-07-22 14:37:15 -07001801 if (enable_local == undefined)
Yinghai Lu55ed83a2013-07-22 14:37:16 -07001802 dev_info(&bus->dev, "Some PCI device resources are unassigned, try booting with pci=realloc\n");
Yinghai Lu967260c2013-07-22 14:37:15 -07001803 else if (enable_local == auto_enabled)
Yinghai Lu55ed83a2013-07-22 14:37:16 -07001804 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 -08001805
Yinghai Lubffc56d2012-01-21 02:08:30 -08001806 free_list(&fail_head);
Yinghai Lu928bea92013-07-22 14:37:17 -07001807 goto dump;
Yinghai Luda7822e2011-05-12 17:11:37 -07001808 }
1809
Yinghai Lu55ed83a2013-07-22 14:37:16 -07001810 dev_printk(KERN_DEBUG, &bus->dev,
1811 "No. %d try to assign unassigned res\n", tried_times + 1);
Yinghai Luda7822e2011-05-12 17:11:37 -07001812
1813 /* third times and later will not check if it is leaf */
1814 if ((tried_times + 1) > 2)
1815 rel_type = whole_subtree;
1816
1817 /*
1818 * Try to release leaf bridge's resources that doesn't fit resource of
1819 * child device under that bridge
1820 */
Yinghai Lu61e83cd2013-07-22 14:37:12 -07001821 list_for_each_entry(fail_res, &fail_head, list)
1822 pci_bus_release_bridge_resources(fail_res->dev->bus,
Christian Königcb21bc92017-10-18 15:58:17 +02001823 fail_res->flags & PCI_RES_TYPE_MASK,
Yinghai Lubdc4abe2012-01-21 02:08:27 -08001824 rel_type);
Yinghai Lu61e83cd2013-07-22 14:37:12 -07001825
Yinghai Luda7822e2011-05-12 17:11:37 -07001826 /* restore size and flags */
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001827 list_for_each_entry(fail_res, &fail_head, list) {
1828 struct resource *res = fail_res->res;
Yinghai Luda7822e2011-05-12 17:11:37 -07001829
Yinghai Lub9b0bba2012-01-21 02:08:29 -08001830 res->start = fail_res->start;
1831 res->end = fail_res->end;
1832 res->flags = fail_res->flags;
1833 if (fail_res->dev->subordinate)
Yinghai Luda7822e2011-05-12 17:11:37 -07001834 res->flags = 0;
Yinghai Luda7822e2011-05-12 17:11:37 -07001835 }
Yinghai Lubffc56d2012-01-21 02:08:30 -08001836 free_list(&fail_head);
Yinghai Luda7822e2011-05-12 17:11:37 -07001837
1838 goto again;
1839
Yinghai Lu928bea92013-07-22 14:37:17 -07001840dump:
Yinghai Lu76fbc262008-06-23 20:33:06 +02001841 /* dump the resource on buses */
Yinghai Lu55ed83a2013-07-22 14:37:16 -07001842 pci_bus_dump_resources(bus);
1843}
1844
1845void __init pci_assign_unassigned_resources(void)
1846{
1847 struct pci_bus *root_bus;
1848
Rui Wang584c5c42016-08-17 16:00:34 +08001849 list_for_each_entry(root_bus, &pci_root_buses, node) {
Yinghai Lu55ed83a2013-07-22 14:37:16 -07001850 pci_assign_unassigned_root_bus_resources(root_bus);
Rui Wangd9c149d2016-09-10 23:40:45 +08001851
1852 /* Make sure the root bridge has a companion ACPI device: */
1853 if (ACPI_HANDLE(root_bus->bridge))
1854 acpi_ioapic_add(ACPI_HANDLE(root_bus->bridge));
Rui Wang584c5c42016-08-17 16:00:34 +08001855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856}
Yinghai Lu6841ec62010-01-22 01:02:25 -08001857
Mika Westerberg1a576772017-10-13 21:35:45 +03001858static void extend_bridge_window(struct pci_dev *bridge, struct resource *res,
1859 struct list_head *add_list, resource_size_t available)
1860{
1861 struct pci_dev_resource *dev_res;
1862
1863 if (res->parent)
1864 return;
1865
1866 if (resource_size(res) >= available)
1867 return;
1868
1869 dev_res = res_to_dev_res(add_list, res);
1870 if (!dev_res)
1871 return;
1872
1873 /* Is there room to extend the window? */
1874 if (available - resource_size(res) <= dev_res->add_size)
1875 return;
1876
1877 dev_res->add_size = available - resource_size(res);
1878 dev_dbg(&bridge->dev, "bridge window %pR extended by %pa\n", res,
1879 &dev_res->add_size);
1880}
1881
1882static void pci_bus_distribute_available_resources(struct pci_bus *bus,
1883 struct list_head *add_list, resource_size_t available_io,
1884 resource_size_t available_mmio, resource_size_t available_mmio_pref)
1885{
1886 resource_size_t remaining_io, remaining_mmio, remaining_mmio_pref;
1887 unsigned int normal_bridges = 0, hotplug_bridges = 0;
1888 struct resource *io_res, *mmio_res, *mmio_pref_res;
1889 struct pci_dev *dev, *bridge = bus->self;
1890
1891 io_res = &bridge->resource[PCI_BRIDGE_RESOURCES + 0];
1892 mmio_res = &bridge->resource[PCI_BRIDGE_RESOURCES + 1];
1893 mmio_pref_res = &bridge->resource[PCI_BRIDGE_RESOURCES + 2];
1894
1895 /*
1896 * Update additional resource list (add_list) to fill all the
1897 * extra resource space available for this port except the space
1898 * calculated in __pci_bus_size_bridges() which covers all the
1899 * devices currently connected to the port and below.
1900 */
1901 extend_bridge_window(bridge, io_res, add_list, available_io);
1902 extend_bridge_window(bridge, mmio_res, add_list, available_mmio);
1903 extend_bridge_window(bridge, mmio_pref_res, add_list,
1904 available_mmio_pref);
1905
1906 /*
1907 * Calculate the total amount of extra resource space we can
1908 * pass to bridges below this one. This is basically the
1909 * extra space reduced by the minimal required space for the
1910 * non-hotplug bridges.
1911 */
1912 remaining_io = available_io;
1913 remaining_mmio = available_mmio;
1914 remaining_mmio_pref = available_mmio_pref;
1915
1916 /*
1917 * Calculate how many hotplug bridges and normal bridges there
1918 * are on this bus. We will distribute the additional available
1919 * resources between hotplug bridges.
1920 */
1921 for_each_pci_bridge(dev, bus) {
1922 if (dev->is_hotplug_bridge)
1923 hotplug_bridges++;
1924 else
1925 normal_bridges++;
1926 }
1927
1928 for_each_pci_bridge(dev, bus) {
1929 const struct resource *res;
1930
1931 if (dev->is_hotplug_bridge)
1932 continue;
1933
1934 /*
1935 * Reduce the available resource space by what the
1936 * bridge and devices below it occupy.
1937 */
1938 res = &dev->resource[PCI_BRIDGE_RESOURCES + 0];
1939 if (!res->parent && available_io > resource_size(res))
1940 remaining_io -= resource_size(res);
1941
1942 res = &dev->resource[PCI_BRIDGE_RESOURCES + 1];
1943 if (!res->parent && available_mmio > resource_size(res))
1944 remaining_mmio -= resource_size(res);
1945
1946 res = &dev->resource[PCI_BRIDGE_RESOURCES + 2];
1947 if (!res->parent && available_mmio_pref > resource_size(res))
1948 remaining_mmio_pref -= resource_size(res);
1949 }
1950
1951 /*
1952 * Go over devices on this bus and distribute the remaining
1953 * resource space between hotplug bridges.
1954 */
1955 for_each_pci_bridge(dev, bus) {
1956 struct pci_bus *b;
1957
1958 b = dev->subordinate;
1959 if (!b)
1960 continue;
1961
1962 if (!hotplug_bridges && normal_bridges == 1) {
1963 /*
1964 * There is only one bridge on the bus (upstream
1965 * port) so it gets all available resources
1966 * which it can then distribute to the possible
1967 * hotplug bridges below.
1968 */
1969 pci_bus_distribute_available_resources(b, add_list,
1970 available_io, available_mmio,
1971 available_mmio_pref);
1972 } else if (dev->is_hotplug_bridge) {
1973 resource_size_t align, io, mmio, mmio_pref;
1974
1975 /*
1976 * Distribute available extra resources equally
1977 * between hotplug-capable downstream ports
1978 * taking alignment into account.
1979 *
1980 * Here hotplug_bridges is always != 0.
1981 */
1982 align = pci_resource_alignment(bridge, io_res);
1983 io = div64_ul(available_io, hotplug_bridges);
1984 io = min(ALIGN(io, align), remaining_io);
1985 remaining_io -= io;
1986
1987 align = pci_resource_alignment(bridge, mmio_res);
1988 mmio = div64_ul(available_mmio, hotplug_bridges);
1989 mmio = min(ALIGN(mmio, align), remaining_mmio);
1990 remaining_mmio -= mmio;
1991
1992 align = pci_resource_alignment(bridge, mmio_pref_res);
1993 mmio_pref = div64_ul(available_mmio_pref,
1994 hotplug_bridges);
1995 mmio_pref = min(ALIGN(mmio_pref, align),
1996 remaining_mmio_pref);
1997 remaining_mmio_pref -= mmio_pref;
1998
1999 pci_bus_distribute_available_resources(b, add_list, io,
2000 mmio, mmio_pref);
2001 }
2002 }
2003}
2004
2005static void
2006pci_bridge_distribute_available_resources(struct pci_dev *bridge,
2007 struct list_head *add_list)
2008{
2009 resource_size_t available_io, available_mmio, available_mmio_pref;
2010 const struct resource *res;
2011
2012 if (!bridge->is_hotplug_bridge)
2013 return;
2014
2015 /* Take the initial extra resources from the hotplug port */
2016 res = &bridge->resource[PCI_BRIDGE_RESOURCES + 0];
2017 available_io = resource_size(res);
2018 res = &bridge->resource[PCI_BRIDGE_RESOURCES + 1];
2019 available_mmio = resource_size(res);
2020 res = &bridge->resource[PCI_BRIDGE_RESOURCES + 2];
2021 available_mmio_pref = resource_size(res);
2022
2023 pci_bus_distribute_available_resources(bridge->subordinate,
2024 add_list, available_io, available_mmio, available_mmio_pref);
2025}
2026
Yinghai Lu6841ec62010-01-22 01:02:25 -08002027void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
2028{
2029 struct pci_bus *parent = bridge->subordinate;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08002030 LIST_HEAD(add_list); /* list of resources that
Yinghai Lu8424d752012-01-21 02:08:21 -08002031 want additional resources */
Yinghai Lu32180e42010-01-22 01:02:27 -08002032 int tried_times = 0;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08002033 LIST_HEAD(fail_head);
Yinghai Lub9b0bba2012-01-21 02:08:29 -08002034 struct pci_dev_resource *fail_res;
Yinghai Lu6841ec62010-01-22 01:02:25 -08002035 int retval;
2036
Yinghai Lu32180e42010-01-22 01:02:27 -08002037again:
Yinghai Lu8424d752012-01-21 02:08:21 -08002038 __pci_bus_size_bridges(parent, &add_list);
Mika Westerberg1a576772017-10-13 21:35:45 +03002039
2040 /*
2041 * Distribute remaining resources (if any) equally between
2042 * hotplug bridges below. This makes it possible to extend the
2043 * hierarchy later without running out of resources.
2044 */
2045 pci_bridge_distribute_available_resources(bridge, &add_list);
2046
Yinghai Lubdc4abe2012-01-21 02:08:27 -08002047 __pci_bridge_assign_resources(bridge, &add_list, &fail_head);
2048 BUG_ON(!list_empty(&add_list));
Yinghai Lu32180e42010-01-22 01:02:27 -08002049 tried_times++;
2050
Yinghai Lubdc4abe2012-01-21 02:08:27 -08002051 if (list_empty(&fail_head))
Yinghai Lu3f579c32010-05-21 14:35:06 -07002052 goto enable_all;
Yinghai Lu32180e42010-01-22 01:02:27 -08002053
2054 if (tried_times >= 2) {
2055 /* still fail, don't need to try more */
Yinghai Lubffc56d2012-01-21 02:08:30 -08002056 free_list(&fail_head);
Yinghai Lu3f579c32010-05-21 14:35:06 -07002057 goto enable_all;
Yinghai Lu32180e42010-01-22 01:02:27 -08002058 }
2059
2060 printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
2061 tried_times + 1);
2062
2063 /*
2064 * Try to release leaf bridge's resources that doesn't fit resource of
2065 * child device under that bridge
2066 */
Yinghai Lu61e83cd2013-07-22 14:37:12 -07002067 list_for_each_entry(fail_res, &fail_head, list)
2068 pci_bus_release_bridge_resources(fail_res->dev->bus,
Christian Königcb21bc92017-10-18 15:58:17 +02002069 fail_res->flags & PCI_RES_TYPE_MASK,
Yinghai Lu32180e42010-01-22 01:02:27 -08002070 whole_subtree);
Yinghai Lu61e83cd2013-07-22 14:37:12 -07002071
Yinghai Lu32180e42010-01-22 01:02:27 -08002072 /* restore size and flags */
Yinghai Lub9b0bba2012-01-21 02:08:29 -08002073 list_for_each_entry(fail_res, &fail_head, list) {
2074 struct resource *res = fail_res->res;
Yinghai Lu32180e42010-01-22 01:02:27 -08002075
Yinghai Lub9b0bba2012-01-21 02:08:29 -08002076 res->start = fail_res->start;
2077 res->end = fail_res->end;
2078 res->flags = fail_res->flags;
2079 if (fail_res->dev->subordinate)
Yinghai Lu32180e42010-01-22 01:02:27 -08002080 res->flags = 0;
Yinghai Lu32180e42010-01-22 01:02:27 -08002081 }
Yinghai Lubffc56d2012-01-21 02:08:30 -08002082 free_list(&fail_head);
Yinghai Lu32180e42010-01-22 01:02:27 -08002083
2084 goto again;
Yinghai Lu3f579c32010-05-21 14:35:06 -07002085
2086enable_all:
2087 retval = pci_reenable_device(bridge);
Bjorn Helgaas9fc9eea2013-04-12 11:35:40 -06002088 if (retval)
2089 dev_err(&bridge->dev, "Error reenabling bridge (%d)\n", retval);
Yinghai Lu3f579c32010-05-21 14:35:06 -07002090 pci_set_master(bridge);
Yinghai Lu6841ec62010-01-22 01:02:25 -08002091}
2092EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
Yinghai Lu9b030882012-01-21 02:08:23 -08002093
Christian König8bb705e2017-10-24 14:40:26 -05002094int pci_reassign_bridge_resources(struct pci_dev *bridge, unsigned long type)
2095{
2096 struct pci_dev_resource *dev_res;
2097 struct pci_dev *next;
2098 LIST_HEAD(saved);
2099 LIST_HEAD(added);
2100 LIST_HEAD(failed);
2101 unsigned int i;
2102 int ret;
2103
2104 /* Walk to the root hub, releasing bridge BARs when possible */
2105 next = bridge;
2106 do {
2107 bridge = next;
2108 for (i = PCI_BRIDGE_RESOURCES; i < PCI_BRIDGE_RESOURCE_END;
2109 i++) {
2110 struct resource *res = &bridge->resource[i];
2111
2112 if ((res->flags ^ type) & PCI_RES_TYPE_MASK)
2113 continue;
2114
2115 /* Ignore BARs which are still in use */
2116 if (res->child)
2117 continue;
2118
2119 ret = add_to_list(&saved, bridge, res, 0, 0);
2120 if (ret)
2121 goto cleanup;
2122
2123 dev_info(&bridge->dev, "BAR %d: releasing %pR\n",
2124 i, res);
2125
2126 if (res->parent)
2127 release_resource(res);
2128 res->start = 0;
2129 res->end = 0;
2130 break;
2131 }
2132 if (i == PCI_BRIDGE_RESOURCE_END)
2133 break;
2134
2135 next = bridge->bus ? bridge->bus->self : NULL;
2136 } while (next);
2137
2138 if (list_empty(&saved))
2139 return -ENOENT;
2140
2141 __pci_bus_size_bridges(bridge->subordinate, &added);
2142 __pci_bridge_assign_resources(bridge, &added, &failed);
2143 BUG_ON(!list_empty(&added));
2144
2145 if (!list_empty(&failed)) {
2146 ret = -ENOSPC;
2147 goto cleanup;
2148 }
2149
2150 list_for_each_entry(dev_res, &saved, list) {
2151 /* Skip the bridge we just assigned resources for. */
2152 if (bridge == dev_res->dev)
2153 continue;
2154
2155 bridge = dev_res->dev;
2156 pci_setup_bridge(bridge->subordinate);
2157 }
2158
2159 free_list(&saved);
2160 return 0;
2161
2162cleanup:
2163 /* restore size and flags */
2164 list_for_each_entry(dev_res, &failed, list) {
2165 struct resource *res = dev_res->res;
2166
2167 res->start = dev_res->start;
2168 res->end = dev_res->end;
2169 res->flags = dev_res->flags;
2170 }
2171 free_list(&failed);
2172
2173 /* Revert to the old configuration */
2174 list_for_each_entry(dev_res, &saved, list) {
2175 struct resource *res = dev_res->res;
2176
2177 bridge = dev_res->dev;
2178 i = res - bridge->resource;
2179
2180 res->start = dev_res->start;
2181 res->end = dev_res->end;
2182 res->flags = dev_res->flags;
2183
2184 pci_claim_resource(bridge, i);
2185 pci_setup_bridge(bridge->subordinate);
2186 }
2187 free_list(&saved);
2188
2189 return ret;
2190}
2191
Yinghai Lu17787942012-10-30 14:31:10 -06002192void pci_assign_unassigned_bus_resources(struct pci_bus *bus)
Yinghai Lu9b030882012-01-21 02:08:23 -08002193{
Yinghai Lu9b030882012-01-21 02:08:23 -08002194 struct pci_dev *dev;
Yinghai Lubdc4abe2012-01-21 02:08:27 -08002195 LIST_HEAD(add_list); /* list of resources that
Yinghai Lu9b030882012-01-21 02:08:23 -08002196 want additional resources */
2197
Yinghai Lu9b030882012-01-21 02:08:23 -08002198 down_read(&pci_bus_sem);
Andy Shevchenko24a0c652017-10-20 15:38:54 -05002199 for_each_pci_bridge(dev, bus)
2200 if (pci_has_subordinate(dev))
2201 __pci_bus_size_bridges(dev->subordinate, &add_list);
Yinghai Lu9b030882012-01-21 02:08:23 -08002202 up_read(&pci_bus_sem);
2203 __pci_bus_assign_resources(bus, &add_list, NULL);
Yinghai Lubdc4abe2012-01-21 02:08:27 -08002204 BUG_ON(!list_empty(&add_list));
Yinghai Lu17787942012-10-30 14:31:10 -06002205}
Ray Juie6b29de2015-04-08 11:21:33 -07002206EXPORT_SYMBOL_GPL(pci_assign_unassigned_bus_resources);