xref: /freebsd/sys/compat/linuxkpi/common/src/linux_acpi.c (revision ba6460df5607e572e9c868413c75ce9bc5ce9a00)
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 #include <linux/uuid.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 suspend_state_t pm_suspend_target_state = PM_SUSPEND_ON;
62 
63 static uint32_t linux_acpi_target_sleep_state = ACPI_STATE_S0;
64 
65 static eventhandler_tag resume_tag;
66 static eventhandler_tag suspend_tag;
67 
68 ACPI_HANDLE
bsd_acpi_get_handle(device_t bsddev)69 bsd_acpi_get_handle(device_t bsddev)
70 {
71 	return (acpi_get_handle(bsddev));
72 }
73 
74 bool
acpi_check_dsm(ACPI_HANDLE handle,const char * uuid,int rev,uint64_t funcs)75 acpi_check_dsm(ACPI_HANDLE handle, const char *uuid, int rev, uint64_t funcs)
76 {
77 
78 	if (funcs == 0)
79 		return (false);
80 
81 	/*
82 	 * From ACPI 6.3 spec 9.1.1:
83 	 * Bit 0 indicates whether there is support for any functions other
84 	 * than function 0 for the specified UUID and Revision ID. If set to
85 	 * zero, no functions are supported (other than function zero) for the
86 	 * specified UUID and Revision ID.
87 	 */
88 	funcs |= 1 << 0;
89 
90 	return ((acpi_DSMQuery(handle, uuid, rev) & funcs) == funcs);
91 }
92 
93 ACPI_OBJECT *
acpi_evaluate_dsm_typed(ACPI_HANDLE handle,const char * uuid,int rev,int func,ACPI_OBJECT * argv4,ACPI_OBJECT_TYPE type)94 acpi_evaluate_dsm_typed(ACPI_HANDLE handle, const char *uuid, int rev,
95     int func, ACPI_OBJECT *argv4, ACPI_OBJECT_TYPE type)
96 {
97 	ACPI_BUFFER buf;
98 
99 	return (ACPI_SUCCESS(acpi_EvaluateDSMTyped(handle, uuid, rev, func,
100 	    argv4, &buf, type)) ? (ACPI_OBJECT *)buf.Pointer : NULL);
101 }
102 
103 union linuxkpi_acpi_object *
acpi_evaluate_dsm(ACPI_HANDLE ObjHandle,const guid_t * guid,UINT64 rev,UINT64 func,union linuxkpi_acpi_object * pkg)104 acpi_evaluate_dsm(ACPI_HANDLE ObjHandle, const guid_t *guid,
105     UINT64 rev, UINT64 func, union linuxkpi_acpi_object *pkg)
106 {
107 	ACPI_BUFFER buf;
108 
109 	return (ACPI_SUCCESS(acpi_EvaluateDSM(ObjHandle, (const uint8_t *)guid,
110 	    rev, func, (ACPI_OBJECT *)pkg, &buf)) ?
111 	    (union linuxkpi_acpi_object *)buf.Pointer : NULL);
112 }
113 
114 static void
linux_handle_power_suspend_event(void * arg __unused)115 linux_handle_power_suspend_event(void *arg __unused)
116 {
117 	/*
118 	 * Only support S3 for now.
119 	 * acpi_sleep_event isn't always called so we use power_suspend_early
120 	 * instead which means we don't know what state we're switching to.
121 	 * TODO: Make acpi_sleep_event consistent
122 	 */
123 	linux_acpi_target_sleep_state = ACPI_STATE_S3;
124 	pm_suspend_target_state = PM_SUSPEND_MEM;
125 }
126 
127 static void
linux_handle_power_resume_event(void * arg __unused)128 linux_handle_power_resume_event(void *arg __unused)
129 {
130 	linux_acpi_target_sleep_state = ACPI_STATE_S0;
131 	pm_suspend_target_state = PM_SUSPEND_ON;
132 }
133 
134 static void
linux_handle_acpi_acad_event(void * arg,int data)135 linux_handle_acpi_acad_event(void *arg, int data)
136 {
137 	struct notifier_block *nb = arg;
138 	/*
139 	 * Event type information is lost ATM in FreeBSD ACPI event handler.
140 	 * Fortunately, drm-kmod do not distinct AC event types too, so we can
141 	 * use any type e.g. ACPI_NOTIFY_BUS_CHECK that suits notifier handler.
142 	 */
143 	struct acpi_bus_event abe = {
144 	    .device_class = ACPI_AC_CLASS,
145 	    .type = ACPI_NOTIFY_BUS_CHECK,
146 	    .data = data,
147 	};
148 
149 	nb->notifier_call(nb, 0, &abe);
150 }
151 
152 static void
linux_handle_acpi_video_event(void * arg,int type)153 linux_handle_acpi_video_event(void *arg, int type)
154 {
155 	struct notifier_block *nb = arg;
156 	struct acpi_bus_event abe = {
157 	    .device_class = ACPI_VIDEO_CLASS,
158 	    .type = type,
159 	    .data = 0,
160 	};
161 
162 	nb->notifier_call(nb, 0, &abe);
163 }
164 
165 int
register_acpi_notifier(struct notifier_block * nb)166 register_acpi_notifier(struct notifier_block *nb)
167 {
168 	nb->tags[LINUX_ACPI_ACAD] = EVENTHANDLER_REGISTER(acpi_acad_event,
169 	    linux_handle_acpi_acad_event, nb, EVENTHANDLER_PRI_FIRST);
170 	nb->tags[LINUX_ACPI_VIDEO] = EVENTHANDLER_REGISTER(acpi_video_event,
171 	    linux_handle_acpi_video_event, nb, EVENTHANDLER_PRI_FIRST);
172 
173 	return (0);
174 }
175 
176 int
unregister_acpi_notifier(struct notifier_block * nb)177 unregister_acpi_notifier(struct notifier_block *nb)
178 {
179 	EVENTHANDLER_DEREGISTER(acpi_acad_event, nb->tags[LINUX_ACPI_ACAD]);
180 	EVENTHANDLER_DEREGISTER(acpi_video_event, nb->tags[LINUX_ACPI_VIDEO]);
181 
182 	return (0);
183 }
184 
185 uint32_t
acpi_target_system_state(void)186 acpi_target_system_state(void)
187 {
188 	return (linux_acpi_target_sleep_state);
189 }
190 
191 struct acpi_dev_present_ctx {
192 	const char *hid;
193 	const char *uid;
194 	int64_t hrv;
195 	struct acpi_device *dev;
196 };
197 
198 static ACPI_STATUS
acpi_dev_present_cb(ACPI_HANDLE handle,UINT32 level,void * context,void ** result)199 acpi_dev_present_cb(ACPI_HANDLE handle, UINT32 level, void *context,
200     void **result)
201 {
202 	ACPI_DEVICE_INFO *devinfo;
203 	struct acpi_device *dev;
204 	struct acpi_dev_present_ctx *match = context;
205 	bool present = false;
206 	UINT32 sta, hrv;
207 	int i;
208 
209 	if (handle == NULL)
210 		return (AE_OK);
211 
212 	if (!ACPI_FAILURE(acpi_GetInteger(handle, "_STA", &sta)) &&
213 	    !ACPI_DEVICE_PRESENT(sta))
214 		return (AE_OK);
215 
216 	if (ACPI_FAILURE(AcpiGetObjectInfo(handle, &devinfo)))
217 		return (AE_OK);
218 
219 	if ((devinfo->Valid & ACPI_VALID_HID) != 0 &&
220 	    strcmp(match->hid, devinfo->HardwareId.String) == 0) {
221 		present = true;
222 	} else if ((devinfo->Valid & ACPI_VALID_CID) != 0) {
223 		for (i = 0; i < devinfo->CompatibleIdList.Count; i++) {
224 			if (strcmp(match->hid,
225 			    devinfo->CompatibleIdList.Ids[i].String) == 0) {
226 				present = true;
227 				break;
228 			}
229 		}
230 	}
231 	if (present && match->uid != NULL &&
232 	    ((devinfo->Valid & ACPI_VALID_UID) == 0 ||
233 	      strcmp(match->uid, devinfo->UniqueId.String) != 0))
234 		present = false;
235 
236 	AcpiOsFree(devinfo);
237 	if (!present)
238 		return (AE_OK);
239 
240 	if (match->hrv != -1) {
241 		if (ACPI_FAILURE(acpi_GetInteger(handle, "_HRV", &hrv)))
242 			return (AE_OK);
243 		if (hrv != match->hrv)
244 			return (AE_OK);
245 	}
246 
247 	dev = acpi_get_device(handle);
248 	if (dev == NULL)
249 		return (AE_OK);
250 	match->dev = dev;
251 
252 	return (AE_ERROR);
253 }
254 
255 bool
lkpi_acpi_dev_present(const char * hid,const char * uid,int64_t hrv)256 lkpi_acpi_dev_present(const char *hid, const char *uid, int64_t hrv)
257 {
258 	struct acpi_dev_present_ctx match;
259 	int rv;
260 
261 	match.hid = hid;
262 	match.uid = uid;
263 	match.hrv = hrv;
264 
265 	rv = AcpiWalkNamespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
266 	    ACPI_UINT32_MAX, acpi_dev_present_cb, NULL, &match, NULL);
267 
268 	return (rv == AE_ERROR);
269 }
270 
271 struct acpi_device *
lkpi_acpi_dev_get_first_match_dev(const char * hid,const char * uid,int64_t hrv)272 lkpi_acpi_dev_get_first_match_dev(const char *hid, const char *uid,
273     int64_t hrv)
274 {
275 	struct acpi_dev_present_ctx match;
276 	int rv;
277 
278 	match.hid = hid;
279 	match.uid = uid;
280 	match.hrv = hrv;
281 	match.dev = NULL;
282 
283 	rv = AcpiWalkNamespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
284 	    ACPI_UINT32_MAX, acpi_dev_present_cb, NULL, &match, NULL);
285 
286 	return (rv == AE_ERROR ? match.dev : NULL);
287 }
288 
289 static void
linux_register_acpi_event_handlers(void * arg __unused)290 linux_register_acpi_event_handlers(void *arg __unused)
291 {
292 	/*
293 	 * XXX johalun: acpi_{sleep,wakeup}_event can't be trusted, use
294 	 * power_{suspend_early,resume} 'acpiconf -s 3' or 'zzz' will not
295 	 * generate acpi_sleep_event... Lid open or wake on button generates
296 	 * acpi_wakeup_event on one of my Dell laptops but not the other
297 	 * (but it does power on)... is this a general thing?
298 	 */
299 	resume_tag = EVENTHANDLER_REGISTER(power_resume,
300 	    linux_handle_power_resume_event, NULL, EVENTHANDLER_PRI_FIRST);
301 	suspend_tag = EVENTHANDLER_REGISTER(power_suspend_early,
302 	    linux_handle_power_suspend_event, NULL, EVENTHANDLER_PRI_FIRST);
303 }
304 
305 static void
linux_deregister_acpi_event_handlers(void * arg __unused)306 linux_deregister_acpi_event_handlers(void *arg __unused)
307 {
308 	EVENTHANDLER_DEREGISTER(power_resume, resume_tag);
309 	EVENTHANDLER_DEREGISTER(power_suspend_early, suspend_tag);
310 }
311 
312 SYSINIT(linux_acpi_events, SI_SUB_DRIVERS, SI_ORDER_ANY,
313     linux_register_acpi_event_handlers, NULL);
314 SYSUNINIT(linux_acpi_events, SI_SUB_DRIVERS, SI_ORDER_ANY,
315     linux_deregister_acpi_event_handlers, NULL);
316 
317 #else	/* !DEV_ACPI */
318 
319 ACPI_HANDLE
bsd_acpi_get_handle(device_t bsddev)320 bsd_acpi_get_handle(device_t bsddev)
321 {
322 	return (NULL);
323 }
324 
325 bool
acpi_check_dsm(ACPI_HANDLE handle,const char * uuid,int rev,uint64_t funcs)326 acpi_check_dsm(ACPI_HANDLE handle, const char *uuid, int rev, uint64_t funcs)
327 {
328 	return (false);
329 }
330 
331 ACPI_OBJECT *
acpi_evaluate_dsm_typed(ACPI_HANDLE handle,const char * uuid,int rev,int func,ACPI_OBJECT * argv4,ACPI_OBJECT_TYPE type)332 acpi_evaluate_dsm_typed(ACPI_HANDLE handle, const char *uuid, int rev,
333      int func, ACPI_OBJECT *argv4, ACPI_OBJECT_TYPE type)
334 {
335 	return (NULL);
336 }
337 
338 union linuxkpi_acpi_object *
acpi_evaluate_dsm(ACPI_HANDLE ObjHandle,const guid_t * guid,UINT64 rev,UINT64 func,union linuxkpi_acpi_object * pkg)339 acpi_evaluate_dsm(ACPI_HANDLE ObjHandle, const guid_t *guid,
340     UINT64 rev, UINT64 func, union linuxkpi_acpi_object *pkg)
341 {
342 	return (NULL);
343 }
344 
345 int
register_acpi_notifier(struct notifier_block * nb)346 register_acpi_notifier(struct notifier_block *nb)
347 {
348 	return (0);
349 }
350 
351 int
unregister_acpi_notifier(struct notifier_block * nb)352 unregister_acpi_notifier(struct notifier_block *nb)
353 {
354 	return (0);
355 }
356 
357 uint32_t
acpi_target_system_state(void)358 acpi_target_system_state(void)
359 {
360 	return (ACPI_STATE_S0);
361 }
362 
363 bool
lkpi_acpi_dev_present(const char * hid,const char * uid,int64_t hrv)364 lkpi_acpi_dev_present(const char *hid, const char *uid, int64_t hrv)
365 {
366 	return (false);
367 }
368 
369 struct acpi_device *
lkpi_acpi_dev_get_first_match_dev(const char * hid,const char * uid,int64_t hrv)370 lkpi_acpi_dev_get_first_match_dev(const char *hid, const char *uid,
371     int64_t hrv)
372 {
373 	return (NULL);
374 }
375 
376 #endif	/* !DEV_ACPI */
377