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 * ACPI CA does not define layers for non-ACPI CA drivers. 107 * We define some here within the range provided. 108 */ 109 #define ACPI_BUS 0x00010000 110 #define ACPI_SYSTEM 0x00020000 111 #define ACPI_POWER 0x00040000 112 #define ACPI_EC 0x00080000 113 #define ACPI_AC_ADAPTER 0x00100000 114 #define ACPI_BATTERY 0x00110000 115 #define ACPI_BUTTON 0x00120000 116 #define ACPI_PROCESSOR 0x00140000 117 #define ACPI_THERMAL 0x00180000 118 #define ACPI_FAN 0x00200000 119 120 /* 121 * This is a cheap and nasty way to get around the horrid counted list 122 * argument format that AcpiEvalateObject uses. 123 */ 124 #define ACPI_OBJECTLIST_MAX 16 125 struct acpi_object_list { 126 UINT32 count; 127 ACPI_OBJECT *pointer[ACPI_OBJECTLIST_MAX]; 128 ACPI_OBJECT object[ACPI_OBJECTLIST_MAX]; 129 }; 130 131 static __inline struct acpi_object_list * 132 acpi_AllocObjectList(int nobj) 133 { 134 struct acpi_object_list *l; 135 int i; 136 137 if (nobj > ACPI_OBJECTLIST_MAX) 138 return(NULL); 139 if ((l = AcpiOsAllocate(sizeof(*l))) == NULL) 140 return(NULL); 141 bzero(l, sizeof(*l)); 142 for (i = 0; i < ACPI_OBJECTLIST_MAX; i++) 143 l->pointer[i] = &l->object[i]; 144 l->count = nobj; 145 return(l); 146 } 147 148 /* 149 * Note that the low ivar values are reserved to provide 150 * interface compatibility with ISA drivers which can also 151 * attach to ACPI. 152 */ 153 #define ACPI_IVAR_HANDLE 0x100 154 #define ACPI_IVAR_MAGIC 0x101 155 #define ACPI_IVAR_PRIVATE 0x102 156 157 static __inline ACPI_HANDLE 158 acpi_get_handle(device_t dev) 159 { 160 uintptr_t up; 161 ACPI_HANDLE h; 162 163 if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_HANDLE, &up)) 164 return(NULL); 165 h = (ACPI_HANDLE)up; 166 return(h); 167 } 168 169 static __inline int 170 acpi_set_handle(device_t dev, ACPI_HANDLE h) 171 { 172 uintptr_t up; 173 174 up = (uintptr_t)h; 175 return(BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_HANDLE, up)); 176 } 177 178 static __inline int 179 acpi_get_magic(device_t dev) 180 { 181 uintptr_t up; 182 int m; 183 184 if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_MAGIC, &up)) 185 return(0); 186 m = (int)up; 187 return(m); 188 } 189 190 static __inline int 191 acpi_set_magic(device_t dev, int m) 192 { 193 uintptr_t up; 194 195 up = (uintptr_t)m; 196 return(BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_MAGIC, up)); 197 } 198 199 static __inline void * 200 acpi_get_private(device_t dev) 201 { 202 uintptr_t up; 203 void *p; 204 205 if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_PRIVATE, &up)) 206 return(NULL); 207 p = (void *)up; 208 return(p); 209 } 210 211 static __inline int 212 acpi_set_private(device_t dev, void *p) 213 { 214 uintptr_t up; 215 216 up = (uintptr_t)p; 217 return(BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_PRIVATE, up)); 218 } 219 220 static __inline ACPI_OBJECT_TYPE 221 acpi_get_type(device_t dev) 222 { 223 ACPI_HANDLE h; 224 ACPI_OBJECT_TYPE t; 225 226 if ((h = acpi_get_handle(dev)) == NULL) 227 return(ACPI_TYPE_NOT_FOUND); 228 if (AcpiGetType(h, &t) != AE_OK) 229 return(ACPI_TYPE_NOT_FOUND); 230 return(t); 231 } 232 233 #ifdef ENABLE_DEBUGGER 234 extern void acpi_EnterDebugger(void); 235 #endif 236 237 #ifdef ACPI_DEBUG 238 #include <sys/cons.h> 239 #define STEP(x) do {printf x, printf("\n"); cngetc();} while (0) 240 #else 241 #define STEP(x) 242 #endif 243 244 #define ACPI_VPRINT(dev, acpi_sc, x...) do { \ 245 if (acpi_get_verbose(acpi_sc)) \ 246 device_printf(dev, x); \ 247 } while (0) 248 249 #define ACPI_DEVINFO_PRESENT(x) (((x) & 0x9) == 9) 250 extern BOOLEAN acpi_DeviceIsPresent(device_t dev); 251 extern BOOLEAN acpi_BatteryIsPresent(device_t dev); 252 extern BOOLEAN acpi_MatchHid(device_t dev, char *hid); 253 extern ACPI_STATUS acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result); 254 extern ACPI_BUFFER *acpi_AllocBuffer(int size); 255 extern ACPI_STATUS acpi_EvaluateInteger(ACPI_HANDLE handle, char *path, int *number); 256 extern ACPI_STATUS acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, int *number); 257 extern ACPI_STATUS acpi_ForeachPackageObject(ACPI_OBJECT *obj, 258 void (* func)(ACPI_OBJECT *comp, void *arg), 259 void *arg); 260 extern ACPI_STATUS acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp); 261 extern ACPI_STATUS acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res); 262 263 extern ACPI_STATUS acpi_SetSleepState(struct acpi_softc *sc, int state); 264 extern ACPI_STATUS acpi_Enable(struct acpi_softc *sc); 265 extern ACPI_STATUS acpi_Disable(struct acpi_softc *sc); 266 267 struct acpi_parse_resource_set { 268 void (* set_init)(device_t dev, void **context); 269 void (* set_done)(device_t dev, void *context); 270 void (* set_ioport)(device_t dev, void *context, u_int32_t base, u_int32_t length); 271 void (* set_iorange)(device_t dev, void *context, u_int32_t low, u_int32_t high, 272 u_int32_t length, u_int32_t align); 273 void (* set_memory)(device_t dev, void *context, u_int32_t base, u_int32_t length); 274 void (* set_memoryrange)(device_t dev, void *context, u_int32_t low, u_int32_t high, 275 u_int32_t length, u_int32_t align); 276 void (* set_irq)(device_t dev, void *context, u_int32_t *irq, int cout); 277 void (* set_drq)(device_t dev, void *context, u_int32_t *drq, int count); 278 void (* set_start_dependant)(device_t dev, void *context, int preference); 279 void (* set_end_dependant)(device_t dev, void *context); 280 }; 281 282 extern struct acpi_parse_resource_set acpi_res_parse_set; 283 extern ACPI_STATUS acpi_parse_resources(device_t dev, ACPI_HANDLE handle, 284 struct acpi_parse_resource_set *set); 285 /* XXX until Intel fix this in their headers, based on NEXT_RESOURCE */ 286 #define ACPI_RESOURCE_NEXT(Res) (ACPI_RESOURCE *)((UINT8 *) Res + Res->Length) 287 288 /* 289 * ACPI event handling 290 */ 291 extern UINT32 acpi_eventhandler_power_button_for_sleep(void *context); 292 extern UINT32 acpi_eventhandler_power_button_for_wakeup(void *context); 293 extern UINT32 acpi_eventhandler_sleep_button_for_sleep(void *context); 294 extern UINT32 acpi_eventhandler_sleep_button_for_wakeup(void *context); 295 296 #define ACPI_EVENT_PRI_FIRST 0 297 #define ACPI_EVENT_PRI_DEFAULT 10000 298 #define ACPI_EVENT_PRI_LAST 20000 299 300 typedef void (*acpi_event_handler_t)(void *, int); 301 302 EVENTHANDLER_DECLARE(acpi_sleep_event, acpi_event_handler_t); 303 EVENTHANDLER_DECLARE(acpi_wakeup_event, acpi_event_handler_t); 304 305 /* 306 * Device power control. 307 */ 308 extern ACPI_STATUS acpi_pwr_switch_consumer(ACPI_HANDLE consumer, int state); 309 310 /* 311 * Misc. 312 */ 313 static __inline struct acpi_softc * 314 acpi_device_get_parent_softc(device_t child) 315 { 316 device_t parent; 317 318 parent = device_get_parent(child); 319 if (parent == NULL) { 320 return(NULL); 321 } 322 return(device_get_softc(parent)); 323 } 324 325 static __inline int 326 acpi_get_verbose(struct acpi_softc *sc) 327 { 328 if (sc) 329 return(sc->acpi_verbose); 330 return(0); 331 } 332 333 extern char *acpi_name(ACPI_HANDLE handle); 334 extern int acpi_avoid(ACPI_HANDLE handle); 335 extern int acpi_disabled(char *subsys); 336 337 extern void acpi_device_enable_wake_capability(ACPI_HANDLE h, int enable); 338 extern void acpi_device_enable_wake_event(ACPI_HANDLE h); 339 340 extern int acpi_machdep_init(device_t dev); 341 extern void acpi_install_wakeup_handler(struct acpi_softc *sc); 342 extern int acpi_sleep_machdep(struct acpi_softc *sc, int state); 343 344 /* 345 * Battery Abstraction. 346 */ 347 struct acpi_battinfo; 348 struct acpi_battdesc; 349 350 extern int acpi_battery_register(int, int); 351 extern int acpi_battery_get_battinfo(int, struct acpi_battinfo *); 352 extern int acpi_battery_get_units(void); 353 extern int acpi_battery_get_info_expire(void); 354 extern int acpi_battery_get_battdesc(int, struct acpi_battdesc *); 355 356 extern int acpi_cmbat_get_battinfo(int, struct acpi_battinfo *); 357 358 /* 359 * AC adapter interface. 360 */ 361 362 extern int acpi_acad_get_acline(int *); 363 364 #if defined(ACPI_MAX_THREADS) && ACPI_MAX_THREADS > 0 365 /* 366 * ACPI task kernel thread initialization. 367 */ 368 extern int acpi_task_thread_init(void); 369 #endif 370 371