1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /* Copyright (c) 2010,2015,2019 The Linux Foundation. All rights reserved.
3*4882a593Smuzhiyun * Copyright (C) 2015 Linaro Ltd.
4*4882a593Smuzhiyun */
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun #include <linux/slab.h>
7*4882a593Smuzhiyun #include <linux/io.h>
8*4882a593Smuzhiyun #include <linux/module.h>
9*4882a593Smuzhiyun #include <linux/mutex.h>
10*4882a593Smuzhiyun #include <linux/errno.h>
11*4882a593Smuzhiyun #include <linux/err.h>
12*4882a593Smuzhiyun #include <linux/qcom_scm.h>
13*4882a593Smuzhiyun #include <linux/arm-smccc.h>
14*4882a593Smuzhiyun #include <linux/dma-mapping.h>
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun #include "qcom_scm.h"
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun static DEFINE_MUTEX(qcom_scm_lock);
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun /**
22*4882a593Smuzhiyun * struct arm_smccc_args
23*4882a593Smuzhiyun * @args: The array of values used in registers in smc instruction
24*4882a593Smuzhiyun */
25*4882a593Smuzhiyun struct arm_smccc_args {
26*4882a593Smuzhiyun unsigned long args[8];
27*4882a593Smuzhiyun };
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun /**
31*4882a593Smuzhiyun * struct scm_legacy_command - one SCM command buffer
32*4882a593Smuzhiyun * @len: total available memory for command and response
33*4882a593Smuzhiyun * @buf_offset: start of command buffer
34*4882a593Smuzhiyun * @resp_hdr_offset: start of response buffer
35*4882a593Smuzhiyun * @id: command to be executed
36*4882a593Smuzhiyun * @buf: buffer returned from scm_legacy_get_command_buffer()
37*4882a593Smuzhiyun *
38*4882a593Smuzhiyun * An SCM command is laid out in memory as follows:
39*4882a593Smuzhiyun *
40*4882a593Smuzhiyun * ------------------- <--- struct scm_legacy_command
41*4882a593Smuzhiyun * | command header |
42*4882a593Smuzhiyun * ------------------- <--- scm_legacy_get_command_buffer()
43*4882a593Smuzhiyun * | command buffer |
44*4882a593Smuzhiyun * ------------------- <--- struct scm_legacy_response and
45*4882a593Smuzhiyun * | response header | scm_legacy_command_to_response()
46*4882a593Smuzhiyun * ------------------- <--- scm_legacy_get_response_buffer()
47*4882a593Smuzhiyun * | response buffer |
48*4882a593Smuzhiyun * -------------------
49*4882a593Smuzhiyun *
50*4882a593Smuzhiyun * There can be arbitrary padding between the headers and buffers so
51*4882a593Smuzhiyun * you should always use the appropriate scm_legacy_get_*_buffer() routines
52*4882a593Smuzhiyun * to access the buffers in a safe manner.
53*4882a593Smuzhiyun */
54*4882a593Smuzhiyun struct scm_legacy_command {
55*4882a593Smuzhiyun __le32 len;
56*4882a593Smuzhiyun __le32 buf_offset;
57*4882a593Smuzhiyun __le32 resp_hdr_offset;
58*4882a593Smuzhiyun __le32 id;
59*4882a593Smuzhiyun __le32 buf[];
60*4882a593Smuzhiyun };
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun /**
63*4882a593Smuzhiyun * struct scm_legacy_response - one SCM response buffer
64*4882a593Smuzhiyun * @len: total available memory for response
65*4882a593Smuzhiyun * @buf_offset: start of response data relative to start of scm_legacy_response
66*4882a593Smuzhiyun * @is_complete: indicates if the command has finished processing
67*4882a593Smuzhiyun */
68*4882a593Smuzhiyun struct scm_legacy_response {
69*4882a593Smuzhiyun __le32 len;
70*4882a593Smuzhiyun __le32 buf_offset;
71*4882a593Smuzhiyun __le32 is_complete;
72*4882a593Smuzhiyun };
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun /**
75*4882a593Smuzhiyun * scm_legacy_command_to_response() - Get a pointer to a scm_legacy_response
76*4882a593Smuzhiyun * @cmd: command
77*4882a593Smuzhiyun *
78*4882a593Smuzhiyun * Returns a pointer to a response for a command.
79*4882a593Smuzhiyun */
scm_legacy_command_to_response(const struct scm_legacy_command * cmd)80*4882a593Smuzhiyun static inline struct scm_legacy_response *scm_legacy_command_to_response(
81*4882a593Smuzhiyun const struct scm_legacy_command *cmd)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun return (void *)cmd + le32_to_cpu(cmd->resp_hdr_offset);
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun /**
87*4882a593Smuzhiyun * scm_legacy_get_command_buffer() - Get a pointer to a command buffer
88*4882a593Smuzhiyun * @cmd: command
89*4882a593Smuzhiyun *
90*4882a593Smuzhiyun * Returns a pointer to the command buffer of a command.
91*4882a593Smuzhiyun */
scm_legacy_get_command_buffer(const struct scm_legacy_command * cmd)92*4882a593Smuzhiyun static inline void *scm_legacy_get_command_buffer(
93*4882a593Smuzhiyun const struct scm_legacy_command *cmd)
94*4882a593Smuzhiyun {
95*4882a593Smuzhiyun return (void *)cmd->buf;
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun /**
99*4882a593Smuzhiyun * scm_legacy_get_response_buffer() - Get a pointer to a response buffer
100*4882a593Smuzhiyun * @rsp: response
101*4882a593Smuzhiyun *
102*4882a593Smuzhiyun * Returns a pointer to a response buffer of a response.
103*4882a593Smuzhiyun */
scm_legacy_get_response_buffer(const struct scm_legacy_response * rsp)104*4882a593Smuzhiyun static inline void *scm_legacy_get_response_buffer(
105*4882a593Smuzhiyun const struct scm_legacy_response *rsp)
106*4882a593Smuzhiyun {
107*4882a593Smuzhiyun return (void *)rsp + le32_to_cpu(rsp->buf_offset);
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun
__scm_legacy_do(const struct arm_smccc_args * smc,struct arm_smccc_res * res)110*4882a593Smuzhiyun static void __scm_legacy_do(const struct arm_smccc_args *smc,
111*4882a593Smuzhiyun struct arm_smccc_res *res)
112*4882a593Smuzhiyun {
113*4882a593Smuzhiyun do {
114*4882a593Smuzhiyun arm_smccc_smc(smc->args[0], smc->args[1], smc->args[2],
115*4882a593Smuzhiyun smc->args[3], smc->args[4], smc->args[5],
116*4882a593Smuzhiyun smc->args[6], smc->args[7], res);
117*4882a593Smuzhiyun } while (res->a0 == QCOM_SCM_INTERRUPTED);
118*4882a593Smuzhiyun }
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun /**
121*4882a593Smuzhiyun * qcom_scm_call() - Sends a command to the SCM and waits for the command to
122*4882a593Smuzhiyun * finish processing.
123*4882a593Smuzhiyun *
124*4882a593Smuzhiyun * A note on cache maintenance:
125*4882a593Smuzhiyun * Note that any buffers that are expected to be accessed by the secure world
126*4882a593Smuzhiyun * must be flushed before invoking qcom_scm_call and invalidated in the cache
127*4882a593Smuzhiyun * immediately after qcom_scm_call returns. Cache maintenance on the command
128*4882a593Smuzhiyun * and response buffers is taken care of by qcom_scm_call; however, callers are
129*4882a593Smuzhiyun * responsible for any other cached buffers passed over to the secure world.
130*4882a593Smuzhiyun */
scm_legacy_call(struct device * dev,const struct qcom_scm_desc * desc,struct qcom_scm_res * res)131*4882a593Smuzhiyun int scm_legacy_call(struct device *dev, const struct qcom_scm_desc *desc,
132*4882a593Smuzhiyun struct qcom_scm_res *res)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun u8 arglen = desc->arginfo & 0xf;
135*4882a593Smuzhiyun int ret = 0, context_id;
136*4882a593Smuzhiyun unsigned int i;
137*4882a593Smuzhiyun struct scm_legacy_command *cmd;
138*4882a593Smuzhiyun struct scm_legacy_response *rsp;
139*4882a593Smuzhiyun struct arm_smccc_args smc = {0};
140*4882a593Smuzhiyun struct arm_smccc_res smc_res;
141*4882a593Smuzhiyun const size_t cmd_len = arglen * sizeof(__le32);
142*4882a593Smuzhiyun const size_t resp_len = MAX_QCOM_SCM_RETS * sizeof(__le32);
143*4882a593Smuzhiyun size_t alloc_len = sizeof(*cmd) + cmd_len + sizeof(*rsp) + resp_len;
144*4882a593Smuzhiyun dma_addr_t cmd_phys;
145*4882a593Smuzhiyun __le32 *arg_buf;
146*4882a593Smuzhiyun const __le32 *res_buf;
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun cmd = kzalloc(PAGE_ALIGN(alloc_len), GFP_KERNEL);
149*4882a593Smuzhiyun if (!cmd)
150*4882a593Smuzhiyun return -ENOMEM;
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun cmd->len = cpu_to_le32(alloc_len);
153*4882a593Smuzhiyun cmd->buf_offset = cpu_to_le32(sizeof(*cmd));
154*4882a593Smuzhiyun cmd->resp_hdr_offset = cpu_to_le32(sizeof(*cmd) + cmd_len);
155*4882a593Smuzhiyun cmd->id = cpu_to_le32(SCM_LEGACY_FNID(desc->svc, desc->cmd));
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun arg_buf = scm_legacy_get_command_buffer(cmd);
158*4882a593Smuzhiyun for (i = 0; i < arglen; i++)
159*4882a593Smuzhiyun arg_buf[i] = cpu_to_le32(desc->args[i]);
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun rsp = scm_legacy_command_to_response(cmd);
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun cmd_phys = dma_map_single(dev, cmd, alloc_len, DMA_TO_DEVICE);
164*4882a593Smuzhiyun if (dma_mapping_error(dev, cmd_phys)) {
165*4882a593Smuzhiyun kfree(cmd);
166*4882a593Smuzhiyun return -ENOMEM;
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun smc.args[0] = 1;
170*4882a593Smuzhiyun smc.args[1] = (unsigned long)&context_id;
171*4882a593Smuzhiyun smc.args[2] = cmd_phys;
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun mutex_lock(&qcom_scm_lock);
174*4882a593Smuzhiyun __scm_legacy_do(&smc, &smc_res);
175*4882a593Smuzhiyun if (smc_res.a0)
176*4882a593Smuzhiyun ret = qcom_scm_remap_error(smc_res.a0);
177*4882a593Smuzhiyun mutex_unlock(&qcom_scm_lock);
178*4882a593Smuzhiyun if (ret)
179*4882a593Smuzhiyun goto out;
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun do {
182*4882a593Smuzhiyun dma_sync_single_for_cpu(dev, cmd_phys + sizeof(*cmd) + cmd_len,
183*4882a593Smuzhiyun sizeof(*rsp), DMA_FROM_DEVICE);
184*4882a593Smuzhiyun } while (!rsp->is_complete);
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun dma_sync_single_for_cpu(dev, cmd_phys + sizeof(*cmd) + cmd_len +
187*4882a593Smuzhiyun le32_to_cpu(rsp->buf_offset),
188*4882a593Smuzhiyun resp_len, DMA_FROM_DEVICE);
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun if (res) {
191*4882a593Smuzhiyun res_buf = scm_legacy_get_response_buffer(rsp);
192*4882a593Smuzhiyun for (i = 0; i < MAX_QCOM_SCM_RETS; i++)
193*4882a593Smuzhiyun res->result[i] = le32_to_cpu(res_buf[i]);
194*4882a593Smuzhiyun }
195*4882a593Smuzhiyun out:
196*4882a593Smuzhiyun dma_unmap_single(dev, cmd_phys, alloc_len, DMA_TO_DEVICE);
197*4882a593Smuzhiyun kfree(cmd);
198*4882a593Smuzhiyun return ret;
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun #define SCM_LEGACY_ATOMIC_N_REG_ARGS 5
202*4882a593Smuzhiyun #define SCM_LEGACY_ATOMIC_FIRST_REG_IDX 2
203*4882a593Smuzhiyun #define SCM_LEGACY_CLASS_REGISTER (0x2 << 8)
204*4882a593Smuzhiyun #define SCM_LEGACY_MASK_IRQS BIT(5)
205*4882a593Smuzhiyun #define SCM_LEGACY_ATOMIC_ID(svc, cmd, n) \
206*4882a593Smuzhiyun ((SCM_LEGACY_FNID(svc, cmd) << 12) | \
207*4882a593Smuzhiyun SCM_LEGACY_CLASS_REGISTER | \
208*4882a593Smuzhiyun SCM_LEGACY_MASK_IRQS | \
209*4882a593Smuzhiyun (n & 0xf))
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun /**
212*4882a593Smuzhiyun * qcom_scm_call_atomic() - Send an atomic SCM command with up to 5 arguments
213*4882a593Smuzhiyun * and 3 return values
214*4882a593Smuzhiyun * @desc: SCM call descriptor containing arguments
215*4882a593Smuzhiyun * @res: SCM call return values
216*4882a593Smuzhiyun *
217*4882a593Smuzhiyun * This shall only be used with commands that are guaranteed to be
218*4882a593Smuzhiyun * uninterruptable, atomic and SMP safe.
219*4882a593Smuzhiyun */
scm_legacy_call_atomic(struct device * unused,const struct qcom_scm_desc * desc,struct qcom_scm_res * res)220*4882a593Smuzhiyun int scm_legacy_call_atomic(struct device *unused,
221*4882a593Smuzhiyun const struct qcom_scm_desc *desc,
222*4882a593Smuzhiyun struct qcom_scm_res *res)
223*4882a593Smuzhiyun {
224*4882a593Smuzhiyun int context_id;
225*4882a593Smuzhiyun struct arm_smccc_res smc_res;
226*4882a593Smuzhiyun size_t arglen = desc->arginfo & 0xf;
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun BUG_ON(arglen > SCM_LEGACY_ATOMIC_N_REG_ARGS);
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun arm_smccc_smc(SCM_LEGACY_ATOMIC_ID(desc->svc, desc->cmd, arglen),
231*4882a593Smuzhiyun (unsigned long)&context_id,
232*4882a593Smuzhiyun desc->args[0], desc->args[1], desc->args[2],
233*4882a593Smuzhiyun desc->args[3], desc->args[4], 0, &smc_res);
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun if (res) {
236*4882a593Smuzhiyun res->result[0] = smc_res.a1;
237*4882a593Smuzhiyun res->result[1] = smc_res.a2;
238*4882a593Smuzhiyun res->result[2] = smc_res.a3;
239*4882a593Smuzhiyun }
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun return smc_res.a0;
242*4882a593Smuzhiyun }
243