1 /***********************license start***************
2 * Copyright (c) 2003-2010 Cavium Inc. ([email protected]). All rights
3 * reserved.
4 *
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17
18 * * Neither the name of Cavium Inc. nor the names of
19 * its contributors may be used to endorse or promote products
20 * derived from this software without specific prior written
21 * permission.
22
23 * This Software, including technical data, may be subject to U.S. export control
24 * laws, including the U.S. Export Administration Act and its associated
25 * regulations, and may be subject to export or import regulations in other
26 * countries.
27
28 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
29 * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS OR
30 * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO
31 * THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR
32 * DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM
33 * SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE,
34 * MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF
35 * VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR
36 * CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK ARISING OUT OF USE OR
37 * PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
38 ***********************license end**************************************/
39
40
41 #ifdef CVMX_BUILD_FOR_LINUX_KERNEL
42 #include <linux/module.h>
43 #include <asm/octeon/cvmx.h>
44 #include <asm/octeon/cvmx-debug.h>
45 #include <asm/octeon/cvmx-uart.h>
46 #include <asm/octeon/octeon-boot-info.h>
47 #include <asm/octeon/cvmx-spinlock.h>
48
49 int cvmx_debug_uart = 1;
50
51 #else
52 #include <limits.h>
53 #include "executive-config.h"
54 #include "cvmx.h"
55 #include "cvmx-debug.h"
56 #include "cvmx-uart.h"
57 #include "cvmx-spinlock.h"
58 #include "octeon-boot-info.h"
59 #endif
60
61 /*
62 * NOTE: CARE SHOULD BE TAKEN USING STD C LIBRARY FUNCTIONS IN
63 * THIS FILE IF SOMEONE PUTS A BREAKPOINT ON THOSE FUNCTIONS
64 * DEBUGGING WILL FAIL.
65 */
66
67
68 #ifdef CVMX_BUILD_FOR_TOOLCHAIN
69 #pragma weak cvmx_uart_enable_intr
70 int cvmx_debug_uart = 1;
71 #endif
72
73
74 /* Default to second uart port for backward compatibility. The default (if
75 -debug does not set the uart number) can now be overridden with
76 CVMX_DEBUG_COMM_UART_NUM. */
77 #ifndef CVMX_DEBUG_COMM_UART_NUM
78 # define CVMX_DEBUG_COMM_UART_NUM 1
79 #endif
80
81 static CVMX_SHARED cvmx_spinlock_t cvmx_debug_uart_lock;
82
83 /**
84 * Interrupt handler for debugger Control-C interrupts.
85 *
86 * @param irq_number IRQ interrupt number
87 * @param registers CPU registers at the time of the interrupt
88 * @param user_arg Unused user argument
89 */
cvmx_debug_uart_process_debug_interrupt(int irq_number,uint64_t registers[32],void * user_arg)90 void cvmx_debug_uart_process_debug_interrupt(int irq_number, uint64_t registers[32], void *user_arg)
91 {
92 cvmx_uart_lsr_t lsrval;
93
94 /* Check for a Control-C interrupt from the debugger. This loop will eat
95 all input received on the uart */
96 lsrval.u64 = cvmx_read_csr(CVMX_MIO_UARTX_LSR(cvmx_debug_uart));
97 while (lsrval.s.dr)
98 {
99 int c = cvmx_read_csr(CVMX_MIO_UARTX_RBR(cvmx_debug_uart));
100 if (c == '\003')
101 {
102 register uint64_t tmp;
103 #ifndef CVMX_BUILD_FOR_LINUX_KERNEL
104 fflush(stderr);
105 fflush(stdout);
106 #endif
107 /* Pulse MCD0 signal on Ctrl-C to stop all the cores. Also
108 set the MCD0 to be not masked by this core so we know
109 the signal is received by someone */
110 asm volatile (
111 "dmfc0 %0, $22\n"
112 "ori %0, %0, 0x1110\n"
113 "dmtc0 %0, $22\n"
114 : "=r" (tmp));
115 }
116 lsrval.u64 = cvmx_read_csr(CVMX_MIO_UARTX_LSR(cvmx_debug_uart));
117 }
118 }
119
120
cvmx_debug_uart_init(void)121 static void cvmx_debug_uart_init(void)
122 {
123 if (cvmx_debug_uart == -1)
124 cvmx_debug_uart = CVMX_DEBUG_COMM_UART_NUM;
125 }
126
cvmx_debug_uart_install_break_handler(void)127 static void cvmx_debug_uart_install_break_handler(void)
128 {
129 #ifndef CVMX_BUILD_FOR_LINUX_KERNEL
130 #ifdef CVMX_BUILD_FOR_TOOLCHAIN
131 if (cvmx_uart_enable_intr)
132 #endif
133 cvmx_uart_enable_intr(cvmx_debug_uart, cvmx_debug_uart_process_debug_interrupt);
134 #endif
135 }
136
137 /**
138 * Routines to handle hex data
139 *
140 * @param ch
141 * @return
142 */
cvmx_debug_uart_hex(char ch)143 static inline int cvmx_debug_uart_hex(char ch)
144 {
145 if ((ch >= 'a') && (ch <= 'f'))
146 return(ch - 'a' + 10);
147 if ((ch >= '0') && (ch <= '9'))
148 return(ch - '0');
149 if ((ch >= 'A') && (ch <= 'F'))
150 return(ch - 'A' + 10);
151 return(-1);
152 }
153
154 /* Get a packet from the UART, return 0 on failure and 1 on success. */
155
cvmx_debug_uart_getpacket(char * buffer,size_t size)156 static int cvmx_debug_uart_getpacket(char *buffer, size_t size)
157 {
158 while (1)
159 {
160 unsigned char checksum;
161 int timedout = 0;
162 size_t count;
163 char ch;
164
165 ch = cvmx_uart_read_byte_with_timeout(cvmx_debug_uart, &timedout, __SHRT_MAX__);
166
167 if (timedout)
168 return 0;
169
170 /* if this is not the start character, ignore it. */
171 if (ch != '$')
172 continue;
173
174 retry:
175 checksum = 0;
176 count = 0;
177
178 /* now, read until a # or end of buffer is found */
179 while (count < size)
180 {
181 ch = cvmx_uart_read_byte(cvmx_debug_uart);
182 if (ch == '$')
183 goto retry;
184 if (ch == '#')
185 break;
186 checksum = checksum + ch;
187 buffer[count] = ch;
188 count = count + 1;
189 }
190 buffer[count] = 0;
191
192 if (ch == '#')
193 {
194 char csumchars0, csumchars1;
195 unsigned xmitcsum;
196 int n0, n1;
197
198 csumchars0 = cvmx_uart_read_byte(cvmx_debug_uart);
199 csumchars1 = cvmx_uart_read_byte(cvmx_debug_uart);
200 n0 = cvmx_debug_uart_hex(csumchars0);
201 n1 = cvmx_debug_uart_hex(csumchars1);
202 if (n0 == -1 || n1 == -1)
203 return 0;
204
205 xmitcsum = (n0 << 4) | n1;
206 return checksum == xmitcsum;
207 }
208 }
209 return 0;
210 }
211
212 /* Put the hex value of t into str. */
cvmx_debug_uart_strhex(char * str,unsigned char t)213 static void cvmx_debug_uart_strhex(char *str, unsigned char t)
214 {
215 char hexchar[] = "0123456789ABCDEF";
216 str[0] = hexchar[(t>>4)];
217 str[1] = hexchar[t&0xF];
218 str[2] = 0;
219 }
220
cvmx_debug_uart_putpacket(char * packet)221 static int cvmx_debug_uart_putpacket(char *packet)
222 {
223 size_t i;
224 unsigned char csum;
225 unsigned char *ptr = (unsigned char *) packet;
226 char csumstr[3];
227
228 for (csum = 0, i = 0; ptr[i]; i++)
229 csum += ptr[i];
230 cvmx_debug_uart_strhex(csumstr, csum);
231
232 cvmx_spinlock_lock(&cvmx_debug_uart_lock);
233 cvmx_uart_write_byte(cvmx_debug_uart, '$');
234 cvmx_uart_write_string(cvmx_debug_uart, packet);
235 cvmx_uart_write_byte(cvmx_debug_uart, '#');
236 cvmx_uart_write_string(cvmx_debug_uart, csumstr);
237 cvmx_spinlock_unlock(&cvmx_debug_uart_lock);
238
239 return 0;
240 }
241
cvmx_debug_uart_change_core(int oldcore,int newcore)242 static void cvmx_debug_uart_change_core(int oldcore, int newcore)
243 {
244 #ifndef CVMX_BUILD_FOR_LINUX_KERNEL
245 cvmx_ciu_intx0_t irq_control;
246
247 irq_control.u64 = cvmx_read_csr(CVMX_CIU_INTX_EN0(newcore * 2));
248 irq_control.s.uart |= (1u<<cvmx_debug_uart);
249 cvmx_write_csr(CVMX_CIU_INTX_EN0(newcore * 2), irq_control.u64);
250
251 /* Disable interrupts to this core since he is about to die */
252 irq_control.u64 = cvmx_read_csr(CVMX_CIU_INTX_EN0(oldcore * 2));
253 irq_control.s.uart &= ~(1u<<cvmx_debug_uart);
254 cvmx_write_csr(CVMX_CIU_INTX_EN0(oldcore* 2), irq_control.u64);
255 #endif
256 }
257
258 cvmx_debug_comm_t cvmx_debug_uart_comm =
259 {
260 .init = cvmx_debug_uart_init,
261 .install_break_handler = cvmx_debug_uart_install_break_handler,
262 .needs_proxy = 1,
263 .getpacket = cvmx_debug_uart_getpacket,
264 .putpacket = cvmx_debug_uart_putpacket,
265 .wait_for_resume = NULL,
266 .change_core = cvmx_debug_uart_change_core,
267 };
268