Home | History | Annotate | Line # | Download | only in pci
genfb_pci.c revision 1.34.2.2
      1  1.34.2.1       tls /*	$NetBSD: genfb_pci.c,v 1.34.2.2 2017/12/03 11:37:07 jdolecek 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  *
     16       1.1  macallan  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17       1.1  macallan  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18       1.1  macallan  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19       1.1  macallan  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20       1.1  macallan  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21       1.1  macallan  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22       1.1  macallan  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23       1.1  macallan  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24       1.1  macallan  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25       1.1  macallan  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26       1.1  macallan  * POSSIBILITY OF SUCH DAMAGE.
     27       1.1  macallan  */
     28       1.1  macallan 
     29       1.1  macallan #include <sys/cdefs.h>
     30  1.34.2.1       tls __KERNEL_RCSID(0, "$NetBSD: genfb_pci.c,v 1.34.2.2 2017/12/03 11:37:07 jdolecek Exp $");
     31       1.1  macallan 
     32       1.1  macallan #include <sys/param.h>
     33       1.1  macallan #include <sys/systm.h>
     34       1.1  macallan #include <sys/kernel.h>
     35       1.1  macallan #include <sys/device.h>
     36       1.1  macallan #include <sys/proc.h>
     37       1.1  macallan #include <sys/mutex.h>
     38       1.1  macallan #include <sys/ioctl.h>
     39       1.1  macallan #include <sys/kernel.h>
     40       1.1  macallan #include <sys/systm.h>
     41       1.1  macallan #include <sys/kauth.h>
     42       1.1  macallan 
     43       1.1  macallan #include <dev/pci/pcidevs.h>
     44       1.1  macallan #include <dev/pci/pcireg.h>
     45       1.1  macallan #include <dev/pci/pcivar.h>
     46       1.1  macallan #include <dev/pci/pciio.h>
     47       1.1  macallan 
     48       1.1  macallan #include <dev/wsfb/genfbvar.h>
     49      1.27    cegger #include <dev/pci/wsdisplay_pci.h>
     50       1.1  macallan 
     51      1.23  jmcneill #include <dev/pci/genfb_pcivar.h>
     52      1.23  jmcneill 
     53       1.1  macallan #include "opt_wsfb.h"
     54       1.1  macallan #include "opt_genfb.h"
     55       1.1  macallan 
     56       1.2  macallan #ifdef GENFB_PCI_DEBUG
     57       1.2  macallan # define DPRINTF printf
     58       1.2  macallan #else
     59       1.2  macallan # define DPRINTF while (0) printf
     60       1.2  macallan #endif
     61       1.2  macallan 
     62      1.18    cegger static int	pci_genfb_match(device_t, cfdata_t, void *);
     63      1.18    cegger static void	pci_genfb_attach(device_t, device_t, void *);
     64       1.1  macallan static int	pci_genfb_ioctl(void *, void *, u_long, void *, int,
     65       1.1  macallan 		    struct lwp *);
     66       1.1  macallan static paddr_t	pci_genfb_mmap(void *, void *, off_t, int);
     67      1.13  jmcneill static int	pci_genfb_borrow(void *, bus_addr_t, bus_space_handle_t *);
     68       1.2  macallan static int	pci_genfb_drm_print(void *, const char *);
     69      1.28  jmcneill static bool	pci_genfb_shutdown(device_t, int);
     70       1.2  macallan 
     71      1.25  macallan CFATTACH_DECL_NEW(genfb_pci, sizeof(struct pci_genfb_softc),
     72       1.1  macallan     pci_genfb_match, pci_genfb_attach, NULL, NULL);
     73       1.1  macallan 
     74       1.1  macallan static int
     75      1.18    cegger pci_genfb_match(device_t parent, cfdata_t match, void *aux)
     76       1.1  macallan {
     77       1.1  macallan 	struct pci_attach_args *pa = aux;
     78      1.12  jmcneill 	int matchlvl = 1;
     79      1.12  jmcneill 
     80      1.14  jmcneill 	if (!genfb_is_enabled())
     81      1.14  jmcneill 		return 0;	/* explicitly disabled by MD code */
     82      1.14  jmcneill 
     83      1.12  jmcneill 	if (genfb_is_console())
     84      1.12  jmcneill 		matchlvl = 5;	/* beat VGA */
     85       1.1  macallan 
     86       1.1  macallan 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE &&
     87       1.1  macallan 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_CONTROL)
     88      1.12  jmcneill 		return matchlvl;
     89       1.1  macallan 
     90       1.1  macallan 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY)
     91      1.12  jmcneill 		return matchlvl;
     92       1.1  macallan 
     93       1.1  macallan 	return 0;
     94       1.1  macallan }
     95       1.1  macallan 
     96       1.1  macallan static void
     97      1.18    cegger pci_genfb_attach(device_t parent, device_t self, void *aux)
     98       1.1  macallan {
     99      1.20    cegger 	struct pci_genfb_softc *sc = device_private(self);
    100       1.1  macallan 	struct pci_attach_args *pa = aux;
    101  1.34.2.1       tls 	static const struct genfb_ops zero_ops;
    102  1.34.2.1       tls 	struct genfb_ops ops = zero_ops;
    103      1.24  macallan 	pcireg_t rom;
    104       1.1  macallan 	int idx, bar, type;
    105       1.1  macallan 
    106      1.33  drochner 	pci_aprint_devinfo(pa, NULL);
    107       1.1  macallan 
    108      1.25  macallan 	sc->sc_gen.sc_dev = self;
    109       1.1  macallan 	sc->sc_memt = pa->pa_memt;
    110       1.1  macallan 	sc->sc_iot = pa->pa_iot;
    111       1.1  macallan 	sc->sc_pc = pa->pa_pc;
    112       1.1  macallan 	sc->sc_pcitag = pa->pa_tag;
    113       1.5  macallan 	sc->sc_want_wsfb = 0;
    114       1.5  macallan 
    115      1.22  jmcneill 	genfb_init(&sc->sc_gen);
    116      1.22  jmcneill 
    117      1.21  jmcneill 	/* firmware / MD code responsible for restoring the display */
    118      1.22  jmcneill 	if (sc->sc_gen.sc_pmfcb == NULL)
    119      1.28  jmcneill 		pmf_device_register1(self, NULL, NULL,
    120      1.28  jmcneill 		    pci_genfb_shutdown);
    121      1.22  jmcneill 	else
    122      1.28  jmcneill 		pmf_device_register1(self,
    123      1.22  jmcneill 		    sc->sc_gen.sc_pmfcb->gpc_suspend,
    124      1.28  jmcneill 		    sc->sc_gen.sc_pmfcb->gpc_resume,
    125      1.28  jmcneill 		    pci_genfb_shutdown);
    126       1.1  macallan 
    127       1.2  macallan 	if ((sc->sc_gen.sc_width == 0) || (sc->sc_gen.sc_fbsize == 0)) {
    128      1.16  jmcneill 		aprint_debug_dev(self, "not configured by firmware\n");
    129       1.2  macallan 		return;
    130       1.2  macallan 	}
    131       1.1  macallan 
    132      1.32  macallan 	/*
    133      1.32  macallan 	 * if some MD code handed us a framebuffer VA we use that instead of
    134      1.32  macallan 	 * mapping our own
    135      1.32  macallan 	 */
    136      1.32  macallan 	if (sc->sc_gen.sc_fbaddr == NULL) {
    137      1.32  macallan 		if (bus_space_map(sc->sc_memt, sc->sc_gen.sc_fboffset,
    138      1.32  macallan 		    sc->sc_gen.sc_fbsize,
    139      1.32  macallan 		    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE,
    140      1.32  macallan 		    &sc->sc_memh) != 0) {
    141  1.34.2.2  jdolecek 			aprint_error_dev(self,
    142  1.34.2.2  jdolecek 			    "unable to map the framebuffer\n");
    143      1.32  macallan 			return;
    144      1.32  macallan 		}
    145  1.34.2.2  jdolecek 		sc->sc_gen.sc_fbaddr = bus_space_vaddr(sc->sc_memt,
    146  1.34.2.2  jdolecek 		    sc->sc_memh);
    147      1.32  macallan 	} else
    148      1.32  macallan 		aprint_debug("%s: recycling existing fb mapping at %lx\n",
    149  1.34.2.2  jdolecek 		    device_xname(sc->sc_gen.sc_dev),
    150  1.34.2.2  jdolecek 		    (unsigned long)sc->sc_gen.sc_fbaddr);
    151       1.1  macallan 
    152       1.1  macallan 	/* mmap()able bus ranges */
    153       1.1  macallan 	idx = 0;
    154      1.30  jmcneill 	bar = PCI_MAPREG_START;
    155      1.30  jmcneill 	while (bar <= PCI_MAPREG_ROM) {
    156       1.1  macallan 
    157      1.31       phx 		sc->sc_bars[(bar - PCI_MAPREG_START) >> 2] = rom =
    158      1.31       phx 		    pci_conf_read(sc->sc_pc, sc->sc_pcitag, bar);
    159      1.31       phx 
    160      1.31       phx 		if ((bar >= PCI_MAPREG_END && bar < PCI_MAPREG_ROM) ||
    161      1.31       phx 		    pci_mapreg_probe(sc->sc_pc, sc->sc_pcitag, bar, &type)
    162      1.31       phx 		    == 0) {
    163      1.31       phx 			/* skip unimplemented and non-BAR registers */
    164      1.31       phx 			bar += 4;
    165      1.31       phx 			continue;
    166      1.31       phx 		}
    167      1.30  jmcneill 		if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_MEM ||
    168      1.30  jmcneill 		    PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_ROM) {
    169       1.1  macallan 			pci_mapreg_info(sc->sc_pc, sc->sc_pcitag, bar, type,
    170       1.1  macallan 			    &sc->sc_ranges[idx].offset,
    171       1.1  macallan 			    &sc->sc_ranges[idx].size,
    172       1.1  macallan 			    &sc->sc_ranges[idx].flags);
    173       1.1  macallan 			idx++;
    174       1.1  macallan 		}
    175      1.24  macallan 		if ((bar == PCI_MAPREG_ROM) && (rom != 0)) {
    176      1.24  macallan 			pci_conf_write(sc->sc_pc, sc->sc_pcitag, bar, rom |
    177      1.24  macallan 			    PCI_MAPREG_ROM_ENABLE);
    178      1.24  macallan 		}
    179      1.30  jmcneill 		if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_MEM &&
    180      1.30  jmcneill 		    PCI_MAPREG_MEM_TYPE(type) == PCI_MAPREG_MEM_TYPE_64BIT)
    181      1.30  jmcneill 			bar += 8;
    182      1.30  jmcneill 		else
    183      1.30  jmcneill 			bar += 4;
    184       1.1  macallan 	}
    185      1.24  macallan 
    186       1.1  macallan 	sc->sc_ranges_used = idx;
    187       1.1  macallan 
    188       1.1  macallan 	ops.genfb_ioctl = pci_genfb_ioctl;
    189       1.1  macallan 	ops.genfb_mmap = pci_genfb_mmap;
    190      1.13  jmcneill 	ops.genfb_borrow = pci_genfb_borrow;
    191       1.1  macallan 
    192       1.7       phx 	if (genfb_attach(&sc->sc_gen, &ops) == 0) {
    193       1.2  macallan 
    194       1.7       phx 		/* now try to attach a DRM */
    195       1.7       phx 		config_found_ia(self, "drm", aux, pci_genfb_drm_print);
    196       1.7       phx 	}
    197       1.1  macallan }
    198       1.1  macallan 
    199       1.1  macallan static int
    200       1.2  macallan pci_genfb_drm_print(void *aux, const char *pnp)
    201       1.2  macallan {
    202       1.2  macallan 	if (pnp)
    203      1.11  jmcneill 		aprint_normal("drm at %s", pnp);
    204      1.11  jmcneill 	return (UNCONF);
    205       1.2  macallan }
    206       1.2  macallan 
    207       1.2  macallan 
    208       1.2  macallan static int
    209       1.1  macallan pci_genfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    210       1.1  macallan     struct lwp *l)
    211       1.1  macallan {
    212       1.1  macallan 	struct pci_genfb_softc *sc = v;
    213       1.1  macallan 
    214       1.1  macallan 	switch (cmd) {
    215      1.26    cegger 	case WSDISPLAYIO_GTYPE:
    216      1.26    cegger 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
    217      1.26    cegger 		return 0;
    218      1.26    cegger 
    219      1.26    cegger 	/* PCI config read/write passthrough. */
    220      1.26    cegger 	case PCI_IOC_CFGREAD:
    221      1.26    cegger 	case PCI_IOC_CFGWRITE:
    222      1.26    cegger 		return pci_devioctl(sc->sc_pc, sc->sc_pcitag,
    223      1.26    cegger 		    cmd, data, flag, l);
    224      1.27    cegger 
    225      1.27    cegger 	case WSDISPLAYIO_GET_BUSID:
    226      1.27    cegger 		return wsdisplayio_busid_pci(sc->sc_gen.sc_dev, sc->sc_pc,
    227      1.27    cegger 		    sc->sc_pcitag, data);
    228      1.27    cegger 
    229      1.26    cegger 	case WSDISPLAYIO_SMODE: {
    230      1.26    cegger 		int new_mode = *(int*)data, i;
    231      1.26    cegger 		if (new_mode == WSDISPLAYIO_MODE_EMUL) {
    232      1.26    cegger 			for (i = 0; i < 9; i++)
    233      1.26    cegger 				pci_conf_write(sc->sc_pc,
    234      1.26    cegger 				     sc->sc_pcitag,
    235      1.26    cegger 				     0x10 + (i << 2),
    236      1.26    cegger 				     sc->sc_bars[i]);
    237      1.26    cegger 		}
    238      1.26    cegger 		}
    239      1.26    cegger 		return 0;
    240       1.1  macallan 	}
    241       1.1  macallan 
    242       1.1  macallan 	return EPASSTHROUGH;
    243       1.1  macallan }
    244       1.1  macallan 
    245       1.1  macallan static paddr_t
    246       1.1  macallan pci_genfb_mmap(void *v, void *vs, off_t offset, int prot)
    247       1.1  macallan {
    248       1.1  macallan 	struct pci_genfb_softc *sc = v;
    249       1.1  macallan 	struct range *r;
    250       1.1  macallan 	int i;
    251       1.1  macallan 
    252       1.5  macallan 	if (offset == 0)
    253       1.5  macallan 		sc->sc_want_wsfb = 1;
    254       1.5  macallan 
    255       1.5  macallan 	/*
    256       1.5  macallan 	 * regular fb mapping at 0
    257       1.5  macallan 	 * since some Sun firmware likes to put PCI resources low enough
    258       1.5  macallan 	 * to collide with the wsfb mapping we only allow it after asking
    259       1.5  macallan 	 * for offset 0
    260       1.5  macallan 	 */
    261       1.2  macallan 	DPRINTF("%s: %08x limit %08x\n", __func__, (uint32_t)offset,
    262       1.2  macallan 	    (uint32_t)sc->sc_gen.sc_fbsize);
    263       1.5  macallan 	if ((offset >= 0) && (offset < sc->sc_gen.sc_fbsize) &&
    264       1.5  macallan 	    (sc->sc_want_wsfb == 1)) {
    265       1.1  macallan 
    266       1.1  macallan 		return bus_space_mmap(sc->sc_memt, sc->sc_gen.sc_fboffset,
    267      1.29  jmcneill 		   offset, prot,
    268      1.29  jmcneill 		   BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
    269       1.1  macallan 	}
    270       1.1  macallan 
    271       1.1  macallan 	/*
    272       1.1  macallan 	 * restrict all other mappings to processes with superuser privileges
    273       1.1  macallan 	 * or the kernel itself
    274       1.1  macallan 	 */
    275      1.34      elad 	if (kauth_authorize_machdep(kauth_cred_get(), KAUTH_MACHDEP_UNMANAGEDMEM,
    276      1.34      elad 	    NULL, NULL, NULL, NULL) != 0) {
    277      1.25  macallan 		aprint_normal_dev(sc->sc_gen.sc_dev, "mmap() rejected.\n");
    278      1.19      elad 		return -1;
    279       1.1  macallan 	}
    280       1.1  macallan 
    281       1.1  macallan #ifdef WSFB_FAKE_VGA_FB
    282       1.1  macallan 	if ((offset >= 0xa0000) && (offset < 0xbffff)) {
    283       1.1  macallan 
    284       1.1  macallan 		return bus_space_mmap(sc->sc_memt, sc->sc_gen.sc_fboffset,
    285       1.1  macallan 		   offset - 0xa0000, prot, BUS_SPACE_MAP_LINEAR);
    286       1.1  macallan 	}
    287       1.1  macallan #endif
    288       1.1  macallan 
    289       1.1  macallan 	/*
    290       1.1  macallan 	 * XXX this should be generalized, let's just
    291       1.1  macallan 	 * #define PCI_IOAREA_PADDR
    292       1.1  macallan 	 * #define PCI_IOAREA_OFFSET
    293       1.1  macallan 	 * #define PCI_IOAREA_SIZE
    294       1.1  macallan 	 * somewhere in a MD header and compile this code only if all are
    295       1.1  macallan 	 * present
    296       1.1  macallan 	 */
    297      1.24  macallan 	/*
    298      1.24  macallan 	 * no.
    299      1.24  macallan 	 * PCI_IOAREA_PADDR would be completely, utterly wrong and completely
    300      1.24  macallan 	 * useless for the following reasons:
    301      1.24  macallan 	 * - it's a bus address, not a physical address
    302      1.24  macallan 	 * - there's no guarantee it's the same for each host bridge
    303      1.24  macallan 	 * - it's already taken care of by the IO tag
    304      1.24  macallan 	 * PCI_IOAREA_OFFSET is the same as PCI_MAGIC_IO_RANGE
    305      1.24  macallan 	 * PCI_IOAREA_SIZE is also useless:
    306      1.24  macallan 	 * - many cards don't decode more than 16 bit IO anyway
    307      1.24  macallan 	 * - even machines with more than 64kB IO space try to keep everything
    308      1.24  macallan 	 *   within 64kB for the reason above
    309      1.24  macallan 	 * - IO ranges tend to be small so in most cases you can't cram enough
    310      1.24  macallan 	 *   cards into a single machine to exhaust 64kB IO space
    311      1.24  macallan 	 * - machines which need this tend to prefer memory space anyway
    312      1.24  macallan 	 * - the only use for this right now is to allow the Xserver to map
    313      1.24  macallan 	 *   VGA registers on macppc and a few other powerpc ports, shark uses
    314      1.24  macallan 	 *   a similar mechanism, and what they need is always within 64kB
    315      1.24  macallan 	 */
    316       1.6  macallan #ifdef PCI_MAGIC_IO_RANGE
    317       1.1  macallan 	/* allow to map our IO space */
    318       1.6  macallan 	if ((offset >= PCI_MAGIC_IO_RANGE) &&
    319       1.6  macallan 	    (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
    320       1.6  macallan 		return bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
    321       1.6  macallan 		    0, prot, BUS_SPACE_MAP_LINEAR);
    322       1.1  macallan 	}
    323       1.1  macallan #endif
    324       1.1  macallan 
    325       1.1  macallan 	/* allow to mmap() our BARs */
    326       1.2  macallan 	/* maybe the ROM BAR too? */
    327       1.1  macallan 	for (i = 0; i < sc->sc_ranges_used; i++) {
    328       1.1  macallan 
    329       1.1  macallan 		r = &sc->sc_ranges[i];
    330       1.1  macallan 		if ((offset >= r->offset) && (offset < (r->offset + r->size))) {
    331       1.1  macallan 			return bus_space_mmap(sc->sc_memt, offset, 0, prot,
    332       1.1  macallan 			    r->flags);
    333       1.1  macallan 		}
    334       1.1  macallan 	}
    335       1.1  macallan 
    336       1.1  macallan 	return -1;
    337       1.1  macallan }
    338      1.13  jmcneill 
    339      1.13  jmcneill int
    340      1.13  jmcneill pci_genfb_borrow(void *opaque, bus_addr_t addr, bus_space_handle_t *hdlp)
    341      1.13  jmcneill {
    342      1.13  jmcneill 	struct pci_genfb_softc *sc = opaque;
    343      1.13  jmcneill 
    344      1.13  jmcneill 	if (sc == NULL)
    345      1.13  jmcneill 		return 0;
    346      1.13  jmcneill 	if (!sc->sc_gen.sc_fboffset)
    347      1.13  jmcneill 		return 0;
    348      1.13  jmcneill 	if (sc->sc_gen.sc_fboffset != addr)
    349      1.13  jmcneill 		return 0;
    350      1.13  jmcneill 	*hdlp = sc->sc_memh;
    351      1.13  jmcneill 	return 1;
    352      1.13  jmcneill }
    353      1.28  jmcneill 
    354      1.28  jmcneill static bool
    355      1.28  jmcneill pci_genfb_shutdown(device_t self, int flags)
    356      1.28  jmcneill {
    357      1.28  jmcneill 	genfb_enable_polling(self);
    358      1.28  jmcneill 	return true;
    359      1.28  jmcneill }
    360