pci-dma_64.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * Dynamic DMA mapping support.
  3. */
  4. #include <linux/types.h>
  5. #include <linux/mm.h>
  6. #include <linux/string.h>
  7. #include <linux/pci.h>
  8. #include <linux/module.h>
  9. #include <asm/io.h>
  10. #include <asm/iommu.h>
  11. #include <asm/calgary.h>
  12. int iommu_merge __read_mostly = 1;
  13. EXPORT_SYMBOL(iommu_merge);
  14. dma_addr_t bad_dma_address __read_mostly;
  15. EXPORT_SYMBOL(bad_dma_address);
  16. /* This tells the BIO block layer to assume merging. Default to off
  17. because we cannot guarantee merging later. */
  18. int iommu_bio_merge __read_mostly = 0;
  19. EXPORT_SYMBOL(iommu_bio_merge);
  20. static int iommu_sac_force __read_mostly = 0;
  21. int no_iommu __read_mostly;
  22. #ifdef CONFIG_IOMMU_DEBUG
  23. int panic_on_overflow __read_mostly = 1;
  24. int force_iommu __read_mostly = 1;
  25. #else
  26. int panic_on_overflow __read_mostly = 0;
  27. int force_iommu __read_mostly= 0;
  28. #endif
  29. /* Set this to 1 if there is a HW IOMMU in the system */
  30. int iommu_detected __read_mostly = 0;
  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 i386. */
  34. struct device fallback_dev = {
  35. .bus_id = "fallback device",
  36. .coherent_dma_mask = DMA_32BIT_MASK,
  37. .dma_mask = &fallback_dev.coherent_dma_mask,
  38. };
  39. /* Allocate DMA memory on node near device */
  40. noinline static void *
  41. dma_alloc_pages(struct device *dev, gfp_t gfp, unsigned order)
  42. {
  43. struct page *page;
  44. int node;
  45. node = dev_to_node(dev);
  46. if (node == -1)
  47. node = numa_node_id();
  48. if (node < first_node(node_online_map))
  49. node = first_node(node_online_map);
  50. page = alloc_pages_node(node, gfp, order);
  51. return page ? page_address(page) : NULL;
  52. }
  53. /*
  54. * Allocate memory for a coherent mapping.
  55. */
  56. void *
  57. dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
  58. gfp_t gfp)
  59. {
  60. void *memory;
  61. unsigned long dma_mask = 0;
  62. u64 bus;
  63. if (!dev)
  64. dev = &fallback_dev;
  65. dma_mask = dev->coherent_dma_mask;
  66. if (dma_mask == 0)
  67. dma_mask = DMA_32BIT_MASK;
  68. /* Device not DMA able */
  69. if (dev->dma_mask == NULL)
  70. return NULL;
  71. /* Don't invoke OOM killer */
  72. gfp |= __GFP_NORETRY;
  73. /* Kludge to make it bug-to-bug compatible with i386. i386
  74. uses the normal dma_mask for alloc_coherent. */
  75. dma_mask &= *dev->dma_mask;
  76. /* Why <=? Even when the mask is smaller than 4GB it is often
  77. larger than 16MB and in this case we have a chance of
  78. finding fitting memory in the next higher zone first. If
  79. not retry with true GFP_DMA. -AK */
  80. if (dma_mask <= DMA_32BIT_MASK)
  81. gfp |= GFP_DMA32;
  82. again:
  83. memory = dma_alloc_pages(dev, gfp, get_order(size));
  84. if (memory == NULL)
  85. return NULL;
  86. {
  87. int high, mmu;
  88. bus = virt_to_bus(memory);
  89. high = (bus + size) >= dma_mask;
  90. mmu = high;
  91. if (force_iommu && !(gfp & GFP_DMA))
  92. mmu = 1;
  93. else if (high) {
  94. free_pages((unsigned long)memory,
  95. get_order(size));
  96. /* Don't use the 16MB ZONE_DMA unless absolutely
  97. needed. It's better to use remapping first. */
  98. if (dma_mask < DMA_32BIT_MASK && !(gfp & GFP_DMA)) {
  99. gfp = (gfp & ~GFP_DMA32) | GFP_DMA;
  100. goto again;
  101. }
  102. /* Let low level make its own zone decisions */
  103. gfp &= ~(GFP_DMA32|GFP_DMA);
  104. if (dma_ops->alloc_coherent)
  105. return dma_ops->alloc_coherent(dev, size,
  106. dma_handle, gfp);
  107. return NULL;
  108. }
  109. memset(memory, 0, size);
  110. if (!mmu) {
  111. *dma_handle = virt_to_bus(memory);
  112. return memory;
  113. }
  114. }
  115. if (dma_ops->alloc_coherent) {
  116. free_pages((unsigned long)memory, get_order(size));
  117. gfp &= ~(GFP_DMA|GFP_DMA32);
  118. return dma_ops->alloc_coherent(dev, size, dma_handle, gfp);
  119. }
  120. if (dma_ops->map_simple) {
  121. *dma_handle = dma_ops->map_simple(dev, memory,
  122. size,
  123. PCI_DMA_BIDIRECTIONAL);
  124. if (*dma_handle != bad_dma_address)
  125. return memory;
  126. }
  127. if (panic_on_overflow)
  128. panic("dma_alloc_coherent: IOMMU overflow by %lu bytes\n",size);
  129. free_pages((unsigned long)memory, get_order(size));
  130. return NULL;
  131. }
  132. EXPORT_SYMBOL(dma_alloc_coherent);
  133. /*
  134. * Unmap coherent memory.
  135. * The caller must ensure that the device has finished accessing the mapping.
  136. */
  137. void dma_free_coherent(struct device *dev, size_t size,
  138. void *vaddr, dma_addr_t bus)
  139. {
  140. WARN_ON(irqs_disabled()); /* for portability */
  141. if (dma_ops->unmap_single)
  142. dma_ops->unmap_single(dev, bus, size, 0);
  143. free_pages((unsigned long)vaddr, get_order(size));
  144. }
  145. EXPORT_SYMBOL(dma_free_coherent);
  146. static int forbid_dac __read_mostly;
  147. int dma_supported(struct device *dev, u64 mask)
  148. {
  149. #ifdef CONFIG_PCI
  150. if (mask > 0xffffffff && forbid_dac > 0) {
  151. printk(KERN_INFO "PCI: Disallowing DAC for device %s\n", dev->bus_id);
  152. return 0;
  153. }
  154. #endif
  155. if (dma_ops->dma_supported)
  156. return dma_ops->dma_supported(dev, mask);
  157. /* Copied from i386. Doesn't make much sense, because it will
  158. only work for pci_alloc_coherent.
  159. The caller just has to use GFP_DMA in this case. */
  160. if (mask < DMA_24BIT_MASK)
  161. return 0;
  162. /* Tell the device to use SAC when IOMMU force is on. This
  163. allows the driver to use cheaper accesses in some cases.
  164. Problem with this is that if we overflow the IOMMU area and
  165. return DAC as fallback address the device may not handle it
  166. correctly.
  167. As a special case some controllers have a 39bit address
  168. mode that is as efficient as 32bit (aic79xx). Don't force
  169. SAC for these. Assume all masks <= 40 bits are of this
  170. type. Normally this doesn't make any difference, but gives
  171. more gentle handling of IOMMU overflow. */
  172. if (iommu_sac_force && (mask >= DMA_40BIT_MASK)) {
  173. printk(KERN_INFO "%s: Force SAC with mask %Lx\n", dev->bus_id,mask);
  174. return 0;
  175. }
  176. return 1;
  177. }
  178. EXPORT_SYMBOL(dma_supported);
  179. int dma_set_mask(struct device *dev, u64 mask)
  180. {
  181. if (!dev->dma_mask || !dma_supported(dev, mask))
  182. return -EIO;
  183. *dev->dma_mask = mask;
  184. return 0;
  185. }
  186. EXPORT_SYMBOL(dma_set_mask);
  187. /*
  188. * See <Documentation/x86_64/boot-options.txt> for the iommu kernel parameter
  189. * documentation.
  190. */
  191. __init int iommu_setup(char *p)
  192. {
  193. iommu_merge = 1;
  194. if (!p)
  195. return -EINVAL;
  196. while (*p) {
  197. if (!strncmp(p,"off",3))
  198. no_iommu = 1;
  199. /* gart_parse_options has more force support */
  200. if (!strncmp(p,"force",5))
  201. force_iommu = 1;
  202. if (!strncmp(p,"noforce",7)) {
  203. iommu_merge = 0;
  204. force_iommu = 0;
  205. }
  206. if (!strncmp(p, "biomerge",8)) {
  207. iommu_bio_merge = 4096;
  208. iommu_merge = 1;
  209. force_iommu = 1;
  210. }
  211. if (!strncmp(p, "panic",5))
  212. panic_on_overflow = 1;
  213. if (!strncmp(p, "nopanic",7))
  214. panic_on_overflow = 0;
  215. if (!strncmp(p, "merge",5)) {
  216. iommu_merge = 1;
  217. force_iommu = 1;
  218. }
  219. if (!strncmp(p, "nomerge",7))
  220. iommu_merge = 0;
  221. if (!strncmp(p, "forcesac",8))
  222. iommu_sac_force = 1;
  223. if (!strncmp(p, "allowdac", 8))
  224. forbid_dac = 0;
  225. if (!strncmp(p, "nodac", 5))
  226. forbid_dac = -1;
  227. #ifdef CONFIG_SWIOTLB
  228. if (!strncmp(p, "soft",4))
  229. swiotlb = 1;
  230. #endif
  231. #ifdef CONFIG_IOMMU
  232. gart_parse_options(p);
  233. #endif
  234. #ifdef CONFIG_CALGARY_IOMMU
  235. if (!strncmp(p, "calgary", 7))
  236. use_calgary = 1;
  237. #endif /* CONFIG_CALGARY_IOMMU */
  238. p += strcspn(p, ",");
  239. if (*p == ',')
  240. ++p;
  241. }
  242. return 0;
  243. }
  244. early_param("iommu", iommu_setup);
  245. void __init pci_iommu_alloc(void)
  246. {
  247. /*
  248. * The order of these functions is important for
  249. * fall-back/fail-over reasons
  250. */
  251. #ifdef CONFIG_IOMMU
  252. iommu_hole_init();
  253. #endif
  254. #ifdef CONFIG_CALGARY_IOMMU
  255. detect_calgary();
  256. #endif
  257. #ifdef CONFIG_SWIOTLB
  258. pci_swiotlb_init();
  259. #endif
  260. }
  261. static int __init pci_iommu_init(void)
  262. {
  263. #ifdef CONFIG_CALGARY_IOMMU
  264. calgary_iommu_init();
  265. #endif
  266. #ifdef CONFIG_IOMMU
  267. gart_iommu_init();
  268. #endif
  269. no_iommu_init();
  270. return 0;
  271. }
  272. void pci_iommu_shutdown(void)
  273. {
  274. gart_iommu_shutdown();
  275. }
  276. #ifdef CONFIG_PCI
  277. /* Many VIA bridges seem to corrupt data for DAC. Disable it here */
  278. static __devinit void via_no_dac(struct pci_dev *dev)
  279. {
  280. if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI && forbid_dac == 0) {
  281. printk(KERN_INFO "PCI: VIA PCI bridge detected. Disabling DAC.\n");
  282. forbid_dac = 1;
  283. }
  284. }
  285. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA, PCI_ANY_ID, via_no_dac);
  286. #endif
  287. /* Must execute after PCI subsystem */
  288. fs_initcall(pci_iommu_init);