Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 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> |
Mike Rapoport | 57c8a66 | 2018-10-30 15:09:49 -0700 | [diff] [blame] | 7 | #include <linux/memblock.h> |
Christoph Hellwig | ea8c64a | 2018-01-10 16:21:13 +0100 | [diff] [blame] | 8 | #include <linux/dma-direct.h> |
Tom Lendacky | 32cb4d0 | 2021-09-08 17:58:36 -0500 | [diff] [blame] | 9 | #include <linux/cc_platform.h> |
Rolf Eike Beer | d6bd3a3 | 2006-09-29 01:59:48 -0700 | [diff] [blame] | 10 | |
FUJITA Tomonori | 46a7fa2 | 2008-07-11 10:23:42 +0900 | [diff] [blame] | 11 | #include <asm/iommu.h> |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 12 | #include <asm/swiotlb.h> |
| 13 | #include <asm/dma.h> |
Konrad Rzeszutek Wilk | c116c54 | 2010-08-26 13:58:00 -0400 | [diff] [blame] | 14 | #include <asm/xen/swiotlb-xen.h> |
| 15 | #include <asm/iommu_table.h> |
Tom Lendacky | c775320 | 2017-07-17 16:10:21 -0500 | [diff] [blame] | 16 | |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 17 | int swiotlb __read_mostly; |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 18 | |
FUJITA Tomonori | b18485e | 2009-11-12 00:03:28 +0900 | [diff] [blame] | 19 | /* |
Konrad Rzeszutek Wilk | efa631c | 2010-08-26 13:57:59 -0400 | [diff] [blame] | 20 | * pci_swiotlb_detect_override - set swiotlb to 1 if necessary |
FUJITA Tomonori | b18485e | 2009-11-12 00:03:28 +0900 | [diff] [blame] | 21 | * |
| 22 | * This returns non-zero if we are forced to use swiotlb (by the boot |
| 23 | * option). |
| 24 | */ |
Konrad Rzeszutek Wilk | efa631c | 2010-08-26 13:57:59 -0400 | [diff] [blame] | 25 | int __init pci_swiotlb_detect_override(void) |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 26 | { |
Geert Uytterhoeven | ae7871b | 2016-12-16 14:28:41 +0100 | [diff] [blame] | 27 | if (swiotlb_force == SWIOTLB_FORCE) |
Konrad Rzeszutek Wilk | efa631c | 2010-08-26 13:57:59 -0400 | [diff] [blame] | 28 | swiotlb = 1; |
| 29 | |
Geert Uytterhoeven | 6c206e4 | 2016-12-16 14:28:40 +0100 | [diff] [blame] | 30 | return swiotlb; |
Konrad Rzeszutek Wilk | efa631c | 2010-08-26 13:57:59 -0400 | [diff] [blame] | 31 | } |
Konrad Rzeszutek Wilk | c116c54 | 2010-08-26 13:58:00 -0400 | [diff] [blame] | 32 | IOMMU_INIT_FINISH(pci_swiotlb_detect_override, |
| 33 | pci_xen_swiotlb_detect, |
| 34 | pci_swiotlb_init, |
| 35 | pci_swiotlb_late_init); |
Konrad Rzeszutek Wilk | efa631c | 2010-08-26 13:57:59 -0400 | [diff] [blame] | 36 | |
| 37 | /* |
Tom Lendacky | c775320 | 2017-07-17 16:10:21 -0500 | [diff] [blame] | 38 | * If 4GB or more detected (and iommu=off not set) or if SME is active |
| 39 | * then set swiotlb to 1 and return 1. |
Konrad Rzeszutek Wilk | efa631c | 2010-08-26 13:57:59 -0400 | [diff] [blame] | 40 | */ |
| 41 | int __init pci_swiotlb_detect_4gb(void) |
| 42 | { |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 43 | /* don't initialize swiotlb if iommu=off (no_iommu=1) */ |
Igor Mammedov | ec941c5 | 2015-12-04 14:07:06 +0100 | [diff] [blame] | 44 | if (!no_iommu && max_possible_pfn > MAX_DMA32_PFN) |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 45 | swiotlb = 1; |
Tom Lendacky | c775320 | 2017-07-17 16:10:21 -0500 | [diff] [blame] | 46 | |
| 47 | /* |
Tom Lendacky | 32cb4d0 | 2021-09-08 17:58:36 -0500 | [diff] [blame] | 48 | * Set swiotlb to 1 so that bounce buffers are allocated and used for |
| 49 | * devices that can't support DMA to encrypted memory. |
Tom Lendacky | c775320 | 2017-07-17 16:10:21 -0500 | [diff] [blame] | 50 | */ |
Tom Lendacky | 32cb4d0 | 2021-09-08 17:58:36 -0500 | [diff] [blame] | 51 | if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)) |
Tom Lendacky | c775320 | 2017-07-17 16:10:21 -0500 | [diff] [blame] | 52 | swiotlb = 1; |
| 53 | |
Konrad Rzeszutek Wilk | efa631c | 2010-08-26 13:57:59 -0400 | [diff] [blame] | 54 | return swiotlb; |
FUJITA Tomonori | 186a250 | 2009-12-15 20:47:56 +0900 | [diff] [blame] | 55 | } |
Konrad Rzeszutek Wilk | c116c54 | 2010-08-26 13:58:00 -0400 | [diff] [blame] | 56 | IOMMU_INIT(pci_swiotlb_detect_4gb, |
| 57 | pci_swiotlb_detect_override, |
| 58 | pci_swiotlb_init, |
| 59 | pci_swiotlb_late_init); |
FUJITA Tomonori | 186a250 | 2009-12-15 20:47:56 +0900 | [diff] [blame] | 60 | |
| 61 | void __init pci_swiotlb_init(void) |
| 62 | { |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 63 | if (swiotlb) |
FUJITA Tomonori | ad32e8c | 2009-11-10 19:46:19 +0900 | [diff] [blame] | 64 | swiotlb_init(0); |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 65 | } |
Konrad Rzeszutek Wilk | efa631c | 2010-08-26 13:57:59 -0400 | [diff] [blame] | 66 | |
| 67 | void __init pci_swiotlb_late_init(void) |
| 68 | { |
| 69 | /* An IOMMU turned us off. */ |
| 70 | if (!swiotlb) |
Christoph Hellwig | 7f2c8bb | 2017-12-23 14:14:54 +0100 | [diff] [blame] | 71 | swiotlb_exit(); |
Konrad Rzeszutek Wilk | efa631c | 2010-08-26 13:57:59 -0400 | [diff] [blame] | 72 | else { |
| 73 | printk(KERN_INFO "PCI-DMA: " |
| 74 | "Using software bounce buffering for IO (SWIOTLB)\n"); |
| 75 | swiotlb_print_info(); |
| 76 | } |
| 77 | } |