setup-bus.c 38 KB

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