Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 1 | /* |
Roberto Vargas | 2b36b15 | 2018-02-12 12:36:17 +0000 | [diff] [blame] | 2 | * Copyright (c) 2014-2018, ARM Limited and Contributors. All rights reserved. |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 7 | #include <assert.h> |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 8 | #include <stdint.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 9 | |
| 10 | #include <arch_helpers.h> |
| 11 | #include <common/debug.h> |
| 12 | #include <plat/common/platform.h> |
| 13 | |
| 14 | #include <css_def.h> |
| 15 | |
Samarth Parikh | 59cfa13 | 2017-11-23 14:23:21 +0530 | [diff] [blame] | 16 | #include "../mhu/css_mhu.h" |
Soby Mathew | 1ced6b8 | 2017-06-12 12:37:10 +0100 | [diff] [blame] | 17 | #include "../scpi/css_scpi.h" |
Roberto Vargas | 2b36b15 | 2018-02-12 12:36:17 +0000 | [diff] [blame] | 18 | #include "css_scp.h" |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 19 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 20 | /* ID of the MHU slot used for the BOM protocol */ |
| 21 | #define BOM_MHU_SLOT_ID 0 |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 22 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 23 | /* Boot commands sent from AP -> SCP */ |
| 24 | #define BOOT_CMD_INFO 0x00 |
| 25 | #define BOOT_CMD_DATA 0x01 |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 26 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 27 | /* BOM command header */ |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 28 | typedef struct { |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 29 | uint32_t id : 8; |
| 30 | uint32_t reserved : 24; |
| 31 | } bom_cmd_t; |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 32 | |
| 33 | typedef struct { |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 34 | uint32_t image_size; |
| 35 | uint32_t checksum; |
| 36 | } cmd_info_payload_t; |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 37 | |
| 38 | /* |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 39 | * Unlike the SCPI protocol, the boot protocol uses the same memory region |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 40 | * for both AP -> SCP and SCP -> AP transfers; define the address of this... |
| 41 | */ |
Vikram Kanigiri | 7208419 | 2016-02-08 16:29:30 +0000 | [diff] [blame] | 42 | #define BOM_SHARED_MEM PLAT_CSS_SCP_COM_SHARED_MEM_BASE |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 43 | #define BOM_CMD_HEADER ((bom_cmd_t *) BOM_SHARED_MEM) |
| 44 | #define BOM_CMD_PAYLOAD ((void *) (BOM_SHARED_MEM + sizeof(bom_cmd_t))) |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 45 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 46 | typedef struct { |
| 47 | /* Offset from the base address of the Trusted RAM */ |
| 48 | uint32_t offset; |
| 49 | uint32_t block_size; |
| 50 | } cmd_data_payload_t; |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 51 | |
Soby Mathew | 2f6cac4 | 2017-06-13 18:00:53 +0100 | [diff] [blame] | 52 | /* |
Soby Mathew | af14b46 | 2018-06-01 16:53:38 +0100 | [diff] [blame] | 53 | * All CSS platforms load SCP_BL2/SCP_BL2U just below BL2 (this is where BL31 |
| 54 | * usually resides except when ARM_BL31_IN_DRAM is |
| 55 | * set). Ensure that SCP_BL2/SCP_BL2U do not overflow into shared RAM and |
| 56 | * the tb_fw_config. |
Soby Mathew | 2f6cac4 | 2017-06-13 18:00:53 +0100 | [diff] [blame] | 57 | */ |
Soby Mathew | af14b46 | 2018-06-01 16:53:38 +0100 | [diff] [blame] | 58 | CASSERT(SCP_BL2_LIMIT <= BL2_BASE, assert_scp_bl2_overwrite_bl2); |
| 59 | CASSERT(SCP_BL2U_LIMIT <= BL2_BASE, assert_scp_bl2u_overwrite_bl2); |
Soby Mathew | 2f6cac4 | 2017-06-13 18:00:53 +0100 | [diff] [blame] | 60 | |
Soby Mathew | af14b46 | 2018-06-01 16:53:38 +0100 | [diff] [blame] | 61 | CASSERT(SCP_BL2_BASE >= ARM_TB_FW_CONFIG_LIMIT, assert_scp_bl2_overflow); |
| 62 | CASSERT(SCP_BL2U_BASE >= ARM_TB_FW_CONFIG_LIMIT, assert_scp_bl2u_overflow); |
Soby Mathew | 2f6cac4 | 2017-06-13 18:00:53 +0100 | [diff] [blame] | 63 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 64 | static void scp_boot_message_start(void) |
| 65 | { |
| 66 | mhu_secure_message_start(BOM_MHU_SLOT_ID); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 69 | static void scp_boot_message_send(size_t payload_size) |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 70 | { |
Juan Castillo | 2e86cb1 | 2016-01-13 15:01:09 +0000 | [diff] [blame] | 71 | /* Ensure that any write to the BOM payload area is seen by SCP before |
| 72 | * we write to the MHU register. If these 2 writes were reordered by |
| 73 | * the CPU then SCP would read stale payload data */ |
| 74 | dmbst(); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 75 | |
| 76 | /* Send command to SCP */ |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 77 | mhu_secure_message_send(BOM_MHU_SLOT_ID); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static uint32_t scp_boot_message_wait(size_t size) |
| 81 | { |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 82 | uint32_t mhu_status; |
| 83 | |
| 84 | mhu_status = mhu_secure_message_wait(); |
| 85 | |
| 86 | /* Expect an SCP Boot Protocol message, reject any other protocol */ |
| 87 | if (mhu_status != (1 << BOM_MHU_SLOT_ID)) { |
| 88 | ERROR("MHU: Unexpected protocol (MHU status: 0x%x)\n", |
| 89 | mhu_status); |
| 90 | panic(); |
| 91 | } |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 92 | |
Juan Castillo | 2e86cb1 | 2016-01-13 15:01:09 +0000 | [diff] [blame] | 93 | /* Ensure that any read to the BOM payload area is done after reading |
| 94 | * the MHU register. If these 2 reads were reordered then the CPU would |
| 95 | * read invalid payload data */ |
| 96 | dmbld(); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 97 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 98 | return *(uint32_t *) BOM_SHARED_MEM; |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | static void scp_boot_message_end(void) |
| 102 | { |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 103 | mhu_secure_message_end(BOM_MHU_SLOT_ID); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Soby Mathew | 73b7bf9 | 2017-05-03 12:58:41 +0100 | [diff] [blame] | 106 | int css_scp_boot_image_xfer(void *image, unsigned int image_size) |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 107 | { |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 108 | uint32_t response; |
| 109 | uint32_t checksum; |
| 110 | cmd_info_payload_t *cmd_info_payload; |
| 111 | cmd_data_payload_t *cmd_data_payload; |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 112 | |
Juan Castillo | a72b647 | 2015-12-10 15:49:17 +0000 | [diff] [blame] | 113 | assert((uintptr_t) image == SCP_BL2_BASE); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 114 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 115 | if ((image_size == 0) || (image_size % 4 != 0)) { |
Juan Castillo | a72b647 | 2015-12-10 15:49:17 +0000 | [diff] [blame] | 116 | ERROR("Invalid size for the SCP_BL2 image. Must be a multiple of " |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 117 | "4 bytes and not zero (current size = 0x%x)\n", |
| 118 | image_size); |
| 119 | return -1; |
| 120 | } |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 121 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 122 | /* Extract the checksum from the image */ |
| 123 | checksum = *(uint32_t *) image; |
| 124 | image = (char *) image + sizeof(checksum); |
| 125 | image_size -= sizeof(checksum); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 126 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 127 | mhu_secure_init(); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 128 | |
Juan Castillo | a72b647 | 2015-12-10 15:49:17 +0000 | [diff] [blame] | 129 | VERBOSE("Send info about the SCP_BL2 image to be transferred to SCP\n"); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 130 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 131 | /* |
| 132 | * Send information about the SCP firmware image about to be transferred |
| 133 | * to SCP |
| 134 | */ |
| 135 | scp_boot_message_start(); |
| 136 | |
| 137 | BOM_CMD_HEADER->id = BOOT_CMD_INFO; |
| 138 | cmd_info_payload = BOM_CMD_PAYLOAD; |
| 139 | cmd_info_payload->image_size = image_size; |
| 140 | cmd_info_payload->checksum = checksum; |
| 141 | |
| 142 | scp_boot_message_send(sizeof(*cmd_info_payload)); |
Sandrine Bailleux | 7da652d | 2015-04-13 11:47:48 +0100 | [diff] [blame] | 143 | #if CSS_DETECT_PRE_1_7_0_SCP |
| 144 | { |
| 145 | const uint32_t deprecated_scp_nack_cmd = 0x404; |
| 146 | uint32_t mhu_status; |
| 147 | |
| 148 | VERBOSE("Detecting SCP version incompatibility\n"); |
| 149 | |
| 150 | mhu_status = mhu_secure_message_wait(); |
| 151 | if (mhu_status == deprecated_scp_nack_cmd) { |
| 152 | ERROR("Detected an incompatible version of the SCP firmware.\n"); |
| 153 | ERROR("Only versions from v1.7.0 onwards are supported.\n"); |
| 154 | ERROR("Please update the SCP firmware.\n"); |
| 155 | return -1; |
| 156 | } |
| 157 | |
| 158 | VERBOSE("SCP version looks OK\n"); |
| 159 | } |
| 160 | #endif /* CSS_DETECT_PRE_1_7_0_SCP */ |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 161 | response = scp_boot_message_wait(sizeof(response)); |
| 162 | scp_boot_message_end(); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 163 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 164 | if (response != 0) { |
| 165 | ERROR("SCP BOOT_CMD_INFO returned error %u\n", response); |
| 166 | return -1; |
| 167 | } |
| 168 | |
Juan Castillo | a72b647 | 2015-12-10 15:49:17 +0000 | [diff] [blame] | 169 | VERBOSE("Transferring SCP_BL2 image to SCP\n"); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 170 | |
Juan Castillo | a72b647 | 2015-12-10 15:49:17 +0000 | [diff] [blame] | 171 | /* Transfer SCP_BL2 image to SCP */ |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 172 | scp_boot_message_start(); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 173 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 174 | BOM_CMD_HEADER->id = BOOT_CMD_DATA; |
| 175 | cmd_data_payload = BOM_CMD_PAYLOAD; |
Sandrine Bailleux | 47ea1bc | 2015-06-09 11:53:33 +0100 | [diff] [blame] | 176 | cmd_data_payload->offset = (uintptr_t) image - ARM_TRUSTED_SRAM_BASE; |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 177 | cmd_data_payload->block_size = image_size; |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 178 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 179 | scp_boot_message_send(sizeof(*cmd_data_payload)); |
| 180 | response = scp_boot_message_wait(sizeof(response)); |
| 181 | scp_boot_message_end(); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 182 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 183 | if (response != 0) { |
| 184 | ERROR("SCP BOOT_CMD_DATA returned error %u\n", response); |
| 185 | return -1; |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Soby Mathew | 73b7bf9 | 2017-05-03 12:58:41 +0100 | [diff] [blame] | 188 | return 0; |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 189 | } |
Soby Mathew | 1ced6b8 | 2017-06-12 12:37:10 +0100 | [diff] [blame] | 190 | |
| 191 | int css_scp_boot_ready(void) |
| 192 | { |
| 193 | VERBOSE("Waiting for SCP to signal it is ready to go on\n"); |
| 194 | |
| 195 | /* Wait for SCP to signal it's ready */ |
| 196 | return scpi_wait_ready(); |
| 197 | } |