1 /*	$NetBSD: pci_usrreq.c,v 1.29 2015/08/24 23:55:04 pooka 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 2015/08/24 23:55:04 pooka 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 
59 #include <dev/pci/pcireg.h>
60 #include <dev/pci/pcivar.h>
61 #include <dev/pci/pciio.h>
62 
63 static int
pciopen(dev_t dev,int flags,int mode,struct lwp * l)64 pciopen(dev_t dev, int flags, int mode, struct lwp *l)
65 {
66 	device_t dv;
67 
68 	dv = device_lookup(&pci_cd, minor(dev));
69 	if (dv == NULL)
70 		return ENXIO;
71 
72 	return 0;
73 }
74 
75 static int
pciioctl(dev_t dev,u_long cmd,void * data,int flag,struct lwp * l)76 pciioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
77 {
78 	struct pci_softc *sc = device_lookup_private(&pci_cd, minor(dev));
79 	struct pci_child *child;
80 	struct pciio_bdf_cfgreg *bdfr;
81 	struct pciio_businfo *binfo;
82 	struct pciio_drvname *dname;
83 	pcitag_t tag;
84 
85 	switch (cmd) {
86 	case PCI_IOC_BDF_CFGREAD:
87 	case PCI_IOC_BDF_CFGWRITE:
88 		bdfr = data;
89 		if (bdfr->bus > 255 || bdfr->device >= sc->sc_maxndevs ||
90 		    bdfr->function > 7 || ISSET(bdfr->cfgreg.reg, 3))
91 			return EINVAL;
92 		tag = pci_make_tag(sc->sc_pc, bdfr->bus, bdfr->device,
93 		    bdfr->function);
94 
95 		if (cmd == PCI_IOC_BDF_CFGREAD) {
96 			bdfr->cfgreg.val = pci_conf_read(sc->sc_pc, tag,
97 			    bdfr->cfgreg.reg);
98 		} else {
99 			if ((flag & FWRITE) == 0)
100 				return EBADF;
101 			pci_conf_write(sc->sc_pc, tag, bdfr->cfgreg.reg,
102 			    bdfr->cfgreg.val);
103 		}
104 		return 0;
105 
106 	case PCI_IOC_BUSINFO:
107 		binfo = data;
108 		binfo->busno = sc->sc_bus;
109 		binfo->maxdevs = sc->sc_maxndevs;
110 		return 0;
111 
112 	case PCI_IOC_DRVNAME:
113 		dname = data;
114 		if (dname->device >= sc->sc_maxndevs || dname->function > 7)
115 			return EINVAL;
116 		child = &sc->PCI_SC_DEVICESC(dname->device, dname->function);
117 		if (!child->c_dev)
118 			return ENXIO;
119 		strlcpy(dname->name, device_xname(child->c_dev),
120 			sizeof dname->name);
121 		return 0;
122 
123 	default:
124 		return ENOTTY;
125 	}
126 }
127 
128 static paddr_t
pcimmap(dev_t dev,off_t offset,int prot)129 pcimmap(dev_t dev, off_t offset, int prot)
130 {
131 	struct pci_softc *sc = device_lookup_private(&pci_cd, minor(dev));
132 	struct pci_child *c;
133 	struct pci_range *r;
134 	int flags = 0;
135 	int device, range;
136 
137 	if (kauth_authorize_machdep(kauth_cred_get(), KAUTH_MACHDEP_UNMANAGEDMEM,
138 	    NULL, NULL, NULL, NULL) != 0) {
139 		return -1;
140 	}
141 	/*
142 	 * Since we allow mapping of the entire bus, we
143 	 * take the offset to be the address on the bus,
144 	 * and pass 0 as the offset into that range.
145 	 *
146 	 * XXX Need a way to deal with linear/etc.
147 	 *
148 	 * XXX we rely on MD mmap() methods to enforce limits since these
149 	 * are hidden in *_tag_t structs if they exist at all
150 	 */
151 
152 #ifdef PCI_MAGIC_IO_RANGE
153 	/*
154 	 * first, check if someone's trying to map the IO range
155 	 * XXX this assumes 64kB IO space even though some machines can have
156 	 * significantly more than that - macppc's bandit host bridge allows
157 	 * 8MB IO space and sparc64 may have the entire 4GB available. The
158 	 * firmware on both tries to use the lower 64kB first though and
159 	 * exausting it is pretty difficult so we should be safe
160 	 */
161 	if ((offset >= PCI_MAGIC_IO_RANGE) &&
162 	    (offset < (PCI_MAGIC_IO_RANGE + 0x10000))) {
163 		return bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
164 		    0, prot, 0);
165 	}
166 #endif /* PCI_MAGIC_IO_RANGE */
167 
168 	for (device = 0; device < __arraycount(sc->sc_devices); device++) {
169 		c = &sc->sc_devices[device];
170 		if (c->c_dev == NULL)
171 			continue;
172 		for (range = 0; range < __arraycount(c->c_range); range++) {
173 			r = &c->c_range[range];
174 			if (r->r_size == 0)
175 				break;
176 			if (offset >= r->r_offset &&
177 			    offset < r->r_offset + r->r_size) {
178 				flags = r->r_flags;
179 				break;
180 			}
181 		}
182 	}
183 
184 	return bus_space_mmap(sc->sc_memt, offset, 0, prot, flags);
185 }
186 
187 const struct cdevsw pci_cdevsw = {
188 	.d_open = pciopen,
189 	.d_close = nullclose,
190 	.d_read = noread,
191 	.d_write = nowrite,
192 	.d_ioctl = pciioctl,
193 	.d_stop = nostop,
194 	.d_tty = notty,
195 	.d_poll = nopoll,
196 	.d_mmap = pcimmap,
197 	.d_kqfilter = nokqfilter,
198 	.d_discard = nodiscard,
199 	.d_flag = D_OTHER
200 };
201 
202 /*
203  * pci_devioctl:
204  *
205  *	PCI ioctls that can be performed on devices directly.
206  */
207 int
pci_devioctl(pci_chipset_tag_t pc,pcitag_t tag,u_long cmd,void * data,int flag,struct lwp * l)208 pci_devioctl(pci_chipset_tag_t pc, pcitag_t tag, u_long cmd, void *data,
209     int flag, struct lwp *l)
210 {
211 	struct pciio_cfgreg *r = (void *) data;
212 
213 	switch (cmd) {
214 	case PCI_IOC_CFGREAD:
215 		r->val = pci_conf_read(pc, tag, r->reg);
216 		break;
217 
218 	case PCI_IOC_CFGWRITE:
219 		if ((flag & FWRITE) == 0)
220 			return EBADF;
221 		pci_conf_write(pc, tag, r->reg, r->val);
222 		break;
223 
224 	default:
225 		return EPASSTHROUGH;
226 	}
227 
228 	return 0;
229 }
230