setup-bus.c 41 KB

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