xref: /arm-trusted-firmware/plat/brcm/common/brcm_scpi.c (revision 91f16700b400a8c0651d24a598fc48ee2997a0d7)
1*91f16700Schasinglulu /*
2*91f16700Schasinglulu  * Copyright (c) 2019-2020, ARM Limited and Contributors. All rights reserved.
3*91f16700Schasinglulu  *
4*91f16700Schasinglulu  * SPDX-License-Identifier: BSD-3-Clause
5*91f16700Schasinglulu  */
6*91f16700Schasinglulu 
7*91f16700Schasinglulu #include <assert.h>
8*91f16700Schasinglulu #include <string.h>
9*91f16700Schasinglulu 
10*91f16700Schasinglulu #include <arch_helpers.h>
11*91f16700Schasinglulu #include <common/debug.h>
12*91f16700Schasinglulu #include <lib/utils.h>
13*91f16700Schasinglulu #include <plat/common/platform.h>
14*91f16700Schasinglulu 
15*91f16700Schasinglulu #include <brcm_mhu.h>
16*91f16700Schasinglulu #include <brcm_scpi.h>
17*91f16700Schasinglulu #include <platform_def.h>
18*91f16700Schasinglulu 
19*91f16700Schasinglulu #define SCPI_SHARED_MEM_SCP_TO_AP	(PLAT_SCP_COM_SHARED_MEM_BASE)
20*91f16700Schasinglulu #define SCPI_SHARED_MEM_AP_TO_SCP	(PLAT_SCP_COM_SHARED_MEM_BASE \
21*91f16700Schasinglulu 								 + 0x100)
22*91f16700Schasinglulu 
23*91f16700Schasinglulu /* Header and payload addresses for commands from AP to SCP */
24*91f16700Schasinglulu #define SCPI_CMD_HEADER_AP_TO_SCP		\
25*91f16700Schasinglulu 	((scpi_cmd_t *) SCPI_SHARED_MEM_AP_TO_SCP)
26*91f16700Schasinglulu #define SCPI_CMD_PAYLOAD_AP_TO_SCP		\
27*91f16700Schasinglulu 	((void *) (SCPI_SHARED_MEM_AP_TO_SCP + sizeof(scpi_cmd_t)))
28*91f16700Schasinglulu 
29*91f16700Schasinglulu /* Header and payload addresses for responses from SCP to AP */
30*91f16700Schasinglulu #define SCPI_RES_HEADER_SCP_TO_AP \
31*91f16700Schasinglulu 	((scpi_cmd_t *) SCPI_SHARED_MEM_SCP_TO_AP)
32*91f16700Schasinglulu #define SCPI_RES_PAYLOAD_SCP_TO_AP \
33*91f16700Schasinglulu 	((void *) (SCPI_SHARED_MEM_SCP_TO_AP + sizeof(scpi_cmd_t)))
34*91f16700Schasinglulu 
35*91f16700Schasinglulu /* ID of the MHU slot used for the SCPI protocol */
36*91f16700Schasinglulu #define SCPI_MHU_SLOT_ID		0
37*91f16700Schasinglulu 
38*91f16700Schasinglulu static void scpi_secure_message_start(void)
39*91f16700Schasinglulu {
40*91f16700Schasinglulu 	mhu_secure_message_start(SCPI_MHU_SLOT_ID);
41*91f16700Schasinglulu }
42*91f16700Schasinglulu 
43*91f16700Schasinglulu static void scpi_secure_message_send(size_t payload_size)
44*91f16700Schasinglulu {
45*91f16700Schasinglulu 	/*
46*91f16700Schasinglulu 	 * Ensure that any write to the SCPI payload area is seen by SCP before
47*91f16700Schasinglulu 	 * we write to the MHU register. If these 2 writes were reordered by
48*91f16700Schasinglulu 	 * the CPU then SCP would read stale payload data
49*91f16700Schasinglulu 	 */
50*91f16700Schasinglulu 	dmbst();
51*91f16700Schasinglulu 
52*91f16700Schasinglulu 	mhu_secure_message_send(SCPI_MHU_SLOT_ID);
53*91f16700Schasinglulu }
54*91f16700Schasinglulu 
55*91f16700Schasinglulu static void scpi_secure_message_receive(scpi_cmd_t *cmd)
56*91f16700Schasinglulu {
57*91f16700Schasinglulu 	uint32_t mhu_status;
58*91f16700Schasinglulu 
59*91f16700Schasinglulu 	assert(cmd != NULL);
60*91f16700Schasinglulu 
61*91f16700Schasinglulu 	mhu_status = mhu_secure_message_wait();
62*91f16700Schasinglulu 
63*91f16700Schasinglulu 	/* Expect an SCPI message, reject any other protocol */
64*91f16700Schasinglulu 	if (mhu_status != (1 << SCPI_MHU_SLOT_ID)) {
65*91f16700Schasinglulu 		ERROR("MHU: Unexpected protocol (MHU status: 0x%x)\n",
66*91f16700Schasinglulu 		      mhu_status);
67*91f16700Schasinglulu 		panic();
68*91f16700Schasinglulu 	}
69*91f16700Schasinglulu 
70*91f16700Schasinglulu 	/*
71*91f16700Schasinglulu 	 * Ensure that any read to the SCPI payload area is done after reading
72*91f16700Schasinglulu 	 * the MHU register. If these 2 reads were reordered then the CPU would
73*91f16700Schasinglulu 	 * read invalid payload data
74*91f16700Schasinglulu 	 */
75*91f16700Schasinglulu 	dmbld();
76*91f16700Schasinglulu 
77*91f16700Schasinglulu 	memcpy(cmd, (void *) SCPI_SHARED_MEM_SCP_TO_AP, sizeof(*cmd));
78*91f16700Schasinglulu }
79*91f16700Schasinglulu 
80*91f16700Schasinglulu static void scpi_secure_message_end(void)
81*91f16700Schasinglulu {
82*91f16700Schasinglulu 	mhu_secure_message_end(SCPI_MHU_SLOT_ID);
83*91f16700Schasinglulu }
84*91f16700Schasinglulu 
85*91f16700Schasinglulu int scpi_wait_ready(void)
86*91f16700Schasinglulu {
87*91f16700Schasinglulu 	scpi_cmd_t scpi_cmd;
88*91f16700Schasinglulu 
89*91f16700Schasinglulu 	VERBOSE("Waiting for SCP_READY command...\n");
90*91f16700Schasinglulu 
91*91f16700Schasinglulu 	/* Get a message from the SCP */
92*91f16700Schasinglulu 	scpi_secure_message_start();
93*91f16700Schasinglulu 	scpi_secure_message_receive(&scpi_cmd);
94*91f16700Schasinglulu 	scpi_secure_message_end();
95*91f16700Schasinglulu 
96*91f16700Schasinglulu 	/* We are expecting 'SCP Ready', produce correct error if it's not */
97*91f16700Schasinglulu 	scpi_status_t status = SCP_OK;
98*91f16700Schasinglulu 
99*91f16700Schasinglulu 	if (scpi_cmd.id != SCPI_CMD_SCP_READY) {
100*91f16700Schasinglulu 		ERROR("Unexpected SCP command: expected #%u, received #%u\n",
101*91f16700Schasinglulu 		      SCPI_CMD_SCP_READY, scpi_cmd.id);
102*91f16700Schasinglulu 		status = SCP_E_SUPPORT;
103*91f16700Schasinglulu 	} else if (scpi_cmd.size != 0) {
104*91f16700Schasinglulu 		ERROR("SCP_READY cmd has incorrect size: expected 0, got %u\n",
105*91f16700Schasinglulu 		      scpi_cmd.size);
106*91f16700Schasinglulu 		status = SCP_E_SIZE;
107*91f16700Schasinglulu 	}
108*91f16700Schasinglulu 
109*91f16700Schasinglulu 	VERBOSE("Sending response for SCP_READY command\n");
110*91f16700Schasinglulu 
111*91f16700Schasinglulu 	/*
112*91f16700Schasinglulu 	 * Send our response back to SCP.
113*91f16700Schasinglulu 	 * We are using the same SCPI header, just update the status field.
114*91f16700Schasinglulu 	 */
115*91f16700Schasinglulu 	scpi_cmd.status = status;
116*91f16700Schasinglulu 	scpi_secure_message_start();
117*91f16700Schasinglulu 	memcpy((void *) SCPI_SHARED_MEM_AP_TO_SCP, &scpi_cmd, sizeof(scpi_cmd));
118*91f16700Schasinglulu 	scpi_secure_message_send(0);
119*91f16700Schasinglulu 	scpi_secure_message_end();
120*91f16700Schasinglulu 
121*91f16700Schasinglulu 	return status == SCP_OK ? 0 : -1;
122*91f16700Schasinglulu }
123*91f16700Schasinglulu 
124*91f16700Schasinglulu void scpi_set_brcm_power_state(unsigned int mpidr,
125*91f16700Schasinglulu 		scpi_power_state_t cpu_state, scpi_power_state_t cluster_state,
126*91f16700Schasinglulu 		scpi_power_state_t brcm_state)
127*91f16700Schasinglulu {
128*91f16700Schasinglulu 	scpi_cmd_t *cmd;
129*91f16700Schasinglulu 	uint32_t state = 0;
130*91f16700Schasinglulu 	uint32_t *payload_addr;
131*91f16700Schasinglulu 
132*91f16700Schasinglulu #if ARM_PLAT_MT
133*91f16700Schasinglulu 	/*
134*91f16700Schasinglulu 	 * The current SCPI driver only caters for single-threaded platforms.
135*91f16700Schasinglulu 	 * Hence we ignore the thread ID (which is always 0) for such platforms.
136*91f16700Schasinglulu 	 */
137*91f16700Schasinglulu 	state |= (mpidr >> MPIDR_AFF1_SHIFT) & 0x0f;	/* CPU ID */
138*91f16700Schasinglulu 	state |= ((mpidr >> MPIDR_AFF2_SHIFT) & 0x0f) << 4;	/* Cluster ID */
139*91f16700Schasinglulu #else
140*91f16700Schasinglulu 	state |= mpidr & 0x0f;	/* CPU ID */
141*91f16700Schasinglulu 	state |= (mpidr & 0xf00) >> 4;	/* Cluster ID */
142*91f16700Schasinglulu #endif /* ARM_PLAT_MT */
143*91f16700Schasinglulu 
144*91f16700Schasinglulu 	state |= cpu_state << 8;
145*91f16700Schasinglulu 	state |= cluster_state << 12;
146*91f16700Schasinglulu 	state |= brcm_state << 16;
147*91f16700Schasinglulu 
148*91f16700Schasinglulu 	scpi_secure_message_start();
149*91f16700Schasinglulu 
150*91f16700Schasinglulu 	/* Populate the command header */
151*91f16700Schasinglulu 	cmd = SCPI_CMD_HEADER_AP_TO_SCP;
152*91f16700Schasinglulu 	cmd->id = SCPI_CMD_SET_POWER_STATE;
153*91f16700Schasinglulu 	cmd->set = SCPI_SET_NORMAL;
154*91f16700Schasinglulu 	cmd->sender = 0;
155*91f16700Schasinglulu 	cmd->size = sizeof(state);
156*91f16700Schasinglulu 	/* Populate the command payload */
157*91f16700Schasinglulu 	payload_addr = SCPI_CMD_PAYLOAD_AP_TO_SCP;
158*91f16700Schasinglulu 	*payload_addr = state;
159*91f16700Schasinglulu 	scpi_secure_message_send(sizeof(state));
160*91f16700Schasinglulu 
161*91f16700Schasinglulu 	/*
162*91f16700Schasinglulu 	 * SCP does not reply to this command in order to avoid MHU interrupts
163*91f16700Schasinglulu 	 * from the sender, which could interfere with its power state request.
164*91f16700Schasinglulu 	 */
165*91f16700Schasinglulu 	scpi_secure_message_end();
166*91f16700Schasinglulu }
167*91f16700Schasinglulu 
168*91f16700Schasinglulu /*
169*91f16700Schasinglulu  * Query and obtain power state from SCP.
170*91f16700Schasinglulu  *
171*91f16700Schasinglulu  * In response to the query, SCP returns power states of all CPUs in all
172*91f16700Schasinglulu  * clusters of the system. The returned response is then filtered based on the
173*91f16700Schasinglulu  * supplied MPIDR. Power states of requested cluster and CPUs within are updated
174*91f16700Schasinglulu  * via. supplied non-NULL pointer arguments.
175*91f16700Schasinglulu  *
176*91f16700Schasinglulu  * Returns 0 on success, or -1 on errors.
177*91f16700Schasinglulu  */
178*91f16700Schasinglulu int scpi_get_brcm_power_state(unsigned int mpidr, unsigned int *cpu_state_p,
179*91f16700Schasinglulu 		unsigned int *cluster_state_p)
180*91f16700Schasinglulu {
181*91f16700Schasinglulu 	scpi_cmd_t *cmd;
182*91f16700Schasinglulu 	scpi_cmd_t response;
183*91f16700Schasinglulu 	int power_state, cpu, cluster, rc = -1;
184*91f16700Schasinglulu 
185*91f16700Schasinglulu 	/*
186*91f16700Schasinglulu 	 * Extract CPU and cluster membership of the given MPIDR. SCPI caters
187*91f16700Schasinglulu 	 * for only up to 0xf clusters, and 8 CPUs per cluster
188*91f16700Schasinglulu 	 */
189*91f16700Schasinglulu 	cpu = mpidr & MPIDR_AFFLVL_MASK;
190*91f16700Schasinglulu 	cluster = (mpidr >> MPIDR_AFF1_SHIFT) & MPIDR_AFFLVL_MASK;
191*91f16700Schasinglulu 	if (cpu >= 8 || cluster >= 0xf)
192*91f16700Schasinglulu 		return -1;
193*91f16700Schasinglulu 
194*91f16700Schasinglulu 	scpi_secure_message_start();
195*91f16700Schasinglulu 
196*91f16700Schasinglulu 	/* Populate request headers */
197*91f16700Schasinglulu 	zeromem(SCPI_CMD_HEADER_AP_TO_SCP, sizeof(*cmd));
198*91f16700Schasinglulu 	cmd = SCPI_CMD_HEADER_AP_TO_SCP;
199*91f16700Schasinglulu 	cmd->id = SCPI_CMD_GET_POWER_STATE;
200*91f16700Schasinglulu 
201*91f16700Schasinglulu 	/*
202*91f16700Schasinglulu 	 * Send message and wait for SCP's response
203*91f16700Schasinglulu 	 */
204*91f16700Schasinglulu 	scpi_secure_message_send(0);
205*91f16700Schasinglulu 	scpi_secure_message_receive(&response);
206*91f16700Schasinglulu 
207*91f16700Schasinglulu 	if (response.status != SCP_OK)
208*91f16700Schasinglulu 		goto exit;
209*91f16700Schasinglulu 
210*91f16700Schasinglulu 	/* Validate SCP response */
211*91f16700Schasinglulu 	if (!CHECK_RESPONSE(response, cluster))
212*91f16700Schasinglulu 		goto exit;
213*91f16700Schasinglulu 
214*91f16700Schasinglulu 	/* Extract power states for required cluster */
215*91f16700Schasinglulu 	power_state = *(((uint16_t *) SCPI_RES_PAYLOAD_SCP_TO_AP) + cluster);
216*91f16700Schasinglulu 	if (CLUSTER_ID(power_state) != cluster)
217*91f16700Schasinglulu 		goto exit;
218*91f16700Schasinglulu 
219*91f16700Schasinglulu 	/* Update power state via. pointers */
220*91f16700Schasinglulu 	if (cluster_state_p)
221*91f16700Schasinglulu 		*cluster_state_p = CLUSTER_POWER_STATE(power_state);
222*91f16700Schasinglulu 	if (cpu_state_p)
223*91f16700Schasinglulu 		*cpu_state_p = CPU_POWER_STATE(power_state);
224*91f16700Schasinglulu 	rc = 0;
225*91f16700Schasinglulu 
226*91f16700Schasinglulu exit:
227*91f16700Schasinglulu 	scpi_secure_message_end();
228*91f16700Schasinglulu 	return rc;
229*91f16700Schasinglulu }
230*91f16700Schasinglulu 
231*91f16700Schasinglulu uint32_t scpi_sys_power_state(scpi_system_state_t system_state)
232*91f16700Schasinglulu {
233*91f16700Schasinglulu 	scpi_cmd_t *cmd;
234*91f16700Schasinglulu 	uint8_t *payload_addr;
235*91f16700Schasinglulu 
236*91f16700Schasinglulu 	scpi_secure_message_start();
237*91f16700Schasinglulu 
238*91f16700Schasinglulu 	/* Populate the command header */
239*91f16700Schasinglulu 	cmd = SCPI_CMD_HEADER_AP_TO_SCP;
240*91f16700Schasinglulu 	cmd->id = SCPI_CMD_SYS_POWER_STATE;
241*91f16700Schasinglulu 	cmd->set = 0;
242*91f16700Schasinglulu 	cmd->sender = 0;
243*91f16700Schasinglulu 	cmd->size = sizeof(*payload_addr);
244*91f16700Schasinglulu 	/* Populate the command payload */
245*91f16700Schasinglulu 	payload_addr = SCPI_CMD_PAYLOAD_AP_TO_SCP;
246*91f16700Schasinglulu 	*payload_addr = system_state & 0xff;
247*91f16700Schasinglulu 	scpi_secure_message_send(sizeof(*payload_addr));
248*91f16700Schasinglulu 
249*91f16700Schasinglulu 	scpi_secure_message_end();
250*91f16700Schasinglulu 
251*91f16700Schasinglulu 	return SCP_OK;
252*91f16700Schasinglulu }
253