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