]> git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOCPU.cpp
c86d5a2314cec614f879a632bb401b8dfa48471f
[apple/xnu.git] / iokit / Kernel / IOCPU.cpp
1 /*
2 * Copyright (c) 1999-2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
18 * under the License.
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
29 extern "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
42 kern_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
51 void PE_cpu_halt(cpu_id_t target)
52 {
53 IOCPU *targetCPU = OSDynamicCast(IOCPU, (OSObject *)target);
54
55 if (targetCPU) targetCPU->haltCPU();
56 }
57
58 void 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
66 void 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
73 void 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
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);
93
94 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
95
96 static OSArray *gIOCPUs;
97 static const OSSymbol *gIOCPUStateKey;
98 static OSString *gIOCPUStateNames[kIOCPUStateCount];
99
100 void 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
125 void 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
143 bool IOCPU::start(IOService *provider)
144 {
145 OSData *busFrequency, *cpuFrequency, *timebaseFrequency;
146
147 if (!super::start(provider)) return false;
148
149 initCPUs();
150
151 _cpuGroup = gIOCPUs;
152 cpuNub = provider;
153
154 gIOCPUs->setObject(this);
155
156 // Correct the bus, cpu and timebase frequencies in the device tree.
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 }
162 provider->setProperty("bus-frequency", busFrequency);
163 busFrequency->release();
164
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 }
170 provider->setProperty("clock-frequency", cpuFrequency);
171 cpuFrequency->release();
172
173 timebaseFrequency = OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo.timebase_frequency_hz, 4);
174 provider->setProperty("timebase-frequency", timebaseFrequency);
175 timebaseFrequency->release();
176
177 super::setProperty("IOCPUID", (UInt32)this, 32);
178
179 setCPUNumber(0);
180 setCPUState(kIOCPUStateUnregistered);
181
182 return true;
183 }
184
185 OSObject *IOCPU::getProperty(const OSSymbol *aKey) const
186 {
187 if (aKey == gIOCPUStateKey) return gIOCPUStateNames[_cpuState];
188
189 return super::getProperty(aKey);
190 }
191
192 bool IOCPU::setProperty(const OSSymbol *aKey, OSObject *anObject)
193 {
194 OSString *stateStr;
195
196 if (aKey == gIOCPUStateKey) {
197 stateStr = OSDynamicCast(OSString, anObject);
198 if (stateStr == 0) return false;
199
200 if (_cpuNumber == 0) return false;
201
202 if (stateStr->isEqualTo("running")) {
203 if (_cpuState == kIOCPUStateStopped) {
204 processor_start(machProcessor);
205 } else if (_cpuState != kIOCPUStateRunning) {
206 return false;
207 }
208 } else if (stateStr->isEqualTo("stopped")) {
209 if (_cpuState == kIOCPUStateRunning) {
210 haltCPU();
211 } else if (_cpuState != kIOCPUStateStopped) {
212 return false;
213 }
214 } else return false;
215
216 return true;
217 }
218
219 return super::setProperty(aKey, anObject);
220 }
221
222 bool IOCPU::serializeProperties(OSSerialize *serialize) const
223 {
224 bool result;
225 OSDictionary *dict = dictionaryWithProperties();
226 dict->setObject(gIOCPUStateKey, gIOCPUStateNames[_cpuState]);
227 result = dict->serialize(serialize);
228 dict->release();
229 return result;
230 }
231
232 IOReturn 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;
246
247 return kIOReturnUnsupported;
248 }
249
250 return kIOReturnUnsupported;
251 }
252
253 void IOCPU::signalCPU(IOCPU */*target*/)
254 {
255 }
256
257 void IOCPU::enableCPUTimeBase(bool /*enable*/)
258 {
259 }
260
261 UInt32 IOCPU::getCPUNumber(void)
262 {
263 return _cpuNumber;
264 }
265
266 void IOCPU::setCPUNumber(UInt32 cpuNumber)
267 {
268 _cpuNumber = cpuNumber;
269 super::setProperty("IOCPUNumber", _cpuNumber, 32);
270 }
271
272 UInt32 IOCPU::getCPUState(void)
273 {
274 return _cpuState;
275 }
276
277 void IOCPU::setCPUState(UInt32 cpuState)
278 {
279 if (cpuState < kIOCPUStateCount) {
280 _cpuState = cpuState;
281 }
282 }
283
284 OSArray *IOCPU::getCPUGroup(void)
285 {
286 return _cpuGroup;
287 }
288
289 UInt32 IOCPU::getCPUGroupSize(void)
290 {
291 return _cpuGroup->getCount();
292 }
293
294 processor_t IOCPU::getMachProcessor(void)
295 {
296 return machProcessor;
297 }
298
299
300 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
301
302 #undef super
303 #define super IOInterruptController
304
305 OSDefineMetaClassAndStructors(IOCPUInterruptController, IOInterruptController);
306
307 OSMetaClassDefineReservedUnused(IOCPUInterruptController, 0);
308 OSMetaClassDefineReservedUnused(IOCPUInterruptController, 1);
309 OSMetaClassDefineReservedUnused(IOCPUInterruptController, 2);
310 OSMetaClassDefineReservedUnused(IOCPUInterruptController, 3);
311 OSMetaClassDefineReservedUnused(IOCPUInterruptController, 4);
312 OSMetaClassDefineReservedUnused(IOCPUInterruptController, 5);
313
314
315
316 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
317
318
319 IOReturn 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
347 ml_init_max_cpus(numCPUs);
348
349 return kIOReturnSuccess;
350 }
351
352 void IOCPUInterruptController::registerCPUInterruptController(void)
353 {
354 registerService();
355
356 getPlatform()->registerInterruptController(gPlatformInterruptControllerName,
357 this);
358 }
359
360 void 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
390 void IOCPUInterruptController::enableCPUInterrupt(IOCPU *cpu)
391 {
392 IOInterruptHandler handler = OSMemberFunctionCast(
393 IOInterruptHandler, this, &IOCPUInterruptController::handleInterrupt);
394
395 ml_install_interrupt_handler(cpu, cpu->getCPUNumber(), this, handler, 0);
396
397 enabledCPUs++;
398
399 if (enabledCPUs == numCPUs) thread_wakeup(this);
400 }
401
402 IOReturn 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);
439 thread_block(THREAD_CONTINUE_NULL);
440 }
441
442 return kIOReturnSuccess;
443 }
444
445 IOReturn 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
456 IOReturn IOCPUInterruptController::enableInterrupt(IOService */*nub*/,
457 int /*source*/)
458 {
459 // ml_set_interrupts_enabled(true);
460 return kIOReturnSuccess;
461 }
462
463 IOReturn IOCPUInterruptController::disableInterrupt(IOService */*nub*/,
464 int /*source*/)
465 {
466 // ml_set_interrupts_enabled(false);
467 return kIOReturnSuccess;
468 }
469
470 IOReturn IOCPUInterruptController::causeInterrupt(IOService */*nub*/,
471 int /*source*/)
472 {
473 ml_cause_interrupt();
474 return kIOReturnSuccess;
475 }
476
477 IOReturn 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 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */