pci-dma.c 6.8 KB

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