Home | History | Annotate | Line # | Download | only in pci
vga_pci.c revision 1.57.8.1
      1  1.57.8.1   thorpej /*	$NetBSD: vga_pci.c,v 1.57.8.1 2021/08/04 21:27:00 thorpej 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.57.8.1   thorpej __KERNEL_RCSID(0, "$NetBSD: vga_pci.c,v 1.57.8.1 2021/08/04 21:27:00 thorpej 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.53    cegger #include <dev/pci/wsdisplay_pci.h>
     55       1.1  drochner 
     56      1.38     joerg #include "opt_vga.h"
     57      1.38     joerg 
     58      1.38     joerg #ifdef VGA_POST
     59      1.44     joerg #  if defined(__i386__) || defined(__amd64__)
     60      1.47  jmcneill #    include "acpica.h"
     61      1.44     joerg #  endif
     62      1.38     joerg #include <x86/vga_post.h>
     63      1.38     joerg #endif
     64      1.38     joerg 
     65       1.7   thorpej #define	NBARS		6	/* number of PCI BARs */
     66       1.7   thorpej 
     67       1.7   thorpej struct vga_bar {
     68       1.7   thorpej 	bus_addr_t vb_base;
     69       1.7   thorpej 	bus_size_t vb_size;
     70       1.7   thorpej 	pcireg_t vb_type;
     71       1.7   thorpej 	int vb_flags;
     72       1.7   thorpej };
     73       1.7   thorpej 
     74       1.1  drochner struct vga_pci_softc {
     75       1.5   thorpej 	struct vga_softc sc_vga;
     76       1.5   thorpej 
     77       1.6   thorpej 	pci_chipset_tag_t sc_pc;
     78       1.5   thorpej 	pcitag_t sc_pcitag;
     79       1.7   thorpej 
     80       1.7   thorpej 	struct vga_bar sc_bars[NBARS];
     81       1.7   thorpej 	struct vga_bar sc_rom;
     82      1.37  jmcneill 
     83      1.38     joerg #ifdef VGA_POST
     84      1.38     joerg 	struct vga_post *sc_posth;
     85      1.38     joerg #endif
     86      1.42  jmcneill 
     87      1.42  jmcneill 	struct pci_attach_args sc_paa;
     88       1.1  drochner };
     89       1.1  drochner 
     90      1.46    cegger static int	vga_pci_match(device_t, cfdata_t, void *);
     91      1.46    cegger static void	vga_pci_attach(device_t, device_t, void *);
     92      1.46    cegger static int	vga_pci_rescan(device_t, const char *, const int *);
     93      1.27   thorpej static int	vga_pci_lookup_quirks(struct pci_attach_args *);
     94      1.49    dyoung static bool	vga_pci_resume(device_t dv, const pmf_qual_t *);
     95       1.1  drochner 
     96      1.42  jmcneill CFATTACH_DECL2_NEW(vga_pci, sizeof(struct vga_pci_softc),
     97      1.42  jmcneill     vga_pci_match, vga_pci_attach, NULL, NULL, vga_pci_rescan, NULL);
     98       1.1  drochner 
     99      1.31  christos static int	vga_pci_ioctl(void *, u_long, void *, int, struct lwp *);
    100      1.27   thorpej static paddr_t	vga_pci_mmap(void *, off_t, int);
    101       1.6   thorpej 
    102      1.27   thorpej static const struct vga_funcs vga_pci_funcs = {
    103       1.6   thorpej 	vga_pci_ioctl,
    104       1.6   thorpej 	vga_pci_mmap,
    105       1.6   thorpej };
    106       1.6   thorpej 
    107      1.15  drochner static const struct {
    108      1.15  drochner 	int id;
    109      1.15  drochner 	int quirks;
    110      1.15  drochner } vga_pci_quirks[] = {
    111      1.21      salo 	{PCI_ID_CODE(PCI_VENDOR_SILMOTION, PCI_PRODUCT_SILMOTION_SM712),
    112      1.17  drochner 	 VGA_QUIRK_NOFASTSCROLL},
    113      1.50  shattere 	{PCI_ID_CODE(PCI_VENDOR_CYRIX, PCI_PRODUCT_CYRIX_CX5530_VIDEO),
    114      1.50  shattere 	 VGA_QUIRK_NOFASTSCROLL},
    115      1.15  drochner };
    116      1.16  drochner 
    117      1.16  drochner static const struct {
    118      1.16  drochner 	int vid;
    119      1.16  drochner 	int quirks;
    120      1.16  drochner } vga_pci_vquirks[] = {
    121      1.16  drochner 	{PCI_VENDOR_ATI, VGA_QUIRK_ONEFONT},
    122      1.16  drochner };
    123      1.15  drochner 
    124      1.15  drochner static int
    125      1.27   thorpej vga_pci_lookup_quirks(struct pci_attach_args *pa)
    126      1.15  drochner {
    127      1.15  drochner 	int i;
    128      1.15  drochner 
    129      1.15  drochner 	for (i = 0; i < sizeof(vga_pci_quirks) / sizeof (vga_pci_quirks[0]);
    130      1.15  drochner 	     i++) {
    131      1.15  drochner 		if (vga_pci_quirks[i].id == pa->pa_id)
    132      1.15  drochner 			return (vga_pci_quirks[i].quirks);
    133      1.16  drochner 	}
    134      1.16  drochner 	for (i = 0; i < sizeof(vga_pci_vquirks) / sizeof (vga_pci_vquirks[0]);
    135      1.16  drochner 	     i++) {
    136      1.16  drochner 		if (vga_pci_vquirks[i].vid == PCI_VENDOR(pa->pa_id))
    137      1.16  drochner 			return (vga_pci_vquirks[i].quirks);
    138      1.15  drochner 	}
    139      1.15  drochner 	return (0);
    140      1.15  drochner }
    141      1.15  drochner 
    142      1.27   thorpej static int
    143      1.46    cegger vga_pci_match(device_t parent, cfdata_t match, void *aux)
    144       1.1  drochner {
    145       1.1  drochner 	struct pci_attach_args *pa = aux;
    146       1.1  drochner 	int potential;
    147       1.1  drochner 
    148       1.1  drochner 	potential = 0;
    149       1.1  drochner 
    150       1.1  drochner 	/*
    151       1.1  drochner 	 * If it's prehistoric/vga or display/vga, we might match.
    152      1.25       wiz 	 * For the console device, this is just a sanity check.
    153       1.1  drochner 	 */
    154       1.1  drochner 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_PREHISTORIC &&
    155       1.1  drochner 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_PREHISTORIC_VGA)
    156       1.1  drochner 		potential = 1;
    157       1.1  drochner 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY &&
    158       1.1  drochner 	     PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_DISPLAY_VGA)
    159       1.1  drochner 		potential = 1;
    160       1.1  drochner 
    161       1.1  drochner 	if (!potential)
    162       1.1  drochner 		return (0);
    163       1.1  drochner 
    164       1.1  drochner 	/* check whether it is disabled by firmware */
    165       1.1  drochner 	if ((pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG)
    166       1.1  drochner 	    & (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE))
    167       1.1  drochner 	    != (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE))
    168       1.1  drochner 		return (0);
    169       1.1  drochner 
    170       1.1  drochner 	/* If it's the console, we have a winner! */
    171       1.1  drochner 	if (vga_is_console(pa->pa_iot, WSDISPLAY_TYPE_PCIVGA))
    172       1.1  drochner 		return (1);
    173       1.1  drochner 
    174       1.1  drochner 	/*
    175       1.1  drochner 	 * If we might match, make sure that the card actually looks OK.
    176       1.1  drochner 	 */
    177       1.1  drochner 	if (!vga_common_probe(pa->pa_iot, pa->pa_memt))
    178       1.1  drochner 		return (0);
    179       1.1  drochner 
    180       1.1  drochner 	return (1);
    181       1.1  drochner }
    182       1.1  drochner 
    183      1.27   thorpej static void
    184      1.46    cegger vga_pci_attach(device_t parent, device_t self, void *aux)
    185       1.1  drochner {
    186      1.40      cube 	struct vga_pci_softc *psc = device_private(self);
    187       1.5   thorpej 	struct vga_softc *sc = &psc->sc_vga;
    188       1.1  drochner 	struct pci_attach_args *pa = aux;
    189       1.7   thorpej 	int bar, reg;
    190       1.1  drochner 
    191      1.40      cube 	sc->sc_dev = self;
    192       1.6   thorpej 	psc->sc_pc = pa->pa_pc;
    193       1.5   thorpej 	psc->sc_pcitag = pa->pa_tag;
    194      1.42  jmcneill 	psc->sc_paa = *pa;
    195       1.1  drochner 
    196      1.54  drochner 	pci_aprint_devinfo(pa, NULL);
    197       1.1  drochner 
    198       1.7   thorpej 	/*
    199       1.7   thorpej 	 * Gather info about all the BARs.  These are used to allow
    200       1.7   thorpej 	 * the X server to map the VGA device.
    201       1.7   thorpej 	 */
    202       1.7   thorpej 	for (bar = 0; bar < NBARS; bar++) {
    203       1.7   thorpej 		reg = PCI_MAPREG_START + (bar * 4);
    204      1.11  drochner 		if (!pci_mapreg_probe(psc->sc_pc, psc->sc_pcitag, reg,
    205      1.11  drochner 				      &psc->sc_bars[bar].vb_type)) {
    206      1.11  drochner 			/* there is no valid mapping register */
    207      1.11  drochner 			continue;
    208      1.11  drochner 		}
    209       1.7   thorpej 		if (PCI_MAPREG_TYPE(psc->sc_bars[bar].vb_type) ==
    210       1.7   thorpej 		    PCI_MAPREG_TYPE_IO) {
    211       1.7   thorpej 			/* Don't bother fetching I/O BARs. */
    212       1.7   thorpej 			continue;
    213       1.7   thorpej 		}
    214      1.33    martin #ifndef __LP64__
    215       1.7   thorpej 		if (PCI_MAPREG_MEM_TYPE(psc->sc_bars[bar].vb_type) ==
    216       1.7   thorpej 		    PCI_MAPREG_MEM_TYPE_64BIT) {
    217       1.7   thorpej 			/* XXX */
    218      1.40      cube 			aprint_error_dev(self,
    219      1.40      cube 			    "WARNING: ignoring 64-bit BAR @ 0x%02x\n", reg);
    220      1.11  drochner 			bar++;
    221       1.7   thorpej 			continue;
    222       1.7   thorpej 		}
    223      1.33    martin #endif
    224      1.11  drochner 		if (pci_mapreg_info(psc->sc_pc, psc->sc_pcitag, reg,
    225       1.7   thorpej 		     psc->sc_bars[bar].vb_type,
    226       1.7   thorpej 		     &psc->sc_bars[bar].vb_base,
    227       1.7   thorpej 		     &psc->sc_bars[bar].vb_size,
    228      1.11  drochner 		     &psc->sc_bars[bar].vb_flags))
    229      1.40      cube 			aprint_error_dev(self,
    230      1.40      cube 			    "WARNING: strange BAR @ 0x%02x\n", reg);
    231       1.7   thorpej 	}
    232       1.7   thorpej 
    233      1.56  jdolecek 	/*
    234      1.56  jdolecek 	 * Disable INTx interrupts, there is no specific chipset driver for
    235      1.56  jdolecek 	 * this PCI device. Else unhandled display adapter interrupts
    236      1.56  jdolecek 	 * might freeze the CPU.
    237      1.56  jdolecek 	 */
    238      1.56  jdolecek 	pcireg_t cmd  = pci_conf_read(pa->pa_pc, pa->pa_tag,
    239      1.56  jdolecek 	    PCI_COMMAND_STATUS_REG);
    240      1.56  jdolecek 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    241      1.56  jdolecek 	    cmd | PCI_COMMAND_INTERRUPT_DISABLE);
    242      1.56  jdolecek 
    243       1.7   thorpej 	/* XXX Expansion ROM? */
    244       1.7   thorpej 
    245       1.5   thorpej 	vga_common_attach(sc, pa->pa_iot, pa->pa_memt, WSDISPLAY_TYPE_PCIVGA,
    246      1.15  drochner 			  vga_pci_lookup_quirks(pa), &vga_pci_funcs);
    247      1.32  drochner 
    248      1.38     joerg #ifdef VGA_POST
    249      1.55   msaitoh 	psc->sc_posth = vga_post_init(pa->pa_bus, pa->pa_device,
    250      1.55   msaitoh 	    pa->pa_function);
    251      1.38     joerg 	if (psc->sc_posth == NULL)
    252      1.55   msaitoh 		aprint_error_dev(self,
    253      1.55   msaitoh 		    "WARNING: could not prepare POST handler\n");
    254      1.38     joerg #endif
    255      1.38     joerg 
    256      1.37  jmcneill 	/*
    257      1.37  jmcneill 	 * XXX Do not use the generic PCI framework for now as
    258      1.37  jmcneill 	 * XXX it would power down the device when the console
    259      1.37  jmcneill 	 * XXX is still using it.
    260      1.37  jmcneill 	 */
    261      1.37  jmcneill 	if (!pmf_device_register(self, NULL, vga_pci_resume))
    262      1.37  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    263      1.57   thorpej 	config_found(self, aux, vga_drm_print,
    264  1.57.8.1   thorpej 	    CFARGS(.iattr = "drm"));
    265       1.1  drochner }
    266       1.1  drochner 
    267      1.42  jmcneill static int
    268      1.46    cegger vga_pci_rescan(device_t self, const char *ifattr, const int *locators)
    269      1.42  jmcneill {
    270      1.42  jmcneill 	struct vga_pci_softc *psc = device_private(self);
    271      1.42  jmcneill 
    272      1.57   thorpej 	config_found(self, &psc->sc_paa, vga_drm_print,
    273  1.57.8.1   thorpej 	    CFARGS(.iattr = "drm"));
    274      1.42  jmcneill 
    275      1.42  jmcneill 	return 0;
    276      1.42  jmcneill }
    277      1.42  jmcneill 
    278      1.37  jmcneill static bool
    279      1.49    dyoung vga_pci_resume(device_t dv, const pmf_qual_t *qual)
    280      1.37  jmcneill {
    281      1.47  jmcneill #if defined(VGA_POST) && NACPICA > 0
    282      1.43     joerg 	extern int acpi_md_vbios_reset;
    283      1.43     joerg #endif
    284      1.37  jmcneill 	struct vga_pci_softc *sc = device_private(dv);
    285      1.37  jmcneill 
    286      1.37  jmcneill 	vga_resume(&sc->sc_vga);
    287      1.37  jmcneill 
    288      1.47  jmcneill #if defined(VGA_POST) && NACPICA > 0
    289      1.43     joerg 	if (sc->sc_posth != NULL && acpi_md_vbios_reset == 2)
    290      1.38     joerg 		vga_post_call(sc->sc_posth);
    291      1.38     joerg #endif
    292      1.38     joerg 
    293      1.37  jmcneill 	return true;
    294      1.37  jmcneill }
    295      1.37  jmcneill 
    296       1.1  drochner int
    297      1.26     perry vga_pci_cnattach(bus_space_tag_t iot, bus_space_tag_t memt,
    298      1.30  christos     pci_chipset_tag_t pc, int bus, int device,
    299      1.30  christos     int function)
    300       1.1  drochner {
    301      1.14  junyoung 
    302       1.1  drochner 	return (vga_cnattach(iot, memt, WSDISPLAY_TYPE_PCIVGA, 0));
    303       1.6   thorpej }
    304       1.6   thorpej 
    305      1.32  drochner int
    306      1.32  drochner vga_drm_print(void *aux, const char *pnp)
    307      1.32  drochner {
    308      1.32  drochner 	if (pnp)
    309      1.41  jmcneill 		aprint_normal("drm at %s", pnp);
    310      1.41  jmcneill 	return (UNCONF);
    311      1.32  drochner }
    312      1.32  drochner 
    313      1.32  drochner 
    314      1.27   thorpej static int
    315      1.31  christos vga_pci_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
    316       1.6   thorpej {
    317       1.6   thorpej 	struct vga_config *vc = v;
    318       1.6   thorpej 	struct vga_pci_softc *psc = (void *) vc->softc;
    319       1.6   thorpej 
    320       1.6   thorpej 	switch (cmd) {
    321       1.6   thorpej 	/* PCI config read/write passthrough. */
    322       1.6   thorpej 	case PCI_IOC_CFGREAD:
    323       1.6   thorpej 	case PCI_IOC_CFGWRITE:
    324      1.52    cegger 		return pci_devioctl(psc->sc_pc, psc->sc_pcitag,
    325      1.52    cegger 		    cmd, data, flag, l);
    326       1.6   thorpej 
    327      1.53    cegger 	case WSDISPLAYIO_GET_BUSID:
    328      1.53    cegger 		return wsdisplayio_busid_pci(vc->softc->sc_dev,
    329      1.53    cegger 		    psc->sc_pc, psc->sc_pcitag, data);
    330      1.53    cegger 
    331       1.6   thorpej 	default:
    332      1.52    cegger 		return EPASSTHROUGH;
    333       1.6   thorpej 	}
    334       1.6   thorpej }
    335       1.6   thorpej 
    336      1.27   thorpej static paddr_t
    337       1.6   thorpej vga_pci_mmap(void *v, off_t offset, int prot)
    338       1.6   thorpej {
    339       1.7   thorpej 	struct vga_config *vc = v;
    340       1.7   thorpej 	struct vga_pci_softc *psc = (void *) vc->softc;
    341       1.7   thorpej 	struct vga_bar *vb;
    342       1.7   thorpej 	int bar;
    343       1.7   thorpej 
    344       1.7   thorpej 	for (bar = 0; bar < NBARS; bar++) {
    345       1.7   thorpej 		vb = &psc->sc_bars[bar];
    346       1.7   thorpej 		if (vb->vb_size == 0)
    347       1.7   thorpej 			continue;
    348       1.7   thorpej 		if (offset >= vb->vb_base &&
    349       1.7   thorpej 		    offset < (vb->vb_base + vb->vb_size)) {
    350       1.7   thorpej 			/* XXX This the right thing to do with flags? */
    351       1.7   thorpej 			return (bus_space_mmap(vc->hdl.vh_memt, vb->vb_base,
    352       1.7   thorpej 			    (offset - vb->vb_base), prot, vb->vb_flags));
    353       1.7   thorpej 		}
    354       1.7   thorpej 	}
    355       1.7   thorpej 
    356       1.7   thorpej 	/* XXX Expansion ROM? */
    357       1.7   thorpej 
    358       1.7   thorpej 	/*
    359       1.7   thorpej 	 * Allow mmap access to the legacy ISA hole.  This is where
    360       1.7   thorpej 	 * the legacy video BIOS will be located, and also where
    361       1.7   thorpej 	 * the legacy VGA display buffer is located.
    362       1.7   thorpej 	 *
    363       1.7   thorpej 	 * XXX Security implications, here?
    364       1.7   thorpej 	 */
    365       1.7   thorpej 	if (offset >= IOM_BEGIN && offset < IOM_END)
    366       1.7   thorpej 		return (bus_space_mmap(vc->hdl.vh_memt, IOM_BEGIN,
    367       1.7   thorpej 		    (offset - IOM_BEGIN), prot, 0));
    368       1.6   thorpej 
    369      1.51  macallan #ifdef PCI_MAGIC_IO_RANGE
    370      1.51  macallan 	/* allow to map our IO space on non-x86 machines */
    371      1.51  macallan 	if ((offset >= PCI_MAGIC_IO_RANGE) &&
    372      1.51  macallan 	    (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
    373      1.51  macallan 		return bus_space_mmap(vc->hdl.vh_iot,
    374      1.51  macallan 		    offset - PCI_MAGIC_IO_RANGE,
    375      1.51  macallan 		    0, prot, BUS_SPACE_MAP_LINEAR);
    376      1.51  macallan 	}
    377      1.51  macallan #endif
    378      1.51  macallan 
    379       1.7   thorpej 	/* Range not found. */
    380       1.6   thorpej 	return (-1);
    381       1.1  drochner }
    382