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 xfer
->hdr
.status
= ioread32(shmem
->msg_payload
);
62 /* Skip the length of header and status in shmem area i.e 8 bytes */
63 xfer
->rx
.len
= min_t(size_t, xfer
->rx
.len
,
64 ioread32(&shmem
->length
) - 8);
66 /* Take a copy to the rx buffer.. */
67 memcpy_fromio(xfer
->rx
.buf
, shmem
->msg_payload
+ 4, xfer
->rx
.len
);
70 void shmem_fetch_notification(struct scmi_shared_mem __iomem
*shmem
,
71 size_t max_len
, struct scmi_xfer
*xfer
)
73 /* Skip only the length of header in shmem area i.e 4 bytes */
74 xfer
->rx
.len
= min_t(size_t, max_len
, ioread32(&shmem
->length
) - 4);
76 /* Take a copy to the rx buffer.. */
77 memcpy_fromio(xfer
->rx
.buf
, shmem
->msg_payload
, xfer
->rx
.len
);
80 void shmem_clear_channel(struct scmi_shared_mem __iomem
*shmem
)
82 iowrite32(SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE
, &shmem
->channel_status
);
85 bool shmem_poll_done(struct scmi_shared_mem __iomem
*shmem
,
86 struct scmi_xfer
*xfer
)
90 xfer_id
= MSG_XTRACT_TOKEN(ioread32(&shmem
->msg_header
));
92 if (xfer
->hdr
.seq
!= xfer_id
)
95 return ioread32(&shmem
->channel_status
) &
96 (SCMI_SHMEM_CHAN_STAT_CHANNEL_ERROR
|
97 SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE
);