xref: /linux/drivers/gpu/drm/i915/gt/uc/intel_guc.c (revision f017f1e9cb3458a86f586a171e284e2ec46286db)
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, 50))
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_16015675438 */
319 	if (!RCS_MASK(gt))
320 		flags |= GUC_WA_RCS_REGS_IN_CCS_REGS_LIST;
321 
322 	return flags;
323 }
324 
325 static u32 guc_ctl_devid(struct intel_guc *guc)
326 {
327 	struct drm_i915_private *i915 = guc_to_gt(guc)->i915;
328 
329 	return (INTEL_DEVID(i915) << 16) | INTEL_REVID(i915);
330 }
331 
332 /*
333  * Initialise the GuC parameter block before starting the firmware
334  * transfer. These parameters are read by the firmware on startup
335  * and cannot be changed thereafter.
336  */
337 static void guc_init_params(struct intel_guc *guc)
338 {
339 	u32 *params = guc->params;
340 	int i;
341 
342 	BUILD_BUG_ON(sizeof(guc->params) != GUC_CTL_MAX_DWORDS * sizeof(u32));
343 
344 	params[GUC_CTL_LOG_PARAMS] = guc_ctl_log_params_flags(guc);
345 	params[GUC_CTL_FEATURE] = guc_ctl_feature_flags(guc);
346 	params[GUC_CTL_DEBUG] = guc_ctl_debug_flags(guc);
347 	params[GUC_CTL_ADS] = guc_ctl_ads_flags(guc);
348 	params[GUC_CTL_WA] = guc_ctl_wa_flags(guc);
349 	params[GUC_CTL_DEVID] = guc_ctl_devid(guc);
350 
351 	for (i = 0; i < GUC_CTL_MAX_DWORDS; i++)
352 		guc_dbg(guc, "param[%2d] = %#x\n", i, params[i]);
353 }
354 
355 /*
356  * Initialise the GuC parameter block before starting the firmware
357  * transfer. These parameters are read by the firmware on startup
358  * and cannot be changed thereafter.
359  */
360 void intel_guc_write_params(struct intel_guc *guc)
361 {
362 	struct intel_uncore *uncore = guc_to_gt(guc)->uncore;
363 	int i;
364 
365 	/*
366 	 * All SOFT_SCRATCH registers are in FORCEWAKE_GT domain and
367 	 * they are power context saved so it's ok to release forcewake
368 	 * when we are done here and take it again at xfer time.
369 	 */
370 	intel_uncore_forcewake_get(uncore, FORCEWAKE_GT);
371 
372 	intel_uncore_write(uncore, SOFT_SCRATCH(0), 0);
373 
374 	for (i = 0; i < GUC_CTL_MAX_DWORDS; i++)
375 		intel_uncore_write(uncore, SOFT_SCRATCH(1 + i), guc->params[i]);
376 
377 	intel_uncore_forcewake_put(uncore, FORCEWAKE_GT);
378 }
379 
380 void intel_guc_dump_time_info(struct intel_guc *guc, struct drm_printer *p)
381 {
382 	struct intel_gt *gt = guc_to_gt(guc);
383 	intel_wakeref_t wakeref;
384 	u32 stamp = 0;
385 	u64 ktime;
386 
387 	with_intel_runtime_pm(&gt->i915->runtime_pm, wakeref)
388 		stamp = intel_uncore_read(gt->uncore, GUCPMTIMESTAMP);
389 	ktime = ktime_get_boottime_ns();
390 
391 	drm_printf(p, "Kernel timestamp: 0x%08llX [%llu]\n", ktime, ktime);
392 	drm_printf(p, "GuC timestamp: 0x%08X [%u]\n", stamp, stamp);
393 	drm_printf(p, "CS timestamp frequency: %u Hz, %u ns\n",
394 		   gt->clock_frequency, gt->clock_period_ns);
395 }
396 
397 int intel_guc_init(struct intel_guc *guc)
398 {
399 	int ret;
400 
401 	ret = intel_uc_fw_init(&guc->fw);
402 	if (ret)
403 		goto out;
404 
405 	ret = intel_guc_log_create(&guc->log);
406 	if (ret)
407 		goto err_fw;
408 
409 	ret = intel_guc_capture_init(guc);
410 	if (ret)
411 		goto err_log;
412 
413 	ret = intel_guc_ads_create(guc);
414 	if (ret)
415 		goto err_capture;
416 
417 	GEM_BUG_ON(!guc->ads_vma);
418 
419 	ret = intel_guc_ct_init(&guc->ct);
420 	if (ret)
421 		goto err_ads;
422 
423 	if (intel_guc_submission_is_used(guc)) {
424 		/*
425 		 * This is stuff we need to have available at fw load time
426 		 * if we are planning to enable submission later
427 		 */
428 		ret = intel_guc_submission_init(guc);
429 		if (ret)
430 			goto err_ct;
431 	}
432 
433 	if (intel_guc_slpc_is_used(guc)) {
434 		ret = intel_guc_slpc_init(&guc->slpc);
435 		if (ret)
436 			goto err_submission;
437 	}
438 
439 	/* now that everything is perma-pinned, initialize the parameters */
440 	guc_init_params(guc);
441 
442 	intel_uc_fw_change_status(&guc->fw, INTEL_UC_FIRMWARE_LOADABLE);
443 
444 	return 0;
445 
446 err_submission:
447 	intel_guc_submission_fini(guc);
448 err_ct:
449 	intel_guc_ct_fini(&guc->ct);
450 err_ads:
451 	intel_guc_ads_destroy(guc);
452 err_capture:
453 	intel_guc_capture_destroy(guc);
454 err_log:
455 	intel_guc_log_destroy(&guc->log);
456 err_fw:
457 	intel_uc_fw_fini(&guc->fw);
458 out:
459 	intel_uc_fw_change_status(&guc->fw, INTEL_UC_FIRMWARE_INIT_FAIL);
460 	guc_probe_error(guc, "failed with %pe\n", ERR_PTR(ret));
461 	return ret;
462 }
463 
464 void intel_guc_fini(struct intel_guc *guc)
465 {
466 	if (!intel_uc_fw_is_loadable(&guc->fw))
467 		return;
468 
469 	flush_work(&guc->dead_guc_worker);
470 
471 	if (intel_guc_slpc_is_used(guc))
472 		intel_guc_slpc_fini(&guc->slpc);
473 
474 	if (intel_guc_submission_is_used(guc))
475 		intel_guc_submission_fini(guc);
476 
477 	intel_guc_ct_fini(&guc->ct);
478 
479 	intel_guc_ads_destroy(guc);
480 	intel_guc_capture_destroy(guc);
481 	intel_guc_log_destroy(&guc->log);
482 	intel_uc_fw_fini(&guc->fw);
483 }
484 
485 /*
486  * This function implements the MMIO based host to GuC interface.
487  */
488 int intel_guc_send_mmio(struct intel_guc *guc, const u32 *request, u32 len,
489 			u32 *response_buf, u32 response_buf_size)
490 {
491 	struct intel_uncore *uncore = guc_to_gt(guc)->uncore;
492 	u32 header;
493 	int i;
494 	int ret;
495 
496 	GEM_BUG_ON(!len);
497 	GEM_BUG_ON(len > guc->send_regs.count);
498 
499 	GEM_BUG_ON(FIELD_GET(GUC_HXG_MSG_0_ORIGIN, request[0]) != GUC_HXG_ORIGIN_HOST);
500 	GEM_BUG_ON(FIELD_GET(GUC_HXG_MSG_0_TYPE, request[0]) != GUC_HXG_TYPE_REQUEST);
501 
502 	mutex_lock(&guc->send_mutex);
503 	intel_uncore_forcewake_get(uncore, guc->send_regs.fw_domains);
504 
505 retry:
506 	for (i = 0; i < len; i++)
507 		intel_uncore_write(uncore, guc_send_reg(guc, i), request[i]);
508 
509 	intel_uncore_posting_read(uncore, guc_send_reg(guc, i - 1));
510 
511 	intel_guc_notify(guc);
512 
513 	/*
514 	 * No GuC command should ever take longer than 10ms.
515 	 * Fast commands should still complete in 10us.
516 	 */
517 	ret = __intel_wait_for_register_fw(uncore,
518 					   guc_send_reg(guc, 0),
519 					   GUC_HXG_MSG_0_ORIGIN,
520 					   FIELD_PREP(GUC_HXG_MSG_0_ORIGIN,
521 						      GUC_HXG_ORIGIN_GUC),
522 					   10, 10, &header);
523 	if (unlikely(ret)) {
524 timeout:
525 		guc_err(guc, "mmio request %#x: no reply %x\n",
526 			request[0], header);
527 		goto out;
528 	}
529 
530 	if (FIELD_GET(GUC_HXG_MSG_0_TYPE, header) == GUC_HXG_TYPE_NO_RESPONSE_BUSY) {
531 #define done ({ header = intel_uncore_read(uncore, guc_send_reg(guc, 0)); \
532 		FIELD_GET(GUC_HXG_MSG_0_ORIGIN, header) != GUC_HXG_ORIGIN_GUC || \
533 		FIELD_GET(GUC_HXG_MSG_0_TYPE, header) != GUC_HXG_TYPE_NO_RESPONSE_BUSY; })
534 
535 		ret = wait_for(done, 1000);
536 		if (unlikely(ret))
537 			goto timeout;
538 		if (unlikely(FIELD_GET(GUC_HXG_MSG_0_ORIGIN, header) !=
539 				       GUC_HXG_ORIGIN_GUC))
540 			goto proto;
541 #undef done
542 	}
543 
544 	if (FIELD_GET(GUC_HXG_MSG_0_TYPE, header) == GUC_HXG_TYPE_NO_RESPONSE_RETRY) {
545 		u32 reason = FIELD_GET(GUC_HXG_RETRY_MSG_0_REASON, header);
546 
547 		guc_dbg(guc, "mmio request %#x: retrying, reason %u\n",
548 			request[0], reason);
549 		goto retry;
550 	}
551 
552 	if (FIELD_GET(GUC_HXG_MSG_0_TYPE, header) == GUC_HXG_TYPE_RESPONSE_FAILURE) {
553 		u32 hint = FIELD_GET(GUC_HXG_FAILURE_MSG_0_HINT, header);
554 		u32 error = FIELD_GET(GUC_HXG_FAILURE_MSG_0_ERROR, header);
555 
556 		guc_err(guc, "mmio request %#x: failure %x/%u\n",
557 			request[0], error, hint);
558 		ret = -ENXIO;
559 		goto out;
560 	}
561 
562 	if (FIELD_GET(GUC_HXG_MSG_0_TYPE, header) != GUC_HXG_TYPE_RESPONSE_SUCCESS) {
563 proto:
564 		guc_err(guc, "mmio request %#x: unexpected reply %#x\n",
565 			request[0], header);
566 		ret = -EPROTO;
567 		goto out;
568 	}
569 
570 	if (response_buf) {
571 		int count = min(response_buf_size, guc->send_regs.count);
572 
573 		GEM_BUG_ON(!count);
574 
575 		response_buf[0] = header;
576 
577 		for (i = 1; i < count; i++)
578 			response_buf[i] = intel_uncore_read(uncore,
579 							    guc_send_reg(guc, i));
580 
581 		/* Use number of copied dwords as our return value */
582 		ret = count;
583 	} else {
584 		/* Use data from the GuC response as our return value */
585 		ret = FIELD_GET(GUC_HXG_RESPONSE_MSG_0_DATA0, header);
586 	}
587 
588 out:
589 	intel_uncore_forcewake_put(uncore, guc->send_regs.fw_domains);
590 	mutex_unlock(&guc->send_mutex);
591 
592 	return ret;
593 }
594 
595 int intel_guc_crash_process_msg(struct intel_guc *guc, u32 action)
596 {
597 	if (action == INTEL_GUC_ACTION_NOTIFY_CRASH_DUMP_POSTED)
598 		guc_err(guc, "Crash dump notification\n");
599 	else if (action == INTEL_GUC_ACTION_NOTIFY_EXCEPTION)
600 		guc_err(guc, "Exception notification\n");
601 	else
602 		guc_err(guc, "Unknown crash notification: 0x%04X\n", action);
603 
604 	queue_work(system_unbound_wq, &guc->dead_guc_worker);
605 
606 	return 0;
607 }
608 
609 int intel_guc_to_host_process_recv_msg(struct intel_guc *guc,
610 				       const u32 *payload, u32 len)
611 {
612 	u32 msg;
613 
614 	if (unlikely(!len))
615 		return -EPROTO;
616 
617 	/* Make sure to handle only enabled messages */
618 	msg = payload[0] & guc->msg_enabled_mask;
619 
620 	if (msg & INTEL_GUC_RECV_MSG_CRASH_DUMP_POSTED)
621 		guc_err(guc, "Received early crash dump notification!\n");
622 	if (msg & INTEL_GUC_RECV_MSG_EXCEPTION)
623 		guc_err(guc, "Received early exception notification!\n");
624 
625 	if (msg & (INTEL_GUC_RECV_MSG_CRASH_DUMP_POSTED | INTEL_GUC_RECV_MSG_EXCEPTION))
626 		queue_work(system_unbound_wq, &guc->dead_guc_worker);
627 
628 	return 0;
629 }
630 
631 /**
632  * intel_guc_auth_huc() - Send action to GuC to authenticate HuC ucode
633  * @guc: intel_guc structure
634  * @rsa_offset: rsa offset w.r.t ggtt base of huc vma
635  *
636  * Triggers a HuC firmware authentication request to the GuC via intel_guc_send
637  * INTEL_GUC_ACTION_AUTHENTICATE_HUC interface. This function is invoked by
638  * intel_huc_auth().
639  *
640  * Return:	non-zero code on error
641  */
642 int intel_guc_auth_huc(struct intel_guc *guc, u32 rsa_offset)
643 {
644 	u32 action[] = {
645 		INTEL_GUC_ACTION_AUTHENTICATE_HUC,
646 		rsa_offset
647 	};
648 
649 	return intel_guc_send(guc, action, ARRAY_SIZE(action));
650 }
651 
652 /**
653  * intel_guc_suspend() - notify GuC entering suspend state
654  * @guc:	the guc
655  */
656 int intel_guc_suspend(struct intel_guc *guc)
657 {
658 	int ret;
659 	u32 action[] = {
660 		INTEL_GUC_ACTION_CLIENT_SOFT_RESET,
661 	};
662 
663 	if (!intel_guc_is_ready(guc))
664 		return 0;
665 
666 	if (intel_guc_submission_is_used(guc)) {
667 		flush_work(&guc->dead_guc_worker);
668 
669 		/*
670 		 * This H2G MMIO command tears down the GuC in two steps. First it will
671 		 * generate a G2H CTB for every active context indicating a reset. In
672 		 * practice the i915 shouldn't ever get a G2H as suspend should only be
673 		 * called when the GPU is idle. Next, it tears down the CTBs and this
674 		 * H2G MMIO command completes.
675 		 *
676 		 * Don't abort on a failure code from the GuC. Keep going and do the
677 		 * clean up in santize() and re-initialisation on resume and hopefully
678 		 * the error here won't be problematic.
679 		 */
680 		ret = intel_guc_send_mmio(guc, action, ARRAY_SIZE(action), NULL, 0);
681 		if (ret)
682 			guc_err(guc, "suspend: RESET_CLIENT action failed with %pe\n",
683 				ERR_PTR(ret));
684 	}
685 
686 	/* Signal that the GuC isn't running. */
687 	intel_guc_sanitize(guc);
688 
689 	return 0;
690 }
691 
692 /**
693  * intel_guc_resume() - notify GuC resuming from suspend state
694  * @guc:	the guc
695  */
696 int intel_guc_resume(struct intel_guc *guc)
697 {
698 	/*
699 	 * NB: This function can still be called even if GuC submission is
700 	 * disabled, e.g. if GuC is enabled for HuC authentication only. Thus,
701 	 * if any code is later added here, it must be support doing nothing
702 	 * if submission is disabled (as per intel_guc_suspend).
703 	 */
704 	return 0;
705 }
706 
707 /**
708  * DOC: GuC Memory Management
709  *
710  * GuC can't allocate any memory for its own usage, so all the allocations must
711  * be handled by the host driver. GuC accesses the memory via the GGTT, with the
712  * exception of the top and bottom parts of the 4GB address space, which are
713  * instead re-mapped by the GuC HW to memory location of the FW itself (WOPCM)
714  * or other parts of the HW. The driver must take care not to place objects that
715  * the GuC is going to access in these reserved ranges. The layout of the GuC
716  * address space is shown below:
717  *
718  * ::
719  *
720  *     +===========> +====================+ <== FFFF_FFFF
721  *     ^             |      Reserved      |
722  *     |             +====================+ <== GUC_GGTT_TOP
723  *     |             |                    |
724  *     |             |        DRAM        |
725  *    GuC            |                    |
726  *  Address    +===> +====================+ <== GuC ggtt_pin_bias
727  *   Space     ^     |                    |
728  *     |       |     |                    |
729  *     |      GuC    |        GuC         |
730  *     |     WOPCM   |       WOPCM        |
731  *     |      Size   |                    |
732  *     |       |     |                    |
733  *     v       v     |                    |
734  *     +=======+===> +====================+ <== 0000_0000
735  *
736  * The lower part of GuC Address Space [0, ggtt_pin_bias) is mapped to GuC WOPCM
737  * while upper part of GuC Address Space [ggtt_pin_bias, GUC_GGTT_TOP) is mapped
738  * to DRAM. The value of the GuC ggtt_pin_bias is the GuC WOPCM size.
739  */
740 
741 /**
742  * intel_guc_allocate_vma() - Allocate a GGTT VMA for GuC usage
743  * @guc:	the guc
744  * @size:	size of area to allocate (both virtual space and memory)
745  *
746  * This is a wrapper to create an object for use with the GuC. In order to
747  * use it inside the GuC, an object needs to be pinned lifetime, so we allocate
748  * both some backing storage and a range inside the Global GTT. We must pin
749  * it in the GGTT somewhere other than than [0, GUC ggtt_pin_bias) because that
750  * range is reserved inside GuC.
751  *
752  * Return:	A i915_vma if successful, otherwise an ERR_PTR.
753  */
754 struct i915_vma *intel_guc_allocate_vma(struct intel_guc *guc, u32 size)
755 {
756 	struct intel_gt *gt = guc_to_gt(guc);
757 	struct drm_i915_gem_object *obj;
758 	struct i915_vma *vma;
759 	u64 flags;
760 	int ret;
761 
762 	if (HAS_LMEM(gt->i915))
763 		obj = i915_gem_object_create_lmem(gt->i915, size,
764 						  I915_BO_ALLOC_CPU_CLEAR |
765 						  I915_BO_ALLOC_CONTIGUOUS |
766 						  I915_BO_ALLOC_PM_EARLY);
767 	else
768 		obj = i915_gem_object_create_shmem(gt->i915, size);
769 
770 	if (IS_ERR(obj))
771 		return ERR_CAST(obj);
772 
773 	/*
774 	 * Wa_22016122933: For Media version 13.0, all Media GT shared
775 	 * memory needs to be mapped as WC on CPU side and UC (PAT
776 	 * index 2) on GPU side.
777 	 */
778 	if (intel_gt_needs_wa_22016122933(gt))
779 		i915_gem_object_set_cache_coherency(obj, I915_CACHE_NONE);
780 
781 	vma = i915_vma_instance(obj, &gt->ggtt->vm, NULL);
782 	if (IS_ERR(vma))
783 		goto err;
784 
785 	flags = PIN_OFFSET_BIAS | i915_ggtt_pin_bias(vma);
786 	ret = i915_ggtt_pin(vma, NULL, 0, flags);
787 	if (ret) {
788 		vma = ERR_PTR(ret);
789 		goto err;
790 	}
791 
792 	return i915_vma_make_unshrinkable(vma);
793 
794 err:
795 	i915_gem_object_put(obj);
796 	return vma;
797 }
798 
799 /**
800  * intel_guc_allocate_and_map_vma() - Allocate and map VMA for GuC usage
801  * @guc:	the guc
802  * @size:	size of area to allocate (both virtual space and memory)
803  * @out_vma:	return variable for the allocated vma pointer
804  * @out_vaddr:	return variable for the obj mapping
805  *
806  * This wrapper calls intel_guc_allocate_vma() and then maps the allocated
807  * object with I915_MAP_WB.
808  *
809  * Return:	0 if successful, a negative errno code otherwise.
810  */
811 int intel_guc_allocate_and_map_vma(struct intel_guc *guc, u32 size,
812 				   struct i915_vma **out_vma, void **out_vaddr)
813 {
814 	struct i915_vma *vma;
815 	void *vaddr;
816 
817 	vma = intel_guc_allocate_vma(guc, size);
818 	if (IS_ERR(vma))
819 		return PTR_ERR(vma);
820 
821 	vaddr = i915_gem_object_pin_map_unlocked(vma->obj,
822 						 intel_gt_coherent_map_type(guc_to_gt(guc),
823 									    vma->obj, true));
824 	if (IS_ERR(vaddr)) {
825 		i915_vma_unpin_and_release(&vma, 0);
826 		return PTR_ERR(vaddr);
827 	}
828 
829 	*out_vma = vma;
830 	*out_vaddr = vaddr;
831 
832 	return 0;
833 }
834 
835 static int __guc_action_self_cfg(struct intel_guc *guc, u16 key, u16 len, u64 value)
836 {
837 	u32 request[HOST2GUC_SELF_CFG_REQUEST_MSG_LEN] = {
838 		FIELD_PREP(GUC_HXG_MSG_0_ORIGIN, GUC_HXG_ORIGIN_HOST) |
839 		FIELD_PREP(GUC_HXG_MSG_0_TYPE, GUC_HXG_TYPE_REQUEST) |
840 		FIELD_PREP(GUC_HXG_REQUEST_MSG_0_ACTION, GUC_ACTION_HOST2GUC_SELF_CFG),
841 		FIELD_PREP(HOST2GUC_SELF_CFG_REQUEST_MSG_1_KLV_KEY, key) |
842 		FIELD_PREP(HOST2GUC_SELF_CFG_REQUEST_MSG_1_KLV_LEN, len),
843 		FIELD_PREP(HOST2GUC_SELF_CFG_REQUEST_MSG_2_VALUE32, lower_32_bits(value)),
844 		FIELD_PREP(HOST2GUC_SELF_CFG_REQUEST_MSG_3_VALUE64, upper_32_bits(value)),
845 	};
846 	int ret;
847 
848 	GEM_BUG_ON(len > 2);
849 	GEM_BUG_ON(len == 1 && upper_32_bits(value));
850 
851 	/* Self config must go over MMIO */
852 	ret = intel_guc_send_mmio(guc, request, ARRAY_SIZE(request), NULL, 0);
853 
854 	if (unlikely(ret < 0))
855 		return ret;
856 	if (unlikely(ret > 1))
857 		return -EPROTO;
858 	if (unlikely(!ret))
859 		return -ENOKEY;
860 
861 	return 0;
862 }
863 
864 static int __guc_self_cfg(struct intel_guc *guc, u16 key, u16 len, u64 value)
865 {
866 	int err = __guc_action_self_cfg(guc, key, len, value);
867 
868 	if (unlikely(err))
869 		guc_probe_error(guc, "Unsuccessful self-config (%pe) key %#hx value %#llx\n",
870 				ERR_PTR(err), key, value);
871 	return err;
872 }
873 
874 int intel_guc_self_cfg32(struct intel_guc *guc, u16 key, u32 value)
875 {
876 	return __guc_self_cfg(guc, key, 1, value);
877 }
878 
879 int intel_guc_self_cfg64(struct intel_guc *guc, u16 key, u64 value)
880 {
881 	return __guc_self_cfg(guc, key, 2, value);
882 }
883 
884 /**
885  * intel_guc_load_status - dump information about GuC load status
886  * @guc: the GuC
887  * @p: the &drm_printer
888  *
889  * Pretty printer for GuC load status.
890  */
891 void intel_guc_load_status(struct intel_guc *guc, struct drm_printer *p)
892 {
893 	struct intel_gt *gt = guc_to_gt(guc);
894 	struct intel_uncore *uncore = gt->uncore;
895 	intel_wakeref_t wakeref;
896 
897 	if (!intel_guc_is_supported(guc)) {
898 		drm_printf(p, "GuC not supported\n");
899 		return;
900 	}
901 
902 	if (!intel_guc_is_wanted(guc)) {
903 		drm_printf(p, "GuC disabled\n");
904 		return;
905 	}
906 
907 	intel_uc_fw_dump(&guc->fw, p);
908 
909 	with_intel_runtime_pm(uncore->rpm, wakeref) {
910 		u32 status = intel_uncore_read(uncore, GUC_STATUS);
911 		u32 i;
912 
913 		drm_printf(p, "GuC status 0x%08x:\n", status);
914 		drm_printf(p, "\tBootrom status = 0x%x\n",
915 			   (status & GS_BOOTROM_MASK) >> GS_BOOTROM_SHIFT);
916 		drm_printf(p, "\tuKernel status = 0x%x\n",
917 			   (status & GS_UKERNEL_MASK) >> GS_UKERNEL_SHIFT);
918 		drm_printf(p, "\tMIA Core status = 0x%x\n",
919 			   (status & GS_MIA_MASK) >> GS_MIA_SHIFT);
920 		drm_puts(p, "Scratch registers:\n");
921 		for (i = 0; i < 16; i++) {
922 			drm_printf(p, "\t%2d: \t0x%x\n",
923 				   i, intel_uncore_read(uncore, SOFT_SCRATCH(i)));
924 		}
925 	}
926 }
927 
928 void intel_guc_write_barrier(struct intel_guc *guc)
929 {
930 	struct intel_gt *gt = guc_to_gt(guc);
931 
932 	if (i915_gem_object_is_lmem(guc->ct.vma->obj)) {
933 		/*
934 		 * Ensure intel_uncore_write_fw can be used rather than
935 		 * intel_uncore_write.
936 		 */
937 		GEM_BUG_ON(guc->send_regs.fw_domains);
938 
939 		/*
940 		 * This register is used by the i915 and GuC for MMIO based
941 		 * communication. Once we are in this code CTBs are the only
942 		 * method the i915 uses to communicate with the GuC so it is
943 		 * safe to write to this register (a value of 0 is NOP for MMIO
944 		 * communication). If we ever start mixing CTBs and MMIOs a new
945 		 * register will have to be chosen. This function is also used
946 		 * to enforce ordering of a work queue item write and an update
947 		 * to the process descriptor. When a work queue is being used,
948 		 * CTBs are also the only mechanism of communication.
949 		 */
950 		intel_uncore_write_fw(gt->uncore, GEN11_SOFT_SCRATCH(0), 0);
951 	} else {
952 		/* wmb() sufficient for a barrier if in smem */
953 		wmb();
954 	}
955 }
956