[RFC PATCH v2 3/3] drm/rockchip: Map the reserved regions 1:1 in the DRM IOMMU domain

Robin Murphy robin.murphy at arm.com
Fri Dec 4 04:00:40 PST 2026


On 2026-12-04 10:27 am, Francesco Saverio Pavone wrote:
> The driver attaches the VOPs to its own IOMMU domain. A framebuffer
> left scanning out by the bootloader has no mapping there, and the VOP
> faults on it until the first new frame is latched.
> 
> Collect the reserved regions of each VOP at bind. Before any VOP is
> attached, merge overlapping regions, keep the result out of the drm_mm,
> rounded out to whole IOMMU pages, so that no GEM object gets those
> addresses, and map the direct ones 1:1 in the DRM domain, with the same
> rounding as the iommu core.
> 
> A direct region outside the aperture, or overlapping one with a
> different prot, is not mapped, with a warning. The mappings stay until
> the domain is freed, because the driver does not know when the old
> buffer is read for the last time.

Surely a bootsplash screen should be described by a simple-framebuffer 
device, and that the standard DRM mechanisms used to take over from that?

If you reach the point of creating private abstractions of entire IOMMU 
API concepts inside a DRM driver, you should probably stop and think 
"Why aren't other drivers doing this?"

Thanks,
Robin.

> Signed-off-by: Francesco Saverio Pavone <pavone.lawyer at gmail.com>
> ---
>   drivers/gpu/drm/rockchip/rockchip_drm_drv.c | 227 ++++++++++++++++++++
>   drivers/gpu/drm/rockchip/rockchip_drm_drv.h |   6 +
>   2 files changed, 233 insertions(+)
> 
> diff --git a/drivers/gpu/drm/rockchip/rockchip_drm_drv.c b/drivers/gpu/drm/rockchip/rockchip_drm_drv.c
> index ba417f7e9e474..95eebd2d3e060 100644
> --- a/drivers/gpu/drm/rockchip/rockchip_drm_drv.c
> +++ b/drivers/gpu/drm/rockchip/rockchip_drm_drv.c
> @@ -13,6 +13,7 @@
>   #include <linux/module.h>
>   #include <linux/of_graph.h>
>   #include <linux/of_platform.h>
> +#include <linux/overflow.h>
>   #include <linux/component.h>
>   #include <linux/console.h>
>   #include <linux/iommu.h>
> @@ -88,6 +89,224 @@ void rockchip_drm_dma_detach_device(struct drm_device *drm_dev,
>   	iommu_detach_device(private->domain, dev);
>   }
>   
> +struct rockchip_drm_resv {
> +	struct list_head list;
> +	u64 start;
> +	u64 length;
> +	int prot;
> +	bool direct;
> +};
> +
> +/* A merged range, kept out of the drm_mm or mapped 1:1 */
> +struct rockchip_drm_range {
> +	struct list_head list;
> +	struct drm_mm_node node;
> +	u64 start;
> +	u64 end;
> +	int prot;
> +	bool mapped;
> +};
> +
> +static void rockchip_drm_collect_resv_regions(struct drm_device *drm_dev,
> +					      struct device *dev)
> +{
> +	struct rockchip_drm_private *private = drm_dev->dev_private;
> +	struct iommu_resv_region *region;
> +	struct rockchip_drm_resv *resv;
> +	LIST_HEAD(regions);
> +
> +	iommu_get_resv_regions(dev, &regions);
> +
> +	list_for_each_entry(region, &regions, list) {
> +		if (region->type != IOMMU_RESV_DIRECT &&
> +		    region->type != IOMMU_RESV_DIRECT_RELAXABLE &&
> +		    region->type != IOMMU_RESV_RESERVED)
> +			continue;
> +
> +		resv = devm_kzalloc_obj(drm_dev->dev, *resv);
> +		if (!resv) {
> +			drm_warn(drm_dev, "cannot record the reserved regions of %s\n",
> +				 dev_name(dev));
> +			break;
> +		}
> +
> +		resv->start = region->start;
> +		resv->length = region->length;
> +		resv->prot = region->prot;
> +		resv->direct = region->type != IOMMU_RESV_RESERVED;
> +		list_add_tail(&resv->list, &private->resv_list);
> +	}
> +
> +	iommu_put_resv_regions(dev, &regions);
> +}
> +
> +/*
> + * The range kept out of the drm_mm is rounded out, so that no partly
> + * reserved page is handed out. The range mapped for a direct region is
> + * rounded like iommu_create_device_direct_mappings() does, so that it gets
> + * the same mapping as in the default domain; it may be empty.
> + */
> +static int rockchip_drm_resv_range(const struct rockchip_drm_resv *resv,
> +				   u64 pgsz, u64 *start, u64 *end,
> +				   u64 *map_start)
> +{
> +	u64 end_excl;
> +
> +	if (check_add_overflow(resv->start, resv->length, &end_excl))
> +		return -EOVERFLOW;
> +
> +	*start = ALIGN_DOWN(resv->start, pgsz);
> +	*end = ALIGN(end_excl, pgsz);
> +	if (*end < end_excl)
> +		return -EOVERFLOW;
> +	if (*start >= *end)
> +		return -EINVAL;
> +
> +	*map_start = ALIGN(resv->start, pgsz);
> +
> +	return 0;
> +}
> +
> +/*
> + * Add [start, end) to a sorted list of disjoint ranges, merging it with the
> + * ranges of the same prot that it overlaps or touches. An overlap with a
> + * range of another prot is refused.
> + */
> +static int rockchip_drm_add_range(struct list_head *head, u64 start, u64 end,
> +				  int prot)
> +{
> +	struct rockchip_drm_range *range, *tmp, *new;
> +	struct list_head *pos = head;
> +
> +	list_for_each_entry(range, head, list)
> +		if (range->prot != prot && range->start < end &&
> +		    start < range->end)
> +			return -EBUSY;
> +
> +	new = kzalloc_obj(*new);
> +	if (!new)
> +		return -ENOMEM;
> +
> +	list_for_each_entry_safe(range, tmp, head, list) {
> +		if (range->end < start ||
> +		    (range->end == start && range->prot != prot)) {
> +			pos = &range->list;
> +			continue;
> +		}
> +		if (range->start > end ||
> +		    (range->start == end && range->prot != prot))
> +			break;
> +
> +		start = min(start, range->start);
> +		end = max(end, range->end);
> +		list_del(&range->list);
> +		kfree(range);
> +	}
> +
> +	new->start = start;
> +	new->end = end;
> +	new->prot = prot;
> +	list_add(&new->list, pos);
> +
> +	return 0;
> +}
> +
> +static void rockchip_drm_unmap_resv_regions(struct rockchip_drm_private *private)
> +{
> +	struct rockchip_drm_range *range, *tmp;
> +
> +	list_for_each_entry_safe(range, tmp, &private->resv_maps, list) {
> +		if (range->mapped)
> +			iommu_unmap(private->domain, range->start,
> +				    range->end - range->start);
> +		list_del(&range->list);
> +		kfree(range);
> +	}
> +
> +	list_for_each_entry_safe(range, tmp, &private->resv_holes, list) {
> +		if (drm_mm_node_allocated(&range->node))
> +			drm_mm_remove_node(&range->node);
> +		list_del(&range->list);
> +		kfree(range);
> +	}
> +}
> +
> +/*
> + * Keep the reserved regions of the VOPs out of the drm_mm, and map the
> + * direct ones 1:1, for as long as the domain exists.
> + */
> +static void rockchip_drm_map_resv_regions(struct drm_device *drm_dev)
> +{
> +	struct rockchip_drm_private *private = drm_dev->dev_private;
> +	struct iommu_domain_geometry *geometry = &private->domain->geometry;
> +	u64 pgsz = 1ULL << __ffs(private->domain->pgsize_bitmap);
> +	struct rockchip_drm_range *range;
> +	struct rockchip_drm_resv *resv;
> +	u64 start, end, map_start;
> +	int ret;
> +
> +	list_for_each_entry(resv, &private->resv_list, list) {
> +		ret = rockchip_drm_resv_range(resv, pgsz, &start, &end,
> +					      &map_start);
> +		if (!ret)
> +			ret = rockchip_drm_add_range(&private->resv_holes,
> +						     start, end, 0);
> +		if (ret) {
> +			drm_warn(drm_dev, "ignoring reserved region %#llx+%#llx: %d\n",
> +				 resv->start, resv->length, ret);
> +			continue;
> +		}
> +
> +		if (!resv->direct || map_start >= end)
> +			continue;
> +
> +		ret = rockchip_drm_add_range(&private->resv_maps, map_start,
> +					     end, resv->prot);
> +		if (ret)
> +			drm_warn(drm_dev, "reserved region %#llx+%#llx not mapped: %d\n",
> +				 resv->start, resv->length, ret);
> +	}
> +
> +	list_for_each_entry(range, &private->resv_holes, list) {
> +		start = max(range->start, geometry->aperture_start);
> +		end = min(range->end - 1, geometry->aperture_end);
> +		if (start > end)
> +			continue;
> +
> +		range->node.start = start;
> +		range->node.size = end - start + 1;
> +		ret = drm_mm_reserve_node(&private->mm, &range->node);
> +		if (ret)
> +			drm_warn(drm_dev, "cannot reserve [%#llx-%#llx]: %d\n",
> +				 start, end, ret);
> +	}
> +
> +	list_for_each_entry(range, &private->resv_maps, list) {
> +		start = range->start;
> +		end = range->end;
> +		if (start < geometry->aperture_start ||
> +		    end - 1 > geometry->aperture_end) {
> +			drm_warn(drm_dev,
> +				 "reserved region [%#llx-%#llx] is outside the IOMMU aperture\n",
> +				 start, end - 1);
> +			continue;
> +		}
> +		if (end - start > SIZE_MAX) {
> +			drm_warn(drm_dev, "reserved region [%#llx-%#llx] is too large to map\n",
> +				 start, end - 1);
> +			continue;
> +		}
> +
> +		ret = iommu_map(private->domain, start, start, end - start,
> +				range->prot, GFP_KERNEL);
> +		if (ret)
> +			drm_warn(drm_dev, "cannot map [%#llx-%#llx]: %d\n",
> +				 start, end - 1, ret);
> +		else
> +			range->mapped = true;
> +	}
> +}
> +
>   void rockchip_drm_dma_init_device(struct drm_device *drm_dev,
>   				  struct device *dev)
>   {
> @@ -100,6 +319,9 @@ void rockchip_drm_dma_init_device(struct drm_device *drm_dev,
>   
>   	if (!IS_ERR(private->iommu_dev))
>   		drm_dev_set_dma_dev(drm_dev, private->iommu_dev);
> +
> +	if (device_iommu_mapped(dev))
> +		rockchip_drm_collect_resv_regions(drm_dev, dev);
>   }
>   
>   static int rockchip_drm_init_iommu(struct drm_device *drm_dev)
> @@ -127,6 +349,7 @@ static int rockchip_drm_init_iommu(struct drm_device *drm_dev)
>   		  start, end);
>   	drm_mm_init(&private->mm, start, end - start + 1);
>   	mutex_init(&private->mm_lock);
> +	rockchip_drm_map_resv_regions(drm_dev);
>   
>   	return 0;
>   }
> @@ -138,6 +361,7 @@ static void rockchip_iommu_cleanup(struct drm_device *drm_dev)
>   	if (!private->domain)
>   		return;
>   
> +	rockchip_drm_unmap_resv_regions(private);
>   	drm_mm_takedown(&private->mm);
>   	iommu_domain_free(private->domain);
>   }
> @@ -170,6 +394,9 @@ static int rockchip_drm_bind(struct device *dev)
>   	}
>   
>   	drm_dev->dev_private = private;
> +	INIT_LIST_HEAD(&private->resv_list);
> +	INIT_LIST_HEAD(&private->resv_holes);
> +	INIT_LIST_HEAD(&private->resv_maps);
>   
>   	ret = drmm_mode_config_init(drm_dev);
>   	if (ret)
> diff --git a/drivers/gpu/drm/rockchip/rockchip_drm_drv.h b/drivers/gpu/drm/rockchip/rockchip_drm_drv.h
> index 4705dc6b8bd7d..42e0c0de93006 100644
> --- a/drivers/gpu/drm/rockchip/rockchip_drm_drv.h
> +++ b/drivers/gpu/drm/rockchip/rockchip_drm_drv.h
> @@ -67,12 +67,18 @@ struct rockchip_crtc_state {
>    * @crtc: array of enabled CRTCs, used to map from "pipe" to drm_crtc.
>    * @num_pipe: number of pipes for this device.
>    * @mm_lock: protect drm_mm on multi-threads.
> + * @resv_list: reserved regions of the VOPs, as reported by the IOMMU.
> + * @resv_holes: merged reserved ranges, kept out of @mm.
> + * @resv_maps: merged direct ranges, mapped 1:1 in @domain.
>    */
>   struct rockchip_drm_private {
>   	struct iommu_domain *domain;
>   	struct device *iommu_dev;
>   	struct mutex mm_lock;
>   	struct drm_mm mm;
> +	struct list_head resv_list;
> +	struct list_head resv_holes;
> +	struct list_head resv_maps;
>   };
>   
>   struct rockchip_encoder {




More information about the linux-arm-kernel mailing list