Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 1 | /* |
Wendy Liang | 328105c | 2017-10-03 23:21:11 -0700 | [diff] [blame] | 2 | * Copyright (c) 2013-2017, 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 | |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 251 | default: |
| 252 | WARN("Unimplemented PM Service Call: 0x%x\n", smc_fid); |
| 253 | SMC_RET1(handle, SMC_UNK); |
| 254 | } |
| 255 | } |