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