xref: /openbsd-src/sys/kern/subr_suspend.c (revision a9d89b3ac6572d4ab13761993223bb332205ec43)
1 /* $OpenBSD: subr_suspend.c,v 1.3 2022/02/11 01:55:12 deraadt Exp $ */
2 /*
3  * Copyright (c) 2005 Thorsten Lockert <tholo@sigmasoft.com>
4  * Copyright (c) 2005 Jordan Hargrave <jordan@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/param.h>
20 #include <sys/systm.h>
21 #include <sys/buf.h>
22 #include <sys/malloc.h>
23 #include <sys/pool.h>
24 #include <sys/reboot.h>
25 #include <sys/proc.h>
26 #include <sys/sensors.h>
27 #include <sys/sysctl.h>
28 #include <sys/mount.h>
29 #include <sys/syscallargs.h>
30 #include <dev/wscons/wsdisplayvar.h>
31 #ifdef HIBERNATE
32 #include <sys/hibernate.h>
33 #endif
34 
35 #include "softraid.h"
36 
37 int
38 sleep_state(void *v, int sleepmode)
39 {
40 	int error = ENXIO;
41 	extern int perflevel;
42 	size_t rndbuflen = 0;
43 	char *rndbuf = NULL;
44 	int s;
45 #if NSOFTRAID > 0
46 	extern void sr_quiesce(void);
47 #endif
48 
49 	if (sleep_showstate(v, sleepmode))
50 		return EOPNOTSUPP;
51 
52 	display_suspend(v);
53 
54 	stop_periodic_resettodr();
55 
56 #ifdef HIBERNATE
57 	if (sleepmode == SLEEP_HIBERNATE) {
58 		/*
59 		 * Discard useless memory to reduce fragmentation,
60 		 * and attempt to create a hibernate work area
61 		 */
62 		hibernate_suspend_bufcache();
63 		uvmpd_hibernate();
64 		if (hibernate_alloc()) {
65 			printf("failed to allocate hibernate memory\n");
66 			goto fail_alloc;
67 		}
68 	}
69 #endif /* HIBERNATE */
70 
71 	sensor_quiesce();
72 	if (config_suspend_all(DVACT_QUIESCE))
73 		goto fail_quiesce;
74 
75 	vfs_stall(curproc, 1);
76 #if NSOFTRAID > 0
77 	sr_quiesce();
78 #endif
79 	bufq_quiesce();
80 
81 
82 #ifdef MULTIPROCESSOR
83 	sleep_mp();
84 #endif
85 
86 #ifdef HIBERNATE
87 	if (sleepmode == SLEEP_HIBERNATE) {
88 		/*
89 		 * We've just done various forms of syncing to disk
90 		 * churned lots of memory dirty.  We don't need to
91 		 * save that dirty memory to hibernate, so release it.
92 		 */
93 		hibernate_suspend_bufcache();
94 		uvmpd_hibernate();
95 	}
96 #endif /* HIBERNATE */
97 
98 	resettodr();
99 
100 	s = splhigh();
101 	intr_disable();	/* PSL_I for resume; PIC/APIC broken until repair */
102 	cold = 2;	/* Force other code to delay() instead of tsleep() */
103 
104 	if (config_suspend_all(DVACT_SUSPEND) != 0)
105 		goto fail_suspend;
106 
107 	suspend_randomness();
108 
109 	if (sleep_setstate(v))
110 		goto fail_pts;
111 
112 	if (sleepmode == SLEEP_SUSPEND) {
113 		/*
114 		 * XXX
115 		 * Flag to disk drivers that they should "power down" the disk
116 		 * when we get to DVACT_POWERDOWN.
117 		 */
118 		boothowto |= RB_POWERDOWN;
119 		config_suspend_all(DVACT_POWERDOWN);
120 		boothowto &= ~RB_POWERDOWN;
121 	}
122 
123 	gosleep(v);
124 
125 #ifdef HIBERNATE
126 	if (sleepmode == SLEEP_HIBERNATE) {
127 		uvm_pmr_dirty_everything();
128 		hib_getentropy(&rndbuf, &rndbuflen);
129 	}
130 #endif /* HIBERNATE */
131 
132 fail_pts:
133 	config_suspend_all(DVACT_RESUME);
134 
135 fail_suspend:
136 	cold = 0;
137 	intr_enable();
138 	splx(s);
139 
140 	disable_lid_wakeups(v);
141 
142 	inittodr(gettime());
143 
144 	sleep_resume(v);
145 
146 	/* force RNG upper level reseed */
147 	resume_randomness(rndbuf, rndbuflen);
148 
149 #ifdef MULTIPROCESSOR
150 	resume_mp();
151 #endif
152 
153 	vfs_stall(curproc, 0);
154 	bufq_restart();
155 
156 fail_quiesce:
157 	config_suspend_all(DVACT_WAKEUP);
158 	sensor_restart();
159 
160 #ifdef HIBERNATE
161 	if (sleepmode == SLEEP_HIBERNATE) {
162 		hibernate_free();
163 fail_alloc:
164 		hibernate_resume_bufcache();
165 	}
166 #endif /* HIBERNATE */
167 
168 	start_periodic_resettodr();
169 
170 	display_resume(v);
171 
172 	sys_sync(curproc, NULL, NULL);
173 
174 	/* Restore hw.setperf */
175 	if (cpu_setperf != NULL)
176 		cpu_setperf(perflevel);
177 
178 	suspend_finish(v);
179 
180 	return (error);
181 }
182