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