xref: /linux/drivers/gpu/drm/i915/gt/uc/intel_guc.c (revision 3027ce13e04eee76539ca65c2cb1028a01c8c508)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2014-2019 Intel Corporation
4  */
5 
6 #include "gem/i915_gem_lmem.h"
7 #include "gt/intel_gt.h"
8 #include "gt/intel_gt_irq.h"
9 #include "gt/intel_gt_pm_irq.h"
10 #include "gt/intel_gt_regs.h"
11 #include "intel_guc.h"
12 #include "intel_guc_ads.h"
13 #include "intel_guc_capture.h"
14 #include "intel_guc_print.h"
15 #include "intel_guc_slpc.h"
16 #include "intel_guc_submission.h"
17 #include "i915_drv.h"
18 #include "i915_irq.h"
19 #include "i915_reg.h"
20 
21 /**
22  * DOC: GuC
23  *
24  * The GuC is a microcontroller inside the GT HW, introduced in gen9. The GuC is
25  * designed to offload some of the functionality usually performed by the host
26  * driver; currently the main operations it can take care of are:
27  *
28  * - Authentication of the HuC, which is required to fully enable HuC usage.
29  * - Low latency graphics context scheduling (a.k.a. GuC submission).
30  * - GT Power management.
31  *
32  * The enable_guc module parameter can be used to select which of those
33  * operations to enable within GuC. Note that not all the operations are
34  * supported on all gen9+ platforms.
35  *
36  * Enabling the GuC is not mandatory and therefore the firmware is only loaded
37  * if at least one of the operations is selected. However, not loading the GuC
38  * might result in the loss of some features that do require the GuC (currently
39  * just the HuC, but more are expected to land in the future).
40  */
41 
42 void intel_guc_notify(struct intel_guc *guc)
43 {
44 	struct intel_gt *gt = guc_to_gt(guc);
45 
46 	/*
47 	 * On Gen11+, the value written to the register is passes as a payload
48 	 * to the FW. However, the FW currently treats all values the same way
49 	 * (H2G interrupt), so we can just write the value that the HW expects
50 	 * on older gens.
51 	 */
52 	intel_uncore_write(gt->uncore, guc->notify_reg, GUC_SEND_TRIGGER);
53 }
54 
55 static inline i915_reg_t guc_send_reg(struct intel_guc *guc, u32 i)
56 {
57 	GEM_BUG_ON(!guc->send_regs.base);
58 	GEM_BUG_ON(!guc->send_regs.count);
59 	GEM_BUG_ON(i >= guc->send_regs.count);
60 
61 	return _MMIO(guc->send_regs.base + 4 * i);
62 }
63 
64 void intel_guc_init_send_regs(struct intel_guc *guc)
65 {
66 	struct intel_gt *gt = guc_to_gt(guc);
67 	enum forcewake_domains fw_domains = 0;
68 	unsigned int i;
69 
70 	GEM_BUG_ON(!guc->send_regs.base);
71 	GEM_BUG_ON(!guc->send_regs.count);
72 
73 	for (i = 0; i < guc->send_regs.count; i++) {
74 		fw_domains |= intel_uncore_forcewake_for_reg(gt->uncore,
75 					guc_send_reg(guc, i),
76 					FW_REG_READ | FW_REG_WRITE);
77 	}
78 	guc->send_regs.fw_domains = fw_domains;
79 }
80 
81 static void gen9_reset_guc_interrupts(struct intel_guc *guc)
82 {
83 	struct intel_gt *gt = guc_to_gt(guc);
84 
85 	assert_rpm_wakelock_held(&gt->i915->runtime_pm);
86 
87 	spin_lock_irq(gt->irq_lock);
88 	gen6_gt_pm_reset_iir(gt, gt->pm_guc_events);
89 	spin_unlock_irq(gt->irq_lock);
90 }
91 
92 static void gen9_enable_guc_interrupts(struct intel_guc *guc)
93 {
94 	struct intel_gt *gt = guc_to_gt(guc);
95 
96 	assert_rpm_wakelock_held(&gt->i915->runtime_pm);
97 
98 	spin_lock_irq(gt->irq_lock);
99 	guc_WARN_ON_ONCE(guc, intel_uncore_read(gt->uncore, GEN8_GT_IIR(2)) &
100 			 gt->pm_guc_events);
101 	gen6_gt_pm_enable_irq(gt, gt->pm_guc_events);
102 	spin_unlock_irq(gt->irq_lock);
103 
104 	guc->interrupts.enabled = true;
105 }
106 
107 static void gen9_disable_guc_interrupts(struct intel_guc *guc)
108 {
109 	struct intel_gt *gt = guc_to_gt(guc);
110 
111 	assert_rpm_wakelock_held(&gt->i915->runtime_pm);
112 	guc->interrupts.enabled = false;
113 
114 	spin_lock_irq(gt->irq_lock);
115 
116 	gen6_gt_pm_disable_irq(gt, gt->pm_guc_events);
117 
118 	spin_unlock_irq(gt->irq_lock);
119 	intel_synchronize_irq(gt->i915);
120 
121 	gen9_reset_guc_interrupts(guc);
122 }
123 
124 static bool __gen11_reset_guc_interrupts(struct intel_gt *gt)
125 {
126 	u32 irq = gt->type == GT_MEDIA ? MTL_MGUC : GEN11_GUC;
127 
128 	lockdep_assert_held(gt->irq_lock);
129 	return gen11_gt_reset_one_iir(gt, 0, irq);
130 }
131 
132 static void gen11_reset_guc_interrupts(struct intel_guc *guc)
133 {
134 	struct intel_gt *gt = guc_to_gt(guc);
135 
136 	spin_lock_irq(gt->irq_lock);
137 	__gen11_reset_guc_interrupts(gt);
138 	spin_unlock_irq(gt->irq_lock);
139 }
140 
141 static void gen11_enable_guc_interrupts(struct intel_guc *guc)
142 {
143 	struct intel_gt *gt = guc_to_gt(guc);
144 
145 	spin_lock_irq(gt->irq_lock);
146 	__gen11_reset_guc_interrupts(gt);
147 	spin_unlock_irq(gt->irq_lock);
148 
149 	guc->interrupts.enabled = true;
150 }
151 
152 static void gen11_disable_guc_interrupts(struct intel_guc *guc)
153 {
154 	struct intel_gt *gt = guc_to_gt(guc);
155 
156 	guc->interrupts.enabled = false;
157 	intel_synchronize_irq(gt->i915);
158 
159 	gen11_reset_guc_interrupts(guc);
160 }
161 
162 static void guc_dead_worker_func(struct work_struct *w)
163 {
164 	struct intel_guc *guc = container_of(w, struct intel_guc, dead_guc_worker);
165 	struct intel_gt *gt = guc_to_gt(guc);
166 	unsigned long last = guc->last_dead_guc_jiffies;
167 	unsigned long delta = jiffies_to_msecs(jiffies - last);
168 
169 	if (delta < 500) {
170 		intel_gt_set_wedged(gt);
171 	} else {
172 		intel_gt_handle_error(gt, ALL_ENGINES, I915_ERROR_CAPTURE, "dead GuC");
173 		guc->last_dead_guc_jiffies = jiffies;
174 	}
175 }
176 
177 void intel_guc_init_early(struct intel_guc *guc)
178 {
179 	struct intel_gt *gt = guc_to_gt(guc);
180 	struct drm_i915_private *i915 = gt->i915;
181 
182 	intel_uc_fw_init_early(&guc->fw, INTEL_UC_FW_TYPE_GUC, true);
183 	intel_guc_ct_init_early(&guc->ct);
184 	intel_guc_log_init_early(&guc->log);
185 	intel_guc_submission_init_early(guc);
186 	intel_guc_slpc_init_early(&guc->slpc);
187 	intel_guc_rc_init_early(guc);
188 
189 	INIT_WORK(&guc->dead_guc_worker, guc_dead_worker_func);
190 
191 	mutex_init(&guc->send_mutex);
192 	spin_lock_init(&guc->irq_lock);
193 	if (GRAPHICS_VER(i915) >= 11) {
194 		guc->interrupts.reset = gen11_reset_guc_interrupts;
195 		guc->interrupts.enable = gen11_enable_guc_interrupts;
196 		guc->interrupts.disable = gen11_disable_guc_interrupts;
197 		if (gt->type == GT_MEDIA) {
198 			guc->notify_reg = MEDIA_GUC_HOST_INTERRUPT;
199 			guc->send_regs.base = i915_mmio_reg_offset(MEDIA_SOFT_SCRATCH(0));
200 		} else {
201 			guc->notify_reg = GEN11_GUC_HOST_INTERRUPT;
202 			guc->send_regs.base = i915_mmio_reg_offset(GEN11_SOFT_SCRATCH(0));
203 		}
204 
205 		guc->send_regs.count = GEN11_SOFT_SCRATCH_COUNT;
206 
207 	} else {
208 		guc->notify_reg = GUC_SEND_INTERRUPT;
209 		guc->interrupts.reset = gen9_reset_guc_interrupts;
210 		guc->interrupts.enable = gen9_enable_guc_interrupts;
211 		guc->interrupts.disable = gen9_disable_guc_interrupts;
212 		guc->send_regs.base = i915_mmio_reg_offset(SOFT_SCRATCH(0));
213 		guc->send_regs.count = GUC_MAX_MMIO_MSG_LEN;
214 		BUILD_BUG_ON(GUC_MAX_MMIO_MSG_LEN > SOFT_SCRATCH_COUNT);
215 	}
216 
217 	intel_guc_enable_msg(guc, INTEL_GUC_RECV_MSG_EXCEPTION |
218 				  INTEL_GUC_RECV_MSG_CRASH_DUMP_POSTED);
219 }
220 
221 void intel_guc_init_late(struct intel_guc *guc)
222 {
223 	intel_guc_ads_init_late(guc);
224 }
225 
226 static u32 guc_ctl_debug_flags(struct intel_guc *guc)
227 {
228 	u32 level = intel_guc_log_get_level(&guc->log);
229 	u32 flags = 0;
230 
231 	if (!GUC_LOG_LEVEL_IS_VERBOSE(level))
232 		flags |= GUC_LOG_DISABLED;
233 	else
234 		flags |= GUC_LOG_LEVEL_TO_VERBOSITY(level) <<
235 			 GUC_LOG_VERBOSITY_SHIFT;
236 
237 	return flags;
238 }
239 
240 static u32 guc_ctl_feature_flags(struct intel_guc *guc)
241 {
242 	u32 flags = 0;
243 
244 	if (!intel_guc_submission_is_used(guc))
245 		flags |= GUC_CTL_DISABLE_SCHEDULER;
246 
247 	if (intel_guc_slpc_is_used(guc))
248 		flags |= GUC_CTL_ENABLE_SLPC;
249 
250 	return flags;
251 }
252 
253 static u32 guc_ctl_log_params_flags(struct intel_guc *guc)
254 {
255 	struct intel_guc_log *log = &guc->log;
256 	u32 offset, flags;
257 
258 	GEM_BUG_ON(!log->sizes_initialised);
259 
260 	offset = intel_guc_ggtt_offset(guc, log->vma) >> PAGE_SHIFT;
261 
262 	flags = GUC_LOG_VALID |
263 		GUC_LOG_NOTIFY_ON_HALF_FULL |
264 		log->sizes[GUC_LOG_SECTIONS_DEBUG].flag |
265 		log->sizes[GUC_LOG_SECTIONS_CAPTURE].flag |
266 		(log->sizes[GUC_LOG_SECTIONS_CRASH].count << GUC_LOG_CRASH_SHIFT) |
267 		(log->sizes[GUC_LOG_SECTIONS_DEBUG].count << GUC_LOG_DEBUG_SHIFT) |
268 		(log->sizes[GUC_LOG_SECTIONS_CAPTURE].count << GUC_LOG_CAPTURE_SHIFT) |
269 		(offset << GUC_LOG_BUF_ADDR_SHIFT);
270 
271 	return flags;
272 }
273 
274 static u32 guc_ctl_ads_flags(struct intel_guc *guc)
275 {
276 	u32 ads = intel_guc_ggtt_offset(guc, guc->ads_vma) >> PAGE_SHIFT;
277 	u32 flags = ads << GUC_ADS_ADDR_SHIFT;
278 
279 	return flags;
280 }
281 
282 static u32 guc_ctl_wa_flags(struct intel_guc *guc)
283 {
284 	struct intel_gt *gt = guc_to_gt(guc);
285 	u32 flags = 0;
286 
287 	/* Wa_22012773006:gen11,gen12 < XeHP */
288 	if (GRAPHICS_VER(gt->i915) >= 11 &&
289 	    GRAPHICS_VER_FULL(gt->i915) < IP_VER(12, 55))
290 		flags |= GUC_WA_POLLCS;
291 
292 	/* Wa_14014475959 */
293 	if (IS_GFX_GT_IP_STEP(gt, IP_VER(12, 70), STEP_A0, STEP_B0) ||
294 	    IS_DG2(gt->i915))
295 		flags |= GUC_WA_HOLD_CCS_SWITCHOUT;
296 
297 	/*
298 	 * Wa_14012197797
299 	 * Wa_22011391025
300 	 *
301 	 * The same WA bit is used for both and 22011391025 is applicable to
302 	 * all DG2.
303 	 */
304 	if (IS_DG2(gt->i915))
305 		flags |= GUC_WA_DUAL_QUEUE;
306 
307 	/* Wa_22011802037: graphics version 11/12 */
308 	if (intel_engine_reset_needs_wa_22011802037(gt))
309 		flags |= GUC_WA_PRE_PARSER;
310 
311 	/*
312 	 * Wa_22012727170
313 	 * Wa_22012727685
314 	 */
315 	if (IS_DG2_G11(gt->i915))
316 		flags |= GUC_WA_CONTEXT_ISOLATION;
317 
318 	/* Wa_14018913170 */
319 	if (GUC_FIRMWARE_VER(guc) >= MAKE_GUC_VER(70, 7, 0)) {
320 		if (IS_DG2(gt->i915) || IS_METEORLAKE(gt->i915))
321 			flags |= GUC_WA_ENABLE_TSC_CHECK_ON_RC6;
322 	}
323 
324 	return flags;
325 }
326 
327 static u32 guc_ctl_devid(struct intel_guc *guc)
328 {
329 	struct drm_i915_private *i915 = guc_to_i915(guc);
330 
331 	return (INTEL_DEVID(i915) << 16) | INTEL_REVID(i915);
332 }
333 
334 /*
335  * Initialise the GuC parameter block before starting the firmware
336  * transfer. These parameters are read by the firmware on startup
337  * and cannot be changed thereafter.
338  */
339 static void guc_init_params(struct intel_guc *guc)
340 {
341 	u32 *params = guc->params;
342 	int i;
343 
344 	BUILD_BUG_ON(sizeof(guc->params) != GUC_CTL_MAX_DWORDS * sizeof(u32));
345 
346 	params[GUC_CTL_LOG_PARAMS] = guc_ctl_log_params_flags(guc);
347 	params[GUC_CTL_FEATURE] = guc_ctl_feature_flags(guc);
348 	params[GUC_CTL_DEBUG] = guc_ctl_debug_flags(guc);
349 	params[GUC_CTL_ADS] = guc_ctl_ads_flags(guc);
350 	params[GUC_CTL_WA] = guc_ctl_wa_flags(guc);
351 	params[GUC_CTL_DEVID] = guc_ctl_devid(guc);
352 
353 	for (i = 0; i < GUC_CTL_MAX_DWORDS; i++)
354 		guc_dbg(guc, "param[%2d] = %#x\n", i, params[i]);
355 }
356 
357 /*
358  * Initialise the GuC parameter block before starting the firmware
359  * transfer. These parameters are read by the firmware on startup
360  * and cannot be changed thereafter.
361  */
362 void intel_guc_write_params(struct intel_guc *guc)
363 {
364 	struct intel_uncore *uncore = guc_to_gt(guc)->uncore;
365 	int i;
366 
367 	/*
368 	 * All SOFT_SCRATCH registers are in FORCEWAKE_GT domain and
369 	 * they are power context saved so it's ok to release forcewake
370 	 * when we are done here and take it again at xfer time.
371 	 */
372 	intel_uncore_forcewake_get(uncore, FORCEWAKE_GT);
373 
374 	intel_uncore_write(uncore, SOFT_SCRATCH(0), 0);
375 
376 	for (i = 0; i < GUC_CTL_MAX_DWORDS; i++)
377 		intel_uncore_write(uncore, SOFT_SCRATCH(1 + i), guc->params[i]);
378 
379 	intel_uncore_forcewake_put(uncore, FORCEWAKE_GT);
380 }
381 
382 void intel_guc_dump_time_info(struct intel_guc *guc, struct drm_printer *p)
383 {
384 	struct intel_gt *gt = guc_to_gt(guc);
385 	intel_wakeref_t wakeref;
386 	u32 stamp = 0;
387 	u64 ktime;
388 
389 	with_intel_runtime_pm(&gt->i915->runtime_pm, wakeref)
390 		stamp = intel_uncore_read(gt->uncore, GUCPMTIMESTAMP);
391 	ktime = ktime_get_boottime_ns();
392 
393 	drm_printf(p, "Kernel timestamp: 0x%08llX [%llu]\n", ktime, ktime);
394 	drm_printf(p, "GuC timestamp: 0x%08X [%u]\n", stamp, stamp);
395 	drm_printf(p, "CS timestamp frequency: %u Hz, %u ns\n",
396 		   gt->clock_frequency, gt->clock_period_ns);
397 }
398 
399 int intel_guc_init(struct intel_guc *guc)
400 {
401 	int ret;
402 
403 	ret = intel_uc_fw_init(&guc->fw);
404 	if (ret)
405 		goto out;
406 
407 	ret = intel_guc_log_create(&guc->log);
408 	if (ret)
409 		goto err_fw;
410 
411 	ret = intel_guc_capture_init(guc);
412 	if (ret)
413 		goto err_log;
414 
415 	ret = intel_guc_ads_create(guc);
416 	if (ret)
417 		goto err_capture;
418 
419 	GEM_BUG_ON(!guc->ads_vma);
420 
421 	ret = intel_guc_ct_init(&guc->ct);
422 	if (ret)
423 		goto err_ads;
424 
425 	if (intel_guc_submission_is_used(guc)) {
426 		/*
427 		 * This is stuff we need to have available at fw load time
428 		 * if we are planning to enable submission later
429 		 */
430 		ret = intel_guc_submission_init(guc);
431 		if (ret)
432 			goto err_ct;
433 	}
434 
435 	if (intel_guc_slpc_is_used(guc)) {
436 		ret = intel_guc_slpc_init(&guc->slpc);
437 		if (ret)
438 			goto err_submission;
439 	}
440 
441 	/* now that everything is perma-pinned, initialize the parameters */
442 	guc_init_params(guc);
443 
444 	intel_uc_fw_change_status(&guc->fw, INTEL_UC_FIRMWARE_LOADABLE);
445 
446 	return 0;
447 
448 err_submission:
449 	intel_guc_submission_fini(guc);
450 err_ct:
451 	intel_guc_ct_fini(&guc->ct);
452 err_ads:
453 	intel_guc_ads_destroy(guc);
454 err_capture:
455 	intel_guc_capture_destroy(guc);
456 err_log:
457 	intel_guc_log_destroy(&guc->log);
458 err_fw:
459 	intel_uc_fw_fini(&guc->fw);
460 out:
461 	intel_uc_fw_change_status(&guc->fw, INTEL_UC_FIRMWARE_INIT_FAIL);
462 	guc_probe_error(guc, "failed with %pe\n", ERR_PTR(ret));
463 	return ret;
464 }
465 
466 void intel_guc_fini(struct intel_guc *guc)
467 {
468 	if (!intel_uc_fw_is_loadable(&guc->fw))
469 		return;
470 
471 	flush_work(&guc->dead_guc_worker);
472 
473 	if (intel_guc_slpc_is_used(guc))
474 		intel_guc_slpc_fini(&guc->slpc);
475 
476 	if (intel_guc_submission_is_used(guc))
477 		intel_guc_submission_fini(guc);
478 
479 	intel_guc_ct_fini(&guc->ct);
480 
481 	intel_guc_ads_destroy(guc);
482 	intel_guc_capture_destroy(guc);
483 	intel_guc_log_destroy(&guc->log);
484 	intel_uc_fw_fini(&guc->fw);
485 }
486 
487 /*
488  * This function implements the MMIO based host to GuC interface.
489  */
490 int intel_guc_send_mmio(struct intel_guc *guc, const u32 *request, u32 len,
491 			u32 *response_buf, u32 response_buf_size)
492 {
493 	struct intel_uncore *uncore = guc_to_gt(guc)->uncore;
494 	u32 header;
495 	int i;
496 	int ret;
497 
498 	GEM_BUG_ON(!len);
499 	GEM_BUG_ON(len > guc->send_regs.count);
500 
501 	GEM_BUG_ON(FIELD_GET(GUC_HXG_MSG_0_ORIGIN, request[0]) != GUC_HXG_ORIGIN_HOST);
502 	GEM_BUG_ON(FIELD_GET(GUC_HXG_MSG_0_TYPE, request[0]) != GUC_HXG_TYPE_REQUEST);
503 
504 	mutex_lock(&guc->send_mutex);
505 	intel_uncore_forcewake_get(uncore, guc->send_regs.fw_domains);
506 
507 retry:
508 	for (i = 0; i < len; i++)
509 		intel_uncore_write(uncore, guc_send_reg(guc, i), request[i]);
510 
511 	intel_uncore_posting_read(uncore, guc_send_reg(guc, i - 1));
512 
513 	intel_guc_notify(guc);
514 
515 	/*
516 	 * No GuC command should ever take longer than 10ms.
517 	 * Fast commands should still complete in 10us.
518 	 */
519 	ret = __intel_wait_for_register_fw(uncore,
520 					   guc_send_reg(guc, 0),
521 					   GUC_HXG_MSG_0_ORIGIN,
522 					   FIELD_PREP(GUC_HXG_MSG_0_ORIGIN,
523 						      GUC_HXG_ORIGIN_GUC),
524 					   10, 10, &header);
525 	if (unlikely(ret)) {
526 timeout:
527 		guc_err(guc, "mmio request %#x: no reply %x\n",
528 			request[0], header);
529 		goto out;
530 	}
531 
532 	if (FIELD_GET(GUC_HXG_MSG_0_TYPE, header) == GUC_HXG_TYPE_NO_RESPONSE_BUSY) {
533 #define done ({ header = intel_uncore_read(uncore, guc_send_reg(guc, 0)); \
534 		FIELD_GET(GUC_HXG_MSG_0_ORIGIN, header) != GUC_HXG_ORIGIN_GUC || \
535 		FIELD_GET(GUC_HXG_MSG_0_TYPE, header) != GUC_HXG_TYPE_NO_RESPONSE_BUSY; })
536 
537 		ret = wait_for(done, 1000);
538 		if (unlikely(ret))
539 			goto timeout;
540 		if (unlikely(FIELD_GET(GUC_HXG_MSG_0_ORIGIN, header) !=
541 				       GUC_HXG_ORIGIN_GUC))
542 			goto proto;
543 #undef done
544 	}
545 
546 	if (FIELD_GET(GUC_HXG_MSG_0_TYPE, header) == GUC_HXG_TYPE_NO_RESPONSE_RETRY) {
547 		u32 reason = FIELD_GET(GUC_HXG_RETRY_MSG_0_REASON, header);
548 
549 		guc_dbg(guc, "mmio request %#x: retrying, reason %u\n",
550 			request[0], reason);
551 		goto retry;
552 	}
553 
554 	if (FIELD_GET(GUC_HXG_MSG_0_TYPE, header) == GUC_HXG_TYPE_RESPONSE_FAILURE) {
555 		u32 hint = FIELD_GET(GUC_HXG_FAILURE_MSG_0_HINT, header);
556 		u32 error = FIELD_GET(GUC_HXG_FAILURE_MSG_0_ERROR, header);
557 
558 		guc_err(guc, "mmio request %#x: failure %x/%u\n",
559 			request[0], error, hint);
560 		ret = -ENXIO;
561 		goto out;
562 	}
563 
564 	if (FIELD_GET(GUC_HXG_MSG_0_TYPE, header) != GUC_HXG_TYPE_RESPONSE_SUCCESS) {
565 proto:
566 		guc_err(guc, "mmio request %#x: unexpected reply %#x\n",
567 			request[0], header);
568 		ret = -EPROTO;
569 		goto out;
570 	}
571 
572 	if (response_buf) {
573 		int count = min(response_buf_size, guc->send_regs.count);
574 
575 		GEM_BUG_ON(!count);
576 
577 		response_buf[0] = header;
578 
579 		for (i = 1; i < count; i++)
580 			response_buf[i] = intel_uncore_read(uncore,
581 							    guc_send_reg(guc, i));
582 
583 		/* Use number of copied dwords as our return value */
584 		ret = count;
585 	} else {
586 		/* Use data from the GuC response as our return value */
587 		ret = FIELD_GET(GUC_HXG_RESPONSE_MSG_0_DATA0, header);
588 	}
589 
590 out:
591 	intel_uncore_forcewake_put(uncore, guc->send_regs.fw_domains);
592 	mutex_unlock(&guc->send_mutex);
593 
594 	return ret;
595 }
596 
597 int intel_guc_crash_process_msg(struct intel_guc *guc, u32 action)
598 {
599 	if (action == INTEL_GUC_ACTION_NOTIFY_CRASH_DUMP_POSTED)
600 		guc_err(guc, "Crash dump notification\n");
601 	else if (action == INTEL_GUC_ACTION_NOTIFY_EXCEPTION)
602 		guc_err(guc, "Exception notification\n");
603 	else
604 		guc_err(guc, "Unknown crash notification: 0x%04X\n", action);
605 
606 	queue_work(system_unbound_wq, &guc->dead_guc_worker);
607 
608 	return 0;
609 }
610 
611 int intel_guc_to_host_process_recv_msg(struct intel_guc *guc,
612 				       const u32 *payload, u32 len)
613 {
614 	u32 msg;
615 
616 	if (unlikely(!len))
617 		return -EPROTO;
618 
619 	/* Make sure to handle only enabled messages */
620 	msg = payload[0] & guc->msg_enabled_mask;
621 
622 	if (msg & INTEL_GUC_RECV_MSG_CRASH_DUMP_POSTED)
623 		guc_err(guc, "Received early crash dump notification!\n");
624 	if (msg & INTEL_GUC_RECV_MSG_EXCEPTION)
625 		guc_err(guc, "Received early exception notification!\n");
626 
627 	if (msg & (INTEL_GUC_RECV_MSG_CRASH_DUMP_POSTED | INTEL_GUC_RECV_MSG_EXCEPTION))
628 		queue_work(system_unbound_wq, &guc->dead_guc_worker);
629 
630 	return 0;
631 }
632 
633 /**
634  * intel_guc_auth_huc() - Send action to GuC to authenticate HuC ucode
635  * @guc: intel_guc structure
636  * @rsa_offset: rsa offset w.r.t ggtt base of huc vma
637  *
638  * Triggers a HuC firmware authentication request to the GuC via intel_guc_send
639  * INTEL_GUC_ACTION_AUTHENTICATE_HUC interface. This function is invoked by
640  * intel_huc_auth().
641  *
642  * Return:	non-zero code on error
643  */
644 int intel_guc_auth_huc(struct intel_guc *guc, u32 rsa_offset)
645 {
646 	u32 action[] = {
647 		INTEL_GUC_ACTION_AUTHENTICATE_HUC,
648 		rsa_offset
649 	};
650 
651 	return intel_guc_send(guc, action, ARRAY_SIZE(action));
652 }
653 
654 /**
655  * intel_guc_suspend() - notify GuC entering suspend state
656  * @guc:	the guc
657  */
658 int intel_guc_suspend(struct intel_guc *guc)
659 {
660 	int ret;
661 	u32 action[] = {
662 		INTEL_GUC_ACTION_CLIENT_SOFT_RESET,
663 	};
664 
665 	if (!intel_guc_is_ready(guc))
666 		return 0;
667 
668 	if (intel_guc_submission_is_used(guc)) {
669 		flush_work(&guc->dead_guc_worker);
670 
671 		/*
672 		 * This H2G MMIO command tears down the GuC in two steps. First it will
673 		 * generate a G2H CTB for every active context indicating a reset. In
674 		 * practice the i915 shouldn't ever get a G2H as suspend should only be
675 		 * called when the GPU is idle. Next, it tears down the CTBs and this
676 		 * H2G MMIO command completes.
677 		 *
678 		 * Don't abort on a failure code from the GuC. Keep going and do the
679 		 * clean up in santize() and re-initialisation on resume and hopefully
680 		 * the error here won't be problematic.
681 		 */
682 		ret = intel_guc_send_mmio(guc, action, ARRAY_SIZE(action), NULL, 0);
683 		if (ret)
684 			guc_err(guc, "suspend: RESET_CLIENT action failed with %pe\n",
685 				ERR_PTR(ret));
686 	}
687 
688 	/* Signal that the GuC isn't running. */
689 	intel_guc_sanitize(guc);
690 
691 	return 0;
692 }
693 
694 /**
695  * intel_guc_resume() - notify GuC resuming from suspend state
696  * @guc:	the guc
697  */
698 int intel_guc_resume(struct intel_guc *guc)
699 {
700 	/*
701 	 * NB: This function can still be called even if GuC submission is
702 	 * disabled, e.g. if GuC is enabled for HuC authentication only. Thus,
703 	 * if any code is later added here, it must be support doing nothing
704 	 * if submission is disabled (as per intel_guc_suspend).
705 	 */
706 	return 0;
707 }
708 
709 /**
710  * DOC: GuC Memory Management
711  *
712  * GuC can't allocate any memory for its own usage, so all the allocations must
713  * be handled by the host driver. GuC accesses the memory via the GGTT, with the
714  * exception of the top and bottom parts of the 4GB address space, which are
715  * instead re-mapped by the GuC HW to memory location of the FW itself (WOPCM)
716  * or other parts of the HW. The driver must take care not to place objects that
717  * the GuC is going to access in these reserved ranges. The layout of the GuC
718  * address space is shown below:
719  *
720  * ::
721  *
722  *     +===========> +====================+ <== FFFF_FFFF
723  *     ^             |      Reserved      |
724  *     |             +====================+ <== GUC_GGTT_TOP
725  *     |             |                    |
726  *     |             |        DRAM        |
727  *    GuC            |                    |
728  *  Address    +===> +====================+ <== GuC ggtt_pin_bias
729  *   Space     ^     |                    |
730  *     |       |     |                    |
731  *     |      GuC    |        GuC         |
732  *     |     WOPCM   |       WOPCM        |
733  *     |      Size   |                    |
734  *     |       |     |                    |
735  *     v       v     |                    |
736  *     +=======+===> +====================+ <== 0000_0000
737  *
738  * The lower part of GuC Address Space [0, ggtt_pin_bias) is mapped to GuC WOPCM
739  * while upper part of GuC Address Space [ggtt_pin_bias, GUC_GGTT_TOP) is mapped
740  * to DRAM. The value of the GuC ggtt_pin_bias is the GuC WOPCM size.
741  */
742 
743 /**
744  * intel_guc_allocate_vma() - Allocate a GGTT VMA for GuC usage
745  * @guc:	the guc
746  * @size:	size of area to allocate (both virtual space and memory)
747  *
748  * This is a wrapper to create an object for use with the GuC. In order to
749  * use it inside the GuC, an object needs to be pinned lifetime, so we allocate
750  * both some backing storage and a range inside the Global GTT. We must pin
751  * it in the GGTT somewhere other than than [0, GUC ggtt_pin_bias) because that
752  * range is reserved inside GuC.
753  *
754  * Return:	A i915_vma if successful, otherwise an ERR_PTR.
755  */
756 struct i915_vma *intel_guc_allocate_vma(struct intel_guc *guc, u32 size)
757 {
758 	struct intel_gt *gt = guc_to_gt(guc);
759 	struct drm_i915_gem_object *obj;
760 	struct i915_vma *vma;
761 	u64 flags;
762 	int ret;
763 
764 	if (HAS_LMEM(gt->i915))
765 		obj = i915_gem_object_create_lmem(gt->i915, size,
766 						  I915_BO_ALLOC_CPU_CLEAR |
767 						  I915_BO_ALLOC_CONTIGUOUS |
768 						  I915_BO_ALLOC_PM_EARLY);
769 	else
770 		obj = i915_gem_object_create_shmem(gt->i915, size);
771 
772 	if (IS_ERR(obj))
773 		return ERR_CAST(obj);
774 
775 	/*
776 	 * Wa_22016122933: For Media version 13.0, all Media GT shared
777 	 * memory needs to be mapped as WC on CPU side and UC (PAT
778 	 * index 2) on GPU side.
779 	 */
780 	if (intel_gt_needs_wa_22016122933(gt))
781 		i915_gem_object_set_cache_coherency(obj, I915_CACHE_NONE);
782 
783 	vma = i915_vma_instance(obj, &gt->ggtt->vm, NULL);
784 	if (IS_ERR(vma))
785 		goto err;
786 
787 	flags = PIN_OFFSET_BIAS | i915_ggtt_pin_bias(vma);
788 	ret = i915_ggtt_pin(vma, NULL, 0, flags);
789 	if (ret) {
790 		vma = ERR_PTR(ret);
791 		goto err;
792 	}
793 
794 	return i915_vma_make_unshrinkable(vma);
795 
796 err:
797 	i915_gem_object_put(obj);
798 	return vma;
799 }
800 
801 /**
802  * intel_guc_allocate_and_map_vma() - Allocate and map VMA for GuC usage
803  * @guc:	the guc
804  * @size:	size of area to allocate (both virtual space and memory)
805  * @out_vma:	return variable for the allocated vma pointer
806  * @out_vaddr:	return variable for the obj mapping
807  *
808  * This wrapper calls intel_guc_allocate_vma() and then maps the allocated
809  * object with I915_MAP_WB.
810  *
811  * Return:	0 if successful, a negative errno code otherwise.
812  */
813 int intel_guc_allocate_and_map_vma(struct intel_guc *guc, u32 size,
814 				   struct i915_vma **out_vma, void **out_vaddr)
815 {
816 	struct i915_vma *vma;
817 	void *vaddr;
818 
819 	vma = intel_guc_allocate_vma(guc, size);
820 	if (IS_ERR(vma))
821 		return PTR_ERR(vma);
822 
823 	vaddr = i915_gem_object_pin_map_unlocked(vma->obj,
824 						 intel_gt_coherent_map_type(guc_to_gt(guc),
825 									    vma->obj, true));
826 	if (IS_ERR(vaddr)) {
827 		i915_vma_unpin_and_release(&vma, 0);
828 		return PTR_ERR(vaddr);
829 	}
830 
831 	*out_vma = vma;
832 	*out_vaddr = vaddr;
833 
834 	return 0;
835 }
836 
837 static int __guc_action_self_cfg(struct intel_guc *guc, u16 key, u16 len, u64 value)
838 {
839 	u32 request[HOST2GUC_SELF_CFG_REQUEST_MSG_LEN] = {
840 		FIELD_PREP(GUC_HXG_MSG_0_ORIGIN, GUC_HXG_ORIGIN_HOST) |
841 		FIELD_PREP(GUC_HXG_MSG_0_TYPE, GUC_HXG_TYPE_REQUEST) |
842 		FIELD_PREP(GUC_HXG_REQUEST_MSG_0_ACTION, GUC_ACTION_HOST2GUC_SELF_CFG),
843 		FIELD_PREP(HOST2GUC_SELF_CFG_REQUEST_MSG_1_KLV_KEY, key) |
844 		FIELD_PREP(HOST2GUC_SELF_CFG_REQUEST_MSG_1_KLV_LEN, len),
845 		FIELD_PREP(HOST2GUC_SELF_CFG_REQUEST_MSG_2_VALUE32, lower_32_bits(value)),
846 		FIELD_PREP(HOST2GUC_SELF_CFG_REQUEST_MSG_3_VALUE64, upper_32_bits(value)),
847 	};
848 	int ret;
849 
850 	GEM_BUG_ON(len > 2);
851 	GEM_BUG_ON(len == 1 && upper_32_bits(value));
852 
853 	/* Self config must go over MMIO */
854 	ret = intel_guc_send_mmio(guc, request, ARRAY_SIZE(request), NULL, 0);
855 
856 	if (unlikely(ret < 0))
857 		return ret;
858 	if (unlikely(ret > 1))
859 		return -EPROTO;
860 	if (unlikely(!ret))
861 		return -ENOKEY;
862 
863 	return 0;
864 }
865 
866 static int __guc_self_cfg(struct intel_guc *guc, u16 key, u16 len, u64 value)
867 {
868 	int err = __guc_action_self_cfg(guc, key, len, value);
869 
870 	if (unlikely(err))
871 		guc_probe_error(guc, "Unsuccessful self-config (%pe) key %#hx value %#llx\n",
872 				ERR_PTR(err), key, value);
873 	return err;
874 }
875 
876 int intel_guc_self_cfg32(struct intel_guc *guc, u16 key, u32 value)
877 {
878 	return __guc_self_cfg(guc, key, 1, value);
879 }
880 
881 int intel_guc_self_cfg64(struct intel_guc *guc, u16 key, u64 value)
882 {
883 	return __guc_self_cfg(guc, key, 2, value);
884 }
885 
886 /**
887  * intel_guc_load_status - dump information about GuC load status
888  * @guc: the GuC
889  * @p: the &drm_printer
890  *
891  * Pretty printer for GuC load status.
892  */
893 void intel_guc_load_status(struct intel_guc *guc, struct drm_printer *p)
894 {
895 	struct intel_gt *gt = guc_to_gt(guc);
896 	struct intel_uncore *uncore = gt->uncore;
897 	intel_wakeref_t wakeref;
898 
899 	if (!intel_guc_is_supported(guc)) {
900 		drm_printf(p, "GuC not supported\n");
901 		return;
902 	}
903 
904 	if (!intel_guc_is_wanted(guc)) {
905 		drm_printf(p, "GuC disabled\n");
906 		return;
907 	}
908 
909 	intel_uc_fw_dump(&guc->fw, p);
910 
911 	with_intel_runtime_pm(uncore->rpm, wakeref) {
912 		u32 status = intel_uncore_read(uncore, GUC_STATUS);
913 		u32 i;
914 
915 		drm_printf(p, "GuC status 0x%08x:\n", status);
916 		drm_printf(p, "\tBootrom status = 0x%x\n",
917 			   (status & GS_BOOTROM_MASK) >> GS_BOOTROM_SHIFT);
918 		drm_printf(p, "\tuKernel status = 0x%x\n",
919 			   (status & GS_UKERNEL_MASK) >> GS_UKERNEL_SHIFT);
920 		drm_printf(p, "\tMIA Core status = 0x%x\n",
921 			   (status & GS_MIA_MASK) >> GS_MIA_SHIFT);
922 		drm_puts(p, "Scratch registers:\n");
923 		for (i = 0; i < 16; i++) {
924 			drm_printf(p, "\t%2d: \t0x%x\n",
925 				   i, intel_uncore_read(uncore, SOFT_SCRATCH(i)));
926 		}
927 	}
928 }
929 
930 void intel_guc_write_barrier(struct intel_guc *guc)
931 {
932 	struct intel_gt *gt = guc_to_gt(guc);
933 
934 	if (i915_gem_object_is_lmem(guc->ct.vma->obj)) {
935 		/*
936 		 * Ensure intel_uncore_write_fw can be used rather than
937 		 * intel_uncore_write.
938 		 */
939 		GEM_BUG_ON(guc->send_regs.fw_domains);
940 
941 		/*
942 		 * This register is used by the i915 and GuC for MMIO based
943 		 * communication. Once we are in this code CTBs are the only
944 		 * method the i915 uses to communicate with the GuC so it is
945 		 * safe to write to this register (a value of 0 is NOP for MMIO
946 		 * communication). If we ever start mixing CTBs and MMIOs a new
947 		 * register will have to be chosen. This function is also used
948 		 * to enforce ordering of a work queue item write and an update
949 		 * to the process descriptor. When a work queue is being used,
950 		 * CTBs are also the only mechanism of communication.
951 		 */
952 		intel_uncore_write_fw(gt->uncore, GEN11_SOFT_SCRATCH(0), 0);
953 	} else {
954 		/* wmb() sufficient for a barrier if in smem */
955 		wmb();
956 	}
957 }
958