1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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  * $FreeBSD$
30  */
31 
32 #include "opt_acpi.h"
33 
34 #include <sys/types.h>
35 #include <sys/bus.h>
36 #include <sys/eventhandler.h>
37 #include <sys/kernel.h>
38 
39 #include <contrib/dev/acpica/include/acpi.h>
40 #include <dev/acpica/acpivar.h>
41 
42 #include <linux/notifier.h>
43 
44 #include <acpi/acpi_bus.h>
45 #include <acpi/video.h>
46 
47 #define	ACPI_AC_CLASS	"ac_adapter"
48 
49 ACPI_MODULE_NAME("linux_acpi")
50 
51 enum {
52 	LINUX_ACPI_ACAD,
53 	LINUX_ACPI_VIDEO,
54 	LINUX_ACPI_TAGS			/* must be last */
55 };
56 _Static_assert(LINUX_ACPI_TAGS <= LINUX_NOTIFY_TAGS,
57     "Not enough space for tags in notifier_block structure");
58 
59 #ifdef DEV_ACPI
60 
61 static uint32_t linux_acpi_target_sleep_state = ACPI_STATE_S0;
62 
63 static eventhandler_tag resume_tag;
64 static eventhandler_tag suspend_tag;
65 
66 ACPI_HANDLE
67 bsd_acpi_get_handle(device_t bsddev)
68 {
69 	return (acpi_get_handle(bsddev));
70 }
71 
72 bool
73 acpi_check_dsm(ACPI_HANDLE handle, const char *uuid, int rev, uint64_t funcs)
74 {
75 
76 	if (funcs == 0)
77 		return (false);
78 
79 	/*
80 	 * From ACPI 6.3 spec 9.1.1:
81 	 * Bit 0 indicates whether there is support for any functions other
82 	 * than function 0 for the specified UUID and Revision ID. If set to
83 	 * zero, no functions are supported (other than function zero) for the
84 	 * specified UUID and Revision ID.
85 	 */
86 	funcs |= 1 << 0;
87 
88 	return ((acpi_DSMQuery(handle, uuid, rev) & funcs) == funcs);
89 }
90 
91 ACPI_OBJECT *
92 acpi_evaluate_dsm_typed(ACPI_HANDLE handle, const char *uuid, int rev,
93     int func, ACPI_OBJECT *argv4, ACPI_OBJECT_TYPE type)
94 {
95 	ACPI_BUFFER buf;
96 
97 	return (ACPI_SUCCESS(acpi_EvaluateDSMTyped(handle, uuid, rev, func,
98 	    argv4, &buf, type)) ? (ACPI_OBJECT *)buf.Pointer : NULL);
99 }
100 
101 static void
102 linux_handle_power_suspend_event(void *arg __unused)
103 {
104 	/*
105 	 * Only support S3 for now.
106 	 * acpi_sleep_event isn't always called so we use power_suspend_early
107 	 * instead which means we don't know what state we're switching to.
108 	 * TODO: Make acpi_sleep_event consistent
109 	 */
110 	linux_acpi_target_sleep_state = ACPI_STATE_S3;
111 }
112 
113 static void
114 linux_handle_power_resume_event(void *arg __unused)
115 {
116 	linux_acpi_target_sleep_state = ACPI_STATE_S0;
117 }
118 
119 static void
120 linux_handle_acpi_acad_event(void *arg, int data)
121 {
122 	struct notifier_block *nb = arg;
123 	/*
124 	 * Event type information is lost ATM in FreeBSD ACPI event handler.
125 	 * Fortunately, drm-kmod do not distinct AC event types too, so we can
126 	 * use any type e.g. ACPI_NOTIFY_BUS_CHECK that suits notifier handler.
127 	 */
128 	struct acpi_bus_event abe = {
129 	    .device_class = ACPI_AC_CLASS,
130 	    .type = ACPI_NOTIFY_BUS_CHECK,
131 	    .data = data,
132 	};
133 
134 	nb->notifier_call(nb, 0, &abe);
135 }
136 
137 static void
138 linux_handle_acpi_video_event(void *arg, int type)
139 {
140 	struct notifier_block *nb = arg;
141 	struct acpi_bus_event abe = {
142 	    .device_class = ACPI_VIDEO_CLASS,
143 	    .type = type,
144 	    .data = 0,
145 	};
146 
147 	nb->notifier_call(nb, 0, &abe);
148 }
149 
150 int
151 register_acpi_notifier(struct notifier_block *nb)
152 {
153 	nb->tags[LINUX_ACPI_ACAD] = EVENTHANDLER_REGISTER(acpi_acad_event,
154 	    linux_handle_acpi_acad_event, nb, EVENTHANDLER_PRI_FIRST);
155 	nb->tags[LINUX_ACPI_VIDEO] = EVENTHANDLER_REGISTER(acpi_video_event,
156 	    linux_handle_acpi_video_event, nb, EVENTHANDLER_PRI_FIRST);
157 
158 	return (0);
159 }
160 
161 int
162 unregister_acpi_notifier(struct notifier_block *nb)
163 {
164 	EVENTHANDLER_DEREGISTER(acpi_acad_event, nb->tags[LINUX_ACPI_ACAD]);
165 	EVENTHANDLER_DEREGISTER(acpi_video_event, nb->tags[LINUX_ACPI_VIDEO]);
166 
167 	return (0);
168 }
169 
170 uint32_t
171 acpi_target_system_state(void)
172 {
173 	return (linux_acpi_target_sleep_state);
174 }
175 
176 static void
177 linux_register_acpi_event_handlers(void *arg __unused)
178 {
179 	/*
180 	 * XXX johalun: acpi_{sleep,wakeup}_event can't be trusted, use
181 	 * power_{suspend_early,resume} 'acpiconf -s 3' or 'zzz' will not
182 	 * generate acpi_sleep_event... Lid open or wake on button generates
183 	 * acpi_wakeup_event on one of my Dell laptops but not the other
184 	 * (but it does power on)... is this a general thing?
185 	 */
186 	resume_tag = EVENTHANDLER_REGISTER(power_resume,
187 	    linux_handle_power_resume_event, NULL, EVENTHANDLER_PRI_FIRST);
188 	suspend_tag = EVENTHANDLER_REGISTER(power_suspend_early,
189 	    linux_handle_power_suspend_event, NULL, EVENTHANDLER_PRI_FIRST);
190 }
191 
192 static void
193 linux_deregister_acpi_event_handlers(void *arg __unused)
194 {
195 	EVENTHANDLER_DEREGISTER(power_resume, resume_tag);
196 	EVENTHANDLER_DEREGISTER(power_suspend_early, suspend_tag);
197 }
198 
199 SYSINIT(linux_acpi_events, SI_SUB_DRIVERS, SI_ORDER_ANY,
200     linux_register_acpi_event_handlers, NULL);
201 SYSUNINIT(linux_acpi_events, SI_SUB_DRIVERS, SI_ORDER_ANY,
202     linux_deregister_acpi_event_handlers, NULL);
203 
204 #else	/* !DEV_ACPI */
205 
206 ACPI_HANDLE
207 bsd_acpi_get_handle(device_t bsddev)
208 {
209 	return (NULL);
210 }
211 
212 bool
213 acpi_check_dsm(ACPI_HANDLE handle, const char *uuid, int rev, uint64_t funcs)
214 {
215 	return (false);
216 }
217 
218 ACPI_OBJECT *
219 acpi_evaluate_dsm_typed(ACPI_HANDLE handle, const char *uuid, int rev,
220      int func, ACPI_OBJECT *argv4, ACPI_OBJECT_TYPE type)
221 {
222 	return (NULL);
223 }
224 
225 int
226 register_acpi_notifier(struct notifier_block *nb)
227 {
228 	return (0);
229 }
230 
231 int
232 unregister_acpi_notifier(struct notifier_block *nb)
233 {
234 	return (0);
235 }
236 
237 uint32_t
238 acpi_target_system_state(void)
239 {
240 	return (ACPI_STATE_S0);
241 }
242 
243 #endif	/* !DEV_ACPI */
244