intel_engine.h 11.0 KB
Newer Older
1
/* SPDX-License-Identifier: MIT */
2 3 4
#ifndef _INTEL_RINGBUFFER_H_
#define _INTEL_RINGBUFFER_H_

5 6
#include <drm/drm_util.h>

7
#include <linux/hashtable.h>
8
#include <linux/irq_work.h>
9
#include <linux/random.h>
10
#include <linux/seqlock.h>
11

12
#include "i915_pmu.h"
13
#include "i915_reg.h"
14
#include "i915_request.h"
15
#include "i915_selftest.h"
16
#include "gt/intel_timeline.h"
17
#include "intel_engine_types.h"
18
#include "intel_gpu_commands.h"
19
#include "intel_workarounds.h"
20

21
struct drm_printer;
22 23
struct intel_gt;

24 25 26 27 28 29
/* Early gen2 devices have a cacheline of just 32 bytes, using 64 is overkill,
 * but keeps the logic simple. Indeed, the whole purpose of this macro is just
 * to give some inclination as to some of the magic values used in the various
 * workarounds!
 */
#define CACHELINE_BYTES 64
30
#define CACHELINE_DWORDS (CACHELINE_BYTES / sizeof(u32))
31

32 33 34 35 36 37 38
#define ENGINE_TRACE(e, fmt, ...) do {					\
	const struct intel_engine_cs *e__ __maybe_unused = (e);		\
	GEM_TRACE("%s %s: " fmt,					\
		  dev_name(e__->i915->drm.dev), e__->name,		\
		  ##__VA_ARGS__);					\
} while (0)

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
/*
 * The register defines to be used with the following macros need to accept a
 * base param, e.g:
 *
 * REG_FOO(base) _MMIO((base) + <relative offset>)
 * ENGINE_READ(engine, REG_FOO);
 *
 * register arrays are to be defined and accessed as follows:
 *
 * REG_BAR(base, i) _MMIO((base) + <relative offset> + (i) * <shift>)
 * ENGINE_READ_IDX(engine, REG_BAR, i)
 */

#define __ENGINE_REG_OP(op__, engine__, ...) \
	intel_uncore_##op__((engine__)->uncore, __VA_ARGS__)

#define __ENGINE_READ_OP(op__, engine__, reg__) \
	__ENGINE_REG_OP(op__, (engine__), reg__((engine__)->mmio_base))
57

58 59 60
#define ENGINE_READ16(...)	__ENGINE_READ_OP(read16, __VA_ARGS__)
#define ENGINE_READ(...)	__ENGINE_READ_OP(read, __VA_ARGS__)
#define ENGINE_READ_FW(...)	__ENGINE_READ_OP(read_fw, __VA_ARGS__)
61
#define ENGINE_POSTING_READ(...) __ENGINE_READ_OP(posting_read_fw, __VA_ARGS__)
T
Tvrtko Ursulin 已提交
62
#define ENGINE_POSTING_READ16(...) __ENGINE_READ_OP(posting_read16, __VA_ARGS__)
63

64 65 66 67
#define ENGINE_READ64(engine__, lower_reg__, upper_reg__) \
	__ENGINE_REG_OP(read64_2x32, (engine__), \
			lower_reg__((engine__)->mmio_base), \
			upper_reg__((engine__)->mmio_base))
68

69 70
#define ENGINE_READ_IDX(engine__, reg__, idx__) \
	__ENGINE_REG_OP(read, (engine__), reg__((engine__)->mmio_base, (idx__)))
71

72 73
#define __ENGINE_WRITE_OP(op__, engine__, reg__, val__) \
	__ENGINE_REG_OP(op__, (engine__), reg__((engine__)->mmio_base), (val__))
74

75 76 77
#define ENGINE_WRITE16(...)	__ENGINE_WRITE_OP(write16, __VA_ARGS__)
#define ENGINE_WRITE(...)	__ENGINE_WRITE_OP(write, __VA_ARGS__)
#define ENGINE_WRITE_FW(...)	__ENGINE_WRITE_OP(write_fw, __VA_ARGS__)
78

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
#define GEN6_RING_FAULT_REG_READ(engine__) \
	intel_uncore_read((engine__)->uncore, RING_FAULT_REG(engine__))

#define GEN6_RING_FAULT_REG_POSTING_READ(engine__) \
	intel_uncore_posting_read((engine__)->uncore, RING_FAULT_REG(engine__))

#define GEN6_RING_FAULT_REG_RMW(engine__, clear__, set__) \
({ \
	u32 __val; \
\
	__val = intel_uncore_read((engine__)->uncore, \
				  RING_FAULT_REG(engine__)); \
	__val &= ~(clear__); \
	__val |= (set__); \
	intel_uncore_write((engine__)->uncore, RING_FAULT_REG(engine__), \
			   __val); \
})

97 98 99
/* seqno size is actually only a uint32, but since we plan to use MI_FLUSH_DW to
 * do the writes, and that must have qw aligned offsets, simply pretend it's 8b.
 */
100

101 102
static inline unsigned int
execlists_num_ports(const struct intel_engine_execlists * const execlists)
103
{
104
	return execlists->port_mask + 1;
105 106
}

107 108
static inline struct i915_request *
execlists_active(const struct intel_engine_execlists *execlists)
109
{
110 111 112 113 114 115 116 117 118 119 120 121 122 123
	struct i915_request * const *cur, * const *old, *active;

	cur = READ_ONCE(execlists->active);
	smp_rmb(); /* pairs with overwrite protection in process_csb() */
	do {
		old = cur;

		active = READ_ONCE(*cur);
		cur = READ_ONCE(execlists->active);

		smp_rmb(); /* and complete the seqlock retry */
	} while (unlikely(cur != old));

	return active;
124 125
}

126 127 128 129 130 131 132 133 134 135 136 137 138 139
static inline void
execlists_active_lock_bh(struct intel_engine_execlists *execlists)
{
	local_bh_disable(); /* prevent local softirq and lock recursion */
	tasklet_lock(&execlists->tasklet);
}

static inline void
execlists_active_unlock_bh(struct intel_engine_execlists *execlists)
{
	tasklet_unlock(&execlists->tasklet);
	local_bh_enable(); /* restore softirq, and kick ksoftirqd! */
}

140
struct i915_request *
141 142
execlists_unwind_incomplete_requests(struct intel_engine_execlists *execlists);

143
static inline u32
144
intel_read_status_page(const struct intel_engine_cs *engine, int reg)
145
{
146
	/* Ensure that the compiler doesn't optimize away the load. */
147
	return READ_ONCE(engine->status_page.addr[reg]);
148 149
}

M
Mika Kuoppala 已提交
150
static inline void
151
intel_write_status_page(struct intel_engine_cs *engine, int reg, u32 value)
M
Mika Kuoppala 已提交
152
{
153 154 155 156 157 158 159
	/* Writing into the status page should be done sparingly. Since
	 * we do when we are uncertain of the device state, we take a bit
	 * of extra paranoia to try and ensure that the HWS takes the value
	 * we give and that it doesn't end up trapped inside the CPU!
	 */
	if (static_cpu_has(X86_FEATURE_CLFLUSH)) {
		mb();
160 161 162
		clflush(&engine->status_page.addr[reg]);
		engine->status_page.addr[reg] = value;
		clflush(&engine->status_page.addr[reg]);
163 164
		mb();
	} else {
165
		WRITE_ONCE(engine->status_page.addr[reg], value);
166
	}
M
Mika Kuoppala 已提交
167 168
}

169
/*
C
Chris Wilson 已提交
170 171 172 173 174 175 176 177 178 179 180
 * Reads a dword out of the status page, which is written to from the command
 * queue by automatic updates, MI_REPORT_HEAD, MI_STORE_DATA_INDEX, or
 * MI_STORE_DATA_IMM.
 *
 * The following dwords have a reserved meaning:
 * 0x00: ISR copy, updated when an ISR bit not set in the HWSTAM changes.
 * 0x04: ring 0 head pointer
 * 0x05: ring 1 head pointer (915-class)
 * 0x06: ring 2 head pointer (915-class)
 * 0x10-0x1b: Context status DWords (GM45)
 * 0x1f: Last written status offset. (GM45)
181
 * 0x20-0x2f: Reserved (Gen6+)
C
Chris Wilson 已提交
182
 *
183
 * The area from dword 0x30 to 0x3ff is available for driver usage.
C
Chris Wilson 已提交
184
 */
185 186
#define I915_GEM_HWS_PREEMPT		0x32
#define I915_GEM_HWS_PREEMPT_ADDR	(I915_GEM_HWS_PREEMPT * sizeof(u32))
187 188 189
#define I915_GEM_HWS_SEQNO		0x40
#define I915_GEM_HWS_SEQNO_ADDR		(I915_GEM_HWS_SEQNO * sizeof(u32))
#define I915_GEM_HWS_SCRATCH		0x80
190
#define I915_GEM_HWS_SCRATCH_ADDR	(I915_GEM_HWS_SCRATCH * sizeof(u32))
C
Chris Wilson 已提交
191

192
#define I915_HWS_CSB_BUF0_INDEX		0x10
193 194
#define I915_HWS_CSB_WRITE_INDEX	0x1f
#define CNL_HWS_CSB_WRITE_INDEX		0x2f
195

196 197
void intel_engine_stop(struct intel_engine_cs *engine);
void intel_engine_cleanup(struct intel_engine_cs *engine);
198

199
int intel_engines_init_mmio(struct intel_gt *gt);
200
int intel_engines_init(struct intel_gt *gt);
201

202 203
void intel_engine_free_request_pool(struct intel_engine_cs *engine);

204 205
void intel_engines_release(struct intel_gt *gt);
void intel_engines_free(struct intel_gt *gt);
206

207
int intel_engine_init_common(struct intel_engine_cs *engine);
208
void intel_engine_cleanup_common(struct intel_engine_cs *engine);
209

210 211
int intel_engine_resume(struct intel_engine_cs *engine);

212
int intel_ring_submission_setup(struct intel_engine_cs *engine);
213

214
int intel_engine_stop_cs(struct intel_engine_cs *engine);
215
void intel_engine_cancel_stop_cs(struct intel_engine_cs *engine);
216

217 218
void intel_engine_set_hwsp_writemask(struct intel_engine_cs *engine, u32 mask);

219 220
u64 intel_engine_get_active_head(const struct intel_engine_cs *engine);
u64 intel_engine_get_last_batch_head(const struct intel_engine_cs *engine);
221

222
void intel_engine_get_instdone(const struct intel_engine_cs *engine,
223 224
			       struct intel_instdone *instdone);

225 226
void intel_engine_init_execlists(struct intel_engine_cs *engine);

227 228
void intel_engine_init_breadcrumbs(struct intel_engine_cs *engine);
void intel_engine_fini_breadcrumbs(struct intel_engine_cs *engine);
229

230
void intel_engine_disarm_breadcrumbs(struct intel_engine_cs *engine);
231

232
static inline void
233
intel_engine_signal_breadcrumbs(struct intel_engine_cs *engine)
234
{
235
	irq_work_queue(&engine->breadcrumbs.irq_work);
236 237
}

238
void intel_engine_reset_breadcrumbs(struct intel_engine_cs *engine);
239 240
void intel_engine_fini_breadcrumbs(struct intel_engine_cs *engine);

241 242 243
void intel_engine_print_breadcrumbs(struct intel_engine_cs *engine,
				    struct drm_printer *p);

244
static inline u32 *__gen8_emit_pipe_control(u32 *batch, u32 flags0, u32 flags1, u32 offset)
245 246 247
{
	memset(batch, 0, 6 * sizeof(u32));

248 249
	batch[0] = GFX_OP_PIPE_CONTROL(6) | flags0;
	batch[1] = flags1;
250 251 252 253 254
	batch[2] = offset;

	return batch + 6;
}

255 256 257 258 259 260 261 262 263 264
static inline u32 *gen8_emit_pipe_control(u32 *batch, u32 flags, u32 offset)
{
	return __gen8_emit_pipe_control(batch, 0, flags, offset);
}

static inline u32 *gen12_emit_pipe_control(u32 *batch, u32 flags0, u32 flags1, u32 offset)
{
	return __gen8_emit_pipe_control(batch, flags0, flags1, offset);
}

265
static inline u32 *
266
__gen8_emit_ggtt_write_rcs(u32 *cs, u32 value, u32 gtt_offset, u32 flags0, u32 flags1)
267 268 269 270 271 272 273 274
{
	/* We're using qword write, offset should be aligned to 8 bytes. */
	GEM_BUG_ON(!IS_ALIGNED(gtt_offset, 8));

	/* w/a for post sync ops following a GPGPU operation we
	 * need a prior CS_STALL, which is emitted by the flush
	 * following the batch.
	 */
275 276
	*cs++ = GFX_OP_PIPE_CONTROL(6) | flags0;
	*cs++ = flags1 | PIPE_CONTROL_QW_WRITE | PIPE_CONTROL_GLOBAL_GTT_IVB;
277 278 279 280 281 282 283 284 285
	*cs++ = gtt_offset;
	*cs++ = 0;
	*cs++ = value;
	/* We're thrashing one dword of HWS. */
	*cs++ = 0;

	return cs;
}

286 287 288 289 290 291 292 293 294 295 296 297
static inline u32*
gen8_emit_ggtt_write_rcs(u32 *cs, u32 value, u32 gtt_offset, u32 flags)
{
	return __gen8_emit_ggtt_write_rcs(cs, value, gtt_offset, 0, flags);
}

static inline u32*
gen12_emit_ggtt_write_rcs(u32 *cs, u32 value, u32 gtt_offset, u32 flags0, u32 flags1)
{
	return __gen8_emit_ggtt_write_rcs(cs, value, gtt_offset, flags0, flags1);
}

298
static inline u32 *
299
gen8_emit_ggtt_write(u32 *cs, u32 value, u32 gtt_offset, u32 flags)
300 301 302 303 304 305
{
	/* w/a: bit 5 needs to be zero for MI_FLUSH_DW address. */
	GEM_BUG_ON(gtt_offset & (1 << 5));
	/* Offset should be aligned to 8 bytes for both (QW/DW) write types */
	GEM_BUG_ON(!IS_ALIGNED(gtt_offset, 8));

306
	*cs++ = (MI_FLUSH_DW + 1) | MI_FLUSH_DW_OP_STOREDW | flags;
307 308 309 310 311 312 313
	*cs++ = gtt_offset | MI_FLUSH_DW_USE_GTT;
	*cs++ = 0;
	*cs++ = value;

	return cs;
}

314 315
static inline void __intel_engine_reset(struct intel_engine_cs *engine,
					bool stalled)
316
{
317 318
	if (engine->reset.rewind)
		engine->reset.rewind(engine, stalled);
319
	engine->serial++; /* contexts lost */
320 321
}

322
bool intel_engines_are_idle(struct intel_gt *gt);
323
bool intel_engine_is_idle(struct intel_engine_cs *engine);
324
void intel_engine_flush_submission(struct intel_engine_cs *engine);
325

326
void intel_engines_reset_default_submission(struct intel_gt *gt);
327

328
bool intel_engine_can_store_dword(struct intel_engine_cs *engine);
329

330 331 332 333
__printf(3, 4)
void intel_engine_dump(struct intel_engine_cs *engine,
		       struct drm_printer *m,
		       const char *header, ...);
334

335 336
ktime_t intel_engine_get_busy_time(struct intel_engine_cs *engine);

337 338 339
struct i915_request *
intel_engine_find_active_request(struct intel_engine_cs *engine);

340
u32 intel_engine_context_size(struct intel_gt *gt, u8 class);
341

342 343 344 345 346 347
void intel_engine_init_active(struct intel_engine_cs *engine,
			      unsigned int subclass);
#define ENGINE_PHYSICAL	0
#define ENGINE_MOCK	1
#define ENGINE_VIRTUAL	2

348 349 350
static inline bool
intel_engine_has_preempt_reset(const struct intel_engine_cs *engine)
{
351 352
	if (!IS_ACTIVE(CONFIG_DRM_I915_PREEMPT_TIMEOUT))
		return false;
353 354 355 356

	return intel_engine_has_preemption(engine);
}

357
#endif /* _INTEL_RINGBUFFER_H_ */