setup-bus.c 39 KB

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