2018-08-28 09:40:15 +02:00
// SPDX-License-Identifier: GPL-2.0
2010-02-09 14:30:55 -05:00
/* Glue code to lib/swiotlb-xen.c */
2020-09-22 15:31:03 +02:00
# include <linux/dma-map-ops.h>
2010-08-19 13:34:58 -04:00
# include <linux/pci.h>
2010-02-09 14:30:55 -05:00
# include <xen/swiotlb-xen.h>
# include <asm/xen/hypervisor.h>
# include <xen/xen.h>
2010-08-26 13:58:01 -04:00
# include <asm/iommu_table.h>
2010-02-09 14:30:55 -05:00
2012-09-22 20:01:16 -04:00
2012-08-21 14:49:34 -04:00
# include <asm/xen/swiotlb-xen.h>
2012-07-27 20:16:00 -04:00
# ifdef CONFIG_X86_64
# include <asm/iommu.h>
# include <asm/dma.h>
# endif
2012-08-23 14:36:15 -04:00
# include <linux/export.h>
2012-09-22 20:01:16 -04:00
2021-09-17 12:49:04 +02:00
static int xen_swiotlb __read_mostly ;
2010-02-09 14:30:55 -05:00
/*
* pci_xen_swiotlb_detect - set xen_swiotlb to 1 if necessary
*
* This returns non - zero if we are forced to use xen_swiotlb ( by the boot
* option ) .
*/
int __init pci_xen_swiotlb_detect ( void )
{
2012-07-27 20:10:58 -04:00
if ( ! xen_pv_domain ( ) )
return 0 ;
2010-02-09 14:30:55 -05:00
/* If running as PV guest, either iommu=soft, or swiotlb=force will
* activate this IOMMU . If running as PV privileged , activate it
2011-05-10 10:16:21 +02:00
* irregardless .
2010-02-09 14:30:55 -05:00
*/
2016-12-16 14:28:41 +01:00
if ( xen_initial_domain ( ) | | swiotlb | | swiotlb_force = = SWIOTLB_FORCE )
2010-02-09 14:30:55 -05:00
xen_swiotlb = 1 ;
/* If we are running under Xen, we MUST disable the native SWIOTLB.
* Don ' t worry about swiotlb_force flag activating the native , as
* the ' swiotlb ' flag is the only one turning it on . */
2012-07-27 20:10:58 -04:00
swiotlb = 0 ;
2010-02-09 14:30:55 -05:00
2012-07-27 20:16:00 -04:00
# ifdef CONFIG_X86_64
/* pci_swiotlb_detect_4gb turns on native SWIOTLB if no_iommu == 0
* ( so no iommu = X command line over - writes ) .
* Considering that PV guests do not want the * native SWIOTLB * but
* only Xen SWIOTLB it is not useful to us so set no_iommu = 1 here .
*/
if ( max_pfn > MAX_DMA32_PFN )
no_iommu = 1 ;
# endif
2010-02-09 14:30:55 -05:00
return xen_swiotlb ;
}
2021-09-17 12:49:04 +02:00
static void __init pci_xen_swiotlb_init ( void )
2010-02-09 14:30:55 -05:00
{
if ( xen_swiotlb ) {
2021-03-01 08:44:32 +01:00
xen_swiotlb_init_early ( ) ;
2010-02-09 14:30:55 -05:00
dma_ops = & xen_swiotlb_dma_ops ;
2010-08-19 13:34:58 -04:00
2013-11-04 18:11:54 +00:00
# ifdef CONFIG_PCI
2010-08-19 13:34:58 -04:00
/* Make sure ACS will be enabled */
pci_request_acs ( ) ;
2013-11-04 18:11:54 +00:00
# endif
2010-02-09 14:30:55 -05:00
}
}
2012-08-23 14:36:15 -04:00
int pci_xen_swiotlb_init_late ( void )
{
int rc ;
if ( xen_swiotlb )
return 0 ;
2021-03-01 08:44:32 +01:00
rc = xen_swiotlb_init ( ) ;
2012-08-23 14:36:15 -04:00
if ( rc )
return rc ;
dma_ops = & xen_swiotlb_dma_ops ;
2013-11-04 18:11:54 +00:00
# ifdef CONFIG_PCI
2012-08-23 14:36:15 -04:00
/* Make sure ACS will be enabled */
pci_request_acs ( ) ;
2013-11-04 18:11:54 +00:00
# endif
2012-08-23 14:36:15 -04:00
return 0 ;
}
EXPORT_SYMBOL_GPL ( pci_xen_swiotlb_init_late ) ;
2010-08-26 13:58:01 -04:00
IOMMU_INIT_FINISH ( pci_xen_swiotlb_detect ,
2012-08-13 11:00:08 -04:00
NULL ,
2010-08-26 13:58:01 -04:00
pci_xen_swiotlb_init ,
2012-08-13 11:00:08 -04:00
NULL ) ;