xref: /freebsd/sys/dev/vt/hw/ofwfb/ofwfb.c (revision e9ac41698b2f322d55ccf9da50a3596edb2c1800)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
30 #include <sys/kernel.h>
31 #include <sys/systm.h>
32 #include <sys/fbio.h>
33 
34 #include <dev/vt/vt.h>
35 #include <dev/vt/hw/fb/vt_fb.h>
36 #include <dev/vt/colors/vt_termcolors.h>
37 
38 #include <vm/vm.h>
39 #include <vm/pmap.h>
40 
41 #include <machine/bus.h>
42 #include <machine/cpu.h>
43 
44 #include <dev/ofw/openfirm.h>
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/ofw_pci.h>
47 #include <dev/ofw/ofw_subr.h>
48 
49 struct ofwfb_softc {
50 	struct fb_info	fb;
51 
52 	phandle_t	sc_node;
53 	ihandle_t	sc_handle;
54 	bus_space_tag_t	sc_memt;
55 	int		iso_palette;
56 	int		argb;
57 	int		endian_flip;
58 	uint32_t	vendor_id;
59 };
60 
61 #define PCI_VID_NVIDIA	0x10de	/* NVIDIA Corporation */
62 #define PCI_VID_ASPEED	0x1a03	/* ASPEED Technology, Inc. */
63 
64 static void ofwfb_initialize(struct vt_device *vd);
65 static vd_probe_t	ofwfb_probe;
66 static vd_init_t	ofwfb_init;
67 static vd_bitblt_text_t	ofwfb_bitblt_text;
68 static vd_bitblt_bmp_t	ofwfb_bitblt_bitmap;
69 static vd_bitblt_argb_t	ofwfb_bitblt_argb;
70 
71 static const struct vt_driver vt_ofwfb_driver = {
72 	.vd_name	= "ofwfb",
73 	.vd_probe	= ofwfb_probe,
74 	.vd_init	= ofwfb_init,
75 	.vd_blank	= vt_fb_blank,
76 	.vd_bitblt_text	= ofwfb_bitblt_text,
77 	.vd_bitblt_bmp	= ofwfb_bitblt_bitmap,
78 	.vd_bitblt_argb	= ofwfb_bitblt_argb,
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 static int
248 ofwfb_bitblt_argb(struct vt_device *vd, const struct vt_window *vw,
249     const uint8_t *argb,
250     unsigned int width, unsigned int height,
251     unsigned int x, unsigned int y)
252 {
253 
254 	return (EOPNOTSUPP);
255 }
256 
257 void
258 ofwfb_bitblt_text(struct vt_device *vd, const struct vt_window *vw,
259     const term_rect_t *area)
260 {
261 	unsigned int col, row, x, y;
262 	struct vt_font *vf;
263 	term_char_t c;
264 	term_color_t fg, bg;
265 	const uint8_t *pattern;
266 
267 	vf = vw->vw_font;
268 
269 	for (row = area->tr_begin.tp_row; row < area->tr_end.tp_row; ++row) {
270 		for (col = area->tr_begin.tp_col; col < area->tr_end.tp_col;
271 		    ++col) {
272 			x = col * vf->vf_width +
273 			    vw->vw_draw_area.tr_begin.tp_col;
274 			y = row * vf->vf_height +
275 			    vw->vw_draw_area.tr_begin.tp_row;
276 
277 			c = VTBUF_GET_FIELD(&vw->vw_buf, row, col);
278 			pattern = vtfont_lookup(vf, c);
279 			vt_determine_colors(c,
280 			    VTBUF_ISCURSOR(&vw->vw_buf, row, col), &fg, &bg);
281 
282 			ofwfb_bitblt_bitmap(vd, vw,
283 			    pattern, NULL, vf->vf_width, vf->vf_height,
284 			    x, y, fg, bg);
285 		}
286 	}
287 
288 #ifndef SC_NO_CUTPASTE
289 	if (!vd->vd_mshown)
290 		return;
291 
292 	term_rect_t drawn_area;
293 
294 	drawn_area.tr_begin.tp_col = area->tr_begin.tp_col * vf->vf_width;
295 	drawn_area.tr_begin.tp_row = area->tr_begin.tp_row * vf->vf_height;
296 	drawn_area.tr_end.tp_col = area->tr_end.tp_col * vf->vf_width;
297 	drawn_area.tr_end.tp_row = area->tr_end.tp_row * vf->vf_height;
298 
299 	if (vt_is_cursor_in_area(vd, &drawn_area)) {
300 		ofwfb_bitblt_bitmap(vd, vw,
301 		    vd->vd_mcursor->map, vd->vd_mcursor->mask,
302 		    vd->vd_mcursor->width, vd->vd_mcursor->height,
303 		    vd->vd_mx_drawn + vw->vw_draw_area.tr_begin.tp_col,
304 		    vd->vd_my_drawn + vw->vw_draw_area.tr_begin.tp_row,
305 		    vd->vd_mcursor_fg, vd->vd_mcursor_bg);
306 	}
307 #endif
308 }
309 
310 
311 /*
312  * Decode OpenFirmware/IEEE 1275-1994 "ranges" property
313  *
314  * XXX: this is similar to ofw_pcib_fill_ranges but cannot use it here because
315  *      it's not possible to allocate memory at the moment this is funcion is
316  *      used. Since there are other similar functions dealing with "ranges"
317  *      property, a proper refactoring is suggested.
318  */
319 static uint64_t
320 decode_pci_ranges_host_addr(phandle_t pcinode)
321 {
322 	struct simplebus_range {
323 		uint64_t bus;
324 		uint64_t host;
325 		uint64_t size;
326 	};
327 
328 	struct simplebus_range ranges[4];
329 	int nranges, host_address_cells;
330 	pcell_t acells, scells;
331 	cell_t base_ranges[64];
332 
333 	ssize_t nbase_ranges;
334 	int i, j, k;
335 
336 	if (!OF_hasprop(pcinode, "ranges"))
337 		return (0);
338 
339 	if (OF_getencprop(pcinode, "#address-cells", &acells, sizeof(acells)) !=
340 	    sizeof(acells))
341 		return (0);
342 
343 	if (OF_getencprop(pcinode, "#size-cells", &scells, sizeof(scells)) !=
344 		sizeof(scells))
345 		return (0);
346 
347 	if (OF_searchencprop(OF_parent(pcinode), "#address-cells",
348 		&host_address_cells, sizeof(host_address_cells)) !=
349 		sizeof(host_address_cells))
350 		return (0);
351 
352 	nbase_ranges = OF_getproplen(pcinode, "ranges");
353 	nranges = nbase_ranges / sizeof(cell_t) / (acells + host_address_cells + scells);
354 
355 	/* prevent buffer overflow during iteration */
356 	if (nranges > sizeof(ranges) / sizeof(ranges[0]))
357 		nranges = sizeof(ranges) / sizeof(ranges[0]);
358 
359 	/* decode range value and return the first valid address */
360 	OF_getencprop(pcinode, "ranges", base_ranges, nbase_ranges);
361 	for (i = 0, j = 0; i < nranges; i++) {
362 		ranges[i].bus = 0;
363 		for (k = 0; k < acells; k++) {
364 			ranges[i].bus <<= 32;
365 			ranges[i].bus |= base_ranges[j++];
366 		}
367 
368 		ranges[i].host = 0;
369 		for (k = 0; k < host_address_cells; k++) {
370 			ranges[i].host <<= 32;
371 			ranges[i].host |= base_ranges[j++];
372 		}
373 		ranges[i].size = 0;
374 		for (k = 0; k < scells; k++) {
375 			ranges[i].size <<= 32;
376 			ranges[i].size |= base_ranges[j++];
377 		}
378 
379 		if (ranges[i].host != 0)
380 			return (ranges[i].host);
381 	}
382 
383 	return (0);
384 }
385 
386 static bus_addr_t
387 find_pci_host_address(phandle_t node)
388 {
389 	uint64_t addr;
390 
391 	/*
392 	 * According to IEEE STD 1275, if property "ranges" exists but has a
393 	 * zero-length property value, the child address space is identical
394 	 * to the parent address space.
395 	 */
396 	while (node) {
397 		if (OF_hasprop(node, "ranges")) {
398 			addr = decode_pci_ranges_host_addr(node);
399 			if (addr != 0)
400 				return ((bus_addr_t)addr);
401 		}
402 		node = OF_parent(node);
403 	}
404 
405 	return (0);
406 }
407 
408 static void
409 ofwfb_initialize(struct vt_device *vd)
410 {
411 	struct ofwfb_softc *sc = vd->vd_softc;
412 	int i, err, r, g, b;
413 	cell_t retval;
414 
415 	sc->fb.fb_cmsize = 16;
416 
417 	if (sc->fb.fb_flags & FB_FLAG_NOWRITE)
418 		return;
419 
420 	/*
421 	 * Set up the color map
422 	 */
423 
424 	sc->iso_palette = 0;
425 	switch (sc->fb.fb_bpp) {
426 	case 8:
427 		/*
428 		 * No color format issues here, since we are passing the RGB
429 		 * components separately to Open Firmware.
430 		 */
431 		vt_config_cons_colors(&sc->fb, COLOR_FORMAT_RGB, 255,
432 		    16, 255, 8, 255, 0);
433 
434 		for (i = 0; i < 16; i++) {
435 			err = OF_call_method("color!", sc->sc_handle, 4, 1,
436 			    (cell_t)((sc->fb.fb_cmap[i] >> 16) & 0xff),
437 			    (cell_t)((sc->fb.fb_cmap[i] >> 8) & 0xff),
438 			    (cell_t)((sc->fb.fb_cmap[i] >> 0) & 0xff),
439 			    (cell_t)i, &retval);
440 			if (err)
441 				break;
442 		}
443 		if (i != 16)
444 			sc->iso_palette = 1;
445 
446 		break;
447 
448 	case 32:
449 		/*
450 		 * There are two main color formats in use.
451 		 * ARGB32 is used mainly on hardware that was designed for
452 		 * LE systems, and RGBA32 is used mainly on hardware designed
453 		 * for BE systems.
454 		 *
455 		 * PowerMacs use either, depending on the video card option.
456 		 * NVidia cards tend to be RGBA32, and ATI cards tend to be ARGB32.
457 		 *
458 		 * There is no good way to determine the correct option, as this
459 		 * is independent of endian swapping.
460 		 */
461 		if (sc->vendor_id == PCI_VID_NVIDIA)
462 			sc->argb = 0;
463 		else
464 			sc->argb = 1;
465 
466 		TUNABLE_INT_FETCH("hw.ofwfb.argb32_pixel", &sc->argb);
467 		if (sc->endian_flip) {
468 			if (sc->argb)
469 				r = 8, g = 16, b = 24;
470 			else
471 				r = 24, g = 16, b = 8;
472 		} else {
473 			if (sc->argb)
474 				r = 16, g = 8, b = 0;
475 			else
476 				r = 0, g = 8, b = 16;
477 		}
478 		vt_config_cons_colors(&sc->fb,
479 		    COLOR_FORMAT_RGB, 255, r, 255, g, 255, b);
480 		break;
481 
482 	default:
483 		panic("Unknown color space depth %d", sc->fb.fb_bpp);
484 		break;
485         }
486 }
487 
488 static int
489 ofwfb_init(struct vt_device *vd)
490 {
491 	struct ofwfb_softc *sc;
492 	char buf[64];
493 	phandle_t chosen;
494 	phandle_t node;
495 	pcell_t depth, height, width, stride;
496 	uint32_t vendor_id = 0;
497 	cell_t adr[2];
498 	uint64_t user_phys;
499 	bus_addr_t fb_phys;
500 	bus_size_t fb_phys_size;
501 	int i, j, len;
502 
503 	/* Initialize softc */
504 	vd->vd_softc = sc = &ofwfb_conssoftc;
505 
506 	node = -1;
507 	chosen = OF_finddevice("/chosen");
508 	if (OF_getencprop(chosen, "stdout", &sc->sc_handle,
509 	    sizeof(ihandle_t)) == sizeof(ihandle_t))
510 		node = OF_instance_to_package(sc->sc_handle);
511 	if (node == -1)
512 		/* Try "/chosen/stdout-path" now */
513 		if (OF_getprop(chosen, "stdout-path", buf, sizeof(buf)) > 0) {
514 			node = OF_finddevice(buf);
515 			if (node != -1)
516 				sc->sc_handle = OF_open(buf);
517 		}
518 	if (node == -1) {
519 		/*
520 		 * The "/chosen/stdout" does not exist try
521 		 * using "screen" directly.
522 		 */
523 		node = OF_finddevice("screen");
524 		sc->sc_handle = OF_open("screen");
525 	}
526 	OF_getprop(node, "device_type", buf, sizeof(buf));
527 	if (strcmp(buf, "display") != 0)
528 		return (CN_DEAD);
529 
530 	/*
531 	 * Retrieve vendor-id from /chosen parent node, usually pointing to
532 	 * video card device. This is used to select pixel format later on
533 	 * ofwfb_initialize()
534 	 */
535 	if (OF_getencprop(OF_parent(node), "vendor-id", &vendor_id,
536 	    sizeof(vendor_id)) == sizeof(vendor_id))
537 		sc->vendor_id = vendor_id;
538 
539 	/* Keep track of the OF node */
540 	sc->sc_node = node;
541 
542 	/*
543 	 * Try to use a 32-bit framebuffer if possible. This may be
544 	 * unimplemented and fail. That's fine -- it just means we are
545 	 * stuck with the defaults.
546 	 */
547 	OF_call_method("set-depth", sc->sc_handle, 1, 1, (cell_t)32, &i);
548 
549 	/* Make sure we have needed properties */
550 	if (OF_getproplen(node, "height") != sizeof(height) ||
551 	    OF_getproplen(node, "width") != sizeof(width) ||
552 	    OF_getproplen(node, "depth") != sizeof(depth))
553 		return (CN_DEAD);
554 
555 	/* Only support 8 and 32-bit framebuffers */
556 	OF_getencprop(node, "depth", &depth, sizeof(depth));
557 	if (depth != 8 && depth != 32)
558 		return (CN_DEAD);
559 	sc->fb.fb_bpp = sc->fb.fb_depth = depth;
560 
561 	OF_getencprop(node, "height", &height, sizeof(height));
562 	OF_getencprop(node, "width", &width, sizeof(width));
563 	if (OF_getencprop(node, "linebytes", &stride, sizeof(stride)) !=
564 	    sizeof(stride))
565 		stride = width*depth/8;
566 
567 
568 	sc->fb.fb_height = height;
569 	sc->fb.fb_width = width;
570 	sc->fb.fb_stride = stride;
571 	sc->fb.fb_size = sc->fb.fb_height * sc->fb.fb_stride;
572 	sc->endian_flip = 0;
573 
574 #if defined(__powerpc__)
575 	if (OF_hasprop(node, "little-endian")) {
576 		sc->sc_memt = &bs_le_tag;
577 #if BYTE_ORDER == BIG_ENDIAN
578 		sc->endian_flip = 1;
579 #endif
580         } else if (OF_hasprop(node, "big-endian")) {
581 		sc->sc_memt = &bs_be_tag;
582 #if BYTE_ORDER == LITTLE_ENDIAN
583 		sc->endian_flip = 1;
584 #endif
585 	}
586 	else {
587 		/* Assume the framebuffer is in native endian. */
588 #if BYTE_ORDER == BIG_ENDIAN
589 		sc->sc_memt = &bs_be_tag;
590 #else
591 		sc->sc_memt = &bs_le_tag;
592 #endif
593 	}
594 #elif defined(__arm__)
595 	sc->sc_memt = fdtbus_bs_tag;
596 #else
597 	#error Unsupported platform!
598 #endif
599 
600 
601 	/*
602 	 * Grab the physical address of the framebuffer, and then map it
603 	 * into our memory space. If the MMU is not yet up, it will be
604 	 * remapped for us when relocation turns on.
605 	 *
606 	 * The ASPEED driver on recent petitboot versions doesn't expose the
607 	 * physical address of framebuffer anymore for security. So it should
608 	 * retrieve the address from PCI device properties.
609 	 */
610 	user_phys = 0;
611 	TUNABLE_UINT64_FETCH("hw.ofwfb.physaddr", &user_phys);
612 
613 	if (user_phys)
614 		sc->fb.fb_pbase = (vm_paddr_t)user_phys;
615 	else if (sc->vendor_id == PCI_VID_ASPEED)
616 		sc->fb.fb_pbase = find_pci_host_address(node);
617 	else if (OF_hasprop(node, "address")) {
618 
619 		switch (OF_getproplen(node, "address")) {
620 		case 4:
621 			OF_getencprop(node, "address", adr, 4);
622 			fb_phys = adr[0];
623 			break;
624 		case 8:
625 			OF_getencprop(node, "address", adr, 8);
626 			fb_phys = ((uint64_t)adr[0] << 32) | adr[1];
627 			break;
628 		default:
629 			/* Bad property? */
630 			return (CN_DEAD);
631 		}
632 
633 		sc->fb.fb_pbase = (vm_paddr_t)fb_phys;
634 	} else {
635 #if defined(__powerpc__)
636 		/*
637 		 * Some IBM systems don't have an address property. Try to
638 		 * guess the framebuffer region from the assigned addresses.
639 		 * This is ugly, but there doesn't seem to be an alternative.
640 		 * Linux does the same thing.
641 		 */
642 
643 		struct ofw_pci_register pciaddrs[8];
644 		int num_pciaddrs = 0;
645 
646 		/*
647 		 * Get the PCI addresses of the adapter, if present. The node
648 		 * may be the child of the PCI device: in that case, try the
649 		 * parent for the assigned-addresses property.
650 		 */
651 		len = OF_getencprop(node, "assigned-addresses",
652 		    (pcell_t *)pciaddrs, sizeof(pciaddrs));
653 		if (len == -1) {
654 			len = OF_getencprop(OF_parent(node), "assigned-addresses",
655 			    (pcell_t *)pciaddrs, sizeof(pciaddrs));
656 		}
657 		if (len == -1)
658 			len = 0;
659 		num_pciaddrs = len / sizeof(struct ofw_pci_register);
660 
661 		j = num_pciaddrs;
662 		for (i = 0; i < num_pciaddrs; i++) {
663 			/* If it is too small, not the framebuffer */
664 			if (pciaddrs[i].size_lo < sc->fb.fb_stride * height)
665 				continue;
666 			/* If it is not memory, it isn't either */
667 			if (!(pciaddrs[i].phys_hi &
668 			    OFW_PCI_PHYS_HI_SPACE_MEM32))
669 				continue;
670 
671 			/* This could be the framebuffer */
672 			j = i;
673 
674 			/* If it is prefetchable, it certainly is */
675 			if (pciaddrs[i].phys_hi & OFW_PCI_PHYS_HI_PREFETCHABLE)
676 				break;
677 		}
678 
679 		if (j == num_pciaddrs) /* No candidates found */
680 			return (CN_DEAD);
681 
682 		if (ofw_reg_to_paddr(node, j, &fb_phys, &fb_phys_size, NULL) < 0)
683 			return (CN_DEAD);
684 
685 		sc->fb.fb_pbase = (vm_paddr_t)fb_phys;
686 #else
687 		/* No ability to interpret assigned-addresses otherwise */
688 		return (CN_DEAD);
689 #endif
690         }
691 
692 	if (!sc->fb.fb_pbase)
693 		return (CN_DEAD);
694 
695 	bus_space_map(sc->sc_memt, sc->fb.fb_pbase, sc->fb.fb_size,
696 	    BUS_SPACE_MAP_PREFETCHABLE,
697 	    (bus_space_handle_t *)&sc->fb.fb_vbase);
698 
699 	#if defined(__powerpc__)
700 	/*
701 	 * If we are running on PowerPC in real mode (supported only on AIM
702 	 * CPUs), the frame buffer may be inaccessible (real mode does not
703 	 * necessarily cover all RAM) and may also be mapped with the wrong
704 	 * cache properties (all real mode accesses are assumed cacheable).
705 	 * Just don't write to it for the time being.
706 	 */
707 	if (!(cpu_features & PPC_FEATURE_BOOKE) && !(mfmsr() & PSL_DR))
708 		sc->fb.fb_flags |= FB_FLAG_NOWRITE;
709 	#endif
710 	ofwfb_initialize(vd);
711 	vt_fb_init(vd);
712 
713 	return (CN_INTERNAL);
714 }
715