powerpc: move device_to_mask() to dma-mapping.h
[deliverable/linux.git] / arch / powerpc / kernel / dma_64.c
CommitLineData
1da177e4 1/*
12d04eef 2 * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corporation
1da177e4 3 *
12d04eef
BH
4 * Provide default implementations of the DMA mapping callbacks for
5 * directly mapped busses and busses using the iommu infrastructure
1da177e4
LT
6 */
7
8#include <linux/device.h>
9#include <linux/dma-mapping.h>
1da177e4 10#include <asm/bug.h>
12d04eef
BH
11#include <asm/iommu.h>
12#include <asm/abs_addr.h>
1da177e4 13
12d04eef
BH
14/*
15 * Generic iommu implementation
16 */
1da177e4 17
12d04eef
BH
18/* Allocates a contiguous real buffer and creates mappings over it.
19 * Returns the virtual address of the buffer and sets dma_handle
20 * to the dma address (mapping) of the first page.
21 */
22static void *dma_iommu_alloc_coherent(struct device *dev, size_t size,
23 dma_addr_t *dma_handle, gfp_t flag)
24{
fb3475e9
FT
25 return iommu_alloc_coherent(dev, dev->archdata.dma_data, size,
26 dma_handle, device_to_mask(dev), flag,
12d04eef 27 dev->archdata.numa_node);
1da177e4 28}
1da177e4 29
12d04eef
BH
30static void dma_iommu_free_coherent(struct device *dev, size_t size,
31 void *vaddr, dma_addr_t dma_handle)
1da177e4 32{
12d04eef 33 iommu_free_coherent(dev->archdata.dma_data, size, vaddr, dma_handle);
1da177e4 34}
1da177e4 35
12d04eef
BH
36/* Creates TCEs for a user provided buffer. The user buffer must be
37 * contiguous real kernel storage (not vmalloc). The address of the buffer
38 * passed here is the kernel (virtual) address of the buffer. The buffer
39 * need not be page aligned, the dma_addr_t returned will point to the same
40 * byte within the page as vaddr.
41 */
42static dma_addr_t dma_iommu_map_single(struct device *dev, void *vaddr,
43 size_t size,
3affedc4
MN
44 enum dma_data_direction direction,
45 struct dma_attrs *attrs)
1da177e4 46{
fb3475e9 47 return iommu_map_single(dev, dev->archdata.dma_data, vaddr, size,
3affedc4 48 device_to_mask(dev), direction, attrs);
1da177e4 49}
1da177e4 50
12d04eef
BH
51
52static void dma_iommu_unmap_single(struct device *dev, dma_addr_t dma_handle,
53 size_t size,
3affedc4
MN
54 enum dma_data_direction direction,
55 struct dma_attrs *attrs)
1da177e4 56{
3affedc4
MN
57 iommu_unmap_single(dev->archdata.dma_data, dma_handle, size, direction,
58 attrs);
12d04eef 59}
1da177e4 60
5d33eebe 61
12d04eef 62static int dma_iommu_map_sg(struct device *dev, struct scatterlist *sglist,
3affedc4
MN
63 int nelems, enum dma_data_direction direction,
64 struct dma_attrs *attrs)
12d04eef 65{
c8692362 66 return iommu_map_sg(dev, dev->archdata.dma_data, sglist, nelems,
3affedc4 67 device_to_mask(dev), direction, attrs);
1da177e4 68}
1da177e4 69
12d04eef 70static void dma_iommu_unmap_sg(struct device *dev, struct scatterlist *sglist,
3affedc4
MN
71 int nelems, enum dma_data_direction direction,
72 struct dma_attrs *attrs)
1da177e4 73{
3affedc4
MN
74 iommu_unmap_sg(dev->archdata.dma_data, sglist, nelems, direction,
75 attrs);
1da177e4 76}
1da177e4 77
12d04eef
BH
78/* We support DMA to/from any memory page via the iommu */
79static int dma_iommu_dma_supported(struct device *dev, u64 mask)
1da177e4 80{
12d04eef
BH
81 struct iommu_table *tbl = dev->archdata.dma_data;
82
83 if (!tbl || tbl->it_offset > mask) {
84 printk(KERN_INFO
85 "Warning: IOMMU offset too big for device mask\n");
86 if (tbl)
87 printk(KERN_INFO
88 "mask: 0x%08lx, table offset: 0x%08lx\n",
89 mask, tbl->it_offset);
90 else
91 printk(KERN_INFO "mask: 0x%08lx, table unavailable\n",
92 mask);
93 return 0;
94 } else
95 return 1;
1da177e4 96}
1da177e4 97
12d04eef
BH
98struct dma_mapping_ops dma_iommu_ops = {
99 .alloc_coherent = dma_iommu_alloc_coherent,
100 .free_coherent = dma_iommu_free_coherent,
101 .map_single = dma_iommu_map_single,
102 .unmap_single = dma_iommu_unmap_single,
103 .map_sg = dma_iommu_map_sg,
104 .unmap_sg = dma_iommu_unmap_sg,
105 .dma_supported = dma_iommu_dma_supported,
106};
107EXPORT_SYMBOL(dma_iommu_ops);
1da177e4 108
12d04eef
BH
109/*
110 * Generic direct DMA implementation
92b20c40 111 *
31d1b493
ME
112 * This implementation supports a per-device offset that can be applied if
113 * the address at which memory is visible to devices is not 0. Platform code
114 * can set archdata.dma_data to an unsigned long holding the offset. By
115 * default the offset is zero.
12d04eef 116 */
5d33eebe 117
35e4a6e2
ME
118static unsigned long get_dma_direct_offset(struct device *dev)
119{
120 return (unsigned long)dev->archdata.dma_data;
121}
122
12d04eef
BH
123static void *dma_direct_alloc_coherent(struct device *dev, size_t size,
124 dma_addr_t *dma_handle, gfp_t flag)
125{
c80d9133 126 struct page *page;
12d04eef 127 void *ret;
c80d9133 128 int node = dev->archdata.numa_node;
12d04eef 129
c80d9133
BH
130 page = alloc_pages_node(node, flag, get_order(size));
131 if (page == NULL)
132 return NULL;
133 ret = page_address(page);
134 memset(ret, 0, size);
35e4a6e2 135 *dma_handle = virt_to_abs(ret) + get_dma_direct_offset(dev);
c80d9133 136
12d04eef 137 return ret;
1da177e4 138}
1da177e4 139
12d04eef
BH
140static void dma_direct_free_coherent(struct device *dev, size_t size,
141 void *vaddr, dma_addr_t dma_handle)
1da177e4 142{
12d04eef
BH
143 free_pages((unsigned long)vaddr, get_order(size));
144}
1da177e4 145
12d04eef
BH
146static dma_addr_t dma_direct_map_single(struct device *dev, void *ptr,
147 size_t size,
3affedc4
MN
148 enum dma_data_direction direction,
149 struct dma_attrs *attrs)
12d04eef 150{
35e4a6e2 151 return virt_to_abs(ptr) + get_dma_direct_offset(dev);
12d04eef 152}
5d33eebe 153
12d04eef
BH
154static void dma_direct_unmap_single(struct device *dev, dma_addr_t dma_addr,
155 size_t size,
3affedc4
MN
156 enum dma_data_direction direction,
157 struct dma_attrs *attrs)
12d04eef 158{
1da177e4 159}
1da177e4 160
78bdc310 161static int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl,
3affedc4
MN
162 int nents, enum dma_data_direction direction,
163 struct dma_attrs *attrs)
1da177e4 164{
78bdc310 165 struct scatterlist *sg;
12d04eef 166 int i;
1da177e4 167
78bdc310 168 for_each_sg(sgl, sg, nents, i) {
35e4a6e2 169 sg->dma_address = sg_phys(sg) + get_dma_direct_offset(dev);
12d04eef
BH
170 sg->dma_length = sg->length;
171 }
5d33eebe 172
12d04eef 173 return nents;
1da177e4 174}
1da177e4 175
12d04eef 176static void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sg,
3affedc4
MN
177 int nents, enum dma_data_direction direction,
178 struct dma_attrs *attrs)
1da177e4 179{
12d04eef 180}
5d33eebe 181
12d04eef
BH
182static int dma_direct_dma_supported(struct device *dev, u64 mask)
183{
184 /* Could be improved to check for memory though it better be
185 * done via some global so platforms can set the limit in case
186 * they have limited DMA windows
187 */
188 return mask >= DMA_32BIT_MASK;
1da177e4 189}
12d04eef
BH
190
191struct dma_mapping_ops dma_direct_ops = {
192 .alloc_coherent = dma_direct_alloc_coherent,
193 .free_coherent = dma_direct_free_coherent,
194 .map_single = dma_direct_map_single,
195 .unmap_single = dma_direct_unmap_single,
196 .map_sg = dma_direct_map_sg,
197 .unmap_sg = dma_direct_unmap_sg,
198 .dma_supported = dma_direct_dma_supported,
199};
200EXPORT_SYMBOL(dma_direct_ops);
This page took 0.317581 seconds and 5 git commands to generate.