setup-bus.c 57 KB

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