sm.c 4.8 KB

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