meson_sm.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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/of.h>
  19. #include <linux/of_device.h>
  20. #include <linux/printk.h>
  21. #include <linux/types.h>
  22. #include <linux/sizes.h>
  23. #include <linux/firmware/meson/meson_sm.h>
  24. struct meson_sm_cmd {
  25. unsigned int index;
  26. u32 smc_id;
  27. };
  28. #define CMD(d, s) { .index = (d), .smc_id = (s), }
  29. struct meson_sm_chip {
  30. unsigned int shmem_size;
  31. u32 cmd_shmem_in_base;
  32. u32 cmd_shmem_out_base;
  33. struct meson_sm_cmd cmd[];
  34. };
  35. struct meson_sm_chip gxbb_chip = {
  36. .shmem_size = SZ_4K,
  37. .cmd_shmem_in_base = 0x82000020,
  38. .cmd_shmem_out_base = 0x82000021,
  39. .cmd = {
  40. CMD(SM_EFUSE_READ, 0x82000030),
  41. CMD(SM_EFUSE_WRITE, 0x82000031),
  42. CMD(SM_EFUSE_USER_MAX, 0x82000033),
  43. { /* sentinel */ },
  44. },
  45. };
  46. struct meson_sm_firmware {
  47. const struct meson_sm_chip *chip;
  48. void __iomem *sm_shmem_in_base;
  49. void __iomem *sm_shmem_out_base;
  50. };
  51. static struct meson_sm_firmware fw;
  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 u32 __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 0;
  73. return ioremap_cache(sm_phy_base, size);
  74. }
  75. /**
  76. * meson_sm_call - generic SMC32 call to the secure-monitor
  77. *
  78. * @cmd_index: Index of the SMC32 function ID
  79. * @ret: Returned value
  80. * @arg0: SMC32 Argument 0
  81. * @arg1: SMC32 Argument 1
  82. * @arg2: SMC32 Argument 2
  83. * @arg3: SMC32 Argument 3
  84. * @arg4: SMC32 Argument 4
  85. *
  86. * Return: 0 on success, a negative value on error
  87. */
  88. int meson_sm_call(unsigned int cmd_index, u32 *ret, u32 arg0,
  89. u32 arg1, u32 arg2, u32 arg3, u32 arg4)
  90. {
  91. u32 cmd, lret;
  92. if (!fw.chip)
  93. return -ENOENT;
  94. cmd = meson_sm_get_cmd(fw.chip, cmd_index);
  95. if (!cmd)
  96. return -EINVAL;
  97. lret = __meson_sm_call(cmd, arg0, arg1, arg2, arg3, arg4);
  98. if (ret)
  99. *ret = lret;
  100. return 0;
  101. }
  102. EXPORT_SYMBOL(meson_sm_call);
  103. /**
  104. * meson_sm_call_read - retrieve data from secure-monitor
  105. *
  106. * @buffer: Buffer to store the retrieved data
  107. * @bsize: Size of the buffer
  108. * @cmd_index: Index of the SMC32 function ID
  109. * @arg0: SMC32 Argument 0
  110. * @arg1: SMC32 Argument 1
  111. * @arg2: SMC32 Argument 2
  112. * @arg3: SMC32 Argument 3
  113. * @arg4: SMC32 Argument 4
  114. *
  115. * Return: size of read data on success, a negative value on error
  116. * When 0 is returned there is no guarantee about the amount of
  117. * data read and bsize bytes are copied in buffer.
  118. */
  119. int meson_sm_call_read(void *buffer, unsigned int bsize, unsigned int cmd_index,
  120. u32 arg0, u32 arg1, u32 arg2, u32 arg3, u32 arg4)
  121. {
  122. u32 size;
  123. int ret;
  124. if (!fw.chip)
  125. return -ENOENT;
  126. if (!fw.chip->cmd_shmem_out_base)
  127. return -EINVAL;
  128. if (bsize > fw.chip->shmem_size)
  129. return -EINVAL;
  130. if (meson_sm_call(cmd_index, &size, arg0, arg1, arg2, arg3, arg4) < 0)
  131. return -EINVAL;
  132. if (size > bsize)
  133. return -EINVAL;
  134. ret = size;
  135. if (!size)
  136. size = bsize;
  137. if (buffer)
  138. memcpy(buffer, fw.sm_shmem_out_base, size);
  139. return ret;
  140. }
  141. EXPORT_SYMBOL(meson_sm_call_read);
  142. /**
  143. * meson_sm_call_write - send data to secure-monitor
  144. *
  145. * @buffer: Buffer containing data to send
  146. * @size: Size of the data to send
  147. * @cmd_index: Index of the SMC32 function ID
  148. * @arg0: SMC32 Argument 0
  149. * @arg1: SMC32 Argument 1
  150. * @arg2: SMC32 Argument 2
  151. * @arg3: SMC32 Argument 3
  152. * @arg4: SMC32 Argument 4
  153. *
  154. * Return: size of sent data on success, a negative value on error
  155. */
  156. int meson_sm_call_write(void *buffer, unsigned int size, unsigned int cmd_index,
  157. u32 arg0, u32 arg1, u32 arg2, u32 arg3, u32 arg4)
  158. {
  159. u32 written;
  160. if (!fw.chip)
  161. return -ENOENT;
  162. if (size > fw.chip->shmem_size)
  163. return -EINVAL;
  164. if (!fw.chip->cmd_shmem_in_base)
  165. return -EINVAL;
  166. memcpy(fw.sm_shmem_in_base, buffer, size);
  167. if (meson_sm_call(cmd_index, &written, arg0, arg1, arg2, arg3, arg4) < 0)
  168. return -EINVAL;
  169. if (!written)
  170. return -EINVAL;
  171. return written;
  172. }
  173. EXPORT_SYMBOL(meson_sm_call_write);
  174. static const struct of_device_id meson_sm_ids[] = {
  175. { .compatible = "amlogic,meson-gxbb-sm", .data = &gxbb_chip },
  176. { /* sentinel */ },
  177. };
  178. int __init meson_sm_init(void)
  179. {
  180. const struct meson_sm_chip *chip;
  181. const struct of_device_id *matched_np;
  182. struct device_node *np;
  183. np = of_find_matching_node_and_match(NULL, meson_sm_ids, &matched_np);
  184. if (!np)
  185. return -ENODEV;
  186. chip = matched_np->data;
  187. if (!chip) {
  188. pr_err("unable to setup secure-monitor data\n");
  189. goto out;
  190. }
  191. if (chip->cmd_shmem_in_base) {
  192. fw.sm_shmem_in_base = meson_sm_map_shmem(chip->cmd_shmem_in_base,
  193. chip->shmem_size);
  194. if (WARN_ON(!fw.sm_shmem_in_base))
  195. goto out;
  196. }
  197. if (chip->cmd_shmem_out_base) {
  198. fw.sm_shmem_out_base = meson_sm_map_shmem(chip->cmd_shmem_out_base,
  199. chip->shmem_size);
  200. if (WARN_ON(!fw.sm_shmem_out_base))
  201. goto out_in_base;
  202. }
  203. fw.chip = chip;
  204. pr_info("secure-monitor enabled\n");
  205. return 0;
  206. out_in_base:
  207. iounmap(fw.sm_shmem_in_base);
  208. out:
  209. return -EINVAL;
  210. }
  211. device_initcall(meson_sm_init);