GNU Linux-libre 4.19.304-gnu1
[releases.git] / drivers / firmware / meson / meson_sm.c
1 /*
2  * Amlogic Secure Monitor driver
3  *
4  * Copyright (C) 2016 Endless Mobile, Inc.
5  * Author: Carlo Caione <carlo@endlessm.com>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * version 2 as published by the Free Software Foundation.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program. If not, see <http://www.gnu.org/licenses/>.
13  */
14
15 #define pr_fmt(fmt) "meson-sm: " fmt
16
17 #include <linux/arm-smccc.h>
18 #include <linux/bug.h>
19 #include <linux/io.h>
20 #include <linux/module.h>
21 #include <linux/of.h>
22 #include <linux/of_device.h>
23 #include <linux/platform_device.h>
24 #include <linux/printk.h>
25 #include <linux/types.h>
26 #include <linux/sizes.h>
27
28 #include <linux/firmware/meson/meson_sm.h>
29
30 struct meson_sm_cmd {
31         unsigned int index;
32         u32 smc_id;
33 };
34 #define CMD(d, s) { .index = (d), .smc_id = (s), }
35
36 struct meson_sm_chip {
37         unsigned int shmem_size;
38         u32 cmd_shmem_in_base;
39         u32 cmd_shmem_out_base;
40         struct meson_sm_cmd cmd[];
41 };
42
43 struct meson_sm_chip gxbb_chip = {
44         .shmem_size             = SZ_4K,
45         .cmd_shmem_in_base      = 0x82000020,
46         .cmd_shmem_out_base     = 0x82000021,
47         .cmd = {
48                 CMD(SM_EFUSE_READ,      0x82000030),
49                 CMD(SM_EFUSE_WRITE,     0x82000031),
50                 CMD(SM_EFUSE_USER_MAX,  0x82000033),
51                 { /* sentinel */ },
52         },
53 };
54
55 struct meson_sm_firmware {
56         const struct meson_sm_chip *chip;
57         void __iomem *sm_shmem_in_base;
58         void __iomem *sm_shmem_out_base;
59 };
60
61 static struct meson_sm_firmware fw;
62
63 static u32 meson_sm_get_cmd(const struct meson_sm_chip *chip,
64                             unsigned int cmd_index)
65 {
66         const struct meson_sm_cmd *cmd = chip->cmd;
67
68         while (cmd->smc_id && cmd->index != cmd_index)
69                 cmd++;
70
71         return cmd->smc_id;
72 }
73
74 static u32 __meson_sm_call(u32 cmd, u32 arg0, u32 arg1, u32 arg2,
75                            u32 arg3, u32 arg4)
76 {
77         struct arm_smccc_res res;
78
79         arm_smccc_smc(cmd, arg0, arg1, arg2, arg3, arg4, 0, 0, &res);
80         return res.a0;
81 }
82
83 static void __iomem *meson_sm_map_shmem(u32 cmd_shmem, unsigned int size)
84 {
85         u32 sm_phy_base;
86
87         sm_phy_base = __meson_sm_call(cmd_shmem, 0, 0, 0, 0, 0);
88         if (!sm_phy_base)
89                 return 0;
90
91         return ioremap_cache(sm_phy_base, size);
92 }
93
94 /**
95  * meson_sm_call - generic SMC32 call to the secure-monitor
96  *
97  * @cmd_index:  Index of the SMC32 function ID
98  * @ret:        Returned value
99  * @arg0:       SMC32 Argument 0
100  * @arg1:       SMC32 Argument 1
101  * @arg2:       SMC32 Argument 2
102  * @arg3:       SMC32 Argument 3
103  * @arg4:       SMC32 Argument 4
104  *
105  * Return:      0 on success, a negative value on error
106  */
107 int meson_sm_call(unsigned int cmd_index, u32 *ret, u32 arg0,
108                   u32 arg1, u32 arg2, u32 arg3, u32 arg4)
109 {
110         u32 cmd, lret;
111
112         if (!fw.chip)
113                 return -ENOENT;
114
115         cmd = meson_sm_get_cmd(fw.chip, cmd_index);
116         if (!cmd)
117                 return -EINVAL;
118
119         lret = __meson_sm_call(cmd, arg0, arg1, arg2, arg3, arg4);
120
121         if (ret)
122                 *ret = lret;
123
124         return 0;
125 }
126 EXPORT_SYMBOL(meson_sm_call);
127
128 /**
129  * meson_sm_call_read - retrieve data from secure-monitor
130  *
131  * @buffer:     Buffer to store the retrieved data
132  * @bsize:      Size of the buffer
133  * @cmd_index:  Index of the SMC32 function ID
134  * @arg0:       SMC32 Argument 0
135  * @arg1:       SMC32 Argument 1
136  * @arg2:       SMC32 Argument 2
137  * @arg3:       SMC32 Argument 3
138  * @arg4:       SMC32 Argument 4
139  *
140  * Return:      size of read data on success, a negative value on error
141  *              When 0 is returned there is no guarantee about the amount of
142  *              data read and bsize bytes are copied in buffer.
143  */
144 int meson_sm_call_read(void *buffer, unsigned int bsize, unsigned int cmd_index,
145                        u32 arg0, u32 arg1, u32 arg2, u32 arg3, u32 arg4)
146 {
147         u32 size;
148         int ret;
149
150         if (!fw.chip)
151                 return -ENOENT;
152
153         if (!fw.chip->cmd_shmem_out_base)
154                 return -EINVAL;
155
156         if (bsize > fw.chip->shmem_size)
157                 return -EINVAL;
158
159         if (meson_sm_call(cmd_index, &size, arg0, arg1, arg2, arg3, arg4) < 0)
160                 return -EINVAL;
161
162         if (size > bsize)
163                 return -EINVAL;
164
165         ret = size;
166
167         if (!size)
168                 size = bsize;
169
170         if (buffer)
171                 memcpy(buffer, fw.sm_shmem_out_base, size);
172
173         return ret;
174 }
175 EXPORT_SYMBOL(meson_sm_call_read);
176
177 /**
178  * meson_sm_call_write - send data to secure-monitor
179  *
180  * @buffer:     Buffer containing data to send
181  * @size:       Size of the data to send
182  * @cmd_index:  Index of the SMC32 function ID
183  * @arg0:       SMC32 Argument 0
184  * @arg1:       SMC32 Argument 1
185  * @arg2:       SMC32 Argument 2
186  * @arg3:       SMC32 Argument 3
187  * @arg4:       SMC32 Argument 4
188  *
189  * Return:      size of sent data on success, a negative value on error
190  */
191 int meson_sm_call_write(void *buffer, unsigned int size, unsigned int cmd_index,
192                         u32 arg0, u32 arg1, u32 arg2, u32 arg3, u32 arg4)
193 {
194         u32 written;
195
196         if (!fw.chip)
197                 return -ENOENT;
198
199         if (size > fw.chip->shmem_size)
200                 return -EINVAL;
201
202         if (!fw.chip->cmd_shmem_in_base)
203                 return -EINVAL;
204
205         memcpy(fw.sm_shmem_in_base, buffer, size);
206
207         if (meson_sm_call(cmd_index, &written, arg0, arg1, arg2, arg3, arg4) < 0)
208                 return -EINVAL;
209
210         if (!written)
211                 return -EINVAL;
212
213         return written;
214 }
215 EXPORT_SYMBOL(meson_sm_call_write);
216
217 static const struct of_device_id meson_sm_ids[] = {
218         { .compatible = "amlogic,meson-gxbb-sm", .data = &gxbb_chip },
219         { /* sentinel */ },
220 };
221
222 static int __init meson_sm_probe(struct platform_device *pdev)
223 {
224         const struct meson_sm_chip *chip;
225
226         chip = of_match_device(meson_sm_ids, &pdev->dev)->data;
227
228         if (chip->cmd_shmem_in_base) {
229                 fw.sm_shmem_in_base = meson_sm_map_shmem(chip->cmd_shmem_in_base,
230                                                          chip->shmem_size);
231                 if (WARN_ON(!fw.sm_shmem_in_base))
232                         goto out;
233         }
234
235         if (chip->cmd_shmem_out_base) {
236                 fw.sm_shmem_out_base = meson_sm_map_shmem(chip->cmd_shmem_out_base,
237                                                           chip->shmem_size);
238                 if (WARN_ON(!fw.sm_shmem_out_base))
239                         goto out_in_base;
240         }
241
242         fw.chip = chip;
243         pr_info("secure-monitor enabled\n");
244
245         return 0;
246
247 out_in_base:
248         iounmap(fw.sm_shmem_in_base);
249 out:
250         return -EINVAL;
251 }
252
253 static struct platform_driver meson_sm_driver = {
254         .driver = {
255                 .name = "meson-sm",
256                 .of_match_table = of_match_ptr(meson_sm_ids),
257         },
258 };
259 module_platform_driver_probe(meson_sm_driver, meson_sm_probe);