#include <sys/param.h>
#include "libsa.h"
#include <stand/boot/bootarg.h>
#include <dev/isa/isareg.h>
#include <machine/apmvar.h>
#include <machine/biosvar.h>
#include "debug.h"
extern int debug;
static __inline u_int
apm_check(void)
{
register u_int detail;
register u_int8_t f;
__asm volatile(DOINT(0x15) "\n\t"
"setc %b1\n\t"
"movzwl %%ax, %0\n\t"
"shll $16, %%ecx\n\t"
"orl %%ecx, %0"
: "=a" (detail), "=b" (f)
: "0" (APM_INSTCHECK), "1" (APM_DEV_APM_BIOS)
: "%ecx", "cc");
if (f || BIOS_regs.biosr_bx != 0x504d ) {
#ifdef DEBUG
if (debug)
printf("apm_check: %x, %x, %x\n",
f, BIOS_regs.biosr_bx, detail);
#endif
return 0;
} else
return detail;
}
static __inline int
apm_disconnect(void)
{
register u_int16_t rv;
__asm volatile(DOINT(0x15) "\n\t"
"setc %b0"
: "=a" (rv)
: "0" (APM_DISCONNECT), "b" (APM_DEV_APM_BIOS)
: "%ecx", "%edx", "cc");
return ((rv & 0xff)? rv >> 8 : 0);
}
static __inline int
apm_connect(bios_apminfo_t *ai)
{
register u_int16_t f;
__asm volatile (DOINT(0x15) "\n\t"
"setc %b1\n\t"
"movb %%ah, %h1\n\t"
"movzwl %%ax, %%eax\n\tshll $4, %0\n\t"
"movzwl %%cx, %%ecx\n\tshll $4, %2\n\t"
"movzwl %%dx, %%edx\n\tshll $4, %3\n\t"
: "=a" (ai->apm_code32_base),
"=b" (f),
"=c" (ai->apm_code16_base),
"=d" (ai->apm_data_base)
: "0" (APM_PROT32_CONNECT), "1" (APM_DEV_APM_BIOS)
: "cc");
if (f & 0xff)
return (f >> 8);
ai->apm_entry = BIOS_regs.biosr_bx;
#if 0
ai->apm_code_len = BIOS_regs.biosr_si & 0xffff;
ai->apm_code16_len = BIOS_regs.biosr_si & 0xffff;
ai->apm_data_len = BIOS_regs.biosr_di & 0xffff;
#else
ai->apm_code_len = 0xffff - (ai->apm_code32_base & 0xffff);
ai->apm_code16_len = 0xffff - (ai->apm_code16_base & 0xffff);
ai->apm_data_len = 0xffff - (ai->apm_data_base & 0xffff);
#endif
if (ai->apm_data_base < BOOTARG_OFF)
ai->apm_data_len = PAGE_SIZE - (ai->apm_data_base & PAGE_MASK) - 1;
#ifdef DEBUG
if (debug)
printf("cs=%x:%x/%x:%x, ds=%x:%x\n",
ai->apm_code32_base, ai->apm_code_len,
ai->apm_code16_base, ai->apm_code16_len,
ai->apm_data_base, ai->apm_data_len);
#endif
__asm volatile (DOINT(0x15) "\n\t"
"setc %b1\n\t"
"movb %%ah, %h1"
: "=b" (f)
: "a" (APM_DRIVER_VERSION),
"0" (APM_DEV_APM_BIOS),
"c" (APM_VERSION)
: "cc");
return 0;
}
static bios_apminfo_t ai;
void
apmprobe(void)
{
if ((ai.apm_detail = apm_check())) {
apm_disconnect();
if (apm_connect(&ai) != 0) {
#ifdef DEBUG
printf("\napm: connect error\n");
#endif
return;
}
#ifdef DEBUG
if (debug)
printf("apm[%x cs=%x[%x]/%x[%x] ds=%x[%x] @ %x]",
ai.apm_detail,
ai.apm_code32_base, ai.apm_code_len,
ai.apm_code16_base, ai.apm_code16_len,
ai.apm_data_base, ai.apm_data_len,
ai.apm_entry);
else
printf(" apm");
#else
printf(" apm");
#endif
addbootarg(BOOTARG_APMINFO, sizeof(ai), &ai);
}
}
#define round_page(x) (((x) + PAGE_MASK) & ~PAGE_MASK)
#define trunc_page(x) ((x) & ~PAGE_MASK)
void
apmfixmem(void)
{
#ifdef DEBUG
printf("apmremove (%d)", ai.apm_detail);
#endif
if (ai.apm_detail)
mem_delete(trunc_page(ai.apm_data_base),
round_page(ai.apm_data_base + ai.apm_data_len));
}