setup-bus.c 47 KB

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