Home | History | Annotate | Line # | Download | only in dev
ofb.c revision 1.56
      1 /*	$NetBSD: ofb.c,v 1.56 2007/03/04 06:00:10 christos 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.56 2007/03/04 06:00:10 christos 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, void *, 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 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    153 	printf(": %s\n", devinfo);
    154 
    155 	if (console_node == 0)
    156 		return;
    157 
    158 	node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
    159 	console = (node == console_node);
    160 	if (!console) {
    161 		/* check if any of the childs matches */
    162 		sub = OF_child(node);
    163 		while ((sub != 0) && (sub != console_node)) {
    164 			sub = OF_peer(sub);
    165 		}
    166 		if (sub == console_node) {
    167 			console = true;
    168 		}
    169 	}
    170 
    171 	sc->sc_memt = pa->pa_memt;
    172 	sc->sc_iot = pa->pa_iot;
    173 	sc->sc_pc = pa->pa_pc;
    174 	sc->sc_pcitag = pa->pa_tag;
    175 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    176 
    177 	if (!console)
    178 		return;
    179 
    180 	vcons_init(&sc->vd, sc, &ofb_stdscreen, &ofb_accessops);
    181 	sc->vd.init_screen = ofb_init_screen;
    182 
    183 	sc->sc_node = console_node;
    184 
    185 	sc->sc_ih = console_instance;
    186 	vcons_init_screen(&sc->vd, &ofb_console_screen, 1, &defattr);
    187 	ofb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    188 
    189 	printf("%s: %d x %d, %dbpp\n", self->dv_xname,
    190 	       ri->ri_width, ri->ri_height, ri->ri_depth);
    191 
    192 	sc->sc_fbaddr = 0;
    193 	if (OF_getprop(sc->sc_node, "address", &sc->sc_fbaddr, 4) != 4)
    194 		OF_interpret("frame-buffer-adr", 1, 1, &sc->sc_fbaddr);
    195 	if (sc->sc_fbaddr == 0) {
    196 		printf("%s: Unable to find the framebuffer address.\n",
    197 		    sc->sc_dev.dv_xname);
    198 		return;
    199 	}
    200 
    201 	/* XXX */
    202 	if (OF_getprop(sc->sc_node, "assigned-addresses", sc->sc_addrs,
    203 	    sizeof(sc->sc_addrs)) == -1) {
    204 		sc->sc_node = OF_parent(sc->sc_node);
    205 		OF_getprop(sc->sc_node, "assigned-addresses", sc->sc_addrs,
    206 		    sizeof(sc->sc_addrs));
    207 	}
    208 
    209 	ofb_init_cmap(sc);
    210 
    211 	a.console = console;
    212 	a.scrdata = &ofb_screenlist;
    213 	a.accessops = &ofb_accessops;
    214 	a.accesscookie = &sc->vd;
    215 
    216 	config_found(self, &a, wsemuldisplaydevprint);
    217 }
    218 
    219 static void
    220 ofb_init_screen(void *cookie, struct vcons_screen *scr,
    221     int existing, long *defattr)
    222 {
    223 	struct ofb_softc *sc = cookie;
    224 	struct rasops_info *ri = &scr->scr_ri;
    225 
    226 	if (scr != &ofb_console_screen) {
    227 		ofb_init_rasops(sc->sc_node, ri);
    228 	}
    229 }
    230 
    231 static int
    232 ofb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
    233 {
    234 	struct vcons_data *vd = v;
    235 	struct ofb_softc *sc = vd->cookie;
    236 	struct wsdisplay_fbinfo *wdf;
    237 	struct vcons_screen *ms = vd->active;
    238 	struct grfinfo *gm;
    239 
    240 	switch (cmd) {
    241 	case WSDISPLAYIO_GTYPE:
    242 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;	/* XXX ? */
    243 		return 0;
    244 
    245 	case WSDISPLAYIO_GINFO:
    246 		/* we won't get here without any screen anyway */
    247 		if (ms != NULL) {
    248 			wdf = (void *)data;
    249 			wdf->height = ms->scr_ri.ri_width;
    250 			wdf->width = ms->scr_ri.ri_height;
    251 			wdf->depth = ms->scr_ri.ri_depth;
    252 			wdf->cmsize = 256;
    253 			return 0;
    254 		} else
    255 			return ENODEV;
    256 
    257 	case WSDISPLAYIO_GETCMAP:
    258 		return ofb_getcmap(sc, (struct wsdisplay_cmap *)data);
    259 
    260 	case WSDISPLAYIO_PUTCMAP:
    261 		return ofb_putcmap(sc, (struct wsdisplay_cmap *)data);
    262 
    263 	/* XXX There are no way to know framebuffer pa from a user program. */
    264 	case GRFIOCGINFO:
    265 		if (ms != NULL) {
    266 			gm = (void *)data;
    267 			memset(gm, 0, sizeof(struct grfinfo));
    268 			gm->gd_fbaddr = (void *)sc->sc_fbaddr;
    269 			gm->gd_fbrowbytes = ms->scr_ri.ri_stride;
    270 			return 0;
    271 		} else
    272 			return ENODEV;
    273 	case WSDISPLAYIO_SMODE:
    274 		{
    275 			int new_mode = *(int*)data;
    276 			if (new_mode != sc->sc_mode)
    277 			{
    278 				sc->sc_mode = new_mode;
    279 				if (new_mode == WSDISPLAYIO_MODE_EMUL)
    280 				{
    281 					ofb_init_cmap(sc);
    282 					vcons_redraw_screen(ms);
    283 				}
    284 			}
    285 		}
    286 		return 0;
    287 	/* PCI config read/write passthrough. */
    288 	case PCI_IOC_CFGREAD:
    289 	case PCI_IOC_CFGWRITE:
    290 		return (pci_devioctl(sc->sc_pc, sc->sc_pcitag,
    291 		    cmd, data, flag, l));
    292 	}
    293 	return EPASSTHROUGH;
    294 }
    295 
    296 static paddr_t
    297 ofb_mmap(void *v, void *vs, off_t offset, int prot)
    298 {
    299 	struct vcons_data *vd = v;
    300 	struct ofb_softc *sc = vd->cookie;
    301 	struct rasops_info *ri;
    302 	u_int32_t *ap = sc->sc_addrs;
    303 	struct lwp *me;
    304 	int i;
    305 
    306 	if (vd->active == NULL) {
    307 		printf("%s: no active screen.\n", sc->sc_dev.dv_xname);
    308 		return -1;
    309 	}
    310 
    311 	ri = &vd->active->scr_ri;
    312 
    313 	/* framebuffer at offset 0 */
    314 	if (offset >=0 && offset < (ri->ri_stride * ri->ri_height))
    315 		return sc->sc_fbaddr + offset;
    316 
    317 	/*
    318 	 * restrict all other mappings to processes with superuser privileges
    319 	 * or the kernel itself
    320 	 */
    321 	me = curlwp;
    322 	if (me != NULL) {
    323 		if (kauth_authorize_generic(me->l_cred, KAUTH_GENERIC_ISSUSER,
    324 		    NULL) != 0) {
    325 			printf("%s: mmap() rejected.\n", sc->sc_dev.dv_xname);
    326 			return -1;
    327 		}
    328 	}
    329 
    330 	/* let them mmap() 0xa0000 - 0xbffff if it's not covered above */
    331 #ifdef OFB_FAKE_VGA_FB
    332 	if (offset >=0xa0000 && offset < 0xbffff)
    333 		return sc->sc_fbaddr + offset - 0xa0000;
    334 #endif
    335 
    336 	/* allow to map our IO space */
    337 	if ((offset >= 0xf2000000) && (offset < 0xf2800000)) {
    338 		return bus_space_mmap(sc->sc_iot, offset-0xf2000000, 0, prot,
    339 		    BUS_SPACE_MAP_LINEAR);
    340 	}
    341 
    342 	for (i = 0; i < 6; i++) {
    343 		switch (ap[0] & OFW_PCI_PHYS_HI_SPACEMASK) {
    344 		case OFW_PCI_PHYS_HI_SPACE_MEM32:
    345 			if (offset >= ap[2] && offset < ap[2] + ap[4])
    346 				return bus_space_mmap(sc->sc_memt, offset, 0,
    347 				    prot, BUS_SPACE_MAP_LINEAR);
    348 		}
    349 		ap += 5;
    350 	}
    351 
    352 	return -1;
    353 }
    354 
    355 static int
    356 ofb_getcmap(struct ofb_softc *sc, struct wsdisplay_cmap *cm)
    357 {
    358 	u_int index = cm->index;
    359 	u_int count = cm->count;
    360 	int error;
    361 
    362 	if (index >= 256 || count > 256 || index + count > 256)
    363 		return EINVAL;
    364 
    365 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    366 	if (error)
    367 		return error;
    368 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    369 	if (error)
    370 		return error;
    371 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    372 	if (error)
    373 		return error;
    374 
    375 	return 0;
    376 }
    377 
    378 int
    379 ofb_putcmap(struct ofb_softc *sc, struct wsdisplay_cmap *cm)
    380 {
    381 	u_int index = cm->index;
    382 	u_int count = cm->count;
    383 	int i, error;
    384 	u_char rbuf[256], gbuf[256], bbuf[256];
    385 	u_char *r, *g, *b;
    386 
    387 	if (cm->index >= 256 || cm->count > 256 ||
    388 	    (cm->index + cm->count) > 256)
    389 		return EINVAL;
    390 	error = copyin(cm->red, &rbuf[index], count);
    391 	if (error)
    392 		return error;
    393 	error = copyin(cm->green, &gbuf[index], count);
    394 	if (error)
    395 		return error;
    396 	error = copyin(cm->blue, &bbuf[index], count);
    397 	if (error)
    398 		return error;
    399 
    400 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    401 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    402 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    403 
    404 	r = &sc->sc_cmap_red[index];
    405 	g = &sc->sc_cmap_green[index];
    406 	b = &sc->sc_cmap_blue[index];
    407 	for (i = 0; i < count; i++) {
    408 		OF_call_method_1("color!", sc->sc_ih, 4, *r, *g, *b, index);
    409 		r++, g++, b++, index++;
    410 	}
    411 	return 0;
    412 }
    413 
    414 static void
    415 ofb_putpalreg(struct ofb_softc *sc, int idx, uint8_t r, uint8_t g, uint8_t b)
    416 {
    417 	if ((idx<0) || (idx > 255))
    418 		return;
    419 	if (sc != NULL) {
    420 		OF_call_method_1("color!", sc->sc_ih, 4, r, g, b, idx);
    421 		sc->sc_cmap_red[idx] = r;
    422 		sc->sc_cmap_green[idx] = g;
    423 		sc->sc_cmap_blue[idx] = b;
    424 	} else {
    425 		OF_call_method_1("color!", console_instance, 4, r, g, b, idx);
    426 	}
    427 }
    428 
    429 static void
    430 ofb_init_cmap(struct ofb_softc *sc)
    431 {
    432 	int idx, i;
    433 	/* mess with the palette only when we're running in 8 bit */
    434 	if (ofb_console_screen.scr_ri.ri_depth == 8) {
    435 		idx = 0;
    436 		for (i = 0; i < 256; i++) {
    437 			ofb_putpalreg(sc, i, rasops_cmap[(i * 3) + 0],
    438 			     rasops_cmap[(i * 3) + 1],
    439 			     rasops_cmap[(i * 3) + 2]);
    440 		}
    441 	}
    442 }
    443