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