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