setup-bus.c 25 KB

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