Home | History | Annotate | Line # | Download | only in pci
gffb.c revision 1.26
      1  1.26  macallan /*	$NetBSD: gffb.c,v 1.26 2025/08/30 07:25:10 macallan Exp $	*/
      2   1.1  macallan 
      3   1.1  macallan /*
      4   1.7  macallan  * Copyright (c) 2013 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.6  macallan  * tested on macppc only so far, should work on other hardware as long as
     31   1.6  macallan  * something sets up a usable graphics mode and sets the right device properties
     32   1.6  macallan  * This driver should work with all NV1x hardware but so far it's been tested
     33   1.6  macallan  * only on NV11 / GeForce2 MX. Needs testing with more hardware and if
     34   1.6  macallan  * successful, PCI IDs need to be added to gffb_match()
     35   1.1  macallan  */
     36   1.1  macallan 
     37   1.1  macallan #include <sys/cdefs.h>
     38  1.26  macallan __KERNEL_RCSID(0, "$NetBSD: gffb.c,v 1.26 2025/08/30 07:25:10 macallan Exp $");
     39   1.1  macallan 
     40   1.1  macallan #include <sys/param.h>
     41   1.1  macallan #include <sys/systm.h>
     42   1.1  macallan #include <sys/kernel.h>
     43   1.1  macallan #include <sys/device.h>
     44   1.1  macallan #include <sys/lwp.h>
     45   1.1  macallan #include <sys/kauth.h>
     46   1.2  macallan #include <sys/atomic.h>
     47   1.1  macallan 
     48   1.1  macallan #include <dev/pci/pcivar.h>
     49   1.1  macallan #include <dev/pci/pcireg.h>
     50   1.1  macallan #include <dev/pci/pcidevs.h>
     51   1.1  macallan #include <dev/pci/pciio.h>
     52   1.1  macallan #include <dev/pci/gffbreg.h>
     53   1.1  macallan 
     54   1.1  macallan #include <dev/wscons/wsdisplayvar.h>
     55   1.1  macallan #include <dev/wscons/wsconsio.h>
     56   1.1  macallan #include <dev/wsfont/wsfont.h>
     57   1.1  macallan #include <dev/rasops/rasops.h>
     58   1.1  macallan #include <dev/wscons/wsdisplay_vconsvar.h>
     59   1.1  macallan #include <dev/pci/wsdisplay_pci.h>
     60   1.1  macallan #include <dev/wscons/wsdisplay_glyphcachevar.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.2  macallan 	size_t sc_vramsize;
     85  1.13  macallan 	uint32_t sc_fboffset;
     86   1.1  macallan 
     87   1.1  macallan 	int sc_width, sc_height, sc_depth, sc_stride;
     88  1.13  macallan 	int sc_locked, sc_accel;
     89   1.1  macallan 	struct vcons_screen sc_console_screen;
     90   1.1  macallan 	struct wsscreen_descr sc_defaultscreen_descr;
     91   1.1  macallan 	const struct wsscreen_descr *sc_screens[1];
     92   1.1  macallan 	struct wsscreen_list sc_screenlist;
     93   1.1  macallan 	struct vcons_data vd;
     94  1.25  macallan 	int sc_mode, sc_arch;
     95   1.1  macallan 	u_char sc_cmap_red[256];
     96   1.1  macallan 	u_char sc_cmap_green[256];
     97   1.1  macallan 	u_char sc_cmap_blue[256];
     98   1.2  macallan 	int sc_put, sc_current, sc_free;
     99   1.3  macallan 	uint32_t sc_rop;
    100   1.3  macallan 	void (*sc_putchar)(void *, int, int, u_int, long);
    101   1.3  macallan 	kmutex_t sc_lock;
    102   1.1  macallan 	glyphcache sc_gc;
    103   1.1  macallan };
    104   1.1  macallan 
    105   1.1  macallan static int	gffb_match(device_t, cfdata_t, void *);
    106   1.1  macallan static void	gffb_attach(device_t, device_t, void *);
    107   1.1  macallan 
    108   1.1  macallan CFATTACH_DECL_NEW(gffb, sizeof(struct gffb_softc),
    109   1.1  macallan     gffb_match, gffb_attach, NULL, NULL);
    110   1.1  macallan 
    111  1.11   msaitoh static int	gffb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
    112   1.1  macallan static paddr_t	gffb_mmap(void *, void *, off_t, int);
    113   1.1  macallan static void	gffb_init_screen(void *, struct vcons_screen *, int, long *);
    114   1.1  macallan 
    115   1.1  macallan static int	gffb_putcmap(struct gffb_softc *, struct wsdisplay_cmap *);
    116   1.1  macallan static int 	gffb_getcmap(struct gffb_softc *, struct wsdisplay_cmap *);
    117   1.1  macallan static void	gffb_restore_palette(struct gffb_softc *);
    118   1.1  macallan static int 	gffb_putpalreg(struct gffb_softc *, uint8_t, uint8_t,
    119   1.1  macallan 			    uint8_t, uint8_t);
    120   1.1  macallan 
    121  1.25  macallan  void	gffb_init(struct gffb_softc *);
    122   1.1  macallan 
    123   1.2  macallan static void	gffb_make_room(struct gffb_softc *, int);
    124   1.3  macallan static void	gffb_sync(struct gffb_softc *);
    125   1.2  macallan 
    126   1.1  macallan static void	gffb_rectfill(struct gffb_softc *, int, int, int, int,
    127   1.1  macallan 			    uint32_t);
    128  1.11   msaitoh static void	gffb_bitblt(void *, int, int, int, int, int, int, int);
    129   1.3  macallan static void	gffb_rop(struct gffb_softc *, int);
    130   1.1  macallan 
    131   1.1  macallan static void	gffb_cursor(void *, int, int, int);
    132   1.1  macallan static void	gffb_putchar(void *, int, int, u_int, long);
    133   1.1  macallan static void	gffb_copycols(void *, int, int, int, int);
    134   1.1  macallan static void	gffb_erasecols(void *, int, int, int, long);
    135   1.1  macallan static void	gffb_copyrows(void *, int, int, int);
    136   1.1  macallan static void	gffb_eraserows(void *, int, int, long);
    137   1.1  macallan 
    138  1.25  macallan #define GFFB_READ_4(o) bus_space_read_stream_4(sc->sc_memt, sc->sc_regh, (o))
    139  1.25  macallan #define GFFB_WRITE_4(o, v) bus_space_write_stream_4(sc->sc_memt, sc->sc_regh, (o), (v))
    140  1.26  macallan #define GFFB_WRITE_1(o, v) bus_space_write_1(sc->sc_memt, sc->sc_regh, (o), (v))
    141   1.4  macallan 
    142   1.1  macallan struct wsdisplay_accessops gffb_accessops = {
    143   1.1  macallan 	gffb_ioctl,
    144   1.1  macallan 	gffb_mmap,
    145   1.1  macallan 	NULL,	/* alloc_screen */
    146   1.1  macallan 	NULL,	/* free_screen */
    147   1.1  macallan 	NULL,	/* show_screen */
    148   1.1  macallan 	NULL, 	/* load_font */
    149   1.1  macallan 	NULL,	/* pollc */
    150   1.1  macallan 	NULL	/* scroll */
    151   1.1  macallan };
    152   1.1  macallan 
    153   1.1  macallan static int
    154   1.1  macallan gffb_match(device_t parent, cfdata_t match, void *aux)
    155   1.1  macallan {
    156   1.1  macallan 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    157   1.1  macallan 
    158   1.1  macallan 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY)
    159   1.1  macallan 		return 0;
    160   1.1  macallan 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NVIDIA)
    161   1.1  macallan 		return 0;
    162   1.1  macallan 
    163  1.25  macallan 	/* only cards tested on so far - likely needs a list */
    164   1.1  macallan 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_NVIDIA_GEFORCE2MX)
    165   1.1  macallan 		return 100;
    166  1.13  macallan 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_NVIDIA_GEFORCE_6800U)
    167  1.13  macallan 		return 100;
    168  1.15  macallan 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_NVIDIA_GF_FXGO5200)
    169  1.15  macallan 		return 100;
    170  1.23  macallan 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_NVIDIA_GF_FX5200U)
    171  1.23  macallan 		return 100;
    172   1.1  macallan 	return (0);
    173   1.1  macallan }
    174   1.1  macallan 
    175   1.1  macallan static void
    176   1.1  macallan gffb_attach(device_t parent, device_t self, void *aux)
    177   1.1  macallan {
    178   1.1  macallan 	struct gffb_softc	*sc = device_private(self);
    179   1.1  macallan 	struct pci_attach_args	*pa = aux;
    180   1.1  macallan 	struct rasops_info	*ri;
    181   1.1  macallan 	bus_space_tag_t		tag;
    182   1.1  macallan 	struct wsemuldisplaydev_attach_args aa;
    183   1.1  macallan 	prop_dictionary_t	dict;
    184   1.1  macallan 	unsigned long		defattr;
    185  1.13  macallan 	pcireg_t		reg;
    186  1.10  macallan 	bool			is_console = FALSE;
    187  1.13  macallan 	uint32_t		addr;
    188   1.6  macallan 	int			i, j, f;
    189   1.1  macallan 	uint8_t			cmap[768];
    190   1.1  macallan 
    191   1.1  macallan 	sc->sc_pc = pa->pa_pc;
    192   1.1  macallan 	sc->sc_pcitag = pa->pa_tag;
    193   1.1  macallan 	sc->sc_memt = pa->pa_memt;
    194   1.1  macallan 	sc->sc_iot = pa->pa_iot;
    195   1.1  macallan 	sc->sc_dev = self;
    196   1.1  macallan 
    197  1.13  macallan 	/* first, see what kind of chip we've got */
    198  1.13  macallan 	reg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, PCI_ID_REG);
    199  1.25  macallan 	switch (PCI_PRODUCT(reg)) {
    200  1.25  macallan 		case PCI_PRODUCT_NVIDIA_GEFORCE2MX:
    201  1.25  macallan 			sc->sc_accel = true;
    202  1.25  macallan 			sc->sc_arch = 10;
    203  1.25  macallan 			break;
    204  1.25  macallan 		case PCI_PRODUCT_NVIDIA_GEFORCE_6800U:
    205  1.25  macallan 			sc->sc_accel = false;
    206  1.25  macallan 			sc->sc_arch = 40;
    207  1.25  macallan 			break;
    208  1.25  macallan 		case PCI_PRODUCT_NVIDIA_GF_FXGO5200:
    209  1.25  macallan 		case PCI_PRODUCT_NVIDIA_GF_FX5200U:
    210  1.25  macallan 			sc->sc_accel = true;
    211  1.25  macallan 			sc->sc_arch = 30;
    212  1.25  macallan 			break;
    213  1.25  macallan 		default:
    214  1.25  macallan 			sc->sc_accel = false;
    215  1.25  macallan 			sc->sc_arch = 0;
    216  1.25  macallan 	}
    217  1.13  macallan 
    218   1.1  macallan 	pci_aprint_devinfo(pa, NULL);
    219  1.25  macallan 	DPRINTF("%s accel %d arch %d\n", __func__, sc->sc_accel, sc->sc_arch);
    220   1.1  macallan 	/* fill in parameters from properties */
    221   1.1  macallan 	dict = device_properties(self);
    222   1.1  macallan 	if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) {
    223   1.1  macallan 		aprint_error("%s: no width property\n", device_xname(self));
    224   1.1  macallan 		return;
    225   1.1  macallan 	}
    226   1.1  macallan 	if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) {
    227   1.1  macallan 		aprint_error("%s: no height property\n", device_xname(self));
    228   1.1  macallan 		return;
    229   1.1  macallan 	}
    230   1.1  macallan 
    231   1.1  macallan #ifdef GLYPHCACHE_DEBUG
    232   1.1  macallan 	/* leave some visible VRAM unused so we can see the glyph cache */
    233   1.3  macallan 	sc->sc_height -= 300;
    234   1.1  macallan #endif
    235   1.1  macallan 
    236   1.1  macallan 	if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth)) {
    237   1.1  macallan 		aprint_error("%s: no depth property\n", device_xname(self));
    238   1.1  macallan 		return;
    239   1.1  macallan 	}
    240   1.1  macallan 	if (!prop_dictionary_get_uint32(dict, "linebytes", &sc->sc_stride)) {
    241   1.1  macallan 		aprint_error("%s: no linebytes property\n",
    242   1.1  macallan 		    device_xname(self));
    243   1.1  macallan 		return;
    244   1.1  macallan 	}
    245   1.1  macallan 
    246  1.13  macallan 	/*
    247  1.13  macallan 	 * on !2MX we need to use the firmware's offset - for some reason
    248  1.13  macallan 	 * register writes to anything other than the DACs go wrong
    249  1.13  macallan 	 */
    250  1.13  macallan 	sc->sc_fboffset = 0;
    251  1.13  macallan 	if (prop_dictionary_get_uint32(dict, "address", &addr)) {
    252  1.13  macallan 		sc->sc_fboffset = addr & 0x000fffff;	/* XXX */
    253  1.13  macallan 	}
    254  1.25  macallan 	DPRINTF("%s: fboffset %8x\n", __func__, sc->sc_fboffset);
    255   1.1  macallan 	prop_dictionary_get_bool(dict, "is_console", &is_console);
    256   1.1  macallan 
    257   1.6  macallan 	if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_MEM, 0,
    258   1.6  macallan 	    &tag, &sc->sc_regh, &sc->sc_reg, &sc->sc_regsize)) {
    259   1.6  macallan 		aprint_error("%s: failed to map registers.\n",
    260   1.6  macallan 		    device_xname(sc->sc_dev));
    261   1.6  macallan 	}
    262  1.25  macallan 	/*
    263  1.25  macallan 	 * first thing we need to make sure register access uses host byte order
    264  1.25  macallan 	 * so we can recycle as much of xf86-video-nv as possible
    265  1.25  macallan 	 */
    266  1.25  macallan #if BYTE_ORDER == BIG_ENDIAN
    267  1.25  macallan 	uint32_t mreg = GFFB_READ_4(GFFB_PMC + 4);
    268  1.25  macallan 	if ((mreg & 0x01000001) == 0) {
    269  1.25  macallan 		GFFB_WRITE_4(GFFB_PMC + 4, 0x01000001);
    270  1.25  macallan 	}
    271  1.25  macallan #endif
    272   1.6  macallan 	sc->sc_vramsize = GFFB_READ_4(GFFB_VRAM) & 0xfff00000;
    273   1.6  macallan 
    274   1.6  macallan 	/* don't map more VRAM than we actually have */
    275   1.6  macallan 	if (pci_mapreg_info(sc->sc_pc, sc->sc_pcitag,
    276   1.6  macallan 	    0x14, PCI_MAPREG_TYPE_MEM, &sc->sc_fb, &sc->sc_fbsize, &f)) {
    277   1.6  macallan 		aprint_error("%s: can't find the framebuffer?!\n",
    278   1.6  macallan 		    device_xname(sc->sc_dev));
    279   1.6  macallan 	}
    280  1.13  macallan 	if (sc->sc_vramsize == 0) sc->sc_vramsize = sc->sc_fbsize;
    281   1.6  macallan 
    282  1.13  macallan 	/* don't map (much) more than we actually need */
    283  1.13  macallan 	if (bus_space_map(sc->sc_memt, sc->sc_fb, 0x1000000,
    284   1.1  macallan 	    BUS_SPACE_MAP_PREFETCHABLE | BUS_SPACE_MAP_LINEAR,
    285   1.6  macallan 	    &sc->sc_fbh)) {
    286   1.1  macallan 		aprint_error("%s: failed to map the framebuffer.\n",
    287   1.1  macallan 		    device_xname(sc->sc_dev));
    288   1.1  macallan 	}
    289   1.1  macallan 	sc->sc_fbaddr = bus_space_vaddr(tag, sc->sc_fbh);
    290   1.1  macallan 
    291   1.1  macallan 	aprint_normal("%s: %d MB aperture at 0x%08x\n", device_xname(self),
    292   1.1  macallan 	    (int)(sc->sc_fbsize >> 20), (uint32_t)sc->sc_fb);
    293   1.6  macallan 	aprint_normal_dev(sc->sc_dev, "%d MB video memory\n",
    294   1.9   tsutsui 	    (int)(sc->sc_vramsize >> 20));
    295   1.1  macallan 
    296   1.1  macallan 	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
    297   1.1  macallan 		"default",
    298   1.1  macallan 		0, 0,
    299   1.1  macallan 		NULL,
    300   1.1  macallan 		8, 16,
    301  1.24  macallan 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_RESIZE,
    302   1.1  macallan 		NULL
    303   1.1  macallan 	};
    304   1.1  macallan 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
    305   1.1  macallan 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
    306   1.1  macallan 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    307   1.1  macallan 	sc->sc_locked = 0;
    308   1.1  macallan 
    309   1.3  macallan #ifdef GFFB_DEBUG
    310   1.4  macallan 	printf("put: %08x\n", GFFB_READ_4(GFFB_FIFO_PUT));
    311   1.4  macallan 	printf("get: %08x\n", GFFB_READ_4(GFFB_FIFO_GET));
    312   1.3  macallan #endif
    313   1.3  macallan 
    314   1.3  macallan 	/*
    315   1.3  macallan 	 * we don't have hardware synchronization so we need a lock to serialize
    316   1.3  macallan 	 * access to the DMA buffer between normal and kernel output
    317   1.3  macallan 	 * actually it might be enough to use atomic ops on sc_current, sc_free
    318   1.3  macallan 	 * etc. but for now we'll play it safe
    319   1.3  macallan 	 * XXX we will probably deadlock if we take an interrupt while sc_lock
    320   1.3  macallan 	 * is held and then try to printf()
    321   1.3  macallan 	 */
    322   1.3  macallan 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    323   1.3  macallan 
    324  1.11   msaitoh 	/* init engine here */
    325   1.1  macallan 	gffb_init(sc);
    326   1.3  macallan 
    327  1.25  macallan #ifdef GFFB_DEBUG
    328  1.25  macallan 	printf("put: %08x\n", GFFB_READ_4(GFFB_FIFO_PUT));
    329  1.25  macallan 	printf("get: %08x\n", GFFB_READ_4(GFFB_FIFO_GET));
    330  1.25  macallan #endif
    331   1.3  macallan 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
    332   1.3  macallan 	    &gffb_accessops);
    333   1.3  macallan 	sc->vd.init_screen = gffb_init_screen;
    334   1.3  macallan 
    335   1.1  macallan 
    336   1.1  macallan 	ri = &sc->sc_console_screen.scr_ri;
    337   1.1  macallan 
    338  1.13  macallan 	if (sc->sc_accel) {
    339  1.13  macallan 		sc->sc_gc.gc_bitblt = gffb_bitblt;
    340  1.13  macallan 		sc->sc_gc.gc_blitcookie = sc;
    341  1.13  macallan 		sc->sc_gc.gc_rop = 0xcc;
    342  1.24  macallan 		sc->vd.show_screen_cookie = &sc->sc_gc;
    343  1.24  macallan 		sc->vd.show_screen_cb = glyphcache_adapt;
    344  1.13  macallan 	}
    345   1.1  macallan 
    346   1.1  macallan 	if (is_console) {
    347   1.1  macallan 		vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
    348   1.1  macallan 		    &defattr);
    349   1.1  macallan 		sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    350   1.1  macallan 
    351  1.13  macallan 		if (sc->sc_accel) {
    352  1.13  macallan 			gffb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height,
    353  1.13  macallan 		    		ri->ri_devcmap[(defattr >> 16) & 0xf]);
    354  1.13  macallan 		} else {
    355  1.13  macallan 			memset(sc->sc_fbaddr + sc->sc_fboffset,
    356  1.13  macallan 			       ri->ri_devcmap[(defattr >> 16) & 0xf],
    357  1.13  macallan 			       sc->sc_stride * sc->sc_height);
    358  1.13  macallan 		}
    359   1.1  macallan 		sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
    360   1.1  macallan 		sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
    361   1.1  macallan 		sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
    362   1.1  macallan 		sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
    363  1.11   msaitoh 
    364  1.13  macallan 		if (sc->sc_accel)
    365  1.13  macallan 			glyphcache_init(&sc->sc_gc, sc->sc_height + 5,
    366   1.1  macallan 				(0x800000 / sc->sc_stride) - sc->sc_height - 5,
    367   1.1  macallan 				sc->sc_width,
    368   1.1  macallan 				ri->ri_font->fontwidth,
    369   1.1  macallan 				ri->ri_font->fontheight,
    370   1.1  macallan 				defattr);
    371  1.11   msaitoh 
    372   1.1  macallan 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
    373   1.1  macallan 		    defattr);
    374   1.1  macallan 		vcons_replay_msgbuf(&sc->sc_console_screen);
    375   1.1  macallan 	} else {
    376   1.1  macallan 		/*
    377   1.1  macallan 		 * since we're not the console we can postpone the rest
    378   1.1  macallan 		 * until someone actually allocates a screen for us
    379   1.1  macallan 		 */
    380   1.1  macallan 		if (sc->sc_console_screen.scr_ri.ri_rows == 0) {
    381   1.1  macallan 			/* do some minimal setup to avoid weirdnesses later */
    382   1.1  macallan 			vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
    383   1.1  macallan 			    &defattr);
    384   1.1  macallan 		} else
    385   1.1  macallan 			(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
    386  1.11   msaitoh 
    387  1.13  macallan 		if (sc->sc_accel)
    388  1.13  macallan 			glyphcache_init(&sc->sc_gc, sc->sc_height + 5,
    389   1.1  macallan 				(0x800000 / sc->sc_stride) - sc->sc_height - 5,
    390   1.1  macallan 				sc->sc_width,
    391   1.1  macallan 				ri->ri_font->fontwidth,
    392   1.1  macallan 				ri->ri_font->fontheight,
    393   1.1  macallan 				defattr);
    394   1.1  macallan 	}
    395   1.1  macallan 
    396   1.1  macallan 	j = 0;
    397   1.1  macallan 	rasops_get_cmap(ri, cmap, sizeof(cmap));
    398   1.1  macallan 	for (i = 0; i < 256; i++) {
    399   1.1  macallan 		sc->sc_cmap_red[i] = cmap[j];
    400   1.1  macallan 		sc->sc_cmap_green[i] = cmap[j + 1];
    401   1.1  macallan 		sc->sc_cmap_blue[i] = cmap[j + 2];
    402   1.1  macallan 		gffb_putpalreg(sc, i, cmap[j], cmap[j + 1], cmap[j + 2]);
    403   1.1  macallan 		j += 3;
    404   1.1  macallan 	}
    405   1.1  macallan 
    406   1.1  macallan 	/* no suspend/resume support yet */
    407  1.12      maya 	if (!pmf_device_register(sc->sc_dev, NULL, NULL))
    408  1.12      maya 		aprint_error_dev(sc->sc_dev,
    409  1.12      maya 		    "couldn't establish power handler\n");
    410   1.1  macallan 
    411   1.1  macallan 	aa.console = is_console;
    412   1.1  macallan 	aa.scrdata = &sc->sc_screenlist;
    413   1.1  macallan 	aa.accessops = &gffb_accessops;
    414   1.1  macallan 	aa.accesscookie = &sc->vd;
    415   1.1  macallan 
    416  1.17   thorpej 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint, CFARGS_NONE);
    417   1.1  macallan }
    418   1.1  macallan 
    419   1.1  macallan static int
    420  1.11   msaitoh gffb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
    421   1.1  macallan {
    422   1.1  macallan 	struct vcons_data *vd = v;
    423   1.1  macallan 	struct gffb_softc *sc = vd->cookie;
    424   1.1  macallan 	struct wsdisplay_fbinfo *wdf;
    425   1.1  macallan 	struct vcons_screen *ms = vd->active;
    426   1.1  macallan 
    427   1.1  macallan 	switch (cmd) {
    428   1.1  macallan 	case WSDISPLAYIO_GTYPE:
    429   1.1  macallan 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
    430   1.1  macallan 		return 0;
    431   1.1  macallan 
    432   1.1  macallan 	/* PCI config read/write passthrough. */
    433   1.1  macallan 	case PCI_IOC_CFGREAD:
    434   1.1  macallan 	case PCI_IOC_CFGWRITE:
    435   1.1  macallan 		return pci_devioctl(sc->sc_pc, sc->sc_pcitag,
    436   1.1  macallan 		    cmd, data, flag, l);
    437   1.1  macallan 
    438   1.1  macallan 	case WSDISPLAYIO_GET_BUSID:
    439  1.11   msaitoh 		return wsdisplayio_busid_pci(sc->sc_dev, sc->sc_pc,
    440   1.1  macallan 		    sc->sc_pcitag, data);
    441   1.1  macallan 
    442   1.1  macallan 	case WSDISPLAYIO_GINFO:
    443   1.1  macallan 		if (ms == NULL)
    444   1.1  macallan 			return ENODEV;
    445   1.1  macallan 		wdf = (void *)data;
    446   1.1  macallan 		wdf->height = ms->scr_ri.ri_height;
    447   1.1  macallan 		wdf->width = ms->scr_ri.ri_width;
    448   1.1  macallan 		wdf->depth = ms->scr_ri.ri_depth;
    449   1.1  macallan 		wdf->cmsize = 256;
    450   1.1  macallan 		return 0;
    451   1.1  macallan 
    452   1.1  macallan 	case WSDISPLAYIO_GETCMAP:
    453   1.1  macallan 		return gffb_getcmap(sc,
    454   1.1  macallan 		    (struct wsdisplay_cmap *)data);
    455   1.1  macallan 
    456   1.1  macallan 	case WSDISPLAYIO_PUTCMAP:
    457   1.1  macallan 		return gffb_putcmap(sc,
    458   1.1  macallan 		    (struct wsdisplay_cmap *)data);
    459   1.1  macallan 
    460   1.1  macallan 	case WSDISPLAYIO_LINEBYTES:
    461   1.1  macallan 		*(u_int *)data = sc->sc_stride;
    462   1.1  macallan 		return 0;
    463   1.1  macallan 
    464   1.1  macallan 	case WSDISPLAYIO_SMODE: {
    465   1.1  macallan 		int new_mode = *(int*)data;
    466   1.1  macallan 		if (new_mode != sc->sc_mode) {
    467   1.1  macallan 			sc->sc_mode = new_mode;
    468   1.1  macallan 			if(new_mode == WSDISPLAYIO_MODE_EMUL) {
    469   1.1  macallan 				gffb_init(sc);
    470   1.1  macallan 				gffb_restore_palette(sc);
    471  1.13  macallan 				if (sc->sc_accel) {
    472  1.13  macallan 					glyphcache_wipe(&sc->sc_gc);
    473  1.13  macallan 					gffb_rectfill(sc, 0, 0, sc->sc_width,
    474  1.13  macallan 					    sc->sc_height, ms->scr_ri.ri_devcmap[
    475  1.13  macallan 					    (ms->scr_defattr >> 16) & 0xf]);
    476  1.13  macallan 				} else {
    477  1.13  macallan 					memset(sc->sc_fbaddr + sc->sc_fboffset,
    478  1.13  macallan 					       ms->scr_ri.ri_devcmap[
    479  1.13  macallan 					         (ms->scr_defattr >> 16) & 0xf],
    480  1.13  macallan 					       sc->sc_stride * sc->sc_height);
    481  1.13  macallan 				}
    482   1.1  macallan 				vcons_redraw_screen(ms);
    483   1.1  macallan 			}
    484   1.1  macallan 		}
    485   1.1  macallan 		}
    486   1.1  macallan 		return 0;
    487  1.11   msaitoh 
    488   1.1  macallan 	case WSDISPLAYIO_GET_EDID: {
    489   1.1  macallan 		struct wsdisplayio_edid_info *d = data;
    490   1.1  macallan 		return wsdisplayio_get_edid(sc->sc_dev, d);
    491   1.1  macallan 	}
    492   1.5  macallan 
    493   1.5  macallan 	case WSDISPLAYIO_GET_FBINFO: {
    494   1.5  macallan 		struct wsdisplayio_fbinfo *fbi = data;
    495   1.5  macallan 		return wsdisplayio_get_fbinfo(&ms->scr_ri, fbi);
    496   1.5  macallan 	}
    497   1.1  macallan 	}
    498   1.1  macallan 	return EPASSTHROUGH;
    499   1.1  macallan }
    500   1.1  macallan 
    501   1.1  macallan static paddr_t
    502   1.1  macallan gffb_mmap(void *v, void *vs, off_t offset, int prot)
    503   1.1  macallan {
    504   1.1  macallan 	struct vcons_data *vd = v;
    505   1.1  macallan 	struct gffb_softc *sc = vd->cookie;
    506   1.1  macallan 	paddr_t pa;
    507   1.1  macallan 
    508   1.1  macallan 	/* 'regular' framebuffer mmap()ing */
    509   1.4  macallan 	if (offset < sc->sc_vramsize) {
    510   1.2  macallan 		pa = bus_space_mmap(sc->sc_memt, sc->sc_fb + offset + 0x2000,
    511   1.2  macallan 		    0, prot, BUS_SPACE_MAP_LINEAR);
    512   1.1  macallan 		return pa;
    513   1.1  macallan 	}
    514   1.1  macallan 
    515   1.1  macallan 	/*
    516   1.1  macallan 	 * restrict all other mappings to processes with superuser privileges
    517   1.1  macallan 	 * or the kernel itself
    518   1.1  macallan 	 */
    519   1.4  macallan 	if (kauth_authorize_machdep(kauth_cred_get(),
    520   1.4  macallan 	    KAUTH_MACHDEP_UNMANAGEDMEM,
    521   1.1  macallan 	    NULL, NULL, NULL, NULL) != 0) {
    522   1.1  macallan 		aprint_normal("%s: mmap() rejected.\n",
    523   1.1  macallan 		    device_xname(sc->sc_dev));
    524   1.1  macallan 		return -1;
    525   1.1  macallan 	}
    526   1.1  macallan 
    527   1.1  macallan 	if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
    528   1.1  macallan 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    529   1.1  macallan 		    BUS_SPACE_MAP_LINEAR);
    530   1.1  macallan 		return pa;
    531   1.1  macallan 	}
    532   1.1  macallan 
    533  1.11   msaitoh 	if ((offset >= sc->sc_reg) &&
    534   1.1  macallan 	    (offset < (sc->sc_reg + sc->sc_regsize))) {
    535   1.1  macallan 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    536   1.1  macallan 		    BUS_SPACE_MAP_LINEAR);
    537   1.1  macallan 		return pa;
    538   1.1  macallan 	}
    539   1.1  macallan 
    540   1.1  macallan #ifdef PCI_MAGIC_IO_RANGE
    541   1.1  macallan 	/* allow mapping of IO space */
    542   1.1  macallan 	if ((offset >= PCI_MAGIC_IO_RANGE) &&
    543   1.1  macallan 	    (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
    544   1.1  macallan 		pa = bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
    545   1.1  macallan 		    0, prot, BUS_SPACE_MAP_LINEAR);
    546   1.1  macallan 		return pa;
    547   1.1  macallan 	}
    548   1.1  macallan #endif
    549   1.1  macallan 
    550   1.1  macallan 	return -1;
    551   1.1  macallan }
    552   1.1  macallan 
    553   1.1  macallan static void
    554   1.1  macallan gffb_init_screen(void *cookie, struct vcons_screen *scr,
    555   1.1  macallan     int existing, long *defattr)
    556   1.1  macallan {
    557   1.1  macallan 	struct gffb_softc *sc = cookie;
    558   1.1  macallan 	struct rasops_info *ri = &scr->scr_ri;
    559   1.1  macallan 
    560   1.1  macallan 	ri->ri_depth = sc->sc_depth;
    561   1.1  macallan 	ri->ri_width = sc->sc_width;
    562   1.1  macallan 	ri->ri_height = sc->sc_height;
    563   1.1  macallan 	ri->ri_stride = sc->sc_stride;
    564  1.13  macallan 	if (sc->sc_depth == 8)
    565  1.13  macallan 	ri->ri_bits = sc->sc_fbaddr + sc->sc_fboffset;
    566   1.1  macallan 	ri->ri_flg = RI_CENTER;
    567   1.1  macallan 		ri->ri_flg |= RI_8BIT_IS_RGB | RI_ENABLE_ALPHA;
    568   1.1  macallan 
    569   1.1  macallan 	rasops_init(ri, 0, 0);
    570  1.24  macallan 	ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_RESIZE;
    571  1.24  macallan 	scr->scr_flags |= VCONS_LOADFONT;
    572   1.1  macallan 
    573   1.1  macallan 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    574   1.1  macallan 		    sc->sc_width / ri->ri_font->fontwidth);
    575   1.1  macallan 
    576   1.1  macallan 	ri->ri_hw = scr;
    577   1.3  macallan 
    578  1.13  macallan 	if (sc->sc_accel) {
    579  1.13  macallan 		sc->sc_putchar = ri->ri_ops.putchar;
    580  1.13  macallan 		ri->ri_ops.copyrows = gffb_copyrows;
    581  1.13  macallan 		ri->ri_ops.copycols = gffb_copycols;
    582  1.13  macallan 		ri->ri_ops.eraserows = gffb_eraserows;
    583  1.13  macallan 		ri->ri_ops.erasecols = gffb_erasecols;
    584  1.13  macallan 		ri->ri_ops.cursor = gffb_cursor;
    585  1.13  macallan 		ri->ri_ops.putchar = gffb_putchar;
    586  1.13  macallan 	} else {
    587  1.13  macallan 		scr->scr_flags |= VCONS_DONT_READ;
    588  1.13  macallan 	}
    589   1.1  macallan }
    590   1.1  macallan 
    591   1.1  macallan static int
    592   1.1  macallan gffb_putcmap(struct gffb_softc *sc, struct wsdisplay_cmap *cm)
    593   1.1  macallan {
    594   1.1  macallan 	u_char *r, *g, *b;
    595   1.1  macallan 	u_int index = cm->index;
    596   1.1  macallan 	u_int count = cm->count;
    597   1.1  macallan 	int i, error;
    598   1.1  macallan 	u_char rbuf[256], gbuf[256], bbuf[256];
    599   1.1  macallan 
    600   1.3  macallan #ifdef GFFB_DEBUG
    601   1.1  macallan 	aprint_debug("putcmap: %d %d\n",index, count);
    602   1.1  macallan #endif
    603   1.1  macallan 	if (cm->index >= 256 || cm->count > 256 ||
    604   1.1  macallan 	    (cm->index + cm->count) > 256)
    605   1.1  macallan 		return EINVAL;
    606   1.1  macallan 	error = copyin(cm->red, &rbuf[index], count);
    607   1.1  macallan 	if (error)
    608   1.1  macallan 		return error;
    609   1.1  macallan 	error = copyin(cm->green, &gbuf[index], count);
    610   1.1  macallan 	if (error)
    611   1.1  macallan 		return error;
    612   1.1  macallan 	error = copyin(cm->blue, &bbuf[index], count);
    613   1.1  macallan 	if (error)
    614   1.1  macallan 		return error;
    615   1.1  macallan 
    616   1.1  macallan 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    617   1.1  macallan 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    618   1.1  macallan 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    619   1.1  macallan 
    620   1.1  macallan 	r = &sc->sc_cmap_red[index];
    621   1.1  macallan 	g = &sc->sc_cmap_green[index];
    622   1.1  macallan 	b = &sc->sc_cmap_blue[index];
    623   1.1  macallan 
    624   1.1  macallan 	for (i = 0; i < count; i++) {
    625   1.1  macallan 		gffb_putpalreg(sc, index, *r, *g, *b);
    626   1.1  macallan 		index++;
    627   1.1  macallan 		r++, g++, b++;
    628   1.1  macallan 	}
    629   1.1  macallan 	return 0;
    630   1.1  macallan }
    631   1.1  macallan 
    632   1.1  macallan static int
    633   1.1  macallan gffb_getcmap(struct gffb_softc *sc, struct wsdisplay_cmap *cm)
    634   1.1  macallan {
    635   1.1  macallan 	u_int index = cm->index;
    636   1.1  macallan 	u_int count = cm->count;
    637   1.1  macallan 	int error;
    638   1.1  macallan 
    639   1.1  macallan 	if (index >= 255 || count > 256 || index + count > 256)
    640   1.1  macallan 		return EINVAL;
    641   1.1  macallan 
    642   1.1  macallan 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    643   1.1  macallan 	if (error)
    644   1.1  macallan 		return error;
    645   1.1  macallan 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    646   1.1  macallan 	if (error)
    647   1.1  macallan 		return error;
    648   1.1  macallan 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    649   1.1  macallan 	if (error)
    650   1.1  macallan 		return error;
    651   1.1  macallan 
    652   1.1  macallan 	return 0;
    653   1.1  macallan }
    654   1.1  macallan 
    655   1.1  macallan static void
    656   1.1  macallan gffb_restore_palette(struct gffb_softc *sc)
    657   1.1  macallan {
    658   1.1  macallan 	int i;
    659   1.1  macallan 
    660   1.1  macallan 	for (i = 0; i < (1 << sc->sc_depth); i++) {
    661   1.1  macallan 		gffb_putpalreg(sc, i, sc->sc_cmap_red[i],
    662   1.1  macallan 		    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
    663   1.1  macallan 	}
    664   1.1  macallan }
    665   1.1  macallan 
    666   1.1  macallan static int
    667   1.1  macallan gffb_putpalreg(struct gffb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
    668   1.1  macallan     uint8_t b)
    669   1.1  macallan {
    670   1.1  macallan 	/* port 0 */
    671  1.26  macallan 	GFFB_WRITE_1(GFFB_PDIO0 + GFFB_PEL_IW, idx);
    672  1.26  macallan 	GFFB_WRITE_1(GFFB_PDIO0 + GFFB_PEL_D, r);
    673  1.26  macallan 	GFFB_WRITE_1(GFFB_PDIO0 + GFFB_PEL_D, g);
    674  1.26  macallan 	GFFB_WRITE_1(GFFB_PDIO0 + GFFB_PEL_D, b);
    675   1.1  macallan 
    676   1.1  macallan 	/* port 1 */
    677  1.26  macallan 	GFFB_WRITE_1(GFFB_PDIO1 + GFFB_PEL_IW, idx);
    678  1.26  macallan 	GFFB_WRITE_1(GFFB_PDIO1 + GFFB_PEL_D, r);
    679  1.26  macallan 	GFFB_WRITE_1(GFFB_PDIO1 + GFFB_PEL_D, g);
    680  1.26  macallan 	GFFB_WRITE_1(GFFB_PDIO1 + GFFB_PEL_D, b);
    681  1.11   msaitoh 
    682   1.1  macallan 	return 0;
    683   1.1  macallan }
    684   1.1  macallan 
    685   1.2  macallan 
    686   1.2  macallan static void
    687   1.2  macallan gffb_dma_kickoff(struct gffb_softc *sc)
    688   1.2  macallan {
    689  1.25  macallan 	volatile uint32_t junk;
    690   1.8       mrg 	if (sc->sc_current != sc->sc_put) {
    691   1.2  macallan 		sc->sc_put = sc->sc_current;
    692  1.18  riastrad 		bus_space_barrier(sc->sc_memt, sc->sc_fbh, 0, 0x1000000,
    693  1.18  riastrad 		    BUS_SPACE_BARRIER_WRITE);
    694  1.25  macallan 		junk = *sc->sc_fbaddr;
    695  1.25  macallan 		__USE(junk);
    696   1.4  macallan 		GFFB_WRITE_4(GFFB_FIFO_PUT, sc->sc_put);
    697  1.18  riastrad 		bus_space_barrier(sc->sc_memt, sc->sc_regh, GFFB_FIFO_PUT, 4,
    698  1.18  riastrad 		    BUS_SPACE_BARRIER_WRITE);
    699   1.2  macallan 	}
    700   1.2  macallan }
    701   1.2  macallan 
    702   1.2  macallan static void
    703   1.2  macallan gffb_dmanext(struct gffb_softc *sc, uint32_t data)
    704   1.2  macallan {
    705   1.2  macallan 	bus_space_write_stream_4(sc->sc_memt, sc->sc_fbh, sc->sc_current, data);
    706   1.2  macallan 	sc->sc_current += 4;
    707   1.2  macallan }
    708   1.2  macallan 
    709   1.2  macallan static void
    710   1.2  macallan gffb_dmastart(struct gffb_softc *sc, uint32_t tag, int size)
    711   1.2  macallan {
    712   1.2  macallan 	if(sc->sc_free <= (size << 2))
    713   1.2  macallan 		gffb_make_room(sc, size);
    714   1.2  macallan 	gffb_dmanext(sc, ((size) << 18) | (tag));
    715   1.2  macallan 	sc->sc_free -= ((size + 1) << 2);
    716   1.2  macallan }
    717   1.2  macallan 
    718   1.2  macallan /*
    719   1.2  macallan  * from xf86_video_nv/nv_xaa.c:
    720   1.2  macallan  * There is a HW race condition with videoram command buffers.
    721   1.2  macallan  * You can't jump to the location of your put offset.  We write put
    722   1.2  macallan  * at the jump offset + SKIPS dwords with noop padding in between
    723   1.2  macallan  * to solve this problem
    724   1.2  macallan  */
    725   1.2  macallan 
    726   1.2  macallan #define SKIPS  8
    727   1.2  macallan 
    728  1.11   msaitoh static void
    729   1.2  macallan gffb_make_room(struct gffb_softc *sc, int size)
    730   1.2  macallan {
    731   1.2  macallan 	uint32_t get;
    732   1.2  macallan 
    733   1.2  macallan 	size = (size + 1) << 2;	/* slots -> offset */
    734   1.2  macallan 
    735   1.2  macallan 	while (sc->sc_free < size) {
    736   1.4  macallan 		get = GFFB_READ_4(GFFB_FIFO_GET);
    737   1.2  macallan 
    738   1.2  macallan 		if (sc->sc_put >= get) {
    739   1.2  macallan 			sc->sc_free = 0x2000 - sc->sc_current;
    740   1.2  macallan 			if (sc->sc_free < size) {
    741   1.2  macallan 				gffb_dmanext(sc, 0x20000000);
    742   1.3  macallan 				if(get <= (SKIPS << 2)) {
    743   1.3  macallan 					if (sc->sc_put <= (SKIPS << 2)) {
    744   1.2  macallan 						/* corner case - will be idle */
    745   1.4  macallan 						GFFB_WRITE_4(GFFB_FIFO_PUT,
    746   1.3  macallan 						    (SKIPS + 1) << 2);
    747   1.2  macallan 					}
    748   1.2  macallan 					do {
    749   1.4  macallan 						get =GFFB_READ_4(GFFB_FIFO_GET);
    750   1.3  macallan 					} while (get <= (SKIPS << 2));
    751   1.2  macallan 				}
    752   1.4  macallan 				GFFB_WRITE_4(GFFB_FIFO_PUT, SKIPS << 2);
    753   1.3  macallan 				sc->sc_current = sc->sc_put = (SKIPS << 2);
    754   1.3  macallan 				sc->sc_free = get - ((SKIPS + 1) << 2);
    755   1.2  macallan 			}
    756   1.2  macallan 		} else
    757   1.3  macallan 			sc->sc_free = get - sc->sc_current - 4;
    758   1.2  macallan 	}
    759   1.2  macallan }
    760   1.2  macallan 
    761   1.1  macallan static void
    762   1.2  macallan gffb_sync(struct gffb_softc *sc)
    763   1.1  macallan {
    764   1.2  macallan 	int bail;
    765   1.3  macallan 	int i;
    766   1.1  macallan 
    767   1.4  macallan 	/*
    768   1.4  macallan 	 * if there are commands in the buffer make sure the chip is actually
    769   1.4  macallan 	 * trying to run them
    770   1.4  macallan 	 */
    771   1.4  macallan 	gffb_dma_kickoff(sc);
    772   1.3  macallan 
    773   1.4  macallan 	/* now wait for the command buffer to drain... */
    774   1.3  macallan 	bail = 100000000;
    775   1.4  macallan 	while ((GFFB_READ_4(GFFB_FIFO_GET) != sc->sc_put) && (bail > 0)) {
    776   1.2  macallan 		bail--;
    777   1.2  macallan 	}
    778  1.25  macallan 	if (bail == 0) {
    779  1.25  macallan 		printf("FIFO isn't moving\n");
    780  1.25  macallan 		goto crap;
    781  1.25  macallan 	}
    782   1.2  macallan 
    783   1.4  macallan 	/* ... and for the engine to go idle */
    784   1.3  macallan 	bail = 100000000;
    785   1.4  macallan 	while((GFFB_READ_4(GFFB_BUSY) != 0) && (bail > 0)) {
    786   1.2  macallan 		bail--;
    787   1.1  macallan 	}
    788   1.3  macallan 	if (bail == 0) goto crap;
    789   1.3  macallan 	return;
    790   1.3  macallan crap:
    791   1.4  macallan 	/* if we time out fill the buffer with NOPs and cross fingers */
    792  1.25  macallan 	DPRINTF("GET %08x\n", GFFB_READ_4(GFFB_FIFO_GET));
    793   1.3  macallan 	sc->sc_put = 0;
    794   1.3  macallan 	sc->sc_current = 0;
    795   1.3  macallan 	for (i = 0; i < 0x2000; i += 4)
    796   1.3  macallan 		bus_space_write_stream_4(sc->sc_memt, sc->sc_fbh, i, 0);
    797   1.4  macallan 	aprint_error_dev(sc->sc_dev, "DMA lockup\n");
    798   1.2  macallan }
    799   1.2  macallan 
    800  1.25  macallan void
    801   1.2  macallan gffb_init(struct gffb_softc *sc)
    802   1.2  macallan {
    803   1.2  macallan 	int i;
    804   1.3  macallan 	uint32_t foo;
    805   1.1  macallan 
    806  1.13  macallan 	if (!sc->sc_accel) return;
    807  1.25  macallan 	DPRINTF("%s offset %08x %08x\n", __func__,
    808  1.25  macallan 	    GFFB_READ_4(GFFB_CRTC0 + GFFB_DISPLAYSTART),
    809  1.25  macallan 	    GFFB_READ_4(GFFB_CRTC1 + GFFB_DISPLAYSTART));
    810  1.13  macallan 
    811  1.13  macallan 	sc->sc_fboffset = 0x2000;
    812  1.13  macallan 
    813   1.1  macallan 	/* init display start */
    814  1.13  macallan 	GFFB_WRITE_4(GFFB_CRTC0 + GFFB_DISPLAYSTART, sc->sc_fboffset);
    815  1.13  macallan 	GFFB_WRITE_4(GFFB_CRTC1 + GFFB_DISPLAYSTART, sc->sc_fboffset);
    816  1.11   msaitoh 
    817  1.26  macallan 	/* make sure we do 8bit per channel */
    818  1.26  macallan 	GFFB_WRITE_1(GFFB_PDIO0 + GFFB_PEL_MASK, 0xff);
    819  1.26  macallan 	GFFB_WRITE_1(GFFB_PDIO1 + GFFB_PEL_MASK, 0xff);
    820  1.26  macallan 
    821   1.2  macallan 	/* DMA stuff. A whole lot of magic number voodoo from xf86-video-nv */
    822   1.4  macallan 	GFFB_WRITE_4(GFFB_PMC + 0x140, 0);
    823   1.4  macallan 	GFFB_WRITE_4(GFFB_PMC + 0x200, 0xffff00ff);
    824   1.4  macallan 	GFFB_WRITE_4(GFFB_PMC + 0x200, 0xffffffff);
    825   1.4  macallan 	GFFB_WRITE_4(GFFB_PTIMER + 0x800, 8);
    826   1.4  macallan 	GFFB_WRITE_4(GFFB_PTIMER + 0x840, 3);
    827   1.4  macallan 	GFFB_WRITE_4(GFFB_PTIMER + 0x500, 0);
    828   1.4  macallan 	GFFB_WRITE_4(GFFB_PTIMER + 0x400, 0xffffffff);
    829   1.3  macallan 	for (i = 0; i < 8; i++) {
    830   1.4  macallan 		GFFB_WRITE_4(GFFB_PFB + 0x0240 + (i * 0x10), 0);
    831   1.4  macallan 		GFFB_WRITE_4(GFFB_PFB + 0x0244 + (i * 0x10),
    832   1.4  macallan 		    sc->sc_vramsize - 1);
    833   1.4  macallan 	}
    834   1.4  macallan 
    835   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN, 0x80000010);
    836   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x04, 0x80011201);
    837   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x08, 0x80000011);
    838   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x0c, 0x80011202);
    839   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x10, 0x80000012);
    840   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x14, 0x80011203);
    841   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x18, 0x80000013);
    842   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x1c, 0x80011204);
    843   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x20, 0x80000014);
    844   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x24, 0x80011205);
    845   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x28, 0x80000015);
    846   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2c, 0x80011206);
    847   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x30, 0x80000016);
    848   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x34, 0x80011207);
    849   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x38, 0x80000017);
    850   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x3c, 0x80011208);
    851   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2000, 0x00003000);
    852   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2004, sc->sc_vramsize - 1);
    853   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2008, 0x00000002);
    854   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x200c, 0x00000002);
    855  1.25  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2010, 0x01008062);	/* nv10+ */
    856   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2014, 0);
    857   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2018, 0x12001200);
    858   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x201c, 0);
    859   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2020, 0x01008043);
    860   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2024, 0);
    861   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2028, 0);
    862   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x202c, 0);
    863   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2030, 0x01008044);
    864   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2034, 0x00000002);
    865   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2038, 0);
    866   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x203c, 0);
    867   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2040, 0x01008019);
    868   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2044, 0);
    869   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2048, 0);
    870   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x204c, 0);
    871   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2050, 0x0100a05c);
    872   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2054, 0);
    873   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2058, 0);
    874   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x205c, 0);
    875   1.2  macallan 	/* XXX 0x0100805f if !WaitVSynvPossible */
    876  1.25  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2060, 0x0100805f);
    877   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2064, 0);
    878   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2068, 0x12001200);
    879   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x206c, 0);
    880   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2070, 0x0100804a);
    881   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2074, 0x00000002);
    882   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2078, 0);
    883   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x207c, 0);
    884   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2080, 0x01018077);
    885   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2084, 0);
    886   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2088, 0x12001200);
    887   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x208c, 0);
    888   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2090, 0x00003002);
    889   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2094, 0x00007fff);
    890   1.4  macallan 	/* command buffer start with some flag in the lower bits */
    891  1.25  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2098, sc->sc_vramsize | 0x00000002);
    892   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x209c, 0x00000002);
    893   1.3  macallan #if BYTE_ORDER == BIG_ENDIAN
    894  1.25  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2010, 0x01088062);
    895   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2020, 0x01088043);
    896   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2030, 0x01088044);
    897   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2040, 0x01088019);
    898   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2050, 0x0108a05c);
    899  1.25  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2060, 0x0108805f);
    900   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2070, 0x0108804a);
    901   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2080, 0x01098077);
    902   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2034, 0x00000001);
    903   1.4  macallan 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2074, 0x00000001);
    904   1.3  macallan #endif
    905  1.25  macallan 	/* PGRAPH setup */
    906  1.25  macallan 	GFFB_WRITE_4(GFFB_PGRAPH + 0x0080, 0xFFFFFFFF);
    907  1.25  macallan 	GFFB_WRITE_4(GFFB_PGRAPH + 0x0080, 0x00000000);
    908  1.25  macallan 
    909  1.25  macallan 	GFFB_WRITE_4(GFFB_PGRAPH + 0x0140, 0x00000000);
    910  1.25  macallan 	GFFB_WRITE_4(GFFB_PGRAPH + 0x0100, 0xFFFFFFFF);
    911  1.25  macallan 	GFFB_WRITE_4(GFFB_PGRAPH + 0x0144, 0x10010100);
    912  1.25  macallan 	GFFB_WRITE_4(GFFB_PGRAPH + 0x0714, 0xFFFFFFFF);
    913  1.25  macallan 	GFFB_WRITE_4(GFFB_PGRAPH + 0x0720, 0x00000001);
    914  1.25  macallan 	/*
    915  1.25  macallan 	 * xf86_video_nv does this in two writes,
    916  1.25  macallan 	 * not sure if they can be combined
    917  1.25  macallan 	 */
    918  1.25  macallan 	foo = GFFB_READ_4(GFFB_PGRAPH + 0x0710);
    919  1.25  macallan 	foo &= 0x0007ff00;
    920  1.25  macallan 	foo |= 0x00020100;
    921  1.25  macallan 	GFFB_WRITE_4(GFFB_PGRAPH + 0x0710, foo);
    922  1.25  macallan 
    923  1.25  macallan 	/* NV_ARCH_10 */
    924  1.25  macallan 	if(sc->sc_arch == 10) {
    925  1.25  macallan 		GFFB_WRITE_4(GFFB_PGRAPH + 0x0084, 0x00118700);
    926  1.25  macallan 		GFFB_WRITE_4(GFFB_PGRAPH + 0x0088, 0x24E00810);
    927  1.25  macallan 		GFFB_WRITE_4(GFFB_PGRAPH + 0x008C, 0x55DE0030);
    928  1.25  macallan 
    929  1.25  macallan 		for(i = 0; i < 128; i += 4) {
    930  1.25  macallan 			GFFB_WRITE_4(GFFB_PGRAPH + 0x0B00 + i,
    931  1.25  macallan 			    GFFB_READ_4(GFFB_PFB + 0x0240 + i));
    932  1.25  macallan 		}
    933  1.25  macallan 
    934  1.25  macallan 		GFFB_WRITE_4(GFFB_PGRAPH + 0x640, 0);
    935  1.25  macallan 		GFFB_WRITE_4(GFFB_PGRAPH + 0x644, 0);
    936  1.25  macallan 		GFFB_WRITE_4(GFFB_PGRAPH + 0x684, sc->sc_vramsize - 1);
    937  1.25  macallan 		GFFB_WRITE_4(GFFB_PGRAPH + 0x688, sc->sc_vramsize - 1);
    938  1.25  macallan 
    939  1.25  macallan 		GFFB_WRITE_4(GFFB_PGRAPH + 0x0810, 0x00000000);
    940  1.25  macallan 		GFFB_WRITE_4(GFFB_PGRAPH + 0x0608, 0xFFFFFFFF);
    941  1.25  macallan 	} else {
    942  1.25  macallan 		/* nv30 */
    943  1.25  macallan 		GFFB_WRITE_4(GFFB_PGRAPH + 0x0084, 0x40108700);
    944  1.25  macallan 		GFFB_WRITE_4(GFFB_PGRAPH + 0x0890, 0x00140000);
    945  1.25  macallan 		GFFB_WRITE_4(GFFB_PGRAPH + 0x008C, 0xf00e0431);
    946  1.25  macallan 		GFFB_WRITE_4(GFFB_PGRAPH + 0x0090, 0x00008000);
    947  1.25  macallan 		GFFB_WRITE_4(GFFB_PGRAPH + 0x0610, 0xf04b1f36);
    948  1.25  macallan 		GFFB_WRITE_4(GFFB_PGRAPH + 0x0B80, 0x1002d888);
    949  1.25  macallan 		GFFB_WRITE_4(GFFB_PGRAPH + 0x0B88, 0x62ff007f);
    950  1.25  macallan 
    951  1.25  macallan 		for (i = 0; i < 128; i += 4) {
    952  1.25  macallan 			GFFB_WRITE_4(GFFB_PGRAPH + 0x0900 + i,
    953  1.25  macallan 			    GFFB_READ_4(GFFB_PFB + 0x0240 + i));
    954  1.25  macallan   			GFFB_WRITE_4(GFFB_PGRAPH + 0x6900 + i,
    955  1.25  macallan 			    GFFB_READ_4(GFFB_PFB + 0x0240 + i));
    956  1.25  macallan 		}
    957  1.25  macallan 
    958  1.25  macallan 		GFFB_WRITE_4(GFFB_PGRAPH + 0x09A4,
    959  1.25  macallan 			GFFB_READ_4(GFFB_PFB + 0x0200));
    960  1.25  macallan 		GFFB_WRITE_4(GFFB_PGRAPH + 0x09A8,
    961  1.25  macallan 			GFFB_READ_4(GFFB_PFB + 0x0204));
    962  1.25  macallan 		GFFB_WRITE_4(GFFB_PGRAPH + 0x0750, 0x00EA0000);
    963  1.25  macallan 		GFFB_WRITE_4(GFFB_PGRAPH + 0x0754,
    964  1.25  macallan 			GFFB_READ_4(GFFB_PFB + 0x0200));
    965  1.25  macallan 		GFFB_WRITE_4(GFFB_PGRAPH + 0x0750, 0x00EA0004);
    966  1.25  macallan 		GFFB_WRITE_4(GFFB_PGRAPH + 0x0754,
    967  1.25  macallan 			GFFB_READ_4(GFFB_PFB + 0x0204));
    968  1.25  macallan 
    969  1.25  macallan 		GFFB_WRITE_4(GFFB_PGRAPH + 0x0820, 0);
    970  1.25  macallan 		GFFB_WRITE_4(GFFB_PGRAPH + 0x0824, 0);
    971  1.25  macallan 		GFFB_WRITE_4(GFFB_PGRAPH + 0x0864, sc->sc_vramsize - 1);
    972  1.25  macallan 		GFFB_WRITE_4(GFFB_PGRAPH + 0x0868, sc->sc_vramsize - 1);
    973  1.25  macallan 
    974  1.25  macallan 		GFFB_WRITE_4(GFFB_PGRAPH + 0x0B20, 0x00000000);
    975  1.25  macallan 		GFFB_WRITE_4(GFFB_PGRAPH + 0x0B04, 0xFFFFFFFF);
    976  1.25  macallan 	}
    977  1.25  macallan 
    978  1.25  macallan 	/* PFIFO setup */
    979  1.25  macallan 	GFFB_WRITE_4(GFFB_PGRAPH + 0x053C, 0);
    980  1.25  macallan 	GFFB_WRITE_4(GFFB_PGRAPH + 0x0540, 0);
    981  1.25  macallan 	GFFB_WRITE_4(GFFB_PGRAPH + 0x0544, 0x00007FFF);
    982  1.25  macallan 	GFFB_WRITE_4(GFFB_PGRAPH + 0x0548, 0x00007FFF);
    983   1.3  macallan 
    984   1.4  macallan 	GFFB_WRITE_4(GFFB_PFIFO + 0x0500, 0);
    985   1.4  macallan 	GFFB_WRITE_4(GFFB_PFIFO + 0x0504, 0x00000001);
    986   1.4  macallan 	GFFB_WRITE_4(GFFB_PFIFO + 0x1200, 0);
    987   1.4  macallan 	GFFB_WRITE_4(GFFB_PFIFO + 0x1250, 0);
    988   1.4  macallan 	GFFB_WRITE_4(GFFB_PFIFO + 0x1204, 0x00000100);	/* different on nv40 */
    989   1.4  macallan 	GFFB_WRITE_4(GFFB_PFIFO + 0x1240, 0);
    990   1.4  macallan 	GFFB_WRITE_4(GFFB_PFIFO + 0x1244, 0);
    991   1.4  macallan 	GFFB_WRITE_4(GFFB_PFIFO + 0x122c, 0x00001209);	/* different on nv40 */
    992   1.4  macallan 	GFFB_WRITE_4(GFFB_PFIFO + 0x1000, 0);
    993   1.4  macallan 	GFFB_WRITE_4(GFFB_PFIFO + 0x1050, 0);
    994   1.4  macallan 	GFFB_WRITE_4(GFFB_PFIFO + 0x0210, 0x03000100);
    995   1.4  macallan 	GFFB_WRITE_4(GFFB_PFIFO + 0x0214, 0x00000110);
    996   1.4  macallan 	GFFB_WRITE_4(GFFB_PFIFO + 0x0218, 0x00000112);
    997   1.4  macallan 	GFFB_WRITE_4(GFFB_PFIFO + 0x050c, 0x0000ffff);
    998   1.4  macallan 	GFFB_WRITE_4(GFFB_PFIFO + 0x1258, 0x0000ffff);
    999   1.4  macallan 	GFFB_WRITE_4(GFFB_PFIFO + 0x0140, 0);
   1000   1.4  macallan 	GFFB_WRITE_4(GFFB_PFIFO + 0x0100, 0xffffffff);
   1001   1.4  macallan 	GFFB_WRITE_4(GFFB_PFIFO + 0x1054, 0x00000001);
   1002   1.4  macallan 	GFFB_WRITE_4(GFFB_PFIFO + 0x1230, 0);
   1003   1.4  macallan 	GFFB_WRITE_4(GFFB_PFIFO + 0x1280, 0);
   1004   1.2  macallan #if BYTE_ORDER == BIG_ENDIAN
   1005   1.4  macallan 	GFFB_WRITE_4(GFFB_PFIFO + 0x1224, 0x800f0078);
   1006   1.2  macallan #else
   1007   1.4  macallan 	GFFB_WRITE_4(GFFB_PFIFO + 0x1224, 0x000f0078);
   1008   1.2  macallan #endif
   1009   1.4  macallan 	GFFB_WRITE_4(GFFB_PFIFO + 0x1220, 0x00000001);
   1010   1.4  macallan 	GFFB_WRITE_4(GFFB_PFIFO + 0x1200, 0x00000001);
   1011   1.4  macallan 	GFFB_WRITE_4(GFFB_PFIFO + 0x1250, 0x00000001);
   1012   1.4  macallan 	GFFB_WRITE_4(GFFB_PFIFO + 0x1254, 0x00000001);
   1013   1.4  macallan 	GFFB_WRITE_4(GFFB_PFIFO + 0x0500, 0x00000001);
   1014  1.25  macallan 
   1015  1.25  macallan 	GFFB_WRITE_4(GFFB_PMC + 0x8704, 1);
   1016  1.25  macallan 	GFFB_WRITE_4(GFFB_PMC + 0x8140, 0);
   1017  1.25  macallan 	GFFB_WRITE_4(GFFB_PMC + 0x8920, 0);
   1018  1.25  macallan 	GFFB_WRITE_4(GFFB_PMC + 0x8924, 0);
   1019  1.25  macallan 	GFFB_WRITE_4(GFFB_PMC + 0x8908, sc->sc_vramsize - 1);
   1020  1.25  macallan 	GFFB_WRITE_4(GFFB_PMC + 0x890C, sc->sc_vramsize - 1);
   1021  1.25  macallan 	GFFB_WRITE_4(GFFB_PMC + 0x1588, 0);
   1022   1.4  macallan 
   1023  1.25  macallan 	__asm("eieio; sync;");
   1024  1.25  macallan 	GFFB_WRITE_4(GFFB_FIFO_GET, 0);
   1025   1.4  macallan 	GFFB_WRITE_4(GFFB_CMDSTART, 0x00000002);
   1026   1.2  macallan 	sc->sc_put = 0;
   1027   1.2  macallan 	sc->sc_current = 0;
   1028   1.2  macallan 	sc->sc_free = 0x2000;
   1029   1.2  macallan 
   1030   1.2  macallan 	for(i = 0; i < SKIPS; i++)
   1031   1.2  macallan 		gffb_dmanext(sc, 0);
   1032  1.11   msaitoh 
   1033   1.2  macallan 	gffb_dmanext(sc, 0x00040000);
   1034   1.2  macallan 	gffb_dmanext(sc, 0x80000010);
   1035   1.2  macallan 	gffb_dmanext(sc, 0x00042000);
   1036   1.2  macallan 	gffb_dmanext(sc, 0x80000011);
   1037   1.2  macallan 	gffb_dmanext(sc, 0x00044000);
   1038   1.2  macallan 	gffb_dmanext(sc, 0x80000012);
   1039   1.2  macallan 	gffb_dmanext(sc, 0x00046000);
   1040   1.2  macallan 	gffb_dmanext(sc, 0x80000013);
   1041   1.2  macallan 	gffb_dmanext(sc, 0x00048000);
   1042   1.2  macallan 	gffb_dmanext(sc, 0x80000014);
   1043   1.2  macallan 	gffb_dmanext(sc, 0x0004A000);
   1044   1.2  macallan 	gffb_dmanext(sc, 0x80000015);
   1045   1.2  macallan 	gffb_dmanext(sc, 0x0004C000);
   1046   1.2  macallan 	gffb_dmanext(sc, 0x80000016);
   1047   1.2  macallan 	gffb_dmanext(sc, 0x0004E000);
   1048   1.2  macallan 	gffb_dmanext(sc, 0x80000017);
   1049   1.2  macallan 	sc->sc_free = 0x2000 - sc->sc_current;
   1050   1.2  macallan 
   1051   1.2  macallan 	gffb_dmastart(sc, SURFACE_FORMAT, 4);
   1052   1.2  macallan 	gffb_dmanext(sc, SURFACE_FORMAT_DEPTH8);
   1053   1.2  macallan 	gffb_dmanext(sc, sc->sc_stride | (sc->sc_stride << 16));
   1054  1.25  macallan 	gffb_dmanext(sc, sc->sc_fboffset);	/* src offset */
   1055  1.25  macallan 	gffb_dmanext(sc, sc->sc_fboffset);	/* dst offset */
   1056   1.2  macallan 
   1057   1.2  macallan 	gffb_dmastart(sc, RECT_FORMAT, 1);
   1058   1.2  macallan 	gffb_dmanext(sc, RECT_FORMAT_DEPTH8);
   1059   1.2  macallan 
   1060   1.3  macallan 	gffb_dmastart(sc, PATTERN_FORMAT, 1);
   1061   1.3  macallan 	gffb_dmanext(sc, PATTERN_FORMAT_DEPTH8);
   1062   1.3  macallan 
   1063   1.3  macallan 	gffb_dmastart(sc, PATTERN_COLOR_0, 4);
   1064   1.3  macallan 	gffb_dmanext(sc, 0xffffffff);
   1065   1.3  macallan 	gffb_dmanext(sc, 0xffffffff);
   1066   1.3  macallan 	gffb_dmanext(sc, 0xffffffff);
   1067   1.3  macallan 	gffb_dmanext(sc, 0xffffffff);
   1068  1.11   msaitoh 
   1069   1.2  macallan 	gffb_dmastart(sc, ROP_SET, 1);
   1070   1.2  macallan 	gffb_dmanext(sc, 0xcc);
   1071   1.3  macallan 	sc->sc_rop = 0xcc;
   1072  1.25  macallan 	DPRINTF("put %x current %x\n", sc->sc_put, sc->sc_current);
   1073   1.2  macallan 
   1074   1.2  macallan 	gffb_dma_kickoff(sc);
   1075  1.25  macallan 	DPRINTF("put %x current %x\n", sc->sc_put, sc->sc_current);
   1076  1.25  macallan #ifdef GFFB_DEBUG
   1077  1.25  macallan 	printf("put: %08x\n", GFFB_READ_4(GFFB_FIFO_PUT));
   1078  1.25  macallan 	printf("get: %08x\n", GFFB_READ_4(GFFB_FIFO_GET));
   1079  1.25  macallan #endif
   1080   1.2  macallan 	gffb_sync(sc);
   1081   1.4  macallan 	DPRINTF("put %x current %x\n", sc->sc_put, sc->sc_current);
   1082   1.1  macallan }
   1083   1.2  macallan 
   1084   1.3  macallan static void
   1085   1.3  macallan gffb_rop(struct gffb_softc *sc, int rop)
   1086   1.3  macallan {
   1087   1.3  macallan 	if (rop == sc->sc_rop)
   1088   1.3  macallan 		return;
   1089   1.3  macallan 	sc->sc_rop = rop;
   1090   1.3  macallan 	gffb_dmastart(sc, ROP_SET, 1);
   1091   1.3  macallan 	gffb_dmanext(sc, rop);
   1092  1.11   msaitoh }
   1093   1.3  macallan 
   1094   1.3  macallan static void
   1095   1.3  macallan gffb_rectfill(struct gffb_softc *sc, int x, int y, int wi, int he,
   1096   1.3  macallan      uint32_t colour)
   1097   1.3  macallan {
   1098  1.13  macallan 	if (!sc->sc_accel) return;
   1099   1.3  macallan 	mutex_enter(&sc->sc_lock);
   1100   1.3  macallan 	gffb_rop(sc, 0xcc);
   1101   1.3  macallan 
   1102   1.3  macallan 	gffb_dmastart(sc, RECT_SOLID_COLOR, 1);
   1103   1.3  macallan 	gffb_dmanext(sc, colour);
   1104   1.3  macallan 
   1105   1.3  macallan 	gffb_dmastart(sc, RECT_SOLID_RECTS(0), 2);
   1106   1.3  macallan 	gffb_dmanext(sc, (x << 16) | y);
   1107   1.3  macallan 	gffb_dmanext(sc, (wi << 16) | he);
   1108   1.3  macallan 	gffb_dma_kickoff(sc);
   1109   1.3  macallan 	mutex_exit(&sc->sc_lock);
   1110   1.3  macallan }
   1111   1.3  macallan 
   1112   1.3  macallan static void
   1113   1.3  macallan gffb_bitblt(void *cookie, int xs, int ys, int xd, int yd,
   1114   1.3  macallan     int wi, int he, int rop)
   1115   1.3  macallan {
   1116   1.3  macallan 	struct gffb_softc *sc = cookie;
   1117   1.3  macallan 
   1118  1.13  macallan 	if (!sc->sc_accel) return;
   1119   1.3  macallan 	mutex_enter(&sc->sc_lock);
   1120   1.3  macallan 
   1121   1.3  macallan 	gffb_rop(sc, rop);
   1122   1.3  macallan 
   1123   1.3  macallan 	gffb_dmastart(sc, BLIT_POINT_SRC, 3);
   1124   1.3  macallan 	gffb_dmanext(sc, (ys << 16) | xs);
   1125   1.3  macallan 	gffb_dmanext(sc, (yd << 16) | xd);
   1126   1.3  macallan 	gffb_dmanext(sc, (he << 16) | wi);
   1127   1.3  macallan 	gffb_dma_kickoff(sc);
   1128   1.3  macallan 	mutex_exit(&sc->sc_lock);
   1129   1.3  macallan }
   1130   1.3  macallan 
   1131   1.3  macallan static void
   1132   1.3  macallan gffb_cursor(void *cookie, int on, int row, int col)
   1133   1.3  macallan {
   1134   1.3  macallan 	struct rasops_info *ri = cookie;
   1135   1.3  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1136   1.3  macallan 	struct gffb_softc *sc = scr->scr_cookie;
   1137   1.3  macallan 	int x, y, wi, he;
   1138  1.11   msaitoh 
   1139   1.3  macallan 	wi = ri->ri_font->fontwidth;
   1140   1.3  macallan 	he = ri->ri_font->fontheight;
   1141  1.11   msaitoh 
   1142   1.3  macallan 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
   1143   1.3  macallan 		x = ri->ri_ccol * wi + ri->ri_xorigin;
   1144   1.3  macallan 		y = ri->ri_crow * he + ri->ri_yorigin;
   1145   1.3  macallan 		if (ri->ri_flg & RI_CURSOR) {
   1146   1.3  macallan 			gffb_bitblt(sc, x, y, x, y, wi, he, 0x33);
   1147   1.3  macallan 			ri->ri_flg &= ~RI_CURSOR;
   1148   1.3  macallan 		}
   1149   1.3  macallan 		ri->ri_crow = row;
   1150   1.3  macallan 		ri->ri_ccol = col;
   1151   1.3  macallan 		if (on) {
   1152   1.3  macallan 			x = ri->ri_ccol * wi + ri->ri_xorigin;
   1153   1.3  macallan 			y = ri->ri_crow * he + ri->ri_yorigin;
   1154   1.3  macallan 			gffb_bitblt(sc, x, y, x, y, wi, he, 0x33);
   1155   1.3  macallan 			ri->ri_flg |= RI_CURSOR;
   1156   1.3  macallan 		}
   1157   1.3  macallan 	} else {
   1158   1.3  macallan 		scr->scr_ri.ri_crow = row;
   1159   1.3  macallan 		scr->scr_ri.ri_ccol = col;
   1160   1.3  macallan 		scr->scr_ri.ri_flg &= ~RI_CURSOR;
   1161   1.3  macallan 	}
   1162   1.3  macallan 
   1163   1.3  macallan }
   1164   1.3  macallan 
   1165   1.3  macallan static void
   1166   1.3  macallan gffb_putchar(void *cookie, int row, int col, u_int c, long attr)
   1167   1.3  macallan {
   1168   1.3  macallan 	struct rasops_info *ri = cookie;
   1169   1.3  macallan 	struct wsdisplay_font *font = PICK_FONT(ri, c);
   1170   1.3  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1171   1.3  macallan 	struct gffb_softc *sc = scr->scr_cookie;
   1172   1.3  macallan 	int x, y, wi, he, rv = GC_NOPE;
   1173   1.3  macallan 	uint32_t bg;
   1174   1.3  macallan 
   1175  1.11   msaitoh 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
   1176   1.3  macallan 		return;
   1177   1.3  macallan 
   1178   1.3  macallan 	if (!CHAR_IN_FONT(c, font))
   1179   1.3  macallan 		return;
   1180   1.3  macallan 
   1181   1.3  macallan 	wi = font->fontwidth;
   1182   1.3  macallan 	he = font->fontheight;
   1183  1.11   msaitoh 
   1184   1.3  macallan 	x = ri->ri_xorigin + col * wi;
   1185   1.3  macallan 	y = ri->ri_yorigin + row * he;
   1186   1.3  macallan 	bg = ri->ri_devcmap[(attr >> 16) & 0xf];
   1187  1.11   msaitoh 
   1188   1.3  macallan 	if (c == 0x20) {
   1189   1.3  macallan 		gffb_rectfill(sc, x, y, wi, he, bg);
   1190   1.3  macallan 		return;
   1191   1.3  macallan 	}
   1192   1.3  macallan 	rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
   1193   1.3  macallan 	if (rv == GC_OK)
   1194   1.3  macallan 		return;
   1195   1.3  macallan 
   1196  1.19  riastrad 	/*
   1197  1.19  riastrad 	 * Use gffb_sync to wait for the engine to become idle before
   1198  1.20    andvar 	 * we start scribbling into VRAM -- we wouldn't want to stomp on
   1199  1.19  riastrad 	 * a scroll in progress or a prior glyphcache_add that hasn't
   1200  1.19  riastrad 	 * completed yet on the GPU.
   1201  1.19  riastrad 	 */
   1202   1.3  macallan 	mutex_enter(&sc->sc_lock);
   1203   1.3  macallan 	gffb_sync(sc);
   1204   1.3  macallan 	sc->sc_putchar(cookie, row, col, c, attr);
   1205   1.3  macallan 	mutex_exit(&sc->sc_lock);
   1206   1.3  macallan 
   1207  1.19  riastrad 	/*
   1208  1.19  riastrad 	 * If glyphcache_try asked us to, cache the newly written
   1209  1.19  riastrad 	 * character.  This will issue a gffb_bitblt which will wait
   1210  1.19  riastrad 	 * for our CPU writes to the framebuffer in VRAM to complete
   1211  1.19  riastrad 	 * before triggering GPU reads from the framebuffer in VRAM.
   1212  1.19  riastrad 	 */
   1213   1.3  macallan 	if (rv == GC_ADD) {
   1214   1.3  macallan 		glyphcache_add(&sc->sc_gc, c, x, y);
   1215   1.3  macallan 	}
   1216   1.3  macallan }
   1217   1.3  macallan 
   1218   1.3  macallan static void
   1219   1.3  macallan gffb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
   1220   1.3  macallan {
   1221   1.3  macallan 	struct rasops_info *ri = cookie;
   1222   1.3  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1223   1.3  macallan 	struct gffb_softc *sc = scr->scr_cookie;
   1224   1.3  macallan 	int32_t xs, xd, y, width, height;
   1225  1.11   msaitoh 
   1226   1.3  macallan 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1227   1.3  macallan 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
   1228   1.3  macallan 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
   1229   1.3  macallan 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1230   1.3  macallan 		width = ri->ri_font->fontwidth * ncols;
   1231   1.3  macallan 		height = ri->ri_font->fontheight;
   1232   1.3  macallan 		gffb_bitblt(sc, xs, y, xd, y, width, height, 0xcc);
   1233   1.3  macallan 	}
   1234   1.3  macallan }
   1235   1.3  macallan 
   1236   1.3  macallan static void
   1237   1.3  macallan gffb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
   1238   1.3  macallan {
   1239   1.3  macallan 	struct rasops_info *ri = cookie;
   1240   1.3  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1241   1.3  macallan 	struct gffb_softc *sc = scr->scr_cookie;
   1242   1.3  macallan 	int32_t x, y, width, height, fg, bg, ul;
   1243  1.11   msaitoh 
   1244   1.3  macallan 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1245   1.3  macallan 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
   1246   1.3  macallan 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1247   1.3  macallan 		width = ri->ri_font->fontwidth * ncols;
   1248   1.3  macallan 		height = ri->ri_font->fontheight;
   1249   1.3  macallan 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
   1250   1.3  macallan 
   1251   1.3  macallan 		gffb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
   1252   1.3  macallan 	}
   1253   1.3  macallan }
   1254   1.3  macallan 
   1255   1.3  macallan static void
   1256   1.3  macallan gffb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
   1257   1.3  macallan {
   1258   1.3  macallan 	struct rasops_info *ri = cookie;
   1259   1.3  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1260   1.3  macallan 	struct gffb_softc *sc = scr->scr_cookie;
   1261   1.3  macallan 	int32_t x, ys, yd, width, height;
   1262   1.3  macallan 
   1263   1.3  macallan 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1264   1.3  macallan 		x = ri->ri_xorigin;
   1265   1.3  macallan 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
   1266   1.3  macallan 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
   1267   1.3  macallan 		width = ri->ri_emuwidth;
   1268   1.3  macallan 		height = ri->ri_font->fontheight * nrows;
   1269   1.3  macallan 		gffb_bitblt(sc, x, ys, x, yd, width, height, 0xcc);
   1270   1.3  macallan 	}
   1271   1.3  macallan }
   1272   1.3  macallan 
   1273   1.3  macallan static void
   1274   1.3  macallan gffb_eraserows(void *cookie, int row, int nrows, long fillattr)
   1275   1.3  macallan {
   1276   1.3  macallan 	struct rasops_info *ri = cookie;
   1277   1.3  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1278   1.3  macallan 	struct gffb_softc *sc = scr->scr_cookie;
   1279   1.3  macallan 	int32_t x, y, width, height, fg, bg, ul;
   1280  1.11   msaitoh 
   1281   1.3  macallan 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1282   1.3  macallan 		x = ri->ri_xorigin;
   1283   1.3  macallan 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1284   1.3  macallan 		width = ri->ri_emuwidth;
   1285   1.3  macallan 		height = ri->ri_font->fontheight * nrows;
   1286   1.3  macallan 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
   1287   1.3  macallan 
   1288   1.3  macallan 		gffb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
   1289   1.3  macallan 	}
   1290   1.3  macallan }
   1291