#include <sys/cdefs.h>
__BEGIN_DECLS
#include <ppc/proc_reg.h>
__END_DECLS
#include <IOKit/IODeviceTreeSupport.h>
#include <IOKit/IOPlatformExpert.h>
#include <IOKit/IOCPU.h>
#include <IOKit/pci/IOPCIBridge.h>
#include <IOKit/pwr_mgt/RootDomain.h>
#include "MacRISC4CPU.h"
#define kMacRISC_GPIO_DIRECTION_BIT 2
#define super IOCPU
OSDefineMetaClassAndStructors(MacRISC4CPU, IOCPU);
#define kU3IIC "PPCI2CInterface.i2c-uni-n"
static const cpu_timebase_params_t cypress =
{ kU3IIC, 0x00, 0xD0, 0x81,
0x0C, 0x0C, 0x00 };
static const cpu_timebase_params_t pulsar =
{ kU3IIC, 0x00, 0xD4, 0x2E,
0x77, 0x22, 0x11 };
static bool gI2CTransactionComplete;
static IOService *gI2CDriver;
static const cpu_timebase_params_t * gTimeBaseParams;
static IOCPUInterruptController *gCPUIC;
bool MacRISC4CPU::start(IOService *provider)
{
kern_return_t result;
IORegistryEntry *cpusRegEntry, *mpicRegEntry;
OSIterator *cpusIterator;
OSData *tmpData;
IOService *service;
const OSSymbol *interruptControllerName, *mpicICSymbol;
OSData *interruptData, *parentICData;
OSArray *tmpArray;
OSDictionary *matchDict;
char mpicICName[48];
UInt32 maxCPUs, physCPU, mpicPHandle;
IOService *i2cresources;
ml_processor_info_t processor_info;
mpic_getProvider = OSSymbol::withCString("mpic_getProvider");
mpic_getIPIVector= OSSymbol::withCString("mpic_getIPIVector");
mpic_setCurrentTaskPriority = OSSymbol::withCString("mpic_setCurrentTaskPriority");
mpic_setUpForSleep = OSSymbol::withCString("mpic_setUpForSleep");
mpic_dispatchIPI = OSSymbol::withCString("mpic_dispatchIPI");
keyLargo_restoreRegisterState = OSSymbol::withCString("keyLargo_restoreRegisterState");
keyLargo_syncTimeBase = OSSymbol::withCString("keyLargo_syncTimeBase");
keyLargo_saveRegisterState = OSSymbol::withCString("keyLargo_saveRegisterState");
keyLargo_turnOffIO = OSSymbol::withCString("keyLargo_turnOffIO");
keyLargo_writeRegUInt8 = OSSymbol::withCString("keyLargo_writeRegUInt8");
keyLargo_setPowerSupply = OSSymbol::withCString("setPowerSupply");
UniNSetPowerState = OSSymbol::withCString("UniNSetPowerState");
UniNPrepareForSleep = OSSymbol::withCString("UniNPrepareForSleep");
i2c_openI2CBus = OSSymbol::withCString("openI2CBus");
i2c_closeI2CBus = OSSymbol::withCString("closeI2CBus");
i2c_setCombinedMode = OSSymbol::withCString("setCombinedMode");
i2c_readI2CBus = OSSymbol::withCString("readI2CBus");
i2c_writeI2CBus = OSSymbol::withCString("writeI2CBus");
u3APIPhyDisableProcessor1 = OSSymbol::withCString("u3APIPhyDisableProcessor1");
macRISC4PE = OSDynamicCast(MacRISC4PE, getPlatform());
if (macRISC4PE == 0) return false;
if (!super::start(provider)) return false;
numCPUs = 0;
cpusRegEntry = fromPath("/cpus", gIODTPlane);
if (cpusRegEntry == 0) return false;
cpusIterator = cpusRegEntry->getChildIterator(gIODTPlane);
while (cpusIterator->getNextObject()) numCPUs++;
cpusIterator->release();
if (PE_parse_boot_arg("cpus", &maxCPUs))
{
if (numCPUs > maxCPUs) numCPUs = maxCPUs;
}
if (numCPUs > 1 && !gTimeBaseParams)
{
IORegistryEntry * clockchip;
if ((strcmp(macRISC4PE->provider_name, "PowerMac7,2") == 0) || (strcmp(macRISC4PE->provider_name, "PowerMac7,3") == 0))
{
if ((clockchip = fromPath("/u3/i2c/i2c-hwclock@d2", gIODTPlane, 0, 0, 0)) != NULL)
{
clockchip->release();
gTimeBaseParams = &cypress;
}
else if ((clockchip = fromPath("/u3/i2c/i2c-hwclock@d4", gIODTPlane, 0, 0, 0)) != NULL)
{
clockchip->release();
gTimeBaseParams = &pulsar;
}
}
else if (strcmp(macRISC4PE->provider_name, "RackMac3,1") == 0)
{
if ((clockchip = fromPath("/u3/i2c/i2c-hwclock@d4", gIODTPlane, 0, 0, 0)) != NULL)
{
clockchip->release();
gTimeBaseParams = &pulsar;
}
}
if (!gTimeBaseParams)
{
IOLog("WARNING: don't know how to sync MP timebase, limiting to one CPU\n");
kprintf("WARNING: don't know how to sync MP timebase, limiting to one CPU\n");
numCPUs = 1;
}
}
doSleep = false;
flushOnLock = false;
cpusRegEntry = fromPath("/cpus/@0", gIODTPlane);
if (cpusRegEntry == 0) return false;
if (cpusRegEntry->getProperty("flush-on-lock") != 0) flushOnLock = true;
tmpData = OSDynamicCast(OSData, provider->getProperty("reg"));
if (tmpData == 0) return false;
physCPU = *(long *)tmpData->getBytesNoCopy();
setCPUNumber(physCPU);
tmpData = OSDynamicCast(OSData, provider->getProperty("soft-reset"));
if (tmpData == 0)
{
IOLog ("MacRISC4CPU::start - cpu(%ld) - no soft-reset property\n", physCPU);
return false;
} else
soft_reset_offset = *(long *)tmpData->getBytesNoCopy();
bootCPU = false;
tmpData = OSDynamicCast(OSData, provider->getProperty("state"));
if (tmpData == 0) return false;
if (!strcmp((char *)tmpData->getBytesNoCopy(), "running")) bootCPU = true;
if (bootCPU)
{
gCPUIC = new IOCPUInterruptController;
if (gCPUIC == 0) return false;
if (gCPUIC->initCPUInterruptController(numCPUs) != kIOReturnSuccess)
return false;
gCPUIC->attach(this);
gCPUIC->registerCPUInterruptController();
}
tmpData = OSDynamicCast(OSData, provider->getProperty("l2cr"));
if (tmpData != 0)
l2crValue = *(long *)tmpData->getBytesNoCopy() & 0x7FFFFFFF;
else
l2crValue = mfl2cr() & 0x7FFFFFFF;
kprintf ("MacRISC4CPU::start - waiting for KeyLargo\n");
keyLargo = waitForService(serviceMatching("KeyLargo"));
if (keyLargo == 0) return false;
parentICData = OSDynamicCast(OSData, provider->getProperty(kMacRISC4ParentICKey));
mpicPHandle = *(UInt32 *)parentICData->getBytesNoCopy();
sprintf(mpicICName, "IOInterruptController%08lX", mpicPHandle);
mpicICSymbol = OSSymbol::withCString(mpicICName);
matchDict = serviceMatching("AppleMPICInterruptController");
matchDict->setObject("InterruptControllerName", mpicICSymbol);
mpic = waitForService(matchDict);
mpic->callPlatformFunction(mpic_getProvider, false, (void *)&mpicRegEntry, 0, 0, 0);
interruptControllerName = IODTInterruptControllerName(mpicRegEntry);
mpic->callPlatformFunction(mpic_getIPIVector, false, (void *)&physCPU, (void *)&interruptData, 0, 0);
if ((interruptControllerName == 0) || (interruptData == 0)) return false;
tmpArray = OSArray::withCapacity(1);
tmpArray->setObject(interruptControllerName);
cpuNub->setProperty(gIOInterruptControllersKey, tmpArray);
tmpArray->release();
tmpArray = OSArray::withCapacity(1);
tmpArray->setObject(interruptData);
cpuNub->setProperty(gIOInterruptSpecifiersKey, tmpArray);
tmpArray->release();
setCPUState(kIOCPUStateUninitalized);
service = waitForService(serviceMatching("IOPMrootDomain"));
pmRootDomain = OSDynamicCast(IOPMrootDomain, service);
if (pmRootDomain != 0)
{
kprintf("Register MacRISC4CPU %d to acknowledge power changes\n", getCPUNumber());
pmRootDomain->registerInterestedDriver(this);
PMinit();
provider->joinPMtree(this);
}
#if 0 // XXX - incomplete
if (macRISC4PE->ioPPluginNub) {
service = waitForService(resourceMatching("IOPlatformPlugin"));
ioPPlugin = OSDynamicCast (IOService, service->getProperty("IOPlatformPlugin"));
if (!ioPPlugin)
return false;
ioPPluginDict = OSDictionary::withCapacity(2);
if (!ioPPluginDict) {
ioPPlugin = NULL;
} else {
ioPPluginDict->setObject (kIOPPluginTypeKey, OSSymbol::withCString (kIOPPluginTypeCPUCon));
ioPPluginDict->setObject (kIOPPluginCPUIDKey, OSNumber::withNumber ((long long)getCPUNumber(), 32));
if (messageClient (kIOPPluginMessageRegister, ioPPlugin, (void *)ioPPluginDict) != kIOReturnSuccess) {
IOLog ("MacRISC4CPU::start - failed to register cpu with IOPlatformPlugin\n");
ioPPluginDict->release();
ioPPlugin = NULL;
return false;
}
}
}
#endif
registerService();
if (!(uniN = waitForService(serviceMatching("AppleU3")))) return false;
if (numCPUs == 1)
uniN->callPlatformFunction (u3APIPhyDisableProcessor1, false, (void *)0, (void *)0, (void *)0, (void *)0);
if (numCPUs > 1 && !gI2CDriver)
{
i2cresources = waitForService (resourceMatching ( gTimeBaseParams->i2c_iface ));
if (i2cresources)
{
gI2CDriver = OSDynamicCast (IOService, i2cresources->getProperty ( gTimeBaseParams->i2c_iface ));
if (!gI2CDriver)
{
kprintf ("MacRISC4CPU::start(%ld) - failed i2cDriver\n", getCPUNumber());
return false;
}
}
}
if (physCPU < numCPUs)
{
processor_info.cpu_id = (cpu_id_t)this;
processor_info.boot_cpu = bootCPU;
processor_info.start_paddr = 0x0100;
processor_info.l2cr_value = l2crValue;
processor_info.supports_nap = !flushOnLock;
processor_info.time_base_enable = MacRISC4CPU::sEnableCPUTimeBase;
kprintf("MacRISC4CPU::start(%ld) - registering with mach\n", getCPUNumber());
result = ml_processor_register(&processor_info, &machProcessor, &ipi_handler);
if (result == KERN_FAILURE) return false;
processor_start(machProcessor);
}
if (!pmu) {
kprintf("MacRISC4CPU::start(%ld) - waiting for IOPMU\n", getCPUNumber());
service = waitForService(resourceMatching("IOPMU"));
if (service)
if (!(pmu = OSDynamicCast (IOService, service->getProperty("IOPMU")))) return false;
}
uniN->callPlatformFunction ("setupUATAforSleep", false, (void *)0, (void *)0, (void *)0, (void *)0);
kprintf ("MacRISC4CPU::start(%ld) - done\n", getCPUNumber());
return true;
}
void MacRISC4CPU::performPMUSpeedChange (UInt32 newLevel)
{
bool tempRememberNap;
currentProcessorSpeed = newLevel;
tempRememberNap = ml_enable_nap(getCPUNumber(), false);
kprintf ("performPMUSpeedChange - initiating speed change to level %ld, nap %d\n", newLevel, tempRememberNap);
IOLog ("performPMUSpeedChange - initiating speed change to level %ld, nap %d\n", newLevel, tempRememberNap);
processorSpeedChange = true;
pmRootDomain->receivePowerNotification(kIOPMProcessorSpeedChange);
processorSpeedChange = false;
kprintf ("performPMUSpeedChange - completing speed change to level %ld, nap %d\n", newLevel, tempRememberNap);
IOLog ("performPMUSpeedChange - completing speed change to level %ld, nap %d\n", newLevel, tempRememberNap);
ml_enable_nap(getCPUNumber(), tempRememberNap);
return;
}
void MacRISC4CPU::initCPU(bool boot)
{
IOPCIBridge *pciDriver;
UInt32 i;
if (!boot && bootCPU) {
uniN->callPlatformFunction (UniNSetPowerState, false, (void *)kUniNNormal,
(void *)0, (void *)0, (void *)0);
if (numCPUs == 1)
uniN->callPlatformFunction (u3APIPhyDisableProcessor1, false, (void *)0, (void *)0, (void *)0, (void *)0);
if (!processorSpeedChange) {
for (i = 0; i < topLevelPCIBridgeCount; i++)
if (pciDriver = topLevelPCIBridges[i])
pciDriver->setDevicePowerState (NULL, 3);
keyLargo->callPlatformFunction(keyLargo_restoreRegisterState, false, 0, 0, 0, 0);
if (macRISC4PE->getMachineType() == kMacRISC4TypePowerMac) {
kprintf("MacRISC4CPU::initCPU %d -> mpic->setUpForSleep on", getCPUNumber());
mpic->callPlatformFunction(mpic_setUpForSleep, false, (void *)false, (void *)getCPUNumber(), 0, 0);
}
}
}
kprintf("MacRISC4CPU::initCPU %d Here!\n", getCPUNumber());
if (bootCPU)
keyLargo->callPlatformFunction(keyLargo_syncTimeBase, false, 0, 0, 0, 0);
if (boot) {
gCPUIC->enableCPUInterrupt(this);
cpuNub->registerInterrupt(0, this, MacRISC4CPU::sIPIHandler, 0);
cpuNub->enableInterrupt(0);
} else {
long priority = 0;
mpic->callPlatformFunction(mpic_setCurrentTaskPriority, false, (void *)&priority, 0, 0, 0);
}
setCPUState(kIOCPUStateRunning);
kprintf ("MacRISC4CPU::initCPU(%ld) - done\n", getCPUNumber());
return;
}
void MacRISC4CPU::quiesceCPU(void)
{
if (bootCPU)
{
uniN->callPlatformFunction (UniNSetPowerState, false, (void *)(kUniNSleep),
(void *)0, (void *)0, (void *)0);
if (processorSpeedChange) {
pmu->callPlatformFunction("setSpeedNow", false, (void *)currentProcessorSpeed, 0, 0, 0);
} else {
pmu->callPlatformFunction("sleepNow", false, 0, 0, 0, 0);
if (macRISC4PE->getMachineType() == kMacRISC4TypePowerMac)
mpic->callPlatformFunction(mpic_setUpForSleep, false, (void *)true, (void *)getCPUNumber(), 0, 0);
keyLargo->callPlatformFunction(keyLargo_saveRegisterState, false, 0, 0, 0, 0);
keyLargo->callPlatformFunction(keyLargo_turnOffIO, false, (void *)false, 0, 0, 0);
}
ml_phys_write(0x0080, 0x100);
}
ml_ppc_sleep();
return;
}
kern_return_t MacRISC4CPU::startCPU(vm_offset_t , vm_offset_t )
{
long gpioOffset = soft_reset_offset;
unsigned long strobe_value;
if (!gTimeBaseParams)
{
IOLog("MacRISC4CPU::startCPU(%ld) - timebase sync params unknown\n", getCPUNumber() );
return KERN_FAILURE;
}
gI2CTransactionComplete = false;
if (gI2CDriver && (kIOReturnSuccess == gI2CDriver->callPlatformFunction (i2c_openI2CBus, false,
(void *) (UInt32)gTimeBaseParams->i2c_port, (void *) 0, (void *) 0, (void *) 0))) {
kprintf ("MacRISC4CPU::startCPU(%ld) i2c bus opened\n", getCPUNumber());
gI2CDriver->callPlatformFunction (i2c_setCombinedMode, false, (void *) 0, (void *) 0, (void *) 0, (void *) 0); }
strobe_value = ( 1 << kMacRISC_GPIO_DIRECTION_BIT );
keyLargo->callPlatformFunction(keyLargo_writeRegUInt8, false,
(void *)&gpioOffset, (void *)strobe_value, 0, 0);
strobe_value = ( 0 << kMacRISC_GPIO_DIRECTION_BIT );
keyLargo->callPlatformFunction(keyLargo_writeRegUInt8, false,
(void *)&gpioOffset, (void *)strobe_value, 0, 0);
while (!gI2CTransactionComplete) IOSleep (10);
gI2CDriver->callPlatformFunction (i2c_closeI2CBus, false, (void *) 0, (void *) 0, (void *) 0, (void *) 0);
kprintf ("MacRISC4CPU::startCPU(%ld) i2c bus closed\n", getCPUNumber());
return KERN_SUCCESS;
}
void MacRISC4CPU::haltCPU(void)
{
OSIterator *childIterator;
IORegistryEntry *childEntry, *childDriver;
IOPCIBridge *pciDriver;
OSData *deviceTypeString;
UInt32 i;
setCPUState(kIOCPUStateStopped);
if (bootCPU)
{
uniN->callPlatformFunction (UniNPrepareForSleep, false,
(void *)0, (void *)0, (void *)0, (void *)0);
if (!topLevelPCIBridgeCount) {
if ((childIterator = macRISC4PE->getChildIterator (gIOServicePlane)) != NULL) {
while ((childEntry = (IORegistryEntry *)(childIterator->getNextObject ())) != NULL) {
deviceTypeString = OSDynamicCast( OSData, childEntry->getProperty( "device_type" ));
if (deviceTypeString) {
if (!strcmp((const char *)deviceTypeString->getBytesNoCopy(), "pci")) {
childDriver = childEntry->copyChildEntry(gIOServicePlane);
if (childDriver) {
pciDriver = OSDynamicCast( IOPCIBridge, childDriver );
if (pciDriver)
if (topLevelPCIBridgeCount < kMaxPCIBridges)
topLevelPCIBridges[topLevelPCIBridgeCount++] = pciDriver;
else
kprintf ("MacRISC4CPU::haltCPU - warning, more than %ld PCI bridges - cannot save/restore them all\n", kMaxPCIBridges);
childDriver->release();
}
}
}
}
childIterator->release();
}
}
for (i = 0; i < topLevelPCIBridgeCount; i++)
if (pciDriver = topLevelPCIBridges[i]) {
pciDriver->setDevicePowerState (NULL, 2);
}
}
kprintf("MacRISC4CPU::haltCPU %d Here!\n", getCPUNumber());
processor_exit(machProcessor);
return;
}
void MacRISC4CPU::signalCPU(IOCPU *target)
{
UInt32 physCPU = getCPUNumber();
MacRISC4CPU *targetCPU = OSDynamicCast(MacRISC4CPU, target);
if (targetCPU == 0) return;
mpic->callPlatformFunction(mpic_dispatchIPI, false, (void *)&physCPU, (void *)(1 << targetCPU->getCPUNumber()), 0, 0);
return;
}
void MacRISC4CPU::sEnableCPUTimeBase( cpu_id_t self, boolean_t enable )
{
MacRISC4CPU* pe = ( MacRISC4CPU * ) self;
pe->enableCPUTimeBase( enable );
}
void MacRISC4CPU::enableCPUTimeBase(bool enable)
{
UInt8 sevenBitAddr, buf, tmp;
sevenBitAddr = gTimeBaseParams->i2c_addr >> 1;
if (kIOReturnSuccess == gI2CDriver->callPlatformFunction (i2c_readI2CBus, false, (void *)(UInt32)sevenBitAddr, (void *) (UInt32)gTimeBaseParams->i2c_subaddr, (void *)(UInt32)&buf, (void *)1)) {
tmp = enable ? gTimeBaseParams->enable_value : gTimeBaseParams->disable_value;
buf = (buf & ~gTimeBaseParams->mask) | (tmp & gTimeBaseParams->mask);
gI2CDriver->callPlatformFunction (i2c_writeI2CBus, false, (void *)(UInt32)sevenBitAddr,(void *) (UInt32)gTimeBaseParams->i2c_subaddr, (void *)(UInt32)& buf, (void *)1);
} else {
kprintf ("MacRISC4CPU::enableCPUTimeBase - I2C read failed\n");
return;
}
kprintf ("MacRISC4CPU::enableCPUTimeBase(%s) - I2C transaction complete\n", enable ? "enable" : "disable");
gI2CTransactionComplete = enable;
return;
}
void MacRISC4CPU::sIPIHandler( OSObject* self, void* refCon, IOService* nub, int source )
{
MacRISC4CPU* pe = ( MacRISC4CPU * ) self;
pe->ipiHandler( refCon, nub, source );
}
void MacRISC4CPU::ipiHandler(void *refCon, void *nub, int source)
{
if (ipi_handler) ipi_handler();
return;
}
const OSSymbol *MacRISC4CPU::getCPUName(void)
{
char tmpStr[256];
sprintf(tmpStr, "Primary%ld", getCPUNumber());
return OSSymbol::withCString(tmpStr);
}