xref: /freebsd/sys/amd64/vmm/io/ppt.c (revision 5db2a4a8129dcf4b2d4c6ddfe4ad980a4c8657e2)
1366f6083SPeter Grehan /*-
2c49761ddSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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  * $FreeBSD$
29366f6083SPeter Grehan  */
30366f6083SPeter Grehan 
31366f6083SPeter Grehan #include <sys/cdefs.h>
32366f6083SPeter Grehan __FBSDID("$FreeBSD$");
33366f6083SPeter Grehan 
34366f6083SPeter Grehan #include <sys/param.h>
35366f6083SPeter Grehan #include <sys/systm.h>
36366f6083SPeter Grehan #include <sys/kernel.h>
37cd942e0fSPeter Grehan #include <sys/malloc.h>
38366f6083SPeter Grehan #include <sys/module.h>
39366f6083SPeter Grehan #include <sys/bus.h>
40366f6083SPeter Grehan #include <sys/pciio.h>
41366f6083SPeter Grehan #include <sys/rman.h>
42a5615c90SPeter Grehan #include <sys/smp.h>
4351f45d01SNeel Natu #include <sys/sysctl.h>
44366f6083SPeter Grehan 
45366f6083SPeter Grehan #include <dev/pci/pcivar.h>
46366f6083SPeter Grehan #include <dev/pci/pcireg.h>
47366f6083SPeter Grehan 
48366f6083SPeter Grehan #include <machine/resource.h>
49366f6083SPeter Grehan 
50366f6083SPeter Grehan #include <machine/vmm.h>
51366f6083SPeter Grehan #include <machine/vmm_dev.h>
52366f6083SPeter Grehan 
53366f6083SPeter Grehan #include "vmm_lapic.h"
54366f6083SPeter Grehan #include "vmm_ktr.h"
55366f6083SPeter Grehan 
56366f6083SPeter Grehan #include "iommu.h"
57366f6083SPeter Grehan #include "ppt.h"
58366f6083SPeter Grehan 
5975dd3366SNeel Natu /* XXX locking */
6075dd3366SNeel Natu 
61366f6083SPeter Grehan #define	MAX_MSIMSGS	32
62366f6083SPeter Grehan 
632b89a044SNeel Natu /*
642b89a044SNeel Natu  * If the MSI-X table is located in the middle of a BAR then that MMIO
652b89a044SNeel Natu  * region gets split into two segments - one segment above the MSI-X table
662b89a044SNeel Natu  * and the other segment below the MSI-X table - with a hole in place of
672b89a044SNeel Natu  * the MSI-X table so accesses to it can be trapped and emulated.
682b89a044SNeel Natu  *
692b89a044SNeel Natu  * So, allocate a MMIO segment for each BAR register + 1 additional segment.
702b89a044SNeel Natu  */
712b89a044SNeel Natu #define	MAX_MMIOSEGS	((PCIR_MAX_BAR_0 + 1) + 1)
722b89a044SNeel Natu 
73cd942e0fSPeter Grehan MALLOC_DEFINE(M_PPTMSIX, "pptmsix", "Passthru MSI-X resources");
74cd942e0fSPeter Grehan 
75366f6083SPeter Grehan struct pptintr_arg {				/* pptintr(pptintr_arg) */
76366f6083SPeter Grehan 	struct pptdev	*pptdev;
774f8be175SNeel Natu 	uint64_t	addr;
784f8be175SNeel Natu 	uint64_t	msg_data;
79366f6083SPeter Grehan };
80366f6083SPeter Grehan 
819b1aa8d6SNeel Natu struct pptseg {
829b1aa8d6SNeel Natu 	vm_paddr_t	gpa;
839b1aa8d6SNeel Natu 	size_t		len;
849b1aa8d6SNeel Natu 	int		wired;
859b1aa8d6SNeel Natu };
869b1aa8d6SNeel Natu 
87a15f820aSRyan Stone struct pptdev {
88366f6083SPeter Grehan 	device_t	dev;
89366f6083SPeter Grehan 	struct vm	*vm;			/* owner of this device */
90a15f820aSRyan Stone 	TAILQ_ENTRY(pptdev)	next;
919b1aa8d6SNeel Natu 	struct pptseg mmio[MAX_MMIOSEGS];
92366f6083SPeter Grehan 	struct {
93366f6083SPeter Grehan 		int	num_msgs;		/* guest state */
94366f6083SPeter Grehan 
95366f6083SPeter Grehan 		int	startrid;		/* host state */
96366f6083SPeter Grehan 		struct resource *res[MAX_MSIMSGS];
97366f6083SPeter Grehan 		void	*cookie[MAX_MSIMSGS];
98366f6083SPeter Grehan 		struct pptintr_arg arg[MAX_MSIMSGS];
99366f6083SPeter Grehan 	} msi;
100cd942e0fSPeter Grehan 
101cd942e0fSPeter Grehan 	struct {
102cd942e0fSPeter Grehan 		int num_msgs;
103cd942e0fSPeter Grehan 		int startrid;
104cd942e0fSPeter Grehan 		int msix_table_rid;
105cd942e0fSPeter Grehan 		struct resource *msix_table_res;
106cd942e0fSPeter Grehan 		struct resource **res;
107cd942e0fSPeter Grehan 		void **cookie;
108cd942e0fSPeter Grehan 		struct pptintr_arg *arg;
109cd942e0fSPeter Grehan 	} msix;
110a15f820aSRyan Stone };
111366f6083SPeter Grehan 
11251f45d01SNeel Natu SYSCTL_DECL(_hw_vmm);
11351f45d01SNeel Natu SYSCTL_NODE(_hw_vmm, OID_AUTO, ppt, CTLFLAG_RW, 0, "bhyve passthru devices");
11451f45d01SNeel Natu 
115366f6083SPeter Grehan static int num_pptdevs;
11651f45d01SNeel Natu SYSCTL_INT(_hw_vmm_ppt, OID_AUTO, devices, CTLFLAG_RD, &num_pptdevs, 0,
11751f45d01SNeel Natu     "number of pci passthru devices");
118366f6083SPeter Grehan 
119a15f820aSRyan Stone static TAILQ_HEAD(, pptdev) pptdev_list = TAILQ_HEAD_INITIALIZER(pptdev_list);
120a15f820aSRyan Stone 
121366f6083SPeter Grehan static int
122366f6083SPeter Grehan ppt_probe(device_t dev)
123366f6083SPeter Grehan {
124366f6083SPeter Grehan 	int bus, slot, func;
125366f6083SPeter Grehan 	struct pci_devinfo *dinfo;
126366f6083SPeter Grehan 
127366f6083SPeter Grehan 	dinfo = (struct pci_devinfo *)device_get_ivars(dev);
128366f6083SPeter Grehan 
129366f6083SPeter Grehan 	bus = pci_get_bus(dev);
130366f6083SPeter Grehan 	slot = pci_get_slot(dev);
131366f6083SPeter Grehan 	func = pci_get_function(dev);
132366f6083SPeter Grehan 
133366f6083SPeter Grehan 	/*
134366f6083SPeter Grehan 	 * To qualify as a pci passthrough device a device must:
135366f6083SPeter Grehan 	 * - be allowed by administrator to be used in this role
136366f6083SPeter Grehan 	 * - be an endpoint device
137366f6083SPeter Grehan 	 */
138a15f820aSRyan Stone 	if ((dinfo->cfg.hdrtype & PCIM_HDRTYPE) != PCIM_HDRTYPE_NORMAL)
139a15f820aSRyan Stone 		return (ENXIO);
140a15f820aSRyan Stone 	else if (vmm_is_pptdev(bus, slot, func))
141366f6083SPeter Grehan 		return (0);
142366f6083SPeter Grehan 	else
143a15f820aSRyan Stone 		/*
144a15f820aSRyan Stone 		 * Returning BUS_PROBE_NOWILDCARD here matches devices that the
145a15f820aSRyan Stone 		 * SR-IOV infrastructure specified as "ppt" passthrough devices.
146a15f820aSRyan Stone 		 * All normal devices that did not have "ppt" specified as their
147a15f820aSRyan Stone 		 * driver will not be matched by this.
148a15f820aSRyan Stone 		 */
149a15f820aSRyan Stone 		return (BUS_PROBE_NOWILDCARD);
150366f6083SPeter Grehan }
151366f6083SPeter Grehan 
152366f6083SPeter Grehan static int
153366f6083SPeter Grehan ppt_attach(device_t dev)
154366f6083SPeter Grehan {
155a15f820aSRyan Stone 	struct pptdev *ppt;
156366f6083SPeter Grehan 
157a15f820aSRyan Stone 	ppt = device_get_softc(dev);
158366f6083SPeter Grehan 
1596db55a0fSJohn Baldwin 	iommu_remove_device(iommu_host_domain(), pci_get_rid(dev));
160a15f820aSRyan Stone 	num_pptdevs++;
161a15f820aSRyan Stone 	TAILQ_INSERT_TAIL(&pptdev_list, ppt, next);
162a15f820aSRyan Stone 	ppt->dev = dev;
163366f6083SPeter Grehan 
164366f6083SPeter Grehan 	if (bootverbose)
165366f6083SPeter Grehan 		device_printf(dev, "attached\n");
166366f6083SPeter Grehan 
167366f6083SPeter Grehan 	return (0);
168366f6083SPeter Grehan }
169366f6083SPeter Grehan 
170366f6083SPeter Grehan static int
171366f6083SPeter Grehan ppt_detach(device_t dev)
172366f6083SPeter Grehan {
173a15f820aSRyan Stone 	struct pptdev *ppt;
174a15f820aSRyan Stone 
175a15f820aSRyan Stone 	ppt = device_get_softc(dev);
176a15f820aSRyan Stone 
177a15f820aSRyan Stone 	if (ppt->vm != NULL)
178a15f820aSRyan Stone 		return (EBUSY);
179a15f820aSRyan Stone 	num_pptdevs--;
180a15f820aSRyan Stone 	TAILQ_REMOVE(&pptdev_list, ppt, next);
1816db55a0fSJohn Baldwin 	pci_disable_busmaster(dev);
1826db55a0fSJohn Baldwin 	iommu_add_device(iommu_host_domain(), pci_get_rid(dev));
183366f6083SPeter Grehan 
184366f6083SPeter Grehan 	return (0);
185366f6083SPeter Grehan }
186366f6083SPeter Grehan 
187366f6083SPeter Grehan static device_method_t ppt_methods[] = {
188366f6083SPeter Grehan 	/* Device interface */
189366f6083SPeter Grehan 	DEVMETHOD(device_probe,		ppt_probe),
190366f6083SPeter Grehan 	DEVMETHOD(device_attach,	ppt_attach),
191366f6083SPeter Grehan 	DEVMETHOD(device_detach,	ppt_detach),
192366f6083SPeter Grehan 	{0, 0}
193366f6083SPeter Grehan };
194366f6083SPeter Grehan 
195366f6083SPeter Grehan static devclass_t ppt_devclass;
196a15f820aSRyan Stone DEFINE_CLASS_0(ppt, ppt_driver, ppt_methods, sizeof(struct pptdev));
197366f6083SPeter Grehan DRIVER_MODULE(ppt, pci, ppt_driver, ppt_devclass, NULL, NULL);
198366f6083SPeter Grehan 
199366f6083SPeter Grehan static struct pptdev *
200366f6083SPeter Grehan ppt_find(int bus, int slot, int func)
201366f6083SPeter Grehan {
202366f6083SPeter Grehan 	device_t dev;
203a15f820aSRyan Stone 	struct pptdev *ppt;
204a15f820aSRyan Stone 	int b, s, f;
205366f6083SPeter Grehan 
206a15f820aSRyan Stone 	TAILQ_FOREACH(ppt, &pptdev_list, next) {
207a15f820aSRyan Stone 		dev = ppt->dev;
208366f6083SPeter Grehan 		b = pci_get_bus(dev);
209366f6083SPeter Grehan 		s = pci_get_slot(dev);
210366f6083SPeter Grehan 		f = pci_get_function(dev);
211366f6083SPeter Grehan 		if (bus == b && slot == s && func == f)
212a15f820aSRyan Stone 			return (ppt);
213366f6083SPeter Grehan 	}
214366f6083SPeter Grehan 	return (NULL);
215366f6083SPeter Grehan }
216366f6083SPeter Grehan 
217366f6083SPeter Grehan static void
218366f6083SPeter Grehan ppt_unmap_mmio(struct vm *vm, struct pptdev *ppt)
219366f6083SPeter Grehan {
220366f6083SPeter Grehan 	int i;
2219b1aa8d6SNeel Natu 	struct pptseg *seg;
222366f6083SPeter Grehan 
223366f6083SPeter Grehan 	for (i = 0; i < MAX_MMIOSEGS; i++) {
224366f6083SPeter Grehan 		seg = &ppt->mmio[i];
225366f6083SPeter Grehan 		if (seg->len == 0)
226366f6083SPeter Grehan 			continue;
227366f6083SPeter Grehan 		(void)vm_unmap_mmio(vm, seg->gpa, seg->len);
2289b1aa8d6SNeel Natu 		bzero(seg, sizeof(struct pptseg));
229366f6083SPeter Grehan 	}
230366f6083SPeter Grehan }
231366f6083SPeter Grehan 
232366f6083SPeter Grehan static void
233366f6083SPeter Grehan ppt_teardown_msi(struct pptdev *ppt)
234366f6083SPeter Grehan {
235366f6083SPeter Grehan 	int i, rid;
236366f6083SPeter Grehan 	void *cookie;
237366f6083SPeter Grehan 	struct resource *res;
238366f6083SPeter Grehan 
239366f6083SPeter Grehan 	if (ppt->msi.num_msgs == 0)
240366f6083SPeter Grehan 		return;
241366f6083SPeter Grehan 
242366f6083SPeter Grehan 	for (i = 0; i < ppt->msi.num_msgs; i++) {
243366f6083SPeter Grehan 		rid = ppt->msi.startrid + i;
244366f6083SPeter Grehan 		res = ppt->msi.res[i];
245366f6083SPeter Grehan 		cookie = ppt->msi.cookie[i];
246366f6083SPeter Grehan 
247366f6083SPeter Grehan 		if (cookie != NULL)
248366f6083SPeter Grehan 			bus_teardown_intr(ppt->dev, res, cookie);
249366f6083SPeter Grehan 
250366f6083SPeter Grehan 		if (res != NULL)
251366f6083SPeter Grehan 			bus_release_resource(ppt->dev, SYS_RES_IRQ, rid, res);
252366f6083SPeter Grehan 
253366f6083SPeter Grehan 		ppt->msi.res[i] = NULL;
254366f6083SPeter Grehan 		ppt->msi.cookie[i] = NULL;
255366f6083SPeter Grehan 	}
256366f6083SPeter Grehan 
257366f6083SPeter Grehan 	if (ppt->msi.startrid == 1)
258366f6083SPeter Grehan 		pci_release_msi(ppt->dev);
259366f6083SPeter Grehan 
260366f6083SPeter Grehan 	ppt->msi.num_msgs = 0;
261366f6083SPeter Grehan }
262366f6083SPeter Grehan 
263cd942e0fSPeter Grehan static void
264cd942e0fSPeter Grehan ppt_teardown_msix_intr(struct pptdev *ppt, int idx)
265cd942e0fSPeter Grehan {
266cd942e0fSPeter Grehan 	int rid;
267cd942e0fSPeter Grehan 	struct resource *res;
268cd942e0fSPeter Grehan 	void *cookie;
269cd942e0fSPeter Grehan 
270cd942e0fSPeter Grehan 	rid = ppt->msix.startrid + idx;
271cd942e0fSPeter Grehan 	res = ppt->msix.res[idx];
272cd942e0fSPeter Grehan 	cookie = ppt->msix.cookie[idx];
273cd942e0fSPeter Grehan 
274cd942e0fSPeter Grehan 	if (cookie != NULL)
275cd942e0fSPeter Grehan 		bus_teardown_intr(ppt->dev, res, cookie);
276cd942e0fSPeter Grehan 
277cd942e0fSPeter Grehan 	if (res != NULL)
278cd942e0fSPeter Grehan 		bus_release_resource(ppt->dev, SYS_RES_IRQ, rid, res);
279cd942e0fSPeter Grehan 
280cd942e0fSPeter Grehan 	ppt->msix.res[idx] = NULL;
281cd942e0fSPeter Grehan 	ppt->msix.cookie[idx] = NULL;
282cd942e0fSPeter Grehan }
283cd942e0fSPeter Grehan 
284cd942e0fSPeter Grehan static void
285cd942e0fSPeter Grehan ppt_teardown_msix(struct pptdev *ppt)
286cd942e0fSPeter Grehan {
287920bc340SNeel Natu 	int i;
288cd942e0fSPeter Grehan 
289cd942e0fSPeter Grehan 	if (ppt->msix.num_msgs == 0)
290cd942e0fSPeter Grehan 		return;
291cd942e0fSPeter Grehan 
292cd942e0fSPeter Grehan 	for (i = 0; i < ppt->msix.num_msgs; i++)
293cd942e0fSPeter Grehan 		ppt_teardown_msix_intr(ppt, i);
294cd942e0fSPeter Grehan 
295cd942e0fSPeter Grehan 	if (ppt->msix.msix_table_res) {
296cd942e0fSPeter Grehan 		bus_release_resource(ppt->dev, SYS_RES_MEMORY,
297cd942e0fSPeter Grehan 				     ppt->msix.msix_table_rid,
298cd942e0fSPeter Grehan 				     ppt->msix.msix_table_res);
299cd942e0fSPeter Grehan 		ppt->msix.msix_table_res = NULL;
300cd942e0fSPeter Grehan 		ppt->msix.msix_table_rid = 0;
301cd942e0fSPeter Grehan 	}
302cd942e0fSPeter Grehan 
303cd942e0fSPeter Grehan 	free(ppt->msix.res, M_PPTMSIX);
304cd942e0fSPeter Grehan 	free(ppt->msix.cookie, M_PPTMSIX);
305cd942e0fSPeter Grehan 	free(ppt->msix.arg, M_PPTMSIX);
306cd942e0fSPeter Grehan 
307920bc340SNeel Natu 	pci_release_msi(ppt->dev);
308cd942e0fSPeter Grehan 
309cd942e0fSPeter Grehan 	ppt->msix.num_msgs = 0;
310cd942e0fSPeter Grehan }
311cd942e0fSPeter Grehan 
312366f6083SPeter Grehan int
31351f45d01SNeel Natu ppt_avail_devices(void)
31451f45d01SNeel Natu {
31551f45d01SNeel Natu 
31651f45d01SNeel Natu 	return (num_pptdevs);
31751f45d01SNeel Natu }
31851f45d01SNeel Natu 
31951f45d01SNeel Natu int
32051f45d01SNeel Natu ppt_assigned_devices(struct vm *vm)
321318224bbSNeel Natu {
322a15f820aSRyan Stone 	struct pptdev *ppt;
323a15f820aSRyan Stone 	int num;
324318224bbSNeel Natu 
325318224bbSNeel Natu 	num = 0;
326a15f820aSRyan Stone 	TAILQ_FOREACH(ppt, &pptdev_list, next) {
327a15f820aSRyan Stone 		if (ppt->vm == vm)
328318224bbSNeel Natu 			num++;
329318224bbSNeel Natu 	}
330318224bbSNeel Natu 	return (num);
331318224bbSNeel Natu }
332318224bbSNeel Natu 
333318224bbSNeel Natu boolean_t
334318224bbSNeel Natu ppt_is_mmio(struct vm *vm, vm_paddr_t gpa)
335318224bbSNeel Natu {
336a15f820aSRyan Stone 	int i;
337318224bbSNeel Natu 	struct pptdev *ppt;
3389b1aa8d6SNeel Natu 	struct pptseg *seg;
339318224bbSNeel Natu 
340a15f820aSRyan Stone 	TAILQ_FOREACH(ppt, &pptdev_list, next) {
341318224bbSNeel Natu 		if (ppt->vm != vm)
342318224bbSNeel Natu 			continue;
343318224bbSNeel Natu 
344318224bbSNeel Natu 		for (i = 0; i < MAX_MMIOSEGS; i++) {
345318224bbSNeel Natu 			seg = &ppt->mmio[i];
346318224bbSNeel Natu 			if (seg->len == 0)
347318224bbSNeel Natu 				continue;
348318224bbSNeel Natu 			if (gpa >= seg->gpa && gpa < seg->gpa + seg->len)
349318224bbSNeel Natu 				return (TRUE);
350318224bbSNeel Natu 		}
351318224bbSNeel Natu 	}
352318224bbSNeel Natu 
353318224bbSNeel Natu 	return (FALSE);
354318224bbSNeel Natu }
355318224bbSNeel Natu 
356adb947a6SPeter Grehan static void
357adb947a6SPeter Grehan ppt_pci_reset(device_t dev)
358adb947a6SPeter Grehan {
359adb947a6SPeter Grehan 
360adb947a6SPeter Grehan 	if (pcie_flr(dev,
361*5db2a4a8SKonstantin Belousov 	     max(pcie_get_max_completion_timeout(dev) / 1000, 10), true))
362adb947a6SPeter Grehan 		return;
363adb947a6SPeter Grehan 
364*5db2a4a8SKonstantin Belousov 	pci_power_reset(dev);
365adb947a6SPeter Grehan }
366adb947a6SPeter Grehan 
367318224bbSNeel Natu int
368366f6083SPeter Grehan ppt_assign_device(struct vm *vm, int bus, int slot, int func)
369366f6083SPeter Grehan {
370366f6083SPeter Grehan 	struct pptdev *ppt;
371366f6083SPeter Grehan 
372366f6083SPeter Grehan 	ppt = ppt_find(bus, slot, func);
373366f6083SPeter Grehan 	if (ppt != NULL) {
374366f6083SPeter Grehan 		/*
375366f6083SPeter Grehan 		 * If this device is owned by a different VM then we
376366f6083SPeter Grehan 		 * cannot change its owner.
377366f6083SPeter Grehan 		 */
378366f6083SPeter Grehan 		if (ppt->vm != NULL && ppt->vm != vm)
379366f6083SPeter Grehan 			return (EBUSY);
380366f6083SPeter Grehan 
381da0fc925SJohn Baldwin 		pci_save_state(ppt->dev);
382adb947a6SPeter Grehan 		ppt_pci_reset(ppt->dev);
383da0fc925SJohn Baldwin 		pci_restore_state(ppt->dev);
384366f6083SPeter Grehan 		ppt->vm = vm;
385a8667250SRyan Stone 		iommu_add_device(vm_iommu_domain(vm), pci_get_rid(ppt->dev));
386366f6083SPeter Grehan 		return (0);
387366f6083SPeter Grehan 	}
388366f6083SPeter Grehan 	return (ENOENT);
389366f6083SPeter Grehan }
390366f6083SPeter Grehan 
391366f6083SPeter Grehan int
392366f6083SPeter Grehan ppt_unassign_device(struct vm *vm, int bus, int slot, int func)
393366f6083SPeter Grehan {
394366f6083SPeter Grehan 	struct pptdev *ppt;
395366f6083SPeter Grehan 
396366f6083SPeter Grehan 	ppt = ppt_find(bus, slot, func);
397366f6083SPeter Grehan 	if (ppt != NULL) {
398366f6083SPeter Grehan 		/*
399366f6083SPeter Grehan 		 * If this device is not owned by this 'vm' then bail out.
400366f6083SPeter Grehan 		 */
401366f6083SPeter Grehan 		if (ppt->vm != vm)
402366f6083SPeter Grehan 			return (EBUSY);
403da0fc925SJohn Baldwin 
404da0fc925SJohn Baldwin 		pci_save_state(ppt->dev);
405adb947a6SPeter Grehan 		ppt_pci_reset(ppt->dev);
406da0fc925SJohn Baldwin 		pci_restore_state(ppt->dev);
407366f6083SPeter Grehan 		ppt_unmap_mmio(vm, ppt);
408366f6083SPeter Grehan 		ppt_teardown_msi(ppt);
409cd942e0fSPeter Grehan 		ppt_teardown_msix(ppt);
410a8667250SRyan Stone 		iommu_remove_device(vm_iommu_domain(vm), pci_get_rid(ppt->dev));
411366f6083SPeter Grehan 		ppt->vm = NULL;
412366f6083SPeter Grehan 		return (0);
413366f6083SPeter Grehan 	}
414366f6083SPeter Grehan 	return (ENOENT);
415366f6083SPeter Grehan }
416366f6083SPeter Grehan 
417366f6083SPeter Grehan int
418366f6083SPeter Grehan ppt_unassign_all(struct vm *vm)
419366f6083SPeter Grehan {
420a15f820aSRyan Stone 	struct pptdev *ppt;
421a15f820aSRyan Stone 	int bus, slot, func;
422366f6083SPeter Grehan 	device_t dev;
423366f6083SPeter Grehan 
424a15f820aSRyan Stone 	TAILQ_FOREACH(ppt, &pptdev_list, next) {
425a15f820aSRyan Stone 		if (ppt->vm == vm) {
426a15f820aSRyan Stone 			dev = ppt->dev;
427366f6083SPeter Grehan 			bus = pci_get_bus(dev);
428366f6083SPeter Grehan 			slot = pci_get_slot(dev);
429366f6083SPeter Grehan 			func = pci_get_function(dev);
430318224bbSNeel Natu 			vm_unassign_pptdev(vm, bus, slot, func);
431366f6083SPeter Grehan 		}
432366f6083SPeter Grehan 	}
433366f6083SPeter Grehan 
434366f6083SPeter Grehan 	return (0);
435366f6083SPeter Grehan }
436366f6083SPeter Grehan 
437366f6083SPeter Grehan int
438366f6083SPeter Grehan ppt_map_mmio(struct vm *vm, int bus, int slot, int func,
439366f6083SPeter Grehan 	     vm_paddr_t gpa, size_t len, vm_paddr_t hpa)
440366f6083SPeter Grehan {
441366f6083SPeter Grehan 	int i, error;
4429b1aa8d6SNeel Natu 	struct pptseg *seg;
443366f6083SPeter Grehan 	struct pptdev *ppt;
444366f6083SPeter Grehan 
445366f6083SPeter Grehan 	ppt = ppt_find(bus, slot, func);
446366f6083SPeter Grehan 	if (ppt != NULL) {
447366f6083SPeter Grehan 		if (ppt->vm != vm)
448366f6083SPeter Grehan 			return (EBUSY);
449366f6083SPeter Grehan 
450366f6083SPeter Grehan 		for (i = 0; i < MAX_MMIOSEGS; i++) {
451366f6083SPeter Grehan 			seg = &ppt->mmio[i];
452366f6083SPeter Grehan 			if (seg->len == 0) {
453366f6083SPeter Grehan 				error = vm_map_mmio(vm, gpa, len, hpa);
454366f6083SPeter Grehan 				if (error == 0) {
455366f6083SPeter Grehan 					seg->gpa = gpa;
456366f6083SPeter Grehan 					seg->len = len;
457366f6083SPeter Grehan 				}
458366f6083SPeter Grehan 				return (error);
459366f6083SPeter Grehan 			}
460366f6083SPeter Grehan 		}
461366f6083SPeter Grehan 		return (ENOSPC);
462366f6083SPeter Grehan 	}
463366f6083SPeter Grehan 	return (ENOENT);
464366f6083SPeter Grehan }
465366f6083SPeter Grehan 
466366f6083SPeter Grehan static int
467366f6083SPeter Grehan pptintr(void *arg)
468366f6083SPeter Grehan {
469366f6083SPeter Grehan 	struct pptdev *ppt;
470366f6083SPeter Grehan 	struct pptintr_arg *pptarg;
471366f6083SPeter Grehan 
472366f6083SPeter Grehan 	pptarg = arg;
473366f6083SPeter Grehan 	ppt = pptarg->pptdev;
474366f6083SPeter Grehan 
475366f6083SPeter Grehan 	if (ppt->vm != NULL)
4764f8be175SNeel Natu 		lapic_intr_msi(ppt->vm, pptarg->addr, pptarg->msg_data);
477366f6083SPeter Grehan 	else {
478366f6083SPeter Grehan 		/*
479366f6083SPeter Grehan 		 * XXX
480366f6083SPeter Grehan 		 * This is not expected to happen - panic?
481366f6083SPeter Grehan 		 */
482366f6083SPeter Grehan 	}
483366f6083SPeter Grehan 
484366f6083SPeter Grehan 	/*
485366f6083SPeter Grehan 	 * For legacy interrupts give other filters a chance in case
486366f6083SPeter Grehan 	 * the interrupt was not generated by the passthrough device.
487366f6083SPeter Grehan 	 */
488366f6083SPeter Grehan 	if (ppt->msi.startrid == 0)
489366f6083SPeter Grehan 		return (FILTER_STRAY);
490366f6083SPeter Grehan 	else
491366f6083SPeter Grehan 		return (FILTER_HANDLED);
492366f6083SPeter Grehan }
493366f6083SPeter Grehan 
494366f6083SPeter Grehan int
495366f6083SPeter Grehan ppt_setup_msi(struct vm *vm, int vcpu, int bus, int slot, int func,
4964f8be175SNeel Natu 	      uint64_t addr, uint64_t msg, int numvec)
497366f6083SPeter Grehan {
498366f6083SPeter Grehan 	int i, rid, flags;
499366f6083SPeter Grehan 	int msi_count, startrid, error, tmp;
500366f6083SPeter Grehan 	struct pptdev *ppt;
501366f6083SPeter Grehan 
5024f8be175SNeel Natu 	if (numvec < 0 || numvec > MAX_MSIMSGS)
503366f6083SPeter Grehan 		return (EINVAL);
504366f6083SPeter Grehan 
505366f6083SPeter Grehan 	ppt = ppt_find(bus, slot, func);
506366f6083SPeter Grehan 	if (ppt == NULL)
507366f6083SPeter Grehan 		return (ENOENT);
508366f6083SPeter Grehan 	if (ppt->vm != vm)		/* Make sure we own this device */
509366f6083SPeter Grehan 		return (EBUSY);
510366f6083SPeter Grehan 
511366f6083SPeter Grehan 	/* Free any allocated resources */
512485b3300SNeel Natu 	ppt_teardown_msi(ppt);
513366f6083SPeter Grehan 
514366f6083SPeter Grehan 	if (numvec == 0)		/* nothing more to do */
515366f6083SPeter Grehan 		return (0);
516366f6083SPeter Grehan 
517366f6083SPeter Grehan 	flags = RF_ACTIVE;
518366f6083SPeter Grehan 	msi_count = pci_msi_count(ppt->dev);
519366f6083SPeter Grehan 	if (msi_count == 0) {
520366f6083SPeter Grehan 		startrid = 0;		/* legacy interrupt */
521366f6083SPeter Grehan 		msi_count = 1;
522366f6083SPeter Grehan 		flags |= RF_SHAREABLE;
523366f6083SPeter Grehan 	} else
524366f6083SPeter Grehan 		startrid = 1;		/* MSI */
525366f6083SPeter Grehan 
526366f6083SPeter Grehan 	/*
527366f6083SPeter Grehan 	 * The device must be capable of supporting the number of vectors
528366f6083SPeter Grehan 	 * the guest wants to allocate.
529366f6083SPeter Grehan 	 */
530366f6083SPeter Grehan 	if (numvec > msi_count)
531366f6083SPeter Grehan 		return (EINVAL);
532366f6083SPeter Grehan 
533366f6083SPeter Grehan 	/*
534366f6083SPeter Grehan 	 * Make sure that we can allocate all the MSI vectors that are needed
535366f6083SPeter Grehan 	 * by the guest.
536366f6083SPeter Grehan 	 */
537366f6083SPeter Grehan 	if (startrid == 1) {
538366f6083SPeter Grehan 		tmp = numvec;
539366f6083SPeter Grehan 		error = pci_alloc_msi(ppt->dev, &tmp);
540366f6083SPeter Grehan 		if (error)
541366f6083SPeter Grehan 			return (error);
542366f6083SPeter Grehan 		else if (tmp != numvec) {
543366f6083SPeter Grehan 			pci_release_msi(ppt->dev);
544366f6083SPeter Grehan 			return (ENOSPC);
545366f6083SPeter Grehan 		} else {
546366f6083SPeter Grehan 			/* success */
547366f6083SPeter Grehan 		}
548366f6083SPeter Grehan 	}
549366f6083SPeter Grehan 
550366f6083SPeter Grehan 	ppt->msi.startrid = startrid;
551366f6083SPeter Grehan 
552366f6083SPeter Grehan 	/*
553366f6083SPeter Grehan 	 * Allocate the irq resource and attach it to the interrupt handler.
554366f6083SPeter Grehan 	 */
555366f6083SPeter Grehan 	for (i = 0; i < numvec; i++) {
556366f6083SPeter Grehan 		ppt->msi.num_msgs = i + 1;
557366f6083SPeter Grehan 		ppt->msi.cookie[i] = NULL;
558366f6083SPeter Grehan 
559366f6083SPeter Grehan 		rid = startrid + i;
560366f6083SPeter Grehan 		ppt->msi.res[i] = bus_alloc_resource_any(ppt->dev, SYS_RES_IRQ,
561366f6083SPeter Grehan 							 &rid, flags);
562366f6083SPeter Grehan 		if (ppt->msi.res[i] == NULL)
563366f6083SPeter Grehan 			break;
564366f6083SPeter Grehan 
565366f6083SPeter Grehan 		ppt->msi.arg[i].pptdev = ppt;
5664f8be175SNeel Natu 		ppt->msi.arg[i].addr = addr;
5674f8be175SNeel Natu 		ppt->msi.arg[i].msg_data = msg + i;
568366f6083SPeter Grehan 
569366f6083SPeter Grehan 		error = bus_setup_intr(ppt->dev, ppt->msi.res[i],
57034a6b2d6SJohn Baldwin 				       INTR_TYPE_NET | INTR_MPSAFE,
571366f6083SPeter Grehan 				       pptintr, NULL, &ppt->msi.arg[i],
572366f6083SPeter Grehan 				       &ppt->msi.cookie[i]);
573366f6083SPeter Grehan 		if (error != 0)
574366f6083SPeter Grehan 			break;
575366f6083SPeter Grehan 	}
576366f6083SPeter Grehan 
577366f6083SPeter Grehan 	if (i < numvec) {
578485b3300SNeel Natu 		ppt_teardown_msi(ppt);
579366f6083SPeter Grehan 		return (ENXIO);
580366f6083SPeter Grehan 	}
581366f6083SPeter Grehan 
582366f6083SPeter Grehan 	return (0);
583366f6083SPeter Grehan }
584cd942e0fSPeter Grehan 
585cd942e0fSPeter Grehan int
586cd942e0fSPeter Grehan ppt_setup_msix(struct vm *vm, int vcpu, int bus, int slot, int func,
5874f8be175SNeel Natu 	       int idx, uint64_t addr, uint64_t msg, uint32_t vector_control)
588cd942e0fSPeter Grehan {
589cd942e0fSPeter Grehan 	struct pptdev *ppt;
590cd942e0fSPeter Grehan 	struct pci_devinfo *dinfo;
591920bc340SNeel Natu 	int numvec, alloced, rid, error;
592cd942e0fSPeter Grehan 	size_t res_size, cookie_size, arg_size;
593cd942e0fSPeter Grehan 
594cd942e0fSPeter Grehan 	ppt = ppt_find(bus, slot, func);
595cd942e0fSPeter Grehan 	if (ppt == NULL)
596cd942e0fSPeter Grehan 		return (ENOENT);
597cd942e0fSPeter Grehan 	if (ppt->vm != vm)		/* Make sure we own this device */
598cd942e0fSPeter Grehan 		return (EBUSY);
599cd942e0fSPeter Grehan 
600cd942e0fSPeter Grehan 	dinfo = device_get_ivars(ppt->dev);
601cd942e0fSPeter Grehan 	if (!dinfo)
602cd942e0fSPeter Grehan 		return (ENXIO);
603cd942e0fSPeter Grehan 
604cd942e0fSPeter Grehan 	/*
605cd942e0fSPeter Grehan 	 * First-time configuration:
606cd942e0fSPeter Grehan 	 * 	Allocate the MSI-X table
607cd942e0fSPeter Grehan 	 *	Allocate the IRQ resources
608cd942e0fSPeter Grehan 	 *	Set up some variables in ppt->msix
609cd942e0fSPeter Grehan 	 */
610920bc340SNeel Natu 	if (ppt->msix.num_msgs == 0) {
611920bc340SNeel Natu 		numvec = pci_msix_count(ppt->dev);
612920bc340SNeel Natu 		if (numvec <= 0)
613920bc340SNeel Natu 			return (EINVAL);
614cd942e0fSPeter Grehan 
615cd942e0fSPeter Grehan 		ppt->msix.startrid = 1;
616920bc340SNeel Natu 		ppt->msix.num_msgs = numvec;
617cd942e0fSPeter Grehan 
618cd942e0fSPeter Grehan 		res_size = numvec * sizeof(ppt->msix.res[0]);
619cd942e0fSPeter Grehan 		cookie_size = numvec * sizeof(ppt->msix.cookie[0]);
620cd942e0fSPeter Grehan 		arg_size = numvec * sizeof(ppt->msix.arg[0]);
621cd942e0fSPeter Grehan 
622920bc340SNeel Natu 		ppt->msix.res = malloc(res_size, M_PPTMSIX, M_WAITOK | M_ZERO);
623920bc340SNeel Natu 		ppt->msix.cookie = malloc(cookie_size, M_PPTMSIX,
624920bc340SNeel Natu 					  M_WAITOK | M_ZERO);
625920bc340SNeel Natu 		ppt->msix.arg = malloc(arg_size, M_PPTMSIX, M_WAITOK | M_ZERO);
626920bc340SNeel Natu 
627920bc340SNeel Natu 		rid = dinfo->cfg.msix.msix_table_bar;
628920bc340SNeel Natu 		ppt->msix.msix_table_res = bus_alloc_resource_any(ppt->dev,
629920bc340SNeel Natu 					       SYS_RES_MEMORY, &rid, RF_ACTIVE);
630920bc340SNeel Natu 
631920bc340SNeel Natu 		if (ppt->msix.msix_table_res == NULL) {
632cd942e0fSPeter Grehan 			ppt_teardown_msix(ppt);
633cd942e0fSPeter Grehan 			return (ENOSPC);
634cd942e0fSPeter Grehan 		}
635920bc340SNeel Natu 		ppt->msix.msix_table_rid = rid;
636920bc340SNeel Natu 
637920bc340SNeel Natu 		alloced = numvec;
638920bc340SNeel Natu 		error = pci_alloc_msix(ppt->dev, &alloced);
639920bc340SNeel Natu 		if (error || alloced != numvec) {
640920bc340SNeel Natu 			ppt_teardown_msix(ppt);
641920bc340SNeel Natu 			return (error == 0 ? ENOSPC: error);
642920bc340SNeel Natu 		}
643cd942e0fSPeter Grehan 	}
644cd942e0fSPeter Grehan 
645cd942e0fSPeter Grehan 	if ((vector_control & PCIM_MSIX_VCTRL_MASK) == 0) {
646cd942e0fSPeter Grehan 		/* Tear down the IRQ if it's already set up */
647cd942e0fSPeter Grehan 		ppt_teardown_msix_intr(ppt, idx);
648cd942e0fSPeter Grehan 
649cd942e0fSPeter Grehan 		/* Allocate the IRQ resource */
650cd942e0fSPeter Grehan 		ppt->msix.cookie[idx] = NULL;
651cd942e0fSPeter Grehan 		rid = ppt->msix.startrid + idx;
652cd942e0fSPeter Grehan 		ppt->msix.res[idx] = bus_alloc_resource_any(ppt->dev, SYS_RES_IRQ,
653cd942e0fSPeter Grehan 							    &rid, RF_ACTIVE);
654cd942e0fSPeter Grehan 		if (ppt->msix.res[idx] == NULL)
655cd942e0fSPeter Grehan 			return (ENXIO);
656cd942e0fSPeter Grehan 
657cd942e0fSPeter Grehan 		ppt->msix.arg[idx].pptdev = ppt;
6584f8be175SNeel Natu 		ppt->msix.arg[idx].addr = addr;
6594f8be175SNeel Natu 		ppt->msix.arg[idx].msg_data = msg;
660cd942e0fSPeter Grehan 
661cd942e0fSPeter Grehan 		/* Setup the MSI-X interrupt */
662cd942e0fSPeter Grehan 		error = bus_setup_intr(ppt->dev, ppt->msix.res[idx],
663cd942e0fSPeter Grehan 				       INTR_TYPE_NET | INTR_MPSAFE,
664cd942e0fSPeter Grehan 				       pptintr, NULL, &ppt->msix.arg[idx],
665cd942e0fSPeter Grehan 				       &ppt->msix.cookie[idx]);
666cd942e0fSPeter Grehan 
667cd942e0fSPeter Grehan 		if (error != 0) {
668cd942e0fSPeter Grehan 			bus_teardown_intr(ppt->dev, ppt->msix.res[idx], ppt->msix.cookie[idx]);
669cd942e0fSPeter Grehan 			bus_release_resource(ppt->dev, SYS_RES_IRQ, rid, ppt->msix.res[idx]);
670cd942e0fSPeter Grehan 			ppt->msix.cookie[idx] = NULL;
671cd942e0fSPeter Grehan 			ppt->msix.res[idx] = NULL;
672cd942e0fSPeter Grehan 			return (ENXIO);
673cd942e0fSPeter Grehan 		}
674cd942e0fSPeter Grehan 	} else {
675cd942e0fSPeter Grehan 		/* Masked, tear it down if it's already been set up */
676cd942e0fSPeter Grehan 		ppt_teardown_msix_intr(ppt, idx);
677cd942e0fSPeter Grehan 	}
678cd942e0fSPeter Grehan 
679cd942e0fSPeter Grehan 	return (0);
680cd942e0fSPeter Grehan }
681