blob: ae5708e3a3127fc9a180ae1d881d5bea5ede48bc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +11002 * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +11004 * Provide default implementations of the DMA mapping callbacks for
5 * directly mapped busses and busses using the iommu infrastructure
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
8#include <linux/device.h>
9#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <asm/bug.h>
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +110011#include <asm/iommu.h>
12#include <asm/abs_addr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +110014/*
15 * Generic iommu implementation
16 */
17
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +110018/* 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024{
FUJITA Tomonorifb3475e2008-02-04 22:28:08 -080025 return iommu_alloc_coherent(dev, dev->archdata.dma_data, size,
26 dma_handle, device_to_mask(dev), flag,
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +110027 dev->archdata.numa_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028}
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +110030static void dma_iommu_free_coherent(struct device *dev, size_t size,
31 void *vaddr, dma_addr_t dma_handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +110033 iommu_free_coherent(dev->archdata.dma_data, size, vaddr, dma_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034}
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +110036/* 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,
Mark Nelson3affedc2008-07-05 05:05:42 +100044 enum dma_data_direction direction,
45 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
FUJITA Tomonorifb3475e2008-02-04 22:28:08 -080047 return iommu_map_single(dev, dev->archdata.dma_data, vaddr, size,
Mark Nelson3affedc2008-07-05 05:05:42 +100048 device_to_mask(dev), direction, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +110051
52static void dma_iommu_unmap_single(struct device *dev, dma_addr_t dma_handle,
53 size_t size,
Mark Nelson3affedc2008-07-05 05:05:42 +100054 enum dma_data_direction direction,
55 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Mark Nelson3affedc2008-07-05 05:05:42 +100057 iommu_unmap_single(dev->archdata.dma_data, dma_handle, size, direction,
58 attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +110061
62static int dma_iommu_map_sg(struct device *dev, struct scatterlist *sglist,
Mark Nelson3affedc2008-07-05 05:05:42 +100063 int nelems, enum dma_data_direction direction,
64 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Mark Nelsonc8692362008-07-05 05:05:41 +100066 return iommu_map_sg(dev, dev->archdata.dma_data, sglist, nelems,
Mark Nelson3affedc2008-07-05 05:05:42 +100067 device_to_mask(dev), direction, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +110070static void dma_iommu_unmap_sg(struct device *dev, struct scatterlist *sglist,
Mark Nelson3affedc2008-07-05 05:05:42 +100071 int nelems, enum dma_data_direction direction,
72 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Mark Nelson3affedc2008-07-05 05:05:42 +100074 iommu_unmap_sg(dev->archdata.dma_data, sglist, nelems, direction,
75 attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076}
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +110078/* We support DMA to/from any memory page via the iommu */
79static int dma_iommu_dma_supported(struct device *dev, u64 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +110081 struct iommu_table *tbl = dev->archdata.dma_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +110083 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +110098struct 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);
108
109/*
110 * Generic direct DMA implementation
Benjamin Herrenschmidt92b20c42006-11-11 17:25:14 +1100111 *
Michael Ellerman31d1b492008-01-21 16:42:48 +1100112 * 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.
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100116 */
117
Michael Ellerman35e4a6e2008-01-21 16:42:43 +1100118static unsigned long get_dma_direct_offset(struct device *dev)
119{
120 return (unsigned long)dev->archdata.dma_data;
121}
122
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100123static void *dma_direct_alloc_coherent(struct device *dev, size_t size,
124 dma_addr_t *dma_handle, gfp_t flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Benjamin Herrenschmidtc80d9132006-11-11 17:25:16 +1100126 struct page *page;
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100127 void *ret;
Benjamin Herrenschmidtc80d9132006-11-11 17:25:16 +1100128 int node = dev->archdata.numa_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Benjamin Herrenschmidtc80d9132006-11-11 17:25:16 +1100130 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);
Michael Ellerman35e4a6e2008-01-21 16:42:43 +1100135 *dma_handle = virt_to_abs(ret) + get_dma_direct_offset(dev);
Benjamin Herrenschmidtc80d9132006-11-11 17:25:16 +1100136
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100137 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100140static void dma_direct_free_coherent(struct device *dev, size_t size,
141 void *vaddr, dma_addr_t dma_handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100143 free_pages((unsigned long)vaddr, get_order(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100146static dma_addr_t dma_direct_map_single(struct device *dev, void *ptr,
147 size_t size,
Mark Nelson3affedc2008-07-05 05:05:42 +1000148 enum dma_data_direction direction,
149 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Michael Ellerman35e4a6e2008-01-21 16:42:43 +1100151 return virt_to_abs(ptr) + get_dma_direct_offset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100153
154static void dma_direct_unmap_single(struct device *dev, dma_addr_t dma_addr,
155 size_t size,
Mark Nelson3affedc2008-07-05 05:05:42 +1000156 enum dma_data_direction direction,
157 struct dma_attrs *attrs)
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100158{
159}
160
Jens Axboe78bdc312007-10-12 13:44:12 +0200161static int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl,
Mark Nelson3affedc2008-07-05 05:05:42 +1000162 int nents, enum dma_data_direction direction,
163 struct dma_attrs *attrs)
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100164{
Jens Axboe78bdc312007-10-12 13:44:12 +0200165 struct scatterlist *sg;
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100166 int i;
167
Jens Axboe78bdc312007-10-12 13:44:12 +0200168 for_each_sg(sgl, sg, nents, i) {
Michael Ellerman35e4a6e2008-01-21 16:42:43 +1100169 sg->dma_address = sg_phys(sg) + get_dma_direct_offset(dev);
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100170 sg->dma_length = sg->length;
171 }
172
173 return nents;
174}
175
176static void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sg,
Mark Nelson3affedc2008-07-05 05:05:42 +1000177 int nents, enum dma_data_direction direction,
178 struct dma_attrs *attrs)
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100179{
180}
181
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;
189}
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);