meson_sm.c 7.8 KB

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