setup-bus.c 46 KB

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