1 #include <stdio.h>
2 
3 #include <hal_mem.h>
4 #include "sunxi_hal_common.h"
5 
dma_free_coherent(void * addr)6 void dma_free_coherent(void *addr)
7 {
8     void *malloc_ptr = NULL;
9     if (!addr)
10     {
11         return;
12     }
13     malloc_ptr = (void *)*(unsigned long *)((unsigned long *)addr - 1);
14     hal_free(malloc_ptr);
15 }
16 
dma_alloc_coherent(size_t size)17 void *dma_alloc_coherent(size_t size)
18 {
19     void *fake_ptr = NULL;
20     void *malloc_ptr = NULL;
21 
22     malloc_ptr = hal_malloc(size + 2 * CACHELINE_LEN);
23     if ((unsigned long)malloc_ptr & (sizeof(long) - 1))
24     {
25         printf("error: mm_alloc not align. \r\n");
26         return NULL;
27     }
28 
29     if (!malloc_ptr) {
30         return NULL;
31     }
32 
33     fake_ptr = (void *)((unsigned long)(malloc_ptr + CACHELINE_LEN) & (~(CACHELINE_LEN - 1)));
34     *(unsigned long *)((unsigned long *)fake_ptr - 1) = (unsigned long)malloc_ptr;
35 
36     return fake_ptr;
37 }
38 
39