meson_sm.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. #define pr_fmt(fmt) "meson-sm: " fmt
  15. #include <linux/arm-smccc.h>
  16. #include <linux/bug.h>
  17. #include <linux/io.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/of_device.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/printk.h>
  23. #include <linux/types.h>
  24. #include <linux/sizes.h>
  25. #include <linux/slab.h>
  26. #include <linux/firmware/meson/meson_sm.h>
  27. struct meson_sm_cmd {
  28. unsigned int index;
  29. u32 smc_id;
  30. };
  31. #define CMD(d, s) { .index = (d), .smc_id = (s), }
  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. struct 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. struct meson_sm_firmware {
  51. const struct meson_sm_chip *chip;
  52. void __iomem *sm_shmem_in_base;
  53. void __iomem *sm_shmem_out_base;
  54. };
  55. static struct meson_sm_firmware fw;
  56. static u32 meson_sm_get_cmd(const struct meson_sm_chip *chip,
  57. unsigned int cmd_index)
  58. {
  59. const struct meson_sm_cmd *cmd = chip->cmd;
  60. while (cmd->smc_id && cmd->index != cmd_index)
  61. cmd++;
  62. return cmd->smc_id;
  63. }
  64. static u32 __meson_sm_call(u32 cmd, u32 arg0, u32 arg1, u32 arg2,
  65. u32 arg3, u32 arg4)
  66. {
  67. struct arm_smccc_res res;
  68. arm_smccc_smc(cmd, arg0, arg1, arg2, arg3, arg4, 0, 0, &res);
  69. return res.a0;
  70. }
  71. static void __iomem *meson_sm_map_shmem(u32 cmd_shmem, unsigned int size)
  72. {
  73. u32 sm_phy_base;
  74. sm_phy_base = __meson_sm_call(cmd_shmem, 0, 0, 0, 0, 0);
  75. if (!sm_phy_base)
  76. return 0;
  77. return ioremap_cache(sm_phy_base, size);
  78. }
  79. /**
  80. * meson_sm_call - generic SMC32 call to the secure-monitor
  81. *
  82. * @cmd_index: Index of the SMC32 function ID
  83. * @ret: Returned value
  84. * @arg0: SMC32 Argument 0
  85. * @arg1: SMC32 Argument 1
  86. * @arg2: SMC32 Argument 2
  87. * @arg3: SMC32 Argument 3
  88. * @arg4: SMC32 Argument 4
  89. *
  90. * Return: 0 on success, a negative value on error
  91. */
  92. int meson_sm_call(unsigned int cmd_index, u32 *ret, u32 arg0,
  93. u32 arg1, u32 arg2, u32 arg3, u32 arg4)
  94. {
  95. u32 cmd, lret;
  96. if (!fw.chip)
  97. return -ENOENT;
  98. cmd = meson_sm_get_cmd(fw.chip, cmd_index);
  99. if (!cmd)
  100. return -EINVAL;
  101. lret = __meson_sm_call(cmd, arg0, arg1, arg2, arg3, arg4);
  102. if (ret)
  103. *ret = lret;
  104. return 0;
  105. }
  106. EXPORT_SYMBOL(meson_sm_call);
  107. /**
  108. * meson_sm_call_read - retrieve data from secure-monitor
  109. *
  110. * @buffer: Buffer to store the retrieved data
  111. * @bsize: Size of the buffer
  112. * @cmd_index: Index of the SMC32 function ID
  113. * @arg0: SMC32 Argument 0
  114. * @arg1: SMC32 Argument 1
  115. * @arg2: SMC32 Argument 2
  116. * @arg3: SMC32 Argument 3
  117. * @arg4: SMC32 Argument 4
  118. *
  119. * Return: size of read data on success, a negative value on error
  120. * When 0 is returned there is no guarantee about the amount of
  121. * data read and bsize bytes are copied in buffer.
  122. */
  123. int meson_sm_call_read(void *buffer, unsigned int bsize, unsigned int cmd_index,
  124. u32 arg0, u32 arg1, u32 arg2, u32 arg3, u32 arg4)
  125. {
  126. u32 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(cmd_index, &size, arg0, arg1, arg2, arg3, arg4) < 0)
  135. return -EINVAL;
  136. if (size > bsize)
  137. return -EINVAL;
  138. ret = size;
  139. if (!size)
  140. size = bsize;
  141. if (buffer)
  142. memcpy(buffer, fw.sm_shmem_out_base, size);
  143. return ret;
  144. }
  145. EXPORT_SYMBOL(meson_sm_call_read);
  146. /**
  147. * meson_sm_call_write - send data to secure-monitor
  148. *
  149. * @buffer: Buffer containing data to send
  150. * @size: Size of the data to send
  151. * @cmd_index: Index of the SMC32 function ID
  152. * @arg0: SMC32 Argument 0
  153. * @arg1: SMC32 Argument 1
  154. * @arg2: SMC32 Argument 2
  155. * @arg3: SMC32 Argument 3
  156. * @arg4: SMC32 Argument 4
  157. *
  158. * Return: size of sent data on success, a negative value on error
  159. */
  160. int meson_sm_call_write(void *buffer, unsigned int size, unsigned int cmd_index,
  161. u32 arg0, u32 arg1, u32 arg2, u32 arg3, u32 arg4)
  162. {
  163. u32 written;
  164. if (!fw.chip)
  165. return -ENOENT;
  166. if (size > fw.chip->shmem_size)
  167. return -EINVAL;
  168. if (!fw.chip->cmd_shmem_in_base)
  169. return -EINVAL;
  170. memcpy(fw.sm_shmem_in_base, buffer, size);
  171. if (meson_sm_call(cmd_index, &written, arg0, arg1, arg2, arg3, arg4) < 0)
  172. return -EINVAL;
  173. if (!written)
  174. return -EINVAL;
  175. return written;
  176. }
  177. EXPORT_SYMBOL(meson_sm_call_write);
  178. #define SM_CHIP_ID_LENGTH 119
  179. #define SM_CHIP_ID_OFFSET 4
  180. #define SM_CHIP_ID_SIZE 12
  181. static ssize_t serial_show(struct device *dev, struct device_attribute *attr,
  182. char *buf)
  183. {
  184. uint8_t *id_buf;
  185. int ret;
  186. id_buf = kmalloc(SM_CHIP_ID_LENGTH, GFP_KERNEL);
  187. if (!id_buf)
  188. return -ENOMEM;
  189. ret = meson_sm_call_read(id_buf, SM_CHIP_ID_LENGTH, SM_GET_CHIP_ID,
  190. 0, 0, 0, 0, 0);
  191. if (ret < 0) {
  192. kfree(id_buf);
  193. return ret;
  194. }
  195. ret = sprintf(buf, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  196. id_buf[SM_CHIP_ID_OFFSET + 0],
  197. id_buf[SM_CHIP_ID_OFFSET + 1],
  198. id_buf[SM_CHIP_ID_OFFSET + 2],
  199. id_buf[SM_CHIP_ID_OFFSET + 3],
  200. id_buf[SM_CHIP_ID_OFFSET + 4],
  201. id_buf[SM_CHIP_ID_OFFSET + 5],
  202. id_buf[SM_CHIP_ID_OFFSET + 6],
  203. id_buf[SM_CHIP_ID_OFFSET + 7],
  204. id_buf[SM_CHIP_ID_OFFSET + 8],
  205. id_buf[SM_CHIP_ID_OFFSET + 9],
  206. id_buf[SM_CHIP_ID_OFFSET + 10],
  207. id_buf[SM_CHIP_ID_OFFSET + 11]);
  208. kfree(id_buf);
  209. return ret;
  210. }
  211. static DEVICE_ATTR_RO(serial);
  212. static struct attribute *meson_sm_sysfs_attributes[] = {
  213. &dev_attr_serial.attr,
  214. NULL,
  215. };
  216. static const struct attribute_group meson_sm_sysfs_attr_group = {
  217. .attrs = meson_sm_sysfs_attributes,
  218. };
  219. static const struct of_device_id meson_sm_ids[] = {
  220. { .compatible = "amlogic,meson-gxbb-sm", .data = &gxbb_chip },
  221. { /* sentinel */ },
  222. };
  223. static int __init meson_sm_probe(struct platform_device *pdev)
  224. {
  225. const struct meson_sm_chip *chip;
  226. chip = of_match_device(meson_sm_ids, &pdev->dev)->data;
  227. if (chip->cmd_shmem_in_base) {
  228. fw.sm_shmem_in_base = meson_sm_map_shmem(chip->cmd_shmem_in_base,
  229. chip->shmem_size);
  230. if (WARN_ON(!fw.sm_shmem_in_base))
  231. goto out;
  232. }
  233. if (chip->cmd_shmem_out_base) {
  234. fw.sm_shmem_out_base = meson_sm_map_shmem(chip->cmd_shmem_out_base,
  235. chip->shmem_size);
  236. if (WARN_ON(!fw.sm_shmem_out_base))
  237. goto out_in_base;
  238. }
  239. fw.chip = chip;
  240. pr_info("secure-monitor enabled\n");
  241. if (sysfs_create_group(&pdev->dev.kobj, &meson_sm_sysfs_attr_group))
  242. goto out_in_base;
  243. return 0;
  244. out_in_base:
  245. iounmap(fw.sm_shmem_in_base);
  246. out:
  247. return -EINVAL;
  248. }
  249. static struct platform_driver meson_sm_driver = {
  250. .driver = {
  251. .name = "meson-sm",
  252. .of_match_table = of_match_ptr(meson_sm_ids),
  253. },
  254. };
  255. module_platform_driver_probe(meson_sm_driver, meson_sm_probe);