1 // SPDX-License-Identifier: GPL-2.0
5 * DMA operations that map to virtual addresses without flushing memory.
7 #include <linux/export.h>
9 #include <linux/dma-mapping.h>
10 #include <linux/scatterlist.h>
12 static void *dma_virt_alloc(struct device *dev, size_t size,
13 dma_addr_t *dma_handle, gfp_t gfp,
18 ret = (void *)__get_free_pages(gfp, get_order(size));
20 *dma_handle = (uintptr_t)ret;
24 static void dma_virt_free(struct device *dev, size_t size,
25 void *cpu_addr, dma_addr_t dma_addr,
28 free_pages((unsigned long)cpu_addr, get_order(size));
31 static dma_addr_t dma_virt_map_page(struct device *dev, struct page *page,
32 unsigned long offset, size_t size,
33 enum dma_data_direction dir,
36 return (uintptr_t)(page_address(page) + offset);
39 static int dma_virt_map_sg(struct device *dev, struct scatterlist *sgl,
40 int nents, enum dma_data_direction dir,
44 struct scatterlist *sg;
46 for_each_sg(sgl, sg, nents, i) {
48 sg_dma_address(sg) = (uintptr_t)sg_virt(sg);
49 sg_dma_len(sg) = sg->length;
55 const struct dma_map_ops dma_virt_ops = {
56 .alloc = dma_virt_alloc,
57 .free = dma_virt_free,
58 .map_page = dma_virt_map_page,
59 .map_sg = dma_virt_map_sg,
61 EXPORT_SYMBOL(dma_virt_ops);