blob: dd55a73b7d4cf8c26364c6f8db3e8991561932ab [file] [log] [blame]
Patrick Williams864cc432023-02-09 14:54:44 -06001From 69cfd14c37bee479eb3cb7e6dd2df23f460f0713 Mon Sep 17 00:00:00 2001
Patrick Williams8dd68482022-10-04 07:57:18 -05002From: Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
Andrew Geisslerea144b032023-01-27 16:03:57 -06003Date: Tue, 29 Nov 2022 14:40:05 +0000
Patrick Williams864cc432023-02-09 14:54:44 -06004Subject: [PATCH 03/27] arm_ffa: introduce Arm FF-A low-level driver
Patrick Williams8dd68482022-10-04 07:57:18 -05005
Andrew Geisslerea144b032023-01-27 16:03:57 -06006Add the core driver implementing Arm Firmware Framework for Armv8-A v1.0
Patrick Williams8dd68482022-10-04 07:57:18 -05007
Andrew Geisslerea144b032023-01-27 16:03:57 -06008The Firmware Framework for Arm A-profile processors (FF-A v1.0) [1]
Patrick Williams8dd68482022-10-04 07:57:18 -05009describes interfaces (ABIs) that standardize communication
10between the Secure World and Normal World leveraging TrustZone
11technology.
12
13This driver uses 64-bit registers as per SMCCCv1.2 spec and comes
14on top of the SMCCC layer. The driver provides the FF-A ABIs needed for
15querying the FF-A framework from the secure world.
16
Andrew Geisslerea144b032023-01-27 16:03:57 -060017The driver uses SMC32 calling convention which means using the first
1832-bit data of the Xn registers.
Patrick Williams8dd68482022-10-04 07:57:18 -050019
Andrew Geisslerea144b032023-01-27 16:03:57 -060020All supported ABIs come with their 32-bit version except FFA_RXTX_MAP
21which has 64-bit version supported.
22
23Both 32-bit and 64-bit direct messaging are supported which allows both
2432-bit and 64-bit clients to use the FF-A bus.
25
26In U-Boot FF-A design, FF-A is considered as a discoverable bus.
Patrick Williams8dd68482022-10-04 07:57:18 -050027The Secure World is considered as one entity to communicate with
28using the FF-A bus. FF-A communication is handled by one device and
29one instance (the bus). This FF-A driver takes care of all the
30interactions between Normal world and Secure World.
31
32The driver exports its operations to be used by upper layers.
33
34Exported operations:
35
36- partition_info_get
37- sync_send_receive
38- rxtx_unmap
39
Andrew Geisslerea144b032023-01-27 16:03:57 -060040For more details please refer to the driver documentation [2].
41
42[1]: https://developer.arm.com/documentation/den0077/latest/
43[2]: doc/arch/arm64.ffa.rst
Patrick Williams8dd68482022-10-04 07:57:18 -050044
45Signed-off-by: Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
Andrew Geisslerea144b032023-01-27 16:03:57 -060046Cc: Tom Rini <trini@konsulko.com>
47Cc: Simon Glass <sjg@chromium.org>
48Cc: Ilias Apalodimas <ilias.apalodimas@linaro.org>
49Cc: Jens Wiklander <jens.wiklander@linaro.org>
50Upstream-Status: Submitted [cover letter: https://lore.kernel.org/all/20221122131751.22747-1-abdellatif.elkhlifi@arm.com/]
Patrick Williams8dd68482022-10-04 07:57:18 -050051
52Changelog:
53===============
54
Andrew Geisslerea144b032023-01-27 16:03:57 -060055v8:
56
57* make ffa_get_partitions_info() second argument to be an SP count in both
58 modes
59* update ffa_bus_prvdata_get() to return a pointer rather than a pointer
60 address
61* remove packing from ffa_partition_info and ffa_send_direct_data structures
62* pass the FF-A bus device to the bus operations
63
64v7:
65
66* add support for 32-bit direct messaging
67* rename be_uuid_str_to_le_bin() to uuid_str_to_le_bin()
68* improve the declaration of error handling mapping
69* stating in doc/arch/arm64.ffa.rst that EFI runtime is not supported
70
71v6:
72
73* drop use of EFI runtime support (We decided with Linaro to add this later)
74* drop discovery from initcalls (discovery will be on demand by FF-A users)
75* set the alignment of the RX/TX buffers to the larger translation granule size
76* move FF-A RX/TX buffers unmapping at ExitBootServices() to a separate commit
77* update the documentation and move it to doc/arch/arm64.ffa.rst
78
Patrick Williams8dd68482022-10-04 07:57:18 -050079v4:
80
81* add doc/README.ffa.drv
82* moving the FF-A driver work to drivers/firmware/arm-ffa
83* use less #ifdefs in lib/efi_loader/efi_boottime.c and replace
84 #if defined by #if CONFIG_IS_ENABLED
85* improving error handling by mapping the FF-A errors to standard errors
86 and logs
87* replacing panics with an error log and returning an error code
88* improving features discovery in FFA_FEATURES by introducing
89 rxtx_min_pages private data field
Andrew Geisslerea144b032023-01-27 16:03:57 -060090* add ffa_remove and ffa_unbind functions
Patrick Williams8dd68482022-10-04 07:57:18 -050091* improve how the driver behaves when bus discovery is done more than
92 once
93
94v3:
95
Andrew Geisslerea144b032023-01-27 16:03:57 -060096* align the interfaces of the U-Boot FF-A driver with those in the linux
Patrick Williams8dd68482022-10-04 07:57:18 -050097 FF-A driver
98* remove the FF-A helper layer
Andrew Geisslerea144b032023-01-27 16:03:57 -060099* make the U-Boot FF-A driver independent from EFI
Patrick Williams8dd68482022-10-04 07:57:18 -0500100* provide an optional config that enables copying the driver data to EFI
101 runtime section at ExitBootServices service
102* use 64-bit version of FFA_RXTX_MAP, FFA_MSG_SEND_DIRECT_{REQ, RESP}
103
104v2:
105
106* make FF-A bus discoverable using device_{bind, probe} APIs
107* remove device tree support
108
109v1:
110
111* introduce FF-A bus driver with device tree support
Patrick Williams864cc432023-02-09 14:54:44 -0600112
113Signed-off-by: Rui Miguel Silva <rui.silva@linaro.org>
Andrew Geisslerea144b032023-01-27 16:03:57 -0600114---
115 MAINTAINERS | 7 +
116 doc/arch/arm64.ffa.rst | 218 ++++
117 doc/arch/index.rst | 1 +
118 drivers/Kconfig | 2 +
119 drivers/Makefile | 1 +
120 drivers/firmware/arm-ffa/Kconfig | 30 +
121 drivers/firmware/arm-ffa/Makefile | 6 +
122 drivers/firmware/arm-ffa/arm-ffa-uclass.c | 16 +
123 drivers/firmware/arm-ffa/arm_ffa_prv.h | 200 ++++
124 drivers/firmware/arm-ffa/core.c | 1315 +++++++++++++++++++++
125 include/arm_ffa.h | 97 ++
126 include/dm/uclass-id.h | 4 +
127 12 files changed, 1897 insertions(+)
128 create mode 100644 doc/arch/arm64.ffa.rst
Patrick Williams8dd68482022-10-04 07:57:18 -0500129 create mode 100644 drivers/firmware/arm-ffa/Kconfig
130 create mode 100644 drivers/firmware/arm-ffa/Makefile
131 create mode 100644 drivers/firmware/arm-ffa/arm-ffa-uclass.c
132 create mode 100644 drivers/firmware/arm-ffa/arm_ffa_prv.h
133 create mode 100644 drivers/firmware/arm-ffa/core.c
Patrick Williams8dd68482022-10-04 07:57:18 -0500134 create mode 100644 include/arm_ffa.h
135
136diff --git a/MAINTAINERS b/MAINTAINERS
Patrick Williams864cc432023-02-09 14:54:44 -0600137index 3fc4cd0f12d3..509619d31ce6 100644
Patrick Williams8dd68482022-10-04 07:57:18 -0500138--- a/MAINTAINERS
139+++ b/MAINTAINERS
Patrick Williams864cc432023-02-09 14:54:44 -0600140@@ -264,6 +264,13 @@ F: drivers/net/cortina_ni.h
Andrew Geisslerea144b032023-01-27 16:03:57 -0600141 F: drivers/net/phy/ca_phy.c
142 F: configs/cortina_presidio-asic-pnand_defconfig
Patrick Williams8dd68482022-10-04 07:57:18 -0500143
144+ARM FF-A
145+M: Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
146+S: Maintained
Andrew Geisslerea144b032023-01-27 16:03:57 -0600147+F: doc/arch/arm64.ffa.rst
Patrick Williams8dd68482022-10-04 07:57:18 -0500148+F: drivers/firmware/arm-ffa/
149+F: include/arm_ffa.h
150+
151 ARM FREESCALE IMX
152 M: Stefano Babic <sbabic@denx.de>
153 M: Fabio Estevam <festevam@gmail.com>
Andrew Geisslerea144b032023-01-27 16:03:57 -0600154diff --git a/doc/arch/arm64.ffa.rst b/doc/arch/arm64.ffa.rst
Patrick Williams8dd68482022-10-04 07:57:18 -0500155new file mode 100644
Patrick Williams864cc432023-02-09 14:54:44 -0600156index 000000000000..dfcec82e456e
Patrick Williams8dd68482022-10-04 07:57:18 -0500157--- /dev/null
Andrew Geisslerea144b032023-01-27 16:03:57 -0600158+++ b/doc/arch/arm64.ffa.rst
159@@ -0,0 +1,218 @@
160+.. SPDX-License-Identifier: GPL-2.0+
Patrick Williams8dd68482022-10-04 07:57:18 -0500161+
Andrew Geisslerea144b032023-01-27 16:03:57 -0600162+Arm FF-A Driver
163+===============
164+
165+Summary
166+-------
Patrick Williams8dd68482022-10-04 07:57:18 -0500167+
168+FF-A stands for Firmware Framework for Arm A-profile processors.
169+
Andrew Geisslerea144b032023-01-27 16:03:57 -0600170+FF-A specifies interfaces that enable a pair of software sandboxes to
171+communicate with each other. A sandbox aka partition could
172+be a VM in the Normal or Secure world, an application in S-EL0, or a
173+Trusted OS in S-EL1.
Patrick Williams8dd68482022-10-04 07:57:18 -0500174+
Andrew Geisslerea144b032023-01-27 16:03:57 -0600175+This FF-A driver implements the interfaces to communicate with partitions in
176+the Secure world aka Secure partitions (SPs).
Patrick Williams8dd68482022-10-04 07:57:18 -0500177+
Andrew Geisslerea144b032023-01-27 16:03:57 -0600178+The driver specifically focuses on communicating with SPs that isolate portions
179+of EFI runtime services that must run in a protected environment which is
180+inaccessible by the Host OS or Hypervisor. Examples of such services are
181+set/get variables.
Patrick Williams8dd68482022-10-04 07:57:18 -0500182+
183+FF-A driver uses the SMC ABIs defined by the FF-A specification to:
184+
Andrew Geisslerea144b032023-01-27 16:03:57 -0600185+- Discover the presence of SPs of interest
186+- Access an SP's service through communication protocols
187+ e.g. EFI MM communication protocol
188+
189+At this stage of development the FF-A driver supports EFI boot time only.
190+
191+Runtime support will be added in future developments.
Patrick Williams8dd68482022-10-04 07:57:18 -0500192+
193+FF-A and SMC specifications
194+-------------------------------------------
195+
Andrew Geisslerea144b032023-01-27 16:03:57 -0600196+The current implementation of the driver relies on FF-A specification v1.0
197+and uses SMC32 calling convention which means using the first 32-bit data of the
198+Xn registers.
Patrick Williams8dd68482022-10-04 07:57:18 -0500199+
Andrew Geisslerea144b032023-01-27 16:03:57 -0600200+At this stage we only need the FF-A v1.0 features.
Patrick Williams8dd68482022-10-04 07:57:18 -0500201+
Andrew Geisslerea144b032023-01-27 16:03:57 -0600202+The driver has been tested with OP-TEE which supports SMC32 calling convention.
203+
204+For more details please refer to the FF-A v1.0 spec:
205+https://documentation-service.arm.com/static/5fb7e8a6ca04df4095c1d65e?token=
206+
207+Hypervisors are supported if they are configured to trap SMC calls.
Patrick Williams8dd68482022-10-04 07:57:18 -0500208+
209+The FF-A driver uses 64-bit registers as per SMCCCv1.2 specification.
210+
Andrew Geisslerea144b032023-01-27 16:03:57 -0600211+For more details please refer to the SMC Calling Convention v1.2 spec:
212+https://documentation-service.arm.com/static/5f8edaeff86e16515cdbe4c6?token=
Patrick Williams8dd68482022-10-04 07:57:18 -0500213+
214+Supported hardware
215+--------------------------------
216+
217+Aarch64 plaforms
218+
219+Configuration
220+----------------------
221+
222+CONFIG_ARM_FFA_TRANSPORT
Andrew Geisslerea144b032023-01-27 16:03:57 -0600223+ Enables the FF-A bus driver. Turn this on if you want to use FF-A
224+ communication.
Patrick Williams8dd68482022-10-04 07:57:18 -0500225+
226+CONFIG_SANDBOX_FFA
Andrew Geisslerea144b032023-01-27 16:03:57 -0600227+ Enables FF-A Sandbox driver. This emulates the FF-A ABIs handling under
228+ Sandbox and provides functional tests for FF-A.
Patrick Williams8dd68482022-10-04 07:57:18 -0500229+
230+FF-A ABIs under the hood
231+---------------------------------------
232+
Andrew Geisslerea144b032023-01-27 16:03:57 -0600233+Invoking an FF-A ABI involves providing to the secure world/hypervisor the
234+expected arguments from the ABI.
Patrick Williams8dd68482022-10-04 07:57:18 -0500235+
Andrew Geisslerea144b032023-01-27 16:03:57 -0600236+The ABI arguments are stored in x0 to x7 registers. Then, an SMC instruction
237+is executed.
Patrick Williams8dd68482022-10-04 07:57:18 -0500238+
Andrew Geisslerea144b032023-01-27 16:03:57 -0600239+At the secure side level or hypervisor the ABI is handled at a higher exception
240+level and the arguments are read and processed.
Patrick Williams8dd68482022-10-04 07:57:18 -0500241+
Andrew Geisslerea144b032023-01-27 16:03:57 -0600242+The response is put back through x0 to x7 registers and control is given back
243+to the U-Boot FF-A driver (non-secure world).
Patrick Williams8dd68482022-10-04 07:57:18 -0500244+
245+The driver reads the response and processes it accordingly.
246+
247+This methodology applies to all the FF-A ABIs in the driver.
248+
Andrew Geisslerea144b032023-01-27 16:03:57 -0600249+FF-A bus discovery in U-Boot
Patrick Williams8dd68482022-10-04 07:57:18 -0500250+-------------------------------------------
251+
Andrew Geisslerea144b032023-01-27 16:03:57 -0600252+When CONFIG_ARM_FFA_TRANSPORT is enabled, the FF-A bus is discovered on
253+demand by the clients (users).
Patrick Williams8dd68482022-10-04 07:57:18 -0500254+
Andrew Geisslerea144b032023-01-27 16:03:57 -0600255+Clients can discover the FF-A bus using ffa_bus_discover() API which triggers the
256+discovery process.
Patrick Williams8dd68482022-10-04 07:57:18 -0500257+
Andrew Geisslerea144b032023-01-27 16:03:57 -0600258+ffa_bus_discover() creates, binds and probes the arm_ffa device using
259+device_{bind, probe} APIs.
Patrick Williams8dd68482022-10-04 07:57:18 -0500260+
Andrew Geisslerea144b032023-01-27 16:03:57 -0600261+The discovery process consists in communicating with secure world (or hypervisor)
262+and querying specific data.
Patrick Williams8dd68482022-10-04 07:57:18 -0500263+
Andrew Geisslerea144b032023-01-27 16:03:57 -0600264+The discovery process takes place during the arm_ffa device probing which is
265+handled by ffa_probe().
266+
267+The FF-A bus discovery is successful and the bus is ready for use when these
268+operations succeed:
Patrick Williams8dd68482022-10-04 07:57:18 -0500269+
270+- querying the FF-A framework version
Andrew Geisslerea144b032023-01-27 16:03:57 -0600271+- querying from secure world the U-Boot endpoint ID
272+- querying from secure world the RX/TX mapping features
Patrick Williams8dd68482022-10-04 07:57:18 -0500273+- mapping the RX/TX buffers
274+- querying from secure world all the partitions information
275+
Andrew Geisslerea144b032023-01-27 16:03:57 -0600276+Discovery failure results in a probing failure and the arm_ffa device is
277+destroyed.
Patrick Williams8dd68482022-10-04 07:57:18 -0500278+
Andrew Geisslerea144b032023-01-27 16:03:57 -0600279+Requirements for clients
280+-------------------------------------
281+
282+When using the FF-A bus with EFI, clients must:
283+
284+- Query SPs in EFI boot time mode using the service UUID.
285+- Unmap RX/TX buffers before EFI runtime mode starts.
286+
287+The RX/TX buffers are only available at EFI boot time. Querying partitions is
288+done at boot time and data is cached for future use.
289+
290+RX/TX buffers should be unmapped by the user before EFI runtime mode
291+starts. The driver provides a bus operation for that: rxtx_unmap()
292+
293+If RX/TX buffers created by U-Boot are not unmapped and by
294+consequence becoming available at EFI runtime, secure world will get confused
295+about RX/TX buffers ownership (U-Boot vs kernel).
296+
297+When invoking FF-A direct messaging, clients should specify which ABI protocol
298+they want to use (32-bit vs 64-bit). Selecting the protocol means using
299+the 32-bit or 64-bit version of FFA_MSG_SEND_DIRECT_{REQ, RESP}.
300+The calling convention stays the same: SMC32.
Patrick Williams8dd68482022-10-04 07:57:18 -0500301+
302+The bus driver layer
303+------------------------------
304+
Andrew Geisslerea144b032023-01-27 16:03:57 -0600305+The driver comes on top of the SMCCC layer and is implemented in
306+drivers/firmware/arm-ffa/core.c
Patrick Williams8dd68482022-10-04 07:57:18 -0500307+
308+The driver provides the following features:
309+
310+- Support for the 32-bit version of the following ABIs:
311+
312+FFA_VERSION
313+FFA_ID_GET
314+FFA_FEATURES
315+FFA_PARTITION_INFO_GET
316+FFA_RXTX_UNMAP
317+FFA_RX_RELEASE
318+FFA_RUN
319+FFA_ERROR
320+FFA_SUCCESS
321+FFA_INTERRUPT
Andrew Geisslerea144b032023-01-27 16:03:57 -0600322+FFA_MSG_SEND_DIRECT_REQ
323+FFA_MSG_SEND_DIRECT_RESP
Patrick Williams8dd68482022-10-04 07:57:18 -0500324+
325+- Support for the 64-bit version of the following ABIs:
326+
327+FFA_RXTX_MAP
328+FFA_MSG_SEND_DIRECT_REQ
329+FFA_MSG_SEND_DIRECT_RESP
330+
331+- Processing the received data from the secure world/hypervisor and caching it
332+
Andrew Geisslerea144b032023-01-27 16:03:57 -0600333+- Hiding from upper layers the FF-A protocol and registers details. Upper
334+ layers focus on exchanged data, the driver takes care of how to transport
335+ that to the secure world/hypervisor using FF-A
Patrick Williams8dd68482022-10-04 07:57:18 -0500336+
337+- The driver provides callbacks to be used by clients to access the FF-A bus:
338+
339+partition_info_get
340+sync_send_receive
341+rxtx_unmap
342+
Andrew Geisslerea144b032023-01-27 16:03:57 -0600343+- FF-A bus discovery makes sure FF-A framework is responsive and compatible
344+ with the driver
Patrick Williams8dd68482022-10-04 07:57:18 -0500345+
Andrew Geisslerea144b032023-01-27 16:03:57 -0600346+- FF-A bus can be compiled and used without EFI
Patrick Williams8dd68482022-10-04 07:57:18 -0500347+
348+Using armffa command
349+-----------------------------------
350+
Andrew Geisslerea144b032023-01-27 16:03:57 -0600351+armffa is an implementation defined command showcasing how to use the FF-A driver and how to invoke
352+its operations.
Patrick Williams8dd68482022-10-04 07:57:18 -0500353+
Andrew Geisslerea144b032023-01-27 16:03:57 -0600354+This provides a guidance to the client developers on how to call the FF-A bus
355+interfaces.
Patrick Williams8dd68482022-10-04 07:57:18 -0500356+
357+Usage:
358+
359+armffa <sub-command> <arguments>
360+
361+sub-commands:
362+
363+ getpart <partition UUID>
364+
365+ lists the partition(s) info
366+
367+ ping <partition ID>
368+
369+ sends a data pattern to the specified partition
370+
371+ devlist
372+
373+ displays the arm_ffa device info
Andrew Geisslerea144b032023-01-27 16:03:57 -0600374+
375+Contributors
376+------------
377+ * Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
378diff --git a/doc/arch/index.rst b/doc/arch/index.rst
Patrick Williams864cc432023-02-09 14:54:44 -0600379index b3e85f9bf347..cf1cfc9287c2 100644
Andrew Geisslerea144b032023-01-27 16:03:57 -0600380--- a/doc/arch/index.rst
381+++ b/doc/arch/index.rst
382@@ -8,6 +8,7 @@ Architecture-specific doc
383
384 arc
385 arm64
386+ arm64.ffa
387 m68k
388 mips
389 nios2
Patrick Williams8dd68482022-10-04 07:57:18 -0500390diff --git a/drivers/Kconfig b/drivers/Kconfig
Patrick Williams864cc432023-02-09 14:54:44 -0600391index 75ac149d3118..ff75b7c3f883 100644
Patrick Williams8dd68482022-10-04 07:57:18 -0500392--- a/drivers/Kconfig
393+++ b/drivers/Kconfig
394@@ -6,6 +6,8 @@ source "drivers/core/Kconfig"
395
396 source "drivers/adc/Kconfig"
397
398+source "drivers/firmware/arm-ffa/Kconfig"
399+
400 source "drivers/ata/Kconfig"
401
402 source "drivers/axi/Kconfig"
403diff --git a/drivers/Makefile b/drivers/Makefile
Patrick Williams864cc432023-02-09 14:54:44 -0600404index 6f1de58e0030..28c8f538c83a 100644
Patrick Williams8dd68482022-10-04 07:57:18 -0500405--- a/drivers/Makefile
406+++ b/drivers/Makefile
Patrick Williams864cc432023-02-09 14:54:44 -0600407@@ -112,6 +112,7 @@ obj-y += iommu/
Patrick Williams8dd68482022-10-04 07:57:18 -0500408 obj-y += smem/
409 obj-y += thermal/
410 obj-$(CONFIG_TEE) += tee/
411+obj-$(CONFIG_ARM_FFA_TRANSPORT) += firmware/arm-ffa/
412 obj-y += axi/
413 obj-y += ufs/
414 obj-$(CONFIG_W1) += w1/
415diff --git a/drivers/firmware/arm-ffa/Kconfig b/drivers/firmware/arm-ffa/Kconfig
416new file mode 100644
Patrick Williams864cc432023-02-09 14:54:44 -0600417index 000000000000..e4914b9bc779
Patrick Williams8dd68482022-10-04 07:57:18 -0500418--- /dev/null
419+++ b/drivers/firmware/arm-ffa/Kconfig
Andrew Geisslerea144b032023-01-27 16:03:57 -0600420@@ -0,0 +1,30 @@
Patrick Williams8dd68482022-10-04 07:57:18 -0500421+# SPDX-License-Identifier: GPL-2.0
422+
423+config ARM_FFA_TRANSPORT
424+ bool "Enable Arm Firmware Framework for Armv8-A driver"
425+ depends on DM && ARM64
426+ select ARM_SMCCC
427+ select LIB_UUID
428+ select DEVRES
429+ help
430+ The Firmware Framework for Arm A-profile processors (FF-A)
431+ describes interfaces (ABIs) that standardize communication
432+ between the Secure World and Normal World leveraging TrustZone
433+ technology.
434+
435+ This driver is based on FF-A specification v1.0 and uses SMC32
436+ calling convention.
437+
438+ FF-A specification:
439+
440+ https://developer.arm.com/documentation/den0077/a/?lang=en
441+
Andrew Geisslerea144b032023-01-27 16:03:57 -0600442+ In U-Boot FF-A design, FF-A is considered as a discoverable bus.
Patrick Williams8dd68482022-10-04 07:57:18 -0500443+ The Secure World is considered as one entity to communicate with
444+ using the FF-A bus.
445+ FF-A communication is handled by one device and one instance (the bus).
446+ This FF-A driver takes care of all the interactions between Normal world
447+ and Secure World.
448+
Andrew Geisslerea144b032023-01-27 16:03:57 -0600449+ For more details about the FF-A driver, please refer to doc/arch/arm64.ffa.rst
Patrick Williams8dd68482022-10-04 07:57:18 -0500450+
Patrick Williams8dd68482022-10-04 07:57:18 -0500451diff --git a/drivers/firmware/arm-ffa/Makefile b/drivers/firmware/arm-ffa/Makefile
452new file mode 100644
Patrick Williams864cc432023-02-09 14:54:44 -0600453index 000000000000..043a8915bec5
Patrick Williams8dd68482022-10-04 07:57:18 -0500454--- /dev/null
455+++ b/drivers/firmware/arm-ffa/Makefile
Andrew Geisslerea144b032023-01-27 16:03:57 -0600456@@ -0,0 +1,6 @@
Patrick Williams8dd68482022-10-04 07:57:18 -0500457+# SPDX-License-Identifier: GPL-2.0+
458+#
Andrew Geisslerea144b032023-01-27 16:03:57 -0600459+# (C) Copyright 2022
460+# Abdellatif El Khlifi, Arm Limited, abdellatif.elkhlifi@arm.com.
Patrick Williams8dd68482022-10-04 07:57:18 -0500461+
462+obj-y += arm-ffa-uclass.o core.o
Patrick Williams8dd68482022-10-04 07:57:18 -0500463diff --git a/drivers/firmware/arm-ffa/arm-ffa-uclass.c b/drivers/firmware/arm-ffa/arm-ffa-uclass.c
464new file mode 100644
Patrick Williams864cc432023-02-09 14:54:44 -0600465index 000000000000..7d9695d28922
Patrick Williams8dd68482022-10-04 07:57:18 -0500466--- /dev/null
467+++ b/drivers/firmware/arm-ffa/arm-ffa-uclass.c
468@@ -0,0 +1,16 @@
469+// SPDX-License-Identifier: GPL-2.0+
470+/*
471+ * (C) Copyright 2022 ARM Limited
472+ * Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
473+ */
474+
475+#include <common.h>
476+#include <dm.h>
477+#include <asm/global_data.h>
478+
479+DECLARE_GLOBAL_DATA_PTR;
480+
481+UCLASS_DRIVER(ffa) = {
482+ .name = "ffa",
483+ .id = UCLASS_FFA,
484+};
485diff --git a/drivers/firmware/arm-ffa/arm_ffa_prv.h b/drivers/firmware/arm-ffa/arm_ffa_prv.h
486new file mode 100644
Patrick Williams864cc432023-02-09 14:54:44 -0600487index 000000000000..4eea7dc03604
Patrick Williams8dd68482022-10-04 07:57:18 -0500488--- /dev/null
489+++ b/drivers/firmware/arm-ffa/arm_ffa_prv.h
Andrew Geisslerea144b032023-01-27 16:03:57 -0600490@@ -0,0 +1,200 @@
Patrick Williams8dd68482022-10-04 07:57:18 -0500491+/* SPDX-License-Identifier: GPL-2.0+ */
492+/*
493+ * (C) Copyright 2022 ARM Limited
494+ * Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
495+ */
496+
497+#ifndef __ARM_FFA_PRV_H
498+#define __ARM_FFA_PRV_H
499+
500+#include <arm_ffa.h>
501+#include <linux/bitfield.h>
502+#include <linux/bitops.h>
503+#include <linux/arm-smccc.h>
504+
505+/*
506+ * This header is private. It is exclusively used by the FF-A driver
507+ */
508+
509+/* FF-A core driver name */
510+#define FFA_DRV_NAME "arm_ffa"
511+
512+/* FF-A driver version definitions */
513+
514+#define MAJOR_VERSION_MASK GENMASK(30, 16)
515+#define MINOR_VERSION_MASK GENMASK(15, 0)
516+#define GET_FFA_MAJOR_VERSION(x) \
517+ ((u16)(FIELD_GET(MAJOR_VERSION_MASK, (x))))
518+#define GET_FFA_MINOR_VERSION(x) \
519+ ((u16)(FIELD_GET(MINOR_VERSION_MASK, (x))))
520+#define PACK_VERSION_INFO(major, minor) \
521+ (FIELD_PREP(MAJOR_VERSION_MASK, (major)) | \
522+ FIELD_PREP(MINOR_VERSION_MASK, (minor)))
523+
524+#define FFA_MAJOR_VERSION (1)
525+#define FFA_MINOR_VERSION (0)
526+#define FFA_VERSION_1_0 \
527+ PACK_VERSION_INFO(FFA_MAJOR_VERSION, FFA_MINOR_VERSION)
528+
529+/* Endpoint ID mask (u-boot endpoint ID) */
530+
531+#define GET_SELF_ENDPOINT_ID_MASK GENMASK(15, 0)
532+#define GET_SELF_ENDPOINT_ID(x) \
533+ ((u16)(FIELD_GET(GET_SELF_ENDPOINT_ID_MASK, (x))))
534+
535+#define PREP_SELF_ENDPOINT_ID_MASK GENMASK(31, 16)
536+#define PREP_SELF_ENDPOINT_ID(x) \
537+ (FIELD_PREP(PREP_SELF_ENDPOINT_ID_MASK, (x)))
538+
539+/* Partition endpoint ID mask (partition with which u-boot communicates with) */
540+
541+#define PREP_PART_ENDPOINT_ID_MASK GENMASK(15, 0)
542+#define PREP_PART_ENDPOINT_ID(x) \
543+ (FIELD_PREP(PREP_PART_ENDPOINT_ID_MASK, (x)))
544+
545+/*
546+ * Definitions of the Arm FF-A interfaces supported by the Arm FF-A driver
547+ */
548+
549+#define FFA_SMC(calling_convention, func_num) \
550+ ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, (calling_convention), \
551+ ARM_SMCCC_OWNER_STANDARD, (func_num))
552+
553+#define FFA_SMC_32(func_num) FFA_SMC(ARM_SMCCC_SMC_32, (func_num))
554+#define FFA_SMC_64(func_num) FFA_SMC(ARM_SMCCC_SMC_64, (func_num))
555+
556+enum ffa_abis {
Andrew Geisslerea144b032023-01-27 16:03:57 -0600557+ FFA_ERROR = 0x60,
558+ FFA_SUCCESS = 0x61,
559+ FFA_INTERRUPT = 0x62,
560+ FFA_VERSION = 0x63,
561+ FFA_FEATURES = 0x64,
562+ FFA_RX_RELEASE = 0x65,
563+ FFA_RXTX_MAP = 0x66,
564+ FFA_RXTX_UNMAP = 0x67,
565+ FFA_PARTITION_INFO_GET = 0x68,
566+ FFA_ID_GET = 0x69,
567+ FFA_RUN = 0x6D,
568+ FFA_MSG_SEND_DIRECT_REQ = 0x6F,
569+ FFA_MSG_SEND_DIRECT_RESP = 0x70,
Patrick Williams8dd68482022-10-04 07:57:18 -0500570+
571+ /* to be updated when adding new FFA IDs */
Andrew Geisslerea144b032023-01-27 16:03:57 -0600572+ FFA_FIRST_ID = FFA_ERROR, /* lowest number ID*/
573+ FFA_LAST_ID = FFA_MSG_SEND_DIRECT_RESP, /* highest number ID*/
Patrick Williams8dd68482022-10-04 07:57:18 -0500574+};
575+
Andrew Geisslerea144b032023-01-27 16:03:57 -0600576+enum ffa_abi_errcode {
577+ NOT_SUPPORTED = 1,
578+ INVALID_PARAMETERS,
579+ NO_MEMORY,
580+ BUSY,
581+ INTERRUPTED,
582+ DENIED,
583+ RETRY,
584+ ABORTED,
585+ MAX_NUMBER_FFA_ERR
586+};
Patrick Williams8dd68482022-10-04 07:57:18 -0500587+
588+/* container structure and helper macros to map between an FF-A error and relevant error log */
589+struct ffa_abi_errmap {
590+ char *err_str[MAX_NUMBER_FFA_ERR];
591+};
592+
593+#define FFA_ERRMAP_COUNT (FFA_LAST_ID - FFA_FIRST_ID + 1)
594+#define FFA_ID_TO_ERRMAP_ID(ffa_id) ((ffa_id) - FFA_FIRST_ID)
595+
596+/* The FF-A SMC function definitions */
597+
598+typedef struct arm_smccc_1_2_regs ffa_value_t;
599+typedef void (*invoke_ffa_fn_t)(ffa_value_t args, ffa_value_t *res);
600+
601+/*
602+ * struct ffa_partition_uuid - 16 bytes UUID transmitted by FFA_PARTITION_INFO_GET
603+ * @a1-4: 32-bit words access to the UUID data
604+ *
605+ */
606+struct ffa_partition_uuid {
607+ u32 a1; /* w1 */
608+ u32 a2; /* w2 */
609+ u32 a3; /* w3 */
610+ u32 a4; /* w4 */
611+};
612+
613+/**
614+ * enum ffa_rxtx_buf_sizes - minimum sizes supported
615+ * for the RX/TX buffers
616+ */
617+enum ffa_rxtx_buf_sizes {
618+ RXTX_4K,
619+ RXTX_64K,
620+ RXTX_16K
621+};
622+
623+/**
624+ * struct ffa_rxtxpair - structure hosting the RX/TX buffers virtual addresses
625+ * @rxbuf: virtual address of the RX buffer
626+ * @txbuf: virtual address of the TX buffer
627+ * @rxtx_min_pages: RX/TX buffers minimum size in pages
628+ *
629+ * Data structure hosting the virtual addresses of the mapped RX/TX buffers
630+ * These addresses are used by the FF-A functions that use the RX/TX buffers
631+ */
632+struct ffa_rxtxpair {
633+ u64 rxbuf; /* virtual address */
634+ u64 txbuf; /* virtual address */
635+ size_t rxtx_min_pages; /* minimum number of pages in each of the RX/TX buffers */
636+};
637+
638+/**
639+ * struct ffa_partition_desc - the secure partition descriptor
640+ * @info: partition information
641+ * @sp_uuid: the secure partition UUID
642+ *
643+ * Each partition has its descriptor containing the partitions information and the UUID
644+ */
645+struct ffa_partition_desc {
646+ struct ffa_partition_info info;
647+ struct ffa_partition_uuid sp_uuid;
648+};
649+
650+/**
651+ * struct ffa_partitions - descriptors for all secure partitions
652+ * @count: The number of partitions descriptors
653+ * @descs The partitions descriptors table
654+ *
655+ * This data structure contains the partitions descriptors table
656+ */
657+struct ffa_partitions {
658+ u32 count;
659+ struct ffa_partition_desc *descs; /* virtual address */
660+};
661+
662+/**
663+ * struct ffa_prvdata - the driver private data structure
664+ *
665+ * @dev: The arm_ffa device under u-boot driver model
666+ * @ffa_ops: The driver operations structure
667+ * @fwk_version: FF-A framework version
668+ * @id: u-boot endpoint ID
669+ * @partitions: The partitions descriptors structure
670+ * @pair: The RX/TX buffers pair
671+ * @invoke_ffa_fn: The function executing the FF-A function
672+ *
673+ * The driver data structure hosting all resident data.
674+ */
675+struct ffa_prvdata {
676+ struct udevice *dev;
677+ struct ffa_bus_ops ffa_ops;
678+ u32 fwk_version;
679+ u16 id;
680+ struct ffa_partitions partitions;
681+ struct ffa_rxtxpair pair;
682+ invoke_ffa_fn_t invoke_ffa_fn;
683+};
684+
685+/**
Patrick Williams8dd68482022-10-04 07:57:18 -0500686+ * ffa_bus_prvdata_get - bus driver private data getter
687+ */
Andrew Geisslerea144b032023-01-27 16:03:57 -0600688+struct ffa_prvdata *ffa_bus_prvdata_get(void);
Patrick Williams8dd68482022-10-04 07:57:18 -0500689+
690+#endif
691diff --git a/drivers/firmware/arm-ffa/core.c b/drivers/firmware/arm-ffa/core.c
692new file mode 100644
Patrick Williams864cc432023-02-09 14:54:44 -0600693index 000000000000..0b1f8e6a078d
Patrick Williams8dd68482022-10-04 07:57:18 -0500694--- /dev/null
695+++ b/drivers/firmware/arm-ffa/core.c
Andrew Geisslerea144b032023-01-27 16:03:57 -0600696@@ -0,0 +1,1315 @@
Patrick Williams8dd68482022-10-04 07:57:18 -0500697+// SPDX-License-Identifier: GPL-2.0+
698+/*
699+ * (C) Copyright 2022 ARM Limited
700+ * Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
701+ */
702+
703+#include "arm_ffa_prv.h"
704+#include <asm/global_data.h>
705+#include <common.h>
706+#include <dm.h>
707+#include <dm/device-internal.h>
708+#include <dm/devres.h>
709+#include <dm/root.h>
710+#include <linux/errno.h>
711+#include <linux/sizes.h>
712+#include <log.h>
713+#include <malloc.h>
714+#include <string.h>
715+#include <uuid.h>
716+
717+DECLARE_GLOBAL_DATA_PTR;
718+
719+/**
Andrew Geisslerea144b032023-01-27 16:03:57 -0600720+ * The device private data structure containing all the
Patrick Williams8dd68482022-10-04 07:57:18 -0500721+ * data read from secure world
722+ */
Andrew Geisslerea144b032023-01-27 16:03:57 -0600723+struct ffa_prvdata *ffa_priv_data;
Patrick Williams8dd68482022-10-04 07:57:18 -0500724+
725+/* Error mapping declarations */
726+
Andrew Geisslerea144b032023-01-27 16:03:57 -0600727+int ffa_to_std_errmap[MAX_NUMBER_FFA_ERR] = {
728+ [NOT_SUPPORTED] = -EOPNOTSUPP,
729+ [INVALID_PARAMETERS] = -EINVAL,
730+ [NO_MEMORY] = -ENOMEM,
731+ [BUSY] = -EBUSY,
732+ [INTERRUPTED] = -EINTR,
733+ [DENIED] = -EACCES,
734+ [RETRY] = -EAGAIN,
735+ [ABORTED] = -ECANCELED,
Patrick Williams8dd68482022-10-04 07:57:18 -0500736+};
737+
738+struct ffa_abi_errmap err_msg_map[FFA_ERRMAP_COUNT] = {
739+ [FFA_ID_TO_ERRMAP_ID(FFA_VERSION)] = {
740+ {
Andrew Geisslerea144b032023-01-27 16:03:57 -0600741+ [NOT_SUPPORTED] =
Patrick Williams8dd68482022-10-04 07:57:18 -0500742+ "NOT_SUPPORTED: A Firmware Framework implementation does not exist",
Patrick Williams8dd68482022-10-04 07:57:18 -0500743+ },
744+ },
745+ [FFA_ID_TO_ERRMAP_ID(FFA_ID_GET)] = {
746+ {
Andrew Geisslerea144b032023-01-27 16:03:57 -0600747+ [NOT_SUPPORTED] =
Patrick Williams8dd68482022-10-04 07:57:18 -0500748+ "NOT_SUPPORTED: This function is not implemented at this FF-A instance",
Patrick Williams8dd68482022-10-04 07:57:18 -0500749+ },
750+ },
751+ [FFA_ID_TO_ERRMAP_ID(FFA_FEATURES)] = {
752+ {
Andrew Geisslerea144b032023-01-27 16:03:57 -0600753+ [NOT_SUPPORTED] =
Patrick Williams8dd68482022-10-04 07:57:18 -0500754+ "NOT_SUPPORTED: FFA_RXTX_MAP is not implemented at this FF-A instance",
Patrick Williams8dd68482022-10-04 07:57:18 -0500755+ },
756+ },
757+ [FFA_ID_TO_ERRMAP_ID(FFA_PARTITION_INFO_GET)] = {
758+ {
Andrew Geisslerea144b032023-01-27 16:03:57 -0600759+ [NOT_SUPPORTED] =
Patrick Williams8dd68482022-10-04 07:57:18 -0500760+ "NOT_SUPPORTED: This function is not implemented at this FF-A instance",
Andrew Geisslerea144b032023-01-27 16:03:57 -0600761+ [INVALID_PARAMETERS] =
Patrick Williams8dd68482022-10-04 07:57:18 -0500762+ "INVALID_PARAMETERS: Unrecognized UUID",
Andrew Geisslerea144b032023-01-27 16:03:57 -0600763+ [NO_MEMORY] =
Patrick Williams8dd68482022-10-04 07:57:18 -0500764+ "NO_MEMORY: Results cannot fit in RX buffer of the caller",
Andrew Geisslerea144b032023-01-27 16:03:57 -0600765+ [BUSY] =
Patrick Williams8dd68482022-10-04 07:57:18 -0500766+ "BUSY: RX buffer of the caller is not free",
Andrew Geisslerea144b032023-01-27 16:03:57 -0600767+ [DENIED] =
Patrick Williams8dd68482022-10-04 07:57:18 -0500768+ "DENIED: Callee is not in a state to handle this request",
Patrick Williams8dd68482022-10-04 07:57:18 -0500769+ },
770+ },
771+ [FFA_ID_TO_ERRMAP_ID(FFA_RXTX_UNMAP)] = {
Andrew Geisslerea144b032023-01-27 16:03:57 -0600772+ {
773+ [NOT_SUPPORTED] =
Patrick Williams8dd68482022-10-04 07:57:18 -0500774+ "NOT_SUPPORTED: FFA_RXTX_UNMAP is not implemented at this FF-A instance",
Andrew Geisslerea144b032023-01-27 16:03:57 -0600775+ [INVALID_PARAMETERS] =
Patrick Williams8dd68482022-10-04 07:57:18 -0500776+ "INVALID_PARAMETERS: No buffer pair registered on behalf of the caller",
Patrick Williams8dd68482022-10-04 07:57:18 -0500777+ },
778+ },
779+ [FFA_ID_TO_ERRMAP_ID(FFA_RX_RELEASE)] = {
Andrew Geisslerea144b032023-01-27 16:03:57 -0600780+ {
781+ [NOT_SUPPORTED] =
Patrick Williams8dd68482022-10-04 07:57:18 -0500782+ "NOT_SUPPORTED: FFA_RX_RELEASE is not implemented at this FF-A instance",
Andrew Geisslerea144b032023-01-27 16:03:57 -0600783+ [DENIED] =
Patrick Williams8dd68482022-10-04 07:57:18 -0500784+ "DENIED: Caller did not have ownership of the RX buffer",
Patrick Williams8dd68482022-10-04 07:57:18 -0500785+ },
786+ },
787+ [FFA_ID_TO_ERRMAP_ID(FFA_RXTX_MAP)] = {
Andrew Geisslerea144b032023-01-27 16:03:57 -0600788+ {
789+ [NOT_SUPPORTED] =
Patrick Williams8dd68482022-10-04 07:57:18 -0500790+ "NOT_SUPPORTED: This function is not implemented at this FF-A instance",
Andrew Geisslerea144b032023-01-27 16:03:57 -0600791+ [INVALID_PARAMETERS] =
Patrick Williams8dd68482022-10-04 07:57:18 -0500792+ "INVALID_PARAMETERS: Field(s) in input parameters incorrectly encoded",
Andrew Geisslerea144b032023-01-27 16:03:57 -0600793+ [NO_MEMORY] =
Patrick Williams8dd68482022-10-04 07:57:18 -0500794+ "NO_MEMORY: Not enough memory",
Andrew Geisslerea144b032023-01-27 16:03:57 -0600795+ [DENIED] =
Patrick Williams8dd68482022-10-04 07:57:18 -0500796+ "DENIED: Buffer pair already registered",
Patrick Williams8dd68482022-10-04 07:57:18 -0500797+ },
798+ },
799+};
800+
801+/**
802+ * ffa_to_std_errno - convert FF-A error code to standard error code
803+ * @ffa_errno: Error code returned by the FF-A ABI
804+ *
Andrew Geisslerea144b032023-01-27 16:03:57 -0600805+ * This function maps the given FF-A error code as specified
Patrick Williams8dd68482022-10-04 07:57:18 -0500806+ * by the spec to a u-boot standard error code.
807+ *
808+ * Return:
809+ *
810+ * The standard error code on success. . Otherwise, failure
811+ */
Andrew Geisslerea144b032023-01-27 16:03:57 -0600812+int ffa_to_std_errno(int ffa_errno)
Patrick Williams8dd68482022-10-04 07:57:18 -0500813+{
814+ int err_idx = -ffa_errno;
815+
816+ /* map the FF-A error code to the standard u-boot error code */
817+ if (err_idx > 0 && err_idx < MAX_NUMBER_FFA_ERR)
818+ return ffa_to_std_errmap[err_idx];
819+ return -EINVAL;
820+}
821+
822+/**
823+ * ffa_print_error_log - print the error log corresponding to the selected FF-A ABI
824+ * @ffa_id: FF-A ABI ID
825+ * @ffa_errno: Error code returned by the FF-A ABI
826+ *
Andrew Geisslerea144b032023-01-27 16:03:57 -0600827+ * This function maps the FF-A error code to the error log relevant to the
Patrick Williams8dd68482022-10-04 07:57:18 -0500828+ * selected FF-A ABI. Then the error log is printed.
829+ *
830+ * Return:
831+ *
832+ * 0 on success. . Otherwise, failure
833+ */
834+int ffa_print_error_log(u32 ffa_id, int ffa_errno)
835+{
836+ int err_idx = -ffa_errno, abi_idx = 0;
837+
838+ /* map the FF-A error code to the corresponding error log */
839+
840+ if (err_idx <= 0 || err_idx >= MAX_NUMBER_FFA_ERR)
841+ return -EINVAL;
842+
843+ if (ffa_id < FFA_FIRST_ID || ffa_id > FFA_LAST_ID)
844+ return -EINVAL;
845+
846+ abi_idx = FFA_ID_TO_ERRMAP_ID(ffa_id);
847+ if (abi_idx < 0 || abi_idx >= FFA_ERRMAP_COUNT)
848+ return -EINVAL;
849+
Andrew Geisslerea144b032023-01-27 16:03:57 -0600850+ if (!err_msg_map[abi_idx].err_str[err_idx])
Patrick Williams8dd68482022-10-04 07:57:18 -0500851+ return -EINVAL;
852+
853+ ffa_err("%s", err_msg_map[abi_idx].err_str[err_idx]);
854+
855+ return 0;
856+}
857+
858+/*
859+ * Driver core functions
860+ */
861+
862+/**
863+ * ffa_remove_device - removes the arm_ffa device
864+ * @dev: the device to be removed
865+ *
Andrew Geisslerea144b032023-01-27 16:03:57 -0600866+ * This function makes sure the arm_ffa device is removed
Patrick Williams8dd68482022-10-04 07:57:18 -0500867+ * No need to free the kmalloced data when the device is destroyed.
868+ * It's automatically done by devm management by
869+ * device_remove() -> device_free() -> devres_release_probe().
870+ *
871+ * Return:
872+ *
873+ * 0 on success. Otherwise, failure
874+ */
875+int ffa_remove_device(struct udevice *dev)
876+{
877+ int ret;
878+
879+ if (!dev) {
880+ ffa_err("no udevice found");
881+ return -ENODEV;
882+ }
883+
884+ ret = device_remove(dev, DM_REMOVE_NORMAL);
885+ if (ret) {
886+ ffa_err("unable to remove. err:%d\n", ret);
887+ return ret;
888+ }
889+
890+ ffa_info("device removed and freed");
891+
892+ ret = device_unbind(dev);
893+ if (ret) {
894+ ffa_err("unable to unbind. err:%d\n", ret);
895+ return ret;
896+ }
897+
898+ ffa_info("device unbound");
899+
900+ return 0;
901+}
902+
903+/**
904+ * ffa_device_get - create, bind and probe the arm_ffa device
Andrew Geisslerea144b032023-01-27 16:03:57 -0600905+ * @pdev: the address of a device pointer (to be filled when the arm_ffa bus device is created
906+ * successfully)
Patrick Williams8dd68482022-10-04 07:57:18 -0500907+ *
Andrew Geisslerea144b032023-01-27 16:03:57 -0600908+ * This function makes sure the arm_ffa device is
Patrick Williams8dd68482022-10-04 07:57:18 -0500909+ * created, bound to this driver, probed and ready to use.
Andrew Geisslerea144b032023-01-27 16:03:57 -0600910+ * Arm FF-A transport is implemented through a single U-Boot
Patrick Williams8dd68482022-10-04 07:57:18 -0500911+ * device managing the FF-A bus (arm_ffa).
912+ *
913+ * Return:
914+ *
915+ * 0 on success. Otherwise, failure
916+ */
Andrew Geisslerea144b032023-01-27 16:03:57 -0600917+int ffa_device_get(struct udevice **pdev)
Patrick Williams8dd68482022-10-04 07:57:18 -0500918+{
919+ int ret;
920+ struct udevice *dev = NULL;
921+
Andrew Geisslerea144b032023-01-27 16:03:57 -0600922+ ret = device_bind(dm_root(), DM_DRIVER_GET(arm_ffa), FFA_DRV_NAME, NULL, ofnode_null(),
Patrick Williams8dd68482022-10-04 07:57:18 -0500923+ &dev);
924+ if (ret)
925+ return ret;
926+
927+ /* The FF-A bus discovery succeeds when probing is successful */
928+ ret = device_probe(dev);
929+ if (ret) {
930+ ffa_err("arm_ffa device probing failed");
931+ ffa_remove_device(dev);
932+ return ret;
933+ }
934+
Andrew Geisslerea144b032023-01-27 16:03:57 -0600935+ if (pdev)
936+ *pdev = dev;
937+
Patrick Williams8dd68482022-10-04 07:57:18 -0500938+ return 0;
939+}
940+
941+/**
942+ * ffa_get_version - FFA_VERSION handler function
943+ *
Andrew Geisslerea144b032023-01-27 16:03:57 -0600944+ * This function implements FFA_VERSION FF-A function
Patrick Williams8dd68482022-10-04 07:57:18 -0500945+ * to get from the secure world the FF-A framework version
946+ *
947+ * Return:
948+ *
949+ * 0 on success. Otherwise, failure
950+ */
951+static int ffa_get_version(void)
952+{
953+ u16 major, minor;
954+ ffa_value_t res = {0};
955+ int ffa_errno;
956+
957+ ffa_priv_data->invoke_ffa_fn((ffa_value_t){
Andrew Geisslerea144b032023-01-27 16:03:57 -0600958+ .a0 = FFA_SMC_32(FFA_VERSION), .a1 = FFA_VERSION_1_0,
Patrick Williams8dd68482022-10-04 07:57:18 -0500959+ }, &res);
960+
Andrew Geisslerea144b032023-01-27 16:03:57 -0600961+ ffa_errno = res.a0;
Patrick Williams8dd68482022-10-04 07:57:18 -0500962+ if (ffa_errno < 0) {
963+ ffa_print_error_log(FFA_VERSION, ffa_errno);
964+ return ffa_to_std_errno(ffa_errno);
965+ }
966+
Andrew Geisslerea144b032023-01-27 16:03:57 -0600967+ major = GET_FFA_MAJOR_VERSION(res.a0);
968+ minor = GET_FFA_MINOR_VERSION(res.a0);
Patrick Williams8dd68482022-10-04 07:57:18 -0500969+
970+ ffa_info("FF-A driver %d.%d\nFF-A framework %d.%d",
971+ FFA_MAJOR_VERSION, FFA_MINOR_VERSION, major, minor);
972+
973+ if ((major == FFA_MAJOR_VERSION && minor >= FFA_MINOR_VERSION)) {
974+ ffa_info("Versions are compatible ");
975+
Andrew Geisslerea144b032023-01-27 16:03:57 -0600976+ ffa_priv_data->fwk_version = res.a0;
Patrick Williams8dd68482022-10-04 07:57:18 -0500977+
978+ return 0;
979+ }
980+
981+ ffa_err("versions are incompatible\nExpected: %d.%d , Found: %d.%d\n",
982+ FFA_MAJOR_VERSION, FFA_MINOR_VERSION, major, minor);
983+
984+ return -EPROTONOSUPPORT;
985+}
986+
987+/**
988+ * ffa_get_endpoint_id - FFA_ID_GET handler function
989+ *
Andrew Geisslerea144b032023-01-27 16:03:57 -0600990+ * This function implements FFA_ID_GET FF-A function
Patrick Williams8dd68482022-10-04 07:57:18 -0500991+ * to get from the secure world u-boot endpoint ID
992+ *
993+ * Return:
994+ *
995+ * 0 on success. Otherwise, failure
996+ */
997+static int ffa_get_endpoint_id(void)
998+{
999+ ffa_value_t res = {0};
1000+ int ffa_errno;
1001+
1002+ ffa_priv_data->invoke_ffa_fn((ffa_value_t){
1003+ .a0 = FFA_SMC_32(FFA_ID_GET),
Patrick Williams8dd68482022-10-04 07:57:18 -05001004+ }, &res);
1005+
1006+ if (res.a0 == FFA_SMC_32(FFA_SUCCESS)) {
1007+ ffa_priv_data->id = GET_SELF_ENDPOINT_ID((u32)res.a2);
1008+ ffa_info("endpoint ID is %u", ffa_priv_data->id);
1009+
1010+ return 0;
1011+ }
1012+
Andrew Geisslerea144b032023-01-27 16:03:57 -06001013+ ffa_errno = res.a2;
Patrick Williams8dd68482022-10-04 07:57:18 -05001014+
1015+ ffa_print_error_log(FFA_ID_GET, ffa_errno);
1016+
1017+ return ffa_to_std_errno(ffa_errno);
1018+}
1019+
1020+/**
1021+ * ffa_set_rxtx_buffers_pages_cnt - sets the minimum number of pages in each of the RX/TX buffers
1022+ * @prop_field: properties field obtained from FFA_FEATURES ABI
1023+ *
Andrew Geisslerea144b032023-01-27 16:03:57 -06001024+ * This function sets the minimum number of pages
Patrick Williams8dd68482022-10-04 07:57:18 -05001025+ * in each of the RX/TX buffers in the private data structure
1026+ *
1027+ * Return:
1028+ *
1029+ * buf_4k_pages points to the returned number of pages
1030+ * 0 on success. Otherwise, failure
1031+ */
1032+static int ffa_set_rxtx_buffers_pages_cnt(u32 prop_field)
1033+{
1034+ if (!ffa_priv_data)
1035+ return -EINVAL;
1036+
1037+ switch (prop_field) {
1038+ case RXTX_4K:
1039+ ffa_priv_data->pair.rxtx_min_pages = 1;
1040+ break;
1041+ case RXTX_16K:
1042+ ffa_priv_data->pair.rxtx_min_pages = 4;
1043+ break;
1044+ case RXTX_64K:
1045+ ffa_priv_data->pair.rxtx_min_pages = 16;
1046+ break;
1047+ default:
1048+ ffa_err("RX/TX buffer size not supported");
1049+ return -EINVAL;
1050+ }
1051+
1052+ return 0;
1053+}
1054+
1055+/**
1056+ * ffa_get_rxtx_map_features - FFA_FEATURES handler function with FFA_RXTX_MAP argument
1057+ *
Andrew Geisslerea144b032023-01-27 16:03:57 -06001058+ * This function implements FFA_FEATURES FF-A function
Patrick Williams8dd68482022-10-04 07:57:18 -05001059+ * to retrieve the FFA_RXTX_MAP features
1060+ *
1061+ * Return:
1062+ *
1063+ * 0 on success. Otherwise, failure
1064+ */
1065+static int ffa_get_rxtx_map_features(void)
1066+{
1067+ ffa_value_t res = {0};
1068+ int ffa_errno;
1069+
1070+ ffa_priv_data->invoke_ffa_fn((ffa_value_t){
1071+ .a0 = FFA_SMC_32(FFA_FEATURES),
1072+ .a1 = FFA_SMC_64(FFA_RXTX_MAP),
Patrick Williams8dd68482022-10-04 07:57:18 -05001073+ }, &res);
1074+
1075+ if (res.a0 == FFA_SMC_32(FFA_SUCCESS))
Andrew Geisslerea144b032023-01-27 16:03:57 -06001076+ return ffa_set_rxtx_buffers_pages_cnt(res.a2);
Patrick Williams8dd68482022-10-04 07:57:18 -05001077+
Andrew Geisslerea144b032023-01-27 16:03:57 -06001078+ ffa_errno = res.a2;
Patrick Williams8dd68482022-10-04 07:57:18 -05001079+ ffa_print_error_log(FFA_FEATURES, ffa_errno);
1080+
1081+ return ffa_to_std_errno(ffa_errno);
1082+}
1083+
1084+/**
1085+ * ffa_free_rxtx_buffers - frees the RX/TX buffers
1086+ *
Andrew Geisslerea144b032023-01-27 16:03:57 -06001087+ * This function frees the RX/TX buffers
Patrick Williams8dd68482022-10-04 07:57:18 -05001088+ *
1089+ */
1090+static void ffa_free_rxtx_buffers(void)
1091+{
1092+ ffa_info("Freeing RX/TX buffers");
1093+
1094+ if (ffa_priv_data->pair.rxbuf) {
1095+ free((void *)ffa_priv_data->pair.rxbuf);
1096+ ffa_priv_data->pair.rxbuf = 0;
1097+ }
1098+
1099+ if (ffa_priv_data->pair.txbuf) {
1100+ free((void *)ffa_priv_data->pair.txbuf);
1101+ ffa_priv_data->pair.txbuf = 0;
1102+ }
1103+}
1104+
1105+/**
1106+ * ffa_alloc_rxtx_buffers - allocates the RX/TX buffers
1107+ *
Andrew Geisslerea144b032023-01-27 16:03:57 -06001108+ * This function is used by ffa_map_rxtx_buffers to allocate
Patrick Williams8dd68482022-10-04 07:57:18 -05001109+ * the RX/TX buffers before mapping them. The allocated memory is physically
1110+ * contiguous since memalign ends up calling malloc which allocates
1111+ * contiguous memory in u-boot.
1112+ *
1113+ * Return:
1114+ *
1115+ * 0 on success. Otherwise, failure
1116+ */
1117+static int ffa_alloc_rxtx_buffers(void)
1118+{
1119+ u64 bytes;
1120+
1121+ ffa_info("Using %lu 4KB page(s) for RX/TX buffers size",
1122+ ffa_priv_data->pair.rxtx_min_pages);
1123+
1124+ bytes = ffa_priv_data->pair.rxtx_min_pages * SZ_4K;
1125+
Andrew Geisslerea144b032023-01-27 16:03:57 -06001126+ /*
1127+ * The alignment of the RX and TX buffers must be equal
1128+ * to the larger translation granule size
1129+ */
Patrick Williams8dd68482022-10-04 07:57:18 -05001130+
Andrew Geisslerea144b032023-01-27 16:03:57 -06001131+ ffa_priv_data->pair.rxbuf = (u64)memalign(bytes, bytes);
Patrick Williams8dd68482022-10-04 07:57:18 -05001132+ if (!ffa_priv_data->pair.rxbuf) {
1133+ ffa_err("failure to allocate RX buffer");
1134+ return -ENOBUFS;
1135+ }
1136+
1137+ ffa_info("RX buffer at virtual address 0x%llx", ffa_priv_data->pair.rxbuf);
1138+
Andrew Geisslerea144b032023-01-27 16:03:57 -06001139+ ffa_priv_data->pair.txbuf = (u64)memalign(bytes, bytes);
Patrick Williams8dd68482022-10-04 07:57:18 -05001140+ if (!ffa_priv_data->pair.txbuf) {
1141+ free((void *)ffa_priv_data->pair.rxbuf);
1142+ ffa_priv_data->pair.rxbuf = 0;
1143+ ffa_err("failure to allocate the TX buffer");
1144+ return -ENOBUFS;
1145+ }
1146+
1147+ ffa_info("TX buffer at virtual address 0x%llx", ffa_priv_data->pair.txbuf);
1148+
1149+ /*
1150+ * make sure the buffers are cleared before use
1151+ */
1152+ memset((void *)ffa_priv_data->pair.rxbuf, 0, bytes);
1153+ memset((void *)ffa_priv_data->pair.txbuf, 0, bytes);
1154+
1155+ return 0;
1156+}
1157+
1158+/**
1159+ * ffa_map_rxtx_buffers - FFA_RXTX_MAP handler function
1160+ *
Andrew Geisslerea144b032023-01-27 16:03:57 -06001161+ * This function implements FFA_RXTX_MAP FF-A function
Patrick Williams8dd68482022-10-04 07:57:18 -05001162+ * to map the RX/TX buffers
1163+ *
1164+ * Return:
1165+ *
1166+ * 0 on success. Otherwise, failure
1167+ */
1168+static int ffa_map_rxtx_buffers(void)
1169+{
1170+ int ret;
1171+ ffa_value_t res = {0};
1172+ int ffa_errno;
1173+
1174+ ret = ffa_alloc_rxtx_buffers();
1175+ if (ret)
1176+ return ret;
1177+
1178+ /*
1179+ * we need to pass the physical addresses of the RX/TX buffers
1180+ * in u-boot physical/virtual mapping is 1:1
Andrew Geisslerea144b032023-01-27 16:03:57 -06001181+ * no need to convert from virtual to physical
Patrick Williams8dd68482022-10-04 07:57:18 -05001182+ */
1183+
1184+ ffa_priv_data->invoke_ffa_fn((ffa_value_t){
1185+ .a0 = FFA_SMC_64(FFA_RXTX_MAP),
1186+ .a1 = ffa_priv_data->pair.txbuf,
1187+ .a2 = ffa_priv_data->pair.rxbuf,
1188+ .a3 = ffa_priv_data->pair.rxtx_min_pages,
Patrick Williams8dd68482022-10-04 07:57:18 -05001189+ }, &res);
1190+
1191+ if (res.a0 == FFA_SMC_32(FFA_SUCCESS)) {
1192+ ffa_info("RX/TX buffers mapped");
1193+ return 0;
1194+ }
1195+
Andrew Geisslerea144b032023-01-27 16:03:57 -06001196+ ffa_errno = res.a2;
Patrick Williams8dd68482022-10-04 07:57:18 -05001197+ ffa_print_error_log(FFA_RXTX_MAP, ffa_errno);
1198+
1199+ ffa_free_rxtx_buffers();
1200+
1201+ return ffa_to_std_errno(ffa_errno);
1202+}
1203+
1204+/**
1205+ * ffa_unmap_rxtx_buffers - FFA_RXTX_UNMAP handler function
Andrew Geisslerea144b032023-01-27 16:03:57 -06001206+ * @dev: The arm_ffa bus device
Patrick Williams8dd68482022-10-04 07:57:18 -05001207+ *
Andrew Geisslerea144b032023-01-27 16:03:57 -06001208+ * This function implements FFA_RXTX_UNMAP FF-A function
Patrick Williams8dd68482022-10-04 07:57:18 -05001209+ * to unmap the RX/TX buffers
1210+ *
1211+ * Return:
1212+ *
1213+ * 0 on success. Otherwise, failure
1214+ */
Andrew Geisslerea144b032023-01-27 16:03:57 -06001215+static int ffa_unmap_rxtx_buffers(struct udevice *dev)
Patrick Williams8dd68482022-10-04 07:57:18 -05001216+{
1217+ ffa_value_t res = {0};
1218+ int ffa_errno;
1219+
1220+ ffa_priv_data->invoke_ffa_fn((ffa_value_t){
1221+ .a0 = FFA_SMC_32(FFA_RXTX_UNMAP),
1222+ .a1 = PREP_SELF_ENDPOINT_ID(ffa_priv_data->id),
Patrick Williams8dd68482022-10-04 07:57:18 -05001223+ }, &res);
1224+
1225+ if (res.a0 == FFA_SMC_32(FFA_SUCCESS)) {
1226+ ffa_free_rxtx_buffers();
1227+ return 0;
1228+ }
1229+
Andrew Geisslerea144b032023-01-27 16:03:57 -06001230+ ffa_errno = res.a2;
Patrick Williams8dd68482022-10-04 07:57:18 -05001231+ ffa_print_error_log(FFA_RXTX_UNMAP, ffa_errno);
1232+
1233+ return ffa_to_std_errno(ffa_errno);
1234+}
1235+
1236+/**
1237+ * ffa_release_rx_buffer - FFA_RX_RELEASE handler function
1238+ *
Andrew Geisslerea144b032023-01-27 16:03:57 -06001239+ * This function invokes FFA_RX_RELEASE FF-A function
Patrick Williams8dd68482022-10-04 07:57:18 -05001240+ * to release the ownership of the RX buffer
1241+ *
1242+ * Return:
1243+ *
1244+ * 0 on success. Otherwise, failure
1245+ */
1246+static int ffa_release_rx_buffer(void)
1247+{
1248+ ffa_value_t res = {0};
1249+ int ffa_errno;
1250+
1251+ ffa_priv_data->invoke_ffa_fn((ffa_value_t){
1252+ .a0 = FFA_SMC_32(FFA_RX_RELEASE),
Patrick Williams8dd68482022-10-04 07:57:18 -05001253+ }, &res);
1254+
1255+ if (res.a0 == FFA_SMC_32(FFA_SUCCESS))
1256+ return 0;
1257+
Andrew Geisslerea144b032023-01-27 16:03:57 -06001258+ ffa_errno = res.a2;
Patrick Williams8dd68482022-10-04 07:57:18 -05001259+ ffa_print_error_log(FFA_RX_RELEASE, ffa_errno);
1260+
1261+ return ffa_to_std_errno(ffa_errno);
1262+}
1263+
1264+/**
1265+ * ffa_uuid_are_identical - checks whether two given UUIDs are identical
1266+ * @uuid1: first UUID
1267+ * @uuid2: second UUID
1268+ *
Andrew Geisslerea144b032023-01-27 16:03:57 -06001269+ * This function is used by ffa_read_partitions_info to search
Patrick Williams8dd68482022-10-04 07:57:18 -05001270+ * for a UUID in the partitions descriptors table
1271+ *
1272+ * Return:
1273+ *
1274+ * 1 when UUIDs match. Otherwise, 0
1275+ */
Andrew Geisslerea144b032023-01-27 16:03:57 -06001276+bool ffa_uuid_are_identical(const struct ffa_partition_uuid *uuid1,
1277+ const struct ffa_partition_uuid *uuid2)
Patrick Williams8dd68482022-10-04 07:57:18 -05001278+{
1279+ if (!uuid1 || !uuid2)
1280+ return 0;
1281+
Andrew Geisslerea144b032023-01-27 16:03:57 -06001282+ return !memcmp(uuid1, uuid2, sizeof(struct ffa_partition_uuid));
Patrick Williams8dd68482022-10-04 07:57:18 -05001283+}
1284+
1285+/**
1286+ * ffa_read_partitions_info - reads the data queried by FFA_PARTITION_INFO_GET
1287+ * and saves it in the private structure
1288+ * @count: The number of partitions queried
1289+ * @part_uuid: Pointer to the partition(s) UUID
1290+ *
Andrew Geisslerea144b032023-01-27 16:03:57 -06001291+ * This function reads the partitions information
Patrick Williams8dd68482022-10-04 07:57:18 -05001292+ * returned by the FFA_PARTITION_INFO_GET and saves it in the private
1293+ * data structure.
1294+ *
1295+ * Return:
1296+ *
1297+ * The private data structure is updated with the partition(s) information
1298+ * 0 is returned on success. Otherwise, failure
1299+ */
1300+static int ffa_read_partitions_info(u32 count, struct ffa_partition_uuid *part_uuid)
1301+{
1302+ if (!count) {
1303+ ffa_err("no partition detected");
1304+ return -ENODATA;
1305+ }
1306+
1307+ ffa_info("Reading partitions data from the RX buffer");
1308+
1309+ if (!part_uuid) {
1310+ /*
1311+ * querying information of all partitions
1312+ */
1313+ u64 buf_bytes;
1314+ u64 data_bytes;
1315+ u32 desc_idx;
1316+ struct ffa_partition_info *parts_info;
1317+
1318+ data_bytes = count * sizeof(struct ffa_partition_desc);
1319+
1320+ buf_bytes = ffa_priv_data->pair.rxtx_min_pages * SZ_4K;
1321+
1322+ if (data_bytes > buf_bytes) {
1323+ ffa_err("partitions data size exceeds the RX buffer size:");
1324+ ffa_err(" sizes in bytes: data %llu , RX buffer %llu ",
1325+ data_bytes,
1326+ buf_bytes);
1327+
1328+ return -ENOMEM;
1329+ }
1330+
Andrew Geisslerea144b032023-01-27 16:03:57 -06001331+ ffa_priv_data->partitions.descs = devm_kmalloc(ffa_priv_data->dev, data_bytes,
1332+ __GFP_ZERO);
Patrick Williams8dd68482022-10-04 07:57:18 -05001333+ if (!ffa_priv_data->partitions.descs) {
1334+ ffa_err("cannot allocate partitions data buffer");
1335+ return -ENOMEM;
1336+ }
1337+
1338+ parts_info = (struct ffa_partition_info *)ffa_priv_data->pair.rxbuf;
1339+
1340+ for (desc_idx = 0 ; desc_idx < count ; desc_idx++) {
1341+ ffa_priv_data->partitions.descs[desc_idx].info =
1342+ parts_info[desc_idx];
1343+
1344+ ffa_info("Partition ID %x : info cached",
1345+ ffa_priv_data->partitions.descs[desc_idx].info.id);
1346+ }
1347+
1348+ ffa_priv_data->partitions.count = count;
1349+
1350+ ffa_info("%d partition(s) found and cached", count);
1351+
1352+ } else {
1353+ u32 rx_desc_idx, cached_desc_idx;
1354+ struct ffa_partition_info *parts_info;
1355+ u8 desc_found;
1356+
1357+ parts_info = (struct ffa_partition_info *)ffa_priv_data->pair.rxbuf;
1358+
1359+ /*
1360+ * search for the SP IDs read from the RX buffer
1361+ * in the already cached SPs.
1362+ * Update the UUID when ID found.
1363+ */
1364+ for (rx_desc_idx = 0; rx_desc_idx < count ; rx_desc_idx++) {
1365+ desc_found = 0;
1366+
1367+ /*
1368+ * search the current ID in the cached partitions
1369+ */
1370+ for (cached_desc_idx = 0;
1371+ cached_desc_idx < ffa_priv_data->partitions.count;
1372+ cached_desc_idx++) {
1373+ /*
1374+ * save the UUID
1375+ */
1376+ if (ffa_priv_data->partitions.descs[cached_desc_idx].info.id ==
1377+ parts_info[rx_desc_idx].id) {
1378+ ffa_priv_data->partitions.descs[cached_desc_idx].sp_uuid =
1379+ *part_uuid;
1380+
1381+ desc_found = 1;
1382+ break;
1383+ }
1384+ }
1385+
1386+ if (!desc_found)
1387+ return -ENODATA;
1388+ }
1389+ }
1390+
1391+ return 0;
1392+}
1393+
1394+/**
1395+ * ffa_query_partitions_info - invokes FFA_PARTITION_INFO_GET and saves partitions data
1396+ *
1397+ * @part_uuid: Pointer to the partition(s) UUID
1398+ * @pcount: Pointer to the number of partitions variable filled when querying
1399+ *
Andrew Geisslerea144b032023-01-27 16:03:57 -06001400+ * This function executes the FFA_PARTITION_INFO_GET
Patrick Williams8dd68482022-10-04 07:57:18 -05001401+ * to query the partitions data. Then, it calls ffa_read_partitions_info
1402+ * to save the data in the private data structure.
1403+ *
1404+ * After reading the data the RX buffer is released using ffa_release_rx_buffer
1405+ *
1406+ * Return:
1407+ *
1408+ * When part_uuid is NULL, all partitions data are retrieved from secure world
1409+ * When part_uuid is non NULL, data for partitions matching the given UUID are
1410+ * retrieved and the number of partitions is returned
1411+ * 0 is returned on success. Otherwise, failure
1412+ */
1413+static int ffa_query_partitions_info(struct ffa_partition_uuid *part_uuid,
1414+ u32 *pcount)
1415+{
1416+ struct ffa_partition_uuid query_uuid = {0};
1417+ ffa_value_t res = {0};
1418+ int ffa_errno;
1419+
1420+ /*
1421+ * If a UUID is specified. Information for one or more
1422+ * partitions in the system is queried. Otherwise, information
1423+ * for all installed partitions is queried
1424+ */
1425+
1426+ if (part_uuid) {
1427+ if (!pcount)
1428+ return -EINVAL;
1429+
1430+ query_uuid = *part_uuid;
1431+ } else if (pcount) {
1432+ return -EINVAL;
1433+ }
1434+
1435+ ffa_priv_data->invoke_ffa_fn((ffa_value_t){
1436+ .a0 = FFA_SMC_32(FFA_PARTITION_INFO_GET),
1437+ .a1 = query_uuid.a1,
1438+ .a2 = query_uuid.a2,
1439+ .a3 = query_uuid.a3,
1440+ .a4 = query_uuid.a4,
Patrick Williams8dd68482022-10-04 07:57:18 -05001441+ }, &res);
1442+
1443+ if (res.a0 == FFA_SMC_32(FFA_SUCCESS)) {
1444+ int ret;
1445+
1446+ /*
1447+ * res.a2 contains the count of partition information descriptors
1448+ * populated in the RX buffer
1449+ */
1450+ if (res.a2) {
1451+ ret = ffa_read_partitions_info((u32)res.a2, part_uuid);
1452+ if (ret) {
1453+ ffa_err("failed to read partition(s) data , error (%d)", ret);
1454+ ffa_release_rx_buffer();
1455+ return -EINVAL;
1456+ }
1457+ }
1458+
1459+ /*
1460+ * return the SP count (when querying using a UUID)
1461+ */
1462+ if (pcount)
1463+ *pcount = (u32)res.a2;
1464+
1465+ /*
1466+ * After calling FFA_PARTITION_INFO_GET the buffer ownership
1467+ * is assigned to the consumer (u-boot). So, we need to give
1468+ * the ownership back to the SPM or hypervisor
1469+ */
1470+ ret = ffa_release_rx_buffer();
1471+
1472+ return ret;
1473+ }
1474+
Andrew Geisslerea144b032023-01-27 16:03:57 -06001475+ ffa_errno = res.a2;
Patrick Williams8dd68482022-10-04 07:57:18 -05001476+ ffa_print_error_log(FFA_PARTITION_INFO_GET, ffa_errno);
1477+
1478+ return ffa_to_std_errno(ffa_errno);
1479+}
1480+
1481+/**
1482+ * ffa_get_partitions_info - FFA_PARTITION_INFO_GET handler function
1483+ *
1484+ * The passed arguments:
1485+ * Mode 1: When getting from the driver the number of
1486+ * secure partitions:
1487+ * @uuid_str: pointer to the UUID string
Andrew Geisslerea144b032023-01-27 16:03:57 -06001488+ * @sp_count: pointer to the variable that contains the number of partitions
Patrick Williams8dd68482022-10-04 07:57:18 -05001489+ * The variable will be set by the driver
1490+ * @buffer: NULL
1491+ *
1492+ * Mode 2: When requesting the driver to return the
1493+ * partitions information:
Andrew Geisslerea144b032023-01-27 16:03:57 -06001494+ * @dev: The arm_ffa bus device
Patrick Williams8dd68482022-10-04 07:57:18 -05001495+ * @uuid_str: pointer to the UUID string
Andrew Geisslerea144b032023-01-27 16:03:57 -06001496+ * @sp_count: pointer to the variable that contains the number of empty partition descriptors
1497+ * The variable will be read by the driver
Patrick Williams8dd68482022-10-04 07:57:18 -05001498+ * @buffer: pointer to SPs information buffer
Andrew Geisslerea144b032023-01-27 16:03:57 -06001499+ * (allocated by the client and contains empty @sp_count descriptors).
Patrick Williams8dd68482022-10-04 07:57:18 -05001500+ * The buffer will be filled by the driver
1501+ *
Andrew Geisslerea144b032023-01-27 16:03:57 -06001502+ * This function queries the secure partition data from
Patrick Williams8dd68482022-10-04 07:57:18 -05001503+ * the private data structure. If not found, it invokes FFA_PARTITION_INFO_GET
1504+ * FF-A function to query the partition information from secure world.
1505+ *
1506+ * A client of the FF-A driver should know the UUID of the service it wants to
1507+ * access. It should use the UUID to request the FF-A driver to provide the
1508+ * partition(s) information of the service. The FF-A driver uses
1509+ * PARTITION_INFO_GET to obtain this information. This is implemented through
1510+ * ffa_get_partitions_info function.
1511+ * A new FFA_PARTITION_INFO_GET call is issued (first one performed through
1512+ * ffa_cache_partitions_info) allowing to retrieve the partition(s) information.
1513+ * They are not saved (already done). We only update the UUID in the cached area.
1514+ * This assumes that partitions data does not change in the secure world.
1515+ * Otherwise u-boot will have an outdated partition data. The benefit of caching
1516+ * the information in the FF-A driver is to accommodate discovery after
1517+ * ExitBootServices().
1518+ *
1519+ * When invoked through a client request, ffa_get_partitions_info should be
1520+ * called twice. First call is to get from the driver the number of secure
1521+ * partitions (SPs) associated to a particular UUID.
1522+ * Then, the caller (client) allocates the buffer to host the SPs data and
1523+ * issues a 2nd call. Then, the driver fills the SPs data in the pre-allocated
1524+ * buffer.
1525+ *
1526+ * To achieve the mechanism described above, ffa_get_partitions_info uses the
1527+ * following functions:
1528+ * ffa_read_partitions_info
1529+ * ffa_query_partitions_info
1530+ *
1531+ * Return:
1532+ *
Andrew Geisslerea144b032023-01-27 16:03:57 -06001533+ * @sp_count: When pointing to the number of partitions variable, the number is
Patrick Williams8dd68482022-10-04 07:57:18 -05001534+ * set by the driver.
1535+ * When pointing to the partitions information buffer size, the buffer will be
1536+ * filled by the driver.
1537+ *
1538+ * On success 0 is returned. Otherwise, failure
1539+ */
Andrew Geisslerea144b032023-01-27 16:03:57 -06001540+static int ffa_get_partitions_info(struct udevice *dev, const char *uuid_str,
1541+ u32 *sp_count, struct ffa_partition_info *buffer)
Patrick Williams8dd68482022-10-04 07:57:18 -05001542+{
1543+ /*
1544+ * fill_data:
1545+ * 0: return the SP count
1546+ * 1: fill SP data and return it to the caller
Patrick Williams8dd68482022-10-04 07:57:18 -05001547+ */
Andrew Geisslerea144b032023-01-27 16:03:57 -06001548+ bool fill_data = 0;
Patrick Williams8dd68482022-10-04 07:57:18 -05001549+ u32 desc_idx, client_desc_idx;
1550+ struct ffa_partition_uuid part_uuid = {0};
Andrew Geisslerea144b032023-01-27 16:03:57 -06001551+ u32 sp_found = 0;
Patrick Williams8dd68482022-10-04 07:57:18 -05001552+
1553+ if (!ffa_priv_data->partitions.count || !ffa_priv_data->partitions.descs) {
1554+ ffa_err("no partition installed");
1555+ return -EINVAL;
1556+ }
1557+
1558+ if (!uuid_str) {
1559+ ffa_err("no UUID provided");
1560+ return -EINVAL;
1561+ }
1562+
Andrew Geisslerea144b032023-01-27 16:03:57 -06001563+ if (!sp_count) {
Patrick Williams8dd68482022-10-04 07:57:18 -05001564+ ffa_err("no size/count provided");
1565+ return -EINVAL;
1566+ }
1567+
Andrew Geisslerea144b032023-01-27 16:03:57 -06001568+ if (uuid_str_to_le_bin(uuid_str, (unsigned char *)&part_uuid)) {
Patrick Williams8dd68482022-10-04 07:57:18 -05001569+ ffa_err("invalid UUID");
1570+ return -EINVAL;
1571+ }
1572+
1573+ if (!buffer) {
1574+ /* Mode 1: getting the number of secure partitions */
1575+
1576+ fill_data = 0;
1577+
1578+ ffa_info("Preparing for checking partitions count");
1579+
Andrew Geisslerea144b032023-01-27 16:03:57 -06001580+ } else if (*sp_count) {
Patrick Williams8dd68482022-10-04 07:57:18 -05001581+ /* Mode 2: retrieving the partitions information */
1582+
1583+ fill_data = 1;
1584+
1585+ client_desc_idx = 0;
1586+
Patrick Williams8dd68482022-10-04 07:57:18 -05001587+ ffa_info("Preparing for filling partitions info");
1588+
1589+ } else {
1590+ ffa_err("invalid function arguments provided");
1591+ return -EINVAL;
1592+ }
1593+
1594+ ffa_info("Searching partitions using the provided UUID");
1595+
1596+ /*
1597+ * search in the cached partitions
1598+ */
1599+ for (desc_idx = 0;
1600+ desc_idx < ffa_priv_data->partitions.count;
1601+ desc_idx++) {
1602+ if (ffa_uuid_are_identical(&ffa_priv_data->partitions.descs[desc_idx].sp_uuid,
1603+ &part_uuid)) {
1604+ ffa_info("Partition ID %x matches the provided UUID",
1605+ ffa_priv_data->partitions.descs[desc_idx].info.id);
1606+
Andrew Geisslerea144b032023-01-27 16:03:57 -06001607+ sp_found++;
Patrick Williams8dd68482022-10-04 07:57:18 -05001608+
1609+ if (fill_data) {
1610+ /*
1611+ * trying to fill the partition info in the input buffer
1612+ */
1613+
Andrew Geisslerea144b032023-01-27 16:03:57 -06001614+ if (client_desc_idx < *sp_count) {
Patrick Williams8dd68482022-10-04 07:57:18 -05001615+ buffer[client_desc_idx++] =
1616+ ffa_priv_data->partitions.descs[desc_idx].info;
1617+ continue;
1618+ }
1619+
1620+ ffa_err("failed to fill the current descriptor client buffer full");
1621+ return -ENOBUFS;
1622+ }
1623+ }
1624+ }
1625+
Andrew Geisslerea144b032023-01-27 16:03:57 -06001626+ if (!sp_found) {
Patrick Williams8dd68482022-10-04 07:57:18 -05001627+ int ret;
1628+
1629+ ffa_info("No partition found. Querying framework ...");
1630+
Andrew Geisslerea144b032023-01-27 16:03:57 -06001631+ ret = ffa_query_partitions_info(&part_uuid, &sp_found);
Patrick Williams8dd68482022-10-04 07:57:18 -05001632+
1633+ if (ret == 0) {
1634+ if (!fill_data) {
Andrew Geisslerea144b032023-01-27 16:03:57 -06001635+ *sp_count = sp_found;
Patrick Williams8dd68482022-10-04 07:57:18 -05001636+
1637+ ffa_info("Number of partition(s) found matching the UUID: %d",
Andrew Geisslerea144b032023-01-27 16:03:57 -06001638+ sp_found);
Patrick Williams8dd68482022-10-04 07:57:18 -05001639+ } else {
1640+ /*
1641+ * If SPs data detected, they are already in the private data
1642+ * structure, retry searching SP data again to return them
1643+ * to the caller
1644+ */
Andrew Geisslerea144b032023-01-27 16:03:57 -06001645+ if (sp_found)
1646+ ret = ffa_get_partitions_info(dev, uuid_str, sp_count,
1647+ buffer);
Patrick Williams8dd68482022-10-04 07:57:18 -05001648+ else
1649+ ret = -ENODATA;
1650+ }
1651+ }
1652+
1653+ return ret;
1654+ }
1655+
1656+ /* partition(s) found */
1657+ if (!fill_data)
Andrew Geisslerea144b032023-01-27 16:03:57 -06001658+ *sp_count = sp_found;
Patrick Williams8dd68482022-10-04 07:57:18 -05001659+
1660+ return 0;
1661+}
1662+
1663+/**
1664+ * ffa_cache_partitions_info - Queries and saves all secure partitions data
1665+ *
Andrew Geisslerea144b032023-01-27 16:03:57 -06001666+ * This function invokes FFA_PARTITION_INFO_GET FF-A
Patrick Williams8dd68482022-10-04 07:57:18 -05001667+ * function to query from secure world all partitions information.
1668+ *
1669+ * The FFA_PARTITION_INFO_GET call is issued with nil UUID as an argument.
1670+ * All installed partitions information are returned. We cache them in the
1671+ * resident private data structure and we keep the UUID field empty
1672+ * (in FF-A 1.0 UUID is not provided by the partition descriptor)
1673+ *
1674+ * This function is called at the device probing level.
1675+ * ffa_cache_partitions_info uses ffa_query_partitions_info to get the data
1676+ *
1677+ * Return:
1678+ *
1679+ * 0 on success. Otherwise, failure
1680+ */
1681+static int ffa_cache_partitions_info(void)
1682+{
1683+ return ffa_query_partitions_info(NULL, NULL);
1684+}
1685+
1686+/**
1687+ * ffa_msg_send_direct_req - FFA_MSG_SEND_DIRECT_{REQ,RESP} handler function
Andrew Geisslerea144b032023-01-27 16:03:57 -06001688+ * @dev: The arm_ffa bus device
Patrick Williams8dd68482022-10-04 07:57:18 -05001689+ * @dst_part_id: destination partition ID
1690+ * @msg: pointer to the message data preallocated by the client (in/out)
Andrew Geisslerea144b032023-01-27 16:03:57 -06001691+ * @is_smc64: select 64-bit or 32-bit FF-A ABI
Patrick Williams8dd68482022-10-04 07:57:18 -05001692+ *
Andrew Geisslerea144b032023-01-27 16:03:57 -06001693+ * This function implements FFA_MSG_SEND_DIRECT_{REQ,RESP}
Patrick Williams8dd68482022-10-04 07:57:18 -05001694+ * FF-A functions.
1695+ *
1696+ * FFA_MSG_SEND_DIRECT_REQ is used to send the data to the secure partition.
1697+ * The response from the secure partition is handled by reading the
1698+ * FFA_MSG_SEND_DIRECT_RESP arguments.
1699+ *
1700+ * The maximum size of the data that can be exchanged is 40 bytes which is
1701+ * sizeof(struct ffa_send_direct_data) as defined by the FF-A specification 1.0
1702+ * in the section relevant to FFA_MSG_SEND_DIRECT_{REQ,RESP}
1703+ *
1704+ * Return:
1705+ *
1706+ * 0 on success. Otherwise, failure
1707+ */
Andrew Geisslerea144b032023-01-27 16:03:57 -06001708+static int ffa_msg_send_direct_req(struct udevice *dev, u16 dst_part_id,
1709+ struct ffa_send_direct_data *msg, bool is_smc64)
Patrick Williams8dd68482022-10-04 07:57:18 -05001710+{
1711+ ffa_value_t res = {0};
1712+ int ffa_errno;
Andrew Geisslerea144b032023-01-27 16:03:57 -06001713+ u64 req_mode, resp_mode;
Patrick Williams8dd68482022-10-04 07:57:18 -05001714+
1715+ if (!ffa_priv_data || !ffa_priv_data->invoke_ffa_fn)
1716+ return -EINVAL;
1717+
1718+ /* No partition installed */
1719+ if (!ffa_priv_data->partitions.count || !ffa_priv_data->partitions.descs)
1720+ return -ENODEV;
1721+
Andrew Geisslerea144b032023-01-27 16:03:57 -06001722+ if (is_smc64) {
1723+ req_mode = FFA_SMC_64(FFA_MSG_SEND_DIRECT_REQ);
1724+ resp_mode = FFA_SMC_64(FFA_MSG_SEND_DIRECT_RESP);
1725+ } else {
1726+ req_mode = FFA_SMC_32(FFA_MSG_SEND_DIRECT_REQ);
1727+ resp_mode = FFA_SMC_32(FFA_MSG_SEND_DIRECT_RESP);
1728+ }
1729+
Patrick Williams8dd68482022-10-04 07:57:18 -05001730+ ffa_priv_data->invoke_ffa_fn((ffa_value_t){
Andrew Geisslerea144b032023-01-27 16:03:57 -06001731+ .a0 = req_mode,
Patrick Williams8dd68482022-10-04 07:57:18 -05001732+ .a1 = PREP_SELF_ENDPOINT_ID(ffa_priv_data->id) |
1733+ PREP_PART_ENDPOINT_ID(dst_part_id),
1734+ .a2 = 0,
1735+ .a3 = msg->data0,
1736+ .a4 = msg->data1,
1737+ .a5 = msg->data2,
1738+ .a6 = msg->data3,
1739+ .a7 = msg->data4,
1740+ }, &res);
1741+
1742+ while (res.a0 == FFA_SMC_32(FFA_INTERRUPT))
1743+ ffa_priv_data->invoke_ffa_fn((ffa_value_t){
1744+ .a0 = FFA_SMC_32(FFA_RUN),
Andrew Geisslerea144b032023-01-27 16:03:57 -06001745+ .a1 = res.a1,
Patrick Williams8dd68482022-10-04 07:57:18 -05001746+ }, &res);
1747+
1748+ if (res.a0 == FFA_SMC_32(FFA_SUCCESS)) {
1749+ /* Message sent with no response */
1750+ return 0;
1751+ }
1752+
Andrew Geisslerea144b032023-01-27 16:03:57 -06001753+ if (res.a0 == resp_mode) {
Patrick Williams8dd68482022-10-04 07:57:18 -05001754+ /*
1755+ * Message sent with response
1756+ * extract the return data
1757+ */
1758+ msg->data0 = res.a3;
1759+ msg->data1 = res.a4;
1760+ msg->data2 = res.a5;
1761+ msg->data3 = res.a6;
1762+ msg->data4 = res.a7;
1763+
1764+ return 0;
1765+ }
1766+
Andrew Geisslerea144b032023-01-27 16:03:57 -06001767+ ffa_errno = res.a2;
Patrick Williams8dd68482022-10-04 07:57:18 -05001768+ return ffa_to_std_errno(ffa_errno);
1769+}
1770+
1771+/**
1772+ * __arm_ffa_fn_smc - SMC wrapper
1773+ * @args: FF-A ABI arguments to be copied to Xn registers
1774+ * @res: FF-A ABI return data to be copied from Xn registers
1775+ *
1776+ * Calls low level SMC assembly function
1777+ *
1778+ * Return: void
1779+ */
Andrew Geisslerea144b032023-01-27 16:03:57 -06001780+void __arm_ffa_fn_smc(ffa_value_t args, ffa_value_t *res)
Patrick Williams8dd68482022-10-04 07:57:18 -05001781+{
1782+ arm_smccc_1_2_smc(&args, res);
1783+}
1784+
1785+/**
1786+ * ffa_set_smc_conduit - Set the SMC conduit
1787+ *
Andrew Geisslerea144b032023-01-27 16:03:57 -06001788+ * This function selects the SMC conduit by setting the driver invoke function
Patrick Williams8dd68482022-10-04 07:57:18 -05001789+ * to SMC assembly function
1790+ *
1791+ * Return:
1792+ *
1793+ * 0 on success. Otherwise, failure
1794+ */
1795+static int ffa_set_smc_conduit(void)
1796+{
1797+ ffa_priv_data->invoke_ffa_fn = __arm_ffa_fn_smc;
1798+
1799+ if (!ffa_priv_data->invoke_ffa_fn) {
1800+ ffa_err("failure to set the invoke function");
1801+ return -EINVAL;
1802+ }
1803+
1804+ ffa_info("Conduit is SMC");
1805+
1806+ return 0;
1807+}
1808+
1809+/**
1810+ * ffa_set_bus_ops - Set the bus driver operations
1811+ *
1812+ * Setting the driver callbacks.
1813+ *
1814+ */
1815+static void ffa_set_bus_ops(void)
1816+{
1817+ ffa_priv_data->ffa_ops.partition_info_get = ffa_get_partitions_info;
1818+ ffa_priv_data->ffa_ops.sync_send_receive = ffa_msg_send_direct_req;
1819+ ffa_priv_data->ffa_ops.rxtx_unmap = ffa_unmap_rxtx_buffers;
1820+}
1821+
1822+/**
1823+ * ffa_alloc_prvdata - allocate the driver main data structure and sets the device
1824+ * @dev: the arm_ffa device
1825+ *
Andrew Geisslerea144b032023-01-27 16:03:57 -06001826+ * This function creates the main data structure embedding all the driver data.
Patrick Williams8dd68482022-10-04 07:57:18 -05001827+ *
1828+ * Return:
1829+ *
1830+ * 0 on success. Otherwise, failure
1831+ */
1832+static int ffa_alloc_prvdata(struct udevice *dev)
1833+{
1834+ if (!dev) {
1835+ ffa_err("no udevice found");
1836+ return -ENODEV;
1837+ }
1838+
1839+ /* The device is registered with the DM. Let's create the driver main data structure*/
1840+
1841+ ffa_priv_data = devm_kmalloc(dev, sizeof(struct ffa_prvdata), __GFP_ZERO);
1842+ if (!ffa_priv_data) {
1843+ ffa_err("can not allocate the driver main data structure");
1844+ return -ENOMEM;
1845+ }
1846+
1847+ ffa_priv_data->dev = dev;
1848+
1849+ return 0;
1850+}
1851+
1852+/**
1853+ * ffa_probe - The driver probe function
1854+ * @dev: the arm_ffa device
1855+ *
1856+ * Probing is done at boot time and triggered by the uclass device discovery.
1857+ * At probe level the following actions are done:
1858+ * - setting the conduit
1859+ * - querying the FF-A framework version
1860+ * - querying from secure world the u-boot endpoint ID
1861+ * - querying from secure world the supported features of FFA_RXTX_MAP
1862+ * - mapping the RX/TX buffers
1863+ * - querying from secure world all the partitions information
1864+ *
1865+ * All data queried from secure world is saved in the resident private data structure.
1866+ *
1867+ * The probe will fail if either FF-A framework is not detected or the
1868+ * FF-A requests are not behaving correctly. This ensures that the
1869+ * driver is not installed and its operations are not exported to the clients.
1870+ *
1871+ * Return:
1872+ *
1873+ * 0 on success. Otherwise, failure
1874+ */
1875+static int ffa_probe(struct udevice *dev)
1876+{
1877+ int ret;
1878+
1879+ ret = ffa_alloc_prvdata(dev);
1880+ if (ret != 0)
1881+ return ret;
1882+
1883+ ffa_set_bus_ops();
1884+
1885+ ret = ffa_set_smc_conduit();
1886+ if (ret != 0)
1887+ return ret;
1888+
1889+ ret = ffa_get_version();
1890+ if (ret != 0)
1891+ return ret;
1892+
1893+ ret = ffa_get_endpoint_id();
1894+ if (ret != 0)
1895+ return ret;
1896+
1897+ ret = ffa_get_rxtx_map_features();
1898+ if (ret != 0)
1899+ return ret;
1900+
1901+ ret = ffa_map_rxtx_buffers();
1902+ if (ret != 0)
1903+ return ret;
1904+
1905+ ret = ffa_cache_partitions_info();
1906+ if (ret != 0) {
1907+ ffa_free_rxtx_buffers();
1908+ return ret;
1909+ }
1910+
1911+ return 0;
1912+}
1913+
1914+/**
1915+ * ffa_remove - The driver remove function
1916+ * @dev: the arm_ffa device
1917+ * When the device is about to be removed , unmap the RX/TX buffers and free the memory
1918+ * Return:
1919+ *
1920+ * 0 on success.
1921+ */
1922+static int ffa_remove(struct udevice *dev)
1923+{
1924+ ffa_info("removing the device");
1925+
Andrew Geisslerea144b032023-01-27 16:03:57 -06001926+ ffa_unmap_rxtx_buffers(dev);
Patrick Williams8dd68482022-10-04 07:57:18 -05001927+
1928+ if (ffa_priv_data->pair.rxbuf || ffa_priv_data->pair.txbuf)
1929+ ffa_free_rxtx_buffers();
1930+
1931+ return 0;
1932+}
1933+
1934+/**
1935+ * ffa_unbind - The driver unbind function
1936+ * @dev: the arm_ffa device
1937+ * After the device is removed and memory freed the device is unbound
1938+ * Return:
1939+ *
1940+ * 0 on success.
1941+ */
1942+static int ffa_unbind(struct udevice *dev)
1943+{
1944+ ffa_info("unbinding the device , private data already released");
1945+
1946+ ffa_priv_data = NULL;
1947+
1948+ return 0;
1949+}
1950+
1951+/**
1952+ * ffa_bus_ops_get - bus driver operations getter
1953+ *
1954+ * Return:
Andrew Geisslerea144b032023-01-27 16:03:57 -06001955+ * This function returns a pointer to the driver operations structure
Patrick Williams8dd68482022-10-04 07:57:18 -05001956+ */
Andrew Geisslerea144b032023-01-27 16:03:57 -06001957+const struct ffa_bus_ops *ffa_bus_ops_get(void)
Patrick Williams8dd68482022-10-04 07:57:18 -05001958+{
1959+ return &ffa_priv_data->ffa_ops;
1960+}
1961+
1962+/**
1963+ * ffa_bus_prvdata_get - bus driver private data getter
1964+ *
1965+ * Return:
Andrew Geisslerea144b032023-01-27 16:03:57 -06001966+ * This function returns a pointer to the main private data structure
Patrick Williams8dd68482022-10-04 07:57:18 -05001967+ */
Andrew Geisslerea144b032023-01-27 16:03:57 -06001968+struct ffa_prvdata *ffa_bus_prvdata_get(void)
Patrick Williams8dd68482022-10-04 07:57:18 -05001969+{
Andrew Geisslerea144b032023-01-27 16:03:57 -06001970+ return ffa_priv_data;
Patrick Williams8dd68482022-10-04 07:57:18 -05001971+}
1972+
1973+/**
Andrew Geisslerea144b032023-01-27 16:03:57 -06001974+ * ffa_bus_discover - discover FF-A bus and probe arm_ffa device
1975+ * @pdev: the address of a device pointer (to be filled when the arm_ffa bus device is created
1976+ * successfully)
Patrick Williams8dd68482022-10-04 07:57:18 -05001977+ *
Andrew Geisslerea144b032023-01-27 16:03:57 -06001978+ * This function makes sure the FF-A bus is discoverable.
1979+ * When probing succeeds FF-A discovery is done. The arm_ffa device is ready to use.
Patrick Williams8dd68482022-10-04 07:57:18 -05001980+ *
1981+ * When the bus was already discovered successfully the discovery will not run again.
1982+ *
1983+ * Arm FF-A transport is implemented through arm_ffa u-boot device managing the FF-A
1984+ * communication.
1985+ * All FF-A clients should use the arm_ffa device to use the FF-A transport.
1986+ *
1987+ * Return:
1988+ *
1989+ * 0 on success. Otherwise, failure
1990+ */
Andrew Geisslerea144b032023-01-27 16:03:57 -06001991+int ffa_bus_discover(struct udevice **pdev)
Patrick Williams8dd68482022-10-04 07:57:18 -05001992+{
1993+ int ret = 0;
1994+
Andrew Geisslerea144b032023-01-27 16:03:57 -06001995+ if (!ffa_priv_data) {
1996+ ret = ffa_device_get(pdev);
Patrick Williams8dd68482022-10-04 07:57:18 -05001997+
1998+ return ret;
1999+}
2000+
2001+/**
2002+ * Declaring the arm_ffa driver under UCLASS_FFA
2003+ */
2004+
2005+U_BOOT_DRIVER(arm_ffa) = {
2006+ .name = FFA_DRV_NAME,
2007+ .id = UCLASS_FFA,
2008+ .probe = ffa_probe,
2009+ .remove = ffa_remove,
2010+ .unbind = ffa_unbind,
2011+};
Patrick Williams8dd68482022-10-04 07:57:18 -05002012diff --git a/include/arm_ffa.h b/include/arm_ffa.h
2013new file mode 100644
Patrick Williams864cc432023-02-09 14:54:44 -06002014index 000000000000..74b16174c292
Patrick Williams8dd68482022-10-04 07:57:18 -05002015--- /dev/null
2016+++ b/include/arm_ffa.h
Andrew Geisslerea144b032023-01-27 16:03:57 -06002017@@ -0,0 +1,97 @@
Patrick Williams8dd68482022-10-04 07:57:18 -05002018+/* SPDX-License-Identifier: GPL-2.0+ */
2019+/*
2020+ * (C) Copyright 2022 ARM Limited
2021+ * Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
2022+ */
2023+
2024+#ifndef __ARM_FFA_H
2025+#define __ARM_FFA_H
2026+
2027+#include <linux/printk.h>
2028+
2029+/*
2030+ * This header is public. It can be used by clients to access
2031+ * data structures and definitions they need
2032+ */
2033+
2034+/*
2035+ * Macros for displaying logs
2036+ */
2037+
2038+#define ffa_info(fmt, ...) pr_info("[FFA] " fmt "\n", ##__VA_ARGS__)
2039+#define ffa_err(fmt, ...) pr_err("[FFA] " fmt "\n", ##__VA_ARGS__)
2040+
2041+/*
2042+ * struct ffa_partition_info - Partition information descriptor
2043+ * @id: Partition ID
2044+ * @exec_ctxt: Execution context count
2045+ * @properties: Partition properties
2046+ *
2047+ * Data structure containing information about partitions instantiated in the system
2048+ * This structure is filled with the data queried by FFA_PARTITION_INFO_GET
2049+ */
Andrew Geisslerea144b032023-01-27 16:03:57 -06002050+struct ffa_partition_info {
Patrick Williams8dd68482022-10-04 07:57:18 -05002051+ u16 id;
2052+ u16 exec_ctxt;
2053+/* partition supports receipt of direct requests */
2054+#define FFA_PARTITION_DIRECT_RECV BIT(0)
2055+/* partition can send direct requests. */
2056+#define FFA_PARTITION_DIRECT_SEND BIT(1)
2057+/* partition can send and receive indirect messages. */
2058+#define FFA_PARTITION_INDIRECT_MSG BIT(2)
2059+ u32 properties;
2060+};
2061+
2062+/*
2063+ * struct ffa_send_direct_data - Data structure hosting the data
2064+ * used by FFA_MSG_SEND_DIRECT_{REQ,RESP}
2065+ * @data0-4: Data read/written from/to x3-x7 registers
2066+ *
2067+ * Data structure containing the data to be sent by FFA_MSG_SEND_DIRECT_REQ
2068+ * or read from FFA_MSG_SEND_DIRECT_RESP
2069+ */
2070+
2071+/* For use with FFA_MSG_SEND_DIRECT_{REQ,RESP} which pass data via registers */
Andrew Geisslerea144b032023-01-27 16:03:57 -06002072+struct ffa_send_direct_data {
Patrick Williams8dd68482022-10-04 07:57:18 -05002073+ unsigned long data0; /* w3/x3 */
2074+ unsigned long data1; /* w4/x4 */
2075+ unsigned long data2; /* w5/x5 */
2076+ unsigned long data3; /* w6/x6 */
2077+ unsigned long data4; /* w7/x7 */
2078+};
2079+
Andrew Geisslerea144b032023-01-27 16:03:57 -06002080+struct udevice;
Patrick Williams8dd68482022-10-04 07:57:18 -05002081+
2082+/**
2083+ * struct ffa_bus_ops - The driver operations structure
2084+ * @partition_info_get: callback for the FFA_PARTITION_INFO_GET
2085+ * @sync_send_receive: callback for the FFA_MSG_SEND_DIRECT_REQ
2086+ * @rxtx_unmap: callback for the FFA_RXTX_UNMAP
2087+ *
2088+ * The data structure providing all the operations supported by the driver.
2089+ * This structure is EFI runtime resident.
2090+ */
2091+struct ffa_bus_ops {
Andrew Geisslerea144b032023-01-27 16:03:57 -06002092+ int (*partition_info_get)(struct udevice *dev, const char *uuid_str,
2093+ u32 *sp_count, struct ffa_partition_info *buffer);
2094+ int (*sync_send_receive)(struct udevice *dev, u16 dst_part_id,
2095+ struct ffa_send_direct_data *msg,
2096+ bool is_smc64);
2097+ int (*rxtx_unmap)(struct udevice *dev);
Patrick Williams8dd68482022-10-04 07:57:18 -05002098+};
2099+
2100+/**
2101+ * The device driver and the Uclass driver public functions
2102+ */
2103+
2104+/**
2105+ * ffa_bus_ops_get - driver operations getter
2106+ */
Andrew Geisslerea144b032023-01-27 16:03:57 -06002107+const struct ffa_bus_ops *ffa_bus_ops_get(void);
Patrick Williams8dd68482022-10-04 07:57:18 -05002108+
2109+/**
2110+ * ffa_bus_discover - discover FF-A bus and probes the arm_ffa device
2111+ */
Andrew Geisslerea144b032023-01-27 16:03:57 -06002112+int ffa_bus_discover(struct udevice **pdev);
Patrick Williams8dd68482022-10-04 07:57:18 -05002113+
2114+#endif
2115diff --git a/include/dm/uclass-id.h b/include/dm/uclass-id.h
Patrick Williams864cc432023-02-09 14:54:44 -06002116index 376f741cc2bb..fa08a66ac8e0 100644
Patrick Williams8dd68482022-10-04 07:57:18 -05002117--- a/include/dm/uclass-id.h
2118+++ b/include/dm/uclass-id.h
Andrew Geisslerea144b032023-01-27 16:03:57 -06002119@@ -4,6 +4,9 @@
2120 *
2121 * (C) Copyright 2012
2122 * Pavel Herrmann <morpheus.ibis@gmail.com>
2123+ *
2124+ * (C) Copyright 2022 ARM Limited
2125+ * Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
2126 */
2127
2128 #ifndef _DM_UCLASS_ID_H
2129@@ -55,6 +58,7 @@ enum uclass_id {
Patrick Williams8dd68482022-10-04 07:57:18 -05002130 UCLASS_EFI_MEDIA, /* Devices provided by UEFI firmware */
2131 UCLASS_ETH, /* Ethernet device */
2132 UCLASS_ETH_PHY, /* Ethernet PHY device */
2133+ UCLASS_FFA, /* Arm Firmware Framework for Armv8-A */
2134 UCLASS_FIRMWARE, /* Firmware */
Patrick Williams864cc432023-02-09 14:54:44 -06002135 UCLASS_FPGA, /* FPGA device */
Andrew Geisslerea144b032023-01-27 16:03:57 -06002136 UCLASS_FUZZING_ENGINE, /* Fuzzing engine */
Patrick Williams8dd68482022-10-04 07:57:18 -05002137--
Patrick Williams864cc432023-02-09 14:54:44 -060021382.39.1
Patrick Williams8dd68482022-10-04 07:57:18 -05002139