Home | History | Annotate | Line # | Download | only in pci
vga_pci.c revision 1.4.2.1
      1 /* $NetBSD: vga_pci.c,v 1.4.2.1 2001/09/21 22:36:03 nathanw Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
      5  * All rights reserved.
      6  *
      7  * Author: Chris G. Demetriou
      8  *
      9  * Permission to use, copy, modify and distribute this software and
     10  * its documentation is hereby granted, provided that both the copyright
     11  * notice and this permission notice appear in all copies of the
     12  * software, derivative works or modified versions, and any portions
     13  * thereof, and that both notices appear in supporting documentation.
     14  *
     15  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     16  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     17  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     18  *
     19  * Carnegie Mellon requests users of this software to return to
     20  *
     21  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     22  *  School of Computer Science
     23  *  Carnegie Mellon University
     24  *  Pittsburgh PA 15213-3890
     25  *
     26  * any improvements or extensions that they make and grant Carnegie the
     27  * rights to redistribute these changes.
     28  */
     29 
     30 #include <sys/param.h>
     31 #include <sys/systm.h>
     32 #include <sys/kernel.h>
     33 #include <sys/device.h>
     34 #include <sys/malloc.h>
     35 
     36 #include <dev/pci/pcireg.h>
     37 #include <dev/pci/pcivar.h>
     38 #include <dev/pci/pcidevs.h>
     39 #include <dev/pci/pciio.h>
     40 
     41 #include <dev/ic/mc6845reg.h>
     42 #include <dev/ic/pcdisplayvar.h>
     43 #include <dev/ic/vgareg.h>
     44 #include <dev/ic/vgavar.h>
     45 #include <dev/pci/vga_pcivar.h>
     46 
     47 #include <dev/isa/isareg.h>	/* For legacy VGA address ranges */
     48 
     49 #include <dev/wscons/wsconsio.h>
     50 #include <dev/wscons/wsdisplayvar.h>
     51 
     52 #define	NBARS		6	/* number of PCI BARs */
     53 
     54 struct vga_bar {
     55 	bus_addr_t vb_base;
     56 	bus_size_t vb_size;
     57 	pcireg_t vb_type;
     58 	int vb_flags;
     59 };
     60 
     61 struct vga_pci_softc {
     62 	struct vga_softc sc_vga;
     63 
     64 	pci_chipset_tag_t sc_pc;
     65 	pcitag_t sc_pcitag;
     66 
     67 	struct vga_bar sc_bars[NBARS];
     68 	struct vga_bar sc_rom;
     69 };
     70 
     71 int	vga_pci_match __P((struct device *, struct cfdata *, void *));
     72 void	vga_pci_attach __P((struct device *, struct device *, void *));
     73 
     74 struct cfattach vga_pci_ca = {
     75 	sizeof(struct vga_pci_softc), vga_pci_match, vga_pci_attach,
     76 };
     77 
     78 int	vga_pci_ioctl(void *, u_long, caddr_t, int, struct proc *);
     79 paddr_t	vga_pci_mmap(void *, off_t, int);
     80 
     81 const struct vga_funcs vga_pci_funcs = {
     82 	vga_pci_ioctl,
     83 	vga_pci_mmap,
     84 };
     85 
     86 int
     87 vga_pci_match(parent, match, aux)
     88 	struct device *parent;
     89 	struct cfdata *match;
     90 	void *aux;
     91 {
     92 	struct pci_attach_args *pa = aux;
     93 	int potential;
     94 
     95 	potential = 0;
     96 
     97 	/*
     98 	 * If it's prehistoric/vga or display/vga, we might match.
     99 	 * For the console device, this is jut a sanity check.
    100 	 */
    101 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_PREHISTORIC &&
    102 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_PREHISTORIC_VGA)
    103 		potential = 1;
    104 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY &&
    105 	     PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_DISPLAY_VGA)
    106 		potential = 1;
    107 
    108 	if (!potential)
    109 		return (0);
    110 
    111 	/* check whether it is disabled by firmware */
    112 	if ((pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG)
    113 	    & (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE))
    114 	    != (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE))
    115 		return (0);
    116 
    117 	/* If it's the console, we have a winner! */
    118 	if (vga_is_console(pa->pa_iot, WSDISPLAY_TYPE_PCIVGA))
    119 		return (1);
    120 
    121 	/*
    122 	 * If we might match, make sure that the card actually looks OK.
    123 	 */
    124 	if (!vga_common_probe(pa->pa_iot, pa->pa_memt))
    125 		return (0);
    126 
    127 	return (1);
    128 }
    129 
    130 void
    131 vga_pci_attach(parent, self, aux)
    132 	struct device *parent, *self;
    133 	void *aux;
    134 {
    135 	struct vga_pci_softc *psc = (void *) self;
    136 	struct vga_softc *sc = &psc->sc_vga;
    137 	struct pci_attach_args *pa = aux;
    138 	char devinfo[256];
    139 	int bar, reg;
    140 
    141 	psc->sc_pc = pa->pa_pc;
    142 	psc->sc_pcitag = pa->pa_tag;
    143 
    144 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
    145 	printf(": %s (rev. 0x%02x)\n", devinfo,
    146 	    PCI_REVISION(pa->pa_class));
    147 
    148 	/*
    149 	 * Gather info about all the BARs.  These are used to allow
    150 	 * the X server to map the VGA device.
    151 	 */
    152 	for (bar = 0; bar < NBARS; bar++) {
    153 		reg = PCI_MAPREG_START + (bar * 4);
    154 		psc->sc_bars[bar].vb_type = pci_mapreg_type(psc->sc_pc,
    155 		    psc->sc_pcitag, reg);
    156 		if (PCI_MAPREG_TYPE(psc->sc_bars[bar].vb_type) ==
    157 		    PCI_MAPREG_TYPE_IO) {
    158 			/* Don't bother fetching I/O BARs. */
    159 			continue;
    160 		}
    161 		if (PCI_MAPREG_MEM_TYPE(psc->sc_bars[bar].vb_type) ==
    162 		    PCI_MAPREG_MEM_TYPE_64BIT) {
    163 			/* XXX */
    164 			printf("%s: WARNING: ignoring 64-bit BAR @ 0x%02x\n",
    165 			    sc->sc_dev.dv_xname, reg);
    166 			continue;
    167 		}
    168 		/* Ignore errors (unimplemented BARs). */
    169 		(void) pci_mapreg_info(psc->sc_pc, psc->sc_pcitag, reg,
    170 		     psc->sc_bars[bar].vb_type,
    171 		     &psc->sc_bars[bar].vb_base,
    172 		     &psc->sc_bars[bar].vb_size,
    173 		     &psc->sc_bars[bar].vb_flags);
    174 	}
    175 
    176 	/* XXX Expansion ROM? */
    177 
    178 	vga_common_attach(sc, pa->pa_iot, pa->pa_memt, WSDISPLAY_TYPE_PCIVGA,
    179 	    &vga_pci_funcs);
    180 }
    181 
    182 int
    183 vga_pci_cnattach(iot, memt, pc, bus, device, function)
    184 	bus_space_tag_t iot, memt;
    185 	pci_chipset_tag_t pc;
    186 	int bus, device, function;
    187 {
    188 	return (vga_cnattach(iot, memt, WSDISPLAY_TYPE_PCIVGA, 0));
    189 }
    190 
    191 int
    192 vga_pci_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
    193 {
    194 	struct vga_config *vc = v;
    195 	struct vga_pci_softc *psc = (void *) vc->softc;
    196 
    197 	switch (cmd) {
    198 	/* PCI config read/write passthrough. */
    199 	case PCI_IOC_CFGREAD:
    200 	case PCI_IOC_CFGWRITE:
    201 		return (pci_devioctl(psc->sc_pc, psc->sc_pcitag,
    202 		    cmd, data, flag, p));
    203 
    204 	default:
    205 		return (ENOTTY);
    206 	}
    207 }
    208 
    209 paddr_t
    210 vga_pci_mmap(void *v, off_t offset, int prot)
    211 {
    212 	struct vga_config *vc = v;
    213 	struct vga_pci_softc *psc = (void *) vc->softc;
    214 	struct vga_bar *vb;
    215 	int bar;
    216 
    217 	for (bar = 0; bar < NBARS; bar++) {
    218 		vb = &psc->sc_bars[bar];
    219 		if (vb->vb_size == 0)
    220 			continue;
    221 		if (offset >= vb->vb_base &&
    222 		    offset < (vb->vb_base + vb->vb_size)) {
    223 			/* XXX This the right thing to do with flags? */
    224 			return (bus_space_mmap(vc->hdl.vh_memt, vb->vb_base,
    225 			    (offset - vb->vb_base), prot, vb->vb_flags));
    226 		}
    227 	}
    228 
    229 	/* XXX Expansion ROM? */
    230 
    231 	/*
    232 	 * Allow mmap access to the legacy ISA hole.  This is where
    233 	 * the legacy video BIOS will be located, and also where
    234 	 * the legacy VGA display buffer is located.
    235 	 *
    236 	 * XXX Security implications, here?
    237 	 */
    238 	if (offset >= IOM_BEGIN && offset < IOM_END)
    239 		return (bus_space_mmap(vc->hdl.vh_memt, IOM_BEGIN,
    240 		    (offset - IOM_BEGIN), prot, 0));
    241 
    242 	/* Range not found. */
    243 	return (-1);
    244 }
    245