]> git.saurik.com Git - apple/xnu.git/blame - iokit/Kernel/IOPMPowerStateQueue.cpp
xnu-1228.9.59.tar.gz
[apple/xnu.git] / iokit / Kernel / IOPMPowerStateQueue.cpp
CommitLineData
55e303ae
A
1/*
2 * Copyright (c) 2001-2002 Apple Computer, Inc. All rights reserved.
3 *
2d21ac55
A
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
55e303ae
A
27 */
28
29#include "IOPMPowerStateQueue.h"
0c530ab8 30#include "IOKit/IOLocks.h"
55e303ae
A
31#undef super
32#define super IOEventSource
33OSDefineMetaClassAndStructors(IOPMPowerStateQueue, IOEventSource);
34
2d21ac55 35#ifndef __ppc__ /* ppc does this right and doesn't need these routines */
0c530ab8 36static
2d21ac55
A
37void * OSDequeueAtomic(void * volatile * inList, SInt32 inOffset)
38{
39 /* The _pointer_ is volatile, not the listhead itself */
40 void * volatile oldListHead;
41 void * volatile newListHead;
0c530ab8
A
42
43 do {
44 oldListHead = *inList;
45 if (oldListHead == NULL) {
46 break;
47 }
48
2d21ac55 49 newListHead = *(void * volatile *) (((char *) oldListHead) + inOffset);
0c530ab8 50 } while (! OSCompareAndSwap((UInt32)oldListHead,
2d21ac55 51 (UInt32)newListHead, (volatile UInt32 *)inList));
0c530ab8
A
52 return oldListHead;
53}
54
55static
2d21ac55 56void OSEnqueueAtomic(void * volatile * inList, void * inNewLink, SInt32 inOffset)
0c530ab8 57{
2d21ac55
A
58 /* The _pointer_ is volatile, not the listhead itself */
59 void * volatile oldListHead;
60 void * volatile newListHead = inNewLink;
61 void * volatile * newLinkNextPtr = (void * volatile *) (((char *) inNewLink) + inOffset);
0c530ab8
A
62
63 do {
64 oldListHead = *inList;
65 *newLinkNextPtr = oldListHead;
66 } while (! OSCompareAndSwap((UInt32)oldListHead, (UInt32)newListHead,
2d21ac55 67 (volatile UInt32 *)inList));
0c530ab8 68}
2d21ac55 69#endif /* ! __ppc__ */
0c530ab8
A
70
71
55e303ae
A
72IOPMPowerStateQueue *IOPMPowerStateQueue::PMPowerStateQueue(OSObject *inOwner)
73{
74 IOPMPowerStateQueue *me = new IOPMPowerStateQueue;
75
76 if(me && !me->init(inOwner, 0) )
77 {
78 me->release();
79 return NULL;
80 }
81
82 return me;
83}
84
85bool IOPMPowerStateQueue::init(OSObject *owner, Action action)
86{
87 if(!(super::init(owner, (IOEventSource::Action) action))) return false;
88
89 // Queue of powerstate changes
90 changes = NULL;
2d21ac55 91#ifndef __ppc__
0c530ab8
A
92 if (!(tmpLock = IOLockAlloc())) panic("IOPMPowerStateQueue::init can't alloc lock");
93#endif
55e303ae
A
94 return true;
95}
96
97
98bool IOPMPowerStateQueue::unIdleOccurred(IOService *inTarget, unsigned long inState)
99{
100 PowerChangeEntry *new_one = NULL;
101
102 new_one = (PowerChangeEntry *)IOMalloc(sizeof(PowerChangeEntry));
103 if(!new_one) return false;
104
105 new_one->actionType = IOPMPowerStateQueue::kUnIdle;
106 new_one->state = inState;
107 new_one->target = inTarget;
108
2d21ac55
A
109 // Change to queue
110#ifndef __ppc__
111 IOLockLock(tmpLock);
112#endif
113 OSEnqueueAtomic((void **)&changes, (void *)new_one, 0);
114#ifndef __ppc__
115 IOLockUnlock(tmpLock);
116#endif
117 signalWorkAvailable();
118
119 return true;
120}
121
122bool IOPMPowerStateQueue::featureChangeOccurred(
123 uint32_t inState,
124 IOService *inTarget)
125{
126 PowerChangeEntry *new_one = NULL;
127
128 new_one = (PowerChangeEntry *)IOMalloc(sizeof(PowerChangeEntry));
129 if(!new_one) return false;
130
131 new_one->actionType = IOPMPowerStateQueue::kPMFeatureChange;
132 new_one->state = inState;
133 new_one->target = inTarget;
134
55e303ae 135 // Change to queue
0c530ab8
A
136#ifdef __i386__
137 IOLockLock(tmpLock);
138#endif
55e303ae 139 OSEnqueueAtomic((void **)&changes, (void *)new_one, 0);
0c530ab8
A
140#ifdef __i386__
141 IOLockUnlock(tmpLock);
142#endif
55e303ae
A
143 signalWorkAvailable();
144
145 return true;
146}
147
2d21ac55 148
55e303ae
A
149// checkForWork() is called in a gated context
150bool IOPMPowerStateQueue::checkForWork()
151{
152 PowerChangeEntry *theNode;
2d21ac55 153 uint32_t theState;
55e303ae 154 IOService *theTarget;
2d21ac55 155 uint16_t theAction;
55e303ae
A
156
157 // Dequeue and process the state change request
2d21ac55 158#ifndef __ppc__
0c530ab8
A
159 IOLockLock(tmpLock);
160#endif
55e303ae
A
161 if((theNode = (PowerChangeEntry *)OSDequeueAtomic((void **)&changes, 0)))
162 {
2d21ac55 163#ifndef __ppc__
0c530ab8
A
164 IOLockUnlock(tmpLock);
165#endif
55e303ae
A
166 theState = theNode->state;
167 theTarget = theNode->target;
168 theAction = theNode->actionType;
169 IOFree((void *)theNode, sizeof(PowerChangeEntry));
170
171 switch (theAction)
172 {
173 case kUnIdle:
0c530ab8 174 theTarget->command_received((void *)theState, 0, 0, 0);
55e303ae 175 break;
2d21ac55
A
176
177 case kPMFeatureChange:
178 theTarget->messageClients(theState, theTarget);
179 break;
55e303ae
A
180 }
181 }
2d21ac55 182#ifndef __ppc__
0c530ab8
A
183 else {
184 IOLockUnlock(tmpLock);
185 }
186#endif
55e303ae
A
187 // Return true if there's more work to be done
188 if(changes) return true;
189 else return false;
190}