setup-bus.c 39 KB

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