Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 1 | From cefc1d101c62e62e66f9cbf1775defde7d2c3d27 Mon Sep 17 00:00:00 2001 |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 2 | From: Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com> |
| 3 | Date: Mon, 15 Aug 2022 15:12:49 +0100 |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 4 | Subject: [PATCH 09/25] arm_ffa: efi: introduce FF-A MM communication |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 5 | |
| 6 | Add MM communication support using FF-A transport |
| 7 | |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 8 | This feature allows accessing MM partitions services through |
| 9 | EFI MM communication protocol. MM partitions such as StandAlonneMM |
| 10 | or smm-gateway secure partitions which reside in secure world. |
| 11 | |
| 12 | An MM shared buffer and a door bell event are used to exchange |
| 13 | the data. |
| 14 | |
| 15 | The data is used by EFI services such as GetVariable()/SetVariable() |
| 16 | and copied from the communication buffer to the MM shared buffer. |
| 17 | |
| 18 | The secure partition is notified about availability of data in the |
| 19 | MM shared buffer by an FF-A message (door bell). |
| 20 | |
| 21 | On such event, MM SP can read the data and updates the MM shared |
| 22 | buffer with the response data. |
| 23 | |
| 24 | The response data is copied back to the communication buffer and |
| 25 | consumed by the EFI subsystem. |
| 26 | |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 27 | MM communication protocol supports FF-A 64-bit direct messaging. |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 28 | |
| 29 | Signed-off-by: Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com> |
| 30 | Signed-off-by: Gowtham Suresh Kumar <gowtham.sureshkumar@arm.com> |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 31 | Cc: Tom Rini <trini@konsulko.com> |
| 32 | Cc: Simon Glass <sjg@chromium.org> |
| 33 | Cc: Ilias Apalodimas <ilias.apalodimas@linaro.org> |
| 34 | Cc: Jens Wiklander <jens.wiklander@linaro.org> |
| 35 | Upstream-Status: Submitted [cover letter: https://lore.kernel.org/all/20221122131751.22747-1-abdellatif.elkhlifi@arm.com/] |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 36 | |
| 37 | Changelog: |
| 38 | =============== |
| 39 | |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 40 | v8: |
| 41 | |
| 42 | * isolate the compilation choices between FF-A and OP-TEE |
| 43 | * update partition_info_get() second argument to be an SP count |
| 44 | * pass NULL device pointer to the FF-A bus discovery and operations |
| 45 | |
| 46 | v7: |
| 47 | |
| 48 | * set the MM door bell event to use 64-bit direct messaging |
| 49 | * issue a compile time error when one of these macros are not found : |
| 50 | FFA_SHARED_MM_BUFFER_SIZE, FFA_SHARED_MM_BUFFER_OFFSET, FFA_SHARED_MM_BUFFER_ADDR |
| 51 | * make mm_sp_svc_uuid static |
| 52 | * replace EINVAL with ENOMEM in ffa_discover_mm_sp_id() when calloc() fails |
| 53 | * improve use of unmap_sysmem() in ffa_mm_communicate() |
| 54 | |
| 55 | v6: |
| 56 | |
| 57 | * add FF-A runtime discovery at MM communication level |
| 58 | * drop EFI runtime support for FF-A MM communication |
| 59 | * revert the changes in include/mm_communication.h for |
| 60 | efi_mm_communicate_header and smm_variable_access structures |
| 61 | |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 62 | v4: |
| 63 | |
| 64 | * use the new FF-A driver interfaces |
| 65 | * discover MM partitions at runtime |
| 66 | * copy FF-A driver private data to EFI runtime section at |
| 67 | ExitBootServices() |
| 68 | * drop use of FFA_ERR_STAT_SUCCESS error code |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 69 | * replace EFI_BUFFER_TOO_SMALL with EFI_OUT_OF_RESOURCES |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 70 | in ffa_mm_communicate(). No need for efi_memcpy_runtime() anymore |
| 71 | * revert the error log in mm_communicate() in case of failure |
| 72 | * remove packed attribute from efi_mm_communicate_header and |
| 73 | smm_variable_communicate_header |
| 74 | |
| 75 | v2: |
| 76 | |
| 77 | * set default values to 0 for FFA_SHARED_MM_BUFFER_SIZE, FFA_SHARED_MM_BUFFER_ADDR and MM_SP_UUID_DATA and add warnings |
| 78 | |
| 79 | v1: |
| 80 | |
| 81 | * introduce FF-A MM communication |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 82 | --- |
| 83 | include/mm_communication.h | 5 + |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 84 | lib/efi_loader/Kconfig | 14 +- |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 85 | lib/efi_loader/efi_variable_tee.c | 294 +++++++++++++++++++++++++++++- |
| 86 | 3 files changed, 307 insertions(+), 6 deletions(-) |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 87 | |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 88 | diff --git a/include/mm_communication.h b/include/mm_communication.h |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 89 | index e65fbde60d..d409bed777 100644 |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 90 | --- a/include/mm_communication.h |
| 91 | +++ b/include/mm_communication.h |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 92 | @@ -6,6 +6,8 @@ |
| 93 | * Copyright (c) 2017, Intel Corporation. All rights reserved. |
| 94 | * Copyright (C) 2020 Linaro Ltd. <sughosh.ganu@linaro.org> |
| 95 | * Copyright (C) 2020 Linaro Ltd. <ilias.apalodimas@linaro.org> |
| 96 | + * (C) Copyright 2022 ARM Limited |
| 97 | + * Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com> |
| 98 | */ |
| 99 | |
| 100 | #ifndef _MM_COMMUNICATION_H_ |
| 101 | @@ -13,6 +15,9 @@ |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 102 | |
| 103 | #include <part_efi.h> |
| 104 | |
| 105 | +/* MM service UUID string (big-endian format). This UUID is common across all MM SPs */ |
| 106 | +#define MM_SP_UUID "33d532ed-e699-0942-c09c-a798d9cd722d" |
| 107 | + |
| 108 | /* |
| 109 | * Interface to the pseudo Trusted Application (TA), which provides a |
| 110 | * communication channel with the Standalone MM (Management Mode) |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 111 | diff --git a/lib/efi_loader/Kconfig b/lib/efi_loader/Kconfig |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 112 | index b8fb2701a7..d292f57244 100644 |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 113 | --- a/lib/efi_loader/Kconfig |
| 114 | +++ b/lib/efi_loader/Kconfig |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 115 | @@ -61,13 +61,23 @@ config EFI_VARIABLE_FILE_STORE |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 116 | stored as file /ubootefi.var on the EFI system partition. |
| 117 | |
| 118 | config EFI_MM_COMM_TEE |
| 119 | - bool "UEFI variables storage service via OP-TEE" |
| 120 | - depends on OPTEE |
| 121 | + bool "UEFI variables storage service via the trusted world" |
| 122 | + depends on OPTEE || ARM_FFA_TRANSPORT |
| 123 | help |
| 124 | + Allowing access to the MM SP services (SPs such as StandAlonneMM, smm-gateway). |
| 125 | + When using the u-boot OP-TEE driver, StandAlonneMM is supported. |
| 126 | + When using the u-boot FF-A driver any MM SP is supported. |
| 127 | + |
| 128 | If OP-TEE is present and running StandAloneMM, dispatch all UEFI |
| 129 | variable related operations to that. The application will verify, |
| 130 | authenticate and store the variables on an RPMB. |
| 131 | |
| 132 | + When ARM_FFA_TRANSPORT is used, dispatch all UEFI variable related |
| 133 | + operations to the MM SP running in the secure world. |
| 134 | + A door bell mechanism is used to notify the SP when there is data in the shared |
| 135 | + MM buffer. The data is copied by u-boot to the shared buffer before issuing |
| 136 | + the door bell event. |
| 137 | + |
| 138 | config EFI_VARIABLE_NO_STORE |
| 139 | bool "Don't persist non-volatile UEFI variables" |
| 140 | help |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 141 | diff --git a/lib/efi_loader/efi_variable_tee.c b/lib/efi_loader/efi_variable_tee.c |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 142 | index dfef18435d..3933a24e8c 100644 |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 143 | --- a/lib/efi_loader/efi_variable_tee.c |
| 144 | +++ b/lib/efi_loader/efi_variable_tee.c |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 145 | @@ -4,9 +4,12 @@ |
| 146 | * |
| 147 | * Copyright (C) 2019 Linaro Ltd. <sughosh.ganu@linaro.org> |
| 148 | * Copyright (C) 2019 Linaro Ltd. <ilias.apalodimas@linaro.org> |
| 149 | + * Copyright (C) 2022 ARM Limited |
| 150 | + * Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com> |
| 151 | */ |
| 152 | |
| 153 | #include <common.h> |
| 154 | +#include <dm.h> |
| 155 | #include <efi.h> |
| 156 | #include <efi_api.h> |
| 157 | #include <efi_loader.h> |
| 158 | @@ -15,6 +18,36 @@ |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 159 | #include <malloc.h> |
| 160 | #include <mm_communication.h> |
| 161 | |
| 162 | +#if (IS_ENABLED(CONFIG_ARM_FFA_TRANSPORT)) |
| 163 | + |
| 164 | +#include <arm_ffa.h> |
| 165 | +#include <cpu_func.h> |
| 166 | +#include <mapmem.h> |
| 167 | + |
| 168 | +#ifndef FFA_SHARED_MM_BUFFER_SIZE |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 169 | +#error "FFA_SHARED_MM_BUFFER_SIZE must be defined in include/configs/<board>.h" |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 170 | +#define FFA_SHARED_MM_BUFFER_SIZE 0 |
| 171 | +#endif |
| 172 | + |
| 173 | +#ifndef FFA_SHARED_MM_BUFFER_OFFSET |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 174 | +#error "FFA_SHARED_MM_BUFFER_OFFSET must be defined in include/configs/<board>.h" |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 175 | +#define FFA_SHARED_MM_BUFFER_OFFSET 0 |
| 176 | +#endif |
| 177 | + |
| 178 | +#ifndef FFA_SHARED_MM_BUFFER_ADDR |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 179 | +#error "FFA_SHARED_MM_BUFFER_ADDR must be defined in include/configs/<board>.h" |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 180 | +#define FFA_SHARED_MM_BUFFER_ADDR 0 |
| 181 | +#endif |
| 182 | + |
| 183 | +/* MM return codes */ |
| 184 | +#define MM_SUCCESS (0) |
| 185 | + |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 186 | +static const char *mm_sp_svc_uuid = MM_SP_UUID; |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 187 | + |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 188 | +static u16 mm_sp_id; |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 189 | + |
| 190 | +#endif |
| 191 | + |
| 192 | extern struct efi_var_file __efi_runtime_data *efi_var_buf; |
| 193 | static efi_uintn_t max_buffer_size; /* comm + var + func + data */ |
| 194 | static efi_uintn_t max_payload_size; /* func + data */ |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 195 | @@ -24,6 +57,7 @@ struct mm_connection { |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 196 | u32 session; |
| 197 | }; |
| 198 | |
| 199 | +#if (IS_ENABLED(CONFIG_OPTEE)) |
| 200 | /** |
| 201 | * get_connection() - Retrieve OP-TEE session for a specific UUID. |
| 202 | * |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 203 | @@ -143,13 +177,248 @@ static efi_status_t optee_mm_communicate(void *comm_buf, ulong dsize) |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 204 | |
| 205 | return ret; |
| 206 | } |
| 207 | +#endif |
| 208 | + |
| 209 | +#if (IS_ENABLED(CONFIG_ARM_FFA_TRANSPORT)) |
| 210 | |
| 211 | /** |
| 212 | - * mm_communicate() - Adjust the cmonnucation buffer to StandAlonneMM and send |
| 213 | + * ffa_notify_mm_sp() - Announce there is data in the shared buffer |
| 214 | + * |
| 215 | + * Notifies the MM partition in the trusted world that |
| 216 | + * data is available in the shared buffer. |
| 217 | + * This is a blocking call during which trusted world has exclusive access |
| 218 | + * to the MM shared buffer. |
| 219 | + * |
| 220 | + * Return: |
| 221 | + * |
| 222 | + * 0 on success |
| 223 | + */ |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 224 | +static int ffa_notify_mm_sp(void) |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 225 | +{ |
| 226 | + struct ffa_send_direct_data msg = {0}; |
| 227 | + int ret; |
| 228 | + int sp_event_ret = -1; |
| 229 | + |
| 230 | + if (!ffa_bus_ops_get()) |
| 231 | + return -EINVAL; |
| 232 | + |
| 233 | + msg.data0 = FFA_SHARED_MM_BUFFER_OFFSET; /* x3 */ |
| 234 | + |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 235 | + ret = ffa_bus_ops_get()->sync_send_receive(NULL, mm_sp_id, &msg, 1); |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 236 | + if (ret != 0) |
| 237 | + return ret; |
| 238 | + |
| 239 | + sp_event_ret = msg.data0; /* x3 */ |
| 240 | + |
| 241 | + if (sp_event_ret == MM_SUCCESS) |
| 242 | + return 0; |
| 243 | + |
| 244 | + /* |
| 245 | + * Failure to notify the MM SP |
| 246 | + */ |
| 247 | + |
| 248 | + return -EACCES; |
| 249 | +} |
| 250 | + |
| 251 | +/** |
| 252 | + * ffa_discover_mm_sp_id() - Query the MM partition ID |
| 253 | + * |
| 254 | + * Use the FF-A driver to get the MM partition ID. |
| 255 | + * If multiple partitions are found, use the first one. |
| 256 | + * This is a boot time function. |
| 257 | + * |
| 258 | + * Return: |
| 259 | + * |
| 260 | + * 0 on success |
| 261 | + */ |
| 262 | +static int ffa_discover_mm_sp_id(void) |
| 263 | +{ |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 264 | + u32 count = 0; |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 265 | + int ret; |
| 266 | + struct ffa_partition_info *parts_info; |
| 267 | + |
| 268 | + if (!ffa_bus_ops_get()) |
| 269 | + return -EINVAL; |
| 270 | + |
| 271 | + /* |
| 272 | + * get from the driver the count of the SPs matching the UUID |
| 273 | + */ |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 274 | + ret = ffa_bus_ops_get()->partition_info_get(NULL, mm_sp_svc_uuid, &count, NULL); |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 275 | + if (ret != 0) { |
| 276 | + log_err("EFI: Failure in querying partitions count (error code: %d)\n", ret); |
| 277 | + return ret; |
| 278 | + } |
| 279 | + |
| 280 | + if (!count) { |
| 281 | + log_info("EFI: No MM partition found\n"); |
| 282 | + return ret; |
| 283 | + } |
| 284 | + |
| 285 | + /* |
| 286 | + * pre-allocate a buffer to be filled by the driver |
| 287 | + * with ffa_partition_info structs |
| 288 | + */ |
| 289 | + |
| 290 | + log_info("EFI: Pre-allocating %d partition(s) info structures\n", count); |
| 291 | + |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 292 | + parts_info = calloc(count, sizeof(*parts_info)); |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 293 | + if (!parts_info) |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 294 | + return -ENOMEM; |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 295 | + |
| 296 | + /* |
| 297 | + * ask the driver to fill the |
| 298 | + * buffer with the SPs info |
| 299 | + */ |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 300 | + ret = ffa_bus_ops_get()->partition_info_get(NULL, mm_sp_svc_uuid, &count, parts_info); |
| 301 | + if (ret) { |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 302 | + log_err("EFI: Failure in querying partition(s) info (error code: %d)\n", ret); |
| 303 | + free(parts_info); |
| 304 | + return ret; |
| 305 | + } |
| 306 | + |
| 307 | + /* |
| 308 | + * MM SPs found , use the first one |
| 309 | + */ |
| 310 | + |
| 311 | + mm_sp_id = parts_info[0].id; |
| 312 | + |
| 313 | + log_info("EFI: MM partition ID 0x%x\n", mm_sp_id); |
| 314 | + |
| 315 | + free(parts_info); |
| 316 | + |
| 317 | + return 0; |
| 318 | +} |
| 319 | + |
| 320 | +/** |
| 321 | + * ffa_mm_communicate() - Exchange EFI services data with the MM partition using FF-A |
| 322 | + * @comm_buf: locally allocated communication buffer used for rx/tx |
| 323 | + * @dsize: communication buffer size |
| 324 | + * |
| 325 | + * Issues a door bell event to notify the MM partition (SP) running in OP-TEE |
| 326 | + * that there is data to read from the shared buffer. |
| 327 | + * Communication with the MM SP is performed using FF-A transport. |
| 328 | + * On the event, MM SP can read the data from the buffer and |
| 329 | + * update the MM shared buffer with response data. |
| 330 | + * The response data is copied back to the communication buffer. |
| 331 | + * |
| 332 | + * Return: |
| 333 | + * |
| 334 | + * EFI status code |
| 335 | + */ |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 336 | +static efi_status_t ffa_mm_communicate(void *comm_buf, ulong comm_buf_size) |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 337 | +{ |
| 338 | + ulong tx_data_size; |
| 339 | + int ffa_ret; |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 340 | + efi_status_t efi_ret; |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 341 | + struct efi_mm_communicate_header *mm_hdr; |
| 342 | + void *virt_shared_buf; |
| 343 | + |
| 344 | + if (!comm_buf) |
| 345 | + return EFI_INVALID_PARAMETER; |
| 346 | + |
| 347 | + /* Discover MM partition ID at boot time */ |
| 348 | + if (!mm_sp_id && ffa_discover_mm_sp_id() != 0) { |
| 349 | + log_err("EFI: Failure to discover MM partition ID at boot time\n"); |
| 350 | + return EFI_UNSUPPORTED; |
| 351 | + } |
| 352 | + |
| 353 | + mm_hdr = (struct efi_mm_communicate_header *)comm_buf; |
| 354 | + tx_data_size = mm_hdr->message_len + sizeof(efi_guid_t) + sizeof(size_t); |
| 355 | + |
| 356 | + if (comm_buf_size != tx_data_size || tx_data_size > FFA_SHARED_MM_BUFFER_SIZE) |
| 357 | + return EFI_INVALID_PARAMETER; |
| 358 | + |
| 359 | + /* Copy the data to the shared buffer */ |
| 360 | + |
| 361 | + virt_shared_buf = (void *)map_sysmem((phys_addr_t)FFA_SHARED_MM_BUFFER_ADDR, 0); |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 362 | + memcpy(virt_shared_buf, comm_buf, tx_data_size); |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 363 | + |
| 364 | + /* |
| 365 | + * The secure world might have cache disabled for |
| 366 | + * the device region used for shared buffer (which is the case for Optee). |
| 367 | + * In this case, the secure world reads the data from DRAM. |
| 368 | + * Let's flush the cache so the DRAM is updated with the latest data. |
| 369 | + */ |
| 370 | + #ifdef CONFIG_ARM64 |
| 371 | + invalidate_dcache_all(); |
| 372 | + #endif |
| 373 | + |
| 374 | + /* Announce there is data in the shared buffer */ |
| 375 | + |
| 376 | + ffa_ret = ffa_notify_mm_sp(); |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 377 | + |
| 378 | + switch (ffa_ret) { |
| 379 | + case 0: |
| 380 | + { |
| 381 | + ulong rx_data_size; |
| 382 | + /* Copy the MM SP response from the shared buffer to the communication buffer */ |
| 383 | + rx_data_size = ((struct efi_mm_communicate_header *)virt_shared_buf)->message_len + |
| 384 | + sizeof(efi_guid_t) + |
| 385 | + sizeof(size_t); |
| 386 | + |
| 387 | + if (rx_data_size > comm_buf_size) { |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 388 | + efi_ret = EFI_OUT_OF_RESOURCES; |
| 389 | + break; |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 390 | + } |
| 391 | + |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 392 | + memcpy(comm_buf, virt_shared_buf, rx_data_size); |
| 393 | + efi_ret = EFI_SUCCESS; |
| 394 | + break; |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 395 | + } |
| 396 | + case -EINVAL: |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 397 | + efi_ret = EFI_DEVICE_ERROR; |
| 398 | + break; |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 399 | + case -EPERM: |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 400 | + efi_ret = EFI_INVALID_PARAMETER; |
| 401 | + break; |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 402 | + case -EACCES: |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 403 | + efi_ret = EFI_ACCESS_DENIED; |
| 404 | + break; |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 405 | + case -EBUSY: |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 406 | + efi_ret = EFI_OUT_OF_RESOURCES; |
| 407 | + break; |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 408 | + default: |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 409 | + efi_ret = EFI_ACCESS_DENIED; |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 410 | + } |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 411 | + |
| 412 | + unmap_sysmem(virt_shared_buf); |
| 413 | + return efi_ret; |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 414 | +} |
| 415 | +#endif |
| 416 | + |
| 417 | +/** |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 418 | + * select_ffa_mm_comms() - checks FF-A support availability |
| 419 | + * |
| 420 | + * Making sure FF-A is compiled in. If that's the case try to discover |
| 421 | + * the FF-A bus. |
| 422 | + * |
| 423 | + * Return: |
| 424 | + * |
| 425 | + * 0: FF-A ready for use. Otherwise, failure |
| 426 | + */ |
| 427 | +static efi_status_t select_ffa_mm_comms(void) |
| 428 | +{ |
| 429 | + efi_status_t ret = EFI_UNSUPPORTED; |
| 430 | +#if (IS_ENABLED(CONFIG_ARM_FFA_TRANSPORT)) |
| 431 | + ret = ffa_bus_discover(NULL); |
| 432 | + if (ret) |
| 433 | + ret = EFI_NOT_READY; |
| 434 | +#endif |
| 435 | + return ret; |
| 436 | +} |
| 437 | + |
| 438 | +/** |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 439 | + * mm_communicate() - Adjust the communication buffer to the MM SP and send |
| 440 | * it to OP-TEE |
| 441 | * |
| 442 | - * @comm_buf: locally allocted communcation buffer |
| 443 | + * @comm_buf: locally allocated communication buffer |
| 444 | * @dsize: buffer size |
| 445 | + * |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 446 | + * The SP (also called partition) can be any MM SP such as StandAlonneMM or smm-gateway. |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 447 | + * The comm_buf format is the same for both partitions. |
| 448 | + * When using the u-boot OP-TEE driver, StandAlonneMM is supported. |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 449 | + * When using the u-boot FF-A driver, any MM SP is supported. |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 450 | + * |
| 451 | * Return: status code |
| 452 | */ |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 453 | static efi_status_t mm_communicate(u8 *comm_buf, efi_uintn_t dsize) |
| 454 | @@ -162,7 +431,17 @@ static efi_status_t mm_communicate(u8 *comm_buf, efi_uintn_t dsize) |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 455 | mm_hdr = (struct efi_mm_communicate_header *)comm_buf; |
| 456 | var_hdr = (struct smm_variable_communicate_header *)mm_hdr->data; |
| 457 | |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 458 | - ret = optee_mm_communicate(comm_buf, dsize); |
| 459 | + ret = select_ffa_mm_comms(); |
| 460 | + if (ret != EFI_SUCCESS) { |
| 461 | +#if (IS_ENABLED(CONFIG_OPTEE)) |
| 462 | + ret = optee_mm_communicate(comm_buf, dsize); |
| 463 | +#endif |
| 464 | + } else { |
| 465 | +#if (IS_ENABLED(CONFIG_ARM_FFA_TRANSPORT)) |
| 466 | + ret = ffa_mm_communicate(comm_buf, dsize); |
| 467 | +#endif |
| 468 | + } |
| 469 | + |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 470 | if (ret != EFI_SUCCESS) { |
| 471 | log_err("%s failed!\n", __func__); |
| 472 | return ret; |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 473 | @@ -258,6 +537,13 @@ efi_status_t EFIAPI get_max_payload(efi_uintn_t *size) |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 474 | goto out; |
| 475 | } |
| 476 | *size = var_payload->size; |
| 477 | + |
| 478 | + #if (IS_ENABLED(CONFIG_ARM_FFA_TRANSPORT)) |
| 479 | + if (*size > FFA_SHARED_MM_BUFFER_SIZE) |
| 480 | + *size = FFA_SHARED_MM_BUFFER_SIZE - MM_COMMUNICATE_HEADER_SIZE - |
| 481 | + MM_VARIABLE_COMMUNICATE_SIZE; |
| 482 | + #endif |
| 483 | + |
| 484 | /* |
| 485 | * There seems to be a bug in EDK2 miscalculating the boundaries and |
| 486 | * size checks, so deduct 2 more bytes to fulfill this requirement. Fix |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 487 | @@ -697,7 +983,7 @@ void efi_variables_boot_exit_notify(void) |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 488 | ret = EFI_NOT_FOUND; |
| 489 | |
| 490 | if (ret != EFI_SUCCESS) |
| 491 | - log_err("Unable to notify StMM for ExitBootServices\n"); |
| 492 | + log_err("Unable to notify the MM partition for ExitBootServices\n"); |
| 493 | free(comm_buf); |
| 494 | |
| 495 | /* |
| 496 | -- |
| 497 | 2.17.1 |
| 498 | |