1 /* 2 * (C) Copyright 2017 Rockchip Electronics Co., Ltd 3 * 4 * SPDX-License-Identifier: GPL-2.0+ 5 */ 6 7 #include <asm/arch/rockchip_smccc.h> 8 #include <asm/io.h> 9 #include <asm/psci.h> 10 #include <asm/suspend.h> 11 #include <linux/arm-smccc.h> 12 #include <linux/io.h> 13 14 #ifdef CONFIG_ARM64 15 #define ARM_PSCI_1_0_SYSTEM_SUSPEND ARM_PSCI_1_0_FN64_SYSTEM_SUSPEND 16 #define ARM_PSCI_0_2_CPU_ON ARM_PSCI_0_2_FN64_CPU_ON 17 #else 18 #define ARM_PSCI_1_0_SYSTEM_SUSPEND ARM_PSCI_1_0_FN_SYSTEM_SUSPEND 19 #define ARM_PSCI_0_2_CPU_ON ARM_PSCI_0_2_FN_CPU_ON 20 #endif 21 22 #define SIZE_PAGE(n) ((n) << 12) 23 24 static struct arm_smccc_res __invoke_sip_fn_smc(unsigned long function_id, 25 unsigned long arg0, 26 unsigned long arg1, 27 unsigned long arg2) 28 { 29 struct arm_smccc_res res; 30 31 arm_smccc_smc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res); 32 return res; 33 } 34 35 int psci_cpu_on(unsigned long cpuid, unsigned long entry_point) 36 { 37 struct arm_smccc_res res; 38 39 res = __invoke_sip_fn_smc(ARM_PSCI_0_2_CPU_ON, cpuid, entry_point, 0); 40 41 return res.a0; 42 } 43 44 int psci_system_suspend(unsigned long unused) 45 { 46 struct arm_smccc_res res; 47 48 res = __invoke_sip_fn_smc(ARM_PSCI_1_0_SYSTEM_SUSPEND, 49 virt_to_phys(cpu_resume), 0, 0); 50 return res.a0; 51 } 52 53 int sip_smc_set_suspend_mode(unsigned long ctrl, 54 unsigned long config1, 55 unsigned long config2) 56 { 57 struct arm_smccc_res res; 58 59 res = __invoke_sip_fn_smc(SIP_SUSPEND_MODE, ctrl, config1, config2); 60 return res.a0; 61 } 62 63 struct arm_smccc_res sip_smc_dram(unsigned long arg0, 64 unsigned long arg1, 65 unsigned long arg2) 66 { 67 return __invoke_sip_fn_smc(SIP_DRAM_CONFIG, arg0, arg1, arg2); 68 } 69 70 struct arm_smccc_res sip_smc_request_share_mem(unsigned long page_num, 71 share_page_type_t page_type) 72 { 73 struct arm_smccc_res res; 74 unsigned long share_mem_phy; 75 76 res = __invoke_sip_fn_smc(SIP_SHARE_MEM, page_num, page_type, 0); 77 if (IS_SIP_ERROR(res.a0)) 78 goto error; 79 80 share_mem_phy = res.a1; 81 res.a1 = (unsigned long)ioremap(share_mem_phy, SIZE_PAGE(page_num)); 82 83 error: 84 return res; 85 } 86 87 struct arm_smccc_res sip_smc_get_sip_version(void) 88 { 89 return __invoke_sip_fn_smc(SIP_SIP_VERSION, 0, 0, 0); 90 } 91 92 /* 93 * OP-TEE works both for kernel 3.10 and 4.4, and these two kernels have 94 * different sip implement that 3.10 uses SIP_IMPLEMENT_V1 and 4.4 uses 95 * SIP_IMPLEMENT_V2. So we should tell OP-TEE the current rockchip sip 96 * version(default SIP_IMPLEMENT_V1) before use. 97 */ 98 int sip_smc_set_sip_version(unsigned long version) 99 { 100 struct arm_smccc_res res; 101 102 res = __invoke_sip_fn_smc(SIP_SIP_VERSION, version, SECURE_REG_WR, 0); 103 if (IS_SIP_ERROR(res.a0)) { 104 printf("%s: set rockchip sip version v%ld failed\n", 105 __func__, version); 106 return res.a0; 107 } 108 109 return 0; 110 } 111