]> git.saurik.com Git - apple/xnu.git/blob - iokit/Drivers/platform/drvAppleNMI/AppleNMI.cpp
084bdabee7ff346818fc766122cb1127ef61f6a2
[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 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*
24 * Copyright (c) 1998-2003 Apple Computer, Inc. All rights reserved.
25 *
26 * DRI: Josh de Cesare
27 *
28 */
29
30 #include <IOKit/IOTypes.h>
31 #include <IOKit/IOLib.h>
32 #include <IOKit/pwr_mgt/RootDomain.h>
33
34 #include <IOKit/platform/AppleNMI.h>
35
36 extern "C" {
37 #include <pexpert/pexpert.h>
38 }
39
40 bool RootRegistered( OSObject * us, void *, IOService * yourDevice );
41
42 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
43
44 #define super IOService
45
46 OSDefineMetaClassAndStructors(AppleNMI, IOService);
47 OSMetaClassDefineReservedUnused(AppleNMI, 0);
48 OSMetaClassDefineReservedUnused(AppleNMI, 1);
49 OSMetaClassDefineReservedUnused(AppleNMI, 2);
50 OSMetaClassDefineReservedUnused(AppleNMI, 3);
51
52 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
53
54 bool AppleNMI::start(IOService *provider)
55 {
56 if (!super::start(provider)) return false;
57
58 enable_debugger = FALSE;
59 mask_NMI = FALSE;
60
61 if (provider->getProperty("enable_debugger"))
62 enable_debugger = TRUE; // Flag to automatically jump to debugger at NMI press
63
64 if (provider->getProperty("mask_NMI"))
65 mask_NMI = TRUE; // Flag to mask/unmask NMI @ sleep/wake
66
67 // Get notified when Root Domain registers
68 addNotification( gIOPublishNotification, serviceMatching("IOPMrootDomain"), (IOServiceNotificationHandler)RootRegistered, this, 0 );
69
70 // Register the interrupt.
71 provider->registerInterrupt(0, this, (IOInterruptAction) &AppleNMI::handleInterrupt, 0);
72 provider->enableInterrupt(0);
73
74 return true;
75 }
76
77 // **********************************************************************************
78 // The Root Power Domain has registered, so now we register as an interested driver
79 // so we know when the system is going to sleep or wake
80 // **********************************************************************************
81 bool RootRegistered( OSObject * us, void *, IOService * yourDevice )
82 {
83 if ( yourDevice != NULL ) {
84 ((AppleNMI *)us)->rootDomain = yourDevice;
85 ((IOPMrootDomain *)yourDevice)->registerInterestedDriver((IOService *) us);
86 }
87
88 return true;
89 }
90
91 IOReturn AppleNMI::initNMI(IOInterruptController *parentController, OSData *parentSource)
92 {
93 return kIOReturnSuccess;
94 }
95
96 IOReturn AppleNMI::handleInterrupt(void * /*refCon*/, IOService * /*nub*/, int /*source*/)
97 {
98 if(enable_debugger == TRUE)
99 Debugger("NMI"); // This is a direct call to the Debugger
100 else
101 PE_enter_debugger("NMI"); // This is a indirect call the Debugger that is dependent on the debug flag
102
103 return kIOReturnSuccess;
104 }
105
106 //*********************************************************************************
107 // powerStateWillChangeTo
108 //
109 // We are notified here of power changes in the root domain. The root domain
110 // cannot actually turn itself on and off, but it notifies us anyway.
111 //*********************************************************************************
112 IOReturn AppleNMI::powerStateWillChangeTo ( IOPMPowerFlags theFlags, unsigned long, IOService*)
113 {
114 volatile unsigned long *nmiIntSourceAddr;
115 unsigned long nmiIntSource;
116
117 if (mask_NMI == TRUE)
118 {
119 if ( ! (theFlags & IOPMPowerOn) )
120 {
121 // Mask NMI and change from edge to level whilst sleeping (copied directly from OS9 code)
122 nmiIntSourceAddr = (volatile unsigned long *)kExtInt9_NMIIntSource;
123 nmiIntSource = ml_phys_read(nmiIntSourceAddr);
124 nmiIntSource |= kNMIIntLevelMask;
125 ml_phys_write(nmiIntSourceAddr, nmiIntSource);
126 eieio();
127 nmiIntSource |= kNMIIntMask;
128 ml_phys_write(nmiIntSourceAddr, nmiIntSource);
129 eieio();
130 }
131 else
132 {
133 // Unmask NMI and change back to edge (copied directly from OS9 code)
134 nmiIntSourceAddr = (volatile unsigned long *)kExtInt9_NMIIntSource;
135 nmiIntSource = ml_phys_read(nmiIntSourceAddr);
136 nmiIntSource &= ~kNMIIntLevelMask;
137 ml_phys_write(nmiIntSourceAddr, nmiIntSource);
138 eieio();
139 nmiIntSource &= ~kNMIIntMask;
140 ml_phys_write(nmiIntSourceAddr, nmiIntSource);
141 eieio();
142 }
143 }
144
145 return IOPMAckImplied;
146 }