Skip to content

Commit eb9d0fe

Browse files
rjwysockijbarnes993
authored andcommitted
PCI ACPI: Rework PCI handling of wake-up
* Introduce function acpi_pm_device_sleep_wake() for enabling and disabling the system wake-up capability of devices that are power manageable by ACPI. * Introduce function acpi_bus_can_wakeup() allowing other (dependent) subsystems to check if ACPI is able to enable the system wake-up capability of given device. * Introduce callback .sleep_wake() in struct pci_platform_pm_ops and for the ACPI PCI 'driver' make it use acpi_pm_device_sleep_wake(). * Introduce callback .can_wakeup() in struct pci_platform_pm_ops and for the ACPI 'driver' make it use acpi_bus_can_wakeup(). * Move the PME# handlig code out of pci_enable_wake() and split it into two functions, pci_pme_capable() and pci_pme_active(), allowing the caller to check if given device is capable of generating PME# from given power state and to enable/disable the device's PME# functionality, respectively. * Modify pci_enable_wake() to use the new ACPI callbacks and the new PME#-related functions. * Drop the generic .platform_enable_wakeup() callback that is not used any more. * Introduce device_set_wakeup_capable() that will set the power.can_wakeup flag of given device. * Rework PCI device PM initialization so that, if given device is capable of generating wake-up events, either natively through the PME# mechanism, or with the help of the platform, its power.can_wakeup flag is set and its power.should_wakeup flag is unset as appropriate. * Make ACPI set the power.can_wakeup flag for devices found to be wake-up capable by it. * Make the ACPI wake-up code enable/disable GPEs for devices that have the wakeup.flags.prepared flag set (which means that their wake-up power has been enabled). Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl> Signed-off-by: Jesse Barnes <jbarnes@virtuousgeek.org>
1 parent 0af4b8c commit eb9d0fe

11 files changed

Lines changed: 208 additions & 116 deletions

File tree

‎drivers/acpi/bus.c‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,17 @@ bool acpi_bus_power_manageable(acpi_handle handle)
306306

307307
EXPORT_SYMBOL(acpi_bus_power_manageable);
308308

309+
bool acpi_bus_can_wakeup(acpi_handle handle)
310+
{
311+
struct acpi_device *device;
312+
int result;
313+
314+
result = acpi_bus_get_device(handle, &device);
315+
return result ? false : device->wakeup.flags.valid;
316+
}
317+
318+
EXPORT_SYMBOL(acpi_bus_can_wakeup);
319+
309320
/* --------------------------------------------------------------------------
310321
Event Management
311322
-------------------------------------------------------------------------- */

‎drivers/acpi/glue.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ static int acpi_bind_one(struct device *dev, acpi_handle handle)
166166
"firmware_node");
167167
ret = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
168168
"physical_node");
169+
if (acpi_dev->wakeup.flags.valid)
170+
device_set_wakeup_capable(dev, true);
169171
}
170172

171173
return 0;

‎drivers/acpi/sleep/main.c‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,31 @@ int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p)
468468
*d_min_p = d_min;
469469
return d_max;
470470
}
471+
472+
/**
473+
* acpi_pm_device_sleep_wake - enable or disable the system wake-up
474+
* capability of given device
475+
* @dev: device to handle
476+
* @enable: 'true' - enable, 'false' - disable the wake-up capability
477+
*/
478+
int acpi_pm_device_sleep_wake(struct device *dev, bool enable)
479+
{
480+
acpi_handle handle;
481+
struct acpi_device *adev;
482+
483+
if (!device_may_wakeup(dev))
484+
return -EINVAL;
485+
486+
handle = DEVICE_ACPI_HANDLE(dev);
487+
if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
488+
printk(KERN_DEBUG "ACPI handle has no context!\n");
489+
return -ENODEV;
490+
}
491+
492+
return enable ?
493+
acpi_enable_wakeup_device_power(adev, acpi_target_sleep_state) :
494+
acpi_disable_wakeup_device_power(adev);
495+
}
471496
#endif
472497

473498
static void acpi_power_off_prepare(void)

‎drivers/acpi/sleep/wakeup.c‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,15 @@ void acpi_enable_wakeup_device(u8 sleep_state)
6666
list_for_each_safe(node, next, &acpi_wakeup_device_list) {
6767
struct acpi_device *dev =
6868
container_of(node, struct acpi_device, wakeup_list);
69+
6970
if (!dev->wakeup.flags.valid)
7071
continue;
72+
7173
/* If users want to disable run-wake GPE,
7274
* we only disable it for wake and leave it for runtime
7375
*/
74-
if (!dev->wakeup.state.enabled ||
75-
sleep_state > (u32) dev->wakeup.sleep_state) {
76+
if ((!dev->wakeup.state.enabled && !dev->wakeup.flags.prepared)
77+
|| sleep_state > (u32) dev->wakeup.sleep_state) {
7678
if (dev->wakeup.flags.run_wake) {
7779
spin_unlock(&acpi_device_lock);
7880
/* set_gpe_type will disable GPE, leave it like that */
@@ -110,8 +112,9 @@ void acpi_disable_wakeup_device(u8 sleep_state)
110112

111113
if (!dev->wakeup.flags.valid)
112114
continue;
113-
if (!dev->wakeup.state.enabled ||
114-
sleep_state > (u32) dev->wakeup.sleep_state) {
115+
116+
if ((!dev->wakeup.state.enabled && !dev->wakeup.flags.prepared)
117+
|| sleep_state > (u32) dev->wakeup.sleep_state) {
115118
if (dev->wakeup.flags.run_wake) {
116119
spin_unlock(&acpi_device_lock);
117120
acpi_set_gpe_type(dev->wakeup.gpe_device,

‎drivers/base/power/sysfs.c‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
#include <linux/string.h>
77
#include "power.h"
88

9-
int (*platform_enable_wakeup)(struct device *dev, int is_on);
10-
11-
129
/*
1310
* wakeup - Report/change current wakeup option for device
1411
*

‎drivers/pci/pci-acpi.c‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,30 @@ static int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state)
299299
return error;
300300
}
301301

302+
static bool acpi_pci_can_wakeup(struct pci_dev *dev)
303+
{
304+
acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
305+
306+
return handle ? acpi_bus_can_wakeup(handle) : false;
307+
}
308+
309+
static int acpi_pci_sleep_wake(struct pci_dev *dev, bool enable)
310+
{
311+
int error = acpi_pm_device_sleep_wake(&dev->dev, enable);
312+
313+
if (!error)
314+
dev_printk(KERN_INFO, &dev->dev,
315+
"wake-up capability %s by ACPI\n",
316+
enable ? "enabled" : "disabled");
317+
return error;
318+
}
319+
302320
static struct pci_platform_pm_ops acpi_pci_platform_pm = {
303321
.is_manageable = acpi_pci_power_manageable,
304322
.set_state = acpi_pci_set_power_state,
305323
.choose_state = acpi_pci_choose_state,
324+
.can_wakeup = acpi_pci_can_wakeup,
325+
.sleep_wake = acpi_pci_sleep_wake,
306326
};
307327

308328
/* ACPI bus type */

‎drivers/pci/pci.c‎

Lines changed: 124 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,8 @@ static struct pci_platform_pm_ops *pci_platform_pm;
380380

381381
int pci_set_platform_pm(struct pci_platform_pm_ops *ops)
382382
{
383-
if (!ops->is_manageable || !ops->set_state || !ops->choose_state)
383+
if (!ops->is_manageable || !ops->set_state || !ops->choose_state
384+
|| !ops->sleep_wake || !ops->can_wakeup)
384385
return -EINVAL;
385386
pci_platform_pm = ops;
386387
return 0;
@@ -403,6 +404,17 @@ static inline pci_power_t platform_pci_choose_state(struct pci_dev *dev)
403404
pci_platform_pm->choose_state(dev) : PCI_POWER_ERROR;
404405
}
405406

407+
static inline bool platform_pci_can_wakeup(struct pci_dev *dev)
408+
{
409+
return pci_platform_pm ? pci_platform_pm->can_wakeup(dev) : false;
410+
}
411+
412+
static inline int platform_pci_sleep_wake(struct pci_dev *dev, bool enable)
413+
{
414+
return pci_platform_pm ?
415+
pci_platform_pm->sleep_wake(dev, enable) : -ENODEV;
416+
}
417+
406418
/**
407419
* pci_raw_set_power_state - Use PCI PM registers to set the power state of
408420
* given PCI device
@@ -1035,6 +1047,56 @@ int pci_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
10351047
return pcibios_set_pcie_reset_state(dev, state);
10361048
}
10371049

1050+
/**
1051+
* pci_pme_capable - check the capability of PCI device to generate PME#
1052+
* @dev: PCI device to handle.
1053+
* @pm: PCI PM capability offset of the device.
1054+
* @state: PCI state from which device will issue PME#.
1055+
*/
1056+
static bool pci_pme_capable(struct pci_dev *dev, int pm, pci_power_t state)
1057+
{
1058+
u16 pmc;
1059+
1060+
if (!pm)
1061+
return false;
1062+
1063+
/* Check device's ability to generate PME# from given state */
1064+
pci_read_config_word(dev, pm + PCI_PM_PMC, &pmc);
1065+
1066+
pmc &= PCI_PM_CAP_PME_MASK;
1067+
pmc >>= ffs(PCI_PM_CAP_PME_MASK) - 1; /* First bit of mask */
1068+
1069+
return !!(pmc & (1 << state));
1070+
}
1071+
1072+
/**
1073+
* pci_pme_active - enable or disable PCI device's PME# function
1074+
* @dev: PCI device to handle.
1075+
* @pm: PCI PM capability offset of the device.
1076+
* @enable: 'true' to enable PME# generation; 'false' to disable it.
1077+
*
1078+
* The caller must verify that the device is capable of generating PME# before
1079+
* calling this function with @enable equal to 'true'.
1080+
*/
1081+
static void pci_pme_active(struct pci_dev *dev, int pm, bool enable)
1082+
{
1083+
u16 pmcsr;
1084+
1085+
if (!pm)
1086+
return;
1087+
1088+
pci_read_config_word(dev, pm + PCI_PM_CTRL, &pmcsr);
1089+
/* Clear PME_Status by writing 1 to it and enable PME# */
1090+
pmcsr |= PCI_PM_CTRL_PME_STATUS | PCI_PM_CTRL_PME_ENABLE;
1091+
if (!enable)
1092+
pmcsr &= ~PCI_PM_CTRL_PME_ENABLE;
1093+
1094+
pci_write_config_word(dev, pm + PCI_PM_CTRL, pmcsr);
1095+
1096+
dev_printk(KERN_INFO, &dev->dev, "PME# %s\n",
1097+
enable ? "enabled" : "disabled");
1098+
}
1099+
10381100
/**
10391101
* pci_enable_wake - enable PCI device as wakeup event source
10401102
* @dev: PCI device affected
@@ -1046,66 +1108,83 @@ int pci_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
10461108
* called automatically by this routine.
10471109
*
10481110
* Devices with legacy power management (no standard PCI PM capabilities)
1049-
* always require such platform hooks. Depending on the platform, devices
1050-
* supporting the standard PCI PME# signal may require such platform hooks;
1051-
* they always update bits in config space to allow PME# generation.
1111+
* always require such platform hooks.
10521112
*
1053-
* -EIO is returned if the device can't ever be a wakeup event source.
1054-
* -EINVAL is returned if the device can't generate wakeup events from
1055-
* the specified PCI state. Returns zero if the operation is successful.
1113+
* RETURN VALUE:
1114+
* 0 is returned on success
1115+
* -EINVAL is returned if device is not supposed to wake up the system
1116+
* Error code depending on the platform is returned if both the platform and
1117+
* the native mechanism fail to enable the generation of wake-up events
10561118
*/
10571119
int pci_enable_wake(struct pci_dev *dev, pci_power_t state, int enable)
10581120
{
10591121
int pm;
1060-
int status;
1061-
u16 value;
1062-
1063-
/* Note that drivers should verify device_may_wakeup(&dev->dev)
1064-
* before calling this function. Platform code should report
1065-
* errors when drivers try to enable wakeup on devices that
1066-
* can't issue wakeups, or on which wakeups were disabled by
1067-
* userspace updating the /sys/devices.../power/wakeup file.
1068-
*/
1122+
int error = 0;
1123+
bool pme_done = false;
10691124

1070-
status = call_platform_enable_wakeup(&dev->dev, enable);
1071-
1072-
/* find PCI PM capability in list */
1073-
pm = pci_find_capability(dev, PCI_CAP_ID_PM);
1125+
if (!device_may_wakeup(&dev->dev))
1126+
return -EINVAL;
10741127

1075-
/* If device doesn't support PM Capabilities, but caller wants to
1076-
* disable wake events, it's a NOP. Otherwise fail unless the
1077-
* platform hooks handled this legacy device already.
1128+
/*
1129+
* According to "PCI System Architecture" 4th ed. by Tom Shanley & Don
1130+
* Anderson we should be doing PME# wake enable followed by ACPI wake
1131+
* enable. To disable wake-up we call the platform first, for symmetry.
10781132
*/
1079-
if (!pm)
1080-
return enable ? status : 0;
10811133

1082-
/* Check device's ability to generate PME# */
1083-
pci_read_config_word(dev,pm+PCI_PM_PMC,&value);
1134+
if (!enable && platform_pci_can_wakeup(dev))
1135+
error = platform_pci_sleep_wake(dev, false);
10841136

1085-
value &= PCI_PM_CAP_PME_MASK;
1086-
value >>= ffs(PCI_PM_CAP_PME_MASK) - 1; /* First bit of mask */
1087-
1088-
/* Check if it can generate PME# from requested state. */
1089-
if (!value || !(value & (1 << state))) {
1090-
/* if it can't, revert what the platform hook changed,
1091-
* always reporting the base "EINVAL, can't PME#" error
1092-
*/
1093-
if (enable)
1094-
call_platform_enable_wakeup(&dev->dev, 0);
1095-
return enable ? -EINVAL : 0;
1137+
pm = pci_find_capability(dev, PCI_CAP_ID_PM);
1138+
if (!enable || pci_pme_capable(dev, pm, state)) {
1139+
pci_pme_active(dev, pm, enable);
1140+
pme_done = true;
10961141
}
10971142

1098-
pci_read_config_word(dev, pm + PCI_PM_CTRL, &value);
1143+
if (enable && platform_pci_can_wakeup(dev))
1144+
error = platform_pci_sleep_wake(dev, true);
10991145

1100-
/* Clear PME_Status by writing 1 to it and enable PME# */
1101-
value |= PCI_PM_CTRL_PME_STATUS | PCI_PM_CTRL_PME_ENABLE;
1146+
return pme_done ? 0 : error;
1147+
}
11021148

1103-
if (!enable)
1104-
value &= ~PCI_PM_CTRL_PME_ENABLE;
1149+
/**
1150+
* pci_pm_init - Initialize PM functions of given PCI device
1151+
* @dev: PCI device to handle.
1152+
*/
1153+
void pci_pm_init(struct pci_dev *dev)
1154+
{
1155+
int pm;
1156+
u16 pmc;
11051157

1106-
pci_write_config_word(dev, pm + PCI_PM_CTRL, value);
1158+
/* find PCI PM capability in list */
1159+
pm = pci_find_capability(dev, PCI_CAP_ID_PM);
1160+
if (!pm)
1161+
return;
1162+
/* Check device's ability to generate PME# */
1163+
pci_read_config_word(dev, pm + PCI_PM_PMC, &pmc);
11071164

1108-
return 0;
1165+
if ((pmc & PCI_PM_CAP_VER_MASK) > 3) {
1166+
dev_err(&dev->dev, "unsupported PM cap regs version (%u)\n",
1167+
pmc & PCI_PM_CAP_VER_MASK);
1168+
return;
1169+
}
1170+
1171+
if (pmc & PCI_PM_CAP_PME_MASK) {
1172+
dev_printk(KERN_INFO, &dev->dev,
1173+
"PME# supported from%s%s%s%s%s\n",
1174+
(pmc & PCI_PM_CAP_PME_D0) ? " D0" : "",
1175+
(pmc & PCI_PM_CAP_PME_D1) ? " D1" : "",
1176+
(pmc & PCI_PM_CAP_PME_D2) ? " D2" : "",
1177+
(pmc & PCI_PM_CAP_PME_D3) ? " D3hot" : "",
1178+
(pmc & PCI_PM_CAP_PME_D3cold) ? " D3cold" : "");
1179+
/*
1180+
* Make device's PM flags reflect the wake-up capability, but
1181+
* let the user space enable it to wake up the system as needed.
1182+
*/
1183+
device_set_wakeup_capable(&dev->dev, true);
1184+
device_set_wakeup_enable(&dev->dev, false);
1185+
/* Disable the PME# generation functionality */
1186+
pci_pme_active(dev, pm, false);
1187+
}
11091188
}
11101189

11111190
int

‎drivers/pci/pci.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,24 @@ extern void pci_cleanup_rom(struct pci_dev *dev);
1717
* platform; to be used during system-wide transitions from a
1818
* sleeping state to the working state and vice versa
1919
*
20+
* @can_wakeup - returns 'true' if given device is capable of waking up the
21+
* system from a sleeping state
22+
*
23+
* @sleep_wake - enables/disables the system wake up capability of given device
24+
*
2025
* If given platform is generally capable of power managing PCI devices, all of
2126
* these callbacks are mandatory.
2227
*/
2328
struct pci_platform_pm_ops {
2429
bool (*is_manageable)(struct pci_dev *dev);
2530
int (*set_state)(struct pci_dev *dev, pci_power_t state);
2631
pci_power_t (*choose_state)(struct pci_dev *dev);
32+
bool (*can_wakeup)(struct pci_dev *dev);
33+
int (*sleep_wake)(struct pci_dev *dev, bool enable);
2734
};
2835

2936
extern int pci_set_platform_pm(struct pci_platform_pm_ops *ops);
37+
extern void pci_pm_init(struct pci_dev *dev);
3038

3139
extern int pci_user_read_config_byte(struct pci_dev *dev, int where, u8 *val);
3240
extern int pci_user_read_config_word(struct pci_dev *dev, int where, u16 *val);

0 commit comments

Comments
 (0)