Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Amlogic Secure Monitor driver
4 *
5 * Copyright (C) 2016 Endless Mobile, Inc.
6 * Author: Carlo Caione <carlo@endlessm.com>
7 */
8
9#define pr_fmt(fmt) "meson-sm: " fmt
10
11#include <linux/arm-smccc.h>
12#include <linux/bug.h>
13#include <linux/io.h>
14#include <linux/module.h>
15#include <linux/of.h>
16#include <linux/of_platform.h>
17#include <linux/platform_device.h>
18#include <linux/printk.h>
19#include <linux/property.h>
20#include <linux/types.h>
21#include <linux/sizes.h>
22 #include <linux/slab.h>
23
24#include <linux/firmware/meson/meson_sm.h>
25
26struct meson_sm_cmd {
27 unsigned int index;
28 u32 smc_id;
29};
30#define CMD(d, s) { .index = (d), .smc_id = (s), }
31
32struct meson_sm_chip {
33 unsigned int shmem_size;
34 u32 cmd_shmem_in_base;
35 u32 cmd_shmem_out_base;
36 struct meson_sm_cmd cmd[];
37};
38
39static const struct meson_sm_chip gxbb_chip = {
40 .shmem_size = SZ_4K,
41 .cmd_shmem_in_base = 0x82000020,
42 .cmd_shmem_out_base = 0x82000021,
43 .cmd = {
44 CMD(SM_EFUSE_READ, 0x82000030),
45 CMD(SM_EFUSE_WRITE, 0x82000031),
46 CMD(SM_EFUSE_USER_MAX, 0x82000033),
47 CMD(SM_GET_CHIP_ID, 0x82000044),
48 CMD(SM_A1_PWRC_SET, 0x82000093),
49 CMD(SM_A1_PWRC_GET, 0x82000095),
50 { /* sentinel */ },
51 },
52};
53
54struct meson_sm_firmware {
55 const struct meson_sm_chip *chip;
56 void __iomem *sm_shmem_in_base;
57 void __iomem *sm_shmem_out_base;
58};
59
60static u32 meson_sm_get_cmd(const struct meson_sm_chip *chip,
61 unsigned int cmd_index)
62{
63 const struct meson_sm_cmd *cmd = chip->cmd;
64
65 while (cmd->smc_id && cmd->index != cmd_index)
66 cmd++;
67
68 return cmd->smc_id;
69}
70
71static s32 __meson_sm_call(u32 cmd, u32 arg0, u32 arg1, u32 arg2,
72 u32 arg3, u32 arg4)
73{
74 struct arm_smccc_res res;
75
76 arm_smccc_smc(cmd, arg0, arg1, arg2, arg3, arg4, 0, 0, &res);
77 return res.a0;
78}
79
80static void __iomem *meson_sm_map_shmem(u32 cmd_shmem, unsigned int size)
81{
82 u32 sm_phy_base;
83
84 sm_phy_base = __meson_sm_call(cmd_shmem, 0, 0, 0, 0, 0);
85 if (!sm_phy_base)
86 return NULL;
87
88 return ioremap_cache(sm_phy_base, size);
89}
90
91/**
92 * meson_sm_call - generic SMC32 call to the secure-monitor
93 *
94 * @fw: Pointer to secure-monitor firmware
95 * @cmd_index: Index of the SMC32 function ID
96 * @ret: Returned value
97 * @arg0: SMC32 Argument 0
98 * @arg1: SMC32 Argument 1
99 * @arg2: SMC32 Argument 2
100 * @arg3: SMC32 Argument 3
101 * @arg4: SMC32 Argument 4
102 *
103 * Return: 0 on success, a negative value on error
104 */
105int meson_sm_call(struct meson_sm_firmware *fw, unsigned int cmd_index,
106 s32 *ret, u32 arg0, u32 arg1, u32 arg2, u32 arg3, u32 arg4)
107{
108 u32 cmd;
109 s32 lret;
110
111 if (!fw->chip)
112 return -ENOENT;
113
114 cmd = meson_sm_get_cmd(fw->chip, cmd_index);
115 if (!cmd)
116 return -EINVAL;
117
118 lret = __meson_sm_call(cmd, arg0, arg1, arg2, arg3, arg4);
119
120 if (ret)
121 *ret = lret;
122
123 return 0;
124}
125EXPORT_SYMBOL(meson_sm_call);
126
127/**
128 * meson_sm_call_read - retrieve data from secure-monitor
129 *
130 * @fw: Pointer to secure-monitor firmware
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 */
144int meson_sm_call_read(struct meson_sm_firmware *fw, void *buffer,
145 unsigned int bsize, unsigned int cmd_index, u32 arg0,
146 u32 arg1, u32 arg2, u32 arg3, u32 arg4)
147{
148 s32 size;
149 int ret;
150
151 if (!fw->chip)
152 return -ENOENT;
153
154 if (!fw->chip->cmd_shmem_out_base)
155 return -EINVAL;
156
157 if (bsize > fw->chip->shmem_size)
158 return -EINVAL;
159
160 if (meson_sm_call(fw, cmd_index, &size, arg0, arg1, arg2, arg3, arg4) < 0)
161 return -EINVAL;
162
163 if (size < 0 || size > bsize)
164 return -EINVAL;
165
166 ret = size;
167
168 /* In some cases (for example GET_CHIP_ID command),
169 * SMC doesn't return the number of bytes read, even
170 * though the bytes were actually read into sm_shmem_out.
171 * So this check is needed.
172 */
173 if (!size)
174 size = bsize;
175
176 if (buffer)
177 memcpy(buffer, fw->sm_shmem_out_base, size);
178
179 return ret;
180}
181EXPORT_SYMBOL(meson_sm_call_read);
182
183/**
184 * meson_sm_call_write - send data to secure-monitor
185 *
186 * @fw: Pointer to secure-monitor firmware
187 * @buffer: Buffer containing data to send
188 * @size: Size of the data to send
189 * @cmd_index: Index of the SMC32 function ID
190 * @arg0: SMC32 Argument 0
191 * @arg1: SMC32 Argument 1
192 * @arg2: SMC32 Argument 2
193 * @arg3: SMC32 Argument 3
194 * @arg4: SMC32 Argument 4
195 *
196 * Return: size of sent data on success, a negative value on error
197 */
198int meson_sm_call_write(struct meson_sm_firmware *fw, void *buffer,
199 unsigned int size, unsigned int cmd_index, u32 arg0,
200 u32 arg1, u32 arg2, u32 arg3, u32 arg4)
201{
202 s32 written;
203
204 if (!fw->chip)
205 return -ENOENT;
206
207 if (size > fw->chip->shmem_size)
208 return -EINVAL;
209
210 if (!fw->chip->cmd_shmem_in_base)
211 return -EINVAL;
212
213 memcpy(fw->sm_shmem_in_base, buffer, size);
214
215 if (meson_sm_call(fw, cmd_index, &written, arg0, arg1, arg2, arg3, arg4) < 0)
216 return -EINVAL;
217
218 if (written <= 0 || written > size)
219 return -EINVAL;
220
221 return written;
222}
223EXPORT_SYMBOL(meson_sm_call_write);
224
225/**
226 * meson_sm_get - get pointer to meson_sm_firmware structure.
227 *
228 * @sm_node: Pointer to the secure-monitor Device Tree node.
229 *
230 * Return: NULL is the secure-monitor device is not ready.
231 */
232struct meson_sm_firmware *meson_sm_get(struct device_node *sm_node)
233{
234 struct platform_device *pdev = of_find_device_by_node(sm_node);
235
236 if (!pdev)
237 return NULL;
238
239 return platform_get_drvdata(pdev);
240}
241EXPORT_SYMBOL_GPL(meson_sm_get);
242
243#define SM_CHIP_ID_LENGTH 119
244#define SM_CHIP_ID_OFFSET 4
245#define SM_CHIP_ID_SIZE 12
246
247static ssize_t serial_show(struct device *dev, struct device_attribute *attr,
248 char *buf)
249{
250 struct platform_device *pdev = to_platform_device(dev);
251 struct meson_sm_firmware *fw;
252 uint8_t *id_buf;
253 int ret;
254
255 fw = platform_get_drvdata(pdev);
256
257 id_buf = kmalloc(SM_CHIP_ID_LENGTH, GFP_KERNEL);
258 if (!id_buf)
259 return -ENOMEM;
260
261 ret = meson_sm_call_read(fw, id_buf, SM_CHIP_ID_LENGTH, SM_GET_CHIP_ID,
262 0, 0, 0, 0, 0);
263 if (ret < 0) {
264 kfree(id_buf);
265 return ret;
266 }
267
268 ret = sprintf(buf, "%12phN\n", &id_buf[SM_CHIP_ID_OFFSET]);
269
270 kfree(id_buf);
271
272 return ret;
273}
274
275static DEVICE_ATTR_RO(serial);
276
277static struct attribute *meson_sm_sysfs_attrs[] = {
278 &dev_attr_serial.attr,
279 NULL,
280};
281ATTRIBUTE_GROUPS(meson_sm_sysfs);
282
283static const struct of_device_id meson_sm_ids[] = {
284 { .compatible = "amlogic,meson-gxbb-sm", .data = &gxbb_chip },
285 { /* sentinel */ },
286};
287
288static int __init meson_sm_probe(struct platform_device *pdev)
289{
290 struct device *dev = &pdev->dev;
291 const struct meson_sm_chip *chip;
292 struct meson_sm_firmware *fw;
293
294 fw = devm_kzalloc(dev, sizeof(*fw), GFP_KERNEL);
295 if (!fw)
296 return -ENOMEM;
297
298 chip = device_get_match_data(dev);
299 if (!chip)
300 return -EINVAL;
301
302 if (chip->cmd_shmem_in_base) {
303 fw->sm_shmem_in_base = meson_sm_map_shmem(chip->cmd_shmem_in_base,
304 chip->shmem_size);
305 if (WARN_ON(!fw->sm_shmem_in_base))
306 goto out;
307 }
308
309 if (chip->cmd_shmem_out_base) {
310 fw->sm_shmem_out_base = meson_sm_map_shmem(chip->cmd_shmem_out_base,
311 chip->shmem_size);
312 if (WARN_ON(!fw->sm_shmem_out_base))
313 goto unmap_in_base;
314 }
315
316 fw->chip = chip;
317
318 platform_set_drvdata(pdev, fw);
319
320 if (devm_of_platform_populate(dev))
321 goto unmap_out_base;
322
323 pr_info("secure-monitor enabled\n");
324
325 return 0;
326
327unmap_out_base:
328 iounmap(fw->sm_shmem_out_base);
329unmap_in_base:
330 iounmap(fw->sm_shmem_in_base);
331out:
332 return -EINVAL;
333}
334
335static struct platform_driver meson_sm_driver = {
336 .driver = {
337 .name = "meson-sm",
338 .of_match_table = of_match_ptr(meson_sm_ids),
339 .dev_groups = meson_sm_sysfs_groups,
340 },
341};
342module_platform_driver_probe(meson_sm_driver, meson_sm_probe);
343MODULE_LICENSE("GPL v2");
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Amlogic Secure Monitor driver
4 *
5 * Copyright (C) 2016 Endless Mobile, Inc.
6 * Author: Carlo Caione <carlo@endlessm.com>
7 */
8
9#define pr_fmt(fmt) "meson-sm: " fmt
10
11#include <linux/arm-smccc.h>
12#include <linux/bug.h>
13#include <linux/io.h>
14#include <linux/module.h>
15#include <linux/of.h>
16#include <linux/of_device.h>
17#include <linux/platform_device.h>
18#include <linux/printk.h>
19#include <linux/types.h>
20#include <linux/sizes.h>
21 #include <linux/slab.h>
22
23#include <linux/firmware/meson/meson_sm.h>
24
25struct meson_sm_cmd {
26 unsigned int index;
27 u32 smc_id;
28};
29#define CMD(d, s) { .index = (d), .smc_id = (s), }
30
31struct meson_sm_chip {
32 unsigned int shmem_size;
33 u32 cmd_shmem_in_base;
34 u32 cmd_shmem_out_base;
35 struct meson_sm_cmd cmd[];
36};
37
38struct meson_sm_chip gxbb_chip = {
39 .shmem_size = SZ_4K,
40 .cmd_shmem_in_base = 0x82000020,
41 .cmd_shmem_out_base = 0x82000021,
42 .cmd = {
43 CMD(SM_EFUSE_READ, 0x82000030),
44 CMD(SM_EFUSE_WRITE, 0x82000031),
45 CMD(SM_EFUSE_USER_MAX, 0x82000033),
46 CMD(SM_GET_CHIP_ID, 0x82000044),
47 { /* sentinel */ },
48 },
49};
50
51struct meson_sm_firmware {
52 const struct meson_sm_chip *chip;
53 void __iomem *sm_shmem_in_base;
54 void __iomem *sm_shmem_out_base;
55};
56
57static struct meson_sm_firmware fw;
58
59static u32 meson_sm_get_cmd(const struct meson_sm_chip *chip,
60 unsigned int cmd_index)
61{
62 const struct meson_sm_cmd *cmd = chip->cmd;
63
64 while (cmd->smc_id && cmd->index != cmd_index)
65 cmd++;
66
67 return cmd->smc_id;
68}
69
70static u32 __meson_sm_call(u32 cmd, u32 arg0, u32 arg1, u32 arg2,
71 u32 arg3, u32 arg4)
72{
73 struct arm_smccc_res res;
74
75 arm_smccc_smc(cmd, arg0, arg1, arg2, arg3, arg4, 0, 0, &res);
76 return res.a0;
77}
78
79static void __iomem *meson_sm_map_shmem(u32 cmd_shmem, unsigned int size)
80{
81 u32 sm_phy_base;
82
83 sm_phy_base = __meson_sm_call(cmd_shmem, 0, 0, 0, 0, 0);
84 if (!sm_phy_base)
85 return 0;
86
87 return ioremap_cache(sm_phy_base, size);
88}
89
90/**
91 * meson_sm_call - generic SMC32 call to the secure-monitor
92 *
93 * @cmd_index: Index of the SMC32 function ID
94 * @ret: Returned value
95 * @arg0: SMC32 Argument 0
96 * @arg1: SMC32 Argument 1
97 * @arg2: SMC32 Argument 2
98 * @arg3: SMC32 Argument 3
99 * @arg4: SMC32 Argument 4
100 *
101 * Return: 0 on success, a negative value on error
102 */
103int meson_sm_call(unsigned int cmd_index, u32 *ret, u32 arg0,
104 u32 arg1, u32 arg2, u32 arg3, u32 arg4)
105{
106 u32 cmd, lret;
107
108 if (!fw.chip)
109 return -ENOENT;
110
111 cmd = meson_sm_get_cmd(fw.chip, cmd_index);
112 if (!cmd)
113 return -EINVAL;
114
115 lret = __meson_sm_call(cmd, arg0, arg1, arg2, arg3, arg4);
116
117 if (ret)
118 *ret = lret;
119
120 return 0;
121}
122EXPORT_SYMBOL(meson_sm_call);
123
124/**
125 * meson_sm_call_read - retrieve data from secure-monitor
126 *
127 * @buffer: Buffer to store the retrieved data
128 * @bsize: Size of the buffer
129 * @cmd_index: Index of the SMC32 function ID
130 * @arg0: SMC32 Argument 0
131 * @arg1: SMC32 Argument 1
132 * @arg2: SMC32 Argument 2
133 * @arg3: SMC32 Argument 3
134 * @arg4: SMC32 Argument 4
135 *
136 * Return: size of read data on success, a negative value on error
137 * When 0 is returned there is no guarantee about the amount of
138 * data read and bsize bytes are copied in buffer.
139 */
140int meson_sm_call_read(void *buffer, unsigned int bsize, unsigned int cmd_index,
141 u32 arg0, u32 arg1, u32 arg2, u32 arg3, u32 arg4)
142{
143 u32 size;
144 int ret;
145
146 if (!fw.chip)
147 return -ENOENT;
148
149 if (!fw.chip->cmd_shmem_out_base)
150 return -EINVAL;
151
152 if (bsize > fw.chip->shmem_size)
153 return -EINVAL;
154
155 if (meson_sm_call(cmd_index, &size, arg0, arg1, arg2, arg3, arg4) < 0)
156 return -EINVAL;
157
158 if (size > bsize)
159 return -EINVAL;
160
161 ret = size;
162
163 if (!size)
164 size = bsize;
165
166 if (buffer)
167 memcpy(buffer, fw.sm_shmem_out_base, size);
168
169 return ret;
170}
171EXPORT_SYMBOL(meson_sm_call_read);
172
173/**
174 * meson_sm_call_write - send data to secure-monitor
175 *
176 * @buffer: Buffer containing data to send
177 * @size: Size of the data to send
178 * @cmd_index: Index of the SMC32 function ID
179 * @arg0: SMC32 Argument 0
180 * @arg1: SMC32 Argument 1
181 * @arg2: SMC32 Argument 2
182 * @arg3: SMC32 Argument 3
183 * @arg4: SMC32 Argument 4
184 *
185 * Return: size of sent data on success, a negative value on error
186 */
187int meson_sm_call_write(void *buffer, unsigned int size, unsigned int cmd_index,
188 u32 arg0, u32 arg1, u32 arg2, u32 arg3, u32 arg4)
189{
190 u32 written;
191
192 if (!fw.chip)
193 return -ENOENT;
194
195 if (size > fw.chip->shmem_size)
196 return -EINVAL;
197
198 if (!fw.chip->cmd_shmem_in_base)
199 return -EINVAL;
200
201 memcpy(fw.sm_shmem_in_base, buffer, size);
202
203 if (meson_sm_call(cmd_index, &written, arg0, arg1, arg2, arg3, arg4) < 0)
204 return -EINVAL;
205
206 if (!written)
207 return -EINVAL;
208
209 return written;
210}
211EXPORT_SYMBOL(meson_sm_call_write);
212
213#define SM_CHIP_ID_LENGTH 119
214#define SM_CHIP_ID_OFFSET 4
215#define SM_CHIP_ID_SIZE 12
216
217static ssize_t serial_show(struct device *dev, struct device_attribute *attr,
218 char *buf)
219{
220 uint8_t *id_buf;
221 int ret;
222
223 id_buf = kmalloc(SM_CHIP_ID_LENGTH, GFP_KERNEL);
224 if (!id_buf)
225 return -ENOMEM;
226
227 ret = meson_sm_call_read(id_buf, SM_CHIP_ID_LENGTH, SM_GET_CHIP_ID,
228 0, 0, 0, 0, 0);
229 if (ret < 0) {
230 kfree(id_buf);
231 return ret;
232 }
233
234 ret = sprintf(buf, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
235 id_buf[SM_CHIP_ID_OFFSET + 0],
236 id_buf[SM_CHIP_ID_OFFSET + 1],
237 id_buf[SM_CHIP_ID_OFFSET + 2],
238 id_buf[SM_CHIP_ID_OFFSET + 3],
239 id_buf[SM_CHIP_ID_OFFSET + 4],
240 id_buf[SM_CHIP_ID_OFFSET + 5],
241 id_buf[SM_CHIP_ID_OFFSET + 6],
242 id_buf[SM_CHIP_ID_OFFSET + 7],
243 id_buf[SM_CHIP_ID_OFFSET + 8],
244 id_buf[SM_CHIP_ID_OFFSET + 9],
245 id_buf[SM_CHIP_ID_OFFSET + 10],
246 id_buf[SM_CHIP_ID_OFFSET + 11]);
247
248 kfree(id_buf);
249
250 return ret;
251}
252
253static DEVICE_ATTR_RO(serial);
254
255static struct attribute *meson_sm_sysfs_attributes[] = {
256 &dev_attr_serial.attr,
257 NULL,
258};
259
260static const struct attribute_group meson_sm_sysfs_attr_group = {
261 .attrs = meson_sm_sysfs_attributes,
262};
263
264static const struct of_device_id meson_sm_ids[] = {
265 { .compatible = "amlogic,meson-gxbb-sm", .data = &gxbb_chip },
266 { /* sentinel */ },
267};
268
269static int __init meson_sm_probe(struct platform_device *pdev)
270{
271 const struct meson_sm_chip *chip;
272
273 chip = of_match_device(meson_sm_ids, &pdev->dev)->data;
274
275 if (chip->cmd_shmem_in_base) {
276 fw.sm_shmem_in_base = meson_sm_map_shmem(chip->cmd_shmem_in_base,
277 chip->shmem_size);
278 if (WARN_ON(!fw.sm_shmem_in_base))
279 goto out;
280 }
281
282 if (chip->cmd_shmem_out_base) {
283 fw.sm_shmem_out_base = meson_sm_map_shmem(chip->cmd_shmem_out_base,
284 chip->shmem_size);
285 if (WARN_ON(!fw.sm_shmem_out_base))
286 goto out_in_base;
287 }
288
289 fw.chip = chip;
290 pr_info("secure-monitor enabled\n");
291
292 if (sysfs_create_group(&pdev->dev.kobj, &meson_sm_sysfs_attr_group))
293 goto out_in_base;
294
295 return 0;
296
297out_in_base:
298 iounmap(fw.sm_shmem_in_base);
299out:
300 return -EINVAL;
301}
302
303static struct platform_driver meson_sm_driver = {
304 .driver = {
305 .name = "meson-sm",
306 .of_match_table = of_match_ptr(meson_sm_ids),
307 },
308};
309module_platform_driver_probe(meson_sm_driver, meson_sm_probe);