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