setup-bus.c 31 KB

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