2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer as
12 * the first lines of this file unmodified.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include "opt_syscons.h"
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
39 #include <sys/consio.h>
41 #include <machine/md_var.h>
42 #include <machine/bus.h>
44 #include <dev/fb/fbreg.h>
45 #include <dev/syscons/syscons.h>
47 #define vtb_wrap(vtb, at, offset) \
48 (((at) + (offset) + (vtb)->vtb_size)%(vtb)->vtb_size)
51 sc_vtb_init(sc_vtb_t *vtb, int type, int cols, int rows, void *buf, int wait)
57 vtb->vtb_size = cols*rows;
64 if ((buf == NULL) && (cols*rows != 0)) {
66 (vm_offset_t)malloc(cols*rows*sizeof(u_int16_t),
68 (wait) ? M_WAITOK : M_NOWAIT);
69 if (vtb->vtb_buffer != 0) {
70 bzero((void *)sc_vtb_pointer(vtb, 0),
71 cols*rows*sizeof(u_int16_t));
72 vtb->vtb_flags |= VTB_ALLOCED;
75 vtb->vtb_buffer = (vm_offset_t)buf;
77 vtb->vtb_flags |= VTB_VALID;
81 vtb->vtb_buffer = (vm_offset_t)buf;
82 vtb->vtb_flags |= VTB_VALID;
91 sc_vtb_destroy(sc_vtb_t *vtb)
102 switch (vtb->vtb_type) {
105 if ((vtb->vtb_flags & VTB_ALLOCED) && (p != 0))
106 free((void *)p, M_DEVBUF);
112 vtb->vtb_type = VTB_INVALID;
116 sc_vtb_size(int cols, int rows)
118 return (size_t)(cols*rows*sizeof(u_int16_t));
122 sc_vtb_getc(sc_vtb_t *vtb, int at)
125 if (vtb->vtb_type == VTB_FRAMEBUFFER)
126 return (readw(sc_vtb_pointer(vtb, at)) & 0x00ff);
129 return (*(u_int16_t *)sc_vtb_pointer(vtb, at) & 0x00ff);
133 sc_vtb_geta(sc_vtb_t *vtb, int at)
136 if (vtb->vtb_type == VTB_FRAMEBUFFER)
137 return (readw(sc_vtb_pointer(vtb, at)) & 0xff00);
140 return (*(u_int16_t *)sc_vtb_pointer(vtb, at) & 0xff00);
144 sc_vtb_putc(sc_vtb_t *vtb, int at, int c, int a)
147 if (vtb->vtb_type == VTB_FRAMEBUFFER)
148 writew(sc_vtb_pointer(vtb, at), a | c);
151 *(u_int16_t *)sc_vtb_pointer(vtb, at) = a | c;
155 sc_vtb_putchar(sc_vtb_t *vtb, vm_offset_t p, int c, int a)
158 if (vtb->vtb_type == VTB_FRAMEBUFFER)
162 *(u_int16_t *)p = a | c;
163 return (p + sizeof(u_int16_t));
167 sc_vtb_pointer(sc_vtb_t *vtb, int at)
169 return (vtb->vtb_buffer + sizeof(u_int16_t)*(at));
173 sc_vtb_pos(sc_vtb_t *vtb, int pos, int offset)
175 return ((pos + offset + vtb->vtb_size)%vtb->vtb_size);
179 sc_vtb_clear(sc_vtb_t *vtb, int c, int attr)
182 if (vtb->vtb_type == VTB_FRAMEBUFFER)
183 fillw_io(attr | c, sc_vtb_pointer(vtb, 0), vtb->vtb_size);
186 fillw(attr | c, (void *)sc_vtb_pointer(vtb, 0), vtb->vtb_size);
190 sc_vtb_copy(sc_vtb_t *vtb1, int from, sc_vtb_t *vtb2, int to, int count)
193 /* XXX if both are VTB_VRAMEBUFFER... */
194 if (vtb2->vtb_type == VTB_FRAMEBUFFER)
195 bcopy_toio(sc_vtb_pointer(vtb1, from),
196 sc_vtb_pointer(vtb2, to),
197 count*sizeof(u_int16_t));
198 else if (vtb1->vtb_type == VTB_FRAMEBUFFER)
199 bcopy_fromio(sc_vtb_pointer(vtb1, from),
200 sc_vtb_pointer(vtb2, to),
201 count*sizeof(u_int16_t));
204 bcopy((void *)sc_vtb_pointer(vtb1, from),
205 (void *)sc_vtb_pointer(vtb2, to),
206 count*sizeof(u_int16_t));
210 sc_vtb_append(sc_vtb_t *vtb1, int from, sc_vtb_t *vtb2, int count)
214 if (vtb2->vtb_type != VTB_RINGBUFFER)
218 len = imin(count, vtb2->vtb_size - vtb2->vtb_tail);
220 if (vtb1->vtb_type == VTB_FRAMEBUFFER)
221 bcopy_fromio(sc_vtb_pointer(vtb1, from),
222 sc_vtb_pointer(vtb2, vtb2->vtb_tail),
223 len*sizeof(u_int16_t));
226 bcopy((void *)sc_vtb_pointer(vtb1, from),
227 (void *)sc_vtb_pointer(vtb2, vtb2->vtb_tail),
228 len*sizeof(u_int16_t));
231 vtb2->vtb_tail = vtb_wrap(vtb2, vtb2->vtb_tail, len);
236 sc_vtb_seek(sc_vtb_t *vtb, int pos)
238 vtb->vtb_tail = pos%vtb->vtb_size;
242 sc_vtb_erase(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;
247 if (vtb->vtb_type == VTB_FRAMEBUFFER)
248 fillw_io(attr | c, sc_vtb_pointer(vtb, at), count);
251 fillw(attr | c, (void *)sc_vtb_pointer(vtb, at), count);
255 sc_vtb_move(sc_vtb_t *vtb, int from, int to, int count)
257 if (from + count > vtb->vtb_size)
258 count = vtb->vtb_size - from;
259 if (to + count > vtb->vtb_size)
260 count = vtb->vtb_size - to;
264 if (vtb->vtb_type == VTB_FRAMEBUFFER)
265 bcopy_io(sc_vtb_pointer(vtb, from),
266 sc_vtb_pointer(vtb, to), count*sizeof(u_int16_t));
269 bcopy((void *)sc_vtb_pointer(vtb, from),
270 (void *)sc_vtb_pointer(vtb, to), count*sizeof(u_int16_t));
274 sc_vtb_delete(sc_vtb_t *vtb, int at, int count, int c, int attr)
278 if (at + count > vtb->vtb_size)
279 count = vtb->vtb_size - at;
280 len = vtb->vtb_size - at - count;
283 if (vtb->vtb_type == VTB_FRAMEBUFFER)
284 bcopy_io(sc_vtb_pointer(vtb, at + count),
285 sc_vtb_pointer(vtb, at),
286 len*sizeof(u_int16_t));
289 bcopy((void *)sc_vtb_pointer(vtb, at + count),
290 (void *)sc_vtb_pointer(vtb, at),
291 len*sizeof(u_int16_t));
294 if (vtb->vtb_type == VTB_FRAMEBUFFER)
295 fillw_io(attr | c, sc_vtb_pointer(vtb, at + len),
296 vtb->vtb_size - at - len);
299 fillw(attr | c, (void *)sc_vtb_pointer(vtb, at + len),
300 vtb->vtb_size - at - len);
304 sc_vtb_ins(sc_vtb_t *vtb, int at, int count, int c, int attr)
306 if (at + count > vtb->vtb_size)
307 count = vtb->vtb_size - at;
310 if (vtb->vtb_type == VTB_FRAMEBUFFER)
311 bcopy_io(sc_vtb_pointer(vtb, at),
312 sc_vtb_pointer(vtb, at + count),
313 (vtb->vtb_size - at - count)*sizeof(u_int16_t));
316 bcopy((void *)sc_vtb_pointer(vtb, at),
317 (void *)sc_vtb_pointer(vtb, at + count),
318 (vtb->vtb_size - at - count)*sizeof(u_int16_t));
321 if (vtb->vtb_type == VTB_FRAMEBUFFER)
322 fillw_io(attr | c, sc_vtb_pointer(vtb, at), count);
325 fillw(attr | c, (void *)sc_vtb_pointer(vtb, at), count);