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;
80 vtb->vtb_buffer = (vm_offset_t)buf;
81 vtb->vtb_flags |= VTB_VALID;
89 sc_vtb_destroy(sc_vtb_t *vtb)
100 switch (vtb->vtb_type) {
103 if ((vtb->vtb_flags & VTB_ALLOCED) && (p != 0))
104 free((void *)p, M_DEVBUF);
110 vtb->vtb_type = VTB_INVALID;
114 sc_vtb_size(int cols, int rows)
116 return (size_t)(cols*rows*sizeof(u_int16_t));
120 sc_vtb_getc(sc_vtb_t *vtb, int at)
122 if (vtb->vtb_type == VTB_FRAMEBUFFER)
123 return (readw(sc_vtb_pointer(vtb, at)) & 0x00ff);
125 return (*(u_int16_t *)sc_vtb_pointer(vtb, at) & 0x00ff);
129 sc_vtb_geta(sc_vtb_t *vtb, int at)
131 if (vtb->vtb_type == VTB_FRAMEBUFFER)
132 return (readw(sc_vtb_pointer(vtb, at)) & 0xff00);
134 return (*(u_int16_t *)sc_vtb_pointer(vtb, at) & 0xff00);
138 sc_vtb_putc(sc_vtb_t *vtb, int at, int c, int a)
140 if (vtb->vtb_type == VTB_FRAMEBUFFER)
141 writew(sc_vtb_pointer(vtb, at), a | c);
143 *(u_int16_t *)sc_vtb_pointer(vtb, at) = a | c;
147 sc_vtb_putchar(sc_vtb_t *vtb, vm_offset_t p, int c, int a)
149 if (vtb->vtb_type == VTB_FRAMEBUFFER)
152 *(u_int16_t *)p = a | c;
153 return (p + sizeof(u_int16_t));
157 sc_vtb_pointer(sc_vtb_t *vtb, int at)
159 return (vtb->vtb_buffer + sizeof(u_int16_t)*(at));
163 sc_vtb_pos(sc_vtb_t *vtb, int pos, int offset)
165 return ((pos + offset + vtb->vtb_size)%vtb->vtb_size);
169 sc_vtb_clear(sc_vtb_t *vtb, int c, int attr)
171 if (vtb->vtb_type == VTB_FRAMEBUFFER)
172 fillw_io(attr | c, sc_vtb_pointer(vtb, 0), vtb->vtb_size);
174 fillw(attr | c, (void *)sc_vtb_pointer(vtb, 0), vtb->vtb_size);
178 sc_vtb_copy(sc_vtb_t *vtb1, int from, sc_vtb_t *vtb2, int to, int count)
180 /* XXX if both are VTB_VRAMEBUFFER... */
181 if (vtb2->vtb_type == VTB_FRAMEBUFFER)
182 bcopy_toio(sc_vtb_pointer(vtb1, from),
183 sc_vtb_pointer(vtb2, to),
184 count*sizeof(u_int16_t));
185 else if (vtb1->vtb_type == VTB_FRAMEBUFFER)
186 bcopy_fromio(sc_vtb_pointer(vtb1, from),
187 sc_vtb_pointer(vtb2, to),
188 count*sizeof(u_int16_t));
190 bcopy((void *)sc_vtb_pointer(vtb1, from),
191 (void *)sc_vtb_pointer(vtb2, to),
192 count*sizeof(u_int16_t));
196 sc_vtb_append(sc_vtb_t *vtb1, int from, sc_vtb_t *vtb2, int count)
200 if (vtb2->vtb_type != VTB_RINGBUFFER)
204 len = imin(count, vtb2->vtb_size - vtb2->vtb_tail);
205 if (vtb1->vtb_type == VTB_FRAMEBUFFER)
206 bcopy_fromio(sc_vtb_pointer(vtb1, from),
207 sc_vtb_pointer(vtb2, vtb2->vtb_tail),
208 len*sizeof(u_int16_t));
210 bcopy((void *)sc_vtb_pointer(vtb1, from),
211 (void *)sc_vtb_pointer(vtb2, vtb2->vtb_tail),
212 len*sizeof(u_int16_t));
215 vtb2->vtb_tail = vtb_wrap(vtb2, vtb2->vtb_tail, len);
220 sc_vtb_seek(sc_vtb_t *vtb, int pos)
222 vtb->vtb_tail = pos%vtb->vtb_size;
226 sc_vtb_erase(sc_vtb_t *vtb, int at, int count, int c, int attr)
228 if (at + count > vtb->vtb_size)
229 count = vtb->vtb_size - at;
230 if (vtb->vtb_type == VTB_FRAMEBUFFER)
231 fillw_io(attr | c, sc_vtb_pointer(vtb, at), count);
233 fillw(attr | c, (void *)sc_vtb_pointer(vtb, at), count);
237 sc_vtb_move(sc_vtb_t *vtb, int from, int to, int count)
239 if (from + count > vtb->vtb_size)
240 count = vtb->vtb_size - from;
241 if (to + count > vtb->vtb_size)
242 count = vtb->vtb_size - to;
245 if (vtb->vtb_type == VTB_FRAMEBUFFER)
246 bcopy_io(sc_vtb_pointer(vtb, from),
247 sc_vtb_pointer(vtb, to), count*sizeof(u_int16_t));
249 bcopy((void *)sc_vtb_pointer(vtb, from),
250 (void *)sc_vtb_pointer(vtb, to), count*sizeof(u_int16_t));
254 sc_vtb_delete(sc_vtb_t *vtb, int at, int count, int c, int attr)
258 if (at + count > vtb->vtb_size)
259 count = vtb->vtb_size - at;
260 len = vtb->vtb_size - at - count;
262 if (vtb->vtb_type == VTB_FRAMEBUFFER)
263 bcopy_io(sc_vtb_pointer(vtb, at + count),
264 sc_vtb_pointer(vtb, at),
265 len*sizeof(u_int16_t));
267 bcopy((void *)sc_vtb_pointer(vtb, at + count),
268 (void *)sc_vtb_pointer(vtb, at),
269 len*sizeof(u_int16_t));
271 if (vtb->vtb_type == VTB_FRAMEBUFFER)
272 fillw_io(attr | c, sc_vtb_pointer(vtb, at + len),
273 vtb->vtb_size - at - len);
275 fillw(attr | c, (void *)sc_vtb_pointer(vtb, at + len),
276 vtb->vtb_size - at - len);
280 sc_vtb_ins(sc_vtb_t *vtb, int at, int count, int c, int attr)
282 if (at + count > vtb->vtb_size)
283 count = vtb->vtb_size - at;
285 if (vtb->vtb_type == VTB_FRAMEBUFFER)
286 bcopy_io(sc_vtb_pointer(vtb, at),
287 sc_vtb_pointer(vtb, at + count),
288 (vtb->vtb_size - at - count)*sizeof(u_int16_t));
290 bcopy((void *)sc_vtb_pointer(vtb, at),
291 (void *)sc_vtb_pointer(vtb, at + count),
292 (vtb->vtb_size - at - count)*sizeof(u_int16_t));
294 if (vtb->vtb_type == VTB_FRAMEBUFFER)
295 fillw_io(attr | c, sc_vtb_pointer(vtb, at), count);
297 fillw(attr | c, (void *)sc_vtb_pointer(vtb, at), count);