1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Copyright (c) 2015-2019, Arm Limited and Contributors. All rights reserved.
4 * Copyright (C) 2019-2022 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 */
19struct 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/**
33 * struct scmi_msg_header - Description of a MSG shared memory message buffer
34 *
35 * MSG communication protocol uses a 32bit header memory cell to store SCMI
36 * protocol data followed by the exchange SCMI message payload.
37 */
38struct scmi_smt_msg_header {
39	__le32 msg_header;
40	u8 msg_payload[0];
41};
42
43#define SMT_HEADER_TOKEN(token)		(((token) << 18) & GENMASK(31, 18))
44#define SMT_HEADER_PROTOCOL_ID(proto)	(((proto) << 10) & GENMASK(17, 10))
45#define SMT_HEADER_MESSAGE_TYPE(type)	(((type) << 18) & GENMASK(9, 8))
46#define SMT_HEADER_MESSAGE_ID(id)	((id) & GENMASK(7, 0))
47
48/**
49 * struct scmi_smt - Description of a SMT memory buffer
50 * @buf:	Shared memory base address
51 * @size:	Shared memory byte size
52 */
53struct scmi_smt {
54	u8 *buf;
55	size_t size;
56};
57
58static inline bool scmi_smt_channel_is_free(struct scmi_smt *smt)
59{
60	struct scmi_smt_header *hdr = (void *)smt->buf;
61
62	return hdr->channel_status & SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE;
63}
64
65static inline bool scmi_smt_channel_reports_error(struct scmi_smt *smt)
66{
67	struct scmi_smt_header *hdr = (void *)smt->buf;
68
69	return hdr->channel_status & SCMI_SHMEM_CHAN_STAT_CHANNEL_ERROR;
70}
71
72static inline void scmi_smt_get_channel(struct scmi_smt *smt)
73{
74	struct scmi_smt_header *hdr = (void *)smt->buf;
75
76	hdr->channel_status &= ~SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE;
77}
78
79static inline void scmi_smt_put_channel(struct scmi_smt *smt)
80{
81	struct scmi_smt_header *hdr = (void *)smt->buf;
82
83	hdr->channel_status |= SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE;
84	hdr->channel_status &= ~SCMI_SHMEM_CHAN_STAT_CHANNEL_ERROR;
85}
86
87int scmi_dt_get_smt_buffer(struct udevice *dev, struct scmi_smt *smt);
88
89/*
90 * Write SCMI message to a SMT shared memory
91 * @dev: SCMI device
92 * @smt: Reference to shared memory using SMT header
93 * @msg: Input SCMI message transmitted
94 */
95int scmi_write_msg_to_smt(struct udevice *dev, struct scmi_smt *smt,
96			  struct scmi_msg *msg);
97
98/*
99 * Read SCMI message from a SMT shared memory
100 * @dev: SCMI device
101 * @smt: Reference to shared memory using SMT header
102 * @msg: Output SCMI message received
103 */
104int scmi_read_resp_from_smt(struct udevice *dev, struct scmi_smt *smt,
105			    struct scmi_msg *msg);
106
107void scmi_clear_smt_channel(struct scmi_smt *smt);
108
109/*
110 * Write SCMI message to SMT_MSG shared memory
111 * @dev: SCMI device
112 * @smt: Reference to shared memory using SMT_MSG header
113 * @msg: Input SCMI message transmitted
114 * @buf_size: Size of the full SMT_MSG buffer transmitted
115 */
116int scmi_msg_to_smt_msg(struct udevice *dev, struct scmi_smt *smt,
117			struct scmi_msg *msg, size_t *buf_size);
118
119/*
120 * Read SCMI message from SMT_MSG shared memory
121 * @dev: SCMI device
122 * @smt: Reference to shared memory using SMT_MSG header
123 * @msg: Output SCMI message received
124 * @buf_size: Size of the full SMT_MSG buffer received
125 */
126int scmi_msg_from_smt_msg(struct udevice *dev, struct scmi_smt *smt,
127			  struct scmi_msg *msg, size_t buf_size);
128
129#endif /* SCMI_SMT_H */
130