iommu-helpers.h 4.5 KB

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