sm.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2016 Beniamino Galvani <b.galvani@gmail.com>
  4. *
  5. * Secure monitor calls.
  6. */
  7. #include <common.h>
  8. #include <command.h>
  9. #include <env.h>
  10. #include <log.h>
  11. #include <asm/arch/sm.h>
  12. #include <asm/cache.h>
  13. #include <asm/global_data.h>
  14. #include <asm/ptrace.h>
  15. #include <linux/bitops.h>
  16. #include <linux/err.h>
  17. #include <linux/kernel.h>
  18. #include <dm.h>
  19. #include <linux/bitfield.h>
  20. #include <regmap.h>
  21. #include <syscon.h>
  22. #define FN_GET_SHARE_MEM_INPUT_BASE 0x82000020
  23. #define FN_GET_SHARE_MEM_OUTPUT_BASE 0x82000021
  24. #define FN_EFUSE_READ 0x82000030
  25. #define FN_EFUSE_WRITE 0x82000031
  26. #define FN_CHIP_ID 0x82000044
  27. static void *shmem_input;
  28. static void *shmem_output;
  29. static void meson_init_shmem(void)
  30. {
  31. struct pt_regs regs;
  32. if (shmem_input && shmem_output)
  33. return;
  34. regs.regs[0] = FN_GET_SHARE_MEM_INPUT_BASE;
  35. smc_call(&regs);
  36. shmem_input = (void *)regs.regs[0];
  37. regs.regs[0] = FN_GET_SHARE_MEM_OUTPUT_BASE;
  38. smc_call(&regs);
  39. shmem_output = (void *)regs.regs[0];
  40. debug("Secure Monitor shmem: 0x%p 0x%p\n", shmem_input, shmem_output);
  41. }
  42. ssize_t meson_sm_read_efuse(uintptr_t offset, void *buffer, size_t size)
  43. {
  44. struct pt_regs regs;
  45. meson_init_shmem();
  46. regs.regs[0] = FN_EFUSE_READ;
  47. regs.regs[1] = offset;
  48. regs.regs[2] = size;
  49. smc_call(&regs);
  50. if (regs.regs[0] == 0)
  51. return -1;
  52. memcpy(buffer, shmem_output, min(size, regs.regs[0]));
  53. return regs.regs[0];
  54. }
  55. #define SM_CHIP_ID_LENGTH 119
  56. #define SM_CHIP_ID_OFFSET 4
  57. #define SM_CHIP_ID_SIZE 12
  58. int meson_sm_get_serial(void *buffer, size_t size)
  59. {
  60. struct pt_regs regs;
  61. meson_init_shmem();
  62. regs.regs[0] = FN_CHIP_ID;
  63. regs.regs[1] = 0;
  64. regs.regs[2] = 0;
  65. smc_call(&regs);
  66. memcpy(buffer, shmem_output + SM_CHIP_ID_OFFSET,
  67. min_t(size_t, size, SM_CHIP_ID_SIZE));
  68. return 0;
  69. }
  70. #define AO_SEC_SD_CFG15 0xfc
  71. #define REBOOT_REASON_MASK GENMASK(15, 12)
  72. int meson_sm_get_reboot_reason(void)
  73. {
  74. struct regmap *regmap;
  75. int nodeoffset;
  76. ofnode node;
  77. unsigned int reason;
  78. /* find the offset of compatible node */
  79. nodeoffset = fdt_node_offset_by_compatible(gd->fdt_blob, -1,
  80. "amlogic,meson-gx-ao-secure");
  81. if (nodeoffset < 0) {
  82. printf("%s: failed to get amlogic,meson-gx-ao-secure\n",
  83. __func__);
  84. return -ENODEV;
  85. }
  86. /* get regmap from the syscon node */
  87. node = offset_to_ofnode(nodeoffset);
  88. regmap = syscon_node_to_regmap(node);
  89. if (IS_ERR(regmap)) {
  90. printf("%s: failed to get regmap\n", __func__);
  91. return -EINVAL;
  92. }
  93. regmap_read(regmap, AO_SEC_SD_CFG15, &reason);
  94. /* The SMC call is not used, we directly use AO_SEC_SD_CFG15 */
  95. return FIELD_GET(REBOOT_REASON_MASK, reason);
  96. }
  97. static int do_sm_serial(struct cmd_tbl *cmdtp, int flag, int argc,
  98. char *const argv[])
  99. {
  100. ulong address;
  101. int ret;
  102. if (argc < 2)
  103. return CMD_RET_USAGE;
  104. address = simple_strtoul(argv[1], NULL, 0);
  105. ret = meson_sm_get_serial((void *)address, SM_CHIP_ID_SIZE);
  106. if (ret)
  107. return CMD_RET_FAILURE;
  108. return CMD_RET_SUCCESS;
  109. }
  110. #define MAX_REBOOT_REASONS 14
  111. static const char *reboot_reasons[MAX_REBOOT_REASONS] = {
  112. [REBOOT_REASON_COLD] = "cold_boot",
  113. [REBOOT_REASON_NORMAL] = "normal",
  114. [REBOOT_REASON_RECOVERY] = "recovery",
  115. [REBOOT_REASON_UPDATE] = "update",
  116. [REBOOT_REASON_FASTBOOT] = "fastboot",
  117. [REBOOT_REASON_SUSPEND_OFF] = "suspend_off",
  118. [REBOOT_REASON_HIBERNATE] = "hibernate",
  119. [REBOOT_REASON_BOOTLOADER] = "bootloader",
  120. [REBOOT_REASON_SHUTDOWN_REBOOT] = "shutdown_reboot",
  121. [REBOOT_REASON_RPMBP] = "rpmbp",
  122. [REBOOT_REASON_CRASH_DUMP] = "crash_dump",
  123. [REBOOT_REASON_KERNEL_PANIC] = "kernel_panic",
  124. [REBOOT_REASON_WATCHDOG_REBOOT] = "watchdog_reboot",
  125. };
  126. static int do_sm_reboot_reason(struct cmd_tbl *cmdtp, int flag, int argc,
  127. char *const argv[])
  128. {
  129. const char *reason_str;
  130. char *destarg = NULL;
  131. int reason;
  132. if (argc > 1)
  133. destarg = argv[1];
  134. reason = meson_sm_get_reboot_reason();
  135. if (reason < 0)
  136. return CMD_RET_FAILURE;
  137. if (reason >= MAX_REBOOT_REASONS ||
  138. !reboot_reasons[reason])
  139. reason_str = "unknown";
  140. else
  141. reason_str = reboot_reasons[reason];
  142. if (destarg)
  143. env_set(destarg, reason_str);
  144. else
  145. printf("reboot reason: %s (%x)\n", reason_str, reason);
  146. return CMD_RET_SUCCESS;
  147. }
  148. static struct cmd_tbl cmd_sm_sub[] = {
  149. U_BOOT_CMD_MKENT(serial, 2, 1, do_sm_serial, "", ""),
  150. U_BOOT_CMD_MKENT(reboot_reason, 1, 1, do_sm_reboot_reason, "", ""),
  151. };
  152. static int do_sm(struct cmd_tbl *cmdtp, int flag, int argc,
  153. char *const argv[])
  154. {
  155. struct cmd_tbl *c;
  156. if (argc < 2)
  157. return CMD_RET_USAGE;
  158. /* Strip off leading 'sm' command argument */
  159. argc--;
  160. argv++;
  161. c = find_cmd_tbl(argv[0], &cmd_sm_sub[0], ARRAY_SIZE(cmd_sm_sub));
  162. if (c)
  163. return c->cmd(cmdtp, flag, argc, argv);
  164. else
  165. return CMD_RET_USAGE;
  166. }
  167. U_BOOT_CMD(
  168. sm, 5, 0, do_sm,
  169. "Secure Monitor Control",
  170. "serial <address> - read chip unique id to memory address\n"
  171. "sm reboot_reason [name] - get reboot reason and store to to environment"
  172. );