sm.c 4.6 KB

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