setup-bus.c 33 KB

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