setup-bus.c 58 KB

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