pci-dma.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. int dma_set_mask(struct device *dev, u64 mask)
  50. {
  51. if (!dev->dma_mask || !dma_supported(dev, mask))
  52. return -EIO;
  53. *dev->dma_mask = mask;
  54. return 0;
  55. }
  56. EXPORT_SYMBOL(dma_set_mask);
  57. void __init pci_iommu_alloc(void)
  58. {
  59. struct iommu_table_entry *p;
  60. sort_iommu_table(__iommu_table, __iommu_table_end);
  61. check_iommu_entries(__iommu_table, __iommu_table_end);
  62. for (p = __iommu_table; p < __iommu_table_end; p++) {
  63. if (p && p->detect && p->detect() > 0) {
  64. p->flags |= IOMMU_DETECTED;
  65. if (p->early_init)
  66. p->early_init();
  67. if (p->flags & IOMMU_FINISH_IF_DETECTED)
  68. break;
  69. }
  70. }
  71. }
  72. void *dma_generic_alloc_coherent(struct device *dev, size_t size,
  73. dma_addr_t *dma_addr, gfp_t flag,
  74. struct dma_attrs *attrs)
  75. {
  76. unsigned long dma_mask;
  77. struct page *page;
  78. unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  79. dma_addr_t addr;
  80. dma_mask = dma_alloc_coherent_mask(dev, flag);
  81. flag |= __GFP_ZERO;
  82. again:
  83. page = NULL;
  84. /* CMA can be used only in the context which permits sleeping */
  85. if (flag & __GFP_WAIT)
  86. page = dma_alloc_from_contiguous(dev, count, get_order(size));
  87. /* fallback */
  88. if (!page)
  89. page = alloc_pages_node(dev_to_node(dev), flag, get_order(size));
  90. if (!page)
  91. return NULL;
  92. addr = page_to_phys(page);
  93. if (addr + size > dma_mask) {
  94. __free_pages(page, get_order(size));
  95. if (dma_mask < DMA_BIT_MASK(32) && !(flag & GFP_DMA)) {
  96. flag = (flag & ~GFP_DMA32) | GFP_DMA;
  97. goto again;
  98. }
  99. return NULL;
  100. }
  101. *dma_addr = addr;
  102. return page_address(page);
  103. }
  104. void dma_generic_free_coherent(struct device *dev, size_t size, void *vaddr,
  105. dma_addr_t dma_addr, struct dma_attrs *attrs)
  106. {
  107. unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  108. struct page *page = virt_to_page(vaddr);
  109. if (!dma_release_from_contiguous(dev, page, count))
  110. free_pages((unsigned long)vaddr, get_order(size));
  111. }
  112. /*
  113. * See <Documentation/x86/x86_64/boot-options.txt> for the iommu kernel
  114. * parameter documentation.
  115. */
  116. static __init int iommu_setup(char *p)
  117. {
  118. iommu_merge = 1;
  119. if (!p)
  120. return -EINVAL;
  121. while (*p) {
  122. if (!strncmp(p, "off", 3))
  123. no_iommu = 1;
  124. /* gart_parse_options has more force support */
  125. if (!strncmp(p, "force", 5))
  126. force_iommu = 1;
  127. if (!strncmp(p, "noforce", 7)) {
  128. iommu_merge = 0;
  129. force_iommu = 0;
  130. }
  131. if (!strncmp(p, "biomerge", 8)) {
  132. iommu_merge = 1;
  133. force_iommu = 1;
  134. }
  135. if (!strncmp(p, "panic", 5))
  136. panic_on_overflow = 1;
  137. if (!strncmp(p, "nopanic", 7))
  138. panic_on_overflow = 0;
  139. if (!strncmp(p, "merge", 5)) {
  140. iommu_merge = 1;
  141. force_iommu = 1;
  142. }
  143. if (!strncmp(p, "nomerge", 7))
  144. iommu_merge = 0;
  145. if (!strncmp(p, "forcesac", 8))
  146. iommu_sac_force = 1;
  147. if (!strncmp(p, "allowdac", 8))
  148. forbid_dac = 0;
  149. if (!strncmp(p, "nodac", 5))
  150. forbid_dac = 1;
  151. if (!strncmp(p, "usedac", 6)) {
  152. forbid_dac = -1;
  153. return 1;
  154. }
  155. #ifdef CONFIG_SWIOTLB
  156. if (!strncmp(p, "soft", 4))
  157. swiotlb = 1;
  158. #endif
  159. if (!strncmp(p, "pt", 2))
  160. iommu_pass_through = 1;
  161. gart_parse_options(p);
  162. #ifdef CONFIG_CALGARY_IOMMU
  163. if (!strncmp(p, "calgary", 7))
  164. use_calgary = 1;
  165. #endif /* CONFIG_CALGARY_IOMMU */
  166. p += strcspn(p, ",");
  167. if (*p == ',')
  168. ++p;
  169. }
  170. return 0;
  171. }
  172. early_param("iommu", iommu_setup);
  173. int dma_supported(struct device *dev, u64 mask)
  174. {
  175. struct dma_map_ops *ops = get_dma_ops(dev);
  176. #ifdef CONFIG_PCI
  177. if (mask > 0xffffffff && forbid_dac > 0) {
  178. dev_info(dev, "PCI: Disallowing DAC for device\n");
  179. return 0;
  180. }
  181. #endif
  182. if (ops->dma_supported)
  183. return ops->dma_supported(dev, mask);
  184. /* Copied from i386. Doesn't make much sense, because it will
  185. only work for pci_alloc_coherent.
  186. The caller just has to use GFP_DMA in this case. */
  187. if (mask < DMA_BIT_MASK(24))
  188. return 0;
  189. /* Tell the device to use SAC when IOMMU force is on. This
  190. allows the driver to use cheaper accesses in some cases.
  191. Problem with this is that if we overflow the IOMMU area and
  192. return DAC as fallback address the device may not handle it
  193. correctly.
  194. As a special case some controllers have a 39bit address
  195. mode that is as efficient as 32bit (aic79xx). Don't force
  196. SAC for these. Assume all masks <= 40 bits are of this
  197. type. Normally this doesn't make any difference, but gives
  198. more gentle handling of IOMMU overflow. */
  199. if (iommu_sac_force && (mask >= DMA_BIT_MASK(40))) {
  200. dev_info(dev, "Force SAC with mask %Lx\n", mask);
  201. return 0;
  202. }
  203. return 1;
  204. }
  205. EXPORT_SYMBOL(dma_supported);
  206. static int __init pci_iommu_init(void)
  207. {
  208. struct iommu_table_entry *p;
  209. dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
  210. #ifdef CONFIG_PCI
  211. dma_debug_add_bus(&pci_bus_type);
  212. #endif
  213. x86_init.iommu.iommu_init();
  214. for (p = __iommu_table; p < __iommu_table_end; p++) {
  215. if (p && (p->flags & IOMMU_DETECTED) && p->late_init)
  216. p->late_init();
  217. }
  218. return 0;
  219. }
  220. /* Must execute after PCI subsystem */
  221. rootfs_initcall(pci_iommu_init);
  222. #ifdef CONFIG_PCI
  223. /* Many VIA bridges seem to corrupt data for DAC. Disable it here */
  224. static void via_no_dac(struct pci_dev *dev)
  225. {
  226. if (forbid_dac == 0) {
  227. dev_info(&dev->dev, "disabling DAC on VIA PCI bridge\n");
  228. forbid_dac = 1;
  229. }
  230. }
  231. DECLARE_PCI_FIXUP_CLASS_FINAL(PCI_VENDOR_ID_VIA, PCI_ANY_ID,
  232. PCI_CLASS_BRIDGE_PCI, 8, via_no_dac);
  233. #endif