Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013-2015, ARM Limited and Contributors. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are met: |
| 6 | * |
| 7 | * Redistributions of source code must retain the above copyright notice, this |
| 8 | * list of conditions and the following disclaimer. |
| 9 | * |
| 10 | * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * Neither the name of ARM nor the names of its contributors may be used |
| 15 | * to endorse or promote products derived from this software without specific |
| 16 | * prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | * POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | /* |
| 32 | * Top-level SMC handler for ZynqMP power management calls and |
| 33 | * IPI setup functions for communication with PMU. |
| 34 | */ |
| 35 | |
| 36 | #include <errno.h> |
| 37 | #include <gic_common.h> |
| 38 | #include <runtime_svc.h> |
| 39 | #include <string.h> |
| 40 | #include "pm_api_sys.h" |
| 41 | #include "pm_client.h" |
| 42 | #include "pm_ipi.h" |
| 43 | #include "../zynqmp_private.h" |
| 44 | |
| 45 | /* 0 - UP, !0 - DOWN */ |
| 46 | static int32_t pm_down = !0; |
| 47 | |
| 48 | /** |
| 49 | * pm_context - Structure which contains data for power management |
| 50 | * @api_version version of PM API, must match with one on PMU side |
| 51 | * @payload payload array used to store received |
| 52 | * data from ipi buffer registers |
| 53 | */ |
| 54 | static struct { |
| 55 | uint32_t api_version; |
| 56 | uint32_t payload[PAYLOAD_ARG_CNT]; |
| 57 | } pm_ctx; |
| 58 | |
| 59 | /** |
| 60 | * pm_setup() - PM service setup |
| 61 | * |
| 62 | * @return On success, the initialization function must return 0. |
| 63 | * Any other return value will cause the framework to ignore |
| 64 | * the service |
| 65 | * |
| 66 | * Initialization functions for ZynqMP power management for |
| 67 | * communicaton with PMU. |
| 68 | * |
| 69 | * Called from sip_svc_setup initialization function with the |
| 70 | * rt_svc_init signature. |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 71 | */ |
| 72 | int pm_setup(void) |
| 73 | { |
| 74 | int status; |
| 75 | |
| 76 | if (!zynqmp_is_pmu_up()) |
| 77 | return -ENODEV; |
| 78 | |
| 79 | status = pm_ipi_init(); |
| 80 | |
| 81 | if (status == 0) |
| 82 | INFO("BL31: PM Service Init Complete: API v%d.%d\n", |
| 83 | PM_VERSION_MAJOR, PM_VERSION_MINOR); |
| 84 | else |
| 85 | INFO("BL31: PM Service Init Failed, Error Code %d!\n", status); |
| 86 | |
| 87 | pm_down = status; |
| 88 | |
| 89 | return status; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * pm_smc_handler() - SMC handler for PM-API calls coming from EL1/EL2. |
| 94 | * @smc_fid - Function Identifier |
| 95 | * @x1 - x4 - Arguments |
| 96 | * @cookie - Unused |
| 97 | * @handler - Pointer to caller's context structure |
| 98 | * |
| 99 | * @return - Unused |
| 100 | * |
| 101 | * Determines that smc_fid is valid and supported PM SMC Function ID from the |
| 102 | * list of pm_api_ids, otherwise completes the request with |
| 103 | * the unknown SMC Function ID |
| 104 | * |
| 105 | * The SMC calls for PM service are forwarded from SIP Service SMC handler |
| 106 | * function with rt_svc_handle signature |
| 107 | */ |
| 108 | uint64_t pm_smc_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2, uint64_t x3, |
| 109 | uint64_t x4, void *cookie, void *handle, uint64_t flags) |
| 110 | { |
| 111 | enum pm_ret_status ret; |
| 112 | |
| 113 | uint32_t pm_arg[4]; |
| 114 | |
| 115 | /* Handle case where PM wasn't initialized properly */ |
| 116 | if (pm_down) |
| 117 | SMC_RET1(handle, SMC_UNK); |
| 118 | |
| 119 | pm_arg[0] = (uint32_t)x1; |
| 120 | pm_arg[1] = (uint32_t)(x1 >> 32); |
| 121 | pm_arg[2] = (uint32_t)x2; |
| 122 | pm_arg[3] = (uint32_t)(x2 >> 32); |
| 123 | |
| 124 | switch (smc_fid & FUNCID_NUM_MASK) { |
| 125 | /* PM API Functions */ |
| 126 | case PM_SELF_SUSPEND: |
| 127 | ret = pm_self_suspend(pm_arg[0], pm_arg[1], pm_arg[2], |
| 128 | pm_arg[3]); |
| 129 | SMC_RET1(handle, (uint64_t)ret); |
| 130 | |
| 131 | case PM_REQ_SUSPEND: |
| 132 | ret = pm_req_suspend(pm_arg[0], pm_arg[1], pm_arg[2], |
| 133 | pm_arg[3]); |
| 134 | SMC_RET1(handle, (uint64_t)ret); |
| 135 | |
| 136 | case PM_REQ_WAKEUP: |
| 137 | ret = pm_req_wakeup(pm_arg[0], pm_arg[1], pm_arg[2], |
| 138 | pm_arg[3]); |
| 139 | SMC_RET1(handle, (uint64_t)ret); |
| 140 | |
| 141 | case PM_FORCE_POWERDOWN: |
| 142 | ret = pm_force_powerdown(pm_arg[0], pm_arg[1]); |
| 143 | SMC_RET1(handle, (uint64_t)ret); |
| 144 | |
| 145 | case PM_ABORT_SUSPEND: |
| 146 | ret = pm_abort_suspend(pm_arg[0]); |
| 147 | SMC_RET1(handle, (uint64_t)ret); |
| 148 | |
| 149 | case PM_SET_WAKEUP_SOURCE: |
| 150 | ret = pm_set_wakeup_source(pm_arg[0], pm_arg[1], pm_arg[2]); |
| 151 | SMC_RET1(handle, (uint64_t)ret); |
| 152 | |
| 153 | case PM_SYSTEM_SHUTDOWN: |
Soren Brinkmann | 58fbb9b | 2016-09-02 09:50:54 -0700 | [diff] [blame] | 154 | ret = pm_system_shutdown(pm_arg[0], pm_arg[1]); |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 155 | SMC_RET1(handle, (uint64_t)ret); |
| 156 | |
| 157 | case PM_REQ_NODE: |
| 158 | ret = pm_req_node(pm_arg[0], pm_arg[1], pm_arg[2], pm_arg[3]); |
| 159 | SMC_RET1(handle, (uint64_t)ret); |
| 160 | |
| 161 | case PM_RELEASE_NODE: |
| 162 | ret = pm_release_node(pm_arg[0]); |
| 163 | SMC_RET1(handle, (uint64_t)ret); |
| 164 | |
| 165 | case PM_SET_REQUIREMENT: |
| 166 | ret = pm_set_requirement(pm_arg[0], pm_arg[1], pm_arg[2], |
| 167 | pm_arg[3]); |
| 168 | SMC_RET1(handle, (uint64_t)ret); |
| 169 | |
| 170 | case PM_SET_MAX_LATENCY: |
| 171 | ret = pm_set_max_latency(pm_arg[0], pm_arg[1]); |
| 172 | SMC_RET1(handle, (uint64_t)ret); |
| 173 | |
| 174 | case PM_GET_API_VERSION: |
| 175 | /* Check is PM API version already verified */ |
| 176 | if (pm_ctx.api_version == PM_VERSION) |
| 177 | SMC_RET1(handle, (uint64_t)PM_RET_SUCCESS | |
| 178 | ((uint64_t)PM_VERSION << 32)); |
| 179 | |
| 180 | ret = pm_get_api_version(&pm_ctx.api_version); |
| 181 | SMC_RET1(handle, (uint64_t)ret | |
| 182 | ((uint64_t)pm_ctx.api_version << 32)); |
| 183 | |
| 184 | case PM_SET_CONFIGURATION: |
| 185 | ret = pm_set_configuration(pm_arg[0]); |
| 186 | SMC_RET1(handle, (uint64_t)ret); |
| 187 | |
| 188 | case PM_GET_NODE_STATUS: |
| 189 | ret = pm_get_node_status(pm_arg[0]); |
| 190 | SMC_RET1(handle, (uint64_t)ret); |
| 191 | |
| 192 | case PM_GET_OP_CHARACTERISTIC: |
Anes Hadziahmetagic | 92aee01 | 2016-05-12 16:17:30 +0200 | [diff] [blame] | 193 | { |
| 194 | uint32_t result; |
| 195 | |
| 196 | ret = pm_get_op_characteristic(pm_arg[0], pm_arg[1], &result); |
| 197 | SMC_RET1(handle, (uint64_t)ret | ((uint64_t)result << 32)); |
| 198 | } |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 199 | |
| 200 | case PM_REGISTER_NOTIFIER: |
| 201 | ret = pm_register_notifier(pm_arg[0], pm_arg[1], pm_arg[2], |
| 202 | pm_arg[3]); |
| 203 | SMC_RET1(handle, (uint64_t)ret); |
| 204 | |
| 205 | case PM_RESET_ASSERT: |
| 206 | ret = pm_reset_assert(pm_arg[0], pm_arg[1]); |
| 207 | SMC_RET1(handle, (uint64_t)ret); |
| 208 | |
| 209 | case PM_RESET_GET_STATUS: |
| 210 | { |
| 211 | uint32_t reset_status; |
| 212 | |
| 213 | ret = pm_reset_get_status(pm_arg[0], &reset_status); |
| 214 | SMC_RET1(handle, (uint64_t)ret | |
| 215 | ((uint64_t)reset_status << 32)); |
| 216 | } |
| 217 | |
| 218 | /* PM memory access functions */ |
| 219 | case PM_MMIO_WRITE: |
| 220 | ret = pm_mmio_write(pm_arg[0], pm_arg[1], pm_arg[2]); |
| 221 | SMC_RET1(handle, (uint64_t)ret); |
| 222 | |
| 223 | case PM_MMIO_READ: |
| 224 | { |
| 225 | uint32_t value; |
| 226 | |
| 227 | ret = pm_mmio_read(pm_arg[0], &value); |
| 228 | SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32); |
| 229 | } |
Nava kishore Manne | 68d460c | 2016-08-20 23:18:09 +0530 | [diff] [blame] | 230 | |
| 231 | case PM_FPGA_LOAD: |
| 232 | ret = pm_fpga_load(pm_arg[0], pm_arg[1], pm_arg[2], pm_arg[3]); |
| 233 | SMC_RET1(handle, (uint64_t)ret); |
| 234 | |
| 235 | case PM_FPGA_GET_STATUS: |
| 236 | { |
| 237 | uint32_t value; |
| 238 | |
| 239 | ret = pm_fpga_get_status(&value); |
| 240 | SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32); |
| 241 | } |
| 242 | |
Siva Durga Prasad Paladugu | 16427d1 | 2016-08-24 11:45:47 +0530 | [diff] [blame] | 243 | case PM_GET_CHIPID: |
Soren Brinkmann | cb36681 | 2016-09-22 12:21:11 -0700 | [diff] [blame] | 244 | { |
| 245 | uint32_t result[2]; |
| 246 | |
| 247 | ret = pm_get_chipid(result); |
| 248 | SMC_RET2(handle, (uint64_t)ret | ((uint64_t)result[0] << 32), |
| 249 | result[1]); |
| 250 | } |
Siva Durga Prasad Paladugu | 16427d1 | 2016-08-24 11:45:47 +0530 | [diff] [blame] | 251 | |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 252 | default: |
| 253 | WARN("Unimplemented PM Service Call: 0x%x\n", smc_fid); |
| 254 | SMC_RET1(handle, SMC_UNK); |
| 255 | } |
| 256 | } |