arm_ffa: introduce Arm FF-A support

Add Arm FF-A support implementing Arm Firmware Framework for Armv8-A v1.0

The Firmware Framework for Arm A-profile processors (FF-A v1.0) [1]
describes interfaces (ABIs) that standardize communication
between the Secure World and Normal World leveraging TrustZone
technology.

This driver uses 64-bit registers as per SMCCCv1.2 spec and comes
on top of the SMCCC layer. The driver provides the FF-A ABIs needed for
querying the FF-A framework from the secure world.

The driver uses SMC32 calling convention which means using the first
32-bit data of the Xn registers.

All supported ABIs come with their 32-bit version except FFA_RXTX_MAP
which has 64-bit version supported.

Both 32-bit and 64-bit direct messaging are supported which allows both
32-bit and 64-bit clients to use the FF-A bus.

FF-A is a discoverable bus and similar to architecture features.
FF-A bus is discovered using ARM_SMCCC_FEATURES mechanism performed
by the PSCI driver.

Clients are able to probe then use the FF-A bus by calling the DM class
searching APIs (e.g: uclass_first_device).

The Secure World is considered as one entity to communicate with
using the FF-A bus. FF-A communication is handled by one device and
one instance (the bus). This FF-A driver takes care of all the
interactions between Normal world and Secure World.

The driver exports its operations to be used by upper layers.

Exported operations:

- ffa_partition_info_get
- ffa_sync_send_receive
- ffa_rxtx_unmap

Generic FF-A methods are implemented in the Uclass (arm-ffa-uclass.c).
Arm specific methods are implemented in the Arm driver (arm-ffa.c).

For more details please refer to the driver documentation [2].

[1]: https://developer.arm.com/documentation/den0077/latest/
[2]: doc/arch/arm64.ffa.rst

Signed-off-by: Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
Cc: Tom Rini <trini@konsulko.com>
Cc: Jens Wiklander <jens.wiklander@linaro.org>
Cc: Heinrich Schuchardt <xypron.glpk@gmx.de>
This commit is contained in:
Abdellatif El Khlifi
2023-08-04 14:33:40 +01:00
committed by Tom Rini
parent b83dc8df64
commit 39d383bdac
13 changed files with 1939 additions and 0 deletions

213
include/arm_ffa.h Normal file
View File

@@ -0,0 +1,213 @@
/* SPDX-License-Identifier: GPL-2.0+ */
/*
* Copyright 2022-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
*
* Authors:
* Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
*/
#ifndef __ARM_FFA_H
#define __ARM_FFA_H
#include <linux/printk.h>
/*
* This header is public. It can be used by clients to access
* data structures and definitions they need
*/
/*
* struct ffa_partition_info - Partition information descriptor
* @id: Partition ID
* @exec_ctxt: Execution context count
* @properties: Partition properties
*
* Data structure containing information about partitions instantiated in the system
* This structure is filled with the data queried by FFA_PARTITION_INFO_GET
*/
struct ffa_partition_info {
u16 id;
u16 exec_ctxt;
/* partition supports receipt of direct requests */
#define FFA_PARTITION_DIRECT_RECV BIT(0)
/* partition can send direct requests. */
#define FFA_PARTITION_DIRECT_SEND BIT(1)
/* partition can send and receive indirect messages. */
#define FFA_PARTITION_INDIRECT_MSG BIT(2)
u32 properties;
};
/*
* struct ffa_partition_uuid - 16 bytes UUID transmitted by FFA_PARTITION_INFO_GET
* @a1-4: 32-bit words access to the UUID data
*
*/
struct ffa_partition_uuid {
u32 a1; /* w1 */
u32 a2; /* w2 */
u32 a3; /* w3 */
u32 a4; /* w4 */
};
/**
* struct ffa_partition_desc - the secure partition descriptor
* @info: partition information
* @sp_uuid: the secure partition UUID
*
* Each partition has its descriptor containing the partitions information and the UUID
*/
struct ffa_partition_desc {
struct ffa_partition_info info;
struct ffa_partition_uuid sp_uuid;
};
/*
* struct ffa_send_direct_data - Data structure hosting the data
* used by FFA_MSG_SEND_DIRECT_{REQ,RESP}
* @data0-4: Data read/written from/to x3-x7 registers
*
* Data structure containing the data to be sent by FFA_MSG_SEND_DIRECT_REQ
* or read from FFA_MSG_SEND_DIRECT_RESP
*/
/* For use with FFA_MSG_SEND_DIRECT_{REQ,RESP} which pass data via registers */
struct ffa_send_direct_data {
ulong data0; /* w3/x3 */
ulong data1; /* w4/x4 */
ulong data2; /* w5/x5 */
ulong data3; /* w6/x6 */
ulong data4; /* w7/x7 */
};
struct udevice;
/**
* struct ffa_bus_ops - Operations for FF-A
* @partition_info_get: callback for the FFA_PARTITION_INFO_GET
* @sync_send_receive: callback for the FFA_MSG_SEND_DIRECT_REQ
* @rxtx_unmap: callback for the FFA_RXTX_UNMAP
*
* The data structure providing all the operations supported by the driver.
* This structure is EFI runtime resident.
*/
struct ffa_bus_ops {
int (*partition_info_get)(struct udevice *dev, const char *uuid_str,
u32 *sp_count, struct ffa_partition_desc **sp_descs);
int (*sync_send_receive)(struct udevice *dev, u16 dst_part_id,
struct ffa_send_direct_data *msg,
bool is_smc64);
int (*rxtx_unmap)(struct udevice *dev);
};
#define ffa_get_ops(dev) ((struct ffa_bus_ops *)(dev)->driver->ops)
/**
* ffa_rxtx_unmap() - FFA_RXTX_UNMAP driver operation
* Please see ffa_unmap_rxtx_buffers_hdlr() description for more details.
*/
int ffa_rxtx_unmap(struct udevice *dev);
/**
* ffa_unmap_rxtx_buffers_hdlr() - FFA_RXTX_UNMAP handler function
* @dev: The arm_ffa bus device
*
* This function implements FFA_RXTX_UNMAP FF-A function
* to unmap the RX/TX buffers
*
* Return:
*
* 0 on success. Otherwise, failure
*/
int ffa_unmap_rxtx_buffers_hdlr(struct udevice *dev);
/**
* ffa_sync_send_receive() - FFA_MSG_SEND_DIRECT_{REQ,RESP} driver operation
* Please see ffa_msg_send_direct_req_hdlr() description for more details.
*/
int ffa_sync_send_receive(struct udevice *dev, u16 dst_part_id,
struct ffa_send_direct_data *msg, bool is_smc64);
/**
* ffa_msg_send_direct_req_hdlr() - FFA_MSG_SEND_DIRECT_{REQ,RESP} handler function
* @dev: The arm_ffa bus device
* @dst_part_id: destination partition ID
* @msg: pointer to the message data preallocated by the client (in/out)
* @is_smc64: select 64-bit or 32-bit FF-A ABI
*
* This function implements FFA_MSG_SEND_DIRECT_{REQ,RESP}
* FF-A functions.
*
* FFA_MSG_SEND_DIRECT_REQ is used to send the data to the secure partition.
* The response from the secure partition is handled by reading the
* FFA_MSG_SEND_DIRECT_RESP arguments.
*
* The maximum size of the data that can be exchanged is 40 bytes which is
* sizeof(struct ffa_send_direct_data) as defined by the FF-A specification 1.0
* in the section relevant to FFA_MSG_SEND_DIRECT_{REQ,RESP}
*
* Return:
*
* 0 on success. Otherwise, failure
*/
int ffa_msg_send_direct_req_hdlr(struct udevice *dev, u16 dst_part_id,
struct ffa_send_direct_data *msg, bool is_smc64);
/**
* ffa_partition_info_get() - FFA_PARTITION_INFO_GET driver operation
* Please see ffa_get_partitions_info_hdlr() description for more details.
*/
int ffa_partition_info_get(struct udevice *dev, const char *uuid_str,
u32 *sp_count, struct ffa_partition_desc **sp_descs);
/**
* ffa_get_partitions_info_hdlr() - FFA_PARTITION_INFO_GET handler function
* @uuid_str: pointer to the UUID string
* @sp_count: address of the variable containing the number of partitions matching the UUID
* The variable is set by the driver
* @sp_descs: address of the descriptors of the partitions matching the UUID
* The address is set by the driver
*
* Return the number of partitions and their descriptors matching the UUID
*
* Query the secure partition data from uc_priv.
* If not found, invoke FFA_PARTITION_INFO_GET
* FF-A function to query the partition information from secure world.
*
* A client of the FF-A driver should know the UUID of the service it wants to
* access. It should use the UUID to request the FF-A driver to provide the
* partition(s) information of the service. The FF-A driver uses
* PARTITION_INFO_GET to obtain this information. This is implemented through
* ffa_get_partitions_info_hdlr() function.
* A new FFA_PARTITION_INFO_GET call is issued (first one performed through
* ffa_cache_partitions_info) allowing to retrieve the partition(s) information.
* They are not saved (already done). We only update the UUID in the cached area.
* This assumes that partitions data does not change in the secure world.
* Otherwise u-boot will have an outdated partition data. The benefit of caching
* the information in the FF-A driver is to accommodate discovery after
* ExitBootServices().
*
* Return:
*
* @sp_count: the number of partitions
* @sp_descs: address of the partitions descriptors
*
* On success 0 is returned. Otherwise, failure
*/
int ffa_get_partitions_info_hdlr(struct udevice *dev, const char *uuid_str,
u32 *sp_count, struct ffa_partition_desc **sp_descs);
struct ffa_priv;
/**
* ffa_set_smc_conduit() - Set the SMC conduit
* @dev: The FF-A bus device
*
* Selects the SMC conduit by setting the FF-A ABI invoke function.
*
* Return:
*
* 0 on success. Otherwise, failure
*/
int ffa_set_smc_conduit(struct udevice *dev);
#endif

246
include/arm_ffa_priv.h Normal file
View File

@@ -0,0 +1,246 @@
/* SPDX-License-Identifier: GPL-2.0+ */
/*
* Copyright 2022-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
*
* Authors:
* Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
*/
#ifndef __ARM_FFA_PRV_H
#define __ARM_FFA_PRV_H
#include <mapmem.h>
#include <linux/bitfield.h>
#include <linux/bitops.h>
/* This header is exclusively used by the FF-A Uclass and FF-A driver(s) */
/* Arm FF-A driver name */
#define FFA_DRV_NAME "arm_ffa"
/* The FF-A SMC function definitions */
#if CONFIG_IS_ENABLED(SANDBOX)
/* Providing Arm SMCCC declarations to sandbox */
/**
* struct sandbox_smccc_1_2_regs - emulated SMC call arguments or results
* @a0-a17 argument values from registers 0 to 17
*/
struct sandbox_smccc_1_2_regs {
ulong a0;
ulong a1;
ulong a2;
ulong a3;
ulong a4;
ulong a5;
ulong a6;
ulong a7;
ulong a8;
ulong a9;
ulong a10;
ulong a11;
ulong a12;
ulong a13;
ulong a14;
ulong a15;
ulong a16;
ulong a17;
};
typedef struct sandbox_smccc_1_2_regs ffa_value_t;
#define ARM_SMCCC_FAST_CALL 1UL
#define ARM_SMCCC_OWNER_STANDARD 4
#define ARM_SMCCC_SMC_32 0
#define ARM_SMCCC_SMC_64 1
#define ARM_SMCCC_TYPE_SHIFT 31
#define ARM_SMCCC_CALL_CONV_SHIFT 30
#define ARM_SMCCC_OWNER_MASK 0x3f
#define ARM_SMCCC_OWNER_SHIFT 24
#define ARM_SMCCC_FUNC_MASK 0xffff
#define ARM_SMCCC_CALL_VAL(type, calling_convention, owner, func_num) \
(((type) << ARM_SMCCC_TYPE_SHIFT) | \
((calling_convention) << ARM_SMCCC_CALL_CONV_SHIFT) | \
(((owner) & ARM_SMCCC_OWNER_MASK) << ARM_SMCCC_OWNER_SHIFT) | \
((func_num) & ARM_SMCCC_FUNC_MASK))
#else
/* CONFIG_ARM64 */
#include <linux/arm-smccc.h>
typedef struct arm_smccc_1_2_regs ffa_value_t;
#endif
/* Defining the function pointer type for the function executing the FF-A ABIs */
typedef void (*invoke_ffa_fn_t)(ffa_value_t args, ffa_value_t *res);
/* FF-A driver version definitions */
#define MAJOR_VERSION_MASK GENMASK(30, 16)
#define MINOR_VERSION_MASK GENMASK(15, 0)
#define GET_FFA_MAJOR_VERSION(x) \
((u16)(FIELD_GET(MAJOR_VERSION_MASK, (x))))
#define GET_FFA_MINOR_VERSION(x) \
((u16)(FIELD_GET(MINOR_VERSION_MASK, (x))))
#define PACK_VERSION_INFO(major, minor) \
(FIELD_PREP(MAJOR_VERSION_MASK, (major)) | \
FIELD_PREP(MINOR_VERSION_MASK, (minor)))
#define FFA_MAJOR_VERSION (1)
#define FFA_MINOR_VERSION (0)
#define FFA_VERSION_1_0 \
PACK_VERSION_INFO(FFA_MAJOR_VERSION, FFA_MINOR_VERSION)
/* Endpoint ID mask (u-boot endpoint ID) */
#define GET_SELF_ENDPOINT_ID_MASK GENMASK(15, 0)
#define GET_SELF_ENDPOINT_ID(x) \
((u16)(FIELD_GET(GET_SELF_ENDPOINT_ID_MASK, (x))))
#define PREP_SELF_ENDPOINT_ID_MASK GENMASK(31, 16)
#define PREP_SELF_ENDPOINT_ID(x) \
(FIELD_PREP(PREP_SELF_ENDPOINT_ID_MASK, (x)))
/* Partition endpoint ID mask (partition with which u-boot communicates with) */
#define PREP_PART_ENDPOINT_ID_MASK GENMASK(15, 0)
#define PREP_PART_ENDPOINT_ID(x) \
(FIELD_PREP(PREP_PART_ENDPOINT_ID_MASK, (x)))
/* Definitions of the Arm FF-A interfaces supported by the Arm FF-A driver */
#define FFA_SMC(calling_convention, func_num) \
ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, (calling_convention), \
ARM_SMCCC_OWNER_STANDARD, (func_num))
#define FFA_SMC_32(func_num) FFA_SMC(ARM_SMCCC_SMC_32, (func_num))
#define FFA_SMC_64(func_num) FFA_SMC(ARM_SMCCC_SMC_64, (func_num))
enum ffa_abis {
FFA_ERROR = 0x60,
FFA_SUCCESS = 0x61,
FFA_INTERRUPT = 0x62,
FFA_VERSION = 0x63,
FFA_FEATURES = 0x64,
FFA_RX_RELEASE = 0x65,
FFA_RXTX_MAP = 0x66,
FFA_RXTX_UNMAP = 0x67,
FFA_PARTITION_INFO_GET = 0x68,
FFA_ID_GET = 0x69,
FFA_RUN = 0x6d,
FFA_MSG_SEND_DIRECT_REQ = 0x6f,
FFA_MSG_SEND_DIRECT_RESP = 0x70,
/* To be updated when adding new FFA IDs */
FFA_FIRST_ID = FFA_ERROR, /* Lowest number ID */
FFA_LAST_ID = FFA_MSG_SEND_DIRECT_RESP, /* Highest number ID */
};
enum ffa_abi_errcode {
NOT_SUPPORTED = 1,
INVALID_PARAMETERS,
NO_MEMORY,
BUSY,
INTERRUPTED,
DENIED,
RETRY,
ABORTED,
MAX_NUMBER_FFA_ERR
};
extern int ffa_to_std_errmap[MAX_NUMBER_FFA_ERR];
/* Container structure and helper macros to map between an FF-A error and relevant error log */
struct ffa_abi_errmap {
char *err_str[MAX_NUMBER_FFA_ERR];
};
#define FFA_ERRMAP_COUNT (FFA_LAST_ID - FFA_FIRST_ID + 1)
#define FFA_ID_TO_ERRMAP_ID(ffa_id) ((ffa_id) - FFA_FIRST_ID)
/**
* enum ffa_rxtx_buf_sizes - minimum sizes supported
* for the RX/TX buffers
*/
enum ffa_rxtx_buf_sizes {
RXTX_4K,
RXTX_64K,
RXTX_16K
};
/**
* struct ffa_rxtxpair - Hosts the RX/TX buffers virtual addresses
* @rxbuf: virtual address of the RX buffer
* @txbuf: virtual address of the TX buffer
* @rxtx_min_pages: RX/TX buffers minimum size in pages
*
* Hosts the virtual addresses of the mapped RX/TX buffers
* These addresses are used by the FF-A functions that use the RX/TX buffers
*/
struct ffa_rxtxpair {
void *rxbuf; /* Virtual address returned by memalign */
void *txbuf; /* Virtual address returned by memalign */
size_t rxtx_min_pages; /* Minimum number of pages in each of the RX/TX buffers */
};
struct ffa_partition_desc;
/**
* struct ffa_partitions - descriptors for all secure partitions
* @count: The number of partitions descriptors
* @descs The partitions descriptors table
*
* Contains the partitions descriptors table
*/
struct ffa_partitions {
u32 count;
struct ffa_partition_desc *descs; /* Virtual address */
};
/**
* struct ffa_priv - the driver private data structure
*
* @fwk_version: FF-A framework version
* @emul: FF-A sandbox emulator
* @id: u-boot endpoint ID
* @partitions: The partitions descriptors structure
* @pair: The RX/TX buffers pair
*
* The device private data structure containing all the
* data read from secure world.
*/
struct ffa_priv {
u32 fwk_version;
struct udevice *emul;
u16 id;
struct ffa_partitions partitions;
struct ffa_rxtxpair pair;
};
/**
* ffa_get_version_hdlr() - FFA_VERSION handler function
* @dev: The FF-A bus device
*
* Implement FFA_VERSION FF-A function
* to get from the secure world the FF-A framework version
* FFA_VERSION is used to discover the FF-A framework.
*
* Return:
*
* 0 on success. Otherwise, failure
*/
int ffa_get_version_hdlr(struct udevice *dev);
/**
* invoke_ffa_fn() - SMC wrapper
* @args: FF-A ABI arguments to be copied to Xn registers
* @res: FF-A ABI return data to be copied from Xn registers
*
* Calls low level SMC implementation.
* This function should be implemented by the user driver.
*/
void invoke_ffa_fn(ffa_value_t args, ffa_value_t *res);
#endif

View File

@@ -4,6 +4,11 @@
*
* (C) Copyright 2012
* Pavel Herrmann <morpheus.ibis@gmail.com>
*
* Copyright 2022-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
*
* Authors:
* Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
*/
#ifndef _DM_UCLASS_ID_H
@@ -57,6 +62,7 @@ enum uclass_id {
UCLASS_ETH, /* Ethernet device */
UCLASS_ETH_PHY, /* Ethernet PHY device */
UCLASS_EXTCON, /* External Connector Class */
UCLASS_FFA, /* Arm Firmware Framework for Armv8-A */
UCLASS_FIRMWARE, /* Firmware */
UCLASS_FPGA, /* FPGA device */
UCLASS_FUZZING_ENGINE, /* Fuzzing engine */