platform: msm_shared: Fix drivers to enable WB cache

When second stage pt is enabled, the arch invalidate routine behaves
as arch clean invalidate and with second stage pt disabled arch invalidate
behaves as arch invalidate. With write back caches enabled some of the
drivers are doing invalidate of non-DMA buffers causing data loss and
resulting into crash. Remove the invalidation of non-DMA buffers to
avoid data loss. Perform clean invalidate when the heap driver allocates
memory which is aligned to cache line. Flush the qseecom logbuf before
passing it to tz.

Change-Id: Ib2fa5a5e0fa28e73e532ac241d7f251367df3be4
diff --git a/platform/msm_shared/dme.c b/platform/msm_shared/dme.c
index 47e94b0..f83ee93 100644
--- a/platform/msm_shared/dme.c
+++ b/platform/msm_shared/dme.c
@@ -291,19 +291,19 @@
 
 int dme_set_fdeviceinit(struct ufs_dev *dev)
 {
-	STACKBUF_DMA_ALIGN(result, sizeof(uint32_t));
+	uint32_t result;
 	uint32_t try_again                        = DME_FDEVICEINIT_RETRIES;
 	struct utp_query_req_upiu_type read_query = {UPIU_QUERY_OP_READ_FLAG,
 												 UFS_IDX_fDeviceInit,
 												 0,
 												 0,
-												 (addr_t) result,
+												 (addr_t) &result,
 												 sizeof(uint32_t)};
 	struct utp_query_req_upiu_type set_query  = {UPIU_QUERY_OP_SET_FLAG,
 												 UFS_IDX_fDeviceInit,
 												 0,
 												 0,
-												 (addr_t) result,
+												 (addr_t) &result,
 												 sizeof(uint32_t)};
 
 
@@ -313,9 +313,7 @@
 		return -UFS_FAILURE;
 	}
 
-	arch_invalidate_cache_range((addr_t) result, sizeof(uint32_t));
-
-	if (*result == 1)
+	if (result == 1)
 		goto utp_set_fdeviceinit_done;
 
 	do
@@ -334,7 +332,7 @@
 			return -UFS_FAILURE;
 		}
 
-		if (*result == 1)
+		if (result == 1)
 			break;
 	} while (try_again);
 
@@ -383,15 +381,13 @@
 
 int dme_read_device_desc(struct ufs_dev *dev)
 {
-	STACKBUF_DMA_ALIGN(dev_desc, sizeof(struct ufs_dev_desc));
-	STACKBUF_DMA_ALIGN(desc, sizeof(struct ufs_string_desc));
-	struct ufs_dev_desc            *device_desc = (struct ufs_dev_desc *) dev_desc;
-	struct ufs_string_desc         *str_desc    = (struct ufs_string_desc *) desc;
+	struct ufs_dev_desc            device_desc;
+	struct ufs_string_desc         str_desc;
 	struct utp_query_req_upiu_type query = {UPIU_QUERY_OP_READ_DESCRIPTOR,
 											UFS_DESC_IDN_DEVICE,
 											0,
 											0,
-											(addr_t) dev_desc,
+											(addr_t) &device_desc,
 											sizeof(struct ufs_dev_desc)};
 
 	if (dme_send_query_upiu(dev, &query))
@@ -400,20 +396,14 @@
 		return -UFS_FAILURE;
 	}
 
-	/* Flush buffer. */
-	arch_invalidate_cache_range((addr_t) device_desc, sizeof(struct ufs_dev_desc));
-
 	/* Store all relevant data */
-	dev->num_lus = device_desc->num_lu;
+	dev->num_lus = device_desc.num_lu;
 
 	/* Get serial number for the device based on the string index. */
-	if (dme_read_string_desc(dev, device_desc->serial_num, (struct ufs_string_desc *) desc))
+	if (dme_read_string_desc(dev, device_desc.serial_num, (struct ufs_string_desc *) &str_desc))
 		return -UFS_FAILURE;
 
-	/* Flush buffer. */
-	arch_invalidate_cache_range((addr_t) str_desc, sizeof(struct ufs_string_desc));
-
-	dev->serial_num = dme_parse_serial_no(str_desc);
+	dev->serial_num = dme_parse_serial_no(&str_desc);
 
 	return UFS_SUCCESS;
 }
@@ -437,21 +427,19 @@
 		return -UFS_FAILURE;
 	}
 
-	// Flush buffer.
-	arch_invalidate_cache_range((addr_t) desc, sizeof(struct ufs_geometry_desc));
 	dev->rpmb_rw_size = desc->rpmb_read_write_size;
 	return UFS_SUCCESS;
 }
 
 int dme_read_unit_desc(struct ufs_dev *dev, uint8_t index)
 {
-	STACKBUF_DMA_ALIGN(unit_desc, sizeof(struct ufs_unit_desc));
-	struct ufs_unit_desc           *desc = (struct ufs_unit_desc *) unit_desc;
+	struct ufs_unit_desc unit_desc;
+	struct ufs_unit_desc           *desc = (struct ufs_unit_desc *) &unit_desc;
 	struct utp_query_req_upiu_type query = {UPIU_QUERY_OP_READ_DESCRIPTOR,
 											UFS_DESC_IDN_UNIT,
 											index,
 											0,
-											(addr_t) unit_desc,
+											(addr_t) &unit_desc,
 											sizeof(struct ufs_unit_desc)};
 
 	if (dme_send_query_upiu(dev, &query))
@@ -459,8 +447,6 @@
 		dprintf(CRITICAL, "%s:%d DME Read Unit Descriptor request failed\n", __func__, __LINE__);
 		return -UFS_FAILURE;
 	}
-	/* Flush buffer. */
-	arch_invalidate_cache_range((addr_t) desc, sizeof(struct ufs_unit_desc));
 
 	dev->lun_cfg[index].logical_blk_cnt = BE64(desc->logical_blk_cnt);
 
diff --git a/platform/msm_shared/qseecom_lk.c b/platform/msm_shared/qseecom_lk.c
index 4374601..0c9077b 100644
--- a/platform/msm_shared/qseecom_lk.c
+++ b/platform/msm_shared/qseecom_lk.c
@@ -1346,6 +1346,8 @@
 		goto err;
 	}
 	memset(buf, 0, ROUNDUP(QSEE_LOG_BUF_SIZE, PAGE_SIZE));
+	/* Make sure the buffer given to TZ is flushed */
+	arch_clean_invalidate_cache_range((addr_t) buf, QSEE_LOG_BUF_SIZE);
 	logbuf_req.qsee_cmd_id = QSEE_REGISTER_LOG_BUF_COMMAND;
 	logbuf_req.phy_addr = (uint32_t)__qseecom_uvirt_to_kphys((uint32_t) buf);
 	logbuf_req.len = QSEE_LOG_BUF_SIZE;
diff --git a/platform/msm_shared/rpm-glink.c b/platform/msm_shared/rpm-glink.c
index 406f2ea..2635cd1 100644
--- a/platform/msm_shared/rpm-glink.c
+++ b/platform/msm_shared/rpm-glink.c
@@ -137,8 +137,6 @@
 	}
 	resp = (rpm_ack_msg *)rx_buffer;
 
-	arch_invalidate_cache_range((addr_t)resp, sizeof(rpm_gen_hdr));
-
 	if(resp->hdr.type == RPM_CMD_MAGIC)
 	{
 		type = RPM_CMD_TYPE;
diff --git a/platform/msm_shared/scm.c b/platform/msm_shared/scm.c
index 2dda14e..df4b136 100644
--- a/platform/msm_shared/scm.c
+++ b/platform/msm_shared/scm.c
@@ -1202,7 +1202,7 @@
 		{
 			indir_arg[i] = arg->x5[i];
 		}
-		arch_clean_invalidate_cache_range((addr_t) indir_arg, ROUNDUP((SCM_INDIR_MAX_LEN * sizeof(uint32_t)), CACHE_LINE));
+		arch_clean_invalidate_cache_range((addr_t) indir_arg, (SCM_INDIR_MAX_LEN * sizeof(uint32_t)));
 		x5 = (addr_t) indir_arg;
 	}
 
diff --git a/platform/msm_shared/ucs.c b/platform/msm_shared/ucs.c
index a1ab6f3..aaf17a8 100644
--- a/platform/msm_shared/ucs.c
+++ b/platform/msm_shared/ucs.c
@@ -429,7 +429,7 @@
 
 	/* Flush cdb to memory. */
 	dsb();
-	arch_invalidate_cache_range((addr_t) cdb_param, SCSI_CDB_PARAM_LEN);
+	arch_clean_invalidate_cache_range((addr_t) cdb_param, SCSI_CDB_PARAM_LEN);
 
 	memset(&req_upiu, 0 , sizeof(struct scsi_req_build_type));
 
@@ -479,7 +479,7 @@
 
 	/* Flush cdb to memory. */
 	dsb();
-	arch_invalidate_cache_range((addr_t) cdb_param, SCSI_CDB_PARAM_LEN);
+	arch_clean_invalidate_cache_range((addr_t) cdb_param, SCSI_CDB_PARAM_LEN);
 
 	memset(&req_upiu, 0 , sizeof(struct scsi_req_build_type));