sm.c 4.7 KB

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