Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 1 | /* |
Vikram Kanigiri | 7208419 | 2016-02-08 16:29:30 +0000 | [diff] [blame] | 2 | * Copyright (c) 2014-2016, 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 | |
| 7 | #include <arch_helpers.h> |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 8 | #include <assert.h> |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 9 | #include <css_def.h> |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 10 | #include <debug.h> |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 11 | #include <platform.h> |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 12 | #include <stdint.h> |
Soby Mathew | 200fffd | 2016-10-21 11:34:59 +0100 | [diff] [blame] | 13 | #include "../drivers/scpi/css_mhu.h" |
| 14 | #include "../drivers/scpi/css_scpi.h" |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 15 | #include "css_scp_bootloader.h" |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 16 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 17 | /* ID of the MHU slot used for the BOM protocol */ |
| 18 | #define BOM_MHU_SLOT_ID 0 |
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 | /* Boot commands sent from AP -> SCP */ |
| 21 | #define BOOT_CMD_INFO 0x00 |
| 22 | #define BOOT_CMD_DATA 0x01 |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 23 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 24 | /* BOM command header */ |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 25 | typedef struct { |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 26 | uint32_t id : 8; |
| 27 | uint32_t reserved : 24; |
| 28 | } bom_cmd_t; |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 29 | |
| 30 | typedef struct { |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 31 | uint32_t image_size; |
| 32 | uint32_t checksum; |
| 33 | } cmd_info_payload_t; |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 34 | |
| 35 | /* |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 36 | * Unlike the SCPI protocol, the boot protocol uses the same memory region |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 37 | * for both AP -> SCP and SCP -> AP transfers; define the address of this... |
| 38 | */ |
Vikram Kanigiri | 7208419 | 2016-02-08 16:29:30 +0000 | [diff] [blame] | 39 | #define BOM_SHARED_MEM PLAT_CSS_SCP_COM_SHARED_MEM_BASE |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 40 | #define BOM_CMD_HEADER ((bom_cmd_t *) BOM_SHARED_MEM) |
| 41 | #define BOM_CMD_PAYLOAD ((void *) (BOM_SHARED_MEM + sizeof(bom_cmd_t))) |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 42 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 43 | typedef struct { |
| 44 | /* Offset from the base address of the Trusted RAM */ |
| 45 | uint32_t offset; |
| 46 | uint32_t block_size; |
| 47 | } cmd_data_payload_t; |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 48 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 49 | static void scp_boot_message_start(void) |
| 50 | { |
| 51 | mhu_secure_message_start(BOM_MHU_SLOT_ID); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 54 | static void scp_boot_message_send(size_t payload_size) |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 55 | { |
Juan Castillo | 2e86cb1 | 2016-01-13 15:01:09 +0000 | [diff] [blame] | 56 | /* Ensure that any write to the BOM payload area is seen by SCP before |
| 57 | * we write to the MHU register. If these 2 writes were reordered by |
| 58 | * the CPU then SCP would read stale payload data */ |
| 59 | dmbst(); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 60 | |
| 61 | /* Send command to SCP */ |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 62 | mhu_secure_message_send(BOM_MHU_SLOT_ID); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | static uint32_t scp_boot_message_wait(size_t size) |
| 66 | { |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 67 | uint32_t mhu_status; |
| 68 | |
| 69 | mhu_status = mhu_secure_message_wait(); |
| 70 | |
| 71 | /* Expect an SCP Boot Protocol message, reject any other protocol */ |
| 72 | if (mhu_status != (1 << BOM_MHU_SLOT_ID)) { |
| 73 | ERROR("MHU: Unexpected protocol (MHU status: 0x%x)\n", |
| 74 | mhu_status); |
| 75 | panic(); |
| 76 | } |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 77 | |
Juan Castillo | 2e86cb1 | 2016-01-13 15:01:09 +0000 | [diff] [blame] | 78 | /* Ensure that any read to the BOM payload area is done after reading |
| 79 | * the MHU register. If these 2 reads were reordered then the CPU would |
| 80 | * read invalid payload data */ |
| 81 | dmbld(); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 82 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 83 | return *(uint32_t *) BOM_SHARED_MEM; |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | static void scp_boot_message_end(void) |
| 87 | { |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 88 | mhu_secure_message_end(BOM_MHU_SLOT_ID); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 91 | int scp_bootloader_transfer(void *image, unsigned int image_size) |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 92 | { |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 93 | uint32_t response; |
| 94 | uint32_t checksum; |
| 95 | cmd_info_payload_t *cmd_info_payload; |
| 96 | cmd_data_payload_t *cmd_data_payload; |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 97 | |
Juan Castillo | a72b647 | 2015-12-10 15:49:17 +0000 | [diff] [blame] | 98 | assert((uintptr_t) image == SCP_BL2_BASE); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 99 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 100 | if ((image_size == 0) || (image_size % 4 != 0)) { |
Juan Castillo | a72b647 | 2015-12-10 15:49:17 +0000 | [diff] [blame] | 101 | 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] | 102 | "4 bytes and not zero (current size = 0x%x)\n", |
| 103 | image_size); |
| 104 | return -1; |
| 105 | } |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 106 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 107 | /* Extract the checksum from the image */ |
| 108 | checksum = *(uint32_t *) image; |
| 109 | image = (char *) image + sizeof(checksum); |
| 110 | image_size -= sizeof(checksum); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 111 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 112 | mhu_secure_init(); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 113 | |
Juan Castillo | a72b647 | 2015-12-10 15:49:17 +0000 | [diff] [blame] | 114 | 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] | 115 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 116 | /* |
| 117 | * Send information about the SCP firmware image about to be transferred |
| 118 | * to SCP |
| 119 | */ |
| 120 | scp_boot_message_start(); |
| 121 | |
| 122 | BOM_CMD_HEADER->id = BOOT_CMD_INFO; |
| 123 | cmd_info_payload = BOM_CMD_PAYLOAD; |
| 124 | cmd_info_payload->image_size = image_size; |
| 125 | cmd_info_payload->checksum = checksum; |
| 126 | |
| 127 | scp_boot_message_send(sizeof(*cmd_info_payload)); |
Sandrine Bailleux | 7da652d | 2015-04-13 11:47:48 +0100 | [diff] [blame] | 128 | #if CSS_DETECT_PRE_1_7_0_SCP |
| 129 | { |
| 130 | const uint32_t deprecated_scp_nack_cmd = 0x404; |
| 131 | uint32_t mhu_status; |
| 132 | |
| 133 | VERBOSE("Detecting SCP version incompatibility\n"); |
| 134 | |
| 135 | mhu_status = mhu_secure_message_wait(); |
| 136 | if (mhu_status == deprecated_scp_nack_cmd) { |
| 137 | ERROR("Detected an incompatible version of the SCP firmware.\n"); |
| 138 | ERROR("Only versions from v1.7.0 onwards are supported.\n"); |
| 139 | ERROR("Please update the SCP firmware.\n"); |
| 140 | return -1; |
| 141 | } |
| 142 | |
| 143 | VERBOSE("SCP version looks OK\n"); |
| 144 | } |
| 145 | #endif /* CSS_DETECT_PRE_1_7_0_SCP */ |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 146 | response = scp_boot_message_wait(sizeof(response)); |
| 147 | scp_boot_message_end(); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 148 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 149 | if (response != 0) { |
| 150 | ERROR("SCP BOOT_CMD_INFO returned error %u\n", response); |
| 151 | return -1; |
| 152 | } |
| 153 | |
Juan Castillo | a72b647 | 2015-12-10 15:49:17 +0000 | [diff] [blame] | 154 | VERBOSE("Transferring SCP_BL2 image to SCP\n"); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 155 | |
Juan Castillo | a72b647 | 2015-12-10 15:49:17 +0000 | [diff] [blame] | 156 | /* Transfer SCP_BL2 image to SCP */ |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 157 | scp_boot_message_start(); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 158 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 159 | BOM_CMD_HEADER->id = BOOT_CMD_DATA; |
| 160 | cmd_data_payload = BOM_CMD_PAYLOAD; |
Sandrine Bailleux | 47ea1bc | 2015-06-09 11:53:33 +0100 | [diff] [blame] | 161 | cmd_data_payload->offset = (uintptr_t) image - ARM_TRUSTED_SRAM_BASE; |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 162 | cmd_data_payload->block_size = image_size; |
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 | scp_boot_message_send(sizeof(*cmd_data_payload)); |
| 165 | response = scp_boot_message_wait(sizeof(response)); |
| 166 | scp_boot_message_end(); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 167 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 168 | if (response != 0) { |
| 169 | ERROR("SCP BOOT_CMD_DATA returned error %u\n", response); |
| 170 | return -1; |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 173 | VERBOSE("Waiting for SCP to signal it is ready to go on\n"); |
| 174 | |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 175 | /* Wait for SCP to signal it's ready */ |
| 176 | return scpi_wait_ready(); |
| 177 | } |