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