vesa.c revision 248799
11573Srgrimes/*- 21573Srgrimes * Copyright (c) 1998 Kazutaka YOKOTA and Michael Smith 31573Srgrimes * Copyright (c) 2009-2012 Jung-uk Kim <jkim@FreeBSD.org> 41573Srgrimes * All rights reserved. 51573Srgrimes * 61573Srgrimes * Redistribution and use in source and binary forms, with or without 71573Srgrimes * modification, are permitted provided that the following conditions 8227753Stheraven * are met: 9227753Stheraven * 1. Redistributions of source code must retain the above copyright 10227753Stheraven * notice, this list of conditions and the following disclaimer as 11227753Stheraven * the first lines of this file unmodified. 12227753Stheraven * 2. Redistributions in binary form must reproduce the above copyright 131573Srgrimes * notice, this list of conditions and the following disclaimer in the 141573Srgrimes * documentation and/or other materials provided with the distribution. 151573Srgrimes * 161573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 171573Srgrimes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 181573Srgrimes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 191573Srgrimes * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 201573Srgrimes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 211573Srgrimes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 221573Srgrimes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 231573Srgrimes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 241573Srgrimes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 251573Srgrimes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 261573Srgrimes */ 271573Srgrimes 281573Srgrimes#include <sys/cdefs.h> 291573Srgrimes__FBSDID("$FreeBSD: head/sys/dev/fb/vesa.c 248799 2013-03-27 18:06:28Z jkim $"); 301573Srgrimes 311573Srgrimes#include "opt_vga.h" 321573Srgrimes#include "opt_vesa.h" 331573Srgrimes 341573Srgrimes#ifndef VGA_NO_MODE_CHANGE 351573Srgrimes 361573Srgrimes#include <sys/param.h> 371573Srgrimes#include <sys/bus.h> 381573Srgrimes#include <sys/systm.h> 391573Srgrimes#include <sys/kernel.h> 401573Srgrimes#include <sys/lock.h> 4190045Sobrien#include <sys/module.h> 4290045Sobrien#include <sys/malloc.h> 431573Srgrimes#include <sys/mutex.h> 441573Srgrimes#include <sys/fbio.h> 451573Srgrimes#include <sys/sysctl.h> 461573Srgrimes 471573Srgrimes#include <vm/vm.h> 481573Srgrimes#include <vm/vm_extern.h> 491573Srgrimes#include <vm/vm_kern.h> 501573Srgrimes#include <vm/vm_param.h> 511573Srgrimes#include <vm/pmap.h> 521573Srgrimes 531573Srgrimes#include <machine/pc/bios.h> 541573Srgrimes#include <dev/fb/vesa.h> 551573Srgrimes 561573Srgrimes#include <dev/fb/fbreg.h> 571573Srgrimes#include <dev/fb/vgareg.h> 581573Srgrimes 591573Srgrimes#include <dev/pci/pcivar.h> 601573Srgrimes 611573Srgrimes#include <isa/isareg.h> 621573Srgrimes 631573Srgrimes#include <compat/x86bios/x86bios.h> 648870Srgrimes 651573Srgrimes#define VESA_BIOS_OFFSET 0xc0000 661573Srgrimes#define VESA_PALETTE_SIZE (256 * 4) 671573Srgrimes#define VESA_VIA_CLE266 "VIA CLE266\r\n" 681573Srgrimes 69132817Stjr#ifndef VESA_DEBUG 70132817Stjr#define VESA_DEBUG 0 71132817Stjr#endif 72132817Stjr 73132817Stjr/* VESA video adapter state buffer stub */ 74304284Sachestruct adp_state { 75132817Stjr int sig; 76132817Stjr#define V_STATE_SIG 0x61736576 77132817Stjr u_char regs[1]; 78132817Stjr}; 791573Srgrimestypedef struct adp_state adp_state_t; 801573Srgrimes 811573Srgrimesstatic struct mtx vesa_lock; 821573Srgrimes 831573Srgrimesstatic int vesa_state; 841573Srgrimesstatic void *vesa_state_buf; 851573Srgrimesstatic uint32_t vesa_state_buf_offs; 86132817Stjrstatic ssize_t vesa_state_buf_size; 871573Srgrimes 88132817Stjrstatic u_char *vesa_palette; 891573Srgrimesstatic uint32_t vesa_palette_offs; 901573Srgrimes 911573Srgrimesstatic void *vesa_vmem_buf; 921573Srgrimesstatic size_t vesa_vmem_max; 93132817Stjr 941573Srgrimesstatic void *vesa_bios; 9519276Sachestatic uint32_t vesa_bios_offs; 9619276Sachestatic uint32_t vesa_bios_int10; 97243779Smarcelstatic size_t vesa_bios_size; 98243779Smarcel 99243779Smarcel/* VESA video adapter */ 100243779Smarcelstatic video_adapter_t *vesa_adp; 101243779Smarcel 102243779Smarcelstatic SYSCTL_NODE(_debug, OID_AUTO, vesa, CTLFLAG_RD, NULL, "VESA debugging"); 103243779Smarcelstatic int vesa_shadow_rom; 104243779SmarcelTUNABLE_INT("debug.vesa.shadow_rom", &vesa_shadow_rom); 105243779SmarcelSYSCTL_INT(_debug_vesa, OID_AUTO, shadow_rom, CTLFLAG_RDTUN, &vesa_shadow_rom, 106243779Smarcel 0, "Enable video BIOS shadow"); 107243779Smarcel 108243779Smarcel/* VESA functions */ 109243779Smarcel#if 0 110243779Smarcelstatic int vesa_nop(void); 111243779Smarcel#endif 112243779Smarcelstatic int vesa_error(void); 113243779Smarcelstatic vi_probe_t vesa_probe; 114243779Smarcelstatic vi_init_t vesa_init; 115243779Smarcelstatic vi_get_info_t vesa_get_info; 116304284Sachestatic vi_query_mode_t vesa_query_mode; 117304284Sachestatic vi_set_mode_t vesa_set_mode; 118304284Sachestatic vi_save_font_t vesa_save_font; 119304284Sachestatic vi_load_font_t vesa_load_font; 120304284Sachestatic vi_show_font_t vesa_show_font; 121304284Sachestatic vi_save_palette_t vesa_save_palette; 122304284Sachestatic vi_load_palette_t vesa_load_palette; 123304284Sachestatic vi_set_border_t vesa_set_border; 124304284Sachestatic vi_save_state_t vesa_save_state; 125304284Sachestatic vi_load_state_t vesa_load_state; 126304284Sachestatic vi_set_win_org_t vesa_set_origin; 127304284Sachestatic vi_read_hw_cursor_t vesa_read_hw_cursor; 128304284Sachestatic vi_set_hw_cursor_t vesa_set_hw_cursor; 129304284Sachestatic vi_set_hw_cursor_shape_t vesa_set_hw_cursor_shape; 1301573Srgrimesstatic vi_blank_display_t vesa_blank_display; 131132817Stjrstatic vi_mmap_t vesa_mmap; 132132817Stjrstatic vi_ioctl_t vesa_ioctl; 133132817Stjrstatic vi_clear_t vesa_clear; 134132817Stjrstatic vi_fill_rect_t vesa_fill_rect; 1351573Srgrimesstatic vi_bitblt_t vesa_bitblt; 136132817Stjrstatic vi_diag_t vesa_diag; 1371573Srgrimesstatic int vesa_bios_info(int level); 138132817Stjr 1391573Srgrimesstatic video_switch_t vesavidsw = { 140304284Sache vesa_probe, 141304284Sache vesa_init, 142304284Sache vesa_get_info, 143304284Sache vesa_query_mode, 144304284Sache vesa_set_mode, 145304284Sache vesa_save_font, 146304284Sache vesa_load_font, 1471573Srgrimes vesa_show_font, 148304284Sache vesa_save_palette, 149304284Sache vesa_load_palette, 150304284Sache vesa_set_border, 1511573Srgrimes vesa_save_state, 15290045Sobrien vesa_load_state, 153158812Sache vesa_set_origin, 15490045Sobrien vesa_read_hw_cursor, 15590045Sobrien vesa_set_hw_cursor, 156180021Smtm vesa_set_hw_cursor_shape, 1571573Srgrimes vesa_blank_display, 15890045Sobrien vesa_mmap, 1591573Srgrimes vesa_ioctl, 16090045Sobrien vesa_clear, 161304284Sache vesa_fill_rect, 162304284Sache vesa_bitblt, 163243779Smarcel vesa_error, 164243779Smarcel vesa_error, 165243779Smarcel vesa_diag, 166243779Smarcel}; 167243779Smarcel 168304284Sachestatic video_switch_t *prevvidsw; 169304284Sache 170243779Smarcel/* VESA BIOS video modes */ 17190045Sobrien#define VESA_MAXMODES 64 172304284Sache#define EOT (-1) 173304284Sache#define NA (-2) 174243779Smarcel 175304284Sache#define MODE_TABLE_DELTA 8 176243779Smarcel 177304284Sachestatic int vesa_vmode_max; 178304284Sachestatic video_info_t *vesa_vmode; 17990045Sobrien 180304284Sachestatic int vesa_init_done; 181304284Sachestatic struct vesa_info *vesa_adp_info; 1821573Srgrimesstatic u_int16_t *vesa_vmodetab; 18390045Sobrienstatic char *vesa_oemstr; 1841573Srgrimesstatic char *vesa_venderstr; 1851573Srgrimesstatic char *vesa_prodstr; 1861573Srgrimesstatic char *vesa_revstr; 187228754Seadler 188228754Seadler/* local macros and functions */ 1891573Srgrimes#define BIOS_SADDRTOLADDR(p) ((((p) & 0xffff0000) >> 12) + ((p) & 0x0000ffff)) 190243779Smarcel 191159294Sdelphijstatic int int10_set_mode(int mode); 192132817Stjrstatic int vesa_bios_post(void); 193132817Stjrstatic int vesa_bios_get_mode(int mode, struct vesa_mode *vmode, int flags); 194132817Stjrstatic int vesa_bios_set_mode(int mode); 195132817Stjr#if 0 196304284Sachestatic int vesa_bios_get_dac(void); 1971573Srgrimes#endif 198159294Sdelphijstatic int vesa_bios_set_dac(int bits); 1991573Srgrimesstatic int vesa_bios_save_palette(int start, int colors, u_char *palette, 2001573Srgrimes int bits); 2011573Srgrimesstatic int vesa_bios_save_palette2(int start, int colors, u_char *r, u_char *g, 2021573Srgrimes u_char *b, int bits); 2031573Srgrimesstatic int vesa_bios_load_palette(int start, int colors, u_char *palette, 2041573Srgrimes int bits); 20580525Smikehstatic int vesa_bios_load_palette2(int start, int colors, u_char *r, u_char *g, 206243779Smarcel u_char *b, int bits); 207243779Smarcel#define STATE_SIZE 0 208243779Smarcel#define STATE_SAVE 1 209243779Smarcel#define STATE_LOAD 2 2101573Srgrimesstatic ssize_t vesa_bios_state_buf_size(int); 2111573Srgrimesstatic int vesa_bios_save_restore(int code, void *p); 2121573Srgrimes#ifdef MODE_TABLE_BROKEN 2131573Srgrimesstatic int vesa_bios_get_line_length(void); 2141573Srgrimes#endif 21574963Speterstatic int vesa_bios_set_line_length(int pixel, int *bytes, int *lines); 216304284Sache#if 0 217132817Stjrstatic int vesa_bios_get_start(int *x, int *y); 218132817Stjr#endif 219304284Sachestatic int vesa_bios_set_start(int x, int y); 220132817Stjrstatic int vesa_map_gen_mode_num(int type, int color, int mode); 221132817Stjrstatic int vesa_translate_flags(u_int16_t vflags); 222304284Sachestatic int vesa_translate_mmodel(u_int8_t vmodel); 223304284Sachestatic int vesa_get_bpscanline(struct vesa_mode *vmode); 224304284Sachestatic int vesa_bios_init(void); 225132817Stjrstatic void vesa_bios_uninit(void); 226304284Sachestatic void vesa_clear_modes(video_info_t *info, int color); 227132817Stjr 228132817Stjr#if 0 229132817Stjrstatic int vesa_get_origin(video_adapter_t *adp, off_t *offset); 230132817Stjr#endif 2311573Srgrimes 232132817Stjr/* INT 10 BIOS calls */ 233304284Sachestatic int 234304284Sacheint10_set_mode(int mode) 235304284Sache{ 236304284Sache x86regs_t regs; 237132817Stjr 2381573Srgrimes x86bios_init_regs(®s); 239132817Stjr regs.R_AL = mode; 240132817Stjr 241132817Stjr x86bios_intr(®s, 0x10); 242132817Stjr 243132817Stjr return (0); 244304284Sache} 245304284Sache 246304284Sachestatic int 247132817Stjrvesa_bios_post(void) 248304284Sache{ 249132817Stjr x86regs_t regs; 250132817Stjr devclass_t dc; 251132817Stjr device_t *devs; 2521573Srgrimes device_t dev; 253304284Sache int count, i, is_pci; 254304284Sache 2551573Srgrimes if (x86bios_get_orm(vesa_bios_offs) == NULL) 2561573Srgrimes return (1); 2571573Srgrimes 258304284Sache dev = NULL; 2591573Srgrimes is_pci = 0; 260304284Sache 2611573Srgrimes /* Find the matching PCI video controller. */ 2621573Srgrimes dc = devclass_find("vgapci"); 263304284Sache if (dc != NULL && devclass_get_devices(dc, &devs, &count) == 0) { 264304284Sache for (i = 0; i < count; i++) 265304284Sache if (device_get_flags(devs[i]) != 0 && 266304284Sache x86bios_match_device(vesa_bios_offs, devs[i])) { 267304284Sache dev = devs[i]; 268304284Sache is_pci = 1; 269304284Sache break; 270304284Sache } 271304284Sache free(devs, M_TEMP); 272304284Sache } 273304284Sache 274304284Sache /* Try VGA if a PCI device is not found. */ 275304284Sache if (dev == NULL) { 276304284Sache dc = devclass_find(VGA_DRIVER_NAME); 277304284Sache if (dc != NULL) 278304284Sache dev = devclass_get_device(dc, 0); 279304284Sache } 280304284Sache 281304284Sache if (bootverbose) 282304284Sache printf("%s: calling BIOS POST\n", 283304284Sache dev == NULL ? "VESA" : device_get_nameunit(dev)); 284304284Sache 285304284Sache x86bios_init_regs(®s); 286304284Sache if (is_pci) { 2871573Srgrimes regs.R_AH = pci_get_bus(dev); 2881573Srgrimes regs.R_AL = (pci_get_slot(dev) << 3) | 2891573Srgrimes (pci_get_function(dev) & 0x07); 2901573Srgrimes } 2911573Srgrimes regs.R_DL = 0x80; 29274963Speter x86bios_call(®s, X86BIOS_PHYSTOSEG(vesa_bios_offs + 3), 293243779Smarcel X86BIOS_PHYSTOOFF(vesa_bios_offs + 3)); 2941573Srgrimes 295304284Sache if (x86bios_get_intr(0x10) == 0) 2961573Srgrimes return (1); 297304284Sache 298304284Sache return (0); 299304284Sache} 300304284Sache 301304284Sache/* VESA BIOS calls */ 302304284Sachestatic int 303304284Sachevesa_bios_get_mode(int mode, struct vesa_mode *vmode, int flags) 304243779Smarcel{ 305243779Smarcel x86regs_t regs; 306304284Sache uint32_t offs; 3071573Srgrimes void *buf; 3081573Srgrimes 3091573Srgrimes buf = x86bios_alloc(&offs, sizeof(*vmode), flags); 3101573Srgrimes if (buf == NULL) 3111573Srgrimes return (1); 3121573Srgrimes 3131573Srgrimes x86bios_init_regs(®s); 3141573Srgrimes regs.R_AX = 0x4f01; 31574963Speter regs.R_CX = mode; 316304284Sache 317243779Smarcel regs.R_ES = X86BIOS_PHYSTOSEG(offs); 3181573Srgrimes regs.R_DI = X86BIOS_PHYSTOOFF(offs); 319304284Sache 3201573Srgrimes x86bios_intr(®s, 0x10); 321150137Sache 32274963Speter if (regs.R_AX != 0x004f) { 3231573Srgrimes x86bios_free(buf, sizeof(*vmode)); 3241573Srgrimes return (1); 3251573Srgrimes } 3261573Srgrimes 32774963Speter bcopy(buf, vmode, sizeof(*vmode)); 3281573Srgrimes x86bios_free(buf, sizeof(*vmode)); 3291573Srgrimes 3301573Srgrimes return (0); 331304284Sache} 3321573Srgrimes 3331573Srgrimesstatic int 3341573Srgrimesvesa_bios_set_mode(int mode) 3351573Srgrimes{ 3361573Srgrimes x86regs_t regs; 3378870Srgrimes 3381573Srgrimes x86bios_init_regs(®s); 3391573Srgrimes regs.R_AX = 0x4f02; 3401573Srgrimes regs.R_BX = mode; 3411573Srgrimes 3421573Srgrimes x86bios_intr(®s, 0x10); 3431573Srgrimes 3441573Srgrimes return (regs.R_AX != 0x004f); 3451573Srgrimes} 3461573Srgrimes 3471573Srgrimes#if 0 3481573Srgrimesstatic int 3491573Srgrimesvesa_bios_get_dac(void) 3501573Srgrimes{ 3511573Srgrimes x86regs_t regs; 3521573Srgrimes 353304284Sache x86bios_init_regs(®s); 354304284Sache regs.R_AX = 0x4f08; 3551573Srgrimes regs.R_BL = 1; 3561573Srgrimes 3571573Srgrimes x86bios_intr(®s, 0x10); 3581573Srgrimes 3591573Srgrimes if (regs.R_AX != 0x004f) 360150137Sache return (6); 3611573Srgrimes 3621573Srgrimes return (regs.R_BH); 3638870Srgrimes} 3641573Srgrimes#endif 3651573Srgrimes 3661573Srgrimesstatic int 367150137Sachevesa_bios_set_dac(int bits) 3681573Srgrimes{ 3691573Srgrimes x86regs_t regs; 3701573Srgrimes 3711573Srgrimes x86bios_init_regs(®s); 3721573Srgrimes regs.R_AX = 0x4f08; 3731573Srgrimes /* regs.R_BL = 0; */ 3741573Srgrimes regs.R_BH = bits; 3751573Srgrimes 3761573Srgrimes x86bios_intr(®s, 0x10); 3771573Srgrimes 3781573Srgrimes if (regs.R_AX != 0x004f) 3791573Srgrimes return (6); 3801573Srgrimes 3811573Srgrimes return (regs.R_BH); 3821573Srgrimes} 3831573Srgrimes 3841573Srgrimesstatic int 3851573Srgrimesvesa_bios_save_palette(int start, int colors, u_char *palette, int bits) 3861573Srgrimes{ 3871573Srgrimes x86regs_t regs; 3888870Srgrimes int i; 3891573Srgrimes 3901573Srgrimes x86bios_init_regs(®s); 3911573Srgrimes regs.R_AX = 0x4f09; 3921573Srgrimes regs.R_BL = 1; 3931573Srgrimes regs.R_CX = colors; 3941573Srgrimes regs.R_DX = start; 3951573Srgrimes 3961573Srgrimes regs.R_ES = X86BIOS_PHYSTOSEG(vesa_palette_offs); 3971573Srgrimes regs.R_DI = X86BIOS_PHYSTOOFF(vesa_palette_offs); 3981573Srgrimes 399304284Sache bits = 8 - bits; 400304284Sache mtx_lock(&vesa_lock); 401304284Sache x86bios_intr(®s, 0x10); 4021573Srgrimes if (regs.R_AX != 0x004f) { 4031573Srgrimes mtx_unlock(&vesa_lock); 4041573Srgrimes return (1); 4051573Srgrimes } 4061573Srgrimes for (i = 0; i < colors; ++i) { 4071573Srgrimes palette[i * 3] = vesa_palette[i * 4 + 2] << bits; 4081573Srgrimes palette[i * 3 + 1] = vesa_palette[i * 4 + 1] << bits; 4091573Srgrimes palette[i * 3 + 2] = vesa_palette[i * 4] << bits; 4101573Srgrimes } 411228755Seadler mtx_unlock(&vesa_lock); 4121573Srgrimes 4131573Srgrimes return (0); 4141573Srgrimes} 4151573Srgrimes 4161573Srgrimesstatic int 4171573Srgrimesvesa_bios_save_palette2(int start, int colors, u_char *r, u_char *g, u_char *b, 4181573Srgrimes int bits) 4191573Srgrimes{ 420159294Sdelphij x86regs_t regs; 4211573Srgrimes int i; 4221573Srgrimes 423304284Sache x86bios_init_regs(®s); 4241573Srgrimes regs.R_AX = 0x4f09; 42524158Simp regs.R_BL = 1; 426304284Sache regs.R_CX = colors; 427304284Sache regs.R_DX = start; 428304284Sache 429304284Sache regs.R_ES = X86BIOS_PHYSTOSEG(vesa_palette_offs); 430304284Sache regs.R_DI = X86BIOS_PHYSTOOFF(vesa_palette_offs); 431304284Sache 4321573Srgrimes bits = 8 - bits; 4331573Srgrimes mtx_lock(&vesa_lock); 434228755Seadler x86bios_intr(®s, 0x10); 4351573Srgrimes if (regs.R_AX != 0x004f) { 43624158Simp mtx_unlock(&vesa_lock); 43724158Simp return (1); 43824158Simp } 43924158Simp for (i = 0; i < colors; ++i) { 440304284Sache r[i] = vesa_palette[i * 4 + 2] << bits; 441304284Sache g[i] = vesa_palette[i * 4 + 1] << bits; 4421573Srgrimes b[i] = vesa_palette[i * 4] << bits; 4431573Srgrimes } 444304284Sache mtx_unlock(&vesa_lock); 445304284Sache 4461573Srgrimes return (0); 447304284Sache} 448304284Sache 449304284Sachestatic int 450304284Sachevesa_bios_load_palette(int start, int colors, u_char *palette, int bits) 4518870Srgrimes{ 45228820Simp x86regs_t regs; 45328820Simp int i; 45428820Simp 4551573Srgrimes x86bios_init_regs(®s); 456121667Stjr regs.R_AX = 0x4f09; 45733664Sjb /* regs.R_BL = 0; */ 45828836Sache regs.R_CX = colors; 45928836Sache regs.R_DX = start; 46028836Sache 46128836Sache regs.R_ES = X86BIOS_PHYSTOSEG(vesa_palette_offs); 46228836Sache regs.R_DI = X86BIOS_PHYSTOOFF(vesa_palette_offs); 463228755Seadler 4641573Srgrimes bits = 8 - bits; 4651573Srgrimes mtx_lock(&vesa_lock); 4661573Srgrimes for (i = 0; i < colors; ++i) { 4671573Srgrimes vesa_palette[i * 4] = palette[i * 3 + 2] >> bits; 4681573Srgrimes vesa_palette[i * 4 + 1] = palette[i * 3 + 1] >> bits; 4691573Srgrimes vesa_palette[i * 4 + 2] = palette[i * 3] >> bits; 470304284Sache vesa_palette[i * 4 + 3] = 0; 471304284Sache } 472304284Sache x86bios_intr(®s, 0x10); 473228755Seadler mtx_unlock(&vesa_lock); 4741573Srgrimes 4751573Srgrimes return (regs.R_AX != 0x004f); 4761573Srgrimes} 4771573Srgrimes 4781573Srgrimesstatic int 479304284Sachevesa_bios_load_palette2(int start, int colors, u_char *r, u_char *g, u_char *b, 480304284Sache int bits) 481304284Sache{ 482304284Sache x86regs_t regs; 483304284Sache int i; 484304284Sache 485304284Sache x86bios_init_regs(®s); 486304284Sache regs.R_AX = 0x4f09; 487304284Sache /* regs.R_BL = 0; */ 488304284Sache regs.R_CX = colors; 489304284Sache regs.R_DX = start; 490304284Sache 491304284Sache regs.R_ES = X86BIOS_PHYSTOSEG(vesa_palette_offs); 492304284Sache regs.R_DI = X86BIOS_PHYSTOOFF(vesa_palette_offs); 493304284Sache 494304284Sache bits = 8 - bits; 495304284Sache mtx_lock(&vesa_lock); 496304284Sache for (i = 0; i < colors; ++i) { 497304284Sache vesa_palette[i * 4] = b[i] >> bits; 498304284Sache vesa_palette[i * 4 + 1] = g[i] >> bits; 499304284Sache vesa_palette[i * 4 + 2] = r[i] >> bits; 500304284Sache vesa_palette[i * 4 + 3] = 0; 501304284Sache } 502304284Sache x86bios_intr(®s, 0x10); 5031573Srgrimes mtx_unlock(&vesa_lock); 504304284Sache 505304284Sache return (regs.R_AX != 0x004f); 5068870Srgrimes} 5071573Srgrimes 508304284Sachestatic ssize_t 509304284Sachevesa_bios_state_buf_size(int state) 510304284Sache{ 511304284Sache x86regs_t regs; 512304284Sache 513304284Sache x86bios_init_regs(®s); 514304284Sache regs.R_AX = 0x4f04; 515304284Sache /* regs.R_DL = STATE_SIZE; */ 516304284Sache regs.R_CX = state; 517304284Sache 518304284Sache x86bios_intr(®s, 0x10); 519304284Sache 5201573Srgrimes if (regs.R_AX != 0x004f) 521228755Seadler return (0); 5221573Srgrimes 5231573Srgrimes return (regs.R_BX * 64); 5248870Srgrimes} 5251573Srgrimes 5261573Srgrimesstatic int 5271573Srgrimesvesa_bios_save_restore(int code, void *p) 5281573Srgrimes{ 529100217Smikeh x86regs_t regs; 5301573Srgrimes 5311573Srgrimes if (code != STATE_SAVE && code != STATE_LOAD) 532304284Sache return (1); 533304284Sache 5341573Srgrimes x86bios_init_regs(®s); 535207981Sgordon regs.R_AX = 0x4f04; 536158812Sache regs.R_DL = code; 537207981Sgordon regs.R_CX = vesa_state; 5381573Srgrimes 53974963Speter regs.R_ES = X86BIOS_PHYSTOSEG(vesa_state_buf_offs); 540304284Sache regs.R_BX = X86BIOS_PHYSTOOFF(vesa_state_buf_offs); 541304284Sache 542304284Sache mtx_lock(&vesa_lock); 543304284Sache switch (code) { 5441573Srgrimes case STATE_SAVE: 5451573Srgrimes x86bios_intr(®s, 0x10); 5461573Srgrimes if (regs.R_AX == 0x004f) 5471573Srgrimes bcopy(vesa_state_buf, p, vesa_state_buf_size); 5481573Srgrimes break; 5491573Srgrimes case STATE_LOAD: 5501573Srgrimes bcopy(p, vesa_state_buf, vesa_state_buf_size); 5511573Srgrimes x86bios_intr(®s, 0x10); 5521573Srgrimes break; 5531573Srgrimes } 5541573Srgrimes mtx_unlock(&vesa_lock); 555180021Smtm 5561573Srgrimes return (regs.R_AX != 0x004f); 5571573Srgrimes} 5581573Srgrimes 5591573Srgrimes#ifdef MODE_TABLE_BROKEN 5601573Srgrimesstatic int 5611573Srgrimesvesa_bios_get_line_length(void) 5621573Srgrimes{ 5631573Srgrimes x86regs_t regs; 5641573Srgrimes 5651573Srgrimes x86bios_init_regs(®s); 5661573Srgrimes regs.R_AX = 0x4f06; 5671573Srgrimes regs.R_BL = 1; 5681573Srgrimes 5691573Srgrimes x86bios_intr(®s, 0x10); 5701573Srgrimes 5711573Srgrimes if (regs.R_AX != 0x004f) 5721573Srgrimes return (-1); 5731573Srgrimes 5741573Srgrimes return (regs.R_BX); 5751573Srgrimes} 5761573Srgrimes#endif 5771573Srgrimes 5781573Srgrimesstatic int 5791573Srgrimesvesa_bios_set_line_length(int pixel, int *bytes, int *lines) 5801573Srgrimes{ 5811573Srgrimes x86regs_t regs; 5821573Srgrimes 5838870Srgrimes x86bios_init_regs(®s); 5841573Srgrimes regs.R_AX = 0x4f06; 5851573Srgrimes /* regs.R_BL = 0; */ 5861573Srgrimes regs.R_CX = pixel; 5871573Srgrimes 5881573Srgrimes x86bios_intr(®s, 0x10); 5891573Srgrimes 5901573Srgrimes#if VESA_DEBUG > 1 5911573Srgrimes printf("bx:%d, cx:%d, dx:%d\n", regs.R_BX, regs.R_CX, regs.R_DX); 5921573Srgrimes#endif 5931573Srgrimes if (regs.R_AX != 0x004f) 5941573Srgrimes return (-1); 5951573Srgrimes 5961573Srgrimes if (bytes != NULL) 5971573Srgrimes *bytes = regs.R_BX; 5981573Srgrimes if (lines != NULL) 59974469Sjlemon *lines = regs.R_DX; 6001573Srgrimes 6011573Srgrimes return (0); 602304284Sache} 603304284Sache 604304284Sache#if 0 605304284Sachestatic int 606304284Sachevesa_bios_get_start(int *x, int *y) 607304284Sache{ 608304284Sache x86regs_t regs; 609304284Sache 610304284Sache x86bios_init_regs(®s); 611304284Sache regs.R_AX = 0x4f07; 612304284Sache regs.R_BL = 1; 613304284Sache 614100217Smikeh x86bios_intr(®s, 0x10); 6151573Srgrimes 6161573Srgrimes if (regs.R_AX != 0x004f) 617304284Sache return (-1); 618228755Seadler 6191573Srgrimes *x = regs.R_CX; 6201573Srgrimes *y = regs.R_DX; 6211573Srgrimes 622159294Sdelphij return (0); 6231573Srgrimes} 624304284Sache#endif 6251573Srgrimes 6261573Srgrimesstatic int 6271573Srgrimesvesa_bios_set_start(int x, int y) 628243779Smarcel{ 6291573Srgrimes x86regs_t regs; 63074963Speter 6311573Srgrimes x86bios_init_regs(®s); 6321573Srgrimes regs.R_AX = 0x4f07; 6331573Srgrimes regs.R_BL = 0x80; 634228755Seadler regs.R_CX = x; 635228755Seadler regs.R_DX = y; 63674963Speter 6371573Srgrimes x86bios_intr(®s, 0x10); 6381573Srgrimes 6391573Srgrimes return (regs.R_AX != 0x004f); 6401573Srgrimes} 6411573Srgrimes 6421573Srgrimes/* map a generic video mode to a known mode */ 6431573Srgrimesstatic int 6441573Srgrimesvesa_map_gen_mode_num(int type, int color, int mode) 645159294Sdelphij{ 646243779Smarcel static struct { 6471573Srgrimes int from; 6481573Srgrimes int to; 6491573Srgrimes } mode_map[] = { 6501573Srgrimes { M_TEXT_132x25, M_VESA_C132x25 }, 6511573Srgrimes { M_TEXT_132x43, M_VESA_C132x43 }, 6521573Srgrimes { M_TEXT_132x50, M_VESA_C132x50 }, 6531573Srgrimes { M_TEXT_132x60, M_VESA_C132x60 }, 6541573Srgrimes }; 6551573Srgrimes int i; 6561573Srgrimes 6571573Srgrimes for (i = 0; i < sizeof(mode_map)/sizeof(mode_map[0]); ++i) { 6581573Srgrimes if (mode_map[i].from == mode) 6591573Srgrimes return (mode_map[i].to); 660228755Seadler } 6618870Srgrimes return (mode); 662243779Smarcel} 663243779Smarcel 664304284Sachestatic int 665243779Smarcelvesa_translate_flags(u_int16_t vflags) 666243779Smarcel{ 667304284Sache static struct { 668304284Sache u_int16_t mask; 669304284Sache int set; 670304284Sache int reset; 671304284Sache } ftable[] = { 6721573Srgrimes { V_MODECOLOR, V_INFO_COLOR, 0 }, 673304284Sache { V_MODEGRAPHICS, V_INFO_GRAPHICS, 0 }, 674304284Sache { V_MODELFB, V_INFO_LINEAR, 0 }, 675304284Sache { V_MODENONVGA, V_INFO_NONVGA, 0 }, 676304284Sache }; 6771573Srgrimes int flags; 6781573Srgrimes int i; 6791573Srgrimes 6801573Srgrimes for (flags = 0, i = 0; i < sizeof(ftable)/sizeof(ftable[0]); ++i) { 681304284Sache flags |= (vflags & ftable[i].mask) ? 6821573Srgrimes ftable[i].set : ftable[i].reset; 6831573Srgrimes } 6841573Srgrimes return (flags); 6851573Srgrimes} 6861573Srgrimes 687304284Sachestatic int 6881573Srgrimesvesa_translate_mmodel(u_int8_t vmodel) 6891573Srgrimes{ 690304284Sache static struct { 691304284Sache u_int8_t vmodel; 692304284Sache int mmodel; 693304284Sache } mtable[] = { 6941573Srgrimes { V_MMTEXT, V_INFO_MM_TEXT }, 6951573Srgrimes { V_MMCGA, V_INFO_MM_CGA }, 6961573Srgrimes { V_MMHGC, V_INFO_MM_HGC }, 6971573Srgrimes { V_MMEGA, V_INFO_MM_PLANAR }, 6981573Srgrimes { V_MMPACKED, V_INFO_MM_PACKED }, 6991573Srgrimes { V_MMDIRCOLOR, V_INFO_MM_DIRECT }, 700304284Sache }; 701304284Sache int i; 702304284Sache 703304284Sache for (i = 0; mtable[i].mmodel >= 0; ++i) { 704304284Sache if (mtable[i].vmodel == vmodel) 7051573Srgrimes return (mtable[i].mmodel); 70674963Speter } 7071573Srgrimes return (V_INFO_MM_OTHER); 708228755Seadler} 709228755Seadler 7101573Srgrimesstatic int 7111573Srgrimesvesa_get_bpscanline(struct vesa_mode *vmode) 7121573Srgrimes{ 7131573Srgrimes int bpsl; 7141573Srgrimes 715159294Sdelphij if ((vmode->v_modeattr & V_MODEGRAPHICS) != 0) { 716159294Sdelphij /* Find the minimum length. */ 717243779Smarcel switch (vmode->v_bpp / vmode->v_planes) { 7181573Srgrimes case 1: 71990045Sobrien bpsl = vmode->v_width / 8; 7201573Srgrimes break; 721304284Sache case 2: 722304284Sache bpsl = vmode->v_width / 4; 7231573Srgrimes break; 724288098Srodrigc case 4: 7251573Srgrimes bpsl = vmode->v_width / 2; 726304284Sache break; 727304284Sache default: 728304284Sache bpsl = vmode->v_width * ((vmode->v_bpp + 7) / 8); 729304284Sache bpsl /= vmode->v_planes; 7301573Srgrimes break; 731304284Sache } 732304284Sache 733304284Sache /* Use VBE 3.0 information if it looks sane. */ 734304284Sache if ((vmode->v_modeattr & V_MODELFB) != 0 && 735304284Sache vesa_adp_info->v_version >= 0x0300 && 736304284Sache vmode->v_linbpscanline > bpsl) 737304284Sache return (vmode->v_linbpscanline); 7381573Srgrimes 7391573Srgrimes /* Return the minimum if the mode table looks absurd. */ 740304284Sache if (vmode->v_bpscanline < bpsl) 741304284Sache return (bpsl); 742304284Sache } 743304284Sache 744304284Sache return (vmode->v_bpscanline); 745304284Sache} 7461573Srgrimes 7471573Srgrimes#define VESA_MAXSTR 256 7481573Srgrimes 7491573Srgrimes#define VESA_STRCPY(dst, src) do { \ 750288098Srodrigc char *str; \ 7511573Srgrimes int i; \ 752288098Srodrigc dst = malloc(VESA_MAXSTR, M_DEVBUF, M_WAITOK); \ 7531573Srgrimes str = x86bios_offset(BIOS_SADDRTOLADDR(src)); \ 7541573Srgrimes for (i = 0; i < VESA_MAXSTR - 1 && str[i] != '\0'; i++) \ 755288098Srodrigc dst[i] = str[i]; \ 756304284Sache dst[i] = '\0'; \ 757288098Srodrigc} while (0) 758288098Srodrigc 759159294Sdelphijstatic int 76090045Sobrienvesa_bios_init(void) 761132817Stjr{ 762132817Stjr struct vesa_mode vmode; 763132817Stjr struct vesa_info *buf; 7641573Srgrimes video_info_t *p; 765243779Smarcel x86regs_t regs; 766243779Smarcel size_t bsize; 767304284Sache size_t msize; 768304284Sache void *vmbuf; 769243779Smarcel uint8_t *vbios; 770243779Smarcel uint32_t offs; 771243779Smarcel uint16_t vers; 7721573Srgrimes int is_via_cle266; 773304284Sache int modes; 774304284Sache int i; 7751573Srgrimes 776304284Sache if (vesa_init_done) 777132817Stjr return (0); 77874963Speter 779159294Sdelphij vesa_bios_offs = VESA_BIOS_OFFSET; 780304284Sache 781304284Sache /* 782132817Stjr * If the VBE real mode interrupt vector is not found, try BIOS POST. 783132817Stjr */ 784304284Sache vesa_bios_int10 = x86bios_get_intr(0x10); 785304284Sache if (vesa_bios_int10 == 0) { 786132817Stjr if (vesa_bios_post() != 0) 787132817Stjr return (1); 788132817Stjr vesa_bios_int10 = x86bios_get_intr(0x10); 789304284Sache if (vesa_bios_int10 == 0) 790304284Sache return (1); 791132817Stjr } 792304284Sache 793132817Stjr /* 794132817Stjr * Shadow video ROM. 795304284Sache */ 796304284Sache offs = vesa_bios_int10; 797304284Sache if (vesa_shadow_rom) { 798304284Sache vbios = x86bios_get_orm(vesa_bios_offs); 799304284Sache if (vbios != NULL) { 800304284Sache vesa_bios_size = vbios[2] * 512; 8011573Srgrimes if (((VESA_BIOS_OFFSET << 12) & 0xffff0000) == 802304284Sache (vesa_bios_int10 & 0xffff0000) && 8031573Srgrimes vesa_bios_size > (vesa_bios_int10 & 0xffff)) { 8041573Srgrimes vesa_bios = x86bios_alloc(&vesa_bios_offs, 805304284Sache vesa_bios_size, M_WAITOK); 806304284Sache bcopy(vbios, vesa_bios, vesa_bios_size); 80774963Speter offs = ((vesa_bios_offs << 12) & 0xffff0000) + 80874963Speter (vesa_bios_int10 & 0xffff); 8091573Srgrimes x86bios_set_intr(0x10, offs); 8101573Srgrimes } 811304284Sache } 8121573Srgrimes if (vesa_bios == NULL) 8131573Srgrimes printf("VESA: failed to shadow video ROM\n"); 814304284Sache } 8151573Srgrimes if (bootverbose) 8161573Srgrimes printf("VESA: INT 0x10 vector 0x%04x:0x%04x\n", 8171573Srgrimes (offs >> 16) & 0xffff, offs & 0xffff); 8181573Srgrimes 819304284Sache x86bios_init_regs(®s); 820304284Sache regs.R_AX = 0x4f00; 821304284Sache 822304284Sache vmbuf = x86bios_alloc(&offs, sizeof(*buf), M_WAITOK); 823304284Sache 824304284Sache regs.R_ES = X86BIOS_PHYSTOSEG(offs); 825304284Sache regs.R_DI = X86BIOS_PHYSTOOFF(offs); 826304284Sache 827304284Sache bcopy("VBE2", vmbuf, 4); /* try for VBE2 data */ 828304284Sache x86bios_intr(®s, 0x10); 829304284Sache 830304284Sache if (regs.R_AX != 0x004f || bcmp("VESA", vmbuf, 4) != 0) 8311573Srgrimes goto fail; 8321573Srgrimes 8331573Srgrimes vesa_adp_info = buf = malloc(sizeof(*buf), M_DEVBUF, M_WAITOK); 8341573Srgrimes bcopy(vmbuf, buf, sizeof(*buf)); 835249381Semaste 8361573Srgrimes if (bootverbose) { 8371573Srgrimes printf("VESA: information block\n"); 8381573Srgrimes hexdump(buf, sizeof(*buf), NULL, HD_OMIT_CHARS); 8391573Srgrimes } 8401573Srgrimes 8411573Srgrimes vers = buf->v_version = le16toh(buf->v_version); 8421573Srgrimes buf->v_oemstr = le32toh(buf->v_oemstr); 8431573Srgrimes buf->v_flags = le32toh(buf->v_flags); 8441573Srgrimes buf->v_modetable = le32toh(buf->v_modetable); 8451573Srgrimes buf->v_memsize = le16toh(buf->v_memsize); 8461573Srgrimes buf->v_revision = le16toh(buf->v_revision); 8471573Srgrimes buf->v_venderstr = le32toh(buf->v_venderstr); 8481573Srgrimes buf->v_prodstr = le32toh(buf->v_prodstr); 849304284Sache buf->v_revstr = le32toh(buf->v_revstr); 850304284Sache 8511573Srgrimes if (vers < 0x0102) { 85290045Sobrien printf("VESA: VBE version %d.%d is not supported; " 853316613Spfg "version 1.2 or later is required.\n", 8541573Srgrimes ((vers & 0xf000) >> 12) * 10 + ((vers & 0x0f00) >> 8), 8551573Srgrimes ((vers & 0x00f0) >> 4) * 10 + (vers & 0x000f)); 8561573Srgrimes goto fail; 857243779Smarcel } 858243779Smarcel 859304284Sache VESA_STRCPY(vesa_oemstr, buf->v_oemstr); 86080525Smikeh if (vers >= 0x0200) { 86180525Smikeh VESA_STRCPY(vesa_venderstr, buf->v_venderstr); 86274307Sjlemon VESA_STRCPY(vesa_prodstr, buf->v_prodstr); 863316613Spfg VESA_STRCPY(vesa_revstr, buf->v_revstr); 864316613Spfg } 865316613Spfg is_via_cle266 = strncmp(vesa_oemstr, VESA_VIA_CLE266, 866243758Smarcel sizeof(VESA_VIA_CLE266)) == 0; 867228755Seadler 8681573Srgrimes if (buf->v_modetable == 0) 8691573Srgrimes goto fail; 8701573Srgrimes 8711573Srgrimes msize = (size_t)buf->v_memsize * 64 * 1024; 872158812Sache 8731573Srgrimes vesa_vmodetab = x86bios_offset(BIOS_SADDRTOLADDR(buf->v_modetable)); 8741573Srgrimes 8751573Srgrimes for (i = 0, modes = 0; (i < (M_VESA_MODE_MAX - M_VESA_BASE + 1)) && 8761573Srgrimes (vesa_vmodetab[i] != 0xffff); ++i) { 877304284Sache vesa_vmodetab[i] = le16toh(vesa_vmodetab[i]); 878304284Sache if (vesa_bios_get_mode(vesa_vmodetab[i], &vmode, M_WAITOK)) 879304284Sache continue; 880304284Sache 881304284Sache vmode.v_modeattr = le16toh(vmode.v_modeattr); 882304284Sache vmode.v_wgran = le16toh(vmode.v_wgran); 883304284Sache vmode.v_wsize = le16toh(vmode.v_wsize); 884304284Sache vmode.v_waseg = le16toh(vmode.v_waseg); 885304284Sache vmode.v_wbseg = le16toh(vmode.v_wbseg); 886304284Sache vmode.v_posfunc = le32toh(vmode.v_posfunc); 887304284Sache vmode.v_bpscanline = le16toh(vmode.v_bpscanline); 888304284Sache vmode.v_width = le16toh(vmode.v_width); 889304284Sache vmode.v_height = le16toh(vmode.v_height); 890243779Smarcel vmode.v_lfb = le32toh(vmode.v_lfb); 891304284Sache vmode.v_offscreen = le32toh(vmode.v_offscreen); 892304284Sache vmode.v_offscreensize = le16toh(vmode.v_offscreensize); 893304284Sache vmode.v_linbpscanline = le16toh(vmode.v_linbpscanline); 894304284Sache vmode.v_maxpixelclock = le32toh(vmode.v_maxpixelclock); 89574918Speter 896304284Sache /* reject unsupported modes */ 89774918Speter#if 0 89874918Speter if ((vmode.v_modeattr & 8991573Srgrimes (V_MODESUPP | V_MODEOPTINFO | V_MODENONVGA)) != 9001573Srgrimes (V_MODESUPP | V_MODEOPTINFO)) 9011573Srgrimes continue; 902228755Seadler#else 9031573Srgrimes if ((vmode.v_modeattr & V_MODEOPTINFO) == 0) { 9041573Srgrimes#if VESA_DEBUG > 1 9051573Srgrimes printf("Rejecting VESA %s mode: %d x %d x %d bpp " 9061573Srgrimes " attr = %x\n", 9071573Srgrimes vmode.v_modeattr & V_MODEGRAPHICS ? 9081573Srgrimes "graphics" : "text", 9091573Srgrimes vmode.v_width, vmode.v_height, vmode.v_bpp, 910159294Sdelphij vmode.v_modeattr); 9111573Srgrimes#endif 9121573Srgrimes continue; 9131573Srgrimes } 914227753Stheraven#endif 915227753Stheraven 9161573Srgrimes bsize = vesa_get_bpscanline(&vmode) * vmode.v_height; 9171573Srgrimes if ((vmode.v_modeattr & V_MODEGRAPHICS) != 0) 9181573Srgrimes bsize *= vmode.v_planes; 9191573Srgrimes 9201573Srgrimes /* Does it have enough memory to support this mode? */ 9211573Srgrimes if (msize < bsize) { 922228755Seadler#if VESA_DEBUG > 1 9238870Srgrimes printf("Rejecting VESA %s mode: %d x %d x %d bpp " 9241573Srgrimes " attr = %x, not enough memory\n", 925228755Seadler vmode.v_modeattr & V_MODEGRAPHICS ? 9261573Srgrimes "graphics" : "text", 927228755Seadler vmode.v_width, vmode.v_height, vmode.v_bpp, 9281573Srgrimes vmode.v_modeattr); 9291573Srgrimes#endif 930228755Seadler continue; 9311573Srgrimes } 9321573Srgrimes if (bsize > vesa_vmem_max) 9331573Srgrimes vesa_vmem_max = bsize; 9341573Srgrimes 935228755Seadler /* expand the array if necessary */ 936304284Sache if (modes >= vesa_vmode_max) { 9371573Srgrimes vesa_vmode_max += MODE_TABLE_DELTA; 9381573Srgrimes p = malloc(sizeof(*vesa_vmode) * (vesa_vmode_max + 1), 9391573Srgrimes M_DEVBUF, M_WAITOK); 940227753Stheraven#if VESA_DEBUG > 1 941304284Sache printf("vesa_bios_init(): modes:%d, vesa_mode_max:%d\n", 942304284Sache modes, vesa_vmode_max); 943304284Sache#endif 944304284Sache if (modes > 0) { 945304284Sache bcopy(vesa_vmode, p, sizeof(*vesa_vmode)*modes); 946304284Sache free(vesa_vmode, M_DEVBUF); 9471573Srgrimes } 9481573Srgrimes vesa_vmode = p; 9491573Srgrimes } 9501573Srgrimes 9511573Srgrimes#if VESA_DEBUG > 1 952228755Seadler printf("Found VESA %s mode: %d x %d x %d bpp\n", 9531573Srgrimes vmode.v_modeattr & V_MODEGRAPHICS ? "graphics" : "text", 9541573Srgrimes vmode.v_width, vmode.v_height, vmode.v_bpp); 9551573Srgrimes#endif 956228755Seadler if (is_via_cle266) { 9571573Srgrimes if ((vmode.v_width & 0xff00) >> 8 == vmode.v_height - 1) { 9581573Srgrimes vmode.v_width &= 0xff; 9591573Srgrimes vmode.v_waseg = 0xb8000 >> 4; 960228755Seadler } 9611573Srgrimes } 9621573Srgrimes 9631573Srgrimes /* copy some fields */ 9641573Srgrimes bzero(&vesa_vmode[modes], sizeof(vesa_vmode[modes])); 965159294Sdelphij vesa_vmode[modes].vi_mode = vesa_vmodetab[i]; 9661573Srgrimes vesa_vmode[modes].vi_width = vmode.v_width; 967158812Sache vesa_vmode[modes].vi_height = vmode.v_height; 96890045Sobrien vesa_vmode[modes].vi_depth = vmode.v_bpp; 9691573Srgrimes vesa_vmode[modes].vi_planes = vmode.v_planes; 9701573Srgrimes vesa_vmode[modes].vi_cwidth = vmode.v_cwidth; 9711573Srgrimes vesa_vmode[modes].vi_cheight = vmode.v_cheight; 9721573Srgrimes vesa_vmode[modes].vi_window = (vm_offset_t)vmode.v_waseg << 4; 9731573Srgrimes /* XXX window B */ 9741573Srgrimes vesa_vmode[modes].vi_window_size = vmode.v_wsize * 1024; 9751573Srgrimes vesa_vmode[modes].vi_window_gran = vmode.v_wgran * 1024; 97674918Speter if (vmode.v_modeattr & V_MODELFB) 9771573Srgrimes vesa_vmode[modes].vi_buffer = vmode.v_lfb; 9781573Srgrimes vesa_vmode[modes].vi_buffer_size = bsize; 9791573Srgrimes vesa_vmode[modes].vi_mem_model = 9801573Srgrimes vesa_translate_mmodel(vmode.v_memmodel); 981159294Sdelphij switch (vesa_vmode[modes].vi_mem_model) { 9821573Srgrimes case V_INFO_MM_DIRECT: 983304284Sache if ((vmode.v_modeattr & V_MODELFB) != 0 && 9841573Srgrimes vers >= 0x0300) { 985304284Sache vesa_vmode[modes].vi_pixel_fields[0] = 9861573Srgrimes vmode.v_linredfieldpos; 98774918Speter vesa_vmode[modes].vi_pixel_fields[1] = 988304284Sache vmode.v_lingreenfieldpos; 989304284Sache vesa_vmode[modes].vi_pixel_fields[2] = 99074918Speter vmode.v_linbluefieldpos; 991304284Sache vesa_vmode[modes].vi_pixel_fields[3] = 99274918Speter vmode.v_linresfieldpos; 9931573Srgrimes vesa_vmode[modes].vi_pixel_fsizes[0] = 9941573Srgrimes vmode.v_linredmasksize; 995228755Seadler vesa_vmode[modes].vi_pixel_fsizes[1] = 9961573Srgrimes vmode.v_lingreenmasksize; 997228755Seadler vesa_vmode[modes].vi_pixel_fsizes[2] = 9981573Srgrimes vmode.v_linbluemasksize; 9991573Srgrimes vesa_vmode[modes].vi_pixel_fsizes[3] = 10001573Srgrimes vmode.v_linresmasksize; 1001159294Sdelphij } else { 10021573Srgrimes vesa_vmode[modes].vi_pixel_fields[0] = 1003304284Sache vmode.v_redfieldpos; 10041573Srgrimes vesa_vmode[modes].vi_pixel_fields[1] = 100574921Speter vmode.v_greenfieldpos; 100674918Speter vesa_vmode[modes].vi_pixel_fields[2] = 100774918Speter vmode.v_bluefieldpos; 100874918Speter vesa_vmode[modes].vi_pixel_fields[3] = 10091573Srgrimes vmode.v_resfieldpos; 10101573Srgrimes vesa_vmode[modes].vi_pixel_fsizes[0] = 1011228755Seadler vmode.v_redmasksize; 10121573Srgrimes vesa_vmode[modes].vi_pixel_fsizes[1] = 10131573Srgrimes vmode.v_greenmasksize; 10141573Srgrimes vesa_vmode[modes].vi_pixel_fsizes[2] = 1015159294Sdelphij vmode.v_bluemasksize; 10161573Srgrimes vesa_vmode[modes].vi_pixel_fsizes[3] = 1017304284Sache vmode.v_resmasksize; 10181573Srgrimes } 101974921Speter /* FALLTHROUGH */ 102074918Speter case V_INFO_MM_PACKED: 102174918Speter vesa_vmode[modes].vi_pixel_size = (vmode.v_bpp + 7) / 8; 102274918Speter break; 10231573Srgrimes } 1024228755Seadler vesa_vmode[modes].vi_flags = 1025228755Seadler vesa_translate_flags(vmode.v_modeattr) | V_INFO_VESA; 10261573Srgrimes 10271573Srgrimes ++modes; 1028180021Smtm } 1029180021Smtm vesa_vmode[modes].vi_mode = EOT; 10301573Srgrimes 1031159294Sdelphij if (bootverbose) 10321573Srgrimes printf("VESA: %d mode(s) found\n", modes); 10331573Srgrimes 10341573Srgrimes if (modes == 0) 10351573Srgrimes goto fail; 10361573Srgrimes 10371573Srgrimes x86bios_free(vmbuf, sizeof(*buf)); 10381573Srgrimes 103974918Speter /* Probe supported save/restore states. */ 1040159294Sdelphij for (i = 0; i < 4; i++) 10411573Srgrimes if (vesa_bios_state_buf_size(1 << i) > 0) 1042132817Stjr vesa_state |= 1 << i; 1043132817Stjr if (vesa_state != 0) 10441573Srgrimes vesa_state_buf_size = vesa_bios_state_buf_size(vesa_state); 1045132817Stjr vesa_palette = x86bios_alloc(&vesa_palette_offs, 1046132817Stjr VESA_PALETTE_SIZE + vesa_state_buf_size, M_WAITOK); 1047304284Sache if (vesa_state_buf_size > 0) { 1048304284Sache vesa_state_buf = vesa_palette + VESA_PALETTE_SIZE; 1049304284Sache vesa_state_buf_offs = vesa_palette_offs + VESA_PALETTE_SIZE; 1050304284Sache } 1051304284Sache 1052304284Sache return (0); 1053304284Sache 1054304284Sachefail: 105574921Speter x86bios_free(vmbuf, sizeof(buf)); 1056132817Stjr vesa_bios_uninit(); 1057132817Stjr return (1); 1058132817Stjr} 105974921Speter 106074921Speterstatic void 10611573Srgrimesvesa_bios_uninit(void) 10621573Srgrimes{ 1063304284Sache 1064304284Sache if (vesa_bios != NULL) { 1065304284Sache x86bios_set_intr(0x10, vesa_bios_int10); 1066304284Sache vesa_bios_offs = VESA_BIOS_OFFSET; 1067304284Sache x86bios_free(vesa_bios, vesa_bios_size); 1068304284Sache vesa_bios = NULL; 1069304284Sache } 1070304284Sache if (vesa_adp_info != NULL) { 1071304284Sache free(vesa_adp_info, M_DEVBUF); 1072304284Sache vesa_adp_info = NULL; 1073304284Sache } 1074304284Sache if (vesa_oemstr != NULL) { 1075304284Sache free(vesa_oemstr, M_DEVBUF); 1076304284Sache vesa_oemstr = NULL; 1077304284Sache } 1078304284Sache if (vesa_venderstr != NULL) { 1079304284Sache free(vesa_venderstr, M_DEVBUF); 1080304284Sache vesa_venderstr = NULL; 1081304284Sache } 1082304284Sache if (vesa_prodstr != NULL) { 1083304284Sache free(vesa_prodstr, M_DEVBUF); 1084304284Sache vesa_prodstr = NULL; 1085304284Sache } 10861573Srgrimes if (vesa_revstr != NULL) { 10878870Srgrimes free(vesa_revstr, M_DEVBUF); 1088159294Sdelphij vesa_revstr = NULL; 10891573Srgrimes } 109090045Sobrien if (vesa_vmode != NULL) { 10911573Srgrimes free(vesa_vmode, M_DEVBUF); 1092304284Sache vesa_vmode = NULL; 1093304284Sache } 1094304284Sache if (vesa_palette != NULL) { 1095304284Sache x86bios_free(vesa_palette, 1096304284Sache VESA_PALETTE_SIZE + vesa_state_buf_size); 1097304284Sache vesa_palette = NULL; 1098304284Sache } 1099304284Sache} 1100304284Sache 1101304284Sachestatic void 1102304284Sachevesa_clear_modes(video_info_t *info, int color) 1103304284Sache{ 11041573Srgrimes while (info->vi_mode != EOT) { 11051573Srgrimes if ((info->vi_flags & V_INFO_COLOR) != color) 1106 info->vi_mode = NA; 1107 ++info; 1108 } 1109} 1110 1111/* entry points */ 1112 1113static int 1114vesa_configure(int flags) 1115{ 1116 video_adapter_t *adp; 1117 int adapters; 1118 int error; 1119 int i; 1120 1121 if (vesa_init_done) 1122 return (0); 1123 if (flags & VIO_PROBE_ONLY) 1124 return (0); 1125 1126 /* 1127 * If the VESA module has already been loaded, abort loading 1128 * the module this time. 1129 */ 1130 for (i = 0; (adp = vid_get_adapter(i)) != NULL; ++i) { 1131 if (adp->va_flags & V_ADP_VESA) 1132 return (ENXIO); 1133 if (adp->va_type == KD_VGA) 1134 break; 1135 } 1136 1137 /* 1138 * The VGA adapter is not found. This is because either 1139 * 1) the VGA driver has not been initialized, or 2) the VGA card 1140 * is not present. If 1) is the case, we shall defer 1141 * initialization for now and try again later. 1142 */ 1143 if (adp == NULL) { 1144 vga_sub_configure = vesa_configure; 1145 return (ENODEV); 1146 } 1147 1148 /* count number of registered adapters */ 1149 for (++i; vid_get_adapter(i) != NULL; ++i) 1150 ; 1151 adapters = i; 1152 1153 /* call VESA BIOS */ 1154 vesa_adp = adp; 1155 if (vesa_bios_init()) { 1156 vesa_adp = NULL; 1157 return (ENXIO); 1158 } 1159 vesa_adp->va_flags |= V_ADP_VESA; 1160 1161 /* remove conflicting modes if we have more than one adapter */ 1162 if (adapters > 1) { 1163 vesa_clear_modes(vesa_vmode, 1164 (vesa_adp->va_flags & V_ADP_COLOR) ? 1165 V_INFO_COLOR : 0); 1166 } 1167 1168 if ((error = vesa_load_ioctl()) == 0) { 1169 prevvidsw = vidsw[vesa_adp->va_index]; 1170 vidsw[vesa_adp->va_index] = &vesavidsw; 1171 vesa_init_done = TRUE; 1172 } else { 1173 vesa_adp = NULL; 1174 return (error); 1175 } 1176 1177 return (0); 1178} 1179 1180#if 0 1181static int 1182vesa_nop(void) 1183{ 1184 1185 return (0); 1186} 1187#endif 1188 1189static int 1190vesa_error(void) 1191{ 1192 1193 return (1); 1194} 1195 1196static int 1197vesa_probe(int unit, video_adapter_t **adpp, void *arg, int flags) 1198{ 1199 1200 return ((*prevvidsw->probe)(unit, adpp, arg, flags)); 1201} 1202 1203static int 1204vesa_init(int unit, video_adapter_t *adp, int flags) 1205{ 1206 1207 return ((*prevvidsw->init)(unit, adp, flags)); 1208} 1209 1210static int 1211vesa_get_info(video_adapter_t *adp, int mode, video_info_t *info) 1212{ 1213 int i; 1214 1215 if ((*prevvidsw->get_info)(adp, mode, info) == 0) 1216 return (0); 1217 1218 if (adp != vesa_adp) 1219 return (1); 1220 1221 mode = vesa_map_gen_mode_num(vesa_adp->va_type, 1222 vesa_adp->va_flags & V_ADP_COLOR, mode); 1223 for (i = 0; vesa_vmode[i].vi_mode != EOT; ++i) { 1224 if (vesa_vmode[i].vi_mode == NA) 1225 continue; 1226 if (vesa_vmode[i].vi_mode == mode) { 1227 *info = vesa_vmode[i]; 1228 return (0); 1229 } 1230 } 1231 return (1); 1232} 1233 1234static int 1235vesa_query_mode(video_adapter_t *adp, video_info_t *info) 1236{ 1237 int i; 1238 1239 if ((*prevvidsw->query_mode)(adp, info) == 0) 1240 return (0); 1241 if (adp != vesa_adp) 1242 return (ENODEV); 1243 1244 for (i = 0; vesa_vmode[i].vi_mode != EOT; ++i) { 1245 if ((info->vi_width != 0) 1246 && (info->vi_width != vesa_vmode[i].vi_width)) 1247 continue; 1248 if ((info->vi_height != 0) 1249 && (info->vi_height != vesa_vmode[i].vi_height)) 1250 continue; 1251 if ((info->vi_cwidth != 0) 1252 && (info->vi_cwidth != vesa_vmode[i].vi_cwidth)) 1253 continue; 1254 if ((info->vi_cheight != 0) 1255 && (info->vi_cheight != vesa_vmode[i].vi_cheight)) 1256 continue; 1257 if ((info->vi_depth != 0) 1258 && (info->vi_depth != vesa_vmode[i].vi_depth)) 1259 continue; 1260 if ((info->vi_planes != 0) 1261 && (info->vi_planes != vesa_vmode[i].vi_planes)) 1262 continue; 1263 /* pixel format, memory model */ 1264 if ((info->vi_flags != 0) 1265 && (info->vi_flags != vesa_vmode[i].vi_flags)) 1266 continue; 1267 *info = vesa_vmode[i]; 1268 return (0); 1269 } 1270 return (ENODEV); 1271} 1272 1273static int 1274vesa_set_mode(video_adapter_t *adp, int mode) 1275{ 1276 video_info_t info; 1277 1278 if (adp != vesa_adp) 1279 return ((*prevvidsw->set_mode)(adp, mode)); 1280 1281 mode = vesa_map_gen_mode_num(adp->va_type, 1282 adp->va_flags & V_ADP_COLOR, mode); 1283#if VESA_DEBUG > 0 1284 printf("VESA: set_mode(): %d(%x) -> %d(%x)\n", 1285 adp->va_mode, adp->va_mode, mode, mode); 1286#endif 1287 /* 1288 * If the current mode is a VESA mode and the new mode is not, 1289 * restore the state of the adapter first by setting one of the 1290 * standard VGA mode, so that non-standard, extended SVGA registers 1291 * are set to the state compatible with the standard VGA modes. 1292 * Otherwise (*prevvidsw->set_mode)() may not be able to set up 1293 * the new mode correctly. 1294 */ 1295 if (VESA_MODE(adp->va_mode)) { 1296 if (!VESA_MODE(mode) && 1297 (*prevvidsw->get_info)(adp, mode, &info) == 0) { 1298 if ((adp->va_flags & V_ADP_DAC8) != 0) { 1299 vesa_bios_set_dac(6); 1300 adp->va_flags &= ~V_ADP_DAC8; 1301 } 1302 int10_set_mode(adp->va_initial_bios_mode); 1303 if (adp->va_info.vi_flags & V_INFO_LINEAR) 1304 pmap_unmapdev(adp->va_buffer, vesa_vmem_max); 1305 /* 1306 * Once (*prevvidsw->get_info)() succeeded, 1307 * (*prevvidsw->set_mode)() below won't fail... 1308 */ 1309 } 1310 } 1311 1312 /* we may not need to handle this mode after all... */ 1313 if (!VESA_MODE(mode) && (*prevvidsw->set_mode)(adp, mode) == 0) 1314 return (0); 1315 1316 /* is the new mode supported? */ 1317 if (vesa_get_info(adp, mode, &info)) 1318 return (1); 1319 /* assert(VESA_MODE(mode)); */ 1320 1321#if VESA_DEBUG > 0 1322 printf("VESA: about to set a VESA mode...\n"); 1323#endif 1324 /* don't use the linear frame buffer for text modes. XXX */ 1325 if (!(info.vi_flags & V_INFO_GRAPHICS)) 1326 info.vi_flags &= ~V_INFO_LINEAR; 1327 1328 if ((info.vi_flags & V_INFO_LINEAR) != 0) 1329 mode |= 0x4000; 1330 if (vesa_bios_set_mode(mode | 0x8000)) 1331 return (1); 1332 1333 /* Palette format is reset by the above VBE function call. */ 1334 adp->va_flags &= ~V_ADP_DAC8; 1335 1336 if ((vesa_adp_info->v_flags & V_DAC8) != 0 && 1337 (info.vi_flags & V_INFO_GRAPHICS) != 0 && 1338 vesa_bios_set_dac(8) > 6) 1339 adp->va_flags |= V_ADP_DAC8; 1340 1341 if (adp->va_info.vi_flags & V_INFO_LINEAR) 1342 pmap_unmapdev(adp->va_buffer, vesa_vmem_max); 1343 1344#if VESA_DEBUG > 0 1345 printf("VESA: mode set!\n"); 1346#endif 1347 vesa_adp->va_mode = mode & 0x1ff; /* Mode number is 9-bit. */ 1348 vesa_adp->va_flags &= ~V_ADP_COLOR; 1349 vesa_adp->va_flags |= 1350 (info.vi_flags & V_INFO_COLOR) ? V_ADP_COLOR : 0; 1351 vesa_adp->va_crtc_addr = 1352 (vesa_adp->va_flags & V_ADP_COLOR) ? COLOR_CRTC : MONO_CRTC; 1353 1354 vesa_adp->va_line_width = info.vi_buffer_size / info.vi_height; 1355 if ((info.vi_flags & V_INFO_GRAPHICS) != 0) 1356 vesa_adp->va_line_width /= info.vi_planes; 1357 1358#ifdef MODE_TABLE_BROKEN 1359 /* If VBE function returns bigger bytes per scan line, use it. */ 1360 { 1361 int bpsl = vesa_bios_get_line_length(); 1362 if (bpsl > vesa_adp->va_line_width) { 1363 vesa_adp->va_line_width = bpsl; 1364 info.vi_buffer_size = bpsl * info.vi_height; 1365 if ((info.vi_flags & V_INFO_GRAPHICS) != 0) 1366 info.vi_buffer_size *= info.vi_planes; 1367 } 1368 } 1369#endif 1370 1371 if (info.vi_flags & V_INFO_LINEAR) { 1372#if VESA_DEBUG > 1 1373 printf("VESA: setting up LFB\n"); 1374#endif 1375 vesa_adp->va_buffer = 1376 (vm_offset_t)pmap_mapdev_attr(info.vi_buffer, 1377 vesa_vmem_max, PAT_WRITE_COMBINING); 1378 vesa_adp->va_window = vesa_adp->va_buffer; 1379 vesa_adp->va_window_size = info.vi_buffer_size / info.vi_planes; 1380 vesa_adp->va_window_gran = info.vi_buffer_size / info.vi_planes; 1381 } else { 1382 vesa_adp->va_buffer = 0; 1383 vesa_adp->va_window = (vm_offset_t)x86bios_offset(info.vi_window); 1384 vesa_adp->va_window_size = info.vi_window_size; 1385 vesa_adp->va_window_gran = info.vi_window_gran; 1386 } 1387 vesa_adp->va_buffer_size = info.vi_buffer_size; 1388 vesa_adp->va_window_orig = 0; 1389 vesa_adp->va_disp_start.x = 0; 1390 vesa_adp->va_disp_start.y = 0; 1391#if VESA_DEBUG > 0 1392 printf("vesa_set_mode(): vi_width:%d, line_width:%d\n", 1393 info.vi_width, vesa_adp->va_line_width); 1394#endif 1395 bcopy(&info, &vesa_adp->va_info, sizeof(vesa_adp->va_info)); 1396 1397 /* move hardware cursor out of the way */ 1398 (*vidsw[vesa_adp->va_index]->set_hw_cursor)(vesa_adp, -1, -1); 1399 1400 return (0); 1401} 1402 1403static int 1404vesa_save_font(video_adapter_t *adp, int page, int fontsize, int fontwidth, 1405 u_char *data, int ch, int count) 1406{ 1407 1408 return ((*prevvidsw->save_font)(adp, page, fontsize, fontwidth, data, 1409 ch, count)); 1410} 1411 1412static int 1413vesa_load_font(video_adapter_t *adp, int page, int fontsize, int fontwidth, 1414 u_char *data, int ch, int count) 1415{ 1416 1417 return ((*prevvidsw->load_font)(adp, page, fontsize, fontwidth, data, 1418 ch, count)); 1419} 1420 1421static int 1422vesa_show_font(video_adapter_t *adp, int page) 1423{ 1424 1425 return ((*prevvidsw->show_font)(adp, page)); 1426} 1427 1428static int 1429vesa_save_palette(video_adapter_t *adp, u_char *palette) 1430{ 1431 int bits; 1432 1433 if (adp == vesa_adp && VESA_MODE(adp->va_mode)) { 1434 bits = (adp->va_flags & V_ADP_DAC8) != 0 ? 8 : 6; 1435 if (vesa_bios_save_palette(0, 256, palette, bits) == 0) 1436 return (0); 1437 } 1438 1439 return ((*prevvidsw->save_palette)(adp, palette)); 1440} 1441 1442static int 1443vesa_load_palette(video_adapter_t *adp, u_char *palette) 1444{ 1445 int bits; 1446 1447 if (adp == vesa_adp && VESA_MODE(adp->va_mode)) { 1448 bits = (adp->va_flags & V_ADP_DAC8) != 0 ? 8 : 6; 1449 if (vesa_bios_load_palette(0, 256, palette, bits) == 0) 1450 return (0); 1451 } 1452 1453 return ((*prevvidsw->load_palette)(adp, palette)); 1454} 1455 1456static int 1457vesa_set_border(video_adapter_t *adp, int color) 1458{ 1459 1460 return ((*prevvidsw->set_border)(adp, color)); 1461} 1462 1463static int 1464vesa_save_state(video_adapter_t *adp, void *p, size_t size) 1465{ 1466 vm_offset_t buf; 1467 size_t bsize; 1468 1469 if (adp != vesa_adp || (size == 0 && vesa_state_buf_size == 0)) 1470 return ((*prevvidsw->save_state)(adp, p, size)); 1471 1472 bsize = offsetof(adp_state_t, regs) + vesa_state_buf_size; 1473 if (size == 0) 1474 return (bsize); 1475 if (vesa_state_buf_size > 0 && size < bsize) 1476 return (EINVAL); 1477 1478 if (VESA_MODE(adp->va_mode) && adp->va_buffer != 0) { 1479 buf = adp->va_buffer; 1480 bsize = adp->va_buffer_size; 1481 } else { 1482 buf = adp->va_window; 1483 bsize = adp->va_window_size; 1484 } 1485 if (buf != 0) { 1486 vesa_vmem_buf = malloc(bsize, M_DEVBUF, M_NOWAIT); 1487 if (vesa_vmem_buf != NULL) 1488 bcopy((void *)buf, vesa_vmem_buf, bsize); 1489 } else 1490 vesa_vmem_buf = NULL; 1491 if (vesa_state_buf_size == 0) 1492 return ((*prevvidsw->save_state)(adp, p, size)); 1493 ((adp_state_t *)p)->sig = V_STATE_SIG; 1494 return (vesa_bios_save_restore(STATE_SAVE, ((adp_state_t *)p)->regs)); 1495} 1496 1497static int 1498vesa_load_state(video_adapter_t *adp, void *p) 1499{ 1500 vm_offset_t buf; 1501 size_t bsize; 1502 int error, mode; 1503 1504 if (adp != vesa_adp) 1505 return ((*prevvidsw->load_state)(adp, p)); 1506 1507 /* Try BIOS POST to restore a sane state. */ 1508 (void)vesa_bios_post(); 1509 mode = adp->va_mode; 1510 error = vesa_set_mode(adp, adp->va_initial_mode); 1511 if (mode != adp->va_initial_mode) 1512 error = vesa_set_mode(adp, mode); 1513 1514 if (vesa_vmem_buf != NULL) { 1515 if (error == 0) { 1516 if (VESA_MODE(mode) && adp->va_buffer != 0) { 1517 buf = adp->va_buffer; 1518 bsize = adp->va_buffer_size; 1519 } else { 1520 buf = adp->va_window; 1521 bsize = adp->va_window_size; 1522 } 1523 if (buf != 0) 1524 bcopy(vesa_vmem_buf, (void *)buf, bsize); 1525 } 1526 free(vesa_vmem_buf, M_DEVBUF); 1527 } 1528 if (((adp_state_t *)p)->sig != V_STATE_SIG) 1529 return ((*prevvidsw->load_state)(adp, p)); 1530 return (vesa_bios_save_restore(STATE_LOAD, ((adp_state_t *)p)->regs)); 1531} 1532 1533#if 0 1534static int 1535vesa_get_origin(video_adapter_t *adp, off_t *offset) 1536{ 1537 x86regs_t regs; 1538 1539 x86bios_init_regs(®s); 1540 regs.R_AX = 0x4f05; 1541 regs.R_BL = 0x10; 1542 1543 x86bios_intr(®s, 0x10); 1544 1545 if (regs.R_AX != 0x004f) 1546 return (1); 1547 *offset = regs.DX * adp->va_window_gran; 1548 1549 return (0); 1550} 1551#endif 1552 1553static int 1554vesa_set_origin(video_adapter_t *adp, off_t offset) 1555{ 1556 x86regs_t regs; 1557 1558 /* 1559 * This function should return as quickly as possible to 1560 * maintain good performance of the system. For this reason, 1561 * error checking is kept minimal and let the VESA BIOS to 1562 * detect error. 1563 */ 1564 if (adp != vesa_adp) 1565 return ((*prevvidsw->set_win_org)(adp, offset)); 1566 1567 /* if this is a linear frame buffer, do nothing */ 1568 if (adp->va_info.vi_flags & V_INFO_LINEAR) 1569 return (0); 1570 /* XXX */ 1571 if (adp->va_window_gran == 0) 1572 return (1); 1573 1574 x86bios_init_regs(®s); 1575 regs.R_AX = 0x4f05; 1576 regs.R_DX = offset / adp->va_window_gran; 1577 1578 x86bios_intr(®s, 0x10); 1579 1580 if (regs.R_AX != 0x004f) 1581 return (1); 1582 1583 x86bios_init_regs(®s); 1584 regs.R_AX = 0x4f05; 1585 regs.R_BL = 1; 1586 regs.R_DX = offset / adp->va_window_gran; 1587 x86bios_intr(®s, 0x10); 1588 1589 adp->va_window_orig = (offset/adp->va_window_gran)*adp->va_window_gran; 1590 return (0); /* XXX */ 1591} 1592 1593static int 1594vesa_read_hw_cursor(video_adapter_t *adp, int *col, int *row) 1595{ 1596 1597 return ((*prevvidsw->read_hw_cursor)(adp, col, row)); 1598} 1599 1600static int 1601vesa_set_hw_cursor(video_adapter_t *adp, int col, int row) 1602{ 1603 1604 return ((*prevvidsw->set_hw_cursor)(adp, col, row)); 1605} 1606 1607static int 1608vesa_set_hw_cursor_shape(video_adapter_t *adp, int base, int height, 1609 int celsize, int blink) 1610{ 1611 1612 return ((*prevvidsw->set_hw_cursor_shape)(adp, base, height, celsize, 1613 blink)); 1614} 1615 1616static int 1617vesa_blank_display(video_adapter_t *adp, int mode) 1618{ 1619 1620 /* XXX: use VESA DPMS */ 1621 return ((*prevvidsw->blank_display)(adp, mode)); 1622} 1623 1624static int 1625vesa_mmap(video_adapter_t *adp, vm_ooffset_t offset, vm_paddr_t *paddr, 1626 int prot, vm_memattr_t *memattr) 1627{ 1628 1629#if VESA_DEBUG > 0 1630 printf("vesa_mmap(): window:0x%tx, buffer:0x%tx, offset:0x%jx\n", 1631 adp->va_info.vi_window, adp->va_info.vi_buffer, offset); 1632#endif 1633 1634 if ((adp == vesa_adp) && 1635 (adp->va_info.vi_flags & V_INFO_LINEAR) != 0) { 1636 /* va_window_size == va_buffer_size/vi_planes */ 1637 /* XXX: is this correct? */ 1638 if (offset > adp->va_window_size - PAGE_SIZE) 1639 return (-1); 1640 *paddr = adp->va_info.vi_buffer + offset; 1641 return (0); 1642 } 1643 return ((*prevvidsw->mmap)(adp, offset, paddr, prot, memattr)); 1644} 1645 1646static int 1647vesa_clear(video_adapter_t *adp) 1648{ 1649 1650 return ((*prevvidsw->clear)(adp)); 1651} 1652 1653static int 1654vesa_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy) 1655{ 1656 1657 return ((*prevvidsw->fill_rect)(adp, val, x, y, cx, cy)); 1658} 1659 1660static int 1661vesa_bitblt(video_adapter_t *adp,...) 1662{ 1663 1664 /* FIXME */ 1665 return (1); 1666} 1667 1668static int 1669get_palette(video_adapter_t *adp, int base, int count, 1670 u_char *red, u_char *green, u_char *blue, u_char *trans) 1671{ 1672 u_char *r; 1673 u_char *g; 1674 u_char *b; 1675 int bits; 1676 int error; 1677 1678 if (base < 0 || base >= 256 || count < 0 || count > 256) 1679 return (1); 1680 if ((base + count) > 256) 1681 return (1); 1682 if (!VESA_MODE(adp->va_mode)) 1683 return (1); 1684 1685 bits = (adp->va_flags & V_ADP_DAC8) != 0 ? 8 : 6; 1686 r = malloc(count * 3, M_DEVBUF, M_WAITOK); 1687 g = r + count; 1688 b = g + count; 1689 error = vesa_bios_save_palette2(base, count, r, g, b, bits); 1690 if (error == 0) { 1691 copyout(r, red, count); 1692 copyout(g, green, count); 1693 copyout(b, blue, count); 1694 if (trans != NULL) { 1695 bzero(r, count); 1696 copyout(r, trans, count); 1697 } 1698 } 1699 free(r, M_DEVBUF); 1700 1701 return (error); 1702} 1703 1704static int 1705set_palette(video_adapter_t *adp, int base, int count, 1706 u_char *red, u_char *green, u_char *blue, u_char *trans) 1707{ 1708 u_char *r; 1709 u_char *g; 1710 u_char *b; 1711 int bits; 1712 int error; 1713 1714 if (base < 0 || base >= 256 || count < 0 || count > 256) 1715 return (1); 1716 if ((base + count) > 256) 1717 return (1); 1718 if (!VESA_MODE(adp->va_mode)) 1719 return (1); 1720 1721 bits = (adp->va_flags & V_ADP_DAC8) != 0 ? 8 : 6; 1722 r = malloc(count * 3, M_DEVBUF, M_WAITOK); 1723 g = r + count; 1724 b = g + count; 1725 copyin(red, r, count); 1726 copyin(green, g, count); 1727 copyin(blue, b, count); 1728 1729 error = vesa_bios_load_palette2(base, count, r, g, b, bits); 1730 free(r, M_DEVBUF); 1731 1732 return (error); 1733} 1734 1735static int 1736vesa_ioctl(video_adapter_t *adp, u_long cmd, caddr_t arg) 1737{ 1738 int bytes; 1739 1740 if (adp != vesa_adp) 1741 return ((*prevvidsw->ioctl)(adp, cmd, arg)); 1742 1743 switch (cmd) { 1744 case FBIO_SETWINORG: /* set frame buffer window origin */ 1745 if (!VESA_MODE(adp->va_mode)) 1746 return (*prevvidsw->ioctl)(adp, cmd, arg); 1747 return (vesa_set_origin(adp, *(off_t *)arg) ? ENODEV : 0); 1748 1749 case FBIO_SETDISPSTART: /* set display start address */ 1750 if (!VESA_MODE(adp->va_mode)) 1751 return ((*prevvidsw->ioctl)(adp, cmd, arg)); 1752 if (vesa_bios_set_start(((video_display_start_t *)arg)->x, 1753 ((video_display_start_t *)arg)->y)) 1754 return (ENODEV); 1755 adp->va_disp_start.x = ((video_display_start_t *)arg)->x; 1756 adp->va_disp_start.y = ((video_display_start_t *)arg)->y; 1757 return (0); 1758 1759 case FBIO_SETLINEWIDTH: /* set line length in pixel */ 1760 if (!VESA_MODE(adp->va_mode)) 1761 return ((*prevvidsw->ioctl)(adp, cmd, arg)); 1762 if (vesa_bios_set_line_length(*(u_int *)arg, &bytes, NULL)) 1763 return (ENODEV); 1764 adp->va_line_width = bytes; 1765#if VESA_DEBUG > 1 1766 printf("new line width:%d\n", adp->va_line_width); 1767#endif 1768 return (0); 1769 1770 case FBIO_GETPALETTE: /* get color palette */ 1771 if (get_palette(adp, ((video_color_palette_t *)arg)->index, 1772 ((video_color_palette_t *)arg)->count, 1773 ((video_color_palette_t *)arg)->red, 1774 ((video_color_palette_t *)arg)->green, 1775 ((video_color_palette_t *)arg)->blue, 1776 ((video_color_palette_t *)arg)->transparent)) 1777 return ((*prevvidsw->ioctl)(adp, cmd, arg)); 1778 return (0); 1779 1780 1781 case FBIO_SETPALETTE: /* set color palette */ 1782 if (set_palette(adp, ((video_color_palette_t *)arg)->index, 1783 ((video_color_palette_t *)arg)->count, 1784 ((video_color_palette_t *)arg)->red, 1785 ((video_color_palette_t *)arg)->green, 1786 ((video_color_palette_t *)arg)->blue, 1787 ((video_color_palette_t *)arg)->transparent)) 1788 return ((*prevvidsw->ioctl)(adp, cmd, arg)); 1789 return (0); 1790 1791 case FBIOGETCMAP: /* get color palette */ 1792 if (get_palette(adp, ((struct fbcmap *)arg)->index, 1793 ((struct fbcmap *)arg)->count, 1794 ((struct fbcmap *)arg)->red, 1795 ((struct fbcmap *)arg)->green, 1796 ((struct fbcmap *)arg)->blue, NULL)) 1797 return ((*prevvidsw->ioctl)(adp, cmd, arg)); 1798 return (0); 1799 1800 case FBIOPUTCMAP: /* set color palette */ 1801 if (set_palette(adp, ((struct fbcmap *)arg)->index, 1802 ((struct fbcmap *)arg)->count, 1803 ((struct fbcmap *)arg)->red, 1804 ((struct fbcmap *)arg)->green, 1805 ((struct fbcmap *)arg)->blue, NULL)) 1806 return ((*prevvidsw->ioctl)(adp, cmd, arg)); 1807 return (0); 1808 1809 default: 1810 return ((*prevvidsw->ioctl)(adp, cmd, arg)); 1811 } 1812} 1813 1814static int 1815vesa_diag(video_adapter_t *adp, int level) 1816{ 1817 int error; 1818 1819 /* call the previous handler first */ 1820 error = (*prevvidsw->diag)(adp, level); 1821 if (error) 1822 return (error); 1823 1824 if (adp != vesa_adp) 1825 return (1); 1826 1827 if (level <= 0) 1828 return (0); 1829 1830 return (0); 1831} 1832 1833static int 1834vesa_bios_info(int level) 1835{ 1836#if VESA_DEBUG > 1 1837 struct vesa_mode vmode; 1838 int i; 1839#endif 1840 uint16_t vers; 1841 1842 vers = vesa_adp_info->v_version; 1843 1844 if (bootverbose) { 1845 /* general adapter information */ 1846 printf( 1847 "VESA: v%d.%d, %dk memory, flags:0x%x, mode table:%p (%x)\n", 1848 (vers >> 12) * 10 + ((vers & 0x0f00) >> 8), 1849 ((vers & 0x00f0) >> 4) * 10 + (vers & 0x000f), 1850 vesa_adp_info->v_memsize * 64, vesa_adp_info->v_flags, 1851 vesa_vmodetab, vesa_adp_info->v_modetable); 1852 1853 /* OEM string */ 1854 if (vesa_oemstr != NULL) 1855 printf("VESA: %s\n", vesa_oemstr); 1856 } 1857 1858 if (level <= 0) 1859 return (0); 1860 1861 if (vers >= 0x0200 && bootverbose) { 1862 /* vender name, product name, product revision */ 1863 printf("VESA: %s %s %s\n", 1864 (vesa_venderstr != NULL) ? vesa_venderstr : "unknown", 1865 (vesa_prodstr != NULL) ? vesa_prodstr : "unknown", 1866 (vesa_revstr != NULL) ? vesa_revstr : "?"); 1867 } 1868 1869#if VESA_DEBUG > 1 1870 /* mode information */ 1871 for (i = 0; 1872 (i < (M_VESA_MODE_MAX - M_VESA_BASE + 1)) 1873 && (vesa_vmodetab[i] != 0xffff); ++i) { 1874 if (vesa_bios_get_mode(vesa_vmodetab[i], &vmode, M_NOWAIT)) 1875 continue; 1876 1877 /* print something for diagnostic purpose */ 1878 printf("VESA: mode:0x%03x, flags:0x%04x", 1879 vesa_vmodetab[i], vmode.v_modeattr); 1880 if (vmode.v_modeattr & V_MODEOPTINFO) { 1881 if (vmode.v_modeattr & V_MODEGRAPHICS) { 1882 printf(", G %dx%dx%d %d, ", 1883 vmode.v_width, vmode.v_height, 1884 vmode.v_bpp, vmode.v_planes); 1885 } else { 1886 printf(", T %dx%d, ", 1887 vmode.v_width, vmode.v_height); 1888 } 1889 printf("font:%dx%d, ", 1890 vmode.v_cwidth, vmode.v_cheight); 1891 printf("pages:%d, mem:%d", 1892 vmode.v_ipages + 1, vmode.v_memmodel); 1893 } 1894 if (vmode.v_modeattr & V_MODELFB) { 1895 printf("\nVESA: LFB:0x%x, off:0x%x, off_size:0x%x", 1896 vmode.v_lfb, vmode.v_offscreen, 1897 vmode.v_offscreensize*1024); 1898 } 1899 printf("\n"); 1900 printf("VESA: window A:0x%x (%x), window B:0x%x (%x), ", 1901 vmode.v_waseg, vmode.v_waattr, 1902 vmode.v_wbseg, vmode.v_wbattr); 1903 printf("size:%dk, gran:%dk\n", 1904 vmode.v_wsize, vmode.v_wgran); 1905 } 1906#endif /* VESA_DEBUG > 1 */ 1907 1908 return (0); 1909} 1910 1911/* module loading */ 1912 1913static int 1914vesa_load(void) 1915{ 1916 int error; 1917 1918 if (vesa_init_done) 1919 return (0); 1920 1921 mtx_init(&vesa_lock, "VESA lock", NULL, MTX_DEF); 1922 1923 /* locate a VGA adapter */ 1924 vesa_adp = NULL; 1925 error = vesa_configure(0); 1926 1927 if (error == 0) 1928 vesa_bios_info(bootverbose); 1929 1930 return (error); 1931} 1932 1933static int 1934vesa_unload(void) 1935{ 1936 u_char palette[256*3]; 1937 int error; 1938 1939 /* if the adapter is currently in a VESA mode, don't unload */ 1940 if ((vesa_adp != NULL) && VESA_MODE(vesa_adp->va_mode)) 1941 return (EBUSY); 1942 /* 1943 * FIXME: if there is at least one vty which is in a VESA mode, 1944 * we shouldn't be unloading! XXX 1945 */ 1946 1947 if ((error = vesa_unload_ioctl()) == 0) { 1948 if (vesa_adp != NULL) { 1949 if ((vesa_adp->va_flags & V_ADP_DAC8) != 0) { 1950 vesa_bios_save_palette(0, 256, palette, 8); 1951 vesa_bios_set_dac(6); 1952 vesa_adp->va_flags &= ~V_ADP_DAC8; 1953 vesa_bios_load_palette(0, 256, palette, 6); 1954 } 1955 vesa_adp->va_flags &= ~V_ADP_VESA; 1956 vidsw[vesa_adp->va_index] = prevvidsw; 1957 } 1958 } 1959 1960 vesa_bios_uninit(); 1961 mtx_destroy(&vesa_lock); 1962 1963 return (error); 1964} 1965 1966static int 1967vesa_mod_event(module_t mod, int type, void *data) 1968{ 1969 1970 switch (type) { 1971 case MOD_LOAD: 1972 return (vesa_load()); 1973 case MOD_UNLOAD: 1974 return (vesa_unload()); 1975 } 1976 return (EOPNOTSUPP); 1977} 1978 1979static moduledata_t vesa_mod = { 1980 "vesa", 1981 vesa_mod_event, 1982 NULL, 1983}; 1984 1985DECLARE_MODULE(vesa, vesa_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE); 1986MODULE_DEPEND(vesa, x86bios, 1, 1, 1); 1987 1988#endif /* VGA_NO_MODE_CHANGE */ 1989