Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / firmware / arm_scmi / msg.c
blob2cc74e6bbd723f08380ebc1226d419c808ef2ac1
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * For transports using message passing.
5 * Derived from shm.c.
7 * Copyright (C) 2019-2024 ARM Ltd.
8 * Copyright (C) 2020-2021 OpenSynergy GmbH
9 */
11 #include <linux/types.h>
13 #include "common.h"
16 * struct scmi_msg_payld - Transport SDU layout
18 * The SCMI specification requires all parameters, message headers, return
19 * arguments or any protocol data to be expressed in little endian format only.
21 struct scmi_msg_payld {
22 __le32 msg_header;
23 __le32 msg_payload[];
26 /**
27 * msg_command_size() - Actual size of transport SDU for command.
29 * @xfer: message which core has prepared for sending
31 * Return: transport SDU size.
33 static size_t msg_command_size(struct scmi_xfer *xfer)
35 return sizeof(struct scmi_msg_payld) + xfer->tx.len;
38 /**
39 * msg_response_size() - Maximum size of transport SDU for response.
41 * @xfer: message which core has prepared for sending
43 * Return: transport SDU size.
45 static size_t msg_response_size(struct scmi_xfer *xfer)
47 return sizeof(struct scmi_msg_payld) + sizeof(__le32) + xfer->rx.len;
50 /**
51 * msg_tx_prepare() - Set up transport SDU for command.
53 * @msg: transport SDU for command
54 * @xfer: message which is being sent
56 static void msg_tx_prepare(struct scmi_msg_payld *msg, struct scmi_xfer *xfer)
58 msg->msg_header = cpu_to_le32(pack_scmi_header(&xfer->hdr));
59 if (xfer->tx.buf)
60 memcpy(msg->msg_payload, xfer->tx.buf, xfer->tx.len);
63 /**
64 * msg_read_header() - Read SCMI header from transport SDU.
66 * @msg: transport SDU
68 * Return: SCMI header
70 static u32 msg_read_header(struct scmi_msg_payld *msg)
72 return le32_to_cpu(msg->msg_header);
75 /**
76 * msg_fetch_response() - Fetch response SCMI payload from transport SDU.
78 * @msg: transport SDU with response
79 * @len: transport SDU size
80 * @xfer: message being responded to
82 static void msg_fetch_response(struct scmi_msg_payld *msg,
83 size_t len, struct scmi_xfer *xfer)
85 size_t prefix_len = sizeof(*msg) + sizeof(msg->msg_payload[0]);
87 xfer->hdr.status = le32_to_cpu(msg->msg_payload[0]);
88 xfer->rx.len = min_t(size_t, xfer->rx.len,
89 len >= prefix_len ? len - prefix_len : 0);
91 /* Take a copy to the rx buffer.. */
92 memcpy(xfer->rx.buf, &msg->msg_payload[1], xfer->rx.len);
95 /**
96 * msg_fetch_notification() - Fetch notification payload from transport SDU.
98 * @msg: transport SDU with notification
99 * @len: transport SDU size
100 * @max_len: maximum SCMI payload size to fetch
101 * @xfer: notification message
103 static void msg_fetch_notification(struct scmi_msg_payld *msg, size_t len,
104 size_t max_len, struct scmi_xfer *xfer)
106 xfer->rx.len = min_t(size_t, max_len,
107 len >= sizeof(*msg) ? len - sizeof(*msg) : 0);
109 /* Take a copy to the rx buffer.. */
110 memcpy(xfer->rx.buf, msg->msg_payload, xfer->rx.len);
113 static const struct scmi_message_operations scmi_msg_ops = {
114 .tx_prepare = msg_tx_prepare,
115 .command_size = msg_command_size,
116 .response_size = msg_response_size,
117 .read_header = msg_read_header,
118 .fetch_response = msg_fetch_response,
119 .fetch_notification = msg_fetch_notification,
122 const struct scmi_message_operations *scmi_message_operations_get(void)
124 return &scmi_msg_ops;