setup-bus.c 31 KB

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