pci-dma.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. #include <linux/dma-mapping.h>
  2. #include <linux/dma-debug.h>
  3. #include <linux/dmar.h>
  4. #include <linux/bootmem.h>
  5. #include <linux/gfp.h>
  6. #include <linux/pci.h>
  7. #include <linux/kmemleak.h>
  8. #include <asm/proto.h>
  9. #include <asm/dma.h>
  10. #include <asm/iommu.h>
  11. #include <asm/gart.h>
  12. #include <asm/calgary.h>
  13. #include <asm/amd_iommu.h>
  14. #include <asm/x86_init.h>
  15. static int forbid_dac __read_mostly;
  16. struct dma_map_ops *dma_ops = &nommu_dma_ops;
  17. EXPORT_SYMBOL(dma_ops);
  18. static int iommu_sac_force __read_mostly;
  19. #ifdef CONFIG_IOMMU_DEBUG
  20. int panic_on_overflow __read_mostly = 1;
  21. int force_iommu __read_mostly = 1;
  22. #else
  23. int panic_on_overflow __read_mostly = 0;
  24. int force_iommu __read_mostly = 0;
  25. #endif
  26. int iommu_merge __read_mostly = 0;
  27. int no_iommu __read_mostly;
  28. /* Set this to 1 if there is a HW IOMMU in the system */
  29. int iommu_detected __read_mostly = 0;
  30. /*
  31. * This variable becomes 1 if iommu=pt is passed on the kernel command line.
  32. * If this variable is 1, IOMMU implementations do no DMA translation for
  33. * devices and allow every device to access to whole physical memory. This is
  34. * useful if a user wants to use an IOMMU only for KVM device assignment to
  35. * guests and not for driver dma translation.
  36. */
  37. int iommu_pass_through __read_mostly;
  38. /* Dummy device used for NULL arguments (normally ISA). */
  39. struct device x86_dma_fallback_dev = {
  40. .init_name = "fallback device",
  41. .coherent_dma_mask = ISA_DMA_BIT_MASK,
  42. .dma_mask = &x86_dma_fallback_dev.coherent_dma_mask,
  43. };
  44. EXPORT_SYMBOL(x86_dma_fallback_dev);
  45. /* Number of entries preallocated for DMA-API debugging */
  46. #define PREALLOC_DMA_DEBUG_ENTRIES 32768
  47. int dma_set_mask(struct device *dev, u64 mask)
  48. {
  49. if (!dev->dma_mask || !dma_supported(dev, mask))
  50. return -EIO;
  51. *dev->dma_mask = mask;
  52. return 0;
  53. }
  54. EXPORT_SYMBOL(dma_set_mask);
  55. #if defined(CONFIG_X86_64) && !defined(CONFIG_NUMA)
  56. static __initdata void *dma32_bootmem_ptr;
  57. static unsigned long dma32_bootmem_size __initdata = (128ULL<<20);
  58. static int __init parse_dma32_size_opt(char *p)
  59. {
  60. if (!p)
  61. return -EINVAL;
  62. dma32_bootmem_size = memparse(p, &p);
  63. return 0;
  64. }
  65. early_param("dma32_size", parse_dma32_size_opt);
  66. void __init dma32_reserve_bootmem(void)
  67. {
  68. unsigned long size, align;
  69. if (max_pfn <= MAX_DMA32_PFN)
  70. return;
  71. /*
  72. * check aperture_64.c allocate_aperture() for reason about
  73. * using 512M as goal
  74. */
  75. align = 64ULL<<20;
  76. size = roundup(dma32_bootmem_size, align);
  77. dma32_bootmem_ptr = __alloc_bootmem_nopanic(size, align,
  78. 512ULL<<20);
  79. /*
  80. * Kmemleak should not scan this block as it may not be mapped via the
  81. * kernel direct mapping.
  82. */
  83. kmemleak_ignore(dma32_bootmem_ptr);
  84. if (dma32_bootmem_ptr)
  85. dma32_bootmem_size = size;
  86. else
  87. dma32_bootmem_size = 0;
  88. }
  89. static void __init dma32_free_bootmem(void)
  90. {
  91. if (max_pfn <= MAX_DMA32_PFN)
  92. return;
  93. if (!dma32_bootmem_ptr)
  94. return;
  95. free_bootmem(__pa(dma32_bootmem_ptr), dma32_bootmem_size);
  96. dma32_bootmem_ptr = NULL;
  97. dma32_bootmem_size = 0;
  98. }
  99. #else
  100. void __init dma32_reserve_bootmem(void)
  101. {
  102. }
  103. static void __init dma32_free_bootmem(void)
  104. {
  105. }
  106. #endif
  107. void __init pci_iommu_alloc(void)
  108. {
  109. /* free the range so iommu could get some range less than 4G */
  110. dma32_free_bootmem();
  111. if (pci_swiotlb_detect())
  112. goto out;
  113. gart_iommu_hole_init();
  114. detect_calgary();
  115. detect_intel_iommu();
  116. /* needs to be called after gart_iommu_hole_init */
  117. amd_iommu_detect();
  118. out:
  119. pci_swiotlb_init();
  120. }
  121. void *dma_generic_alloc_coherent(struct device *dev, size_t size,
  122. dma_addr_t *dma_addr, gfp_t flag)
  123. {
  124. unsigned long dma_mask;
  125. struct page *page;
  126. dma_addr_t addr;
  127. dma_mask = dma_alloc_coherent_mask(dev, flag);
  128. flag |= __GFP_ZERO;
  129. again:
  130. page = alloc_pages_node(dev_to_node(dev), flag, get_order(size));
  131. if (!page)
  132. return NULL;
  133. addr = page_to_phys(page);
  134. if (addr + size > dma_mask) {
  135. __free_pages(page, get_order(size));
  136. if (dma_mask < DMA_BIT_MASK(32) && !(flag & GFP_DMA)) {
  137. flag = (flag & ~GFP_DMA32) | GFP_DMA;
  138. goto again;
  139. }
  140. return NULL;
  141. }
  142. *dma_addr = addr;
  143. return page_address(page);
  144. }
  145. /*
  146. * See <Documentation/x86_64/boot-options.txt> for the iommu kernel parameter
  147. * documentation.
  148. */
  149. static __init int iommu_setup(char *p)
  150. {
  151. iommu_merge = 1;
  152. if (!p)
  153. return -EINVAL;
  154. while (*p) {
  155. if (!strncmp(p, "off", 3))
  156. no_iommu = 1;
  157. /* gart_parse_options has more force support */
  158. if (!strncmp(p, "force", 5))
  159. force_iommu = 1;
  160. if (!strncmp(p, "noforce", 7)) {
  161. iommu_merge = 0;
  162. force_iommu = 0;
  163. }
  164. if (!strncmp(p, "biomerge", 8)) {
  165. iommu_merge = 1;
  166. force_iommu = 1;
  167. }
  168. if (!strncmp(p, "panic", 5))
  169. panic_on_overflow = 1;
  170. if (!strncmp(p, "nopanic", 7))
  171. panic_on_overflow = 0;
  172. if (!strncmp(p, "merge", 5)) {
  173. iommu_merge = 1;
  174. force_iommu = 1;
  175. }
  176. if (!strncmp(p, "nomerge", 7))
  177. iommu_merge = 0;
  178. if (!strncmp(p, "forcesac", 8))
  179. iommu_sac_force = 1;
  180. if (!strncmp(p, "allowdac", 8))
  181. forbid_dac = 0;
  182. if (!strncmp(p, "nodac", 5))
  183. forbid_dac = 1;
  184. if (!strncmp(p, "usedac", 6)) {
  185. forbid_dac = -1;
  186. return 1;
  187. }
  188. #ifdef CONFIG_SWIOTLB
  189. if (!strncmp(p, "soft", 4))
  190. swiotlb = 1;
  191. #endif
  192. if (!strncmp(p, "pt", 2))
  193. iommu_pass_through = 1;
  194. gart_parse_options(p);
  195. #ifdef CONFIG_CALGARY_IOMMU
  196. if (!strncmp(p, "calgary", 7))
  197. use_calgary = 1;
  198. #endif /* CONFIG_CALGARY_IOMMU */
  199. p += strcspn(p, ",");
  200. if (*p == ',')
  201. ++p;
  202. }
  203. return 0;
  204. }
  205. early_param("iommu", iommu_setup);
  206. int dma_supported(struct device *dev, u64 mask)
  207. {
  208. struct dma_map_ops *ops = get_dma_ops(dev);
  209. #ifdef CONFIG_PCI
  210. if (mask > 0xffffffff && forbid_dac > 0) {
  211. dev_info(dev, "PCI: Disallowing DAC for device\n");
  212. return 0;
  213. }
  214. #endif
  215. if (ops->dma_supported)
  216. return ops->dma_supported(dev, mask);
  217. /* Copied from i386. Doesn't make much sense, because it will
  218. only work for pci_alloc_coherent.
  219. The caller just has to use GFP_DMA in this case. */
  220. if (mask < DMA_BIT_MASK(24))
  221. return 0;
  222. /* Tell the device to use SAC when IOMMU force is on. This
  223. allows the driver to use cheaper accesses in some cases.
  224. Problem with this is that if we overflow the IOMMU area and
  225. return DAC as fallback address the device may not handle it
  226. correctly.
  227. As a special case some controllers have a 39bit address
  228. mode that is as efficient as 32bit (aic79xx). Don't force
  229. SAC for these. Assume all masks <= 40 bits are of this
  230. type. Normally this doesn't make any difference, but gives
  231. more gentle handling of IOMMU overflow. */
  232. if (iommu_sac_force && (mask >= DMA_BIT_MASK(40))) {
  233. dev_info(dev, "Force SAC with mask %Lx\n", mask);
  234. return 0;
  235. }
  236. return 1;
  237. }
  238. EXPORT_SYMBOL(dma_supported);
  239. static int __init pci_iommu_init(void)
  240. {
  241. dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
  242. #ifdef CONFIG_PCI
  243. dma_debug_add_bus(&pci_bus_type);
  244. #endif
  245. x86_init.iommu.iommu_init();
  246. if (swiotlb) {
  247. printk(KERN_INFO "PCI-DMA: "
  248. "Using software bounce buffering for IO (SWIOTLB)\n");
  249. swiotlb_print_info();
  250. } else
  251. swiotlb_free();
  252. return 0;
  253. }
  254. /* Must execute after PCI subsystem */
  255. rootfs_initcall(pci_iommu_init);
  256. #ifdef CONFIG_PCI
  257. /* Many VIA bridges seem to corrupt data for DAC. Disable it here */
  258. static __devinit void via_no_dac(struct pci_dev *dev)
  259. {
  260. if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI && forbid_dac == 0) {
  261. dev_info(&dev->dev, "disabling DAC on VIA PCI bridge\n");
  262. forbid_dac = 1;
  263. }
  264. }
  265. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA, PCI_ANY_ID, via_no_dac);
  266. #endif