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