1 /* 2 * Copyright (c) 2022, Intel Corporation. All rights reserved. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 #include <assert.h> 8 #include <common/debug.h> 9 #include <common/runtime_svc.h> 10 #include <lib/mmio.h> 11 12 #include "socfpga_mailbox.h" 13 #include "socfpga_sip_svc.h" 14 15 static uint32_t intel_v2_mbox_send_cmd(uint32_t req_header, 16 uint32_t *data, uint32_t data_size) 17 { 18 uint32_t value; 19 uint32_t len; 20 21 if ((data == NULL) || (data_size == 0)) { 22 return INTEL_SIP_SMC_STATUS_REJECTED; 23 } 24 25 if (data_size > (MBOX_INC_HEADER_MAX_WORD_SIZE * MBOX_WORD_BYTE)) { 26 return INTEL_SIP_SMC_STATUS_REJECTED; 27 } 28 29 if (!is_size_4_bytes_aligned(data_size)) { 30 return INTEL_SIP_SMC_STATUS_REJECTED; 31 } 32 33 /* Make sure client id align in SMC SiP V2 header and mailbox header */ 34 value = (req_header >> INTEL_SIP_SMC_HEADER_CID_OFFSET) & 35 INTEL_SIP_SMC_HEADER_CID_MASK; 36 37 if (value != MBOX_RESP_CLIENT_ID(data[0])) { 38 return INTEL_SIP_SMC_STATUS_REJECTED; 39 } 40 41 /* Make sure job id align in SMC SiP V2 header and mailbox header */ 42 value = (req_header >> INTEL_SIP_SMC_HEADER_JOB_ID_OFFSET) & 43 INTEL_SIP_SMC_HEADER_JOB_ID_MASK; 44 45 if (value != MBOX_RESP_JOB_ID(data[0])) { 46 return INTEL_SIP_SMC_STATUS_REJECTED; 47 } 48 49 /* 50 * Make sure data length align in SMC SiP V2 header and 51 * mailbox header 52 */ 53 len = (data_size / MBOX_WORD_BYTE) - 1; 54 55 if (len != MBOX_RESP_LEN(data[0])) { 56 return INTEL_SIP_SMC_STATUS_REJECTED; 57 } 58 59 return mailbox_send_cmd_async_ext(data[0], &data[1], len); 60 } 61 62 static uint32_t intel_v2_mbox_poll_resp(uint64_t req_header, 63 uint32_t *data, uint32_t *data_size, 64 uint64_t *resp_header) 65 { 66 int status = 0; 67 uint32_t resp_len; 68 uint32_t job_id = 0; 69 uint32_t client_id = 0; 70 uint32_t version; 71 72 if ((data == NULL) || (data_size == NULL) || (resp_header == NULL)) { 73 return INTEL_SIP_SMC_STATUS_REJECTED; 74 } 75 76 if (!is_size_4_bytes_aligned(*data_size)) { 77 return INTEL_SIP_SMC_STATUS_REJECTED; 78 } 79 80 resp_len = (*data_size / MBOX_WORD_BYTE) - 1; 81 status = mailbox_read_response_async(&job_id, &data[0], &data[1], 82 &resp_len, 1); 83 84 if (status == MBOX_BUSY) { 85 status = INTEL_SIP_SMC_STATUS_BUSY; 86 } else if (status == MBOX_NO_RESPONSE) { 87 status = INTEL_SIP_SMC_STATUS_NO_RESPONSE; 88 } else { 89 *data_size = 0; 90 91 if (resp_len > 0) { 92 /* 93 * Fill in the final response length, 94 * the length include both mailbox header and payload 95 */ 96 *data_size = (resp_len + 1) * MBOX_WORD_BYTE; 97 98 /* Extract the client id from mailbox header */ 99 client_id = MBOX_RESP_CLIENT_ID(data[0]); 100 } 101 102 /* 103 * Extract SMC SiP V2 protocol version from 104 * SMC request header 105 */ 106 version = (req_header >> INTEL_SIP_SMC_HEADER_VERSION_OFFSET) & 107 INTEL_SIP_SMC_HEADER_VERSION_MASK; 108 109 /* Fill in SMC SiP V2 protocol response header */ 110 *resp_header = 0; 111 *resp_header |= (((uint64_t)job_id) & 112 INTEL_SIP_SMC_HEADER_JOB_ID_MASK) << 113 INTEL_SIP_SMC_HEADER_JOB_ID_OFFSET; 114 *resp_header |= (((uint64_t)client_id) & 115 INTEL_SIP_SMC_HEADER_CID_MASK) << 116 INTEL_SIP_SMC_HEADER_CID_OFFSET; 117 *resp_header |= (((uint64_t)version) & 118 INTEL_SIP_SMC_HEADER_VERSION_MASK) << 119 INTEL_SIP_SMC_HEADER_VERSION_OFFSET; 120 } 121 122 return status; 123 } 124 125 uintptr_t sip_smc_handler_v2(uint32_t smc_fid, 126 u_register_t x1, 127 u_register_t x2, 128 u_register_t x3, 129 u_register_t x4, 130 void *cookie, 131 void *handle, 132 u_register_t flags) 133 { 134 uint32_t retval = 0; 135 uint64_t retval64 = 0; 136 int status = INTEL_SIP_SMC_STATUS_OK; 137 138 switch (smc_fid) { 139 case INTEL_SIP_SMC_V2_GET_SVC_VERSION: 140 SMC_RET4(handle, INTEL_SIP_SMC_STATUS_OK, x1, 141 SIP_SVC_VERSION_MAJOR, 142 SIP_SVC_VERSION_MINOR); 143 144 case INTEL_SIP_SMC_V2_REG_READ: 145 status = intel_secure_reg_read(x2, &retval); 146 SMC_RET4(handle, status, x1, retval, x2); 147 148 case INTEL_SIP_SMC_V2_REG_WRITE: 149 status = intel_secure_reg_write(x2, (uint32_t)x3, &retval); 150 SMC_RET4(handle, status, x1, retval, x2); 151 152 case INTEL_SIP_SMC_V2_REG_UPDATE: 153 status = intel_secure_reg_update(x2, (uint32_t)x3, 154 (uint32_t)x4, &retval); 155 SMC_RET4(handle, status, x1, retval, x2); 156 157 case INTEL_SIP_SMC_V2_HPS_SET_BRIDGES: 158 status = intel_hps_set_bridges(x2, x3); 159 SMC_RET2(handle, status, x1); 160 161 case INTEL_SIP_SMC_V2_RSU_UPDATE_ADDR: 162 status = intel_rsu_update(x2); 163 SMC_RET2(handle, status, x1); 164 165 case INTEL_SIP_SMC_V2_MAILBOX_SEND_COMMAND: 166 status = intel_v2_mbox_send_cmd(x1, (uint32_t *)x2, x3); 167 SMC_RET2(handle, status, x1); 168 169 case INTEL_SIP_SMC_V2_MAILBOX_POLL_RESPONSE: 170 status = intel_v2_mbox_poll_resp(x1, (uint32_t *)x2, 171 (uint32_t *) &x3, &retval64); 172 SMC_RET4(handle, status, retval64, x2, x3); 173 174 default: 175 ERROR("%s: unhandled SMC V2 (0x%x)\n", __func__, smc_fid); 176 SMC_RET1(handle, SMC_UNK); 177 } 178 } 179