setup-bus.c 39 KB

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