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