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