xref: /openbsd-src/sys/kern/subr_suspend.c (revision c1a45aed656e7d5627c30c92421893a76f370ccb)
1 /* $OpenBSD: subr_suspend.c,v 1.10 2022/02/16 16:44:17 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 #include "wsdisplay.h"
37 
38 int
39 sleep_state(void *v, int sleepmode)
40 {
41 	int error, s;
42 	extern int perflevel;
43 	size_t rndbuflen;
44 	char *rndbuf;
45 #if NSOFTRAID > 0
46 	extern void sr_quiesce(void);
47 #endif
48 
49 top:
50 	error = ENXIO;
51 	rndbuf = NULL;
52 	rndbuflen = 0;
53 
54 	if (sleep_showstate(v, sleepmode))
55 		return EOPNOTSUPP;
56 #if NWSDISPLAY > 0
57 	wsdisplay_suspend();
58 #endif
59 	stop_periodic_resettodr();
60 
61 #ifdef HIBERNATE
62 	if (sleepmode == SLEEP_HIBERNATE) {
63 		/*
64 		 * Discard useless memory to reduce fragmentation,
65 		 * and attempt to create a hibernate work area
66 		 */
67 		hibernate_suspend_bufcache();
68 		uvmpd_hibernate();
69 		if (hibernate_alloc()) {
70 			printf("failed to allocate hibernate memory\n");
71 			sleep_abort(v);
72 			error = ENOMEM;
73 			goto fail_hiballoc;
74 		}
75 	}
76 #endif /* HIBERNATE */
77 
78 	sensor_quiesce();
79 	if (config_suspend_all(DVACT_QUIESCE)) {
80 		sleep_abort(v);
81 		error = EIO;
82 		goto fail_quiesce;
83 	}
84 
85 	vfs_stall(curproc, 1);
86 #if NSOFTRAID > 0
87 	sr_quiesce();
88 #endif
89 	bufq_quiesce();
90 #ifdef MULTIPROCESSOR
91 	sched_stop_secondary_cpus();
92 	KASSERT(CPU_IS_PRIMARY(curcpu()));
93 	sleep_mp();
94 #endif
95 
96 #ifdef HIBERNATE
97 	if (sleepmode == SLEEP_HIBERNATE) {
98 		/*
99 		 * We've just done various forms of syncing to disk
100 		 * churned lots of memory dirty.  We don't need to
101 		 * save that dirty memory to hibernate, so release it.
102 		 */
103 		hibernate_suspend_bufcache();
104 		uvmpd_hibernate();
105 	}
106 #endif /* HIBERNATE */
107 
108 	resettodr();
109 
110 	s = splhigh();
111 	intr_disable();	/* PSL_I for resume; PIC/APIC broken until repair */
112 	cold = 2;	/* Force other code to delay() instead of tsleep() */
113 
114 	if (config_suspend_all(DVACT_SUSPEND) != 0) {
115 		sleep_abort(v);
116 		error = EDEADLK;
117 		goto fail_suspend;
118 	}
119 	suspend_randomness();
120 	if (sleep_setstate(v)) {
121 		sleep_abort(v);
122 		error = ENOTBLK;
123 		goto fail_pts;
124 	}
125 
126 	if (sleepmode == SLEEP_SUSPEND) {
127 		/*
128 		 * XXX
129 		 * Flag to disk drivers that they should "power down" the disk
130 		 * when we get to DVACT_POWERDOWN.
131 		 */
132 		boothowto |= RB_POWERDOWN;
133 		config_suspend_all(DVACT_POWERDOWN);
134 		boothowto &= ~RB_POWERDOWN;
135 	}
136 
137 	error = gosleep(v);
138 
139 #ifdef HIBERNATE
140 	if (sleepmode == SLEEP_HIBERNATE) {
141 		uvm_pmr_dirty_everything();
142 		hib_getentropy(&rndbuf, &rndbuflen);
143 	}
144 #endif /* HIBERNATE */
145 
146 fail_pts:
147 	config_suspend_all(DVACT_RESUME);
148 
149 fail_suspend:
150 	cold = 0;
151 	intr_enable();
152 	splx(s);
153 
154 	inittodr(gettime());
155 	sleep_resume(v);
156 	resume_randomness(rndbuf, rndbuflen);
157 #ifdef MULTIPROCESSOR
158 	resume_mp();
159 	sched_start_secondary_cpus();
160 #endif
161 	vfs_stall(curproc, 0);
162 	bufq_restart();
163 
164 fail_quiesce:
165 	config_suspend_all(DVACT_WAKEUP);
166 	sensor_restart();
167 
168 #ifdef HIBERNATE
169 	if (sleepmode == SLEEP_HIBERNATE) {
170 		hibernate_free();
171 fail_hiballoc:
172 		hibernate_resume_bufcache();
173 	}
174 #endif /* HIBERNATE */
175 
176 	start_periodic_resettodr();
177 #if NWSDISPLAY > 0
178 	wsdisplay_resume();
179 #endif
180 	sys_sync(curproc, NULL, NULL);
181 	if (cpu_setperf != NULL)
182 		cpu_setperf(perflevel);	/* Restore hw.setperf */
183 	if (suspend_finish(v) == EAGAIN)
184 		goto top;
185 	return (error);
186 }
187