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