Home | History | Annotate | Line # | Download | only in pci
gffb.c revision 1.1
      1  1.1  macallan /*	$NetBSD: gffb.c,v 1.1 2013/09/18 14:30:45 macallan Exp $	*/
      2  1.1  macallan 
      3  1.1  macallan /*
      4  1.1  macallan  * Copyright (c) 2007, 2012 Michael Lorenz
      5  1.1  macallan  * All rights reserved.
      6  1.1  macallan  *
      7  1.1  macallan  * Redistribution and use in source and binary forms, with or without
      8  1.1  macallan  * modification, are permitted provided that the following conditions
      9  1.1  macallan  * are met:
     10  1.1  macallan  * 1. Redistributions of source code must retain the above copyright
     11  1.1  macallan  *    notice, this list of conditions and the following disclaimer.
     12  1.1  macallan  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  macallan  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  macallan  *    documentation and/or other materials provided with the distribution.
     15  1.1  macallan  *
     16  1.1  macallan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  macallan  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1  macallan  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1  macallan  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1  macallan  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  1.1  macallan  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  1.1  macallan  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  1.1  macallan  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  1.1  macallan  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  1.1  macallan  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  1.1  macallan  */
     27  1.1  macallan 
     28  1.1  macallan /*
     29  1.1  macallan  * A console driver for nvidia geforce graphics controllers
     30  1.1  macallan  * tested on macppc only so far
     31  1.1  macallan  */
     32  1.1  macallan 
     33  1.1  macallan #include <sys/cdefs.h>
     34  1.1  macallan __KERNEL_RCSID(0, "$NetBSD: gffb.c,v 1.1 2013/09/18 14:30:45 macallan Exp $");
     35  1.1  macallan 
     36  1.1  macallan #include <sys/param.h>
     37  1.1  macallan #include <sys/systm.h>
     38  1.1  macallan #include <sys/kernel.h>
     39  1.1  macallan #include <sys/device.h>
     40  1.1  macallan #include <sys/malloc.h>
     41  1.1  macallan #include <sys/lwp.h>
     42  1.1  macallan #include <sys/kauth.h>
     43  1.1  macallan 
     44  1.1  macallan #include <dev/videomode/videomode.h>
     45  1.1  macallan 
     46  1.1  macallan #include <dev/pci/pcivar.h>
     47  1.1  macallan #include <dev/pci/pcireg.h>
     48  1.1  macallan #include <dev/pci/pcidevs.h>
     49  1.1  macallan #include <dev/pci/pciio.h>
     50  1.1  macallan #include <dev/pci/gffbreg.h>
     51  1.1  macallan 
     52  1.1  macallan #include <dev/wscons/wsdisplayvar.h>
     53  1.1  macallan #include <dev/wscons/wsconsio.h>
     54  1.1  macallan #include <dev/wsfont/wsfont.h>
     55  1.1  macallan #include <dev/rasops/rasops.h>
     56  1.1  macallan #include <dev/wscons/wsdisplay_vconsvar.h>
     57  1.1  macallan #include <dev/pci/wsdisplay_pci.h>
     58  1.1  macallan #include <dev/wscons/wsdisplay_glyphcachevar.h>
     59  1.1  macallan 
     60  1.1  macallan #include <dev/i2c/i2cvar.h>
     61  1.1  macallan 
     62  1.1  macallan #include "opt_gffb.h"
     63  1.1  macallan #include "opt_vcons.h"
     64  1.1  macallan 
     65  1.1  macallan #ifdef GFFB_DEBUG
     66  1.1  macallan #define DPRINTF printf
     67  1.1  macallan #else
     68  1.1  macallan #define DPRINTF while(0) printf
     69  1.1  macallan #endif
     70  1.1  macallan 
     71  1.1  macallan struct gffb_softc {
     72  1.1  macallan 	device_t sc_dev;
     73  1.1  macallan 
     74  1.1  macallan 	pci_chipset_tag_t sc_pc;
     75  1.1  macallan 	pcitag_t sc_pcitag;
     76  1.1  macallan 
     77  1.1  macallan 	bus_space_tag_t sc_memt;
     78  1.1  macallan 	bus_space_tag_t sc_iot;
     79  1.1  macallan 
     80  1.1  macallan 	bus_space_handle_t sc_regh, sc_fbh;
     81  1.1  macallan 	bus_addr_t sc_fb, sc_reg;
     82  1.1  macallan 	bus_size_t sc_fbsize, sc_regsize;
     83  1.1  macallan 	uint8_t *sc_fbaddr;
     84  1.1  macallan 
     85  1.1  macallan 	int sc_width, sc_height, sc_depth, sc_stride;
     86  1.1  macallan 	int sc_locked;
     87  1.1  macallan 	struct vcons_screen sc_console_screen;
     88  1.1  macallan 	struct wsscreen_descr sc_defaultscreen_descr;
     89  1.1  macallan 	const struct wsscreen_descr *sc_screens[1];
     90  1.1  macallan 	struct wsscreen_list sc_screenlist;
     91  1.1  macallan 	struct vcons_data vd;
     92  1.1  macallan 	int sc_mode;
     93  1.1  macallan 	u_char sc_cmap_red[256];
     94  1.1  macallan 	u_char sc_cmap_green[256];
     95  1.1  macallan 	u_char sc_cmap_blue[256];
     96  1.1  macallan 	glyphcache sc_gc;
     97  1.1  macallan };
     98  1.1  macallan 
     99  1.1  macallan static int	gffb_match(device_t, cfdata_t, void *);
    100  1.1  macallan static void	gffb_attach(device_t, device_t, void *);
    101  1.1  macallan 
    102  1.1  macallan CFATTACH_DECL_NEW(gffb, sizeof(struct gffb_softc),
    103  1.1  macallan     gffb_match, gffb_attach, NULL, NULL);
    104  1.1  macallan 
    105  1.1  macallan static int	gffb_ioctl(void *, void *, u_long, void *, int,
    106  1.1  macallan 			     struct lwp *);
    107  1.1  macallan static paddr_t	gffb_mmap(void *, void *, off_t, int);
    108  1.1  macallan static void	gffb_init_screen(void *, struct vcons_screen *, int, long *);
    109  1.1  macallan 
    110  1.1  macallan static int	gffb_putcmap(struct gffb_softc *, struct wsdisplay_cmap *);
    111  1.1  macallan static int 	gffb_getcmap(struct gffb_softc *, struct wsdisplay_cmap *);
    112  1.1  macallan static void	gffb_restore_palette(struct gffb_softc *);
    113  1.1  macallan static int 	gffb_putpalreg(struct gffb_softc *, uint8_t, uint8_t,
    114  1.1  macallan 			    uint8_t, uint8_t);
    115  1.1  macallan 
    116  1.1  macallan static void	gffb_init(struct gffb_softc *);
    117  1.1  macallan 
    118  1.1  macallan #if notyet
    119  1.1  macallan static void	gffb_flush_engine(struct gffb_softc *);
    120  1.1  macallan static void	gffb_rectfill(struct gffb_softc *, int, int, int, int,
    121  1.1  macallan 			    uint32_t);
    122  1.1  macallan static void	gffb_bitblt(void *, int, int, int, int, int,
    123  1.1  macallan 			    int, int);
    124  1.1  macallan 
    125  1.1  macallan static void	gffb_cursor(void *, int, int, int);
    126  1.1  macallan static void	gffb_putchar(void *, int, int, u_int, long);
    127  1.1  macallan static void	gffb_putchar_aa(void *, int, int, u_int, long);
    128  1.1  macallan static void	gffb_copycols(void *, int, int, int, int);
    129  1.1  macallan static void	gffb_erasecols(void *, int, int, int, long);
    130  1.1  macallan static void	gffb_copyrows(void *, int, int, int);
    131  1.1  macallan static void	gffb_eraserows(void *, int, int, long);
    132  1.1  macallan #endif /* notyet */
    133  1.1  macallan 
    134  1.1  macallan struct wsdisplay_accessops gffb_accessops = {
    135  1.1  macallan 	gffb_ioctl,
    136  1.1  macallan 	gffb_mmap,
    137  1.1  macallan 	NULL,	/* alloc_screen */
    138  1.1  macallan 	NULL,	/* free_screen */
    139  1.1  macallan 	NULL,	/* show_screen */
    140  1.1  macallan 	NULL, 	/* load_font */
    141  1.1  macallan 	NULL,	/* pollc */
    142  1.1  macallan 	NULL	/* scroll */
    143  1.1  macallan };
    144  1.1  macallan 
    145  1.1  macallan static int
    146  1.1  macallan gffb_match(device_t parent, cfdata_t match, void *aux)
    147  1.1  macallan {
    148  1.1  macallan 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    149  1.1  macallan 
    150  1.1  macallan 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY)
    151  1.1  macallan 		return 0;
    152  1.1  macallan 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NVIDIA)
    153  1.1  macallan 		return 0;
    154  1.1  macallan 
    155  1.1  macallan 	/* only card tested on so far - likely need a list */
    156  1.1  macallan 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_NVIDIA_GEFORCE2MX)
    157  1.1  macallan 		return 100;
    158  1.1  macallan 	return (0);
    159  1.1  macallan }
    160  1.1  macallan 
    161  1.1  macallan static void
    162  1.1  macallan gffb_attach(device_t parent, device_t self, void *aux)
    163  1.1  macallan {
    164  1.1  macallan 	struct gffb_softc	*sc = device_private(self);
    165  1.1  macallan 	struct pci_attach_args	*pa = aux;
    166  1.1  macallan 	struct rasops_info	*ri;
    167  1.1  macallan 	bus_space_tag_t		tag;
    168  1.1  macallan 	struct wsemuldisplaydev_attach_args aa;
    169  1.1  macallan 	prop_dictionary_t	dict;
    170  1.1  macallan 	unsigned long		defattr;
    171  1.1  macallan 	bool			is_console;
    172  1.1  macallan 	int			i, j;
    173  1.1  macallan 	uint8_t			cmap[768];
    174  1.1  macallan 
    175  1.1  macallan 	sc->sc_pc = pa->pa_pc;
    176  1.1  macallan 	sc->sc_pcitag = pa->pa_tag;
    177  1.1  macallan 	sc->sc_memt = pa->pa_memt;
    178  1.1  macallan 	sc->sc_iot = pa->pa_iot;
    179  1.1  macallan 	sc->sc_dev = self;
    180  1.1  macallan 
    181  1.1  macallan 	pci_aprint_devinfo(pa, NULL);
    182  1.1  macallan 
    183  1.1  macallan 	/* fill in parameters from properties */
    184  1.1  macallan 	dict = device_properties(self);
    185  1.1  macallan 	if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) {
    186  1.1  macallan 		aprint_error("%s: no width property\n", device_xname(self));
    187  1.1  macallan 		return;
    188  1.1  macallan 	}
    189  1.1  macallan 	if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) {
    190  1.1  macallan 		aprint_error("%s: no height property\n", device_xname(self));
    191  1.1  macallan 		return;
    192  1.1  macallan 	}
    193  1.1  macallan 
    194  1.1  macallan #ifdef GLYPHCACHE_DEBUG
    195  1.1  macallan 	/* leave some visible VRAM unused so we can see the glyph cache */
    196  1.1  macallan 	sc->sc_height -= 200;
    197  1.1  macallan #endif
    198  1.1  macallan 
    199  1.1  macallan 	if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth)) {
    200  1.1  macallan 		aprint_error("%s: no depth property\n", device_xname(self));
    201  1.1  macallan 		return;
    202  1.1  macallan 	}
    203  1.1  macallan 	if (!prop_dictionary_get_uint32(dict, "linebytes", &sc->sc_stride)) {
    204  1.1  macallan 		aprint_error("%s: no linebytes property\n",
    205  1.1  macallan 		    device_xname(self));
    206  1.1  macallan 		return;
    207  1.1  macallan 	}
    208  1.1  macallan 
    209  1.1  macallan 	prop_dictionary_get_bool(dict, "is_console", &is_console);
    210  1.1  macallan 
    211  1.1  macallan 	if (pci_mapreg_map(pa, 0x14, PCI_MAPREG_TYPE_MEM,
    212  1.1  macallan 	    BUS_SPACE_MAP_PREFETCHABLE | BUS_SPACE_MAP_LINEAR,
    213  1.1  macallan 	    &tag, &sc->sc_fbh, &sc->sc_fb, &sc->sc_fbsize)) {
    214  1.1  macallan 		aprint_error("%s: failed to map the framebuffer.\n",
    215  1.1  macallan 		    device_xname(sc->sc_dev));
    216  1.1  macallan 	}
    217  1.1  macallan 	sc->sc_fbaddr = bus_space_vaddr(tag, sc->sc_fbh);
    218  1.1  macallan 
    219  1.1  macallan 	if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_MEM, 0,
    220  1.1  macallan 	    &tag, &sc->sc_regh, &sc->sc_reg, &sc->sc_regsize)) {
    221  1.1  macallan 		aprint_error("%s: failed to map registers.\n",
    222  1.1  macallan 		    device_xname(sc->sc_dev));
    223  1.1  macallan 	}
    224  1.1  macallan 
    225  1.1  macallan 	aprint_normal("%s: %d MB aperture at 0x%08x\n", device_xname(self),
    226  1.1  macallan 	    (int)(sc->sc_fbsize >> 20), (uint32_t)sc->sc_fb);
    227  1.1  macallan 
    228  1.1  macallan 	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
    229  1.1  macallan 		"default",
    230  1.1  macallan 		0, 0,
    231  1.1  macallan 		NULL,
    232  1.1  macallan 		8, 16,
    233  1.1  macallan 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    234  1.1  macallan 		NULL
    235  1.1  macallan 	};
    236  1.1  macallan 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
    237  1.1  macallan 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
    238  1.1  macallan 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    239  1.1  macallan 	sc->sc_locked = 0;
    240  1.1  macallan 
    241  1.1  macallan 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
    242  1.1  macallan 	    &gffb_accessops);
    243  1.1  macallan 	sc->vd.init_screen = gffb_init_screen;
    244  1.1  macallan 
    245  1.1  macallan 	/* init engine here */
    246  1.1  macallan 	gffb_init(sc);
    247  1.1  macallan 
    248  1.1  macallan 	ri = &sc->sc_console_screen.scr_ri;
    249  1.1  macallan 
    250  1.1  macallan #if notyet
    251  1.1  macallan 	sc->sc_gc.gc_bitblt = gffb_bitblt;
    252  1.1  macallan 	sc->sc_gc.gc_blitcookie = sc;
    253  1.1  macallan 	sc->sc_gc.gc_rop = R128_ROP3_S;
    254  1.1  macallan #endif
    255  1.1  macallan 
    256  1.1  macallan 	if (is_console) {
    257  1.1  macallan 		vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
    258  1.1  macallan 		    &defattr);
    259  1.1  macallan 		sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    260  1.1  macallan 
    261  1.1  macallan #if notyet
    262  1.1  macallan 		gffb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height,
    263  1.1  macallan 		    ri->ri_devcmap[(defattr >> 16) & 0xff]);
    264  1.1  macallan #else
    265  1.1  macallan 		memset(sc->sc_fbaddr,
    266  1.1  macallan 		    ri->ri_devcmap[(defattr >> 16) & 0xff],
    267  1.1  macallan 		    sc->sc_height * sc->sc_stride);
    268  1.1  macallan #endif
    269  1.1  macallan 		sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
    270  1.1  macallan 		sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
    271  1.1  macallan 		sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
    272  1.1  macallan 		sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
    273  1.1  macallan #if notyet
    274  1.1  macallan 		glyphcache_init(&sc->sc_gc, sc->sc_height + 5,
    275  1.1  macallan 				(0x800000 / sc->sc_stride) - sc->sc_height - 5,
    276  1.1  macallan 				sc->sc_width,
    277  1.1  macallan 				ri->ri_font->fontwidth,
    278  1.1  macallan 				ri->ri_font->fontheight,
    279  1.1  macallan 				defattr);
    280  1.1  macallan #endif
    281  1.1  macallan 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
    282  1.1  macallan 		    defattr);
    283  1.1  macallan 		vcons_replay_msgbuf(&sc->sc_console_screen);
    284  1.1  macallan 	} else {
    285  1.1  macallan 		/*
    286  1.1  macallan 		 * since we're not the console we can postpone the rest
    287  1.1  macallan 		 * until someone actually allocates a screen for us
    288  1.1  macallan 		 */
    289  1.1  macallan 		if (sc->sc_console_screen.scr_ri.ri_rows == 0) {
    290  1.1  macallan 			/* do some minimal setup to avoid weirdnesses later */
    291  1.1  macallan 			vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
    292  1.1  macallan 			    &defattr);
    293  1.1  macallan 		} else
    294  1.1  macallan 			(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
    295  1.1  macallan #if notyet
    296  1.1  macallan 		glyphcache_init(&sc->sc_gc, sc->sc_height + 5,
    297  1.1  macallan 				(0x800000 / sc->sc_stride) - sc->sc_height - 5,
    298  1.1  macallan 				sc->sc_width,
    299  1.1  macallan 				ri->ri_font->fontwidth,
    300  1.1  macallan 				ri->ri_font->fontheight,
    301  1.1  macallan 				defattr);
    302  1.1  macallan #endif
    303  1.1  macallan 	}
    304  1.1  macallan 
    305  1.1  macallan 	j = 0;
    306  1.1  macallan 	rasops_get_cmap(ri, cmap, sizeof(cmap));
    307  1.1  macallan 	for (i = 0; i < 256; i++) {
    308  1.1  macallan 		sc->sc_cmap_red[i] = cmap[j];
    309  1.1  macallan 		sc->sc_cmap_green[i] = cmap[j + 1];
    310  1.1  macallan 		sc->sc_cmap_blue[i] = cmap[j + 2];
    311  1.1  macallan 		gffb_putpalreg(sc, i, cmap[j], cmap[j + 1], cmap[j + 2]);
    312  1.1  macallan 		j += 3;
    313  1.1  macallan 	}
    314  1.1  macallan 
    315  1.1  macallan 	/* no suspend/resume support yet */
    316  1.1  macallan 	pmf_device_register(sc->sc_dev, NULL, NULL);
    317  1.1  macallan 
    318  1.1  macallan 	aa.console = is_console;
    319  1.1  macallan 	aa.scrdata = &sc->sc_screenlist;
    320  1.1  macallan 	aa.accessops = &gffb_accessops;
    321  1.1  macallan 	aa.accesscookie = &sc->vd;
    322  1.1  macallan 
    323  1.1  macallan 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
    324  1.1  macallan }
    325  1.1  macallan 
    326  1.1  macallan static int
    327  1.1  macallan gffb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    328  1.1  macallan 	struct lwp *l)
    329  1.1  macallan {
    330  1.1  macallan 	struct vcons_data *vd = v;
    331  1.1  macallan 	struct gffb_softc *sc = vd->cookie;
    332  1.1  macallan 	struct wsdisplay_fbinfo *wdf;
    333  1.1  macallan 	struct vcons_screen *ms = vd->active;
    334  1.1  macallan 
    335  1.1  macallan 	switch (cmd) {
    336  1.1  macallan 	case WSDISPLAYIO_GTYPE:
    337  1.1  macallan 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
    338  1.1  macallan 		return 0;
    339  1.1  macallan 
    340  1.1  macallan 	/* PCI config read/write passthrough. */
    341  1.1  macallan 	case PCI_IOC_CFGREAD:
    342  1.1  macallan 	case PCI_IOC_CFGWRITE:
    343  1.1  macallan 		return pci_devioctl(sc->sc_pc, sc->sc_pcitag,
    344  1.1  macallan 		    cmd, data, flag, l);
    345  1.1  macallan 
    346  1.1  macallan 	case WSDISPLAYIO_GET_BUSID:
    347  1.1  macallan 		return wsdisplayio_busid_pci(sc->sc_dev, sc->sc_pc,
    348  1.1  macallan 		    sc->sc_pcitag, data);
    349  1.1  macallan 
    350  1.1  macallan 	case WSDISPLAYIO_GINFO:
    351  1.1  macallan 		if (ms == NULL)
    352  1.1  macallan 			return ENODEV;
    353  1.1  macallan 		wdf = (void *)data;
    354  1.1  macallan 		wdf->height = ms->scr_ri.ri_height;
    355  1.1  macallan 		wdf->width = ms->scr_ri.ri_width;
    356  1.1  macallan 		wdf->depth = ms->scr_ri.ri_depth;
    357  1.1  macallan 		wdf->cmsize = 256;
    358  1.1  macallan 		return 0;
    359  1.1  macallan 
    360  1.1  macallan 	case WSDISPLAYIO_GETCMAP:
    361  1.1  macallan 		return gffb_getcmap(sc,
    362  1.1  macallan 		    (struct wsdisplay_cmap *)data);
    363  1.1  macallan 
    364  1.1  macallan 	case WSDISPLAYIO_PUTCMAP:
    365  1.1  macallan 		return gffb_putcmap(sc,
    366  1.1  macallan 		    (struct wsdisplay_cmap *)data);
    367  1.1  macallan 
    368  1.1  macallan 	case WSDISPLAYIO_LINEBYTES:
    369  1.1  macallan 		*(u_int *)data = sc->sc_stride;
    370  1.1  macallan 		return 0;
    371  1.1  macallan 
    372  1.1  macallan 	case WSDISPLAYIO_SMODE: {
    373  1.1  macallan 		int new_mode = *(int*)data;
    374  1.1  macallan 		if (new_mode != sc->sc_mode) {
    375  1.1  macallan 			sc->sc_mode = new_mode;
    376  1.1  macallan 			if(new_mode == WSDISPLAYIO_MODE_EMUL) {
    377  1.1  macallan 				gffb_init(sc);
    378  1.1  macallan 				gffb_restore_palette(sc);
    379  1.1  macallan #if notyet
    380  1.1  macallan 				glyphcache_wipe(&sc->sc_gc);
    381  1.1  macallan 				gffb_rectfill(sc, 0, 0, sc->sc_width,
    382  1.1  macallan 				    sc->sc_height, ms->scr_ri.ri_devcmap[
    383  1.1  macallan 				    (ms->scr_defattr >> 16) & 0xff]);
    384  1.1  macallan #endif
    385  1.1  macallan 				vcons_redraw_screen(ms);
    386  1.1  macallan 			}
    387  1.1  macallan 		}
    388  1.1  macallan 		}
    389  1.1  macallan 		return 0;
    390  1.1  macallan #if notyet
    391  1.1  macallan 	case WSDISPLAYIO_GET_EDID: {
    392  1.1  macallan 		struct wsdisplayio_edid_info *d = data;
    393  1.1  macallan 		return wsdisplayio_get_edid(sc->sc_dev, d);
    394  1.1  macallan 	}
    395  1.1  macallan #endif
    396  1.1  macallan 	}
    397  1.1  macallan 	return EPASSTHROUGH;
    398  1.1  macallan }
    399  1.1  macallan 
    400  1.1  macallan static paddr_t
    401  1.1  macallan gffb_mmap(void *v, void *vs, off_t offset, int prot)
    402  1.1  macallan {
    403  1.1  macallan 	struct vcons_data *vd = v;
    404  1.1  macallan 	struct gffb_softc *sc = vd->cookie;
    405  1.1  macallan 	paddr_t pa;
    406  1.1  macallan 
    407  1.1  macallan 	/* 'regular' framebuffer mmap()ing */
    408  1.1  macallan 	if (offset < sc->sc_fbsize) {
    409  1.1  macallan 		pa = bus_space_mmap(sc->sc_memt, sc->sc_fb + offset, 0, prot,
    410  1.1  macallan 		    BUS_SPACE_MAP_LINEAR);
    411  1.1  macallan 		return pa;
    412  1.1  macallan 	}
    413  1.1  macallan 
    414  1.1  macallan 	/*
    415  1.1  macallan 	 * restrict all other mappings to processes with superuser privileges
    416  1.1  macallan 	 * or the kernel itself
    417  1.1  macallan 	 */
    418  1.1  macallan 	if (kauth_authorize_machdep(kauth_cred_get(), KAUTH_MACHDEP_UNMANAGEDMEM,
    419  1.1  macallan 	    NULL, NULL, NULL, NULL) != 0) {
    420  1.1  macallan 		aprint_normal("%s: mmap() rejected.\n",
    421  1.1  macallan 		    device_xname(sc->sc_dev));
    422  1.1  macallan 		return -1;
    423  1.1  macallan 	}
    424  1.1  macallan 
    425  1.1  macallan 	if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
    426  1.1  macallan 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    427  1.1  macallan 		    BUS_SPACE_MAP_LINEAR);
    428  1.1  macallan 		return pa;
    429  1.1  macallan 	}
    430  1.1  macallan 
    431  1.1  macallan 	if ((offset >= sc->sc_reg) &&
    432  1.1  macallan 	    (offset < (sc->sc_reg + sc->sc_regsize))) {
    433  1.1  macallan 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    434  1.1  macallan 		    BUS_SPACE_MAP_LINEAR);
    435  1.1  macallan 		return pa;
    436  1.1  macallan 	}
    437  1.1  macallan 
    438  1.1  macallan #ifdef PCI_MAGIC_IO_RANGE
    439  1.1  macallan 	/* allow mapping of IO space */
    440  1.1  macallan 	if ((offset >= PCI_MAGIC_IO_RANGE) &&
    441  1.1  macallan 	    (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
    442  1.1  macallan 		pa = bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
    443  1.1  macallan 		    0, prot, BUS_SPACE_MAP_LINEAR);
    444  1.1  macallan 		return pa;
    445  1.1  macallan 	}
    446  1.1  macallan #endif
    447  1.1  macallan 
    448  1.1  macallan #ifdef OFB_ALLOW_OTHERS
    449  1.1  macallan 	if (offset >= 0x80000000) {
    450  1.1  macallan 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    451  1.1  macallan 		    BUS_SPACE_MAP_LINEAR);
    452  1.1  macallan 		return pa;
    453  1.1  macallan 	}
    454  1.1  macallan #endif
    455  1.1  macallan 	return -1;
    456  1.1  macallan }
    457  1.1  macallan 
    458  1.1  macallan static void
    459  1.1  macallan gffb_init_screen(void *cookie, struct vcons_screen *scr,
    460  1.1  macallan     int existing, long *defattr)
    461  1.1  macallan {
    462  1.1  macallan 	struct gffb_softc *sc = cookie;
    463  1.1  macallan 	struct rasops_info *ri = &scr->scr_ri;
    464  1.1  macallan 
    465  1.1  macallan 	ri->ri_depth = sc->sc_depth;
    466  1.1  macallan 	ri->ri_width = sc->sc_width;
    467  1.1  macallan 	ri->ri_height = sc->sc_height;
    468  1.1  macallan 	ri->ri_stride = sc->sc_stride;
    469  1.1  macallan 	ri->ri_bits = sc->sc_fbaddr;
    470  1.1  macallan 	ri->ri_flg = RI_CENTER;
    471  1.1  macallan 	if (sc->sc_depth == 8)
    472  1.1  macallan 		ri->ri_flg |= RI_8BIT_IS_RGB | RI_ENABLE_ALPHA;
    473  1.1  macallan 
    474  1.1  macallan 	rasops_init(ri, 0, 0);
    475  1.1  macallan 	ri->ri_caps = WSSCREEN_WSCOLORS;
    476  1.1  macallan 	scr->scr_flags |= VCONS_DONT_READ;
    477  1.1  macallan 
    478  1.1  macallan 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    479  1.1  macallan 		    sc->sc_width / ri->ri_font->fontwidth);
    480  1.1  macallan 
    481  1.1  macallan 	ri->ri_hw = scr;
    482  1.1  macallan #if notyet
    483  1.1  macallan 	ri->ri_ops.copyrows = gffb_copyrows;
    484  1.1  macallan 	ri->ri_ops.copycols = gffb_copycols;
    485  1.1  macallan 	ri->ri_ops.eraserows = gffb_eraserows;
    486  1.1  macallan 	ri->ri_ops.erasecols = gffb_erasecols;
    487  1.1  macallan 	ri->ri_ops.cursor = gffb_cursor;
    488  1.1  macallan 	if (FONT_IS_ALPHA(ri->ri_font)) {
    489  1.1  macallan 		ri->ri_ops.putchar = gffb_putchar_aa;
    490  1.1  macallan 	} else
    491  1.1  macallan 		ri->ri_ops.putchar = gffb_putchar;
    492  1.1  macallan #endif
    493  1.1  macallan }
    494  1.1  macallan 
    495  1.1  macallan static int
    496  1.1  macallan gffb_putcmap(struct gffb_softc *sc, struct wsdisplay_cmap *cm)
    497  1.1  macallan {
    498  1.1  macallan 	u_char *r, *g, *b;
    499  1.1  macallan 	u_int index = cm->index;
    500  1.1  macallan 	u_int count = cm->count;
    501  1.1  macallan 	int i, error;
    502  1.1  macallan 	u_char rbuf[256], gbuf[256], bbuf[256];
    503  1.1  macallan 
    504  1.1  macallan #ifdef R128FB_DEBUG
    505  1.1  macallan 	aprint_debug("putcmap: %d %d\n",index, count);
    506  1.1  macallan #endif
    507  1.1  macallan 	if (cm->index >= 256 || cm->count > 256 ||
    508  1.1  macallan 	    (cm->index + cm->count) > 256)
    509  1.1  macallan 		return EINVAL;
    510  1.1  macallan 	error = copyin(cm->red, &rbuf[index], count);
    511  1.1  macallan 	if (error)
    512  1.1  macallan 		return error;
    513  1.1  macallan 	error = copyin(cm->green, &gbuf[index], count);
    514  1.1  macallan 	if (error)
    515  1.1  macallan 		return error;
    516  1.1  macallan 	error = copyin(cm->blue, &bbuf[index], count);
    517  1.1  macallan 	if (error)
    518  1.1  macallan 		return error;
    519  1.1  macallan 
    520  1.1  macallan 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    521  1.1  macallan 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    522  1.1  macallan 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    523  1.1  macallan 
    524  1.1  macallan 	r = &sc->sc_cmap_red[index];
    525  1.1  macallan 	g = &sc->sc_cmap_green[index];
    526  1.1  macallan 	b = &sc->sc_cmap_blue[index];
    527  1.1  macallan 
    528  1.1  macallan 	for (i = 0; i < count; i++) {
    529  1.1  macallan 		gffb_putpalreg(sc, index, *r, *g, *b);
    530  1.1  macallan 		index++;
    531  1.1  macallan 		r++, g++, b++;
    532  1.1  macallan 	}
    533  1.1  macallan 	return 0;
    534  1.1  macallan }
    535  1.1  macallan 
    536  1.1  macallan static int
    537  1.1  macallan gffb_getcmap(struct gffb_softc *sc, struct wsdisplay_cmap *cm)
    538  1.1  macallan {
    539  1.1  macallan 	u_int index = cm->index;
    540  1.1  macallan 	u_int count = cm->count;
    541  1.1  macallan 	int error;
    542  1.1  macallan 
    543  1.1  macallan 	if (index >= 255 || count > 256 || index + count > 256)
    544  1.1  macallan 		return EINVAL;
    545  1.1  macallan 
    546  1.1  macallan 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    547  1.1  macallan 	if (error)
    548  1.1  macallan 		return error;
    549  1.1  macallan 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    550  1.1  macallan 	if (error)
    551  1.1  macallan 		return error;
    552  1.1  macallan 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    553  1.1  macallan 	if (error)
    554  1.1  macallan 		return error;
    555  1.1  macallan 
    556  1.1  macallan 	return 0;
    557  1.1  macallan }
    558  1.1  macallan 
    559  1.1  macallan static void
    560  1.1  macallan gffb_restore_palette(struct gffb_softc *sc)
    561  1.1  macallan {
    562  1.1  macallan 	int i;
    563  1.1  macallan 
    564  1.1  macallan 	for (i = 0; i < (1 << sc->sc_depth); i++) {
    565  1.1  macallan 		gffb_putpalreg(sc, i, sc->sc_cmap_red[i],
    566  1.1  macallan 		    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
    567  1.1  macallan 	}
    568  1.1  macallan }
    569  1.1  macallan 
    570  1.1  macallan static int
    571  1.1  macallan gffb_putpalreg(struct gffb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
    572  1.1  macallan     uint8_t b)
    573  1.1  macallan {
    574  1.1  macallan 	/* port 0 */
    575  1.1  macallan 	bus_space_write_1(sc->sc_memt, sc->sc_regh,
    576  1.1  macallan 	    GFFB_PDIO0 + GFFB_PEL_IW, idx);
    577  1.1  macallan 	bus_space_write_1(sc->sc_memt, sc->sc_regh,
    578  1.1  macallan 	    GFFB_PDIO0 + GFFB_PEL_D, r);
    579  1.1  macallan 	bus_space_write_1(sc->sc_memt, sc->sc_regh,
    580  1.1  macallan 	    GFFB_PDIO0 + GFFB_PEL_D, g);
    581  1.1  macallan 	bus_space_write_1(sc->sc_memt, sc->sc_regh,
    582  1.1  macallan 	    GFFB_PDIO0 + GFFB_PEL_D, b);
    583  1.1  macallan 
    584  1.1  macallan 	/* port 1 */
    585  1.1  macallan 	bus_space_write_1(sc->sc_memt, sc->sc_regh,
    586  1.1  macallan 	    GFFB_PDIO1 + GFFB_PEL_IW, idx);
    587  1.1  macallan 	bus_space_write_1(sc->sc_memt, sc->sc_regh,
    588  1.1  macallan 	    GFFB_PDIO1 + GFFB_PEL_D, r);
    589  1.1  macallan 	bus_space_write_1(sc->sc_memt, sc->sc_regh,
    590  1.1  macallan 	    GFFB_PDIO1 + GFFB_PEL_D, g);
    591  1.1  macallan 	bus_space_write_1(sc->sc_memt, sc->sc_regh,
    592  1.1  macallan 	    GFFB_PDIO1 + GFFB_PEL_D, b);
    593  1.1  macallan 
    594  1.1  macallan 	return 0;
    595  1.1  macallan }
    596  1.1  macallan 
    597  1.1  macallan static void
    598  1.1  macallan gffb_init(struct gffb_softc *sc)
    599  1.1  macallan {
    600  1.1  macallan #ifdef GFFB_DEBUG
    601  1.1  macallan 	int i, j;
    602  1.1  macallan 
    603  1.1  macallan 	/* dump the RAMDAC1 register space */
    604  1.1  macallan 	for (i = 0; i < 0x2000; i+= 32) {
    605  1.1  macallan 		printf("%04x:", i);
    606  1.1  macallan 		for (j = 0; j < 32; j += 4) {
    607  1.1  macallan 			printf(" %08x", bus_space_read_4(sc->sc_memt, sc->sc_regh, GFFB_RAMDAC1 + i + j));
    608  1.1  macallan 		}
    609  1.1  macallan 		printf("\n");
    610  1.1  macallan 	}
    611  1.1  macallan #endif
    612  1.1  macallan 
    613  1.1  macallan 	/* init display start */
    614  1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
    615  1.1  macallan 	    GFFB_CRTC0 + GFFB_DISPLAYSTART, 0);
    616  1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
    617  1.1  macallan 	    GFFB_CRTC1 + GFFB_DISPLAYSTART, 0);
    618  1.1  macallan 	bus_space_write_1(sc->sc_memt, sc->sc_regh,
    619  1.1  macallan 	    GFFB_PDIO0 + GFFB_PEL_MASK, 0xff);
    620  1.1  macallan 	bus_space_write_1(sc->sc_memt, sc->sc_regh,
    621  1.1  macallan 	    GFFB_PDIO1 + GFFB_PEL_MASK, 0xff);
    622  1.1  macallan }
    623