setup-bus.c 26 KB

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