xref: /linux/drivers/gpu/drm/xe/xe_rtp.h (revision dc72c52a42e0255441bed7444ab16f2b6c98b681)
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Copyright © 2022 Intel Corporation
4  */
5 
6 #ifndef _XE_RTP_
7 #define _XE_RTP_
8 
9 #include <linux/types.h>
10 #include <linux/xarray.h>
11 
12 #define _XE_RTP_INCLUDE_PRIVATE_HELPERS
13 
14 #include "xe_rtp_helpers.h"
15 #include "xe_rtp_types.h"
16 
17 #undef _XE_RTP_INCLUDE_PRIVATE_HELPERS
18 
19 /*
20  * Register table poke infrastructure
21  */
22 
23 struct xe_hw_engine;
24 struct xe_gt;
25 struct xe_reg_sr;
26 
27 /*
28  * Macros to encode rules to match against platform, IP version, stepping, etc.
29  * Shouldn't be used directly - see XE_RTP_RULES()
30  */
31 #define _XE_RTP_RULE_PLATFORM(plat__)						\
32 	{ .match_type = XE_RTP_MATCH_PLATFORM, .platform = plat__ }
33 
34 #define _XE_RTP_RULE_SUBPLATFORM(plat__, sub__)					\
35 	{ .match_type = XE_RTP_MATCH_SUBPLATFORM,				\
36 	  .platform = plat__, .subplatform = sub__ }
37 
38 #define _XE_RTP_RULE_GRAPHICS_STEP(start__, end__)				\
39 	{ .match_type = XE_RTP_MATCH_GRAPHICS_STEP,				\
40 	  .step_start = start__, .step_end = end__ }
41 
42 #define _XE_RTP_RULE_MEDIA_STEP(start__, end__)					\
43 	{ .match_type = XE_RTP_MATCH_MEDIA_STEP,				\
44 	  .step_start = start__, .step_end = end__ }
45 
46 #define _XE_RTP_RULE_ENGINE_CLASS(cls__)					\
47 	{ .match_type = XE_RTP_MATCH_ENGINE_CLASS,				\
48 	  .engine_class = (cls__) }
49 
50 /**
51  * XE_RTP_RULE_PLATFORM - Create rule matching platform
52  * @plat_: platform to match
53  *
54  * Refer to XE_RTP_RULES() for expected usage.
55  */
56 #define XE_RTP_RULE_PLATFORM(plat_)						\
57 	_XE_RTP_RULE_PLATFORM(XE_##plat_)
58 
59 /**
60  * XE_RTP_RULE_SUBPLATFORM - Create rule matching platform and sub-platform
61  * @plat_: platform to match
62  * @sub_: sub-platform to match
63  *
64  * Refer to XE_RTP_RULES() for expected usage.
65  */
66 #define XE_RTP_RULE_SUBPLATFORM(plat_, sub_)					\
67 	_XE_RTP_RULE_SUBPLATFORM(XE_##plat_, XE_SUBPLATFORM_##plat_##_##sub_)
68 
69 /**
70  * XE_RTP_RULE_GRAPHICS_STEP - Create rule matching graphics stepping
71  * @start_: First stepping matching the rule
72  * @end_: First stepping that does not match the rule
73  *
74  * Note that the range matching this rule is [ @start_, @end_ ), i.e. inclusive
75  * on the left, exclusive on the right.
76  *
77  * Refer to XE_RTP_RULES() for expected usage.
78  */
79 #define XE_RTP_RULE_GRAPHICS_STEP(start_, end_)					\
80 	_XE_RTP_RULE_GRAPHICS_STEP(STEP_##start_, STEP_##end_)
81 
82 /**
83  * XE_RTP_RULE_MEDIA_STEP - Create rule matching media stepping
84  * @start_: First stepping matching the rule
85  * @end_: First stepping that does not match the rule
86  *
87  * Note that the range matching this rule is [ @start_, @end_ ), i.e. inclusive
88  * on the left, exclusive on the right.
89  *
90  * Refer to XE_RTP_RULES() for expected usage.
91  */
92 #define XE_RTP_RULE_MEDIA_STEP(start_, end_)					\
93 	_XE_RTP_RULE_MEDIA_STEP(STEP_##start_, STEP_##end_)
94 
95 /**
96  * XE_RTP_RULE_ENGINE_CLASS - Create rule matching an engine class
97  * @cls_: Engine class to match
98  *
99  * Refer to XE_RTP_RULES() for expected usage.
100  */
101 #define XE_RTP_RULE_ENGINE_CLASS(cls_)						\
102 	_XE_RTP_RULE_ENGINE_CLASS(XE_ENGINE_CLASS_##cls_)
103 
104 /**
105  * XE_RTP_RULE_FUNC - Create rule using callback function for match
106  * @func__: Function to call to decide if rule matches
107  *
108  * This allows more complex checks to be performed. The ``XE_RTP``
109  * infrastructure will simply call the function @func_ passed to decide if this
110  * rule matches the device.
111  *
112  * Refer to XE_RTP_RULES() for expected usage.
113  */
114 #define XE_RTP_RULE_FUNC(func__)						\
115 	{ .match_type = XE_RTP_MATCH_FUNC,					\
116 	  .match_func = (func__) }
117 
118 /**
119  * XE_RTP_RULE_GRAPHICS_VERSION - Create rule matching graphics version
120  * @ver__: Graphics IP version to match
121  *
122  * Refer to XE_RTP_RULES() for expected usage.
123  */
124 #define XE_RTP_RULE_GRAPHICS_VERSION(ver__)					\
125 	{ .match_type = XE_RTP_MATCH_GRAPHICS_VERSION,				\
126 	  .ver_start = ver__, }
127 
128 /**
129  * XE_RTP_RULE_GRAPHICS_VERSION_RANGE - Create rule matching a range of graphics version
130  * @ver_start__: First graphics IP version to match
131  * @ver_end__: Last graphics IP version to match
132  *
133  * Note that the range matching this rule is [ @ver_start__, @ver_end__ ], i.e.
134  * inclusive on boths sides
135  *
136  * Refer to XE_RTP_RULES() for expected usage.
137  */
138 #define XE_RTP_RULE_GRAPHICS_VERSION_RANGE(ver_start__, ver_end__)		\
139 	{ .match_type = XE_RTP_MATCH_GRAPHICS_VERSION_RANGE,			\
140 	  .ver_start = ver_start__, .ver_end = ver_end__, }
141 
142 /**
143  * XE_RTP_RULE_MEDIA_VERSION - Create rule matching media version
144  * @ver__: Graphics IP version to match
145  *
146  * Refer to XE_RTP_RULES() for expected usage.
147  */
148 #define XE_RTP_RULE_MEDIA_VERSION(ver__)					\
149 	{ .match_type = XE_RTP_MATCH_MEDIA_VERSION,				\
150 	  .ver_start = ver__, }
151 
152 /**
153  * XE_RTP_RULE_MEDIA_VERSION_RANGE - Create rule matching a range of media version
154  * @ver_start__: First media IP version to match
155  * @ver_end__: Last media IP version to match
156  *
157  * Note that the range matching this rule is [ @ver_start__, @ver_end__ ], i.e.
158  * inclusive on boths sides
159  *
160  * Refer to XE_RTP_RULES() for expected usage.
161  */
162 #define XE_RTP_RULE_MEDIA_VERSION_RANGE(ver_start__, ver_end__)			\
163 	{ .match_type = XE_RTP_MATCH_MEDIA_VERSION_RANGE,			\
164 	  .ver_start = ver_start__, .ver_end = ver_end__, }
165 
166 /**
167  * XE_RTP_RULE_IS_INTEGRATED - Create a rule matching integrated graphics devices
168  *
169  * Refer to XE_RTP_RULES() for expected usage.
170  */
171 #define XE_RTP_RULE_IS_INTEGRATED						\
172 	{ .match_type = XE_RTP_MATCH_INTEGRATED }
173 
174 /**
175  * XE_RTP_RULE_IS_DISCRETE - Create a rule matching discrete graphics devices
176  *
177  * Refer to XE_RTP_RULES() for expected usage.
178  */
179 #define XE_RTP_RULE_IS_DISCRETE							\
180 	{ .match_type = XE_RTP_MATCH_DISCRETE }
181 
182 /**
183  * XE_RTP_RULE_OR - Create an OR condition for rtp rules
184  *
185  * RTP rules are AND'ed when evaluated and all of them need to match.
186  * XE_RTP_RULE_OR allows to create set of rules where any of them matching is
187  * sufficient for the action to trigger. Example:
188  *
189  * .. code-block:: c
190  *
191  *	const struct xe_rtp_entry_sr entries[] = {
192  *		...
193  *		{ XE_RTP_NAME("test-entry"),
194  *		  XE_RTP_RULES(PLATFORM(DG2), OR, PLATFORM(TIGERLAKE)),
195  *		  ...
196  *		},
197  *		...
198  *	};
199  */
200 #define XE_RTP_RULE_OR								\
201 	{ .match_type = XE_RTP_MATCH_OR }
202 
203 /**
204  * XE_RTP_ACTION_WR - Helper to write a value to the register, overriding all
205  *                    the bits
206  * @reg_: Register
207  * @val_: Value to set
208  * @...: Additional fields to override in the struct xe_rtp_action entry
209  *
210  * The correspondent notation in bspec is:
211  *
212  *	REGNAME = VALUE
213  */
214 #define XE_RTP_ACTION_WR(reg_, val_, ...)					\
215 	{ .reg = XE_RTP_DROP_CAST(reg_),					\
216 	  .clr_bits = ~0u, .set_bits = (val_),					\
217 	  .read_mask = (~0u), ##__VA_ARGS__ }
218 
219 /**
220  * XE_RTP_ACTION_SET - Set bits from @val_ in the register.
221  * @reg_: Register
222  * @val_: Bits to set in the register
223  * @...: Additional fields to override in the struct xe_rtp_action entry
224  *
225  * For masked registers this translates to a single write, while for other
226  * registers it's a RMW. The correspondent bspec notation is (example for bits 2
227  * and 5, but could be any):
228  *
229  *	REGNAME[2] = 1
230  *	REGNAME[5] = 1
231  */
232 #define XE_RTP_ACTION_SET(reg_, val_, ...)					\
233 	{ .reg = XE_RTP_DROP_CAST(reg_),					\
234 	  .clr_bits = val_, .set_bits = val_,					\
235 	  .read_mask = val_, ##__VA_ARGS__ }
236 
237 /**
238  * XE_RTP_ACTION_CLR: Clear bits from @val_ in the register.
239  * @reg_: Register
240  * @val_: Bits to clear in the register
241  * @...: Additional fields to override in the struct xe_rtp_action entry
242  *
243  * For masked registers this translates to a single write, while for other
244  * registers it's a RMW. The correspondent bspec notation is (example for bits 2
245  * and 5, but could be any):
246  *
247  *	REGNAME[2] = 0
248  *	REGNAME[5] = 0
249  */
250 #define XE_RTP_ACTION_CLR(reg_, val_, ...)					\
251 	{ .reg = XE_RTP_DROP_CAST(reg_),					\
252 	  .clr_bits = val_, .set_bits = 0,					\
253 	  .read_mask = val_, ##__VA_ARGS__ }
254 
255 /**
256  * XE_RTP_ACTION_FIELD_SET: Set a bit range
257  * @reg_: Register
258  * @mask_bits_: Mask of bits to be changed in the register, forming a field
259  * @val_: Value to set in the field denoted by @mask_bits_
260  * @...: Additional fields to override in the struct xe_rtp_action entry
261  *
262  * For masked registers this translates to a single write, while for other
263  * registers it's a RMW. The correspondent bspec notation is:
264  *
265  *	REGNAME[<end>:<start>] = VALUE
266  */
267 #define XE_RTP_ACTION_FIELD_SET(reg_, mask_bits_, val_, ...)			\
268 	{ .reg = XE_RTP_DROP_CAST(reg_),					\
269 	  .clr_bits = mask_bits_, .set_bits = val_,				\
270 	  .read_mask = mask_bits_, ##__VA_ARGS__ }
271 
272 #define XE_RTP_ACTION_FIELD_SET_NO_READ_MASK(reg_, mask_bits_, val_, ...)	\
273 	{ .reg = XE_RTP_DROP_CAST(reg_),					\
274 	  .clr_bits = (mask_bits_), .set_bits = (val_),				\
275 	  .read_mask = 0, ##__VA_ARGS__ }
276 
277 /**
278  * XE_RTP_ACTION_WHITELIST - Add register to userspace whitelist
279  * @reg_: Register
280  * @val_: Whitelist-specific flags to set
281  * @...: Additional fields to override in the struct xe_rtp_action entry
282  *
283  * Add a register to the whitelist, allowing userspace to modify the ster with
284  * regular user privileges.
285  */
286 #define XE_RTP_ACTION_WHITELIST(reg_, val_, ...)				\
287 	/* TODO fail build if ((flags) & ~(RING_FORCE_TO_NONPRIV_MASK_VALID)) */\
288 	{ .reg = XE_RTP_DROP_CAST(reg_),					\
289 	  .set_bits = val_,							\
290 	  .clr_bits = RING_FORCE_TO_NONPRIV_MASK_VALID,				\
291 	  ##__VA_ARGS__ }
292 
293 /**
294  * XE_RTP_NAME - Helper to set the name in xe_rtp_entry
295  * @s_: Name describing this rule, often a HW-specific number
296  *
297  * TODO: maybe move this behind a debug config?
298  */
299 #define XE_RTP_NAME(s_)	.name = (s_)
300 
301 /**
302  * XE_RTP_ENTRY_FLAG - Helper to add multiple flags to a struct xe_rtp_entry_sr
303  * @...: Entry flags, without the ``XE_RTP_ENTRY_FLAG_`` prefix
304  *
305  * Helper to automatically add a ``XE_RTP_ENTRY_FLAG_`` prefix to the flags
306  * when defining struct xe_rtp_entry entries. Example:
307  *
308  * .. code-block:: c
309  *
310  *	const struct xe_rtp_entry_sr wa_entries[] = {
311  *		...
312  *		{ XE_RTP_NAME("test-entry"),
313  *		  ...
314  *		  XE_RTP_ENTRY_FLAG(FOREACH_ENGINE),
315  *		  ...
316  *		},
317  *		...
318  *	};
319  */
320 #define XE_RTP_ENTRY_FLAG(...)							\
321 	.flags = (XE_RTP_PASTE_FOREACH(ENTRY_FLAG_, BITWISE_OR, (__VA_ARGS__)))
322 
323 /**
324  * XE_RTP_ACTION_FLAG - Helper to add multiple flags to a struct xe_rtp_action
325  * @...: Action flags, without the ``XE_RTP_ACTION_FLAG_`` prefix
326  *
327  * Helper to automatically add a ``XE_RTP_ACTION_FLAG_`` prefix to the flags
328  * when defining struct xe_rtp_action entries. Example:
329  *
330  * .. code-block:: c
331  *
332  *	const struct xe_rtp_entry_sr wa_entries[] = {
333  *		...
334  *		{ XE_RTP_NAME("test-entry"),
335  *		  ...
336  *		  XE_RTP_ACTION_SET(..., XE_RTP_ACTION_FLAG(FOREACH_ENGINE)),
337  *		  ...
338  *		},
339  *		...
340  *	};
341  */
342 #define XE_RTP_ACTION_FLAG(...)							\
343 	.flags = (XE_RTP_PASTE_FOREACH(ACTION_FLAG_, BITWISE_OR, (__VA_ARGS__)))
344 
345 /**
346  * XE_RTP_RULES - Helper to set multiple rules to a struct xe_rtp_entry_sr entry
347  * @...: Rules
348  *
349  * At least one rule is needed and up to 6 are supported. Multiple rules are
350  * AND'ed together, i.e. all the rules must evaluate to true for the entry to
351  * be processed. See XE_RTP_MATCH_* for the possible match rules. Example:
352  *
353  * .. code-block:: c
354  *
355  *	const struct xe_rtp_entry_sr wa_entries[] = {
356  *		...
357  *		{ XE_RTP_NAME("test-entry"),
358  *		  XE_RTP_RULES(SUBPLATFORM(DG2, G10), GRAPHICS_STEP(A0, B0)),
359  *		  ...
360  *		},
361  *		...
362  *	};
363  */
364 #define XE_RTP_RULES(...)							\
365 	.n_rules = COUNT_ARGS(__VA_ARGS__),					\
366 	.rules = (const struct xe_rtp_rule[]) {					\
367 		XE_RTP_PASTE_FOREACH(RULE_, COMMA, (__VA_ARGS__))	\
368 	}
369 
370 /**
371  * XE_RTP_ACTIONS - Helper to set multiple actions to a struct xe_rtp_entry_sr
372  * @...: Actions to be taken
373  *
374  * At least one action is needed and up to 6 are supported. See XE_RTP_ACTION_*
375  * for the possible actions. Example:
376  *
377  * .. code-block:: c
378  *
379  *	const struct xe_rtp_entry_sr wa_entries[] = {
380  *		...
381  *		{ XE_RTP_NAME("test-entry"),
382  *		  XE_RTP_RULES(...),
383  *		  XE_RTP_ACTIONS(SET(..), SET(...), CLR(...)),
384  *		  ...
385  *		},
386  *		...
387  *	};
388  */
389 #define XE_RTP_ACTIONS(...)							\
390 	.n_actions = COUNT_ARGS(__VA_ARGS__),					\
391 	.actions = (const struct xe_rtp_action[]) {				\
392 		XE_RTP_PASTE_FOREACH(ACTION_, COMMA, (__VA_ARGS__))	\
393 	}
394 
395 #define XE_RTP_PROCESS_CTX_INITIALIZER(arg__) _Generic((arg__),							\
396 	struct xe_hw_engine * :	(struct xe_rtp_process_ctx){ { (void *)(arg__) }, XE_RTP_PROCESS_TYPE_ENGINE },	\
397 	struct xe_gt * :	(struct xe_rtp_process_ctx){ { (void *)(arg__) }, XE_RTP_PROCESS_TYPE_GT })
398 
399 void xe_rtp_process_ctx_enable_active_tracking(struct xe_rtp_process_ctx *ctx,
400 					       unsigned long *active_entries,
401 					       size_t n_entries);
402 
403 void xe_rtp_process_to_sr(struct xe_rtp_process_ctx *ctx,
404 			  const struct xe_rtp_entry_sr *entries,
405 			  struct xe_reg_sr *sr);
406 
407 void xe_rtp_process(struct xe_rtp_process_ctx *ctx,
408 		    const struct xe_rtp_entry *entries);
409 
410 /* Match functions to be used with XE_RTP_MATCH_FUNC */
411 
412 /**
413  * xe_rtp_match_even_instance - Match if engine instance is even
414  * @gt: GT structure
415  * @hwe: Engine instance
416  *
417  * Returns: true if engine instance is even, false otherwise
418  */
419 bool xe_rtp_match_even_instance(const struct xe_gt *gt,
420 				const struct xe_hw_engine *hwe);
421 
422 /*
423  * xe_rtp_match_first_render_or_compute - Match if it's first render or compute
424  * engine in the GT
425  *
426  * @gt: GT structure
427  * @hwe: Engine instance
428  *
429  * Registers on the render reset domain need to have their values re-applied
430  * when any of those engines are reset. Since the engines reset together, a
431  * programming can be set to just one of them. For simplicity the first engine
432  * of either render or compute class can be chosen.
433  *
434  * Returns: true if engine id is the first to match the render reset domain,
435  * false otherwise.
436  */
437 bool xe_rtp_match_first_render_or_compute(const struct xe_gt *gt,
438 					  const struct xe_hw_engine *hwe);
439 
440 /*
441  * xe_rtp_match_first_gslice_fused_off - Match when first gslice is fused off
442  *
443  * @gt: GT structure
444  * @hwe: Engine instance
445  *
446  * Returns: true if first gslice is fused off, false otherwise.
447  */
448 bool xe_rtp_match_first_gslice_fused_off(const struct xe_gt *gt,
449 					 const struct xe_hw_engine *hwe);
450 
451 #endif
452