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