omap-secure.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * OMAP Secure API infrastructure.
  4. *
  5. * Copyright (C) 2011 Texas Instruments, Inc.
  6. * Santosh Shilimkar <[email protected]>
  7. * Copyright (C) 2012 Ivaylo Dimitrov <[email protected]>
  8. * Copyright (C) 2013 Pali Rohár <[email protected]>
  9. */
  10. #include <linux/arm-smccc.h>
  11. #include <linux/cpu_pm.h>
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/io.h>
  15. #include <linux/memblock.h>
  16. #include <linux/of.h>
  17. #include <asm/cacheflush.h>
  18. #include <asm/memblock.h>
  19. #include "common.h"
  20. #include "omap-secure.h"
  21. #include "soc.h"
  22. static phys_addr_t omap_secure_memblock_base;
  23. bool optee_available;
  24. #define OMAP_SIP_SMC_STD_CALL_VAL(func_num) \
  25. ARM_SMCCC_CALL_VAL(ARM_SMCCC_STD_CALL, ARM_SMCCC_SMC_32, \
  26. ARM_SMCCC_OWNER_SIP, (func_num))
  27. static void __init omap_optee_init_check(void)
  28. {
  29. struct device_node *np;
  30. /*
  31. * We only check that the OP-TEE node is present and available. The
  32. * OP-TEE kernel driver is not needed for the type of interaction made
  33. * with OP-TEE here so the driver's status is not checked.
  34. */
  35. np = of_find_node_by_path("/firmware/optee");
  36. if (np && of_device_is_available(np))
  37. optee_available = true;
  38. of_node_put(np);
  39. }
  40. /**
  41. * omap_sec_dispatcher: Routine to dispatch low power secure
  42. * service routines
  43. * @idx: The HAL API index
  44. * @flag: The flag indicating criticality of operation
  45. * @nargs: Number of valid arguments out of four.
  46. * @arg1, arg2, arg3 args4: Parameters passed to secure API
  47. *
  48. * Return the non-zero error value on failure.
  49. */
  50. u32 omap_secure_dispatcher(u32 idx, u32 flag, u32 nargs, u32 arg1, u32 arg2,
  51. u32 arg3, u32 arg4)
  52. {
  53. static u32 buf[NR_CPUS][5];
  54. u32 *param;
  55. int cpu;
  56. u32 ret;
  57. cpu = get_cpu();
  58. param = buf[cpu];
  59. param[0] = nargs;
  60. param[1] = arg1;
  61. param[2] = arg2;
  62. param[3] = arg3;
  63. param[4] = arg4;
  64. /*
  65. * Secure API needs physical address
  66. * pointer for the parameters
  67. */
  68. flush_cache_all();
  69. outer_clean_range(__pa(param), __pa(param + 5));
  70. ret = omap_smc2(idx, flag, __pa(param));
  71. put_cpu();
  72. return ret;
  73. }
  74. void omap_smccc_smc(u32 fn, u32 arg)
  75. {
  76. struct arm_smccc_res res;
  77. arm_smccc_smc(OMAP_SIP_SMC_STD_CALL_VAL(fn), arg,
  78. 0, 0, 0, 0, 0, 0, &res);
  79. WARN(res.a0, "Secure function call 0x%08x failed\n", fn);
  80. }
  81. void omap_smc1(u32 fn, u32 arg)
  82. {
  83. /*
  84. * If this platform has OP-TEE installed we use ARM SMC calls
  85. * otherwise fall back to the OMAP ROM style calls.
  86. */
  87. if (optee_available)
  88. omap_smccc_smc(fn, arg);
  89. else
  90. _omap_smc1(fn, arg);
  91. }
  92. /* Allocate the memory to save secure ram */
  93. int __init omap_secure_ram_reserve_memblock(void)
  94. {
  95. u32 size = OMAP_SECURE_RAM_STORAGE;
  96. size = ALIGN(size, SECTION_SIZE);
  97. omap_secure_memblock_base = arm_memblock_steal(size, SECTION_SIZE);
  98. return 0;
  99. }
  100. phys_addr_t omap_secure_ram_mempool_base(void)
  101. {
  102. return omap_secure_memblock_base;
  103. }
  104. #if defined(CONFIG_ARCH_OMAP3) && defined(CONFIG_PM)
  105. u32 omap3_save_secure_ram(void *addr, int size)
  106. {
  107. static u32 param[5];
  108. u32 ret;
  109. if (size != OMAP3_SAVE_SECURE_RAM_SZ)
  110. return OMAP3_SAVE_SECURE_RAM_SZ;
  111. param[0] = 4; /* Number of arguments */
  112. param[1] = __pa(addr); /* Physical address for saving */
  113. param[2] = 0;
  114. param[3] = 1;
  115. param[4] = 1;
  116. ret = save_secure_ram_context(__pa(param));
  117. return ret;
  118. }
  119. #endif
  120. /**
  121. * rx51_secure_dispatcher: Routine to dispatch secure PPA API calls
  122. * @idx: The PPA API index
  123. * @process: Process ID
  124. * @flag: The flag indicating criticality of operation
  125. * @nargs: Number of valid arguments out of four.
  126. * @arg1, arg2, arg3 args4: Parameters passed to secure API
  127. *
  128. * Return the non-zero error value on failure.
  129. *
  130. * NOTE: rx51_secure_dispatcher differs from omap_secure_dispatcher because
  131. * it calling omap_smc3() instead omap_smc2() and param[0] is nargs+1
  132. */
  133. u32 rx51_secure_dispatcher(u32 idx, u32 process, u32 flag, u32 nargs,
  134. u32 arg1, u32 arg2, u32 arg3, u32 arg4)
  135. {
  136. static u32 param[5];
  137. u32 ret;
  138. param[0] = nargs+1; /* RX-51 needs number of arguments + 1 */
  139. param[1] = arg1;
  140. param[2] = arg2;
  141. param[3] = arg3;
  142. param[4] = arg4;
  143. /*
  144. * Secure API needs physical address
  145. * pointer for the parameters
  146. */
  147. local_irq_disable();
  148. local_fiq_disable();
  149. flush_cache_all();
  150. outer_clean_range(__pa(param), __pa(param + 5));
  151. ret = omap_smc3(idx, process, flag, __pa(param));
  152. flush_cache_all();
  153. local_fiq_enable();
  154. local_irq_enable();
  155. return ret;
  156. }
  157. /**
  158. * rx51_secure_update_aux_cr: Routine to modify the contents of Auxiliary Control Register
  159. * @set_bits: bits to set in ACR
  160. * @clr_bits: bits to clear in ACR
  161. *
  162. * Return the non-zero error value on failure.
  163. */
  164. u32 rx51_secure_update_aux_cr(u32 set_bits, u32 clear_bits)
  165. {
  166. u32 acr;
  167. /* Read ACR */
  168. asm volatile ("mrc p15, 0, %0, c1, c0, 1" : "=r" (acr));
  169. acr &= ~clear_bits;
  170. acr |= set_bits;
  171. return rx51_secure_dispatcher(RX51_PPA_WRITE_ACR,
  172. 0,
  173. FLAG_START_CRITICAL,
  174. 1, acr, 0, 0, 0);
  175. }
  176. /**
  177. * rx51_secure_rng_call: Routine for HW random generator
  178. */
  179. u32 rx51_secure_rng_call(u32 ptr, u32 count, u32 flag)
  180. {
  181. return rx51_secure_dispatcher(RX51_PPA_HWRNG,
  182. 0,
  183. NO_FLAG,
  184. 3, ptr, count, flag, 0);
  185. }
  186. void __init omap_secure_init(void)
  187. {
  188. omap_optee_init_check();
  189. }
  190. /*
  191. * Dummy dispatcher call after core OSWR and MPU off. Updates the ROM return
  192. * address after MMU has been re-enabled after CPU1 has been woken up again.
  193. * Otherwise the ROM code will attempt to use the earlier physical return
  194. * address that got set with MMU off when waking up CPU1. Only used on secure
  195. * devices.
  196. */
  197. static int cpu_notifier(struct notifier_block *nb, unsigned long cmd, void *v)
  198. {
  199. switch (cmd) {
  200. case CPU_CLUSTER_PM_EXIT:
  201. omap_secure_dispatcher(OMAP4_PPA_SERVICE_0,
  202. FLAG_START_CRITICAL,
  203. 0, 0, 0, 0, 0);
  204. break;
  205. default:
  206. break;
  207. }
  208. return NOTIFY_OK;
  209. }
  210. static struct notifier_block secure_notifier_block = {
  211. .notifier_call = cpu_notifier,
  212. };
  213. static int __init secure_pm_init(void)
  214. {
  215. if (omap_type() == OMAP2_DEVICE_TYPE_GP || !soc_is_omap44xx())
  216. return 0;
  217. cpu_pm_register_notifier(&secure_notifier_block);
  218. return 0;
  219. }
  220. omap_arch_initcall(secure_pm_init);