Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 1 | /* |
Vikram Kanigiri | 7208419 | 2016-02-08 16:29:30 +0000 | [diff] [blame] | 2 | * Copyright (c) 2014-2016, ARM Limited and Contributors. All rights reserved. |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 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 | #include <arch_helpers.h> |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 32 | #include <assert.h> |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 33 | #include <css_def.h> |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 34 | #include <debug.h> |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 35 | #include <platform.h> |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 36 | #include <string.h> |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 37 | #include "css_mhu.h" |
| 38 | #include "css_scpi.h" |
| 39 | |
Vikram Kanigiri | 7208419 | 2016-02-08 16:29:30 +0000 | [diff] [blame] | 40 | #define SCPI_SHARED_MEM_SCP_TO_AP PLAT_CSS_SCP_COM_SHARED_MEM_BASE |
| 41 | #define SCPI_SHARED_MEM_AP_TO_SCP (PLAT_CSS_SCP_COM_SHARED_MEM_BASE \ |
| 42 | + 0x100) |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 43 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 44 | #define SCPI_CMD_HEADER_AP_TO_SCP \ |
| 45 | ((scpi_cmd_t *) SCPI_SHARED_MEM_AP_TO_SCP) |
| 46 | #define SCPI_CMD_PAYLOAD_AP_TO_SCP \ |
| 47 | ((void *) (SCPI_SHARED_MEM_AP_TO_SCP + sizeof(scpi_cmd_t))) |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 48 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 49 | /* ID of the MHU slot used for the SCPI protocol */ |
| 50 | #define SCPI_MHU_SLOT_ID 0 |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 51 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 52 | static void scpi_secure_message_start(void) |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 53 | { |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 54 | mhu_secure_message_start(SCPI_MHU_SLOT_ID); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 57 | static void scpi_secure_message_send(size_t payload_size) |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 58 | { |
Juan Castillo | 2e86cb1 | 2016-01-13 15:01:09 +0000 | [diff] [blame] | 59 | /* Ensure that any write to the SCPI payload area is seen by SCP before |
| 60 | * we write to the MHU register. If these 2 writes were reordered by |
| 61 | * the CPU then SCP would read stale payload data */ |
| 62 | dmbst(); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 63 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 64 | mhu_secure_message_send(SCPI_MHU_SLOT_ID); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 67 | static void scpi_secure_message_receive(scpi_cmd_t *cmd) |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 68 | { |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 69 | uint32_t mhu_status; |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 70 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 71 | assert(cmd != NULL); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 72 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 73 | mhu_status = mhu_secure_message_wait(); |
| 74 | |
| 75 | /* Expect an SCPI message, reject any other protocol */ |
| 76 | if (mhu_status != (1 << SCPI_MHU_SLOT_ID)) { |
| 77 | ERROR("MHU: Unexpected protocol (MHU status: 0x%x)\n", |
| 78 | mhu_status); |
| 79 | panic(); |
| 80 | } |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 81 | |
Juan Castillo | 2e86cb1 | 2016-01-13 15:01:09 +0000 | [diff] [blame] | 82 | /* Ensure that any read to the SCPI payload area is done after reading |
| 83 | * the MHU register. If these 2 reads were reordered then the CPU would |
| 84 | * read invalid payload data */ |
| 85 | dmbld(); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 86 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 87 | memcpy(cmd, (void *) SCPI_SHARED_MEM_SCP_TO_AP, sizeof(*cmd)); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 90 | static void scpi_secure_message_end(void) |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 91 | { |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 92 | mhu_secure_message_end(SCPI_MHU_SLOT_ID); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | int scpi_wait_ready(void) |
| 96 | { |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 97 | scpi_cmd_t scpi_cmd; |
| 98 | |
| 99 | VERBOSE("Waiting for SCP_READY command...\n"); |
| 100 | |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 101 | /* Get a message from the SCP */ |
| 102 | scpi_secure_message_start(); |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 103 | scpi_secure_message_receive(&scpi_cmd); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 104 | scpi_secure_message_end(); |
| 105 | |
| 106 | /* We are expecting 'SCP Ready', produce correct error if it's not */ |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 107 | scpi_status_t status = SCP_OK; |
| 108 | if (scpi_cmd.id != SCPI_CMD_SCP_READY) { |
| 109 | ERROR("Unexpected SCP command: expected command #%u, got command #%u\n", |
| 110 | SCPI_CMD_SCP_READY, scpi_cmd.id); |
| 111 | status = SCP_E_SUPPORT; |
| 112 | } else if (scpi_cmd.size != 0) { |
| 113 | ERROR("SCP_READY command has incorrect size: expected 0, got %u\n", |
| 114 | scpi_cmd.size); |
| 115 | status = SCP_E_SIZE; |
| 116 | } |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 117 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 118 | VERBOSE("Sending response for SCP_READY command\n"); |
| 119 | |
| 120 | /* |
| 121 | * Send our response back to SCP. |
| 122 | * We are using the same SCPI header, just update the status field. |
| 123 | */ |
| 124 | scpi_cmd.status = status; |
| 125 | scpi_secure_message_start(); |
| 126 | memcpy((void *) SCPI_SHARED_MEM_AP_TO_SCP, &scpi_cmd, sizeof(scpi_cmd)); |
| 127 | scpi_secure_message_send(0); |
| 128 | scpi_secure_message_end(); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 129 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 130 | return status == SCP_OK ? 0 : -1; |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void scpi_set_css_power_state(unsigned mpidr, scpi_power_state_t cpu_state, |
| 134 | scpi_power_state_t cluster_state, scpi_power_state_t css_state) |
| 135 | { |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 136 | scpi_cmd_t *cmd; |
| 137 | uint32_t state = 0; |
| 138 | uint32_t *payload_addr; |
| 139 | |
| 140 | state |= mpidr & 0x0f; /* CPU ID */ |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 141 | state |= (mpidr & 0xf00) >> 4; /* Cluster ID */ |
| 142 | state |= cpu_state << 8; |
| 143 | state |= cluster_state << 12; |
| 144 | state |= css_state << 16; |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 145 | |
| 146 | scpi_secure_message_start(); |
| 147 | |
| 148 | /* Populate the command header */ |
| 149 | cmd = SCPI_CMD_HEADER_AP_TO_SCP; |
| 150 | cmd->id = SCPI_CMD_SET_CSS_POWER_STATE; |
| 151 | cmd->set = SCPI_SET_NORMAL; |
| 152 | cmd->sender = 0; |
| 153 | cmd->size = sizeof(state); |
| 154 | /* Populate the command payload */ |
| 155 | payload_addr = SCPI_CMD_PAYLOAD_AP_TO_SCP; |
| 156 | *payload_addr = state; |
| 157 | scpi_secure_message_send(sizeof(state)); |
| 158 | /* |
| 159 | * SCP does not reply to this command in order to avoid MHU interrupts |
| 160 | * from the sender, which could interfere with its power state request. |
| 161 | */ |
| 162 | |
| 163 | scpi_secure_message_end(); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | uint32_t scpi_sys_power_state(scpi_system_state_t system_state) |
| 167 | { |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 168 | scpi_cmd_t *cmd; |
| 169 | uint8_t *payload_addr; |
| 170 | scpi_cmd_t response; |
| 171 | |
| 172 | scpi_secure_message_start(); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 173 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 174 | /* Populate the command header */ |
| 175 | cmd = SCPI_CMD_HEADER_AP_TO_SCP; |
| 176 | cmd->id = SCPI_CMD_SYS_POWER_STATE; |
| 177 | cmd->set = 0; |
| 178 | cmd->sender = 0; |
| 179 | cmd->size = sizeof(*payload_addr); |
| 180 | /* Populate the command payload */ |
| 181 | payload_addr = SCPI_CMD_PAYLOAD_AP_TO_SCP; |
| 182 | *payload_addr = system_state & 0xff; |
| 183 | scpi_secure_message_send(sizeof(*payload_addr)); |
| 184 | |
| 185 | scpi_secure_message_receive(&response); |
| 186 | |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 187 | scpi_secure_message_end(); |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 188 | |
| 189 | return response.status; |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 190 | } |