xref: /rk3399_ARM-atf/plat/qemu/qemu_sbsa/sbsa_sip_svc.c (revision c681d02c6ce2652307a4fcef16bd5626135dfad9)
1 /*
2  * Copyright (c) 2023, Linaro Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 
9 #include <common/runtime_svc.h>
10 #include <libfdt.h>
11 #include <smccc_helpers.h>
12 
13 /* default platform version is 0.0 */
14 static int platform_version_major;
15 static int platform_version_minor;
16 
17 #define SMC_FASTCALL       0x80000000
18 #define SMC64_FUNCTION     (SMC_FASTCALL   | 0x40000000)
19 #define SIP_FUNCTION       (SMC64_FUNCTION | 0x02000000)
20 #define SIP_FUNCTION_ID(n) (SIP_FUNCTION   | (n))
21 
22 /*
23  * We do not use SMCCC_ARCH_SOC_ID here because qemu_sbsa is virtual platform
24  * which uses SoC present in QEMU. And they can change on their own while we
25  * need version of whole 'virtual hardware platform'.
26  */
27 #define SIP_SVC_VERSION  SIP_FUNCTION_ID(1)
28 
29 void read_platform_version(void *dtb)
30 {
31 	int node;
32 
33 	node = fdt_path_offset(dtb, "/");
34 	if (node >= 0) {
35 		platform_version_major = fdt32_ld(fdt_getprop(dtb, node,
36 							      "machine-version-major", NULL));
37 		platform_version_minor = fdt32_ld(fdt_getprop(dtb, node,
38 							      "machine-version-minor", NULL));
39 	}
40 }
41 
42 void sip_svc_init(void)
43 {
44 	/* Read DeviceTree data before MMU is enabled */
45 
46 	void *dtb = (void *)(uintptr_t)ARM_PRELOADED_DTB_BASE;
47 	int err;
48 
49 	err = fdt_open_into(dtb, dtb, PLAT_QEMU_DT_MAX_SIZE);
50 	if (err < 0) {
51 		ERROR("Invalid Device Tree at %p: error %d\n", dtb, err);
52 		return;
53 	}
54 
55 	err = fdt_check_header(dtb);
56 	if (err < 0) {
57 		ERROR("Invalid DTB file passed\n");
58 		return;
59 	}
60 
61 	read_platform_version(dtb);
62 	INFO("Platform version: %d.%d\n", platform_version_major, platform_version_minor);
63 }
64 
65 /*
66  * This function is responsible for handling all SiP calls from the NS world
67  */
68 uintptr_t sbsa_sip_smc_handler(uint32_t smc_fid,
69 			       u_register_t x1,
70 			       u_register_t x2,
71 			       u_register_t x3,
72 			       u_register_t x4,
73 			       void *cookie,
74 			       void *handle,
75 			       u_register_t flags)
76 {
77 	uint32_t ns;
78 
79 	/* Determine which security state this SMC originated from */
80 	ns = is_caller_non_secure(flags);
81 	if (!ns) {
82 		ERROR("%s: wrong world SMC (0x%x)\n", __func__, smc_fid);
83 		SMC_RET1(handle, SMC_UNK);
84 	}
85 
86 	switch (smc_fid) {
87 	case SIP_SVC_VERSION:
88 		INFO("Platform version requested\n");
89 		SMC_RET3(handle, NULL, platform_version_major, platform_version_minor);
90 
91 	default:
92 		ERROR("%s: unhandled SMC (0x%x) (function id: %d)\n", __func__, smc_fid,
93 		      smc_fid - SIP_FUNCTION);
94 		SMC_RET1(handle, SMC_UNK);
95 	}
96 }
97 
98 int sbsa_sip_smc_setup(void)
99 {
100 	return 0;
101 }
102 
103 /* Define a runtime service descriptor for fast SMC calls */
104 DECLARE_RT_SVC(
105 	sbsa_sip_svc,
106 	OEN_SIP_START,
107 	OEN_SIP_END,
108 	SMC_TYPE_FAST,
109 	sbsa_sip_smc_setup,
110 	sbsa_sip_smc_handler
111 );
112