libnexus-rv
msg-types.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: Apache 2.0
2 /*
3  * msg-types.h - NexusRV message definitions
4  *
5  * Copyright (C) 2025, Bo Gan <ganboing@gmail.com>
6  */
7 
8 #ifndef LIBNEXUS_RV_MSG_TYPES_H
9 #define LIBNEXUS_RV_MSG_TYPES_H
10 
11 #include <stdint.h>
12 #include <stddef.h>
13 #include <stdbool.h>
17 #define NEXUS_RV_BITS_TCODE 6
19 #define NEXUS_RV_BITS_ETYPE 4
21 #define NEXUS_RV_BITS_RCODE 4
23 #define NEXUS_RV_BITS_EVCODE 4
25 #define NEXUS_RV_BITS_CDF 2
27 #define NEXUS_RV_BITS_ADDR_SYNC 4
29 #define NEXUS_RV_BITS_ADDR_BTYPE 2
31 #define NEXUS_RV_BITS_OWNERSHIP_FMT 2
33 #define NEXUS_RV_BITS_OWNERSHIP_PRV 2
35 #define NEXUS_RV_BITS_OWNERSHIP_V 1
37 #define NEXUS_RV_BITS_CKSRC 4
39 #define NEXUS_RV_BITS_CKDF 2
40 
44  NEXUSRV_TCODE_Ownership = 2,
45  NEXUSRV_TCODE_DirectBranch = 3,
46  NEXUSRV_TCODE_IndirectBranch = 4,
47  NEXUSRV_TCODE_Error = 8,
48  NEXUSRV_TCODE_DataAcquisition = 7,
49  NEXUSRV_TCODE_ProgTraceSync = 9,
50  NEXUSRV_TCODE_DirectBranchSync = 11,
51  NEXUSRV_TCODE_IndirectBranchSync = 12,
52  NEXUSRV_TCODE_ResourceFull = 27,
53  NEXUSRV_TCODE_IndirectBranchHist = 28,
54  NEXUSRV_TCODE_IndirectBranchHistSync = 29,
55  NEXUSRV_TCODE_RepeatBranch = 30,
56  NEXUSRV_TCODE_ICT = 34,
57  NEXUSRV_TCODE_ProgTraceCorrelation = 33,
58  NEXUSRV_TCODE_VendorStart = 56,
59  NEXUSRV_TCODE_VendorLast = 62,
60  NEXUSRV_TCODE_Idle = 63,
61 };
62 
63 static inline const char* nexusrv_tcode_str(enum nexusrv_tcodes tcode) {
64  switch (tcode) {
65  case NEXUSRV_TCODE_Ownership:
66  return "Ownership";
67  case NEXUSRV_TCODE_DirectBranch:
68  return "DirectBranch";
69  case NEXUSRV_TCODE_IndirectBranch:
70  return "IndirectBranch";
71  case NEXUSRV_TCODE_Error:
72  return "Error";
73  case NEXUSRV_TCODE_DataAcquisition:
74  return "DataAcquisition";
75  case NEXUSRV_TCODE_ProgTraceSync:
76  return "ProgTraceSync";
77  case NEXUSRV_TCODE_DirectBranchSync:
78  return "DirectBranchSync";
79  case NEXUSRV_TCODE_IndirectBranchSync:
80  return "IndirectBranchSync";
81  case NEXUSRV_TCODE_ResourceFull:
82  return "ResourceFull";
83  case NEXUSRV_TCODE_IndirectBranchHist:
84  return "IndirectBranchHist";
85  case NEXUSRV_TCODE_IndirectBranchHistSync:
86  return "IndirectBranchHistSync";
87  case NEXUSRV_TCODE_RepeatBranch:
88  return "RepeatBranch";
89  case NEXUSRV_TCODE_ICT:
90  return "ICT";
91  case NEXUSRV_TCODE_ProgTraceCorrelation:
92  return "ProgTraceCorrelation";
93  case NEXUSRV_TCODE_Idle:
94  return "Idle";
95  default:
96  return "Unknown";
97  }
98 }
99 
102 typedef struct nexusrv_msg {
103  uint64_t timestamp;
104  uint16_t src;
105  uint8_t tcode;
106  union {
107  struct {
108  uint8_t sync_type : 4;
109  uint8_t branch_type : 2;
110  };
111  struct {
112  uint8_t error_type : 4;
113  };
114  struct {
115  uint8_t res_code : 4;
116  };
117  struct {
118  uint8_t stop_code : 4;
119  uint8_t cdf : 2;
120  };
121  struct {
122  uint8_t ownership_fmt : 2;
123  uint8_t ownership_priv : 2;
124  uint8_t ownership_v : 1;
125  };
126  struct {
127  uint8_t idtag : 3;
128  };
129  struct {
130  uint8_t cksrc : 4;
131  uint8_t ckdf : 2;
132  };
133  };
134  union {
135  uint32_t icnt;
136  uint32_t error_code;
137  uint32_t res_data;
138  };
139  union {
140  struct {
141  uint32_t hist;
142  uint32_t hrepeat;
143  };
144  uint64_t ckdata0;
145  };
146  union {
147  uint64_t xaddr;
148  uint64_t context;
149  uint64_t dqdata;
150  uint64_t ckdata1;
151  };
153 
154 static inline bool nexusrv_msg_known(const nexusrv_msg *msg) {
155  switch (msg->tcode) {
156  case NEXUSRV_TCODE_Idle:
157  case NEXUSRV_TCODE_ResourceFull:
158  case NEXUSRV_TCODE_DirectBranch:
159  case NEXUSRV_TCODE_DirectBranchSync:
160  case NEXUSRV_TCODE_IndirectBranch:
161  case NEXUSRV_TCODE_IndirectBranchSync:
162  case NEXUSRV_TCODE_IndirectBranchHist:
163  case NEXUSRV_TCODE_IndirectBranchHistSync:
164  case NEXUSRV_TCODE_RepeatBranch:
165  case NEXUSRV_TCODE_Error:
166  case NEXUSRV_TCODE_DataAcquisition:
167  case NEXUSRV_TCODE_Ownership:
168  case NEXUSRV_TCODE_ProgTraceSync:
169  return true;
170  case NEXUSRV_TCODE_ProgTraceCorrelation:
171  return msg->cdf < 2;
172  case NEXUSRV_TCODE_ICT:
173  return msg->cksrc < 2;
174  default:
175  return false;
176  }
177 }
178 
179 static inline bool nexusrv_msg_idle(const nexusrv_msg *msg) {
180  return msg->tcode == NEXUSRV_TCODE_Idle;
181 }
182 
183 static inline bool nexusrv_msg_has_src(const nexusrv_msg *msg) {
184  return !nexusrv_msg_idle(msg);
185 }
186 
187 static inline bool nexusrv_msg_is_branch(const nexusrv_msg *msg) {
188  switch (msg->tcode) {
189  case NEXUSRV_TCODE_DirectBranch:
190  case NEXUSRV_TCODE_DirectBranchSync:
191  case NEXUSRV_TCODE_IndirectBranch:
192  case NEXUSRV_TCODE_IndirectBranchSync:
193  case NEXUSRV_TCODE_IndirectBranchHist:
194  case NEXUSRV_TCODE_IndirectBranchHistSync:
195  return true;
196  default:
197  return false;
198  }
199 }
200 
201 static inline bool nexusrv_msg_is_indir_branch(const nexusrv_msg *msg) {
202  switch (msg->tcode) {
203  case NEXUSRV_TCODE_IndirectBranch:
204  case NEXUSRV_TCODE_IndirectBranchSync:
205  case NEXUSRV_TCODE_IndirectBranchHist:
206  case NEXUSRV_TCODE_IndirectBranchHistSync:
207  return true;
208  default:
209  return false;
210  }
211 }
212 
213 static inline bool nexusrv_msg_is_res(const nexusrv_msg *msg) {
214  return msg->tcode == NEXUSRV_TCODE_ResourceFull;
215 }
216 
217 static inline bool nexusrv_msg_is_sync(const nexusrv_msg *msg) {
218  switch (msg->tcode) {
219  case NEXUSRV_TCODE_DirectBranchSync:
220  case NEXUSRV_TCODE_IndirectBranchSync:
221  case NEXUSRV_TCODE_IndirectBranchHistSync:
222  case NEXUSRV_TCODE_ProgTraceSync:
223  return true;
224  default:
225  return false;
226  }
227 }
228 
229 static inline bool nexusrv_msg_is_error(const nexusrv_msg *msg) {
230  return msg->tcode == NEXUSRV_TCODE_Error;
231 }
232 
233 static inline bool nexusrv_msg_is_stop(const nexusrv_msg *msg) {
234  return msg->tcode == NEXUSRV_TCODE_ProgTraceCorrelation;
235 }
236 
237 static inline bool nexusrv_msg_is_data_acq(const nexusrv_msg *msg) {
238  return msg->tcode == NEXUSRV_TCODE_DataAcquisition;
239 }
240 
241 static inline bool nexusrv_msg_has_icnt(const nexusrv_msg *msg) {
242  switch (msg->tcode) {
243  case NEXUSRV_TCODE_ResourceFull:
244  return !msg->res_code;
245  case NEXUSRV_TCODE_DirectBranch:
246  case NEXUSRV_TCODE_DirectBranchSync:
247  case NEXUSRV_TCODE_IndirectBranch:
248  case NEXUSRV_TCODE_IndirectBranchSync:
249  case NEXUSRV_TCODE_IndirectBranchHist:
250  case NEXUSRV_TCODE_IndirectBranchHistSync:
251  case NEXUSRV_TCODE_ProgTraceSync:
252  case NEXUSRV_TCODE_ProgTraceCorrelation:
253  return true;
254  default:
255  return false;
256  }
257 }
258 
259 static inline bool nexusrv_msg_has_xaddr(const nexusrv_msg *msg) {
260  switch (msg->tcode) {
261  case NEXUSRV_TCODE_IndirectBranch:
262  case NEXUSRV_TCODE_IndirectBranchSync:
263  case NEXUSRV_TCODE_IndirectBranchHist:
264  case NEXUSRV_TCODE_IndirectBranchHistSync:
265  case NEXUSRV_TCODE_DirectBranchSync:
266  case NEXUSRV_TCODE_ProgTraceSync:
267  return true;
268  default:
269  return false;
270  }
271 }
272 
273 static inline bool nexusrv_msg_has_hist(const nexusrv_msg *msg) {
274  switch (msg->tcode) {
275  case NEXUSRV_TCODE_ResourceFull:
276  return msg->res_code == 1 || msg->res_code == 2;
277  case NEXUSRV_TCODE_ProgTraceCorrelation:
278  return msg->cdf == 1;
279  case NEXUSRV_TCODE_IndirectBranchHist:
280  case NEXUSRV_TCODE_IndirectBranchHistSync:
281  return true;
282  default:
283  return false;
284  }
285 }
286 
287 static inline bool nexusrv_msg_known_rescode(uint8_t rescode) {
288  return rescode < 3;
289 }
290 
291 static inline unsigned nexusrv_msg_hist_bits(uint32_t hist) {
292  if (!hist)
293  return 0;
294  return sizeof(long) * 8 - 1 - __builtin_clzl(hist);
295 }
296 
297 #endif
struct nexusrv_msg nexusrv_msg
Decoded NexusRV Message.
nexusrv_tcodes
TCODE enumeration.
Definition: msg-types.h:43
Decoded NexusRV Message.
Definition: msg-types.h:102
uint8_t tcode
Definition: msg-types.h:105
uint32_t error_code
Definition: msg-types.h:136
uint32_t icnt
Definition: msg-types.h:135
uint64_t context
Definition: msg-types.h:148
uint8_t branch_type
Definition: msg-types.h:109
uint8_t ownership_priv
Definition: msg-types.h:123
uint8_t ownership_fmt
Definition: msg-types.h:122
uint8_t ownership_v
Definition: msg-types.h:124
uint64_t ckdata0
Definition: msg-types.h:144
uint32_t hrepeat
Definition: msg-types.h:142
uint8_t idtag
Definition: msg-types.h:127
uint16_t src
Definition: msg-types.h:104
uint64_t dqdata
Definition: msg-types.h:149
uint8_t res_code
Definition: msg-types.h:115
uint8_t sync_type
Definition: msg-types.h:108
uint8_t cdf
Definition: msg-types.h:119
uint64_t xaddr
Definition: msg-types.h:147
uint32_t res_data
Definition: msg-types.h:137
uint8_t stop_code
Definition: msg-types.h:118
uint32_t hist
Definition: msg-types.h:141
uint64_t timestamp
Definition: msg-types.h:103
uint64_t ckdata1
Definition: msg-types.h:150
uint8_t ckdf
Definition: msg-types.h:131
uint8_t error_type
Definition: msg-types.h:112
uint8_t cksrc
Definition: msg-types.h:130