pci-dma.c 6.6 KB

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