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