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