xref: /openbmc/linux/drivers/gpu/drm/i915/gt/uc/intel_guc.h (revision 6db6b729)
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Copyright © 2014-2019 Intel Corporation
4  */
5 
6 #ifndef _INTEL_GUC_H_
7 #define _INTEL_GUC_H_
8 
9 #include <linux/delay.h>
10 #include <linux/iosys-map.h>
11 #include <linux/xarray.h>
12 
13 #include "intel_guc_ct.h"
14 #include "intel_guc_fw.h"
15 #include "intel_guc_fwif.h"
16 #include "intel_guc_log.h"
17 #include "intel_guc_reg.h"
18 #include "intel_guc_slpc_types.h"
19 #include "intel_uc_fw.h"
20 #include "intel_uncore.h"
21 #include "i915_utils.h"
22 #include "i915_vma.h"
23 
24 struct __guc_ads_blob;
25 struct intel_guc_state_capture;
26 
27 /**
28  * struct intel_guc - Top level structure of GuC.
29  *
30  * It handles firmware loading and manages client pool. intel_guc owns an
31  * i915_sched_engine for submission.
32  */
33 struct intel_guc {
34 	/** @fw: the GuC firmware */
35 	struct intel_uc_fw fw;
36 	/** @log: sub-structure containing GuC log related data and objects */
37 	struct intel_guc_log log;
38 	/** @ct: the command transport communication channel */
39 	struct intel_guc_ct ct;
40 	/** @slpc: sub-structure containing SLPC related data and objects */
41 	struct intel_guc_slpc slpc;
42 	/** @capture: the error-state-capture module's data and objects */
43 	struct intel_guc_state_capture *capture;
44 
45 	/** @dbgfs_node: debugfs node */
46 	struct dentry *dbgfs_node;
47 
48 	/** @sched_engine: Global engine used to submit requests to GuC */
49 	struct i915_sched_engine *sched_engine;
50 	/**
51 	 * @stalled_request: if GuC can't process a request for any reason, we
52 	 * save it until GuC restarts processing. No other request can be
53 	 * submitted until the stalled request is processed.
54 	 */
55 	struct i915_request *stalled_request;
56 	/**
57 	 * @submission_stall_reason: reason why submission is stalled
58 	 */
59 	enum {
60 		STALL_NONE,
61 		STALL_REGISTER_CONTEXT,
62 		STALL_MOVE_LRC_TAIL,
63 		STALL_ADD_REQUEST,
64 	} submission_stall_reason;
65 
66 	/* intel_guc_recv interrupt related state */
67 	/** @irq_lock: protects GuC irq state */
68 	spinlock_t irq_lock;
69 	/**
70 	 * @msg_enabled_mask: mask of events that are processed when receiving
71 	 * an INTEL_GUC_ACTION_DEFAULT G2H message.
72 	 */
73 	unsigned int msg_enabled_mask;
74 
75 	/**
76 	 * @outstanding_submission_g2h: number of outstanding GuC to Host
77 	 * responses related to GuC submission, used to determine if the GT is
78 	 * idle
79 	 */
80 	atomic_t outstanding_submission_g2h;
81 
82 	/** @interrupts: pointers to GuC interrupt-managing functions. */
83 	struct {
84 		bool enabled;
85 		void (*reset)(struct intel_guc *guc);
86 		void (*enable)(struct intel_guc *guc);
87 		void (*disable)(struct intel_guc *guc);
88 	} interrupts;
89 
90 	/**
91 	 * @submission_state: sub-structure for submission state protected by
92 	 * single lock
93 	 */
94 	struct {
95 		/**
96 		 * @lock: protects everything in submission_state,
97 		 * ce->guc_id.id, and ce->guc_id.ref when transitioning in and
98 		 * out of zero
99 		 */
100 		spinlock_t lock;
101 		/**
102 		 * @guc_ids: used to allocate new guc_ids, single-lrc
103 		 */
104 		struct ida guc_ids;
105 		/**
106 		 * @num_guc_ids: Number of guc_ids, selftest feature to be able
107 		 * to reduce this number while testing.
108 		 */
109 		int num_guc_ids;
110 		/**
111 		 * @guc_ids_bitmap: used to allocate new guc_ids, multi-lrc
112 		 */
113 		unsigned long *guc_ids_bitmap;
114 		/**
115 		 * @guc_id_list: list of intel_context with valid guc_ids but no
116 		 * refs
117 		 */
118 		struct list_head guc_id_list;
119 		/**
120 		 * @guc_ids_in_use: Number single-lrc guc_ids in use
121 		 */
122 		unsigned int guc_ids_in_use;
123 		/**
124 		 * @destroyed_contexts: list of contexts waiting to be destroyed
125 		 * (deregistered with the GuC)
126 		 */
127 		struct list_head destroyed_contexts;
128 		/**
129 		 * @destroyed_worker: worker to deregister contexts, need as we
130 		 * need to take a GT PM reference and can't from destroy
131 		 * function as it might be in an atomic context (no sleeping)
132 		 */
133 		struct work_struct destroyed_worker;
134 		/**
135 		 * @reset_fail_worker: worker to trigger a GT reset after an
136 		 * engine reset fails
137 		 */
138 		struct work_struct reset_fail_worker;
139 		/**
140 		 * @reset_fail_mask: mask of engines that failed to reset
141 		 */
142 		intel_engine_mask_t reset_fail_mask;
143 		/**
144 		 * @sched_disable_delay_ms: schedule disable delay, in ms, for
145 		 * contexts
146 		 */
147 		unsigned int sched_disable_delay_ms;
148 		/**
149 		 * @sched_disable_gucid_threshold: threshold of min remaining available
150 		 * guc_ids before we start bypassing the schedule disable delay
151 		 */
152 		unsigned int sched_disable_gucid_threshold;
153 	} submission_state;
154 
155 	/**
156 	 * @submission_supported: tracks whether we support GuC submission on
157 	 * the current platform
158 	 */
159 	bool submission_supported;
160 	/** @submission_selected: tracks whether the user enabled GuC submission */
161 	bool submission_selected;
162 	/** @submission_initialized: tracks whether GuC submission has been initialised */
163 	bool submission_initialized;
164 	/** @submission_version: Submission API version of the currently loaded firmware */
165 	struct intel_uc_fw_ver submission_version;
166 
167 	/**
168 	 * @rc_supported: tracks whether we support GuC rc on the current platform
169 	 */
170 	bool rc_supported;
171 	/** @rc_selected: tracks whether the user enabled GuC rc */
172 	bool rc_selected;
173 
174 	/** @ads_vma: object allocated to hold the GuC ADS */
175 	struct i915_vma *ads_vma;
176 	/** @ads_map: contents of the GuC ADS */
177 	struct iosys_map ads_map;
178 	/** @ads_regset_size: size of the save/restore regsets in the ADS */
179 	u32 ads_regset_size;
180 	/**
181 	 * @ads_regset_count: number of save/restore registers in the ADS for
182 	 * each engine
183 	 */
184 	u32 ads_regset_count[I915_NUM_ENGINES];
185 	/** @ads_regset: save/restore regsets in the ADS */
186 	struct guc_mmio_reg *ads_regset;
187 	/** @ads_golden_ctxt_size: size of the golden contexts in the ADS */
188 	u32 ads_golden_ctxt_size;
189 	/** @ads_capture_size: size of register lists in the ADS used for error capture */
190 	u32 ads_capture_size;
191 	/** @ads_engine_usage_size: size of engine usage in the ADS */
192 	u32 ads_engine_usage_size;
193 
194 	/** @lrc_desc_pool_v69: object allocated to hold the GuC LRC descriptor pool */
195 	struct i915_vma *lrc_desc_pool_v69;
196 	/** @lrc_desc_pool_vaddr_v69: contents of the GuC LRC descriptor pool */
197 	void *lrc_desc_pool_vaddr_v69;
198 
199 	/**
200 	 * @context_lookup: used to resolve intel_context from guc_id, if a
201 	 * context is present in this structure it is registered with the GuC
202 	 */
203 	struct xarray context_lookup;
204 
205 	/** @params: Control params for fw initialization */
206 	u32 params[GUC_CTL_MAX_DWORDS];
207 
208 	/** @send_regs: GuC's FW specific registers used for sending MMIO H2G */
209 	struct {
210 		u32 base;
211 		unsigned int count;
212 		enum forcewake_domains fw_domains;
213 	} send_regs;
214 
215 	/** @notify_reg: register used to send interrupts to the GuC FW */
216 	i915_reg_t notify_reg;
217 
218 	/**
219 	 * @mmio_msg: notification bitmask that the GuC writes in one of its
220 	 * registers when the CT channel is disabled, to be processed when the
221 	 * channel is back up.
222 	 */
223 	u32 mmio_msg;
224 
225 	/** @send_mutex: used to serialize the intel_guc_send actions */
226 	struct mutex send_mutex;
227 
228 	/**
229 	 * @timestamp: GT timestamp object that stores a copy of the timestamp
230 	 * and adjusts it for overflow using a worker.
231 	 */
232 	struct {
233 		/**
234 		 * @lock: Lock protecting the below fields and the engine stats.
235 		 */
236 		spinlock_t lock;
237 
238 		/**
239 		 * @gt_stamp: 64 bit extended value of the GT timestamp.
240 		 */
241 		u64 gt_stamp;
242 
243 		/**
244 		 * @ping_delay: Period for polling the GT timestamp for
245 		 * overflow.
246 		 */
247 		unsigned long ping_delay;
248 
249 		/**
250 		 * @work: Periodic work to adjust GT timestamp, engine and
251 		 * context usage for overflows.
252 		 */
253 		struct delayed_work work;
254 
255 		/**
256 		 * @shift: Right shift value for the gpm timestamp
257 		 */
258 		u32 shift;
259 
260 		/**
261 		 * @last_stat_jiffies: jiffies at last actual stats collection time
262 		 * We use this timestamp to ensure we don't oversample the
263 		 * stats because runtime power management events can trigger
264 		 * stats collection at much higher rates than required.
265 		 */
266 		unsigned long last_stat_jiffies;
267 	} timestamp;
268 
269 #ifdef CONFIG_DRM_I915_SELFTEST
270 	/**
271 	 * @number_guc_id_stolen: The number of guc_ids that have been stolen
272 	 */
273 	int number_guc_id_stolen;
274 #endif
275 };
276 
277 /*
278  * GuC version number components are only 8-bit, so converting to a 32bit 8.8.8
279  * integer works.
280  */
281 #define MAKE_GUC_VER(maj, min, pat)	(((maj) << 16) | ((min) << 8) | (pat))
282 #define MAKE_GUC_VER_STRUCT(ver)	MAKE_GUC_VER((ver).major, (ver).minor, (ver).patch)
283 #define GUC_SUBMIT_VER(guc)		MAKE_GUC_VER_STRUCT((guc)->submission_version)
284 
285 static inline struct intel_guc *log_to_guc(struct intel_guc_log *log)
286 {
287 	return container_of(log, struct intel_guc, log);
288 }
289 
290 static
291 inline int intel_guc_send(struct intel_guc *guc, const u32 *action, u32 len)
292 {
293 	return intel_guc_ct_send(&guc->ct, action, len, NULL, 0, 0);
294 }
295 
296 static
297 inline int intel_guc_send_nb(struct intel_guc *guc, const u32 *action, u32 len,
298 			     u32 g2h_len_dw)
299 {
300 	return intel_guc_ct_send(&guc->ct, action, len, NULL, 0,
301 				 MAKE_SEND_FLAGS(g2h_len_dw));
302 }
303 
304 static inline int
305 intel_guc_send_and_receive(struct intel_guc *guc, const u32 *action, u32 len,
306 			   u32 *response_buf, u32 response_buf_size)
307 {
308 	return intel_guc_ct_send(&guc->ct, action, len,
309 				 response_buf, response_buf_size, 0);
310 }
311 
312 static inline int intel_guc_send_busy_loop(struct intel_guc *guc,
313 					   const u32 *action,
314 					   u32 len,
315 					   u32 g2h_len_dw,
316 					   bool loop)
317 {
318 	int err;
319 	unsigned int sleep_period_ms = 1;
320 	bool not_atomic = !in_atomic() && !irqs_disabled();
321 
322 	/*
323 	 * FIXME: Have caller pass in if we are in an atomic context to avoid
324 	 * using in_atomic(). It is likely safe here as we check for irqs
325 	 * disabled which basically all the spin locks in the i915 do but
326 	 * regardless this should be cleaned up.
327 	 */
328 
329 	/* No sleeping with spin locks, just busy loop */
330 	might_sleep_if(loop && not_atomic);
331 
332 retry:
333 	err = intel_guc_send_nb(guc, action, len, g2h_len_dw);
334 	if (unlikely(err == -EBUSY && loop)) {
335 		if (likely(not_atomic)) {
336 			if (msleep_interruptible(sleep_period_ms))
337 				return -EINTR;
338 			sleep_period_ms = sleep_period_ms << 1;
339 		} else {
340 			cpu_relax();
341 		}
342 		goto retry;
343 	}
344 
345 	return err;
346 }
347 
348 /* Only call this from the interrupt handler code */
349 static inline void intel_guc_to_host_event_handler(struct intel_guc *guc)
350 {
351 	if (guc->interrupts.enabled)
352 		intel_guc_ct_event_handler(&guc->ct);
353 }
354 
355 /* GuC addresses above GUC_GGTT_TOP also don't map through the GTT */
356 #define GUC_GGTT_TOP	0xFEE00000
357 
358 /**
359  * intel_guc_ggtt_offset() - Get and validate the GGTT offset of @vma
360  * @guc: intel_guc structure.
361  * @vma: i915 graphics virtual memory area.
362  *
363  * GuC does not allow any gfx GGTT address that falls into range
364  * [0, ggtt.pin_bias), which is reserved for Boot ROM, SRAM and WOPCM.
365  * Currently, in order to exclude [0, ggtt.pin_bias) address space from
366  * GGTT, all gfx objects used by GuC are allocated with intel_guc_allocate_vma()
367  * and pinned with PIN_OFFSET_BIAS along with the value of ggtt.pin_bias.
368  *
369  * Return: GGTT offset of the @vma.
370  */
371 static inline u32 intel_guc_ggtt_offset(struct intel_guc *guc,
372 					struct i915_vma *vma)
373 {
374 	u32 offset = i915_ggtt_offset(vma);
375 
376 	GEM_BUG_ON(offset < i915_ggtt_pin_bias(vma));
377 	GEM_BUG_ON(range_overflows_t(u64, offset, vma->size, GUC_GGTT_TOP));
378 
379 	return offset;
380 }
381 
382 void intel_guc_init_early(struct intel_guc *guc);
383 void intel_guc_init_late(struct intel_guc *guc);
384 void intel_guc_init_send_regs(struct intel_guc *guc);
385 void intel_guc_write_params(struct intel_guc *guc);
386 int intel_guc_init(struct intel_guc *guc);
387 void intel_guc_fini(struct intel_guc *guc);
388 void intel_guc_notify(struct intel_guc *guc);
389 int intel_guc_send_mmio(struct intel_guc *guc, const u32 *action, u32 len,
390 			u32 *response_buf, u32 response_buf_size);
391 int intel_guc_to_host_process_recv_msg(struct intel_guc *guc,
392 				       const u32 *payload, u32 len);
393 int intel_guc_auth_huc(struct intel_guc *guc, u32 rsa_offset);
394 int intel_guc_suspend(struct intel_guc *guc);
395 int intel_guc_resume(struct intel_guc *guc);
396 struct i915_vma *intel_guc_allocate_vma(struct intel_guc *guc, u32 size);
397 int intel_guc_allocate_and_map_vma(struct intel_guc *guc, u32 size,
398 				   struct i915_vma **out_vma, void **out_vaddr);
399 int intel_guc_self_cfg32(struct intel_guc *guc, u16 key, u32 value);
400 int intel_guc_self_cfg64(struct intel_guc *guc, u16 key, u64 value);
401 
402 static inline bool intel_guc_is_supported(struct intel_guc *guc)
403 {
404 	return intel_uc_fw_is_supported(&guc->fw);
405 }
406 
407 static inline bool intel_guc_is_wanted(struct intel_guc *guc)
408 {
409 	return intel_uc_fw_is_enabled(&guc->fw);
410 }
411 
412 static inline bool intel_guc_is_used(struct intel_guc *guc)
413 {
414 	GEM_BUG_ON(__intel_uc_fw_status(&guc->fw) == INTEL_UC_FIRMWARE_SELECTED);
415 	return intel_uc_fw_is_available(&guc->fw);
416 }
417 
418 static inline bool intel_guc_is_fw_running(struct intel_guc *guc)
419 {
420 	return intel_uc_fw_is_running(&guc->fw);
421 }
422 
423 static inline bool intel_guc_is_ready(struct intel_guc *guc)
424 {
425 	return intel_guc_is_fw_running(guc) && intel_guc_ct_enabled(&guc->ct);
426 }
427 
428 static inline void intel_guc_reset_interrupts(struct intel_guc *guc)
429 {
430 	guc->interrupts.reset(guc);
431 }
432 
433 static inline void intel_guc_enable_interrupts(struct intel_guc *guc)
434 {
435 	guc->interrupts.enable(guc);
436 }
437 
438 static inline void intel_guc_disable_interrupts(struct intel_guc *guc)
439 {
440 	guc->interrupts.disable(guc);
441 }
442 
443 static inline int intel_guc_sanitize(struct intel_guc *guc)
444 {
445 	intel_uc_fw_sanitize(&guc->fw);
446 	intel_guc_disable_interrupts(guc);
447 	intel_guc_ct_sanitize(&guc->ct);
448 	guc->mmio_msg = 0;
449 
450 	return 0;
451 }
452 
453 static inline void intel_guc_enable_msg(struct intel_guc *guc, u32 mask)
454 {
455 	spin_lock_irq(&guc->irq_lock);
456 	guc->msg_enabled_mask |= mask;
457 	spin_unlock_irq(&guc->irq_lock);
458 }
459 
460 static inline void intel_guc_disable_msg(struct intel_guc *guc, u32 mask)
461 {
462 	spin_lock_irq(&guc->irq_lock);
463 	guc->msg_enabled_mask &= ~mask;
464 	spin_unlock_irq(&guc->irq_lock);
465 }
466 
467 int intel_guc_wait_for_idle(struct intel_guc *guc, long timeout);
468 
469 int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
470 					  const u32 *msg, u32 len);
471 int intel_guc_sched_done_process_msg(struct intel_guc *guc,
472 				     const u32 *msg, u32 len);
473 int intel_guc_context_reset_process_msg(struct intel_guc *guc,
474 					const u32 *msg, u32 len);
475 int intel_guc_engine_failure_process_msg(struct intel_guc *guc,
476 					 const u32 *msg, u32 len);
477 int intel_guc_error_capture_process_msg(struct intel_guc *guc,
478 					const u32 *msg, u32 len);
479 
480 struct intel_engine_cs *
481 intel_guc_lookup_engine(struct intel_guc *guc, u8 guc_class, u8 instance);
482 
483 void intel_guc_find_hung_context(struct intel_engine_cs *engine);
484 
485 int intel_guc_global_policies_update(struct intel_guc *guc);
486 
487 void intel_guc_context_ban(struct intel_context *ce, struct i915_request *rq);
488 
489 void intel_guc_submission_reset_prepare(struct intel_guc *guc);
490 void intel_guc_submission_reset(struct intel_guc *guc, intel_engine_mask_t stalled);
491 void intel_guc_submission_reset_finish(struct intel_guc *guc);
492 void intel_guc_submission_cancel_requests(struct intel_guc *guc);
493 
494 void intel_guc_load_status(struct intel_guc *guc, struct drm_printer *p);
495 
496 void intel_guc_write_barrier(struct intel_guc *guc);
497 
498 void intel_guc_dump_time_info(struct intel_guc *guc, struct drm_printer *p);
499 
500 int intel_guc_sched_disable_gucid_threshold_max(struct intel_guc *guc);
501 
502 #endif
503