Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 1 | /* |
Rajan Vaja | 8368761 | 2018-01-17 02:39:20 -0800 | [diff] [blame] | 2 | * Copyright (c) 2013-2018, ARM Limited and Contributors. All rights reserved. |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * Top-level SMC handler for ZynqMP power management calls and |
| 9 | * IPI setup functions for communication with PMU. |
| 10 | */ |
| 11 | |
| 12 | #include <errno.h> |
| 13 | #include <gic_common.h> |
| 14 | #include <runtime_svc.h> |
| 15 | #include <string.h> |
Isla Mitchell | e363146 | 2017-07-14 10:46:32 +0100 | [diff] [blame] | 16 | #include "../zynqmp_private.h" |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 17 | #include "pm_api_sys.h" |
| 18 | #include "pm_client.h" |
| 19 | #include "pm_ipi.h" |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 20 | |
Soren Brinkmann | 84f0af4 | 2016-09-30 14:24:25 -0700 | [diff] [blame] | 21 | #define PM_GET_CALLBACK_DATA 0xa01 |
Rajan Vaja | c7ee23d | 2018-02-14 23:10:54 -0800 | [diff] [blame] | 22 | #define PM_GET_TRUSTZONE_VERSION 0xa03 |
Soren Brinkmann | 84f0af4 | 2016-09-30 14:24:25 -0700 | [diff] [blame] | 23 | |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 24 | /* 0 - UP, !0 - DOWN */ |
| 25 | static int32_t pm_down = !0; |
| 26 | |
| 27 | /** |
| 28 | * pm_context - Structure which contains data for power management |
| 29 | * @api_version version of PM API, must match with one on PMU side |
| 30 | * @payload payload array used to store received |
| 31 | * data from ipi buffer registers |
| 32 | */ |
| 33 | static struct { |
| 34 | uint32_t api_version; |
| 35 | uint32_t payload[PAYLOAD_ARG_CNT]; |
| 36 | } pm_ctx; |
| 37 | |
| 38 | /** |
| 39 | * pm_setup() - PM service setup |
| 40 | * |
| 41 | * @return On success, the initialization function must return 0. |
| 42 | * Any other return value will cause the framework to ignore |
| 43 | * the service |
| 44 | * |
| 45 | * Initialization functions for ZynqMP power management for |
| 46 | * communicaton with PMU. |
| 47 | * |
| 48 | * Called from sip_svc_setup initialization function with the |
| 49 | * rt_svc_init signature. |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 50 | */ |
| 51 | int pm_setup(void) |
| 52 | { |
Wendy Liang | 328105c | 2017-10-03 23:21:11 -0700 | [diff] [blame] | 53 | int status, ret; |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 54 | |
| 55 | if (!zynqmp_is_pmu_up()) |
| 56 | return -ENODEV; |
| 57 | |
Wendy Liang | 328105c | 2017-10-03 23:21:11 -0700 | [diff] [blame] | 58 | status = pm_ipi_init(primary_proc); |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 59 | |
Wendy Liang | 328105c | 2017-10-03 23:21:11 -0700 | [diff] [blame] | 60 | if (status >= 0) { |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 61 | INFO("BL31: PM Service Init Complete: API v%d.%d\n", |
| 62 | PM_VERSION_MAJOR, PM_VERSION_MINOR); |
Wendy Liang | 328105c | 2017-10-03 23:21:11 -0700 | [diff] [blame] | 63 | ret = 0; |
| 64 | } else { |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 65 | INFO("BL31: PM Service Init Failed, Error Code %d!\n", status); |
Wendy Liang | 328105c | 2017-10-03 23:21:11 -0700 | [diff] [blame] | 66 | ret = status; |
| 67 | } |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 68 | |
| 69 | pm_down = status; |
| 70 | |
Wendy Liang | 328105c | 2017-10-03 23:21:11 -0700 | [diff] [blame] | 71 | return ret; |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** |
| 75 | * pm_smc_handler() - SMC handler for PM-API calls coming from EL1/EL2. |
| 76 | * @smc_fid - Function Identifier |
| 77 | * @x1 - x4 - Arguments |
| 78 | * @cookie - Unused |
| 79 | * @handler - Pointer to caller's context structure |
| 80 | * |
| 81 | * @return - Unused |
| 82 | * |
| 83 | * Determines that smc_fid is valid and supported PM SMC Function ID from the |
| 84 | * list of pm_api_ids, otherwise completes the request with |
| 85 | * the unknown SMC Function ID |
| 86 | * |
| 87 | * The SMC calls for PM service are forwarded from SIP Service SMC handler |
| 88 | * function with rt_svc_handle signature |
| 89 | */ |
| 90 | uint64_t pm_smc_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2, uint64_t x3, |
| 91 | uint64_t x4, void *cookie, void *handle, uint64_t flags) |
| 92 | { |
| 93 | enum pm_ret_status ret; |
| 94 | |
| 95 | uint32_t pm_arg[4]; |
| 96 | |
| 97 | /* Handle case where PM wasn't initialized properly */ |
| 98 | if (pm_down) |
| 99 | SMC_RET1(handle, SMC_UNK); |
| 100 | |
| 101 | pm_arg[0] = (uint32_t)x1; |
| 102 | pm_arg[1] = (uint32_t)(x1 >> 32); |
| 103 | pm_arg[2] = (uint32_t)x2; |
| 104 | pm_arg[3] = (uint32_t)(x2 >> 32); |
| 105 | |
| 106 | switch (smc_fid & FUNCID_NUM_MASK) { |
| 107 | /* PM API Functions */ |
| 108 | case PM_SELF_SUSPEND: |
| 109 | ret = pm_self_suspend(pm_arg[0], pm_arg[1], pm_arg[2], |
| 110 | pm_arg[3]); |
| 111 | SMC_RET1(handle, (uint64_t)ret); |
| 112 | |
| 113 | case PM_REQ_SUSPEND: |
| 114 | ret = pm_req_suspend(pm_arg[0], pm_arg[1], pm_arg[2], |
| 115 | pm_arg[3]); |
| 116 | SMC_RET1(handle, (uint64_t)ret); |
| 117 | |
| 118 | case PM_REQ_WAKEUP: |
| 119 | ret = pm_req_wakeup(pm_arg[0], pm_arg[1], pm_arg[2], |
| 120 | pm_arg[3]); |
| 121 | SMC_RET1(handle, (uint64_t)ret); |
| 122 | |
| 123 | case PM_FORCE_POWERDOWN: |
| 124 | ret = pm_force_powerdown(pm_arg[0], pm_arg[1]); |
| 125 | SMC_RET1(handle, (uint64_t)ret); |
| 126 | |
| 127 | case PM_ABORT_SUSPEND: |
| 128 | ret = pm_abort_suspend(pm_arg[0]); |
| 129 | SMC_RET1(handle, (uint64_t)ret); |
| 130 | |
| 131 | case PM_SET_WAKEUP_SOURCE: |
| 132 | ret = pm_set_wakeup_source(pm_arg[0], pm_arg[1], pm_arg[2]); |
| 133 | SMC_RET1(handle, (uint64_t)ret); |
| 134 | |
| 135 | case PM_SYSTEM_SHUTDOWN: |
Soren Brinkmann | 58fbb9b | 2016-09-02 09:50:54 -0700 | [diff] [blame] | 136 | ret = pm_system_shutdown(pm_arg[0], pm_arg[1]); |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 137 | SMC_RET1(handle, (uint64_t)ret); |
| 138 | |
| 139 | case PM_REQ_NODE: |
| 140 | ret = pm_req_node(pm_arg[0], pm_arg[1], pm_arg[2], pm_arg[3]); |
| 141 | SMC_RET1(handle, (uint64_t)ret); |
| 142 | |
| 143 | case PM_RELEASE_NODE: |
| 144 | ret = pm_release_node(pm_arg[0]); |
| 145 | SMC_RET1(handle, (uint64_t)ret); |
| 146 | |
| 147 | case PM_SET_REQUIREMENT: |
| 148 | ret = pm_set_requirement(pm_arg[0], pm_arg[1], pm_arg[2], |
| 149 | pm_arg[3]); |
| 150 | SMC_RET1(handle, (uint64_t)ret); |
| 151 | |
| 152 | case PM_SET_MAX_LATENCY: |
| 153 | ret = pm_set_max_latency(pm_arg[0], pm_arg[1]); |
| 154 | SMC_RET1(handle, (uint64_t)ret); |
| 155 | |
| 156 | case PM_GET_API_VERSION: |
| 157 | /* Check is PM API version already verified */ |
Soren Brinkmann | a1b0a90 | 2016-09-30 11:30:21 -0700 | [diff] [blame] | 158 | if (pm_ctx.api_version == PM_VERSION) { |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 159 | SMC_RET1(handle, (uint64_t)PM_RET_SUCCESS | |
| 160 | ((uint64_t)PM_VERSION << 32)); |
Soren Brinkmann | a1b0a90 | 2016-09-30 11:30:21 -0700 | [diff] [blame] | 161 | } |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 162 | |
| 163 | ret = pm_get_api_version(&pm_ctx.api_version); |
Soren Brinkmann | a1b0a90 | 2016-09-30 11:30:21 -0700 | [diff] [blame] | 164 | /* |
| 165 | * Enable IPI IRQ |
| 166 | * assume the rich OS is OK to handle callback IRQs now. |
| 167 | * Even if we were wrong, it would not enable the IRQ in |
| 168 | * the GIC. |
| 169 | */ |
Wendy Liang | 328105c | 2017-10-03 23:21:11 -0700 | [diff] [blame] | 170 | pm_ipi_irq_enable(primary_proc); |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 171 | SMC_RET1(handle, (uint64_t)ret | |
| 172 | ((uint64_t)pm_ctx.api_version << 32)); |
| 173 | |
| 174 | case PM_SET_CONFIGURATION: |
| 175 | ret = pm_set_configuration(pm_arg[0]); |
| 176 | SMC_RET1(handle, (uint64_t)ret); |
| 177 | |
| 178 | case PM_GET_NODE_STATUS: |
| 179 | ret = pm_get_node_status(pm_arg[0]); |
| 180 | SMC_RET1(handle, (uint64_t)ret); |
| 181 | |
| 182 | case PM_GET_OP_CHARACTERISTIC: |
Anes Hadziahmetagic | 92aee01 | 2016-05-12 16:17:30 +0200 | [diff] [blame] | 183 | { |
| 184 | uint32_t result; |
| 185 | |
| 186 | ret = pm_get_op_characteristic(pm_arg[0], pm_arg[1], &result); |
| 187 | SMC_RET1(handle, (uint64_t)ret | ((uint64_t)result << 32)); |
| 188 | } |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 189 | |
| 190 | case PM_REGISTER_NOTIFIER: |
| 191 | ret = pm_register_notifier(pm_arg[0], pm_arg[1], pm_arg[2], |
| 192 | pm_arg[3]); |
| 193 | SMC_RET1(handle, (uint64_t)ret); |
| 194 | |
| 195 | case PM_RESET_ASSERT: |
| 196 | ret = pm_reset_assert(pm_arg[0], pm_arg[1]); |
| 197 | SMC_RET1(handle, (uint64_t)ret); |
| 198 | |
| 199 | case PM_RESET_GET_STATUS: |
| 200 | { |
| 201 | uint32_t reset_status; |
| 202 | |
| 203 | ret = pm_reset_get_status(pm_arg[0], &reset_status); |
| 204 | SMC_RET1(handle, (uint64_t)ret | |
| 205 | ((uint64_t)reset_status << 32)); |
| 206 | } |
| 207 | |
| 208 | /* PM memory access functions */ |
| 209 | case PM_MMIO_WRITE: |
| 210 | ret = pm_mmio_write(pm_arg[0], pm_arg[1], pm_arg[2]); |
| 211 | SMC_RET1(handle, (uint64_t)ret); |
| 212 | |
| 213 | case PM_MMIO_READ: |
| 214 | { |
| 215 | uint32_t value; |
| 216 | |
| 217 | ret = pm_mmio_read(pm_arg[0], &value); |
| 218 | SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32); |
| 219 | } |
Nava kishore Manne | 68d460c | 2016-08-20 23:18:09 +0530 | [diff] [blame] | 220 | |
| 221 | case PM_FPGA_LOAD: |
| 222 | ret = pm_fpga_load(pm_arg[0], pm_arg[1], pm_arg[2], pm_arg[3]); |
| 223 | SMC_RET1(handle, (uint64_t)ret); |
| 224 | |
| 225 | case PM_FPGA_GET_STATUS: |
| 226 | { |
| 227 | uint32_t value; |
| 228 | |
| 229 | ret = pm_fpga_get_status(&value); |
| 230 | SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32); |
| 231 | } |
| 232 | |
Siva Durga Prasad Paladugu | 16427d1 | 2016-08-24 11:45:47 +0530 | [diff] [blame] | 233 | case PM_GET_CHIPID: |
Soren Brinkmann | cb36681 | 2016-09-22 12:21:11 -0700 | [diff] [blame] | 234 | { |
| 235 | uint32_t result[2]; |
| 236 | |
| 237 | ret = pm_get_chipid(result); |
| 238 | SMC_RET2(handle, (uint64_t)ret | ((uint64_t)result[0] << 32), |
| 239 | result[1]); |
| 240 | } |
Siva Durga Prasad Paladugu | 16427d1 | 2016-08-24 11:45:47 +0530 | [diff] [blame] | 241 | |
Soren Brinkmann | 84f0af4 | 2016-09-30 14:24:25 -0700 | [diff] [blame] | 242 | case PM_GET_CALLBACK_DATA: |
| 243 | { |
| 244 | uint32_t result[4]; |
| 245 | |
| 246 | pm_get_callbackdata(result, sizeof(result)); |
| 247 | SMC_RET2(handle, |
| 248 | (uint64_t)result[0] | ((uint64_t)result[1] << 32), |
| 249 | (uint64_t)result[2] | ((uint64_t)result[3] << 32)); |
| 250 | } |
| 251 | |
Rajan Vaja | 8368761 | 2018-01-17 02:39:20 -0800 | [diff] [blame] | 252 | case PM_PINCTRL_REQUEST: |
| 253 | ret = pm_pinctrl_request(pm_arg[0]); |
| 254 | SMC_RET1(handle, (uint64_t)ret); |
| 255 | |
| 256 | case PM_PINCTRL_RELEASE: |
| 257 | ret = pm_pinctrl_release(pm_arg[0]); |
| 258 | SMC_RET1(handle, (uint64_t)ret); |
| 259 | |
| 260 | case PM_PINCTRL_GET_FUNCTION: |
| 261 | { |
Jolly Shah | 69fb5bf | 2018-02-07 16:25:41 -0800 | [diff] [blame] | 262 | uint32_t value = 0; |
Rajan Vaja | 8368761 | 2018-01-17 02:39:20 -0800 | [diff] [blame] | 263 | |
| 264 | ret = pm_pinctrl_get_function(pm_arg[0], &value); |
| 265 | SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32); |
| 266 | } |
| 267 | |
| 268 | case PM_PINCTRL_SET_FUNCTION: |
| 269 | ret = pm_pinctrl_set_function(pm_arg[0], pm_arg[1]); |
| 270 | SMC_RET1(handle, (uint64_t)ret); |
| 271 | |
| 272 | case PM_PINCTRL_CONFIG_PARAM_GET: |
| 273 | { |
| 274 | uint32_t value; |
| 275 | |
| 276 | ret = pm_pinctrl_get_config(pm_arg[0], pm_arg[1], &value); |
| 277 | SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32); |
| 278 | } |
| 279 | |
| 280 | case PM_PINCTRL_CONFIG_PARAM_SET: |
| 281 | ret = pm_pinctrl_set_config(pm_arg[0], pm_arg[1], pm_arg[2]); |
| 282 | SMC_RET1(handle, (uint64_t)ret); |
| 283 | |
Rajan Vaja | 5529a01 | 2018-01-17 02:39:23 -0800 | [diff] [blame] | 284 | case PM_IOCTL: |
| 285 | { |
| 286 | uint32_t value; |
| 287 | |
| 288 | ret = pm_ioctl(pm_arg[0], pm_arg[1], pm_arg[2], |
| 289 | pm_arg[3], &value); |
| 290 | SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32); |
| 291 | } |
| 292 | |
Rajan Vaja | 3511613 | 2018-01-17 02:39:25 -0800 | [diff] [blame] | 293 | case PM_QUERY_DATA: |
| 294 | { |
Jolly Shah | 69fb5bf | 2018-02-07 16:25:41 -0800 | [diff] [blame] | 295 | uint32_t data[4] = { 0 }; |
Rajan Vaja | 3511613 | 2018-01-17 02:39:25 -0800 | [diff] [blame] | 296 | |
| 297 | ret = pm_query_data(pm_arg[0], pm_arg[1], pm_arg[2], |
| 298 | pm_arg[3], data); |
| 299 | SMC_RET2(handle, (uint64_t)data[0] | ((uint64_t)data[1] << 32), |
| 300 | (uint64_t)data[2] | ((uint64_t)data[3] << 32)); |
| 301 | } |
| 302 | |
| 303 | case PM_CLOCK_ENABLE: |
| 304 | ret = pm_clock_enable(pm_arg[0]); |
| 305 | SMC_RET1(handle, (uint64_t)ret); |
| 306 | |
| 307 | case PM_CLOCK_DISABLE: |
| 308 | ret = pm_clock_disable(pm_arg[0]); |
| 309 | SMC_RET1(handle, (uint64_t)ret); |
| 310 | |
| 311 | case PM_CLOCK_GETSTATE: |
| 312 | { |
| 313 | uint32_t value; |
| 314 | |
| 315 | ret = pm_clock_getstate(pm_arg[0], &value); |
| 316 | SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32); |
| 317 | } |
| 318 | |
| 319 | case PM_CLOCK_SETDIVIDER: |
| 320 | ret = pm_clock_setdivider(pm_arg[0], pm_arg[1]); |
| 321 | SMC_RET1(handle, (uint64_t)ret); |
| 322 | |
| 323 | case PM_CLOCK_GETDIVIDER: |
| 324 | { |
| 325 | uint32_t value; |
| 326 | |
| 327 | ret = pm_clock_getdivider(pm_arg[0], &value); |
| 328 | SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32); |
| 329 | } |
| 330 | |
| 331 | case PM_CLOCK_SETRATE: |
| 332 | ret = pm_clock_setrate(pm_arg[0], |
| 333 | ((uint64_t)pm_arg[2]) << 32 | pm_arg[1]); |
| 334 | |
| 335 | SMC_RET1(handle, (uint64_t)ret); |
| 336 | |
| 337 | case PM_CLOCK_GETRATE: |
| 338 | { |
| 339 | uint64_t value; |
| 340 | |
| 341 | ret = pm_clock_getrate(pm_arg[0], &value); |
Jolly Shah | 69fb5bf | 2018-02-07 16:25:41 -0800 | [diff] [blame] | 342 | SMC_RET2(handle, (uint64_t)ret | |
| 343 | (((uint64_t)value & 0xFFFFFFFFU) << 32U), |
| 344 | (value >> 32U) & 0xFFFFFFFFU); |
Rajan Vaja | 3511613 | 2018-01-17 02:39:25 -0800 | [diff] [blame] | 345 | |
| 346 | } |
| 347 | |
| 348 | case PM_CLOCK_SETPARENT: |
| 349 | ret = pm_clock_setparent(pm_arg[0], pm_arg[1]); |
| 350 | SMC_RET1(handle, (uint64_t)ret); |
| 351 | |
| 352 | case PM_CLOCK_GETPARENT: |
| 353 | { |
| 354 | uint32_t value; |
| 355 | |
| 356 | ret = pm_clock_getparent(pm_arg[0], &value); |
| 357 | SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32); |
| 358 | } |
| 359 | |
Rajan Vaja | c7ee23d | 2018-02-14 23:10:54 -0800 | [diff] [blame] | 360 | case PM_GET_TRUSTZONE_VERSION: |
| 361 | SMC_RET1(handle, (uint64_t)PM_RET_SUCCESS | |
| 362 | ((uint64_t)ZYNQMP_TZ_VERSION << 32)); |
| 363 | |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 364 | default: |
| 365 | WARN("Unimplemented PM Service Call: 0x%x\n", smc_fid); |
| 366 | SMC_RET1(handle, SMC_UNK); |
| 367 | } |
| 368 | } |