setup-bus.c 40 KB

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