The reserved (SPM) VMID handling was designed on single-XCC hardware,
where one GFXHUB is all there is. On multi-XCC parts with partitioning
every XCC has an own GFXHUB and every partition is exposed as an own
render node, and the current code is incoherent in three ways at once:

1. AMDGPU_VM_OP_RESERVE_VMID records the reservation on
   AMDGPU_GFXHUB(0) only. The VMID grab path checks
   vm->reserved_vmid[ring->vm_hub], and the rings of XCC i use
   AMDGPU_GFXHUB(i) - so for any job running on XCC 1..n the
   reservation silently does nothing. A debugger relying on a stable
   VMID gets one on exactly one XCC of the partition it runs on, or
   none at all.

2. The physical programming goes the other way: the setup_vm_pt_regs
   gfxhub callback broadcasts the SPM VMID's page table base to every
   XCC of the device, including XCCs of partitions the caller cannot
   even submit to.

3. The exclusion is device-global: because every reservation contends
   for hub 0's id_mgr, one partition's render node blocks every other
   partition from reserving, which breaks the isolation the render
   nodes are supposed to provide.

None of this is visible on single-XCC hardware, where "hub 0", "all
hubs" and "the caller's hubs" happen to be the same thing. gfx 9.4.3
partition modes are where the three diverge.

This series makes the per-hub infrastructure, which already exists in
the data structures (vm->reserved_vmid[] and id_mgr[] are per-vmhub
arrays), real end to end:

Patch 1 extends the setup_vm_pt_regs gfxhub callback with the mask of
logical XCCs to program and adds amdgpu_gfx_logical_xcc_mask() for
deriving that mask (the GFXHUBs are indexed by logical XCC, while
adev->gfx.xcc_mask holds physical instances and can be sparse under
harvesting); no functional change.

Patch 2 makes the hub argument of the binding path mean what it says:
amdgpu_gmc_bind_vmid() programs and flushes only the given hub, and
amdgpu_vmid_alloc_spm()/free_spm() take the set of GFXHUBs directly,
reserving on every hub the VM's jobs can run on, under the root PD
reservation as an asserted lock contract. This fixes 1. and 2. while
keeping the device-wide scope.

Patch 3 then derives the hub set from the caller's partition
(fpriv->xcp_id), fixing 3.: each partition reserves, binds and
conflicts only on its own XCCs. Unreserve keeps operating on every hub,
freeing whatever the VM actually holds.

A note on the record granularity: the reservation could instead be
recorded on the partition's first GFXHUB only and treated as a pure
ownership token, closest to the original single-hub design. But the
record plays three roles at once: the ownership token, the set of hubs
whose page table base has to be bound (each XCC translates through its
own hub, so the binding must cover every XCC of the partition either
way), and the per-hub check of the VMID grab and CS paths
(vm->reserved_vmid[ring->vm_hub]). Recording on every hub of the
partition keeps the three consistent by construction, lets
amdgpu_vm_spm_update() derive the binding set from the existing
structures without a new field, and gives jobs on every XCC of the
partition the same reserved-VMID semantics instead of only on the
first one. The VMID number itself is the same everywhere either way:
it is always adev->vm_manager.spm_vmid.

No UAPI change. On devices without partitioning, or on the whole-device
node, the mask degenerates to every XCC and nothing changes; on
single-XCC parts the series is a no-op.

Compile-tested on amd-staging-drm-next with CONFIG_DRM_AMDGPU=m (the
whole amdgpu directory builds warning-free); checkpatch clean.

v3: address Christian's review of the v1 structure and the Sashiko AI
review of v2:
- Per Christian: no parallel interfaces. Patch 1 extends the existing
  setup_vm_pt_regs callback with an xcc_mask argument (all nine
  implementations and all callers updated; gfxhub v1.2/v12.1 hook up
  their internal XCC-masked variants directly and lose the broadcast
  wrappers) instead of adding a setup_vm_pt_regs_xcc callback. Patch 2
  changes amdgpu_vmid_alloc_spm()/amdgpu_vmid_free_spm() themselves to
  take the GFXHUB set instead of adding *_gfxhubs() wrappers.
- Per the Sashiko review of v2 (verified): AMDGPU_VM_OP_RESERVE_VMID
  takes the root PD reservation before reserving and holds it across
  amdgpu_vm_spm_update(), matching UNRESERVE; both functions assert the
  root PD reservation. This serializes the per-VM reservation state
  against concurrent RESERVE/UNRESERVE, keeps a new CS of the same VM
  from grabbing a freshly reserved VMID between the rollback's wait and
  its unbind, and makes a failed root PD reservation an ioctl error.
  amdgpu_vm_fini() consequently frees the reservation while the root PD
  is still held.
- Further hardening from self-review: the logical-mask idiom lives in
  one helper (amdgpu_gfx_logical_xcc_mask(), guarding xcc_mask == 0)
  instead of eight open-coded copies; UNRESERVE checks the root PD
  reservation result like RESERVE does; amdgpu_vm_spm_update() skips
  hubs without a reservation with a lock-free check (reserved_vmid[]
  only changes under the root PD reservation the caller holds), so the
  common non-SPM VM takes no id_mgr lock on the userqueue validate
  path.

v2: address the Sashiko AI review findings of v1, all of which checked
out:
- Iterate the GFXHUBs with a logical XCC mask instead of the physical
  adev->gfx.xcc_mask, which can be sparse when an XCC is harvested and
  would send out-of-range logical indices into GET_INST(). The
  partition path already used the logical xcp inst_mask.
- amdgpu_vm_fini() now checks every VMHUB, not just GFXHUB(0), before
  waiting for jobs that may still use a reserved VMID.
- The reservation rollback releases only the hubs the call newly
  reserved, after waiting for jobs which might have grabbed the VMID.
- The return of amdgpu_xcp_get_inst_details() is checked; a stale
  fpriv->xcp_id now fails the ioctl instead of silently reserving the
  whole device.

James Zhu (3):
  drm/amdgpu: pass an XCC mask to the setup_vm_pt_regs gfxhub callback
  drm/amdgpu: reserve and bind the SPM VMID on every GFXHUB instance
  drm/amdgpu: scope AMDGPU_VM_OP_RESERVE_VMID to the caller's partition

 .../drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10.c    |   3 +-
 .../drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10_3.c  |   3 +-
 .../drm/amd/amdgpu/amdgpu_amdkfd_gfx_v11.c    |   3 +-
 .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v9.c |   3 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c       |  18 ++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h       |   1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfxhub.h    |   2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c       |   6 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c       | 102 ++++++++++++++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_ids.h       |   4 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c        | 101 ++++++++++++-----
 drivers/gpu/drm/amd/amdgpu/gfxhub_v11_5_0.c   |   4 +-
 drivers/gpu/drm/amd/amdgpu/gfxhub_v12_0.c     |   5 +-
 drivers/gpu/drm/amd/amdgpu/gfxhub_v12_1.c     |  13 +--
 drivers/gpu/drm/amd/amdgpu/gfxhub_v1_0.c      |   5 +-
 drivers/gpu/drm/amd/amdgpu/gfxhub_v1_2.c      |  12 +--
 drivers/gpu/drm/amd/amdgpu/gfxhub_v2_0.c      |   4 +-
 drivers/gpu/drm/amd/amdgpu/gfxhub_v2_1.c      |   4 +-
 drivers/gpu/drm/amd/amdgpu/gfxhub_v3_0.c      |   4 +-
 drivers/gpu/drm/amd/amdgpu/gfxhub_v3_0_3.c    |   4 +-
 20 files changed, 211 insertions(+), 90 deletions(-)

-- 
2.34.1

Reply via email to