pci-dma.c 7.4 KB

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