On Wed, Nov 18, 2026 at 3:11 AM Srinivasan Shanmugam
<[email protected]> wrote:
>
> Add SET_L2_TRAP and CLEAR_L2_TRAP operations to the VM ioctl.
>
> The second-level trap handler is set per GPUVM. All shader waves using
> the same GPUVM use the same second-level trap handler, regardless of the
> queue type.
>
> SET_L2_TRAP sets the userspace TBA and TMA addresses for the GPUVM. The
> kernel checks that both address ranges are mapped in the GPUVM before
> using them.
>
> CLEAR_L2_TRAP clears the second-level TBA and TMA addresses from the
> first-level trap descriptor.
>
> Userspace must keep the TBA and TMA mappings valid after SET_L2_TRAP.
> The mappings can be removed after CLEAR_L2_TRAP completes successfully.
>
> Suggested-by: Christian König <[email protected]>
> Suggested-by: Alex Deucher <[email protected]>
> Cc: Felix Kuehling <[email protected]>
> Cc: Lancelot Six <[email protected]>
> Cc: James Zhu <[email protected]>
> Cc: Lijo Lazar <[email protected]>
> Cc: Samuel Pitoiset <[email protected]>
> Cc: Natalie Vock <[email protected]>
> Cc: Timur Kristóf <[email protected]>
> Cc: Pierre-Eric Pelloux-Prayer <[email protected]>
> Signed-off-by: Srinivasan Shanmugam <[email protected]>
> Reviewed-by: Christian König <[email protected]>
> ---
>  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 36 ++++++++++++++-
>  include/uapi/drm/amdgpu_drm.h          | 63 ++++++++++++++++++++++++++

This also adds UAPI.  We need proposed userspace before we can land this.

Alex

>  2 files changed, 98 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c 
> b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> index eafd4367d3b1..df3a8dda606a 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> @@ -3064,19 +3064,53 @@ int amdgpu_vm_ioctl(struct drm_device *dev, void 
> *data, struct drm_file *filp)
>                 }
>                 return r;
>         }
> +
>         case AMDGPU_VM_OP_UNRESERVE_VMID: {
>                 struct amdgpu_bo *root = vm->root.bo;
>
>                 amdgpu_bo_reserve(root, true);
> +
>                 /* Wait for the jobs which might still use the reserved VMID 
> */
>                 if (vm->reserved_vmid[AMDGPU_GFXHUB(0)])
>                         dma_resv_wait_timeout(root->tbo.base.resv,
> -                                             DMA_RESV_USAGE_READ, false,
> +                                             DMA_RESV_USAGE_READ,
> +                                             false,
>                                               MAX_SCHEDULE_TIMEOUT);
> +
>                 amdgpu_vmid_free_spm(adev, vm, AMDGPU_GFXHUB(0));
>                 amdgpu_bo_unreserve(root);
> +
>                 break;
>         }
> +
> +       case AMDGPU_VM_OP_SET_L2_TRAP:
> +               /*
> +                * Install or replace the per-VM second-level trap handler.
> +                * Applies to all shader waves using this GPUVM regardless of
> +                * queue type.
> +                */
> +               if (!vm->trap_obj)
> +                       return -EOPNOTSUPP;
> +
> +               return amdgpu_trap_vm_set_l2_trap(adev, vm,
> +                                                 vm->trap_obj,
> +                                                 args->in.l2trap.tba_va,
> +                                                 args->in.l2trap.tba_sz,
> +                                                 args->in.l2trap.tma_va,
> +                                                 args->in.l2trap.tma_sz);
> +
> +       case AMDGPU_VM_OP_CLEAR_L2_TRAP:
> +               if (memchr_inv(&args->in.l2trap, 0,
> +                              sizeof(args->in.l2trap)))
> +                       return -EINVAL;
> +
> +               /* Disable the per-VM second-level trap handler. */
> +               if (!vm->trap_obj)
> +                       return -EOPNOTSUPP;
> +
> +               return amdgpu_trap_vm_clear_l2_trap(adev, vm,
> +                                                   vm->trap_obj);
> +
>         default:
>                 return -EINVAL;
>         }
> diff --git a/include/uapi/drm/amdgpu_drm.h b/include/uapi/drm/amdgpu_drm.h
> index 9317018330ec..b576d62bcd87 100644
> --- a/include/uapi/drm/amdgpu_drm.h
> +++ b/include/uapi/drm/amdgpu_drm.h
> @@ -614,10 +614,73 @@ struct drm_amdgpu_userq_wait {
>  #define AMDGPU_VM_OP_RESERVE_VMID      1
>  #define AMDGPU_VM_OP_UNRESERVE_VMID    2
>
> +/**
> + * AMDGPU_VM_OP_SET_L2_TRAP - install or replace the second-level trap
> + * handler for this GPUVM.
> + *
> + * The second-level trap handler is a per-GPUVM setting. All shader waves
> + * using this GPUVM use the same handler regardless of queue type.
> + *
> + * TBA is the GPU virtual address of the second-level trap handler code.
> + * TMA is the GPU virtual address of the second-level trap handler scratch
> + * memory. Each range must be non-empty and contained within a single mapping
> + * in this GPUVM.
> + *
> + * This operation may block until outstanding GPUVM work completes.
> + *
> + * Userspace must keep the TBA and TMA mappings valid while the second-level
> + * trap handler is installed.
> + *
> + * Returns:
> + *  0             on success;
> + *  -EOPNOTSUPP   if the first-level CWSR handler is unavailable;
> + *  -EINVAL       if TBA/TMA is invalid or not mapped in this GPUVM;
> + *  negative errno if waiting for GPUVM work or reserving the GPUVM fails.
> + */
> +#define AMDGPU_VM_OP_SET_L2_TRAP       3
> +
> +/**
> + * AMDGPU_VM_OP_CLEAR_L2_TRAP - disable the second-level trap handler.
> + *
> + * This operation may block until outstanding GPUVM work completes.
> + *
> + * The l2trap input members must be zero for this operation.
> + *
> + * After this operation succeeds, userspace may remove the second-level
> + * TBA and TMA mappings.
> + *
> + * Returns:
> + *  0             on success;
> + *  -EOPNOTSUPP   if the first-level CWSR handler is unavailable;
> + *  -EINVAL       if the GPUVM trap state is invalid;
> + *  negative errno if waiting for GPUVM work or reserving the GPUVM fails.
> + */
> +#define AMDGPU_VM_OP_CLEAR_L2_TRAP     4
> +
>  struct drm_amdgpu_vm_in {
>         /** AMDGPU_VM_OP_* */
>         __u32   op;
>         __u32   flags;
> +       union {
> +               struct {
> +                       /** GPU VA of second-level trap handler code buffer */
> +                       __u64 tba_va;
> +
> +                       /** GPU VA of second-level trap handler scratch 
> buffer */
> +                       __u64 tma_va;
> +
> +                       /** TBA buffer size in bytes */
> +                       __u32 tba_sz;
> +
> +                       /** TMA buffer size in bytes */
> +                       __u32 tma_sz;
> +               } l2trap;
> +               /**
> +                * Reserved; keeps the union at a fixed 32-byte size for
> +                * future operations.
> +                */
> +               __u64 reserved[4];
> +       };
>  };
>
>  struct drm_amdgpu_vm_out {
> --
> 2.34.1
>

Reply via email to