#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/fbio.h>
#include <dev/vt/vt.h>
#include <dev/vt/hw/fb/vt_fb.h>
#include <dev/vt/colors/vt_termcolors.h>
#include <vm/vm.h>
#include <vm/pmap.h>
#include <machine/bus.h>
#include <machine/cpu.h>
#include <dev/ofw/openfirm.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_pci.h>
#include <dev/ofw/ofw_subr.h>
struct ofwfb_softc {
struct fb_info fb;
phandle_t sc_node;
ihandle_t sc_handle;
bus_space_tag_t sc_memt;
int iso_palette;
int argb;
int endian_flip;
uint32_t vendor_id;
};
#define PCI_VID_NVIDIA 0x10de
#define PCI_VID_ASPEED 0x1a03
static void ofwfb_initialize(struct vt_device *vd);
static vd_probe_t ofwfb_probe;
static vd_init_t ofwfb_init;
static vd_bitblt_text_t ofwfb_bitblt_text;
static vd_bitblt_bmp_t ofwfb_bitblt_bitmap;
static vd_bitblt_argb_t ofwfb_bitblt_argb;
static const struct vt_driver vt_ofwfb_driver = {
.vd_name = "ofwfb",
.vd_probe = ofwfb_probe,
.vd_init = ofwfb_init,
.vd_blank = vt_fb_blank,
.vd_bitblt_text = ofwfb_bitblt_text,
.vd_bitblt_bmp = ofwfb_bitblt_bitmap,
.vd_bitblt_argb = ofwfb_bitblt_argb,
.vd_fb_ioctl = vt_fb_ioctl,
.vd_fb_mmap = vt_fb_mmap,
.vd_priority = VD_PRIORITY_GENERIC+1,
};
static unsigned char ofw_colors[16] = {
0, 4, 2, 6, 1, 5, 3, 7,
8, 12, 10, 14, 9, 13, 11, 15
};
static struct ofwfb_softc ofwfb_conssoftc;
VT_DRIVER_DECLARE(vt_ofwfb, vt_ofwfb_driver);
static int
ofwfb_probe(struct vt_device *vd)
{
int disabled;
phandle_t chosen, node;
ihandle_t stdout;
char buf[64];
disabled = 0;
TUNABLE_INT_FETCH("hw.ofwfb.disable", &disabled);
if (disabled)
return (CN_DEAD);
chosen = OF_finddevice("/chosen");
if (chosen == -1)
return (CN_DEAD);
node = -1;
if (OF_getencprop(chosen, "stdout", &stdout, sizeof(stdout)) ==
sizeof(stdout))
node = OF_instance_to_package(stdout);
if (node == -1)
if (OF_getprop(chosen, "stdout-path", buf, sizeof(buf)) > 0)
node = OF_finddevice(buf);
if (node == -1) {
node = OF_finddevice("screen");
}
OF_getprop(node, "device_type", buf, sizeof(buf));
if (strcmp(buf, "display") != 0)
return (CN_DEAD);
return (CN_INTERNAL);
}
static void
ofwfb_bitblt_bitmap(struct vt_device *vd, const struct vt_window *vw,
const uint8_t *pattern, const uint8_t *mask,
unsigned int width, unsigned int height,
unsigned int x, unsigned int y, term_color_t fg, term_color_t bg)
{
struct fb_info *sc = vd->vd_softc;
u_long line;
uint32_t fgc, bgc;
int c, l;
uint8_t b, m;
union {
uint32_t l;
uint8_t c[4];
} ch1, ch2;
#ifdef __powerpc__
if (sc->fb_flags & FB_FLAG_NOWRITE) {
if (pmap_bootstrapped) {
sc->fb_flags &= ~FB_FLAG_NOWRITE;
ofwfb_initialize(vd);
vd->vd_driver->vd_blank(vd, TC_BLACK);
} else {
return;
}
}
#endif
fgc = sc->fb_cmap[fg];
bgc = sc->fb_cmap[bg];
b = m = 0;
if (((struct ofwfb_softc *)vd->vd_softc)->iso_palette) {
fg = ofw_colors[fg];
bg = ofw_colors[bg];
}
line = (sc->fb_stride * y) + x * sc->fb_bpp/8;
if (mask == NULL && sc->fb_bpp == 8 && (width % 8 == 0)) {
if (((x + width) > vd->vd_width) || ((y + height) >
vd->vd_height))
return;
for (; height > 0; height--) {
for (c = 0; c < width; c += 8) {
b = *pattern++;
ch1.l = ch2.l = (bg << 24) | (bg << 16) |
(bg << 8) | bg;
if (b & 0x80) ch1.c[0] = fg;
if (b & 0x40) ch1.c[1] = fg;
if (b & 0x20) ch1.c[2] = fg;
if (b & 0x10) ch1.c[3] = fg;
if (b & 0x08) ch2.c[0] = fg;
if (b & 0x04) ch2.c[1] = fg;
if (b & 0x02) ch2.c[2] = fg;
if (b & 0x01) ch2.c[3] = fg;
*(uint32_t *)(sc->fb_vbase + line + c) = ch1.l;
*(uint32_t *)(sc->fb_vbase + line + c + 4) =
ch2.l;
}
line += sc->fb_stride;
}
} else {
for (l = 0;
l < height && y + l < vw->vw_draw_area.tr_end.tp_row;
l++) {
for (c = 0;
c < width && x + c < vw->vw_draw_area.tr_end.tp_col;
c++) {
if (c % 8 == 0)
b = *pattern++;
else
b <<= 1;
if (mask != NULL) {
if (c % 8 == 0)
m = *mask++;
else
m <<= 1;
if ((m & 0x80) == 0)
continue;
}
switch(sc->fb_bpp) {
case 8:
*(uint8_t *)(sc->fb_vbase + line + c) =
b & 0x80 ? fg : bg;
break;
case 32:
*(uint32_t *)(sc->fb_vbase + line + 4*c)
= (b & 0x80) ? fgc : bgc;
break;
default:
break;
}
}
line += sc->fb_stride;
}
}
}
static int
ofwfb_bitblt_argb(struct vt_device *vd, const struct vt_window *vw,
const uint8_t *argb,
unsigned int width, unsigned int height,
unsigned int x, unsigned int y)
{
return (EOPNOTSUPP);
}
void
ofwfb_bitblt_text(struct vt_device *vd, const struct vt_window *vw,
const term_rect_t *area)
{
unsigned int col, row, x, y;
struct vt_font *vf;
term_char_t c;
term_color_t fg, bg;
const uint8_t *pattern;
vf = vw->vw_font;
for (row = area->tr_begin.tp_row; row < area->tr_end.tp_row; ++row) {
for (col = area->tr_begin.tp_col; col < area->tr_end.tp_col;
++col) {
x = col * vf->vf_width +
vw->vw_draw_area.tr_begin.tp_col;
y = row * vf->vf_height +
vw->vw_draw_area.tr_begin.tp_row;
c = VTBUF_GET_FIELD(&vw->vw_buf, row, col);
pattern = vtfont_lookup(vf, c);
vt_determine_colors(c,
VTBUF_ISCURSOR(&vw->vw_buf, row, col), &fg, &bg);
ofwfb_bitblt_bitmap(vd, vw,
pattern, NULL, vf->vf_width, vf->vf_height,
x, y, fg, bg);
}
}
#ifndef SC_NO_CUTPASTE
if (!vd->vd_mshown)
return;
term_rect_t drawn_area;
drawn_area.tr_begin.tp_col = area->tr_begin.tp_col * vf->vf_width;
drawn_area.tr_begin.tp_row = area->tr_begin.tp_row * vf->vf_height;
drawn_area.tr_end.tp_col = area->tr_end.tp_col * vf->vf_width;
drawn_area.tr_end.tp_row = area->tr_end.tp_row * vf->vf_height;
if (vt_is_cursor_in_area(vd, &drawn_area)) {
ofwfb_bitblt_bitmap(vd, vw,
vd->vd_mcursor->map, vd->vd_mcursor->mask,
vd->vd_mcursor->width, vd->vd_mcursor->height,
vd->vd_mx_drawn + vw->vw_draw_area.tr_begin.tp_col,
vd->vd_my_drawn + vw->vw_draw_area.tr_begin.tp_row,
vd->vd_mcursor_fg, vd->vd_mcursor_bg);
}
#endif
}
static uint64_t
decode_pci_ranges_host_addr(phandle_t pcinode)
{
struct simplebus_range {
uint64_t bus;
uint64_t host;
uint64_t size;
};
struct simplebus_range ranges[4];
int nranges, host_address_cells;
pcell_t acells, scells;
cell_t base_ranges[64];
ssize_t nbase_ranges;
int i, j, k;
if (!OF_hasprop(pcinode, "ranges"))
return (0);
if (OF_getencprop(pcinode, "#address-cells", &acells, sizeof(acells)) !=
sizeof(acells))
return (0);
if (OF_getencprop(pcinode, "#size-cells", &scells, sizeof(scells)) !=
sizeof(scells))
return (0);
if (OF_searchencprop(OF_parent(pcinode), "#address-cells",
&host_address_cells, sizeof(host_address_cells)) !=
sizeof(host_address_cells))
return (0);
nbase_ranges = OF_getproplen(pcinode, "ranges");
nranges = nbase_ranges / sizeof(cell_t) / (acells + host_address_cells + scells);
if (nranges > sizeof(ranges) / sizeof(ranges[0]))
nranges = sizeof(ranges) / sizeof(ranges[0]);
OF_getencprop(pcinode, "ranges", base_ranges, nbase_ranges);
for (i = 0, j = 0; i < nranges; i++) {
ranges[i].bus = 0;
for (k = 0; k < acells; k++) {
ranges[i].bus <<= 32;
ranges[i].bus |= base_ranges[j++];
}
ranges[i].host = 0;
for (k = 0; k < host_address_cells; k++) {
ranges[i].host <<= 32;
ranges[i].host |= base_ranges[j++];
}
ranges[i].size = 0;
for (k = 0; k < scells; k++) {
ranges[i].size <<= 32;
ranges[i].size |= base_ranges[j++];
}
if (ranges[i].host != 0)
return (ranges[i].host);
}
return (0);
}
static bus_addr_t
find_pci_host_address(phandle_t node)
{
uint64_t addr;
while (node) {
if (OF_hasprop(node, "ranges")) {
addr = decode_pci_ranges_host_addr(node);
if (addr != 0)
return ((bus_addr_t)addr);
}
node = OF_parent(node);
}
return (0);
}
static void
ofwfb_initialize(struct vt_device *vd)
{
struct ofwfb_softc *sc = vd->vd_softc;
int i, err, r, g, b;
cell_t retval;
sc->fb.fb_cmsize = 16;
if (sc->fb.fb_flags & FB_FLAG_NOWRITE)
return;
sc->iso_palette = 0;
switch (sc->fb.fb_bpp) {
case 8:
vt_config_cons_colors(&sc->fb, COLOR_FORMAT_RGB, 255,
16, 255, 8, 255, 0);
for (i = 0; i < 16; i++) {
err = OF_call_method("color!", sc->sc_handle, 4, 1,
(cell_t)((sc->fb.fb_cmap[i] >> 16) & 0xff),
(cell_t)((sc->fb.fb_cmap[i] >> 8) & 0xff),
(cell_t)((sc->fb.fb_cmap[i] >> 0) & 0xff),
(cell_t)i, &retval);
if (err)
break;
}
if (i != 16)
sc->iso_palette = 1;
break;
case 32:
if (sc->vendor_id == PCI_VID_NVIDIA)
sc->argb = 0;
else
sc->argb = 1;
TUNABLE_INT_FETCH("hw.ofwfb.argb32_pixel", &sc->argb);
if (sc->endian_flip) {
if (sc->argb)
r = 8, g = 16, b = 24;
else
r = 24, g = 16, b = 8;
} else {
if (sc->argb)
r = 16, g = 8, b = 0;
else
r = 0, g = 8, b = 16;
}
vt_config_cons_colors(&sc->fb,
COLOR_FORMAT_RGB, 255, r, 255, g, 255, b);
break;
default:
panic("Unknown color space depth %d", sc->fb.fb_bpp);
break;
}
}
static int
ofwfb_init(struct vt_device *vd)
{
struct ofwfb_softc *sc;
char buf[64];
phandle_t chosen;
phandle_t node;
pcell_t depth, height, width, stride;
uint32_t vendor_id = 0;
cell_t adr[2];
uint64_t user_phys;
bus_addr_t fb_phys;
bus_size_t fb_phys_size;
int i, j, len;
vd->vd_softc = sc = &ofwfb_conssoftc;
node = -1;
chosen = OF_finddevice("/chosen");
if (OF_getencprop(chosen, "stdout", &sc->sc_handle,
sizeof(ihandle_t)) == sizeof(ihandle_t))
node = OF_instance_to_package(sc->sc_handle);
if (node == -1)
if (OF_getprop(chosen, "stdout-path", buf, sizeof(buf)) > 0) {
node = OF_finddevice(buf);
if (node != -1)
sc->sc_handle = OF_open(buf);
}
if (node == -1) {
node = OF_finddevice("screen");
sc->sc_handle = OF_open("screen");
}
OF_getprop(node, "device_type", buf, sizeof(buf));
if (strcmp(buf, "display") != 0)
return (CN_DEAD);
if (OF_getencprop(OF_parent(node), "vendor-id", &vendor_id,
sizeof(vendor_id)) == sizeof(vendor_id))
sc->vendor_id = vendor_id;
sc->sc_node = node;
OF_call_method("set-depth", sc->sc_handle, 1, 1, (cell_t)32, &i);
if (OF_getproplen(node, "height") != sizeof(height) ||
OF_getproplen(node, "width") != sizeof(width) ||
OF_getproplen(node, "depth") != sizeof(depth))
return (CN_DEAD);
OF_getencprop(node, "depth", &depth, sizeof(depth));
if (depth != 8 && depth != 32)
return (CN_DEAD);
sc->fb.fb_bpp = sc->fb.fb_depth = depth;
OF_getencprop(node, "height", &height, sizeof(height));
OF_getencprop(node, "width", &width, sizeof(width));
if (OF_getencprop(node, "linebytes", &stride, sizeof(stride)) !=
sizeof(stride))
stride = width*depth/8;
sc->fb.fb_height = height;
sc->fb.fb_width = width;
sc->fb.fb_stride = stride;
sc->fb.fb_size = sc->fb.fb_height * sc->fb.fb_stride;
sc->endian_flip = 0;
#if defined(__powerpc__)
if (OF_hasprop(node, "little-endian")) {
sc->sc_memt = &bs_le_tag;
#if BYTE_ORDER == BIG_ENDIAN
sc->endian_flip = 1;
#endif
} else if (OF_hasprop(node, "big-endian")) {
sc->sc_memt = &bs_be_tag;
#if BYTE_ORDER == LITTLE_ENDIAN
sc->endian_flip = 1;
#endif
}
else {
#if BYTE_ORDER == BIG_ENDIAN
sc->sc_memt = &bs_be_tag;
#else
sc->sc_memt = &bs_le_tag;
#endif
}
#elif defined(__arm__)
sc->sc_memt = fdtbus_bs_tag;
#else
#error Unsupported platform!
#endif
user_phys = 0;
TUNABLE_UINT64_FETCH("hw.ofwfb.physaddr", &user_phys);
if (user_phys)
sc->fb.fb_pbase = (vm_paddr_t)user_phys;
else if (sc->vendor_id == PCI_VID_ASPEED)
sc->fb.fb_pbase = find_pci_host_address(node);
else if (OF_hasprop(node, "address")) {
switch (OF_getproplen(node, "address")) {
case 4:
OF_getencprop(node, "address", adr, 4);
fb_phys = adr[0];
break;
case 8:
OF_getencprop(node, "address", adr, 8);
fb_phys = ((uint64_t)adr[0] << 32) | adr[1];
break;
default:
return (CN_DEAD);
}
sc->fb.fb_pbase = (vm_paddr_t)fb_phys;
} else {
#if defined(__powerpc__)
struct ofw_pci_register pciaddrs[8];
int num_pciaddrs = 0;
len = OF_getencprop(node, "assigned-addresses",
(pcell_t *)pciaddrs, sizeof(pciaddrs));
if (len == -1) {
len = OF_getencprop(OF_parent(node), "assigned-addresses",
(pcell_t *)pciaddrs, sizeof(pciaddrs));
}
if (len == -1)
len = 0;
num_pciaddrs = len / sizeof(struct ofw_pci_register);
j = num_pciaddrs;
for (i = 0; i < num_pciaddrs; i++) {
if (pciaddrs[i].size_lo < sc->fb.fb_stride * height)
continue;
if (!(pciaddrs[i].phys_hi &
OFW_PCI_PHYS_HI_SPACE_MEM32))
continue;
j = i;
if (pciaddrs[i].phys_hi & OFW_PCI_PHYS_HI_PREFETCHABLE)
break;
}
if (j == num_pciaddrs)
return (CN_DEAD);
if (ofw_reg_to_paddr(node, j, &fb_phys, &fb_phys_size, NULL) < 0)
return (CN_DEAD);
sc->fb.fb_pbase = (vm_paddr_t)fb_phys;
#else
return (CN_DEAD);
#endif
}
if (!sc->fb.fb_pbase)
return (CN_DEAD);
bus_space_map(sc->sc_memt, sc->fb.fb_pbase, sc->fb.fb_size,
BUS_SPACE_MAP_PREFETCHABLE,
(bus_space_handle_t *)&sc->fb.fb_vbase);
#if defined(__powerpc__)
if (!(cpu_features & PPC_FEATURE_BOOKE) && !(mfmsr() & PSL_DR))
sc->fb.fb_flags |= FB_FLAG_NOWRITE;
#endif
ofwfb_initialize(vd);
vt_fb_init(vd);
return (CN_INTERNAL);
}