xref: /freebsd-13-stable/sys/dev/vt/hw/fb/vt_fb.c (revision 3bc80996974a61a4223eae4c1ccd47b6ee32a48a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2013 The FreeBSD Foundation
5  *
6  * This software was developed by Aleksandr Rybalko under sponsorship from the
7  * FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/malloc.h>
35 #include <sys/queue.h>
36 #include <sys/fbio.h>
37 #include <sys/kernel.h>
38 #include <dev/vt/vt.h>
39 #include <dev/vt/hw/fb/vt_fb.h>
40 #include <dev/vt/colors/vt_termcolors.h>
41 
42 #include <vm/vm.h>
43 #include <vm/pmap.h>
44 
45 static struct vt_driver vt_fb_driver = {
46 	.vd_name = "fb",
47 	.vd_init = vt_fb_init,
48 	.vd_fini = vt_fb_fini,
49 	.vd_blank = vt_fb_blank,
50 	.vd_bitblt_text = vt_fb_bitblt_text,
51 	.vd_invalidate_text = vt_fb_invalidate_text,
52 	.vd_bitblt_bmp = vt_fb_bitblt_bitmap,
53 	.vd_drawrect = vt_fb_drawrect,
54 	.vd_setpixel = vt_fb_setpixel,
55 	.vd_postswitch = vt_fb_postswitch,
56 	.vd_priority = VD_PRIORITY_GENERIC+10,
57 	.vd_fb_ioctl = vt_fb_ioctl,
58 	.vd_fb_mmap = vt_fb_mmap,
59 	.vd_suspend = vt_fb_suspend,
60 	.vd_resume = vt_fb_resume,
61 };
62 
63 VT_DRIVER_DECLARE(vt_fb, vt_fb_driver);
64 
65 static void
vt_fb_mem_wr1(struct fb_info * sc,uint32_t o,uint8_t v)66 vt_fb_mem_wr1(struct fb_info *sc, uint32_t o, uint8_t v)
67 {
68 
69 	KASSERT((o < sc->fb_size), ("Offset %#08x out of fb size", o));
70 	*(uint8_t *)(sc->fb_vbase + o) = v;
71 }
72 
73 static void
vt_fb_mem_wr2(struct fb_info * sc,uint32_t o,uint16_t v)74 vt_fb_mem_wr2(struct fb_info *sc, uint32_t o, uint16_t v)
75 {
76 
77 	KASSERT((o < sc->fb_size), ("Offset %#08x out of fb size", o));
78 	*(uint16_t *)(sc->fb_vbase + o) = v;
79 }
80 
81 static void
vt_fb_mem_wr4(struct fb_info * sc,uint32_t o,uint32_t v)82 vt_fb_mem_wr4(struct fb_info *sc, uint32_t o, uint32_t v)
83 {
84 
85 	KASSERT((o < sc->fb_size), ("Offset %#08x out of fb size", o));
86 	*(uint32_t *)(sc->fb_vbase + o) = v;
87 }
88 
89 int
vt_fb_ioctl(struct vt_device * vd,u_long cmd,caddr_t data,struct thread * td)90 vt_fb_ioctl(struct vt_device *vd, u_long cmd, caddr_t data, struct thread *td)
91 {
92 	struct fb_info *info;
93 	int error = 0;
94 
95 	info = vd->vd_softc;
96 
97 	switch (cmd) {
98 	case FBIOGTYPE:
99 		bcopy(info, (struct fbtype *)data, sizeof(struct fbtype));
100 		break;
101 
102 	case FBIO_GETWINORG:	/* get frame buffer window origin */
103 		*(u_int *)data = 0;
104 		break;
105 
106 	case FBIO_GETDISPSTART:	/* get display start address */
107 		((video_display_start_t *)data)->x = 0;
108 		((video_display_start_t *)data)->y = 0;
109 		break;
110 
111 	case FBIO_GETLINEWIDTH:	/* get scan line width in bytes */
112 		*(u_int *)data = info->fb_stride;
113 		break;
114 
115 	case FBIO_BLANK:	/* blank display */
116 		if (vd->vd_driver->vd_blank == NULL)
117 			return (ENODEV);
118 		vd->vd_driver->vd_blank(vd, TC_BLACK);
119 		break;
120 
121 	default:
122 		error = ENOIOCTL;
123 		break;
124 	}
125 
126 	return (error);
127 }
128 
129 int
vt_fb_mmap(struct vt_device * vd,vm_ooffset_t offset,vm_paddr_t * paddr,int prot,vm_memattr_t * memattr)130 vt_fb_mmap(struct vt_device *vd, vm_ooffset_t offset, vm_paddr_t *paddr,
131     int prot, vm_memattr_t *memattr)
132 {
133 	struct fb_info *info;
134 
135 	info = vd->vd_softc;
136 
137 	if (info->fb_flags & FB_FLAG_NOMMAP)
138 		return (ENODEV);
139 
140 	if (offset < info->fb_size) {
141 		if (info->fb_pbase == 0) {
142 			*paddr = vtophys((uint8_t *)info->fb_vbase + offset);
143 		} else {
144 			*paddr = info->fb_pbase + offset;
145 			if (info->fb_flags & FB_FLAG_MEMATTR)
146 				*memattr = info->fb_memattr;
147 #ifdef VM_MEMATTR_WRITE_COMBINING
148 			else
149 				*memattr = VM_MEMATTR_WRITE_COMBINING;
150 #endif
151 		}
152 		return (0);
153 	}
154 
155 	return (EINVAL);
156 }
157 
158 void
vt_fb_setpixel(struct vt_device * vd,int x,int y,term_color_t color)159 vt_fb_setpixel(struct vt_device *vd, int x, int y, term_color_t color)
160 {
161 	struct fb_info *info;
162 	uint32_t c;
163 	u_int o;
164 
165 	info = vd->vd_softc;
166 	c = info->fb_cmap[color];
167 	o = info->fb_stride * y + x * FBTYPE_GET_BYTESPP(info);
168 
169 	if (info->fb_flags & FB_FLAG_NOWRITE)
170 		return;
171 
172 	KASSERT((info->fb_vbase != 0), ("Unmapped framebuffer"));
173 
174 	switch (FBTYPE_GET_BYTESPP(info)) {
175 	case 1:
176 		vt_fb_mem_wr1(info, o, c);
177 		break;
178 	case 2:
179 		vt_fb_mem_wr2(info, o, c);
180 		break;
181 	case 3:
182 		vt_fb_mem_wr1(info, o, (c >> 16) & 0xff);
183 		vt_fb_mem_wr1(info, o + 1, (c >> 8) & 0xff);
184 		vt_fb_mem_wr1(info, o + 2, c & 0xff);
185 		break;
186 	case 4:
187 		vt_fb_mem_wr4(info, o, c);
188 		break;
189 	default:
190 		/* panic? */
191 		return;
192 	}
193 }
194 
195 void
vt_fb_drawrect(struct vt_device * vd,int x1,int y1,int x2,int y2,int fill,term_color_t color)196 vt_fb_drawrect(struct vt_device *vd, int x1, int y1, int x2, int y2, int fill,
197     term_color_t color)
198 {
199 	int x, y;
200 
201 	for (y = y1; y <= y2; y++) {
202 		if (fill || (y == y1) || (y == y2)) {
203 			for (x = x1; x <= x2; x++)
204 				vt_fb_setpixel(vd, x, y, color);
205 		} else {
206 			vt_fb_setpixel(vd, x1, y, color);
207 			vt_fb_setpixel(vd, x2, y, color);
208 		}
209 	}
210 }
211 
212 void
vt_fb_blank(struct vt_device * vd,term_color_t color)213 vt_fb_blank(struct vt_device *vd, term_color_t color)
214 {
215 	struct fb_info *info;
216 	uint32_t c;
217 	u_int o, h;
218 
219 	info = vd->vd_softc;
220 	c = info->fb_cmap[color];
221 
222 	if (info->fb_flags & FB_FLAG_NOWRITE)
223 		return;
224 
225 	KASSERT((info->fb_vbase != 0), ("Unmapped framebuffer"));
226 
227 	switch (FBTYPE_GET_BYTESPP(info)) {
228 	case 1:
229 		for (h = 0; h < info->fb_height; h++)
230 			for (o = 0; o < info->fb_stride; o++)
231 				vt_fb_mem_wr1(info, h*info->fb_stride + o, c);
232 		break;
233 	case 2:
234 		for (h = 0; h < info->fb_height; h++)
235 			for (o = 0; o < info->fb_stride - 1; o += 2)
236 				vt_fb_mem_wr2(info, h*info->fb_stride + o, c);
237 		break;
238 	case 3:
239 		for (h = 0; h < info->fb_height; h++)
240 			for (o = 0; o < info->fb_stride - 2; o += 3) {
241 				vt_fb_mem_wr1(info, h*info->fb_stride + o,
242 				    (c >> 16) & 0xff);
243 				vt_fb_mem_wr1(info, h*info->fb_stride + o + 1,
244 				    (c >> 8) & 0xff);
245 				vt_fb_mem_wr1(info, h*info->fb_stride + o + 2,
246 				    c & 0xff);
247 			}
248 		break;
249 	case 4:
250 		for (h = 0; h < info->fb_height; h++)
251 			for (o = 0; o < info->fb_stride - 3; o += 4)
252 				vt_fb_mem_wr4(info, h*info->fb_stride + o, c);
253 		break;
254 	default:
255 		/* panic? */
256 		return;
257 	}
258 }
259 
260 void
vt_fb_bitblt_bitmap(struct vt_device * vd,const struct vt_window * vw,const uint8_t * pattern,const uint8_t * mask,unsigned int width,unsigned int height,unsigned int x,unsigned int y,term_color_t fg,term_color_t bg)261 vt_fb_bitblt_bitmap(struct vt_device *vd, const struct vt_window *vw,
262     const uint8_t *pattern, const uint8_t *mask,
263     unsigned int width, unsigned int height,
264     unsigned int x, unsigned int y, term_color_t fg, term_color_t bg)
265 {
266 	struct fb_info *info;
267 	uint32_t fgc, bgc, cc, o;
268 	int bpp, bpl, xi, yi;
269 	int bit, byte;
270 
271 	info = vd->vd_softc;
272 	bpp = FBTYPE_GET_BYTESPP(info);
273 	fgc = info->fb_cmap[fg];
274 	bgc = info->fb_cmap[bg];
275 	bpl = (width + 7) / 8; /* Bytes per source line. */
276 
277 	if (info->fb_flags & FB_FLAG_NOWRITE)
278 		return;
279 
280 	KASSERT((info->fb_vbase != 0), ("Unmapped framebuffer"));
281 
282 	/* Bound by right and bottom edges. */
283 	if (y + height > vw->vw_draw_area.tr_end.tp_row) {
284 		if (y >= vw->vw_draw_area.tr_end.tp_row)
285 			return;
286 		height = vw->vw_draw_area.tr_end.tp_row - y;
287 	}
288 	if (x + width > vw->vw_draw_area.tr_end.tp_col) {
289 		if (x >= vw->vw_draw_area.tr_end.tp_col)
290 			return;
291 		width = vw->vw_draw_area.tr_end.tp_col - x;
292 	}
293 	for (yi = 0; yi < height; yi++) {
294 		for (xi = 0; xi < width; xi++) {
295 			byte = yi * bpl + xi / 8;
296 			bit = 0x80 >> (xi % 8);
297 			/* Skip pixel write, if mask bit not set. */
298 			if (mask != NULL && (mask[byte] & bit) == 0)
299 				continue;
300 			o = (y + yi) * info->fb_stride + (x + xi) * bpp;
301 			o += vd->vd_transpose;
302 			cc = pattern[byte] & bit ? fgc : bgc;
303 
304 			switch(bpp) {
305 			case 1:
306 				vt_fb_mem_wr1(info, o, cc);
307 				break;
308 			case 2:
309 				vt_fb_mem_wr2(info, o, cc);
310 				break;
311 			case 3:
312 				/* Packed mode, so unaligned. Byte access. */
313 				vt_fb_mem_wr1(info, o, (cc >> 16) & 0xff);
314 				vt_fb_mem_wr1(info, o + 1, (cc >> 8) & 0xff);
315 				vt_fb_mem_wr1(info, o + 2, cc & 0xff);
316 				break;
317 			case 4:
318 				vt_fb_mem_wr4(info, o, cc);
319 				break;
320 			default:
321 				/* panic? */
322 				break;
323 			}
324 		}
325 	}
326 }
327 
328 void
vt_fb_bitblt_text(struct vt_device * vd,const struct vt_window * vw,const term_rect_t * area)329 vt_fb_bitblt_text(struct vt_device *vd, const struct vt_window *vw,
330     const term_rect_t *area)
331 {
332 	unsigned int col, row, x, y;
333 	struct vt_font *vf;
334 	term_char_t c;
335 	term_color_t fg, bg;
336 	const uint8_t *pattern;
337 	size_t z;
338 
339 	vf = vw->vw_font;
340 
341 	for (row = area->tr_begin.tp_row; row < area->tr_end.tp_row; ++row) {
342 		for (col = area->tr_begin.tp_col; col < area->tr_end.tp_col;
343 		    ++col) {
344 			x = col * vf->vf_width +
345 			    vw->vw_draw_area.tr_begin.tp_col;
346 			y = row * vf->vf_height +
347 			    vw->vw_draw_area.tr_begin.tp_row;
348 
349 			c = VTBUF_GET_FIELD(&vw->vw_buf, row, col);
350 			pattern = vtfont_lookup(vf, c);
351 			vt_determine_colors(c,
352 			    VTBUF_ISCURSOR(&vw->vw_buf, row, col), &fg, &bg);
353 
354 			z = row * PIXEL_WIDTH(VT_FB_MAX_WIDTH) + col;
355 			if (z >= PIXEL_HEIGHT(VT_FB_MAX_HEIGHT) *
356 			    PIXEL_WIDTH(VT_FB_MAX_WIDTH))
357 				continue;
358 			if (vd->vd_drawn && (vd->vd_drawn[z] == c) &&
359 			    vd->vd_drawnfg && (vd->vd_drawnfg[z] == fg) &&
360 			    vd->vd_drawnbg && (vd->vd_drawnbg[z] == bg))
361 				continue;
362 
363 			vt_fb_bitblt_bitmap(vd, vw,
364 			    pattern, NULL, vf->vf_width, vf->vf_height,
365 			    x, y, fg, bg);
366 
367 			if (vd->vd_drawn)
368 				vd->vd_drawn[z] = c;
369 			if (vd->vd_drawnfg)
370 				vd->vd_drawnfg[z] = fg;
371 			if (vd->vd_drawnbg)
372 				vd->vd_drawnbg[z] = bg;
373 		}
374 	}
375 
376 #ifndef SC_NO_CUTPASTE
377 	if (!vd->vd_mshown)
378 		return;
379 
380 	term_rect_t drawn_area;
381 
382 	drawn_area.tr_begin.tp_col = area->tr_begin.tp_col * vf->vf_width;
383 	drawn_area.tr_begin.tp_row = area->tr_begin.tp_row * vf->vf_height;
384 	drawn_area.tr_end.tp_col = area->tr_end.tp_col * vf->vf_width;
385 	drawn_area.tr_end.tp_row = area->tr_end.tp_row * vf->vf_height;
386 
387 	if (vt_is_cursor_in_area(vd, &drawn_area)) {
388 		vt_fb_bitblt_bitmap(vd, vw,
389 		    vd->vd_mcursor->map, vd->vd_mcursor->mask,
390 		    vd->vd_mcursor->width, vd->vd_mcursor->height,
391 		    vd->vd_mx_drawn + vw->vw_draw_area.tr_begin.tp_col,
392 		    vd->vd_my_drawn + vw->vw_draw_area.tr_begin.tp_row,
393 		    vd->vd_mcursor_fg, vd->vd_mcursor_bg);
394 	}
395 #endif
396 }
397 
398 void
vt_fb_invalidate_text(struct vt_device * vd,const term_rect_t * area)399 vt_fb_invalidate_text(struct vt_device *vd, const term_rect_t *area)
400 {
401 	unsigned int col, row;
402 	size_t z;
403 
404 	for (row = area->tr_begin.tp_row; row < area->tr_end.tp_row; ++row) {
405 		for (col = area->tr_begin.tp_col; col < area->tr_end.tp_col;
406 		    ++col) {
407 			z = row * PIXEL_WIDTH(VT_FB_MAX_WIDTH) + col;
408 			if (z >= PIXEL_HEIGHT(VT_FB_MAX_HEIGHT) *
409 			    PIXEL_WIDTH(VT_FB_MAX_WIDTH))
410 				continue;
411 			if (vd->vd_drawn)
412 				vd->vd_drawn[z] = 0;
413 			if (vd->vd_drawnfg)
414 				vd->vd_drawnfg[z] = 0;
415 			if (vd->vd_drawnbg)
416 				vd->vd_drawnbg[z] = 0;
417 		}
418 	}
419 }
420 
421 void
vt_fb_postswitch(struct vt_device * vd)422 vt_fb_postswitch(struct vt_device *vd)
423 {
424 	struct fb_info *info;
425 
426 	info = vd->vd_softc;
427 
428 	if (info->enter != NULL)
429 		info->enter(info->fb_priv);
430 }
431 
432 static int
vt_fb_init_cmap(uint32_t * cmap,int depth)433 vt_fb_init_cmap(uint32_t *cmap, int depth)
434 {
435 
436 	switch (depth) {
437 	case 8:
438 		return (vt_generate_cons_palette(cmap, COLOR_FORMAT_RGB,
439 		    0x7, 5, 0x7, 2, 0x3, 0));
440 	case 15:
441 		return (vt_generate_cons_palette(cmap, COLOR_FORMAT_RGB,
442 		    0x1f, 10, 0x1f, 5, 0x1f, 0));
443 	case 16:
444 		return (vt_generate_cons_palette(cmap, COLOR_FORMAT_RGB,
445 		    0x1f, 11, 0x3f, 5, 0x1f, 0));
446 	case 24:
447 	case 32: /* Ignore alpha. */
448 		return (vt_generate_cons_palette(cmap, COLOR_FORMAT_RGB,
449 		    0xff, 16, 0xff, 8, 0xff, 0));
450 	default:
451 		return (1);
452 	}
453 }
454 
455 int
vt_fb_init(struct vt_device * vd)456 vt_fb_init(struct vt_device *vd)
457 {
458 	struct fb_info *info;
459 	u_int margin;
460 	int bg, err;
461 	term_color_t c;
462 
463 	info = vd->vd_softc;
464 	vd->vd_height = MIN(VT_FB_MAX_HEIGHT, info->fb_height);
465 	margin = (info->fb_height - vd->vd_height) >> 1;
466 	vd->vd_transpose = margin * info->fb_stride;
467 	vd->vd_width = MIN(VT_FB_MAX_WIDTH, info->fb_width);
468 	margin = (info->fb_width - vd->vd_width) >> 1;
469 	vd->vd_transpose += margin * (info->fb_bpp / NBBY);
470 	vd->vd_video_dev = info->fb_video_dev;
471 
472 	if (info->fb_size == 0)
473 		return (CN_DEAD);
474 
475 	if (info->fb_pbase == 0 && info->fb_vbase == 0)
476 		info->fb_flags |= FB_FLAG_NOMMAP;
477 
478 	if (info->fb_cmsize <= 0) {
479 		err = vt_fb_init_cmap(info->fb_cmap, FBTYPE_GET_BPP(info));
480 		if (err)
481 			return (CN_DEAD);
482 		info->fb_cmsize = 16;
483 	}
484 
485 	c = TC_BLACK;
486 	if (TUNABLE_INT_FETCH("teken.bg_color", &bg) != 0) {
487 		if (bg == TC_WHITE)
488 			bg |= TC_LIGHT;
489 		c = bg;
490 	}
491 	/* Clear the screen. */
492 	vd->vd_driver->vd_blank(vd, c);
493 
494 	/* Wakeup screen. KMS need this. */
495 	vt_fb_postswitch(vd);
496 
497 	return (CN_INTERNAL);
498 }
499 
500 void
vt_fb_fini(struct vt_device * vd,void * softc)501 vt_fb_fini(struct vt_device *vd, void *softc)
502 {
503 
504 	vd->vd_video_dev = NULL;
505 }
506 
507 int
vt_fb_attach(struct fb_info * info)508 vt_fb_attach(struct fb_info *info)
509 {
510 	int ret;
511 
512 	ret = vt_allocate(&vt_fb_driver, info);
513 
514 	return (ret);
515 }
516 
517 int
vt_fb_detach(struct fb_info * info)518 vt_fb_detach(struct fb_info *info)
519 {
520 	int ret;
521 
522 	ret = vt_deallocate(&vt_fb_driver, info);
523 
524 	return (ret);
525 }
526 
527 void
vt_fb_suspend(struct vt_device * vd)528 vt_fb_suspend(struct vt_device *vd)
529 {
530 
531 	vt_suspend(vd);
532 }
533 
534 void
vt_fb_resume(struct vt_device * vd)535 vt_fb_resume(struct vt_device *vd)
536 {
537 
538 	vt_resume(vd);
539 }
540