1*d58fda43Sjbeloro 2fd358c0dSmyers /* 3fd358c0dSmyers * CDDL HEADER START 4fd358c0dSmyers * 5fd358c0dSmyers * The contents of this file are subject to the terms of the 6fd358c0dSmyers * Common Development and Distribution License, Version 1.0 only 7fd358c0dSmyers * (the "License"). You may not use this file except in compliance 8fd358c0dSmyers * with the License. 9fd358c0dSmyers * 10fd358c0dSmyers * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 11fd358c0dSmyers * or http://www.opensolaris.org/os/licensing. 12fd358c0dSmyers * See the License for the specific language governing permissions 13fd358c0dSmyers * and limitations under the License. 14fd358c0dSmyers * 15fd358c0dSmyers * When distributing Covered Code, include this CDDL HEADER in each 16fd358c0dSmyers * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 17fd358c0dSmyers * If applicable, add the following below this CDDL HEADER, with the 18fd358c0dSmyers * fields enclosed by brackets "[]" replaced with your own identifying 19fd358c0dSmyers * information: Portions Copyright [yyyy] [name of copyright owner] 20fd358c0dSmyers * 21fd358c0dSmyers * CDDL HEADER END 22fd358c0dSmyers */ 23fd358c0dSmyers /* 24fd358c0dSmyers * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 25fd358c0dSmyers * Use is subject to license terms. 26fd358c0dSmyers */ 27fd358c0dSmyers 28fd358c0dSmyers #pragma ident "%Z%%M% %I% %E% SMI" 29fd358c0dSmyers 30fd358c0dSmyers /* 31fd358c0dSmyers * Power Button Driver 32fd358c0dSmyers * 33fd358c0dSmyers * This driver handles interrupt generated by the power button on 34fd358c0dSmyers * platforms with "power" device node which has "button" property. 35fd358c0dSmyers * Currently, these platforms are: 36fd358c0dSmyers * 37fd358c0dSmyers * ACPI-enabled x86/x64 platforms 38fd358c0dSmyers * Ultra-5_10, Ultra-80, Sun-Blade-100, Sun-Blade-150, 39fd358c0dSmyers * Sun-Blade-1500, Sun-Blade-2500, 40fd358c0dSmyers * Sun-Fire-V210, Sun-Fire-V240, Netra-240 41fd358c0dSmyers * 42fd358c0dSmyers * Only one instance is allowed to attach. In order to know when 43fd358c0dSmyers * an application that has opened the device is going away, a new 44fd358c0dSmyers * minor clone is created for each open(9E) request. There are 45fd358c0dSmyers * allocations for creating minor clones between 1 and 255. The ioctl 46fd358c0dSmyers * interface is defined by pbio(7I) and approved as part of 47fd358c0dSmyers * PSARC/1999/393 case. 48fd358c0dSmyers */ 49fd358c0dSmyers 50fd358c0dSmyers #include <sys/types.h> 51fd358c0dSmyers #include <sys/conf.h> 52fd358c0dSmyers #include <sys/ddi.h> 53fd358c0dSmyers #include <sys/sunddi.h> 54fd358c0dSmyers #include <sys/ddi_impldefs.h> 55fd358c0dSmyers #include <sys/cmn_err.h> 56fd358c0dSmyers #include <sys/errno.h> 57fd358c0dSmyers #include <sys/modctl.h> 58fd358c0dSmyers #include <sys/machsystm.h> 59fd358c0dSmyers #include <sys/open.h> 60fd358c0dSmyers #include <sys/stat.h> 61fd358c0dSmyers #include <sys/poll.h> 62fd358c0dSmyers #include <sys/pbio.h> 63*d58fda43Sjbeloro 64fd358c0dSmyers #ifdef ACPI_POWER_BUTTON 65*d58fda43Sjbeloro 66fd358c0dSmyers #include <sys/acpi/acpi.h> 67fd358c0dSmyers #include <sys/acpica.h> 68*d58fda43Sjbeloro 69*d58fda43Sjbeloro #else 70*d58fda43Sjbeloro 71*d58fda43Sjbeloro #include <sys/epic.h> 72*d58fda43Sjbeloro /* 73*d58fda43Sjbeloro * Some #defs that must be here as they differ for power.c 74*d58fda43Sjbeloro * and epic.c 75*d58fda43Sjbeloro */ 76*d58fda43Sjbeloro #define EPIC_REGS_OFFSET 0x00 77*d58fda43Sjbeloro #define EPIC_REGS_LEN 0x82 78*d58fda43Sjbeloro 79*d58fda43Sjbeloro 80*d58fda43Sjbeloro /* 81*d58fda43Sjbeloro * This flag, which is set for platforms, that have EPIC processor 82*d58fda43Sjbeloro * to process power button interrupt, helps in executing platform 83*d58fda43Sjbeloro * specific code. 84*d58fda43Sjbeloro */ 85*d58fda43Sjbeloro static char hasEPIC = B_FALSE; 86fd358c0dSmyers #endif /* ACPI_POWER_BUTTON */ 87fd358c0dSmyers 88fd358c0dSmyers /* 89fd358c0dSmyers * Maximum number of clone minors that is allowed. This value 90fd358c0dSmyers * is defined relatively low to save memory. 91fd358c0dSmyers */ 92fd358c0dSmyers #define POWER_MAX_CLONE 256 93fd358c0dSmyers 94fd358c0dSmyers /* 95fd358c0dSmyers * Minor number is instance << 8 + clone minor from range 1-255; clone 0 96fd358c0dSmyers * is reserved for "original" minor. 97fd358c0dSmyers */ 98fd358c0dSmyers #define POWER_MINOR_TO_CLONE(minor) ((minor) & (POWER_MAX_CLONE - 1)) 99fd358c0dSmyers 100fd358c0dSmyers /* 101fd358c0dSmyers * Power Button Abort Delay 102fd358c0dSmyers */ 103fd358c0dSmyers #define ABORT_INCREMENT_DELAY 10 104fd358c0dSmyers 105fd358c0dSmyers /* 106fd358c0dSmyers * Driver global variables 107fd358c0dSmyers */ 108fd358c0dSmyers static void *power_state; 109fd358c0dSmyers static int power_inst = -1; 110fd358c0dSmyers 111fd358c0dSmyers static hrtime_t power_button_debounce = NANOSEC/MILLISEC*10; 112fd358c0dSmyers static hrtime_t power_button_abort_interval = 1.5 * NANOSEC; 113fd358c0dSmyers static int power_button_abort_presses = 3; 114fd358c0dSmyers static int power_button_abort_enable = 1; 115fd358c0dSmyers static int power_button_enable = 1; 116fd358c0dSmyers 117fd358c0dSmyers static int power_button_pressed = 0; 118fd358c0dSmyers static int power_button_cancel = 0; 119fd358c0dSmyers static int power_button_timeouts = 0; 120fd358c0dSmyers static int timeout_cancel = 0; 121fd358c0dSmyers static int additional_presses = 0; 122fd358c0dSmyers 123fd358c0dSmyers /* 124fd358c0dSmyers * Function prototypes 125fd358c0dSmyers */ 126fd358c0dSmyers static int power_attach(dev_info_t *, ddi_attach_cmd_t); 127fd358c0dSmyers static int power_detach(dev_info_t *, ddi_detach_cmd_t); 128fd358c0dSmyers static int power_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **); 129fd358c0dSmyers static int power_open(dev_t *, int, int, cred_t *); 130fd358c0dSmyers static int power_close(dev_t, int, int, cred_t *); 131fd358c0dSmyers static int power_ioctl(dev_t, int, intptr_t, int, cred_t *, int *); 132fd358c0dSmyers static int power_chpoll(dev_t, short, int, short *, struct pollhead **); 133fd358c0dSmyers #ifndef ACPI_POWER_BUTTON 134fd358c0dSmyers static uint_t power_high_intr(caddr_t); 135fd358c0dSmyers #endif 136fd358c0dSmyers static uint_t power_soft_intr(caddr_t); 137fd358c0dSmyers static uint_t power_issue_shutdown(caddr_t); 138fd358c0dSmyers static void power_timeout(caddr_t); 139fd358c0dSmyers static void power_log_message(void); 140fd358c0dSmyers 141fd358c0dSmyers /* 142fd358c0dSmyers * Structure used in the driver 143fd358c0dSmyers */ 144fd358c0dSmyers struct power_soft_state { 145fd358c0dSmyers dev_info_t *dip; /* device info pointer */ 146fd358c0dSmyers kmutex_t power_mutex; /* mutex lock */ 147fd358c0dSmyers kmutex_t power_intr_mutex; /* interrupt mutex lock */ 148fd358c0dSmyers ddi_iblock_cookie_t soft_iblock_cookie; /* holds interrupt cookie */ 149fd358c0dSmyers ddi_iblock_cookie_t high_iblock_cookie; /* holds interrupt cookie */ 150fd358c0dSmyers ddi_softintr_t softintr_id; /* soft interrupt id */ 151fd358c0dSmyers uchar_t clones[POWER_MAX_CLONE]; /* array of minor clones */ 152fd358c0dSmyers int monitor_on; /* clone monitoring the button event */ 153fd358c0dSmyers /* clone 0 indicates no one is */ 154fd358c0dSmyers /* monitoring the button event */ 155fd358c0dSmyers pollhead_t pollhd; /* poll head struct */ 156fd358c0dSmyers int events; /* bit map of occured events */ 157fd358c0dSmyers int shutdown_pending; /* system shutdown in progress */ 158fd358c0dSmyers #ifdef ACPI_POWER_BUTTON 159fd358c0dSmyers boolean_t fixed_attached; /* true means fixed is attached */ 160fd358c0dSmyers boolean_t gpe_attached; /* true means GPE is attached */ 161fd358c0dSmyers ACPI_HANDLE button_obj; /* handle to device power button */ 162fd358c0dSmyers #else 163fd358c0dSmyers ddi_acc_handle_t power_rhandle; /* power button register handle */ 164fd358c0dSmyers uint8_t *power_btn_reg; /* power button register address */ 165fd358c0dSmyers uint8_t power_btn_bit; /* power button register bit */ 166fd358c0dSmyers boolean_t power_regs_mapped; /* flag to tell if regs mapped */ 167fd358c0dSmyers boolean_t power_btn_ioctl; /* flag to specify ioctl request */ 168fd358c0dSmyers #endif 169fd358c0dSmyers }; 170fd358c0dSmyers 171fd358c0dSmyers #ifdef ACPI_POWER_BUTTON 172fd358c0dSmyers static int power_attach_acpi(struct power_soft_state *softsp); 173fd358c0dSmyers static void power_detach_acpi(struct power_soft_state *softsp); 174fd358c0dSmyers static UINT32 power_acpi_fixed_event(void *ctx); 175fd358c0dSmyers #else 176fd358c0dSmyers static int power_setup_regs(struct power_soft_state *softsp); 177fd358c0dSmyers static void power_free_regs(struct power_soft_state *softsp); 178fd358c0dSmyers #endif /* ACPI_POWER_BUTTON */ 179fd358c0dSmyers 180fd358c0dSmyers /* 181fd358c0dSmyers * Configuration data structures 182fd358c0dSmyers */ 183fd358c0dSmyers static struct cb_ops power_cb_ops = { 184fd358c0dSmyers power_open, /* open */ 185fd358c0dSmyers power_close, /* close */ 186fd358c0dSmyers nodev, /* strategy */ 187fd358c0dSmyers nodev, /* print */ 188fd358c0dSmyers nodev, /* dump */ 189fd358c0dSmyers nodev, /* read */ 190fd358c0dSmyers nodev, /* write */ 191fd358c0dSmyers power_ioctl, /* ioctl */ 192fd358c0dSmyers nodev, /* devmap */ 193fd358c0dSmyers nodev, /* mmap */ 194fd358c0dSmyers nodev, /* segmap */ 195fd358c0dSmyers power_chpoll, /* poll */ 196fd358c0dSmyers ddi_prop_op, /* cb_prop_op */ 197fd358c0dSmyers NULL, /* streamtab */ 198fd358c0dSmyers D_MP | D_NEW, /* Driver compatibility flag */ 199fd358c0dSmyers CB_REV, /* rev */ 200fd358c0dSmyers nodev, /* cb_aread */ 201fd358c0dSmyers nodev /* cb_awrite */ 202fd358c0dSmyers }; 203fd358c0dSmyers 204fd358c0dSmyers static struct dev_ops power_ops = { 205fd358c0dSmyers DEVO_REV, /* devo_rev, */ 206fd358c0dSmyers 0, /* refcnt */ 207fd358c0dSmyers power_getinfo, /* getinfo */ 208fd358c0dSmyers nulldev, /* identify */ 209fd358c0dSmyers nulldev, /* probe */ 210fd358c0dSmyers power_attach, /* attach */ 211fd358c0dSmyers power_detach, /* detach */ 212fd358c0dSmyers nodev, /* reset */ 213fd358c0dSmyers &power_cb_ops, /* cb_ops */ 214fd358c0dSmyers (struct bus_ops *)NULL, /* bus_ops */ 215fd358c0dSmyers NULL /* power */ 216fd358c0dSmyers }; 217fd358c0dSmyers 218fd358c0dSmyers static struct modldrv modldrv = { 219fd358c0dSmyers &mod_driverops, /* Type of module. This one is a driver */ 220fd358c0dSmyers "power button driver v%I%", /* name of module */ 221fd358c0dSmyers &power_ops, /* driver ops */ 222fd358c0dSmyers }; 223fd358c0dSmyers 224fd358c0dSmyers static struct modlinkage modlinkage = { 225fd358c0dSmyers MODREV_1, 226fd358c0dSmyers (void *)&modldrv, 227fd358c0dSmyers NULL 228fd358c0dSmyers }; 229fd358c0dSmyers 230fd358c0dSmyers /* 231fd358c0dSmyers * These are the module initialization routines. 232fd358c0dSmyers */ 233fd358c0dSmyers 234fd358c0dSmyers int 235fd358c0dSmyers _init(void) 236fd358c0dSmyers { 237fd358c0dSmyers int error; 238fd358c0dSmyers 239fd358c0dSmyers if ((error = ddi_soft_state_init(&power_state, 240fd358c0dSmyers sizeof (struct power_soft_state), 0)) != 0) 241fd358c0dSmyers return (error); 242fd358c0dSmyers 243fd358c0dSmyers if ((error = mod_install(&modlinkage)) != 0) 244fd358c0dSmyers ddi_soft_state_fini(&power_state); 245fd358c0dSmyers 246fd358c0dSmyers return (error); 247fd358c0dSmyers } 248fd358c0dSmyers 249fd358c0dSmyers int 250fd358c0dSmyers _fini(void) 251fd358c0dSmyers { 252fd358c0dSmyers int error; 253fd358c0dSmyers 254fd358c0dSmyers if ((error = mod_remove(&modlinkage)) == 0) 255fd358c0dSmyers ddi_soft_state_fini(&power_state); 256fd358c0dSmyers 257fd358c0dSmyers return (error); 258fd358c0dSmyers } 259fd358c0dSmyers 260fd358c0dSmyers int 261fd358c0dSmyers _info(struct modinfo *modinfop) 262fd358c0dSmyers { 263fd358c0dSmyers return (mod_info(&modlinkage, modinfop)); 264fd358c0dSmyers } 265fd358c0dSmyers 266fd358c0dSmyers /*ARGSUSED*/ 267fd358c0dSmyers static int 268fd358c0dSmyers power_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, 269fd358c0dSmyers void **result) 270fd358c0dSmyers { 271fd358c0dSmyers struct power_soft_state *softsp; 272fd358c0dSmyers 273fd358c0dSmyers if (power_inst == -1) 274fd358c0dSmyers return (DDI_FAILURE); 275fd358c0dSmyers 276fd358c0dSmyers switch (infocmd) { 277fd358c0dSmyers case DDI_INFO_DEVT2DEVINFO: 278fd358c0dSmyers if ((softsp = ddi_get_soft_state(power_state, power_inst)) 279fd358c0dSmyers == NULL) 280fd358c0dSmyers return (DDI_FAILURE); 281fd358c0dSmyers *result = (void *)softsp->dip; 282fd358c0dSmyers return (DDI_SUCCESS); 283fd358c0dSmyers 284fd358c0dSmyers case DDI_INFO_DEVT2INSTANCE: 285fd358c0dSmyers *result = (void *)(uintptr_t)power_inst; 286fd358c0dSmyers return (DDI_SUCCESS); 287fd358c0dSmyers 288fd358c0dSmyers default: 289fd358c0dSmyers return (DDI_FAILURE); 290fd358c0dSmyers } 291fd358c0dSmyers } 292fd358c0dSmyers 293fd358c0dSmyers static int 294fd358c0dSmyers power_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 295fd358c0dSmyers { 296fd358c0dSmyers struct power_soft_state *softsp; 297fd358c0dSmyers 298fd358c0dSmyers switch (cmd) { 299fd358c0dSmyers case DDI_ATTACH: 300fd358c0dSmyers break; 301fd358c0dSmyers case DDI_RESUME: 302fd358c0dSmyers return (DDI_SUCCESS); 303fd358c0dSmyers default: 304fd358c0dSmyers return (DDI_FAILURE); 305fd358c0dSmyers } 306fd358c0dSmyers 307fd358c0dSmyers /* 308fd358c0dSmyers * If the power node doesn't have "button" property, quietly 309fd358c0dSmyers * fail to attach. 310fd358c0dSmyers */ 311fd358c0dSmyers if (ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 312fd358c0dSmyers "button") == 0) 313fd358c0dSmyers return (DDI_FAILURE); 314fd358c0dSmyers 315fd358c0dSmyers if (power_inst != -1) 316fd358c0dSmyers return (DDI_FAILURE); 317fd358c0dSmyers 318fd358c0dSmyers power_inst = ddi_get_instance(dip); 319fd358c0dSmyers 320fd358c0dSmyers if (ddi_soft_state_zalloc(power_state, power_inst) != DDI_SUCCESS) 321fd358c0dSmyers return (DDI_FAILURE); 322fd358c0dSmyers 323fd358c0dSmyers if (ddi_create_minor_node(dip, "power_button", S_IFCHR, 324fd358c0dSmyers (power_inst << 8) + 0, "ddi_power_button", 0) != DDI_SUCCESS) 325fd358c0dSmyers return (DDI_FAILURE); 326fd358c0dSmyers 327fd358c0dSmyers softsp = ddi_get_soft_state(power_state, power_inst); 328fd358c0dSmyers softsp->dip = dip; 329fd358c0dSmyers 330fd358c0dSmyers #ifdef ACPI_POWER_BUTTON 3319f43a0a2Smyers (void) power_attach_acpi(softsp); 332fd358c0dSmyers #else 333fd358c0dSmyers if (power_setup_regs(softsp) != DDI_SUCCESS) { 334fd358c0dSmyers cmn_err(CE_WARN, "power_attach: failed to setup registers"); 335fd358c0dSmyers goto error; 336fd358c0dSmyers } 337fd358c0dSmyers 338fd358c0dSmyers if (ddi_get_iblock_cookie(dip, 0, 339fd358c0dSmyers &softsp->high_iblock_cookie) != DDI_SUCCESS) { 340fd358c0dSmyers cmn_err(CE_WARN, "power_attach: ddi_get_soft_iblock_cookie " 341fd358c0dSmyers "failed."); 342fd358c0dSmyers goto error; 343fd358c0dSmyers } 344fd358c0dSmyers mutex_init(&softsp->power_intr_mutex, NULL, MUTEX_DRIVER, 345fd358c0dSmyers softsp->high_iblock_cookie); 346fd358c0dSmyers 347fd358c0dSmyers if (ddi_add_intr(dip, 0, &softsp->high_iblock_cookie, NULL, 348fd358c0dSmyers power_high_intr, (caddr_t)softsp) != DDI_SUCCESS) { 349fd358c0dSmyers cmn_err(CE_WARN, "power_attach: failed to add high-level " 350fd358c0dSmyers " interrupt handler."); 351fd358c0dSmyers mutex_destroy(&softsp->power_intr_mutex); 352fd358c0dSmyers goto error; 353fd358c0dSmyers } 354fd358c0dSmyers #endif /* ACPI_POWER_BUTTON */ 355fd358c0dSmyers 356fd358c0dSmyers if (ddi_get_soft_iblock_cookie(dip, DDI_SOFTINT_LOW, 357fd358c0dSmyers &softsp->soft_iblock_cookie) != DDI_SUCCESS) { 358fd358c0dSmyers cmn_err(CE_WARN, "power_attach: ddi_get_soft_iblock_cookie " 359fd358c0dSmyers "failed."); 360fd358c0dSmyers mutex_destroy(&softsp->power_intr_mutex); 361fd358c0dSmyers ddi_remove_intr(dip, 0, NULL); 362fd358c0dSmyers goto error; 363fd358c0dSmyers } 364fd358c0dSmyers 365fd358c0dSmyers mutex_init(&softsp->power_mutex, NULL, MUTEX_DRIVER, 366fd358c0dSmyers (void *)softsp->soft_iblock_cookie); 367fd358c0dSmyers 368fd358c0dSmyers if (ddi_add_softintr(dip, DDI_SOFTINT_LOW, &softsp->softintr_id, 369fd358c0dSmyers NULL, NULL, power_soft_intr, (caddr_t)softsp) != DDI_SUCCESS) { 370fd358c0dSmyers cmn_err(CE_WARN, "power_attach: failed to add soft " 371fd358c0dSmyers "interrupt handler."); 372fd358c0dSmyers mutex_destroy(&softsp->power_mutex); 373fd358c0dSmyers mutex_destroy(&softsp->power_intr_mutex); 374fd358c0dSmyers ddi_remove_intr(dip, 0, NULL); 375fd358c0dSmyers goto error; 376fd358c0dSmyers } 377fd358c0dSmyers 378fd358c0dSmyers ddi_report_dev(dip); 379fd358c0dSmyers 380fd358c0dSmyers return (DDI_SUCCESS); 381fd358c0dSmyers 382fd358c0dSmyers error: 383fd358c0dSmyers #ifdef ACPI_POWER_BUTTON 384fd358c0dSmyers /* 385fd358c0dSmyers * detach ACPI power button 386fd358c0dSmyers */ 387fd358c0dSmyers power_detach_acpi(softsp); 388fd358c0dSmyers #else 389fd358c0dSmyers power_free_regs(softsp); 390fd358c0dSmyers #endif /* ACPI_POWER_BUTTON */ 391fd358c0dSmyers ddi_remove_minor_node(dip, "power_button"); 392fd358c0dSmyers ddi_soft_state_free(power_state, power_inst); 393fd358c0dSmyers return (DDI_FAILURE); 394fd358c0dSmyers } 395fd358c0dSmyers 396fd358c0dSmyers /*ARGSUSED*/ 397fd358c0dSmyers /* 398fd358c0dSmyers * This driver doesn't detach. 399fd358c0dSmyers */ 400fd358c0dSmyers static int 401fd358c0dSmyers power_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 402fd358c0dSmyers { 403fd358c0dSmyers /* 404fd358c0dSmyers * Since the "power" node has "reg" property, as part of 405fd358c0dSmyers * the suspend operation, detach(9E) entry point is called. 406fd358c0dSmyers * There is no state to save, since this register is used 407fd358c0dSmyers * by OBP to power off the system and the state of the 408fd358c0dSmyers * power off is preserved by hardware. 409fd358c0dSmyers */ 410fd358c0dSmyers return ((cmd == DDI_SUSPEND) ? DDI_SUCCESS : 411fd358c0dSmyers DDI_FAILURE); 412fd358c0dSmyers } 413fd358c0dSmyers 414*d58fda43Sjbeloro 415fd358c0dSmyers #ifndef ACPI_POWER_BUTTON 416fd358c0dSmyers /* 417fd358c0dSmyers * Handler for the high-level interrupt. 418fd358c0dSmyers */ 419fd358c0dSmyers static uint_t 420fd358c0dSmyers power_high_intr(caddr_t arg) 421fd358c0dSmyers { 422fd358c0dSmyers struct power_soft_state *softsp = (struct power_soft_state *)arg; 423fd358c0dSmyers ddi_acc_handle_t hdl = softsp->power_rhandle; 424fd358c0dSmyers uint8_t reg; 425fd358c0dSmyers 426fd358c0dSmyers hrtime_t tstamp; 427fd358c0dSmyers static hrtime_t o_tstamp = 0; 428fd358c0dSmyers static hrtime_t power_button_tstamp = 0; 429fd358c0dSmyers static int power_button_cnt; 430fd358c0dSmyers 431fd358c0dSmyers if (softsp->power_regs_mapped) { 432fd358c0dSmyers mutex_enter(&softsp->power_intr_mutex); 433*d58fda43Sjbeloro 434*d58fda43Sjbeloro /* Check if power button interrupt is delivered by EPIC HW */ 435*d58fda43Sjbeloro if (hasEPIC) { 436*d58fda43Sjbeloro /* read isr - first issue command */ 437*d58fda43Sjbeloro EPIC_WR(hdl, softsp->power_btn_reg, 438*d58fda43Sjbeloro EPIC_ATOM_INTR_READ); 439*d58fda43Sjbeloro /* next, read the reg */ 440*d58fda43Sjbeloro EPIC_RD(hdl, softsp->power_btn_reg, reg); 441*d58fda43Sjbeloro 442*d58fda43Sjbeloro if (reg & EPIC_FIRE_INTERRUPT) { /* PB pressed */ 443*d58fda43Sjbeloro /* clear the interrupt */ 444*d58fda43Sjbeloro EPIC_WR(hdl, softsp->power_btn_reg, 445*d58fda43Sjbeloro EPIC_ATOM_INTR_CLEAR); 446*d58fda43Sjbeloro } else { 447*d58fda43Sjbeloro if (!softsp->power_btn_ioctl) { 448*d58fda43Sjbeloro mutex_exit(&softsp->power_intr_mutex); 449*d58fda43Sjbeloro return (DDI_INTR_CLAIMED); 450*d58fda43Sjbeloro } 451*d58fda43Sjbeloro softsp->power_btn_ioctl = B_FALSE; 452*d58fda43Sjbeloro } 453*d58fda43Sjbeloro } else { 454fd358c0dSmyers reg = ddi_get8(hdl, softsp->power_btn_reg); 455fd358c0dSmyers if (reg & softsp->power_btn_bit) { 456fd358c0dSmyers reg &= softsp->power_btn_bit; 457fd358c0dSmyers ddi_put8(hdl, softsp->power_btn_reg, reg); 458fd358c0dSmyers (void) ddi_get8(hdl, softsp->power_btn_reg); 459fd358c0dSmyers } else { 460fd358c0dSmyers if (!softsp->power_btn_ioctl) { 461fd358c0dSmyers mutex_exit(&softsp->power_intr_mutex); 462fd358c0dSmyers return (DDI_INTR_CLAIMED); 463fd358c0dSmyers } 464fd358c0dSmyers softsp->power_btn_ioctl = B_FALSE; 465fd358c0dSmyers } 466*d58fda43Sjbeloro } 467fd358c0dSmyers mutex_exit(&softsp->power_intr_mutex); 468fd358c0dSmyers } 469fd358c0dSmyers 470fd358c0dSmyers tstamp = gethrtime(); 471fd358c0dSmyers 472fd358c0dSmyers /* need to deal with power button debounce */ 473fd358c0dSmyers if (o_tstamp && (tstamp - o_tstamp) < power_button_debounce) { 474fd358c0dSmyers o_tstamp = tstamp; 475fd358c0dSmyers return (DDI_INTR_CLAIMED); 476fd358c0dSmyers } 477fd358c0dSmyers o_tstamp = tstamp; 478fd358c0dSmyers 479fd358c0dSmyers power_button_cnt++; 480fd358c0dSmyers 481fd358c0dSmyers mutex_enter(&softsp->power_intr_mutex); 482fd358c0dSmyers power_button_pressed++; 483fd358c0dSmyers mutex_exit(&softsp->power_intr_mutex); 484fd358c0dSmyers 485fd358c0dSmyers /* 486fd358c0dSmyers * If power button abort is enabled and power button was pressed 487fd358c0dSmyers * power_button_abort_presses times within power_button_abort_interval 488fd358c0dSmyers * then call abort_sequence_enter(); 489fd358c0dSmyers */ 490fd358c0dSmyers if (power_button_abort_enable) { 491fd358c0dSmyers if (power_button_abort_presses == 1 || 492fd358c0dSmyers tstamp < (power_button_tstamp + 493fd358c0dSmyers power_button_abort_interval)) { 494fd358c0dSmyers if (power_button_cnt == power_button_abort_presses) { 495fd358c0dSmyers mutex_enter(&softsp->power_intr_mutex); 496fd358c0dSmyers power_button_cancel += power_button_timeouts; 497fd358c0dSmyers power_button_pressed = 0; 498fd358c0dSmyers mutex_exit(&softsp->power_intr_mutex); 499fd358c0dSmyers power_button_cnt = 0; 500fd358c0dSmyers abort_sequence_enter("Power Button Abort"); 501fd358c0dSmyers return (DDI_INTR_CLAIMED); 502fd358c0dSmyers } 503fd358c0dSmyers } else { 504fd358c0dSmyers power_button_cnt = 1; 505fd358c0dSmyers power_button_tstamp = tstamp; 506fd358c0dSmyers } 507fd358c0dSmyers } 508fd358c0dSmyers 509fd358c0dSmyers if (!power_button_enable) 510fd358c0dSmyers return (DDI_INTR_CLAIMED); 511fd358c0dSmyers 512fd358c0dSmyers /* post softint to issue timeout for power button action */ 513fd358c0dSmyers if (softsp->softintr_id != NULL) 514fd358c0dSmyers ddi_trigger_softintr(softsp->softintr_id); 515fd358c0dSmyers 516fd358c0dSmyers return (DDI_INTR_CLAIMED); 517fd358c0dSmyers } 518fd358c0dSmyers #endif /* ifndef ACPI_POWER_BUTTON */ 519fd358c0dSmyers 520fd358c0dSmyers /* 521fd358c0dSmyers * Handle the softints.... 522fd358c0dSmyers * 523fd358c0dSmyers * If only one softint is posted for several button presses, record 524fd358c0dSmyers * the number of additional presses just incase this was actually not quite 525fd358c0dSmyers * an Abort sequence so that we can log this event later. 526fd358c0dSmyers * 527fd358c0dSmyers * Issue a timeout with a duration being a fraction larger than 528fd358c0dSmyers * the specified Abort interval inorder to perform a power down if required. 529fd358c0dSmyers */ 530fd358c0dSmyers static uint_t 531fd358c0dSmyers power_soft_intr(caddr_t arg) 532fd358c0dSmyers { 533fd358c0dSmyers struct power_soft_state *softsp = (struct power_soft_state *)arg; 534fd358c0dSmyers 535fd358c0dSmyers if (!power_button_abort_enable) 536fd358c0dSmyers return (power_issue_shutdown(arg)); 537fd358c0dSmyers 538fd358c0dSmyers mutex_enter(&softsp->power_intr_mutex); 539fd358c0dSmyers if (!power_button_pressed) { 540fd358c0dSmyers mutex_exit(&softsp->power_intr_mutex); 541fd358c0dSmyers return (DDI_INTR_CLAIMED); 542fd358c0dSmyers } 543fd358c0dSmyers 544fd358c0dSmyers /* 545fd358c0dSmyers * Schedule a timeout to do the necessary 546fd358c0dSmyers * work for shutdown, only one timeout for 547fd358c0dSmyers * n presses if power button was pressed 548fd358c0dSmyers * more than once before softint fired 549fd358c0dSmyers */ 550fd358c0dSmyers if (power_button_pressed > 1) 551fd358c0dSmyers additional_presses += power_button_pressed - 1; 552fd358c0dSmyers 553fd358c0dSmyers timeout_cancel = 0; 554fd358c0dSmyers power_button_pressed = 0; 555fd358c0dSmyers power_button_timeouts++; 556fd358c0dSmyers mutex_exit(&softsp->power_intr_mutex); 557fd358c0dSmyers (void) timeout((void(*)(void *))power_timeout, 558fd358c0dSmyers softsp, NSEC_TO_TICK(power_button_abort_interval) + 559fd358c0dSmyers ABORT_INCREMENT_DELAY); 560fd358c0dSmyers 561fd358c0dSmyers return (DDI_INTR_CLAIMED); 562fd358c0dSmyers } 563fd358c0dSmyers 564fd358c0dSmyers /* 565fd358c0dSmyers * Upon receiving a timeout the following is determined: 566fd358c0dSmyers * 567fd358c0dSmyers * If an Abort sequence was issued, then we cancel all outstanding timeouts 568fd358c0dSmyers * and additional presses prior to the Abort sequence. 569fd358c0dSmyers * 570fd358c0dSmyers * If we had multiple timeouts issued and the abort sequence was not met, 571fd358c0dSmyers * then we had more than one button press to power down the machine. We 572fd358c0dSmyers * were probably trying to issue an abort. So log a message indicating this 573fd358c0dSmyers * and cancel all outstanding timeouts. 574fd358c0dSmyers * 575fd358c0dSmyers * If we had just one timeout and the abort sequence was not met then 576fd358c0dSmyers * we really did want to power down the machine, so call power_issue_shutdown() 577fd358c0dSmyers * to do the work and schedule a power down 578fd358c0dSmyers */ 579fd358c0dSmyers static void 580fd358c0dSmyers power_timeout(caddr_t arg) 581fd358c0dSmyers { 582fd358c0dSmyers struct power_soft_state *softsp = (struct power_soft_state *)arg; 583fd358c0dSmyers static int first = 0; 584fd358c0dSmyers 585fd358c0dSmyers /* 586fd358c0dSmyers * Abort was generated cancel all outstanding power 587fd358c0dSmyers * button timeouts 588fd358c0dSmyers */ 589fd358c0dSmyers mutex_enter(&softsp->power_intr_mutex); 590fd358c0dSmyers if (power_button_cancel) { 591fd358c0dSmyers power_button_cancel--; 592fd358c0dSmyers power_button_timeouts--; 593fd358c0dSmyers if (!first) { 594fd358c0dSmyers first++; 595fd358c0dSmyers additional_presses = 0; 596fd358c0dSmyers } 597fd358c0dSmyers mutex_exit(&softsp->power_intr_mutex); 598fd358c0dSmyers return; 599fd358c0dSmyers } 600fd358c0dSmyers first = 0; 601fd358c0dSmyers 602fd358c0dSmyers /* 603fd358c0dSmyers * We get here if the timeout(s) have fired and they were 604fd358c0dSmyers * not issued prior to an abort. 605fd358c0dSmyers * 606fd358c0dSmyers * If we had more than one press in the interval we were 607fd358c0dSmyers * probably trying to issue an abort, but didnt press the 608fd358c0dSmyers * required number within the interval. Hence cancel all 609fd358c0dSmyers * timeouts and do not continue towards shutdown. 610fd358c0dSmyers */ 611fd358c0dSmyers if (!timeout_cancel) { 612fd358c0dSmyers timeout_cancel = power_button_timeouts + 613fd358c0dSmyers additional_presses; 614fd358c0dSmyers 615fd358c0dSmyers power_button_timeouts--; 616fd358c0dSmyers if (!power_button_timeouts) 617fd358c0dSmyers additional_presses = 0; 618fd358c0dSmyers 619fd358c0dSmyers if (timeout_cancel > 1) { 620fd358c0dSmyers mutex_exit(&softsp->power_intr_mutex); 621fd358c0dSmyers cmn_err(CE_NOTE, "Power Button pressed " 622fd358c0dSmyers "%d times, cancelling all requests", 623fd358c0dSmyers timeout_cancel); 624fd358c0dSmyers return; 625fd358c0dSmyers } 626fd358c0dSmyers mutex_exit(&softsp->power_intr_mutex); 627fd358c0dSmyers 628fd358c0dSmyers /* Go and do the work to request shutdown */ 629fd358c0dSmyers (void) power_issue_shutdown((caddr_t)softsp); 630fd358c0dSmyers return; 631fd358c0dSmyers } 632fd358c0dSmyers 633fd358c0dSmyers power_button_timeouts--; 634fd358c0dSmyers if (!power_button_timeouts) 635fd358c0dSmyers additional_presses = 0; 636fd358c0dSmyers mutex_exit(&softsp->power_intr_mutex); 637fd358c0dSmyers } 638fd358c0dSmyers 639fd358c0dSmyers #ifdef ACPI_POWER_BUTTON 640fd358c0dSmyers static void 641fd358c0dSmyers do_shutdown(void) 642fd358c0dSmyers { 643fd358c0dSmyers proc_t *initpp; 644fd358c0dSmyers 645fd358c0dSmyers /* 646fd358c0dSmyers * If we're still booting and init(1) isn't set up yet, simply halt. 647fd358c0dSmyers */ 648fd358c0dSmyers mutex_enter(&pidlock); 649fd358c0dSmyers initpp = prfind(P_INITPID); 650fd358c0dSmyers mutex_exit(&pidlock); 651fd358c0dSmyers if (initpp == NULL) { 652fd358c0dSmyers extern void halt(char *); 653fd358c0dSmyers halt("Power off the System"); /* just in case */ 654fd358c0dSmyers } 655fd358c0dSmyers 656fd358c0dSmyers /* 657fd358c0dSmyers * else, graceful shutdown with inittab and all getting involved 658fd358c0dSmyers */ 659fd358c0dSmyers psignal(initpp, SIGPWR); 660fd358c0dSmyers } 661fd358c0dSmyers #endif 662fd358c0dSmyers 663fd358c0dSmyers static uint_t 664fd358c0dSmyers power_issue_shutdown(caddr_t arg) 665fd358c0dSmyers { 666fd358c0dSmyers struct power_soft_state *softsp = (struct power_soft_state *)arg; 667fd358c0dSmyers 668fd358c0dSmyers mutex_enter(&softsp->power_mutex); 669fd358c0dSmyers softsp->events |= PB_BUTTON_PRESS; 670fd358c0dSmyers if (softsp->monitor_on != 0) { 671fd358c0dSmyers mutex_exit(&softsp->power_mutex); 672fd358c0dSmyers pollwakeup(&softsp->pollhd, POLLRDNORM); 673fd358c0dSmyers pollwakeup(&softsp->pollhd, POLLIN); 674fd358c0dSmyers return (DDI_INTR_CLAIMED); 675fd358c0dSmyers } 676fd358c0dSmyers 677fd358c0dSmyers if (!softsp->shutdown_pending) { 678fd358c0dSmyers cmn_err(CE_WARN, "Power off requested from power button or " 679fd358c0dSmyers "SC, powering down the system!"); 680fd358c0dSmyers softsp->shutdown_pending = 1; 681fd358c0dSmyers do_shutdown(); 682fd358c0dSmyers 683fd358c0dSmyers /* 684fd358c0dSmyers * Wait a while for "do_shutdown()" to shut down the system 685fd358c0dSmyers * before logging an error message. 686fd358c0dSmyers */ 687fd358c0dSmyers (void) timeout((void(*)(void *))power_log_message, NULL, 688fd358c0dSmyers 100 * hz); 689fd358c0dSmyers } 690fd358c0dSmyers mutex_exit(&softsp->power_mutex); 691fd358c0dSmyers 692fd358c0dSmyers return (DDI_INTR_CLAIMED); 693fd358c0dSmyers } 694fd358c0dSmyers 695fd358c0dSmyers /* 696fd358c0dSmyers * Open the device. 697fd358c0dSmyers */ 698fd358c0dSmyers /*ARGSUSED*/ 699fd358c0dSmyers static int 700fd358c0dSmyers power_open(dev_t *devp, int openflags, int otyp, cred_t *credp) 701fd358c0dSmyers { 702fd358c0dSmyers struct power_soft_state *softsp; 703fd358c0dSmyers int clone; 704fd358c0dSmyers 705fd358c0dSmyers if (otyp != OTYP_CHR) 706fd358c0dSmyers return (EINVAL); 707fd358c0dSmyers 708fd358c0dSmyers if ((softsp = ddi_get_soft_state(power_state, power_inst)) == 709fd358c0dSmyers NULL) 710fd358c0dSmyers return (ENXIO); 711fd358c0dSmyers 712fd358c0dSmyers mutex_enter(&softsp->power_mutex); 713fd358c0dSmyers for (clone = 1; clone < POWER_MAX_CLONE; clone++) 714fd358c0dSmyers if (!softsp->clones[clone]) 715fd358c0dSmyers break; 716fd358c0dSmyers 717fd358c0dSmyers if (clone == POWER_MAX_CLONE) { 718fd358c0dSmyers cmn_err(CE_WARN, "power_open: No more allocation left " 719fd358c0dSmyers "to create a clone minor."); 720fd358c0dSmyers mutex_exit(&softsp->power_mutex); 721fd358c0dSmyers return (ENXIO); 722fd358c0dSmyers } 723fd358c0dSmyers 724fd358c0dSmyers *devp = makedevice(getmajor(*devp), (power_inst << 8) + clone); 725fd358c0dSmyers softsp->clones[clone] = 1; 726fd358c0dSmyers mutex_exit(&softsp->power_mutex); 727fd358c0dSmyers 728fd358c0dSmyers return (0); 729fd358c0dSmyers } 730fd358c0dSmyers 731fd358c0dSmyers /* 732fd358c0dSmyers * Close the device. 733fd358c0dSmyers */ 734fd358c0dSmyers /*ARGSUSED*/ 735fd358c0dSmyers static int 736fd358c0dSmyers power_close(dev_t dev, int openflags, int otyp, cred_t *credp) 737fd358c0dSmyers { 738fd358c0dSmyers struct power_soft_state *softsp; 739fd358c0dSmyers int clone; 740fd358c0dSmyers 741fd358c0dSmyers if (otyp != OTYP_CHR) 742fd358c0dSmyers return (EINVAL); 743fd358c0dSmyers 744fd358c0dSmyers if ((softsp = ddi_get_soft_state(power_state, power_inst)) == 745fd358c0dSmyers NULL) 746fd358c0dSmyers return (ENXIO); 747fd358c0dSmyers 748fd358c0dSmyers clone = POWER_MINOR_TO_CLONE(getminor(dev)); 749fd358c0dSmyers mutex_enter(&softsp->power_mutex); 750fd358c0dSmyers if (softsp->monitor_on == clone) 751fd358c0dSmyers softsp->monitor_on = 0; 752fd358c0dSmyers softsp->clones[clone] = 0; 753fd358c0dSmyers mutex_exit(&softsp->power_mutex); 754fd358c0dSmyers 755fd358c0dSmyers return (0); 756fd358c0dSmyers } 757fd358c0dSmyers 758fd358c0dSmyers /*ARGSUSED*/ 759fd358c0dSmyers static int 760fd358c0dSmyers power_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *cred_p, 761fd358c0dSmyers int *rval_p) 762fd358c0dSmyers { 763fd358c0dSmyers struct power_soft_state *softsp; 764fd358c0dSmyers int clone; 765fd358c0dSmyers 766fd358c0dSmyers if ((softsp = ddi_get_soft_state(power_state, power_inst)) == 767fd358c0dSmyers NULL) 768fd358c0dSmyers return (ENXIO); 769fd358c0dSmyers 770fd358c0dSmyers clone = POWER_MINOR_TO_CLONE(getminor(dev)); 771fd358c0dSmyers switch (cmd) { 772fd358c0dSmyers case PB_BEGIN_MONITOR: 773fd358c0dSmyers mutex_enter(&softsp->power_mutex); 774fd358c0dSmyers if (softsp->monitor_on) { 775fd358c0dSmyers mutex_exit(&softsp->power_mutex); 776fd358c0dSmyers return (EBUSY); 777fd358c0dSmyers } 778fd358c0dSmyers softsp->monitor_on = clone; 779fd358c0dSmyers mutex_exit(&softsp->power_mutex); 780fd358c0dSmyers return (0); 781fd358c0dSmyers 782fd358c0dSmyers case PB_END_MONITOR: 783fd358c0dSmyers mutex_enter(&softsp->power_mutex); 784fd358c0dSmyers 785fd358c0dSmyers /* 786fd358c0dSmyers * If PB_END_MONITOR is called without first 787fd358c0dSmyers * calling PB_BEGIN_MONITOR, an error will be 788fd358c0dSmyers * returned. 789fd358c0dSmyers */ 790fd358c0dSmyers if (!softsp->monitor_on) { 791fd358c0dSmyers mutex_exit(&softsp->power_mutex); 792fd358c0dSmyers return (ENXIO); 793fd358c0dSmyers } 794fd358c0dSmyers 795fd358c0dSmyers /* 796fd358c0dSmyers * This clone is not monitoring the button. 797fd358c0dSmyers */ 798fd358c0dSmyers if (softsp->monitor_on != clone) { 799fd358c0dSmyers mutex_exit(&softsp->power_mutex); 800fd358c0dSmyers return (EINVAL); 801fd358c0dSmyers } 802fd358c0dSmyers softsp->monitor_on = 0; 803fd358c0dSmyers mutex_exit(&softsp->power_mutex); 804fd358c0dSmyers return (0); 805fd358c0dSmyers 806fd358c0dSmyers case PB_GET_EVENTS: 807fd358c0dSmyers mutex_enter(&softsp->power_mutex); 808fd358c0dSmyers if (ddi_copyout((void *)&softsp->events, (void *)arg, 809fd358c0dSmyers sizeof (int), mode) != 0) { 810fd358c0dSmyers mutex_exit(&softsp->power_mutex); 811fd358c0dSmyers return (EFAULT); 812fd358c0dSmyers } 813fd358c0dSmyers 814fd358c0dSmyers /* 815fd358c0dSmyers * This ioctl returned the events detected since last 816fd358c0dSmyers * call. Note that any application can get the events 817fd358c0dSmyers * and clear the event register. 818fd358c0dSmyers */ 819fd358c0dSmyers softsp->events = 0; 820fd358c0dSmyers mutex_exit(&softsp->power_mutex); 821fd358c0dSmyers return (0); 822fd358c0dSmyers 823fd358c0dSmyers /* 824fd358c0dSmyers * This ioctl is used by the test suite. 825fd358c0dSmyers */ 826fd358c0dSmyers case PB_CREATE_BUTTON_EVENT: 827fd358c0dSmyers #ifdef ACPI_POWER_BUTTON 828fd358c0dSmyers (UINT32)power_acpi_fixed_event((void *)softsp); 829fd358c0dSmyers #else 830fd358c0dSmyers if (softsp->power_regs_mapped) { 831fd358c0dSmyers mutex_enter(&softsp->power_intr_mutex); 832fd358c0dSmyers softsp->power_btn_ioctl = B_TRUE; 833fd358c0dSmyers mutex_exit(&softsp->power_intr_mutex); 834fd358c0dSmyers } 835fd358c0dSmyers (void) power_high_intr((caddr_t)softsp); 836fd358c0dSmyers #endif /* ACPI_POWER_BUTTON */ 837fd358c0dSmyers return (0); 838fd358c0dSmyers 839fd358c0dSmyers default: 840fd358c0dSmyers return (ENOTTY); 841fd358c0dSmyers } 842fd358c0dSmyers } 843fd358c0dSmyers 844fd358c0dSmyers /*ARGSUSED*/ 845fd358c0dSmyers static int 846fd358c0dSmyers power_chpoll(dev_t dev, short events, int anyyet, 847fd358c0dSmyers short *reventsp, struct pollhead **phpp) 848fd358c0dSmyers { 849fd358c0dSmyers struct power_soft_state *softsp; 850fd358c0dSmyers 851fd358c0dSmyers if ((softsp = ddi_get_soft_state(power_state, power_inst)) == NULL) 852fd358c0dSmyers return (ENXIO); 853fd358c0dSmyers 854fd358c0dSmyers mutex_enter(&softsp->power_mutex); 855fd358c0dSmyers *reventsp = 0; 856fd358c0dSmyers if (softsp->events) 857fd358c0dSmyers *reventsp = POLLRDNORM|POLLIN; 858fd358c0dSmyers else { 859fd358c0dSmyers if (!anyyet) 860fd358c0dSmyers *phpp = &softsp->pollhd; 861fd358c0dSmyers } 862fd358c0dSmyers mutex_exit(&softsp->power_mutex); 863fd358c0dSmyers 864fd358c0dSmyers return (0); 865fd358c0dSmyers } 866fd358c0dSmyers 867fd358c0dSmyers static void 868fd358c0dSmyers power_log_message(void) 869fd358c0dSmyers { 870fd358c0dSmyers struct power_soft_state *softsp; 871fd358c0dSmyers 872fd358c0dSmyers if ((softsp = ddi_get_soft_state(power_state, power_inst)) == NULL) { 873fd358c0dSmyers cmn_err(CE_WARN, "Failed to get internal state!"); 874fd358c0dSmyers return; 875fd358c0dSmyers } 876fd358c0dSmyers 877fd358c0dSmyers mutex_enter(&softsp->power_mutex); 878fd358c0dSmyers softsp->shutdown_pending = 0; 879fd358c0dSmyers cmn_err(CE_WARN, "Failed to shut down the system!"); 880fd358c0dSmyers mutex_exit(&softsp->power_mutex); 881fd358c0dSmyers } 882fd358c0dSmyers 883fd358c0dSmyers #ifdef ACPI_POWER_BUTTON 884fd358c0dSmyers /* 885fd358c0dSmyers * Given a handle to a device object, locate a _PRW object 886fd358c0dSmyers * if present and fetch the GPE info for this device object 887fd358c0dSmyers */ 888fd358c0dSmyers static ACPI_STATUS 889fd358c0dSmyers power_get_prw_gpe(ACPI_HANDLE dev, ACPI_HANDLE *gpe_dev, UINT32 *gpe_num) 890fd358c0dSmyers { 891fd358c0dSmyers ACPI_BUFFER buf; 892fd358c0dSmyers ACPI_STATUS status; 893fd358c0dSmyers ACPI_HANDLE prw; 894fd358c0dSmyers ACPI_OBJECT *gpe; 895fd358c0dSmyers 896fd358c0dSmyers /* 897fd358c0dSmyers * Evaluate _PRW if present 898fd358c0dSmyers */ 899fd358c0dSmyers status = AcpiGetHandle(dev, "_PRW", &prw); 900fd358c0dSmyers if (status != AE_OK) 901fd358c0dSmyers return (status); 902fd358c0dSmyers buf.Length = ACPI_ALLOCATE_BUFFER; 903fd358c0dSmyers status = AcpiEvaluateObjectTyped(prw, NULL, NULL, &buf, 904fd358c0dSmyers ACPI_TYPE_PACKAGE); 905fd358c0dSmyers if (status != AE_OK) 906fd358c0dSmyers return (status); 907fd358c0dSmyers 908fd358c0dSmyers /* 909fd358c0dSmyers * Sanity-check the package; need at least two elements 910fd358c0dSmyers */ 911fd358c0dSmyers status = AE_ERROR; 912fd358c0dSmyers if (((ACPI_OBJECT *)buf.Pointer)->Package.Count < 2) 913fd358c0dSmyers goto done; 914fd358c0dSmyers 915fd358c0dSmyers gpe = &((ACPI_OBJECT *)buf.Pointer)->Package.Elements[0]; 916fd358c0dSmyers if (gpe->Type == ACPI_TYPE_INTEGER) { 917fd358c0dSmyers *gpe_dev = NULL; 918fd358c0dSmyers *gpe_num = gpe->Integer.Value; 919fd358c0dSmyers status = AE_OK; 920fd358c0dSmyers } else if (gpe->Type == ACPI_TYPE_PACKAGE) { 921fd358c0dSmyers if ((gpe->Package.Count != 2) || 922fd358c0dSmyers (gpe->Package.Elements[0].Type != ACPI_TYPE_DEVICE) || 923fd358c0dSmyers (gpe->Package.Elements[1].Type != ACPI_TYPE_INTEGER)) 924fd358c0dSmyers goto done; 925fd358c0dSmyers *gpe_dev = gpe->Package.Elements[0].Reference.Handle; 926fd358c0dSmyers *gpe_num = gpe->Package.Elements[1].Integer.Value; 927fd358c0dSmyers status = AE_OK; 928fd358c0dSmyers } 929fd358c0dSmyers 930fd358c0dSmyers done: 931fd358c0dSmyers AcpiOsFree(buf.Pointer); 932fd358c0dSmyers return (status); 933fd358c0dSmyers } 934fd358c0dSmyers 935fd358c0dSmyers 936fd358c0dSmyers /* 937fd358c0dSmyers * 938fd358c0dSmyers */ 9399f43a0a2Smyers /*ARGSUSED*/ 940fd358c0dSmyers static ACPI_STATUS 941fd358c0dSmyers acpi_device(ACPI_HANDLE obj, UINT32 nesting, void *context, void **rv) 942fd358c0dSmyers { 9439f43a0a2Smyers 944fd358c0dSmyers *((ACPI_HANDLE *)context) = obj; 945fd358c0dSmyers return (AE_OK); 946fd358c0dSmyers } 947fd358c0dSmyers 948fd358c0dSmyers /* 949fd358c0dSmyers * 950fd358c0dSmyers */ 951fd358c0dSmyers static ACPI_HANDLE 952fd358c0dSmyers probe_acpi_pwrbutton() 953fd358c0dSmyers { 954fd358c0dSmyers ACPI_HANDLE obj = NULL; 955fd358c0dSmyers 9569f43a0a2Smyers (void) AcpiGetDevices("PNP0C0C", acpi_device, (void *)&obj, NULL); 957fd358c0dSmyers return (obj); 958fd358c0dSmyers } 959fd358c0dSmyers 960fd358c0dSmyers static UINT32 961fd358c0dSmyers power_acpi_fixed_event(void *ctx) 962fd358c0dSmyers { 963fd358c0dSmyers 964fd358c0dSmyers mutex_enter(&((struct power_soft_state *)ctx)->power_intr_mutex); 965fd358c0dSmyers power_button_pressed++; 966fd358c0dSmyers mutex_exit(&((struct power_soft_state *)ctx)->power_intr_mutex); 967fd358c0dSmyers 968fd358c0dSmyers /* post softint to issue timeout for power button action */ 969fd358c0dSmyers if (((struct power_soft_state *)ctx)->softintr_id != NULL) 970fd358c0dSmyers ddi_trigger_softintr( 971fd358c0dSmyers ((struct power_soft_state *)ctx)->softintr_id); 972fd358c0dSmyers 973fd358c0dSmyers return (AE_OK); 974fd358c0dSmyers } 975fd358c0dSmyers 9769f43a0a2Smyers /*ARGSUSED*/ 977fd358c0dSmyers static void 978fd358c0dSmyers power_acpi_notify_event(ACPI_HANDLE obj, UINT32 val, void *ctx) 979fd358c0dSmyers { 980fd358c0dSmyers if (val == 0x80) 9819f43a0a2Smyers (void) power_acpi_fixed_event(ctx); 982fd358c0dSmyers } 983fd358c0dSmyers 984fd358c0dSmyers /* 985fd358c0dSmyers * 986fd358c0dSmyers */ 987fd358c0dSmyers static int 988fd358c0dSmyers power_probe_method_button(struct power_soft_state *softsp) 989fd358c0dSmyers { 990fd358c0dSmyers ACPI_HANDLE button_obj; 991fd358c0dSmyers UINT32 gpe_num; 992fd358c0dSmyers ACPI_HANDLE gpe_dev; 993fd358c0dSmyers 994fd358c0dSmyers button_obj = probe_acpi_pwrbutton(); 995fd358c0dSmyers softsp->button_obj = button_obj; /* remember obj */ 996fd358c0dSmyers if ((button_obj != NULL) && 997fd358c0dSmyers (power_get_prw_gpe(button_obj, &gpe_dev, &gpe_num) == AE_OK) && 998fd358c0dSmyers (AcpiSetGpeType(gpe_dev, gpe_num, ACPI_GPE_TYPE_WAKE_RUN) == 999fd358c0dSmyers AE_OK) && 1000fd358c0dSmyers (AcpiEnableGpe(gpe_dev, gpe_num, ACPI_NOT_ISR) == AE_OK) && 1001fd358c0dSmyers (AcpiInstallNotifyHandler(button_obj, ACPI_DEVICE_NOTIFY, 1002fd358c0dSmyers power_acpi_notify_event, (void*)softsp) == AE_OK)) 1003fd358c0dSmyers return (1); 1004fd358c0dSmyers return (0); 1005fd358c0dSmyers } 1006fd358c0dSmyers 1007fd358c0dSmyers /* 1008fd358c0dSmyers * 1009fd358c0dSmyers */ 1010fd358c0dSmyers static int 1011fd358c0dSmyers power_probe_fixed_button(struct power_soft_state *softsp) 1012fd358c0dSmyers { 1013fd358c0dSmyers FADT_DESCRIPTOR *fadt; 1014fd358c0dSmyers 1015fd358c0dSmyers if (AcpiGetFirmwareTable(FADT_SIG, 1, ACPI_LOGICAL_ADDRESSING, 1016fd358c0dSmyers (ACPI_TABLE_HEADER **) &fadt) != AE_OK) 1017fd358c0dSmyers return (0); 1018fd358c0dSmyers 1019fd358c0dSmyers if (!fadt->PwrButton) { 1020fd358c0dSmyers if (AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON, 1021fd358c0dSmyers power_acpi_fixed_event, (void *)softsp) == AE_OK) 1022fd358c0dSmyers return (1); 1023fd358c0dSmyers } 1024fd358c0dSmyers return (0); 1025fd358c0dSmyers } 1026fd358c0dSmyers 1027fd358c0dSmyers 1028fd358c0dSmyers /* 1029fd358c0dSmyers * 1030fd358c0dSmyers */ 1031fd358c0dSmyers static int 1032fd358c0dSmyers power_attach_acpi(struct power_soft_state *softsp) 1033fd358c0dSmyers { 1034fd358c0dSmyers 1035fd358c0dSmyers /* 1036fd358c0dSmyers * If we've attached anything already, return an error 1037fd358c0dSmyers */ 1038fd358c0dSmyers if ((softsp->gpe_attached) || (softsp->fixed_attached)) 1039fd358c0dSmyers return (DDI_FAILURE); 1040fd358c0dSmyers 1041fd358c0dSmyers /* 1042fd358c0dSmyers * attempt to attach both a fixed-event handler and a GPE 1043fd358c0dSmyers * handler; remember what we got 1044fd358c0dSmyers */ 1045fd358c0dSmyers softsp->fixed_attached = (power_probe_fixed_button(softsp) != 0); 1046fd358c0dSmyers softsp->gpe_attached = (power_probe_method_button(softsp) != 0); 1047fd358c0dSmyers 1048fd358c0dSmyers /* 1049fd358c0dSmyers * If we've attached anything now, return success 1050fd358c0dSmyers */ 1051fd358c0dSmyers if ((softsp->gpe_attached) || (softsp->fixed_attached)) 1052fd358c0dSmyers return (DDI_SUCCESS); 1053fd358c0dSmyers 1054fd358c0dSmyers return (DDI_FAILURE); 1055fd358c0dSmyers } 1056fd358c0dSmyers 1057fd358c0dSmyers /* 1058fd358c0dSmyers * 1059fd358c0dSmyers */ 1060fd358c0dSmyers static void 1061fd358c0dSmyers power_detach_acpi(struct power_soft_state *softsp) 1062fd358c0dSmyers { 1063fd358c0dSmyers if (softsp->gpe_attached) { 1064fd358c0dSmyers if (AcpiRemoveNotifyHandler(softsp->button_obj, 1065fd358c0dSmyers ACPI_DEVICE_NOTIFY, power_acpi_notify_event) != AE_OK) 1066fd358c0dSmyers cmn_err(CE_WARN, "!power: failed to remove Notify" 1067fd358c0dSmyers " handler"); 1068fd358c0dSmyers } 1069fd358c0dSmyers 1070fd358c0dSmyers if (softsp->fixed_attached) { 1071fd358c0dSmyers if (AcpiRemoveFixedEventHandler(ACPI_EVENT_POWER_BUTTON, 1072fd358c0dSmyers power_acpi_fixed_event) != AE_OK) 1073fd358c0dSmyers cmn_err(CE_WARN, "!power: failed to remove Power" 1074fd358c0dSmyers " Button handler"); 1075fd358c0dSmyers } 1076fd358c0dSmyers } 1077fd358c0dSmyers 1078fd358c0dSmyers #else 1079fd358c0dSmyers /* 1080*d58fda43Sjbeloro * Code for platforms that have EPIC processor for processing power 1081*d58fda43Sjbeloro * button interrupts. 1082*d58fda43Sjbeloro */ 1083*d58fda43Sjbeloro static int 1084*d58fda43Sjbeloro power_setup_epic_regs(dev_info_t *dip, struct power_soft_state *softsp) 1085*d58fda43Sjbeloro { 1086*d58fda43Sjbeloro ddi_device_acc_attr_t attr; 1087*d58fda43Sjbeloro uint8_t *reg_base; 1088*d58fda43Sjbeloro 1089*d58fda43Sjbeloro attr.devacc_attr_version = DDI_DEVICE_ATTR_V0; 1090*d58fda43Sjbeloro attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC; 1091*d58fda43Sjbeloro attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC; 1092*d58fda43Sjbeloro if (ddi_regs_map_setup(dip, 0, (caddr_t *)®_base, 1093*d58fda43Sjbeloro EPIC_REGS_OFFSET, EPIC_REGS_LEN, &attr, 1094*d58fda43Sjbeloro &softsp->power_rhandle) != DDI_SUCCESS) { 1095*d58fda43Sjbeloro return (DDI_FAILURE); 1096*d58fda43Sjbeloro } 1097*d58fda43Sjbeloro 1098*d58fda43Sjbeloro softsp->power_btn_reg = reg_base; 1099*d58fda43Sjbeloro softsp->power_regs_mapped = B_TRUE; 1100*d58fda43Sjbeloro 1101*d58fda43Sjbeloro /* Clear power button interrupt first */ 1102*d58fda43Sjbeloro EPIC_WR(softsp->power_rhandle, softsp->power_btn_reg, 1103*d58fda43Sjbeloro EPIC_ATOM_INTR_CLEAR); 1104*d58fda43Sjbeloro 1105*d58fda43Sjbeloro /* Enable EPIC interrupt for power button single press event */ 1106*d58fda43Sjbeloro EPIC_WR(softsp->power_rhandle, softsp->power_btn_reg, 1107*d58fda43Sjbeloro EPIC_ATOM_INTR_ENABLE); 1108*d58fda43Sjbeloro 1109*d58fda43Sjbeloro /* 1110*d58fda43Sjbeloro * At this point, EPIC interrupt processing is fully initialised. 1111*d58fda43Sjbeloro */ 1112*d58fda43Sjbeloro hasEPIC = B_TRUE; 1113*d58fda43Sjbeloro return (DDI_SUCCESS); 1114*d58fda43Sjbeloro } 1115*d58fda43Sjbeloro 1116*d58fda43Sjbeloro /* 1117*d58fda43Sjbeloro * 1118fd358c0dSmyers * power button register definitions for acpi register on m1535d 1119fd358c0dSmyers */ 1120fd358c0dSmyers #define M1535D_PWR_BTN_REG_01 0x1 1121fd358c0dSmyers #define M1535D_PWR_BTN_EVENT_FLAG 0x1 1122fd358c0dSmyers 1123fd358c0dSmyers static int 1124fd358c0dSmyers power_setup_m1535_regs(dev_info_t *dip, struct power_soft_state *softsp) 1125fd358c0dSmyers { 1126fd358c0dSmyers ddi_device_acc_attr_t attr; 1127fd358c0dSmyers uint8_t *reg_base; 1128fd358c0dSmyers 1129fd358c0dSmyers attr.devacc_attr_version = DDI_DEVICE_ATTR_V0; 1130fd358c0dSmyers attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC; 1131fd358c0dSmyers attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC; 1132fd358c0dSmyers if (ddi_regs_map_setup(dip, 0, (caddr_t *)®_base, 0, 0, &attr, 1133fd358c0dSmyers &softsp->power_rhandle) != DDI_SUCCESS) { 1134fd358c0dSmyers return (DDI_FAILURE); 1135fd358c0dSmyers } 1136fd358c0dSmyers softsp->power_btn_reg = ®_base[M1535D_PWR_BTN_REG_01]; 1137fd358c0dSmyers softsp->power_btn_bit = M1535D_PWR_BTN_EVENT_FLAG; 1138fd358c0dSmyers softsp->power_regs_mapped = B_TRUE; 1139fd358c0dSmyers return (DDI_SUCCESS); 1140fd358c0dSmyers } 1141fd358c0dSmyers 1142fd358c0dSmyers /* 1143*d58fda43Sjbeloro * MBC Fire/SSI Interrupt Status Register definitions 1144*d58fda43Sjbeloro */ 1145*d58fda43Sjbeloro #define FIRE_SSI_ISR 0x0 1146*d58fda43Sjbeloro #define FIRE_SSI_INTR_ENA 0x8 1147*d58fda43Sjbeloro #define FIRE_SSI_SHUTDOWN_REQ 0x4 1148*d58fda43Sjbeloro 1149*d58fda43Sjbeloro static int 1150*d58fda43Sjbeloro power_setup_mbc_regs(dev_info_t *dip, struct power_soft_state *softsp) 1151*d58fda43Sjbeloro { 1152*d58fda43Sjbeloro ddi_device_acc_attr_t attr; 1153*d58fda43Sjbeloro uint8_t *reg_base; 1154*d58fda43Sjbeloro ddi_acc_handle_t hdl; 1155*d58fda43Sjbeloro uint8_t reg; 1156*d58fda43Sjbeloro 1157*d58fda43Sjbeloro attr.devacc_attr_version = DDI_DEVICE_ATTR_V0; 1158*d58fda43Sjbeloro attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC; 1159*d58fda43Sjbeloro attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC; 1160*d58fda43Sjbeloro if (ddi_regs_map_setup(dip, 0, (caddr_t *)®_base, 0, 0, &attr, 1161*d58fda43Sjbeloro &softsp->power_rhandle) != DDI_SUCCESS) { 1162*d58fda43Sjbeloro return (DDI_FAILURE); 1163*d58fda43Sjbeloro } 1164*d58fda43Sjbeloro softsp->power_btn_reg = ®_base[FIRE_SSI_ISR]; 1165*d58fda43Sjbeloro softsp->power_btn_bit = FIRE_SSI_SHUTDOWN_REQ; 1166*d58fda43Sjbeloro /* 1167*d58fda43Sjbeloro * Enable MBC Fire Power Button interrupt. 1168*d58fda43Sjbeloro */ 1169*d58fda43Sjbeloro hdl = softsp->power_rhandle; 1170*d58fda43Sjbeloro reg = ddi_get8(hdl, ®_base[FIRE_SSI_INTR_ENA]); 1171*d58fda43Sjbeloro reg |= FIRE_SSI_SHUTDOWN_REQ; 1172*d58fda43Sjbeloro ddi_put8(hdl, ®_base[FIRE_SSI_INTR_ENA], reg); 1173*d58fda43Sjbeloro 1174*d58fda43Sjbeloro softsp->power_regs_mapped = B_TRUE; 1175*d58fda43Sjbeloro 1176*d58fda43Sjbeloro return (DDI_SUCCESS); 1177*d58fda43Sjbeloro } 1178*d58fda43Sjbeloro 1179*d58fda43Sjbeloro /* 1180fd358c0dSmyers * Setup register map for the power button 1181fd358c0dSmyers * NOTE:- we only map registers for platforms 1182fd358c0dSmyers * binding with the ali1535d+-power compatible 1183*d58fda43Sjbeloro * property or mbc-power or epic property. 1184fd358c0dSmyers */ 1185fd358c0dSmyers static int 1186fd358c0dSmyers power_setup_regs(struct power_soft_state *softsp) 1187fd358c0dSmyers { 1188fd358c0dSmyers char *binding_name; 1189fd358c0dSmyers 1190fd358c0dSmyers softsp->power_regs_mapped = B_FALSE; 1191fd358c0dSmyers softsp->power_btn_ioctl = B_FALSE; 1192fd358c0dSmyers binding_name = ddi_binding_name(softsp->dip); 1193*d58fda43Sjbeloro if (strcmp(binding_name, "mbc-power") == 0) 1194*d58fda43Sjbeloro return (power_setup_mbc_regs(softsp->dip, softsp)); 1195*d58fda43Sjbeloro else if (strcmp(binding_name, "SUNW,ebus-pic18lf65j10-power") == 0) 1196*d58fda43Sjbeloro return (power_setup_epic_regs(softsp->dip, softsp)); 1197*d58fda43Sjbeloro else if (strcmp(binding_name, "ali1535d+-power") == 0) 1198fd358c0dSmyers return (power_setup_m1535_regs(softsp->dip, softsp)); 1199fd358c0dSmyers 1200*d58fda43Sjbeloro /* 1201*d58fda43Sjbeloro * If the binding name is not one of these, that means there is no 1202*d58fda43Sjbeloro * additional HW and hence no extra processing is necessary. Just 1203*d58fda43Sjbeloro * return SUCCESS. 1204*d58fda43Sjbeloro */ 1205fd358c0dSmyers return (DDI_SUCCESS); 1206fd358c0dSmyers } 1207fd358c0dSmyers 1208fd358c0dSmyers static void 1209fd358c0dSmyers power_free_regs(struct power_soft_state *softsp) 1210fd358c0dSmyers { 1211fd358c0dSmyers if (softsp->power_regs_mapped) 1212fd358c0dSmyers ddi_regs_map_free(&softsp->power_rhandle); 1213fd358c0dSmyers } 1214fd358c0dSmyers #endif /* ACPI_POWER_BUTTON */ 1215