pci-dma.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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/calgary.h>
  9. #include <asm/amd_iommu.h>
  10. static int forbid_dac __read_mostly;
  11. struct dma_mapping_ops *dma_ops;
  12. EXPORT_SYMBOL(dma_ops);
  13. static int iommu_sac_force __read_mostly;
  14. #ifdef CONFIG_IOMMU_DEBUG
  15. int panic_on_overflow __read_mostly = 1;
  16. int force_iommu __read_mostly = 1;
  17. #else
  18. int panic_on_overflow __read_mostly = 0;
  19. int force_iommu __read_mostly = 0;
  20. #endif
  21. int iommu_merge __read_mostly = 0;
  22. int no_iommu __read_mostly;
  23. /* Set this to 1 if there is a HW IOMMU in the system */
  24. int iommu_detected __read_mostly = 0;
  25. /* This tells the BIO block layer to assume merging. Default to off
  26. because we cannot guarantee merging later. */
  27. int iommu_bio_merge __read_mostly = 0;
  28. EXPORT_SYMBOL(iommu_bio_merge);
  29. dma_addr_t bad_dma_address __read_mostly = 0;
  30. EXPORT_SYMBOL(bad_dma_address);
  31. /* Dummy device used for NULL arguments (normally ISA). Better would
  32. be probably a smaller DMA mask, but this is bug-to-bug compatible
  33. to older i386. */
  34. struct device x86_dma_fallback_dev = {
  35. .bus_id = "fallback device",
  36. .coherent_dma_mask = DMA_32BIT_MASK,
  37. .dma_mask = &x86_dma_fallback_dev.coherent_dma_mask,
  38. };
  39. EXPORT_SYMBOL(x86_dma_fallback_dev);
  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. void __init pci_iommu_alloc(void)
  88. {
  89. /* free the range so iommu could get some range less than 4G */
  90. dma32_free_bootmem();
  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. unsigned long iommu_num_pages(unsigned long addr, unsigned long len)
  102. {
  103. unsigned long size = roundup((addr & ~PAGE_MASK) + len, PAGE_SIZE);
  104. return size >> PAGE_SHIFT;
  105. }
  106. EXPORT_SYMBOL(iommu_num_pages);
  107. #endif
  108. void *dma_generic_alloc_coherent(struct device *dev, size_t size,
  109. dma_addr_t *dma_addr, gfp_t flag)
  110. {
  111. unsigned long dma_mask;
  112. struct page *page;
  113. dma_addr_t addr;
  114. dma_mask = dma_alloc_coherent_mask(dev, flag);
  115. flag |= __GFP_ZERO;
  116. again:
  117. page = alloc_pages_node(dev_to_node(dev), flag, get_order(size));
  118. if (!page)
  119. return NULL;
  120. addr = page_to_phys(page);
  121. if (!is_buffer_dma_capable(dma_mask, addr, size)) {
  122. __free_pages(page, get_order(size));
  123. if (dma_mask < DMA_32BIT_MASK && !(flag & GFP_DMA)) {
  124. flag = (flag & ~GFP_DMA32) | GFP_DMA;
  125. goto again;
  126. }
  127. return NULL;
  128. }
  129. *dma_addr = addr;
  130. return page_address(page);
  131. }
  132. /*
  133. * See <Documentation/x86_64/boot-options.txt> for the iommu kernel parameter
  134. * documentation.
  135. */
  136. static __init int iommu_setup(char *p)
  137. {
  138. iommu_merge = 1;
  139. if (!p)
  140. return -EINVAL;
  141. while (*p) {
  142. if (!strncmp(p, "off", 3))
  143. no_iommu = 1;
  144. /* gart_parse_options has more force support */
  145. if (!strncmp(p, "force", 5))
  146. force_iommu = 1;
  147. if (!strncmp(p, "noforce", 7)) {
  148. iommu_merge = 0;
  149. force_iommu = 0;
  150. }
  151. if (!strncmp(p, "biomerge", 8)) {
  152. iommu_bio_merge = 4096;
  153. iommu_merge = 1;
  154. force_iommu = 1;
  155. }
  156. if (!strncmp(p, "panic", 5))
  157. panic_on_overflow = 1;
  158. if (!strncmp(p, "nopanic", 7))
  159. panic_on_overflow = 0;
  160. if (!strncmp(p, "merge", 5)) {
  161. iommu_merge = 1;
  162. force_iommu = 1;
  163. }
  164. if (!strncmp(p, "nomerge", 7))
  165. iommu_merge = 0;
  166. if (!strncmp(p, "forcesac", 8))
  167. iommu_sac_force = 1;
  168. if (!strncmp(p, "allowdac", 8))
  169. forbid_dac = 0;
  170. if (!strncmp(p, "nodac", 5))
  171. forbid_dac = -1;
  172. if (!strncmp(p, "usedac", 6)) {
  173. forbid_dac = -1;
  174. return 1;
  175. }
  176. #ifdef CONFIG_SWIOTLB
  177. if (!strncmp(p, "soft", 4))
  178. swiotlb = 1;
  179. #endif
  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_mapping_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_24BIT_MASK)
  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_40BIT_MASK)) {
  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. calgary_iommu_init();
  228. intel_iommu_init();
  229. amd_iommu_init();
  230. gart_iommu_init();
  231. no_iommu_init();
  232. return 0;
  233. }
  234. void pci_iommu_shutdown(void)
  235. {
  236. gart_iommu_shutdown();
  237. }
  238. /* Must execute after PCI subsystem */
  239. fs_initcall(pci_iommu_init);
  240. #ifdef CONFIG_PCI
  241. /* Many VIA bridges seem to corrupt data for DAC. Disable it here */
  242. static __devinit void via_no_dac(struct pci_dev *dev)
  243. {
  244. if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI && forbid_dac == 0) {
  245. printk(KERN_INFO "PCI: VIA PCI bridge detected."
  246. "Disabling DAC.\n");
  247. forbid_dac = 1;
  248. }
  249. }
  250. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA, PCI_ANY_ID, via_no_dac);
  251. #endif