1bf21cd93STycho Nightingale /*-
2*32640292SAndy Fiddaman * SPDX-License-Identifier: BSD-2-Clause
34c87aefeSPatrick Mooney *
44c87aefeSPatrick Mooney * Copyright (c) 2014 Hudson River Trading LLC
5bf21cd93STycho Nightingale * Written by: John H. Baldwin <jhb@FreeBSD.org>
6bf21cd93STycho Nightingale * All rights reserved.
7bf21cd93STycho Nightingale *
8bf21cd93STycho Nightingale * Redistribution and use in source and binary forms, with or without
9bf21cd93STycho Nightingale * modification, are permitted provided that the following conditions
10bf21cd93STycho Nightingale * are met:
11bf21cd93STycho Nightingale * 1. Redistributions of source code must retain the above copyright
12bf21cd93STycho Nightingale * notice, this list of conditions and the following disclaimer.
13bf21cd93STycho Nightingale * 2. Redistributions in binary form must reproduce the above copyright
14bf21cd93STycho Nightingale * notice, this list of conditions and the following disclaimer in the
15bf21cd93STycho Nightingale * documentation and/or other materials provided with the distribution.
16bf21cd93STycho Nightingale *
17bf21cd93STycho Nightingale * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18bf21cd93STycho Nightingale * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19bf21cd93STycho Nightingale * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20bf21cd93STycho Nightingale * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21bf21cd93STycho Nightingale * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22bf21cd93STycho Nightingale * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23bf21cd93STycho Nightingale * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24bf21cd93STycho Nightingale * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25bf21cd93STycho Nightingale * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26bf21cd93STycho Nightingale * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27bf21cd93STycho Nightingale * SUCH DAMAGE.
28bf21cd93STycho Nightingale */
29bf21cd93STycho Nightingale
30bf21cd93STycho Nightingale #include <sys/cdefs.h>
31bf21cd93STycho Nightingale
32bf21cd93STycho Nightingale #include <sys/types.h>
334c87aefeSPatrick Mooney #include <stdio.h>
34bf21cd93STycho Nightingale
35bf21cd93STycho Nightingale #include <machine/vmm.h>
36bf21cd93STycho Nightingale #include <vmmapi.h>
37bf21cd93STycho Nightingale
38bf21cd93STycho Nightingale #include "ioapic.h"
394c87aefeSPatrick Mooney #include "pci_emul.h"
404c87aefeSPatrick Mooney #include "pci_lpc.h"
41bf21cd93STycho Nightingale
42bf21cd93STycho Nightingale /*
43bf21cd93STycho Nightingale * Assign PCI INTx interrupts to I/O APIC pins in a round-robin
44bf21cd93STycho Nightingale * fashion. Note that we have no idea what the HPET is using, but the
45bf21cd93STycho Nightingale * HPET is also programmable whereas this is intended for hardwired
46bf21cd93STycho Nightingale * PCI interrupts.
47bf21cd93STycho Nightingale *
48bf21cd93STycho Nightingale * This assumes a single I/O APIC where pins >= 16 are permitted for
49bf21cd93STycho Nightingale * PCI devices.
50bf21cd93STycho Nightingale */
51bf21cd93STycho Nightingale static int pci_pins;
52bf21cd93STycho Nightingale
53bf21cd93STycho Nightingale void
ioapic_init(struct vmctx * ctx)54bf21cd93STycho Nightingale ioapic_init(struct vmctx *ctx)
55bf21cd93STycho Nightingale {
56bf21cd93STycho Nightingale
57bf21cd93STycho Nightingale if (vm_ioapic_pincount(ctx, &pci_pins) < 0) {
58bf21cd93STycho Nightingale pci_pins = 0;
59bf21cd93STycho Nightingale return;
60bf21cd93STycho Nightingale }
61bf21cd93STycho Nightingale
62bf21cd93STycho Nightingale /* Ignore the first 16 pins. */
63bf21cd93STycho Nightingale if (pci_pins <= 16) {
64bf21cd93STycho Nightingale pci_pins = 0;
65bf21cd93STycho Nightingale return;
66bf21cd93STycho Nightingale }
67bf21cd93STycho Nightingale pci_pins -= 16;
68bf21cd93STycho Nightingale }
69bf21cd93STycho Nightingale
70bf21cd93STycho Nightingale int
ioapic_pci_alloc_irq(struct pci_devinst * pi)714c87aefeSPatrick Mooney ioapic_pci_alloc_irq(struct pci_devinst *pi)
72bf21cd93STycho Nightingale {
73bf21cd93STycho Nightingale static int last_pin;
74bf21cd93STycho Nightingale
75bf21cd93STycho Nightingale if (pci_pins == 0)
76bf21cd93STycho Nightingale return (-1);
774c87aefeSPatrick Mooney if (lpc_bootrom()) {
784c87aefeSPatrick Mooney /* For external bootrom use fixed mapping. */
794c87aefeSPatrick Mooney return (16 + (4 + pi->pi_slot + pi->pi_lintr.pin) % 8);
804c87aefeSPatrick Mooney }
81bf21cd93STycho Nightingale return (16 + (last_pin++ % pci_pins));
82bf21cd93STycho Nightingale }
83