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 35 #include <machine/bus.h> 36 #include <machine/resource.h> 37 38 struct acpi_softc { 39 device_t acpi_dev; 40 dev_t acpi_dev_t; 41 42 struct resource *acpi_irq; 43 int acpi_irq_rid; 44 void *acpi_irq_handle; 45 46 int acpi_enabled; 47 int acpi_sstate; 48 int acpi_sleep_disabled; 49 50 struct sysctl_ctx_list acpi_sysctl_ctx; 51 struct sysctl_oid *acpi_sysctl_tree; 52 #define ACPI_POWER_BUTTON_DEFAULT_SX ACPI_STATE_S5; 53 #define ACPI_SLEEP_BUTTON_DEFAULT_SX ACPI_STATE_S1; 54 #define ACPI_LID_SWITCH_DEFAULT_SX ACPI_STATE_S1; 55 int acpi_power_button_sx; 56 int acpi_sleep_button_sx; 57 int acpi_lid_switch_sx; 58 59 int acpi_standby_sx; 60 int acpi_suspend_sx; 61 62 int acpi_s4bios; 63 64 int acpi_verbose; 65 66 bus_dma_tag_t acpi_waketag; 67 bus_dmamap_t acpi_wakemap; 68 vm_offset_t acpi_wakeaddr; 69 vm_offset_t acpi_wakephys; 70 71 struct sysctl_ctx_list acpi_battery_sysctl_ctx; 72 struct sysctl_oid *acpi_battery_sysctl_tree; 73 }; 74 75 struct acpi_device { 76 /* ACPI ivars */ 77 ACPI_HANDLE ad_handle; 78 int ad_magic; 79 void *ad_private; 80 81 /* resources */ 82 struct resource_list ad_rl; 83 84 }; 85 86 /* 87 * The ACPI subsystem lives under a single mutex. You *must* 88 * acquire this mutex before calling any of the acpi_ or Acpi* functions. 89 * 90 * XXX the ACPI_MSLEEP macro should go away once locking is resolved 91 */ 92 extern struct mtx acpi_mutex; 93 #if 0 94 # define ACPI_LOCK mtx_lock(&acpi_mutex) 95 # define ACPI_UNLOCK mtx_unlock(&acpi_mutex) 96 # define ACPI_ASSERTLOCK mtx_assert(&acpi_mutex, MA_OWNED) 97 # define ACPI_MSLEEP(a, b, c, d, e) msleep(a, b, c, d, e) 98 #else 99 # define ACPI_LOCK 100 # define ACPI_UNLOCK 101 # define ACPI_ASSERTLOCK 102 # define ACPI_MSLEEP(a, b, c, d, e) tsleep(a, c, d, e) 103 #endif 104 105 /* 106 * This is a cheap and nasty way to get around the horrid counted list 107 * argument format that AcpiEvalateObject uses. 108 */ 109 #define ACPI_OBJECTLIST_MAX 16 110 struct acpi_object_list { 111 UINT32 count; 112 ACPI_OBJECT *pointer[ACPI_OBJECTLIST_MAX]; 113 ACPI_OBJECT object[ACPI_OBJECTLIST_MAX]; 114 }; 115 116 static __inline struct acpi_object_list * 117 acpi_AllocObjectList(int nobj) 118 { 119 struct acpi_object_list *l; 120 int i; 121 122 if (nobj > ACPI_OBJECTLIST_MAX) 123 return(NULL); 124 if ((l = AcpiOsAllocate(sizeof(*l))) == NULL) 125 return(NULL); 126 bzero(l, sizeof(*l)); 127 for (i = 0; i < ACPI_OBJECTLIST_MAX; i++) 128 l->pointer[i] = &l->object[i]; 129 l->count = nobj; 130 return(l); 131 } 132 133 /* 134 * Note that the low ivar values are reserved to provide 135 * interface compatibility with ISA drivers which can also 136 * attach to ACPI. 137 */ 138 #define ACPI_IVAR_HANDLE 0x100 139 #define ACPI_IVAR_MAGIC 0x101 140 #define ACPI_IVAR_PRIVATE 0x102 141 142 static __inline ACPI_HANDLE 143 acpi_get_handle(device_t dev) 144 { 145 uintptr_t up; 146 ACPI_HANDLE h; 147 148 if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_HANDLE, &up)) 149 return(NULL); 150 h = (ACPI_HANDLE)up; 151 return(h); 152 } 153 154 static __inline int 155 acpi_set_handle(device_t dev, ACPI_HANDLE h) 156 { 157 uintptr_t up; 158 159 up = (uintptr_t)h; 160 return(BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_HANDLE, up)); 161 } 162 163 static __inline int 164 acpi_get_magic(device_t dev) 165 { 166 uintptr_t up; 167 int m; 168 169 if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_MAGIC, &up)) 170 return(0); 171 m = (int)up; 172 return(m); 173 } 174 175 static __inline int 176 acpi_set_magic(device_t dev, int m) 177 { 178 uintptr_t up; 179 180 up = (uintptr_t)m; 181 return(BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_MAGIC, up)); 182 } 183 184 static __inline void * 185 acpi_get_private(device_t dev) 186 { 187 uintptr_t up; 188 void *p; 189 190 if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_PRIVATE, &up)) 191 return(NULL); 192 p = (void *)up; 193 return(p); 194 } 195 196 static __inline int 197 acpi_set_private(device_t dev, void *p) 198 { 199 uintptr_t up; 200 201 up = (uintptr_t)p; 202 return(BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_PRIVATE, up)); 203 } 204 205 static __inline ACPI_OBJECT_TYPE 206 acpi_get_type(device_t dev) 207 { 208 ACPI_HANDLE h; 209 ACPI_OBJECT_TYPE t; 210 211 if ((h = acpi_get_handle(dev)) == NULL) 212 return(ACPI_TYPE_NOT_FOUND); 213 if (AcpiGetType(h, &t) != AE_OK) 214 return(ACPI_TYPE_NOT_FOUND); 215 return(t); 216 } 217 218 #ifdef ENABLE_DEBUGGER 219 extern void acpi_EnterDebugger(void); 220 #endif 221 222 #ifdef ACPI_DEBUG 223 #include <sys/cons.h> 224 #define STEP(x) do {printf x, printf("\n"); cngetc();} while (0) 225 #else 226 #define STEP(x) 227 #endif 228 229 #define ACPI_VPRINT(dev, acpi_sc, x...) do { \ 230 if (acpi_get_verbose(acpi_sc)) \ 231 device_printf(dev, x); \ 232 } while (0) 233 234 extern BOOLEAN acpi_DeviceIsPresent(device_t dev); 235 extern BOOLEAN acpi_MatchHid(device_t dev, char *hid); 236 extern ACPI_STATUS acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result); 237 extern ACPI_BUFFER *acpi_AllocBuffer(int size); 238 extern ACPI_STATUS acpi_GetIntoBuffer(ACPI_HANDLE handle, 239 ACPI_STATUS (*func)(ACPI_HANDLE, ACPI_BUFFER *), 240 ACPI_BUFFER *buf); 241 extern ACPI_STATUS acpi_GetTableIntoBuffer(ACPI_TABLE_TYPE table, UINT32 instance, ACPI_BUFFER *buf); 242 extern ACPI_STATUS acpi_EvaluateIntoBuffer(ACPI_HANDLE object, ACPI_STRING pathname, 243 ACPI_OBJECT_LIST *params, ACPI_BUFFER *buf); 244 extern ACPI_STATUS acpi_EvaluateInteger(ACPI_HANDLE handle, char *path, int *number); 245 extern ACPI_STATUS acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, int *number); 246 extern ACPI_STATUS acpi_ForeachPackageObject(ACPI_OBJECT *obj, 247 void (* func)(ACPI_OBJECT *comp, void *arg), 248 void *arg); 249 extern ACPI_STATUS acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp); 250 extern ACPI_STATUS acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res); 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) __P((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 int acpi_machdep_init(device_t dev); 327 extern void acpi_install_wakeup_handler(struct acpi_softc *sc); 328 extern int acpi_sleep_machdep(struct acpi_softc *sc, int state); 329 330 /* 331 * Battery Abstruction. 332 */ 333 struct acpi_battinfo; 334 struct acpi_battdesc; 335 336 extern int acpi_battery_register(int, int); 337 extern int acpi_battery_get_battinfo(int, struct acpi_battinfo *); 338 extern int acpi_battery_get_units(void); 339 extern int acpi_battery_get_info_expire(void); 340 extern int acpi_battery_get_battdesc(int, struct acpi_battdesc *); 341 342 extern int acpi_cmbat_get_battinfo(int, struct acpi_battinfo *); 343 344 /* 345 * AC adapter interface. 346 */ 347 348 extern int acpi_acad_get_acline(int *); 349 350 /* 351 * System power API. 352 * 353 * XXX should this be further generalised? 354 * 355 */ 356 #define POWERPROFILE_PERFORMANCE 0 357 #define POWERPROFILE_ECONOMY 1 358 359 extern int powerprofile_get_state(void); 360 extern void powerprofile_set_state(int state); 361 362 typedef void (*powerprofile_change_hook)(void *); 363 EVENTHANDLER_DECLARE(powerprofile_change, powerprofile_change_hook); 364 365 #if defined(ACPI_MAX_THREADS) && ACPI_MAX_THREADS > 0 366 /* 367 * ACPI task kernel thread initialization. 368 */ 369 extern int acpi_task_thread_init(void); 370 #endif 371 372