For multiple iommu_domains, we need to reserve some iova regions. Take a
example, If the default iova region is 0 ~ 4G, but the 0x4000_0000 ~
0x43ff_ffff is only for the special CCU0 domain. Thus we should exclude
this region for the default iova region.

Signed-off-by: Anan sun <anan....@mediatek.com>
Signed-off-by: Chao Hao <chao....@mediatek.com>
Signed-off-by: Yong Wu <yong...@mediatek.com>
---
 drivers/iommu/mtk_iommu.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/drivers/iommu/mtk_iommu.c b/drivers/iommu/mtk_iommu.c
index b42fd2535b77..764dc0b93477 100644
--- a/drivers/iommu/mtk_iommu.c
+++ b/drivers/iommu/mtk_iommu.c
@@ -626,6 +626,35 @@ static int mtk_iommu_of_xlate(struct device *dev, struct 
of_phandle_args *args)
        return iommu_fwspec_add_ids(dev, args->args, 1);
 }
 
+static void mtk_iommu_get_resv_regions(struct device *dev,
+                                      struct list_head *head)
+{
+       struct mtk_iommu_data *data = dev_iommu_priv_get(dev);
+       unsigned int domid = mtk_iommu_get_domain_id(dev, data->plat_data), i;
+       const struct mtk_iommu_iova_region *resv, *curdom;
+       struct iommu_resv_region *region;
+       int prot = IOMMU_WRITE | IOMMU_READ;
+
+       if (domid < 0)
+               return;
+       curdom = data->plat_data->iova_region + domid;
+       for (i = 0; i < data->plat_data->iova_region_nr; i++) {
+               resv = data->plat_data->iova_region + i;
+
+               /* Only reserve when the region is inside the current domain */
+               if (resv->iova_base <= curdom->iova_base ||
+                   resv->iova_base + resv->size >= curdom->iova_base + 
curdom->size)
+                       continue;
+
+               region = iommu_alloc_resv_region(resv->iova_base, resv->size,
+                                                prot, IOMMU_RESV_RESERVED);
+               if (!region)
+                       return;
+
+               list_add_tail(&region->list, head);
+       }
+}
+
 static const struct iommu_ops mtk_iommu_ops = {
        .domain_alloc   = mtk_iommu_domain_alloc,
        .domain_free    = mtk_iommu_domain_free,
@@ -641,6 +670,8 @@ static const struct iommu_ops mtk_iommu_ops = {
        .release_device = mtk_iommu_release_device,
        .device_group   = mtk_iommu_device_group,
        .of_xlate       = mtk_iommu_of_xlate,
+       .get_resv_regions = mtk_iommu_get_resv_regions,
+       .put_resv_regions = generic_iommu_put_resv_regions,
        .pgsize_bitmap  = SZ_4K | SZ_64K | SZ_1M | SZ_16M,
 };
 
-- 
2.18.0

Reply via email to