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