[Feature][T106_eSDK]update from T106-V2.01.01.02P56U09.AP.17.09_CAP.17.09.01 to T106-M42-PLXXXX-P56U11.AP.19.00_CAP.19.00.01 -- code

Only Configure: No
Affected branch: master
Affected module: unknow
Is it affected on both ZXIC and MTK: only ZXIC
Self-test: Yes
Doc Update: No

Change-Id: I5eb7f586f78a987785b0f9885f1300c42bfd6819
diff --git a/upstream/linux-5.10/kernel/ramdump/ramdump_client_cap.c b/upstream/linux-5.10/kernel/ramdump/ramdump_client_cap.c
index bcb6a53..1492b49 100755
--- a/upstream/linux-5.10/kernel/ramdump/ramdump_client_cap.c
+++ b/upstream/linux-5.10/kernel/ramdump/ramdump_client_cap.c
@@ -45,6 +45,7 @@
 extern void ramdump_data_transfer_to_device(void);
 extern void ramdump_oss_data_trans_init(void);
 extern unsigned char *ramdump_export_flag_base;
+extern void zxic_reset_reason(int reason, const char *cpu, const char *app);
 
 /*******************************************************************************
 *                              ¾Ö²¿¾²Ì¬±äÁ¿¶¨Òå                                *
@@ -71,7 +72,7 @@
 unsigned char *ramdump_cap_error_log  = NULL;
 unsigned int *cap_ddr_len_base        = NULL;
 unsigned int   sysctl_ramdump_emmc_size = 0x0;
-unsigned int   sysctl_ramdump_emmc_start_addr = 0xFFFF;
+u64 sysctl_ramdump_emmc_start_addr = 0xFFFF;
 
 static struct ctl_table cfg_ramdump_array[] = {
 #ifdef CONFIG_RAMDUMP_USER
@@ -128,7 +129,7 @@
 {
 	ramdump_msg_t *icp_msg = (ramdump_msg_t *)buf;
 
-	ramdump_server_exp_core = RAMDUMP_SUCCESS;
+	ramdump_server_exp_core = RAMDUMP_TRUE;
 
 	switch(icp_msg->msg_id)
 	{
@@ -413,6 +414,9 @@
 void ramdump_entry (void)
 {
 	unsigned long flags;
+
+	if (ramdump_server_exp_core == RAMDUMP_FALSE)
+		zxic_reset_reason(1, "cap", current->comm); /* not ap ramdump and cap ramdump */
 	if (sysctl_ramdump_on_panic == false)
 		return;
 
diff --git a/upstream/linux-5.10/kernel/ramdump/ramdump_device_trans.c b/upstream/linux-5.10/kernel/ramdump/ramdump_device_trans.c
index f3e91e9..0b0f0dc 100755
--- a/upstream/linux-5.10/kernel/ramdump/ramdump_device_trans.c
+++ b/upstream/linux-5.10/kernel/ramdump/ramdump_device_trans.c
@@ -51,6 +51,8 @@
 extern unsigned int ramdump_compress_flag;
 extern unsigned char *ramdump_log_buf;
 extern unsigned int ramdump_export_mode;
+extern unsigned int ramdump_emmc_size;
+extern unsigned int ramdump_spinand_size;
 
 /*******************************************************************************
  *                             Macro definitions                               *
@@ -288,6 +290,8 @@
 *******************************************************************************/
 void ramdump_device_close(void)
 {
+	g_ramdump_dev_fp->file_num = ramdump_device_file_cnt;
+	
 	if(ramdump_export_mode == RAMDUMP_MODE_EMMC)
 	{
 #ifdef CONFIG_RAMDUMP_EMMC
@@ -333,11 +337,16 @@
 int ramdump_device_write_file(ramdump_trans_server_file_info_req *server_to_cap)
 {
 	int ret = -1;
+	unsigned int file_size = 0;
+
+	/* Started by AICoder, pid:wcfb91c2aa35add146d90b5530cd112845133621 */
+	file_size = server_to_cap->file_size;
 	
 	if(ramdump_export_mode == RAMDUMP_MODE_EMMC)
 	{
 #ifdef CONFIG_RAMDUMP_EMMC
-		if (ramdump_emmc_offset >= RAMDUMP_TRANS_EMMC_LEN)
+		if ((ramdump_emmc_offset >= RAMDUMP_TRANS_EMMC_LEN)
+			|| ((ramdump_emmc_offset + file_size) > ramdump_emmc_size))
 			return -1;
 
 		ret = ramdump_fill_header(server_to_cap->file_name, 
@@ -349,9 +358,11 @@
 	else if(ramdump_export_mode == RAMDUMP_MODE_SPINAND)
 	{
 #ifdef CONFIG_MTD_SPI_NAND
-		if (ramdump_spinand_offset >= RAMDUMP_SPINAND_LEN)
+		if ((ramdump_spinand_offset >= RAMDUMP_SPINAND_LEN)
+			|| ((ramdump_spinand_offset + file_size) > ramdump_spinand_size))
 			return -1;
-
+		/* Ended by AICoder, pid:wcfb91c2aa35add146d90b5530cd112845133621 */
+		
 		ret = ramdump_fill_header(server_to_cap->file_name, 
 								  server_to_cap->file_size,
 								  &ramdump_spinand_fp, 
@@ -557,9 +568,9 @@
 int ramdump_device_write_data(ramdump_shmem_t *msg, unsigned int size, ssize_t *dstlen)
 {
 	int ret = 0;
-		
+
 	if(ramdump_export_mode == RAMDUMP_MODE_EMMC)
-	{		
+	{
 #ifdef CONFIG_RAMDUMP_EMMC
 		ret = ramdump_emmc_write_data(msg, &ramdump_device_fp, size);
 		if(ret < 0)
@@ -637,8 +648,6 @@
 				ramdump_trans_server_interactive_req *server_to_cap_msg = (ramdump_trans_server_interactive_req *)req_buf;
 				/* data from server to cap */
 				ramdump_file_num = server_to_cap_msg->file_num;
-				ramdump_device_fp.file_num  += ramdump_file_num;
-				ramdump_spinand_fp.file_num += ramdump_file_num;
 
 				/* data from cap to server */
 				cap_to_server_msg.cmd = RAMDUMP_PC_FILE_INFO_READ_REQ;
@@ -658,8 +667,12 @@
 				/*device memory file create*/
 				if(ramdump_device_write_file(server_to_cap_msg) == -1){
 					cap_to_server_msg.cmd = RAMDUMP_PC_FILE_TRANS_DONE_REQ;
-					ramdump_device_write_file_head();//±£Ö¤³ö´íǰ¼¸¸öÎļþ¾ùд¶Ô¡£
-					ramdump_printf("ramdump write emmc file error!\n");
+					/* Started by AICoder, pid:ddd3ag3c37x6798145ec08ac1067150b58735197 */
+					ramdump_oss_data_trans_write(
+					(unsigned char*)(&cap_to_server_msg), 
+					sizeof(cap_to_server_msg));	
+					break; 
+					/* Ended by AICoder, pid:ddd3ag3c37x6798145ec08ac1067150b58735197 */
 				}
 				file_size = server_to_cap_msg->file_size;
 				file_offset = 0;
@@ -675,7 +688,8 @@
 				file_trans_size = cap_to_server_msg.length;
 				file_left_size = file_left_size - cap_to_server_msg.length;
 				file_offset = file_offset + cap_to_server_msg.length;
-				printk("device memory trans file:%s !!!\n", server_to_cap_msg->file_name);
+				
+				printk("device memory trans file:%-30s size %9d, offset %9d!!!\n", server_to_cap_msg->file_name, file_size, ramdump_emmc_offset);
 				/* interactive data trans */
 				ramdump_oss_data_trans_write(
 					(unsigned char*)(&cap_to_server_msg), 
@@ -690,14 +704,24 @@
 				/* data from server to cap */
 				ramdump_shmem_t *server_to_cap_msg = (ramdump_shmem_t *)ramdump_shared_mem_base;
 				server_to_cap_msg->core_flag = 0;
+
 				/*data from cap to emmc*/
-				
 				write_len = ramdump_device_write_data(server_to_cap_msg, file_left_size, &file_dstlen);
-				if(write_len < 0)
+				if(write_len < 0 )
 				{
-					ramdump_printf("ramdump write emmc data error!\n");
+					/* Started by AICoder, pid:u5befs8483y615f142ce0bda306d660bed685275 */		
+					if(write_len == -RAMDUMP_NO_FREE_SPACE)
+					{
+						cap_to_server_msg.cmd = RAMDUMP_PC_FILE_TRANS_DONE_REQ;
+						ramdump_oss_data_trans_write(
+						(unsigned char*)(&cap_to_server_msg), 
+						sizeof(cap_to_server_msg));	
+						break;
+					}
+					else
+						ramdump_printf("ramdump write emmc data error!\n");
+					/* Ended by AICoder, pid:u5befs8483y615f142ce0bda306d660bed685275 */	
 				}
-				
 				/*ÅжÏÊ£Óà´óС*/
 				if (file_left_size == 0)
 				{
diff --git a/upstream/linux-5.10/kernel/ramdump/ramdump_emmc.c b/upstream/linux-5.10/kernel/ramdump/ramdump_emmc.c
index 0c28f27..5054440 100755
--- a/upstream/linux-5.10/kernel/ramdump/ramdump_emmc.c
+++ b/upstream/linux-5.10/kernel/ramdump/ramdump_emmc.c
@@ -128,16 +128,28 @@
 int ramdump_emmc_write_data(ramdump_shmem_t *msg, ramdump_file_t *fp, unsigned int size)
 {
 	int ret = 0;
-	unsigned int buffer = RAMDUMP_EMMC_ADDR + ramdump_emmc_offset;
+	u64 buffer = RAMDUMP_EMMC_ADDR + ramdump_emmc_offset;
 
 	if (ramdump_device_file_cnt >= RAMDUMP_FILE_NUM_MAX)
 		return -1;
 
 	while(1){
 		if ((msg->core_flag == 1) && (msg->rw_flag == 2)){
-			if(msg->size >= (ramdump_emmc_size - fp->file_fp[ramdump_device_file_cnt].offset))
-				return -1;
+			/* Started by AICoder, pid:fe298k6b27edc1c14f9e0be2e0451e1abfc5830e */
+			if((ramdump_emmc_size < ramdump_emmc_offset)
+				|| (msg->size >= (ramdump_emmc_size - fp->file_fp[ramdump_device_file_cnt].offset)))
+			{
+				printk("[ramdump] No space left in emmc, Emmc_size is %ld,ramdump_emmc_offset is %d!\n", ramdump_emmc_size, ramdump_emmc_offset);
+				return -RAMDUMP_NO_FREE_SPACE;
+			}
 			ret = mmc_bwrite(buffer, msg->size, msg->buf);
+			if(ret < 0)
+			{
+				printk("[ramdump] ramdump_emmc_write_data Error.\n");
+				ramdump_wait_delay(0);
+				continue;
+			}
+			/* Ended by AICoder, pid:fe298k6b27edc1c14f9e0be2e0451e1abfc5830e */
 			ramdump_emmc_offset = ramdump_emmc_offset + roundup(msg->size, RAMDUMP_EMMC_ALIGN_SIZE);
 			msg->core_flag = 1;
 			msg->rw_flag = 1;
diff --git a/upstream/linux-5.10/kernel/ramdump/ramdump_emmc.h b/upstream/linux-5.10/kernel/ramdump/ramdump_emmc.h
index 1028ab2..6c9817e 100755
--- a/upstream/linux-5.10/kernel/ramdump/ramdump_emmc.h
+++ b/upstream/linux-5.10/kernel/ramdump/ramdump_emmc.h
@@ -24,13 +24,14 @@
 /*******************************************************************************
 *                                Íⲿ±äÁ¿ÉùÃ÷                                  *
 *******************************************************************************/
-extern unsigned int sysctl_ramdump_emmc_start_addr;
+extern u64 sysctl_ramdump_emmc_start_addr;
 extern unsigned int sysctl_ramdump_emmc_size;
 extern volatile unsigned int ramdump_emmc_offset;
 
 /*******************************************************************************
 *                                   ºê¶¨Òå                                     *
 *******************************************************************************/
+#define RAMDUMP_NO_FREE_SPACE     (2)
 #define RAMDUMP_EMMC_ADDR         (sysctl_ramdump_emmc_start_addr * 512) 
 #define RAMDUMP_TRANS_EMMC_LEN    (sysctl_ramdump_emmc_size * 512)
 
diff --git a/upstream/linux-5.10/kernel/tracker.c b/upstream/linux-5.10/kernel/tracker.c
index 6f7e1ab..792818b 100755
--- a/upstream/linux-5.10/kernel/tracker.c
+++ b/upstream/linux-5.10/kernel/tracker.c
@@ -63,6 +63,7 @@
 #define OS_IRAM_SOFTIRQ_END        (OS_IRAM_SOFTIRQ_START + sizeof(t_os_iram_statistic))
 #define OS_IRAM_TIMER_START        (OS_IRAM_SOFTIRQ_END + sizeof(t_os_iram_statistic))
 #define OS_IRAM_TIMER_END          (OS_IRAM_TIMER_START + sizeof(t_os_iram_statistic))
+#define OS_IRAM_RESET_REASON_START (OS_STATISTIC_IRAM_BASE + 0x800 - sizeof(T_Reset_Reason))
 #endif
 
 #define os_statistic_check()       *((volatile unsigned long *)OS_STATISTIC_IRAM_BASE)
@@ -98,6 +99,12 @@
     } statistics[OS_DDR_STATISTIC_CNT];
 }t_os_ddr_statistic;
 
+typedef struct    
+{
+    char   ramdump_reason[32];      //±ÈÈ磺ramdump_ap_appname
+    char   kernel_reboot[32];       //±ÈÈ磺reboot_ap_appname
+} T_Reset_Reason;
+
 /*******************************************************************************
 *                                   È«¾Ö±äÁ¿                                   *
 *******************************************************************************/
@@ -134,6 +141,7 @@
 volatile  static t_os_ddr_statistic *g_os_ddr_softirq_end_statistic;
 volatile  static t_os_ddr_statistic *g_os_ddr_timer_start_statistic;
 volatile  static t_os_ddr_statistic *g_os_ddr_timer_end_statistic;
+volatile T_Reset_Reason *g_os_reset_reason;
 #endif
 
 /*******************************************************************************
@@ -418,7 +426,32 @@
     os_statistic_in_ddr(g_os_ddr_timer_end_statistic, func, time);
     os_statistic_info_update();
 }
+/*
+reason: 1 for ramdump, 2 for reboot
+cpu: ap/cap/rpm/phy
+app: current->comm
+*/
+/* Started by AICoder, pid:pf139dce4f7776c149ec081b508bae14e6084ede */
+void zxic_reset_reason(int reason, const char *cpu, const char *app)
+{
+	char buffer[32];
 
+	memset(buffer, 0, sizeof(buffer));
+	switch (reason)
+	{
+		case 1:
+			snprintf(buffer, 32, "reset_ramdump_%s_%s", cpu, app);
+			memcpy(g_os_reset_reason->ramdump_reason, buffer, sizeof(buffer));
+			break;
+		case 2:
+			snprintf(buffer, 32, "reset_kreboot_%s_%s", cpu, app);
+			memcpy(g_os_reset_reason->kernel_reboot, buffer, sizeof(buffer));
+			break;
+		default:
+			break;
+	}
+}
+/* Ended by AICoder, pid:pf139dce4f7776c149ec081b508bae14e6084ede */
 
 /*******************************************************************************
 * ¹¦ÄÜÃèÊö:     ¹ì¼£Í³¼Æµ½DDR
@@ -438,9 +471,10 @@
 #ifdef IRAM_BASE_ADDR_VA
     g_zxic_trace_apcpu_addr = IRAM_BASE_ADDR_OS_STATISTIC_PSCPU;
 #else
-    g_zxic_trace_apcpu_addr = ioremap(IRAM_BASE_ADDR_OS_STATISTIC_PSCPU, IRAM_BASE_LEN_OS_STATISTIC_PSCPU);
+    g_zxic_trace_apcpu_addr = ioremap(IRAM_BASE_ADDR_OS_STATISTIC_PSCPU, IRAM_BASE_LEN_OS_STATISTIC_PSCPU + IRAM_BASE_LEN_OS_STATISTIC_PHYCPU + IRAM_BASE_LEN_OS_STATISTIC_APCPU);
 #endif
 
+    g_os_reset_reason = (T_Reset_Reason *)OS_IRAM_RESET_REASON_START;
     /*
     init_timer(&timer);
     timer.expires = jiffies + 40*HZ;//msecs_to_jiffies(40*1000);//ÑÓ³Ù40Ãë