1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2020-2024 Intel Corporation 4 */ 5 6 #include <linux/ctype.h> 7 #include <linux/highmem.h> 8 #include <linux/fs.h> 9 #include <linux/slab.h> 10 #include <linux/moduleparam.h> 11 12 #include "vpu_boot_api.h" 13 #include "ivpu_drv.h" 14 #include "ivpu_fw.h" 15 #include "ivpu_fw_log.h" 16 #include "ivpu_gem.h" 17 18 #define IVPU_FW_LOG_LINE_LENGTH 256 19 20 unsigned int ivpu_fw_log_level = IVPU_FW_LOG_ERROR; 21 module_param_named(fw_log_level, ivpu_fw_log_level, uint, 0444); 22 MODULE_PARM_DESC(fw_log_level, 23 "NPU firmware default log level: debug=" __stringify(IVPU_FW_LOG_DEBUG) 24 " info=" __stringify(IVPU_FW_LOG_INFO) 25 " warn=" __stringify(IVPU_FW_LOG_WARN) 26 " error=" __stringify(IVPU_FW_LOG_ERROR) 27 " fatal=" __stringify(IVPU_FW_LOG_FATAL)); 28 29 static int fw_log_from_bo(struct ivpu_device *vdev, struct ivpu_bo *bo, u32 *offset, 30 struct vpu_tracing_buffer_header **out_log) 31 { 32 struct vpu_tracing_buffer_header *log; 33 34 if ((*offset + sizeof(*log)) > ivpu_bo_size(bo)) 35 return -EINVAL; 36 37 log = ivpu_bo_vaddr(bo) + *offset; 38 39 if (log->vpu_canary_start != VPU_TRACING_BUFFER_CANARY) 40 return -EINVAL; 41 42 if (log->header_size < sizeof(*log) || log->header_size > 1024) { 43 ivpu_dbg(vdev, FW_BOOT, "Invalid header size 0x%x\n", log->header_size); 44 return -EINVAL; 45 } 46 if (log->size < log->header_size) { 47 ivpu_dbg(vdev, FW_BOOT, "Invalid log size 0x%x\n", log->size); 48 return -EINVAL; 49 } 50 if ((char *)log + log->size > (char *)ivpu_bo_vaddr(bo) + ivpu_bo_size(bo)) { 51 ivpu_dbg(vdev, FW_BOOT, "Invalid log size 0x%x\n", log->size); 52 return -EINVAL; 53 } 54 55 *out_log = log; 56 *offset += log->size; 57 58 ivpu_dbg(vdev, FW_BOOT, 59 "FW log name \"%s\", write offset 0x%x size 0x%x, wrap count %d, hdr version %d size %d format %d, alignment %d", 60 log->name, log->write_index, log->size, log->wrap_count, log->header_version, 61 log->header_size, log->format, log->alignment); 62 63 return 0; 64 } 65 66 static void fw_log_print_lines(char *buffer, u32 size, struct drm_printer *p) 67 { 68 char line[IVPU_FW_LOG_LINE_LENGTH]; 69 u32 index = 0; 70 71 if (!size || !buffer) 72 return; 73 74 while (size--) { 75 if (*buffer == '\n' || *buffer == 0) { 76 line[index] = 0; 77 if (index != 0) 78 drm_printf(p, "%s\n", line); 79 index = 0; 80 buffer++; 81 continue; 82 } 83 if (index == IVPU_FW_LOG_LINE_LENGTH - 1) { 84 line[index] = 0; 85 index = 0; 86 drm_printf(p, "%s\n", line); 87 } 88 if (*buffer != '\r' && (isprint(*buffer) || iscntrl(*buffer))) 89 line[index++] = *buffer; 90 buffer++; 91 } 92 line[index] = 0; 93 if (index != 0) 94 drm_printf(p, "%s", line); 95 } 96 97 static void fw_log_print_buffer(struct vpu_tracing_buffer_header *log, const char *prefix, 98 bool only_new_msgs, struct drm_printer *p) 99 { 100 char *log_data = (void *)log + log->header_size; 101 u32 data_size = log->size - log->header_size; 102 u32 log_start = only_new_msgs ? READ_ONCE(log->read_index) : 0; 103 u32 log_end = READ_ONCE(log->write_index); 104 105 if (log_start >= data_size) 106 log_start = 0; 107 if (log_end > data_size) 108 log_end = data_size; 109 110 if (log->wrap_count == log->read_wrap_count) { 111 if (log_end <= log_start) { 112 drm_printf(p, "==== %s \"%s\" log empty ====\n", prefix, log->name); 113 return; 114 } 115 } else if (log->wrap_count == log->read_wrap_count + 1) { 116 if (log_end > log_start) 117 log_start = log_end; 118 } else { 119 log_start = log_end; 120 } 121 122 drm_printf(p, "==== %s \"%s\" log start ====\n", prefix, log->name); 123 if (log_end > log_start) { 124 fw_log_print_lines(log_data + log_start, log_end - log_start, p); 125 } else { 126 fw_log_print_lines(log_data + log_start, data_size - log_start, p); 127 fw_log_print_lines(log_data, log_end, p); 128 } 129 drm_printf(p, "\n\x1b[0m"); /* add new line and clear formatting */ 130 drm_printf(p, "==== %s \"%s\" log end ====\n", prefix, log->name); 131 } 132 133 static void 134 fw_log_print_all_in_bo(struct ivpu_device *vdev, const char *name, 135 struct ivpu_bo *bo, bool only_new_msgs, struct drm_printer *p) 136 { 137 struct vpu_tracing_buffer_header *log; 138 u32 next = 0; 139 140 while (fw_log_from_bo(vdev, bo, &next, &log) == 0) 141 fw_log_print_buffer(log, name, only_new_msgs, p); 142 } 143 144 void ivpu_fw_log_print(struct ivpu_device *vdev, bool only_new_msgs, struct drm_printer *p) 145 { 146 fw_log_print_all_in_bo(vdev, "NPU critical", vdev->fw->mem_log_crit, only_new_msgs, p); 147 fw_log_print_all_in_bo(vdev, "NPU verbose", vdev->fw->mem_log_verb, only_new_msgs, p); 148 } 149 150 void ivpu_fw_log_mark_read(struct ivpu_device *vdev) 151 { 152 struct vpu_tracing_buffer_header *log; 153 u32 next; 154 155 next = 0; 156 while (fw_log_from_bo(vdev, vdev->fw->mem_log_crit, &next, &log) == 0) { 157 log->read_index = READ_ONCE(log->write_index); 158 log->read_wrap_count = READ_ONCE(log->wrap_count); 159 } 160 161 next = 0; 162 while (fw_log_from_bo(vdev, vdev->fw->mem_log_verb, &next, &log) == 0) { 163 log->read_index = READ_ONCE(log->write_index); 164 log->read_wrap_count = READ_ONCE(log->wrap_count); 165 } 166 } 167 168 void ivpu_fw_log_reset(struct ivpu_device *vdev) 169 { 170 struct vpu_tracing_buffer_header *log; 171 u32 next; 172 173 next = 0; 174 while (fw_log_from_bo(vdev, vdev->fw->mem_log_crit, &next, &log) == 0) { 175 log->read_index = 0; 176 log->read_wrap_count = 0; 177 } 178 179 next = 0; 180 while (fw_log_from_bo(vdev, vdev->fw->mem_log_verb, &next, &log) == 0) { 181 log->read_index = 0; 182 log->read_wrap_count = 0; 183 } 184 } 185