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