/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2014 Hudson River Trading LLC4* Written by: John H. Baldwin <[email protected]>5* All rights reserved.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10* 1. Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829#include <sys/types.h>30#include <stdio.h>3132#include <machine/vmm.h>33#include <vmmapi.h>3435#include "bootrom.h"36#include "ioapic.h"37#include "pci_emul.h"38#include "pci_lpc.h"3940/*41* Assign PCI INTx interrupts to I/O APIC pins in a round-robin42* fashion. Note that we have no idea what the HPET is using, but the43* HPET is also programmable whereas this is intended for hardwired44* PCI interrupts.45*46* This assumes a single I/O APIC where pins >= 16 are permitted for47* PCI devices.48*/49static int pci_pins;5051void52ioapic_init(struct vmctx *ctx)53{5455if (vm_ioapic_pincount(ctx, &pci_pins) < 0) {56pci_pins = 0;57return;58}5960/* Ignore the first 16 pins. */61if (pci_pins <= 16) {62pci_pins = 0;63return;64}65pci_pins -= 16;66}6768int69ioapic_pci_alloc_irq(struct pci_devinst *pi)70{71static int last_pin;7273if (pci_pins == 0)74return (-1);75if (bootrom_boot()) {76/* For external bootrom use fixed mapping. */77return (16 + (4 + pi->pi_slot + pi->pi_lintr.pin) % 8);78}79return (16 + (last_pin++ % pci_pins));80}818283