Home | History | Annotate | Line # | Download | only in pci
vga_pci.c revision 1.32.8.2
      1  1.32.8.2  jmcneill /*	$NetBSD: vga_pci.c,v 1.32.8.2 2007/09/03 16:48:24 jmcneill 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.32.8.2  jmcneill __KERNEL_RCSID(0, "$NetBSD: vga_pci.c,v 1.32.8.2 2007/09/03 16:48:24 jmcneill 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.7   thorpej #define	NBARS		6	/* number of PCI BARs */
     56       1.7   thorpej 
     57       1.7   thorpej struct vga_bar {
     58       1.7   thorpej 	bus_addr_t vb_base;
     59       1.7   thorpej 	bus_size_t vb_size;
     60       1.7   thorpej 	pcireg_t vb_type;
     61       1.7   thorpej 	int vb_flags;
     62       1.7   thorpej };
     63       1.7   thorpej 
     64       1.1  drochner struct vga_pci_softc {
     65       1.5   thorpej 	struct vga_softc sc_vga;
     66       1.5   thorpej 
     67       1.6   thorpej 	pci_chipset_tag_t sc_pc;
     68       1.5   thorpej 	pcitag_t sc_pcitag;
     69       1.7   thorpej 
     70       1.7   thorpej 	struct vga_bar sc_bars[NBARS];
     71       1.7   thorpej 	struct vga_bar sc_rom;
     72  1.32.8.1  jmcneill 
     73  1.32.8.1  jmcneill 	struct pci_conf_state sc_pciconf;
     74       1.1  drochner };
     75       1.1  drochner 
     76      1.27   thorpej static int	vga_pci_match(struct device *, struct cfdata *, void *);
     77      1.27   thorpej static void	vga_pci_attach(struct device *, struct device *, void *);
     78  1.32.8.1  jmcneill static pnp_status_t vga_pci_power(device_t, pnp_request_t, void *);
     79      1.27   thorpej static int	vga_pci_lookup_quirks(struct pci_attach_args *);
     80       1.1  drochner 
     81      1.19   thorpej CFATTACH_DECL(vga_pci, sizeof(struct vga_pci_softc),
     82      1.20   thorpej     vga_pci_match, vga_pci_attach, NULL, NULL);
     83       1.1  drochner 
     84      1.31  christos static int	vga_pci_ioctl(void *, u_long, void *, int, struct lwp *);
     85      1.27   thorpej static paddr_t	vga_pci_mmap(void *, off_t, int);
     86       1.6   thorpej 
     87      1.27   thorpej static const struct vga_funcs vga_pci_funcs = {
     88       1.6   thorpej 	vga_pci_ioctl,
     89       1.6   thorpej 	vga_pci_mmap,
     90       1.6   thorpej };
     91       1.6   thorpej 
     92      1.15  drochner static const struct {
     93      1.15  drochner 	int id;
     94      1.15  drochner 	int quirks;
     95      1.15  drochner } vga_pci_quirks[] = {
     96      1.21      salo 	{PCI_ID_CODE(PCI_VENDOR_SILMOTION, PCI_PRODUCT_SILMOTION_SM712),
     97      1.17  drochner 	 VGA_QUIRK_NOFASTSCROLL},
     98      1.15  drochner };
     99      1.16  drochner 
    100      1.16  drochner static const struct {
    101      1.16  drochner 	int vid;
    102      1.16  drochner 	int quirks;
    103      1.16  drochner } vga_pci_vquirks[] = {
    104      1.16  drochner 	{PCI_VENDOR_ATI, VGA_QUIRK_ONEFONT},
    105      1.16  drochner };
    106      1.15  drochner 
    107      1.15  drochner static int
    108      1.27   thorpej vga_pci_lookup_quirks(struct pci_attach_args *pa)
    109      1.15  drochner {
    110      1.15  drochner 	int i;
    111      1.15  drochner 
    112      1.15  drochner 	for (i = 0; i < sizeof(vga_pci_quirks) / sizeof (vga_pci_quirks[0]);
    113      1.15  drochner 	     i++) {
    114      1.15  drochner 		if (vga_pci_quirks[i].id == pa->pa_id)
    115      1.15  drochner 			return (vga_pci_quirks[i].quirks);
    116      1.16  drochner 	}
    117      1.16  drochner 	for (i = 0; i < sizeof(vga_pci_vquirks) / sizeof (vga_pci_vquirks[0]);
    118      1.16  drochner 	     i++) {
    119      1.16  drochner 		if (vga_pci_vquirks[i].vid == PCI_VENDOR(pa->pa_id))
    120      1.16  drochner 			return (vga_pci_vquirks[i].quirks);
    121      1.15  drochner 	}
    122      1.15  drochner 	return (0);
    123      1.15  drochner }
    124      1.15  drochner 
    125      1.27   thorpej static int
    126      1.30  christos vga_pci_match(struct device *parent, struct cfdata *match,
    127      1.29  christos     void *aux)
    128       1.1  drochner {
    129       1.1  drochner 	struct pci_attach_args *pa = aux;
    130       1.1  drochner 	int potential;
    131       1.1  drochner 
    132       1.1  drochner 	potential = 0;
    133       1.1  drochner 
    134       1.1  drochner 	/*
    135       1.1  drochner 	 * If it's prehistoric/vga or display/vga, we might match.
    136      1.25       wiz 	 * For the console device, this is just a sanity check.
    137       1.1  drochner 	 */
    138       1.1  drochner 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_PREHISTORIC &&
    139       1.1  drochner 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_PREHISTORIC_VGA)
    140       1.1  drochner 		potential = 1;
    141       1.1  drochner 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY &&
    142       1.1  drochner 	     PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_DISPLAY_VGA)
    143       1.1  drochner 		potential = 1;
    144       1.1  drochner 
    145       1.1  drochner 	if (!potential)
    146       1.1  drochner 		return (0);
    147       1.1  drochner 
    148       1.1  drochner 	/* check whether it is disabled by firmware */
    149       1.1  drochner 	if ((pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG)
    150       1.1  drochner 	    & (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE))
    151       1.1  drochner 	    != (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE))
    152       1.1  drochner 		return (0);
    153       1.1  drochner 
    154       1.1  drochner 	/* If it's the console, we have a winner! */
    155       1.1  drochner 	if (vga_is_console(pa->pa_iot, WSDISPLAY_TYPE_PCIVGA))
    156       1.1  drochner 		return (1);
    157       1.1  drochner 
    158       1.1  drochner 	/*
    159       1.1  drochner 	 * If we might match, make sure that the card actually looks OK.
    160       1.1  drochner 	 */
    161       1.1  drochner 	if (!vga_common_probe(pa->pa_iot, pa->pa_memt))
    162       1.1  drochner 		return (0);
    163       1.1  drochner 
    164       1.1  drochner 	return (1);
    165       1.1  drochner }
    166       1.1  drochner 
    167      1.27   thorpej static void
    168      1.30  christos vga_pci_attach(struct device *parent, struct device *self, void *aux)
    169       1.1  drochner {
    170       1.5   thorpej 	struct vga_pci_softc *psc = (void *) self;
    171       1.5   thorpej 	struct vga_softc *sc = &psc->sc_vga;
    172       1.1  drochner 	struct pci_attach_args *pa = aux;
    173  1.32.8.1  jmcneill 	pnp_status_t status;
    174       1.1  drochner 	char devinfo[256];
    175       1.7   thorpej 	int bar, reg;
    176       1.1  drochner 
    177       1.6   thorpej 	psc->sc_pc = pa->pa_pc;
    178       1.5   thorpej 	psc->sc_pcitag = pa->pa_tag;
    179       1.1  drochner 
    180      1.24    itojun 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    181       1.1  drochner 	printf(": %s (rev. 0x%02x)\n", devinfo,
    182       1.1  drochner 	    PCI_REVISION(pa->pa_class));
    183       1.1  drochner 
    184       1.7   thorpej 	/*
    185       1.7   thorpej 	 * Gather info about all the BARs.  These are used to allow
    186       1.7   thorpej 	 * the X server to map the VGA device.
    187       1.7   thorpej 	 */
    188       1.7   thorpej 	for (bar = 0; bar < NBARS; bar++) {
    189       1.7   thorpej 		reg = PCI_MAPREG_START + (bar * 4);
    190      1.11  drochner 		if (!pci_mapreg_probe(psc->sc_pc, psc->sc_pcitag, reg,
    191      1.11  drochner 				      &psc->sc_bars[bar].vb_type)) {
    192      1.11  drochner 			/* there is no valid mapping register */
    193      1.11  drochner 			continue;
    194      1.11  drochner 		}
    195       1.7   thorpej 		if (PCI_MAPREG_TYPE(psc->sc_bars[bar].vb_type) ==
    196       1.7   thorpej 		    PCI_MAPREG_TYPE_IO) {
    197       1.7   thorpej 			/* Don't bother fetching I/O BARs. */
    198       1.7   thorpej 			continue;
    199       1.7   thorpej 		}
    200  1.32.8.2  jmcneill #ifndef __LP64__
    201       1.7   thorpej 		if (PCI_MAPREG_MEM_TYPE(psc->sc_bars[bar].vb_type) ==
    202       1.7   thorpej 		    PCI_MAPREG_MEM_TYPE_64BIT) {
    203       1.7   thorpej 			/* XXX */
    204       1.7   thorpej 			printf("%s: WARNING: ignoring 64-bit BAR @ 0x%02x\n",
    205       1.7   thorpej 			    sc->sc_dev.dv_xname, reg);
    206      1.11  drochner 			bar++;
    207       1.7   thorpej 			continue;
    208       1.7   thorpej 		}
    209  1.32.8.2  jmcneill #endif
    210      1.11  drochner 		if (pci_mapreg_info(psc->sc_pc, psc->sc_pcitag, reg,
    211       1.7   thorpej 		     psc->sc_bars[bar].vb_type,
    212       1.7   thorpej 		     &psc->sc_bars[bar].vb_base,
    213       1.7   thorpej 		     &psc->sc_bars[bar].vb_size,
    214      1.11  drochner 		     &psc->sc_bars[bar].vb_flags))
    215      1.11  drochner 			printf("%s: WARNING: strange BAR @ 0x%02x\n",
    216      1.11  drochner 			       sc->sc_dev.dv_xname, reg);
    217       1.7   thorpej 	}
    218       1.7   thorpej 
    219       1.7   thorpej 	/* XXX Expansion ROM? */
    220       1.7   thorpej 
    221       1.5   thorpej 	vga_common_attach(sc, pa->pa_iot, pa->pa_memt, WSDISPLAY_TYPE_PCIVGA,
    222      1.15  drochner 			  vga_pci_lookup_quirks(pa), &vga_pci_funcs);
    223      1.32  drochner 
    224  1.32.8.1  jmcneill 	status = pnp_register(self, vga_pci_power);
    225  1.32.8.1  jmcneill 	if (status != PNP_STATUS_SUCCESS)
    226  1.32.8.1  jmcneill 		aprint_error("%s: couldn't establish power handler\n",
    227  1.32.8.1  jmcneill 		    device_xname(self));
    228      1.32  drochner 	config_found_ia(self, "drm", aux, vga_drm_print);
    229       1.1  drochner }
    230       1.1  drochner 
    231  1.32.8.1  jmcneill static pnp_status_t
    232  1.32.8.1  jmcneill vga_pci_power(device_t dv, pnp_request_t req, void *opaque)
    233  1.32.8.1  jmcneill {
    234  1.32.8.1  jmcneill 	struct vga_pci_softc *sc;
    235  1.32.8.1  jmcneill 	pnp_capabilities_t *pcaps;
    236  1.32.8.1  jmcneill 	pnp_state_t *pstate;
    237  1.32.8.1  jmcneill 	pci_chipset_tag_t pc;
    238  1.32.8.1  jmcneill 	pcitag_t tag;
    239  1.32.8.1  jmcneill 
    240  1.32.8.1  jmcneill 	sc = (struct vga_pci_softc *)dv;
    241  1.32.8.1  jmcneill 	pc = sc->sc_pc;
    242  1.32.8.1  jmcneill 	tag = sc->sc_pcitag;
    243  1.32.8.1  jmcneill 
    244  1.32.8.1  jmcneill 	switch (req) {
    245  1.32.8.1  jmcneill 	case PNP_REQUEST_GET_CAPABILITIES:
    246  1.32.8.1  jmcneill 		pcaps = opaque;
    247  1.32.8.1  jmcneill 		pcaps->state = PNP_STATE_D0 | PNP_STATE_D3;
    248  1.32.8.1  jmcneill 		break;
    249  1.32.8.1  jmcneill 	case PNP_REQUEST_GET_STATE:
    250  1.32.8.1  jmcneill 		pstate = opaque;
    251  1.32.8.1  jmcneill 		*pstate = PNP_STATE_D0; /* XXX */
    252  1.32.8.1  jmcneill 		break;
    253  1.32.8.1  jmcneill 	case PNP_REQUEST_SET_STATE:
    254  1.32.8.1  jmcneill 		pstate = opaque;
    255  1.32.8.1  jmcneill 		switch (*pstate) {
    256  1.32.8.1  jmcneill 		case PNP_STATE_D0:
    257  1.32.8.1  jmcneill 			pci_conf_restore(pc, tag, &sc->sc_pciconf);
    258  1.32.8.1  jmcneill 			return vga_power(dv, req, opaque);
    259  1.32.8.1  jmcneill 			break;
    260  1.32.8.1  jmcneill 		case PNP_STATE_D3:
    261  1.32.8.1  jmcneill 			pci_conf_capture(pc, tag, &sc->sc_pciconf);
    262  1.32.8.1  jmcneill 			break;
    263  1.32.8.1  jmcneill 		default:
    264  1.32.8.1  jmcneill 			return PNP_STATUS_UNSUPPORTED;
    265  1.32.8.1  jmcneill 		}
    266  1.32.8.1  jmcneill 		break;
    267  1.32.8.1  jmcneill 	default:
    268  1.32.8.1  jmcneill 		return PNP_STATUS_UNSUPPORTED;
    269  1.32.8.1  jmcneill 	}
    270  1.32.8.1  jmcneill 
    271  1.32.8.1  jmcneill 	return PNP_STATUS_SUCCESS;
    272  1.32.8.1  jmcneill }
    273  1.32.8.1  jmcneill 
    274       1.1  drochner int
    275      1.26     perry vga_pci_cnattach(bus_space_tag_t iot, bus_space_tag_t memt,
    276      1.30  christos     pci_chipset_tag_t pc, int bus, int device,
    277      1.30  christos     int function)
    278       1.1  drochner {
    279      1.14  junyoung 
    280       1.1  drochner 	return (vga_cnattach(iot, memt, WSDISPLAY_TYPE_PCIVGA, 0));
    281       1.6   thorpej }
    282       1.6   thorpej 
    283      1.32  drochner int
    284      1.32  drochner vga_drm_print(void *aux, const char *pnp)
    285      1.32  drochner {
    286      1.32  drochner 	if (pnp)
    287      1.32  drochner 		aprint_normal("direct rendering for %s", pnp);
    288      1.32  drochner 	return (UNSUPP);
    289      1.32  drochner }
    290      1.32  drochner 
    291      1.32  drochner 
    292      1.27   thorpej static int
    293      1.31  christos vga_pci_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
    294       1.6   thorpej {
    295       1.6   thorpej 	struct vga_config *vc = v;
    296       1.6   thorpej 	struct vga_pci_softc *psc = (void *) vc->softc;
    297       1.6   thorpej 
    298       1.6   thorpej 	switch (cmd) {
    299       1.6   thorpej 	/* PCI config read/write passthrough. */
    300       1.6   thorpej 	case PCI_IOC_CFGREAD:
    301       1.6   thorpej 	case PCI_IOC_CFGWRITE:
    302       1.6   thorpej 		return (pci_devioctl(psc->sc_pc, psc->sc_pcitag,
    303      1.28  christos 		    cmd, data, flag, l));
    304       1.6   thorpej 
    305       1.6   thorpej 	default:
    306      1.10    atatat 		return (EPASSTHROUGH);
    307       1.6   thorpej 	}
    308       1.6   thorpej }
    309       1.6   thorpej 
    310      1.27   thorpej static paddr_t
    311       1.6   thorpej vga_pci_mmap(void *v, off_t offset, int prot)
    312       1.6   thorpej {
    313       1.7   thorpej 	struct vga_config *vc = v;
    314       1.7   thorpej 	struct vga_pci_softc *psc = (void *) vc->softc;
    315       1.7   thorpej 	struct vga_bar *vb;
    316       1.7   thorpej 	int bar;
    317       1.7   thorpej 
    318       1.7   thorpej 	for (bar = 0; bar < NBARS; bar++) {
    319       1.7   thorpej 		vb = &psc->sc_bars[bar];
    320       1.7   thorpej 		if (vb->vb_size == 0)
    321       1.7   thorpej 			continue;
    322       1.7   thorpej 		if (offset >= vb->vb_base &&
    323       1.7   thorpej 		    offset < (vb->vb_base + vb->vb_size)) {
    324       1.7   thorpej 			/* XXX This the right thing to do with flags? */
    325       1.7   thorpej 			return (bus_space_mmap(vc->hdl.vh_memt, vb->vb_base,
    326       1.7   thorpej 			    (offset - vb->vb_base), prot, vb->vb_flags));
    327       1.7   thorpej 		}
    328       1.7   thorpej 	}
    329       1.7   thorpej 
    330       1.7   thorpej 	/* XXX Expansion ROM? */
    331       1.7   thorpej 
    332       1.7   thorpej 	/*
    333       1.7   thorpej 	 * Allow mmap access to the legacy ISA hole.  This is where
    334       1.7   thorpej 	 * the legacy video BIOS will be located, and also where
    335       1.7   thorpej 	 * the legacy VGA display buffer is located.
    336       1.7   thorpej 	 *
    337       1.7   thorpej 	 * XXX Security implications, here?
    338       1.7   thorpej 	 */
    339       1.7   thorpej 	if (offset >= IOM_BEGIN && offset < IOM_END)
    340       1.7   thorpej 		return (bus_space_mmap(vc->hdl.vh_memt, IOM_BEGIN,
    341       1.7   thorpej 		    (offset - IOM_BEGIN), prot, 0));
    342       1.6   thorpej 
    343       1.7   thorpej 	/* Range not found. */
    344       1.6   thorpej 	return (-1);
    345       1.1  drochner }
    346