Home | History | Annotate | Line # | Download | only in pci
vga_pci.c revision 1.38.6.2
      1  1.38.6.1       mjf /*	$NetBSD: vga_pci.c,v 1.38.6.2 2008/06/02 13:23:44 mjf Exp $	*/
      2       1.1  drochner 
      3       1.1  drochner /*
      4       1.1  drochner  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
      5       1.1  drochner  * All rights reserved.
      6       1.1  drochner  *
      7       1.1  drochner  * Author: Chris G. Demetriou
      8      1.26     perry  *
      9       1.1  drochner  * Permission to use, copy, modify and distribute this software and
     10       1.1  drochner  * its documentation is hereby granted, provided that both the copyright
     11       1.1  drochner  * notice and this permission notice appear in all copies of the
     12       1.1  drochner  * software, derivative works or modified versions, and any portions
     13       1.1  drochner  * thereof, and that both notices appear in supporting documentation.
     14      1.26     perry  *
     15      1.26     perry  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     16      1.26     perry  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     17       1.1  drochner  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     18      1.26     perry  *
     19       1.1  drochner  * Carnegie Mellon requests users of this software to return to
     20       1.1  drochner  *
     21       1.1  drochner  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     22       1.1  drochner  *  School of Computer Science
     23       1.1  drochner  *  Carnegie Mellon University
     24       1.1  drochner  *  Pittsburgh PA 15213-3890
     25       1.1  drochner  *
     26       1.1  drochner  * any improvements or extensions that they make and grant Carnegie the
     27       1.1  drochner  * rights to redistribute these changes.
     28       1.1  drochner  */
     29       1.8     lukem 
     30       1.8     lukem #include <sys/cdefs.h>
     31  1.38.6.1       mjf __KERNEL_RCSID(0, "$NetBSD: vga_pci.c,v 1.38.6.2 2008/06/02 13:23:44 mjf Exp $");
     32       1.1  drochner 
     33       1.1  drochner #include <sys/param.h>
     34       1.1  drochner #include <sys/systm.h>
     35       1.1  drochner #include <sys/kernel.h>
     36       1.1  drochner #include <sys/device.h>
     37       1.1  drochner #include <sys/malloc.h>
     38       1.1  drochner 
     39       1.1  drochner #include <dev/pci/pcireg.h>
     40       1.1  drochner #include <dev/pci/pcivar.h>
     41       1.1  drochner #include <dev/pci/pcidevs.h>
     42       1.6   thorpej #include <dev/pci/pciio.h>
     43       1.1  drochner 
     44       1.2  drochner #include <dev/ic/mc6845reg.h>
     45       1.2  drochner #include <dev/ic/pcdisplayvar.h>
     46       1.1  drochner #include <dev/ic/vgareg.h>
     47       1.1  drochner #include <dev/ic/vgavar.h>
     48       1.1  drochner #include <dev/pci/vga_pcivar.h>
     49       1.1  drochner 
     50       1.7   thorpej #include <dev/isa/isareg.h>	/* For legacy VGA address ranges */
     51       1.7   thorpej 
     52       1.1  drochner #include <dev/wscons/wsconsio.h>
     53       1.1  drochner #include <dev/wscons/wsdisplayvar.h>
     54       1.1  drochner 
     55      1.38     joerg #include "opt_vga.h"
     56      1.38     joerg 
     57      1.38     joerg #ifdef VGA_POST
     58      1.38     joerg #include <x86/vga_post.h>
     59      1.38     joerg #endif
     60      1.38     joerg 
     61       1.7   thorpej #define	NBARS		6	/* number of PCI BARs */
     62       1.7   thorpej 
     63       1.7   thorpej struct vga_bar {
     64       1.7   thorpej 	bus_addr_t vb_base;
     65       1.7   thorpej 	bus_size_t vb_size;
     66       1.7   thorpej 	pcireg_t vb_type;
     67       1.7   thorpej 	int vb_flags;
     68       1.7   thorpej };
     69       1.7   thorpej 
     70       1.1  drochner struct vga_pci_softc {
     71       1.5   thorpej 	struct vga_softc sc_vga;
     72       1.5   thorpej 
     73       1.6   thorpej 	pci_chipset_tag_t sc_pc;
     74       1.5   thorpej 	pcitag_t sc_pcitag;
     75       1.7   thorpej 
     76       1.7   thorpej 	struct vga_bar sc_bars[NBARS];
     77       1.7   thorpej 	struct vga_bar sc_rom;
     78      1.37  jmcneill 
     79      1.38     joerg #ifdef VGA_POST
     80      1.38     joerg 	struct vga_post *sc_posth;
     81      1.38     joerg #endif
     82  1.38.6.2       mjf 
     83  1.38.6.2       mjf 	struct pci_attach_args sc_paa;
     84       1.1  drochner };
     85       1.1  drochner 
     86      1.27   thorpej static int	vga_pci_match(struct device *, struct cfdata *, void *);
     87      1.27   thorpej static void	vga_pci_attach(struct device *, struct device *, void *);
     88  1.38.6.2       mjf static int	vga_pci_rescan(struct device *, const char *, const int *);
     89      1.27   thorpej static int	vga_pci_lookup_quirks(struct pci_attach_args *);
     90  1.38.6.1       mjf static bool	vga_pci_resume(device_t dv PMF_FN_PROTO);
     91       1.1  drochner 
     92  1.38.6.2       mjf CFATTACH_DECL2_NEW(vga_pci, sizeof(struct vga_pci_softc),
     93  1.38.6.2       mjf     vga_pci_match, vga_pci_attach, NULL, NULL, vga_pci_rescan, NULL);
     94       1.1  drochner 
     95      1.31  christos static int	vga_pci_ioctl(void *, u_long, void *, int, struct lwp *);
     96      1.27   thorpej static paddr_t	vga_pci_mmap(void *, off_t, int);
     97       1.6   thorpej 
     98      1.27   thorpej static const struct vga_funcs vga_pci_funcs = {
     99       1.6   thorpej 	vga_pci_ioctl,
    100       1.6   thorpej 	vga_pci_mmap,
    101       1.6   thorpej };
    102       1.6   thorpej 
    103      1.15  drochner static const struct {
    104      1.15  drochner 	int id;
    105      1.15  drochner 	int quirks;
    106      1.15  drochner } vga_pci_quirks[] = {
    107      1.21      salo 	{PCI_ID_CODE(PCI_VENDOR_SILMOTION, PCI_PRODUCT_SILMOTION_SM712),
    108      1.17  drochner 	 VGA_QUIRK_NOFASTSCROLL},
    109      1.15  drochner };
    110      1.16  drochner 
    111      1.16  drochner static const struct {
    112      1.16  drochner 	int vid;
    113      1.16  drochner 	int quirks;
    114      1.16  drochner } vga_pci_vquirks[] = {
    115      1.16  drochner 	{PCI_VENDOR_ATI, VGA_QUIRK_ONEFONT},
    116      1.16  drochner };
    117      1.15  drochner 
    118      1.15  drochner static int
    119      1.27   thorpej vga_pci_lookup_quirks(struct pci_attach_args *pa)
    120      1.15  drochner {
    121      1.15  drochner 	int i;
    122      1.15  drochner 
    123      1.15  drochner 	for (i = 0; i < sizeof(vga_pci_quirks) / sizeof (vga_pci_quirks[0]);
    124      1.15  drochner 	     i++) {
    125      1.15  drochner 		if (vga_pci_quirks[i].id == pa->pa_id)
    126      1.15  drochner 			return (vga_pci_quirks[i].quirks);
    127      1.16  drochner 	}
    128      1.16  drochner 	for (i = 0; i < sizeof(vga_pci_vquirks) / sizeof (vga_pci_vquirks[0]);
    129      1.16  drochner 	     i++) {
    130      1.16  drochner 		if (vga_pci_vquirks[i].vid == PCI_VENDOR(pa->pa_id))
    131      1.16  drochner 			return (vga_pci_vquirks[i].quirks);
    132      1.15  drochner 	}
    133      1.15  drochner 	return (0);
    134      1.15  drochner }
    135      1.15  drochner 
    136      1.27   thorpej static int
    137      1.30  christos vga_pci_match(struct device *parent, struct cfdata *match,
    138      1.29  christos     void *aux)
    139       1.1  drochner {
    140       1.1  drochner 	struct pci_attach_args *pa = aux;
    141       1.1  drochner 	int potential;
    142       1.1  drochner 
    143       1.1  drochner 	potential = 0;
    144       1.1  drochner 
    145       1.1  drochner 	/*
    146       1.1  drochner 	 * If it's prehistoric/vga or display/vga, we might match.
    147      1.25       wiz 	 * For the console device, this is just a sanity check.
    148       1.1  drochner 	 */
    149       1.1  drochner 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_PREHISTORIC &&
    150       1.1  drochner 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_PREHISTORIC_VGA)
    151       1.1  drochner 		potential = 1;
    152       1.1  drochner 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY &&
    153       1.1  drochner 	     PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_DISPLAY_VGA)
    154       1.1  drochner 		potential = 1;
    155       1.1  drochner 
    156       1.1  drochner 	if (!potential)
    157       1.1  drochner 		return (0);
    158       1.1  drochner 
    159       1.1  drochner 	/* check whether it is disabled by firmware */
    160       1.1  drochner 	if ((pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG)
    161       1.1  drochner 	    & (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE))
    162       1.1  drochner 	    != (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE))
    163       1.1  drochner 		return (0);
    164       1.1  drochner 
    165       1.1  drochner 	/* If it's the console, we have a winner! */
    166       1.1  drochner 	if (vga_is_console(pa->pa_iot, WSDISPLAY_TYPE_PCIVGA))
    167       1.1  drochner 		return (1);
    168       1.1  drochner 
    169       1.1  drochner 	/*
    170       1.1  drochner 	 * If we might match, make sure that the card actually looks OK.
    171       1.1  drochner 	 */
    172       1.1  drochner 	if (!vga_common_probe(pa->pa_iot, pa->pa_memt))
    173       1.1  drochner 		return (0);
    174       1.1  drochner 
    175       1.1  drochner 	return (1);
    176       1.1  drochner }
    177       1.1  drochner 
    178      1.27   thorpej static void
    179      1.30  christos vga_pci_attach(struct device *parent, struct device *self, void *aux)
    180       1.1  drochner {
    181  1.38.6.1       mjf 	struct vga_pci_softc *psc = device_private(self);
    182       1.5   thorpej 	struct vga_softc *sc = &psc->sc_vga;
    183       1.1  drochner 	struct pci_attach_args *pa = aux;
    184       1.1  drochner 	char devinfo[256];
    185       1.7   thorpej 	int bar, reg;
    186       1.1  drochner 
    187  1.38.6.1       mjf 	sc->sc_dev = self;
    188       1.6   thorpej 	psc->sc_pc = pa->pa_pc;
    189       1.5   thorpej 	psc->sc_pcitag = pa->pa_tag;
    190  1.38.6.2       mjf 	psc->sc_paa = *pa;
    191       1.1  drochner 
    192      1.24    itojun 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    193      1.34  jmcneill 	aprint_naive("\n");
    194      1.34  jmcneill 	aprint_normal(": %s (rev. 0x%02x)\n", devinfo,
    195       1.1  drochner 	    PCI_REVISION(pa->pa_class));
    196       1.1  drochner 
    197       1.7   thorpej 	/*
    198       1.7   thorpej 	 * Gather info about all the BARs.  These are used to allow
    199       1.7   thorpej 	 * the X server to map the VGA device.
    200       1.7   thorpej 	 */
    201       1.7   thorpej 	for (bar = 0; bar < NBARS; bar++) {
    202       1.7   thorpej 		reg = PCI_MAPREG_START + (bar * 4);
    203      1.11  drochner 		if (!pci_mapreg_probe(psc->sc_pc, psc->sc_pcitag, reg,
    204      1.11  drochner 				      &psc->sc_bars[bar].vb_type)) {
    205      1.11  drochner 			/* there is no valid mapping register */
    206      1.11  drochner 			continue;
    207      1.11  drochner 		}
    208       1.7   thorpej 		if (PCI_MAPREG_TYPE(psc->sc_bars[bar].vb_type) ==
    209       1.7   thorpej 		    PCI_MAPREG_TYPE_IO) {
    210       1.7   thorpej 			/* Don't bother fetching I/O BARs. */
    211       1.7   thorpej 			continue;
    212       1.7   thorpej 		}
    213      1.33    martin #ifndef __LP64__
    214       1.7   thorpej 		if (PCI_MAPREG_MEM_TYPE(psc->sc_bars[bar].vb_type) ==
    215       1.7   thorpej 		    PCI_MAPREG_MEM_TYPE_64BIT) {
    216       1.7   thorpej 			/* XXX */
    217  1.38.6.1       mjf 			aprint_error_dev(self,
    218  1.38.6.1       mjf 			    "WARNING: ignoring 64-bit BAR @ 0x%02x\n", reg);
    219      1.11  drochner 			bar++;
    220       1.7   thorpej 			continue;
    221       1.7   thorpej 		}
    222      1.33    martin #endif
    223      1.11  drochner 		if (pci_mapreg_info(psc->sc_pc, psc->sc_pcitag, reg,
    224       1.7   thorpej 		     psc->sc_bars[bar].vb_type,
    225       1.7   thorpej 		     &psc->sc_bars[bar].vb_base,
    226       1.7   thorpej 		     &psc->sc_bars[bar].vb_size,
    227      1.11  drochner 		     &psc->sc_bars[bar].vb_flags))
    228  1.38.6.1       mjf 			aprint_error_dev(self,
    229  1.38.6.1       mjf 			    "WARNING: strange BAR @ 0x%02x\n", reg);
    230       1.7   thorpej 	}
    231       1.7   thorpej 
    232       1.7   thorpej 	/* XXX Expansion ROM? */
    233       1.7   thorpej 
    234       1.5   thorpej 	vga_common_attach(sc, pa->pa_iot, pa->pa_memt, WSDISPLAY_TYPE_PCIVGA,
    235      1.15  drochner 			  vga_pci_lookup_quirks(pa), &vga_pci_funcs);
    236      1.32  drochner 
    237      1.38     joerg #ifdef VGA_POST
    238      1.38     joerg 	psc->sc_posth = vga_post_init(pa->pa_bus, pa->pa_device, pa->pa_function);
    239      1.38     joerg 	if (psc->sc_posth == NULL)
    240      1.38     joerg 		aprint_error_dev(self, "WARNING: could not prepare POST handler\n");
    241      1.38     joerg #endif
    242      1.38     joerg 
    243      1.37  jmcneill 	/*
    244      1.37  jmcneill 	 * XXX Do not use the generic PCI framework for now as
    245      1.37  jmcneill 	 * XXX it would power down the device when the console
    246      1.37  jmcneill 	 * XXX is still using it.
    247      1.37  jmcneill 	 */
    248      1.37  jmcneill 	if (!pmf_device_register(self, NULL, vga_pci_resume))
    249      1.37  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    250      1.32  drochner 	config_found_ia(self, "drm", aux, vga_drm_print);
    251       1.1  drochner }
    252       1.1  drochner 
    253  1.38.6.2       mjf static int
    254  1.38.6.2       mjf vga_pci_rescan(struct device *self, const char *ifattr, const int *locators)
    255  1.38.6.2       mjf {
    256  1.38.6.2       mjf 	struct vga_pci_softc *psc = device_private(self);
    257  1.38.6.2       mjf 
    258  1.38.6.2       mjf 	config_found_ia(self, "drm", &psc->sc_paa, vga_drm_print);
    259  1.38.6.2       mjf 
    260  1.38.6.2       mjf 	return 0;
    261  1.38.6.2       mjf }
    262  1.38.6.2       mjf 
    263      1.37  jmcneill static bool
    264  1.38.6.1       mjf vga_pci_resume(device_t dv PMF_FN_ARGS)
    265      1.37  jmcneill {
    266      1.37  jmcneill 	struct vga_pci_softc *sc = device_private(dv);
    267      1.37  jmcneill 
    268      1.37  jmcneill 	vga_resume(&sc->sc_vga);
    269      1.37  jmcneill 
    270      1.38     joerg #ifdef VGA_POST
    271      1.38     joerg 	if (sc->sc_posth != NULL)
    272      1.38     joerg 		vga_post_call(sc->sc_posth);
    273      1.38     joerg #endif
    274      1.38     joerg 
    275      1.37  jmcneill 	return true;
    276      1.37  jmcneill }
    277      1.37  jmcneill 
    278       1.1  drochner int
    279      1.26     perry vga_pci_cnattach(bus_space_tag_t iot, bus_space_tag_t memt,
    280      1.30  christos     pci_chipset_tag_t pc, int bus, int device,
    281      1.30  christos     int function)
    282       1.1  drochner {
    283      1.14  junyoung 
    284       1.1  drochner 	return (vga_cnattach(iot, memt, WSDISPLAY_TYPE_PCIVGA, 0));
    285       1.6   thorpej }
    286       1.6   thorpej 
    287      1.32  drochner int
    288      1.32  drochner vga_drm_print(void *aux, const char *pnp)
    289      1.32  drochner {
    290      1.32  drochner 	if (pnp)
    291  1.38.6.2       mjf 		aprint_normal("drm at %s", pnp);
    292  1.38.6.2       mjf 	return (UNCONF);
    293      1.32  drochner }
    294      1.32  drochner 
    295      1.32  drochner 
    296      1.27   thorpej static int
    297      1.31  christos vga_pci_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
    298       1.6   thorpej {
    299       1.6   thorpej 	struct vga_config *vc = v;
    300       1.6   thorpej 	struct vga_pci_softc *psc = (void *) vc->softc;
    301       1.6   thorpej 
    302       1.6   thorpej 	switch (cmd) {
    303       1.6   thorpej 	/* PCI config read/write passthrough. */
    304       1.6   thorpej 	case PCI_IOC_CFGREAD:
    305       1.6   thorpej 	case PCI_IOC_CFGWRITE:
    306       1.6   thorpej 		return (pci_devioctl(psc->sc_pc, psc->sc_pcitag,
    307      1.28  christos 		    cmd, data, flag, l));
    308       1.6   thorpej 
    309       1.6   thorpej 	default:
    310      1.10    atatat 		return (EPASSTHROUGH);
    311       1.6   thorpej 	}
    312       1.6   thorpej }
    313       1.6   thorpej 
    314      1.27   thorpej static paddr_t
    315       1.6   thorpej vga_pci_mmap(void *v, off_t offset, int prot)
    316       1.6   thorpej {
    317       1.7   thorpej 	struct vga_config *vc = v;
    318       1.7   thorpej 	struct vga_pci_softc *psc = (void *) vc->softc;
    319       1.7   thorpej 	struct vga_bar *vb;
    320       1.7   thorpej 	int bar;
    321       1.7   thorpej 
    322       1.7   thorpej 	for (bar = 0; bar < NBARS; bar++) {
    323       1.7   thorpej 		vb = &psc->sc_bars[bar];
    324       1.7   thorpej 		if (vb->vb_size == 0)
    325       1.7   thorpej 			continue;
    326       1.7   thorpej 		if (offset >= vb->vb_base &&
    327       1.7   thorpej 		    offset < (vb->vb_base + vb->vb_size)) {
    328       1.7   thorpej 			/* XXX This the right thing to do with flags? */
    329       1.7   thorpej 			return (bus_space_mmap(vc->hdl.vh_memt, vb->vb_base,
    330       1.7   thorpej 			    (offset - vb->vb_base), prot, vb->vb_flags));
    331       1.7   thorpej 		}
    332       1.7   thorpej 	}
    333       1.7   thorpej 
    334       1.7   thorpej 	/* XXX Expansion ROM? */
    335       1.7   thorpej 
    336       1.7   thorpej 	/*
    337       1.7   thorpej 	 * Allow mmap access to the legacy ISA hole.  This is where
    338       1.7   thorpej 	 * the legacy video BIOS will be located, and also where
    339       1.7   thorpej 	 * the legacy VGA display buffer is located.
    340       1.7   thorpej 	 *
    341       1.7   thorpej 	 * XXX Security implications, here?
    342       1.7   thorpej 	 */
    343       1.7   thorpej 	if (offset >= IOM_BEGIN && offset < IOM_END)
    344       1.7   thorpej 		return (bus_space_mmap(vc->hdl.vh_memt, IOM_BEGIN,
    345       1.7   thorpej 		    (offset - IOM_BEGIN), prot, 0));
    346       1.6   thorpej 
    347       1.7   thorpej 	/* Range not found. */
    348       1.6   thorpej 	return (-1);
    349       1.1  drochner }
    350