setup-bus.c 39 KB

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