apm.c revision 47625
1/*
2 * APM (Advanced Power Management) BIOS Device Driver
3 *
4 * Copyright (c) 1994 UKAI, Fumitoshi.
5 * Copyright (c) 1994-1995 by HOSOKAWA, Tatsumi <hosokawa@jp.FreeBSD.org>
6 * Copyright (c) 1996 Nate Williams <nate@FreeBSD.org>
7 * Copyright (c) 1997 Poul-Henning Kamp <phk@FreeBSD.org>
8 *
9 * This software may be used, modified, copied, and distributed, in
10 * both source and binary form provided that the above copyright and
11 * these terms are retained. Under no circumstances is the author
12 * responsible for the proper functioning of this software, nor does
13 * the author assume any responsibility for damages incurred with its
14 * use.
15 *
16 * Sep, 1994	Implemented on FreeBSD 1.1.5.1R (Toshiba AVS001WD)
17 *
18 *	$Id: apm.c,v 1.86 1999/05/11 19:54:03 phk Exp $
19 */
20
21#include "opt_devfs.h"
22#include "opt_vm86.h"
23#include "opt_smp.h"
24
25#include <sys/param.h>
26#include <sys/systm.h>
27#include <sys/conf.h>
28#include <sys/kernel.h>
29#ifdef DEVFS
30#include <sys/devfsext.h>
31#endif /*DEVFS*/
32#include <sys/time.h>
33#include <sys/reboot.h>
34#include <sys/bus.h>
35#include <machine/apm_bios.h>
36#include <machine/segments.h>
37#include <machine/clock.h>
38#include <vm/vm.h>
39#include <vm/vm_param.h>
40#include <vm/pmap.h>
41#include <sys/syslog.h>
42#include <i386/apm/apm_setup.h>
43
44#ifdef VM86
45#include <machine/psl.h>
46#include <machine/vm86.h>
47#endif
48
49#ifdef SMP
50#include <machine/smp.h>
51#endif
52
53static int apm_display __P((int newstate));
54static int apm_int __P((u_long *eax, u_long *ebx, u_long *ecx, u_long *edx));
55static void apm_resume __P((void));
56
57/* static data */
58struct apm_softc {
59	int	initialized, active;
60	int	always_halt_cpu, slow_idle_cpu;
61	int	disabled, disengaged;
62	u_int	minorversion, majorversion;
63	u_int	cs32_base, cs16_base, ds_base;
64	u_int	cs16_limit, cs32_limit, ds_limit;
65	u_int	cs_entry;
66	u_int	intversion;
67	struct apmhook sc_suspend;
68	struct apmhook sc_resume;
69#ifdef DEVFS
70	void 	*sc_devfs_token;
71#endif
72};
73
74static struct apm_softc apm_softc;
75static struct apmhook	*hook[NAPM_HOOK];		/* XXX */
76
77#define is_enabled(foo) ((foo) ? "enabled" : "disabled")
78
79/* Map version number to integer (keeps ordering of version numbers) */
80#define INTVERSION(major, minor)	((major)*100 + (minor))
81
82static struct callout_handle apm_timeout_ch =
83    CALLOUT_HANDLE_INITIALIZER(&apm_timeout_ch);
84
85static timeout_t apm_timeout;
86static d_open_t apmopen;
87static d_close_t apmclose;
88static d_ioctl_t apmioctl;
89
90#define CDEV_MAJOR 39
91static struct cdevsw apm_cdevsw = {
92	/* open */	apmopen,
93	/* close */	apmclose,
94	/* read */	noread,
95	/* write */	nowrite,
96	/* ioctl */	apmioctl,
97	/* stop */	nostop,
98	/* reset */	noreset,
99	/* devtotty */	nodevtotty,
100	/* poll */	nopoll,
101	/* mmap */	nommap,
102	/* strategy */	nostrategy,
103	/* name */	"apm",
104	/* parms */	noparms,
105	/* maj */	CDEV_MAJOR,
106	/* dump */	nodump,
107	/* psize */	nopsize,
108	/* flags */	0,
109	/* maxio */	0,
110	/* bmaj */	-1
111};
112
113/* setup APM GDT discriptors */
114static void
115setup_apm_gdt(u_int code32_base, u_int code16_base, u_int data_base, u_int code32_limit, u_int code16_limit, u_int data_limit)
116{
117#ifdef SMP
118	int x;
119#endif
120
121	/* setup 32bit code segment */
122	gdt_segs[GAPMCODE32_SEL].ssd_base  = code32_base;
123	gdt_segs[GAPMCODE32_SEL].ssd_limit = code32_limit;
124
125	/* setup 16bit code segment */
126	gdt_segs[GAPMCODE16_SEL].ssd_base  = code16_base;
127	gdt_segs[GAPMCODE16_SEL].ssd_limit = code16_limit;
128
129	/* setup data segment */
130	gdt_segs[GAPMDATA_SEL  ].ssd_base  = data_base;
131	gdt_segs[GAPMDATA_SEL  ].ssd_limit = data_limit;
132
133	/* reflect these changes on physical GDT */
134	ssdtosd(gdt_segs + GAPMCODE32_SEL, &gdt[GAPMCODE32_SEL].sd);
135	ssdtosd(gdt_segs + GAPMCODE16_SEL, &gdt[GAPMCODE16_SEL].sd);
136	ssdtosd(gdt_segs + GAPMDATA_SEL  , &gdt[GAPMDATA_SEL  ].sd);
137
138#ifdef SMP
139	for (x = 1; x < NCPU; x++) {
140		gdt[x * NGDT + GAPMCODE32_SEL].sd = gdt[GAPMCODE32_SEL].sd;
141		gdt[x * NGDT + GAPMCODE16_SEL].sd = gdt[GAPMCODE16_SEL].sd;
142		gdt[x * NGDT + GAPMDATA_SEL  ].sd = gdt[GAPMDATA_SEL  ].sd;
143	}
144#endif
145}
146
147/* 48bit far pointer. Do not staticize - used from apm_setup.s */
148struct addr48 {
149	u_long		offset;
150	u_short		segment;
151} apm_addr;
152
153static int apm_errno;
154
155static int
156apm_int(u_long *eax, u_long *ebx, u_long *ecx, u_long *edx)
157{
158	struct apm_bios_arg apa;
159	int cf;
160
161	apa.eax = *eax;
162	apa.ebx = *ebx;
163	apa.ecx = *ecx;
164	apa.edx = *edx;
165	cf = apm_bios_call(&apa);
166	*eax = apa.eax;
167	*ebx = apa.ebx;
168	*ecx = apa.ecx;
169	*edx = apa.edx;
170	apm_errno = ((*eax) >> 8) & 0xff;
171	return cf;
172}
173
174
175/* enable/disable power management */
176static int
177apm_enable_disable_pm(int enable)
178{
179	struct apm_softc *sc = &apm_softc;
180
181	u_long eax, ebx, ecx, edx;
182
183	eax = (APM_BIOS << 8) | APM_ENABLEDISABLEPM;
184
185	if (sc->intversion >= INTVERSION(1, 1))
186		ebx  = PMDV_ALLDEV;
187	else
188		ebx  = 0xffff;	/* APM version 1.0 only */
189	ecx  = enable;
190	edx = 0;
191	return apm_int(&eax, &ebx, &ecx, &edx);
192}
193
194static void
195apm_driver_version(int version)
196{
197	u_long eax, ebx, ecx, edx;
198
199	/* First try APM 1.2 */
200	eax = (APM_BIOS << 8) | APM_DRVVERSION;
201	ebx  = 0x0;
202	ecx  = version;
203	edx = 0;
204	if(!apm_int(&eax, &ebx, &ecx, &edx))
205		apm_version = eax & 0xffff;
206}
207
208/* engage/disengage power management (APM 1.1 or later) */
209static int
210apm_engage_disengage_pm(int engage)
211{
212	u_long eax, ebx, ecx, edx;
213
214	eax = (APM_BIOS << 8) | APM_ENGAGEDISENGAGEPM;
215	ebx = PMDV_ALLDEV;
216	ecx = engage;
217	edx = 0;
218	return(apm_int(&eax, &ebx, &ecx, &edx));
219}
220
221/* get PM event */
222static u_int
223apm_getevent(void)
224{
225	u_long eax, ebx, ecx, edx;
226
227	eax = (APM_BIOS << 8) | APM_GETPMEVENT;
228
229	ebx = 0;
230	ecx = 0;
231	edx = 0;
232	if (apm_int(&eax, &ebx, &ecx, &edx))
233		return PMEV_NOEVENT;
234
235	return ebx & 0xffff;
236}
237
238/* suspend entire system */
239static int
240apm_suspend_system(int state)
241{
242	u_long eax, ebx, ecx, edx;
243
244	eax = (APM_BIOS << 8) | APM_SETPWSTATE;
245	ebx = PMDV_ALLDEV;
246	ecx = state;
247	edx = 0;
248
249	if (apm_int(&eax, &ebx, &ecx, &edx)) {
250		printf("Entire system suspend failure: errcode = %ld\n",
251			0xff & (eax >> 8));
252		return 1;
253	}
254	return 0;
255}
256
257/* Display control */
258/*
259 * Experimental implementation: My laptop machine can't handle this function
260 * If your laptop can control the display via APM, please inform me.
261 *                            HOSOKAWA, Tatsumi <hosokawa@jp.FreeBSD.org>
262 */
263static int
264apm_display(int newstate)
265{
266	u_long eax, ebx, ecx, edx;
267
268	eax = (APM_BIOS << 8) | APM_SETPWSTATE;
269	ebx = PMDV_DISP0;
270	ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND;
271	edx = 0;
272	if (apm_int(&eax, &ebx, &ecx, &edx)) {
273		printf("Display off failure: errcode = %ld\n",
274			0xff & (eax >> 8));
275		return 1;
276	}
277	return 0;
278}
279
280/*
281 * Turn off the entire system.
282 */
283static void
284apm_power_off(int howto, void *junk)
285{
286	u_long eax, ebx, ecx, edx;
287
288	/* Not halting powering off, or not active */
289	if (!(howto & RB_POWEROFF) || !apm_softc.active)
290		return;
291	eax = (APM_BIOS << 8) | APM_SETPWSTATE;
292	ebx = PMDV_ALLDEV;
293	ecx = PMST_OFF;
294	edx = 0;
295	apm_int(&eax, &ebx, &ecx, &edx);
296}
297
298/* APM Battery low handler */
299static void
300apm_battery_low(void)
301{
302	printf("\007\007 * * * BATTERY IS LOW * * * \007\007");
303}
304
305/* APM hook manager */
306static struct apmhook *
307apm_add_hook(struct apmhook **list, struct apmhook *ah)
308{
309	int s;
310	struct apmhook *p, *prev;
311
312#ifdef APM_DEBUG
313	printf("Add hook \"%s\"\n", ah->ah_name);
314#endif
315
316	s = splhigh();
317	if (ah == NULL)
318		panic("illegal apm_hook!");
319	prev = NULL;
320	for (p = *list; p != NULL; prev = p, p = p->ah_next)
321		if (p->ah_order > ah->ah_order)
322			break;
323
324	if (prev == NULL) {
325		ah->ah_next = *list;
326		*list = ah;
327	} else {
328		ah->ah_next = prev->ah_next;
329		prev->ah_next = ah;
330	}
331	splx(s);
332	return ah;
333}
334
335static void
336apm_del_hook(struct apmhook **list, struct apmhook *ah)
337{
338	int s;
339	struct apmhook *p, *prev;
340
341	s = splhigh();
342	prev = NULL;
343	for (p = *list; p != NULL; prev = p, p = p->ah_next)
344		if (p == ah)
345			goto deleteit;
346	panic("Tried to delete unregistered apm_hook.");
347	goto nosuchnode;
348deleteit:
349	if (prev != NULL)
350		prev->ah_next = p->ah_next;
351	else
352		*list = p->ah_next;
353nosuchnode:
354	splx(s);
355}
356
357
358/* APM driver calls some functions automatically */
359static void
360apm_execute_hook(struct apmhook *list)
361{
362	struct apmhook *p;
363
364	for (p = list; p != NULL; p = p->ah_next) {
365#ifdef APM_DEBUG
366		printf("Execute APM hook \"%s.\"\n", p->ah_name);
367#endif
368		if ((*(p->ah_fun))(p->ah_arg))
369			printf("Warning: APM hook \"%s\" failed", p->ah_name);
370	}
371}
372
373
374/* establish an apm hook */
375struct apmhook *
376apm_hook_establish(int apmh, struct apmhook *ah)
377{
378	if (apmh < 0 || apmh >= NAPM_HOOK)
379		return NULL;
380
381	return apm_add_hook(&hook[apmh], ah);
382}
383
384/* disestablish an apm hook */
385void
386apm_hook_disestablish(int apmh, struct apmhook *ah)
387{
388	if (apmh < 0 || apmh >= NAPM_HOOK)
389		return;
390
391	apm_del_hook(&hook[apmh], ah);
392}
393
394
395static struct timeval suspend_time;
396static struct timeval diff_time;
397
398static int
399apm_default_resume(void *arg)
400{
401	int pl;
402	u_int second, minute, hour;
403	struct timeval resume_time, tmp_time;
404
405	/* modified for adjkerntz */
406	pl = splsoftclock();
407	inittodr(0);			/* adjust time to RTC */
408	microtime(&resume_time);
409	getmicrotime(&tmp_time);
410	timevaladd(&tmp_time, &diff_time);
411
412#ifdef FIXME
413	/* XXX THIS DOESN'T WORK!!! */
414	time = tmp_time;
415#endif
416
417#ifdef APM_FIXUP_CALLTODO
418	/* Calculate the delta time suspended */
419	timevalsub(&resume_time, &suspend_time);
420	/* Fixup the calltodo list with the delta time. */
421	adjust_timeout_calltodo(&resume_time);
422#endif /* APM_FIXUP_CALLTODOK */
423	splx(pl);
424#ifndef APM_FIXUP_CALLTODO
425	second = resume_time.tv_sec - suspend_time.tv_sec;
426#else /* APM_FIXUP_CALLTODO */
427	/*
428	 * We've already calculated resume_time to be the delta between
429	 * the suspend and the resume.
430	 */
431	second = resume_time.tv_sec;
432#endif /* APM_FIXUP_CALLTODO */
433	hour = second / 3600;
434	second %= 3600;
435	minute = second / 60;
436	second %= 60;
437	log(LOG_NOTICE, "resumed from suspended mode (slept %02d:%02d:%02d)\n",
438		hour, minute, second);
439	return 0;
440}
441
442static int
443apm_default_suspend(void *arg)
444{
445	int	pl;
446
447	pl = splsoftclock();
448	microtime(&diff_time);
449	inittodr(0);
450	microtime(&suspend_time);
451	timevalsub(&diff_time, &suspend_time);
452	splx(pl);
453	return 0;
454}
455
456static void apm_processevent(void);
457
458/*
459 * Public interface to the suspend/resume:
460 *
461 * Execute suspend and resume hook before and after sleep, respectively.
462 *
463 */
464
465void
466apm_suspend(int state)
467{
468	struct apm_softc *sc = &apm_softc;
469	int error;
470
471	if (!sc)
472		return;
473
474	if (sc->initialized) {
475		error = DEVICE_SUSPEND(root_bus);
476		/*
477		 * XXX Shouldn't ignore the error like this, but should
478		 * instead fix the newbus code.  Until that happens,
479		 * I'm doing this to get suspend working again.
480		 */
481		if (error)
482			printf("DEVICE_SUSPEND error %d, ignored\n", error);
483		apm_execute_hook(hook[APM_HOOK_SUSPEND]);
484		if (apm_suspend_system(state) == 0)
485			apm_processevent();
486		else
487			/* Failure, 'resume' the system again */
488			apm_execute_hook(hook[APM_HOOK_RESUME]);
489	}
490}
491
492void
493apm_resume(void)
494{
495	struct apm_softc *sc = &apm_softc;
496
497	if (!sc)
498		return;
499
500	if (sc->initialized) {
501		DEVICE_RESUME(root_bus);
502		apm_execute_hook(hook[APM_HOOK_RESUME]);
503	}
504}
505
506
507/* get APM information */
508static int
509apm_get_info(apm_info_t aip)
510{
511	struct apm_softc *sc = &apm_softc;
512	u_long eax, ebx, ecx, edx;
513
514	eax = (APM_BIOS << 8) | APM_GETPWSTATUS;
515	ebx = PMDV_ALLDEV;
516	ecx = 0;
517	edx = 0xffff;			/* default to unknown battery time */
518
519	if (apm_int(&eax, &ebx, &ecx, &edx))
520		return 1;
521
522	aip->ai_infoversion = 1;
523	aip->ai_acline      = (ebx >> 8) & 0xff;
524	aip->ai_batt_stat   = ebx & 0xff;
525	aip->ai_batt_life   = ecx & 0xff;
526	aip->ai_major       = (u_int)sc->majorversion;
527	aip->ai_minor       = (u_int)sc->minorversion;
528	aip->ai_status      = (u_int)sc->active;
529	edx &= 0xffff;
530	if (edx == 0xffff)	/* Time is unknown */
531		aip->ai_batt_time = -1;
532	else if (edx & 0x8000)	/* Time is in minutes */
533		aip->ai_batt_time = (edx & 0x7fff) * 60;
534	else			/* Time is in seconds */
535		aip->ai_batt_time = edx;
536
537	eax = (APM_BIOS << 8) | APM_GETCAPABILITIES;
538	ebx = 0;
539	ecx = 0;
540	edx = 0;
541	if (apm_int(&eax, &ebx, &ecx, &edx)) {
542		aip->ai_batteries = -1;	/* Unknown */
543		aip->ai_capabilities = 0xff00; /* Unknown, with no bits set */
544	} else {
545		aip->ai_batteries = ebx & 0xff;
546		aip->ai_capabilities = ecx & 0xf;
547	}
548
549	bzero(aip->ai_spare, sizeof aip->ai_spare);
550
551	return 0;
552}
553
554
555/* inform APM BIOS that CPU is idle */
556void
557apm_cpu_idle(void)
558{
559	struct apm_softc *sc = &apm_softc;
560
561	if (sc->active) {
562		u_long eax, ebx, ecx, edx;
563
564		eax = (APM_BIOS <<8) | APM_CPUIDLE;
565		edx = ecx = ebx = 0;
566		apm_int(&eax, &ebx, &ecx, &edx);
567	}
568	/*
569	 * Some APM implementation halts CPU in BIOS, whenever
570	 * "CPU-idle" function are invoked, but swtch() of
571	 * FreeBSD halts CPU, therefore, CPU is halted twice
572	 * in the sched loop. It makes the interrupt latency
573	 * terribly long and be able to cause a serious problem
574	 * in interrupt processing. We prevent it by removing
575	 * "hlt" operation from swtch() and managed it under
576	 * APM driver.
577	 */
578	if (!sc->active || sc->always_halt_cpu)
579		__asm("hlt");	/* wait for interrupt */
580}
581
582/* inform APM BIOS that CPU is busy */
583void
584apm_cpu_busy(void)
585{
586	struct apm_softc *sc = &apm_softc;
587
588	/*
589	 * The APM specification says this is only necessary if your BIOS
590	 * slows down the processor in the idle task, otherwise it's not
591	 * necessary.
592	 */
593	if (sc->slow_idle_cpu && sc->active) {
594		u_long eax, ebx, ecx, edx;
595
596		eax = (APM_BIOS <<8) | APM_CPUBUSY;
597		edx = ecx = ebx = 0;
598		apm_int(&eax, &ebx, &ecx, &edx);
599	}
600}
601
602
603/*
604 * APM timeout routine:
605 *
606 * This routine is automatically called by timer once per second.
607 */
608
609static void
610apm_timeout(void *dummy)
611{
612	struct apm_softc *sc = &apm_softc;
613
614	apm_processevent();
615	if (sc->active == 1)
616		/* Run slightly more oftan than 1 Hz */
617		apm_timeout_ch = timeout(apm_timeout, NULL, hz - 1 );
618}
619
620/* enable APM BIOS */
621static void
622apm_event_enable(void)
623{
624	struct apm_softc *sc = &apm_softc;
625
626#ifdef APM_DEBUG
627	printf("called apm_event_enable()\n");
628#endif
629	if (sc->initialized) {
630		sc->active = 1;
631		apm_timeout(sc);
632	}
633}
634
635/* disable APM BIOS */
636static void
637apm_event_disable(void)
638{
639	struct apm_softc *sc = &apm_softc;
640
641#ifdef APM_DEBUG
642	printf("called apm_event_disable()\n");
643#endif
644	if (sc->initialized) {
645		untimeout(apm_timeout, NULL, apm_timeout_ch);
646		sc->active = 0;
647	}
648}
649
650/* halt CPU in scheduling loop */
651static void
652apm_halt_cpu(void)
653{
654	struct apm_softc *sc = &apm_softc;
655
656	if (sc->initialized)
657		sc->always_halt_cpu = 1;
658}
659
660/* don't halt CPU in scheduling loop */
661static void
662apm_not_halt_cpu(void)
663{
664	struct apm_softc *sc = &apm_softc;
665
666	if (sc->initialized)
667		sc->always_halt_cpu = 0;
668}
669
670/* device driver definitions */
671
672/*
673 * probe APM (dummy):
674 *
675 * APM probing routine is placed on locore.s and apm_init.S because
676 * this process forces the CPU to turn to real mode or V86 mode.
677 * Current version uses real mode, but in a future version, we want
678 * to use V86 mode in APM initialization.
679 *
680 * XXX If VM86 is defined, we do.
681 */
682
683static int
684apm_probe(device_t dev)
685{
686#ifdef VM86
687	struct vm86frame	vmf;
688	int			i;
689#endif
690	int			disabled, flags;
691
692	if (resource_int_value("apm", 0, "disabled", &disabled) == 0
693	    && disabled != 0)
694		return ENXIO;
695
696	device_set_desc(dev, "APM BIOS");
697
698	if ( device_get_unit(dev) > 0 ) {
699		printf("apm: Only one APM driver supported.\n");
700		return ENXIO;
701	}
702
703	if (resource_int_value("apm", 0, "flags", &flags) != 0)
704		flags = 0;
705
706#ifdef VM86
707	bzero(&vmf, sizeof(struct vm86frame));		/* safety */
708	vmf.vmf_ax = (APM_BIOS << 8) | APM_INSTCHECK;
709	vmf.vmf_bx = 0;
710	if (((i = vm86_intcall(SYSTEM_BIOS, &vmf)) == 0) &&
711	    !(vmf.vmf_eflags & PSL_C) &&
712	    (vmf.vmf_bx == 0x504d)) {
713
714		apm_version   = vmf.vmf_ax;
715		apm_flags     = vmf.vmf_cx;
716
717		vmf.vmf_ax = (APM_BIOS << 8) | APM_PROT32CONNECT;
718		vmf.vmf_bx = 0;
719		if (((i = vm86_intcall(SYSTEM_BIOS, &vmf)) == 0) &&
720		    !(vmf.vmf_eflags & PSL_C)) {
721
722			apm_cs32_base = vmf.vmf_ax;
723			apm_cs_entry  = vmf.vmf_ebx;
724			apm_cs16_base = vmf.vmf_cx;
725			apm_ds_base   = vmf.vmf_dx;
726			apm_cs32_limit  = vmf.vmf_si;
727			if (apm_version >= 0x0102)
728				apm_cs16_limit = (vmf.esi.r_ex >> 16);
729			apm_ds_limit  = vmf.vmf_di;
730#ifdef APM_DEBUG
731			printf("apm: BIOS probe/32-bit connect successful\n");
732#endif
733		} else {
734			/* XXX constant typo! */
735			if (vmf.vmf_ah == APME_PROT32NOTDUPPORTED) {
736				apm_version = APMINI_NOT32BIT;
737			} else {
738				apm_version = APMINI_CONNECTERR;
739			}
740#ifdef APM_DEBUG
741			printf("apm: BIOS 32-bit connect failed: error 0x%x  carry %d  ah 0x%x\n",
742			       i, (vmf.vmf_eflags & PSL_C) ? 1 : 0, vmf.vmf_ah);
743#endif
744		}
745	} else {
746		apm_version = APMINI_CANTFIND;
747#ifdef APM_DEBUG
748		printf("apm: BIOS probe failed: error 0x%x  carry %d  bx 0x%x\n",
749		       i, (vmf.vmf_eflags & PSL_C) ? 1 : 0, vmf.vmf_bx);
750#endif
751	}
752#endif
753
754	bzero(&apm_softc, sizeof(apm_softc));
755
756	switch (apm_version) {
757	case APMINI_CANTFIND:
758		/* silent */
759		return ENXIO;
760	case APMINI_NOT32BIT:
761		printf("apm: 32bit connection is not supported.\n");
762		return ENXIO;
763	case APMINI_CONNECTERR:
764		printf("apm: 32-bit connection error.\n");
765		return ENXIO;
766	}
767	if (flags & 0x20)
768		statclock_disable = 1;
769	return 0;
770}
771
772
773/* Process APM event */
774static void
775apm_processevent(void)
776{
777	int apm_event;
778
779#ifdef APM_DEBUG
780#  define OPMEV_DEBUGMESSAGE(symbol) case symbol: \
781	printf("Received APM Event: " #symbol "\n");
782#else
783#  define OPMEV_DEBUGMESSAGE(symbol) case symbol:
784#endif
785	do {
786		apm_event = apm_getevent();
787		switch (apm_event) {
788		    OPMEV_DEBUGMESSAGE(PMEV_STANDBYREQ);
789			apm_suspend(PMST_STANDBY);
790			break;
791		    OPMEV_DEBUGMESSAGE(PMEV_SUSPENDREQ);
792			apm_suspend(PMST_SUSPEND);
793			break;
794		    OPMEV_DEBUGMESSAGE(PMEV_USERSUSPENDREQ);
795			apm_suspend(PMST_SUSPEND);
796			break;
797		    OPMEV_DEBUGMESSAGE(PMEV_CRITSUSPEND);
798			apm_suspend(PMST_SUSPEND);
799			break;
800		    OPMEV_DEBUGMESSAGE(PMEV_NORMRESUME);
801			apm_resume();
802			break;
803		    OPMEV_DEBUGMESSAGE(PMEV_CRITRESUME);
804			apm_resume();
805			break;
806		    OPMEV_DEBUGMESSAGE(PMEV_STANDBYRESUME);
807			apm_resume();
808			break;
809		    OPMEV_DEBUGMESSAGE(PMEV_BATTERYLOW);
810			apm_battery_low();
811			apm_suspend(PMST_SUSPEND);
812			break;
813		    OPMEV_DEBUGMESSAGE(PMEV_POWERSTATECHANGE);
814			break;
815		    OPMEV_DEBUGMESSAGE(PMEV_UPDATETIME);
816			inittodr(0);	/* adjust time to RTC */
817			break;
818		    case PMEV_NOEVENT:
819			break;
820		    default:
821			printf("Unknown Original APM Event 0x%x\n", apm_event);
822			    break;
823		}
824	} while (apm_event != PMEV_NOEVENT);
825}
826
827/*
828 * Attach APM:
829 *
830 * Initialize APM driver (APM BIOS itself has been initialized in locore.s)
831 */
832
833static int
834apm_attach(device_t dev)
835{
836#define APM_KERNBASE	KERNBASE
837	struct apm_softc	*sc = &apm_softc;
838	int			flags;
839
840	if (resource_int_value("apm", 0, "flags", &flags) != 0)
841		flags = 0;
842
843	sc->initialized = 0;
844
845	/* Must be externally enabled */
846	sc->active = 0;
847
848	/* setup APM parameters */
849	sc->cs16_base = (apm_cs16_base << 4) + APM_KERNBASE;
850	sc->cs32_base = (apm_cs32_base << 4) + APM_KERNBASE;
851	sc->ds_base = (apm_ds_base << 4) + APM_KERNBASE;
852	sc->cs32_limit = apm_cs32_limit - 1;
853	if (apm_cs16_limit == 0)
854	    apm_cs16_limit = apm_cs32_limit;
855	sc->cs16_limit = apm_cs16_limit - 1;
856	sc->ds_limit = apm_ds_limit - 1;
857	sc->cs_entry = apm_cs_entry;
858
859	/* Always call HLT in idle loop */
860	sc->always_halt_cpu = 1;
861
862	sc->slow_idle_cpu = ((apm_flags & APM_CPUIDLE_SLOW) != 0);
863	sc->disabled = ((apm_flags & APM_DISABLED) != 0);
864	sc->disengaged = ((apm_flags & APM_DISENGAGED) != 0);
865
866	/* print bootstrap messages */
867#ifdef APM_DEBUG
868	printf("apm: APM BIOS version %04x\n",  apm_version);
869	printf("apm: Code32 0x%08x, Code16 0x%08x, Data 0x%08x\n",
870		sc->cs32_base, sc->cs16_base, sc->ds_base);
871	printf("apm: Code entry 0x%08x, Idling CPU %s, Management %s\n",
872		sc->cs_entry, is_enabled(sc->slow_idle_cpu),
873		is_enabled(!sc->disabled));
874	printf("apm: CS32_limit=0x%x, CS16_limit=0x%x, DS_limit=0x%x\n",
875		(u_short)sc->cs32_limit, (u_short)sc->cs16_limit, (u_short)sc->ds_limit);
876#endif /* APM_DEBUG */
877
878#if 0
879	/* Workaround for some buggy APM BIOS implementations */
880	sc->cs_limit = 0xffff;
881	sc->ds_limit = 0xffff;
882#endif
883
884	/* setup GDT */
885	setup_apm_gdt(sc->cs32_base, sc->cs16_base, sc->ds_base,
886			sc->cs32_limit, sc->cs16_limit, sc->ds_limit);
887
888	/* setup entry point 48bit pointer */
889	apm_addr.segment = GSEL(GAPMCODE32_SEL, SEL_KPL);
890	apm_addr.offset  = sc->cs_entry;
891
892	if ((flags & 0x10)) {
893		if ((flags & 0xf) >= 0x2) {
894			apm_driver_version(0x102);
895		}
896		if (!apm_version && (flags & 0xf) >= 0x1) {
897			apm_driver_version(0x101);
898		}
899	} else {
900		apm_driver_version(0x102);
901		if (!apm_version)
902			apm_driver_version(0x101);
903	}
904	if (!apm_version)
905		apm_version = 0x100;
906
907	sc->minorversion = ((apm_version & 0x00f0) >>  4) * 10 +
908			((apm_version & 0x000f) >> 0);
909	sc->majorversion = ((apm_version & 0xf000) >> 12) * 10 +
910			((apm_version & 0x0f00) >> 8);
911
912	sc->intversion = INTVERSION(sc->majorversion, sc->minorversion);
913
914#ifdef APM_DEBUG
915	if (sc->intversion >= INTVERSION(1, 1))
916		printf("apm: Engaged control %s\n", is_enabled(!sc->disengaged));
917#endif
918
919	printf("apm: found APM BIOS version %d.%d\n",
920		sc->majorversion, sc->minorversion);
921
922#ifdef APM_DEBUG
923	printf("apm: Slow Idling CPU %s\n", is_enabled(sc->slow_idle_cpu));
924#endif
925
926	/* enable power management */
927	if (sc->disabled) {
928		if (apm_enable_disable_pm(1)) {
929#ifdef APM_DEBUG
930			printf("apm: *Warning* enable function failed! [%x]\n",
931				apm_errno);
932#endif
933		}
934	}
935
936	/* engage power managment (APM 1.1 or later) */
937	if (sc->intversion >= INTVERSION(1, 1) && sc->disengaged) {
938		if (apm_engage_disengage_pm(1)) {
939#ifdef APM_DEBUG
940			printf("apm: *Warning* engage function failed err=[%x]",
941				apm_errno);
942			printf(" (Docked or using external power?).\n");
943#endif
944		}
945	}
946
947        /* default suspend hook */
948        sc->sc_suspend.ah_fun = apm_default_suspend;
949        sc->sc_suspend.ah_arg = sc;
950        sc->sc_suspend.ah_name = "default suspend";
951        sc->sc_suspend.ah_order = APM_MAX_ORDER;
952
953        /* default resume hook */
954        sc->sc_resume.ah_fun = apm_default_resume;
955        sc->sc_resume.ah_arg = sc;
956        sc->sc_resume.ah_name = "default resume";
957        sc->sc_resume.ah_order = APM_MIN_ORDER;
958
959        apm_hook_establish(APM_HOOK_SUSPEND, &sc->sc_suspend);
960        apm_hook_establish(APM_HOOK_RESUME , &sc->sc_resume);
961
962	apm_event_enable();
963
964	/* Power the system off using APM */
965	at_shutdown_pri(apm_power_off, NULL, SHUTDOWN_FINAL, SHUTDOWN_PRI_LAST);
966
967	sc->initialized = 1;
968
969#ifdef DEVFS
970	sc->sc_devfs_token =
971		devfs_add_devswf(&apm_cdevsw, 0, DV_CHR, 0, 0, 0600, "apm");
972#endif
973	return 0;
974}
975
976static int
977apmopen(dev_t dev, int flag, int fmt, struct proc *p)
978{
979	struct apm_softc *sc = &apm_softc;
980
981	if (minor(dev) != 0 || !sc->initialized)
982		return (ENXIO);
983
984	return 0;
985}
986
987static int
988apmclose(dev_t dev, int flag, int fmt, struct proc *p)
989{
990	return 0;
991}
992
993static int
994apmioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
995{
996	struct apm_softc *sc = &apm_softc;
997	int error = 0;
998	int newstate;
999
1000	if (minor(dev) != 0 || !sc->initialized)
1001		return (ENXIO);
1002#ifdef APM_DEBUG
1003	printf("APM ioctl: cmd = 0x%x\n", cmd);
1004#endif
1005	switch (cmd) {
1006	case APMIO_SUSPEND:
1007		if (sc->active)
1008			apm_suspend(PMST_SUSPEND);
1009		else
1010			error = EINVAL;
1011		break;
1012
1013	case APMIO_STANDBY:
1014		if (sc->active)
1015			apm_suspend(PMST_STANDBY);
1016		else
1017			error = EINVAL;
1018		break;
1019
1020	case APMIO_GETINFO_OLD:
1021		{
1022			struct apm_info info;
1023			apm_info_old_t aiop;
1024
1025			if (apm_get_info(&info))
1026				error = ENXIO;
1027			aiop = (apm_info_old_t)addr;
1028			aiop->ai_major = info.ai_major;
1029			aiop->ai_minor = info.ai_minor;
1030			aiop->ai_acline = info.ai_acline;
1031			aiop->ai_batt_stat = info.ai_batt_stat;
1032			aiop->ai_batt_life = info.ai_batt_life;
1033			aiop->ai_status = info.ai_status;
1034		}
1035		break;
1036	case APMIO_GETINFO:
1037		if (apm_get_info((apm_info_t)addr))
1038			error = ENXIO;
1039		break;
1040	case APMIO_ENABLE:
1041		apm_event_enable();
1042		break;
1043	case APMIO_DISABLE:
1044		apm_event_disable();
1045		break;
1046	case APMIO_HALTCPU:
1047		apm_halt_cpu();
1048		break;
1049	case APMIO_NOTHALTCPU:
1050		apm_not_halt_cpu();
1051		break;
1052	case APMIO_DISPLAY:
1053		newstate = *(int *)addr;
1054		if (apm_display(newstate))
1055			error = ENXIO;
1056		break;
1057	case APMIO_BIOS:
1058		if (apm_bios_call((struct apm_bios_arg*)addr) == 0)
1059			((struct apm_bios_arg*)addr)->eax &= 0xff;
1060		break;
1061	default:
1062		error = EINVAL;
1063		break;
1064	}
1065	return error;
1066}
1067
1068static device_method_t apm_methods[] = {
1069	/* Device interface */
1070	DEVMETHOD(device_probe,		apm_probe),
1071	DEVMETHOD(device_attach,	apm_attach),
1072
1073	{ 0, 0 }
1074};
1075
1076static driver_t apm_driver = {
1077	"apm",
1078	apm_methods,
1079	1,			/* no softc (XXX) */
1080};
1081
1082static devclass_t apm_devclass;
1083
1084DEV_DRIVER_MODULE(apm, nexus, apm_driver, apm_devclass,
1085	   CDEV_MAJOR, NOMAJ, apm_cdevsw, 0, 0);
1086