Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 1 | /* Glue code to lib/swiotlb.c */ |
| 2 | |
| 3 | #include <linux/pci.h> |
| 4 | #include <linux/cache.h> |
| 5 | #include <linux/module.h> |
Jeremy Fitzhardinge | 8ce7996 | 2008-12-16 12:17:35 -0800 | [diff] [blame] | 6 | #include <linux/swiotlb.h> |
| 7 | #include <linux/bootmem.h> |
Rolf Eike Beer | d6bd3a3 | 2006-09-29 01:59:48 -0700 | [diff] [blame] | 8 | #include <linux/dma-mapping.h> |
| 9 | |
FUJITA Tomonori | 46a7fa2 | 2008-07-11 10:23:42 +0900 | [diff] [blame] | 10 | #include <asm/iommu.h> |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 11 | #include <asm/swiotlb.h> |
| 12 | #include <asm/dma.h> |
Konrad Rzeszutek Wilk | c116c54 | 2010-08-26 13:58:00 -0400 | [diff] [blame] | 13 | #include <asm/xen/swiotlb-xen.h> |
| 14 | #include <asm/iommu_table.h> |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 15 | int swiotlb __read_mostly; |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 16 | |
FUJITA Tomonori | 03967c5 | 2008-10-23 23:14:29 +0900 | [diff] [blame] | 17 | static 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 Rajput | ff6c6fed | 2009-04-12 23:24:21 +0530 | [diff] [blame] | 29 | static struct dma_map_ops swiotlb_dma_ops = { |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 30 | .mapping_error = swiotlb_dma_mapping_error, |
FUJITA Tomonori | 03967c5 | 2008-10-23 23:14:29 +0900 | [diff] [blame] | 31 | .alloc_coherent = x86_swiotlb_alloc_coherent, |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 32 | .free_coherent = swiotlb_free_coherent, |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 33 | .sync_single_for_cpu = swiotlb_sync_single_for_cpu, |
| 34 | .sync_single_for_device = swiotlb_sync_single_for_device, |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 35 | .sync_sg_for_cpu = swiotlb_sync_sg_for_cpu, |
| 36 | .sync_sg_for_device = swiotlb_sync_sg_for_device, |
FUJITA Tomonori | 160c1d8 | 2009-01-05 23:59:02 +0900 | [diff] [blame] | 37 | .map_sg = swiotlb_map_sg_attrs, |
| 38 | .unmap_sg = swiotlb_unmap_sg_attrs, |
FUJITA Tomonori | 4cf37bb | 2009-01-05 23:47:22 +0900 | [diff] [blame] | 39 | .map_page = swiotlb_map_page, |
| 40 | .unmap_page = swiotlb_unmap_page, |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 41 | .dma_supported = NULL, |
| 42 | }; |
| 43 | |
FUJITA Tomonori | b18485e | 2009-11-12 00:03:28 +0900 | [diff] [blame] | 44 | /* |
Konrad Rzeszutek Wilk | efa631c | 2010-08-26 13:57:59 -0400 | [diff] [blame] | 45 | * pci_swiotlb_detect_override - set swiotlb to 1 if necessary |
FUJITA Tomonori | b18485e | 2009-11-12 00:03:28 +0900 | [diff] [blame] | 46 | * |
| 47 | * This returns non-zero if we are forced to use swiotlb (by the boot |
| 48 | * option). |
| 49 | */ |
Konrad Rzeszutek Wilk | efa631c | 2010-08-26 13:57:59 -0400 | [diff] [blame] | 50 | int __init pci_swiotlb_detect_override(void) |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 51 | { |
FUJITA Tomonori | 273bee2 | 2009-11-25 08:46:28 +0900 | [diff] [blame] | 52 | int use_swiotlb = swiotlb | swiotlb_force; |
| 53 | |
Konrad Rzeszutek Wilk | efa631c | 2010-08-26 13:57:59 -0400 | [diff] [blame] | 54 | if (swiotlb_force) |
| 55 | swiotlb = 1; |
| 56 | |
| 57 | return use_swiotlb; |
| 58 | } |
Konrad Rzeszutek Wilk | c116c54 | 2010-08-26 13:58:00 -0400 | [diff] [blame] | 59 | IOMMU_INIT_FINISH(pci_swiotlb_detect_override, |
| 60 | pci_xen_swiotlb_detect, |
| 61 | pci_swiotlb_init, |
| 62 | pci_swiotlb_late_init); |
Konrad Rzeszutek Wilk | efa631c | 2010-08-26 13:57:59 -0400 | [diff] [blame] | 63 | |
| 64 | /* |
| 65 | * if 4GB or more detected (and iommu=off not set) return 1 |
| 66 | * and set swiotlb to 1. |
| 67 | */ |
| 68 | int __init pci_swiotlb_detect_4gb(void) |
| 69 | { |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 70 | /* don't initialize swiotlb if iommu=off (no_iommu=1) */ |
Jeremy Fitzhardinge | cfb80c9 | 2008-12-16 12:17:36 -0800 | [diff] [blame] | 71 | #ifdef CONFIG_X86_64 |
FUJITA Tomonori | 75f1cdf | 2009-11-10 19:46:20 +0900 | [diff] [blame] | 72 | if (!no_iommu && max_pfn > MAX_DMA32_PFN) |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 73 | swiotlb = 1; |
Jeremy Fitzhardinge | cfb80c9 | 2008-12-16 12:17:36 -0800 | [diff] [blame] | 74 | #endif |
Konrad Rzeszutek Wilk | efa631c | 2010-08-26 13:57:59 -0400 | [diff] [blame] | 75 | return swiotlb; |
FUJITA Tomonori | 186a250 | 2009-12-15 20:47:56 +0900 | [diff] [blame] | 76 | } |
Konrad Rzeszutek Wilk | c116c54 | 2010-08-26 13:58:00 -0400 | [diff] [blame] | 77 | IOMMU_INIT(pci_swiotlb_detect_4gb, |
| 78 | pci_swiotlb_detect_override, |
| 79 | pci_swiotlb_init, |
| 80 | pci_swiotlb_late_init); |
FUJITA Tomonori | 186a250 | 2009-12-15 20:47:56 +0900 | [diff] [blame] | 81 | |
| 82 | void __init pci_swiotlb_init(void) |
| 83 | { |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 84 | if (swiotlb) { |
FUJITA Tomonori | ad32e8c | 2009-11-10 19:46:19 +0900 | [diff] [blame] | 85 | swiotlb_init(0); |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 86 | dma_ops = &swiotlb_dma_ops; |
FUJITA Tomonori | a3b28ee | 2009-11-14 20:46:36 +0900 | [diff] [blame] | 87 | } |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 88 | } |
Konrad Rzeszutek Wilk | efa631c | 2010-08-26 13:57:59 -0400 | [diff] [blame] | 89 | |
| 90 | void __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 | } |