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 timebase frequencies in the device tree.
157 if (gPEClockFrequencyInfo
.bus_frequency_hz
< 0x100000000ULL
)
158 busFrequency
= OSData::withBytesNoCopy((void *)((char *)&gPEClockFrequencyInfo
.bus_frequency_hz
+ 4), 4);
160 busFrequency
= OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo
.bus_clock_rate_hz
, 8);
161 provider
->setProperty("bus-frequency", busFrequency
);
162 busFrequency
->release();
164 if (gPEClockFrequencyInfo
.cpu_frequency_hz
< 0x100000000ULL
)
165 cpuFrequency
= OSData::withBytesNoCopy((void *)((char *)&gPEClockFrequencyInfo
.cpu_frequency_hz
+ 4), 4);
167 cpuFrequency
= OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo
.cpu_clock_rate_hz
, 8);
168 provider
->setProperty("clock-frequency", cpuFrequency
);
169 cpuFrequency
->release();
171 timebaseFrequency
= OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo
.timebase_frequency_hz
, 4);
172 provider
->setProperty("timebase-frequency", timebaseFrequency
);
173 timebaseFrequency
->release();
175 setProperty("IOCPUID", (UInt32
)this, 32);
178 setCPUState(kIOCPUStateUnregistered
);
183 IOReturn
IOCPU::setProperties(OSObject
*properties
)
185 OSDictionary
*dict
= OSDynamicCast(OSDictionary
, properties
);
189 if (dict
== 0) return kIOReturnUnsupported
;
191 stateStr
= OSDynamicCast(OSString
, dict
->getObject(gIOCPUStateKey
));
193 result
= IOUserClient::clientHasPrivilege(current_task(), kIOClientPrivilegeAdministrator
);
194 if (result
!= kIOReturnSuccess
) return result
;
196 if (_cpuNumber
== 0) return kIOReturnUnsupported
;
198 if (stateStr
->isEqualTo("running")) {
199 if (_cpuState
== kIOCPUStateStopped
) {
200 processor_start(machProcessor
);
201 } else if (_cpuState
!= kIOCPUStateRunning
) {
202 return kIOReturnUnsupported
;
204 } else if (stateStr
->isEqualTo("stopped")) {
205 if (_cpuState
== kIOCPUStateRunning
) {
207 } else if (_cpuState
!= kIOCPUStateStopped
) {
208 return kIOReturnUnsupported
;
210 } else return kIOReturnUnsupported
;
212 return kIOReturnSuccess
;
215 return kIOReturnUnsupported
;
218 void IOCPU::signalCPU(IOCPU */
*target*/
)
222 void IOCPU::enableCPUTimeBase(bool /*enable*/)
226 UInt32
IOCPU::getCPUNumber(void)
231 void IOCPU::setCPUNumber(UInt32 cpuNumber
)
233 _cpuNumber
= cpuNumber
;
234 setProperty("IOCPUNumber", _cpuNumber
, 32);
237 UInt32
IOCPU::getCPUState(void)
242 void IOCPU::setCPUState(UInt32 cpuState
)
244 if ((cpuState
>= 0) && (cpuState
< kIOCPUStateCount
)) {
245 _cpuState
= cpuState
;
246 setProperty(gIOCPUStateKey
, gIOCPUStateNames
[cpuState
]);
250 OSArray
*IOCPU::getCPUGroup(void)
255 UInt32
IOCPU::getCPUGroupSize(void)
257 return _cpuGroup
->getCount();
260 processor_t
IOCPU::getMachProcessor(void)
262 return machProcessor
;
266 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
269 #define super IOInterruptController
271 OSDefineMetaClassAndStructors(IOCPUInterruptController
, IOInterruptController
);
273 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 0);
274 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 1);
275 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 2);
276 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 3);
277 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 4);
278 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 5);
282 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
285 IOReturn
IOCPUInterruptController::initCPUInterruptController(int sources
)
289 if (!super::init()) return kIOReturnInvalid
;
293 cpus
= (IOCPU
**)IOMalloc(numCPUs
* sizeof(IOCPU
*));
294 if (cpus
== 0) return kIOReturnNoMemory
;
295 bzero(cpus
, numCPUs
* sizeof(IOCPU
*));
297 vectors
= (IOInterruptVector
*)IOMalloc(numCPUs
* sizeof(IOInterruptVector
));
298 if (vectors
== 0) return kIOReturnNoMemory
;
299 bzero(vectors
, numCPUs
* sizeof(IOInterruptVector
));
301 // Allocate locks for the
302 for (cnt
= 0; cnt
< numCPUs
; cnt
++) {
303 vectors
[cnt
].interruptLock
= IOLockAlloc();
304 if (vectors
[cnt
].interruptLock
== NULL
) {
305 for (cnt
= 0; cnt
< numCPUs
; cnt
++) {
306 if (vectors
[cnt
].interruptLock
!= NULL
)
307 IOLockFree(vectors
[cnt
].interruptLock
);
309 return kIOReturnNoResources
;
313 ml_init_max_cpus(numCPUs
);
315 return kIOReturnSuccess
;
318 void IOCPUInterruptController::registerCPUInterruptController(void)
322 getPlatform()->registerInterruptController(gPlatformInterruptControllerName
,
326 void IOCPUInterruptController::setCPUInterruptProperties(IOService
*service
)
334 // Create the interrupt specifer array.
335 specifier
= OSArray::withCapacity(numCPUs
);
336 for (cnt
= 0; cnt
< numCPUs
; cnt
++) {
338 tmpData
= OSData::withBytes(&tmpLong
, sizeof(tmpLong
));
339 specifier
->setObject(tmpData
);
343 // Create the interrupt controller array.
344 controller
= OSArray::withCapacity(numCPUs
);
345 for (cnt
= 0; cnt
< numCPUs
; cnt
++) {
346 controller
->setObject(gPlatformInterruptControllerName
);
349 // Put the two arrays into the property table.
350 service
->setProperty(gIOInterruptControllersKey
, controller
);
351 service
->setProperty(gIOInterruptSpecifiersKey
, specifier
);
352 controller
->release();
353 specifier
->release();
356 void IOCPUInterruptController::enableCPUInterrupt(IOCPU
*cpu
)
358 ml_install_interrupt_handler(cpu
, cpu
->getCPUNumber(), this,
359 (IOInterruptHandler
)&IOCPUInterruptController::handleInterrupt
, 0);
363 if (enabledCPUs
== numCPUs
) thread_wakeup(this);
366 IOReturn
IOCPUInterruptController::registerInterrupt(IOService
*nub
,
369 IOInterruptHandler handler
,
372 IOInterruptVector
*vector
;
374 if (source
>= numCPUs
) return kIOReturnNoResources
;
376 vector
= &vectors
[source
];
378 // Get the lock for this vector.
379 IOTakeLock(vector
->interruptLock
);
381 // Make sure the vector is not in use.
382 if (vector
->interruptRegistered
) {
383 IOUnlock(vector
->interruptLock
);
384 return kIOReturnNoResources
;
387 // Fill in vector with the client's info.
388 vector
->handler
= handler
;
390 vector
->source
= source
;
391 vector
->target
= target
;
392 vector
->refCon
= refCon
;
394 // Get the vector ready. It starts hard disabled.
395 vector
->interruptDisabledHard
= 1;
396 vector
->interruptDisabledSoft
= 1;
397 vector
->interruptRegistered
= 1;
399 IOUnlock(vector
->interruptLock
);
401 if (enabledCPUs
!= numCPUs
) {
402 assert_wait(this, THREAD_UNINT
);
403 thread_block(THREAD_CONTINUE_NULL
);
406 return kIOReturnSuccess
;
409 IOReturn
IOCPUInterruptController::getInterruptType(IOService */
*nub*/
,
413 if (interruptType
== 0) return kIOReturnBadArgument
;
415 *interruptType
= kIOInterruptTypeLevel
;
417 return kIOReturnSuccess
;
420 IOReturn
IOCPUInterruptController::enableInterrupt(IOService */
*nub*/
,
423 // ml_set_interrupts_enabled(true);
424 return kIOReturnSuccess
;
427 IOReturn
IOCPUInterruptController::disableInterrupt(IOService */
*nub*/
,
430 // ml_set_interrupts_enabled(false);
431 return kIOReturnSuccess
;
434 IOReturn
IOCPUInterruptController::causeInterrupt(IOService */
*nub*/
,
437 ml_cause_interrupt();
438 return kIOReturnSuccess
;
441 IOReturn
IOCPUInterruptController::handleInterrupt(void */
*refCon*/
,
445 IOInterruptVector
*vector
;
447 vector
= &vectors
[source
];
449 if (!vector
->interruptRegistered
) return kIOReturnInvalid
;
451 vector
->handler(vector
->target
, vector
->refCon
,
452 vector
->nub
, vector
->source
);
454 return kIOReturnSuccess
;
457 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */