• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #if MMIO_GUARD_ENABLED
31 #include <err.h>
32 #include <lib/libhypervisor/libhypervisor.h>
33 #endif
34 
35 #include "debug.h"
36 
37 #if GENERIC_ARM64_DEBUG_SMC_DEBUG_PUTC
38 #include "smc.h"
39 #endif
40 
41 #if GENERIC_ARM64_DEBUG_FFA
42 #include <lib/arm_ffa/arm_ffa.h>
43 #endif
44 
45 #if GENERIC_ARM64_DEBUG_UART
46 enum uart_type {
47     UART_NONE,
48     UART_PL011,
49     UART_8250,
50 };
51 
52 static uint8_t* uart_base;
53 static enum uart_type uart_type;
54 
map_uart(paddr_t reg_paddr,enum uart_type new_uart_type)55 static void map_uart(paddr_t reg_paddr, enum uart_type new_uart_type) {
56     int ret;
57     void* page_vaddr;
58 
59     ASSERT(!uart_base);
60 
61     paddr_t reg_pbase = round_down(reg_paddr, PAGE_SIZE);
62     ret = vmm_alloc_physical(
63             vmm_get_kernel_aspace(), "uart", PAGE_SIZE, &page_vaddr, 0,
64             reg_pbase, 0,
65             ARCH_MMU_FLAG_PERM_NO_EXECUTE | ARCH_MMU_FLAG_UNCACHED_DEVICE);
66     if (ret) {
67         return;
68     }
69 
70 #if MMIO_GUARD_ENABLED
71     /*
72      * MMIO Guard map UART registers. Ignore not supported which implies that
73      * guard is not used.
74      *
75      * TODO: Figure out why we sometimes get ERR_INVALID_ARGS when MMIO Guard is
76      * not supported. It happens on qemu-generic-arm64-gicv3-test-debug builds.
77      */
78     ret = hypervisor_mmio_map_region(reg_pbase, PAGE_SIZE);
79     if (ret != NO_ERROR && ret != ERR_NOT_SUPPORTED &&
80         ret != ERR_INVALID_ARGS) {
81         dprintf(CRITICAL, "failed to mmio guard map uart. error=%d\n", ret);
82         return;
83     }
84 #endif
85 
86     uart_type = new_uart_type;
87     uart_base = (uint8_t*)page_vaddr + (reg_paddr - reg_pbase);
88 }
89 
generic_arm64_setup_uart(const void * fdt)90 void generic_arm64_setup_uart(const void* fdt) {
91     enum uart_type uart_type;
92     int fdt_chosen_offset = fdt_path_offset(fdt, "/chosen");
93     int fdt_stdout_path_len;
94     const char* fdt_stdout_path = fdt_getprop(
95             fdt, fdt_chosen_offset, "stdout-path", &fdt_stdout_path_len);
96     if (!fdt_stdout_path) {
97         return;
98     }
99     int fdt_stdout_offset = fdt_path_offset_namelen(fdt, fdt_stdout_path,
100                                                     fdt_stdout_path_len - 1);
101 
102     if (!fdt_node_check_compatible(fdt, fdt_stdout_offset, "arm,pl011")) {
103         uart_type = UART_PL011;
104     } else if (!fdt_node_check_compatible(fdt, fdt_stdout_offset, "ns8250") ||
105                !fdt_node_check_compatible(fdt, fdt_stdout_offset, "ns16550a")) {
106         uart_type = UART_8250;
107     } else {
108         return;
109     }
110 
111     paddr_t uart_reg;
112     if (fdt_helper_get_reg(fdt, fdt_stdout_offset, 0, &uart_reg, NULL)) {
113         return;
114     }
115 
116     map_uart(uart_reg, uart_type);
117 }
118 #endif
119 
platform_dputc(char c)120 void platform_dputc(char c) {
121 #if GENERIC_ARM64_DEBUG_SMC_DEBUG_PUTC
122     generic_arm64_smc(SMC_FC_DEBUG_PUTC, (unsigned long)c, 0, 0);
123 #elif GENERIC_ARM64_DEBUG_FFA
124     arm_ffa_console_log(&c, 1);
125 #elif GENERIC_ARM64_DEBUG_UART
126     if (!uart_base) {
127         return;
128     }
129     if (uart_type == UART_8250) {
130         while (!(readb(uart_base + 5) & (1U << 5))) {
131         }
132         writeb(c, uart_base + 0);
133     } else if (uart_type == UART_PL011) {
134         while ((*REG16(uart_base + 0x018) & (1U << 5))) {
135         }
136         *REG16(uart_base + 0x000) = c;
137     }
138 #else
139 #error Unsupported GENERIC_ARM64_DEBUG mode
140 #endif
141 }
142 
platform_dgetc(char * c,bool wait)143 int platform_dgetc(char* c, bool wait) {
144     int ret = -1;
145 
146     while (wait)
147         thread_sleep(100);
148 
149     return ret;
150 }
151