]> git.saurik.com Git - apple/xnu.git/blame - iokit/Kernel/IOCPU.cpp
xnu-792.10.96.tar.gz
[apple/xnu.git] / iokit / Kernel / IOCPU.cpp
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 1999-2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
37839358
A
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.
1c79356b 11 *
37839358
A
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
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
37839358
A
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
1c79356b
A
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/*
23 * Copyright (c) 1999-2000 Apple Computer, Inc. All rights reserved.
24 *
25 * DRI: Josh de Cesare
26 *
27 */
28
29extern "C" {
30#include <machine/machine_routines.h>
31#include <pexpert/pexpert.h>
32}
33
34#include <IOKit/IOLib.h>
35#include <IOKit/IOPlatformExpert.h>
36#include <IOKit/IOUserClient.h>
37#include <IOKit/IOCPU.h>
38
39
40/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
41
42kern_return_t PE_cpu_start(cpu_id_t target,
43 vm_offset_t start_paddr, vm_offset_t arg_paddr)
44{
45 IOCPU *targetCPU = OSDynamicCast(IOCPU, (OSObject *)target);
46
47 if (targetCPU == 0) return KERN_FAILURE;
48 return targetCPU->startCPU(start_paddr, arg_paddr);
49}
50
51void PE_cpu_halt(cpu_id_t target)
52{
53 IOCPU *targetCPU = OSDynamicCast(IOCPU, (OSObject *)target);
54
55 if (targetCPU) targetCPU->haltCPU();
56}
57
58void PE_cpu_signal(cpu_id_t source, cpu_id_t target)
59{
60 IOCPU *sourceCPU = OSDynamicCast(IOCPU, (OSObject *)source);
61 IOCPU *targetCPU = OSDynamicCast(IOCPU, (OSObject *)target);
62
63 if (sourceCPU && targetCPU) sourceCPU->signalCPU(targetCPU);
64}
65
66void PE_cpu_machine_init(cpu_id_t target, boolean_t boot)
67{
68 IOCPU *targetCPU = OSDynamicCast(IOCPU, (OSObject *)target);
69
70 if (targetCPU) targetCPU->initCPU(boot);
71}
72
73void PE_cpu_machine_quiesce(cpu_id_t target)
74{
75 IOCPU *targetCPU = OSDynamicCast(IOCPU, (OSObject *)target);
76
77 if (targetCPU) targetCPU->quiesceCPU();
78}
79
80/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
81
82#define super IOService
83
84OSDefineMetaClassAndAbstractStructors(IOCPU, IOService);
85OSMetaClassDefineReservedUnused(IOCPU, 0);
86OSMetaClassDefineReservedUnused(IOCPU, 1);
87OSMetaClassDefineReservedUnused(IOCPU, 2);
88OSMetaClassDefineReservedUnused(IOCPU, 3);
89OSMetaClassDefineReservedUnused(IOCPU, 4);
90OSMetaClassDefineReservedUnused(IOCPU, 5);
91OSMetaClassDefineReservedUnused(IOCPU, 6);
92OSMetaClassDefineReservedUnused(IOCPU, 7);
93
94/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
95
96static OSArray *gIOCPUs;
97static const OSSymbol *gIOCPUStateKey;
98static OSString *gIOCPUStateNames[kIOCPUStateCount];
99
100void IOCPUSleepKernel(void)
101{
102 long cnt, numCPUs;
103 IOCPU *target;
104
105 numCPUs = gIOCPUs->getCount();
106
107 // Sleep the CPUs.
108 cnt = numCPUs;
109 while (cnt--) {
110 target = OSDynamicCast(IOCPU, gIOCPUs->getObject(cnt));
111 if (target->getCPUState() == kIOCPUStateRunning) {
112 target->haltCPU();
113 }
114 }
115
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());
121 }
122 }
123}
124
125void IOCPU::initCPUs(void)
126{
127 if (gIOCPUs == 0) {
128 gIOCPUs = OSArray::withCapacity(1);
129
130 gIOCPUStateKey = OSSymbol::withCStringNoCopy("IOCPUState");
131
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");
140 }
141}
142
143bool IOCPU::start(IOService *provider)
144{
90556fb8 145 OSData *busFrequency, *cpuFrequency, *timebaseFrequency;
1c79356b
A
146
147 if (!super::start(provider)) return false;
148
149 initCPUs();
150
151 _cpuGroup = gIOCPUs;
152 cpuNub = provider;
153
154 gIOCPUs->setObject(this);
155
43866e37 156 // Correct the bus, cpu and timebase frequencies in the device tree.
91447636
A
157 if (gPEClockFrequencyInfo.bus_frequency_hz < 0x100000000ULL) {
158 busFrequency = OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo.bus_clock_rate_hz, 4);
159 } else {
160 busFrequency = OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo.bus_frequency_hz, 8);
161 }
1c79356b 162 provider->setProperty("bus-frequency", busFrequency);
de355530 163 busFrequency->release();
43866e37 164
91447636
A
165 if (gPEClockFrequencyInfo.cpu_frequency_hz < 0x100000000ULL) {
166 cpuFrequency = OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo.cpu_clock_rate_hz, 4);
167 } else {
168 cpuFrequency = OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo.cpu_frequency_hz, 8);
169 }
43866e37 170 provider->setProperty("clock-frequency", cpuFrequency);
1c79356b 171 cpuFrequency->release();
43866e37
A
172
173 timebaseFrequency = OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo.timebase_frequency_hz, 4);
174 provider->setProperty("timebase-frequency", timebaseFrequency);
90556fb8 175 timebaseFrequency->release();
1c79356b 176
91447636 177 super::setProperty("IOCPUID", (UInt32)this, 32);
1c79356b
A
178
179 setCPUNumber(0);
180 setCPUState(kIOCPUStateUnregistered);
181
182 return true;
183}
184
91447636 185OSObject *IOCPU::getProperty(const OSSymbol *aKey) const
1c79356b 186{
91447636 187 if (aKey == gIOCPUStateKey) return gIOCPUStateNames[_cpuState];
1c79356b 188
91447636
A
189 return super::getProperty(aKey);
190}
191
192bool IOCPU::setProperty(const OSSymbol *aKey, OSObject *anObject)
193{
194 OSString *stateStr;
1c79356b 195
91447636
A
196 if (aKey == gIOCPUStateKey) {
197 stateStr = OSDynamicCast(OSString, anObject);
198 if (stateStr == 0) return false;
1c79356b 199
91447636 200 if (_cpuNumber == 0) return false;
1c79356b
A
201
202 if (stateStr->isEqualTo("running")) {
203 if (_cpuState == kIOCPUStateStopped) {
204 processor_start(machProcessor);
205 } else if (_cpuState != kIOCPUStateRunning) {
91447636 206 return false;
1c79356b
A
207 }
208 } else if (stateStr->isEqualTo("stopped")) {
209 if (_cpuState == kIOCPUStateRunning) {
210 haltCPU();
211 } else if (_cpuState != kIOCPUStateStopped) {
91447636 212 return false;
1c79356b 213 }
91447636
A
214 } else return false;
215
216 return true;
217 }
218
219 return super::setProperty(aKey, anObject);
220}
221
222bool IOCPU::serializeProperties(OSSerialize *serialize) const
223{
c0fea474
A
224 bool result;
225 OSDictionary *dict = dictionaryWithProperties();
226 dict->setObject(gIOCPUStateKey, gIOCPUStateNames[_cpuState]);
227 result = dict->serialize(serialize);
228 dict->release();
229 return result;
91447636
A
230}
231
232IOReturn IOCPU::setProperties(OSObject *properties)
233{
234 OSDictionary *dict = OSDynamicCast(OSDictionary, properties);
235 OSString *stateStr;
236 IOReturn result;
237
238 if (dict == 0) return kIOReturnUnsupported;
239
240 stateStr = OSDynamicCast(OSString, dict->getObject(gIOCPUStateKey));
241 if (stateStr != 0) {
242 result = IOUserClient::clientHasPrivilege(current_task(), kIOClientPrivilegeAdministrator);
243 if (result != kIOReturnSuccess) return result;
244
245 if (setProperty(gIOCPUStateKey, stateStr)) return kIOReturnSuccess;
1c79356b 246
91447636 247 return kIOReturnUnsupported;
1c79356b
A
248 }
249
250 return kIOReturnUnsupported;
251}
252
253void IOCPU::signalCPU(IOCPU */*target*/)
254{
255}
256
257void IOCPU::enableCPUTimeBase(bool /*enable*/)
258{
259}
260
261UInt32 IOCPU::getCPUNumber(void)
262{
263 return _cpuNumber;
264}
265
266void IOCPU::setCPUNumber(UInt32 cpuNumber)
267{
268 _cpuNumber = cpuNumber;
91447636 269 super::setProperty("IOCPUNumber", _cpuNumber, 32);
1c79356b
A
270}
271
272UInt32 IOCPU::getCPUState(void)
273{
274 return _cpuState;
275}
276
277void IOCPU::setCPUState(UInt32 cpuState)
278{
91447636 279 if (cpuState < kIOCPUStateCount) {
1c79356b 280 _cpuState = cpuState;
1c79356b
A
281 }
282}
283
284OSArray *IOCPU::getCPUGroup(void)
285{
286 return _cpuGroup;
287}
288
289UInt32 IOCPU::getCPUGroupSize(void)
290{
291 return _cpuGroup->getCount();
292}
293
294processor_t IOCPU::getMachProcessor(void)
295{
296 return machProcessor;
297}
298
299
300/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
301
302#undef super
303#define super IOInterruptController
304
305OSDefineMetaClassAndStructors(IOCPUInterruptController, IOInterruptController);
306
307OSMetaClassDefineReservedUnused(IOCPUInterruptController, 0);
308OSMetaClassDefineReservedUnused(IOCPUInterruptController, 1);
309OSMetaClassDefineReservedUnused(IOCPUInterruptController, 2);
310OSMetaClassDefineReservedUnused(IOCPUInterruptController, 3);
311OSMetaClassDefineReservedUnused(IOCPUInterruptController, 4);
312OSMetaClassDefineReservedUnused(IOCPUInterruptController, 5);
313
314
315
316/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
317
318
319IOReturn IOCPUInterruptController::initCPUInterruptController(int sources)
320{
321 int cnt;
322
323 if (!super::init()) return kIOReturnInvalid;
324
325 numCPUs = sources;
326
327 cpus = (IOCPU **)IOMalloc(numCPUs * sizeof(IOCPU *));
328 if (cpus == 0) return kIOReturnNoMemory;
329 bzero(cpus, numCPUs * sizeof(IOCPU *));
330
331 vectors = (IOInterruptVector *)IOMalloc(numCPUs * sizeof(IOInterruptVector));
332 if (vectors == 0) return kIOReturnNoMemory;
333 bzero(vectors, numCPUs * sizeof(IOInterruptVector));
334
335 // Allocate locks for the
336 for (cnt = 0; cnt < numCPUs; cnt++) {
337 vectors[cnt].interruptLock = IOLockAlloc();
338 if (vectors[cnt].interruptLock == NULL) {
339 for (cnt = 0; cnt < numCPUs; cnt++) {
340 if (vectors[cnt].interruptLock != NULL)
341 IOLockFree(vectors[cnt].interruptLock);
342 }
343 return kIOReturnNoResources;
344 }
345 }
346
43866e37
A
347 ml_init_max_cpus(numCPUs);
348
1c79356b
A
349 return kIOReturnSuccess;
350}
351
352void IOCPUInterruptController::registerCPUInterruptController(void)
353{
354 registerService();
355
356 getPlatform()->registerInterruptController(gPlatformInterruptControllerName,
357 this);
358}
359
360void IOCPUInterruptController::setCPUInterruptProperties(IOService *service)
361{
362 int cnt;
363 OSArray *controller;
364 OSArray *specifier;
365 OSData *tmpData;
366 long tmpLong;
367
368 // Create the interrupt specifer array.
369 specifier = OSArray::withCapacity(numCPUs);
370 for (cnt = 0; cnt < numCPUs; cnt++) {
371 tmpLong = cnt;
372 tmpData = OSData::withBytes(&tmpLong, sizeof(tmpLong));
373 specifier->setObject(tmpData);
374 tmpData->release();
375 };
376
377 // Create the interrupt controller array.
378 controller = OSArray::withCapacity(numCPUs);
379 for (cnt = 0; cnt < numCPUs; cnt++) {
380 controller->setObject(gPlatformInterruptControllerName);
381 }
382
383 // Put the two arrays into the property table.
384 service->setProperty(gIOInterruptControllersKey, controller);
385 service->setProperty(gIOInterruptSpecifiersKey, specifier);
386 controller->release();
387 specifier->release();
388}
389
390void IOCPUInterruptController::enableCPUInterrupt(IOCPU *cpu)
391{
c0fea474
A
392 IOInterruptHandler handler = OSMemberFunctionCast(
393 IOInterruptHandler, this, &IOCPUInterruptController::handleInterrupt);
394
395 ml_install_interrupt_handler(cpu, cpu->getCPUNumber(), this, handler, 0);
1c79356b
A
396
397 enabledCPUs++;
398
399 if (enabledCPUs == numCPUs) thread_wakeup(this);
400}
401
402IOReturn IOCPUInterruptController::registerInterrupt(IOService *nub,
403 int source,
404 void *target,
405 IOInterruptHandler handler,
406 void *refCon)
407{
408 IOInterruptVector *vector;
409
410 if (source >= numCPUs) return kIOReturnNoResources;
411
412 vector = &vectors[source];
413
414 // Get the lock for this vector.
415 IOTakeLock(vector->interruptLock);
416
417 // Make sure the vector is not in use.
418 if (vector->interruptRegistered) {
419 IOUnlock(vector->interruptLock);
420 return kIOReturnNoResources;
421 }
422
423 // Fill in vector with the client's info.
424 vector->handler = handler;
425 vector->nub = nub;
426 vector->source = source;
427 vector->target = target;
428 vector->refCon = refCon;
429
430 // Get the vector ready. It starts hard disabled.
431 vector->interruptDisabledHard = 1;
432 vector->interruptDisabledSoft = 1;
433 vector->interruptRegistered = 1;
434
435 IOUnlock(vector->interruptLock);
436
437 if (enabledCPUs != numCPUs) {
438 assert_wait(this, THREAD_UNINT);
9bccf70c 439 thread_block(THREAD_CONTINUE_NULL);
1c79356b
A
440 }
441
442 return kIOReturnSuccess;
443}
444
445IOReturn IOCPUInterruptController::getInterruptType(IOService */*nub*/,
446 int /*source*/,
447 int *interruptType)
448{
449 if (interruptType == 0) return kIOReturnBadArgument;
450
451 *interruptType = kIOInterruptTypeLevel;
452
453 return kIOReturnSuccess;
454}
455
456IOReturn IOCPUInterruptController::enableInterrupt(IOService */*nub*/,
457 int /*source*/)
458{
459// ml_set_interrupts_enabled(true);
460 return kIOReturnSuccess;
461}
462
463IOReturn IOCPUInterruptController::disableInterrupt(IOService */*nub*/,
464 int /*source*/)
465{
466// ml_set_interrupts_enabled(false);
467 return kIOReturnSuccess;
468}
469
470IOReturn IOCPUInterruptController::causeInterrupt(IOService */*nub*/,
471 int /*source*/)
472{
473 ml_cause_interrupt();
474 return kIOReturnSuccess;
475}
476
477IOReturn IOCPUInterruptController::handleInterrupt(void */*refCon*/,
478 IOService */*nub*/,
479 int source)
480{
481 IOInterruptVector *vector;
482
483 vector = &vectors[source];
484
485 if (!vector->interruptRegistered) return kIOReturnInvalid;
486
487 vector->handler(vector->target, vector->refCon,
488 vector->nub, vector->source);
489
490 return kIOReturnSuccess;
491}
492
493/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */