Home | History | Annotate | Line # | Download | only in dev
ofb.c revision 1.52
      1 /*	$NetBSD: ofb.c,v 1.52 2006/11/08 01:25:10 macallan Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
      5  * All rights reserved.
      6  *
      7  * Author: Chris G. Demetriou
      8  *
      9  * Permission to use, copy, modify and distribute this software and
     10  * its documentation is hereby granted, provided that both the copyright
     11  * notice and this permission notice appear in all copies of the
     12  * software, derivative works or modified versions, and any portions
     13  * thereof, and that both notices appear in supporting documentation.
     14  *
     15  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     16  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     17  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     18  *
     19  * Carnegie Mellon requests users of this software to return to
     20  *
     21  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     22  *  School of Computer Science
     23  *  Carnegie Mellon University
     24  *  Pittsburgh PA 15213-3890
     25  *
     26  * any improvements or extensions that they make and grant Carnegie the
     27  * rights to redistribute these changes.
     28  */
     29 
     30 #include <sys/cdefs.h>
     31 __KERNEL_RCSID(0, "$NetBSD: ofb.c,v 1.52 2006/11/08 01:25:10 macallan Exp $");
     32 
     33 #include <sys/param.h>
     34 #include <sys/buf.h>
     35 #include <sys/conf.h>
     36 #include <sys/device.h>
     37 #include <sys/ioctl.h>
     38 #include <sys/kernel.h>
     39 #include <sys/malloc.h>
     40 #include <sys/systm.h>
     41 #include <sys/kauth.h>
     42 
     43 #include <uvm/uvm_extern.h>
     44 
     45 #include <dev/pci/pcidevs.h>
     46 #include <dev/pci/pcireg.h>
     47 #include <dev/pci/pcivar.h>
     48 #include <dev/pci/pciio.h>
     49 
     50 #include <dev/wscons/wsconsio.h>
     51 #include <dev/wscons/wsdisplayvar.h>
     52 #include <dev/rasops/rasops.h>
     53 #include <dev/wsfont/wsfont.h>
     54 
     55 #include <dev/ofw/openfirm.h>
     56 #include <dev/ofw/ofw_pci.h>
     57 
     58 #include <machine/bus.h>
     59 #include <machine/autoconf.h>
     60 #include <machine/grfioctl.h>
     61 
     62 #include <dev/wscons/wsdisplay_vconsvar.h>
     63 
     64 #include <macppc/dev/ofbvar.h>
     65 
     66 struct ofb_softc {
     67 	struct	device sc_dev;
     68 
     69 	pci_chipset_tag_t sc_pc;
     70 	pcitag_t sc_pcitag;
     71 	bus_space_tag_t sc_memt;
     72 	bus_space_tag_t sc_iot;
     73 
     74 	u_int32_t sc_addrs[30];		/* "assigned-addresses" storage */
     75 	u_char sc_cmap_red[256];
     76 	u_char sc_cmap_green[256];
     77 	u_char sc_cmap_blue[256];
     78 
     79 	int sc_node, sc_ih, sc_mode;
     80 	paddr_t sc_fbaddr;
     81 
     82 	struct vcons_data vd;
     83 };
     84 
     85 static int	ofbmatch(struct device *, struct cfdata *, void *);
     86 static void	ofbattach(struct device *, struct device *, void *);
     87 
     88 CFATTACH_DECL(ofb, sizeof(struct ofb_softc),
     89     ofbmatch, ofbattach, NULL, NULL);
     90 
     91 const struct wsscreen_descr *_ofb_scrlist[] = {
     92 	&ofb_stdscreen,
     93 	/* XXX other formats, graphics screen? */
     94 };
     95 
     96 struct wsscreen_list ofb_screenlist = {
     97 	sizeof(_ofb_scrlist) / sizeof(struct wsscreen_descr *), _ofb_scrlist
     98 };
     99 
    100 static int	ofb_ioctl(void *, void *, u_long, caddr_t, int, struct lwp *);
    101 static paddr_t	ofb_mmap(void *, void *, off_t, int);
    102 
    103 static void	ofb_init_screen(void *, struct vcons_screen *, int, long *);
    104 
    105 struct wsdisplay_accessops ofb_accessops = {
    106 	ofb_ioctl,
    107 	ofb_mmap,
    108 	NULL,		/* vcons_alloc_screen */
    109 	NULL,		/* vcons_free_screen */
    110 	NULL,		/* vcons_show_screen */
    111 	NULL		/* load_font */
    112 };
    113 
    114 static void	ofb_putpalreg(struct ofb_softc *, int, uint8_t, uint8_t,
    115     uint8_t);
    116 
    117 static int	ofb_getcmap(struct ofb_softc *, struct wsdisplay_cmap *);
    118 static int	ofb_putcmap(struct ofb_softc *, struct wsdisplay_cmap *);
    119 static void	ofb_init_cmap(struct ofb_softc *);
    120 
    121 extern const u_char rasops_cmap[768];
    122 
    123 extern int console_node;
    124 extern int console_instance;
    125 
    126 static int
    127 ofbmatch(struct device *parent, struct cfdata *match, void *aux)
    128 {
    129 	struct pci_attach_args *pa = aux;
    130 
    131 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE &&
    132 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_CONTROL)
    133 		return 1;
    134 
    135 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY)
    136 		return 1;
    137 
    138 	return 0;
    139 }
    140 
    141 static void
    142 ofbattach(struct device *parent, struct device *self, void *aux)
    143 {
    144 	struct ofb_softc *sc = (struct ofb_softc *)self;
    145 	struct pci_attach_args *pa = aux;
    146 	struct wsemuldisplaydev_attach_args a;
    147 	struct rasops_info *ri = &ofb_console_screen.scr_ri;
    148 	long defattr;
    149 	int console, node, sub;
    150 	char devinfo[256];
    151 
    152 	node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
    153 	console = (node == console_node);
    154 	if (!console) {
    155 		/* check if any of the childs matches */
    156 		sub = OF_child(node);
    157 		while ((sub != 0) && (sub != console_node)) {
    158 			sub = OF_peer(sub);
    159 		}
    160 		if (sub == console_node) {
    161 			console = TRUE;
    162 		}
    163 	}
    164 
    165 	sc->sc_memt = pa->pa_memt;
    166 	sc->sc_iot = pa->pa_iot;
    167 	sc->sc_pc = pa->pa_pc;
    168 	sc->sc_pcitag = pa->pa_tag;
    169 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    170 
    171 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    172 	printf(": %s\n", devinfo);
    173 
    174 	if (!console)
    175 		return;
    176 
    177 	vcons_init(&sc->vd, sc, &ofb_stdscreen, &ofb_accessops);
    178 	sc->vd.init_screen = ofb_init_screen;
    179 
    180 	sc->sc_node = console_node;
    181 
    182 	sc->sc_ih = console_instance;
    183 	vcons_init_screen(&sc->vd, &ofb_console_screen, 1, &defattr);
    184 	ofb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    185 
    186 	printf("%s: %d x %d, %dbpp\n", self->dv_xname,
    187 	       ri->ri_width, ri->ri_height, ri->ri_depth);
    188 
    189 	sc->sc_fbaddr = 0;
    190 	if (OF_getprop(sc->sc_node, "address", &sc->sc_fbaddr, 4) != 4)
    191 		OF_interpret("frame-buffer-adr", 1, &sc->sc_fbaddr);
    192 	if (sc->sc_fbaddr == 0) {
    193 		printf("%s: Unable to find the framebuffer address.\n",
    194 		    sc->sc_dev.dv_xname);
    195 		return;
    196 	}
    197 
    198 	/* XXX */
    199 	if (OF_getprop(sc->sc_node, "assigned-addresses", sc->sc_addrs,
    200 	    sizeof(sc->sc_addrs)) == -1) {
    201 		sc->sc_node = OF_parent(sc->sc_node);
    202 		OF_getprop(sc->sc_node, "assigned-addresses", sc->sc_addrs,
    203 		    sizeof(sc->sc_addrs));
    204 	}
    205 
    206 	ofb_init_cmap(sc);
    207 
    208 	a.console = console;
    209 	a.scrdata = &ofb_screenlist;
    210 	a.accessops = &ofb_accessops;
    211 	a.accesscookie = &sc->vd;
    212 
    213 	config_found(self, &a, wsemuldisplaydevprint);
    214 }
    215 
    216 static void
    217 ofb_init_screen(void *cookie, struct vcons_screen *scr,
    218     int existing, long *defattr)
    219 {
    220 	struct ofb_softc *sc = cookie;
    221 	struct rasops_info *ri = &scr->scr_ri;
    222 
    223 	if (scr != &ofb_console_screen) {
    224 		ofb_init_rasops(sc->sc_node, ri);
    225 	}
    226 }
    227 
    228 static int
    229 ofb_ioctl(void *v, void *vs, u_long cmd, caddr_t data, int flag, struct lwp *l)
    230 {
    231 	struct vcons_data *vd = v;
    232 	struct ofb_softc *sc = vd->cookie;
    233 	struct wsdisplay_fbinfo *wdf;
    234 	struct vcons_screen *ms = vd->active;
    235 	struct grfinfo *gm;
    236 
    237 	switch (cmd) {
    238 	case WSDISPLAYIO_GTYPE:
    239 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;	/* XXX ? */
    240 		return 0;
    241 
    242 	case WSDISPLAYIO_GINFO:
    243 		/* we won't get here without any screen anyway */
    244 		if (ms != NULL) {
    245 			wdf = (void *)data;
    246 			wdf->height = ms->scr_ri.ri_width;
    247 			wdf->width = ms->scr_ri.ri_height;
    248 			wdf->depth = ms->scr_ri.ri_depth;
    249 			wdf->cmsize = 256;
    250 			return 0;
    251 		} else
    252 			return ENODEV;
    253 
    254 	case WSDISPLAYIO_GETCMAP:
    255 		return ofb_getcmap(sc, (struct wsdisplay_cmap *)data);
    256 
    257 	case WSDISPLAYIO_PUTCMAP:
    258 		return ofb_putcmap(sc, (struct wsdisplay_cmap *)data);
    259 
    260 	/* XXX There are no way to know framebuffer pa from a user program. */
    261 	case GRFIOCGINFO:
    262 		if (ms != NULL) {
    263 			gm = (void *)data;
    264 			memset(gm, 0, sizeof(struct grfinfo));
    265 			gm->gd_fbaddr = (caddr_t)sc->sc_fbaddr;
    266 			gm->gd_fbrowbytes = ms->scr_ri.ri_stride;
    267 			return 0;
    268 		} else
    269 			return ENODEV;
    270 	case WSDISPLAYIO_SMODE:
    271 		{
    272 			int new_mode = *(int*)data;
    273 			if (new_mode != sc->sc_mode)
    274 			{
    275 				sc->sc_mode = new_mode;
    276 				if (new_mode == WSDISPLAYIO_MODE_EMUL)
    277 				{
    278 					ofb_init_cmap(sc);
    279 					vcons_redraw_screen(ms);
    280 				}
    281 			}
    282 		}
    283 		return 0;
    284 	/* PCI config read/write passthrough. */
    285 	case PCI_IOC_CFGREAD:
    286 	case PCI_IOC_CFGWRITE:
    287 		return (pci_devioctl(sc->sc_pc, sc->sc_pcitag,
    288 		    cmd, data, flag, l));
    289 	}
    290 	return EPASSTHROUGH;
    291 }
    292 
    293 static paddr_t
    294 ofb_mmap(void *v, void *vs, off_t offset, int prot)
    295 {
    296 	struct vcons_data *vd = v;
    297 	struct ofb_softc *sc = vd->cookie;
    298 	struct rasops_info *ri;
    299 	u_int32_t *ap = sc->sc_addrs;
    300 	struct lwp *me;
    301 	int i;
    302 
    303 	if (vd->active == NULL) {
    304 		printf("%s: no active screen.\n", sc->sc_dev.dv_xname);
    305 		return -1;
    306 	}
    307 
    308 	ri = &vd->active->scr_ri;
    309 
    310 	/* framebuffer at offset 0 */
    311 	if (offset >=0 && offset < (ri->ri_stride * ri->ri_height))
    312 		return sc->sc_fbaddr + offset;
    313 
    314 	/*
    315 	 * restrict all other mappings to processes with superuser privileges
    316 	 * or the kernel itself
    317 	 */
    318 	me = curlwp;
    319 	if (me != NULL) {
    320 		if (kauth_authorize_generic(me->l_cred, KAUTH_GENERIC_ISSUSER,
    321 		    NULL) != 0) {
    322 			printf("%s: mmap() rejected.\n", sc->sc_dev.dv_xname);
    323 			return -1;
    324 		}
    325 	}
    326 
    327 	/* let them mmap() 0xa0000 - 0xbffff if it's not covered above */
    328 #ifdef OFB_FAKE_VGA_FB
    329 	if (offset >=0xa0000 && offset < 0xbffff)
    330 		return sc->sc_fbaddr + offset - 0xa0000;
    331 #endif
    332 
    333 	/* allow to map our IO space */
    334 	if ((offset >= 0xf2000000) && (offset < 0xf2800000)) {
    335 		return bus_space_mmap(sc->sc_iot, offset-0xf2000000, 0, prot,
    336 		    BUS_SPACE_MAP_LINEAR);
    337 	}
    338 
    339 	for (i = 0; i < 6; i++) {
    340 		switch (ap[0] & OFW_PCI_PHYS_HI_SPACEMASK) {
    341 		case OFW_PCI_PHYS_HI_SPACE_MEM32:
    342 			if (offset >= ap[2] && offset < ap[2] + ap[4])
    343 				return bus_space_mmap(sc->sc_memt, offset, 0,
    344 				    prot, BUS_SPACE_MAP_LINEAR);
    345 		}
    346 		ap += 5;
    347 	}
    348 
    349 	return -1;
    350 }
    351 
    352 static int
    353 ofb_getcmap(struct ofb_softc *sc, struct wsdisplay_cmap *cm)
    354 {
    355 	u_int index = cm->index;
    356 	u_int count = cm->count;
    357 	int error;
    358 
    359 	if (index >= 256 || count > 256 || index + count > 256)
    360 		return EINVAL;
    361 
    362 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    363 	if (error)
    364 		return error;
    365 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    366 	if (error)
    367 		return error;
    368 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    369 	if (error)
    370 		return error;
    371 
    372 	return 0;
    373 }
    374 
    375 int
    376 ofb_putcmap(struct ofb_softc *sc, struct wsdisplay_cmap *cm)
    377 {
    378 	u_int index = cm->index;
    379 	u_int count = cm->count;
    380 	int i, error;
    381 	u_char rbuf[256], gbuf[256], bbuf[256];
    382 	u_char *r, *g, *b;
    383 
    384 	if (cm->index >= 256 || cm->count > 256 ||
    385 	    (cm->index + cm->count) > 256)
    386 		return EINVAL;
    387 	error = copyin(cm->red, &rbuf[index], count);
    388 	if (error)
    389 		return error;
    390 	error = copyin(cm->green, &gbuf[index], count);
    391 	if (error)
    392 		return error;
    393 	error = copyin(cm->blue, &bbuf[index], count);
    394 	if (error)
    395 		return error;
    396 
    397 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    398 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    399 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    400 
    401 	r = &sc->sc_cmap_red[index];
    402 	g = &sc->sc_cmap_green[index];
    403 	b = &sc->sc_cmap_blue[index];
    404 	for (i = 0; i < count; i++) {
    405 		OF_call_method_1("color!", sc->sc_ih, 4, *r, *g, *b, index);
    406 		r++, g++, b++, index++;
    407 	}
    408 	return 0;
    409 }
    410 
    411 static void
    412 ofb_putpalreg(struct ofb_softc *sc, int idx, uint8_t r, uint8_t g, uint8_t b)
    413 {
    414 	if ((idx<0) || (idx > 255))
    415 		return;
    416 	if (sc != NULL) {
    417 		OF_call_method_1("color!", sc->sc_ih, 4, r, g, b, idx);
    418 		sc->sc_cmap_red[idx] = r;
    419 		sc->sc_cmap_green[idx] = g;
    420 		sc->sc_cmap_blue[idx] = b;
    421 	} else {
    422 		OF_call_method_1("color!", console_instance, 4, r, g, b, idx);
    423 	}
    424 }
    425 
    426 static void
    427 ofb_init_cmap(struct ofb_softc *sc)
    428 {
    429 	int idx, i;
    430 	/* mess with the palette only when we're running in 8 bit */
    431 	if (ofb_console_screen.scr_ri.ri_depth == 8) {
    432 		idx = 0;
    433 		for (i = 0; i < 256; i++) {
    434 			ofb_putpalreg(sc, i, rasops_cmap[(i * 3) + 0],
    435 			     rasops_cmap[(i * 3) + 1],
    436 			     rasops_cmap[(i * 3) + 2]);
    437 		}
    438 	}
    439 }
    440