1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (c) 2015-2019, Arm Limited and Contributors. All rights reserved.
4  * Copyright (C) 2019-2020 Linaro Limited.
5  */
6 #ifndef SCMI_SMT_H
7 #define SCMI_SMT_H
8 
9 #include <asm/types.h>
10 
11 /**
12  * struct scmi_smt_header - Description of the shared memory message buffer
13  *
14  * SMT stands for Shared Memory based Transport.
15  * SMT uses 28 byte header prior message payload to handle the state of
16  * the communication channel realized by the shared memory area and
17  * to define SCMI protocol information the payload relates to.
18  */
19 struct scmi_smt_header {
20 	__le32 reserved;
21 	__le32 channel_status;
22 #define SCMI_SHMEM_CHAN_STAT_CHANNEL_ERROR	BIT(1)
23 #define SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE	BIT(0)
24 	__le32 reserved1[2];
25 	__le32 flags;
26 #define SCMI_SHMEM_FLAG_INTR_ENABLED		BIT(0)
27 	__le32 length;
28 	__le32 msg_header;
29 	u8 msg_payload[0];
30 };
31 
32 #define SMT_HEADER_TOKEN(token)		(((token) << 18) & GENMASK(31, 18))
33 #define SMT_HEADER_PROTOCOL_ID(proto)	(((proto) << 10) & GENMASK(17, 10))
34 #define SMT_HEADER_MESSAGE_TYPE(type)	(((type) << 18) & GENMASK(9, 8))
35 #define SMT_HEADER_MESSAGE_ID(id)	((id) & GENMASK(7, 0))
36 
37 /**
38  * struct scmi_smt - Description of a SMT memory buffer
39  * @buf:	Shared memory base address
40  * @size:	Shared memory byte size
41  */
42 struct scmi_smt {
43 	u8 *buf;
44 	size_t size;
45 };
46 
scmi_smt_channel_is_free(struct scmi_smt * smt)47 static inline bool scmi_smt_channel_is_free(struct scmi_smt *smt)
48 {
49 	struct scmi_smt_header *hdr = (void *)smt->buf;
50 
51 	return hdr->channel_status & SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE;
52 }
53 
scmi_smt_channel_reports_error(struct scmi_smt * smt)54 static inline bool scmi_smt_channel_reports_error(struct scmi_smt *smt)
55 {
56 	struct scmi_smt_header *hdr = (void *)smt->buf;
57 
58 	return hdr->channel_status & SCMI_SHMEM_CHAN_STAT_CHANNEL_ERROR;
59 }
60 
scmi_smt_get_channel(struct scmi_smt * smt)61 static inline void scmi_smt_get_channel(struct scmi_smt *smt)
62 {
63 	struct scmi_smt_header *hdr = (void *)smt->buf;
64 
65 	hdr->channel_status &= ~SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE;
66 }
67 
scmi_smt_put_channel(struct scmi_smt * smt)68 static inline void scmi_smt_put_channel(struct scmi_smt *smt)
69 {
70 	struct scmi_smt_header *hdr = (void *)smt->buf;
71 
72 	hdr->channel_status |= SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE;
73 	hdr->channel_status &= ~SCMI_SHMEM_CHAN_STAT_CHANNEL_ERROR;
74 }
75 
76 int scmi_dt_get_smt_buffer(struct udevice *dev, struct scmi_smt *smt);
77 
78 int scmi_write_msg_to_smt(struct udevice *dev, struct scmi_smt *smt,
79 			  struct scmi_msg *msg);
80 
81 int scmi_read_resp_from_smt(struct udevice *dev, struct scmi_smt *smt,
82 			    struct scmi_msg *msg);
83 
84 void scmi_clear_smt_channel(struct scmi_smt *smt);
85 
86 #endif /* SCMI_SMT_H */
87