18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 28c2ecf20Sopenharmony_ci#include <linux/prefetch.h> 38c2ecf20Sopenharmony_ci 48c2ecf20Sopenharmony_ci/** 58c2ecf20Sopenharmony_ci * iommu_fill_pdir - Insert coalesced scatter/gather chunks into the I/O Pdir. 68c2ecf20Sopenharmony_ci * @ioc: The I/O Controller. 78c2ecf20Sopenharmony_ci * @startsg: The scatter/gather list of coalesced chunks. 88c2ecf20Sopenharmony_ci * @nents: The number of entries in the scatter/gather list. 98c2ecf20Sopenharmony_ci * @hint: The DMA Hint. 108c2ecf20Sopenharmony_ci * 118c2ecf20Sopenharmony_ci * This function inserts the coalesced scatter/gather list chunks into the 128c2ecf20Sopenharmony_ci * I/O Controller's I/O Pdir. 138c2ecf20Sopenharmony_ci */ 148c2ecf20Sopenharmony_cistatic inline unsigned int 158c2ecf20Sopenharmony_ciiommu_fill_pdir(struct ioc *ioc, struct scatterlist *startsg, int nents, 168c2ecf20Sopenharmony_ci unsigned long hint, 178c2ecf20Sopenharmony_ci void (*iommu_io_pdir_entry)(u64 *, space_t, unsigned long, 188c2ecf20Sopenharmony_ci unsigned long)) 198c2ecf20Sopenharmony_ci{ 208c2ecf20Sopenharmony_ci struct scatterlist *dma_sg = startsg; /* pointer to current DMA */ 218c2ecf20Sopenharmony_ci unsigned int n_mappings = 0; 228c2ecf20Sopenharmony_ci unsigned long dma_offset = 0, dma_len = 0; 238c2ecf20Sopenharmony_ci u64 *pdirp = NULL; 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci /* Horrible hack. For efficiency's sake, dma_sg starts one 268c2ecf20Sopenharmony_ci * entry below the true start (it is immediately incremented 278c2ecf20Sopenharmony_ci * in the loop) */ 288c2ecf20Sopenharmony_ci dma_sg--; 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci while (nents-- > 0) { 318c2ecf20Sopenharmony_ci unsigned long vaddr; 328c2ecf20Sopenharmony_ci long size; 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci DBG_RUN_SG(" %d : %08lx/%05x %p/%05x\n", nents, 358c2ecf20Sopenharmony_ci (unsigned long)sg_dma_address(startsg), cnt, 368c2ecf20Sopenharmony_ci sg_virt(startsg), startsg->length 378c2ecf20Sopenharmony_ci ); 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci /* 418c2ecf20Sopenharmony_ci ** Look for the start of a new DMA stream 428c2ecf20Sopenharmony_ci */ 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci if (sg_dma_address(startsg) & PIDE_FLAG) { 458c2ecf20Sopenharmony_ci u32 pide = sg_dma_address(startsg) & ~PIDE_FLAG; 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci BUG_ON(pdirp && (dma_len != sg_dma_len(dma_sg))); 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci dma_sg++; 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci dma_len = sg_dma_len(startsg); 528c2ecf20Sopenharmony_ci sg_dma_len(startsg) = 0; 538c2ecf20Sopenharmony_ci dma_offset = (unsigned long) pide & ~IOVP_MASK; 548c2ecf20Sopenharmony_ci n_mappings++; 558c2ecf20Sopenharmony_ci#if defined(ZX1_SUPPORT) 568c2ecf20Sopenharmony_ci /* Pluto IOMMU IO Virt Address is not zero based */ 578c2ecf20Sopenharmony_ci sg_dma_address(dma_sg) = pide | ioc->ibase; 588c2ecf20Sopenharmony_ci#else 598c2ecf20Sopenharmony_ci /* SBA, ccio, and dino are zero based. 608c2ecf20Sopenharmony_ci * Trying to save a few CPU cycles for most users. 618c2ecf20Sopenharmony_ci */ 628c2ecf20Sopenharmony_ci sg_dma_address(dma_sg) = pide; 638c2ecf20Sopenharmony_ci#endif 648c2ecf20Sopenharmony_ci pdirp = &(ioc->pdir_base[pide >> IOVP_SHIFT]); 658c2ecf20Sopenharmony_ci prefetchw(pdirp); 668c2ecf20Sopenharmony_ci } 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci BUG_ON(pdirp == NULL); 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci vaddr = (unsigned long)sg_virt(startsg); 718c2ecf20Sopenharmony_ci sg_dma_len(dma_sg) += startsg->length; 728c2ecf20Sopenharmony_ci size = startsg->length + dma_offset; 738c2ecf20Sopenharmony_ci dma_offset = 0; 748c2ecf20Sopenharmony_ci#ifdef IOMMU_MAP_STATS 758c2ecf20Sopenharmony_ci ioc->msg_pages += startsg->length >> IOVP_SHIFT; 768c2ecf20Sopenharmony_ci#endif 778c2ecf20Sopenharmony_ci do { 788c2ecf20Sopenharmony_ci iommu_io_pdir_entry(pdirp, KERNEL_SPACE, 798c2ecf20Sopenharmony_ci vaddr, hint); 808c2ecf20Sopenharmony_ci vaddr += IOVP_SIZE; 818c2ecf20Sopenharmony_ci size -= IOVP_SIZE; 828c2ecf20Sopenharmony_ci pdirp++; 838c2ecf20Sopenharmony_ci } while(unlikely(size > 0)); 848c2ecf20Sopenharmony_ci startsg++; 858c2ecf20Sopenharmony_ci } 868c2ecf20Sopenharmony_ci return(n_mappings); 878c2ecf20Sopenharmony_ci} 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci/* 918c2ecf20Sopenharmony_ci** First pass is to walk the SG list and determine where the breaks are 928c2ecf20Sopenharmony_ci** in the DMA stream. Allocates PDIR entries but does not fill them. 938c2ecf20Sopenharmony_ci** Returns the number of DMA chunks. 948c2ecf20Sopenharmony_ci** 958c2ecf20Sopenharmony_ci** Doing the fill separate from the coalescing/allocation keeps the 968c2ecf20Sopenharmony_ci** code simpler. Future enhancement could make one pass through 978c2ecf20Sopenharmony_ci** the sglist do both. 988c2ecf20Sopenharmony_ci*/ 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_cistatic inline unsigned int 1018c2ecf20Sopenharmony_ciiommu_coalesce_chunks(struct ioc *ioc, struct device *dev, 1028c2ecf20Sopenharmony_ci struct scatterlist *startsg, int nents, 1038c2ecf20Sopenharmony_ci int (*iommu_alloc_range)(struct ioc *, struct device *, size_t)) 1048c2ecf20Sopenharmony_ci{ 1058c2ecf20Sopenharmony_ci struct scatterlist *contig_sg; /* contig chunk head */ 1068c2ecf20Sopenharmony_ci unsigned long dma_offset, dma_len; /* start/len of DMA stream */ 1078c2ecf20Sopenharmony_ci unsigned int n_mappings = 0; 1088c2ecf20Sopenharmony_ci unsigned int max_seg_size = min(dma_get_max_seg_size(dev), 1098c2ecf20Sopenharmony_ci (unsigned)DMA_CHUNK_SIZE); 1108c2ecf20Sopenharmony_ci unsigned int max_seg_boundary = dma_get_seg_boundary(dev) + 1; 1118c2ecf20Sopenharmony_ci if (max_seg_boundary) /* check if the addition above didn't overflow */ 1128c2ecf20Sopenharmony_ci max_seg_size = min(max_seg_size, max_seg_boundary); 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci while (nents > 0) { 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci /* 1178c2ecf20Sopenharmony_ci ** Prepare for first/next DMA stream 1188c2ecf20Sopenharmony_ci */ 1198c2ecf20Sopenharmony_ci contig_sg = startsg; 1208c2ecf20Sopenharmony_ci dma_len = startsg->length; 1218c2ecf20Sopenharmony_ci dma_offset = startsg->offset; 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci /* PARANOID: clear entries */ 1248c2ecf20Sopenharmony_ci sg_dma_address(startsg) = 0; 1258c2ecf20Sopenharmony_ci sg_dma_len(startsg) = 0; 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci /* 1288c2ecf20Sopenharmony_ci ** This loop terminates one iteration "early" since 1298c2ecf20Sopenharmony_ci ** it's always looking one "ahead". 1308c2ecf20Sopenharmony_ci */ 1318c2ecf20Sopenharmony_ci while(--nents > 0) { 1328c2ecf20Sopenharmony_ci unsigned long prev_end, sg_start; 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci prev_end = (unsigned long)sg_virt(startsg) + 1358c2ecf20Sopenharmony_ci startsg->length; 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci startsg++; 1388c2ecf20Sopenharmony_ci sg_start = (unsigned long)sg_virt(startsg); 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci /* PARANOID: clear entries */ 1418c2ecf20Sopenharmony_ci sg_dma_address(startsg) = 0; 1428c2ecf20Sopenharmony_ci sg_dma_len(startsg) = 0; 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci /* 1458c2ecf20Sopenharmony_ci ** First make sure current dma stream won't 1468c2ecf20Sopenharmony_ci ** exceed max_seg_size if we coalesce the 1478c2ecf20Sopenharmony_ci ** next entry. 1488c2ecf20Sopenharmony_ci */ 1498c2ecf20Sopenharmony_ci if (unlikely(ALIGN(dma_len + dma_offset + startsg->length, IOVP_SIZE) > 1508c2ecf20Sopenharmony_ci max_seg_size)) 1518c2ecf20Sopenharmony_ci break; 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci /* 1548c2ecf20Sopenharmony_ci * Next see if we can append the next chunk (i.e. 1558c2ecf20Sopenharmony_ci * it must end on one page and begin on another, or 1568c2ecf20Sopenharmony_ci * it must start on the same address as the previous 1578c2ecf20Sopenharmony_ci * entry ended. 1588c2ecf20Sopenharmony_ci */ 1598c2ecf20Sopenharmony_ci if (unlikely((prev_end != sg_start) || 1608c2ecf20Sopenharmony_ci ((prev_end | sg_start) & ~PAGE_MASK))) 1618c2ecf20Sopenharmony_ci break; 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci dma_len += startsg->length; 1648c2ecf20Sopenharmony_ci } 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci /* 1678c2ecf20Sopenharmony_ci ** End of DMA Stream 1688c2ecf20Sopenharmony_ci ** Terminate last VCONTIG block. 1698c2ecf20Sopenharmony_ci ** Allocate space for DMA stream. 1708c2ecf20Sopenharmony_ci */ 1718c2ecf20Sopenharmony_ci sg_dma_len(contig_sg) = dma_len; 1728c2ecf20Sopenharmony_ci dma_len = ALIGN(dma_len + dma_offset, IOVP_SIZE); 1738c2ecf20Sopenharmony_ci sg_dma_address(contig_sg) = 1748c2ecf20Sopenharmony_ci PIDE_FLAG 1758c2ecf20Sopenharmony_ci | (iommu_alloc_range(ioc, dev, dma_len) << IOVP_SHIFT) 1768c2ecf20Sopenharmony_ci | dma_offset; 1778c2ecf20Sopenharmony_ci n_mappings++; 1788c2ecf20Sopenharmony_ci } 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ci return n_mappings; 1818c2ecf20Sopenharmony_ci} 1828c2ecf20Sopenharmony_ci 183