intel_guc.h 11.6 KB
Newer Older
1
/* SPDX-License-Identifier: MIT */
2
/*
3
 * Copyright © 2014-2019 Intel Corporation
4 5 6 7 8
 */

#ifndef _INTEL_GUC_H_
#define _INTEL_GUC_H_

9
#include <linux/xarray.h>
10
#include <linux/delay.h>
11

12
#include "intel_uncore.h"
13
#include "intel_guc_fw.h"
14 15 16
#include "intel_guc_fwif.h"
#include "intel_guc_ct.h"
#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 "i915_utils.h"
21 22
#include "i915_vma.h"

23 24
struct __guc_ads_blob;

M
Matthew Brost 已提交
25 26 27 28 29
/**
 * struct intel_guc - Top level structure of GuC.
 *
 * It handles firmware loading and manages client pool. intel_guc owns an
 * i915_sched_engine for submission.
30
 */
31
struct intel_guc {
M
Matthew Brost 已提交
32
	/** @fw: the GuC firmware */
33
	struct intel_uc_fw fw;
M
Matthew Brost 已提交
34
	/** @log: sub-structure containing GuC log related data and objects */
35
	struct intel_guc_log log;
M
Matthew Brost 已提交
36
	/** @ct: the command transport communication channel */
37
	struct intel_guc_ct ct;
M
Matthew Brost 已提交
38
	/** @slpc: sub-structure containing SLPC related data and objects */
39
	struct intel_guc_slpc slpc;
40

M
Matthew Brost 已提交
41
	/** @sched_engine: Global engine used to submit requests to GuC */
42
	struct i915_sched_engine *sched_engine;
M
Matthew Brost 已提交
43 44 45 46 47
	/**
	 * @stalled_request: if GuC can't process a request for any reason, we
	 * save it until GuC restarts processing. No other request can be
	 * submitted until the stalled request is processed.
	 */
48
	struct i915_request *stalled_request;
49 50 51 52 53 54 55 56 57
	/**
	 * @submission_stall_reason: reason why submission is stalled
	 */
	enum {
		STALL_NONE,
		STALL_REGISTER_CONTEXT,
		STALL_MOVE_LRC_TAIL,
		STALL_ADD_REQUEST,
	} submission_stall_reason;
58

59
	/* intel_guc_recv interrupt related state */
M
Matthew Brost 已提交
60
	/** @irq_lock: protects GuC irq state */
61
	spinlock_t irq_lock;
M
Matthew Brost 已提交
62 63 64 65
	/**
	 * @msg_enabled_mask: mask of events that are processed when receiving
	 * an INTEL_GUC_ACTION_DEFAULT G2H message.
	 */
66
	unsigned int msg_enabled_mask;
67

M
Matthew Brost 已提交
68 69 70 71 72
	/**
	 * @outstanding_submission_g2h: number of outstanding GuC to Host
	 * responses related to GuC submission, used to determine if the GT is
	 * idle
	 */
73 74
	atomic_t outstanding_submission_g2h;

M
Matthew Brost 已提交
75
	/** @interrupts: pointers to GuC interrupt-managing functions. */
76
	struct {
77 78 79
		void (*reset)(struct intel_guc *guc);
		void (*enable)(struct intel_guc *guc);
		void (*disable)(struct intel_guc *guc);
80 81
	} interrupts;

M
Matthew Brost 已提交
82
	/**
83 84
	 * @submission_state: sub-structure for submission state protected by
	 * single lock
85
	 */
86 87 88 89 90 91 92 93
	struct {
		/**
		 * @lock: protects everything in submission_state,
		 * ce->guc_id.id, and ce->guc_id.ref when transitioning in and
		 * out of zero
		 */
		spinlock_t lock;
		/**
94
		 * @guc_ids: used to allocate new guc_ids, single-lrc
95 96
		 */
		struct ida guc_ids;
97 98 99 100
		/**
		 * @guc_ids_bitmap: used to allocate new guc_ids, multi-lrc
		 */
		unsigned long *guc_ids_bitmap;
101 102 103 104 105
		/**
		 * @guc_id_list: list of intel_context with valid guc_ids but no
		 * refs
		 */
		struct list_head guc_id_list;
106 107 108 109 110 111 112 113 114 115 116
		/**
		 * @destroyed_contexts: list of contexts waiting to be destroyed
		 * (deregistered with the GuC)
		 */
		struct list_head destroyed_contexts;
		/**
		 * @destroyed_worker: worker to deregister contexts, need as we
		 * need to take a GT PM reference and can't from destroy
		 * function as it might be in an atomic context (no sleeping)
		 */
		struct work_struct destroyed_worker;
117
	} submission_state;
118

M
Matthew Brost 已提交
119 120 121 122
	/**
	 * @submission_supported: tracks whether we support GuC submission on
	 * the current platform
	 */
123
	bool submission_supported;
M
Matthew Brost 已提交
124
	/** @submission_selected: tracks whether the user enabled GuC submission */
125
	bool submission_selected;
M
Matthew Brost 已提交
126 127 128
	/**
	 * @rc_supported: tracks whether we support GuC rc on the current platform
	 */
129
	bool rc_supported;
M
Matthew Brost 已提交
130
	/** @rc_selected: tracks whether the user enabled GuC rc */
131
	bool rc_selected;
132

M
Matthew Brost 已提交
133
	/** @ads_vma: object allocated to hold the GuC ADS */
134
	struct i915_vma *ads_vma;
M
Matthew Brost 已提交
135
	/** @ads_blob: contents of the GuC ADS */
136
	struct __guc_ads_blob *ads_blob;
M
Matthew Brost 已提交
137
	/** @ads_regset_size: size of the save/restore regsets in the ADS */
138
	u32 ads_regset_size;
M
Matthew Brost 已提交
139
	/** @ads_golden_ctxt_size: size of the golden contexts in the ADS */
140
	u32 ads_golden_ctxt_size;
141 142
	/** @ads_engine_usage_size: size of engine usage in the ADS */
	u32 ads_engine_usage_size;
143

M
Matthew Brost 已提交
144
	/** @lrc_desc_pool: object allocated to hold the GuC LRC descriptor pool */
145
	struct i915_vma *lrc_desc_pool;
M
Matthew Brost 已提交
146
	/** @lrc_desc_pool_vaddr: contents of the GuC LRC descriptor pool */
147
	void *lrc_desc_pool_vaddr;
148

M
Matthew Brost 已提交
149 150 151 152
	/**
	 * @context_lookup: used to resolve intel_context from guc_id, if a
	 * context is present in this structure it is registered with the GuC
	 */
153 154
	struct xarray context_lookup;

M
Matthew Brost 已提交
155
	/** @params: Control params for fw initialization */
156 157
	u32 params[GUC_CTL_MAX_DWORDS];

M
Matthew Brost 已提交
158
	/** @send_regs: GuC's FW specific registers used for sending MMIO H2G */
159 160 161 162 163 164
	struct {
		u32 base;
		unsigned int count;
		enum forcewake_domains fw_domains;
	} send_regs;

M
Matthew Brost 已提交
165
	/** @notify_reg: register used to send interrupts to the GuC FW */
166 167
	i915_reg_t notify_reg;

M
Matthew Brost 已提交
168 169 170 171 172
	/**
	 * @mmio_msg: notification bitmask that the GuC writes in one of its
	 * registers when the CT channel is disabled, to be processed when the
	 * channel is back up.
	 */
173 174
	u32 mmio_msg;

M
Matthew Brost 已提交
175
	/** @send_mutex: used to serialize the intel_guc_send actions */
176
	struct mutex send_mutex;
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

	/**
	 * @timestamp: GT timestamp object that stores a copy of the timestamp
	 * and adjusts it for overflow using a worker.
	 */
	struct {
		/**
		 * @lock: Lock protecting the below fields and the engine stats.
		 */
		spinlock_t lock;

		/**
		 * @gt_stamp: 64 bit extended value of the GT timestamp.
		 */
		u64 gt_stamp;

		/**
		 * @ping_delay: Period for polling the GT timestamp for
		 * overflow.
		 */
		unsigned long ping_delay;

		/**
		 * @work: Periodic work to adjust GT timestamp, engine and
		 * context usage for overflows.
		 */
		struct delayed_work work;
	} timestamp;
205 206
};

207 208 209 210 211
static inline struct intel_guc *log_to_guc(struct intel_guc_log *log)
{
	return container_of(log, struct intel_guc, log);
}

212 213 214
static
inline int intel_guc_send(struct intel_guc *guc, const u32 *action, u32 len)
{
215 216 217 218
	return intel_guc_ct_send(&guc->ct, action, len, NULL, 0, 0);
}

static
219 220
inline int intel_guc_send_nb(struct intel_guc *guc, const u32 *action, u32 len,
			     u32 g2h_len_dw)
221 222
{
	return intel_guc_ct_send(&guc->ct, action, len, NULL, 0,
223
				 MAKE_SEND_FLAGS(g2h_len_dw));
224 225 226 227 228 229
}

static inline int
intel_guc_send_and_receive(struct intel_guc *guc, const u32 *action, u32 len,
			   u32 *response_buf, u32 response_buf_size)
{
230
	return intel_guc_ct_send(&guc->ct, action, len,
231
				 response_buf, response_buf_size, 0);
232 233
}

234 235 236
static inline int intel_guc_send_busy_loop(struct intel_guc *guc,
					   const u32 *action,
					   u32 len,
237
					   u32 g2h_len_dw,
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
					   bool loop)
{
	int err;
	unsigned int sleep_period_ms = 1;
	bool not_atomic = !in_atomic() && !irqs_disabled();

	/*
	 * FIXME: Have caller pass in if we are in an atomic context to avoid
	 * using in_atomic(). It is likely safe here as we check for irqs
	 * disabled which basically all the spin locks in the i915 do but
	 * regardless this should be cleaned up.
	 */

	/* No sleeping with spin locks, just busy loop */
	might_sleep_if(loop && not_atomic);

retry:
255
	err = intel_guc_send_nb(guc, action, len, g2h_len_dw);
256 257 258 259 260 261 262 263 264 265 266 267 268 269
	if (unlikely(err == -EBUSY && loop)) {
		if (likely(not_atomic)) {
			if (msleep_interruptible(sleep_period_ms))
				return -EINTR;
			sleep_period_ms = sleep_period_ms << 1;
		} else {
			cpu_relax();
		}
		goto retry;
	}

	return err;
}

270 271
static inline void intel_guc_to_host_event_handler(struct intel_guc *guc)
{
272
	intel_guc_ct_event_handler(&guc->ct);
273 274
}

275 276 277 278 279 280 281 282
/* GuC addresses above GUC_GGTT_TOP also don't map through the GTT */
#define GUC_GGTT_TOP	0xFEE00000

/**
 * intel_guc_ggtt_offset() - Get and validate the GGTT offset of @vma
 * @guc: intel_guc structure.
 * @vma: i915 graphics virtual memory area.
 *
283
 * GuC does not allow any gfx GGTT address that falls into range
284 285
 * [0, ggtt.pin_bias), which is reserved for Boot ROM, SRAM and WOPCM.
 * Currently, in order to exclude [0, ggtt.pin_bias) address space from
286
 * GGTT, all gfx objects used by GuC are allocated with intel_guc_allocate_vma()
287
 * and pinned with PIN_OFFSET_BIAS along with the value of ggtt.pin_bias.
288
 *
289
 * Return: GGTT offset of the @vma.
290
 */
291 292
static inline u32 intel_guc_ggtt_offset(struct intel_guc *guc,
					struct i915_vma *vma)
293 294 295
{
	u32 offset = i915_ggtt_offset(vma);

296
	GEM_BUG_ON(offset < i915_ggtt_pin_bias(vma));
297 298 299 300 301 302
	GEM_BUG_ON(range_overflows_t(u64, offset, vma->size, GUC_GGTT_TOP));

	return offset;
}

void intel_guc_init_early(struct intel_guc *guc);
303
void intel_guc_init_late(struct intel_guc *guc);
304
void intel_guc_init_send_regs(struct intel_guc *guc);
305
void intel_guc_write_params(struct intel_guc *guc);
306 307
int intel_guc_init(struct intel_guc *guc);
void intel_guc_fini(struct intel_guc *guc);
308
void intel_guc_notify(struct intel_guc *guc);
309 310
int intel_guc_send_mmio(struct intel_guc *guc, const u32 *action, u32 len,
			u32 *response_buf, u32 response_buf_size);
311 312
int intel_guc_to_host_process_recv_msg(struct intel_guc *guc,
				       const u32 *payload, u32 len);
313
int intel_guc_auth_huc(struct intel_guc *guc, u32 rsa_offset);
314 315
int intel_guc_suspend(struct intel_guc *guc);
int intel_guc_resume(struct intel_guc *guc);
316
struct i915_vma *intel_guc_allocate_vma(struct intel_guc *guc, u32 size);
317 318
int intel_guc_allocate_and_map_vma(struct intel_guc *guc, u32 size,
				   struct i915_vma **out_vma, void **out_vaddr);
319

320 321
static inline bool intel_guc_is_supported(struct intel_guc *guc)
{
322 323 324
	return intel_uc_fw_is_supported(&guc->fw);
}

325
static inline bool intel_guc_is_wanted(struct intel_guc *guc)
326 327
{
	return intel_uc_fw_is_enabled(&guc->fw);
328 329
}

330 331 332 333 334 335
static inline bool intel_guc_is_used(struct intel_guc *guc)
{
	GEM_BUG_ON(__intel_uc_fw_status(&guc->fw) == INTEL_UC_FIRMWARE_SELECTED);
	return intel_uc_fw_is_available(&guc->fw);
}

336
static inline bool intel_guc_is_fw_running(struct intel_guc *guc)
337
{
338
	return intel_uc_fw_is_running(&guc->fw);
339 340
}

341 342 343 344 345
static inline bool intel_guc_is_ready(struct intel_guc *guc)
{
	return intel_guc_is_fw_running(guc) && intel_guc_ct_enabled(&guc->ct);
}

346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
static inline void intel_guc_reset_interrupts(struct intel_guc *guc)
{
	guc->interrupts.reset(guc);
}

static inline void intel_guc_enable_interrupts(struct intel_guc *guc)
{
	guc->interrupts.enable(guc);
}

static inline void intel_guc_disable_interrupts(struct intel_guc *guc)
{
	guc->interrupts.disable(guc);
}

361 362 363
static inline int intel_guc_sanitize(struct intel_guc *guc)
{
	intel_uc_fw_sanitize(&guc->fw);
364
	intel_guc_disable_interrupts(guc);
365
	intel_guc_ct_sanitize(&guc->ct);
366 367
	guc->mmio_msg = 0;

368 369 370
	return 0;
}

371 372 373 374 375 376 377 378 379 380 381 382 383 384
static inline void intel_guc_enable_msg(struct intel_guc *guc, u32 mask)
{
	spin_lock_irq(&guc->irq_lock);
	guc->msg_enabled_mask |= mask;
	spin_unlock_irq(&guc->irq_lock);
}

static inline void intel_guc_disable_msg(struct intel_guc *guc, u32 mask)
{
	spin_lock_irq(&guc->irq_lock);
	guc->msg_enabled_mask &= ~mask;
	spin_unlock_irq(&guc->irq_lock);
}

385 386
int intel_guc_wait_for_idle(struct intel_guc *guc, long timeout);

387 388
int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
					  const u32 *msg, u32 len);
389 390
int intel_guc_sched_done_process_msg(struct intel_guc *guc,
				     const u32 *msg, u32 len);
391 392
int intel_guc_context_reset_process_msg(struct intel_guc *guc,
					const u32 *msg, u32 len);
393 394
int intel_guc_engine_failure_process_msg(struct intel_guc *guc,
					 const u32 *msg, u32 len);
395

396 397
void intel_guc_find_hung_context(struct intel_engine_cs *engine);

398 399
int intel_guc_global_policies_update(struct intel_guc *guc);

400 401
void intel_guc_context_ban(struct intel_context *ce, struct i915_request *rq);

402 403 404 405 406
void intel_guc_submission_reset_prepare(struct intel_guc *guc);
void intel_guc_submission_reset(struct intel_guc *guc, bool stalled);
void intel_guc_submission_reset_finish(struct intel_guc *guc);
void intel_guc_submission_cancel_requests(struct intel_guc *guc);

407 408
void intel_guc_load_status(struct intel_guc *guc, struct drm_printer *p);

409 410
void intel_guc_write_barrier(struct intel_guc *guc);

411
#endif