ANDROID: trusty: Rename transfer memory to lend

Renaming trusty_transfer_memory to trusty_lend_memory allows Trusty
to export memory operation like share, lend, reclaim and use common
code for memory share and lend operations.

Define TRUSTY_DEFAULT_MEM_OBJ_TAG as 0 and use that in existing calls.

Bug: 283822781
Test: Build & boot Trusty
Signed-off-by: Arunachalam Ganapathy <[email protected]>
Change-Id: Ie165a609cc4398bb916967595d0b748d54d75faf
diff --git a/drivers/trusty/trusty-ipc.c b/drivers/trusty/trusty-ipc.c
index 581c7c9..9a8334b 100644
--- a/drivers/trusty/trusty-ipc.c
+++ b/drivers/trusty/trusty-ipc.c
@@ -1546,10 +1546,16 @@
 		goto cleanup_handle;
 	}
 
-	ret = trusty_transfer_memory(tipc_shared_handle_dev(shared_handle),
-				     &mem_id, shared_handle->sgt->sgl,
-				     shared_handle->sgt->orig_nents, prot, tag,
-				     lend);
+	if (lend)
+		ret = trusty_lend_memory(tipc_shared_handle_dev(shared_handle),
+					 &mem_id, shared_handle->sgt->sgl,
+					 shared_handle->sgt->orig_nents, prot,
+					 tag);
+	else
+		ret = trusty_share_memory(tipc_shared_handle_dev(shared_handle),
+					  &mem_id, shared_handle->sgt->sgl,
+					  shared_handle->sgt->orig_nents, prot,
+					  tag);
 
 	if (ret < 0) {
 		dev_dbg(dev, "Transferring memory failed: %d\n", ret);
diff --git a/drivers/trusty/trusty-sched-share.c b/drivers/trusty/trusty-sched-share.c
index 07e6231..36fd052 100644
--- a/drivers/trusty/trusty-sched-share.c
+++ b/drivers/trusty/trusty-sched-share.c
@@ -158,8 +158,9 @@
 	}
 
 	/* share memory with Trusty */
-	result = trusty_share_memory(sched_share_state->dev, &mem_id, sched_share_state->sg,
-				     sched_share_state->num_pages, PAGE_KERNEL);
+	result = trusty_share_memory(sched_share_state->dev, &mem_id,
+				     sched_share_state->sg, sched_share_state->num_pages,
+				     PAGE_KERNEL, TRUSTY_DEFAULT_MEM_OBJ_TAG);
 	if (result != 0) {
 		dev_err(sched_share_state->dev, "trusty_share_memory failed: %d\n",
 			result);
diff --git a/drivers/trusty/trusty-test.c b/drivers/trusty/trusty-test.c
index 8448689..c25fc0f 100644
--- a/drivers/trusty/trusty-test.c
+++ b/drivers/trusty/trusty-test.c
@@ -138,7 +138,8 @@
 		t1 = ktime_get();
 		tmpret = trusty_share_memory(s->trusty_dev, &obj->mem_id,
 					     obj->sgt.sgl, obj->sgt.nents,
-					     PAGE_KERNEL);
+					     PAGE_KERNEL,
+					     TRUSTY_DEFAULT_MEM_OBJ_TAG);
 		t2 = ktime_get();
 		if (tmpret) {
 			ret = tmpret;
diff --git a/drivers/trusty/trusty-virtio.c b/drivers/trusty/trusty-virtio.c
index 2df10d1..ad3de7f 100644
--- a/drivers/trusty/trusty-virtio.c
+++ b/drivers/trusty/trusty-virtio.c
@@ -628,7 +628,8 @@
 
 	sg_init_one(&tctx->shared_sg, descr_va, descr_buf_sz);
 	ret = trusty_share_memory(tctx->dev->parent, &descr_id,
-				  &tctx->shared_sg, 1, PAGE_KERNEL);
+				  &tctx->shared_sg, 1, PAGE_KERNEL,
+				  TRUSTY_DEFAULT_MEM_OBJ_TAG);
 	if (ret) {
 		dev_err(tctx->dev, "trusty_share_memory failed: %d\n", ret);
 		goto err_share_memory;
diff --git a/drivers/trusty/trusty.c b/drivers/trusty/trusty.c
index 5017e0d..5e7d547 100644
--- a/drivers/trusty/trusty.c
+++ b/drivers/trusty/trusty.c
@@ -260,18 +260,9 @@
 }
 EXPORT_SYMBOL(trusty_std_call32);
 
-int trusty_share_memory(struct device *dev, u64 *id,
-			struct scatterlist *sglist, unsigned int nents,
-			pgprot_t pgprot)
-{
-	return trusty_transfer_memory(dev, id, sglist, nents, pgprot, 0,
-				      false);
-}
-EXPORT_SYMBOL(trusty_share_memory);
-
-int trusty_transfer_memory(struct device *dev, u64 *id,
-			   struct scatterlist *sglist, unsigned int nents,
-			   pgprot_t pgprot, u64 tag, bool lend)
+static int __trusty_share_memory(struct device *dev, u64 *id,
+				 struct scatterlist *sglist, unsigned int nents,
+				 pgprot_t pgprot, u64 tag, bool lend)
 {
 	struct trusty_state *s = platform_get_drvdata(to_platform_device(dev));
 	int ret;
@@ -304,6 +295,12 @@
 		return -EOPNOTSUPP;
 	}
 
+	if (lend && s->api_version < TRUSTY_API_VERSION_MEM_OBJ) {
+		dev_err(s->dev, "%s: old trusty version does not support lending memory objects\n",
+			__func__);
+		return -EOPNOTSUPP;
+	}
+
 	count = dma_map_sg(dev, sglist, nents, DMA_BIDIRECTIONAL);
 	if (count != nents) {
 		dev_err(s->dev, "failed to dma map sg_table\n");
@@ -441,7 +438,22 @@
 	trace_trusty_share_memory_done(len, nents, lend, ffa_handle, ret);
 	return ret;
 }
-EXPORT_SYMBOL(trusty_transfer_memory);
+
+int trusty_share_memory(struct device *dev, u64 *id,
+			struct scatterlist *sglist, unsigned int nents,
+			pgprot_t pgprot, u64 tag)
+{
+	return __trusty_share_memory(dev, id, sglist, nents, pgprot, tag, false);
+}
+EXPORT_SYMBOL(trusty_share_memory);
+
+int trusty_lend_memory(struct device *dev, u64 *id,
+		       struct scatterlist *sglist, unsigned int nents,
+		       pgprot_t pgprot, u64 tag)
+{
+	return __trusty_share_memory(dev, id, sglist, nents, pgprot, tag, true);
+}
+EXPORT_SYMBOL(trusty_lend_memory);
 
 /*
  * trusty_share_memory_compat - trusty_share_memory wrapper for old apis
@@ -457,7 +469,8 @@
 	int ret;
 	struct trusty_state *s = platform_get_drvdata(to_platform_device(dev));
 
-	ret = trusty_share_memory(dev, id, sglist, nents, pgprot);
+	ret = trusty_share_memory(dev, id, sglist, nents, pgprot,
+				  TRUSTY_DEFAULT_MEM_OBJ_TAG);
 	if (!ret && s->api_version < TRUSTY_API_VERSION_PHYS_MEM_OBJ)
 		*id &= 0x0000FFFFFFFFF000ull;
 
diff --git a/include/linux/trusty/trusty.h b/include/linux/trusty/trusty.h
index 751e5c7..d8b4f2d 100644
--- a/include/linux/trusty/trusty.h
+++ b/include/linux/trusty/trusty.h
@@ -11,6 +11,7 @@
 #include <linux/device.h>
 #include <linux/pagemap.h>
 
+#define TRUSTY_DEFAULT_MEM_OBJ_TAG	(0)
 
 /*
  * map Trusty priorities to Linux nice values (see trusty-sched-share.h)
@@ -72,13 +73,13 @@
 typedef u64 trusty_shared_mem_id_t;
 int trusty_share_memory(struct device *dev, trusty_shared_mem_id_t *id,
 			struct scatterlist *sglist, unsigned int nents,
-			pgprot_t pgprot);
+			pgprot_t pgprot, u64 tag);
 int trusty_share_memory_compat(struct device *dev, trusty_shared_mem_id_t *id,
 			       struct scatterlist *sglist, unsigned int nents,
 			       pgprot_t pgprot);
-int trusty_transfer_memory(struct device *dev, u64 *id,
-			   struct scatterlist *sglist, unsigned int nents,
-			   pgprot_t pgprot, u64 tag, bool lend);
+int trusty_lend_memory(struct device *dev, u64 *id,
+		       struct scatterlist *sglist, unsigned int nents,
+		       pgprot_t pgprot, u64 tag);
 int trusty_reclaim_memory(struct device *dev, trusty_shared_mem_id_t id,
 			  struct scatterlist *sglist, unsigned int nents);
 
@@ -88,7 +89,7 @@
 #else
 static inline u64 trusty_dma_buf_get_ffa_tag(struct dma_buf *dma_buf)
 {
-	return 0;
+	return TRUSTY_DEFAULT_MEM_OBJ_TAG;
 }
 #endif