setup-bus.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  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, 0, additional_io_size, realloc_head);
  841. /* If the bridge supports prefetchable range, size it
  842. separately. If it doesn't, or its prefetchable window
  843. has already been allocated by arch code, try
  844. non-prefetchable range for both types of PCI memory
  845. resources. */
  846. mask = IORESOURCE_MEM;
  847. prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
  848. if (pbus_size_mem(bus, prefmask, prefmask, 0, additional_mem_size, realloc_head))
  849. mask = prefmask; /* Success, size non-prefetch only. */
  850. else
  851. additional_mem_size += additional_mem_size;
  852. pbus_size_mem(bus, mask, IORESOURCE_MEM, 0, additional_mem_size, realloc_head);
  853. break;
  854. }
  855. }
  856. void __ref pci_bus_size_bridges(struct pci_bus *bus)
  857. {
  858. __pci_bus_size_bridges(bus, NULL);
  859. }
  860. EXPORT_SYMBOL(pci_bus_size_bridges);
  861. static void __ref __pci_bus_assign_resources(const struct pci_bus *bus,
  862. struct resource_list_x *realloc_head,
  863. struct resource_list_x *fail_head)
  864. {
  865. struct pci_bus *b;
  866. struct pci_dev *dev;
  867. pbus_assign_resources_sorted(bus, realloc_head, fail_head);
  868. list_for_each_entry(dev, &bus->devices, bus_list) {
  869. b = dev->subordinate;
  870. if (!b)
  871. continue;
  872. __pci_bus_assign_resources(b, realloc_head, fail_head);
  873. switch (dev->class >> 8) {
  874. case PCI_CLASS_BRIDGE_PCI:
  875. if (!pci_is_enabled(dev))
  876. pci_setup_bridge(b);
  877. break;
  878. case PCI_CLASS_BRIDGE_CARDBUS:
  879. pci_setup_cardbus(b);
  880. break;
  881. default:
  882. dev_info(&dev->dev, "not setting up bridge for bus "
  883. "%04x:%02x\n", pci_domain_nr(b), b->number);
  884. break;
  885. }
  886. }
  887. }
  888. void __ref pci_bus_assign_resources(const struct pci_bus *bus)
  889. {
  890. __pci_bus_assign_resources(bus, NULL, NULL);
  891. }
  892. EXPORT_SYMBOL(pci_bus_assign_resources);
  893. static void __ref __pci_bridge_assign_resources(const struct pci_dev *bridge,
  894. struct resource_list_x *add_head,
  895. struct resource_list_x *fail_head)
  896. {
  897. struct pci_bus *b;
  898. pdev_assign_resources_sorted((struct pci_dev *)bridge,
  899. add_head, fail_head);
  900. b = bridge->subordinate;
  901. if (!b)
  902. return;
  903. __pci_bus_assign_resources(b, add_head, fail_head);
  904. switch (bridge->class >> 8) {
  905. case PCI_CLASS_BRIDGE_PCI:
  906. pci_setup_bridge(b);
  907. break;
  908. case PCI_CLASS_BRIDGE_CARDBUS:
  909. pci_setup_cardbus(b);
  910. break;
  911. default:
  912. dev_info(&bridge->dev, "not setting up bridge for bus "
  913. "%04x:%02x\n", pci_domain_nr(b), b->number);
  914. break;
  915. }
  916. }
  917. static void pci_bridge_release_resources(struct pci_bus *bus,
  918. unsigned long type)
  919. {
  920. int idx;
  921. bool changed = false;
  922. struct pci_dev *dev;
  923. struct resource *r;
  924. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
  925. IORESOURCE_PREFETCH;
  926. dev = bus->self;
  927. for (idx = PCI_BRIDGE_RESOURCES; idx <= PCI_BRIDGE_RESOURCE_END;
  928. idx++) {
  929. r = &dev->resource[idx];
  930. if ((r->flags & type_mask) != type)
  931. continue;
  932. if (!r->parent)
  933. continue;
  934. /*
  935. * if there are children under that, we should release them
  936. * all
  937. */
  938. release_child_resources(r);
  939. if (!release_resource(r)) {
  940. dev_printk(KERN_DEBUG, &dev->dev,
  941. "resource %d %pR released\n", idx, r);
  942. /* keep the old size */
  943. r->end = resource_size(r) - 1;
  944. r->start = 0;
  945. r->flags = 0;
  946. changed = true;
  947. }
  948. }
  949. if (changed) {
  950. /* avoiding touch the one without PREF */
  951. if (type & IORESOURCE_PREFETCH)
  952. type = IORESOURCE_PREFETCH;
  953. __pci_setup_bridge(bus, type);
  954. }
  955. }
  956. enum release_type {
  957. leaf_only,
  958. whole_subtree,
  959. };
  960. /*
  961. * try to release pci bridge resources that is from leaf bridge,
  962. * so we can allocate big new one later
  963. */
  964. static void __ref pci_bus_release_bridge_resources(struct pci_bus *bus,
  965. unsigned long type,
  966. enum release_type rel_type)
  967. {
  968. struct pci_dev *dev;
  969. bool is_leaf_bridge = true;
  970. list_for_each_entry(dev, &bus->devices, bus_list) {
  971. struct pci_bus *b = dev->subordinate;
  972. if (!b)
  973. continue;
  974. is_leaf_bridge = false;
  975. if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  976. continue;
  977. if (rel_type == whole_subtree)
  978. pci_bus_release_bridge_resources(b, type,
  979. whole_subtree);
  980. }
  981. if (pci_is_root_bus(bus))
  982. return;
  983. if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  984. return;
  985. if ((rel_type == whole_subtree) || is_leaf_bridge)
  986. pci_bridge_release_resources(bus, type);
  987. }
  988. static void pci_bus_dump_res(struct pci_bus *bus)
  989. {
  990. struct resource *res;
  991. int i;
  992. pci_bus_for_each_resource(bus, res, i) {
  993. if (!res || !res->end || !res->flags)
  994. continue;
  995. dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res);
  996. }
  997. }
  998. static void pci_bus_dump_resources(struct pci_bus *bus)
  999. {
  1000. struct pci_bus *b;
  1001. struct pci_dev *dev;
  1002. pci_bus_dump_res(bus);
  1003. list_for_each_entry(dev, &bus->devices, bus_list) {
  1004. b = dev->subordinate;
  1005. if (!b)
  1006. continue;
  1007. pci_bus_dump_resources(b);
  1008. }
  1009. }
  1010. static int __init pci_bus_get_depth(struct pci_bus *bus)
  1011. {
  1012. int depth = 0;
  1013. struct pci_dev *dev;
  1014. list_for_each_entry(dev, &bus->devices, bus_list) {
  1015. int ret;
  1016. struct pci_bus *b = dev->subordinate;
  1017. if (!b)
  1018. continue;
  1019. ret = pci_bus_get_depth(b);
  1020. if (ret + 1 > depth)
  1021. depth = ret + 1;
  1022. }
  1023. return depth;
  1024. }
  1025. static int __init pci_get_max_depth(void)
  1026. {
  1027. int depth = 0;
  1028. struct pci_bus *bus;
  1029. list_for_each_entry(bus, &pci_root_buses, node) {
  1030. int ret;
  1031. ret = pci_bus_get_depth(bus);
  1032. if (ret > depth)
  1033. depth = ret;
  1034. }
  1035. return depth;
  1036. }
  1037. /*
  1038. * first try will not touch pci bridge res
  1039. * second and later try will clear small leaf bridge res
  1040. * will stop till to the max deepth if can not find good one
  1041. */
  1042. void __init
  1043. pci_assign_unassigned_resources(void)
  1044. {
  1045. struct pci_bus *bus;
  1046. struct resource_list_x realloc_list; /* list of resources that
  1047. want additional resources */
  1048. int tried_times = 0;
  1049. enum release_type rel_type = leaf_only;
  1050. struct resource_list_x head, *list;
  1051. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
  1052. IORESOURCE_PREFETCH;
  1053. unsigned long failed_type;
  1054. int max_depth = pci_get_max_depth();
  1055. int pci_try_num;
  1056. head.next = NULL;
  1057. realloc_list.next = NULL;
  1058. pci_try_num = max_depth + 1;
  1059. printk(KERN_DEBUG "PCI: max bus depth: %d pci_try_num: %d\n",
  1060. max_depth, pci_try_num);
  1061. again:
  1062. /* Depth first, calculate sizes and alignments of all
  1063. subordinate buses. */
  1064. list_for_each_entry(bus, &pci_root_buses, node)
  1065. __pci_bus_size_bridges(bus, &realloc_list);
  1066. /* Depth last, allocate resources and update the hardware. */
  1067. list_for_each_entry(bus, &pci_root_buses, node)
  1068. __pci_bus_assign_resources(bus, &realloc_list, &head);
  1069. BUG_ON(realloc_list.next);
  1070. tried_times++;
  1071. /* any device complain? */
  1072. if (!head.next)
  1073. goto enable_and_dump;
  1074. /* don't realloc if asked to do so */
  1075. if (!pci_realloc_enabled()) {
  1076. free_list(resource_list_x, &head);
  1077. goto enable_and_dump;
  1078. }
  1079. failed_type = 0;
  1080. for (list = head.next; list;) {
  1081. failed_type |= list->flags;
  1082. list = list->next;
  1083. }
  1084. /*
  1085. * io port are tight, don't try extra
  1086. * or if reach the limit, don't want to try more
  1087. */
  1088. failed_type &= type_mask;
  1089. if ((failed_type == IORESOURCE_IO) || (tried_times >= pci_try_num)) {
  1090. free_list(resource_list_x, &head);
  1091. goto enable_and_dump;
  1092. }
  1093. printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
  1094. tried_times + 1);
  1095. /* third times and later will not check if it is leaf */
  1096. if ((tried_times + 1) > 2)
  1097. rel_type = whole_subtree;
  1098. /*
  1099. * Try to release leaf bridge's resources that doesn't fit resource of
  1100. * child device under that bridge
  1101. */
  1102. for (list = head.next; list;) {
  1103. bus = list->dev->bus;
  1104. pci_bus_release_bridge_resources(bus, list->flags & type_mask,
  1105. rel_type);
  1106. list = list->next;
  1107. }
  1108. /* restore size and flags */
  1109. for (list = head.next; list;) {
  1110. struct resource *res = list->res;
  1111. res->start = list->start;
  1112. res->end = list->end;
  1113. res->flags = list->flags;
  1114. if (list->dev->subordinate)
  1115. res->flags = 0;
  1116. list = list->next;
  1117. }
  1118. free_list(resource_list_x, &head);
  1119. goto again;
  1120. enable_and_dump:
  1121. /* Depth last, update the hardware. */
  1122. list_for_each_entry(bus, &pci_root_buses, node)
  1123. pci_enable_bridges(bus);
  1124. /* dump the resource on buses */
  1125. list_for_each_entry(bus, &pci_root_buses, node)
  1126. pci_bus_dump_resources(bus);
  1127. }
  1128. void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
  1129. {
  1130. struct pci_bus *parent = bridge->subordinate;
  1131. struct resource_list_x add_list; /* list of resources that
  1132. want additional resources */
  1133. int tried_times = 0;
  1134. struct resource_list_x head, *list;
  1135. int retval;
  1136. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
  1137. IORESOURCE_PREFETCH;
  1138. head.next = NULL;
  1139. add_list.next = NULL;
  1140. again:
  1141. __pci_bus_size_bridges(parent, &add_list);
  1142. __pci_bridge_assign_resources(bridge, &add_list, &head);
  1143. BUG_ON(add_list.next);
  1144. tried_times++;
  1145. if (!head.next)
  1146. goto enable_all;
  1147. if (tried_times >= 2) {
  1148. /* still fail, don't need to try more */
  1149. free_list(resource_list_x, &head);
  1150. goto enable_all;
  1151. }
  1152. printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
  1153. tried_times + 1);
  1154. /*
  1155. * Try to release leaf bridge's resources that doesn't fit resource of
  1156. * child device under that bridge
  1157. */
  1158. for (list = head.next; list;) {
  1159. struct pci_bus *bus = list->dev->bus;
  1160. unsigned long flags = list->flags;
  1161. pci_bus_release_bridge_resources(bus, flags & type_mask,
  1162. whole_subtree);
  1163. list = list->next;
  1164. }
  1165. /* restore size and flags */
  1166. for (list = head.next; list;) {
  1167. struct resource *res = list->res;
  1168. res->start = list->start;
  1169. res->end = list->end;
  1170. res->flags = list->flags;
  1171. if (list->dev->subordinate)
  1172. res->flags = 0;
  1173. list = list->next;
  1174. }
  1175. free_list(resource_list_x, &head);
  1176. goto again;
  1177. enable_all:
  1178. retval = pci_reenable_device(bridge);
  1179. pci_set_master(bridge);
  1180. pci_enable_bridges(parent);
  1181. }
  1182. EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);