Remove unused definition.
[dragonfly.git] / sys / dev / video / i386 / vesa / vesa.c
CommitLineData
984263bc
MD
1/*-
2 * Copyright (c) 1998 Kazutaka YOKOTA and Michael Smith
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer as
10 * the first lines of this file unmodified.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 * $FreeBSD: src/sys/i386/isa/vesa.c,v 1.32.2.1 2002/08/13 02:42:33 rwatson Exp $
e2cb63eb 27 * $DragonFly: src/sys/dev/video/i386/vesa/vesa.c,v 1.19 2007/04/04 07:17:52 swildner Exp $
984263bc
MD
28 */
29
30#include "opt_vga.h"
5d556382 31#include "opt_vesa.h"
984263bc
MD
32
33#ifndef VGA_NO_MODE_CHANGE
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/kernel.h>
38#include <sys/module.h>
39#include <sys/malloc.h>
40#include <sys/fbio.h>
052b6ffa 41#include <sys/thread2.h>
984263bc
MD
42
43#include <vm/vm.h>
44#include <vm/vm_extern.h>
45#include <vm/vm_kern.h>
46#include <vm/pmap.h>
47
48#include <machine/md_var.h>
49#include <machine/vm86.h>
50#include <machine/pc/bios.h>
51#include <machine/pc/vesa.h>
52
583d9351
MD
53#include <dev/video/fb/fbreg.h>
54#include <dev/video/fb/vgareg.h>
984263bc
MD
55
56#ifndef __i386__
1f2de5d4 57#include <bus/isa/isareg.h>
984263bc 58#else
583d9351 59#include <bus/isa/i386/isa.h>
984263bc
MD
60#endif
61
5d556382
MD
62#define VESA_VIA_CLE266 "VIA CLE266\r\n"
63
984263bc
MD
64#ifndef VESA_DEBUG
65#define VESA_DEBUG 0
66#endif
67
68/* VESA video adapter state buffer stub */
69struct adp_state {
70 int sig;
71#define V_STATE_SIG 0x61736576
72 u_char regs[1];
73};
74typedef struct adp_state adp_state_t;
75
76/* VESA video adapter */
77static video_adapter_t *vesa_adp = NULL;
78static int vesa_state_buf_size = 0;
b5f86e2f
SW
79#define VESA_VM86_BUFSIZE (3 * PAGE_SIZE)
80static void *vesa_vm86_buf;
984263bc
MD
81
82/* VESA functions */
83#if 0
84static int vesa_nop(void);
85#endif
86static int vesa_error(void);
87static vi_probe_t vesa_probe;
88static vi_init_t vesa_init;
89static vi_get_info_t vesa_get_info;
90static vi_query_mode_t vesa_query_mode;
91static vi_set_mode_t vesa_set_mode;
92static vi_save_font_t vesa_save_font;
93static vi_load_font_t vesa_load_font;
94static vi_show_font_t vesa_show_font;
95static vi_save_palette_t vesa_save_palette;
96static vi_load_palette_t vesa_load_palette;
97static vi_set_border_t vesa_set_border;
98static vi_save_state_t vesa_save_state;
99static vi_load_state_t vesa_load_state;
100static vi_set_win_org_t vesa_set_origin;
101static vi_read_hw_cursor_t vesa_read_hw_cursor;
102static vi_set_hw_cursor_t vesa_set_hw_cursor;
103static vi_set_hw_cursor_shape_t vesa_set_hw_cursor_shape;
104static vi_blank_display_t vesa_blank_display;
105static vi_mmap_t vesa_mmap;
106static vi_ioctl_t vesa_ioctl;
107static vi_clear_t vesa_clear;
108static vi_fill_rect_t vesa_fill_rect;
109static vi_bitblt_t vesa_bitblt;
110static vi_diag_t vesa_diag;
111static int vesa_bios_info(int level);
112static struct vm86context vesa_vmcontext;
113
114static video_switch_t vesavidsw = {
115 vesa_probe,
116 vesa_init,
117 vesa_get_info,
118 vesa_query_mode,
119 vesa_set_mode,
120 vesa_save_font,
121 vesa_load_font,
122 vesa_show_font,
123 vesa_save_palette,
124 vesa_load_palette,
125 vesa_set_border,
126 vesa_save_state,
127 vesa_load_state,
128 vesa_set_origin,
129 vesa_read_hw_cursor,
130 vesa_set_hw_cursor,
131 vesa_set_hw_cursor_shape,
132 vesa_blank_display,
133 vesa_mmap,
134 vesa_ioctl,
135 vesa_clear,
136 vesa_fill_rect,
137 vesa_bitblt,
138 vesa_error,
139 vesa_error,
140 vesa_diag,
141};
142
143static video_switch_t *prevvidsw;
144
145/* VESA BIOS video modes */
984263bc
MD
146#define EOT (-1)
147#define NA (-2)
148
149#define MODE_TABLE_DELTA 8
150
151static int vesa_vmode_max = 0;
152static video_info_t vesa_vmode_empty = { EOT };
153static video_info_t *vesa_vmode = &vesa_vmode_empty;
154
155static int vesa_init_done = FALSE;
156static int has_vesa_bios = FALSE;
157static struct vesa_info *vesa_adp_info = NULL;
2e458ca3 158static uint16_t *vesa_vmodetab = NULL;
984263bc 159static char *vesa_oemstr = NULL;
2e46b2a9 160static char *vesa_vendorstr = NULL;
984263bc
MD
161static char *vesa_prodstr = NULL;
162static char *vesa_revstr = NULL;
163
164/* local macros and functions */
165#define BIOS_SADDRTOLADDR(p) ((((p) & 0xffff0000) >> 12) + ((p) & 0x0000ffff))
166
167static int int10_set_mode(int mode);
168static int vesa_bios_get_mode(int mode, struct vesa_mode *vmode);
169static int vesa_bios_set_mode(int mode);
170static int vesa_bios_get_dac(void);
171static int vesa_bios_set_dac(int bits);
172static int vesa_bios_save_palette(int start, int colors, u_char *palette,
173 int bits);
174static int vesa_bios_save_palette2(int start, int colors, u_char *r, u_char *g,
175 u_char *b, int bits);
736a05b5 176static int vesa_bios_load_palette(int start, int colors, const u_char *palette,
984263bc
MD
177 int bits);
178#ifdef notyet
179static int vesa_bios_load_palette2(int start, int colors, u_char *r, u_char *g,
180 u_char *b, int bits);
181#endif
182#define STATE_SIZE 0
183#define STATE_SAVE 1
184#define STATE_LOAD 2
185#define STATE_HW (1<<0)
186#define STATE_DATA (1<<1)
187#define STATE_DAC (1<<2)
188#define STATE_REG (1<<3)
189#define STATE_MOST (STATE_HW | STATE_DATA | STATE_REG)
190#define STATE_ALL (STATE_HW | STATE_DATA | STATE_DAC | STATE_REG)
191static int vesa_bios_state_buf_size(void);
192static int vesa_bios_save_restore(int code, void *p, size_t size);
193static int vesa_bios_get_line_length(void);
194static int vesa_bios_set_line_length(int pixel, int *bytes, int *lines);
195#if 0
196static int vesa_bios_get_start(int *x, int *y);
197#endif
198static int vesa_bios_set_start(int x, int y);
2e458ca3
SW
199static int vesa_translate_flags(uint16_t vflags);
200static int vesa_translate_mmodel(uint8_t vmodel);
201static void *vesa_fix_ptr(uint32_t p, uint16_t seg, uint16_t off,
984263bc
MD
202 u_char *buf);
203static int vesa_bios_init(void);
204static void vesa_clear_modes(video_info_t *info, int color);
205static vm_offset_t vesa_map_buffer(u_int paddr, size_t size);
206static void vesa_unmap_buffer(vm_offset_t vaddr, size_t size);
207
208#if 0
209static int vesa_get_origin(video_adapter_t *adp, off_t *offset);
210#endif
211
212static void
213dump_buffer(u_char *buf, size_t len)
214{
2e458ca3 215 int i;
984263bc 216
2e458ca3 217 for (i = 0; i < len;) {
e3869ec7 218 kprintf("%02x ", buf[i]);
2e458ca3 219 if ((++i % 16) == 0)
e3869ec7 220 kprintf("\n");
2e458ca3 221 }
984263bc
MD
222}
223
224/* INT 10 BIOS calls */
225static int
226int10_set_mode(int mode)
227{
228 struct vm86frame vmf;
229
230 bzero(&vmf, sizeof(vmf));
231 vmf.vmf_eax = 0x0000 | mode;
232 vm86_intcall(0x10, &vmf);
233 return 0;
234}
235
236/* VESA BIOS calls */
237static int
238vesa_bios_get_mode(int mode, struct vesa_mode *vmode)
239{
240 struct vm86frame vmf;
241 u_char *buf;
242 int err;
243
244 bzero(&vmf, sizeof(vmf));
245 vmf.vmf_eax = 0x4f01;
246 vmf.vmf_ecx = mode;
b5f86e2f 247 buf = vesa_vm86_buf;
984263bc
MD
248 vm86_getptr(&vesa_vmcontext, (vm_offset_t)buf, &vmf.vmf_es, &vmf.vmf_di);
249
250 err = vm86_datacall(0x10, &vmf, &vesa_vmcontext);
251 if ((err != 0) || (vmf.vmf_ax != 0x4f))
252 return 1;
253 bcopy(buf, vmode, sizeof(*vmode));
254 return 0;
255}
256
257static int
258vesa_bios_set_mode(int mode)
259{
260 struct vm86frame vmf;
261 int err;
262
263 bzero(&vmf, sizeof(vmf));
264 vmf.vmf_eax = 0x4f02;
265 vmf.vmf_ebx = mode;
266 err = vm86_intcall(0x10, &vmf);
267 return ((err != 0) || (vmf.vmf_ax != 0x4f));
268}
269
270static int
271vesa_bios_get_dac(void)
272{
273 struct vm86frame vmf;
274 int err;
275
276 bzero(&vmf, sizeof(vmf));
277 vmf.vmf_eax = 0x4f08;
278 vmf.vmf_ebx = 1; /* get DAC width */
279 err = vm86_intcall(0x10, &vmf);
280 if ((err != 0) || (vmf.vmf_ax != 0x4f))
281 return 6; /* XXX */
282 return ((vmf.vmf_ebx >> 8) & 0x00ff);
283}
284
285static int
286vesa_bios_set_dac(int bits)
287{
288 struct vm86frame vmf;
289 int err;
290
291 bzero(&vmf, sizeof(vmf));
292 vmf.vmf_eax = 0x4f08;
293 vmf.vmf_ebx = (bits << 8);
294 err = vm86_intcall(0x10, &vmf);
295 if ((err != 0) || (vmf.vmf_ax != 0x4f))
296 return 6; /* XXX */
297 return ((vmf.vmf_ebx >> 8) & 0x00ff);
298}
299
300static int
301vesa_bios_save_palette(int start, int colors, u_char *palette, int bits)
302{
303 struct vm86frame vmf;
304 u_char *p;
305 int err;
306 int i;
307
308 bzero(&vmf, sizeof(vmf));
309 vmf.vmf_eax = 0x4f09;
310 vmf.vmf_ebx = 1; /* get primary palette data */
311 vmf.vmf_ecx = colors;
312 vmf.vmf_edx = start;
b5f86e2f 313 p = vesa_vm86_buf;
984263bc
MD
314 vm86_getptr(&vesa_vmcontext, (vm_offset_t)p, &vmf.vmf_es, &vmf.vmf_di);
315
316 err = vm86_datacall(0x10, &vmf, &vesa_vmcontext);
317 if ((err != 0) || (vmf.vmf_ax != 0x4f))
318 return 1;
319
320 bits = 8 - bits;
321 for (i = 0; i < colors; ++i) {
322 palette[i*3] = p[i*4 + 2] << bits;
323 palette[i*3 + 1] = p[i*4 + 1] << bits;
324 palette[i*3 + 2] = p[i*4] << bits;
325 }
326 return 0;
327}
328
329static int
330vesa_bios_save_palette2(int start, int colors, u_char *r, u_char *g, u_char *b,
331 int bits)
332{
333 struct vm86frame vmf;
334 u_char *p;
335 int err;
336 int i;
337
338 bzero(&vmf, sizeof(vmf));
339 vmf.vmf_eax = 0x4f09;
340 vmf.vmf_ebx = 1; /* get primary palette data */
341 vmf.vmf_ecx = colors;
342 vmf.vmf_edx = start;
b5f86e2f 343 p = vesa_vm86_buf;
984263bc
MD
344 vm86_getptr(&vesa_vmcontext, (vm_offset_t)p, &vmf.vmf_es, &vmf.vmf_di);
345
346 err = vm86_datacall(0x10, &vmf, &vesa_vmcontext);
347 if ((err != 0) || (vmf.vmf_ax != 0x4f))
348 return 1;
349
350 bits = 8 - bits;
351 for (i = 0; i < colors; ++i) {
352 r[i] = p[i*4 + 2] << bits;
353 g[i] = p[i*4 + 1] << bits;
354 b[i] = p[i*4] << bits;
355 }
356 return 0;
357}
358
359static int
736a05b5 360vesa_bios_load_palette(int start, int colors, const u_char *palette, int bits)
984263bc
MD
361{
362 struct vm86frame vmf;
363 u_char *p;
364 int err;
365 int i;
366
b5f86e2f 367 p = vesa_vm86_buf;
984263bc
MD
368 bits = 8 - bits;
369 for (i = 0; i < colors; ++i) {
370 p[i*4] = palette[i*3 + 2] >> bits;
371 p[i*4 + 1] = palette[i*3 + 1] >> bits;
372 p[i*4 + 2] = palette[i*3] >> bits;
373 p[i*4 + 3] = 0;
374 }
375
376 bzero(&vmf, sizeof(vmf));
377 vmf.vmf_eax = 0x4f09;
378 vmf.vmf_ebx = 0; /* set primary palette data */
379 vmf.vmf_ecx = colors;
380 vmf.vmf_edx = start;
381 vm86_getptr(&vesa_vmcontext, (vm_offset_t)p, &vmf.vmf_es, &vmf.vmf_di);
382
383 err = vm86_datacall(0x10, &vmf, &vesa_vmcontext);
384 return ((err != 0) || (vmf.vmf_ax != 0x4f));
385}
386
387#ifdef notyet
388static int
389vesa_bios_load_palette2(int start, int colors, u_char *r, u_char *g, u_char *b,
390 int bits)
391{
392 struct vm86frame vmf;
393 u_char *p;
394 int err;
395 int i;
396
b5f86e2f 397 p = vesa_vm86_buf;
984263bc
MD
398 bits = 8 - bits;
399 for (i = 0; i < colors; ++i) {
400 p[i*4] = b[i] >> bits;
401 p[i*4 + 1] = g[i] >> bits;
402 p[i*4 + 2] = r[i] >> bits;
403 p[i*4 + 3] = 0;
404 }
405
406 bzero(&vmf, sizeof(vmf));
407 vmf.vmf_eax = 0x4f09;
408 vmf.vmf_ebx = 0; /* set primary palette data */
409 vmf.vmf_ecx = colors;
410 vmf.vmf_edx = start;
411 vm86_getptr(&vesa_vmcontext, (vm_offset_t)p, &vmf.vmf_es, &vmf.vmf_di);
412
413 err = vm86_datacall(0x10, &vmf, &vesa_vmcontext);
414 return ((err != 0) || (vmf.vmf_ax != 0x4f));
415}
416#endif
417
418static int
419vesa_bios_state_buf_size(void)
420{
421 struct vm86frame vmf;
422 int err;
423
424 bzero(&vmf, sizeof(vmf));
425 vmf.vmf_eax = 0x4f04;
b5f86e2f 426 vmf.vmf_ecx = STATE_ALL;
984263bc
MD
427 vmf.vmf_edx = STATE_SIZE;
428 err = vm86_intcall(0x10, &vmf);
429 if ((err != 0) || (vmf.vmf_ax != 0x4f))
430 return 0;
431 return vmf.vmf_bx*64;
432}
433
434static int
435vesa_bios_save_restore(int code, void *p, size_t size)
436{
437 struct vm86frame vmf;
438 u_char *buf;
439 int err;
440
b5f86e2f
SW
441 if (size > VESA_VM86_BUFSIZE)
442 return (1);
443
984263bc
MD
444 bzero(&vmf, sizeof(vmf));
445 vmf.vmf_eax = 0x4f04;
b5f86e2f 446 vmf.vmf_ecx = STATE_ALL;
984263bc 447 vmf.vmf_edx = code; /* STATE_SAVE/STATE_LOAD */
b5f86e2f 448 buf = vesa_vm86_buf;
f81610c1 449 vm86_getptr(&vesa_vmcontext, (vm_offset_t)buf, &vmf.vmf_es, &vmf.vmf_bx);
984263bc
MD
450 bcopy(p, buf, size);
451
452 err = vm86_datacall(0x10, &vmf, &vesa_vmcontext);
b5f86e2f 453 bcopy(buf, p, size);
984263bc
MD
454 return ((err != 0) || (vmf.vmf_ax != 0x4f));
455}
456
457static int
458vesa_bios_get_line_length(void)
459{
460 struct vm86frame vmf;
461 int err;
462
463 bzero(&vmf, sizeof(vmf));
464 vmf.vmf_eax = 0x4f06;
465 vmf.vmf_ebx = 1; /* get scan line length */
466 err = vm86_intcall(0x10, &vmf);
467 if ((err != 0) || (vmf.vmf_ax != 0x4f))
468 return -1;
469 return vmf.vmf_bx; /* line length in bytes */
470}
471
472static int
473vesa_bios_set_line_length(int pixel, int *bytes, int *lines)
474{
475 struct vm86frame vmf;
476 int err;
477
478 bzero(&vmf, sizeof(vmf));
479 vmf.vmf_eax = 0x4f06;
480 vmf.vmf_ebx = 0; /* set scan line length in pixel */
481 vmf.vmf_ecx = pixel;
482 err = vm86_intcall(0x10, &vmf);
483#if VESA_DEBUG > 1
e3869ec7 484 kprintf("bx:%d, cx:%d, dx:%d\n", vmf.vmf_bx, vmf.vmf_cx, vmf.vmf_dx);
984263bc
MD
485#endif
486 if ((err != 0) || (vmf.vmf_ax != 0x4f))
487 return 1;
488 if (bytes)
489 *bytes = vmf.vmf_bx;
490 if (lines)
491 *lines = vmf.vmf_dx;
492 return 0;
493}
494
495#if 0
496static int
497vesa_bios_get_start(int *x, int *y)
498{
499 struct vm86frame vmf;
500 int err;
501
502 bzero(&vmf, sizeof(vmf));
503 vmf.vmf_eax = 0x4f07;
504 vmf.vmf_ebx = 1; /* get display start */
505 err = vm86_intcall(0x10, &vmf);
506 if ((err != 0) || (vmf.vmf_ax != 0x4f))
507 return 1;
508 *x = vmf.vmf_cx;
509 *y = vmf.vmf_dx;
510 return 0;
511}
512#endif
513
514static int
515vesa_bios_set_start(int x, int y)
516{
517 struct vm86frame vmf;
518 int err;
519
520 bzero(&vmf, sizeof(vmf));
521 vmf.vmf_eax = 0x4f07;
522 vmf.vmf_ebx = 0x80; /* set display start */
523 vmf.vmf_edx = y;
524 vmf.vmf_ecx = x;
525 err = vm86_intcall(0x10, &vmf);
526 return ((err != 0) || (vmf.vmf_ax != 0x4f));
527}
528
984263bc 529static int
2e458ca3 530vesa_translate_flags(uint16_t vflags)
984263bc
MD
531{
532 static struct {
2e458ca3 533 uint16_t mask;
984263bc
MD
534 int set;
535 int reset;
536 } ftable[] = {
537 { V_MODECOLOR, V_INFO_COLOR, 0 },
538 { V_MODEGRAPHICS, V_INFO_GRAPHICS, 0 },
539 { V_MODELFB, V_INFO_LINEAR, 0 },
540 };
541 int flags;
542 int i;
543
544 for (flags = 0, i = 0; i < sizeof(ftable)/sizeof(ftable[0]); ++i) {
545 flags |= (vflags & ftable[i].mask) ?
546 ftable[i].set : ftable[i].reset;
547 }
548 return flags;
549}
550
551static int
2e458ca3 552vesa_translate_mmodel(uint8_t vmodel)
984263bc
MD
553{
554 static struct {
2e458ca3 555 uint8_t vmodel;
984263bc
MD
556 int mmodel;
557 } mtable[] = {
558 { V_MMTEXT, V_INFO_MM_TEXT },
559 { V_MMCGA, V_INFO_MM_CGA },
560 { V_MMHGC, V_INFO_MM_HGC },
561 { V_MMEGA, V_INFO_MM_PLANAR },
562 { V_MMPACKED, V_INFO_MM_PACKED },
563 { V_MMDIRCOLOR, V_INFO_MM_DIRECT },
564 };
565 int i;
566
567 for (i = 0; mtable[i].mmodel >= 0; ++i) {
568 if (mtable[i].vmodel == vmodel)
569 return mtable[i].mmodel;
570 }
571 return V_INFO_MM_OTHER;
572}
573
2e458ca3
SW
574static void *
575vesa_fix_ptr(uint32_t p, uint16_t seg, uint16_t off, u_char *buf)
984263bc
MD
576{
577 if (p == 0)
578 return NULL;
579 if (((p >> 16) == seg) && ((p & 0xffff) >= off))
580 return (void *)(buf + ((p & 0xffff) - off));
581 else {
582 p = BIOS_SADDRTOLADDR(p);
583 return (void *)BIOS_PADDRTOVADDR(p);
584 }
585}
586
587static int
588vesa_bios_init(void)
589{
590 static u_char buf[512];
591 struct vm86frame vmf;
592 struct vesa_mode vmode;
593 video_info_t *p;
594 u_char *vmbuf;
5d556382 595 int is_via_cle266;
984263bc
MD
596 int modes;
597 int err;
598 int i;
599
600 if (vesa_init_done)
601 return 0;
602
603 has_vesa_bios = FALSE;
604 vesa_adp_info = NULL;
605 vesa_vmode_max = 0;
606 vesa_vmode[0].vi_mode = EOT;
607
b5f86e2f 608 /* Allocate a buffer and add each page to the vm86 context. */
efda3bd0 609 vesa_vm86_buf = kmalloc(VESA_VM86_BUFSIZE, M_DEVBUF, M_WAITOK | M_ZERO);
b5f86e2f
SW
610 KASSERT(((vm_offset_t)vesa_vm86_buf & PAGE_MASK) == 0,
611 ("bad vesa_vm86_buf alignment"));
612 for (i = 0; i < howmany(VESA_VM86_BUFSIZE, PAGE_SIZE); i++)
613 vm86_addpage(&vesa_vmcontext, i + 1,
614 (vm_offset_t)vesa_vm86_buf + PAGE_SIZE * i);
615
616 vmbuf = vesa_vm86_buf;
984263bc
MD
617 bzero(&vmf, sizeof(vmf)); /* paranoia */
618 bcopy("VBE2", vmbuf, 4); /* try for VBE2 data */
619 vmf.vmf_eax = 0x4f00;
620 vm86_getptr(&vesa_vmcontext, (vm_offset_t)vmbuf, &vmf.vmf_es, &vmf.vmf_di);
621
622 err = vm86_datacall(0x10, &vmf, &vesa_vmcontext);
623 if ((err != 0) || (vmf.vmf_ax != 0x4f) || bcmp("VESA", vmbuf, 4))
624 return 1;
625 bcopy(vmbuf, buf, sizeof(buf));
626 vesa_adp_info = (struct vesa_info *)buf;
627 if (bootverbose) {
e3869ec7 628 kprintf("VESA: information block\n");
984263bc
MD
629 dump_buffer(buf, 64);
630 }
984263bc 631 if (vesa_adp_info->v_version < 0x0102) {
e3869ec7 632 kprintf("VESA: VBE version %d.%d is not supported; "
984263bc
MD
633 "version 1.2 or later is required.\n",
634 ((vesa_adp_info->v_version & 0xf000) >> 12) * 10
635 + ((vesa_adp_info->v_version & 0x0f00) >> 8),
636 ((vesa_adp_info->v_version & 0x00f0) >> 4) * 10
637 + (vesa_adp_info->v_version & 0x000f));
638 return 1;
639 }
640
641 /* fix string ptrs */
642 vesa_oemstr = (char *)vesa_fix_ptr(vesa_adp_info->v_oemstr,
643 vmf.vmf_es, vmf.vmf_di, buf);
5d556382
MD
644 is_via_cle266 = strcmp(vesa_oemstr, VESA_VIA_CLE266) == 0;
645
984263bc 646 if (vesa_adp_info->v_version >= 0x0200) {
2e46b2a9
SW
647 vesa_vendorstr =
648 (char *)vesa_fix_ptr(vesa_adp_info->v_vendorstr,
984263bc
MD
649 vmf.vmf_es, vmf.vmf_di, buf);
650 vesa_prodstr =
651 (char *)vesa_fix_ptr(vesa_adp_info->v_prodstr,
652 vmf.vmf_es, vmf.vmf_di, buf);
653 vesa_revstr =
654 (char *)vesa_fix_ptr(vesa_adp_info->v_revstr,
655 vmf.vmf_es, vmf.vmf_di, buf);
656 }
657
658 /* obtain video mode information */
2e458ca3 659 vesa_vmodetab = (uint16_t *)vesa_fix_ptr(vesa_adp_info->v_modetable,
984263bc
MD
660 vmf.vmf_es, vmf.vmf_di, buf);
661 if (vesa_vmodetab == NULL)
662 return 1;
663 for (i = 0, modes = 0;
664 (i < (M_VESA_MODE_MAX - M_VESA_BASE + 1))
665 && (vesa_vmodetab[i] != 0xffff); ++i) {
666 if (vesa_bios_get_mode(vesa_vmodetab[i], &vmode))
667 continue;
668
669 /* reject unsupported modes */
670#if 0
671 if ((vmode.v_modeattr & (V_MODESUPP | V_MODEOPTINFO
672 | V_MODENONVGA))
673 != (V_MODESUPP | V_MODEOPTINFO))
674 continue;
675#else
5d556382
MD
676 if ((vmode.v_modeattr & V_MODEOPTINFO) == 0) {
677#if VESA_DEBUG > 1
e3869ec7 678 kprintf("Rejecting VESA %s mode: %d x %d x %d bpp attr = %x\n",
5d556382
MD
679 vmode.v_modeattr & V_MODEGRAPHICS ? "graphics" : "text",
680 vmode.v_width, vmode.v_height, vmode.v_bpp,
681 vmode.v_modeattr);
682#endif
984263bc 683 continue;
5d556382 684 }
984263bc
MD
685#endif
686
687 /* expand the array if necessary */
688 if (modes >= vesa_vmode_max) {
689 vesa_vmode_max += MODE_TABLE_DELTA;
83d04dc5 690 p = kmalloc(sizeof(*vesa_vmode)*(vesa_vmode_max + 1),
984263bc
MD
691 M_DEVBUF, M_WAITOK);
692#if VESA_DEBUG > 1
e3869ec7 693 kprintf("vesa_bios_init(): modes:%d, vesa_mode_max:%d\n",
984263bc
MD
694 modes, vesa_vmode_max);
695#endif
696 if (modes > 0) {
697 bcopy(vesa_vmode, p, sizeof(*vesa_vmode)*modes);
efda3bd0 698 kfree(vesa_vmode, M_DEVBUF);
984263bc
MD
699 }
700 vesa_vmode = p;
701 }
702
5d556382 703#if VESA_DEBUG > 1
e3869ec7 704 kprintf("Found VESA %s mode: %d x %d x %d bpp\n",
5d556382
MD
705 vmode.v_modeattr & V_MODEGRAPHICS ? "graphics" : "text",
706 vmode.v_width, vmode.v_height, vmode.v_bpp);
707#endif
708 if (is_via_cle266) {
2e458ca3
SW
709 if ((vmode.v_width & 0xff00) >> 8 == vmode.v_height - 1) {
710 vmode.v_width &= 0xff;
711 vmode.v_waseg = 0xb8000 >> 4;
712 }
5d556382
MD
713 }
714
984263bc
MD
715 /* copy some fields */
716 bzero(&vesa_vmode[modes], sizeof(vesa_vmode[modes]));
717 vesa_vmode[modes].vi_mode = vesa_vmodetab[i];
718 vesa_vmode[modes].vi_width = vmode.v_width;
719 vesa_vmode[modes].vi_height = vmode.v_height;
720 vesa_vmode[modes].vi_depth = vmode.v_bpp;
721 vesa_vmode[modes].vi_planes = vmode.v_planes;
722 vesa_vmode[modes].vi_cwidth = vmode.v_cwidth;
723 vesa_vmode[modes].vi_cheight = vmode.v_cheight;
724 vesa_vmode[modes].vi_window = (u_int)vmode.v_waseg << 4;
725 /* XXX window B */
726 vesa_vmode[modes].vi_window_size = vmode.v_wsize*1024;
727 vesa_vmode[modes].vi_window_gran = vmode.v_wgran*1024;
728 if (vmode.v_modeattr & V_MODELFB)
729 vesa_vmode[modes].vi_buffer = vmode.v_lfb;
730 else
731 vesa_vmode[modes].vi_buffer = 0;
732 /* XXX */
733 vesa_vmode[modes].vi_buffer_size
734 = vesa_adp_info->v_memsize*64*1024;
735#if 0
736 if (vmode.v_offscreen > vmode.v_lfb)
737 vesa_vmode[modes].vi_buffer_size
738 = vmode.v_offscreen + vmode.v_offscreensize*1024
739 - vmode.v_lfb;
740 else
741 vesa_vmode[modes].vi_buffer_size
742 = vmode.v_offscreen + vmode.v_offscreensize*1024
743#endif
744 vesa_vmode[modes].vi_mem_model
745 = vesa_translate_mmodel(vmode.v_memmodel);
746 vesa_vmode[modes].vi_pixel_fields[0] = 0;
747 vesa_vmode[modes].vi_pixel_fields[1] = 0;
748 vesa_vmode[modes].vi_pixel_fields[2] = 0;
749 vesa_vmode[modes].vi_pixel_fields[3] = 0;
750 vesa_vmode[modes].vi_pixel_fsizes[0] = 0;
751 vesa_vmode[modes].vi_pixel_fsizes[1] = 0;
752 vesa_vmode[modes].vi_pixel_fsizes[2] = 0;
753 vesa_vmode[modes].vi_pixel_fsizes[3] = 0;
754 if (vesa_vmode[modes].vi_mem_model == V_INFO_MM_PACKED) {
755 vesa_vmode[modes].vi_pixel_size = (vmode.v_bpp + 7)/8;
756 } else if (vesa_vmode[modes].vi_mem_model == V_INFO_MM_DIRECT) {
757 vesa_vmode[modes].vi_pixel_size = (vmode.v_bpp + 7)/8;
758 vesa_vmode[modes].vi_pixel_fields[0]
759 = vmode.v_redfieldpos;
760 vesa_vmode[modes].vi_pixel_fields[1]
761 = vmode.v_greenfieldpos;
762 vesa_vmode[modes].vi_pixel_fields[2]
763 = vmode.v_bluefieldpos;
764 vesa_vmode[modes].vi_pixel_fields[3]
765 = vmode.v_resfieldpos;
766 vesa_vmode[modes].vi_pixel_fsizes[0]
767 = vmode.v_redmasksize;
768 vesa_vmode[modes].vi_pixel_fsizes[1]
769 = vmode.v_greenmasksize;
770 vesa_vmode[modes].vi_pixel_fsizes[2]
771 = vmode.v_bluemasksize;
772 vesa_vmode[modes].vi_pixel_fsizes[3]
773 = vmode.v_resmasksize;
774 } else {
775 vesa_vmode[modes].vi_pixel_size = 0;
776 }
777
778 vesa_vmode[modes].vi_flags
779 = vesa_translate_flags(vmode.v_modeattr) | V_INFO_VESA;
780 ++modes;
781 }
782 vesa_vmode[modes].vi_mode = EOT;
783 if (bootverbose)
e3869ec7 784 kprintf("VESA: %d mode(s) found\n", modes);
984263bc
MD
785
786 has_vesa_bios = (modes > 0);
b5f86e2f
SW
787 if (!has_vesa_bios)
788 return (1);
789
790 return (0);
984263bc
MD
791}
792
793static void
794vesa_clear_modes(video_info_t *info, int color)
795{
796 while (info->vi_mode != EOT) {
797 if ((info->vi_flags & V_INFO_COLOR) != color)
798 info->vi_mode = NA;
799 ++info;
800 }
801}
802
803static vm_offset_t
804vesa_map_buffer(u_int paddr, size_t size)
805{
806 vm_offset_t vaddr;
807 u_int off;
808
809 off = paddr - trunc_page(paddr);
810 vaddr = (vm_offset_t)pmap_mapdev(paddr - off, size + off);
811#if VESA_DEBUG > 1
e3869ec7 812 kprintf("vesa_map_buffer: paddr:%x vaddr:%x size:%x off:%x\n",
984263bc
MD
813 paddr, vaddr, size, off);
814#endif
815 return (vaddr + off);
816}
817
818static void
819vesa_unmap_buffer(vm_offset_t vaddr, size_t size)
820{
821#if VESA_DEBUG > 1
e3869ec7 822 kprintf("vesa_unmap_buffer: vaddr:%x size:%x\n", vaddr, size);
984263bc 823#endif
e4846942 824 kmem_free(&kernel_map, vaddr, size);
984263bc
MD
825}
826
827/* entry points */
828
829static int
830vesa_configure(int flags)
831{
832 video_adapter_t *adp;
833 int adapters;
834 int error;
835 int i;
836
837 if (vesa_init_done)
838 return 0;
839 if (flags & VIO_PROBE_ONLY)
840 return 0; /* XXX */
841
842 /*
843 * If the VESA module has already been loaded, abort loading
844 * the module this time.
845 */
846 for (i = 0; (adp = vid_get_adapter(i)) != NULL; ++i) {
847 if (adp->va_flags & V_ADP_VESA)
848 return ENXIO;
849 if (adp->va_type == KD_VGA)
850 break;
851 }
852 /*
853 * The VGA adapter is not found. This is because either
854 * 1) the VGA driver has not been initialized, or 2) the VGA card
855 * is not present. If 1) is the case, we shall defer
856 * initialization for now and try again later.
857 */
858 if (adp == NULL) {
859 vga_sub_configure = vesa_configure;
860 return ENODEV;
861 }
862
863 /* count number of registered adapters */
864 for (++i; vid_get_adapter(i) != NULL; ++i)
865 ;
866 adapters = i;
867
868 /* call VESA BIOS */
869 vesa_adp = adp;
870 if (vesa_bios_init()) {
871 vesa_adp = NULL;
872 return ENXIO;
873 }
874 vesa_adp->va_flags |= V_ADP_VESA;
875
876 /* remove conflicting modes if we have more than one adapter */
877 if (adapters > 1) {
878 vesa_clear_modes(vesa_vmode,
879 (vesa_adp->va_flags & V_ADP_COLOR) ?
880 V_INFO_COLOR : 0);
881 }
882
883 if ((error = vesa_load_ioctl()) == 0) {
884 prevvidsw = vidsw[vesa_adp->va_index];
885 vidsw[vesa_adp->va_index] = &vesavidsw;
886 vesa_init_done = TRUE;
887 } else {
888 vesa_adp = NULL;
889 return error;
890 }
891
892 return 0;
893}
894
895#if 0
896static int
897vesa_nop(void)
898{
899 return 0;
900}
901#endif
902
903static int
904vesa_error(void)
905{
906 return 1;
907}
908
909static int
910vesa_probe(int unit, video_adapter_t **adpp, void *arg, int flags)
911{
912 return (*prevvidsw->probe)(unit, adpp, arg, flags);
913}
914
915static int
916vesa_init(int unit, video_adapter_t *adp, int flags)
917{
918 return (*prevvidsw->init)(unit, adp, flags);
919}
920
921static int
922vesa_get_info(video_adapter_t *adp, int mode, video_info_t *info)
923{
924 int i;
925
926 if ((*prevvidsw->get_info)(adp, mode, info) == 0)
927 return 0;
928
929 if (adp != vesa_adp)
930 return 1;
931
984263bc
MD
932 for (i = 0; vesa_vmode[i].vi_mode != EOT; ++i) {
933 if (vesa_vmode[i].vi_mode == NA)
934 continue;
935 if (vesa_vmode[i].vi_mode == mode) {
936 *info = vesa_vmode[i];
937 return 0;
938 }
939 }
940 return 1;
941}
942
943static int
944vesa_query_mode(video_adapter_t *adp, video_info_t *info)
945{
946 int i;
947
948 if ((*prevvidsw->query_mode)(adp, info) == 0)
949 return 0;
950 if (adp != vesa_adp)
951 return ENODEV;
952
953 for (i = 0; vesa_vmode[i].vi_mode != EOT; ++i) {
954 if ((info->vi_width != 0)
955 && (info->vi_width != vesa_vmode[i].vi_width))
956 continue;
957 if ((info->vi_height != 0)
958 && (info->vi_height != vesa_vmode[i].vi_height))
959 continue;
960 if ((info->vi_cwidth != 0)
961 && (info->vi_cwidth != vesa_vmode[i].vi_cwidth))
962 continue;
963 if ((info->vi_cheight != 0)
964 && (info->vi_cheight != vesa_vmode[i].vi_cheight))
965 continue;
966 if ((info->vi_depth != 0)
967 && (info->vi_depth != vesa_vmode[i].vi_depth))
968 continue;
969 if ((info->vi_planes != 0)
970 && (info->vi_planes != vesa_vmode[i].vi_planes))
971 continue;
972 /* pixel format, memory model */
973 if ((info->vi_flags != 0)
974 && (info->vi_flags != vesa_vmode[i].vi_flags))
975 continue;
976 *info = vesa_vmode[i];
977 return 0;
978 }
979 return ENODEV;
980}
981
982static int
983vesa_set_mode(video_adapter_t *adp, int mode)
984{
985 video_info_t info;
986 int len;
987
988 if (adp != vesa_adp)
989 return (*prevvidsw->set_mode)(adp, mode);
990
984263bc 991#if VESA_DEBUG > 0
e3869ec7 992 kprintf("VESA: set_mode(): %d(%x) -> %d(%x)\n",
984263bc
MD
993 adp->va_mode, adp->va_mode, mode, mode);
994#endif
995 /*
996 * If the current mode is a VESA mode and the new mode is not,
997 * restore the state of the adapter first by setting one of the
998 * standard VGA mode, so that non-standard, extended SVGA registers
999 * are set to the state compatible with the standard VGA modes.
1000 * Otherwise (*prevvidsw->set_mode)() may not be able to set up
1001 * the new mode correctly.
1002 */
1003 if (VESA_MODE(adp->va_mode)) {
1004 if ((*prevvidsw->get_info)(adp, mode, &info) == 0) {
1005 int10_set_mode(adp->va_initial_bios_mode);
1006 if (adp->va_info.vi_flags & V_INFO_LINEAR)
1007 vesa_unmap_buffer(adp->va_buffer,
1008 vesa_adp_info->v_memsize*64*1024);
1009 /*
1010 * Once (*prevvidsw->get_info)() succeeded,
1011 * (*prevvidsw->set_mode)() below won't fail...
1012 */
1013 }
1014 }
1015
1016 /* we may not need to handle this mode after all... */
1017 if ((*prevvidsw->set_mode)(adp, mode) == 0)
1018 return 0;
1019
1020 /* is the new mode supported? */
1021 if (vesa_get_info(adp, mode, &info))
1022 return 1;
1023 /* assert(VESA_MODE(mode)); */
1024
1025#if VESA_DEBUG > 0
e3869ec7 1026 kprintf("VESA: about to set a VESA mode...\n");
984263bc
MD
1027#endif
1028 /* don't use the linear frame buffer for text modes. XXX */
1029 if (!(info.vi_flags & V_INFO_GRAPHICS))
1030 info.vi_flags &= ~V_INFO_LINEAR;
1031
1032 if (vesa_bios_set_mode(mode | ((info.vi_flags & V_INFO_LINEAR) ? 0x4000 : 0)))
1033 return 1;
1034
1035 if (adp->va_info.vi_flags & V_INFO_LINEAR)
1036 vesa_unmap_buffer(adp->va_buffer,
1037 vesa_adp_info->v_memsize*64*1024);
1038
1039#if VESA_DEBUG > 0
e3869ec7 1040 kprintf("VESA: mode set!\n");
984263bc
MD
1041#endif
1042 vesa_adp->va_mode = mode;
1043 vesa_adp->va_flags &= ~V_ADP_COLOR;
1044 vesa_adp->va_flags |=
1045 (info.vi_flags & V_INFO_COLOR) ? V_ADP_COLOR : 0;
1046 vesa_adp->va_crtc_addr =
1047 (vesa_adp->va_flags & V_ADP_COLOR) ? COLOR_CRTC : MONO_CRTC;
1048 if (info.vi_flags & V_INFO_LINEAR) {
1049#if VESA_DEBUG > 1
e3869ec7 1050 kprintf("VESA: setting up LFB\n");
984263bc
MD
1051#endif
1052 vesa_adp->va_buffer =
1053 vesa_map_buffer(info.vi_buffer,
1054 vesa_adp_info->v_memsize*64*1024);
1055 vesa_adp->va_buffer_size = info.vi_buffer_size;
1056 vesa_adp->va_window = vesa_adp->va_buffer;
1057 vesa_adp->va_window_size = info.vi_buffer_size/info.vi_planes;
1058 vesa_adp->va_window_gran = info.vi_buffer_size/info.vi_planes;
1059 } else {
1060 vesa_adp->va_buffer = 0;
1061 vesa_adp->va_buffer_size = info.vi_buffer_size;
1062 vesa_adp->va_window = BIOS_PADDRTOVADDR(info.vi_window);
1063 vesa_adp->va_window_size = info.vi_window_size;
1064 vesa_adp->va_window_gran = info.vi_window_gran;
1065 }
1066 vesa_adp->va_window_orig = 0;
1067 len = vesa_bios_get_line_length();
1068 if (len > 0) {
1069 vesa_adp->va_line_width = len;
1070 } else if (info.vi_flags & V_INFO_GRAPHICS) {
1071 switch (info.vi_depth/info.vi_planes) {
1072 case 1:
1073 vesa_adp->va_line_width = info.vi_width/8;
1074 break;
1075 case 2:
1076 vesa_adp->va_line_width = info.vi_width/4;
1077 break;
1078 case 4:
1079 vesa_adp->va_line_width = info.vi_width/2;
1080 break;
1081 case 8:
1082 default: /* shouldn't happen */
1083 vesa_adp->va_line_width = info.vi_width;
1084 break;
1085 }
1086 } else {
1087 vesa_adp->va_line_width = info.vi_width;
1088 }
1089 vesa_adp->va_disp_start.x = 0;
1090 vesa_adp->va_disp_start.y = 0;
1091#if VESA_DEBUG > 0
e3869ec7 1092 kprintf("vesa_set_mode(): vi_width:%d, len:%d, line_width:%d\n",
984263bc
MD
1093 info.vi_width, len, vesa_adp->va_line_width);
1094#endif
1095 bcopy(&info, &vesa_adp->va_info, sizeof(vesa_adp->va_info));
1096
1097 /* move hardware cursor out of the way */
1098 (*vidsw[vesa_adp->va_index]->set_hw_cursor)(vesa_adp, -1, -1);
1099
1100 return 0;
1101}
1102
1103static int
1104vesa_save_font(video_adapter_t *adp, int page, int fontsize, u_char *data,
1105 int ch, int count)
1106{
1107 return (*prevvidsw->save_font)(adp, page, fontsize, data, ch, count);
1108}
1109
1110static int
1111vesa_load_font(video_adapter_t *adp, int page, int fontsize, u_char *data,
1112 int ch, int count)
1113{
1114 return (*prevvidsw->load_font)(adp, page, fontsize, data, ch, count);
1115}
1116
1117static int
1118vesa_show_font(video_adapter_t *adp, int page)
1119{
1120 return (*prevvidsw->show_font)(adp, page);
1121}
1122
1123static int
1124vesa_save_palette(video_adapter_t *adp, u_char *palette)
1125{
1126 int bits;
1127 int error;
1128
1129 if ((adp == vesa_adp) && (vesa_adp_info->v_flags & V_DAC8)
1130 && VESA_MODE(adp->va_mode)) {
1131 bits = vesa_bios_get_dac();
1132 error = vesa_bios_save_palette(0, 256, palette, bits);
1133 if (error == 0)
1134 return 0;
1135 if (bits != 6)
1136 return error;
1137 }
1138
1139 return (*prevvidsw->save_palette)(adp, palette);
1140}
1141
1142static int
736a05b5 1143vesa_load_palette(video_adapter_t *adp, const u_char *palette)
984263bc
MD
1144{
1145#if notyet
1146 int bits;
1147 int error;
1148
1149 if ((adp == vesa_adp) && (vesa_adp_info->v_flags & V_DAC8)
1150 && VESA_MODE(adp->va_mode) && ((bits = vesa_bios_set_dac(8)) > 6)) {
1151 error = vesa_bios_load_palette(0, 256, palette, bits);
1152 if (error == 0)
1153 return 0;
1154 if (vesa_bios_set_dac(6) != 6)
1155 return 1;
1156 }
1157#endif /* notyet */
1158
1159 return (*prevvidsw->load_palette)(adp, palette);
1160}
1161
1162static int
1163vesa_set_border(video_adapter_t *adp, int color)
1164{
1165 return (*prevvidsw->set_border)(adp, color);
1166}
1167
1168static int
1169vesa_save_state(video_adapter_t *adp, void *p, size_t size)
1170{
1171 if (adp != vesa_adp)
1172 return (*prevvidsw->save_state)(adp, p, size);
1173
1174 if (vesa_state_buf_size == 0)
1175 vesa_state_buf_size = vesa_bios_state_buf_size();
1176 if (size == 0)
1177 return (sizeof(int) + vesa_state_buf_size);
1178 else if (size < (sizeof(int) + vesa_state_buf_size))
1179 return 1;
1180
1181 ((adp_state_t *)p)->sig = V_STATE_SIG;
1182 bzero(((adp_state_t *)p)->regs, vesa_state_buf_size);
1183 return vesa_bios_save_restore(STATE_SAVE, ((adp_state_t *)p)->regs,
1184 vesa_state_buf_size);
1185}
1186
1187static int
1188vesa_load_state(video_adapter_t *adp, void *p)
1189{
1190 if ((adp != vesa_adp) || (((adp_state_t *)p)->sig != V_STATE_SIG))
1191 return (*prevvidsw->load_state)(adp, p);
1192
1193 return vesa_bios_save_restore(STATE_LOAD, ((adp_state_t *)p)->regs,
1194 vesa_state_buf_size);
1195}
1196
1197#if 0
1198static int
1199vesa_get_origin(video_adapter_t *adp, off_t *offset)
1200{
1201 struct vm86frame vmf;
1202 int err;
1203
1204 bzero(&vmf, sizeof(vmf));
1205 vmf.vmf_eax = 0x4f05;
1206 vmf.vmf_ebx = 0x10; /* WINDOW_A, XXX */
1207 err = vm86_intcall(0x10, &vmf);
1208 if ((err != 0) || (vmf.vmf_ax != 0x4f))
1209 return 1;
1210 *offset = vmf.vmf_dx*adp->va_window_gran;
1211 return 0;
1212}
1213#endif
1214
1215static int
1216vesa_set_origin(video_adapter_t *adp, off_t offset)
1217{
1218 struct vm86frame vmf;
1219 int err;
1220
1221 /*
1222 * This function should return as quickly as possible to
1223 * maintain good performance of the system. For this reason,
1224 * error checking is kept minimal and let the VESA BIOS to
1225 * detect error.
1226 */
1227 if (adp != vesa_adp)
1228 return (*prevvidsw->set_win_org)(adp, offset);
1229
1230 /* if this is a linear frame buffer, do nothing */
1231 if (adp->va_info.vi_flags & V_INFO_LINEAR)
1232 return 0;
1233 /* XXX */
1234 if (adp->va_window_gran == 0)
1235 return 1;
1236
1237 bzero(&vmf, sizeof(vmf));
1238 vmf.vmf_eax = 0x4f05;
1239 vmf.vmf_ebx = 0; /* WINDOW_A, XXX */
1240 vmf.vmf_edx = offset/adp->va_window_gran;
1241 err = vm86_intcall(0x10, &vmf);
1242 if ((err != 0) || (vmf.vmf_ax != 0x4f))
1243 return 1;
1244 bzero(&vmf, sizeof(vmf));
1245 vmf.vmf_eax = 0x4f05;
1246 vmf.vmf_ebx = 1; /* WINDOW_B, XXX */
1247 vmf.vmf_edx = offset/adp->va_window_gran;
1248 err = vm86_intcall(0x10, &vmf);
1249 adp->va_window_orig = (offset/adp->va_window_gran)*adp->va_window_gran;
1250 return 0; /* XXX */
1251}
1252
1253static int
1254vesa_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
1255{
1256 return (*prevvidsw->read_hw_cursor)(adp, col, row);
1257}
1258
1259static int
1260vesa_set_hw_cursor(video_adapter_t *adp, int col, int row)
1261{
1262 return (*prevvidsw->set_hw_cursor)(adp, col, row);
1263}
1264
1265static int
1266vesa_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
1267 int celsize, int blink)
1268{
1269 return (*prevvidsw->set_hw_cursor_shape)(adp, base, height, celsize,
1270 blink);
1271}
1272
1273static int
1274vesa_blank_display(video_adapter_t *adp, int mode)
1275{
1276 /* XXX: use VESA DPMS */
1277 return (*prevvidsw->blank_display)(adp, mode);
1278}
1279
1280static int
1281vesa_mmap(video_adapter_t *adp, vm_offset_t offset, int prot)
1282{
1283#if VESA_DEBUG > 0
e3869ec7 1284 kprintf("vesa_mmap(): window:0x%x, buffer:0x%x, offset:0x%x\n",
984263bc
MD
1285 adp->va_info.vi_window, adp->va_info.vi_buffer, offset);
1286#endif
1287
1288 if ((adp == vesa_adp) && (adp->va_info.vi_flags & V_INFO_LINEAR)) {
1289 /* va_window_size == va_buffer_size/vi_planes */
1290 /* XXX: is this correct? */
1291 if (offset > adp->va_window_size - PAGE_SIZE)
1292 return -1;
1293#ifdef __i386__
1294 return i386_btop(adp->va_info.vi_buffer + offset);
984263bc
MD
1295#endif
1296 } else {
1297 return (*prevvidsw->mmap)(adp, offset, prot);
1298 }
1299}
1300
1301static int
1302vesa_clear(video_adapter_t *adp)
1303{
1304 return (*prevvidsw->clear)(adp);
1305}
1306
1307static int
1308vesa_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
1309{
1310 return (*prevvidsw->fill_rect)(adp, val, x, y, cx, cy);
1311}
1312
1313static int
1314vesa_bitblt(video_adapter_t *adp,...)
1315{
1316 /* FIXME */
1317 return 1;
1318}
1319
1320static int
1321get_palette(video_adapter_t *adp, int base, int count,
1322 u_char *red, u_char *green, u_char *blue, u_char *trans)
1323{
1324 u_char *r;
1325 u_char *g;
1326 u_char *b;
1327 int bits;
1328 int error;
1329
1330 if ((base < 0) || (base >= 256) || (count < 0) || (count > 256))
1331 return 1;
5d556382 1332 if ((base + count) > 256)
984263bc
MD
1333 return 1;
1334 if (!(vesa_adp_info->v_flags & V_DAC8) || !VESA_MODE(adp->va_mode))
1335 return 1;
1336
1337 bits = vesa_bios_get_dac();
1338 if (bits <= 6)
1339 return 1;
1340
efda3bd0 1341 r = kmalloc(count*3, M_DEVBUF, M_WAITOK);
984263bc
MD
1342 g = r + count;
1343 b = g + count;
1344 error = vesa_bios_save_palette2(base, count, r, g, b, bits);
1345 if (error == 0) {
1346 copyout(r, red, count);
1347 copyout(g, green, count);
1348 copyout(b, blue, count);
1349 if (trans != NULL) {
1350 bzero(r, count);
1351 copyout(r, trans, count);
1352 }
1353 }
efda3bd0 1354 kfree(r, M_DEVBUF);
984263bc
MD
1355
1356 /* if error && bits != 6 at this point, we are in in trouble... XXX */
1357 return error;
1358}
1359
1360static int
1361set_palette(video_adapter_t *adp, int base, int count,
1362 u_char *red, u_char *green, u_char *blue, u_char *trans)
1363{
1364 return 1;
1365#if notyet
1366 u_char *r;
1367 u_char *g;
1368 u_char *b;
1369 int bits;
1370 int error;
1371
1372 if ((base < 0) || (base >= 256) || (base + count > 256))
1373 return 1;
1374 if (!(vesa_adp_info->v_flags & V_DAC8) || !VESA_MODE(adp->va_mode)
1375 || ((bits = vesa_bios_set_dac(8)) <= 6))
1376 return 1;
1377
efda3bd0 1378 r = kmalloc(count*3, M_DEVBUF, M_WAITOK);
984263bc
MD
1379 g = r + count;
1380 b = g + count;
1381 copyin(red, r, count);
1382 copyin(green, g, count);
1383 copyin(blue, b, count);
1384
1385 error = vesa_bios_load_palette2(base, count, r, g, b, bits);
efda3bd0 1386 kfree(r, M_DEVBUF);
984263bc
MD
1387 if (error == 0)
1388 return 0;
1389
1390 /* if the following call fails, we are in trouble... XXX */
1391 vesa_bios_set_dac(6);
1392 return 1;
1393#endif /* notyet */
1394}
1395
1396static int
1397vesa_ioctl(video_adapter_t *adp, u_long cmd, caddr_t arg)
1398{
1399 int bytes;
1400
1401 if (adp != vesa_adp)
1402 return (*prevvidsw->ioctl)(adp, cmd, arg);
1403
1404 switch (cmd) {
1405 case FBIO_SETWINORG: /* set frame buffer window origin */
1406 if (!VESA_MODE(adp->va_mode))
1407 return (*prevvidsw->ioctl)(adp, cmd, arg);
1408 return (vesa_set_origin(adp, *(off_t *)arg) ? ENODEV : 0);
1409
1410 case FBIO_SETDISPSTART: /* set display start address */
1411 if (!VESA_MODE(adp->va_mode))
1412 return (*prevvidsw->ioctl)(adp, cmd, arg);
1413 if (vesa_bios_set_start(((video_display_start_t *)arg)->x,
1414 ((video_display_start_t *)arg)->y))
1415 return ENODEV;
1416 adp->va_disp_start.x = ((video_display_start_t *)arg)->x;
1417 adp->va_disp_start.y = ((video_display_start_t *)arg)->y;
1418 return 0;
1419
1420 case FBIO_SETLINEWIDTH: /* set line length in pixel */
1421 if (!VESA_MODE(adp->va_mode))
1422 return (*prevvidsw->ioctl)(adp, cmd, arg);
1423 if (vesa_bios_set_line_length(*(u_int *)arg, &bytes, NULL))
1424 return ENODEV;
1425 adp->va_line_width = bytes;
1426#if VESA_DEBUG > 1
e3869ec7 1427 kprintf("new line width:%d\n", adp->va_line_width);
984263bc
MD
1428#endif
1429 return 0;
1430
1431 case FBIO_GETPALETTE: /* get color palette */
1432 if (get_palette(adp, ((video_color_palette_t *)arg)->index,
1433 ((video_color_palette_t *)arg)->count,
1434 ((video_color_palette_t *)arg)->red,
1435 ((video_color_palette_t *)arg)->green,
1436 ((video_color_palette_t *)arg)->blue,
1437 ((video_color_palette_t *)arg)->transparent))
1438 return (*prevvidsw->ioctl)(adp, cmd, arg);
1439 return 0;
1440
1441
1442 case FBIO_SETPALETTE: /* set color palette */
1443 if (set_palette(adp, ((video_color_palette_t *)arg)->index,
1444 ((video_color_palette_t *)arg)->count,
1445 ((video_color_palette_t *)arg)->red,
1446 ((video_color_palette_t *)arg)->green,
1447 ((video_color_palette_t *)arg)->blue,
1448 ((video_color_palette_t *)arg)->transparent))
1449 return (*prevvidsw->ioctl)(adp, cmd, arg);
1450 return 0;
1451
1452 case FBIOGETCMAP: /* get color palette */
1453 if (get_palette(adp, ((struct fbcmap *)arg)->index,
1454 ((struct fbcmap *)arg)->count,
1455 ((struct fbcmap *)arg)->red,
1456 ((struct fbcmap *)arg)->green,
1457 ((struct fbcmap *)arg)->blue, NULL))
1458 return (*prevvidsw->ioctl)(adp, cmd, arg);
1459 return 0;
1460
1461 case FBIOPUTCMAP: /* set color palette */
1462 if (set_palette(adp, ((struct fbcmap *)arg)->index,
1463 ((struct fbcmap *)arg)->count,
1464 ((struct fbcmap *)arg)->red,
1465 ((struct fbcmap *)arg)->green,
1466 ((struct fbcmap *)arg)->blue, NULL))
1467 return (*prevvidsw->ioctl)(adp, cmd, arg);
1468 return 0;
1469
1470 default:
1471 return (*prevvidsw->ioctl)(adp, cmd, arg);
1472 }
1473}
1474
1475static int
1476vesa_diag(video_adapter_t *adp, int level)
1477{
1478 int error;
1479
1480 /* call the previous handler first */
1481 error = (*prevvidsw->diag)(adp, level);
1482 if (error)
1483 return error;
1484
1485 if (adp != vesa_adp)
1486 return 1;
1487
1488 if (level <= 0)
1489 return 0;
1490
1491 return 0;
1492}
1493
1494static int
1495vesa_bios_info(int level)
1496{
1497#if VESA_DEBUG > 1
1498 struct vesa_mode vmode;
1499 int i;
1500#endif
1501
5d556382
MD
1502 if (bootverbose) {
1503 /* general adapter information */
e3869ec7 1504 kprintf("VESA: v%d.%d, %dk memory, flags:0x%x, mode table:%p (%x)\n",
5d556382
MD
1505 ((vesa_adp_info->v_version & 0xf000) >> 12) * 10 +
1506 ((vesa_adp_info->v_version & 0x0f00) >> 8),
1507 ((vesa_adp_info->v_version & 0x00f0) >> 4) * 10 +
1508 (vesa_adp_info->v_version & 0x000f),
1509 vesa_adp_info->v_memsize * 64, vesa_adp_info->v_flags,
1510 vesa_vmodetab, vesa_adp_info->v_modetable);
1511
1512 /* OEM string */
1513 if (vesa_oemstr != NULL)
e3869ec7 1514 kprintf("VESA: %s\n", vesa_oemstr);
5d556382 1515 }
984263bc
MD
1516
1517 if (level <= 0)
1518 return 0;
1519
5d556382 1520 if (vesa_adp_info->v_version >= 0x0200 && bootverbose) {
2e46b2a9 1521 /* vendor name, product name, product revision */
e3869ec7 1522 kprintf("VESA: %s %s %s\n",
2e46b2a9 1523 (vesa_vendorstr != NULL) ? vesa_vendorstr : "unknown",
984263bc
MD
1524 (vesa_prodstr != NULL) ? vesa_prodstr : "unknown",
1525 (vesa_revstr != NULL) ? vesa_revstr : "?");
1526 }
1527
1528#if VESA_DEBUG > 1
1529 /* mode information */
1530 for (i = 0;
1531 (i < (M_VESA_MODE_MAX - M_VESA_BASE + 1))
1532 && (vesa_vmodetab[i] != 0xffff); ++i) {
1533 if (vesa_bios_get_mode(vesa_vmodetab[i], &vmode))
1534 continue;
1535
1536 /* print something for diagnostic purpose */
e3869ec7 1537 kprintf("VESA: mode:0x%03x, flags:0x%04x",
984263bc
MD
1538 vesa_vmodetab[i], vmode.v_modeattr);
1539 if (vmode.v_modeattr & V_MODEOPTINFO) {
1540 if (vmode.v_modeattr & V_MODEGRAPHICS) {
e3869ec7 1541 kprintf(", G %dx%dx%d %d, ",
984263bc
MD
1542 vmode.v_width, vmode.v_height,
1543 vmode.v_bpp, vmode.v_planes);
1544 } else {
e3869ec7 1545 kprintf(", T %dx%d, ",
984263bc
MD
1546 vmode.v_width, vmode.v_height);
1547 }
e3869ec7 1548 kprintf("font:%dx%d, ",
984263bc 1549 vmode.v_cwidth, vmode.v_cheight);
e3869ec7 1550 kprintf("pages:%d, mem:%d",
984263bc
MD
1551 vmode.v_ipages + 1, vmode.v_memmodel);
1552 }
1553 if (vmode.v_modeattr & V_MODELFB) {
e3869ec7 1554 kprintf("\nVESA: LFB:0x%x, off:0x%x, off_size:0x%x",
984263bc
MD
1555 vmode.v_lfb, vmode.v_offscreen,
1556 vmode.v_offscreensize*1024);
1557 }
e3869ec7
SW
1558 kprintf("\n");
1559 kprintf("VESA: window A:0x%x (%x), window B:0x%x (%x), ",
984263bc
MD
1560 vmode.v_waseg, vmode.v_waattr,
1561 vmode.v_wbseg, vmode.v_wbattr);
e3869ec7 1562 kprintf("size:%dk, gran:%dk\n",
984263bc
MD
1563 vmode.v_wsize, vmode.v_wgran);
1564 }
1565#endif /* VESA_DEBUG > 1 */
1566
1567 return 0;
1568}
1569
1570/* module loading */
1571
1572static int
1573vesa_load(void)
1574{
1575 int error;
984263bc
MD
1576
1577 if (vesa_init_done)
1578 return 0;
1579
1580 /* locate a VGA adapter */
052b6ffa 1581 crit_enter();
984263bc
MD
1582 vesa_adp = NULL;
1583 error = vesa_configure(0);
052b6ffa 1584 crit_exit();
984263bc
MD
1585
1586 if (error == 0)
1587 vesa_bios_info(bootverbose);
1588
1589 return error;
1590}
1591
1592static int
1593vesa_unload(void)
1594{
1595 u_char palette[256*3];
1596 int error;
1597 int bits;
984263bc
MD
1598
1599 /* if the adapter is currently in a VESA mode, don't unload */
1600 if ((vesa_adp != NULL) && VESA_MODE(vesa_adp->va_mode))
1601 return EBUSY;
1602 /*
1603 * FIXME: if there is at least one vty which is in a VESA mode,
1604 * we shouldn't be unloading! XXX
1605 */
1606
052b6ffa 1607 crit_enter();
984263bc
MD
1608 if ((error = vesa_unload_ioctl()) == 0) {
1609 if (vesa_adp != NULL) {
1610 if (vesa_adp_info->v_flags & V_DAC8) {
1611 bits = vesa_bios_get_dac();
1612 if (bits > 6) {
1613 vesa_bios_save_palette(0, 256,
1614 palette, bits);
1615 vesa_bios_set_dac(6);
1616 vesa_bios_load_palette(0, 256,
1617 palette, 6);
1618 }
1619 }
1620 vesa_adp->va_flags &= ~V_ADP_VESA;
1621 vidsw[vesa_adp->va_index] = prevvidsw;
1622 }
1623 }
052b6ffa 1624 crit_exit();
984263bc 1625
b5f86e2f 1626 if (vesa_vm86_buf != NULL)
efda3bd0 1627 kfree(vesa_vm86_buf, M_DEVBUF);
b5f86e2f 1628
984263bc
MD
1629 return error;
1630}
1631
1632static int
1633vesa_mod_event(module_t mod, int type, void *data)
1634{
1635 switch (type) {
1636 case MOD_LOAD:
1637 return vesa_load();
1638 case MOD_UNLOAD:
1639 return vesa_unload();
1640 default:
1641 break;
1642 }
1643 return 0;
1644}
1645
1646static moduledata_t vesa_mod = {
1647 "vesa",
1648 vesa_mod_event,
1649 NULL,
1650};
1651
1652DECLARE_MODULE(vesa, vesa_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
1653
1654#endif /* VGA_NO_MODE_CHANGE */