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