grf.c revision 1.44 1 /* $NetBSD: grf.c,v 1.44 2014/07/25 08:10:35 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.44 2014/07/25 08:10:35 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_discard = nodiscard,
113 .d_flag = 0
114 };
115
116 /*ARGSUSED*/
117 int
118 grfopen(dev_t dev, int flags, int mode, struct lwp *l)
119 {
120 struct grf_softc *gp;
121 int error = 0;
122
123 gp = device_lookup_private(&grf_cd, GRFUNIT(dev));
124 if (gp == NULL)
125 return ENXIO;
126
127 if ((gp->g_flags & GF_ALIVE) == 0)
128 return ENXIO;
129
130 if ((gp->g_flags & (GF_OPEN|GF_EXCLUDE)) == (GF_OPEN|GF_EXCLUDE))
131 return EBUSY;
132
133 /*
134 * First open.
135 * XXX: always put in graphics mode.
136 */
137 error = 0;
138 if ((gp->g_flags & GF_OPEN) == 0) {
139 gp->g_flags |= GF_OPEN;
140 error = grfon(gp);
141 }
142 return error;
143 }
144
145 /*ARGSUSED*/
146 int
147 grfclose(dev_t dev, int flags, int mode, struct lwp *l)
148 {
149 struct grf_softc *gp = device_lookup_private(&grf_cd, GRFUNIT(dev));
150
151 if ((gp->g_flags & GF_ALIVE) == 0)
152 return ENXIO;
153
154 (void) grfoff(gp);
155 gp->g_flags &= GF_ALIVE;
156
157 return 0;
158 }
159
160 /*ARGSUSED*/
161 int
162 grfioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
163 {
164 int unit = GRFUNIT(dev);
165 struct grf_softc *gp = device_lookup_private(&grf_cd, GRFUNIT(dev));
166 int error;
167
168 if ((gp->g_flags & GF_ALIVE) == 0)
169 return ENXIO;
170
171 error = 0;
172 switch (cmd) {
173
174 case GRFIOCGINFO:
175 memcpy(data, (void *)&gp->g_display, sizeof(struct grfinfo));
176 break;
177
178 case GRFIOCON:
179 error = grfon(gp);
180 break;
181
182 case GRFIOCOFF:
183 error = grfoff(gp);
184 break;
185
186 case GRFIOCMAP:
187 error = grfmap(dev, (void **)data, l->l_proc);
188 break;
189
190 case GRFIOCUNMAP:
191 error = grfunmap(dev, *(void **)data, l->l_proc);
192 break;
193
194 case GRFSETVMODE:
195 error = (*gp->g_sw->gd_mode)(gp, GM_GRFSETVMODE, data);
196 if (error == 0)
197 ite_reinit(unit);
198 break;
199
200 default:
201 error = EINVAL;
202 break;
203
204 }
205 return error;
206 }
207
208 /*ARGSUSED*/
209 paddr_t
210 grfmmap(dev_t dev, off_t off, int prot)
211 {
212
213 return grfaddr(device_lookup_private(&grf_cd, GRFUNIT(dev)), off);
214 }
215
216 int
217 grfon(struct grf_softc *gp)
218 {
219 int unit = device_unit(gp->g_device);
220
221 /*
222 * XXX: iteoff call relies on devices being in same order
223 * as ITEs and the fact that iteoff only uses the minor part
224 * of the dev arg.
225 */
226 iteoff(unit, 2);
227
228 return (*gp->g_sw->gd_mode)(gp, GM_GRFON, (void *) 0);
229 }
230
231 int
232 grfoff(struct grf_softc *gp)
233 {
234 int unit = device_unit(gp->g_device);
235 int error;
236
237 #if 0 /* always fails in EINVAL... */
238 (void) grfunmap(dev, (void *) 0, curproc);
239 #endif
240 error = (*gp->g_sw->gd_mode)(gp, GM_GRFOFF, (void *) 0);
241 /* XXX: see comment for iteoff above */
242 iteon(unit, 2);
243
244 return error;
245 }
246
247 off_t
248 grfaddr(struct grf_softc *gp, off_t off)
249 {
250 struct grfinfo *gi = &gp->g_display;
251
252 /* control registers */
253 if (off >= 0 && off < gi->gd_regsize)
254 return ((u_int)gi->gd_regaddr + off) >> PGSHIFT;
255
256 /* frame buffer */
257 if (off >= gi->gd_regsize && off < gi->gd_regsize+gi->gd_fbsize) {
258 off -= gi->gd_regsize;
259 return ((u_int)gi->gd_fbaddr + off) >> PGSHIFT;
260 }
261 /* bogus */
262 return -1;
263 }
264
265 int
266 grfmap(dev_t dev, void **addrp, struct proc *p)
267 {
268 struct grf_softc *gp = device_lookup_private(&grf_cd, GRFUNIT(dev));
269 int len, error;
270 struct vnode vn;
271 int flags;
272
273 #ifdef DEBUG
274 if (grfdebug & GDB_MMAP)
275 printf("grfmap(%d): addr %p\n", p->p_pid, *addrp);
276 #endif
277
278 len = gp->g_display.gd_regsize + gp->g_display.gd_fbsize;
279 flags = MAP_SHARED;
280 if (*addrp)
281 flags |= MAP_FIXED;
282 else
283 *addrp = (void *)p->p_emul->e_vm_default_addr(p,
284 (vaddr_t)p->p_vmspace->vm_daddr, len);
285
286 vn.v_type = VCHR; /* XXX */
287 vn.v_rdev = dev; /* XXX */
288 error = uvm_mmap(&p->p_vmspace->vm_map, (vaddr_t *)addrp,
289 (vsize_t)len, VM_PROT_ALL, VM_PROT_ALL,
290 flags, (void *)&vn, 0,
291 p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur);
292 if (error == 0)
293 (void) (*gp->g_sw->gd_mode)(gp, GM_MAP, *addrp);
294
295 return error;
296 }
297
298 int
299 grfunmap(dev_t dev, void *addr, struct proc *p)
300 {
301 struct grf_softc *gp = device_lookup_private(&grf_cd, GRFUNIT(dev));
302 vsize_t size;
303
304 #ifdef DEBUG
305 if (grfdebug & GDB_MMAP) {
306 printf("grfunmap(%d): dev %x addr %p\n",
307 p->p_pid, GRFUNIT(dev), addr);
308 }
309 #endif
310 if (addr == 0)
311 return EINVAL; /* XXX: how do we deal with this? */
312 (void) (*gp->g_sw->gd_mode)(gp, GM_UNMAP, 0);
313 size = round_page(gp->g_display.gd_regsize + gp->g_display.gd_fbsize);
314 uvm_unmap(&p->p_vmspace->vm_map, (vaddr_t)addr,
315 (vaddr_t)addr + size);
316
317 return 0;
318 }
319