2018-01-26 20:45:16 +03:00
// SPDX-License-Identifier: GPL-2.0
2011-09-27 17:57:13 +04:00
/*
2018-03-10 01:36:33 +03:00
* PCI Express I / O Virtualization ( IOV ) support
2011-09-27 17:57:13 +04:00
* Address Translation Service 1.0
2011-09-27 17:57:15 +04:00
* Page Request Interface added by Joerg Roedel < joerg . roedel @ amd . com >
2011-09-27 17:57:16 +04:00
* PASID support added by Joerg Roedel < joerg . roedel @ amd . com >
2018-03-10 01:36:33 +03:00
*
* Copyright ( C ) 2009 Intel Corporation , Yu Zhao < yu . zhao @ intel . com >
* Copyright ( C ) 2011 Advanced Micro Devices ,
2011-09-27 17:57:13 +04:00
*/
2011-05-27 17:37:25 +04:00
# include <linux/export.h>
2011-09-27 17:57:13 +04:00
# include <linux/pci-ats.h>
# include <linux/pci.h>
2011-11-29 23:20:23 +04:00
# include <linux/slab.h>
2011-09-27 17:57:13 +04:00
# include "pci.h"
2015-07-17 23:35:18 +03:00
void pci_ats_init ( struct pci_dev * dev )
2011-09-27 17:57:13 +04:00
{
int pos ;
2018-05-11 01:56:02 +03:00
if ( pci_ats_disabled ( ) )
return ;
2011-09-27 17:57:13 +04:00
pos = pci_find_ext_capability ( dev , PCI_EXT_CAP_ID_ATS ) ;
if ( ! pos )
2015-07-17 23:05:46 +03:00
return ;
2011-09-27 17:57:13 +04:00
2015-07-17 23:15:19 +03:00
dev - > ats_cap = pos ;
2011-09-27 17:57:13 +04:00
}
/**
* pci_enable_ats - enable the ATS capability
* @ dev : the PCI device
* @ ps : the IOMMU page shift
*
* Returns 0 on success , or negative on failure .
*/
int pci_enable_ats ( struct pci_dev * dev , int ps )
{
u16 ctrl ;
2015-07-17 23:38:13 +03:00
struct pci_dev * pdev ;
2011-09-27 17:57:13 +04:00
2015-07-17 23:15:19 +03:00
if ( ! dev - > ats_cap )
2015-07-17 23:05:46 +03:00
return - EINVAL ;
2015-07-20 17:23:37 +03:00
if ( WARN_ON ( dev - > ats_enabled ) )
2015-07-17 23:43:27 +03:00
return - EBUSY ;
2011-09-27 17:57:13 +04:00
if ( ps < PCI_ATS_MIN_STU )
return - EINVAL ;
2015-07-17 23:05:46 +03:00
/*
* Note that enabling ATS on a VF fails unless it ' s already enabled
* with the same STU on the PF .
*/
ctrl = PCI_ATS_CTRL_ENABLE ;
if ( dev - > is_virtfn ) {
2015-07-17 23:38:13 +03:00
pdev = pci_physfn ( dev ) ;
2015-07-17 23:15:19 +03:00
if ( pdev - > ats_stu ! = ps )
2015-07-17 23:05:46 +03:00
return - EINVAL ;
2011-09-27 17:57:13 +04:00
2015-07-17 23:15:19 +03:00
atomic_inc ( & pdev - > ats_ref_cnt ) ; /* count enabled VFs */
2015-07-17 23:05:46 +03:00
} else {
2015-07-17 23:15:19 +03:00
dev - > ats_stu = ps ;
ctrl | = PCI_ATS_CTRL_STU ( dev - > ats_stu - PCI_ATS_MIN_STU ) ;
2011-09-27 17:57:13 +04:00
}
2015-07-17 23:15:19 +03:00
pci_write_config_word ( dev , dev - > ats_cap + PCI_ATS_CTRL , ctrl ) ;
2011-09-27 17:57:13 +04:00
2015-07-17 23:15:19 +03:00
dev - > ats_enabled = 1 ;
2011-09-27 17:57:13 +04:00
return 0 ;
}
2011-09-27 17:57:14 +04:00
EXPORT_SYMBOL_GPL ( pci_enable_ats ) ;
2011-09-27 17:57:13 +04:00
/**
* pci_disable_ats - disable the ATS capability
* @ dev : the PCI device
*/
void pci_disable_ats ( struct pci_dev * dev )
{
2015-07-17 23:38:13 +03:00
struct pci_dev * pdev ;
2011-09-27 17:57:13 +04:00
u16 ctrl ;
2015-07-20 17:23:37 +03:00
if ( WARN_ON ( ! dev - > ats_enabled ) )
2015-07-17 23:43:27 +03:00
return ;
2011-09-27 17:57:13 +04:00
2015-07-17 23:15:19 +03:00
if ( atomic_read ( & dev - > ats_ref_cnt ) )
2015-07-17 23:05:46 +03:00
return ; /* VFs still enabled */
if ( dev - > is_virtfn ) {
2015-07-17 23:38:13 +03:00
pdev = pci_physfn ( dev ) ;
2015-07-17 23:15:19 +03:00
atomic_dec ( & pdev - > ats_ref_cnt ) ;
2015-07-17 23:05:46 +03:00
}
2015-07-17 23:15:19 +03:00
pci_read_config_word ( dev , dev - > ats_cap + PCI_ATS_CTRL , & ctrl ) ;
2011-09-27 17:57:13 +04:00
ctrl & = ~ PCI_ATS_CTRL_ENABLE ;
2015-07-17 23:15:19 +03:00
pci_write_config_word ( dev , dev - > ats_cap + PCI_ATS_CTRL , ctrl ) ;
2011-09-27 17:57:13 +04:00
2015-07-17 23:15:19 +03:00
dev - > ats_enabled = 0 ;
2011-09-27 17:57:13 +04:00
}
2011-09-27 17:57:14 +04:00
EXPORT_SYMBOL_GPL ( pci_disable_ats ) ;
2011-09-27 17:57:13 +04:00
2011-12-17 17:24:40 +04:00
void pci_restore_ats_state ( struct pci_dev * dev )
{
u16 ctrl ;
2015-07-20 17:23:37 +03:00
if ( ! dev - > ats_enabled )
2011-12-17 17:24:40 +04:00
return ;
ctrl = PCI_ATS_CTRL_ENABLE ;
if ( ! dev - > is_virtfn )
2015-07-17 23:15:19 +03:00
ctrl | = PCI_ATS_CTRL_STU ( dev - > ats_stu - PCI_ATS_MIN_STU ) ;
pci_write_config_word ( dev , dev - > ats_cap + PCI_ATS_CTRL , ctrl ) ;
2011-12-17 17:24:40 +04:00
}
EXPORT_SYMBOL_GPL ( pci_restore_ats_state ) ;
2011-09-27 17:57:13 +04:00
/**
* pci_ats_queue_depth - query the ATS Invalidate Queue Depth
* @ dev : the PCI device
*
* Returns the queue depth on success , or negative on failure .
*
* The ATS spec uses 0 in the Invalidate Queue Depth field to
* indicate that the function can accept 32 Invalidate Request .
* But here we use the ` real ' values ( i . e . 1 ~ 32 ) for the Queue
* Depth ; and 0 indicates the function shares the Queue with
* other functions ( doesn ' t exclusively own a Queue ) .
*/
int pci_ats_queue_depth ( struct pci_dev * dev )
{
2015-07-20 17:24:32 +03:00
u16 cap ;
2015-07-17 23:30:26 +03:00
if ( ! dev - > ats_cap )
return - EINVAL ;
2011-09-27 17:57:13 +04:00
if ( dev - > is_virtfn )
return 0 ;
2015-07-20 17:24:32 +03:00
pci_read_config_word ( dev , dev - > ats_cap + PCI_ATS_CAP , & cap ) ;
return PCI_ATS_CAP_QDEP ( cap ) ? PCI_ATS_CAP_QDEP ( cap ) : PCI_ATS_MAX_QDEP ;
2011-09-27 17:57:13 +04:00
}
2011-09-27 17:57:14 +04:00
EXPORT_SYMBOL_GPL ( pci_ats_queue_depth ) ;
2011-09-27 17:57:15 +04:00
2019-02-19 22:06:09 +03:00
/**
* pci_ats_page_aligned - Return Page Aligned Request bit status .
* @ pdev : the PCI device
*
* Returns 1 , if the Untranslated Addresses generated by the device
* are always aligned or 0 otherwise .
*
* Per PCIe spec r4 .0 , sec 10.5 .1 .2 , if the Page Aligned Request bit
* is set , it indicates the Untranslated Addresses generated by the
* device are always aligned to a 4096 byte boundary .
*/
int pci_ats_page_aligned ( struct pci_dev * pdev )
{
u16 cap ;
if ( ! pdev - > ats_cap )
return 0 ;
pci_read_config_word ( pdev , pdev - > ats_cap + PCI_ATS_CAP , & cap ) ;
if ( cap & PCI_ATS_CAP_PAGE_ALIGNED )
return 1 ;
return 0 ;
}
EXPORT_SYMBOL_GPL ( pci_ats_page_aligned ) ;
2011-09-27 17:57:15 +04:00
# ifdef CONFIG_PCI_PRI
/**
* pci_enable_pri - Enable PRI capability
* @ pdev : PCI device structure
*
* Returns 0 on success , negative value on error
*/
int pci_enable_pri ( struct pci_dev * pdev , u32 reqs )
{
u16 control , status ;
u32 max_requests ;
int pos ;
2017-05-30 19:25:48 +03:00
if ( WARN_ON ( pdev - > pri_enabled ) )
return - EBUSY ;
2011-11-03 00:07:15 +04:00
pos = pci_find_ext_capability ( pdev , PCI_EXT_CAP_ID_PRI ) ;
2011-09-27 17:57:15 +04:00
if ( ! pos )
return - EINVAL ;
2011-11-11 21:07:36 +04:00
pci_read_config_word ( pdev , pos + PCI_PRI_STATUS , & status ) ;
2017-05-30 19:25:49 +03:00
if ( ! ( status & PCI_PRI_STATUS_STOPPED ) )
2011-09-27 17:57:15 +04:00
return - EBUSY ;
2011-11-11 21:07:36 +04:00
pci_read_config_dword ( pdev , pos + PCI_PRI_MAX_REQ , & max_requests ) ;
2011-09-27 17:57:15 +04:00
reqs = min ( max_requests , reqs ) ;
2017-05-30 19:25:49 +03:00
pdev - > pri_reqs_alloc = reqs ;
2011-11-11 21:07:36 +04:00
pci_write_config_dword ( pdev , pos + PCI_PRI_ALLOC_REQ , reqs ) ;
2011-09-27 17:57:15 +04:00
2017-05-30 19:25:49 +03:00
control = PCI_PRI_CTRL_ENABLE ;
2011-11-11 21:07:36 +04:00
pci_write_config_word ( pdev , pos + PCI_PRI_CTRL , control ) ;
2011-09-27 17:57:15 +04:00
2017-05-30 19:25:48 +03:00
pdev - > pri_enabled = 1 ;
2011-09-27 17:57:15 +04:00
return 0 ;
}
EXPORT_SYMBOL_GPL ( pci_enable_pri ) ;
/**
* pci_disable_pri - Disable PRI capability
* @ pdev : PCI device structure
*
* Only clears the enabled - bit , regardless of its former value
*/
void pci_disable_pri ( struct pci_dev * pdev )
{
u16 control ;
int pos ;
2017-05-30 19:25:48 +03:00
if ( WARN_ON ( ! pdev - > pri_enabled ) )
return ;
2011-11-03 00:07:15 +04:00
pos = pci_find_ext_capability ( pdev , PCI_EXT_CAP_ID_PRI ) ;
2011-09-27 17:57:15 +04:00
if ( ! pos )
return ;
2011-11-11 21:07:36 +04:00
pci_read_config_word ( pdev , pos + PCI_PRI_CTRL , & control ) ;
control & = ~ PCI_PRI_CTRL_ENABLE ;
pci_write_config_word ( pdev , pos + PCI_PRI_CTRL , control ) ;
2017-05-30 19:25:48 +03:00
pdev - > pri_enabled = 0 ;
2011-09-27 17:57:15 +04:00
}
EXPORT_SYMBOL_GPL ( pci_disable_pri ) ;
2017-05-30 19:25:49 +03:00
/**
* pci_restore_pri_state - Restore PRI
* @ pdev : PCI device structure
*/
void pci_restore_pri_state ( struct pci_dev * pdev )
{
u16 control = PCI_PRI_CTRL_ENABLE ;
u32 reqs = pdev - > pri_reqs_alloc ;
int pos ;
if ( ! pdev - > pri_enabled )
return ;
pos = pci_find_ext_capability ( pdev , PCI_EXT_CAP_ID_PRI ) ;
if ( ! pos )
return ;
pci_write_config_dword ( pdev , pos + PCI_PRI_ALLOC_REQ , reqs ) ;
pci_write_config_word ( pdev , pos + PCI_PRI_CTRL , control ) ;
}
EXPORT_SYMBOL_GPL ( pci_restore_pri_state ) ;
2011-09-27 17:57:15 +04:00
/**
* pci_reset_pri - Resets device ' s PRI state
* @ pdev : PCI device structure
*
* The PRI capability must be disabled before this function is called .
* Returns 0 on success , negative value on error .
*/
int pci_reset_pri ( struct pci_dev * pdev )
{
u16 control ;
int pos ;
2017-05-30 19:25:48 +03:00
if ( WARN_ON ( pdev - > pri_enabled ) )
return - EBUSY ;
2011-11-03 00:07:15 +04:00
pos = pci_find_ext_capability ( pdev , PCI_EXT_CAP_ID_PRI ) ;
2011-09-27 17:57:15 +04:00
if ( ! pos )
return - EINVAL ;
2017-05-30 19:25:49 +03:00
control = PCI_PRI_CTRL_RESET ;
2011-11-11 21:07:36 +04:00
pci_write_config_word ( pdev , pos + PCI_PRI_CTRL , control ) ;
2011-09-27 17:57:15 +04:00
return 0 ;
}
EXPORT_SYMBOL_GPL ( pci_reset_pri ) ;
# endif /* CONFIG_PCI_PRI */
2011-09-27 17:57:16 +04:00
# ifdef CONFIG_PCI_PASID
/**
* pci_enable_pasid - Enable the PASID capability
* @ pdev : PCI device structure
* @ features : Features to enable
*
* Returns 0 on success , negative value on error . This function checks
* whether the features are actually supported by the device and returns
* an error if not .
*/
int pci_enable_pasid ( struct pci_dev * pdev , int features )
{
u16 control , supported ;
int pos ;
2017-05-30 19:25:48 +03:00
if ( WARN_ON ( pdev - > pasid_enabled ) )
return - EBUSY ;
2018-06-30 18:24:24 +03:00
if ( ! pdev - > eetlp_prefix_path )
return - EINVAL ;
2011-11-03 00:07:15 +04:00
pos = pci_find_ext_capability ( pdev , PCI_EXT_CAP_ID_PASID ) ;
2011-09-27 17:57:16 +04:00
if ( ! pos )
return - EINVAL ;
2011-11-11 21:07:36 +04:00
pci_read_config_word ( pdev , pos + PCI_PASID_CAP , & supported ) ;
supported & = PCI_PASID_CAP_EXEC | PCI_PASID_CAP_PRIV ;
2011-09-27 17:57:16 +04:00
/* User wants to enable anything unsupported? */
if ( ( supported & features ) ! = features )
return - EINVAL ;
2011-11-11 21:07:36 +04:00
control = PCI_PASID_CTRL_ENABLE | features ;
2017-05-30 19:25:49 +03:00
pdev - > pasid_features = features ;
2011-09-27 17:57:16 +04:00
2011-11-11 21:07:36 +04:00
pci_write_config_word ( pdev , pos + PCI_PASID_CTRL , control ) ;
2011-09-27 17:57:16 +04:00
2017-05-30 19:25:48 +03:00
pdev - > pasid_enabled = 1 ;
2011-09-27 17:57:16 +04:00
return 0 ;
}
EXPORT_SYMBOL_GPL ( pci_enable_pasid ) ;
/**
* pci_disable_pasid - Disable the PASID capability
* @ pdev : PCI device structure
*/
void pci_disable_pasid ( struct pci_dev * pdev )
{
u16 control = 0 ;
int pos ;
2017-05-30 19:25:48 +03:00
if ( WARN_ON ( ! pdev - > pasid_enabled ) )
return ;
2011-11-03 00:07:15 +04:00
pos = pci_find_ext_capability ( pdev , PCI_EXT_CAP_ID_PASID ) ;
2011-09-27 17:57:16 +04:00
if ( ! pos )
return ;
2011-11-11 21:07:36 +04:00
pci_write_config_word ( pdev , pos + PCI_PASID_CTRL , control ) ;
2017-05-30 19:25:48 +03:00
pdev - > pasid_enabled = 0 ;
2011-09-27 17:57:16 +04:00
}
EXPORT_SYMBOL_GPL ( pci_disable_pasid ) ;
2017-05-30 19:25:49 +03:00
/**
* pci_restore_pasid_state - Restore PASID capabilities
* @ pdev : PCI device structure
*/
void pci_restore_pasid_state ( struct pci_dev * pdev )
{
u16 control ;
int pos ;
if ( ! pdev - > pasid_enabled )
return ;
pos = pci_find_ext_capability ( pdev , PCI_EXT_CAP_ID_PASID ) ;
if ( ! pos )
return ;
control = PCI_PASID_CTRL_ENABLE | pdev - > pasid_features ;
pci_write_config_word ( pdev , pos + PCI_PASID_CTRL , control ) ;
}
EXPORT_SYMBOL_GPL ( pci_restore_pasid_state ) ;
2011-09-27 17:57:16 +04:00
/**
* pci_pasid_features - Check which PASID features are supported
* @ pdev : PCI device structure
*
* Returns a negative value when no PASI capability is present .
* Otherwise is returns a bitmask with supported features . Current
* features reported are :
2011-11-11 21:07:36 +04:00
* PCI_PASID_CAP_EXEC - Execute permission supported
2013-11-14 22:28:18 +04:00
* PCI_PASID_CAP_PRIV - Privileged mode supported
2011-09-27 17:57:16 +04:00
*/
int pci_pasid_features ( struct pci_dev * pdev )
{
u16 supported ;
int pos ;
2011-11-03 00:07:15 +04:00
pos = pci_find_ext_capability ( pdev , PCI_EXT_CAP_ID_PASID ) ;
2011-09-27 17:57:16 +04:00
if ( ! pos )
return - EINVAL ;
2011-11-11 21:07:36 +04:00
pci_read_config_word ( pdev , pos + PCI_PASID_CAP , & supported ) ;
2011-09-27 17:57:16 +04:00
2011-11-11 21:07:36 +04:00
supported & = PCI_PASID_CAP_EXEC | PCI_PASID_CAP_PRIV ;
2011-09-27 17:57:16 +04:00
return supported ;
}
EXPORT_SYMBOL_GPL ( pci_pasid_features ) ;
2019-02-19 22:04:51 +03:00
/**
* pci_prg_resp_pasid_required - Return PRG Response PASID Required bit
* status .
* @ pdev : PCI device structure
*
* Returns 1 if PASID is required in PRG Response Message , 0 otherwise .
*
* Even though the PRG response PASID status is read from PRI Status
* Register , since this API will mainly be used by PASID users , this
* function is defined within # ifdef CONFIG_PCI_PASID instead of
* CONFIG_PCI_PRI .
*/
int pci_prg_resp_pasid_required ( struct pci_dev * pdev )
{
u16 status ;
int pos ;
pos = pci_find_ext_capability ( pdev , PCI_EXT_CAP_ID_PRI ) ;
if ( ! pos )
return 0 ;
pci_read_config_word ( pdev , pos + PCI_PRI_STATUS , & status ) ;
if ( status & PCI_PRI_STATUS_PASID )
return 1 ;
return 0 ;
}
EXPORT_SYMBOL_GPL ( pci_prg_resp_pasid_required ) ;
2011-09-27 17:57:16 +04:00
# define PASID_NUMBER_SHIFT 8
# define PASID_NUMBER_MASK (0x1f << PASID_NUMBER_SHIFT)
/**
* pci_max_pasid - Get maximum number of PASIDs supported by device
* @ pdev : PCI device structure
*
* Returns negative value when PASID capability is not present .
* Otherwise it returns the numer of supported PASIDs .
*/
int pci_max_pasids ( struct pci_dev * pdev )
{
u16 supported ;
int pos ;
2011-11-03 00:07:15 +04:00
pos = pci_find_ext_capability ( pdev , PCI_EXT_CAP_ID_PASID ) ;
2011-09-27 17:57:16 +04:00
if ( ! pos )
return - EINVAL ;
2011-11-11 21:07:36 +04:00
pci_read_config_word ( pdev , pos + PCI_PASID_CAP , & supported ) ;
2011-09-27 17:57:16 +04:00
supported = ( supported & PASID_NUMBER_MASK ) > > PASID_NUMBER_SHIFT ;
return ( 1 < < supported ) ;
}
EXPORT_SYMBOL_GPL ( pci_max_pasids ) ;
# endif /* CONFIG_PCI_PASID */