1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2023-2024 Intel Corporation 4 */ 5 6 #include <drm/drm_managed.h> 7 8 #include "xe_assert.h" 9 #include "xe_device.h" 10 #include "xe_gt_sriov_printk.h" 11 #include "xe_gt_sriov_vf.h" 12 #include "xe_pm.h" 13 #include "xe_sriov.h" 14 #include "xe_sriov_printk.h" 15 #include "xe_sriov_vf.h" 16 17 /** 18 * DOC: VF restore procedure in PF KMD and VF KMD 19 * 20 * Restoring previously saved state of a VF is one of core features of 21 * SR-IOV. All major VM Management applications allow saving and restoring 22 * the VM state, and doing that to a VM which uses SRIOV VF as one of 23 * the accessible devices requires support from KMD on both PF and VF side. 24 * VMM initiates all required operations through VFIO module, which then 25 * translates them into PF KMD calls. This description will focus on these 26 * calls, leaving out the module which initiates these steps (VFIO). 27 * 28 * In order to start the restore procedure, GuC needs to keep the VF in 29 * proper state. The PF driver can ensure GuC set it to VF_READY state 30 * by provisioning the VF, which in turn can be done after Function Level 31 * Reset of said VF (or after it was freshly created - in that case FLR 32 * is not needed). The FLR procedure ends with GuC sending message 33 * `GUC_PF_NOTIFY_VF_FLR_DONE`, and then provisioning data is sent to GuC. 34 * After the provisioning is completed, the VF needs to be paused, and 35 * at that point the actual restore can begin. 36 * 37 * During VF Restore, state of several resources is restored. These may 38 * include local memory content (system memory is restored by VMM itself), 39 * values of MMIO registers, stateless compression metadata and others. 40 * The final resource which also needs restoring is state of the VF 41 * submission maintained within GuC. For that, `GUC_PF_OPCODE_VF_RESTORE` 42 * message is used, with reference to the state blob to be consumed by 43 * GuC. 44 * 45 * Next, when VFIO is asked to set the VM into running state, the PF driver 46 * sends `GUC_PF_TRIGGER_VF_RESUME` to GuC. When sent after restore, this 47 * changes VF state within GuC to `VF_RESFIX_BLOCKED` rather than the 48 * usual `VF_RUNNING`. At this point GuC triggers an interrupt to inform 49 * the VF KMD within the VM that it was migrated. 50 * 51 * As soon as Virtual GPU of the VM starts, the VF driver within receives 52 * the MIGRATED interrupt and schedules post-migration recovery worker. 53 * That worker queries GuC for new provisioning (using MMIO communication), 54 * and applies fixups to any non-virtualized resources used by the VF. 55 * 56 * When the VF driver is ready to continue operation on the newly connected 57 * hardware, it sends `VF2GUC_NOTIFY_RESFIX_DONE` which causes it to 58 * enter the long awaited `VF_RUNNING` state, and therefore start handling 59 * CTB messages and scheduling workloads from the VF:: 60 * 61 * PF GuC VF 62 * [ ] | | 63 * [ ] PF2GUC_VF_CONTROL(pause) | | 64 * [ ]---------------------------> [ ] | 65 * [ ] [ ] GuC sets new VF state to | 66 * [ ] [ ]------- VF_READY_PAUSED | 67 * [ ] [ ] | | 68 * [ ] [ ] <----- | 69 * [ ] success [ ] | 70 * [ ] <---------------------------[ ] | 71 * [ ] | | 72 * [ ] PF loads resources from the | | 73 * [ ]------- saved image supplied | | 74 * [ ] | | | 75 * [ ] <----- | | 76 * [ ] | | 77 * [ ] GUC_PF_OPCODE_VF_RESTORE | | 78 * [ ]---------------------------> [ ] | 79 * [ ] [ ] GuC loads contexts and CTB | 80 * [ ] [ ]------- state from image | 81 * [ ] [ ] | | 82 * [ ] [ ] <----- | 83 * [ ] [ ] | 84 * [ ] [ ] GuC sets new VF state to | 85 * [ ] [ ]------- VF_RESFIX_PAUSED | 86 * [ ] [ ] | | 87 * [ ] success [ ] <----- | 88 * [ ] <---------------------------[ ] | 89 * [ ] | | 90 * [ ] GUC_PF_TRIGGER_VF_RESUME | | 91 * [ ]---------------------------> [ ] | 92 * [ ] [ ] GuC sets new VF state to | 93 * [ ] [ ]------- VF_RESFIX_BLOCKED | 94 * [ ] [ ] | | 95 * [ ] [ ] <----- | 96 * [ ] [ ] | 97 * [ ] [ ] GUC_INTR_SW_INT_0 | 98 * [ ] success [ ]---------------------------> [ ] 99 * [ ] <---------------------------[ ] [ ] 100 * | | VF2GUC_QUERY_SINGLE_KLV [ ] 101 * | [ ] <---------------------------[ ] 102 * | [ ] [ ] 103 * | [ ] new VF provisioning [ ] 104 * | [ ]---------------------------> [ ] 105 * | | [ ] 106 * | | VF driver applies post [ ] 107 * | | migration fixups -------[ ] 108 * | | | [ ] 109 * | | -----> [ ] 110 * | | [ ] 111 * | | VF2GUC_NOTIFY_RESFIX_DONE [ ] 112 * | [ ] <---------------------------[ ] 113 * | [ ] [ ] 114 * | [ ] GuC sets new VF state to [ ] 115 * | [ ]------- VF_RUNNING [ ] 116 * | [ ] | [ ] 117 * | [ ] <----- [ ] 118 * | [ ] success [ ] 119 * | [ ]---------------------------> [ ] 120 * | | | 121 * | | | 122 */ 123 124 static void migration_worker_func(struct work_struct *w); 125 126 /** 127 * xe_sriov_vf_init_early - Initialize SR-IOV VF specific data. 128 * @xe: the &xe_device to initialize 129 */ 130 void xe_sriov_vf_init_early(struct xe_device *xe) 131 { 132 INIT_WORK(&xe->sriov.vf.migration.worker, migration_worker_func); 133 } 134 135 /* 136 * Notify all GuCs about resource fixups apply finished. 137 */ 138 static void vf_post_migration_notify_resfix_done(struct xe_device *xe) 139 { 140 struct xe_gt *gt; 141 unsigned int id; 142 143 for_each_gt(gt, xe, id) { 144 xe_gt_sriov_vf_notify_resfix_done(gt); 145 } 146 } 147 148 static void vf_post_migration_recovery(struct xe_device *xe) 149 { 150 drm_dbg(&xe->drm, "migration recovery in progress\n"); 151 xe_pm_runtime_get(xe); 152 /* FIXME: add the recovery steps */ 153 vf_post_migration_notify_resfix_done(xe); 154 xe_pm_runtime_put(xe); 155 drm_notice(&xe->drm, "migration recovery ended\n"); 156 } 157 158 static void migration_worker_func(struct work_struct *w) 159 { 160 struct xe_device *xe = container_of(w, struct xe_device, 161 sriov.vf.migration.worker); 162 163 vf_post_migration_recovery(xe); 164 } 165 166 static bool vf_ready_to_recovery_on_all_gts(struct xe_device *xe) 167 { 168 struct xe_gt *gt; 169 unsigned int id; 170 171 for_each_gt(gt, xe, id) { 172 if (!test_bit(id, &xe->sriov.vf.migration.gt_flags)) { 173 xe_gt_sriov_dbg_verbose(gt, "still not ready to recover\n"); 174 return false; 175 } 176 } 177 return true; 178 } 179 180 /** 181 * xe_sriov_vf_start_migration_recovery - Start VF migration recovery. 182 * @xe: the &xe_device to start recovery on 183 * 184 * This function shall be called only by VF. 185 */ 186 void xe_sriov_vf_start_migration_recovery(struct xe_device *xe) 187 { 188 bool started; 189 190 xe_assert(xe, IS_SRIOV_VF(xe)); 191 192 if (!vf_ready_to_recovery_on_all_gts(xe)) 193 return; 194 195 WRITE_ONCE(xe->sriov.vf.migration.gt_flags, 0); 196 /* Ensure other threads see that no flags are set now. */ 197 smp_mb(); 198 199 started = queue_work(xe->sriov.wq, &xe->sriov.vf.migration.worker); 200 drm_info(&xe->drm, "VF migration recovery %s\n", started ? 201 "scheduled" : "already in progress"); 202 } 203