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