xref: /freebsd/sys/dev/xen/xenpci/xenpci.c (revision 3e5e0e2f16785210d796ea6ece4b69d35a03fe47)
1718cf2ccSPedro F. Giffuni /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
412678024SDoug Rabson  * Copyright (c) 2008 Citrix Systems, Inc.
512678024SDoug Rabson  * All rights reserved.
612678024SDoug Rabson  *
712678024SDoug Rabson  * Redistribution and use in source and binary forms, with or without
812678024SDoug Rabson  * modification, are permitted provided that the following conditions
912678024SDoug Rabson  * are met:
1012678024SDoug Rabson  * 1. Redistributions of source code must retain the above copyright
1112678024SDoug Rabson  *    notice, this list of conditions and the following disclaimer.
1212678024SDoug Rabson  * 2. Redistributions in binary form must reproduce the above copyright
1312678024SDoug Rabson  *    notice, this list of conditions and the following disclaimer in the
1412678024SDoug Rabson  *    documentation and/or other materials provided with the distribution.
1512678024SDoug Rabson  *
1612678024SDoug Rabson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS AS IS'' AND
1712678024SDoug Rabson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1812678024SDoug Rabson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1912678024SDoug Rabson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2012678024SDoug Rabson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2112678024SDoug Rabson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2212678024SDoug Rabson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2312678024SDoug Rabson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2412678024SDoug Rabson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2512678024SDoug Rabson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2612678024SDoug Rabson  * SUCH DAMAGE.
2712678024SDoug Rabson  */
2812678024SDoug Rabson 
2912678024SDoug Rabson #include <sys/param.h>
3012678024SDoug Rabson #include <sys/bus.h>
3112678024SDoug Rabson #include <sys/kernel.h>
3212678024SDoug Rabson #include <sys/malloc.h>
3312678024SDoug Rabson #include <sys/module.h>
3412678024SDoug Rabson 
3512678024SDoug Rabson #include <machine/bus.h>
3612678024SDoug Rabson #include <machine/resource.h>
3712678024SDoug Rabson #include <sys/rman.h>
3812678024SDoug Rabson 
3912678024SDoug Rabson #include <machine/stdarg.h>
4076acc41fSJustin T. Gibbs 
4176acc41fSJustin T. Gibbs #include <xen/xen-os.h>
4212678024SDoug Rabson #include <xen/features.h>
4312678024SDoug Rabson #include <xen/hypervisor.h>
4476acc41fSJustin T. Gibbs #include <xen/hvm.h>
452f9ec994SRoger Pau Monné #include <xen/xen_intr.h>
4612678024SDoug Rabson 
4712678024SDoug Rabson #include <dev/pci/pcireg.h>
4812678024SDoug Rabson #include <dev/pci/pcivar.h>
4912678024SDoug Rabson 
5012678024SDoug Rabson #include <dev/xen/xenpci/xenpcivar.h>
5112678024SDoug Rabson 
5212678024SDoug Rabson static int
xenpci_irq_init(device_t device,struct xenpci_softc * scp)5376acc41fSJustin T. Gibbs xenpci_irq_init(device_t device, struct xenpci_softc *scp)
5476acc41fSJustin T. Gibbs {
5576acc41fSJustin T. Gibbs 	int error;
5676acc41fSJustin T. Gibbs 
5776acc41fSJustin T. Gibbs 	error = BUS_SETUP_INTR(device_get_parent(device), device,
5876acc41fSJustin T. Gibbs 			       scp->res_irq, INTR_MPSAFE|INTR_TYPE_MISC,
59af610cabSElliott Mitchell 			       xen_intr_handle_upcall, NULL, NULL,
6076acc41fSJustin T. Gibbs 			       &scp->intr_cookie);
6176acc41fSJustin T. Gibbs 	if (error)
6276acc41fSJustin T. Gibbs 		return error;
6312678024SDoug Rabson 
64428b7ca2SJustin T. Gibbs #ifdef SMP
6512678024SDoug Rabson 	/*
6676acc41fSJustin T. Gibbs 	 * When using the PCI event delivery callback we cannot assign
6776acc41fSJustin T. Gibbs 	 * events to specific vCPUs, so all events are delivered to vCPU#0 by
6876acc41fSJustin T. Gibbs 	 * Xen. Since the PCI interrupt can fire on any CPU by default, we
6976acc41fSJustin T. Gibbs 	 * need to bind it to vCPU#0 in order to ensure that
7076acc41fSJustin T. Gibbs 	 * xen_intr_handle_upcall always gets called on vCPU#0.
7112678024SDoug Rabson 	 */
7276acc41fSJustin T. Gibbs 	error = BUS_BIND_INTR(device_get_parent(device), device,
7376acc41fSJustin T. Gibbs 	                      scp->res_irq, 0);
7476acc41fSJustin T. Gibbs 	if (error)
7576acc41fSJustin T. Gibbs 		return error;
76428b7ca2SJustin T. Gibbs #endif
7712678024SDoug Rabson 
7876acc41fSJustin T. Gibbs 	xen_hvm_set_callback(device);
7912678024SDoug Rabson 	return (0);
8012678024SDoug Rabson }
8112678024SDoug Rabson 
8212678024SDoug Rabson /*
8312678024SDoug Rabson  * Deallocate anything allocated by xenpci_allocate_resources.
8412678024SDoug Rabson  */
8512678024SDoug Rabson static int
xenpci_deallocate_resources(device_t dev)8612678024SDoug Rabson xenpci_deallocate_resources(device_t dev)
8712678024SDoug Rabson {
8812678024SDoug Rabson 	struct xenpci_softc *scp = device_get_softc(dev);
8912678024SDoug Rabson 
9012678024SDoug Rabson 	if (scp->res_irq != 0) {
9112678024SDoug Rabson 		bus_deactivate_resource(dev, SYS_RES_IRQ,
9212678024SDoug Rabson 			scp->rid_irq, scp->res_irq);
9312678024SDoug Rabson 		bus_release_resource(dev, SYS_RES_IRQ,
9412678024SDoug Rabson 			scp->rid_irq, scp->res_irq);
9512678024SDoug Rabson 		scp->res_irq = 0;
9612678024SDoug Rabson 	}
9712678024SDoug Rabson 
9812678024SDoug Rabson 	return (0);
9912678024SDoug Rabson }
10012678024SDoug Rabson 
10112678024SDoug Rabson /*
10212678024SDoug Rabson  * Allocate irq and memory resources.
10312678024SDoug Rabson  */
10412678024SDoug Rabson static int
xenpci_allocate_resources(device_t dev)10512678024SDoug Rabson xenpci_allocate_resources(device_t dev)
10612678024SDoug Rabson {
10712678024SDoug Rabson 	struct xenpci_softc *scp = device_get_softc(dev);
10812678024SDoug Rabson 
10912678024SDoug Rabson 	scp->res_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
11012678024SDoug Rabson 			&scp->rid_irq, RF_SHAREABLE|RF_ACTIVE);
111ff662b5cSJustin T. Gibbs 	if (scp->res_irq == NULL) {
112ff662b5cSJustin T. Gibbs 		printf("xenpci Could not allocate irq.\n");
11312678024SDoug Rabson 		goto errexit;
114ff662b5cSJustin T. Gibbs 	}
11512678024SDoug Rabson 
11612678024SDoug Rabson 	return (0);
11712678024SDoug Rabson 
11812678024SDoug Rabson errexit:
11912678024SDoug Rabson 	/* Cleanup anything we may have assigned. */
12012678024SDoug Rabson 	xenpci_deallocate_resources(dev);
12112678024SDoug Rabson 	return (ENXIO); /* For want of a better idea. */
12212678024SDoug Rabson }
12312678024SDoug Rabson 
12412678024SDoug Rabson /*
12512678024SDoug Rabson  * Probe - just check device ID.
12612678024SDoug Rabson  */
12712678024SDoug Rabson static int
xenpci_probe(device_t dev)12812678024SDoug Rabson xenpci_probe(device_t dev)
12912678024SDoug Rabson {
13012678024SDoug Rabson 
13112678024SDoug Rabson 	if (pci_get_devid(dev) != 0x00015853)
13212678024SDoug Rabson 		return (ENXIO);
13312678024SDoug Rabson 
13412678024SDoug Rabson 	device_set_desc(dev, "Xen Platform Device");
135aa64d12bSRoger Pau Monné 	return (BUS_PROBE_DEFAULT);
13612678024SDoug Rabson }
13712678024SDoug Rabson 
13812678024SDoug Rabson /*
13912678024SDoug Rabson  * Attach - find resources and talk to Xen.
14012678024SDoug Rabson  */
14112678024SDoug Rabson static int
xenpci_attach(device_t dev)14212678024SDoug Rabson xenpci_attach(device_t dev)
14312678024SDoug Rabson {
14412678024SDoug Rabson 	struct xenpci_softc *scp = device_get_softc(dev);
14576acc41fSJustin T. Gibbs 	int error;
146ff662b5cSJustin T. Gibbs 
14712678024SDoug Rabson 	error = xenpci_allocate_resources(dev);
148ff662b5cSJustin T. Gibbs 	if (error) {
149ff662b5cSJustin T. Gibbs 		device_printf(dev, "xenpci_allocate_resources failed(%d).\n",
150ff662b5cSJustin T. Gibbs 		    error);
15112678024SDoug Rabson 		goto errexit;
152ff662b5cSJustin T. Gibbs 	}
15312678024SDoug Rabson 
15412678024SDoug Rabson 	/*
15512678024SDoug Rabson 	 * Hook the irq up to evtchn
15612678024SDoug Rabson 	 */
15776acc41fSJustin T. Gibbs 	error = xenpci_irq_init(dev, scp);
15876acc41fSJustin T. Gibbs 	if (error) {
15976acc41fSJustin T. Gibbs 		device_printf(dev, "xenpci_irq_init failed(%d).\n",
16076acc41fSJustin T. Gibbs 			error);
16176acc41fSJustin T. Gibbs 		goto errexit;
16276acc41fSJustin T. Gibbs 	}
16312678024SDoug Rabson 
164aa64d12bSRoger Pau Monné 	return (0);
16512678024SDoug Rabson 
16612678024SDoug Rabson errexit:
16712678024SDoug Rabson 	/*
16812678024SDoug Rabson 	 * Undo anything we may have done.
16912678024SDoug Rabson 	 */
17012678024SDoug Rabson 	xenpci_deallocate_resources(dev);
17112678024SDoug Rabson 	return (error);
17212678024SDoug Rabson }
17312678024SDoug Rabson 
17412678024SDoug Rabson /*
17512678024SDoug Rabson  * Detach - reverse anything done by attach.
17612678024SDoug Rabson  */
17712678024SDoug Rabson static int
xenpci_detach(device_t dev)17812678024SDoug Rabson xenpci_detach(device_t dev)
17912678024SDoug Rabson {
18012678024SDoug Rabson 	struct xenpci_softc *scp = device_get_softc(dev);
18112678024SDoug Rabson 	device_t parent = device_get_parent(dev);
18212678024SDoug Rabson 
18312678024SDoug Rabson 	/*
18412678024SDoug Rabson 	 * Take our interrupt handler out of the list of handlers
18512678024SDoug Rabson 	 * that can handle this irq.
18612678024SDoug Rabson 	 */
18712678024SDoug Rabson 	if (scp->intr_cookie != NULL) {
18812678024SDoug Rabson 		if (BUS_TEARDOWN_INTR(parent, dev,
18912678024SDoug Rabson 		    scp->res_irq, scp->intr_cookie) != 0)
190ff662b5cSJustin T. Gibbs 			device_printf(dev,
191ff662b5cSJustin T. Gibbs 			    "intr teardown failed.. continuing\n");
19212678024SDoug Rabson 		scp->intr_cookie = NULL;
19312678024SDoug Rabson 	}
19412678024SDoug Rabson 
19512678024SDoug Rabson 	/*
19612678024SDoug Rabson 	 * Deallocate any system resources we may have
19712678024SDoug Rabson 	 * allocated on behalf of this driver.
19812678024SDoug Rabson 	 */
19912678024SDoug Rabson 	return (xenpci_deallocate_resources(dev));
20012678024SDoug Rabson }
20112678024SDoug Rabson 
20276acc41fSJustin T. Gibbs static int
xenpci_resume(device_t dev)20376acc41fSJustin T. Gibbs xenpci_resume(device_t dev)
20476acc41fSJustin T. Gibbs {
20576acc41fSJustin T. Gibbs 	xen_hvm_set_callback(dev);
206aa64d12bSRoger Pau Monné 	return (0);
20776acc41fSJustin T. Gibbs }
20876acc41fSJustin T. Gibbs 
20912678024SDoug Rabson static device_method_t xenpci_methods[] = {
21012678024SDoug Rabson 	/* Device interface */
21112678024SDoug Rabson 	DEVMETHOD(device_probe,		xenpci_probe),
21212678024SDoug Rabson 	DEVMETHOD(device_attach,	xenpci_attach),
21312678024SDoug Rabson 	DEVMETHOD(device_detach,	xenpci_detach),
21476acc41fSJustin T. Gibbs 	DEVMETHOD(device_resume,	xenpci_resume),
215*3e5e0e2fSElliott Mitchell 
216*3e5e0e2fSElliott Mitchell 	DEVMETHOD_END
21712678024SDoug Rabson };
21812678024SDoug Rabson 
21912678024SDoug Rabson static driver_t xenpci_driver = {
22012678024SDoug Rabson 	"xenpci",
22112678024SDoug Rabson 	xenpci_methods,
22212678024SDoug Rabson 	sizeof(struct xenpci_softc),
22312678024SDoug Rabson };
22412678024SDoug Rabson 
225f929eb1eSJohn Baldwin DRIVER_MODULE(xenpci, pci, xenpci_driver, 0, 0);
226