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