setup-bus.c 38 KB

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