1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2011 NetApp, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/linker_set.h> 34 #include <sys/mman.h> 35 36 #include <ctype.h> 37 #include <err.h> 38 #include <errno.h> 39 #include <pthread.h> 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <strings.h> 44 #include <assert.h> 45 #include <stdbool.h> 46 #include <sysexits.h> 47 48 #include <machine/vmm.h> 49 #include <machine/vmm_snapshot.h> 50 #include <vmmapi.h> 51 52 #include "acpi.h" 53 #include "bhyverun.h" 54 #include "config.h" 55 #include "debug.h" 56 #include "inout.h" 57 #include "ioapic.h" 58 #include "mem.h" 59 #include "pci_emul.h" 60 #include "pci_irq.h" 61 #include "pci_lpc.h" 62 #include "pci_passthru.h" 63 #include "qemu_fwcfg.h" 64 65 #define CONF1_ADDR_PORT 0x0cf8 66 #define CONF1_DATA_PORT 0x0cfc 67 68 #define CONF1_ENABLE 0x80000000ul 69 70 #define MAXBUSES (PCI_BUSMAX + 1) 71 #define MAXSLOTS (PCI_SLOTMAX + 1) 72 #define MAXFUNCS (PCI_FUNCMAX + 1) 73 74 #define GB (1024 * 1024 * 1024UL) 75 76 struct funcinfo { 77 nvlist_t *fi_config; 78 struct pci_devemu *fi_pde; 79 struct pci_devinst *fi_devi; 80 }; 81 82 struct intxinfo { 83 int ii_count; 84 int ii_pirq_pin; 85 int ii_ioapic_irq; 86 }; 87 88 struct slotinfo { 89 struct intxinfo si_intpins[4]; 90 struct funcinfo si_funcs[MAXFUNCS]; 91 }; 92 93 struct businfo { 94 uint16_t iobase, iolimit; /* I/O window */ 95 uint32_t membase32, memlimit32; /* mmio window below 4GB */ 96 uint64_t membase64, memlimit64; /* mmio window above 4GB */ 97 struct slotinfo slotinfo[MAXSLOTS]; 98 }; 99 100 static struct businfo *pci_businfo[MAXBUSES]; 101 102 SET_DECLARE(pci_devemu_set, struct pci_devemu); 103 104 static uint64_t pci_emul_iobase; 105 static uint8_t *pci_emul_rombase; 106 static uint64_t pci_emul_romoffset; 107 static uint8_t *pci_emul_romlim; 108 static uint64_t pci_emul_membase32; 109 static uint64_t pci_emul_membase64; 110 static uint64_t pci_emul_memlim64; 111 112 struct pci_bar_allocation { 113 TAILQ_ENTRY(pci_bar_allocation) chain; 114 struct pci_devinst *pdi; 115 int idx; 116 enum pcibar_type type; 117 uint64_t size; 118 }; 119 120 static TAILQ_HEAD(pci_bar_list, pci_bar_allocation) pci_bars = 121 TAILQ_HEAD_INITIALIZER(pci_bars); 122 123 struct boot_device { 124 TAILQ_ENTRY(boot_device) boot_device_chain; 125 struct pci_devinst *pdi; 126 int bootindex; 127 }; 128 static TAILQ_HEAD(boot_list, boot_device) boot_devices = TAILQ_HEAD_INITIALIZER( 129 boot_devices); 130 131 #define PCI_EMUL_IOBASE 0x2000 132 #define PCI_EMUL_IOLIMIT 0x10000 133 134 #define PCI_EMUL_ROMSIZE 0x10000000 135 136 #define PCI_EMUL_ECFG_BASE 0xE0000000 /* 3.5GB */ 137 #define PCI_EMUL_ECFG_SIZE (MAXBUSES * 1024 * 1024) /* 1MB per bus */ 138 SYSRES_MEM(PCI_EMUL_ECFG_BASE, PCI_EMUL_ECFG_SIZE); 139 140 /* 141 * OVMF always uses 0xC0000000 as base address for 32 bit PCI MMIO. Don't 142 * change this address without changing it in OVMF. 143 */ 144 #define PCI_EMUL_MEMBASE32 0xC0000000 145 #define PCI_EMUL_MEMLIMIT32 PCI_EMUL_ECFG_BASE 146 #define PCI_EMUL_MEMSIZE64 (32*GB) 147 148 static struct pci_devemu *pci_emul_finddev(const char *name); 149 static void pci_lintr_route(struct pci_devinst *pi); 150 static void pci_lintr_update(struct pci_devinst *pi); 151 static void pci_cfgrw(int in, int bus, int slot, int func, int coff, 152 int bytes, uint32_t *val); 153 154 static __inline void 155 CFGWRITE(struct pci_devinst *pi, int coff, uint32_t val, int bytes) 156 { 157 158 if (bytes == 1) 159 pci_set_cfgdata8(pi, coff, val); 160 else if (bytes == 2) 161 pci_set_cfgdata16(pi, coff, val); 162 else 163 pci_set_cfgdata32(pi, coff, val); 164 } 165 166 static __inline uint32_t 167 CFGREAD(struct pci_devinst *pi, int coff, int bytes) 168 { 169 170 if (bytes == 1) 171 return (pci_get_cfgdata8(pi, coff)); 172 else if (bytes == 2) 173 return (pci_get_cfgdata16(pi, coff)); 174 else 175 return (pci_get_cfgdata32(pi, coff)); 176 } 177 178 static int 179 is_pcir_bar(int coff) 180 { 181 return (coff >= PCIR_BAR(0) && coff < PCIR_BAR(PCI_BARMAX + 1)); 182 } 183 184 static int 185 is_pcir_bios(int coff) 186 { 187 return (coff >= PCIR_BIOS && coff < PCIR_BIOS + 4); 188 } 189 190 /* 191 * I/O access 192 */ 193 194 /* 195 * Slot options are in the form: 196 * 197 * <bus>:<slot>:<func>,<emul>[,<config>] 198 * <slot>[:<func>],<emul>[,<config>] 199 * 200 * slot is 0..31 201 * func is 0..7 202 * emul is a string describing the type of PCI device e.g. virtio-net 203 * config is an optional string, depending on the device, that can be 204 * used for configuration. 205 * Examples are: 206 * 1,virtio-net,tap0 207 * 3:0,dummy 208 */ 209 static void 210 pci_parse_slot_usage(char *aopt) 211 { 212 213 EPRINTLN("Invalid PCI slot info field \"%s\"", aopt); 214 } 215 216 /* 217 * Helper function to parse a list of comma-separated options where 218 * each option is formatted as "name[=value]". If no value is 219 * provided, the option is treated as a boolean and is given a value 220 * of true. 221 */ 222 int 223 pci_parse_legacy_config(nvlist_t *nvl, const char *opt) 224 { 225 char *config, *name, *tofree, *value; 226 227 if (opt == NULL) 228 return (0); 229 230 config = tofree = strdup(opt); 231 while ((name = strsep(&config, ",")) != NULL) { 232 value = strchr(name, '='); 233 if (value != NULL) { 234 *value = '\0'; 235 value++; 236 set_config_value_node(nvl, name, value); 237 } else 238 set_config_bool_node(nvl, name, true); 239 } 240 free(tofree); 241 return (0); 242 } 243 244 /* 245 * PCI device configuration is stored in MIBs that encode the device's 246 * location: 247 * 248 * pci.<bus>.<slot>.<func> 249 * 250 * Where "bus", "slot", and "func" are all decimal values without 251 * leading zeroes. Each valid device must have a "device" node which 252 * identifies the driver model of the device. 253 * 254 * Device backends can provide a parser for the "config" string. If 255 * a custom parser is not provided, pci_parse_legacy_config() is used 256 * to parse the string. 257 */ 258 int 259 pci_parse_slot(char *opt) 260 { 261 char node_name[sizeof("pci.XXX.XX.X")]; 262 struct pci_devemu *pde; 263 char *emul, *config, *str, *cp; 264 int error, bnum, snum, fnum; 265 nvlist_t *nvl; 266 267 error = -1; 268 str = strdup(opt); 269 270 emul = config = NULL; 271 if ((cp = strchr(str, ',')) != NULL) { 272 *cp = '\0'; 273 emul = cp + 1; 274 if ((cp = strchr(emul, ',')) != NULL) { 275 *cp = '\0'; 276 config = cp + 1; 277 } 278 } else { 279 pci_parse_slot_usage(opt); 280 goto done; 281 } 282 283 /* <bus>:<slot>:<func> */ 284 if (sscanf(str, "%d:%d:%d", &bnum, &snum, &fnum) != 3) { 285 bnum = 0; 286 /* <slot>:<func> */ 287 if (sscanf(str, "%d:%d", &snum, &fnum) != 2) { 288 fnum = 0; 289 /* <slot> */ 290 if (sscanf(str, "%d", &snum) != 1) { 291 snum = -1; 292 } 293 } 294 } 295 296 if (bnum < 0 || bnum >= MAXBUSES || snum < 0 || snum >= MAXSLOTS || 297 fnum < 0 || fnum >= MAXFUNCS) { 298 pci_parse_slot_usage(opt); 299 goto done; 300 } 301 302 pde = pci_emul_finddev(emul); 303 if (pde == NULL) { 304 EPRINTLN("pci slot %d:%d:%d: unknown device \"%s\"", bnum, snum, 305 fnum, emul); 306 goto done; 307 } 308 309 snprintf(node_name, sizeof(node_name), "pci.%d.%d.%d", bnum, snum, 310 fnum); 311 nvl = find_config_node(node_name); 312 if (nvl != NULL) { 313 EPRINTLN("pci slot %d:%d:%d already occupied!", bnum, snum, 314 fnum); 315 goto done; 316 } 317 nvl = create_config_node(node_name); 318 if (pde->pe_alias != NULL) 319 set_config_value_node(nvl, "device", pde->pe_alias); 320 else 321 set_config_value_node(nvl, "device", pde->pe_emu); 322 323 if (pde->pe_legacy_config != NULL) 324 error = pde->pe_legacy_config(nvl, config); 325 else 326 error = pci_parse_legacy_config(nvl, config); 327 done: 328 free(str); 329 return (error); 330 } 331 332 void 333 pci_print_supported_devices(void) 334 { 335 struct pci_devemu **pdpp, *pdp; 336 337 SET_FOREACH(pdpp, pci_devemu_set) { 338 pdp = *pdpp; 339 printf("%s\n", pdp->pe_emu); 340 } 341 } 342 343 uint32_t 344 pci_config_read_reg(const struct pcisel *const host_sel, nvlist_t *nvl, 345 const uint32_t reg, const uint8_t size, const uint32_t def) 346 { 347 const char *config; 348 const nvlist_t *pci_regs; 349 350 assert(size == 1 || size == 2 || size == 4); 351 352 pci_regs = find_relative_config_node(nvl, "pcireg"); 353 if (pci_regs == NULL) { 354 return def; 355 } 356 357 switch (reg) { 358 case PCIR_DEVICE: 359 config = get_config_value_node(pci_regs, "device"); 360 break; 361 case PCIR_VENDOR: 362 config = get_config_value_node(pci_regs, "vendor"); 363 break; 364 case PCIR_REVID: 365 config = get_config_value_node(pci_regs, "revid"); 366 break; 367 case PCIR_SUBVEND_0: 368 config = get_config_value_node(pci_regs, "subvendor"); 369 break; 370 case PCIR_SUBDEV_0: 371 config = get_config_value_node(pci_regs, "subdevice"); 372 break; 373 default: 374 return (-1); 375 } 376 377 if (config == NULL) { 378 return def; 379 } else if (host_sel != NULL && strcmp(config, "host") == 0) { 380 return read_config(host_sel, reg, size); 381 } else { 382 return strtol(config, NULL, 16); 383 } 384 } 385 386 static int 387 pci_valid_pba_offset(struct pci_devinst *pi, uint64_t offset) 388 { 389 390 if (offset < pi->pi_msix.pba_offset) 391 return (0); 392 393 if (offset >= pi->pi_msix.pba_offset + pi->pi_msix.pba_size) { 394 return (0); 395 } 396 397 return (1); 398 } 399 400 int 401 pci_emul_msix_twrite(struct pci_devinst *pi, uint64_t offset, int size, 402 uint64_t value) 403 { 404 int msix_entry_offset; 405 int tab_index; 406 char *dest; 407 408 /* support only 4 or 8 byte writes */ 409 if (size != 4 && size != 8) 410 return (-1); 411 412 /* 413 * Return if table index is beyond what device supports 414 */ 415 tab_index = offset / MSIX_TABLE_ENTRY_SIZE; 416 if (tab_index >= pi->pi_msix.table_count) 417 return (-1); 418 419 msix_entry_offset = offset % MSIX_TABLE_ENTRY_SIZE; 420 421 /* support only aligned writes */ 422 if ((msix_entry_offset % size) != 0) 423 return (-1); 424 425 dest = (char *)(pi->pi_msix.table + tab_index); 426 dest += msix_entry_offset; 427 428 if (size == 4) 429 *((uint32_t *)dest) = value; 430 else 431 *((uint64_t *)dest) = value; 432 433 return (0); 434 } 435 436 uint64_t 437 pci_emul_msix_tread(struct pci_devinst *pi, uint64_t offset, int size) 438 { 439 char *dest; 440 int msix_entry_offset; 441 int tab_index; 442 uint64_t retval = ~0; 443 444 /* 445 * The PCI standard only allows 4 and 8 byte accesses to the MSI-X 446 * table but we also allow 1 byte access to accommodate reads from 447 * ddb. 448 */ 449 if (size != 1 && size != 4 && size != 8) 450 return (retval); 451 452 msix_entry_offset = offset % MSIX_TABLE_ENTRY_SIZE; 453 454 /* support only aligned reads */ 455 if ((msix_entry_offset % size) != 0) { 456 return (retval); 457 } 458 459 tab_index = offset / MSIX_TABLE_ENTRY_SIZE; 460 461 if (tab_index < pi->pi_msix.table_count) { 462 /* valid MSI-X Table access */ 463 dest = (char *)(pi->pi_msix.table + tab_index); 464 dest += msix_entry_offset; 465 466 if (size == 1) 467 retval = *((uint8_t *)dest); 468 else if (size == 4) 469 retval = *((uint32_t *)dest); 470 else 471 retval = *((uint64_t *)dest); 472 } else if (pci_valid_pba_offset(pi, offset)) { 473 /* return 0 for PBA access */ 474 retval = 0; 475 } 476 477 return (retval); 478 } 479 480 int 481 pci_msix_table_bar(struct pci_devinst *pi) 482 { 483 484 if (pi->pi_msix.table != NULL) 485 return (pi->pi_msix.table_bar); 486 else 487 return (-1); 488 } 489 490 int 491 pci_msix_pba_bar(struct pci_devinst *pi) 492 { 493 494 if (pi->pi_msix.table != NULL) 495 return (pi->pi_msix.pba_bar); 496 else 497 return (-1); 498 } 499 500 static int 501 pci_emul_io_handler(struct vmctx *ctx __unused, int in, int port, 502 int bytes, uint32_t *eax, void *arg) 503 { 504 struct pci_devinst *pdi = arg; 505 struct pci_devemu *pe = pdi->pi_d; 506 uint64_t offset; 507 int i; 508 509 assert(port >= 0); 510 511 for (i = 0; i <= PCI_BARMAX; i++) { 512 if (pdi->pi_bar[i].type == PCIBAR_IO && 513 (uint64_t)port >= pdi->pi_bar[i].addr && 514 (uint64_t)port + bytes <= 515 pdi->pi_bar[i].addr + pdi->pi_bar[i].size) { 516 offset = port - pdi->pi_bar[i].addr; 517 if (in) 518 *eax = (*pe->pe_barread)(pdi, i, 519 offset, bytes); 520 else 521 (*pe->pe_barwrite)(pdi, i, offset, 522 bytes, *eax); 523 return (0); 524 } 525 } 526 return (-1); 527 } 528 529 static int 530 pci_emul_mem_handler(struct vcpu *vcpu __unused, int dir, 531 uint64_t addr, int size, uint64_t *val, void *arg1, long arg2) 532 { 533 struct pci_devinst *pdi = arg1; 534 struct pci_devemu *pe = pdi->pi_d; 535 uint64_t offset; 536 int bidx = (int) arg2; 537 538 assert(bidx <= PCI_BARMAX); 539 assert(pdi->pi_bar[bidx].type == PCIBAR_MEM32 || 540 pdi->pi_bar[bidx].type == PCIBAR_MEM64); 541 assert(addr >= pdi->pi_bar[bidx].addr && 542 addr + size <= pdi->pi_bar[bidx].addr + pdi->pi_bar[bidx].size); 543 544 offset = addr - pdi->pi_bar[bidx].addr; 545 546 if (dir == MEM_F_WRITE) { 547 if (size == 8) { 548 (*pe->pe_barwrite)(pdi, bidx, offset, 549 4, *val & 0xffffffff); 550 (*pe->pe_barwrite)(pdi, bidx, offset + 4, 551 4, *val >> 32); 552 } else { 553 (*pe->pe_barwrite)(pdi, bidx, offset, 554 size, *val); 555 } 556 } else { 557 if (size == 8) { 558 *val = (*pe->pe_barread)(pdi, bidx, 559 offset, 4); 560 *val |= (*pe->pe_barread)(pdi, bidx, 561 offset + 4, 4) << 32; 562 } else { 563 *val = (*pe->pe_barread)(pdi, bidx, 564 offset, size); 565 } 566 } 567 568 return (0); 569 } 570 571 572 static int 573 pci_emul_alloc_resource(uint64_t *baseptr, uint64_t limit, uint64_t size, 574 uint64_t *addr) 575 { 576 uint64_t base; 577 578 assert((size & (size - 1)) == 0); /* must be a power of 2 */ 579 580 base = roundup2(*baseptr, size); 581 582 if (base + size <= limit) { 583 *addr = base; 584 *baseptr = base + size; 585 return (0); 586 } else 587 return (-1); 588 } 589 590 /* 591 * Register (or unregister) the MMIO or I/O region associated with the BAR 592 * register 'idx' of an emulated pci device. 593 */ 594 static void 595 modify_bar_registration(struct pci_devinst *pi, int idx, int registration) 596 { 597 struct pci_devemu *pe; 598 int error; 599 struct inout_port iop; 600 struct mem_range mr; 601 602 pe = pi->pi_d; 603 switch (pi->pi_bar[idx].type) { 604 case PCIBAR_IO: 605 bzero(&iop, sizeof(struct inout_port)); 606 iop.name = pi->pi_name; 607 iop.port = pi->pi_bar[idx].addr; 608 iop.size = pi->pi_bar[idx].size; 609 if (registration) { 610 iop.flags = IOPORT_F_INOUT; 611 iop.handler = pci_emul_io_handler; 612 iop.arg = pi; 613 error = register_inout(&iop); 614 } else 615 error = unregister_inout(&iop); 616 break; 617 case PCIBAR_MEM32: 618 case PCIBAR_MEM64: 619 bzero(&mr, sizeof(struct mem_range)); 620 mr.name = pi->pi_name; 621 mr.base = pi->pi_bar[idx].addr; 622 mr.size = pi->pi_bar[idx].size; 623 if (registration) { 624 mr.flags = MEM_F_RW; 625 mr.handler = pci_emul_mem_handler; 626 mr.arg1 = pi; 627 mr.arg2 = idx; 628 error = register_mem(&mr); 629 } else 630 error = unregister_mem(&mr); 631 break; 632 case PCIBAR_ROM: 633 error = 0; 634 break; 635 default: 636 error = EINVAL; 637 break; 638 } 639 assert(error == 0); 640 641 if (pe->pe_baraddr != NULL) 642 (*pe->pe_baraddr)(pi, idx, registration, pi->pi_bar[idx].addr); 643 } 644 645 static void 646 unregister_bar(struct pci_devinst *pi, int idx) 647 { 648 649 modify_bar_registration(pi, idx, 0); 650 } 651 652 static void 653 register_bar(struct pci_devinst *pi, int idx) 654 { 655 656 modify_bar_registration(pi, idx, 1); 657 } 658 659 /* Is the ROM enabled for the emulated pci device? */ 660 static int 661 romen(struct pci_devinst *pi) 662 { 663 return (pi->pi_bar[PCI_ROM_IDX].lobits & PCIM_BIOS_ENABLE) == 664 PCIM_BIOS_ENABLE; 665 } 666 667 /* Are we decoding i/o port accesses for the emulated pci device? */ 668 static int 669 porten(struct pci_devinst *pi) 670 { 671 uint16_t cmd; 672 673 cmd = pci_get_cfgdata16(pi, PCIR_COMMAND); 674 675 return (cmd & PCIM_CMD_PORTEN); 676 } 677 678 /* Are we decoding memory accesses for the emulated pci device? */ 679 static int 680 memen(struct pci_devinst *pi) 681 { 682 uint16_t cmd; 683 684 cmd = pci_get_cfgdata16(pi, PCIR_COMMAND); 685 686 return (cmd & PCIM_CMD_MEMEN); 687 } 688 689 /* 690 * Update the MMIO or I/O address that is decoded by the BAR register. 691 * 692 * If the pci device has enabled the address space decoding then intercept 693 * the address range decoded by the BAR register. 694 */ 695 static void 696 update_bar_address(struct pci_devinst *pi, uint64_t addr, int idx, int type) 697 { 698 int decode; 699 700 if (pi->pi_bar[idx].type == PCIBAR_IO) 701 decode = porten(pi); 702 else 703 decode = memen(pi); 704 705 if (decode) 706 unregister_bar(pi, idx); 707 708 switch (type) { 709 case PCIBAR_IO: 710 case PCIBAR_MEM32: 711 pi->pi_bar[idx].addr = addr; 712 break; 713 case PCIBAR_MEM64: 714 pi->pi_bar[idx].addr &= ~0xffffffffUL; 715 pi->pi_bar[idx].addr |= addr; 716 break; 717 case PCIBAR_MEMHI64: 718 pi->pi_bar[idx].addr &= 0xffffffff; 719 pi->pi_bar[idx].addr |= addr; 720 break; 721 default: 722 assert(0); 723 } 724 725 if (decode) 726 register_bar(pi, idx); 727 } 728 729 int 730 pci_emul_alloc_bar(struct pci_devinst *pdi, int idx, enum pcibar_type type, 731 uint64_t size) 732 { 733 assert((type == PCIBAR_ROM) || (idx >= 0 && idx <= PCI_BARMAX)); 734 assert((type != PCIBAR_ROM) || (idx == PCI_ROM_IDX)); 735 736 if ((size & (size - 1)) != 0) 737 size = 1UL << flsl(size); /* round up to a power of 2 */ 738 739 /* Enforce minimum BAR sizes required by the PCI standard */ 740 if (type == PCIBAR_IO) { 741 if (size < 4) 742 size = 4; 743 } else if (type == PCIBAR_ROM) { 744 if (size < ~PCIM_BIOS_ADDR_MASK + 1) 745 size = ~PCIM_BIOS_ADDR_MASK + 1; 746 } else { 747 if (size < 16) 748 size = 16; 749 } 750 751 /* 752 * To reduce fragmentation of the MMIO space, we allocate the BARs by 753 * size. Therefore, don't allocate the BAR yet. We create a list of all 754 * BAR allocation which is sorted by BAR size. When all PCI devices are 755 * initialized, we will assign an address to the BARs. 756 */ 757 758 /* create a new list entry */ 759 struct pci_bar_allocation *const new_bar = malloc(sizeof(*new_bar)); 760 memset(new_bar, 0, sizeof(*new_bar)); 761 new_bar->pdi = pdi; 762 new_bar->idx = idx; 763 new_bar->type = type; 764 new_bar->size = size; 765 766 /* 767 * Search for a BAR which size is lower than the size of our newly 768 * allocated BAR. 769 */ 770 struct pci_bar_allocation *bar = NULL; 771 TAILQ_FOREACH(bar, &pci_bars, chain) { 772 if (bar->size < size) { 773 break; 774 } 775 } 776 777 if (bar == NULL) { 778 /* 779 * Either the list is empty or new BAR is the smallest BAR of 780 * the list. Append it to the end of our list. 781 */ 782 TAILQ_INSERT_TAIL(&pci_bars, new_bar, chain); 783 } else { 784 /* 785 * The found BAR is smaller than our new BAR. For that reason, 786 * insert our new BAR before the found BAR. 787 */ 788 TAILQ_INSERT_BEFORE(bar, new_bar, chain); 789 } 790 791 /* 792 * pci_passthru devices synchronize their physical and virtual command 793 * register on init. For that reason, the virtual cmd reg should be 794 * updated as early as possible. 795 */ 796 uint16_t enbit = 0; 797 switch (type) { 798 case PCIBAR_IO: 799 enbit = PCIM_CMD_PORTEN; 800 break; 801 case PCIBAR_MEM64: 802 case PCIBAR_MEM32: 803 enbit = PCIM_CMD_MEMEN; 804 break; 805 default: 806 enbit = 0; 807 break; 808 } 809 810 const uint16_t cmd = pci_get_cfgdata16(pdi, PCIR_COMMAND); 811 pci_set_cfgdata16(pdi, PCIR_COMMAND, cmd | enbit); 812 813 return (0); 814 } 815 816 static int 817 pci_emul_assign_bar(struct pci_devinst *const pdi, const int idx, 818 const enum pcibar_type type, const uint64_t size) 819 { 820 int error; 821 uint64_t *baseptr, limit, addr, mask, lobits, bar; 822 823 switch (type) { 824 case PCIBAR_NONE: 825 baseptr = NULL; 826 addr = mask = lobits = 0; 827 break; 828 case PCIBAR_IO: 829 baseptr = &pci_emul_iobase; 830 limit = PCI_EMUL_IOLIMIT; 831 mask = PCIM_BAR_IO_BASE; 832 lobits = PCIM_BAR_IO_SPACE; 833 break; 834 case PCIBAR_MEM64: 835 /* 836 * XXX 837 * Some drivers do not work well if the 64-bit BAR is allocated 838 * above 4GB. Allow for this by allocating small requests under 839 * 4GB unless then allocation size is larger than some arbitrary 840 * number (128MB currently). 841 */ 842 if (size > 128 * 1024 * 1024) { 843 baseptr = &pci_emul_membase64; 844 limit = pci_emul_memlim64; 845 mask = PCIM_BAR_MEM_BASE; 846 lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64 | 847 PCIM_BAR_MEM_PREFETCH; 848 } else { 849 baseptr = &pci_emul_membase32; 850 limit = PCI_EMUL_MEMLIMIT32; 851 mask = PCIM_BAR_MEM_BASE; 852 lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64; 853 } 854 break; 855 case PCIBAR_MEM32: 856 baseptr = &pci_emul_membase32; 857 limit = PCI_EMUL_MEMLIMIT32; 858 mask = PCIM_BAR_MEM_BASE; 859 lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_32; 860 break; 861 case PCIBAR_ROM: 862 /* do not claim memory for ROM. OVMF will do it for us. */ 863 baseptr = NULL; 864 limit = 0; 865 mask = PCIM_BIOS_ADDR_MASK; 866 lobits = 0; 867 break; 868 default: 869 printf("pci_emul_alloc_base: invalid bar type %d\n", type); 870 assert(0); 871 } 872 873 if (baseptr != NULL) { 874 error = pci_emul_alloc_resource(baseptr, limit, size, &addr); 875 if (error != 0) 876 return (error); 877 } else { 878 addr = 0; 879 } 880 881 pdi->pi_bar[idx].type = type; 882 pdi->pi_bar[idx].addr = addr; 883 pdi->pi_bar[idx].size = size; 884 /* 885 * passthru devices are using same lobits as physical device they set 886 * this property 887 */ 888 if (pdi->pi_bar[idx].lobits != 0) { 889 lobits = pdi->pi_bar[idx].lobits; 890 } else { 891 pdi->pi_bar[idx].lobits = lobits; 892 } 893 894 /* Initialize the BAR register in config space */ 895 bar = (addr & mask) | lobits; 896 pci_set_cfgdata32(pdi, PCIR_BAR(idx), bar); 897 898 if (type == PCIBAR_MEM64) { 899 assert(idx + 1 <= PCI_BARMAX); 900 pdi->pi_bar[idx + 1].type = PCIBAR_MEMHI64; 901 pci_set_cfgdata32(pdi, PCIR_BAR(idx + 1), bar >> 32); 902 } 903 904 if (type != PCIBAR_ROM) { 905 register_bar(pdi, idx); 906 } 907 908 return (0); 909 } 910 911 int 912 pci_emul_alloc_rom(struct pci_devinst *const pdi, const uint64_t size, 913 void **const addr) 914 { 915 /* allocate ROM space once on first call */ 916 if (pci_emul_rombase == 0) { 917 pci_emul_rombase = vm_create_devmem(pdi->pi_vmctx, VM_PCIROM, 918 "pcirom", PCI_EMUL_ROMSIZE); 919 if (pci_emul_rombase == MAP_FAILED) { 920 warnx("%s: failed to create rom segment", __func__); 921 return (-1); 922 } 923 pci_emul_romlim = pci_emul_rombase + PCI_EMUL_ROMSIZE; 924 pci_emul_romoffset = 0; 925 } 926 927 /* ROM size should be a power of 2 and greater than 2 KB */ 928 const uint64_t rom_size = MAX(1UL << flsl(size), 929 ~PCIM_BIOS_ADDR_MASK + 1); 930 931 /* check if ROM fits into ROM space */ 932 if (pci_emul_romoffset + rom_size > PCI_EMUL_ROMSIZE) { 933 warnx("%s: no space left in rom segment:", __func__); 934 warnx("%16lu bytes left", 935 PCI_EMUL_ROMSIZE - pci_emul_romoffset); 936 warnx("%16lu bytes required by %d/%d/%d", rom_size, pdi->pi_bus, 937 pdi->pi_slot, pdi->pi_func); 938 return (-1); 939 } 940 941 /* allocate ROM BAR */ 942 const int error = pci_emul_alloc_bar(pdi, PCI_ROM_IDX, PCIBAR_ROM, 943 rom_size); 944 if (error) 945 return error; 946 947 /* return address */ 948 *addr = pci_emul_rombase + pci_emul_romoffset; 949 950 /* save offset into ROM Space */ 951 pdi->pi_romoffset = pci_emul_romoffset; 952 953 /* increase offset for next ROM */ 954 pci_emul_romoffset += rom_size; 955 956 return (0); 957 } 958 959 int 960 pci_emul_add_boot_device(struct pci_devinst *pi, int bootindex) 961 { 962 struct boot_device *new_device, *device; 963 964 /* don't permit a negative bootindex */ 965 if (bootindex < 0) { 966 errx(4, "Invalid bootindex %d for %s", bootindex, pi->pi_name); 967 } 968 969 /* alloc new boot device */ 970 new_device = calloc(1, sizeof(struct boot_device)); 971 if (new_device == NULL) { 972 return (ENOMEM); 973 } 974 new_device->pdi = pi; 975 new_device->bootindex = bootindex; 976 977 /* search for boot device with higher boot index */ 978 TAILQ_FOREACH(device, &boot_devices, boot_device_chain) { 979 if (device->bootindex == bootindex) { 980 errx(4, 981 "Could not set bootindex %d for %s. Bootindex already occupied by %s", 982 bootindex, pi->pi_name, device->pdi->pi_name); 983 } else if (device->bootindex > bootindex) { 984 break; 985 } 986 } 987 988 /* add boot device to queue */ 989 if (device == NULL) { 990 TAILQ_INSERT_TAIL(&boot_devices, new_device, boot_device_chain); 991 } else { 992 TAILQ_INSERT_BEFORE(device, new_device, boot_device_chain); 993 } 994 995 return (0); 996 } 997 998 #define CAP_START_OFFSET 0x40 999 static int 1000 pci_emul_add_capability(struct pci_devinst *pi, u_char *capdata, int caplen) 1001 { 1002 int i, capoff, reallen; 1003 uint16_t sts; 1004 1005 assert(caplen > 0); 1006 1007 reallen = roundup2(caplen, 4); /* dword aligned */ 1008 1009 sts = pci_get_cfgdata16(pi, PCIR_STATUS); 1010 if ((sts & PCIM_STATUS_CAPPRESENT) == 0) 1011 capoff = CAP_START_OFFSET; 1012 else 1013 capoff = pi->pi_capend + 1; 1014 1015 /* Check if we have enough space */ 1016 if (capoff + reallen > PCI_REGMAX + 1) 1017 return (-1); 1018 1019 /* Set the previous capability pointer */ 1020 if ((sts & PCIM_STATUS_CAPPRESENT) == 0) { 1021 pci_set_cfgdata8(pi, PCIR_CAP_PTR, capoff); 1022 pci_set_cfgdata16(pi, PCIR_STATUS, sts|PCIM_STATUS_CAPPRESENT); 1023 } else 1024 pci_set_cfgdata8(pi, pi->pi_prevcap + 1, capoff); 1025 1026 /* Copy the capability */ 1027 for (i = 0; i < caplen; i++) 1028 pci_set_cfgdata8(pi, capoff + i, capdata[i]); 1029 1030 /* Set the next capability pointer */ 1031 pci_set_cfgdata8(pi, capoff + 1, 0); 1032 1033 pi->pi_prevcap = capoff; 1034 pi->pi_capend = capoff + reallen - 1; 1035 return (0); 1036 } 1037 1038 static struct pci_devemu * 1039 pci_emul_finddev(const char *name) 1040 { 1041 struct pci_devemu **pdpp, *pdp; 1042 1043 SET_FOREACH(pdpp, pci_devemu_set) { 1044 pdp = *pdpp; 1045 if (!strcmp(pdp->pe_emu, name)) { 1046 return (pdp); 1047 } 1048 } 1049 1050 return (NULL); 1051 } 1052 1053 static int 1054 pci_emul_init(struct vmctx *ctx, struct pci_devemu *pde, int bus, int slot, 1055 int func, struct funcinfo *fi) 1056 { 1057 struct pci_devinst *pdi; 1058 int err; 1059 1060 pdi = calloc(1, sizeof(struct pci_devinst)); 1061 1062 pdi->pi_vmctx = ctx; 1063 pdi->pi_bus = bus; 1064 pdi->pi_slot = slot; 1065 pdi->pi_func = func; 1066 pthread_mutex_init(&pdi->pi_lintr.lock, NULL); 1067 pdi->pi_lintr.pin = 0; 1068 pdi->pi_lintr.state = IDLE; 1069 pdi->pi_lintr.pirq_pin = 0; 1070 pdi->pi_lintr.ioapic_irq = 0; 1071 pdi->pi_d = pde; 1072 snprintf(pdi->pi_name, PI_NAMESZ, "%s@pci.%d.%d.%d", pde->pe_emu, bus, 1073 slot, func); 1074 1075 /* Disable legacy interrupts */ 1076 pci_set_cfgdata8(pdi, PCIR_INTLINE, 255); 1077 pci_set_cfgdata8(pdi, PCIR_INTPIN, 0); 1078 1079 pci_set_cfgdata8(pdi, PCIR_COMMAND, PCIM_CMD_BUSMASTEREN); 1080 1081 err = (*pde->pe_init)(pdi, fi->fi_config); 1082 if (err == 0) 1083 fi->fi_devi = pdi; 1084 else 1085 free(pdi); 1086 1087 return (err); 1088 } 1089 1090 void 1091 pci_populate_msicap(struct msicap *msicap, int msgnum, int nextptr) 1092 { 1093 int mmc; 1094 1095 /* Number of msi messages must be a power of 2 between 1 and 32 */ 1096 assert((msgnum & (msgnum - 1)) == 0 && msgnum >= 1 && msgnum <= 32); 1097 mmc = ffs(msgnum) - 1; 1098 1099 bzero(msicap, sizeof(struct msicap)); 1100 msicap->capid = PCIY_MSI; 1101 msicap->nextptr = nextptr; 1102 msicap->msgctrl = PCIM_MSICTRL_64BIT | (mmc << 1); 1103 } 1104 1105 int 1106 pci_emul_add_msicap(struct pci_devinst *pi, int msgnum) 1107 { 1108 struct msicap msicap; 1109 1110 pci_populate_msicap(&msicap, msgnum, 0); 1111 1112 return (pci_emul_add_capability(pi, (u_char *)&msicap, sizeof(msicap))); 1113 } 1114 1115 static void 1116 pci_populate_msixcap(struct msixcap *msixcap, int msgnum, int barnum, 1117 uint32_t msix_tab_size) 1118 { 1119 1120 assert(msix_tab_size % 4096 == 0); 1121 1122 bzero(msixcap, sizeof(struct msixcap)); 1123 msixcap->capid = PCIY_MSIX; 1124 1125 /* 1126 * Message Control Register, all fields set to 1127 * zero except for the Table Size. 1128 * Note: Table size N is encoded as N-1 1129 */ 1130 msixcap->msgctrl = msgnum - 1; 1131 1132 /* 1133 * MSI-X BAR setup: 1134 * - MSI-X table start at offset 0 1135 * - PBA table starts at a 4K aligned offset after the MSI-X table 1136 */ 1137 msixcap->table_info = barnum & PCIM_MSIX_BIR_MASK; 1138 msixcap->pba_info = msix_tab_size | (barnum & PCIM_MSIX_BIR_MASK); 1139 } 1140 1141 static void 1142 pci_msix_table_init(struct pci_devinst *pi, int table_entries) 1143 { 1144 int i, table_size; 1145 1146 assert(table_entries > 0); 1147 assert(table_entries <= MAX_MSIX_TABLE_ENTRIES); 1148 1149 table_size = table_entries * MSIX_TABLE_ENTRY_SIZE; 1150 pi->pi_msix.table = calloc(1, table_size); 1151 1152 /* set mask bit of vector control register */ 1153 for (i = 0; i < table_entries; i++) 1154 pi->pi_msix.table[i].vector_control |= PCIM_MSIX_VCTRL_MASK; 1155 } 1156 1157 int 1158 pci_emul_add_msixcap(struct pci_devinst *pi, int msgnum, int barnum) 1159 { 1160 uint32_t tab_size; 1161 struct msixcap msixcap; 1162 1163 assert(msgnum >= 1 && msgnum <= MAX_MSIX_TABLE_ENTRIES); 1164 assert(barnum >= 0 && barnum <= PCIR_MAX_BAR_0); 1165 1166 tab_size = msgnum * MSIX_TABLE_ENTRY_SIZE; 1167 1168 /* Align table size to nearest 4K */ 1169 tab_size = roundup2(tab_size, 4096); 1170 1171 pi->pi_msix.table_bar = barnum; 1172 pi->pi_msix.pba_bar = barnum; 1173 pi->pi_msix.table_offset = 0; 1174 pi->pi_msix.table_count = msgnum; 1175 pi->pi_msix.pba_offset = tab_size; 1176 pi->pi_msix.pba_size = PBA_SIZE(msgnum); 1177 1178 pci_msix_table_init(pi, msgnum); 1179 1180 pci_populate_msixcap(&msixcap, msgnum, barnum, tab_size); 1181 1182 /* allocate memory for MSI-X Table and PBA */ 1183 pci_emul_alloc_bar(pi, barnum, PCIBAR_MEM32, 1184 tab_size + pi->pi_msix.pba_size); 1185 1186 return (pci_emul_add_capability(pi, (u_char *)&msixcap, 1187 sizeof(msixcap))); 1188 } 1189 1190 static void 1191 msixcap_cfgwrite(struct pci_devinst *pi, int capoff, int offset, 1192 int bytes, uint32_t val) 1193 { 1194 uint16_t msgctrl, rwmask; 1195 int off; 1196 1197 off = offset - capoff; 1198 /* Message Control Register */ 1199 if (off == 2 && bytes == 2) { 1200 rwmask = PCIM_MSIXCTRL_MSIX_ENABLE | PCIM_MSIXCTRL_FUNCTION_MASK; 1201 msgctrl = pci_get_cfgdata16(pi, offset); 1202 msgctrl &= ~rwmask; 1203 msgctrl |= val & rwmask; 1204 val = msgctrl; 1205 1206 pi->pi_msix.enabled = val & PCIM_MSIXCTRL_MSIX_ENABLE; 1207 pi->pi_msix.function_mask = val & PCIM_MSIXCTRL_FUNCTION_MASK; 1208 pci_lintr_update(pi); 1209 } 1210 1211 CFGWRITE(pi, offset, val, bytes); 1212 } 1213 1214 static void 1215 msicap_cfgwrite(struct pci_devinst *pi, int capoff, int offset, 1216 int bytes, uint32_t val) 1217 { 1218 uint16_t msgctrl, rwmask, msgdata, mme; 1219 uint32_t addrlo; 1220 1221 /* 1222 * If guest is writing to the message control register make sure 1223 * we do not overwrite read-only fields. 1224 */ 1225 if ((offset - capoff) == 2 && bytes == 2) { 1226 rwmask = PCIM_MSICTRL_MME_MASK | PCIM_MSICTRL_MSI_ENABLE; 1227 msgctrl = pci_get_cfgdata16(pi, offset); 1228 msgctrl &= ~rwmask; 1229 msgctrl |= val & rwmask; 1230 val = msgctrl; 1231 } 1232 CFGWRITE(pi, offset, val, bytes); 1233 1234 msgctrl = pci_get_cfgdata16(pi, capoff + 2); 1235 addrlo = pci_get_cfgdata32(pi, capoff + 4); 1236 if (msgctrl & PCIM_MSICTRL_64BIT) 1237 msgdata = pci_get_cfgdata16(pi, capoff + 12); 1238 else 1239 msgdata = pci_get_cfgdata16(pi, capoff + 8); 1240 1241 mme = msgctrl & PCIM_MSICTRL_MME_MASK; 1242 pi->pi_msi.enabled = msgctrl & PCIM_MSICTRL_MSI_ENABLE ? 1 : 0; 1243 if (pi->pi_msi.enabled) { 1244 pi->pi_msi.addr = addrlo; 1245 pi->pi_msi.msg_data = msgdata; 1246 pi->pi_msi.maxmsgnum = 1 << (mme >> 4); 1247 } else { 1248 pi->pi_msi.maxmsgnum = 0; 1249 } 1250 pci_lintr_update(pi); 1251 } 1252 1253 static void 1254 pciecap_cfgwrite(struct pci_devinst *pi, int capoff __unused, int offset, 1255 int bytes, uint32_t val) 1256 { 1257 1258 /* XXX don't write to the readonly parts */ 1259 CFGWRITE(pi, offset, val, bytes); 1260 } 1261 1262 #define PCIECAP_VERSION 0x2 1263 int 1264 pci_emul_add_pciecap(struct pci_devinst *pi, int type) 1265 { 1266 int err; 1267 struct pciecap pciecap; 1268 1269 bzero(&pciecap, sizeof(pciecap)); 1270 1271 /* 1272 * Use the integrated endpoint type for endpoints on a root complex bus. 1273 * 1274 * NB: bhyve currently only supports a single PCI bus that is the root 1275 * complex bus, so all endpoints are integrated. 1276 */ 1277 if ((type == PCIEM_TYPE_ENDPOINT) && (pi->pi_bus == 0)) 1278 type = PCIEM_TYPE_ROOT_INT_EP; 1279 1280 pciecap.capid = PCIY_EXPRESS; 1281 pciecap.pcie_capabilities = PCIECAP_VERSION | type; 1282 if (type != PCIEM_TYPE_ROOT_INT_EP) { 1283 pciecap.link_capabilities = 0x411; /* gen1, x1 */ 1284 pciecap.link_status = 0x11; /* gen1, x1 */ 1285 } 1286 1287 err = pci_emul_add_capability(pi, (u_char *)&pciecap, sizeof(pciecap)); 1288 return (err); 1289 } 1290 1291 /* 1292 * This function assumes that 'coff' is in the capabilities region of the 1293 * config space. A capoff parameter of zero will force a search for the 1294 * offset and type. 1295 */ 1296 void 1297 pci_emul_capwrite(struct pci_devinst *pi, int offset, int bytes, uint32_t val, 1298 uint8_t capoff, int capid) 1299 { 1300 uint8_t nextoff; 1301 1302 /* Do not allow un-aligned writes */ 1303 if ((offset & (bytes - 1)) != 0) 1304 return; 1305 1306 if (capoff == 0) { 1307 /* Find the capability that we want to update */ 1308 capoff = CAP_START_OFFSET; 1309 while (1) { 1310 nextoff = pci_get_cfgdata8(pi, capoff + 1); 1311 if (nextoff == 0) 1312 break; 1313 if (offset >= capoff && offset < nextoff) 1314 break; 1315 1316 capoff = nextoff; 1317 } 1318 assert(offset >= capoff); 1319 capid = pci_get_cfgdata8(pi, capoff); 1320 } 1321 1322 /* 1323 * Capability ID and Next Capability Pointer are readonly. 1324 * However, some o/s's do 4-byte writes that include these. 1325 * For this case, trim the write back to 2 bytes and adjust 1326 * the data. 1327 */ 1328 if (offset == capoff || offset == capoff + 1) { 1329 if (offset == capoff && bytes == 4) { 1330 bytes = 2; 1331 offset += 2; 1332 val >>= 16; 1333 } else 1334 return; 1335 } 1336 1337 switch (capid) { 1338 case PCIY_MSI: 1339 msicap_cfgwrite(pi, capoff, offset, bytes, val); 1340 break; 1341 case PCIY_MSIX: 1342 msixcap_cfgwrite(pi, capoff, offset, bytes, val); 1343 break; 1344 case PCIY_EXPRESS: 1345 pciecap_cfgwrite(pi, capoff, offset, bytes, val); 1346 break; 1347 default: 1348 break; 1349 } 1350 } 1351 1352 static int 1353 pci_emul_iscap(struct pci_devinst *pi, int offset) 1354 { 1355 uint16_t sts; 1356 1357 sts = pci_get_cfgdata16(pi, PCIR_STATUS); 1358 if ((sts & PCIM_STATUS_CAPPRESENT) != 0) { 1359 if (offset >= CAP_START_OFFSET && offset <= pi->pi_capend) 1360 return (1); 1361 } 1362 return (0); 1363 } 1364 1365 static int 1366 pci_emul_fallback_handler(struct vcpu *vcpu __unused, int dir, 1367 uint64_t addr __unused, int size __unused, uint64_t *val, 1368 void *arg1 __unused, long arg2 __unused) 1369 { 1370 /* 1371 * Ignore writes; return 0xff's for reads. The mem read code 1372 * will take care of truncating to the correct size. 1373 */ 1374 if (dir == MEM_F_READ) { 1375 *val = 0xffffffffffffffff; 1376 } 1377 1378 return (0); 1379 } 1380 1381 static int 1382 pci_emul_ecfg_handler(struct vcpu *vcpu __unused, int dir, uint64_t addr, 1383 int bytes, uint64_t *val, void *arg1 __unused, long arg2 __unused) 1384 { 1385 int bus, slot, func, coff, in; 1386 1387 coff = addr & 0xfff; 1388 func = (addr >> 12) & 0x7; 1389 slot = (addr >> 15) & 0x1f; 1390 bus = (addr >> 20) & 0xff; 1391 in = (dir == MEM_F_READ); 1392 if (in) 1393 *val = ~0UL; 1394 pci_cfgrw(in, bus, slot, func, coff, bytes, (uint32_t *)val); 1395 return (0); 1396 } 1397 1398 uint64_t 1399 pci_ecfg_base(void) 1400 { 1401 1402 return (PCI_EMUL_ECFG_BASE); 1403 } 1404 1405 static int 1406 init_bootorder(void) 1407 { 1408 struct boot_device *device; 1409 FILE *fp; 1410 char *bootorder; 1411 size_t bootorder_len; 1412 1413 if (TAILQ_EMPTY(&boot_devices)) 1414 return (0); 1415 1416 fp = open_memstream(&bootorder, &bootorder_len); 1417 TAILQ_FOREACH(device, &boot_devices, boot_device_chain) { 1418 fprintf(fp, "/pci@i0cf8/pci@%d,%d\n", 1419 device->pdi->pi_slot, device->pdi->pi_func); 1420 } 1421 fclose(fp); 1422 1423 return (qemu_fwcfg_add_file("bootorder", bootorder_len, bootorder)); 1424 } 1425 1426 #define BUSIO_ROUNDUP 32 1427 #define BUSMEM32_ROUNDUP (1024 * 1024) 1428 #define BUSMEM64_ROUNDUP (512 * 1024 * 1024) 1429 1430 int 1431 init_pci(struct vmctx *ctx) 1432 { 1433 char node_name[sizeof("pci.XXX.XX.X")]; 1434 struct mem_range mr; 1435 struct pci_devemu *pde; 1436 struct businfo *bi; 1437 struct slotinfo *si; 1438 struct funcinfo *fi; 1439 nvlist_t *nvl; 1440 const char *emul; 1441 size_t lowmem; 1442 int bus, slot, func; 1443 int error; 1444 1445 if (vm_get_lowmem_limit(ctx) > PCI_EMUL_MEMBASE32) 1446 errx(EX_OSERR, "Invalid lowmem limit"); 1447 1448 pci_emul_iobase = PCI_EMUL_IOBASE; 1449 pci_emul_membase32 = PCI_EMUL_MEMBASE32; 1450 1451 pci_emul_membase64 = 4*GB + vm_get_highmem_size(ctx); 1452 pci_emul_membase64 = roundup2(pci_emul_membase64, PCI_EMUL_MEMSIZE64); 1453 pci_emul_memlim64 = pci_emul_membase64 + PCI_EMUL_MEMSIZE64; 1454 1455 TAILQ_INIT(&boot_devices); 1456 1457 for (bus = 0; bus < MAXBUSES; bus++) { 1458 snprintf(node_name, sizeof(node_name), "pci.%d", bus); 1459 nvl = find_config_node(node_name); 1460 if (nvl == NULL) 1461 continue; 1462 pci_businfo[bus] = calloc(1, sizeof(struct businfo)); 1463 bi = pci_businfo[bus]; 1464 1465 /* 1466 * Keep track of the i/o and memory resources allocated to 1467 * this bus. 1468 */ 1469 bi->iobase = pci_emul_iobase; 1470 bi->membase32 = pci_emul_membase32; 1471 bi->membase64 = pci_emul_membase64; 1472 1473 /* first run: init devices */ 1474 for (slot = 0; slot < MAXSLOTS; slot++) { 1475 si = &bi->slotinfo[slot]; 1476 for (func = 0; func < MAXFUNCS; func++) { 1477 fi = &si->si_funcs[func]; 1478 snprintf(node_name, sizeof(node_name), 1479 "pci.%d.%d.%d", bus, slot, func); 1480 nvl = find_config_node(node_name); 1481 if (nvl == NULL) 1482 continue; 1483 1484 fi->fi_config = nvl; 1485 emul = get_config_value_node(nvl, "device"); 1486 if (emul == NULL) { 1487 EPRINTLN("pci slot %d:%d:%d: missing " 1488 "\"device\" value", bus, slot, func); 1489 return (EINVAL); 1490 } 1491 pde = pci_emul_finddev(emul); 1492 if (pde == NULL) { 1493 EPRINTLN("pci slot %d:%d:%d: unknown " 1494 "device \"%s\"", bus, slot, func, 1495 emul); 1496 return (EINVAL); 1497 } 1498 if (pde->pe_alias != NULL) { 1499 EPRINTLN("pci slot %d:%d:%d: legacy " 1500 "device \"%s\", use \"%s\" instead", 1501 bus, slot, func, emul, 1502 pde->pe_alias); 1503 return (EINVAL); 1504 } 1505 fi->fi_pde = pde; 1506 error = pci_emul_init(ctx, pde, bus, slot, 1507 func, fi); 1508 if (error) 1509 return (error); 1510 } 1511 } 1512 1513 /* second run: assign BARs and free list */ 1514 struct pci_bar_allocation *bar; 1515 struct pci_bar_allocation *bar_tmp; 1516 TAILQ_FOREACH_SAFE(bar, &pci_bars, chain, bar_tmp) { 1517 pci_emul_assign_bar(bar->pdi, bar->idx, bar->type, 1518 bar->size); 1519 free(bar); 1520 } 1521 TAILQ_INIT(&pci_bars); 1522 1523 /* 1524 * Add some slop to the I/O and memory resources decoded by 1525 * this bus to give a guest some flexibility if it wants to 1526 * reprogram the BARs. 1527 */ 1528 pci_emul_iobase += BUSIO_ROUNDUP; 1529 pci_emul_iobase = roundup2(pci_emul_iobase, BUSIO_ROUNDUP); 1530 bi->iolimit = pci_emul_iobase; 1531 1532 pci_emul_membase32 += BUSMEM32_ROUNDUP; 1533 pci_emul_membase32 = roundup2(pci_emul_membase32, 1534 BUSMEM32_ROUNDUP); 1535 bi->memlimit32 = pci_emul_membase32; 1536 1537 pci_emul_membase64 += BUSMEM64_ROUNDUP; 1538 pci_emul_membase64 = roundup2(pci_emul_membase64, 1539 BUSMEM64_ROUNDUP); 1540 bi->memlimit64 = pci_emul_membase64; 1541 } 1542 1543 /* 1544 * PCI backends are initialized before routing INTx interrupts 1545 * so that LPC devices are able to reserve ISA IRQs before 1546 * routing PIRQ pins. 1547 */ 1548 for (bus = 0; bus < MAXBUSES; bus++) { 1549 if ((bi = pci_businfo[bus]) == NULL) 1550 continue; 1551 1552 for (slot = 0; slot < MAXSLOTS; slot++) { 1553 si = &bi->slotinfo[slot]; 1554 for (func = 0; func < MAXFUNCS; func++) { 1555 fi = &si->si_funcs[func]; 1556 if (fi->fi_devi == NULL) 1557 continue; 1558 pci_lintr_route(fi->fi_devi); 1559 } 1560 } 1561 } 1562 lpc_pirq_routed(); 1563 1564 if ((error = init_bootorder()) != 0) { 1565 warnx("%s: Unable to init bootorder", __func__); 1566 return (error); 1567 } 1568 1569 /* 1570 * The guest physical memory map looks like the following: 1571 * [0, lowmem) guest system memory 1572 * [lowmem, 0xC0000000) memory hole (may be absent) 1573 * [0xC0000000, 0xE0000000) PCI hole (32-bit BAR allocation) 1574 * [0xE0000000, 0xF0000000) PCI extended config window 1575 * [0xF0000000, 4GB) LAPIC, IOAPIC, HPET, firmware 1576 * [4GB, 4GB + highmem) 1577 */ 1578 1579 /* 1580 * Accesses to memory addresses that are not allocated to system 1581 * memory or PCI devices return 0xff's. 1582 */ 1583 lowmem = vm_get_lowmem_size(ctx); 1584 bzero(&mr, sizeof(struct mem_range)); 1585 mr.name = "PCI hole"; 1586 mr.flags = MEM_F_RW | MEM_F_IMMUTABLE; 1587 mr.base = lowmem; 1588 mr.size = (4ULL * 1024 * 1024 * 1024) - lowmem; 1589 mr.handler = pci_emul_fallback_handler; 1590 error = register_mem_fallback(&mr); 1591 assert(error == 0); 1592 1593 /* PCI extended config space */ 1594 bzero(&mr, sizeof(struct mem_range)); 1595 mr.name = "PCI ECFG"; 1596 mr.flags = MEM_F_RW | MEM_F_IMMUTABLE; 1597 mr.base = PCI_EMUL_ECFG_BASE; 1598 mr.size = PCI_EMUL_ECFG_SIZE; 1599 mr.handler = pci_emul_ecfg_handler; 1600 error = register_mem(&mr); 1601 assert(error == 0); 1602 1603 return (0); 1604 } 1605 1606 static void 1607 pci_apic_prt_entry(int bus __unused, int slot, int pin, int pirq_pin __unused, 1608 int ioapic_irq, void *arg __unused) 1609 { 1610 1611 dsdt_line(" Package ()"); 1612 dsdt_line(" {"); 1613 dsdt_line(" 0x%X,", slot << 16 | 0xffff); 1614 dsdt_line(" 0x%02X,", pin - 1); 1615 dsdt_line(" Zero,"); 1616 dsdt_line(" 0x%X", ioapic_irq); 1617 dsdt_line(" },"); 1618 } 1619 1620 static void 1621 pci_pirq_prt_entry(int bus __unused, int slot, int pin, int pirq_pin, 1622 int ioapic_irq __unused, void *arg __unused) 1623 { 1624 char *name; 1625 1626 name = lpc_pirq_name(pirq_pin); 1627 if (name == NULL) 1628 return; 1629 dsdt_line(" Package ()"); 1630 dsdt_line(" {"); 1631 dsdt_line(" 0x%X,", slot << 16 | 0xffff); 1632 dsdt_line(" 0x%02X,", pin - 1); 1633 dsdt_line(" %s,", name); 1634 dsdt_line(" 0x00"); 1635 dsdt_line(" },"); 1636 free(name); 1637 } 1638 1639 /* 1640 * A bhyve virtual machine has a flat PCI hierarchy with a root port 1641 * corresponding to each PCI bus. 1642 */ 1643 static void 1644 pci_bus_write_dsdt(int bus) 1645 { 1646 struct businfo *bi; 1647 struct slotinfo *si; 1648 struct pci_devinst *pi; 1649 int count, func, slot; 1650 1651 /* 1652 * If there are no devices on this 'bus' then just return. 1653 */ 1654 if ((bi = pci_businfo[bus]) == NULL) { 1655 /* 1656 * Bus 0 is special because it decodes the I/O ports used 1657 * for PCI config space access even if there are no devices 1658 * on it. 1659 */ 1660 if (bus != 0) 1661 return; 1662 } 1663 1664 dsdt_line(" Device (PC%02X)", bus); 1665 dsdt_line(" {"); 1666 dsdt_line(" Name (_HID, EisaId (\"PNP0A03\"))"); 1667 1668 dsdt_line(" Method (_BBN, 0, NotSerialized)"); 1669 dsdt_line(" {"); 1670 dsdt_line(" Return (0x%08X)", bus); 1671 dsdt_line(" }"); 1672 dsdt_line(" Name (_CRS, ResourceTemplate ()"); 1673 dsdt_line(" {"); 1674 dsdt_line(" WordBusNumber (ResourceProducer, MinFixed, " 1675 "MaxFixed, PosDecode,"); 1676 dsdt_line(" 0x0000, // Granularity"); 1677 dsdt_line(" 0x%04X, // Range Minimum", bus); 1678 dsdt_line(" 0x%04X, // Range Maximum", bus); 1679 dsdt_line(" 0x0000, // Translation Offset"); 1680 dsdt_line(" 0x0001, // Length"); 1681 dsdt_line(" ,, )"); 1682 1683 if (bus == 0) { 1684 dsdt_indent(3); 1685 dsdt_fixed_ioport(0xCF8, 8); 1686 dsdt_unindent(3); 1687 1688 dsdt_line(" WordIO (ResourceProducer, MinFixed, MaxFixed, " 1689 "PosDecode, EntireRange,"); 1690 dsdt_line(" 0x0000, // Granularity"); 1691 dsdt_line(" 0x0000, // Range Minimum"); 1692 dsdt_line(" 0x0CF7, // Range Maximum"); 1693 dsdt_line(" 0x0000, // Translation Offset"); 1694 dsdt_line(" 0x0CF8, // Length"); 1695 dsdt_line(" ,, , TypeStatic)"); 1696 1697 dsdt_line(" WordIO (ResourceProducer, MinFixed, MaxFixed, " 1698 "PosDecode, EntireRange,"); 1699 dsdt_line(" 0x0000, // Granularity"); 1700 dsdt_line(" 0x0D00, // Range Minimum"); 1701 dsdt_line(" 0x%04X, // Range Maximum", 1702 PCI_EMUL_IOBASE - 1); 1703 dsdt_line(" 0x0000, // Translation Offset"); 1704 dsdt_line(" 0x%04X, // Length", 1705 PCI_EMUL_IOBASE - 0x0D00); 1706 dsdt_line(" ,, , TypeStatic)"); 1707 1708 if (bi == NULL) { 1709 dsdt_line(" })"); 1710 goto done; 1711 } 1712 } 1713 assert(bi != NULL); 1714 1715 /* i/o window */ 1716 dsdt_line(" WordIO (ResourceProducer, MinFixed, MaxFixed, " 1717 "PosDecode, EntireRange,"); 1718 dsdt_line(" 0x0000, // Granularity"); 1719 dsdt_line(" 0x%04X, // Range Minimum", bi->iobase); 1720 dsdt_line(" 0x%04X, // Range Maximum", 1721 bi->iolimit - 1); 1722 dsdt_line(" 0x0000, // Translation Offset"); 1723 dsdt_line(" 0x%04X, // Length", 1724 bi->iolimit - bi->iobase); 1725 dsdt_line(" ,, , TypeStatic)"); 1726 1727 /* mmio window (32-bit) */ 1728 dsdt_line(" DWordMemory (ResourceProducer, PosDecode, " 1729 "MinFixed, MaxFixed, NonCacheable, ReadWrite,"); 1730 dsdt_line(" 0x00000000, // Granularity"); 1731 dsdt_line(" 0x%08X, // Range Minimum\n", bi->membase32); 1732 dsdt_line(" 0x%08X, // Range Maximum\n", 1733 bi->memlimit32 - 1); 1734 dsdt_line(" 0x00000000, // Translation Offset"); 1735 dsdt_line(" 0x%08X, // Length\n", 1736 bi->memlimit32 - bi->membase32); 1737 dsdt_line(" ,, , AddressRangeMemory, TypeStatic)"); 1738 1739 /* mmio window (64-bit) */ 1740 dsdt_line(" QWordMemory (ResourceProducer, PosDecode, " 1741 "MinFixed, MaxFixed, NonCacheable, ReadWrite,"); 1742 dsdt_line(" 0x0000000000000000, // Granularity"); 1743 dsdt_line(" 0x%016lX, // Range Minimum\n", bi->membase64); 1744 dsdt_line(" 0x%016lX, // Range Maximum\n", 1745 bi->memlimit64 - 1); 1746 dsdt_line(" 0x0000000000000000, // Translation Offset"); 1747 dsdt_line(" 0x%016lX, // Length\n", 1748 bi->memlimit64 - bi->membase64); 1749 dsdt_line(" ,, , AddressRangeMemory, TypeStatic)"); 1750 dsdt_line(" })"); 1751 1752 count = pci_count_lintr(bus); 1753 if (count != 0) { 1754 dsdt_indent(2); 1755 dsdt_line("Name (PPRT, Package ()"); 1756 dsdt_line("{"); 1757 pci_walk_lintr(bus, pci_pirq_prt_entry, NULL); 1758 dsdt_line("})"); 1759 dsdt_line("Name (APRT, Package ()"); 1760 dsdt_line("{"); 1761 pci_walk_lintr(bus, pci_apic_prt_entry, NULL); 1762 dsdt_line("})"); 1763 dsdt_line("Method (_PRT, 0, NotSerialized)"); 1764 dsdt_line("{"); 1765 dsdt_line(" If (PICM)"); 1766 dsdt_line(" {"); 1767 dsdt_line(" Return (APRT)"); 1768 dsdt_line(" }"); 1769 dsdt_line(" Else"); 1770 dsdt_line(" {"); 1771 dsdt_line(" Return (PPRT)"); 1772 dsdt_line(" }"); 1773 dsdt_line("}"); 1774 dsdt_unindent(2); 1775 } 1776 1777 dsdt_indent(2); 1778 for (slot = 0; slot < MAXSLOTS; slot++) { 1779 si = &bi->slotinfo[slot]; 1780 for (func = 0; func < MAXFUNCS; func++) { 1781 pi = si->si_funcs[func].fi_devi; 1782 if (pi != NULL && pi->pi_d->pe_write_dsdt != NULL) 1783 pi->pi_d->pe_write_dsdt(pi); 1784 } 1785 } 1786 dsdt_unindent(2); 1787 done: 1788 dsdt_line(" }"); 1789 } 1790 1791 void 1792 pci_write_dsdt(void) 1793 { 1794 int bus; 1795 1796 dsdt_indent(1); 1797 dsdt_line("Name (PICM, 0x00)"); 1798 dsdt_line("Method (_PIC, 1, NotSerialized)"); 1799 dsdt_line("{"); 1800 dsdt_line(" Store (Arg0, PICM)"); 1801 dsdt_line("}"); 1802 dsdt_line(""); 1803 dsdt_line("Scope (_SB)"); 1804 dsdt_line("{"); 1805 for (bus = 0; bus < MAXBUSES; bus++) 1806 pci_bus_write_dsdt(bus); 1807 dsdt_line("}"); 1808 dsdt_unindent(1); 1809 } 1810 1811 int 1812 pci_bus_configured(int bus) 1813 { 1814 assert(bus >= 0 && bus < MAXBUSES); 1815 return (pci_businfo[bus] != NULL); 1816 } 1817 1818 int 1819 pci_msi_enabled(struct pci_devinst *pi) 1820 { 1821 return (pi->pi_msi.enabled); 1822 } 1823 1824 int 1825 pci_msi_maxmsgnum(struct pci_devinst *pi) 1826 { 1827 if (pi->pi_msi.enabled) 1828 return (pi->pi_msi.maxmsgnum); 1829 else 1830 return (0); 1831 } 1832 1833 int 1834 pci_msix_enabled(struct pci_devinst *pi) 1835 { 1836 1837 return (pi->pi_msix.enabled && !pi->pi_msi.enabled); 1838 } 1839 1840 void 1841 pci_generate_msix(struct pci_devinst *pi, int index) 1842 { 1843 struct msix_table_entry *mte; 1844 1845 if (!pci_msix_enabled(pi)) 1846 return; 1847 1848 if (pi->pi_msix.function_mask) 1849 return; 1850 1851 if (index >= pi->pi_msix.table_count) 1852 return; 1853 1854 mte = &pi->pi_msix.table[index]; 1855 if ((mte->vector_control & PCIM_MSIX_VCTRL_MASK) == 0) { 1856 /* XXX Set PBA bit if interrupt is disabled */ 1857 vm_lapic_msi(pi->pi_vmctx, mte->addr, mte->msg_data); 1858 } 1859 } 1860 1861 void 1862 pci_generate_msi(struct pci_devinst *pi, int index) 1863 { 1864 1865 if (pci_msi_enabled(pi) && index < pci_msi_maxmsgnum(pi)) { 1866 vm_lapic_msi(pi->pi_vmctx, pi->pi_msi.addr, 1867 pi->pi_msi.msg_data + index); 1868 } 1869 } 1870 1871 static bool 1872 pci_lintr_permitted(struct pci_devinst *pi) 1873 { 1874 uint16_t cmd; 1875 1876 cmd = pci_get_cfgdata16(pi, PCIR_COMMAND); 1877 return (!(pi->pi_msi.enabled || pi->pi_msix.enabled || 1878 (cmd & PCIM_CMD_INTxDIS))); 1879 } 1880 1881 void 1882 pci_lintr_request(struct pci_devinst *pi) 1883 { 1884 struct businfo *bi; 1885 struct slotinfo *si; 1886 int bestpin, bestcount, pin; 1887 1888 bi = pci_businfo[pi->pi_bus]; 1889 assert(bi != NULL); 1890 1891 /* 1892 * Just allocate a pin from our slot. The pin will be 1893 * assigned IRQs later when interrupts are routed. 1894 */ 1895 si = &bi->slotinfo[pi->pi_slot]; 1896 bestpin = 0; 1897 bestcount = si->si_intpins[0].ii_count; 1898 for (pin = 1; pin < 4; pin++) { 1899 if (si->si_intpins[pin].ii_count < bestcount) { 1900 bestpin = pin; 1901 bestcount = si->si_intpins[pin].ii_count; 1902 } 1903 } 1904 1905 si->si_intpins[bestpin].ii_count++; 1906 pi->pi_lintr.pin = bestpin + 1; 1907 pci_set_cfgdata8(pi, PCIR_INTPIN, bestpin + 1); 1908 } 1909 1910 static void 1911 pci_lintr_route(struct pci_devinst *pi) 1912 { 1913 struct businfo *bi; 1914 struct intxinfo *ii; 1915 1916 if (pi->pi_lintr.pin == 0) 1917 return; 1918 1919 bi = pci_businfo[pi->pi_bus]; 1920 assert(bi != NULL); 1921 ii = &bi->slotinfo[pi->pi_slot].si_intpins[pi->pi_lintr.pin - 1]; 1922 1923 /* 1924 * Attempt to allocate an I/O APIC pin for this intpin if one 1925 * is not yet assigned. 1926 */ 1927 if (ii->ii_ioapic_irq == 0) 1928 ii->ii_ioapic_irq = ioapic_pci_alloc_irq(pi); 1929 assert(ii->ii_ioapic_irq > 0); 1930 1931 /* 1932 * Attempt to allocate a PIRQ pin for this intpin if one is 1933 * not yet assigned. 1934 */ 1935 if (ii->ii_pirq_pin == 0) 1936 ii->ii_pirq_pin = pirq_alloc_pin(pi); 1937 assert(ii->ii_pirq_pin > 0); 1938 1939 pi->pi_lintr.ioapic_irq = ii->ii_ioapic_irq; 1940 pi->pi_lintr.pirq_pin = ii->ii_pirq_pin; 1941 pci_set_cfgdata8(pi, PCIR_INTLINE, pirq_irq(ii->ii_pirq_pin)); 1942 } 1943 1944 void 1945 pci_lintr_assert(struct pci_devinst *pi) 1946 { 1947 1948 assert(pi->pi_lintr.pin > 0); 1949 1950 pthread_mutex_lock(&pi->pi_lintr.lock); 1951 if (pi->pi_lintr.state == IDLE) { 1952 if (pci_lintr_permitted(pi)) { 1953 pi->pi_lintr.state = ASSERTED; 1954 pci_irq_assert(pi); 1955 } else 1956 pi->pi_lintr.state = PENDING; 1957 } 1958 pthread_mutex_unlock(&pi->pi_lintr.lock); 1959 } 1960 1961 void 1962 pci_lintr_deassert(struct pci_devinst *pi) 1963 { 1964 1965 assert(pi->pi_lintr.pin > 0); 1966 1967 pthread_mutex_lock(&pi->pi_lintr.lock); 1968 if (pi->pi_lintr.state == ASSERTED) { 1969 pi->pi_lintr.state = IDLE; 1970 pci_irq_deassert(pi); 1971 } else if (pi->pi_lintr.state == PENDING) 1972 pi->pi_lintr.state = IDLE; 1973 pthread_mutex_unlock(&pi->pi_lintr.lock); 1974 } 1975 1976 static void 1977 pci_lintr_update(struct pci_devinst *pi) 1978 { 1979 1980 pthread_mutex_lock(&pi->pi_lintr.lock); 1981 if (pi->pi_lintr.state == ASSERTED && !pci_lintr_permitted(pi)) { 1982 pci_irq_deassert(pi); 1983 pi->pi_lintr.state = PENDING; 1984 } else if (pi->pi_lintr.state == PENDING && pci_lintr_permitted(pi)) { 1985 pi->pi_lintr.state = ASSERTED; 1986 pci_irq_assert(pi); 1987 } 1988 pthread_mutex_unlock(&pi->pi_lintr.lock); 1989 } 1990 1991 int 1992 pci_count_lintr(int bus) 1993 { 1994 int count, slot, pin; 1995 struct slotinfo *slotinfo; 1996 1997 count = 0; 1998 if (pci_businfo[bus] != NULL) { 1999 for (slot = 0; slot < MAXSLOTS; slot++) { 2000 slotinfo = &pci_businfo[bus]->slotinfo[slot]; 2001 for (pin = 0; pin < 4; pin++) { 2002 if (slotinfo->si_intpins[pin].ii_count != 0) 2003 count++; 2004 } 2005 } 2006 } 2007 return (count); 2008 } 2009 2010 void 2011 pci_walk_lintr(int bus, pci_lintr_cb cb, void *arg) 2012 { 2013 struct businfo *bi; 2014 struct slotinfo *si; 2015 struct intxinfo *ii; 2016 int slot, pin; 2017 2018 if ((bi = pci_businfo[bus]) == NULL) 2019 return; 2020 2021 for (slot = 0; slot < MAXSLOTS; slot++) { 2022 si = &bi->slotinfo[slot]; 2023 for (pin = 0; pin < 4; pin++) { 2024 ii = &si->si_intpins[pin]; 2025 if (ii->ii_count != 0) 2026 cb(bus, slot, pin + 1, ii->ii_pirq_pin, 2027 ii->ii_ioapic_irq, arg); 2028 } 2029 } 2030 } 2031 2032 /* 2033 * Return 1 if the emulated device in 'slot' is a multi-function device. 2034 * Return 0 otherwise. 2035 */ 2036 static int 2037 pci_emul_is_mfdev(int bus, int slot) 2038 { 2039 struct businfo *bi; 2040 struct slotinfo *si; 2041 int f, numfuncs; 2042 2043 numfuncs = 0; 2044 if ((bi = pci_businfo[bus]) != NULL) { 2045 si = &bi->slotinfo[slot]; 2046 for (f = 0; f < MAXFUNCS; f++) { 2047 if (si->si_funcs[f].fi_devi != NULL) { 2048 numfuncs++; 2049 } 2050 } 2051 } 2052 return (numfuncs > 1); 2053 } 2054 2055 /* 2056 * Ensure that the PCIM_MFDEV bit is properly set (or unset) depending on 2057 * whether or not is a multi-function being emulated in the pci 'slot'. 2058 */ 2059 static void 2060 pci_emul_hdrtype_fixup(int bus, int slot, int off, int bytes, uint32_t *rv) 2061 { 2062 int mfdev; 2063 2064 if (off <= PCIR_HDRTYPE && off + bytes > PCIR_HDRTYPE) { 2065 mfdev = pci_emul_is_mfdev(bus, slot); 2066 switch (bytes) { 2067 case 1: 2068 case 2: 2069 *rv &= ~PCIM_MFDEV; 2070 if (mfdev) { 2071 *rv |= PCIM_MFDEV; 2072 } 2073 break; 2074 case 4: 2075 *rv &= ~(PCIM_MFDEV << 16); 2076 if (mfdev) { 2077 *rv |= (PCIM_MFDEV << 16); 2078 } 2079 break; 2080 } 2081 } 2082 } 2083 2084 /* 2085 * Update device state in response to changes to the PCI command 2086 * register. 2087 */ 2088 void 2089 pci_emul_cmd_changed(struct pci_devinst *pi, uint16_t old) 2090 { 2091 int i; 2092 uint16_t changed, new; 2093 2094 new = pci_get_cfgdata16(pi, PCIR_COMMAND); 2095 changed = old ^ new; 2096 2097 /* 2098 * If the MMIO or I/O address space decoding has changed then 2099 * register/unregister all BARs that decode that address space. 2100 */ 2101 for (i = 0; i <= PCI_BARMAX_WITH_ROM; i++) { 2102 switch (pi->pi_bar[i].type) { 2103 case PCIBAR_NONE: 2104 case PCIBAR_MEMHI64: 2105 break; 2106 case PCIBAR_IO: 2107 /* I/O address space decoding changed? */ 2108 if (changed & PCIM_CMD_PORTEN) { 2109 if (new & PCIM_CMD_PORTEN) 2110 register_bar(pi, i); 2111 else 2112 unregister_bar(pi, i); 2113 } 2114 break; 2115 case PCIBAR_ROM: 2116 /* skip (un-)register of ROM if it disabled */ 2117 if (!romen(pi)) 2118 break; 2119 /* fallthrough */ 2120 case PCIBAR_MEM32: 2121 case PCIBAR_MEM64: 2122 /* MMIO address space decoding changed? */ 2123 if (changed & PCIM_CMD_MEMEN) { 2124 if (new & PCIM_CMD_MEMEN) 2125 register_bar(pi, i); 2126 else 2127 unregister_bar(pi, i); 2128 } 2129 break; 2130 default: 2131 assert(0); 2132 } 2133 } 2134 2135 /* 2136 * If INTx has been unmasked and is pending, assert the 2137 * interrupt. 2138 */ 2139 pci_lintr_update(pi); 2140 } 2141 2142 static void 2143 pci_emul_cmdsts_write(struct pci_devinst *pi, int coff, uint32_t new, int bytes) 2144 { 2145 int rshift; 2146 uint32_t cmd, old, readonly; 2147 2148 cmd = pci_get_cfgdata16(pi, PCIR_COMMAND); /* stash old value */ 2149 2150 /* 2151 * From PCI Local Bus Specification 3.0 sections 6.2.2 and 6.2.3. 2152 * 2153 * XXX Bits 8, 11, 12, 13, 14 and 15 in the status register are 2154 * 'write 1 to clear'. However these bits are not set to '1' by 2155 * any device emulation so it is simpler to treat them as readonly. 2156 */ 2157 rshift = (coff & 0x3) * 8; 2158 readonly = 0xFFFFF880 >> rshift; 2159 2160 old = CFGREAD(pi, coff, bytes); 2161 new &= ~readonly; 2162 new |= (old & readonly); 2163 CFGWRITE(pi, coff, new, bytes); /* update config */ 2164 2165 pci_emul_cmd_changed(pi, cmd); 2166 } 2167 2168 static void 2169 pci_cfgrw(int in, int bus, int slot, int func, int coff, int bytes, 2170 uint32_t *valp) 2171 { 2172 struct businfo *bi; 2173 struct slotinfo *si; 2174 struct pci_devinst *pi; 2175 struct pci_devemu *pe; 2176 int idx, needcfg; 2177 uint64_t addr, bar, mask; 2178 2179 if ((bi = pci_businfo[bus]) != NULL) { 2180 si = &bi->slotinfo[slot]; 2181 pi = si->si_funcs[func].fi_devi; 2182 } else 2183 pi = NULL; 2184 2185 /* 2186 * Just return if there is no device at this slot:func or if the 2187 * the guest is doing an un-aligned access. 2188 */ 2189 if (pi == NULL || (bytes != 1 && bytes != 2 && bytes != 4) || 2190 (coff & (bytes - 1)) != 0) { 2191 if (in) 2192 *valp = 0xffffffff; 2193 return; 2194 } 2195 2196 /* 2197 * Ignore all writes beyond the standard config space and return all 2198 * ones on reads. 2199 */ 2200 if (coff >= PCI_REGMAX + 1) { 2201 if (in) { 2202 *valp = 0xffffffff; 2203 /* 2204 * Extended capabilities begin at offset 256 in config 2205 * space. Absence of extended capabilities is signaled 2206 * with all 0s in the extended capability header at 2207 * offset 256. 2208 */ 2209 if (coff <= PCI_REGMAX + 4) 2210 *valp = 0x00000000; 2211 } 2212 return; 2213 } 2214 2215 pe = pi->pi_d; 2216 2217 /* 2218 * Config read 2219 */ 2220 if (in) { 2221 /* Let the device emulation override the default handler */ 2222 if (pe->pe_cfgread != NULL) { 2223 needcfg = pe->pe_cfgread(pi, coff, bytes, valp); 2224 } else { 2225 needcfg = 1; 2226 } 2227 2228 if (needcfg) 2229 *valp = CFGREAD(pi, coff, bytes); 2230 2231 pci_emul_hdrtype_fixup(bus, slot, coff, bytes, valp); 2232 } else { 2233 /* Let the device emulation override the default handler */ 2234 if (pe->pe_cfgwrite != NULL && 2235 (*pe->pe_cfgwrite)(pi, coff, bytes, *valp) == 0) 2236 return; 2237 2238 /* 2239 * Special handling for write to BAR and ROM registers 2240 */ 2241 if (is_pcir_bar(coff) || is_pcir_bios(coff)) { 2242 /* 2243 * Ignore writes to BAR registers that are not 2244 * 4-byte aligned. 2245 */ 2246 if (bytes != 4 || (coff & 0x3) != 0) 2247 return; 2248 2249 if (is_pcir_bar(coff)) { 2250 idx = (coff - PCIR_BAR(0)) / 4; 2251 } else if (is_pcir_bios(coff)) { 2252 idx = PCI_ROM_IDX; 2253 } else { 2254 errx(4, "%s: invalid BAR offset %d", __func__, 2255 coff); 2256 } 2257 2258 mask = ~(pi->pi_bar[idx].size - 1); 2259 switch (pi->pi_bar[idx].type) { 2260 case PCIBAR_NONE: 2261 pi->pi_bar[idx].addr = bar = 0; 2262 break; 2263 case PCIBAR_IO: 2264 addr = *valp & mask; 2265 addr &= 0xffff; 2266 bar = addr | pi->pi_bar[idx].lobits; 2267 /* 2268 * Register the new BAR value for interception 2269 */ 2270 if (addr != pi->pi_bar[idx].addr) { 2271 update_bar_address(pi, addr, idx, 2272 PCIBAR_IO); 2273 } 2274 break; 2275 case PCIBAR_MEM32: 2276 addr = bar = *valp & mask; 2277 bar |= pi->pi_bar[idx].lobits; 2278 if (addr != pi->pi_bar[idx].addr) { 2279 update_bar_address(pi, addr, idx, 2280 PCIBAR_MEM32); 2281 } 2282 break; 2283 case PCIBAR_MEM64: 2284 addr = bar = *valp & mask; 2285 bar |= pi->pi_bar[idx].lobits; 2286 if (addr != (uint32_t)pi->pi_bar[idx].addr) { 2287 update_bar_address(pi, addr, idx, 2288 PCIBAR_MEM64); 2289 } 2290 break; 2291 case PCIBAR_MEMHI64: 2292 mask = ~(pi->pi_bar[idx - 1].size - 1); 2293 addr = ((uint64_t)*valp << 32) & mask; 2294 bar = addr >> 32; 2295 if (bar != pi->pi_bar[idx - 1].addr >> 32) { 2296 update_bar_address(pi, addr, idx - 1, 2297 PCIBAR_MEMHI64); 2298 } 2299 break; 2300 case PCIBAR_ROM: 2301 addr = bar = *valp & mask; 2302 if (memen(pi) && romen(pi)) { 2303 unregister_bar(pi, idx); 2304 } 2305 pi->pi_bar[idx].addr = addr; 2306 pi->pi_bar[idx].lobits = *valp & 2307 PCIM_BIOS_ENABLE; 2308 /* romen could have changed it value */ 2309 if (memen(pi) && romen(pi)) { 2310 register_bar(pi, idx); 2311 } 2312 bar |= pi->pi_bar[idx].lobits; 2313 break; 2314 default: 2315 assert(0); 2316 } 2317 pci_set_cfgdata32(pi, coff, bar); 2318 2319 } else if (pci_emul_iscap(pi, coff)) { 2320 pci_emul_capwrite(pi, coff, bytes, *valp, 0, 0); 2321 } else if (coff >= PCIR_COMMAND && coff < PCIR_REVID) { 2322 pci_emul_cmdsts_write(pi, coff, *valp, bytes); 2323 } else { 2324 CFGWRITE(pi, coff, *valp, bytes); 2325 } 2326 } 2327 } 2328 2329 static int cfgenable, cfgbus, cfgslot, cfgfunc, cfgoff; 2330 2331 static int 2332 pci_emul_cfgaddr(struct vmctx *ctx __unused, int in, 2333 int port __unused, int bytes, uint32_t *eax, void *arg __unused) 2334 { 2335 uint32_t x; 2336 2337 if (bytes != 4) { 2338 if (in) 2339 *eax = (bytes == 2) ? 0xffff : 0xff; 2340 return (0); 2341 } 2342 2343 if (in) { 2344 x = (cfgbus << 16) | (cfgslot << 11) | (cfgfunc << 8) | cfgoff; 2345 if (cfgenable) 2346 x |= CONF1_ENABLE; 2347 *eax = x; 2348 } else { 2349 x = *eax; 2350 cfgenable = (x & CONF1_ENABLE) == CONF1_ENABLE; 2351 cfgoff = (x & PCI_REGMAX) & ~0x03; 2352 cfgfunc = (x >> 8) & PCI_FUNCMAX; 2353 cfgslot = (x >> 11) & PCI_SLOTMAX; 2354 cfgbus = (x >> 16) & PCI_BUSMAX; 2355 } 2356 2357 return (0); 2358 } 2359 INOUT_PORT(pci_cfgaddr, CONF1_ADDR_PORT, IOPORT_F_INOUT, pci_emul_cfgaddr); 2360 2361 static int 2362 pci_emul_cfgdata(struct vmctx *ctx __unused, int in, int port, 2363 int bytes, uint32_t *eax, void *arg __unused) 2364 { 2365 int coff; 2366 2367 assert(bytes == 1 || bytes == 2 || bytes == 4); 2368 2369 coff = cfgoff + (port - CONF1_DATA_PORT); 2370 if (cfgenable) { 2371 pci_cfgrw(in, cfgbus, cfgslot, cfgfunc, coff, bytes, eax); 2372 } else { 2373 /* Ignore accesses to cfgdata if not enabled by cfgaddr */ 2374 if (in) 2375 *eax = 0xffffffff; 2376 } 2377 return (0); 2378 } 2379 2380 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+0, IOPORT_F_INOUT, pci_emul_cfgdata); 2381 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+1, IOPORT_F_INOUT, pci_emul_cfgdata); 2382 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+2, IOPORT_F_INOUT, pci_emul_cfgdata); 2383 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+3, IOPORT_F_INOUT, pci_emul_cfgdata); 2384 2385 #ifdef BHYVE_SNAPSHOT 2386 /* 2387 * Saves/restores PCI device emulated state. Returns 0 on success. 2388 */ 2389 static int 2390 pci_snapshot_pci_dev(struct vm_snapshot_meta *meta) 2391 { 2392 struct pci_devinst *pi; 2393 int i; 2394 int ret; 2395 2396 pi = meta->dev_data; 2397 2398 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msi.enabled, meta, ret, done); 2399 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msi.addr, meta, ret, done); 2400 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msi.msg_data, meta, ret, done); 2401 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msi.maxmsgnum, meta, ret, done); 2402 2403 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.enabled, meta, ret, done); 2404 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.table_bar, meta, ret, done); 2405 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.pba_bar, meta, ret, done); 2406 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.table_offset, meta, ret, done); 2407 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.table_count, meta, ret, done); 2408 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.pba_offset, meta, ret, done); 2409 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.pba_size, meta, ret, done); 2410 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.function_mask, meta, ret, done); 2411 2412 SNAPSHOT_BUF_OR_LEAVE(pi->pi_cfgdata, sizeof(pi->pi_cfgdata), 2413 meta, ret, done); 2414 2415 for (i = 0; i < (int)nitems(pi->pi_bar); i++) { 2416 SNAPSHOT_VAR_OR_LEAVE(pi->pi_bar[i].type, meta, ret, done); 2417 SNAPSHOT_VAR_OR_LEAVE(pi->pi_bar[i].size, meta, ret, done); 2418 SNAPSHOT_VAR_OR_LEAVE(pi->pi_bar[i].addr, meta, ret, done); 2419 } 2420 2421 /* Restore MSI-X table. */ 2422 for (i = 0; i < pi->pi_msix.table_count; i++) { 2423 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.table[i].addr, 2424 meta, ret, done); 2425 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.table[i].msg_data, 2426 meta, ret, done); 2427 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.table[i].vector_control, 2428 meta, ret, done); 2429 } 2430 2431 done: 2432 return (ret); 2433 } 2434 2435 int 2436 pci_snapshot(struct vm_snapshot_meta *meta) 2437 { 2438 struct pci_devemu *pde; 2439 struct pci_devinst *pdi; 2440 int ret; 2441 2442 assert(meta->dev_name != NULL); 2443 2444 pdi = meta->dev_data; 2445 pde = pdi->pi_d; 2446 2447 if (pde->pe_snapshot == NULL) 2448 return (ENOTSUP); 2449 2450 ret = pci_snapshot_pci_dev(meta); 2451 if (ret == 0) 2452 ret = (*pde->pe_snapshot)(meta); 2453 2454 return (ret); 2455 } 2456 2457 int 2458 pci_pause(struct pci_devinst *pdi) 2459 { 2460 struct pci_devemu *pde = pdi->pi_d; 2461 2462 if (pde->pe_pause == NULL) { 2463 /* The pause/resume functionality is optional. */ 2464 return (0); 2465 } 2466 2467 return (*pde->pe_pause)(pdi); 2468 } 2469 2470 int 2471 pci_resume(struct pci_devinst *pdi) 2472 { 2473 struct pci_devemu *pde = pdi->pi_d; 2474 2475 if (pde->pe_resume == NULL) { 2476 /* The pause/resume functionality is optional. */ 2477 return (0); 2478 } 2479 2480 return (*pde->pe_resume)(pdi); 2481 } 2482 #endif 2483 2484 #define PCI_EMUL_TEST 2485 #ifdef PCI_EMUL_TEST 2486 /* 2487 * Define a dummy test device 2488 */ 2489 #define DIOSZ 8 2490 #define DMEMSZ 4096 2491 struct pci_emul_dsoftc { 2492 uint8_t ioregs[DIOSZ]; 2493 uint8_t memregs[2][DMEMSZ]; 2494 }; 2495 2496 #define PCI_EMUL_MSI_MSGS 4 2497 #define PCI_EMUL_MSIX_MSGS 16 2498 2499 static int 2500 pci_emul_dinit(struct pci_devinst *pi, nvlist_t *nvl __unused) 2501 { 2502 int error; 2503 struct pci_emul_dsoftc *sc; 2504 2505 sc = calloc(1, sizeof(struct pci_emul_dsoftc)); 2506 2507 pi->pi_arg = sc; 2508 2509 pci_set_cfgdata16(pi, PCIR_DEVICE, 0x0001); 2510 pci_set_cfgdata16(pi, PCIR_VENDOR, 0x10DD); 2511 pci_set_cfgdata8(pi, PCIR_CLASS, 0x02); 2512 2513 error = pci_emul_add_msicap(pi, PCI_EMUL_MSI_MSGS); 2514 assert(error == 0); 2515 2516 error = pci_emul_alloc_bar(pi, 0, PCIBAR_IO, DIOSZ); 2517 assert(error == 0); 2518 2519 error = pci_emul_alloc_bar(pi, 1, PCIBAR_MEM32, DMEMSZ); 2520 assert(error == 0); 2521 2522 error = pci_emul_alloc_bar(pi, 2, PCIBAR_MEM32, DMEMSZ); 2523 assert(error == 0); 2524 2525 return (0); 2526 } 2527 2528 static void 2529 pci_emul_diow(struct pci_devinst *pi, int baridx, uint64_t offset, int size, 2530 uint64_t value) 2531 { 2532 int i; 2533 struct pci_emul_dsoftc *sc = pi->pi_arg; 2534 2535 if (baridx == 0) { 2536 if (offset + size > DIOSZ) { 2537 printf("diow: iow too large, offset %ld size %d\n", 2538 offset, size); 2539 return; 2540 } 2541 2542 if (size == 1) { 2543 sc->ioregs[offset] = value & 0xff; 2544 } else if (size == 2) { 2545 *(uint16_t *)&sc->ioregs[offset] = value & 0xffff; 2546 } else if (size == 4) { 2547 *(uint32_t *)&sc->ioregs[offset] = value; 2548 } else { 2549 printf("diow: iow unknown size %d\n", size); 2550 } 2551 2552 /* 2553 * Special magic value to generate an interrupt 2554 */ 2555 if (offset == 4 && size == 4 && pci_msi_enabled(pi)) 2556 pci_generate_msi(pi, value % pci_msi_maxmsgnum(pi)); 2557 2558 if (value == 0xabcdef) { 2559 for (i = 0; i < pci_msi_maxmsgnum(pi); i++) 2560 pci_generate_msi(pi, i); 2561 } 2562 } 2563 2564 if (baridx == 1 || baridx == 2) { 2565 if (offset + size > DMEMSZ) { 2566 printf("diow: memw too large, offset %ld size %d\n", 2567 offset, size); 2568 return; 2569 } 2570 2571 i = baridx - 1; /* 'memregs' index */ 2572 2573 if (size == 1) { 2574 sc->memregs[i][offset] = value; 2575 } else if (size == 2) { 2576 *(uint16_t *)&sc->memregs[i][offset] = value; 2577 } else if (size == 4) { 2578 *(uint32_t *)&sc->memregs[i][offset] = value; 2579 } else if (size == 8) { 2580 *(uint64_t *)&sc->memregs[i][offset] = value; 2581 } else { 2582 printf("diow: memw unknown size %d\n", size); 2583 } 2584 2585 /* 2586 * magic interrupt ?? 2587 */ 2588 } 2589 2590 if (baridx > 2 || baridx < 0) { 2591 printf("diow: unknown bar idx %d\n", baridx); 2592 } 2593 } 2594 2595 static uint64_t 2596 pci_emul_dior(struct pci_devinst *pi, int baridx, uint64_t offset, int size) 2597 { 2598 struct pci_emul_dsoftc *sc = pi->pi_arg; 2599 uint32_t value; 2600 int i; 2601 2602 if (baridx == 0) { 2603 if (offset + size > DIOSZ) { 2604 printf("dior: ior too large, offset %ld size %d\n", 2605 offset, size); 2606 return (0); 2607 } 2608 2609 value = 0; 2610 if (size == 1) { 2611 value = sc->ioregs[offset]; 2612 } else if (size == 2) { 2613 value = *(uint16_t *) &sc->ioregs[offset]; 2614 } else if (size == 4) { 2615 value = *(uint32_t *) &sc->ioregs[offset]; 2616 } else { 2617 printf("dior: ior unknown size %d\n", size); 2618 } 2619 } 2620 2621 if (baridx == 1 || baridx == 2) { 2622 if (offset + size > DMEMSZ) { 2623 printf("dior: memr too large, offset %ld size %d\n", 2624 offset, size); 2625 return (0); 2626 } 2627 2628 i = baridx - 1; /* 'memregs' index */ 2629 2630 if (size == 1) { 2631 value = sc->memregs[i][offset]; 2632 } else if (size == 2) { 2633 value = *(uint16_t *) &sc->memregs[i][offset]; 2634 } else if (size == 4) { 2635 value = *(uint32_t *) &sc->memregs[i][offset]; 2636 } else if (size == 8) { 2637 value = *(uint64_t *) &sc->memregs[i][offset]; 2638 } else { 2639 printf("dior: ior unknown size %d\n", size); 2640 } 2641 } 2642 2643 2644 if (baridx > 2 || baridx < 0) { 2645 printf("dior: unknown bar idx %d\n", baridx); 2646 return (0); 2647 } 2648 2649 return (value); 2650 } 2651 2652 #ifdef BHYVE_SNAPSHOT 2653 struct pci_devinst * 2654 pci_next(const struct pci_devinst *cursor) 2655 { 2656 unsigned bus = 0, slot = 0, func = 0; 2657 struct businfo *bi; 2658 struct slotinfo *si; 2659 struct funcinfo *fi; 2660 2661 bus = cursor ? cursor->pi_bus : 0; 2662 slot = cursor ? cursor->pi_slot : 0; 2663 func = cursor ? (cursor->pi_func + 1) : 0; 2664 2665 for (; bus < MAXBUSES; bus++) { 2666 if ((bi = pci_businfo[bus]) == NULL) 2667 continue; 2668 2669 if (slot >= MAXSLOTS) 2670 slot = 0; 2671 2672 for (; slot < MAXSLOTS; slot++) { 2673 si = &bi->slotinfo[slot]; 2674 if (func >= MAXFUNCS) 2675 func = 0; 2676 for (; func < MAXFUNCS; func++) { 2677 fi = &si->si_funcs[func]; 2678 if (fi->fi_devi == NULL) 2679 continue; 2680 2681 return (fi->fi_devi); 2682 } 2683 } 2684 } 2685 2686 return (NULL); 2687 } 2688 2689 static int 2690 pci_emul_snapshot(struct vm_snapshot_meta *meta __unused) 2691 { 2692 return (0); 2693 } 2694 #endif 2695 2696 static const struct pci_devemu pci_dummy = { 2697 .pe_emu = "dummy", 2698 .pe_init = pci_emul_dinit, 2699 .pe_barwrite = pci_emul_diow, 2700 .pe_barread = pci_emul_dior, 2701 #ifdef BHYVE_SNAPSHOT 2702 .pe_snapshot = pci_emul_snapshot, 2703 #endif 2704 }; 2705 PCI_EMUL_SET(pci_dummy); 2706 2707 #endif /* PCI_EMUL_TEST */ 2708