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