xref: /linux/drivers/gpu/drm/xe/xe_uc.c (revision f9f0b4a1f35d39a1a2a2f8ec46eb7b81efc70a63)
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_rc.h"
17 #include "xe_guc_engine_activity.h"
18 #include "xe_huc.h"
19 #include "xe_sriov.h"
20 #include "xe_wopcm.h"
21 
22 static struct xe_gt *
23 uc_to_gt(struct xe_uc *uc)
24 {
25 	return container_of(uc, struct xe_gt, uc);
26 }
27 
28 static struct xe_device *
29 uc_to_xe(struct xe_uc *uc)
30 {
31 	return gt_to_xe(uc_to_gt(uc));
32 }
33 
34 /* Should be called once at driver load only */
35 int xe_uc_init_noalloc(struct xe_uc *uc)
36 {
37 	int ret;
38 
39 	ret = xe_guc_init_noalloc(&uc->guc);
40 	if (ret)
41 		goto err;
42 
43 	/* HuC and GSC have no early dependencies and will be initialized during xe_uc_init(). */
44 	return 0;
45 
46 err:
47 	xe_gt_err(uc_to_gt(uc), "Failed to early initialize uC (%pe)\n", ERR_PTR(ret));
48 	return ret;
49 }
50 
51 int xe_uc_init(struct xe_uc *uc)
52 {
53 	int ret;
54 
55 	/*
56 	 * We call the GuC/HuC/GSC init functions even if GuC submission is off
57 	 * to correctly move our tracking of the FW state to "disabled".
58 	 */
59 	ret = xe_guc_init(&uc->guc);
60 	if (ret)
61 		goto err;
62 
63 	ret = xe_huc_init(&uc->huc);
64 	if (ret)
65 		goto err;
66 
67 	ret = xe_gsc_init(&uc->gsc);
68 	if (ret)
69 		goto err;
70 
71 	if (!xe_device_uc_enabled(uc_to_xe(uc)))
72 		return 0;
73 
74 	if (!IS_SRIOV_VF(uc_to_xe(uc))) {
75 		ret = xe_wopcm_init(&uc->wopcm);
76 		if (ret)
77 			goto err;
78 	}
79 
80 	ret = xe_guc_min_load_for_hwconfig(&uc->guc);
81 	if (ret)
82 		goto err;
83 
84 	return 0;
85 err:
86 	xe_gt_err(uc_to_gt(uc), "Failed to initialize uC (%pe)\n", ERR_PTR(ret));
87 	return ret;
88 }
89 
90 /**
91  * xe_uc_init_post_hwconfig - init Uc post hwconfig load
92  * @uc: The UC object
93  *
94  * Return: 0 on success, negative error code on error.
95  */
96 int xe_uc_init_post_hwconfig(struct xe_uc *uc)
97 {
98 	int err;
99 
100 	/* GuC submission not enabled, nothing to do */
101 	if (!xe_device_uc_enabled(uc_to_xe(uc)))
102 		return 0;
103 
104 	err = xe_uc_sanitize_reset(uc);
105 	if (err)
106 		return err;
107 
108 	err = xe_guc_init_post_hwconfig(&uc->guc);
109 	if (err)
110 		return err;
111 
112 	err = xe_huc_init_post_hwconfig(&uc->huc);
113 	if (err)
114 		return err;
115 
116 	return xe_gsc_init_post_hwconfig(&uc->gsc);
117 }
118 
119 static int uc_reset(struct xe_uc *uc)
120 {
121 	struct xe_device *xe = uc_to_xe(uc);
122 	int ret;
123 
124 	ret = xe_guc_reset(&uc->guc);
125 	if (ret) {
126 		drm_err(&xe->drm, "Failed to reset GuC, ret = %d\n", ret);
127 		return ret;
128 	}
129 
130 	return 0;
131 }
132 
133 static void xe_uc_sanitize(struct xe_uc *uc)
134 {
135 	xe_huc_sanitize(&uc->huc);
136 	xe_guc_sanitize(&uc->guc);
137 }
138 
139 int xe_uc_sanitize_reset(struct xe_uc *uc)
140 {
141 	xe_uc_sanitize(uc);
142 
143 	return uc_reset(uc);
144 }
145 
146 static int vf_uc_load_hw(struct xe_uc *uc)
147 {
148 	int err;
149 
150 	err = xe_uc_sanitize_reset(uc);
151 	if (err)
152 		return err;
153 
154 	err = xe_guc_enable_communication(&uc->guc);
155 	if (err)
156 		return err;
157 
158 	err = xe_gt_sriov_vf_connect(uc_to_gt(uc));
159 	if (err)
160 		goto err_out;
161 
162 	uc->guc.submission_state.enabled = true;
163 
164 	err = xe_guc_opt_in_features_enable(&uc->guc);
165 	if (err)
166 		goto err_out;
167 
168 	err = xe_gt_record_default_lrcs(uc_to_gt(uc));
169 	if (err)
170 		goto err_out;
171 
172 	return 0;
173 
174 err_out:
175 	xe_guc_sanitize(&uc->guc);
176 	return err;
177 }
178 
179 /*
180  * Should be called during driver load, after every GT reset, and after every
181  * suspend to reload / auth the firmwares.
182  */
183 int xe_uc_load_hw(struct xe_uc *uc)
184 {
185 	int ret;
186 
187 	/* GuC submission not enabled, nothing to do */
188 	if (!xe_device_uc_enabled(uc_to_xe(uc)))
189 		return 0;
190 
191 	if (IS_SRIOV_VF(uc_to_xe(uc)))
192 		return vf_uc_load_hw(uc);
193 
194 	ret = xe_huc_upload(&uc->huc);
195 	if (ret)
196 		return ret;
197 
198 	ret = xe_guc_upload(&uc->guc);
199 	if (ret)
200 		return ret;
201 
202 	ret = xe_guc_enable_communication(&uc->guc);
203 	if (ret)
204 		return ret;
205 
206 	ret = xe_gt_record_default_lrcs(uc_to_gt(uc));
207 	if (ret)
208 		goto err_out;
209 
210 	ret = xe_guc_post_load_init(&uc->guc);
211 	if (ret)
212 		goto err_out;
213 
214 	ret = xe_guc_pc_start(&uc->guc.pc);
215 	if (ret)
216 		goto err_out;
217 
218 	ret = xe_guc_rc_enable(&uc->guc);
219 	if (ret)
220 		goto err_out;
221 
222 	xe_guc_engine_activity_enable_stats(&uc->guc);
223 
224 	/* We don't fail the driver load if HuC fails to auth */
225 	ret = xe_huc_auth(&uc->huc, XE_HUC_AUTH_VIA_GUC);
226 	if (ret)
227 		xe_gt_err(uc_to_gt(uc),
228 			  "HuC authentication failed (%pe), continuing with no HuC\n",
229 			  ERR_PTR(ret));
230 
231 	/* GSC load is async */
232 	xe_gsc_load_start(&uc->gsc);
233 
234 	return 0;
235 
236 err_out:
237 	xe_guc_sanitize(&uc->guc);
238 	return ret;
239 }
240 
241 int xe_uc_reset_prepare(struct xe_uc *uc)
242 {
243 	/* GuC submission not enabled, nothing to do */
244 	if (!xe_device_uc_enabled(uc_to_xe(uc)))
245 		return 0;
246 
247 	return xe_guc_reset_prepare(&uc->guc);
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