1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3 * Copyright 2025 NXP
4 */
5
6 #ifndef __SBI_MPXY_RPMI_H
7 #define __SBI_MPXY_RPMI_H
8
9 #if defined(CFG_RISCV_SBI_MPXY_RPMI)
10
11 #ifndef __ASSEMBLER__
12
13 #include <compiler.h>
14 #include <encoding.h>
15 #include <rpmi.h>
16 #include <sbi_mpxy.h>
17 #include <stdint.h>
18 #include <sys/cdefs.h>
19 #include <types_ext.h>
20 #include <util.h>
21
22 /* RPMI message protocol specific MPXY attributes */
23 enum sbi_mpxy_rpmi_attribute_id {
24 SBI_MPXY_RPMI_ATTR_SERVICEGROUP_ID = SBI_MPXY_ATTR_MSGPROTO_ATTR_START,
25 SBI_MPXY_RPMI_ATTR_SERVICEGROUP_VERSION,
26 SBI_MPXY_RPMI_ATTR_IMPLEMENTATION_ID,
27 SBI_MPXY_RPMI_ATTR_IMPLEMENTATION_VERSION,
28 SBI_MPXY_RPMI_ATTR_MAX_ID
29 };
30
31 /* RPMI message protocol specific MPXY message types */
32 enum sbi_mpxy_rpmi_message_type {
33 SBI_MPXY_RPMI_MSG_TYPE_GET_ATTRIBUTE,
34 SBI_MPXY_RPMI_MSG_TYPE_SET_ATTRIBUTE,
35 SBI_MPXY_RPMI_MSG_TYPE_SEND_WITH_RESPONSE,
36 SBI_MPXY_RPMI_MSG_TYPE_SEND_WITHOUT_RESPONSE,
37 SBI_MPXY_RPMI_MSG_TYPE_NOTIFICATION_EVENT,
38 SBI_MPXY_RPMI_MSG_MAX_TYPE
39 };
40
41 /* RPMI specific SBI MPXY channel attributes. */
42 struct sbi_mpxy_rpmi_channel_attrs {
43 uint32_t servicegroup_id;
44 uint32_t servicegroup_version;
45 uint32_t implementation_id;
46 uint32_t implementation_version;
47 };
48
49 /* RPMI/MPXY message instance */
50 struct sbi_mpxy_rpmi_message {
51 enum sbi_mpxy_rpmi_message_type type;
52 union {
53 struct {
54 enum sbi_mpxy_rpmi_attribute_id id;
55 uint32_t value;
56 } attribute;
57
58 struct {
59 uint32_t service_id;
60 void *request;
61 unsigned long request_len;
62 void *response;
63 unsigned long response_len;
64 unsigned long max_response_len;
65 } data;
66
67 struct {
68 uint16_t event_datalen;
69 uint8_t event_id;
70 uint8_t *event_data;
71 } notification;
72 };
73 int error;
74 };
75
76 /*
77 * struct sbi_mpxy_rpmi_channel - RPMI-capable MPXY channel descriptor
78 * @hart_id: Logical hart ID associated with the channel (if applicable)
79 * @channel_id: MPXY channel ID as returned by SBI
80 * @attrs: Basic MPXY channel attributes (protocol ID, max len, etc.)
81 * @rpmi_attrs: RPMI-specific channel attributes (service group, MSI, etc.)
82 * @notif: Pointer to allocated buffer for receiving notifications
83 */
84 struct sbi_mpxy_rpmi_channel {
85 uint32_t hart_id;
86 uint32_t channel_id;
87 struct sbi_mpxy_channel_attrs attrs;
88 struct sbi_mpxy_rpmi_channel_attrs rpmi_attrs;
89 struct sbi_mpxy_notification_data *notif;
90 };
91
92 /* An instance of RPMI-over-MPXY channel group */
93 struct sbi_mpxy_rpmi_context {
94 uint32_t channel_count;
95 struct sbi_mpxy_rpmi_channel *channels;
96 };
97
98 /* Forward declaration of the RPMI context */
99 extern struct sbi_mpxy_rpmi_context *sbi_mpxy_rpmi_ctx;
100
101 /** RPMI/MPXY message helper routines */
102
103 static inline void
sbi_mpxy_rpmi_init_get_attribute(struct sbi_mpxy_rpmi_message * message,enum sbi_mpxy_rpmi_attribute_id id)104 sbi_mpxy_rpmi_init_get_attribute(struct sbi_mpxy_rpmi_message *message,
105 enum sbi_mpxy_rpmi_attribute_id id)
106 {
107 message->type = SBI_MPXY_RPMI_MSG_TYPE_GET_ATTRIBUTE;
108 message->attribute.id = id;
109 message->attribute.value = 0;
110 message->error = 0;
111 }
112
113 static inline void
sbi_mpxy_rpmi_init_set_attribute(struct sbi_mpxy_rpmi_message * message,enum sbi_mpxy_rpmi_attribute_id id,uint32_t value)114 sbi_mpxy_rpmi_init_set_attribute(struct sbi_mpxy_rpmi_message *message,
115 enum sbi_mpxy_rpmi_attribute_id id,
116 uint32_t value)
117 {
118 message->type = SBI_MPXY_RPMI_MSG_TYPE_SET_ATTRIBUTE;
119 message->attribute.id = id;
120 message->attribute.value = value;
121 message->error = 0;
122 }
123
124 static inline void
sbi_mpxy_rpmi_init_send_with_response(struct sbi_mpxy_rpmi_message * message,uint32_t service_id,void * request,unsigned long request_len,void * response,unsigned long max_response_len)125 sbi_mpxy_rpmi_init_send_with_response(struct sbi_mpxy_rpmi_message *message,
126 uint32_t service_id, void *request,
127 unsigned long request_len, void *response,
128 unsigned long max_response_len)
129 {
130 message->type = SBI_MPXY_RPMI_MSG_TYPE_SEND_WITH_RESPONSE;
131 message->data.service_id = service_id;
132 message->data.request = request;
133 message->data.request_len = request_len;
134 message->data.response = response;
135 message->data.response_len = 0;
136 message->data.max_response_len = max_response_len;
137 message->error = 0;
138 }
139
140 static inline void
sbi_mpxy_rpmi_init_send_without_response(struct sbi_mpxy_rpmi_message * message,uint32_t service_id,void * request,unsigned long request_len)141 sbi_mpxy_rpmi_init_send_without_response(struct sbi_mpxy_rpmi_message *message,
142 uint32_t service_id, void *request,
143 unsigned long request_len)
144 {
145 message->type = SBI_MPXY_RPMI_MSG_TYPE_SEND_WITHOUT_RESPONSE;
146 message->data.service_id = service_id;
147 message->data.request = request;
148 message->data.request_len = request_len;
149 message->data.response = NULL;
150 message->data.max_response_len = 0;
151 message->data.response_len = 0;
152 message->error = 0;
153 }
154
155 void sbi_mpxy_rpmi_probe_channels(void);
156 int sbi_mpxy_rpmi_read_attributes(struct sbi_mpxy_rpmi_channel *channel);
157 int sbi_mpxy_rpmi_send_data(struct sbi_mpxy_rpmi_channel *channel, void *data);
158
159 #endif /*__ASSEMBLER__*/
160 #endif /*defined(CFG_RISCV_SBI_MPXY_RPMI)*/
161 #endif /*__SBI_MPXY_RPMI_H*/
162