pci-dma.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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/proto.h>
  11. int iommu_merge __read_mostly = 0;
  12. EXPORT_SYMBOL(iommu_merge);
  13. dma_addr_t bad_dma_address __read_mostly;
  14. EXPORT_SYMBOL(bad_dma_address);
  15. /* This tells the BIO block layer to assume merging. Default to off
  16. because we cannot guarantee merging later. */
  17. int iommu_bio_merge __read_mostly = 0;
  18. EXPORT_SYMBOL(iommu_bio_merge);
  19. int iommu_sac_force __read_mostly = 0;
  20. EXPORT_SYMBOL(iommu_sac_force);
  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. /* Dummy device used for NULL arguments (normally ISA). Better would
  30. be probably a smaller DMA mask, but this is bug-to-bug compatible
  31. to i386. */
  32. struct device fallback_dev = {
  33. .bus_id = "fallback device",
  34. .coherent_dma_mask = 0xffffffff,
  35. .dma_mask = &fallback_dev.coherent_dma_mask,
  36. };
  37. /* Allocate DMA memory on node near device */
  38. noinline static void *
  39. dma_alloc_pages(struct device *dev, gfp_t gfp, unsigned order)
  40. {
  41. struct page *page;
  42. int node;
  43. #ifdef CONFIG_PCI
  44. if (dev->bus == &pci_bus_type)
  45. node = pcibus_to_node(to_pci_dev(dev)->bus);
  46. else
  47. #endif
  48. node = numa_node_id();
  49. page = alloc_pages_node(node, gfp, order);
  50. return page ? page_address(page) : NULL;
  51. }
  52. /*
  53. * Allocate memory for a coherent mapping.
  54. */
  55. void *
  56. dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
  57. gfp_t gfp)
  58. {
  59. void *memory;
  60. unsigned long dma_mask = 0;
  61. u64 bus;
  62. if (!dev)
  63. dev = &fallback_dev;
  64. dma_mask = dev->coherent_dma_mask;
  65. if (dma_mask == 0)
  66. dma_mask = 0xffffffff;
  67. /* Don't invoke OOM killer */
  68. gfp |= __GFP_NORETRY;
  69. /* Kludge to make it bug-to-bug compatible with i386. i386
  70. uses the normal dma_mask for alloc_coherent. */
  71. dma_mask &= *dev->dma_mask;
  72. /* Why <=? Even when the mask is smaller than 4GB it is often
  73. larger than 16MB and in this case we have a chance of
  74. finding fitting memory in the next higher zone first. If
  75. not retry with true GFP_DMA. -AK */
  76. if (dma_mask <= 0xffffffff)
  77. gfp |= GFP_DMA32;
  78. again:
  79. memory = dma_alloc_pages(dev, gfp, get_order(size));
  80. if (memory == NULL)
  81. return NULL;
  82. {
  83. int high, mmu;
  84. bus = virt_to_bus(memory);
  85. high = (bus + size) >= dma_mask;
  86. mmu = high;
  87. if (force_iommu && !(gfp & GFP_DMA))
  88. mmu = 1;
  89. else if (high) {
  90. free_pages((unsigned long)memory,
  91. get_order(size));
  92. /* Don't use the 16MB ZONE_DMA unless absolutely
  93. needed. It's better to use remapping first. */
  94. if (dma_mask < 0xffffffff && !(gfp & GFP_DMA)) {
  95. gfp = (gfp & ~GFP_DMA32) | GFP_DMA;
  96. goto again;
  97. }
  98. /* Let low level make its own zone decisions */
  99. gfp &= ~(GFP_DMA32|GFP_DMA);
  100. if (dma_ops->alloc_coherent)
  101. return dma_ops->alloc_coherent(dev, size,
  102. dma_handle, gfp);
  103. return NULL;
  104. }
  105. memset(memory, 0, size);
  106. if (!mmu) {
  107. *dma_handle = virt_to_bus(memory);
  108. return memory;
  109. }
  110. }
  111. if (dma_ops->alloc_coherent) {
  112. free_pages((unsigned long)memory, get_order(size));
  113. gfp &= ~(GFP_DMA|GFP_DMA32);
  114. return dma_ops->alloc_coherent(dev, size, dma_handle, gfp);
  115. }
  116. if (dma_ops->map_simple) {
  117. *dma_handle = dma_ops->map_simple(dev, memory,
  118. size,
  119. PCI_DMA_BIDIRECTIONAL);
  120. if (*dma_handle != bad_dma_address)
  121. return memory;
  122. }
  123. if (panic_on_overflow)
  124. panic("dma_alloc_coherent: IOMMU overflow by %lu bytes\n",size);
  125. free_pages((unsigned long)memory, get_order(size));
  126. return NULL;
  127. }
  128. EXPORT_SYMBOL(dma_alloc_coherent);
  129. /*
  130. * Unmap coherent memory.
  131. * The caller must ensure that the device has finished accessing the mapping.
  132. */
  133. void dma_free_coherent(struct device *dev, size_t size,
  134. void *vaddr, dma_addr_t bus)
  135. {
  136. if (dma_ops->unmap_single)
  137. dma_ops->unmap_single(dev, bus, size, 0);
  138. free_pages((unsigned long)vaddr, get_order(size));
  139. }
  140. EXPORT_SYMBOL(dma_free_coherent);
  141. int dma_supported(struct device *dev, u64 mask)
  142. {
  143. if (dma_ops->dma_supported)
  144. return dma_ops->dma_supported(dev, mask);
  145. /* Copied from i386. Doesn't make much sense, because it will
  146. only work for pci_alloc_coherent.
  147. The caller just has to use GFP_DMA in this case. */
  148. if (mask < 0x00ffffff)
  149. return 0;
  150. /* Tell the device to use SAC when IOMMU force is on. This
  151. allows the driver to use cheaper accesses in some cases.
  152. Problem with this is that if we overflow the IOMMU area and
  153. return DAC as fallback address the device may not handle it
  154. correctly.
  155. As a special case some controllers have a 39bit address
  156. mode that is as efficient as 32bit (aic79xx). Don't force
  157. SAC for these. Assume all masks <= 40 bits are of this
  158. type. Normally this doesn't make any difference, but gives
  159. more gentle handling of IOMMU overflow. */
  160. if (iommu_sac_force && (mask >= 0xffffffffffULL)) {
  161. printk(KERN_INFO "%s: Force SAC with mask %Lx\n", dev->bus_id,mask);
  162. return 0;
  163. }
  164. return 1;
  165. }
  166. EXPORT_SYMBOL(dma_supported);
  167. int dma_set_mask(struct device *dev, u64 mask)
  168. {
  169. if (!dev->dma_mask || !dma_supported(dev, mask))
  170. return -EIO;
  171. *dev->dma_mask = mask;
  172. return 0;
  173. }
  174. EXPORT_SYMBOL(dma_set_mask);
  175. /* iommu=[size][,noagp][,off][,force][,noforce][,leak][,memaper[=order]][,merge]
  176. [,forcesac][,fullflush][,nomerge][,biomerge]
  177. size set size of iommu (in bytes)
  178. noagp don't initialize the AGP driver and use full aperture.
  179. off don't use the IOMMU
  180. leak turn on simple iommu leak tracing (only when CONFIG_IOMMU_LEAK is on)
  181. memaper[=order] allocate an own aperture over RAM with size 32MB^order.
  182. noforce don't force IOMMU usage. Default.
  183. force Force IOMMU.
  184. merge Do lazy merging. This may improve performance on some block devices.
  185. Implies force (experimental)
  186. biomerge Do merging at the BIO layer. This is more efficient than merge,
  187. but should be only done with very big IOMMUs. Implies merge,force.
  188. nomerge Don't do SG merging.
  189. forcesac For SAC mode for masks <40bits (experimental)
  190. fullflush Flush IOMMU on each allocation (default)
  191. nofullflush Don't use IOMMU fullflush
  192. allowed overwrite iommu off workarounds for specific chipsets.
  193. soft Use software bounce buffering (default for Intel machines)
  194. noaperture Don't touch the aperture for AGP.
  195. */
  196. __init int iommu_setup(char *p)
  197. {
  198. iommu_merge = 1;
  199. while (*p) {
  200. if (!strncmp(p,"off",3))
  201. no_iommu = 1;
  202. /* gart_parse_options has more force support */
  203. if (!strncmp(p,"force",5))
  204. force_iommu = 1;
  205. if (!strncmp(p,"noforce",7)) {
  206. iommu_merge = 0;
  207. force_iommu = 0;
  208. }
  209. if (!strncmp(p, "biomerge",8)) {
  210. iommu_bio_merge = 4096;
  211. iommu_merge = 1;
  212. force_iommu = 1;
  213. }
  214. if (!strncmp(p, "panic",5))
  215. panic_on_overflow = 1;
  216. if (!strncmp(p, "nopanic",7))
  217. panic_on_overflow = 0;
  218. if (!strncmp(p, "merge",5)) {
  219. iommu_merge = 1;
  220. force_iommu = 1;
  221. }
  222. if (!strncmp(p, "nomerge",7))
  223. iommu_merge = 0;
  224. if (!strncmp(p, "forcesac",8))
  225. iommu_sac_force = 1;
  226. #ifdef CONFIG_SWIOTLB
  227. if (!strncmp(p, "soft",4))
  228. swiotlb = 1;
  229. #endif
  230. #ifdef CONFIG_GART_IOMMU
  231. gart_parse_options(p);
  232. #endif
  233. p += strcspn(p, ",");
  234. if (*p == ',')
  235. ++p;
  236. }
  237. return 1;
  238. }