@@ -275,13 +275,12 @@ static int amdgpu_vmid_grab_reserved(struct amdgpu_vm *vm,
275275{
276276 struct amdgpu_device * adev = ring -> adev ;
277277 unsigned vmhub = ring -> vm_hub ;
278- struct amdgpu_vmid_mgr * id_mgr = & adev -> vm_manager .id_mgr [vmhub ];
279278 uint64_t fence_context = adev -> fence_context + ring -> idx ;
280279 bool needs_flush = vm -> use_cpu_for_update ;
281280 uint64_t updates = amdgpu_vm_tlb_seq (vm );
282281 int r ;
283282
284- * id = id_mgr -> reserved ;
283+ * id = vm -> reserved_vmid [ vmhub ] ;
285284 if ((* id )-> owner != vm -> immediate .fence_context ||
286285 !amdgpu_vmid_compatible (* id , job ) ||
287286 (* id )-> flushed_updates < updates ||
@@ -474,40 +473,61 @@ bool amdgpu_vmid_uses_reserved(struct amdgpu_vm *vm, unsigned int vmhub)
474473 return vm -> reserved_vmid [vmhub ];
475474}
476475
477- int amdgpu_vmid_alloc_reserved (struct amdgpu_device * adev ,
476+ /*
477+ * amdgpu_vmid_alloc_reserved - reserve a specific VMID for this vm
478+ * @adev: amdgpu device structure
479+ * @vm: the VM to reserve an ID for
480+ * @vmhub: the VMHUB which should be used
481+ *
482+ * Mostly used to have a reserved VMID for debugging and SPM.
483+ *
484+ * Returns: 0 for success, -ENOENT if an ID is already reserved.
485+ */
486+ int amdgpu_vmid_alloc_reserved (struct amdgpu_device * adev , struct amdgpu_vm * vm ,
478487 unsigned vmhub )
479488{
480489 struct amdgpu_vmid_mgr * id_mgr = & adev -> vm_manager .id_mgr [vmhub ];
490+ struct amdgpu_vmid * id ;
491+ int r = 0 ;
481492
482493 mutex_lock (& id_mgr -> lock );
483-
484- ++ id_mgr -> reserved_use_count ;
485- if (!id_mgr -> reserved ) {
486- struct amdgpu_vmid * id ;
487-
488- id = list_first_entry (& id_mgr -> ids_lru , struct amdgpu_vmid ,
489- list );
490- /* Remove from normal round robin handling */
491- list_del_init (& id -> list );
492- id_mgr -> reserved = id ;
494+ if (vm -> reserved_vmid [vmhub ])
495+ goto unlock ;
496+ if (id_mgr -> reserved_vmid ) {
497+ r = - ENOENT ;
498+ goto unlock ;
493499 }
494-
500+ /* Remove from normal round robin handling */
501+ id = list_first_entry (& id_mgr -> ids_lru , struct amdgpu_vmid , list );
502+ list_del_init (& id -> list );
503+ vm -> reserved_vmid [vmhub ] = id ;
504+ id_mgr -> reserved_vmid = true;
495505 mutex_unlock (& id_mgr -> lock );
506+
496507 return 0 ;
508+ unlock :
509+ mutex_unlock (& id_mgr -> lock );
510+ return r ;
497511}
498512
499- void amdgpu_vmid_free_reserved (struct amdgpu_device * adev ,
513+ /*
514+ * amdgpu_vmid_free_reserved - free up a reserved VMID again
515+ * @adev: amdgpu device structure
516+ * @vm: the VM with the reserved ID
517+ * @vmhub: the VMHUB which should be used
518+ */
519+ void amdgpu_vmid_free_reserved (struct amdgpu_device * adev , struct amdgpu_vm * vm ,
500520 unsigned vmhub )
501521{
502522 struct amdgpu_vmid_mgr * id_mgr = & adev -> vm_manager .id_mgr [vmhub ];
503523
504524 mutex_lock (& id_mgr -> lock );
505- if (!-- id_mgr -> reserved_use_count ) {
506- /* give the reserved ID back to normal round robin */
507- list_add (& id_mgr -> reserved -> list , & id_mgr -> ids_lru );
508- id_mgr -> reserved = NULL ;
525+ if (vm -> reserved_vmid [vmhub ]) {
526+ list_add (& vm -> reserved_vmid [vmhub ]-> list ,
527+ & id_mgr -> ids_lru );
528+ vm -> reserved_vmid [vmhub ] = NULL ;
529+ id_mgr -> reserved_vmid = false;
509530 }
510-
511531 mutex_unlock (& id_mgr -> lock );
512532}
513533
@@ -574,7 +594,6 @@ void amdgpu_vmid_mgr_init(struct amdgpu_device *adev)
574594
575595 mutex_init (& id_mgr -> lock );
576596 INIT_LIST_HEAD (& id_mgr -> ids_lru );
577- id_mgr -> reserved_use_count = 0 ;
578597
579598 /* for GC <10, SDMA uses MMHUB so use first_kfd_vmid for both GC and MM */
580599 if (amdgpu_ip_version (adev , GC_HWIP , 0 ) < IP_VERSION (10 , 0 , 0 ))
0 commit comments