Add "struct secure_heap_ops". For the secure memory, totally there are
two steps:
a) memory_alloc: Allocate the buffer in kernel;
b) secure_the_memory: Secure/Proect that buffer.
The memory_alloc is mandatory while secure_the_memory is optinal since
it may be part of memory_alloc.
Signed-off-by: Yong Wu <yong.wu@mediatek.com>
---
drivers/dma-buf/heaps/secure_heap.c | 39 ++++++++++++++++++++++++++++-
drivers/dma-buf/heaps/secure_heap.h | 11 ++++++++
2 files changed, 49 insertions(+), 1 deletion(-)
diff --git a/drivers/dma-buf/heaps/secure_heap.c b/drivers/dma-buf/heaps/secure_heap.c
index e087da5638aa..925cf8e1c7ce 100644
--- a/drivers/dma-buf/heaps/secure_heap.c
+++ b/drivers/dma-buf/heaps/secure_heap.c
@@ -12,10 +12,42 @@
#include "secure_heap.h"
+static int secure_heap_memory_allocate(struct secure_heap *sec_heap, struct secure_buffer *sec_buf)
+{
+ const struct secure_heap_ops *ops = sec_heap->ops;
+ int ret;
+
+ ret = ops->memory_alloc(sec_heap, sec_buf);
+ if (ret)
+ return ret;
+
+ if (ops->secure_the_memory) {
+ ret = ops->secure_the_memory(sec_heap, sec_buf);
+ if (ret)
+ goto sec_memory_free;
+ }
+ return 0;
+
+sec_memory_free:
+ ops->memory_free(sec_heap, sec_buf);
+ return ret;
+}
+
+static void secure_heap_memory_free(struct secure_heap *sec_heap, struct secure_buffer *sec_buf)
+{
+ const struct secure_heap_ops *ops = sec_heap->ops;
+
+ if (ops->unsecure_the_memory)
+ ops->unsecure_the_memory(sec_heap, sec_buf);
+
+ ops->memory_free(sec_heap, sec_buf);
+}
+
static struct dma_buf *
secure_heap_allocate(struct dma_heap *heap, unsigned long size,
unsigned long fd_flags, unsigned long heap_flags)
{
+ struct secure_heap *sec_heap = dma_heap_get_drvdata(heap);
struct secure_buffer *sec_buf;
DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
struct dma_buf *dmabuf;
@@ -28,6 +60,9 @@ secure_heap_allocate(struct dma_heap *heap, unsigned long size,
sec_buf->size = ALIGN(size, PAGE_SIZE);
sec_buf->heap = heap;
+ ret = secure_heap_memory_allocate(sec_heap, sec_buf);
+ if (ret)
+ goto err_free_buf;
exp_info.exp_name = dma_heap_get_name(heap);
exp_info.size = sec_buf->size;
exp_info.flags = fd_flags;
@@ -36,11 +71,13 @@ secure_heap_allocate(struct dma_heap *heap, unsigned long size,
dmabuf = dma_buf_export(&exp_info);
if (IS_ERR(dmabuf)) {
ret = PTR_ERR(dmabuf);
- goto err_free_buf;
+ goto err_free_sec_mem;
}
return dmabuf;
+err_free_sec_mem:
+ secure_heap_memory_free(sec_heap, sec_buf);
err_free_buf:
kfree(sec_buf);
return ERR_PTR(ret);
diff --git a/drivers/dma-buf/heaps/secure_heap.h b/drivers/dma-buf/heaps/secure_heap.h
index 50733dc6d4db..ec5349cd28d0 100644
--- a/drivers/dma-buf/heaps/secure_heap.h
+++ b/drivers/dma-buf/heaps/secure_heap.h
@@ -15,6 +15,17 @@ struct secure_buffer {
struct secure_heap {
const char *name;
+
+ const struct secure_heap_ops *ops;
+};
+
+struct secure_heap_ops {
+ int (*memory_alloc)(struct secure_heap *sec_heap, struct secure_buffer *sec_buf);
+ void (*memory_free)(struct secure_heap *sec_heap, struct secure_buffer *sec_buf);
+
+ /* Protect/unprotect the memory */
+ int (*secure_the_memory)(struct secure_heap *sec_heap, struct secure_buffer *sec_buf);
+ void (*unsecure_the_memory)(struct secure_heap *sec_heap, struct secure_buffer *sec_buf);
};
int secure_heap_add(struct secure_heap *sec_heap);
--
2.25.1