blob: 3ae0c35d21f95b3fd8fee14ee208d1230cf9c887 [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
18static inline unsigned long device_to_mask(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070019{
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +110020 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070024}
25
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +110026
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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
FUJITA Tomonorifb3475e2008-02-04 22:28:08 -080034 return iommu_alloc_coherent(dev, dev->archdata.dma_data, size,
35 dma_handle, device_to_mask(dev), flag,
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +110036 dev->archdata.numa_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037}
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +110039static void dma_iommu_free_coherent(struct device *dev, size_t size,
40 void *vaddr, dma_addr_t dma_handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +110042 iommu_free_coherent(dev->archdata.dma_data, size, vaddr, dma_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043}
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +110045/* 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,
Mark Nelson3affedc2008-07-05 05:05:42 +100053 enum dma_data_direction direction,
54 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
FUJITA Tomonorifb3475e2008-02-04 22:28:08 -080056 return iommu_map_single(dev, dev->archdata.dma_data, vaddr, size,
Mark Nelson3affedc2008-07-05 05:05:42 +100057 device_to_mask(dev), direction, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058}
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +110060
61static void dma_iommu_unmap_single(struct device *dev, dma_addr_t dma_handle,
62 size_t size,
Mark Nelson3affedc2008-07-05 05:05:42 +100063 enum dma_data_direction direction,
64 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Mark Nelson3affedc2008-07-05 05:05:42 +100066 iommu_unmap_single(dev->archdata.dma_data, dma_handle, size, direction,
67 attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +110070
71static int dma_iommu_map_sg(struct device *dev, struct scatterlist *sglist,
Mark Nelson3affedc2008-07-05 05:05:42 +100072 int nelems, enum dma_data_direction direction,
73 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Mark Nelsonc8692362008-07-05 05:05:41 +100075 return iommu_map_sg(dev, dev->archdata.dma_data, sglist, nelems,
Mark Nelson3affedc2008-07-05 05:05:42 +100076 device_to_mask(dev), direction, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +110079static void dma_iommu_unmap_sg(struct device *dev, struct scatterlist *sglist,
Mark Nelson3affedc2008-07-05 05:05:42 +100080 int nelems, enum dma_data_direction direction,
81 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Mark Nelson3affedc2008-07-05 05:05:42 +100083 iommu_unmap_sg(dev->archdata.dma_data, sglist, nelems, direction,
84 attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +110087/* We support DMA to/from any memory page via the iommu */
88static int dma_iommu_dma_supported(struct device *dev, u64 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +110090 struct iommu_table *tbl = dev->archdata.dma_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +110092 if (!tbl || tbl->it_offset > mask) {
93 printk(KERN_INFO
94 "Warning: IOMMU offset too big for device mask\n");
95 if (tbl)
96 printk(KERN_INFO
97 "mask: 0x%08lx, table offset: 0x%08lx\n",
98 mask, tbl->it_offset);
99 else
100 printk(KERN_INFO "mask: 0x%08lx, table unavailable\n",
101 mask);
102 return 0;
103 } else
104 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100107struct dma_mapping_ops dma_iommu_ops = {
108 .alloc_coherent = dma_iommu_alloc_coherent,
109 .free_coherent = dma_iommu_free_coherent,
110 .map_single = dma_iommu_map_single,
111 .unmap_single = dma_iommu_unmap_single,
112 .map_sg = dma_iommu_map_sg,
113 .unmap_sg = dma_iommu_unmap_sg,
114 .dma_supported = dma_iommu_dma_supported,
115};
116EXPORT_SYMBOL(dma_iommu_ops);
117
118/*
119 * Generic direct DMA implementation
Benjamin Herrenschmidt92b20c42006-11-11 17:25:14 +1100120 *
Michael Ellerman31d1b492008-01-21 16:42:48 +1100121 * This implementation supports a per-device offset that can be applied if
122 * the address at which memory is visible to devices is not 0. Platform code
123 * can set archdata.dma_data to an unsigned long holding the offset. By
124 * default the offset is zero.
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100125 */
126
Michael Ellerman35e4a6e2008-01-21 16:42:43 +1100127static unsigned long get_dma_direct_offset(struct device *dev)
128{
129 return (unsigned long)dev->archdata.dma_data;
130}
131
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100132static void *dma_direct_alloc_coherent(struct device *dev, size_t size,
133 dma_addr_t *dma_handle, gfp_t flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Benjamin Herrenschmidtc80d9132006-11-11 17:25:16 +1100135 struct page *page;
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100136 void *ret;
Benjamin Herrenschmidtc80d9132006-11-11 17:25:16 +1100137 int node = dev->archdata.numa_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Benjamin Herrenschmidtc80d9132006-11-11 17:25:16 +1100139 page = alloc_pages_node(node, flag, get_order(size));
140 if (page == NULL)
141 return NULL;
142 ret = page_address(page);
143 memset(ret, 0, size);
Michael Ellerman35e4a6e2008-01-21 16:42:43 +1100144 *dma_handle = virt_to_abs(ret) + get_dma_direct_offset(dev);
Benjamin Herrenschmidtc80d9132006-11-11 17:25:16 +1100145
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100146 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100149static void dma_direct_free_coherent(struct device *dev, size_t size,
150 void *vaddr, dma_addr_t dma_handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100152 free_pages((unsigned long)vaddr, get_order(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100155static dma_addr_t dma_direct_map_single(struct device *dev, void *ptr,
156 size_t size,
Mark Nelson3affedc2008-07-05 05:05:42 +1000157 enum dma_data_direction direction,
158 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Michael Ellerman35e4a6e2008-01-21 16:42:43 +1100160 return virt_to_abs(ptr) + get_dma_direct_offset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100162
163static void dma_direct_unmap_single(struct device *dev, dma_addr_t dma_addr,
164 size_t size,
Mark Nelson3affedc2008-07-05 05:05:42 +1000165 enum dma_data_direction direction,
166 struct dma_attrs *attrs)
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100167{
168}
169
Jens Axboe78bdc312007-10-12 13:44:12 +0200170static int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl,
Mark Nelson3affedc2008-07-05 05:05:42 +1000171 int nents, enum dma_data_direction direction,
172 struct dma_attrs *attrs)
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100173{
Jens Axboe78bdc312007-10-12 13:44:12 +0200174 struct scatterlist *sg;
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100175 int i;
176
Jens Axboe78bdc312007-10-12 13:44:12 +0200177 for_each_sg(sgl, sg, nents, i) {
Michael Ellerman35e4a6e2008-01-21 16:42:43 +1100178 sg->dma_address = sg_phys(sg) + get_dma_direct_offset(dev);
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100179 sg->dma_length = sg->length;
180 }
181
182 return nents;
183}
184
185static void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sg,
Mark Nelson3affedc2008-07-05 05:05:42 +1000186 int nents, enum dma_data_direction direction,
187 struct dma_attrs *attrs)
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100188{
189}
190
191static int dma_direct_dma_supported(struct device *dev, u64 mask)
192{
193 /* Could be improved to check for memory though it better be
194 * done via some global so platforms can set the limit in case
195 * they have limited DMA windows
196 */
197 return mask >= DMA_32BIT_MASK;
198}
199
200struct dma_mapping_ops dma_direct_ops = {
201 .alloc_coherent = dma_direct_alloc_coherent,
202 .free_coherent = dma_direct_free_coherent,
203 .map_single = dma_direct_map_single,
204 .unmap_single = dma_direct_unmap_single,
205 .map_sg = dma_direct_map_sg,
206 .unmap_sg = dma_direct_unmap_sg,
207 .dma_supported = dma_direct_dma_supported,
208};
209EXPORT_SYMBOL(dma_direct_ops);