xref: /linux/drivers/gpu/drm/xe/xe_lrc.h (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Copyright © 2021 Intel Corporation
4  */
5 #ifndef _XE_LRC_H_
6 #define _XE_LRC_H_
7 
8 #include <linux/types.h>
9 
10 #include "xe_lrc_types.h"
11 
12 /*
13  * Sentinel value stored in lrc->ctx_timestamp while a context is starting.
14  * The hardware hasn't yet written the real CTX_TIMESTAMP, so this is not a
15  * valid elapsed-time sample and must not be used as one.
16  */
17 #define XE_LRC_CTX_TIMESTAMP_ACTIVE 1ULL
18 
19 struct drm_printer;
20 struct xe_bb;
21 struct xe_device;
22 struct xe_exec_queue;
23 enum xe_multi_queue_priority;
24 enum xe_engine_class;
25 struct xe_gt;
26 struct xe_hw_engine;
27 struct xe_lrc;
28 struct xe_vm;
29 
30 struct xe_lrc_snapshot {
31 	struct xe_bo *lrc_bo;
32 	void *lrc_snapshot;
33 	unsigned long lrc_size, lrc_offset;
34 	unsigned long replay_size, replay_offset;
35 
36 	u32 context_desc;
37 	u32 ring_addr;
38 	u32 indirect_context_desc;
39 	u32 head;
40 	u32 start;
41 	struct {
42 		u32 internal;
43 		u32 memory;
44 	} tail;
45 	u32 start_seqno;
46 	u32 seqno;
47 	u64 ctx_timestamp;
48 	u64 ctx_timestamp_ms;
49 	u64 queue_timestamp;
50 	u64 queue_timestamp_ms;
51 	u32 ctx_job_timestamp;
52 };
53 
54 #define LRC_PPHWSP_FLUSH_INVAL_SCRATCH_ADDR (0x34 * 4)
55 #define LRC_PPHWSP_PXP_INVAL_SCRATCH_ADDR (0x40 * 4)
56 
57 #define LRC_WA_BB_SIZE SZ_4K
58 
59 #define XE_LRC_CREATE_RUNALONE		BIT(0)
60 #define XE_LRC_CREATE_PXP		BIT(1)
61 #define XE_LRC_CREATE_USER_CTX		BIT(2)
62 #define XE_LRC_DISABLE_STATE_CACHE_PERF_FIX	BIT(3)
63 
64 struct xe_lrc *xe_lrc_create(struct xe_hw_engine *hwe, struct xe_vm *vm,
65 			     void *replay_state, u32 ring_size, u16 msix_vec, u32 flags);
66 void xe_lrc_destroy(struct kref *ref);
67 
68 /**
69  * xe_lrc_get - Get reference to the LRC
70  * @lrc: Logical Ring Context
71  *
72  * Increment reference count of @lrc
73  */
74 static inline struct xe_lrc *xe_lrc_get(struct xe_lrc *lrc)
75 {
76 	kref_get(&lrc->refcount);
77 	return lrc;
78 }
79 
80 /**
81  * xe_lrc_put - Put reference of the LRC
82  * @lrc: Logical Ring Context
83  *
84  * Decrement reference count of @lrc, call xe_lrc_destroy when
85  * reference count reaches 0.
86  */
87 static inline void xe_lrc_put(struct xe_lrc *lrc)
88 {
89 	if (lrc)
90 		kref_put(&lrc->refcount, xe_lrc_destroy);
91 }
92 
93 /**
94  * xe_lrc_ring_size() - Xe LRC ring size
95  *
96  * Return: Size of LRC ring buffer
97  */
98 static inline size_t xe_lrc_ring_size(void)
99 {
100 	return SZ_16K;
101 }
102 
103 static inline bool xe_lrc_is_multi_queue(struct xe_lrc *lrc)
104 {
105 	return lrc->multi_queue.primary_lrc;
106 }
107 
108 size_t xe_gt_lrc_hang_replay_size(struct xe_gt *gt, enum xe_engine_class class);
109 size_t xe_gt_lrc_size(struct xe_gt *gt, enum xe_engine_class class);
110 u32 xe_lrc_pphwsp_offset(struct xe_lrc *lrc);
111 u32 xe_lrc_regs_offset(struct xe_lrc *lrc);
112 
113 void xe_lrc_set_ring_tail(struct xe_lrc *lrc, u32 tail);
114 u32 xe_lrc_ring_tail(struct xe_lrc *lrc);
115 void xe_lrc_set_ring_head(struct xe_lrc *lrc, u32 head);
116 u32 xe_lrc_ring_head(struct xe_lrc *lrc);
117 u32 xe_lrc_ring_space(struct xe_lrc *lrc);
118 void xe_lrc_write_ring(struct xe_lrc *lrc, const void *data, size_t size);
119 
120 bool xe_lrc_ring_is_idle(struct xe_lrc *lrc);
121 
122 u32 xe_lrc_indirect_ring_ggtt_addr(struct xe_lrc *lrc);
123 u32 xe_lrc_ggtt_addr(struct xe_lrc *lrc);
124 u32 *xe_lrc_regs(struct xe_lrc *lrc);
125 void xe_lrc_update_hwctx_regs_with_address(struct xe_lrc *lrc);
126 void xe_default_lrc_update_memirq_regs_with_address(struct xe_hw_engine *hwe);
127 void xe_lrc_update_memirq_regs_with_address(struct xe_lrc *lrc, struct xe_hw_engine *hwe,
128 					    u32 *regs);
129 
130 u32 xe_lrc_read_ctx_reg(struct xe_lrc *lrc, int reg_nr);
131 void xe_lrc_write_ctx_reg(struct xe_lrc *lrc, int reg_nr, u32 val);
132 
133 u64 xe_lrc_descriptor(struct xe_lrc *lrc);
134 
135 u32 xe_lrc_seqno_ggtt_addr(struct xe_lrc *lrc);
136 struct dma_fence *xe_lrc_alloc_seqno_fence(void);
137 void xe_lrc_free_seqno_fence(struct dma_fence *fence);
138 void xe_lrc_init_seqno_fence(struct xe_lrc *lrc, struct dma_fence *fence);
139 s32 xe_lrc_seqno(struct xe_lrc *lrc);
140 
141 u32 xe_lrc_start_seqno_ggtt_addr(struct xe_lrc *lrc);
142 s32 xe_lrc_start_seqno(struct xe_lrc *lrc);
143 
144 u32 xe_lrc_parallel_ggtt_addr(struct xe_lrc *lrc);
145 struct iosys_map xe_lrc_parallel_map(struct xe_lrc *lrc);
146 
147 size_t xe_lrc_reg_size(struct xe_device *xe);
148 size_t xe_lrc_engine_state_size(struct xe_gt *gt, enum xe_engine_class class);
149 
150 void xe_lrc_dump_default(struct drm_printer *p,
151 			 struct xe_gt *gt,
152 			 enum xe_engine_class);
153 int xe_lrc_lookup_default_reg_value(struct xe_gt *gt,
154 				    enum xe_engine_class hwe_class,
155 				    u32 offset,
156 				    u32 *value);
157 
158 u32 *xe_lrc_emit_hwe_state_instructions(struct xe_exec_queue *q, u32 *cs);
159 
160 void xe_lrc_set_multi_queue_priority(struct xe_lrc *lrc, enum xe_multi_queue_priority priority);
161 
162 struct xe_lrc_snapshot *xe_lrc_snapshot_capture(struct xe_lrc *lrc);
163 void xe_lrc_snapshot_capture_delayed(struct xe_lrc_snapshot *snapshot);
164 void xe_lrc_snapshot_print(struct xe_lrc_snapshot *snapshot, struct drm_printer *p);
165 void xe_lrc_snapshot_free(struct xe_lrc_snapshot *snapshot);
166 
167 u32 xe_lrc_ctx_timestamp_ggtt_addr(struct xe_lrc *lrc);
168 u32 xe_lrc_ctx_timestamp_udw_ggtt_addr(struct xe_lrc *lrc);
169 u32 xe_lrc_ctx_job_timestamp_ggtt_addr(struct xe_lrc *lrc);
170 u32 xe_lrc_ctx_job_timestamp(struct xe_lrc *lrc);
171 int xe_lrc_setup_wa_bb_with_scratch(struct xe_lrc *lrc, struct xe_hw_engine *hwe,
172 				    u32 *scratch);
173 
174 /**
175  * xe_lrc_update_timestamp - readout LRC timestamp and update cached value
176  * @lrc: logical ring context for this exec queue
177  * @old_ts: pointer where to save the previous timestamp
178  *
179  * Read the current timestamp for this LRC and update the cached value. The
180  * previous cached value is also returned in @old_ts so the caller can calculate
181  * the delta between 2 updates. Note that this is not intended to be called from
182  * any place, but just by the paths updating the drm client utilization.
183  *
184  * Returns the current LRC timestamp
185  */
186 u64 xe_lrc_update_timestamp(struct xe_lrc *lrc, u64 *old_ts);
187 
188 u64 xe_lrc_timestamp(struct xe_lrc *lrc);
189 
190 #endif
191