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