1/*
2 * Copyright (c) 2013 Roger Pau Monn�� <roger.pau@citrix.com>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD$");
29
30#include <sys/param.h>
31#include <sys/bus.h>
32#include <sys/kernel.h>
33#include <sys/module.h>
34#include <sys/sysctl.h>
35#include <sys/systm.h>
36#include <sys/smp.h>
37
38#include <contrib/dev/acpica/include/acpi.h>
39
40#include <dev/acpica/acpivar.h>
41
42#include <x86/init.h>
43#include <machine/nexusvar.h>
44#include <machine/intr_machdep.h>
45
46#include <xen/xen-os.h>
47#include <xen/xen_intr.h>
48#include <xen/xen_msi.h>
49
50#include "pcib_if.h"
51
52/*
53 * Xen nexus(4) driver.
54 */
55static int
56nexus_xen_probe(device_t dev)
57{
58
59	if (!xen_pv_domain())
60		return (ENXIO);
61
62	return (BUS_PROBE_SPECIFIC);
63}
64
65static int
66nexus_xen_attach(device_t dev)
67{
68	int error;
69	device_t acpi_dev = NULL;
70
71	nexus_init_resources();
72	bus_generic_probe(dev);
73
74	if (xen_initial_domain()) {
75		/* Disable some ACPI devices that are not usable by Dom0 */
76		acpi_cpu_disabled = true;
77		acpi_hpet_disabled = true;
78		acpi_timer_disabled = true;
79
80		acpi_dev = BUS_ADD_CHILD(dev, 10, "acpi", 0);
81		if (acpi_dev == NULL)
82			panic("Unable to add ACPI bus to Xen Dom0");
83	}
84
85	error = bus_generic_attach(dev);
86	if (xen_initial_domain() && (error == 0))
87		acpi_install_wakeup_handler(device_get_softc(acpi_dev));
88
89	return (error);
90}
91
92static int
93nexus_xen_config_intr(device_t dev, int irq, enum intr_trigger trig,
94    enum intr_polarity pol)
95{
96	int ret;
97
98	/*
99	 * ISA and PCI intline IRQs are not preregistered on Xen, so
100	 * intercept calls to configure those and register them on the fly.
101	 */
102	if ((irq < first_msi_irq) && (intr_lookup_source(irq) == NULL)) {
103		ret = xen_register_pirq(irq, trig, pol);
104		if (ret != 0)
105			return (ret);
106		nexus_add_irq(irq);
107	}
108	return (intr_config_intr(irq, trig, pol));
109}
110
111static int
112nexus_xen_alloc_msix(device_t pcib, device_t dev, int *irq)
113{
114
115	return (xen_msix_alloc(dev, irq));
116}
117
118static int
119nexus_xen_release_msix(device_t pcib, device_t dev, int irq)
120{
121
122	return (xen_msix_release(irq));
123}
124
125static int
126nexus_xen_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs)
127{
128
129	return (xen_msi_alloc(dev, count, maxcount, irqs));
130}
131
132static int
133nexus_xen_release_msi(device_t pcib, device_t dev, int count, int *irqs)
134{
135
136	return (xen_msi_release(irqs, count));
137}
138
139static int
140nexus_xen_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr, uint32_t *data)
141{
142
143	return (xen_msi_map(irq, addr, data));
144}
145
146static device_method_t nexus_xen_methods[] = {
147	/* Device interface */
148	DEVMETHOD(device_probe,		nexus_xen_probe),
149	DEVMETHOD(device_attach,	nexus_xen_attach),
150
151	/* INTR */
152	DEVMETHOD(bus_config_intr,	nexus_xen_config_intr),
153
154	/* MSI */
155	DEVMETHOD(pcib_alloc_msi,	nexus_xen_alloc_msi),
156	DEVMETHOD(pcib_release_msi,	nexus_xen_release_msi),
157	DEVMETHOD(pcib_alloc_msix,	nexus_xen_alloc_msix),
158	DEVMETHOD(pcib_release_msix,	nexus_xen_release_msix),
159	DEVMETHOD(pcib_map_msi,		nexus_xen_map_msi),
160
161	{ 0, 0 }
162};
163
164DEFINE_CLASS_1(nexus, nexus_xen_driver, nexus_xen_methods, 1, nexus_driver);
165static devclass_t nexus_devclass;
166
167DRIVER_MODULE(nexus_xen, root, nexus_xen_driver, nexus_devclass, 0, 0);
168