xref: /titanic_41/usr/src/uts/i86pc/io/ppm/acpisleep.c (revision 7800901e60d340b6af88e94a2149805dcfcaaf56)
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/machsystm.h>
40 #include <sys/clock.h>
41 
42 #include <sys/cpr_wakecode.h>
43 #include <sys/acpi/acpi.h>
44 
45 #ifdef OLDPMCODE
46 #include "acpi.h"
47 #endif
48 
49 #include	<sys/x86_archext.h>
50 #include	<sys/reboot.h>
51 #include	<sys/cpu_module.h>
52 #include	<sys/kdi.h>
53 
54 /*
55  * S3 stuff
56  */
57 
58 int acpi_rtc_wake = 0x0;		/* wake in N seconds */
59 
60 #if 0	/* debug */
61 static uint8_t	branchbuf[64 * 1024];	/* for the HDT branch trace stuff */
62 #endif	/* debug */
63 
64 extern int boothowto;
65 
66 #define	BOOTCPU	0	/* cpu 0 is always the boot cpu */
67 
68 extern void		kernel_wc_code(void);
69 extern tod_ops_t	*tod_ops;
70 extern int flushes_require_xcalls;
71 extern int tsc_gethrtime_enable;
72 
73 extern cpuset_t cpu_ready_set;
74 extern void *(*cpu_pause_func)(void *);
75 
76 /*
77  * This probably belong in apic.c, along with the save/restore stuff.
78  */
79 static void
80 reinit_picmode(void)
81 {
82 	ACPI_OBJECT_LIST	arglist;
83 	ACPI_OBJECT		arg;
84 	ACPI_STATUS		status;
85 
86 	/* Setup parameter object */
87 	arglist.Count = 1;
88 	arglist.Pointer = &arg;
89 	arg.Type = ACPI_TYPE_INTEGER;
90 	arg.Integer.Value = 1;
91 
92 	status = AcpiEvaluateObject(NULL, "\\_PIC", &arglist, NULL);
93 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
94 		PMD(PMD_SX, ("Method _PIC failed, %d\n", status))
95 	}
96 }
97 
98 
99 /*
100  * This is what we've all been waiting for!
101  */
102 int
103 acpi_enter_sleepstate(s3a_t *s3ap)
104 {
105 	ACPI_PHYSICAL_ADDRESS	wakephys = s3ap->s3a_wakephys;
106 	caddr_t			wakevirt = rm_platter_va;
107 	/*LINTED*/
108 	wakecode_t		*wp = (wakecode_t *)wakevirt;
109 	uint_t			Sx = s3ap->s3a_state;
110 
111 	PT(PT_SWV);
112 	/* Set waking vector */
113 	if (AcpiSetFirmwareWakingVector(wakephys) != AE_OK) {
114 		PT(PT_SWV_FAIL);
115 		PMD(PMD_SX, ("Can't SetFirmwareWakingVector(%lx)\n",
116 		    (long)wakephys))
117 		goto insomnia;
118 	}
119 
120 	PT(PT_EWE);
121 	/* Enable wake events */
122 	if (AcpiEnableEvent(ACPI_EVENT_POWER_BUTTON, 0) != AE_OK) {
123 		PT(PT_EWE_FAIL);
124 		PMD(PMD_SX, ("Can't EnableEvent(POWER_BUTTON)\n"))
125 	}
126 	if (acpi_rtc_wake > 0) {
127 		PT(PT_RTCW);
128 		if (AcpiEnableEvent(ACPI_EVENT_RTC, 0) != AE_OK) {
129 			PT(PT_RTCW_FAIL);
130 			PMD(PMD_SX, ("Can't EnableEvent(RTC)\n"))
131 		}
132 
133 		/*
134 		 * Set RTC to wake us in a wee while.
135 		 */
136 		mutex_enter(&tod_lock);
137 		PT(PT_TOD);
138 		TODOP_SETWAKE(tod_ops, acpi_rtc_wake);
139 		mutex_exit(&tod_lock);
140 	}
141 
142 	/*
143 	 * Prepare for sleep ... could've done this earlier?
144 	 */
145 	PT(PT_SXP);
146 	PMD(PMD_SX, ("Calling AcpiEnterSleepStatePrep(%d) ...\n", Sx))
147 	if (AcpiEnterSleepStatePrep(Sx) != AE_OK) {
148 		PMD(PMD_SX, ("... failed\n!"))
149 		goto insomnia;
150 	}
151 
152 	switch (s3ap->s3a_test_point) {
153 	case DEVICE_SUSPEND_TO_RAM:
154 	case LOOP_BACK_PASS:
155 		return (0);
156 	case LOOP_BACK_FAIL:
157 		return (1);
158 	default:
159 		ASSERT(s3ap->s3a_test_point == LOOP_BACK_NONE ||
160 		    s3ap->s3a_test_point == FORCE_SUSPEND_TO_RAM);
161 	}
162 
163 	/*
164 	 * Tell the hardware to sleep.
165 	 */
166 	PT(PT_SXE);
167 	PMD(PMD_SX, ("Calling AcpiEnterSleepState(%d) ...\n", Sx))
168 	if (AcpiEnterSleepState(Sx) != AE_OK) {
169 		PT(PT_SXE_FAIL);
170 		PMD(PMD_SX, ("... failed!\n"))
171 	}
172 
173 insomnia:
174 	PT(PT_INSOM);
175 	/* cleanup is done in the caller */
176 	return (1);
177 }
178 
179 int
180 acpi_exit_sleepstate(s3a_t *s3ap)
181 {
182 	int Sx = s3ap->s3a_state;
183 
184 	PT(PT_WOKE);
185 	PMD(PMD_SX, ("!We woke up!\n"))
186 
187 	PT(PT_LSS);
188 	if (AcpiLeaveSleepState(Sx) != AE_OK) {
189 		PT(PT_LSS_FAIL);
190 		PMD(PMD_SX, ("Problem with LeaveSleepState!\n"))
191 	}
192 
193 	PT(PT_DPB);
194 	if (AcpiDisableEvent(ACPI_EVENT_POWER_BUTTON, 0) != AE_OK) {
195 		PT(PT_DPB_FAIL);
196 		PMD(PMD_SX, ("Problem w/ DisableEvent(POWER_BUTTON)\n"))
197 	}
198 	if (acpi_rtc_wake > 0 &&
199 	    AcpiDisableEvent(ACPI_EVENT_RTC, 0) != AE_OK) {
200 		PT(PT_DRTC_FAIL);
201 		PMD(PMD_SX, ("Problem w/ DisableEvent(RTC)\n"))
202 	}
203 
204 	PMD(PMD_SX, ("Restore state of APICs\n"))
205 
206 	/* Restore state of APICs */
207 	PT(PT_ACPIREINIT);
208 	reinit_picmode();
209 	PT(PT_ACPIRESTORE);
210 
211 	PMD(PMD_SX, ("Exiting acpi_sleepstate() => 0\n"))
212 
213 	return (0);
214 }
215