1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2022 Intel Corporation 4 */ 5 6 #include "xe_uc.h" 7 8 #include "xe_assert.h" 9 #include "xe_device.h" 10 #include "xe_gsc.h" 11 #include "xe_gt.h" 12 #include "xe_gt_printk.h" 13 #include "xe_gt_sriov_vf.h" 14 #include "xe_guc.h" 15 #include "xe_guc_pc.h" 16 #include "xe_guc_engine_activity.h" 17 #include "xe_huc.h" 18 #include "xe_sriov.h" 19 #include "xe_wopcm.h" 20 21 static struct xe_gt * 22 uc_to_gt(struct xe_uc *uc) 23 { 24 return container_of(uc, struct xe_gt, uc); 25 } 26 27 static struct xe_device * 28 uc_to_xe(struct xe_uc *uc) 29 { 30 return gt_to_xe(uc_to_gt(uc)); 31 } 32 33 /* Should be called once at driver load only */ 34 int xe_uc_init_noalloc(struct xe_uc *uc) 35 { 36 int ret; 37 38 ret = xe_guc_init_noalloc(&uc->guc); 39 if (ret) 40 goto err; 41 42 /* HuC and GSC have no early dependencies and will be initialized during xe_uc_init(). */ 43 return 0; 44 45 err: 46 xe_gt_err(uc_to_gt(uc), "Failed to early initialize uC (%pe)\n", ERR_PTR(ret)); 47 return ret; 48 } 49 50 int xe_uc_init(struct xe_uc *uc) 51 { 52 int ret; 53 54 /* 55 * We call the GuC/HuC/GSC init functions even if GuC submission is off 56 * to correctly move our tracking of the FW state to "disabled". 57 */ 58 ret = xe_guc_init(&uc->guc); 59 if (ret) 60 goto err; 61 62 ret = xe_huc_init(&uc->huc); 63 if (ret) 64 goto err; 65 66 ret = xe_gsc_init(&uc->gsc); 67 if (ret) 68 goto err; 69 70 if (!xe_device_uc_enabled(uc_to_xe(uc))) 71 return 0; 72 73 if (!IS_SRIOV_VF(uc_to_xe(uc))) { 74 ret = xe_wopcm_init(&uc->wopcm); 75 if (ret) 76 goto err; 77 } 78 79 ret = xe_guc_min_load_for_hwconfig(&uc->guc); 80 if (ret) 81 goto err; 82 83 return 0; 84 err: 85 xe_gt_err(uc_to_gt(uc), "Failed to initialize uC (%pe)\n", ERR_PTR(ret)); 86 return ret; 87 } 88 89 /** 90 * xe_uc_init_post_hwconfig - init Uc post hwconfig load 91 * @uc: The UC object 92 * 93 * Return: 0 on success, negative error code on error. 94 */ 95 int xe_uc_init_post_hwconfig(struct xe_uc *uc) 96 { 97 int err; 98 99 /* GuC submission not enabled, nothing to do */ 100 if (!xe_device_uc_enabled(uc_to_xe(uc))) 101 return 0; 102 103 err = xe_uc_sanitize_reset(uc); 104 if (err) 105 return err; 106 107 err = xe_guc_init_post_hwconfig(&uc->guc); 108 if (err) 109 return err; 110 111 err = xe_huc_init_post_hwconfig(&uc->huc); 112 if (err) 113 return err; 114 115 return xe_gsc_init_post_hwconfig(&uc->gsc); 116 } 117 118 static int uc_reset(struct xe_uc *uc) 119 { 120 struct xe_device *xe = uc_to_xe(uc); 121 int ret; 122 123 ret = xe_guc_reset(&uc->guc); 124 if (ret) { 125 drm_err(&xe->drm, "Failed to reset GuC, ret = %d\n", ret); 126 return ret; 127 } 128 129 return 0; 130 } 131 132 static void xe_uc_sanitize(struct xe_uc *uc) 133 { 134 xe_huc_sanitize(&uc->huc); 135 xe_guc_sanitize(&uc->guc); 136 } 137 138 int xe_uc_sanitize_reset(struct xe_uc *uc) 139 { 140 xe_uc_sanitize(uc); 141 142 return uc_reset(uc); 143 } 144 145 static int vf_uc_load_hw(struct xe_uc *uc) 146 { 147 int err; 148 149 err = xe_uc_sanitize_reset(uc); 150 if (err) 151 return err; 152 153 err = xe_guc_enable_communication(&uc->guc); 154 if (err) 155 return err; 156 157 err = xe_gt_sriov_vf_connect(uc_to_gt(uc)); 158 if (err) 159 goto err_out; 160 161 uc->guc.submission_state.enabled = true; 162 163 err = xe_guc_opt_in_features_enable(&uc->guc); 164 if (err) 165 goto err_out; 166 167 err = xe_gt_record_default_lrcs(uc_to_gt(uc)); 168 if (err) 169 goto err_out; 170 171 return 0; 172 173 err_out: 174 xe_guc_sanitize(&uc->guc); 175 return err; 176 } 177 178 /* 179 * Should be called during driver load, after every GT reset, and after every 180 * suspend to reload / auth the firmwares. 181 */ 182 int xe_uc_load_hw(struct xe_uc *uc) 183 { 184 int ret; 185 186 /* GuC submission not enabled, nothing to do */ 187 if (!xe_device_uc_enabled(uc_to_xe(uc))) 188 return 0; 189 190 if (IS_SRIOV_VF(uc_to_xe(uc))) 191 return vf_uc_load_hw(uc); 192 193 ret = xe_huc_upload(&uc->huc); 194 if (ret) 195 return ret; 196 197 ret = xe_guc_upload(&uc->guc); 198 if (ret) 199 return ret; 200 201 ret = xe_guc_enable_communication(&uc->guc); 202 if (ret) 203 return ret; 204 205 ret = xe_gt_record_default_lrcs(uc_to_gt(uc)); 206 if (ret) 207 goto err_out; 208 209 ret = xe_guc_post_load_init(&uc->guc); 210 if (ret) 211 goto err_out; 212 213 ret = xe_guc_pc_start(&uc->guc.pc); 214 if (ret) 215 goto err_out; 216 217 xe_guc_engine_activity_enable_stats(&uc->guc); 218 219 /* We don't fail the driver load if HuC fails to auth */ 220 ret = xe_huc_auth(&uc->huc, XE_HUC_AUTH_VIA_GUC); 221 if (ret) 222 xe_gt_err(uc_to_gt(uc), 223 "HuC authentication failed (%pe), continuing with no HuC\n", 224 ERR_PTR(ret)); 225 226 /* GSC load is async */ 227 xe_gsc_load_start(&uc->gsc); 228 229 return 0; 230 231 err_out: 232 xe_guc_sanitize(&uc->guc); 233 return ret; 234 } 235 236 int xe_uc_reset_prepare(struct xe_uc *uc) 237 { 238 /* GuC submission not enabled, nothing to do */ 239 if (!xe_device_uc_enabled(uc_to_xe(uc))) 240 return 0; 241 242 return xe_guc_reset_prepare(&uc->guc); 243 } 244 245 void xe_uc_gucrc_disable(struct xe_uc *uc) 246 { 247 XE_WARN_ON(xe_guc_pc_gucrc_disable(&uc->guc.pc)); 248 } 249 250 void xe_uc_stop_prepare(struct xe_uc *uc) 251 { 252 xe_gsc_stop_prepare(&uc->gsc); 253 xe_guc_stop_prepare(&uc->guc); 254 } 255 256 void xe_uc_stop(struct xe_uc *uc) 257 { 258 /* GuC submission not enabled, nothing to do */ 259 if (!xe_device_uc_enabled(uc_to_xe(uc))) 260 return; 261 262 xe_guc_stop(&uc->guc); 263 } 264 265 int xe_uc_start(struct xe_uc *uc) 266 { 267 /* GuC submission not enabled, nothing to do */ 268 if (!xe_device_uc_enabled(uc_to_xe(uc))) 269 return 0; 270 271 return xe_guc_start(&uc->guc); 272 } 273 274 static void uc_reset_wait(struct xe_uc *uc) 275 { 276 int ret; 277 278 again: 279 xe_guc_reset_wait(&uc->guc); 280 281 ret = xe_uc_reset_prepare(uc); 282 if (ret) 283 goto again; 284 } 285 286 void xe_uc_suspend_prepare(struct xe_uc *uc) 287 { 288 xe_gsc_wait_for_worker_completion(&uc->gsc); 289 xe_guc_stop_prepare(&uc->guc); 290 } 291 292 int xe_uc_suspend(struct xe_uc *uc) 293 { 294 /* GuC submission not enabled, nothing to do */ 295 if (!xe_device_uc_enabled(uc_to_xe(uc))) 296 return 0; 297 298 uc_reset_wait(uc); 299 300 xe_uc_stop(uc); 301 302 return xe_guc_suspend(&uc->guc); 303 } 304 305 /** 306 * xe_uc_runtime_suspend() - UC runtime suspend 307 * @uc: the UC object 308 * 309 * Runtime suspend all UCs. 310 */ 311 void xe_uc_runtime_suspend(struct xe_uc *uc) 312 { 313 if (!xe_device_uc_enabled(uc_to_xe(uc))) 314 return; 315 316 xe_guc_runtime_suspend(&uc->guc); 317 } 318 319 /** 320 * xe_uc_runtime_resume() - UC runtime resume 321 * @uc: the UC object 322 * 323 * Runtime resume all UCs. 324 */ 325 void xe_uc_runtime_resume(struct xe_uc *uc) 326 { 327 if (!xe_device_uc_enabled(uc_to_xe(uc))) 328 return; 329 330 xe_guc_runtime_resume(&uc->guc); 331 } 332 333 /** 334 * xe_uc_declare_wedged() - Declare UC wedged 335 * @uc: the UC object 336 * 337 * Wedge the UC which stops all submission, saves desired debug state, and 338 * cleans up anything which could timeout. 339 */ 340 void xe_uc_declare_wedged(struct xe_uc *uc) 341 { 342 xe_gt_assert(uc_to_gt(uc), uc_to_xe(uc)->wedged.mode); 343 344 xe_guc_declare_wedged(&uc->guc); 345 } 346