2 * Copyright (c) 1999-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1999-2000 Apple Computer, Inc. All rights reserved.
33 #include <machine/machine_routines.h>
34 #include <pexpert/pexpert.h>
37 #include <IOKit/IOLib.h>
38 #include <IOKit/IOPlatformExpert.h>
39 #include <IOKit/IOUserClient.h>
40 #include <IOKit/IOCPU.h>
43 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
45 kern_return_t
PE_cpu_start(cpu_id_t target
,
46 vm_offset_t start_paddr
, vm_offset_t arg_paddr
)
48 IOCPU
*targetCPU
= OSDynamicCast(IOCPU
, (OSObject
*)target
);
50 if (targetCPU
== 0) return KERN_FAILURE
;
51 return targetCPU
->startCPU(start_paddr
, arg_paddr
);
54 void PE_cpu_halt(cpu_id_t target
)
56 IOCPU
*targetCPU
= OSDynamicCast(IOCPU
, (OSObject
*)target
);
58 if (targetCPU
) targetCPU
->haltCPU();
61 void PE_cpu_signal(cpu_id_t source
, cpu_id_t target
)
63 IOCPU
*sourceCPU
= OSDynamicCast(IOCPU
, (OSObject
*)source
);
64 IOCPU
*targetCPU
= OSDynamicCast(IOCPU
, (OSObject
*)target
);
66 if (sourceCPU
&& targetCPU
) sourceCPU
->signalCPU(targetCPU
);
69 void PE_cpu_machine_init(cpu_id_t target
, boolean_t boot
)
71 IOCPU
*targetCPU
= OSDynamicCast(IOCPU
, (OSObject
*)target
);
73 if (targetCPU
) targetCPU
->initCPU(boot
);
76 void PE_cpu_machine_quiesce(cpu_id_t target
)
78 IOCPU
*targetCPU
= OSDynamicCast(IOCPU
, (OSObject
*)target
);
80 if (targetCPU
) targetCPU
->quiesceCPU();
83 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
85 #define super IOService
87 OSDefineMetaClassAndAbstractStructors(IOCPU
, IOService
);
88 OSMetaClassDefineReservedUnused(IOCPU
, 0);
89 OSMetaClassDefineReservedUnused(IOCPU
, 1);
90 OSMetaClassDefineReservedUnused(IOCPU
, 2);
91 OSMetaClassDefineReservedUnused(IOCPU
, 3);
92 OSMetaClassDefineReservedUnused(IOCPU
, 4);
93 OSMetaClassDefineReservedUnused(IOCPU
, 5);
94 OSMetaClassDefineReservedUnused(IOCPU
, 6);
95 OSMetaClassDefineReservedUnused(IOCPU
, 7);
97 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
99 static OSArray
*gIOCPUs
;
100 static const OSSymbol
*gIOCPUStateKey
;
101 static OSString
*gIOCPUStateNames
[kIOCPUStateCount
];
103 void IOCPUSleepKernel(void)
108 numCPUs
= gIOCPUs
->getCount();
113 target
= OSDynamicCast(IOCPU
, gIOCPUs
->getObject(cnt
));
114 if (target
->getCPUState() == kIOCPUStateRunning
) {
119 // Wake the other CPUs.
120 for (cnt
= 1; cnt
< numCPUs
; cnt
++) {
121 target
= OSDynamicCast(IOCPU
, gIOCPUs
->getObject(cnt
));
122 if (target
->getCPUState() == kIOCPUStateStopped
) {
123 processor_start(target
->getMachProcessor());
128 void IOCPU::initCPUs(void)
131 gIOCPUs
= OSArray::withCapacity(1);
133 gIOCPUStateKey
= OSSymbol::withCStringNoCopy("IOCPUState");
135 gIOCPUStateNames
[kIOCPUStateUnregistered
] =
136 OSString::withCStringNoCopy("Unregistered");
137 gIOCPUStateNames
[kIOCPUStateUninitalized
] =
138 OSString::withCStringNoCopy("Uninitalized");
139 gIOCPUStateNames
[kIOCPUStateStopped
] =
140 OSString::withCStringNoCopy("Stopped");
141 gIOCPUStateNames
[kIOCPUStateRunning
] =
142 OSString::withCStringNoCopy("Running");
146 bool IOCPU::start(IOService
*provider
)
148 OSData
*busFrequency
, *cpuFrequency
, *timebaseFrequency
;
150 if (!super::start(provider
)) return false;
157 gIOCPUs
->setObject(this);
159 // Correct the bus, cpu and timebase frequencies in the device tree.
160 if (gPEClockFrequencyInfo
.bus_frequency_hz
< 0x100000000ULL
)
161 busFrequency
= OSData::withBytesNoCopy((void *)((char *)&gPEClockFrequencyInfo
.bus_frequency_hz
+ 4), 4);
163 busFrequency
= OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo
.bus_clock_rate_hz
, 8);
164 provider
->setProperty("bus-frequency", busFrequency
);
165 busFrequency
->release();
167 if (gPEClockFrequencyInfo
.cpu_frequency_hz
< 0x100000000ULL
)
168 cpuFrequency
= OSData::withBytesNoCopy((void *)((char *)&gPEClockFrequencyInfo
.cpu_frequency_hz
+ 4), 4);
170 cpuFrequency
= OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo
.cpu_clock_rate_hz
, 8);
171 provider
->setProperty("clock-frequency", cpuFrequency
);
172 cpuFrequency
->release();
174 timebaseFrequency
= OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo
.timebase_frequency_hz
, 4);
175 provider
->setProperty("timebase-frequency", timebaseFrequency
);
176 timebaseFrequency
->release();
178 setProperty("IOCPUID", (UInt32
)this, 32);
181 setCPUState(kIOCPUStateUnregistered
);
186 IOReturn
IOCPU::setProperties(OSObject
*properties
)
188 OSDictionary
*dict
= OSDynamicCast(OSDictionary
, properties
);
191 if (dict
== 0) return kIOReturnUnsupported
;
193 stateStr
= OSDynamicCast(OSString
, dict
->getObject(gIOCPUStateKey
));
195 if (!IOUserClient::clientHasPrivilege(current_task(), "root"))
196 return kIOReturnNotPrivileged
;
198 if (_cpuNumber
== 0) return kIOReturnUnsupported
;
200 if (stateStr
->isEqualTo("running")) {
201 if (_cpuState
== kIOCPUStateStopped
) {
202 processor_start(machProcessor
);
203 } else if (_cpuState
!= kIOCPUStateRunning
) {
204 return kIOReturnUnsupported
;
206 } else if (stateStr
->isEqualTo("stopped")) {
207 if (_cpuState
== kIOCPUStateRunning
) {
209 } else if (_cpuState
!= kIOCPUStateStopped
) {
210 return kIOReturnUnsupported
;
212 } else return kIOReturnUnsupported
;
214 return kIOReturnSuccess
;
217 return kIOReturnUnsupported
;
220 void IOCPU::signalCPU(IOCPU */
*target*/
)
224 void IOCPU::enableCPUTimeBase(bool /*enable*/)
228 UInt32
IOCPU::getCPUNumber(void)
233 void IOCPU::setCPUNumber(UInt32 cpuNumber
)
235 _cpuNumber
= cpuNumber
;
236 setProperty("IOCPUNumber", _cpuNumber
, 32);
239 UInt32
IOCPU::getCPUState(void)
244 void IOCPU::setCPUState(UInt32 cpuState
)
246 if ((cpuState
>= 0) && (cpuState
< kIOCPUStateCount
)) {
247 _cpuState
= cpuState
;
248 setProperty(gIOCPUStateKey
, gIOCPUStateNames
[cpuState
]);
252 OSArray
*IOCPU::getCPUGroup(void)
257 UInt32
IOCPU::getCPUGroupSize(void)
259 return _cpuGroup
->getCount();
262 processor_t
IOCPU::getMachProcessor(void)
264 return machProcessor
;
268 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
271 #define super IOInterruptController
273 OSDefineMetaClassAndStructors(IOCPUInterruptController
, IOInterruptController
);
275 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 0);
276 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 1);
277 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 2);
278 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 3);
279 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 4);
280 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 5);
284 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
287 IOReturn
IOCPUInterruptController::initCPUInterruptController(int sources
)
291 if (!super::init()) return kIOReturnInvalid
;
295 cpus
= (IOCPU
**)IOMalloc(numCPUs
* sizeof(IOCPU
*));
296 if (cpus
== 0) return kIOReturnNoMemory
;
297 bzero(cpus
, numCPUs
* sizeof(IOCPU
*));
299 vectors
= (IOInterruptVector
*)IOMalloc(numCPUs
* sizeof(IOInterruptVector
));
300 if (vectors
== 0) return kIOReturnNoMemory
;
301 bzero(vectors
, numCPUs
* sizeof(IOInterruptVector
));
303 // Allocate locks for the
304 for (cnt
= 0; cnt
< numCPUs
; cnt
++) {
305 vectors
[cnt
].interruptLock
= IOLockAlloc();
306 if (vectors
[cnt
].interruptLock
== NULL
) {
307 for (cnt
= 0; cnt
< numCPUs
; cnt
++) {
308 if (vectors
[cnt
].interruptLock
!= NULL
)
309 IOLockFree(vectors
[cnt
].interruptLock
);
311 return kIOReturnNoResources
;
315 ml_init_max_cpus(numCPUs
);
317 return kIOReturnSuccess
;
320 void IOCPUInterruptController::registerCPUInterruptController(void)
324 getPlatform()->registerInterruptController(gPlatformInterruptControllerName
,
328 void IOCPUInterruptController::setCPUInterruptProperties(IOService
*service
)
336 // Create the interrupt specifer array.
337 specifier
= OSArray::withCapacity(numCPUs
);
338 for (cnt
= 0; cnt
< numCPUs
; cnt
++) {
340 tmpData
= OSData::withBytes(&tmpLong
, sizeof(tmpLong
));
341 specifier
->setObject(tmpData
);
345 // Create the interrupt controller array.
346 controller
= OSArray::withCapacity(numCPUs
);
347 for (cnt
= 0; cnt
< numCPUs
; cnt
++) {
348 controller
->setObject(gPlatformInterruptControllerName
);
351 // Put the two arrays into the property table.
352 service
->setProperty(gIOInterruptControllersKey
, controller
);
353 service
->setProperty(gIOInterruptSpecifiersKey
, specifier
);
354 controller
->release();
355 specifier
->release();
358 void IOCPUInterruptController::enableCPUInterrupt(IOCPU
*cpu
)
360 ml_install_interrupt_handler(cpu
, cpu
->getCPUNumber(), this,
361 (IOInterruptHandler
)&IOCPUInterruptController::handleInterrupt
, 0);
365 if (enabledCPUs
== numCPUs
) thread_wakeup(this);
368 IOReturn
IOCPUInterruptController::registerInterrupt(IOService
*nub
,
371 IOInterruptHandler handler
,
374 IOInterruptVector
*vector
;
376 if (source
>= numCPUs
) return kIOReturnNoResources
;
378 vector
= &vectors
[source
];
380 // Get the lock for this vector.
381 IOTakeLock(vector
->interruptLock
);
383 // Make sure the vector is not in use.
384 if (vector
->interruptRegistered
) {
385 IOUnlock(vector
->interruptLock
);
386 return kIOReturnNoResources
;
389 // Fill in vector with the client's info.
390 vector
->handler
= handler
;
392 vector
->source
= source
;
393 vector
->target
= target
;
394 vector
->refCon
= refCon
;
396 // Get the vector ready. It starts hard disabled.
397 vector
->interruptDisabledHard
= 1;
398 vector
->interruptDisabledSoft
= 1;
399 vector
->interruptRegistered
= 1;
401 IOUnlock(vector
->interruptLock
);
403 if (enabledCPUs
!= numCPUs
) {
404 assert_wait(this, THREAD_UNINT
);
405 thread_block(THREAD_CONTINUE_NULL
);
408 return kIOReturnSuccess
;
411 IOReturn
IOCPUInterruptController::getInterruptType(IOService */
*nub*/
,
415 if (interruptType
== 0) return kIOReturnBadArgument
;
417 *interruptType
= kIOInterruptTypeLevel
;
419 return kIOReturnSuccess
;
422 IOReturn
IOCPUInterruptController::enableInterrupt(IOService */
*nub*/
,
425 // ml_set_interrupts_enabled(true);
426 return kIOReturnSuccess
;
429 IOReturn
IOCPUInterruptController::disableInterrupt(IOService */
*nub*/
,
432 // ml_set_interrupts_enabled(false);
433 return kIOReturnSuccess
;
436 IOReturn
IOCPUInterruptController::causeInterrupt(IOService */
*nub*/
,
439 ml_cause_interrupt();
440 return kIOReturnSuccess
;
443 IOReturn
IOCPUInterruptController::handleInterrupt(void */
*refCon*/
,
447 IOInterruptVector
*vector
;
449 vector
= &vectors
[source
];
451 if (!vector
->interruptRegistered
) return kIOReturnInvalid
;
453 vector
->handler(vector
->target
, vector
->refCon
,
454 vector
->nub
, vector
->source
);
456 return kIOReturnSuccess
;
459 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */