Brad Bishop | bec4ebc | 2022-08-03 09:55:16 -0400 | [diff] [blame] | 1 | From 85df9333f0adf60fd76eb5ebb21b89c5b0a86c10 Mon Sep 17 00:00:00 2001 |
| 2 | From: Sudeep Holla <sudeep.holla@arm.com> |
| 3 | Date: Tue, 18 May 2021 17:36:18 +0100 |
| 4 | Subject: [PATCH 01/32] firmware: arm_ffa: Backport of arm_ffa driver |
| 5 | |
| 6 | This is a backport of upstream ARM FFA driver from: |
| 7 | https://git.kernel.org/pub/scm/linux/kernel/git/sudeep.holla/linux.git/commit/?h=v5.10/ffa&id=c0aff30cb9ad6a00c82acef0f2a48f99adf997c0 |
| 8 | |
| 9 | to branch=android12-5.10-lts |
| 10 | |
| 11 | arm64: smccc: Add support for SMCCCv1.2 extended input/output registers |
| 12 | commit 3fdc0cb59d97f87e2cc708d424f1538e31744286 upstream. |
| 13 | |
| 14 | firmware: arm_ffa: Add initial FFA bus support for device enumeration |
| 15 | commit e781858488b918e30a6ff28e9eab6058b787e3b3 upstream. |
| 16 | |
| 17 | firmware: arm_ffa: Add initial Arm FFA driver support |
| 18 | commit 3bbfe9871005f38df2955b2e125933edf1d2feef upstream. |
| 19 | |
| 20 | firmware: arm_ffa: Add support for SMCCC as transport to FFA driver |
| 21 | commit 714be77e976a4b013b935b3223b2ef68856084d0 upstream. |
| 22 | |
| 23 | firmware: arm_ffa: Setup in-kernel users of FFA partitions |
| 24 | commit d0c0bce831223b08e5bade2cefc93c3ddb790796 upstream. |
| 25 | |
| 26 | firmware: arm_ffa: Add support for MEM_* interfaces |
| 27 | commit cc2195fe536c28e192df5d07e6dd277af36814b4 upstream. |
| 28 | |
| 29 | firmware: arm_ffa: Ensure drivers provide a probe function |
| 30 | commit 92743071464fca5acbbe812d9a0d88de3eaaad36 upstream. |
| 31 | |
| 32 | firmware: arm_ffa: Simplify probe function |
| 33 | commit e362547addc39e4bb18ad5bdfd59ce4d512d0c08 upstream. |
| 34 | |
| 35 | firmware: arm_ffa: Fix the comment style |
| 36 | commit ba684a31d3626c86cd9097e12d6ed57d224d077d upstream. |
| 37 | |
| 38 | firmware: arm_ffa: Fix a possible ffa_linux_errmap buffer overflow |
| 39 | commit dd925db6f07556061c11ab1fbfa4a0145ae6b438 upstream. |
| 40 | |
| 41 | firmware: arm_ffa: Add missing remove callback to ffa_bus_type |
| 42 | commit 244f5d597e1ea519c2085fbd9819458688775e42 upstream. |
| 43 | |
| 44 | firmware: arm_ffa: Fix __ffa_devices_unregister |
| 45 | commit eb7b52e6db7c21400b9b2d539f9343fb6e94bd94 upstream. |
| 46 | |
| 47 | firmware: arm_ffa: Handle compatibility with different firmware versions |
| 48 | commit 8e3f9da608f14cfebac2659d8dd8737b79d01308 upstream. |
| 49 | |
| 50 | firmware: arm_ffa: Add support for MEM_LEND |
| 51 | commit 82a8daaecfd9382e9450a05f86be8a274cf69a27 upstream. |
| 52 | |
| 53 | firmware: arm_ffa: Remove unused 'compat_version' variable |
| 54 | commit 01537a078b86917c7bb69aa4b756b42b980c158b upstream. |
| 55 | |
| 56 | Signed-off-by: Sudeep Holla <sudeep.holla@arm.com> |
| 57 | Change-Id: If9df40d2d10be9e3c95298820bc20c201ea1774c |
| 58 | Signed-off-by: Arunachalam Ganapathy <arunachalam.ganapathy@arm.com> |
| 59 | |
| 60 | Upstream-Status: Backport |
| 61 | Change-Id: I8e6197d8b7ef6654dacd21450069b8e284a3cec5 |
| 62 | --- |
| 63 | MAINTAINERS | 7 + |
| 64 | arch/arm64/kernel/asm-offsets.c | 9 + |
| 65 | arch/arm64/kernel/smccc-call.S | 57 +++ |
| 66 | drivers/firmware/Kconfig | 1 + |
| 67 | drivers/firmware/Makefile | 1 + |
| 68 | drivers/firmware/arm_ffa/Kconfig | 21 + |
| 69 | drivers/firmware/arm_ffa/Makefile | 6 + |
| 70 | drivers/firmware/arm_ffa/bus.c | 220 +++++++++ |
| 71 | drivers/firmware/arm_ffa/common.h | 31 ++ |
| 72 | drivers/firmware/arm_ffa/driver.c | 776 ++++++++++++++++++++++++++++++ |
| 73 | drivers/firmware/arm_ffa/smccc.c | 39 ++ |
| 74 | include/linux/arm-smccc.h | 55 +++ |
| 75 | include/linux/arm_ffa.h | 269 +++++++++++ |
| 76 | 13 files changed, 1492 insertions(+) |
| 77 | create mode 100644 drivers/firmware/arm_ffa/Kconfig |
| 78 | create mode 100644 drivers/firmware/arm_ffa/Makefile |
| 79 | create mode 100644 drivers/firmware/arm_ffa/bus.c |
| 80 | create mode 100644 drivers/firmware/arm_ffa/common.h |
| 81 | create mode 100644 drivers/firmware/arm_ffa/driver.c |
| 82 | create mode 100644 drivers/firmware/arm_ffa/smccc.c |
| 83 | create mode 100644 include/linux/arm_ffa.h |
| 84 | |
| 85 | diff --git a/MAINTAINERS b/MAINTAINERS |
| 86 | index 5234423c477a..d5fdc9e68c89 100644 |
| 87 | --- a/MAINTAINERS |
| 88 | +++ b/MAINTAINERS |
| 89 | @@ -6847,6 +6847,13 @@ F: include/linux/firewire.h |
| 90 | F: include/uapi/linux/firewire*.h |
| 91 | F: tools/firewire/ |
| 92 | |
| 93 | +FIRMWARE FRAMEWORK FOR ARMV8-A |
| 94 | +M: Sudeep Holla <sudeep.holla@arm.com> |
| 95 | +L: linux-arm-kernel@lists.infradead.org |
| 96 | +S: Maintained |
| 97 | +F: drivers/firmware/arm_ffa/ |
| 98 | +F: include/linux/arm_ffa.h |
| 99 | + |
| 100 | FIRMWARE LOADER (request_firmware) |
| 101 | M: Luis Chamberlain <mcgrof@kernel.org> |
| 102 | L: linux-kernel@vger.kernel.org |
| 103 | diff --git a/arch/arm64/kernel/asm-offsets.c b/arch/arm64/kernel/asm-offsets.c |
| 104 | index 93da876a58e6..bad4a367da28 100644 |
| 105 | --- a/arch/arm64/kernel/asm-offsets.c |
| 106 | +++ b/arch/arm64/kernel/asm-offsets.c |
| 107 | @@ -139,6 +139,15 @@ int main(void) |
| 108 | DEFINE(ARM_SMCCC_RES_X2_OFFS, offsetof(struct arm_smccc_res, a2)); |
| 109 | DEFINE(ARM_SMCCC_QUIRK_ID_OFFS, offsetof(struct arm_smccc_quirk, id)); |
| 110 | DEFINE(ARM_SMCCC_QUIRK_STATE_OFFS, offsetof(struct arm_smccc_quirk, state)); |
| 111 | + DEFINE(ARM_SMCCC_1_2_REGS_X0_OFFS, offsetof(struct arm_smccc_1_2_regs, a0)); |
| 112 | + DEFINE(ARM_SMCCC_1_2_REGS_X2_OFFS, offsetof(struct arm_smccc_1_2_regs, a2)); |
| 113 | + DEFINE(ARM_SMCCC_1_2_REGS_X4_OFFS, offsetof(struct arm_smccc_1_2_regs, a4)); |
| 114 | + DEFINE(ARM_SMCCC_1_2_REGS_X6_OFFS, offsetof(struct arm_smccc_1_2_regs, a6)); |
| 115 | + DEFINE(ARM_SMCCC_1_2_REGS_X8_OFFS, offsetof(struct arm_smccc_1_2_regs, a8)); |
| 116 | + DEFINE(ARM_SMCCC_1_2_REGS_X10_OFFS, offsetof(struct arm_smccc_1_2_regs, a10)); |
| 117 | + DEFINE(ARM_SMCCC_1_2_REGS_X12_OFFS, offsetof(struct arm_smccc_1_2_regs, a12)); |
| 118 | + DEFINE(ARM_SMCCC_1_2_REGS_X14_OFFS, offsetof(struct arm_smccc_1_2_regs, a14)); |
| 119 | + DEFINE(ARM_SMCCC_1_2_REGS_X16_OFFS, offsetof(struct arm_smccc_1_2_regs, a16)); |
| 120 | BLANK(); |
| 121 | DEFINE(HIBERN_PBE_ORIG, offsetof(struct pbe, orig_address)); |
| 122 | DEFINE(HIBERN_PBE_ADDR, offsetof(struct pbe, address)); |
| 123 | diff --git a/arch/arm64/kernel/smccc-call.S b/arch/arm64/kernel/smccc-call.S |
| 124 | index d62447964ed9..2def9d0dd3dd 100644 |
| 125 | --- a/arch/arm64/kernel/smccc-call.S |
| 126 | +++ b/arch/arm64/kernel/smccc-call.S |
| 127 | @@ -43,3 +43,60 @@ SYM_FUNC_START(__arm_smccc_hvc) |
| 128 | SMCCC hvc |
| 129 | SYM_FUNC_END(__arm_smccc_hvc) |
| 130 | EXPORT_SYMBOL(__arm_smccc_hvc) |
| 131 | + |
| 132 | + .macro SMCCC_1_2 instr |
| 133 | + /* Save `res` and free a GPR that won't be clobbered */ |
| 134 | + stp x1, x19, [sp, #-16]! |
| 135 | + |
| 136 | + /* Ensure `args` won't be clobbered while loading regs in next step */ |
| 137 | + mov x19, x0 |
| 138 | + |
| 139 | + /* Load the registers x0 - x17 from the struct arm_smccc_1_2_regs */ |
| 140 | + ldp x0, x1, [x19, #ARM_SMCCC_1_2_REGS_X0_OFFS] |
| 141 | + ldp x2, x3, [x19, #ARM_SMCCC_1_2_REGS_X2_OFFS] |
| 142 | + ldp x4, x5, [x19, #ARM_SMCCC_1_2_REGS_X4_OFFS] |
| 143 | + ldp x6, x7, [x19, #ARM_SMCCC_1_2_REGS_X6_OFFS] |
| 144 | + ldp x8, x9, [x19, #ARM_SMCCC_1_2_REGS_X8_OFFS] |
| 145 | + ldp x10, x11, [x19, #ARM_SMCCC_1_2_REGS_X10_OFFS] |
| 146 | + ldp x12, x13, [x19, #ARM_SMCCC_1_2_REGS_X12_OFFS] |
| 147 | + ldp x14, x15, [x19, #ARM_SMCCC_1_2_REGS_X14_OFFS] |
| 148 | + ldp x16, x17, [x19, #ARM_SMCCC_1_2_REGS_X16_OFFS] |
| 149 | + |
| 150 | + \instr #0 |
| 151 | + |
| 152 | + /* Load the `res` from the stack */ |
| 153 | + ldr x19, [sp] |
| 154 | + |
| 155 | + /* Store the registers x0 - x17 into the result structure */ |
| 156 | + stp x0, x1, [x19, #ARM_SMCCC_1_2_REGS_X0_OFFS] |
| 157 | + stp x2, x3, [x19, #ARM_SMCCC_1_2_REGS_X2_OFFS] |
| 158 | + stp x4, x5, [x19, #ARM_SMCCC_1_2_REGS_X4_OFFS] |
| 159 | + stp x6, x7, [x19, #ARM_SMCCC_1_2_REGS_X6_OFFS] |
| 160 | + stp x8, x9, [x19, #ARM_SMCCC_1_2_REGS_X8_OFFS] |
| 161 | + stp x10, x11, [x19, #ARM_SMCCC_1_2_REGS_X10_OFFS] |
| 162 | + stp x12, x13, [x19, #ARM_SMCCC_1_2_REGS_X12_OFFS] |
| 163 | + stp x14, x15, [x19, #ARM_SMCCC_1_2_REGS_X14_OFFS] |
| 164 | + stp x16, x17, [x19, #ARM_SMCCC_1_2_REGS_X16_OFFS] |
| 165 | + |
| 166 | + /* Restore original x19 */ |
| 167 | + ldp xzr, x19, [sp], #16 |
| 168 | + ret |
| 169 | +.endm |
| 170 | + |
| 171 | +/* |
| 172 | + * void arm_smccc_1_2_hvc(const struct arm_smccc_1_2_regs *args, |
| 173 | + * struct arm_smccc_1_2_regs *res); |
| 174 | + */ |
| 175 | +SYM_FUNC_START(arm_smccc_1_2_hvc) |
| 176 | + SMCCC_1_2 hvc |
| 177 | +SYM_FUNC_END(arm_smccc_1_2_hvc) |
| 178 | +EXPORT_SYMBOL(arm_smccc_1_2_hvc) |
| 179 | + |
| 180 | +/* |
| 181 | + * void arm_smccc_1_2_smc(const struct arm_smccc_1_2_regs *args, |
| 182 | + * struct arm_smccc_1_2_regs *res); |
| 183 | + */ |
| 184 | +SYM_FUNC_START(arm_smccc_1_2_smc) |
| 185 | + SMCCC_1_2 smc |
| 186 | +SYM_FUNC_END(arm_smccc_1_2_smc) |
| 187 | +EXPORT_SYMBOL(arm_smccc_1_2_smc) |
| 188 | diff --git a/drivers/firmware/Kconfig b/drivers/firmware/Kconfig |
| 189 | index bfef3d8d14e7..90e6dd32f2cd 100644 |
| 190 | --- a/drivers/firmware/Kconfig |
| 191 | +++ b/drivers/firmware/Kconfig |
| 192 | @@ -296,6 +296,7 @@ config TURRIS_MOX_RWTM |
| 193 | other manufacturing data and also utilize the Entropy Bit Generator |
| 194 | for hardware random number generation. |
| 195 | |
| 196 | +source "drivers/firmware/arm_ffa/Kconfig" |
| 197 | source "drivers/firmware/broadcom/Kconfig" |
| 198 | source "drivers/firmware/google/Kconfig" |
| 199 | source "drivers/firmware/efi/Kconfig" |
| 200 | diff --git a/drivers/firmware/Makefile b/drivers/firmware/Makefile |
| 201 | index 523173cbff33..3c2af2e98def 100644 |
| 202 | --- a/drivers/firmware/Makefile |
| 203 | +++ b/drivers/firmware/Makefile |
| 204 | @@ -23,6 +23,7 @@ obj-$(CONFIG_TI_SCI_PROTOCOL) += ti_sci.o |
| 205 | obj-$(CONFIG_TRUSTED_FOUNDATIONS) += trusted_foundations.o |
| 206 | obj-$(CONFIG_TURRIS_MOX_RWTM) += turris-mox-rwtm.o |
| 207 | |
| 208 | +obj-y += arm_ffa/ |
| 209 | obj-y += arm_scmi/ |
| 210 | obj-y += broadcom/ |
| 211 | obj-y += meson/ |
| 212 | diff --git a/drivers/firmware/arm_ffa/Kconfig b/drivers/firmware/arm_ffa/Kconfig |
| 213 | new file mode 100644 |
| 214 | index 000000000000..5e3ae5cf82e8 |
| 215 | --- /dev/null |
| 216 | +++ b/drivers/firmware/arm_ffa/Kconfig |
| 217 | @@ -0,0 +1,21 @@ |
| 218 | +# SPDX-License-Identifier: GPL-2.0-only |
| 219 | +config ARM_FFA_TRANSPORT |
| 220 | + tristate "Arm Firmware Framework for Armv8-A" |
| 221 | + depends on OF |
| 222 | + depends on ARM64 |
| 223 | + default n |
| 224 | + help |
| 225 | + This Firmware Framework(FF) for Arm A-profile processors describes |
| 226 | + interfaces that standardize communication between the various |
| 227 | + software images which includes communication between images in |
| 228 | + the Secure world and Normal world. It also leverages the |
| 229 | + virtualization extension to isolate software images provided |
| 230 | + by an ecosystem of vendors from each other. |
| 231 | + |
| 232 | + This driver provides interface for all the client drivers making |
| 233 | + use of the features offered by ARM FF-A. |
| 234 | + |
| 235 | +config ARM_FFA_SMCCC |
| 236 | + bool |
| 237 | + default ARM_FFA_TRANSPORT |
| 238 | + depends on ARM64 && HAVE_ARM_SMCCC_DISCOVERY |
| 239 | diff --git a/drivers/firmware/arm_ffa/Makefile b/drivers/firmware/arm_ffa/Makefile |
| 240 | new file mode 100644 |
| 241 | index 000000000000..9d9f37523200 |
| 242 | --- /dev/null |
| 243 | +++ b/drivers/firmware/arm_ffa/Makefile |
| 244 | @@ -0,0 +1,6 @@ |
| 245 | +# SPDX-License-Identifier: GPL-2.0-only |
| 246 | +ffa-bus-y = bus.o |
| 247 | +ffa-driver-y = driver.o |
| 248 | +ffa-transport-$(CONFIG_ARM_FFA_SMCCC) += smccc.o |
| 249 | +ffa-module-objs := $(ffa-bus-y) $(ffa-driver-y) $(ffa-transport-y) |
| 250 | +obj-$(CONFIG_ARM_FFA_TRANSPORT) = ffa-module.o |
| 251 | diff --git a/drivers/firmware/arm_ffa/bus.c b/drivers/firmware/arm_ffa/bus.c |
| 252 | new file mode 100644 |
| 253 | index 000000000000..fca1e311ea6c |
| 254 | --- /dev/null |
| 255 | +++ b/drivers/firmware/arm_ffa/bus.c |
| 256 | @@ -0,0 +1,220 @@ |
| 257 | +// SPDX-License-Identifier: GPL-2.0 |
| 258 | +/* |
| 259 | + * Copyright (C) 2021 ARM Ltd. |
| 260 | + */ |
| 261 | + |
| 262 | +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 263 | + |
| 264 | +#include <linux/arm_ffa.h> |
| 265 | +#include <linux/device.h> |
| 266 | +#include <linux/fs.h> |
| 267 | +#include <linux/kernel.h> |
| 268 | +#include <linux/module.h> |
| 269 | +#include <linux/slab.h> |
| 270 | +#include <linux/types.h> |
| 271 | + |
| 272 | +#include "common.h" |
| 273 | + |
| 274 | +static int ffa_device_match(struct device *dev, struct device_driver *drv) |
| 275 | +{ |
| 276 | + const struct ffa_device_id *id_table; |
| 277 | + struct ffa_device *ffa_dev; |
| 278 | + |
| 279 | + id_table = to_ffa_driver(drv)->id_table; |
| 280 | + ffa_dev = to_ffa_dev(dev); |
| 281 | + |
| 282 | + while (!uuid_is_null(&id_table->uuid)) { |
| 283 | + /* |
| 284 | + * FF-A v1.0 doesn't provide discovery of UUIDs, just the |
| 285 | + * partition IDs, so fetch the partitions IDs for this |
| 286 | + * id_table UUID and assign the UUID to the device if the |
| 287 | + * partition ID matches |
| 288 | + */ |
| 289 | + if (uuid_is_null(&ffa_dev->uuid)) |
| 290 | + ffa_device_match_uuid(ffa_dev, &id_table->uuid); |
| 291 | + |
| 292 | + if (uuid_equal(&ffa_dev->uuid, &id_table->uuid)) |
| 293 | + return 1; |
| 294 | + id_table++; |
| 295 | + } |
| 296 | + |
| 297 | + return 0; |
| 298 | +} |
| 299 | + |
| 300 | +static int ffa_device_probe(struct device *dev) |
| 301 | +{ |
| 302 | + struct ffa_driver *ffa_drv = to_ffa_driver(dev->driver); |
| 303 | + struct ffa_device *ffa_dev = to_ffa_dev(dev); |
| 304 | + |
| 305 | + return ffa_drv->probe(ffa_dev); |
| 306 | +} |
| 307 | + |
| 308 | +static int ffa_device_remove(struct device *dev) |
| 309 | +{ |
| 310 | + struct ffa_driver *ffa_drv = to_ffa_driver(dev->driver); |
| 311 | + |
| 312 | + ffa_drv->remove(to_ffa_dev(dev)); |
| 313 | + |
| 314 | + return 0; |
| 315 | +} |
| 316 | + |
| 317 | +static int ffa_device_uevent(struct device *dev, struct kobj_uevent_env *env) |
| 318 | +{ |
| 319 | + struct ffa_device *ffa_dev = to_ffa_dev(dev); |
| 320 | + |
| 321 | + return add_uevent_var(env, "MODALIAS=arm_ffa:%04x:%pUb", |
| 322 | + ffa_dev->vm_id, &ffa_dev->uuid); |
| 323 | +} |
| 324 | + |
| 325 | +static ssize_t partition_id_show(struct device *dev, |
| 326 | + struct device_attribute *attr, char *buf) |
| 327 | +{ |
| 328 | + struct ffa_device *ffa_dev = to_ffa_dev(dev); |
| 329 | + |
| 330 | + return sprintf(buf, "0x%04x\n", ffa_dev->vm_id); |
| 331 | +} |
| 332 | +static DEVICE_ATTR_RO(partition_id); |
| 333 | + |
| 334 | +static ssize_t uuid_show(struct device *dev, struct device_attribute *attr, |
| 335 | + char *buf) |
| 336 | +{ |
| 337 | + struct ffa_device *ffa_dev = to_ffa_dev(dev); |
| 338 | + |
| 339 | + return sprintf(buf, "%pUb\n", &ffa_dev->uuid); |
| 340 | +} |
| 341 | +static DEVICE_ATTR_RO(uuid); |
| 342 | + |
| 343 | +static struct attribute *ffa_device_attributes_attrs[] = { |
| 344 | + &dev_attr_partition_id.attr, |
| 345 | + &dev_attr_uuid.attr, |
| 346 | + NULL, |
| 347 | +}; |
| 348 | +ATTRIBUTE_GROUPS(ffa_device_attributes); |
| 349 | + |
| 350 | +struct bus_type ffa_bus_type = { |
| 351 | + .name = "arm_ffa", |
| 352 | + .match = ffa_device_match, |
| 353 | + .probe = ffa_device_probe, |
| 354 | + .remove = ffa_device_remove, |
| 355 | + .uevent = ffa_device_uevent, |
| 356 | + .dev_groups = ffa_device_attributes_groups, |
| 357 | +}; |
| 358 | +EXPORT_SYMBOL_GPL(ffa_bus_type); |
| 359 | + |
| 360 | +int ffa_driver_register(struct ffa_driver *driver, struct module *owner, |
| 361 | + const char *mod_name) |
| 362 | +{ |
| 363 | + int ret; |
| 364 | + |
| 365 | + if (!driver->probe) |
| 366 | + return -EINVAL; |
| 367 | + |
| 368 | + driver->driver.bus = &ffa_bus_type; |
| 369 | + driver->driver.name = driver->name; |
| 370 | + driver->driver.owner = owner; |
| 371 | + driver->driver.mod_name = mod_name; |
| 372 | + |
| 373 | + ret = driver_register(&driver->driver); |
| 374 | + if (!ret) |
| 375 | + pr_debug("registered new ffa driver %s\n", driver->name); |
| 376 | + |
| 377 | + return ret; |
| 378 | +} |
| 379 | +EXPORT_SYMBOL_GPL(ffa_driver_register); |
| 380 | + |
| 381 | +void ffa_driver_unregister(struct ffa_driver *driver) |
| 382 | +{ |
| 383 | + driver_unregister(&driver->driver); |
| 384 | +} |
| 385 | +EXPORT_SYMBOL_GPL(ffa_driver_unregister); |
| 386 | + |
| 387 | +static void ffa_release_device(struct device *dev) |
| 388 | +{ |
| 389 | + struct ffa_device *ffa_dev = to_ffa_dev(dev); |
| 390 | + |
| 391 | + kfree(ffa_dev); |
| 392 | +} |
| 393 | + |
| 394 | +static int __ffa_devices_unregister(struct device *dev, void *data) |
| 395 | +{ |
| 396 | + device_unregister(dev); |
| 397 | + |
| 398 | + return 0; |
| 399 | +} |
| 400 | + |
| 401 | +static void ffa_devices_unregister(void) |
| 402 | +{ |
| 403 | + bus_for_each_dev(&ffa_bus_type, NULL, NULL, |
| 404 | + __ffa_devices_unregister); |
| 405 | +} |
| 406 | + |
| 407 | +bool ffa_device_is_valid(struct ffa_device *ffa_dev) |
| 408 | +{ |
| 409 | + bool valid = false; |
| 410 | + struct device *dev = NULL; |
| 411 | + struct ffa_device *tmp_dev; |
| 412 | + |
| 413 | + do { |
| 414 | + dev = bus_find_next_device(&ffa_bus_type, dev); |
| 415 | + tmp_dev = to_ffa_dev(dev); |
| 416 | + if (tmp_dev == ffa_dev) { |
| 417 | + valid = true; |
| 418 | + break; |
| 419 | + } |
| 420 | + put_device(dev); |
| 421 | + } while (dev); |
| 422 | + |
| 423 | + put_device(dev); |
| 424 | + |
| 425 | + return valid; |
| 426 | +} |
| 427 | + |
| 428 | +struct ffa_device *ffa_device_register(const uuid_t *uuid, int vm_id) |
| 429 | +{ |
| 430 | + int ret; |
| 431 | + struct device *dev; |
| 432 | + struct ffa_device *ffa_dev; |
| 433 | + |
| 434 | + ffa_dev = kzalloc(sizeof(*ffa_dev), GFP_KERNEL); |
| 435 | + if (!ffa_dev) |
| 436 | + return NULL; |
| 437 | + |
| 438 | + dev = &ffa_dev->dev; |
| 439 | + dev->bus = &ffa_bus_type; |
| 440 | + dev->release = ffa_release_device; |
| 441 | + dev_set_name(&ffa_dev->dev, "arm-ffa-%04x", vm_id); |
| 442 | + |
| 443 | + ffa_dev->vm_id = vm_id; |
| 444 | + uuid_copy(&ffa_dev->uuid, uuid); |
| 445 | + |
| 446 | + ret = device_register(&ffa_dev->dev); |
| 447 | + if (ret) { |
| 448 | + dev_err(dev, "unable to register device %s err=%d\n", |
| 449 | + dev_name(dev), ret); |
| 450 | + put_device(dev); |
| 451 | + return NULL; |
| 452 | + } |
| 453 | + |
| 454 | + return ffa_dev; |
| 455 | +} |
| 456 | +EXPORT_SYMBOL_GPL(ffa_device_register); |
| 457 | + |
| 458 | +void ffa_device_unregister(struct ffa_device *ffa_dev) |
| 459 | +{ |
| 460 | + if (!ffa_dev) |
| 461 | + return; |
| 462 | + |
| 463 | + device_unregister(&ffa_dev->dev); |
| 464 | +} |
| 465 | +EXPORT_SYMBOL_GPL(ffa_device_unregister); |
| 466 | + |
| 467 | +int arm_ffa_bus_init(void) |
| 468 | +{ |
| 469 | + return bus_register(&ffa_bus_type); |
| 470 | +} |
| 471 | + |
| 472 | +void arm_ffa_bus_exit(void) |
| 473 | +{ |
| 474 | + ffa_devices_unregister(); |
| 475 | + bus_unregister(&ffa_bus_type); |
| 476 | +} |
| 477 | diff --git a/drivers/firmware/arm_ffa/common.h b/drivers/firmware/arm_ffa/common.h |
| 478 | new file mode 100644 |
| 479 | index 000000000000..d6eccf1fd3f6 |
| 480 | --- /dev/null |
| 481 | +++ b/drivers/firmware/arm_ffa/common.h |
| 482 | @@ -0,0 +1,31 @@ |
| 483 | +/* SPDX-License-Identifier: GPL-2.0 */ |
| 484 | +/* |
| 485 | + * Copyright (C) 2021 ARM Ltd. |
| 486 | + */ |
| 487 | + |
| 488 | +#ifndef _FFA_COMMON_H |
| 489 | +#define _FFA_COMMON_H |
| 490 | + |
| 491 | +#include <linux/arm_ffa.h> |
| 492 | +#include <linux/arm-smccc.h> |
| 493 | +#include <linux/err.h> |
| 494 | + |
| 495 | +typedef struct arm_smccc_1_2_regs ffa_value_t; |
| 496 | + |
| 497 | +typedef void (ffa_fn)(ffa_value_t, ffa_value_t *); |
| 498 | + |
| 499 | +int arm_ffa_bus_init(void); |
| 500 | +void arm_ffa_bus_exit(void); |
| 501 | +bool ffa_device_is_valid(struct ffa_device *ffa_dev); |
| 502 | +void ffa_device_match_uuid(struct ffa_device *ffa_dev, const uuid_t *uuid); |
| 503 | + |
| 504 | +#ifdef CONFIG_ARM_FFA_SMCCC |
| 505 | +int __init ffa_transport_init(ffa_fn **invoke_ffa_fn); |
| 506 | +#else |
| 507 | +static inline int __init ffa_transport_init(ffa_fn **invoke_ffa_fn) |
| 508 | +{ |
| 509 | + return -EOPNOTSUPP; |
| 510 | +} |
| 511 | +#endif |
| 512 | + |
| 513 | +#endif /* _FFA_COMMON_H */ |
| 514 | diff --git a/drivers/firmware/arm_ffa/driver.c b/drivers/firmware/arm_ffa/driver.c |
| 515 | new file mode 100644 |
| 516 | index 000000000000..14f900047ac0 |
| 517 | --- /dev/null |
| 518 | +++ b/drivers/firmware/arm_ffa/driver.c |
| 519 | @@ -0,0 +1,776 @@ |
| 520 | +// SPDX-License-Identifier: GPL-2.0-only |
| 521 | +/* |
| 522 | + * Arm Firmware Framework for ARMv8-A(FFA) interface driver |
| 523 | + * |
| 524 | + * The Arm FFA specification[1] describes a software architecture to |
| 525 | + * leverages the virtualization extension to isolate software images |
| 526 | + * provided by an ecosystem of vendors from each other and describes |
| 527 | + * interfaces that standardize communication between the various software |
| 528 | + * images including communication between images in the Secure world and |
| 529 | + * Normal world. Any Hypervisor could use the FFA interfaces to enable |
| 530 | + * communication between VMs it manages. |
| 531 | + * |
| 532 | + * The Hypervisor a.k.a Partition managers in FFA terminology can assign |
| 533 | + * system resources(Memory regions, Devices, CPU cycles) to the partitions |
| 534 | + * and manage isolation amongst them. |
| 535 | + * |
| 536 | + * [1] https://developer.arm.com/docs/den0077/latest |
| 537 | + * |
| 538 | + * Copyright (C) 2021 ARM Ltd. |
| 539 | + */ |
| 540 | + |
| 541 | +#define DRIVER_NAME "ARM FF-A" |
| 542 | +#define pr_fmt(fmt) DRIVER_NAME ": " fmt |
| 543 | + |
| 544 | +#include <linux/arm_ffa.h> |
| 545 | +#include <linux/bitfield.h> |
| 546 | +#include <linux/device.h> |
| 547 | +#include <linux/io.h> |
| 548 | +#include <linux/kernel.h> |
| 549 | +#include <linux/module.h> |
| 550 | +#include <linux/mm.h> |
| 551 | +#include <linux/scatterlist.h> |
| 552 | +#include <linux/slab.h> |
| 553 | +#include <linux/uuid.h> |
| 554 | + |
| 555 | +#include "common.h" |
| 556 | + |
| 557 | +#define FFA_DRIVER_VERSION FFA_VERSION_1_0 |
| 558 | + |
| 559 | +#define FFA_SMC(calling_convention, func_num) \ |
| 560 | + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, (calling_convention), \ |
| 561 | + ARM_SMCCC_OWNER_STANDARD, (func_num)) |
| 562 | + |
| 563 | +#define FFA_SMC_32(func_num) FFA_SMC(ARM_SMCCC_SMC_32, (func_num)) |
| 564 | +#define FFA_SMC_64(func_num) FFA_SMC(ARM_SMCCC_SMC_64, (func_num)) |
| 565 | + |
| 566 | +#define FFA_ERROR FFA_SMC_32(0x60) |
| 567 | +#define FFA_SUCCESS FFA_SMC_32(0x61) |
| 568 | +#define FFA_INTERRUPT FFA_SMC_32(0x62) |
| 569 | +#define FFA_VERSION FFA_SMC_32(0x63) |
| 570 | +#define FFA_FEATURES FFA_SMC_32(0x64) |
| 571 | +#define FFA_RX_RELEASE FFA_SMC_32(0x65) |
| 572 | +#define FFA_RXTX_MAP FFA_SMC_32(0x66) |
| 573 | +#define FFA_FN64_RXTX_MAP FFA_SMC_64(0x66) |
| 574 | +#define FFA_RXTX_UNMAP FFA_SMC_32(0x67) |
| 575 | +#define FFA_PARTITION_INFO_GET FFA_SMC_32(0x68) |
| 576 | +#define FFA_ID_GET FFA_SMC_32(0x69) |
| 577 | +#define FFA_MSG_POLL FFA_SMC_32(0x6A) |
| 578 | +#define FFA_MSG_WAIT FFA_SMC_32(0x6B) |
| 579 | +#define FFA_YIELD FFA_SMC_32(0x6C) |
| 580 | +#define FFA_RUN FFA_SMC_32(0x6D) |
| 581 | +#define FFA_MSG_SEND FFA_SMC_32(0x6E) |
| 582 | +#define FFA_MSG_SEND_DIRECT_REQ FFA_SMC_32(0x6F) |
| 583 | +#define FFA_FN64_MSG_SEND_DIRECT_REQ FFA_SMC_64(0x6F) |
| 584 | +#define FFA_MSG_SEND_DIRECT_RESP FFA_SMC_32(0x70) |
| 585 | +#define FFA_FN64_MSG_SEND_DIRECT_RESP FFA_SMC_64(0x70) |
| 586 | +#define FFA_MEM_DONATE FFA_SMC_32(0x71) |
| 587 | +#define FFA_FN64_MEM_DONATE FFA_SMC_64(0x71) |
| 588 | +#define FFA_MEM_LEND FFA_SMC_32(0x72) |
| 589 | +#define FFA_FN64_MEM_LEND FFA_SMC_64(0x72) |
| 590 | +#define FFA_MEM_SHARE FFA_SMC_32(0x73) |
| 591 | +#define FFA_FN64_MEM_SHARE FFA_SMC_64(0x73) |
| 592 | +#define FFA_MEM_RETRIEVE_REQ FFA_SMC_32(0x74) |
| 593 | +#define FFA_FN64_MEM_RETRIEVE_REQ FFA_SMC_64(0x74) |
| 594 | +#define FFA_MEM_RETRIEVE_RESP FFA_SMC_32(0x75) |
| 595 | +#define FFA_MEM_RELINQUISH FFA_SMC_32(0x76) |
| 596 | +#define FFA_MEM_RECLAIM FFA_SMC_32(0x77) |
| 597 | +#define FFA_MEM_OP_PAUSE FFA_SMC_32(0x78) |
| 598 | +#define FFA_MEM_OP_RESUME FFA_SMC_32(0x79) |
| 599 | +#define FFA_MEM_FRAG_RX FFA_SMC_32(0x7A) |
| 600 | +#define FFA_MEM_FRAG_TX FFA_SMC_32(0x7B) |
| 601 | +#define FFA_NORMAL_WORLD_RESUME FFA_SMC_32(0x7C) |
| 602 | + |
| 603 | +/* |
| 604 | + * For some calls it is necessary to use SMC64 to pass or return 64-bit values. |
| 605 | + * For such calls FFA_FN_NATIVE(name) will choose the appropriate |
| 606 | + * (native-width) function ID. |
| 607 | + */ |
| 608 | +#ifdef CONFIG_64BIT |
| 609 | +#define FFA_FN_NATIVE(name) FFA_FN64_##name |
| 610 | +#else |
| 611 | +#define FFA_FN_NATIVE(name) FFA_##name |
| 612 | +#endif |
| 613 | + |
| 614 | +/* FFA error codes. */ |
| 615 | +#define FFA_RET_SUCCESS (0) |
| 616 | +#define FFA_RET_NOT_SUPPORTED (-1) |
| 617 | +#define FFA_RET_INVALID_PARAMETERS (-2) |
| 618 | +#define FFA_RET_NO_MEMORY (-3) |
| 619 | +#define FFA_RET_BUSY (-4) |
| 620 | +#define FFA_RET_INTERRUPTED (-5) |
| 621 | +#define FFA_RET_DENIED (-6) |
| 622 | +#define FFA_RET_RETRY (-7) |
| 623 | +#define FFA_RET_ABORTED (-8) |
| 624 | + |
| 625 | +#define MAJOR_VERSION_MASK GENMASK(30, 16) |
| 626 | +#define MINOR_VERSION_MASK GENMASK(15, 0) |
| 627 | +#define MAJOR_VERSION(x) ((u16)(FIELD_GET(MAJOR_VERSION_MASK, (x)))) |
| 628 | +#define MINOR_VERSION(x) ((u16)(FIELD_GET(MINOR_VERSION_MASK, (x)))) |
| 629 | +#define PACK_VERSION_INFO(major, minor) \ |
| 630 | + (FIELD_PREP(MAJOR_VERSION_MASK, (major)) | \ |
| 631 | + FIELD_PREP(MINOR_VERSION_MASK, (minor))) |
| 632 | +#define FFA_VERSION_1_0 PACK_VERSION_INFO(1, 0) |
| 633 | +#define FFA_MIN_VERSION FFA_VERSION_1_0 |
| 634 | + |
| 635 | +#define SENDER_ID_MASK GENMASK(31, 16) |
| 636 | +#define RECEIVER_ID_MASK GENMASK(15, 0) |
| 637 | +#define SENDER_ID(x) ((u16)(FIELD_GET(SENDER_ID_MASK, (x)))) |
| 638 | +#define RECEIVER_ID(x) ((u16)(FIELD_GET(RECEIVER_ID_MASK, (x)))) |
| 639 | +#define PACK_TARGET_INFO(s, r) \ |
| 640 | + (FIELD_PREP(SENDER_ID_MASK, (s)) | FIELD_PREP(RECEIVER_ID_MASK, (r))) |
| 641 | + |
| 642 | +/* |
| 643 | + * FF-A specification mentions explicitly about '4K pages'. This should |
| 644 | + * not be confused with the kernel PAGE_SIZE, which is the translation |
| 645 | + * granule kernel is configured and may be one among 4K, 16K and 64K. |
| 646 | + */ |
| 647 | +#define FFA_PAGE_SIZE SZ_4K |
| 648 | +/* |
| 649 | + * Keeping RX TX buffer size as 4K for now |
| 650 | + * 64K may be preferred to keep it min a page in 64K PAGE_SIZE config |
| 651 | + */ |
| 652 | +#define RXTX_BUFFER_SIZE SZ_4K |
| 653 | + |
| 654 | +static ffa_fn *invoke_ffa_fn; |
| 655 | + |
| 656 | +static const int ffa_linux_errmap[] = { |
| 657 | + /* better than switch case as long as return value is continuous */ |
| 658 | + 0, /* FFA_RET_SUCCESS */ |
| 659 | + -EOPNOTSUPP, /* FFA_RET_NOT_SUPPORTED */ |
| 660 | + -EINVAL, /* FFA_RET_INVALID_PARAMETERS */ |
| 661 | + -ENOMEM, /* FFA_RET_NO_MEMORY */ |
| 662 | + -EBUSY, /* FFA_RET_BUSY */ |
| 663 | + -EINTR, /* FFA_RET_INTERRUPTED */ |
| 664 | + -EACCES, /* FFA_RET_DENIED */ |
| 665 | + -EAGAIN, /* FFA_RET_RETRY */ |
| 666 | + -ECANCELED, /* FFA_RET_ABORTED */ |
| 667 | +}; |
| 668 | + |
| 669 | +static inline int ffa_to_linux_errno(int errno) |
| 670 | +{ |
| 671 | + int err_idx = -errno; |
| 672 | + |
| 673 | + if (err_idx >= 0 && err_idx < ARRAY_SIZE(ffa_linux_errmap)) |
| 674 | + return ffa_linux_errmap[err_idx]; |
| 675 | + return -EINVAL; |
| 676 | +} |
| 677 | + |
| 678 | +struct ffa_drv_info { |
| 679 | + u32 version; |
| 680 | + u16 vm_id; |
| 681 | + struct mutex rx_lock; /* lock to protect Rx buffer */ |
| 682 | + struct mutex tx_lock; /* lock to protect Tx buffer */ |
| 683 | + void *rx_buffer; |
| 684 | + void *tx_buffer; |
| 685 | +}; |
| 686 | + |
| 687 | +static struct ffa_drv_info *drv_info; |
| 688 | + |
| 689 | +/* |
| 690 | + * The driver must be able to support all the versions from the earliest |
| 691 | + * supported FFA_MIN_VERSION to the latest supported FFA_DRIVER_VERSION. |
| 692 | + * The specification states that if firmware supports a FFA implementation |
| 693 | + * that is incompatible with and at a greater version number than specified |
| 694 | + * by the caller(FFA_DRIVER_VERSION passed as parameter to FFA_VERSION), |
| 695 | + * it must return the NOT_SUPPORTED error code. |
| 696 | + */ |
| 697 | +static u32 ffa_compatible_version_find(u32 version) |
| 698 | +{ |
| 699 | + u16 major = MAJOR_VERSION(version), minor = MINOR_VERSION(version); |
| 700 | + u16 drv_major = MAJOR_VERSION(FFA_DRIVER_VERSION); |
| 701 | + u16 drv_minor = MINOR_VERSION(FFA_DRIVER_VERSION); |
| 702 | + |
| 703 | + if ((major < drv_major) || (major == drv_major && minor <= drv_minor)) |
| 704 | + return version; |
| 705 | + |
| 706 | + pr_info("Firmware version higher than driver version, downgrading\n"); |
| 707 | + return FFA_DRIVER_VERSION; |
| 708 | +} |
| 709 | + |
| 710 | +static int ffa_version_check(u32 *version) |
| 711 | +{ |
| 712 | + ffa_value_t ver; |
| 713 | + |
| 714 | + invoke_ffa_fn((ffa_value_t){ |
| 715 | + .a0 = FFA_VERSION, .a1 = FFA_DRIVER_VERSION, |
| 716 | + }, &ver); |
| 717 | + |
| 718 | + if (ver.a0 == FFA_RET_NOT_SUPPORTED) { |
| 719 | + pr_info("FFA_VERSION returned not supported\n"); |
| 720 | + return -EOPNOTSUPP; |
| 721 | + } |
| 722 | + |
| 723 | + if (ver.a0 < FFA_MIN_VERSION) { |
| 724 | + pr_err("Incompatible v%d.%d! Earliest supported v%d.%d\n", |
| 725 | + MAJOR_VERSION(ver.a0), MINOR_VERSION(ver.a0), |
| 726 | + MAJOR_VERSION(FFA_MIN_VERSION), |
| 727 | + MINOR_VERSION(FFA_MIN_VERSION)); |
| 728 | + return -EINVAL; |
| 729 | + } |
| 730 | + |
| 731 | + pr_info("Driver version %d.%d\n", MAJOR_VERSION(FFA_DRIVER_VERSION), |
| 732 | + MINOR_VERSION(FFA_DRIVER_VERSION)); |
| 733 | + pr_info("Firmware version %d.%d found\n", MAJOR_VERSION(ver.a0), |
| 734 | + MINOR_VERSION(ver.a0)); |
| 735 | + *version = ffa_compatible_version_find(ver.a0); |
| 736 | + |
| 737 | + return 0; |
| 738 | +} |
| 739 | + |
| 740 | +static int ffa_rx_release(void) |
| 741 | +{ |
| 742 | + ffa_value_t ret; |
| 743 | + |
| 744 | + invoke_ffa_fn((ffa_value_t){ |
| 745 | + .a0 = FFA_RX_RELEASE, |
| 746 | + }, &ret); |
| 747 | + |
| 748 | + if (ret.a0 == FFA_ERROR) |
| 749 | + return ffa_to_linux_errno((int)ret.a2); |
| 750 | + |
| 751 | + /* check for ret.a0 == FFA_RX_RELEASE ? */ |
| 752 | + |
| 753 | + return 0; |
| 754 | +} |
| 755 | + |
| 756 | +static int ffa_rxtx_map(phys_addr_t tx_buf, phys_addr_t rx_buf, u32 pg_cnt) |
| 757 | +{ |
| 758 | + ffa_value_t ret; |
| 759 | + |
| 760 | + invoke_ffa_fn((ffa_value_t){ |
| 761 | + .a0 = FFA_FN_NATIVE(RXTX_MAP), |
| 762 | + .a1 = tx_buf, .a2 = rx_buf, .a3 = pg_cnt, |
| 763 | + }, &ret); |
| 764 | + |
| 765 | + if (ret.a0 == FFA_ERROR) |
| 766 | + return ffa_to_linux_errno((int)ret.a2); |
| 767 | + |
| 768 | + return 0; |
| 769 | +} |
| 770 | + |
| 771 | +static int ffa_rxtx_unmap(u16 vm_id) |
| 772 | +{ |
| 773 | + ffa_value_t ret; |
| 774 | + |
| 775 | + invoke_ffa_fn((ffa_value_t){ |
| 776 | + .a0 = FFA_RXTX_UNMAP, .a1 = PACK_TARGET_INFO(vm_id, 0), |
| 777 | + }, &ret); |
| 778 | + |
| 779 | + if (ret.a0 == FFA_ERROR) |
| 780 | + return ffa_to_linux_errno((int)ret.a2); |
| 781 | + |
| 782 | + return 0; |
| 783 | +} |
| 784 | + |
| 785 | +/* buffer must be sizeof(struct ffa_partition_info) * num_partitions */ |
| 786 | +static int |
| 787 | +__ffa_partition_info_get(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3, |
| 788 | + struct ffa_partition_info *buffer, int num_partitions) |
| 789 | +{ |
| 790 | + int count; |
| 791 | + ffa_value_t partition_info; |
| 792 | + |
| 793 | + mutex_lock(&drv_info->rx_lock); |
| 794 | + invoke_ffa_fn((ffa_value_t){ |
| 795 | + .a0 = FFA_PARTITION_INFO_GET, |
| 796 | + .a1 = uuid0, .a2 = uuid1, .a3 = uuid2, .a4 = uuid3, |
| 797 | + }, &partition_info); |
| 798 | + |
| 799 | + if (partition_info.a0 == FFA_ERROR) { |
| 800 | + mutex_unlock(&drv_info->rx_lock); |
| 801 | + return ffa_to_linux_errno((int)partition_info.a2); |
| 802 | + } |
| 803 | + |
| 804 | + count = partition_info.a2; |
| 805 | + |
| 806 | + if (buffer && count <= num_partitions) |
| 807 | + memcpy(buffer, drv_info->rx_buffer, sizeof(*buffer) * count); |
| 808 | + |
| 809 | + ffa_rx_release(); |
| 810 | + |
| 811 | + mutex_unlock(&drv_info->rx_lock); |
| 812 | + |
| 813 | + return count; |
| 814 | +} |
| 815 | + |
| 816 | +/* buffer is allocated and caller must free the same if returned count > 0 */ |
| 817 | +static int |
| 818 | +ffa_partition_probe(const uuid_t *uuid, struct ffa_partition_info **buffer) |
| 819 | +{ |
| 820 | + int count; |
| 821 | + u32 uuid0_4[4]; |
| 822 | + struct ffa_partition_info *pbuf; |
| 823 | + |
| 824 | + export_uuid((u8 *)uuid0_4, uuid); |
| 825 | + count = __ffa_partition_info_get(uuid0_4[0], uuid0_4[1], uuid0_4[2], |
| 826 | + uuid0_4[3], NULL, 0); |
| 827 | + if (count <= 0) |
| 828 | + return count; |
| 829 | + |
| 830 | + pbuf = kcalloc(count, sizeof(*pbuf), GFP_KERNEL); |
| 831 | + if (!pbuf) |
| 832 | + return -ENOMEM; |
| 833 | + |
| 834 | + count = __ffa_partition_info_get(uuid0_4[0], uuid0_4[1], uuid0_4[2], |
| 835 | + uuid0_4[3], pbuf, count); |
| 836 | + if (count <= 0) |
| 837 | + kfree(pbuf); |
| 838 | + else |
| 839 | + *buffer = pbuf; |
| 840 | + |
| 841 | + return count; |
| 842 | +} |
| 843 | + |
| 844 | +#define VM_ID_MASK GENMASK(15, 0) |
| 845 | +static int ffa_id_get(u16 *vm_id) |
| 846 | +{ |
| 847 | + ffa_value_t id; |
| 848 | + |
| 849 | + invoke_ffa_fn((ffa_value_t){ |
| 850 | + .a0 = FFA_ID_GET, |
| 851 | + }, &id); |
| 852 | + |
| 853 | + if (id.a0 == FFA_ERROR) |
| 854 | + return ffa_to_linux_errno((int)id.a2); |
| 855 | + |
| 856 | + *vm_id = FIELD_GET(VM_ID_MASK, (id.a2)); |
| 857 | + |
| 858 | + return 0; |
| 859 | +} |
| 860 | + |
| 861 | +static int ffa_msg_send_direct_req(u16 src_id, u16 dst_id, bool mode_32bit, |
| 862 | + struct ffa_send_direct_data *data) |
| 863 | +{ |
| 864 | + u32 req_id, resp_id, src_dst_ids = PACK_TARGET_INFO(src_id, dst_id); |
| 865 | + ffa_value_t ret; |
| 866 | + |
| 867 | + if (mode_32bit) { |
| 868 | + req_id = FFA_MSG_SEND_DIRECT_REQ; |
| 869 | + resp_id = FFA_MSG_SEND_DIRECT_RESP; |
| 870 | + } else { |
| 871 | + req_id = FFA_FN_NATIVE(MSG_SEND_DIRECT_REQ); |
| 872 | + resp_id = FFA_FN_NATIVE(MSG_SEND_DIRECT_RESP); |
| 873 | + } |
| 874 | + |
| 875 | + invoke_ffa_fn((ffa_value_t){ |
| 876 | + .a0 = req_id, .a1 = src_dst_ids, .a2 = 0, |
| 877 | + .a3 = data->data0, .a4 = data->data1, .a5 = data->data2, |
| 878 | + .a6 = data->data3, .a7 = data->data4, |
| 879 | + }, &ret); |
| 880 | + |
| 881 | + while (ret.a0 == FFA_INTERRUPT) |
| 882 | + invoke_ffa_fn((ffa_value_t){ |
| 883 | + .a0 = FFA_RUN, .a1 = ret.a1, |
| 884 | + }, &ret); |
| 885 | + |
| 886 | + if (ret.a0 == FFA_ERROR) |
| 887 | + return ffa_to_linux_errno((int)ret.a2); |
| 888 | + |
| 889 | + if (ret.a0 == resp_id) { |
| 890 | + data->data0 = ret.a3; |
| 891 | + data->data1 = ret.a4; |
| 892 | + data->data2 = ret.a5; |
| 893 | + data->data3 = ret.a6; |
| 894 | + data->data4 = ret.a7; |
| 895 | + return 0; |
| 896 | + } |
| 897 | + |
| 898 | + return -EINVAL; |
| 899 | +} |
| 900 | + |
| 901 | +static int ffa_mem_first_frag(u32 func_id, phys_addr_t buf, u32 buf_sz, |
| 902 | + u32 frag_len, u32 len, u64 *handle) |
| 903 | +{ |
| 904 | + ffa_value_t ret; |
| 905 | + |
| 906 | + invoke_ffa_fn((ffa_value_t){ |
| 907 | + .a0 = func_id, .a1 = len, .a2 = frag_len, |
| 908 | + .a3 = buf, .a4 = buf_sz, |
| 909 | + }, &ret); |
| 910 | + |
| 911 | + while (ret.a0 == FFA_MEM_OP_PAUSE) |
| 912 | + invoke_ffa_fn((ffa_value_t){ |
| 913 | + .a0 = FFA_MEM_OP_RESUME, |
| 914 | + .a1 = ret.a1, .a2 = ret.a2, |
| 915 | + }, &ret); |
| 916 | + |
| 917 | + if (ret.a0 == FFA_ERROR) |
| 918 | + return ffa_to_linux_errno((int)ret.a2); |
| 919 | + |
| 920 | + if (ret.a0 != FFA_SUCCESS) |
| 921 | + return -EOPNOTSUPP; |
| 922 | + |
| 923 | + if (handle) |
| 924 | + *handle = PACK_HANDLE(ret.a2, ret.a3); |
| 925 | + |
| 926 | + return frag_len; |
| 927 | +} |
| 928 | + |
| 929 | +static int ffa_mem_next_frag(u64 handle, u32 frag_len) |
| 930 | +{ |
| 931 | + ffa_value_t ret; |
| 932 | + |
| 933 | + invoke_ffa_fn((ffa_value_t){ |
| 934 | + .a0 = FFA_MEM_FRAG_TX, |
| 935 | + .a1 = HANDLE_LOW(handle), .a2 = HANDLE_HIGH(handle), |
| 936 | + .a3 = frag_len, |
| 937 | + }, &ret); |
| 938 | + |
| 939 | + while (ret.a0 == FFA_MEM_OP_PAUSE) |
| 940 | + invoke_ffa_fn((ffa_value_t){ |
| 941 | + .a0 = FFA_MEM_OP_RESUME, |
| 942 | + .a1 = ret.a1, .a2 = ret.a2, |
| 943 | + }, &ret); |
| 944 | + |
| 945 | + if (ret.a0 == FFA_ERROR) |
| 946 | + return ffa_to_linux_errno((int)ret.a2); |
| 947 | + |
| 948 | + if (ret.a0 != FFA_MEM_FRAG_RX) |
| 949 | + return -EOPNOTSUPP; |
| 950 | + |
| 951 | + return ret.a3; |
| 952 | +} |
| 953 | + |
| 954 | +static int |
| 955 | +ffa_transmit_fragment(u32 func_id, phys_addr_t buf, u32 buf_sz, u32 frag_len, |
| 956 | + u32 len, u64 *handle, bool first) |
| 957 | +{ |
| 958 | + if (!first) |
| 959 | + return ffa_mem_next_frag(*handle, frag_len); |
| 960 | + |
| 961 | + return ffa_mem_first_frag(func_id, buf, buf_sz, frag_len, len, handle); |
| 962 | +} |
| 963 | + |
| 964 | +static u32 ffa_get_num_pages_sg(struct scatterlist *sg) |
| 965 | +{ |
| 966 | + u32 num_pages = 0; |
| 967 | + |
| 968 | + do { |
| 969 | + num_pages += sg->length / FFA_PAGE_SIZE; |
| 970 | + } while ((sg = sg_next(sg))); |
| 971 | + |
| 972 | + return num_pages; |
| 973 | +} |
| 974 | + |
| 975 | +static int |
| 976 | +ffa_setup_and_transmit(u32 func_id, void *buffer, u32 max_fragsize, |
| 977 | + struct ffa_mem_ops_args *args) |
| 978 | +{ |
| 979 | + int rc = 0; |
| 980 | + bool first = true; |
| 981 | + phys_addr_t addr = 0; |
| 982 | + struct ffa_composite_mem_region *composite; |
| 983 | + struct ffa_mem_region_addr_range *constituents; |
| 984 | + struct ffa_mem_region_attributes *ep_mem_access; |
| 985 | + struct ffa_mem_region *mem_region = buffer; |
| 986 | + u32 idx, frag_len, length, buf_sz = 0, num_entries = sg_nents(args->sg); |
| 987 | + |
| 988 | + mem_region->tag = args->tag; |
| 989 | + mem_region->flags = args->flags; |
| 990 | + mem_region->sender_id = drv_info->vm_id; |
| 991 | + mem_region->attributes = FFA_MEM_NORMAL | FFA_MEM_WRITE_BACK | |
| 992 | + FFA_MEM_INNER_SHAREABLE; |
| 993 | + ep_mem_access = &mem_region->ep_mem_access[0]; |
| 994 | + |
| 995 | + for (idx = 0; idx < args->nattrs; idx++, ep_mem_access++) { |
| 996 | + ep_mem_access->receiver = args->attrs[idx].receiver; |
| 997 | + ep_mem_access->attrs = args->attrs[idx].attrs; |
| 998 | + ep_mem_access->composite_off = COMPOSITE_OFFSET(args->nattrs); |
| 999 | + } |
| 1000 | + mem_region->ep_count = args->nattrs; |
| 1001 | + |
| 1002 | + composite = buffer + COMPOSITE_OFFSET(args->nattrs); |
| 1003 | + composite->total_pg_cnt = ffa_get_num_pages_sg(args->sg); |
| 1004 | + composite->addr_range_cnt = num_entries; |
| 1005 | + |
| 1006 | + length = COMPOSITE_CONSTITUENTS_OFFSET(args->nattrs, num_entries); |
| 1007 | + frag_len = COMPOSITE_CONSTITUENTS_OFFSET(args->nattrs, 0); |
| 1008 | + if (frag_len > max_fragsize) |
| 1009 | + return -ENXIO; |
| 1010 | + |
| 1011 | + if (!args->use_txbuf) { |
| 1012 | + addr = virt_to_phys(buffer); |
| 1013 | + buf_sz = max_fragsize / FFA_PAGE_SIZE; |
| 1014 | + } |
| 1015 | + |
| 1016 | + constituents = buffer + frag_len; |
| 1017 | + idx = 0; |
| 1018 | + do { |
| 1019 | + if (frag_len == max_fragsize) { |
| 1020 | + rc = ffa_transmit_fragment(func_id, addr, buf_sz, |
| 1021 | + frag_len, length, |
| 1022 | + &args->g_handle, first); |
| 1023 | + if (rc < 0) |
| 1024 | + return -ENXIO; |
| 1025 | + |
| 1026 | + first = false; |
| 1027 | + idx = 0; |
| 1028 | + frag_len = 0; |
| 1029 | + constituents = buffer; |
| 1030 | + } |
| 1031 | + |
| 1032 | + if ((void *)constituents - buffer > max_fragsize) { |
| 1033 | + pr_err("Memory Region Fragment > Tx Buffer size\n"); |
| 1034 | + return -EFAULT; |
| 1035 | + } |
| 1036 | + |
| 1037 | + constituents->address = sg_phys(args->sg); |
| 1038 | + constituents->pg_cnt = args->sg->length / FFA_PAGE_SIZE; |
| 1039 | + constituents++; |
| 1040 | + frag_len += sizeof(struct ffa_mem_region_addr_range); |
| 1041 | + } while ((args->sg = sg_next(args->sg))); |
| 1042 | + |
| 1043 | + return ffa_transmit_fragment(func_id, addr, buf_sz, frag_len, |
| 1044 | + length, &args->g_handle, first); |
| 1045 | +} |
| 1046 | + |
| 1047 | +static int ffa_memory_ops(u32 func_id, struct ffa_mem_ops_args *args) |
| 1048 | +{ |
| 1049 | + int ret; |
| 1050 | + void *buffer; |
| 1051 | + |
| 1052 | + if (!args->use_txbuf) { |
| 1053 | + buffer = alloc_pages_exact(RXTX_BUFFER_SIZE, GFP_KERNEL); |
| 1054 | + if (!buffer) |
| 1055 | + return -ENOMEM; |
| 1056 | + } else { |
| 1057 | + buffer = drv_info->tx_buffer; |
| 1058 | + mutex_lock(&drv_info->tx_lock); |
| 1059 | + } |
| 1060 | + |
| 1061 | + ret = ffa_setup_and_transmit(func_id, buffer, RXTX_BUFFER_SIZE, args); |
| 1062 | + |
| 1063 | + if (args->use_txbuf) |
| 1064 | + mutex_unlock(&drv_info->tx_lock); |
| 1065 | + else |
| 1066 | + free_pages_exact(buffer, RXTX_BUFFER_SIZE); |
| 1067 | + |
| 1068 | + return ret < 0 ? ret : 0; |
| 1069 | +} |
| 1070 | + |
| 1071 | +static int ffa_memory_reclaim(u64 g_handle, u32 flags) |
| 1072 | +{ |
| 1073 | + ffa_value_t ret; |
| 1074 | + |
| 1075 | + invoke_ffa_fn((ffa_value_t){ |
| 1076 | + .a0 = FFA_MEM_RECLAIM, |
| 1077 | + .a1 = HANDLE_LOW(g_handle), .a2 = HANDLE_HIGH(g_handle), |
| 1078 | + .a3 = flags, |
| 1079 | + }, &ret); |
| 1080 | + |
| 1081 | + if (ret.a0 == FFA_ERROR) |
| 1082 | + return ffa_to_linux_errno((int)ret.a2); |
| 1083 | + |
| 1084 | + return 0; |
| 1085 | +} |
| 1086 | + |
| 1087 | +static u32 ffa_api_version_get(void) |
| 1088 | +{ |
| 1089 | + return drv_info->version; |
| 1090 | +} |
| 1091 | + |
| 1092 | +static int ffa_partition_info_get(const char *uuid_str, |
| 1093 | + struct ffa_partition_info *buffer) |
| 1094 | +{ |
| 1095 | + int count; |
| 1096 | + uuid_t uuid; |
| 1097 | + struct ffa_partition_info *pbuf; |
| 1098 | + |
| 1099 | + if (uuid_parse(uuid_str, &uuid)) { |
| 1100 | + pr_err("invalid uuid (%s)\n", uuid_str); |
| 1101 | + return -ENODEV; |
| 1102 | + } |
| 1103 | + |
| 1104 | + count = ffa_partition_probe(&uuid_null, &pbuf); |
| 1105 | + if (count <= 0) |
| 1106 | + return -ENOENT; |
| 1107 | + |
| 1108 | + memcpy(buffer, pbuf, sizeof(*pbuf) * count); |
| 1109 | + kfree(pbuf); |
| 1110 | + return 0; |
| 1111 | +} |
| 1112 | + |
| 1113 | +static void ffa_mode_32bit_set(struct ffa_device *dev) |
| 1114 | +{ |
| 1115 | + dev->mode_32bit = true; |
| 1116 | +} |
| 1117 | + |
| 1118 | +static int ffa_sync_send_receive(struct ffa_device *dev, |
| 1119 | + struct ffa_send_direct_data *data) |
| 1120 | +{ |
| 1121 | + return ffa_msg_send_direct_req(drv_info->vm_id, dev->vm_id, |
| 1122 | + dev->mode_32bit, data); |
| 1123 | +} |
| 1124 | + |
| 1125 | +static int |
| 1126 | +ffa_memory_share(struct ffa_device *dev, struct ffa_mem_ops_args *args) |
| 1127 | +{ |
| 1128 | + if (dev->mode_32bit) |
| 1129 | + return ffa_memory_ops(FFA_MEM_SHARE, args); |
| 1130 | + |
| 1131 | + return ffa_memory_ops(FFA_FN_NATIVE(MEM_SHARE), args); |
| 1132 | +} |
| 1133 | + |
| 1134 | +static int |
| 1135 | +ffa_memory_lend(struct ffa_device *dev, struct ffa_mem_ops_args *args) |
| 1136 | +{ |
| 1137 | + /* Note that upon a successful MEM_LEND request the caller |
| 1138 | + * must ensure that the memory region specified is not accessed |
| 1139 | + * until a successful MEM_RECALIM call has been made. |
| 1140 | + * On systems with a hypervisor present this will been enforced, |
| 1141 | + * however on systems without a hypervisor the responsibility |
| 1142 | + * falls to the calling kernel driver to prevent access. |
| 1143 | + */ |
| 1144 | + if (dev->mode_32bit) |
| 1145 | + return ffa_memory_ops(FFA_MEM_LEND, args); |
| 1146 | + |
| 1147 | + return ffa_memory_ops(FFA_FN_NATIVE(MEM_LEND), args); |
| 1148 | +} |
| 1149 | + |
| 1150 | +static const struct ffa_dev_ops ffa_ops = { |
| 1151 | + .api_version_get = ffa_api_version_get, |
| 1152 | + .partition_info_get = ffa_partition_info_get, |
| 1153 | + .mode_32bit_set = ffa_mode_32bit_set, |
| 1154 | + .sync_send_receive = ffa_sync_send_receive, |
| 1155 | + .memory_reclaim = ffa_memory_reclaim, |
| 1156 | + .memory_share = ffa_memory_share, |
| 1157 | + .memory_lend = ffa_memory_lend, |
| 1158 | +}; |
| 1159 | + |
| 1160 | +const struct ffa_dev_ops *ffa_dev_ops_get(struct ffa_device *dev) |
| 1161 | +{ |
| 1162 | + if (ffa_device_is_valid(dev)) |
| 1163 | + return &ffa_ops; |
| 1164 | + |
| 1165 | + return NULL; |
| 1166 | +} |
| 1167 | +EXPORT_SYMBOL_GPL(ffa_dev_ops_get); |
| 1168 | + |
| 1169 | +void ffa_device_match_uuid(struct ffa_device *ffa_dev, const uuid_t *uuid) |
| 1170 | +{ |
| 1171 | + int count, idx; |
| 1172 | + struct ffa_partition_info *pbuf, *tpbuf; |
| 1173 | + |
| 1174 | + count = ffa_partition_probe(uuid, &pbuf); |
| 1175 | + if (count <= 0) |
| 1176 | + return; |
| 1177 | + |
| 1178 | + for (idx = 0, tpbuf = pbuf; idx < count; idx++, tpbuf++) |
| 1179 | + if (tpbuf->id == ffa_dev->vm_id) |
| 1180 | + uuid_copy(&ffa_dev->uuid, uuid); |
| 1181 | + kfree(pbuf); |
| 1182 | +} |
| 1183 | + |
| 1184 | +static void ffa_setup_partitions(void) |
| 1185 | +{ |
| 1186 | + int count, idx; |
| 1187 | + struct ffa_device *ffa_dev; |
| 1188 | + struct ffa_partition_info *pbuf, *tpbuf; |
| 1189 | + |
| 1190 | + count = ffa_partition_probe(&uuid_null, &pbuf); |
| 1191 | + if (count <= 0) { |
| 1192 | + pr_info("%s: No partitions found, error %d\n", __func__, count); |
| 1193 | + return; |
| 1194 | + } |
| 1195 | + |
| 1196 | + for (idx = 0, tpbuf = pbuf; idx < count; idx++, tpbuf++) { |
| 1197 | + /* Note that the &uuid_null parameter will require |
| 1198 | + * ffa_device_match() to find the UUID of this partition id |
| 1199 | + * with help of ffa_device_match_uuid(). Once the FF-A spec |
| 1200 | + * is updated to provide correct UUID here for each partition |
| 1201 | + * as part of the discovery API, we need to pass the |
| 1202 | + * discovered UUID here instead. |
| 1203 | + */ |
| 1204 | + ffa_dev = ffa_device_register(&uuid_null, tpbuf->id); |
| 1205 | + if (!ffa_dev) { |
| 1206 | + pr_err("%s: failed to register partition ID 0x%x\n", |
| 1207 | + __func__, tpbuf->id); |
| 1208 | + continue; |
| 1209 | + } |
| 1210 | + |
| 1211 | + ffa_dev_set_drvdata(ffa_dev, drv_info); |
| 1212 | + } |
| 1213 | + kfree(pbuf); |
| 1214 | +} |
| 1215 | + |
| 1216 | +static int __init ffa_init(void) |
| 1217 | +{ |
| 1218 | + int ret; |
| 1219 | + |
| 1220 | + ret = ffa_transport_init(&invoke_ffa_fn); |
| 1221 | + if (ret) |
| 1222 | + return ret; |
| 1223 | + |
| 1224 | + ret = arm_ffa_bus_init(); |
| 1225 | + if (ret) |
| 1226 | + return ret; |
| 1227 | + |
| 1228 | + drv_info = kzalloc(sizeof(*drv_info), GFP_KERNEL); |
| 1229 | + if (!drv_info) { |
| 1230 | + ret = -ENOMEM; |
| 1231 | + goto ffa_bus_exit; |
| 1232 | + } |
| 1233 | + |
| 1234 | + ret = ffa_version_check(&drv_info->version); |
| 1235 | + if (ret) |
| 1236 | + goto free_drv_info; |
| 1237 | + |
| 1238 | + if (ffa_id_get(&drv_info->vm_id)) { |
| 1239 | + pr_err("failed to obtain VM id for self\n"); |
| 1240 | + ret = -ENODEV; |
| 1241 | + goto free_drv_info; |
| 1242 | + } |
| 1243 | + |
| 1244 | + drv_info->rx_buffer = alloc_pages_exact(RXTX_BUFFER_SIZE, GFP_KERNEL); |
| 1245 | + if (!drv_info->rx_buffer) { |
| 1246 | + ret = -ENOMEM; |
| 1247 | + goto free_pages; |
| 1248 | + } |
| 1249 | + |
| 1250 | + drv_info->tx_buffer = alloc_pages_exact(RXTX_BUFFER_SIZE, GFP_KERNEL); |
| 1251 | + if (!drv_info->tx_buffer) { |
| 1252 | + ret = -ENOMEM; |
| 1253 | + goto free_pages; |
| 1254 | + } |
| 1255 | + |
| 1256 | + ret = ffa_rxtx_map(virt_to_phys(drv_info->tx_buffer), |
| 1257 | + virt_to_phys(drv_info->rx_buffer), |
| 1258 | + RXTX_BUFFER_SIZE / FFA_PAGE_SIZE); |
| 1259 | + if (ret) { |
| 1260 | + pr_err("failed to register FFA RxTx buffers\n"); |
| 1261 | + goto free_pages; |
| 1262 | + } |
| 1263 | + |
| 1264 | + mutex_init(&drv_info->rx_lock); |
| 1265 | + mutex_init(&drv_info->tx_lock); |
| 1266 | + |
| 1267 | + ffa_setup_partitions(); |
| 1268 | + |
| 1269 | + return 0; |
| 1270 | +free_pages: |
| 1271 | + if (drv_info->tx_buffer) |
| 1272 | + free_pages_exact(drv_info->tx_buffer, RXTX_BUFFER_SIZE); |
| 1273 | + free_pages_exact(drv_info->rx_buffer, RXTX_BUFFER_SIZE); |
| 1274 | +free_drv_info: |
| 1275 | + kfree(drv_info); |
| 1276 | +ffa_bus_exit: |
| 1277 | + arm_ffa_bus_exit(); |
| 1278 | + return ret; |
| 1279 | +} |
| 1280 | +subsys_initcall(ffa_init); |
| 1281 | + |
| 1282 | +static void __exit ffa_exit(void) |
| 1283 | +{ |
| 1284 | + ffa_rxtx_unmap(drv_info->vm_id); |
| 1285 | + free_pages_exact(drv_info->tx_buffer, RXTX_BUFFER_SIZE); |
| 1286 | + free_pages_exact(drv_info->rx_buffer, RXTX_BUFFER_SIZE); |
| 1287 | + kfree(drv_info); |
| 1288 | + arm_ffa_bus_exit(); |
| 1289 | +} |
| 1290 | +module_exit(ffa_exit); |
| 1291 | + |
| 1292 | +MODULE_ALIAS("arm-ffa"); |
| 1293 | +MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>"); |
| 1294 | +MODULE_DESCRIPTION("Arm FF-A interface driver"); |
| 1295 | +MODULE_LICENSE("GPL v2"); |
| 1296 | diff --git a/drivers/firmware/arm_ffa/smccc.c b/drivers/firmware/arm_ffa/smccc.c |
| 1297 | new file mode 100644 |
| 1298 | index 000000000000..4d85bfff0a4e |
| 1299 | --- /dev/null |
| 1300 | +++ b/drivers/firmware/arm_ffa/smccc.c |
| 1301 | @@ -0,0 +1,39 @@ |
| 1302 | +// SPDX-License-Identifier: GPL-2.0-only |
| 1303 | +/* |
| 1304 | + * Copyright (C) 2021 ARM Ltd. |
| 1305 | + */ |
| 1306 | + |
| 1307 | +#include <linux/printk.h> |
| 1308 | + |
| 1309 | +#include "common.h" |
| 1310 | + |
| 1311 | +static void __arm_ffa_fn_smc(ffa_value_t args, ffa_value_t *res) |
| 1312 | +{ |
| 1313 | + arm_smccc_1_2_smc(&args, res); |
| 1314 | +} |
| 1315 | + |
| 1316 | +static void __arm_ffa_fn_hvc(ffa_value_t args, ffa_value_t *res) |
| 1317 | +{ |
| 1318 | + arm_smccc_1_2_hvc(&args, res); |
| 1319 | +} |
| 1320 | + |
| 1321 | +int __init ffa_transport_init(ffa_fn **invoke_ffa_fn) |
| 1322 | +{ |
| 1323 | + enum arm_smccc_conduit conduit; |
| 1324 | + |
| 1325 | + if (arm_smccc_get_version() < ARM_SMCCC_VERSION_1_2) |
| 1326 | + return -EOPNOTSUPP; |
| 1327 | + |
| 1328 | + conduit = arm_smccc_1_1_get_conduit(); |
| 1329 | + if (conduit == SMCCC_CONDUIT_NONE) { |
| 1330 | + pr_err("%s: invalid SMCCC conduit\n", __func__); |
| 1331 | + return -EOPNOTSUPP; |
| 1332 | + } |
| 1333 | + |
| 1334 | + if (conduit == SMCCC_CONDUIT_SMC) |
| 1335 | + *invoke_ffa_fn = __arm_ffa_fn_smc; |
| 1336 | + else |
| 1337 | + *invoke_ffa_fn = __arm_ffa_fn_hvc; |
| 1338 | + |
| 1339 | + return 0; |
| 1340 | +} |
| 1341 | diff --git a/include/linux/arm-smccc.h b/include/linux/arm-smccc.h |
| 1342 | index 62c54234576c..c8eb24af3c62 100644 |
| 1343 | --- a/include/linux/arm-smccc.h |
| 1344 | +++ b/include/linux/arm-smccc.h |
| 1345 | @@ -186,6 +186,61 @@ struct arm_smccc_res { |
| 1346 | unsigned long a3; |
| 1347 | }; |
| 1348 | |
| 1349 | +#ifdef CONFIG_ARM64 |
| 1350 | +/** |
| 1351 | + * struct arm_smccc_1_2_regs - Arguments for or Results from SMC/HVC call |
| 1352 | + * @a0-a17 argument values from registers 0 to 17 |
| 1353 | + */ |
| 1354 | +struct arm_smccc_1_2_regs { |
| 1355 | + unsigned long a0; |
| 1356 | + unsigned long a1; |
| 1357 | + unsigned long a2; |
| 1358 | + unsigned long a3; |
| 1359 | + unsigned long a4; |
| 1360 | + unsigned long a5; |
| 1361 | + unsigned long a6; |
| 1362 | + unsigned long a7; |
| 1363 | + unsigned long a8; |
| 1364 | + unsigned long a9; |
| 1365 | + unsigned long a10; |
| 1366 | + unsigned long a11; |
| 1367 | + unsigned long a12; |
| 1368 | + unsigned long a13; |
| 1369 | + unsigned long a14; |
| 1370 | + unsigned long a15; |
| 1371 | + unsigned long a16; |
| 1372 | + unsigned long a17; |
| 1373 | +}; |
| 1374 | + |
| 1375 | +/** |
| 1376 | + * arm_smccc_1_2_hvc() - make HVC calls |
| 1377 | + * @args: arguments passed via struct arm_smccc_1_2_regs |
| 1378 | + * @res: result values via struct arm_smccc_1_2_regs |
| 1379 | + * |
| 1380 | + * This function is used to make HVC calls following SMC Calling Convention |
| 1381 | + * v1.2 or above. The content of the supplied param are copied from the |
| 1382 | + * structure to registers prior to the HVC instruction. The return values |
| 1383 | + * are updated with the content from registers on return from the HVC |
| 1384 | + * instruction. |
| 1385 | + */ |
| 1386 | +asmlinkage void arm_smccc_1_2_hvc(const struct arm_smccc_1_2_regs *args, |
| 1387 | + struct arm_smccc_1_2_regs *res); |
| 1388 | + |
| 1389 | +/** |
| 1390 | + * arm_smccc_1_2_smc() - make SMC calls |
| 1391 | + * @args: arguments passed via struct arm_smccc_1_2_regs |
| 1392 | + * @res: result values via struct arm_smccc_1_2_regs |
| 1393 | + * |
| 1394 | + * This function is used to make SMC calls following SMC Calling Convention |
| 1395 | + * v1.2 or above. The content of the supplied param are copied from the |
| 1396 | + * structure to registers prior to the SMC instruction. The return values |
| 1397 | + * are updated with the content from registers on return from the SMC |
| 1398 | + * instruction. |
| 1399 | + */ |
| 1400 | +asmlinkage void arm_smccc_1_2_smc(const struct arm_smccc_1_2_regs *args, |
| 1401 | + struct arm_smccc_1_2_regs *res); |
| 1402 | +#endif |
| 1403 | + |
| 1404 | /** |
| 1405 | * struct arm_smccc_quirk - Contains quirk information |
| 1406 | * @id: quirk identification |
| 1407 | diff --git a/include/linux/arm_ffa.h b/include/linux/arm_ffa.h |
| 1408 | new file mode 100644 |
| 1409 | index 000000000000..85651e41ded8 |
| 1410 | --- /dev/null |
| 1411 | +++ b/include/linux/arm_ffa.h |
| 1412 | @@ -0,0 +1,269 @@ |
| 1413 | +/* SPDX-License-Identifier: GPL-2.0-only */ |
| 1414 | +/* |
| 1415 | + * Copyright (C) 2021 ARM Ltd. |
| 1416 | + */ |
| 1417 | + |
| 1418 | +#ifndef _LINUX_ARM_FFA_H |
| 1419 | +#define _LINUX_ARM_FFA_H |
| 1420 | + |
| 1421 | +#include <linux/device.h> |
| 1422 | +#include <linux/module.h> |
| 1423 | +#include <linux/types.h> |
| 1424 | +#include <linux/uuid.h> |
| 1425 | + |
| 1426 | +/* FFA Bus/Device/Driver related */ |
| 1427 | +struct ffa_device { |
| 1428 | + int vm_id; |
| 1429 | + bool mode_32bit; |
| 1430 | + uuid_t uuid; |
| 1431 | + struct device dev; |
| 1432 | +}; |
| 1433 | + |
| 1434 | +#define to_ffa_dev(d) container_of(d, struct ffa_device, dev) |
| 1435 | + |
| 1436 | +struct ffa_device_id { |
| 1437 | + uuid_t uuid; |
| 1438 | +}; |
| 1439 | + |
| 1440 | +struct ffa_driver { |
| 1441 | + const char *name; |
| 1442 | + int (*probe)(struct ffa_device *sdev); |
| 1443 | + void (*remove)(struct ffa_device *sdev); |
| 1444 | + const struct ffa_device_id *id_table; |
| 1445 | + |
| 1446 | + struct device_driver driver; |
| 1447 | +}; |
| 1448 | + |
| 1449 | +#define to_ffa_driver(d) container_of(d, struct ffa_driver, driver) |
| 1450 | + |
| 1451 | +static inline void ffa_dev_set_drvdata(struct ffa_device *fdev, void *data) |
| 1452 | +{ |
| 1453 | + fdev->dev.driver_data = data; |
| 1454 | +} |
| 1455 | + |
| 1456 | +#if IS_REACHABLE(CONFIG_ARM_FFA_TRANSPORT) |
| 1457 | +struct ffa_device *ffa_device_register(const uuid_t *uuid, int vm_id); |
| 1458 | +void ffa_device_unregister(struct ffa_device *ffa_dev); |
| 1459 | +int ffa_driver_register(struct ffa_driver *driver, struct module *owner, |
| 1460 | + const char *mod_name); |
| 1461 | +void ffa_driver_unregister(struct ffa_driver *driver); |
| 1462 | +bool ffa_device_is_valid(struct ffa_device *ffa_dev); |
| 1463 | +const struct ffa_dev_ops *ffa_dev_ops_get(struct ffa_device *dev); |
| 1464 | + |
| 1465 | +#else |
| 1466 | +static inline |
| 1467 | +struct ffa_device *ffa_device_register(const uuid_t *uuid, int vm_id) |
| 1468 | +{ |
| 1469 | + return NULL; |
| 1470 | +} |
| 1471 | + |
| 1472 | +static inline void ffa_device_unregister(struct ffa_device *dev) {} |
| 1473 | + |
| 1474 | +static inline int |
| 1475 | +ffa_driver_register(struct ffa_driver *driver, struct module *owner, |
| 1476 | + const char *mod_name) |
| 1477 | +{ |
| 1478 | + return -EINVAL; |
| 1479 | +} |
| 1480 | + |
| 1481 | +static inline void ffa_driver_unregister(struct ffa_driver *driver) {} |
| 1482 | + |
| 1483 | +static inline |
| 1484 | +bool ffa_device_is_valid(struct ffa_device *ffa_dev) { return false; } |
| 1485 | + |
| 1486 | +static inline |
| 1487 | +const struct ffa_dev_ops *ffa_dev_ops_get(struct ffa_device *dev) |
| 1488 | +{ |
| 1489 | + return NULL; |
| 1490 | +} |
| 1491 | +#endif /* CONFIG_ARM_FFA_TRANSPORT */ |
| 1492 | + |
| 1493 | +#define ffa_register(driver) \ |
| 1494 | + ffa_driver_register(driver, THIS_MODULE, KBUILD_MODNAME) |
| 1495 | +#define ffa_unregister(driver) \ |
| 1496 | + ffa_driver_unregister(driver) |
| 1497 | + |
| 1498 | +/** |
| 1499 | + * module_ffa_driver() - Helper macro for registering a psa_ffa driver |
| 1500 | + * @__ffa_driver: ffa_driver structure |
| 1501 | + * |
| 1502 | + * Helper macro for psa_ffa drivers to set up proper module init / exit |
| 1503 | + * functions. Replaces module_init() and module_exit() and keeps people from |
| 1504 | + * printing pointless things to the kernel log when their driver is loaded. |
| 1505 | + */ |
| 1506 | +#define module_ffa_driver(__ffa_driver) \ |
| 1507 | + module_driver(__ffa_driver, ffa_register, ffa_unregister) |
| 1508 | + |
| 1509 | +/* FFA transport related */ |
| 1510 | +struct ffa_partition_info { |
| 1511 | + u16 id; |
| 1512 | + u16 exec_ctxt; |
| 1513 | +/* partition supports receipt of direct requests */ |
| 1514 | +#define FFA_PARTITION_DIRECT_RECV BIT(0) |
| 1515 | +/* partition can send direct requests. */ |
| 1516 | +#define FFA_PARTITION_DIRECT_SEND BIT(1) |
| 1517 | +/* partition can send and receive indirect messages. */ |
| 1518 | +#define FFA_PARTITION_INDIRECT_MSG BIT(2) |
| 1519 | + u32 properties; |
| 1520 | +}; |
| 1521 | + |
| 1522 | +/* For use with FFA_MSG_SEND_DIRECT_{REQ,RESP} which pass data via registers */ |
| 1523 | +struct ffa_send_direct_data { |
| 1524 | + unsigned long data0; /* w3/x3 */ |
| 1525 | + unsigned long data1; /* w4/x4 */ |
| 1526 | + unsigned long data2; /* w5/x5 */ |
| 1527 | + unsigned long data3; /* w6/x6 */ |
| 1528 | + unsigned long data4; /* w7/x7 */ |
| 1529 | +}; |
| 1530 | + |
| 1531 | +struct ffa_mem_region_addr_range { |
| 1532 | + /* The base IPA of the constituent memory region, aligned to 4 kiB */ |
| 1533 | + u64 address; |
| 1534 | + /* The number of 4 kiB pages in the constituent memory region. */ |
| 1535 | + u32 pg_cnt; |
| 1536 | + u32 reserved; |
| 1537 | +}; |
| 1538 | + |
| 1539 | +struct ffa_composite_mem_region { |
| 1540 | + /* |
| 1541 | + * The total number of 4 kiB pages included in this memory region. This |
| 1542 | + * must be equal to the sum of page counts specified in each |
| 1543 | + * `struct ffa_mem_region_addr_range`. |
| 1544 | + */ |
| 1545 | + u32 total_pg_cnt; |
| 1546 | + /* The number of constituents included in this memory region range */ |
| 1547 | + u32 addr_range_cnt; |
| 1548 | + u64 reserved; |
| 1549 | + /** An array of `addr_range_cnt` memory region constituents. */ |
| 1550 | + struct ffa_mem_region_addr_range constituents[]; |
| 1551 | +}; |
| 1552 | + |
| 1553 | +struct ffa_mem_region_attributes { |
| 1554 | + /* The ID of the VM to which the memory is being given or shared. */ |
| 1555 | + u16 receiver; |
| 1556 | + /* |
| 1557 | + * The permissions with which the memory region should be mapped in the |
| 1558 | + * receiver's page table. |
| 1559 | + */ |
| 1560 | +#define FFA_MEM_EXEC BIT(3) |
| 1561 | +#define FFA_MEM_NO_EXEC BIT(2) |
| 1562 | +#define FFA_MEM_RW BIT(1) |
| 1563 | +#define FFA_MEM_RO BIT(0) |
| 1564 | + u8 attrs; |
| 1565 | + /* |
| 1566 | + * Flags used during FFA_MEM_RETRIEVE_REQ and FFA_MEM_RETRIEVE_RESP |
| 1567 | + * for memory regions with multiple borrowers. |
| 1568 | + */ |
| 1569 | +#define FFA_MEM_RETRIEVE_SELF_BORROWER BIT(0) |
| 1570 | + u8 flag; |
| 1571 | + u32 composite_off; |
| 1572 | + /* |
| 1573 | + * Offset in bytes from the start of the outer `ffa_memory_region` to |
| 1574 | + * an `struct ffa_mem_region_addr_range`. |
| 1575 | + */ |
| 1576 | + u64 reserved; |
| 1577 | +}; |
| 1578 | + |
| 1579 | +struct ffa_mem_region { |
| 1580 | + /* The ID of the VM/owner which originally sent the memory region */ |
| 1581 | + u16 sender_id; |
| 1582 | +#define FFA_MEM_NORMAL BIT(5) |
| 1583 | +#define FFA_MEM_DEVICE BIT(4) |
| 1584 | + |
| 1585 | +#define FFA_MEM_WRITE_BACK (3 << 2) |
| 1586 | +#define FFA_MEM_NON_CACHEABLE (1 << 2) |
| 1587 | + |
| 1588 | +#define FFA_DEV_nGnRnE (0 << 2) |
| 1589 | +#define FFA_DEV_nGnRE (1 << 2) |
| 1590 | +#define FFA_DEV_nGRE (2 << 2) |
| 1591 | +#define FFA_DEV_GRE (3 << 2) |
| 1592 | + |
| 1593 | +#define FFA_MEM_NON_SHAREABLE (0) |
| 1594 | +#define FFA_MEM_OUTER_SHAREABLE (2) |
| 1595 | +#define FFA_MEM_INNER_SHAREABLE (3) |
| 1596 | + u8 attributes; |
| 1597 | + u8 reserved_0; |
| 1598 | +/* |
| 1599 | + * Clear memory region contents after unmapping it from the sender and |
| 1600 | + * before mapping it for any receiver. |
| 1601 | + */ |
| 1602 | +#define FFA_MEM_CLEAR BIT(0) |
| 1603 | +/* |
| 1604 | + * Whether the hypervisor may time slice the memory sharing or retrieval |
| 1605 | + * operation. |
| 1606 | + */ |
| 1607 | +#define FFA_TIME_SLICE_ENABLE BIT(1) |
| 1608 | + |
| 1609 | +#define FFA_MEM_RETRIEVE_TYPE_IN_RESP (0 << 3) |
| 1610 | +#define FFA_MEM_RETRIEVE_TYPE_SHARE (1 << 3) |
| 1611 | +#define FFA_MEM_RETRIEVE_TYPE_LEND (2 << 3) |
| 1612 | +#define FFA_MEM_RETRIEVE_TYPE_DONATE (3 << 3) |
| 1613 | + |
| 1614 | +#define FFA_MEM_RETRIEVE_ADDR_ALIGN_HINT BIT(9) |
| 1615 | +#define FFA_MEM_RETRIEVE_ADDR_ALIGN(x) ((x) << 5) |
| 1616 | + /* Flags to control behaviour of the transaction. */ |
| 1617 | + u32 flags; |
| 1618 | +#define HANDLE_LOW_MASK GENMASK_ULL(31, 0) |
| 1619 | +#define HANDLE_HIGH_MASK GENMASK_ULL(63, 32) |
| 1620 | +#define HANDLE_LOW(x) ((u32)(FIELD_GET(HANDLE_LOW_MASK, (x)))) |
| 1621 | +#define HANDLE_HIGH(x) ((u32)(FIELD_GET(HANDLE_HIGH_MASK, (x)))) |
| 1622 | + |
| 1623 | +#define PACK_HANDLE(l, h) \ |
| 1624 | + (FIELD_PREP(HANDLE_LOW_MASK, (l)) | FIELD_PREP(HANDLE_HIGH_MASK, (h))) |
| 1625 | + /* |
| 1626 | + * A globally-unique ID assigned by the hypervisor for a region |
| 1627 | + * of memory being sent between VMs. |
| 1628 | + */ |
| 1629 | + u64 handle; |
| 1630 | + /* |
| 1631 | + * An implementation defined value associated with the receiver and the |
| 1632 | + * memory region. |
| 1633 | + */ |
| 1634 | + u64 tag; |
| 1635 | + u32 reserved_1; |
| 1636 | + /* |
| 1637 | + * The number of `ffa_mem_region_attributes` entries included in this |
| 1638 | + * transaction. |
| 1639 | + */ |
| 1640 | + u32 ep_count; |
| 1641 | + /* |
| 1642 | + * An array of endpoint memory access descriptors. |
| 1643 | + * Each one specifies a memory region offset, an endpoint and the |
| 1644 | + * attributes with which this memory region should be mapped in that |
| 1645 | + * endpoint's page table. |
| 1646 | + */ |
| 1647 | + struct ffa_mem_region_attributes ep_mem_access[]; |
| 1648 | +}; |
| 1649 | + |
| 1650 | +#define COMPOSITE_OFFSET(x) \ |
| 1651 | + (offsetof(struct ffa_mem_region, ep_mem_access[x])) |
| 1652 | +#define CONSTITUENTS_OFFSET(x) \ |
| 1653 | + (offsetof(struct ffa_composite_mem_region, constituents[x])) |
| 1654 | +#define COMPOSITE_CONSTITUENTS_OFFSET(x, y) \ |
| 1655 | + (COMPOSITE_OFFSET(x) + CONSTITUENTS_OFFSET(y)) |
| 1656 | + |
| 1657 | +struct ffa_mem_ops_args { |
| 1658 | + bool use_txbuf; |
| 1659 | + u32 nattrs; |
| 1660 | + u32 flags; |
| 1661 | + u64 tag; |
| 1662 | + u64 g_handle; |
| 1663 | + struct scatterlist *sg; |
| 1664 | + struct ffa_mem_region_attributes *attrs; |
| 1665 | +}; |
| 1666 | + |
| 1667 | +struct ffa_dev_ops { |
| 1668 | + u32 (*api_version_get)(void); |
| 1669 | + int (*partition_info_get)(const char *uuid_str, |
| 1670 | + struct ffa_partition_info *buffer); |
| 1671 | + void (*mode_32bit_set)(struct ffa_device *dev); |
| 1672 | + int (*sync_send_receive)(struct ffa_device *dev, |
| 1673 | + struct ffa_send_direct_data *data); |
| 1674 | + int (*memory_reclaim)(u64 g_handle, u32 flags); |
| 1675 | + int (*memory_share)(struct ffa_device *dev, |
| 1676 | + struct ffa_mem_ops_args *args); |
| 1677 | + int (*memory_lend)(struct ffa_device *dev, |
| 1678 | + struct ffa_mem_ops_args *args); |
| 1679 | +}; |
| 1680 | + |
| 1681 | +#endif /* _LINUX_ARM_FFA_H */ |
| 1682 | -- |
| 1683 | 2.30.2 |
| 1684 | |