pci-dma.c 6.4 KB

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