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> |
Paul Gortmaker | 186f436 | 2016-07-13 20:18:56 -0400 | [diff] [blame^] | 5 | #include <linux/init.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 | |
Akinobu Mita | 9c5a362 | 2014-06-04 16:06:50 -0700 | [diff] [blame] | 17 | void *x86_swiotlb_alloc_coherent(struct device *hwdev, size_t size, |
Andrzej Pietrasiewicz | baa676f | 2012-03-27 14:28:18 +0200 | [diff] [blame] | 18 | dma_addr_t *dma_handle, gfp_t flags, |
| 19 | struct dma_attrs *attrs) |
FUJITA Tomonori | 03967c5 | 2008-10-23 23:14:29 +0900 | [diff] [blame] | 20 | { |
| 21 | void *vaddr; |
| 22 | |
Joerg Roedel | 186dfc9 | 2015-06-10 17:49:41 +0200 | [diff] [blame] | 23 | /* |
| 24 | * Don't print a warning when the first allocation attempt fails. |
| 25 | * swiotlb_alloc_coherent() will print a warning when the DMA |
| 26 | * memory allocation ultimately failed. |
| 27 | */ |
| 28 | flags |= __GFP_NOWARN; |
| 29 | |
Andrzej Pietrasiewicz | baa676f | 2012-03-27 14:28:18 +0200 | [diff] [blame] | 30 | vaddr = dma_generic_alloc_coherent(hwdev, size, dma_handle, flags, |
| 31 | attrs); |
FUJITA Tomonori | 03967c5 | 2008-10-23 23:14:29 +0900 | [diff] [blame] | 32 | if (vaddr) |
| 33 | return vaddr; |
| 34 | |
| 35 | return swiotlb_alloc_coherent(hwdev, size, dma_handle, flags); |
| 36 | } |
| 37 | |
Akinobu Mita | 9c5a362 | 2014-06-04 16:06:50 -0700 | [diff] [blame] | 38 | void x86_swiotlb_free_coherent(struct device *dev, size_t size, |
Andrzej Pietrasiewicz | baa676f | 2012-03-27 14:28:18 +0200 | [diff] [blame] | 39 | void *vaddr, dma_addr_t dma_addr, |
| 40 | struct dma_attrs *attrs) |
| 41 | { |
Akinobu Mita | 9c5a362 | 2014-06-04 16:06:50 -0700 | [diff] [blame] | 42 | if (is_swiotlb_buffer(dma_to_phys(dev, dma_addr))) |
| 43 | swiotlb_free_coherent(dev, size, vaddr, dma_addr); |
| 44 | else |
| 45 | dma_generic_free_coherent(dev, size, vaddr, dma_addr, attrs); |
Andrzej Pietrasiewicz | baa676f | 2012-03-27 14:28:18 +0200 | [diff] [blame] | 46 | } |
| 47 | |
Jaswinder Singh Rajput | ff6c6fed | 2009-04-12 23:24:21 +0530 | [diff] [blame] | 48 | static struct dma_map_ops swiotlb_dma_ops = { |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 49 | .mapping_error = swiotlb_dma_mapping_error, |
Andrzej Pietrasiewicz | baa676f | 2012-03-27 14:28:18 +0200 | [diff] [blame] | 50 | .alloc = x86_swiotlb_alloc_coherent, |
| 51 | .free = x86_swiotlb_free_coherent, |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 52 | .sync_single_for_cpu = swiotlb_sync_single_for_cpu, |
| 53 | .sync_single_for_device = swiotlb_sync_single_for_device, |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 54 | .sync_sg_for_cpu = swiotlb_sync_sg_for_cpu, |
| 55 | .sync_sg_for_device = swiotlb_sync_sg_for_device, |
FUJITA Tomonori | 160c1d8 | 2009-01-05 23:59:02 +0900 | [diff] [blame] | 56 | .map_sg = swiotlb_map_sg_attrs, |
| 57 | .unmap_sg = swiotlb_unmap_sg_attrs, |
FUJITA Tomonori | 4cf37bb | 2009-01-05 23:47:22 +0900 | [diff] [blame] | 58 | .map_page = swiotlb_map_page, |
| 59 | .unmap_page = swiotlb_unmap_page, |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 60 | .dma_supported = NULL, |
| 61 | }; |
| 62 | |
FUJITA Tomonori | b18485e | 2009-11-12 00:03:28 +0900 | [diff] [blame] | 63 | /* |
Konrad Rzeszutek Wilk | efa631c | 2010-08-26 13:57:59 -0400 | [diff] [blame] | 64 | * pci_swiotlb_detect_override - set swiotlb to 1 if necessary |
FUJITA Tomonori | b18485e | 2009-11-12 00:03:28 +0900 | [diff] [blame] | 65 | * |
| 66 | * This returns non-zero if we are forced to use swiotlb (by the boot |
| 67 | * option). |
| 68 | */ |
Konrad Rzeszutek Wilk | efa631c | 2010-08-26 13:57:59 -0400 | [diff] [blame] | 69 | int __init pci_swiotlb_detect_override(void) |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 70 | { |
FUJITA Tomonori | 273bee2 | 2009-11-25 08:46:28 +0900 | [diff] [blame] | 71 | int use_swiotlb = swiotlb | swiotlb_force; |
| 72 | |
Konrad Rzeszutek Wilk | efa631c | 2010-08-26 13:57:59 -0400 | [diff] [blame] | 73 | if (swiotlb_force) |
| 74 | swiotlb = 1; |
| 75 | |
| 76 | return use_swiotlb; |
| 77 | } |
Konrad Rzeszutek Wilk | c116c54 | 2010-08-26 13:58:00 -0400 | [diff] [blame] | 78 | IOMMU_INIT_FINISH(pci_swiotlb_detect_override, |
| 79 | pci_xen_swiotlb_detect, |
| 80 | pci_swiotlb_init, |
| 81 | pci_swiotlb_late_init); |
Konrad Rzeszutek Wilk | efa631c | 2010-08-26 13:57:59 -0400 | [diff] [blame] | 82 | |
| 83 | /* |
| 84 | * if 4GB or more detected (and iommu=off not set) return 1 |
| 85 | * and set swiotlb to 1. |
| 86 | */ |
| 87 | int __init pci_swiotlb_detect_4gb(void) |
| 88 | { |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 89 | /* don't initialize swiotlb if iommu=off (no_iommu=1) */ |
Jeremy Fitzhardinge | cfb80c9 | 2008-12-16 12:17:36 -0800 | [diff] [blame] | 90 | #ifdef CONFIG_X86_64 |
Igor Mammedov | ec941c5 | 2015-12-04 14:07:06 +0100 | [diff] [blame] | 91 | if (!no_iommu && max_possible_pfn > MAX_DMA32_PFN) |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 92 | swiotlb = 1; |
Jeremy Fitzhardinge | cfb80c9 | 2008-12-16 12:17:36 -0800 | [diff] [blame] | 93 | #endif |
Konrad Rzeszutek Wilk | efa631c | 2010-08-26 13:57:59 -0400 | [diff] [blame] | 94 | return swiotlb; |
FUJITA Tomonori | 186a250 | 2009-12-15 20:47:56 +0900 | [diff] [blame] | 95 | } |
Konrad Rzeszutek Wilk | c116c54 | 2010-08-26 13:58:00 -0400 | [diff] [blame] | 96 | IOMMU_INIT(pci_swiotlb_detect_4gb, |
| 97 | pci_swiotlb_detect_override, |
| 98 | pci_swiotlb_init, |
| 99 | pci_swiotlb_late_init); |
FUJITA Tomonori | 186a250 | 2009-12-15 20:47:56 +0900 | [diff] [blame] | 100 | |
| 101 | void __init pci_swiotlb_init(void) |
| 102 | { |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 103 | if (swiotlb) { |
FUJITA Tomonori | ad32e8c | 2009-11-10 19:46:19 +0900 | [diff] [blame] | 104 | swiotlb_init(0); |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 105 | dma_ops = &swiotlb_dma_ops; |
FUJITA Tomonori | a3b28ee | 2009-11-14 20:46:36 +0900 | [diff] [blame] | 106 | } |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 107 | } |
Konrad Rzeszutek Wilk | efa631c | 2010-08-26 13:57:59 -0400 | [diff] [blame] | 108 | |
| 109 | void __init pci_swiotlb_late_init(void) |
| 110 | { |
| 111 | /* An IOMMU turned us off. */ |
| 112 | if (!swiotlb) |
| 113 | swiotlb_free(); |
| 114 | else { |
| 115 | printk(KERN_INFO "PCI-DMA: " |
| 116 | "Using software bounce buffering for IO (SWIOTLB)\n"); |
| 117 | swiotlb_print_info(); |
| 118 | } |
| 119 | } |