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