]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/firmware/qcom_scm-64.c
01949f1828f49ba59bae4d3101d13b1ca6af82f7
[karo-tx-linux.git] / drivers / firmware / qcom_scm-64.c
1 /* Copyright (c) 2015, The Linux Foundation. All rights reserved.
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 and
5  * only version 2 as published by the Free Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  */
12
13 #include <linux/io.h>
14 #include <linux/errno.h>
15 #include <linux/delay.h>
16 #include <linux/mutex.h>
17 #include <linux/slab.h>
18 #include <linux/types.h>
19 #include <linux/qcom_scm.h>
20 #include <linux/arm-smccc.h>
21 #include <linux/dma-mapping.h>
22
23 #include "qcom_scm.h"
24
25 #define QCOM_SCM_FNID(s, c) ((((s) & 0xFF) << 8) | ((c) & 0xFF))
26
27 #define MAX_QCOM_SCM_ARGS 10
28 #define MAX_QCOM_SCM_RETS 3
29
30 #define QCOM_SCM_ARGS_IMPL(num, a, b, c, d, e, f, g, h, i, j, ...) (\
31                            (((a) & 0x3) << 4) | \
32                            (((b) & 0x3) << 6) | \
33                            (((c) & 0x3) << 8) | \
34                            (((d) & 0x3) << 10) | \
35                            (((e) & 0x3) << 12) | \
36                            (((f) & 0x3) << 14) | \
37                            (((g) & 0x3) << 16) | \
38                            (((h) & 0x3) << 18) | \
39                            (((i) & 0x3) << 20) | \
40                            (((j) & 0x3) << 22) | \
41                            ((num) & 0xf))
42
43 #define QCOM_SCM_ARGS(...) QCOM_SCM_ARGS_IMPL(__VA_ARGS__, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
44
45 /**
46  * struct qcom_scm_desc
47  * @arginfo:    Metadata describing the arguments in args[]
48  * @args:       The array of arguments for the secure syscall
49  * @res:        The values returned by the secure syscall
50  */
51 struct qcom_scm_desc {
52         u32 arginfo;
53         u64 args[MAX_QCOM_SCM_ARGS];
54 };
55
56 static u64 qcom_smccc_convention = -1;
57 static DEFINE_MUTEX(qcom_scm_lock);
58
59 #define QCOM_SCM_EBUSY_WAIT_MS 30
60 #define QCOM_SCM_EBUSY_MAX_RETRY 20
61
62 #define N_EXT_QCOM_SCM_ARGS 7
63 #define FIRST_EXT_ARG_IDX 3
64 #define N_REGISTER_ARGS (MAX_QCOM_SCM_ARGS - N_EXT_QCOM_SCM_ARGS + 1)
65
66 /**
67  * qcom_scm_call() - Invoke a syscall in the secure world
68  * @dev:        device
69  * @svc_id:     service identifier
70  * @cmd_id:     command identifier
71  * @desc:       Descriptor structure containing arguments and return values
72  *
73  * Sends a command to the SCM and waits for the command to finish processing.
74  * This should *only* be called in pre-emptible context.
75 */
76 static int qcom_scm_call(struct device *dev, u32 svc_id, u32 cmd_id,
77                          const struct qcom_scm_desc *desc,
78                          struct arm_smccc_res *res)
79 {
80         int arglen = desc->arginfo & 0xf;
81         int retry_count = 0, i;
82         u32 fn_id = QCOM_SCM_FNID(svc_id, cmd_id);
83         u64 cmd, x5 = desc->args[FIRST_EXT_ARG_IDX];
84         dma_addr_t args_phys = 0;
85         void *args_virt = NULL;
86         size_t alloc_len;
87
88         if (unlikely(arglen > N_REGISTER_ARGS)) {
89                 alloc_len = N_EXT_QCOM_SCM_ARGS * sizeof(u64);
90                 args_virt = kzalloc(PAGE_ALIGN(alloc_len), GFP_KERNEL);
91
92                 if (!args_virt)
93                         return -ENOMEM;
94
95                 if (qcom_smccc_convention == ARM_SMCCC_SMC_32) {
96                         __le32 *args = args_virt;
97
98                         for (i = 0; i < N_EXT_QCOM_SCM_ARGS; i++)
99                                 args[i] = cpu_to_le32(desc->args[i +
100                                                       FIRST_EXT_ARG_IDX]);
101                 } else {
102                         __le64 *args = args_virt;
103
104                         for (i = 0; i < N_EXT_QCOM_SCM_ARGS; i++)
105                                 args[i] = cpu_to_le64(desc->args[i +
106                                                       FIRST_EXT_ARG_IDX]);
107                 }
108
109                 args_phys = dma_map_single(dev, args_virt, alloc_len,
110                                            DMA_TO_DEVICE);
111
112                 if (dma_mapping_error(dev, args_phys)) {
113                         kfree(args_virt);
114                         return -ENOMEM;
115                 }
116
117                 x5 = args_phys;
118         }
119
120         do {
121                 mutex_lock(&qcom_scm_lock);
122
123                 cmd = ARM_SMCCC_CALL_VAL(ARM_SMCCC_STD_CALL,
124                                          qcom_smccc_convention,
125                                          ARM_SMCCC_OWNER_SIP, fn_id);
126
127                 do {
128                         arm_smccc_smc(cmd, desc->arginfo, desc->args[0],
129                                       desc->args[1], desc->args[2], x5, 0, 0,
130                                       res);
131                 } while (res->a0 == QCOM_SCM_INTERRUPTED);
132
133                 mutex_unlock(&qcom_scm_lock);
134
135                 if (res->a0 == QCOM_SCM_V2_EBUSY) {
136                         if (retry_count++ > QCOM_SCM_EBUSY_MAX_RETRY)
137                                 break;
138                         msleep(QCOM_SCM_EBUSY_WAIT_MS);
139                 }
140         }  while (res->a0 == QCOM_SCM_V2_EBUSY);
141
142         if (args_virt) {
143                 dma_unmap_single(dev, args_phys, alloc_len, DMA_TO_DEVICE);
144                 kfree(args_virt);
145         }
146
147         if (res->a0 < 0)
148                 return qcom_scm_remap_error(res->a0);
149
150         return 0;
151 }
152
153 /**
154  * qcom_scm_set_cold_boot_addr() - Set the cold boot address for cpus
155  * @entry: Entry point function for the cpus
156  * @cpus: The cpumask of cpus that will use the entry point
157  *
158  * Set the cold boot address of the cpus. Any cpu outside the supported
159  * range would be removed from the cpu present mask.
160  */
161 int __qcom_scm_set_cold_boot_addr(void *entry, const cpumask_t *cpus)
162 {
163         return -ENOTSUPP;
164 }
165
166 /**
167  * qcom_scm_set_warm_boot_addr() - Set the warm boot address for cpus
168  * @dev: Device pointer
169  * @entry: Entry point function for the cpus
170  * @cpus: The cpumask of cpus that will use the entry point
171  *
172  * Set the Linux entry point for the SCM to transfer control to when coming
173  * out of a power down. CPU power down may be executed on cpuidle or hotplug.
174  */
175 int __qcom_scm_set_warm_boot_addr(struct device *dev, void *entry,
176                                   const cpumask_t *cpus)
177 {
178         return -ENOTSUPP;
179 }
180
181 /**
182  * qcom_scm_cpu_power_down() - Power down the cpu
183  * @flags - Flags to flush cache
184  *
185  * This is an end point to power down cpu. If there was a pending interrupt,
186  * the control would return from this function, otherwise, the cpu jumps to the
187  * warm boot entry point set for this cpu upon reset.
188  */
189 void __qcom_scm_cpu_power_down(u32 flags)
190 {
191 }
192
193 int __qcom_scm_is_call_available(struct device *dev, u32 svc_id, u32 cmd_id)
194 {
195         int ret;
196         struct qcom_scm_desc desc = {0};
197         struct arm_smccc_res res;
198
199         desc.arginfo = QCOM_SCM_ARGS(1);
200         desc.args[0] = QCOM_SCM_FNID(svc_id, cmd_id) |
201                         (ARM_SMCCC_OWNER_SIP << ARM_SMCCC_OWNER_SHIFT);
202
203         ret = qcom_scm_call(dev, QCOM_SCM_SVC_INFO, QCOM_IS_CALL_AVAIL_CMD,
204                             &desc, &res);
205
206         return ret ? : res.a1;
207 }
208
209 int __qcom_scm_hdcp_req(struct device *dev, struct qcom_scm_hdcp_req *req,
210                         u32 req_cnt, u32 *resp)
211 {
212         int ret;
213         struct qcom_scm_desc desc = {0};
214         struct arm_smccc_res res;
215
216         if (req_cnt > QCOM_SCM_HDCP_MAX_REQ_CNT)
217                 return -ERANGE;
218
219         desc.args[0] = req[0].addr;
220         desc.args[1] = req[0].val;
221         desc.args[2] = req[1].addr;
222         desc.args[3] = req[1].val;
223         desc.args[4] = req[2].addr;
224         desc.args[5] = req[2].val;
225         desc.args[6] = req[3].addr;
226         desc.args[7] = req[3].val;
227         desc.args[8] = req[4].addr;
228         desc.args[9] = req[4].val;
229         desc.arginfo = QCOM_SCM_ARGS(10);
230
231         ret = qcom_scm_call(dev, QCOM_SCM_SVC_HDCP, QCOM_SCM_CMD_HDCP, &desc,
232                             &res);
233         *resp = res.a1;
234
235         return ret;
236 }
237
238 void __qcom_scm_init(void)
239 {
240         u64 cmd;
241         struct arm_smccc_res res;
242         u32 function = QCOM_SCM_FNID(QCOM_SCM_SVC_INFO, QCOM_IS_CALL_AVAIL_CMD);
243
244         /* First try a SMC64 call */
245         cmd = ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, ARM_SMCCC_SMC_64,
246                                  ARM_SMCCC_OWNER_SIP, function);
247
248         arm_smccc_smc(cmd, QCOM_SCM_ARGS(1), cmd & (~BIT(ARM_SMCCC_TYPE_SHIFT)),
249                       0, 0, 0, 0, 0, &res);
250
251         if (!res.a0 && res.a1)
252                 qcom_smccc_convention = ARM_SMCCC_SMC_64;
253         else
254                 qcom_smccc_convention = ARM_SMCCC_SMC_32;
255 }