setup-bus.c 31 KB

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