1 /*- 2 * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@jp.freebsd.org> 3 * Copyright (c) 2000 Michael Smith <msmith@freebsd.org> 4 * Copyright (c) 2000 BSDi 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are 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 the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 #include "acpi_if.h" 32 #include "bus_if.h" 33 #include <sys/eventhandler.h> 34 #include <sys/sysctl.h> 35 #if __FreeBSD_version >= 500000 36 #include <sys/lock.h> 37 #include <sys/mutex.h> 38 #endif 39 40 #include <machine/bus.h> 41 #include <machine/resource.h> 42 43 #if __FreeBSD_version < 500000 44 typedef vm_offset_t vm_paddr_t; 45 #endif 46 47 struct acpi_softc { 48 device_t acpi_dev; 49 struct cdev *acpi_dev_t; 50 51 struct resource *acpi_irq; 52 int acpi_irq_rid; 53 void *acpi_irq_handle; 54 55 int acpi_enabled; 56 int acpi_sstate; 57 int acpi_sleep_disabled; 58 59 struct sysctl_ctx_list acpi_sysctl_ctx; 60 struct sysctl_oid *acpi_sysctl_tree; 61 int acpi_power_button_sx; 62 int acpi_sleep_button_sx; 63 int acpi_lid_switch_sx; 64 65 int acpi_standby_sx; 66 int acpi_suspend_sx; 67 68 int acpi_sleep_delay; 69 int acpi_s4bios; 70 int acpi_disable_on_poweroff; 71 int acpi_verbose; 72 73 bus_dma_tag_t acpi_waketag; 74 bus_dmamap_t acpi_wakemap; 75 vm_offset_t acpi_wakeaddr; 76 vm_paddr_t acpi_wakephys; 77 78 struct sysctl_ctx_list acpi_battery_sysctl_ctx; 79 struct sysctl_oid *acpi_battery_sysctl_tree; 80 }; 81 82 struct acpi_device { 83 /* ACPI ivars */ 84 ACPI_HANDLE ad_handle; 85 int ad_magic; 86 void *ad_private; 87 int ad_flags; 88 89 /* Resources */ 90 struct resource_list ad_rl; 91 }; 92 93 #define ACPI_PRW_MAX_POWERRES 8 94 95 struct acpi_prw_data { 96 ACPI_HANDLE gpe_handle; 97 int gpe_bit; 98 int lowest_wake; 99 ACPI_OBJECT power_res[ACPI_PRW_MAX_POWERRES]; 100 int power_res_count; 101 }; 102 103 /* Flags for each device defined in the AML namespace. */ 104 #define ACPI_FLAG_WAKE_ENABLED 0x1 105 106 #if __FreeBSD_version < 500000 107 /* 108 * In 4.x, ACPI is protected by splhigh(). 109 */ 110 # define ACPI_LOCK s = splhigh() 111 # define ACPI_UNLOCK splx(s) 112 # define ACPI_ASSERTLOCK 113 # define ACPI_MSLEEP(a, b, c, d, e) tsleep(a, c, d, e) 114 # define ACPI_LOCK_DECL int s 115 # define kthread_create(a, b, c, d, e, f) kthread_create(a, b, c, f) 116 # define tc_init(a) init_timecounter(a) 117 #else 118 # define ACPI_LOCK 119 # define ACPI_UNLOCK 120 # define ACPI_ASSERTLOCK 121 # define ACPI_LOCK_DECL 122 #endif 123 124 /* 125 * ACPI CA does not define layers for non-ACPI CA drivers. 126 * We define some here within the range provided. 127 */ 128 #define ACPI_AC_ADAPTER 0x00010000 129 #define ACPI_BATTERY 0x00020000 130 #define ACPI_BUS 0x00040000 131 #define ACPI_BUTTON 0x00080000 132 #define ACPI_EC 0x00100000 133 #define ACPI_FAN 0x00200000 134 #define ACPI_POWERRES 0x00400000 135 #define ACPI_PROCESSOR 0x00800000 136 #define ACPI_THERMAL 0x01000000 137 #define ACPI_TIMER 0x02000000 138 #define ACPI_ASUS 0x04000000 139 140 /* 141 * Constants for different interrupt models used with acpi_SetIntrModel(). 142 */ 143 #define ACPI_INTR_PIC 0 144 #define ACPI_INTR_APIC 1 145 #define ACPI_INTR_SAPIC 2 146 147 /* Quirk flags. */ 148 #define ACPI_Q_OK 0 149 #define ACPI_Q_BROKEN (1 << 0) /* Disable ACPI completely. */ 150 151 /* 152 * Note that the low ivar values are reserved to provide 153 * interface compatibility with ISA drivers which can also 154 * attach to ACPI. 155 */ 156 #define ACPI_IVAR_HANDLE 0x100 157 #define ACPI_IVAR_MAGIC 0x101 158 #define ACPI_IVAR_PRIVATE 0x102 159 #define ACPI_IVAR_FLAGS 0x103 160 161 /* 162 * Accessor functions for our ivars. Default value for BUS_READ_IVAR is 163 * (type) 0. The <sys/bus.h> accessor functions don't check return values. 164 */ 165 #define __ACPI_BUS_ACCESSOR(varp, var, ivarp, ivar, type) \ 166 \ 167 static __inline type varp ## _get_ ## var(device_t dev) \ 168 { \ 169 uintptr_t v = 0; \ 170 BUS_READ_IVAR(device_get_parent(dev), dev, \ 171 ivarp ## _IVAR_ ## ivar, &v); \ 172 return ((type) v); \ 173 } \ 174 \ 175 static __inline void varp ## _set_ ## var(device_t dev, type t) \ 176 { \ 177 uintptr_t v = (uintptr_t) t; \ 178 BUS_WRITE_IVAR(device_get_parent(dev), dev, \ 179 ivarp ## _IVAR_ ## ivar, v); \ 180 } 181 182 __ACPI_BUS_ACCESSOR(acpi, handle, ACPI, HANDLE, ACPI_HANDLE) 183 __ACPI_BUS_ACCESSOR(acpi, magic, ACPI, MAGIC, int) 184 __ACPI_BUS_ACCESSOR(acpi, private, ACPI, PRIVATE, void *) 185 __ACPI_BUS_ACCESSOR(acpi, flags, ACPI, FLAGS, int) 186 187 void acpi_fake_objhandler(ACPI_HANDLE h, UINT32 fn, void *data); 188 static __inline device_t 189 acpi_get_device(ACPI_HANDLE handle) 190 { 191 void *dev = NULL; 192 AcpiGetData(handle, acpi_fake_objhandler, &dev); 193 return ((device_t)dev); 194 } 195 196 static __inline ACPI_OBJECT_TYPE 197 acpi_get_type(device_t dev) 198 { 199 ACPI_HANDLE h; 200 ACPI_OBJECT_TYPE t; 201 202 if ((h = acpi_get_handle(dev)) == NULL) 203 return (ACPI_TYPE_NOT_FOUND); 204 if (AcpiGetType(h, &t) != AE_OK) 205 return (ACPI_TYPE_NOT_FOUND); 206 return (t); 207 } 208 209 #ifdef ACPI_DEBUGGER 210 void acpi_EnterDebugger(void); 211 #endif 212 213 #ifdef ACPI_DEBUG 214 #include <sys/cons.h> 215 #define STEP(x) do {printf x, printf("\n"); cngetc();} while (0) 216 #else 217 #define STEP(x) 218 #endif 219 220 #define ACPI_VPRINT(dev, acpi_sc, x...) do { \ 221 if (acpi_get_verbose(acpi_sc)) \ 222 device_printf(dev, x); \ 223 } while (0) 224 225 #define ACPI_DEVINFO_PRESENT(x) (((x) & 0x9) == 9) 226 BOOLEAN acpi_DeviceIsPresent(device_t dev); 227 BOOLEAN acpi_BatteryIsPresent(device_t dev); 228 ACPI_STATUS acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, 229 ACPI_HANDLE *result); 230 uint32_t acpi_TimerDelta(uint32_t end, uint32_t start); 231 ACPI_BUFFER *acpi_AllocBuffer(int size); 232 ACPI_STATUS acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, 233 UINT32 *number); 234 ACPI_STATUS acpi_GetInteger(ACPI_HANDLE handle, char *path, 235 UINT32 *number); 236 ACPI_STATUS acpi_SetInteger(ACPI_HANDLE handle, char *path, 237 UINT32 number); 238 ACPI_STATUS acpi_ForeachPackageObject(ACPI_OBJECT *obj, 239 void (*func)(ACPI_OBJECT *comp, void *arg), void *arg); 240 ACPI_STATUS acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, 241 ACPI_RESOURCE **resp); 242 ACPI_STATUS acpi_AppendBufferResource(ACPI_BUFFER *buf, 243 ACPI_RESOURCE *res); 244 ACPI_STATUS acpi_OverrideInterruptLevel(UINT32 InterruptNumber); 245 ACPI_STATUS acpi_SetIntrModel(int model); 246 ACPI_STATUS acpi_SetSleepState(struct acpi_softc *sc, int state); 247 int acpi_wake_init(device_t dev, int type); 248 int acpi_wake_set_enable(device_t dev, int enable); 249 int acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw); 250 ACPI_STATUS acpi_Startup(void); 251 ACPI_STATUS acpi_Enable(struct acpi_softc *sc); 252 ACPI_STATUS acpi_Disable(struct acpi_softc *sc); 253 void acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, 254 uint8_t notify); 255 struct resource *acpi_bus_alloc_gas(device_t dev, int *rid, 256 ACPI_GENERIC_ADDRESS *gas); 257 258 struct acpi_parse_resource_set { 259 void (*set_init)(device_t dev, void *arg, void **context); 260 void (*set_done)(device_t dev, void *context); 261 void (*set_ioport)(device_t dev, void *context, uint32_t base, 262 uint32_t length); 263 void (*set_iorange)(device_t dev, void *context, uint32_t low, 264 uint32_t high, uint32_t length, uint32_t align); 265 void (*set_memory)(device_t dev, void *context, uint32_t base, 266 uint32_t length); 267 void (*set_memoryrange)(device_t dev, void *context, uint32_t low, 268 uint32_t high, uint32_t length, uint32_t align); 269 void (*set_irq)(device_t dev, void *context, u_int32_t *irq, 270 int count, int trig, int pol); 271 void (*set_drq)(device_t dev, void *context, u_int32_t *drq, 272 int count); 273 void (*set_start_dependant)(device_t dev, void *context, 274 int preference); 275 void (*set_end_dependant)(device_t dev, void *context); 276 }; 277 278 extern struct acpi_parse_resource_set acpi_res_parse_set; 279 280 void acpi_config_intr(device_t dev, ACPI_RESOURCE *res); 281 ACPI_STATUS acpi_lookup_irq_resource(device_t dev, int rid, 282 struct resource *res, ACPI_RESOURCE *acpi_res); 283 ACPI_STATUS acpi_parse_resources(device_t dev, ACPI_HANDLE handle, 284 struct acpi_parse_resource_set *set, void *arg); 285 extern struct rman acpi_rman_io, acpi_rman_mem; 286 struct resource_list_entry *acpi_sysres_find(int type, u_long addr); 287 288 /* ACPI event handling */ 289 UINT32 acpi_event_power_button_sleep(void *context); 290 UINT32 acpi_event_power_button_wake(void *context); 291 UINT32 acpi_event_sleep_button_sleep(void *context); 292 UINT32 acpi_event_sleep_button_wake(void *context); 293 294 #define ACPI_EVENT_PRI_FIRST 0 295 #define ACPI_EVENT_PRI_DEFAULT 10000 296 #define ACPI_EVENT_PRI_LAST 20000 297 298 typedef void (*acpi_event_handler_t)(void *, int); 299 300 EVENTHANDLER_DECLARE(acpi_sleep_event, acpi_event_handler_t); 301 EVENTHANDLER_DECLARE(acpi_wakeup_event, acpi_event_handler_t); 302 303 /* Device power control. */ 304 ACPI_STATUS acpi_pwr_wake_enable(ACPI_HANDLE consumer, int enable); 305 ACPI_STATUS acpi_pwr_switch_consumer(ACPI_HANDLE consumer, int state); 306 307 /* Misc. */ 308 static __inline struct acpi_softc * 309 acpi_device_get_parent_softc(device_t child) 310 { 311 device_t parent; 312 313 parent = device_get_parent(child); 314 if (parent == NULL) 315 return (NULL); 316 return (device_get_softc(parent)); 317 } 318 319 static __inline int 320 acpi_get_verbose(struct acpi_softc *sc) 321 { 322 if (sc) 323 return (sc->acpi_verbose); 324 return (0); 325 } 326 327 char *acpi_name(ACPI_HANDLE handle); 328 int acpi_avoid(ACPI_HANDLE handle); 329 int acpi_disabled(char *subsys); 330 int acpi_machdep_init(device_t dev); 331 void acpi_install_wakeup_handler(struct acpi_softc *sc); 332 int acpi_sleep_machdep(struct acpi_softc *sc, int state); 333 int acpi_table_quirks(int *quirks); 334 int acpi_machdep_quirks(int *quirks); 335 336 /* Battery Abstraction. */ 337 struct acpi_battinfo; 338 struct acpi_battdesc; 339 340 int acpi_battery_register(int, int); 341 int acpi_battery_remove(int, int); 342 int acpi_battery_get_battinfo(int, struct acpi_battinfo *); 343 int acpi_battery_get_units(void); 344 int acpi_battery_get_info_expire(void); 345 int acpi_battery_get_battdesc(int, struct acpi_battdesc *); 346 347 int acpi_cmbat_get_battinfo(int, struct acpi_battinfo *); 348 349 /* Embedded controller. */ 350 void acpi_ec_ecdt_probe(device_t); 351 352 /* AC adapter interface. */ 353 int acpi_acad_get_acline(int *); 354 355 /* Package manipulation convenience functions. */ 356 #define ACPI_PKG_VALID(pkg, size) \ 357 ((pkg) != NULL && (pkg)->Type == ACPI_TYPE_PACKAGE && \ 358 (pkg)->Package.Count >= (size)) 359 int acpi_PkgInt(ACPI_OBJECT *res, int idx, ACPI_INTEGER *dst); 360 int acpi_PkgInt32(ACPI_OBJECT *res, int idx, uint32_t *dst); 361 int acpi_PkgStr(ACPI_OBJECT *res, int idx, void *dst, size_t size); 362 int acpi_PkgGas(device_t dev, ACPI_OBJECT *res, int idx, int *rid, 363 struct resource **dst); 364 ACPI_HANDLE acpi_GetReference(ACPI_HANDLE scope, ACPI_OBJECT *obj); 365 366 #if __FreeBSD_version >= 500000 367 #ifndef ACPI_MAX_THREADS 368 #define ACPI_MAX_THREADS 3 369 #endif 370 #if ACPI_MAX_THREADS > 0 371 #define ACPI_USE_THREADS 372 #endif 373 #endif 374 375 #ifdef ACPI_USE_THREADS 376 /* ACPI task kernel thread initialization. */ 377 int acpi_task_thread_init(void); 378 #endif 379