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