Home | History | Annotate | Line # | Download | only in dev
grf.c revision 1.38
      1  1.38     isaki /*	$NetBSD: grf.c,v 1.38 2009/01/18 02:40:05 isaki Exp $	*/
      2   1.1       oki 
      3   1.1       oki /*
      4   1.1       oki  * Copyright (c) 1990, 1993
      5   1.1       oki  *	The Regents of the University of California.  All rights reserved.
      6   1.1       oki  *
      7   1.1       oki  * This code is derived from software contributed to Berkeley by
      8   1.1       oki  * the Systems Programming Group of the University of Utah Computer
      9   1.1       oki  * Science Department.
     10   1.1       oki  *
     11   1.1       oki  * Redistribution and use in source and binary forms, with or without
     12   1.1       oki  * modification, are permitted provided that the following conditions
     13   1.1       oki  * are met:
     14   1.1       oki  * 1. Redistributions of source code must retain the above copyright
     15   1.1       oki  *    notice, this list of conditions and the following disclaimer.
     16   1.1       oki  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1       oki  *    notice, this list of conditions and the following disclaimer in the
     18   1.1       oki  *    documentation and/or other materials provided with the distribution.
     19  1.28       agc  * 3. Neither the name of the University nor the names of its contributors
     20  1.28       agc  *    may be used to endorse or promote products derived from this software
     21  1.28       agc  *    without specific prior written permission.
     22  1.28       agc  *
     23  1.28       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.28       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.28       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.28       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.28       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.28       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.28       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.28       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.28       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.28       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.28       agc  * SUCH DAMAGE.
     34  1.28       agc  *
     35  1.28       agc  * from: Utah $Hdr: grf.c 1.36 93/08/13$
     36  1.28       agc  *
     37  1.28       agc  *	@(#)grf.c	8.4 (Berkeley) 1/12/94
     38  1.28       agc  */
     39  1.28       agc /*
     40  1.28       agc  * Copyright (c) 1988 University of Utah.
     41  1.28       agc  *
     42  1.28       agc  * This code is derived from software contributed to Berkeley by
     43  1.28       agc  * the Systems Programming Group of the University of Utah Computer
     44  1.28       agc  * Science Department.
     45  1.28       agc  *
     46  1.28       agc  * Redistribution and use in source and binary forms, with or without
     47  1.28       agc  * modification, are permitted provided that the following conditions
     48  1.28       agc  * are met:
     49  1.28       agc  * 1. Redistributions of source code must retain the above copyright
     50  1.28       agc  *    notice, this list of conditions and the following disclaimer.
     51  1.28       agc  * 2. Redistributions in binary form must reproduce the above copyright
     52  1.28       agc  *    notice, this list of conditions and the following disclaimer in the
     53  1.28       agc  *    documentation and/or other materials provided with the distribution.
     54   1.1       oki  * 3. All advertising materials mentioning features or use of this software
     55   1.1       oki  *    must display the following acknowledgement:
     56   1.1       oki  *	This product includes software developed by the University of
     57   1.1       oki  *	California, Berkeley and its contributors.
     58   1.1       oki  * 4. Neither the name of the University nor the names of its contributors
     59   1.1       oki  *    may be used to endorse or promote products derived from this software
     60   1.1       oki  *    without specific prior written permission.
     61   1.1       oki  *
     62   1.1       oki  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     63   1.1       oki  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     64   1.1       oki  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     65   1.1       oki  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     66   1.1       oki  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     67   1.1       oki  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     68   1.1       oki  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     69   1.1       oki  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     70   1.1       oki  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     71   1.1       oki  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     72   1.1       oki  * SUCH DAMAGE.
     73   1.1       oki  *
     74   1.1       oki  * from: Utah $Hdr: grf.c 1.36 93/08/13$
     75   1.1       oki  *
     76   1.1       oki  *	@(#)grf.c	8.4 (Berkeley) 1/12/94
     77   1.1       oki  */
     78   1.1       oki 
     79   1.1       oki /*
     80   1.1       oki  * Graphics display driver for the X68K machines.
     81   1.1       oki  * This is the hardware-independent portion of the driver.
     82   1.1       oki  * Hardware access is through the machine dependent grf switch routines.
     83   1.1       oki  */
     84  1.27     lukem 
     85  1.27     lukem #include <sys/cdefs.h>
     86  1.38     isaki __KERNEL_RCSID(0, "$NetBSD: grf.c,v 1.38 2009/01/18 02:40:05 isaki Exp $");
     87   1.1       oki 
     88   1.1       oki #include <sys/param.h>
     89   1.2       oki #include <sys/systm.h>
     90   1.1       oki #include <sys/device.h>
     91   1.1       oki #include <sys/proc.h>
     92  1.16   minoura #include <sys/resourcevar.h>
     93   1.1       oki #include <sys/ioctl.h>
     94   1.1       oki #include <sys/file.h>
     95   1.1       oki #include <sys/malloc.h>
     96   1.1       oki #include <sys/vnode.h>
     97   1.1       oki #include <sys/mman.h>
     98   1.6       oki #include <sys/conf.h>
     99   1.1       oki 
    100  1.38     isaki #include <machine/cpu.h>
    101  1.10   minoura #include <machine/grfioctl.h>
    102  1.10   minoura 
    103   1.1       oki #include <x68k/dev/grfvar.h>
    104   1.1       oki #include <x68k/dev/itevar.h>
    105   1.1       oki 
    106  1.13       mrg #include <uvm/uvm_extern.h>
    107  1.14   minoura #include <uvm/uvm_map.h>
    108   1.9   msaitoh 
    109   1.1       oki #include <miscfs/specfs/specdev.h>
    110   1.1       oki 
    111   1.1       oki #include "ite.h"
    112   1.1       oki #if NITE == 0
    113  1.29   minoura #define	iteon(u,f)	0
    114   1.1       oki #define	iteoff(u,f)
    115   1.9   msaitoh #define	ite_reinit(u)
    116   1.1       oki #endif
    117   1.1       oki 
    118   1.1       oki #ifdef DEBUG
    119   1.1       oki int grfdebug = 0;
    120   1.1       oki #define GDB_DEVNO	0x01
    121   1.1       oki #define GDB_MMAP	0x02
    122   1.1       oki #define GDB_IOMAP	0x04
    123   1.1       oki #define GDB_LOCK	0x08
    124   1.1       oki #endif
    125   1.1       oki 
    126  1.29   minoura static int grfon(struct grf_softc *);
    127  1.29   minoura static int grfoff(struct grf_softc *);
    128  1.29   minoura static off_t grfaddr(struct grf_softc *, off_t);
    129  1.33  christos static int grfmap(dev_t, void **, struct proc *);
    130  1.33  christos static int grfunmap(dev_t, void *, struct proc *);
    131   1.7   thorpej 
    132   1.7   thorpej extern struct cfdriver grf_cd;
    133  1.22   gehenna 
    134  1.22   gehenna dev_type_open(grfopen);
    135  1.22   gehenna dev_type_close(grfclose);
    136  1.22   gehenna dev_type_ioctl(grfioctl);
    137  1.22   gehenna dev_type_mmap(grfmmap);
    138  1.22   gehenna 
    139  1.22   gehenna const struct cdevsw grf_cdevsw = {
    140  1.22   gehenna 	grfopen, grfclose, nullread, nullwrite, grfioctl,
    141  1.24  jdolecek 	nostop, notty, nopoll, grfmmap, nokqfilter,
    142  1.22   gehenna };
    143   1.6       oki 
    144   1.1       oki /*ARGSUSED*/
    145   1.2       oki int
    146  1.31  christos grfopen(dev_t dev, int flags, int mode, struct lwp *l)
    147   1.1       oki {
    148  1.30       chs 	struct grf_softc *gp;
    149   1.1       oki 	int error = 0;
    150   1.1       oki 
    151  1.35    cegger 	gp = device_lookup_private(&grf_cd, GRFUNIT(dev));
    152  1.35    cegger 	if (gp == NULL)
    153  1.35    cegger 		return ENXIO;
    154  1.35    cegger 
    155  1.36     isaki 	if ((gp->g_flags & GF_ALIVE) == 0)
    156  1.29   minoura 		return ENXIO;
    157   1.9   msaitoh 
    158   1.1       oki 	if ((gp->g_flags & (GF_OPEN|GF_EXCLUDE)) == (GF_OPEN|GF_EXCLUDE))
    159  1.29   minoura 		return EBUSY;
    160  1.29   minoura 
    161   1.1       oki 	/*
    162   1.1       oki 	 * First open.
    163   1.1       oki 	 * XXX: always put in graphics mode.
    164   1.1       oki 	 */
    165   1.1       oki 	error = 0;
    166   1.1       oki 	if ((gp->g_flags & GF_OPEN) == 0) {
    167   1.1       oki 		gp->g_flags |= GF_OPEN;
    168  1.29   minoura 		error = grfon(gp);
    169   1.1       oki 	}
    170  1.29   minoura 	return error;
    171   1.1       oki }
    172   1.1       oki 
    173   1.1       oki /*ARGSUSED*/
    174   1.2       oki int
    175  1.31  christos grfclose(dev_t dev, int flags, int mode, struct lwp *l)
    176   1.1       oki {
    177  1.35    cegger 	struct grf_softc *gp = device_lookup_private(&grf_cd, GRFUNIT(dev));
    178   1.1       oki 
    179   1.9   msaitoh 	if ((gp->g_flags & GF_ALIVE) == 0)
    180  1.29   minoura 		return ENXIO;
    181   1.9   msaitoh 
    182  1.29   minoura 	(void) grfoff(gp);
    183   1.1       oki 	gp->g_flags &= GF_ALIVE;
    184  1.29   minoura 
    185  1.29   minoura 	return 0;
    186   1.1       oki }
    187   1.1       oki 
    188   1.1       oki /*ARGSUSED*/
    189   1.2       oki int
    190  1.33  christos grfioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    191   1.1       oki {
    192   1.1       oki 	int unit = GRFUNIT(dev);
    193  1.35    cegger 	struct grf_softc *gp = device_lookup_private(&grf_cd, GRFUNIT(dev));
    194   1.1       oki 	int error;
    195   1.1       oki 
    196   1.9   msaitoh 	if ((gp->g_flags & GF_ALIVE) == 0)
    197  1.29   minoura 		return ENXIO;
    198   1.9   msaitoh 
    199   1.1       oki 	error = 0;
    200   1.1       oki 	switch (cmd) {
    201   1.1       oki 
    202   1.1       oki 	case GRFIOCGINFO:
    203  1.33  christos 		memcpy(data, (void *)&gp->g_display, sizeof(struct grfinfo));
    204   1.1       oki 		break;
    205   1.1       oki 
    206   1.1       oki 	case GRFIOCON:
    207  1.29   minoura 		error = grfon(gp);
    208   1.1       oki 		break;
    209   1.1       oki 
    210   1.1       oki 	case GRFIOCOFF:
    211  1.29   minoura 		error = grfoff(gp);
    212   1.1       oki 		break;
    213   1.1       oki 
    214   1.1       oki 	case GRFIOCMAP:
    215  1.33  christos 		error = grfmap(dev, (void **)data, l->l_proc);
    216   1.1       oki 		break;
    217   1.1       oki 
    218   1.1       oki 	case GRFIOCUNMAP:
    219  1.33  christos 		error = grfunmap(dev, *(void **)data, l->l_proc);
    220   1.1       oki 		break;
    221   1.1       oki 
    222   1.1       oki 	case GRFSETVMODE:
    223   1.1       oki 		error = (*gp->g_sw->gd_mode)(gp, GM_GRFSETVMODE, data);
    224   1.1       oki 		if (error == 0)
    225   1.1       oki 			ite_reinit(unit);
    226   1.1       oki 		break;
    227   1.1       oki 
    228   1.1       oki 	default:
    229   1.1       oki 		error = EINVAL;
    230   1.1       oki 		break;
    231   1.1       oki 
    232   1.1       oki 	}
    233  1.29   minoura 	return error;
    234   1.1       oki }
    235   1.1       oki 
    236   1.1       oki /*ARGSUSED*/
    237  1.17    simonb paddr_t
    238  1.30       chs grfmmap(dev_t dev, off_t off, int prot)
    239   1.1       oki {
    240   1.9   msaitoh 
    241  1.35    cegger 	return grfaddr(device_lookup_private(&grf_cd, GRFUNIT(dev)), off);
    242   1.1       oki }
    243   1.1       oki 
    244   1.2       oki int
    245  1.30       chs grfon(struct grf_softc *gp)
    246   1.1       oki {
    247  1.37     isaki 	int unit = device_unit(gp->g_device);
    248   1.1       oki 
    249   1.1       oki 	/*
    250   1.1       oki 	 * XXX: iteoff call relies on devices being in same order
    251   1.1       oki 	 * as ITEs and the fact that iteoff only uses the minor part
    252   1.1       oki 	 * of the dev arg.
    253   1.1       oki 	 */
    254   1.1       oki 	iteoff(unit, 2);
    255  1.29   minoura 
    256  1.33  christos 	return (*gp->g_sw->gd_mode)(gp, GM_GRFON, (void *) 0);
    257   1.1       oki }
    258   1.1       oki 
    259   1.2       oki int
    260  1.30       chs grfoff(struct grf_softc *gp)
    261   1.1       oki {
    262  1.37     isaki 	int unit = device_unit(gp->g_device);
    263   1.1       oki 	int error;
    264   1.1       oki 
    265  1.29   minoura #if 0				/* always fails in EINVAL... */
    266  1.33  christos 	(void) grfunmap(dev, (void *) 0, curproc);
    267  1.29   minoura #endif
    268  1.33  christos 	error = (*gp->g_sw->gd_mode)(gp, GM_GRFOFF, (void *) 0);
    269   1.1       oki 	/* XXX: see comment for iteoff above */
    270   1.1       oki 	iteon(unit, 2);
    271  1.29   minoura 
    272  1.29   minoura 	return error;
    273   1.1       oki }
    274   1.1       oki 
    275  1.17    simonb off_t
    276  1.30       chs grfaddr(struct grf_softc *gp, off_t off)
    277   1.1       oki {
    278  1.30       chs 	struct grfinfo *gi = &gp->g_display;
    279   1.1       oki 
    280   1.1       oki 	/* control registers */
    281   1.1       oki 	if (off >= 0 && off < gi->gd_regsize)
    282  1.29   minoura 		return ((u_int)gi->gd_regaddr + off) >> PGSHIFT;
    283   1.1       oki 
    284   1.1       oki 	/* frame buffer */
    285   1.1       oki 	if (off >= gi->gd_regsize && off < gi->gd_regsize+gi->gd_fbsize) {
    286   1.1       oki 		off -= gi->gd_regsize;
    287  1.29   minoura 		return ((u_int)gi->gd_fbaddr + off) >> PGSHIFT;
    288   1.1       oki 	}
    289   1.1       oki 	/* bogus */
    290  1.29   minoura 	return -1;
    291   1.1       oki }
    292   1.1       oki 
    293   1.2       oki int
    294  1.33  christos grfmap(dev_t dev, void **addrp, struct proc *p)
    295   1.1       oki {
    296  1.35    cegger 	struct grf_softc *gp = device_lookup_private(&grf_cd, GRFUNIT(dev));
    297   1.1       oki 	int len, error;
    298   1.1       oki 	struct vnode vn;
    299   1.1       oki 	int flags;
    300   1.1       oki 
    301   1.1       oki #ifdef DEBUG
    302   1.1       oki 	if (grfdebug & GDB_MMAP)
    303   1.4  christos 		printf("grfmap(%d): addr %p\n", p->p_pid, *addrp);
    304   1.1       oki #endif
    305  1.29   minoura 
    306   1.1       oki 	len = gp->g_display.gd_regsize + gp->g_display.gd_fbsize;
    307   1.1       oki 	flags = MAP_SHARED;
    308   1.1       oki 	if (*addrp)
    309   1.1       oki 		flags |= MAP_FIXED;
    310   1.1       oki 	else
    311  1.26    atatat 		*addrp =
    312  1.33  christos 		    (void *)VM_DEFAULT_ADDRESS(p->p_vmspace->vm_daddr, len);
    313   1.1       oki 	vn.v_type = VCHR;			/* XXX */
    314   1.1       oki 	vn.v_rdev = dev;			/* XXX */
    315  1.11   minoura 	error = uvm_mmap(&p->p_vmspace->vm_map, (vaddr_t *)addrp,
    316  1.11   minoura 			 (vsize_t)len, VM_PROT_ALL, VM_PROT_ALL,
    317  1.33  christos 			 flags, (void *)&vn, 0,
    318  1.15   thorpej 			 p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur);
    319   1.1       oki 	if (error == 0)
    320   1.1       oki 		(void) (*gp->g_sw->gd_mode)(gp, GM_MAP, *addrp);
    321  1.29   minoura 
    322  1.29   minoura 	return error;
    323   1.1       oki }
    324   1.1       oki 
    325   1.2       oki int
    326  1.33  christos grfunmap(dev_t dev, void *addr, struct proc *p)
    327   1.1       oki {
    328  1.35    cegger 	struct grf_softc *gp = device_lookup_private(&grf_cd, GRFUNIT(dev));
    329  1.11   minoura 	vsize_t size;
    330   1.1       oki 
    331   1.1       oki #ifdef DEBUG
    332   1.1       oki 	if (grfdebug & GDB_MMAP)
    333   1.4  christos 		printf("grfunmap(%d): dev %x addr %p\n", p->p_pid, dev, addr);
    334   1.1       oki #endif
    335   1.1       oki 	if (addr == 0)
    336  1.29   minoura 		return EINVAL;		/* XXX: how do we deal with this? */
    337   1.1       oki 	(void) (*gp->g_sw->gd_mode)(gp, GM_UNMAP, 0);
    338   1.1       oki 	size = round_page(gp->g_display.gd_regsize + gp->g_display.gd_fbsize);
    339  1.20       chs 	uvm_unmap(&p->p_vmspace->vm_map, (vaddr_t)addr,
    340  1.12     chuck 	    (vaddr_t)addr + size);
    341  1.29   minoura 
    342  1.20       chs 	return 0;
    343   1.1       oki }
    344