Rust for Linux List
 help / color / mirror / Atom feed
From: Boris Brezillon <boris.brezillon@collabora.com>
To: Ketil Johnsen <ketil.johnsen@arm.com>
Cc: Steven Price <steven.price@arm.com>,
	Liviu Dudau <liviu.dudau@arm.com>,
	Akash Goel <akash.goel@arm.com>,
	Karunika Choo <karunika.choo@arm.com>,
	Lukas Zapolskas <lukas.zapolskas@arm.com>,
	Chris Diamand <chris.diamand@arm.com>,
	Maarten Lankhorst <maarten.lankhorst@linux.intel.com>,
	Maxime Ripard <mripard@kernel.org>,
	Thomas Zimmermann <tzimmermann@suse.de>,
	David Airlie <airlied@gmail.com>, Simona Vetter <simona@ffwll.ch>,
	Daniel Almeida <daniel.almeida@collabora.com>,
	Alice Ryhl <aliceryhl@google.com>,
	dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org,
	rust-for-linux@vger.kernel.org
Subject: Re: [PATCH v5 11/13] drm/panthor: Support FW init of protm
Date: Tue, 1 Dec 2026 12:21:57 +0100	[thread overview]
Message-ID: <20261201122157.49f72e97@fedora1.home> (raw)
In-Reply-To: <20261116112601.3483649-12-ketil.johnsen@arm.com>

On Mon, 16 Nov 2026 12:25:59 +0100
Ketil Johnsen <ketil.johnsen@arm.com> wrote:

> From: Boris Brezillon <boris.brezillon@collabora.com>
> 
> The Mali GPU FW needs some protected memory when executing in protected
> mode. This FW memory section is assigned a VA during device init.
> A user space process with the needed privileges (CAP_SYS_MODULE) must
> provide a suitable memory buffer before the Mali GPU is capable of
> executing in protected mode.
> 
> Adds user query of FW protm state, min FW section size and min protm
> suspend buffer size.
> Adds user initiated init of FW protm section.
> 
> Signed-off-by: Boris Brezillon <boris.brezillon@collabora.com>
> Co-developed-by: Ketil Johnsen <ketil.johnsen@arm.com>
> Signed-off-by: Ketil Johnsen <ketil.johnsen@arm.com>
> 
> ---
> 
> v5:
> - Ditched the lockless approach, now using a simpler mutex.
> - Handle potential races reported by Sashiko.
> - Do not write to protected FW section memory (during GPU reset).
> - No longer mix guard() and goto error handling in panthor_fw_protm_init()
> 
> v4:
> - First version of this change as a separate commit
> ---
>  drivers/gpu/drm/panthor/panthor_drv.c |  21 +++-
>  drivers/gpu/drm/panthor/panthor_fw.c  | 146 ++++++++++++++++++++++++--
>  drivers/gpu/drm/panthor/panthor_fw.h  |   5 +
>  include/uapi/drm/panthor_drm.h        |  75 +++++++++++++
>  4 files changed, 238 insertions(+), 9 deletions(-)
> 
> diff --git a/drivers/gpu/drm/panthor/panthor_drv.c b/drivers/gpu/drm/panthor/panthor_drv.c
> index 7edb8d329a705..ffea1eef134f5 100644
> --- a/drivers/gpu/drm/panthor/panthor_drv.c
> +++ b/drivers/gpu/drm/panthor/panthor_drv.c
> @@ -177,11 +177,13 @@ panthor_get_uobj_array(const struct drm_panthor_obj_array *in, u32 min_stride,
>  		 PANTHOR_UOBJ_DECL(struct drm_panthor_mmu_info, page_size_bitmap), \
>  		 PANTHOR_UOBJ_DECL(struct drm_panthor_timestamp_info, current_timestamp), \
>  		 PANTHOR_UOBJ_DECL(struct drm_panthor_group_priorities_info, pad), \
> +		 PANTHOR_UOBJ_DECL(struct drm_panthor_protm_info, pad), \
>  		 PANTHOR_UOBJ_DECL(struct drm_panthor_sync_op, timeline_value), \
>  		 PANTHOR_UOBJ_DECL(struct drm_panthor_queue_submit, syncs), \
>  		 PANTHOR_UOBJ_DECL(struct drm_panthor_queue_create, ringbuf_size), \
>  		 PANTHOR_UOBJ_DECL(struct drm_panthor_vm_bind_op, syncs), \
> -		 PANTHOR_UOBJ_DECL(struct drm_panthor_bo_sync_op, size))
> +		 PANTHOR_UOBJ_DECL(struct drm_panthor_bo_sync_op, size), \
> +		 PANTHOR_UOBJ_DECL(struct drm_panthor_protm_init, pad))
>  
>  /**
>   * PANTHOR_UOBJ_SET() - Copy a kernel object to a user object.
> @@ -934,6 +936,8 @@ static int panthor_ioctl_dev_query(struct drm_device *ddev, void *data, struct d
>  	struct drm_panthor_dev_query *args = data;
>  	struct drm_panthor_timestamp_info timestamp_info;
>  	struct drm_panthor_group_priorities_info priorities_info;
> +	struct drm_panthor_protm_info protm_info;
> +
>  	int ret;
>  
>  	if (!args->pointer) {
> @@ -958,6 +962,10 @@ static int panthor_ioctl_dev_query(struct drm_device *ddev, void *data, struct d
>  			args->size = sizeof(ptdev->mmu_info);
>  			return 0;
>  
> +		case DRM_PANTHOR_DEV_QUERY_PROTM_INFO:
> +			args->size = sizeof(protm_info);
> +			return 0;
> +
>  		default:
>  			return -EINVAL;
>  		}
> @@ -991,6 +999,10 @@ static int panthor_ioctl_dev_query(struct drm_device *ddev, void *data, struct d
>  	case DRM_PANTHOR_DEV_QUERY_MMU_INFO:
>  		return PANTHOR_UOBJ_SET(args->pointer, args->size, ptdev->mmu_info);
>  
> +	case DRM_PANTHOR_DEV_QUERY_PROTM_INFO:
> +		panthor_fw_query_protm_info(file, &protm_info);
> +		return PANTHOR_UOBJ_SET(args->pointer, args->size, protm_info);
> +
>  	default:
>  		return -EINVAL;
>  	}
> @@ -1584,6 +1596,12 @@ static int panthor_ioctl_bo_query_info(struct drm_device *ddev, void *data,
>  	return 0;
>  }
>  
> +static int panthor_ioctl_protm_init(struct drm_device *ddev, void *data,
> +				    struct drm_file *file)
> +{
> +	return panthor_fw_protm_init(file, data);
> +}
> +
>  static int
>  panthor_open(struct drm_device *ddev, struct drm_file *file)
>  {
> @@ -1660,6 +1678,7 @@ static const struct drm_ioctl_desc panthor_drm_driver_ioctls[] = {
>  	PANTHOR_IOCTL(SET_USER_MMIO_OFFSET, set_user_mmio_offset, DRM_RENDER_ALLOW),
>  	PANTHOR_IOCTL(BO_SYNC, bo_sync, DRM_RENDER_ALLOW),
>  	PANTHOR_IOCTL(BO_QUERY_INFO, bo_query_info, DRM_RENDER_ALLOW),
> +	PANTHOR_IOCTL(PROTM_INIT, protm_init, DRM_RENDER_ALLOW),
>  };
>  
>  static int panthor_mmap(struct file *filp, struct vm_area_struct *vma)
> diff --git a/drivers/gpu/drm/panthor/panthor_fw.c b/drivers/gpu/drm/panthor/panthor_fw.c
> index b565b362f401f..af86b1844ead8 100644
> --- a/drivers/gpu/drm/panthor/panthor_fw.c
> +++ b/drivers/gpu/drm/panthor/panthor_fw.c
> @@ -13,8 +13,10 @@
>  #include <linux/mutex.h>
>  #include <linux/platform_device.h>
>  #include <linux/pm_runtime.h>
> +#include <linux/capability.h>
>  
>  #include <drm/drm_drv.h>
> +#include <drm/drm_file.h>
>  #include <drm/drm_managed.h>
>  #include <drm/drm_print.h>
>  
> @@ -267,6 +269,18 @@ struct panthor_fw {
>  		struct delayed_work ping_work;
>  	} watchdog;
>  

	/** protm: Protected mode related fields. */

otherwise sphinxdoc complains.

> +	struct  {
> +		/** @protm.init_lock: Used to serialize protm initialization. */
> +		struct mutex init_lock;
> +
> +		/** @protm.inited: set to true when protm.section is initialized */
> +		bool inited;

		bool initialized;

But also, do we really need this field, can't it be inferred from
protm::section::mem != NULL?

> +
> +		/** @protm.section: The protected mode section. */
> +		struct panthor_fw_section *section;
> +

nit: Drop the extra blank line.

> +	} protm;
> +
>  	/**
>  	 * @req_waitqueue: FW request waitqueue.
>  	 *
> @@ -460,7 +474,7 @@ static int panthor_fw_binary_sub_iter_init(struct panthor_device *ptdev,
>  static void panthor_fw_init_section_mem(struct panthor_device *ptdev,
>  					struct panthor_fw_section *section)
>  {
> -	bool was_mapped = !!section->mem->kmap;
> +	bool was_mapped;
>  	struct sg_table *sgt;
>  	int ret;
>  
> @@ -468,6 +482,14 @@ static void panthor_fw_init_section_mem(struct panthor_device *ptdev,
>  	    !(section->flags & CSF_FW_BINARY_IFACE_ENTRY_ZERO))
>  		return;
>  
> +	if (section->flags & CSF_FW_BINARY_IFACE_ENTRY_PROT)
> +		return;
> +
> +	if (!section->mem)
> +		return;
> +
> +	was_mapped = !!section->mem->kmap;
> +
>  	ret = panthor_kernel_bo_vmap(section->mem);
>  	if (drm_WARN_ON(&ptdev->base, ret))
>  		return;
> @@ -637,12 +659,6 @@ static int panthor_fw_load_section_entry(struct panthor_device *ptdev,
>  		return -EINVAL;
>  	}
>  
> -	if (hdr.flags & CSF_FW_BINARY_IFACE_ENTRY_PROT) {
> -		drm_warn(&ptdev->base,
> -			 "Firmware protected mode entry is not supported, ignoring");
> -		return 0;
> -	}
> -
>  	if (hdr.va.start == CSF_MCU_SHARED_REGION_START &&
>  	    !(hdr.flags & CSF_FW_BINARY_IFACE_ENTRY_SHARED)) {
>  		drm_err(&ptdev->base,
> @@ -694,7 +710,22 @@ static int panthor_fw_load_section_entry(struct panthor_device *ptdev,
>  	section->size = section_size;
>  	section->va = hdr.va.start;
>  
> -	if (section_size) {
> +	if (hdr.flags & CSF_FW_BINARY_IFACE_ENTRY_PROT) {
> +		guard(mutex)(&ptdev->fw->protm.init_lock);
> +
> +		if (ptdev->fw->protm.section) {
> +			drm_err(&ptdev->base,
> +				"Only one protected section supported\n");
> +			return -EINVAL;
> +		}
> +
> +		ptdev->fw->protm.section = section;
> +	}
> +
> +	/* Defer the section->mem creation if this is a protected entry.
> +	 * This will be populated when DRM_IOCTL_PANTHOR_PROTM_INIT is called.
> +	 */
> +	if (section->size && !(hdr.flags & CSF_FW_BINARY_IFACE_ENTRY_PROT)) {
>  		u32 vm_map_flags = section_vm_map_flags(section);
>  
>  		section->mem = panthor_kernel_bo_create(
> @@ -1610,6 +1641,101 @@ int panthor_fw_protm_exit(struct panthor_device *ptdev, u32 timeout_ms)
>  	return panthor_fw_protm_exit_wait(ptdev, timeout_ms);
>  }
>  
> +int panthor_fw_protm_init(struct drm_file *file,
> +			  struct drm_panthor_protm_init *args)
> +{
> +	struct panthor_file *pfile = file->driver_priv;
> +	struct panthor_device *ptdev = pfile->ptdev;
> +	ssize_t vm_pgsz = panthor_vm_page_size(ptdev->fw->vm);
> +	struct panthor_fw_section *protm_section;
> +	struct panthor_kernel_bo *mem;
> +	struct drm_gem_object *obj;
> +	u32 vm_map_flags;
> +	int cookie, ret = 0;
> +
> +	if (!capable(CAP_SYS_MODULE))
> +		return -EPERM;
> +
> +	if (args->pad)
> +		return -EINVAL;
> +
> +	mutex_lock(&ptdev->fw->protm.init_lock);

	guard(mutex)(&ptdev->fw->protm.init_lock);

and you can kill the out_unlock label.

> +
> +	protm_section = ptdev->fw->protm.section;
> +
> +	if (!protm_section || !protm_section->size) {
> +		ret = -EINVAL;
> +		goto out_unlock;
> +	}
> +
> +	if (ptdev->fw->protm.inited)
> +		goto out_unlock;
> +
> +	if (!drm_dev_enter(&ptdev->base, &cookie)) {
> +		ret = -ENODEV;
> +		goto out_unlock;
> +	}
> +
> +	obj = drm_gem_object_lookup(file,
> +				    args->fw_protected_sections_bo_handle);
> +	if (!obj) {
> +		ret = -ENOENT;
> +		goto out_dev_exit;
> +	}
> +
> +	if (obj->size < ALIGN(ptdev->fw->protm.section->size, vm_pgsz)) {
> +		ret = -EINVAL;
> +		goto out_gem_put;
> +	}
> +
> +	vm_map_flags = section_vm_map_flags(protm_section);
> +
> +	mem = panthor_kernel_bo_create_from_gem(
> +		ptdev, panthor_fw_vm(ptdev), to_panthor_bo(obj), vm_map_flags,
> +		protm_section->va, protm_section->size);
> +	if (IS_ERR(mem)) {
> +		ret = PTR_ERR(mem);
> +		goto out_gem_put;
> +	}
> +
> +	protm_section->mem = mem;
> +
> +	ptdev->fw->protm.inited = true;
> +
> +out_gem_put:
> +	drm_gem_object_put(obj);
> +
> +out_dev_exit:
> +	drm_dev_exit(cookie);
> +
> +out_unlock:
> +	mutex_unlock(&ptdev->fw->protm.init_lock);
> +
> +	return ret;
> +}
> +
> +bool panthor_fw_protm_is_inited(struct panthor_device *ptdev)
> +{
> +	guard(mutex)(&ptdev->fw->protm.init_lock);
> +	return ptdev->fw->protm.inited;
> +}
> +
> +void panthor_fw_query_protm_info(struct drm_file *file,
> +				 struct drm_panthor_protm_info *arg)
> +{
> +	struct panthor_file *pfile = file->driver_priv;
> +	struct panthor_device *ptdev = pfile->ptdev;
> +	ssize_t vm_pgsz = panthor_vm_page_size(ptdev->fw->vm);
> +
> +	guard(mutex)(&ptdev->fw->protm.init_lock);

	if (!ptdev->fw->protm.section) {
		*args = (struct drm_panthor_protm_info){};
		return;
	}

or you might end up with a NULL deref if the FW doesn't expose a protm section.
I know it's not supposed to happen, but the FW parsing logic let it through,
so we either have to deal with it here, or fail the FW load early on when
the protm section is missing.

> +
> +	memset(arg, 0, sizeof(*arg));
> +	arg->state = ptdev->fw->protm.inited ? DRM_PANTHOR_PROTM_INITIALIZED : 0;
> +	arg->group_protected_suspend_buf_size = ptdev->fw->protm_suspend_size;
> +	arg->fw_protected_sections_size =  ALIGN(ptdev->fw->protm.section->size,
> +						 vm_pgsz);

	*args = (struct drm_panthor_protm_info) {
		.state = ptdev->fw->protm.section->mem ? DRM_PANTHOR_PROTM_INITIALIZED : 0,
		.group_protected_suspend_buf_size = ptdev->fw->protm_suspend_size,
		.fw_protected_sections_size =  ALIGN(ptdev->fw->protm.section->size, vm_pgsz),
	};

> +}
> +


  parent reply	other threads:[~2026-12-01 11:22 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16 11:25 [PATCH v5 00/13] drm/panthor: Protected mode support for Mali CSF GPUs Ketil Johnsen
2026-11-16 11:25 ` [PATCH v5 01/13] drm/panthor: De-duplicate FW memory section sync Ketil Johnsen
2026-11-16 11:25 ` [PATCH v5 02/13] drm/panthor: Minor scheduler refactoring Ketil Johnsen
2026-11-27 16:36   ` Steven Price
2026-11-16 11:25 ` [PATCH v5 03/13] drm/panthor: Pass drm_file instead of panthor_file Ketil Johnsen
2026-11-27 16:53   ` Steven Price
2026-11-16 11:25 ` [PATCH v5 04/13] drm/panthor: Don't allocate protm_suspend_buf Ketil Johnsen
2026-11-16 11:25 ` [PATCH v5 05/13] drm/panthor: Add support for entering and exiting protected mode Ketil Johnsen
2026-11-30 17:17   ` Boris Brezillon
2026-12-01 11:05   ` Boris Brezillon
2026-11-16 11:25 ` [PATCH v5 06/13] drm/panthor: New function for FW section map flags Ketil Johnsen
2026-11-16 11:25 ` [PATCH v5 07/13] drm/panthor: Keep size and VA in FW section obj Ketil Johnsen
2026-11-16 11:25 ` [PATCH v5 08/13] drm/panthor: Refactor GEM usage flags assignment Ketil Johnsen
2026-11-16 11:25 ` [PATCH v5 09/13] drm/panthor: Create kernel BO from existing GEM Ketil Johnsen
2026-11-16 11:25 ` [PATCH v5 10/13] drm/panthor: Use a copy of suspend buffer sizes Ketil Johnsen
2026-12-01 11:07   ` Boris Brezillon
2026-11-16 11:25 ` [PATCH v5 11/13] drm/panthor: Support FW init of protm Ketil Johnsen
2026-11-16 18:00   ` kernel test robot
2026-12-01 11:21   ` Boris Brezillon [this message]
2026-11-16 11:26 ` [PATCH v5 12/13] drm/panthor: Protm support for groups Ketil Johnsen
2026-11-16 11:26 ` [PATCH v5 13/13] drm/panthor: Bump version to expose protm feature Ketil Johnsen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201122157.49f72e97@fedora1.home \
    --to=boris.brezillon@collabora.com \
    --cc=airlied@gmail.com \
    --cc=akash.goel@arm.com \
    --cc=aliceryhl@google.com \
    --cc=chris.diamand@arm.com \
    --cc=daniel.almeida@collabora.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=karunika.choo@arm.com \
    --cc=ketil.johnsen@arm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=liviu.dudau@arm.com \
    --cc=lukas.zapolskas@arm.com \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=mripard@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=simona@ffwll.ch \
    --cc=steven.price@arm.com \
    --cc=tzimmermann@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox