Merge branch 'pci/aspm'

- Remove unnecessary ASPM locking (Bjorn Helgaas)

  - Add support for disabling L1 PM Substates (Heiner Kallweit)

  - Allow re-enabling Clock PM after it has been disabled (Heiner Kallweit)

  - Add sysfs attributes for controlling ASPM link states (Heiner Kallweit)

  - Remove CONFIG_PCIEASPM_DEBUG, including "link_state" and "clk_ctl"
    sysfs files (Heiner Kallweit)

* pci/aspm:
  PCI/ASPM: Remove PCIEASPM_DEBUG Kconfig option and related code
  PCI/ASPM: Add sysfs attributes for controlling ASPM link states
  PCI/ASPM: Add pcie_aspm_get_link()
  PCI/ASPM: Allow re-enabling Clock PM
  PCI/ASPM: Add L1 PM substate support to pci_disable_link_state()
  PCI/ASPM: Remove pcie_aspm_enabled() unnecessary locking
This commit is contained in:
Bjorn Helgaas 2019-11-28 08:54:29 -06:00
commit abd05c97f9
6 changed files with 186 additions and 117 deletions

View File

@ -347,3 +347,16 @@ Description:
If the device has any Peer-to-Peer memory registered, this If the device has any Peer-to-Peer memory registered, this
file contains a '1' if the memory has been published for file contains a '1' if the memory has been published for
use outside the driver that owns the device. use outside the driver that owns the device.
What: /sys/bus/pci/devices/.../link/clkpm
/sys/bus/pci/devices/.../link/l0s_aspm
/sys/bus/pci/devices/.../link/l1_aspm
/sys/bus/pci/devices/.../link/l1_1_aspm
/sys/bus/pci/devices/.../link/l1_2_aspm
/sys/bus/pci/devices/.../link/l1_1_pcipm
/sys/bus/pci/devices/.../link/l1_2_pcipm
Date: October 2019
Contact: Heiner Kallweit <hkallweit1@gmail.com>
Description: If ASPM is supported for an endpoint, these files can be
used to disable or enable the individual power management
states. Write y/1/on to enable, n/0/off to disable.

View File

@ -1330,7 +1330,6 @@ static int pci_create_capabilities_sysfs(struct pci_dev *dev)
int retval; int retval;
pcie_vpd_create_sysfs_dev_files(dev); pcie_vpd_create_sysfs_dev_files(dev);
pcie_aspm_create_sysfs_dev_files(dev);
if (dev->reset_fn) { if (dev->reset_fn) {
retval = device_create_file(&dev->dev, &dev_attr_reset); retval = device_create_file(&dev->dev, &dev_attr_reset);
@ -1340,7 +1339,6 @@ static int pci_create_capabilities_sysfs(struct pci_dev *dev)
return 0; return 0;
error: error:
pcie_aspm_remove_sysfs_dev_files(dev);
pcie_vpd_remove_sysfs_dev_files(dev); pcie_vpd_remove_sysfs_dev_files(dev);
return retval; return retval;
} }
@ -1416,7 +1414,6 @@ err:
static void pci_remove_capabilities_sysfs(struct pci_dev *dev) static void pci_remove_capabilities_sysfs(struct pci_dev *dev)
{ {
pcie_vpd_remove_sysfs_dev_files(dev); pcie_vpd_remove_sysfs_dev_files(dev);
pcie_aspm_remove_sysfs_dev_files(dev);
if (dev->reset_fn) { if (dev->reset_fn) {
device_remove_file(&dev->dev, &dev_attr_reset); device_remove_file(&dev->dev, &dev_attr_reset);
dev->reset_fn = 0; dev->reset_fn = 0;
@ -1587,6 +1584,9 @@ static const struct attribute_group *pci_dev_attr_groups[] = {
&pcie_dev_attr_group, &pcie_dev_attr_group,
#ifdef CONFIG_PCIEAER #ifdef CONFIG_PCIEAER
&aer_stats_attr_group, &aer_stats_attr_group,
#endif
#ifdef CONFIG_PCIEASPM
&aspm_ctrl_attr_group,
#endif #endif
NULL, NULL,
}; };

View File

@ -542,14 +542,6 @@ static inline void pcie_aspm_pm_state_change(struct pci_dev *pdev) { }
static inline void pcie_aspm_powersave_config_link(struct pci_dev *pdev) { } static inline void pcie_aspm_powersave_config_link(struct pci_dev *pdev) { }
#endif #endif
#ifdef CONFIG_PCIEASPM_DEBUG
void pcie_aspm_create_sysfs_dev_files(struct pci_dev *pdev);
void pcie_aspm_remove_sysfs_dev_files(struct pci_dev *pdev);
#else
static inline void pcie_aspm_create_sysfs_dev_files(struct pci_dev *pdev) { }
static inline void pcie_aspm_remove_sysfs_dev_files(struct pci_dev *pdev) { }
#endif
#ifdef CONFIG_PCIE_ECRC #ifdef CONFIG_PCIE_ECRC
void pcie_set_ecrc_checking(struct pci_dev *dev); void pcie_set_ecrc_checking(struct pci_dev *dev);
void pcie_ecrc_get_policy(char *str); void pcie_ecrc_get_policy(char *str);
@ -668,4 +660,8 @@ static inline int pci_acpi_program_hp_params(struct pci_dev *dev)
} }
#endif #endif
#ifdef CONFIG_PCIEASPM
extern const struct attribute_group aspm_ctrl_attr_group;
#endif
#endif /* DRIVERS_PCI_H */ #endif /* DRIVERS_PCI_H */

View File

@ -79,13 +79,6 @@ config PCIEASPM
When in doubt, say Y. When in doubt, say Y.
config PCIEASPM_DEBUG
bool "Debug PCI Express ASPM"
depends on PCIEASPM
help
This enables PCI Express ASPM debug support. It will add per-device
interface to control ASPM.
choice choice
prompt "Default ASPM policy" prompt "Default ASPM policy"
default PCIEASPM_DEFAULT default PCIEASPM_DEFAULT

View File

@ -64,6 +64,7 @@ struct pcie_link_state {
u32 clkpm_capable:1; /* Clock PM capable? */ u32 clkpm_capable:1; /* Clock PM capable? */
u32 clkpm_enabled:1; /* Current Clock PM state */ u32 clkpm_enabled:1; /* Current Clock PM state */
u32 clkpm_default:1; /* Default Clock PM state by BIOS */ u32 clkpm_default:1; /* Default Clock PM state by BIOS */
u32 clkpm_disable:1; /* Clock PM disabled */
/* Exit latencies */ /* Exit latencies */
struct aspm_latency latency_up; /* Upstream direction exit latency */ struct aspm_latency latency_up; /* Upstream direction exit latency */
@ -161,8 +162,11 @@ static void pcie_set_clkpm_nocheck(struct pcie_link_state *link, int enable)
static void pcie_set_clkpm(struct pcie_link_state *link, int enable) static void pcie_set_clkpm(struct pcie_link_state *link, int enable)
{ {
/* Don't enable Clock PM if the link is not Clock PM capable */ /*
if (!link->clkpm_capable) * Don't enable Clock PM if the link is not Clock PM capable
* or Clock PM is disabled
*/
if (!link->clkpm_capable || link->clkpm_disable)
enable = 0; enable = 0;
/* Need nothing if the specified equals to current state */ /* Need nothing if the specified equals to current state */
if (link->clkpm_enabled == enable) if (link->clkpm_enabled == enable)
@ -192,7 +196,8 @@ static void pcie_clkpm_cap_init(struct pcie_link_state *link, int blacklist)
} }
link->clkpm_enabled = enabled; link->clkpm_enabled = enabled;
link->clkpm_default = enabled; link->clkpm_default = enabled;
link->clkpm_capable = (blacklist) ? 0 : capable; link->clkpm_capable = capable;
link->clkpm_disable = blacklist ? 1 : 0;
} }
static bool pcie_retrain_link(struct pcie_link_state *link) static bool pcie_retrain_link(struct pcie_link_state *link)
@ -894,6 +899,14 @@ static struct pcie_link_state *alloc_pcie_link_state(struct pci_dev *pdev)
return link; return link;
} }
static void pcie_aspm_update_sysfs_visibility(struct pci_dev *pdev)
{
struct pci_dev *child;
list_for_each_entry(child, &pdev->subordinate->devices, bus_list)
sysfs_update_group(&child->dev.kobj, &aspm_ctrl_attr_group);
}
/* /*
* pcie_aspm_init_link_state: Initiate PCI express link state. * pcie_aspm_init_link_state: Initiate PCI express link state.
* It is called after the pcie and its children devices are scanned. * It is called after the pcie and its children devices are scanned.
@ -955,6 +968,8 @@ void pcie_aspm_init_link_state(struct pci_dev *pdev)
pcie_set_clkpm(link, policy_to_clkpm_state(link)); pcie_set_clkpm(link, policy_to_clkpm_state(link));
} }
pcie_aspm_update_sysfs_visibility(pdev);
unlock: unlock:
mutex_unlock(&aspm_lock); mutex_unlock(&aspm_lock);
out: out:
@ -1061,19 +1076,26 @@ void pcie_aspm_powersave_config_link(struct pci_dev *pdev)
up_read(&pci_bus_sem); up_read(&pci_bus_sem);
} }
static int __pci_disable_link_state(struct pci_dev *pdev, int state, bool sem) static struct pcie_link_state *pcie_aspm_get_link(struct pci_dev *pdev)
{ {
struct pci_dev *parent = pdev->bus->self; struct pci_dev *bridge;
struct pcie_link_state *link;
if (!pci_is_pcie(pdev)) if (!pci_is_pcie(pdev))
return 0; return NULL;
if (pcie_downstream_port(pdev)) bridge = pci_upstream_bridge(pdev);
parent = pdev; if (!bridge || !pci_is_pcie(bridge))
if (!parent || !parent->link_state) return NULL;
return bridge->link_state;
}
static int __pci_disable_link_state(struct pci_dev *pdev, int state, bool sem)
{
struct pcie_link_state *link = pcie_aspm_get_link(pdev);
if (!link)
return -EINVAL; return -EINVAL;
/* /*
* A driver requested that ASPM be disabled on this device, but * A driver requested that ASPM be disabled on this device, but
* if we don't have permission to manage ASPM (e.g., on ACPI * if we don't have permission to manage ASPM (e.g., on ACPI
@ -1090,17 +1112,24 @@ static int __pci_disable_link_state(struct pci_dev *pdev, int state, bool sem)
if (sem) if (sem)
down_read(&pci_bus_sem); down_read(&pci_bus_sem);
mutex_lock(&aspm_lock); mutex_lock(&aspm_lock);
link = parent->link_state;
if (state & PCIE_LINK_STATE_L0S) if (state & PCIE_LINK_STATE_L0S)
link->aspm_disable |= ASPM_STATE_L0S; link->aspm_disable |= ASPM_STATE_L0S;
if (state & PCIE_LINK_STATE_L1) if (state & PCIE_LINK_STATE_L1)
link->aspm_disable |= ASPM_STATE_L1; /* L1 PM substates require L1 */
link->aspm_disable |= ASPM_STATE_L1 | ASPM_STATE_L1SS;
if (state & PCIE_LINK_STATE_L1_1)
link->aspm_disable |= ASPM_STATE_L1_1;
if (state & PCIE_LINK_STATE_L1_2)
link->aspm_disable |= ASPM_STATE_L1_2;
if (state & PCIE_LINK_STATE_L1_1_PCIPM)
link->aspm_disable |= ASPM_STATE_L1_1_PCIPM;
if (state & PCIE_LINK_STATE_L1_2_PCIPM)
link->aspm_disable |= ASPM_STATE_L1_2_PCIPM;
pcie_config_aspm_link(link, policy_to_aspm_state(link)); pcie_config_aspm_link(link, policy_to_aspm_state(link));
if (state & PCIE_LINK_STATE_CLKPM) { if (state & PCIE_LINK_STATE_CLKPM)
link->clkpm_capable = 0; link->clkpm_disable = 1;
pcie_set_clkpm(link, 0); pcie_set_clkpm(link, policy_to_clkpm_state(link));
}
mutex_unlock(&aspm_lock); mutex_unlock(&aspm_lock);
if (sem) if (sem)
up_read(&pci_bus_sem); up_read(&pci_bus_sem);
@ -1172,127 +1201,161 @@ module_param_call(policy, pcie_aspm_set_policy, pcie_aspm_get_policy,
/** /**
* pcie_aspm_enabled - Check if PCIe ASPM has been enabled for a device. * pcie_aspm_enabled - Check if PCIe ASPM has been enabled for a device.
* @pdev: Target device. * @pdev: Target device.
*
* Relies on the upstream bridge's link_state being valid. The link_state
* is deallocated only when the last child of the bridge (i.e., @pdev or a
* sibling) is removed, and the caller should be holding a reference to
* @pdev, so this should be safe.
*/ */
bool pcie_aspm_enabled(struct pci_dev *pdev) bool pcie_aspm_enabled(struct pci_dev *pdev)
{ {
struct pci_dev *bridge = pci_upstream_bridge(pdev); struct pcie_link_state *link = pcie_aspm_get_link(pdev);
bool ret;
if (!bridge) if (!link)
return false; return false;
mutex_lock(&aspm_lock); return link->aspm_enabled;
ret = bridge->link_state ? !!bridge->link_state->aspm_enabled : false;
mutex_unlock(&aspm_lock);
return ret;
} }
EXPORT_SYMBOL_GPL(pcie_aspm_enabled); EXPORT_SYMBOL_GPL(pcie_aspm_enabled);
#ifdef CONFIG_PCIEASPM_DEBUG static ssize_t aspm_attr_show_common(struct device *dev,
static ssize_t link_state_show(struct device *dev,
struct device_attribute *attr, struct device_attribute *attr,
char *buf) char *buf, u8 state)
{
struct pci_dev *pci_device = to_pci_dev(dev);
struct pcie_link_state *link_state = pci_device->link_state;
return sprintf(buf, "%d\n", link_state->aspm_enabled);
}
static ssize_t link_state_store(struct device *dev,
struct device_attribute *attr,
const char *buf,
size_t n)
{ {
struct pci_dev *pdev = to_pci_dev(dev); struct pci_dev *pdev = to_pci_dev(dev);
struct pcie_link_state *link, *root = pdev->link_state->root; struct pcie_link_state *link = pcie_aspm_get_link(pdev);
u32 state;
if (aspm_disabled) return sprintf(buf, "%d\n", (link->aspm_enabled & state) ? 1 : 0);
return -EPERM; }
if (kstrtouint(buf, 10, &state)) static ssize_t aspm_attr_store_common(struct device *dev,
return -EINVAL; struct device_attribute *attr,
if ((state & ~ASPM_STATE_ALL) != 0) const char *buf, size_t len, u8 state)
{
struct pci_dev *pdev = to_pci_dev(dev);
struct pcie_link_state *link = pcie_aspm_get_link(pdev);
bool state_enable;
if (strtobool(buf, &state_enable) < 0)
return -EINVAL; return -EINVAL;
down_read(&pci_bus_sem); down_read(&pci_bus_sem);
mutex_lock(&aspm_lock); mutex_lock(&aspm_lock);
list_for_each_entry(link, &link_list, sibling) {
if (link->root != root) if (state_enable) {
continue; link->aspm_disable &= ~state;
pcie_config_aspm_link(link, state); /* need to enable L1 for substates */
if (state & ASPM_STATE_L1SS)
link->aspm_disable &= ~ASPM_STATE_L1;
} else {
link->aspm_disable |= state;
} }
pcie_config_aspm_link(link, policy_to_aspm_state(link));
mutex_unlock(&aspm_lock); mutex_unlock(&aspm_lock);
up_read(&pci_bus_sem); up_read(&pci_bus_sem);
return n;
return len;
} }
static ssize_t clk_ctl_show(struct device *dev, #define ASPM_ATTR(_f, _s) \
struct device_attribute *attr, static ssize_t _f##_show(struct device *dev, \
char *buf) struct device_attribute *attr, char *buf) \
{ { return aspm_attr_show_common(dev, attr, buf, ASPM_STATE_##_s); } \
struct pci_dev *pci_device = to_pci_dev(dev); \
struct pcie_link_state *link_state = pci_device->link_state; static ssize_t _f##_store(struct device *dev, \
struct device_attribute *attr, \
const char *buf, size_t len) \
{ return aspm_attr_store_common(dev, attr, buf, len, ASPM_STATE_##_s); }
return sprintf(buf, "%d\n", link_state->clkpm_enabled); ASPM_ATTR(l0s_aspm, L0S)
} ASPM_ATTR(l1_aspm, L1)
ASPM_ATTR(l1_1_aspm, L1_1)
ASPM_ATTR(l1_2_aspm, L1_2)
ASPM_ATTR(l1_1_pcipm, L1_1_PCIPM)
ASPM_ATTR(l1_2_pcipm, L1_2_PCIPM)
static ssize_t clk_ctl_store(struct device *dev, static ssize_t clkpm_show(struct device *dev,
struct device_attribute *attr, struct device_attribute *attr, char *buf)
const char *buf,
size_t n)
{ {
struct pci_dev *pdev = to_pci_dev(dev); struct pci_dev *pdev = to_pci_dev(dev);
bool state; struct pcie_link_state *link = pcie_aspm_get_link(pdev);
if (strtobool(buf, &state)) return sprintf(buf, "%d\n", link->clkpm_enabled);
}
static ssize_t clkpm_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t len)
{
struct pci_dev *pdev = to_pci_dev(dev);
struct pcie_link_state *link = pcie_aspm_get_link(pdev);
bool state_enable;
if (strtobool(buf, &state_enable) < 0)
return -EINVAL; return -EINVAL;
down_read(&pci_bus_sem); down_read(&pci_bus_sem);
mutex_lock(&aspm_lock); mutex_lock(&aspm_lock);
pcie_set_clkpm_nocheck(pdev->link_state, state);
link->clkpm_disable = !state_enable;
pcie_set_clkpm(link, policy_to_clkpm_state(link));
mutex_unlock(&aspm_lock); mutex_unlock(&aspm_lock);
up_read(&pci_bus_sem); up_read(&pci_bus_sem);
return n; return len;
} }
static DEVICE_ATTR_RW(link_state); static DEVICE_ATTR_RW(clkpm);
static DEVICE_ATTR_RW(clk_ctl); static DEVICE_ATTR_RW(l0s_aspm);
static DEVICE_ATTR_RW(l1_aspm);
static DEVICE_ATTR_RW(l1_1_aspm);
static DEVICE_ATTR_RW(l1_2_aspm);
static DEVICE_ATTR_RW(l1_1_pcipm);
static DEVICE_ATTR_RW(l1_2_pcipm);
static char power_group[] = "power"; static struct attribute *aspm_ctrl_attrs[] = {
void pcie_aspm_create_sysfs_dev_files(struct pci_dev *pdev) &dev_attr_clkpm.attr,
&dev_attr_l0s_aspm.attr,
&dev_attr_l1_aspm.attr,
&dev_attr_l1_1_aspm.attr,
&dev_attr_l1_2_aspm.attr,
&dev_attr_l1_1_pcipm.attr,
&dev_attr_l1_2_pcipm.attr,
NULL
};
static umode_t aspm_ctrl_attrs_are_visible(struct kobject *kobj,
struct attribute *a, int n)
{ {
struct pcie_link_state *link_state = pdev->link_state; struct device *dev = kobj_to_dev(kobj);
struct pci_dev *pdev = to_pci_dev(dev);
struct pcie_link_state *link = pcie_aspm_get_link(pdev);
static const u8 aspm_state_map[] = {
ASPM_STATE_L0S,
ASPM_STATE_L1,
ASPM_STATE_L1_1,
ASPM_STATE_L1_2,
ASPM_STATE_L1_1_PCIPM,
ASPM_STATE_L1_2_PCIPM,
};
if (!link_state) if (aspm_disabled || !link)
return; return 0;
if (link_state->aspm_support) if (n == 0)
sysfs_add_file_to_group(&pdev->dev.kobj, return link->clkpm_capable ? a->mode : 0;
&dev_attr_link_state.attr, power_group);
if (link_state->clkpm_capable) return link->aspm_capable & aspm_state_map[n - 1] ? a->mode : 0;
sysfs_add_file_to_group(&pdev->dev.kobj,
&dev_attr_clk_ctl.attr, power_group);
} }
void pcie_aspm_remove_sysfs_dev_files(struct pci_dev *pdev) const struct attribute_group aspm_ctrl_attr_group = {
{ .name = "link",
struct pcie_link_state *link_state = pdev->link_state; .attrs = aspm_ctrl_attrs,
.is_visible = aspm_ctrl_attrs_are_visible,
if (!link_state) };
return;
if (link_state->aspm_support)
sysfs_remove_file_from_group(&pdev->dev.kobj,
&dev_attr_link_state.attr, power_group);
if (link_state->clkpm_capable)
sysfs_remove_file_from_group(&pdev->dev.kobj,
&dev_attr_clk_ctl.attr, power_group);
}
#endif
static int __init pcie_aspm_disable(char *str) static int __init pcie_aspm_disable(char *str)
{ {

View File

@ -1544,9 +1544,13 @@ extern bool pcie_ports_native;
#define pcie_ports_native false #define pcie_ports_native false
#endif #endif
#define PCIE_LINK_STATE_L0S 1 #define PCIE_LINK_STATE_L0S BIT(0)
#define PCIE_LINK_STATE_L1 2 #define PCIE_LINK_STATE_L1 BIT(1)
#define PCIE_LINK_STATE_CLKPM 4 #define PCIE_LINK_STATE_CLKPM BIT(2)
#define PCIE_LINK_STATE_L1_1 BIT(3)
#define PCIE_LINK_STATE_L1_2 BIT(4)
#define PCIE_LINK_STATE_L1_1_PCIPM BIT(5)
#define PCIE_LINK_STATE_L1_2_PCIPM BIT(6)
#ifdef CONFIG_PCIEASPM #ifdef CONFIG_PCIEASPM
int pci_disable_link_state(struct pci_dev *pdev, int state); int pci_disable_link_state(struct pci_dev *pdev, int state);