1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2022-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
4 *
5 * Authors:
6 *   Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
7 */
8#include <common.h>
9#include <arm_ffa.h>
10#include <dm.h>
11#include <log.h>
12#include <asm/global_data.h>
13#include <asm/sandbox_arm_ffa_priv.h>
14#include <dm/device-internal.h>
15#include <linux/errno.h>
16
17DECLARE_GLOBAL_DATA_PTR;
18
19/**
20 * sandbox_ffa_discover() - perform sandbox FF-A discovery
21 * @dev: The sandbox FF-A bus device
22 * Try to discover the FF-A framework. Discovery is performed by
23 * querying the FF-A framework version from secure world using the FFA_VERSION ABI.
24 * Return:
25 *
26 * 0 on success. Otherwise, failure
27 */
28static int sandbox_ffa_discover(struct udevice *dev)
29{
30	int ret;
31	struct udevice *emul;
32
33	log_debug("Emulated FF-A framework discovery\n");
34
35	ret = ffa_emul_find(dev, &emul);
36	if (ret) {
37		log_err("Cannot find FF-A emulator\n");
38		return ret;
39	}
40
41	ret = ffa_get_version_hdlr(dev);
42	if (ret)
43		return ret;
44
45	return 0;
46}
47
48/**
49 * sandbox_ffa_probe() - The sandbox FF-A driver probe function
50 * @dev:	the sandbox-arm-ffa device
51 * Save the emulator device in uc_priv.
52 * Return:
53 *
54 * 0 on success.
55 */
56static int sandbox_ffa_probe(struct udevice *dev)
57{
58	int ret;
59	struct ffa_priv *uc_priv = dev_get_uclass_priv(dev);
60
61	ret = uclass_first_device_err(UCLASS_FFA_EMUL, &uc_priv->emul);
62	if (ret) {
63		log_err("Cannot find FF-A emulator\n");
64		return ret;
65	}
66
67	return 0;
68}
69
70/**
71 * sandbox_ffa_bind() - The sandbox FF-A driver bind function
72 * @dev:	the sandbox-arm-ffa device
73 * Try to discover the emulated FF-A bus.
74 * Return:
75 *
76 * 0 on success.
77 */
78static int sandbox_ffa_bind(struct udevice *dev)
79{
80	int ret;
81
82	ret = sandbox_ffa_discover(dev);
83	if (ret)
84		return ret;
85
86	return 0;
87}
88
89/* Sandbox Arm FF-A emulator operations */
90
91static const struct ffa_bus_ops sandbox_ffa_ops = {
92	.partition_info_get = ffa_get_partitions_info_hdlr,
93	.sync_send_receive = ffa_msg_send_direct_req_hdlr,
94	.rxtx_unmap = ffa_unmap_rxtx_buffers_hdlr,
95};
96
97static const struct udevice_id sandbox_ffa_id[] = {
98	{ "sandbox,arm-ffa", 0 },
99	{ },
100};
101
102/* Declaring the sandbox FF-A driver under UCLASS_FFA */
103U_BOOT_DRIVER(sandbox_arm_ffa) = {
104	.name		= "sandbox_arm_ffa",
105	.of_match = sandbox_ffa_id,
106	.id		= UCLASS_FFA,
107	.bind		= sandbox_ffa_bind,
108	.probe		= sandbox_ffa_probe,
109	.ops		= &sandbox_ffa_ops,
110};
111