grf.c revision 1.43 1 /* $NetBSD: grf.c,v 1.43 2014/03/16 05:20:26 dholland 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.43 2014/03/16 05:20:26 dholland 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/file.h>
57 #include <sys/malloc.h>
58 #include <sys/vnode.h>
59 #include <sys/mman.h>
60 #include <sys/conf.h>
61
62 #include <machine/cpu.h>
63 #include <machine/grfioctl.h>
64
65 #include <x68k/dev/grfvar.h>
66 #include <x68k/dev/itevar.h>
67
68 #include <uvm/uvm_extern.h>
69 #include <uvm/uvm_map.h>
70
71 #include <miscfs/specfs/specdev.h>
72
73 #include "ite.h"
74 #if NITE == 0
75 #define iteon(u,f) 0
76 #define iteoff(u,f)
77 #define ite_reinit(u)
78 #endif
79
80 #ifdef DEBUG
81 int grfdebug = 0;
82 #define GDB_DEVNO 0x01
83 #define GDB_MMAP 0x02
84 #define GDB_IOMAP 0x04
85 #define GDB_LOCK 0x08
86 #endif
87
88 static int grfon(struct grf_softc *);
89 static int grfoff(struct grf_softc *);
90 static off_t grfaddr(struct grf_softc *, off_t);
91 static int grfmap(dev_t, void **, struct proc *);
92 static int grfunmap(dev_t, void *, struct proc *);
93
94 extern struct cfdriver grf_cd;
95
96 dev_type_open(grfopen);
97 dev_type_close(grfclose);
98 dev_type_ioctl(grfioctl);
99 dev_type_mmap(grfmmap);
100
101 const struct cdevsw grf_cdevsw = {
102 .d_open = grfopen,
103 .d_close = grfclose,
104 .d_read = nullread,
105 .d_write = nullwrite,
106 .d_ioctl = grfioctl,
107 .d_stop = nostop,
108 .d_tty = notty,
109 .d_poll = nopoll,
110 .d_mmap = grfmmap,
111 .d_kqfilter = nokqfilter,
112 .d_flag = 0
113 };
114
115 /*ARGSUSED*/
116 int
117 grfopen(dev_t dev, int flags, int mode, struct lwp *l)
118 {
119 struct grf_softc *gp;
120 int error = 0;
121
122 gp = device_lookup_private(&grf_cd, GRFUNIT(dev));
123 if (gp == NULL)
124 return ENXIO;
125
126 if ((gp->g_flags & GF_ALIVE) == 0)
127 return ENXIO;
128
129 if ((gp->g_flags & (GF_OPEN|GF_EXCLUDE)) == (GF_OPEN|GF_EXCLUDE))
130 return EBUSY;
131
132 /*
133 * First open.
134 * XXX: always put in graphics mode.
135 */
136 error = 0;
137 if ((gp->g_flags & GF_OPEN) == 0) {
138 gp->g_flags |= GF_OPEN;
139 error = grfon(gp);
140 }
141 return error;
142 }
143
144 /*ARGSUSED*/
145 int
146 grfclose(dev_t dev, int flags, int mode, struct lwp *l)
147 {
148 struct grf_softc *gp = device_lookup_private(&grf_cd, GRFUNIT(dev));
149
150 if ((gp->g_flags & GF_ALIVE) == 0)
151 return ENXIO;
152
153 (void) grfoff(gp);
154 gp->g_flags &= GF_ALIVE;
155
156 return 0;
157 }
158
159 /*ARGSUSED*/
160 int
161 grfioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
162 {
163 int unit = GRFUNIT(dev);
164 struct grf_softc *gp = device_lookup_private(&grf_cd, GRFUNIT(dev));
165 int error;
166
167 if ((gp->g_flags & GF_ALIVE) == 0)
168 return ENXIO;
169
170 error = 0;
171 switch (cmd) {
172
173 case GRFIOCGINFO:
174 memcpy(data, (void *)&gp->g_display, sizeof(struct grfinfo));
175 break;
176
177 case GRFIOCON:
178 error = grfon(gp);
179 break;
180
181 case GRFIOCOFF:
182 error = grfoff(gp);
183 break;
184
185 case GRFIOCMAP:
186 error = grfmap(dev, (void **)data, l->l_proc);
187 break;
188
189 case GRFIOCUNMAP:
190 error = grfunmap(dev, *(void **)data, l->l_proc);
191 break;
192
193 case GRFSETVMODE:
194 error = (*gp->g_sw->gd_mode)(gp, GM_GRFSETVMODE, data);
195 if (error == 0)
196 ite_reinit(unit);
197 break;
198
199 default:
200 error = EINVAL;
201 break;
202
203 }
204 return error;
205 }
206
207 /*ARGSUSED*/
208 paddr_t
209 grfmmap(dev_t dev, off_t off, int prot)
210 {
211
212 return grfaddr(device_lookup_private(&grf_cd, GRFUNIT(dev)), off);
213 }
214
215 int
216 grfon(struct grf_softc *gp)
217 {
218 int unit = device_unit(gp->g_device);
219
220 /*
221 * XXX: iteoff call relies on devices being in same order
222 * as ITEs and the fact that iteoff only uses the minor part
223 * of the dev arg.
224 */
225 iteoff(unit, 2);
226
227 return (*gp->g_sw->gd_mode)(gp, GM_GRFON, (void *) 0);
228 }
229
230 int
231 grfoff(struct grf_softc *gp)
232 {
233 int unit = device_unit(gp->g_device);
234 int error;
235
236 #if 0 /* always fails in EINVAL... */
237 (void) grfunmap(dev, (void *) 0, curproc);
238 #endif
239 error = (*gp->g_sw->gd_mode)(gp, GM_GRFOFF, (void *) 0);
240 /* XXX: see comment for iteoff above */
241 iteon(unit, 2);
242
243 return error;
244 }
245
246 off_t
247 grfaddr(struct grf_softc *gp, off_t off)
248 {
249 struct grfinfo *gi = &gp->g_display;
250
251 /* control registers */
252 if (off >= 0 && off < gi->gd_regsize)
253 return ((u_int)gi->gd_regaddr + off) >> PGSHIFT;
254
255 /* frame buffer */
256 if (off >= gi->gd_regsize && off < gi->gd_regsize+gi->gd_fbsize) {
257 off -= gi->gd_regsize;
258 return ((u_int)gi->gd_fbaddr + off) >> PGSHIFT;
259 }
260 /* bogus */
261 return -1;
262 }
263
264 int
265 grfmap(dev_t dev, void **addrp, struct proc *p)
266 {
267 struct grf_softc *gp = device_lookup_private(&grf_cd, GRFUNIT(dev));
268 int len, error;
269 struct vnode vn;
270 int flags;
271
272 #ifdef DEBUG
273 if (grfdebug & GDB_MMAP)
274 printf("grfmap(%d): addr %p\n", p->p_pid, *addrp);
275 #endif
276
277 len = gp->g_display.gd_regsize + gp->g_display.gd_fbsize;
278 flags = MAP_SHARED;
279 if (*addrp)
280 flags |= MAP_FIXED;
281 else
282 *addrp = (void *)p->p_emul->e_vm_default_addr(p,
283 (vaddr_t)p->p_vmspace->vm_daddr, len);
284
285 vn.v_type = VCHR; /* XXX */
286 vn.v_rdev = dev; /* XXX */
287 error = uvm_mmap(&p->p_vmspace->vm_map, (vaddr_t *)addrp,
288 (vsize_t)len, VM_PROT_ALL, VM_PROT_ALL,
289 flags, (void *)&vn, 0,
290 p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur);
291 if (error == 0)
292 (void) (*gp->g_sw->gd_mode)(gp, GM_MAP, *addrp);
293
294 return error;
295 }
296
297 int
298 grfunmap(dev_t dev, void *addr, struct proc *p)
299 {
300 struct grf_softc *gp = device_lookup_private(&grf_cd, GRFUNIT(dev));
301 vsize_t size;
302
303 #ifdef DEBUG
304 if (grfdebug & GDB_MMAP) {
305 printf("grfunmap(%d): dev %x addr %p\n",
306 p->p_pid, GRFUNIT(dev), addr);
307 }
308 #endif
309 if (addr == 0)
310 return EINVAL; /* XXX: how do we deal with this? */
311 (void) (*gp->g_sw->gd_mode)(gp, GM_UNMAP, 0);
312 size = round_page(gp->g_display.gd_regsize + gp->g_display.gd_fbsize);
313 uvm_unmap(&p->p_vmspace->vm_map, (vaddr_t)addr,
314 (vaddr_t)addr + size);
315
316 return 0;
317 }
318