Home | History | Annotate | Line # | Download | only in pci
drmfb_pci.c revision 1.1
      1 /*	$NetBSD: drmfb_pci.c,v 1.1 2015/03/05 17:50:41 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Taylor R. Campbell.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * drmfb_pci: drmfb hooks for PCI devices.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: drmfb_pci.c,v 1.1 2015/03/05 17:50:41 riastradh Exp $");
     38 
     39 #include <sys/types.h>
     40 #include <sys/device.h>
     41 #include <sys/errno.h>
     42 #include <sys/systm.h>
     43 
     44 #include <dev/pci/pciio.h>
     45 #include <dev/pci/pcireg.h>
     46 #include <dev/pci/pcivar.h>
     47 #include <dev/pci/wsdisplay_pci.h>
     48 
     49 #include <drm/drmP.h>
     50 #include <drm/drm_fb_helper.h>
     51 
     52 #include "drmfb.h"
     53 #include "drmfb_pci.h"
     54 
     55 /*
     56  * drmfb_pci_mmap: Implementation of drmfb_params::dp_mmap.  Don't use
     57  * this for dp_mmapfb -- how to get at the framebuffer is device-
     58  * specific.
     59  */
     60 paddr_t
     61 drmfb_pci_mmap(struct drmfb_softc *sc, off_t offset, int prot)
     62 {
     63 	struct drm_device *const dev = sc->sc_da.da_fb_helper->dev;
     64 	const struct pci_attach_args *const pa = &dev->pdev->pd_pa;
     65 	unsigned i;
     66 
     67 	for (i = 0; PCI_BAR(i) <= PCI_MAPREG_ROM; i++) {
     68 		pcireg_t type;
     69 		bus_addr_t addr;
     70 		bus_size_t size;
     71 		int flags;
     72 
     73 		/* Interrogate the BAR.  */
     74 		if (!pci_mapreg_probe(pa->pa_pc, pa->pa_tag, PCI_BAR(i),
     75 			&type))
     76 			continue;
     77 		if (PCI_MAPREG_TYPE(type) != PCI_MAPREG_TYPE_MEM)
     78 			continue;
     79 		if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, PCI_BAR(i), type,
     80 			&addr, &size, &flags))
     81 			continue;
     82 
     83 		/* Try to map it if it's in range.  */
     84 		if ((addr <= offset) && (offset < (addr + size)))
     85 			return bus_space_mmap(pa->pa_memt, addr,
     86 			    (offset - addr), prot, flags);
     87 
     88 		/* Skip a slot if this was a 64-bit BAR.  */
     89 		if ((PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_MEM) &&
     90 		    (PCI_MAPREG_MEM_TYPE(type) == PCI_MAPREG_MEM_TYPE_64BIT))
     91 			i += 1;
     92 	}
     93 
     94 	/* Failure!  */
     95 	return -1;
     96 }
     97 
     98 /*
     99  * drmfb_pci_ioctl: Implementation of drmfb_params::dp_ioctl.  Provides:
    100  *
    101  * - WSDISPLAY_GET_BUSID
    102  * - WSDISPLAY_GTYPE
    103  *
    104  * Additionally allows access to PCI registers.
    105  *
    106  * XXX Do we need to provide access to PCI registers?  I can't find
    107  * anything that uses this functionality, and as is it is very
    108  * dangerous.
    109  */
    110 int
    111 drmfb_pci_ioctl(struct drmfb_softc *sc, unsigned long cmd, void *data,
    112     int flag, struct lwp *l)
    113 {
    114 	struct drm_device *const dev = sc->sc_da.da_fb_helper->dev;
    115 	struct pci_attach_args *const pa = &dev->pdev->pd_pa;
    116 
    117 	switch (cmd) {
    118 	/* PCI config read/write passthrough.  */
    119 	case PCI_IOC_CFGREAD:
    120 	case PCI_IOC_CFGWRITE:
    121 		return pci_devioctl(pa->pa_pc, pa->pa_tag, cmd, data, flag, l);
    122 
    123 	/* PCI-specific wsdisplay ioctls.  */
    124 	case WSDISPLAYIO_GET_BUSID:
    125 		return wsdisplayio_busid_pci(dev->dev, pa->pa_pc, pa->pa_tag,
    126 		    data);
    127 	case WSDISPLAYIO_GTYPE:
    128 		*(unsigned int *)data = WSDISPLAY_TYPE_PCIVGA;
    129 		return 0;
    130 
    131 	default:
    132 		return EPASSTHROUGH;
    133 	}
    134 }
    135