setup-bus.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  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. dev_info(&bridge->dev, " bridge window [io disabled]\n");
  303. }
  304. /* Temporarily disable the I/O range before updating PCI_IO_BASE. */
  305. pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
  306. /* Update lower 16 bits of I/O base/limit. */
  307. pci_write_config_dword(bridge, PCI_IO_BASE, l);
  308. /* Update upper 16 bits of I/O base/limit. */
  309. pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
  310. }
  311. static void pci_setup_bridge_mmio(struct pci_bus *bus)
  312. {
  313. struct pci_dev *bridge = bus->self;
  314. struct resource *res;
  315. struct pci_bus_region region;
  316. u32 l;
  317. /* Set up the top and bottom of the PCI Memory segment for this bus. */
  318. res = bus->resource[1];
  319. pcibios_resource_to_bus(bridge, &region, res);
  320. if (res->flags & IORESOURCE_MEM) {
  321. l = (region.start >> 16) & 0xfff0;
  322. l |= region.end & 0xfff00000;
  323. dev_info(&bridge->dev, " bridge window %pR\n", res);
  324. } else {
  325. l = 0x0000fff0;
  326. dev_info(&bridge->dev, " bridge window [mem disabled]\n");
  327. }
  328. pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
  329. }
  330. static void pci_setup_bridge_mmio_pref(struct pci_bus *bus)
  331. {
  332. struct pci_dev *bridge = bus->self;
  333. struct resource *res;
  334. struct pci_bus_region region;
  335. u32 l, bu, lu;
  336. /* Clear out the upper 32 bits of PREF limit.
  337. If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
  338. disables PREF range, which is ok. */
  339. pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
  340. /* Set up PREF base/limit. */
  341. bu = lu = 0;
  342. res = bus->resource[2];
  343. pcibios_resource_to_bus(bridge, &region, res);
  344. if (res->flags & IORESOURCE_PREFETCH) {
  345. l = (region.start >> 16) & 0xfff0;
  346. l |= region.end & 0xfff00000;
  347. if (res->flags & IORESOURCE_MEM_64) {
  348. bu = upper_32_bits(region.start);
  349. lu = upper_32_bits(region.end);
  350. }
  351. dev_info(&bridge->dev, " bridge window %pR\n", res);
  352. } else {
  353. l = 0x0000fff0;
  354. dev_info(&bridge->dev, " bridge window [mem pref disabled]\n");
  355. }
  356. pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
  357. /* Set the upper 32 bits of PREF base & limit. */
  358. pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
  359. pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
  360. }
  361. static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type)
  362. {
  363. struct pci_dev *bridge = bus->self;
  364. dev_info(&bridge->dev, "PCI bridge to [bus %02x-%02x]\n",
  365. bus->secondary, bus->subordinate);
  366. if (type & IORESOURCE_IO)
  367. pci_setup_bridge_io(bus);
  368. if (type & IORESOURCE_MEM)
  369. pci_setup_bridge_mmio(bus);
  370. if (type & IORESOURCE_PREFETCH)
  371. pci_setup_bridge_mmio_pref(bus);
  372. pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
  373. }
  374. static void pci_setup_bridge(struct pci_bus *bus)
  375. {
  376. unsigned long type = IORESOURCE_IO | IORESOURCE_MEM |
  377. IORESOURCE_PREFETCH;
  378. __pci_setup_bridge(bus, type);
  379. }
  380. /* Check whether the bridge supports optional I/O and
  381. prefetchable memory ranges. If not, the respective
  382. base/limit registers must be read-only and read as 0. */
  383. static void pci_bridge_check_ranges(struct pci_bus *bus)
  384. {
  385. u16 io;
  386. u32 pmem;
  387. struct pci_dev *bridge = bus->self;
  388. struct resource *b_res;
  389. b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
  390. b_res[1].flags |= IORESOURCE_MEM;
  391. pci_read_config_word(bridge, PCI_IO_BASE, &io);
  392. if (!io) {
  393. pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0);
  394. pci_read_config_word(bridge, PCI_IO_BASE, &io);
  395. pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
  396. }
  397. if (io)
  398. b_res[0].flags |= IORESOURCE_IO;
  399. /* DECchip 21050 pass 2 errata: the bridge may miss an address
  400. disconnect boundary by one PCI data phase.
  401. Workaround: do not use prefetching on this device. */
  402. if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001)
  403. return;
  404. pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
  405. if (!pmem) {
  406. pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE,
  407. 0xfff0fff0);
  408. pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
  409. pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0);
  410. }
  411. if (pmem) {
  412. b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
  413. if ((pmem & PCI_PREF_RANGE_TYPE_MASK) ==
  414. PCI_PREF_RANGE_TYPE_64) {
  415. b_res[2].flags |= IORESOURCE_MEM_64;
  416. b_res[2].flags |= PCI_PREF_RANGE_TYPE_64;
  417. }
  418. }
  419. /* double check if bridge does support 64 bit pref */
  420. if (b_res[2].flags & IORESOURCE_MEM_64) {
  421. u32 mem_base_hi, tmp;
  422. pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32,
  423. &mem_base_hi);
  424. pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
  425. 0xffffffff);
  426. pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp);
  427. if (!tmp)
  428. b_res[2].flags &= ~IORESOURCE_MEM_64;
  429. pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
  430. mem_base_hi);
  431. }
  432. }
  433. /* Helper function for sizing routines: find first available
  434. bus resource of a given type. Note: we intentionally skip
  435. the bus resources which have already been assigned (that is,
  436. have non-NULL parent resource). */
  437. static struct resource *find_free_bus_resource(struct pci_bus *bus, unsigned long type)
  438. {
  439. int i;
  440. struct resource *r;
  441. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
  442. IORESOURCE_PREFETCH;
  443. pci_bus_for_each_resource(bus, r, i) {
  444. if (r == &ioport_resource || r == &iomem_resource)
  445. continue;
  446. if (r && (r->flags & type_mask) == type && !r->parent)
  447. return r;
  448. }
  449. return NULL;
  450. }
  451. static resource_size_t calculate_iosize(resource_size_t size,
  452. resource_size_t min_size,
  453. resource_size_t size1,
  454. resource_size_t old_size,
  455. resource_size_t align)
  456. {
  457. if (size < min_size)
  458. size = min_size;
  459. if (old_size == 1 )
  460. old_size = 0;
  461. /* To be fixed in 2.5: we should have sort of HAVE_ISA
  462. flag in the struct pci_bus. */
  463. #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
  464. size = (size & 0xff) + ((size & ~0xffUL) << 2);
  465. #endif
  466. size = ALIGN(size + size1, align);
  467. if (size < old_size)
  468. size = old_size;
  469. return size;
  470. }
  471. static resource_size_t calculate_memsize(resource_size_t size,
  472. resource_size_t min_size,
  473. resource_size_t size1,
  474. resource_size_t old_size,
  475. resource_size_t align)
  476. {
  477. if (size < min_size)
  478. size = min_size;
  479. if (old_size == 1 )
  480. old_size = 0;
  481. if (size < old_size)
  482. size = old_size;
  483. size = ALIGN(size + size1, align);
  484. return size;
  485. }
  486. /**
  487. * pbus_size_io() - size the io window of a given bus
  488. *
  489. * @bus : the bus
  490. * @min_size : the minimum io window that must to be allocated
  491. * @add_size : additional optional io window
  492. * @add_head : track the additional io window on this list
  493. *
  494. * Sizing the IO windows of the PCI-PCI bridge is trivial,
  495. * since these windows have 4K granularity and the IO ranges
  496. * of non-bridge PCI devices are limited to 256 bytes.
  497. * We must be careful with the ISA aliasing though.
  498. */
  499. static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size,
  500. resource_size_t add_size, struct resource_list_x *add_head)
  501. {
  502. struct pci_dev *dev;
  503. struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO);
  504. unsigned long size = 0, size0 = 0, size1 = 0;
  505. if (!b_res)
  506. return;
  507. list_for_each_entry(dev, &bus->devices, bus_list) {
  508. int i;
  509. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  510. struct resource *r = &dev->resource[i];
  511. unsigned long r_size;
  512. if (r->parent || !(r->flags & IORESOURCE_IO))
  513. continue;
  514. r_size = resource_size(r);
  515. if (r_size < 0x400)
  516. /* Might be re-aligned for ISA */
  517. size += r_size;
  518. else
  519. size1 += r_size;
  520. }
  521. }
  522. size0 = calculate_iosize(size, min_size, size1,
  523. resource_size(b_res), 4096);
  524. size1 = (!add_head || (add_head && !add_size)) ? size0 :
  525. calculate_iosize(size, min_size+add_size, size1,
  526. resource_size(b_res), 4096);
  527. if (!size0 && !size1) {
  528. if (b_res->start || b_res->end)
  529. dev_info(&bus->self->dev, "disabling bridge window "
  530. "%pR to [bus %02x-%02x] (unused)\n", b_res,
  531. bus->secondary, bus->subordinate);
  532. b_res->flags = 0;
  533. return;
  534. }
  535. /* Alignment of the IO window is always 4K */
  536. b_res->start = 4096;
  537. b_res->end = b_res->start + size0 - 1;
  538. b_res->flags |= IORESOURCE_STARTALIGN;
  539. if (size1 > size0 && add_head)
  540. add_to_list(add_head, bus->self, b_res, size1-size0);
  541. }
  542. /**
  543. * pbus_size_mem() - size the memory window of a given bus
  544. *
  545. * @bus : the bus
  546. * @min_size : the minimum memory window that must to be allocated
  547. * @add_size : additional optional memory window
  548. * @add_head : track the additional memory window on this list
  549. *
  550. * Calculate the size of the bus and minimal alignment which
  551. * guarantees that all child resources fit in this size.
  552. */
  553. static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
  554. unsigned long type, resource_size_t min_size,
  555. resource_size_t add_size,
  556. struct resource_list_x *add_head)
  557. {
  558. struct pci_dev *dev;
  559. resource_size_t min_align, align, size, size0, size1;
  560. resource_size_t aligns[12]; /* Alignments from 1Mb to 2Gb */
  561. int order, max_order;
  562. struct resource *b_res = find_free_bus_resource(bus, type);
  563. unsigned int mem64_mask = 0;
  564. if (!b_res)
  565. return 0;
  566. memset(aligns, 0, sizeof(aligns));
  567. max_order = 0;
  568. size = 0;
  569. mem64_mask = b_res->flags & IORESOURCE_MEM_64;
  570. b_res->flags &= ~IORESOURCE_MEM_64;
  571. list_for_each_entry(dev, &bus->devices, bus_list) {
  572. int i;
  573. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  574. struct resource *r = &dev->resource[i];
  575. resource_size_t r_size;
  576. if (r->parent || (r->flags & mask) != type)
  577. continue;
  578. r_size = resource_size(r);
  579. /* For bridges size != alignment */
  580. align = pci_resource_alignment(dev, r);
  581. order = __ffs(align) - 20;
  582. if (order > 11) {
  583. dev_warn(&dev->dev, "disabling BAR %d: %pR "
  584. "(bad alignment %#llx)\n", i, r,
  585. (unsigned long long) align);
  586. r->flags = 0;
  587. continue;
  588. }
  589. size += r_size;
  590. if (order < 0)
  591. order = 0;
  592. /* Exclude ranges with size > align from
  593. calculation of the alignment. */
  594. if (r_size == align)
  595. aligns[order] += align;
  596. if (order > max_order)
  597. max_order = order;
  598. mem64_mask &= r->flags & IORESOURCE_MEM_64;
  599. }
  600. }
  601. align = 0;
  602. min_align = 0;
  603. for (order = 0; order <= max_order; order++) {
  604. resource_size_t align1 = 1;
  605. align1 <<= (order + 20);
  606. if (!align)
  607. min_align = align1;
  608. else if (ALIGN(align + min_align, min_align) < align1)
  609. min_align = align1 >> 1;
  610. align += aligns[order];
  611. }
  612. size0 = calculate_memsize(size, min_size, 0, resource_size(b_res), min_align);
  613. size1 = (!add_head || (add_head && !add_size)) ? size0 :
  614. calculate_memsize(size, min_size+add_size, 0,
  615. resource_size(b_res), min_align);
  616. if (!size0 && !size1) {
  617. if (b_res->start || b_res->end)
  618. dev_info(&bus->self->dev, "disabling bridge window "
  619. "%pR to [bus %02x-%02x] (unused)\n", b_res,
  620. bus->secondary, bus->subordinate);
  621. b_res->flags = 0;
  622. return 1;
  623. }
  624. b_res->start = min_align;
  625. b_res->end = size0 + min_align - 1;
  626. b_res->flags |= IORESOURCE_STARTALIGN | mem64_mask;
  627. if (size1 > size0 && add_head)
  628. add_to_list(add_head, bus->self, b_res, size1-size0);
  629. return 1;
  630. }
  631. static void pci_bus_size_cardbus(struct pci_bus *bus)
  632. {
  633. struct pci_dev *bridge = bus->self;
  634. struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
  635. u16 ctrl;
  636. /*
  637. * Reserve some resources for CardBus. We reserve
  638. * a fixed amount of bus space for CardBus bridges.
  639. */
  640. b_res[0].start = 0;
  641. b_res[0].end = pci_cardbus_io_size - 1;
  642. b_res[0].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
  643. b_res[1].start = 0;
  644. b_res[1].end = pci_cardbus_io_size - 1;
  645. b_res[1].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
  646. /*
  647. * Check whether prefetchable memory is supported
  648. * by this bridge.
  649. */
  650. pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
  651. if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
  652. ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
  653. pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
  654. pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
  655. }
  656. /*
  657. * If we have prefetchable memory support, allocate
  658. * two regions. Otherwise, allocate one region of
  659. * twice the size.
  660. */
  661. if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
  662. b_res[2].start = 0;
  663. b_res[2].end = pci_cardbus_mem_size - 1;
  664. b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_SIZEALIGN;
  665. b_res[3].start = 0;
  666. b_res[3].end = pci_cardbus_mem_size - 1;
  667. b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
  668. } else {
  669. b_res[3].start = 0;
  670. b_res[3].end = pci_cardbus_mem_size * 2 - 1;
  671. b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
  672. }
  673. }
  674. void __ref __pci_bus_size_bridges(struct pci_bus *bus,
  675. struct resource_list_x *add_head)
  676. {
  677. struct pci_dev *dev;
  678. unsigned long mask, prefmask;
  679. resource_size_t additional_mem_size = 0, additional_io_size = 0;
  680. list_for_each_entry(dev, &bus->devices, bus_list) {
  681. struct pci_bus *b = dev->subordinate;
  682. if (!b)
  683. continue;
  684. switch (dev->class >> 8) {
  685. case PCI_CLASS_BRIDGE_CARDBUS:
  686. pci_bus_size_cardbus(b);
  687. break;
  688. case PCI_CLASS_BRIDGE_PCI:
  689. default:
  690. __pci_bus_size_bridges(b, add_head);
  691. break;
  692. }
  693. }
  694. /* The root bus? */
  695. if (!bus->self)
  696. return;
  697. switch (bus->self->class >> 8) {
  698. case PCI_CLASS_BRIDGE_CARDBUS:
  699. /* don't size cardbuses yet. */
  700. break;
  701. case PCI_CLASS_BRIDGE_PCI:
  702. pci_bridge_check_ranges(bus);
  703. if (bus->self->is_hotplug_bridge) {
  704. additional_io_size = pci_hotplug_io_size;
  705. additional_mem_size = pci_hotplug_mem_size;
  706. }
  707. /*
  708. * Follow thru
  709. */
  710. default:
  711. pbus_size_io(bus, 0, additional_io_size, add_head);
  712. /* If the bridge supports prefetchable range, size it
  713. separately. If it doesn't, or its prefetchable window
  714. has already been allocated by arch code, try
  715. non-prefetchable range for both types of PCI memory
  716. resources. */
  717. mask = IORESOURCE_MEM;
  718. prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
  719. if (pbus_size_mem(bus, prefmask, prefmask, 0, additional_mem_size, add_head))
  720. mask = prefmask; /* Success, size non-prefetch only. */
  721. else
  722. additional_mem_size += additional_mem_size;
  723. pbus_size_mem(bus, mask, IORESOURCE_MEM, 0, additional_mem_size, add_head);
  724. break;
  725. }
  726. }
  727. void __ref pci_bus_size_bridges(struct pci_bus *bus)
  728. {
  729. __pci_bus_size_bridges(bus, NULL);
  730. }
  731. EXPORT_SYMBOL(pci_bus_size_bridges);
  732. static void __ref __pci_bus_assign_resources(const struct pci_bus *bus,
  733. struct resource_list_x *add_head,
  734. struct resource_list_x *fail_head)
  735. {
  736. struct pci_bus *b;
  737. struct pci_dev *dev;
  738. pbus_assign_resources_sorted(bus, add_head, fail_head);
  739. list_for_each_entry(dev, &bus->devices, bus_list) {
  740. b = dev->subordinate;
  741. if (!b)
  742. continue;
  743. __pci_bus_assign_resources(b, add_head, fail_head);
  744. switch (dev->class >> 8) {
  745. case PCI_CLASS_BRIDGE_PCI:
  746. if (!pci_is_enabled(dev))
  747. pci_setup_bridge(b);
  748. break;
  749. case PCI_CLASS_BRIDGE_CARDBUS:
  750. pci_setup_cardbus(b);
  751. break;
  752. default:
  753. dev_info(&dev->dev, "not setting up bridge for bus "
  754. "%04x:%02x\n", pci_domain_nr(b), b->number);
  755. break;
  756. }
  757. }
  758. }
  759. void __ref pci_bus_assign_resources(const struct pci_bus *bus)
  760. {
  761. __pci_bus_assign_resources(bus, NULL, NULL);
  762. }
  763. EXPORT_SYMBOL(pci_bus_assign_resources);
  764. static void __ref __pci_bridge_assign_resources(const struct pci_dev *bridge,
  765. struct resource_list_x *fail_head)
  766. {
  767. struct pci_bus *b;
  768. pdev_assign_resources_sorted((struct pci_dev *)bridge, fail_head);
  769. b = bridge->subordinate;
  770. if (!b)
  771. return;
  772. __pci_bus_assign_resources(b, NULL, fail_head);
  773. switch (bridge->class >> 8) {
  774. case PCI_CLASS_BRIDGE_PCI:
  775. pci_setup_bridge(b);
  776. break;
  777. case PCI_CLASS_BRIDGE_CARDBUS:
  778. pci_setup_cardbus(b);
  779. break;
  780. default:
  781. dev_info(&bridge->dev, "not setting up bridge for bus "
  782. "%04x:%02x\n", pci_domain_nr(b), b->number);
  783. break;
  784. }
  785. }
  786. static void pci_bridge_release_resources(struct pci_bus *bus,
  787. unsigned long type)
  788. {
  789. int idx;
  790. bool changed = false;
  791. struct pci_dev *dev;
  792. struct resource *r;
  793. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
  794. IORESOURCE_PREFETCH;
  795. dev = bus->self;
  796. for (idx = PCI_BRIDGE_RESOURCES; idx <= PCI_BRIDGE_RESOURCE_END;
  797. idx++) {
  798. r = &dev->resource[idx];
  799. if ((r->flags & type_mask) != type)
  800. continue;
  801. if (!r->parent)
  802. continue;
  803. /*
  804. * if there are children under that, we should release them
  805. * all
  806. */
  807. release_child_resources(r);
  808. if (!release_resource(r)) {
  809. dev_printk(KERN_DEBUG, &dev->dev,
  810. "resource %d %pR released\n", idx, r);
  811. /* keep the old size */
  812. r->end = resource_size(r) - 1;
  813. r->start = 0;
  814. r->flags = 0;
  815. changed = true;
  816. }
  817. }
  818. if (changed) {
  819. /* avoiding touch the one without PREF */
  820. if (type & IORESOURCE_PREFETCH)
  821. type = IORESOURCE_PREFETCH;
  822. __pci_setup_bridge(bus, type);
  823. }
  824. }
  825. enum release_type {
  826. leaf_only,
  827. whole_subtree,
  828. };
  829. /*
  830. * try to release pci bridge resources that is from leaf bridge,
  831. * so we can allocate big new one later
  832. */
  833. static void __ref pci_bus_release_bridge_resources(struct pci_bus *bus,
  834. unsigned long type,
  835. enum release_type rel_type)
  836. {
  837. struct pci_dev *dev;
  838. bool is_leaf_bridge = true;
  839. list_for_each_entry(dev, &bus->devices, bus_list) {
  840. struct pci_bus *b = dev->subordinate;
  841. if (!b)
  842. continue;
  843. is_leaf_bridge = false;
  844. if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  845. continue;
  846. if (rel_type == whole_subtree)
  847. pci_bus_release_bridge_resources(b, type,
  848. whole_subtree);
  849. }
  850. if (pci_is_root_bus(bus))
  851. return;
  852. if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  853. return;
  854. if ((rel_type == whole_subtree) || is_leaf_bridge)
  855. pci_bridge_release_resources(bus, type);
  856. }
  857. static void pci_bus_dump_res(struct pci_bus *bus)
  858. {
  859. struct resource *res;
  860. int i;
  861. pci_bus_for_each_resource(bus, res, i) {
  862. if (!res || !res->end || !res->flags)
  863. continue;
  864. dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res);
  865. }
  866. }
  867. static void pci_bus_dump_resources(struct pci_bus *bus)
  868. {
  869. struct pci_bus *b;
  870. struct pci_dev *dev;
  871. pci_bus_dump_res(bus);
  872. list_for_each_entry(dev, &bus->devices, bus_list) {
  873. b = dev->subordinate;
  874. if (!b)
  875. continue;
  876. pci_bus_dump_resources(b);
  877. }
  878. }
  879. static int __init pci_bus_get_depth(struct pci_bus *bus)
  880. {
  881. int depth = 0;
  882. struct pci_dev *dev;
  883. list_for_each_entry(dev, &bus->devices, bus_list) {
  884. int ret;
  885. struct pci_bus *b = dev->subordinate;
  886. if (!b)
  887. continue;
  888. ret = pci_bus_get_depth(b);
  889. if (ret + 1 > depth)
  890. depth = ret + 1;
  891. }
  892. return depth;
  893. }
  894. static int __init pci_get_max_depth(void)
  895. {
  896. int depth = 0;
  897. struct pci_bus *bus;
  898. list_for_each_entry(bus, &pci_root_buses, node) {
  899. int ret;
  900. ret = pci_bus_get_depth(bus);
  901. if (ret > depth)
  902. depth = ret;
  903. }
  904. return depth;
  905. }
  906. /*
  907. * first try will not touch pci bridge res
  908. * second and later try will clear small leaf bridge res
  909. * will stop till to the max deepth if can not find good one
  910. */
  911. void __init
  912. pci_assign_unassigned_resources(void)
  913. {
  914. struct pci_bus *bus;
  915. struct resource_list_x add_list; /* list of resources that
  916. want additional resources */
  917. int tried_times = 0;
  918. enum release_type rel_type = leaf_only;
  919. struct resource_list_x head, *list;
  920. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
  921. IORESOURCE_PREFETCH;
  922. unsigned long failed_type;
  923. int max_depth = pci_get_max_depth();
  924. int pci_try_num;
  925. head.next = NULL;
  926. add_list.next = NULL;
  927. pci_try_num = max_depth + 1;
  928. printk(KERN_DEBUG "PCI: max bus depth: %d pci_try_num: %d\n",
  929. max_depth, pci_try_num);
  930. again:
  931. /* Depth first, calculate sizes and alignments of all
  932. subordinate buses. */
  933. list_for_each_entry(bus, &pci_root_buses, node)
  934. __pci_bus_size_bridges(bus, &add_list);
  935. /* Depth last, allocate resources and update the hardware. */
  936. list_for_each_entry(bus, &pci_root_buses, node)
  937. __pci_bus_assign_resources(bus, &add_list, &head);
  938. BUG_ON(add_list.next);
  939. tried_times++;
  940. /* any device complain? */
  941. if (!head.next)
  942. goto enable_and_dump;
  943. /* don't realloc if asked to do so */
  944. if (!pci_realloc_enabled()) {
  945. free_list(resource_list_x, &head);
  946. goto enable_and_dump;
  947. }
  948. failed_type = 0;
  949. for (list = head.next; list;) {
  950. failed_type |= list->flags;
  951. list = list->next;
  952. }
  953. /*
  954. * io port are tight, don't try extra
  955. * or if reach the limit, don't want to try more
  956. */
  957. failed_type &= type_mask;
  958. if ((failed_type == IORESOURCE_IO) || (tried_times >= pci_try_num)) {
  959. free_list(resource_list_x, &head);
  960. goto enable_and_dump;
  961. }
  962. printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
  963. tried_times + 1);
  964. /* third times and later will not check if it is leaf */
  965. if ((tried_times + 1) > 2)
  966. rel_type = whole_subtree;
  967. /*
  968. * Try to release leaf bridge's resources that doesn't fit resource of
  969. * child device under that bridge
  970. */
  971. for (list = head.next; list;) {
  972. bus = list->dev->bus;
  973. pci_bus_release_bridge_resources(bus, list->flags & type_mask,
  974. rel_type);
  975. list = list->next;
  976. }
  977. /* restore size and flags */
  978. for (list = head.next; list;) {
  979. struct resource *res = list->res;
  980. res->start = list->start;
  981. res->end = list->end;
  982. res->flags = list->flags;
  983. if (list->dev->subordinate)
  984. res->flags = 0;
  985. list = list->next;
  986. }
  987. free_list(resource_list_x, &head);
  988. goto again;
  989. enable_and_dump:
  990. /* Depth last, update the hardware. */
  991. list_for_each_entry(bus, &pci_root_buses, node)
  992. pci_enable_bridges(bus);
  993. /* dump the resource on buses */
  994. list_for_each_entry(bus, &pci_root_buses, node)
  995. pci_bus_dump_resources(bus);
  996. }
  997. void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
  998. {
  999. struct pci_bus *parent = bridge->subordinate;
  1000. int tried_times = 0;
  1001. struct resource_list_x head, *list;
  1002. int retval;
  1003. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
  1004. IORESOURCE_PREFETCH;
  1005. head.next = NULL;
  1006. again:
  1007. pci_bus_size_bridges(parent);
  1008. __pci_bridge_assign_resources(bridge, &head);
  1009. tried_times++;
  1010. if (!head.next)
  1011. goto enable_all;
  1012. if (tried_times >= 2) {
  1013. /* still fail, don't need to try more */
  1014. free_list(resource_list_x, &head);
  1015. goto enable_all;
  1016. }
  1017. printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
  1018. tried_times + 1);
  1019. /*
  1020. * Try to release leaf bridge's resources that doesn't fit resource of
  1021. * child device under that bridge
  1022. */
  1023. for (list = head.next; list;) {
  1024. struct pci_bus *bus = list->dev->bus;
  1025. unsigned long flags = list->flags;
  1026. pci_bus_release_bridge_resources(bus, flags & type_mask,
  1027. whole_subtree);
  1028. list = list->next;
  1029. }
  1030. /* restore size and flags */
  1031. for (list = head.next; list;) {
  1032. struct resource *res = list->res;
  1033. res->start = list->start;
  1034. res->end = list->end;
  1035. res->flags = list->flags;
  1036. if (list->dev->subordinate)
  1037. res->flags = 0;
  1038. list = list->next;
  1039. }
  1040. free_list(resource_list_x, &head);
  1041. goto again;
  1042. enable_all:
  1043. retval = pci_reenable_device(bridge);
  1044. pci_set_master(bridge);
  1045. pci_enable_bridges(parent);
  1046. }
  1047. EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);