blob: 0a4eafe0cb546bc7010557f07c766fb4db5eb92a [file] [log] [blame]
Dan Handley9df48042015-03-19 18:58:55 +00001/*
Sandrine Bailleux04b66d82015-03-18 14:52:53 +00002 * Copyright (c) 2014-2015, ARM Limited and Contributors. All rights reserved.
Dan Handley9df48042015-03-19 18:58:55 +00003 *
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 Bailleux04b66d82015-03-18 14:52:53 +000032#include <assert.h>
Dan Handley9df48042015-03-19 18:58:55 +000033#include <css_def.h>
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000034#include <debug.h>
Dan Handley9df48042015-03-19 18:58:55 +000035#include <platform.h>
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000036#include <string.h>
Dan Handley9df48042015-03-19 18:58:55 +000037#include "css_mhu.h"
38#include "css_scpi.h"
39
Sandrine Bailleux47ea1bc2015-06-09 11:53:33 +010040#define SCPI_SHARED_MEM_SCP_TO_AP SCP_COM_SHARED_MEM_BASE
41#define SCPI_SHARED_MEM_AP_TO_SCP (SCP_COM_SHARED_MEM_BASE + 0x100)
Dan Handley9df48042015-03-19 18:58:55 +000042
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000043#define SCPI_CMD_HEADER_AP_TO_SCP \
44 ((scpi_cmd_t *) SCPI_SHARED_MEM_AP_TO_SCP)
45#define SCPI_CMD_PAYLOAD_AP_TO_SCP \
46 ((void *) (SCPI_SHARED_MEM_AP_TO_SCP + sizeof(scpi_cmd_t)))
Dan Handley9df48042015-03-19 18:58:55 +000047
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000048/* ID of the MHU slot used for the SCPI protocol */
49#define SCPI_MHU_SLOT_ID 0
Dan Handley9df48042015-03-19 18:58:55 +000050
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000051static void scpi_secure_message_start(void)
Dan Handley9df48042015-03-19 18:58:55 +000052{
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000053 mhu_secure_message_start(SCPI_MHU_SLOT_ID);
Dan Handley9df48042015-03-19 18:58:55 +000054}
55
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000056static void scpi_secure_message_send(size_t payload_size)
Dan Handley9df48042015-03-19 18:58:55 +000057{
58 /* Make sure payload can be seen by SCP */
59 if (MHU_PAYLOAD_CACHED)
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000060 flush_dcache_range(SCPI_SHARED_MEM_AP_TO_SCP,
61 sizeof(scpi_cmd_t) + payload_size);
Dan Handley9df48042015-03-19 18:58:55 +000062
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000063 mhu_secure_message_send(SCPI_MHU_SLOT_ID);
Dan Handley9df48042015-03-19 18:58:55 +000064}
65
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000066static void scpi_secure_message_receive(scpi_cmd_t *cmd)
Dan Handley9df48042015-03-19 18:58:55 +000067{
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000068 uint32_t mhu_status;
Dan Handley9df48042015-03-19 18:58:55 +000069
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000070 assert(cmd != NULL);
Dan Handley9df48042015-03-19 18:58:55 +000071
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000072 mhu_status = mhu_secure_message_wait();
73
74 /* Expect an SCPI message, reject any other protocol */
75 if (mhu_status != (1 << SCPI_MHU_SLOT_ID)) {
76 ERROR("MHU: Unexpected protocol (MHU status: 0x%x)\n",
77 mhu_status);
78 panic();
79 }
Dan Handley9df48042015-03-19 18:58:55 +000080
81 /* Make sure we don't read stale data */
82 if (MHU_PAYLOAD_CACHED)
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000083 inv_dcache_range(SCPI_SHARED_MEM_SCP_TO_AP, sizeof(*cmd));
Dan Handley9df48042015-03-19 18:58:55 +000084
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000085 memcpy(cmd, (void *) SCPI_SHARED_MEM_SCP_TO_AP, sizeof(*cmd));
Dan Handley9df48042015-03-19 18:58:55 +000086}
87
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000088static void scpi_secure_message_end(void)
Dan Handley9df48042015-03-19 18:58:55 +000089{
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000090 mhu_secure_message_end(SCPI_MHU_SLOT_ID);
Dan Handley9df48042015-03-19 18:58:55 +000091}
92
93int scpi_wait_ready(void)
94{
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000095 scpi_cmd_t scpi_cmd;
96
97 VERBOSE("Waiting for SCP_READY command...\n");
98
Dan Handley9df48042015-03-19 18:58:55 +000099 /* Get a message from the SCP */
100 scpi_secure_message_start();
Sandrine Bailleux04b66d82015-03-18 14:52:53 +0000101 scpi_secure_message_receive(&scpi_cmd);
Dan Handley9df48042015-03-19 18:58:55 +0000102 scpi_secure_message_end();
103
104 /* We are expecting 'SCP Ready', produce correct error if it's not */
Sandrine Bailleux04b66d82015-03-18 14:52:53 +0000105 scpi_status_t status = SCP_OK;
106 if (scpi_cmd.id != SCPI_CMD_SCP_READY) {
107 ERROR("Unexpected SCP command: expected command #%u, got command #%u\n",
108 SCPI_CMD_SCP_READY, scpi_cmd.id);
109 status = SCP_E_SUPPORT;
110 } else if (scpi_cmd.size != 0) {
111 ERROR("SCP_READY command has incorrect size: expected 0, got %u\n",
112 scpi_cmd.size);
113 status = SCP_E_SIZE;
114 }
Dan Handley9df48042015-03-19 18:58:55 +0000115
Sandrine Bailleux04b66d82015-03-18 14:52:53 +0000116 VERBOSE("Sending response for SCP_READY command\n");
117
118 /*
119 * Send our response back to SCP.
120 * We are using the same SCPI header, just update the status field.
121 */
122 scpi_cmd.status = status;
123 scpi_secure_message_start();
124 memcpy((void *) SCPI_SHARED_MEM_AP_TO_SCP, &scpi_cmd, sizeof(scpi_cmd));
125 scpi_secure_message_send(0);
126 scpi_secure_message_end();
Dan Handley9df48042015-03-19 18:58:55 +0000127
Sandrine Bailleux04b66d82015-03-18 14:52:53 +0000128 return status == SCP_OK ? 0 : -1;
Dan Handley9df48042015-03-19 18:58:55 +0000129}
130
131void scpi_set_css_power_state(unsigned mpidr, scpi_power_state_t cpu_state,
132 scpi_power_state_t cluster_state, scpi_power_state_t css_state)
133{
Sandrine Bailleux04b66d82015-03-18 14:52:53 +0000134 scpi_cmd_t *cmd;
135 uint32_t state = 0;
136 uint32_t *payload_addr;
137
138 state |= mpidr & 0x0f; /* CPU ID */
Dan Handley9df48042015-03-19 18:58:55 +0000139 state |= (mpidr & 0xf00) >> 4; /* Cluster ID */
140 state |= cpu_state << 8;
141 state |= cluster_state << 12;
142 state |= css_state << 16;
Sandrine Bailleux04b66d82015-03-18 14:52:53 +0000143
144 scpi_secure_message_start();
145
146 /* Populate the command header */
147 cmd = SCPI_CMD_HEADER_AP_TO_SCP;
148 cmd->id = SCPI_CMD_SET_CSS_POWER_STATE;
149 cmd->set = SCPI_SET_NORMAL;
150 cmd->sender = 0;
151 cmd->size = sizeof(state);
152 /* Populate the command payload */
153 payload_addr = SCPI_CMD_PAYLOAD_AP_TO_SCP;
154 *payload_addr = state;
155 scpi_secure_message_send(sizeof(state));
156 /*
157 * SCP does not reply to this command in order to avoid MHU interrupts
158 * from the sender, which could interfere with its power state request.
159 */
160
161 scpi_secure_message_end();
Dan Handley9df48042015-03-19 18:58:55 +0000162}
163
164uint32_t scpi_sys_power_state(scpi_system_state_t system_state)
165{
Sandrine Bailleux04b66d82015-03-18 14:52:53 +0000166 scpi_cmd_t *cmd;
167 uint8_t *payload_addr;
168 scpi_cmd_t response;
169
170 scpi_secure_message_start();
Dan Handley9df48042015-03-19 18:58:55 +0000171
Sandrine Bailleux04b66d82015-03-18 14:52:53 +0000172 /* Populate the command header */
173 cmd = SCPI_CMD_HEADER_AP_TO_SCP;
174 cmd->id = SCPI_CMD_SYS_POWER_STATE;
175 cmd->set = 0;
176 cmd->sender = 0;
177 cmd->size = sizeof(*payload_addr);
178 /* Populate the command payload */
179 payload_addr = SCPI_CMD_PAYLOAD_AP_TO_SCP;
180 *payload_addr = system_state & 0xff;
181 scpi_secure_message_send(sizeof(*payload_addr));
182
183 scpi_secure_message_receive(&response);
184
Dan Handley9df48042015-03-19 18:58:55 +0000185 scpi_secure_message_end();
Sandrine Bailleux04b66d82015-03-18 14:52:53 +0000186
187 return response.status;
Dan Handley9df48042015-03-19 18:58:55 +0000188}