From owner-acpi-jp@jp.freebsd.org  Sat Oct 13 19:29:53 2001
Received: (from daemon@localhost)
	by castle.jp.freebsd.org (8.9.3+3.2W/8.7.3) id TAA85356;
	Sat, 13 Oct 2001 19:29:53 +0900 (JST)
	(envelope-from owner-acpi-jp@jp.FreeBSD.org)
Received: from tasogare.imasy.or.jp (root@tasogare.imasy.or.jp [202.227.24.5])
	by castle.jp.freebsd.org (8.9.3+3.2W/8.7.3) with ESMTP id TAA85351
	for <acpi-jp@jp.freebsd.org>; Sat, 13 Oct 2001 19:29:52 +0900 (JST)
	(envelope-from iwasaki@jp.FreeBSD.org)
Received: from localhost (iwasaki.imasy.or.jp [202.227.24.92])
	by tasogare.imasy.or.jp (8.11.6+3.4W/8.11.6/tasogare) with ESMTP/inet id f9DATnm67088;
	Sat, 13 Oct 2001 19:29:49 +0900 (JST)
	(envelope-from iwasaki@jp.FreeBSD.org)
Date: Sat, 13 Oct 2001 19:29:45 +0900 (JST)
Message-Id: <20011013.192945.74756155.iwasaki@jp.FreeBSD.org>
To: acpi-jp@jp.freebsd.org
Cc: robert.moore@intel.com, andrew.grover@intel.com
From: Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
In-Reply-To: <20011013072103.A13195%yakoh@sd.keio.ac.jp>
References: <20010905152853.A12323%yakoh@sd.keio.ac.jp>
	<20011013.011311.74756308.iwasaki@jp.FreeBSD.org>
	<20011013072103.A13195%yakoh@sd.keio.ac.jp>
X-Mailer: Mew version 2.0 on Emacs 20.7 / Mule 4.0 (HANANOEN)
Mime-Version: 1.0
Content-Type: Text/Plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Reply-To: acpi-jp@jp.freebsd.org
Precedence: list
X-Distribute: distribute version 2.1 (Alpha) patchlevel 24e+010328
X-Sequence: acpi-jp 1340
Subject: [acpi-jp 1340] Re: S4BIOS implementation (was Re: panic in
 EcWaitEventIntr?(Re: HEADS UP: ACPI CHANGES AFFECTING MOST -CURRENT USERS))
Errors-To: owner-acpi-jp@jp.freebsd.org
Sender: owner-acpi-jp@jp.freebsd.org
X-Originator: iwasaki@jp.freebsd.org

Hi, Intel folks.  I've just made S4BIOS patches for ACPICA.
I think that S4BIOS sleep transition should be OS-independent,
so could you review this and integrate this to ACPICA if acceptable?

From: Takahiro Yakoh <yakoh@sd.keio.ac.jp>
Subject: [acpi-jp 1339] Re: S4BIOS implementation (was Re: panic in EcWaitEventIntr?(Re: HEADS UP: ACPI CHANGES AFFECTING MOST -CURRENT USERS))
Date: Sat, 13 Oct 2001 07:21:04 +0900
Message-ID: <20011013072103.A13195%yakoh@sd.keio.ac.jp>

> > How is this going?  I get some time to spend for this tonight and tomorrow.
> > Hopefully I can submit the S4BIOS patch within a couple of days, I hope
> > that someone test it.
> 
> I have to say I'm sorry.  Though I've already finished to read and
> understand the ACPI spec, I've not read the current ACPI source code.

Never mind :-)  and Great!
# I haven't finish reading thru the spec and whole source code yet.

> So I've done nothing yet.  But since my machine is still updated to
> -CURRENT, I can test your fantastic job.  So please send me your patch.

Here the patches.  Note that FIVA users need to set your disk to
UDMA33 mode in BIOS setting.  I think atacontrol wouldn't be helpful...
Paches for syscons driver are also included, this would be useful for
some laptops which have a problem with wakeup when X running.
# These machines usually need vidcontrol to switch video chip to text
# mode on sleeping.  This technique is used in Win2K too.

Thanks

Index: contrib/dev/acpica/acpixf.h
===================================================================
RCS file: /home/ncvs/src/sys/contrib/dev/acpica/acpixf.h,v
retrieving revision 1.11
diff -u -r1.11 acpixf.h
--- contrib/dev/acpica/acpixf.h	4 Oct 2001 23:15:26 -0000	1.11
+++ contrib/dev/acpica/acpixf.h	9 Oct 2001 16:39:22 -0000
@@ -407,8 +407,16 @@
     ACPI_PHYSICAL_ADDRESS   *PhysicalAddress);
 
 ACPI_STATUS
+AcpiPrepareToSleep (
+    UINT8 SleepState);
+
+ACPI_STATUS
 AcpiEnterSleepState (
     UINT8 SleepState);
+
+ACPI_STATUS
+AcpiEnterSleepStateS4Bios (
+    void);
 
 ACPI_STATUS
 AcpiLeaveSleepState (
Index: contrib/dev/acpica/actbl.h
===================================================================
RCS file: /home/ncvs/src/sys/contrib/dev/acpica/actbl.h,v
retrieving revision 1.1.1.5
diff -u -r1.1.1.5 actbl.h
--- contrib/dev/acpica/actbl.h	29 May 2001 19:52:28 -0000	1.1.1.5
+++ contrib/dev/acpica/actbl.h	9 Oct 2001 05:45:29 -0000
@@ -202,6 +202,8 @@
 {
     UINT32                  *GlobalLock;
     UINT64                  *FirmwareWakingVector;
+    UINT32_BIT              S4Bios_f      : 1;
+    UINT32_BIT              Reserved1     : 31;
     UINT8                   VectorWidth;
 
 } ACPI_COMMON_FACS;
Index: contrib/dev/acpica/hwsleep.c
===================================================================
RCS file: /home/ncvs/src/sys/contrib/dev/acpica/hwsleep.c,v
retrieving revision 1.1.1.9
diff -u -r1.1.1.9 hwsleep.c
--- contrib/dev/acpica/hwsleep.c	4 Oct 2001 23:12:11 -0000	1.1.1.9
+++ contrib/dev/acpica/hwsleep.c	9 Oct 2001 16:11:11 -0000
@@ -216,6 +216,44 @@
 
 /******************************************************************************
  *
+ * FUNCTION:    AcpiPrepareToSleep
+ *
+ * PARAMETERS:  SleepState          - Which sleep state to enter
+ *
+ * RETURN:      Status
+ *
+ * DESCRIPTION:	run the _PTS and _GTS methods
+ *
+ ******************************************************************************/
+
+ACPI_STATUS
+AcpiPrepareToSleep (
+    UINT8               SleepState)
+{
+    ACPI_OBJECT_LIST    ArgList;
+    ACPI_OBJECT         Arg;
+
+
+    FUNCTION_TRACE ("AcpiPrepareToSleep");
+
+    /* run the _PTS and _GTS methods */
+
+    MEMSET(&ArgList, 0, sizeof(ArgList));
+    ArgList.Count = 1;
+    ArgList.Pointer = &Arg;
+
+    MEMSET(&Arg, 0, sizeof(Arg));
+    Arg.Type = ACPI_TYPE_INTEGER;
+    Arg.Integer.Value = SleepState;
+
+    AcpiEvaluateObject (NULL, "\\_PTS", &ArgList, NULL);
+    AcpiEvaluateObject (NULL, "\\_GTS", &ArgList, NULL);
+
+    return_ACPI_STATUS (AE_OK);
+}
+
+/******************************************************************************
+ *
  * FUNCTION:    AcpiEnterSleepState
  *
  * PARAMETERS:  SleepState          - Which sleep state to enter
@@ -231,8 +269,6 @@
     UINT8               SleepState)
 {
     ACPI_STATUS         Status;
-    ACPI_OBJECT_LIST    ArgList;
-    ACPI_OBJECT         Arg;
     UINT8               TypeA;
     UINT8               TypeB;
     UINT16              PM1AControl;
@@ -253,16 +289,11 @@
 
     /* run the _PTS and _GTS methods */
 
-    MEMSET(&ArgList, 0, sizeof(ArgList));
-    ArgList.Count = 1;
-    ArgList.Pointer = &Arg;
-
-    MEMSET(&Arg, 0, sizeof(Arg));
-    Arg.Type = ACPI_TYPE_INTEGER;
-    Arg.Integer.Value = SleepState;
-
-    AcpiEvaluateObject (NULL, "\\_PTS", &ArgList, NULL);
-    AcpiEvaluateObject (NULL, "\\_GTS", &ArgList, NULL);
+    Status = AcpiPrepareToSleep (SleepState);
+    if (!ACPI_SUCCESS (Status))
+    {
+        return Status;
+    }
 
     /* clear wake status */
 
@@ -321,6 +352,68 @@
     do 
     {
         AcpiOsStall(10000);
+    }
+    while (!AcpiHwRegisterBitAccess (ACPI_READ, ACPI_MTX_LOCK, WAK_STS));
+
+    AcpiHwEnableNonWakeupGpes();
+
+    enable ();
+
+    return_ACPI_STATUS (AE_OK);
+}
+
+/******************************************************************************
+ *
+ * FUNCTION:    AcpiEnterSleepStateS4Bios
+ *
+ * PARAMETERS:  None
+ *
+ * RETURN:      Status
+ *
+ * DESCRIPTION: Enter a system sleep state S4BIOS
+ *
+ ******************************************************************************/
+
+ACPI_STATUS
+AcpiEnterSleepStateS4Bios (
+    void)
+{
+    ACPI_STATUS         Status;
+
+
+    FUNCTION_TRACE ("AcpiEnterSleepStateS4Bios");
+
+
+    if (!AcpiGbl_FACS->S4Bios_f)
+    {
+        return_ACPI_STATUS (AE_NOT_EXIST);
+    }
+
+    /* run the _PTS and _GTS methods */
+
+    Status = AcpiPrepareToSleep (ACPI_STATE_S4);
+    if (!ACPI_SUCCESS (Status))
+    {
+        return Status;
+    }
+
+    /* clear wake status */
+
+    AcpiHwRegisterBitAccess (ACPI_WRITE, ACPI_MTX_LOCK, WAK_STS, 1);
+
+    disable ();
+
+    AcpiHwDisableNonWakeupGpes();
+
+    /* flush caches */
+
+    wbinvd();
+
+    /* write the value to command port and wait until we enter sleep state */
+    do
+    {
+        AcpiOsStall(1000000);
+        AcpiOsWritePort (AcpiGbl_FADT->SmiCmd, AcpiGbl_FADT->S4BiosReq, 8);
     }
     while (!AcpiHwRegisterBitAccess (ACPI_READ, ACPI_MTX_LOCK, WAK_STS));
 
Index: contrib/dev/acpica/tbconvrt.c
===================================================================
RCS file: /home/ncvs/src/sys/contrib/dev/acpica/tbconvrt.c,v
retrieving revision 1.1.1.9
diff -u -r1.1.1.9 tbconvrt.c
--- contrib/dev/acpica/tbconvrt.c	7 Sep 2001 01:22:25 -0000	1.1.1.9
+++ contrib/dev/acpica/tbconvrt.c	9 Oct 2001 05:46:22 -0000
@@ -635,6 +635,7 @@
 
         CommonFacs->GlobalLock = (UINT32 *) &(FACS71->GlobalLock);
         CommonFacs->FirmwareWakingVector = &FACS71->FirmwareWakingVector;
+        CommonFacs->S4Bios_f = FACS71->S4Bios_f;
         CommonFacs->VectorWidth = 64;
 #else
         /* ACPI 1.0 FACS */
@@ -642,6 +643,7 @@
         FACS1 = (FACS_DESCRIPTOR_REV1 *) AcpiGbl_FACS;
 
         CommonFacs->GlobalLock = &(FACS1->GlobalLock);
+        CommonFacs->S4Bios_f = FACS1->S4Bios_f;
         CommonFacs->FirmwareWakingVector = (UINT64 *) &FACS1->FirmwareWakingVector;
         CommonFacs->VectorWidth = 32;
 
@@ -655,6 +657,7 @@
         FACS2 = (FACS_DESCRIPTOR_REV2 *) AcpiGbl_FACS;
 
         CommonFacs->GlobalLock = &(FACS2->GlobalLock);
+        CommonFacs->S4Bios_f = FACS2->S4Bios_f;
         CommonFacs->FirmwareWakingVector = &FACS2->XFirmwareWakingVector;
         CommonFacs->VectorWidth = 64;
     }
Index: i386/acpica/acpi_wakeup.c
===================================================================
RCS file: /home/ncvs/src/sys/i386/acpica/acpi_wakeup.c,v
retrieving revision 1.3
diff -u -r1.3 acpi_wakeup.c
--- i386/acpica/acpi_wakeup.c	22 Jul 2001 19:13:54 -0000	1.3
+++ i386/acpica/acpi_wakeup.c	9 Oct 2001 16:08:32 -0000
@@ -236,7 +242,13 @@
 			acpi_printcpu();
 		}
 
-		if ((status = AcpiEnterSleepState(state)) != AE_OK) {
+		if (state == ACPI_STATE_S4 /* && sc->useS4Bios */) {
+			status = AcpiEnterSleepStateS4Bios();
+		} else {
+			status = AcpiEnterSleepState(state);
+		}
+
+		if (status != AE_OK) {
 			device_printf(sc->acpi_dev,
 				"AcpiEnterSleepState failed - %s\n",
 				AcpiFormatException(status));
Index: isa/syscons_isa.c
===================================================================
RCS file: /home/ncvs/src/sys/isa/syscons_isa.c,v
retrieving revision 1.17
diff -u -r1.17 syscons_isa.c
--- isa/syscons_isa.c	30 Jun 2001 10:15:06 -0000	1.17
+++ isa/syscons_isa.c	12 Oct 2001 21:30:36 -0000
@@ -88,6 +88,40 @@
 	return sc_attach_unit(device_get_unit(dev), device_get_flags(dev));
 }
 
+static int	sc_cur_scr;
+
+static int
+scsuspend(device_t dev)
+{
+	int		retry = 10;
+	static int	dummy;
+	sc_softc_t	*sc;
+
+	sc = &main_softc;
+	sc_cur_scr = sc->cur_scp->index;
+	do {
+		sc_switch_scr(sc, 0);
+		if (sc->switch_in_progress) {
+			tsleep(&dummy, 0, "scsuspend", 1000); 
+		} else {
+			break;
+		}
+	} while (retry--);
+
+	return (0);
+}
+
+static int
+scresume(device_t dev)
+{
+	sc_softc_t	*sc;
+
+	sc = &main_softc;
+	sc_switch_scr(sc, sc_cur_scr);
+
+	return (0);
+}
+
 int
 sc_max_unit(void)
 {
@@ -230,6 +264,8 @@
 	DEVMETHOD(device_identify,	scidentify),
 	DEVMETHOD(device_probe,         scprobe),
 	DEVMETHOD(device_attach,        scattach),
+	DEVMETHOD(device_suspend,       scsuspend),
+	DEVMETHOD(device_resume,        scresume),
 	{ 0, 0 }
 };
 
