1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2015-2019, Arm Limited and Contributors. All rights reserved. 4 * Copyright (c) 2019-2020, Linaro Limited 5 */ 6 #include <assert.h> 7 #include <drivers/scmi-msg.h> 8 #include <drivers/scmi.h> 9 #include <io.h> 10 #include <kernel/misc.h> 11 #include <stdbool.h> 12 #include <stdint.h> 13 #include <string.h> 14 #include <trace.h> 15 #include <util.h> 16 17 #include "common.h" 18 19 /* Legacy SMT/SCMI messages are 128 bytes at most including SMT header */ 20 #define SCMI_PLAYLOAD_MAX 92 21 #define SCMI_PLAYLOAD_U32_MAX (SCMI_PLAYLOAD_MAX / sizeof(uint32_t)) 22 23 /** 24 * struct smt_header - SMT formatted header for SMT base shared memory transfer 25 * 26 * @status: Bit flags, see SMT_STATUS_* 27 * @flags: Bit flags, see SMT_FLAG_* 28 * @length: Byte size of message payload (variable) + ::message_header (32bit) 29 * payload: SCMI message payload data 30 */ 31 struct smt_header { 32 uint32_t reserved0; 33 uint32_t status; 34 uint64_t reserved1; 35 uint32_t flags; 36 uint32_t length; /* message_header + payload */ 37 uint32_t message_header; 38 uint32_t payload[]; 39 }; 40 41 /* Flag set in smt_header::status when SMT does not contain pending message */ 42 #define SMT_STATUS_FREE BIT(0) 43 /* Flag set in smt_header::status when SMT reports an error */ 44 #define SMT_STATUS_ERROR BIT(1) 45 46 /* Flag set in smt_header::flags when SMT uses interrupts */ 47 #define SMT_FLAG_INTR_ENABLED BIT(1) 48 49 /* Bit fields packed in smt_header::message_header */ 50 #define SMT_MSG_ID_MASK GENMASK_32(7, 0) 51 #define SMT_HDR_MSG_ID(_hdr) ((_hdr) & SMT_MSG_ID_MASK) 52 53 #define SMT_MSG_TYPE_MASK GENMASK_32(9, 8) 54 #define SMT_HDR_TYPE_ID(_hdr) (((_hdr) & SMT_MSG_TYPE_MASK) >> 8) 55 56 #define SMT_MSG_PROT_ID_MASK GENMASK_32(17, 10) 57 #define SMT_HDR_PROT_ID(_hdr) (((_hdr) & SMT_MSG_PROT_ID_MASK) >> 10) 58 59 static struct smt_header *channel_to_smt_hdr(struct scmi_msg_channel *chan) 60 { 61 return (struct smt_header *)io_pa_or_va(&chan->shm_addr, 62 sizeof(struct smt_header)); 63 } 64 65 /* 66 * Creates a SCMI message instance in secure memory and push it in the SCMI 67 * message drivers. Message structure contains SCMI protocol meta-data and 68 * references to input payload in secure memory and output message buffer 69 * in shared memory. 70 */ 71 static void scmi_process_smt(unsigned int channel_id, uint32_t *payload_buf) 72 { 73 struct scmi_msg_channel *chan = NULL; 74 struct smt_header *smt_hdr = NULL; 75 size_t in_payload_size = 0; 76 uint32_t smt_status = 0; 77 struct scmi_msg msg = { }; 78 bool error = true; 79 80 chan = plat_scmi_get_channel(channel_id); 81 if (!chan) 82 return; 83 84 smt_hdr = channel_to_smt_hdr(chan); 85 assert(smt_hdr); 86 87 smt_status = READ_ONCE(smt_hdr->status); 88 89 if (!scmi_msg_claim_channel(chan)) { 90 DMSG("SCMI channel %u busy", channel_id); 91 goto out; 92 } 93 94 in_payload_size = READ_ONCE(smt_hdr->length) - 95 sizeof(smt_hdr->message_header); 96 97 if (in_payload_size > SCMI_PLAYLOAD_MAX) { 98 DMSG("SCMI payload too big %u", in_payload_size); 99 goto out; 100 } 101 102 if (smt_status & (SMT_STATUS_ERROR | SMT_STATUS_FREE)) { 103 DMSG("SCMI channel bad status 0x%x", 104 smt_hdr->status & (SMT_STATUS_ERROR | SMT_STATUS_FREE)); 105 goto out; 106 } 107 108 /* Fill message */ 109 msg.in = (char *)payload_buf; 110 msg.in_size = in_payload_size; 111 msg.out = (char *)smt_hdr->payload; 112 msg.out_size = chan->shm_size - sizeof(*smt_hdr); 113 114 assert(msg.out && msg.out_size >= sizeof(int32_t)); 115 116 /* Here the payload is copied in secure memory */ 117 memcpy(msg.in, smt_hdr->payload, in_payload_size); 118 119 msg.protocol_id = SMT_HDR_PROT_ID(smt_hdr->message_header); 120 msg.message_id = SMT_HDR_MSG_ID(smt_hdr->message_header); 121 msg.channel_id = channel_id; 122 123 scmi_process_message(&msg); 124 125 /* Update message length with the length of the response message */ 126 smt_hdr->length = msg.out_size_out + sizeof(smt_hdr->message_header); 127 128 scmi_msg_release_channel(chan); 129 error = false; 130 131 out: 132 if (error) { 133 DMSG("SCMI error"); 134 smt_hdr->status |= SMT_STATUS_ERROR | SMT_STATUS_FREE; 135 } else { 136 smt_hdr->status |= SMT_STATUS_FREE; 137 } 138 } 139 140 #ifdef CFG_SCMI_MSG_SMT_FASTCALL_ENTRY 141 /* Provision input message payload buffers for fastcall SMC context entries */ 142 static uint32_t fast_smc_payload[CFG_TEE_CORE_NB_CORE][SCMI_PLAYLOAD_U32_MAX]; 143 144 void scmi_smt_fastcall_smc_entry(unsigned int channel_id) 145 { 146 scmi_process_smt(channel_id, fast_smc_payload[get_core_pos()]); 147 } 148 #endif 149 150 #ifdef CFG_SCMI_MSG_SMT_INTERRUPT_ENTRY 151 /* Provision input message payload buffers for fastcall SMC context entries */ 152 static uint32_t interrupt_payload[CFG_TEE_CORE_NB_CORE][SCMI_PLAYLOAD_U32_MAX]; 153 154 void scmi_smt_interrupt_entry(unsigned int channel_id) 155 { 156 scmi_process_smt(channel_id, interrupt_payload[get_core_pos()]); 157 } 158 #endif 159 160 #ifdef CFG_SCMI_MSG_SMT_THREAD_ENTRY 161 /* Provision input message payload buffers for fastcall SMC context entries */ 162 static uint32_t threaded_payload[CFG_NUM_THREADS][SCMI_PLAYLOAD_U32_MAX]; 163 164 void scmi_smt_threaded_entry(unsigned int channel_id) 165 { 166 assert(plat_scmi_get_channel(channel_id)->threaded); 167 168 scmi_process_smt(channel_id, threaded_payload[thread_get_id()]); 169 } 170 #endif 171 172 /* Init a SMT header for a shared memory buffer: state it a free/no-error */ 173 void scmi_smt_init_agent_channel(struct scmi_msg_channel *chan) 174 { 175 COMPILE_TIME_ASSERT(SCMI_PLAYLOAD_MAX + sizeof(struct smt_header) <= 176 SMT_BUF_SLOT_SIZE); 177 178 if (chan) { 179 struct smt_header *smt_header = channel_to_smt_hdr(chan); 180 181 if (smt_header) { 182 memset(smt_header, 0, sizeof(*smt_header)); 183 smt_header->status = SMT_STATUS_FREE; 184 185 return; 186 } 187 } 188 189 panic(); 190 } 191 192 void scmi_smt_set_shared_buffer(struct scmi_msg_channel *channel, void *base) 193 { 194 paddr_t p_base = 0; 195 196 if (base) { 197 assert(!channel->shm_addr.va && !channel->shm_addr.pa); 198 p_base = virt_to_phys(base); 199 assert(p_base); 200 } 201 202 channel->shm_addr.va = (vaddr_t)base; 203 channel->shm_addr.pa = p_base; 204 } 205