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. |
| 71 | * |
| 72 | */ |
| 73 | int pm_setup(void) |
| 74 | { |
| 75 | int status; |
| 76 | |
| 77 | if (!zynqmp_is_pmu_up()) |
| 78 | return -ENODEV; |
| 79 | |
| 80 | status = pm_ipi_init(); |
| 81 | |
| 82 | if (status == 0) |
| 83 | INFO("BL31: PM Service Init Complete: API v%d.%d\n", |
| 84 | PM_VERSION_MAJOR, PM_VERSION_MINOR); |
| 85 | else |
| 86 | INFO("BL31: PM Service Init Failed, Error Code %d!\n", status); |
| 87 | |
| 88 | pm_down = status; |
| 89 | |
| 90 | return status; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * pm_smc_handler() - SMC handler for PM-API calls coming from EL1/EL2. |
| 95 | * @smc_fid - Function Identifier |
| 96 | * @x1 - x4 - Arguments |
| 97 | * @cookie - Unused |
| 98 | * @handler - Pointer to caller's context structure |
| 99 | * |
| 100 | * @return - Unused |
| 101 | * |
| 102 | * Determines that smc_fid is valid and supported PM SMC Function ID from the |
| 103 | * list of pm_api_ids, otherwise completes the request with |
| 104 | * the unknown SMC Function ID |
| 105 | * |
| 106 | * The SMC calls for PM service are forwarded from SIP Service SMC handler |
| 107 | * function with rt_svc_handle signature |
| 108 | */ |
| 109 | uint64_t pm_smc_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2, uint64_t x3, |
| 110 | uint64_t x4, void *cookie, void *handle, uint64_t flags) |
| 111 | { |
| 112 | enum pm_ret_status ret; |
| 113 | |
| 114 | uint32_t pm_arg[4]; |
| 115 | |
| 116 | /* Handle case where PM wasn't initialized properly */ |
| 117 | if (pm_down) |
| 118 | SMC_RET1(handle, SMC_UNK); |
| 119 | |
| 120 | pm_arg[0] = (uint32_t)x1; |
| 121 | pm_arg[1] = (uint32_t)(x1 >> 32); |
| 122 | pm_arg[2] = (uint32_t)x2; |
| 123 | pm_arg[3] = (uint32_t)(x2 >> 32); |
| 124 | |
| 125 | switch (smc_fid & FUNCID_NUM_MASK) { |
| 126 | /* PM API Functions */ |
| 127 | case PM_SELF_SUSPEND: |
| 128 | ret = pm_self_suspend(pm_arg[0], pm_arg[1], pm_arg[2], |
| 129 | pm_arg[3]); |
| 130 | SMC_RET1(handle, (uint64_t)ret); |
| 131 | |
| 132 | case PM_REQ_SUSPEND: |
| 133 | ret = pm_req_suspend(pm_arg[0], pm_arg[1], pm_arg[2], |
| 134 | pm_arg[3]); |
| 135 | SMC_RET1(handle, (uint64_t)ret); |
| 136 | |
| 137 | case PM_REQ_WAKEUP: |
| 138 | ret = pm_req_wakeup(pm_arg[0], pm_arg[1], pm_arg[2], |
| 139 | pm_arg[3]); |
| 140 | SMC_RET1(handle, (uint64_t)ret); |
| 141 | |
| 142 | case PM_FORCE_POWERDOWN: |
| 143 | ret = pm_force_powerdown(pm_arg[0], pm_arg[1]); |
| 144 | SMC_RET1(handle, (uint64_t)ret); |
| 145 | |
| 146 | case PM_ABORT_SUSPEND: |
| 147 | ret = pm_abort_suspend(pm_arg[0]); |
| 148 | SMC_RET1(handle, (uint64_t)ret); |
| 149 | |
| 150 | case PM_SET_WAKEUP_SOURCE: |
| 151 | ret = pm_set_wakeup_source(pm_arg[0], pm_arg[1], pm_arg[2]); |
| 152 | SMC_RET1(handle, (uint64_t)ret); |
| 153 | |
| 154 | case PM_SYSTEM_SHUTDOWN: |
| 155 | ret = pm_system_shutdown(pm_arg[0]); |
| 156 | SMC_RET1(handle, (uint64_t)ret); |
| 157 | |
| 158 | case PM_REQ_NODE: |
| 159 | ret = pm_req_node(pm_arg[0], pm_arg[1], pm_arg[2], pm_arg[3]); |
| 160 | SMC_RET1(handle, (uint64_t)ret); |
| 161 | |
| 162 | case PM_RELEASE_NODE: |
| 163 | ret = pm_release_node(pm_arg[0]); |
| 164 | SMC_RET1(handle, (uint64_t)ret); |
| 165 | |
| 166 | case PM_SET_REQUIREMENT: |
| 167 | ret = pm_set_requirement(pm_arg[0], pm_arg[1], pm_arg[2], |
| 168 | pm_arg[3]); |
| 169 | SMC_RET1(handle, (uint64_t)ret); |
| 170 | |
| 171 | case PM_SET_MAX_LATENCY: |
| 172 | ret = pm_set_max_latency(pm_arg[0], pm_arg[1]); |
| 173 | SMC_RET1(handle, (uint64_t)ret); |
| 174 | |
| 175 | case PM_GET_API_VERSION: |
| 176 | /* Check is PM API version already verified */ |
| 177 | if (pm_ctx.api_version == PM_VERSION) |
| 178 | SMC_RET1(handle, (uint64_t)PM_RET_SUCCESS | |
| 179 | ((uint64_t)PM_VERSION << 32)); |
| 180 | |
| 181 | ret = pm_get_api_version(&pm_ctx.api_version); |
| 182 | SMC_RET1(handle, (uint64_t)ret | |
| 183 | ((uint64_t)pm_ctx.api_version << 32)); |
| 184 | |
| 185 | case PM_SET_CONFIGURATION: |
| 186 | ret = pm_set_configuration(pm_arg[0]); |
| 187 | SMC_RET1(handle, (uint64_t)ret); |
| 188 | |
| 189 | case PM_GET_NODE_STATUS: |
| 190 | ret = pm_get_node_status(pm_arg[0]); |
| 191 | SMC_RET1(handle, (uint64_t)ret); |
| 192 | |
| 193 | case PM_GET_OP_CHARACTERISTIC: |
Anes Hadziahmetagic | 92aee01 | 2016-05-12 16:17:30 +0200 | [diff] [blame] | 194 | { |
| 195 | uint32_t result; |
| 196 | |
| 197 | ret = pm_get_op_characteristic(pm_arg[0], pm_arg[1], &result); |
| 198 | SMC_RET1(handle, (uint64_t)ret | ((uint64_t)result << 32)); |
| 199 | } |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 200 | |
| 201 | case PM_REGISTER_NOTIFIER: |
| 202 | ret = pm_register_notifier(pm_arg[0], pm_arg[1], pm_arg[2], |
| 203 | pm_arg[3]); |
| 204 | SMC_RET1(handle, (uint64_t)ret); |
| 205 | |
| 206 | case PM_RESET_ASSERT: |
| 207 | ret = pm_reset_assert(pm_arg[0], pm_arg[1]); |
| 208 | SMC_RET1(handle, (uint64_t)ret); |
| 209 | |
| 210 | case PM_RESET_GET_STATUS: |
| 211 | { |
| 212 | uint32_t reset_status; |
| 213 | |
| 214 | ret = pm_reset_get_status(pm_arg[0], &reset_status); |
| 215 | SMC_RET1(handle, (uint64_t)ret | |
| 216 | ((uint64_t)reset_status << 32)); |
| 217 | } |
| 218 | |
| 219 | /* PM memory access functions */ |
| 220 | case PM_MMIO_WRITE: |
| 221 | ret = pm_mmio_write(pm_arg[0], pm_arg[1], pm_arg[2]); |
| 222 | SMC_RET1(handle, (uint64_t)ret); |
| 223 | |
| 224 | case PM_MMIO_READ: |
| 225 | { |
| 226 | uint32_t value; |
| 227 | |
| 228 | ret = pm_mmio_read(pm_arg[0], &value); |
| 229 | SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32); |
| 230 | } |
| 231 | default: |
| 232 | WARN("Unimplemented PM Service Call: 0x%x\n", smc_fid); |
| 233 | SMC_RET1(handle, SMC_UNK); |
| 234 | } |
| 235 | } |