1 // SPDX-License-Identifier: GPL-2.0
3 * For transport using shared mem structure.
5 * Copyright (C) 2019 ARM Ltd.
9 #include <linux/processor.h>
10 #include <linux/types.h>
15 * SCMI specification requires all parameters, message headers, return
16 * arguments or any protocol data to be expressed in little endian
19 struct scmi_shared_mem {
21 __le32 channel_status;
22 #define SCMI_SHMEM_CHAN_STAT_CHANNEL_ERROR BIT(1)
23 #define SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE BIT(0)
26 #define SCMI_SHMEM_FLAG_INTR_ENABLED BIT(0)
32 void shmem_tx_prepare(struct scmi_shared_mem __iomem *shmem,
33 struct scmi_xfer *xfer)
36 * Ideally channel must be free by now unless OS timeout last
37 * request and platform continued to process the same, wait
38 * until it releases the shared memory, otherwise we may endup
39 * overwriting its response with new message payload or vice-versa
41 spin_until_cond(ioread32(&shmem->channel_status) &
42 SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE);
43 /* Mark channel busy + clear error */
44 iowrite32(0x0, &shmem->channel_status);
45 iowrite32(xfer->hdr.poll_completion ? 0 : SCMI_SHMEM_FLAG_INTR_ENABLED,
47 iowrite32(sizeof(shmem->msg_header) + xfer->tx.len, &shmem->length);
48 iowrite32(pack_scmi_header(&xfer->hdr), &shmem->msg_header);
50 memcpy_toio(shmem->msg_payload, xfer->tx.buf, xfer->tx.len);
53 u32 shmem_read_header(struct scmi_shared_mem __iomem *shmem)
55 return ioread32(&shmem->msg_header);
58 void shmem_fetch_response(struct scmi_shared_mem __iomem *shmem,
59 struct scmi_xfer *xfer)
61 size_t len = ioread32(&shmem->length);
63 xfer->hdr.status = ioread32(shmem->msg_payload);
64 /* Skip the length of header and status in shmem area i.e 8 bytes */
65 xfer->rx.len = min_t(size_t, xfer->rx.len, len > 8 ? len - 8 : 0);
67 /* Take a copy to the rx buffer.. */
68 memcpy_fromio(xfer->rx.buf, shmem->msg_payload + 4, xfer->rx.len);
71 void shmem_fetch_notification(struct scmi_shared_mem __iomem *shmem,
72 size_t max_len, struct scmi_xfer *xfer)
74 size_t len = ioread32(&shmem->length);
76 /* Skip only the length of header in shmem area i.e 4 bytes */
77 xfer->rx.len = min_t(size_t, max_len, len > 4 ? len - 4 : 0);
79 /* Take a copy to the rx buffer.. */
80 memcpy_fromio(xfer->rx.buf, shmem->msg_payload, xfer->rx.len);
83 void shmem_clear_channel(struct scmi_shared_mem __iomem *shmem)
85 iowrite32(SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE, &shmem->channel_status);
88 bool shmem_poll_done(struct scmi_shared_mem __iomem *shmem,
89 struct scmi_xfer *xfer)
93 xfer_id = MSG_XTRACT_TOKEN(ioread32(&shmem->msg_header));
95 if (xfer->hdr.seq != xfer_id)
98 return ioread32(&shmem->channel_status) &
99 (SCMI_SHMEM_CHAN_STAT_CHANNEL_ERROR |
100 SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE);