setup-bus.c 24 KB

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