hwsw_iommu.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. void __init
  52. hwsw_init (void)
  53. {
  54. /* default to a smallish 2MB sw I/O TLB */
  55. if (swiotlb_late_init_with_default_size (2 * (1<<20)) != 0) {
  56. #ifdef CONFIG_IA64_GENERIC
  57. /* Better to have normal DMA than panic */
  58. printk(KERN_WARNING "%s: Failed to initialize software I/O TLB,"
  59. " reverting to hpzx1 platform vector\n", __func__);
  60. machvec_init("hpzx1");
  61. #else
  62. panic("Unable to initialize software I/O TLB services");
  63. #endif
  64. }
  65. }
  66. void *
  67. hwsw_alloc_coherent (struct device *dev, size_t size, dma_addr_t *dma_handle, gfp_t flags)
  68. {
  69. if (use_swiotlb(dev))
  70. return swiotlb_alloc_coherent(dev, size, dma_handle, flags);
  71. else
  72. return hwiommu_alloc_coherent(dev, size, dma_handle, flags);
  73. }
  74. void
  75. hwsw_free_coherent (struct device *dev, size_t size, void *vaddr, dma_addr_t dma_handle)
  76. {
  77. if (use_swiotlb(dev))
  78. swiotlb_free_coherent(dev, size, vaddr, dma_handle);
  79. else
  80. hwiommu_free_coherent(dev, size, vaddr, dma_handle);
  81. }
  82. dma_addr_t
  83. hwsw_map_single_attrs(struct device *dev, void *addr, size_t size, int dir,
  84. struct dma_attrs *attrs)
  85. {
  86. if (use_swiotlb(dev))
  87. return swiotlb_map_single_attrs(dev, addr, size, dir, attrs);
  88. else
  89. return hwiommu_map_single_attrs(dev, addr, size, dir, attrs);
  90. }
  91. EXPORT_SYMBOL(hwsw_map_single_attrs);
  92. void
  93. hwsw_unmap_single_attrs(struct device *dev, dma_addr_t iova, size_t size,
  94. int dir, struct dma_attrs *attrs)
  95. {
  96. if (use_swiotlb(dev))
  97. return swiotlb_unmap_single_attrs(dev, iova, size, dir, attrs);
  98. else
  99. return hwiommu_unmap_single_attrs(dev, iova, size, dir, attrs);
  100. }
  101. EXPORT_SYMBOL(hwsw_unmap_single_attrs);
  102. int
  103. hwsw_map_sg_attrs(struct device *dev, struct scatterlist *sglist, int nents,
  104. int dir, struct dma_attrs *attrs)
  105. {
  106. if (use_swiotlb(dev))
  107. return swiotlb_map_sg_attrs(dev, sglist, nents, dir, attrs);
  108. else
  109. return hwiommu_map_sg_attrs(dev, sglist, nents, dir, attrs);
  110. }
  111. EXPORT_SYMBOL(hwsw_map_sg_attrs);
  112. void
  113. hwsw_unmap_sg_attrs(struct device *dev, struct scatterlist *sglist, int nents,
  114. int dir, struct dma_attrs *attrs)
  115. {
  116. if (use_swiotlb(dev))
  117. return swiotlb_unmap_sg_attrs(dev, sglist, nents, dir, attrs);
  118. else
  119. return hwiommu_unmap_sg_attrs(dev, sglist, nents, dir, attrs);
  120. }
  121. EXPORT_SYMBOL(hwsw_unmap_sg_attrs);
  122. void
  123. hwsw_sync_single_for_cpu (struct device *dev, dma_addr_t addr, size_t size, int dir)
  124. {
  125. if (use_swiotlb(dev))
  126. swiotlb_sync_single_for_cpu(dev, addr, size, dir);
  127. else
  128. hwiommu_sync_single_for_cpu(dev, addr, size, dir);
  129. }
  130. void
  131. hwsw_sync_sg_for_cpu (struct device *dev, struct scatterlist *sg, int nelems, int dir)
  132. {
  133. if (use_swiotlb(dev))
  134. swiotlb_sync_sg_for_cpu(dev, sg, nelems, dir);
  135. else
  136. hwiommu_sync_sg_for_cpu(dev, sg, nelems, dir);
  137. }
  138. void
  139. hwsw_sync_single_for_device (struct device *dev, dma_addr_t addr, size_t size, int dir)
  140. {
  141. if (use_swiotlb(dev))
  142. swiotlb_sync_single_for_device(dev, addr, size, dir);
  143. else
  144. hwiommu_sync_single_for_device(dev, addr, size, dir);
  145. }
  146. void
  147. hwsw_sync_sg_for_device (struct device *dev, struct scatterlist *sg, int nelems, int dir)
  148. {
  149. if (use_swiotlb(dev))
  150. swiotlb_sync_sg_for_device(dev, sg, nelems, dir);
  151. else
  152. hwiommu_sync_sg_for_device(dev, sg, nelems, dir);
  153. }
  154. int
  155. hwsw_dma_supported (struct device *dev, u64 mask)
  156. {
  157. if (hwiommu_dma_supported(dev, mask))
  158. return 1;
  159. return swiotlb_dma_supported(dev, mask);
  160. }
  161. int
  162. hwsw_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  163. {
  164. return hwiommu_dma_mapping_error(dev, dma_addr) ||
  165. swiotlb_dma_mapping_error(dev, dma_addr);
  166. }
  167. EXPORT_SYMBOL(hwsw_dma_mapping_error);
  168. EXPORT_SYMBOL(hwsw_dma_supported);
  169. EXPORT_SYMBOL(hwsw_alloc_coherent);
  170. EXPORT_SYMBOL(hwsw_free_coherent);
  171. EXPORT_SYMBOL(hwsw_sync_single_for_cpu);
  172. EXPORT_SYMBOL(hwsw_sync_single_for_device);
  173. EXPORT_SYMBOL(hwsw_sync_sg_for_cpu);
  174. EXPORT_SYMBOL(hwsw_sync_sg_for_device);
  175. struct dma_mapping_ops hwsw_dma_ops = {
  176. .alloc_coherent = hwsw_alloc_coherent,
  177. .free_coherent = hwsw_free_coherent,
  178. .map_single_attrs = hwsw_map_single_attrs,
  179. .unmap_single_attrs = hwsw_unmap_single_attrs,
  180. .map_sg_attrs = hwsw_map_sg_attrs,
  181. .unmap_sg_attrs = hwsw_unmap_sg_attrs,
  182. .sync_single_for_cpu = hwsw_sync_single_for_cpu,
  183. .sync_sg_for_cpu = hwsw_sync_sg_for_cpu,
  184. .sync_single_for_device = hwsw_sync_single_for_device,
  185. .sync_sg_for_device = hwsw_sync_sg_for_device,
  186. .dma_supported_op = hwsw_dma_supported,
  187. .mapping_error = hwsw_dma_mapping_error,
  188. };