1/***********************license start*************** 2 * Copyright (c) 2003-2010 Cavium Inc. (support@cavium.com). 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 42 43 44 45 46/** 47 * @file 48 * 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". 52 * 53 * <hr>$Revision: 70030 $<hr> 54 */ 55#include "cvmx.h" 56#include "cvmx-core.h" 57#include "cvmx-log.h" 58 59#define CVMX_LOG_BUFFER_SIZE (1<<15) 60#define CVMX_LOG_NUM_BUFFERS 4 61 62/** 63 * The possible types of log data that can be stored in the 64 * buffer. 65 */ 66typedef enum 67{ 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 */ 73} cvmx_log_type_t; 74 75/** 76 * Header definition for each log entry. 77 */ 78typedef union 79{ 80 uint64_t u64; 81 struct 82 { 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 */ 86 } s; 87} cvmx_log_header_t; 88 89/** 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. 94 */ 95static uint64_t cvmx_log_buffers[CVMX_LOG_NUM_BUFFERS][CVMX_LOG_BUFFER_SIZE]; 96 97/** 98 * Current locations in the log. 99 */ 100uint64_t *cvmx_log_buffer_write_ptr = NULL; /* The next write will occur here */ 101uint64_t *cvmx_log_buffer_end_ptr = NULL; /* Write must move to the next buffer when it equals this */ 102uint64_t *cvmx_log_buffer_head_ptr = NULL; /* Pointer to begin extracting log data from */ 103static uint64_t *cvmx_log_buffer_read_ptr = NULL; /* Location cvmx_display is reading from */ 104static uint64_t *cvmx_log_buffer_read_end_ptr = NULL; /* Location where read will need the next buffer */ 105uint64_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. */ 108 109 110/** 111 * @INTERNAL 112 * Initialize the log for writing 113 */ 114static void __cvmx_log_initialize(void) CVMX_LOG_DISABLE_PC_LOGGING; 115static void __cvmx_log_initialize(void) 116{ 117 int buf_num; 118 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); 123 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; 127} 128 129 130/** 131 * @INTERNAL 132 * Called when the log is full of data. This function must 133 * make room for more log data before returning. 134 */ 135static void __cvmx_log_full_process(void) CVMX_LOG_DISABLE_PC_LOGGING; 136static void __cvmx_log_full_process(void) 137{ 138 if (cvmx_log_mcd0_on_full) 139 { 140 register uint64_t tmp; 141 /* Pulse MCD0 signal so a remote utility can extract the data */ 142 asm volatile ( 143 "dmfc0 %0, $22\n" 144 "ori %0, %0, 0x1110\n" 145 "dmtc0 %0, $22\n" 146 "nop\n" 147 "nop\n" 148 "nop\n" 149 "nop\n" 150 "nop\n" 151 "nop\n" 152 : "=r" (tmp)); 153 } 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)) 156 { 157 #ifndef __KERNEL__ 158 /* Disabled for the Linux kernel since printk is also profiled */ 159 cvmx_dprintf("Log is full, reusing first buffer\n"); 160 #endif 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); 166 } 167} 168 169 170/** 171 * @INTERNAL 172 * Simple inline function to build a log header 173 * 174 * @param type Type of header to build 175 * @param size Amount of data that follows the header in dwords 176 * @return The header 177 */ 178static inline uint64_t __cvmx_log_build_header(cvmx_log_type_t type, uint64_t size) CVMX_LOG_DISABLE_PC_LOGGING; 179static inline uint64_t __cvmx_log_build_header(cvmx_log_type_t type, uint64_t size) 180{ 181 cvmx_log_header_t header; 182 header.u64 = 0; 183 header.s.type = type; 184 header.s.size = size; 185 header.s.cycle = cvmx_get_cycle(); 186 return header.u64; 187} 188 189 190/** 191 * @INTERNAL 192 * Function to write and increment the position. It rotates 193 * to the next log buffer as necessary. 194 * 195 * @param data Data to write to the log 196 */ 197static inline void __cvmx_log_write(uint64_t data) CVMX_LOG_DISABLE_PC_LOGGING; 198static inline void __cvmx_log_write(uint64_t data) 199{ 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)) 202 { 203 /* No rotate is necessary, just write the data */ 204 *cvmx_log_buffer_write_ptr++ = data; 205 } 206 else 207 { 208 /* Initialize the log if necessary */ 209 if (cvmx_unlikely(cvmx_log_buffer_head_ptr == NULL)) 210 __cvmx_log_initialize(); 211 else 212 { 213 cvmx_log_buffer_write_ptr = CASTPTR(uint64_t, *cvmx_log_buffer_end_ptr); 214 if (cvmx_likely(cvmx_log_buffer_write_ptr)) 215 { 216 /* Rotate the log. Might be a good time to send the old buffer 217 somewhere */ 218 cvmx_log_buffer_end_ptr = cvmx_log_buffer_write_ptr + CVMX_LOG_BUFFER_SIZE-1; 219 } 220 else 221 __cvmx_log_full_process(); /* After this function returns, the log must be ready for updates */ 222 } 223 *cvmx_log_buffer_write_ptr++ = data; 224 } 225} 226 227 228/** 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. 232 * 233 * @param pc Program counter address to log 234 */ 235void cvmx_log_pc(uint64_t pc) CVMX_LOG_DISABLE_PC_LOGGING; 236void cvmx_log_pc(uint64_t pc) 237{ 238 __cvmx_log_write(__cvmx_log_build_header(CVMX_LOG_TYPE_PC, 1)); 239 __cvmx_log_write(pc); 240} 241 242 243/** 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. 247 * 248 * @param format Constant printf style format string. 249 */ 250void cvmx_log_printf0(const char *format) 251{ 252 __cvmx_log_write(__cvmx_log_build_header(CVMX_LOG_TYPE_PRINTF, 1)); 253 __cvmx_log_write(CAST64(format)); 254} 255 256 257/** 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. 261 * 262 * @param format Constant printf style format string. 263 * @param number1 64bit argument to the printf format string 264 */ 265void cvmx_log_printf1(const char *format, uint64_t number1) 266{ 267 __cvmx_log_write(__cvmx_log_build_header(CVMX_LOG_TYPE_PRINTF, 2)); 268 __cvmx_log_write(CAST64(format)); 269 __cvmx_log_write(number1); 270} 271 272 273/** 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. 277 * 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 281 */ 282void cvmx_log_printf2(const char *format, uint64_t number1, uint64_t number2) 283{ 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); 288} 289 290 291/** 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. 295 * 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 300 */ 301void cvmx_log_printf3(const char *format, uint64_t number1, uint64_t number2, uint64_t number3) 302{ 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); 308} 309 310 311/** 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. 315 * 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 321 */ 322void cvmx_log_printf4(const char *format, uint64_t number1, uint64_t number2, uint64_t number3, uint64_t number4) 323{ 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); 330} 331 332 333/** 334 * Log an arbitrary block of 64bit words. At most 255 64bit 335 * words can be logged. The words are copied into the log. 336 * 337 * @param size_in_dwords 338 * Number of 64bit dwords to copy into the log. 339 * @param data Array of 64bit dwords to copy 340 */ 341void cvmx_log_data(uint64_t size_in_dwords, const uint64_t *data) 342{ 343 if (size_in_dwords > 255) 344 size_in_dwords = 255; 345 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++); 349} 350 351 352/** 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. 356 * 357 * Example: 358 * cvmx_log_structure("cvmx_wqe_t", work, sizeof(*work)); 359 * 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. 366 */ 367void cvmx_log_structure(const char *type, void *data, int size_in_bytes) 368{ 369 uint64_t size_in_dwords = (size_in_bytes + 7) >> 3; 370 uint64_t *ptr = (uint64_t*)data; 371 372 if (size_in_dwords > 254) 373 size_in_dwords = 254; 374 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++); 379} 380 381 382/** 383 * Setup the mips performance counters 384 * 385 * @param counter1 Event type for counter 1 386 * @param counter2 Event type for counter 2 387 */ 388void cvmx_log_perf_setup(cvmx_core_perf_t counter1, cvmx_core_perf_t counter2) 389{ 390 cvmx_core_perf_control_t control; 391 392 control.u32 = 0; 393 control.s.event = counter1; 394 control.s.u = 1; 395 control.s.s = 1; 396 control.s.k = 1; 397 control.s.ex = 1; 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)); 401} 402 403 404/** 405 * Log the performance counters 406 */ 407void cvmx_log_perf(void) 408{ 409 uint64_t control1; 410 uint64_t control2; 411 uint64_t data1; 412 uint64_t data2; 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); 421} 422 423 424/** 425 * @INTERNAL 426 * Read a dword from the log 427 * 428 * @return the dword 429 */ 430static uint64_t __cvmx_log_read(void) CVMX_LOG_DISABLE_PC_LOGGING; 431static uint64_t __cvmx_log_read(void) 432{ 433 uint64_t data; 434 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)) 437 { 438 /* No rotate is necessary, just read the data */ 439 data = *cvmx_log_buffer_read_ptr++; 440 } 441 else 442 { 443 cvmx_log_buffer_read_ptr = CASTPTR(uint64_t, *cvmx_log_buffer_read_end_ptr); 444 if (cvmx_likely(cvmx_log_buffer_read_ptr)) 445 { 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++; 449 } 450 else 451 { 452 /* No more log buffers, return 0 */ 453 cvmx_log_buffer_read_end_ptr = NULL; 454 data = 0; 455 } 456 } 457 458 return data; 459} 460 461 462/** 463 * Display the current log in a human readable format. 464 */ 465void cvmx_log_display(void) 466{ 467 unsigned int i; 468 cvmx_log_header_t header; 469 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; 472 473 while (cvmx_log_buffer_read_ptr && (cvmx_log_buffer_read_ptr != cvmx_log_buffer_write_ptr)) 474 { 475 header.u64 = __cvmx_log_read(); 476 if (header.s.cycle == 0) 477 continue; 478 printf("%llu: ", (unsigned long long)header.s.cycle); 479 switch (header.s.type) 480 { 481 case CVMX_LOG_TYPE_PC: 482 if (header.s.size == 1) 483 printf("pc 0x%016llx\n", (unsigned long long)__cvmx_log_read()); 484 else 485 printf("Illegal size (%d) for log entry: pc\n", header.s.size); 486 break; 487 case CVMX_LOG_TYPE_PRINTF: 488 switch (header.s.size) 489 { 490 case 1: 491 printf(CASTPTR(const char, __cvmx_log_read())); 492 break; 493 case 2: 494 printf(CASTPTR(const char, __cvmx_log_read()), __cvmx_log_read()); 495 break; 496 case 3: 497 printf(CASTPTR(const char, __cvmx_log_read()), __cvmx_log_read(), __cvmx_log_read()); 498 break; 499 case 4: 500 printf(CASTPTR(const char, __cvmx_log_read()), __cvmx_log_read(), __cvmx_log_read(), __cvmx_log_read()); 501 break; 502 case 5: 503 printf(CASTPTR(const char, __cvmx_log_read()), __cvmx_log_read(), __cvmx_log_read(), __cvmx_log_read(), __cvmx_log_read()); 504 break; 505 default: 506 printf("Illegal size (%d) for log entry: printf\n", header.s.size); 507 break; 508 } 509 printf("\n"); 510 break; 511 case CVMX_LOG_TYPE_DATA: 512 printf("data"); 513 for (i=0; i<header.s.size; i++) 514 printf(" 0x%016llx", (unsigned long long)__cvmx_log_read()); 515 printf("\n"); 516 break; 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()); 521 printf("\n"); 522 break; 523 case CVMX_LOG_TYPE_PERF: 524 if (header.s.size == 3) 525 { 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); 530 } 531 else 532 printf("Illegal size (%d) for log entry: perf\n", header.s.size); 533 break; 534 default: 535 break; 536 } 537 } 538} 539 540