apm.c revision 5120
1#define APM_DEBUG 1 2/* 3 * LP (Laptop Package) 4 * 5 * Copyright (c) 1994 by HOSOKAWA, Tatsumi <hosokawa@mt.cs.keio.ac.jp> 6 * 7 * This software may be used, modified, copied, and distributed, in 8 * both source and binary form provided that the above copyright and 9 * these terms are retained. Under no circumstances is the author 10 * responsible for the proper functioning of this software, nor does 11 * the author assume any responsibility for damages incurred with its 12 * use. 13 * 14 * Sep, 1994 Implemented on FreeBSD 1.1.5.1R (Toshiba AVS001WD) 15 * 16 * $Id: apm.c,v 1.7 1994/11/15 14:09:18 bde Exp $ 17 */ 18 19#include "apm.h" 20 21#if NAPM > 0 22 23#include <sys/param.h> 24#include "conf.h" 25#include <sys/kernel.h> 26#include <sys/systm.h> 27#include <sys/malloc.h> 28#include <sys/ioctl.h> 29#include <sys/tty.h> 30#include <sys/file.h> 31#include <sys/proc.h> 32#include <sys/vnode.h> 33#include "i386/isa/isa.h" 34#include "i386/isa/isa_device.h" 35#include <machine/apm_bios.h> 36#include <machine/segments.h> 37#include <machine/clock.h> 38#include <vm/vm.h> 39#include <sys/syslog.h> 40#include "apm_setup.h" 41 42/* static data */ 43static int apm_initialized = 0, active = 0, halt_cpu = 1; 44static u_int minorversion, majorversion; 45static u_int cs32_base, cs16_base, ds_base; 46static u_int cs_limit, ds_limit; 47static u_int cs_entry; 48static u_int intversion; 49static int idle_cpu, disabled, disengaged; 50 51#define is_enabled(foo) ((foo) ? "enabled" : "disabled") 52 53/* Map version number to integer (keeps ordering of version numbers) */ 54#define INTVERSION(major, minor) ((major)*100 + (minor)) 55 56static timeout_t apm_timeout; 57 58/* setup APM GDT discriptors */ 59static void 60setup_apm_gdt(u_int code32_base, u_int code16_base, u_int data_base, u_int code_limit, u_int data_limit) 61{ 62 /* setup 32bit code segment */ 63 gdt_segs[GAPMCODE32_SEL].ssd_base = code32_base; 64 gdt_segs[GAPMCODE32_SEL].ssd_limit = code_limit; 65 66 /* setup 16bit code segment */ 67 gdt_segs[GAPMCODE16_SEL].ssd_base = code16_base; 68 gdt_segs[GAPMCODE16_SEL].ssd_limit = code_limit; 69 70 /* setup data segment */ 71 gdt_segs[GAPMDATA_SEL ].ssd_base = data_base; 72 gdt_segs[GAPMDATA_SEL ].ssd_limit = data_limit; 73 74 /* reflect these changes on physical GDT */ 75 ssdtosd(gdt_segs + GAPMCODE32_SEL, &gdt[GAPMCODE32_SEL].sd); 76 ssdtosd(gdt_segs + GAPMCODE16_SEL, &gdt[GAPMCODE16_SEL].sd); 77 ssdtosd(gdt_segs + GAPMDATA_SEL , &gdt[GAPMDATA_SEL ].sd); 78} 79 80/* 48bit far pointer */ 81struct addr48 { 82 u_long offset; 83 u_short segment; 84} apm_addr; 85 86int apm_errno; 87 88inline 89int 90apm_int(u_long *eax,u_long *ebx,u_long *ecx) 91{ 92 u_long cf; 93 __asm ("pushl %%ebp 94 pushl %%edx 95 pushl %%esi 96 pushl %%edi 97 xorl %3,%3 98 movl %%edi,%3 99 movl %%esi,%3 100 lcall _apm_addr 101 jnc 1f 102 incl %3 103 1: 104 popl %%edi 105 popl %%esi 106 popl %%edx 107 popl %%ebp" 108 : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=D" (cf) 109 : "0" (*eax), "1" (*ebx), "2" (*ecx) 110 ); 111 apm_errno = ((*eax) >> 8) & 0xff; 112 return cf; 113} 114 115 116/* enable/disable power management */ 117static int 118apm_enable_disable_pm(int enable) 119{ 120 u_long eax,ebx,ecx; 121 122 eax = (APM_BIOS<<8) | APM_ENABLEDISABLEPM; 123 124 if (intversion >= INTVERSION(1, 1)) { 125 ebx = PMDV_ALLDEV; 126 } else { 127 ebx = 0xffff; /* APM version 1.0 only */ 128 } 129 ecx = enable; 130 return apm_int(&eax,&ebx,&ecx); 131} 132 133/* Tell APM-BIOS that WE will do 1.1 and see what they say... */ 134static void 135apm_driver_version() 136{ 137 u_long eax,ebx,ecx,i; 138 139#ifdef APM_DEBUG 140 eax = (APM_BIOS<<8) | APM_INSTCHECK; 141 ebx = 0x0; 142 ecx = 0x0101; 143 i = apm_int(&eax,&ebx,&ecx); 144 printf("[%04lx %04lx %04lx %ld %02x]\n", 145 eax,ebx,ecx,i,apm_errno); 146#endif 147 148 eax = (APM_BIOS<<8) | APM_DRVVERSION; 149 ebx = 0x0; 150 ecx = 0x0101; 151 if(!apm_int(&eax,&ebx,&ecx)) 152 apm_version = eax & 0xffff; 153 154#ifdef APM_DEBUG 155 eax = (APM_BIOS<<8) | APM_INSTCHECK; 156 ebx = 0x0; 157 ecx = 0x0101; 158 i = apm_int(&eax,&ebx,&ecx); 159 printf("[%04lx %04lx %04lx %ld %02x]\n", 160 eax,ebx,ecx,i,apm_errno); 161#endif 162} 163 164/* engage/disengage power management (APM 1.1 or later) */ 165static int 166apm_engage_disengage_pm(int engage) 167{ 168 u_long eax,ebx,ecx,i; 169 170 eax = (APM_BIOS<<8) | APM_ENGAGEDISENGAGEPM; 171 ebx = PMDV_ALLDEV; 172 ecx = engage; 173 i = apm_int(&eax,&ebx,&ecx); 174 return i; 175} 176 177/* get PM event */ 178static u_int 179apm_getevent(void) 180{ 181 u_long eax,ebx,ecx; 182 183 eax = (APM_BIOS<<8) | APM_GETPMEVENT; 184 185 ebx = 0; 186 ecx = 0; 187 if (apm_int(&eax,&ebx,&ecx)) 188 return PMEV_NOEVENT; 189 190 return ebx & 0xffff; 191} 192 193/* suspend entire system */ 194static int 195apm_suspend_system(void) 196{ 197 u_long eax,ebx,ecx; 198 199 eax = (APM_BIOS<<8) | APM_SETPWSTATE; 200 ebx = PMDV_ALLDEV; 201 ecx = PMST_SUSPEND; 202 203 if (apm_int(&eax,&ebx,&ecx)) { 204 printf("Entire system suspend failure: errcode = %ld\n", 205 0xff & (eax >> 8)); 206 return 1; 207 } 208 return 0; 209} 210 211/* APM Battery low handler */ 212static void 213apm_battery_low(void) 214{ 215 printf("\007\007 * * * BATTERY IS LOW * * * \007\007"); 216} 217 218static struct timeval suspend_time; 219 220static int 221apm_default_resume(void) 222{ 223 u_int second, minute, hour; 224 struct timeval resume_time; 225 226 inittodr(0); /* adjust time to RTC */ 227 microtime(&resume_time); 228 second = resume_time.tv_sec - suspend_time.tv_sec; 229 hour = second / 3600; 230 second %= 3600; 231 minute = second / 60; 232 second %= 60; 233 log(LOG_NOTICE, "resumed from suspended mode (slept %02d:%02d:%02d)\n", 234 hour, minute, second); 235 return 0; 236} 237 238static int 239apm_default_suspend(void) 240{ 241 int pl; 242 microtime(&suspend_time); 243 apm_suspend_system(); 244 return 0; 245} 246 247/* get APM information */ 248static int 249apm_get_info(apm_info_t aip) 250{ 251 u_long eax,ebx,ecx; 252 253 eax = (APM_BIOS<<8)|APM_GETPWSTATUS; 254 ebx = PMDV_ALLDEV; 255 ecx = 0; 256 257 if (apm_int(&eax,&ebx,&ecx)) 258 return 1; 259 260 aip->ai_acline = (ebx >> 8) & 0xff; 261 aip->ai_batt_stat = ebx & 0xff; 262 aip->ai_batt_life = ecx & 0xff; 263 aip->ai_major = (u_int)majorversion; 264 aip->ai_minor = (u_int)minorversion; 265 return 0; 266} 267 268 269static void apm_processevent(void); 270 271/* inform APM BIOS that CPU is idle */ 272void 273apm_cpu_idle(void) 274{ 275 if (idle_cpu) { 276 if (active) { 277 __asm ("movw $0x5305, %ax; lcall _apm_addr"); 278 } 279 } 280 /* 281 * Some APM implementation halts CPU in BIOS, whenever 282 * "CPU-idle" function are invoked, but swtch() of 283 * FreeBSD halts CPU, therefore, CPU is halted twice 284 * in the sched loop. It makes the interrupt latency 285 * terribly long and be able to cause a serious problem 286 * in interrupt processing. We prevent it by removing 287 * "hlt" operation from swtch() and managed it under 288 * APM driver. 289 */ 290 if (!active || halt_cpu) { 291 __asm("sti ; hlt"); /* wait for interrupt */ 292 } 293} 294 295/* inform APM BIOS that CPU is busy */ 296void 297apm_cpu_busy(void) 298{ 299 if (idle_cpu && active) { 300 __asm("movw $0x5306, %ax; lcall _apm_addr"); 301 } 302} 303 304 305/* 306 * APM timeout routine: 307 * 308 * This routine is automatically called by timer once per second. 309 */ 310 311static void 312apm_timeout(void *arg1) 313{ 314 apm_processevent(); 315 timeout(apm_timeout, NULL, hz ); /* 1 Hz */ 316} 317 318/* enable APM BIOS */ 319static void 320apm_event_enable(void) 321{ 322#ifdef APM_DEBUG 323 printf("called apm_event_enable()\n"); 324#endif 325 if (apm_initialized) { 326 active = 1; 327 timeout(apm_timeout, NULL, 2 * hz); 328 } 329} 330 331/* disable APM BIOS */ 332static void 333apm_event_disable(void) 334{ 335#ifdef APM_DEBUG 336 printf("called apm_event_disable()\n"); 337#endif 338 if (apm_initialized) { 339 untimeout(apm_timeout, NULL); 340 active = 0; 341 } 342} 343 344/* halt CPU in scheduling loop */ 345static void apm_halt_cpu(void) 346{ 347 if (apm_initialized) { 348 halt_cpu = 1; 349 } 350} 351 352/* don't halt CPU in scheduling loop */ 353static void apm_not_halt_cpu(void) 354{ 355 if (apm_initialized) { 356 halt_cpu = 0; 357 } 358} 359 360/* device driver definitions */ 361int apmprobe (struct isa_device *); 362int apmattach(struct isa_device *); 363 364struct isa_driver apmdriver = { apmprobe, apmattach, "apm" }; 365 366/* 367 * probe APM (dummy): 368 * 369 * APM probing routine is placed on locore.s and apm_init.S because 370 * this process forces the CPU to turn to real mode or V86 mode. 371 * Current version uses real mode, but on future version, we want 372 * to use V86 mode in APM initialization. 373 */ 374 375int 376apmprobe(struct isa_device *dvp) 377{ 378 switch (apm_version) { 379 case APMINI_CANTFIND: 380 /* silent */ 381 return 0; 382 case APMINI_NOT32BIT: 383 printf("apm%d: 32bit connection is not supported.\n", 384 dvp->id_unit); 385 return 0; 386 case APMINI_CONNECTERR: 387 printf("apm%d: 32-bit connection error.\n", dvp->id_unit); 388 return 0; 389 } 390 391 if ((apm_version & 0xff00) != 0x0100) return 0; 392 if ((apm_version & 0x00f0) >= 0x00a0) return 0; 393 if ((apm_version & 0x000f) >= 0x000a) return 0; 394 return -1; 395} 396 397 398/* Process APM event */ 399static void 400apm_processevent(void) 401{ 402 int apm_event; 403 404#ifdef APM_DEBUG 405# define OPMEV_DEBUGMESSAGE(symbol) case symbol: \ 406 printf("Original APM Event: " #symbol "\n"); 407#else 408# define OPMEV_DEBUGMESSAGE(symbol) case symbol: 409#endif 410 411 while (1) { 412 apm_event = apm_getevent(); 413 if (apm_event == PMEV_NOEVENT) 414 break; 415 switch (apm_event) { 416 OPMEV_DEBUGMESSAGE(PMEV_STANDBYREQ); 417 apm_default_suspend(); 418 break; 419 OPMEV_DEBUGMESSAGE(PMEV_SUSPENDREQ); 420 apm_default_suspend(); 421 break; 422 OPMEV_DEBUGMESSAGE(PMEV_USERSUSPENDREQ); 423 apm_default_suspend(); 424 break; 425 426 OPMEV_DEBUGMESSAGE(PMEV_CRITSUSPEND); 427 apm_default_suspend(); 428 break; 429 430 OPMEV_DEBUGMESSAGE(PMEV_NORMRESUME); 431 apm_default_resume(); 432 break; 433 OPMEV_DEBUGMESSAGE(PMEV_CRITRESUME); 434 apm_default_resume(); 435 break; 436 OPMEV_DEBUGMESSAGE(PMEV_STANDBYRESUME); 437 apm_default_resume(); 438 break; 439 440 OPMEV_DEBUGMESSAGE(PMEV_BATTERYLOW); 441 apm_battery_low(); 442 apm_default_suspend(); 443 break; 444 445 OPMEV_DEBUGMESSAGE(PMEV_POWERSTATECHANGE); 446 break; 447 448 OPMEV_DEBUGMESSAGE(PMEV_UPDATETIME); 449 inittodr(0); /* adjust time to RTC */ 450 break; 451 452 default: 453 printf("Unknown Original APM Event 0x%x\n", apm_event); 454 break; 455 } 456 } 457} 458 459/* 460 * Attach APM: 461 * 462 * Initialize APM driver (APM BIOS itself has been initialized in locore.s) 463 * 464 * Now, unless I'm mad, (not quite ruled out yet), the APM-1.1 spec is bogus: 465 * 466 * Appendix C says under the header "APM 1.0/APM 1.1 Modal BIOS Behavior" 467 * that "When an APM Driver connects with an APM 1.1 BIOS, the APM 1.1 BIOS 468 * will default to an APM 1.0 connection. After an APM Driver calls the APM 469 * Driver Version function, specifying that it supports APM 1.1, and [sic!] 470 * APM BIOS will change its behavior to an APM 1.1 connection. If the APM 471 * BIOS is an APM 1.0 BIOS, the APM Driver Version function call will fail, 472 * and the connection will remain an APM 1.0 connection." 473 * 474 * OK so I can establish a 1.0 connection, and then tell that I'm a 1.1 475 * and maybe then the BIOS will tell that it too is a 1.1. 476 * Fine. 477 * Now how will I ever get the segment-limits for instance ? There is no 478 * way I can see that I can get a 1.1 response back from an "APM Protected 479 * Mode 32-bit Interface Connect" function ??? 480 * 481 * Who made this, Intel and Microsoft ? -- How did you guess ! 482 * 483 * /phk 484 */ 485 486int 487apmattach(struct isa_device *dvp) 488{ 489 490 /* setup APM parameters */ 491 cs32_base = (apm_cs32_base << 4) + KERNBASE; 492 cs16_base = (apm_cs16_base << 4) + KERNBASE; 493 ds_base = (apm_ds_base << 4) + KERNBASE; 494 cs_limit = apm_cs_limit; 495 ds_limit = apm_ds_limit; 496 cs_entry = apm_cs_entry; 497 498 idle_cpu = ((apm_flags & APM_CPUIDLE_SLOW) != 0); 499 disabled = ((apm_flags & APM_DISABLED) != 0); 500 disengaged = ((apm_flags & APM_DISENGAGED) != 0); 501 502 /* print bootstrap messages */ 503#ifdef APM_DEBUG 504 printf(" found APM BIOS version %04x\n", apm_version); 505 printf("apm%d: Code32 0x%08x, Code16 0x%08x, Data 0x%08x\n", 506 dvp->id_unit, cs32_base, cs16_base, ds_base); 507 printf("apm%d: Code entry 0x%08x, Idling CPU %s, Management %s\n", 508 dvp->id_unit, cs_entry, is_enabled(idle_cpu), 509 is_enabled(!disabled)); 510 printf("apm%d: CS_limit=%x, DS_limit=%x\n", 511 dvp->id_unit, cs_limit,ds_limit); 512 513#endif /* APM_DEBUG */ 514 515 cs_limit = 0xffff; 516 ds_limit = 0xffff; 517 518 /* setup GDT */ 519 setup_apm_gdt(cs32_base, cs16_base, ds_base, cs_limit, ds_limit); 520 521 /* setup entry point 48bit pointer */ 522 apm_addr.segment = GSEL(GAPMCODE32_SEL, SEL_KPL); 523 apm_addr.offset = cs_entry; 524 525 /* Try to kick bios into 1.1 mode */ 526 apm_driver_version(); 527 528 minorversion = ((apm_version & 0x00f0) >> 4) * 10 + 529 ((apm_version & 0x000f) >> 0); 530 majorversion = ((apm_version & 0xf000) >> 12) * 10 + 531 ((apm_version & 0x0f00) >> 8); 532 533 intversion = INTVERSION(majorversion, minorversion); 534 535 if (intversion >= INTVERSION(1, 1)) { 536 printf("apm%d: Engaged control %s\n", 537 dvp->id_unit, is_enabled(!disengaged)); 538 } 539 540 printf(" found APM BIOS version %d.%d\n", majorversion, minorversion); 541 printf("apm%d: Idling CPU %s\n", dvp->id_unit, is_enabled(idle_cpu)); 542 543 /* enable power management */ 544 if (disabled) { 545 if (apm_enable_disable_pm(1)) { 546 printf("Warning: APM enable function failed! [%x]\n", 547 apm_errno); 548 } 549 } 550 551 /* engage power managment (APM 1.1 or later) */ 552 if (intversion >= INTVERSION(1, 1) && disengaged) { 553 if (apm_engage_disengage_pm(1)) { 554 printf("Warning: APM engage function failed [%x]\n", 555 apm_errno); 556 } 557 } 558 559 apm_initialized = 1; 560 561 apm_event_enable(); 562 563 return 0; 564} 565 566int 567apmopen(dev_t dev, int flag, int fmt, struct proc *p) 568{ 569 if (!apm_initialized) { 570 return ENXIO; 571 } 572 if (minor(dev)) 573 return (ENXIO); 574 return 0; 575} 576 577int 578apmclose(dev_t dev, int flag, int fmt, struct proc *p) 579{ 580 return 0; 581} 582 583int 584apmioctl(dev_t dev, int cmd, caddr_t addr, int flag, struct proc *p) 585{ 586 int error = 0; 587 int pl; 588 589#ifdef APM_DEBUG 590 printf("APM ioctl: minor = %d, cmd = 0x%x\n", minor(dev), cmd); 591#endif 592 593 pl = splhigh(); 594 if (minor(dev) != 0) { 595 return ENXIO; 596 } 597 if (!apm_initialized) { 598 return ENXIO; 599 } 600 switch (cmd) { 601 case APMIO_SUSPEND: 602 apm_default_suspend(); 603 break; 604 case APMIO_GETINFO: 605 if (apm_get_info((apm_info_t)addr)) { 606 error = ENXIO; 607 } 608 break; 609 case APMIO_ENABLE: 610 apm_event_enable(); 611 break; 612 case APMIO_DISABLE: 613 apm_event_disable(); 614 break; 615 case APMIO_HALTCPU: 616 apm_halt_cpu(); 617 break; 618 case APMIO_NOTHALTCPU: 619 apm_not_halt_cpu(); 620 break; 621 default: 622 error = EINVAL; 623 break; 624 } 625 splx(pl); 626 return error; 627} 628 629#endif /* NAPM > 0 */ 630