C H A P T E R  10

Power Management

Power management has become a fundamental feature across all computing devices. Every platform that runs Mac OS X can be put into a lower power mode, and so power management is just as important for a desktop computer that is always connected to a power supply as it is for a laptop or an iPhone that is running from a battery.

Even a Mac Pro, for example, can be placed into “sleep” mode, which puts the computer and its connected peripherals into a low power mode. During sleep mode, the CPU is put into a suspended state, the computer's display is powered off, and the hard drive is spun down. If the computer has PCI expansion slots, these will be powered down during sleep, with only a small amount of auxiliary power provided to allow a PCI card to initiate waking the computer from sleep.

Not all drivers will need to handle power management events. Whether a driver needs to implement power management will depend on the capabilities of the device and where the device draws power from. For example, if the driver for a PCI device doesn't support power management, the system must maintain full power to its PCI slots when it enters sleep mode because a PCI card is powered from the computer's motherboard. This leaves the computer in a state called “doze”, which is not a complete sleep mode. Note that if the power to PCI slots is suspended during sleep, any PCI devices will lose their configuration and must be reinitialized by their driver when the system wakes, which can only happen if the driver receives power management events.

As the example of a PCI card's driver demonstrates, the drivers for hardware devices play a role in the system's transition from one power state to another. Drivers can opt to receive a notification before the system goes into the low power sleep mode, at which time the driver can prepare its device for the new power state. Similarly, a driver can receive a notification when the system wakes from sleep, at which time the driver can restore its device to full operating functionality.

The I/O Registry Power Plane

Part of the complexities of power management is that the power state for one device usually cannot be looked at in isolation, since a device will typically be dependent on another device through which it draws power and, in turn, may have devices that are dependent on it for their power. Consider, for example, a PCI peripheral card that implements a USB host and provides USB ports. The card will be powered by the PCI bus and will provide power over the USB bus to devices that are connected to its ports. This has implications for the power management system; the PCI card can only enter a low power state if there are no USB devices connected to it or only if all of the USB devices connected to it are in a low power state themselves. Similarly, when the system is put to sleep, the PCI slot will lose power, and any USB devices that are connected to the card must be informed of the change in power state as well.

To model this power dependency, the I/O Kit maintains a tree that represents the power dependencies between hardware devices in the system. This tree is stored in its own section of the I/O Registry in a plane known as the “power plane.” The power plane can be viewed using the IORegistryExplorer utility, as shown in Figure 10-1.

images

Figure 10-1. A view of the power plane in IORegistryExplorer

Each driver that supports power management is represented as a node in the power plane connected to the parent nodes, which represents devices that provide it power, and with children nodes, which represent devices that it provides power to. The parent of a device in the power plane is typically the driver's provider class (that is, the same object that the driver is connected to in the service plane of the I/O Registry), although this does not need to be the case.

The tree representation makes it very easy to visualize the power dependencies between devices in the system and to see which devices draw their power from a particular hardware device. The tree structure also provides an important role for the system itself, since it allows the system to determine which devices will be affected when the power state of one device changes.

Power Management in the I/O Kit

The system's power management, including transitions from one power state to another, is handled by the I/O Kit. Power management is performed by drivers that run in the kernel; user space drivers that can be written for hardware, such as USB devices, cannot play a part in the power management of that device. The I/O Kit provides support for power management in the IOService superclass from which all Mac OS X drivers are ultimately derived. This makes power management accessible to all drivers, providing that the driver has chosen to insert itself into the power plane.

The IOService also manages synchronization between a parent device and the children devices that depend on it for power. For example, before transitioning into a lower power state, the system will ensure that all children devices have transitioned to the lower power state before the parent device, on which they rely on for power, is sent a request to lower its power state. Similarly, when waking from sleep, the system will ensure that a parent device has become fully powered before sending a request to the dependent children devices to raise their power state.

Transitions from one power state to another are incredibly difficult because it can take some time for hardware to change to the new state. This can lead to situations where a power state transition is initiated while an existing power state transition is in progress. For example, a hard disk may spin down due to inactivity, but then a read request may immediately be made, requiring the disk to spin up again. The IOService class handles a lot of the details that would otherwise make power management difficult to implement. Each power state transition is serialized by IOService, and a driver will never receive a power request until it has finished handling its previous power request.

The support provided by the IOService class for power management means that a driver is free to concentrate only on the power management of its own device; the I/O Kit framework will take care of the details that would otherwise make power management difficult to implement.

Power state changes can be initiated by two sources. The system may request that a device be placed into a new power state in response to the computer being put to sleep, or a device (or its driver) may initiate a change of its power state. An example of a device initiating a transition to a lower power state is the computer display switching itself off when the computer hasn't been used for a period of time or a hard disk spinning down if it hasn't received a read or write request for some time

A driver can choose to implement support for either of these two types of power management; it can choose to respond to changes to system power, such as a sleep event, or it can volunteer to lower its power state itself, such as by spinning down its disk. Both of these cases are handled through the I/O Kit and are discussed in this chapter.

In general, power management is necessary for a device that not only has hardware support for being placed into a lower power state but also has the opportunity to be placed into a low power state due to the way that it is used. Depending on the type of hardware device that your driver manages, there may be little work to support transitions between various power states.

images Note This chapter describes the work that a generic I/O Kit driver needs to take to handle power management requests. Certain I/O Kit driver families, such as the audio family, will add their driver to the power plane and respond to power management requests on behalf of the driver. Before adding power management support to your driver, you should check that your driver's superclass is not already handling power requests.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.235.63