dma_alloc.c 829 B

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