]> git.saurik.com Git - apple/xnu.git/blob - iokit/Drivers/platform/drvAppleNMI/AppleNMI.cpp
74ceaaa95d525f6fa83d6efb0f5748efef0f7a79
[apple/xnu.git] / iokit / Drivers / platform / drvAppleNMI / AppleNMI.cpp
1 /*
2 * Copyright (c) 1998-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) 1998-9 Apple Computer, Inc. All rights reserved.
27 *
28 * DRI: Josh de Cesare
29 *
30 */
31
32 #include <IOKit/IOTypes.h>
33 #include <IOKit/IOLib.h>
34 #include <IOKit/pwr_mgt/RootDomain.h>
35
36 #include <IOKit/platform/AppleNMI.h>
37
38 extern "C" {
39 #include <pexpert/pexpert.h>
40 }
41
42 bool RootRegistered( OSObject * us, void *, IOService * yourDevice );
43
44 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
45
46 #define super IOService
47
48 OSDefineMetaClassAndStructors(AppleNMI, IOService);
49 OSMetaClassDefineReservedUnused(AppleNMI, 0);
50 OSMetaClassDefineReservedUnused(AppleNMI, 1);
51 OSMetaClassDefineReservedUnused(AppleNMI, 2);
52 OSMetaClassDefineReservedUnused(AppleNMI, 3);
53
54 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
55
56 bool AppleNMI::start(IOService *provider)
57 {
58 if (!super::init()) return false;
59
60 enable_debugger = FALSE;
61 mask_NMI = FALSE;
62
63 if (provider->getProperty("enable_debugger"))
64 enable_debugger = TRUE; // Flag to automatically jump to debugger at NMI press
65
66 if (provider->getProperty("mask_NMI"))
67 mask_NMI = TRUE; // Flag to mask/unmask NMI @ sleep/wake
68
69 // Get notified when Root Domain registers
70 addNotification( gIOPublishNotification, serviceMatching("IOPMrootDomain"), (IOServiceNotificationHandler)RootRegistered, this, 0 );
71
72 // Register the interrupt.
73 provider->registerInterrupt(0, this, (IOInterruptAction) &AppleNMI::handleInterrupt, 0);
74 provider->enableInterrupt(0);
75
76 return true;
77 }
78
79 // **********************************************************************************
80 // The Root Power Domain has registered, so now we register as an interested driver
81 // so we know when the system is going to sleep or wake
82 // **********************************************************************************
83 bool RootRegistered( OSObject * us, void *, IOService * yourDevice )
84 {
85 if ( yourDevice != NULL ) {
86 ((AppleNMI *)us)->rootDomain = yourDevice;
87 ((IOPMrootDomain *)yourDevice)->registerInterestedDriver((IOService *) us);
88 }
89
90 return true;
91 }
92
93 IOReturn AppleNMI::initNMI(IOInterruptController *parentController, OSData *parentSource)
94 {
95 // Allocate the IOInterruptSource so this can act like a nub.
96 _interruptSources = (IOInterruptSource *)IOMalloc(sizeof(IOInterruptSource));
97 if (_interruptSources == 0) return kIOReturnNoMemory;
98 _numInterruptSources = 1;
99
100 // Set up the IOInterruptSource to point at this.
101 _interruptSources[0].interruptController = parentController;
102 _interruptSources[0].vectorData = parentSource;
103
104 // call start using itself as its provider.
105 if (!start(this)) return kIOReturnError;
106
107 return kIOReturnSuccess;
108 }
109
110 IOReturn AppleNMI::handleInterrupt(void * /*refCon*/, IOService * /*nub*/, int /*source*/)
111 {
112 if(enable_debugger == TRUE)
113 Debugger("NMI"); // This is a direct call to the Debugger
114 else
115 PE_enter_debugger("NMI"); // This is a indirect call the Debugger that is dependent on the debug flag
116
117 return kIOReturnSuccess;
118 }
119
120 //*********************************************************************************
121 // powerStateWillChangeTo
122 //
123 // We are notified here of power changes in the root domain. The root domain
124 // cannot actually turn itself on and off, but it notifies us anyway.
125 //*********************************************************************************
126 IOReturn AppleNMI::powerStateWillChangeTo ( IOPMPowerFlags theFlags, unsigned long, IOService*)
127 {
128 volatile unsigned long *nmiIntSourceAddr;
129 unsigned long nmiIntSource;
130
131 if (mask_NMI == TRUE)
132 {
133 if ( ! (theFlags & IOPMPowerOn) )
134 {
135 IOLog("AppleNMI mask NMI\n");
136
137 // Mask NMI and change from edge to level whilst sleeping (copied directly from OS9 code)
138 nmiIntSourceAddr = (volatile unsigned long *)kExtInt9_NMIIntSource;
139 nmiIntSource = ml_phys_read(nmiIntSourceAddr);
140 nmiIntSource |= kNMIIntLevelMask;
141 ml_phys_write(nmiIntSourceAddr, nmiIntSource);
142 eieio();
143 nmiIntSource |= kNMIIntMask;
144 ml_phys_write(nmiIntSourceAddr, nmiIntSource);
145 eieio();
146 }
147 else
148 {
149 IOLog("AppleNMI unmask NMI\n");
150
151 // Unmask NMI and change back to edge (copied directly from OS9 code)
152 nmiIntSourceAddr = (volatile unsigned long *)kExtInt9_NMIIntSource;
153 nmiIntSource = ml_phys_read(nmiIntSourceAddr);
154 nmiIntSource &= ~kNMIIntLevelMask;
155 ml_phys_write(nmiIntSourceAddr, nmiIntSource);
156 eieio();
157 nmiIntSource &= ~kNMIIntMask;
158 ml_phys_write(nmiIntSourceAddr, nmiIntSource);
159 eieio();
160 }
161 }
162
163 return IOPMAckImplied;
164 }