xref: /titanic_50/usr/src/uts/i86pc/io/ppm/acpisleep.c (revision 142c9f13e148d687426ed2d4e8bd93717eeaebbc)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/smp_impldefs.h>
31 #include <sys/promif.h>
32 
33 #include <sys/kmem.h>
34 #include <sys/archsystm.h>
35 #include <sys/cpuvar.h>
36 #include <sys/pte.h>
37 #include <vm/seg_kmem.h>
38 #include <sys/epm.h>
39 #include <sys/cpr.h>
40 #include <sys/machsystm.h>
41 #include <sys/clock.h>
42 
43 #include <sys/cpr_wakecode.h>
44 #include <sys/acpi/acpi.h>
45 
46 #ifdef OLDPMCODE
47 #include "acpi.h"
48 #endif
49 
50 #include	<sys/x86_archext.h>
51 #include	<sys/reboot.h>
52 #include	<sys/cpu_module.h>
53 #include	<sys/kdi.h>
54 
55 /*
56  * S3 stuff
57  */
58 
59 int acpi_rtc_wake = 0x0;		/* wake in N seconds */
60 
61 #if 0	/* debug */
62 static uint8_t	branchbuf[64 * 1024];	/* for the HDT branch trace stuff */
63 #endif	/* debug */
64 
65 extern int boothowto;
66 
67 #define	BOOTCPU	0	/* cpu 0 is always the boot cpu */
68 
69 extern void		kernel_wc_code(void);
70 extern tod_ops_t	*tod_ops;
71 extern int flushes_require_xcalls;
72 extern int tsc_gethrtime_enable;
73 
74 extern cpuset_t cpu_ready_set;
75 extern void *(*cpu_pause_func)(void *);
76 
77 /*
78  * This probably belong in apic.c, along with the save/restore stuff.
79  */
80 static void
81 reinit_picmode(void)
82 {
83 	ACPI_OBJECT_LIST	arglist;
84 	ACPI_OBJECT		arg;
85 	ACPI_STATUS		status;
86 
87 	/* Setup parameter object */
88 	arglist.Count = 1;
89 	arglist.Pointer = &arg;
90 	arg.Type = ACPI_TYPE_INTEGER;
91 	arg.Integer.Value = 1;
92 
93 	status = AcpiEvaluateObject(NULL, "\\_PIC", &arglist, NULL);
94 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
95 		PMD(PMD_SX, ("Method _PIC failed, %d\n", status))
96 	}
97 }
98 
99 
100 /*
101  * This is what we've all been waiting for!
102  */
103 int
104 acpi_enter_sleepstate(s3a_t *s3ap)
105 {
106 	ACPI_PHYSICAL_ADDRESS	wakephys = s3ap->s3a_wakephys;
107 	caddr_t			wakevirt = rm_platter_va;
108 	/*LINTED*/
109 	wakecode_t		*wp = (wakecode_t *)wakevirt;
110 	uint_t			Sx = s3ap->s3a_state;
111 
112 	PT(PT_SWV);
113 	/* Set waking vector */
114 	if (AcpiSetFirmwareWakingVector(wakephys) != AE_OK) {
115 		PT(PT_SWV_FAIL);
116 		PMD(PMD_SX, ("Can't SetFirmwareWakingVector(%lx)\n",
117 		    (long)wakephys))
118 		goto insomnia;
119 	}
120 
121 	PT(PT_EWE);
122 	/* Enable wake events */
123 	if (AcpiEnableEvent(ACPI_EVENT_POWER_BUTTON, 0) != AE_OK) {
124 		PT(PT_EWE_FAIL);
125 		PMD(PMD_SX, ("Can't EnableEvent(POWER_BUTTON)\n"))
126 	}
127 	if (acpi_rtc_wake > 0) {
128 		(void) AcpiSetRegister(ACPI_BITREG_RT_CLOCK_STATUS, 1,
129 		    ACPI_MTX_DO_NOT_LOCK); /* clear the RTC bit first */
130 		PT(PT_RTCW);
131 		if (AcpiEnableEvent(ACPI_EVENT_RTC, 0) != AE_OK) {
132 			PT(PT_RTCW_FAIL);
133 			PMD(PMD_SX, ("Can't EnableEvent(RTC)\n"))
134 		}
135 
136 		/*
137 		 * Set RTC to wake us in a wee while.
138 		 */
139 		mutex_enter(&tod_lock);
140 		PT(PT_TOD);
141 		TODOP_SETWAKE(tod_ops, acpi_rtc_wake);
142 		mutex_exit(&tod_lock);
143 	}
144 
145 	/*
146 	 * Prepare for sleep ... could've done this earlier?
147 	 */
148 	PT(PT_SXP);
149 	PMD(PMD_SX, ("Calling AcpiEnterSleepStatePrep(%d) ...\n", Sx))
150 	if (AcpiEnterSleepStatePrep(Sx) != AE_OK) {
151 		PMD(PMD_SX, ("... failed\n!"))
152 		goto insomnia;
153 	}
154 
155 	switch (s3ap->s3a_test_point) {
156 	case DEVICE_SUSPEND_TO_RAM:
157 	case FORCE_SUSPEND_TO_RAM:
158 	case LOOP_BACK_PASS:
159 		return (0);
160 	case LOOP_BACK_FAIL:
161 		return (1);
162 	default:
163 		ASSERT(s3ap->s3a_test_point == LOOP_BACK_NONE);
164 	}
165 
166 	/*
167 	 * Tell the hardware to sleep.
168 	 */
169 	PT(PT_SXE);
170 	PMD(PMD_SX, ("Calling AcpiEnterSleepState(%d) ...\n", Sx))
171 	if (AcpiEnterSleepState(Sx) != AE_OK) {
172 		PT(PT_SXE_FAIL);
173 		PMD(PMD_SX, ("... failed!\n"))
174 	}
175 
176 insomnia:
177 	PT(PT_INSOM);
178 	/* cleanup is done in the caller */
179 	return (1);
180 }
181 
182 int
183 acpi_exit_sleepstate(s3a_t *s3ap)
184 {
185 	int Sx = s3ap->s3a_state;
186 
187 	PT(PT_WOKE);
188 	PMD(PMD_SX, ("!We woke up!\n"))
189 
190 	PT(PT_LSS);
191 	if (AcpiLeaveSleepState(Sx) != AE_OK) {
192 		PT(PT_LSS_FAIL);
193 		PMD(PMD_SX, ("Problem with LeaveSleepState!\n"))
194 	}
195 
196 	PT(PT_DPB);
197 	if (AcpiDisableEvent(ACPI_EVENT_POWER_BUTTON, 0) != AE_OK) {
198 		PT(PT_DPB_FAIL);
199 		PMD(PMD_SX, ("Problem w/ DisableEvent(POWER_BUTTON)\n"))
200 	}
201 	if (acpi_rtc_wake > 0 &&
202 	    AcpiDisableEvent(ACPI_EVENT_RTC, 0) != AE_OK) {
203 		PT(PT_DRTC_FAIL);
204 		PMD(PMD_SX, ("Problem w/ DisableEvent(RTC)\n"))
205 	}
206 
207 	PMD(PMD_SX, ("Restore state of APICs\n"))
208 
209 	/* Restore state of APICs */
210 	PT(PT_ACPIREINIT);
211 	reinit_picmode();
212 	PT(PT_ACPIRESTORE);
213 
214 	PMD(PMD_SX, ("Exiting acpi_sleepstate() => 0\n"))
215 
216 	return (0);
217 }
218