xref: /linux/drivers/firmware/meson/meson_sm.c (revision 2b109eccd80b660e58d4523e8b80ce11bc3e532e)
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 
26 struct meson_sm_cmd {
27 	unsigned int index;
28 	u32 smc_id;
29 };
30 #define CMD(d, s) { .index = (d), .smc_id = (s), }
31 
32 struct 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 
39 static 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_THERMAL_CALIB_READ,	0x82000047),
49 		CMD(SM_A1_PWRC_SET,		0x82000093),
50 		CMD(SM_A1_PWRC_GET,		0x82000095),
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 u32 meson_sm_get_cmd(const struct meson_sm_chip *chip,
62 			    unsigned int cmd_index)
63 {
64 	const struct meson_sm_cmd *cmd = chip->cmd;
65 
66 	while (cmd->smc_id && cmd->index != cmd_index)
67 		cmd++;
68 
69 	return cmd->smc_id;
70 }
71 
72 static s32 __meson_sm_call(u32 cmd, u32 arg0, u32 arg1, u32 arg2,
73 			   u32 arg3, u32 arg4)
74 {
75 	struct arm_smccc_res res;
76 
77 	arm_smccc_smc(cmd, arg0, arg1, arg2, arg3, arg4, 0, 0, &res);
78 	return res.a0;
79 }
80 
81 static void __iomem *meson_sm_map_shmem(u32 cmd_shmem, unsigned int size)
82 {
83 	u32 sm_phy_base;
84 
85 	sm_phy_base = __meson_sm_call(cmd_shmem, 0, 0, 0, 0, 0);
86 	if (!sm_phy_base)
87 		return NULL;
88 
89 	return ioremap_cache(sm_phy_base, size);
90 }
91 
92 /**
93  * meson_sm_call - generic SMC32 call to the secure-monitor
94  *
95  * @fw:		Pointer to secure-monitor firmware
96  * @cmd_index:	Index of the SMC32 function ID
97  * @ret:	Returned value
98  * @arg0:	SMC32 Argument 0
99  * @arg1:	SMC32 Argument 1
100  * @arg2:	SMC32 Argument 2
101  * @arg3:	SMC32 Argument 3
102  * @arg4:	SMC32 Argument 4
103  *
104  * Return:	0 on success, a negative value on error
105  */
106 int meson_sm_call(struct meson_sm_firmware *fw, unsigned int cmd_index,
107 		  s32 *ret, u32 arg0, u32 arg1, u32 arg2, u32 arg3, u32 arg4)
108 {
109 	u32 cmd;
110 	s32 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  * @fw:		Pointer to secure-monitor firmware
132  * @buffer:	Buffer to store the retrieved data
133  * @bsize:	Size of the buffer
134  * @cmd_index:	Index of the SMC32 function ID
135  * @arg0:	SMC32 Argument 0
136  * @arg1:	SMC32 Argument 1
137  * @arg2:	SMC32 Argument 2
138  * @arg3:	SMC32 Argument 3
139  * @arg4:	SMC32 Argument 4
140  *
141  * Return:	size of read data on success, a negative value on error
142  *		When 0 is returned there is no guarantee about the amount of
143  *		data read and bsize bytes are copied in buffer.
144  */
145 int meson_sm_call_read(struct meson_sm_firmware *fw, void *buffer,
146 		       unsigned int bsize, unsigned int cmd_index, u32 arg0,
147 		       u32 arg1, u32 arg2, u32 arg3, u32 arg4)
148 {
149 	s32 size;
150 	int ret;
151 
152 	if (!fw->chip)
153 		return -ENOENT;
154 
155 	if (!fw->chip->cmd_shmem_out_base)
156 		return -EINVAL;
157 
158 	if (bsize > fw->chip->shmem_size)
159 		return -EINVAL;
160 
161 	if (meson_sm_call(fw, cmd_index, &size, arg0, arg1, arg2, arg3, arg4) < 0)
162 		return -EINVAL;
163 
164 	if (size < 0 || size > bsize)
165 		return -EINVAL;
166 
167 	ret = size;
168 
169 	/* In some cases (for example GET_CHIP_ID command),
170 	 * SMC doesn't return the number of bytes read, even
171 	 * though the bytes were actually read into sm_shmem_out.
172 	 * So this check is needed.
173 	 */
174 	if (!size)
175 		size = bsize;
176 
177 	if (buffer)
178 		memcpy(buffer, fw->sm_shmem_out_base, size);
179 
180 	return ret;
181 }
182 EXPORT_SYMBOL(meson_sm_call_read);
183 
184 /**
185  * meson_sm_call_write - send data to secure-monitor
186  *
187  * @fw:		Pointer to secure-monitor firmware
188  * @buffer:	Buffer containing data to send
189  * @size:	Size of the data to send
190  * @cmd_index:	Index of the SMC32 function ID
191  * @arg0:	SMC32 Argument 0
192  * @arg1:	SMC32 Argument 1
193  * @arg2:	SMC32 Argument 2
194  * @arg3:	SMC32 Argument 3
195  * @arg4:	SMC32 Argument 4
196  *
197  * Return:	size of sent data on success, a negative value on error
198  */
199 int meson_sm_call_write(struct meson_sm_firmware *fw, void *buffer,
200 			unsigned int size, unsigned int cmd_index, u32 arg0,
201 			u32 arg1, u32 arg2, u32 arg3, u32 arg4)
202 {
203 	s32 written;
204 
205 	if (!fw->chip)
206 		return -ENOENT;
207 
208 	if (size > fw->chip->shmem_size)
209 		return -EINVAL;
210 
211 	if (!fw->chip->cmd_shmem_in_base)
212 		return -EINVAL;
213 
214 	memcpy(fw->sm_shmem_in_base, buffer, size);
215 
216 	if (meson_sm_call(fw, cmd_index, &written, arg0, arg1, arg2, arg3, arg4) < 0)
217 		return -EINVAL;
218 
219 	if (written <= 0 || written > size)
220 		return -EINVAL;
221 
222 	return written;
223 }
224 EXPORT_SYMBOL(meson_sm_call_write);
225 
226 /**
227  * meson_sm_get - get pointer to meson_sm_firmware structure.
228  *
229  * @sm_node:		Pointer to the secure-monitor Device Tree node.
230  *
231  * Return:		NULL is the secure-monitor device is not ready.
232  */
233 struct meson_sm_firmware *meson_sm_get(struct device_node *sm_node)
234 {
235 	struct platform_device *pdev = of_find_device_by_node(sm_node);
236 	struct meson_sm_firmware *fw;
237 
238 	if (!pdev)
239 		return NULL;
240 
241 	fw = platform_get_drvdata(pdev);
242 
243 	put_device(&pdev->dev);
244 
245 	return fw;
246 }
247 EXPORT_SYMBOL_GPL(meson_sm_get);
248 
249 /**
250  * meson_sm_get_thermal_calib - Read thermal sensor calibration data.
251  * @fw:		Pointer to secure-monitor firmware.
252  * @trim_info:	Pointer to store the returned calibration data.
253  * @tsensor_id:	Sensor index to identify which sensor's calibration data
254  *		to retrieve
255  *
256  * Return: 0 on success, negative error code on failure.
257  */
258 int meson_sm_get_thermal_calib(struct meson_sm_firmware *fw, u32 *trim_info,
259 			       u32 tsensor_id)
260 {
261 	return meson_sm_call(fw, SM_THERMAL_CALIB_READ, trim_info, tsensor_id,
262 			     0, 0, 0, 0);
263 }
264 EXPORT_SYMBOL_GPL(meson_sm_get_thermal_calib);
265 
266 #define SM_CHIP_ID_LENGTH	119
267 #define SM_CHIP_ID_OFFSET	4
268 #define SM_CHIP_ID_SIZE		12
269 
270 static ssize_t serial_show(struct device *dev, struct device_attribute *attr,
271 			 char *buf)
272 {
273 	struct platform_device *pdev = to_platform_device(dev);
274 	struct meson_sm_firmware *fw;
275 	uint8_t *id_buf;
276 	int ret;
277 
278 	fw = platform_get_drvdata(pdev);
279 
280 	id_buf = kmalloc(SM_CHIP_ID_LENGTH, GFP_KERNEL);
281 	if (!id_buf)
282 		return -ENOMEM;
283 
284 	ret = meson_sm_call_read(fw, id_buf, SM_CHIP_ID_LENGTH, SM_GET_CHIP_ID,
285 				 0, 0, 0, 0, 0);
286 	if (ret < 0) {
287 		kfree(id_buf);
288 		return ret;
289 	}
290 
291 	ret = sprintf(buf, "%12phN\n", &id_buf[SM_CHIP_ID_OFFSET]);
292 
293 	kfree(id_buf);
294 
295 	return ret;
296 }
297 
298 static DEVICE_ATTR_RO(serial);
299 
300 static struct attribute *meson_sm_sysfs_attrs[] = {
301 	&dev_attr_serial.attr,
302 	NULL,
303 };
304 ATTRIBUTE_GROUPS(meson_sm_sysfs);
305 
306 static const struct of_device_id meson_sm_ids[] = {
307 	{ .compatible = "amlogic,meson-gxbb-sm", .data = &gxbb_chip },
308 	{ /* sentinel */ },
309 };
310 
311 static int __init meson_sm_probe(struct platform_device *pdev)
312 {
313 	struct device *dev = &pdev->dev;
314 	const struct meson_sm_chip *chip;
315 	struct meson_sm_firmware *fw;
316 
317 	fw = devm_kzalloc(dev, sizeof(*fw), GFP_KERNEL);
318 	if (!fw)
319 		return -ENOMEM;
320 
321 	chip = device_get_match_data(dev);
322 	if (!chip)
323 		return -EINVAL;
324 
325 	if (chip->cmd_shmem_in_base) {
326 		fw->sm_shmem_in_base = meson_sm_map_shmem(chip->cmd_shmem_in_base,
327 							  chip->shmem_size);
328 		if (WARN_ON(!fw->sm_shmem_in_base))
329 			goto out;
330 	}
331 
332 	if (chip->cmd_shmem_out_base) {
333 		fw->sm_shmem_out_base = meson_sm_map_shmem(chip->cmd_shmem_out_base,
334 							   chip->shmem_size);
335 		if (WARN_ON(!fw->sm_shmem_out_base))
336 			goto unmap_in_base;
337 	}
338 
339 	fw->chip = chip;
340 
341 	platform_set_drvdata(pdev, fw);
342 
343 	if (devm_of_platform_populate(dev))
344 		goto unmap_out_base;
345 
346 	pr_info("secure-monitor enabled\n");
347 
348 	return 0;
349 
350 unmap_out_base:
351 	iounmap(fw->sm_shmem_out_base);
352 unmap_in_base:
353 	iounmap(fw->sm_shmem_in_base);
354 out:
355 	return -EINVAL;
356 }
357 
358 static struct platform_driver meson_sm_driver = {
359 	.driver = {
360 		.name = "meson-sm",
361 		.of_match_table = of_match_ptr(meson_sm_ids),
362 		.dev_groups = meson_sm_sysfs_groups,
363 	},
364 };
365 module_platform_driver_probe(meson_sm_driver, meson_sm_probe);
366 MODULE_DESCRIPTION("Amlogic Secure Monitor driver");
367 MODULE_LICENSE("GPL v2");
368