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