2018-01-27 00:45:16 +07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2011-09-27 20:57:13 +07:00
|
|
|
/*
|
2018-03-10 05:36:33 +07:00
|
|
|
* PCI Express I/O Virtualization (IOV) support
|
2011-09-27 20:57:13 +07:00
|
|
|
* Address Translation Service 1.0
|
2011-09-27 20:57:15 +07:00
|
|
|
* Page Request Interface added by Joerg Roedel <joerg.roedel@amd.com>
|
2011-09-27 20:57:16 +07:00
|
|
|
* PASID support added by Joerg Roedel <joerg.roedel@amd.com>
|
2018-03-10 05:36:33 +07:00
|
|
|
*
|
|
|
|
* Copyright (C) 2009 Intel Corporation, Yu Zhao <yu.zhao@intel.com>
|
|
|
|
* Copyright (C) 2011 Advanced Micro Devices,
|
2011-09-27 20:57:13 +07:00
|
|
|
*/
|
|
|
|
|
2011-05-27 20:37:25 +07:00
|
|
|
#include <linux/export.h>
|
2011-09-27 20:57:13 +07:00
|
|
|
#include <linux/pci-ats.h>
|
|
|
|
#include <linux/pci.h>
|
2011-11-30 02:20:23 +07:00
|
|
|
#include <linux/slab.h>
|
2011-09-27 20:57:13 +07:00
|
|
|
|
|
|
|
#include "pci.h"
|
|
|
|
|
2015-07-18 03:35:18 +07:00
|
|
|
void pci_ats_init(struct pci_dev *dev)
|
2011-09-27 20:57:13 +07:00
|
|
|
{
|
|
|
|
int pos;
|
|
|
|
|
2018-05-11 05:56:02 +07:00
|
|
|
if (pci_ats_disabled())
|
|
|
|
return;
|
|
|
|
|
2011-09-27 20:57:13 +07:00
|
|
|
pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ATS);
|
|
|
|
if (!pos)
|
2015-07-18 03:05:46 +07:00
|
|
|
return;
|
2011-09-27 20:57:13 +07:00
|
|
|
|
2015-07-18 03:15:19 +07:00
|
|
|
dev->ats_cap = pos;
|
2011-09-27 20:57:13 +07: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-18 03:38:13 +07:00
|
|
|
struct pci_dev *pdev;
|
2011-09-27 20:57:13 +07:00
|
|
|
|
2015-07-18 03:15:19 +07:00
|
|
|
if (!dev->ats_cap)
|
2015-07-18 03:05:46 +07:00
|
|
|
return -EINVAL;
|
|
|
|
|
2015-07-20 21:23:37 +07:00
|
|
|
if (WARN_ON(dev->ats_enabled))
|
2015-07-18 03:43:27 +07:00
|
|
|
return -EBUSY;
|
|
|
|
|
2011-09-27 20:57:13 +07:00
|
|
|
if (ps < PCI_ATS_MIN_STU)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2015-07-18 03:05:46 +07: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-18 03:38:13 +07:00
|
|
|
pdev = pci_physfn(dev);
|
2015-07-18 03:15:19 +07:00
|
|
|
if (pdev->ats_stu != ps)
|
2015-07-18 03:05:46 +07:00
|
|
|
return -EINVAL;
|
|
|
|
} else {
|
2015-07-18 03:15:19 +07:00
|
|
|
dev->ats_stu = ps;
|
|
|
|
ctrl |= PCI_ATS_CTRL_STU(dev->ats_stu - PCI_ATS_MIN_STU);
|
2011-09-27 20:57:13 +07:00
|
|
|
}
|
2015-07-18 03:15:19 +07:00
|
|
|
pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl);
|
2011-09-27 20:57:13 +07:00
|
|
|
|
2015-07-18 03:15:19 +07:00
|
|
|
dev->ats_enabled = 1;
|
2011-09-27 20:57:13 +07:00
|
|
|
return 0;
|
|
|
|
}
|
2019-12-19 19:03:39 +07:00
|
|
|
EXPORT_SYMBOL_GPL(pci_enable_ats);
|
2011-09-27 20:57:13 +07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* pci_disable_ats - disable the ATS capability
|
|
|
|
* @dev: the PCI device
|
|
|
|
*/
|
|
|
|
void pci_disable_ats(struct pci_dev *dev)
|
|
|
|
{
|
|
|
|
u16 ctrl;
|
|
|
|
|
2015-07-20 21:23:37 +07:00
|
|
|
if (WARN_ON(!dev->ats_enabled))
|
2015-07-18 03:43:27 +07:00
|
|
|
return;
|
2011-09-27 20:57:13 +07:00
|
|
|
|
2015-07-18 03:15:19 +07:00
|
|
|
pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, &ctrl);
|
2011-09-27 20:57:13 +07:00
|
|
|
ctrl &= ~PCI_ATS_CTRL_ENABLE;
|
2015-07-18 03:15:19 +07:00
|
|
|
pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl);
|
2011-09-27 20:57:13 +07:00
|
|
|
|
2015-07-18 03:15:19 +07:00
|
|
|
dev->ats_enabled = 0;
|
2011-09-27 20:57:13 +07:00
|
|
|
}
|
2019-12-19 19:03:39 +07:00
|
|
|
EXPORT_SYMBOL_GPL(pci_disable_ats);
|
2011-09-27 20:57:13 +07:00
|
|
|
|
2011-12-17 20:24:40 +07:00
|
|
|
void pci_restore_ats_state(struct pci_dev *dev)
|
|
|
|
{
|
|
|
|
u16 ctrl;
|
|
|
|
|
2015-07-20 21:23:37 +07:00
|
|
|
if (!dev->ats_enabled)
|
2011-12-17 20:24:40 +07:00
|
|
|
return;
|
|
|
|
|
|
|
|
ctrl = PCI_ATS_CTRL_ENABLE;
|
|
|
|
if (!dev->is_virtfn)
|
2015-07-18 03:15:19 +07: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 20:24:40 +07:00
|
|
|
}
|
|
|
|
|
2011-09-27 20:57:13 +07: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 21:24:32 +07:00
|
|
|
u16 cap;
|
|
|
|
|
2015-07-18 03:30:26 +07:00
|
|
|
if (!dev->ats_cap)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2011-09-27 20:57:13 +07:00
|
|
|
if (dev->is_virtfn)
|
|
|
|
return 0;
|
|
|
|
|
2015-07-20 21:24:32 +07: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 20:57:13 +07:00
|
|
|
}
|
2011-09-27 20:57:15 +07:00
|
|
|
|
2019-02-20 02:06:09 +07: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;
|
|
|
|
}
|
|
|
|
|
2011-09-27 20:57:15 +07:00
|
|
|
#ifdef CONFIG_PCI_PRI
|
2019-09-06 02:31:45 +07:00
|
|
|
void pci_pri_init(struct pci_dev *pdev)
|
|
|
|
{
|
2019-10-10 04:07:51 +07:00
|
|
|
u16 status;
|
|
|
|
|
2019-09-06 02:31:45 +07:00
|
|
|
pdev->pri_cap = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
|
2019-10-10 04:07:51 +07:00
|
|
|
|
|
|
|
if (!pdev->pri_cap)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pci_read_config_word(pdev, pdev->pri_cap + PCI_PRI_STATUS, &status);
|
|
|
|
if (status & PCI_PRI_STATUS_PASID)
|
|
|
|
pdev->pasid_required = 1;
|
2019-09-06 02:31:45 +07:00
|
|
|
}
|
|
|
|
|
2011-09-27 20:57:15 +07:00
|
|
|
/**
|
|
|
|
* 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;
|
2019-09-06 02:31:45 +07:00
|
|
|
int pri = pdev->pri_cap;
|
2011-09-27 20:57:15 +07:00
|
|
|
|
2019-09-06 02:31:42 +07:00
|
|
|
/*
|
|
|
|
* VFs must not implement the PRI Capability. If their PF
|
|
|
|
* implements PRI, it is shared by the VFs, so if the PF PRI is
|
|
|
|
* enabled, it is also enabled for the VF.
|
|
|
|
*/
|
|
|
|
if (pdev->is_virtfn) {
|
|
|
|
if (pci_physfn(pdev)->pri_enabled)
|
|
|
|
return 0;
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2017-05-30 23:25:48 +07:00
|
|
|
if (WARN_ON(pdev->pri_enabled))
|
|
|
|
return -EBUSY;
|
|
|
|
|
2019-09-06 02:31:45 +07:00
|
|
|
if (!pri)
|
2011-09-27 20:57:15 +07:00
|
|
|
return -EINVAL;
|
|
|
|
|
2019-09-06 02:31:45 +07:00
|
|
|
pci_read_config_word(pdev, pri + PCI_PRI_STATUS, &status);
|
2017-05-30 23:25:49 +07:00
|
|
|
if (!(status & PCI_PRI_STATUS_STOPPED))
|
2011-09-27 20:57:15 +07:00
|
|
|
return -EBUSY;
|
|
|
|
|
2019-09-06 02:31:45 +07:00
|
|
|
pci_read_config_dword(pdev, pri + PCI_PRI_MAX_REQ, &max_requests);
|
2011-09-27 20:57:15 +07:00
|
|
|
reqs = min(max_requests, reqs);
|
2017-05-30 23:25:49 +07:00
|
|
|
pdev->pri_reqs_alloc = reqs;
|
2019-09-06 02:31:45 +07:00
|
|
|
pci_write_config_dword(pdev, pri + PCI_PRI_ALLOC_REQ, reqs);
|
2011-09-27 20:57:15 +07:00
|
|
|
|
2017-05-30 23:25:49 +07:00
|
|
|
control = PCI_PRI_CTRL_ENABLE;
|
2019-09-06 02:31:45 +07:00
|
|
|
pci_write_config_word(pdev, pri + PCI_PRI_CTRL, control);
|
2011-09-27 20:57:15 +07:00
|
|
|
|
2017-05-30 23:25:48 +07:00
|
|
|
pdev->pri_enabled = 1;
|
|
|
|
|
2011-09-27 20:57:15 +07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2019-09-06 02:31:45 +07:00
|
|
|
int pri = pdev->pri_cap;
|
2011-09-27 20:57:15 +07:00
|
|
|
|
2019-09-06 02:31:42 +07:00
|
|
|
/* VFs share the PF PRI */
|
|
|
|
if (pdev->is_virtfn)
|
|
|
|
return;
|
|
|
|
|
2017-05-30 23:25:48 +07:00
|
|
|
if (WARN_ON(!pdev->pri_enabled))
|
|
|
|
return;
|
|
|
|
|
2019-09-06 02:31:45 +07:00
|
|
|
if (!pri)
|
2011-09-27 20:57:15 +07:00
|
|
|
return;
|
|
|
|
|
2019-09-06 02:31:45 +07:00
|
|
|
pci_read_config_word(pdev, pri + PCI_PRI_CTRL, &control);
|
2011-11-12 00:07:36 +07:00
|
|
|
control &= ~PCI_PRI_CTRL_ENABLE;
|
2019-09-06 02:31:45 +07:00
|
|
|
pci_write_config_word(pdev, pri + PCI_PRI_CTRL, control);
|
2017-05-30 23:25:48 +07:00
|
|
|
|
|
|
|
pdev->pri_enabled = 0;
|
2011-09-27 20:57:15 +07:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(pci_disable_pri);
|
|
|
|
|
2017-05-30 23:25:49 +07: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;
|
2019-09-06 02:31:45 +07:00
|
|
|
int pri = pdev->pri_cap;
|
2017-05-30 23:25:49 +07:00
|
|
|
|
2019-09-06 02:31:42 +07:00
|
|
|
if (pdev->is_virtfn)
|
|
|
|
return;
|
|
|
|
|
2017-05-30 23:25:49 +07:00
|
|
|
if (!pdev->pri_enabled)
|
|
|
|
return;
|
|
|
|
|
2019-09-06 02:31:45 +07:00
|
|
|
if (!pri)
|
2017-05-30 23:25:49 +07:00
|
|
|
return;
|
|
|
|
|
2019-09-06 02:31:45 +07:00
|
|
|
pci_write_config_dword(pdev, pri + PCI_PRI_ALLOC_REQ, reqs);
|
|
|
|
pci_write_config_word(pdev, pri + PCI_PRI_CTRL, control);
|
2017-05-30 23:25:49 +07:00
|
|
|
}
|
|
|
|
|
2011-09-27 20:57:15 +07: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;
|
2019-09-06 02:31:45 +07:00
|
|
|
int pri = pdev->pri_cap;
|
2011-09-27 20:57:15 +07:00
|
|
|
|
2019-09-06 02:31:42 +07:00
|
|
|
if (pdev->is_virtfn)
|
|
|
|
return 0;
|
|
|
|
|
2017-05-30 23:25:48 +07:00
|
|
|
if (WARN_ON(pdev->pri_enabled))
|
|
|
|
return -EBUSY;
|
|
|
|
|
2019-09-06 02:31:45 +07:00
|
|
|
if (!pri)
|
2011-09-27 20:57:15 +07:00
|
|
|
return -EINVAL;
|
|
|
|
|
2017-05-30 23:25:49 +07:00
|
|
|
control = PCI_PRI_CTRL_RESET;
|
2019-09-06 02:31:45 +07:00
|
|
|
pci_write_config_word(pdev, pri + PCI_PRI_CTRL, control);
|
2011-09-27 20:57:15 +07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2019-10-10 02:54:01 +07: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.
|
|
|
|
*/
|
|
|
|
int pci_prg_resp_pasid_required(struct pci_dev *pdev)
|
|
|
|
{
|
2019-09-06 02:31:42 +07:00
|
|
|
if (pdev->is_virtfn)
|
|
|
|
pdev = pci_physfn(pdev);
|
|
|
|
|
2019-10-10 04:07:51 +07:00
|
|
|
return pdev->pasid_required;
|
2019-10-10 02:54:01 +07:00
|
|
|
}
|
2011-09-27 20:57:15 +07:00
|
|
|
#endif /* CONFIG_PCI_PRI */
|
2011-09-27 20:57:16 +07:00
|
|
|
|
|
|
|
#ifdef CONFIG_PCI_PASID
|
2019-09-06 02:31:46 +07:00
|
|
|
void pci_pasid_init(struct pci_dev *pdev)
|
|
|
|
{
|
|
|
|
pdev->pasid_cap = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
|
|
|
|
}
|
|
|
|
|
2011-09-27 20:57:16 +07:00
|
|
|
/**
|
|
|
|
* 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;
|
2019-09-06 02:31:46 +07:00
|
|
|
int pasid = pdev->pasid_cap;
|
2011-09-27 20:57:16 +07:00
|
|
|
|
2019-09-06 02:31:43 +07:00
|
|
|
/*
|
|
|
|
* VFs must not implement the PASID Capability, but if a PF
|
|
|
|
* supports PASID, its VFs share the PF PASID configuration.
|
|
|
|
*/
|
|
|
|
if (pdev->is_virtfn) {
|
|
|
|
if (pci_physfn(pdev)->pasid_enabled)
|
|
|
|
return 0;
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2017-05-30 23:25:48 +07:00
|
|
|
if (WARN_ON(pdev->pasid_enabled))
|
|
|
|
return -EBUSY;
|
|
|
|
|
2018-06-30 22:24:24 +07:00
|
|
|
if (!pdev->eetlp_prefix_path)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2019-09-06 02:31:46 +07:00
|
|
|
if (!pasid)
|
2011-09-27 20:57:16 +07:00
|
|
|
return -EINVAL;
|
|
|
|
|
2019-09-06 02:31:46 +07:00
|
|
|
pci_read_config_word(pdev, pasid + PCI_PASID_CAP, &supported);
|
2011-11-12 00:07:36 +07:00
|
|
|
supported &= PCI_PASID_CAP_EXEC | PCI_PASID_CAP_PRIV;
|
2011-09-27 20:57:16 +07:00
|
|
|
|
|
|
|
/* User wants to enable anything unsupported? */
|
|
|
|
if ((supported & features) != features)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2011-11-12 00:07:36 +07:00
|
|
|
control = PCI_PASID_CTRL_ENABLE | features;
|
2017-05-30 23:25:49 +07:00
|
|
|
pdev->pasid_features = features;
|
2011-09-27 20:57:16 +07:00
|
|
|
|
2019-09-06 02:31:46 +07:00
|
|
|
pci_write_config_word(pdev, pasid + PCI_PASID_CTRL, control);
|
2011-09-27 20:57:16 +07:00
|
|
|
|
2017-05-30 23:25:48 +07:00
|
|
|
pdev->pasid_enabled = 1;
|
|
|
|
|
2011-09-27 20:57:16 +07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* pci_disable_pasid - Disable the PASID capability
|
|
|
|
* @pdev: PCI device structure
|
|
|
|
*/
|
|
|
|
void pci_disable_pasid(struct pci_dev *pdev)
|
|
|
|
{
|
|
|
|
u16 control = 0;
|
2019-09-06 02:31:46 +07:00
|
|
|
int pasid = pdev->pasid_cap;
|
2011-09-27 20:57:16 +07:00
|
|
|
|
2019-09-06 02:31:43 +07:00
|
|
|
/* VFs share the PF PASID configuration */
|
|
|
|
if (pdev->is_virtfn)
|
|
|
|
return;
|
|
|
|
|
2017-05-30 23:25:48 +07:00
|
|
|
if (WARN_ON(!pdev->pasid_enabled))
|
|
|
|
return;
|
|
|
|
|
2019-09-06 02:31:46 +07:00
|
|
|
if (!pasid)
|
2011-09-27 20:57:16 +07:00
|
|
|
return;
|
|
|
|
|
2019-09-06 02:31:46 +07:00
|
|
|
pci_write_config_word(pdev, pasid + PCI_PASID_CTRL, control);
|
2017-05-30 23:25:48 +07:00
|
|
|
|
|
|
|
pdev->pasid_enabled = 0;
|
2011-09-27 20:57:16 +07:00
|
|
|
}
|
|
|
|
|
2017-05-30 23:25:49 +07:00
|
|
|
/**
|
|
|
|
* pci_restore_pasid_state - Restore PASID capabilities
|
|
|
|
* @pdev: PCI device structure
|
|
|
|
*/
|
|
|
|
void pci_restore_pasid_state(struct pci_dev *pdev)
|
|
|
|
{
|
|
|
|
u16 control;
|
2019-09-06 02:31:46 +07:00
|
|
|
int pasid = pdev->pasid_cap;
|
2017-05-30 23:25:49 +07:00
|
|
|
|
2019-09-06 02:31:43 +07:00
|
|
|
if (pdev->is_virtfn)
|
|
|
|
return;
|
|
|
|
|
2017-05-30 23:25:49 +07:00
|
|
|
if (!pdev->pasid_enabled)
|
|
|
|
return;
|
|
|
|
|
2019-09-06 02:31:46 +07:00
|
|
|
if (!pasid)
|
2017-05-30 23:25:49 +07:00
|
|
|
return;
|
|
|
|
|
|
|
|
control = PCI_PASID_CTRL_ENABLE | pdev->pasid_features;
|
2019-09-06 02:31:46 +07:00
|
|
|
pci_write_config_word(pdev, pasid + PCI_PASID_CTRL, control);
|
2017-05-30 23:25:49 +07:00
|
|
|
}
|
|
|
|
|
2011-09-27 20:57:16 +07: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-12 00:07:36 +07:00
|
|
|
* PCI_PASID_CAP_EXEC - Execute permission supported
|
2013-11-15 01:28:18 +07:00
|
|
|
* PCI_PASID_CAP_PRIV - Privileged mode supported
|
2011-09-27 20:57:16 +07:00
|
|
|
*/
|
|
|
|
int pci_pasid_features(struct pci_dev *pdev)
|
|
|
|
{
|
|
|
|
u16 supported;
|
2020-01-30 02:14:00 +07:00
|
|
|
int pasid;
|
2011-09-27 20:57:16 +07:00
|
|
|
|
2019-09-06 02:31:43 +07:00
|
|
|
if (pdev->is_virtfn)
|
|
|
|
pdev = pci_physfn(pdev);
|
|
|
|
|
2020-01-30 02:14:00 +07:00
|
|
|
pasid = pdev->pasid_cap;
|
2019-09-06 02:31:46 +07:00
|
|
|
if (!pasid)
|
2011-09-27 20:57:16 +07:00
|
|
|
return -EINVAL;
|
|
|
|
|
2019-09-06 02:31:46 +07:00
|
|
|
pci_read_config_word(pdev, pasid + PCI_PASID_CAP, &supported);
|
2011-09-27 20:57:16 +07:00
|
|
|
|
2011-11-12 00:07:36 +07:00
|
|
|
supported &= PCI_PASID_CAP_EXEC | PCI_PASID_CAP_PRIV;
|
2011-09-27 20:57:16 +07:00
|
|
|
|
|
|
|
return supported;
|
|
|
|
}
|
|
|
|
|
|
|
|
#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.
|
2019-05-30 20:05:58 +07:00
|
|
|
* Otherwise it returns the number of supported PASIDs.
|
2011-09-27 20:57:16 +07:00
|
|
|
*/
|
|
|
|
int pci_max_pasids(struct pci_dev *pdev)
|
|
|
|
{
|
|
|
|
u16 supported;
|
2020-01-30 02:14:00 +07:00
|
|
|
int pasid;
|
2011-09-27 20:57:16 +07:00
|
|
|
|
2019-09-06 02:31:43 +07:00
|
|
|
if (pdev->is_virtfn)
|
|
|
|
pdev = pci_physfn(pdev);
|
|
|
|
|
2020-01-30 02:14:00 +07:00
|
|
|
pasid = pdev->pasid_cap;
|
2019-09-06 02:31:46 +07:00
|
|
|
if (!pasid)
|
2011-09-27 20:57:16 +07:00
|
|
|
return -EINVAL;
|
|
|
|
|
2019-09-06 02:31:46 +07:00
|
|
|
pci_read_config_word(pdev, pasid + PCI_PASID_CAP, &supported);
|
2011-09-27 20:57:16 +07:00
|
|
|
|
|
|
|
supported = (supported & PASID_NUMBER_MASK) >> PASID_NUMBER_SHIFT;
|
|
|
|
|
|
|
|
return (1 << supported);
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_PCI_PASID */
|