blob: 814ab46a0dadab7b0c4f985c0bc9df25506dd661 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +01002
3#include <linux/pci.h>
4#include <linux/cache.h>
Paul Gortmaker186f4362016-07-13 20:18:56 -04005#include <linux/init.h>
Jeremy Fitzhardinge8ce79962008-12-16 12:17:35 -08006#include <linux/swiotlb.h>
Mike Rapoport57c8a662018-10-30 15:09:49 -07007#include <linux/memblock.h>
Christoph Hellwigea8c64a2018-01-10 16:21:13 +01008#include <linux/dma-direct.h>
Tom Lendacky32cb4d02021-09-08 17:58:36 -05009#include <linux/cc_platform.h>
Rolf Eike Beerd6bd3a32006-09-29 01:59:48 -070010
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090011#include <asm/iommu.h>
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010012#include <asm/swiotlb.h>
13#include <asm/dma.h>
Konrad Rzeszutek Wilkc116c542010-08-26 13:58:00 -040014#include <asm/xen/swiotlb-xen.h>
15#include <asm/iommu_table.h>
Tom Lendackyc7753202017-07-17 16:10:21 -050016
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010017int swiotlb __read_mostly;
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010018
FUJITA Tomonorib18485e2009-11-12 00:03:28 +090019/*
Konrad Rzeszutek Wilkefa631c2010-08-26 13:57:59 -040020 * pci_swiotlb_detect_override - set swiotlb to 1 if necessary
FUJITA Tomonorib18485e2009-11-12 00:03:28 +090021 *
22 * This returns non-zero if we are forced to use swiotlb (by the boot
23 * option).
24 */
Konrad Rzeszutek Wilkefa631c2010-08-26 13:57:59 -040025int __init pci_swiotlb_detect_override(void)
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010026{
Geert Uytterhoevenae7871b2016-12-16 14:28:41 +010027 if (swiotlb_force == SWIOTLB_FORCE)
Konrad Rzeszutek Wilkefa631c2010-08-26 13:57:59 -040028 swiotlb = 1;
29
Geert Uytterhoeven6c206e42016-12-16 14:28:40 +010030 return swiotlb;
Konrad Rzeszutek Wilkefa631c2010-08-26 13:57:59 -040031}
Konrad Rzeszutek Wilkc116c542010-08-26 13:58:00 -040032IOMMU_INIT_FINISH(pci_swiotlb_detect_override,
33 pci_xen_swiotlb_detect,
34 pci_swiotlb_init,
35 pci_swiotlb_late_init);
Konrad Rzeszutek Wilkefa631c2010-08-26 13:57:59 -040036
37/*
Tom Lendackyc7753202017-07-17 16:10:21 -050038 * 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 Wilkefa631c2010-08-26 13:57:59 -040040 */
41int __init pci_swiotlb_detect_4gb(void)
42{
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010043 /* don't initialize swiotlb if iommu=off (no_iommu=1) */
Igor Mammedovec941c52015-12-04 14:07:06 +010044 if (!no_iommu && max_possible_pfn > MAX_DMA32_PFN)
David Woodhouse19943b02009-08-04 16:19:20 +010045 swiotlb = 1;
Tom Lendackyc7753202017-07-17 16:10:21 -050046
47 /*
Tom Lendacky32cb4d02021-09-08 17:58:36 -050048 * Set swiotlb to 1 so that bounce buffers are allocated and used for
49 * devices that can't support DMA to encrypted memory.
Tom Lendackyc7753202017-07-17 16:10:21 -050050 */
Tom Lendacky32cb4d02021-09-08 17:58:36 -050051 if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))
Tom Lendackyc7753202017-07-17 16:10:21 -050052 swiotlb = 1;
53
Konrad Rzeszutek Wilkefa631c2010-08-26 13:57:59 -040054 return swiotlb;
FUJITA Tomonori186a2502009-12-15 20:47:56 +090055}
Konrad Rzeszutek Wilkc116c542010-08-26 13:58:00 -040056IOMMU_INIT(pci_swiotlb_detect_4gb,
57 pci_swiotlb_detect_override,
58 pci_swiotlb_init,
59 pci_swiotlb_late_init);
FUJITA Tomonori186a2502009-12-15 20:47:56 +090060
61void __init pci_swiotlb_init(void)
62{
Christoph Hellwig55897af2018-12-03 11:43:54 +010063 if (swiotlb)
FUJITA Tomonoriad32e8c2009-11-10 19:46:19 +090064 swiotlb_init(0);
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010065}
Konrad Rzeszutek Wilkefa631c2010-08-26 13:57:59 -040066
67void __init pci_swiotlb_late_init(void)
68{
69 /* An IOMMU turned us off. */
70 if (!swiotlb)
Christoph Hellwig7f2c8bb2017-12-23 14:14:54 +010071 swiotlb_exit();
Konrad Rzeszutek Wilkefa631c2010-08-26 13:57:59 -040072 else {
73 printk(KERN_INFO "PCI-DMA: "
74 "Using software bounce buffering for IO (SWIOTLB)\n");
75 swiotlb_print_info();
76 }
77}