|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
| 2 | +/* |
| 3 | + * Arm Firmware Framework for ARMv8-A(FFA) interface driver |
| 4 | + * |
| 5 | + * The Arm FFA specification[1] describes a software architecture to |
| 6 | + * leverages the virtualization extension to isolate software images |
| 7 | + * provided by an ecosystem of vendors from each other and describes |
| 8 | + * interfaces that standardize communication between the various software |
| 9 | + * images including communication between images in the Secure world and |
| 10 | + * Normal world. Any Hypervisor could use the FFA interfaces to enable |
| 11 | + * communication between VMs it manages. |
| 12 | + * |
| 13 | + * The Hypervisor a.k.a Partition managers in FFA terminology can assign |
| 14 | + * system resources(Memory regions, Devices, CPU cycles) to the partitions |
| 15 | + * and manage isolation amongst them. |
| 16 | + * |
| 17 | + * [1] https://developer.arm.com/docs/den0077/latest |
| 18 | + * |
| 19 | + * Copyright (C) 2021 ARM Ltd. |
| 20 | + */ |
| 21 | + |
| 22 | +#define DRIVER_NAME "ARM FF-A" |
| 23 | +#define pr_fmt(fmt) DRIVER_NAME ": " fmt |
| 24 | + |
| 25 | +#include <linux/arm_ffa.h> |
| 26 | +#include <linux/bitfield.h> |
| 27 | +#include <linux/io.h> |
| 28 | +#include <linux/module.h> |
| 29 | +#include <linux/slab.h> |
| 30 | + |
| 31 | +#include "common.h" |
| 32 | + |
| 33 | +#define FFA_DRIVER_VERSION FFA_VERSION_1_0 |
| 34 | + |
| 35 | +#define FFA_SMC(calling_convention, func_num) \ |
| 36 | + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, (calling_convention), \ |
| 37 | + ARM_SMCCC_OWNER_STANDARD, (func_num)) |
| 38 | + |
| 39 | +#define FFA_SMC_32(func_num) FFA_SMC(ARM_SMCCC_SMC_32, (func_num)) |
| 40 | +#define FFA_SMC_64(func_num) FFA_SMC(ARM_SMCCC_SMC_64, (func_num)) |
| 41 | + |
| 42 | +#define FFA_ERROR FFA_SMC_32(0x60) |
| 43 | +#define FFA_SUCCESS FFA_SMC_32(0x61) |
| 44 | +#define FFA_INTERRUPT FFA_SMC_32(0x62) |
| 45 | +#define FFA_VERSION FFA_SMC_32(0x63) |
| 46 | +#define FFA_FEATURES FFA_SMC_32(0x64) |
| 47 | +#define FFA_RX_RELEASE FFA_SMC_32(0x65) |
| 48 | +#define FFA_RXTX_MAP FFA_SMC_32(0x66) |
| 49 | +#define FFA_FN64_RXTX_MAP FFA_SMC_64(0x66) |
| 50 | +#define FFA_RXTX_UNMAP FFA_SMC_32(0x67) |
| 51 | +#define FFA_PARTITION_INFO_GET FFA_SMC_32(0x68) |
| 52 | +#define FFA_ID_GET FFA_SMC_32(0x69) |
| 53 | +#define FFA_MSG_POLL FFA_SMC_32(0x6A) |
| 54 | +#define FFA_MSG_WAIT FFA_SMC_32(0x6B) |
| 55 | +#define FFA_YIELD FFA_SMC_32(0x6C) |
| 56 | +#define FFA_RUN FFA_SMC_32(0x6D) |
| 57 | +#define FFA_MSG_SEND FFA_SMC_32(0x6E) |
| 58 | +#define FFA_MSG_SEND_DIRECT_REQ FFA_SMC_32(0x6F) |
| 59 | +#define FFA_FN64_MSG_SEND_DIRECT_REQ FFA_SMC_64(0x6F) |
| 60 | +#define FFA_MSG_SEND_DIRECT_RESP FFA_SMC_32(0x70) |
| 61 | +#define FFA_FN64_MSG_SEND_DIRECT_RESP FFA_SMC_64(0x70) |
| 62 | +#define FFA_MEM_DONATE FFA_SMC_32(0x71) |
| 63 | +#define FFA_FN64_MEM_DONATE FFA_SMC_64(0x71) |
| 64 | +#define FFA_MEM_LEND FFA_SMC_32(0x72) |
| 65 | +#define FFA_FN64_MEM_LEND FFA_SMC_64(0x72) |
| 66 | +#define FFA_MEM_SHARE FFA_SMC_32(0x73) |
| 67 | +#define FFA_FN64_MEM_SHARE FFA_SMC_64(0x73) |
| 68 | +#define FFA_MEM_RETRIEVE_REQ FFA_SMC_32(0x74) |
| 69 | +#define FFA_FN64_MEM_RETRIEVE_REQ FFA_SMC_64(0x74) |
| 70 | +#define FFA_MEM_RETRIEVE_RESP FFA_SMC_32(0x75) |
| 71 | +#define FFA_MEM_RELINQUISH FFA_SMC_32(0x76) |
| 72 | +#define FFA_MEM_RECLAIM FFA_SMC_32(0x77) |
| 73 | +#define FFA_MEM_OP_PAUSE FFA_SMC_32(0x78) |
| 74 | +#define FFA_MEM_OP_RESUME FFA_SMC_32(0x79) |
| 75 | +#define FFA_MEM_FRAG_RX FFA_SMC_32(0x7A) |
| 76 | +#define FFA_MEM_FRAG_TX FFA_SMC_32(0x7B) |
| 77 | +#define FFA_NORMAL_WORLD_RESUME FFA_SMC_32(0x7C) |
| 78 | + |
| 79 | +/* |
| 80 | + * For some calls it is necessary to use SMC64 to pass or return 64-bit values. |
| 81 | + * For such calls FFA_FN_NATIVE(name) will choose the appropriate |
| 82 | + * (native-width) function ID. |
| 83 | + */ |
| 84 | +#ifdef CONFIG_64BIT |
| 85 | +#define FFA_FN_NATIVE(name) FFA_FN64_##name |
| 86 | +#else |
| 87 | +#define FFA_FN_NATIVE(name) FFA_##name |
| 88 | +#endif |
| 89 | + |
| 90 | +/* FFA error codes. */ |
| 91 | +#define FFA_RET_SUCCESS (0) |
| 92 | +#define FFA_RET_NOT_SUPPORTED (-1) |
| 93 | +#define FFA_RET_INVALID_PARAMETERS (-2) |
| 94 | +#define FFA_RET_NO_MEMORY (-3) |
| 95 | +#define FFA_RET_BUSY (-4) |
| 96 | +#define FFA_RET_INTERRUPTED (-5) |
| 97 | +#define FFA_RET_DENIED (-6) |
| 98 | +#define FFA_RET_RETRY (-7) |
| 99 | +#define FFA_RET_ABORTED (-8) |
| 100 | + |
| 101 | +#define MAJOR_VERSION_MASK GENMASK(30, 16) |
| 102 | +#define MINOR_VERSION_MASK GENMASK(15, 0) |
| 103 | +#define MAJOR_VERSION(x) ((u16)(FIELD_GET(MAJOR_VERSION_MASK, (x)))) |
| 104 | +#define MINOR_VERSION(x) ((u16)(FIELD_GET(MINOR_VERSION_MASK, (x)))) |
| 105 | +#define PACK_VERSION_INFO(major, minor) \ |
| 106 | + (FIELD_PREP(MAJOR_VERSION_MASK, (major)) | \ |
| 107 | + FIELD_PREP(MINOR_VERSION_MASK, (minor))) |
| 108 | +#define FFA_VERSION_1_0 PACK_VERSION_INFO(1, 0) |
| 109 | +#define FFA_MIN_VERSION FFA_VERSION_1_0 |
| 110 | + |
| 111 | +#define SENDER_ID_MASK GENMASK(31, 16) |
| 112 | +#define RECEIVER_ID_MASK GENMASK(15, 0) |
| 113 | +#define SENDER_ID(x) ((u16)(FIELD_GET(SENDER_ID_MASK, (x)))) |
| 114 | +#define RECEIVER_ID(x) ((u16)(FIELD_GET(RECEIVER_ID_MASK, (x)))) |
| 115 | +#define PACK_TARGET_INFO(s, r) \ |
| 116 | + (FIELD_PREP(SENDER_ID_MASK, (s)) | FIELD_PREP(RECEIVER_ID_MASK, (r))) |
| 117 | + |
| 118 | +/** |
| 119 | + * FF-A specification mentions explicitly about '4K pages'. This should |
| 120 | + * not be confused with the kernel PAGE_SIZE, which is the translation |
| 121 | + * granule kernel is configured and may be one among 4K, 16K and 64K. |
| 122 | + */ |
| 123 | +#define FFA_PAGE_SIZE SZ_4K |
| 124 | +/* |
| 125 | + * Keeping RX TX buffer size as 4K for now |
| 126 | + * 64K may be preferred to keep it min a page in 64K PAGE_SIZE config |
| 127 | + */ |
| 128 | +#define RXTX_BUFFER_SIZE SZ_4K |
| 129 | + |
| 130 | +static ffa_fn *invoke_ffa_fn; |
| 131 | + |
| 132 | +static const int ffa_linux_errmap[] = { |
| 133 | + /* better than switch case as long as return value is continuous */ |
| 134 | + 0, /* FFA_RET_SUCCESS */ |
| 135 | + -EOPNOTSUPP, /* FFA_RET_NOT_SUPPORTED */ |
| 136 | + -EINVAL, /* FFA_RET_INVALID_PARAMETERS */ |
| 137 | + -ENOMEM, /* FFA_RET_NO_MEMORY */ |
| 138 | + -EBUSY, /* FFA_RET_BUSY */ |
| 139 | + -EINTR, /* FFA_RET_INTERRUPTED */ |
| 140 | + -EACCES, /* FFA_RET_DENIED */ |
| 141 | + -EAGAIN, /* FFA_RET_RETRY */ |
| 142 | + -ECANCELED, /* FFA_RET_ABORTED */ |
| 143 | +}; |
| 144 | + |
| 145 | +static inline int ffa_to_linux_errno(int errno) |
| 146 | +{ |
| 147 | + if (errno < FFA_RET_SUCCESS && errno >= -ARRAY_SIZE(ffa_linux_errmap)) |
| 148 | + return ffa_linux_errmap[-errno]; |
| 149 | + return -EINVAL; |
| 150 | +} |
| 151 | + |
| 152 | +struct ffa_drv_info { |
| 153 | + u32 version; |
| 154 | + u16 vm_id; |
| 155 | + struct mutex rx_lock; /* lock to protect Rx buffer */ |
| 156 | + struct mutex tx_lock; /* lock to protect Tx buffer */ |
| 157 | + void *rx_buffer; |
| 158 | + void *tx_buffer; |
| 159 | +}; |
| 160 | + |
| 161 | +static struct ffa_drv_info *drv_info; |
| 162 | + |
| 163 | +static int ffa_version_check(u32 *version) |
| 164 | +{ |
| 165 | + ffa_value_t ver; |
| 166 | + |
| 167 | + invoke_ffa_fn((ffa_value_t){ |
| 168 | + .a0 = FFA_VERSION, .a1 = FFA_DRIVER_VERSION, |
| 169 | + }, &ver); |
| 170 | + |
| 171 | + if (ver.a0 == FFA_RET_NOT_SUPPORTED) { |
| 172 | + pr_info("FFA_VERSION returned not supported\n"); |
| 173 | + return -EOPNOTSUPP; |
| 174 | + } |
| 175 | + |
| 176 | + if (ver.a0 < FFA_MIN_VERSION || ver.a0 > FFA_DRIVER_VERSION) { |
| 177 | + pr_err("Incompatible version %d.%d found\n", |
| 178 | + MAJOR_VERSION(ver.a0), MINOR_VERSION(ver.a0)); |
| 179 | + return -EINVAL; |
| 180 | + } |
| 181 | + |
| 182 | + *version = ver.a0; |
| 183 | + pr_info("Version %d.%d found\n", MAJOR_VERSION(ver.a0), |
| 184 | + MINOR_VERSION(ver.a0)); |
| 185 | + return 0; |
| 186 | +} |
| 187 | + |
| 188 | +static int ffa_rxtx_map(phys_addr_t tx_buf, phys_addr_t rx_buf, u32 pg_cnt) |
| 189 | +{ |
| 190 | + ffa_value_t ret; |
| 191 | + |
| 192 | + invoke_ffa_fn((ffa_value_t){ |
| 193 | + .a0 = FFA_FN_NATIVE(RXTX_MAP), |
| 194 | + .a1 = tx_buf, .a2 = rx_buf, .a3 = pg_cnt, |
| 195 | + }, &ret); |
| 196 | + |
| 197 | + if (ret.a0 == FFA_ERROR) |
| 198 | + return ffa_to_linux_errno((int)ret.a2); |
| 199 | + |
| 200 | + return 0; |
| 201 | +} |
| 202 | + |
| 203 | +static int ffa_rxtx_unmap(u16 vm_id) |
| 204 | +{ |
| 205 | + ffa_value_t ret; |
| 206 | + |
| 207 | + invoke_ffa_fn((ffa_value_t){ |
| 208 | + .a0 = FFA_RXTX_UNMAP, .a1 = PACK_TARGET_INFO(vm_id, 0), |
| 209 | + }, &ret); |
| 210 | + |
| 211 | + if (ret.a0 == FFA_ERROR) |
| 212 | + return ffa_to_linux_errno((int)ret.a2); |
| 213 | + |
| 214 | + return 0; |
| 215 | +} |
| 216 | + |
| 217 | +#define VM_ID_MASK GENMASK(15, 0) |
| 218 | +static int ffa_id_get(u16 *vm_id) |
| 219 | +{ |
| 220 | + ffa_value_t id; |
| 221 | + |
| 222 | + invoke_ffa_fn((ffa_value_t){ |
| 223 | + .a0 = FFA_ID_GET, |
| 224 | + }, &id); |
| 225 | + |
| 226 | + if (id.a0 == FFA_ERROR) |
| 227 | + return ffa_to_linux_errno((int)id.a2); |
| 228 | + |
| 229 | + *vm_id = FIELD_GET(VM_ID_MASK, (id.a2)); |
| 230 | + |
| 231 | + return 0; |
| 232 | +} |
| 233 | + |
| 234 | +static int __init ffa_init(void) |
| 235 | +{ |
| 236 | + int ret; |
| 237 | + |
| 238 | + ret = ffa_transport_init(&invoke_ffa_fn); |
| 239 | + if (ret) |
| 240 | + return ret; |
| 241 | + |
| 242 | + ret = arm_ffa_bus_init(); |
| 243 | + if (ret) |
| 244 | + return ret; |
| 245 | + |
| 246 | + drv_info = kzalloc(sizeof(*drv_info), GFP_KERNEL); |
| 247 | + if (!drv_info) { |
| 248 | + ret = -ENOMEM; |
| 249 | + goto ffa_bus_exit; |
| 250 | + } |
| 251 | + |
| 252 | + ret = ffa_version_check(&drv_info->version); |
| 253 | + if (ret) |
| 254 | + goto free_drv_info; |
| 255 | + |
| 256 | + if (ffa_id_get(&drv_info->vm_id)) { |
| 257 | + pr_err("failed to obtain VM id for self\n"); |
| 258 | + ret = -ENODEV; |
| 259 | + goto free_drv_info; |
| 260 | + } |
| 261 | + |
| 262 | + drv_info->rx_buffer = alloc_pages_exact(RXTX_BUFFER_SIZE, GFP_KERNEL); |
| 263 | + if (!drv_info->rx_buffer) { |
| 264 | + ret = -ENOMEM; |
| 265 | + goto free_pages; |
| 266 | + } |
| 267 | + |
| 268 | + drv_info->tx_buffer = alloc_pages_exact(RXTX_BUFFER_SIZE, GFP_KERNEL); |
| 269 | + if (!drv_info->tx_buffer) { |
| 270 | + ret = -ENOMEM; |
| 271 | + goto free_pages; |
| 272 | + } |
| 273 | + |
| 274 | + ret = ffa_rxtx_map(virt_to_phys(drv_info->tx_buffer), |
| 275 | + virt_to_phys(drv_info->rx_buffer), |
| 276 | + RXTX_BUFFER_SIZE / FFA_PAGE_SIZE); |
| 277 | + if (ret) { |
| 278 | + pr_err("failed to register FFA RxTx buffers\n"); |
| 279 | + goto free_pages; |
| 280 | + } |
| 281 | + |
| 282 | + mutex_init(&drv_info->rx_lock); |
| 283 | + mutex_init(&drv_info->tx_lock); |
| 284 | + |
| 285 | + return 0; |
| 286 | +free_pages: |
| 287 | + if (drv_info->tx_buffer) |
| 288 | + free_pages_exact(drv_info->tx_buffer, RXTX_BUFFER_SIZE); |
| 289 | + free_pages_exact(drv_info->rx_buffer, RXTX_BUFFER_SIZE); |
| 290 | +free_drv_info: |
| 291 | + kfree(drv_info); |
| 292 | +ffa_bus_exit: |
| 293 | + arm_ffa_bus_exit(); |
| 294 | + return ret; |
| 295 | +} |
| 296 | +subsys_initcall(ffa_init); |
| 297 | + |
| 298 | +static void __exit ffa_exit(void) |
| 299 | +{ |
| 300 | + ffa_rxtx_unmap(drv_info->vm_id); |
| 301 | + free_pages_exact(drv_info->tx_buffer, RXTX_BUFFER_SIZE); |
| 302 | + free_pages_exact(drv_info->rx_buffer, RXTX_BUFFER_SIZE); |
| 303 | + kfree(drv_info); |
| 304 | + arm_ffa_bus_exit(); |
| 305 | +} |
| 306 | +module_exit(ffa_exit); |
| 307 | + |
| 308 | +MODULE_ALIAS("arm-ffa"); |
| 309 | +MODULE_AUTHOR( "Sudeep Holla <[email protected]>"); |
| 310 | +MODULE_DESCRIPTION("Arm FF-A interface driver"); |
| 311 | +MODULE_LICENSE("GPL v2"); |
0 commit comments