1 /* $NetBSD: grf.c,v 1.37 2009/01/17 10:02:23 isaki Exp $ */
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * the Systems Programming Group of the University of Utah Computer
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
35 * from: Utah $Hdr: grf.c 1.36 93/08/13$
37 * @(#)grf.c 8.4 (Berkeley) 1/12/94
40 * Copyright (c) 1988 University of Utah.
42 * This code is derived from software contributed to Berkeley by
43 * the Systems Programming Group of the University of Utah Computer
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
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.
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
74 * from: Utah $Hdr: grf.c 1.36 93/08/13$
76 * @(#)grf.c 8.4 (Berkeley) 1/12/94
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.
85 #include <sys/cdefs.h>
86 __KERNEL_RCSID(0, "$NetBSD: grf.c,v 1.37 2009/01/17 10:02:23 isaki Exp $");
88 #include <sys/param.h>
89 #include <sys/systm.h>
90 #include <sys/device.h>
92 #include <sys/resourcevar.h>
93 #include <sys/ioctl.h>
95 #include <sys/malloc.h>
96 #include <sys/vnode.h>
100 #include <machine/cpu.h>
101 #include <machine/grfioctl.h>
103 #include <x68k/dev/grfvar.h>
104 #include <x68k/dev/itevar.h>
106 #include <uvm/uvm_extern.h>
107 #include <uvm/uvm_map.h>
109 #include <miscfs/specfs/specdev.h>
115 #define ite_reinit(u)
120 #define GDB_DEVNO 0x01
121 #define GDB_MMAP 0x02
122 #define GDB_IOMAP 0x04
123 #define GDB_LOCK 0x08
126 static int grfon(struct grf_softc
*);
127 static int grfoff(struct grf_softc
*);
128 static off_t
grfaddr(struct grf_softc
*, off_t
);
129 static int grfmap(dev_t
, void **, struct proc
*);
130 static int grfunmap(dev_t
, void *, struct proc
*);
132 extern struct cfdriver grf_cd
;
134 dev_type_open(grfopen
);
135 dev_type_close(grfclose
);
136 dev_type_ioctl(grfioctl
);
137 dev_type_mmap(grfmmap
);
139 const struct cdevsw grf_cdevsw
= {
140 grfopen
, grfclose
, nullread
, nullwrite
, grfioctl
,
141 nostop
, notty
, nopoll
, grfmmap
, nokqfilter
,
146 grfopen(dev_t dev
, int flags
, int mode
, struct lwp
*l
)
148 struct grf_softc
*gp
;
151 gp
= device_lookup_private(&grf_cd
, GRFUNIT(dev
));
155 if ((gp
->g_flags
& GF_ALIVE
) == 0)
158 if ((gp
->g_flags
& (GF_OPEN
|GF_EXCLUDE
)) == (GF_OPEN
|GF_EXCLUDE
))
163 * XXX: always put in graphics mode.
166 if ((gp
->g_flags
& GF_OPEN
) == 0) {
167 gp
->g_flags
|= GF_OPEN
;
175 grfclose(dev_t dev
, int flags
, int mode
, struct lwp
*l
)
177 struct grf_softc
*gp
= device_lookup_private(&grf_cd
, GRFUNIT(dev
));
179 if ((gp
->g_flags
& GF_ALIVE
) == 0)
183 gp
->g_flags
&= GF_ALIVE
;
190 grfioctl(dev_t dev
, u_long cmd
, void *data
, int flag
, struct lwp
*l
)
192 int unit
= GRFUNIT(dev
);
193 struct grf_softc
*gp
= device_lookup_private(&grf_cd
, GRFUNIT(dev
));
196 if ((gp
->g_flags
& GF_ALIVE
) == 0)
203 memcpy(data
, (void *)&gp
->g_display
, sizeof(struct grfinfo
));
215 error
= grfmap(dev
, (void **)data
, l
->l_proc
);
219 error
= grfunmap(dev
, *(void **)data
, l
->l_proc
);
223 error
= (*gp
->g_sw
->gd_mode
)(gp
, GM_GRFSETVMODE
, data
);
238 grfmmap(dev_t dev
, off_t off
, int prot
)
241 return grfaddr(device_lookup_private(&grf_cd
, GRFUNIT(dev
)), off
);
245 grfon(struct grf_softc
*gp
)
247 int unit
= device_unit(gp
->g_device
);
250 * XXX: iteoff call relies on devices being in same order
251 * as ITEs and the fact that iteoff only uses the minor part
256 return (*gp
->g_sw
->gd_mode
)(gp
, GM_GRFON
, (void *) 0);
260 grfoff(struct grf_softc
*gp
)
262 int unit
= device_unit(gp
->g_device
);
265 #if 0 /* always fails in EINVAL... */
266 (void) grfunmap(dev
, (void *) 0, curproc
);
268 error
= (*gp
->g_sw
->gd_mode
)(gp
, GM_GRFOFF
, (void *) 0);
269 /* XXX: see comment for iteoff above */
276 grfaddr(struct grf_softc
*gp
, off_t off
)
278 struct grfinfo
*gi
= &gp
->g_display
;
280 /* control registers */
281 if (off
>= 0 && off
< gi
->gd_regsize
)
282 return ((u_int
)gi
->gd_regaddr
+ off
) >> PGSHIFT
;
285 if (off
>= gi
->gd_regsize
&& off
< gi
->gd_regsize
+gi
->gd_fbsize
) {
286 off
-= gi
->gd_regsize
;
287 return ((u_int
)gi
->gd_fbaddr
+ off
) >> PGSHIFT
;
294 grfmap(dev_t dev
, void **addrp
, struct proc
*p
)
296 struct grf_softc
*gp
= device_lookup_private(&grf_cd
, GRFUNIT(dev
));
302 if (grfdebug
& GDB_MMAP
)
303 printf("grfmap(%d): addr %p\n", p
->p_pid
, *addrp
);
306 len
= gp
->g_display
.gd_regsize
+ gp
->g_display
.gd_fbsize
;
312 (void *)VM_DEFAULT_ADDRESS(p
->p_vmspace
->vm_daddr
, len
);
313 vn
.v_type
= VCHR
; /* XXX */
314 vn
.v_rdev
= dev
; /* XXX */
315 error
= uvm_mmap(&p
->p_vmspace
->vm_map
, (vaddr_t
*)addrp
,
316 (vsize_t
)len
, VM_PROT_ALL
, VM_PROT_ALL
,
317 flags
, (void *)&vn
, 0,
318 p
->p_rlimit
[RLIMIT_MEMLOCK
].rlim_cur
);
320 (void) (*gp
->g_sw
->gd_mode
)(gp
, GM_MAP
, *addrp
);
326 grfunmap(dev_t dev
, void *addr
, struct proc
*p
)
328 struct grf_softc
*gp
= device_lookup_private(&grf_cd
, GRFUNIT(dev
));
332 if (grfdebug
& GDB_MMAP
)
333 printf("grfunmap(%d): dev %x addr %p\n", p
->p_pid
, dev
, addr
);
336 return EINVAL
; /* XXX: how do we deal with this? */
337 (void) (*gp
->g_sw
->gd_mode
)(gp
, GM_UNMAP
, 0);
338 size
= round_page(gp
->g_display
.gd_regsize
+ gp
->g_display
.gd_fbsize
);
339 uvm_unmap(&p
->p_vmspace
->vm_map
, (vaddr_t
)addr
,
340 (vaddr_t
)addr
+ size
);