From owner-acpi-jp@jp.freebsd.org  Wed Jun 27 21:20:47 2001
Received: (from daemon@localhost)
	by castle.jp.freebsd.org (8.9.3+3.2W/8.7.3) id VAA89368;
	Wed, 27 Jun 2001 21:20:47 +0900 (JST)
	(envelope-from owner-acpi-jp@jp.FreeBSD.org)
Received: from shidahara1.planet.sci.kobe-u.ac.jp (shidahara1.planet.sci.kobe-u.ac.jp [133.30.50.200])
	by castle.jp.freebsd.org (8.9.3+3.2W/8.7.3) with ESMTP id VAA89363
	for <acpi-jp@jp.freebsd.org>; Wed, 27 Jun 2001 21:20:46 +0900 (JST)
	(envelope-from takawata@shidahara1.planet.sci.kobe-u.ac.jp)
Received: from shidahara1.planet.sci.kobe-u.ac.jp (localhost [127.0.0.1])
	by shidahara1.planet.sci.kobe-u.ac.jp (8.9.3/8.9.3) with ESMTP id VAA86709
	for <acpi-jp@jp.freebsd.org>; Wed, 27 Jun 2001 21:27:33 +0900 (JST)
	(envelope-from takawata@shidahara1.planet.sci.kobe-u.ac.jp)
Message-Id: <200106271227.VAA86709@shidahara1.planet.sci.kobe-u.ac.jp>
To: acpi-jp@jp.freebsd.org
Date: Wed, 27 Jun 2001 21:27:33 +0900
From: Takanori Watanabe <takawata@shidahara1.planet.sci.kobe-u.ac.jp>
Reply-To: acpi-jp@jp.freebsd.org
Precedence: list
X-Distribute: distribute version 2.1 (Alpha) patchlevel 24e+010328
X-Sequence: acpi-jp 1149
Subject: [acpi-jp 1149] ACPI S3 progress
Errors-To: owner-acpi-jp@jp.freebsd.org
Sender: owner-acpi-jp@jp.freebsd.org
X-Originator: takawata@shidahara1.planet.sci.kobe-u.ac.jp

I managed to make S3 wakeup and transfer to Kernel Virtual Address
I'm not sure where it is now. But I'm sure it reaches to acpi_ioctl
and kernel printf is working on resumed  context

Following is the diff. This will not be applied cleanly because 
it is a bit older version.
It includes knote related stuff.
Please review it.

$BEOJUB:5*(B
$B?@8MBg3XBg3X1!<+A32J3X8&5f2J(BD3$B>pJs%a%G%#%"2J3X@l96(B
<a href="http://www.planet.sci.kobe-u.ac.jp/~takawata/key.html">
Public Key</a>
Key fingerprint =  2C 51 E2 78 2C E1 C5 2D  0F F1 20 A3 11 3A 62 2A 

--- /sys/contrib/dev/acpica/hwsleep.c	Sun Jun 17 22:46:11 2001
+++ acpica/hwsleep.c	Tue Jun 12 19:09:34 2001
@@ -264,7 +264,7 @@
 
     AcpiEvaluateObject(NULL, "\\_PTS", &ArgList, NULL);
     AcpiEvaluateObject(NULL, "\\_GTS", &ArgList, NULL);
-
+   
     /* clear wake status */
 
     AcpiHwRegisterBitAccess(ACPI_WRITE, ACPI_MTX_LOCK, WAK_STS, 1);
@@ -277,7 +277,7 @@
 
     /* mask in SLP_EN */
 
-    PM1AControl |= (1 << AcpiHwGetBitShift (SLP_EN_MASK));
+    PM1AControl |= SLP_EN_MASK;
 
     PM1BControl = PM1AControl;
 
@@ -290,13 +290,17 @@
 
     disable();
 
+    wbinvd();
     AcpiHwRegisterWrite(ACPI_MTX_LOCK, PM1A_CONTROL, PM1AControl);
     AcpiHwRegisterWrite(ACPI_MTX_LOCK, PM1B_CONTROL, PM1BControl);
 
     /* one system won't work with this, one won't work without */
-    /*AcpiHwRegisterWrite(ACPI_MTX_LOCK, PM1_CONTROL,
-        (1 << AcpiHwGetBitShift (SLP_EN_MASK)));*/
-
+#if 0
+    AcpiHwRegisterWrite(ACPI_MTX_LOCK, PM1_CONTROL,
+        (1 << AcpiHwGetBitShift (SLP_EN_MASK)));
+#endif
+    while(!AcpiHwRegisterBitAccess(ACPI_READ,ACPI_MTX_LOCK,WAK_STS))
+      ;
     enable();
 
     return_ACPI_STATUS (AE_OK);


diff -ruN /sys/dev/acpica/acpi.c acpica/acpi.c
--- /sys/dev/acpica/acpi.c	Sun Jun 17 22:46:12 2001
+++ acpica/acpi.c	Wed Jun 27 18:59:23 2001
@@ -46,6 +46,7 @@
 #include <machine/clock.h>
 
 #include <machine/resource.h>
+#include <machine/clock.h>
 
 #include "acpi.h"
 
@@ -67,6 +68,9 @@
 static d_open_t		acpiopen;
 static d_close_t	acpiclose;
 static d_ioctl_t	acpiioctl;
+static int acpikqfilter (dev_t, struct knote *);
+static int  filt_acpi(struct knote *, long );
+static void filt_acpidetach(struct knote *);
 
 #define CDEV_MAJOR 152
 static struct cdevsw acpi_cdevsw = {
@@ -82,7 +86,8 @@
     CDEV_MAJOR,
     nodump,
     nopsize,
-    0
+    D_KQFILTER,
+    acpikqfilter,
 };
 
 static const char* sleep_state_names[] = {
@@ -227,7 +232,7 @@
 
     FUNCTION_TRACE(__func__);
 
-    if ((error = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th)) != AE_OK) {
+    if ((error = AcpiGetTableHeader(ACPI_TABLE_FADT, 1, &th)) != AE_OK) {
 	device_printf(dev, "couldn't get XSDT header: %s\n", acpi_strerror(error));
 	return_VALUE(ENXIO);
     }
@@ -251,7 +256,9 @@
     sc = device_get_softc(dev);
     bzero(sc, sizeof(*sc));
     sc->acpi_dev = dev;
-
+    if(acpi_alloc_wakeup_handler(sc)){
+      printf("allocwake error\n");
+    }
 #ifdef ENABLE_DEBUGGER
     if (debugpoint && !strcmp(debugpoint, "spaces"))
 	acpi_EnterDebugger();
@@ -299,7 +306,8 @@
 	device_printf(dev, "could not enable ACPI: %s\n", acpi_strerror(error));
 	return_VALUE(ENXIO);
     }
-
+    bus_dmamap_load(sc->acpi_waketag,sc->acpi_wakemap,sc->acpi_wakeaddr,4096,
+		    acpi_realmodeinst, sc,0);
     /*
      * Setup our sysctl tree.
      *
@@ -318,7 +326,9 @@
     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
 	OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
 	&sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", "");
-    
+    mtx_init(&sc->acpi_mtx,"ACPI softc mtx",MTX_DEF);
+    sc->acpi_filter = -1;
+
     /*
      * Dispatch the default sleep state to devices.
      * TBD: should be configured from userland policy manager.
@@ -339,6 +349,7 @@
     if (!acpi_disabled("bus"))
 	acpi_probe_children(dev);
 
+
     /*
      * Register our shutdown handlers
      */
@@ -665,7 +676,7 @@
 acpi_shutdown_final(void *arg, int howto)
 {
     ACPI_STATUS	status;
-
+    struct acpi_softc  *sc =arg;
     if (howto & RB_POWEROFF) {
 	printf("Power system off using ACPI...\n");
 	if ((status = AcpiEnterSleepState(acpi_off_state)) != AE_OK) {
@@ -895,7 +906,9 @@
 	break;
 
     case ACPI_STATE_S3:
-	acpi_off_state = ACPI_STATE_S3;
+        acpi_sleep(sc,state);
+	printf("HEHEHE\n");
+	break;
 	/* FALLTHROUGH */
     case ACPI_STATE_S5:
 	/*
@@ -1032,9 +1045,10 @@
     struct acpi_softc	*sc = (struct acpi_softc *)context;
 
     FUNCTION_TRACE(__func__);
-
+    acpi_knote(ACPI_KNOTE_POWERBUTTON);
+#if 0
     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
-
+#endif
     return_VALUE(INTERRUPT_HANDLED);
 }
 
@@ -1056,9 +1070,10 @@
     struct acpi_softc	*sc = (struct acpi_softc *)context;
 
     FUNCTION_TRACE(__func__);
-
+    acpi_knote(ACPI_KNOTE_SLEEPBUTTON);
+#if 0
     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx);
-
+#endif
     return_VALUE(INTERRUPT_HANDLED);
 }
 
@@ -1302,6 +1317,59 @@
 
 out:
     return(error);
+}
+
+static struct filterops acpi_filtops = 
+{ 1, NULL, filt_acpidetach, filt_acpi };
+static int acpikqfilter(dev_t dev, struct knote * kn)
+{
+	struct acpi_softc *sc = dev->si_drv1;
+	struct klist *kl = &sc->acpi_klist;
+	
+	printf("KQFILT:%d\n",kn->kn_filter);
+	switch(kn->kn_filter){
+	case EVFILT_READ:
+		kn->kn_fop = & acpi_filtops;
+		break;
+	default:
+		return 1;
+	}
+	kn->kn_hook = (caddr_t) sc;
+	mtx_lock(&sc->acpi_mtx);
+	SLIST_INSERT_HEAD(kl, kn, kn_selnext);
+	mtx_unlock(&sc->acpi_mtx);
+	return 0;
+}
+static void filt_acpidetach(struct knote *kn)
+{
+	struct acpi_softc *sc = (struct acpi_softc *)kn->kn_hook;
+	mtx_lock(&sc->acpi_mtx);
+	SLIST_REMOVE(&sc->acpi_klist, kn, knote, kn_selnext);
+	if(SLIST_EMPTY(&sc->acpi_klist)){
+	  sc->acpi_filter = -1;
+	}
+	mtx_unlock(&sc->acpi_mtx);
+}
+ 
+static int filt_acpi(struct knote *kn, long hint)
+{
+	/*This assumes with acpi_mtx acquired*/
+	struct acpi_softc *sc = (struct acpi_softc *)kn->kn_hook;
+	if((sc->acpi_filter != -1)&&(kn->kn_sfflags&(1<<sc->acpi_filter))){
+		kn->kn_fflags |= (1<<sc->acpi_filter);
+		return 1;
+	}
+	return 0;
+}
+void acpi_knote(int filter){
+	struct acpi_softc *sc = devclass_get_softc(acpi_devclass,0);
+	if((filter <0) || (filter > 32)){
+	  Debugger("HOE\n");
+	}
+	mtx_lock(&sc->acpi_mtx);
+	sc->acpi_filter = filter; 
+	KNOTE(&sc->acpi_klist, 0);
+	mtx_unlock(&sc->acpi_mtx);
 }
 
 static int
diff -ruN /sys/dev/acpica/acpi_acad.c acpica/acpi_acad.c
--- /sys/dev/acpica/acpi_acad.c	Sun Jun 17 22:46:12 2001
+++ acpica/acpi_acad.c	Wed Jun 20 07:54:18 2001
@@ -71,7 +71,6 @@
 
 	if (acpi_EvaluateInteger(h, "_PSR", &sc->status) != AE_OK)
 		return;
-	device_printf(dev,"%s\n",(sc->status) ? "On Line" : "Off Line");
 }
 
 static void
@@ -84,9 +83,7 @@
 	case ACPI_DEVICE_CHECK_PNP:
 	case ACPI_DEVICE_CHECK_EXISTENCE:
 	case ACPI_POWERSOURCE_STAT_CHANGE:
-		/*Temporally. It is better to notify policy manager*/
-		AcpiOsQueueForExecution(OSD_PRIORITY_LO,
-		    acpi_acad_get_status,context);
+		acpi_knote(ACPI_KNOTE_ACAD);
 		break;
 	default:
 		break;
@@ -126,7 +123,7 @@
 	acpi_acad_get_status((void *)dev);
 
 	error = acpi_register_ioctl(ACPIIO_ACAD_GET_STATUS, acpi_acad_ioctl,
-	    device_get_softc(dev));
+	    dev);
 	if (error)
 		return (error);
 
@@ -153,15 +150,17 @@
 static int
 acpi_acad_ioctl(u_long cmd, caddr_t addr, void *arg)
 {
+        device_t dev = arg;
 	struct	 acpi_acad_softc *sc;
 
-	sc = (struct acpi_acad_softc *)arg;
+	sc = device_get_softc(dev);
 	if (sc == NULL) {
 		return(ENXIO);
 	}
 
 	switch (cmd) {
 	case ACPIIO_ACAD_GET_STATUS:
+	        acpi_acad_get_status(dev);
 		*(int *)addr = sc->status;
 		break;
 	}
diff -ruN /sys/dev/acpica/acpi_cmbat.c acpica/acpi_cmbat.c
--- /sys/dev/acpica/acpi_cmbat.c	Sun Jun 17 22:46:12 2001
+++ acpica/acpi_cmbat.c	Wed Jun 20 08:08:06 2001
@@ -61,8 +61,12 @@
 	device_t	dev;
 	struct acpi_bif	bif;
 	struct acpi_bst bst;
+        struct mtx acpi_cmbat_mtx;
 	ACPI_BUFFER	bif_buffer;
 	ACPI_BUFFER	bst_buffer;
+        int bif_valid;
+        int bst_valid;
+        int bif_gen;
 };
 
 /* XXX: devclass_get_maxunit() don't give us the current allocated units... */
@@ -151,6 +155,7 @@
 	PKG_GETINT(res, tmp, 2, sc->bst.cap, end);
 	PKG_GETINT(res, tmp, 3, sc->bst.volt, end);
 end:
+	sc->bst_valid = 1;
 }
 
 static void
@@ -210,26 +215,27 @@
 	PKG_GETSTR(res, tmp, 11, sc->bif.type, ACPI_CMBAT_MAXSTRLEN, end);
 	PKG_GETSTR(res, tmp, 12, sc->bif.oeminfo, ACPI_CMBAT_MAXSTRLEN, end);
 end:
+	sc->bif_valid = 1;
 }
 
 static void
 acpi_cmbat_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
 {
+        device_t dev = context;
+	struct acpi_cmbat_softc *sc = device_get_softc(dev);
 	switch (notify) {
-#if 0
-	/* XXX
-	 * AML method execution is somewhat heavy even using swi.
-	 * better to disable them until we fix the problem.
-	 */
-	case ACPI_BATTERY_BST_CHANGE:
-		AcpiOsQueueForExecution(OSD_PRIORITY_LO,
-		    acpi_cmbat_get_bst, context);
-		break;
 	case ACPI_BATTERY_BIF_CHANGE:
-		AcpiOsQueueForExecution(OSD_PRIORITY_LO,
-		    acpi_cmbat_get_bif, context);
+	        sc->bif_gen ++;
+		mtx_lock(&sc->acpi_cmbat_mtx);
+		sc->bif_valid = 0;
+		mtx_unlock(&sc->acpi_cmbat_mtx);
+		/*FALLTHROUGH*/
+	case ACPI_BATTERY_BST_CHANGE:
+		mtx_lock(&sc->acpi_cmbat_mtx);
+	        sc->bst_valid = 0;
+		mtx_unlock(&sc->acpi_cmbat_mtx);
+ 	        acpi_knote(ACPI_KNOTE_BAT_START+device_get_unit(dev));
 		break;
-#endif
 	default:
 		break;
 	}
@@ -261,7 +267,10 @@
 				 acpi_cmbat_notify_handler,dev);
 	bzero(&sc->bif_buffer, sizeof(sc->bif_buffer));
 	bzero(&sc->bst_buffer, sizeof(sc->bst_buffer));
-	
+	sc->bif_gen = 1;
+	sc->bif_valid = 0;
+	sc->bst_valid = 0;
+	mtx_init(&sc->acpi_cmbat_mtx,"CMBAT softc MTX",MTX_DEF);
 	acpi_cmbat_get_bif(dev);
 	acpi_cmbat_get_bst(dev);
 
@@ -334,7 +343,11 @@
 		break;
 
 	case ACPIIO_CMBAT_GET_BIF:
-		acpi_cmbat_get_bif(dev);
+	        mtx_lock(&sc->acpi_cmbat_mtx);
+		if(!sc->bif_valid){
+		    acpi_cmbat_get_bif(dev);
+		}
+		mtx_unlock(&sc->acpi_cmbat_mtx);
 		bifp = &ioctl_arg->bif;
 		bifp->unit = sc->bif.unit;
 		bifp->dcap = sc->bif.dcap;
@@ -352,7 +365,11 @@
 		break;
 
 	case ACPIIO_CMBAT_GET_BST:
-		acpi_cmbat_get_bst(dev);
+	        mtx_lock(&sc->acpi_cmbat_mtx);
+		if(!sc->bst_valid){
+                    acpi_cmbat_get_bst(dev);
+		}
+		mtx_unlock(&sc->acpi_cmbat_mtx);
 		bstp = &ioctl_arg->bst;
 		bstp->state = sc->bst.state;
 		bstp->rate = sc->bst.rate;
diff -ruN /sys/dev/acpica/acpi_lid.c acpica/acpi_lid.c
--- /sys/dev/acpica/acpi_lid.c	Sun Jun 17 22:46:12 2001
+++ acpica/acpi_lid.c	Wed Jun 20 06:58:50 2001
@@ -36,7 +36,7 @@
 #include <sys/lock.h>
 #include <sys/mutex.h>
 #include <sys/proc.h>
-
+#include <dev/acpica/acpiio.h>
 #include "acpi.h"
 
 #include <dev/acpica/acpivar.h>
@@ -128,13 +128,14 @@
     if (acpi_sc == NULL) {
         return_VOID;
     }
-
+    acpi_knote(ACPI_KNOTE_LID);
+#if 0
     if (sc->lid_status == 0) {
 	EVENTHANDLER_INVOKE(acpi_sleep_event, acpi_sc->acpi_lid_switch_sx);
     } else {
 	EVENTHANDLER_INVOKE(acpi_wakeup_event, acpi_sc->acpi_lid_switch_sx);
     }
-
+#endif
     return_VOID;
 }
 
diff -ruN /sys/dev/acpica/acpi_thermal.c acpica/acpi_thermal.c
--- /sys/dev/acpica/acpi_thermal.c	Sun Jun 17 22:46:12 2001
+++ acpica/acpi_thermal.c	Wed Jun 20 07:01:38 2001
@@ -35,7 +35,7 @@
 #include "acpi.h"
 
 #include <dev/acpica/acpivar.h>
-
+#include <dev/acpica/acpiio.h>
 /*
  * Hooks for the ACPI CA debugging infrastructure
  */
@@ -133,6 +133,7 @@
 static void
 acpi_tz_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
 {
+    device_t dev = context;
     FUNCTION_TRACE(__func__);
 
     switch(notify){
@@ -140,6 +141,7 @@
     case ACPI_TZ_TRIPPOINT_CHANGE:
 	/*Check trip point*/
 	AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_tz_check_tripping_point, context);
+	acpi_knote(ACPI_KNOTE_THRM_START + device_get_unit(dev));
 	break;	
     }
     return_VOID;
diff -ruN /sys/dev/acpica/acpi_wake.c acpica/acpi_wake.c
--- /sys/dev/acpica/acpi_wake.c	Thu Jan  1 09:00:00 1970
+++ acpica/acpi_wake.c	Wed Jun 27 20:10:59 2001
@@ -0,0 +1,178 @@
+#include <sys/types.h>
+#include <sys/bus.h>
+#include <acpi.h>
+#include <machine/bus.h>
+#include <dev/acpica/acpivar.h>
+#include <sys/mutex.h>
+#include <vm/vm.h>
+#include <vm/vm_param.h>
+#include <vm/vm_kern.h>
+#include <vm/vm_page.h>
+#include <vm/vm_map.h>
+#include <vm/vm_extern.h>
+#include <vm/pmap.h>
+#include <sys/proc.h>
+#include "acpi_wake.h"
+
+static u_int32_t stackpointer = 0;
+struct {
+  short limit;
+  long base;
+}__attribute__((packed)) r_idt,r_gdt;
+static u_int16_t r_ldt;
+static u_int32_t r_eax,r_ebx,r_ecx,r_edx,r_ebp,r_esi,r_edi,r_efl,r_cr0,r_cr1,r_cr2,ret_addr;
+static short r_ss,r_ds,r_es,r_fs,r_gs,r_tr;
+static int acpi_savecpu(void) ;
+static int acpi_restorecpu(void);
+__asm__("
+   .text
+   .p2align 2,0x90
+   .type acpi_restorecpu,@function
+acpi_restorecpu:
+      .align 4
+      lidt r_idt
+      lgdt r_gdt   
+      jmp 1f
+1:    nop
+      movw $0x10,%ax
+      movw %ax,%ds
+      movw %ax,%ss
+      movw %ax,%es
+      movw %ax,%gs
+      movw $0x18,%ax
+      movw %ax,%fs
+      movl r_ebx,%ebx
+      movl r_ecx,%ecx
+      movl r_edx,%edx
+      movl r_ebp,%ebp
+      movl r_esi,%esi
+      movl r_edi,%edi
+      movl stackpointer,%esp
+      sti
+      lldt r_ldt 
+      movl r_eax,%eax
+      movl r_efl,%eax
+      pushl %eax
+      popfl
+      popl %eax
+      movl ret_addr,%eax
+      pushl %eax
+      xorl %eax,%eax
+      ret
+
+   .text
+   .p2align 2,0x90
+   .type acpi_savecpu,@function
+acpi_savecpu:
+      movw %ss,r_ss
+      movw %ds,r_ds
+      movw %es,r_es
+      movw %fs,r_fs
+      movw %gs,r_gs
+      movl %eax, r_eax
+      movl %ebx, r_ebx
+      movl %ecx, r_ecx
+      movl %edx, r_edx
+      movl %ebp, r_ebp
+      movl %esi, r_esi
+      movl %edi, r_edi
+      movl %cr0, %eax
+      movl %eax, r_cr0
+      movl %cr2, %eax
+      movl %eax, r_cr2
+      pushfl
+      popl %eax
+      mov %eax, r_efl
+      movl %esp,stackpointer
+      movl $1, %eax
+      sldt r_ldt 
+      popl %eax
+      mov %eax,ret_addr 
+      pushl %eax
+      ret
+");
+
+
+void 
+acpi_realmodeinst(void *arg,bus_dma_segment_t *segs, int nsegs, int error)
+{
+	struct acpi_softc *sc = arg;
+	u_int32_t *addr;
+	addr = &wakecode[sw32+2];
+	printf("%x\n",*addr);
+	*addr = segs[0].ds_addr+next;
+	printf("%p,%x\n",sc->acpi_wakeaddr,*addr);
+	bcopy(wakecode,sc->acpi_wakeaddr,sizeof(wakecode));
+	sc->acpi_wakephys = segs[0].ds_addr;
+	AcpiSetFirmwareWakingVector(segs[0].ds_addr);
+};
+
+int acpi_sleep(struct acpi_softc *sc ,int off_state)
+{
+	void ** addr;
+	int error;
+	void *oldphys;
+	struct pmap *pm;
+	vm_page_t page;
+	u_int32_t tmpval;
+	static vm_page_t opage=NULL;
+	pm = &curproc->p_vmspace->vm_pmap;
+	addr = (void **)&sc->acpi_wakeaddr[where_to_recover];
+	*addr = acpi_restorecpu;
+	printf("%p\n",*addr);
+	addr = (void **)&sc->acpi_wakeaddr[previous_cr3];
+	__asm__("mov %%cr3,%0":"=r"(*addr));
+	printf("CR3:%p\n",*addr);
+	addr = (void **)&sc->acpi_wakeaddr[previous_cr4];
+	__asm__("mov %%cr4,%0":"=r"(*addr));
+	printf("CR4:%p\n",*addr);
+	__asm__("sgdt %0":"=m"(r_gdt));
+	tmpval = r_gdt.base;
+	printf("%x:%x\n",r_gdt.base,r_gdt.limit);
+	r_gdt.base = vtophys(r_gdt.base);
+	printf("%x:%x\n",r_gdt.base,r_gdt.limit);
+	addr = (void **)&sc->acpi_wakeaddr[previous_gdt];
+	bcopy(&r_gdt,addr,sizeof(r_gdt));
+	r_gdt.base = tmpval;
+	__asm__("sidt %0":"=m"(r_idt));
+	addr = (void **)&sc->acpi_wakeaddr[previous_idt];
+	bcopy(&r_idt,addr,sizeof(r_idt));
+	/*Create Identity Mapping*/
+	oldphys = pmap_extract(pm,sc->acpi_wakephys);
+	if(oldphys)
+	  opage = PHYS_TO_VM_PAGE(oldphys);
+	page = PHYS_TO_VM_PAGE(sc->acpi_wakephys);
+	pmap_enter(pm,sc->acpi_wakephys,page,
+		   VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE,1);
+	if(acpi_savecpu()){
+	  printf("%x %x\n",r_ss,r_ds);
+	  AcpiEnterSleepState(off_state);
+	  while(1)
+	    ;
+	  /*Execute Sleep*/
+	}else{
+	  pmap_remove(pm,sc->acpi_wakephys,sc->acpi_wakephys+PAGE_SIZE);
+	  if(opage)
+	    pmap_enter(pm,sc->acpi_wakephys,page,VM_PROT_READ|VM_PROT_WRITE,0);
+	  printf("HELO\n");
+	  /*Execute Wakeup*/
+	}
+	return 0;
+}
+/*This should be MD*/
+int acpi_alloc_wakeup_handler(struct acpi_softc *sc)
+{
+	if( bus_dma_tag_create(/*parent*/ NULL,/*alignment*/2, 0,
+			       /*lowaddr*/0x9ffff,
+			       /*highaddr*/BUS_SPACE_MAXADDR,NULL,NULL,
+			       4096,1,4096,0,&sc->acpi_waketag) != 0){
+		device_printf(sc->acpi_dev,"unable to create wake tag\n");
+		return ENOMEM;
+	}
+	if(bus_dmamem_alloc(sc->acpi_waketag,(void **)&sc->acpi_wakeaddr,
+	    BUS_DMA_NOWAIT,&sc->acpi_wakemap)){
+		device_printf(sc->acpi_dev,"unable to alloc wake mem\n");
+		return ENOMEM;
+	}
+	return 0;
+}
diff -ruN /sys/dev/acpica/acpi_wakesrc.S acpica/acpi_wakesrc.S
--- /sys/dev/acpica/acpi_wakesrc.S	Thu Jan  1 09:00:00 1970
+++ acpica/acpi_wakesrc.S	Wed Jun 27 19:21:13 2001
@@ -0,0 +1,54 @@
+#define LOCORE	
+#include <machine/specialreg.h>
+#undef LOCORE
+#define BEEP	movb $0xc0,%al	; \
+		outb %al,$0x42	; \
+		movb $0x01,%al	; \
+		outb %al,$0x42	; \
+		inb  $0x61,%al	; \
+		orb $0x3,%al	; \
+		outb %al,$0x61	; \
+
+		.align 4
+		.code16
+start:          nop
+		cli
+		movw %cs,%ax
+		movw %ax,%ds		# Ensure CS=DS
+		movw %ax,%ss		# Ensure SS=DS
+		movw $0x1000,%sp		# LAST OF THE PAGE
+		lgdt previous_gdt	#
+		mov previous_cr3,%ebx
+		mov previous_cr4,%edx
+		mov where_to_recover,%ecx
+		mov %cr0,%eax
+		orl $(CR0_PE),%eax
+		mov %edx,%cr4
+		mov %ebx,%cr3
+		mov %eax,%cr0
+sw32:		ljmpl $0x8,$0x12345678  # Code location
+		.code32
+next:		nop
+		movw $0x10,%ax
+		movw %ax,%ss
+		movw %ax,%ds
+		movw %ax,%fs
+		movw %ax,%gs
+		mov %cr0,%eax
+		orl $(CR0_PE|CR0_PG),%eax
+		mov %eax,%cr0
+		jmp 1f
+1:		jmp 1f
+1:		nop
+		jmp *%ecx
+		
+previous_gdt:	.word 0,0,0
+previous_idt:	.word 0		#store IDTR
+		.long 0
+previous_cr3:	.long 0		#store PDTR
+previous_cr4:	.long 0		#store PDTR
+where_to_recover:	.long 0	#store Restore Address
+
+						
+
+
diff -ruN /sys/dev/acpica/acpiio.h acpica/acpiio.h
--- /sys/dev/acpica/acpiio.h	Mon May 21 00:51:46 2001
+++ acpica/acpiio.h	Wed Jun 20 06:57:55 2001
@@ -70,7 +70,17 @@
 
 #define ACPIIO_ACAD_GET_STATUS	_IOR('A', 1, int)
 
+#define ACPI_KNOTE_POWERBUTTON 0
+#define ACPI_KNOTE_SLEEPBUTTON 1
+#define ACPI_KNOTE_LID         2
+#define ACPI_KNOTE_ACAD        3
+#define ACPI_KNOTE_THRM_START  8
+#define ACPI_KNOTE_BAT_START   16
+
+
 #ifdef _KERNEL
 extern int	acpi_register_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg), void *arg);
 extern void	acpi_deregister_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg));
 #endif
+/*Kqueue releated stuff (1<<bit) represents the value*/
+
diff -ruN /sys/dev/acpica/acpivar.h acpica/acpivar.h
--- /sys/dev/acpica/acpivar.h	Sun Jun 17 22:46:12 2001
+++ acpica/acpivar.h	Wed Jun 20 21:16:46 2001
@@ -31,6 +31,10 @@
 #include "bus_if.h"
 #include <sys/eventhandler.h>
 #include <sys/sysctl.h>
+#include <machine/bus.h>
+#include <sys/mutex.h>
+#include <sys/event.h>
+
 
 extern devclass_t	acpi_devclass;
 
@@ -53,6 +57,13 @@
     int			acpi_power_button_sx;
     int			acpi_sleep_button_sx;
     int			acpi_lid_switch_sx;
+    bus_dma_tag_t       acpi_waketag;
+    bus_dmamap_t       acpi_wakemap;
+    char               *acpi_wakeaddr;
+    char               *acpi_wakephys;
+    struct  klist      acpi_klist;
+    struct  mtx      acpi_mtx;
+    int                acpi_filter;
 };
 
 struct acpi_device {
@@ -171,7 +182,9 @@
 extern ACPI_STATUS	acpi_Disable(struct acpi_softc *sc);
 extern BOOLEAN		acpi_DeviceIsPresent(device_t dev);
 extern ACPI_STATUS	acpi_EvaluateInteger(ACPI_HANDLE handle, char *path, int *number);
-
+extern void acpi_realmodeinst(void *arg,bus_dma_segment_t *segs, int nsegs, int error);
+extern int acpi_sleep(struct acpi_softc *sc,int state);
+extern int acpi_alloc_wakeup_handler(struct acpi_softc *sc);
 struct acpi_parse_resource_set {
     void	(* set_init)(device_t dev, void **context);
     void	(* set_done)(device_t dev, void *context);
@@ -229,4 +242,4 @@
 extern char	*acpi_name(ACPI_HANDLE handle);
 extern int	acpi_avoid(ACPI_HANDLE handle);
 extern int	acpi_disabled(char *subsys);
-
+extern void     acpi_knote(int filter);
diff -ruN /sys/dev/acpica/bin2c.c acpica/bin2c.c
--- /sys/dev/acpica/bin2c.c	Thu Jan  1 09:00:00 1970
+++ acpica/bin2c.c	Sat Jun  9 21:04:35 2001
@@ -0,0 +1,36 @@
+#include <stdio.h>
+
+main(int argc,char *argv[])
+{
+	int count = 0;
+	int c;
+	FILE *fp;
+	int x;
+	char symbol[50],line[80];
+	if(argc<3){
+	  exit(-1);
+	}
+	if((fp=fopen(argv[1],"r"))==NULL){
+	  exit(-1);
+	}
+	printf("static char wakecode[] = {");
+	while((c = fgetc(fp)) != EOF){
+		count ++;
+		printf("0x%x ,",c);
+	}
+	printf("};\n",count);
+	fclose(fp);
+	if((fp=fopen(argv[2],"r"))==NULL){
+	  exit(-1);
+	}
+	for(;fgets(line,80,fp)!=NULL;){
+	    count=sscanf(line,"%x %c %50s",&x,&c,symbol);
+	    if(count==3){
+	      printf("#define %s 0x%x\n",symbol,x);
+	    }else if(count==-1){
+	      break;
+	    }
+	}
+	return 0;
+}
+
