sm.c 4.7 KB

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