1
2
3
4
5
6
7
8
9#ifndef _SCMI_COMMON_H
10#define _SCMI_COMMON_H
11
12#include <linux/bitfield.h>
13#include <linux/completion.h>
14#include <linux/device.h>
15#include <linux/errno.h>
16#include <linux/kernel.h>
17#include <linux/hashtable.h>
18#include <linux/list.h>
19#include <linux/module.h>
20#include <linux/refcount.h>
21#include <linux/scmi_protocol.h>
22#include <linux/spinlock.h>
23#include <linux/types.h>
24
25#include <asm/unaligned.h>
26
27#include "notify.h"
28
29#define PROTOCOL_REV_MINOR_MASK GENMASK(15, 0)
30#define PROTOCOL_REV_MAJOR_MASK GENMASK(31, 16)
31#define PROTOCOL_REV_MAJOR(x) (u16)(FIELD_GET(PROTOCOL_REV_MAJOR_MASK, (x)))
32#define PROTOCOL_REV_MINOR(x) (u16)(FIELD_GET(PROTOCOL_REV_MINOR_MASK, (x)))
33#define MAX_PROTOCOLS_IMP 16
34#define MAX_OPPS 16
35
36enum scmi_common_cmd {
37 PROTOCOL_VERSION = 0x0,
38 PROTOCOL_ATTRIBUTES = 0x1,
39 PROTOCOL_MESSAGE_ATTRIBUTES = 0x2,
40};
41
42
43
44
45
46
47
48
49
50
51
52
53
54struct scmi_msg_resp_prot_version {
55 __le16 minor_version;
56 __le16 major_version;
57};
58
59#define MSG_ID_MASK GENMASK(7, 0)
60#define MSG_XTRACT_ID(hdr) FIELD_GET(MSG_ID_MASK, (hdr))
61#define MSG_TYPE_MASK GENMASK(9, 8)
62#define MSG_XTRACT_TYPE(hdr) FIELD_GET(MSG_TYPE_MASK, (hdr))
63#define MSG_TYPE_COMMAND 0
64#define MSG_TYPE_DELAYED_RESP 2
65#define MSG_TYPE_NOTIFICATION 3
66#define MSG_PROTOCOL_ID_MASK GENMASK(17, 10)
67#define MSG_XTRACT_PROT_ID(hdr) FIELD_GET(MSG_PROTOCOL_ID_MASK, (hdr))
68#define MSG_TOKEN_ID_MASK GENMASK(27, 18)
69#define MSG_XTRACT_TOKEN(hdr) FIELD_GET(MSG_TOKEN_ID_MASK, (hdr))
70#define MSG_TOKEN_MAX (MSG_XTRACT_TOKEN(MSG_TOKEN_ID_MASK) + 1)
71
72
73
74
75
76
77
78
79
80#define SCMI_PENDING_XFERS_HT_ORDER_SZ 9
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95struct scmi_msg_hdr {
96 u8 id;
97 u8 protocol_id;
98 u8 type;
99 u16 seq;
100 u32 status;
101 bool poll_completion;
102};
103
104
105
106
107
108
109
110
111
112static inline u32 pack_scmi_header(struct scmi_msg_hdr *hdr)
113{
114 return FIELD_PREP(MSG_ID_MASK, hdr->id) |
115 FIELD_PREP(MSG_TYPE_MASK, hdr->type) |
116 FIELD_PREP(MSG_TOKEN_ID_MASK, hdr->seq) |
117 FIELD_PREP(MSG_PROTOCOL_ID_MASK, hdr->protocol_id);
118}
119
120
121
122
123
124
125
126static inline void unpack_scmi_header(u32 msg_hdr, struct scmi_msg_hdr *hdr)
127{
128 hdr->id = MSG_XTRACT_ID(msg_hdr);
129 hdr->protocol_id = MSG_XTRACT_PROT_ID(msg_hdr);
130 hdr->type = MSG_XTRACT_TYPE(msg_hdr);
131}
132
133
134
135
136
137
138
139struct scmi_msg {
140 void *buf;
141 size_t len;
142};
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177struct scmi_xfer {
178 int transfer_id;
179 struct scmi_msg_hdr hdr;
180 struct scmi_msg tx;
181 struct scmi_msg rx;
182 struct completion done;
183 struct completion *async_done;
184 bool pending;
185 struct hlist_node node;
186 refcount_t users;
187#define SCMI_XFER_FREE 0
188#define SCMI_XFER_BUSY 1
189 atomic_t busy;
190#define SCMI_XFER_SENT_OK 0
191#define SCMI_XFER_RESP_OK 1
192#define SCMI_XFER_DRESP_OK 2
193 int state;
194
195 spinlock_t lock;
196 void *priv;
197};
198
199
200
201
202
203#define XFER_FIND(__ht, __k) \
204({ \
205 typeof(__k) k_ = __k; \
206 struct scmi_xfer *xfer_ = NULL; \
207 \
208 hash_for_each_possible((__ht), xfer_, node, k_) \
209 if (xfer_->hdr.seq == k_) \
210 break; \
211 xfer_; \
212})
213
214struct scmi_xfer_ops;
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236struct scmi_protocol_handle {
237 struct device *dev;
238 const struct scmi_xfer_ops *xops;
239 int (*set_priv)(const struct scmi_protocol_handle *ph, void *priv);
240 void *(*get_priv)(const struct scmi_protocol_handle *ph);
241};
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257struct scmi_xfer_ops {
258 int (*version_get)(const struct scmi_protocol_handle *ph, u32 *version);
259 int (*xfer_get_init)(const struct scmi_protocol_handle *ph, u8 msg_id,
260 size_t tx_size, size_t rx_size,
261 struct scmi_xfer **p);
262 void (*reset_rx_to_maxsz)(const struct scmi_protocol_handle *ph,
263 struct scmi_xfer *xfer);
264 int (*do_xfer)(const struct scmi_protocol_handle *ph,
265 struct scmi_xfer *xfer);
266 int (*do_xfer_with_response)(const struct scmi_protocol_handle *ph,
267 struct scmi_xfer *xfer);
268 void (*xfer_put)(const struct scmi_protocol_handle *ph,
269 struct scmi_xfer *xfer);
270};
271
272struct scmi_revision_info *
273scmi_revision_area_get(const struct scmi_protocol_handle *ph);
274int scmi_handle_put(const struct scmi_handle *handle);
275struct scmi_handle *scmi_handle_get(struct device *dev);
276void scmi_set_handle(struct scmi_device *scmi_dev);
277void scmi_setup_protocol_implemented(const struct scmi_protocol_handle *ph,
278 u8 *prot_imp);
279
280typedef int (*scmi_prot_init_ph_fn_t)(const struct scmi_protocol_handle *);
281
282
283
284
285
286
287
288
289
290
291
292struct scmi_protocol {
293 const u8 id;
294 struct module *owner;
295 const scmi_prot_init_ph_fn_t instance_init;
296 const scmi_prot_init_ph_fn_t instance_deinit;
297 const void *ops;
298 const struct scmi_protocol_events *events;
299};
300
301int __init scmi_bus_init(void);
302void __exit scmi_bus_exit(void);
303
304#define DECLARE_SCMI_REGISTER_UNREGISTER(func) \
305 int __init scmi_##func##_register(void); \
306 void __exit scmi_##func##_unregister(void)
307DECLARE_SCMI_REGISTER_UNREGISTER(base);
308DECLARE_SCMI_REGISTER_UNREGISTER(clock);
309DECLARE_SCMI_REGISTER_UNREGISTER(perf);
310DECLARE_SCMI_REGISTER_UNREGISTER(power);
311DECLARE_SCMI_REGISTER_UNREGISTER(reset);
312DECLARE_SCMI_REGISTER_UNREGISTER(sensors);
313DECLARE_SCMI_REGISTER_UNREGISTER(voltage);
314DECLARE_SCMI_REGISTER_UNREGISTER(system);
315
316#define DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(name, proto) \
317static const struct scmi_protocol *__this_proto = &(proto); \
318 \
319int __init scmi_##name##_register(void) \
320{ \
321 return scmi_protocol_register(__this_proto); \
322} \
323 \
324void __exit scmi_##name##_unregister(void) \
325{ \
326 scmi_protocol_unregister(__this_proto); \
327}
328
329const struct scmi_protocol *scmi_protocol_get(int protocol_id);
330void scmi_protocol_put(int protocol_id);
331
332int scmi_protocol_acquire(const struct scmi_handle *handle, u8 protocol_id);
333void scmi_protocol_release(const struct scmi_handle *handle, u8 protocol_id);
334
335
336
337
338
339
340
341
342
343
344struct scmi_chan_info {
345 struct device *dev;
346 struct scmi_handle *handle;
347 void *transport_info;
348};
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367struct scmi_transport_ops {
368 int (*link_supplier)(struct device *dev);
369 bool (*chan_available)(struct device *dev, int idx);
370 int (*chan_setup)(struct scmi_chan_info *cinfo, struct device *dev,
371 bool tx);
372 int (*chan_free)(int id, void *p, void *data);
373 unsigned int (*get_max_msg)(struct scmi_chan_info *base_cinfo);
374 int (*send_message)(struct scmi_chan_info *cinfo,
375 struct scmi_xfer *xfer);
376 void (*mark_txdone)(struct scmi_chan_info *cinfo, int ret);
377 void (*fetch_response)(struct scmi_chan_info *cinfo,
378 struct scmi_xfer *xfer);
379 void (*fetch_notification)(struct scmi_chan_info *cinfo,
380 size_t max_len, struct scmi_xfer *xfer);
381 void (*clear_channel)(struct scmi_chan_info *cinfo);
382 bool (*poll_done)(struct scmi_chan_info *cinfo, struct scmi_xfer *xfer);
383};
384
385int scmi_protocol_device_request(const struct scmi_device_id *id_table);
386void scmi_protocol_device_unrequest(const struct scmi_device_id *id_table);
387struct scmi_device *scmi_child_dev_find(struct device *parent,
388 int prot_id, const char *name);
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406struct scmi_desc {
407 int (*transport_init)(void);
408 void (*transport_exit)(void);
409 const struct scmi_transport_ops *ops;
410 int max_rx_timeout_ms;
411 int max_msg;
412 int max_msg_size;
413};
414
415#ifdef CONFIG_ARM_SCMI_TRANSPORT_MAILBOX
416extern const struct scmi_desc scmi_mailbox_desc;
417#endif
418#ifdef CONFIG_ARM_SCMI_TRANSPORT_SMC
419extern const struct scmi_desc scmi_smc_desc;
420#endif
421#ifdef CONFIG_ARM_SCMI_TRANSPORT_VIRTIO
422extern const struct scmi_desc scmi_virtio_desc;
423#endif
424
425void scmi_rx_callback(struct scmi_chan_info *cinfo, u32 msg_hdr, void *priv);
426void scmi_free_channel(struct scmi_chan_info *cinfo, struct idr *idr, int id);
427
428
429struct scmi_shared_mem;
430
431void shmem_tx_prepare(struct scmi_shared_mem __iomem *shmem,
432 struct scmi_xfer *xfer);
433u32 shmem_read_header(struct scmi_shared_mem __iomem *shmem);
434void shmem_fetch_response(struct scmi_shared_mem __iomem *shmem,
435 struct scmi_xfer *xfer);
436void shmem_fetch_notification(struct scmi_shared_mem __iomem *shmem,
437 size_t max_len, struct scmi_xfer *xfer);
438void shmem_clear_channel(struct scmi_shared_mem __iomem *shmem);
439bool shmem_poll_done(struct scmi_shared_mem __iomem *shmem,
440 struct scmi_xfer *xfer);
441
442
443struct scmi_msg_payld;
444
445
446#define SCMI_MSG_MAX_PROT_OVERHEAD (2 * sizeof(__le32))
447
448size_t msg_response_size(struct scmi_xfer *xfer);
449size_t msg_command_size(struct scmi_xfer *xfer);
450void msg_tx_prepare(struct scmi_msg_payld *msg, struct scmi_xfer *xfer);
451u32 msg_read_header(struct scmi_msg_payld *msg);
452void msg_fetch_response(struct scmi_msg_payld *msg, size_t len,
453 struct scmi_xfer *xfer);
454void msg_fetch_notification(struct scmi_msg_payld *msg, size_t len,
455 size_t max_len, struct scmi_xfer *xfer);
456
457void scmi_notification_instance_data_set(const struct scmi_handle *handle,
458 void *priv);
459void *scmi_notification_instance_data_get(const struct scmi_handle *handle);
460#endif
461