setup-bus.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444
  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. resource_size_t start;
  32. resource_size_t end;
  33. resource_size_t add_size;
  34. resource_size_t min_align;
  35. unsigned long flags;
  36. };
  37. #define free_list(type, head) do { \
  38. struct type *dev_res, *tmp; \
  39. list_for_each_entry_safe(dev_res, tmp, head, list) { \
  40. list_del(&dev_res->list); \
  41. kfree(dev_res); \
  42. } \
  43. } while (0)
  44. int pci_realloc_enable = 0;
  45. #define pci_realloc_enabled() pci_realloc_enable
  46. void pci_realloc(void)
  47. {
  48. pci_realloc_enable = 1;
  49. }
  50. /**
  51. * add_to_list() - add a new resource tracker to the list
  52. * @head: Head of the list
  53. * @dev: device corresponding to which the resource
  54. * belongs
  55. * @res: The resource to be tracked
  56. * @add_size: additional size to be optionally added
  57. * to the resource
  58. */
  59. static int add_to_list(struct list_head *head,
  60. struct pci_dev *dev, struct resource *res,
  61. resource_size_t add_size, resource_size_t min_align)
  62. {
  63. struct pci_dev_resource *tmp;
  64. tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
  65. if (!tmp) {
  66. pr_warning("add_to_list: kmalloc() failed!\n");
  67. return -ENOMEM;
  68. }
  69. tmp->res = res;
  70. tmp->dev = dev;
  71. tmp->start = res->start;
  72. tmp->end = res->end;
  73. tmp->flags = res->flags;
  74. tmp->add_size = add_size;
  75. tmp->min_align = min_align;
  76. list_add(&tmp->list, head);
  77. return 0;
  78. }
  79. static void add_to_failed_list(struct list_head *head,
  80. struct pci_dev *dev, struct resource *res)
  81. {
  82. add_to_list(head, dev, res,
  83. 0 /* dont care */,
  84. 0 /* dont care */);
  85. }
  86. static void remove_from_list(struct list_head *realloc_head,
  87. struct resource *res)
  88. {
  89. struct pci_dev_resource *dev_res_x, *tmp;
  90. list_for_each_entry_safe(dev_res_x, tmp, realloc_head, list) {
  91. if (dev_res_x->res == res) {
  92. list_del(&dev_res_x->list);
  93. kfree(dev_res_x);
  94. break;
  95. }
  96. }
  97. }
  98. static resource_size_t get_res_add_size(struct list_head *realloc_head,
  99. struct resource *res)
  100. {
  101. struct pci_dev_resource *dev_res_x;
  102. list_for_each_entry(dev_res_x, realloc_head, list) {
  103. if (dev_res_x->res == res) {
  104. dev_printk(KERN_DEBUG, &dev_res_x->dev->dev,
  105. "%pR get_res_add_size add_size %llx\n",
  106. dev_res_x->res,
  107. (unsigned long long)dev_res_x->add_size);
  108. return dev_res_x->add_size;
  109. }
  110. }
  111. return 0;
  112. }
  113. /* Sort resources by alignment */
  114. static void pdev_sort_resources(struct pci_dev *dev, struct list_head *head)
  115. {
  116. int i;
  117. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  118. struct resource *r;
  119. struct pci_dev_resource *dev_res, *tmp;
  120. resource_size_t r_align;
  121. struct list_head *n;
  122. r = &dev->resource[i];
  123. if (r->flags & IORESOURCE_PCI_FIXED)
  124. continue;
  125. if (!(r->flags) || r->parent)
  126. continue;
  127. r_align = pci_resource_alignment(dev, r);
  128. if (!r_align) {
  129. dev_warn(&dev->dev, "BAR %d: %pR has bogus alignment\n",
  130. i, r);
  131. continue;
  132. }
  133. tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
  134. if (!tmp)
  135. panic("pdev_sort_resources(): "
  136. "kmalloc() failed!\n");
  137. tmp->res = r;
  138. tmp->dev = dev;
  139. /* fallback is smallest one or list is empty*/
  140. n = head;
  141. list_for_each_entry(dev_res, head, list) {
  142. resource_size_t align;
  143. align = pci_resource_alignment(dev_res->dev,
  144. dev_res->res);
  145. if (r_align > align) {
  146. n = &dev_res->list;
  147. break;
  148. }
  149. }
  150. /* Insert it just before n*/
  151. list_add_tail(&tmp->list, n);
  152. }
  153. }
  154. static void __dev_sort_resources(struct pci_dev *dev,
  155. struct list_head *head)
  156. {
  157. u16 class = dev->class >> 8;
  158. /* Don't touch classless devices or host bridges or ioapics. */
  159. if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST)
  160. return;
  161. /* Don't touch ioapic devices already enabled by firmware */
  162. if (class == PCI_CLASS_SYSTEM_PIC) {
  163. u16 command;
  164. pci_read_config_word(dev, PCI_COMMAND, &command);
  165. if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
  166. return;
  167. }
  168. pdev_sort_resources(dev, head);
  169. }
  170. static inline void reset_resource(struct resource *res)
  171. {
  172. res->start = 0;
  173. res->end = 0;
  174. res->flags = 0;
  175. }
  176. /**
  177. * reassign_resources_sorted() - satisfy any additional resource requests
  178. *
  179. * @realloc_head : head of the list tracking requests requiring additional
  180. * resources
  181. * @head : head of the list tracking requests with allocated
  182. * resources
  183. *
  184. * Walk through each element of the realloc_head and try to procure
  185. * additional resources for the element, provided the element
  186. * is in the head list.
  187. */
  188. static void reassign_resources_sorted(struct list_head *realloc_head,
  189. struct list_head *head)
  190. {
  191. struct resource *res;
  192. struct pci_dev_resource *dev_res_x, *tmp;
  193. struct pci_dev_resource *dev_res;
  194. resource_size_t add_size;
  195. int idx;
  196. list_for_each_entry_safe(dev_res_x, tmp, realloc_head, list) {
  197. bool found_match = false;
  198. res = dev_res_x->res;
  199. /* skip resource that has been reset */
  200. if (!res->flags)
  201. goto out;
  202. /* skip this resource if not found in head list */
  203. list_for_each_entry(dev_res, head, list) {
  204. if (dev_res->res == res) {
  205. found_match = true;
  206. break;
  207. }
  208. }
  209. if (!found_match)/* just skip */
  210. continue;
  211. idx = res - &dev_res_x->dev->resource[0];
  212. add_size = dev_res_x->add_size;
  213. if (!resource_size(res)) {
  214. res->start = dev_res_x->start;
  215. res->end = res->start + add_size - 1;
  216. if (pci_assign_resource(dev_res_x->dev, idx))
  217. reset_resource(res);
  218. } else {
  219. resource_size_t align = dev_res_x->min_align;
  220. res->flags |= dev_res_x->flags &
  221. (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN);
  222. if (pci_reassign_resource(dev_res_x->dev, idx,
  223. add_size, align))
  224. dev_printk(KERN_DEBUG, &dev_res_x->dev->dev,
  225. "failed to add optional resources res=%pR\n",
  226. res);
  227. }
  228. out:
  229. list_del(&dev_res_x->list);
  230. kfree(dev_res_x);
  231. }
  232. }
  233. /**
  234. * assign_requested_resources_sorted() - satisfy resource requests
  235. *
  236. * @head : head of the list tracking requests for resources
  237. * @failed_list : head of the list tracking requests that could
  238. * not be allocated
  239. *
  240. * Satisfy resource requests of each element in the list. Add
  241. * requests that could not satisfied to the failed_list.
  242. */
  243. static void assign_requested_resources_sorted(struct list_head *head,
  244. struct list_head *fail_head)
  245. {
  246. struct resource *res;
  247. struct pci_dev_resource *dev_res;
  248. int idx;
  249. list_for_each_entry(dev_res, head, list) {
  250. res = dev_res->res;
  251. idx = res - &dev_res->dev->resource[0];
  252. if (resource_size(res) &&
  253. pci_assign_resource(dev_res->dev, idx)) {
  254. if (fail_head && !pci_is_root_bus(dev_res->dev->bus)) {
  255. /*
  256. * if the failed res is for ROM BAR, and it will
  257. * be enabled later, don't add it to the list
  258. */
  259. if (!((idx == PCI_ROM_RESOURCE) &&
  260. (!(res->flags & IORESOURCE_ROM_ENABLE))))
  261. add_to_failed_list(fail_head,
  262. dev_res->dev, res);
  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 *dev_res_x;
  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(pci_dev_resource, &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(pci_dev_resource, &save_head);
  307. free_list(pci_dev_resource, head);
  308. return;
  309. }
  310. free_list(pci_dev_resource, &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(dev_res_x, &save_head, list) {
  317. struct resource *res = dev_res_x->res;
  318. res->start = dev_res_x->start;
  319. res->end = dev_res_x->end;
  320. res->flags = dev_res_x->flags;
  321. }
  322. free_list(pci_dev_resource, &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(pci_dev_resource, 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. }
  672. /**
  673. * pbus_size_mem() - size the memory window of a given bus
  674. *
  675. * @bus : the bus
  676. * @min_size : the minimum memory window that must to be allocated
  677. * @add_size : additional optional memory window
  678. * @realloc_head : track the additional memory window on this list
  679. *
  680. * Calculate the size of the bus and minimal alignment which
  681. * guarantees that all child resources fit in this size.
  682. */
  683. static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
  684. unsigned long type, resource_size_t min_size,
  685. resource_size_t add_size,
  686. struct list_head *realloc_head)
  687. {
  688. struct pci_dev *dev;
  689. resource_size_t min_align, align, size, size0, size1;
  690. resource_size_t aligns[12]; /* Alignments from 1Mb to 2Gb */
  691. int order, max_order;
  692. struct resource *b_res = find_free_bus_resource(bus, type);
  693. unsigned int mem64_mask = 0;
  694. resource_size_t children_add_size = 0;
  695. if (!b_res)
  696. return 0;
  697. memset(aligns, 0, sizeof(aligns));
  698. max_order = 0;
  699. size = 0;
  700. mem64_mask = b_res->flags & IORESOURCE_MEM_64;
  701. b_res->flags &= ~IORESOURCE_MEM_64;
  702. list_for_each_entry(dev, &bus->devices, bus_list) {
  703. int i;
  704. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  705. struct resource *r = &dev->resource[i];
  706. resource_size_t r_size;
  707. if (r->parent || (r->flags & mask) != type)
  708. continue;
  709. r_size = resource_size(r);
  710. #ifdef CONFIG_PCI_IOV
  711. /* put SRIOV requested res to the optional list */
  712. if (realloc_head && i >= PCI_IOV_RESOURCES &&
  713. i <= PCI_IOV_RESOURCE_END) {
  714. r->end = r->start - 1;
  715. add_to_list(realloc_head, dev, r, r_size, 0/* dont' care */);
  716. children_add_size += r_size;
  717. continue;
  718. }
  719. #endif
  720. /* For bridges size != alignment */
  721. align = pci_resource_alignment(dev, r);
  722. order = __ffs(align) - 20;
  723. if (order > 11) {
  724. dev_warn(&dev->dev, "disabling BAR %d: %pR "
  725. "(bad alignment %#llx)\n", i, r,
  726. (unsigned long long) align);
  727. r->flags = 0;
  728. continue;
  729. }
  730. size += r_size;
  731. if (order < 0)
  732. order = 0;
  733. /* Exclude ranges with size > align from
  734. calculation of the alignment. */
  735. if (r_size == align)
  736. aligns[order] += align;
  737. if (order > max_order)
  738. max_order = order;
  739. mem64_mask &= r->flags & IORESOURCE_MEM_64;
  740. if (realloc_head)
  741. children_add_size += get_res_add_size(realloc_head, r);
  742. }
  743. }
  744. align = 0;
  745. min_align = 0;
  746. for (order = 0; order <= max_order; order++) {
  747. resource_size_t align1 = 1;
  748. align1 <<= (order + 20);
  749. if (!align)
  750. min_align = align1;
  751. else if (ALIGN(align + min_align, min_align) < align1)
  752. min_align = align1 >> 1;
  753. align += aligns[order];
  754. }
  755. size0 = calculate_memsize(size, min_size, 0, resource_size(b_res), min_align);
  756. if (children_add_size > add_size)
  757. add_size = children_add_size;
  758. size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
  759. calculate_memsize(size, min_size, add_size,
  760. resource_size(b_res), min_align);
  761. if (!size0 && !size1) {
  762. if (b_res->start || b_res->end)
  763. dev_info(&bus->self->dev, "disabling bridge window "
  764. "%pR to [bus %02x-%02x] (unused)\n", b_res,
  765. bus->secondary, bus->subordinate);
  766. b_res->flags = 0;
  767. return 1;
  768. }
  769. b_res->start = min_align;
  770. b_res->end = size0 + min_align - 1;
  771. b_res->flags |= IORESOURCE_STARTALIGN | mem64_mask;
  772. if (size1 > size0 && realloc_head)
  773. add_to_list(realloc_head, bus->self, b_res, size1-size0, min_align);
  774. return 1;
  775. }
  776. unsigned long pci_cardbus_resource_alignment(struct resource *res)
  777. {
  778. if (res->flags & IORESOURCE_IO)
  779. return pci_cardbus_io_size;
  780. if (res->flags & IORESOURCE_MEM)
  781. return pci_cardbus_mem_size;
  782. return 0;
  783. }
  784. static void pci_bus_size_cardbus(struct pci_bus *bus,
  785. struct list_head *realloc_head)
  786. {
  787. struct pci_dev *bridge = bus->self;
  788. struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
  789. u16 ctrl;
  790. /*
  791. * Reserve some resources for CardBus. We reserve
  792. * a fixed amount of bus space for CardBus bridges.
  793. */
  794. b_res[0].start = 0;
  795. b_res[0].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
  796. if (realloc_head)
  797. add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size, 0 /* dont care */);
  798. b_res[1].start = 0;
  799. b_res[1].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
  800. if (realloc_head)
  801. add_to_list(realloc_head, bridge, b_res+1, pci_cardbus_io_size, 0 /* dont care */);
  802. /*
  803. * Check whether prefetchable memory is supported
  804. * by this bridge.
  805. */
  806. pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
  807. if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
  808. ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
  809. pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
  810. pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
  811. }
  812. /*
  813. * If we have prefetchable memory support, allocate
  814. * two regions. Otherwise, allocate one region of
  815. * twice the size.
  816. */
  817. if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
  818. b_res[2].start = 0;
  819. b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_SIZEALIGN;
  820. if (realloc_head)
  821. add_to_list(realloc_head, bridge, b_res+2, pci_cardbus_mem_size, 0 /* dont care */);
  822. b_res[3].start = 0;
  823. b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
  824. if (realloc_head)
  825. add_to_list(realloc_head, bridge, b_res+3, pci_cardbus_mem_size, 0 /* dont care */);
  826. } else {
  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 * 2, 0 /* dont care */);
  831. }
  832. /* set the size of the resource to zero, so that the resource does not
  833. * get assigned during required-resource allocation cycle but gets assigned
  834. * during the optional-resource allocation cycle.
  835. */
  836. b_res[0].start = b_res[1].start = b_res[2].start = b_res[3].start = 1;
  837. b_res[0].end = b_res[1].end = b_res[2].end = b_res[3].end = 0;
  838. }
  839. void __ref __pci_bus_size_bridges(struct pci_bus *bus,
  840. struct list_head *realloc_head)
  841. {
  842. struct pci_dev *dev;
  843. unsigned long mask, prefmask;
  844. resource_size_t additional_mem_size = 0, additional_io_size = 0;
  845. list_for_each_entry(dev, &bus->devices, bus_list) {
  846. struct pci_bus *b = dev->subordinate;
  847. if (!b)
  848. continue;
  849. switch (dev->class >> 8) {
  850. case PCI_CLASS_BRIDGE_CARDBUS:
  851. pci_bus_size_cardbus(b, realloc_head);
  852. break;
  853. case PCI_CLASS_BRIDGE_PCI:
  854. default:
  855. __pci_bus_size_bridges(b, realloc_head);
  856. break;
  857. }
  858. }
  859. /* The root bus? */
  860. if (!bus->self)
  861. return;
  862. switch (bus->self->class >> 8) {
  863. case PCI_CLASS_BRIDGE_CARDBUS:
  864. /* don't size cardbuses yet. */
  865. break;
  866. case PCI_CLASS_BRIDGE_PCI:
  867. pci_bridge_check_ranges(bus);
  868. if (bus->self->is_hotplug_bridge) {
  869. additional_io_size = pci_hotplug_io_size;
  870. additional_mem_size = pci_hotplug_mem_size;
  871. }
  872. /*
  873. * Follow thru
  874. */
  875. default:
  876. pbus_size_io(bus, realloc_head ? 0 : additional_io_size,
  877. additional_io_size, realloc_head);
  878. /* If the bridge supports prefetchable range, size it
  879. separately. If it doesn't, or its prefetchable window
  880. has already been allocated by arch code, try
  881. non-prefetchable range for both types of PCI memory
  882. resources. */
  883. mask = IORESOURCE_MEM;
  884. prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
  885. if (pbus_size_mem(bus, prefmask, prefmask,
  886. realloc_head ? 0 : additional_mem_size,
  887. additional_mem_size, realloc_head))
  888. mask = prefmask; /* Success, size non-prefetch only. */
  889. else
  890. additional_mem_size += additional_mem_size;
  891. pbus_size_mem(bus, mask, IORESOURCE_MEM,
  892. realloc_head ? 0 : additional_mem_size,
  893. additional_mem_size, realloc_head);
  894. break;
  895. }
  896. }
  897. void __ref pci_bus_size_bridges(struct pci_bus *bus)
  898. {
  899. __pci_bus_size_bridges(bus, NULL);
  900. }
  901. EXPORT_SYMBOL(pci_bus_size_bridges);
  902. static void __ref __pci_bus_assign_resources(const struct pci_bus *bus,
  903. struct list_head *realloc_head,
  904. struct list_head *fail_head)
  905. {
  906. struct pci_bus *b;
  907. struct pci_dev *dev;
  908. pbus_assign_resources_sorted(bus, realloc_head, fail_head);
  909. list_for_each_entry(dev, &bus->devices, bus_list) {
  910. b = dev->subordinate;
  911. if (!b)
  912. continue;
  913. __pci_bus_assign_resources(b, realloc_head, fail_head);
  914. switch (dev->class >> 8) {
  915. case PCI_CLASS_BRIDGE_PCI:
  916. if (!pci_is_enabled(dev))
  917. pci_setup_bridge(b);
  918. break;
  919. case PCI_CLASS_BRIDGE_CARDBUS:
  920. pci_setup_cardbus(b);
  921. break;
  922. default:
  923. dev_info(&dev->dev, "not setting up bridge for bus "
  924. "%04x:%02x\n", pci_domain_nr(b), b->number);
  925. break;
  926. }
  927. }
  928. }
  929. void __ref pci_bus_assign_resources(const struct pci_bus *bus)
  930. {
  931. __pci_bus_assign_resources(bus, NULL, NULL);
  932. }
  933. EXPORT_SYMBOL(pci_bus_assign_resources);
  934. static void __ref __pci_bridge_assign_resources(const struct pci_dev *bridge,
  935. struct list_head *add_head,
  936. struct list_head *fail_head)
  937. {
  938. struct pci_bus *b;
  939. pdev_assign_resources_sorted((struct pci_dev *)bridge,
  940. add_head, fail_head);
  941. b = bridge->subordinate;
  942. if (!b)
  943. return;
  944. __pci_bus_assign_resources(b, add_head, fail_head);
  945. switch (bridge->class >> 8) {
  946. case PCI_CLASS_BRIDGE_PCI:
  947. pci_setup_bridge(b);
  948. break;
  949. case PCI_CLASS_BRIDGE_CARDBUS:
  950. pci_setup_cardbus(b);
  951. break;
  952. default:
  953. dev_info(&bridge->dev, "not setting up bridge for bus "
  954. "%04x:%02x\n", pci_domain_nr(b), b->number);
  955. break;
  956. }
  957. }
  958. static void pci_bridge_release_resources(struct pci_bus *bus,
  959. unsigned long type)
  960. {
  961. int idx;
  962. bool changed = false;
  963. struct pci_dev *dev;
  964. struct resource *r;
  965. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
  966. IORESOURCE_PREFETCH;
  967. dev = bus->self;
  968. for (idx = PCI_BRIDGE_RESOURCES; idx <= PCI_BRIDGE_RESOURCE_END;
  969. idx++) {
  970. r = &dev->resource[idx];
  971. if ((r->flags & type_mask) != type)
  972. continue;
  973. if (!r->parent)
  974. continue;
  975. /*
  976. * if there are children under that, we should release them
  977. * all
  978. */
  979. release_child_resources(r);
  980. if (!release_resource(r)) {
  981. dev_printk(KERN_DEBUG, &dev->dev,
  982. "resource %d %pR released\n", idx, r);
  983. /* keep the old size */
  984. r->end = resource_size(r) - 1;
  985. r->start = 0;
  986. r->flags = 0;
  987. changed = true;
  988. }
  989. }
  990. if (changed) {
  991. /* avoiding touch the one without PREF */
  992. if (type & IORESOURCE_PREFETCH)
  993. type = IORESOURCE_PREFETCH;
  994. __pci_setup_bridge(bus, type);
  995. }
  996. }
  997. enum release_type {
  998. leaf_only,
  999. whole_subtree,
  1000. };
  1001. /*
  1002. * try to release pci bridge resources that is from leaf bridge,
  1003. * so we can allocate big new one later
  1004. */
  1005. static void __ref pci_bus_release_bridge_resources(struct pci_bus *bus,
  1006. unsigned long type,
  1007. enum release_type rel_type)
  1008. {
  1009. struct pci_dev *dev;
  1010. bool is_leaf_bridge = true;
  1011. list_for_each_entry(dev, &bus->devices, bus_list) {
  1012. struct pci_bus *b = dev->subordinate;
  1013. if (!b)
  1014. continue;
  1015. is_leaf_bridge = false;
  1016. if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  1017. continue;
  1018. if (rel_type == whole_subtree)
  1019. pci_bus_release_bridge_resources(b, type,
  1020. whole_subtree);
  1021. }
  1022. if (pci_is_root_bus(bus))
  1023. return;
  1024. if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  1025. return;
  1026. if ((rel_type == whole_subtree) || is_leaf_bridge)
  1027. pci_bridge_release_resources(bus, type);
  1028. }
  1029. static void pci_bus_dump_res(struct pci_bus *bus)
  1030. {
  1031. struct resource *res;
  1032. int i;
  1033. pci_bus_for_each_resource(bus, res, i) {
  1034. if (!res || !res->end || !res->flags)
  1035. continue;
  1036. dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res);
  1037. }
  1038. }
  1039. static void pci_bus_dump_resources(struct pci_bus *bus)
  1040. {
  1041. struct pci_bus *b;
  1042. struct pci_dev *dev;
  1043. pci_bus_dump_res(bus);
  1044. list_for_each_entry(dev, &bus->devices, bus_list) {
  1045. b = dev->subordinate;
  1046. if (!b)
  1047. continue;
  1048. pci_bus_dump_resources(b);
  1049. }
  1050. }
  1051. static int __init pci_bus_get_depth(struct pci_bus *bus)
  1052. {
  1053. int depth = 0;
  1054. struct pci_dev *dev;
  1055. list_for_each_entry(dev, &bus->devices, bus_list) {
  1056. int ret;
  1057. struct pci_bus *b = dev->subordinate;
  1058. if (!b)
  1059. continue;
  1060. ret = pci_bus_get_depth(b);
  1061. if (ret + 1 > depth)
  1062. depth = ret + 1;
  1063. }
  1064. return depth;
  1065. }
  1066. static int __init pci_get_max_depth(void)
  1067. {
  1068. int depth = 0;
  1069. struct pci_bus *bus;
  1070. list_for_each_entry(bus, &pci_root_buses, node) {
  1071. int ret;
  1072. ret = pci_bus_get_depth(bus);
  1073. if (ret > depth)
  1074. depth = ret;
  1075. }
  1076. return depth;
  1077. }
  1078. /*
  1079. * first try will not touch pci bridge res
  1080. * second and later try will clear small leaf bridge res
  1081. * will stop till to the max deepth if can not find good one
  1082. */
  1083. void __init
  1084. pci_assign_unassigned_resources(void)
  1085. {
  1086. struct pci_bus *bus;
  1087. LIST_HEAD(realloc_head); /* list of resources that
  1088. want additional resources */
  1089. struct list_head *add_list = NULL;
  1090. int tried_times = 0;
  1091. enum release_type rel_type = leaf_only;
  1092. LIST_HEAD(fail_head);
  1093. struct pci_dev_resource *dev_res_x;
  1094. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
  1095. IORESOURCE_PREFETCH;
  1096. unsigned long failed_type;
  1097. int pci_try_num = 1;
  1098. /* don't realloc if asked to do so */
  1099. if (pci_realloc_enabled()) {
  1100. int max_depth = pci_get_max_depth();
  1101. pci_try_num = max_depth + 1;
  1102. printk(KERN_DEBUG "PCI: max bus depth: %d pci_try_num: %d\n",
  1103. max_depth, pci_try_num);
  1104. }
  1105. again:
  1106. /*
  1107. * last try will use add_list, otherwise will try good to have as
  1108. * must have, so can realloc parent bridge resource
  1109. */
  1110. if (tried_times + 1 == pci_try_num)
  1111. add_list = &realloc_head;
  1112. /* Depth first, calculate sizes and alignments of all
  1113. subordinate buses. */
  1114. list_for_each_entry(bus, &pci_root_buses, node)
  1115. __pci_bus_size_bridges(bus, add_list);
  1116. /* Depth last, allocate resources and update the hardware. */
  1117. list_for_each_entry(bus, &pci_root_buses, node)
  1118. __pci_bus_assign_resources(bus, add_list, &fail_head);
  1119. if (add_list)
  1120. BUG_ON(!list_empty(add_list));
  1121. tried_times++;
  1122. /* any device complain? */
  1123. if (list_empty(&fail_head))
  1124. goto enable_and_dump;
  1125. failed_type = 0;
  1126. list_for_each_entry(dev_res_x, &fail_head, list)
  1127. failed_type |= dev_res_x->flags;
  1128. /*
  1129. * io port are tight, don't try extra
  1130. * or if reach the limit, don't want to try more
  1131. */
  1132. failed_type &= type_mask;
  1133. if ((failed_type == IORESOURCE_IO) || (tried_times >= pci_try_num)) {
  1134. free_list(pci_dev_resource, &fail_head);
  1135. goto enable_and_dump;
  1136. }
  1137. printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
  1138. tried_times + 1);
  1139. /* third times and later will not check if it is leaf */
  1140. if ((tried_times + 1) > 2)
  1141. rel_type = whole_subtree;
  1142. /*
  1143. * Try to release leaf bridge's resources that doesn't fit resource of
  1144. * child device under that bridge
  1145. */
  1146. list_for_each_entry(dev_res_x, &fail_head, list) {
  1147. bus = dev_res_x->dev->bus;
  1148. pci_bus_release_bridge_resources(bus,
  1149. dev_res_x->flags & type_mask,
  1150. rel_type);
  1151. }
  1152. /* restore size and flags */
  1153. list_for_each_entry(dev_res_x, &fail_head, list) {
  1154. struct resource *res = dev_res_x->res;
  1155. res->start = dev_res_x->start;
  1156. res->end = dev_res_x->end;
  1157. res->flags = dev_res_x->flags;
  1158. if (dev_res_x->dev->subordinate)
  1159. res->flags = 0;
  1160. }
  1161. free_list(pci_dev_resource, &fail_head);
  1162. goto again;
  1163. enable_and_dump:
  1164. /* Depth last, update the hardware. */
  1165. list_for_each_entry(bus, &pci_root_buses, node)
  1166. pci_enable_bridges(bus);
  1167. /* dump the resource on buses */
  1168. list_for_each_entry(bus, &pci_root_buses, node)
  1169. pci_bus_dump_resources(bus);
  1170. }
  1171. void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
  1172. {
  1173. struct pci_bus *parent = bridge->subordinate;
  1174. LIST_HEAD(add_list); /* list of resources that
  1175. want additional resources */
  1176. int tried_times = 0;
  1177. LIST_HEAD(fail_head);
  1178. struct pci_dev_resource *dev_res_x;
  1179. int retval;
  1180. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
  1181. IORESOURCE_PREFETCH;
  1182. again:
  1183. __pci_bus_size_bridges(parent, &add_list);
  1184. __pci_bridge_assign_resources(bridge, &add_list, &fail_head);
  1185. BUG_ON(!list_empty(&add_list));
  1186. tried_times++;
  1187. if (list_empty(&fail_head))
  1188. goto enable_all;
  1189. if (tried_times >= 2) {
  1190. /* still fail, don't need to try more */
  1191. free_list(pci_dev_resource, &fail_head);
  1192. goto enable_all;
  1193. }
  1194. printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
  1195. tried_times + 1);
  1196. /*
  1197. * Try to release leaf bridge's resources that doesn't fit resource of
  1198. * child device under that bridge
  1199. */
  1200. list_for_each_entry(dev_res_x, &fail_head, list) {
  1201. struct pci_bus *bus = dev_res_x->dev->bus;
  1202. unsigned long flags = dev_res_x->flags;
  1203. pci_bus_release_bridge_resources(bus, flags & type_mask,
  1204. whole_subtree);
  1205. }
  1206. /* restore size and flags */
  1207. list_for_each_entry(dev_res_x, &fail_head, list) {
  1208. struct resource *res = dev_res_x->res;
  1209. res->start = dev_res_x->start;
  1210. res->end = dev_res_x->end;
  1211. res->flags = dev_res_x->flags;
  1212. if (dev_res_x->dev->subordinate)
  1213. res->flags = 0;
  1214. }
  1215. free_list(pci_dev_resource, &fail_head);
  1216. goto again;
  1217. enable_all:
  1218. retval = pci_reenable_device(bridge);
  1219. pci_set_master(bridge);
  1220. pci_enable_bridges(parent);
  1221. }
  1222. EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
  1223. #ifdef CONFIG_HOTPLUG
  1224. /**
  1225. * pci_rescan_bus - scan a PCI bus for devices.
  1226. * @bus: PCI bus to scan
  1227. *
  1228. * Scan a PCI bus and child buses for new devices, adds them,
  1229. * and enables them.
  1230. *
  1231. * Returns the max number of subordinate bus discovered.
  1232. */
  1233. unsigned int __ref pci_rescan_bus(struct pci_bus *bus)
  1234. {
  1235. unsigned int max;
  1236. struct pci_dev *dev;
  1237. LIST_HEAD(add_list); /* list of resources that
  1238. want additional resources */
  1239. max = pci_scan_child_bus(bus);
  1240. down_read(&pci_bus_sem);
  1241. list_for_each_entry(dev, &bus->devices, bus_list)
  1242. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  1243. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
  1244. if (dev->subordinate)
  1245. __pci_bus_size_bridges(dev->subordinate,
  1246. &add_list);
  1247. up_read(&pci_bus_sem);
  1248. __pci_bus_assign_resources(bus, &add_list, NULL);
  1249. BUG_ON(!list_empty(&add_list));
  1250. pci_enable_bridges(bus);
  1251. pci_bus_add_devices(bus);
  1252. return max;
  1253. }
  1254. EXPORT_SYMBOL_GPL(pci_rescan_bus);
  1255. #endif