xref: /titanic_50/usr/src/uts/i86pc/io/ppm/acpisleep.c (revision 5b7f77ad52bf657ba49d64d16f527e958d0fb820)
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 2007 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 		PT(PT_RTCW);
129 		if (AcpiEnableEvent(ACPI_EVENT_RTC, 0) != AE_OK) {
130 			PT(PT_RTCW_FAIL);
131 			PMD(PMD_SX, ("Can't EnableEvent(RTC)\n"))
132 		}
133 
134 		/*
135 		 * Set RTC to wake us in a wee while.
136 		 */
137 		mutex_enter(&tod_lock);
138 		PT(PT_TOD);
139 		TODOP_SETWAKE(tod_ops, acpi_rtc_wake);
140 		mutex_exit(&tod_lock);
141 	}
142 
143 	/*
144 	 * Prepare for sleep ... could've done this earlier?
145 	 */
146 	PT(PT_SXP);
147 	PMD(PMD_SX, ("Calling AcpiEnterSleepStatePrep(%d) ...\n", Sx))
148 	if (AcpiEnterSleepStatePrep(Sx) != AE_OK) {
149 		PMD(PMD_SX, ("... failed\n!"))
150 		goto insomnia;
151 	}
152 
153 	switch (s3ap->s3a_test_point) {
154 	case DEVICE_SUSPEND_TO_RAM:
155 	case FORCE_SUSPEND_TO_RAM:
156 	case LOOP_BACK_PASS:
157 		return (0);
158 	case LOOP_BACK_FAIL:
159 		return (1);
160 	default:
161 		ASSERT(s3ap->s3a_test_point == LOOP_BACK_NONE);
162 	}
163 
164 	/*
165 	 * Tell the hardware to sleep.
166 	 */
167 	PT(PT_SXE);
168 	PMD(PMD_SX, ("Calling AcpiEnterSleepState(%d) ...\n", Sx))
169 	if (AcpiEnterSleepState(Sx) != AE_OK) {
170 		PT(PT_SXE_FAIL);
171 		PMD(PMD_SX, ("... failed!\n"))
172 	}
173 
174 insomnia:
175 	PT(PT_INSOM);
176 	/* cleanup is done in the caller */
177 	return (1);
178 }
179 
180 int
181 acpi_exit_sleepstate(s3a_t *s3ap)
182 {
183 	int Sx = s3ap->s3a_state;
184 
185 	PT(PT_WOKE);
186 	PMD(PMD_SX, ("!We woke up!\n"))
187 
188 	PT(PT_LSS);
189 	if (AcpiLeaveSleepState(Sx) != AE_OK) {
190 		PT(PT_LSS_FAIL);
191 		PMD(PMD_SX, ("Problem with LeaveSleepState!\n"))
192 	}
193 
194 	PT(PT_DPB);
195 	if (AcpiDisableEvent(ACPI_EVENT_POWER_BUTTON, 0) != AE_OK) {
196 		PT(PT_DPB_FAIL);
197 		PMD(PMD_SX, ("Problem w/ DisableEvent(POWER_BUTTON)\n"))
198 	}
199 	if (acpi_rtc_wake > 0 &&
200 	    AcpiDisableEvent(ACPI_EVENT_RTC, 0) != AE_OK) {
201 		PT(PT_DRTC_FAIL);
202 		PMD(PMD_SX, ("Problem w/ DisableEvent(RTC)\n"))
203 	}
204 
205 	PMD(PMD_SX, ("Restore state of APICs\n"))
206 
207 	/* Restore state of APICs */
208 	PT(PT_ACPIREINIT);
209 	reinit_picmode();
210 	PT(PT_ACPIRESTORE);
211 
212 	PMD(PMD_SX, ("Exiting acpi_sleepstate() => 0\n"))
213 
214 	return (0);
215 }
216