2 * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include "opt_bus.h" /* XXX trim includes */
31 #include "opt_compat.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/linker.h>
38 #include <sys/fcntl.h>
40 #include <sys/kernel.h>
42 #include <sys/queue.h>
43 #include <sys/types.h>
47 #include <vm/vm_extern.h>
50 #include <machine/bus.h>
52 #include <machine/resource.h>
54 #include <sys/pciio.h>
55 #include <dev/pci/pcireg.h>
56 #include <dev/pci/pcivar.h>
62 * This is the user interface to PCI configuration space.
65 static d_open_t pci_open;
66 static d_close_t pci_close;
67 static int pci_conf_match(struct pci_match_conf *matches, int num_matches,
68 struct pci_conf *match_buf);
69 static d_ioctl_t pci_ioctl;
71 struct cdevsw pcicdev = {
72 .d_version = D_VERSION,
73 .d_flags = D_NEEDGIANT,
81 pci_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
85 if (oflags & FWRITE) {
86 error = securelevel_gt(td->td_ucred, 0);
95 pci_close(struct cdev *dev, int flag, int devtype, struct thread *td)
101 * Match a single pci_conf structure against an array of pci_match_conf
102 * structures. The first argument, 'matches', is an array of num_matches
103 * pci_match_conf structures. match_buf is a pointer to the pci_conf
104 * structure that will be compared to every entry in the matches array.
105 * This function returns 1 on failure, 0 on success.
108 pci_conf_match(struct pci_match_conf *matches, int num_matches,
109 struct pci_conf *match_buf)
113 if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0))
116 for (i = 0; i < num_matches; i++) {
118 * I'm not sure why someone would do this...but...
120 if (matches[i].flags == PCI_GETCONF_NO_MATCH)
124 * Look at each of the match flags. If it's set, do the
125 * comparison. If the comparison fails, we don't have a
126 * match, go on to the next item if there is one.
128 if (((matches[i].flags & PCI_GETCONF_MATCH_BUS) != 0)
129 && (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus))
132 if (((matches[i].flags & PCI_GETCONF_MATCH_DEV) != 0)
133 && (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev))
136 if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC) != 0)
137 && (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func))
140 if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR) != 0)
141 && (match_buf->pc_vendor != matches[i].pc_vendor))
144 if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE) != 0)
145 && (match_buf->pc_device != matches[i].pc_device))
148 if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS) != 0)
149 && (match_buf->pc_class != matches[i].pc_class))
152 if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT) != 0)
153 && (match_buf->pd_unit != matches[i].pd_unit))
156 if (((matches[i].flags & PCI_GETCONF_MATCH_NAME) != 0)
157 && (strncmp(matches[i].pd_name, match_buf->pd_name,
158 sizeof(match_buf->pd_name)) != 0))
168 pci_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
175 if (!(flag & FWRITE) && cmd != PCIOCGETCONF)
181 struct pci_devinfo *dinfo;
182 struct pci_conf_io *cio;
183 struct devlist *devlist_head;
184 struct pci_match_conf *pattern_buf;
189 cio = (struct pci_conf_io *)data;
195 * If the user specified an offset into the device list,
196 * but the list has changed since they last called this
197 * ioctl, tell them that the list has changed. They will
198 * have to get the list from the beginning.
200 if ((cio->offset != 0)
201 && (cio->generation != pci_generation)){
202 cio->num_matches = 0;
203 cio->status = PCI_GETCONF_LIST_CHANGED;
209 * Check to see whether the user has asked for an offset
210 * past the end of our list.
212 if (cio->offset >= pci_numdevs) {
213 cio->num_matches = 0;
214 cio->status = PCI_GETCONF_LAST_DEVICE;
219 /* get the head of the device queue */
220 devlist_head = &pci_devq;
223 * Determine how much room we have for pci_conf structures.
224 * Round the user's buffer size down to the nearest
225 * multiple of sizeof(struct pci_conf) in case the user
226 * didn't specify a multiple of that size.
228 iolen = min(cio->match_buf_len -
229 (cio->match_buf_len % sizeof(struct pci_conf)),
230 pci_numdevs * sizeof(struct pci_conf));
233 * Since we know that iolen is a multiple of the size of
234 * the pciconf union, it's okay to do this.
236 ionum = iolen / sizeof(struct pci_conf);
239 * If this test is true, the user wants the pci_conf
240 * structures returned to match the supplied entries.
242 if ((cio->num_patterns > 0) && (cio->num_patterns < pci_numdevs)
243 && (cio->pat_buf_len > 0)) {
245 * pat_buf_len needs to be:
246 * num_patterns * sizeof(struct pci_match_conf)
247 * While it is certainly possible the user just
248 * allocated a large buffer, but set the number of
249 * matches correctly, it is far more likely that
250 * their kernel doesn't match the userland utility
251 * they're using. It's also possible that the user
252 * forgot to initialize some variables. Yes, this
253 * may be overly picky, but I hazard to guess that
254 * it's far more likely to just catch folks that
255 * updated their kernel but not their userland.
257 if ((cio->num_patterns *
258 sizeof(struct pci_match_conf)) != cio->pat_buf_len){
259 /* The user made a mistake, return an error*/
260 cio->status = PCI_GETCONF_ERROR;
261 cio->num_matches = 0;
267 * Allocate a buffer to hold the patterns.
269 pattern_buf = malloc(cio->pat_buf_len, M_TEMP,
271 error = copyin(cio->patterns, pattern_buf,
277 num_patterns = cio->num_patterns;
279 } else if ((cio->num_patterns > 0)
280 || (cio->pat_buf_len > 0)) {
282 * The user made a mistake, spit out an error.
284 cio->status = PCI_GETCONF_ERROR;
285 cio->num_matches = 0;
292 * Go through the list of devices and copy out the devices
293 * that match the user's criteria.
295 for (cio->num_matches = 0, error = 0, i = 0,
296 dinfo = STAILQ_FIRST(devlist_head);
297 (dinfo != NULL) && (cio->num_matches < ionum)
298 && (error == 0) && (i < pci_numdevs) && (dinfo != NULL);
299 dinfo = STAILQ_NEXT(dinfo, pci_links), i++) {
304 /* Populate pd_name and pd_unit */
306 if (dinfo->cfg.dev && dinfo->conf.pd_name[0] == '\0')
307 name = device_get_name(dinfo->cfg.dev);
309 strncpy(dinfo->conf.pd_name, name,
310 sizeof(dinfo->conf.pd_name));
311 dinfo->conf.pd_name[PCI_MAXNAMELEN] = 0;
312 dinfo->conf.pd_unit =
313 device_get_unit(dinfo->cfg.dev);
316 if ((pattern_buf == NULL) ||
317 (pci_conf_match(pattern_buf, num_patterns,
318 &dinfo->conf) == 0)) {
321 * If we've filled up the user's buffer,
322 * break out at this point. Since we've
323 * got a match here, we'll pick right back
324 * up at the matching entry. We can also
325 * tell the user that there are more matches
328 if (cio->num_matches >= ionum)
331 /* only if can copy it out do we count it */
332 if (!(error = copyout(&dinfo->conf,
333 &cio->matches[cio->num_matches],
334 sizeof(struct pci_conf)))) {
341 * Set the pointer into the list, so if the user is getting
342 * n records at a time, where n < pci_numdevs,
347 * Set the generation, the user will need this if they make
348 * another ioctl call with offset != 0.
350 cio->generation = pci_generation;
353 * If this is the last device, inform the user so he won't
354 * bother asking for more devices. If dinfo isn't NULL, we
355 * know that there are more matches in the list because of
356 * the way the traversal is done.
359 cio->status = PCI_GETCONF_LAST_DEVICE;
361 cio->status = PCI_GETCONF_MORE_DEVS;
364 if (pattern_buf != NULL)
365 free(pattern_buf, M_TEMP);
372 io = (struct pci_io *)data;
373 switch(io->pi_width) {
377 /* Make sure register is in bounds and aligned. */
378 if ((cmd == PCIOCREAD || cmd == PCIOCWRITE) &&
380 io->pi_reg + io->pi_width > PCI_REGMAX + 1 ||
381 io->pi_reg & (io->pi_width - 1))) {
386 * Assume that the user-level bus number is
387 * in fact the physical PCI bus number.
388 * Look up the grandparent, i.e. the bridge device,
389 * so that we can issue configuration space cycles.
391 pcidev = pci_find_bsf(io->pi_sel.pc_bus,
392 io->pi_sel.pc_dev, io->pi_sel.pc_func);
394 device_t busdev, brdev;
396 busdev = device_get_parent(pcidev);
397 brdev = device_get_parent(busdev);
399 if (cmd == PCIOCWRITE)
400 PCIB_WRITE_CONFIG(brdev,
409 PCIB_READ_CONFIG(brdev,
417 #ifdef COMPAT_FREEBSD4
418 if (cmd == PCIOCREAD) {