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