xref: /freebsd/sys/dev/syscons/scvtb.c (revision 817420dc8eac7df799c78f5309b75092b7f7cd40)
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$
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 
46 void
47 sc_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 		} else {
69 			vtb->vtb_buffer = (vm_offset_t)buf;
70 		}
71 		vtb->vtb_flags |= VTB_VALID;
72 		break;
73 	case VTB_FRAMEBUFFER:
74 		vtb->vtb_buffer = (vm_offset_t)buf;
75 		vtb->vtb_flags |= VTB_VALID;
76 		break;
77 	default:
78 		break;
79 	}
80 }
81 
82 void
83 sc_vtb_destroy(sc_vtb_t *vtb)
84 {
85 	vm_offset_t p;
86 
87 	vtb->vtb_flags = 0;
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 (p != NULL)
99 			free((void *)p, M_DEVBUF);
100 		break;
101 	default:
102 		break;
103 	}
104 	vtb->vtb_type = VTB_INVALID;
105 }
106 
107 size_t
108 sc_vtb_size(int cols, int rows)
109 {
110 	return (size_t)(cols*rows*sizeof(u_int16_t));
111 }
112 
113 int
114 sc_vtb_getc(sc_vtb_t *vtb, int at)
115 {
116 	if (vtb->vtb_type == VTB_FRAMEBUFFER)
117 		return (readw(sc_vtb_pointer(vtb, at)) & 0x00ff);
118 	else
119 		return (*(u_int16_t *)sc_vtb_pointer(vtb, at) & 0x00ff);
120 }
121 
122 int
123 sc_vtb_geta(sc_vtb_t *vtb, int at)
124 {
125 	if (vtb->vtb_type == VTB_FRAMEBUFFER)
126 		return (readw(sc_vtb_pointer(vtb, at)) & 0xff00);
127 	else
128 		return (*(u_int16_t *)sc_vtb_pointer(vtb, at) & 0xff00);
129 }
130 
131 void
132 sc_vtb_putc(sc_vtb_t *vtb, int at, int c, int a)
133 {
134 	if (vtb->vtb_type == VTB_FRAMEBUFFER)
135 		writew(sc_vtb_pointer(vtb, at), a | c);
136 	else
137 		*(u_int16_t *)sc_vtb_pointer(vtb, at) = a | c;
138 }
139 
140 vm_offset_t
141 sc_vtb_putchar(sc_vtb_t *vtb, vm_offset_t p, int c, int a)
142 {
143 	if (vtb->vtb_type == VTB_FRAMEBUFFER)
144 		writew(p, a | c);
145 	else
146 		*(u_int16_t *)p = a | c;
147 	return (p + sizeof(u_int16_t));
148 }
149 
150 vm_offset_t
151 sc_vtb_pointer(sc_vtb_t *vtb, int at)
152 {
153 	return (vtb->vtb_buffer + sizeof(u_int16_t)*(at));
154 }
155 
156 int
157 sc_vtb_pos(sc_vtb_t *vtb, int pos, int offset)
158 {
159 	return ((pos + offset + vtb->vtb_size)%vtb->vtb_size);
160 }
161 
162 void
163 sc_vtb_clear(sc_vtb_t *vtb, int c, int attr)
164 {
165 	if (vtb->vtb_type == VTB_FRAMEBUFFER)
166 		fillw_io(attr | c, sc_vtb_pointer(vtb, 0), vtb->vtb_size);
167 	else
168 		fillw(attr | c, (void *)sc_vtb_pointer(vtb, 0), vtb->vtb_size);
169 }
170 
171 void
172 sc_vtb_copy(sc_vtb_t *vtb1, int from, sc_vtb_t *vtb2, int to, int count)
173 {
174 	/* XXX if both are VTB_VRAMEBUFFER... */
175 	if (vtb2->vtb_type == VTB_FRAMEBUFFER) {
176 		bcopy_toio(sc_vtb_pointer(vtb1, from),
177 			   sc_vtb_pointer(vtb2, to),
178 			   count*sizeof(u_int16_t));
179 	} else if (vtb1->vtb_type == VTB_FRAMEBUFFER) {
180 		bcopy_fromio(sc_vtb_pointer(vtb1, from),
181 			     sc_vtb_pointer(vtb2, to),
182 			     count*sizeof(u_int16_t));
183 	} else {
184 		bcopy((void *)sc_vtb_pointer(vtb1, from),
185 		      (void *)sc_vtb_pointer(vtb2, to),
186 		      count*sizeof(u_int16_t));
187 	}
188 }
189 
190 void
191 sc_vtb_append(sc_vtb_t *vtb1, int from, sc_vtb_t *vtb2, int count)
192 {
193 	int len;
194 
195 	if (vtb2->vtb_type != VTB_RINGBUFFER)
196 		return;
197 
198 	while (count > 0) {
199 		len = imin(count, vtb2->vtb_size - vtb2->vtb_tail);
200 		if (vtb1->vtb_type == VTB_FRAMEBUFFER) {
201 			bcopy_fromio(sc_vtb_pointer(vtb1, from),
202 				     sc_vtb_pointer(vtb2, vtb2->vtb_tail),
203 				     len*sizeof(u_int16_t));
204 		} else {
205 			bcopy((void *)sc_vtb_pointer(vtb1, from),
206 			      (void *)sc_vtb_pointer(vtb2, vtb2->vtb_tail),
207 			      len*sizeof(u_int16_t));
208 		}
209 		from += len;
210 		count -= len;
211 		vtb2->vtb_tail = vtb_wrap(vtb2, vtb2->vtb_tail, len);
212 	}
213 }
214 
215 void
216 sc_vtb_seek(sc_vtb_t *vtb, int pos)
217 {
218 	vtb->vtb_tail = pos%vtb->vtb_size;
219 }
220 
221 void
222 sc_vtb_erase(sc_vtb_t *vtb, int at, int count, int c, int attr)
223 {
224 	if (at + count > vtb->vtb_size)
225 		count = vtb->vtb_size - at;
226 	if (vtb->vtb_type == VTB_FRAMEBUFFER)
227 		fillw_io(attr | c, sc_vtb_pointer(vtb, at), count);
228 	else
229 		fillw(attr | c, (void *)sc_vtb_pointer(vtb, at), count);
230 }
231 
232 void
233 sc_vtb_move(sc_vtb_t *vtb, int from, int to, int count)
234 {
235 	if (from + count > vtb->vtb_size)
236 		count = vtb->vtb_size - from;
237 	if (to + count > vtb->vtb_size)
238 		count = vtb->vtb_size - to;
239 	if (count <= 0)
240 		return;
241 	if (vtb->vtb_type == VTB_FRAMEBUFFER) {
242 		bcopy_io(sc_vtb_pointer(vtb, from),
243 			 sc_vtb_pointer(vtb, to), count*sizeof(u_int16_t));
244 	} else {
245 		bcopy((void *)sc_vtb_pointer(vtb, from),
246 		      (void *)sc_vtb_pointer(vtb, to), count*sizeof(u_int16_t));
247 	}
248 }
249 
250 void
251 sc_vtb_delete(sc_vtb_t *vtb, int at, int count, int c, int attr)
252 {
253 	int len;
254 
255 	if (at + count > vtb->vtb_size)
256 		count = vtb->vtb_size - at;
257 	len = vtb->vtb_size - at - count;
258 	if (len > 0) {
259 		if (vtb->vtb_type == VTB_FRAMEBUFFER) {
260 			bcopy_io(sc_vtb_pointer(vtb, at + count),
261 				 sc_vtb_pointer(vtb, at),
262 				 len*sizeof(u_int16_t));
263 		} else {
264 			bcopy((void *)sc_vtb_pointer(vtb, at + count),
265 			      (void *)sc_vtb_pointer(vtb, at),
266 			      len*sizeof(u_int16_t));
267 		}
268 	}
269 	if (vtb->vtb_type == VTB_FRAMEBUFFER)
270 		fillw_io(attr | c, sc_vtb_pointer(vtb, at + len),
271 			 vtb->vtb_size - at - len);
272 	else
273 		fillw(attr | c, (void *)sc_vtb_pointer(vtb, at + len),
274 		      vtb->vtb_size - at - len);
275 }
276 
277 void
278 sc_vtb_ins(sc_vtb_t *vtb, int at, int count, int c, int attr)
279 {
280 	if (at + count > vtb->vtb_size) {
281 		count = vtb->vtb_size - at;
282 	} else {
283 		if (vtb->vtb_type == VTB_FRAMEBUFFER) {
284 			bcopy_io(sc_vtb_pointer(vtb, at),
285 				 sc_vtb_pointer(vtb, at + count),
286 				 (vtb->vtb_size - at - count)*sizeof(u_int16_t));
287 		} else {
288 			bcopy((void *)sc_vtb_pointer(vtb, at),
289 			      (void *)sc_vtb_pointer(vtb, at + count),
290 			      (vtb->vtb_size - at - count)*sizeof(u_int16_t));
291 		}
292 	}
293 	if (vtb->vtb_type == VTB_FRAMEBUFFER)
294 		fillw_io(attr | c, sc_vtb_pointer(vtb, at), count);
295 	else
296 		fillw(attr | c, (void *)sc_vtb_pointer(vtb, at), count);
297 }
298