xref: /rk3399_ARM-atf/services/spd/trusty/generic-arm64-smcall.c (revision fd7b287cbe9147ca9e07dd9f30c49c58bbdd92a8)
1 /*
2  * Copyright (c) 2016-2019, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <stdio.h>
8 
9 #include <common/debug.h>
10 #include <common/runtime_svc.h>
11 #include <platform_def.h>
12 
13 #include "generic-arm64-smcall.h"
14 
15 int trusty_disable_serial_debug;
16 
17 struct dputc_state {
18 	char linebuf[128];
19 	unsigned l;
20 };
21 
22 static struct dputc_state dputc_state[2];
23 
24 static void trusty_dputc(char ch, int secure)
25 {
26 	unsigned i;
27 	struct dputc_state *s = &dputc_state[!secure];
28 
29 	if (trusty_disable_serial_debug)
30 		return;
31 
32 	s->linebuf[s->l++] = ch;
33 	if (s->l == sizeof(s->linebuf) || ch == '\n') {
34 		if (secure)
35 			printf("secure os: ");
36 		else
37 			printf("non-secure os: ");
38 		for (i = 0; i < s->l; i++) {
39 			putchar(s->linebuf[i]);
40 		}
41 		if (ch != '\n') {
42 			printf(" <...>\n");
43 		}
44 		s->l = 0;
45 	}
46 }
47 
48 static uint64_t trusty_get_reg_base(uint32_t reg)
49 {
50 	switch (reg) {
51 	case 0:
52 		return PLAT_ARM_GICD_BASE;
53 
54 	case 1:
55 		return PLAT_ARM_GICC_BASE;
56 
57 	default:
58 		NOTICE("%s(0x%x) unknown reg\n", __func__, reg);
59 		return SMC_UNK;
60 	}
61 }
62 
63 static uintptr_t trusty_generic_platform_smc(uint32_t smc_fid,
64 			 u_register_t x1,
65 			 u_register_t x2,
66 			 u_register_t x3,
67 			 u_register_t x4,
68 			 void *cookie,
69 			 void *handle,
70 			 u_register_t flags)
71 {
72 	switch (smc_fid) {
73 	case SMC_FC_DEBUG_PUTC:
74 		trusty_dputc(x1, is_caller_secure(flags));
75 		SMC_RET1(handle, 0);
76 
77 	case SMC_FC_GET_REG_BASE:
78 	case SMC_FC64_GET_REG_BASE:
79 		SMC_RET1(handle, trusty_get_reg_base(x1));
80 
81 	default:
82 		NOTICE("%s(0x%x, 0x%lx) unknown smc\n", __func__, smc_fid, x1);
83 		SMC_RET1(handle, SMC_UNK);
84 	}
85 }
86 
87 /* Define a SPD runtime service descriptor for fast SMC calls */
88 DECLARE_RT_SVC(
89 	trusty_fast,
90 
91 	SMC_ENTITY_PLATFORM_MONITOR,
92 	SMC_ENTITY_PLATFORM_MONITOR,
93 	SMC_TYPE_FAST,
94 	NULL,
95 	trusty_generic_platform_smc
96 );
97 
98