Home | History | Annotate | Line # | Download | only in pci
pci_usrreq.c revision 1.29.2.4
      1 /*	$NetBSD: pci_usrreq.c,v 1.29.2.4 2016/11/04 14:49:10 pgoyette Exp $	*/
      2 
      3 /*
      4  * Copyright 2001 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed for the NetBSD Project by
     20  *	Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*
     39  * User -> kernel interface for PCI bus access.
     40  */
     41 
     42 #include <sys/cdefs.h>
     43 __KERNEL_RCSID(0, "$NetBSD: pci_usrreq.c,v 1.29.2.4 2016/11/04 14:49:10 pgoyette Exp $");
     44 
     45 #ifdef _KERNEL_OPT
     46 #include "opt_pci.h"
     47 #endif
     48 
     49 #include <sys/param.h>
     50 #include <sys/conf.h>
     51 #include <sys/device.h>
     52 #include <sys/ioctl.h>
     53 #include <sys/proc.h>
     54 #include <sys/systm.h>
     55 #include <sys/errno.h>
     56 #include <sys/fcntl.h>
     57 #include <sys/kauth.h>
     58 #include <sys/localcount.h>
     59 
     60 #include <dev/pci/pcireg.h>
     61 #include <dev/pci/pcivar.h>
     62 #include <dev/pci/pciio.h>
     63 
     64 static int
     65 pciopen(dev_t dev, int flags, int mode, struct lwp *l)
     66 {
     67 	device_t dv;
     68 
     69 	dv = device_lookup(&pci_cd, minor(dev));
     70 	if (dv == NULL)
     71 		return ENXIO;
     72 
     73 	return 0;
     74 }
     75 
     76 static int
     77 pciioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
     78 {
     79 	struct pci_softc *sc = device_lookup_private(&pci_cd, minor(dev));
     80 	struct pci_child *child;
     81 	struct pciio_bdf_cfgreg *bdfr;
     82 	struct pciio_businfo *binfo;
     83 	struct pciio_drvname *dname;
     84 	struct pciio_drvnameonbus *dnameonbus;
     85 	pcitag_t tag;
     86 
     87 	switch (cmd) {
     88 	case PCI_IOC_BDF_CFGREAD:
     89 	case PCI_IOC_BDF_CFGWRITE:
     90 		bdfr = data;
     91 		if (bdfr->bus > 255 || bdfr->device >= sc->sc_maxndevs ||
     92 		    bdfr->function > 7 || ISSET(bdfr->cfgreg.reg, 3))
     93 			return EINVAL;
     94 		tag = pci_make_tag(sc->sc_pc, bdfr->bus, bdfr->device,
     95 		    bdfr->function);
     96 
     97 		if (cmd == PCI_IOC_BDF_CFGREAD) {
     98 			bdfr->cfgreg.val = pci_conf_read(sc->sc_pc, tag,
     99 			    bdfr->cfgreg.reg);
    100 		} else {
    101 			if ((flag & FWRITE) == 0)
    102 				return EBADF;
    103 			pci_conf_write(sc->sc_pc, tag, bdfr->cfgreg.reg,
    104 			    bdfr->cfgreg.val);
    105 		}
    106 		return 0;
    107 
    108 	case PCI_IOC_BUSINFO:
    109 		binfo = data;
    110 		binfo->busno = sc->sc_bus;
    111 		binfo->maxdevs = sc->sc_maxndevs;
    112 		return 0;
    113 
    114 	case PCI_IOC_DRVNAME:
    115 		dname = data;
    116 		if (dname->device >= sc->sc_maxndevs || dname->function > 7)
    117 			return EINVAL;
    118 		child = &sc->PCI_SC_DEVICESC(dname->device, dname->function);
    119 		if (!child->c_dev)
    120 			return ENXIO;
    121 		strlcpy(dname->name, device_xname(child->c_dev),
    122 			sizeof dname->name);
    123 		return 0;
    124 
    125 	case PCI_IOC_DRVNAMEONBUS:
    126 		dnameonbus = data;
    127 		int i;
    128 
    129 		for (i = 0; i < pci_cd.cd_ndevs; i++) {
    130 			sc = device_lookup_private(&pci_cd, i);
    131 			if (sc->sc_bus == dnameonbus->bus)
    132 				break;	/* found the right bus */
    133 		}
    134 		if (i == pci_cd.cd_ndevs || sc == NULL)
    135 			return ENXIO;
    136 		if (dnameonbus->device >= sc->sc_maxndevs ||
    137 		    dnameonbus->function > 7)
    138 			return EINVAL;
    139 
    140 		child = &sc->PCI_SC_DEVICESC(dnameonbus->device,
    141 					     dnameonbus->function);
    142 		if (!child->c_dev)
    143 			return ENXIO;
    144 		strlcpy(dnameonbus->name, device_xname(child->c_dev),
    145 			sizeof dnameonbus->name);
    146 		return 0;
    147 
    148 	default:
    149 		return ENOTTY;
    150 	}
    151 }
    152 
    153 static paddr_t
    154 pcimmap(dev_t dev, off_t offset, int prot)
    155 {
    156 	struct pci_softc *sc = device_lookup_private(&pci_cd, minor(dev));
    157 	struct pci_child *c;
    158 	struct pci_range *r;
    159 	int flags = 0;
    160 	int device, range;
    161 
    162 	if (kauth_authorize_machdep(kauth_cred_get(), KAUTH_MACHDEP_UNMANAGEDMEM,
    163 	    NULL, NULL, NULL, NULL) != 0) {
    164 		return -1;
    165 	}
    166 	/*
    167 	 * Since we allow mapping of the entire bus, we
    168 	 * take the offset to be the address on the bus,
    169 	 * and pass 0 as the offset into that range.
    170 	 *
    171 	 * XXX Need a way to deal with linear/etc.
    172 	 *
    173 	 * XXX we rely on MD mmap() methods to enforce limits since these
    174 	 * are hidden in *_tag_t structs if they exist at all
    175 	 */
    176 
    177 #ifdef PCI_MAGIC_IO_RANGE
    178 	/*
    179 	 * first, check if someone's trying to map the IO range
    180 	 * XXX this assumes 64kB IO space even though some machines can have
    181 	 * significantly more than that - macppc's bandit host bridge allows
    182 	 * 8MB IO space and sparc64 may have the entire 4GB available. The
    183 	 * firmware on both tries to use the lower 64kB first though and
    184 	 * exausting it is pretty difficult so we should be safe
    185 	 */
    186 	if ((offset >= PCI_MAGIC_IO_RANGE) &&
    187 	    (offset < (PCI_MAGIC_IO_RANGE + 0x10000))) {
    188 		return bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
    189 		    0, prot, 0);
    190 	}
    191 #endif /* PCI_MAGIC_IO_RANGE */
    192 
    193 	for (device = 0; device < __arraycount(sc->sc_devices); device++) {
    194 		c = &sc->sc_devices[device];
    195 		if (c->c_dev == NULL)
    196 			continue;
    197 		for (range = 0; range < __arraycount(c->c_range); range++) {
    198 			r = &c->c_range[range];
    199 			if (r->r_size == 0)
    200 				break;
    201 			if (offset >= r->r_offset &&
    202 			    offset < r->r_offset + r->r_size) {
    203 				flags = r->r_flags;
    204 				break;
    205 			}
    206 		}
    207 	}
    208 
    209 	return bus_space_mmap(sc->sc_memt, offset, 0, prot, flags);
    210 }
    211 
    212 const struct cdevsw pci_cdevsw = {
    213 	DEVSW_MODULE_INIT
    214 	.d_open = pciopen,
    215 	.d_close = nullclose,
    216 	.d_read = noread,
    217 	.d_write = nowrite,
    218 	.d_ioctl = pciioctl,
    219 	.d_stop = nostop,
    220 	.d_tty = notty,
    221 	.d_poll = nopoll,
    222 	.d_mmap = pcimmap,
    223 	.d_kqfilter = nokqfilter,
    224 	.d_discard = nodiscard,
    225 	.d_flag = D_OTHER
    226 };
    227 
    228 /*
    229  * pci_devioctl:
    230  *
    231  *	PCI ioctls that can be performed on devices directly.
    232  */
    233 int
    234 pci_devioctl(pci_chipset_tag_t pc, pcitag_t tag, u_long cmd, void *data,
    235     int flag, struct lwp *l)
    236 {
    237 	struct pciio_cfgreg *r = (void *) data;
    238 
    239 	switch (cmd) {
    240 	case PCI_IOC_CFGREAD:
    241 		r->val = pci_conf_read(pc, tag, r->reg);
    242 		break;
    243 
    244 	case PCI_IOC_CFGWRITE:
    245 		if ((flag & FWRITE) == 0)
    246 			return EBADF;
    247 		pci_conf_write(pc, tag, r->reg, r->val);
    248 		break;
    249 
    250 	default:
    251 		return EPASSTHROUGH;
    252 	}
    253 
    254 	return 0;
    255 }
    256