hwsw_iommu.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 void swiotlb_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 swiotlb_map_single;
  21. extern ia64_mv_dma_unmap_single swiotlb_unmap_single;
  22. extern ia64_mv_dma_map_sg swiotlb_map_sg;
  23. extern ia64_mv_dma_unmap_sg swiotlb_unmap_sg;
  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 sba_map_single;
  30. extern ia64_mv_dma_unmap_single sba_unmap_single;
  31. extern ia64_mv_dma_map_sg sba_map_sg;
  32. extern ia64_mv_dma_unmap_sg sba_unmap_sg;
  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 sba_map_single
  38. #define hwiommu_unmap_single sba_unmap_single
  39. #define hwiommu_map_sg sba_map_sg
  40. #define hwiommu_unmap_sg sba_unmap_sg
  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
  58. hwsw_init (void)
  59. {
  60. /* default to a smallish 2MB sw I/O TLB */
  61. swiotlb_init_with_default_size (2 * (1<<20));
  62. }
  63. void *
  64. hwsw_alloc_coherent (struct device *dev, size_t size, dma_addr_t *dma_handle, int flags)
  65. {
  66. if (use_swiotlb(dev))
  67. return swiotlb_alloc_coherent(dev, size, dma_handle, flags);
  68. else
  69. return hwiommu_alloc_coherent(dev, size, dma_handle, flags);
  70. }
  71. void
  72. hwsw_free_coherent (struct device *dev, size_t size, void *vaddr, dma_addr_t dma_handle)
  73. {
  74. if (use_swiotlb(dev))
  75. swiotlb_free_coherent(dev, size, vaddr, dma_handle);
  76. else
  77. hwiommu_free_coherent(dev, size, vaddr, dma_handle);
  78. }
  79. dma_addr_t
  80. hwsw_map_single (struct device *dev, void *addr, size_t size, int dir)
  81. {
  82. if (use_swiotlb(dev))
  83. return swiotlb_map_single(dev, addr, size, dir);
  84. else
  85. return hwiommu_map_single(dev, addr, size, dir);
  86. }
  87. void
  88. hwsw_unmap_single (struct device *dev, dma_addr_t iova, size_t size, int dir)
  89. {
  90. if (use_swiotlb(dev))
  91. return swiotlb_unmap_single(dev, iova, size, dir);
  92. else
  93. return hwiommu_unmap_single(dev, iova, size, dir);
  94. }
  95. int
  96. hwsw_map_sg (struct device *dev, struct scatterlist *sglist, int nents, int dir)
  97. {
  98. if (use_swiotlb(dev))
  99. return swiotlb_map_sg(dev, sglist, nents, dir);
  100. else
  101. return hwiommu_map_sg(dev, sglist, nents, dir);
  102. }
  103. void
  104. hwsw_unmap_sg (struct device *dev, struct scatterlist *sglist, int nents, int dir)
  105. {
  106. if (use_swiotlb(dev))
  107. return swiotlb_unmap_sg(dev, sglist, nents, dir);
  108. else
  109. return hwiommu_unmap_sg(dev, sglist, nents, dir);
  110. }
  111. void
  112. hwsw_sync_single_for_cpu (struct device *dev, dma_addr_t addr, size_t size, int dir)
  113. {
  114. if (use_swiotlb(dev))
  115. swiotlb_sync_single_for_cpu(dev, addr, size, dir);
  116. else
  117. hwiommu_sync_single_for_cpu(dev, addr, size, dir);
  118. }
  119. void
  120. hwsw_sync_sg_for_cpu (struct device *dev, struct scatterlist *sg, int nelems, int dir)
  121. {
  122. if (use_swiotlb(dev))
  123. swiotlb_sync_sg_for_cpu(dev, sg, nelems, dir);
  124. else
  125. hwiommu_sync_sg_for_cpu(dev, sg, nelems, dir);
  126. }
  127. void
  128. hwsw_sync_single_for_device (struct device *dev, dma_addr_t addr, size_t size, int dir)
  129. {
  130. if (use_swiotlb(dev))
  131. swiotlb_sync_single_for_device(dev, addr, size, dir);
  132. else
  133. hwiommu_sync_single_for_device(dev, addr, size, dir);
  134. }
  135. void
  136. hwsw_sync_sg_for_device (struct device *dev, struct scatterlist *sg, int nelems, int dir)
  137. {
  138. if (use_swiotlb(dev))
  139. swiotlb_sync_sg_for_device(dev, sg, nelems, dir);
  140. else
  141. hwiommu_sync_sg_for_device(dev, sg, nelems, dir);
  142. }
  143. int
  144. hwsw_dma_supported (struct device *dev, u64 mask)
  145. {
  146. if (hwiommu_dma_supported(dev, mask))
  147. return 1;
  148. return swiotlb_dma_supported(dev, mask);
  149. }
  150. int
  151. hwsw_dma_mapping_error (dma_addr_t dma_addr)
  152. {
  153. return hwiommu_dma_mapping_error (dma_addr) || swiotlb_dma_mapping_error(dma_addr);
  154. }
  155. EXPORT_SYMBOL(hwsw_dma_mapping_error);
  156. EXPORT_SYMBOL(hwsw_map_single);
  157. EXPORT_SYMBOL(hwsw_unmap_single);
  158. EXPORT_SYMBOL(hwsw_map_sg);
  159. EXPORT_SYMBOL(hwsw_unmap_sg);
  160. EXPORT_SYMBOL(hwsw_dma_supported);
  161. EXPORT_SYMBOL(hwsw_alloc_coherent);
  162. EXPORT_SYMBOL(hwsw_free_coherent);