1 /*
2  * Copyright (c) 2014, 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 #include <arch_helpers.h>
32 #include <platform.h>
33 #include "juno_def.h"
34 #include "mhu.h"
35 #include "scpi.h"
36 
37 #define MHU_SECURE_SCP_TO_AP_PAYLOAD	(MHU_SECURE_BASE+0x0080)
38 #define MHU_SECURE_AP_TO_SCP_PAYLOAD	(MHU_SECURE_BASE+0x0280)
39 
40 #define SIZE_SHIFT	20	/* Bit position for size value in MHU header */
41 #define SIZE_MASK	0x1ff	/* Mask to extract size value in MHU header*/
42 
43 
scpi_secure_message_start(void)44 void *scpi_secure_message_start(void)
45 {
46 	mhu_secure_message_start();
47 
48 	/* Return address of payload area. */
49 	return (void *)MHU_SECURE_AP_TO_SCP_PAYLOAD;
50 }
51 
scpi_secure_message_send(unsigned command,size_t size)52 void scpi_secure_message_send(unsigned command, size_t size)
53 {
54 	/* Make sure payload can be seen by SCP */
55 	if (MHU_PAYLOAD_CACHED)
56 		flush_dcache_range(MHU_SECURE_AP_TO_SCP_PAYLOAD, size);
57 
58 	mhu_secure_message_send(command | (size << SIZE_SHIFT));
59 }
60 
scpi_secure_message_receive(void ** message_out,size_t * size_out)61 unsigned scpi_secure_message_receive(void **message_out, size_t *size_out)
62 {
63 	uint32_t response =  mhu_secure_message_wait();
64 
65 	/* Get size of payload */
66 	size_t size = (response >> SIZE_SHIFT) & SIZE_MASK;
67 
68 	/* Clear size from response */
69 	response &= ~(SIZE_MASK << SIZE_SHIFT);
70 
71 	/* Make sure we don't read stale data */
72 	if (MHU_PAYLOAD_CACHED)
73 		inv_dcache_range(MHU_SECURE_SCP_TO_AP_PAYLOAD, size);
74 
75 	if (size_out)
76 		*size_out = size;
77 
78 	if (message_out)
79 		*message_out = (void *)MHU_SECURE_SCP_TO_AP_PAYLOAD;
80 
81 	return response;
82 }
83 
scpi_secure_message_end(void)84 void scpi_secure_message_end(void)
85 {
86 	mhu_secure_message_end();
87 }
88 
scpi_secure_send32(unsigned command,uint32_t message)89 static void scpi_secure_send32(unsigned command, uint32_t message)
90 {
91 	*(__typeof__(message) *)scpi_secure_message_start() = message;
92 	scpi_secure_message_send(command, sizeof(message));
93 	scpi_secure_message_end();
94 }
95 
scpi_wait_ready(void)96 int scpi_wait_ready(void)
97 {
98 	/* Get a message from the SCP */
99 	scpi_secure_message_start();
100 	size_t size;
101 	unsigned command = scpi_secure_message_receive(NULL, &size);
102 	scpi_secure_message_end();
103 
104 	/* We are expecting 'SCP Ready', produce correct error if it's not */
105 	scpi_status_t response = SCP_OK;
106 	if (command != SCPI_CMD_SCP_READY)
107 		response = SCP_E_SUPPORT;
108 	else if (size != 0)
109 		response = SCP_E_SIZE;
110 
111 	/* Send our response back to SCP */
112 	scpi_secure_send32(command, response);
113 
114 	return response == SCP_OK ? 0 : -1;
115 }
116 
scpi_set_css_power_state(unsigned mpidr,scpi_power_state_t cpu_state,scpi_power_state_t cluster_state,scpi_power_state_t css_state)117 void scpi_set_css_power_state(unsigned mpidr, scpi_power_state_t cpu_state,
118 		scpi_power_state_t cluster_state, scpi_power_state_t css_state)
119 {
120 	uint32_t state = mpidr & 0x0f;	/* CPU ID */
121 	state |= (mpidr & 0xf00) >> 4;	/* Cluster ID */
122 	state |= cpu_state << 8;
123 	state |= cluster_state << 12;
124 	state |= css_state << 16;
125 	scpi_secure_send32(SCPI_CMD_SET_CSS_POWER_STATE, state);
126 }
127 
scpi_sys_power_state(scpi_system_state_t system_state)128 uint32_t scpi_sys_power_state(scpi_system_state_t system_state)
129 {
130 	uint32_t *response;
131 	size_t size;
132 	uint8_t state = system_state & 0xff;
133 
134 	/* Send the command */
135 	*(__typeof__(state) *)scpi_secure_message_start() = state;
136 	scpi_secure_message_send(SCPI_CMD_SYS_POWER_STATE, sizeof(state));
137 	scpi_secure_message_receive((void *)&response, &size);
138 	scpi_secure_message_end();
139 	return *response;
140 }
141