setup-bus.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582
  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. * @fail_head : 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 beginning
  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 %pR\n",
  350. &bus->busn_res);
  351. res = bus->resource[0];
  352. pcibios_resource_to_bus(bridge, &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, &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, &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, &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. unsigned long io_mask;
  409. u8 io_base_lo, io_limit_lo;
  410. u32 l, io_upper16;
  411. io_mask = PCI_IO_RANGE_MASK;
  412. if (bridge->io_window_1k)
  413. io_mask = PCI_IO_1K_RANGE_MASK;
  414. /* Set up the top and bottom of the PCI I/O segment for this bus. */
  415. res = bus->resource[0];
  416. pcibios_resource_to_bus(bridge, &region, res);
  417. if (res->flags & IORESOURCE_IO) {
  418. pci_read_config_dword(bridge, PCI_IO_BASE, &l);
  419. l &= 0xffff0000;
  420. io_base_lo = (region.start >> 8) & io_mask;
  421. io_limit_lo = (region.end >> 8) & io_mask;
  422. l |= ((u32) io_limit_lo << 8) | io_base_lo;
  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 %pR\n",
  490. &bus->busn_res);
  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. resource_size_t __weak pcibios_window_alignment(struct pci_bus *bus,
  612. unsigned long type)
  613. {
  614. return 1;
  615. }
  616. #define PCI_P2P_DEFAULT_MEM_ALIGN 0x100000 /* 1MiB */
  617. #define PCI_P2P_DEFAULT_IO_ALIGN 0x1000 /* 4KiB */
  618. #define PCI_P2P_DEFAULT_IO_ALIGN_1K 0x400 /* 1KiB */
  619. static resource_size_t window_alignment(struct pci_bus *bus,
  620. unsigned long type)
  621. {
  622. resource_size_t align = 1, arch_align;
  623. if (type & IORESOURCE_MEM)
  624. align = PCI_P2P_DEFAULT_MEM_ALIGN;
  625. else if (type & IORESOURCE_IO) {
  626. /*
  627. * Per spec, I/O windows are 4K-aligned, but some
  628. * bridges have an extension to support 1K alignment.
  629. */
  630. if (bus->self->io_window_1k)
  631. align = PCI_P2P_DEFAULT_IO_ALIGN_1K;
  632. else
  633. align = PCI_P2P_DEFAULT_IO_ALIGN;
  634. }
  635. arch_align = pcibios_window_alignment(bus, type);
  636. return max(align, arch_align);
  637. }
  638. /**
  639. * pbus_size_io() - size the io window of a given bus
  640. *
  641. * @bus : the bus
  642. * @min_size : the minimum io window that must to be allocated
  643. * @add_size : additional optional io window
  644. * @realloc_head : track the additional io window on this list
  645. *
  646. * Sizing the IO windows of the PCI-PCI bridge is trivial,
  647. * since these windows have 1K or 4K granularity and the IO ranges
  648. * of non-bridge PCI devices are limited to 256 bytes.
  649. * We must be careful with the ISA aliasing though.
  650. */
  651. static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size,
  652. resource_size_t add_size, struct list_head *realloc_head)
  653. {
  654. struct pci_dev *dev;
  655. struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO);
  656. unsigned long size = 0, size0 = 0, size1 = 0;
  657. resource_size_t children_add_size = 0;
  658. resource_size_t min_align = 4096, align;
  659. if (!b_res)
  660. return;
  661. /*
  662. * Per spec, I/O windows are 4K-aligned, but some bridges have an
  663. * extension to support 1K alignment.
  664. */
  665. if (bus->self->io_window_1k)
  666. min_align = 1024;
  667. list_for_each_entry(dev, &bus->devices, bus_list) {
  668. int i;
  669. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  670. struct resource *r = &dev->resource[i];
  671. unsigned long r_size;
  672. if (r->parent || !(r->flags & IORESOURCE_IO))
  673. continue;
  674. r_size = resource_size(r);
  675. if (r_size < 0x400)
  676. /* Might be re-aligned for ISA */
  677. size += r_size;
  678. else
  679. size1 += r_size;
  680. align = pci_resource_alignment(dev, r);
  681. if (align > min_align)
  682. min_align = align;
  683. if (realloc_head)
  684. children_add_size += get_res_add_size(realloc_head, r);
  685. }
  686. }
  687. if (min_align > 4096)
  688. min_align = 4096;
  689. size0 = calculate_iosize(size, min_size, size1,
  690. resource_size(b_res), min_align);
  691. if (children_add_size > add_size)
  692. add_size = children_add_size;
  693. size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
  694. calculate_iosize(size, min_size, add_size + size1,
  695. resource_size(b_res), min_align);
  696. if (!size0 && !size1) {
  697. if (b_res->start || b_res->end)
  698. dev_info(&bus->self->dev, "disabling bridge window "
  699. "%pR to %pR (unused)\n", b_res,
  700. &bus->busn_res);
  701. b_res->flags = 0;
  702. return;
  703. }
  704. b_res->start = min_align;
  705. b_res->end = b_res->start + size0 - 1;
  706. b_res->flags |= IORESOURCE_STARTALIGN;
  707. if (size1 > size0 && realloc_head) {
  708. add_to_list(realloc_head, bus->self, b_res, size1-size0,
  709. min_align);
  710. dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window "
  711. "%pR to %pR add_size %lx\n", b_res,
  712. &bus->busn_res, size1-size0);
  713. }
  714. }
  715. /**
  716. * pbus_size_mem() - size the memory window of a given bus
  717. *
  718. * @bus : the bus
  719. * @min_size : the minimum memory window that must to be allocated
  720. * @add_size : additional optional memory window
  721. * @realloc_head : track the additional memory window on this list
  722. *
  723. * Calculate the size of the bus and minimal alignment which
  724. * guarantees that all child resources fit in this size.
  725. */
  726. static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
  727. unsigned long type, resource_size_t min_size,
  728. resource_size_t add_size,
  729. struct list_head *realloc_head)
  730. {
  731. struct pci_dev *dev;
  732. resource_size_t min_align, align, size, size0, size1;
  733. resource_size_t aligns[12]; /* Alignments from 1Mb to 2Gb */
  734. int order, max_order;
  735. struct resource *b_res = find_free_bus_resource(bus, type);
  736. unsigned int mem64_mask = 0;
  737. resource_size_t children_add_size = 0;
  738. if (!b_res)
  739. return 0;
  740. memset(aligns, 0, sizeof(aligns));
  741. max_order = 0;
  742. size = 0;
  743. mem64_mask = b_res->flags & IORESOURCE_MEM_64;
  744. b_res->flags &= ~IORESOURCE_MEM_64;
  745. list_for_each_entry(dev, &bus->devices, bus_list) {
  746. int i;
  747. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  748. struct resource *r = &dev->resource[i];
  749. resource_size_t r_size;
  750. if (r->parent || (r->flags & mask) != type)
  751. continue;
  752. r_size = resource_size(r);
  753. #ifdef CONFIG_PCI_IOV
  754. /* put SRIOV requested res to the optional list */
  755. if (realloc_head && i >= PCI_IOV_RESOURCES &&
  756. i <= PCI_IOV_RESOURCE_END) {
  757. r->end = r->start - 1;
  758. add_to_list(realloc_head, dev, r, r_size, 0/* dont' care */);
  759. children_add_size += r_size;
  760. continue;
  761. }
  762. #endif
  763. /* For bridges size != alignment */
  764. align = pci_resource_alignment(dev, r);
  765. order = __ffs(align) - 20;
  766. if (order > 11) {
  767. dev_warn(&dev->dev, "disabling BAR %d: %pR "
  768. "(bad alignment %#llx)\n", i, r,
  769. (unsigned long long) align);
  770. r->flags = 0;
  771. continue;
  772. }
  773. size += r_size;
  774. if (order < 0)
  775. order = 0;
  776. /* Exclude ranges with size > align from
  777. calculation of the alignment. */
  778. if (r_size == align)
  779. aligns[order] += align;
  780. if (order > max_order)
  781. max_order = order;
  782. mem64_mask &= r->flags & IORESOURCE_MEM_64;
  783. if (realloc_head)
  784. children_add_size += get_res_add_size(realloc_head, r);
  785. }
  786. }
  787. align = 0;
  788. min_align = 0;
  789. for (order = 0; order <= max_order; order++) {
  790. resource_size_t align1 = 1;
  791. align1 <<= (order + 20);
  792. if (!align)
  793. min_align = align1;
  794. else if (ALIGN(align + min_align, min_align) < align1)
  795. min_align = align1 >> 1;
  796. align += aligns[order];
  797. }
  798. size0 = calculate_memsize(size, min_size, 0, resource_size(b_res), min_align);
  799. if (children_add_size > add_size)
  800. add_size = children_add_size;
  801. size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
  802. calculate_memsize(size, min_size, add_size,
  803. resource_size(b_res), min_align);
  804. if (!size0 && !size1) {
  805. if (b_res->start || b_res->end)
  806. dev_info(&bus->self->dev, "disabling bridge window "
  807. "%pR to %pR (unused)\n", b_res,
  808. &bus->busn_res);
  809. b_res->flags = 0;
  810. return 1;
  811. }
  812. b_res->start = min_align;
  813. b_res->end = size0 + min_align - 1;
  814. b_res->flags |= IORESOURCE_STARTALIGN | mem64_mask;
  815. if (size1 > size0 && realloc_head) {
  816. add_to_list(realloc_head, bus->self, b_res, size1-size0, min_align);
  817. dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window "
  818. "%pR to %pR add_size %llx\n", b_res,
  819. &bus->busn_res, (unsigned long long)size1-size0);
  820. }
  821. return 1;
  822. }
  823. unsigned long pci_cardbus_resource_alignment(struct resource *res)
  824. {
  825. if (res->flags & IORESOURCE_IO)
  826. return pci_cardbus_io_size;
  827. if (res->flags & IORESOURCE_MEM)
  828. return pci_cardbus_mem_size;
  829. return 0;
  830. }
  831. static void pci_bus_size_cardbus(struct pci_bus *bus,
  832. struct list_head *realloc_head)
  833. {
  834. struct pci_dev *bridge = bus->self;
  835. struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
  836. resource_size_t b_res_3_size = pci_cardbus_mem_size * 2;
  837. u16 ctrl;
  838. if (b_res[0].parent)
  839. goto handle_b_res_1;
  840. /*
  841. * Reserve some resources for CardBus. We reserve
  842. * a fixed amount of bus space for CardBus bridges.
  843. */
  844. b_res[0].start = pci_cardbus_io_size;
  845. b_res[0].end = b_res[0].start + pci_cardbus_io_size - 1;
  846. b_res[0].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
  847. if (realloc_head) {
  848. b_res[0].end -= pci_cardbus_io_size;
  849. add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size,
  850. pci_cardbus_io_size);
  851. }
  852. handle_b_res_1:
  853. if (b_res[1].parent)
  854. goto handle_b_res_2;
  855. b_res[1].start = pci_cardbus_io_size;
  856. b_res[1].end = b_res[1].start + pci_cardbus_io_size - 1;
  857. b_res[1].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
  858. if (realloc_head) {
  859. b_res[1].end -= pci_cardbus_io_size;
  860. add_to_list(realloc_head, bridge, b_res+1, pci_cardbus_io_size,
  861. pci_cardbus_io_size);
  862. }
  863. handle_b_res_2:
  864. /* MEM1 must not be pref mmio */
  865. pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
  866. if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM1) {
  867. ctrl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1;
  868. pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
  869. pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
  870. }
  871. /*
  872. * Check whether prefetchable memory is supported
  873. * by this bridge.
  874. */
  875. pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
  876. if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
  877. ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
  878. pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
  879. pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
  880. }
  881. if (b_res[2].parent)
  882. goto handle_b_res_3;
  883. /*
  884. * If we have prefetchable memory support, allocate
  885. * two regions. Otherwise, allocate one region of
  886. * twice the size.
  887. */
  888. if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
  889. b_res[2].start = pci_cardbus_mem_size;
  890. b_res[2].end = b_res[2].start + pci_cardbus_mem_size - 1;
  891. b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH |
  892. IORESOURCE_STARTALIGN;
  893. if (realloc_head) {
  894. b_res[2].end -= pci_cardbus_mem_size;
  895. add_to_list(realloc_head, bridge, b_res+2,
  896. pci_cardbus_mem_size, pci_cardbus_mem_size);
  897. }
  898. /* reduce that to half */
  899. b_res_3_size = pci_cardbus_mem_size;
  900. }
  901. handle_b_res_3:
  902. if (b_res[3].parent)
  903. goto handle_done;
  904. b_res[3].start = pci_cardbus_mem_size;
  905. b_res[3].end = b_res[3].start + b_res_3_size - 1;
  906. b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_STARTALIGN;
  907. if (realloc_head) {
  908. b_res[3].end -= b_res_3_size;
  909. add_to_list(realloc_head, bridge, b_res+3, b_res_3_size,
  910. pci_cardbus_mem_size);
  911. }
  912. handle_done:
  913. ;
  914. }
  915. void __ref __pci_bus_size_bridges(struct pci_bus *bus,
  916. struct list_head *realloc_head)
  917. {
  918. struct pci_dev *dev;
  919. unsigned long mask, prefmask;
  920. resource_size_t additional_mem_size = 0, additional_io_size = 0;
  921. list_for_each_entry(dev, &bus->devices, bus_list) {
  922. struct pci_bus *b = dev->subordinate;
  923. if (!b)
  924. continue;
  925. switch (dev->class >> 8) {
  926. case PCI_CLASS_BRIDGE_CARDBUS:
  927. pci_bus_size_cardbus(b, realloc_head);
  928. break;
  929. case PCI_CLASS_BRIDGE_PCI:
  930. default:
  931. __pci_bus_size_bridges(b, realloc_head);
  932. break;
  933. }
  934. }
  935. /* The root bus? */
  936. if (!bus->self)
  937. return;
  938. switch (bus->self->class >> 8) {
  939. case PCI_CLASS_BRIDGE_CARDBUS:
  940. /* don't size cardbuses yet. */
  941. break;
  942. case PCI_CLASS_BRIDGE_PCI:
  943. pci_bridge_check_ranges(bus);
  944. if (bus->self->is_hotplug_bridge) {
  945. additional_io_size = pci_hotplug_io_size;
  946. additional_mem_size = pci_hotplug_mem_size;
  947. }
  948. /*
  949. * Follow thru
  950. */
  951. default:
  952. pbus_size_io(bus, realloc_head ? 0 : additional_io_size,
  953. additional_io_size, realloc_head);
  954. /* If the bridge supports prefetchable range, size it
  955. separately. If it doesn't, or its prefetchable window
  956. has already been allocated by arch code, try
  957. non-prefetchable range for both types of PCI memory
  958. resources. */
  959. mask = IORESOURCE_MEM;
  960. prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
  961. if (pbus_size_mem(bus, prefmask, prefmask,
  962. realloc_head ? 0 : additional_mem_size,
  963. additional_mem_size, realloc_head))
  964. mask = prefmask; /* Success, size non-prefetch only. */
  965. else
  966. additional_mem_size += additional_mem_size;
  967. pbus_size_mem(bus, mask, IORESOURCE_MEM,
  968. realloc_head ? 0 : additional_mem_size,
  969. additional_mem_size, realloc_head);
  970. break;
  971. }
  972. }
  973. void __ref pci_bus_size_bridges(struct pci_bus *bus)
  974. {
  975. __pci_bus_size_bridges(bus, NULL);
  976. }
  977. EXPORT_SYMBOL(pci_bus_size_bridges);
  978. static void __ref __pci_bus_assign_resources(const struct pci_bus *bus,
  979. struct list_head *realloc_head,
  980. struct list_head *fail_head)
  981. {
  982. struct pci_bus *b;
  983. struct pci_dev *dev;
  984. pbus_assign_resources_sorted(bus, realloc_head, fail_head);
  985. list_for_each_entry(dev, &bus->devices, bus_list) {
  986. b = dev->subordinate;
  987. if (!b)
  988. continue;
  989. __pci_bus_assign_resources(b, realloc_head, fail_head);
  990. switch (dev->class >> 8) {
  991. case PCI_CLASS_BRIDGE_PCI:
  992. if (!pci_is_enabled(dev))
  993. pci_setup_bridge(b);
  994. break;
  995. case PCI_CLASS_BRIDGE_CARDBUS:
  996. pci_setup_cardbus(b);
  997. break;
  998. default:
  999. dev_info(&dev->dev, "not setting up bridge for bus "
  1000. "%04x:%02x\n", pci_domain_nr(b), b->number);
  1001. break;
  1002. }
  1003. }
  1004. }
  1005. void __ref pci_bus_assign_resources(const struct pci_bus *bus)
  1006. {
  1007. __pci_bus_assign_resources(bus, NULL, NULL);
  1008. }
  1009. EXPORT_SYMBOL(pci_bus_assign_resources);
  1010. static void __ref __pci_bridge_assign_resources(const struct pci_dev *bridge,
  1011. struct list_head *add_head,
  1012. struct list_head *fail_head)
  1013. {
  1014. struct pci_bus *b;
  1015. pdev_assign_resources_sorted((struct pci_dev *)bridge,
  1016. add_head, fail_head);
  1017. b = bridge->subordinate;
  1018. if (!b)
  1019. return;
  1020. __pci_bus_assign_resources(b, add_head, fail_head);
  1021. switch (bridge->class >> 8) {
  1022. case PCI_CLASS_BRIDGE_PCI:
  1023. pci_setup_bridge(b);
  1024. break;
  1025. case PCI_CLASS_BRIDGE_CARDBUS:
  1026. pci_setup_cardbus(b);
  1027. break;
  1028. default:
  1029. dev_info(&bridge->dev, "not setting up bridge for bus "
  1030. "%04x:%02x\n", pci_domain_nr(b), b->number);
  1031. break;
  1032. }
  1033. }
  1034. static void pci_bridge_release_resources(struct pci_bus *bus,
  1035. unsigned long type)
  1036. {
  1037. int idx;
  1038. bool changed = false;
  1039. struct pci_dev *dev;
  1040. struct resource *r;
  1041. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
  1042. IORESOURCE_PREFETCH;
  1043. dev = bus->self;
  1044. for (idx = PCI_BRIDGE_RESOURCES; idx <= PCI_BRIDGE_RESOURCE_END;
  1045. idx++) {
  1046. r = &dev->resource[idx];
  1047. if ((r->flags & type_mask) != type)
  1048. continue;
  1049. if (!r->parent)
  1050. continue;
  1051. /*
  1052. * if there are children under that, we should release them
  1053. * all
  1054. */
  1055. release_child_resources(r);
  1056. if (!release_resource(r)) {
  1057. dev_printk(KERN_DEBUG, &dev->dev,
  1058. "resource %d %pR released\n", idx, r);
  1059. /* keep the old size */
  1060. r->end = resource_size(r) - 1;
  1061. r->start = 0;
  1062. r->flags = 0;
  1063. changed = true;
  1064. }
  1065. }
  1066. if (changed) {
  1067. /* avoiding touch the one without PREF */
  1068. if (type & IORESOURCE_PREFETCH)
  1069. type = IORESOURCE_PREFETCH;
  1070. __pci_setup_bridge(bus, type);
  1071. }
  1072. }
  1073. enum release_type {
  1074. leaf_only,
  1075. whole_subtree,
  1076. };
  1077. /*
  1078. * try to release pci bridge resources that is from leaf bridge,
  1079. * so we can allocate big new one later
  1080. */
  1081. static void __ref pci_bus_release_bridge_resources(struct pci_bus *bus,
  1082. unsigned long type,
  1083. enum release_type rel_type)
  1084. {
  1085. struct pci_dev *dev;
  1086. bool is_leaf_bridge = true;
  1087. list_for_each_entry(dev, &bus->devices, bus_list) {
  1088. struct pci_bus *b = dev->subordinate;
  1089. if (!b)
  1090. continue;
  1091. is_leaf_bridge = false;
  1092. if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  1093. continue;
  1094. if (rel_type == whole_subtree)
  1095. pci_bus_release_bridge_resources(b, type,
  1096. whole_subtree);
  1097. }
  1098. if (pci_is_root_bus(bus))
  1099. return;
  1100. if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  1101. return;
  1102. if ((rel_type == whole_subtree) || is_leaf_bridge)
  1103. pci_bridge_release_resources(bus, type);
  1104. }
  1105. static void pci_bus_dump_res(struct pci_bus *bus)
  1106. {
  1107. struct resource *res;
  1108. int i;
  1109. pci_bus_for_each_resource(bus, res, i) {
  1110. if (!res || !res->end || !res->flags)
  1111. continue;
  1112. dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res);
  1113. }
  1114. }
  1115. static void pci_bus_dump_resources(struct pci_bus *bus)
  1116. {
  1117. struct pci_bus *b;
  1118. struct pci_dev *dev;
  1119. pci_bus_dump_res(bus);
  1120. list_for_each_entry(dev, &bus->devices, bus_list) {
  1121. b = dev->subordinate;
  1122. if (!b)
  1123. continue;
  1124. pci_bus_dump_resources(b);
  1125. }
  1126. }
  1127. static int __init pci_bus_get_depth(struct pci_bus *bus)
  1128. {
  1129. int depth = 0;
  1130. struct pci_dev *dev;
  1131. list_for_each_entry(dev, &bus->devices, bus_list) {
  1132. int ret;
  1133. struct pci_bus *b = dev->subordinate;
  1134. if (!b)
  1135. continue;
  1136. ret = pci_bus_get_depth(b);
  1137. if (ret + 1 > depth)
  1138. depth = ret + 1;
  1139. }
  1140. return depth;
  1141. }
  1142. static int __init pci_get_max_depth(void)
  1143. {
  1144. int depth = 0;
  1145. struct pci_bus *bus;
  1146. list_for_each_entry(bus, &pci_root_buses, node) {
  1147. int ret;
  1148. ret = pci_bus_get_depth(bus);
  1149. if (ret > depth)
  1150. depth = ret;
  1151. }
  1152. return depth;
  1153. }
  1154. /*
  1155. * -1: undefined, will auto detect later
  1156. * 0: disabled by user
  1157. * 1: disabled by auto detect
  1158. * 2: enabled by user
  1159. * 3: enabled by auto detect
  1160. */
  1161. enum enable_type {
  1162. undefined = -1,
  1163. user_disabled,
  1164. auto_disabled,
  1165. user_enabled,
  1166. auto_enabled,
  1167. };
  1168. static enum enable_type pci_realloc_enable __initdata = undefined;
  1169. void __init pci_realloc_get_opt(char *str)
  1170. {
  1171. if (!strncmp(str, "off", 3))
  1172. pci_realloc_enable = user_disabled;
  1173. else if (!strncmp(str, "on", 2))
  1174. pci_realloc_enable = user_enabled;
  1175. }
  1176. static bool __init pci_realloc_enabled(void)
  1177. {
  1178. return pci_realloc_enable >= user_enabled;
  1179. }
  1180. static void __init pci_realloc_detect(void)
  1181. {
  1182. #if defined(CONFIG_PCI_IOV) && defined(CONFIG_PCI_REALLOC_ENABLE_AUTO)
  1183. struct pci_dev *dev = NULL;
  1184. if (pci_realloc_enable != undefined)
  1185. return;
  1186. for_each_pci_dev(dev) {
  1187. int i;
  1188. for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++) {
  1189. struct resource *r = &dev->resource[i];
  1190. /* Not assigned, or rejected by kernel ? */
  1191. if (r->flags && !r->start) {
  1192. pci_realloc_enable = auto_enabled;
  1193. return;
  1194. }
  1195. }
  1196. }
  1197. #endif
  1198. }
  1199. /*
  1200. * first try will not touch pci bridge res
  1201. * second and later try will clear small leaf bridge res
  1202. * will stop till to the max deepth if can not find good one
  1203. */
  1204. void __init
  1205. pci_assign_unassigned_resources(void)
  1206. {
  1207. struct pci_bus *bus;
  1208. LIST_HEAD(realloc_head); /* list of resources that
  1209. want additional resources */
  1210. struct list_head *add_list = NULL;
  1211. int tried_times = 0;
  1212. enum release_type rel_type = leaf_only;
  1213. LIST_HEAD(fail_head);
  1214. struct pci_dev_resource *fail_res;
  1215. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
  1216. IORESOURCE_PREFETCH;
  1217. int pci_try_num = 1;
  1218. /* don't realloc if asked to do so */
  1219. pci_realloc_detect();
  1220. if (pci_realloc_enabled()) {
  1221. int max_depth = pci_get_max_depth();
  1222. pci_try_num = max_depth + 1;
  1223. printk(KERN_DEBUG "PCI: max bus depth: %d pci_try_num: %d\n",
  1224. max_depth, pci_try_num);
  1225. }
  1226. again:
  1227. /*
  1228. * last try will use add_list, otherwise will try good to have as
  1229. * must have, so can realloc parent bridge resource
  1230. */
  1231. if (tried_times + 1 == pci_try_num)
  1232. add_list = &realloc_head;
  1233. /* Depth first, calculate sizes and alignments of all
  1234. subordinate buses. */
  1235. list_for_each_entry(bus, &pci_root_buses, node)
  1236. __pci_bus_size_bridges(bus, add_list);
  1237. /* Depth last, allocate resources and update the hardware. */
  1238. list_for_each_entry(bus, &pci_root_buses, node)
  1239. __pci_bus_assign_resources(bus, add_list, &fail_head);
  1240. if (add_list)
  1241. BUG_ON(!list_empty(add_list));
  1242. tried_times++;
  1243. /* any device complain? */
  1244. if (list_empty(&fail_head))
  1245. goto enable_and_dump;
  1246. if (tried_times >= pci_try_num) {
  1247. if (pci_realloc_enable == undefined)
  1248. printk(KERN_INFO "Some PCI device resources are unassigned, try booting with pci=realloc\n");
  1249. else if (pci_realloc_enable == auto_enabled)
  1250. printk(KERN_INFO "Automatically enabled pci realloc, if you have problem, try booting with pci=realloc=off\n");
  1251. free_list(&fail_head);
  1252. goto enable_and_dump;
  1253. }
  1254. printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
  1255. tried_times + 1);
  1256. /* third times and later will not check if it is leaf */
  1257. if ((tried_times + 1) > 2)
  1258. rel_type = whole_subtree;
  1259. /*
  1260. * Try to release leaf bridge's resources that doesn't fit resource of
  1261. * child device under that bridge
  1262. */
  1263. list_for_each_entry(fail_res, &fail_head, list) {
  1264. bus = fail_res->dev->bus;
  1265. pci_bus_release_bridge_resources(bus,
  1266. fail_res->flags & type_mask,
  1267. rel_type);
  1268. }
  1269. /* restore size and flags */
  1270. list_for_each_entry(fail_res, &fail_head, list) {
  1271. struct resource *res = fail_res->res;
  1272. res->start = fail_res->start;
  1273. res->end = fail_res->end;
  1274. res->flags = fail_res->flags;
  1275. if (fail_res->dev->subordinate)
  1276. res->flags = 0;
  1277. }
  1278. free_list(&fail_head);
  1279. goto again;
  1280. enable_and_dump:
  1281. /* Depth last, update the hardware. */
  1282. list_for_each_entry(bus, &pci_root_buses, node)
  1283. pci_enable_bridges(bus);
  1284. /* dump the resource on buses */
  1285. list_for_each_entry(bus, &pci_root_buses, node)
  1286. pci_bus_dump_resources(bus);
  1287. }
  1288. void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
  1289. {
  1290. struct pci_bus *parent = bridge->subordinate;
  1291. LIST_HEAD(add_list); /* list of resources that
  1292. want additional resources */
  1293. int tried_times = 0;
  1294. LIST_HEAD(fail_head);
  1295. struct pci_dev_resource *fail_res;
  1296. int retval;
  1297. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
  1298. IORESOURCE_PREFETCH;
  1299. again:
  1300. __pci_bus_size_bridges(parent, &add_list);
  1301. __pci_bridge_assign_resources(bridge, &add_list, &fail_head);
  1302. BUG_ON(!list_empty(&add_list));
  1303. tried_times++;
  1304. if (list_empty(&fail_head))
  1305. goto enable_all;
  1306. if (tried_times >= 2) {
  1307. /* still fail, don't need to try more */
  1308. free_list(&fail_head);
  1309. goto enable_all;
  1310. }
  1311. printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
  1312. tried_times + 1);
  1313. /*
  1314. * Try to release leaf bridge's resources that doesn't fit resource of
  1315. * child device under that bridge
  1316. */
  1317. list_for_each_entry(fail_res, &fail_head, list) {
  1318. struct pci_bus *bus = fail_res->dev->bus;
  1319. unsigned long flags = fail_res->flags;
  1320. pci_bus_release_bridge_resources(bus, flags & type_mask,
  1321. whole_subtree);
  1322. }
  1323. /* restore size and flags */
  1324. list_for_each_entry(fail_res, &fail_head, list) {
  1325. struct resource *res = fail_res->res;
  1326. res->start = fail_res->start;
  1327. res->end = fail_res->end;
  1328. res->flags = fail_res->flags;
  1329. if (fail_res->dev->subordinate)
  1330. res->flags = 0;
  1331. }
  1332. free_list(&fail_head);
  1333. goto again;
  1334. enable_all:
  1335. retval = pci_reenable_device(bridge);
  1336. pci_set_master(bridge);
  1337. pci_enable_bridges(parent);
  1338. }
  1339. EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
  1340. #ifdef CONFIG_HOTPLUG
  1341. /**
  1342. * pci_rescan_bus - scan a PCI bus for devices.
  1343. * @bus: PCI bus to scan
  1344. *
  1345. * Scan a PCI bus and child buses for new devices, adds them,
  1346. * and enables them.
  1347. *
  1348. * Returns the max number of subordinate bus discovered.
  1349. */
  1350. unsigned int __ref pci_rescan_bus(struct pci_bus *bus)
  1351. {
  1352. unsigned int max;
  1353. struct pci_dev *dev;
  1354. LIST_HEAD(add_list); /* list of resources that
  1355. want additional resources */
  1356. max = pci_scan_child_bus(bus);
  1357. down_read(&pci_bus_sem);
  1358. list_for_each_entry(dev, &bus->devices, bus_list)
  1359. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  1360. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
  1361. if (dev->subordinate)
  1362. __pci_bus_size_bridges(dev->subordinate,
  1363. &add_list);
  1364. up_read(&pci_bus_sem);
  1365. __pci_bus_assign_resources(bus, &add_list, NULL);
  1366. BUG_ON(!list_empty(&add_list));
  1367. pci_enable_bridges(bus);
  1368. pci_bus_add_devices(bus);
  1369. return max;
  1370. }
  1371. EXPORT_SYMBOL_GPL(pci_rescan_bus);
  1372. #endif