setup-bus.c 49 KB

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