0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/arm-smccc.h>
0012 #include <linux/cpu_pm.h>
0013 #include <linux/kernel.h>
0014 #include <linux/init.h>
0015 #include <linux/io.h>
0016 #include <linux/memblock.h>
0017 #include <linux/of.h>
0018
0019 #include <asm/cacheflush.h>
0020 #include <asm/memblock.h>
0021
0022 #include "common.h"
0023 #include "omap-secure.h"
0024 #include "soc.h"
0025
0026 static phys_addr_t omap_secure_memblock_base;
0027
0028 bool optee_available;
0029
0030 #define OMAP_SIP_SMC_STD_CALL_VAL(func_num) \
0031 ARM_SMCCC_CALL_VAL(ARM_SMCCC_STD_CALL, ARM_SMCCC_SMC_32, \
0032 ARM_SMCCC_OWNER_SIP, (func_num))
0033
0034 static void __init omap_optee_init_check(void)
0035 {
0036 struct device_node *np;
0037
0038
0039
0040
0041
0042
0043 np = of_find_node_by_path("/firmware/optee");
0044 if (np && of_device_is_available(np))
0045 optee_available = true;
0046 of_node_put(np);
0047 }
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059 u32 omap_secure_dispatcher(u32 idx, u32 flag, u32 nargs, u32 arg1, u32 arg2,
0060 u32 arg3, u32 arg4)
0061 {
0062 static u32 buf[NR_CPUS][5];
0063 u32 *param;
0064 int cpu;
0065 u32 ret;
0066
0067 cpu = get_cpu();
0068 param = buf[cpu];
0069
0070 param[0] = nargs;
0071 param[1] = arg1;
0072 param[2] = arg2;
0073 param[3] = arg3;
0074 param[4] = arg4;
0075
0076
0077
0078
0079
0080 flush_cache_all();
0081 outer_clean_range(__pa(param), __pa(param + 5));
0082 ret = omap_smc2(idx, flag, __pa(param));
0083
0084 put_cpu();
0085
0086 return ret;
0087 }
0088
0089 void omap_smccc_smc(u32 fn, u32 arg)
0090 {
0091 struct arm_smccc_res res;
0092
0093 arm_smccc_smc(OMAP_SIP_SMC_STD_CALL_VAL(fn), arg,
0094 0, 0, 0, 0, 0, 0, &res);
0095 WARN(res.a0, "Secure function call 0x%08x failed\n", fn);
0096 }
0097
0098 void omap_smc1(u32 fn, u32 arg)
0099 {
0100
0101
0102
0103
0104 if (optee_available)
0105 omap_smccc_smc(fn, arg);
0106 else
0107 _omap_smc1(fn, arg);
0108 }
0109
0110
0111 int __init omap_secure_ram_reserve_memblock(void)
0112 {
0113 u32 size = OMAP_SECURE_RAM_STORAGE;
0114
0115 size = ALIGN(size, SECTION_SIZE);
0116 omap_secure_memblock_base = arm_memblock_steal(size, SECTION_SIZE);
0117
0118 return 0;
0119 }
0120
0121 phys_addr_t omap_secure_ram_mempool_base(void)
0122 {
0123 return omap_secure_memblock_base;
0124 }
0125
0126 #if defined(CONFIG_ARCH_OMAP3) && defined(CONFIG_PM)
0127 u32 omap3_save_secure_ram(void *addr, int size)
0128 {
0129 static u32 param[5];
0130 u32 ret;
0131
0132 if (size != OMAP3_SAVE_SECURE_RAM_SZ)
0133 return OMAP3_SAVE_SECURE_RAM_SZ;
0134
0135 param[0] = 4;
0136 param[1] = __pa(addr);
0137 param[2] = 0;
0138 param[3] = 1;
0139 param[4] = 1;
0140
0141 ret = save_secure_ram_context(__pa(param));
0142
0143 return ret;
0144 }
0145 #endif
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160 u32 rx51_secure_dispatcher(u32 idx, u32 process, u32 flag, u32 nargs,
0161 u32 arg1, u32 arg2, u32 arg3, u32 arg4)
0162 {
0163 static u32 param[5];
0164 u32 ret;
0165
0166 param[0] = nargs+1;
0167 param[1] = arg1;
0168 param[2] = arg2;
0169 param[3] = arg3;
0170 param[4] = arg4;
0171
0172
0173
0174
0175
0176 local_irq_disable();
0177 local_fiq_disable();
0178 flush_cache_all();
0179 outer_clean_range(__pa(param), __pa(param + 5));
0180 ret = omap_smc3(idx, process, flag, __pa(param));
0181 flush_cache_all();
0182 local_fiq_enable();
0183 local_irq_enable();
0184
0185 return ret;
0186 }
0187
0188
0189
0190
0191
0192
0193
0194
0195 u32 rx51_secure_update_aux_cr(u32 set_bits, u32 clear_bits)
0196 {
0197 u32 acr;
0198
0199
0200 asm volatile ("mrc p15, 0, %0, c1, c0, 1" : "=r" (acr));
0201 acr &= ~clear_bits;
0202 acr |= set_bits;
0203
0204 return rx51_secure_dispatcher(RX51_PPA_WRITE_ACR,
0205 0,
0206 FLAG_START_CRITICAL,
0207 1, acr, 0, 0, 0);
0208 }
0209
0210
0211
0212
0213 u32 rx51_secure_rng_call(u32 ptr, u32 count, u32 flag)
0214 {
0215 return rx51_secure_dispatcher(RX51_PPA_HWRNG,
0216 0,
0217 NO_FLAG,
0218 3, ptr, count, flag, 0);
0219 }
0220
0221 void __init omap_secure_init(void)
0222 {
0223 omap_optee_init_check();
0224 }
0225
0226
0227
0228
0229
0230
0231
0232
0233 static int cpu_notifier(struct notifier_block *nb, unsigned long cmd, void *v)
0234 {
0235 switch (cmd) {
0236 case CPU_CLUSTER_PM_EXIT:
0237 omap_secure_dispatcher(OMAP4_PPA_SERVICE_0,
0238 FLAG_START_CRITICAL,
0239 0, 0, 0, 0, 0);
0240 break;
0241 default:
0242 break;
0243 }
0244
0245 return NOTIFY_OK;
0246 }
0247
0248 static struct notifier_block secure_notifier_block = {
0249 .notifier_call = cpu_notifier,
0250 };
0251
0252 static int __init secure_pm_init(void)
0253 {
0254 if (omap_type() == OMAP2_DEVICE_TYPE_GP || !soc_is_omap44xx())
0255 return 0;
0256
0257 cpu_pm_register_notifier(&secure_notifier_block);
0258
0259 return 0;
0260 }
0261 omap_arch_initcall(secure_pm_init);