pci-dma.c 6.4 KB

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