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