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 extern devclass_t acpi_devclass; 39 40 struct acpi_softc { 41 device_t acpi_dev; 42 dev_t acpi_dev_t; 43 44 struct resource *acpi_irq; 45 int acpi_irq_rid; 46 void *acpi_irq_handle; 47 48 int acpi_enabled; 49 int acpi_sstate; 50 51 struct sysctl_ctx_list acpi_sysctl_ctx; 52 struct sysctl_oid *acpi_sysctl_tree; 53 #define ACPI_POWER_BUTTON_DEFAULT_SX ACPI_STATE_S5; 54 #define ACPI_SLEEP_BUTTON_DEFAULT_SX ACPI_STATE_S1; 55 #define ACPI_LID_SWITCH_DEFAULT_SX ACPI_STATE_S1; 56 int acpi_power_button_sx; 57 int acpi_sleep_button_sx; 58 int acpi_lid_switch_sx; 59 60 int acpi_standby_sx; 61 int acpi_suspend_sx; 62 63 int acpi_s4bios; 64 65 int acpi_verbose; 66 67 bus_dma_tag_t acpi_waketag; 68 bus_dmamap_t acpi_wakemap; 69 vm_offset_t acpi_wakeaddr; 70 vm_offset_t acpi_wakephys; 71 72 struct sysctl_ctx_list acpi_battery_sysctl_ctx; 73 struct sysctl_oid *acpi_battery_sysctl_tree; 74 }; 75 76 struct acpi_device { 77 /* ACPI ivars */ 78 ACPI_HANDLE ad_handle; 79 int ad_magic; 80 void *ad_private; 81 82 /* resources */ 83 struct resource_list ad_rl; 84 85 }; 86 87 /* 88 * The ACPI subsystem lives under a single mutex. You *must* 89 * acquire this mutex before calling any of the acpi_ or Acpi* functions. 90 * 91 * XXX the ACPI_MSLEEP macro should go away once locking is resolved 92 */ 93 extern struct mtx acpi_mutex; 94 #if 0 95 # define ACPI_LOCK mtx_lock(&acpi_mutex) 96 # define ACPI_UNLOCK mtx_unlock(&acpi_mutex) 97 # define ACPI_ASSERTLOCK mtx_assert(&acpi_mutex, MA_OWNED) 98 # define ACPI_MSLEEP(a, b, c, d, e) msleep(a, b, c, d, e) 99 #else 100 # define ACPI_LOCK 101 # define ACPI_UNLOCK 102 # define ACPI_ASSERTLOCK 103 # define ACPI_MSLEEP(a, b, c, d, e) tsleep(a, c, d, e) 104 #endif 105 106 /* 107 * This is a cheap and nasty way to get around the horrid counted list 108 * argument format that AcpiEvalateObject uses. 109 */ 110 #define ACPI_OBJECTLIST_MAX 16 111 struct acpi_object_list { 112 UINT32 count; 113 ACPI_OBJECT *pointer[ACPI_OBJECTLIST_MAX]; 114 ACPI_OBJECT object[ACPI_OBJECTLIST_MAX]; 115 }; 116 117 static __inline struct acpi_object_list * 118 acpi_AllocObjectList(int nobj) 119 { 120 struct acpi_object_list *l; 121 int i; 122 123 if (nobj > ACPI_OBJECTLIST_MAX) 124 return(NULL); 125 if ((l = AcpiOsAllocate(sizeof(*l))) == NULL) 126 return(NULL); 127 bzero(l, sizeof(*l)); 128 for (i = 0; i < ACPI_OBJECTLIST_MAX; i++) 129 l->pointer[i] = &l->object[i]; 130 l->count = nobj; 131 return(l); 132 } 133 134 /* 135 * Note that the low ivar values are reserved to provide 136 * interface compatibility with ISA drivers which can also 137 * attach to ACPI. 138 */ 139 #define ACPI_IVAR_HANDLE 0x100 140 #define ACPI_IVAR_MAGIC 0x101 141 #define ACPI_IVAR_PRIVATE 0x102 142 143 static __inline ACPI_HANDLE 144 acpi_get_handle(device_t dev) 145 { 146 uintptr_t up; 147 ACPI_HANDLE h; 148 149 if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_HANDLE, &up)) 150 return(NULL); 151 h = (ACPI_HANDLE)up; 152 return(h); 153 } 154 155 static __inline int 156 acpi_set_handle(device_t dev, ACPI_HANDLE h) 157 { 158 uintptr_t up; 159 160 up = (uintptr_t)h; 161 return(BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_HANDLE, up)); 162 } 163 164 static __inline int 165 acpi_get_magic(device_t dev) 166 { 167 uintptr_t up; 168 int m; 169 170 if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_MAGIC, &up)) 171 return(0); 172 m = (int)up; 173 return(m); 174 } 175 176 static __inline int 177 acpi_set_magic(device_t dev, int m) 178 { 179 uintptr_t up; 180 181 up = (uintptr_t)m; 182 return(BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_MAGIC, up)); 183 } 184 185 static __inline void * 186 acpi_get_private(device_t dev) 187 { 188 uintptr_t up; 189 void *p; 190 191 if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_PRIVATE, &up)) 192 return(NULL); 193 p = (void *)up; 194 return(p); 195 } 196 197 static __inline int 198 acpi_set_private(device_t dev, void *p) 199 { 200 uintptr_t up; 201 202 up = (uintptr_t)p; 203 return(BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_PRIVATE, up)); 204 } 205 206 static __inline ACPI_OBJECT_TYPE 207 acpi_get_type(device_t dev) 208 { 209 ACPI_HANDLE h; 210 ACPI_OBJECT_TYPE t; 211 212 if ((h = acpi_get_handle(dev)) == NULL) 213 return(ACPI_TYPE_NOT_FOUND); 214 if (AcpiGetType(h, &t) != AE_OK) 215 return(ACPI_TYPE_NOT_FOUND); 216 return(t); 217 } 218 219 #ifdef ENABLE_DEBUGGER 220 extern void acpi_EnterDebugger(void); 221 #endif 222 223 #ifdef ACPI_DEBUG 224 #include <sys/cons.h> 225 #define STEP(x) do {printf x, printf("\n"); cngetc();} while (0) 226 #else 227 #define STEP(x) 228 #endif 229 230 extern BOOLEAN acpi_DeviceIsPresent(device_t dev); 231 extern BOOLEAN acpi_MatchHid(device_t dev, char *hid); 232 extern ACPI_STATUS acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result); 233 extern ACPI_BUFFER *acpi_AllocBuffer(int size); 234 extern ACPI_STATUS acpi_GetIntoBuffer(ACPI_HANDLE handle, 235 ACPI_STATUS (*func)(ACPI_HANDLE, ACPI_BUFFER *), 236 ACPI_BUFFER *buf); 237 extern ACPI_STATUS acpi_GetTableIntoBuffer(ACPI_TABLE_TYPE table, UINT32 instance, ACPI_BUFFER *buf); 238 extern ACPI_STATUS acpi_EvaluateIntoBuffer(ACPI_HANDLE object, ACPI_STRING pathname, 239 ACPI_OBJECT_LIST *params, ACPI_BUFFER *buf); 240 extern ACPI_STATUS acpi_EvaluateInteger(ACPI_HANDLE handle, char *path, int *number); 241 extern ACPI_STATUS acpi_ForeachPackageObject(ACPI_OBJECT *obj, 242 void (* func)(ACPI_OBJECT *comp, void *arg), 243 void *arg); 244 extern ACPI_STATUS acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp); 245 extern ACPI_STATUS acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res); 246 247 extern ACPI_STATUS acpi_SetSleepState(struct acpi_softc *sc, int state); 248 extern ACPI_STATUS acpi_Enable(struct acpi_softc *sc); 249 extern ACPI_STATUS acpi_Disable(struct acpi_softc *sc); 250 251 struct acpi_parse_resource_set { 252 void (* set_init)(device_t dev, void **context); 253 void (* set_done)(device_t dev, void *context); 254 void (* set_ioport)(device_t dev, void *context, u_int32_t base, u_int32_t length); 255 void (* set_iorange)(device_t dev, void *context, u_int32_t low, u_int32_t high, 256 u_int32_t length, u_int32_t align); 257 void (* set_memory)(device_t dev, void *context, u_int32_t base, u_int32_t length); 258 void (* set_memoryrange)(device_t dev, void *context, u_int32_t low, u_int32_t high, 259 u_int32_t length, u_int32_t align); 260 void (* set_irq)(device_t dev, void *context, u_int32_t irq); 261 void (* set_drq)(device_t dev, void *context, u_int32_t drq); 262 void (* set_start_dependant)(device_t dev, void *context, int preference); 263 void (* set_end_dependant)(device_t dev, void *context); 264 }; 265 266 extern struct acpi_parse_resource_set acpi_res_parse_set; 267 extern ACPI_STATUS acpi_parse_resources(device_t dev, ACPI_HANDLE handle, 268 struct acpi_parse_resource_set *set); 269 /* XXX until Intel fix this in their headers, based on NEXT_RESOURCE */ 270 #define ACPI_RESOURCE_NEXT(Res) (ACPI_RESOURCE *)((UINT8 *) Res + Res->Length) 271 272 /* 273 * ACPI event handling 274 */ 275 extern UINT32 acpi_eventhandler_power_button_for_sleep(void *context); 276 extern UINT32 acpi_eventhandler_power_button_for_wakeup(void *context); 277 extern UINT32 acpi_eventhandler_sleep_button_for_sleep(void *context); 278 extern UINT32 acpi_eventhandler_sleep_button_for_wakeup(void *context); 279 280 #define ACPI_EVENT_PRI_FIRST 0 281 #define ACPI_EVENT_PRI_DEFAULT 10000 282 #define ACPI_EVENT_PRI_LAST 20000 283 284 typedef void (*acpi_event_handler_t) __P((void *, int)); 285 286 EVENTHANDLER_DECLARE(acpi_sleep_event, acpi_event_handler_t); 287 EVENTHANDLER_DECLARE(acpi_wakeup_event, acpi_event_handler_t); 288 289 /* 290 * Device power control. 291 */ 292 extern ACPI_STATUS acpi_pwr_switch_consumer(ACPI_HANDLE consumer, int state); 293 294 /* 295 * Misc. 296 */ 297 static __inline struct acpi_softc * 298 acpi_device_get_parent_softc(device_t child) 299 { 300 device_t parent; 301 302 parent = device_get_parent(child); 303 if (parent == NULL) { 304 return(NULL); 305 } 306 return(device_get_softc(parent)); 307 } 308 309 static __inline int 310 acpi_get_verbose(struct acpi_softc *sc) 311 { 312 if (sc) 313 return(sc->acpi_verbose); 314 return(0); 315 } 316 317 extern char *acpi_name(ACPI_HANDLE handle); 318 extern int acpi_avoid(ACPI_HANDLE handle); 319 extern int acpi_disabled(char *subsys); 320 321 extern int acpi_machdep_init(device_t dev); 322 extern void acpi_install_wakeup_handler(struct acpi_softc *sc); 323 extern int acpi_sleep_machdep(struct acpi_softc *sc, int state); 324 325 /* 326 * Battery Abstruction. 327 */ 328 struct acpi_battinfo; 329 struct acpi_battdesc; 330 331 extern int acpi_battery_register(int, int); 332 extern int acpi_battery_get_battinfo(int, struct acpi_battinfo *); 333 extern int acpi_battery_get_units(void); 334 extern int acpi_battery_get_info_expire(void); 335 extern int acpi_battery_get_battdesc(int, struct acpi_battdesc *); 336 337 extern int acpi_cmbat_get_battinfo(int, struct acpi_battinfo *); 338 339 /* 340 * AC adapter interface. 341 */ 342 343 extern int acpi_acad_get_acline(int *); 344 345 /* 346 * System power API. 347 * 348 * XXX should this be further generalised? 349 * 350 */ 351 #define POWERPROFILE_PERFORMANCE 0 352 #define POWERPROFILE_ECONOMY 1 353 354 extern int powerprofile_get_state(void); 355 extern void powerprofile_set_state(int state); 356 357 typedef void (*powerprofile_change_hook)(void *); 358 EVENTHANDLER_DECLARE(powerprofile_change, powerprofile_change_hook); 359