1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2004 - 2007 Paul Mundt
6 #include <linux/init.h>
7 #include <linux/dma-noncoherent.h>
8 #include <linux/module.h>
9 #include <asm/cacheflush.h>
10 #include <asm/addrspace.h>
12 void *arch_dma_alloc(struct device
*dev
, size_t size
, dma_addr_t
*dma_handle
,
13 gfp_t gfp
, unsigned long attrs
)
15 void *ret
, *ret_nocache
;
16 int order
= get_order(size
);
20 ret
= (void *)__get_free_pages(gfp
, order
);
25 * Pages from the page allocator may have data present in
26 * cache. So flush the cache before using uncached memory.
28 arch_sync_dma_for_device(virt_to_phys(ret
), size
,
31 ret_nocache
= (void __force
*)ioremap(virt_to_phys(ret
), size
);
33 free_pages((unsigned long)ret
, order
);
37 split_page(pfn_to_page(virt_to_phys(ret
) >> PAGE_SHIFT
), order
);
39 *dma_handle
= virt_to_phys(ret
);
41 *dma_handle
-= PFN_PHYS(dev
->dma_pfn_offset
);
46 void arch_dma_free(struct device
*dev
, size_t size
, void *vaddr
,
47 dma_addr_t dma_handle
, unsigned long attrs
)
49 int order
= get_order(size
);
50 unsigned long pfn
= (dma_handle
>> PAGE_SHIFT
);
54 pfn
+= dev
->dma_pfn_offset
;
56 for (k
= 0; k
< (1 << order
); k
++)
57 __free_pages(pfn_to_page(pfn
+ k
), 0);
62 void arch_sync_dma_for_device(phys_addr_t paddr
, size_t size
,
63 enum dma_data_direction dir
)
65 void *addr
= sh_cacheop_vaddr(phys_to_virt(paddr
));
68 case DMA_FROM_DEVICE
: /* invalidate only */
69 __flush_invalidate_region(addr
, size
);
71 case DMA_TO_DEVICE
: /* writeback only */
72 __flush_wback_region(addr
, size
);
74 case DMA_BIDIRECTIONAL
: /* writeback and invalidate */
75 __flush_purge_region(addr
, size
);