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