2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "opt_bus.h" /* XXX trim includes */
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/linker.h>
40 #include <sys/fcntl.h>
42 #include <sys/kernel.h>
45 #include <sys/queue.h>
46 #include <sys/rwlock.h>
47 #include <sys/sglist.h>
51 #include <vm/vm_extern.h>
52 #include <vm/vm_map.h>
53 #include <vm/vm_object.h>
54 #include <vm/vm_page.h>
55 #include <vm/vm_pager.h>
58 #include <machine/bus.h>
60 #include <machine/resource.h>
62 #include <sys/pciio.h>
63 #include <dev/pci/pcireg.h>
64 #include <dev/pci/pcivar.h>
70 * This is the user interface to PCI configuration space.
73 static d_open_t pci_open;
74 static d_close_t pci_close;
75 static d_ioctl_t pci_ioctl;
77 struct cdevsw pcicdev = {
78 .d_version = D_VERSION,
79 .d_flags = D_NEEDGIANT,
87 pci_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
91 if (oflags & FWRITE) {
92 error = securelevel_gt(td->td_ucred, 0);
101 pci_close(struct cdev *dev, int flag, int devtype, struct thread *td)
107 * Match a single pci_conf structure against an array of pci_match_conf
108 * structures. The first argument, 'matches', is an array of num_matches
109 * pci_match_conf structures. match_buf is a pointer to the pci_conf
110 * structure that will be compared to every entry in the matches array.
111 * This function returns 1 on failure, 0 on success.
114 pci_conf_match_native(struct pci_match_conf *matches, int num_matches,
115 struct pci_conf *match_buf)
119 if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0))
122 for (i = 0; i < num_matches; i++) {
124 * I'm not sure why someone would do this...but...
126 if (matches[i].flags == PCI_GETCONF_NO_MATCH)
130 * Look at each of the match flags. If it's set, do the
131 * comparison. If the comparison fails, we don't have a
132 * match, go on to the next item if there is one.
134 if (((matches[i].flags & PCI_GETCONF_MATCH_DOMAIN) != 0)
135 && (match_buf->pc_sel.pc_domain !=
136 matches[i].pc_sel.pc_domain))
139 if (((matches[i].flags & PCI_GETCONF_MATCH_BUS) != 0)
140 && (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus))
143 if (((matches[i].flags & PCI_GETCONF_MATCH_DEV) != 0)
144 && (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev))
147 if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC) != 0)
148 && (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func))
151 if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR) != 0)
152 && (match_buf->pc_vendor != matches[i].pc_vendor))
155 if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE) != 0)
156 && (match_buf->pc_device != matches[i].pc_device))
159 if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS) != 0)
160 && (match_buf->pc_class != matches[i].pc_class))
163 if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT) != 0)
164 && (match_buf->pd_unit != matches[i].pd_unit))
167 if (((matches[i].flags & PCI_GETCONF_MATCH_NAME) != 0)
168 && (strncmp(matches[i].pd_name, match_buf->pd_name,
169 sizeof(match_buf->pd_name)) != 0))
178 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
179 defined(COMPAT_FREEBSD6)
183 PCI_GETCONF_NO_MATCH_OLD = 0x00,
184 PCI_GETCONF_MATCH_BUS_OLD = 0x01,
185 PCI_GETCONF_MATCH_DEV_OLD = 0x02,
186 PCI_GETCONF_MATCH_FUNC_OLD = 0x04,
187 PCI_GETCONF_MATCH_NAME_OLD = 0x08,
188 PCI_GETCONF_MATCH_UNIT_OLD = 0x10,
189 PCI_GETCONF_MATCH_VENDOR_OLD = 0x20,
190 PCI_GETCONF_MATCH_DEVICE_OLD = 0x40,
191 PCI_GETCONF_MATCH_CLASS_OLD = 0x80
192 } pci_getconf_flags_old;
195 u_int8_t pc_bus; /* bus number */
196 u_int8_t pc_dev; /* device on this bus */
197 u_int8_t pc_func; /* function on this device */
200 struct pci_conf_old {
201 struct pcisel_old pc_sel; /* bus+slot+function */
202 u_int8_t pc_hdr; /* PCI header type */
203 u_int16_t pc_subvendor; /* card vendor ID */
204 u_int16_t pc_subdevice; /* card device ID, assigned by
206 u_int16_t pc_vendor; /* chip vendor ID */
207 u_int16_t pc_device; /* chip device ID, assigned by
209 u_int8_t pc_class; /* chip PCI class */
210 u_int8_t pc_subclass; /* chip PCI subclass */
211 u_int8_t pc_progif; /* chip PCI programming interface */
212 u_int8_t pc_revid; /* chip revision ID */
213 char pd_name[PCI_MAXNAMELEN + 1]; /* device name */
214 u_long pd_unit; /* device unit number */
217 struct pci_match_conf_old {
218 struct pcisel_old pc_sel; /* bus+slot+function */
219 char pd_name[PCI_MAXNAMELEN + 1]; /* device name */
220 u_long pd_unit; /* Unit number */
221 u_int16_t pc_vendor; /* PCI Vendor ID */
222 u_int16_t pc_device; /* PCI Device ID */
223 u_int8_t pc_class; /* PCI class */
224 pci_getconf_flags_old flags; /* Matching expression */
228 struct pcisel_old pi_sel; /* device to operate on */
229 int pi_reg; /* configuration register to examine */
230 int pi_width; /* width (in bytes) of read or write */
231 u_int32_t pi_data; /* data to write or result of read */
234 #ifdef COMPAT_FREEBSD32
235 struct pci_conf_old32 {
236 struct pcisel_old pc_sel; /* bus+slot+function */
237 uint8_t pc_hdr; /* PCI header type */
238 uint16_t pc_subvendor; /* card vendor ID */
239 uint16_t pc_subdevice; /* card device ID, assigned by
241 uint16_t pc_vendor; /* chip vendor ID */
242 uint16_t pc_device; /* chip device ID, assigned by
244 uint8_t pc_class; /* chip PCI class */
245 uint8_t pc_subclass; /* chip PCI subclass */
246 uint8_t pc_progif; /* chip PCI programming interface */
247 uint8_t pc_revid; /* chip revision ID */
248 char pd_name[PCI_MAXNAMELEN + 1]; /* device name */
249 uint32_t pd_unit; /* device unit number (u_long) */
252 struct pci_match_conf_old32 {
253 struct pcisel_old pc_sel; /* bus+slot+function */
254 char pd_name[PCI_MAXNAMELEN + 1]; /* device name */
255 uint32_t pd_unit; /* Unit number (u_long) */
256 uint16_t pc_vendor; /* PCI Vendor ID */
257 uint16_t pc_device; /* PCI Device ID */
258 uint8_t pc_class; /* PCI class */
259 pci_getconf_flags_old flags; /* Matching expression */
262 struct pci_conf_io32 {
263 uint32_t pat_buf_len; /* pattern buffer length */
264 uint32_t num_patterns; /* number of patterns */
265 uint32_t patterns; /* pattern buffer
266 (struct pci_match_conf_old32 *) */
267 uint32_t match_buf_len; /* match buffer length */
268 uint32_t num_matches; /* number of matches returned */
269 uint32_t matches; /* match buffer
270 (struct pci_conf_old32 *) */
271 uint32_t offset; /* offset into device list */
272 uint32_t generation; /* device list generation */
273 pci_getconf_status status; /* request status */
276 #define PCIOCGETCONF_OLD32 _IOWR('p', 1, struct pci_conf_io32)
277 #endif /* COMPAT_FREEBSD32 */
279 #define PCIOCGETCONF_OLD _IOWR('p', 1, struct pci_conf_io)
280 #define PCIOCREAD_OLD _IOWR('p', 2, struct pci_io_old)
281 #define PCIOCWRITE_OLD _IOWR('p', 3, struct pci_io_old)
284 pci_conf_match_old(struct pci_match_conf_old *matches, int num_matches,
285 struct pci_conf *match_buf)
289 if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0))
292 for (i = 0; i < num_matches; i++) {
293 if (match_buf->pc_sel.pc_domain != 0)
297 * I'm not sure why someone would do this...but...
299 if (matches[i].flags == PCI_GETCONF_NO_MATCH_OLD)
303 * Look at each of the match flags. If it's set, do the
304 * comparison. If the comparison fails, we don't have a
305 * match, go on to the next item if there is one.
307 if (((matches[i].flags & PCI_GETCONF_MATCH_BUS_OLD) != 0)
308 && (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus))
311 if (((matches[i].flags & PCI_GETCONF_MATCH_DEV_OLD) != 0)
312 && (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev))
315 if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC_OLD) != 0)
316 && (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func))
319 if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR_OLD) != 0)
320 && (match_buf->pc_vendor != matches[i].pc_vendor))
323 if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE_OLD) != 0)
324 && (match_buf->pc_device != matches[i].pc_device))
327 if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS_OLD) != 0)
328 && (match_buf->pc_class != matches[i].pc_class))
331 if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT_OLD) != 0)
332 && (match_buf->pd_unit != matches[i].pd_unit))
335 if (((matches[i].flags & PCI_GETCONF_MATCH_NAME_OLD) != 0)
336 && (strncmp(matches[i].pd_name, match_buf->pd_name,
337 sizeof(match_buf->pd_name)) != 0))
346 #ifdef COMPAT_FREEBSD32
348 pci_conf_match_old32(struct pci_match_conf_old32 *matches, int num_matches,
349 struct pci_conf *match_buf)
353 if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0))
356 for (i = 0; i < num_matches; i++) {
357 if (match_buf->pc_sel.pc_domain != 0)
361 * I'm not sure why someone would do this...but...
363 if (matches[i].flags == PCI_GETCONF_NO_MATCH_OLD)
367 * Look at each of the match flags. If it's set, do the
368 * comparison. If the comparison fails, we don't have a
369 * match, go on to the next item if there is one.
371 if (((matches[i].flags & PCI_GETCONF_MATCH_BUS_OLD) != 0) &&
372 (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus))
375 if (((matches[i].flags & PCI_GETCONF_MATCH_DEV_OLD) != 0) &&
376 (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev))
379 if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC_OLD) != 0) &&
380 (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func))
383 if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR_OLD) != 0) &&
384 (match_buf->pc_vendor != matches[i].pc_vendor))
387 if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE_OLD) != 0) &&
388 (match_buf->pc_device != matches[i].pc_device))
391 if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS_OLD) != 0) &&
392 (match_buf->pc_class != matches[i].pc_class))
395 if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT_OLD) != 0) &&
396 ((u_int32_t)match_buf->pd_unit != matches[i].pd_unit))
399 if (((matches[i].flags & PCI_GETCONF_MATCH_NAME_OLD) != 0) &&
400 (strncmp(matches[i].pd_name, match_buf->pd_name,
401 sizeof(match_buf->pd_name)) != 0))
409 #endif /* COMPAT_FREEBSD32 */
410 #endif /* !PRE7_COMPAT */
412 union pci_conf_union {
415 struct pci_conf_old pco;
416 #ifdef COMPAT_FREEBSD32
417 struct pci_conf_old32 pco32;
423 pci_conf_match(u_long cmd, struct pci_match_conf *matches, int num_matches,
424 struct pci_conf *match_buf)
429 return (pci_conf_match_native(
430 (struct pci_match_conf *)matches, num_matches, match_buf));
432 case PCIOCGETCONF_OLD:
433 return (pci_conf_match_old(
434 (struct pci_match_conf_old *)matches, num_matches,
436 #ifdef COMPAT_FREEBSD32
437 case PCIOCGETCONF_OLD32:
438 return (pci_conf_match_old32(
439 (struct pci_match_conf_old32 *)matches, num_matches,
444 /* programmer error */
450 * Like PVE_NEXT but takes an explicit length since 'pve' is a user
451 * pointer that cannot be dereferenced.
453 #define PVE_NEXT_LEN(pve, datalen) \
454 ((struct pci_vpd_element *)((char *)(pve) + \
455 sizeof(struct pci_vpd_element) + (datalen)))
458 pci_list_vpd(device_t dev, struct pci_list_vpd_io *lvio)
460 struct pci_vpd_element vpd_element, *vpd_user;
461 struct pcicfg_vpd *vpd;
465 vpd = pci_fetch_vpd_list(dev);
466 if (vpd->vpd_reg == 0 || vpd->vpd_ident == NULL)
470 * Calculate the amount of space needed in the data buffer. An
471 * identifier element is always present followed by the read-only
472 * and read-write keywords.
474 len = sizeof(struct pci_vpd_element) + strlen(vpd->vpd_ident);
475 for (i = 0; i < vpd->vpd_rocnt; i++)
476 len += sizeof(struct pci_vpd_element) + vpd->vpd_ros[i].len;
477 for (i = 0; i < vpd->vpd_wcnt; i++)
478 len += sizeof(struct pci_vpd_element) + vpd->vpd_w[i].len;
480 if (lvio->plvi_len == 0) {
481 lvio->plvi_len = len;
484 if (lvio->plvi_len < len) {
485 lvio->plvi_len = len;
490 * Copyout the identifier string followed by each keyword and
493 vpd_user = lvio->plvi_data;
494 vpd_element.pve_keyword[0] = '\0';
495 vpd_element.pve_keyword[1] = '\0';
496 vpd_element.pve_flags = PVE_FLAG_IDENT;
497 vpd_element.pve_datalen = strlen(vpd->vpd_ident);
498 error = copyout(&vpd_element, vpd_user, sizeof(vpd_element));
501 error = copyout(vpd->vpd_ident, vpd_user->pve_data,
502 strlen(vpd->vpd_ident));
505 vpd_user = PVE_NEXT_LEN(vpd_user, vpd_element.pve_datalen);
506 vpd_element.pve_flags = 0;
507 for (i = 0; i < vpd->vpd_rocnt; i++) {
508 vpd_element.pve_keyword[0] = vpd->vpd_ros[i].keyword[0];
509 vpd_element.pve_keyword[1] = vpd->vpd_ros[i].keyword[1];
510 vpd_element.pve_datalen = vpd->vpd_ros[i].len;
511 error = copyout(&vpd_element, vpd_user, sizeof(vpd_element));
514 error = copyout(vpd->vpd_ros[i].value, vpd_user->pve_data,
515 vpd->vpd_ros[i].len);
518 vpd_user = PVE_NEXT_LEN(vpd_user, vpd_element.pve_datalen);
520 vpd_element.pve_flags = PVE_FLAG_RW;
521 for (i = 0; i < vpd->vpd_wcnt; i++) {
522 vpd_element.pve_keyword[0] = vpd->vpd_w[i].keyword[0];
523 vpd_element.pve_keyword[1] = vpd->vpd_w[i].keyword[1];
524 vpd_element.pve_datalen = vpd->vpd_w[i].len;
525 error = copyout(&vpd_element, vpd_user, sizeof(vpd_element));
528 error = copyout(vpd->vpd_w[i].value, vpd_user->pve_data,
532 vpd_user = PVE_NEXT_LEN(vpd_user, vpd_element.pve_datalen);
534 KASSERT((char *)vpd_user - (char *)lvio->plvi_data == len,
535 ("length mismatch"));
536 lvio->plvi_len = len;
541 pci_match_conf_size(u_long cmd)
546 return (sizeof(struct pci_match_conf));
548 case PCIOCGETCONF_OLD:
549 return (sizeof(struct pci_match_conf_old));
550 #ifdef COMPAT_FREEBSD32
551 case PCIOCGETCONF_OLD32:
552 return (sizeof(struct pci_match_conf_old32));
556 /* programmer error */
562 pci_conf_size(u_long cmd)
567 return (sizeof(struct pci_conf));
569 case PCIOCGETCONF_OLD:
570 return (sizeof(struct pci_conf_old));
571 #ifdef COMPAT_FREEBSD32
572 case PCIOCGETCONF_OLD32:
573 return (sizeof(struct pci_conf_old32));
577 /* programmer error */
583 pci_conf_io_init(struct pci_conf_io *cio, caddr_t data, u_long cmd)
585 #if defined(PRE7_COMPAT) && defined(COMPAT_FREEBSD32)
586 struct pci_conf_io32 *cio32;
592 case PCIOCGETCONF_OLD:
594 *cio = *(struct pci_conf_io *)data;
597 #if defined(PRE7_COMPAT) && defined(COMPAT_FREEBSD32)
598 case PCIOCGETCONF_OLD32:
599 cio32 = (struct pci_conf_io32 *)data;
600 cio->pat_buf_len = cio32->pat_buf_len;
601 cio->num_patterns = cio32->num_patterns;
602 cio->patterns = (void *)(uintptr_t)cio32->patterns;
603 cio->match_buf_len = cio32->match_buf_len;
604 cio->num_matches = cio32->num_matches;
605 cio->matches = (void *)(uintptr_t)cio32->matches;
606 cio->offset = cio32->offset;
607 cio->generation = cio32->generation;
608 cio->status = cio32->status;
613 /* programmer error */
619 pci_conf_io_update_data(const struct pci_conf_io *cio, caddr_t data,
622 struct pci_conf_io *d_cio;
623 #if defined(PRE7_COMPAT) && defined(COMPAT_FREEBSD32)
624 struct pci_conf_io32 *cio32;
630 case PCIOCGETCONF_OLD:
632 d_cio = (struct pci_conf_io *)data;
633 d_cio->status = cio->status;
634 d_cio->generation = cio->generation;
635 d_cio->offset = cio->offset;
636 d_cio->num_matches = cio->num_matches;
639 #if defined(PRE7_COMPAT) && defined(COMPAT_FREEBSD32)
640 case PCIOCGETCONF_OLD32:
641 cio32 = (struct pci_conf_io32 *)data;
643 cio32->status = cio->status;
644 cio32->generation = cio->generation;
645 cio32->offset = cio->offset;
646 cio32->num_matches = cio->num_matches;
651 /* programmer error */
657 pci_conf_for_copyout(const struct pci_conf *pcp, union pci_conf_union *pcup,
661 memset(pcup, 0, sizeof(*pcup));
669 #ifdef COMPAT_FREEBSD32
670 case PCIOCGETCONF_OLD32:
671 pcup->pco32.pc_sel.pc_bus = pcp->pc_sel.pc_bus;
672 pcup->pco32.pc_sel.pc_dev = pcp->pc_sel.pc_dev;
673 pcup->pco32.pc_sel.pc_func = pcp->pc_sel.pc_func;
674 pcup->pco32.pc_hdr = pcp->pc_hdr;
675 pcup->pco32.pc_subvendor = pcp->pc_subvendor;
676 pcup->pco32.pc_subdevice = pcp->pc_subdevice;
677 pcup->pco32.pc_vendor = pcp->pc_vendor;
678 pcup->pco32.pc_device = pcp->pc_device;
679 pcup->pco32.pc_class = pcp->pc_class;
680 pcup->pco32.pc_subclass = pcp->pc_subclass;
681 pcup->pco32.pc_progif = pcp->pc_progif;
682 pcup->pco32.pc_revid = pcp->pc_revid;
683 strlcpy(pcup->pco32.pd_name, pcp->pd_name,
684 sizeof(pcup->pco32.pd_name));
685 pcup->pco32.pd_unit = (uint32_t)pcp->pd_unit;
688 #endif /* COMPAT_FREEBSD32 */
689 case PCIOCGETCONF_OLD:
690 pcup->pco.pc_sel.pc_bus = pcp->pc_sel.pc_bus;
691 pcup->pco.pc_sel.pc_dev = pcp->pc_sel.pc_dev;
692 pcup->pco.pc_sel.pc_func = pcp->pc_sel.pc_func;
693 pcup->pco.pc_hdr = pcp->pc_hdr;
694 pcup->pco.pc_subvendor = pcp->pc_subvendor;
695 pcup->pco.pc_subdevice = pcp->pc_subdevice;
696 pcup->pco.pc_vendor = pcp->pc_vendor;
697 pcup->pco.pc_device = pcp->pc_device;
698 pcup->pco.pc_class = pcp->pc_class;
699 pcup->pco.pc_subclass = pcp->pc_subclass;
700 pcup->pco.pc_progif = pcp->pc_progif;
701 pcup->pco.pc_revid = pcp->pc_revid;
702 strlcpy(pcup->pco.pd_name, pcp->pd_name,
703 sizeof(pcup->pco.pd_name));
704 pcup->pco.pd_unit = pcp->pd_unit;
706 #endif /* PRE7_COMPAT */
709 /* programmer error */
715 pci_bar_mmap(device_t pcidev, struct pci_bar_mmap *pbm)
729 map = &td->td_proc->p_vmspace->vm_map;
730 if ((pbm->pbm_flags & ~(PCIIO_BAR_MMAP_FIXED | PCIIO_BAR_MMAP_EXCL |
731 PCIIO_BAR_MMAP_RW | PCIIO_BAR_MMAP_ACTIVATE)) != 0 ||
732 pbm->pbm_memattr != (vm_memattr_t)pbm->pbm_memattr ||
733 !pmap_is_valid_memattr(map->pmap, pbm->pbm_memattr))
736 /* Fetch the BAR physical base and length. */
737 pm = pci_find_bar(pcidev, pbm->pbm_reg);
740 if (!pci_bar_enabled(pcidev, pm))
741 return (EBUSY); /* XXXKIB enable if _ACTIVATE */
742 if (!PCI_BAR_MEM(pm->pm_value))
744 pbase = trunc_page(pm->pm_value);
745 plen = round_page(pm->pm_value + ((pci_addr_t)1 << pm->pm_size)) -
747 prot = VM_PROT_READ | (((pbm->pbm_flags & PCIIO_BAR_MMAP_RW) != 0) ?
750 /* Create vm structures and mmap. */
751 sg = sglist_alloc(1, M_WAITOK);
752 error = sglist_append_phys(sg, pbase, plen);
755 obj = vm_pager_allocate(OBJT_SG, sg, plen, prot, 0, td->td_ucred);
760 obj->memattr = pbm->pbm_memattr;
763 if ((pbm->pbm_flags & PCIIO_BAR_MMAP_FIXED) != 0) {
764 addr = (uintptr_t)pbm->pbm_map_base;
767 if ((pbm->pbm_flags & PCIIO_BAR_MMAP_EXCL) != 0)
768 flags |= MAP_CHECK_EXCL;
769 error = vm_mmap_object(map, &addr, plen, prot, prot, flags, obj, 0,
772 vm_object_deallocate(obj);
775 pbm->pbm_map_base = (void *)addr;
776 pbm->pbm_map_length = plen;
777 pbm->pbm_bar_off = pm->pm_value - pbase;
778 pbm->pbm_bar_length = (pci_addr_t)1 << pm->pm_size;
786 pci_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
790 struct devlist *devlist_head;
791 struct pci_conf_io *cio = NULL;
792 struct pci_devinfo *dinfo;
794 struct pci_bar_io *bio;
795 struct pci_list_vpd_io *lvio;
796 struct pci_match_conf *pattern_buf;
798 struct pci_bar_mmap *pbm;
799 size_t confsz, iolen;
800 int error, ionum, i, num_patterns;
801 union pci_conf_union pcu;
803 struct pci_io iodata;
804 struct pci_io_old *io_old;
809 if (!(flag & FWRITE)) {
813 case PCIOCGETCONF_OLD:
814 #ifdef COMPAT_FREEBSD32
815 case PCIOCGETCONF_OLD32:
830 case PCIOCGETCONF_OLD:
831 #ifdef COMPAT_FREEBSD32
832 case PCIOCGETCONF_OLD32:
835 cio = malloc(sizeof(struct pci_conf_io), M_TEMP,
837 pci_conf_io_init(cio, data, cmd);
842 cio->num_matches = 0;
845 * If the user specified an offset into the device list,
846 * but the list has changed since they last called this
847 * ioctl, tell them that the list has changed. They will
848 * have to get the list from the beginning.
850 if ((cio->offset != 0)
851 && (cio->generation != pci_generation)){
852 cio->status = PCI_GETCONF_LIST_CHANGED;
858 * Check to see whether the user has asked for an offset
859 * past the end of our list.
861 if (cio->offset >= pci_numdevs) {
862 cio->status = PCI_GETCONF_LAST_DEVICE;
867 /* get the head of the device queue */
868 devlist_head = &pci_devq;
871 * Determine how much room we have for pci_conf structures.
872 * Round the user's buffer size down to the nearest
873 * multiple of sizeof(struct pci_conf) in case the user
874 * didn't specify a multiple of that size.
876 confsz = pci_conf_size(cmd);
877 iolen = min(cio->match_buf_len - (cio->match_buf_len % confsz),
878 pci_numdevs * confsz);
881 * Since we know that iolen is a multiple of the size of
882 * the pciconf union, it's okay to do this.
884 ionum = iolen / confsz;
887 * If this test is true, the user wants the pci_conf
888 * structures returned to match the supplied entries.
890 if ((cio->num_patterns > 0) && (cio->num_patterns < pci_numdevs)
891 && (cio->pat_buf_len > 0)) {
893 * pat_buf_len needs to be:
894 * num_patterns * sizeof(struct pci_match_conf)
895 * While it is certainly possible the user just
896 * allocated a large buffer, but set the number of
897 * matches correctly, it is far more likely that
898 * their kernel doesn't match the userland utility
899 * they're using. It's also possible that the user
900 * forgot to initialize some variables. Yes, this
901 * may be overly picky, but I hazard to guess that
902 * it's far more likely to just catch folks that
903 * updated their kernel but not their userland.
905 if (cio->num_patterns * pci_match_conf_size(cmd) !=
907 /* The user made a mistake, return an error. */
908 cio->status = PCI_GETCONF_ERROR;
914 * Allocate a buffer to hold the patterns.
916 pattern_buf = malloc(cio->pat_buf_len, M_TEMP,
918 error = copyin(cio->patterns, pattern_buf,
924 num_patterns = cio->num_patterns;
925 } else if ((cio->num_patterns > 0)
926 || (cio->pat_buf_len > 0)) {
928 * The user made a mistake, spit out an error.
930 cio->status = PCI_GETCONF_ERROR;
936 * Go through the list of devices and copy out the devices
937 * that match the user's criteria.
939 for (cio->num_matches = 0, i = 0,
940 dinfo = STAILQ_FIRST(devlist_head);
942 dinfo = STAILQ_NEXT(dinfo, pci_links), i++) {
947 /* Populate pd_name and pd_unit */
950 name = device_get_name(dinfo->cfg.dev);
952 strncpy(dinfo->conf.pd_name, name,
953 sizeof(dinfo->conf.pd_name));
954 dinfo->conf.pd_name[PCI_MAXNAMELEN] = 0;
955 dinfo->conf.pd_unit =
956 device_get_unit(dinfo->cfg.dev);
958 dinfo->conf.pd_name[0] = '\0';
959 dinfo->conf.pd_unit = 0;
962 if (pattern_buf == NULL ||
963 pci_conf_match(cmd, pattern_buf, num_patterns,
964 &dinfo->conf) == 0) {
966 * If we've filled up the user's buffer,
967 * break out at this point. Since we've
968 * got a match here, we'll pick right back
969 * up at the matching entry. We can also
970 * tell the user that there are more matches
973 if (cio->num_matches >= ionum) {
978 pci_conf_for_copyout(&dinfo->conf, &pcu, cmd);
979 error = copyout(&pcu,
980 (caddr_t)cio->matches +
981 confsz * cio->num_matches, confsz);
989 * Set the pointer into the list, so if the user is getting
990 * n records at a time, where n < pci_numdevs,
995 * Set the generation, the user will need this if they make
996 * another ioctl call with offset != 0.
998 cio->generation = pci_generation;
1001 * If this is the last device, inform the user so he won't
1002 * bother asking for more devices. If dinfo isn't NULL, we
1003 * know that there are more matches in the list because of
1004 * the way the traversal is done.
1007 cio->status = PCI_GETCONF_LAST_DEVICE;
1009 cio->status = PCI_GETCONF_MORE_DEVS;
1012 pci_conf_io_update_data(cio, data, cmd);
1014 free(pattern_buf, M_TEMP);
1020 case PCIOCWRITE_OLD:
1021 io_old = (struct pci_io_old *)data;
1022 iodata.pi_sel.pc_domain = 0;
1023 iodata.pi_sel.pc_bus = io_old->pi_sel.pc_bus;
1024 iodata.pi_sel.pc_dev = io_old->pi_sel.pc_dev;
1025 iodata.pi_sel.pc_func = io_old->pi_sel.pc_func;
1026 iodata.pi_reg = io_old->pi_reg;
1027 iodata.pi_width = io_old->pi_width;
1028 iodata.pi_data = io_old->pi_data;
1029 data = (caddr_t)&iodata;
1034 io = (struct pci_io *)data;
1035 switch(io->pi_width) {
1039 /* Make sure register is not negative and aligned. */
1040 if (io->pi_reg < 0 ||
1041 io->pi_reg & (io->pi_width - 1)) {
1046 * Assume that the user-level bus number is
1047 * in fact the physical PCI bus number.
1048 * Look up the grandparent, i.e. the bridge device,
1049 * so that we can issue configuration space cycles.
1051 pcidev = pci_find_dbsf(io->pi_sel.pc_domain,
1052 io->pi_sel.pc_bus, io->pi_sel.pc_dev,
1053 io->pi_sel.pc_func);
1056 if (cmd == PCIOCWRITE || cmd == PCIOCWRITE_OLD)
1058 if (cmd == PCIOCWRITE)
1060 pci_write_config(pcidev,
1065 else if (cmd == PCIOCREAD_OLD)
1067 pci_read_config(pcidev,
1073 pci_read_config(pcidev,
1078 #ifdef COMPAT_FREEBSD4
1079 if (cmd == PCIOCREAD_OLD) {
1080 io_old->pi_data = -1;
1094 bio = (struct pci_bar_io *)data;
1097 * Assume that the user-level bus number is
1098 * in fact the physical PCI bus number.
1100 pcidev = pci_find_dbsf(bio->pbi_sel.pc_domain,
1101 bio->pbi_sel.pc_bus, bio->pbi_sel.pc_dev,
1102 bio->pbi_sel.pc_func);
1103 if (pcidev == NULL) {
1107 pm = pci_find_bar(pcidev, bio->pbi_reg);
1112 bio->pbi_base = pm->pm_value;
1113 bio->pbi_length = (pci_addr_t)1 << pm->pm_size;
1114 bio->pbi_enabled = pci_bar_enabled(pcidev, pm);
1119 io = (struct pci_io *)data;
1120 pcidev = pci_find_dbsf(io->pi_sel.pc_domain, io->pi_sel.pc_bus,
1121 io->pi_sel.pc_dev, io->pi_sel.pc_func);
1123 io->pi_data = device_is_attached(pcidev);
1128 lvio = (struct pci_list_vpd_io *)data;
1131 * Assume that the user-level bus number is
1132 * in fact the physical PCI bus number.
1134 pcidev = pci_find_dbsf(lvio->plvi_sel.pc_domain,
1135 lvio->plvi_sel.pc_bus, lvio->plvi_sel.pc_dev,
1136 lvio->plvi_sel.pc_func);
1137 if (pcidev == NULL) {
1141 error = pci_list_vpd(pcidev, lvio);
1145 pbm = (struct pci_bar_mmap *)data;
1146 if ((flag & FWRITE) == 0 &&
1147 (pbm->pbm_flags & PCIIO_BAR_MMAP_RW) != 0)
1149 pcidev = pci_find_dbsf(pbm->pbm_sel.pc_domain,
1150 pbm->pbm_sel.pc_bus, pbm->pbm_sel.pc_dev,
1151 pbm->pbm_sel.pc_func);
1152 error = pcidev == NULL ? ENODEV : pci_bar_mmap(pcidev, pbm);