l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 1 | |
| 2 | #include <linux/module.h> |
| 3 | #include <linux/kernel.h> |
| 4 | #include <linux/init.h> |
| 5 | #include <linux/cdev.h> |
| 6 | #include <linux/semaphore.h> |
| 7 | #include <linux/timer.h> |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 8 | #include <linux/ioport.h> |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 9 | #include <linux/poll.h> |
| 10 | #include <linux/delay.h> |
| 11 | #include <linux/wait.h> |
| 12 | #include <linux/err.h> |
| 13 | #include <linux/interrupt.h> |
| 14 | #include <linux/sched.h> |
| 15 | #include <linux/types.h> |
| 16 | #include <linux/device.h> |
| 17 | #include <linux/miscdevice.h> |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 18 | #include <linux/list.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/sched.h> |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 21 | #include <asm/io.h> |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 22 | #include <linux/vmalloc.h> |
| 23 | #include <linux/soc/zte/rpmsg.h> |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 24 | #include "pub_debug_info.h" |
| 25 | #include "ringbuf.h" |
l.yang | afee7ee | 2024-10-10 15:01:10 +0800 | [diff] [blame^] | 26 | #include "ram_config.h" |
| 27 | #include "ZspTrace.h" |
| 28 | #include <linux/proc_fs.h> |
| 29 | #include <asm/barrier.h> |
| 30 | #include <asm/cache.h> |
| 31 | #include <asm/cacheflush.h> |
| 32 | #include <linux/seq_file.h> |
| 33 | #include <linux/ktime.h> |
| 34 | #include <linux/time.h> |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 35 | |
l.yang | afee7ee | 2024-10-10 15:01:10 +0800 | [diff] [blame^] | 36 | #if defined(_USE_ZXIC_DEBUG_INFO) |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 37 | /******************************************************************************* |
| 38 | * 宏定义 * |
| 39 | *******************************************************************************/ |
l.yang | afee7ee | 2024-10-10 15:01:10 +0800 | [diff] [blame^] | 40 | #define DEBUG_INFO_AP_MEM_LEN (0x2000) |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 41 | #define DEBUG_INFO_READABLE_LEN (0x1400) |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 42 | #define DEBUG_INFO_READ_TIME_MSECS (10000) |
| 43 | |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 44 | #define DEBUG_INFO_OK (0) |
| 45 | #define DEBUG_INFO_ERROR (-1) |
l.yang | afee7ee | 2024-10-10 15:01:10 +0800 | [diff] [blame^] | 46 | #define DEBUG_READ_NOBLOCK (11) |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 47 | |
l.yang | afee7ee | 2024-10-10 15:01:10 +0800 | [diff] [blame^] | 48 | #define DEBUG_INFO_MAX_ID_LEN (16+3) |
| 49 | #define DEBUG_INFO_MAX_DATA_LEN (128) |
| 50 | #define DEBUG_INFO_MAX_TIME_LEN (0)//(20+3) |
| 51 | #define DEBUG_INFO_MAX_DATE_LEN (19+3) |
| 52 | #define DEBUG_INFO_MAX_TOTAL_LEN (DEBUG_INFO_MAX_ID_LEN + DEBUG_INFO_MAX_DATA_LEN + DEBUG_INFO_MAX_TIME_LEN) |
| 53 | |
| 54 | #define DEBUG_INFO_CORE_NUM (2) |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 55 | |
| 56 | /******************************************************************************* |
| 57 | * 结构体定义 * |
| 58 | *******************************************************************************/ |
l.yang | afee7ee | 2024-10-10 15:01:10 +0800 | [diff] [blame^] | 59 | typedef unsigned int UINT32; |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 60 | typedef unsigned short UINT16; |
l.yang | afee7ee | 2024-10-10 15:01:10 +0800 | [diff] [blame^] | 61 | typedef unsigned char UINT8; |
| 62 | typedef unsigned long UINT64; |
| 63 | typedef struct{ |
| 64 | unsigned int head; /* 0x010a0a0a */ |
| 65 | unsigned int datalen[DEBUG_INFO_CORE_NUM]; /* 数据内容长度 */ |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 66 | } T_SAVE_FILE_DATA; |
| 67 | |
| 68 | /******************************************************************************* |
| 69 | * 全局变量 * |
| 70 | *******************************************************************************/ |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 71 | static struct semaphore debug_sem; |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 72 | static int g_init_flag = 0; |
l.yang | afee7ee | 2024-10-10 15:01:10 +0800 | [diff] [blame^] | 73 | UINT32 *g_ps_debug_write_cnt; |
| 74 | UINT32 *g_ps_debug_full_cnt; |
| 75 | UINT32 g_ps_debug_read_cnt; |
| 76 | UINT32 g_ps_debug_read_full_cnt; |
| 77 | unsigned long g_debug_read_cnt; |
| 78 | unsigned long g_ap_debug_write_cnt; |
| 79 | unsigned long g_ap_debug_read_cnt; |
| 80 | UINT8 *g_ap_debug_buffer_start; |
| 81 | UINT8 *g_ap_debug_buffer_read; |
| 82 | UINT8 *g_ap_debug_buffer_write; |
| 83 | UINT8 *g_ap_debug_buffer_end; |
| 84 | UINT8 *g_ps_debug_buffer_start; |
| 85 | UINT8 *g_ps_debug_buffer_end; |
| 86 | UINT8 *g_ps_debug_buffer_read; |
| 87 | static DEFINE_SPINLOCK(debug_info_lock); |
| 88 | static char g_ap_debug_buffer[DEBUG_INFO_AP_MEM_LEN]; |
| 89 | unsigned long g_debug_read_state = 0; |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 90 | |
| 91 | /******************************************************************************* |
| 92 | * 内部函数定义 * |
| 93 | *******************************************************************************/ |
l.yang | afee7ee | 2024-10-10 15:01:10 +0800 | [diff] [blame^] | 94 | static int sc_debug_info_read_to_user(char *buf, size_t count); |
| 95 | static int sc_debug_info_record_from_user(const char *info, size_t count); |
| 96 | static void sc_debug_info_from_ps(void *buf, unsigned int len); |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 97 | static void kernel_timer_timeout(struct timer_list *t); |
| 98 | static ssize_t debug_info_read(struct file *fp, char __user *buf, size_t count, loff_t *pos); |
| 99 | static ssize_t debug_info_write(struct file *fp, const char __user *buf, size_t count, loff_t *pos); |
| 100 | static int debug_info_open(struct inode *ip, struct file *fp); |
| 101 | static long debug_info_ioctl(struct file *fp, unsigned int cmd, unsigned long arg); |
| 102 | static int debug_info_release(struct inode *ip, struct file *fp); |
l.yang | afee7ee | 2024-10-10 15:01:10 +0800 | [diff] [blame^] | 103 | extern u64 cop_time_get_value64(void); |
| 104 | extern int seq_write(struct seq_file *seq, const void *data, size_t len); |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 105 | static DEFINE_TIMER(timer, kernel_timer_timeout); |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 106 | static const struct file_operations debug_info_fops = { |
| 107 | .owner = THIS_MODULE, |
| 108 | .read = debug_info_read, |
| 109 | .write = debug_info_write, |
| 110 | .open = debug_info_open, |
| 111 | .unlocked_ioctl = debug_info_ioctl, |
| 112 | .release = debug_info_release, |
| 113 | }; |
| 114 | |
| 115 | static struct miscdevice debug_info_device = { |
| 116 | .minor = MISC_DYNAMIC_MINOR, |
| 117 | .name = "debug_info", |
| 118 | .fops = &debug_info_fops, |
| 119 | }; |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 120 | static void kernel_timer_timeout(struct timer_list *t) |
l.yang | afee7ee | 2024-10-10 15:01:10 +0800 | [diff] [blame^] | 121 | { |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 122 | if (debug_sem.count == 0) |
l.yang | afee7ee | 2024-10-10 15:01:10 +0800 | [diff] [blame^] | 123 | { |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 124 | up(&debug_sem); |
| 125 | } |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 126 | /* Kernel Timer restart */ |
l.yang | afee7ee | 2024-10-10 15:01:10 +0800 | [diff] [blame^] | 127 | mod_timer(&timer, jiffies + msecs_to_jiffies(DEBUG_INFO_READ_TIME_MSECS)); |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 128 | } |
| 129 | |
l.yang | afee7ee | 2024-10-10 15:01:10 +0800 | [diff] [blame^] | 130 | /* Started by AICoder, pid:y6ffbg9a06ieda114a190b4cb0ff4417bd78c0f4 */ |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 131 | static ssize_t debug_info_read(struct file *fp, char __user *buf, size_t count, loff_t *pos) |
| 132 | { |
| 133 | int ret; |
| 134 | int rd_len; |
| 135 | |
l.yang | afee7ee | 2024-10-10 15:01:10 +0800 | [diff] [blame^] | 136 | if (g_debug_read_state != DEBUG_READ_NOBLOCK) |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 137 | { |
l.yang | afee7ee | 2024-10-10 15:01:10 +0800 | [diff] [blame^] | 138 | ret = down_interruptible(&debug_sem); |
| 139 | if (ret < 0) |
| 140 | { |
| 141 | return ret; |
| 142 | } |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 143 | } |
l.yang | afee7ee | 2024-10-10 15:01:10 +0800 | [diff] [blame^] | 144 | |
| 145 | rd_len = sc_debug_info_read_to_user(buf, count); |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 146 | |
| 147 | return rd_len; |
| 148 | } |
l.yang | afee7ee | 2024-10-10 15:01:10 +0800 | [diff] [blame^] | 149 | /* Ended by AICoder, pid:y6ffbg9a06ieda114a190b4cb0ff4417bd78c0f4 */ |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 150 | |
| 151 | static ssize_t debug_info_write(struct file *fp, const char __user *buf, size_t count, loff_t *pos) |
| 152 | { |
| 153 | int wr_len = sc_debug_info_record_from_user(buf, count); |
| 154 | |
| 155 | return wr_len; |
| 156 | } |
| 157 | |
| 158 | static int debug_info_open(struct inode *ip, struct file *fp) |
| 159 | { |
| 160 | return 0; |
| 161 | } |
| 162 | |
l.yang | afee7ee | 2024-10-10 15:01:10 +0800 | [diff] [blame^] | 163 | /* Started by AICoder, pid:u635ed463ce5ab2145a109c3c0f54d149e6386b3 */ |
| 164 | static long debug_info_ioctl(struct file *fp, unsigned int cmd, unsigned long arg) { |
| 165 | if (cmd == DEBUG_READ_NOBLOCK) { |
| 166 | g_debug_read_state = DEBUG_READ_NOBLOCK; |
| 167 | } else { |
| 168 | // 如果命令不匹配,返回错误码 |
| 169 | return -EINVAL; |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 170 | } |
l.yang | afee7ee | 2024-10-10 15:01:10 +0800 | [diff] [blame^] | 171 | |
| 172 | // 记录调试信息 |
| 173 | sc_debug_info_record("debug_info", "read noblock!\n"); |
| 174 | |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 175 | return 0; |
| 176 | } |
l.yang | afee7ee | 2024-10-10 15:01:10 +0800 | [diff] [blame^] | 177 | /* Ended by AICoder, pid:u635ed463ce5ab2145a109c3c0f54d149e6386b3 */ |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 178 | |
| 179 | static int debug_info_release(struct inode *ip, struct file *fp) |
| 180 | { |
| 181 | return 0; |
| 182 | } |
| 183 | |
l.yang | afee7ee | 2024-10-10 15:01:10 +0800 | [diff] [blame^] | 184 | static int ap_log_proc_show(struct seq_file *m, void *v) |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 185 | { |
l.yang | afee7ee | 2024-10-10 15:01:10 +0800 | [diff] [blame^] | 186 | unsigned long writecnt = g_ap_debug_write_cnt; |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 187 | |
l.yang | afee7ee | 2024-10-10 15:01:10 +0800 | [diff] [blame^] | 188 | if (writecnt > DEBUG_INFO_AP_MEM_LEN) |
| 189 | { |
| 190 | writecnt = DEBUG_INFO_AP_MEM_LEN; |
| 191 | } |
| 192 | |
| 193 | seq_write(m, g_ap_debug_buffer_start, writecnt); |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | static int ps_log_proc_show(struct seq_file *m, void *v) |
| 199 | { |
| 200 | unsigned long writecnt = 0; |
| 201 | unsigned long fulllcnt = 0; |
| 202 | |
| 203 | writecnt = *(UINT32 *)g_ps_debug_write_cnt; |
| 204 | fulllcnt = *(UINT32 *)g_ps_debug_full_cnt; |
| 205 | |
| 206 | if (fulllcnt > 0) |
| 207 | { |
| 208 | writecnt = PS_DEBUG_INFO_SIZE; |
| 209 | } |
| 210 | |
| 211 | //__inval_dcache_area((void *)g_ps_debug_buffer_start, writecnt); |
| 212 | seq_write(m, g_ps_debug_buffer_start, writecnt); |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | UINT32 skip_end_null(char *buf, size_t count) |
| 218 | { |
| 219 | UINT32 len = 0; |
| 220 | char *tmpbuf = buf; |
| 221 | |
| 222 | while (count > 0) |
| 223 | { |
| 224 | if (*tmpbuf == '\0') |
| 225 | { |
| 226 | ++len; |
| 227 | tmpbuf -= 1; |
| 228 | count -= 1; |
| 229 | } |
| 230 | else |
| 231 | { |
| 232 | break; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | return len; |
| 237 | } |
| 238 | |
| 239 | int sc_debug_info_read_ap(char *buf, size_t count) |
| 240 | { |
| 241 | UINT32 readLen = 0; |
| 242 | UINT32 updatelen = 0; |
| 243 | UINT32 readremainLen = 0; |
| 244 | UINT8 *readbuffaddr = 0; |
| 245 | UINT32 nullLen = 0; |
| 246 | char *tmpbuf = buf; |
| 247 | |
| 248 | updatelen = g_ap_debug_write_cnt - g_ap_debug_read_cnt; |
| 249 | if (updatelen > DEBUG_INFO_AP_MEM_LEN) |
| 250 | updatelen = DEBUG_INFO_AP_MEM_LEN; |
| 251 | |
| 252 | if(g_ap_debug_buffer_end - g_ap_debug_buffer_read >= updatelen) |
| 253 | { |
| 254 | readbuffaddr = g_ap_debug_buffer_read; |
| 255 | readLen = updatelen; |
| 256 | if (copy_to_user(tmpbuf , readbuffaddr, readLen)) |
| 257 | { |
| 258 | return -EFAULT; |
| 259 | } |
| 260 | g_ap_debug_buffer_read += readLen; |
| 261 | } |
| 262 | else if(g_ap_debug_buffer_end - g_ap_debug_buffer_read < updatelen) |
| 263 | { |
| 264 | readbuffaddr = g_ap_debug_buffer_read; |
| 265 | readLen = g_ap_debug_buffer_end - readbuffaddr; |
| 266 | readremainLen = updatelen - readLen; |
| 267 | nullLen = skip_end_null(g_ap_debug_buffer_end - 1, readLen); |
| 268 | readLen = readLen - nullLen; |
| 269 | if (copy_to_user(tmpbuf, readbuffaddr, readLen)) |
| 270 | { |
| 271 | return -EFAULT; |
| 272 | } |
| 273 | if (copy_to_user(tmpbuf + readLen, g_ap_debug_buffer_start, readremainLen)) |
| 274 | { |
| 275 | return -EFAULT; |
| 276 | } |
| 277 | g_ap_debug_buffer_read = g_ap_debug_buffer_start + readremainLen; |
| 278 | } |
| 279 | else |
| 280 | { |
| 281 | panic("gaohf sc_debug_info_read_to_user error"); |
| 282 | } |
| 283 | g_ap_debug_read_cnt = g_ap_debug_write_cnt; |
| 284 | |
| 285 | return (readLen + readremainLen); |
| 286 | } |
| 287 | |
| 288 | int sc_debug_info_read_ps(char *buf, size_t count) |
| 289 | { |
| 290 | UINT32 readLen = 0; |
| 291 | UINT32 readremainLen = 0; |
| 292 | UINT8 *readbuffaddr = 0; |
| 293 | UINT32 fulllcnt = 0; |
| 294 | UINT32 fulllcntdiff = 0; |
| 295 | unsigned long writecnt = 0; |
| 296 | UINT32 nullLen = 0; |
| 297 | char *tmpbuf = buf; |
| 298 | |
| 299 | writecnt = *(volatile UINT32 *)g_ps_debug_write_cnt; |
| 300 | fulllcnt = *(volatile UINT32 *)g_ps_debug_full_cnt; |
| 301 | |
| 302 | if (fulllcnt >= g_ps_debug_read_full_cnt) |
| 303 | { |
| 304 | fulllcntdiff = fulllcnt - g_ps_debug_read_full_cnt; |
| 305 | } |
| 306 | else |
| 307 | { |
| 308 | fulllcntdiff = 0xFFFFFFFF - g_ps_debug_read_full_cnt + fulllcnt; |
| 309 | } |
| 310 | if(fulllcntdiff >= 2) |
| 311 | { |
| 312 | readbuffaddr = g_ps_debug_buffer_start + writecnt; |
| 313 | readLen = g_ps_debug_buffer_end - readbuffaddr; |
| 314 | readremainLen = writecnt; |
| 315 | nullLen = skip_end_null(g_ps_debug_buffer_end - 1, readLen); |
| 316 | readLen = readLen - nullLen; |
| 317 | if (copy_to_user(tmpbuf, readbuffaddr, readLen)) |
| 318 | { |
| 319 | return -EFAULT; |
| 320 | } |
| 321 | if (copy_to_user(tmpbuf + readLen, g_ps_debug_buffer_start, readremainLen)) |
| 322 | { |
| 323 | return -EFAULT; |
| 324 | } |
| 325 | g_ps_debug_buffer_read = g_ps_debug_buffer_start + readremainLen; |
| 326 | } |
| 327 | else if(fulllcntdiff == 0) |
| 328 | { |
| 329 | readbuffaddr = g_ps_debug_buffer_read; |
| 330 | readLen = writecnt - g_ps_debug_read_cnt;; |
| 331 | if (copy_to_user(tmpbuf , readbuffaddr, readLen)) |
| 332 | { |
| 333 | return -EFAULT; |
| 334 | } |
| 335 | g_ps_debug_buffer_read += readLen; |
| 336 | } |
| 337 | else if(fulllcntdiff == 1) |
| 338 | { |
| 339 | readLen = writecnt >= g_ps_debug_read_cnt ? writecnt : g_ps_debug_read_cnt; |
| 340 | readLen = PS_DEBUG_INFO_SIZE - readLen; |
| 341 | readbuffaddr = g_ps_debug_buffer_read; |
| 342 | nullLen = skip_end_null(g_ps_debug_buffer_end - 1, readLen); |
| 343 | readLen = readLen - nullLen; |
| 344 | if (copy_to_user(tmpbuf, readbuffaddr, readLen)) |
| 345 | { |
| 346 | return -EFAULT; |
| 347 | } |
| 348 | readremainLen = writecnt; |
| 349 | if (copy_to_user(tmpbuf + readLen , g_ps_debug_buffer_start, readremainLen)) |
| 350 | { |
| 351 | return -EFAULT; |
| 352 | } |
| 353 | g_ps_debug_buffer_read = g_ps_debug_buffer_start + readremainLen; |
| 354 | } |
| 355 | else |
| 356 | { |
| 357 | panic("gaohf sc_debug_info_read_to_user error"); |
| 358 | } |
| 359 | g_ps_debug_read_cnt = writecnt; |
| 360 | g_ps_debug_read_full_cnt = fulllcnt; |
| 361 | |
| 362 | return (readLen + readremainLen); |
| 363 | } |
| 364 | |
| 365 | int sc_debug_info_read_to_user(char *buf, size_t count) |
| 366 | { |
| 367 | char *tmpbuf = 0; |
| 368 | int ret = 0; |
| 369 | int ap_len = 0; |
| 370 | int ps_len = 0; |
| 371 | T_SAVE_FILE_DATA fileDataHead; |
| 372 | |
| 373 | if (g_init_flag == 0) |
| 374 | { |
| 375 | printk("debug_info not init.\n"); |
| 376 | return DEBUG_INFO_ERROR; |
| 377 | } |
| 378 | |
| 379 | if (count == 0 || buf == NULL ) |
| 380 | { |
| 381 | printk("sc_debug_info_read_to_user count == 0 || buf == NULL \n"); |
| 382 | return DEBUG_INFO_ERROR; |
| 383 | } |
| 384 | tmpbuf = buf + sizeof(T_SAVE_FILE_DATA); |
| 385 | ap_len = sc_debug_info_read_ap(tmpbuf, DEBUG_INFO_AP_MEM_LEN); |
| 386 | tmpbuf += ap_len; |
| 387 | ps_len = sc_debug_info_read_ps(tmpbuf, PS_DEBUG_INFO_SIZE); |
| 388 | |
| 389 | fileDataHead.head = 0x010a0a0a; |
| 390 | fileDataHead.datalen[0] = ap_len; |
| 391 | fileDataHead.datalen[1] = ps_len; |
| 392 | |
| 393 | if (copy_to_user(buf, &fileDataHead, sizeof(T_SAVE_FILE_DATA))) |
| 394 | { |
| 395 | printk("sc_debug_info_read_to_user copy_to_user error \n"); |
| 396 | return -EFAULT; |
| 397 | } |
| 398 | return sizeof(T_SAVE_FILE_DATA) + ap_len + ps_len; |
| 399 | } |
| 400 | |
| 401 | static int sc_debug_info_record_from_user(const char *info, size_t count) |
| 402 | { |
| 403 | UINT32 cnt = 0; |
| 404 | int spacelen; |
| 405 | int msg_len = 0; |
| 406 | struct tm tm; |
| 407 | time64_t time; |
| 408 | unsigned long used_space; |
| 409 | char buffer[DEBUG_INFO_MAX_TOTAL_LEN]; |
| 410 | unsigned long flags; |
| 411 | UINT8 *tmp_write_addr = buffer; |
| 412 | |
| 413 | if (g_init_flag == 0 || info == NULL) |
| 414 | { |
| 415 | printk("debug_info not init or sc_debug_info_record_from_user info is NULL\n"); |
| 416 | return DEBUG_INFO_ERROR; |
| 417 | } |
| 418 | |
| 419 | if(count > DEBUG_INFO_MAX_DATA_LEN + DEBUG_INFO_MAX_ID_LEN) |
| 420 | { |
| 421 | printk("debug_info data too long\n"); |
| 422 | return DEBUG_INFO_ERROR; |
| 423 | } |
| 424 | |
| 425 | time = ktime_get_real_seconds(); |
| 426 | time64_to_tm(time, 0, &tm); |
| 427 | cnt = snprintf((char *)tmp_write_addr, DEBUG_INFO_MAX_DATE_LEN, "[%ld-%02d-%02d %d:%02d:%02d]", \ |
| 428 | tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); |
| 429 | tmp_write_addr += cnt; |
| 430 | msg_len = cnt; |
| 431 | |
| 432 | spin_lock_irqsave(&debug_info_lock, flags); |
| 433 | spacelen = g_ap_debug_buffer_end - g_ap_debug_buffer_write; |
| 434 | if (spacelen < msg_len + count) |
| 435 | { |
| 436 | g_ap_debug_write_cnt += spacelen; |
| 437 | memset(g_ap_debug_buffer_write, '\0', spacelen); |
| 438 | g_ap_debug_buffer_write = g_ap_debug_buffer_start; |
| 439 | } |
| 440 | memcpy(g_ap_debug_buffer_write, buffer, msg_len); |
| 441 | if (copy_from_user(g_ap_debug_buffer_write + msg_len, info, count)) |
| 442 | return -EFAULT; |
| 443 | msg_len += count; |
| 444 | g_ap_debug_buffer_write += msg_len; |
| 445 | g_ap_debug_write_cnt += msg_len; |
| 446 | used_space = g_ap_debug_write_cnt - g_ap_debug_read_cnt; |
| 447 | spin_unlock_irqrestore(&debug_info_lock, flags); |
| 448 | if (used_space > DEBUG_INFO_READABLE_LEN) |
| 449 | { |
| 450 | if (debug_sem.count == 0) |
| 451 | { |
| 452 | up(&debug_sem); |
| 453 | } |
| 454 | } |
| 455 | return msg_len; |
| 456 | } |
| 457 | |
| 458 | int sc_debug_info_vrecord(char *id, const char *format, va_list args) |
| 459 | { |
| 460 | int spacelen; |
| 461 | int msg_len = 0; |
| 462 | int cnt = 0; |
| 463 | unsigned long used_space; |
| 464 | struct tm tm; |
| 465 | time64_t time; |
| 466 | char buffer[DEBUG_INFO_MAX_TOTAL_LEN]; |
| 467 | unsigned long flags; |
| 468 | UINT8 *tmp_write_addr = buffer; |
| 469 | |
| 470 | time = ktime_get_real_seconds(); |
| 471 | time64_to_tm(time, 0, &tm); |
| 472 | cnt = snprintf((char *)tmp_write_addr, DEBUG_INFO_MAX_DATE_LEN, "[%ld-%02d-%02d %d:%02d:%02d]", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); |
| 473 | tmp_write_addr += cnt; |
| 474 | msg_len = cnt; |
| 475 | |
| 476 | cnt = snprintf((char *)tmp_write_addr, DEBUG_INFO_MAX_ID_LEN, "[%s]",id); |
| 477 | tmp_write_addr += cnt; |
| 478 | msg_len += cnt; |
| 479 | msg_len += vsnprintf((char *)tmp_write_addr, DEBUG_INFO_MAX_DATA_LEN, format, args); |
| 480 | |
| 481 | spin_lock_irqsave(&debug_info_lock, flags); |
| 482 | spacelen = g_ap_debug_buffer_end - g_ap_debug_buffer_write; |
| 483 | if (spacelen < DEBUG_INFO_MAX_TOTAL_LEN) |
| 484 | { |
| 485 | g_ap_debug_write_cnt += spacelen; |
| 486 | memset(g_ap_debug_buffer_write, '\0', spacelen); |
| 487 | g_ap_debug_buffer_write = g_ap_debug_buffer_start; |
| 488 | } |
| 489 | |
| 490 | memcpy(g_ap_debug_buffer_write, buffer, msg_len); |
| 491 | g_ap_debug_buffer_write += msg_len; |
| 492 | g_ap_debug_write_cnt += msg_len; |
| 493 | used_space = g_ap_debug_write_cnt - g_ap_debug_read_cnt; |
| 494 | spin_unlock_irqrestore(&debug_info_lock, flags); |
| 495 | if (used_space > DEBUG_INFO_READABLE_LEN) |
| 496 | { |
| 497 | if (debug_sem.count == 0) |
| 498 | { |
| 499 | up(&debug_sem); |
| 500 | } |
| 501 | } |
| 502 | return msg_len; |
| 503 | } |
| 504 | EXPORT_SYMBOL(sc_debug_info_vrecord); |
| 505 | |
| 506 | int sc_debug_info_record(char *id, const char *format, ...) |
| 507 | { |
| 508 | va_list args; |
| 509 | int r; |
| 510 | |
| 511 | if (g_init_flag == 0) |
| 512 | { |
| 513 | printk("debug_info not init.\n"); |
| 514 | return DEBUG_INFO_ERROR; |
| 515 | } |
| 516 | |
| 517 | va_start(args, format); |
| 518 | r = sc_debug_info_vrecord(id, format, args); |
| 519 | va_end(args); |
| 520 | |
| 521 | return r; |
| 522 | } |
| 523 | EXPORT_SYMBOL(sc_debug_info_record); |
| 524 | |
| 525 | void early_debug_info_init(void) |
| 526 | { |
| 527 | g_ap_debug_buffer_start = g_ap_debug_buffer; |
| 528 | g_ap_debug_buffer_write = g_ap_debug_buffer_start; |
| 529 | g_ap_debug_buffer_read = g_ap_debug_buffer_start; |
| 530 | g_ap_debug_buffer_end = g_ap_debug_buffer + DEBUG_INFO_AP_MEM_LEN; |
| 531 | g_ap_debug_read_cnt = 0; |
| 532 | g_ap_debug_write_cnt = 0; |
| 533 | |
| 534 | g_init_flag = 1; |
| 535 | printk("cap early_debug_info_init success \n"); |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | static int __init debug_info_init(void) |
| 539 | { |
l.yang | afee7ee | 2024-10-10 15:01:10 +0800 | [diff] [blame^] | 540 | int ret; |
| 541 | |
| 542 | ret = misc_register(&debug_info_device); |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 543 | if (ret) |
| 544 | { |
l.yang | afee7ee | 2024-10-10 15:01:10 +0800 | [diff] [blame^] | 545 | printk("debug_info_init init.\n"); |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 546 | return DEBUG_INFO_ERROR; |
| 547 | } |
| 548 | |
l.yang | afee7ee | 2024-10-10 15:01:10 +0800 | [diff] [blame^] | 549 | g_ps_debug_write_cnt = (UINT32 *)ioremap(PS_DEBUG_INFO_LEN_ADDR_PA, PS_DEBUG_INFO_LEN_SIZE); |
| 550 | // printf("PS_DEBUG_INFO_LEN_ADDR_PA \n",) |
| 551 | g_ps_debug_full_cnt = (UINT32 *)((char *)g_ps_debug_write_cnt + 4); |
| 552 | g_ps_debug_buffer_start = (unsigned long)ioremap(PS_DEBUG_INFO_ADDR_PA, PS_DEBUG_INFO_SIZE); |
| 553 | g_ps_debug_buffer_read = g_ps_debug_buffer_start; |
| 554 | g_ps_debug_buffer_end = g_ps_debug_buffer_start + PS_DEBUG_INFO_SIZE; |
| 555 | g_ps_debug_read_cnt = 0; |
| 556 | g_ps_debug_read_full_cnt = 0; |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 557 | sema_init(&debug_sem, 0); |
l.yang | afee7ee | 2024-10-10 15:01:10 +0800 | [diff] [blame^] | 558 | proc_create_single("debug_info_cap", 0, NULL, ap_log_proc_show); |
| 559 | proc_create_single("debug_info_ap", 0, NULL, ps_log_proc_show); |
| 560 | mod_timer(&timer, jiffies + 1); |
| 561 | printk("cap debug_info_init success \n"); |
| 562 | // g_init_flag = 1; |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | static void __exit debug_info_exit(void) |
| 567 | { |
| 568 | misc_deregister(&debug_info_device); |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 569 | del_timer(&timer); |
| 570 | } |
| 571 | |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 572 | module_init(debug_info_init); |
| 573 | module_exit(debug_info_exit); |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 574 | MODULE_DESCRIPTION("debug_info driver"); |
| 575 | MODULE_LICENSE("GPL"); |
| 576 | |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 577 | #else |
l.yang | afee7ee | 2024-10-10 15:01:10 +0800 | [diff] [blame^] | 578 | int sc_debug_info_record(char *id, const char *format, ...) |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 579 | { |
l.yang | afee7ee | 2024-10-10 15:01:10 +0800 | [diff] [blame^] | 580 | return 0; |
l.yang | b8fdece | 2024-10-10 14:56:17 +0800 | [diff] [blame] | 581 | } |
| 582 | #endif /* _USE_ZXIC_DEBUG_INFO */ |
| 583 | |
l.yang | afee7ee | 2024-10-10 15:01:10 +0800 | [diff] [blame^] | 584 | |