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