dp-arm | 1cebefd | 2016-09-19 11:21:03 +0100 | [diff] [blame] | 1 | /* |
Madhukar Pappireddy | cc30710 | 2023-09-09 23:02:34 -0500 | [diff] [blame] | 2 | * Copyright (c) 2016-2023, Arm Limited and Contributors. All rights reserved. |
dp-arm | 1cebefd | 2016-09-19 11:21:03 +0100 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
dp-arm | 1cebefd | 2016-09-19 11:21:03 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
dp-arm | 1cebefd | 2016-09-19 11:21:03 +0100 | [diff] [blame] | 7 | #include <stdint.h> |
dp-arm | 1cebefd | 2016-09-19 11:21:03 +0100 | [diff] [blame] | 8 | |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 9 | #include <common/debug.h> |
| 10 | #include <common/runtime_svc.h> |
Mikael Olsson | 7da6619 | 2021-02-12 17:30:22 +0100 | [diff] [blame] | 11 | #include <drivers/arm/ethosn.h> |
Ambroise Vincent | 9660dc1 | 2019-07-12 13:47:03 +0100 | [diff] [blame] | 12 | #include <lib/debugfs.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 13 | #include <lib/pmf/pmf.h> |
Antonio Nino Diaz | bd7b740 | 2019-01-25 14:30:04 +0000 | [diff] [blame] | 14 | #include <plat/arm/common/arm_sip_svc.h> |
| 15 | #include <plat/arm/common/plat_arm.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 16 | #include <tools_share/uuid.h> |
| 17 | |
dp-arm | 1cebefd | 2016-09-19 11:21:03 +0100 | [diff] [blame] | 18 | /* ARM SiP Service UUID */ |
Roberto Vargas | eace8f1 | 2018-04-26 13:36:53 +0100 | [diff] [blame] | 19 | DEFINE_SVC_UUID2(arm_sip_svc_uid, |
| 20 | 0x556d75e2, 0x6033, 0xb54b, 0xb5, 0x75, |
| 21 | 0x62, 0x79, 0xfd, 0x11, 0x37, 0xff); |
dp-arm | 1cebefd | 2016-09-19 11:21:03 +0100 | [diff] [blame] | 22 | |
| 23 | static int arm_sip_setup(void) |
| 24 | { |
Ambroise Vincent | 9660dc1 | 2019-07-12 13:47:03 +0100 | [diff] [blame] | 25 | if (pmf_setup() != 0) { |
dp-arm | 1cebefd | 2016-09-19 11:21:03 +0100 | [diff] [blame] | 26 | return 1; |
Ambroise Vincent | 9660dc1 | 2019-07-12 13:47:03 +0100 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | #if USE_DEBUGFS |
| 30 | |
| 31 | if (debugfs_smc_setup() != 0) { |
| 32 | return 1; |
| 33 | } |
| 34 | |
| 35 | #endif /* USE_DEBUGFS */ |
| 36 | |
Rajasekaran Kalidoss | 85999a8 | 2023-05-08 14:55:13 +0200 | [diff] [blame] | 37 | #if ETHOSN_NPU_DRIVER |
Mikael Olsson | 461bf7d | 2023-01-18 18:05:15 +0100 | [diff] [blame] | 38 | |
| 39 | if (ethosn_smc_setup() != 0) { |
| 40 | return 1; |
| 41 | } |
| 42 | |
Rajasekaran Kalidoss | 85999a8 | 2023-05-08 14:55:13 +0200 | [diff] [blame] | 43 | #endif /* ETHOSN_NPU_DRIVER */ |
Mikael Olsson | 461bf7d | 2023-01-18 18:05:15 +0100 | [diff] [blame] | 44 | |
dp-arm | 1cebefd | 2016-09-19 11:21:03 +0100 | [diff] [blame] | 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | /* |
| 49 | * This function handles ARM defined SiP Calls |
| 50 | */ |
| 51 | static uintptr_t arm_sip_handler(unsigned int smc_fid, |
| 52 | u_register_t x1, |
| 53 | u_register_t x2, |
| 54 | u_register_t x3, |
| 55 | u_register_t x4, |
| 56 | void *cookie, |
| 57 | void *handle, |
| 58 | u_register_t flags) |
| 59 | { |
Jeenu Viswambharan | bc1a929 | 2017-02-16 14:55:15 +0000 | [diff] [blame] | 60 | int call_count = 0; |
| 61 | |
Mikael Olsson | 7da6619 | 2021-02-12 17:30:22 +0100 | [diff] [blame] | 62 | #if ENABLE_PMF |
| 63 | |
dp-arm | 1cebefd | 2016-09-19 11:21:03 +0100 | [diff] [blame] | 64 | /* |
| 65 | * Dispatch PMF calls to PMF SMC handler and return its return |
| 66 | * value |
| 67 | */ |
| 68 | if (is_pmf_fid(smc_fid)) { |
| 69 | return pmf_smc_handler(smc_fid, x1, x2, x3, x4, cookie, |
| 70 | handle, flags); |
| 71 | } |
| 72 | |
Mikael Olsson | 7da6619 | 2021-02-12 17:30:22 +0100 | [diff] [blame] | 73 | #endif /* ENABLE_PMF */ |
| 74 | |
Ambroise Vincent | 9660dc1 | 2019-07-12 13:47:03 +0100 | [diff] [blame] | 75 | #if USE_DEBUGFS |
| 76 | |
| 77 | if (is_debugfs_fid(smc_fid)) { |
| 78 | return debugfs_smc_handler(smc_fid, x1, x2, x3, x4, cookie, |
| 79 | handle, flags); |
| 80 | } |
| 81 | |
| 82 | #endif /* USE_DEBUGFS */ |
| 83 | |
Rajasekaran Kalidoss | 85999a8 | 2023-05-08 14:55:13 +0200 | [diff] [blame] | 84 | #if ETHOSN_NPU_DRIVER |
Mikael Olsson | 7da6619 | 2021-02-12 17:30:22 +0100 | [diff] [blame] | 85 | |
| 86 | if (is_ethosn_fid(smc_fid)) { |
| 87 | return ethosn_smc_handler(smc_fid, x1, x2, x3, x4, cookie, |
| 88 | handle, flags); |
| 89 | } |
| 90 | |
Rajasekaran Kalidoss | 85999a8 | 2023-05-08 14:55:13 +0200 | [diff] [blame] | 91 | #endif /* ETHOSN_NPU_DRIVER */ |
Mikael Olsson | 7da6619 | 2021-02-12 17:30:22 +0100 | [diff] [blame] | 92 | |
dp-arm | 1cebefd | 2016-09-19 11:21:03 +0100 | [diff] [blame] | 93 | switch (smc_fid) { |
Jeenu Viswambharan | bc1a929 | 2017-02-16 14:55:15 +0000 | [diff] [blame] | 94 | case ARM_SIP_SVC_EXE_STATE_SWITCH: { |
Bence Szépkúti | 16362c6 | 2019-10-24 15:53:23 +0200 | [diff] [blame] | 95 | /* Execution state can be switched only if EL3 is AArch64 */ |
| 96 | #ifdef __aarch64__ |
Jeenu Viswambharan | bc1a929 | 2017-02-16 14:55:15 +0000 | [diff] [blame] | 97 | /* Allow calls from non-secure only */ |
| 98 | if (!is_caller_non_secure(flags)) |
| 99 | SMC_RET1(handle, STATE_SW_E_DENIED); |
| 100 | |
dp-arm | 1cebefd | 2016-09-19 11:21:03 +0100 | [diff] [blame] | 101 | /* |
Jeenu Viswambharan | bc1a929 | 2017-02-16 14:55:15 +0000 | [diff] [blame] | 102 | * Pointers used in execution state switch are all 32 bits wide |
dp-arm | 1cebefd | 2016-09-19 11:21:03 +0100 | [diff] [blame] | 103 | */ |
Jeenu Viswambharan | 210f0a8 | 2018-08-02 10:14:12 +0100 | [diff] [blame] | 104 | return (uintptr_t) arm_execution_state_switch(smc_fid, |
| 105 | (uint32_t) x1, (uint32_t) x2, (uint32_t) x3, |
| 106 | (uint32_t) x4, handle); |
Bence Szépkúti | 16362c6 | 2019-10-24 15:53:23 +0200 | [diff] [blame] | 107 | #else |
| 108 | /* State switch denied */ |
| 109 | SMC_RET1(handle, STATE_SW_E_DENIED); |
| 110 | #endif /* __aarch64__ */ |
Jeenu Viswambharan | bc1a929 | 2017-02-16 14:55:15 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | case ARM_SIP_SVC_CALL_COUNT: |
| 114 | /* PMF calls */ |
| 115 | call_count += PMF_NUM_SMC_CALLS; |
| 116 | |
Rajasekaran Kalidoss | 85999a8 | 2023-05-08 14:55:13 +0200 | [diff] [blame] | 117 | #if ETHOSN_NPU_DRIVER |
Mikael Olsson | 7da6619 | 2021-02-12 17:30:22 +0100 | [diff] [blame] | 118 | /* ETHOSN calls */ |
| 119 | call_count += ETHOSN_NUM_SMC_CALLS; |
Rajasekaran Kalidoss | 85999a8 | 2023-05-08 14:55:13 +0200 | [diff] [blame] | 120 | #endif /* ETHOSN_NPU_DRIVER */ |
Mikael Olsson | 7da6619 | 2021-02-12 17:30:22 +0100 | [diff] [blame] | 121 | |
Jeenu Viswambharan | bc1a929 | 2017-02-16 14:55:15 +0000 | [diff] [blame] | 122 | /* State switch call */ |
| 123 | call_count += 1; |
| 124 | |
| 125 | SMC_RET1(handle, call_count); |
dp-arm | 1cebefd | 2016-09-19 11:21:03 +0100 | [diff] [blame] | 126 | |
| 127 | case ARM_SIP_SVC_UID: |
| 128 | /* Return UID to the caller */ |
| 129 | SMC_UUID_RET(handle, arm_sip_svc_uid); |
| 130 | |
| 131 | case ARM_SIP_SVC_VERSION: |
| 132 | /* Return the version of current implementation */ |
| 133 | SMC_RET2(handle, ARM_SIP_SVC_VERSION_MAJOR, ARM_SIP_SVC_VERSION_MINOR); |
| 134 | |
| 135 | default: |
Madhukar Pappireddy | cc30710 | 2023-09-09 23:02:34 -0500 | [diff] [blame] | 136 | break; |
dp-arm | 1cebefd | 2016-09-19 11:21:03 +0100 | [diff] [blame] | 137 | } |
| 138 | |
Madhukar Pappireddy | cc30710 | 2023-09-09 23:02:34 -0500 | [diff] [blame] | 139 | /* |
| 140 | * Fall back to allow Arm platform specific handler. |
| 141 | * TODO: Refactor needed to move out generic handlers from this file and |
| 142 | * only keep Arm Platform specific handlers here. |
| 143 | */ |
| 144 | return plat_arm_sip_handler(smc_fid, x1, x2, x3, x4, |
| 145 | cookie, handle, flags); |
dp-arm | 1cebefd | 2016-09-19 11:21:03 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | |
| 149 | /* Define a runtime service descriptor for fast SMC calls */ |
| 150 | DECLARE_RT_SVC( |
| 151 | arm_sip_svc, |
| 152 | OEN_SIP_START, |
| 153 | OEN_SIP_END, |
| 154 | SMC_TYPE_FAST, |
| 155 | arm_sip_setup, |
| 156 | arm_sip_handler |
| 157 | ); |