setup-bus.c 51 KB

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