setup-bus.c 39 KB

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