From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796385648; b=JWxIVL0zeuNcjyMXO3pUM5/Dvyux7iz5GjRRrr4KJC79MGOVSw0gKyl/T9ODwZOk0aG5 YdThiei56rzxpUvMXLYFpCKPqXnNscHXA2r5pazlC3VQcq7nMOKNlgTGGYL5V6iloeIeE QzScJc3LMFyK0xp4jFQtVLx0t1IPKreZsPmyQqWmOD6GQS3w3nQN+drppWHkH9ry/FMoI XAE3xgvF0H/3WaqoQy6oNLFEbwwVyDP6WCYDj/G42sD81t2x4R+pR7NBarM2f9TJp8YGO cZfgaEzhkefWJDuwLvBpVDctAC8FS94mpaarmR1bLUOuNBjscnW+I6W9H2Xz6wXQLjg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796385648; h=DMARC-Filter:Received:Received:DKIM-Signature:Message-ID:Date: MIME-Version:User-Agent:Subject:To:Cc:References:From: Content-Language:In-Reply-To:Content-Type:Content-Transfer-Encoding; bh=jf0EzFkSAFOwG2fCEYIJ/Lr/cXlYN7VGVhefv7dcwBE=; b=j0Litjrs/M0XWU6aDC3paugbP8HA/CfoG101Cv5bTagfozTIAAoGyuGWNxwZdB4V/z6p TxumM2TOBYDPiJFSmPNxVwqa/F9etExgCwiSrEucYjpGoA6CEROUhT0qGWugjPa0uOhLg BzfPJAxncQIUhQJyBmzbxf2dCfncyB8Okz7Rat2wA7KlrLMTeVdu+hWbzkYfhlVkNpW1p QHAfgC7d0JpChXmGlyqhzOqjKaY4+t8IrdhwhctPf9tPWmAfhCkavgy96b5jW1pl414SP zT/hZLHTzrxCw7dW46aRCxyfoj7PPr/yGXqkW7ezKLltY4wXVYtx32nvgN73P5ha3zw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=YowePsds; arc=none smtp.remote-ip=217.140.110.172 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A70FD606A32D for ; Fri, 4 Dec 2026 12:00:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A70FD606A32D Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=YowePsds Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 47AB314BF; Fri, 4 Dec 2026 04:00:44 -0800 (PST) Received: from [192.168.1.102] (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 224053F632; Fri, 4 Dec 2026 04:00:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1796385647; bh=+xWzmwsSpCz6RlMSw+cshEkFCXfb9mZDm3Sguk41iu4=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=YowePsdsX9PCf1aj2XUidwvecK2wNLZiB4L+Wq9mxSSRO4Vw0Kwf9WVoIZA9mt+xU fa+vESkVFrX4tMBmepA5JUWrWPIEfBJC7Yy7Hy9B7N4n4a4sJY5EbwA0jDpPAVAILF CqeX/e2fXplucXAIQcLE1uIoOIxN2qwnlm/O94fI= Message-ID: Date: Fri, 4 Dec 2026 12:00:40 +0000 X-Mailing-List: devicetree@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [RFC PATCH v2 3/3] drm/rockchip: Map the reserved regions 1:1 in the DRM IOMMU domain To: Francesco Saverio Pavone , Joerg Roedel , Will Deacon , Sandy Huang , Heiko Stuebner , Andy Yan , Chaoyi Chen Cc: Damon Ding , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Jiaxing Hu , iommu@lists.linux.dev, dri-devel@lists.freedesktop.org, devicetree@vger.kernel.org, linux-rockchip@lists.infradead.org, linux-arm-kernel@lists.infradead.org References: <20261204102757.47697-1-pavone.lawyer@gmail.com> <20261204102757.47697-4-pavone.lawyer@gmail.com> From: Robin Murphy Content-Language: en-GB In-Reply-To: <20261204102757.47697-4-pavone.lawyer@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit 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 > --- > 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 > #include > #include > +#include > #include > #include > #include > @@ -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, ®ions); > + > + list_for_each_entry(region, ®ions, 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, ®ions); > +} > + > +/* > + * 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 {