[205] | 1 | /**
|
---|
[161] | 2 | * apm.c - Functions to interface with the legacy APM driver, and suspend / resume functions.
|
---|
[84] | 3 | *
|
---|
[87] | 4 | * Copyright (c) 2011 thi.guten Software Development
|
---|
| 5 | * Copyright (c) 2011 Mensys B.V.
|
---|
[211] | 6 | * Copyright (c) 2013-2023 David Azarewicz <david@88watts.net>
|
---|
[87] | 7 | *
|
---|
| 8 | * Authors: Christian Mueller, Markus Thielen
|
---|
| 9 | *
|
---|
| 10 | * Parts copied from/inspired by the Linux AHCI driver;
|
---|
[84] | 11 | * those parts are (c) Linux AHCI/ATA maintainers
|
---|
| 12 | *
|
---|
| 13 | * This program is free software; you can redistribute it and/or modify
|
---|
| 14 | * it under the terms of the GNU General Public License as published by
|
---|
| 15 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 16 | * (at your option) any later version.
|
---|
| 17 | *
|
---|
| 18 | * This program is distributed in the hope that it will be useful,
|
---|
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 21 | * GNU General Public License for more details.
|
---|
| 22 | *
|
---|
| 23 | * You should have received a copy of the GNU General Public License
|
---|
| 24 | * along with this program; if not, write to the Free Software
|
---|
| 25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
| 26 | */
|
---|
| 27 |
|
---|
| 28 | #include "os2ahci.h"
|
---|
| 29 |
|
---|
[161] | 30 | /* Legacy APM support is not needed on eCS systems with ACPI and is more
|
---|
| 31 | * reliable without it enabled.
|
---|
| 32 | */
|
---|
| 33 | #ifdef LEGACY_APM
|
---|
| 34 |
|
---|
[84] | 35 | #include <apmcalls.h>
|
---|
[178] | 36 | USHORT _cdecl apm_event (APMEVENT *evt);
|
---|
[84] | 37 |
|
---|
| 38 | /******************************************************************************
|
---|
| 39 | * Connect to APM driver and register for power state change events.
|
---|
| 40 | */
|
---|
| 41 | void apm_init(void)
|
---|
| 42 | {
|
---|
| 43 | USHORT rc;
|
---|
| 44 |
|
---|
| 45 | /* connect to APM driver */
|
---|
| 46 | if ((rc = APMAttach()) != 0) {
|
---|
[209] | 47 | DPRINTF(0, DBG_PREFIX": couldn't connect to APM driver (rc = %d)\n", rc);
|
---|
[84] | 48 | return;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | /* register for suspend/resume events */
|
---|
| 52 | if ((rc = APMRegister(apm_event, APM_NOTIFYSETPWR |
|
---|
| 53 | APM_NOTIFYNORMRESUME |
|
---|
| 54 | APM_NOTIFYCRITRESUME, 0)) != 0) {
|
---|
[209] | 55 | DPRINTF(0, DBG_PREFIX": couldn't register for power event notificatins (rc = %d)\n", rc);
|
---|
[84] | 56 | return;
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | /******************************************************************************
|
---|
| 61 | * APM event handler
|
---|
| 62 | */
|
---|
[178] | 63 | USHORT _cdecl apm_event(APMEVENT *evt)
|
---|
[84] | 64 | {
|
---|
| 65 | USHORT msg = (USHORT) evt->ulParm1;
|
---|
| 66 |
|
---|
[209] | 67 | DPRINTF(DBG_FUNCBEG, DBG_PREFIX": received APM event: 0x%x/0x%x\n");
|
---|
[84] | 68 |
|
---|
| 69 | switch (msg) {
|
---|
| 70 |
|
---|
| 71 | case APM_SETPWRSTATE:
|
---|
| 72 | if (evt->ulParm2 >> 16 != APM_PWRSTATEREADY) {
|
---|
| 73 | /* we're suspending */
|
---|
[160] | 74 | suspend();
|
---|
[84] | 75 | }
|
---|
| 76 | break;
|
---|
| 77 |
|
---|
| 78 | case APM_NORMRESUMEEVENT:
|
---|
| 79 | case APM_CRITRESUMEEVENT:
|
---|
| 80 | /* we're resuming */
|
---|
[160] | 81 | resume();
|
---|
[84] | 82 | break;
|
---|
| 83 |
|
---|
| 84 | default:
|
---|
[209] | 85 | DPRINTF(0, DBG_PREFIX": unknown APM event; ignoring...\n");
|
---|
[84] | 86 | break;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | return(0);
|
---|
| 90 | }
|
---|
[161] | 91 | #endif /* LEGACY_APM */
|
---|
[84] | 92 |
|
---|
| 93 | /******************************************************************************
|
---|
[160] | 94 | * Suspend handler. In a nutshell, it'll turn off interrupts and flush all
|
---|
[84] | 95 | * write caches.
|
---|
| 96 | */
|
---|
[160] | 97 | void suspend(void)
|
---|
[84] | 98 | {
|
---|
| 99 | int a;
|
---|
| 100 | int p;
|
---|
| 101 | int d;
|
---|
[157] | 102 | TIMER Timer;
|
---|
[84] | 103 |
|
---|
[153] | 104 | if (suspended) return;
|
---|
[209] | 105 | DPRINTF(DBG_FUNCBEG, DBG_PREFIX": suspend()\n");
|
---|
[84] | 106 |
|
---|
| 107 | /* restart all ports with interrupts disabled */
|
---|
| 108 | for (a = 0; a < ad_info_cnt; a++) {
|
---|
| 109 | AD_INFO *ai = ad_infos + a;
|
---|
| 110 |
|
---|
| 111 | lock_adapter(ai);
|
---|
| 112 | for (p = 0; p <= ai->port_max; p++) {
|
---|
| 113 | /* wait until all active commands have completed on this port */
|
---|
[178] | 114 | TimerInit(&Timer, 250);
|
---|
[84] | 115 | while (ahci_port_busy(ai, p)) {
|
---|
[178] | 116 | if (TimerCheckAndBlock(&Timer)) break;
|
---|
[84] | 117 | }
|
---|
| 118 |
|
---|
| 119 | /* restart port with interrupts disabled */
|
---|
| 120 | ahci_stop_port(ai, p);
|
---|
| 121 | ahci_start_port(ai, p, 0);
|
---|
| 122 |
|
---|
| 123 | /* flush cache on all attached devices */
|
---|
[198] | 124 | for (d = 0; d <= ai->ports[p].dev_max; d++)
|
---|
| 125 | {
|
---|
[206] | 126 | if (ai->ports[p].devs[d].present)
|
---|
[198] | 127 | {
|
---|
[165] | 128 | ahci_flush_cache(ai, p, d);
|
---|
| 129 | }
|
---|
[84] | 130 | }
|
---|
| 131 | }
|
---|
[160] | 132 |
|
---|
| 133 | /* AHCI spec rev1.1 section 8.3.3:
|
---|
| 134 | * Software must disable interrupts prior to requesting a transition of the HBA to D3 state.
|
---|
| 135 | */
|
---|
| 136 | writel(ai->mmio + HOST_CTL, readl(ai->mmio + HOST_CTL) & ~HOST_IRQ_EN);
|
---|
| 137 | readl(ai->mmio + HOST_CTL); /* flush */
|
---|
[162] | 138 |
|
---|
| 139 | /* TODO: put the device into the D3 state */
|
---|
[84] | 140 | }
|
---|
| 141 |
|
---|
| 142 | /* reset init_complete so that we can process IORBs without interrupts */
|
---|
| 143 | init_complete = 0;
|
---|
| 144 |
|
---|
[153] | 145 | suspended = 1;
|
---|
[209] | 146 | DPRINTF(DBG_FUNCEND, DBG_PREFIX": suspend() finished\n");
|
---|
[84] | 147 | }
|
---|
| 148 |
|
---|
| 149 | /******************************************************************************
|
---|
[160] | 150 | * Resume handler. All ports are restarted with interrupts enabled using
|
---|
[84] | 151 | * the same function as the IOCM_COMPLETE_INIT handler does.
|
---|
| 152 | */
|
---|
[160] | 153 | void resume(void)
|
---|
[84] | 154 | {
|
---|
| 155 | int a;
|
---|
| 156 |
|
---|
[153] | 157 | if (!suspended) return;
|
---|
[209] | 158 | DPRINTF(DBG_FUNCBEG, DBG_PREFIX": resume()\n");
|
---|
[84] | 159 |
|
---|
| 160 | for (a = 0; a < ad_info_cnt; a++) {
|
---|
| 161 | AD_INFO *ai = ad_infos + a;
|
---|
| 162 |
|
---|
[162] | 163 | /* TODO: put the device into the D0 state */
|
---|
| 164 |
|
---|
[161] | 165 | //ahci_reset_controller(ai);
|
---|
| 166 |
|
---|
[84] | 167 | /* Complete initialization of this adapter; this will restart the ports
|
---|
| 168 | * with interrupts enabled and take care of whatever else needs to be
|
---|
| 169 | * done to get the adapter and its ports up and running.
|
---|
| 170 | */
|
---|
| 171 | ahci_complete_init(ai);
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | /* tell the driver we're again fully operational */
|
---|
| 175 | init_complete = 1;
|
---|
| 176 |
|
---|
| 177 | /* unlock all adapters now that we have set the init_complete flag */
|
---|
| 178 | for (a = 0; a < ad_info_cnt; a++) {
|
---|
[161] | 179 | AD_INFO *ai = ad_infos + a;
|
---|
| 180 | unlock_adapter(ai);
|
---|
[84] | 181 | }
|
---|
| 182 |
|
---|
[153] | 183 | suspended = 0;
|
---|
[161] | 184 |
|
---|
| 185 | /* restart engine to resume IORB processing */
|
---|
[162] | 186 | /* The resume_sleep_flag and probably rearming the ctx hook is a temporary hack
|
---|
| 187 | * to make resume kind of work when I/O operations are outstanding or started
|
---|
| 188 | * during the suspend operation. This behavior may change with future versions
|
---|
| 189 | * of the ACPI software which will make this hack unnecessary.
|
---|
| 190 | */
|
---|
[161] | 191 | resume_sleep_flag = 5000;
|
---|
[178] | 192 | KernArmHook(engine_ctxhook_h, 0, 0);
|
---|
[161] | 193 |
|
---|
[209] | 194 | DPRINTF(DBG_FUNCEND, DBG_PREFIX": resume() finished\n");
|
---|
[84] | 195 | }
|
---|
[160] | 196 |
|
---|
| 197 | /******************************************************************************
|
---|
| 198 | * This is the kernel exit handler for panics and traps.
|
---|
| 199 | * Assume the system is trashed and do the absolute minimum necessary
|
---|
| 200 | * to put the adapters into a state so that the BIOS can operate the
|
---|
| 201 | * adapters. We never need to recover from this as the system will be rebooted.
|
---|
| 202 | */
|
---|
| 203 | void shutdown_driver(void)
|
---|
| 204 | {
|
---|
| 205 | int a;
|
---|
| 206 | int p;
|
---|
| 207 | u16 i;
|
---|
| 208 | u32 tmp;
|
---|
| 209 | //int d;
|
---|
| 210 |
|
---|
[209] | 211 | DPRINTF(DBG_FUNCBEG, DBG_PREFIX": shutdown_driver() enter\n");
|
---|
[160] | 212 |
|
---|
[178] | 213 | for (a = 0; a < ad_info_cnt; a++)
|
---|
| 214 | {
|
---|
[160] | 215 | AD_INFO *ai = ad_infos + a;
|
---|
| 216 |
|
---|
| 217 | /* Try to be nice. Wait 50ms for adapter to go not busy.
|
---|
| 218 | * If it doesn't go not busy in that time, too bad. Stop it anyway.
|
---|
| 219 | */
|
---|
| 220 | for (i=0; i<50000 && ai->busy; i++) udelay(1000);
|
---|
| 221 |
|
---|
[178] | 222 | for (p = 0; p <= ai->port_max; p++)
|
---|
| 223 | {
|
---|
| 224 | u8 *port_mmio = port_base(ai, p);
|
---|
[160] | 225 |
|
---|
| 226 | /* Wait up to 50ms for port to go not busy. Again stop it
|
---|
| 227 | * anyway if it doesn't go not busy in that time.
|
---|
| 228 | */
|
---|
| 229 | for (i=0; i<50000 && ahci_port_busy(ai, p); i++) udelay(1000);
|
---|
| 230 |
|
---|
| 231 | /* stop port */
|
---|
| 232 | writel(port_mmio + PORT_IRQ_MASK, 0); /* disable port interrupts */
|
---|
| 233 | writel(port_mmio + PORT_CMD, readl(port_mmio + PORT_CMD) & ~PORT_CMD_FIS_RX); /* disable FIS reception */
|
---|
| 234 | while (readl(port_mmio + PORT_CMD) & PORT_CMD_FIS_ON); /* wait for it to stop */
|
---|
| 235 | writel(port_mmio + PORT_CMD, readl(port_mmio + PORT_CMD) & ~PORT_CMD_START); /* set port to idle */
|
---|
| 236 | while (readl(port_mmio + PORT_CMD) & PORT_CMD_LIST_ON); /* wait for it to stop */
|
---|
| 237 |
|
---|
| 238 | /* clear any pending port IRQs */
|
---|
| 239 | tmp = readl(port_mmio + PORT_IRQ_STAT);
|
---|
| 240 | if (tmp) writel(port_mmio + PORT_IRQ_STAT, tmp);
|
---|
| 241 | writel(ai->mmio + HOST_IRQ_STAT, 1UL << p);
|
---|
| 242 |
|
---|
| 243 | /* reset PxSACT register (tagged command queues, not reset by COMRESET) */
|
---|
| 244 | writel(port_mmio + PORT_SCR_ACT, 0);
|
---|
| 245 | readl(port_mmio + PORT_SCR_ACT); /* flush */
|
---|
| 246 | }
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | init_complete = 0;
|
---|
| 250 |
|
---|
| 251 | /* restore BIOS configuration for each adapter */
|
---|
[178] | 252 | for (a = 0; a < ad_info_cnt; a++)
|
---|
| 253 | {
|
---|
[160] | 254 | ahci_restore_bios_config(ad_infos + a);
|
---|
| 255 | }
|
---|
| 256 |
|
---|
[209] | 257 | DPRINTF(DBG_FUNCEND, DBG_PREFIX": shutdown_driver() finished\n");
|
---|
[160] | 258 | }
|
---|
| 259 |
|
---|