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 <bakery_lock.h>
33 #include <mmio.h>
34 #include "juno_def.h"
35 #include "juno_private.h"
36 #include "mhu.h"
37 
38 /* SCP MHU secure channel registers */
39 #define SCP_INTR_S_STAT		0x200
40 #define SCP_INTR_S_SET		0x208
41 #define SCP_INTR_S_CLEAR	0x210
42 
43 /* CPU MHU secure channel registers */
44 #define CPU_INTR_S_STAT		0x300
45 #define CPU_INTR_S_SET		0x308
46 #define CPU_INTR_S_CLEAR	0x310
47 
48 #if IMAGE_BL31
49 #if USE_COHERENT_MEM
50 static bakery_lock_t mhu_secure_lock __attribute__ ((section("tzfw_coherent_mem")));
51 #define LOCK_ARG		&mhu_secure_lock
52 #else
53 #define LOCK_ARG		JUNO_MHU_BAKERY_ID
54 #endif /*__USE_COHERENT_MEM__ */
55 #else
56 #define LOCK_ARG	/* Locks required only for BL3-1 images */
57 #endif /* __IMAGE_BL31__ */
58 
mhu_secure_message_start(void)59 void mhu_secure_message_start(void)
60 {
61 	juno_lock_get(LOCK_ARG);
62 
63 	/* Make sure any previous command has finished */
64 	while (mmio_read_32(MHU_BASE + CPU_INTR_S_STAT) != 0)
65 		;
66 }
67 
mhu_secure_message_send(uint32_t command)68 void mhu_secure_message_send(uint32_t command)
69 {
70 	/* Send command to SCP and wait for it to pick it up */
71 	mmio_write_32(MHU_BASE + CPU_INTR_S_SET, command);
72 	while (mmio_read_32(MHU_BASE + CPU_INTR_S_STAT) != 0)
73 		;
74 }
75 
mhu_secure_message_wait(void)76 uint32_t mhu_secure_message_wait(void)
77 {
78 	/* Wait for response from SCP */
79 	uint32_t response;
80 	while (!(response = mmio_read_32(MHU_BASE + SCP_INTR_S_STAT)))
81 		;
82 
83 	return response;
84 }
85 
mhu_secure_message_end(void)86 void mhu_secure_message_end(void)
87 {
88 	/* Clear any response we got by writing all ones to the CLEAR register */
89 	mmio_write_32(MHU_BASE + SCP_INTR_S_CLEAR, 0xffffffffu);
90 
91 	juno_lock_release(LOCK_ARG);
92 }
93 
mhu_secure_init(void)94 void mhu_secure_init(void)
95 {
96 	juno_lock_init(LOCK_ARG);
97 
98 	/*
99 	 * Clear the CPU's INTR register to make sure we don't see a stale
100 	 * or garbage value and think it's a message we've already sent.
101 	 */
102 	mmio_write_32(MHU_BASE + CPU_INTR_S_CLEAR, 0xffffffffu);
103 }
104