2 * Copyright (c) 1999-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. 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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1999-2000 Apple Computer, Inc. All rights reserved.
36 #include <machine/machine_routines.h>
37 #include <pexpert/pexpert.h>
40 #include <IOKit/IOLib.h>
41 #include <IOKit/IOPlatformExpert.h>
42 #include <IOKit/IOUserClient.h>
43 #include <IOKit/IOCPU.h>
46 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
48 kern_return_t
PE_cpu_start(cpu_id_t target
,
49 vm_offset_t start_paddr
, vm_offset_t arg_paddr
)
51 IOCPU
*targetCPU
= OSDynamicCast(IOCPU
, (OSObject
*)target
);
53 if (targetCPU
== 0) return KERN_FAILURE
;
54 return targetCPU
->startCPU(start_paddr
, arg_paddr
);
57 void PE_cpu_halt(cpu_id_t target
)
59 IOCPU
*targetCPU
= OSDynamicCast(IOCPU
, (OSObject
*)target
);
61 if (targetCPU
) targetCPU
->haltCPU();
64 void PE_cpu_signal(cpu_id_t source
, cpu_id_t target
)
66 IOCPU
*sourceCPU
= OSDynamicCast(IOCPU
, (OSObject
*)source
);
67 IOCPU
*targetCPU
= OSDynamicCast(IOCPU
, (OSObject
*)target
);
69 if (sourceCPU
&& targetCPU
) sourceCPU
->signalCPU(targetCPU
);
72 void PE_cpu_machine_init(cpu_id_t target
, boolean_t boot
)
74 IOCPU
*targetCPU
= OSDynamicCast(IOCPU
, (OSObject
*)target
);
76 if (targetCPU
) targetCPU
->initCPU(boot
);
79 void PE_cpu_machine_quiesce(cpu_id_t target
)
81 IOCPU
*targetCPU
= OSDynamicCast(IOCPU
, (OSObject
*)target
);
83 if (targetCPU
) targetCPU
->quiesceCPU();
86 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
88 #define super IOService
90 OSDefineMetaClassAndAbstractStructors(IOCPU
, IOService
);
91 OSMetaClassDefineReservedUnused(IOCPU
, 0);
92 OSMetaClassDefineReservedUnused(IOCPU
, 1);
93 OSMetaClassDefineReservedUnused(IOCPU
, 2);
94 OSMetaClassDefineReservedUnused(IOCPU
, 3);
95 OSMetaClassDefineReservedUnused(IOCPU
, 4);
96 OSMetaClassDefineReservedUnused(IOCPU
, 5);
97 OSMetaClassDefineReservedUnused(IOCPU
, 6);
98 OSMetaClassDefineReservedUnused(IOCPU
, 7);
100 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
102 static OSArray
*gIOCPUs
;
103 static const OSSymbol
*gIOCPUStateKey
;
104 static OSString
*gIOCPUStateNames
[kIOCPUStateCount
];
106 void IOCPUSleepKernel(void)
111 numCPUs
= gIOCPUs
->getCount();
116 target
= OSDynamicCast(IOCPU
, gIOCPUs
->getObject(cnt
));
117 if (target
->getCPUState() == kIOCPUStateRunning
) {
122 // Wake the other CPUs.
123 for (cnt
= 1; cnt
< numCPUs
; cnt
++) {
124 target
= OSDynamicCast(IOCPU
, gIOCPUs
->getObject(cnt
));
125 if (target
->getCPUState() == kIOCPUStateStopped
) {
126 processor_start(target
->getMachProcessor());
131 void IOCPU::initCPUs(void)
134 gIOCPUs
= OSArray::withCapacity(1);
136 gIOCPUStateKey
= OSSymbol::withCStringNoCopy("IOCPUState");
138 gIOCPUStateNames
[kIOCPUStateUnregistered
] =
139 OSString::withCStringNoCopy("Unregistered");
140 gIOCPUStateNames
[kIOCPUStateUninitalized
] =
141 OSString::withCStringNoCopy("Uninitalized");
142 gIOCPUStateNames
[kIOCPUStateStopped
] =
143 OSString::withCStringNoCopy("Stopped");
144 gIOCPUStateNames
[kIOCPUStateRunning
] =
145 OSString::withCStringNoCopy("Running");
149 bool IOCPU::start(IOService
*provider
)
151 OSData
*busFrequency
, *cpuFrequency
, *timebaseFrequency
;
153 if (!super::start(provider
)) return false;
160 gIOCPUs
->setObject(this);
162 // Correct the bus, cpu and timebase frequencies in the device tree.
163 if (gPEClockFrequencyInfo
.bus_frequency_hz
< 0x100000000ULL
) {
164 busFrequency
= OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo
.bus_clock_rate_hz
, 4);
166 busFrequency
= OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo
.bus_frequency_hz
, 8);
168 provider
->setProperty("bus-frequency", busFrequency
);
169 busFrequency
->release();
171 if (gPEClockFrequencyInfo
.cpu_frequency_hz
< 0x100000000ULL
) {
172 cpuFrequency
= OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo
.cpu_clock_rate_hz
, 4);
174 cpuFrequency
= OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo
.cpu_frequency_hz
, 8);
176 provider
->setProperty("clock-frequency", cpuFrequency
);
177 cpuFrequency
->release();
179 timebaseFrequency
= OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo
.timebase_frequency_hz
, 4);
180 provider
->setProperty("timebase-frequency", timebaseFrequency
);
181 timebaseFrequency
->release();
183 super::setProperty("IOCPUID", (UInt32
)this, 32);
186 setCPUState(kIOCPUStateUnregistered
);
191 OSObject
*IOCPU::getProperty(const OSSymbol
*aKey
) const
193 if (aKey
== gIOCPUStateKey
) return gIOCPUStateNames
[_cpuState
];
195 return super::getProperty(aKey
);
198 bool IOCPU::setProperty(const OSSymbol
*aKey
, OSObject
*anObject
)
202 if (aKey
== gIOCPUStateKey
) {
203 stateStr
= OSDynamicCast(OSString
, anObject
);
204 if (stateStr
== 0) return false;
206 if (_cpuNumber
== 0) return false;
208 if (stateStr
->isEqualTo("running")) {
209 if (_cpuState
== kIOCPUStateStopped
) {
210 processor_start(machProcessor
);
211 } else if (_cpuState
!= kIOCPUStateRunning
) {
214 } else if (stateStr
->isEqualTo("stopped")) {
215 if (_cpuState
== kIOCPUStateRunning
) {
217 } else if (_cpuState
!= kIOCPUStateStopped
) {
225 return super::setProperty(aKey
, anObject
);
228 bool IOCPU::serializeProperties(OSSerialize
*serialize
) const
230 super::setProperty(gIOCPUStateKey
, gIOCPUStateNames
[_cpuState
]);
232 return super::serializeProperties(serialize
);
235 IOReturn
IOCPU::setProperties(OSObject
*properties
)
237 OSDictionary
*dict
= OSDynamicCast(OSDictionary
, properties
);
241 if (dict
== 0) return kIOReturnUnsupported
;
243 stateStr
= OSDynamicCast(OSString
, dict
->getObject(gIOCPUStateKey
));
245 result
= IOUserClient::clientHasPrivilege(current_task(), kIOClientPrivilegeAdministrator
);
246 if (result
!= kIOReturnSuccess
) return result
;
248 if (setProperty(gIOCPUStateKey
, stateStr
)) return kIOReturnSuccess
;
250 return kIOReturnUnsupported
;
253 return kIOReturnUnsupported
;
256 void IOCPU::signalCPU(IOCPU */
*target*/
)
260 void IOCPU::enableCPUTimeBase(bool /*enable*/)
264 UInt32
IOCPU::getCPUNumber(void)
269 void IOCPU::setCPUNumber(UInt32 cpuNumber
)
271 _cpuNumber
= cpuNumber
;
272 super::setProperty("IOCPUNumber", _cpuNumber
, 32);
275 UInt32
IOCPU::getCPUState(void)
280 void IOCPU::setCPUState(UInt32 cpuState
)
282 if (cpuState
< kIOCPUStateCount
) {
283 _cpuState
= cpuState
;
287 OSArray
*IOCPU::getCPUGroup(void)
292 UInt32
IOCPU::getCPUGroupSize(void)
294 return _cpuGroup
->getCount();
297 processor_t
IOCPU::getMachProcessor(void)
299 return machProcessor
;
303 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
306 #define super IOInterruptController
308 OSDefineMetaClassAndStructors(IOCPUInterruptController
, IOInterruptController
);
310 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 0);
311 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 1);
312 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 2);
313 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 3);
314 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 4);
315 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 5);
319 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
322 IOReturn
IOCPUInterruptController::initCPUInterruptController(int sources
)
326 if (!super::init()) return kIOReturnInvalid
;
330 cpus
= (IOCPU
**)IOMalloc(numCPUs
* sizeof(IOCPU
*));
331 if (cpus
== 0) return kIOReturnNoMemory
;
332 bzero(cpus
, numCPUs
* sizeof(IOCPU
*));
334 vectors
= (IOInterruptVector
*)IOMalloc(numCPUs
* sizeof(IOInterruptVector
));
335 if (vectors
== 0) return kIOReturnNoMemory
;
336 bzero(vectors
, numCPUs
* sizeof(IOInterruptVector
));
338 // Allocate locks for the
339 for (cnt
= 0; cnt
< numCPUs
; cnt
++) {
340 vectors
[cnt
].interruptLock
= IOLockAlloc();
341 if (vectors
[cnt
].interruptLock
== NULL
) {
342 for (cnt
= 0; cnt
< numCPUs
; cnt
++) {
343 if (vectors
[cnt
].interruptLock
!= NULL
)
344 IOLockFree(vectors
[cnt
].interruptLock
);
346 return kIOReturnNoResources
;
350 ml_init_max_cpus(numCPUs
);
352 return kIOReturnSuccess
;
355 void IOCPUInterruptController::registerCPUInterruptController(void)
359 getPlatform()->registerInterruptController(gPlatformInterruptControllerName
,
363 void IOCPUInterruptController::setCPUInterruptProperties(IOService
*service
)
371 // Create the interrupt specifer array.
372 specifier
= OSArray::withCapacity(numCPUs
);
373 for (cnt
= 0; cnt
< numCPUs
; cnt
++) {
375 tmpData
= OSData::withBytes(&tmpLong
, sizeof(tmpLong
));
376 specifier
->setObject(tmpData
);
380 // Create the interrupt controller array.
381 controller
= OSArray::withCapacity(numCPUs
);
382 for (cnt
= 0; cnt
< numCPUs
; cnt
++) {
383 controller
->setObject(gPlatformInterruptControllerName
);
386 // Put the two arrays into the property table.
387 service
->setProperty(gIOInterruptControllersKey
, controller
);
388 service
->setProperty(gIOInterruptSpecifiersKey
, specifier
);
389 controller
->release();
390 specifier
->release();
393 void IOCPUInterruptController::enableCPUInterrupt(IOCPU
*cpu
)
395 ml_install_interrupt_handler(cpu
, cpu
->getCPUNumber(), this,
396 (IOInterruptHandler
)&IOCPUInterruptController::handleInterrupt
, 0);
400 if (enabledCPUs
== numCPUs
) thread_wakeup(this);
403 IOReturn
IOCPUInterruptController::registerInterrupt(IOService
*nub
,
406 IOInterruptHandler handler
,
409 IOInterruptVector
*vector
;
411 if (source
>= numCPUs
) return kIOReturnNoResources
;
413 vector
= &vectors
[source
];
415 // Get the lock for this vector.
416 IOTakeLock(vector
->interruptLock
);
418 // Make sure the vector is not in use.
419 if (vector
->interruptRegistered
) {
420 IOUnlock(vector
->interruptLock
);
421 return kIOReturnNoResources
;
424 // Fill in vector with the client's info.
425 vector
->handler
= handler
;
427 vector
->source
= source
;
428 vector
->target
= target
;
429 vector
->refCon
= refCon
;
431 // Get the vector ready. It starts hard disabled.
432 vector
->interruptDisabledHard
= 1;
433 vector
->interruptDisabledSoft
= 1;
434 vector
->interruptRegistered
= 1;
436 IOUnlock(vector
->interruptLock
);
438 if (enabledCPUs
!= numCPUs
) {
439 assert_wait(this, THREAD_UNINT
);
440 thread_block(THREAD_CONTINUE_NULL
);
443 return kIOReturnSuccess
;
446 IOReturn
IOCPUInterruptController::getInterruptType(IOService */
*nub*/
,
450 if (interruptType
== 0) return kIOReturnBadArgument
;
452 *interruptType
= kIOInterruptTypeLevel
;
454 return kIOReturnSuccess
;
457 IOReturn
IOCPUInterruptController::enableInterrupt(IOService */
*nub*/
,
460 // ml_set_interrupts_enabled(true);
461 return kIOReturnSuccess
;
464 IOReturn
IOCPUInterruptController::disableInterrupt(IOService */
*nub*/
,
467 // ml_set_interrupts_enabled(false);
468 return kIOReturnSuccess
;
471 IOReturn
IOCPUInterruptController::causeInterrupt(IOService */
*nub*/
,
474 ml_cause_interrupt();
475 return kIOReturnSuccess
;
478 IOReturn
IOCPUInterruptController::handleInterrupt(void */
*refCon*/
,
482 IOInterruptVector
*vector
;
484 vector
= &vectors
[source
];
486 if (!vector
->interruptRegistered
) return kIOReturnInvalid
;
488 vector
->handler(vector
->target
, vector
->refCon
,
489 vector
->nub
, vector
->source
);
491 return kIOReturnSuccess
;
494 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */