xref: /netbsd-src/sys/arch/amiga/dev/grf.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1 /*	$NetBSD: grf.c,v 1.67 2021/08/07 16:18:41 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.67 2021/08/07 16:18:41 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 		    CFARGS(.iattr = "wsemuldisplaydev"));
218 #endif  /* NWSDISPLAY > 0 */
219 	}
220 
221 #if NWSDISPLAY == 0
222 	/*
223 	 * try and attach an ite
224 	 */
225 	amiga_config_found(cfdata, self, gp, grfprint,
226 	    CFARGS(.iattr = "grf"));
227 #endif
228 }
229 
230 int
231 grfprint(void *aux, const char *pnp)
232 {
233 	if (pnp)
234 		aprint_normal("ite at %s", pnp);
235 	return(UNCONF);
236 }
237 
238 /*ARGSUSED*/
239 int
240 grfopen(dev_t dev, int flags, int devtype, struct lwp *l)
241 {
242 	struct grf_softc *gp;
243 
244 	if (GRFUNIT(dev) >= NGRF || (gp = grfsp[GRFUNIT(dev)]) == NULL)
245 		return(ENXIO);
246 
247 	if ((gp->g_flags & GF_ALIVE) == 0)
248 		return(ENXIO);
249 
250 	if ((gp->g_flags & (GF_OPEN|GF_EXCLUDE)) == (GF_OPEN|GF_EXCLUDE))
251 		return(EBUSY);
252 
253 	return(0);
254 }
255 
256 /*ARGSUSED*/
257 int
258 grfclose(dev_t dev, int flags, int mode, struct lwp *l)
259 {
260 	struct grf_softc *gp;
261 
262 	gp = grfsp[GRFUNIT(dev)];
263 	(void)grfoff(dev);
264 	gp->g_flags &= GF_ALIVE;
265 	return(0);
266 }
267 
268 /*ARGSUSED*/
269 int
270 grfioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
271 {
272 	struct grf_softc *gp;
273 	int error;
274 
275 	gp = grfsp[GRFUNIT(dev)];
276 	error = 0;
277 
278 	switch (cmd) {
279 	case OGRFIOCGINFO:
280 	        /* argl.. no bank-member.. */
281 	  	memcpy(data, (void *)&gp->g_display, sizeof(struct grfinfo)-4);
282 		break;
283 	case GRFIOCGINFO:
284 		memcpy(data, (void *)&gp->g_display, sizeof(struct grfinfo));
285 		break;
286 	case GRFIOCON:
287 		error = grfon(dev);
288 		break;
289 	case GRFIOCOFF:
290 		error = grfoff(dev);
291 		break;
292 	case GRFIOCSINFO:
293 		error = grfsinfo(dev, (struct grfdyninfo *) data);
294 		break;
295 	case GRFGETVMODE:
296 		return(gp->g_mode(gp, GM_GRFGETVMODE, data, 0, 0));
297 	case GRFSETVMODE:
298 		error = gp->g_mode(gp, GM_GRFSETVMODE, data, 0, 0);
299 		if (error == 0 && gp->g_itedev && !(gp->g_flags & GF_GRFON))
300 			ite_reinit(gp->g_itedev);
301 		break;
302 	case GRFGETNUMVM:
303 		return(gp->g_mode(gp, GM_GRFGETNUMVM, data, 0, 0));
304 	/*
305 	 * these are all hardware dependent, and have to be resolved
306 	 * in the respective driver.
307 	 */
308 	case GRFIOCPUTCMAP:
309 	case GRFIOCGETCMAP:
310 	case GRFIOCSSPRITEPOS:
311 	case GRFIOCGSPRITEPOS:
312 	case GRFIOCSSPRITEINF:
313 	case GRFIOCGSPRITEINF:
314 	case GRFIOCGSPRITEMAX:
315 	case GRFIOCBITBLT:
316     	case GRFIOCSETMON:
317 	case GRFTOGGLE: /* Toggles between Cirrus boards and native ECS on
318                      Amiga. 15/11/94 ill */
319 		/*
320 		 * We need the minor dev number to get the overlay/image
321 		 * information for grf_ul.
322 		 */
323 		return(gp->g_mode(gp, GM_GRFIOCTL, data, cmd, dev));
324 
325 	case GRFIOCBLANK:	/* blank ioctl, IOCON/OFF will turn ite on */
326 	case FBIOSVIDEO:
327 		error = gp->g_mode(gp, GM_GRFIOCTL, data, GRFIOCBLANK, dev);
328 		if (!error)
329 			gp->g_blank = *(int *)data;
330 		return (error);
331 
332 	case FBIOGVIDEO:
333 		*(int *)data = gp->g_blank;
334 		return (0);
335 
336 	default:
337 #if NVIEW > 0
338 		/*
339 		 * check to see whether it's a command recognized by the
340 		 * view code if the unit is 0
341 		 * XXX
342 		 */
343 		if (GRFUNIT(dev) == 0) {
344 			extern const struct cdevsw view_cdevsw;
345 
346 			return((*view_cdevsw.d_ioctl)(dev, cmd, data, flag, l));
347 		}
348 #endif
349 		error = EPASSTHROUGH;
350 		break;
351 
352 	}
353 	return(error);
354 }
355 
356 /*
357  * map the contents of a graphics display card into process'
358  * memory space.
359  */
360 paddr_t
361 grfmmap(dev_t dev, off_t off, int prot)
362 {
363 	struct grf_softc *gp;
364 	struct grfinfo *gi;
365 
366 	gp = grfsp[GRFUNIT(dev)];
367 	gi = &gp->g_display;
368 
369 	/*
370 	 * control registers
371 	 */
372 	if (off >= 0 && off < gi->gd_regsize)
373 		return MD_BTOP((paddr_t)gi->gd_regaddr + off);
374 
375 	/*
376 	 * frame buffer
377 	 */
378 	if (off >= gi->gd_regsize && off < gi->gd_regsize+gi->gd_fbsize) {
379 		off -= gi->gd_regsize;
380 		return MD_BTOP((paddr_t)gi->gd_fbaddr + off);
381 	}
382 	/* bogus */
383 	return(-1);
384 }
385 
386 int
387 grfon(dev_t dev)
388 {
389 	struct grf_softc *gp;
390 
391 	gp = grfsp[GRFUNIT(dev)];
392 
393 	if (gp->g_flags & GF_GRFON)
394 		return(0);
395 
396 	gp->g_flags |= GF_GRFON;
397 	if (gp->g_itedev != NODEV)
398 		ite_off(gp->g_itedev, 3);
399 
400 	return(gp->g_mode(gp, (dev & GRFOVDEV) ? GM_GRFOVON : GM_GRFON,
401 							NULL, 0, 0));
402 }
403 
404 int
405 grfoff(dev_t dev)
406 {
407 	struct grf_softc *gp;
408 	int error;
409 
410 	gp = grfsp[GRFUNIT(dev)];
411 
412 	if ((gp->g_flags & GF_GRFON) == 0)
413 		return(0);
414 
415 	gp->g_flags &= ~GF_GRFON;
416 	error = gp->g_mode(gp, (dev & GRFOVDEV) ? GM_GRFOVOFF : GM_GRFOFF,
417 							NULL, 0, 0);
418 
419 	/*
420 	 * Closely tied together no X's
421 	 */
422 	if (gp->g_itedev != NODEV)
423 		ite_on(gp->g_itedev, 2);
424 
425 	return(error);
426 }
427 
428 int
429 grfsinfo(dev_t dev, struct grfdyninfo *dyninfo)
430 {
431 	struct grf_softc *gp;
432 	int error;
433 
434 	gp = grfsp[GRFUNIT(dev)];
435 	error = gp->g_mode(gp, GM_GRFCONFIG, dyninfo, 0, 0);
436 
437 	/*
438 	 * Closely tied together no X's
439 	 */
440 	if (gp->g_itedev != NODEV)
441 		ite_reinit(gp->g_itedev);
442 	return(error);
443 }
444 
445 #if NWSDISPLAY > 0
446 void
447 grfcnprobe(struct consdev *cd)
448 {
449 	struct grf_softc *gp;
450 	int unit;
451 
452 	/*
453 	 * Find the first working grf device for being console.
454 	 * Ignore unit 0 (grfcc), which should use amidisplaycc instead.
455 	 */
456 	for (unit = 1; unit < NGRF; unit++) {
457 		gp = grfsp[unit];
458 		if (gp != NULL && (gp->g_flags & GF_ALIVE)) {
459 			cd->cn_pri = CN_INTERNAL;
460 			cd->cn_dev = NODEV;  /* initialized later by wscons */
461 			return;
462 		}
463 	}
464 
465 	/* no grf console alive */
466 	cd->cn_pri = CN_DEAD;
467 }
468 
469 void
470 grfcninit(struct consdev *cd)
471 {
472 	struct grf_softc *gp;
473 	struct rasops_info *ri;
474 	long defattr;
475 	int unit;
476 
477 	/* find console grf and set up wsdisplay for it */
478 	for (unit = 1; unit < NGRF; unit++) {
479 		gp = grfsp[unit];
480 		if (gp != NULL && (gp->g_flags & GF_ALIVE)) {
481 			gp->g_flags |= GF_CONSOLE;  /* we are console! */
482 
483 			gp->g_defaultscr->ncols = gp->g_display.gd_fbwidth /
484 			    gp->g_defaultscr->fontwidth;
485 			gp->g_defaultscr->nrows = gp->g_display.gd_fbheight /
486 			    gp->g_defaultscr->fontheight;
487 
488 			ri = grf_setup_rasops(gp, &console_vcons);
489 			console_vcons.scr_cookie = gp;
490 			defattr = 0;  /* XXX */
491 
492 			wsdisplay_preattach(gp->g_defaultscr, ri, 0, 0,
493 			    defattr);
494 #if NKBD > 0
495 			/* tell kbd device it is used as console keyboard */
496 			kbd_cnattach();
497 #endif
498 			return;
499 		}
500 	}
501 	panic("grfcninit: lost console");
502 }
503 
504 static void
505 grf_init_screen(void *cookie, struct vcons_screen *scr, int existing,
506     long *defattr)
507 {
508 	struct grf_softc *gp;
509 	struct rasops_info *ri __unused;
510 
511 	gp = cookie;
512 	ri = grf_setup_rasops(gp, scr);
513 }
514 
515 static struct rasops_info *
516 grf_setup_rasops(struct grf_softc *gp, struct vcons_screen *scr)
517 {
518 	struct rasops_info *ri;
519 	int i;
520 
521 	ri = &scr->scr_ri;
522 	scr->scr_flags |= VCONS_DONT_READ;
523 	memset(ri, 0, sizeof(struct rasops_info));
524 
525 	ri->ri_rows = gp->g_defaultscr->nrows;
526 	ri->ri_cols = gp->g_defaultscr->ncols;
527 	ri->ri_hw = scr;
528 	ri->ri_ops.cursor    = gp->g_emulops->cursor;
529 	ri->ri_ops.mapchar   = gp->g_emulops->mapchar;
530 	ri->ri_ops.copyrows  = gp->g_emulops->copyrows;
531 	ri->ri_ops.eraserows = gp->g_emulops->eraserows;
532 	ri->ri_ops.copycols  = gp->g_emulops->copycols;
533 	ri->ri_ops.erasecols = gp->g_emulops->erasecols;
534 	ri->ri_ops.putchar   = gp->g_emulops->putchar;
535 	ri->ri_ops.allocattr = gp->g_emulops->allocattr;
536 
537 	/* multiplication table for row-offsets */
538 	for (i = 0; i < ri->ri_rows; i++)
539 		gp->g_rowoffset[i] = i * ri->ri_cols;
540 
541 	return ri;
542 }
543 
544 /*
545  * Called as fallback for ioctls which are not handled by the specific
546  * grf driver.
547  */
548 int
549 grf_wsioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
550 {
551 	struct wsdisplayio_fbinfo *iofbi;
552 	struct wsdisplay_fbinfo *fbinfo;
553 	struct vcons_data *vd;
554 	struct grf_softc *gp;
555 	struct vcons_screen *scr;
556 	struct grfinfo *gi;
557 
558 	vd = v;
559 	gp = vd->cookie;
560 	scr = vd->active;
561 
562 	switch (cmd) {
563 	case WSDISPLAYIO_GET_FBINFO:
564 		if (scr != NULL) {
565 			iofbi = data;
566 			return wsdisplayio_get_fbinfo(&scr->scr_ri, iofbi);
567 		}
568 		return ENODEV;
569 
570 	case WSDISPLAYIO_GINFO:
571 		if (scr != NULL) {
572 			fbinfo = (struct wsdisplay_fbinfo *)data;
573 			gi = &gp->g_display;
574 
575 			/*
576 			 * We should return truth about the current mode here,
577 			 * because X11 wsfb driver depends on this!
578 			 */
579 			fbinfo->height = gi->gd_fbheight;
580 			fbinfo->width = gi->gd_fbwidth;
581 			fbinfo->depth = gi->gd_planes;
582 			fbinfo->cmsize = gi->gd_colors;
583 			return 0;
584 		}
585 		return ENODEV;
586 
587 	case WSDISPLAYIO_GTYPE:
588 		*(u_int *)data = WSDISPLAY_TYPE_GRF;
589 		return 0;
590 
591 	case WSDISPLAYIO_SMODE:
592 		if ((*(int *)data) != gp->g_wsmode) {
593 			gp->g_wsmode = *(int *)data;
594 			if (gp->g_wsmode == WSDISPLAYIO_MODE_EMUL &&
595 			    scr != NULL)
596 				vcons_redraw_screen(scr);
597 		}
598 		return 0;
599 	}
600 
601 	return EPASSTHROUGH;
602 }
603 
604 paddr_t
605 grf_wsmmap(void *v, void *vs, off_t off, int prot)
606 {
607 	struct vcons_data *vd;
608 	struct grf_softc *gp;
609 	struct grfinfo *gi;
610 
611 	vd = v;
612 	gp = vd->cookie;
613 	gi = &gp->g_display;
614 
615 	/* Normal fb mapping */
616 	if (off < gi->gd_fbsize)
617 		return MD_BTOP(((paddr_t)gi->gd_fbaddr) + off);
618 
619 	/*
620 	 * restrict all other mappings to processes with superuser privileges
621 	 * or the kernel itself
622 	 */
623 	if (kauth_authorize_machdep(kauth_cred_get(), KAUTH_MACHDEP_UNMANAGEDMEM,
624 	    NULL, NULL, NULL, NULL) != 0) {
625 		aprint_normal("%s: permission to mmap denied.\n",
626 		    device_xname(gp->g_device));
627 		return -1;
628 	}
629 
630 	/* Handle register mapping */
631 	if ((off >= (paddr_t)gi->gd_regaddr) &&
632 	    (off < ((paddr_t)gi->gd_regaddr + (size_t)gi->gd_regsize)))
633 		return MD_BTOP(off);
634 
635 	if ((off >= (paddr_t)gi->gd_fbaddr) &&
636 	    (off < ((paddr_t)gi->gd_fbaddr + (size_t)gi->gd_fbsize)))
637 		return MD_BTOP(off);
638 
639 	return -1;
640 }
641 
642 #endif  /* NWSDISPLAY > 0 */
643 
644 #endif	/* NGRF > 0 */
645