sm.c 4.7 KB

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