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