setup-bus.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816
  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. dev_res->res->start = add_align;
  373. dev_res->res->end = add_align +
  374. resource_size(dev_res->res);
  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. }
  382. }
  383. }
  384. /* Try updated head list with add_size added */
  385. assign_requested_resources_sorted(head, &local_fail_head);
  386. /* all assigned with add_size ? */
  387. if (list_empty(&local_fail_head)) {
  388. /* Remove head list from realloc_head list */
  389. list_for_each_entry(dev_res, head, list)
  390. remove_from_list(realloc_head, dev_res->res);
  391. free_list(&save_head);
  392. free_list(head);
  393. return;
  394. }
  395. /* check failed type */
  396. fail_type = pci_fail_res_type_mask(&local_fail_head);
  397. /* remove not need to be released assigned res from head list etc */
  398. list_for_each_entry_safe(dev_res, tmp_res, head, list)
  399. if (dev_res->res->parent &&
  400. !pci_need_to_release(fail_type, dev_res->res)) {
  401. /* remove it from realloc_head list */
  402. remove_from_list(realloc_head, dev_res->res);
  403. remove_from_list(&save_head, dev_res->res);
  404. list_del(&dev_res->list);
  405. kfree(dev_res);
  406. }
  407. free_list(&local_fail_head);
  408. /* Release assigned resource */
  409. list_for_each_entry(dev_res, head, list)
  410. if (dev_res->res->parent)
  411. release_resource(dev_res->res);
  412. /* Restore start/end/flags from saved list */
  413. list_for_each_entry(save_res, &save_head, list) {
  414. struct resource *res = save_res->res;
  415. res->start = save_res->start;
  416. res->end = save_res->end;
  417. res->flags = save_res->flags;
  418. }
  419. free_list(&save_head);
  420. requested_and_reassign:
  421. /* Satisfy the must-have resource requests */
  422. assign_requested_resources_sorted(head, fail_head);
  423. /* Try to satisfy any additional optional resource
  424. requests */
  425. if (realloc_head)
  426. reassign_resources_sorted(realloc_head, head);
  427. free_list(head);
  428. }
  429. static void pdev_assign_resources_sorted(struct pci_dev *dev,
  430. struct list_head *add_head,
  431. struct list_head *fail_head)
  432. {
  433. LIST_HEAD(head);
  434. __dev_sort_resources(dev, &head);
  435. __assign_resources_sorted(&head, add_head, fail_head);
  436. }
  437. static void pbus_assign_resources_sorted(const struct pci_bus *bus,
  438. struct list_head *realloc_head,
  439. struct list_head *fail_head)
  440. {
  441. struct pci_dev *dev;
  442. LIST_HEAD(head);
  443. list_for_each_entry(dev, &bus->devices, bus_list)
  444. __dev_sort_resources(dev, &head);
  445. __assign_resources_sorted(&head, realloc_head, fail_head);
  446. }
  447. void pci_setup_cardbus(struct pci_bus *bus)
  448. {
  449. struct pci_dev *bridge = bus->self;
  450. struct resource *res;
  451. struct pci_bus_region region;
  452. dev_info(&bridge->dev, "CardBus bridge to %pR\n",
  453. &bus->busn_res);
  454. res = bus->resource[0];
  455. pcibios_resource_to_bus(bridge->bus, &region, res);
  456. if (res->flags & IORESOURCE_IO) {
  457. /*
  458. * The IO resource is allocated a range twice as large as it
  459. * would normally need. This allows us to set both IO regs.
  460. */
  461. dev_info(&bridge->dev, " bridge window %pR\n", res);
  462. pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
  463. region.start);
  464. pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
  465. region.end);
  466. }
  467. res = bus->resource[1];
  468. pcibios_resource_to_bus(bridge->bus, &region, res);
  469. if (res->flags & IORESOURCE_IO) {
  470. dev_info(&bridge->dev, " bridge window %pR\n", res);
  471. pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
  472. region.start);
  473. pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
  474. region.end);
  475. }
  476. res = bus->resource[2];
  477. pcibios_resource_to_bus(bridge->bus, &region, res);
  478. if (res->flags & IORESOURCE_MEM) {
  479. dev_info(&bridge->dev, " bridge window %pR\n", res);
  480. pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
  481. region.start);
  482. pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
  483. region.end);
  484. }
  485. res = bus->resource[3];
  486. pcibios_resource_to_bus(bridge->bus, &region, res);
  487. if (res->flags & IORESOURCE_MEM) {
  488. dev_info(&bridge->dev, " bridge window %pR\n", res);
  489. pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
  490. region.start);
  491. pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
  492. region.end);
  493. }
  494. }
  495. EXPORT_SYMBOL(pci_setup_cardbus);
  496. /* Initialize bridges with base/limit values we have collected.
  497. PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
  498. requires that if there is no I/O ports or memory behind the
  499. bridge, corresponding range must be turned off by writing base
  500. value greater than limit to the bridge's base/limit registers.
  501. Note: care must be taken when updating I/O base/limit registers
  502. of bridges which support 32-bit I/O. This update requires two
  503. config space writes, so it's quite possible that an I/O window of
  504. the bridge will have some undesirable address (e.g. 0) after the
  505. first write. Ditto 64-bit prefetchable MMIO. */
  506. static void pci_setup_bridge_io(struct pci_dev *bridge)
  507. {
  508. struct resource *res;
  509. struct pci_bus_region region;
  510. unsigned long io_mask;
  511. u8 io_base_lo, io_limit_lo;
  512. u16 l;
  513. u32 io_upper16;
  514. io_mask = PCI_IO_RANGE_MASK;
  515. if (bridge->io_window_1k)
  516. io_mask = PCI_IO_1K_RANGE_MASK;
  517. /* Set up the top and bottom of the PCI I/O segment for this bus. */
  518. res = &bridge->resource[PCI_BRIDGE_RESOURCES + 0];
  519. pcibios_resource_to_bus(bridge->bus, &region, res);
  520. if (res->flags & IORESOURCE_IO) {
  521. pci_read_config_word(bridge, PCI_IO_BASE, &l);
  522. io_base_lo = (region.start >> 8) & io_mask;
  523. io_limit_lo = (region.end >> 8) & io_mask;
  524. l = ((u16) io_limit_lo << 8) | io_base_lo;
  525. /* Set up upper 16 bits of I/O base/limit. */
  526. io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
  527. dev_info(&bridge->dev, " bridge window %pR\n", res);
  528. } else {
  529. /* Clear upper 16 bits of I/O base/limit. */
  530. io_upper16 = 0;
  531. l = 0x00f0;
  532. }
  533. /* Temporarily disable the I/O range before updating PCI_IO_BASE. */
  534. pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
  535. /* Update lower 16 bits of I/O base/limit. */
  536. pci_write_config_word(bridge, PCI_IO_BASE, l);
  537. /* Update upper 16 bits of I/O base/limit. */
  538. pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
  539. }
  540. static void pci_setup_bridge_mmio(struct pci_dev *bridge)
  541. {
  542. struct resource *res;
  543. struct pci_bus_region region;
  544. u32 l;
  545. /* Set up the top and bottom of the PCI Memory segment for this bus. */
  546. res = &bridge->resource[PCI_BRIDGE_RESOURCES + 1];
  547. pcibios_resource_to_bus(bridge->bus, &region, res);
  548. if (res->flags & IORESOURCE_MEM) {
  549. l = (region.start >> 16) & 0xfff0;
  550. l |= region.end & 0xfff00000;
  551. dev_info(&bridge->dev, " bridge window %pR\n", res);
  552. } else {
  553. l = 0x0000fff0;
  554. }
  555. pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
  556. }
  557. static void pci_setup_bridge_mmio_pref(struct pci_dev *bridge)
  558. {
  559. struct resource *res;
  560. struct pci_bus_region region;
  561. u32 l, bu, lu;
  562. /* Clear out the upper 32 bits of PREF limit.
  563. If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
  564. disables PREF range, which is ok. */
  565. pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
  566. /* Set up PREF base/limit. */
  567. bu = lu = 0;
  568. res = &bridge->resource[PCI_BRIDGE_RESOURCES + 2];
  569. pcibios_resource_to_bus(bridge->bus, &region, res);
  570. if (res->flags & IORESOURCE_PREFETCH) {
  571. l = (region.start >> 16) & 0xfff0;
  572. l |= region.end & 0xfff00000;
  573. if (res->flags & IORESOURCE_MEM_64) {
  574. bu = upper_32_bits(region.start);
  575. lu = upper_32_bits(region.end);
  576. }
  577. dev_info(&bridge->dev, " bridge window %pR\n", res);
  578. } else {
  579. l = 0x0000fff0;
  580. }
  581. pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
  582. /* Set the upper 32 bits of PREF base & limit. */
  583. pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
  584. pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
  585. }
  586. static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type)
  587. {
  588. struct pci_dev *bridge = bus->self;
  589. dev_info(&bridge->dev, "PCI bridge to %pR\n",
  590. &bus->busn_res);
  591. if (type & IORESOURCE_IO)
  592. pci_setup_bridge_io(bridge);
  593. if (type & IORESOURCE_MEM)
  594. pci_setup_bridge_mmio(bridge);
  595. if (type & IORESOURCE_PREFETCH)
  596. pci_setup_bridge_mmio_pref(bridge);
  597. pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
  598. }
  599. void pci_setup_bridge(struct pci_bus *bus)
  600. {
  601. unsigned long type = IORESOURCE_IO | IORESOURCE_MEM |
  602. IORESOURCE_PREFETCH;
  603. __pci_setup_bridge(bus, type);
  604. }
  605. int pci_claim_bridge_resource(struct pci_dev *bridge, int i)
  606. {
  607. if (i < PCI_BRIDGE_RESOURCES || i > PCI_BRIDGE_RESOURCE_END)
  608. return 0;
  609. if (pci_claim_resource(bridge, i) == 0)
  610. return 0; /* claimed the window */
  611. if ((bridge->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  612. return 0;
  613. if (!pci_bus_clip_resource(bridge, i))
  614. return -EINVAL; /* clipping didn't change anything */
  615. switch (i - PCI_BRIDGE_RESOURCES) {
  616. case 0:
  617. pci_setup_bridge_io(bridge);
  618. break;
  619. case 1:
  620. pci_setup_bridge_mmio(bridge);
  621. break;
  622. case 2:
  623. pci_setup_bridge_mmio_pref(bridge);
  624. break;
  625. default:
  626. return -EINVAL;
  627. }
  628. if (pci_claim_resource(bridge, i) == 0)
  629. return 0; /* claimed a smaller window */
  630. return -EINVAL;
  631. }
  632. /* Check whether the bridge supports optional I/O and
  633. prefetchable memory ranges. If not, the respective
  634. base/limit registers must be read-only and read as 0. */
  635. static void pci_bridge_check_ranges(struct pci_bus *bus)
  636. {
  637. u16 io;
  638. u32 pmem;
  639. struct pci_dev *bridge = bus->self;
  640. struct resource *b_res;
  641. b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
  642. b_res[1].flags |= IORESOURCE_MEM;
  643. pci_read_config_word(bridge, PCI_IO_BASE, &io);
  644. if (!io) {
  645. pci_write_config_word(bridge, PCI_IO_BASE, 0xe0f0);
  646. pci_read_config_word(bridge, PCI_IO_BASE, &io);
  647. pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
  648. }
  649. if (io)
  650. b_res[0].flags |= IORESOURCE_IO;
  651. /* DECchip 21050 pass 2 errata: the bridge may miss an address
  652. disconnect boundary by one PCI data phase.
  653. Workaround: do not use prefetching on this device. */
  654. if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001)
  655. return;
  656. pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
  657. if (!pmem) {
  658. pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE,
  659. 0xffe0fff0);
  660. pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
  661. pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0);
  662. }
  663. if (pmem) {
  664. b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
  665. if ((pmem & PCI_PREF_RANGE_TYPE_MASK) ==
  666. PCI_PREF_RANGE_TYPE_64) {
  667. b_res[2].flags |= IORESOURCE_MEM_64;
  668. b_res[2].flags |= PCI_PREF_RANGE_TYPE_64;
  669. }
  670. }
  671. /* double check if bridge does support 64 bit pref */
  672. if (b_res[2].flags & IORESOURCE_MEM_64) {
  673. u32 mem_base_hi, tmp;
  674. pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32,
  675. &mem_base_hi);
  676. pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
  677. 0xffffffff);
  678. pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp);
  679. if (!tmp)
  680. b_res[2].flags &= ~IORESOURCE_MEM_64;
  681. pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
  682. mem_base_hi);
  683. }
  684. }
  685. /* Helper function for sizing routines: find first available
  686. bus resource of a given type. Note: we intentionally skip
  687. the bus resources which have already been assigned (that is,
  688. have non-NULL parent resource). */
  689. static struct resource *find_free_bus_resource(struct pci_bus *bus,
  690. unsigned long type_mask, unsigned long type)
  691. {
  692. int i;
  693. struct resource *r;
  694. pci_bus_for_each_resource(bus, r, i) {
  695. if (r == &ioport_resource || r == &iomem_resource)
  696. continue;
  697. if (r && (r->flags & type_mask) == type && !r->parent)
  698. return r;
  699. }
  700. return NULL;
  701. }
  702. static resource_size_t calculate_iosize(resource_size_t size,
  703. resource_size_t min_size,
  704. resource_size_t size1,
  705. resource_size_t old_size,
  706. resource_size_t align)
  707. {
  708. if (size < min_size)
  709. size = min_size;
  710. if (old_size == 1)
  711. old_size = 0;
  712. /* To be fixed in 2.5: we should have sort of HAVE_ISA
  713. flag in the struct pci_bus. */
  714. #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
  715. size = (size & 0xff) + ((size & ~0xffUL) << 2);
  716. #endif
  717. size = ALIGN(size + size1, align);
  718. if (size < old_size)
  719. size = old_size;
  720. return size;
  721. }
  722. static resource_size_t calculate_memsize(resource_size_t size,
  723. resource_size_t min_size,
  724. resource_size_t size1,
  725. resource_size_t old_size,
  726. resource_size_t align)
  727. {
  728. if (size < min_size)
  729. size = min_size;
  730. if (old_size == 1)
  731. old_size = 0;
  732. if (size < old_size)
  733. size = old_size;
  734. size = ALIGN(size + size1, align);
  735. return size;
  736. }
  737. resource_size_t __weak pcibios_window_alignment(struct pci_bus *bus,
  738. unsigned long type)
  739. {
  740. return 1;
  741. }
  742. #define PCI_P2P_DEFAULT_MEM_ALIGN 0x100000 /* 1MiB */
  743. #define PCI_P2P_DEFAULT_IO_ALIGN 0x1000 /* 4KiB */
  744. #define PCI_P2P_DEFAULT_IO_ALIGN_1K 0x400 /* 1KiB */
  745. static resource_size_t window_alignment(struct pci_bus *bus,
  746. unsigned long type)
  747. {
  748. resource_size_t align = 1, arch_align;
  749. if (type & IORESOURCE_MEM)
  750. align = PCI_P2P_DEFAULT_MEM_ALIGN;
  751. else if (type & IORESOURCE_IO) {
  752. /*
  753. * Per spec, I/O windows are 4K-aligned, but some
  754. * bridges have an extension to support 1K alignment.
  755. */
  756. if (bus->self->io_window_1k)
  757. align = PCI_P2P_DEFAULT_IO_ALIGN_1K;
  758. else
  759. align = PCI_P2P_DEFAULT_IO_ALIGN;
  760. }
  761. arch_align = pcibios_window_alignment(bus, type);
  762. return max(align, arch_align);
  763. }
  764. /**
  765. * pbus_size_io() - size the io window of a given bus
  766. *
  767. * @bus : the bus
  768. * @min_size : the minimum io window that must to be allocated
  769. * @add_size : additional optional io window
  770. * @realloc_head : track the additional io window on this list
  771. *
  772. * Sizing the IO windows of the PCI-PCI bridge is trivial,
  773. * since these windows have 1K or 4K granularity and the IO ranges
  774. * of non-bridge PCI devices are limited to 256 bytes.
  775. * We must be careful with the ISA aliasing though.
  776. */
  777. static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size,
  778. resource_size_t add_size, struct list_head *realloc_head)
  779. {
  780. struct pci_dev *dev;
  781. struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO,
  782. IORESOURCE_IO);
  783. resource_size_t size = 0, size0 = 0, size1 = 0;
  784. resource_size_t children_add_size = 0;
  785. resource_size_t min_align, align;
  786. if (!b_res)
  787. return;
  788. min_align = window_alignment(bus, IORESOURCE_IO);
  789. list_for_each_entry(dev, &bus->devices, bus_list) {
  790. int i;
  791. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  792. struct resource *r = &dev->resource[i];
  793. unsigned long r_size;
  794. if (r->parent || !(r->flags & IORESOURCE_IO))
  795. continue;
  796. r_size = resource_size(r);
  797. if (r_size < 0x400)
  798. /* Might be re-aligned for ISA */
  799. size += r_size;
  800. else
  801. size1 += r_size;
  802. align = pci_resource_alignment(dev, r);
  803. if (align > min_align)
  804. min_align = align;
  805. if (realloc_head)
  806. children_add_size += get_res_add_size(realloc_head, r);
  807. }
  808. }
  809. size0 = calculate_iosize(size, min_size, size1,
  810. resource_size(b_res), min_align);
  811. if (children_add_size > add_size)
  812. add_size = children_add_size;
  813. size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
  814. calculate_iosize(size, min_size, add_size + size1,
  815. resource_size(b_res), min_align);
  816. if (!size0 && !size1) {
  817. if (b_res->start || b_res->end)
  818. dev_info(&bus->self->dev, "disabling bridge window %pR to %pR (unused)\n",
  819. b_res, &bus->busn_res);
  820. b_res->flags = 0;
  821. return;
  822. }
  823. b_res->start = min_align;
  824. b_res->end = b_res->start + size0 - 1;
  825. b_res->flags |= IORESOURCE_STARTALIGN;
  826. if (size1 > size0 && realloc_head) {
  827. add_to_list(realloc_head, bus->self, b_res, size1-size0,
  828. min_align);
  829. dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window %pR to %pR add_size %llx\n",
  830. b_res, &bus->busn_res,
  831. (unsigned long long)size1-size0);
  832. }
  833. }
  834. static inline resource_size_t calculate_mem_align(resource_size_t *aligns,
  835. int max_order)
  836. {
  837. resource_size_t align = 0;
  838. resource_size_t min_align = 0;
  839. int order;
  840. for (order = 0; order <= max_order; order++) {
  841. resource_size_t align1 = 1;
  842. align1 <<= (order + 20);
  843. if (!align)
  844. min_align = align1;
  845. else if (ALIGN(align + min_align, min_align) < align1)
  846. min_align = align1 >> 1;
  847. align += aligns[order];
  848. }
  849. return min_align;
  850. }
  851. /**
  852. * pbus_size_mem() - size the memory window of a given bus
  853. *
  854. * @bus : the bus
  855. * @mask: mask the resource flag, then compare it with type
  856. * @type: the type of free resource from bridge
  857. * @type2: second match type
  858. * @type3: third match type
  859. * @min_size : the minimum memory window that must to be allocated
  860. * @add_size : additional optional memory window
  861. * @realloc_head : track the additional memory window on this list
  862. *
  863. * Calculate the size of the bus and minimal alignment which
  864. * guarantees that all child resources fit in this size.
  865. *
  866. * Returns -ENOSPC if there's no available bus resource of the desired type.
  867. * Otherwise, sets the bus resource start/end to indicate the required
  868. * size, adds things to realloc_head (if supplied), and returns 0.
  869. */
  870. static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
  871. unsigned long type, unsigned long type2,
  872. unsigned long type3,
  873. resource_size_t min_size, resource_size_t add_size,
  874. struct list_head *realloc_head)
  875. {
  876. struct pci_dev *dev;
  877. resource_size_t min_align, align, size, size0, size1;
  878. resource_size_t aligns[18]; /* Alignments from 1Mb to 128Gb */
  879. int order, max_order;
  880. struct resource *b_res = find_free_bus_resource(bus,
  881. mask | IORESOURCE_PREFETCH, type);
  882. resource_size_t children_add_size = 0;
  883. resource_size_t children_add_align = 0;
  884. resource_size_t add_align = 0;
  885. if (!b_res)
  886. return -ENOSPC;
  887. memset(aligns, 0, sizeof(aligns));
  888. max_order = 0;
  889. size = 0;
  890. list_for_each_entry(dev, &bus->devices, bus_list) {
  891. int i;
  892. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  893. struct resource *r = &dev->resource[i];
  894. resource_size_t r_size;
  895. if (r->parent || ((r->flags & mask) != type &&
  896. (r->flags & mask) != type2 &&
  897. (r->flags & mask) != type3))
  898. continue;
  899. r_size = resource_size(r);
  900. #ifdef CONFIG_PCI_IOV
  901. /* put SRIOV requested res to the optional list */
  902. if (realloc_head && i >= PCI_IOV_RESOURCES &&
  903. i <= PCI_IOV_RESOURCE_END) {
  904. add_align = max(pci_resource_alignment(dev, r), add_align);
  905. r->end = r->start - 1;
  906. add_to_list(realloc_head, dev, r, r_size, 0/* don't care */);
  907. children_add_size += r_size;
  908. continue;
  909. }
  910. #endif
  911. /*
  912. * aligns[0] is for 1MB (since bridge memory
  913. * windows are always at least 1MB aligned), so
  914. * keep "order" from being negative for smaller
  915. * resources.
  916. */
  917. align = pci_resource_alignment(dev, r);
  918. order = __ffs(align) - 20;
  919. if (order < 0)
  920. order = 0;
  921. if (order >= ARRAY_SIZE(aligns)) {
  922. dev_warn(&dev->dev, "disabling BAR %d: %pR (bad alignment %#llx)\n",
  923. i, r, (unsigned long long) align);
  924. r->flags = 0;
  925. continue;
  926. }
  927. size += r_size;
  928. /* Exclude ranges with size > align from
  929. calculation of the alignment. */
  930. if (r_size == align)
  931. aligns[order] += align;
  932. if (order > max_order)
  933. max_order = order;
  934. if (realloc_head) {
  935. children_add_size += get_res_add_size(realloc_head, r);
  936. children_add_align = get_res_add_align(realloc_head, r);
  937. add_align = max(add_align, children_add_align);
  938. }
  939. }
  940. }
  941. min_align = calculate_mem_align(aligns, max_order);
  942. min_align = max(min_align, window_alignment(bus, b_res->flags));
  943. size0 = calculate_memsize(size, min_size, 0, resource_size(b_res), min_align);
  944. add_align = max(min_align, add_align);
  945. if (children_add_size > add_size)
  946. add_size = children_add_size;
  947. size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
  948. calculate_memsize(size, min_size, add_size,
  949. resource_size(b_res), add_align);
  950. if (!size0 && !size1) {
  951. if (b_res->start || b_res->end)
  952. dev_info(&bus->self->dev, "disabling bridge window %pR to %pR (unused)\n",
  953. b_res, &bus->busn_res);
  954. b_res->flags = 0;
  955. return 0;
  956. }
  957. b_res->start = min_align;
  958. b_res->end = size0 + min_align - 1;
  959. b_res->flags |= IORESOURCE_STARTALIGN;
  960. if (size1 > size0 && realloc_head) {
  961. add_to_list(realloc_head, bus->self, b_res, size1-size0, add_align);
  962. dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window %pR to %pR add_size %llx add_align %llx\n",
  963. b_res, &bus->busn_res,
  964. (unsigned long long) (size1 - size0),
  965. (unsigned long long) add_align);
  966. }
  967. return 0;
  968. }
  969. unsigned long pci_cardbus_resource_alignment(struct resource *res)
  970. {
  971. if (res->flags & IORESOURCE_IO)
  972. return pci_cardbus_io_size;
  973. if (res->flags & IORESOURCE_MEM)
  974. return pci_cardbus_mem_size;
  975. return 0;
  976. }
  977. static void pci_bus_size_cardbus(struct pci_bus *bus,
  978. struct list_head *realloc_head)
  979. {
  980. struct pci_dev *bridge = bus->self;
  981. struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
  982. resource_size_t b_res_3_size = pci_cardbus_mem_size * 2;
  983. u16 ctrl;
  984. if (b_res[0].parent)
  985. goto handle_b_res_1;
  986. /*
  987. * Reserve some resources for CardBus. We reserve
  988. * a fixed amount of bus space for CardBus bridges.
  989. */
  990. b_res[0].start = pci_cardbus_io_size;
  991. b_res[0].end = b_res[0].start + pci_cardbus_io_size - 1;
  992. b_res[0].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
  993. if (realloc_head) {
  994. b_res[0].end -= pci_cardbus_io_size;
  995. add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size,
  996. pci_cardbus_io_size);
  997. }
  998. handle_b_res_1:
  999. if (b_res[1].parent)
  1000. goto handle_b_res_2;
  1001. b_res[1].start = pci_cardbus_io_size;
  1002. b_res[1].end = b_res[1].start + pci_cardbus_io_size - 1;
  1003. b_res[1].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
  1004. if (realloc_head) {
  1005. b_res[1].end -= pci_cardbus_io_size;
  1006. add_to_list(realloc_head, bridge, b_res+1, pci_cardbus_io_size,
  1007. pci_cardbus_io_size);
  1008. }
  1009. handle_b_res_2:
  1010. /* MEM1 must not be pref mmio */
  1011. pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
  1012. if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM1) {
  1013. ctrl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1;
  1014. pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
  1015. pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
  1016. }
  1017. /*
  1018. * Check whether prefetchable memory is supported
  1019. * by this bridge.
  1020. */
  1021. pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
  1022. if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
  1023. ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
  1024. pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
  1025. pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
  1026. }
  1027. if (b_res[2].parent)
  1028. goto handle_b_res_3;
  1029. /*
  1030. * If we have prefetchable memory support, allocate
  1031. * two regions. Otherwise, allocate one region of
  1032. * twice the size.
  1033. */
  1034. if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
  1035. b_res[2].start = pci_cardbus_mem_size;
  1036. b_res[2].end = b_res[2].start + pci_cardbus_mem_size - 1;
  1037. b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH |
  1038. IORESOURCE_STARTALIGN;
  1039. if (realloc_head) {
  1040. b_res[2].end -= pci_cardbus_mem_size;
  1041. add_to_list(realloc_head, bridge, b_res+2,
  1042. pci_cardbus_mem_size, pci_cardbus_mem_size);
  1043. }
  1044. /* reduce that to half */
  1045. b_res_3_size = pci_cardbus_mem_size;
  1046. }
  1047. handle_b_res_3:
  1048. if (b_res[3].parent)
  1049. goto handle_done;
  1050. b_res[3].start = pci_cardbus_mem_size;
  1051. b_res[3].end = b_res[3].start + b_res_3_size - 1;
  1052. b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_STARTALIGN;
  1053. if (realloc_head) {
  1054. b_res[3].end -= b_res_3_size;
  1055. add_to_list(realloc_head, bridge, b_res+3, b_res_3_size,
  1056. pci_cardbus_mem_size);
  1057. }
  1058. handle_done:
  1059. ;
  1060. }
  1061. void __pci_bus_size_bridges(struct pci_bus *bus, struct list_head *realloc_head)
  1062. {
  1063. struct pci_dev *dev;
  1064. unsigned long mask, prefmask, type2 = 0, type3 = 0;
  1065. resource_size_t additional_mem_size = 0, additional_io_size = 0;
  1066. struct resource *b_res;
  1067. int ret;
  1068. list_for_each_entry(dev, &bus->devices, bus_list) {
  1069. struct pci_bus *b = dev->subordinate;
  1070. if (!b)
  1071. continue;
  1072. switch (dev->class >> 8) {
  1073. case PCI_CLASS_BRIDGE_CARDBUS:
  1074. pci_bus_size_cardbus(b, realloc_head);
  1075. break;
  1076. case PCI_CLASS_BRIDGE_PCI:
  1077. default:
  1078. __pci_bus_size_bridges(b, realloc_head);
  1079. break;
  1080. }
  1081. }
  1082. /* The root bus? */
  1083. if (pci_is_root_bus(bus))
  1084. return;
  1085. switch (bus->self->class >> 8) {
  1086. case PCI_CLASS_BRIDGE_CARDBUS:
  1087. /* don't size cardbuses yet. */
  1088. break;
  1089. case PCI_CLASS_BRIDGE_PCI:
  1090. pci_bridge_check_ranges(bus);
  1091. if (bus->self->is_hotplug_bridge) {
  1092. additional_io_size = pci_hotplug_io_size;
  1093. additional_mem_size = pci_hotplug_mem_size;
  1094. }
  1095. /* Fall through */
  1096. default:
  1097. pbus_size_io(bus, realloc_head ? 0 : additional_io_size,
  1098. additional_io_size, realloc_head);
  1099. /*
  1100. * If there's a 64-bit prefetchable MMIO window, compute
  1101. * the size required to put all 64-bit prefetchable
  1102. * resources in it.
  1103. */
  1104. b_res = &bus->self->resource[PCI_BRIDGE_RESOURCES];
  1105. mask = IORESOURCE_MEM;
  1106. prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
  1107. if (b_res[2].flags & IORESOURCE_MEM_64) {
  1108. prefmask |= IORESOURCE_MEM_64;
  1109. ret = pbus_size_mem(bus, prefmask, prefmask,
  1110. prefmask, prefmask,
  1111. realloc_head ? 0 : additional_mem_size,
  1112. additional_mem_size, realloc_head);
  1113. /*
  1114. * If successful, all non-prefetchable resources
  1115. * and any 32-bit prefetchable resources will go in
  1116. * the non-prefetchable window.
  1117. */
  1118. if (ret == 0) {
  1119. mask = prefmask;
  1120. type2 = prefmask & ~IORESOURCE_MEM_64;
  1121. type3 = prefmask & ~IORESOURCE_PREFETCH;
  1122. }
  1123. }
  1124. /*
  1125. * If there is no 64-bit prefetchable window, compute the
  1126. * size required to put all prefetchable resources in the
  1127. * 32-bit prefetchable window (if there is one).
  1128. */
  1129. if (!type2) {
  1130. prefmask &= ~IORESOURCE_MEM_64;
  1131. ret = pbus_size_mem(bus, prefmask, prefmask,
  1132. prefmask, prefmask,
  1133. realloc_head ? 0 : additional_mem_size,
  1134. additional_mem_size, realloc_head);
  1135. /*
  1136. * If successful, only non-prefetchable resources
  1137. * will go in the non-prefetchable window.
  1138. */
  1139. if (ret == 0)
  1140. mask = prefmask;
  1141. else
  1142. additional_mem_size += additional_mem_size;
  1143. type2 = type3 = IORESOURCE_MEM;
  1144. }
  1145. /*
  1146. * Compute the size required to put everything else in the
  1147. * non-prefetchable window. This includes:
  1148. *
  1149. * - all non-prefetchable resources
  1150. * - 32-bit prefetchable resources if there's a 64-bit
  1151. * prefetchable window or no prefetchable window at all
  1152. * - 64-bit prefetchable resources if there's no
  1153. * prefetchable window at all
  1154. *
  1155. * Note that the strategy in __pci_assign_resource() must
  1156. * match that used here. Specifically, we cannot put a
  1157. * 32-bit prefetchable resource in a 64-bit prefetchable
  1158. * window.
  1159. */
  1160. pbus_size_mem(bus, mask, IORESOURCE_MEM, type2, type3,
  1161. realloc_head ? 0 : additional_mem_size,
  1162. additional_mem_size, realloc_head);
  1163. break;
  1164. }
  1165. }
  1166. void pci_bus_size_bridges(struct pci_bus *bus)
  1167. {
  1168. __pci_bus_size_bridges(bus, NULL);
  1169. }
  1170. EXPORT_SYMBOL(pci_bus_size_bridges);
  1171. void __pci_bus_assign_resources(const struct pci_bus *bus,
  1172. struct list_head *realloc_head,
  1173. struct list_head *fail_head)
  1174. {
  1175. struct pci_bus *b;
  1176. struct pci_dev *dev;
  1177. pbus_assign_resources_sorted(bus, realloc_head, fail_head);
  1178. list_for_each_entry(dev, &bus->devices, bus_list) {
  1179. b = dev->subordinate;
  1180. if (!b)
  1181. continue;
  1182. __pci_bus_assign_resources(b, realloc_head, fail_head);
  1183. switch (dev->class >> 8) {
  1184. case PCI_CLASS_BRIDGE_PCI:
  1185. if (!pci_is_enabled(dev))
  1186. pci_setup_bridge(b);
  1187. break;
  1188. case PCI_CLASS_BRIDGE_CARDBUS:
  1189. pci_setup_cardbus(b);
  1190. break;
  1191. default:
  1192. dev_info(&dev->dev, "not setting up bridge for bus %04x:%02x\n",
  1193. pci_domain_nr(b), b->number);
  1194. break;
  1195. }
  1196. }
  1197. }
  1198. void pci_bus_assign_resources(const struct pci_bus *bus)
  1199. {
  1200. __pci_bus_assign_resources(bus, NULL, NULL);
  1201. }
  1202. EXPORT_SYMBOL(pci_bus_assign_resources);
  1203. static void __pci_bridge_assign_resources(const struct pci_dev *bridge,
  1204. struct list_head *add_head,
  1205. struct list_head *fail_head)
  1206. {
  1207. struct pci_bus *b;
  1208. pdev_assign_resources_sorted((struct pci_dev *)bridge,
  1209. add_head, fail_head);
  1210. b = bridge->subordinate;
  1211. if (!b)
  1212. return;
  1213. __pci_bus_assign_resources(b, add_head, fail_head);
  1214. switch (bridge->class >> 8) {
  1215. case PCI_CLASS_BRIDGE_PCI:
  1216. pci_setup_bridge(b);
  1217. break;
  1218. case PCI_CLASS_BRIDGE_CARDBUS:
  1219. pci_setup_cardbus(b);
  1220. break;
  1221. default:
  1222. dev_info(&bridge->dev, "not setting up bridge for bus %04x:%02x\n",
  1223. pci_domain_nr(b), b->number);
  1224. break;
  1225. }
  1226. }
  1227. static void pci_bridge_release_resources(struct pci_bus *bus,
  1228. unsigned long type)
  1229. {
  1230. struct pci_dev *dev = bus->self;
  1231. struct resource *r;
  1232. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
  1233. IORESOURCE_PREFETCH | IORESOURCE_MEM_64;
  1234. unsigned old_flags = 0;
  1235. struct resource *b_res;
  1236. int idx = 1;
  1237. b_res = &dev->resource[PCI_BRIDGE_RESOURCES];
  1238. /*
  1239. * 1. if there is io port assign fail, will release bridge
  1240. * io port.
  1241. * 2. if there is non pref mmio assign fail, release bridge
  1242. * nonpref mmio.
  1243. * 3. if there is 64bit pref mmio assign fail, and bridge pref
  1244. * is 64bit, release bridge pref mmio.
  1245. * 4. if there is pref mmio assign fail, and bridge pref is
  1246. * 32bit mmio, release bridge pref mmio
  1247. * 5. if there is pref mmio assign fail, and bridge pref is not
  1248. * assigned, release bridge nonpref mmio.
  1249. */
  1250. if (type & IORESOURCE_IO)
  1251. idx = 0;
  1252. else if (!(type & IORESOURCE_PREFETCH))
  1253. idx = 1;
  1254. else if ((type & IORESOURCE_MEM_64) &&
  1255. (b_res[2].flags & IORESOURCE_MEM_64))
  1256. idx = 2;
  1257. else if (!(b_res[2].flags & IORESOURCE_MEM_64) &&
  1258. (b_res[2].flags & IORESOURCE_PREFETCH))
  1259. idx = 2;
  1260. else
  1261. idx = 1;
  1262. r = &b_res[idx];
  1263. if (!r->parent)
  1264. return;
  1265. /*
  1266. * if there are children under that, we should release them
  1267. * all
  1268. */
  1269. release_child_resources(r);
  1270. if (!release_resource(r)) {
  1271. type = old_flags = r->flags & type_mask;
  1272. dev_printk(KERN_DEBUG, &dev->dev, "resource %d %pR released\n",
  1273. PCI_BRIDGE_RESOURCES + idx, r);
  1274. /* keep the old size */
  1275. r->end = resource_size(r) - 1;
  1276. r->start = 0;
  1277. r->flags = 0;
  1278. /* avoiding touch the one without PREF */
  1279. if (type & IORESOURCE_PREFETCH)
  1280. type = IORESOURCE_PREFETCH;
  1281. __pci_setup_bridge(bus, type);
  1282. /* for next child res under same bridge */
  1283. r->flags = old_flags;
  1284. }
  1285. }
  1286. enum release_type {
  1287. leaf_only,
  1288. whole_subtree,
  1289. };
  1290. /*
  1291. * try to release pci bridge resources that is from leaf bridge,
  1292. * so we can allocate big new one later
  1293. */
  1294. static void pci_bus_release_bridge_resources(struct pci_bus *bus,
  1295. unsigned long type,
  1296. enum release_type rel_type)
  1297. {
  1298. struct pci_dev *dev;
  1299. bool is_leaf_bridge = true;
  1300. list_for_each_entry(dev, &bus->devices, bus_list) {
  1301. struct pci_bus *b = dev->subordinate;
  1302. if (!b)
  1303. continue;
  1304. is_leaf_bridge = false;
  1305. if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  1306. continue;
  1307. if (rel_type == whole_subtree)
  1308. pci_bus_release_bridge_resources(b, type,
  1309. whole_subtree);
  1310. }
  1311. if (pci_is_root_bus(bus))
  1312. return;
  1313. if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  1314. return;
  1315. if ((rel_type == whole_subtree) || is_leaf_bridge)
  1316. pci_bridge_release_resources(bus, type);
  1317. }
  1318. static void pci_bus_dump_res(struct pci_bus *bus)
  1319. {
  1320. struct resource *res;
  1321. int i;
  1322. pci_bus_for_each_resource(bus, res, i) {
  1323. if (!res || !res->end || !res->flags)
  1324. continue;
  1325. dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res);
  1326. }
  1327. }
  1328. static void pci_bus_dump_resources(struct pci_bus *bus)
  1329. {
  1330. struct pci_bus *b;
  1331. struct pci_dev *dev;
  1332. pci_bus_dump_res(bus);
  1333. list_for_each_entry(dev, &bus->devices, bus_list) {
  1334. b = dev->subordinate;
  1335. if (!b)
  1336. continue;
  1337. pci_bus_dump_resources(b);
  1338. }
  1339. }
  1340. static int pci_bus_get_depth(struct pci_bus *bus)
  1341. {
  1342. int depth = 0;
  1343. struct pci_bus *child_bus;
  1344. list_for_each_entry(child_bus, &bus->children, node) {
  1345. int ret;
  1346. ret = pci_bus_get_depth(child_bus);
  1347. if (ret + 1 > depth)
  1348. depth = ret + 1;
  1349. }
  1350. return depth;
  1351. }
  1352. /*
  1353. * -1: undefined, will auto detect later
  1354. * 0: disabled by user
  1355. * 1: disabled by auto detect
  1356. * 2: enabled by user
  1357. * 3: enabled by auto detect
  1358. */
  1359. enum enable_type {
  1360. undefined = -1,
  1361. user_disabled,
  1362. auto_disabled,
  1363. user_enabled,
  1364. auto_enabled,
  1365. };
  1366. static enum enable_type pci_realloc_enable = undefined;
  1367. void __init pci_realloc_get_opt(char *str)
  1368. {
  1369. if (!strncmp(str, "off", 3))
  1370. pci_realloc_enable = user_disabled;
  1371. else if (!strncmp(str, "on", 2))
  1372. pci_realloc_enable = user_enabled;
  1373. }
  1374. static bool pci_realloc_enabled(enum enable_type enable)
  1375. {
  1376. return enable >= user_enabled;
  1377. }
  1378. #if defined(CONFIG_PCI_IOV) && defined(CONFIG_PCI_REALLOC_ENABLE_AUTO)
  1379. static int iov_resources_unassigned(struct pci_dev *dev, void *data)
  1380. {
  1381. int i;
  1382. bool *unassigned = data;
  1383. for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++) {
  1384. struct resource *r = &dev->resource[i];
  1385. struct pci_bus_region region;
  1386. /* Not assigned or rejected by kernel? */
  1387. if (!r->flags)
  1388. continue;
  1389. pcibios_resource_to_bus(dev->bus, &region, r);
  1390. if (!region.start) {
  1391. *unassigned = true;
  1392. return 1; /* return early from pci_walk_bus() */
  1393. }
  1394. }
  1395. return 0;
  1396. }
  1397. static enum enable_type pci_realloc_detect(struct pci_bus *bus,
  1398. enum enable_type enable_local)
  1399. {
  1400. bool unassigned = false;
  1401. if (enable_local != undefined)
  1402. return enable_local;
  1403. pci_walk_bus(bus, iov_resources_unassigned, &unassigned);
  1404. if (unassigned)
  1405. return auto_enabled;
  1406. return enable_local;
  1407. }
  1408. #else
  1409. static enum enable_type pci_realloc_detect(struct pci_bus *bus,
  1410. enum enable_type enable_local)
  1411. {
  1412. return enable_local;
  1413. }
  1414. #endif
  1415. /*
  1416. * first try will not touch pci bridge res
  1417. * second and later try will clear small leaf bridge res
  1418. * will stop till to the max depth if can not find good one
  1419. */
  1420. void pci_assign_unassigned_root_bus_resources(struct pci_bus *bus)
  1421. {
  1422. LIST_HEAD(realloc_head); /* list of resources that
  1423. want additional resources */
  1424. struct list_head *add_list = NULL;
  1425. int tried_times = 0;
  1426. enum release_type rel_type = leaf_only;
  1427. LIST_HEAD(fail_head);
  1428. struct pci_dev_resource *fail_res;
  1429. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
  1430. IORESOURCE_PREFETCH | IORESOURCE_MEM_64;
  1431. int pci_try_num = 1;
  1432. enum enable_type enable_local;
  1433. /* don't realloc if asked to do so */
  1434. enable_local = pci_realloc_detect(bus, pci_realloc_enable);
  1435. if (pci_realloc_enabled(enable_local)) {
  1436. int max_depth = pci_bus_get_depth(bus);
  1437. pci_try_num = max_depth + 1;
  1438. dev_printk(KERN_DEBUG, &bus->dev,
  1439. "max bus depth: %d pci_try_num: %d\n",
  1440. max_depth, pci_try_num);
  1441. }
  1442. again:
  1443. /*
  1444. * last try will use add_list, otherwise will try good to have as
  1445. * must have, so can realloc parent bridge resource
  1446. */
  1447. if (tried_times + 1 == pci_try_num)
  1448. add_list = &realloc_head;
  1449. /* Depth first, calculate sizes and alignments of all
  1450. subordinate buses. */
  1451. __pci_bus_size_bridges(bus, add_list);
  1452. /* Depth last, allocate resources and update the hardware. */
  1453. __pci_bus_assign_resources(bus, add_list, &fail_head);
  1454. if (add_list)
  1455. BUG_ON(!list_empty(add_list));
  1456. tried_times++;
  1457. /* any device complain? */
  1458. if (list_empty(&fail_head))
  1459. goto dump;
  1460. if (tried_times >= pci_try_num) {
  1461. if (enable_local == undefined)
  1462. dev_info(&bus->dev, "Some PCI device resources are unassigned, try booting with pci=realloc\n");
  1463. else if (enable_local == auto_enabled)
  1464. dev_info(&bus->dev, "Automatically enabled pci realloc, if you have problem, try booting with pci=realloc=off\n");
  1465. free_list(&fail_head);
  1466. goto dump;
  1467. }
  1468. dev_printk(KERN_DEBUG, &bus->dev,
  1469. "No. %d try to assign unassigned res\n", tried_times + 1);
  1470. /* third times and later will not check if it is leaf */
  1471. if ((tried_times + 1) > 2)
  1472. rel_type = whole_subtree;
  1473. /*
  1474. * Try to release leaf bridge's resources that doesn't fit resource of
  1475. * child device under that bridge
  1476. */
  1477. list_for_each_entry(fail_res, &fail_head, list)
  1478. pci_bus_release_bridge_resources(fail_res->dev->bus,
  1479. fail_res->flags & type_mask,
  1480. rel_type);
  1481. /* restore size and flags */
  1482. list_for_each_entry(fail_res, &fail_head, list) {
  1483. struct resource *res = fail_res->res;
  1484. res->start = fail_res->start;
  1485. res->end = fail_res->end;
  1486. res->flags = fail_res->flags;
  1487. if (fail_res->dev->subordinate)
  1488. res->flags = 0;
  1489. }
  1490. free_list(&fail_head);
  1491. goto again;
  1492. dump:
  1493. /* dump the resource on buses */
  1494. pci_bus_dump_resources(bus);
  1495. }
  1496. void __init pci_assign_unassigned_resources(void)
  1497. {
  1498. struct pci_bus *root_bus;
  1499. list_for_each_entry(root_bus, &pci_root_buses, node)
  1500. pci_assign_unassigned_root_bus_resources(root_bus);
  1501. }
  1502. void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
  1503. {
  1504. struct pci_bus *parent = bridge->subordinate;
  1505. LIST_HEAD(add_list); /* list of resources that
  1506. want additional resources */
  1507. int tried_times = 0;
  1508. LIST_HEAD(fail_head);
  1509. struct pci_dev_resource *fail_res;
  1510. int retval;
  1511. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
  1512. IORESOURCE_PREFETCH | IORESOURCE_MEM_64;
  1513. again:
  1514. __pci_bus_size_bridges(parent, &add_list);
  1515. __pci_bridge_assign_resources(bridge, &add_list, &fail_head);
  1516. BUG_ON(!list_empty(&add_list));
  1517. tried_times++;
  1518. if (list_empty(&fail_head))
  1519. goto enable_all;
  1520. if (tried_times >= 2) {
  1521. /* still fail, don't need to try more */
  1522. free_list(&fail_head);
  1523. goto enable_all;
  1524. }
  1525. printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
  1526. tried_times + 1);
  1527. /*
  1528. * Try to release leaf bridge's resources that doesn't fit resource of
  1529. * child device under that bridge
  1530. */
  1531. list_for_each_entry(fail_res, &fail_head, list)
  1532. pci_bus_release_bridge_resources(fail_res->dev->bus,
  1533. fail_res->flags & type_mask,
  1534. whole_subtree);
  1535. /* restore size and flags */
  1536. list_for_each_entry(fail_res, &fail_head, list) {
  1537. struct resource *res = fail_res->res;
  1538. res->start = fail_res->start;
  1539. res->end = fail_res->end;
  1540. res->flags = fail_res->flags;
  1541. if (fail_res->dev->subordinate)
  1542. res->flags = 0;
  1543. }
  1544. free_list(&fail_head);
  1545. goto again;
  1546. enable_all:
  1547. retval = pci_reenable_device(bridge);
  1548. if (retval)
  1549. dev_err(&bridge->dev, "Error reenabling bridge (%d)\n", retval);
  1550. pci_set_master(bridge);
  1551. }
  1552. EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
  1553. void pci_assign_unassigned_bus_resources(struct pci_bus *bus)
  1554. {
  1555. struct pci_dev *dev;
  1556. LIST_HEAD(add_list); /* list of resources that
  1557. want additional resources */
  1558. down_read(&pci_bus_sem);
  1559. list_for_each_entry(dev, &bus->devices, bus_list)
  1560. if (pci_is_bridge(dev) && pci_has_subordinate(dev))
  1561. __pci_bus_size_bridges(dev->subordinate,
  1562. &add_list);
  1563. up_read(&pci_bus_sem);
  1564. __pci_bus_assign_resources(bus, &add_list, NULL);
  1565. BUG_ON(!list_empty(&add_list));
  1566. }
  1567. EXPORT_SYMBOL_GPL(pci_assign_unassigned_bus_resources);