ce linux pm - read.pudn.comread.pudn.com/downloads157/ebook/701678/%c7%b6%c8... · 远见品质 spm...

50
The success's road CE Linux PM CE Linux PM

Upload: buicong

Post on 30-Aug-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

The success's road

CE Linux PMCE Linux PM

远见品质 What We’ll Talkv PM Theory and Hardware Supportv 2.6 kernel PM_Subsystemv CELF PM Specification Appendixv Current PM Implementation

远见品质 Hardware components

远见品质 Hardware components

vPower Controlled Components include:Ø Intel PXA Xscale CPU, Ø real-time clock, DRAM, Flash, LCD, front light,

UART, audio codec, touchscreen, keys and power button,etc.

vThe Xscale PXA27X CPU supports several power-saving features.

远见品质 Xscale CPU PM Features

v PXA27X PM features, including frequency scaling and CPU core voltage scaling.

vAnd several modes of operation:Ø Run mode

All power supplies enabled, all clocks running and every on-chip resource is functional.

Ø Idle modeCPU clock stopped. other on-chip resources are active. When an interrupt occurs, the CPU is reactivated.

Ø Sleep modePower is switched off to the majority of the processor. Some preprogrammed event, such as a power button press, wakes up the CPU from this mode

远见品质 Handheld Energy Budgets

远见品质 PM Theory

vOS PM focuses on active power consumptionvModulate methods:ØVoltage/clock modulateØClock selectedØPower supply selected

远见品质 Kernel PM_Subsystem

远见品质 Kernel PM_Subsystem

vWhat we will talk Covers the two broad areas of power management:

ØSystem Power Management (SPM)ØDevice Power Management (DPM) ØAnd more……

远见品质 Device Power Management (DPM)

vDevice PMØDeals with the process of placing individual

devices into low-power states while the system is running. Ø Invoked explicitly on devices, or may happen

automatically after a device has been idle for a set of amount of time.

远见品质 System Power Management (SPM)

v System PMØ The entire system is placed into a low-power

state. running system is saved before powered down, and restored once the system has regained power.

Ø Automatically enter a low-power state after it has been idle for some amount of time, or when some critical state has been reached.

Ø Also policy decisions that are up to the user to configure and require some global mechanism for controlling.

远见品质 Device Power Management

vNew driver model (LDM) in the 2.6 kernel, which provides a hierarchical representation of the devices in the system.vDevice power states are described as

'D' states, and consist of states D0-D3, inclusive. (Influenced by ACPI Spec)

远见品质 Device PM Data Structs

vA device's PM infor stored in structdevice_pm, struct device contains a device_pm object.vstruct device_pm

{dev_power_t power_state;u8 * saved_state;atomic_t depend;atomic_t disable;struct kobject kobj;

};

远见品质 Device PM Data Structsv typedef enum {

DEVICE_PM_ON,DEVICE_PM_INT1,DEVICE_PM_INT2,DEVICE_PM_OFF,DEVICE_PM_UNKNOWN, //Device’s initial power state

} dev_power_t;v enum {

SUSPEND_SAVE_STATE,SUSPEND_POWER_DOWN,

};v enum {

RESUME_POWER_ON,RESUME_RESTORE_STATE,

};

远见品质 Power Dependenciesv Two steps to suspend deviceØ one to save stateØ another to power the device down

v Conversely, Ø one call is made to the resume() method to power the device upØ another to restore device state.

v all children devices must be powered down beforetheir parent can be powered down.

v Conversely, the parent device must be powered up before any children devices may be accessed.

v Only when a device's depend count (structdevice_pm::depend field.)is 0 may it be powered down.

v int device_pm_get/put(struct device *);

远见品质 Register PM handlersv static int __init i2c_init(void){

retval = bus_register(&i2c_bus_type);retval = driver_register(&i2c_adapter_driver);return class_register(&i2c_adapter_class);

}v struct bus_type i2c_bus_type = {

.name = "i2c",

.match = i2c_device_match,

.suspend = i2c_bus_suspend,

.resume = i2c_bus_resume,};

v static int i2c_bus_suspend(struct device* dev, u32 state){if (dev->driver && dev->driver->suspend)rc = dev->driver->suspend(dev,state,0);

} //Device Specified driver, explain later

远见品质 Register PM handlers

vstatic struct device_driveri2c_adapter_driver = {

.name = "i2c_adapter",

.bus = &i2c_bus_type,

.probe = i2c_device_probe,

.remove = i2c_device_remove,

};vstatic struct class i2c_adapter_class = {

.name = "i2c-adapter",

.release = &i2c_adapter_class_dev_release,

};

远见品质 Register PM handlersv static struct device_driver i2c_pxa_driver ={

.name = "pxa2xx-i2c",

.bus = &platform_bus_type,

.probe = i2c_pxa_probe,

.remove = i2c_pxa_remove,

.suspend = i2c_pxa_controller_suspend,

.resume = i2c_pxa_controller_resume,

};v static int __init i2c_adap_pxa_init(void){

return driver_register(&i2c_pxa_driver);

}

远见品质 Register PM handlers

vstatic inti2c_pxa_controller_suspend(struct device *dev, u32 state, u32 level){

#ifdef CONFIG_PMi2c_pxa_wait_bus_not_busy();i2c_pxa_stop();#endifreturn 0;

}

远见品质 Internal Sequence of Device PM

v echo n > /sys/bus/i2c/devices/1-1/power/state (n can be “0~3”)vWhen n == 0 (resume steps similar)Ø state_store() [drivers/base/power/sysfs.c] -> üdpm_runtime_suspend()

[drivers/base/power/runtime.c] -> § suspend_device()

[drivers/base/power/suspend.c] -> • call suspend handler of the bus which

the specified device belongs to . -> • call suspend handler for the specified

device

远见品质 System Power Management

vPower States:Kernel supports states are Standby, Suspend,

and Hibernate.

vtypedef enum {POWER_ON = 0,POWER_STANDBY = 0x01,POWER_SUSPEND = 0x02,POWER_HIBERNATE = 0x04,

} pm_system_state_t;

远见品质 System PM Statesv StandbyØ low-latency power as 'power-on suspend'. Ø system conserves power by placing the CPU in a halt state and the

devices in the D1 state. Ø The power savings are not significant, but the response latency is

minimal.v SuspendØ As 'suspend-to-RAM'. Ø all devices are placed in the D3 state and the entire system, except

main memory, is expected to lose power. Ø Memory is placed in self-refresh mode, so its contents are not lost.

Response latency is higher than Standby.v HibernateØ conserves the most power by turning off the entire system, after

saving state to a persistent medium. All devices are powered offunconditionally.

Ø The response latency is the highest .Ø But we won’t talk about it here!

远见品质 System PM Data Structs

v struct pm_state {struct pm_driver * drv; // handle the power statepm_system_state_t sys; // low-level power state system will enterpm_device_state_t dev; // lowest power state devices may enter.struct kobject kobj; // object for managing an instance's lifetime

};v struct pm_state pm_state_standby;v struct pm_state pm_state_suspend;v struct pm_state pm_state_hibernate;

远见品质 SPM sysfs interface

vExample:v# cat /sys/power/state ØResults: “standby suspend hibernate”

v# echo suspend > /sys/power/state ØBy writing the name of a state to this file, the

system will perform a power state transition.

远见品质 SPM Platform Drivers

vstruct pm_driver {u32 states;int (*prepare) (u32 state);int (*save) (u32 state);int (*sleep) (u32 state);int (*restore) (u32 state);int (*cleanup) (u32 state);struct kobject kobj;

};v int pm_driver_register(struct pm_driver *);v void pm_driver_unregister(struct pm_driver *);

远见品质 SPM Platform Drivers

v struct pm_attribute {struct attribute attr;ssize_t (*show)(struct pm_driver *, char *);ssize_t (*store)(struct pm_driver *, const char *, size_t);

};v int pm_attribute_create(struct pm_driver *,

struct pm_attribute *);v void pm_attribute_remove(struct pm_driver *,

struct pm_attribute *);v pm_attribute “store” method will call:Ø enter_state(state); and enter related pm state

远见品质 Power State TransitionsvTriggered by writing the requested state to the

sysfs file /sys/power/state. v Suspend sequence:Ø First verify that the system can enter the power state. Ø PM core then quests the running system by disabling

preemption and 'freezing' all processes.Ø Next, device_suspend() to save system device state , driver's

save() method to save low-level system state.Ø PM core then disables interrupts and calls

device_power_down() to place each device in the specified low power state.

Ø Finally, it calls the driver's sleep() method to transition the system to the low-power state.

远见品质 Power State TransitionsvResume sequenceØPlatform returning excecution to the correct

place (after the return from the driver's sleep() method).ØPM core power on all devices and restore

interrupts. driver's restore() method is called to restore low-level system state, and device_pm_resume() is called to restore device context. ØFinally, the driver's cleanup() method is called,

processes are 'thawed', and preemption is reenabled.

远见品质 Internal Sequence of System PM

v echo “standby" > /sys/power/state Ø state_store() [kernel/power/main.c] -> Ø enter_state() [kernel/power/main.c] :

v This is the entrance to SPM, go with the same route when pm_suspend() called.

远见品质 Internal Sequence of System PM

v pm_suspend() call flowØ enter_state() [kernel/power/main.c] ->Ø suspend_prepare() [kernel/power/main.c] -> Ø pm_prepare_console() [kernel/power/console.c] Ø freeze_processes() [kernel/power/process.c] Ø pm_ops->prepare() Ø device_suspend() [driver/base/power/suspend.c] -> Ø suspend_device(), foreach device on the "dpm_active" list

[driver/base/power/suspend.c] Ø move the entry from "dpm_active" to "dpm_off"

or "dpm_off_irq" on suspend handler returns -EAGAIN

Ø suspend_enter() [kernel/power/main.c] -> Ø local_irq_save()Ø Con….

远见品质 Internal Sequence of System PMØ device_power_down() [drivers/base/power/suspend.c] -> Ø suspend_device(), foreach device on the "dpm_off_irq" list

[drivers/base/power/suspend.c] Ø sysdev_suspend() [drivers/base/sys.c] Ø pm_ops->enter() // System will down here Ø device_power_up() // called with "irq off"

[drivers/base/power/resume.c] -> Ø sysdev_resume() [drivers/base/sys.c] Ø dpm_power_up() [drivers/base/power/resume.c] -

> Ø resume_device(), foreach device on the "dpm_off_irq" list

[driver/base/power/suspend.c] Ø move the entry from "dpm_irq_off" to "dpm_active" Ø local_irq_restore() Ø suspend_finish() [kernel/power/main.c] ->

远见品质 Internal Sequence of System PM

Ødevice_resume() // called with "irq on"

[drivers/base/power/resume.c] -> Ødpm_resume()

[drivers/base/power/resume.c] -> Ø resume_device(), foreach device on the

"dpm_off" list. Ømove the entry to "dpm_active" from "dpm_off" Øpm_ops->finish() // target PM specific Ø thaw_processes() [kernel/power/process.c] Øpm_restore_console() [kernel/power/console.c]

远见品质 Internal Sequence of System PMØ suspend_device()

[drivers/base/power/suspend.c] -> Ø call suspend handler the bus which the specified

device belongs to . -> Ø call suspend handler for the specified deviceØ sysdev_suspend() [drivers/base/sys.c]Ø for each kernel susbsys and classØ call suspend handler for each driver on

"global_drivers", Ø registered with sysdev_register(). Ø call a suspend handler for each driver under the

class. Ø call a suspend handler for the class

远见品质 Internal Sequence of System PMØ sysdev_resume() [drivers/base/sys.c] Ø for each kernel susbsys and class Ø call a resume handler for the class Ø call a resume handler for each driver under the

class. Ø call resume handler for each driver on

"global_drivers", Ø registered with sysdev_register(). Ø resume_device() [drivers/base/power/resume.c]

-> Ø call resume handler of the bus which the

specified device belongs to. -> Ø call resume handler for the specified device

远见品质 PM_OPS

v static struct pm_ops acpi_pm_ops = { .prepare = acpi_pm_prepare, .enter = acpi_pm_enter, .finish = acpi_pm_finish,

}; v [drivers\acpi\sleep\ main.c]:

pm_set_ops(&acpi_pm_ops);vThe globle struct pm_ops * pm_ops is link

to acpi_pm_ops. v So the PM Subsystem can be used with

ACPI Implement.

远见品质 PXA PM_OPSv PXA PM_OPS ImplementationØ pm_set_ops(&pxa_pm_ops); [arch\arm\mach-pxa\ pm.c]Ø static struct pm_ops pxa_pm_ops = {

.pm_disk_mode = PM_DISK_FIRMWARE,

.prepare = pxa_pm_prepare,

.enter = pxa_pm_enter,

.finish = pxa_pm_finish,};

Ø static int pxa_pm_enter(suspend_state_t state) {if (state != PM_SUSPEND_MEM)

return -EINVAL;......//implement suspend to ram

vAll the PM function can be accessed by sysfsinterfaces, or apmd, or others D.

远见品质 CELF PM Specification Appendix

vCELF specified PM technologies are classified into three categories:Øplatform suspend/resume (Talked Above)Ødevice power management (Talked Above)Øplatform dynamic power management (Will Talk)üDynamic power management is used for

reducing power dissipation while a product is being used. Dynamic power management covers also reducing power for very short idle periods of time while a product is busy. üAnd more……

远见品质 Dynamic power management

vDynamic power management(So called “Dynamic PM”)

v Such power management can involve dynamic control of peripheral clocks and power supplies, varying the timer tick frequency during idle periods and CPU frequency/voltage scaling.

vThis uses a combination of user-space and kernel-space software Ø a Policy Manager component running in user-space Ø a Power Management Engine ("PM-Engine") component

running in kernel-space

远见品质 Dynamic power management

vThe software selects the system operating state based on a combination of the following:Ø application requirementsØ the parameters for battery lifetime or task

deadlinesØCPU loadingØ the current device constraintsØ the current operating stateØuser interaction

远见品质 Kernel PM_Subsystem

Here we add DPM Engine

远见品质 Dynamic PM Implement

Driver : 1 . Platform 2 . Peripheral

Sysfs Proc

DPM Engine

HW

Task State Transform

Policy Manager

DPM Command

远见品质 DPM - Power Management Policy

v Policies tell DPM how to respond to system events and lower/raise the power usage accordingly

v Policy is based on the following system dependent terms:Ø Operating Points:üUnique processor power states defined by the adjustable

power parameters for your systemØ Operating States:üCPU execution states (such as Sleep, Task, and Idle) that

are triggered by an operating pointØ Classes:üAssociate operating points with operating states, allowing

you to assign an operating state more than one operating point

远见品质 Using DPM- Operating StatesvDPM Provides these operating states:Ø SleepØ IdleØ Task

v If more are required, define them in your arch-specific DPM file

v Filename will be as follows depending on your architecture:Ø linux/include/asm-arm/arch <processor>/<arch>_dpm.hØ linux/include/arch/ppc/platforms/<arch>_dpm.h

远见品质 Using DPM- Initialize

#!/bin/shecho terminate > /sys/dpm/controlecho init > /sys/dpm/control

远见品质 Using DPM- Install OPs

#!/bin/shecho create 192 1500 16 1 1 2 -1 -1 -1 -1 > /sys/dpm/op/controlecho create 168 1500 14 1 1 2 -1 -1 -1 -1 > /sys/dpm/op/controlecho create 84 1500 14 1 2 2 -1 -1 -1 -1 > /sys/dpm/op/controlecho create 84-1.1v 1100 14 1 2 2 -1 -1 -1 -1 > /sys/dpm/op/controlecho create 60 1500 5 1 1 1 -1 -1 -1 -1 > /sys/dpm/op/controlecho create 60-1.1v 1100 5 1 1 1 -1 -1 -1 -1 > /sys/dpm/op/controlecho create sleep-168 1500 14 1 0 -1 -1 -1 -1 -1 >

/sys/dpm/op/controlecho create sleep-60 1500 1 2 0 2 -1 -1 -1 -1 > /sys/dpm/op/control

远见品质 Using DPM- Create Policies#!/bin/sh# The states for the policies are:# "idle-task", "idle", "sleep",# "task-4", "task-3", "task-2", "task-1",# "task",# "task+1", "task+2", "task+3", "task+4"

echo create hi-power 192 192 sleep-168 \168 168 168 168 \192 \192 192 192 192 > /sys/dpm/policy/control

echo create lo-power 60 60 sleep-60 \60 60 60 60 \60 \84 84 84 84 > /sys/dpm/policy/control

echo create lo-1.1v 60-1.1v 60-1.1v sleep-60 \60-1.1v 60-1.1v 60-1.1v 60-1.1v \60-1.1v \84-1.1v 84-1.1v 84-1.1v 84-1.1v > /sys/dpm/policy/control

远见品质 Using DPM- Set Active Policy

#!/bin/shecho set hi-power > /sys/dpm/policy/controlcat /sys/dpm/control

远见品质 Sum.

vCE Linux Power Management is still on development.vHope Chinese Kernel Hacker can

make contribution to it!ØHope that’ll happen J

远见品质

DISCUSS PLZ!

write to me: [email protected]

www.farsight.com.cn

Thank you!

The success's road