[POWERPC] Refactor 64 bits DMA operations
[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 18static inline unsigned long device_to_mask(struct device *dev)
1da177e4 19{
12d04eef
BH
20 if (dev->dma_mask && *dev->dma_mask)
21 return *dev->dma_mask;
22 /* Assume devices without mask can take 32 bit addresses */
23 return 0xfffffffful;
24}
1da177e4 25
5d33eebe 26
12d04eef
BH
27/* Allocates a contiguous real buffer and creates mappings over it.
28 * Returns the virtual address of the buffer and sets dma_handle
29 * to the dma address (mapping) of the first page.
30 */
31static void *dma_iommu_alloc_coherent(struct device *dev, size_t size,
32 dma_addr_t *dma_handle, gfp_t flag)
33{
34 return iommu_alloc_coherent(dev->archdata.dma_data, size, dma_handle,
35 device_to_mask(dev), flag,
36 dev->archdata.numa_node);
1da177e4 37}
1da177e4 38
12d04eef
BH
39static void dma_iommu_free_coherent(struct device *dev, size_t size,
40 void *vaddr, dma_addr_t dma_handle)
1da177e4 41{
12d04eef 42 iommu_free_coherent(dev->archdata.dma_data, size, vaddr, dma_handle);
1da177e4 43}
1da177e4 44
12d04eef
BH
45/* Creates TCEs for a user provided buffer. The user buffer must be
46 * contiguous real kernel storage (not vmalloc). The address of the buffer
47 * passed here is the kernel (virtual) address of the buffer. The buffer
48 * need not be page aligned, the dma_addr_t returned will point to the same
49 * byte within the page as vaddr.
50 */
51static dma_addr_t dma_iommu_map_single(struct device *dev, void *vaddr,
52 size_t size,
53 enum dma_data_direction direction)
1da177e4 54{
12d04eef
BH
55 return iommu_map_single(dev->archdata.dma_data, vaddr, size,
56 device_to_mask(dev), direction);
1da177e4 57}
1da177e4 58
12d04eef
BH
59
60static void dma_iommu_unmap_single(struct device *dev, dma_addr_t dma_handle,
61 size_t size,
62 enum dma_data_direction direction)
1da177e4 63{
12d04eef
BH
64 iommu_unmap_single(dev->archdata.dma_data, dma_handle, size, direction);
65}
1da177e4 66
5d33eebe 67
12d04eef
BH
68static int dma_iommu_map_sg(struct device *dev, struct scatterlist *sglist,
69 int nelems, enum dma_data_direction direction)
70{
71 return iommu_map_sg(dev->archdata.dma_data, sglist, nelems,
72 device_to_mask(dev), direction);
1da177e4 73}
1da177e4 74
12d04eef
BH
75static void dma_iommu_unmap_sg(struct device *dev, struct scatterlist *sglist,
76 int nelems, enum dma_data_direction direction)
1da177e4 77{
12d04eef 78 iommu_unmap_sg(dev->archdata.dma_data, sglist, nelems, direction);
1da177e4 79}
1da177e4 80
12d04eef
BH
81/* We support DMA to/from any memory page via the iommu */
82static int dma_iommu_dma_supported(struct device *dev, u64 mask)
1da177e4 83{
12d04eef
BH
84 struct iommu_table *tbl = dev->archdata.dma_data;
85
86 if (!tbl || tbl->it_offset > mask) {
87 printk(KERN_INFO
88 "Warning: IOMMU offset too big for device mask\n");
89 if (tbl)
90 printk(KERN_INFO
91 "mask: 0x%08lx, table offset: 0x%08lx\n",
92 mask, tbl->it_offset);
93 else
94 printk(KERN_INFO "mask: 0x%08lx, table unavailable\n",
95 mask);
96 return 0;
97 } else
98 return 1;
1da177e4 99}
1da177e4 100
12d04eef
BH
101struct dma_mapping_ops dma_iommu_ops = {
102 .alloc_coherent = dma_iommu_alloc_coherent,
103 .free_coherent = dma_iommu_free_coherent,
104 .map_single = dma_iommu_map_single,
105 .unmap_single = dma_iommu_unmap_single,
106 .map_sg = dma_iommu_map_sg,
107 .unmap_sg = dma_iommu_unmap_sg,
108 .dma_supported = dma_iommu_dma_supported,
109};
110EXPORT_SYMBOL(dma_iommu_ops);
1da177e4 111
12d04eef
BH
112/*
113 * Generic direct DMA implementation
114 */
5d33eebe 115
12d04eef
BH
116static void *dma_direct_alloc_coherent(struct device *dev, size_t size,
117 dma_addr_t *dma_handle, gfp_t flag)
118{
119 void *ret;
120
121 /* TODO: Maybe use the numa node here too ? */
122 ret = (void *)__get_free_pages(flag, get_order(size));
123 if (ret != NULL) {
124 memset(ret, 0, size);
125 *dma_handle = virt_to_abs(ret);
126 }
127 return ret;
1da177e4 128}
1da177e4 129
12d04eef
BH
130static void dma_direct_free_coherent(struct device *dev, size_t size,
131 void *vaddr, dma_addr_t dma_handle)
1da177e4 132{
12d04eef
BH
133 free_pages((unsigned long)vaddr, get_order(size));
134}
1da177e4 135
12d04eef
BH
136static dma_addr_t dma_direct_map_single(struct device *dev, void *ptr,
137 size_t size,
138 enum dma_data_direction direction)
139{
140 return virt_to_abs(ptr);
141}
5d33eebe 142
12d04eef
BH
143static void dma_direct_unmap_single(struct device *dev, dma_addr_t dma_addr,
144 size_t size,
145 enum dma_data_direction direction)
146{
1da177e4 147}
1da177e4 148
12d04eef
BH
149static int dma_direct_map_sg(struct device *dev, struct scatterlist *sg,
150 int nents, enum dma_data_direction direction)
1da177e4 151{
12d04eef 152 int i;
1da177e4 153
12d04eef
BH
154 for (i = 0; i < nents; i++, sg++) {
155 sg->dma_address = page_to_phys(sg->page) + sg->offset;
156 sg->dma_length = sg->length;
157 }
5d33eebe 158
12d04eef 159 return nents;
1da177e4 160}
1da177e4 161
12d04eef
BH
162static void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sg,
163 int nents, enum dma_data_direction direction)
1da177e4 164{
12d04eef 165}
5d33eebe 166
12d04eef
BH
167static int dma_direct_dma_supported(struct device *dev, u64 mask)
168{
169 /* Could be improved to check for memory though it better be
170 * done via some global so platforms can set the limit in case
171 * they have limited DMA windows
172 */
173 return mask >= DMA_32BIT_MASK;
1da177e4 174}
12d04eef
BH
175
176struct dma_mapping_ops dma_direct_ops = {
177 .alloc_coherent = dma_direct_alloc_coherent,
178 .free_coherent = dma_direct_free_coherent,
179 .map_single = dma_direct_map_single,
180 .unmap_single = dma_direct_unmap_single,
181 .map_sg = dma_direct_map_sg,
182 .unmap_sg = dma_direct_unmap_sg,
183 .dma_supported = dma_direct_dma_supported,
184};
185EXPORT_SYMBOL(dma_direct_ops);
This page took 0.17163 seconds and 5 git commands to generate.