1251877Speter/*-
2251877Speter * Copyright (c) 2014 Hudson River Trading LLC
3251877Speter * Written by: John H. Baldwin <jhb@FreeBSD.org>
4251877Speter * All rights reserved.
5251877Speter *
6251877Speter * Redistribution and use in source and binary forms, with or without
7251877Speter * modification, are permitted provided that the following conditions
8251877Speter * are met:
9251877Speter * 1. Redistributions of source code must retain the above copyright
10251877Speter *    notice, this list of conditions and the following disclaimer.
11251877Speter * 2. Redistributions in binary form must reproduce the above copyright
12251877Speter *    notice, this list of conditions and the following disclaimer in the
13251877Speter *    documentation and/or other materials provided with the distribution.
14251877Speter *
15251877Speter * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16251877Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17253895Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18251877Speter * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19253895Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20251877Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21262324Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22253895Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23253895Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24253895Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25253895Speter * SUCH DAMAGE.
26251877Speter */
27253895Speter
28262324Speter#include <sys/cdefs.h>
29251877Speter__FBSDID("$FreeBSD$");
30251877Speter
31253895Speter#include <sys/types.h>
32251877Speter
33253895Speter#include <machine/vmm.h>
34251877Speter#include <vmmapi.h>
35253895Speter
36251877Speter#include "ioapic.h"
37253895Speter
38253895Speter/*
39253895Speter * Assign PCI INTx interrupts to I/O APIC pins in a round-robin
40253895Speter * fashion.  Note that we have no idea what the HPET is using, but the
41253895Speter * HPET is also programmable whereas this is intended for hardwired
42253895Speter * PCI interrupts.
43253895Speter *
44253895Speter * This assumes a single I/O APIC where pins >= 16 are permitted for
45253895Speter * PCI devices.
46253895Speter */
47253895Speterstatic int pci_pins;
48253895Speter
49253895Spetervoid
50253895Speterioapic_init(struct vmctx *ctx)
51253895Speter{
52253895Speter
53253895Speter	if (vm_ioapic_pincount(ctx, &pci_pins) < 0) {
54253895Speter		pci_pins = 0;
55253895Speter		return;
56253895Speter	}
57262324Speter
58262324Speter	/* Ignore the first 16 pins. */
59262324Speter	if (pci_pins <= 16) {
60262324Speter		pci_pins = 0;
61262324Speter		return;
62262324Speter	}
63253895Speter	pci_pins -= 16;
64253895Speter}
65253895Speter
66253895Speterint
67253895Speterioapic_pci_alloc_irq(void)
68253895Speter{
69253895Speter	static int last_pin;
70253895Speter
71253895Speter	if (pci_pins == 0)
72253895Speter		return (-1);
73253895Speter	return (16 + (last_pin++ % pci_pins));
74253895Speter}
75253895Speter