1 /***********************license start***************
2 * Copyright (c) 2003-2010 Cavium Inc. (support@cavium.com). All rights
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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
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
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**************************************/
49 * cvmx-log supplies a fast log buffer implementation. Each core writes
50 * log data to a differnet buffer to avoid synchronization overhead. Function
51 * call logging can be turned on with the GCC option "-pg".
53 * <hr>$Revision: 70030 $<hr>
56 #include "cvmx-core.h"
59 #define CVMX_LOG_BUFFER_SIZE (1<<15)
60 #define CVMX_LOG_NUM_BUFFERS 4
63 * The possible types of log data that can be stored in the
68 CVMX_LOG_TYPE_PC = 0, /**< Log of the program counter location. used for code profiling / tracing */
69 CVMX_LOG_TYPE_PRINTF, /**< Constant printf format string with two 64bit arguments */
70 CVMX_LOG_TYPE_DATA, /**< Arbitrary array of dwords. Max size is 31 dwords */
71 CVMX_LOG_TYPE_STRUCTURE,/**< Log a structured data element. Max size is 30 dwords */
72 CVMX_LOG_TYPE_PERF, /**< Mips performance counters control registers followed by the data */
76 * Header definition for each log entry.
83 cvmx_log_type_t type : 3; /* Data in the log entry */
84 uint64_t size : 8; /* Data size in 64bit words */
85 uint64_t cycle :53; /* Low bits of the cycle counter as a timestamp */
90 * Circular log buffer. Each processor gets a private one to
91 * write to. Log entries are added at the current write
92 * location, then the write location is incremented. The
93 * buffer may wrap in the middle of a log entry.
95 static uint64_t cvmx_log_buffers[CVMX_LOG_NUM_BUFFERS][CVMX_LOG_BUFFER_SIZE];
98 * Current locations in the log.
100 uint64_t *cvmx_log_buffer_write_ptr = NULL; /* The next write will occur here */
101 uint64_t *cvmx_log_buffer_end_ptr = NULL; /* Write must move to the next buffer when it equals this */
102 uint64_t *cvmx_log_buffer_head_ptr = NULL; /* Pointer to begin extracting log data from */
103 static uint64_t *cvmx_log_buffer_read_ptr = NULL; /* Location cvmx_display is reading from */
104 static uint64_t *cvmx_log_buffer_read_end_ptr = NULL; /* Location where read will need the next buffer */
105 uint64_t cvmx_log_mcd0_on_full = 0; /* If this is set, cvm-log will assert MCD0 when the log
106 is full. This is set by the remote logging utility through
107 the debugger interface. */
112 * Initialize the log for writing
114 static void __cvmx_log_initialize(void) CVMX_LOG_DISABLE_PC_LOGGING;
115 static void __cvmx_log_initialize(void)
119 /* Link the buffers together using the last element in each buffer */
120 for (buf_num=0; buf_num<CVMX_LOG_NUM_BUFFERS-1; buf_num++)
121 cvmx_log_buffers[buf_num][CVMX_LOG_BUFFER_SIZE-1] = CAST64(cvmx_log_buffers[buf_num+1]);
122 cvmx_log_buffers[CVMX_LOG_NUM_BUFFERS-1][CVMX_LOG_BUFFER_SIZE-1] = CAST64(NULL);
124 cvmx_log_buffer_head_ptr = &cvmx_log_buffers[0][0];
125 cvmx_log_buffer_write_ptr = &cvmx_log_buffers[0][0];
126 cvmx_log_buffer_end_ptr = cvmx_log_buffer_write_ptr + CVMX_LOG_BUFFER_SIZE-1;
132 * Called when the log is full of data. This function must
133 * make room for more log data before returning.
135 static void __cvmx_log_full_process(void) CVMX_LOG_DISABLE_PC_LOGGING;
136 static void __cvmx_log_full_process(void)
138 if (cvmx_log_mcd0_on_full)
140 register uint64_t tmp;
141 /* Pulse MCD0 signal so a remote utility can extract the data */
144 "ori %0, %0, 0x1110\n"
154 /* The write ptr may have been modifed by the debugger, check it again */
155 if (!(volatile uint64_t)CAST64(cvmx_log_buffer_write_ptr))
158 /* Disabled for the Linux kernel since printk is also profiled */
159 cvmx_dprintf("Log is full, reusing first buffer\n");
161 *cvmx_log_buffer_end_ptr = CAST64(cvmx_log_buffer_head_ptr);
162 cvmx_log_buffer_write_ptr = cvmx_log_buffer_head_ptr;
163 cvmx_log_buffer_end_ptr = cvmx_log_buffer_write_ptr + CVMX_LOG_BUFFER_SIZE-1;
164 cvmx_log_buffer_head_ptr = CASTPTR(uint64_t, *cvmx_log_buffer_end_ptr);
165 *cvmx_log_buffer_end_ptr = CAST64(NULL);
172 * Simple inline function to build a log header
174 * @param type Type of header to build
175 * @param size Amount of data that follows the header in dwords
178 static inline uint64_t __cvmx_log_build_header(cvmx_log_type_t type, uint64_t size) CVMX_LOG_DISABLE_PC_LOGGING;
179 static inline uint64_t __cvmx_log_build_header(cvmx_log_type_t type, uint64_t size)
181 cvmx_log_header_t header;
183 header.s.type = type;
184 header.s.size = size;
185 header.s.cycle = cvmx_get_cycle();
192 * Function to write and increment the position. It rotates
193 * to the next log buffer as necessary.
195 * @param data Data to write to the log
197 static inline void __cvmx_log_write(uint64_t data) CVMX_LOG_DISABLE_PC_LOGGING;
198 static inline void __cvmx_log_write(uint64_t data)
200 /* Check and see if we need to rotate the log */
201 if (cvmx_likely(cvmx_log_buffer_write_ptr != cvmx_log_buffer_end_ptr))
203 /* No rotate is necessary, just write the data */
204 *cvmx_log_buffer_write_ptr++ = data;
208 /* Initialize the log if necessary */
209 if (cvmx_unlikely(cvmx_log_buffer_head_ptr == NULL))
210 __cvmx_log_initialize();
213 cvmx_log_buffer_write_ptr = CASTPTR(uint64_t, *cvmx_log_buffer_end_ptr);
214 if (cvmx_likely(cvmx_log_buffer_write_ptr))
216 /* Rotate the log. Might be a good time to send the old buffer
218 cvmx_log_buffer_end_ptr = cvmx_log_buffer_write_ptr + CVMX_LOG_BUFFER_SIZE-1;
221 __cvmx_log_full_process(); /* After this function returns, the log must be ready for updates */
223 *cvmx_log_buffer_write_ptr++ = data;
229 * Log a program counter address to the log. This is caused by
230 * the assembly code function mcount when writing the PC value
231 * is more complicated that the simple case support by it.
233 * @param pc Program counter address to log
235 void cvmx_log_pc(uint64_t pc) CVMX_LOG_DISABLE_PC_LOGGING;
236 void cvmx_log_pc(uint64_t pc)
238 __cvmx_log_write(__cvmx_log_build_header(CVMX_LOG_TYPE_PC, 1));
239 __cvmx_log_write(pc);
244 * Log a constant printf style format string with 0 to 4
245 * arguments. The string must persist until the log is read,
246 * but the parameters are copied into the log.
248 * @param format Constant printf style format string.
250 void cvmx_log_printf0(const char *format)
252 __cvmx_log_write(__cvmx_log_build_header(CVMX_LOG_TYPE_PRINTF, 1));
253 __cvmx_log_write(CAST64(format));
258 * Log a constant printf style format string with 0 to 4
259 * arguments. The string must persist until the log is read,
260 * but the parameters are copied into the log.
262 * @param format Constant printf style format string.
263 * @param number1 64bit argument to the printf format string
265 void cvmx_log_printf1(const char *format, uint64_t number1)
267 __cvmx_log_write(__cvmx_log_build_header(CVMX_LOG_TYPE_PRINTF, 2));
268 __cvmx_log_write(CAST64(format));
269 __cvmx_log_write(number1);
274 * Log a constant printf style format string with 0 to 4
275 * arguments. The string must persist until the log is read,
276 * but the parameters are copied into the log.
278 * @param format Constant printf style format string.
279 * @param number1 64bit argument to the printf format string
280 * @param number2 64bit argument to the printf format string
282 void cvmx_log_printf2(const char *format, uint64_t number1, uint64_t number2)
284 __cvmx_log_write(__cvmx_log_build_header(CVMX_LOG_TYPE_PRINTF, 3));
285 __cvmx_log_write(CAST64(format));
286 __cvmx_log_write(number1);
287 __cvmx_log_write(number2);
292 * Log a constant printf style format string with 0 to 4
293 * arguments. The string must persist until the log is read,
294 * but the parameters are copied into the log.
296 * @param format Constant printf style format string.
297 * @param number1 64bit argument to the printf format string
298 * @param number2 64bit argument to the printf format string
299 * @param number3 64bit argument to the printf format string
301 void cvmx_log_printf3(const char *format, uint64_t number1, uint64_t number2, uint64_t number3)
303 __cvmx_log_write(__cvmx_log_build_header(CVMX_LOG_TYPE_PRINTF, 4));
304 __cvmx_log_write(CAST64(format));
305 __cvmx_log_write(number1);
306 __cvmx_log_write(number2);
307 __cvmx_log_write(number3);
312 * Log a constant printf style format string with 0 to 4
313 * arguments. The string must persist until the log is read,
314 * but the parameters are copied into the log.
316 * @param format Constant printf style format string.
317 * @param number1 64bit argument to the printf format string
318 * @param number2 64bit argument to the printf format string
319 * @param number3 64bit argument to the printf format string
320 * @param number4 64bit argument to the printf format string
322 void cvmx_log_printf4(const char *format, uint64_t number1, uint64_t number2, uint64_t number3, uint64_t number4)
324 __cvmx_log_write(__cvmx_log_build_header(CVMX_LOG_TYPE_PRINTF, 5));
325 __cvmx_log_write(CAST64(format));
326 __cvmx_log_write(number1);
327 __cvmx_log_write(number2);
328 __cvmx_log_write(number3);
329 __cvmx_log_write(number4);
334 * Log an arbitrary block of 64bit words. At most 255 64bit
335 * words can be logged. The words are copied into the log.
337 * @param size_in_dwords
338 * Number of 64bit dwords to copy into the log.
339 * @param data Array of 64bit dwords to copy
341 void cvmx_log_data(uint64_t size_in_dwords, const uint64_t *data)
343 if (size_in_dwords > 255)
344 size_in_dwords = 255;
346 __cvmx_log_write(__cvmx_log_build_header(CVMX_LOG_TYPE_DATA, size_in_dwords));
347 while (size_in_dwords--)
348 __cvmx_log_write(*data++);
353 * Log a structured data object. Post processing will use the
354 * debugging information in the ELF file to determine how to
355 * display the structure. Max of 2032 bytes.
358 * cvmx_log_structure("cvmx_wqe_t", work, sizeof(*work));
360 * @param type C typedef expressed as a string. This will be used to
361 * lookup the structure in the debugging infirmation.
362 * @param data Data to be written to the log.
363 * @param size_in_bytes
364 * Size if the data in bytes. Normally you'll use the
365 * sizeof() operator here.
367 void cvmx_log_structure(const char *type, void *data, int size_in_bytes)
369 uint64_t size_in_dwords = (size_in_bytes + 7) >> 3;
370 uint64_t *ptr = (uint64_t*)data;
372 if (size_in_dwords > 254)
373 size_in_dwords = 254;
375 __cvmx_log_write(__cvmx_log_build_header(CVMX_LOG_TYPE_STRUCTURE, size_in_dwords + 1));
376 __cvmx_log_write(CAST64(type));
377 while (size_in_dwords--)
378 __cvmx_log_write(*ptr++);
383 * Setup the mips performance counters
385 * @param counter1 Event type for counter 1
386 * @param counter2 Event type for counter 2
388 void cvmx_log_perf_setup(cvmx_core_perf_t counter1, cvmx_core_perf_t counter2)
390 cvmx_core_perf_control_t control;
393 control.s.event = counter1;
398 asm ("mtc0 %0, $25, 0\n" : : "r"(control.u32));
399 control.s.event = counter2;
400 asm ("mtc0 %0, $25, 2\n" : : "r"(control.u32));
405 * Log the performance counters
407 void cvmx_log_perf(void)
413 asm ("dmfc0 %0, $25, 1\n" : "=r"(data1));
414 asm ("dmfc0 %0, $25, 3\n" : "=r"(data2));
415 asm ("mfc0 %0, $25, 0\n" : "=r"(control1));
416 asm ("mfc0 %0, $25, 2\n" : "=r"(control2));
417 __cvmx_log_write(__cvmx_log_build_header(CVMX_LOG_TYPE_PERF, 3));
418 __cvmx_log_write(((control1 & 0xffffffff) << 32) | (control2 & 0xffffffff));
419 __cvmx_log_write(data1);
420 __cvmx_log_write(data2);
426 * Read a dword from the log
430 static uint64_t __cvmx_log_read(void) CVMX_LOG_DISABLE_PC_LOGGING;
431 static uint64_t __cvmx_log_read(void)
435 /* Check and see if we need to rotate the log */
436 if (cvmx_likely(cvmx_log_buffer_read_ptr != cvmx_log_buffer_read_end_ptr))
438 /* No rotate is necessary, just read the data */
439 data = *cvmx_log_buffer_read_ptr++;
443 cvmx_log_buffer_read_ptr = CASTPTR(uint64_t, *cvmx_log_buffer_read_end_ptr);
444 if (cvmx_likely(cvmx_log_buffer_read_ptr))
446 /* Rotate to the next log buffer */
447 cvmx_log_buffer_read_end_ptr = cvmx_log_buffer_read_ptr + CVMX_LOG_BUFFER_SIZE-1;
448 data = *cvmx_log_buffer_read_ptr++;
452 /* No more log buffers, return 0 */
453 cvmx_log_buffer_read_end_ptr = NULL;
463 * Display the current log in a human readable format.
465 void cvmx_log_display(void)
468 cvmx_log_header_t header;
470 cvmx_log_buffer_read_ptr = cvmx_log_buffer_head_ptr;
471 cvmx_log_buffer_read_end_ptr = cvmx_log_buffer_read_ptr + CVMX_LOG_BUFFER_SIZE-1;
473 while (cvmx_log_buffer_read_ptr && (cvmx_log_buffer_read_ptr != cvmx_log_buffer_write_ptr))
475 header.u64 = __cvmx_log_read();
476 if (header.s.cycle == 0)
478 printf("%llu: ", (unsigned long long)header.s.cycle);
479 switch (header.s.type)
481 case CVMX_LOG_TYPE_PC:
482 if (header.s.size == 1)
483 printf("pc 0x%016llx\n", (unsigned long long)__cvmx_log_read());
485 printf("Illegal size (%d) for log entry: pc\n", header.s.size);
487 case CVMX_LOG_TYPE_PRINTF:
488 switch (header.s.size)
491 printf(CASTPTR(const char, __cvmx_log_read()));
494 printf(CASTPTR(const char, __cvmx_log_read()), __cvmx_log_read());
497 printf(CASTPTR(const char, __cvmx_log_read()), __cvmx_log_read(), __cvmx_log_read());
500 printf(CASTPTR(const char, __cvmx_log_read()), __cvmx_log_read(), __cvmx_log_read(), __cvmx_log_read());
503 printf(CASTPTR(const char, __cvmx_log_read()), __cvmx_log_read(), __cvmx_log_read(), __cvmx_log_read(), __cvmx_log_read());
506 printf("Illegal size (%d) for log entry: printf\n", header.s.size);
511 case CVMX_LOG_TYPE_DATA:
513 for (i=0; i<header.s.size; i++)
514 printf(" 0x%016llx", (unsigned long long)__cvmx_log_read());
517 case CVMX_LOG_TYPE_STRUCTURE:
518 printf("struct %s", CASTPTR(const char, __cvmx_log_read()));
519 for (i=1; i<header.s.size; i++)
520 printf(" 0x%016llx", (unsigned long long)__cvmx_log_read());
523 case CVMX_LOG_TYPE_PERF:
524 if (header.s.size == 3)
526 unsigned long long control = __cvmx_log_read();
527 unsigned long long data1 = __cvmx_log_read();
528 unsigned long long data2 = __cvmx_log_read();
529 printf("perf control=0x%016llx data1=0x%016llx data2=0x%016llx\n", control, data1, data2);
532 printf("Illegal size (%d) for log entry: perf\n", header.s.size);