Tejas Patel | 354fe57 | 2018-12-14 00:55:37 -0800 | [diff] [blame] | 1 | /* |
Tejas Patel | 5b433b6 | 2020-01-29 22:09:55 -0800 | [diff] [blame] | 2 | * Copyright (c) 2019-2020, Xilinx, Inc. All rights reserved. |
Tejas Patel | 354fe57 | 2018-12-14 00:55:37 -0800 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * Top-level SMC handler for Versal power management calls and |
| 9 | * IPI setup functions for communication with PMC. |
| 10 | */ |
| 11 | |
| 12 | #include <errno.h> |
| 13 | #include <plat_private.h> |
Tejas Patel | 59c608a | 2019-01-09 04:10:29 -0800 | [diff] [blame] | 14 | #include <stdbool.h> |
| 15 | #include <common/runtime_svc.h> |
| 16 | #include "pm_api_sys.h" |
Tejas Patel | 354fe57 | 2018-12-14 00:55:37 -0800 | [diff] [blame] | 17 | #include "pm_client.h" |
| 18 | #include "pm_ipi.h" |
| 19 | |
Tejas Patel | 59c608a | 2019-01-09 04:10:29 -0800 | [diff] [blame] | 20 | /* pm_up = true - UP, pm_up = false - DOWN */ |
| 21 | static bool pm_up; |
| 22 | |
Tejas Patel | 354fe57 | 2018-12-14 00:55:37 -0800 | [diff] [blame] | 23 | /** |
| 24 | * pm_setup() - PM service setup |
| 25 | * |
| 26 | * @return On success, the initialization function must return 0. |
| 27 | * Any other return value will cause the framework to ignore |
| 28 | * the service |
| 29 | * |
| 30 | * Initialization functions for Versal power management for |
| 31 | * communicaton with PMC. |
| 32 | * |
| 33 | * Called from sip_svc_setup initialization function with the |
| 34 | * rt_svc_init signature. |
| 35 | */ |
| 36 | int pm_setup(void) |
| 37 | { |
| 38 | int status, ret = 0; |
| 39 | |
| 40 | status = pm_ipi_init(primary_proc); |
| 41 | |
| 42 | if (status < 0) { |
| 43 | INFO("BL31: PM Service Init Failed, Error Code %d!\n", status); |
| 44 | ret = status; |
Tejas Patel | 59c608a | 2019-01-09 04:10:29 -0800 | [diff] [blame] | 45 | } else { |
| 46 | pm_up = true; |
Tejas Patel | 354fe57 | 2018-12-14 00:55:37 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | return ret; |
| 50 | } |
Tejas Patel | 59c608a | 2019-01-09 04:10:29 -0800 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * pm_smc_handler() - SMC handler for PM-API calls coming from EL1/EL2. |
| 54 | * @smc_fid - Function Identifier |
| 55 | * @x1 - x4 - Arguments |
| 56 | * @cookie - Unused |
| 57 | * @handler - Pointer to caller's context structure |
| 58 | * |
| 59 | * @return - Unused |
| 60 | * |
| 61 | * Determines that smc_fid is valid and supported PM SMC Function ID from the |
| 62 | * list of pm_api_ids, otherwise completes the request with |
| 63 | * the unknown SMC Function ID |
| 64 | * |
| 65 | * The SMC calls for PM service are forwarded from SIP Service SMC handler |
| 66 | * function with rt_svc_handle signature |
| 67 | */ |
| 68 | uint64_t pm_smc_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2, uint64_t x3, |
| 69 | uint64_t x4, void *cookie, void *handle, uint64_t flags) |
| 70 | { |
| 71 | enum pm_ret_status ret; |
| 72 | |
| 73 | uint32_t pm_arg[4]; |
| 74 | |
| 75 | /* Handle case where PM wasn't initialized properly */ |
| 76 | if (!pm_up) |
| 77 | SMC_RET1(handle, SMC_UNK); |
| 78 | |
| 79 | pm_arg[0] = (uint32_t)x1; |
| 80 | pm_arg[1] = (uint32_t)(x1 >> 32); |
| 81 | pm_arg[2] = (uint32_t)x2; |
| 82 | pm_arg[3] = (uint32_t)(x2 >> 32); |
| 83 | |
| 84 | switch (smc_fid & FUNCID_NUM_MASK) { |
| 85 | /* PM API Functions */ |
| 86 | case PM_SELF_SUSPEND: |
| 87 | ret = pm_self_suspend(pm_arg[0], pm_arg[1], pm_arg[2], |
| 88 | pm_arg[3]); |
| 89 | SMC_RET1(handle, (uint64_t)ret); |
| 90 | |
Tejas Patel | 6b28225 | 2019-01-10 03:03:47 -0800 | [diff] [blame] | 91 | case PM_FORCE_POWERDOWN: |
| 92 | ret = pm_force_powerdown(pm_arg[0], pm_arg[1]); |
| 93 | SMC_RET1(handle, (uint64_t)ret); |
| 94 | |
Tejas Patel | 59c608a | 2019-01-09 04:10:29 -0800 | [diff] [blame] | 95 | case PM_REQ_SUSPEND: |
| 96 | ret = pm_req_suspend(pm_arg[0], pm_arg[1], pm_arg[2], |
| 97 | pm_arg[3]); |
| 98 | SMC_RET1(handle, (uint64_t)ret); |
| 99 | |
| 100 | case PM_ABORT_SUSPEND: |
| 101 | ret = pm_abort_suspend(pm_arg[0]); |
| 102 | SMC_RET1(handle, (uint64_t)ret); |
| 103 | |
Tejas Patel | 6b28225 | 2019-01-10 03:03:47 -0800 | [diff] [blame] | 104 | case PM_SYSTEM_SHUTDOWN: |
| 105 | ret = pm_system_shutdown(pm_arg[0], pm_arg[1]); |
| 106 | SMC_RET1(handle, (uint64_t)ret); |
| 107 | |
Tejas Patel | 49cd871 | 2019-01-23 14:18:51 +0530 | [diff] [blame] | 108 | case PM_REQ_WAKEUP: |
| 109 | ret = pm_req_wakeup(pm_arg[0], pm_arg[1], pm_arg[2], pm_arg[3]); |
| 110 | SMC_RET1(handle, (uint64_t)ret); |
| 111 | |
Tejas Patel | db81205 | 2019-01-23 14:18:53 +0530 | [diff] [blame] | 112 | case PM_SET_WAKEUP_SOURCE: |
| 113 | ret = pm_set_wakeup_source(pm_arg[0], pm_arg[1], pm_arg[2]); |
| 114 | SMC_RET1(handle, (uint64_t)ret); |
| 115 | |
Tejas Patel | 59c608a | 2019-01-09 04:10:29 -0800 | [diff] [blame] | 116 | case PM_REQUEST_DEVICE: |
| 117 | ret = pm_request_device(pm_arg[0], pm_arg[1], pm_arg[2], |
| 118 | pm_arg[3]); |
| 119 | SMC_RET1(handle, (uint64_t)ret); |
| 120 | |
| 121 | case PM_RELEASE_DEVICE: |
| 122 | ret = pm_release_device(pm_arg[0]); |
| 123 | SMC_RET1(handle, (uint64_t)ret); |
| 124 | |
| 125 | case PM_SET_REQUIREMENT: |
| 126 | ret = pm_set_requirement(pm_arg[0], pm_arg[1], pm_arg[2], |
| 127 | pm_arg[3]); |
| 128 | SMC_RET1(handle, (uint64_t)ret); |
| 129 | |
| 130 | case PM_GET_API_VERSION: |
| 131 | { |
| 132 | uint32_t api_version; |
| 133 | |
| 134 | ret = pm_get_api_version(&api_version); |
| 135 | SMC_RET1(handle, (uint64_t)PM_RET_SUCCESS | |
| 136 | ((uint64_t)api_version << 32)); |
| 137 | } |
| 138 | |
| 139 | case PM_GET_DEVICE_STATUS: |
| 140 | { |
| 141 | uint32_t buff[3]; |
| 142 | |
| 143 | ret = pm_get_device_status(pm_arg[0], buff); |
| 144 | SMC_RET2(handle, (uint64_t)ret | ((uint64_t)buff[0] << 32), |
| 145 | (uint64_t)buff[1] | ((uint64_t)buff[2] << 32)); |
| 146 | } |
| 147 | |
| 148 | case PM_RESET_ASSERT: |
| 149 | ret = pm_reset_assert(pm_arg[0], pm_arg[1]); |
| 150 | SMC_RET1(handle, (uint64_t)ret); |
| 151 | |
| 152 | case PM_RESET_GET_STATUS: |
| 153 | { |
| 154 | uint32_t reset_status; |
| 155 | |
| 156 | ret = pm_reset_get_status(pm_arg[0], &reset_status); |
| 157 | SMC_RET1(handle, (uint64_t)ret | |
| 158 | ((uint64_t)reset_status << 32)); |
| 159 | } |
| 160 | |
Tejas Patel | 0266202 | 2019-01-21 17:56:49 +0530 | [diff] [blame] | 161 | case PM_INIT_FINALIZE: |
| 162 | SMC_RET1(handle, (uint64_t)PM_RET_SUCCESS); |
| 163 | |
Rajan Vaja | 649e924 | 2019-03-04 11:09:40 +0530 | [diff] [blame] | 164 | case PM_GET_CALLBACK_DATA: |
| 165 | { |
| 166 | uint32_t result[4] = {0}; |
| 167 | |
Tejas Patel | 5b433b6 | 2020-01-29 22:09:55 -0800 | [diff] [blame] | 168 | pm_get_callbackdata(result, ARRAY_SIZE(result)); |
Rajan Vaja | 649e924 | 2019-03-04 11:09:40 +0530 | [diff] [blame] | 169 | SMC_RET2(handle, |
| 170 | (uint64_t)result[0] | ((uint64_t)result[1] << 32), |
| 171 | (uint64_t)result[2] | ((uint64_t)result[3] << 32)); |
| 172 | } |
| 173 | |
Tejas Patel | 59c608a | 2019-01-09 04:10:29 -0800 | [diff] [blame] | 174 | case PM_PINCTRL_REQUEST: |
| 175 | ret = pm_pinctrl_request(pm_arg[0]); |
| 176 | SMC_RET1(handle, (uint64_t)ret); |
| 177 | |
| 178 | case PM_PINCTRL_RELEASE: |
| 179 | ret = pm_pinctrl_release(pm_arg[0]); |
| 180 | SMC_RET1(handle, (uint64_t)ret); |
| 181 | |
| 182 | case PM_PINCTRL_GET_FUNCTION: |
| 183 | { |
| 184 | uint32_t value = 0; |
| 185 | |
| 186 | ret = pm_pinctrl_get_function(pm_arg[0], &value); |
| 187 | SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32); |
| 188 | } |
| 189 | |
| 190 | case PM_PINCTRL_SET_FUNCTION: |
| 191 | ret = pm_pinctrl_set_function(pm_arg[0], pm_arg[1]); |
| 192 | SMC_RET1(handle, (uint64_t)ret); |
| 193 | |
| 194 | case PM_PINCTRL_CONFIG_PARAM_GET: |
| 195 | { |
| 196 | uint32_t value; |
| 197 | |
| 198 | ret = pm_pinctrl_get_pin_param(pm_arg[0], pm_arg[1], &value); |
| 199 | SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32); |
| 200 | } |
| 201 | |
| 202 | case PM_PINCTRL_CONFIG_PARAM_SET: |
| 203 | ret = pm_pinctrl_set_pin_param(pm_arg[0], pm_arg[1], pm_arg[2]); |
| 204 | SMC_RET1(handle, (uint64_t)ret); |
| 205 | |
Tejas Patel | 9141f44 | 2019-01-10 03:03:48 -0800 | [diff] [blame] | 206 | case PM_IOCTL: |
| 207 | { |
| 208 | uint32_t value; |
| 209 | |
| 210 | ret = pm_api_ioctl(pm_arg[0], pm_arg[1], pm_arg[2], |
| 211 | pm_arg[3], &value); |
| 212 | SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32); |
| 213 | } |
| 214 | |
Tejas Patel | a3e34ad | 2019-02-01 17:25:19 +0530 | [diff] [blame] | 215 | case PM_QUERY_DATA: |
| 216 | { |
Rajan Vaja | 030620d | 2020-11-23 04:13:54 -0800 | [diff] [blame^] | 217 | uint32_t data[8] = { 0 }; |
Tejas Patel | a3e34ad | 2019-02-01 17:25:19 +0530 | [diff] [blame] | 218 | |
| 219 | ret = pm_query_data(pm_arg[0], pm_arg[1], pm_arg[2], |
Rajan Vaja | 030620d | 2020-11-23 04:13:54 -0800 | [diff] [blame^] | 220 | pm_arg[3], data); |
| 221 | |
Tejas Patel | a3e34ad | 2019-02-01 17:25:19 +0530 | [diff] [blame] | 222 | SMC_RET2(handle, (uint64_t)ret | ((uint64_t)data[0] << 32), |
Rajan Vaja | 030620d | 2020-11-23 04:13:54 -0800 | [diff] [blame^] | 223 | (uint64_t)data[1] | ((uint64_t)data[2] << 32)); |
Tejas Patel | a3e34ad | 2019-02-01 17:25:19 +0530 | [diff] [blame] | 224 | |
Rajan Vaja | 030620d | 2020-11-23 04:13:54 -0800 | [diff] [blame^] | 225 | } |
Tejas Patel | 59c608a | 2019-01-09 04:10:29 -0800 | [diff] [blame] | 226 | case PM_CLOCK_ENABLE: |
| 227 | ret = pm_clock_enable(pm_arg[0]); |
| 228 | SMC_RET1(handle, (uint64_t)ret); |
| 229 | |
| 230 | case PM_CLOCK_DISABLE: |
| 231 | ret = pm_clock_disable(pm_arg[0]); |
| 232 | SMC_RET1(handle, (uint64_t)ret); |
| 233 | |
| 234 | case PM_CLOCK_GETSTATE: |
| 235 | { |
| 236 | uint32_t value; |
| 237 | |
| 238 | ret = pm_clock_get_state(pm_arg[0], &value); |
| 239 | SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32); |
| 240 | } |
| 241 | |
| 242 | case PM_CLOCK_SETDIVIDER: |
| 243 | ret = pm_clock_set_divider(pm_arg[0], pm_arg[1]); |
| 244 | SMC_RET1(handle, (uint64_t)ret); |
| 245 | |
| 246 | case PM_CLOCK_GETDIVIDER: |
| 247 | { |
| 248 | uint32_t value; |
| 249 | |
| 250 | ret = pm_clock_get_divider(pm_arg[0], &value); |
| 251 | SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32); |
| 252 | } |
| 253 | |
| 254 | case PM_CLOCK_SETPARENT: |
| 255 | ret = pm_clock_set_parent(pm_arg[0], pm_arg[1]); |
| 256 | SMC_RET1(handle, (uint64_t)ret); |
| 257 | |
| 258 | case PM_CLOCK_GETPARENT: |
| 259 | { |
| 260 | uint32_t value; |
| 261 | |
| 262 | ret = pm_clock_get_parent(pm_arg[0], &value); |
| 263 | SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32); |
| 264 | } |
| 265 | |
| 266 | case PM_PLL_SET_PARAMETER: |
| 267 | ret = pm_pll_set_param(pm_arg[0], pm_arg[1], pm_arg[2]); |
| 268 | SMC_RET1(handle, (uint64_t)ret); |
| 269 | |
| 270 | case PM_PLL_GET_PARAMETER: |
| 271 | { |
| 272 | uint32_t value; |
| 273 | |
| 274 | ret = pm_pll_get_param(pm_arg[0], pm_arg[1], &value); |
| 275 | SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value << 32)); |
| 276 | } |
| 277 | |
| 278 | case PM_PLL_SET_MODE: |
| 279 | ret = pm_pll_set_mode(pm_arg[0], pm_arg[1]); |
| 280 | SMC_RET1(handle, (uint64_t)ret); |
| 281 | |
| 282 | case PM_PLL_GET_MODE: |
| 283 | { |
| 284 | uint32_t mode; |
| 285 | |
| 286 | ret = pm_pll_get_mode(pm_arg[0], &mode); |
| 287 | SMC_RET1(handle, (uint64_t)ret | ((uint64_t)mode << 32)); |
| 288 | } |
| 289 | |
Tejas Patel | 044001d | 2019-01-21 17:56:48 +0530 | [diff] [blame] | 290 | case PM_GET_TRUSTZONE_VERSION: |
| 291 | SMC_RET1(handle, (uint64_t)PM_RET_SUCCESS | |
| 292 | ((uint64_t)VERSAL_TZ_VERSION << 32)); |
| 293 | |
Ravi Patel | bd4aa5a | 2019-08-12 03:17:54 -0700 | [diff] [blame] | 294 | case PM_GET_CHIPID: |
| 295 | { |
| 296 | uint32_t result[2]; |
| 297 | |
| 298 | ret = pm_get_chipid(result); |
| 299 | SMC_RET2(handle, (uint64_t)ret | ((uint64_t)result[0] << 32), |
| 300 | result[1]); |
| 301 | } |
| 302 | |
Ravi Patel | 22b0b49 | 2019-03-06 12:34:46 +0530 | [diff] [blame] | 303 | case PM_FEATURE_CHECK: |
| 304 | { |
| 305 | uint32_t version; |
| 306 | |
| 307 | ret = pm_feature_check(pm_arg[0], &version); |
| 308 | SMC_RET1(handle, (uint64_t)ret | ((uint64_t)version << 32)); |
| 309 | } |
| 310 | |
Jolly Shah | ed05a71 | 2019-03-22 05:33:39 +0530 | [diff] [blame] | 311 | case PM_LOAD_PDI: |
| 312 | { |
| 313 | ret = pm_load_pdi(pm_arg[0], pm_arg[1], pm_arg[2]); |
| 314 | SMC_RET1(handle, (uint64_t)ret); |
| 315 | } |
| 316 | |
Saeed Nowshadi | 2294b42 | 2019-06-03 10:22:35 -0700 | [diff] [blame] | 317 | case PM_GET_OP_CHARACTERISTIC: |
| 318 | { |
| 319 | uint32_t result; |
| 320 | |
| 321 | ret = pm_get_op_characteristic(pm_arg[0], pm_arg[1], &result); |
| 322 | SMC_RET1(handle, (uint64_t)ret | ((uint64_t)result << 32)); |
| 323 | } |
| 324 | |
Tejas Patel | 59c608a | 2019-01-09 04:10:29 -0800 | [diff] [blame] | 325 | default: |
| 326 | WARN("Unimplemented PM Service Call: 0x%x\n", smc_fid); |
| 327 | SMC_RET1(handle, SMC_UNK); |
| 328 | } |
| 329 | } |