Home | History | Annotate | Line # | Download | only in dev
ofb.c revision 1.43
      1  1.43  christos /*	$NetBSD: ofb.c,v 1.43 2005/12/11 12:18:03 christos Exp $	*/
      2   1.1    tsubai 
      3   1.1    tsubai /*
      4   1.1    tsubai  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
      5   1.1    tsubai  * All rights reserved.
      6   1.1    tsubai  *
      7   1.1    tsubai  * Author: Chris G. Demetriou
      8   1.1    tsubai  *
      9   1.1    tsubai  * Permission to use, copy, modify and distribute this software and
     10   1.1    tsubai  * its documentation is hereby granted, provided that both the copyright
     11   1.1    tsubai  * notice and this permission notice appear in all copies of the
     12   1.1    tsubai  * software, derivative works or modified versions, and any portions
     13   1.1    tsubai  * thereof, and that both notices appear in supporting documentation.
     14   1.1    tsubai  *
     15   1.1    tsubai  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     16   1.1    tsubai  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     17   1.1    tsubai  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     18   1.1    tsubai  *
     19   1.1    tsubai  * Carnegie Mellon requests users of this software to return to
     20   1.1    tsubai  *
     21   1.1    tsubai  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     22   1.1    tsubai  *  School of Computer Science
     23   1.1    tsubai  *  Carnegie Mellon University
     24   1.1    tsubai  *  Pittsburgh PA 15213-3890
     25   1.1    tsubai  *
     26   1.1    tsubai  * any improvements or extensions that they make and grant Carnegie the
     27   1.1    tsubai  * rights to redistribute these changes.
     28   1.1    tsubai  */
     29  1.36     lukem 
     30  1.36     lukem #include <sys/cdefs.h>
     31  1.43  christos __KERNEL_RCSID(0, "$NetBSD: ofb.c,v 1.43 2005/12/11 12:18:03 christos Exp $");
     32   1.1    tsubai 
     33   1.1    tsubai #include <sys/param.h>
     34   1.1    tsubai #include <sys/buf.h>
     35   1.1    tsubai #include <sys/conf.h>
     36   1.1    tsubai #include <sys/device.h>
     37   1.1    tsubai #include <sys/ioctl.h>
     38   1.1    tsubai #include <sys/kernel.h>
     39   1.1    tsubai #include <sys/malloc.h>
     40   1.1    tsubai #include <sys/systm.h>
     41   1.1    tsubai 
     42  1.15       mrg #include <uvm/uvm_extern.h>
     43  1.11    tsubai 
     44   1.3    tsubai #include <dev/pci/pcidevs.h>
     45   1.1    tsubai #include <dev/pci/pcireg.h>
     46   1.1    tsubai #include <dev/pci/pcivar.h>
     47  1.27   nathanw #include <dev/pci/pciio.h>
     48   1.1    tsubai 
     49   1.1    tsubai #include <dev/wscons/wsconsio.h>
     50   1.1    tsubai #include <dev/wscons/wsdisplayvar.h>
     51  1.11    tsubai #include <dev/rasops/rasops.h>
     52   1.1    tsubai 
     53  1.20      matt #include <dev/ofw/openfirm.h>
     54  1.13    tsubai #include <dev/ofw/ofw_pci.h>
     55  1.13    tsubai 
     56   1.1    tsubai #include <machine/bus.h>
     57  1.20      matt #include <machine/autoconf.h>
     58   1.1    tsubai #include <machine/grfioctl.h>
     59  1.37      matt 
     60  1.37      matt #include <powerpc/oea/bat.h>
     61   1.1    tsubai 
     62   1.1    tsubai #include <macppc/dev/ofbvar.h>
     63   1.1    tsubai 
     64  1.18    tsubai #if OFB_ENABLE_CACHE
     65  1.18    tsubai int ofb_enable_cache = 1;
     66  1.18    tsubai #else
     67  1.18    tsubai int ofb_enable_cache = 0;
     68  1.18    tsubai #endif
     69  1.18    tsubai 
     70   1.1    tsubai int	ofbmatch __P((struct device *, struct cfdata *, void *));
     71   1.1    tsubai void	ofbattach __P((struct device *, struct device *, void *));
     72   1.1    tsubai int	ofbprint __P((void *, const char *));
     73   1.1    tsubai 
     74  1.32   thorpej CFATTACH_DECL(ofb, sizeof(struct ofb_softc),
     75  1.32   thorpej     ofbmatch, ofbattach, NULL, NULL);
     76   1.1    tsubai 
     77  1.22    tsubai struct ofb_devconfig ofb_console_dc;
     78   1.1    tsubai 
     79   1.1    tsubai struct wsscreen_descr ofb_stdscreen = {
     80   1.1    tsubai 	"std",
     81   1.1    tsubai 	0, 0,	/* will be filled in -- XXX shouldn't, it's global */
     82  1.11    tsubai 	0,
     83   1.1    tsubai 	0, 0,
     84   1.1    tsubai 	WSSCREEN_REVERSE
     85   1.1    tsubai };
     86   1.1    tsubai 
     87   1.1    tsubai const struct wsscreen_descr *_ofb_scrlist[] = {
     88   1.1    tsubai 	&ofb_stdscreen,
     89   1.1    tsubai 	/* XXX other formats, graphics screen? */
     90   1.1    tsubai };
     91   1.1    tsubai 
     92   1.1    tsubai struct wsscreen_list ofb_screenlist = {
     93   1.1    tsubai 	sizeof(_ofb_scrlist) / sizeof(struct wsscreen_descr *), _ofb_scrlist
     94   1.1    tsubai };
     95   1.1    tsubai 
     96  1.43  christos static int ofb_ioctl __P((void *, u_long, caddr_t, int, struct lwp *));
     97  1.14    simonb static paddr_t ofb_mmap __P((void *, off_t, int));
     98   1.1    tsubai static int ofb_alloc_screen __P((void *, const struct wsscreen_descr *,
     99   1.1    tsubai 				void **, int *, int *, long *));
    100   1.1    tsubai static void ofb_free_screen __P((void *, void *));
    101  1.10  drochner static int ofb_show_screen __P((void *, void *, int,
    102  1.10  drochner 				void (*) (void *, int, int), void *));
    103  1.20      matt static int copy_rom_font __P((void));
    104   1.1    tsubai 
    105   1.1    tsubai struct wsdisplay_accessops ofb_accessops = {
    106   1.1    tsubai 	ofb_ioctl,
    107   1.1    tsubai 	ofb_mmap,
    108   1.1    tsubai 	ofb_alloc_screen,
    109   1.1    tsubai 	ofb_free_screen,
    110   1.1    tsubai 	ofb_show_screen,
    111   1.6  drochner 	0 /* load_font */
    112   1.1    tsubai };
    113   1.1    tsubai 
    114  1.11    tsubai static struct wsdisplay_font openfirm6x11;
    115  1.11    tsubai 
    116  1.22    tsubai static void ofb_common_init __P((int, struct ofb_devconfig *));
    117  1.22    tsubai static int ofb_getcmap __P((struct ofb_softc *, struct wsdisplay_cmap *));
    118  1.22    tsubai static int ofb_putcmap __P((struct ofb_softc *, struct wsdisplay_cmap *));
    119   1.1    tsubai 
    120   1.1    tsubai int
    121   1.1    tsubai ofbmatch(parent, match, aux)
    122   1.1    tsubai 	struct device *parent;
    123   1.1    tsubai 	struct cfdata *match;
    124   1.1    tsubai 	void *aux;
    125   1.1    tsubai {
    126   1.1    tsubai 	struct pci_attach_args *pa = aux;
    127   1.1    tsubai 
    128   1.3    tsubai 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE &&
    129  1.11    tsubai 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_CONTROL)
    130   1.3    tsubai 		return 1;
    131   1.1    tsubai 
    132   1.3    tsubai 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY)
    133   1.3    tsubai 		return 1;
    134   1.3    tsubai 
    135   1.3    tsubai 	return 0;
    136   1.1    tsubai }
    137   1.1    tsubai 
    138   1.1    tsubai void
    139   1.1    tsubai ofbattach(parent, self, aux)
    140   1.1    tsubai 	struct device *parent, *self;
    141   1.1    tsubai 	void *aux;
    142   1.1    tsubai {
    143  1.22    tsubai 	struct ofb_softc *sc = (struct ofb_softc *)self;
    144   1.1    tsubai 	struct pci_attach_args *pa = aux;
    145   1.1    tsubai 	struct wsemuldisplaydev_attach_args a;
    146  1.16    tsubai 	int console, node;
    147  1.22    tsubai 	struct ofb_devconfig *dc;
    148   1.5    tsubai 	char devinfo[256];
    149   1.1    tsubai 
    150  1.42  macallan 	sc->sc_memt = pa->pa_memt;
    151  1.42  macallan 	sc->sc_iot = pa->pa_iot;
    152  1.42  macallan 
    153   1.1    tsubai 	console = ofb_is_console();
    154   1.1    tsubai 
    155   1.1    tsubai 	if (console) {
    156   1.1    tsubai 		dc = &ofb_console_dc;
    157  1.16    tsubai 		node = dc->dc_node;
    158   1.1    tsubai 		sc->nscreens = 1;
    159   1.1    tsubai 	} else {
    160  1.40    briggs 		int len;
    161   1.1    tsubai 
    162  1.22    tsubai 		dc = malloc(sizeof(struct ofb_devconfig), M_DEVBUF, M_WAITOK);
    163  1.24       wiz 		memset(dc, 0, sizeof(struct ofb_devconfig));
    164   1.5    tsubai 		node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
    165   1.3    tsubai 		if (node == 0) {
    166   1.3    tsubai 			printf(": ofdev not found\n");
    167   1.3    tsubai 			return;
    168   1.3    tsubai 		}
    169  1.16    tsubai 
    170  1.16    tsubai 		/* XXX There are two child screens on PowerBook. */
    171  1.24       wiz 		memset(devinfo, 0, sizeof(devinfo));
    172  1.16    tsubai 		OF_getprop(node, "device_type", devinfo, sizeof(devinfo));
    173  1.16    tsubai 		len = strlen(devinfo);
    174  1.16    tsubai 		if (strcmp(devinfo + len - 7, "-parent") == 0)
    175  1.16    tsubai 			node = OF_child(node);
    176  1.16    tsubai 
    177   1.1    tsubai 		ofb_common_init(node, dc);
    178   1.1    tsubai 	}
    179   1.1    tsubai 	sc->sc_dc = dc;
    180   1.1    tsubai 
    181  1.27   nathanw 	sc->sc_pc = pa->pa_pc;
    182  1.27   nathanw 	sc->sc_pcitag = pa->pa_tag;
    183  1.27   nathanw 
    184  1.16    tsubai 	/* XXX */
    185  1.16    tsubai 	if (OF_getprop(node, "assigned-addresses", sc->sc_addrs,
    186  1.16    tsubai 	    sizeof(sc->sc_addrs)) == -1) {
    187  1.16    tsubai 		node = OF_parent(node);
    188  1.16    tsubai 		OF_getprop(node, "assigned-addresses", sc->sc_addrs,
    189  1.16    tsubai 		    sizeof(sc->sc_addrs));
    190  1.16    tsubai 	}
    191  1.13    tsubai 
    192   1.1    tsubai 	if (dc->dc_paddr == 0) {
    193   1.1    tsubai 		printf(": cannot map framebuffer\n");
    194   1.1    tsubai 		return;
    195   1.1    tsubai 	}
    196   1.1    tsubai 
    197  1.40    briggs 	/*
    198  1.40    briggs 	 * Clear the screen here, instead of above, in case
    199  1.40    briggs 	 * ofb_common_init() failed to map the framebuffer.
    200  1.40    briggs 	 */
    201  1.40    briggs 	if (!console) {
    202  1.40    briggs 		int i, screenbytes;
    203  1.40    briggs 
    204  1.40    briggs 		screenbytes = dc->dc_ri.ri_stride * dc->dc_ri.ri_height;
    205  1.40    briggs 		for (i = 0; i < screenbytes; i += sizeof(u_int32_t))
    206  1.40    briggs 			*(u_int32_t *)(dc->dc_paddr + i) = 0xffffffff;
    207  1.40    briggs 	}
    208  1.40    briggs 
    209  1.39    kleink 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    210   1.5    tsubai 	printf(": %s\n", devinfo);
    211   1.5    tsubai 	printf("%s: %d x %d, %dbpp\n", self->dv_xname,
    212  1.11    tsubai 	       dc->dc_ri.ri_width, dc->dc_ri.ri_height, dc->dc_ri.ri_depth);
    213  1.11    tsubai 
    214  1.11    tsubai 	sc->sc_cmap_red[0] = sc->sc_cmap_green[0] = sc->sc_cmap_blue[0] = 0;
    215  1.11    tsubai 	sc->sc_cmap_red[15] = sc->sc_cmap_red[255] = 0xff;
    216  1.11    tsubai 	sc->sc_cmap_green[15] = sc->sc_cmap_green[255] = 0xff;
    217  1.11    tsubai 	sc->sc_cmap_blue[15] = sc->sc_cmap_blue[255] = 0xff;
    218   1.1    tsubai 
    219   1.1    tsubai 	a.console = console;
    220   1.1    tsubai 	a.scrdata = &ofb_screenlist;
    221   1.1    tsubai 	a.accessops = &ofb_accessops;
    222   1.1    tsubai 	a.accesscookie = sc;
    223   1.1    tsubai 
    224   1.1    tsubai 	config_found(self, &a, wsemuldisplaydevprint);
    225   1.1    tsubai }
    226   1.1    tsubai 
    227   1.1    tsubai void
    228   1.1    tsubai ofb_common_init(node, dc)
    229   1.1    tsubai 	int node;
    230  1.22    tsubai 	struct ofb_devconfig *dc;
    231   1.1    tsubai {
    232  1.11    tsubai 	struct rasops_info *ri = &dc->dc_ri;
    233  1.11    tsubai 	int32_t addr, width, height, linebytes, depth;
    234   1.1    tsubai 
    235  1.13    tsubai 	dc->dc_node = node;
    236   1.1    tsubai 	if (dc->dc_ih == 0) {
    237   1.1    tsubai 		char name[64];
    238   1.1    tsubai 
    239  1.24       wiz 		memset(name, 0, 64);
    240   1.1    tsubai 		OF_package_to_path(node, name, sizeof(name));
    241   1.1    tsubai 		dc->dc_ih = OF_open(name);
    242   1.1    tsubai 	}
    243   1.3    tsubai 
    244   1.8    tsubai 	/* XXX /chaos/control doesn't have "width", "height", ... */
    245   1.3    tsubai 	width = height = -1;
    246  1.11    tsubai 	if (OF_getprop(node, "width", &width, 4) != 4)
    247   1.3    tsubai 		OF_interpret("screen-width", 1, &width);
    248  1.11    tsubai 	if (OF_getprop(node, "height", &height, 4) != 4)
    249   1.3    tsubai 		OF_interpret("screen-height", 1, &height);
    250  1.11    tsubai 	if (OF_getprop(node, "linebytes", &linebytes, 4) != 4)
    251   1.3    tsubai 		linebytes = width;			/* XXX */
    252  1.11    tsubai 	if (OF_getprop(node, "depth", &depth, 4) != 4)
    253   1.3    tsubai 		depth = 8;				/* XXX */
    254  1.11    tsubai 	if (OF_getprop(node, "address", &addr, 4) != 4)
    255  1.11    tsubai 		OF_interpret("frame-buffer-adr", 1, &addr);
    256  1.11    tsubai 
    257  1.11    tsubai 	if (width == -1 || height == -1 || addr == 0 || addr == -1)
    258   1.3    tsubai 		return;
    259   1.1    tsubai 
    260  1.11    tsubai 	dc->dc_paddr = addr;		/* PA of the frame buffer */
    261  1.11    tsubai 
    262  1.12    tsubai 	/* Make sure 0/0/0 is black and 255/255/255 is white. */
    263  1.12    tsubai 	OF_call_method_1("color!", dc->dc_ih, 4, 0, 0, 0, 0);
    264  1.11    tsubai 	OF_call_method_1("color!", dc->dc_ih, 4, 255, 255, 255, 255);
    265  1.18    tsubai 
    266  1.18    tsubai 	/* Enable write-through cache. */
    267  1.29      matt 	if (ofb_enable_cache) {
    268  1.29      matt 		vaddr_t va;
    269  1.29      matt 		/*
    270  1.29      matt 		 * Let's try to find an empty BAT to use
    271  1.29      matt 		 */
    272  1.29      matt 		for (va = SEGMENT_LENGTH; va < (USER_SR << ADDR_SR_SHFT);
    273  1.29      matt 		     va += SEGMENT_LENGTH) {
    274  1.29      matt 			if (battable[va >> ADDR_SR_SHFT].batu == 0) {
    275  1.29      matt 				battable[va >> ADDR_SR_SHFT].batl =
    276  1.33    briggs 				    BATL(addr & 0xf0000000,
    277  1.33    briggs 					 BAT_G | BAT_W | BAT_M, BAT_PP_RW);
    278  1.29      matt 				battable[va >> ADDR_SR_SHFT].batu =
    279  1.29      matt 				    BATL(va, BAT_BL_256M, BAT_Vs);
    280  1.29      matt 				addr &= 0x0fffffff;
    281  1.29      matt 				addr |= va;
    282  1.29      matt 				break;
    283  1.29      matt 			}
    284  1.29      matt 		}
    285  1.18    tsubai 	}
    286   1.1    tsubai 
    287  1.11    tsubai 	/* initialize rasops */
    288  1.11    tsubai 	ri->ri_width = width;
    289  1.11    tsubai 	ri->ri_height = height;
    290  1.11    tsubai 	ri->ri_depth = depth;
    291  1.11    tsubai 	ri->ri_stride = linebytes;
    292  1.11    tsubai 	ri->ri_bits = (char *)addr;
    293  1.11    tsubai 	ri->ri_flg = RI_FORCEMONO | RI_FULLCLEAR | RI_CENTER;
    294   1.1    tsubai 
    295   1.8    tsubai 	/* If screen is smaller than 1024x768, use small font. */
    296   1.8    tsubai 	if ((width < 1024 || height < 768) && copy_rom_font() == 0) {
    297  1.11    tsubai 		int cols, rows;
    298   1.8    tsubai 
    299  1.11    tsubai 		OF_interpret("#lines", 1, &rows);
    300  1.11    tsubai 		OF_interpret("#columns", 1, &cols);
    301   1.8    tsubai 
    302  1.11    tsubai 		ri->ri_font = &openfirm6x11;
    303  1.11    tsubai 		ri->ri_wsfcookie = -1;		/* not using wsfont */
    304  1.11    tsubai 		rasops_init(ri, rows, cols);
    305  1.11    tsubai 
    306  1.42  macallan 		ri->ri_xorigin = (width - cols * ri->ri_font->fontwidth) >> 1;
    307  1.42  macallan 		ri->ri_yorigin = (height - rows * ri->ri_font->fontheight)
    308  1.42  macallan 		    >> 1;
    309  1.11    tsubai 		ri->ri_bits = (char *)addr + ri->ri_xorigin +
    310  1.11    tsubai 			      ri->ri_stride * ri->ri_yorigin;
    311  1.17    tsubai 	} else {
    312  1.17    tsubai 		rasops_init(ri, 24, 80);
    313  1.17    tsubai 		rasops_reconfig(ri, (height - 2) / ri->ri_font->fontheight,
    314  1.17    tsubai 		    ((width - 2) / ri->ri_font->fontwidth) & ~7);
    315  1.17    tsubai 	}
    316  1.11    tsubai 
    317  1.11    tsubai 	/* black on white */
    318  1.11    tsubai 	ri->ri_devcmap[0] = 0xffffffff;			/* bg */
    319  1.11    tsubai 	ri->ri_devcmap[1] = 0;				/* fg */
    320  1.11    tsubai 
    321  1.11    tsubai 	ofb_stdscreen.nrows = ri->ri_rows;
    322  1.11    tsubai 	ofb_stdscreen.ncols = ri->ri_cols;
    323  1.11    tsubai 	ofb_stdscreen.textops = &ri->ri_ops;
    324  1.11    tsubai 	ofb_stdscreen.capabilities = ri->ri_caps;
    325   1.1    tsubai }
    326   1.1    tsubai 
    327   1.1    tsubai int
    328   1.1    tsubai ofb_is_console()
    329   1.1    tsubai {
    330   1.1    tsubai 	int chosen, stdout, node;
    331   1.1    tsubai 	char type[16];
    332   1.1    tsubai 
    333   1.1    tsubai 	chosen = OF_finddevice("/chosen");
    334   1.1    tsubai 	OF_getprop(chosen, "stdout", &stdout, 4);
    335   1.1    tsubai 	node = OF_instance_to_package(stdout);
    336   1.1    tsubai 	OF_getprop(node, "device_type", type, sizeof(type));
    337   1.1    tsubai 	if (strcmp(type, "display") == 0)
    338   1.1    tsubai 		return 1;
    339   1.1    tsubai 	else
    340   1.1    tsubai 		return 0;
    341   1.1    tsubai }
    342   1.1    tsubai 
    343   1.1    tsubai int
    344  1.43  christos ofb_ioctl(v, cmd, data, flag, l)
    345   1.1    tsubai 	void *v;
    346   1.1    tsubai 	u_long cmd;
    347   1.1    tsubai 	caddr_t data;
    348   1.1    tsubai 	int flag;
    349  1.43  christos 	struct lwp *l;
    350   1.1    tsubai {
    351  1.22    tsubai 	struct ofb_softc *sc = v;
    352  1.22    tsubai 	struct ofb_devconfig *dc = sc->sc_dc;
    353   1.1    tsubai 	struct wsdisplay_fbinfo *wdf;
    354   1.1    tsubai 	struct grfinfo *gm;
    355   1.1    tsubai 
    356   1.1    tsubai 	switch (cmd) {
    357   1.1    tsubai 	case WSDISPLAYIO_GTYPE:
    358   1.1    tsubai 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;	/* XXX ? */
    359   1.1    tsubai 		return 0;
    360   1.1    tsubai 
    361   1.1    tsubai 	case WSDISPLAYIO_GINFO:
    362   1.1    tsubai 		wdf = (void *)data;
    363  1.11    tsubai 		wdf->height = dc->dc_ri.ri_height;
    364  1.11    tsubai 		wdf->width = dc->dc_ri.ri_width;
    365  1.11    tsubai 		wdf->depth = dc->dc_ri.ri_depth;
    366   1.1    tsubai 		wdf->cmsize = 256;
    367   1.1    tsubai 		return 0;
    368   1.1    tsubai 
    369  1.11    tsubai 	case WSDISPLAYIO_GETCMAP:
    370  1.11    tsubai 		return ofb_getcmap(sc, (struct wsdisplay_cmap *)data);
    371  1.11    tsubai 
    372   1.1    tsubai 	case WSDISPLAYIO_PUTCMAP:
    373  1.11    tsubai 		return ofb_putcmap(sc, (struct wsdisplay_cmap *)data);
    374   1.1    tsubai 
    375   1.1    tsubai 	/* XXX There are no way to know framebuffer pa from a user program. */
    376   1.1    tsubai 	case GRFIOCGINFO:
    377   1.1    tsubai 		gm = (void *)data;
    378  1.24       wiz 		memset(gm, 0, sizeof(struct grfinfo));
    379  1.11    tsubai 		gm->gd_fbaddr = (caddr_t)dc->dc_paddr;
    380  1.11    tsubai 		gm->gd_fbrowbytes = dc->dc_ri.ri_stride;
    381   1.1    tsubai 		return 0;
    382  1.27   nathanw 	/* PCI config read/write passthrough. */
    383  1.27   nathanw 	case PCI_IOC_CFGREAD:
    384  1.27   nathanw 	case PCI_IOC_CFGWRITE:
    385  1.27   nathanw 		return (pci_devioctl(sc->sc_pc, sc->sc_pcitag,
    386  1.43  christos 		    cmd, data, flag, l));
    387   1.1    tsubai 	}
    388  1.26    atatat 	return EPASSTHROUGH;
    389   1.1    tsubai }
    390   1.1    tsubai 
    391  1.14    simonb paddr_t
    392   1.1    tsubai ofb_mmap(v, offset, prot)
    393   1.1    tsubai 	void *v;
    394   1.1    tsubai 	off_t offset;
    395   1.1    tsubai 	int prot;
    396   1.1    tsubai {
    397  1.22    tsubai 	struct ofb_softc *sc = v;
    398  1.22    tsubai 	struct ofb_devconfig *dc = sc->sc_dc;
    399  1.11    tsubai 	struct rasops_info *ri = &dc->dc_ri;
    400  1.13    tsubai 	u_int32_t *ap = sc->sc_addrs;
    401  1.42  macallan 	struct proc *me;
    402  1.13    tsubai 	int i;
    403  1.13    tsubai 
    404  1.42  macallan 	/* framebuffer at offset 0 */
    405  1.13    tsubai 	if (offset >=0 && offset < (ri->ri_stride * ri->ri_height))
    406  1.13    tsubai 		return dc->dc_paddr + offset;
    407  1.13    tsubai 
    408  1.42  macallan 	/*
    409  1.42  macallan 	 * restrict all other mappings to processes with superuser privileges
    410  1.42  macallan 	 * or the kernel itself
    411  1.42  macallan 	 */
    412  1.42  macallan 	me = __curproc();
    413  1.42  macallan 	if (me != NULL) {
    414  1.42  macallan 		if (suser(me->p_ucred, NULL) != 0) {
    415  1.42  macallan 			return -1;
    416  1.42  macallan 		}
    417  1.42  macallan 	}
    418  1.42  macallan 
    419  1.42  macallan 	/* let them mmap() 0xa0000 - 0xbffff if it's not covered above */
    420  1.42  macallan #ifdef OFB_FAKE_VGA_FB
    421  1.42  macallan 	if (offset >=0xa0000 && offset < 0xbffff)
    422  1.42  macallan 		return dc->dc_paddr + offset - 0xa0000;
    423  1.42  macallan #endif
    424  1.42  macallan 
    425  1.42  macallan 	/* allow to map our IO space */
    426  1.42  macallan 	if ((offset >= 0xf2000000) && (offset < 0xf2800000)) {
    427  1.42  macallan 		return bus_space_mmap(sc->sc_iot, offset-0xf2000000, 0, prot,
    428  1.42  macallan 		    BUS_SPACE_MAP_LINEAR);
    429  1.42  macallan 	}
    430  1.42  macallan 
    431  1.13    tsubai 	for (i = 0; i < 6; i++) {
    432  1.13    tsubai 		switch (ap[0] & OFW_PCI_PHYS_HI_SPACEMASK) {
    433  1.13    tsubai 		case OFW_PCI_PHYS_HI_SPACE_MEM32:
    434  1.42  macallan 			if (offset >= ap[2] && offset < ap[2] + ap[4])
    435  1.42  macallan 				return bus_space_mmap(sc->sc_memt, offset, 0,
    436  1.42  macallan 				    prot, BUS_SPACE_MAP_LINEAR);
    437  1.13    tsubai 		}
    438  1.13    tsubai 		ap += 5;
    439  1.13    tsubai 	}
    440   1.1    tsubai 
    441  1.13    tsubai 	return -1;
    442   1.1    tsubai }
    443   1.1    tsubai 
    444   1.1    tsubai int
    445   1.1    tsubai ofb_alloc_screen(v, type, cookiep, curxp, curyp, attrp)
    446   1.1    tsubai 	void *v;
    447   1.1    tsubai 	const struct wsscreen_descr *type;
    448   1.1    tsubai 	void **cookiep;
    449   1.1    tsubai 	int *curxp, *curyp;
    450   1.1    tsubai 	long *attrp;
    451   1.1    tsubai {
    452  1.22    tsubai 	struct ofb_softc *sc = v;
    453  1.11    tsubai 	struct rasops_info *ri = &sc->sc_dc->dc_ri;
    454   1.1    tsubai 	long defattr;
    455   1.1    tsubai 
    456   1.1    tsubai 	if (sc->nscreens > 0)
    457   1.1    tsubai 		return (ENOMEM);
    458   1.1    tsubai 
    459  1.11    tsubai 	*cookiep = ri;			/* one and only for now */
    460   1.1    tsubai 	*curxp = 0;
    461   1.1    tsubai 	*curyp = 0;
    462  1.28  junyoung 	(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
    463   1.1    tsubai 	*attrp = defattr;
    464   1.1    tsubai 	sc->nscreens++;
    465   1.1    tsubai 	return 0;
    466   1.1    tsubai }
    467   1.1    tsubai 
    468   1.1    tsubai void
    469   1.1    tsubai ofb_free_screen(v, cookie)
    470   1.1    tsubai 	void *v;
    471   1.1    tsubai 	void *cookie;
    472   1.1    tsubai {
    473  1.22    tsubai 	struct ofb_softc *sc = v;
    474   1.1    tsubai 
    475   1.1    tsubai 	if (sc->sc_dc == &ofb_console_dc)
    476   1.1    tsubai 		panic("ofb_free_screen: console");
    477   1.1    tsubai 
    478   1.1    tsubai 	sc->nscreens--;
    479   1.1    tsubai }
    480   1.1    tsubai 
    481  1.10  drochner int
    482  1.10  drochner ofb_show_screen(v, cookie, waitok, cb, cbarg)
    483   1.1    tsubai 	void *v;
    484   1.1    tsubai 	void *cookie;
    485  1.10  drochner 	int waitok;
    486  1.10  drochner 	void (*cb) __P((void *, int, int));
    487  1.10  drochner 	void *cbarg;
    488   1.1    tsubai {
    489  1.10  drochner 
    490  1.10  drochner 	return (0);
    491   1.1    tsubai }
    492   1.1    tsubai 
    493   1.1    tsubai int
    494   1.1    tsubai ofb_cnattach()
    495   1.1    tsubai {
    496  1.22    tsubai 	struct ofb_devconfig *dc = &ofb_console_dc;
    497  1.11    tsubai 	struct rasops_info *ri = &dc->dc_ri;
    498   1.1    tsubai 	long defattr;
    499  1.11    tsubai 	int crow = 0;
    500   1.1    tsubai 	int chosen, stdout, node;
    501   1.1    tsubai 
    502   1.1    tsubai 	chosen = OF_finddevice("/chosen");
    503   1.1    tsubai 	OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
    504   1.1    tsubai 	node = OF_instance_to_package(stdout);
    505   1.1    tsubai 	dc->dc_ih = stdout;
    506   1.1    tsubai 
    507  1.11    tsubai 	/* get current cursor position */
    508  1.11    tsubai 	OF_interpret("line#", 1, &crow);
    509   1.8    tsubai 
    510  1.11    tsubai 	/* move (rom monitor) cursor to the lowest line - 1 */
    511  1.11    tsubai 	OF_interpret("#lines 2 - to line#", 0);
    512   1.8    tsubai 
    513  1.11    tsubai 	ofb_common_init(node, dc);
    514   1.1    tsubai 
    515  1.11    tsubai 	if (ri->ri_width >= 1024 && ri->ri_height >= 768) {
    516  1.11    tsubai 		int i, screenbytes = ri->ri_stride * ri->ri_height;
    517   1.8    tsubai 
    518   1.8    tsubai 		for (i = 0; i < screenbytes; i += sizeof(u_int32_t))
    519  1.11    tsubai 			*(u_int32_t *)(dc->dc_paddr + i) = 0xffffffff;
    520   1.8    tsubai 		crow = 0;
    521   1.8    tsubai 	}
    522   1.8    tsubai 
    523  1.28  junyoung 	(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
    524  1.11    tsubai 	wsdisplay_cnattach(&ofb_stdscreen, ri, 0, crow, defattr);
    525   1.8    tsubai 
    526   1.8    tsubai 	return 0;
    527   1.8    tsubai }
    528   1.8    tsubai 
    529   1.8    tsubai int
    530   1.8    tsubai copy_rom_font()
    531   1.8    tsubai {
    532   1.8    tsubai 	u_char *romfont;
    533   1.8    tsubai 	int char_width, char_height;
    534   1.8    tsubai 	int chosen, mmu, m, e;
    535   1.8    tsubai 
    536   1.8    tsubai 	/* Get ROM FONT address. */
    537   1.8    tsubai 	OF_interpret("font-adr", 1, &romfont);
    538   1.8    tsubai 	if (romfont == NULL)
    539   1.8    tsubai 		return -1;
    540   1.8    tsubai 
    541   1.8    tsubai 	chosen = OF_finddevice("/chosen");
    542   1.8    tsubai 	OF_getprop(chosen, "mmu", &mmu, 4);
    543   1.1    tsubai 
    544   1.8    tsubai 	/*
    545   1.8    tsubai 	 * Convert to physcal address.  We cannot access to Open Firmware's
    546   1.8    tsubai 	 * virtual address space.
    547   1.8    tsubai 	 */
    548   1.8    tsubai 	OF_call_method("translate", mmu, 1, 3, romfont, &romfont, &m, &e);
    549   1.8    tsubai 
    550   1.8    tsubai 	/* Get character size */
    551   1.8    tsubai 	OF_interpret("char-width", 1, &char_width);
    552   1.8    tsubai 	OF_interpret("char-height", 1, &char_height);
    553   1.8    tsubai 
    554  1.11    tsubai 	openfirm6x11.name = "Open Firmware";
    555  1.11    tsubai 	openfirm6x11.firstchar = 32;
    556  1.11    tsubai 	openfirm6x11.numchars = 96;
    557  1.11    tsubai 	openfirm6x11.encoding = WSDISPLAY_FONTENC_ISO;
    558  1.11    tsubai 	openfirm6x11.fontwidth = char_width;
    559  1.11    tsubai 	openfirm6x11.fontheight = char_height;
    560  1.11    tsubai 	openfirm6x11.stride = 1;
    561  1.11    tsubai 	openfirm6x11.bitorder = WSDISPLAY_FONTORDER_L2R;
    562  1.11    tsubai 	openfirm6x11.byteorder = WSDISPLAY_FONTORDER_L2R;
    563  1.11    tsubai 	openfirm6x11.data = romfont;
    564  1.11    tsubai 
    565  1.11    tsubai 	return 0;
    566  1.11    tsubai }
    567  1.11    tsubai 
    568  1.11    tsubai int
    569  1.11    tsubai ofb_getcmap(sc, cm)
    570  1.22    tsubai 	struct ofb_softc *sc;
    571  1.11    tsubai 	struct wsdisplay_cmap *cm;
    572  1.11    tsubai {
    573  1.11    tsubai 	u_int index = cm->index;
    574  1.11    tsubai 	u_int count = cm->count;
    575  1.11    tsubai 	int error;
    576  1.11    tsubai 
    577  1.11    tsubai 	if (index >= 256 || count > 256 || index + count > 256)
    578  1.11    tsubai 		return EINVAL;
    579  1.11    tsubai 
    580  1.11    tsubai 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    581  1.11    tsubai 	if (error)
    582  1.11    tsubai 		return error;
    583  1.11    tsubai 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    584  1.11    tsubai 	if (error)
    585  1.11    tsubai 		return error;
    586  1.11    tsubai 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    587  1.11    tsubai 	if (error)
    588  1.11    tsubai 		return error;
    589   1.8    tsubai 
    590   1.1    tsubai 	return 0;
    591   1.1    tsubai }
    592   1.1    tsubai 
    593   1.1    tsubai int
    594  1.11    tsubai ofb_putcmap(sc, cm)
    595  1.22    tsubai 	struct ofb_softc *sc;
    596   1.1    tsubai 	struct wsdisplay_cmap *cm;
    597   1.1    tsubai {
    598  1.22    tsubai 	struct ofb_devconfig *dc = sc->sc_dc;
    599  1.25  jdolecek 	u_int index = cm->index;
    600  1.25  jdolecek 	u_int count = cm->count;
    601  1.38       chs 	int i, error;
    602  1.38       chs 	u_char rbuf[256], gbuf[256], bbuf[256];
    603   1.1    tsubai 	u_char *r, *g, *b;
    604   1.1    tsubai 
    605   1.1    tsubai 	if (cm->index >= 256 || cm->count > 256 ||
    606   1.1    tsubai 	    (cm->index + cm->count) > 256)
    607   1.1    tsubai 		return EINVAL;
    608  1.38       chs 	error = copyin(cm->red, &rbuf[index], count);
    609  1.38       chs 	if (error)
    610  1.38       chs 		return error;
    611  1.38       chs 	error = copyin(cm->green, &gbuf[index], count);
    612  1.38       chs 	if (error)
    613  1.38       chs 		return error;
    614  1.38       chs 	error = copyin(cm->blue, &bbuf[index], count);
    615  1.38       chs 	if (error)
    616  1.38       chs 		return error;
    617  1.38       chs 
    618  1.38       chs 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    619  1.38       chs 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    620  1.38       chs 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    621   1.1    tsubai 
    622   1.1    tsubai 	r = &sc->sc_cmap_red[index];
    623   1.1    tsubai 	g = &sc->sc_cmap_green[index];
    624   1.1    tsubai 	b = &sc->sc_cmap_blue[index];
    625   1.1    tsubai 	for (i = 0; i < count; i++) {
    626   1.1    tsubai 		OF_call_method_1("color!", dc->dc_ih, 4, *r, *g, *b, index);
    627   1.1    tsubai 		r++, g++, b++, index++;
    628   1.1    tsubai 	}
    629   1.1    tsubai 	return 0;
    630   1.1    tsubai }
    631