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 struct acpi_dev_present_ctx { 178 const char *hid; 179 const char *uid; 180 int64_t hrv; 181 }; 182 183 static ACPI_STATUS 184 acpi_dev_present_cb(ACPI_HANDLE handle, UINT32 level, void *context, 185 void **result) 186 { 187 ACPI_DEVICE_INFO *devinfo; 188 struct acpi_dev_present_ctx *match = context; 189 bool present = false; 190 UINT32 sta, hrv; 191 int i; 192 193 if (handle == NULL) 194 return (AE_OK); 195 196 if (!ACPI_FAILURE(acpi_GetInteger(handle, "_STA", &sta)) && 197 !ACPI_DEVICE_PRESENT(sta)) 198 return (AE_OK); 199 200 if (ACPI_FAILURE(AcpiGetObjectInfo(handle, &devinfo))) 201 return (AE_OK); 202 203 if ((devinfo->Valid & ACPI_VALID_HID) != 0 && 204 strcmp(match->hid, devinfo->HardwareId.String) == 0) { 205 present = true; 206 } else if ((devinfo->Valid & ACPI_VALID_CID) != 0) { 207 for (i = 0; i < devinfo->CompatibleIdList.Count; i++) { 208 if (strcmp(match->hid, 209 devinfo->CompatibleIdList.Ids[i].String) == 0) { 210 present = true; 211 break; 212 } 213 } 214 } 215 if (present && match->uid != NULL && 216 ((devinfo->Valid & ACPI_VALID_UID) == 0 || 217 strcmp(match->uid, devinfo->UniqueId.String) != 0)) 218 present = false; 219 220 AcpiOsFree(devinfo); 221 if (!present) 222 return (AE_OK); 223 224 if (match->hrv != -1) { 225 if (ACPI_FAILURE(acpi_GetInteger(handle, "_HRV", &hrv))) 226 return (AE_OK); 227 if (hrv != match->hrv) 228 return (AE_OK); 229 } 230 231 return (AE_ERROR); 232 } 233 234 bool 235 lkpi_acpi_dev_present(const char *hid, const char *uid, int64_t hrv) 236 { 237 struct acpi_dev_present_ctx match; 238 int rv; 239 240 match.hid = hid; 241 match.uid = uid; 242 match.hrv = hrv; 243 244 rv = AcpiWalkNamespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, 245 ACPI_UINT32_MAX, acpi_dev_present_cb, NULL, &match, NULL); 246 247 return (rv == AE_ERROR); 248 } 249 250 static void 251 linux_register_acpi_event_handlers(void *arg __unused) 252 { 253 /* 254 * XXX johalun: acpi_{sleep,wakeup}_event can't be trusted, use 255 * power_{suspend_early,resume} 'acpiconf -s 3' or 'zzz' will not 256 * generate acpi_sleep_event... Lid open or wake on button generates 257 * acpi_wakeup_event on one of my Dell laptops but not the other 258 * (but it does power on)... is this a general thing? 259 */ 260 resume_tag = EVENTHANDLER_REGISTER(power_resume, 261 linux_handle_power_resume_event, NULL, EVENTHANDLER_PRI_FIRST); 262 suspend_tag = EVENTHANDLER_REGISTER(power_suspend_early, 263 linux_handle_power_suspend_event, NULL, EVENTHANDLER_PRI_FIRST); 264 } 265 266 static void 267 linux_deregister_acpi_event_handlers(void *arg __unused) 268 { 269 EVENTHANDLER_DEREGISTER(power_resume, resume_tag); 270 EVENTHANDLER_DEREGISTER(power_suspend_early, suspend_tag); 271 } 272 273 SYSINIT(linux_acpi_events, SI_SUB_DRIVERS, SI_ORDER_ANY, 274 linux_register_acpi_event_handlers, NULL); 275 SYSUNINIT(linux_acpi_events, SI_SUB_DRIVERS, SI_ORDER_ANY, 276 linux_deregister_acpi_event_handlers, NULL); 277 278 #else /* !DEV_ACPI */ 279 280 ACPI_HANDLE 281 bsd_acpi_get_handle(device_t bsddev) 282 { 283 return (NULL); 284 } 285 286 bool 287 acpi_check_dsm(ACPI_HANDLE handle, const char *uuid, int rev, uint64_t funcs) 288 { 289 return (false); 290 } 291 292 ACPI_OBJECT * 293 acpi_evaluate_dsm_typed(ACPI_HANDLE handle, const char *uuid, int rev, 294 int func, ACPI_OBJECT *argv4, ACPI_OBJECT_TYPE type) 295 { 296 return (NULL); 297 } 298 299 int 300 register_acpi_notifier(struct notifier_block *nb) 301 { 302 return (0); 303 } 304 305 int 306 unregister_acpi_notifier(struct notifier_block *nb) 307 { 308 return (0); 309 } 310 311 uint32_t 312 acpi_target_system_state(void) 313 { 314 return (ACPI_STATE_S0); 315 } 316 317 bool 318 lkpi_acpi_dev_present(const char *hid, const char *uid, int64_t hrv) 319 { 320 return (false); 321 } 322 323 #endif /* !DEV_ACPI */ 324