grf_compat.c revision 1.1.2.4 1 1.1.2.4 scottr /* $NetBSD: grf_compat.c,v 1.1.2.4 1999/11/18 08:15:49 scottr Exp $ */
2 1.1.2.1 scottr
3 1.1.2.1 scottr /*
4 1.1.2.1 scottr * Copyright (C) 1999 Scott Reynolds
5 1.1.2.1 scottr * All rights reserved.
6 1.1.2.1 scottr *
7 1.1.2.1 scottr * Redistribution and use in source and binary forms, with or without
8 1.1.2.1 scottr * modification, are permitted provided that the following conditions
9 1.1.2.1 scottr * are met:
10 1.1.2.1 scottr * 1. Redistributions of source code must retain the above copyright
11 1.1.2.1 scottr * notice, this list of conditions and the following disclaimer.
12 1.1.2.1 scottr * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.2.1 scottr * notice, this list of conditions and the following disclaimer in the
14 1.1.2.1 scottr * documentation and/or other materials provided with the distribution.
15 1.1.2.1 scottr * 3. The name of the author may not be used to endorse or promote products
16 1.1.2.1 scottr * derived from this software without specific prior written permission.
17 1.1.2.1 scottr *
18 1.1.2.1 scottr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1.2.1 scottr * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.1.2.1 scottr * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.1.2.1 scottr * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1.2.1 scottr * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 1.1.2.1 scottr * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 1.1.2.1 scottr * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 1.1.2.1 scottr * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 1.1.2.1 scottr * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 1.1.2.1 scottr * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.1.2.1 scottr */
29 1.1.2.1 scottr
30 1.1.2.1 scottr /*
31 1.1.2.1 scottr * macfb compatibility with legacy grf devices
32 1.1.2.1 scottr */
33 1.1.2.1 scottr
34 1.1.2.1 scottr #include <sys/param.h>
35 1.1.2.1 scottr #include <sys/systm.h>
36 1.1.2.1 scottr #include <sys/cdefs.h>
37 1.1.2.1 scottr #include <sys/conf.h>
38 1.1.2.1 scottr #include <sys/device.h>
39 1.1.2.1 scottr #include <sys/errno.h>
40 1.1.2.1 scottr #include <sys/ioctl.h>
41 1.1.2.1 scottr #include <sys/malloc.h>
42 1.1.2.1 scottr #include <sys/mman.h>
43 1.1.2.1 scottr #include <sys/proc.h>
44 1.1.2.1 scottr #include <sys/resourcevar.h>
45 1.1.2.1 scottr #include <sys/vnode.h>
46 1.1.2.1 scottr
47 1.1.2.1 scottr #include <machine/autoconf.h>
48 1.1.2.1 scottr #include <machine/bus.h>
49 1.1.2.1 scottr #include <machine/grfioctl.h>
50 1.1.2.1 scottr
51 1.1.2.1 scottr #include <mac68k/nubus/nubus.h>
52 1.1.2.1 scottr #include <mac68k/dev/grfvar.h>
53 1.1.2.1 scottr #include <mac68k/dev/macfbvar.h>
54 1.1.2.1 scottr
55 1.1.2.1 scottr #include <miscfs/specfs/specdev.h>
56 1.1.2.1 scottr
57 1.1.2.1 scottr #include <vm/vm.h>
58 1.1.2.1 scottr #include <uvm/uvm_extern.h>
59 1.1.2.1 scottr #include <uvm/uvm_map.h>
60 1.1.2.1 scottr
61 1.1.2.1 scottr cdev_decl(grf);
62 1.1.2.1 scottr
63 1.1.2.1 scottr void grf_scinit __P((struct grf_softc *, const char *, int));
64 1.1.2.1 scottr void grf_init __P((int));
65 1.1.2.1 scottr void grfattach __P((int));
66 1.1.2.2 scottr int grfmap __P((dev_t, struct macfb_softc *, caddr_t *, struct proc *));
67 1.1.2.2 scottr int grfunmap __P((dev_t, struct macfb_softc *, caddr_t, struct proc *));
68 1.1.2.1 scottr
69 1.1.2.1 scottr /* Non-private for the benefit of libkvm. */
70 1.1.2.1 scottr struct grf_softc *grf_softc;
71 1.1.2.1 scottr int numgrf = 0;
72 1.1.2.1 scottr
73 1.1.2.1 scottr /*
74 1.1.2.1 scottr * Initialize a softc to sane defaults.
75 1.1.2.1 scottr */
76 1.1.2.1 scottr void
77 1.1.2.1 scottr grf_scinit(sc, name, unit)
78 1.1.2.1 scottr struct grf_softc *sc;
79 1.1.2.1 scottr const char *name;
80 1.1.2.1 scottr int unit;
81 1.1.2.1 scottr {
82 1.1.2.1 scottr memset(sc, 0, sizeof(struct grf_softc));
83 1.1.2.1 scottr snprintf(sc->sc_xname, sizeof(sc->sc_xname), "%s%d", name, unit);
84 1.1.2.1 scottr sc->mfb_sc = NULL;
85 1.1.2.1 scottr }
86 1.1.2.1 scottr
87 1.1.2.1 scottr /*
88 1.1.2.1 scottr * (Re-)initialize the grf_softc block so that at least the requested
89 1.1.2.1 scottr * number of elements has been allocated. If this results in more
90 1.1.2.1 scottr * elements than we had prior to getting here, we initialize each of
91 1.1.2.1 scottr * them to avoid problems down the road.
92 1.1.2.1 scottr */
93 1.1.2.1 scottr void
94 1.1.2.1 scottr grf_init(n)
95 1.1.2.1 scottr int n;
96 1.1.2.1 scottr {
97 1.1.2.1 scottr struct grf_softc *sc;
98 1.1.2.1 scottr int i;
99 1.1.2.1 scottr
100 1.1.2.1 scottr if (n >= numgrf) {
101 1.1.2.1 scottr i = numgrf;
102 1.1.2.1 scottr numgrf = n + 1;
103 1.1.2.1 scottr
104 1.1.2.1 scottr if (grf_softc == NULL)
105 1.1.2.1 scottr sc = (struct grf_softc *)
106 1.1.2.1 scottr malloc(numgrf * sizeof(*sc),
107 1.1.2.1 scottr M_DEVBUF, M_NOWAIT);
108 1.1.2.1 scottr else
109 1.1.2.1 scottr sc = (struct grf_softc *)
110 1.1.2.1 scottr realloc(grf_softc, numgrf * sizeof(*sc),
111 1.1.2.1 scottr M_DEVBUF, M_NOWAIT);
112 1.1.2.1 scottr if (sc == NULL) {
113 1.1.2.1 scottr printf("WARNING: no memory for grf emulation\n");
114 1.1.2.1 scottr if (grf_softc != NULL)
115 1.1.2.1 scottr free(grf_softc, M_DEVBUF);
116 1.1.2.1 scottr return;
117 1.1.2.1 scottr }
118 1.1.2.1 scottr grf_softc = sc;
119 1.1.2.1 scottr
120 1.1.2.1 scottr /* Initialize per-softc structures. */
121 1.1.2.1 scottr while (i < numgrf) {
122 1.1.2.1 scottr grf_scinit(&grf_softc[i], "grf", i);
123 1.1.2.1 scottr i++;
124 1.1.2.1 scottr }
125 1.1.2.1 scottr }
126 1.1.2.1 scottr }
127 1.1.2.1 scottr
128 1.1.2.1 scottr /*
129 1.1.2.1 scottr * Called by main() during pseudo-device attachment. If we had a
130 1.1.2.1 scottr * way to configure additional grf devices later, this would actually
131 1.1.2.1 scottr * allocate enough space for them. As it stands, it's nonsensical,
132 1.1.2.1 scottr * so other than a basic sanity check we do nothing.
133 1.1.2.1 scottr */
134 1.1.2.1 scottr void
135 1.1.2.1 scottr grfattach(n)
136 1.1.2.1 scottr int n;
137 1.1.2.1 scottr {
138 1.1.2.1 scottr if (n <= 0) {
139 1.1.2.1 scottr #ifdef DIAGNOSTIC
140 1.1.2.1 scottr panic("grfattach: count <= 0");
141 1.1.2.1 scottr #endif
142 1.1.2.1 scottr return;
143 1.1.2.1 scottr }
144 1.1.2.1 scottr
145 1.1.2.2 scottr #if 0 /* XXX someday, if we implement a way to attach after autoconfig */
146 1.1.2.1 scottr grf_init(n);
147 1.1.2.1 scottr #endif
148 1.1.2.1 scottr }
149 1.1.2.1 scottr
150 1.1.2.1 scottr /*
151 1.1.2.1 scottr * Called from macfb_attach() after setting up the frame buffer. Since
152 1.1.2.1 scottr * there is a 1:1 correspondence between the macfb device and the grf
153 1.1.2.1 scottr * device, the only bit of information we really need is the macfb_softc.
154 1.1.2.1 scottr */
155 1.1.2.1 scottr void
156 1.1.2.1 scottr grf_attach(sc, unit)
157 1.1.2.1 scottr struct macfb_softc *sc;
158 1.1.2.1 scottr int unit;
159 1.1.2.1 scottr {
160 1.1.2.1 scottr grf_init(unit);
161 1.1.2.1 scottr
162 1.1.2.1 scottr if (unit < numgrf)
163 1.1.2.1 scottr grf_softc[unit].mfb_sc = sc;
164 1.1.2.1 scottr }
165 1.1.2.1 scottr
166 1.1.2.1 scottr /*
167 1.1.2.1 scottr * Standard device ops
168 1.1.2.1 scottr */
169 1.1.2.1 scottr int
170 1.1.2.1 scottr grfopen(dev, flag, mode, p)
171 1.1.2.1 scottr dev_t dev;
172 1.1.2.1 scottr int flag;
173 1.1.2.1 scottr int mode;
174 1.1.2.1 scottr struct proc *p;
175 1.1.2.1 scottr {
176 1.1.2.1 scottr struct grf_softc *sc;
177 1.1.2.1 scottr int unit = GRFUNIT(dev);
178 1.1.2.1 scottr int rv = 0;
179 1.1.2.1 scottr
180 1.1.2.1 scottr if (grf_softc == NULL || unit >= numgrf)
181 1.1.2.1 scottr return ENXIO;
182 1.1.2.1 scottr
183 1.1.2.1 scottr sc = &grf_softc[unit];
184 1.1.2.1 scottr if (sc->mfb_sc != NULL)
185 1.1.2.1 scottr sc->sc_isopen++;
186 1.1.2.1 scottr else
187 1.1.2.1 scottr rv = ENXIO;
188 1.1.2.1 scottr
189 1.1.2.1 scottr return rv;
190 1.1.2.1 scottr }
191 1.1.2.1 scottr
192 1.1.2.1 scottr int
193 1.1.2.1 scottr grfclose(dev, flag, mode, p)
194 1.1.2.1 scottr dev_t dev;
195 1.1.2.1 scottr int flag;
196 1.1.2.1 scottr int mode;
197 1.1.2.1 scottr struct proc *p;
198 1.1.2.1 scottr {
199 1.1.2.1 scottr struct grf_softc *sc;
200 1.1.2.1 scottr int unit = GRFUNIT(dev);
201 1.1.2.1 scottr int rv = 0;
202 1.1.2.1 scottr
203 1.1.2.1 scottr if (grf_softc == NULL || unit >= numgrf)
204 1.1.2.1 scottr return ENXIO;
205 1.1.2.1 scottr
206 1.1.2.1 scottr sc = &grf_softc[unit];
207 1.1.2.1 scottr if (sc->mfb_sc != NULL && sc->sc_isopen)
208 1.1.2.1 scottr sc->sc_isopen = 0;
209 1.1.2.1 scottr else
210 1.1.2.1 scottr rv = ENXIO;
211 1.1.2.1 scottr
212 1.1.2.1 scottr return rv;
213 1.1.2.1 scottr }
214 1.1.2.1 scottr
215 1.1.2.1 scottr int
216 1.1.2.1 scottr grfread(dev, uio, ioflag)
217 1.1.2.1 scottr dev_t dev;
218 1.1.2.1 scottr struct uio *uio;
219 1.1.2.1 scottr int ioflag;
220 1.1.2.1 scottr {
221 1.1.2.1 scottr return ENXIO;
222 1.1.2.1 scottr }
223 1.1.2.1 scottr
224 1.1.2.1 scottr int
225 1.1.2.1 scottr grfwrite(dev, uio, ioflag)
226 1.1.2.1 scottr dev_t dev;
227 1.1.2.1 scottr struct uio *uio;
228 1.1.2.1 scottr int ioflag;
229 1.1.2.1 scottr {
230 1.1.2.1 scottr return ENXIO;
231 1.1.2.1 scottr }
232 1.1.2.1 scottr
233 1.1.2.1 scottr int
234 1.1.2.1 scottr grfioctl(dev, cmd, data, flag, p)
235 1.1.2.1 scottr dev_t dev;
236 1.1.2.1 scottr u_long cmd;
237 1.1.2.1 scottr caddr_t data;
238 1.1.2.1 scottr int flag;
239 1.1.2.1 scottr struct proc *p;
240 1.1.2.1 scottr {
241 1.1.2.1 scottr struct grf_softc *sc;
242 1.1.2.1 scottr struct macfb_devconfig *dc;
243 1.1.2.1 scottr struct grfinfo *gd;
244 1.1.2.1 scottr struct grfmode *gm;
245 1.1.2.1 scottr int unit = GRFUNIT(dev);
246 1.1.2.1 scottr int rv;
247 1.1.2.1 scottr
248 1.1.2.1 scottr if (grf_softc == NULL || unit >= numgrf)
249 1.1.2.1 scottr return ENXIO;
250 1.1.2.1 scottr
251 1.1.2.1 scottr sc = &grf_softc[unit];
252 1.1.2.1 scottr if (sc->mfb_sc == NULL)
253 1.1.2.1 scottr return ENXIO;
254 1.1.2.1 scottr
255 1.1.2.1 scottr dc = sc->mfb_sc->sc_dc;
256 1.1.2.1 scottr
257 1.1.2.1 scottr switch (cmd) {
258 1.1.2.1 scottr case GRFIOCGINFO:
259 1.1.2.1 scottr gd = (struct grfinfo *)data;
260 1.1.2.1 scottr memset(gd, 0, sizeof(struct grfinfo));
261 1.1.2.1 scottr gd->gd_fbaddr = (caddr_t)dc->dc_paddr;
262 1.1.2.1 scottr gd->gd_fbsize = dc->dc_size;
263 1.1.2.1 scottr gd->gd_colors = (short)(1 << dc->dc_depth);
264 1.1.2.1 scottr gd->gd_planes = (short)dc->dc_depth;
265 1.1.2.1 scottr gd->gd_fbwidth = dc->dc_wid;
266 1.1.2.1 scottr gd->gd_fbheight = dc->dc_ht;
267 1.1.2.1 scottr gd->gd_fbrowbytes = dc->dc_rowbytes;
268 1.1.2.1 scottr gd->gd_dwidth = dc->dc_raster.width;
269 1.1.2.1 scottr gd->gd_dheight = dc->dc_raster.height;
270 1.1.2.1 scottr rv = 0;
271 1.1.2.1 scottr break;
272 1.1.2.1 scottr
273 1.1.2.1 scottr case GRFIOCON:
274 1.1.2.1 scottr case GRFIOCOFF:
275 1.1.2.1 scottr /* Nothing to do */
276 1.1.2.1 scottr rv = 0;
277 1.1.2.1 scottr break;
278 1.1.2.1 scottr
279 1.1.2.1 scottr case GRFIOCMAP:
280 1.1.2.2 scottr rv = grfmap(dev, sc->mfb_sc, (caddr_t *)data, p);
281 1.1.2.1 scottr break;
282 1.1.2.1 scottr
283 1.1.2.1 scottr case GRFIOCUNMAP:
284 1.1.2.2 scottr rv = grfunmap(dev, sc->mfb_sc, *(caddr_t *)data, p);
285 1.1.2.1 scottr break;
286 1.1.2.1 scottr
287 1.1.2.1 scottr case GRFIOCGMODE:
288 1.1.2.1 scottr gm = (struct grfmode *)data;
289 1.1.2.1 scottr memset(gm, 0, sizeof(struct grfmode));
290 1.1.2.1 scottr gm->fbbase = (char *)dc->dc_vaddr;
291 1.1.2.1 scottr gm->fbsize = dc->dc_size;
292 1.1.2.1 scottr gm->fboff = dc->dc_offset;
293 1.1.2.1 scottr gm->rowbytes = dc->dc_rowbytes;
294 1.1.2.1 scottr gm->width = dc->dc_wid;
295 1.1.2.1 scottr gm->height = dc->dc_ht;
296 1.1.2.1 scottr gm->psize = dc->dc_depth;
297 1.1.2.1 scottr rv = 0;
298 1.1.2.1 scottr break;
299 1.1.2.1 scottr
300 1.1.2.1 scottr case GRFIOCLISTMODES:
301 1.1.2.1 scottr case GRFIOCGETMODE:
302 1.1.2.1 scottr case GRFIOCSETMODE:
303 1.1.2.3 scottr /* NONE of these operations are (officially) supported. */
304 1.1.2.1 scottr default:
305 1.1.2.3 scottr rv = EINVAL;
306 1.1.2.1 scottr break;
307 1.1.2.1 scottr }
308 1.1.2.1 scottr return rv;
309 1.1.2.1 scottr }
310 1.1.2.1 scottr
311 1.1.2.1 scottr int
312 1.1.2.1 scottr grfpoll(dev, events, p)
313 1.1.2.1 scottr dev_t dev;
314 1.1.2.1 scottr int events;
315 1.1.2.1 scottr struct proc *p;
316 1.1.2.1 scottr {
317 1.1.2.1 scottr return EINVAL;
318 1.1.2.1 scottr }
319 1.1.2.1 scottr
320 1.1.2.1 scottr int
321 1.1.2.1 scottr grfmmap(dev, off, prot)
322 1.1.2.1 scottr dev_t dev;
323 1.1.2.1 scottr int off;
324 1.1.2.1 scottr int prot;
325 1.1.2.1 scottr {
326 1.1.2.1 scottr struct grf_softc *sc;
327 1.1.2.1 scottr struct macfb_devconfig *dc;
328 1.1.2.1 scottr u_long addr;
329 1.1.2.1 scottr int unit = GRFUNIT(dev);
330 1.1.2.1 scottr
331 1.1.2.1 scottr if (grf_softc == NULL || unit >= numgrf)
332 1.1.2.1 scottr return ENXIO;
333 1.1.2.1 scottr
334 1.1.2.1 scottr sc = &grf_softc[unit];
335 1.1.2.1 scottr if (sc->mfb_sc == NULL || !sc->sc_isopen)
336 1.1.2.1 scottr return ENXIO;
337 1.1.2.1 scottr
338 1.1.2.1 scottr dc = sc->mfb_sc->sc_dc;
339 1.1.2.1 scottr
340 1.1.2.1 scottr if (off >= 0 &&
341 1.1.2.1 scottr off < m68k_round_page(dc->dc_offset + dc->dc_size))
342 1.1.2.3 scottr addr = m68k_btop(dc->dc_paddr + off);
343 1.1.2.1 scottr else
344 1.1.2.1 scottr addr = (-1); /* XXX bogus */
345 1.1.2.1 scottr
346 1.1.2.1 scottr return (int)addr;
347 1.1.2.1 scottr }
348 1.1.2.1 scottr
349 1.1.2.1 scottr int
350 1.1.2.2 scottr grfmap(dev, sc, addrp, p)
351 1.1.2.2 scottr dev_t dev;
352 1.1.2.1 scottr struct macfb_softc *sc;
353 1.1.2.1 scottr caddr_t *addrp;
354 1.1.2.1 scottr struct proc *p;
355 1.1.2.1 scottr {
356 1.1.2.1 scottr struct specinfo si;
357 1.1.2.1 scottr struct vnode vn;
358 1.1.2.1 scottr u_long len;
359 1.1.2.1 scottr int error, flags;
360 1.1.2.1 scottr
361 1.1.2.1 scottr *addrp = (caddr_t)sc->sc_dc->dc_paddr;
362 1.1.2.1 scottr len = m68k_round_page(sc->sc_dc->dc_offset + sc->sc_dc->dc_size);
363 1.1.2.4 scottr flags = MAP_SHARED | MAP_FIXED;
364 1.1.2.1 scottr
365 1.1.2.2 scottr vn.v_type = VCHR; /* XXX */
366 1.1.2.2 scottr vn.v_specinfo = &si; /* XXX */
367 1.1.2.2 scottr vn.v_rdev = dev; /* XXX */
368 1.1.2.1 scottr
369 1.1.2.1 scottr error = uvm_mmap(&p->p_vmspace->vm_map, (vaddr_t *)addrp,
370 1.1.2.1 scottr (vsize_t)len, VM_PROT_ALL, VM_PROT_ALL,
371 1.1.2.1 scottr flags, (caddr_t)&vn, 0, p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur);
372 1.1.2.1 scottr
373 1.1.2.1 scottr /* Offset into page: */
374 1.1.2.1 scottr *addrp += sc->sc_dc->dc_offset;
375 1.1.2.1 scottr
376 1.1.2.1 scottr return (error);
377 1.1.2.1 scottr }
378 1.1.2.1 scottr
379 1.1.2.1 scottr int
380 1.1.2.2 scottr grfunmap(dev, sc, addr, p)
381 1.1.2.2 scottr dev_t dev;
382 1.1.2.1 scottr struct macfb_softc *sc;
383 1.1.2.1 scottr caddr_t addr;
384 1.1.2.1 scottr struct proc *p;
385 1.1.2.1 scottr {
386 1.1.2.1 scottr vm_size_t size;
387 1.1.2.1 scottr int rv;
388 1.1.2.1 scottr
389 1.1.2.1 scottr addr -= sc->sc_dc->dc_offset;
390 1.1.2.1 scottr
391 1.1.2.1 scottr if (addr <= 0)
392 1.1.2.1 scottr return (-1);
393 1.1.2.1 scottr
394 1.1.2.1 scottr size = m68k_round_page(sc->sc_dc->dc_offset + sc->sc_dc->dc_size);
395 1.1.2.1 scottr
396 1.1.2.1 scottr rv = uvm_unmap(&p->p_vmspace->vm_map, (vaddr_t)addr,
397 1.1.2.1 scottr (vaddr_t)addr + size);
398 1.1.2.1 scottr
399 1.1.2.1 scottr return (rv == KERN_SUCCESS ? 0 : EINVAL);
400 1.1.2.1 scottr }
401