blob: 8f972cbddef0c390df0a386ff4d08a8c20865ea1 [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>
Konrad Rzeszutek Wilkc116c542010-08-26 13:58:00 -040013#include <asm/xen/swiotlb-xen.h>
14#include <asm/iommu_table.h>
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010015int swiotlb __read_mostly;
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010016
FUJITA Tomonori03967c52008-10-23 23:14:29 +090017static void *x86_swiotlb_alloc_coherent(struct device *hwdev, size_t size,
18 dma_addr_t *dma_handle, gfp_t flags)
19{
20 void *vaddr;
21
22 vaddr = dma_generic_alloc_coherent(hwdev, size, dma_handle, flags);
23 if (vaddr)
24 return vaddr;
25
26 return swiotlb_alloc_coherent(hwdev, size, dma_handle, flags);
27}
28
Jaswinder Singh Rajputff6c6fed2009-04-12 23:24:21 +053029static struct dma_map_ops swiotlb_dma_ops = {
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010030 .mapping_error = swiotlb_dma_mapping_error,
FUJITA Tomonori03967c52008-10-23 23:14:29 +090031 .alloc_coherent = x86_swiotlb_alloc_coherent,
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010032 .free_coherent = swiotlb_free_coherent,
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010033 .sync_single_for_cpu = swiotlb_sync_single_for_cpu,
34 .sync_single_for_device = swiotlb_sync_single_for_device,
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010035 .sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,
36 .sync_sg_for_device = swiotlb_sync_sg_for_device,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +090037 .map_sg = swiotlb_map_sg_attrs,
38 .unmap_sg = swiotlb_unmap_sg_attrs,
FUJITA Tomonori4cf37bb2009-01-05 23:47:22 +090039 .map_page = swiotlb_map_page,
40 .unmap_page = swiotlb_unmap_page,
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010041 .dma_supported = NULL,
42};
43
FUJITA Tomonorib18485e2009-11-12 00:03:28 +090044/*
Konrad Rzeszutek Wilkefa631c2010-08-26 13:57:59 -040045 * pci_swiotlb_detect_override - set swiotlb to 1 if necessary
FUJITA Tomonorib18485e2009-11-12 00:03:28 +090046 *
47 * This returns non-zero if we are forced to use swiotlb (by the boot
48 * option).
49 */
Konrad Rzeszutek Wilkefa631c2010-08-26 13:57:59 -040050int __init pci_swiotlb_detect_override(void)
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010051{
FUJITA Tomonori273bee22009-11-25 08:46:28 +090052 int use_swiotlb = swiotlb | swiotlb_force;
53
Konrad Rzeszutek Wilkefa631c2010-08-26 13:57:59 -040054 if (swiotlb_force)
55 swiotlb = 1;
56
57 return use_swiotlb;
58}
Konrad Rzeszutek Wilkc116c542010-08-26 13:58:00 -040059IOMMU_INIT_FINISH(pci_swiotlb_detect_override,
60 pci_xen_swiotlb_detect,
61 pci_swiotlb_init,
62 pci_swiotlb_late_init);
Konrad Rzeszutek Wilkefa631c2010-08-26 13:57:59 -040063
64/*
65 * if 4GB or more detected (and iommu=off not set) return 1
66 * and set swiotlb to 1.
67 */
68int __init pci_swiotlb_detect_4gb(void)
69{
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010070 /* don't initialize swiotlb if iommu=off (no_iommu=1) */
Jeremy Fitzhardingecfb80c92008-12-16 12:17:36 -080071#ifdef CONFIG_X86_64
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +090072 if (!no_iommu && max_pfn > MAX_DMA32_PFN)
David Woodhouse19943b02009-08-04 16:19:20 +010073 swiotlb = 1;
Jeremy Fitzhardingecfb80c92008-12-16 12:17:36 -080074#endif
Konrad Rzeszutek Wilkefa631c2010-08-26 13:57:59 -040075 return swiotlb;
FUJITA Tomonori186a2502009-12-15 20:47:56 +090076}
Konrad Rzeszutek Wilkc116c542010-08-26 13:58:00 -040077IOMMU_INIT(pci_swiotlb_detect_4gb,
78 pci_swiotlb_detect_override,
79 pci_swiotlb_init,
80 pci_swiotlb_late_init);
FUJITA Tomonori186a2502009-12-15 20:47:56 +090081
82void __init pci_swiotlb_init(void)
83{
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010084 if (swiotlb) {
FUJITA Tomonoriad32e8c2009-11-10 19:46:19 +090085 swiotlb_init(0);
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010086 dma_ops = &swiotlb_dma_ops;
FUJITA Tomonoria3b28ee2009-11-14 20:46:36 +090087 }
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010088}
Konrad Rzeszutek Wilkefa631c2010-08-26 13:57:59 -040089
90void __init pci_swiotlb_late_init(void)
91{
92 /* An IOMMU turned us off. */
93 if (!swiotlb)
94 swiotlb_free();
95 else {
96 printk(KERN_INFO "PCI-DMA: "
97 "Using software bounce buffering for IO (SWIOTLB)\n");
98 swiotlb_print_info();
99 }
100}