blob: e4a44fe64776d2df5dfe3002f13f5f0a4f94bc9c [file] [log] [blame]
Dan Handley9df48042015-03-19 18:58:55 +00001/*
Soby Mathew73b7bf92017-05-03 12:58:41 +01002 * Copyright (c) 2014-2017, ARM Limited and Contributors. All rights reserved.
Dan Handley9df48042015-03-19 18:58:55 +00003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Dan Handley9df48042015-03-19 18:58:55 +00005 */
6
7#include <arch_helpers.h>
Sandrine Bailleux04b66d82015-03-18 14:52:53 +00008#include <assert.h>
Dan Handley9df48042015-03-19 18:58:55 +00009#include <css_def.h>
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000010#include <debug.h>
Dan Handley9df48042015-03-19 18:58:55 +000011#include <platform.h>
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000012#include <stdint.h>
Soby Mathew73b7bf92017-05-03 12:58:41 +010013#include "../scpi/css_mhu.h"
Dan Handley9df48042015-03-19 18:58:55 +000014
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000015/* ID of the MHU slot used for the BOM protocol */
16#define BOM_MHU_SLOT_ID 0
Dan Handley9df48042015-03-19 18:58:55 +000017
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000018/* Boot commands sent from AP -> SCP */
19#define BOOT_CMD_INFO 0x00
20#define BOOT_CMD_DATA 0x01
Dan Handley9df48042015-03-19 18:58:55 +000021
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000022/* BOM command header */
Dan Handley9df48042015-03-19 18:58:55 +000023typedef struct {
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000024 uint32_t id : 8;
25 uint32_t reserved : 24;
26} bom_cmd_t;
Dan Handley9df48042015-03-19 18:58:55 +000027
28typedef struct {
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000029 uint32_t image_size;
30 uint32_t checksum;
31} cmd_info_payload_t;
Dan Handley9df48042015-03-19 18:58:55 +000032
33/*
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000034 * Unlike the SCPI protocol, the boot protocol uses the same memory region
Dan Handley9df48042015-03-19 18:58:55 +000035 * for both AP -> SCP and SCP -> AP transfers; define the address of this...
36 */
Vikram Kanigiri72084192016-02-08 16:29:30 +000037#define BOM_SHARED_MEM PLAT_CSS_SCP_COM_SHARED_MEM_BASE
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000038#define BOM_CMD_HEADER ((bom_cmd_t *) BOM_SHARED_MEM)
39#define BOM_CMD_PAYLOAD ((void *) (BOM_SHARED_MEM + sizeof(bom_cmd_t)))
Dan Handley9df48042015-03-19 18:58:55 +000040
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000041typedef struct {
42 /* Offset from the base address of the Trusted RAM */
43 uint32_t offset;
44 uint32_t block_size;
45} cmd_data_payload_t;
Dan Handley9df48042015-03-19 18:58:55 +000046
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000047static void scp_boot_message_start(void)
48{
49 mhu_secure_message_start(BOM_MHU_SLOT_ID);
Dan Handley9df48042015-03-19 18:58:55 +000050}
51
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000052static void scp_boot_message_send(size_t payload_size)
Dan Handley9df48042015-03-19 18:58:55 +000053{
Juan Castillo2e86cb12016-01-13 15:01:09 +000054 /* Ensure that any write to the BOM payload area is seen by SCP before
55 * we write to the MHU register. If these 2 writes were reordered by
56 * the CPU then SCP would read stale payload data */
57 dmbst();
Dan Handley9df48042015-03-19 18:58:55 +000058
59 /* Send command to SCP */
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000060 mhu_secure_message_send(BOM_MHU_SLOT_ID);
Dan Handley9df48042015-03-19 18:58:55 +000061}
62
63static uint32_t scp_boot_message_wait(size_t size)
64{
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000065 uint32_t mhu_status;
66
67 mhu_status = mhu_secure_message_wait();
68
69 /* Expect an SCP Boot Protocol message, reject any other protocol */
70 if (mhu_status != (1 << BOM_MHU_SLOT_ID)) {
71 ERROR("MHU: Unexpected protocol (MHU status: 0x%x)\n",
72 mhu_status);
73 panic();
74 }
Dan Handley9df48042015-03-19 18:58:55 +000075
Juan Castillo2e86cb12016-01-13 15:01:09 +000076 /* Ensure that any read to the BOM payload area is done after reading
77 * the MHU register. If these 2 reads were reordered then the CPU would
78 * read invalid payload data */
79 dmbld();
Dan Handley9df48042015-03-19 18:58:55 +000080
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000081 return *(uint32_t *) BOM_SHARED_MEM;
Dan Handley9df48042015-03-19 18:58:55 +000082}
83
84static void scp_boot_message_end(void)
85{
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000086 mhu_secure_message_end(BOM_MHU_SLOT_ID);
Dan Handley9df48042015-03-19 18:58:55 +000087}
88
Soby Mathew73b7bf92017-05-03 12:58:41 +010089int css_scp_boot_image_xfer(void *image, unsigned int image_size)
Dan Handley9df48042015-03-19 18:58:55 +000090{
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000091 uint32_t response;
92 uint32_t checksum;
93 cmd_info_payload_t *cmd_info_payload;
94 cmd_data_payload_t *cmd_data_payload;
Dan Handley9df48042015-03-19 18:58:55 +000095
Juan Castilloa72b6472015-12-10 15:49:17 +000096 assert((uintptr_t) image == SCP_BL2_BASE);
Dan Handley9df48042015-03-19 18:58:55 +000097
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000098 if ((image_size == 0) || (image_size % 4 != 0)) {
Juan Castilloa72b6472015-12-10 15:49:17 +000099 ERROR("Invalid size for the SCP_BL2 image. Must be a multiple of "
Sandrine Bailleux04b66d82015-03-18 14:52:53 +0000100 "4 bytes and not zero (current size = 0x%x)\n",
101 image_size);
102 return -1;
103 }
Dan Handley9df48042015-03-19 18:58:55 +0000104
Sandrine Bailleux04b66d82015-03-18 14:52:53 +0000105 /* Extract the checksum from the image */
106 checksum = *(uint32_t *) image;
107 image = (char *) image + sizeof(checksum);
108 image_size -= sizeof(checksum);
Dan Handley9df48042015-03-19 18:58:55 +0000109
Sandrine Bailleux04b66d82015-03-18 14:52:53 +0000110 mhu_secure_init();
Dan Handley9df48042015-03-19 18:58:55 +0000111
Juan Castilloa72b6472015-12-10 15:49:17 +0000112 VERBOSE("Send info about the SCP_BL2 image to be transferred to SCP\n");
Dan Handley9df48042015-03-19 18:58:55 +0000113
Sandrine Bailleux04b66d82015-03-18 14:52:53 +0000114 /*
115 * Send information about the SCP firmware image about to be transferred
116 * to SCP
117 */
118 scp_boot_message_start();
119
120 BOM_CMD_HEADER->id = BOOT_CMD_INFO;
121 cmd_info_payload = BOM_CMD_PAYLOAD;
122 cmd_info_payload->image_size = image_size;
123 cmd_info_payload->checksum = checksum;
124
125 scp_boot_message_send(sizeof(*cmd_info_payload));
Sandrine Bailleux7da652d2015-04-13 11:47:48 +0100126#if CSS_DETECT_PRE_1_7_0_SCP
127 {
128 const uint32_t deprecated_scp_nack_cmd = 0x404;
129 uint32_t mhu_status;
130
131 VERBOSE("Detecting SCP version incompatibility\n");
132
133 mhu_status = mhu_secure_message_wait();
134 if (mhu_status == deprecated_scp_nack_cmd) {
135 ERROR("Detected an incompatible version of the SCP firmware.\n");
136 ERROR("Only versions from v1.7.0 onwards are supported.\n");
137 ERROR("Please update the SCP firmware.\n");
138 return -1;
139 }
140
141 VERBOSE("SCP version looks OK\n");
142 }
143#endif /* CSS_DETECT_PRE_1_7_0_SCP */
Sandrine Bailleux04b66d82015-03-18 14:52:53 +0000144 response = scp_boot_message_wait(sizeof(response));
145 scp_boot_message_end();
Dan Handley9df48042015-03-19 18:58:55 +0000146
Sandrine Bailleux04b66d82015-03-18 14:52:53 +0000147 if (response != 0) {
148 ERROR("SCP BOOT_CMD_INFO returned error %u\n", response);
149 return -1;
150 }
151
Juan Castilloa72b6472015-12-10 15:49:17 +0000152 VERBOSE("Transferring SCP_BL2 image to SCP\n");
Dan Handley9df48042015-03-19 18:58:55 +0000153
Juan Castilloa72b6472015-12-10 15:49:17 +0000154 /* Transfer SCP_BL2 image to SCP */
Sandrine Bailleux04b66d82015-03-18 14:52:53 +0000155 scp_boot_message_start();
Dan Handley9df48042015-03-19 18:58:55 +0000156
Sandrine Bailleux04b66d82015-03-18 14:52:53 +0000157 BOM_CMD_HEADER->id = BOOT_CMD_DATA;
158 cmd_data_payload = BOM_CMD_PAYLOAD;
Sandrine Bailleux47ea1bc2015-06-09 11:53:33 +0100159 cmd_data_payload->offset = (uintptr_t) image - ARM_TRUSTED_SRAM_BASE;
Sandrine Bailleux04b66d82015-03-18 14:52:53 +0000160 cmd_data_payload->block_size = image_size;
Dan Handley9df48042015-03-19 18:58:55 +0000161
Sandrine Bailleux04b66d82015-03-18 14:52:53 +0000162 scp_boot_message_send(sizeof(*cmd_data_payload));
163 response = scp_boot_message_wait(sizeof(response));
164 scp_boot_message_end();
Dan Handley9df48042015-03-19 18:58:55 +0000165
Sandrine Bailleux04b66d82015-03-18 14:52:53 +0000166 if (response != 0) {
167 ERROR("SCP BOOT_CMD_DATA returned error %u\n", response);
168 return -1;
Dan Handley9df48042015-03-19 18:58:55 +0000169 }
170
Soby Mathew73b7bf92017-05-03 12:58:41 +0100171 return 0;
Dan Handley9df48042015-03-19 18:58:55 +0000172}