44e3486419e17ff1998dc47a8ecdbf4048cb94a7
[firefly-linux-kernel-4.4.55.git] / arch / powerpc / kernel / dma.c
1 /*
2  * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corporation
3  *
4  * Provide default implementations of the DMA mapping callbacks for
5  * directly mapped busses.
6  */
7
8 #include <linux/device.h>
9 #include <linux/dma-mapping.h>
10 #include <asm/bug.h>
11 #include <asm/abs_addr.h>
12
13 /*
14  * Generic direct DMA implementation
15  *
16  * This implementation supports a per-device offset that can be applied if
17  * the address at which memory is visible to devices is not 0. Platform code
18  * can set archdata.dma_data to an unsigned long holding the offset. By
19  * default the offset is zero.
20  */
21
22 static unsigned long get_dma_direct_offset(struct device *dev)
23 {
24         return (unsigned long)dev->archdata.dma_data;
25 }
26
27 static void *dma_direct_alloc_coherent(struct device *dev, size_t size,
28                                        dma_addr_t *dma_handle, gfp_t flag)
29 {
30         struct page *page;
31         void *ret;
32         int node = dev->archdata.numa_node;
33
34         page = alloc_pages_node(node, flag, get_order(size));
35         if (page == NULL)
36                 return NULL;
37         ret = page_address(page);
38         memset(ret, 0, size);
39         *dma_handle = virt_to_abs(ret) + get_dma_direct_offset(dev);
40
41         return ret;
42 }
43
44 static void dma_direct_free_coherent(struct device *dev, size_t size,
45                                      void *vaddr, dma_addr_t dma_handle)
46 {
47         free_pages((unsigned long)vaddr, get_order(size));
48 }
49
50 static dma_addr_t dma_direct_map_single(struct device *dev, void *ptr,
51                                         size_t size,
52                                         enum dma_data_direction direction,
53                                         struct dma_attrs *attrs)
54 {
55         return virt_to_abs(ptr) + get_dma_direct_offset(dev);
56 }
57
58 static void dma_direct_unmap_single(struct device *dev, dma_addr_t dma_addr,
59                                     size_t size,
60                                     enum dma_data_direction direction,
61                                     struct dma_attrs *attrs)
62 {
63 }
64
65 static int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl,
66                              int nents, enum dma_data_direction direction,
67                              struct dma_attrs *attrs)
68 {
69         struct scatterlist *sg;
70         int i;
71
72         for_each_sg(sgl, sg, nents, i) {
73                 sg->dma_address = sg_phys(sg) + get_dma_direct_offset(dev);
74                 sg->dma_length = sg->length;
75         }
76
77         return nents;
78 }
79
80 static void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sg,
81                                 int nents, enum dma_data_direction direction,
82                                 struct dma_attrs *attrs)
83 {
84 }
85
86 static int dma_direct_dma_supported(struct device *dev, u64 mask)
87 {
88         /* Could be improved to check for memory though it better be
89          * done via some global so platforms can set the limit in case
90          * they have limited DMA windows
91          */
92         return mask >= DMA_32BIT_MASK;
93 }
94
95 struct dma_mapping_ops dma_direct_ops = {
96         .alloc_coherent = dma_direct_alloc_coherent,
97         .free_coherent  = dma_direct_free_coherent,
98         .map_single     = dma_direct_map_single,
99         .unmap_single   = dma_direct_unmap_single,
100         .map_sg         = dma_direct_map_sg,
101         .unmap_sg       = dma_direct_unmap_sg,
102         .dma_supported  = dma_direct_dma_supported,
103 };
104 EXPORT_SYMBOL(dma_direct_ops);