]> git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOCPU.cpp
xnu-517.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_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 IOReturn result;
191
192 if (dict == 0) return kIOReturnUnsupported;
193
194 stateStr = OSDynamicCast(OSString, dict->getObject(gIOCPUStateKey));
195 if (stateStr != 0) {
196 result = IOUserClient::clientHasPrivilege(current_task(), kIOClientPrivilegeAdministrator);
197 if (result != kIOReturnSuccess) return result;
198
199 if (_cpuNumber == 0) return kIOReturnUnsupported;
200
201 if (stateStr->isEqualTo("running")) {
202 if (_cpuState == kIOCPUStateStopped) {
203 processor_start(machProcessor);
204 } else if (_cpuState != kIOCPUStateRunning) {
205 return kIOReturnUnsupported;
206 }
207 } else if (stateStr->isEqualTo("stopped")) {
208 if (_cpuState == kIOCPUStateRunning) {
209 haltCPU();
210 } else if (_cpuState != kIOCPUStateStopped) {
211 return kIOReturnUnsupported;
212 }
213 } else return kIOReturnUnsupported;
214
215 return kIOReturnSuccess;
216 }
217
218 return kIOReturnUnsupported;
219 }
220
221 void IOCPU::signalCPU(IOCPU */*target*/)
222 {
223 }
224
225 void IOCPU::enableCPUTimeBase(bool /*enable*/)
226 {
227 }
228
229 UInt32 IOCPU::getCPUNumber(void)
230 {
231 return _cpuNumber;
232 }
233
234 void IOCPU::setCPUNumber(UInt32 cpuNumber)
235 {
236 _cpuNumber = cpuNumber;
237 setProperty("IOCPUNumber", _cpuNumber, 32);
238 }
239
240 UInt32 IOCPU::getCPUState(void)
241 {
242 return _cpuState;
243 }
244
245 void IOCPU::setCPUState(UInt32 cpuState)
246 {
247 if ((cpuState >= 0) && (cpuState < kIOCPUStateCount)) {
248 _cpuState = cpuState;
249 setProperty(gIOCPUStateKey, gIOCPUStateNames[cpuState]);
250 }
251 }
252
253 OSArray *IOCPU::getCPUGroup(void)
254 {
255 return _cpuGroup;
256 }
257
258 UInt32 IOCPU::getCPUGroupSize(void)
259 {
260 return _cpuGroup->getCount();
261 }
262
263 processor_t IOCPU::getMachProcessor(void)
264 {
265 return machProcessor;
266 }
267
268
269 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
270
271 #undef super
272 #define super IOInterruptController
273
274 OSDefineMetaClassAndStructors(IOCPUInterruptController, IOInterruptController);
275
276 OSMetaClassDefineReservedUnused(IOCPUInterruptController, 0);
277 OSMetaClassDefineReservedUnused(IOCPUInterruptController, 1);
278 OSMetaClassDefineReservedUnused(IOCPUInterruptController, 2);
279 OSMetaClassDefineReservedUnused(IOCPUInterruptController, 3);
280 OSMetaClassDefineReservedUnused(IOCPUInterruptController, 4);
281 OSMetaClassDefineReservedUnused(IOCPUInterruptController, 5);
282
283
284
285 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
286
287
288 IOReturn IOCPUInterruptController::initCPUInterruptController(int sources)
289 {
290 int cnt;
291
292 if (!super::init()) return kIOReturnInvalid;
293
294 numCPUs = sources;
295
296 cpus = (IOCPU **)IOMalloc(numCPUs * sizeof(IOCPU *));
297 if (cpus == 0) return kIOReturnNoMemory;
298 bzero(cpus, numCPUs * sizeof(IOCPU *));
299
300 vectors = (IOInterruptVector *)IOMalloc(numCPUs * sizeof(IOInterruptVector));
301 if (vectors == 0) return kIOReturnNoMemory;
302 bzero(vectors, numCPUs * sizeof(IOInterruptVector));
303
304 // Allocate locks for the
305 for (cnt = 0; cnt < numCPUs; cnt++) {
306 vectors[cnt].interruptLock = IOLockAlloc();
307 if (vectors[cnt].interruptLock == NULL) {
308 for (cnt = 0; cnt < numCPUs; cnt++) {
309 if (vectors[cnt].interruptLock != NULL)
310 IOLockFree(vectors[cnt].interruptLock);
311 }
312 return kIOReturnNoResources;
313 }
314 }
315
316 ml_init_max_cpus(numCPUs);
317
318 return kIOReturnSuccess;
319 }
320
321 void IOCPUInterruptController::registerCPUInterruptController(void)
322 {
323 registerService();
324
325 getPlatform()->registerInterruptController(gPlatformInterruptControllerName,
326 this);
327 }
328
329 void IOCPUInterruptController::setCPUInterruptProperties(IOService *service)
330 {
331 int cnt;
332 OSArray *controller;
333 OSArray *specifier;
334 OSData *tmpData;
335 long tmpLong;
336
337 // Create the interrupt specifer array.
338 specifier = OSArray::withCapacity(numCPUs);
339 for (cnt = 0; cnt < numCPUs; cnt++) {
340 tmpLong = cnt;
341 tmpData = OSData::withBytes(&tmpLong, sizeof(tmpLong));
342 specifier->setObject(tmpData);
343 tmpData->release();
344 };
345
346 // Create the interrupt controller array.
347 controller = OSArray::withCapacity(numCPUs);
348 for (cnt = 0; cnt < numCPUs; cnt++) {
349 controller->setObject(gPlatformInterruptControllerName);
350 }
351
352 // Put the two arrays into the property table.
353 service->setProperty(gIOInterruptControllersKey, controller);
354 service->setProperty(gIOInterruptSpecifiersKey, specifier);
355 controller->release();
356 specifier->release();
357 }
358
359 void IOCPUInterruptController::enableCPUInterrupt(IOCPU *cpu)
360 {
361 ml_install_interrupt_handler(cpu, cpu->getCPUNumber(), this,
362 (IOInterruptHandler)&IOCPUInterruptController::handleInterrupt, 0);
363
364 enabledCPUs++;
365
366 if (enabledCPUs == numCPUs) thread_wakeup(this);
367 }
368
369 IOReturn IOCPUInterruptController::registerInterrupt(IOService *nub,
370 int source,
371 void *target,
372 IOInterruptHandler handler,
373 void *refCon)
374 {
375 IOInterruptVector *vector;
376
377 if (source >= numCPUs) return kIOReturnNoResources;
378
379 vector = &vectors[source];
380
381 // Get the lock for this vector.
382 IOTakeLock(vector->interruptLock);
383
384 // Make sure the vector is not in use.
385 if (vector->interruptRegistered) {
386 IOUnlock(vector->interruptLock);
387 return kIOReturnNoResources;
388 }
389
390 // Fill in vector with the client's info.
391 vector->handler = handler;
392 vector->nub = nub;
393 vector->source = source;
394 vector->target = target;
395 vector->refCon = refCon;
396
397 // Get the vector ready. It starts hard disabled.
398 vector->interruptDisabledHard = 1;
399 vector->interruptDisabledSoft = 1;
400 vector->interruptRegistered = 1;
401
402 IOUnlock(vector->interruptLock);
403
404 if (enabledCPUs != numCPUs) {
405 assert_wait(this, THREAD_UNINT);
406 thread_block(THREAD_CONTINUE_NULL);
407 }
408
409 return kIOReturnSuccess;
410 }
411
412 IOReturn IOCPUInterruptController::getInterruptType(IOService */*nub*/,
413 int /*source*/,
414 int *interruptType)
415 {
416 if (interruptType == 0) return kIOReturnBadArgument;
417
418 *interruptType = kIOInterruptTypeLevel;
419
420 return kIOReturnSuccess;
421 }
422
423 IOReturn IOCPUInterruptController::enableInterrupt(IOService */*nub*/,
424 int /*source*/)
425 {
426 // ml_set_interrupts_enabled(true);
427 return kIOReturnSuccess;
428 }
429
430 IOReturn IOCPUInterruptController::disableInterrupt(IOService */*nub*/,
431 int /*source*/)
432 {
433 // ml_set_interrupts_enabled(false);
434 return kIOReturnSuccess;
435 }
436
437 IOReturn IOCPUInterruptController::causeInterrupt(IOService */*nub*/,
438 int /*source*/)
439 {
440 ml_cause_interrupt();
441 return kIOReturnSuccess;
442 }
443
444 IOReturn IOCPUInterruptController::handleInterrupt(void */*refCon*/,
445 IOService */*nub*/,
446 int source)
447 {
448 IOInterruptVector *vector;
449
450 vector = &vectors[source];
451
452 if (!vector->interruptRegistered) return kIOReturnInvalid;
453
454 vector->handler(vector->target, vector->refCon,
455 vector->nub, vector->source);
456
457 return kIOReturnSuccess;
458 }
459
460 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */