]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/soc/qcom/smd-rpm.c
Merge tag 'kbuild-v5.14' of git://git.kernel.org/pub/scm/linux/kernel/git/masahiroy...
[mirror_ubuntu-jammy-kernel.git] / drivers / soc / qcom / smd-rpm.c
CommitLineData
97fb5e8d 1// SPDX-License-Identifier: GPL-2.0-only
936f14cf
BA
2/*
3 * Copyright (c) 2015, Sony Mobile Communications AB.
4 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
936f14cf
BA
5 */
6
7#include <linux/module.h>
8#include <linux/platform_device.h>
9#include <linux/of_platform.h>
10#include <linux/io.h>
11#include <linux/interrupt.h>
50e1b29b 12#include <linux/slab.h>
936f14cf 13
5052de8d 14#include <linux/rpmsg.h>
936f14cf
BA
15#include <linux/soc/qcom/smd-rpm.h>
16
17#define RPM_REQUEST_TIMEOUT (5 * HZ)
18
19/**
20 * struct qcom_smd_rpm - state of the rpm device driver
21 * @rpm_channel: reference to the smd channel
66e6a633 22 * @icc: interconnect proxy device
e434e0c4 23 * @dev: rpm device
936f14cf
BA
24 * @ack: completion for acks
25 * @lock: mutual exclusion around the send/complete pair
26 * @ack_status: result of the rpm request
27 */
28struct qcom_smd_rpm {
5052de8d 29 struct rpmsg_endpoint *rpm_channel;
66e6a633 30 struct platform_device *icc;
b853cb96 31 struct device *dev;
936f14cf
BA
32
33 struct completion ack;
34 struct mutex lock;
35 int ack_status;
36};
37
38/**
39 * struct qcom_rpm_header - header for all rpm requests and responses
40 * @service_type: identifier of the service
41 * @length: length of the payload
42 */
43struct qcom_rpm_header {
30b7ea5e
SB
44 __le32 service_type;
45 __le32 length;
936f14cf
BA
46};
47
48/**
49 * struct qcom_rpm_request - request message to the rpm
50 * @msg_id: identifier of the outgoing message
51 * @flags: active/sleep state flags
52 * @type: resource type
53 * @id: resource id
54 * @data_len: length of the payload following this header
55 */
56struct qcom_rpm_request {
30b7ea5e
SB
57 __le32 msg_id;
58 __le32 flags;
59 __le32 type;
60 __le32 id;
61 __le32 data_len;
936f14cf
BA
62};
63
64/**
65 * struct qcom_rpm_message - response message from the rpm
66 * @msg_type: indicator of the type of message
67 * @length: the size of this message, including the message header
68 * @msg_id: message id
69 * @message: textual message from the rpm
70 *
71 * Multiple of these messages can be stacked in an rpm message.
72 */
73struct qcom_rpm_message {
30b7ea5e
SB
74 __le32 msg_type;
75 __le32 length;
936f14cf 76 union {
30b7ea5e 77 __le32 msg_id;
936f14cf
BA
78 u8 message[0];
79 };
80};
81
82#define RPM_SERVICE_TYPE_REQUEST 0x00716572 /* "req\0" */
83
84#define RPM_MSG_TYPE_ERR 0x00727265 /* "err\0" */
85#define RPM_MSG_TYPE_MSG_ID 0x2367736d /* "msg#" */
86
87/**
88 * qcom_rpm_smd_write - write @buf to @type:@id
89 * @rpm: rpm handle
e434e0c4 90 * @state: active/sleep state flags
936f14cf
BA
91 * @type: resource type
92 * @id: resource identifier
93 * @buf: the data to be written
94 * @count: number of bytes in @buf
95 */
96int qcom_rpm_smd_write(struct qcom_smd_rpm *rpm,
97 int state,
98 u32 type, u32 id,
99 void *buf,
100 size_t count)
101{
102 static unsigned msg_id = 1;
103 int left;
104 int ret;
936f14cf
BA
105 struct {
106 struct qcom_rpm_header hdr;
107 struct qcom_rpm_request req;
50e1b29b
SB
108 u8 payload[];
109 } *pkt;
110 size_t size = sizeof(*pkt) + count;
936f14cf
BA
111
112 /* SMD packets to the RPM may not exceed 256 bytes */
50e1b29b 113 if (WARN_ON(size >= 256))
936f14cf
BA
114 return -EINVAL;
115
50e1b29b
SB
116 pkt = kmalloc(size, GFP_KERNEL);
117 if (!pkt)
118 return -ENOMEM;
119
936f14cf
BA
120 mutex_lock(&rpm->lock);
121
30b7ea5e
SB
122 pkt->hdr.service_type = cpu_to_le32(RPM_SERVICE_TYPE_REQUEST);
123 pkt->hdr.length = cpu_to_le32(sizeof(struct qcom_rpm_request) + count);
936f14cf 124
30b7ea5e 125 pkt->req.msg_id = cpu_to_le32(msg_id++);
a8ddd1b9 126 pkt->req.flags = cpu_to_le32(state);
30b7ea5e
SB
127 pkt->req.type = cpu_to_le32(type);
128 pkt->req.id = cpu_to_le32(id);
129 pkt->req.data_len = cpu_to_le32(count);
50e1b29b 130 memcpy(pkt->payload, buf, count);
936f14cf 131
5052de8d 132 ret = rpmsg_send(rpm->rpm_channel, pkt, size);
936f14cf
BA
133 if (ret)
134 goto out;
135
136 left = wait_for_completion_timeout(&rpm->ack, RPM_REQUEST_TIMEOUT);
137 if (!left)
138 ret = -ETIMEDOUT;
139 else
140 ret = rpm->ack_status;
141
142out:
50e1b29b 143 kfree(pkt);
936f14cf
BA
144 mutex_unlock(&rpm->lock);
145 return ret;
146}
147EXPORT_SYMBOL(qcom_rpm_smd_write);
148
5052de8d
BA
149static int qcom_smd_rpm_callback(struct rpmsg_device *rpdev,
150 void *data,
151 int count,
152 void *priv,
153 u32 addr)
936f14cf
BA
154{
155 const struct qcom_rpm_header *hdr = data;
30b7ea5e 156 size_t hdr_length = le32_to_cpu(hdr->length);
936f14cf 157 const struct qcom_rpm_message *msg;
5052de8d 158 struct qcom_smd_rpm *rpm = dev_get_drvdata(&rpdev->dev);
936f14cf 159 const u8 *buf = data + sizeof(struct qcom_rpm_header);
30b7ea5e 160 const u8 *end = buf + hdr_length;
936f14cf
BA
161 char msgbuf[32];
162 int status = 0;
30b7ea5e 163 u32 len, msg_length;
936f14cf 164
30b7ea5e
SB
165 if (le32_to_cpu(hdr->service_type) != RPM_SERVICE_TYPE_REQUEST ||
166 hdr_length < sizeof(struct qcom_rpm_message)) {
b853cb96 167 dev_err(rpm->dev, "invalid request\n");
936f14cf
BA
168 return 0;
169 }
170
171 while (buf < end) {
172 msg = (struct qcom_rpm_message *)buf;
30b7ea5e
SB
173 msg_length = le32_to_cpu(msg->length);
174 switch (le32_to_cpu(msg->msg_type)) {
936f14cf
BA
175 case RPM_MSG_TYPE_MSG_ID:
176 break;
177 case RPM_MSG_TYPE_ERR:
30b7ea5e 178 len = min_t(u32, ALIGN(msg_length, 4), sizeof(msgbuf));
936f14cf
BA
179 memcpy_fromio(msgbuf, msg->message, len);
180 msgbuf[len - 1] = 0;
181
182 if (!strcmp(msgbuf, "resource does not exist"))
183 status = -ENXIO;
184 else
185 status = -EINVAL;
186 break;
187 }
188
30b7ea5e 189 buf = PTR_ALIGN(buf + 2 * sizeof(u32) + msg_length, 4);
936f14cf
BA
190 }
191
192 rpm->ack_status = status;
193 complete(&rpm->ack);
194 return 0;
195}
196
5052de8d 197static int qcom_smd_rpm_probe(struct rpmsg_device *rpdev)
936f14cf
BA
198{
199 struct qcom_smd_rpm *rpm;
66e6a633 200 int ret;
936f14cf 201
5052de8d 202 rpm = devm_kzalloc(&rpdev->dev, sizeof(*rpm), GFP_KERNEL);
936f14cf
BA
203 if (!rpm)
204 return -ENOMEM;
205
206 mutex_init(&rpm->lock);
207 init_completion(&rpm->ack);
208
5052de8d
BA
209 rpm->dev = &rpdev->dev;
210 rpm->rpm_channel = rpdev->ept;
211 dev_set_drvdata(&rpdev->dev, rpm);
936f14cf 212
66e6a633
GD
213 rpm->icc = platform_device_register_data(&rpdev->dev, "icc_smd_rpm", -1,
214 NULL, 0);
215 if (IS_ERR(rpm->icc))
216 return PTR_ERR(rpm->icc);
217
218 ret = of_platform_populate(rpdev->dev.of_node, NULL, NULL, &rpdev->dev);
219 if (ret)
220 platform_device_unregister(rpm->icc);
221
222 return ret;
936f14cf
BA
223}
224
5052de8d 225static void qcom_smd_rpm_remove(struct rpmsg_device *rpdev)
936f14cf 226{
66e6a633
GD
227 struct qcom_smd_rpm *rpm = dev_get_drvdata(&rpdev->dev);
228
229 platform_device_unregister(rpm->icc);
5052de8d 230 of_platform_depopulate(&rpdev->dev);
936f14cf
BA
231}
232
233static const struct of_device_id qcom_smd_rpm_of_match[] = {
1f0947fe 234 { .compatible = "qcom,rpm-apq8084" },
84da0cf9 235 { .compatible = "qcom,rpm-ipq6018" },
d8ea59e7 236 { .compatible = "qcom,rpm-msm8226" },
1f0947fe 237 { .compatible = "qcom,rpm-msm8916" },
d6e52482 238 { .compatible = "qcom,rpm-msm8936" },
936f14cf 239 { .compatible = "qcom,rpm-msm8974" },
83a81c1b 240 { .compatible = "qcom,rpm-msm8976" },
a9541d2e 241 { .compatible = "qcom,rpm-msm8994" },
2b624250 242 { .compatible = "qcom,rpm-msm8996" },
64bf6b26 243 { .compatible = "qcom,rpm-msm8998" },
4e2256d3 244 { .compatible = "qcom,rpm-sdm660" },
055c9aff 245 { .compatible = "qcom,rpm-sm6125" },
b7e38617 246 { .compatible = "qcom,rpm-qcs404" },
936f14cf
BA
247 {}
248};
249MODULE_DEVICE_TABLE(of, qcom_smd_rpm_of_match);
250
5052de8d 251static struct rpmsg_driver qcom_smd_rpm_driver = {
936f14cf
BA
252 .probe = qcom_smd_rpm_probe,
253 .remove = qcom_smd_rpm_remove,
254 .callback = qcom_smd_rpm_callback,
5052de8d 255 .drv = {
936f14cf 256 .name = "qcom_smd_rpm",
936f14cf
BA
257 .of_match_table = qcom_smd_rpm_of_match,
258 },
259};
260
261static int __init qcom_smd_rpm_init(void)
262{
5052de8d 263 return register_rpmsg_driver(&qcom_smd_rpm_driver);
936f14cf
BA
264}
265arch_initcall(qcom_smd_rpm_init);
266
267static void __exit qcom_smd_rpm_exit(void)
268{
5052de8d 269 unregister_rpmsg_driver(&qcom_smd_rpm_driver);
936f14cf
BA
270}
271module_exit(qcom_smd_rpm_exit);
272
273MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@sonymobile.com>");
274MODULE_DESCRIPTION("Qualcomm SMD backed RPM driver");
275MODULE_LICENSE("GPL v2");