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