pci-dma.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. #include <asm/calgary.h>
  12. int iommu_merge __read_mostly = 0;
  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. int iommu_sac_force __read_mostly = 0;
  21. EXPORT_SYMBOL(iommu_sac_force);
  22. int no_iommu __read_mostly;
  23. #ifdef CONFIG_IOMMU_DEBUG
  24. int panic_on_overflow __read_mostly = 1;
  25. int force_iommu __read_mostly = 1;
  26. #else
  27. int panic_on_overflow __read_mostly = 0;
  28. int force_iommu __read_mostly= 0;
  29. #endif
  30. /* Set this to 1 if there is a HW IOMMU in the system */
  31. int iommu_detected __read_mostly = 0;
  32. /* Dummy device used for NULL arguments (normally ISA). Better would
  33. be probably a smaller DMA mask, but this is bug-to-bug compatible
  34. to i386. */
  35. struct device fallback_dev = {
  36. .bus_id = "fallback device",
  37. .coherent_dma_mask = DMA_32BIT_MASK,
  38. .dma_mask = &fallback_dev.coherent_dma_mask,
  39. };
  40. /* Allocate DMA memory on node near device */
  41. noinline static void *
  42. dma_alloc_pages(struct device *dev, gfp_t gfp, unsigned order)
  43. {
  44. struct page *page;
  45. int node;
  46. #ifdef CONFIG_PCI
  47. if (dev->bus == &pci_bus_type)
  48. node = pcibus_to_node(to_pci_dev(dev)->bus);
  49. else
  50. #endif
  51. node = numa_node_id();
  52. if (node < first_node(node_online_map))
  53. node = first_node(node_online_map);
  54. page = alloc_pages_node(node, gfp, order);
  55. return page ? page_address(page) : NULL;
  56. }
  57. /*
  58. * Allocate memory for a coherent mapping.
  59. */
  60. void *
  61. dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
  62. gfp_t gfp)
  63. {
  64. void *memory;
  65. unsigned long dma_mask = 0;
  66. u64 bus;
  67. if (!dev)
  68. dev = &fallback_dev;
  69. dma_mask = dev->coherent_dma_mask;
  70. if (dma_mask == 0)
  71. dma_mask = DMA_32BIT_MASK;
  72. /* Don't invoke OOM killer */
  73. gfp |= __GFP_NORETRY;
  74. /* Kludge to make it bug-to-bug compatible with i386. i386
  75. uses the normal dma_mask for alloc_coherent. */
  76. dma_mask &= *dev->dma_mask;
  77. /* Why <=? Even when the mask is smaller than 4GB it is often
  78. larger than 16MB and in this case we have a chance of
  79. finding fitting memory in the next higher zone first. If
  80. not retry with true GFP_DMA. -AK */
  81. if (dma_mask <= DMA_32BIT_MASK)
  82. gfp |= GFP_DMA32;
  83. again:
  84. memory = dma_alloc_pages(dev, gfp, get_order(size));
  85. if (memory == NULL)
  86. return NULL;
  87. {
  88. int high, mmu;
  89. bus = virt_to_bus(memory);
  90. high = (bus + size) >= dma_mask;
  91. mmu = high;
  92. if (force_iommu && !(gfp & GFP_DMA))
  93. mmu = 1;
  94. else if (high) {
  95. free_pages((unsigned long)memory,
  96. get_order(size));
  97. /* Don't use the 16MB ZONE_DMA unless absolutely
  98. needed. It's better to use remapping first. */
  99. if (dma_mask < DMA_32BIT_MASK && !(gfp & GFP_DMA)) {
  100. gfp = (gfp & ~GFP_DMA32) | GFP_DMA;
  101. goto again;
  102. }
  103. /* Let low level make its own zone decisions */
  104. gfp &= ~(GFP_DMA32|GFP_DMA);
  105. if (dma_ops->alloc_coherent)
  106. return dma_ops->alloc_coherent(dev, size,
  107. dma_handle, gfp);
  108. return NULL;
  109. }
  110. memset(memory, 0, size);
  111. if (!mmu) {
  112. *dma_handle = virt_to_bus(memory);
  113. return memory;
  114. }
  115. }
  116. if (dma_ops->alloc_coherent) {
  117. free_pages((unsigned long)memory, get_order(size));
  118. gfp &= ~(GFP_DMA|GFP_DMA32);
  119. return dma_ops->alloc_coherent(dev, size, dma_handle, gfp);
  120. }
  121. if (dma_ops->map_simple) {
  122. *dma_handle = dma_ops->map_simple(dev, memory,
  123. size,
  124. PCI_DMA_BIDIRECTIONAL);
  125. if (*dma_handle != bad_dma_address)
  126. return memory;
  127. }
  128. if (panic_on_overflow)
  129. panic("dma_alloc_coherent: IOMMU overflow by %lu bytes\n",size);
  130. free_pages((unsigned long)memory, get_order(size));
  131. return NULL;
  132. }
  133. EXPORT_SYMBOL(dma_alloc_coherent);
  134. /*
  135. * Unmap coherent memory.
  136. * The caller must ensure that the device has finished accessing the mapping.
  137. */
  138. void dma_free_coherent(struct device *dev, size_t size,
  139. void *vaddr, dma_addr_t bus)
  140. {
  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. /* iommu=[size][,noagp][,off][,force][,noforce][,leak][,memaper[=order]][,merge]
  188. [,forcesac][,fullflush][,nomerge][,biomerge]
  189. size set size of iommu (in bytes)
  190. noagp don't initialize the AGP driver and use full aperture.
  191. off don't use the IOMMU
  192. leak turn on simple iommu leak tracing (only when CONFIG_IOMMU_LEAK is on)
  193. memaper[=order] allocate an own aperture over RAM with size 32MB^order.
  194. noforce don't force IOMMU usage. Default.
  195. force Force IOMMU.
  196. merge Do lazy merging. This may improve performance on some block devices.
  197. Implies force (experimental)
  198. biomerge Do merging at the BIO layer. This is more efficient than merge,
  199. but should be only done with very big IOMMUs. Implies merge,force.
  200. nomerge Don't do SG merging.
  201. forcesac For SAC mode for masks <40bits (experimental)
  202. fullflush Flush IOMMU on each allocation (default)
  203. nofullflush Don't use IOMMU fullflush
  204. allowed overwrite iommu off workarounds for specific chipsets.
  205. soft Use software bounce buffering (default for Intel machines)
  206. noaperture Don't touch the aperture for AGP.
  207. allowdac Allow DMA >4GB
  208. nodac Forbid DMA >4GB
  209. panic Force panic when IOMMU overflows
  210. */
  211. __init int iommu_setup(char *p)
  212. {
  213. iommu_merge = 1;
  214. if (!p)
  215. return -EINVAL;
  216. while (*p) {
  217. if (!strncmp(p,"off",3))
  218. no_iommu = 1;
  219. /* gart_parse_options has more force support */
  220. if (!strncmp(p,"force",5))
  221. force_iommu = 1;
  222. if (!strncmp(p,"noforce",7)) {
  223. iommu_merge = 0;
  224. force_iommu = 0;
  225. }
  226. if (!strncmp(p, "biomerge",8)) {
  227. iommu_bio_merge = 4096;
  228. iommu_merge = 1;
  229. force_iommu = 1;
  230. }
  231. if (!strncmp(p, "panic",5))
  232. panic_on_overflow = 1;
  233. if (!strncmp(p, "nopanic",7))
  234. panic_on_overflow = 0;
  235. if (!strncmp(p, "merge",5)) {
  236. iommu_merge = 1;
  237. force_iommu = 1;
  238. }
  239. if (!strncmp(p, "nomerge",7))
  240. iommu_merge = 0;
  241. if (!strncmp(p, "forcesac",8))
  242. iommu_sac_force = 1;
  243. if (!strncmp(p, "allowdac", 8))
  244. forbid_dac = 0;
  245. if (!strncmp(p, "nodac", 5))
  246. forbid_dac = -1;
  247. #ifdef CONFIG_SWIOTLB
  248. if (!strncmp(p, "soft",4))
  249. swiotlb = 1;
  250. #endif
  251. #ifdef CONFIG_IOMMU
  252. gart_parse_options(p);
  253. #endif
  254. p += strcspn(p, ",");
  255. if (*p == ',')
  256. ++p;
  257. }
  258. return 0;
  259. }
  260. early_param("iommu", iommu_setup);
  261. void __init pci_iommu_alloc(void)
  262. {
  263. /*
  264. * The order of these functions is important for
  265. * fall-back/fail-over reasons
  266. */
  267. #ifdef CONFIG_IOMMU
  268. iommu_hole_init();
  269. #endif
  270. #ifdef CONFIG_CALGARY_IOMMU
  271. detect_calgary();
  272. #endif
  273. #ifdef CONFIG_SWIOTLB
  274. pci_swiotlb_init();
  275. #endif
  276. }
  277. static int __init pci_iommu_init(void)
  278. {
  279. #ifdef CONFIG_CALGARY_IOMMU
  280. calgary_iommu_init();
  281. #endif
  282. #ifdef CONFIG_IOMMU
  283. gart_iommu_init();
  284. #endif
  285. no_iommu_init();
  286. return 0;
  287. }
  288. /* Must execute after PCI subsystem */
  289. fs_initcall(pci_iommu_init);