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