scvtb.c revision 78956
1/*-
2 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
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: head/sys/dev/syscons/scvtb.c 78956 2001-06-29 08:24:56Z yokota $
27 */
28
29#include "opt_syscons.h"
30
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/malloc.h>
34#include <sys/fbio.h>
35#include <sys/consio.h>
36
37#include <machine/md_var.h>
38#include <machine/bus.h>
39
40#include <dev/fb/fbreg.h>
41#include <dev/syscons/syscons.h>
42
43#define vtb_wrap(vtb, at, offset)				\
44    (((at) + (offset) + (vtb)->vtb_size)%(vtb)->vtb_size)
45
46void
47sc_vtb_init(sc_vtb_t *vtb, int type, int cols, int rows, void *buf, int wait)
48{
49	vtb->vtb_flags = 0;
50	vtb->vtb_type = type;
51	vtb->vtb_cols = cols;
52	vtb->vtb_rows = rows;
53	vtb->vtb_size = cols*rows;
54	vtb->vtb_buffer = NULL;
55	vtb->vtb_tail = 0;
56
57	switch (type) {
58	case VTB_MEMORY:
59	case VTB_RINGBUFFER:
60		if ((buf == NULL) && (cols*rows != 0)) {
61			vtb->vtb_buffer =
62				(vm_offset_t)malloc(cols*rows*sizeof(u_int16_t),
63						    M_DEVBUF,
64						    (wait) ? M_WAITOK : M_NOWAIT);
65			if (vtb->vtb_buffer != NULL) {
66				bzero((void *)sc_vtb_pointer(vtb, 0),
67				      cols*rows*sizeof(u_int16_t));
68				vtb->vtb_flags |= VTB_ALLOCED;
69			}
70		} else {
71			vtb->vtb_buffer = (vm_offset_t)buf;
72		}
73		vtb->vtb_flags |= VTB_VALID;
74		break;
75	case VTB_FRAMEBUFFER:
76		vtb->vtb_buffer = (vm_offset_t)buf;
77		vtb->vtb_flags |= VTB_VALID;
78		break;
79	default:
80		break;
81	}
82}
83
84void
85sc_vtb_destroy(sc_vtb_t *vtb)
86{
87	vm_offset_t p;
88
89	vtb->vtb_cols = 0;
90	vtb->vtb_rows = 0;
91	vtb->vtb_size = 0;
92	vtb->vtb_tail = 0;
93
94	p = vtb->vtb_buffer;
95	vtb->vtb_buffer = NULL;
96	switch (vtb->vtb_type) {
97	case VTB_MEMORY:
98	case VTB_RINGBUFFER:
99		if ((vtb->vtb_flags & VTB_ALLOCED) && (p != NULL))
100			free((void *)p, M_DEVBUF);
101		break;
102	default:
103		break;
104	}
105	vtb->vtb_flags = 0;
106	vtb->vtb_type = VTB_INVALID;
107}
108
109size_t
110sc_vtb_size(int cols, int rows)
111{
112	return (size_t)(cols*rows*sizeof(u_int16_t));
113}
114
115int
116sc_vtb_getc(sc_vtb_t *vtb, int at)
117{
118	if (vtb->vtb_type == VTB_FRAMEBUFFER)
119		return (readw(sc_vtb_pointer(vtb, at)) & 0x00ff);
120	else
121		return (*(u_int16_t *)sc_vtb_pointer(vtb, at) & 0x00ff);
122}
123
124int
125sc_vtb_geta(sc_vtb_t *vtb, int at)
126{
127	if (vtb->vtb_type == VTB_FRAMEBUFFER)
128		return (readw(sc_vtb_pointer(vtb, at)) & 0xff00);
129	else
130		return (*(u_int16_t *)sc_vtb_pointer(vtb, at) & 0xff00);
131}
132
133void
134sc_vtb_putc(sc_vtb_t *vtb, int at, int c, int a)
135{
136	if (vtb->vtb_type == VTB_FRAMEBUFFER)
137		writew(sc_vtb_pointer(vtb, at), a | c);
138	else
139		*(u_int16_t *)sc_vtb_pointer(vtb, at) = a | c;
140}
141
142vm_offset_t
143sc_vtb_putchar(sc_vtb_t *vtb, vm_offset_t p, int c, int a)
144{
145	if (vtb->vtb_type == VTB_FRAMEBUFFER)
146		writew(p, a | c);
147	else
148		*(u_int16_t *)p = a | c;
149	return (p + sizeof(u_int16_t));
150}
151
152vm_offset_t
153sc_vtb_pointer(sc_vtb_t *vtb, int at)
154{
155	return (vtb->vtb_buffer + sizeof(u_int16_t)*(at));
156}
157
158int
159sc_vtb_pos(sc_vtb_t *vtb, int pos, int offset)
160{
161	return ((pos + offset + vtb->vtb_size)%vtb->vtb_size);
162}
163
164void
165sc_vtb_clear(sc_vtb_t *vtb, int c, int attr)
166{
167	if (vtb->vtb_type == VTB_FRAMEBUFFER)
168		fillw_io(attr | c, sc_vtb_pointer(vtb, 0), vtb->vtb_size);
169	else
170		fillw(attr | c, (void *)sc_vtb_pointer(vtb, 0), vtb->vtb_size);
171}
172
173void
174sc_vtb_copy(sc_vtb_t *vtb1, int from, sc_vtb_t *vtb2, int to, int count)
175{
176	/* XXX if both are VTB_VRAMEBUFFER... */
177	if (vtb2->vtb_type == VTB_FRAMEBUFFER) {
178		bcopy_toio(sc_vtb_pointer(vtb1, from),
179			   sc_vtb_pointer(vtb2, to),
180			   count*sizeof(u_int16_t));
181	} else if (vtb1->vtb_type == VTB_FRAMEBUFFER) {
182		bcopy_fromio(sc_vtb_pointer(vtb1, from),
183			     sc_vtb_pointer(vtb2, to),
184			     count*sizeof(u_int16_t));
185	} else {
186		bcopy((void *)sc_vtb_pointer(vtb1, from),
187		      (void *)sc_vtb_pointer(vtb2, to),
188		      count*sizeof(u_int16_t));
189	}
190}
191
192void
193sc_vtb_append(sc_vtb_t *vtb1, int from, sc_vtb_t *vtb2, int count)
194{
195	int len;
196
197	if (vtb2->vtb_type != VTB_RINGBUFFER)
198		return;
199
200	while (count > 0) {
201		len = imin(count, vtb2->vtb_size - vtb2->vtb_tail);
202		if (vtb1->vtb_type == VTB_FRAMEBUFFER) {
203			bcopy_fromio(sc_vtb_pointer(vtb1, from),
204				     sc_vtb_pointer(vtb2, vtb2->vtb_tail),
205				     len*sizeof(u_int16_t));
206		} else {
207			bcopy((void *)sc_vtb_pointer(vtb1, from),
208			      (void *)sc_vtb_pointer(vtb2, vtb2->vtb_tail),
209			      len*sizeof(u_int16_t));
210		}
211		from += len;
212		count -= len;
213		vtb2->vtb_tail = vtb_wrap(vtb2, vtb2->vtb_tail, len);
214	}
215}
216
217void
218sc_vtb_seek(sc_vtb_t *vtb, int pos)
219{
220	vtb->vtb_tail = pos%vtb->vtb_size;
221}
222
223void
224sc_vtb_erase(sc_vtb_t *vtb, int at, int count, int c, int attr)
225{
226	if (at + count > vtb->vtb_size)
227		count = vtb->vtb_size - at;
228	if (vtb->vtb_type == VTB_FRAMEBUFFER)
229		fillw_io(attr | c, sc_vtb_pointer(vtb, at), count);
230	else
231		fillw(attr | c, (void *)sc_vtb_pointer(vtb, at), count);
232}
233
234void
235sc_vtb_move(sc_vtb_t *vtb, int from, int to, int count)
236{
237	if (from + count > vtb->vtb_size)
238		count = vtb->vtb_size - from;
239	if (to + count > vtb->vtb_size)
240		count = vtb->vtb_size - to;
241	if (count <= 0)
242		return;
243	if (vtb->vtb_type == VTB_FRAMEBUFFER) {
244		bcopy_io(sc_vtb_pointer(vtb, from),
245			 sc_vtb_pointer(vtb, to), count*sizeof(u_int16_t));
246	} else {
247		bcopy((void *)sc_vtb_pointer(vtb, from),
248		      (void *)sc_vtb_pointer(vtb, to), count*sizeof(u_int16_t));
249	}
250}
251
252void
253sc_vtb_delete(sc_vtb_t *vtb, int at, int count, int c, int attr)
254{
255	int len;
256
257	if (at + count > vtb->vtb_size)
258		count = vtb->vtb_size - at;
259	len = vtb->vtb_size - at - count;
260	if (len > 0) {
261		if (vtb->vtb_type == VTB_FRAMEBUFFER) {
262			bcopy_io(sc_vtb_pointer(vtb, at + count),
263				 sc_vtb_pointer(vtb, at),
264				 len*sizeof(u_int16_t));
265		} else {
266			bcopy((void *)sc_vtb_pointer(vtb, at + count),
267			      (void *)sc_vtb_pointer(vtb, at),
268			      len*sizeof(u_int16_t));
269		}
270	}
271	if (vtb->vtb_type == VTB_FRAMEBUFFER)
272		fillw_io(attr | c, sc_vtb_pointer(vtb, at + len),
273			 vtb->vtb_size - at - len);
274	else
275		fillw(attr | c, (void *)sc_vtb_pointer(vtb, at + len),
276		      vtb->vtb_size - at - len);
277}
278
279void
280sc_vtb_ins(sc_vtb_t *vtb, int at, int count, int c, int attr)
281{
282	if (at + count > vtb->vtb_size) {
283		count = vtb->vtb_size - at;
284	} else {
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));
289		} else {
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));
293		}
294	}
295	if (vtb->vtb_type == VTB_FRAMEBUFFER)
296		fillw_io(attr | c, sc_vtb_pointer(vtb, at), count);
297	else
298		fillw(attr | c, (void *)sc_vtb_pointer(vtb, at), count);
299}
300