xref: /freebsd/sys/amd64/vmm/io/ppt.c (revision 3416500aef140042c64bc149cb1ec6620483bc44)
1 /*-
2  * Copyright (c) 2011 NetApp, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/bus.h>
38 #include <sys/pciio.h>
39 #include <sys/rman.h>
40 #include <sys/smp.h>
41 #include <sys/sysctl.h>
42 
43 #include <dev/pci/pcivar.h>
44 #include <dev/pci/pcireg.h>
45 
46 #include <machine/resource.h>
47 
48 #include <machine/vmm.h>
49 #include <machine/vmm_dev.h>
50 
51 #include "vmm_lapic.h"
52 #include "vmm_ktr.h"
53 
54 #include "iommu.h"
55 #include "ppt.h"
56 
57 /* XXX locking */
58 
59 #define	MAX_MSIMSGS	32
60 
61 /*
62  * If the MSI-X table is located in the middle of a BAR then that MMIO
63  * region gets split into two segments - one segment above the MSI-X table
64  * and the other segment below the MSI-X table - with a hole in place of
65  * the MSI-X table so accesses to it can be trapped and emulated.
66  *
67  * So, allocate a MMIO segment for each BAR register + 1 additional segment.
68  */
69 #define	MAX_MMIOSEGS	((PCIR_MAX_BAR_0 + 1) + 1)
70 
71 MALLOC_DEFINE(M_PPTMSIX, "pptmsix", "Passthru MSI-X resources");
72 
73 struct pptintr_arg {				/* pptintr(pptintr_arg) */
74 	struct pptdev	*pptdev;
75 	uint64_t	addr;
76 	uint64_t	msg_data;
77 };
78 
79 struct pptseg {
80 	vm_paddr_t	gpa;
81 	size_t		len;
82 	int		wired;
83 };
84 
85 struct pptdev {
86 	device_t	dev;
87 	struct vm	*vm;			/* owner of this device */
88 	TAILQ_ENTRY(pptdev)	next;
89 	struct pptseg mmio[MAX_MMIOSEGS];
90 	struct {
91 		int	num_msgs;		/* guest state */
92 
93 		int	startrid;		/* host state */
94 		struct resource *res[MAX_MSIMSGS];
95 		void	*cookie[MAX_MSIMSGS];
96 		struct pptintr_arg arg[MAX_MSIMSGS];
97 	} msi;
98 
99 	struct {
100 		int num_msgs;
101 		int startrid;
102 		int msix_table_rid;
103 		struct resource *msix_table_res;
104 		struct resource **res;
105 		void **cookie;
106 		struct pptintr_arg *arg;
107 	} msix;
108 };
109 
110 SYSCTL_DECL(_hw_vmm);
111 SYSCTL_NODE(_hw_vmm, OID_AUTO, ppt, CTLFLAG_RW, 0, "bhyve passthru devices");
112 
113 static int num_pptdevs;
114 SYSCTL_INT(_hw_vmm_ppt, OID_AUTO, devices, CTLFLAG_RD, &num_pptdevs, 0,
115     "number of pci passthru devices");
116 
117 static TAILQ_HEAD(, pptdev) pptdev_list = TAILQ_HEAD_INITIALIZER(pptdev_list);
118 
119 static int
120 ppt_probe(device_t dev)
121 {
122 	int bus, slot, func;
123 	struct pci_devinfo *dinfo;
124 
125 	dinfo = (struct pci_devinfo *)device_get_ivars(dev);
126 
127 	bus = pci_get_bus(dev);
128 	slot = pci_get_slot(dev);
129 	func = pci_get_function(dev);
130 
131 	/*
132 	 * To qualify as a pci passthrough device a device must:
133 	 * - be allowed by administrator to be used in this role
134 	 * - be an endpoint device
135 	 */
136 	if ((dinfo->cfg.hdrtype & PCIM_HDRTYPE) != PCIM_HDRTYPE_NORMAL)
137 		return (ENXIO);
138 	else if (vmm_is_pptdev(bus, slot, func))
139 		return (0);
140 	else
141 		/*
142 		 * Returning BUS_PROBE_NOWILDCARD here matches devices that the
143 		 * SR-IOV infrastructure specified as "ppt" passthrough devices.
144 		 * All normal devices that did not have "ppt" specified as their
145 		 * driver will not be matched by this.
146 		 */
147 		return (BUS_PROBE_NOWILDCARD);
148 }
149 
150 static int
151 ppt_attach(device_t dev)
152 {
153 	struct pptdev *ppt;
154 
155 	ppt = device_get_softc(dev);
156 
157 	num_pptdevs++;
158 	TAILQ_INSERT_TAIL(&pptdev_list, ppt, next);
159 	ppt->dev = dev;
160 
161 	if (bootverbose)
162 		device_printf(dev, "attached\n");
163 
164 	return (0);
165 }
166 
167 static int
168 ppt_detach(device_t dev)
169 {
170 	struct pptdev *ppt;
171 
172 	ppt = device_get_softc(dev);
173 
174 	if (ppt->vm != NULL)
175 		return (EBUSY);
176 	num_pptdevs--;
177 	TAILQ_REMOVE(&pptdev_list, ppt, next);
178 
179 	return (0);
180 }
181 
182 static device_method_t ppt_methods[] = {
183 	/* Device interface */
184 	DEVMETHOD(device_probe,		ppt_probe),
185 	DEVMETHOD(device_attach,	ppt_attach),
186 	DEVMETHOD(device_detach,	ppt_detach),
187 	{0, 0}
188 };
189 
190 static devclass_t ppt_devclass;
191 DEFINE_CLASS_0(ppt, ppt_driver, ppt_methods, sizeof(struct pptdev));
192 DRIVER_MODULE(ppt, pci, ppt_driver, ppt_devclass, NULL, NULL);
193 
194 static struct pptdev *
195 ppt_find(int bus, int slot, int func)
196 {
197 	device_t dev;
198 	struct pptdev *ppt;
199 	int b, s, f;
200 
201 	TAILQ_FOREACH(ppt, &pptdev_list, next) {
202 		dev = ppt->dev;
203 		b = pci_get_bus(dev);
204 		s = pci_get_slot(dev);
205 		f = pci_get_function(dev);
206 		if (bus == b && slot == s && func == f)
207 			return (ppt);
208 	}
209 	return (NULL);
210 }
211 
212 static void
213 ppt_unmap_mmio(struct vm *vm, struct pptdev *ppt)
214 {
215 	int i;
216 	struct pptseg *seg;
217 
218 	for (i = 0; i < MAX_MMIOSEGS; i++) {
219 		seg = &ppt->mmio[i];
220 		if (seg->len == 0)
221 			continue;
222 		(void)vm_unmap_mmio(vm, seg->gpa, seg->len);
223 		bzero(seg, sizeof(struct pptseg));
224 	}
225 }
226 
227 static void
228 ppt_teardown_msi(struct pptdev *ppt)
229 {
230 	int i, rid;
231 	void *cookie;
232 	struct resource *res;
233 
234 	if (ppt->msi.num_msgs == 0)
235 		return;
236 
237 	for (i = 0; i < ppt->msi.num_msgs; i++) {
238 		rid = ppt->msi.startrid + i;
239 		res = ppt->msi.res[i];
240 		cookie = ppt->msi.cookie[i];
241 
242 		if (cookie != NULL)
243 			bus_teardown_intr(ppt->dev, res, cookie);
244 
245 		if (res != NULL)
246 			bus_release_resource(ppt->dev, SYS_RES_IRQ, rid, res);
247 
248 		ppt->msi.res[i] = NULL;
249 		ppt->msi.cookie[i] = NULL;
250 	}
251 
252 	if (ppt->msi.startrid == 1)
253 		pci_release_msi(ppt->dev);
254 
255 	ppt->msi.num_msgs = 0;
256 }
257 
258 static void
259 ppt_teardown_msix_intr(struct pptdev *ppt, int idx)
260 {
261 	int rid;
262 	struct resource *res;
263 	void *cookie;
264 
265 	rid = ppt->msix.startrid + idx;
266 	res = ppt->msix.res[idx];
267 	cookie = ppt->msix.cookie[idx];
268 
269 	if (cookie != NULL)
270 		bus_teardown_intr(ppt->dev, res, cookie);
271 
272 	if (res != NULL)
273 		bus_release_resource(ppt->dev, SYS_RES_IRQ, rid, res);
274 
275 	ppt->msix.res[idx] = NULL;
276 	ppt->msix.cookie[idx] = NULL;
277 }
278 
279 static void
280 ppt_teardown_msix(struct pptdev *ppt)
281 {
282 	int i;
283 
284 	if (ppt->msix.num_msgs == 0)
285 		return;
286 
287 	for (i = 0; i < ppt->msix.num_msgs; i++)
288 		ppt_teardown_msix_intr(ppt, i);
289 
290 	if (ppt->msix.msix_table_res) {
291 		bus_release_resource(ppt->dev, SYS_RES_MEMORY,
292 				     ppt->msix.msix_table_rid,
293 				     ppt->msix.msix_table_res);
294 		ppt->msix.msix_table_res = NULL;
295 		ppt->msix.msix_table_rid = 0;
296 	}
297 
298 	free(ppt->msix.res, M_PPTMSIX);
299 	free(ppt->msix.cookie, M_PPTMSIX);
300 	free(ppt->msix.arg, M_PPTMSIX);
301 
302 	pci_release_msi(ppt->dev);
303 
304 	ppt->msix.num_msgs = 0;
305 }
306 
307 int
308 ppt_avail_devices(void)
309 {
310 
311 	return (num_pptdevs);
312 }
313 
314 int
315 ppt_assigned_devices(struct vm *vm)
316 {
317 	struct pptdev *ppt;
318 	int num;
319 
320 	num = 0;
321 	TAILQ_FOREACH(ppt, &pptdev_list, next) {
322 		if (ppt->vm == vm)
323 			num++;
324 	}
325 	return (num);
326 }
327 
328 boolean_t
329 ppt_is_mmio(struct vm *vm, vm_paddr_t gpa)
330 {
331 	int i;
332 	struct pptdev *ppt;
333 	struct pptseg *seg;
334 
335 	TAILQ_FOREACH(ppt, &pptdev_list, next) {
336 		if (ppt->vm != vm)
337 			continue;
338 
339 		for (i = 0; i < MAX_MMIOSEGS; i++) {
340 			seg = &ppt->mmio[i];
341 			if (seg->len == 0)
342 				continue;
343 			if (gpa >= seg->gpa && gpa < seg->gpa + seg->len)
344 				return (TRUE);
345 		}
346 	}
347 
348 	return (FALSE);
349 }
350 
351 int
352 ppt_assign_device(struct vm *vm, int bus, int slot, int func)
353 {
354 	struct pptdev *ppt;
355 
356 	ppt = ppt_find(bus, slot, func);
357 	if (ppt != NULL) {
358 		/*
359 		 * If this device is owned by a different VM then we
360 		 * cannot change its owner.
361 		 */
362 		if (ppt->vm != NULL && ppt->vm != vm)
363 			return (EBUSY);
364 
365 		pci_save_state(ppt->dev);
366 		pcie_flr(ppt->dev,
367 		    max(pcie_get_max_completion_timeout(ppt->dev) / 1000, 10),
368 		    true);
369 		pci_restore_state(ppt->dev);
370 		ppt->vm = vm;
371 		iommu_remove_device(iommu_host_domain(), pci_get_rid(ppt->dev));
372 		iommu_add_device(vm_iommu_domain(vm), pci_get_rid(ppt->dev));
373 		return (0);
374 	}
375 	return (ENOENT);
376 }
377 
378 int
379 ppt_unassign_device(struct vm *vm, int bus, int slot, int func)
380 {
381 	struct pptdev *ppt;
382 
383 	ppt = ppt_find(bus, slot, func);
384 	if (ppt != NULL) {
385 		/*
386 		 * If this device is not owned by this 'vm' then bail out.
387 		 */
388 		if (ppt->vm != vm)
389 			return (EBUSY);
390 
391 		pci_save_state(ppt->dev);
392 		pcie_flr(ppt->dev,
393 		    max(pcie_get_max_completion_timeout(ppt->dev) / 1000, 10),
394 		    true);
395 		pci_restore_state(ppt->dev);
396 		ppt_unmap_mmio(vm, ppt);
397 		ppt_teardown_msi(ppt);
398 		ppt_teardown_msix(ppt);
399 		iommu_remove_device(vm_iommu_domain(vm), pci_get_rid(ppt->dev));
400 		iommu_add_device(iommu_host_domain(), pci_get_rid(ppt->dev));
401 		ppt->vm = NULL;
402 		return (0);
403 	}
404 	return (ENOENT);
405 }
406 
407 int
408 ppt_unassign_all(struct vm *vm)
409 {
410 	struct pptdev *ppt;
411 	int bus, slot, func;
412 	device_t dev;
413 
414 	TAILQ_FOREACH(ppt, &pptdev_list, next) {
415 		if (ppt->vm == vm) {
416 			dev = ppt->dev;
417 			bus = pci_get_bus(dev);
418 			slot = pci_get_slot(dev);
419 			func = pci_get_function(dev);
420 			vm_unassign_pptdev(vm, bus, slot, func);
421 		}
422 	}
423 
424 	return (0);
425 }
426 
427 int
428 ppt_map_mmio(struct vm *vm, int bus, int slot, int func,
429 	     vm_paddr_t gpa, size_t len, vm_paddr_t hpa)
430 {
431 	int i, error;
432 	struct pptseg *seg;
433 	struct pptdev *ppt;
434 
435 	ppt = ppt_find(bus, slot, func);
436 	if (ppt != NULL) {
437 		if (ppt->vm != vm)
438 			return (EBUSY);
439 
440 		for (i = 0; i < MAX_MMIOSEGS; i++) {
441 			seg = &ppt->mmio[i];
442 			if (seg->len == 0) {
443 				error = vm_map_mmio(vm, gpa, len, hpa);
444 				if (error == 0) {
445 					seg->gpa = gpa;
446 					seg->len = len;
447 				}
448 				return (error);
449 			}
450 		}
451 		return (ENOSPC);
452 	}
453 	return (ENOENT);
454 }
455 
456 static int
457 pptintr(void *arg)
458 {
459 	struct pptdev *ppt;
460 	struct pptintr_arg *pptarg;
461 
462 	pptarg = arg;
463 	ppt = pptarg->pptdev;
464 
465 	if (ppt->vm != NULL)
466 		lapic_intr_msi(ppt->vm, pptarg->addr, pptarg->msg_data);
467 	else {
468 		/*
469 		 * XXX
470 		 * This is not expected to happen - panic?
471 		 */
472 	}
473 
474 	/*
475 	 * For legacy interrupts give other filters a chance in case
476 	 * the interrupt was not generated by the passthrough device.
477 	 */
478 	if (ppt->msi.startrid == 0)
479 		return (FILTER_STRAY);
480 	else
481 		return (FILTER_HANDLED);
482 }
483 
484 int
485 ppt_setup_msi(struct vm *vm, int vcpu, int bus, int slot, int func,
486 	      uint64_t addr, uint64_t msg, int numvec)
487 {
488 	int i, rid, flags;
489 	int msi_count, startrid, error, tmp;
490 	struct pptdev *ppt;
491 
492 	if (numvec < 0 || numvec > MAX_MSIMSGS)
493 		return (EINVAL);
494 
495 	ppt = ppt_find(bus, slot, func);
496 	if (ppt == NULL)
497 		return (ENOENT);
498 	if (ppt->vm != vm)		/* Make sure we own this device */
499 		return (EBUSY);
500 
501 	/* Free any allocated resources */
502 	ppt_teardown_msi(ppt);
503 
504 	if (numvec == 0)		/* nothing more to do */
505 		return (0);
506 
507 	flags = RF_ACTIVE;
508 	msi_count = pci_msi_count(ppt->dev);
509 	if (msi_count == 0) {
510 		startrid = 0;		/* legacy interrupt */
511 		msi_count = 1;
512 		flags |= RF_SHAREABLE;
513 	} else
514 		startrid = 1;		/* MSI */
515 
516 	/*
517 	 * The device must be capable of supporting the number of vectors
518 	 * the guest wants to allocate.
519 	 */
520 	if (numvec > msi_count)
521 		return (EINVAL);
522 
523 	/*
524 	 * Make sure that we can allocate all the MSI vectors that are needed
525 	 * by the guest.
526 	 */
527 	if (startrid == 1) {
528 		tmp = numvec;
529 		error = pci_alloc_msi(ppt->dev, &tmp);
530 		if (error)
531 			return (error);
532 		else if (tmp != numvec) {
533 			pci_release_msi(ppt->dev);
534 			return (ENOSPC);
535 		} else {
536 			/* success */
537 		}
538 	}
539 
540 	ppt->msi.startrid = startrid;
541 
542 	/*
543 	 * Allocate the irq resource and attach it to the interrupt handler.
544 	 */
545 	for (i = 0; i < numvec; i++) {
546 		ppt->msi.num_msgs = i + 1;
547 		ppt->msi.cookie[i] = NULL;
548 
549 		rid = startrid + i;
550 		ppt->msi.res[i] = bus_alloc_resource_any(ppt->dev, SYS_RES_IRQ,
551 							 &rid, flags);
552 		if (ppt->msi.res[i] == NULL)
553 			break;
554 
555 		ppt->msi.arg[i].pptdev = ppt;
556 		ppt->msi.arg[i].addr = addr;
557 		ppt->msi.arg[i].msg_data = msg + i;
558 
559 		error = bus_setup_intr(ppt->dev, ppt->msi.res[i],
560 				       INTR_TYPE_NET | INTR_MPSAFE,
561 				       pptintr, NULL, &ppt->msi.arg[i],
562 				       &ppt->msi.cookie[i]);
563 		if (error != 0)
564 			break;
565 	}
566 
567 	if (i < numvec) {
568 		ppt_teardown_msi(ppt);
569 		return (ENXIO);
570 	}
571 
572 	return (0);
573 }
574 
575 int
576 ppt_setup_msix(struct vm *vm, int vcpu, int bus, int slot, int func,
577 	       int idx, uint64_t addr, uint64_t msg, uint32_t vector_control)
578 {
579 	struct pptdev *ppt;
580 	struct pci_devinfo *dinfo;
581 	int numvec, alloced, rid, error;
582 	size_t res_size, cookie_size, arg_size;
583 
584 	ppt = ppt_find(bus, slot, func);
585 	if (ppt == NULL)
586 		return (ENOENT);
587 	if (ppt->vm != vm)		/* Make sure we own this device */
588 		return (EBUSY);
589 
590 	dinfo = device_get_ivars(ppt->dev);
591 	if (!dinfo)
592 		return (ENXIO);
593 
594 	/*
595 	 * First-time configuration:
596 	 * 	Allocate the MSI-X table
597 	 *	Allocate the IRQ resources
598 	 *	Set up some variables in ppt->msix
599 	 */
600 	if (ppt->msix.num_msgs == 0) {
601 		numvec = pci_msix_count(ppt->dev);
602 		if (numvec <= 0)
603 			return (EINVAL);
604 
605 		ppt->msix.startrid = 1;
606 		ppt->msix.num_msgs = numvec;
607 
608 		res_size = numvec * sizeof(ppt->msix.res[0]);
609 		cookie_size = numvec * sizeof(ppt->msix.cookie[0]);
610 		arg_size = numvec * sizeof(ppt->msix.arg[0]);
611 
612 		ppt->msix.res = malloc(res_size, M_PPTMSIX, M_WAITOK | M_ZERO);
613 		ppt->msix.cookie = malloc(cookie_size, M_PPTMSIX,
614 					  M_WAITOK | M_ZERO);
615 		ppt->msix.arg = malloc(arg_size, M_PPTMSIX, M_WAITOK | M_ZERO);
616 
617 		rid = dinfo->cfg.msix.msix_table_bar;
618 		ppt->msix.msix_table_res = bus_alloc_resource_any(ppt->dev,
619 					       SYS_RES_MEMORY, &rid, RF_ACTIVE);
620 
621 		if (ppt->msix.msix_table_res == NULL) {
622 			ppt_teardown_msix(ppt);
623 			return (ENOSPC);
624 		}
625 		ppt->msix.msix_table_rid = rid;
626 
627 		alloced = numvec;
628 		error = pci_alloc_msix(ppt->dev, &alloced);
629 		if (error || alloced != numvec) {
630 			ppt_teardown_msix(ppt);
631 			return (error == 0 ? ENOSPC: error);
632 		}
633 	}
634 
635 	if ((vector_control & PCIM_MSIX_VCTRL_MASK) == 0) {
636 		/* Tear down the IRQ if it's already set up */
637 		ppt_teardown_msix_intr(ppt, idx);
638 
639 		/* Allocate the IRQ resource */
640 		ppt->msix.cookie[idx] = NULL;
641 		rid = ppt->msix.startrid + idx;
642 		ppt->msix.res[idx] = bus_alloc_resource_any(ppt->dev, SYS_RES_IRQ,
643 							    &rid, RF_ACTIVE);
644 		if (ppt->msix.res[idx] == NULL)
645 			return (ENXIO);
646 
647 		ppt->msix.arg[idx].pptdev = ppt;
648 		ppt->msix.arg[idx].addr = addr;
649 		ppt->msix.arg[idx].msg_data = msg;
650 
651 		/* Setup the MSI-X interrupt */
652 		error = bus_setup_intr(ppt->dev, ppt->msix.res[idx],
653 				       INTR_TYPE_NET | INTR_MPSAFE,
654 				       pptintr, NULL, &ppt->msix.arg[idx],
655 				       &ppt->msix.cookie[idx]);
656 
657 		if (error != 0) {
658 			bus_teardown_intr(ppt->dev, ppt->msix.res[idx], ppt->msix.cookie[idx]);
659 			bus_release_resource(ppt->dev, SYS_RES_IRQ, rid, ppt->msix.res[idx]);
660 			ppt->msix.cookie[idx] = NULL;
661 			ppt->msix.res[idx] = NULL;
662 			return (ENXIO);
663 		}
664 	} else {
665 		/* Masked, tear it down if it's already been set up */
666 		ppt_teardown_msix_intr(ppt, idx);
667 	}
668 
669 	return (0);
670 }
671