setup-bus.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202
  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. u16 io;
  633. u32 pmem;
  634. struct pci_dev *bridge = bus->self;
  635. struct resource *b_res;
  636. b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
  637. b_res[1].flags |= IORESOURCE_MEM;
  638. pci_read_config_word(bridge, PCI_IO_BASE, &io);
  639. if (!io) {
  640. pci_write_config_word(bridge, PCI_IO_BASE, 0xe0f0);
  641. pci_read_config_word(bridge, PCI_IO_BASE, &io);
  642. pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
  643. }
  644. if (io)
  645. b_res[0].flags |= IORESOURCE_IO;
  646. /* DECchip 21050 pass 2 errata: the bridge may miss an address
  647. disconnect boundary by one PCI data phase.
  648. Workaround: do not use prefetching on this device. */
  649. if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001)
  650. return;
  651. pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
  652. if (!pmem) {
  653. pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE,
  654. 0xffe0fff0);
  655. pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
  656. pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0);
  657. }
  658. if (pmem) {
  659. b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
  660. if ((pmem & PCI_PREF_RANGE_TYPE_MASK) ==
  661. PCI_PREF_RANGE_TYPE_64) {
  662. b_res[2].flags |= IORESOURCE_MEM_64;
  663. b_res[2].flags |= PCI_PREF_RANGE_TYPE_64;
  664. }
  665. }
  666. /* double check if bridge does support 64 bit pref */
  667. if (b_res[2].flags & IORESOURCE_MEM_64) {
  668. u32 mem_base_hi, tmp;
  669. pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32,
  670. &mem_base_hi);
  671. pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
  672. 0xffffffff);
  673. pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp);
  674. if (!tmp)
  675. b_res[2].flags &= ~IORESOURCE_MEM_64;
  676. pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
  677. mem_base_hi);
  678. }
  679. }
  680. /* Helper function for sizing routines: find first available
  681. bus resource of a given type. Note: we intentionally skip
  682. the bus resources which have already been assigned (that is,
  683. have non-NULL parent resource). */
  684. static struct resource *find_free_bus_resource(struct pci_bus *bus,
  685. unsigned long type_mask, unsigned long type)
  686. {
  687. int i;
  688. struct resource *r;
  689. pci_bus_for_each_resource(bus, r, i) {
  690. if (r == &ioport_resource || r == &iomem_resource)
  691. continue;
  692. if (r && (r->flags & type_mask) == type && !r->parent)
  693. return r;
  694. }
  695. return NULL;
  696. }
  697. static resource_size_t calculate_iosize(resource_size_t size,
  698. resource_size_t min_size,
  699. resource_size_t size1,
  700. resource_size_t add_size,
  701. resource_size_t children_add_size,
  702. resource_size_t old_size,
  703. resource_size_t align)
  704. {
  705. if (size < min_size)
  706. size = min_size;
  707. if (old_size == 1)
  708. old_size = 0;
  709. /* To be fixed in 2.5: we should have sort of HAVE_ISA
  710. flag in the struct pci_bus. */
  711. #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
  712. size = (size & 0xff) + ((size & ~0xffUL) << 2);
  713. #endif
  714. size = size + size1;
  715. if (size < old_size)
  716. size = old_size;
  717. size = ALIGN(max(size, add_size) + children_add_size, align);
  718. return size;
  719. }
  720. static resource_size_t calculate_memsize(resource_size_t size,
  721. resource_size_t min_size,
  722. resource_size_t add_size,
  723. resource_size_t children_add_size,
  724. resource_size_t old_size,
  725. resource_size_t align)
  726. {
  727. if (size < min_size)
  728. size = min_size;
  729. if (old_size == 1)
  730. old_size = 0;
  731. if (size < old_size)
  732. size = old_size;
  733. size = ALIGN(max(size, add_size) + children_add_size, align);
  734. return size;
  735. }
  736. resource_size_t __weak pcibios_window_alignment(struct pci_bus *bus,
  737. unsigned long type)
  738. {
  739. return 1;
  740. }
  741. #define PCI_P2P_DEFAULT_MEM_ALIGN 0x100000 /* 1MiB */
  742. #define PCI_P2P_DEFAULT_IO_ALIGN 0x1000 /* 4KiB */
  743. #define PCI_P2P_DEFAULT_IO_ALIGN_1K 0x400 /* 1KiB */
  744. static resource_size_t window_alignment(struct pci_bus *bus,
  745. unsigned long type)
  746. {
  747. resource_size_t align = 1, arch_align;
  748. if (type & IORESOURCE_MEM)
  749. align = PCI_P2P_DEFAULT_MEM_ALIGN;
  750. else if (type & IORESOURCE_IO) {
  751. /*
  752. * Per spec, I/O windows are 4K-aligned, but some
  753. * bridges have an extension to support 1K alignment.
  754. */
  755. if (bus->self->io_window_1k)
  756. align = PCI_P2P_DEFAULT_IO_ALIGN_1K;
  757. else
  758. align = PCI_P2P_DEFAULT_IO_ALIGN;
  759. }
  760. arch_align = pcibios_window_alignment(bus, type);
  761. return max(align, arch_align);
  762. }
  763. /**
  764. * pbus_size_io() - size the io window of a given bus
  765. *
  766. * @bus : the bus
  767. * @min_size : the minimum io window that must to be allocated
  768. * @add_size : additional optional io window
  769. * @realloc_head : track the additional io window on this list
  770. *
  771. * Sizing the IO windows of the PCI-PCI bridge is trivial,
  772. * since these windows have 1K or 4K granularity and the IO ranges
  773. * of non-bridge PCI devices are limited to 256 bytes.
  774. * We must be careful with the ISA aliasing though.
  775. */
  776. static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size,
  777. resource_size_t add_size, struct list_head *realloc_head)
  778. {
  779. struct pci_dev *dev;
  780. struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO,
  781. IORESOURCE_IO);
  782. resource_size_t size = 0, size0 = 0, size1 = 0;
  783. resource_size_t children_add_size = 0;
  784. resource_size_t min_align, align;
  785. if (!b_res)
  786. return;
  787. min_align = window_alignment(bus, IORESOURCE_IO);
  788. list_for_each_entry(dev, &bus->devices, bus_list) {
  789. int i;
  790. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  791. struct resource *r = &dev->resource[i];
  792. unsigned long r_size;
  793. if (r->parent || !(r->flags & IORESOURCE_IO))
  794. continue;
  795. r_size = resource_size(r);
  796. if (r_size < 0x400)
  797. /* Might be re-aligned for ISA */
  798. size += r_size;
  799. else
  800. size1 += r_size;
  801. align = pci_resource_alignment(dev, r);
  802. if (align > min_align)
  803. min_align = align;
  804. if (realloc_head)
  805. children_add_size += get_res_add_size(realloc_head, r);
  806. }
  807. }
  808. size0 = calculate_iosize(size, min_size, size1, 0, 0,
  809. resource_size(b_res), min_align);
  810. size1 = (!realloc_head || (realloc_head && !add_size && !children_add_size)) ? size0 :
  811. calculate_iosize(size, min_size, size1, add_size, children_add_size,
  812. resource_size(b_res), min_align);
  813. if (!size0 && !size1) {
  814. if (b_res->start || b_res->end)
  815. pci_info(bus->self, "disabling bridge window %pR to %pR (unused)\n",
  816. b_res, &bus->busn_res);
  817. b_res->flags = 0;
  818. return;
  819. }
  820. b_res->start = min_align;
  821. b_res->end = b_res->start + size0 - 1;
  822. b_res->flags |= IORESOURCE_STARTALIGN;
  823. if (size1 > size0 && realloc_head) {
  824. add_to_list(realloc_head, bus->self, b_res, size1-size0,
  825. min_align);
  826. pci_printk(KERN_DEBUG, bus->self, "bridge window %pR to %pR add_size %llx\n",
  827. b_res, &bus->busn_res,
  828. (unsigned long long)size1-size0);
  829. }
  830. }
  831. static inline resource_size_t calculate_mem_align(resource_size_t *aligns,
  832. int max_order)
  833. {
  834. resource_size_t align = 0;
  835. resource_size_t min_align = 0;
  836. int order;
  837. for (order = 0; order <= max_order; order++) {
  838. resource_size_t align1 = 1;
  839. align1 <<= (order + 20);
  840. if (!align)
  841. min_align = align1;
  842. else if (ALIGN(align + min_align, min_align) < align1)
  843. min_align = align1 >> 1;
  844. align += aligns[order];
  845. }
  846. return min_align;
  847. }
  848. /**
  849. * pbus_size_mem() - size the memory window of a given bus
  850. *
  851. * @bus : the bus
  852. * @mask: mask the resource flag, then compare it with type
  853. * @type: the type of free resource from bridge
  854. * @type2: second match type
  855. * @type3: third match type
  856. * @min_size : the minimum memory window that must to be allocated
  857. * @add_size : additional optional memory window
  858. * @realloc_head : track the additional memory window on this list
  859. *
  860. * Calculate the size of the bus and minimal alignment which
  861. * guarantees that all child resources fit in this size.
  862. *
  863. * Returns -ENOSPC if there's no available bus resource of the desired type.
  864. * Otherwise, sets the bus resource start/end to indicate the required
  865. * size, adds things to realloc_head (if supplied), and returns 0.
  866. */
  867. static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
  868. unsigned long type, unsigned long type2,
  869. unsigned long type3,
  870. resource_size_t min_size, resource_size_t add_size,
  871. struct list_head *realloc_head)
  872. {
  873. struct pci_dev *dev;
  874. resource_size_t min_align, align, size, size0, size1;
  875. resource_size_t aligns[18]; /* Alignments from 1Mb to 128Gb */
  876. int order, max_order;
  877. struct resource *b_res = find_free_bus_resource(bus,
  878. mask | IORESOURCE_PREFETCH, type);
  879. resource_size_t children_add_size = 0;
  880. resource_size_t children_add_align = 0;
  881. resource_size_t add_align = 0;
  882. if (!b_res)
  883. return -ENOSPC;
  884. memset(aligns, 0, sizeof(aligns));
  885. max_order = 0;
  886. size = 0;
  887. list_for_each_entry(dev, &bus->devices, bus_list) {
  888. int i;
  889. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  890. struct resource *r = &dev->resource[i];
  891. resource_size_t r_size;
  892. if (r->parent || (r->flags & IORESOURCE_PCI_FIXED) ||
  893. ((r->flags & mask) != type &&
  894. (r->flags & mask) != type2 &&
  895. (r->flags & mask) != type3))
  896. continue;
  897. r_size = resource_size(r);
  898. #ifdef CONFIG_PCI_IOV
  899. /* put SRIOV requested res to the optional list */
  900. if (realloc_head && i >= PCI_IOV_RESOURCES &&
  901. i <= PCI_IOV_RESOURCE_END) {
  902. add_align = max(pci_resource_alignment(dev, r), add_align);
  903. r->end = r->start - 1;
  904. add_to_list(realloc_head, dev, r, r_size, 0/* don't care */);
  905. children_add_size += r_size;
  906. continue;
  907. }
  908. #endif
  909. /*
  910. * aligns[0] is for 1MB (since bridge memory
  911. * windows are always at least 1MB aligned), so
  912. * keep "order" from being negative for smaller
  913. * resources.
  914. */
  915. align = pci_resource_alignment(dev, r);
  916. order = __ffs(align) - 20;
  917. if (order < 0)
  918. order = 0;
  919. if (order >= ARRAY_SIZE(aligns)) {
  920. pci_warn(dev, "disabling BAR %d: %pR (bad alignment %#llx)\n",
  921. i, r, (unsigned long long) align);
  922. r->flags = 0;
  923. continue;
  924. }
  925. size += max(r_size, align);
  926. /* Exclude ranges with size > align from
  927. calculation of the alignment. */
  928. if (r_size <= align)
  929. aligns[order] += align;
  930. if (order > max_order)
  931. max_order = order;
  932. if (realloc_head) {
  933. children_add_size += get_res_add_size(realloc_head, r);
  934. children_add_align = get_res_add_align(realloc_head, r);
  935. add_align = max(add_align, children_add_align);
  936. }
  937. }
  938. }
  939. min_align = calculate_mem_align(aligns, max_order);
  940. min_align = max(min_align, window_alignment(bus, b_res->flags));
  941. size0 = calculate_memsize(size, min_size, 0, 0, resource_size(b_res), min_align);
  942. add_align = max(min_align, add_align);
  943. size1 = (!realloc_head || (realloc_head && !add_size && !children_add_size)) ? size0 :
  944. calculate_memsize(size, min_size, add_size, children_add_size,
  945. resource_size(b_res), add_align);
  946. if (!size0 && !size1) {
  947. if (b_res->start || b_res->end)
  948. pci_info(bus->self, "disabling bridge window %pR to %pR (unused)\n",
  949. b_res, &bus->busn_res);
  950. b_res->flags = 0;
  951. return 0;
  952. }
  953. b_res->start = min_align;
  954. b_res->end = size0 + min_align - 1;
  955. b_res->flags |= IORESOURCE_STARTALIGN;
  956. if (size1 > size0 && realloc_head) {
  957. add_to_list(realloc_head, bus->self, b_res, size1-size0, add_align);
  958. pci_printk(KERN_DEBUG, bus->self, "bridge window %pR to %pR add_size %llx add_align %llx\n",
  959. b_res, &bus->busn_res,
  960. (unsigned long long) (size1 - size0),
  961. (unsigned long long) add_align);
  962. }
  963. return 0;
  964. }
  965. unsigned long pci_cardbus_resource_alignment(struct resource *res)
  966. {
  967. if (res->flags & IORESOURCE_IO)
  968. return pci_cardbus_io_size;
  969. if (res->flags & IORESOURCE_MEM)
  970. return pci_cardbus_mem_size;
  971. return 0;
  972. }
  973. static void pci_bus_size_cardbus(struct pci_bus *bus,
  974. struct list_head *realloc_head)
  975. {
  976. struct pci_dev *bridge = bus->self;
  977. struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
  978. resource_size_t b_res_3_size = pci_cardbus_mem_size * 2;
  979. u16 ctrl;
  980. if (b_res[0].parent)
  981. goto handle_b_res_1;
  982. /*
  983. * Reserve some resources for CardBus. We reserve
  984. * a fixed amount of bus space for CardBus bridges.
  985. */
  986. b_res[0].start = pci_cardbus_io_size;
  987. b_res[0].end = b_res[0].start + pci_cardbus_io_size - 1;
  988. b_res[0].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
  989. if (realloc_head) {
  990. b_res[0].end -= pci_cardbus_io_size;
  991. add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size,
  992. pci_cardbus_io_size);
  993. }
  994. handle_b_res_1:
  995. if (b_res[1].parent)
  996. goto handle_b_res_2;
  997. b_res[1].start = pci_cardbus_io_size;
  998. b_res[1].end = b_res[1].start + pci_cardbus_io_size - 1;
  999. b_res[1].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
  1000. if (realloc_head) {
  1001. b_res[1].end -= pci_cardbus_io_size;
  1002. add_to_list(realloc_head, bridge, b_res+1, pci_cardbus_io_size,
  1003. pci_cardbus_io_size);
  1004. }
  1005. handle_b_res_2:
  1006. /* MEM1 must not be pref mmio */
  1007. pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
  1008. if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM1) {
  1009. ctrl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1;
  1010. pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
  1011. pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
  1012. }
  1013. /*
  1014. * Check whether prefetchable memory is supported
  1015. * by this bridge.
  1016. */
  1017. pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
  1018. if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
  1019. ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
  1020. pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
  1021. pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
  1022. }
  1023. if (b_res[2].parent)
  1024. goto handle_b_res_3;
  1025. /*
  1026. * If we have prefetchable memory support, allocate
  1027. * two regions. Otherwise, allocate one region of
  1028. * twice the size.
  1029. */
  1030. if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
  1031. b_res[2].start = pci_cardbus_mem_size;
  1032. b_res[2].end = b_res[2].start + pci_cardbus_mem_size - 1;
  1033. b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH |
  1034. IORESOURCE_STARTALIGN;
  1035. if (realloc_head) {
  1036. b_res[2].end -= pci_cardbus_mem_size;
  1037. add_to_list(realloc_head, bridge, b_res+2,
  1038. pci_cardbus_mem_size, pci_cardbus_mem_size);
  1039. }
  1040. /* reduce that to half */
  1041. b_res_3_size = pci_cardbus_mem_size;
  1042. }
  1043. handle_b_res_3:
  1044. if (b_res[3].parent)
  1045. goto handle_done;
  1046. b_res[3].start = pci_cardbus_mem_size;
  1047. b_res[3].end = b_res[3].start + b_res_3_size - 1;
  1048. b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_STARTALIGN;
  1049. if (realloc_head) {
  1050. b_res[3].end -= b_res_3_size;
  1051. add_to_list(realloc_head, bridge, b_res+3, b_res_3_size,
  1052. pci_cardbus_mem_size);
  1053. }
  1054. handle_done:
  1055. ;
  1056. }
  1057. void __pci_bus_size_bridges(struct pci_bus *bus, struct list_head *realloc_head)
  1058. {
  1059. struct pci_dev *dev;
  1060. unsigned long mask, prefmask, type2 = 0, type3 = 0;
  1061. resource_size_t additional_mem_size = 0, additional_io_size = 0;
  1062. struct resource *b_res;
  1063. int ret;
  1064. list_for_each_entry(dev, &bus->devices, bus_list) {
  1065. struct pci_bus *b = dev->subordinate;
  1066. if (!b)
  1067. continue;
  1068. switch (dev->class >> 8) {
  1069. case PCI_CLASS_BRIDGE_CARDBUS:
  1070. pci_bus_size_cardbus(b, realloc_head);
  1071. break;
  1072. case PCI_CLASS_BRIDGE_PCI:
  1073. default:
  1074. __pci_bus_size_bridges(b, realloc_head);
  1075. break;
  1076. }
  1077. }
  1078. /* The root bus? */
  1079. if (pci_is_root_bus(bus))
  1080. return;
  1081. switch (bus->self->class >> 8) {
  1082. case PCI_CLASS_BRIDGE_CARDBUS:
  1083. /* don't size cardbuses yet. */
  1084. break;
  1085. case PCI_CLASS_BRIDGE_PCI:
  1086. pci_bridge_check_ranges(bus);
  1087. if (bus->self->is_hotplug_bridge) {
  1088. additional_io_size = pci_hotplug_io_size;
  1089. additional_mem_size = pci_hotplug_mem_size;
  1090. }
  1091. /* Fall through */
  1092. default:
  1093. pbus_size_io(bus, realloc_head ? 0 : additional_io_size,
  1094. additional_io_size, realloc_head);
  1095. /*
  1096. * If there's a 64-bit prefetchable MMIO window, compute
  1097. * the size required to put all 64-bit prefetchable
  1098. * resources in it.
  1099. */
  1100. b_res = &bus->self->resource[PCI_BRIDGE_RESOURCES];
  1101. mask = IORESOURCE_MEM;
  1102. prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
  1103. if (b_res[2].flags & IORESOURCE_MEM_64) {
  1104. prefmask |= IORESOURCE_MEM_64;
  1105. ret = pbus_size_mem(bus, prefmask, prefmask,
  1106. prefmask, prefmask,
  1107. realloc_head ? 0 : additional_mem_size,
  1108. additional_mem_size, realloc_head);
  1109. /*
  1110. * If successful, all non-prefetchable resources
  1111. * and any 32-bit prefetchable resources will go in
  1112. * the non-prefetchable window.
  1113. */
  1114. if (ret == 0) {
  1115. mask = prefmask;
  1116. type2 = prefmask & ~IORESOURCE_MEM_64;
  1117. type3 = prefmask & ~IORESOURCE_PREFETCH;
  1118. }
  1119. }
  1120. /*
  1121. * If there is no 64-bit prefetchable window, compute the
  1122. * size required to put all prefetchable resources in the
  1123. * 32-bit prefetchable window (if there is one).
  1124. */
  1125. if (!type2) {
  1126. prefmask &= ~IORESOURCE_MEM_64;
  1127. ret = pbus_size_mem(bus, prefmask, prefmask,
  1128. prefmask, prefmask,
  1129. realloc_head ? 0 : additional_mem_size,
  1130. additional_mem_size, realloc_head);
  1131. /*
  1132. * If successful, only non-prefetchable resources
  1133. * will go in the non-prefetchable window.
  1134. */
  1135. if (ret == 0)
  1136. mask = prefmask;
  1137. else
  1138. additional_mem_size += additional_mem_size;
  1139. type2 = type3 = IORESOURCE_MEM;
  1140. }
  1141. /*
  1142. * Compute the size required to put everything else in the
  1143. * non-prefetchable window. This includes:
  1144. *
  1145. * - all non-prefetchable resources
  1146. * - 32-bit prefetchable resources if there's a 64-bit
  1147. * prefetchable window or no prefetchable window at all
  1148. * - 64-bit prefetchable resources if there's no
  1149. * prefetchable window at all
  1150. *
  1151. * Note that the strategy in __pci_assign_resource() must
  1152. * match that used here. Specifically, we cannot put a
  1153. * 32-bit prefetchable resource in a 64-bit prefetchable
  1154. * window.
  1155. */
  1156. pbus_size_mem(bus, mask, IORESOURCE_MEM, type2, type3,
  1157. realloc_head ? 0 : additional_mem_size,
  1158. additional_mem_size, realloc_head);
  1159. break;
  1160. }
  1161. }
  1162. void pci_bus_size_bridges(struct pci_bus *bus)
  1163. {
  1164. __pci_bus_size_bridges(bus, NULL);
  1165. }
  1166. EXPORT_SYMBOL(pci_bus_size_bridges);
  1167. static void assign_fixed_resource_on_bus(struct pci_bus *b, struct resource *r)
  1168. {
  1169. int i;
  1170. struct resource *parent_r;
  1171. unsigned long mask = IORESOURCE_IO | IORESOURCE_MEM |
  1172. IORESOURCE_PREFETCH;
  1173. pci_bus_for_each_resource(b, parent_r, i) {
  1174. if (!parent_r)
  1175. continue;
  1176. if ((r->flags & mask) == (parent_r->flags & mask) &&
  1177. resource_contains(parent_r, r))
  1178. request_resource(parent_r, r);
  1179. }
  1180. }
  1181. /*
  1182. * Try to assign any resources marked as IORESOURCE_PCI_FIXED, as they
  1183. * are skipped by pbus_assign_resources_sorted().
  1184. */
  1185. static void pdev_assign_fixed_resources(struct pci_dev *dev)
  1186. {
  1187. int i;
  1188. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  1189. struct pci_bus *b;
  1190. struct resource *r = &dev->resource[i];
  1191. if (r->parent || !(r->flags & IORESOURCE_PCI_FIXED) ||
  1192. !(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
  1193. continue;
  1194. b = dev->bus;
  1195. while (b && !r->parent) {
  1196. assign_fixed_resource_on_bus(b, r);
  1197. b = b->parent;
  1198. }
  1199. }
  1200. }
  1201. void __pci_bus_assign_resources(const struct pci_bus *bus,
  1202. struct list_head *realloc_head,
  1203. struct list_head *fail_head)
  1204. {
  1205. struct pci_bus *b;
  1206. struct pci_dev *dev;
  1207. pbus_assign_resources_sorted(bus, realloc_head, fail_head);
  1208. list_for_each_entry(dev, &bus->devices, bus_list) {
  1209. pdev_assign_fixed_resources(dev);
  1210. b = dev->subordinate;
  1211. if (!b)
  1212. continue;
  1213. __pci_bus_assign_resources(b, realloc_head, fail_head);
  1214. switch (dev->class >> 8) {
  1215. case PCI_CLASS_BRIDGE_PCI:
  1216. if (!pci_is_enabled(dev))
  1217. pci_setup_bridge(b);
  1218. break;
  1219. case PCI_CLASS_BRIDGE_CARDBUS:
  1220. pci_setup_cardbus(b);
  1221. break;
  1222. default:
  1223. pci_info(dev, "not setting up bridge for bus %04x:%02x\n",
  1224. pci_domain_nr(b), b->number);
  1225. break;
  1226. }
  1227. }
  1228. }
  1229. void pci_bus_assign_resources(const struct pci_bus *bus)
  1230. {
  1231. __pci_bus_assign_resources(bus, NULL, NULL);
  1232. }
  1233. EXPORT_SYMBOL(pci_bus_assign_resources);
  1234. static void pci_claim_device_resources(struct pci_dev *dev)
  1235. {
  1236. int i;
  1237. for (i = 0; i < PCI_BRIDGE_RESOURCES; i++) {
  1238. struct resource *r = &dev->resource[i];
  1239. if (!r->flags || r->parent)
  1240. continue;
  1241. pci_claim_resource(dev, i);
  1242. }
  1243. }
  1244. static void pci_claim_bridge_resources(struct pci_dev *dev)
  1245. {
  1246. int i;
  1247. for (i = PCI_BRIDGE_RESOURCES; i < PCI_NUM_RESOURCES; i++) {
  1248. struct resource *r = &dev->resource[i];
  1249. if (!r->flags || r->parent)
  1250. continue;
  1251. pci_claim_bridge_resource(dev, i);
  1252. }
  1253. }
  1254. static void pci_bus_allocate_dev_resources(struct pci_bus *b)
  1255. {
  1256. struct pci_dev *dev;
  1257. struct pci_bus *child;
  1258. list_for_each_entry(dev, &b->devices, bus_list) {
  1259. pci_claim_device_resources(dev);
  1260. child = dev->subordinate;
  1261. if (child)
  1262. pci_bus_allocate_dev_resources(child);
  1263. }
  1264. }
  1265. static void pci_bus_allocate_resources(struct pci_bus *b)
  1266. {
  1267. struct pci_bus *child;
  1268. /*
  1269. * Carry out a depth-first search on the PCI bus
  1270. * tree to allocate bridge apertures. Read the
  1271. * programmed bridge bases and recursively claim
  1272. * the respective bridge resources.
  1273. */
  1274. if (b->self) {
  1275. pci_read_bridge_bases(b);
  1276. pci_claim_bridge_resources(b->self);
  1277. }
  1278. list_for_each_entry(child, &b->children, node)
  1279. pci_bus_allocate_resources(child);
  1280. }
  1281. void pci_bus_claim_resources(struct pci_bus *b)
  1282. {
  1283. pci_bus_allocate_resources(b);
  1284. pci_bus_allocate_dev_resources(b);
  1285. }
  1286. EXPORT_SYMBOL(pci_bus_claim_resources);
  1287. static void __pci_bridge_assign_resources(const struct pci_dev *bridge,
  1288. struct list_head *add_head,
  1289. struct list_head *fail_head)
  1290. {
  1291. struct pci_bus *b;
  1292. pdev_assign_resources_sorted((struct pci_dev *)bridge,
  1293. add_head, fail_head);
  1294. b = bridge->subordinate;
  1295. if (!b)
  1296. return;
  1297. __pci_bus_assign_resources(b, add_head, fail_head);
  1298. switch (bridge->class >> 8) {
  1299. case PCI_CLASS_BRIDGE_PCI:
  1300. pci_setup_bridge(b);
  1301. break;
  1302. case PCI_CLASS_BRIDGE_CARDBUS:
  1303. pci_setup_cardbus(b);
  1304. break;
  1305. default:
  1306. pci_info(bridge, "not setting up bridge for bus %04x:%02x\n",
  1307. pci_domain_nr(b), b->number);
  1308. break;
  1309. }
  1310. }
  1311. #define PCI_RES_TYPE_MASK \
  1312. (IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH |\
  1313. IORESOURCE_MEM_64)
  1314. static void pci_bridge_release_resources(struct pci_bus *bus,
  1315. unsigned long type)
  1316. {
  1317. struct pci_dev *dev = bus->self;
  1318. struct resource *r;
  1319. unsigned old_flags = 0;
  1320. struct resource *b_res;
  1321. int idx = 1;
  1322. b_res = &dev->resource[PCI_BRIDGE_RESOURCES];
  1323. /*
  1324. * 1. if there is io port assign fail, will release bridge
  1325. * io port.
  1326. * 2. if there is non pref mmio assign fail, release bridge
  1327. * nonpref mmio.
  1328. * 3. if there is 64bit pref mmio assign fail, and bridge pref
  1329. * is 64bit, release bridge pref mmio.
  1330. * 4. if there is pref mmio assign fail, and bridge pref is
  1331. * 32bit mmio, release bridge pref mmio
  1332. * 5. if there is pref mmio assign fail, and bridge pref is not
  1333. * assigned, release bridge nonpref mmio.
  1334. */
  1335. if (type & IORESOURCE_IO)
  1336. idx = 0;
  1337. else if (!(type & IORESOURCE_PREFETCH))
  1338. idx = 1;
  1339. else if ((type & IORESOURCE_MEM_64) &&
  1340. (b_res[2].flags & IORESOURCE_MEM_64))
  1341. idx = 2;
  1342. else if (!(b_res[2].flags & IORESOURCE_MEM_64) &&
  1343. (b_res[2].flags & IORESOURCE_PREFETCH))
  1344. idx = 2;
  1345. else
  1346. idx = 1;
  1347. r = &b_res[idx];
  1348. if (!r->parent)
  1349. return;
  1350. /*
  1351. * if there are children under that, we should release them
  1352. * all
  1353. */
  1354. release_child_resources(r);
  1355. if (!release_resource(r)) {
  1356. type = old_flags = r->flags & PCI_RES_TYPE_MASK;
  1357. pci_printk(KERN_DEBUG, dev, "resource %d %pR released\n",
  1358. PCI_BRIDGE_RESOURCES + idx, r);
  1359. /* keep the old size */
  1360. r->end = resource_size(r) - 1;
  1361. r->start = 0;
  1362. r->flags = 0;
  1363. /* avoiding touch the one without PREF */
  1364. if (type & IORESOURCE_PREFETCH)
  1365. type = IORESOURCE_PREFETCH;
  1366. __pci_setup_bridge(bus, type);
  1367. /* for next child res under same bridge */
  1368. r->flags = old_flags;
  1369. }
  1370. }
  1371. enum release_type {
  1372. leaf_only,
  1373. whole_subtree,
  1374. };
  1375. /*
  1376. * try to release pci bridge resources that is from leaf bridge,
  1377. * so we can allocate big new one later
  1378. */
  1379. static void pci_bus_release_bridge_resources(struct pci_bus *bus,
  1380. unsigned long type,
  1381. enum release_type rel_type)
  1382. {
  1383. struct pci_dev *dev;
  1384. bool is_leaf_bridge = true;
  1385. list_for_each_entry(dev, &bus->devices, bus_list) {
  1386. struct pci_bus *b = dev->subordinate;
  1387. if (!b)
  1388. continue;
  1389. is_leaf_bridge = false;
  1390. if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  1391. continue;
  1392. if (rel_type == whole_subtree)
  1393. pci_bus_release_bridge_resources(b, type,
  1394. whole_subtree);
  1395. }
  1396. if (pci_is_root_bus(bus))
  1397. return;
  1398. if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  1399. return;
  1400. if ((rel_type == whole_subtree) || is_leaf_bridge)
  1401. pci_bridge_release_resources(bus, type);
  1402. }
  1403. static void pci_bus_dump_res(struct pci_bus *bus)
  1404. {
  1405. struct resource *res;
  1406. int i;
  1407. pci_bus_for_each_resource(bus, res, i) {
  1408. if (!res || !res->end || !res->flags)
  1409. continue;
  1410. dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res);
  1411. }
  1412. }
  1413. static void pci_bus_dump_resources(struct pci_bus *bus)
  1414. {
  1415. struct pci_bus *b;
  1416. struct pci_dev *dev;
  1417. pci_bus_dump_res(bus);
  1418. list_for_each_entry(dev, &bus->devices, bus_list) {
  1419. b = dev->subordinate;
  1420. if (!b)
  1421. continue;
  1422. pci_bus_dump_resources(b);
  1423. }
  1424. }
  1425. static int pci_bus_get_depth(struct pci_bus *bus)
  1426. {
  1427. int depth = 0;
  1428. struct pci_bus *child_bus;
  1429. list_for_each_entry(child_bus, &bus->children, node) {
  1430. int ret;
  1431. ret = pci_bus_get_depth(child_bus);
  1432. if (ret + 1 > depth)
  1433. depth = ret + 1;
  1434. }
  1435. return depth;
  1436. }
  1437. /*
  1438. * -1: undefined, will auto detect later
  1439. * 0: disabled by user
  1440. * 1: disabled by auto detect
  1441. * 2: enabled by user
  1442. * 3: enabled by auto detect
  1443. */
  1444. enum enable_type {
  1445. undefined = -1,
  1446. user_disabled,
  1447. auto_disabled,
  1448. user_enabled,
  1449. auto_enabled,
  1450. };
  1451. static enum enable_type pci_realloc_enable = undefined;
  1452. void __init pci_realloc_get_opt(char *str)
  1453. {
  1454. if (!strncmp(str, "off", 3))
  1455. pci_realloc_enable = user_disabled;
  1456. else if (!strncmp(str, "on", 2))
  1457. pci_realloc_enable = user_enabled;
  1458. }
  1459. static bool pci_realloc_enabled(enum enable_type enable)
  1460. {
  1461. return enable >= user_enabled;
  1462. }
  1463. #if defined(CONFIG_PCI_IOV) && defined(CONFIG_PCI_REALLOC_ENABLE_AUTO)
  1464. static int iov_resources_unassigned(struct pci_dev *dev, void *data)
  1465. {
  1466. int i;
  1467. bool *unassigned = data;
  1468. for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++) {
  1469. struct resource *r = &dev->resource[i];
  1470. struct pci_bus_region region;
  1471. /* Not assigned or rejected by kernel? */
  1472. if (!r->flags)
  1473. continue;
  1474. pcibios_resource_to_bus(dev->bus, &region, r);
  1475. if (!region.start) {
  1476. *unassigned = true;
  1477. return 1; /* return early from pci_walk_bus() */
  1478. }
  1479. }
  1480. return 0;
  1481. }
  1482. static enum enable_type pci_realloc_detect(struct pci_bus *bus,
  1483. enum enable_type enable_local)
  1484. {
  1485. bool unassigned = false;
  1486. if (enable_local != undefined)
  1487. return enable_local;
  1488. pci_walk_bus(bus, iov_resources_unassigned, &unassigned);
  1489. if (unassigned)
  1490. return auto_enabled;
  1491. return enable_local;
  1492. }
  1493. #else
  1494. static enum enable_type pci_realloc_detect(struct pci_bus *bus,
  1495. enum enable_type enable_local)
  1496. {
  1497. return enable_local;
  1498. }
  1499. #endif
  1500. /*
  1501. * first try will not touch pci bridge res
  1502. * second and later try will clear small leaf bridge res
  1503. * will stop till to the max depth if can not find good one
  1504. */
  1505. void pci_assign_unassigned_root_bus_resources(struct pci_bus *bus)
  1506. {
  1507. LIST_HEAD(realloc_head); /* list of resources that
  1508. want additional resources */
  1509. struct list_head *add_list = NULL;
  1510. int tried_times = 0;
  1511. enum release_type rel_type = leaf_only;
  1512. LIST_HEAD(fail_head);
  1513. struct pci_dev_resource *fail_res;
  1514. int pci_try_num = 1;
  1515. enum enable_type enable_local;
  1516. /* don't realloc if asked to do so */
  1517. enable_local = pci_realloc_detect(bus, pci_realloc_enable);
  1518. if (pci_realloc_enabled(enable_local)) {
  1519. int max_depth = pci_bus_get_depth(bus);
  1520. pci_try_num = max_depth + 1;
  1521. dev_printk(KERN_DEBUG, &bus->dev,
  1522. "max bus depth: %d pci_try_num: %d\n",
  1523. max_depth, pci_try_num);
  1524. }
  1525. again:
  1526. /*
  1527. * last try will use add_list, otherwise will try good to have as
  1528. * must have, so can realloc parent bridge resource
  1529. */
  1530. if (tried_times + 1 == pci_try_num)
  1531. add_list = &realloc_head;
  1532. /* Depth first, calculate sizes and alignments of all
  1533. subordinate buses. */
  1534. __pci_bus_size_bridges(bus, add_list);
  1535. /* Depth last, allocate resources and update the hardware. */
  1536. __pci_bus_assign_resources(bus, add_list, &fail_head);
  1537. if (add_list)
  1538. BUG_ON(!list_empty(add_list));
  1539. tried_times++;
  1540. /* any device complain? */
  1541. if (list_empty(&fail_head))
  1542. goto dump;
  1543. if (tried_times >= pci_try_num) {
  1544. if (enable_local == undefined)
  1545. dev_info(&bus->dev, "Some PCI device resources are unassigned, try booting with pci=realloc\n");
  1546. else if (enable_local == auto_enabled)
  1547. dev_info(&bus->dev, "Automatically enabled pci realloc, if you have problem, try booting with pci=realloc=off\n");
  1548. free_list(&fail_head);
  1549. goto dump;
  1550. }
  1551. dev_printk(KERN_DEBUG, &bus->dev,
  1552. "No. %d try to assign unassigned res\n", tried_times + 1);
  1553. /* third times and later will not check if it is leaf */
  1554. if ((tried_times + 1) > 2)
  1555. rel_type = whole_subtree;
  1556. /*
  1557. * Try to release leaf bridge's resources that doesn't fit resource of
  1558. * child device under that bridge
  1559. */
  1560. list_for_each_entry(fail_res, &fail_head, list)
  1561. pci_bus_release_bridge_resources(fail_res->dev->bus,
  1562. fail_res->flags & PCI_RES_TYPE_MASK,
  1563. rel_type);
  1564. /* restore size and flags */
  1565. list_for_each_entry(fail_res, &fail_head, list) {
  1566. struct resource *res = fail_res->res;
  1567. res->start = fail_res->start;
  1568. res->end = fail_res->end;
  1569. res->flags = fail_res->flags;
  1570. if (fail_res->dev->subordinate)
  1571. res->flags = 0;
  1572. }
  1573. free_list(&fail_head);
  1574. goto again;
  1575. dump:
  1576. /* dump the resource on buses */
  1577. pci_bus_dump_resources(bus);
  1578. }
  1579. void __init pci_assign_unassigned_resources(void)
  1580. {
  1581. struct pci_bus *root_bus;
  1582. list_for_each_entry(root_bus, &pci_root_buses, node) {
  1583. pci_assign_unassigned_root_bus_resources(root_bus);
  1584. /* Make sure the root bridge has a companion ACPI device: */
  1585. if (ACPI_HANDLE(root_bus->bridge))
  1586. acpi_ioapic_add(ACPI_HANDLE(root_bus->bridge));
  1587. }
  1588. }
  1589. static void extend_bridge_window(struct pci_dev *bridge, struct resource *res,
  1590. struct list_head *add_list, resource_size_t available)
  1591. {
  1592. struct pci_dev_resource *dev_res;
  1593. if (res->parent)
  1594. return;
  1595. if (resource_size(res) >= available)
  1596. return;
  1597. dev_res = res_to_dev_res(add_list, res);
  1598. if (!dev_res)
  1599. return;
  1600. /* Is there room to extend the window? */
  1601. if (available - resource_size(res) <= dev_res->add_size)
  1602. return;
  1603. dev_res->add_size = available - resource_size(res);
  1604. pci_dbg(bridge, "bridge window %pR extended by %pa\n", res,
  1605. &dev_res->add_size);
  1606. }
  1607. static void pci_bus_distribute_available_resources(struct pci_bus *bus,
  1608. struct list_head *add_list, resource_size_t available_io,
  1609. resource_size_t available_mmio, resource_size_t available_mmio_pref)
  1610. {
  1611. resource_size_t remaining_io, remaining_mmio, remaining_mmio_pref;
  1612. unsigned int normal_bridges = 0, hotplug_bridges = 0;
  1613. struct resource *io_res, *mmio_res, *mmio_pref_res;
  1614. struct pci_dev *dev, *bridge = bus->self;
  1615. io_res = &bridge->resource[PCI_BRIDGE_RESOURCES + 0];
  1616. mmio_res = &bridge->resource[PCI_BRIDGE_RESOURCES + 1];
  1617. mmio_pref_res = &bridge->resource[PCI_BRIDGE_RESOURCES + 2];
  1618. /*
  1619. * Update additional resource list (add_list) to fill all the
  1620. * extra resource space available for this port except the space
  1621. * calculated in __pci_bus_size_bridges() which covers all the
  1622. * devices currently connected to the port and below.
  1623. */
  1624. extend_bridge_window(bridge, io_res, add_list, available_io);
  1625. extend_bridge_window(bridge, mmio_res, add_list, available_mmio);
  1626. extend_bridge_window(bridge, mmio_pref_res, add_list,
  1627. available_mmio_pref);
  1628. /*
  1629. * Calculate the total amount of extra resource space we can
  1630. * pass to bridges below this one. This is basically the
  1631. * extra space reduced by the minimal required space for the
  1632. * non-hotplug bridges.
  1633. */
  1634. remaining_io = available_io;
  1635. remaining_mmio = available_mmio;
  1636. remaining_mmio_pref = available_mmio_pref;
  1637. /*
  1638. * Calculate how many hotplug bridges and normal bridges there
  1639. * are on this bus. We will distribute the additional available
  1640. * resources between hotplug bridges.
  1641. */
  1642. for_each_pci_bridge(dev, bus) {
  1643. if (dev->is_hotplug_bridge)
  1644. hotplug_bridges++;
  1645. else
  1646. normal_bridges++;
  1647. }
  1648. for_each_pci_bridge(dev, bus) {
  1649. const struct resource *res;
  1650. if (dev->is_hotplug_bridge)
  1651. continue;
  1652. /*
  1653. * Reduce the available resource space by what the
  1654. * bridge and devices below it occupy.
  1655. */
  1656. res = &dev->resource[PCI_BRIDGE_RESOURCES + 0];
  1657. if (!res->parent && available_io > resource_size(res))
  1658. remaining_io -= resource_size(res);
  1659. res = &dev->resource[PCI_BRIDGE_RESOURCES + 1];
  1660. if (!res->parent && available_mmio > resource_size(res))
  1661. remaining_mmio -= resource_size(res);
  1662. res = &dev->resource[PCI_BRIDGE_RESOURCES + 2];
  1663. if (!res->parent && available_mmio_pref > resource_size(res))
  1664. remaining_mmio_pref -= resource_size(res);
  1665. }
  1666. /*
  1667. * There is only one bridge on the bus so it gets all available
  1668. * resources which it can then distribute to the possible
  1669. * hotplug bridges below.
  1670. */
  1671. if (hotplug_bridges + normal_bridges == 1) {
  1672. dev = list_first_entry(&bus->devices, struct pci_dev, bus_list);
  1673. if (dev->subordinate) {
  1674. pci_bus_distribute_available_resources(dev->subordinate,
  1675. add_list, available_io, available_mmio,
  1676. available_mmio_pref);
  1677. }
  1678. return;
  1679. }
  1680. /*
  1681. * Go over devices on this bus and distribute the remaining
  1682. * resource space between hotplug bridges.
  1683. */
  1684. for_each_pci_bridge(dev, bus) {
  1685. resource_size_t align, io, mmio, mmio_pref;
  1686. struct pci_bus *b;
  1687. b = dev->subordinate;
  1688. if (!b || !dev->is_hotplug_bridge)
  1689. continue;
  1690. /*
  1691. * Distribute available extra resources equally between
  1692. * hotplug-capable downstream ports taking alignment into
  1693. * account.
  1694. *
  1695. * Here hotplug_bridges is always != 0.
  1696. */
  1697. align = pci_resource_alignment(bridge, io_res);
  1698. io = div64_ul(available_io, hotplug_bridges);
  1699. io = min(ALIGN(io, align), remaining_io);
  1700. remaining_io -= io;
  1701. align = pci_resource_alignment(bridge, mmio_res);
  1702. mmio = div64_ul(available_mmio, hotplug_bridges);
  1703. mmio = min(ALIGN(mmio, align), remaining_mmio);
  1704. remaining_mmio -= mmio;
  1705. align = pci_resource_alignment(bridge, mmio_pref_res);
  1706. mmio_pref = div64_ul(available_mmio_pref, hotplug_bridges);
  1707. mmio_pref = min(ALIGN(mmio_pref, align), remaining_mmio_pref);
  1708. remaining_mmio_pref -= mmio_pref;
  1709. pci_bus_distribute_available_resources(b, add_list, io, mmio,
  1710. mmio_pref);
  1711. }
  1712. }
  1713. static void
  1714. pci_bridge_distribute_available_resources(struct pci_dev *bridge,
  1715. struct list_head *add_list)
  1716. {
  1717. resource_size_t available_io, available_mmio, available_mmio_pref;
  1718. const struct resource *res;
  1719. if (!bridge->is_hotplug_bridge)
  1720. return;
  1721. /* Take the initial extra resources from the hotplug port */
  1722. res = &bridge->resource[PCI_BRIDGE_RESOURCES + 0];
  1723. available_io = resource_size(res);
  1724. res = &bridge->resource[PCI_BRIDGE_RESOURCES + 1];
  1725. available_mmio = resource_size(res);
  1726. res = &bridge->resource[PCI_BRIDGE_RESOURCES + 2];
  1727. available_mmio_pref = resource_size(res);
  1728. pci_bus_distribute_available_resources(bridge->subordinate,
  1729. add_list, available_io, available_mmio, available_mmio_pref);
  1730. }
  1731. void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
  1732. {
  1733. struct pci_bus *parent = bridge->subordinate;
  1734. LIST_HEAD(add_list); /* list of resources that
  1735. want additional resources */
  1736. int tried_times = 0;
  1737. LIST_HEAD(fail_head);
  1738. struct pci_dev_resource *fail_res;
  1739. int retval;
  1740. again:
  1741. __pci_bus_size_bridges(parent, &add_list);
  1742. /*
  1743. * Distribute remaining resources (if any) equally between
  1744. * hotplug bridges below. This makes it possible to extend the
  1745. * hierarchy later without running out of resources.
  1746. */
  1747. pci_bridge_distribute_available_resources(bridge, &add_list);
  1748. __pci_bridge_assign_resources(bridge, &add_list, &fail_head);
  1749. BUG_ON(!list_empty(&add_list));
  1750. tried_times++;
  1751. if (list_empty(&fail_head))
  1752. goto enable_all;
  1753. if (tried_times >= 2) {
  1754. /* still fail, don't need to try more */
  1755. free_list(&fail_head);
  1756. goto enable_all;
  1757. }
  1758. printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
  1759. tried_times + 1);
  1760. /*
  1761. * Try to release leaf bridge's resources that doesn't fit resource of
  1762. * child device under that bridge
  1763. */
  1764. list_for_each_entry(fail_res, &fail_head, list)
  1765. pci_bus_release_bridge_resources(fail_res->dev->bus,
  1766. fail_res->flags & PCI_RES_TYPE_MASK,
  1767. whole_subtree);
  1768. /* restore size and flags */
  1769. list_for_each_entry(fail_res, &fail_head, list) {
  1770. struct resource *res = fail_res->res;
  1771. res->start = fail_res->start;
  1772. res->end = fail_res->end;
  1773. res->flags = fail_res->flags;
  1774. if (fail_res->dev->subordinate)
  1775. res->flags = 0;
  1776. }
  1777. free_list(&fail_head);
  1778. goto again;
  1779. enable_all:
  1780. retval = pci_reenable_device(bridge);
  1781. if (retval)
  1782. pci_err(bridge, "Error reenabling bridge (%d)\n", retval);
  1783. pci_set_master(bridge);
  1784. }
  1785. EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
  1786. int pci_reassign_bridge_resources(struct pci_dev *bridge, unsigned long type)
  1787. {
  1788. struct pci_dev_resource *dev_res;
  1789. struct pci_dev *next;
  1790. LIST_HEAD(saved);
  1791. LIST_HEAD(added);
  1792. LIST_HEAD(failed);
  1793. unsigned int i;
  1794. int ret;
  1795. /* Walk to the root hub, releasing bridge BARs when possible */
  1796. next = bridge;
  1797. do {
  1798. bridge = next;
  1799. for (i = PCI_BRIDGE_RESOURCES; i < PCI_BRIDGE_RESOURCE_END;
  1800. i++) {
  1801. struct resource *res = &bridge->resource[i];
  1802. if ((res->flags ^ type) & PCI_RES_TYPE_MASK)
  1803. continue;
  1804. /* Ignore BARs which are still in use */
  1805. if (res->child)
  1806. continue;
  1807. ret = add_to_list(&saved, bridge, res, 0, 0);
  1808. if (ret)
  1809. goto cleanup;
  1810. pci_info(bridge, "BAR %d: releasing %pR\n",
  1811. i, res);
  1812. if (res->parent)
  1813. release_resource(res);
  1814. res->start = 0;
  1815. res->end = 0;
  1816. break;
  1817. }
  1818. if (i == PCI_BRIDGE_RESOURCE_END)
  1819. break;
  1820. next = bridge->bus ? bridge->bus->self : NULL;
  1821. } while (next);
  1822. if (list_empty(&saved))
  1823. return -ENOENT;
  1824. __pci_bus_size_bridges(bridge->subordinate, &added);
  1825. __pci_bridge_assign_resources(bridge, &added, &failed);
  1826. BUG_ON(!list_empty(&added));
  1827. if (!list_empty(&failed)) {
  1828. ret = -ENOSPC;
  1829. goto cleanup;
  1830. }
  1831. list_for_each_entry(dev_res, &saved, list) {
  1832. /* Skip the bridge we just assigned resources for. */
  1833. if (bridge == dev_res->dev)
  1834. continue;
  1835. bridge = dev_res->dev;
  1836. pci_setup_bridge(bridge->subordinate);
  1837. }
  1838. free_list(&saved);
  1839. return 0;
  1840. cleanup:
  1841. /* restore size and flags */
  1842. list_for_each_entry(dev_res, &failed, list) {
  1843. struct resource *res = dev_res->res;
  1844. res->start = dev_res->start;
  1845. res->end = dev_res->end;
  1846. res->flags = dev_res->flags;
  1847. }
  1848. free_list(&failed);
  1849. /* Revert to the old configuration */
  1850. list_for_each_entry(dev_res, &saved, list) {
  1851. struct resource *res = dev_res->res;
  1852. bridge = dev_res->dev;
  1853. i = res - bridge->resource;
  1854. res->start = dev_res->start;
  1855. res->end = dev_res->end;
  1856. res->flags = dev_res->flags;
  1857. pci_claim_resource(bridge, i);
  1858. pci_setup_bridge(bridge->subordinate);
  1859. }
  1860. free_list(&saved);
  1861. return ret;
  1862. }
  1863. void pci_assign_unassigned_bus_resources(struct pci_bus *bus)
  1864. {
  1865. struct pci_dev *dev;
  1866. LIST_HEAD(add_list); /* list of resources that
  1867. want additional resources */
  1868. down_read(&pci_bus_sem);
  1869. for_each_pci_bridge(dev, bus)
  1870. if (pci_has_subordinate(dev))
  1871. __pci_bus_size_bridges(dev->subordinate, &add_list);
  1872. up_read(&pci_bus_sem);
  1873. __pci_bus_assign_resources(bus, &add_list, NULL);
  1874. BUG_ON(!list_empty(&add_list));
  1875. }
  1876. EXPORT_SYMBOL_GPL(pci_assign_unassigned_bus_resources);