Files
2026-11-18 01:39:26 +09:00

49 lines
1.2 KiB
C

/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef PLATINA_IPA_V2_DMA_H
#define PLATINA_IPA_V2_DMA_H
#include <linux/dma-mapping.h>
#include <linux/iommu.h>
#include <linux/scatterlist.h>
/* Validate every page boundary against the DMA allocator's actual CPU pages. */
static inline int ipa_v2_verify_mapping(struct device *dev,
struct iommu_domain *domain,
void *cpu, dma_addr_t dma)
{
struct sg_table table;
struct scatterlist *sg;
size_t offset = 0;
unsigned int i;
int ret;
if (dma > U32_MAX || PAGE_SIZE - 1 > U32_MAX - dma)
return -ERANGE;
ret = dma_get_sgtable(dev, &table, cpu, dma, PAGE_SIZE);
if (ret)
return ret;
for_each_sg(table.sgl, sg, table.orig_nents, i) {
size_t j;
for (j = 0; j < sg->length; j += PAGE_SIZE) {
size_t len = min_t(size_t, PAGE_SIZE, sg->length - j);
phys_addr_t physical = sg_phys(sg) + j;
if (len > PAGE_SIZE - offset ||
iommu_iova_to_phys(domain, dma + offset) != physical ||
iommu_iova_to_phys(domain, dma + offset + len - 1) !=
physical + len - 1) {
ret = -EFAULT;
goto out;
}
offset += len;
}
}
ret = offset == PAGE_SIZE ? 0 : -EFAULT;
out:
sg_free_table(&table);
return ret;
}
#endif