blob: 5f5302028a9af0734c2bdc125414a0b98af073da [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/* Glue code to lib/swiotlb.c */
3
4#include <linux/pci.h>
5#include <linux/cache.h>
Paul Gortmaker186f4362016-07-13 20:18:56 -04006#include <linux/init.h>
Jeremy Fitzhardinge8ce79962008-12-16 12:17:35 -08007#include <linux/swiotlb.h>
Mike Rapoport57c8a662018-10-30 15:09:49 -07008#include <linux/memblock.h>
Christoph Hellwigea8c64a2018-01-10 16:21:13 +01009#include <linux/dma-direct.h>
Tom Lendackyc7753202017-07-17 16:10:21 -050010#include <linux/mem_encrypt.h>
Rolf Eike Beerd6bd3a32006-09-29 01:59:48 -070011
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090012#include <asm/iommu.h>
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010013#include <asm/swiotlb.h>
14#include <asm/dma.h>
Konrad Rzeszutek Wilkc116c542010-08-26 13:58:00 -040015#include <asm/xen/swiotlb-xen.h>
16#include <asm/iommu_table.h>
Tom Lendackyc7753202017-07-17 16:10:21 -050017
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010018int swiotlb __read_mostly;
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010019
FUJITA Tomonorib18485e2009-11-12 00:03:28 +090020/*
Konrad Rzeszutek Wilkefa631c2010-08-26 13:57:59 -040021 * pci_swiotlb_detect_override - set swiotlb to 1 if necessary
FUJITA Tomonorib18485e2009-11-12 00:03:28 +090022 *
23 * This returns non-zero if we are forced to use swiotlb (by the boot
24 * option).
25 */
Konrad Rzeszutek Wilkefa631c2010-08-26 13:57:59 -040026int __init pci_swiotlb_detect_override(void)
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010027{
Geert Uytterhoevenae7871b2016-12-16 14:28:41 +010028 if (swiotlb_force == SWIOTLB_FORCE)
Konrad Rzeszutek Wilkefa631c2010-08-26 13:57:59 -040029 swiotlb = 1;
30
Geert Uytterhoeven6c206e42016-12-16 14:28:40 +010031 return swiotlb;
Konrad Rzeszutek Wilkefa631c2010-08-26 13:57:59 -040032}
Konrad Rzeszutek Wilkc116c542010-08-26 13:58:00 -040033IOMMU_INIT_FINISH(pci_swiotlb_detect_override,
34 pci_xen_swiotlb_detect,
35 pci_swiotlb_init,
36 pci_swiotlb_late_init);
Konrad Rzeszutek Wilkefa631c2010-08-26 13:57:59 -040037
38/*
Tom Lendackyc7753202017-07-17 16:10:21 -050039 * If 4GB or more detected (and iommu=off not set) or if SME is active
40 * then set swiotlb to 1 and return 1.
Konrad Rzeszutek Wilkefa631c2010-08-26 13:57:59 -040041 */
42int __init pci_swiotlb_detect_4gb(void)
43{
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010044 /* don't initialize swiotlb if iommu=off (no_iommu=1) */
Igor Mammedovec941c52015-12-04 14:07:06 +010045 if (!no_iommu && max_possible_pfn > MAX_DMA32_PFN)
David Woodhouse19943b02009-08-04 16:19:20 +010046 swiotlb = 1;
Tom Lendackyc7753202017-07-17 16:10:21 -050047
48 /*
49 * If SME is active then swiotlb will be set to 1 so that bounce
50 * buffers are allocated and used for devices that do not support
51 * the addressing range required for the encryption mask.
52 */
53 if (sme_active())
54 swiotlb = 1;
55
Konrad Rzeszutek Wilkefa631c2010-08-26 13:57:59 -040056 return swiotlb;
FUJITA Tomonori186a2502009-12-15 20:47:56 +090057}
Konrad Rzeszutek Wilkc116c542010-08-26 13:58:00 -040058IOMMU_INIT(pci_swiotlb_detect_4gb,
59 pci_swiotlb_detect_override,
60 pci_swiotlb_init,
61 pci_swiotlb_late_init);
FUJITA Tomonori186a2502009-12-15 20:47:56 +090062
63void __init pci_swiotlb_init(void)
64{
Christoph Hellwig55897af2018-12-03 11:43:54 +010065 if (swiotlb)
FUJITA Tomonoriad32e8c2009-11-10 19:46:19 +090066 swiotlb_init(0);
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010067}
Konrad Rzeszutek Wilkefa631c2010-08-26 13:57:59 -040068
69void __init pci_swiotlb_late_init(void)
70{
71 /* An IOMMU turned us off. */
72 if (!swiotlb)
Christoph Hellwig7f2c8bb2017-12-23 14:14:54 +010073 swiotlb_exit();
Konrad Rzeszutek Wilkefa631c2010-08-26 13:57:59 -040074 else {
75 printk(KERN_INFO "PCI-DMA: "
76 "Using software bounce buffering for IO (SWIOTLB)\n");
77 swiotlb_print_info();
78 }
79}