setup-bus.c 51 KB

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