2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2013 Tycho Nightingale <tycho.nightingale@pluribusnetworks.com>
5 * Copyright (c) 2013 Neel Natu <neel@freebsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_bhyve_snapshot.h"
37 #include <sys/param.h>
38 #include <sys/queue.h>
40 #include <sys/mutex.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
45 #include <x86/apicreg.h>
46 #include <machine/vmm.h>
47 #include <machine/vmm_snapshot.h>
50 #include "vmm_lapic.h"
57 #define REDIR_ENTRIES 32
58 #define RTBL_RO_BITS ((uint64_t)(IOART_REM_IRR | IOART_DELIVS))
67 int acnt; /* sum of pin asserts (+1) and deasserts (-1) */
68 } rtbl[REDIR_ENTRIES];
71 #define VIOAPIC_LOCK(vioapic) mtx_lock_spin(&((vioapic)->mtx))
72 #define VIOAPIC_UNLOCK(vioapic) mtx_unlock_spin(&((vioapic)->mtx))
73 #define VIOAPIC_LOCKED(vioapic) mtx_owned(&((vioapic)->mtx))
75 static MALLOC_DEFINE(M_VIOAPIC, "vioapic", "bhyve virtual ioapic");
77 #define VIOAPIC_CTR1(vioapic, fmt, a1) \
78 VM_CTR1((vioapic)->vm, fmt, a1)
80 #define VIOAPIC_CTR2(vioapic, fmt, a1, a2) \
81 VM_CTR2((vioapic)->vm, fmt, a1, a2)
83 #define VIOAPIC_CTR3(vioapic, fmt, a1, a2, a3) \
84 VM_CTR3((vioapic)->vm, fmt, a1, a2, a3)
86 #define VIOAPIC_CTR4(vioapic, fmt, a1, a2, a3, a4) \
87 VM_CTR4((vioapic)->vm, fmt, a1, a2, a3, a4)
91 pinstate_str(bool asserted)
97 return ("deasserted");
102 vioapic_send_intr(struct vioapic *vioapic, int pin)
105 uint32_t low, high, dest;
108 KASSERT(pin >= 0 && pin < REDIR_ENTRIES,
109 ("vioapic_set_pinstate: invalid pin number %d", pin));
111 KASSERT(VIOAPIC_LOCKED(vioapic),
112 ("vioapic_set_pinstate: vioapic is not locked"));
114 low = vioapic->rtbl[pin].reg;
115 high = vioapic->rtbl[pin].reg >> 32;
117 if ((low & IOART_INTMASK) == IOART_INTMSET) {
118 VIOAPIC_CTR1(vioapic, "ioapic pin%d: masked", pin);
122 phys = ((low & IOART_DESTMOD) == IOART_DESTPHY);
123 delmode = low & IOART_DELMOD;
124 level = low & IOART_TRGRLVL ? true : false;
126 vioapic->rtbl[pin].reg |= IOART_REM_IRR;
128 vector = low & IOART_INTVEC;
129 dest = high >> APIC_ID_SHIFT;
130 vlapic_deliver_intr(vioapic->vm, level, dest, phys, delmode, vector);
134 vioapic_set_pinstate(struct vioapic *vioapic, int pin, bool newstate)
139 KASSERT(pin >= 0 && pin < REDIR_ENTRIES,
140 ("vioapic_set_pinstate: invalid pin number %d", pin));
142 KASSERT(VIOAPIC_LOCKED(vioapic),
143 ("vioapic_set_pinstate: vioapic is not locked"));
145 oldcnt = vioapic->rtbl[pin].acnt;
147 vioapic->rtbl[pin].acnt++;
149 vioapic->rtbl[pin].acnt--;
150 newcnt = vioapic->rtbl[pin].acnt;
153 VIOAPIC_CTR2(vioapic, "ioapic pin%d: bad acnt %d",
158 if (oldcnt == 0 && newcnt == 1) {
160 VIOAPIC_CTR1(vioapic, "ioapic pin%d: asserted", pin);
161 } else if (oldcnt == 1 && newcnt == 0) {
162 VIOAPIC_CTR1(vioapic, "ioapic pin%d: deasserted", pin);
164 VIOAPIC_CTR3(vioapic, "ioapic pin%d: %s, ignored, acnt %d",
165 pin, pinstate_str(newstate), newcnt);
169 vioapic_send_intr(vioapic, pin);
179 vioapic_set_irqstate(struct vm *vm, int irq, enum irqstate irqstate)
181 struct vioapic *vioapic;
183 if (irq < 0 || irq >= REDIR_ENTRIES)
186 vioapic = vm_ioapic(vm);
188 VIOAPIC_LOCK(vioapic);
190 case IRQSTATE_ASSERT:
191 vioapic_set_pinstate(vioapic, irq, true);
193 case IRQSTATE_DEASSERT:
194 vioapic_set_pinstate(vioapic, irq, false);
197 vioapic_set_pinstate(vioapic, irq, true);
198 vioapic_set_pinstate(vioapic, irq, false);
201 panic("vioapic_set_irqstate: invalid irqstate %d", irqstate);
203 VIOAPIC_UNLOCK(vioapic);
209 vioapic_assert_irq(struct vm *vm, int irq)
212 return (vioapic_set_irqstate(vm, irq, IRQSTATE_ASSERT));
216 vioapic_deassert_irq(struct vm *vm, int irq)
219 return (vioapic_set_irqstate(vm, irq, IRQSTATE_DEASSERT));
223 vioapic_pulse_irq(struct vm *vm, int irq)
226 return (vioapic_set_irqstate(vm, irq, IRQSTATE_PULSE));
230 * Reset the vlapic's trigger-mode register to reflect the ioapic pin
234 vioapic_update_tmr(struct vm *vm, int vcpuid, void *arg)
236 struct vioapic *vioapic;
237 struct vlapic *vlapic;
238 uint32_t low, high, dest;
239 int delmode, pin, vector;
242 vlapic = vm_lapic(vm, vcpuid);
243 vioapic = vm_ioapic(vm);
245 VIOAPIC_LOCK(vioapic);
247 * Reset all vectors to be edge-triggered.
249 vlapic_reset_tmr(vlapic);
250 for (pin = 0; pin < REDIR_ENTRIES; pin++) {
251 low = vioapic->rtbl[pin].reg;
252 high = vioapic->rtbl[pin].reg >> 32;
254 level = low & IOART_TRGRLVL ? true : false;
259 * For a level-triggered 'pin' let the vlapic figure out if
260 * an assertion on this 'pin' would result in an interrupt
261 * being delivered to it. If yes, then it will modify the
262 * TMR bit associated with this vector to level-triggered.
264 phys = ((low & IOART_DESTMOD) == IOART_DESTPHY);
265 delmode = low & IOART_DELMOD;
266 vector = low & IOART_INTVEC;
267 dest = high >> APIC_ID_SHIFT;
268 vlapic_set_tmr_level(vlapic, dest, phys, delmode, vector);
270 VIOAPIC_UNLOCK(vioapic);
274 vioapic_read(struct vioapic *vioapic, int vcpuid, uint32_t addr)
276 int regnum, pin, rshift;
278 regnum = addr & 0xff;
281 return (vioapic->id);
284 return (((REDIR_ENTRIES - 1) << MAXREDIRSHIFT) | 0x11);
287 return (vioapic->id);
293 /* redirection table entries */
294 if (regnum >= IOAPIC_REDTBL &&
295 regnum < IOAPIC_REDTBL + REDIR_ENTRIES * 2) {
296 pin = (regnum - IOAPIC_REDTBL) / 2;
297 if ((regnum - IOAPIC_REDTBL) % 2)
302 return (vioapic->rtbl[pin].reg >> rshift);
309 vioapic_write(struct vioapic *vioapic, int vcpuid, uint32_t addr, uint32_t data)
311 uint64_t data64, mask64;
312 uint64_t last, changed;
313 int regnum, pin, lshift;
316 regnum = addr & 0xff;
319 vioapic->id = data & APIC_ID_MASK;
329 /* redirection table entries */
330 if (regnum >= IOAPIC_REDTBL &&
331 regnum < IOAPIC_REDTBL + REDIR_ENTRIES * 2) {
332 pin = (regnum - IOAPIC_REDTBL) / 2;
333 if ((regnum - IOAPIC_REDTBL) % 2)
338 last = vioapic->rtbl[pin].reg;
340 data64 = (uint64_t)data << lshift;
341 mask64 = (uint64_t)0xffffffff << lshift;
342 vioapic->rtbl[pin].reg &= ~mask64 | RTBL_RO_BITS;
343 vioapic->rtbl[pin].reg |= data64 & ~RTBL_RO_BITS;
345 VIOAPIC_CTR2(vioapic, "ioapic pin%d: redir table entry %#lx",
346 pin, vioapic->rtbl[pin].reg);
349 * If any fields in the redirection table entry (except mask
350 * or polarity) have changed then rendezvous all the vcpus
351 * to update their vlapic trigger-mode registers.
353 changed = last ^ vioapic->rtbl[pin].reg;
354 if (changed & ~(IOART_INTMASK | IOART_INTPOL)) {
355 VIOAPIC_CTR1(vioapic, "ioapic pin%d: recalculate "
356 "vlapic trigger-mode register", pin);
357 VIOAPIC_UNLOCK(vioapic);
358 allvcpus = vm_active_cpus(vioapic->vm);
359 (void)vm_smp_rendezvous(vioapic->vm, vcpuid, allvcpus,
360 vioapic_update_tmr, NULL);
361 VIOAPIC_LOCK(vioapic);
365 * Generate an interrupt if the following conditions are met:
366 * - pin is not masked
367 * - previous interrupt has been EOIed
368 * - pin level is asserted
370 if ((vioapic->rtbl[pin].reg & IOART_INTMASK) == IOART_INTMCLR &&
371 (vioapic->rtbl[pin].reg & IOART_REM_IRR) == 0 &&
372 (vioapic->rtbl[pin].acnt > 0)) {
373 VIOAPIC_CTR2(vioapic, "ioapic pin%d: asserted at rtbl "
374 "write, acnt %d", pin, vioapic->rtbl[pin].acnt);
375 vioapic_send_intr(vioapic, pin);
381 vioapic_mmio_rw(struct vioapic *vioapic, int vcpuid, uint64_t gpa,
382 uint64_t *data, int size, bool doread)
386 offset = gpa - VIOAPIC_BASE;
389 * The IOAPIC specification allows 32-bit wide accesses to the
390 * IOREGSEL (offset 0) and IOWIN (offset 16) registers.
392 if (size != 4 || (offset != IOREGSEL && offset != IOWIN)) {
398 VIOAPIC_LOCK(vioapic);
399 if (offset == IOREGSEL) {
401 *data = vioapic->ioregsel;
403 vioapic->ioregsel = *data;
406 *data = vioapic_read(vioapic, vcpuid,
409 vioapic_write(vioapic, vcpuid, vioapic->ioregsel,
413 VIOAPIC_UNLOCK(vioapic);
419 vioapic_mmio_read(void *vm, int vcpuid, uint64_t gpa, uint64_t *rval,
423 struct vioapic *vioapic;
425 vioapic = vm_ioapic(vm);
426 error = vioapic_mmio_rw(vioapic, vcpuid, gpa, rval, size, true);
431 vioapic_mmio_write(void *vm, int vcpuid, uint64_t gpa, uint64_t wval,
435 struct vioapic *vioapic;
437 vioapic = vm_ioapic(vm);
438 error = vioapic_mmio_rw(vioapic, vcpuid, gpa, &wval, size, false);
443 vioapic_process_eoi(struct vm *vm, int vcpuid, int vector)
445 struct vioapic *vioapic;
448 KASSERT(vector >= 0 && vector < 256,
449 ("vioapic_process_eoi: invalid vector %d", vector));
451 vioapic = vm_ioapic(vm);
452 VIOAPIC_CTR1(vioapic, "ioapic processing eoi for vector %d", vector);
455 * XXX keep track of the pins associated with this vector instead
456 * of iterating on every single pin each time.
458 VIOAPIC_LOCK(vioapic);
459 for (pin = 0; pin < REDIR_ENTRIES; pin++) {
460 if ((vioapic->rtbl[pin].reg & IOART_REM_IRR) == 0)
462 if ((vioapic->rtbl[pin].reg & IOART_INTVEC) != vector)
464 vioapic->rtbl[pin].reg &= ~IOART_REM_IRR;
465 if (vioapic->rtbl[pin].acnt > 0) {
466 VIOAPIC_CTR2(vioapic, "ioapic pin%d: asserted at eoi, "
467 "acnt %d", pin, vioapic->rtbl[pin].acnt);
468 vioapic_send_intr(vioapic, pin);
471 VIOAPIC_UNLOCK(vioapic);
475 vioapic_init(struct vm *vm)
478 struct vioapic *vioapic;
480 vioapic = malloc(sizeof(struct vioapic), M_VIOAPIC, M_WAITOK | M_ZERO);
483 mtx_init(&vioapic->mtx, "vioapic lock", NULL, MTX_SPIN);
485 /* Initialize all redirection entries to mask all interrupts */
486 for (i = 0; i < REDIR_ENTRIES; i++)
487 vioapic->rtbl[i].reg = 0x0001000000010000UL;
493 vioapic_cleanup(struct vioapic *vioapic)
496 free(vioapic, M_VIOAPIC);
500 vioapic_pincount(struct vm *vm)
503 return (REDIR_ENTRIES);
506 #ifdef BHYVE_SNAPSHOT
508 vioapic_snapshot(struct vioapic *vioapic, struct vm_snapshot_meta *meta)
513 SNAPSHOT_VAR_OR_LEAVE(vioapic->ioregsel, meta, ret, done);
515 for (i = 0; i < nitems(vioapic->rtbl); i++) {
516 SNAPSHOT_VAR_OR_LEAVE(vioapic->rtbl[i].reg, meta, ret, done);
517 SNAPSHOT_VAR_OR_LEAVE(vioapic->rtbl[i].acnt, meta, ret, done);