blob: 65e49dd35b7b9d39b7b42b89d53295230be80b86 [file] [log] [blame]
Christian Borntraegera8463d42016-02-02 21:46:32 -08001/*
2 * lib/dma-noop.c
3 *
4 * Simple DMA noop-ops that map 1:1 with memory
5 */
6#include <linux/export.h>
7#include <linux/mm.h>
8#include <linux/dma-mapping.h>
9#include <linux/scatterlist.h>
10
11static void *dma_noop_alloc(struct device *dev, size_t size,
12 dma_addr_t *dma_handle, gfp_t gfp,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070013 unsigned long attrs)
Christian Borntraegera8463d42016-02-02 21:46:32 -080014{
15 void *ret;
16
17 ret = (void *)__get_free_pages(gfp, get_order(size));
18 if (ret)
19 *dma_handle = virt_to_phys(ret);
20 return ret;
21}
22
23static void dma_noop_free(struct device *dev, size_t size,
24 void *cpu_addr, dma_addr_t dma_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070025 unsigned long attrs)
Christian Borntraegera8463d42016-02-02 21:46:32 -080026{
27 free_pages((unsigned long)cpu_addr, get_order(size));
28}
29
30static dma_addr_t dma_noop_map_page(struct device *dev, struct page *page,
31 unsigned long offset, size_t size,
32 enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070033 unsigned long attrs)
Christian Borntraegera8463d42016-02-02 21:46:32 -080034{
35 return page_to_phys(page) + offset;
36}
37
38static int dma_noop_map_sg(struct device *dev, struct scatterlist *sgl, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070039 enum dma_data_direction dir,
40 unsigned long attrs)
Christian Borntraegera8463d42016-02-02 21:46:32 -080041{
42 int i;
43 struct scatterlist *sg;
44
45 for_each_sg(sgl, sg, nents, i) {
46 void *va;
47
48 BUG_ON(!sg_page(sg));
49 va = sg_virt(sg);
50 sg_dma_address(sg) = (dma_addr_t)virt_to_phys(va);
51 sg_dma_len(sg) = sg->length;
52 }
53
54 return nents;
55}
56
57static int dma_noop_mapping_error(struct device *dev, dma_addr_t dma_addr)
58{
59 return 0;
60}
61
62static int dma_noop_supported(struct device *dev, u64 mask)
63{
64 return 1;
65}
66
Bart Van Assche52997092017-01-20 13:04:01 -080067const struct dma_map_ops dma_noop_ops = {
Christian Borntraegera8463d42016-02-02 21:46:32 -080068 .alloc = dma_noop_alloc,
69 .free = dma_noop_free,
70 .map_page = dma_noop_map_page,
71 .map_sg = dma_noop_map_sg,
72 .mapping_error = dma_noop_mapping_error,
73 .dma_supported = dma_noop_supported,
74};
75
76EXPORT_SYMBOL(dma_noop_ops);