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: src/sys/dev/acpica/acpivar.h,v 1.43.2.1 2003/08/22 20:49:20 jhb Exp $ 29 * $DragonFly: src/sys/dev/acpica/Attic/acpivar.h,v 1.1 2003/09/24 03:32:16 drhodus Exp $ 30 */ 31 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 struct acpi_softc { 44 device_t acpi_dev; 45 dev_t acpi_dev_t; 46 47 struct resource *acpi_irq; 48 int acpi_irq_rid; 49 void *acpi_irq_handle; 50 51 int acpi_enabled; 52 int acpi_sstate; 53 int acpi_sleep_disabled; 54 55 struct sysctl_ctx_list acpi_sysctl_ctx; 56 struct sysctl_oid *acpi_sysctl_tree; 57 #define ACPI_POWER_BUTTON_DEFAULT_SX ACPI_STATE_S5; 58 #define ACPI_SLEEP_BUTTON_DEFAULT_SX ACPI_STATE_S1; 59 #define ACPI_LID_SWITCH_DEFAULT_SX ACPI_STATE_S1; 60 int acpi_power_button_sx; 61 int acpi_sleep_button_sx; 62 int acpi_lid_switch_sx; 63 64 int acpi_standby_sx; 65 int acpi_suspend_sx; 66 67 int acpi_sleep_delay; 68 int acpi_s4bios; 69 int acpi_disable_on_poweroff; 70 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 88 /* resources */ 89 struct resource_list ad_rl; 90 91 }; 92 93 #if __FreeBSD_version < 500000 94 /* 95 * In 4.x, ACPI is protected by splhigh(). 96 */ 97 # define ACPI_LOCK s = splhigh() 98 # define ACPI_UNLOCK splx(s) 99 # define ACPI_ASSERTLOCK 100 # define ACPI_MSLEEP(a, b, c, d, e) tsleep(a, c, d, e) 101 # define ACPI_LOCK_DECL int s 102 # define kthread_create(a, b, c, d, e, f) kthread_create(a, b, c, f) 103 # define tc_init(a) init_timecounter(a) 104 #elif 0 105 /* 106 * The ACPI subsystem lives under a single mutex. You *must* 107 * acquire this mutex before calling any of the acpi_ or Acpi* functions. 108 */ 109 extern struct mtx acpi_mutex; 110 # define ACPI_LOCK mtx_lock(&acpi_mutex) 111 # define ACPI_UNLOCK mtx_unlock(&acpi_mutex) 112 # define ACPI_ASSERTLOCK mtx_assert(&acpi_mutex, MA_OWNED) 113 # define ACPI_MSLEEP(a, b, c, d, e) msleep(a, b, c, d, e) 114 # define ACPI_LOCK_DECL 115 #else 116 # define ACPI_LOCK 117 # define ACPI_UNLOCK 118 # define ACPI_ASSERTLOCK 119 # define ACPI_MSLEEP(a, b, c, d, e) tsleep(a, c, d, e) 120 # define ACPI_LOCK_DECL 121 #endif 122 123 /* 124 * ACPI CA does not define layers for non-ACPI CA drivers. 125 * We define some here within the range provided. 126 */ 127 #define ACPI_BUS 0x00010000 128 #define ACPI_SYSTEM 0x00020000 129 #define ACPI_POWER 0x00040000 130 #define ACPI_EC 0x00080000 131 #define ACPI_AC_ADAPTER 0x00100000 132 #define ACPI_BATTERY 0x00110000 133 #define ACPI_BUTTON 0x00120000 134 #define ACPI_PROCESSOR 0x00140000 135 #define ACPI_THERMAL 0x00180000 136 #define ACPI_FAN 0x00200000 137 138 /* 139 * Constants for different interrupt models used with acpi_SetIntrModel(). 140 */ 141 #define ACPI_INTR_PIC 0 142 #define ACPI_INTR_APIC 1 143 #define ACPI_INTR_SAPIC 2 144 145 /* XXX this is no longer referenced anywhere, remove? */ 146 #if 0 147 /* 148 * This is a cheap and nasty way to get around the horrid counted list 149 * argument format that AcpiEvalateObject uses. 150 */ 151 #define ACPI_OBJECTLIST_MAX 16 152 struct acpi_object_list { 153 UINT32 count; 154 ACPI_OBJECT *pointer[ACPI_OBJECTLIST_MAX]; 155 ACPI_OBJECT object[ACPI_OBJECTLIST_MAX]; 156 }; 157 158 static __inline struct acpi_object_list * 159 acpi_AllocObjectList(int nobj) 160 { 161 struct acpi_object_list *l; 162 int i; 163 164 if (nobj > ACPI_OBJECTLIST_MAX) 165 return(NULL); 166 if ((l = AcpiOsAllocate(sizeof(*l))) == NULL) 167 return(NULL); 168 bzero(l, sizeof(*l)); 169 for (i = 0; i < ACPI_OBJECTLIST_MAX; i++) 170 l->pointer[i] = &l->object[i]; 171 l->count = nobj; 172 return(l); 173 } 174 #endif /* unused */ 175 176 /* 177 * Note that the low ivar values are reserved to provide 178 * interface compatibility with ISA drivers which can also 179 * attach to ACPI. 180 */ 181 #define ACPI_IVAR_HANDLE 0x100 182 #define ACPI_IVAR_MAGIC 0x101 183 #define ACPI_IVAR_PRIVATE 0x102 184 185 static __inline ACPI_HANDLE 186 acpi_get_handle(device_t dev) 187 { 188 uintptr_t up; 189 ACPI_HANDLE h; 190 191 if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_HANDLE, &up)) 192 return(NULL); 193 h = (ACPI_HANDLE)up; 194 return(h); 195 } 196 197 static __inline int 198 acpi_set_handle(device_t dev, ACPI_HANDLE h) 199 { 200 uintptr_t up; 201 202 up = (uintptr_t)h; 203 return(BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_HANDLE, up)); 204 } 205 206 static __inline int 207 acpi_get_magic(device_t dev) 208 { 209 uintptr_t up; 210 int m; 211 212 if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_MAGIC, &up)) 213 return(0); 214 m = (int)up; 215 return(m); 216 } 217 218 static __inline int 219 acpi_set_magic(device_t dev, int m) 220 { 221 uintptr_t up; 222 223 up = (uintptr_t)m; 224 return(BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_MAGIC, up)); 225 } 226 227 static __inline void * 228 acpi_get_private(device_t dev) 229 { 230 uintptr_t up; 231 void *p; 232 233 if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_PRIVATE, &up)) 234 return(NULL); 235 p = (void *)up; 236 return(p); 237 } 238 239 static __inline int 240 acpi_set_private(device_t dev, void *p) 241 { 242 uintptr_t up; 243 244 up = (uintptr_t)p; 245 return(BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_PRIVATE, up)); 246 } 247 248 static __inline ACPI_OBJECT_TYPE 249 acpi_get_type(device_t dev) 250 { 251 ACPI_HANDLE h; 252 ACPI_OBJECT_TYPE t; 253 254 if ((h = acpi_get_handle(dev)) == NULL) 255 return(ACPI_TYPE_NOT_FOUND); 256 if (AcpiGetType(h, &t) != AE_OK) 257 return(ACPI_TYPE_NOT_FOUND); 258 return(t); 259 } 260 261 #ifdef ACPI_DEBUGGER 262 extern void acpi_EnterDebugger(void); 263 #endif 264 265 #ifdef ACPI_DEBUG 266 #include <sys/cons.h> 267 #define STEP(x) do {printf x, printf("\n"); cngetc();} while (0) 268 #else 269 #define STEP(x) 270 #endif 271 272 #define ACPI_VPRINT(dev, acpi_sc, x...) do { \ 273 if (acpi_get_verbose(acpi_sc)) \ 274 device_printf(dev, x); \ 275 } while (0) 276 277 #define ACPI_DEVINFO_PRESENT(x) (((x) & 0x9) == 9) 278 extern BOOLEAN acpi_DeviceIsPresent(device_t dev); 279 extern BOOLEAN acpi_BatteryIsPresent(device_t dev); 280 extern BOOLEAN acpi_MatchHid(device_t dev, char *hid); 281 extern ACPI_STATUS acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result); 282 extern ACPI_BUFFER *acpi_AllocBuffer(int size); 283 extern ACPI_STATUS acpi_EvaluateInteger(ACPI_HANDLE handle, char *path, int *number); 284 extern ACPI_STATUS acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, int *number); 285 extern ACPI_STATUS acpi_ForeachPackageObject(ACPI_OBJECT *obj, 286 void (* func)(ACPI_OBJECT *comp, void *arg), 287 void *arg); 288 extern ACPI_STATUS acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp); 289 extern ACPI_STATUS acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res); 290 extern ACPI_STATUS acpi_SetIntrModel(int model); 291 292 extern ACPI_STATUS acpi_SetSleepState(struct acpi_softc *sc, int state); 293 extern ACPI_STATUS acpi_Enable(struct acpi_softc *sc); 294 extern ACPI_STATUS acpi_Disable(struct acpi_softc *sc); 295 296 struct acpi_parse_resource_set { 297 void (* set_init)(device_t dev, void **context); 298 void (* set_done)(device_t dev, void *context); 299 void (* set_ioport)(device_t dev, void *context, u_int32_t base, u_int32_t length); 300 void (* set_iorange)(device_t dev, void *context, u_int32_t low, u_int32_t high, 301 u_int32_t length, u_int32_t align); 302 void (* set_memory)(device_t dev, void *context, u_int32_t base, u_int32_t length); 303 void (* set_memoryrange)(device_t dev, void *context, u_int32_t low, u_int32_t high, 304 u_int32_t length, u_int32_t align); 305 void (* set_irq)(device_t dev, void *context, u_int32_t *irq, int cout); 306 void (* set_drq)(device_t dev, void *context, u_int32_t *drq, int count); 307 void (* set_start_dependant)(device_t dev, void *context, int preference); 308 void (* set_end_dependant)(device_t dev, void *context); 309 }; 310 311 extern struct acpi_parse_resource_set acpi_res_parse_set; 312 extern ACPI_STATUS acpi_parse_resources(device_t dev, ACPI_HANDLE handle, 313 struct acpi_parse_resource_set *set); 314 /* XXX until Intel fix this in their headers, based on NEXT_RESOURCE */ 315 #define ACPI_RESOURCE_NEXT(Res) (ACPI_RESOURCE *)((UINT8 *) Res + Res->Length) 316 317 /* 318 * ACPI event handling 319 */ 320 extern UINT32 acpi_eventhandler_power_button_for_sleep(void *context); 321 extern UINT32 acpi_eventhandler_power_button_for_wakeup(void *context); 322 extern UINT32 acpi_eventhandler_sleep_button_for_sleep(void *context); 323 extern UINT32 acpi_eventhandler_sleep_button_for_wakeup(void *context); 324 325 #define ACPI_EVENT_PRI_FIRST 0 326 #define ACPI_EVENT_PRI_DEFAULT 10000 327 #define ACPI_EVENT_PRI_LAST 20000 328 329 typedef void (*acpi_event_handler_t)(void *, int); 330 331 EVENTHANDLER_DECLARE(acpi_sleep_event, acpi_event_handler_t); 332 EVENTHANDLER_DECLARE(acpi_wakeup_event, acpi_event_handler_t); 333 334 /* 335 * Device power control. 336 */ 337 extern ACPI_STATUS acpi_pwr_switch_consumer(ACPI_HANDLE consumer, int state); 338 339 /* 340 * Misc. 341 */ 342 static __inline struct acpi_softc * 343 acpi_device_get_parent_softc(device_t child) 344 { 345 device_t parent; 346 347 parent = device_get_parent(child); 348 if (parent == NULL) { 349 return(NULL); 350 } 351 return(device_get_softc(parent)); 352 } 353 354 static __inline int 355 acpi_get_verbose(struct acpi_softc *sc) 356 { 357 if (sc) 358 return(sc->acpi_verbose); 359 return(0); 360 } 361 362 extern char *acpi_name(ACPI_HANDLE handle); 363 extern int acpi_avoid(ACPI_HANDLE handle); 364 extern int acpi_disabled(char *subsys); 365 366 extern void acpi_device_enable_wake_capability(ACPI_HANDLE h, int enable); 367 extern void acpi_device_enable_wake_event(ACPI_HANDLE h); 368 369 extern int acpi_machdep_init(device_t dev); 370 extern void acpi_install_wakeup_handler(struct acpi_softc *sc); 371 extern int acpi_sleep_machdep(struct acpi_softc *sc, int state); 372 373 /* 374 * Battery Abstraction. 375 */ 376 struct acpi_battinfo; 377 struct acpi_battdesc; 378 379 extern int acpi_battery_register(int, int); 380 extern int acpi_battery_get_battinfo(int, struct acpi_battinfo *); 381 extern int acpi_battery_get_units(void); 382 extern int acpi_battery_get_info_expire(void); 383 extern int acpi_battery_get_battdesc(int, struct acpi_battdesc *); 384 385 extern int acpi_cmbat_get_battinfo(int, struct acpi_battinfo *); 386 387 /* 388 * AC adapter interface. 389 */ 390 391 extern int acpi_acad_get_acline(int *); 392 393 #if __FreeBSD_version >= 500000 394 #ifndef ACPI_MAX_THREADS 395 #define ACPI_MAX_THREADS 3 396 #endif 397 #if ACPI_MAX_THREADS > 0 398 #define ACPI_USE_THREADS 399 #endif 400 #endif 401 402 #ifdef ACPI_USE_THREADS 403 /* 404 * ACPI task kernel thread initialization. 405 */ 406 extern int acpi_task_thread_init(void); 407 #endif 408 409