setup-bus.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  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) == PCI_PREF_RANGE_TYPE_64)
  311. b_res[2].flags |= IORESOURCE_MEM_64;
  312. }
  313. /* double check if bridge does support 64 bit pref */
  314. if (b_res[2].flags & IORESOURCE_MEM_64) {
  315. u32 mem_base_hi, tmp;
  316. pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32,
  317. &mem_base_hi);
  318. pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
  319. 0xffffffff);
  320. pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp);
  321. if (!tmp)
  322. b_res[2].flags &= ~IORESOURCE_MEM_64;
  323. pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
  324. mem_base_hi);
  325. }
  326. }
  327. /* Helper function for sizing routines: find first available
  328. bus resource of a given type. Note: we intentionally skip
  329. the bus resources which have already been assigned (that is,
  330. have non-NULL parent resource). */
  331. static struct resource *find_free_bus_resource(struct pci_bus *bus, unsigned long type)
  332. {
  333. int i;
  334. struct resource *r;
  335. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
  336. IORESOURCE_PREFETCH;
  337. for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
  338. r = bus->resource[i];
  339. if (r == &ioport_resource || r == &iomem_resource)
  340. continue;
  341. if (r && (r->flags & type_mask) == type && !r->parent)
  342. return r;
  343. }
  344. return NULL;
  345. }
  346. /* Sizing the IO windows of the PCI-PCI bridge is trivial,
  347. since these windows have 4K granularity and the IO ranges
  348. of non-bridge PCI devices are limited to 256 bytes.
  349. We must be careful with the ISA aliasing though. */
  350. static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size)
  351. {
  352. struct pci_dev *dev;
  353. struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO);
  354. unsigned long size = 0, size1 = 0, old_size;
  355. if (!b_res)
  356. return;
  357. list_for_each_entry(dev, &bus->devices, bus_list) {
  358. int i;
  359. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  360. struct resource *r = &dev->resource[i];
  361. unsigned long r_size;
  362. if (r->parent || !(r->flags & IORESOURCE_IO))
  363. continue;
  364. r_size = resource_size(r);
  365. if (r_size < 0x400)
  366. /* Might be re-aligned for ISA */
  367. size += r_size;
  368. else
  369. size1 += r_size;
  370. }
  371. }
  372. if (size < min_size)
  373. size = min_size;
  374. old_size = resource_size(b_res);
  375. if (old_size == 1)
  376. old_size = 0;
  377. /* To be fixed in 2.5: we should have sort of HAVE_ISA
  378. flag in the struct pci_bus. */
  379. #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
  380. size = (size & 0xff) + ((size & ~0xffUL) << 2);
  381. #endif
  382. size = ALIGN(size + size1, 4096);
  383. if (size < old_size)
  384. size = old_size;
  385. if (!size) {
  386. if (b_res->start || b_res->end)
  387. dev_info(&bus->self->dev, "disabling bridge window "
  388. "%pR to [bus %02x-%02x] (unused)\n", b_res,
  389. bus->secondary, bus->subordinate);
  390. b_res->flags = 0;
  391. return;
  392. }
  393. /* Alignment of the IO window is always 4K */
  394. b_res->start = 4096;
  395. b_res->end = b_res->start + size - 1;
  396. b_res->flags |= IORESOURCE_STARTALIGN;
  397. }
  398. /* Calculate the size of the bus and minimal alignment which
  399. guarantees that all child resources fit in this size. */
  400. static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
  401. unsigned long type, resource_size_t min_size)
  402. {
  403. struct pci_dev *dev;
  404. resource_size_t min_align, align, size, old_size;
  405. resource_size_t aligns[12]; /* Alignments from 1Mb to 2Gb */
  406. int order, max_order;
  407. struct resource *b_res = find_free_bus_resource(bus, type);
  408. unsigned int mem64_mask = 0;
  409. if (!b_res)
  410. return 0;
  411. memset(aligns, 0, sizeof(aligns));
  412. max_order = 0;
  413. size = 0;
  414. mem64_mask = b_res->flags & IORESOURCE_MEM_64;
  415. b_res->flags &= ~IORESOURCE_MEM_64;
  416. list_for_each_entry(dev, &bus->devices, bus_list) {
  417. int i;
  418. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  419. struct resource *r = &dev->resource[i];
  420. resource_size_t r_size;
  421. if (r->parent || (r->flags & mask) != type)
  422. continue;
  423. r_size = resource_size(r);
  424. /* For bridges size != alignment */
  425. align = pci_resource_alignment(dev, r);
  426. order = __ffs(align) - 20;
  427. if (order > 11) {
  428. dev_warn(&dev->dev, "disabling BAR %d: %pR "
  429. "(bad alignment %#llx)\n", i, r,
  430. (unsigned long long) align);
  431. r->flags = 0;
  432. continue;
  433. }
  434. size += r_size;
  435. if (order < 0)
  436. order = 0;
  437. /* Exclude ranges with size > align from
  438. calculation of the alignment. */
  439. if (r_size == align)
  440. aligns[order] += align;
  441. if (order > max_order)
  442. max_order = order;
  443. mem64_mask &= r->flags & IORESOURCE_MEM_64;
  444. }
  445. }
  446. if (size < min_size)
  447. size = min_size;
  448. old_size = resource_size(b_res);
  449. if (old_size == 1)
  450. old_size = 0;
  451. if (size < old_size)
  452. size = old_size;
  453. align = 0;
  454. min_align = 0;
  455. for (order = 0; order <= max_order; order++) {
  456. resource_size_t align1 = 1;
  457. align1 <<= (order + 20);
  458. if (!align)
  459. min_align = align1;
  460. else if (ALIGN(align + min_align, min_align) < align1)
  461. min_align = align1 >> 1;
  462. align += aligns[order];
  463. }
  464. size = ALIGN(size, min_align);
  465. if (!size) {
  466. if (b_res->start || b_res->end)
  467. dev_info(&bus->self->dev, "disabling bridge window "
  468. "%pR to [bus %02x-%02x] (unused)\n", b_res,
  469. bus->secondary, bus->subordinate);
  470. b_res->flags = 0;
  471. return 1;
  472. }
  473. b_res->start = min_align;
  474. b_res->end = size + min_align - 1;
  475. b_res->flags |= IORESOURCE_STARTALIGN;
  476. b_res->flags |= mem64_mask;
  477. return 1;
  478. }
  479. static void pci_bus_size_cardbus(struct pci_bus *bus)
  480. {
  481. struct pci_dev *bridge = bus->self;
  482. struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
  483. u16 ctrl;
  484. /*
  485. * Reserve some resources for CardBus. We reserve
  486. * a fixed amount of bus space for CardBus bridges.
  487. */
  488. b_res[0].start = 0;
  489. b_res[0].end = pci_cardbus_io_size - 1;
  490. b_res[0].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
  491. b_res[1].start = 0;
  492. b_res[1].end = pci_cardbus_io_size - 1;
  493. b_res[1].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
  494. /*
  495. * Check whether prefetchable memory is supported
  496. * by this bridge.
  497. */
  498. pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
  499. if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
  500. ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
  501. pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
  502. pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
  503. }
  504. /*
  505. * If we have prefetchable memory support, allocate
  506. * two regions. Otherwise, allocate one region of
  507. * twice the size.
  508. */
  509. if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
  510. b_res[2].start = 0;
  511. b_res[2].end = pci_cardbus_mem_size - 1;
  512. b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_SIZEALIGN;
  513. b_res[3].start = 0;
  514. b_res[3].end = pci_cardbus_mem_size - 1;
  515. b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
  516. } else {
  517. b_res[3].start = 0;
  518. b_res[3].end = pci_cardbus_mem_size * 2 - 1;
  519. b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
  520. }
  521. }
  522. void __ref pci_bus_size_bridges(struct pci_bus *bus)
  523. {
  524. struct pci_dev *dev;
  525. unsigned long mask, prefmask;
  526. resource_size_t min_mem_size = 0, min_io_size = 0;
  527. list_for_each_entry(dev, &bus->devices, bus_list) {
  528. struct pci_bus *b = dev->subordinate;
  529. if (!b)
  530. continue;
  531. switch (dev->class >> 8) {
  532. case PCI_CLASS_BRIDGE_CARDBUS:
  533. pci_bus_size_cardbus(b);
  534. break;
  535. case PCI_CLASS_BRIDGE_PCI:
  536. default:
  537. pci_bus_size_bridges(b);
  538. break;
  539. }
  540. }
  541. /* The root bus? */
  542. if (!bus->self)
  543. return;
  544. switch (bus->self->class >> 8) {
  545. case PCI_CLASS_BRIDGE_CARDBUS:
  546. /* don't size cardbuses yet. */
  547. break;
  548. case PCI_CLASS_BRIDGE_PCI:
  549. pci_bridge_check_ranges(bus);
  550. if (bus->self->is_hotplug_bridge) {
  551. min_io_size = pci_hotplug_io_size;
  552. min_mem_size = pci_hotplug_mem_size;
  553. }
  554. default:
  555. pbus_size_io(bus, min_io_size);
  556. /* If the bridge supports prefetchable range, size it
  557. separately. If it doesn't, or its prefetchable window
  558. has already been allocated by arch code, try
  559. non-prefetchable range for both types of PCI memory
  560. resources. */
  561. mask = IORESOURCE_MEM;
  562. prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
  563. if (pbus_size_mem(bus, prefmask, prefmask, min_mem_size))
  564. mask = prefmask; /* Success, size non-prefetch only. */
  565. else
  566. min_mem_size += min_mem_size;
  567. pbus_size_mem(bus, mask, IORESOURCE_MEM, min_mem_size);
  568. break;
  569. }
  570. }
  571. EXPORT_SYMBOL(pci_bus_size_bridges);
  572. static void __ref __pci_bus_assign_resources(const struct pci_bus *bus,
  573. struct resource_list_x *fail_head)
  574. {
  575. struct pci_bus *b;
  576. struct pci_dev *dev;
  577. pbus_assign_resources_sorted(bus, fail_head);
  578. list_for_each_entry(dev, &bus->devices, bus_list) {
  579. b = dev->subordinate;
  580. if (!b)
  581. continue;
  582. __pci_bus_assign_resources(b, fail_head);
  583. switch (dev->class >> 8) {
  584. case PCI_CLASS_BRIDGE_PCI:
  585. if (!pci_is_enabled(dev))
  586. pci_setup_bridge(b);
  587. break;
  588. case PCI_CLASS_BRIDGE_CARDBUS:
  589. pci_setup_cardbus(b);
  590. break;
  591. default:
  592. dev_info(&dev->dev, "not setting up bridge for bus "
  593. "%04x:%02x\n", pci_domain_nr(b), b->number);
  594. break;
  595. }
  596. }
  597. }
  598. void __ref pci_bus_assign_resources(const struct pci_bus *bus)
  599. {
  600. __pci_bus_assign_resources(bus, NULL);
  601. }
  602. EXPORT_SYMBOL(pci_bus_assign_resources);
  603. static void __ref __pci_bridge_assign_resources(const struct pci_dev *bridge,
  604. struct resource_list_x *fail_head)
  605. {
  606. struct pci_bus *b;
  607. pdev_assign_resources_sorted((struct pci_dev *)bridge, fail_head);
  608. b = bridge->subordinate;
  609. if (!b)
  610. return;
  611. __pci_bus_assign_resources(b, fail_head);
  612. switch (bridge->class >> 8) {
  613. case PCI_CLASS_BRIDGE_PCI:
  614. pci_setup_bridge(b);
  615. break;
  616. case PCI_CLASS_BRIDGE_CARDBUS:
  617. pci_setup_cardbus(b);
  618. break;
  619. default:
  620. dev_info(&bridge->dev, "not setting up bridge for bus "
  621. "%04x:%02x\n", pci_domain_nr(b), b->number);
  622. break;
  623. }
  624. }
  625. static void pci_bridge_release_resources(struct pci_bus *bus,
  626. unsigned long type)
  627. {
  628. int idx;
  629. bool changed = false;
  630. struct pci_dev *dev;
  631. struct resource *r;
  632. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
  633. IORESOURCE_PREFETCH;
  634. dev = bus->self;
  635. for (idx = PCI_BRIDGE_RESOURCES; idx <= PCI_BRIDGE_RESOURCE_END;
  636. idx++) {
  637. r = &dev->resource[idx];
  638. if ((r->flags & type_mask) != type)
  639. continue;
  640. if (!r->parent)
  641. continue;
  642. /*
  643. * if there are children under that, we should release them
  644. * all
  645. */
  646. release_child_resources(r);
  647. if (!release_resource(r)) {
  648. dev_printk(KERN_DEBUG, &dev->dev,
  649. "resource %d %pR released\n", idx, r);
  650. /* keep the old size */
  651. r->end = resource_size(r) - 1;
  652. r->start = 0;
  653. r->flags = 0;
  654. changed = true;
  655. }
  656. }
  657. if (changed) {
  658. /* avoiding touch the one without PREF */
  659. if (type & IORESOURCE_PREFETCH)
  660. type = IORESOURCE_PREFETCH;
  661. __pci_setup_bridge(bus, type);
  662. }
  663. }
  664. enum release_type {
  665. leaf_only,
  666. whole_subtree,
  667. };
  668. /*
  669. * try to release pci bridge resources that is from leaf bridge,
  670. * so we can allocate big new one later
  671. */
  672. static void __ref pci_bus_release_bridge_resources(struct pci_bus *bus,
  673. unsigned long type,
  674. enum release_type rel_type)
  675. {
  676. struct pci_dev *dev;
  677. bool is_leaf_bridge = true;
  678. list_for_each_entry(dev, &bus->devices, bus_list) {
  679. struct pci_bus *b = dev->subordinate;
  680. if (!b)
  681. continue;
  682. is_leaf_bridge = false;
  683. if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  684. continue;
  685. if (rel_type == whole_subtree)
  686. pci_bus_release_bridge_resources(b, type,
  687. whole_subtree);
  688. }
  689. if (pci_is_root_bus(bus))
  690. return;
  691. if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  692. return;
  693. if ((rel_type == whole_subtree) || is_leaf_bridge)
  694. pci_bridge_release_resources(bus, type);
  695. }
  696. static void pci_bus_dump_res(struct pci_bus *bus)
  697. {
  698. int i;
  699. for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
  700. struct resource *res = bus->resource[i];
  701. if (!res || !res->end || !res->flags)
  702. continue;
  703. dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res);
  704. }
  705. }
  706. static void pci_bus_dump_resources(struct pci_bus *bus)
  707. {
  708. struct pci_bus *b;
  709. struct pci_dev *dev;
  710. pci_bus_dump_res(bus);
  711. list_for_each_entry(dev, &bus->devices, bus_list) {
  712. b = dev->subordinate;
  713. if (!b)
  714. continue;
  715. pci_bus_dump_resources(b);
  716. }
  717. }
  718. static int __init pci_bus_get_depth(struct pci_bus *bus)
  719. {
  720. int depth = 0;
  721. struct pci_dev *dev;
  722. list_for_each_entry(dev, &bus->devices, bus_list) {
  723. int ret;
  724. struct pci_bus *b = dev->subordinate;
  725. if (!b)
  726. continue;
  727. ret = pci_bus_get_depth(b);
  728. if (ret + 1 > depth)
  729. depth = ret + 1;
  730. }
  731. return depth;
  732. }
  733. static int __init pci_get_max_depth(void)
  734. {
  735. int depth = 0;
  736. struct pci_bus *bus;
  737. list_for_each_entry(bus, &pci_root_buses, node) {
  738. int ret;
  739. ret = pci_bus_get_depth(bus);
  740. if (ret > depth)
  741. depth = ret;
  742. }
  743. return depth;
  744. }
  745. /*
  746. * first try will not touch pci bridge res
  747. * second and later try will clear small leaf bridge res
  748. * will stop till to the max deepth if can not find good one
  749. */
  750. void __init
  751. pci_assign_unassigned_resources(void)
  752. {
  753. struct pci_bus *bus;
  754. int tried_times = 0;
  755. enum release_type rel_type = leaf_only;
  756. struct resource_list_x head, *list;
  757. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
  758. IORESOURCE_PREFETCH;
  759. unsigned long failed_type;
  760. int max_depth = pci_get_max_depth();
  761. int pci_try_num;
  762. head.next = NULL;
  763. pci_try_num = max_depth + 1;
  764. printk(KERN_DEBUG "PCI: max bus depth: %d pci_try_num: %d\n",
  765. max_depth, pci_try_num);
  766. again:
  767. /* Depth first, calculate sizes and alignments of all
  768. subordinate buses. */
  769. list_for_each_entry(bus, &pci_root_buses, node) {
  770. pci_bus_size_bridges(bus);
  771. }
  772. /* Depth last, allocate resources and update the hardware. */
  773. list_for_each_entry(bus, &pci_root_buses, node) {
  774. __pci_bus_assign_resources(bus, &head);
  775. }
  776. tried_times++;
  777. /* any device complain? */
  778. if (!head.next)
  779. goto enable_and_dump;
  780. failed_type = 0;
  781. for (list = head.next; list;) {
  782. failed_type |= list->flags;
  783. list = list->next;
  784. }
  785. /*
  786. * io port are tight, don't try extra
  787. * or if reach the limit, don't want to try more
  788. */
  789. failed_type &= type_mask;
  790. if ((failed_type == IORESOURCE_IO) || (tried_times >= pci_try_num)) {
  791. free_failed_list(&head);
  792. goto enable_and_dump;
  793. }
  794. printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
  795. tried_times + 1);
  796. /* third times and later will not check if it is leaf */
  797. if ((tried_times + 1) > 2)
  798. rel_type = whole_subtree;
  799. /*
  800. * Try to release leaf bridge's resources that doesn't fit resource of
  801. * child device under that bridge
  802. */
  803. for (list = head.next; list;) {
  804. bus = list->dev->bus;
  805. pci_bus_release_bridge_resources(bus, list->flags & type_mask,
  806. rel_type);
  807. list = list->next;
  808. }
  809. /* restore size and flags */
  810. for (list = head.next; list;) {
  811. struct resource *res = list->res;
  812. res->start = list->start;
  813. res->end = list->end;
  814. res->flags = list->flags;
  815. if (list->dev->subordinate)
  816. res->flags = 0;
  817. list = list->next;
  818. }
  819. free_failed_list(&head);
  820. goto again;
  821. enable_and_dump:
  822. /* Depth last, update the hardware. */
  823. list_for_each_entry(bus, &pci_root_buses, node)
  824. pci_enable_bridges(bus);
  825. /* dump the resource on buses */
  826. list_for_each_entry(bus, &pci_root_buses, node) {
  827. pci_bus_dump_resources(bus);
  828. }
  829. }
  830. void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
  831. {
  832. struct pci_bus *parent = bridge->subordinate;
  833. int retval;
  834. pci_bus_size_bridges(parent);
  835. __pci_bridge_assign_resources(bridge, NULL);
  836. retval = pci_reenable_device(bridge);
  837. pci_set_master(bridge);
  838. pci_enable_bridges(parent);
  839. }
  840. EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);