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