Home | History | Annotate | Line # | Download | only in pci
genfb_pci.c revision 1.9.4.5
      1  1.9.4.5      yamt /*	$NetBSD: genfb_pci.c,v 1.9.4.5 2010/03/11 15:03:44 yamt 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.9.4.5      yamt __KERNEL_RCSID(0, "$NetBSD: genfb_pci.c,v 1.9.4.5 2010/03/11 15:03:44 yamt 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.1  macallan 
     50  1.9.4.4      yamt #include <dev/pci/genfb_pcivar.h>
     51  1.9.4.4      yamt 
     52      1.1  macallan #include "opt_wsfb.h"
     53      1.1  macallan #include "opt_genfb.h"
     54      1.1  macallan 
     55      1.2  macallan #ifdef GENFB_PCI_DEBUG
     56      1.2  macallan # define DPRINTF printf
     57      1.2  macallan #else
     58      1.2  macallan # define DPRINTF while (0) printf
     59      1.2  macallan #endif
     60      1.2  macallan 
     61  1.9.4.3      yamt static int	pci_genfb_match(device_t, cfdata_t, void *);
     62  1.9.4.3      yamt static void	pci_genfb_attach(device_t, device_t, void *);
     63      1.1  macallan static int	pci_genfb_ioctl(void *, void *, u_long, void *, int,
     64      1.1  macallan 		    struct lwp *);
     65      1.1  macallan static paddr_t	pci_genfb_mmap(void *, void *, off_t, int);
     66  1.9.4.2      yamt static int	pci_genfb_borrow(void *, bus_addr_t, bus_space_handle_t *);
     67      1.2  macallan static int	pci_genfb_drm_print(void *, const char *);
     68      1.2  macallan 
     69      1.1  macallan CFATTACH_DECL(genfb_pci, sizeof(struct pci_genfb_softc),
     70      1.1  macallan     pci_genfb_match, pci_genfb_attach, NULL, NULL);
     71      1.1  macallan 
     72      1.1  macallan static int
     73  1.9.4.3      yamt pci_genfb_match(device_t parent, cfdata_t match, void *aux)
     74      1.1  macallan {
     75      1.1  macallan 	struct pci_attach_args *pa = aux;
     76  1.9.4.2      yamt 	int matchlvl = 1;
     77  1.9.4.2      yamt 
     78  1.9.4.2      yamt 	if (!genfb_is_enabled())
     79  1.9.4.2      yamt 		return 0;	/* explicitly disabled by MD code */
     80  1.9.4.2      yamt 
     81  1.9.4.2      yamt 	if (genfb_is_console())
     82  1.9.4.2      yamt 		matchlvl = 5;	/* beat VGA */
     83      1.1  macallan 
     84      1.1  macallan 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE &&
     85      1.1  macallan 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_CONTROL)
     86  1.9.4.2      yamt 		return matchlvl;
     87      1.1  macallan 
     88      1.1  macallan 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY)
     89  1.9.4.2      yamt 		return matchlvl;
     90      1.1  macallan 
     91      1.1  macallan 	return 0;
     92      1.1  macallan }
     93      1.1  macallan 
     94      1.1  macallan static void
     95  1.9.4.3      yamt pci_genfb_attach(device_t parent, device_t self, void *aux)
     96      1.1  macallan {
     97  1.9.4.3      yamt 	struct pci_genfb_softc *sc = device_private(self);
     98      1.1  macallan 	struct pci_attach_args *pa = aux;
     99      1.1  macallan 	struct genfb_ops ops;
    100  1.9.4.5      yamt 	pcireg_t rom;
    101      1.1  macallan 	int idx, bar, type;
    102      1.1  macallan 	char devinfo[256];
    103      1.1  macallan 
    104      1.1  macallan 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    105  1.9.4.2      yamt 	aprint_naive("\n");
    106  1.9.4.2      yamt 	aprint_normal(": %s\n", devinfo);
    107      1.1  macallan 
    108      1.1  macallan 	sc->sc_memt = pa->pa_memt;
    109      1.1  macallan 	sc->sc_iot = pa->pa_iot;
    110      1.1  macallan 	sc->sc_pc = pa->pa_pc;
    111      1.1  macallan 	sc->sc_pcitag = pa->pa_tag;
    112      1.5  macallan 	sc->sc_want_wsfb = 0;
    113      1.5  macallan 
    114      1.2  macallan 	genfb_init(&sc->sc_gen);
    115      1.1  macallan 
    116  1.9.4.4      yamt 	/* firmware / MD code responsible for restoring the display */
    117  1.9.4.4      yamt 	if (sc->sc_gen.sc_pmfcb == NULL)
    118  1.9.4.4      yamt 		pmf_device_register(self, NULL, NULL);
    119  1.9.4.4      yamt 	else
    120  1.9.4.4      yamt 		pmf_device_register(self,
    121  1.9.4.4      yamt 		    sc->sc_gen.sc_pmfcb->gpc_suspend,
    122  1.9.4.4      yamt 		    sc->sc_gen.sc_pmfcb->gpc_resume);
    123  1.9.4.4      yamt 
    124      1.2  macallan 	if ((sc->sc_gen.sc_width == 0) || (sc->sc_gen.sc_fbsize == 0)) {
    125  1.9.4.2      yamt 		aprint_debug_dev(self, "not configured by firmware\n");
    126      1.2  macallan 		return;
    127      1.2  macallan 	}
    128      1.1  macallan 
    129      1.1  macallan 	if (bus_space_map(sc->sc_memt, sc->sc_gen.sc_fboffset,
    130      1.1  macallan 	    sc->sc_gen.sc_fbsize, BUS_SPACE_MAP_LINEAR, &sc->sc_memh) != 0) {
    131      1.1  macallan 
    132      1.9    cegger 		aprint_error_dev(self, "unable to map the framebuffer\n");
    133      1.8       mrg 		return;
    134      1.1  macallan 	}
    135      1.1  macallan 	sc->sc_gen.sc_fbaddr = bus_space_vaddr(sc->sc_memt, sc->sc_memh);
    136      1.1  macallan 
    137      1.1  macallan 	/* mmap()able bus ranges */
    138      1.1  macallan 	idx = 0;
    139      1.1  macallan 	bar = 0x10;
    140  1.9.4.5      yamt 	while (bar < 0x34) {
    141      1.1  macallan 
    142      1.1  macallan 		type = pci_mapreg_type(sc->sc_pc, sc->sc_pcitag, bar);
    143      1.1  macallan 		if ((type == PCI_MAPREG_TYPE_MEM) ||
    144      1.1  macallan 		    (type == PCI_MAPREG_TYPE_ROM)) {
    145      1.1  macallan 
    146      1.1  macallan 			pci_mapreg_info(sc->sc_pc, sc->sc_pcitag, bar, type,
    147      1.1  macallan 			    &sc->sc_ranges[idx].offset,
    148      1.1  macallan 			    &sc->sc_ranges[idx].size,
    149      1.1  macallan 			    &sc->sc_ranges[idx].flags);
    150      1.1  macallan 			idx++;
    151      1.1  macallan 		}
    152  1.9.4.5      yamt 		sc->sc_bars[(bar - 0x10) >> 2] = rom =
    153      1.2  macallan 		    pci_conf_read(sc->sc_pc, sc->sc_pcitag, bar);
    154  1.9.4.5      yamt 		if ((bar == PCI_MAPREG_ROM) && (rom != 0)) {
    155  1.9.4.5      yamt 			pci_conf_write(sc->sc_pc, sc->sc_pcitag, bar, rom |
    156  1.9.4.5      yamt 			    PCI_MAPREG_ROM_ENABLE);
    157  1.9.4.5      yamt 		}
    158      1.1  macallan 		bar += 4;
    159      1.1  macallan 	}
    160  1.9.4.5      yamt 
    161      1.1  macallan 	sc->sc_ranges_used = idx;
    162      1.1  macallan 
    163      1.1  macallan 	ops.genfb_ioctl = pci_genfb_ioctl;
    164      1.1  macallan 	ops.genfb_mmap = pci_genfb_mmap;
    165  1.9.4.2      yamt 	ops.genfb_borrow = pci_genfb_borrow;
    166      1.1  macallan 
    167      1.7       phx 	if (genfb_attach(&sc->sc_gen, &ops) == 0) {
    168      1.2  macallan 
    169      1.7       phx 		/* now try to attach a DRM */
    170      1.7       phx 		config_found_ia(self, "drm", aux, pci_genfb_drm_print);
    171      1.7       phx 	}
    172      1.1  macallan }
    173      1.1  macallan 
    174      1.1  macallan static int
    175      1.2  macallan pci_genfb_drm_print(void *aux, const char *pnp)
    176      1.2  macallan {
    177      1.2  macallan 	if (pnp)
    178  1.9.4.1      yamt 		aprint_normal("drm at %s", pnp);
    179  1.9.4.1      yamt 	return (UNCONF);
    180      1.2  macallan }
    181      1.2  macallan 
    182      1.2  macallan 
    183      1.2  macallan static int
    184      1.1  macallan pci_genfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    185      1.1  macallan     struct lwp *l)
    186      1.1  macallan {
    187      1.1  macallan 	struct pci_genfb_softc *sc = v;
    188      1.1  macallan 
    189      1.1  macallan 	switch (cmd) {
    190      1.1  macallan 		case WSDISPLAYIO_GTYPE:
    191      1.1  macallan 			*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
    192      1.1  macallan 			return 0;
    193      1.1  macallan 
    194      1.1  macallan 		/* PCI config read/write passthrough. */
    195      1.1  macallan 		case PCI_IOC_CFGREAD:
    196      1.1  macallan 		case PCI_IOC_CFGWRITE:
    197      1.1  macallan 			return (pci_devioctl(sc->sc_pc, sc->sc_pcitag,
    198      1.1  macallan 			    cmd, data, flag, l));
    199      1.2  macallan 		case WSDISPLAYIO_SMODE:
    200      1.2  macallan 			{
    201      1.2  macallan 				int new_mode = *(int*)data, i;
    202      1.2  macallan 				if (new_mode == WSDISPLAYIO_MODE_EMUL) {
    203      1.2  macallan 					for (i = 0; i < 9; i++)
    204      1.2  macallan 						pci_conf_write(sc->sc_pc,
    205      1.2  macallan 						     sc->sc_pcitag,
    206      1.2  macallan 						     0x10 + (i << 2),
    207      1.2  macallan 						     sc->sc_bars[i]);
    208      1.2  macallan 				}
    209      1.2  macallan 			}
    210      1.2  macallan 			return 0;
    211      1.1  macallan 	}
    212      1.1  macallan 
    213      1.1  macallan 	return EPASSTHROUGH;
    214      1.1  macallan }
    215      1.1  macallan 
    216      1.1  macallan static paddr_t
    217      1.1  macallan pci_genfb_mmap(void *v, void *vs, off_t offset, int prot)
    218      1.1  macallan {
    219      1.1  macallan 	struct pci_genfb_softc *sc = v;
    220      1.1  macallan 	struct range *r;
    221      1.1  macallan 	int i;
    222      1.1  macallan 
    223      1.5  macallan 	if (offset == 0)
    224      1.5  macallan 		sc->sc_want_wsfb = 1;
    225      1.5  macallan 
    226      1.5  macallan 	/*
    227      1.5  macallan 	 * regular fb mapping at 0
    228      1.5  macallan 	 * since some Sun firmware likes to put PCI resources low enough
    229      1.5  macallan 	 * to collide with the wsfb mapping we only allow it after asking
    230      1.5  macallan 	 * for offset 0
    231      1.5  macallan 	 */
    232      1.2  macallan 	DPRINTF("%s: %08x limit %08x\n", __func__, (uint32_t)offset,
    233      1.2  macallan 	    (uint32_t)sc->sc_gen.sc_fbsize);
    234      1.5  macallan 	if ((offset >= 0) && (offset < sc->sc_gen.sc_fbsize) &&
    235      1.5  macallan 	    (sc->sc_want_wsfb == 1)) {
    236      1.1  macallan 
    237      1.1  macallan 		return bus_space_mmap(sc->sc_memt, sc->sc_gen.sc_fboffset,
    238      1.1  macallan 		   offset, prot, BUS_SPACE_MAP_LINEAR);
    239      1.1  macallan 	}
    240      1.1  macallan 
    241      1.1  macallan 	/*
    242      1.1  macallan 	 * restrict all other mappings to processes with superuser privileges
    243      1.1  macallan 	 * or the kernel itself
    244      1.1  macallan 	 */
    245  1.9.4.3      yamt 	if (kauth_authorize_generic(kauth_cred_get(), KAUTH_GENERIC_ISSUSER,
    246  1.9.4.3      yamt 	    NULL) != 0) {
    247  1.9.4.3      yamt 		aprint_normal_dev(&sc->sc_gen.sc_dev, "mmap() rejected.\n");
    248  1.9.4.3      yamt 		return -1;
    249      1.1  macallan 	}
    250      1.1  macallan 
    251      1.1  macallan #ifdef WSFB_FAKE_VGA_FB
    252      1.1  macallan 	if ((offset >= 0xa0000) && (offset < 0xbffff)) {
    253      1.1  macallan 
    254      1.1  macallan 		return bus_space_mmap(sc->sc_memt, sc->sc_gen.sc_fboffset,
    255      1.1  macallan 		   offset - 0xa0000, prot, BUS_SPACE_MAP_LINEAR);
    256      1.1  macallan 	}
    257      1.1  macallan #endif
    258      1.1  macallan 
    259      1.1  macallan 	/*
    260      1.1  macallan 	 * XXX this should be generalized, let's just
    261      1.1  macallan 	 * #define PCI_IOAREA_PADDR
    262      1.1  macallan 	 * #define PCI_IOAREA_OFFSET
    263      1.1  macallan 	 * #define PCI_IOAREA_SIZE
    264      1.1  macallan 	 * somewhere in a MD header and compile this code only if all are
    265      1.1  macallan 	 * present
    266      1.1  macallan 	 */
    267  1.9.4.5      yamt 	/*
    268  1.9.4.5      yamt 	 * no.
    269  1.9.4.5      yamt 	 * PCI_IOAREA_PADDR would be completely, utterly wrong and completely
    270  1.9.4.5      yamt 	 * useless for the following reasons:
    271  1.9.4.5      yamt 	 * - it's a bus address, not a physical address
    272  1.9.4.5      yamt 	 * - there's no guarantee it's the same for each host bridge
    273  1.9.4.5      yamt 	 * - it's already taken care of by the IO tag
    274  1.9.4.5      yamt 	 * PCI_IOAREA_OFFSET is the same as PCI_MAGIC_IO_RANGE
    275  1.9.4.5      yamt 	 * PCI_IOAREA_SIZE is also useless:
    276  1.9.4.5      yamt 	 * - many cards don't decode more than 16 bit IO anyway
    277  1.9.4.5      yamt 	 * - even machines with more than 64kB IO space try to keep everything
    278  1.9.4.5      yamt 	 *   within 64kB for the reason above
    279  1.9.4.5      yamt 	 * - IO ranges tend to be small so in most cases you can't cram enough
    280  1.9.4.5      yamt 	 *   cards into a single machine to exhaust 64kB IO space
    281  1.9.4.5      yamt 	 * - machines which need this tend to prefer memory space anyway
    282  1.9.4.5      yamt 	 * - the only use for this right now is to allow the Xserver to map
    283  1.9.4.5      yamt 	 *   VGA registers on macppc and a few other powerpc ports, shark uses
    284  1.9.4.5      yamt 	 *   a similar mechanism, and what they need is always within 64kB
    285  1.9.4.5      yamt 	 */
    286      1.6  macallan #ifdef PCI_MAGIC_IO_RANGE
    287      1.1  macallan 	/* allow to map our IO space */
    288      1.6  macallan 	if ((offset >= PCI_MAGIC_IO_RANGE) &&
    289      1.6  macallan 	    (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
    290      1.6  macallan 		return bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
    291      1.6  macallan 		    0, prot, BUS_SPACE_MAP_LINEAR);
    292      1.1  macallan 	}
    293      1.1  macallan #endif
    294      1.1  macallan 
    295      1.1  macallan 	/* allow to mmap() our BARs */
    296      1.2  macallan 	/* maybe the ROM BAR too? */
    297      1.1  macallan 	for (i = 0; i < sc->sc_ranges_used; i++) {
    298      1.1  macallan 
    299      1.1  macallan 		r = &sc->sc_ranges[i];
    300      1.1  macallan 		if ((offset >= r->offset) && (offset < (r->offset + r->size))) {
    301      1.1  macallan 			return bus_space_mmap(sc->sc_memt, offset, 0, prot,
    302      1.1  macallan 			    r->flags);
    303      1.1  macallan 		}
    304      1.1  macallan 	}
    305      1.1  macallan 
    306      1.1  macallan 	return -1;
    307      1.1  macallan }
    308  1.9.4.2      yamt 
    309  1.9.4.2      yamt int
    310  1.9.4.2      yamt pci_genfb_borrow(void *opaque, bus_addr_t addr, bus_space_handle_t *hdlp)
    311  1.9.4.2      yamt {
    312  1.9.4.2      yamt 	struct pci_genfb_softc *sc = opaque;
    313  1.9.4.2      yamt 
    314  1.9.4.2      yamt 	if (sc == NULL)
    315  1.9.4.2      yamt 		return 0;
    316  1.9.4.2      yamt 	if (!sc->sc_gen.sc_fboffset)
    317  1.9.4.2      yamt 		return 0;
    318  1.9.4.2      yamt 	if (sc->sc_gen.sc_fboffset != addr)
    319  1.9.4.2      yamt 		return 0;
    320  1.9.4.2      yamt 	*hdlp = sc->sc_memh;
    321  1.9.4.2      yamt 	return 1;
    322  1.9.4.2      yamt }
    323