hwsw_iommu.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 <asm/machvec.h>
  16. /* swiotlb declarations & definitions: */
  17. extern int swiotlb_late_init_with_default_size (size_t size);
  18. extern ia64_mv_dma_alloc_coherent swiotlb_alloc_coherent;
  19. extern ia64_mv_dma_free_coherent swiotlb_free_coherent;
  20. extern ia64_mv_dma_map_single_attrs swiotlb_map_single_attrs;
  21. extern ia64_mv_dma_unmap_single_attrs swiotlb_unmap_single_attrs;
  22. extern ia64_mv_dma_map_sg_attrs swiotlb_map_sg_attrs;
  23. extern ia64_mv_dma_unmap_sg_attrs swiotlb_unmap_sg_attrs;
  24. extern ia64_mv_dma_supported swiotlb_dma_supported;
  25. extern ia64_mv_dma_mapping_error swiotlb_dma_mapping_error;
  26. /* hwiommu declarations & definitions: */
  27. extern ia64_mv_dma_alloc_coherent sba_alloc_coherent;
  28. extern ia64_mv_dma_free_coherent sba_free_coherent;
  29. extern ia64_mv_dma_map_single_attrs sba_map_single_attrs;
  30. extern ia64_mv_dma_unmap_single_attrs sba_unmap_single_attrs;
  31. extern ia64_mv_dma_map_sg_attrs sba_map_sg_attrs;
  32. extern ia64_mv_dma_unmap_sg_attrs sba_unmap_sg_attrs;
  33. extern ia64_mv_dma_supported sba_dma_supported;
  34. extern ia64_mv_dma_mapping_error sba_dma_mapping_error;
  35. #define hwiommu_alloc_coherent sba_alloc_coherent
  36. #define hwiommu_free_coherent sba_free_coherent
  37. #define hwiommu_map_single_attrs sba_map_single_attrs
  38. #define hwiommu_unmap_single_attrs sba_unmap_single_attrs
  39. #define hwiommu_map_sg_attrs sba_map_sg_attrs
  40. #define hwiommu_unmap_sg_attrs sba_unmap_sg_attrs
  41. #define hwiommu_dma_supported sba_dma_supported
  42. #define hwiommu_dma_mapping_error sba_dma_mapping_error
  43. #define hwiommu_sync_single_for_cpu machvec_dma_sync_single
  44. #define hwiommu_sync_sg_for_cpu machvec_dma_sync_sg
  45. #define hwiommu_sync_single_for_device machvec_dma_sync_single
  46. #define hwiommu_sync_sg_for_device machvec_dma_sync_sg
  47. /*
  48. * Note: we need to make the determination of whether or not to use
  49. * the sw I/O TLB based purely on the device structure. Anything else
  50. * would be unreliable or would be too intrusive.
  51. */
  52. static inline int
  53. use_swiotlb (struct device *dev)
  54. {
  55. return dev && dev->dma_mask && !hwiommu_dma_supported(dev, *dev->dma_mask);
  56. }
  57. void __init
  58. hwsw_init (void)
  59. {
  60. /* default to a smallish 2MB sw I/O TLB */
  61. if (swiotlb_late_init_with_default_size (2 * (1<<20)) != 0) {
  62. #ifdef CONFIG_IA64_GENERIC
  63. /* Better to have normal DMA than panic */
  64. printk(KERN_WARNING "%s: Failed to initialize software I/O TLB,"
  65. " reverting to hpzx1 platform vector\n", __func__);
  66. machvec_init("hpzx1");
  67. #else
  68. panic("Unable to initialize software I/O TLB services");
  69. #endif
  70. }
  71. }
  72. void *
  73. hwsw_alloc_coherent (struct device *dev, size_t size, dma_addr_t *dma_handle, gfp_t flags)
  74. {
  75. if (use_swiotlb(dev))
  76. return swiotlb_alloc_coherent(dev, size, dma_handle, flags);
  77. else
  78. return hwiommu_alloc_coherent(dev, size, dma_handle, flags);
  79. }
  80. void
  81. hwsw_free_coherent (struct device *dev, size_t size, void *vaddr, dma_addr_t dma_handle)
  82. {
  83. if (use_swiotlb(dev))
  84. swiotlb_free_coherent(dev, size, vaddr, dma_handle);
  85. else
  86. hwiommu_free_coherent(dev, size, vaddr, dma_handle);
  87. }
  88. dma_addr_t
  89. hwsw_map_single_attrs(struct device *dev, void *addr, size_t size, int dir,
  90. struct dma_attrs *attrs)
  91. {
  92. if (use_swiotlb(dev))
  93. return swiotlb_map_single_attrs(dev, addr, size, dir, attrs);
  94. else
  95. return hwiommu_map_single_attrs(dev, addr, size, dir, attrs);
  96. }
  97. EXPORT_SYMBOL(hwsw_map_single_attrs);
  98. void
  99. hwsw_unmap_single_attrs(struct device *dev, dma_addr_t iova, size_t size,
  100. int dir, struct dma_attrs *attrs)
  101. {
  102. if (use_swiotlb(dev))
  103. return swiotlb_unmap_single_attrs(dev, iova, size, dir, attrs);
  104. else
  105. return hwiommu_unmap_single_attrs(dev, iova, size, dir, attrs);
  106. }
  107. EXPORT_SYMBOL(hwsw_unmap_single_attrs);
  108. int
  109. hwsw_map_sg_attrs(struct device *dev, struct scatterlist *sglist, int nents,
  110. int dir, struct dma_attrs *attrs)
  111. {
  112. if (use_swiotlb(dev))
  113. return swiotlb_map_sg_attrs(dev, sglist, nents, dir, attrs);
  114. else
  115. return hwiommu_map_sg_attrs(dev, sglist, nents, dir, attrs);
  116. }
  117. EXPORT_SYMBOL(hwsw_map_sg_attrs);
  118. void
  119. hwsw_unmap_sg_attrs(struct device *dev, struct scatterlist *sglist, int nents,
  120. int dir, struct dma_attrs *attrs)
  121. {
  122. if (use_swiotlb(dev))
  123. return swiotlb_unmap_sg_attrs(dev, sglist, nents, dir, attrs);
  124. else
  125. return hwiommu_unmap_sg_attrs(dev, sglist, nents, dir, attrs);
  126. }
  127. EXPORT_SYMBOL(hwsw_unmap_sg_attrs);
  128. void
  129. hwsw_sync_single_for_cpu (struct device *dev, dma_addr_t addr, size_t size, int dir)
  130. {
  131. if (use_swiotlb(dev))
  132. swiotlb_sync_single_for_cpu(dev, addr, size, dir);
  133. else
  134. hwiommu_sync_single_for_cpu(dev, addr, size, dir);
  135. }
  136. void
  137. hwsw_sync_sg_for_cpu (struct device *dev, struct scatterlist *sg, int nelems, int dir)
  138. {
  139. if (use_swiotlb(dev))
  140. swiotlb_sync_sg_for_cpu(dev, sg, nelems, dir);
  141. else
  142. hwiommu_sync_sg_for_cpu(dev, sg, nelems, dir);
  143. }
  144. void
  145. hwsw_sync_single_for_device (struct device *dev, dma_addr_t addr, size_t size, int dir)
  146. {
  147. if (use_swiotlb(dev))
  148. swiotlb_sync_single_for_device(dev, addr, size, dir);
  149. else
  150. hwiommu_sync_single_for_device(dev, addr, size, dir);
  151. }
  152. void
  153. hwsw_sync_sg_for_device (struct device *dev, struct scatterlist *sg, int nelems, int dir)
  154. {
  155. if (use_swiotlb(dev))
  156. swiotlb_sync_sg_for_device(dev, sg, nelems, dir);
  157. else
  158. hwiommu_sync_sg_for_device(dev, sg, nelems, dir);
  159. }
  160. int
  161. hwsw_dma_supported (struct device *dev, u64 mask)
  162. {
  163. if (hwiommu_dma_supported(dev, mask))
  164. return 1;
  165. return swiotlb_dma_supported(dev, mask);
  166. }
  167. int
  168. hwsw_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  169. {
  170. return hwiommu_dma_mapping_error(dev, dma_addr) ||
  171. swiotlb_dma_mapping_error(dev, dma_addr);
  172. }
  173. EXPORT_SYMBOL(hwsw_dma_mapping_error);
  174. EXPORT_SYMBOL(hwsw_dma_supported);
  175. EXPORT_SYMBOL(hwsw_alloc_coherent);
  176. EXPORT_SYMBOL(hwsw_free_coherent);
  177. EXPORT_SYMBOL(hwsw_sync_single_for_cpu);
  178. EXPORT_SYMBOL(hwsw_sync_single_for_device);
  179. EXPORT_SYMBOL(hwsw_sync_sg_for_cpu);
  180. EXPORT_SYMBOL(hwsw_sync_sg_for_device);