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_secure_reg_read(unsigned long addr_phy) 88 { 89 struct arm_smccc_res res; 90 91 res = __invoke_sip_fn_smc(SIP_ACCESS_REG, 0, addr_phy, SECURE_REG_RD); 92 return res; 93 } 94 95 int sip_smc_secure_reg_write(unsigned long addr_phy, unsigned long val) 96 { 97 struct arm_smccc_res res; 98 99 res = __invoke_sip_fn_smc(SIP_ACCESS_REG, val, addr_phy, SECURE_REG_WR); 100 return res.a0; 101 } 102 103 struct arm_smccc_res sip_smc_get_sip_version(void) 104 { 105 return __invoke_sip_fn_smc(SIP_SIP_VERSION, 0, 0, 0); 106 } 107 108 /* 109 * OP-TEE works both for kernel 3.10 and 4.4, and these two kernels have 110 * different sip implement that 3.10 uses SIP_IMPLEMENT_V1 and 4.4 uses 111 * SIP_IMPLEMENT_V2. So we should tell OP-TEE the current rockchip sip 112 * version(default SIP_IMPLEMENT_V1) before use. 113 */ 114 int sip_smc_set_sip_version(unsigned long version) 115 { 116 struct arm_smccc_res res; 117 118 res = __invoke_sip_fn_smc(SIP_SIP_VERSION, version, SECURE_REG_WR, 0); 119 if (IS_SIP_ERROR(res.a0)) { 120 printf("%s: set rockchip sip version v%ld failed\n", 121 __func__, version); 122 return res.a0; 123 } 124 125 return 0; 126 } 127