xref: /freebsd/sys/dev/vt/hw/ofwfb/ofwfb.c (revision 834063202a16592e1ef5c3a9fbd04ca5f1df3ed0)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2011 Nathan Whitehorn
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.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/systm.h>
35 #include <sys/fbio.h>
36 
37 #include <dev/vt/vt.h>
38 #include <dev/vt/hw/fb/vt_fb.h>
39 #include <dev/vt/colors/vt_termcolors.h>
40 
41 #include <vm/vm.h>
42 #include <vm/pmap.h>
43 
44 #include <machine/bus.h>
45 #ifdef __sparc64__
46 #include <machine/bus_private.h>
47 #endif
48 
49 #include <dev/ofw/openfirm.h>
50 #include <dev/ofw/ofw_bus.h>
51 #include <dev/ofw/ofw_pci.h>
52 
53 struct ofwfb_softc {
54 	struct fb_info	fb;
55 
56 	phandle_t	sc_node;
57 	ihandle_t	sc_handle;
58 	bus_space_tag_t	sc_memt;
59 	int		iso_palette;
60 };
61 
62 static void ofwfb_initialize(struct vt_device *vd);
63 static vd_probe_t	ofwfb_probe;
64 static vd_init_t	ofwfb_init;
65 static vd_bitblt_text_t	ofwfb_bitblt_text;
66 static vd_bitblt_bmp_t	ofwfb_bitblt_bitmap;
67 
68 static const struct vt_driver vt_ofwfb_driver = {
69 	.vd_name	= "ofwfb",
70 	.vd_probe	= ofwfb_probe,
71 	.vd_init	= ofwfb_init,
72 	.vd_blank	= vt_fb_blank,
73 	.vd_bitblt_text	= ofwfb_bitblt_text,
74 	.vd_bitblt_bmp	= ofwfb_bitblt_bitmap,
75 	.vd_fb_ioctl	= vt_fb_ioctl,
76 	.vd_fb_mmap	= vt_fb_mmap,
77 	.vd_priority	= VD_PRIORITY_GENERIC+1,
78 };
79 
80 static unsigned char ofw_colors[16] = {
81 	/* See "16-color Text Extension" Open Firmware document, page 4 */
82 	0, 4, 2, 6, 1, 5, 3, 7,
83 	8, 12, 10, 14, 9, 13, 11, 15
84 };
85 
86 static struct ofwfb_softc ofwfb_conssoftc;
87 VT_DRIVER_DECLARE(vt_ofwfb, vt_ofwfb_driver);
88 
89 static int
90 ofwfb_probe(struct vt_device *vd)
91 {
92 	phandle_t chosen, node;
93 	ihandle_t stdout;
94 	char type[64];
95 
96 	chosen = OF_finddevice("/chosen");
97 	if (chosen == -1)
98 		return (CN_DEAD);
99 
100 	node = -1;
101 	if (OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)) ==
102 	    sizeof(stdout))
103 		node = OF_instance_to_package(stdout);
104 	if (node == -1) {
105 		/*
106 		 * The "/chosen/stdout" does not exist try
107 		 * using "screen" directly.
108 		 */
109 		node = OF_finddevice("screen");
110 	}
111 	OF_getprop(node, "device_type", type, sizeof(type));
112 	if (strcmp(type, "display") != 0)
113 		return (CN_DEAD);
114 
115 	/* Looks OK... */
116 	return (CN_INTERNAL);
117 }
118 
119 static void
120 ofwfb_bitblt_bitmap(struct vt_device *vd, const struct vt_window *vw,
121     const uint8_t *pattern, const uint8_t *mask,
122     unsigned int width, unsigned int height,
123     unsigned int x, unsigned int y, term_color_t fg, term_color_t bg)
124 {
125 	struct fb_info *sc = vd->vd_softc;
126 	u_long line;
127 	uint32_t fgc, bgc;
128 	int c, l;
129 	uint8_t b, m;
130 	union {
131 		uint32_t l;
132 		uint8_t	 c[4];
133 	} ch1, ch2;
134 
135 #ifdef __powerpc__
136 	/* Deal with unmapped framebuffers */
137 	if (sc->fb_flags & FB_FLAG_NOWRITE) {
138 		if (pmap_bootstrapped) {
139 			sc->fb_flags &= ~FB_FLAG_NOWRITE;
140 			ofwfb_initialize(vd);
141 		} else {
142 			return;
143 		}
144 	}
145 #endif
146 
147 	fgc = sc->fb_cmap[fg];
148 	bgc = sc->fb_cmap[bg];
149 	b = m = 0;
150 
151 	if (((struct ofwfb_softc *)vd->vd_softc)->iso_palette) {
152 		fg = ofw_colors[fg];
153 		bg = ofw_colors[bg];
154 	}
155 
156 	line = (sc->fb_stride * y) + x * sc->fb_bpp/8;
157 	if (mask == NULL && sc->fb_bpp == 8 && (width % 8 == 0)) {
158 		/* Don't try to put off screen pixels */
159 		if (((x + width) > vd->vd_width) || ((y + height) >
160 		    vd->vd_height))
161 			return;
162 
163 		for (; height > 0; height--) {
164 			for (c = 0; c < width; c += 8) {
165 				b = *pattern++;
166 
167 				/*
168 				 * Assume that there is more background than
169 				 * foreground in characters and init accordingly
170 				 */
171 				ch1.l = ch2.l = (bg << 24) | (bg << 16) |
172 				    (bg << 8) | bg;
173 
174 				/*
175 				 * Calculate 2 x 4-chars at a time, and then
176 				 * write these out.
177 				 */
178 				if (b & 0x80) ch1.c[0] = fg;
179 				if (b & 0x40) ch1.c[1] = fg;
180 				if (b & 0x20) ch1.c[2] = fg;
181 				if (b & 0x10) ch1.c[3] = fg;
182 
183 				if (b & 0x08) ch2.c[0] = fg;
184 				if (b & 0x04) ch2.c[1] = fg;
185 				if (b & 0x02) ch2.c[2] = fg;
186 				if (b & 0x01) ch2.c[3] = fg;
187 
188 				*(uint32_t *)(sc->fb_vbase + line + c) = ch1.l;
189 				*(uint32_t *)(sc->fb_vbase + line + c + 4) =
190 				    ch2.l;
191 			}
192 			line += sc->fb_stride;
193 		}
194 	} else {
195 		for (l = 0;
196 		    l < height && y + l < vw->vw_draw_area.tr_end.tp_row;
197 		    l++) {
198 			for (c = 0;
199 			    c < width && x + c < vw->vw_draw_area.tr_end.tp_col;
200 			    c++) {
201 				if (c % 8 == 0)
202 					b = *pattern++;
203 				else
204 					b <<= 1;
205 				if (mask != NULL) {
206 					if (c % 8 == 0)
207 						m = *mask++;
208 					else
209 						m <<= 1;
210 					/* Skip pixel write, if mask not set. */
211 					if ((m & 0x80) == 0)
212 						continue;
213 				}
214 				switch(sc->fb_bpp) {
215 				case 8:
216 					*(uint8_t *)(sc->fb_vbase + line + c) =
217 					    b & 0x80 ? fg : bg;
218 					break;
219 				case 32:
220 					*(uint32_t *)(sc->fb_vbase + line + 4*c)
221 					    = (b & 0x80) ? fgc : bgc;
222 					break;
223 				default:
224 					/* panic? */
225 					break;
226 				}
227 			}
228 			line += sc->fb_stride;
229 		}
230 	}
231 }
232 
233 void
234 ofwfb_bitblt_text(struct vt_device *vd, const struct vt_window *vw,
235     const term_rect_t *area)
236 {
237 	unsigned int col, row, x, y;
238 	struct vt_font *vf;
239 	term_char_t c;
240 	term_color_t fg, bg;
241 	const uint8_t *pattern;
242 
243 	vf = vw->vw_font;
244 
245 	for (row = area->tr_begin.tp_row; row < area->tr_end.tp_row; ++row) {
246 		for (col = area->tr_begin.tp_col; col < area->tr_end.tp_col;
247 		    ++col) {
248 			x = col * vf->vf_width +
249 			    vw->vw_draw_area.tr_begin.tp_col;
250 			y = row * vf->vf_height +
251 			    vw->vw_draw_area.tr_begin.tp_row;
252 
253 			c = VTBUF_GET_FIELD(&vw->vw_buf, row, col);
254 			pattern = vtfont_lookup(vf, c);
255 			vt_determine_colors(c,
256 			    VTBUF_ISCURSOR(&vw->vw_buf, row, col), &fg, &bg);
257 
258 			ofwfb_bitblt_bitmap(vd, vw,
259 			    pattern, NULL, vf->vf_width, vf->vf_height,
260 			    x, y, fg, bg);
261 		}
262 	}
263 
264 #ifndef SC_NO_CUTPASTE
265 	if (!vd->vd_mshown)
266 		return;
267 
268 	term_rect_t drawn_area;
269 
270 	drawn_area.tr_begin.tp_col = area->tr_begin.tp_col * vf->vf_width;
271 	drawn_area.tr_begin.tp_row = area->tr_begin.tp_row * vf->vf_height;
272 	drawn_area.tr_end.tp_col = area->tr_end.tp_col * vf->vf_width;
273 	drawn_area.tr_end.tp_row = area->tr_end.tp_row * vf->vf_height;
274 
275 	if (vt_is_cursor_in_area(vd, &drawn_area)) {
276 		ofwfb_bitblt_bitmap(vd, vw,
277 		    vd->vd_mcursor->map, vd->vd_mcursor->mask,
278 		    vd->vd_mcursor->width, vd->vd_mcursor->height,
279 		    vd->vd_mx_drawn + vw->vw_draw_area.tr_begin.tp_col,
280 		    vd->vd_my_drawn + vw->vw_draw_area.tr_begin.tp_row,
281 		    vd->vd_mcursor_fg, vd->vd_mcursor_bg);
282 	}
283 #endif
284 }
285 
286 static void
287 ofwfb_initialize(struct vt_device *vd)
288 {
289 	struct ofwfb_softc *sc = vd->vd_softc;
290 	int i, err;
291 	cell_t retval;
292 	uint32_t oldpix;
293 
294 	sc->fb.fb_cmsize = 16;
295 
296 	if (sc->fb.fb_flags & FB_FLAG_NOWRITE)
297 		return;
298 
299 	/*
300 	 * Set up the color map
301 	 */
302 
303 	sc->iso_palette = 0;
304 	switch (sc->fb.fb_bpp) {
305 	case 8:
306 		vt_generate_cons_palette(sc->fb.fb_cmap, COLOR_FORMAT_RGB, 255,
307 		    16, 255, 8, 255, 0);
308 
309 		for (i = 0; i < 16; i++) {
310 			err = OF_call_method("color!", sc->sc_handle, 4, 1,
311 			    (cell_t)((sc->fb.fb_cmap[i] >> 16) & 0xff),
312 			    (cell_t)((sc->fb.fb_cmap[i] >> 8) & 0xff),
313 			    (cell_t)((sc->fb.fb_cmap[i] >> 0) & 0xff),
314 			    (cell_t)i, &retval);
315 			if (err)
316 				break;
317 		}
318 		if (i != 16)
319 			sc->iso_palette = 1;
320 
321 		break;
322 
323 	case 32:
324 		/*
325 		 * We bypass the usual bus_space_() accessors here, mostly
326 		 * for performance reasons. In particular, we don't want
327 		 * any barrier operations that may be performed and handle
328 		 * endianness slightly different. Figure out the host-view
329 		 * endianness of the frame buffer.
330 		 */
331 		oldpix = bus_space_read_4(sc->sc_memt, sc->fb.fb_vbase, 0);
332 		bus_space_write_4(sc->sc_memt, sc->fb.fb_vbase, 0, 0xff000000);
333 		if (*(uint8_t *)(sc->fb.fb_vbase) == 0xff)
334 			vt_generate_cons_palette(sc->fb.fb_cmap,
335 			    COLOR_FORMAT_RGB, 255, 0, 255, 8, 255, 16);
336 		else
337 			vt_generate_cons_palette(sc->fb.fb_cmap,
338 			    COLOR_FORMAT_RGB, 255, 16, 255, 8, 255, 0);
339 		bus_space_write_4(sc->sc_memt, sc->fb.fb_vbase, 0, oldpix);
340 		break;
341 
342 	default:
343 		panic("Unknown color space depth %d", sc->fb.fb_bpp);
344 		break;
345         }
346 }
347 
348 static int
349 ofwfb_init(struct vt_device *vd)
350 {
351 	struct ofwfb_softc *sc;
352 	char type[64];
353 	phandle_t chosen;
354 	phandle_t node;
355 	uint32_t depth, height, width, stride;
356 	uint32_t fb_phys;
357 	int i, len;
358 #ifdef __sparc64__
359 	static struct bus_space_tag ofwfb_memt[1];
360 	bus_addr_t phys;
361 	int space;
362 #endif
363 
364 	/* Initialize softc */
365 	vd->vd_softc = sc = &ofwfb_conssoftc;
366 
367 	chosen = OF_finddevice("/chosen");
368 	OF_getprop(chosen, "stdout", &sc->sc_handle, sizeof(ihandle_t));
369 	node = OF_instance_to_package(sc->sc_handle);
370 	if (node == -1) {
371 		/*
372 		 * The "/chosen/stdout" does not exist try
373 		 * using "screen" directly.
374 		 */
375 		node = OF_finddevice("screen");
376 		sc->sc_handle = OF_open("screen");
377 	}
378 	OF_getprop(node, "device_type", type, sizeof(type));
379 	if (strcmp(type, "display") != 0)
380 		return (CN_DEAD);
381 
382 	/* Keep track of the OF node */
383 	sc->sc_node = node;
384 
385 	/*
386 	 * Try to use a 32-bit framebuffer if possible. This may be
387 	 * unimplemented and fail. That's fine -- it just means we are
388 	 * stuck with the defaults.
389 	 */
390 	OF_call_method("set-depth", sc->sc_handle, 1, 1, (cell_t)32, &i);
391 
392 	/* Make sure we have needed properties */
393 	if (OF_getproplen(node, "height") != sizeof(height) ||
394 	    OF_getproplen(node, "width") != sizeof(width) ||
395 	    OF_getproplen(node, "depth") != sizeof(depth) ||
396 	    OF_getproplen(node, "linebytes") != sizeof(sc->fb.fb_stride))
397 		return (CN_DEAD);
398 
399 	/* Only support 8 and 32-bit framebuffers */
400 	OF_getprop(node, "depth", &depth, sizeof(depth));
401 	if (depth != 8 && depth != 32)
402 		return (CN_DEAD);
403 	sc->fb.fb_bpp = sc->fb.fb_depth = depth;
404 
405 	OF_getprop(node, "height", &height, sizeof(height));
406 	OF_getprop(node, "width", &width, sizeof(width));
407 	OF_getprop(node, "linebytes", &stride, sizeof(stride));
408 
409 	sc->fb.fb_height = height;
410 	sc->fb.fb_width = width;
411 	sc->fb.fb_stride = stride;
412 	sc->fb.fb_size = sc->fb.fb_height * sc->fb.fb_stride;
413 
414 	/*
415 	 * Grab the physical address of the framebuffer, and then map it
416 	 * into our memory space. If the MMU is not yet up, it will be
417 	 * remapped for us when relocation turns on.
418 	 */
419 	if (OF_getproplen(node, "address") == sizeof(fb_phys)) {
420 	 	/* XXX We assume #address-cells is 1 at this point. */
421 		OF_getprop(node, "address", &fb_phys, sizeof(fb_phys));
422 
423 	#if defined(__powerpc__)
424 		sc->sc_memt = &bs_be_tag;
425 		bus_space_map(sc->sc_memt, fb_phys, sc->fb.fb_size,
426 		    BUS_SPACE_MAP_PREFETCHABLE, &sc->fb.fb_vbase);
427 	#elif defined(__sparc64__)
428 		OF_decode_addr(node, 0, &space, &phys);
429 		sc->sc_memt = &ofwfb_memt[0];
430 		sc->fb.fb_vbase =
431 		    sparc64_fake_bustag(space, fb_phys, sc->sc_memt);
432 	#elif defined(__arm__)
433 		sc->sc_memt = fdtbus_bs_tag;
434 		bus_space_map(sc->sc_memt, sc->fb.fb_pbase, sc->fb.fb_size,
435 		    BUS_SPACE_MAP_PREFETCHABLE,
436 		    (bus_space_handle_t *)&sc->fb.fb_vbase);
437 	#else
438 		#error Unsupported platform!
439 	#endif
440 
441 		sc->fb.fb_pbase = fb_phys;
442 	} else {
443 		/*
444 		 * Some IBM systems don't have an address property. Try to
445 		 * guess the framebuffer region from the assigned addresses.
446 		 * This is ugly, but there doesn't seem to be an alternative.
447 		 * Linux does the same thing.
448 		 */
449 
450 		struct ofw_pci_register pciaddrs[8];
451 		int num_pciaddrs = 0;
452 
453 		/*
454 		 * Get the PCI addresses of the adapter, if present. The node
455 		 * may be the child of the PCI device: in that case, try the
456 		 * parent for the assigned-addresses property.
457 		 */
458 		len = OF_getprop(node, "assigned-addresses", pciaddrs,
459 		    sizeof(pciaddrs));
460 		if (len == -1) {
461 			len = OF_getprop(OF_parent(node), "assigned-addresses",
462 			    pciaddrs, sizeof(pciaddrs));
463 		}
464 		if (len == -1)
465 			len = 0;
466 		num_pciaddrs = len / sizeof(struct ofw_pci_register);
467 
468 		fb_phys = num_pciaddrs;
469 		for (i = 0; i < num_pciaddrs; i++) {
470 			/* If it is too small, not the framebuffer */
471 			if (pciaddrs[i].size_lo < sc->fb.fb_stride * height)
472 				continue;
473 			/* If it is not memory, it isn't either */
474 			if (!(pciaddrs[i].phys_hi &
475 			    OFW_PCI_PHYS_HI_SPACE_MEM32))
476 				continue;
477 
478 			/* This could be the framebuffer */
479 			fb_phys = i;
480 
481 			/* If it is prefetchable, it certainly is */
482 			if (pciaddrs[i].phys_hi & OFW_PCI_PHYS_HI_PREFETCHABLE)
483 				break;
484 		}
485 
486 		if (fb_phys == num_pciaddrs) /* No candidates found */
487 			return (CN_DEAD);
488 
489 	#if defined(__powerpc__)
490 		OF_decode_addr(node, fb_phys, &sc->sc_memt, &sc->fb.fb_vbase,
491 		    NULL);
492 		sc->fb.fb_pbase = sc->fb.fb_vbase; /* 1:1 mapped */
493 		#ifdef __powerpc64__
494 		/* Real mode under a hypervisor probably doesn't cover FB */
495 		if (!(mfmsr() & (PSL_HV | PSL_DR)))
496 			sc->fb.fb_flags |= FB_FLAG_NOWRITE;
497 		#endif
498 	#else
499 		/* No ability to interpret assigned-addresses otherwise */
500 		return (CN_DEAD);
501 	#endif
502         }
503 
504 
505 	ofwfb_initialize(vd);
506 	vt_fb_init(vd);
507 
508 	return (CN_INTERNAL);
509 }
510 
511