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