iommu-helpers.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 scatterlist *startsg, int nents,
  85. int (*iommu_alloc_range)(struct ioc *, size_t))
  86. {
  87. struct scatterlist *contig_sg; /* contig chunk head */
  88. unsigned long dma_offset, dma_len; /* start/len of DMA stream */
  89. unsigned int n_mappings = 0;
  90. while (nents > 0) {
  91. /*
  92. ** Prepare for first/next DMA stream
  93. */
  94. contig_sg = startsg;
  95. dma_len = startsg->length;
  96. dma_offset = sg_virt_addr(startsg) & ~IOVP_MASK;
  97. /* PARANOID: clear entries */
  98. sg_dma_address(startsg) = 0;
  99. sg_dma_len(startsg) = 0;
  100. /*
  101. ** This loop terminates one iteration "early" since
  102. ** it's always looking one "ahead".
  103. */
  104. while(--nents > 0) {
  105. unsigned long prevstartsg_end, startsg_end;
  106. prevstartsg_end = sg_virt_addr(startsg) +
  107. startsg->length;
  108. startsg++;
  109. startsg_end = sg_virt_addr(startsg) +
  110. startsg->length;
  111. /* PARANOID: clear entries */
  112. sg_dma_address(startsg) = 0;
  113. sg_dma_len(startsg) = 0;
  114. /*
  115. ** First make sure current dma stream won't
  116. ** exceed DMA_CHUNK_SIZE if we coalesce the
  117. ** next entry.
  118. */
  119. if(unlikely(ROUNDUP(dma_len + dma_offset + startsg->length,
  120. IOVP_SIZE) > DMA_CHUNK_SIZE))
  121. break;
  122. /*
  123. ** Next see if we can append the next chunk (i.e.
  124. ** it must end on one page and begin on another
  125. */
  126. if (unlikely(((prevstartsg_end | sg_virt_addr(startsg)) & ~PAGE_MASK) != 0))
  127. break;
  128. dma_len += startsg->length;
  129. }
  130. /*
  131. ** End of DMA Stream
  132. ** Terminate last VCONTIG block.
  133. ** Allocate space for DMA stream.
  134. */
  135. sg_dma_len(contig_sg) = dma_len;
  136. dma_len = ROUNDUP(dma_len + dma_offset, IOVP_SIZE);
  137. sg_dma_address(contig_sg) =
  138. PIDE_FLAG
  139. | (iommu_alloc_range(ioc, dma_len) << IOVP_SHIFT)
  140. | dma_offset;
  141. n_mappings++;
  142. }
  143. return n_mappings;
  144. }