blob: a5bc528d43282a344936922328d2b5bf59b187ae [file] [log] [blame]
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +01001/* Glue code to lib/swiotlb.c */
2
3#include <linux/pci.h>
4#include <linux/cache.h>
5#include <linux/module.h>
Jeremy Fitzhardinge8ce79962008-12-16 12:17:35 -08006#include <linux/swiotlb.h>
7#include <linux/bootmem.h>
Rolf Eike Beerd6bd3a32006-09-29 01:59:48 -07008#include <linux/dma-mapping.h>
9
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090010#include <asm/iommu.h>
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010011#include <asm/swiotlb.h>
12#include <asm/dma.h>
13
14int swiotlb __read_mostly;
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010015
FUJITA Tomonori03967c52008-10-23 23:14:29 +090016static void *x86_swiotlb_alloc_coherent(struct device *hwdev, size_t size,
17 dma_addr_t *dma_handle, gfp_t flags)
18{
19 void *vaddr;
20
21 vaddr = dma_generic_alloc_coherent(hwdev, size, dma_handle, flags);
22 if (vaddr)
23 return vaddr;
24
25 return swiotlb_alloc_coherent(hwdev, size, dma_handle, flags);
26}
27
Jaswinder Singh Rajputff6c6fe2009-04-12 23:24:21 +053028static struct dma_map_ops swiotlb_dma_ops = {
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010029 .mapping_error = swiotlb_dma_mapping_error,
FUJITA Tomonori03967c52008-10-23 23:14:29 +090030 .alloc_coherent = x86_swiotlb_alloc_coherent,
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010031 .free_coherent = swiotlb_free_coherent,
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010032 .sync_single_for_cpu = swiotlb_sync_single_for_cpu,
33 .sync_single_for_device = swiotlb_sync_single_for_device,
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010034 .sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,
35 .sync_sg_for_device = swiotlb_sync_sg_for_device,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +090036 .map_sg = swiotlb_map_sg_attrs,
37 .unmap_sg = swiotlb_unmap_sg_attrs,
FUJITA Tomonori4cf37bb2009-01-05 23:47:22 +090038 .map_page = swiotlb_map_page,
39 .unmap_page = swiotlb_unmap_page,
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010040 .dma_supported = NULL,
41};
42
FUJITA Tomonorib18485e2009-11-12 00:03:28 +090043/*
FUJITA Tomonori186a2502009-12-15 20:47:56 +090044 * pci_swiotlb_detect - set swiotlb to 1 if necessary
FUJITA Tomonorib18485e2009-11-12 00:03:28 +090045 *
46 * This returns non-zero if we are forced to use swiotlb (by the boot
47 * option).
48 */
FUJITA Tomonori186a2502009-12-15 20:47:56 +090049int __init pci_swiotlb_detect(void)
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010050{
FUJITA Tomonori273bee22009-11-25 08:46:28 +090051 int use_swiotlb = swiotlb | swiotlb_force;
52
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010053 /* don't initialize swiotlb if iommu=off (no_iommu=1) */
Jeremy Fitzhardingecfb80c92008-12-16 12:17:36 -080054#ifdef CONFIG_X86_64
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +090055 if (!no_iommu && max_pfn > MAX_DMA32_PFN)
David Woodhouse19943b02009-08-04 16:19:20 +010056 swiotlb = 1;
Jeremy Fitzhardingecfb80c92008-12-16 12:17:36 -080057#endif
FUJITA Tomonorib18485e2009-11-12 00:03:28 +090058 if (swiotlb_force)
59 swiotlb = 1;
60
FUJITA Tomonori186a2502009-12-15 20:47:56 +090061 return use_swiotlb;
62}
63
64void __init pci_swiotlb_init(void)
65{
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010066 if (swiotlb) {
FUJITA Tomonoriad32e8c2009-11-10 19:46:19 +090067 swiotlb_init(0);
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010068 dma_ops = &swiotlb_dma_ops;
FUJITA Tomonoria3b28ee2009-11-14 20:46:36 +090069 }
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010070}