xref: /netbsd-src/sys/arch/amiga/dev/grf.c (revision 53d1339bf7f9c7367b35a9e1ebe693f9b047a47b)
1 /*	$NetBSD: grf.c,v 1.66 2021/04/27 14:48:28 thorpej Exp $ */
2 
3 /*
4  * Copyright (c) 1988 University of Utah.
5  * Copyright (c) 1990 The Regents of the University of California.
6  * 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.31 91/01/21$
37  *
38  *	@(#)grf.c	7.8 (Berkeley) 5/7/91
39  */
40 
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: grf.c,v 1.66 2021/04/27 14:48:28 thorpej Exp $");
43 
44 /*
45  * Graphics display driver for the Amiga
46  * This is the hardware-independent portion of the driver.
47  * Hardware access is through the grf_softc->g_mode routine.
48  */
49 
50 #include "view.h"
51 #include "grf.h"
52 #include "kbd.h"
53 #include "wsdisplay.h"
54 
55 #include <sys/param.h>
56 #include <sys/proc.h>
57 #include <sys/ioctl.h>
58 #include <sys/device.h>
59 #include <sys/file.h>
60 #include <sys/malloc.h>
61 #include <sys/systm.h>
62 #include <sys/vnode.h>
63 #include <sys/mman.h>
64 #include <sys/bus.h>
65 #include <sys/kauth.h>
66 
67 #include <machine/cpu.h>
68 
69 #include <dev/cons.h>
70 #include <dev/sun/fbio.h>
71 #include <dev/wscons/wsconsio.h>
72 #include <dev/wscons/wsdisplayvar.h>
73 #include <dev/rasops/rasops.h>
74 #include <dev/wscons/wsdisplay_vconsvar.h>
75 
76 #include <amiga/amiga/color.h>	/* DEBUG */
77 #include <amiga/amiga/device.h>
78 #include <amiga/dev/grfioctl.h>
79 #include <amiga/dev/grfvar.h>
80 #include <amiga/dev/itevar.h>
81 #include <amiga/dev/kbdvar.h>
82 #include <amiga/dev/viewioctl.h>
83 
84 #include <sys/conf.h>
85 
86 #if NGRF > 0
87 #include "ite.h"
88 #if NITE == 0
89 #define	ite_on(u,f)
90 #define	ite_off(u,f)
91 #define ite_reinit(d)
92 #endif
93 
94 int grfon(dev_t);
95 int grfoff(dev_t);
96 int grfsinfo(dev_t, struct grfdyninfo *);
97 
98 void grfattach(device_t, device_t, void *);
99 int grfmatch(device_t, cfdata_t, void *);
100 int grfprint(void *, const char *);
101 #ifdef DEBUG
102 void grfdebug(struct grf_softc *, const char *, ...);
103 #endif
104 /*
105  * pointers to grf drivers device structs
106  */
107 struct grf_softc *grfsp[NGRF];
108 
109 CFATTACH_DECL_NEW(grf, 0,
110     grfmatch, grfattach, NULL, NULL);
111 
112 dev_type_open(grfopen);
113 dev_type_close(grfclose);
114 dev_type_ioctl(grfioctl);
115 dev_type_mmap(grfmmap);
116 
117 const struct cdevsw grf_cdevsw = {
118 	.d_open = grfopen,
119 	.d_close = grfclose,
120 	.d_read = nullread,
121 	.d_write = nullwrite,
122 	.d_ioctl = grfioctl,
123 	.d_stop = nostop,
124 	.d_tty = notty,
125 	.d_poll = nopoll,
126 	.d_mmap = grfmmap,
127 	.d_kqfilter = nokqfilter,
128 	.d_discard = nodiscard,
129 	.d_flag = 0
130 };
131 
132 /*
133  * only used in console init.
134  */
135 static cfdata_t cfdata;
136 
137 #if NWSDISPLAY > 0
138 static struct vcons_screen console_vcons;
139 
140 static void grf_init_screen(void *, struct vcons_screen *, int, long *);
141 static struct rasops_info *grf_setup_rasops(struct grf_softc *,
142     struct vcons_screen *);
143 
144 cons_decl(grf);
145 #endif
146 
147 /*
148  * match if the unit of grf matches its perspective
149  * low level board driver.
150  */
151 int
152 grfmatch(device_t parent, cfdata_t cf, void *aux)
153 {
154 	struct grf_softc *psc;
155 
156 	psc = device_private(parent);
157 	if (cf->cf_unit != psc->g_unit)
158 		return(0);
159 	cfdata = cf;
160 	return(1);
161 }
162 
163 /*
164  * Attach.. plug pointer in and print some info.
165  * Then try and attach a wsdisplay or ite to us.
166  * Note: self is NULL durring console init.
167  */
168 void
169 grfattach(device_t parent, device_t self, void *aux)
170 {
171 #if NWSDISPLAY > 0
172 	struct wsemuldisplaydev_attach_args wa;
173 	long defattr;
174 #endif
175 	struct grf_softc *gp;
176 	int maj;
177 
178 	gp = device_private(parent);
179 	gp->g_device = self;
180 	grfsp[gp->g_unit] = gp;
181 
182 	/*
183 	 * find our major device number, make device
184 	 */
185 	maj = cdevsw_lookup_major(&grf_cdevsw);
186 	gp->g_grfdev = makedev(maj, gp->g_unit);
187 
188 	if (self != NULL) {
189 		printf(": width %d height %d", gp->g_display.gd_dwidth,
190 		    gp->g_display.gd_dheight);
191 		if (gp->g_display.gd_colors == 2)
192 			printf(" monochrome\n");
193 		else
194 			printf(" colors %d\n", gp->g_display.gd_colors);
195 
196 #if NWSDISPLAY > 0
197 		vcons_init(&gp->g_vd, gp, gp->g_defaultscr, gp->g_accessops);
198 		gp->g_vd.init_screen = grf_init_screen;
199 
200 		if (gp->g_flags & GF_CONSOLE) {
201 			console_vcons.scr_flags |= VCONS_SCREEN_IS_STATIC;
202 			vcons_init_screen(&gp->g_vd,
203 			    &console_vcons, 1, &defattr);
204 			gp->g_defaultscr->textops =
205 			    &console_vcons.scr_ri.ri_ops;
206 			wsdisplay_cnattach(gp->g_defaultscr,
207 			    &console_vcons.scr_ri, 0, 0, defattr);
208 			vcons_replay_msgbuf(&console_vcons);
209 		}
210 
211 		/* attach wsdisplay */
212 		wa.console = (gp->g_flags & GF_CONSOLE) != 0;
213 		wa.scrdata = gp->g_scrlist;
214 		wa.accessops = gp->g_accessops;
215 		wa.accesscookie = &gp->g_vd;
216 		config_found(self, &wa, wsemuldisplaydevprint,
217 		    CFARG_IATTR, "wsemuldisplaydev",
218 		    CFARG_EOL);
219 #endif  /* NWSDISPLAY > 0 */
220 	}
221 
222 #if NWSDISPLAY == 0
223 	/*
224 	 * try and attach an ite
225 	 */
226 	amiga_config_found(cfdata, self, gp, grfprint,
227 	    CFARG_IATTR, "grf",
228 	    CFARG_EOL);
229 #endif
230 }
231 
232 int
233 grfprint(void *aux, const char *pnp)
234 {
235 	if (pnp)
236 		aprint_normal("ite at %s", pnp);
237 	return(UNCONF);
238 }
239 
240 /*ARGSUSED*/
241 int
242 grfopen(dev_t dev, int flags, int devtype, struct lwp *l)
243 {
244 	struct grf_softc *gp;
245 
246 	if (GRFUNIT(dev) >= NGRF || (gp = grfsp[GRFUNIT(dev)]) == NULL)
247 		return(ENXIO);
248 
249 	if ((gp->g_flags & GF_ALIVE) == 0)
250 		return(ENXIO);
251 
252 	if ((gp->g_flags & (GF_OPEN|GF_EXCLUDE)) == (GF_OPEN|GF_EXCLUDE))
253 		return(EBUSY);
254 
255 	return(0);
256 }
257 
258 /*ARGSUSED*/
259 int
260 grfclose(dev_t dev, int flags, int mode, struct lwp *l)
261 {
262 	struct grf_softc *gp;
263 
264 	gp = grfsp[GRFUNIT(dev)];
265 	(void)grfoff(dev);
266 	gp->g_flags &= GF_ALIVE;
267 	return(0);
268 }
269 
270 /*ARGSUSED*/
271 int
272 grfioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
273 {
274 	struct grf_softc *gp;
275 	int error;
276 
277 	gp = grfsp[GRFUNIT(dev)];
278 	error = 0;
279 
280 	switch (cmd) {
281 	case OGRFIOCGINFO:
282 	        /* argl.. no bank-member.. */
283 	  	memcpy(data, (void *)&gp->g_display, sizeof(struct grfinfo)-4);
284 		break;
285 	case GRFIOCGINFO:
286 		memcpy(data, (void *)&gp->g_display, sizeof(struct grfinfo));
287 		break;
288 	case GRFIOCON:
289 		error = grfon(dev);
290 		break;
291 	case GRFIOCOFF:
292 		error = grfoff(dev);
293 		break;
294 	case GRFIOCSINFO:
295 		error = grfsinfo(dev, (struct grfdyninfo *) data);
296 		break;
297 	case GRFGETVMODE:
298 		return(gp->g_mode(gp, GM_GRFGETVMODE, data, 0, 0));
299 	case GRFSETVMODE:
300 		error = gp->g_mode(gp, GM_GRFSETVMODE, data, 0, 0);
301 		if (error == 0 && gp->g_itedev && !(gp->g_flags & GF_GRFON))
302 			ite_reinit(gp->g_itedev);
303 		break;
304 	case GRFGETNUMVM:
305 		return(gp->g_mode(gp, GM_GRFGETNUMVM, data, 0, 0));
306 	/*
307 	 * these are all hardware dependent, and have to be resolved
308 	 * in the respective driver.
309 	 */
310 	case GRFIOCPUTCMAP:
311 	case GRFIOCGETCMAP:
312 	case GRFIOCSSPRITEPOS:
313 	case GRFIOCGSPRITEPOS:
314 	case GRFIOCSSPRITEINF:
315 	case GRFIOCGSPRITEINF:
316 	case GRFIOCGSPRITEMAX:
317 	case GRFIOCBITBLT:
318     	case GRFIOCSETMON:
319 	case GRFTOGGLE: /* Toggles between Cirrus boards and native ECS on
320                      Amiga. 15/11/94 ill */
321 		/*
322 		 * We need the minor dev number to get the overlay/image
323 		 * information for grf_ul.
324 		 */
325 		return(gp->g_mode(gp, GM_GRFIOCTL, data, cmd, dev));
326 
327 	case GRFIOCBLANK:	/* blank ioctl, IOCON/OFF will turn ite on */
328 	case FBIOSVIDEO:
329 		error = gp->g_mode(gp, GM_GRFIOCTL, data, GRFIOCBLANK, dev);
330 		if (!error)
331 			gp->g_blank = *(int *)data;
332 		return (error);
333 
334 	case FBIOGVIDEO:
335 		*(int *)data = gp->g_blank;
336 		return (0);
337 
338 	default:
339 #if NVIEW > 0
340 		/*
341 		 * check to see whether it's a command recognized by the
342 		 * view code if the unit is 0
343 		 * XXX
344 		 */
345 		if (GRFUNIT(dev) == 0) {
346 			extern const struct cdevsw view_cdevsw;
347 
348 			return((*view_cdevsw.d_ioctl)(dev, cmd, data, flag, l));
349 		}
350 #endif
351 		error = EPASSTHROUGH;
352 		break;
353 
354 	}
355 	return(error);
356 }
357 
358 /*
359  * map the contents of a graphics display card into process'
360  * memory space.
361  */
362 paddr_t
363 grfmmap(dev_t dev, off_t off, int prot)
364 {
365 	struct grf_softc *gp;
366 	struct grfinfo *gi;
367 
368 	gp = grfsp[GRFUNIT(dev)];
369 	gi = &gp->g_display;
370 
371 	/*
372 	 * control registers
373 	 */
374 	if (off >= 0 && off < gi->gd_regsize)
375 		return MD_BTOP((paddr_t)gi->gd_regaddr + off);
376 
377 	/*
378 	 * frame buffer
379 	 */
380 	if (off >= gi->gd_regsize && off < gi->gd_regsize+gi->gd_fbsize) {
381 		off -= gi->gd_regsize;
382 		return MD_BTOP((paddr_t)gi->gd_fbaddr + off);
383 	}
384 	/* bogus */
385 	return(-1);
386 }
387 
388 int
389 grfon(dev_t dev)
390 {
391 	struct grf_softc *gp;
392 
393 	gp = grfsp[GRFUNIT(dev)];
394 
395 	if (gp->g_flags & GF_GRFON)
396 		return(0);
397 
398 	gp->g_flags |= GF_GRFON;
399 	if (gp->g_itedev != NODEV)
400 		ite_off(gp->g_itedev, 3);
401 
402 	return(gp->g_mode(gp, (dev & GRFOVDEV) ? GM_GRFOVON : GM_GRFON,
403 							NULL, 0, 0));
404 }
405 
406 int
407 grfoff(dev_t dev)
408 {
409 	struct grf_softc *gp;
410 	int error;
411 
412 	gp = grfsp[GRFUNIT(dev)];
413 
414 	if ((gp->g_flags & GF_GRFON) == 0)
415 		return(0);
416 
417 	gp->g_flags &= ~GF_GRFON;
418 	error = gp->g_mode(gp, (dev & GRFOVDEV) ? GM_GRFOVOFF : GM_GRFOFF,
419 							NULL, 0, 0);
420 
421 	/*
422 	 * Closely tied together no X's
423 	 */
424 	if (gp->g_itedev != NODEV)
425 		ite_on(gp->g_itedev, 2);
426 
427 	return(error);
428 }
429 
430 int
431 grfsinfo(dev_t dev, struct grfdyninfo *dyninfo)
432 {
433 	struct grf_softc *gp;
434 	int error;
435 
436 	gp = grfsp[GRFUNIT(dev)];
437 	error = gp->g_mode(gp, GM_GRFCONFIG, dyninfo, 0, 0);
438 
439 	/*
440 	 * Closely tied together no X's
441 	 */
442 	if (gp->g_itedev != NODEV)
443 		ite_reinit(gp->g_itedev);
444 	return(error);
445 }
446 
447 #if NWSDISPLAY > 0
448 void
449 grfcnprobe(struct consdev *cd)
450 {
451 	struct grf_softc *gp;
452 	int unit;
453 
454 	/*
455 	 * Find the first working grf device for being console.
456 	 * Ignore unit 0 (grfcc), which should use amidisplaycc instead.
457 	 */
458 	for (unit = 1; unit < NGRF; unit++) {
459 		gp = grfsp[unit];
460 		if (gp != NULL && (gp->g_flags & GF_ALIVE)) {
461 			cd->cn_pri = CN_INTERNAL;
462 			cd->cn_dev = NODEV;  /* initialized later by wscons */
463 			return;
464 		}
465 	}
466 
467 	/* no grf console alive */
468 	cd->cn_pri = CN_DEAD;
469 }
470 
471 void
472 grfcninit(struct consdev *cd)
473 {
474 	struct grf_softc *gp;
475 	struct rasops_info *ri;
476 	long defattr;
477 	int unit;
478 
479 	/* find console grf and set up wsdisplay for it */
480 	for (unit = 1; unit < NGRF; unit++) {
481 		gp = grfsp[unit];
482 		if (gp != NULL && (gp->g_flags & GF_ALIVE)) {
483 			gp->g_flags |= GF_CONSOLE;  /* we are console! */
484 
485 			gp->g_defaultscr->ncols = gp->g_display.gd_fbwidth /
486 			    gp->g_defaultscr->fontwidth;
487 			gp->g_defaultscr->nrows = gp->g_display.gd_fbheight /
488 			    gp->g_defaultscr->fontheight;
489 
490 			ri = grf_setup_rasops(gp, &console_vcons);
491 			console_vcons.scr_cookie = gp;
492 			defattr = 0;  /* XXX */
493 
494 			wsdisplay_preattach(gp->g_defaultscr, ri, 0, 0,
495 			    defattr);
496 #if NKBD > 0
497 			/* tell kbd device it is used as console keyboard */
498 			kbd_cnattach();
499 #endif
500 			return;
501 		}
502 	}
503 	panic("grfcninit: lost console");
504 }
505 
506 static void
507 grf_init_screen(void *cookie, struct vcons_screen *scr, int existing,
508     long *defattr)
509 {
510 	struct grf_softc *gp;
511 	struct rasops_info *ri __unused;
512 
513 	gp = cookie;
514 	ri = grf_setup_rasops(gp, scr);
515 }
516 
517 static struct rasops_info *
518 grf_setup_rasops(struct grf_softc *gp, struct vcons_screen *scr)
519 {
520 	struct rasops_info *ri;
521 	int i;
522 
523 	ri = &scr->scr_ri;
524 	scr->scr_flags |= VCONS_DONT_READ;
525 	memset(ri, 0, sizeof(struct rasops_info));
526 
527 	ri->ri_rows = gp->g_defaultscr->nrows;
528 	ri->ri_cols = gp->g_defaultscr->ncols;
529 	ri->ri_hw = scr;
530 	ri->ri_ops.cursor    = gp->g_emulops->cursor;
531 	ri->ri_ops.mapchar   = gp->g_emulops->mapchar;
532 	ri->ri_ops.copyrows  = gp->g_emulops->copyrows;
533 	ri->ri_ops.eraserows = gp->g_emulops->eraserows;
534 	ri->ri_ops.copycols  = gp->g_emulops->copycols;
535 	ri->ri_ops.erasecols = gp->g_emulops->erasecols;
536 	ri->ri_ops.putchar   = gp->g_emulops->putchar;
537 	ri->ri_ops.allocattr = gp->g_emulops->allocattr;
538 
539 	/* multiplication table for row-offsets */
540 	for (i = 0; i < ri->ri_rows; i++)
541 		gp->g_rowoffset[i] = i * ri->ri_cols;
542 
543 	return ri;
544 }
545 
546 /*
547  * Called as fallback for ioctls which are not handled by the specific
548  * grf driver.
549  */
550 int
551 grf_wsioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
552 {
553 	struct wsdisplayio_fbinfo *iofbi;
554 	struct wsdisplay_fbinfo *fbinfo;
555 	struct vcons_data *vd;
556 	struct grf_softc *gp;
557 	struct vcons_screen *scr;
558 	struct grfinfo *gi;
559 
560 	vd = v;
561 	gp = vd->cookie;
562 	scr = vd->active;
563 
564 	switch (cmd) {
565 	case WSDISPLAYIO_GET_FBINFO:
566 		if (scr != NULL) {
567 			iofbi = data;
568 			return wsdisplayio_get_fbinfo(&scr->scr_ri, iofbi);
569 		}
570 		return ENODEV;
571 
572 	case WSDISPLAYIO_GINFO:
573 		if (scr != NULL) {
574 			fbinfo = (struct wsdisplay_fbinfo *)data;
575 			gi = &gp->g_display;
576 
577 			/*
578 			 * We should return truth about the current mode here,
579 			 * because X11 wsfb driver depends on this!
580 			 */
581 			fbinfo->height = gi->gd_fbheight;
582 			fbinfo->width = gi->gd_fbwidth;
583 			fbinfo->depth = gi->gd_planes;
584 			fbinfo->cmsize = gi->gd_colors;
585 			return 0;
586 		}
587 		return ENODEV;
588 
589 	case WSDISPLAYIO_GTYPE:
590 		*(u_int *)data = WSDISPLAY_TYPE_GRF;
591 		return 0;
592 
593 	case WSDISPLAYIO_SMODE:
594 		if ((*(int *)data) != gp->g_wsmode) {
595 			gp->g_wsmode = *(int *)data;
596 			if (gp->g_wsmode == WSDISPLAYIO_MODE_EMUL &&
597 			    scr != NULL)
598 				vcons_redraw_screen(scr);
599 		}
600 		return 0;
601 	}
602 
603 	return EPASSTHROUGH;
604 }
605 
606 paddr_t
607 grf_wsmmap(void *v, void *vs, off_t off, int prot)
608 {
609 	struct vcons_data *vd;
610 	struct grf_softc *gp;
611 	struct grfinfo *gi;
612 
613 	vd = v;
614 	gp = vd->cookie;
615 	gi = &gp->g_display;
616 
617 	/* Normal fb mapping */
618 	if (off < gi->gd_fbsize)
619 		return MD_BTOP(((paddr_t)gi->gd_fbaddr) + off);
620 
621 	/*
622 	 * restrict all other mappings to processes with superuser privileges
623 	 * or the kernel itself
624 	 */
625 	if (kauth_authorize_machdep(kauth_cred_get(), KAUTH_MACHDEP_UNMANAGEDMEM,
626 	    NULL, NULL, NULL, NULL) != 0) {
627 		aprint_normal("%s: permission to mmap denied.\n",
628 		    device_xname(gp->g_device));
629 		return -1;
630 	}
631 
632 	/* Handle register mapping */
633 	if ((off >= (paddr_t)gi->gd_regaddr) &&
634 	    (off < ((paddr_t)gi->gd_regaddr + (size_t)gi->gd_regsize)))
635 		return MD_BTOP(off);
636 
637 	if ((off >= (paddr_t)gi->gd_fbaddr) &&
638 	    (off < ((paddr_t)gi->gd_fbaddr + (size_t)gi->gd_fbsize)))
639 		return MD_BTOP(off);
640 
641 	return -1;
642 }
643 
644 #endif  /* NWSDISPLAY > 0 */
645 
646 #endif	/* NGRF > 0 */
647