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