setup-bus.c 50 KB

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