meson_sm.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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/firmware/meson/meson_sm.h>
  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. struct 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. struct meson_sm_chip gxbb_chip = {
  38. .shmem_size = SZ_4K,
  39. .cmd_shmem_in_base = 0x82000020,
  40. .cmd_shmem_out_base = 0x82000021,
  41. .cmd = {
  42. CMD(SM_EFUSE_READ, 0x82000030),
  43. CMD(SM_EFUSE_WRITE, 0x82000031),
  44. CMD(SM_EFUSE_USER_MAX, 0x82000033),
  45. { /* sentinel */ },
  46. },
  47. };
  48. struct meson_sm_firmware {
  49. const struct meson_sm_chip *chip;
  50. void __iomem *sm_shmem_in_base;
  51. void __iomem *sm_shmem_out_base;
  52. };
  53. static struct meson_sm_firmware fw;
  54. static u32 meson_sm_get_cmd(const struct meson_sm_chip *chip,
  55. unsigned int cmd_index)
  56. {
  57. const struct meson_sm_cmd *cmd = chip->cmd;
  58. while (cmd->smc_id && cmd->index != cmd_index)
  59. cmd++;
  60. return cmd->smc_id;
  61. }
  62. static u32 __meson_sm_call(u32 cmd, u32 arg0, u32 arg1, u32 arg2,
  63. u32 arg3, u32 arg4)
  64. {
  65. struct arm_smccc_res res;
  66. arm_smccc_smc(cmd, arg0, arg1, arg2, arg3, arg4, 0, 0, &res);
  67. return res.a0;
  68. }
  69. static void __iomem *meson_sm_map_shmem(u32 cmd_shmem, unsigned int size)
  70. {
  71. u32 sm_phy_base;
  72. sm_phy_base = __meson_sm_call(cmd_shmem, 0, 0, 0, 0, 0);
  73. if (!sm_phy_base)
  74. return 0;
  75. return ioremap_cache(sm_phy_base, size);
  76. }
  77. /**
  78. * meson_sm_call - generic SMC32 call to the secure-monitor
  79. *
  80. * @cmd_index: Index of the SMC32 function ID
  81. * @ret: Returned value
  82. * @arg0: SMC32 Argument 0
  83. * @arg1: SMC32 Argument 1
  84. * @arg2: SMC32 Argument 2
  85. * @arg3: SMC32 Argument 3
  86. * @arg4: SMC32 Argument 4
  87. *
  88. * Return: 0 on success, a negative value on error
  89. */
  90. int meson_sm_call(unsigned int cmd_index, u32 *ret, u32 arg0,
  91. u32 arg1, u32 arg2, u32 arg3, u32 arg4)
  92. {
  93. u32 cmd, 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. * @buffer: Buffer to store the retrieved data
  109. * @bsize: Size of the buffer
  110. * @cmd_index: Index of the SMC32 function ID
  111. * @arg0: SMC32 Argument 0
  112. * @arg1: SMC32 Argument 1
  113. * @arg2: SMC32 Argument 2
  114. * @arg3: SMC32 Argument 3
  115. * @arg4: SMC32 Argument 4
  116. *
  117. * Return: size of read data on success, a negative value on error
  118. * When 0 is returned there is no guarantee about the amount of
  119. * data read and bsize bytes are copied in buffer.
  120. */
  121. int meson_sm_call_read(void *buffer, unsigned int bsize, unsigned int cmd_index,
  122. u32 arg0, u32 arg1, u32 arg2, u32 arg3, u32 arg4)
  123. {
  124. u32 size;
  125. int ret;
  126. if (!fw.chip)
  127. return -ENOENT;
  128. if (!fw.chip->cmd_shmem_out_base)
  129. return -EINVAL;
  130. if (bsize > fw.chip->shmem_size)
  131. return -EINVAL;
  132. if (meson_sm_call(cmd_index, &size, arg0, arg1, arg2, arg3, arg4) < 0)
  133. return -EINVAL;
  134. if (size > bsize)
  135. return -EINVAL;
  136. ret = size;
  137. if (!size)
  138. size = bsize;
  139. if (buffer)
  140. memcpy(buffer, fw.sm_shmem_out_base, size);
  141. return ret;
  142. }
  143. EXPORT_SYMBOL(meson_sm_call_read);
  144. /**
  145. * meson_sm_call_write - send data to secure-monitor
  146. *
  147. * @buffer: Buffer containing data to send
  148. * @size: Size of the data to send
  149. * @cmd_index: Index of the SMC32 function ID
  150. * @arg0: SMC32 Argument 0
  151. * @arg1: SMC32 Argument 1
  152. * @arg2: SMC32 Argument 2
  153. * @arg3: SMC32 Argument 3
  154. * @arg4: SMC32 Argument 4
  155. *
  156. * Return: size of sent data on success, a negative value on error
  157. */
  158. int meson_sm_call_write(void *buffer, unsigned int size, unsigned int cmd_index,
  159. u32 arg0, u32 arg1, u32 arg2, u32 arg3, u32 arg4)
  160. {
  161. u32 written;
  162. if (!fw.chip)
  163. return -ENOENT;
  164. if (size > fw.chip->shmem_size)
  165. return -EINVAL;
  166. if (!fw.chip->cmd_shmem_in_base)
  167. return -EINVAL;
  168. memcpy(fw.sm_shmem_in_base, buffer, size);
  169. if (meson_sm_call(cmd_index, &written, arg0, arg1, arg2, arg3, arg4) < 0)
  170. return -EINVAL;
  171. if (!written)
  172. return -EINVAL;
  173. return written;
  174. }
  175. EXPORT_SYMBOL(meson_sm_call_write);
  176. static const struct of_device_id meson_sm_ids[] = {
  177. { .compatible = "amlogic,meson-gxbb-sm", .data = &gxbb_chip },
  178. { /* sentinel */ },
  179. };
  180. static int __init meson_sm_probe(struct platform_device *pdev)
  181. {
  182. const struct meson_sm_chip *chip;
  183. chip = of_match_device(meson_sm_ids, &pdev->dev)->data;
  184. if (chip->cmd_shmem_in_base) {
  185. fw.sm_shmem_in_base = meson_sm_map_shmem(chip->cmd_shmem_in_base,
  186. chip->shmem_size);
  187. if (WARN_ON(!fw.sm_shmem_in_base))
  188. goto out;
  189. }
  190. if (chip->cmd_shmem_out_base) {
  191. fw.sm_shmem_out_base = meson_sm_map_shmem(chip->cmd_shmem_out_base,
  192. chip->shmem_size);
  193. if (WARN_ON(!fw.sm_shmem_out_base))
  194. goto out_in_base;
  195. }
  196. fw.chip = chip;
  197. pr_info("secure-monitor enabled\n");
  198. return 0;
  199. out_in_base:
  200. iounmap(fw.sm_shmem_in_base);
  201. out:
  202. return -EINVAL;
  203. }
  204. static struct platform_driver meson_sm_driver = {
  205. .driver = {
  206. .name = "meson-sm",
  207. .of_match_table = of_match_ptr(meson_sm_ids),
  208. },
  209. };
  210. module_platform_driver_probe(meson_sm_driver, meson_sm_probe);