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