xref: /aosp_15_r20/trusty/kernel/platform/generic-arm64/debug.c (revision 344aa361028b423587d4ef3fa52a23d194628137)
1 /*
2  * Copyright (c) 2015 Google Inc. All rights reserved
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files
6  * (the "Software"), to deal in the Software without restriction,
7  * including without limitation the rights to use, copy, modify, merge,
8  * publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include <kernel/thread.h>
25 #include <kernel/vm.h>
26 #include <lib/device_tree/libfdt_helpers.h>
27 #include <lk/reg.h>
28 #include <lk/types.h>
29 #include <platform/debug.h>
30 
31 #include "debug.h"
32 
33 #if GENERIC_ARM64_DEBUG_SMC_DEBUG_PUTC
34 #include "smc.h"
35 #endif
36 
37 #if GENERIC_ARM64_DEBUG_FFA
38 #include <lib/arm_ffa/arm_ffa.h>
39 #endif
40 
41 #if GENERIC_ARM64_DEBUG_UART
42 enum uart_type {
43     UART_NONE,
44     UART_PL011,
45     UART_8250,
46 };
47 
48 static uint8_t* uart_base;
49 static enum uart_type uart_type;
50 
map_uart(paddr_t reg_paddr,enum uart_type new_uart_type)51 static void map_uart(paddr_t reg_paddr, enum uart_type new_uart_type) {
52     int ret;
53     void* page_vaddr;
54 
55     ASSERT(!uart_base);
56 
57     paddr_t reg_pbase = round_down(reg_paddr, PAGE_SIZE);
58     ret = vmm_alloc_physical(
59             vmm_get_kernel_aspace(), "uart", PAGE_SIZE, &page_vaddr, 0,
60             reg_pbase, 0,
61             ARCH_MMU_FLAG_PERM_NO_EXECUTE | ARCH_MMU_FLAG_UNCACHED_DEVICE);
62     if (ret) {
63         return;
64     }
65     uart_type = new_uart_type;
66     uart_base = (uint8_t*)page_vaddr + (reg_paddr - reg_pbase);
67 }
68 
generic_arm64_setup_uart(void * fdt)69 void generic_arm64_setup_uart(void* fdt) {
70     enum uart_type uart_type;
71     int fdt_chosen_offset = fdt_path_offset(fdt, "/chosen");
72     int fdt_stdout_path_len;
73     const char* fdt_stdout_path = fdt_getprop(
74             fdt, fdt_chosen_offset, "stdout-path", &fdt_stdout_path_len);
75     if (!fdt_stdout_path) {
76         return;
77     }
78     int fdt_stdout_offset = fdt_path_offset_namelen(fdt, fdt_stdout_path,
79                                                     fdt_stdout_path_len - 1);
80 
81     if (!fdt_node_check_compatible(fdt, fdt_stdout_offset, "arm,pl011")) {
82         uart_type = UART_PL011;
83     } else if (!fdt_node_check_compatible(fdt, fdt_stdout_offset, "ns8250") ||
84                !fdt_node_check_compatible(fdt, fdt_stdout_offset, "ns16550a")) {
85         uart_type = UART_8250;
86     } else {
87         return;
88     }
89 
90     paddr_t uart_reg;
91     if (fdt_helper_get_reg(fdt, fdt_stdout_offset, 0, &uart_reg, NULL)) {
92         return;
93     }
94 
95     map_uart(uart_reg, uart_type);
96 }
97 #endif
98 
platform_dputc(char c)99 void platform_dputc(char c) {
100 #if GENERIC_ARM64_DEBUG_SMC_DEBUG_PUTC
101     generic_arm64_smc(SMC_FC_DEBUG_PUTC, (unsigned long)c, 0, 0);
102 #elif GENERIC_ARM64_DEBUG_FFA
103     arm_ffa_console_log(&c, 1);
104 #elif GENERIC_ARM64_DEBUG_UART
105     if (!uart_base) {
106         return;
107     }
108     if (uart_type == UART_8250) {
109         while (!(readb(uart_base + 5) & (1U << 5))) {
110         }
111         writeb(c, uart_base + 0);
112     } else if (uart_type == UART_PL011) {
113         while ((*REG16(uart_base + 0x018) & (1U << 5))) {
114         }
115         *REG16(uart_base + 0x000) = c;
116     }
117 #else
118 #error Unsupported GENERIC_ARM64_DEBUG mode
119 #endif
120 }
121 
platform_dgetc(char * c,bool wait)122 int platform_dgetc(char* c, bool wait) {
123     int ret = -1;
124 
125     while (wait)
126         thread_sleep(100);
127 
128     return ret;
129 }
130