2 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 * $FreeBSD: src/sys/dev/syscons/scvtb.c,v 1.5.2.1 2001/07/16 05:21:23 yokota Exp $
27 * $DragonFly: src/sys/dev/misc/syscons/scvtb.c,v 1.9 2006/09/05 03:48:10 dillon Exp $
30 #include "opt_syscons.h"
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <machine/console.h>
36 #include <machine/md_var.h>
38 #include <dev/video/fb/fbreg.h>
41 #define vtb_wrap(vtb, at, offset) \
42 (((at) + (offset) + (vtb)->vtb_size)%(vtb)->vtb_size)
45 sc_vtb_init(sc_vtb_t *vtb, int type, int cols, int rows, void *buf, int wait)
51 vtb->vtb_size = cols*rows;
52 vtb->vtb_buffer = NULL;
58 if ((buf == NULL) && (cols*rows != 0)) {
59 vtb->vtb_buffer = kmalloc(cols*rows*sizeof(uint16_t),
61 M_ZERO | ((wait) ? M_WAITOK : M_NOWAIT));
62 if (vtb->vtb_buffer != NULL) {
63 vtb->vtb_flags |= VTB_VALID;
64 vtb->vtb_flags |= VTB_ALLOCED;
67 vtb->vtb_buffer = buf;
68 vtb->vtb_flags |= VTB_VALID;
72 vtb->vtb_buffer = buf;
73 vtb->vtb_flags |= VTB_VALID;
81 sc_vtb_destroy(sc_vtb_t *vtb)
91 vtb->vtb_buffer = NULL;
92 switch (vtb->vtb_type) {
95 if ((vtb->vtb_flags & VTB_ALLOCED) && (p != NULL))
102 vtb->vtb_type = VTB_INVALID;
106 sc_vtb_size(int cols, int rows)
108 return (size_t)(cols*rows*sizeof(uint16_t));
112 sc_vtb_getc(sc_vtb_t *vtb, int at)
114 if (vtb->vtb_type == VTB_FRAMEBUFFER)
115 return (readw(vtb->vtb_buffer + at) & 0x00ff);
117 return (*(vtb->vtb_buffer + at) & 0x00ff);
121 sc_vtb_geta(sc_vtb_t *vtb, int at)
123 if (vtb->vtb_type == VTB_FRAMEBUFFER)
124 return (readw(vtb->vtb_buffer + at) & 0xff00);
126 return (*(vtb->vtb_buffer + at) & 0xff00);
130 sc_vtb_putc(sc_vtb_t *vtb, int at, int c, int a)
132 if (vtb->vtb_type == VTB_FRAMEBUFFER)
133 writew(vtb->vtb_buffer + at, a | c);
135 *(vtb->vtb_buffer + at) = a | c;
139 sc_vtb_putchar(sc_vtb_t *vtb, uint16_t *p, int c, int a)
141 if (vtb->vtb_type == VTB_FRAMEBUFFER)
149 sc_vtb_pos(sc_vtb_t *vtb, int pos, int offset)
151 return ((pos + offset + vtb->vtb_size)%vtb->vtb_size);
155 sc_vtb_clear(sc_vtb_t *vtb, int c, int attr)
157 if (vtb->vtb_type == VTB_FRAMEBUFFER)
158 fillw_io(attr | c, vtb->vtb_buffer, vtb->vtb_size);
160 fillw(attr | c, vtb->vtb_buffer, vtb->vtb_size);
164 sc_vtb_copy(sc_vtb_t *vtb1, int from, sc_vtb_t *vtb2, int to, int count)
166 /* XXX if both are VTB_VRAMEBUFFER... */
167 if (vtb2->vtb_type == VTB_FRAMEBUFFER) {
168 bcopy_toio(vtb1->vtb_buffer + from, vtb2->vtb_buffer + to,
169 count*sizeof(uint16_t));
170 } else if (vtb1->vtb_type == VTB_FRAMEBUFFER) {
171 bcopy_fromio(vtb1->vtb_buffer + from, vtb2->vtb_buffer + to,
172 count*sizeof(uint16_t));
174 bcopy(vtb1->vtb_buffer + from, vtb2->vtb_buffer + to,
175 count*sizeof(uint16_t));
180 sc_vtb_append(sc_vtb_t *vtb1, int from, sc_vtb_t *vtb2, int count)
184 if (vtb2->vtb_type != VTB_RINGBUFFER)
188 len = imin(count, vtb2->vtb_size - vtb2->vtb_tail);
189 if (vtb1->vtb_type == VTB_FRAMEBUFFER) {
190 bcopy_fromio(vtb1->vtb_buffer + from,
191 vtb2->vtb_buffer + vtb2->vtb_tail,
192 len*sizeof(uint16_t));
194 bcopy(vtb1->vtb_buffer + from,
195 vtb2->vtb_buffer + vtb2->vtb_tail,
196 len*sizeof(uint16_t));
200 vtb2->vtb_tail = vtb_wrap(vtb2, vtb2->vtb_tail, len);
205 sc_vtb_seek(sc_vtb_t *vtb, int pos)
207 vtb->vtb_tail = pos%vtb->vtb_size;
211 sc_vtb_erase(sc_vtb_t *vtb, int at, int count, int c, int attr)
213 if (at + count > vtb->vtb_size)
214 count = vtb->vtb_size - at;
215 if (vtb->vtb_type == VTB_FRAMEBUFFER)
216 fillw_io(attr | c, vtb->vtb_buffer + at, count);
218 fillw(attr | c, vtb->vtb_buffer + at, count);
222 sc_vtb_move(sc_vtb_t *vtb, int from, int to, int count)
224 if (from + count > vtb->vtb_size)
225 count = vtb->vtb_size - from;
226 if (to + count > vtb->vtb_size)
227 count = vtb->vtb_size - to;
230 if (vtb->vtb_type == VTB_FRAMEBUFFER) {
231 bcopy_io(vtb->vtb_buffer + from, vtb->vtb_buffer + to,
232 count*sizeof(uint16_t));
234 bcopy(vtb->vtb_buffer + from, vtb->vtb_buffer + to,
235 count*sizeof(uint16_t));
240 sc_vtb_delete(sc_vtb_t *vtb, int at, int count, int c, int attr)
244 if (at + count > vtb->vtb_size)
245 count = vtb->vtb_size - at;
246 len = vtb->vtb_size - at - count;
248 if (vtb->vtb_type == VTB_FRAMEBUFFER) {
249 bcopy_io(vtb->vtb_buffer + at + count,
250 vtb->vtb_buffer + at,
251 len*sizeof(uint16_t));
253 bcopy(vtb->vtb_buffer + at + count,
254 vtb->vtb_buffer + at,
255 len*sizeof(uint16_t));
258 if (vtb->vtb_type == VTB_FRAMEBUFFER)
259 fillw_io(attr | c, vtb->vtb_buffer + at + len,
260 vtb->vtb_size - at - len);
262 fillw(attr | c, vtb->vtb_buffer + at + len,
263 vtb->vtb_size - at - len);
267 sc_vtb_ins(sc_vtb_t *vtb, int at, int count, int c, int attr)
269 if (at + count > vtb->vtb_size) {
270 count = vtb->vtb_size - at;
272 if (vtb->vtb_type == VTB_FRAMEBUFFER) {
273 bcopy_io(vtb->vtb_buffer + at,
274 vtb->vtb_buffer + at + count,
275 (vtb->vtb_size - at - count)*sizeof(uint16_t));
277 bcopy(vtb->vtb_buffer + at,
278 vtb->vtb_buffer + at + count,
279 (vtb->vtb_size - at - count)*sizeof(uint16_t));
282 if (vtb->vtb_type == VTB_FRAMEBUFFER)
283 fillw_io(attr | c, vtb->vtb_buffer + at, count);
285 fillw(attr | c, vtb->vtb_buffer + at, count);