Home | History | Annotate | Line # | Download | only in pci
genfb_pci.c revision 1.11
      1 /*	$NetBSD: genfb_pci.c,v 1.11 2008/05/05 11:42:45 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2007 Michael Lorenz
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: genfb_pci.c,v 1.11 2008/05/05 11:42:45 jmcneill Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 #include <sys/kernel.h>
     35 #include <sys/device.h>
     36 #include <sys/proc.h>
     37 #include <sys/mutex.h>
     38 #include <sys/ioctl.h>
     39 #include <sys/kernel.h>
     40 #include <sys/systm.h>
     41 #include <sys/kauth.h>
     42 
     43 #include <dev/pci/pcidevs.h>
     44 #include <dev/pci/pcireg.h>
     45 #include <dev/pci/pcivar.h>
     46 #include <dev/pci/pciio.h>
     47 
     48 #include <dev/wsfb/genfbvar.h>
     49 
     50 #include "opt_wsfb.h"
     51 #include "opt_genfb.h"
     52 
     53 #ifdef GENFB_PCI_DEBUG
     54 # define DPRINTF printf
     55 #else
     56 # define DPRINTF while (0) printf
     57 #endif
     58 
     59 struct range {
     60 	bus_addr_t offset;
     61 	bus_size_t size;
     62 	int flags;
     63 };
     64 
     65 struct pci_genfb_softc {
     66 	struct genfb_softc sc_gen;
     67 
     68 	pci_chipset_tag_t sc_pc;
     69 	pcitag_t sc_pcitag;
     70 	bus_space_tag_t sc_memt;
     71 	bus_space_tag_t sc_iot;
     72 	bus_space_handle_t sc_memh;
     73 	pcireg_t sc_bars[9];
     74 	struct range sc_ranges[8];
     75 	int sc_ranges_used;
     76 	int sc_want_wsfb;
     77 };
     78 
     79 static int	pci_genfb_match(struct device *, struct cfdata *, void *);
     80 static void	pci_genfb_attach(struct device *, struct device *, void *);
     81 static int	pci_genfb_ioctl(void *, void *, u_long, void *, int,
     82 		    struct lwp *);
     83 static paddr_t	pci_genfb_mmap(void *, void *, off_t, int);
     84 static int	pci_genfb_drm_print(void *, const char *);
     85 
     86 
     87 CFATTACH_DECL(genfb_pci, sizeof(struct pci_genfb_softc),
     88     pci_genfb_match, pci_genfb_attach, NULL, NULL);
     89 
     90 static int
     91 pci_genfb_match(struct device *parent, struct cfdata *match, void *aux)
     92 {
     93 	struct pci_attach_args *pa = aux;
     94 
     95 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE &&
     96 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_CONTROL)
     97 		return 1;
     98 
     99 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY)
    100 		return 1;
    101 
    102 	return 0;
    103 }
    104 
    105 static void
    106 pci_genfb_attach(struct device *parent, struct device *self, void *aux)
    107 {
    108 	struct pci_genfb_softc *sc = (struct pci_genfb_softc *)self;
    109 	struct pci_attach_args *pa = aux;
    110 	struct genfb_ops ops;
    111 	int idx, bar, type;
    112 	char devinfo[256];
    113 
    114 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    115 	printf(": %s\n", devinfo);
    116 
    117 	sc->sc_memt = pa->pa_memt;
    118 	sc->sc_iot = pa->pa_iot;
    119 	sc->sc_pc = pa->pa_pc;
    120 	sc->sc_pcitag = pa->pa_tag;
    121 	sc->sc_want_wsfb = 0;
    122 
    123 	genfb_init(&sc->sc_gen);
    124 
    125 	if ((sc->sc_gen.sc_width == 0) || (sc->sc_gen.sc_fbsize == 0)) {
    126 		aprint_error("%s: bogus parameters, unable to continue\n",
    127 		    device_xname(self));
    128 		return;
    129 	}
    130 
    131 	if (bus_space_map(sc->sc_memt, sc->sc_gen.sc_fboffset,
    132 	    sc->sc_gen.sc_fbsize, BUS_SPACE_MAP_LINEAR, &sc->sc_memh) != 0) {
    133 
    134 		aprint_error_dev(self, "unable to map the framebuffer\n");
    135 		return;
    136 	}
    137 	sc->sc_gen.sc_fbaddr = bus_space_vaddr(sc->sc_memt, sc->sc_memh);
    138 
    139 	/* mmap()able bus ranges */
    140 	idx = 0;
    141 	bar = 0x10;
    142 	while (bar < 0x30) {
    143 
    144 		type = pci_mapreg_type(sc->sc_pc, sc->sc_pcitag, bar);
    145 		if ((type == PCI_MAPREG_TYPE_MEM) ||
    146 		    (type == PCI_MAPREG_TYPE_ROM)) {
    147 
    148 			pci_mapreg_info(sc->sc_pc, sc->sc_pcitag, bar, type,
    149 			    &sc->sc_ranges[idx].offset,
    150 			    &sc->sc_ranges[idx].size,
    151 			    &sc->sc_ranges[idx].flags);
    152 			idx++;
    153 		}
    154 		sc->sc_bars[(bar - 0x10) >> 2] =
    155 		    pci_conf_read(sc->sc_pc, sc->sc_pcitag, bar);
    156 		bar += 4;
    157 	}
    158 	sc->sc_ranges_used = idx;
    159 
    160 	ops.genfb_ioctl = pci_genfb_ioctl;
    161 	ops.genfb_mmap = pci_genfb_mmap;
    162 
    163 	if (genfb_attach(&sc->sc_gen, &ops) == 0) {
    164 
    165 		/* now try to attach a DRM */
    166 		config_found_ia(self, "drm", aux, pci_genfb_drm_print);
    167 	}
    168 }
    169 
    170 static int
    171 pci_genfb_drm_print(void *aux, const char *pnp)
    172 {
    173 	if (pnp)
    174 		aprint_normal("drm at %s", pnp);
    175 	return (UNCONF);
    176 }
    177 
    178 
    179 static int
    180 pci_genfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    181     struct lwp *l)
    182 {
    183 	struct pci_genfb_softc *sc = v;
    184 
    185 	switch (cmd) {
    186 		case WSDISPLAYIO_GTYPE:
    187 			*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
    188 			return 0;
    189 
    190 		/* PCI config read/write passthrough. */
    191 		case PCI_IOC_CFGREAD:
    192 		case PCI_IOC_CFGWRITE:
    193 			return (pci_devioctl(sc->sc_pc, sc->sc_pcitag,
    194 			    cmd, data, flag, l));
    195 		case WSDISPLAYIO_SMODE:
    196 			{
    197 				int new_mode = *(int*)data, i;
    198 				if (new_mode == WSDISPLAYIO_MODE_EMUL) {
    199 					for (i = 0; i < 9; i++)
    200 						pci_conf_write(sc->sc_pc,
    201 						     sc->sc_pcitag,
    202 						     0x10 + (i << 2),
    203 						     sc->sc_bars[i]);
    204 				}
    205 			}
    206 			return 0;
    207 	}
    208 
    209 	return EPASSTHROUGH;
    210 }
    211 
    212 static paddr_t
    213 pci_genfb_mmap(void *v, void *vs, off_t offset, int prot)
    214 {
    215 	struct pci_genfb_softc *sc = v;
    216 	struct range *r;
    217 	struct lwp *me;
    218 	int i;
    219 
    220 	if (offset == 0)
    221 		sc->sc_want_wsfb = 1;
    222 
    223 	/*
    224 	 * regular fb mapping at 0
    225 	 * since some Sun firmware likes to put PCI resources low enough
    226 	 * to collide with the wsfb mapping we only allow it after asking
    227 	 * for offset 0
    228 	 */
    229 	DPRINTF("%s: %08x limit %08x\n", __func__, (uint32_t)offset,
    230 	    (uint32_t)sc->sc_gen.sc_fbsize);
    231 	if ((offset >= 0) && (offset < sc->sc_gen.sc_fbsize) &&
    232 	    (sc->sc_want_wsfb == 1)) {
    233 
    234 		return bus_space_mmap(sc->sc_memt, sc->sc_gen.sc_fboffset,
    235 		   offset, prot, BUS_SPACE_MAP_LINEAR);
    236 	}
    237 
    238 	/*
    239 	 * restrict all other mappings to processes with superuser privileges
    240 	 * or the kernel itself
    241 	 */
    242 	me = curlwp;
    243 	if (me != NULL) {
    244 		if (kauth_authorize_generic(me->l_cred, KAUTH_GENERIC_ISSUSER,
    245 		    NULL) != 0) {
    246 			aprint_normal_dev(&sc->sc_gen.sc_dev, "mmap() rejected.\n");
    247 			return -1;
    248 		}
    249 	}
    250 
    251 #ifdef WSFB_FAKE_VGA_FB
    252 	if ((offset >= 0xa0000) && (offset < 0xbffff)) {
    253 
    254 		return bus_space_mmap(sc->sc_memt, sc->sc_gen.sc_fboffset,
    255 		   offset - 0xa0000, prot, BUS_SPACE_MAP_LINEAR);
    256 	}
    257 #endif
    258 
    259 	/*
    260 	 * XXX this should be generalized, let's just
    261 	 * #define PCI_IOAREA_PADDR
    262 	 * #define PCI_IOAREA_OFFSET
    263 	 * #define PCI_IOAREA_SIZE
    264 	 * somewhere in a MD header and compile this code only if all are
    265 	 * present
    266 	 */
    267 #ifdef PCI_MAGIC_IO_RANGE
    268 	/* allow to map our IO space */
    269 	if ((offset >= PCI_MAGIC_IO_RANGE) &&
    270 	    (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
    271 		return bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
    272 		    0, prot, BUS_SPACE_MAP_LINEAR);
    273 	}
    274 #endif
    275 
    276 	/* allow to mmap() our BARs */
    277 	/* maybe the ROM BAR too? */
    278 	for (i = 0; i < sc->sc_ranges_used; i++) {
    279 
    280 		r = &sc->sc_ranges[i];
    281 		if ((offset >= r->offset) && (offset < (r->offset + r->size))) {
    282 			return bus_space_mmap(sc->sc_memt, offset, 0, prot,
    283 			    r->flags);
    284 		}
    285 	}
    286 
    287 	return -1;
    288 }
    289