1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. 16 * 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. 27 * 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include "opt_syscons.h" 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/malloc.h> 38 #include <sys/fbio.h> 39 #include <sys/consio.h> 40 41 #include <machine/md_var.h> 42 #include <machine/bus.h> 43 44 #include <dev/fb/fbreg.h> 45 #include <dev/syscons/syscons.h> 46 47 #define vtb_wrap(vtb, at, offset) \ 48 (((at) + (offset) + (vtb)->vtb_size)%(vtb)->vtb_size) 49 50 void 51 sc_vtb_init(sc_vtb_t *vtb, int type, int cols, int rows, void *buf, int wait) 52 { 53 vtb->vtb_flags = 0; 54 vtb->vtb_type = type; 55 vtb->vtb_cols = cols; 56 vtb->vtb_rows = rows; 57 vtb->vtb_size = cols*rows; 58 vtb->vtb_buffer = 0; 59 vtb->vtb_tail = 0; 60 61 switch (type) { 62 case VTB_MEMORY: 63 case VTB_RINGBUFFER: 64 if ((buf == NULL) && (cols*rows != 0)) { 65 vtb->vtb_buffer = 66 (vm_offset_t)malloc(cols*rows*sizeof(u_int16_t), 67 M_DEVBUF, 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; 73 } 74 } else { 75 vtb->vtb_buffer = (vm_offset_t)buf; 76 } 77 vtb->vtb_flags |= VTB_VALID; 78 break; 79 case VTB_FRAMEBUFFER: 80 vtb->vtb_buffer = (vm_offset_t)buf; 81 vtb->vtb_flags |= VTB_VALID; 82 break; 83 default: 84 break; 85 } 86 } 87 88 void 89 sc_vtb_destroy(sc_vtb_t *vtb) 90 { 91 vm_offset_t p; 92 93 vtb->vtb_cols = 0; 94 vtb->vtb_rows = 0; 95 vtb->vtb_size = 0; 96 vtb->vtb_tail = 0; 97 98 p = vtb->vtb_buffer; 99 vtb->vtb_buffer = 0; 100 switch (vtb->vtb_type) { 101 case VTB_MEMORY: 102 case VTB_RINGBUFFER: 103 if ((vtb->vtb_flags & VTB_ALLOCED) && (p != 0)) 104 free((void *)p, M_DEVBUF); 105 break; 106 default: 107 break; 108 } 109 vtb->vtb_flags = 0; 110 vtb->vtb_type = VTB_INVALID; 111 } 112 113 size_t 114 sc_vtb_size(int cols, int rows) 115 { 116 return (size_t)(cols*rows*sizeof(u_int16_t)); 117 } 118 119 int 120 sc_vtb_getc(sc_vtb_t *vtb, int at) 121 { 122 if (vtb->vtb_type == VTB_FRAMEBUFFER) 123 return (readw(sc_vtb_pointer(vtb, at)) & 0x00ff); 124 else 125 return (*(u_int16_t *)sc_vtb_pointer(vtb, at) & 0x00ff); 126 } 127 128 int 129 sc_vtb_geta(sc_vtb_t *vtb, int at) 130 { 131 if (vtb->vtb_type == VTB_FRAMEBUFFER) 132 return (readw(sc_vtb_pointer(vtb, at)) & 0xff00); 133 else 134 return (*(u_int16_t *)sc_vtb_pointer(vtb, at) & 0xff00); 135 } 136 137 void 138 sc_vtb_putc(sc_vtb_t *vtb, int at, int c, int a) 139 { 140 if (vtb->vtb_type == VTB_FRAMEBUFFER) 141 writew(sc_vtb_pointer(vtb, at), a | c); 142 else 143 *(u_int16_t *)sc_vtb_pointer(vtb, at) = a | c; 144 } 145 146 vm_offset_t 147 sc_vtb_putchar(sc_vtb_t *vtb, vm_offset_t p, int c, int a) 148 { 149 if (vtb->vtb_type == VTB_FRAMEBUFFER) 150 writew(p, a | c); 151 else 152 *(u_int16_t *)p = a | c; 153 return (p + sizeof(u_int16_t)); 154 } 155 156 vm_offset_t 157 sc_vtb_pointer(sc_vtb_t *vtb, int at) 158 { 159 return (vtb->vtb_buffer + sizeof(u_int16_t)*(at)); 160 } 161 162 int 163 sc_vtb_pos(sc_vtb_t *vtb, int pos, int offset) 164 { 165 return ((pos + offset + vtb->vtb_size)%vtb->vtb_size); 166 } 167 168 void 169 sc_vtb_clear(sc_vtb_t *vtb, int c, int attr) 170 { 171 if (vtb->vtb_type == VTB_FRAMEBUFFER) 172 fillw_io(attr | c, sc_vtb_pointer(vtb, 0), vtb->vtb_size); 173 else 174 fillw(attr | c, (void *)sc_vtb_pointer(vtb, 0), vtb->vtb_size); 175 } 176 177 void 178 sc_vtb_copy(sc_vtb_t *vtb1, int from, sc_vtb_t *vtb2, int to, int count) 179 { 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)); 189 else 190 bcopy((void *)sc_vtb_pointer(vtb1, from), 191 (void *)sc_vtb_pointer(vtb2, to), 192 count*sizeof(u_int16_t)); 193 } 194 195 void 196 sc_vtb_append(sc_vtb_t *vtb1, int from, sc_vtb_t *vtb2, int count) 197 { 198 int len; 199 200 if (vtb2->vtb_type != VTB_RINGBUFFER) 201 return; 202 203 while (count > 0) { 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)); 209 else 210 bcopy((void *)sc_vtb_pointer(vtb1, from), 211 (void *)sc_vtb_pointer(vtb2, vtb2->vtb_tail), 212 len*sizeof(u_int16_t)); 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 void 254 sc_vtb_delete(sc_vtb_t *vtb, int at, int count, int c, int attr) 255 { 256 int len; 257 258 if (at + count > vtb->vtb_size) 259 count = vtb->vtb_size - at; 260 len = vtb->vtb_size - at - count; 261 if (len > 0) { 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)); 266 else 267 bcopy((void *)sc_vtb_pointer(vtb, at + count), 268 (void *)sc_vtb_pointer(vtb, at), 269 len*sizeof(u_int16_t)); 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 279 void 280 sc_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 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