xref: /freebsd/sys/compat/linuxkpi/common/src/linux_acpi.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2018 Johannes Lundberg <johalun@FreeBSD.org>
5  * Copyright (c) 2020 Vladimir Kondratyev <wulf@FreeBSD.org>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are
9  * met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in
14  *    the documentation and/or other materials provided with the
15  *    distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include "opt_acpi.h"
31 
32 #include <sys/types.h>
33 #include <sys/bus.h>
34 #include <sys/eventhandler.h>
35 #include <sys/kernel.h>
36 
37 #include <contrib/dev/acpica/include/acpi.h>
38 #include <dev/acpica/acpivar.h>
39 
40 #include <linux/notifier.h>
41 #include <linux/suspend.h>
42 
43 #include <acpi/acpi_bus.h>
44 #include <acpi/video.h>
45 
46 #define	ACPI_AC_CLASS	"ac_adapter"
47 
48 ACPI_MODULE_NAME("linux_acpi")
49 
50 enum {
51 	LINUX_ACPI_ACAD,
52 	LINUX_ACPI_VIDEO,
53 	LINUX_ACPI_TAGS			/* must be last */
54 };
55 _Static_assert(LINUX_ACPI_TAGS <= LINUX_NOTIFY_TAGS,
56     "Not enough space for tags in notifier_block structure");
57 
58 #ifdef DEV_ACPI
59 
60 suspend_state_t pm_suspend_target_state = PM_SUSPEND_ON;
61 
62 static uint32_t linux_acpi_target_sleep_state = ACPI_STATE_S0;
63 
64 static eventhandler_tag resume_tag;
65 static eventhandler_tag suspend_tag;
66 
67 ACPI_HANDLE
68 bsd_acpi_get_handle(device_t bsddev)
69 {
70 	return (acpi_get_handle(bsddev));
71 }
72 
73 bool
74 acpi_check_dsm(ACPI_HANDLE handle, const char *uuid, int rev, uint64_t funcs)
75 {
76 
77 	if (funcs == 0)
78 		return (false);
79 
80 	/*
81 	 * From ACPI 6.3 spec 9.1.1:
82 	 * Bit 0 indicates whether there is support for any functions other
83 	 * than function 0 for the specified UUID and Revision ID. If set to
84 	 * zero, no functions are supported (other than function zero) for the
85 	 * specified UUID and Revision ID.
86 	 */
87 	funcs |= 1 << 0;
88 
89 	return ((acpi_DSMQuery(handle, uuid, rev) & funcs) == funcs);
90 }
91 
92 ACPI_OBJECT *
93 acpi_evaluate_dsm_typed(ACPI_HANDLE handle, const char *uuid, int rev,
94     int func, ACPI_OBJECT *argv4, ACPI_OBJECT_TYPE type)
95 {
96 	ACPI_BUFFER buf;
97 
98 	return (ACPI_SUCCESS(acpi_EvaluateDSMTyped(handle, uuid, rev, func,
99 	    argv4, &buf, type)) ? (ACPI_OBJECT *)buf.Pointer : NULL);
100 }
101 
102 static void
103 linux_handle_power_suspend_event(void *arg __unused)
104 {
105 	/*
106 	 * Only support S3 for now.
107 	 * acpi_sleep_event isn't always called so we use power_suspend_early
108 	 * instead which means we don't know what state we're switching to.
109 	 * TODO: Make acpi_sleep_event consistent
110 	 */
111 	linux_acpi_target_sleep_state = ACPI_STATE_S3;
112 }
113 
114 static void
115 linux_handle_power_resume_event(void *arg __unused)
116 {
117 	linux_acpi_target_sleep_state = ACPI_STATE_S0;
118 }
119 
120 static void
121 linux_handle_acpi_acad_event(void *arg, int data)
122 {
123 	struct notifier_block *nb = arg;
124 	/*
125 	 * Event type information is lost ATM in FreeBSD ACPI event handler.
126 	 * Fortunately, drm-kmod do not distinct AC event types too, so we can
127 	 * use any type e.g. ACPI_NOTIFY_BUS_CHECK that suits notifier handler.
128 	 */
129 	struct acpi_bus_event abe = {
130 	    .device_class = ACPI_AC_CLASS,
131 	    .type = ACPI_NOTIFY_BUS_CHECK,
132 	    .data = data,
133 	};
134 
135 	nb->notifier_call(nb, 0, &abe);
136 }
137 
138 static void
139 linux_handle_acpi_video_event(void *arg, int type)
140 {
141 	struct notifier_block *nb = arg;
142 	struct acpi_bus_event abe = {
143 	    .device_class = ACPI_VIDEO_CLASS,
144 	    .type = type,
145 	    .data = 0,
146 	};
147 
148 	nb->notifier_call(nb, 0, &abe);
149 }
150 
151 int
152 register_acpi_notifier(struct notifier_block *nb)
153 {
154 	nb->tags[LINUX_ACPI_ACAD] = EVENTHANDLER_REGISTER(acpi_acad_event,
155 	    linux_handle_acpi_acad_event, nb, EVENTHANDLER_PRI_FIRST);
156 	nb->tags[LINUX_ACPI_VIDEO] = EVENTHANDLER_REGISTER(acpi_video_event,
157 	    linux_handle_acpi_video_event, nb, EVENTHANDLER_PRI_FIRST);
158 
159 	return (0);
160 }
161 
162 int
163 unregister_acpi_notifier(struct notifier_block *nb)
164 {
165 	EVENTHANDLER_DEREGISTER(acpi_acad_event, nb->tags[LINUX_ACPI_ACAD]);
166 	EVENTHANDLER_DEREGISTER(acpi_video_event, nb->tags[LINUX_ACPI_VIDEO]);
167 
168 	return (0);
169 }
170 
171 uint32_t
172 acpi_target_system_state(void)
173 {
174 	return (linux_acpi_target_sleep_state);
175 }
176 
177 static void
178 linux_register_acpi_event_handlers(void *arg __unused)
179 {
180 	/*
181 	 * XXX johalun: acpi_{sleep,wakeup}_event can't be trusted, use
182 	 * power_{suspend_early,resume} 'acpiconf -s 3' or 'zzz' will not
183 	 * generate acpi_sleep_event... Lid open or wake on button generates
184 	 * acpi_wakeup_event on one of my Dell laptops but not the other
185 	 * (but it does power on)... is this a general thing?
186 	 */
187 	resume_tag = EVENTHANDLER_REGISTER(power_resume,
188 	    linux_handle_power_resume_event, NULL, EVENTHANDLER_PRI_FIRST);
189 	suspend_tag = EVENTHANDLER_REGISTER(power_suspend_early,
190 	    linux_handle_power_suspend_event, NULL, EVENTHANDLER_PRI_FIRST);
191 }
192 
193 static void
194 linux_deregister_acpi_event_handlers(void *arg __unused)
195 {
196 	EVENTHANDLER_DEREGISTER(power_resume, resume_tag);
197 	EVENTHANDLER_DEREGISTER(power_suspend_early, suspend_tag);
198 }
199 
200 SYSINIT(linux_acpi_events, SI_SUB_DRIVERS, SI_ORDER_ANY,
201     linux_register_acpi_event_handlers, NULL);
202 SYSUNINIT(linux_acpi_events, SI_SUB_DRIVERS, SI_ORDER_ANY,
203     linux_deregister_acpi_event_handlers, NULL);
204 
205 #else	/* !DEV_ACPI */
206 
207 ACPI_HANDLE
208 bsd_acpi_get_handle(device_t bsddev)
209 {
210 	return (NULL);
211 }
212 
213 bool
214 acpi_check_dsm(ACPI_HANDLE handle, const char *uuid, int rev, uint64_t funcs)
215 {
216 	return (false);
217 }
218 
219 ACPI_OBJECT *
220 acpi_evaluate_dsm_typed(ACPI_HANDLE handle, const char *uuid, int rev,
221      int func, ACPI_OBJECT *argv4, ACPI_OBJECT_TYPE type)
222 {
223 	return (NULL);
224 }
225 
226 int
227 register_acpi_notifier(struct notifier_block *nb)
228 {
229 	return (0);
230 }
231 
232 int
233 unregister_acpi_notifier(struct notifier_block *nb)
234 {
235 	return (0);
236 }
237 
238 uint32_t
239 acpi_target_system_state(void)
240 {
241 	return (ACPI_STATE_S0);
242 }
243 
244 #endif	/* !DEV_ACPI */
245