setup-bus.c 24 KB

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