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