intel_engine.h revision 1.2 1 /* $NetBSD: intel_engine.h,v 1.2 2021/12/18 23:45:30 riastradh Exp $ */
2
3 /* SPDX-License-Identifier: MIT */
4 #ifndef _INTEL_RINGBUFFER_H_
5 #define _INTEL_RINGBUFFER_H_
6
7 #include <drm/drm_util.h>
8
9 #include <linux/hashtable.h>
10 #include <linux/irq_work.h>
11 #include <linux/random.h>
12 #include <linux/seqlock.h>
13
14 #include "i915_pmu.h"
15 #include "i915_reg.h"
16 #include "i915_request.h"
17 #include "i915_selftest.h"
18 #include "gt/intel_timeline.h"
19 #include "intel_engine_types.h"
20 #include "intel_gpu_commands.h"
21 #include "intel_workarounds.h"
22
23 struct drm_printer;
24 struct intel_gt;
25
26 /* Early gen2 devices have a cacheline of just 32 bytes, using 64 is overkill,
27 * but keeps the logic simple. Indeed, the whole purpose of this macro is just
28 * to give some inclination as to some of the magic values used in the various
29 * workarounds!
30 */
31 #define CACHELINE_BYTES 64
32 #define CACHELINE_DWORDS (CACHELINE_BYTES / sizeof(u32))
33
34 #define ENGINE_TRACE(e, fmt, ...) do { \
35 const struct intel_engine_cs *e__ __maybe_unused = (e); \
36 GEM_TRACE("%s %s: " fmt, \
37 dev_name(e__->i915->drm.dev), e__->name, \
38 ##__VA_ARGS__); \
39 } while (0)
40
41 /*
42 * The register defines to be used with the following macros need to accept a
43 * base param, e.g:
44 *
45 * REG_FOO(base) _MMIO((base) + <relative offset>)
46 * ENGINE_READ(engine, REG_FOO);
47 *
48 * register arrays are to be defined and accessed as follows:
49 *
50 * REG_BAR(base, i) _MMIO((base) + <relative offset> + (i) * <shift>)
51 * ENGINE_READ_IDX(engine, REG_BAR, i)
52 */
53
54 #define __ENGINE_REG_OP(op__, engine__, ...) \
55 intel_uncore_##op__((engine__)->uncore, __VA_ARGS__)
56
57 #define __ENGINE_READ_OP(op__, engine__, reg__) \
58 __ENGINE_REG_OP(op__, (engine__), reg__((engine__)->mmio_base))
59
60 #define ENGINE_READ16(...) __ENGINE_READ_OP(read16, __VA_ARGS__)
61 #define ENGINE_READ(...) __ENGINE_READ_OP(read, __VA_ARGS__)
62 #define ENGINE_READ_FW(...) __ENGINE_READ_OP(read_fw, __VA_ARGS__)
63 #define ENGINE_POSTING_READ(...) __ENGINE_READ_OP(posting_read_fw, __VA_ARGS__)
64 #define ENGINE_POSTING_READ16(...) __ENGINE_READ_OP(posting_read16, __VA_ARGS__)
65
66 #define ENGINE_READ64(engine__, lower_reg__, upper_reg__) \
67 __ENGINE_REG_OP(read64_2x32, (engine__), \
68 lower_reg__((engine__)->mmio_base), \
69 upper_reg__((engine__)->mmio_base))
70
71 #define ENGINE_READ_IDX(engine__, reg__, idx__) \
72 __ENGINE_REG_OP(read, (engine__), reg__((engine__)->mmio_base, (idx__)))
73
74 #define __ENGINE_WRITE_OP(op__, engine__, reg__, val__) \
75 __ENGINE_REG_OP(op__, (engine__), reg__((engine__)->mmio_base), (val__))
76
77 #define ENGINE_WRITE16(...) __ENGINE_WRITE_OP(write16, __VA_ARGS__)
78 #define ENGINE_WRITE(...) __ENGINE_WRITE_OP(write, __VA_ARGS__)
79 #define ENGINE_WRITE_FW(...) __ENGINE_WRITE_OP(write_fw, __VA_ARGS__)
80
81 #define GEN6_RING_FAULT_REG_READ(engine__) \
82 intel_uncore_read((engine__)->uncore, RING_FAULT_REG(engine__))
83
84 #define GEN6_RING_FAULT_REG_POSTING_READ(engine__) \
85 intel_uncore_posting_read((engine__)->uncore, RING_FAULT_REG(engine__))
86
87 #define GEN6_RING_FAULT_REG_RMW(engine__, clear__, set__) \
88 ({ \
89 u32 __val; \
90 \
91 __val = intel_uncore_read((engine__)->uncore, \
92 RING_FAULT_REG(engine__)); \
93 __val &= ~(clear__); \
94 __val |= (set__); \
95 intel_uncore_write((engine__)->uncore, RING_FAULT_REG(engine__), \
96 __val); \
97 })
98
99 /* seqno size is actually only a uint32, but since we plan to use MI_FLUSH_DW to
100 * do the writes, and that must have qw aligned offsets, simply pretend it's 8b.
101 */
102
103 static inline unsigned int
104 execlists_num_ports(const struct intel_engine_execlists * const execlists)
105 {
106 return execlists->port_mask + 1;
107 }
108
109 static inline struct i915_request *
110 execlists_active(const struct intel_engine_execlists *execlists)
111 {
112 return *READ_ONCE(execlists->active);
113 }
114
115 static inline void
116 execlists_active_lock_bh(struct intel_engine_execlists *execlists)
117 {
118 local_bh_disable(); /* prevent local softirq and lock recursion */
119 tasklet_lock(&execlists->tasklet);
120 }
121
122 static inline void
123 execlists_active_unlock_bh(struct intel_engine_execlists *execlists)
124 {
125 tasklet_unlock(&execlists->tasklet);
126 local_bh_enable(); /* restore softirq, and kick ksoftirqd! */
127 }
128
129 struct i915_request *
130 execlists_unwind_incomplete_requests(struct intel_engine_execlists *execlists);
131
132 static inline u32
133 intel_read_status_page(const struct intel_engine_cs *engine, int reg)
134 {
135 /* Ensure that the compiler doesn't optimize away the load. */
136 return READ_ONCE(engine->status_page.addr[reg]);
137 }
138
139 static inline void
140 intel_write_status_page(struct intel_engine_cs *engine, int reg, u32 value)
141 {
142 /* Writing into the status page should be done sparingly. Since
143 * we do when we are uncertain of the device state, we take a bit
144 * of extra paranoia to try and ensure that the HWS takes the value
145 * we give and that it doesn't end up trapped inside the CPU!
146 */
147 if (static_cpu_has(X86_FEATURE_CLFLUSH)) {
148 mb();
149 clflush(&engine->status_page.addr[reg]);
150 engine->status_page.addr[reg] = value;
151 clflush(&engine->status_page.addr[reg]);
152 mb();
153 } else {
154 WRITE_ONCE(engine->status_page.addr[reg], value);
155 }
156 }
157
158 /*
159 * Reads a dword out of the status page, which is written to from the command
160 * queue by automatic updates, MI_REPORT_HEAD, MI_STORE_DATA_INDEX, or
161 * MI_STORE_DATA_IMM.
162 *
163 * The following dwords have a reserved meaning:
164 * 0x00: ISR copy, updated when an ISR bit not set in the HWSTAM changes.
165 * 0x04: ring 0 head pointer
166 * 0x05: ring 1 head pointer (915-class)
167 * 0x06: ring 2 head pointer (915-class)
168 * 0x10-0x1b: Context status DWords (GM45)
169 * 0x1f: Last written status offset. (GM45)
170 * 0x20-0x2f: Reserved (Gen6+)
171 *
172 * The area from dword 0x30 to 0x3ff is available for driver usage.
173 */
174 #define I915_GEM_HWS_PREEMPT 0x32
175 #define I915_GEM_HWS_PREEMPT_ADDR (I915_GEM_HWS_PREEMPT * sizeof(u32))
176 #define I915_GEM_HWS_SEQNO 0x40
177 #define I915_GEM_HWS_SEQNO_ADDR (I915_GEM_HWS_SEQNO * sizeof(u32))
178 #define I915_GEM_HWS_SCRATCH 0x80
179 #define I915_GEM_HWS_SCRATCH_ADDR (I915_GEM_HWS_SCRATCH * sizeof(u32))
180
181 #define I915_HWS_CSB_BUF0_INDEX 0x10
182 #define I915_HWS_CSB_WRITE_INDEX 0x1f
183 #define CNL_HWS_CSB_WRITE_INDEX 0x2f
184
185 void intel_engine_stop(struct intel_engine_cs *engine);
186 void intel_engine_cleanup(struct intel_engine_cs *engine);
187
188 int intel_engines_init_mmio(struct intel_gt *gt);
189 int intel_engines_init(struct intel_gt *gt);
190
191 void intel_engines_release(struct intel_gt *gt);
192 void intel_engines_free(struct intel_gt *gt);
193
194 int intel_engine_init_common(struct intel_engine_cs *engine);
195 void intel_engine_cleanup_common(struct intel_engine_cs *engine);
196
197 int intel_ring_submission_setup(struct intel_engine_cs *engine);
198
199 int intel_engine_stop_cs(struct intel_engine_cs *engine);
200 void intel_engine_cancel_stop_cs(struct intel_engine_cs *engine);
201
202 void intel_engine_set_hwsp_writemask(struct intel_engine_cs *engine, u32 mask);
203
204 u64 intel_engine_get_active_head(const struct intel_engine_cs *engine);
205 u64 intel_engine_get_last_batch_head(const struct intel_engine_cs *engine);
206
207 void intel_engine_get_instdone(const struct intel_engine_cs *engine,
208 struct intel_instdone *instdone);
209
210 void intel_engine_init_execlists(struct intel_engine_cs *engine);
211
212 void intel_engine_init_breadcrumbs(struct intel_engine_cs *engine);
213 void intel_engine_fini_breadcrumbs(struct intel_engine_cs *engine);
214
215 void intel_engine_disarm_breadcrumbs(struct intel_engine_cs *engine);
216
217 static inline void
218 intel_engine_signal_breadcrumbs(struct intel_engine_cs *engine)
219 {
220 irq_work_queue(&engine->breadcrumbs.irq_work);
221 }
222
223 void intel_engine_reset_breadcrumbs(struct intel_engine_cs *engine);
224 void intel_engine_fini_breadcrumbs(struct intel_engine_cs *engine);
225
226 void intel_engine_print_breadcrumbs(struct intel_engine_cs *engine,
227 struct drm_printer *p);
228
229 static inline u32 *gen8_emit_pipe_control(u32 *batch, u32 flags, u32 offset)
230 {
231 memset(batch, 0, 6 * sizeof(u32));
232
233 batch[0] = GFX_OP_PIPE_CONTROL(6);
234 batch[1] = flags;
235 batch[2] = offset;
236
237 return batch + 6;
238 }
239
240 static inline u32 *
241 gen8_emit_ggtt_write_rcs(u32 *cs, u32 value, u32 gtt_offset, u32 flags)
242 {
243 /* We're using qword write, offset should be aligned to 8 bytes. */
244 GEM_BUG_ON(!IS_ALIGNED(gtt_offset, 8));
245
246 /* w/a for post sync ops following a GPGPU operation we
247 * need a prior CS_STALL, which is emitted by the flush
248 * following the batch.
249 */
250 *cs++ = GFX_OP_PIPE_CONTROL(6);
251 *cs++ = flags | PIPE_CONTROL_QW_WRITE | PIPE_CONTROL_GLOBAL_GTT_IVB;
252 *cs++ = gtt_offset;
253 *cs++ = 0;
254 *cs++ = value;
255 /* We're thrashing one dword of HWS. */
256 *cs++ = 0;
257
258 return cs;
259 }
260
261 static inline u32 *
262 gen8_emit_ggtt_write(u32 *cs, u32 value, u32 gtt_offset, u32 flags)
263 {
264 /* w/a: bit 5 needs to be zero for MI_FLUSH_DW address. */
265 GEM_BUG_ON(gtt_offset & (1 << 5));
266 /* Offset should be aligned to 8 bytes for both (QW/DW) write types */
267 GEM_BUG_ON(!IS_ALIGNED(gtt_offset, 8));
268
269 *cs++ = (MI_FLUSH_DW + 1) | MI_FLUSH_DW_OP_STOREDW | flags;
270 *cs++ = gtt_offset | MI_FLUSH_DW_USE_GTT;
271 *cs++ = 0;
272 *cs++ = value;
273
274 return cs;
275 }
276
277 static inline void __intel_engine_reset(struct intel_engine_cs *engine,
278 bool stalled)
279 {
280 if (engine->reset.rewind)
281 engine->reset.rewind(engine, stalled);
282 engine->serial++; /* contexts lost */
283 }
284
285 bool intel_engines_are_idle(struct intel_gt *gt);
286 bool intel_engine_is_idle(struct intel_engine_cs *engine);
287 void intel_engine_flush_submission(struct intel_engine_cs *engine);
288
289 void intel_engines_reset_default_submission(struct intel_gt *gt);
290
291 bool intel_engine_can_store_dword(struct intel_engine_cs *engine);
292
293 __printf(3, 4)
294 void intel_engine_dump(struct intel_engine_cs *engine,
295 struct drm_printer *m,
296 const char *header, ...);
297
298 int intel_enable_engine_stats(struct intel_engine_cs *engine);
299 void intel_disable_engine_stats(struct intel_engine_cs *engine);
300
301 ktime_t intel_engine_get_busy_time(struct intel_engine_cs *engine);
302
303 struct i915_request *
304 intel_engine_find_active_request(struct intel_engine_cs *engine);
305
306 u32 intel_engine_context_size(struct intel_gt *gt, u8 class);
307
308 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
309
310 static inline bool inject_preempt_hang(struct intel_engine_execlists *execlists)
311 {
312 if (!execlists->preempt_hang.inject_hang)
313 return false;
314
315 complete(&execlists->preempt_hang.completion);
316 return true;
317 }
318
319 #else
320
321 static inline bool inject_preempt_hang(struct intel_engine_execlists *execlists)
322 {
323 return false;
324 }
325
326 #endif
327
328 void intel_engine_init_active(struct intel_engine_cs *engine,
329 unsigned int subclass);
330 #define ENGINE_PHYSICAL 0
331 #define ENGINE_MOCK 1
332 #define ENGINE_VIRTUAL 2
333
334 static inline bool
335 intel_engine_has_preempt_reset(const struct intel_engine_cs *engine)
336 {
337 if (!IS_ACTIVE(CONFIG_DRM_I915_PREEMPT_TIMEOUT))
338 return false;
339
340 return intel_engine_has_preemption(engine);
341 }
342
343 static inline bool
344 intel_engine_has_timeslices(const struct intel_engine_cs *engine)
345 {
346 if (!IS_ACTIVE(CONFIG_DRM_I915_TIMESLICE_DURATION))
347 return false;
348
349 return intel_engine_has_semaphores(engine);
350 }
351
352 #endif /* _INTEL_RINGBUFFER_H_ */
353