hwsw_iommu.c 5.9 KB

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