2 * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corporation
4 * Provide default implementations of the DMA mapping callbacks for
5 * directly mapped busses and busses using the iommu infrastructure
8 #include <linux/device.h>
9 #include <linux/dma-mapping.h>
11 #include <asm/iommu.h>
12 #include <asm/abs_addr.h>
15 * Generic iommu implementation
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.
22 static void *dma_iommu_alloc_coherent(struct device *dev, size_t size,
23 dma_addr_t *dma_handle, gfp_t flag)
25 return iommu_alloc_coherent(dev, dev->archdata.dma_data, size,
26 dma_handle, device_to_mask(dev), flag,
27 dev->archdata.numa_node);
30 static void dma_iommu_free_coherent(struct device *dev, size_t size,
31 void *vaddr, dma_addr_t dma_handle)
33 iommu_free_coherent(dev->archdata.dma_data, size, vaddr, dma_handle);
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.
42 static dma_addr_t dma_iommu_map_single(struct device *dev, void *vaddr,
44 enum dma_data_direction direction,
45 struct dma_attrs *attrs)
47 return iommu_map_single(dev, dev->archdata.dma_data, vaddr, size,
48 device_to_mask(dev), direction, attrs);
52 static void dma_iommu_unmap_single(struct device *dev, dma_addr_t dma_handle,
54 enum dma_data_direction direction,
55 struct dma_attrs *attrs)
57 iommu_unmap_single(dev->archdata.dma_data, dma_handle, size, direction,
62 static int dma_iommu_map_sg(struct device *dev, struct scatterlist *sglist,
63 int nelems, enum dma_data_direction direction,
64 struct dma_attrs *attrs)
66 return iommu_map_sg(dev, dev->archdata.dma_data, sglist, nelems,
67 device_to_mask(dev), direction, attrs);
70 static void dma_iommu_unmap_sg(struct device *dev, struct scatterlist *sglist,
71 int nelems, enum dma_data_direction direction,
72 struct dma_attrs *attrs)
74 iommu_unmap_sg(dev->archdata.dma_data, sglist, nelems, direction,
78 /* We support DMA to/from any memory page via the iommu */
79 static int dma_iommu_dma_supported(struct device *dev, u64 mask)
81 struct iommu_table *tbl = dev->archdata.dma_data;
83 if (!tbl || tbl->it_offset > mask) {
85 "Warning: IOMMU offset too big for device mask\n");
88 "mask: 0x%08lx, table offset: 0x%08lx\n",
89 mask, tbl->it_offset);
91 printk(KERN_INFO "mask: 0x%08lx, table unavailable\n",
98 struct 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,
107 EXPORT_SYMBOL(dma_iommu_ops);
110 * Generic direct DMA implementation
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.
118 static unsigned long get_dma_direct_offset(struct device *dev)
120 return (unsigned long)dev->archdata.dma_data;
123 static void *dma_direct_alloc_coherent(struct device *dev, size_t size,
124 dma_addr_t *dma_handle, gfp_t flag)
128 int node = dev->archdata.numa_node;
130 page = alloc_pages_node(node, flag, get_order(size));
133 ret = page_address(page);
134 memset(ret, 0, size);
135 *dma_handle = virt_to_abs(ret) + get_dma_direct_offset(dev);
140 static void dma_direct_free_coherent(struct device *dev, size_t size,
141 void *vaddr, dma_addr_t dma_handle)
143 free_pages((unsigned long)vaddr, get_order(size));
146 static dma_addr_t dma_direct_map_single(struct device *dev, void *ptr,
148 enum dma_data_direction direction,
149 struct dma_attrs *attrs)
151 return virt_to_abs(ptr) + get_dma_direct_offset(dev);
154 static void dma_direct_unmap_single(struct device *dev, dma_addr_t dma_addr,
156 enum dma_data_direction direction,
157 struct dma_attrs *attrs)
161 static int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl,
162 int nents, enum dma_data_direction direction,
163 struct dma_attrs *attrs)
165 struct scatterlist *sg;
168 for_each_sg(sgl, sg, nents, i) {
169 sg->dma_address = sg_phys(sg) + get_dma_direct_offset(dev);
170 sg->dma_length = sg->length;
176 static void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sg,
177 int nents, enum dma_data_direction direction,
178 struct dma_attrs *attrs)
182 static int dma_direct_dma_supported(struct device *dev, u64 mask)
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
188 return mask >= DMA_32BIT_MASK;
191 struct 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,
200 EXPORT_SYMBOL(dma_direct_ops);