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