iommu-helpers.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #include <linux/prefetch.h>
  3. /**
  4. * iommu_fill_pdir - Insert coalesced scatter/gather chunks into the I/O Pdir.
  5. * @ioc: The I/O Controller.
  6. * @startsg: The scatter/gather list of coalesced chunks.
  7. * @nents: The number of entries in the scatter/gather list.
  8. * @hint: The DMA Hint.
  9. *
  10. * This function inserts the coalesced scatter/gather list chunks into the
  11. * I/O Controller's I/O Pdir.
  12. */
  13. static inline unsigned int
  14. iommu_fill_pdir(struct ioc *ioc, struct scatterlist *startsg, int nents,
  15. unsigned long hint,
  16. void (*iommu_io_pdir_entry)(u64 *, space_t, unsigned long,
  17. unsigned long))
  18. {
  19. struct scatterlist *dma_sg = startsg; /* pointer to current DMA */
  20. unsigned int n_mappings = 0;
  21. unsigned long dma_offset = 0, dma_len = 0;
  22. u64 *pdirp = NULL;
  23. /* Horrible hack. For efficiency's sake, dma_sg starts one
  24. * entry below the true start (it is immediately incremented
  25. * in the loop) */
  26. dma_sg--;
  27. while (nents-- > 0) {
  28. unsigned long vaddr;
  29. long size;
  30. DBG_RUN_SG(" %d : %08lx/%05x %p/%05x\n", nents,
  31. (unsigned long)sg_dma_address(startsg), cnt,
  32. sg_virt(startsg), startsg->length
  33. );
  34. /*
  35. ** Look for the start of a new DMA stream
  36. */
  37. if (sg_dma_address(startsg) & PIDE_FLAG) {
  38. u32 pide = sg_dma_address(startsg) & ~PIDE_FLAG;
  39. BUG_ON(pdirp && (dma_len != sg_dma_len(dma_sg)));
  40. dma_sg++;
  41. dma_len = sg_dma_len(startsg);
  42. sg_dma_len(startsg) = 0;
  43. dma_offset = (unsigned long) pide & ~IOVP_MASK;
  44. n_mappings++;
  45. #if defined(ZX1_SUPPORT)
  46. /* Pluto IOMMU IO Virt Address is not zero based */
  47. sg_dma_address(dma_sg) = pide | ioc->ibase;
  48. #else
  49. /* SBA, ccio, and dino are zero based.
  50. * Trying to save a few CPU cycles for most users.
  51. */
  52. sg_dma_address(dma_sg) = pide;
  53. #endif
  54. pdirp = &(ioc->pdir_base[pide >> IOVP_SHIFT]);
  55. prefetchw(pdirp);
  56. }
  57. BUG_ON(pdirp == NULL);
  58. vaddr = (unsigned long)sg_virt(startsg);
  59. sg_dma_len(dma_sg) += startsg->length;
  60. size = startsg->length + dma_offset;
  61. dma_offset = 0;
  62. #ifdef IOMMU_MAP_STATS
  63. ioc->msg_pages += startsg->length >> IOVP_SHIFT;
  64. #endif
  65. do {
  66. iommu_io_pdir_entry(pdirp, KERNEL_SPACE,
  67. vaddr, hint);
  68. vaddr += IOVP_SIZE;
  69. size -= IOVP_SIZE;
  70. pdirp++;
  71. } while(unlikely(size > 0));
  72. startsg++;
  73. }
  74. return(n_mappings);
  75. }
  76. /*
  77. ** First pass is to walk the SG list and determine where the breaks are
  78. ** in the DMA stream. Allocates PDIR entries but does not fill them.
  79. ** Returns the number of DMA chunks.
  80. **
  81. ** Doing the fill separate from the coalescing/allocation keeps the
  82. ** code simpler. Future enhancement could make one pass through
  83. ** the sglist do both.
  84. */
  85. static inline unsigned int
  86. iommu_coalesce_chunks(struct ioc *ioc, struct device *dev,
  87. struct scatterlist *startsg, int nents,
  88. int (*iommu_alloc_range)(struct ioc *, struct device *, size_t))
  89. {
  90. struct scatterlist *contig_sg; /* contig chunk head */
  91. unsigned long dma_offset, dma_len; /* start/len of DMA stream */
  92. unsigned int n_mappings = 0;
  93. unsigned int max_seg_size = min(dma_get_max_seg_size(dev),
  94. (unsigned)DMA_CHUNK_SIZE);
  95. unsigned int max_seg_boundary = dma_get_seg_boundary(dev) + 1;
  96. if (max_seg_boundary) /* check if the addition above didn't overflow */
  97. max_seg_size = min(max_seg_size, max_seg_boundary);
  98. while (nents > 0) {
  99. /*
  100. ** Prepare for first/next DMA stream
  101. */
  102. contig_sg = startsg;
  103. dma_len = startsg->length;
  104. dma_offset = startsg->offset;
  105. /* PARANOID: clear entries */
  106. sg_dma_address(startsg) = 0;
  107. sg_dma_len(startsg) = 0;
  108. /*
  109. ** This loop terminates one iteration "early" since
  110. ** it's always looking one "ahead".
  111. */
  112. while(--nents > 0) {
  113. unsigned long prev_end, sg_start;
  114. prev_end = (unsigned long)sg_virt(startsg) +
  115. startsg->length;
  116. startsg++;
  117. sg_start = (unsigned long)sg_virt(startsg);
  118. /* PARANOID: clear entries */
  119. sg_dma_address(startsg) = 0;
  120. sg_dma_len(startsg) = 0;
  121. /*
  122. ** First make sure current dma stream won't
  123. ** exceed max_seg_size if we coalesce the
  124. ** next entry.
  125. */
  126. if (unlikely(ALIGN(dma_len + dma_offset + startsg->length, IOVP_SIZE) >
  127. max_seg_size))
  128. break;
  129. /*
  130. * Next see if we can append the next chunk (i.e.
  131. * it must end on one page and begin on another, or
  132. * it must start on the same address as the previous
  133. * entry ended.
  134. */
  135. if (unlikely((prev_end != sg_start) ||
  136. ((prev_end | sg_start) & ~PAGE_MASK)))
  137. break;
  138. dma_len += startsg->length;
  139. }
  140. /*
  141. ** End of DMA Stream
  142. ** Terminate last VCONTIG block.
  143. ** Allocate space for DMA stream.
  144. */
  145. sg_dma_len(contig_sg) = dma_len;
  146. dma_len = ALIGN(dma_len + dma_offset, IOVP_SIZE);
  147. sg_dma_address(contig_sg) =
  148. PIDE_FLAG
  149. | (iommu_alloc_range(ioc, dev, dma_len) << IOVP_SHIFT)
  150. | dma_offset;
  151. n_mappings++;
  152. }
  153. return n_mappings;
  154. }