xref: /linux/drivers/gpu/drm/xe/xe_guc_hxg_helpers.h (revision 50a0844bf8c4d38be540e423672ef9408d029252)
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Copyright © 2023 Intel Corporation
4  */
5 
6 #ifndef _XE_GUC_HXG_HELPERS_H_
7 #define _XE_GUC_HXG_HELPERS_H_
8 
9 #include <linux/bitfield.h>
10 #include <linux/types.h>
11 
12 #include "abi/guc_messages_abi.h"
13 
14 /**
15  * hxg_sizeof - Queries size of the object or type (in HXG units).
16  * @T: the object or type
17  *
18  * Force a compilation error if actual size is not aligned to HXG unit (u32).
19  *
20  * Return: size in dwords (u32).
21  */
22 #define hxg_sizeof(T)	(sizeof(T) / sizeof(u32) + BUILD_BUG_ON_ZERO(sizeof(T) % sizeof(u32)))
23 
24 static inline const char *guc_hxg_type_to_string(unsigned int type)
25 {
26 	switch (type) {
27 	case GUC_HXG_TYPE_REQUEST:
28 		return "request";
29 	case GUC_HXG_TYPE_FAST_REQUEST:
30 		return "fast-request";
31 	case GUC_HXG_TYPE_EVENT:
32 		return "event";
33 	case GUC_HXG_TYPE_NO_RESPONSE_BUSY:
34 		return "busy";
35 	case GUC_HXG_TYPE_NO_RESPONSE_RETRY:
36 		return "retry";
37 	case GUC_HXG_TYPE_RESPONSE_FAILURE:
38 		return "failure";
39 	case GUC_HXG_TYPE_RESPONSE_SUCCESS:
40 		return "response";
41 	default:
42 		return "<invalid>";
43 	}
44 }
45 
46 static inline bool guc_hxg_type_is_action(unsigned int type)
47 {
48 	switch (type) {
49 	case GUC_HXG_TYPE_REQUEST:
50 	case GUC_HXG_TYPE_FAST_REQUEST:
51 	case GUC_HXG_TYPE_EVENT:
52 		return true;
53 	default:
54 		return false;
55 	}
56 }
57 
58 static inline bool guc_hxg_type_is_reply(unsigned int type)
59 {
60 	switch (type) {
61 	case GUC_HXG_TYPE_NO_RESPONSE_BUSY:
62 	case GUC_HXG_TYPE_NO_RESPONSE_RETRY:
63 	case GUC_HXG_TYPE_RESPONSE_FAILURE:
64 	case GUC_HXG_TYPE_RESPONSE_SUCCESS:
65 		return true;
66 	default:
67 		return false;
68 	}
69 }
70 
71 static inline u32 guc_hxg_msg_encode_success(u32 *msg, u32 data0)
72 {
73 	msg[0] = FIELD_PREP(GUC_HXG_MSG_0_ORIGIN, GUC_HXG_ORIGIN_HOST) |
74 		 FIELD_PREP(GUC_HXG_MSG_0_TYPE, GUC_HXG_TYPE_RESPONSE_SUCCESS) |
75 		 FIELD_PREP(GUC_HXG_RESPONSE_MSG_0_DATA0, data0);
76 
77 	return GUC_HXG_RESPONSE_MSG_MIN_LEN;
78 }
79 
80 static inline u32 guc_hxg_msg_encode_failure(u32 *msg, u32 error, u32 hint)
81 {
82 	msg[0] = FIELD_PREP(GUC_HXG_MSG_0_ORIGIN, GUC_HXG_ORIGIN_HOST) |
83 		 FIELD_PREP(GUC_HXG_MSG_0_TYPE, GUC_HXG_TYPE_RESPONSE_FAILURE) |
84 		 FIELD_PREP(GUC_HXG_FAILURE_MSG_0_HINT, hint) |
85 		 FIELD_PREP(GUC_HXG_FAILURE_MSG_0_ERROR, error);
86 
87 	return GUC_HXG_FAILURE_MSG_LEN;
88 }
89 
90 static inline u32 guc_hxg_msg_encode_busy(u32 *msg, u32 counter)
91 {
92 	msg[0] = FIELD_PREP(GUC_HXG_MSG_0_ORIGIN, GUC_HXG_ORIGIN_HOST) |
93 		 FIELD_PREP(GUC_HXG_MSG_0_TYPE, GUC_HXG_TYPE_NO_RESPONSE_BUSY) |
94 		 FIELD_PREP(GUC_HXG_BUSY_MSG_0_COUNTER, counter);
95 
96 	return GUC_HXG_BUSY_MSG_LEN;
97 }
98 
99 static inline u32 guc_hxg_msg_encode_retry(u32 *msg, u32 reason)
100 {
101 	msg[0] = FIELD_PREP(GUC_HXG_MSG_0_ORIGIN, GUC_HXG_ORIGIN_HOST) |
102 		 FIELD_PREP(GUC_HXG_MSG_0_TYPE, GUC_HXG_TYPE_NO_RESPONSE_RETRY) |
103 		 FIELD_PREP(GUC_HXG_RETRY_MSG_0_REASON, reason);
104 
105 	return GUC_HXG_RETRY_MSG_LEN;
106 }
107 
108 #endif
109