pci-dma.c 7.1 KB

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