setup-bus.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640
  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 /* don't care */,
  256. 0 /* don't care */);
  257. }
  258. reset_resource(res);
  259. }
  260. }
  261. }
  262. static unsigned long pci_fail_res_type_mask(struct list_head *fail_head)
  263. {
  264. struct pci_dev_resource *fail_res;
  265. unsigned long mask = 0;
  266. /* check failed type */
  267. list_for_each_entry(fail_res, fail_head, list)
  268. mask |= fail_res->flags;
  269. /*
  270. * one pref failed resource will set IORESOURCE_MEM,
  271. * as we can allocate pref in non-pref range.
  272. * Will release all assigned non-pref sibling resources
  273. * according to that bit.
  274. */
  275. return mask & (IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH);
  276. }
  277. static bool pci_need_to_release(unsigned long mask, struct resource *res)
  278. {
  279. if (res->flags & IORESOURCE_IO)
  280. return !!(mask & IORESOURCE_IO);
  281. /* check pref at first */
  282. if (res->flags & IORESOURCE_PREFETCH) {
  283. if (mask & IORESOURCE_PREFETCH)
  284. return true;
  285. /* count pref if its parent is non-pref */
  286. else if ((mask & IORESOURCE_MEM) &&
  287. !(res->parent->flags & IORESOURCE_PREFETCH))
  288. return true;
  289. else
  290. return false;
  291. }
  292. if (res->flags & IORESOURCE_MEM)
  293. return !!(mask & IORESOURCE_MEM);
  294. return false; /* should not get here */
  295. }
  296. static void __assign_resources_sorted(struct list_head *head,
  297. struct list_head *realloc_head,
  298. struct list_head *fail_head)
  299. {
  300. /*
  301. * Should not assign requested resources at first.
  302. * they could be adjacent, so later reassign can not reallocate
  303. * them one by one in parent resource window.
  304. * Try to assign requested + add_size at beginning
  305. * if could do that, could get out early.
  306. * if could not do that, we still try to assign requested at first,
  307. * then try to reassign add_size for some resources.
  308. *
  309. * Separate three resource type checking if we need to release
  310. * assigned resource after requested + add_size try.
  311. * 1. if there is io port assign fail, will release assigned
  312. * io port.
  313. * 2. if there is pref mmio assign fail, release assigned
  314. * pref mmio.
  315. * if assigned pref mmio's parent is non-pref mmio and there
  316. * is non-pref mmio assign fail, will release that assigned
  317. * pref mmio.
  318. * 3. if there is non-pref mmio assign fail or pref mmio
  319. * assigned fail, will release assigned non-pref mmio.
  320. */
  321. LIST_HEAD(save_head);
  322. LIST_HEAD(local_fail_head);
  323. struct pci_dev_resource *save_res;
  324. struct pci_dev_resource *dev_res, *tmp_res;
  325. unsigned long fail_type;
  326. /* Check if optional add_size is there */
  327. if (!realloc_head || list_empty(realloc_head))
  328. goto requested_and_reassign;
  329. /* Save original start, end, flags etc at first */
  330. list_for_each_entry(dev_res, head, list) {
  331. if (add_to_list(&save_head, dev_res->dev, dev_res->res, 0, 0)) {
  332. free_list(&save_head);
  333. goto requested_and_reassign;
  334. }
  335. }
  336. /* Update res in head list with add_size in realloc_head list */
  337. list_for_each_entry(dev_res, head, list)
  338. dev_res->res->end += get_res_add_size(realloc_head,
  339. dev_res->res);
  340. /* Try updated head list with add_size added */
  341. assign_requested_resources_sorted(head, &local_fail_head);
  342. /* all assigned with add_size ? */
  343. if (list_empty(&local_fail_head)) {
  344. /* Remove head list from realloc_head list */
  345. list_for_each_entry(dev_res, head, list)
  346. remove_from_list(realloc_head, dev_res->res);
  347. free_list(&save_head);
  348. free_list(head);
  349. return;
  350. }
  351. /* check failed type */
  352. fail_type = pci_fail_res_type_mask(&local_fail_head);
  353. /* remove not need to be released assigned res from head list etc */
  354. list_for_each_entry_safe(dev_res, tmp_res, head, list)
  355. if (dev_res->res->parent &&
  356. !pci_need_to_release(fail_type, dev_res->res)) {
  357. /* remove it from realloc_head list */
  358. remove_from_list(realloc_head, dev_res->res);
  359. remove_from_list(&save_head, dev_res->res);
  360. list_del(&dev_res->list);
  361. kfree(dev_res);
  362. }
  363. free_list(&local_fail_head);
  364. /* Release assigned resource */
  365. list_for_each_entry(dev_res, head, list)
  366. if (dev_res->res->parent)
  367. release_resource(dev_res->res);
  368. /* Restore start/end/flags from saved list */
  369. list_for_each_entry(save_res, &save_head, list) {
  370. struct resource *res = save_res->res;
  371. res->start = save_res->start;
  372. res->end = save_res->end;
  373. res->flags = save_res->flags;
  374. }
  375. free_list(&save_head);
  376. requested_and_reassign:
  377. /* Satisfy the must-have resource requests */
  378. assign_requested_resources_sorted(head, fail_head);
  379. /* Try to satisfy any additional optional resource
  380. requests */
  381. if (realloc_head)
  382. reassign_resources_sorted(realloc_head, head);
  383. free_list(head);
  384. }
  385. static void pdev_assign_resources_sorted(struct pci_dev *dev,
  386. struct list_head *add_head,
  387. struct list_head *fail_head)
  388. {
  389. LIST_HEAD(head);
  390. __dev_sort_resources(dev, &head);
  391. __assign_resources_sorted(&head, add_head, fail_head);
  392. }
  393. static void pbus_assign_resources_sorted(const struct pci_bus *bus,
  394. struct list_head *realloc_head,
  395. struct list_head *fail_head)
  396. {
  397. struct pci_dev *dev;
  398. LIST_HEAD(head);
  399. list_for_each_entry(dev, &bus->devices, bus_list)
  400. __dev_sort_resources(dev, &head);
  401. __assign_resources_sorted(&head, realloc_head, fail_head);
  402. }
  403. void pci_setup_cardbus(struct pci_bus *bus)
  404. {
  405. struct pci_dev *bridge = bus->self;
  406. struct resource *res;
  407. struct pci_bus_region region;
  408. dev_info(&bridge->dev, "CardBus bridge to %pR\n",
  409. &bus->busn_res);
  410. res = bus->resource[0];
  411. pcibios_resource_to_bus(bridge->bus, &region, res);
  412. if (res->flags & IORESOURCE_IO) {
  413. /*
  414. * The IO resource is allocated a range twice as large as it
  415. * would normally need. This allows us to set both IO regs.
  416. */
  417. dev_info(&bridge->dev, " bridge window %pR\n", res);
  418. pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
  419. region.start);
  420. pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
  421. region.end);
  422. }
  423. res = bus->resource[1];
  424. pcibios_resource_to_bus(bridge->bus, &region, res);
  425. if (res->flags & IORESOURCE_IO) {
  426. dev_info(&bridge->dev, " bridge window %pR\n", res);
  427. pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
  428. region.start);
  429. pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
  430. region.end);
  431. }
  432. res = bus->resource[2];
  433. pcibios_resource_to_bus(bridge->bus, &region, res);
  434. if (res->flags & IORESOURCE_MEM) {
  435. dev_info(&bridge->dev, " bridge window %pR\n", res);
  436. pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
  437. region.start);
  438. pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
  439. region.end);
  440. }
  441. res = bus->resource[3];
  442. pcibios_resource_to_bus(bridge->bus, &region, res);
  443. if (res->flags & IORESOURCE_MEM) {
  444. dev_info(&bridge->dev, " bridge window %pR\n", res);
  445. pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
  446. region.start);
  447. pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
  448. region.end);
  449. }
  450. }
  451. EXPORT_SYMBOL(pci_setup_cardbus);
  452. /* Initialize bridges with base/limit values we have collected.
  453. PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
  454. requires that if there is no I/O ports or memory behind the
  455. bridge, corresponding range must be turned off by writing base
  456. value greater than limit to the bridge's base/limit registers.
  457. Note: care must be taken when updating I/O base/limit registers
  458. of bridges which support 32-bit I/O. This update requires two
  459. config space writes, so it's quite possible that an I/O window of
  460. the bridge will have some undesirable address (e.g. 0) after the
  461. first write. Ditto 64-bit prefetchable MMIO. */
  462. static void pci_setup_bridge_io(struct pci_bus *bus)
  463. {
  464. struct pci_dev *bridge = bus->self;
  465. struct resource *res;
  466. struct pci_bus_region region;
  467. unsigned long io_mask;
  468. u8 io_base_lo, io_limit_lo;
  469. u16 l;
  470. u32 io_upper16;
  471. io_mask = PCI_IO_RANGE_MASK;
  472. if (bridge->io_window_1k)
  473. io_mask = PCI_IO_1K_RANGE_MASK;
  474. /* Set up the top and bottom of the PCI I/O segment for this bus. */
  475. res = bus->resource[0];
  476. pcibios_resource_to_bus(bridge->bus, &region, res);
  477. if (res->flags & IORESOURCE_IO) {
  478. pci_read_config_word(bridge, PCI_IO_BASE, &l);
  479. io_base_lo = (region.start >> 8) & io_mask;
  480. io_limit_lo = (region.end >> 8) & io_mask;
  481. l = ((u16) io_limit_lo << 8) | io_base_lo;
  482. /* Set up upper 16 bits of I/O base/limit. */
  483. io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
  484. dev_info(&bridge->dev, " bridge window %pR\n", res);
  485. } else {
  486. /* Clear upper 16 bits of I/O base/limit. */
  487. io_upper16 = 0;
  488. l = 0x00f0;
  489. }
  490. /* Temporarily disable the I/O range before updating PCI_IO_BASE. */
  491. pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
  492. /* Update lower 16 bits of I/O base/limit. */
  493. pci_write_config_word(bridge, PCI_IO_BASE, l);
  494. /* Update upper 16 bits of I/O base/limit. */
  495. pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
  496. }
  497. static void pci_setup_bridge_mmio(struct pci_bus *bus)
  498. {
  499. struct pci_dev *bridge = bus->self;
  500. struct resource *res;
  501. struct pci_bus_region region;
  502. u32 l;
  503. /* Set up the top and bottom of the PCI Memory segment for this bus. */
  504. res = bus->resource[1];
  505. pcibios_resource_to_bus(bridge->bus, &region, res);
  506. if (res->flags & IORESOURCE_MEM) {
  507. l = (region.start >> 16) & 0xfff0;
  508. l |= region.end & 0xfff00000;
  509. dev_info(&bridge->dev, " bridge window %pR\n", res);
  510. } else {
  511. l = 0x0000fff0;
  512. }
  513. pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
  514. }
  515. static void pci_setup_bridge_mmio_pref(struct pci_bus *bus)
  516. {
  517. struct pci_dev *bridge = bus->self;
  518. struct resource *res;
  519. struct pci_bus_region region;
  520. u32 l, bu, lu;
  521. /* Clear out the upper 32 bits of PREF limit.
  522. If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
  523. disables PREF range, which is ok. */
  524. pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
  525. /* Set up PREF base/limit. */
  526. bu = lu = 0;
  527. res = bus->resource[2];
  528. pcibios_resource_to_bus(bridge->bus, &region, res);
  529. if (res->flags & IORESOURCE_PREFETCH) {
  530. l = (region.start >> 16) & 0xfff0;
  531. l |= region.end & 0xfff00000;
  532. if (res->flags & IORESOURCE_MEM_64) {
  533. bu = upper_32_bits(region.start);
  534. lu = upper_32_bits(region.end);
  535. }
  536. dev_info(&bridge->dev, " bridge window %pR\n", res);
  537. } else {
  538. l = 0x0000fff0;
  539. }
  540. pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
  541. /* Set the upper 32 bits of PREF base & limit. */
  542. pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
  543. pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
  544. }
  545. static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type)
  546. {
  547. struct pci_dev *bridge = bus->self;
  548. dev_info(&bridge->dev, "PCI bridge to %pR\n",
  549. &bus->busn_res);
  550. if (type & IORESOURCE_IO)
  551. pci_setup_bridge_io(bus);
  552. if (type & IORESOURCE_MEM)
  553. pci_setup_bridge_mmio(bus);
  554. if (type & IORESOURCE_PREFETCH)
  555. pci_setup_bridge_mmio_pref(bus);
  556. pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
  557. }
  558. void pci_setup_bridge(struct pci_bus *bus)
  559. {
  560. unsigned long type = IORESOURCE_IO | IORESOURCE_MEM |
  561. IORESOURCE_PREFETCH;
  562. __pci_setup_bridge(bus, type);
  563. }
  564. /* Check whether the bridge supports optional I/O and
  565. prefetchable memory ranges. If not, the respective
  566. base/limit registers must be read-only and read as 0. */
  567. static void pci_bridge_check_ranges(struct pci_bus *bus)
  568. {
  569. u16 io;
  570. u32 pmem;
  571. struct pci_dev *bridge = bus->self;
  572. struct resource *b_res;
  573. b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
  574. b_res[1].flags |= IORESOURCE_MEM;
  575. pci_read_config_word(bridge, PCI_IO_BASE, &io);
  576. if (!io) {
  577. pci_write_config_word(bridge, PCI_IO_BASE, 0xe0f0);
  578. pci_read_config_word(bridge, PCI_IO_BASE, &io);
  579. pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
  580. }
  581. if (io)
  582. b_res[0].flags |= IORESOURCE_IO;
  583. /* DECchip 21050 pass 2 errata: the bridge may miss an address
  584. disconnect boundary by one PCI data phase.
  585. Workaround: do not use prefetching on this device. */
  586. if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001)
  587. return;
  588. pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
  589. if (!pmem) {
  590. pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE,
  591. 0xffe0fff0);
  592. pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
  593. pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0);
  594. }
  595. if (pmem) {
  596. b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
  597. if ((pmem & PCI_PREF_RANGE_TYPE_MASK) ==
  598. PCI_PREF_RANGE_TYPE_64) {
  599. b_res[2].flags |= IORESOURCE_MEM_64;
  600. b_res[2].flags |= PCI_PREF_RANGE_TYPE_64;
  601. }
  602. }
  603. /* double check if bridge does support 64 bit pref */
  604. if (b_res[2].flags & IORESOURCE_MEM_64) {
  605. u32 mem_base_hi, tmp;
  606. pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32,
  607. &mem_base_hi);
  608. pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
  609. 0xffffffff);
  610. pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp);
  611. if (!tmp)
  612. b_res[2].flags &= ~IORESOURCE_MEM_64;
  613. pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
  614. mem_base_hi);
  615. }
  616. }
  617. /* Helper function for sizing routines: find first available
  618. bus resource of a given type. Note: we intentionally skip
  619. the bus resources which have already been assigned (that is,
  620. have non-NULL parent resource). */
  621. static struct resource *find_free_bus_resource(struct pci_bus *bus, unsigned long type)
  622. {
  623. int i;
  624. struct resource *r;
  625. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
  626. IORESOURCE_PREFETCH;
  627. pci_bus_for_each_resource(bus, r, i) {
  628. if (r == &ioport_resource || r == &iomem_resource)
  629. continue;
  630. if (r && (r->flags & type_mask) == type && !r->parent)
  631. return r;
  632. }
  633. return NULL;
  634. }
  635. static resource_size_t calculate_iosize(resource_size_t size,
  636. resource_size_t min_size,
  637. resource_size_t size1,
  638. resource_size_t old_size,
  639. resource_size_t align)
  640. {
  641. if (size < min_size)
  642. size = min_size;
  643. if (old_size == 1 )
  644. old_size = 0;
  645. /* To be fixed in 2.5: we should have sort of HAVE_ISA
  646. flag in the struct pci_bus. */
  647. #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
  648. size = (size & 0xff) + ((size & ~0xffUL) << 2);
  649. #endif
  650. size = ALIGN(size + size1, align);
  651. if (size < old_size)
  652. size = old_size;
  653. return size;
  654. }
  655. static resource_size_t calculate_memsize(resource_size_t size,
  656. resource_size_t min_size,
  657. resource_size_t size1,
  658. resource_size_t old_size,
  659. resource_size_t align)
  660. {
  661. if (size < min_size)
  662. size = min_size;
  663. if (old_size == 1 )
  664. old_size = 0;
  665. if (size < old_size)
  666. size = old_size;
  667. size = ALIGN(size + size1, align);
  668. return size;
  669. }
  670. resource_size_t __weak pcibios_window_alignment(struct pci_bus *bus,
  671. unsigned long type)
  672. {
  673. return 1;
  674. }
  675. #define PCI_P2P_DEFAULT_MEM_ALIGN 0x100000 /* 1MiB */
  676. #define PCI_P2P_DEFAULT_IO_ALIGN 0x1000 /* 4KiB */
  677. #define PCI_P2P_DEFAULT_IO_ALIGN_1K 0x400 /* 1KiB */
  678. static resource_size_t window_alignment(struct pci_bus *bus,
  679. unsigned long type)
  680. {
  681. resource_size_t align = 1, arch_align;
  682. if (type & IORESOURCE_MEM)
  683. align = PCI_P2P_DEFAULT_MEM_ALIGN;
  684. else if (type & IORESOURCE_IO) {
  685. /*
  686. * Per spec, I/O windows are 4K-aligned, but some
  687. * bridges have an extension to support 1K alignment.
  688. */
  689. if (bus->self->io_window_1k)
  690. align = PCI_P2P_DEFAULT_IO_ALIGN_1K;
  691. else
  692. align = PCI_P2P_DEFAULT_IO_ALIGN;
  693. }
  694. arch_align = pcibios_window_alignment(bus, type);
  695. return max(align, arch_align);
  696. }
  697. /**
  698. * pbus_size_io() - size the io window of a given bus
  699. *
  700. * @bus : the bus
  701. * @min_size : the minimum io window that must to be allocated
  702. * @add_size : additional optional io window
  703. * @realloc_head : track the additional io window on this list
  704. *
  705. * Sizing the IO windows of the PCI-PCI bridge is trivial,
  706. * since these windows have 1K or 4K granularity and the IO ranges
  707. * of non-bridge PCI devices are limited to 256 bytes.
  708. * We must be careful with the ISA aliasing though.
  709. */
  710. static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size,
  711. resource_size_t add_size, struct list_head *realloc_head)
  712. {
  713. struct pci_dev *dev;
  714. struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO);
  715. resource_size_t size = 0, size0 = 0, size1 = 0;
  716. resource_size_t children_add_size = 0;
  717. resource_size_t min_align, align;
  718. if (!b_res)
  719. return;
  720. min_align = window_alignment(bus, IORESOURCE_IO);
  721. list_for_each_entry(dev, &bus->devices, bus_list) {
  722. int i;
  723. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  724. struct resource *r = &dev->resource[i];
  725. unsigned long r_size;
  726. if (r->parent || !(r->flags & IORESOURCE_IO))
  727. continue;
  728. r_size = resource_size(r);
  729. if (r_size < 0x400)
  730. /* Might be re-aligned for ISA */
  731. size += r_size;
  732. else
  733. size1 += r_size;
  734. align = pci_resource_alignment(dev, r);
  735. if (align > min_align)
  736. min_align = align;
  737. if (realloc_head)
  738. children_add_size += get_res_add_size(realloc_head, r);
  739. }
  740. }
  741. size0 = calculate_iosize(size, min_size, size1,
  742. resource_size(b_res), min_align);
  743. if (children_add_size > add_size)
  744. add_size = children_add_size;
  745. size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
  746. calculate_iosize(size, min_size, add_size + size1,
  747. resource_size(b_res), min_align);
  748. if (!size0 && !size1) {
  749. if (b_res->start || b_res->end)
  750. dev_info(&bus->self->dev, "disabling bridge window "
  751. "%pR to %pR (unused)\n", b_res,
  752. &bus->busn_res);
  753. b_res->flags = 0;
  754. return;
  755. }
  756. b_res->start = min_align;
  757. b_res->end = b_res->start + size0 - 1;
  758. b_res->flags |= IORESOURCE_STARTALIGN;
  759. if (size1 > size0 && realloc_head) {
  760. add_to_list(realloc_head, bus->self, b_res, size1-size0,
  761. min_align);
  762. dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window "
  763. "%pR to %pR add_size %llx\n", b_res,
  764. &bus->busn_res,
  765. (unsigned long long)size1-size0);
  766. }
  767. }
  768. static inline resource_size_t calculate_mem_align(resource_size_t *aligns,
  769. int max_order)
  770. {
  771. resource_size_t align = 0;
  772. resource_size_t min_align = 0;
  773. int order;
  774. for (order = 0; order <= max_order; order++) {
  775. resource_size_t align1 = 1;
  776. align1 <<= (order + 20);
  777. if (!align)
  778. min_align = align1;
  779. else if (ALIGN(align + min_align, min_align) < align1)
  780. min_align = align1 >> 1;
  781. align += aligns[order];
  782. }
  783. return min_align;
  784. }
  785. /**
  786. * pbus_size_mem() - size the memory window of a given bus
  787. *
  788. * @bus : the bus
  789. * @mask: mask the resource flag, then compare it with type
  790. * @type: the type of free resource from bridge
  791. * @min_size : the minimum memory window that must to be allocated
  792. * @add_size : additional optional memory window
  793. * @realloc_head : track the additional memory window on this list
  794. *
  795. * Calculate the size of the bus and minimal alignment which
  796. * guarantees that all child resources fit in this size.
  797. */
  798. static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
  799. unsigned long type, resource_size_t min_size,
  800. resource_size_t add_size,
  801. struct list_head *realloc_head)
  802. {
  803. struct pci_dev *dev;
  804. resource_size_t min_align, align, size, size0, size1;
  805. resource_size_t aligns[12]; /* Alignments from 1Mb to 2Gb */
  806. int order, max_order;
  807. struct resource *b_res = find_free_bus_resource(bus, type);
  808. unsigned int mem64_mask = 0;
  809. resource_size_t children_add_size = 0;
  810. if (!b_res)
  811. return 0;
  812. memset(aligns, 0, sizeof(aligns));
  813. max_order = 0;
  814. size = 0;
  815. mem64_mask = b_res->flags & IORESOURCE_MEM_64;
  816. b_res->flags &= ~IORESOURCE_MEM_64;
  817. list_for_each_entry(dev, &bus->devices, bus_list) {
  818. int i;
  819. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  820. struct resource *r = &dev->resource[i];
  821. resource_size_t r_size;
  822. if (r->parent || (r->flags & mask) != type)
  823. continue;
  824. r_size = resource_size(r);
  825. #ifdef CONFIG_PCI_IOV
  826. /* put SRIOV requested res to the optional list */
  827. if (realloc_head && i >= PCI_IOV_RESOURCES &&
  828. i <= PCI_IOV_RESOURCE_END) {
  829. r->end = r->start - 1;
  830. add_to_list(realloc_head, dev, r, r_size, 0/* don't care */);
  831. children_add_size += r_size;
  832. continue;
  833. }
  834. #endif
  835. /* For bridges size != alignment */
  836. align = pci_resource_alignment(dev, r);
  837. order = __ffs(align) - 20;
  838. if (order > 11) {
  839. dev_warn(&dev->dev, "disabling BAR %d: %pR "
  840. "(bad alignment %#llx)\n", i, r,
  841. (unsigned long long) align);
  842. r->flags = 0;
  843. continue;
  844. }
  845. size += r_size;
  846. if (order < 0)
  847. order = 0;
  848. /* Exclude ranges with size > align from
  849. calculation of the alignment. */
  850. if (r_size == align)
  851. aligns[order] += align;
  852. if (order > max_order)
  853. max_order = order;
  854. mem64_mask &= r->flags & IORESOURCE_MEM_64;
  855. if (realloc_head)
  856. children_add_size += get_res_add_size(realloc_head, r);
  857. }
  858. }
  859. min_align = calculate_mem_align(aligns, max_order);
  860. min_align = max(min_align, window_alignment(bus, b_res->flags));
  861. size0 = calculate_memsize(size, min_size, 0, resource_size(b_res), min_align);
  862. if (children_add_size > add_size)
  863. add_size = children_add_size;
  864. size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
  865. calculate_memsize(size, min_size, add_size,
  866. resource_size(b_res), min_align);
  867. if (!size0 && !size1) {
  868. if (b_res->start || b_res->end)
  869. dev_info(&bus->self->dev, "disabling bridge window "
  870. "%pR to %pR (unused)\n", b_res,
  871. &bus->busn_res);
  872. b_res->flags = 0;
  873. return 1;
  874. }
  875. b_res->start = min_align;
  876. b_res->end = size0 + min_align - 1;
  877. b_res->flags |= IORESOURCE_STARTALIGN | mem64_mask;
  878. if (size1 > size0 && realloc_head) {
  879. add_to_list(realloc_head, bus->self, b_res, size1-size0, min_align);
  880. dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window "
  881. "%pR to %pR add_size %llx\n", b_res,
  882. &bus->busn_res, (unsigned long long)size1-size0);
  883. }
  884. return 1;
  885. }
  886. unsigned long pci_cardbus_resource_alignment(struct resource *res)
  887. {
  888. if (res->flags & IORESOURCE_IO)
  889. return pci_cardbus_io_size;
  890. if (res->flags & IORESOURCE_MEM)
  891. return pci_cardbus_mem_size;
  892. return 0;
  893. }
  894. static void pci_bus_size_cardbus(struct pci_bus *bus,
  895. struct list_head *realloc_head)
  896. {
  897. struct pci_dev *bridge = bus->self;
  898. struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
  899. resource_size_t b_res_3_size = pci_cardbus_mem_size * 2;
  900. u16 ctrl;
  901. if (b_res[0].parent)
  902. goto handle_b_res_1;
  903. /*
  904. * Reserve some resources for CardBus. We reserve
  905. * a fixed amount of bus space for CardBus bridges.
  906. */
  907. b_res[0].start = pci_cardbus_io_size;
  908. b_res[0].end = b_res[0].start + pci_cardbus_io_size - 1;
  909. b_res[0].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
  910. if (realloc_head) {
  911. b_res[0].end -= pci_cardbus_io_size;
  912. add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size,
  913. pci_cardbus_io_size);
  914. }
  915. handle_b_res_1:
  916. if (b_res[1].parent)
  917. goto handle_b_res_2;
  918. b_res[1].start = pci_cardbus_io_size;
  919. b_res[1].end = b_res[1].start + pci_cardbus_io_size - 1;
  920. b_res[1].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
  921. if (realloc_head) {
  922. b_res[1].end -= pci_cardbus_io_size;
  923. add_to_list(realloc_head, bridge, b_res+1, pci_cardbus_io_size,
  924. pci_cardbus_io_size);
  925. }
  926. handle_b_res_2:
  927. /* MEM1 must not be pref mmio */
  928. pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
  929. if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM1) {
  930. ctrl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1;
  931. pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
  932. pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
  933. }
  934. /*
  935. * Check whether prefetchable memory is supported
  936. * by this bridge.
  937. */
  938. pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
  939. if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
  940. ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
  941. pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
  942. pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
  943. }
  944. if (b_res[2].parent)
  945. goto handle_b_res_3;
  946. /*
  947. * If we have prefetchable memory support, allocate
  948. * two regions. Otherwise, allocate one region of
  949. * twice the size.
  950. */
  951. if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
  952. b_res[2].start = pci_cardbus_mem_size;
  953. b_res[2].end = b_res[2].start + pci_cardbus_mem_size - 1;
  954. b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH |
  955. IORESOURCE_STARTALIGN;
  956. if (realloc_head) {
  957. b_res[2].end -= pci_cardbus_mem_size;
  958. add_to_list(realloc_head, bridge, b_res+2,
  959. pci_cardbus_mem_size, pci_cardbus_mem_size);
  960. }
  961. /* reduce that to half */
  962. b_res_3_size = pci_cardbus_mem_size;
  963. }
  964. handle_b_res_3:
  965. if (b_res[3].parent)
  966. goto handle_done;
  967. b_res[3].start = pci_cardbus_mem_size;
  968. b_res[3].end = b_res[3].start + b_res_3_size - 1;
  969. b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_STARTALIGN;
  970. if (realloc_head) {
  971. b_res[3].end -= b_res_3_size;
  972. add_to_list(realloc_head, bridge, b_res+3, b_res_3_size,
  973. pci_cardbus_mem_size);
  974. }
  975. handle_done:
  976. ;
  977. }
  978. void __ref __pci_bus_size_bridges(struct pci_bus *bus,
  979. struct list_head *realloc_head)
  980. {
  981. struct pci_dev *dev;
  982. unsigned long mask, prefmask;
  983. resource_size_t additional_mem_size = 0, additional_io_size = 0;
  984. list_for_each_entry(dev, &bus->devices, bus_list) {
  985. struct pci_bus *b = dev->subordinate;
  986. if (!b)
  987. continue;
  988. switch (dev->class >> 8) {
  989. case PCI_CLASS_BRIDGE_CARDBUS:
  990. pci_bus_size_cardbus(b, realloc_head);
  991. break;
  992. case PCI_CLASS_BRIDGE_PCI:
  993. default:
  994. __pci_bus_size_bridges(b, realloc_head);
  995. break;
  996. }
  997. }
  998. /* The root bus? */
  999. if (pci_is_root_bus(bus))
  1000. return;
  1001. switch (bus->self->class >> 8) {
  1002. case PCI_CLASS_BRIDGE_CARDBUS:
  1003. /* don't size cardbuses yet. */
  1004. break;
  1005. case PCI_CLASS_BRIDGE_PCI:
  1006. pci_bridge_check_ranges(bus);
  1007. if (bus->self->is_hotplug_bridge) {
  1008. additional_io_size = pci_hotplug_io_size;
  1009. additional_mem_size = pci_hotplug_mem_size;
  1010. }
  1011. /*
  1012. * Follow thru
  1013. */
  1014. default:
  1015. pbus_size_io(bus, realloc_head ? 0 : additional_io_size,
  1016. additional_io_size, realloc_head);
  1017. /* If the bridge supports prefetchable range, size it
  1018. separately. If it doesn't, or its prefetchable window
  1019. has already been allocated by arch code, try
  1020. non-prefetchable range for both types of PCI memory
  1021. resources. */
  1022. mask = IORESOURCE_MEM;
  1023. prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
  1024. if (pbus_size_mem(bus, prefmask, prefmask,
  1025. realloc_head ? 0 : additional_mem_size,
  1026. additional_mem_size, realloc_head))
  1027. mask = prefmask; /* Success, size non-prefetch only. */
  1028. else
  1029. additional_mem_size += additional_mem_size;
  1030. pbus_size_mem(bus, mask, IORESOURCE_MEM,
  1031. realloc_head ? 0 : additional_mem_size,
  1032. additional_mem_size, realloc_head);
  1033. break;
  1034. }
  1035. }
  1036. void __ref pci_bus_size_bridges(struct pci_bus *bus)
  1037. {
  1038. __pci_bus_size_bridges(bus, NULL);
  1039. }
  1040. EXPORT_SYMBOL(pci_bus_size_bridges);
  1041. void __ref __pci_bus_assign_resources(const struct pci_bus *bus,
  1042. struct list_head *realloc_head,
  1043. struct list_head *fail_head)
  1044. {
  1045. struct pci_bus *b;
  1046. struct pci_dev *dev;
  1047. pbus_assign_resources_sorted(bus, realloc_head, fail_head);
  1048. list_for_each_entry(dev, &bus->devices, bus_list) {
  1049. b = dev->subordinate;
  1050. if (!b)
  1051. continue;
  1052. __pci_bus_assign_resources(b, realloc_head, fail_head);
  1053. switch (dev->class >> 8) {
  1054. case PCI_CLASS_BRIDGE_PCI:
  1055. if (!pci_is_enabled(dev))
  1056. pci_setup_bridge(b);
  1057. break;
  1058. case PCI_CLASS_BRIDGE_CARDBUS:
  1059. pci_setup_cardbus(b);
  1060. break;
  1061. default:
  1062. dev_info(&dev->dev, "not setting up bridge for bus "
  1063. "%04x:%02x\n", pci_domain_nr(b), b->number);
  1064. break;
  1065. }
  1066. }
  1067. }
  1068. void __ref pci_bus_assign_resources(const struct pci_bus *bus)
  1069. {
  1070. __pci_bus_assign_resources(bus, NULL, NULL);
  1071. }
  1072. EXPORT_SYMBOL(pci_bus_assign_resources);
  1073. static void __ref __pci_bridge_assign_resources(const struct pci_dev *bridge,
  1074. struct list_head *add_head,
  1075. struct list_head *fail_head)
  1076. {
  1077. struct pci_bus *b;
  1078. pdev_assign_resources_sorted((struct pci_dev *)bridge,
  1079. add_head, fail_head);
  1080. b = bridge->subordinate;
  1081. if (!b)
  1082. return;
  1083. __pci_bus_assign_resources(b, add_head, fail_head);
  1084. switch (bridge->class >> 8) {
  1085. case PCI_CLASS_BRIDGE_PCI:
  1086. pci_setup_bridge(b);
  1087. break;
  1088. case PCI_CLASS_BRIDGE_CARDBUS:
  1089. pci_setup_cardbus(b);
  1090. break;
  1091. default:
  1092. dev_info(&bridge->dev, "not setting up bridge for bus "
  1093. "%04x:%02x\n", pci_domain_nr(b), b->number);
  1094. break;
  1095. }
  1096. }
  1097. static void pci_bridge_release_resources(struct pci_bus *bus,
  1098. unsigned long type)
  1099. {
  1100. int idx;
  1101. bool changed = false;
  1102. struct pci_dev *dev;
  1103. struct resource *r;
  1104. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
  1105. IORESOURCE_PREFETCH;
  1106. dev = bus->self;
  1107. for (idx = PCI_BRIDGE_RESOURCES; idx <= PCI_BRIDGE_RESOURCE_END;
  1108. idx++) {
  1109. r = &dev->resource[idx];
  1110. if ((r->flags & type_mask) != type)
  1111. continue;
  1112. if (!r->parent)
  1113. continue;
  1114. /*
  1115. * if there are children under that, we should release them
  1116. * all
  1117. */
  1118. release_child_resources(r);
  1119. if (!release_resource(r)) {
  1120. dev_printk(KERN_DEBUG, &dev->dev,
  1121. "resource %d %pR released\n", idx, r);
  1122. /* keep the old size */
  1123. r->end = resource_size(r) - 1;
  1124. r->start = 0;
  1125. r->flags = 0;
  1126. changed = true;
  1127. }
  1128. }
  1129. if (changed) {
  1130. /* avoiding touch the one without PREF */
  1131. if (type & IORESOURCE_PREFETCH)
  1132. type = IORESOURCE_PREFETCH;
  1133. __pci_setup_bridge(bus, type);
  1134. }
  1135. }
  1136. enum release_type {
  1137. leaf_only,
  1138. whole_subtree,
  1139. };
  1140. /*
  1141. * try to release pci bridge resources that is from leaf bridge,
  1142. * so we can allocate big new one later
  1143. */
  1144. static void __ref pci_bus_release_bridge_resources(struct pci_bus *bus,
  1145. unsigned long type,
  1146. enum release_type rel_type)
  1147. {
  1148. struct pci_dev *dev;
  1149. bool is_leaf_bridge = true;
  1150. list_for_each_entry(dev, &bus->devices, bus_list) {
  1151. struct pci_bus *b = dev->subordinate;
  1152. if (!b)
  1153. continue;
  1154. is_leaf_bridge = false;
  1155. if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  1156. continue;
  1157. if (rel_type == whole_subtree)
  1158. pci_bus_release_bridge_resources(b, type,
  1159. whole_subtree);
  1160. }
  1161. if (pci_is_root_bus(bus))
  1162. return;
  1163. if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  1164. return;
  1165. if ((rel_type == whole_subtree) || is_leaf_bridge)
  1166. pci_bridge_release_resources(bus, type);
  1167. }
  1168. static void pci_bus_dump_res(struct pci_bus *bus)
  1169. {
  1170. struct resource *res;
  1171. int i;
  1172. pci_bus_for_each_resource(bus, res, i) {
  1173. if (!res || !res->end || !res->flags)
  1174. continue;
  1175. dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res);
  1176. }
  1177. }
  1178. static void pci_bus_dump_resources(struct pci_bus *bus)
  1179. {
  1180. struct pci_bus *b;
  1181. struct pci_dev *dev;
  1182. pci_bus_dump_res(bus);
  1183. list_for_each_entry(dev, &bus->devices, bus_list) {
  1184. b = dev->subordinate;
  1185. if (!b)
  1186. continue;
  1187. pci_bus_dump_resources(b);
  1188. }
  1189. }
  1190. static int pci_bus_get_depth(struct pci_bus *bus)
  1191. {
  1192. int depth = 0;
  1193. struct pci_bus *child_bus;
  1194. list_for_each_entry(child_bus, &bus->children, node){
  1195. int ret;
  1196. ret = pci_bus_get_depth(child_bus);
  1197. if (ret + 1 > depth)
  1198. depth = ret + 1;
  1199. }
  1200. return depth;
  1201. }
  1202. /*
  1203. * -1: undefined, will auto detect later
  1204. * 0: disabled by user
  1205. * 1: disabled by auto detect
  1206. * 2: enabled by user
  1207. * 3: enabled by auto detect
  1208. */
  1209. enum enable_type {
  1210. undefined = -1,
  1211. user_disabled,
  1212. auto_disabled,
  1213. user_enabled,
  1214. auto_enabled,
  1215. };
  1216. static enum enable_type pci_realloc_enable = undefined;
  1217. void __init pci_realloc_get_opt(char *str)
  1218. {
  1219. if (!strncmp(str, "off", 3))
  1220. pci_realloc_enable = user_disabled;
  1221. else if (!strncmp(str, "on", 2))
  1222. pci_realloc_enable = user_enabled;
  1223. }
  1224. static bool pci_realloc_enabled(enum enable_type enable)
  1225. {
  1226. return enable >= user_enabled;
  1227. }
  1228. #if defined(CONFIG_PCI_IOV) && defined(CONFIG_PCI_REALLOC_ENABLE_AUTO)
  1229. static int iov_resources_unassigned(struct pci_dev *dev, void *data)
  1230. {
  1231. int i;
  1232. bool *unassigned = data;
  1233. for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++) {
  1234. struct resource *r = &dev->resource[i];
  1235. struct pci_bus_region region;
  1236. /* Not assigned or rejected by kernel? */
  1237. if (!r->flags)
  1238. continue;
  1239. pcibios_resource_to_bus(dev->bus, &region, r);
  1240. if (!region.start) {
  1241. *unassigned = true;
  1242. return 1; /* return early from pci_walk_bus() */
  1243. }
  1244. }
  1245. return 0;
  1246. }
  1247. static enum enable_type pci_realloc_detect(struct pci_bus *bus,
  1248. enum enable_type enable_local)
  1249. {
  1250. bool unassigned = false;
  1251. if (enable_local != undefined)
  1252. return enable_local;
  1253. pci_walk_bus(bus, iov_resources_unassigned, &unassigned);
  1254. if (unassigned)
  1255. return auto_enabled;
  1256. return enable_local;
  1257. }
  1258. #else
  1259. static enum enable_type pci_realloc_detect(struct pci_bus *bus,
  1260. enum enable_type enable_local)
  1261. {
  1262. return enable_local;
  1263. }
  1264. #endif
  1265. /*
  1266. * first try will not touch pci bridge res
  1267. * second and later try will clear small leaf bridge res
  1268. * will stop till to the max depth if can not find good one
  1269. */
  1270. void pci_assign_unassigned_root_bus_resources(struct pci_bus *bus)
  1271. {
  1272. LIST_HEAD(realloc_head); /* list of resources that
  1273. want additional resources */
  1274. struct list_head *add_list = NULL;
  1275. int tried_times = 0;
  1276. enum release_type rel_type = leaf_only;
  1277. LIST_HEAD(fail_head);
  1278. struct pci_dev_resource *fail_res;
  1279. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
  1280. IORESOURCE_PREFETCH;
  1281. int pci_try_num = 1;
  1282. enum enable_type enable_local;
  1283. /* don't realloc if asked to do so */
  1284. enable_local = pci_realloc_detect(bus, pci_realloc_enable);
  1285. if (pci_realloc_enabled(enable_local)) {
  1286. int max_depth = pci_bus_get_depth(bus);
  1287. pci_try_num = max_depth + 1;
  1288. dev_printk(KERN_DEBUG, &bus->dev,
  1289. "max bus depth: %d pci_try_num: %d\n",
  1290. max_depth, pci_try_num);
  1291. }
  1292. again:
  1293. /*
  1294. * last try will use add_list, otherwise will try good to have as
  1295. * must have, so can realloc parent bridge resource
  1296. */
  1297. if (tried_times + 1 == pci_try_num)
  1298. add_list = &realloc_head;
  1299. /* Depth first, calculate sizes and alignments of all
  1300. subordinate buses. */
  1301. __pci_bus_size_bridges(bus, add_list);
  1302. /* Depth last, allocate resources and update the hardware. */
  1303. __pci_bus_assign_resources(bus, add_list, &fail_head);
  1304. if (add_list)
  1305. BUG_ON(!list_empty(add_list));
  1306. tried_times++;
  1307. /* any device complain? */
  1308. if (list_empty(&fail_head))
  1309. goto dump;
  1310. if (tried_times >= pci_try_num) {
  1311. if (enable_local == undefined)
  1312. dev_info(&bus->dev, "Some PCI device resources are unassigned, try booting with pci=realloc\n");
  1313. else if (enable_local == auto_enabled)
  1314. dev_info(&bus->dev, "Automatically enabled pci realloc, if you have problem, try booting with pci=realloc=off\n");
  1315. free_list(&fail_head);
  1316. goto dump;
  1317. }
  1318. dev_printk(KERN_DEBUG, &bus->dev,
  1319. "No. %d try to assign unassigned res\n", tried_times + 1);
  1320. /* third times and later will not check if it is leaf */
  1321. if ((tried_times + 1) > 2)
  1322. rel_type = whole_subtree;
  1323. /*
  1324. * Try to release leaf bridge's resources that doesn't fit resource of
  1325. * child device under that bridge
  1326. */
  1327. list_for_each_entry(fail_res, &fail_head, list)
  1328. pci_bus_release_bridge_resources(fail_res->dev->bus,
  1329. fail_res->flags & type_mask,
  1330. rel_type);
  1331. /* restore size and flags */
  1332. list_for_each_entry(fail_res, &fail_head, list) {
  1333. struct resource *res = fail_res->res;
  1334. res->start = fail_res->start;
  1335. res->end = fail_res->end;
  1336. res->flags = fail_res->flags;
  1337. if (fail_res->dev->subordinate)
  1338. res->flags = 0;
  1339. }
  1340. free_list(&fail_head);
  1341. goto again;
  1342. dump:
  1343. /* dump the resource on buses */
  1344. pci_bus_dump_resources(bus);
  1345. }
  1346. void __init pci_assign_unassigned_resources(void)
  1347. {
  1348. struct pci_bus *root_bus;
  1349. list_for_each_entry(root_bus, &pci_root_buses, node)
  1350. pci_assign_unassigned_root_bus_resources(root_bus);
  1351. }
  1352. void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
  1353. {
  1354. struct pci_bus *parent = bridge->subordinate;
  1355. LIST_HEAD(add_list); /* list of resources that
  1356. want additional resources */
  1357. int tried_times = 0;
  1358. LIST_HEAD(fail_head);
  1359. struct pci_dev_resource *fail_res;
  1360. int retval;
  1361. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
  1362. IORESOURCE_PREFETCH;
  1363. again:
  1364. __pci_bus_size_bridges(parent, &add_list);
  1365. __pci_bridge_assign_resources(bridge, &add_list, &fail_head);
  1366. BUG_ON(!list_empty(&add_list));
  1367. tried_times++;
  1368. if (list_empty(&fail_head))
  1369. goto enable_all;
  1370. if (tried_times >= 2) {
  1371. /* still fail, don't need to try more */
  1372. free_list(&fail_head);
  1373. goto enable_all;
  1374. }
  1375. printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
  1376. tried_times + 1);
  1377. /*
  1378. * Try to release leaf bridge's resources that doesn't fit resource of
  1379. * child device under that bridge
  1380. */
  1381. list_for_each_entry(fail_res, &fail_head, list)
  1382. pci_bus_release_bridge_resources(fail_res->dev->bus,
  1383. fail_res->flags & type_mask,
  1384. whole_subtree);
  1385. /* restore size and flags */
  1386. list_for_each_entry(fail_res, &fail_head, list) {
  1387. struct resource *res = fail_res->res;
  1388. res->start = fail_res->start;
  1389. res->end = fail_res->end;
  1390. res->flags = fail_res->flags;
  1391. if (fail_res->dev->subordinate)
  1392. res->flags = 0;
  1393. }
  1394. free_list(&fail_head);
  1395. goto again;
  1396. enable_all:
  1397. retval = pci_reenable_device(bridge);
  1398. if (retval)
  1399. dev_err(&bridge->dev, "Error reenabling bridge (%d)\n", retval);
  1400. pci_set_master(bridge);
  1401. }
  1402. EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
  1403. void pci_assign_unassigned_bus_resources(struct pci_bus *bus)
  1404. {
  1405. struct pci_dev *dev;
  1406. LIST_HEAD(add_list); /* list of resources that
  1407. want additional resources */
  1408. down_read(&pci_bus_sem);
  1409. list_for_each_entry(dev, &bus->devices, bus_list)
  1410. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  1411. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
  1412. if (dev->subordinate)
  1413. __pci_bus_size_bridges(dev->subordinate,
  1414. &add_list);
  1415. up_read(&pci_bus_sem);
  1416. __pci_bus_assign_resources(bus, &add_list, NULL);
  1417. BUG_ON(!list_empty(&add_list));
  1418. }