2 * Copyright (c) 1999-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1999-2000 Apple Computer, Inc. All rights reserved.
31 #include <machine/machine_routines.h>
32 #include <pexpert/pexpert.h>
35 #include <IOKit/IOLib.h>
36 #include <IOKit/IOPlatformExpert.h>
37 #include <IOKit/IOUserClient.h>
38 #include <IOKit/IOCPU.h>
41 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
43 kern_return_t
PE_cpu_start(cpu_id_t target
,
44 vm_offset_t start_paddr
, vm_offset_t arg_paddr
)
46 IOCPU
*targetCPU
= OSDynamicCast(IOCPU
, (OSObject
*)target
);
48 if (targetCPU
== 0) return KERN_FAILURE
;
49 return targetCPU
->startCPU(start_paddr
, arg_paddr
);
52 void PE_cpu_halt(cpu_id_t target
)
54 IOCPU
*targetCPU
= OSDynamicCast(IOCPU
, (OSObject
*)target
);
56 if (targetCPU
) targetCPU
->haltCPU();
59 void PE_cpu_signal(cpu_id_t source
, cpu_id_t target
)
61 IOCPU
*sourceCPU
= OSDynamicCast(IOCPU
, (OSObject
*)source
);
62 IOCPU
*targetCPU
= OSDynamicCast(IOCPU
, (OSObject
*)target
);
64 if (sourceCPU
&& targetCPU
) sourceCPU
->signalCPU(targetCPU
);
67 void PE_cpu_machine_init(cpu_id_t target
, boolean_t boot
)
69 IOCPU
*targetCPU
= OSDynamicCast(IOCPU
, (OSObject
*)target
);
71 if (targetCPU
) targetCPU
->initCPU(boot
);
74 void PE_cpu_machine_quiesce(cpu_id_t target
)
76 IOCPU
*targetCPU
= OSDynamicCast(IOCPU
, (OSObject
*)target
);
78 if (targetCPU
) targetCPU
->quiesceCPU();
81 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
83 #define super IOService
85 OSDefineMetaClassAndAbstractStructors(IOCPU
, IOService
);
86 OSMetaClassDefineReservedUnused(IOCPU
, 0);
87 OSMetaClassDefineReservedUnused(IOCPU
, 1);
88 OSMetaClassDefineReservedUnused(IOCPU
, 2);
89 OSMetaClassDefineReservedUnused(IOCPU
, 3);
90 OSMetaClassDefineReservedUnused(IOCPU
, 4);
91 OSMetaClassDefineReservedUnused(IOCPU
, 5);
92 OSMetaClassDefineReservedUnused(IOCPU
, 6);
93 OSMetaClassDefineReservedUnused(IOCPU
, 7);
95 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
97 static OSArray
*gIOCPUs
;
98 static const OSSymbol
*gIOCPUStateKey
;
99 static OSString
*gIOCPUStateNames
[kIOCPUStateCount
];
101 void IOCPUSleepKernel(void)
106 numCPUs
= gIOCPUs
->getCount();
111 target
= OSDynamicCast(IOCPU
, gIOCPUs
->getObject(cnt
));
112 if (target
->getCPUState() == kIOCPUStateRunning
) {
117 // Wake the other CPUs.
118 for (cnt
= 1; cnt
< numCPUs
; cnt
++) {
119 target
= OSDynamicCast(IOCPU
, gIOCPUs
->getObject(cnt
));
120 if (target
->getCPUState() == kIOCPUStateStopped
) {
121 processor_start(target
->getMachProcessor());
126 void IOCPU::initCPUs(void)
129 gIOCPUs
= OSArray::withCapacity(1);
131 gIOCPUStateKey
= OSSymbol::withCStringNoCopy("IOCPUState");
133 gIOCPUStateNames
[kIOCPUStateUnregistered
] =
134 OSString::withCStringNoCopy("Unregistered");
135 gIOCPUStateNames
[kIOCPUStateUninitalized
] =
136 OSString::withCStringNoCopy("Uninitalized");
137 gIOCPUStateNames
[kIOCPUStateStopped
] =
138 OSString::withCStringNoCopy("Stopped");
139 gIOCPUStateNames
[kIOCPUStateRunning
] =
140 OSString::withCStringNoCopy("Running");
144 bool IOCPU::start(IOService
*provider
)
146 OSData
*busFrequency
, *cpuFrequency
, *timebaseFrequency
;
148 if (!super::start(provider
)) return false;
155 gIOCPUs
->setObject(this);
157 // Correct the bus, cpu and timebase frequencies in the device tree.
158 if (gPEClockFrequencyInfo
.bus_frequency_hz
< 0x100000000ULL
) {
159 busFrequency
= OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo
.bus_clock_rate_hz
, 4);
161 busFrequency
= OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo
.bus_frequency_hz
, 8);
163 provider
->setProperty("bus-frequency", busFrequency
);
164 busFrequency
->release();
166 if (gPEClockFrequencyInfo
.cpu_frequency_hz
< 0x100000000ULL
) {
167 cpuFrequency
= OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo
.cpu_clock_rate_hz
, 4);
169 cpuFrequency
= OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo
.cpu_frequency_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 super::setProperty("IOCPUID", (UInt32
)this, 32);
181 setCPUState(kIOCPUStateUnregistered
);
186 OSObject
*IOCPU::getProperty(const OSSymbol
*aKey
) const
188 if (aKey
== gIOCPUStateKey
) return gIOCPUStateNames
[_cpuState
];
190 return super::getProperty(aKey
);
193 bool IOCPU::setProperty(const OSSymbol
*aKey
, OSObject
*anObject
)
197 if (aKey
== gIOCPUStateKey
) {
198 stateStr
= OSDynamicCast(OSString
, anObject
);
199 if (stateStr
== 0) return false;
201 if (_cpuNumber
== 0) return false;
203 if (stateStr
->isEqualTo("running")) {
204 if (_cpuState
== kIOCPUStateStopped
) {
205 processor_start(machProcessor
);
206 } else if (_cpuState
!= kIOCPUStateRunning
) {
209 } else if (stateStr
->isEqualTo("stopped")) {
210 if (_cpuState
== kIOCPUStateRunning
) {
212 } else if (_cpuState
!= kIOCPUStateStopped
) {
220 return super::setProperty(aKey
, anObject
);
223 bool IOCPU::serializeProperties(OSSerialize
*serialize
) const
225 super::setProperty(gIOCPUStateKey
, gIOCPUStateNames
[_cpuState
]);
227 return super::serializeProperties(serialize
);
230 IOReturn
IOCPU::setProperties(OSObject
*properties
)
232 OSDictionary
*dict
= OSDynamicCast(OSDictionary
, properties
);
236 if (dict
== 0) return kIOReturnUnsupported
;
238 stateStr
= OSDynamicCast(OSString
, dict
->getObject(gIOCPUStateKey
));
240 result
= IOUserClient::clientHasPrivilege(current_task(), kIOClientPrivilegeAdministrator
);
241 if (result
!= kIOReturnSuccess
) return result
;
243 if (setProperty(gIOCPUStateKey
, stateStr
)) return kIOReturnSuccess
;
245 return kIOReturnUnsupported
;
248 return kIOReturnUnsupported
;
251 void IOCPU::signalCPU(IOCPU */
*target*/
)
255 void IOCPU::enableCPUTimeBase(bool /*enable*/)
259 UInt32
IOCPU::getCPUNumber(void)
264 void IOCPU::setCPUNumber(UInt32 cpuNumber
)
266 _cpuNumber
= cpuNumber
;
267 super::setProperty("IOCPUNumber", _cpuNumber
, 32);
270 UInt32
IOCPU::getCPUState(void)
275 void IOCPU::setCPUState(UInt32 cpuState
)
277 if (cpuState
< kIOCPUStateCount
) {
278 _cpuState
= cpuState
;
282 OSArray
*IOCPU::getCPUGroup(void)
287 UInt32
IOCPU::getCPUGroupSize(void)
289 return _cpuGroup
->getCount();
292 processor_t
IOCPU::getMachProcessor(void)
294 return machProcessor
;
298 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
301 #define super IOInterruptController
303 OSDefineMetaClassAndStructors(IOCPUInterruptController
, IOInterruptController
);
305 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 0);
306 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 1);
307 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 2);
308 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 3);
309 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 4);
310 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 5);
314 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
317 IOReturn
IOCPUInterruptController::initCPUInterruptController(int sources
)
321 if (!super::init()) return kIOReturnInvalid
;
325 cpus
= (IOCPU
**)IOMalloc(numCPUs
* sizeof(IOCPU
*));
326 if (cpus
== 0) return kIOReturnNoMemory
;
327 bzero(cpus
, numCPUs
* sizeof(IOCPU
*));
329 vectors
= (IOInterruptVector
*)IOMalloc(numCPUs
* sizeof(IOInterruptVector
));
330 if (vectors
== 0) return kIOReturnNoMemory
;
331 bzero(vectors
, numCPUs
* sizeof(IOInterruptVector
));
333 // Allocate locks for the
334 for (cnt
= 0; cnt
< numCPUs
; cnt
++) {
335 vectors
[cnt
].interruptLock
= IOLockAlloc();
336 if (vectors
[cnt
].interruptLock
== NULL
) {
337 for (cnt
= 0; cnt
< numCPUs
; cnt
++) {
338 if (vectors
[cnt
].interruptLock
!= NULL
)
339 IOLockFree(vectors
[cnt
].interruptLock
);
341 return kIOReturnNoResources
;
345 ml_init_max_cpus(numCPUs
);
347 return kIOReturnSuccess
;
350 void IOCPUInterruptController::registerCPUInterruptController(void)
354 getPlatform()->registerInterruptController(gPlatformInterruptControllerName
,
358 void IOCPUInterruptController::setCPUInterruptProperties(IOService
*service
)
366 // Create the interrupt specifer array.
367 specifier
= OSArray::withCapacity(numCPUs
);
368 for (cnt
= 0; cnt
< numCPUs
; cnt
++) {
370 tmpData
= OSData::withBytes(&tmpLong
, sizeof(tmpLong
));
371 specifier
->setObject(tmpData
);
375 // Create the interrupt controller array.
376 controller
= OSArray::withCapacity(numCPUs
);
377 for (cnt
= 0; cnt
< numCPUs
; cnt
++) {
378 controller
->setObject(gPlatformInterruptControllerName
);
381 // Put the two arrays into the property table.
382 service
->setProperty(gIOInterruptControllersKey
, controller
);
383 service
->setProperty(gIOInterruptSpecifiersKey
, specifier
);
384 controller
->release();
385 specifier
->release();
388 void IOCPUInterruptController::enableCPUInterrupt(IOCPU
*cpu
)
390 ml_install_interrupt_handler(cpu
, cpu
->getCPUNumber(), this,
391 (IOInterruptHandler
)&IOCPUInterruptController::handleInterrupt
, 0);
395 if (enabledCPUs
== numCPUs
) thread_wakeup(this);
398 IOReturn
IOCPUInterruptController::registerInterrupt(IOService
*nub
,
401 IOInterruptHandler handler
,
404 IOInterruptVector
*vector
;
406 if (source
>= numCPUs
) return kIOReturnNoResources
;
408 vector
= &vectors
[source
];
410 // Get the lock for this vector.
411 IOTakeLock(vector
->interruptLock
);
413 // Make sure the vector is not in use.
414 if (vector
->interruptRegistered
) {
415 IOUnlock(vector
->interruptLock
);
416 return kIOReturnNoResources
;
419 // Fill in vector with the client's info.
420 vector
->handler
= handler
;
422 vector
->source
= source
;
423 vector
->target
= target
;
424 vector
->refCon
= refCon
;
426 // Get the vector ready. It starts hard disabled.
427 vector
->interruptDisabledHard
= 1;
428 vector
->interruptDisabledSoft
= 1;
429 vector
->interruptRegistered
= 1;
431 IOUnlock(vector
->interruptLock
);
433 if (enabledCPUs
!= numCPUs
) {
434 assert_wait(this, THREAD_UNINT
);
435 thread_block(THREAD_CONTINUE_NULL
);
438 return kIOReturnSuccess
;
441 IOReturn
IOCPUInterruptController::getInterruptType(IOService */
*nub*/
,
445 if (interruptType
== 0) return kIOReturnBadArgument
;
447 *interruptType
= kIOInterruptTypeLevel
;
449 return kIOReturnSuccess
;
452 IOReturn
IOCPUInterruptController::enableInterrupt(IOService */
*nub*/
,
455 // ml_set_interrupts_enabled(true);
456 return kIOReturnSuccess
;
459 IOReturn
IOCPUInterruptController::disableInterrupt(IOService */
*nub*/
,
462 // ml_set_interrupts_enabled(false);
463 return kIOReturnSuccess
;
466 IOReturn
IOCPUInterruptController::causeInterrupt(IOService */
*nub*/
,
469 ml_cause_interrupt();
470 return kIOReturnSuccess
;
473 IOReturn
IOCPUInterruptController::handleInterrupt(void */
*refCon*/
,
477 IOInterruptVector
*vector
;
479 vector
= &vectors
[source
];
481 if (!vector
->interruptRegistered
) return kIOReturnInvalid
;
483 vector
->handler(vector
->target
, vector
->refCon
,
484 vector
->nub
, vector
->source
);
486 return kIOReturnSuccess
;
489 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */