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
bsd_acpi_get_handle(device_t bsddev)68 bsd_acpi_get_handle(device_t bsddev)
69 {
70 return (acpi_get_handle(bsddev));
71 }
72
73 bool
acpi_check_dsm(ACPI_HANDLE handle,const char * uuid,int rev,uint64_t funcs)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 *
acpi_evaluate_dsm_typed(ACPI_HANDLE handle,const char * uuid,int rev,int func,ACPI_OBJECT * argv4,ACPI_OBJECT_TYPE type)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
linux_handle_power_suspend_event(void * arg __unused)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 pm_suspend_target_state = PM_SUSPEND_MEM;
113 }
114
115 static void
linux_handle_power_resume_event(void * arg __unused)116 linux_handle_power_resume_event(void *arg __unused)
117 {
118 linux_acpi_target_sleep_state = ACPI_STATE_S0;
119 pm_suspend_target_state = PM_SUSPEND_ON;
120 }
121
122 static void
linux_handle_acpi_acad_event(void * arg,int data)123 linux_handle_acpi_acad_event(void *arg, int data)
124 {
125 struct notifier_block *nb = arg;
126 /*
127 * Event type information is lost ATM in FreeBSD ACPI event handler.
128 * Fortunately, drm-kmod do not distinct AC event types too, so we can
129 * use any type e.g. ACPI_NOTIFY_BUS_CHECK that suits notifier handler.
130 */
131 struct acpi_bus_event abe = {
132 .device_class = ACPI_AC_CLASS,
133 .type = ACPI_NOTIFY_BUS_CHECK,
134 .data = data,
135 };
136
137 nb->notifier_call(nb, 0, &abe);
138 }
139
140 static void
linux_handle_acpi_video_event(void * arg,int type)141 linux_handle_acpi_video_event(void *arg, int type)
142 {
143 struct notifier_block *nb = arg;
144 struct acpi_bus_event abe = {
145 .device_class = ACPI_VIDEO_CLASS,
146 .type = type,
147 .data = 0,
148 };
149
150 nb->notifier_call(nb, 0, &abe);
151 }
152
153 int
register_acpi_notifier(struct notifier_block * nb)154 register_acpi_notifier(struct notifier_block *nb)
155 {
156 nb->tags[LINUX_ACPI_ACAD] = EVENTHANDLER_REGISTER(acpi_acad_event,
157 linux_handle_acpi_acad_event, nb, EVENTHANDLER_PRI_FIRST);
158 nb->tags[LINUX_ACPI_VIDEO] = EVENTHANDLER_REGISTER(acpi_video_event,
159 linux_handle_acpi_video_event, nb, EVENTHANDLER_PRI_FIRST);
160
161 return (0);
162 }
163
164 int
unregister_acpi_notifier(struct notifier_block * nb)165 unregister_acpi_notifier(struct notifier_block *nb)
166 {
167 EVENTHANDLER_DEREGISTER(acpi_acad_event, nb->tags[LINUX_ACPI_ACAD]);
168 EVENTHANDLER_DEREGISTER(acpi_video_event, nb->tags[LINUX_ACPI_VIDEO]);
169
170 return (0);
171 }
172
173 uint32_t
acpi_target_system_state(void)174 acpi_target_system_state(void)
175 {
176 return (linux_acpi_target_sleep_state);
177 }
178
179 struct acpi_dev_present_ctx {
180 const char *hid;
181 const char *uid;
182 int64_t hrv;
183 struct acpi_device *dev;
184 };
185
186 static ACPI_STATUS
acpi_dev_present_cb(ACPI_HANDLE handle,UINT32 level,void * context,void ** result)187 acpi_dev_present_cb(ACPI_HANDLE handle, UINT32 level, void *context,
188 void **result)
189 {
190 ACPI_DEVICE_INFO *devinfo;
191 struct acpi_device *dev;
192 struct acpi_dev_present_ctx *match = context;
193 bool present = false;
194 UINT32 sta, hrv;
195 int i;
196
197 if (handle == NULL)
198 return (AE_OK);
199
200 if (!ACPI_FAILURE(acpi_GetInteger(handle, "_STA", &sta)) &&
201 !ACPI_DEVICE_PRESENT(sta))
202 return (AE_OK);
203
204 if (ACPI_FAILURE(AcpiGetObjectInfo(handle, &devinfo)))
205 return (AE_OK);
206
207 if ((devinfo->Valid & ACPI_VALID_HID) != 0 &&
208 strcmp(match->hid, devinfo->HardwareId.String) == 0) {
209 present = true;
210 } else if ((devinfo->Valid & ACPI_VALID_CID) != 0) {
211 for (i = 0; i < devinfo->CompatibleIdList.Count; i++) {
212 if (strcmp(match->hid,
213 devinfo->CompatibleIdList.Ids[i].String) == 0) {
214 present = true;
215 break;
216 }
217 }
218 }
219 if (present && match->uid != NULL &&
220 ((devinfo->Valid & ACPI_VALID_UID) == 0 ||
221 strcmp(match->uid, devinfo->UniqueId.String) != 0))
222 present = false;
223
224 AcpiOsFree(devinfo);
225 if (!present)
226 return (AE_OK);
227
228 if (match->hrv != -1) {
229 if (ACPI_FAILURE(acpi_GetInteger(handle, "_HRV", &hrv)))
230 return (AE_OK);
231 if (hrv != match->hrv)
232 return (AE_OK);
233 }
234
235 dev = acpi_get_device(handle);
236 if (dev == NULL)
237 return (AE_OK);
238 match->dev = dev;
239
240 return (AE_ERROR);
241 }
242
243 bool
lkpi_acpi_dev_present(const char * hid,const char * uid,int64_t hrv)244 lkpi_acpi_dev_present(const char *hid, const char *uid, int64_t hrv)
245 {
246 struct acpi_dev_present_ctx match;
247 int rv;
248
249 match.hid = hid;
250 match.uid = uid;
251 match.hrv = hrv;
252
253 rv = AcpiWalkNamespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
254 ACPI_UINT32_MAX, acpi_dev_present_cb, NULL, &match, NULL);
255
256 return (rv == AE_ERROR);
257 }
258
259 struct acpi_device *
lkpi_acpi_dev_get_first_match_dev(const char * hid,const char * uid,int64_t hrv)260 lkpi_acpi_dev_get_first_match_dev(const char *hid, const char *uid,
261 int64_t hrv)
262 {
263 struct acpi_dev_present_ctx match;
264 int rv;
265
266 match.hid = hid;
267 match.uid = uid;
268 match.hrv = hrv;
269 match.dev = NULL;
270
271 rv = AcpiWalkNamespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
272 ACPI_UINT32_MAX, acpi_dev_present_cb, NULL, &match, NULL);
273
274 return (rv == AE_ERROR ? match.dev : NULL);
275 }
276
277 static void
linux_register_acpi_event_handlers(void * arg __unused)278 linux_register_acpi_event_handlers(void *arg __unused)
279 {
280 /*
281 * XXX johalun: acpi_{sleep,wakeup}_event can't be trusted, use
282 * power_{suspend_early,resume} 'acpiconf -s 3' or 'zzz' will not
283 * generate acpi_sleep_event... Lid open or wake on button generates
284 * acpi_wakeup_event on one of my Dell laptops but not the other
285 * (but it does power on)... is this a general thing?
286 */
287 resume_tag = EVENTHANDLER_REGISTER(power_resume,
288 linux_handle_power_resume_event, NULL, EVENTHANDLER_PRI_FIRST);
289 suspend_tag = EVENTHANDLER_REGISTER(power_suspend_early,
290 linux_handle_power_suspend_event, NULL, EVENTHANDLER_PRI_FIRST);
291 }
292
293 static void
linux_deregister_acpi_event_handlers(void * arg __unused)294 linux_deregister_acpi_event_handlers(void *arg __unused)
295 {
296 EVENTHANDLER_DEREGISTER(power_resume, resume_tag);
297 EVENTHANDLER_DEREGISTER(power_suspend_early, suspend_tag);
298 }
299
300 SYSINIT(linux_acpi_events, SI_SUB_DRIVERS, SI_ORDER_ANY,
301 linux_register_acpi_event_handlers, NULL);
302 SYSUNINIT(linux_acpi_events, SI_SUB_DRIVERS, SI_ORDER_ANY,
303 linux_deregister_acpi_event_handlers, NULL);
304
305 #else /* !DEV_ACPI */
306
307 ACPI_HANDLE
bsd_acpi_get_handle(device_t bsddev)308 bsd_acpi_get_handle(device_t bsddev)
309 {
310 return (NULL);
311 }
312
313 bool
acpi_check_dsm(ACPI_HANDLE handle,const char * uuid,int rev,uint64_t funcs)314 acpi_check_dsm(ACPI_HANDLE handle, const char *uuid, int rev, uint64_t funcs)
315 {
316 return (false);
317 }
318
319 ACPI_OBJECT *
acpi_evaluate_dsm_typed(ACPI_HANDLE handle,const char * uuid,int rev,int func,ACPI_OBJECT * argv4,ACPI_OBJECT_TYPE type)320 acpi_evaluate_dsm_typed(ACPI_HANDLE handle, const char *uuid, int rev,
321 int func, ACPI_OBJECT *argv4, ACPI_OBJECT_TYPE type)
322 {
323 return (NULL);
324 }
325
326 int
register_acpi_notifier(struct notifier_block * nb)327 register_acpi_notifier(struct notifier_block *nb)
328 {
329 return (0);
330 }
331
332 int
unregister_acpi_notifier(struct notifier_block * nb)333 unregister_acpi_notifier(struct notifier_block *nb)
334 {
335 return (0);
336 }
337
338 uint32_t
acpi_target_system_state(void)339 acpi_target_system_state(void)
340 {
341 return (ACPI_STATE_S0);
342 }
343
344 bool
lkpi_acpi_dev_present(const char * hid,const char * uid,int64_t hrv)345 lkpi_acpi_dev_present(const char *hid, const char *uid, int64_t hrv)
346 {
347 return (false);
348 }
349
350 struct acpi_device *
lkpi_acpi_dev_get_first_match_dev(const char * hid,const char * uid,int64_t hrv)351 lkpi_acpi_dev_get_first_match_dev(const char *hid, const char *uid,
352 int64_t hrv)
353 {
354 return (NULL);
355 }
356
357 #endif /* !DEV_ACPI */
358