Home | History | Annotate | Line # | Download | only in dev
ofb.c revision 1.47
      1  1.47  macallan /*	$NetBSD: ofb.c,v 1.47 2006/02/16 18:48:38 macallan Exp $	*/
      2   1.1    tsubai 
      3   1.1    tsubai /*
      4   1.1    tsubai  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
      5   1.1    tsubai  * All rights reserved.
      6   1.1    tsubai  *
      7   1.1    tsubai  * Author: Chris G. Demetriou
      8   1.1    tsubai  *
      9   1.1    tsubai  * Permission to use, copy, modify and distribute this software and
     10   1.1    tsubai  * its documentation is hereby granted, provided that both the copyright
     11   1.1    tsubai  * notice and this permission notice appear in all copies of the
     12   1.1    tsubai  * software, derivative works or modified versions, and any portions
     13   1.1    tsubai  * thereof, and that both notices appear in supporting documentation.
     14   1.1    tsubai  *
     15   1.1    tsubai  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     16   1.1    tsubai  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     17   1.1    tsubai  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     18   1.1    tsubai  *
     19   1.1    tsubai  * Carnegie Mellon requests users of this software to return to
     20   1.1    tsubai  *
     21   1.1    tsubai  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     22   1.1    tsubai  *  School of Computer Science
     23   1.1    tsubai  *  Carnegie Mellon University
     24   1.1    tsubai  *  Pittsburgh PA 15213-3890
     25   1.1    tsubai  *
     26   1.1    tsubai  * any improvements or extensions that they make and grant Carnegie the
     27   1.1    tsubai  * rights to redistribute these changes.
     28   1.1    tsubai  */
     29  1.36     lukem 
     30  1.36     lukem #include <sys/cdefs.h>
     31  1.47  macallan __KERNEL_RCSID(0, "$NetBSD: ofb.c,v 1.47 2006/02/16 18:48:38 macallan Exp $");
     32   1.1    tsubai 
     33   1.1    tsubai #include <sys/param.h>
     34   1.1    tsubai #include <sys/buf.h>
     35   1.1    tsubai #include <sys/conf.h>
     36   1.1    tsubai #include <sys/device.h>
     37   1.1    tsubai #include <sys/ioctl.h>
     38   1.1    tsubai #include <sys/kernel.h>
     39   1.1    tsubai #include <sys/malloc.h>
     40   1.1    tsubai #include <sys/systm.h>
     41   1.1    tsubai 
     42  1.15       mrg #include <uvm/uvm_extern.h>
     43  1.11    tsubai 
     44   1.3    tsubai #include <dev/pci/pcidevs.h>
     45   1.1    tsubai #include <dev/pci/pcireg.h>
     46   1.1    tsubai #include <dev/pci/pcivar.h>
     47  1.27   nathanw #include <dev/pci/pciio.h>
     48   1.1    tsubai 
     49   1.1    tsubai #include <dev/wscons/wsconsio.h>
     50   1.1    tsubai #include <dev/wscons/wsdisplayvar.h>
     51  1.11    tsubai #include <dev/rasops/rasops.h>
     52  1.45  macallan #include <dev/wsfont/wsfont.h>
     53   1.1    tsubai 
     54  1.20      matt #include <dev/ofw/openfirm.h>
     55  1.13    tsubai #include <dev/ofw/ofw_pci.h>
     56  1.13    tsubai 
     57   1.1    tsubai #include <machine/bus.h>
     58  1.20      matt #include <machine/autoconf.h>
     59   1.1    tsubai #include <machine/grfioctl.h>
     60  1.37      matt 
     61  1.37      matt #include <powerpc/oea/bat.h>
     62   1.1    tsubai 
     63  1.45  macallan #include <dev/wscons/wsdisplay_vconsvar.h>
     64   1.1    tsubai #include <macppc/dev/ofbvar.h>
     65   1.1    tsubai 
     66  1.18    tsubai #if OFB_ENABLE_CACHE
     67  1.18    tsubai int ofb_enable_cache = 1;
     68  1.18    tsubai #else
     69  1.18    tsubai int ofb_enable_cache = 0;
     70  1.18    tsubai #endif
     71  1.18    tsubai 
     72  1.44  macallan static int	ofbmatch(struct device *, struct cfdata *, void *);
     73  1.44  macallan static void	ofbattach(struct device *, struct device *, void *);
     74   1.1    tsubai 
     75  1.32   thorpej CFATTACH_DECL(ofb, sizeof(struct ofb_softc),
     76  1.32   thorpej     ofbmatch, ofbattach, NULL, NULL);
     77   1.1    tsubai 
     78   1.1    tsubai struct wsscreen_descr ofb_stdscreen = {
     79   1.1    tsubai 	"std",
     80   1.1    tsubai 	0, 0,	/* will be filled in -- XXX shouldn't, it's global */
     81  1.11    tsubai 	0,
     82   1.1    tsubai 	0, 0,
     83   1.1    tsubai 	WSSCREEN_REVERSE
     84   1.1    tsubai };
     85   1.1    tsubai 
     86   1.1    tsubai const struct wsscreen_descr *_ofb_scrlist[] = {
     87   1.1    tsubai 	&ofb_stdscreen,
     88   1.1    tsubai 	/* XXX other formats, graphics screen? */
     89   1.1    tsubai };
     90   1.1    tsubai 
     91   1.1    tsubai struct wsscreen_list ofb_screenlist = {
     92   1.1    tsubai 	sizeof(_ofb_scrlist) / sizeof(struct wsscreen_descr *), _ofb_scrlist
     93   1.1    tsubai };
     94   1.1    tsubai 
     95  1.45  macallan static int	ofb_ioctl(void *, u_long, caddr_t, int, struct lwp *);
     96  1.45  macallan static paddr_t	ofb_mmap(void *, off_t, int);
     97  1.45  macallan static int	copy_rom_font(void);
     98  1.45  macallan 
     99  1.45  macallan static int	ofb_init_rasops(int, struct rasops_info *);
    100  1.45  macallan static void	ofb_init_screen(void *, struct vcons_screen *, int, long *);
    101   1.1    tsubai 
    102   1.1    tsubai struct wsdisplay_accessops ofb_accessops = {
    103   1.1    tsubai 	ofb_ioctl,
    104   1.1    tsubai 	ofb_mmap,
    105  1.45  macallan 	NULL,		/* vcons_alloc_screen */
    106  1.45  macallan 	NULL,		/* vcons_free_screen */
    107  1.45  macallan 	NULL,		/* vcons_show_screen */
    108  1.45  macallan 	NULL		/* load_font */
    109   1.1    tsubai };
    110   1.1    tsubai 
    111  1.45  macallan static struct vcons_screen ofb_console_screen;
    112  1.11    tsubai static struct wsdisplay_font openfirm6x11;
    113  1.45  macallan static int    console_node, console_instance;
    114  1.45  macallan static vaddr_t fbaddr;
    115  1.45  macallan static int romfont_loaded = 0;
    116  1.45  macallan 
    117  1.45  macallan static void	ofb_putpalreg(struct ofb_softc *, int, uint8_t, uint8_t,
    118  1.45  macallan     uint8_t);
    119  1.45  macallan 
    120  1.45  macallan static int	ofb_getcmap(struct ofb_softc *, struct wsdisplay_cmap *);
    121  1.45  macallan static int	ofb_putcmap(struct ofb_softc *, struct wsdisplay_cmap *);
    122  1.45  macallan static void	ofb_init_cmap(struct ofb_softc *);
    123  1.11    tsubai 
    124  1.45  macallan extern const u_char rasops_cmap[768];
    125   1.1    tsubai 
    126  1.45  macallan static int
    127  1.44  macallan ofbmatch(struct device *parent, struct cfdata *match, void *aux)
    128   1.1    tsubai {
    129   1.1    tsubai 	struct pci_attach_args *pa = aux;
    130   1.1    tsubai 
    131   1.3    tsubai 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE &&
    132  1.11    tsubai 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_CONTROL)
    133   1.3    tsubai 		return 1;
    134   1.1    tsubai 
    135   1.3    tsubai 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY)
    136   1.3    tsubai 		return 1;
    137   1.3    tsubai 
    138   1.3    tsubai 	return 0;
    139   1.1    tsubai }
    140   1.1    tsubai 
    141  1.44  macallan static void
    142  1.44  macallan ofbattach(struct device *parent, struct device *self, void *aux)
    143   1.1    tsubai {
    144  1.22    tsubai 	struct ofb_softc *sc = (struct ofb_softc *)self;
    145   1.1    tsubai 	struct pci_attach_args *pa = aux;
    146   1.1    tsubai 	struct wsemuldisplaydev_attach_args a;
    147  1.45  macallan 	struct rasops_info *ri = &ofb_console_screen.scr_ri;
    148  1.45  macallan 	long defattr;
    149  1.47  macallan 	int console, len, node, sub;
    150   1.5    tsubai 	char devinfo[256];
    151   1.1    tsubai 
    152  1.46  macallan 	node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
    153  1.46  macallan 	console = (node == console_node);
    154  1.47  macallan 	if (!console) {
    155  1.47  macallan 		/* check if any of the childs matches */
    156  1.47  macallan 		sub = OF_child(node);
    157  1.47  macallan 		while ((sub != 0) && (sub != console_node)) {
    158  1.47  macallan 			sub = OF_peer(sub);
    159  1.47  macallan 		}
    160  1.47  macallan 		if (sub == console_node) {
    161  1.47  macallan 			console = TRUE;
    162  1.47  macallan 		}
    163  1.47  macallan 	}
    164  1.46  macallan 
    165  1.42  macallan 	sc->sc_memt = pa->pa_memt;
    166  1.42  macallan 	sc->sc_iot = pa->pa_iot;
    167  1.45  macallan 	sc->sc_pc = pa->pa_pc;
    168  1.45  macallan 	sc->sc_pcitag = pa->pa_tag;
    169  1.45  macallan 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    170  1.42  macallan 
    171  1.45  macallan 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    172  1.45  macallan 	printf(": %s\n", devinfo);
    173  1.46  macallan 
    174  1.46  macallan 	if (!console)
    175  1.46  macallan 		return;
    176  1.45  macallan 
    177  1.45  macallan 	vcons_init(&sc->vd, sc, &ofb_stdscreen, &ofb_accessops);
    178  1.45  macallan 	sc->vd.init_screen = ofb_init_screen;
    179  1.45  macallan 
    180   1.1    tsubai 	console = ofb_is_console();
    181   1.1    tsubai 
    182  1.46  macallan 	sc->sc_node = node;
    183  1.46  macallan 
    184   1.1    tsubai 	if (console) {
    185  1.45  macallan 		sc->sc_ih = console_instance;
    186  1.45  macallan 		vcons_init_screen(&sc->vd, &ofb_console_screen, 1, &defattr);
    187  1.45  macallan 		ofb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    188  1.45  macallan 		printf("%s: %d x %d, %dbpp\n", self->dv_xname,
    189  1.45  macallan 		       ri->ri_width, ri->ri_height, ri->ri_depth);
    190   1.1    tsubai 	} else {
    191  1.45  macallan 		char name[64];
    192  1.45  macallan 		if (sc->sc_node == 0) {
    193   1.3    tsubai 			printf(": ofdev not found\n");
    194   1.3    tsubai 			return;
    195   1.3    tsubai 		}
    196  1.16    tsubai 
    197  1.16    tsubai 		/* XXX There are two child screens on PowerBook. */
    198  1.24       wiz 		memset(devinfo, 0, sizeof(devinfo));
    199  1.45  macallan 		OF_getprop(sc->sc_node, "device_type", devinfo, sizeof(devinfo));
    200  1.16    tsubai 		len = strlen(devinfo);
    201  1.16    tsubai 		if (strcmp(devinfo + len - 7, "-parent") == 0)
    202  1.45  macallan 			sc->sc_node = OF_child(sc->sc_node);
    203  1.16    tsubai 
    204  1.45  macallan 		memset(name, 0, 64);
    205  1.45  macallan 		OF_package_to_path(sc->sc_node, name, sizeof(name));
    206  1.45  macallan 		sc->sc_ih = OF_open(name);
    207  1.45  macallan 
    208  1.45  macallan 	}
    209  1.45  macallan 
    210  1.45  macallan 	sc->sc_fbaddr = 0;
    211  1.45  macallan 	if (OF_getprop(sc->sc_node, "address", &sc->sc_fbaddr, 4) != 4)
    212  1.45  macallan 		OF_interpret("frame-buffer-adr", 1, &sc->sc_fbaddr);
    213  1.45  macallan 	if (sc->sc_fbaddr == 0) {
    214  1.45  macallan 		printf("%s: Unable to find the framebuffer address.\n",
    215  1.45  macallan 		    sc->sc_dev.dv_xname);
    216  1.45  macallan 		return;
    217   1.1    tsubai 	}
    218  1.45  macallan 
    219  1.16    tsubai 	/* XXX */
    220  1.45  macallan 	if (OF_getprop(sc->sc_node, "assigned-addresses", sc->sc_addrs,
    221  1.16    tsubai 	    sizeof(sc->sc_addrs)) == -1) {
    222  1.45  macallan 		sc->sc_node = OF_parent(sc->sc_node);
    223  1.45  macallan 		OF_getprop(sc->sc_node, "assigned-addresses", sc->sc_addrs,
    224  1.16    tsubai 		    sizeof(sc->sc_addrs));
    225  1.16    tsubai 	}
    226  1.13    tsubai 
    227  1.45  macallan 	ofb_init_cmap(sc);
    228   1.1    tsubai 
    229   1.1    tsubai 	a.console = console;
    230   1.1    tsubai 	a.scrdata = &ofb_screenlist;
    231   1.1    tsubai 	a.accessops = &ofb_accessops;
    232  1.45  macallan 	a.accesscookie = &sc->vd;
    233   1.1    tsubai 
    234   1.1    tsubai 	config_found(self, &a, wsemuldisplaydevprint);
    235   1.1    tsubai }
    236   1.1    tsubai 
    237  1.44  macallan static void
    238  1.45  macallan ofb_init_screen(void *cookie, struct vcons_screen *scr,
    239  1.45  macallan     int existing, long *defattr)
    240   1.1    tsubai {
    241  1.45  macallan 	struct ofb_softc *sc = cookie;
    242  1.45  macallan 	struct rasops_info *ri = &scr->scr_ri;
    243  1.45  macallan 
    244  1.45  macallan 	if (scr != &ofb_console_screen) {
    245  1.45  macallan 		ofb_init_rasops(sc->sc_node, ri);
    246  1.45  macallan 	}
    247  1.45  macallan }
    248   1.1    tsubai 
    249  1.45  macallan static int
    250  1.45  macallan ofb_init_rasops(int node, struct rasops_info *ri)
    251  1.45  macallan {
    252  1.45  macallan 	int32_t width, height, linebytes, depth;
    253   1.3    tsubai 
    254   1.8    tsubai 	/* XXX /chaos/control doesn't have "width", "height", ... */
    255   1.3    tsubai 	width = height = -1;
    256  1.11    tsubai 	if (OF_getprop(node, "width", &width, 4) != 4)
    257   1.3    tsubai 		OF_interpret("screen-width", 1, &width);
    258  1.11    tsubai 	if (OF_getprop(node, "height", &height, 4) != 4)
    259   1.3    tsubai 		OF_interpret("screen-height", 1, &height);
    260  1.11    tsubai 	if (OF_getprop(node, "linebytes", &linebytes, 4) != 4)
    261   1.3    tsubai 		linebytes = width;			/* XXX */
    262  1.11    tsubai 	if (OF_getprop(node, "depth", &depth, 4) != 4)
    263   1.3    tsubai 		depth = 8;				/* XXX */
    264  1.45  macallan 	if (OF_getprop(node, "address", &fbaddr, 4) != 4)
    265  1.45  macallan 		OF_interpret("frame-buffer-adr", 1, &fbaddr);
    266  1.11    tsubai 
    267  1.45  macallan 	if (width == -1 || height == -1 || fbaddr == 0 || fbaddr == -1)
    268  1.45  macallan 		return FALSE;
    269  1.18    tsubai 
    270  1.18    tsubai 	/* Enable write-through cache. */
    271  1.29      matt 	if (ofb_enable_cache) {
    272  1.29      matt 		vaddr_t va;
    273  1.29      matt 		/*
    274  1.29      matt 		 * Let's try to find an empty BAT to use
    275  1.29      matt 		 */
    276  1.29      matt 		for (va = SEGMENT_LENGTH; va < (USER_SR << ADDR_SR_SHFT);
    277  1.29      matt 		     va += SEGMENT_LENGTH) {
    278  1.29      matt 			if (battable[va >> ADDR_SR_SHFT].batu == 0) {
    279  1.29      matt 				battable[va >> ADDR_SR_SHFT].batl =
    280  1.45  macallan 				    BATL(fbaddr & 0xf0000000,
    281  1.33    briggs 					 BAT_G | BAT_W | BAT_M, BAT_PP_RW);
    282  1.29      matt 				battable[va >> ADDR_SR_SHFT].batu =
    283  1.29      matt 				    BATL(va, BAT_BL_256M, BAT_Vs);
    284  1.45  macallan 				fbaddr &= 0x0fffffff;
    285  1.45  macallan 				fbaddr |= va;
    286  1.29      matt 				break;
    287  1.29      matt 			}
    288  1.29      matt 		}
    289  1.18    tsubai 	}
    290   1.1    tsubai 
    291  1.11    tsubai 	/* initialize rasops */
    292  1.11    tsubai 	ri->ri_width = width;
    293  1.11    tsubai 	ri->ri_height = height;
    294  1.11    tsubai 	ri->ri_depth = depth;
    295  1.11    tsubai 	ri->ri_stride = linebytes;
    296  1.45  macallan 	ri->ri_bits = (char *)fbaddr;
    297  1.45  macallan 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
    298   1.1    tsubai 
    299   1.8    tsubai 	/* If screen is smaller than 1024x768, use small font. */
    300  1.45  macallan 	if ((width < 1024 || height < 768) && (romfont_loaded)) {
    301  1.11    tsubai 		int cols, rows;
    302   1.8    tsubai 
    303  1.45  macallan 		/*
    304  1.45  macallan 		 * XXX this assumes we're the console which may or may not
    305  1.45  macallan 		 * be the case
    306  1.45  macallan 		 */
    307  1.11    tsubai 		OF_interpret("#lines", 1, &rows);
    308  1.11    tsubai 		OF_interpret("#columns", 1, &cols);
    309  1.11    tsubai 		ri->ri_font = &openfirm6x11;
    310  1.11    tsubai 		ri->ri_wsfcookie = -1;		/* not using wsfont */
    311  1.11    tsubai 		rasops_init(ri, rows, cols);
    312  1.11    tsubai 
    313  1.42  macallan 		ri->ri_xorigin = (width - cols * ri->ri_font->fontwidth) >> 1;
    314  1.42  macallan 		ri->ri_yorigin = (height - rows * ri->ri_font->fontheight)
    315  1.42  macallan 		    >> 1;
    316  1.45  macallan 		ri->ri_bits = (char *)fbaddr + ri->ri_xorigin +
    317  1.11    tsubai 			      ri->ri_stride * ri->ri_yorigin;
    318  1.17    tsubai 	} else {
    319  1.45  macallan 		/* use as much of the screen as the font permits */
    320  1.45  macallan 		rasops_init(ri, height/8, width/8);
    321  1.45  macallan 		ri->ri_caps = WSSCREEN_WSCOLORS;
    322  1.45  macallan 		rasops_reconfig(ri, height / ri->ri_font->fontheight,
    323  1.45  macallan 		    width / ri->ri_font->fontwidth);
    324  1.17    tsubai 	}
    325  1.11    tsubai 
    326  1.45  macallan 	return TRUE;
    327   1.1    tsubai }
    328   1.1    tsubai 
    329   1.1    tsubai int
    330   1.1    tsubai ofb_is_console()
    331   1.1    tsubai {
    332   1.1    tsubai 	int chosen, stdout, node;
    333   1.1    tsubai 	char type[16];
    334   1.1    tsubai 
    335   1.1    tsubai 	chosen = OF_finddevice("/chosen");
    336   1.1    tsubai 	OF_getprop(chosen, "stdout", &stdout, 4);
    337   1.1    tsubai 	node = OF_instance_to_package(stdout);
    338   1.1    tsubai 	OF_getprop(node, "device_type", type, sizeof(type));
    339   1.1    tsubai 	if (strcmp(type, "display") == 0)
    340   1.1    tsubai 		return 1;
    341   1.1    tsubai 	else
    342   1.1    tsubai 		return 0;
    343   1.1    tsubai }
    344   1.1    tsubai 
    345  1.44  macallan static int
    346  1.44  macallan ofb_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct lwp *l)
    347   1.1    tsubai {
    348  1.45  macallan 	struct vcons_data *vd = v;
    349  1.45  macallan 	struct ofb_softc *sc = vd->cookie;
    350   1.1    tsubai 	struct wsdisplay_fbinfo *wdf;
    351  1.45  macallan 	struct vcons_screen *ms = vd->active;
    352   1.1    tsubai 	struct grfinfo *gm;
    353   1.1    tsubai 
    354   1.1    tsubai 	switch (cmd) {
    355   1.1    tsubai 	case WSDISPLAYIO_GTYPE:
    356   1.1    tsubai 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;	/* XXX ? */
    357   1.1    tsubai 		return 0;
    358   1.1    tsubai 
    359   1.1    tsubai 	case WSDISPLAYIO_GINFO:
    360  1.45  macallan 		/* we won't get here without any screen anyway */
    361  1.45  macallan 		if (ms != NULL) {
    362  1.45  macallan 			wdf = (void *)data;
    363  1.45  macallan 			wdf->height = ms->scr_ri.ri_width;
    364  1.45  macallan 			wdf->width = ms->scr_ri.ri_height;
    365  1.45  macallan 			wdf->depth = ms->scr_ri.ri_depth;
    366  1.45  macallan 			wdf->cmsize = 256;
    367  1.45  macallan 			return 0;
    368  1.45  macallan 		} else
    369  1.45  macallan 			return ENODEV;
    370   1.1    tsubai 
    371  1.11    tsubai 	case WSDISPLAYIO_GETCMAP:
    372  1.11    tsubai 		return ofb_getcmap(sc, (struct wsdisplay_cmap *)data);
    373  1.11    tsubai 
    374   1.1    tsubai 	case WSDISPLAYIO_PUTCMAP:
    375  1.11    tsubai 		return ofb_putcmap(sc, (struct wsdisplay_cmap *)data);
    376   1.1    tsubai 
    377   1.1    tsubai 	/* XXX There are no way to know framebuffer pa from a user program. */
    378   1.1    tsubai 	case GRFIOCGINFO:
    379  1.45  macallan 		if (ms != NULL) {
    380  1.45  macallan 			gm = (void *)data;
    381  1.45  macallan 			memset(gm, 0, sizeof(struct grfinfo));
    382  1.45  macallan 			gm->gd_fbaddr = (caddr_t)sc->sc_fbaddr;
    383  1.45  macallan 			gm->gd_fbrowbytes = ms->scr_ri.ri_stride;
    384  1.45  macallan 			return 0;
    385  1.45  macallan 		} else
    386  1.45  macallan 			return ENODEV;
    387  1.45  macallan 	case WSDISPLAYIO_SMODE:
    388  1.45  macallan 		{
    389  1.45  macallan 			int new_mode = *(int*)data;
    390  1.45  macallan 			if (new_mode != sc->sc_mode)
    391  1.45  macallan 			{
    392  1.45  macallan 				sc->sc_mode = new_mode;
    393  1.45  macallan 				if (new_mode == WSDISPLAYIO_MODE_EMUL)
    394  1.45  macallan 				{
    395  1.45  macallan 					ofb_init_cmap(sc);
    396  1.45  macallan 					vcons_redraw_screen(ms);
    397  1.45  macallan 				}
    398  1.45  macallan 			}
    399  1.45  macallan 		}
    400   1.1    tsubai 		return 0;
    401  1.27   nathanw 	/* PCI config read/write passthrough. */
    402  1.27   nathanw 	case PCI_IOC_CFGREAD:
    403  1.27   nathanw 	case PCI_IOC_CFGWRITE:
    404  1.27   nathanw 		return (pci_devioctl(sc->sc_pc, sc->sc_pcitag,
    405  1.43  christos 		    cmd, data, flag, l));
    406   1.1    tsubai 	}
    407  1.26    atatat 	return EPASSTHROUGH;
    408   1.1    tsubai }
    409   1.1    tsubai 
    410  1.44  macallan static paddr_t
    411  1.44  macallan ofb_mmap(void *v, off_t offset, int prot)
    412   1.1    tsubai {
    413  1.45  macallan 	struct vcons_data *vd = v;
    414  1.45  macallan 	struct ofb_softc *sc = vd->cookie;
    415  1.45  macallan 	struct rasops_info *ri;
    416  1.13    tsubai 	u_int32_t *ap = sc->sc_addrs;
    417  1.42  macallan 	struct proc *me;
    418  1.13    tsubai 	int i;
    419  1.13    tsubai 
    420  1.45  macallan 	if (vd->active == NULL) {
    421  1.45  macallan 		printf("%s: no active screen.\n", sc->sc_dev.dv_xname);
    422  1.45  macallan 		return -1;
    423  1.45  macallan 	}
    424  1.45  macallan 
    425  1.45  macallan 	ri = &vd->active->scr_ri;
    426  1.45  macallan 
    427  1.42  macallan 	/* framebuffer at offset 0 */
    428  1.13    tsubai 	if (offset >=0 && offset < (ri->ri_stride * ri->ri_height))
    429  1.45  macallan 		return sc->sc_fbaddr + offset;
    430  1.13    tsubai 
    431  1.42  macallan 	/*
    432  1.42  macallan 	 * restrict all other mappings to processes with superuser privileges
    433  1.42  macallan 	 * or the kernel itself
    434  1.42  macallan 	 */
    435  1.42  macallan 	me = __curproc();
    436  1.42  macallan 	if (me != NULL) {
    437  1.42  macallan 		if (suser(me->p_ucred, NULL) != 0) {
    438  1.45  macallan 			printf("%s: mmap() rejected.\n", sc->sc_dev.dv_xname);
    439  1.42  macallan 			return -1;
    440  1.42  macallan 		}
    441  1.42  macallan 	}
    442  1.42  macallan 
    443  1.42  macallan 	/* let them mmap() 0xa0000 - 0xbffff if it's not covered above */
    444  1.42  macallan #ifdef OFB_FAKE_VGA_FB
    445  1.42  macallan 	if (offset >=0xa0000 && offset < 0xbffff)
    446  1.45  macallan 		return sc->sc_fbaddr + offset - 0xa0000;
    447  1.42  macallan #endif
    448  1.42  macallan 
    449  1.42  macallan 	/* allow to map our IO space */
    450  1.42  macallan 	if ((offset >= 0xf2000000) && (offset < 0xf2800000)) {
    451  1.42  macallan 		return bus_space_mmap(sc->sc_iot, offset-0xf2000000, 0, prot,
    452  1.42  macallan 		    BUS_SPACE_MAP_LINEAR);
    453  1.42  macallan 	}
    454  1.42  macallan 
    455  1.13    tsubai 	for (i = 0; i < 6; i++) {
    456  1.13    tsubai 		switch (ap[0] & OFW_PCI_PHYS_HI_SPACEMASK) {
    457  1.13    tsubai 		case OFW_PCI_PHYS_HI_SPACE_MEM32:
    458  1.42  macallan 			if (offset >= ap[2] && offset < ap[2] + ap[4])
    459  1.42  macallan 				return bus_space_mmap(sc->sc_memt, offset, 0,
    460  1.42  macallan 				    prot, BUS_SPACE_MAP_LINEAR);
    461  1.13    tsubai 		}
    462  1.13    tsubai 		ap += 5;
    463  1.13    tsubai 	}
    464   1.1    tsubai 
    465  1.13    tsubai 	return -1;
    466   1.1    tsubai }
    467   1.1    tsubai 
    468   1.1    tsubai int
    469   1.1    tsubai ofb_cnattach()
    470   1.1    tsubai {
    471  1.45  macallan 	struct rasops_info *ri = &ofb_console_screen.scr_ri;
    472   1.1    tsubai 	long defattr;
    473  1.11    tsubai 	int crow = 0;
    474   1.1    tsubai 	int chosen, stdout, node;
    475   1.1    tsubai 
    476   1.1    tsubai 	chosen = OF_finddevice("/chosen");
    477   1.1    tsubai 	OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
    478   1.1    tsubai 	node = OF_instance_to_package(stdout);
    479  1.45  macallan 	console_node = node;
    480  1.45  macallan 	console_instance = stdout;
    481   1.1    tsubai 
    482  1.11    tsubai 	/* get current cursor position */
    483  1.11    tsubai 	OF_interpret("line#", 1, &crow);
    484   1.8    tsubai 
    485  1.11    tsubai 	/* move (rom monitor) cursor to the lowest line - 1 */
    486  1.11    tsubai 	OF_interpret("#lines 2 - to line#", 0);
    487  1.45  macallan 
    488  1.45  macallan 	wsfont_init();
    489  1.45  macallan 	if (copy_rom_font() == 0) {
    490  1.45  macallan 		romfont_loaded = 1;
    491  1.45  macallan 	}
    492  1.45  macallan 
    493  1.45  macallan 	/* set up rasops */
    494  1.45  macallan 	ofb_init_rasops(console_node, ri);
    495   1.8    tsubai 
    496  1.45  macallan 	/*
    497  1.45  macallan 	 * no need to clear the screen here when we're mimicing firmware
    498  1.45  macallan 	 * output anyway
    499  1.45  macallan 	 */
    500  1.45  macallan #if 0
    501  1.11    tsubai 	if (ri->ri_width >= 1024 && ri->ri_height >= 768) {
    502  1.11    tsubai 		int i, screenbytes = ri->ri_stride * ri->ri_height;
    503   1.8    tsubai 
    504   1.8    tsubai 		for (i = 0; i < screenbytes; i += sizeof(u_int32_t))
    505  1.45  macallan 			*(u_int32_t *)(fbaddr + i) = 0xffffffff;
    506   1.8    tsubai 		crow = 0;
    507   1.8    tsubai 	}
    508  1.45  macallan #endif
    509  1.45  macallan 
    510  1.45  macallan 	ofb_stdscreen.nrows = ri->ri_rows;
    511  1.45  macallan 	ofb_stdscreen.ncols = ri->ri_cols;
    512  1.45  macallan 	ofb_stdscreen.textops = &ri->ri_ops;
    513  1.45  macallan 	ofb_stdscreen.capabilities = ri->ri_caps;
    514   1.8    tsubai 
    515  1.45  macallan 	ri->ri_ops.allocattr(ri, 0, 0, 0, &defattr);
    516  1.11    tsubai 	wsdisplay_cnattach(&ofb_stdscreen, ri, 0, crow, defattr);
    517   1.8    tsubai 
    518   1.8    tsubai 	return 0;
    519   1.8    tsubai }
    520   1.8    tsubai 
    521  1.44  macallan static int
    522   1.8    tsubai copy_rom_font()
    523   1.8    tsubai {
    524   1.8    tsubai 	u_char *romfont;
    525   1.8    tsubai 	int char_width, char_height;
    526   1.8    tsubai 	int chosen, mmu, m, e;
    527   1.8    tsubai 
    528   1.8    tsubai 	/* Get ROM FONT address. */
    529   1.8    tsubai 	OF_interpret("font-adr", 1, &romfont);
    530   1.8    tsubai 	if (romfont == NULL)
    531   1.8    tsubai 		return -1;
    532   1.8    tsubai 
    533   1.8    tsubai 	chosen = OF_finddevice("/chosen");
    534   1.8    tsubai 	OF_getprop(chosen, "mmu", &mmu, 4);
    535   1.1    tsubai 
    536   1.8    tsubai 	/*
    537   1.8    tsubai 	 * Convert to physcal address.  We cannot access to Open Firmware's
    538   1.8    tsubai 	 * virtual address space.
    539   1.8    tsubai 	 */
    540   1.8    tsubai 	OF_call_method("translate", mmu, 1, 3, romfont, &romfont, &m, &e);
    541   1.8    tsubai 
    542   1.8    tsubai 	/* Get character size */
    543   1.8    tsubai 	OF_interpret("char-width", 1, &char_width);
    544   1.8    tsubai 	OF_interpret("char-height", 1, &char_height);
    545   1.8    tsubai 
    546  1.11    tsubai 	openfirm6x11.name = "Open Firmware";
    547  1.11    tsubai 	openfirm6x11.firstchar = 32;
    548  1.11    tsubai 	openfirm6x11.numchars = 96;
    549  1.11    tsubai 	openfirm6x11.encoding = WSDISPLAY_FONTENC_ISO;
    550  1.11    tsubai 	openfirm6x11.fontwidth = char_width;
    551  1.11    tsubai 	openfirm6x11.fontheight = char_height;
    552  1.11    tsubai 	openfirm6x11.stride = 1;
    553  1.11    tsubai 	openfirm6x11.bitorder = WSDISPLAY_FONTORDER_L2R;
    554  1.11    tsubai 	openfirm6x11.byteorder = WSDISPLAY_FONTORDER_L2R;
    555  1.11    tsubai 	openfirm6x11.data = romfont;
    556  1.11    tsubai 
    557  1.11    tsubai 	return 0;
    558  1.11    tsubai }
    559  1.11    tsubai 
    560  1.44  macallan static int
    561  1.44  macallan ofb_getcmap(struct ofb_softc *sc, struct wsdisplay_cmap *cm)
    562  1.11    tsubai {
    563  1.11    tsubai 	u_int index = cm->index;
    564  1.11    tsubai 	u_int count = cm->count;
    565  1.11    tsubai 	int error;
    566  1.11    tsubai 
    567  1.11    tsubai 	if (index >= 256 || count > 256 || index + count > 256)
    568  1.11    tsubai 		return EINVAL;
    569  1.11    tsubai 
    570  1.11    tsubai 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    571  1.11    tsubai 	if (error)
    572  1.11    tsubai 		return error;
    573  1.11    tsubai 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    574  1.11    tsubai 	if (error)
    575  1.11    tsubai 		return error;
    576  1.11    tsubai 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    577  1.11    tsubai 	if (error)
    578  1.11    tsubai 		return error;
    579   1.8    tsubai 
    580   1.1    tsubai 	return 0;
    581   1.1    tsubai }
    582   1.1    tsubai 
    583  1.45  macallan int
    584  1.44  macallan ofb_putcmap(struct ofb_softc *sc, struct wsdisplay_cmap *cm)
    585   1.1    tsubai {
    586  1.25  jdolecek 	u_int index = cm->index;
    587  1.25  jdolecek 	u_int count = cm->count;
    588  1.38       chs 	int i, error;
    589  1.38       chs 	u_char rbuf[256], gbuf[256], bbuf[256];
    590   1.1    tsubai 	u_char *r, *g, *b;
    591   1.1    tsubai 
    592   1.1    tsubai 	if (cm->index >= 256 || cm->count > 256 ||
    593   1.1    tsubai 	    (cm->index + cm->count) > 256)
    594   1.1    tsubai 		return EINVAL;
    595  1.38       chs 	error = copyin(cm->red, &rbuf[index], count);
    596  1.38       chs 	if (error)
    597  1.38       chs 		return error;
    598  1.38       chs 	error = copyin(cm->green, &gbuf[index], count);
    599  1.38       chs 	if (error)
    600  1.38       chs 		return error;
    601  1.38       chs 	error = copyin(cm->blue, &bbuf[index], count);
    602  1.38       chs 	if (error)
    603  1.38       chs 		return error;
    604  1.38       chs 
    605  1.38       chs 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    606  1.38       chs 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    607  1.38       chs 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    608   1.1    tsubai 
    609   1.1    tsubai 	r = &sc->sc_cmap_red[index];
    610   1.1    tsubai 	g = &sc->sc_cmap_green[index];
    611   1.1    tsubai 	b = &sc->sc_cmap_blue[index];
    612   1.1    tsubai 	for (i = 0; i < count; i++) {
    613  1.45  macallan 		OF_call_method_1("color!", sc->sc_ih, 4, *r, *g, *b, index);
    614   1.1    tsubai 		r++, g++, b++, index++;
    615   1.1    tsubai 	}
    616   1.1    tsubai 	return 0;
    617   1.1    tsubai }
    618  1.45  macallan 
    619  1.45  macallan static void
    620  1.45  macallan ofb_putpalreg(struct ofb_softc *sc, int idx, uint8_t r, uint8_t g, uint8_t b)
    621  1.45  macallan {
    622  1.45  macallan 	if ((idx<0) || (idx > 255))
    623  1.45  macallan 		return;
    624  1.45  macallan 	OF_call_method_1("color!", sc->sc_ih, 4, r, g, b, idx);
    625  1.45  macallan 	sc->sc_cmap_red[idx] = r;
    626  1.45  macallan 	sc->sc_cmap_green[idx] = g;
    627  1.45  macallan 	sc->sc_cmap_blue[idx] = b;
    628  1.45  macallan }
    629  1.45  macallan 
    630  1.45  macallan static void
    631  1.45  macallan ofb_init_cmap(struct ofb_softc *sc)
    632  1.45  macallan {
    633  1.45  macallan 	int idx, i;
    634  1.45  macallan 	/* mess with the palette only when we're running in 8 bit */
    635  1.45  macallan 	if (ofb_console_screen.scr_ri.ri_depth == 8) {
    636  1.45  macallan 		idx = 0;
    637  1.45  macallan 		for (i = 0; i < 256; i++) {
    638  1.45  macallan 			ofb_putpalreg(sc, i, rasops_cmap[idx],
    639  1.45  macallan 			    rasops_cmap[idx + 1], rasops_cmap[idx + 2]);
    640  1.45  macallan 			idx += 3;
    641  1.45  macallan 		}
    642  1.45  macallan 	}
    643  1.45  macallan }
    644