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