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 static inline unsigned long device_to_mask(struct device
*dev
)
20 if (dev
->dma_mask
&& *dev
->dma_mask
)
21 return *dev
->dma_mask
;
22 /* Assume devices without mask can take 32 bit addresses */
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.
31 static void *dma_iommu_alloc_coherent(struct device
*dev
, size_t size
,
32 dma_addr_t
*dma_handle
, gfp_t flag
)
34 return iommu_alloc_coherent(dev
->archdata
.dma_data
, size
, dma_handle
,
35 device_to_mask(dev
), flag
,
36 dev
->archdata
.numa_node
);
39 static void dma_iommu_free_coherent(struct device
*dev
, size_t size
,
40 void *vaddr
, dma_addr_t dma_handle
)
42 iommu_free_coherent(dev
->archdata
.dma_data
, size
, vaddr
, dma_handle
);
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.
51 static dma_addr_t
dma_iommu_map_single(struct device
*dev
, void *vaddr
,
53 enum dma_data_direction direction
)
55 return iommu_map_single(dev
->archdata
.dma_data
, vaddr
, size
,
56 device_to_mask(dev
), direction
);
60 static void dma_iommu_unmap_single(struct device
*dev
, dma_addr_t dma_handle
,
62 enum dma_data_direction direction
)
64 iommu_unmap_single(dev
->archdata
.dma_data
, dma_handle
, size
, direction
);
68 static int dma_iommu_map_sg(struct device
*dev
, struct scatterlist
*sglist
,
69 int nelems
, enum dma_data_direction direction
)
71 return iommu_map_sg(dev
->archdata
.dma_data
, sglist
, nelems
,
72 device_to_mask(dev
), direction
);
75 static void dma_iommu_unmap_sg(struct device
*dev
, struct scatterlist
*sglist
,
76 int nelems
, enum dma_data_direction direction
)
78 iommu_unmap_sg(dev
->archdata
.dma_data
, sglist
, nelems
, direction
);
81 /* We support DMA to/from any memory page via the iommu */
82 static int dma_iommu_dma_supported(struct device
*dev
, u64 mask
)
84 struct iommu_table
*tbl
= dev
->archdata
.dma_data
;
86 if (!tbl
|| tbl
->it_offset
> mask
) {
88 "Warning: IOMMU offset too big for device mask\n");
91 "mask: 0x%08lx, table offset: 0x%08lx\n",
92 mask
, tbl
->it_offset
);
94 printk(KERN_INFO
"mask: 0x%08lx, table unavailable\n",
101 struct 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
,
110 EXPORT_SYMBOL(dma_iommu_ops
);
113 * Generic direct DMA implementation
115 * This implementation supports a global offset that can be applied if
116 * the address at which memory is visible to devices is not 0.
118 unsigned long dma_direct_offset
;
120 static void *dma_direct_alloc_coherent(struct device
*dev
, size_t size
,
121 dma_addr_t
*dma_handle
, gfp_t flag
)
125 /* TODO: Maybe use the numa node here too ? */
126 ret
= (void *)__get_free_pages(flag
, get_order(size
));
128 memset(ret
, 0, size
);
129 *dma_handle
= virt_to_abs(ret
) | dma_direct_offset
;
134 static void dma_direct_free_coherent(struct device
*dev
, size_t size
,
135 void *vaddr
, dma_addr_t dma_handle
)
137 free_pages((unsigned long)vaddr
, get_order(size
));
140 static dma_addr_t
dma_direct_map_single(struct device
*dev
, void *ptr
,
142 enum dma_data_direction direction
)
144 return virt_to_abs(ptr
) | dma_direct_offset
;
147 static void dma_direct_unmap_single(struct device
*dev
, dma_addr_t dma_addr
,
149 enum dma_data_direction direction
)
153 static int dma_direct_map_sg(struct device
*dev
, struct scatterlist
*sg
,
154 int nents
, enum dma_data_direction direction
)
158 for (i
= 0; i
< nents
; i
++, sg
++) {
159 sg
->dma_address
= (page_to_phys(sg
->page
) + sg
->offset
) |
161 sg
->dma_length
= sg
->length
;
167 static void dma_direct_unmap_sg(struct device
*dev
, struct scatterlist
*sg
,
168 int nents
, enum dma_data_direction direction
)
172 static int dma_direct_dma_supported(struct device
*dev
, u64 mask
)
174 /* Could be improved to check for memory though it better be
175 * done via some global so platforms can set the limit in case
176 * they have limited DMA windows
178 return mask
>= DMA_32BIT_MASK
;
181 struct dma_mapping_ops dma_direct_ops
= {
182 .alloc_coherent
= dma_direct_alloc_coherent
,
183 .free_coherent
= dma_direct_free_coherent
,
184 .map_single
= dma_direct_map_single
,
185 .unmap_single
= dma_direct_unmap_single
,
186 .map_sg
= dma_direct_map_sg
,
187 .unmap_sg
= dma_direct_unmap_sg
,
188 .dma_supported
= dma_direct_dma_supported
,
190 EXPORT_SYMBOL(dma_direct_ops
);