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