xref: /freebsd/sys/amd64/vmm/io/ppt.c (revision c8343ba84af75c3710a1c587ff9bd2165fcd37a9)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2011 NetApp, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/bus.h>
32 #include <sys/kernel.h>
33 #include <sys/lock.h>
34 #include <sys/malloc.h>
35 #include <sys/module.h>
36 #include <sys/pciio.h>
37 #include <sys/rman.h>
38 #include <sys/smp.h>
39 #include <sys/sx.h>
40 #include <sys/sysctl.h>
41 
42 #include <dev/pci/pcivar.h>
43 #include <dev/pci/pcireg.h>
44 
45 #include <machine/resource.h>
46 #include <machine/vmm.h>
47 #include <machine/vmm_dev.h>
48 
49 #include <dev/vmm/vmm_ktr.h>
50 
51 #include "vmm_lapic.h"
52 
53 #include "iommu.h"
54 #include "ppt.h"
55 
56 /* XXX locking */
57 
58 #define	MAX_MSIMSGS	32
59 
60 /*
61  * If the MSI-X table is located in the middle of a BAR then that MMIO
62  * region gets split into two segments - one segment above the MSI-X table
63  * and the other segment below the MSI-X table - with a hole in place of
64  * the MSI-X table so accesses to it can be trapped and emulated.
65  *
66  * So, allocate a MMIO segment for each BAR register + 1 additional segment.
67  */
68 #define	MAX_MMIOSEGS	((PCIR_MAX_BAR_0 + 1) + 1)
69 
70 MALLOC_DEFINE(M_PPTMSIX, "pptmsix", "Passthru MSI-X resources");
71 
72 static struct sx ppt_mtx;
73 SX_SYSINIT(ppt_mtx, &ppt_mtx, "ppt_mtx");
74 #define	PPT_LOCK()		sx_xlock(&ppt_mtx)
75 #define	PPT_UNLOCK()		sx_xunlock(&ppt_mtx)
76 #define	PPT_ASSERT_LOCKED()	sx_assert(&ppt_mtx, SA_XLOCKED)
77 
78 struct pptintr_arg {				/* pptintr(pptintr_arg) */
79 	struct pptdev	*pptdev;
80 	uint64_t	addr;
81 	uint64_t	msg_data;
82 };
83 
84 struct pptseg {
85 	vm_paddr_t	gpa;
86 	size_t		len;
87 	int		wired;
88 };
89 
90 struct pptdev {
91 	device_t	dev;
92 	struct vm	*vm;			/* owner of this device */
93 	bool		resetting;		/* guest FLR in progress */
94 	TAILQ_ENTRY(pptdev)	next;
95 	struct pptseg mmio[MAX_MMIOSEGS];
96 	struct {
97 		int	num_msgs;		/* guest state */
98 
99 		int	startrid;		/* host state */
100 		struct resource *res[MAX_MSIMSGS];
101 		void	*cookie[MAX_MSIMSGS];
102 		struct pptintr_arg arg[MAX_MSIMSGS];
103 	} msi;
104 
105 	struct {
106 		int num_msgs;
107 		int startrid;
108 		int msix_table_rid;
109 		int msix_pba_rid;
110 		struct resource *msix_table_res;
111 		struct resource *msix_pba_res;
112 		struct resource **res;
113 		void **cookie;
114 		struct pptintr_arg *arg;
115 	} msix;
116 };
117 
118 SYSCTL_DECL(_hw_vmm);
119 SYSCTL_NODE(_hw_vmm, OID_AUTO, ppt, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
120     "bhyve passthru devices");
121 
122 static int num_pptdevs;
123 SYSCTL_INT(_hw_vmm_ppt, OID_AUTO, devices, CTLFLAG_RD, &num_pptdevs, 0,
124     "number of pci passthru devices");
125 
126 static TAILQ_HEAD(, pptdev) pptdev_list = TAILQ_HEAD_INITIALIZER(pptdev_list);
127 
128 static int
ppt_probe(device_t dev)129 ppt_probe(device_t dev)
130 {
131 	int bus, slot, func;
132 	struct pci_devinfo *dinfo;
133 
134 	dinfo = (struct pci_devinfo *)device_get_ivars(dev);
135 
136 	bus = pci_get_bus(dev);
137 	slot = pci_get_slot(dev);
138 	func = pci_get_function(dev);
139 
140 	/*
141 	 * To qualify as a pci passthrough device a device must:
142 	 * - be allowed by administrator to be used in this role
143 	 * - be an endpoint device
144 	 */
145 	if ((dinfo->cfg.hdrtype & PCIM_HDRTYPE) != PCIM_HDRTYPE_NORMAL)
146 		return (ENXIO);
147 	else if (vmm_is_pptdev(bus, slot, func))
148 		return (0);
149 	else
150 		/*
151 		 * Returning BUS_PROBE_NOWILDCARD here matches devices that the
152 		 * SR-IOV infrastructure specified as "ppt" passthrough devices.
153 		 * All normal devices that did not have "ppt" specified as their
154 		 * driver will not be matched by this.
155 		 */
156 		return (BUS_PROBE_NOWILDCARD);
157 }
158 
159 static int
ppt_attach(device_t dev)160 ppt_attach(device_t dev)
161 {
162 	struct pptdev *ppt;
163 	uint16_t cmd, cmd1;
164 	int error;
165 
166 	ppt = device_get_softc(dev);
167 
168 	PPT_LOCK();
169 	cmd1 = cmd = pci_read_config(dev, PCIR_COMMAND, 2);
170 	cmd &= ~(PCIM_CMD_PORTEN | PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN);
171 	pci_write_config(dev, PCIR_COMMAND, cmd, 2);
172 	error = iommu_remove_device(iommu_host_domain(), dev, pci_get_rid(dev));
173 	if (error != 0) {
174 		pci_write_config(dev, PCIR_COMMAND, cmd1, 2);
175 		PPT_UNLOCK();
176 		return (error);
177 	}
178 	num_pptdevs++;
179 	TAILQ_INSERT_TAIL(&pptdev_list, ppt, next);
180 	ppt->dev = dev;
181 	PPT_UNLOCK();
182 
183 	if (bootverbose)
184 		device_printf(dev, "attached\n");
185 
186 	return (0);
187 }
188 
189 static int
ppt_detach(device_t dev)190 ppt_detach(device_t dev)
191 {
192 	struct pptdev *ppt;
193 	int error;
194 
195 	error = 0;
196 	ppt = device_get_softc(dev);
197 
198 	PPT_LOCK();
199 	if (ppt->vm != NULL) {
200 		error = EBUSY;
201 		goto out;
202 	}
203 	if (iommu_host_domain() != NULL) {
204 		error = iommu_add_device(iommu_host_domain(), dev,
205 		    pci_get_rid(dev));
206 		if (error != 0)
207 			goto out;
208 	}
209 	num_pptdevs--;
210 	TAILQ_REMOVE(&pptdev_list, ppt, next);
211 out:
212 	PPT_UNLOCK();
213 
214 	return (error);
215 }
216 
217 static device_method_t ppt_methods[] = {
218 	/* Device interface */
219 	DEVMETHOD(device_probe,		ppt_probe),
220 	DEVMETHOD(device_attach,	ppt_attach),
221 	DEVMETHOD(device_detach,	ppt_detach),
222 	{0, 0}
223 };
224 
225 DEFINE_CLASS_0(ppt, ppt_driver, ppt_methods, sizeof(struct pptdev));
226 DRIVER_MODULE(ppt, pci, ppt_driver, NULL, NULL);
227 
228 static int
ppt_find(struct vm * vm,int bus,int slot,int func,struct pptdev ** pptp)229 ppt_find(struct vm *vm, int bus, int slot, int func, struct pptdev **pptp)
230 {
231 	device_t dev;
232 	struct pptdev *ppt;
233 	int b, s, f;
234 
235 	PPT_ASSERT_LOCKED();
236 
237 	for (;;) {
238 		TAILQ_FOREACH(ppt, &pptdev_list, next) {
239 			dev = ppt->dev;
240 			b = pci_get_bus(dev);
241 			s = pci_get_slot(dev);
242 			f = pci_get_function(dev);
243 			if (bus == b && slot == s && func == f)
244 				break;
245 		}
246 
247 		if (ppt == NULL)
248 			return (ENOENT);
249 		if (ppt->vm != vm)	/* Make sure we own this device. */
250 			return (EBUSY);
251 		if (!ppt->resetting)
252 			break;
253 		/*
254 		 * Once resetting is set, every exit from ppt_reset_device()
255 		 * reacquires ppt_mtx, clears resetting, and wakes us.  The FLR wait
256 		 * itself is bounded.
257 		 */
258 		sx_sleep(ppt, &ppt_mtx, 0, "pptflr", 0);
259 	}
260 
261 	*pptp = ppt;
262 	return (0);
263 }
264 
265 static void
ppt_unmap_all_mmio(struct vm * vm,struct pptdev * ppt)266 ppt_unmap_all_mmio(struct vm *vm, struct pptdev *ppt)
267 {
268 	int i;
269 	struct pptseg *seg;
270 
271 	for (i = 0; i < MAX_MMIOSEGS; i++) {
272 		seg = &ppt->mmio[i];
273 		if (seg->len == 0)
274 			continue;
275 		(void)vm_unmap_mmio(vm, seg->gpa, seg->len);
276 		bzero(seg, sizeof(struct pptseg));
277 	}
278 }
279 
280 static void
ppt_teardown_msi(struct pptdev * ppt)281 ppt_teardown_msi(struct pptdev *ppt)
282 {
283 	int i, rid;
284 	void *cookie;
285 	struct resource *res;
286 
287 	if (ppt->msi.num_msgs == 0)
288 		return;
289 
290 	for (i = 0; i < ppt->msi.num_msgs; i++) {
291 		rid = ppt->msi.startrid + i;
292 		res = ppt->msi.res[i];
293 		cookie = ppt->msi.cookie[i];
294 
295 		if (cookie != NULL)
296 			bus_teardown_intr(ppt->dev, res, cookie);
297 
298 		if (res != NULL)
299 			bus_release_resource(ppt->dev, SYS_RES_IRQ, rid, res);
300 
301 		ppt->msi.res[i] = NULL;
302 		ppt->msi.cookie[i] = NULL;
303 	}
304 
305 	if (ppt->msi.startrid == 1)
306 		pci_release_msi(ppt->dev);
307 
308 	ppt->msi.num_msgs = 0;
309 }
310 
311 static void
ppt_teardown_msix_intr(struct pptdev * ppt,int idx)312 ppt_teardown_msix_intr(struct pptdev *ppt, int idx)
313 {
314 	int rid;
315 	struct resource *res;
316 	void *cookie;
317 
318 	rid = ppt->msix.startrid + idx;
319 	res = ppt->msix.res[idx];
320 	cookie = ppt->msix.cookie[idx];
321 
322 	if (cookie != NULL)
323 		bus_teardown_intr(ppt->dev, res, cookie);
324 
325 	if (res != NULL)
326 		bus_release_resource(ppt->dev, SYS_RES_IRQ, rid, res);
327 
328 	ppt->msix.res[idx] = NULL;
329 	ppt->msix.cookie[idx] = NULL;
330 }
331 
332 static void
ppt_teardown_msix(struct pptdev * ppt)333 ppt_teardown_msix(struct pptdev *ppt)
334 {
335 	int i;
336 
337 	if (ppt->msix.num_msgs == 0)
338 		return;
339 
340 	for (i = 0; i < ppt->msix.num_msgs; i++)
341 		ppt_teardown_msix_intr(ppt, i);
342 
343 	free(ppt->msix.res, M_PPTMSIX);
344 	free(ppt->msix.cookie, M_PPTMSIX);
345 	free(ppt->msix.arg, M_PPTMSIX);
346 
347 	pci_release_msi(ppt->dev);
348 
349 	if (ppt->msix.msix_table_res) {
350 		bus_release_resource(ppt->dev, SYS_RES_MEMORY,
351 				     ppt->msix.msix_table_rid,
352 				     ppt->msix.msix_table_res);
353 		ppt->msix.msix_table_res = NULL;
354 		ppt->msix.msix_table_rid = 0;
355 	}
356 	if (ppt->msix.msix_pba_res) {
357 		bus_release_resource(ppt->dev, SYS_RES_MEMORY,
358 				     ppt->msix.msix_pba_rid,
359 				     ppt->msix.msix_pba_res);
360 		ppt->msix.msix_pba_res = NULL;
361 		ppt->msix.msix_pba_rid = 0;
362 	}
363 
364 	ppt->msix.num_msgs = 0;
365 }
366 
367 int
ppt_assigned_devices(struct vm * vm)368 ppt_assigned_devices(struct vm *vm)
369 {
370 	struct pptdev *ppt;
371 	int num;
372 
373 	num = 0;
374 	TAILQ_FOREACH(ppt, &pptdev_list, next) {
375 		if (ppt->vm == vm)
376 			num++;
377 	}
378 	return (num);
379 }
380 
381 bool
ppt_is_mmio(struct vm * vm,vm_paddr_t gpa)382 ppt_is_mmio(struct vm *vm, vm_paddr_t gpa)
383 {
384 	int i;
385 	struct pptdev *ppt;
386 	struct pptseg *seg;
387 
388 	TAILQ_FOREACH(ppt, &pptdev_list, next) {
389 		if (ppt->vm != vm)
390 			continue;
391 
392 		for (i = 0; i < MAX_MMIOSEGS; i++) {
393 			seg = &ppt->mmio[i];
394 			if (seg->len == 0)
395 				continue;
396 			if (gpa >= seg->gpa && gpa < seg->gpa + seg->len)
397 				return (true);
398 		}
399 	}
400 
401 	return (false);
402 }
403 
404 static void
ppt_pci_reset(device_t dev)405 ppt_pci_reset(device_t dev)
406 {
407 
408 	if (pcie_flr(dev,
409 	     max(pcie_get_max_completion_timeout(dev) / 1000, 10), true))
410 		return;
411 
412 	pci_power_reset(dev);
413 }
414 
415 static uint16_t
ppt_bar_enables(struct pptdev * ppt)416 ppt_bar_enables(struct pptdev *ppt)
417 {
418 	struct pci_map *pm;
419 	uint16_t cmd;
420 
421 	cmd = 0;
422 	for (pm = pci_first_bar(ppt->dev); pm != NULL; pm = pci_next_bar(pm)) {
423 		if (PCI_BAR_IO(pm->pm_value))
424 			cmd |= PCIM_CMD_PORTEN;
425 		if (PCI_BAR_MEM(pm->pm_value))
426 			cmd |= PCIM_CMD_MEMEN;
427 	}
428 	return (cmd);
429 }
430 
431 int
ppt_assign_device(struct vm * vm,int bus,int slot,int func)432 ppt_assign_device(struct vm *vm, int bus, int slot, int func)
433 {
434 	struct pptdev *ppt;
435 	int error;
436 	uint16_t cmd;
437 
438 	PPT_LOCK();
439 	/* Passing NULL requires the device to be unowned. */
440 	error = ppt_find(NULL, bus, slot, func, &ppt);
441 	if (error != 0)
442 		goto out;
443 
444 	pci_save_state(ppt->dev);
445 	ppt_pci_reset(ppt->dev);
446 	pci_restore_state(ppt->dev);
447 	error = iommu_add_device(vm_iommu_domain(vm), ppt->dev,
448 	    pci_get_rid(ppt->dev));
449 	if (error != 0)
450 		goto out;
451 	ppt->vm = vm;
452 	cmd = pci_read_config(ppt->dev, PCIR_COMMAND, 2);
453 	cmd |= PCIM_CMD_BUSMASTEREN | ppt_bar_enables(ppt);
454 	pci_write_config(ppt->dev, PCIR_COMMAND, cmd, 2);
455 out:
456 	PPT_UNLOCK();
457 	return (error);
458 }
459 
460 int
ppt_unassign_device(struct vm * vm,int bus,int slot,int func)461 ppt_unassign_device(struct vm *vm, int bus, int slot, int func)
462 {
463 	struct pptdev *ppt;
464 	int error;
465 	uint16_t cmd;
466 
467 	PPT_LOCK();
468 	error = ppt_find(vm, bus, slot, func, &ppt);
469 	if (error != 0)
470 		goto out;
471 
472 	cmd = pci_read_config(ppt->dev, PCIR_COMMAND, 2);
473 	cmd &= ~(PCIM_CMD_PORTEN | PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN);
474 	pci_write_config(ppt->dev, PCIR_COMMAND, cmd, 2);
475 	pci_save_state(ppt->dev);
476 	ppt_pci_reset(ppt->dev);
477 	pci_restore_state(ppt->dev);
478 	ppt_unmap_all_mmio(vm, ppt);
479 	ppt_teardown_msi(ppt);
480 	ppt_teardown_msix(ppt);
481 	error = iommu_remove_device(vm_iommu_domain(vm), ppt->dev,
482 	    pci_get_rid(ppt->dev));
483 	ppt->vm = NULL;
484 out:
485 	PPT_UNLOCK();
486 	return (error);
487 }
488 
489 int
ppt_reset_device(struct vm * vm,int bus,int slot,int func)490 ppt_reset_device(struct vm *vm, int bus, int slot, int func)
491 {
492 	struct pptdev *ppt;
493 	uint16_t cmd, enables, original_cmd;
494 	int error;
495 
496 	PPT_LOCK();
497 	error = ppt_find(vm, bus, slot, func, &ppt);
498 	if (error != 0)
499 		goto out_locked;
500 
501 	/*
502 	 * FLR takes at least 100 ms.  Reserve this function, but do not let a
503 	 * guest hold the global PPT lock and delay operations on other VMs.
504 	 */
505 	ppt->resetting = true;
506 	PPT_UNLOCK();
507 
508 	original_cmd = pci_read_config(ppt->dev, PCIR_COMMAND, 2);
509 	if (original_cmd == 0xffff) {
510 		error = ENXIO;
511 		goto out;
512 	}
513 	if (!pcie_flr_supported(ppt->dev)) {
514 		error = ENOTSUP;
515 		goto out;
516 	}
517 
518 	/*
519 	 * Disable physical INTx before releasing its handler.  This also makes
520 	 * an asserted Function send Deassert_INTx before FLR, as required by
521 	 * PCIe.  Gate decoding and DMA before tearing down MSI or MSI-X.
522 	 */
523 	cmd = original_cmd | PCIM_CMD_INTxDIS;
524 	cmd &= ~(PCIM_CMD_PORTEN | PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN);
525 	pci_write_config(ppt->dev, PCIR_COMMAND, cmd, 2);
526 	ppt_teardown_msi(ppt);
527 	ppt_teardown_msix(ppt);
528 
529 	/*
530 	 * Save the host-owned state after interrupt teardown, then restore BARs
531 	 * and PCIe controls after FLR.  The IOMMU domain remains intact.  bhyve
532 	 * removes guest BAR mappings before this ioctl; a later guest MEMEN write
533 	 * recreates them.
534 	 */
535 	pci_save_state(ppt->dev);
536 
537 	/*
538 	 * A guest-requested FLR must not be escalated to a power reset.  The
539 	 * support check above and force=true mean that pcie_flr() cannot fail
540 	 * for a stable function.  If its support nevertheless disappears
541 	 * between the two checks, destructive preparation has already torn down
542 	 * host interrupt resources.  Return EIO so bhyve discards the now-stale
543 	 * guest interrupt state even though the FLR was not initiated.
544 	 */
545 	if (!pcie_flr(ppt->dev,
546 	    max(pcie_get_max_completion_timeout(ppt->dev) / 1000, 10), true)) {
547 		device_printf(ppt->dev, "guest FLR could not be performed\n");
548 		error = EIO;
549 		goto restore;
550 	}
551 	error = 0;
552 
553 	/*
554 	 * Restore the decode and DMA enables which were set before the FLR;
555 	 * the guest command register is intentionally virtual.  Do not infer
556 	 * writable enables from the BAR resources here.  For example, a VF can
557 	 * use PF-owned BAR apertures while its own MEMEN bit is RsvdP.
558 	 *
559 	 * A post-reset readback failure does not undo the guest-visible reset.
560 	 */
561 restore:
562 	pci_restore_state(ppt->dev);
563 	enables = original_cmd &
564 	    (PCIM_CMD_PORTEN | PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN);
565 	cmd = pci_read_config(ppt->dev, PCIR_COMMAND, 2);
566 	if (cmd == 0xffff) {
567 		device_printf(ppt->dev,
568 		    "config space unavailable after guest FLR\n");
569 		error = EIO;
570 		goto out;
571 	}
572 	cmd &= ~(PCIM_CMD_PORTEN | PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN |
573 	    PCIM_CMD_INTxDIS);
574 	cmd |= enables | (original_cmd & PCIM_CMD_INTxDIS);
575 	pci_write_config(ppt->dev, PCIR_COMMAND, cmd, 2);
576 	cmd = pci_read_config(ppt->dev, PCIR_COMMAND, 2);
577 	if (cmd == 0xffff || (cmd & enables) != enables) {
578 		device_printf(ppt->dev,
579 		    "failed to restore command register after guest FLR\n");
580 		error = EIO;
581 	}
582 out:
583 	PPT_LOCK();
584 	ppt->resetting = false;
585 	wakeup(ppt);
586 out_locked:
587 	PPT_UNLOCK();
588 	return (error);
589 }
590 
591 int
ppt_unassign_all(struct vm * vm)592 ppt_unassign_all(struct vm *vm)
593 {
594 	struct pptdev *ppt;
595 	int bus, slot, func;
596 	device_t dev;
597 
598 	TAILQ_FOREACH(ppt, &pptdev_list, next) {
599 		if (ppt->vm == vm) {
600 			dev = ppt->dev;
601 			bus = pci_get_bus(dev);
602 			slot = pci_get_slot(dev);
603 			func = pci_get_function(dev);
604 			vm_unassign_pptdev(vm, bus, slot, func);
605 		}
606 	}
607 
608 	return (0);
609 }
610 
611 static bool
ppt_valid_bar_mapping(struct pptdev * ppt,vm_paddr_t hpa,size_t len)612 ppt_valid_bar_mapping(struct pptdev *ppt, vm_paddr_t hpa, size_t len)
613 {
614 	struct pci_map *pm;
615 	pci_addr_t base, size;
616 
617 	for (pm = pci_first_bar(ppt->dev); pm != NULL; pm = pci_next_bar(pm)) {
618 		if (!PCI_BAR_MEM(pm->pm_value))
619 			continue;
620 		base = pm->pm_value & PCIM_BAR_MEM_BASE;
621 		size = (pci_addr_t)1 << pm->pm_size;
622 		if (hpa >= base && hpa + len <= base + size)
623 			return (true);
624 	}
625 	return (false);
626 }
627 
628 int
ppt_map_mmio(struct vm * vm,int bus,int slot,int func,vm_paddr_t gpa,size_t len,vm_paddr_t hpa)629 ppt_map_mmio(struct vm *vm, int bus, int slot, int func,
630 	     vm_paddr_t gpa, size_t len, vm_paddr_t hpa)
631 {
632 	int i, error;
633 	struct pptseg *seg;
634 	struct pptdev *ppt;
635 
636 	if (len % PAGE_SIZE != 0 || len == 0 || gpa % PAGE_SIZE != 0 ||
637 	    hpa % PAGE_SIZE != 0 || gpa + len < gpa || hpa + len < hpa)
638 		return (EINVAL);
639 
640 	PPT_LOCK();
641 	error = ppt_find(vm, bus, slot, func, &ppt);
642 	if (error)
643 		goto out;
644 
645 	if (!ppt_valid_bar_mapping(ppt, hpa, len)) {
646 		error = EINVAL;
647 		goto out;
648 	}
649 
650 	error = ENOSPC;
651 	for (i = 0; i < MAX_MMIOSEGS; i++) {
652 		seg = &ppt->mmio[i];
653 		if (seg->len == 0) {
654 			error = vm_map_mmio(vm, gpa, len, hpa);
655 			if (error == 0) {
656 				seg->gpa = gpa;
657 				seg->len = len;
658 			}
659 			break;
660 		}
661 	}
662 out:
663 	PPT_UNLOCK();
664 	return (error);
665 }
666 
667 int
ppt_unmap_mmio(struct vm * vm,int bus,int slot,int func,vm_paddr_t gpa,size_t len)668 ppt_unmap_mmio(struct vm *vm, int bus, int slot, int func,
669 	       vm_paddr_t gpa, size_t len)
670 {
671 	int i, error;
672 	struct pptseg *seg;
673 	struct pptdev *ppt;
674 
675 	PPT_LOCK();
676 	error = ppt_find(vm, bus, slot, func, &ppt);
677 	if (error)
678 		goto out;
679 
680 	error = ENOENT;
681 	for (i = 0; i < MAX_MMIOSEGS; i++) {
682 		seg = &ppt->mmio[i];
683 		if (seg->gpa == gpa && seg->len == len) {
684 			error = vm_unmap_mmio(vm, seg->gpa, seg->len);
685 			if (error == 0) {
686 				seg->gpa = 0;
687 				seg->len = 0;
688 			}
689 			break;
690 		}
691 	}
692 out:
693 	PPT_UNLOCK();
694 	return (error);
695 }
696 
697 static int
pptintr(void * arg)698 pptintr(void *arg)
699 {
700 	struct pptdev *ppt;
701 	struct pptintr_arg *pptarg;
702 
703 	pptarg = arg;
704 	ppt = pptarg->pptdev;
705 
706 	if (ppt->vm != NULL)
707 		lapic_intr_msi(ppt->vm, pptarg->addr, pptarg->msg_data);
708 	else {
709 		/*
710 		 * XXX
711 		 * This is not expected to happen - panic?
712 		 */
713 	}
714 
715 	/*
716 	 * For legacy interrupts give other filters a chance in case
717 	 * the interrupt was not generated by the passthrough device.
718 	 */
719 	if (ppt->msi.startrid == 0)
720 		return (FILTER_STRAY);
721 	else
722 		return (FILTER_HANDLED);
723 }
724 
725 int
ppt_setup_msi(struct vm * vm,int bus,int slot,int func,uint64_t addr,uint64_t msg,int numvec)726 ppt_setup_msi(struct vm *vm, int bus, int slot, int func,
727 	      uint64_t addr, uint64_t msg, int numvec)
728 {
729 	int i, rid, flags;
730 	int msi_count, startrid, error, tmp;
731 	struct pptdev *ppt;
732 
733 	if (numvec < 0 || numvec > MAX_MSIMSGS)
734 		return (EINVAL);
735 
736 	PPT_LOCK();
737 	error = ppt_find(vm, bus, slot, func, &ppt);
738 	if (error)
739 		goto out;
740 
741 	/* Reject attempts to enable MSI while MSI-X is active. */
742 	if (ppt->msix.num_msgs != 0 && numvec != 0) {
743 		error = EBUSY;
744 		goto out;
745 	}
746 
747 	/* Free any allocated resources */
748 	ppt_teardown_msi(ppt);
749 
750 	if (numvec == 0)		/* nothing more to do */
751 		goto out;
752 
753 	flags = RF_ACTIVE;
754 	msi_count = pci_msi_count(ppt->dev);
755 	if (msi_count == 0) {
756 		startrid = 0;		/* legacy interrupt */
757 		msi_count = 1;
758 		flags |= RF_SHAREABLE;
759 	} else
760 		startrid = 1;		/* MSI */
761 
762 	/*
763 	 * The device must be capable of supporting the number of vectors
764 	 * the guest wants to allocate.
765 	 */
766 	if (numvec > msi_count) {
767 		error = EINVAL;
768 		goto out;
769 	}
770 
771 	/*
772 	 * Make sure that we can allocate all the MSI vectors that are needed
773 	 * by the guest.
774 	 */
775 	if (startrid == 1) {
776 		tmp = numvec;
777 		error = pci_alloc_msi(ppt->dev, &tmp);
778 		if (error)
779 			goto out;
780 		else if (tmp != numvec) {
781 			pci_release_msi(ppt->dev);
782 			error = ENOSPC;
783 			goto out;
784 		} else {
785 			/* success */
786 		}
787 	}
788 
789 	ppt->msi.startrid = startrid;
790 
791 	/*
792 	 * Allocate the irq resource and attach it to the interrupt handler.
793 	 */
794 	for (i = 0; i < numvec; i++) {
795 		ppt->msi.num_msgs = i + 1;
796 		ppt->msi.cookie[i] = NULL;
797 
798 		rid = startrid + i;
799 		ppt->msi.res[i] = bus_alloc_resource_any(ppt->dev, SYS_RES_IRQ,
800 							 &rid, flags);
801 		if (ppt->msi.res[i] == NULL)
802 			break;
803 
804 		ppt->msi.arg[i].pptdev = ppt;
805 		ppt->msi.arg[i].addr = addr;
806 		ppt->msi.arg[i].msg_data = msg + i;
807 
808 		error = bus_setup_intr(ppt->dev, ppt->msi.res[i],
809 				       INTR_TYPE_NET | INTR_MPSAFE,
810 				       pptintr, NULL, &ppt->msi.arg[i],
811 				       &ppt->msi.cookie[i]);
812 		if (error != 0)
813 			break;
814 	}
815 
816 	if (i < numvec) {
817 		ppt_teardown_msi(ppt);
818 		error = ENXIO;
819 	}
820 
821 out:
822 	PPT_UNLOCK();
823 	return (error);
824 }
825 
826 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)827 ppt_setup_msix(struct vm *vm, int bus, int slot, int func,
828 	       int idx, uint64_t addr, uint64_t msg, uint32_t vector_control)
829 {
830 	struct pptdev *ppt;
831 	struct pci_devinfo *dinfo;
832 	int numvec, alloced, rid, error;
833 	size_t res_size, cookie_size, arg_size;
834 
835 	PPT_LOCK();
836 	error = ppt_find(vm, bus, slot, func, &ppt);
837 	if (error)
838 		goto out;
839 
840 	/* Reject attempts to enable MSI-X while MSI is active. */
841 	if (ppt->msi.num_msgs != 0) {
842 		error = EBUSY;
843 		goto out;
844 	}
845 
846 	dinfo = device_get_ivars(ppt->dev);
847 	if (dinfo == NULL) {
848 		error = ENXIO;
849 		goto out;
850 	}
851 
852 	/*
853 	 * First-time configuration:
854 	 * 	Allocate the MSI-X table
855 	 *	Allocate the IRQ resources
856 	 *	Set up some variables in ppt->msix
857 	 */
858 	if (ppt->msix.num_msgs == 0) {
859 		numvec = pci_msix_count(ppt->dev);
860 		if (numvec <= 0) {
861 			error = EINVAL;
862 			goto out;
863 		}
864 
865 		ppt->msix.startrid = 1;
866 		ppt->msix.num_msgs = numvec;
867 
868 		res_size = numvec * sizeof(ppt->msix.res[0]);
869 		cookie_size = numvec * sizeof(ppt->msix.cookie[0]);
870 		arg_size = numvec * sizeof(ppt->msix.arg[0]);
871 
872 		ppt->msix.res = malloc(res_size, M_PPTMSIX, M_WAITOK | M_ZERO);
873 		ppt->msix.cookie = malloc(cookie_size, M_PPTMSIX,
874 					  M_WAITOK | M_ZERO);
875 		ppt->msix.arg = malloc(arg_size, M_PPTMSIX, M_WAITOK | M_ZERO);
876 
877 		rid = dinfo->cfg.msix.msix_table_bar;
878 		ppt->msix.msix_table_res = bus_alloc_resource_any(ppt->dev,
879 					       SYS_RES_MEMORY, &rid, RF_ACTIVE);
880 
881 		if (ppt->msix.msix_table_res == NULL) {
882 			ppt_teardown_msix(ppt);
883 			error = ENOSPC;
884 			goto out;
885 		}
886 		ppt->msix.msix_table_rid = rid;
887 
888 		if (dinfo->cfg.msix.msix_table_bar !=
889 		    dinfo->cfg.msix.msix_pba_bar) {
890 			rid = dinfo->cfg.msix.msix_pba_bar;
891 			ppt->msix.msix_pba_res = bus_alloc_resource_any(
892 			    ppt->dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
893 
894 			if (ppt->msix.msix_pba_res == NULL) {
895 				ppt_teardown_msix(ppt);
896 				error = ENOSPC;
897 				goto out;
898 			}
899 			ppt->msix.msix_pba_rid = rid;
900 		}
901 
902 		alloced = numvec;
903 		error = pci_alloc_msix(ppt->dev, &alloced);
904 		if (error || alloced != numvec) {
905 			ppt_teardown_msix(ppt);
906 			if (error == 0)
907 				error = ENOSPC;
908 			goto out;
909 		}
910 	}
911 
912 	if (idx >= ppt->msix.num_msgs) {
913 		error = EINVAL;
914 		goto out;
915 	}
916 
917 	if ((vector_control & PCIM_MSIX_VCTRL_MASK) == 0) {
918 		/* Tear down the IRQ if it's already set up */
919 		ppt_teardown_msix_intr(ppt, idx);
920 
921 		/* Allocate the IRQ resource */
922 		ppt->msix.cookie[idx] = NULL;
923 		rid = ppt->msix.startrid + idx;
924 		ppt->msix.res[idx] = bus_alloc_resource_any(ppt->dev, SYS_RES_IRQ,
925 							    &rid, RF_ACTIVE);
926 		if (ppt->msix.res[idx] == NULL) {
927 			error = ENXIO;
928 			goto out;
929 		}
930 
931 		ppt->msix.arg[idx].pptdev = ppt;
932 		ppt->msix.arg[idx].addr = addr;
933 		ppt->msix.arg[idx].msg_data = msg;
934 
935 		/* Setup the MSI-X interrupt */
936 		error = bus_setup_intr(ppt->dev, ppt->msix.res[idx],
937 				       INTR_TYPE_NET | INTR_MPSAFE,
938 				       pptintr, NULL, &ppt->msix.arg[idx],
939 				       &ppt->msix.cookie[idx]);
940 		if (error != 0) {
941 			bus_release_resource(ppt->dev, SYS_RES_IRQ, rid, ppt->msix.res[idx]);
942 			ppt->msix.cookie[idx] = NULL;
943 			ppt->msix.res[idx] = NULL;
944 			error = ENXIO;
945 			goto out;
946 		}
947 	} else {
948 		/* Masked, tear it down if it's already been set up */
949 		ppt_teardown_msix_intr(ppt, idx);
950 	}
951 out:
952 	PPT_UNLOCK();
953 	return (error);
954 }
955 
956 int
ppt_disable_msix(struct vm * vm,int bus,int slot,int func)957 ppt_disable_msix(struct vm *vm, int bus, int slot, int func)
958 {
959 	struct pptdev *ppt;
960 	int error;
961 
962 	PPT_LOCK();
963 	error = ppt_find(vm, bus, slot, func, &ppt);
964 	if (error != 0) {
965 		PPT_UNLOCK();
966 		return (error);
967 	}
968 	ppt_teardown_msix(ppt);
969 	PPT_UNLOCK();
970 	return (0);
971 }
972