xref: /freebsd/sys/amd64/vmm/io/ppt.c (revision f3754afd5901857787271e73f9c34d3b9069a03f)
1366f6083SPeter Grehan /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3c49761ddSPedro F. Giffuni  *
4366f6083SPeter Grehan  * Copyright (c) 2011 NetApp, Inc.
5366f6083SPeter Grehan  * All rights reserved.
6366f6083SPeter Grehan  *
7366f6083SPeter Grehan  * Redistribution and use in source and binary forms, with or without
8366f6083SPeter Grehan  * modification, are permitted provided that the following conditions
9366f6083SPeter Grehan  * are met:
10366f6083SPeter Grehan  * 1. Redistributions of source code must retain the above copyright
11366f6083SPeter Grehan  *    notice, this list of conditions and the following disclaimer.
12366f6083SPeter Grehan  * 2. Redistributions in binary form must reproduce the above copyright
13366f6083SPeter Grehan  *    notice, this list of conditions and the following disclaimer in the
14366f6083SPeter Grehan  *    documentation and/or other materials provided with the distribution.
15366f6083SPeter Grehan  *
16366f6083SPeter Grehan  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17366f6083SPeter Grehan  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18366f6083SPeter Grehan  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19366f6083SPeter Grehan  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20366f6083SPeter Grehan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21366f6083SPeter Grehan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22366f6083SPeter Grehan  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23366f6083SPeter Grehan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24366f6083SPeter Grehan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25366f6083SPeter Grehan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26366f6083SPeter Grehan  * SUCH DAMAGE.
27366f6083SPeter Grehan  */
28366f6083SPeter Grehan 
29366f6083SPeter Grehan #include <sys/param.h>
30366f6083SPeter Grehan #include <sys/systm.h>
31366f6083SPeter Grehan #include <sys/kernel.h>
32cd942e0fSPeter Grehan #include <sys/malloc.h>
33366f6083SPeter Grehan #include <sys/module.h>
34366f6083SPeter Grehan #include <sys/bus.h>
35366f6083SPeter Grehan #include <sys/pciio.h>
36366f6083SPeter Grehan #include <sys/rman.h>
37a5615c90SPeter Grehan #include <sys/smp.h>
3851f45d01SNeel Natu #include <sys/sysctl.h>
39366f6083SPeter Grehan 
40366f6083SPeter Grehan #include <dev/pci/pcivar.h>
41366f6083SPeter Grehan #include <dev/pci/pcireg.h>
42366f6083SPeter Grehan 
43366f6083SPeter Grehan #include <machine/resource.h>
44366f6083SPeter Grehan #include <machine/vmm.h>
45366f6083SPeter Grehan #include <machine/vmm_dev.h>
46366f6083SPeter Grehan 
47*3ccb0233SMark Johnston #include <dev/vmm/vmm_ktr.h>
48*3ccb0233SMark Johnston 
49366f6083SPeter Grehan #include "vmm_lapic.h"
50366f6083SPeter Grehan 
51366f6083SPeter Grehan #include "iommu.h"
52366f6083SPeter Grehan #include "ppt.h"
53366f6083SPeter Grehan 
5475dd3366SNeel Natu /* XXX locking */
5575dd3366SNeel Natu 
56366f6083SPeter Grehan #define	MAX_MSIMSGS	32
57366f6083SPeter Grehan 
582b89a044SNeel Natu /*
592b89a044SNeel Natu  * If the MSI-X table is located in the middle of a BAR then that MMIO
602b89a044SNeel Natu  * region gets split into two segments - one segment above the MSI-X table
612b89a044SNeel Natu  * and the other segment below the MSI-X table - with a hole in place of
622b89a044SNeel Natu  * the MSI-X table so accesses to it can be trapped and emulated.
632b89a044SNeel Natu  *
642b89a044SNeel Natu  * So, allocate a MMIO segment for each BAR register + 1 additional segment.
652b89a044SNeel Natu  */
662b89a044SNeel Natu #define	MAX_MMIOSEGS	((PCIR_MAX_BAR_0 + 1) + 1)
672b89a044SNeel Natu 
68cd942e0fSPeter Grehan MALLOC_DEFINE(M_PPTMSIX, "pptmsix", "Passthru MSI-X resources");
69cd942e0fSPeter Grehan 
70366f6083SPeter Grehan struct pptintr_arg {				/* pptintr(pptintr_arg) */
71366f6083SPeter Grehan 	struct pptdev	*pptdev;
724f8be175SNeel Natu 	uint64_t	addr;
734f8be175SNeel Natu 	uint64_t	msg_data;
74366f6083SPeter Grehan };
75366f6083SPeter Grehan 
769b1aa8d6SNeel Natu struct pptseg {
779b1aa8d6SNeel Natu 	vm_paddr_t	gpa;
789b1aa8d6SNeel Natu 	size_t		len;
799b1aa8d6SNeel Natu 	int		wired;
809b1aa8d6SNeel Natu };
819b1aa8d6SNeel Natu 
82a15f820aSRyan Stone struct pptdev {
83366f6083SPeter Grehan 	device_t	dev;
84366f6083SPeter Grehan 	struct vm	*vm;			/* owner of this device */
85a15f820aSRyan Stone 	TAILQ_ENTRY(pptdev)	next;
869b1aa8d6SNeel Natu 	struct pptseg mmio[MAX_MMIOSEGS];
87366f6083SPeter Grehan 	struct {
88366f6083SPeter Grehan 		int	num_msgs;		/* guest state */
89366f6083SPeter Grehan 
90366f6083SPeter Grehan 		int	startrid;		/* host state */
91366f6083SPeter Grehan 		struct resource *res[MAX_MSIMSGS];
92366f6083SPeter Grehan 		void	*cookie[MAX_MSIMSGS];
93366f6083SPeter Grehan 		struct pptintr_arg arg[MAX_MSIMSGS];
94366f6083SPeter Grehan 	} msi;
95cd942e0fSPeter Grehan 
96cd942e0fSPeter Grehan 	struct {
97cd942e0fSPeter Grehan 		int num_msgs;
98cd942e0fSPeter Grehan 		int startrid;
99cd942e0fSPeter Grehan 		int msix_table_rid;
1000d1fd6e5SJohn Baldwin 		int msix_pba_rid;
101cd942e0fSPeter Grehan 		struct resource *msix_table_res;
1020d1fd6e5SJohn Baldwin 		struct resource *msix_pba_res;
103cd942e0fSPeter Grehan 		struct resource **res;
104cd942e0fSPeter Grehan 		void **cookie;
105cd942e0fSPeter Grehan 		struct pptintr_arg *arg;
106cd942e0fSPeter Grehan 	} msix;
107a15f820aSRyan Stone };
108366f6083SPeter Grehan 
10951f45d01SNeel Natu SYSCTL_DECL(_hw_vmm);
110b40598c5SPawel Biernacki SYSCTL_NODE(_hw_vmm, OID_AUTO, ppt, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
111b40598c5SPawel Biernacki     "bhyve passthru devices");
11251f45d01SNeel Natu 
113366f6083SPeter Grehan static int num_pptdevs;
11451f45d01SNeel Natu SYSCTL_INT(_hw_vmm_ppt, OID_AUTO, devices, CTLFLAG_RD, &num_pptdevs, 0,
11551f45d01SNeel Natu     "number of pci passthru devices");
116366f6083SPeter Grehan 
117a15f820aSRyan Stone static TAILQ_HEAD(, pptdev) pptdev_list = TAILQ_HEAD_INITIALIZER(pptdev_list);
118a15f820aSRyan Stone 
119366f6083SPeter Grehan static int
ppt_probe(device_t dev)120366f6083SPeter Grehan ppt_probe(device_t dev)
121366f6083SPeter Grehan {
122366f6083SPeter Grehan 	int bus, slot, func;
123366f6083SPeter Grehan 	struct pci_devinfo *dinfo;
124366f6083SPeter Grehan 
125366f6083SPeter Grehan 	dinfo = (struct pci_devinfo *)device_get_ivars(dev);
126366f6083SPeter Grehan 
127366f6083SPeter Grehan 	bus = pci_get_bus(dev);
128366f6083SPeter Grehan 	slot = pci_get_slot(dev);
129366f6083SPeter Grehan 	func = pci_get_function(dev);
130366f6083SPeter Grehan 
131366f6083SPeter Grehan 	/*
132366f6083SPeter Grehan 	 * To qualify as a pci passthrough device a device must:
133366f6083SPeter Grehan 	 * - be allowed by administrator to be used in this role
134366f6083SPeter Grehan 	 * - be an endpoint device
135366f6083SPeter Grehan 	 */
136a15f820aSRyan Stone 	if ((dinfo->cfg.hdrtype & PCIM_HDRTYPE) != PCIM_HDRTYPE_NORMAL)
137a15f820aSRyan Stone 		return (ENXIO);
138a15f820aSRyan Stone 	else if (vmm_is_pptdev(bus, slot, func))
139366f6083SPeter Grehan 		return (0);
140366f6083SPeter Grehan 	else
141a15f820aSRyan Stone 		/*
142a15f820aSRyan Stone 		 * Returning BUS_PROBE_NOWILDCARD here matches devices that the
143a15f820aSRyan Stone 		 * SR-IOV infrastructure specified as "ppt" passthrough devices.
144a15f820aSRyan Stone 		 * All normal devices that did not have "ppt" specified as their
145a15f820aSRyan Stone 		 * driver will not be matched by this.
146a15f820aSRyan Stone 		 */
147a15f820aSRyan Stone 		return (BUS_PROBE_NOWILDCARD);
148366f6083SPeter Grehan }
149366f6083SPeter Grehan 
150366f6083SPeter Grehan static int
ppt_attach(device_t dev)151366f6083SPeter Grehan ppt_attach(device_t dev)
152366f6083SPeter Grehan {
153a15f820aSRyan Stone 	struct pptdev *ppt;
154776cd02bSJohn Baldwin 	uint16_t cmd;
155366f6083SPeter Grehan 
156a15f820aSRyan Stone 	ppt = device_get_softc(dev);
157366f6083SPeter Grehan 
158776cd02bSJohn Baldwin 	cmd = pci_read_config(dev, PCIR_COMMAND, 2);
159776cd02bSJohn Baldwin 	cmd &= ~(PCIM_CMD_PORTEN | PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN);
160776cd02bSJohn Baldwin 	pci_write_config(dev, PCIR_COMMAND, cmd, 2);
1616db55a0fSJohn Baldwin 	iommu_remove_device(iommu_host_domain(), pci_get_rid(dev));
162a15f820aSRyan Stone 	num_pptdevs++;
163a15f820aSRyan Stone 	TAILQ_INSERT_TAIL(&pptdev_list, ppt, next);
164a15f820aSRyan Stone 	ppt->dev = dev;
165366f6083SPeter Grehan 
166366f6083SPeter Grehan 	if (bootverbose)
167366f6083SPeter Grehan 		device_printf(dev, "attached\n");
168366f6083SPeter Grehan 
169366f6083SPeter Grehan 	return (0);
170366f6083SPeter Grehan }
171366f6083SPeter Grehan 
172366f6083SPeter Grehan static int
ppt_detach(device_t dev)173366f6083SPeter Grehan ppt_detach(device_t dev)
174366f6083SPeter Grehan {
175a15f820aSRyan Stone 	struct pptdev *ppt;
176a15f820aSRyan Stone 
177a15f820aSRyan Stone 	ppt = device_get_softc(dev);
178a15f820aSRyan Stone 
179a15f820aSRyan Stone 	if (ppt->vm != NULL)
180a15f820aSRyan Stone 		return (EBUSY);
181a15f820aSRyan Stone 	num_pptdevs--;
182a15f820aSRyan Stone 	TAILQ_REMOVE(&pptdev_list, ppt, next);
18327029bc0SRobert Wing 
18427029bc0SRobert Wing 	if (iommu_host_domain() != NULL)
1856db55a0fSJohn Baldwin 		iommu_add_device(iommu_host_domain(), pci_get_rid(dev));
186366f6083SPeter Grehan 
187366f6083SPeter Grehan 	return (0);
188366f6083SPeter Grehan }
189366f6083SPeter Grehan 
190366f6083SPeter Grehan static device_method_t ppt_methods[] = {
191366f6083SPeter Grehan 	/* Device interface */
192366f6083SPeter Grehan 	DEVMETHOD(device_probe,		ppt_probe),
193366f6083SPeter Grehan 	DEVMETHOD(device_attach,	ppt_attach),
194366f6083SPeter Grehan 	DEVMETHOD(device_detach,	ppt_detach),
195366f6083SPeter Grehan 	{0, 0}
196366f6083SPeter Grehan };
197366f6083SPeter Grehan 
198a15f820aSRyan Stone DEFINE_CLASS_0(ppt, ppt_driver, ppt_methods, sizeof(struct pptdev));
199badcc74fSJohn Baldwin DRIVER_MODULE(ppt, pci, ppt_driver, NULL, NULL);
200366f6083SPeter Grehan 
201908dca3eSJohn Baldwin static int
ppt_find(struct vm * vm,int bus,int slot,int func,struct pptdev ** pptp)202908dca3eSJohn Baldwin ppt_find(struct vm *vm, int bus, int slot, int func, struct pptdev **pptp)
203366f6083SPeter Grehan {
204366f6083SPeter Grehan 	device_t dev;
205a15f820aSRyan Stone 	struct pptdev *ppt;
206a15f820aSRyan Stone 	int b, s, f;
207366f6083SPeter Grehan 
208a15f820aSRyan Stone 	TAILQ_FOREACH(ppt, &pptdev_list, next) {
209a15f820aSRyan Stone 		dev = ppt->dev;
210366f6083SPeter Grehan 		b = pci_get_bus(dev);
211366f6083SPeter Grehan 		s = pci_get_slot(dev);
212366f6083SPeter Grehan 		f = pci_get_function(dev);
213366f6083SPeter Grehan 		if (bus == b && slot == s && func == f)
214908dca3eSJohn Baldwin 			break;
215366f6083SPeter Grehan 	}
216908dca3eSJohn Baldwin 
217908dca3eSJohn Baldwin 	if (ppt == NULL)
218908dca3eSJohn Baldwin 		return (ENOENT);
219908dca3eSJohn Baldwin 	if (ppt->vm != vm)		/* Make sure we own this device */
220908dca3eSJohn Baldwin 		return (EBUSY);
221908dca3eSJohn Baldwin 	*pptp = ppt;
222908dca3eSJohn Baldwin 	return (0);
223366f6083SPeter Grehan }
224366f6083SPeter Grehan 
225366f6083SPeter Grehan static void
ppt_unmap_all_mmio(struct vm * vm,struct pptdev * ppt)226f8a6ec2dSD Scott Phillips ppt_unmap_all_mmio(struct vm *vm, struct pptdev *ppt)
227366f6083SPeter Grehan {
228366f6083SPeter Grehan 	int i;
2299b1aa8d6SNeel Natu 	struct pptseg *seg;
230366f6083SPeter Grehan 
231366f6083SPeter Grehan 	for (i = 0; i < MAX_MMIOSEGS; i++) {
232366f6083SPeter Grehan 		seg = &ppt->mmio[i];
233366f6083SPeter Grehan 		if (seg->len == 0)
234366f6083SPeter Grehan 			continue;
235366f6083SPeter Grehan 		(void)vm_unmap_mmio(vm, seg->gpa, seg->len);
2369b1aa8d6SNeel Natu 		bzero(seg, sizeof(struct pptseg));
237366f6083SPeter Grehan 	}
238366f6083SPeter Grehan }
239366f6083SPeter Grehan 
240366f6083SPeter Grehan static void
ppt_teardown_msi(struct pptdev * ppt)241366f6083SPeter Grehan ppt_teardown_msi(struct pptdev *ppt)
242366f6083SPeter Grehan {
243366f6083SPeter Grehan 	int i, rid;
244366f6083SPeter Grehan 	void *cookie;
245366f6083SPeter Grehan 	struct resource *res;
246366f6083SPeter Grehan 
247366f6083SPeter Grehan 	if (ppt->msi.num_msgs == 0)
248366f6083SPeter Grehan 		return;
249366f6083SPeter Grehan 
250366f6083SPeter Grehan 	for (i = 0; i < ppt->msi.num_msgs; i++) {
251366f6083SPeter Grehan 		rid = ppt->msi.startrid + i;
252366f6083SPeter Grehan 		res = ppt->msi.res[i];
253366f6083SPeter Grehan 		cookie = ppt->msi.cookie[i];
254366f6083SPeter Grehan 
255366f6083SPeter Grehan 		if (cookie != NULL)
256366f6083SPeter Grehan 			bus_teardown_intr(ppt->dev, res, cookie);
257366f6083SPeter Grehan 
258366f6083SPeter Grehan 		if (res != NULL)
259366f6083SPeter Grehan 			bus_release_resource(ppt->dev, SYS_RES_IRQ, rid, res);
260366f6083SPeter Grehan 
261366f6083SPeter Grehan 		ppt->msi.res[i] = NULL;
262366f6083SPeter Grehan 		ppt->msi.cookie[i] = NULL;
263366f6083SPeter Grehan 	}
264366f6083SPeter Grehan 
265366f6083SPeter Grehan 	if (ppt->msi.startrid == 1)
266366f6083SPeter Grehan 		pci_release_msi(ppt->dev);
267366f6083SPeter Grehan 
268366f6083SPeter Grehan 	ppt->msi.num_msgs = 0;
269366f6083SPeter Grehan }
270366f6083SPeter Grehan 
271cd942e0fSPeter Grehan static void
ppt_teardown_msix_intr(struct pptdev * ppt,int idx)272cd942e0fSPeter Grehan ppt_teardown_msix_intr(struct pptdev *ppt, int idx)
273cd942e0fSPeter Grehan {
274cd942e0fSPeter Grehan 	int rid;
275cd942e0fSPeter Grehan 	struct resource *res;
276cd942e0fSPeter Grehan 	void *cookie;
277cd942e0fSPeter Grehan 
278cd942e0fSPeter Grehan 	rid = ppt->msix.startrid + idx;
279cd942e0fSPeter Grehan 	res = ppt->msix.res[idx];
280cd942e0fSPeter Grehan 	cookie = ppt->msix.cookie[idx];
281cd942e0fSPeter Grehan 
282cd942e0fSPeter Grehan 	if (cookie != NULL)
283cd942e0fSPeter Grehan 		bus_teardown_intr(ppt->dev, res, cookie);
284cd942e0fSPeter Grehan 
285cd942e0fSPeter Grehan 	if (res != NULL)
286cd942e0fSPeter Grehan 		bus_release_resource(ppt->dev, SYS_RES_IRQ, rid, res);
287cd942e0fSPeter Grehan 
288cd942e0fSPeter Grehan 	ppt->msix.res[idx] = NULL;
289cd942e0fSPeter Grehan 	ppt->msix.cookie[idx] = NULL;
290cd942e0fSPeter Grehan }
291cd942e0fSPeter Grehan 
292cd942e0fSPeter Grehan static void
ppt_teardown_msix(struct pptdev * ppt)293cd942e0fSPeter Grehan ppt_teardown_msix(struct pptdev *ppt)
294cd942e0fSPeter Grehan {
295920bc340SNeel Natu 	int i;
296cd942e0fSPeter Grehan 
297cd942e0fSPeter Grehan 	if (ppt->msix.num_msgs == 0)
298cd942e0fSPeter Grehan 		return;
299cd942e0fSPeter Grehan 
300cd942e0fSPeter Grehan 	for (i = 0; i < ppt->msix.num_msgs; i++)
301cd942e0fSPeter Grehan 		ppt_teardown_msix_intr(ppt, i);
302cd942e0fSPeter Grehan 
3030d1fd6e5SJohn Baldwin 	free(ppt->msix.res, M_PPTMSIX);
3040d1fd6e5SJohn Baldwin 	free(ppt->msix.cookie, M_PPTMSIX);
3050d1fd6e5SJohn Baldwin 	free(ppt->msix.arg, M_PPTMSIX);
3060d1fd6e5SJohn Baldwin 
3070d1fd6e5SJohn Baldwin 	pci_release_msi(ppt->dev);
3080d1fd6e5SJohn Baldwin 
309cd942e0fSPeter Grehan 	if (ppt->msix.msix_table_res) {
310cd942e0fSPeter Grehan 		bus_release_resource(ppt->dev, SYS_RES_MEMORY,
311cd942e0fSPeter Grehan 				     ppt->msix.msix_table_rid,
312cd942e0fSPeter Grehan 				     ppt->msix.msix_table_res);
313cd942e0fSPeter Grehan 		ppt->msix.msix_table_res = NULL;
314cd942e0fSPeter Grehan 		ppt->msix.msix_table_rid = 0;
315cd942e0fSPeter Grehan 	}
3160d1fd6e5SJohn Baldwin 	if (ppt->msix.msix_pba_res) {
3170d1fd6e5SJohn Baldwin 		bus_release_resource(ppt->dev, SYS_RES_MEMORY,
3180d1fd6e5SJohn Baldwin 				     ppt->msix.msix_pba_rid,
3190d1fd6e5SJohn Baldwin 				     ppt->msix.msix_pba_res);
3200d1fd6e5SJohn Baldwin 		ppt->msix.msix_pba_res = NULL;
3210d1fd6e5SJohn Baldwin 		ppt->msix.msix_pba_rid = 0;
3220d1fd6e5SJohn Baldwin 	}
323cd942e0fSPeter Grehan 
324cd942e0fSPeter Grehan 	ppt->msix.num_msgs = 0;
325cd942e0fSPeter Grehan }
326cd942e0fSPeter Grehan 
327366f6083SPeter Grehan int
ppt_avail_devices(void)32851f45d01SNeel Natu ppt_avail_devices(void)
32951f45d01SNeel Natu {
33051f45d01SNeel Natu 
33151f45d01SNeel Natu 	return (num_pptdevs);
33251f45d01SNeel Natu }
33351f45d01SNeel Natu 
33451f45d01SNeel Natu int
ppt_assigned_devices(struct vm * vm)33551f45d01SNeel Natu ppt_assigned_devices(struct vm *vm)
336318224bbSNeel Natu {
337a15f820aSRyan Stone 	struct pptdev *ppt;
338a15f820aSRyan Stone 	int num;
339318224bbSNeel Natu 
340318224bbSNeel Natu 	num = 0;
341a15f820aSRyan Stone 	TAILQ_FOREACH(ppt, &pptdev_list, next) {
342a15f820aSRyan Stone 		if (ppt->vm == vm)
343318224bbSNeel Natu 			num++;
344318224bbSNeel Natu 	}
345318224bbSNeel Natu 	return (num);
346318224bbSNeel Natu }
347318224bbSNeel Natu 
348490d56c5SEd Maste bool
ppt_is_mmio(struct vm * vm,vm_paddr_t gpa)349318224bbSNeel Natu ppt_is_mmio(struct vm *vm, vm_paddr_t gpa)
350318224bbSNeel Natu {
351a15f820aSRyan Stone 	int i;
352318224bbSNeel Natu 	struct pptdev *ppt;
3539b1aa8d6SNeel Natu 	struct pptseg *seg;
354318224bbSNeel Natu 
355a15f820aSRyan Stone 	TAILQ_FOREACH(ppt, &pptdev_list, next) {
356318224bbSNeel Natu 		if (ppt->vm != vm)
357318224bbSNeel Natu 			continue;
358318224bbSNeel Natu 
359318224bbSNeel Natu 		for (i = 0; i < MAX_MMIOSEGS; i++) {
360318224bbSNeel Natu 			seg = &ppt->mmio[i];
361318224bbSNeel Natu 			if (seg->len == 0)
362318224bbSNeel Natu 				continue;
363318224bbSNeel Natu 			if (gpa >= seg->gpa && gpa < seg->gpa + seg->len)
364490d56c5SEd Maste 				return (true);
365318224bbSNeel Natu 		}
366318224bbSNeel Natu 	}
367318224bbSNeel Natu 
368490d56c5SEd Maste 	return (false);
369318224bbSNeel Natu }
370318224bbSNeel Natu 
371adb947a6SPeter Grehan static void
ppt_pci_reset(device_t dev)372adb947a6SPeter Grehan ppt_pci_reset(device_t dev)
373adb947a6SPeter Grehan {
374adb947a6SPeter Grehan 
375adb947a6SPeter Grehan 	if (pcie_flr(dev,
3765db2a4a8SKonstantin Belousov 	     max(pcie_get_max_completion_timeout(dev) / 1000, 10), true))
377adb947a6SPeter Grehan 		return;
378adb947a6SPeter Grehan 
3795db2a4a8SKonstantin Belousov 	pci_power_reset(dev);
380adb947a6SPeter Grehan }
381adb947a6SPeter Grehan 
382776cd02bSJohn Baldwin static uint16_t
ppt_bar_enables(struct pptdev * ppt)383776cd02bSJohn Baldwin ppt_bar_enables(struct pptdev *ppt)
384776cd02bSJohn Baldwin {
385776cd02bSJohn Baldwin 	struct pci_map *pm;
386776cd02bSJohn Baldwin 	uint16_t cmd;
387776cd02bSJohn Baldwin 
388776cd02bSJohn Baldwin 	cmd = 0;
389776cd02bSJohn Baldwin 	for (pm = pci_first_bar(ppt->dev); pm != NULL; pm = pci_next_bar(pm)) {
390776cd02bSJohn Baldwin 		if (PCI_BAR_IO(pm->pm_value))
391776cd02bSJohn Baldwin 			cmd |= PCIM_CMD_PORTEN;
392776cd02bSJohn Baldwin 		if (PCI_BAR_MEM(pm->pm_value))
393776cd02bSJohn Baldwin 			cmd |= PCIM_CMD_MEMEN;
394776cd02bSJohn Baldwin 	}
395776cd02bSJohn Baldwin 	return (cmd);
396776cd02bSJohn Baldwin }
397776cd02bSJohn Baldwin 
398318224bbSNeel Natu int
ppt_assign_device(struct vm * vm,int bus,int slot,int func)399366f6083SPeter Grehan ppt_assign_device(struct vm *vm, int bus, int slot, int func)
400366f6083SPeter Grehan {
401366f6083SPeter Grehan 	struct pptdev *ppt;
402908dca3eSJohn Baldwin 	int error;
403776cd02bSJohn Baldwin 	uint16_t cmd;
404366f6083SPeter Grehan 
405908dca3eSJohn Baldwin 	/* Passing NULL requires the device to be unowned. */
406908dca3eSJohn Baldwin 	error = ppt_find(NULL, bus, slot, func, &ppt);
407908dca3eSJohn Baldwin 	if (error)
408908dca3eSJohn Baldwin 		return (error);
409366f6083SPeter Grehan 
410da0fc925SJohn Baldwin 	pci_save_state(ppt->dev);
411adb947a6SPeter Grehan 	ppt_pci_reset(ppt->dev);
412da0fc925SJohn Baldwin 	pci_restore_state(ppt->dev);
413366f6083SPeter Grehan 	ppt->vm = vm;
414a8667250SRyan Stone 	iommu_add_device(vm_iommu_domain(vm), pci_get_rid(ppt->dev));
415776cd02bSJohn Baldwin 	cmd = pci_read_config(ppt->dev, PCIR_COMMAND, 2);
416776cd02bSJohn Baldwin 	cmd |= PCIM_CMD_BUSMASTEREN | ppt_bar_enables(ppt);
417776cd02bSJohn Baldwin 	pci_write_config(ppt->dev, PCIR_COMMAND, cmd, 2);
418366f6083SPeter Grehan 	return (0);
419366f6083SPeter Grehan }
420366f6083SPeter Grehan 
421366f6083SPeter Grehan int
ppt_unassign_device(struct vm * vm,int bus,int slot,int func)422366f6083SPeter Grehan ppt_unassign_device(struct vm *vm, int bus, int slot, int func)
423366f6083SPeter Grehan {
424366f6083SPeter Grehan 	struct pptdev *ppt;
425908dca3eSJohn Baldwin 	int error;
426776cd02bSJohn Baldwin 	uint16_t cmd;
427366f6083SPeter Grehan 
428908dca3eSJohn Baldwin 	error = ppt_find(vm, bus, slot, func, &ppt);
429908dca3eSJohn Baldwin 	if (error)
430908dca3eSJohn Baldwin 		return (error);
431da0fc925SJohn Baldwin 
432776cd02bSJohn Baldwin 	cmd = pci_read_config(ppt->dev, PCIR_COMMAND, 2);
433776cd02bSJohn Baldwin 	cmd &= ~(PCIM_CMD_PORTEN | PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN);
434776cd02bSJohn Baldwin 	pci_write_config(ppt->dev, PCIR_COMMAND, cmd, 2);
435da0fc925SJohn Baldwin 	pci_save_state(ppt->dev);
436adb947a6SPeter Grehan 	ppt_pci_reset(ppt->dev);
437da0fc925SJohn Baldwin 	pci_restore_state(ppt->dev);
438f8a6ec2dSD Scott Phillips 	ppt_unmap_all_mmio(vm, ppt);
439366f6083SPeter Grehan 	ppt_teardown_msi(ppt);
440cd942e0fSPeter Grehan 	ppt_teardown_msix(ppt);
441a8667250SRyan Stone 	iommu_remove_device(vm_iommu_domain(vm), pci_get_rid(ppt->dev));
442366f6083SPeter Grehan 	ppt->vm = NULL;
443366f6083SPeter Grehan 	return (0);
444366f6083SPeter Grehan }
445366f6083SPeter Grehan 
446366f6083SPeter Grehan int
ppt_unassign_all(struct vm * vm)447366f6083SPeter Grehan ppt_unassign_all(struct vm *vm)
448366f6083SPeter Grehan {
449a15f820aSRyan Stone 	struct pptdev *ppt;
450a15f820aSRyan Stone 	int bus, slot, func;
451366f6083SPeter Grehan 	device_t dev;
452366f6083SPeter Grehan 
453a15f820aSRyan Stone 	TAILQ_FOREACH(ppt, &pptdev_list, next) {
454a15f820aSRyan Stone 		if (ppt->vm == vm) {
455a15f820aSRyan Stone 			dev = ppt->dev;
456366f6083SPeter Grehan 			bus = pci_get_bus(dev);
457366f6083SPeter Grehan 			slot = pci_get_slot(dev);
458366f6083SPeter Grehan 			func = pci_get_function(dev);
459318224bbSNeel Natu 			vm_unassign_pptdev(vm, bus, slot, func);
460366f6083SPeter Grehan 		}
461366f6083SPeter Grehan 	}
462366f6083SPeter Grehan 
463366f6083SPeter Grehan 	return (0);
464366f6083SPeter Grehan }
465366f6083SPeter Grehan 
466c94f30eaSJohn Baldwin static bool
ppt_valid_bar_mapping(struct pptdev * ppt,vm_paddr_t hpa,size_t len)467c94f30eaSJohn Baldwin ppt_valid_bar_mapping(struct pptdev *ppt, vm_paddr_t hpa, size_t len)
468c94f30eaSJohn Baldwin {
469c94f30eaSJohn Baldwin 	struct pci_map *pm;
470c94f30eaSJohn Baldwin 	pci_addr_t base, size;
471c94f30eaSJohn Baldwin 
472c94f30eaSJohn Baldwin 	for (pm = pci_first_bar(ppt->dev); pm != NULL; pm = pci_next_bar(pm)) {
473c94f30eaSJohn Baldwin 		if (!PCI_BAR_MEM(pm->pm_value))
474c94f30eaSJohn Baldwin 			continue;
475c94f30eaSJohn Baldwin 		base = pm->pm_value & PCIM_BAR_MEM_BASE;
476c94f30eaSJohn Baldwin 		size = (pci_addr_t)1 << pm->pm_size;
477c94f30eaSJohn Baldwin 		if (hpa >= base && hpa + len <= base + size)
478c94f30eaSJohn Baldwin 			return (true);
479c94f30eaSJohn Baldwin 	}
480c94f30eaSJohn Baldwin 	return (false);
481c94f30eaSJohn Baldwin }
482c94f30eaSJohn Baldwin 
483366f6083SPeter Grehan int
ppt_map_mmio(struct vm * vm,int bus,int slot,int func,vm_paddr_t gpa,size_t len,vm_paddr_t hpa)484366f6083SPeter Grehan ppt_map_mmio(struct vm *vm, int bus, int slot, int func,
485366f6083SPeter Grehan 	     vm_paddr_t gpa, size_t len, vm_paddr_t hpa)
486366f6083SPeter Grehan {
487366f6083SPeter Grehan 	int i, error;
4889b1aa8d6SNeel Natu 	struct pptseg *seg;
489366f6083SPeter Grehan 	struct pptdev *ppt;
490366f6083SPeter Grehan 
491c94f30eaSJohn Baldwin 	if (len % PAGE_SIZE != 0 || len == 0 || gpa % PAGE_SIZE != 0 ||
492c94f30eaSJohn Baldwin 	    hpa % PAGE_SIZE != 0 || gpa + len < gpa || hpa + len < hpa)
493c94f30eaSJohn Baldwin 		return (EINVAL);
494c94f30eaSJohn Baldwin 
495908dca3eSJohn Baldwin 	error = ppt_find(vm, bus, slot, func, &ppt);
496908dca3eSJohn Baldwin 	if (error)
497908dca3eSJohn Baldwin 		return (error);
498366f6083SPeter Grehan 
499c94f30eaSJohn Baldwin 	if (!ppt_valid_bar_mapping(ppt, hpa, len))
500c94f30eaSJohn Baldwin 		return (EINVAL);
501c94f30eaSJohn Baldwin 
502366f6083SPeter Grehan 	for (i = 0; i < MAX_MMIOSEGS; i++) {
503366f6083SPeter Grehan 		seg = &ppt->mmio[i];
504366f6083SPeter Grehan 		if (seg->len == 0) {
505366f6083SPeter Grehan 			error = vm_map_mmio(vm, gpa, len, hpa);
506366f6083SPeter Grehan 			if (error == 0) {
507366f6083SPeter Grehan 				seg->gpa = gpa;
508366f6083SPeter Grehan 				seg->len = len;
509366f6083SPeter Grehan 			}
510366f6083SPeter Grehan 			return (error);
511366f6083SPeter Grehan 		}
512366f6083SPeter Grehan 	}
513366f6083SPeter Grehan 	return (ENOSPC);
514366f6083SPeter Grehan }
515366f6083SPeter Grehan 
516f8a6ec2dSD Scott Phillips int
ppt_unmap_mmio(struct vm * vm,int bus,int slot,int func,vm_paddr_t gpa,size_t len)517f8a6ec2dSD Scott Phillips ppt_unmap_mmio(struct vm *vm, int bus, int slot, int func,
518f8a6ec2dSD Scott Phillips 	       vm_paddr_t gpa, size_t len)
519f8a6ec2dSD Scott Phillips {
520f8a6ec2dSD Scott Phillips 	int i, error;
521f8a6ec2dSD Scott Phillips 	struct pptseg *seg;
522f8a6ec2dSD Scott Phillips 	struct pptdev *ppt;
523f8a6ec2dSD Scott Phillips 
524f8a6ec2dSD Scott Phillips 	error = ppt_find(vm, bus, slot, func, &ppt);
525f8a6ec2dSD Scott Phillips 	if (error)
526f8a6ec2dSD Scott Phillips 		return (error);
527f8a6ec2dSD Scott Phillips 
528f8a6ec2dSD Scott Phillips 	for (i = 0; i < MAX_MMIOSEGS; i++) {
529f8a6ec2dSD Scott Phillips 		seg = &ppt->mmio[i];
530f8a6ec2dSD Scott Phillips 		if (seg->gpa == gpa && seg->len == len) {
531f8a6ec2dSD Scott Phillips 			error = vm_unmap_mmio(vm, seg->gpa, seg->len);
532f8a6ec2dSD Scott Phillips 			if (error == 0) {
533f8a6ec2dSD Scott Phillips 				seg->gpa = 0;
534f8a6ec2dSD Scott Phillips 				seg->len = 0;
535f8a6ec2dSD Scott Phillips 			}
536f8a6ec2dSD Scott Phillips 			return (error);
537f8a6ec2dSD Scott Phillips 		}
538f8a6ec2dSD Scott Phillips 	}
539f8a6ec2dSD Scott Phillips 	return (ENOENT);
540f8a6ec2dSD Scott Phillips }
541f8a6ec2dSD Scott Phillips 
542366f6083SPeter Grehan static int
pptintr(void * arg)543366f6083SPeter Grehan pptintr(void *arg)
544366f6083SPeter Grehan {
545366f6083SPeter Grehan 	struct pptdev *ppt;
546366f6083SPeter Grehan 	struct pptintr_arg *pptarg;
547366f6083SPeter Grehan 
548366f6083SPeter Grehan 	pptarg = arg;
549366f6083SPeter Grehan 	ppt = pptarg->pptdev;
550366f6083SPeter Grehan 
551366f6083SPeter Grehan 	if (ppt->vm != NULL)
5524f8be175SNeel Natu 		lapic_intr_msi(ppt->vm, pptarg->addr, pptarg->msg_data);
553366f6083SPeter Grehan 	else {
554366f6083SPeter Grehan 		/*
555366f6083SPeter Grehan 		 * XXX
556366f6083SPeter Grehan 		 * This is not expected to happen - panic?
557366f6083SPeter Grehan 		 */
558366f6083SPeter Grehan 	}
559366f6083SPeter Grehan 
560366f6083SPeter Grehan 	/*
561366f6083SPeter Grehan 	 * For legacy interrupts give other filters a chance in case
562366f6083SPeter Grehan 	 * the interrupt was not generated by the passthrough device.
563366f6083SPeter Grehan 	 */
564366f6083SPeter Grehan 	if (ppt->msi.startrid == 0)
565366f6083SPeter Grehan 		return (FILTER_STRAY);
566366f6083SPeter Grehan 	else
567366f6083SPeter Grehan 		return (FILTER_HANDLED);
568366f6083SPeter Grehan }
569366f6083SPeter Grehan 
570366f6083SPeter Grehan int
ppt_setup_msi(struct vm * vm,int bus,int slot,int func,uint64_t addr,uint64_t msg,int numvec)5710cbc39d5SJohn Baldwin ppt_setup_msi(struct vm *vm, int bus, int slot, int func,
5724f8be175SNeel Natu 	      uint64_t addr, uint64_t msg, int numvec)
573366f6083SPeter Grehan {
574366f6083SPeter Grehan 	int i, rid, flags;
575366f6083SPeter Grehan 	int msi_count, startrid, error, tmp;
576366f6083SPeter Grehan 	struct pptdev *ppt;
577366f6083SPeter Grehan 
5784f8be175SNeel Natu 	if (numvec < 0 || numvec > MAX_MSIMSGS)
579366f6083SPeter Grehan 		return (EINVAL);
580366f6083SPeter Grehan 
581908dca3eSJohn Baldwin 	error = ppt_find(vm, bus, slot, func, &ppt);
582908dca3eSJohn Baldwin 	if (error)
583908dca3eSJohn Baldwin 		return (error);
584366f6083SPeter Grehan 
5851925586eSJohn Baldwin 	/* Reject attempts to enable MSI while MSI-X is active. */
5861925586eSJohn Baldwin 	if (ppt->msix.num_msgs != 0 && numvec != 0)
5871925586eSJohn Baldwin 		return (EBUSY);
5881925586eSJohn Baldwin 
589366f6083SPeter Grehan 	/* Free any allocated resources */
590485b3300SNeel Natu 	ppt_teardown_msi(ppt);
591366f6083SPeter Grehan 
592366f6083SPeter Grehan 	if (numvec == 0)		/* nothing more to do */
593366f6083SPeter Grehan 		return (0);
594366f6083SPeter Grehan 
595366f6083SPeter Grehan 	flags = RF_ACTIVE;
596366f6083SPeter Grehan 	msi_count = pci_msi_count(ppt->dev);
597366f6083SPeter Grehan 	if (msi_count == 0) {
598366f6083SPeter Grehan 		startrid = 0;		/* legacy interrupt */
599366f6083SPeter Grehan 		msi_count = 1;
600366f6083SPeter Grehan 		flags |= RF_SHAREABLE;
601366f6083SPeter Grehan 	} else
602366f6083SPeter Grehan 		startrid = 1;		/* MSI */
603366f6083SPeter Grehan 
604366f6083SPeter Grehan 	/*
605366f6083SPeter Grehan 	 * The device must be capable of supporting the number of vectors
606366f6083SPeter Grehan 	 * the guest wants to allocate.
607366f6083SPeter Grehan 	 */
608366f6083SPeter Grehan 	if (numvec > msi_count)
609366f6083SPeter Grehan 		return (EINVAL);
610366f6083SPeter Grehan 
611366f6083SPeter Grehan 	/*
612366f6083SPeter Grehan 	 * Make sure that we can allocate all the MSI vectors that are needed
613366f6083SPeter Grehan 	 * by the guest.
614366f6083SPeter Grehan 	 */
615366f6083SPeter Grehan 	if (startrid == 1) {
616366f6083SPeter Grehan 		tmp = numvec;
617366f6083SPeter Grehan 		error = pci_alloc_msi(ppt->dev, &tmp);
618366f6083SPeter Grehan 		if (error)
619366f6083SPeter Grehan 			return (error);
620366f6083SPeter Grehan 		else if (tmp != numvec) {
621366f6083SPeter Grehan 			pci_release_msi(ppt->dev);
622366f6083SPeter Grehan 			return (ENOSPC);
623366f6083SPeter Grehan 		} else {
624366f6083SPeter Grehan 			/* success */
625366f6083SPeter Grehan 		}
626366f6083SPeter Grehan 	}
627366f6083SPeter Grehan 
628366f6083SPeter Grehan 	ppt->msi.startrid = startrid;
629366f6083SPeter Grehan 
630366f6083SPeter Grehan 	/*
631366f6083SPeter Grehan 	 * Allocate the irq resource and attach it to the interrupt handler.
632366f6083SPeter Grehan 	 */
633366f6083SPeter Grehan 	for (i = 0; i < numvec; i++) {
634366f6083SPeter Grehan 		ppt->msi.num_msgs = i + 1;
635366f6083SPeter Grehan 		ppt->msi.cookie[i] = NULL;
636366f6083SPeter Grehan 
637366f6083SPeter Grehan 		rid = startrid + i;
638366f6083SPeter Grehan 		ppt->msi.res[i] = bus_alloc_resource_any(ppt->dev, SYS_RES_IRQ,
639366f6083SPeter Grehan 							 &rid, flags);
640366f6083SPeter Grehan 		if (ppt->msi.res[i] == NULL)
641366f6083SPeter Grehan 			break;
642366f6083SPeter Grehan 
643366f6083SPeter Grehan 		ppt->msi.arg[i].pptdev = ppt;
6444f8be175SNeel Natu 		ppt->msi.arg[i].addr = addr;
6454f8be175SNeel Natu 		ppt->msi.arg[i].msg_data = msg + i;
646366f6083SPeter Grehan 
647366f6083SPeter Grehan 		error = bus_setup_intr(ppt->dev, ppt->msi.res[i],
64834a6b2d6SJohn Baldwin 				       INTR_TYPE_NET | INTR_MPSAFE,
649366f6083SPeter Grehan 				       pptintr, NULL, &ppt->msi.arg[i],
650366f6083SPeter Grehan 				       &ppt->msi.cookie[i]);
651366f6083SPeter Grehan 		if (error != 0)
652366f6083SPeter Grehan 			break;
653366f6083SPeter Grehan 	}
654366f6083SPeter Grehan 
655366f6083SPeter Grehan 	if (i < numvec) {
656485b3300SNeel Natu 		ppt_teardown_msi(ppt);
657366f6083SPeter Grehan 		return (ENXIO);
658366f6083SPeter Grehan 	}
659366f6083SPeter Grehan 
660366f6083SPeter Grehan 	return (0);
661366f6083SPeter Grehan }
662cd942e0fSPeter Grehan 
663cd942e0fSPeter Grehan int
ppt_setup_msix(struct vm * vm,int bus,int slot,int func,int idx,uint64_t addr,uint64_t msg,uint32_t vector_control)6640cbc39d5SJohn Baldwin ppt_setup_msix(struct vm *vm, int bus, int slot, int func,
6654f8be175SNeel Natu 	       int idx, uint64_t addr, uint64_t msg, uint32_t vector_control)
666cd942e0fSPeter Grehan {
667cd942e0fSPeter Grehan 	struct pptdev *ppt;
668cd942e0fSPeter Grehan 	struct pci_devinfo *dinfo;
669920bc340SNeel Natu 	int numvec, alloced, rid, error;
670cd942e0fSPeter Grehan 	size_t res_size, cookie_size, arg_size;
671cd942e0fSPeter Grehan 
672908dca3eSJohn Baldwin 	error = ppt_find(vm, bus, slot, func, &ppt);
673908dca3eSJohn Baldwin 	if (error)
674908dca3eSJohn Baldwin 		return (error);
675cd942e0fSPeter Grehan 
6761925586eSJohn Baldwin 	/* Reject attempts to enable MSI-X while MSI is active. */
6771925586eSJohn Baldwin 	if (ppt->msi.num_msgs != 0)
6781925586eSJohn Baldwin 		return (EBUSY);
6791925586eSJohn Baldwin 
680cd942e0fSPeter Grehan 	dinfo = device_get_ivars(ppt->dev);
681cd942e0fSPeter Grehan 	if (!dinfo)
682cd942e0fSPeter Grehan 		return (ENXIO);
683cd942e0fSPeter Grehan 
684cd942e0fSPeter Grehan 	/*
685cd942e0fSPeter Grehan 	 * First-time configuration:
686cd942e0fSPeter Grehan 	 * 	Allocate the MSI-X table
687cd942e0fSPeter Grehan 	 *	Allocate the IRQ resources
688cd942e0fSPeter Grehan 	 *	Set up some variables in ppt->msix
689cd942e0fSPeter Grehan 	 */
690920bc340SNeel Natu 	if (ppt->msix.num_msgs == 0) {
691920bc340SNeel Natu 		numvec = pci_msix_count(ppt->dev);
692920bc340SNeel Natu 		if (numvec <= 0)
693920bc340SNeel Natu 			return (EINVAL);
694cd942e0fSPeter Grehan 
695cd942e0fSPeter Grehan 		ppt->msix.startrid = 1;
696920bc340SNeel Natu 		ppt->msix.num_msgs = numvec;
697cd942e0fSPeter Grehan 
698cd942e0fSPeter Grehan 		res_size = numvec * sizeof(ppt->msix.res[0]);
699cd942e0fSPeter Grehan 		cookie_size = numvec * sizeof(ppt->msix.cookie[0]);
700cd942e0fSPeter Grehan 		arg_size = numvec * sizeof(ppt->msix.arg[0]);
701cd942e0fSPeter Grehan 
702920bc340SNeel Natu 		ppt->msix.res = malloc(res_size, M_PPTMSIX, M_WAITOK | M_ZERO);
703920bc340SNeel Natu 		ppt->msix.cookie = malloc(cookie_size, M_PPTMSIX,
704920bc340SNeel Natu 					  M_WAITOK | M_ZERO);
705920bc340SNeel Natu 		ppt->msix.arg = malloc(arg_size, M_PPTMSIX, M_WAITOK | M_ZERO);
706920bc340SNeel Natu 
707920bc340SNeel Natu 		rid = dinfo->cfg.msix.msix_table_bar;
708920bc340SNeel Natu 		ppt->msix.msix_table_res = bus_alloc_resource_any(ppt->dev,
709920bc340SNeel Natu 					       SYS_RES_MEMORY, &rid, RF_ACTIVE);
710920bc340SNeel Natu 
711920bc340SNeel Natu 		if (ppt->msix.msix_table_res == NULL) {
712cd942e0fSPeter Grehan 			ppt_teardown_msix(ppt);
713cd942e0fSPeter Grehan 			return (ENOSPC);
714cd942e0fSPeter Grehan 		}
715920bc340SNeel Natu 		ppt->msix.msix_table_rid = rid;
716920bc340SNeel Natu 
7170d1fd6e5SJohn Baldwin 		if (dinfo->cfg.msix.msix_table_bar !=
7180d1fd6e5SJohn Baldwin 		    dinfo->cfg.msix.msix_pba_bar) {
7190d1fd6e5SJohn Baldwin 			rid = dinfo->cfg.msix.msix_pba_bar;
7200d1fd6e5SJohn Baldwin 			ppt->msix.msix_pba_res = bus_alloc_resource_any(
7210d1fd6e5SJohn Baldwin 			    ppt->dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
7220d1fd6e5SJohn Baldwin 
7230d1fd6e5SJohn Baldwin 			if (ppt->msix.msix_pba_res == NULL) {
7240d1fd6e5SJohn Baldwin 				ppt_teardown_msix(ppt);
7250d1fd6e5SJohn Baldwin 				return (ENOSPC);
7260d1fd6e5SJohn Baldwin 			}
7270d1fd6e5SJohn Baldwin 			ppt->msix.msix_pba_rid = rid;
7280d1fd6e5SJohn Baldwin 		}
7290d1fd6e5SJohn Baldwin 
730920bc340SNeel Natu 		alloced = numvec;
731920bc340SNeel Natu 		error = pci_alloc_msix(ppt->dev, &alloced);
732920bc340SNeel Natu 		if (error || alloced != numvec) {
733920bc340SNeel Natu 			ppt_teardown_msix(ppt);
734920bc340SNeel Natu 			return (error == 0 ? ENOSPC: error);
735920bc340SNeel Natu 		}
736cd942e0fSPeter Grehan 	}
737cd942e0fSPeter Grehan 
738cd942e0fSPeter Grehan 	if ((vector_control & PCIM_MSIX_VCTRL_MASK) == 0) {
739cd942e0fSPeter Grehan 		/* Tear down the IRQ if it's already set up */
740cd942e0fSPeter Grehan 		ppt_teardown_msix_intr(ppt, idx);
741cd942e0fSPeter Grehan 
742cd942e0fSPeter Grehan 		/* Allocate the IRQ resource */
743cd942e0fSPeter Grehan 		ppt->msix.cookie[idx] = NULL;
744cd942e0fSPeter Grehan 		rid = ppt->msix.startrid + idx;
745cd942e0fSPeter Grehan 		ppt->msix.res[idx] = bus_alloc_resource_any(ppt->dev, SYS_RES_IRQ,
746cd942e0fSPeter Grehan 							    &rid, RF_ACTIVE);
747cd942e0fSPeter Grehan 		if (ppt->msix.res[idx] == NULL)
748cd942e0fSPeter Grehan 			return (ENXIO);
749cd942e0fSPeter Grehan 
750cd942e0fSPeter Grehan 		ppt->msix.arg[idx].pptdev = ppt;
7514f8be175SNeel Natu 		ppt->msix.arg[idx].addr = addr;
7524f8be175SNeel Natu 		ppt->msix.arg[idx].msg_data = msg;
753cd942e0fSPeter Grehan 
754cd942e0fSPeter Grehan 		/* Setup the MSI-X interrupt */
755cd942e0fSPeter Grehan 		error = bus_setup_intr(ppt->dev, ppt->msix.res[idx],
756cd942e0fSPeter Grehan 				       INTR_TYPE_NET | INTR_MPSAFE,
757cd942e0fSPeter Grehan 				       pptintr, NULL, &ppt->msix.arg[idx],
758cd942e0fSPeter Grehan 				       &ppt->msix.cookie[idx]);
759cd942e0fSPeter Grehan 
760cd942e0fSPeter Grehan 		if (error != 0) {
761cd942e0fSPeter Grehan 			bus_release_resource(ppt->dev, SYS_RES_IRQ, rid, ppt->msix.res[idx]);
762cd942e0fSPeter Grehan 			ppt->msix.cookie[idx] = NULL;
763cd942e0fSPeter Grehan 			ppt->msix.res[idx] = NULL;
764cd942e0fSPeter Grehan 			return (ENXIO);
765cd942e0fSPeter Grehan 		}
766cd942e0fSPeter Grehan 	} else {
767cd942e0fSPeter Grehan 		/* Masked, tear it down if it's already been set up */
768cd942e0fSPeter Grehan 		ppt_teardown_msix_intr(ppt, idx);
769cd942e0fSPeter Grehan 	}
770cd942e0fSPeter Grehan 
771cd942e0fSPeter Grehan 	return (0);
772cd942e0fSPeter Grehan }
7731925586eSJohn Baldwin 
7741925586eSJohn Baldwin int
ppt_disable_msix(struct vm * vm,int bus,int slot,int func)7751925586eSJohn Baldwin ppt_disable_msix(struct vm *vm, int bus, int slot, int func)
7761925586eSJohn Baldwin {
7771925586eSJohn Baldwin 	struct pptdev *ppt;
778908dca3eSJohn Baldwin 	int error;
7791925586eSJohn Baldwin 
780908dca3eSJohn Baldwin 	error = ppt_find(vm, bus, slot, func, &ppt);
781908dca3eSJohn Baldwin 	if (error)
782908dca3eSJohn Baldwin 		return (error);
7831925586eSJohn Baldwin 
7841925586eSJohn Baldwin 	ppt_teardown_msix(ppt);
7851925586eSJohn Baldwin 	return (0);
7861925586eSJohn Baldwin }
787