hwsw_iommu.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright (c) 2004 Hewlett-Packard Development Company, L.P.
  3. * Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
  4. *
  5. * This is a pseudo I/O MMU which dispatches to the hardware I/O MMU
  6. * whenever possible. We assume that the hardware I/O MMU requires
  7. * full 32-bit addressability, as is the case, e.g., for HP zx1-based
  8. * systems (there, the I/O MMU window is mapped at 3-4GB). If a
  9. * device doesn't provide full 32-bit addressability, we fall back on
  10. * the sw I/O TLB. This is good enough to let us support broken
  11. * hardware such as soundcards which have a DMA engine that can
  12. * address only 28 bits.
  13. */
  14. #include <linux/device.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/swiotlb.h>
  17. #include <asm/machvec.h>
  18. /* swiotlb declarations & definitions: */
  19. extern int swiotlb_late_init_with_default_size (size_t size);
  20. /* hwiommu declarations & definitions: */
  21. extern ia64_mv_dma_alloc_coherent sba_alloc_coherent;
  22. extern ia64_mv_dma_free_coherent sba_free_coherent;
  23. extern ia64_mv_dma_map_single_attrs sba_map_single_attrs;
  24. extern ia64_mv_dma_unmap_single_attrs sba_unmap_single_attrs;
  25. extern ia64_mv_dma_map_sg_attrs sba_map_sg_attrs;
  26. extern ia64_mv_dma_unmap_sg_attrs sba_unmap_sg_attrs;
  27. extern ia64_mv_dma_supported sba_dma_supported;
  28. extern ia64_mv_dma_mapping_error sba_dma_mapping_error;
  29. #define hwiommu_alloc_coherent sba_alloc_coherent
  30. #define hwiommu_free_coherent sba_free_coherent
  31. #define hwiommu_map_single_attrs sba_map_single_attrs
  32. #define hwiommu_unmap_single_attrs sba_unmap_single_attrs
  33. #define hwiommu_map_sg_attrs sba_map_sg_attrs
  34. #define hwiommu_unmap_sg_attrs sba_unmap_sg_attrs
  35. #define hwiommu_dma_supported sba_dma_supported
  36. #define hwiommu_dma_mapping_error sba_dma_mapping_error
  37. #define hwiommu_sync_single_for_cpu machvec_dma_sync_single
  38. #define hwiommu_sync_sg_for_cpu machvec_dma_sync_sg
  39. #define hwiommu_sync_single_for_device machvec_dma_sync_single
  40. #define hwiommu_sync_sg_for_device machvec_dma_sync_sg
  41. /*
  42. * Note: we need to make the determination of whether or not to use
  43. * the sw I/O TLB based purely on the device structure. Anything else
  44. * would be unreliable or would be too intrusive.
  45. */
  46. static inline int
  47. use_swiotlb (struct device *dev)
  48. {
  49. return dev && dev->dma_mask && !hwiommu_dma_supported(dev, *dev->dma_mask);
  50. }
  51. struct dma_mapping_ops hwsw_dma_ops;
  52. void __init
  53. hwsw_init (void)
  54. {
  55. dma_ops = &hwsw_dma_ops;
  56. /* default to a smallish 2MB sw I/O TLB */
  57. if (swiotlb_late_init_with_default_size (2 * (1<<20)) != 0) {
  58. #ifdef CONFIG_IA64_GENERIC
  59. /* Better to have normal DMA than panic */
  60. printk(KERN_WARNING "%s: Failed to initialize software I/O TLB,"
  61. " reverting to hpzx1 platform vector\n", __func__);
  62. machvec_init("hpzx1");
  63. #else
  64. panic("Unable to initialize software I/O TLB services");
  65. #endif
  66. }
  67. }
  68. void *
  69. hwsw_alloc_coherent (struct device *dev, size_t size, dma_addr_t *dma_handle, gfp_t flags)
  70. {
  71. if (use_swiotlb(dev))
  72. return swiotlb_alloc_coherent(dev, size, dma_handle, flags);
  73. else
  74. return hwiommu_alloc_coherent(dev, size, dma_handle, flags);
  75. }
  76. void
  77. hwsw_free_coherent (struct device *dev, size_t size, void *vaddr, dma_addr_t dma_handle)
  78. {
  79. if (use_swiotlb(dev))
  80. swiotlb_free_coherent(dev, size, vaddr, dma_handle);
  81. else
  82. hwiommu_free_coherent(dev, size, vaddr, dma_handle);
  83. }
  84. dma_addr_t
  85. hwsw_map_single_attrs(struct device *dev, void *addr, size_t size, int dir,
  86. struct dma_attrs *attrs)
  87. {
  88. if (use_swiotlb(dev))
  89. return swiotlb_map_single_attrs(dev, addr, size, dir, attrs);
  90. else
  91. return hwiommu_map_single_attrs(dev, addr, size, dir, attrs);
  92. }
  93. EXPORT_SYMBOL(hwsw_map_single_attrs);
  94. void
  95. hwsw_unmap_single_attrs(struct device *dev, dma_addr_t iova, size_t size,
  96. int dir, struct dma_attrs *attrs)
  97. {
  98. if (use_swiotlb(dev))
  99. return swiotlb_unmap_single_attrs(dev, iova, size, dir, attrs);
  100. else
  101. return hwiommu_unmap_single_attrs(dev, iova, size, dir, attrs);
  102. }
  103. EXPORT_SYMBOL(hwsw_unmap_single_attrs);
  104. int
  105. hwsw_map_sg_attrs(struct device *dev, struct scatterlist *sglist, int nents,
  106. int dir, struct dma_attrs *attrs)
  107. {
  108. if (use_swiotlb(dev))
  109. return swiotlb_map_sg_attrs(dev, sglist, nents, dir, attrs);
  110. else
  111. return hwiommu_map_sg_attrs(dev, sglist, nents, dir, attrs);
  112. }
  113. EXPORT_SYMBOL(hwsw_map_sg_attrs);
  114. void
  115. hwsw_unmap_sg_attrs(struct device *dev, struct scatterlist *sglist, int nents,
  116. int dir, struct dma_attrs *attrs)
  117. {
  118. if (use_swiotlb(dev))
  119. return swiotlb_unmap_sg_attrs(dev, sglist, nents, dir, attrs);
  120. else
  121. return hwiommu_unmap_sg_attrs(dev, sglist, nents, dir, attrs);
  122. }
  123. EXPORT_SYMBOL(hwsw_unmap_sg_attrs);
  124. void
  125. hwsw_sync_single_for_cpu (struct device *dev, dma_addr_t addr, size_t size, int dir)
  126. {
  127. if (use_swiotlb(dev))
  128. swiotlb_sync_single_for_cpu(dev, addr, size, dir);
  129. else
  130. hwiommu_sync_single_for_cpu(dev, addr, size, dir);
  131. }
  132. void
  133. hwsw_sync_sg_for_cpu (struct device *dev, struct scatterlist *sg, int nelems, int dir)
  134. {
  135. if (use_swiotlb(dev))
  136. swiotlb_sync_sg_for_cpu(dev, sg, nelems, dir);
  137. else
  138. hwiommu_sync_sg_for_cpu(dev, sg, nelems, dir);
  139. }
  140. void
  141. hwsw_sync_single_for_device (struct device *dev, dma_addr_t addr, size_t size, int dir)
  142. {
  143. if (use_swiotlb(dev))
  144. swiotlb_sync_single_for_device(dev, addr, size, dir);
  145. else
  146. hwiommu_sync_single_for_device(dev, addr, size, dir);
  147. }
  148. void
  149. hwsw_sync_sg_for_device (struct device *dev, struct scatterlist *sg, int nelems, int dir)
  150. {
  151. if (use_swiotlb(dev))
  152. swiotlb_sync_sg_for_device(dev, sg, nelems, dir);
  153. else
  154. hwiommu_sync_sg_for_device(dev, sg, nelems, dir);
  155. }
  156. int
  157. hwsw_dma_supported (struct device *dev, u64 mask)
  158. {
  159. if (hwiommu_dma_supported(dev, mask))
  160. return 1;
  161. return swiotlb_dma_supported(dev, mask);
  162. }
  163. int
  164. hwsw_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  165. {
  166. return hwiommu_dma_mapping_error(dev, dma_addr) ||
  167. swiotlb_dma_mapping_error(dev, dma_addr);
  168. }
  169. EXPORT_SYMBOL(hwsw_dma_mapping_error);
  170. EXPORT_SYMBOL(hwsw_dma_supported);
  171. EXPORT_SYMBOL(hwsw_alloc_coherent);
  172. EXPORT_SYMBOL(hwsw_free_coherent);
  173. EXPORT_SYMBOL(hwsw_sync_single_for_cpu);
  174. EXPORT_SYMBOL(hwsw_sync_single_for_device);
  175. EXPORT_SYMBOL(hwsw_sync_sg_for_cpu);
  176. EXPORT_SYMBOL(hwsw_sync_sg_for_device);
  177. struct dma_mapping_ops hwsw_dma_ops = {
  178. .alloc_coherent = hwsw_alloc_coherent,
  179. .free_coherent = hwsw_free_coherent,
  180. .map_single_attrs = hwsw_map_single_attrs,
  181. .unmap_single_attrs = hwsw_unmap_single_attrs,
  182. .map_sg_attrs = hwsw_map_sg_attrs,
  183. .unmap_sg_attrs = hwsw_unmap_sg_attrs,
  184. .sync_single_for_cpu = hwsw_sync_single_for_cpu,
  185. .sync_sg_for_cpu = hwsw_sync_sg_for_cpu,
  186. .sync_single_for_device = hwsw_sync_single_for_device,
  187. .sync_sg_for_device = hwsw_sync_sg_for_device,
  188. .dma_supported_op = hwsw_dma_supported,
  189. .mapping_error = hwsw_dma_mapping_error,
  190. };