setup-bus.c 51 KB

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