1From 57e4d6e9c5fc174a96366268150bc85de75baa79 Mon Sep 17 00:00:00 2001
2From: Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
3Date: Wed, 10 May 2023 17:27:01 +0100
4Subject: [PATCH] FF-A v15: arm_ffa: introduce armffa command
5
6Provide armffa command showcasing the use of the U-Boot FF-A support
7
8armffa is a command showcasing how to invoke FF-A operations.
9This provides a guidance to the client developers on how to
10call the FF-A bus interfaces. The command also allows to gather secure
11partitions information and ping these  partitions. The command is also
12helpful in testing the communication with secure partitions.
13
14For more details please refer to the command documentation [1].
15
16[1]: doc/usage/cmd/armffa.rst
17
18Signed-off-by: Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
19Reviewed-by: Simon Glass <sjg@chromium.org>
20Cc: Tom Rini <trini@konsulko.com>
21Cc: Ilias Apalodimas <ilias.apalodimas@linaro.org>
22Cc: Jens Wiklander <jens.wiklander@linaro.org>
23Cc: Heinrich Schuchardt <xypron.glpk@gmx.de>
24Upstream-Status: Submitted [cover letter: https://lore.kernel.org/all/20230713132847.176000-1-abdellatif.elkhlifi@arm.com/]
25---
26 MAINTAINERS                      |   2 +
27 cmd/Kconfig                      |  10 ++
28 cmd/Makefile                     |   1 +
29 cmd/armffa.c                     | 202 +++++++++++++++++++++++++++++++
30 doc/arch/arm64.ffa.rst           |   7 ++
31 doc/usage/cmd/armffa.rst         |  93 ++++++++++++++
32 doc/usage/index.rst              |   1 +
33 drivers/firmware/arm-ffa/Kconfig |   1 +
34 8 files changed, 317 insertions(+)
35 create mode 100644 cmd/armffa.c
36 create mode 100644 doc/usage/cmd/armffa.rst
37
38diff --git a/MAINTAINERS b/MAINTAINERS
39index 9c5ebf312c..4ae82229fc 100644
40--- a/MAINTAINERS
41+++ b/MAINTAINERS
42@@ -269,7 +269,9 @@ F:	configs/cortina_presidio-asic-pnand_defconfig
43 ARM FF-A
44 M:	Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
45 S:	Maintained
46+F:	cmd/armffa.c
47 F:	doc/arch/arm64.ffa.rst
48+F:	doc/usage/cmd/armffa.rst
49 F:	drivers/firmware/arm-ffa/
50 F:	include/arm_ffa.h
51 F:	include/sandbox_arm_ffa.h
52diff --git a/cmd/Kconfig b/cmd/Kconfig
53index 02e54f1e50..79b4f8367a 100644
54--- a/cmd/Kconfig
55+++ b/cmd/Kconfig
56@@ -935,6 +935,16 @@ endmenu
57
58 menu "Device access commands"
59
60+config CMD_ARMFFA
61+	bool "Arm FF-A test command"
62+	depends on ARM_FFA_TRANSPORT
63+	help
64+	  Provides a test command for the FF-A support
65+	  supported options:
66+		- Listing the partition(s) info
67+		- Sending a data pattern to the specified partition
68+		- Displaying the arm_ffa device info
69+
70 config CMD_ARMFLASH
71 	#depends on FLASH_CFI_DRIVER
72 	bool "armflash"
73diff --git a/cmd/Makefile b/cmd/Makefile
74index 6c37521b4e..7d20a85a46 100644
75--- a/cmd/Makefile
76+++ b/cmd/Makefile
77@@ -12,6 +12,7 @@ obj-y += panic.o
78 obj-y += version.o
79
80 # command
81+obj-$(CONFIG_CMD_ARMFFA) += armffa.o
82 obj-$(CONFIG_CMD_2048) += 2048.o
83 obj-$(CONFIG_CMD_ACPI) += acpi.o
84 obj-$(CONFIG_CMD_ADDRMAP) += addrmap.o
85diff --git a/cmd/armffa.c b/cmd/armffa.c
86new file mode 100644
87index 0000000000..7e6eafc03a
88--- /dev/null
89+++ b/cmd/armffa.c
90@@ -0,0 +1,202 @@
91+// SPDX-License-Identifier: GPL-2.0+
92+/*
93+ * Copyright 2022-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
94+ *
95+ * Authors:
96+ *   Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
97+ */
98+#include <common.h>
99+#include <arm_ffa.h>
100+#include <command.h>
101+#include <dm.h>
102+#include <mapmem.h>
103+#include <stdlib.h>
104+#include <asm/io.h>
105+
106+/* Select the right physical address formatting according to the platform */
107+#ifdef CONFIG_PHYS_64BIT
108+#define PhysAddrLength "ll"
109+#else
110+#define PhysAddrLength ""
111+#endif
112+#define PHYS_ADDR_LN "%" PhysAddrLength "x"
113+
114+/**
115+ * ffa_get_dev() - Return the FF-A device
116+ * @devp:	pointer to the FF-A device
117+ *
118+ * Search for the FF-A device.
119+ *
120+ * Return:
121+ * 0 on success. Otherwise, failure
122+ */
123+static int ffa_get_dev(struct udevice **devp)
124+{
125+	int ret;
126+
127+	ret = uclass_first_device_err(UCLASS_FFA, devp);
128+	if (ret) {
129+		log_err("Cannot find FF-A bus device\n");
130+		return ret;
131+	}
132+
133+	return 0;
134+}
135+
136+/**
137+ * do_ffa_getpart() - implementation of the getpart subcommand
138+ * @cmdtp:		Command Table
139+ * @flag:		flags
140+ * @argc:		number of arguments
141+ * @argv:		arguments
142+ *
143+ * Query a secure partition information. The secure partition UUID is provided
144+ * as an argument. The function uses the arm_ffa driver
145+ * partition_info_get operation which implements FFA_PARTITION_INFO_GET
146+ * ABI to retrieve the data. The input UUID string is expected to be in big
147+ * endian format.
148+ *
149+ * Return:
150+ *
151+ * CMD_RET_SUCCESS: on success, otherwise failure
152+ */
153+static int do_ffa_getpart(struct cmd_tbl *cmdtp, int flag, int argc,
154+			  char *const argv[])
155+{
156+	u32 count = 0;
157+	int ret;
158+	struct ffa_partition_desc *descs;
159+	u32 i;
160+	struct udevice *dev;
161+
162+	if (argc != 2) {
163+		log_err("Missing argument\n");
164+		return CMD_RET_USAGE;
165+	}
166+
167+	ret = ffa_get_dev(&dev);
168+	if (ret)
169+		return CMD_RET_FAILURE;
170+
171+	/* Ask the driver to fill the buffer with the SPs info */
172+
173+	ret = ffa_partition_info_get(dev, argv[1], &count, &descs);
174+	if (ret) {
175+		log_err("Failure in querying partition(s) info (error code: %d)\n", ret);
176+		return CMD_RET_FAILURE;
177+	}
178+
179+	/* SPs found , show the partition information */
180+	for (i = 0; i < count ; i++) {
181+		log_info("Partition: id = %x , exec_ctxt %x , properties %x\n",
182+			 descs[i].info.id,
183+			 descs[i].info.exec_ctxt,
184+			 descs[i].info.properties);
185+	}
186+
187+	return CMD_RET_SUCCESS;
188+}
189+
190+/**
191+ * do_ffa_ping() - implementation of the ping subcommand
192+ * @cmdtp:		Command Table
193+ * @flag:		flags
194+ * @argc:		number of arguments
195+ * @argv:		arguments
196+ *
197+ * Send data to a secure partition. The secure partition UUID is provided
198+ * as an argument. Use the arm_ffa driver sync_send_receive operation
199+ * which implements FFA_MSG_SEND_DIRECT_{REQ,RESP} ABIs to send/receive data.
200+ *
201+ * Return:
202+ *
203+ * CMD_RET_SUCCESS: on success, otherwise failure
204+ */
205+static int do_ffa_ping(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
206+{
207+	struct ffa_send_direct_data msg = {
208+			.data0 = 0xaaaaaaaa,
209+			.data1 = 0xbbbbbbbb,
210+			.data2 = 0xcccccccc,
211+			.data3 = 0xdddddddd,
212+			.data4 = 0xeeeeeeee,
213+	};
214+	u16 part_id;
215+	int ret;
216+	struct udevice *dev;
217+
218+	if (argc != 2) {
219+		log_err("Missing argument\n");
220+		return CMD_RET_USAGE;
221+	}
222+
223+	part_id = strtoul(argv[1], NULL, 16);
224+	if (!part_id) {
225+		log_err("Partition ID can not be 0\n");
226+		return CMD_RET_USAGE;
227+	}
228+
229+	ret = ffa_get_dev(&dev);
230+	if (ret)
231+		return CMD_RET_FAILURE;
232+
233+	ret = ffa_sync_send_receive(dev, part_id, &msg, 1);
234+	if (!ret) {
235+		u8 cnt;
236+
237+		log_info("SP response:\n[LSB]\n");
238+		for (cnt = 0;
239+		     cnt < sizeof(struct ffa_send_direct_data) / sizeof(u64);
240+		     cnt++)
241+			log_info("%llx\n", ((u64 *)&msg)[cnt]);
242+		return CMD_RET_SUCCESS;
243+	}
244+
245+	log_err("Sending direct request error (%d)\n", ret);
246+	return CMD_RET_FAILURE;
247+}
248+
249+/**
250+ *do_ffa_devlist() - implementation of the devlist subcommand
251+ * @cmdtp: [in]		Command Table
252+ * @flag:		flags
253+ * @argc:		number of arguments
254+ * @argv:		arguments
255+ *
256+ * Query the device belonging to the UCLASS_FFA
257+ * class.
258+ *
259+ * Return:
260+ *
261+ * CMD_RET_SUCCESS: on success, otherwise failure
262+ */
263+static int do_ffa_devlist(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
264+{
265+	struct udevice *dev;
266+	int ret;
267+
268+	ret = ffa_get_dev(&dev);
269+	if (ret)
270+		return CMD_RET_FAILURE;
271+
272+	log_info("device %s, addr " PHYS_ADDR_LN ", driver %s, ops " PHYS_ADDR_LN "\n",
273+		 dev->name,
274+		 map_to_sysmem(dev),
275+		 dev->driver->name,
276+		 map_to_sysmem(dev->driver->ops));
277+
278+	return CMD_RET_SUCCESS;
279+}
280+
281+static char armffa_help_text[] =
282+	"getpart <partition UUID>\n"
283+	"       - lists the partition(s) info\n"
284+	"ping <partition ID>\n"
285+	"       - sends a data pattern to the specified partition\n"
286+	"devlist\n"
287+	"       - displays information about the FF-A device/driver\n";
288+
289+U_BOOT_CMD_WITH_SUBCMDS(armffa, "Arm FF-A test command", armffa_help_text,
290+			U_BOOT_SUBCMD_MKENT(getpart, 2, 1, do_ffa_getpart),
291+			U_BOOT_SUBCMD_MKENT(ping, 2, 1, do_ffa_ping),
292+			U_BOOT_SUBCMD_MKENT(devlist, 1, 1, do_ffa_devlist));
293diff --git a/doc/arch/arm64.ffa.rst b/doc/arch/arm64.ffa.rst
294index 4f817f053c..aefd527447 100644
295--- a/doc/arch/arm64.ffa.rst
296+++ b/doc/arch/arm64.ffa.rst
297@@ -205,6 +205,13 @@ The following features are provided:
298
299 - FF-A bus can be compiled and used without EFI
300
301+The armffa command
302+-----------------------------------
303+
304+armffa is a command showcasing how to use the FF-A bus and how to invoke the driver operations.
305+
306+Please refer the command documentation at :doc:`../usage/cmd/armffa`
307+
308 Example of boot logs with FF-A enabled
309 --------------------------------------
310
311diff --git a/doc/usage/cmd/armffa.rst b/doc/usage/cmd/armffa.rst
312new file mode 100644
313index 0000000000..3d422686c1
314--- /dev/null
315+++ b/doc/usage/cmd/armffa.rst
316@@ -0,0 +1,93 @@
317+.. SPDX-License-Identifier: GPL-2.0+:
318+
319+armffa command
320+==============
321+
322+Synopsis
323+--------
324+
325+::
326+
327+   armffa [sub-command] [arguments]
328+
329+   sub-commands:
330+
331+        getpart [partition UUID]
332+
333+            lists the partition(s) info
334+
335+        ping [partition ID]
336+
337+            sends a data pattern to the specified partition
338+
339+        devlist
340+
341+            displays information about the FF-A device/driver
342+
343+Description
344+-----------
345+
346+armffa is a command showcasing how to use the FF-A bus and how to invoke its operations.
347+
348+This provides a guidance to the client developers on how to call the FF-A bus interfaces.
349+
350+The command also allows to gather secure partitions information and ping these  partitions.
351+
352+The command is also helpful in testing the communication with secure partitions.
353+
354+Example
355+-------
356+
357+The following examples are run on Corstone-1000 platform.
358+
359+* ping
360+
361+::
362+
363+   corstone1000# armffa ping 0x8003
364+   SP response:
365+   [LSB]
366+   fffffffe
367+   0
368+   0
369+   0
370+   0
371+
372+* ping (failure case)
373+
374+::
375+
376+   corstone1000# armffa ping 0
377+   Sending direct request error (-22)
378+
379+* getpart
380+
381+::
382+
383+   corstone1000# armffa getpart 33d532ed-e699-0942-c09c-a798d9cd722d
384+   Partition: id = 8003 , exec_ctxt 1 , properties 3
385+
386+* getpart (failure case)
387+
388+::
389+
390+   corstone1000# armffa getpart 33d532ed-e699-0942-c09c-a798d9cd7221
391+   INVALID_PARAMETERS: Unrecognized UUID
392+   Failure in querying partitions count (error code: -22)
393+
394+* devlist
395+
396+::
397+
398+   corstone1000# armffa devlist
399+   device name arm_ffa, dev 00000000fdf41c30, driver name arm_ffa, ops 00000000fffc0e98
400+
401+Configuration
402+-------------
403+
404+The command is available if CONFIG_CMD_ARMFFA=y and CONFIG_ARM_FFA_TRANSPORT=y.
405+
406+Return value
407+------------
408+
409+The return value $? is 0 (true) on success, 1 (false) on failure.
410diff --git a/doc/usage/index.rst b/doc/usage/index.rst
411index 388e59f173..e462de2806 100644
412--- a/doc/usage/index.rst
413+++ b/doc/usage/index.rst
414@@ -22,6 +22,7 @@ Shell commands
415
416    cmd/acpi
417    cmd/addrmap
418+   cmd/armffa
419    cmd/askenv
420    cmd/base
421    cmd/bdinfo
422diff --git a/drivers/firmware/arm-ffa/Kconfig b/drivers/firmware/arm-ffa/Kconfig
423index 9200c8028b..a7d5392859 100644
424--- a/drivers/firmware/arm-ffa/Kconfig
425+++ b/drivers/firmware/arm-ffa/Kconfig
426@@ -5,6 +5,7 @@ config ARM_FFA_TRANSPORT
427 	depends on DM && ARM64
428 	select ARM_SMCCC
429 	select ARM_SMCCC_FEATURES
430+	imply CMD_ARMFFA
431 	select LIB_UUID
432 	select DEVRES
433 	help
434