2 * Copyright (c) 1999-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1999-2000 Apple Computer, Inc. All rights reserved.
30 #include <machine/machine_routines.h>
31 #include <pexpert/pexpert.h>
34 #include <IOKit/IOLib.h>
35 #include <IOKit/IOPlatformExpert.h>
36 #include <IOKit/IOUserClient.h>
37 #include <IOKit/IOCPU.h>
40 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
42 kern_return_t
PE_cpu_start(cpu_id_t target
,
43 vm_offset_t start_paddr
, vm_offset_t arg_paddr
)
45 IOCPU
*targetCPU
= OSDynamicCast(IOCPU
, (OSObject
*)target
);
47 if (targetCPU
== 0) return KERN_FAILURE
;
48 return targetCPU
->startCPU(start_paddr
, arg_paddr
);
51 void PE_cpu_halt(cpu_id_t target
)
53 IOCPU
*targetCPU
= OSDynamicCast(IOCPU
, (OSObject
*)target
);
55 if (targetCPU
) targetCPU
->haltCPU();
58 void PE_cpu_signal(cpu_id_t source
, cpu_id_t target
)
60 IOCPU
*sourceCPU
= OSDynamicCast(IOCPU
, (OSObject
*)source
);
61 IOCPU
*targetCPU
= OSDynamicCast(IOCPU
, (OSObject
*)target
);
63 if (sourceCPU
&& targetCPU
) sourceCPU
->signalCPU(targetCPU
);
66 void PE_cpu_machine_init(cpu_id_t target
, boolean_t boot
)
68 IOCPU
*targetCPU
= OSDynamicCast(IOCPU
, (OSObject
*)target
);
70 if (targetCPU
) targetCPU
->initCPU(boot
);
73 void PE_cpu_machine_quiesce(cpu_id_t target
)
75 IOCPU
*targetCPU
= OSDynamicCast(IOCPU
, (OSObject
*)target
);
77 if (targetCPU
) targetCPU
->quiesceCPU();
80 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
82 #define super IOService
84 OSDefineMetaClassAndAbstractStructors(IOCPU
, IOService
);
85 OSMetaClassDefineReservedUnused(IOCPU
, 0);
86 OSMetaClassDefineReservedUnused(IOCPU
, 1);
87 OSMetaClassDefineReservedUnused(IOCPU
, 2);
88 OSMetaClassDefineReservedUnused(IOCPU
, 3);
89 OSMetaClassDefineReservedUnused(IOCPU
, 4);
90 OSMetaClassDefineReservedUnused(IOCPU
, 5);
91 OSMetaClassDefineReservedUnused(IOCPU
, 6);
92 OSMetaClassDefineReservedUnused(IOCPU
, 7);
94 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
96 static OSArray
*gIOCPUs
;
97 static const OSSymbol
*gIOCPUStateKey
;
98 static OSString
*gIOCPUStateNames
[kIOCPUStateCount
];
100 void IOCPUSleepKernel(void)
105 numCPUs
= gIOCPUs
->getCount();
110 target
= OSDynamicCast(IOCPU
, gIOCPUs
->getObject(cnt
));
111 if (target
->getCPUState() == kIOCPUStateRunning
) {
116 // Wake the other CPUs.
117 for (cnt
= 1; cnt
< numCPUs
; cnt
++) {
118 target
= OSDynamicCast(IOCPU
, gIOCPUs
->getObject(cnt
));
119 if (target
->getCPUState() == kIOCPUStateStopped
) {
120 processor_start(target
->getMachProcessor());
125 void IOCPU::initCPUs(void)
128 gIOCPUs
= OSArray::withCapacity(1);
130 gIOCPUStateKey
= OSSymbol::withCStringNoCopy("IOCPUState");
132 gIOCPUStateNames
[kIOCPUStateUnregistered
] =
133 OSString::withCStringNoCopy("Unregistered");
134 gIOCPUStateNames
[kIOCPUStateUninitalized
] =
135 OSString::withCStringNoCopy("Uninitalized");
136 gIOCPUStateNames
[kIOCPUStateStopped
] =
137 OSString::withCStringNoCopy("Stopped");
138 gIOCPUStateNames
[kIOCPUStateRunning
] =
139 OSString::withCStringNoCopy("Running");
143 bool IOCPU::start(IOService
*provider
)
145 OSData
*busFrequency
, *cpuFrequency
, *timebaseFrequency
;
147 if (!super::start(provider
)) return false;
154 gIOCPUs
->setObject(this);
156 // Correct the bus, cpu and dec frequencies in the device tree.
157 busFrequency
= OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo
.bus_clock_rate_hz
, 4);
158 cpuFrequency
= OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo
.cpu_clock_rate_hz
, 4);
159 timebaseFrequency
= OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo
.timebase_frequency_hz
, 4);
160 provider
->setProperty("bus-frequency", busFrequency
);
161 provider
->setProperty("clock-frequency", cpuFrequency
);
162 provider
->setProperty("timebase-frequency", timebaseFrequency
);
163 busFrequency
->release();
164 cpuFrequency
->release();
165 timebaseFrequency
->release();
167 setProperty("IOCPUID", (UInt32
)this, 32);
170 setCPUState(kIOCPUStateUnregistered
);
175 IOReturn
IOCPU::setProperties(OSObject
*properties
)
177 OSDictionary
*dict
= OSDynamicCast(OSDictionary
, properties
);
180 if (dict
== 0) return kIOReturnUnsupported
;
182 stateStr
= OSDynamicCast(OSString
, dict
->getObject(gIOCPUStateKey
));
184 if (!IOUserClient::clientHasPrivilege(current_task(), "root"))
185 return kIOReturnNotPrivileged
;
187 if (_cpuNumber
== 0) return kIOReturnUnsupported
;
189 if (stateStr
->isEqualTo("running")) {
190 if (_cpuState
== kIOCPUStateStopped
) {
191 processor_start(machProcessor
);
192 } else if (_cpuState
!= kIOCPUStateRunning
) {
193 return kIOReturnUnsupported
;
195 } else if (stateStr
->isEqualTo("stopped")) {
196 if (_cpuState
== kIOCPUStateRunning
) {
198 } else if (_cpuState
!= kIOCPUStateStopped
) {
199 return kIOReturnUnsupported
;
201 } else return kIOReturnUnsupported
;
203 return kIOReturnSuccess
;
206 return kIOReturnUnsupported
;
209 void IOCPU::signalCPU(IOCPU */
*target*/
)
213 void IOCPU::enableCPUTimeBase(bool /*enable*/)
217 UInt32
IOCPU::getCPUNumber(void)
222 void IOCPU::setCPUNumber(UInt32 cpuNumber
)
224 _cpuNumber
= cpuNumber
;
225 setProperty("IOCPUNumber", _cpuNumber
, 32);
228 UInt32
IOCPU::getCPUState(void)
233 void IOCPU::setCPUState(UInt32 cpuState
)
235 if ((cpuState
>= 0) && (cpuState
< kIOCPUStateCount
)) {
236 _cpuState
= cpuState
;
237 setProperty(gIOCPUStateKey
, gIOCPUStateNames
[cpuState
]);
241 OSArray
*IOCPU::getCPUGroup(void)
246 UInt32
IOCPU::getCPUGroupSize(void)
248 return _cpuGroup
->getCount();
251 processor_t
IOCPU::getMachProcessor(void)
253 return machProcessor
;
257 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
260 #define super IOInterruptController
262 OSDefineMetaClassAndStructors(IOCPUInterruptController
, IOInterruptController
);
264 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 0);
265 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 1);
266 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 2);
267 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 3);
268 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 4);
269 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 5);
273 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
276 IOReturn
IOCPUInterruptController::initCPUInterruptController(int sources
)
280 if (!super::init()) return kIOReturnInvalid
;
284 cpus
= (IOCPU
**)IOMalloc(numCPUs
* sizeof(IOCPU
*));
285 if (cpus
== 0) return kIOReturnNoMemory
;
286 bzero(cpus
, numCPUs
* sizeof(IOCPU
*));
288 vectors
= (IOInterruptVector
*)IOMalloc(numCPUs
* sizeof(IOInterruptVector
));
289 if (vectors
== 0) return kIOReturnNoMemory
;
290 bzero(vectors
, numCPUs
* sizeof(IOInterruptVector
));
292 // Allocate locks for the
293 for (cnt
= 0; cnt
< numCPUs
; cnt
++) {
294 vectors
[cnt
].interruptLock
= IOLockAlloc();
295 if (vectors
[cnt
].interruptLock
== NULL
) {
296 for (cnt
= 0; cnt
< numCPUs
; cnt
++) {
297 if (vectors
[cnt
].interruptLock
!= NULL
)
298 IOLockFree(vectors
[cnt
].interruptLock
);
300 return kIOReturnNoResources
;
304 return kIOReturnSuccess
;
307 void IOCPUInterruptController::registerCPUInterruptController(void)
311 getPlatform()->registerInterruptController(gPlatformInterruptControllerName
,
315 void IOCPUInterruptController::setCPUInterruptProperties(IOService
*service
)
323 // Create the interrupt specifer array.
324 specifier
= OSArray::withCapacity(numCPUs
);
325 for (cnt
= 0; cnt
< numCPUs
; cnt
++) {
327 tmpData
= OSData::withBytes(&tmpLong
, sizeof(tmpLong
));
328 specifier
->setObject(tmpData
);
332 // Create the interrupt controller array.
333 controller
= OSArray::withCapacity(numCPUs
);
334 for (cnt
= 0; cnt
< numCPUs
; cnt
++) {
335 controller
->setObject(gPlatformInterruptControllerName
);
338 // Put the two arrays into the property table.
339 service
->setProperty(gIOInterruptControllersKey
, controller
);
340 service
->setProperty(gIOInterruptSpecifiersKey
, specifier
);
341 controller
->release();
342 specifier
->release();
345 void IOCPUInterruptController::enableCPUInterrupt(IOCPU
*cpu
)
347 ml_install_interrupt_handler(cpu
, cpu
->getCPUNumber(), this,
348 (IOInterruptHandler
)&IOCPUInterruptController::handleInterrupt
, 0);
352 if (enabledCPUs
== numCPUs
) thread_wakeup(this);
355 IOReturn
IOCPUInterruptController::registerInterrupt(IOService
*nub
,
358 IOInterruptHandler handler
,
361 IOInterruptVector
*vector
;
363 if (source
>= numCPUs
) return kIOReturnNoResources
;
365 vector
= &vectors
[source
];
367 // Get the lock for this vector.
368 IOTakeLock(vector
->interruptLock
);
370 // Make sure the vector is not in use.
371 if (vector
->interruptRegistered
) {
372 IOUnlock(vector
->interruptLock
);
373 return kIOReturnNoResources
;
376 // Fill in vector with the client's info.
377 vector
->handler
= handler
;
379 vector
->source
= source
;
380 vector
->target
= target
;
381 vector
->refCon
= refCon
;
383 // Get the vector ready. It starts hard disabled.
384 vector
->interruptDisabledHard
= 1;
385 vector
->interruptDisabledSoft
= 1;
386 vector
->interruptRegistered
= 1;
388 IOUnlock(vector
->interruptLock
);
390 if (enabledCPUs
!= numCPUs
) {
391 assert_wait(this, THREAD_UNINT
);
392 thread_block(THREAD_CONTINUE_NULL
);
395 return kIOReturnSuccess
;
398 IOReturn
IOCPUInterruptController::getInterruptType(IOService */
*nub*/
,
402 if (interruptType
== 0) return kIOReturnBadArgument
;
404 *interruptType
= kIOInterruptTypeLevel
;
406 return kIOReturnSuccess
;
409 IOReturn
IOCPUInterruptController::enableInterrupt(IOService */
*nub*/
,
412 // ml_set_interrupts_enabled(true);
413 return kIOReturnSuccess
;
416 IOReturn
IOCPUInterruptController::disableInterrupt(IOService */
*nub*/
,
419 // ml_set_interrupts_enabled(false);
420 return kIOReturnSuccess
;
423 IOReturn
IOCPUInterruptController::causeInterrupt(IOService */
*nub*/
,
426 ml_cause_interrupt();
427 return kIOReturnSuccess
;
430 IOReturn
IOCPUInterruptController::handleInterrupt(void */
*refCon*/
,
434 IOInterruptVector
*vector
;
436 vector
= &vectors
[source
];
438 if (!vector
->interruptRegistered
) return kIOReturnInvalid
;
440 vector
->handler(vector
->target
, vector
->refCon
,
441 vector
->nub
, vector
->source
);
443 return kIOReturnSuccess
;
446 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */