pci-dma.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #include <linux/dma-mapping.h>
  2. #include <linux/dma-debug.h>
  3. #include <linux/dmar.h>
  4. #include <linux/export.h>
  5. #include <linux/bootmem.h>
  6. #include <linux/gfp.h>
  7. #include <linux/pci.h>
  8. #include <linux/kmemleak.h>
  9. #include <asm/proto.h>
  10. #include <asm/dma.h>
  11. #include <asm/iommu.h>
  12. #include <asm/gart.h>
  13. #include <asm/calgary.h>
  14. #include <asm/x86_init.h>
  15. #include <asm/iommu_table.h>
  16. static int forbid_dac __read_mostly;
  17. struct dma_map_ops *dma_ops = &nommu_dma_ops;
  18. EXPORT_SYMBOL(dma_ops);
  19. static int iommu_sac_force __read_mostly;
  20. #ifdef CONFIG_IOMMU_DEBUG
  21. int panic_on_overflow __read_mostly = 1;
  22. int force_iommu __read_mostly = 1;
  23. #else
  24. int panic_on_overflow __read_mostly = 0;
  25. int force_iommu __read_mostly = 0;
  26. #endif
  27. int iommu_merge __read_mostly = 0;
  28. int no_iommu __read_mostly;
  29. /* Set this to 1 if there is a HW IOMMU in the system */
  30. int iommu_detected __read_mostly = 0;
  31. /*
  32. * This variable becomes 1 if iommu=pt is passed on the kernel command line.
  33. * If this variable is 1, IOMMU implementations do no DMA translation for
  34. * devices and allow every device to access to whole physical memory. This is
  35. * useful if a user wants to use an IOMMU only for KVM device assignment to
  36. * guests and not for driver dma translation.
  37. */
  38. int iommu_pass_through __read_mostly;
  39. extern struct iommu_table_entry __iommu_table[], __iommu_table_end[];
  40. /* Dummy device used for NULL arguments (normally ISA). */
  41. struct device x86_dma_fallback_dev = {
  42. .init_name = "fallback device",
  43. .coherent_dma_mask = ISA_DMA_BIT_MASK,
  44. .dma_mask = &x86_dma_fallback_dev.coherent_dma_mask,
  45. };
  46. EXPORT_SYMBOL(x86_dma_fallback_dev);
  47. /* Number of entries preallocated for DMA-API debugging */
  48. #define PREALLOC_DMA_DEBUG_ENTRIES 65536
  49. void __init pci_iommu_alloc(void)
  50. {
  51. struct iommu_table_entry *p;
  52. sort_iommu_table(__iommu_table, __iommu_table_end);
  53. check_iommu_entries(__iommu_table, __iommu_table_end);
  54. for (p = __iommu_table; p < __iommu_table_end; p++) {
  55. if (p && p->detect && p->detect() > 0) {
  56. p->flags |= IOMMU_DETECTED;
  57. if (p->early_init)
  58. p->early_init();
  59. if (p->flags & IOMMU_FINISH_IF_DETECTED)
  60. break;
  61. }
  62. }
  63. }
  64. void *dma_generic_alloc_coherent(struct device *dev, size_t size,
  65. dma_addr_t *dma_addr, gfp_t flag,
  66. unsigned long attrs)
  67. {
  68. unsigned long dma_mask;
  69. struct page *page;
  70. unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  71. dma_addr_t addr;
  72. dma_mask = dma_alloc_coherent_mask(dev, flag);
  73. flag &= ~__GFP_ZERO;
  74. again:
  75. page = NULL;
  76. /* CMA can be used only in the context which permits sleeping */
  77. if (gfpflags_allow_blocking(flag)) {
  78. page = dma_alloc_from_contiguous(dev, count, get_order(size));
  79. if (page && page_to_phys(page) + size > dma_mask) {
  80. dma_release_from_contiguous(dev, page, count);
  81. page = NULL;
  82. }
  83. }
  84. /* fallback */
  85. if (!page)
  86. page = alloc_pages_node(dev_to_node(dev), flag, get_order(size));
  87. if (!page)
  88. return NULL;
  89. addr = page_to_phys(page);
  90. if (addr + size > dma_mask) {
  91. __free_pages(page, get_order(size));
  92. if (dma_mask < DMA_BIT_MASK(32) && !(flag & GFP_DMA)) {
  93. flag = (flag & ~GFP_DMA32) | GFP_DMA;
  94. goto again;
  95. }
  96. return NULL;
  97. }
  98. memset(page_address(page), 0, size);
  99. *dma_addr = addr;
  100. return page_address(page);
  101. }
  102. void dma_generic_free_coherent(struct device *dev, size_t size, void *vaddr,
  103. dma_addr_t dma_addr, unsigned long attrs)
  104. {
  105. unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  106. struct page *page = virt_to_page(vaddr);
  107. if (!dma_release_from_contiguous(dev, page, count))
  108. free_pages((unsigned long)vaddr, get_order(size));
  109. }
  110. bool arch_dma_alloc_attrs(struct device **dev, gfp_t *gfp)
  111. {
  112. if (!*dev)
  113. *dev = &x86_dma_fallback_dev;
  114. *gfp &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
  115. *gfp = dma_alloc_coherent_gfp_flags(*dev, *gfp);
  116. if (!is_device_dma_capable(*dev))
  117. return false;
  118. return true;
  119. }
  120. EXPORT_SYMBOL(arch_dma_alloc_attrs);
  121. /*
  122. * See <Documentation/x86/x86_64/boot-options.txt> for the iommu kernel
  123. * parameter documentation.
  124. */
  125. static __init int iommu_setup(char *p)
  126. {
  127. iommu_merge = 1;
  128. if (!p)
  129. return -EINVAL;
  130. while (*p) {
  131. if (!strncmp(p, "off", 3))
  132. no_iommu = 1;
  133. /* gart_parse_options has more force support */
  134. if (!strncmp(p, "force", 5))
  135. force_iommu = 1;
  136. if (!strncmp(p, "noforce", 7)) {
  137. iommu_merge = 0;
  138. force_iommu = 0;
  139. }
  140. if (!strncmp(p, "biomerge", 8)) {
  141. iommu_merge = 1;
  142. force_iommu = 1;
  143. }
  144. if (!strncmp(p, "panic", 5))
  145. panic_on_overflow = 1;
  146. if (!strncmp(p, "nopanic", 7))
  147. panic_on_overflow = 0;
  148. if (!strncmp(p, "merge", 5)) {
  149. iommu_merge = 1;
  150. force_iommu = 1;
  151. }
  152. if (!strncmp(p, "nomerge", 7))
  153. iommu_merge = 0;
  154. if (!strncmp(p, "forcesac", 8))
  155. iommu_sac_force = 1;
  156. if (!strncmp(p, "allowdac", 8))
  157. forbid_dac = 0;
  158. if (!strncmp(p, "nodac", 5))
  159. forbid_dac = 1;
  160. if (!strncmp(p, "usedac", 6)) {
  161. forbid_dac = -1;
  162. return 1;
  163. }
  164. #ifdef CONFIG_SWIOTLB
  165. if (!strncmp(p, "soft", 4))
  166. swiotlb = 1;
  167. #endif
  168. if (!strncmp(p, "pt", 2))
  169. iommu_pass_through = 1;
  170. gart_parse_options(p);
  171. #ifdef CONFIG_CALGARY_IOMMU
  172. if (!strncmp(p, "calgary", 7))
  173. use_calgary = 1;
  174. #endif /* CONFIG_CALGARY_IOMMU */
  175. p += strcspn(p, ",");
  176. if (*p == ',')
  177. ++p;
  178. }
  179. return 0;
  180. }
  181. early_param("iommu", iommu_setup);
  182. int dma_supported(struct device *dev, u64 mask)
  183. {
  184. struct dma_map_ops *ops = get_dma_ops(dev);
  185. #ifdef CONFIG_PCI
  186. if (mask > 0xffffffff && forbid_dac > 0) {
  187. dev_info(dev, "PCI: Disallowing DAC for device\n");
  188. return 0;
  189. }
  190. #endif
  191. if (ops->dma_supported)
  192. return ops->dma_supported(dev, mask);
  193. /* Copied from i386. Doesn't make much sense, because it will
  194. only work for pci_alloc_coherent.
  195. The caller just has to use GFP_DMA in this case. */
  196. if (mask < DMA_BIT_MASK(24))
  197. return 0;
  198. /* Tell the device to use SAC when IOMMU force is on. This
  199. allows the driver to use cheaper accesses in some cases.
  200. Problem with this is that if we overflow the IOMMU area and
  201. return DAC as fallback address the device may not handle it
  202. correctly.
  203. As a special case some controllers have a 39bit address
  204. mode that is as efficient as 32bit (aic79xx). Don't force
  205. SAC for these. Assume all masks <= 40 bits are of this
  206. type. Normally this doesn't make any difference, but gives
  207. more gentle handling of IOMMU overflow. */
  208. if (iommu_sac_force && (mask >= DMA_BIT_MASK(40))) {
  209. dev_info(dev, "Force SAC with mask %Lx\n", mask);
  210. return 0;
  211. }
  212. return 1;
  213. }
  214. EXPORT_SYMBOL(dma_supported);
  215. static int __init pci_iommu_init(void)
  216. {
  217. struct iommu_table_entry *p;
  218. dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
  219. #ifdef CONFIG_PCI
  220. dma_debug_add_bus(&pci_bus_type);
  221. #endif
  222. x86_init.iommu.iommu_init();
  223. for (p = __iommu_table; p < __iommu_table_end; p++) {
  224. if (p && (p->flags & IOMMU_DETECTED) && p->late_init)
  225. p->late_init();
  226. }
  227. return 0;
  228. }
  229. /* Must execute after PCI subsystem */
  230. rootfs_initcall(pci_iommu_init);
  231. #ifdef CONFIG_PCI
  232. /* Many VIA bridges seem to corrupt data for DAC. Disable it here */
  233. static void via_no_dac(struct pci_dev *dev)
  234. {
  235. if (forbid_dac == 0) {
  236. dev_info(&dev->dev, "disabling DAC on VIA PCI bridge\n");
  237. forbid_dac = 1;
  238. }
  239. }
  240. DECLARE_PCI_FIXUP_CLASS_FINAL(PCI_VENDOR_ID_VIA, PCI_ANY_ID,
  241. PCI_CLASS_BRIDGE_PCI, 8, via_no_dac);
  242. #endif