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