xref: /freebsd/sys/x86/iommu/intel_qi.c (revision fe75646a0234a261c0013bf1840fdac4acaf0cec)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2013 The FreeBSD Foundation
5  *
6  * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
7  * under sponsorship from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include "opt_acpi.h"
32 
33 #include <sys/param.h>
34 #include <sys/bus.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/memdesc.h>
38 #include <sys/module.h>
39 #include <sys/rman.h>
40 #include <sys/taskqueue.h>
41 #include <sys/time.h>
42 #include <sys/tree.h>
43 #include <sys/vmem.h>
44 #include <vm/vm.h>
45 #include <vm/vm_extern.h>
46 #include <vm/vm_kern.h>
47 #include <vm/vm_page.h>
48 #include <vm/vm_map.h>
49 #include <contrib/dev/acpica/include/acpi.h>
50 #include <contrib/dev/acpica/include/accommon.h>
51 #include <dev/acpica/acpivar.h>
52 #include <dev/pci/pcireg.h>
53 #include <machine/bus.h>
54 #include <machine/cpu.h>
55 #include <x86/include/busdma_impl.h>
56 #include <dev/iommu/busdma_iommu.h>
57 #include <x86/iommu/intel_reg.h>
58 #include <x86/iommu/intel_dmar.h>
59 
60 static bool
61 dmar_qi_seq_processed(const struct dmar_unit *unit,
62     const struct iommu_qi_genseq *pseq)
63 {
64 	u_int gen;
65 
66 	gen = unit->inv_waitd_gen;
67 	return (pseq->gen < gen ||
68 	    (pseq->gen == gen && pseq->seq <= unit->inv_waitd_seq_hw));
69 }
70 
71 static int
72 dmar_enable_qi(struct dmar_unit *unit)
73 {
74 	int error;
75 
76 	DMAR_ASSERT_LOCKED(unit);
77 	unit->hw_gcmd |= DMAR_GCMD_QIE;
78 	dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd);
79 	DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_QIES)
80 	    != 0));
81 	return (error);
82 }
83 
84 static int
85 dmar_disable_qi(struct dmar_unit *unit)
86 {
87 	int error;
88 
89 	DMAR_ASSERT_LOCKED(unit);
90 	unit->hw_gcmd &= ~DMAR_GCMD_QIE;
91 	dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd);
92 	DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_QIES)
93 	    == 0));
94 	return (error);
95 }
96 
97 static void
98 dmar_qi_advance_tail(struct dmar_unit *unit)
99 {
100 
101 	DMAR_ASSERT_LOCKED(unit);
102 	dmar_write4(unit, DMAR_IQT_REG, unit->inv_queue_tail);
103 }
104 
105 static void
106 dmar_qi_ensure(struct dmar_unit *unit, int descr_count)
107 {
108 	uint32_t head;
109 	int bytes;
110 
111 	DMAR_ASSERT_LOCKED(unit);
112 	bytes = descr_count << DMAR_IQ_DESCR_SZ_SHIFT;
113 	for (;;) {
114 		if (bytes <= unit->inv_queue_avail)
115 			break;
116 		/* refill */
117 		head = dmar_read4(unit, DMAR_IQH_REG);
118 		head &= DMAR_IQH_MASK;
119 		unit->inv_queue_avail = head - unit->inv_queue_tail -
120 		    DMAR_IQ_DESCR_SZ;
121 		if (head <= unit->inv_queue_tail)
122 			unit->inv_queue_avail += unit->inv_queue_size;
123 		if (bytes <= unit->inv_queue_avail)
124 			break;
125 
126 		/*
127 		 * No space in the queue, do busy wait.  Hardware must
128 		 * make a progress.  But first advance the tail to
129 		 * inform the descriptor streamer about entries we
130 		 * might have already filled, otherwise they could
131 		 * clog the whole queue..
132 		 *
133 		 * See dmar_qi_invalidate_locked() for a discussion
134 		 * about data race prevention.
135 		 */
136 		dmar_qi_advance_tail(unit);
137 		unit->inv_queue_full++;
138 		cpu_spinwait();
139 	}
140 	unit->inv_queue_avail -= bytes;
141 }
142 
143 static void
144 dmar_qi_emit(struct dmar_unit *unit, uint64_t data1, uint64_t data2)
145 {
146 
147 	DMAR_ASSERT_LOCKED(unit);
148 	*(volatile uint64_t *)(unit->inv_queue + unit->inv_queue_tail) = data1;
149 	unit->inv_queue_tail += DMAR_IQ_DESCR_SZ / 2;
150 	KASSERT(unit->inv_queue_tail <= unit->inv_queue_size,
151 	    ("tail overflow 0x%x 0x%jx", unit->inv_queue_tail,
152 	    (uintmax_t)unit->inv_queue_size));
153 	unit->inv_queue_tail &= unit->inv_queue_size - 1;
154 	*(volatile uint64_t *)(unit->inv_queue + unit->inv_queue_tail) = data2;
155 	unit->inv_queue_tail += DMAR_IQ_DESCR_SZ / 2;
156 	KASSERT(unit->inv_queue_tail <= unit->inv_queue_size,
157 	    ("tail overflow 0x%x 0x%jx", unit->inv_queue_tail,
158 	    (uintmax_t)unit->inv_queue_size));
159 	unit->inv_queue_tail &= unit->inv_queue_size - 1;
160 }
161 
162 static void
163 dmar_qi_emit_wait_descr(struct dmar_unit *unit, uint32_t seq, bool intr,
164     bool memw, bool fence)
165 {
166 
167 	DMAR_ASSERT_LOCKED(unit);
168 	dmar_qi_emit(unit, DMAR_IQ_DESCR_WAIT_ID |
169 	    (intr ? DMAR_IQ_DESCR_WAIT_IF : 0) |
170 	    (memw ? DMAR_IQ_DESCR_WAIT_SW : 0) |
171 	    (fence ? DMAR_IQ_DESCR_WAIT_FN : 0) |
172 	    (memw ? DMAR_IQ_DESCR_WAIT_SD(seq) : 0),
173 	    memw ? unit->inv_waitd_seq_hw_phys : 0);
174 }
175 
176 static void
177 dmar_qi_emit_wait_seq(struct dmar_unit *unit, struct iommu_qi_genseq *pseq,
178     bool emit_wait)
179 {
180 	struct iommu_qi_genseq gsec;
181 	uint32_t seq;
182 
183 	KASSERT(pseq != NULL, ("wait descriptor with no place for seq"));
184 	DMAR_ASSERT_LOCKED(unit);
185 	if (unit->inv_waitd_seq == 0xffffffff) {
186 		gsec.gen = unit->inv_waitd_gen;
187 		gsec.seq = unit->inv_waitd_seq;
188 		dmar_qi_ensure(unit, 1);
189 		dmar_qi_emit_wait_descr(unit, gsec.seq, false, true, false);
190 		dmar_qi_advance_tail(unit);
191 		while (!dmar_qi_seq_processed(unit, &gsec))
192 			cpu_spinwait();
193 		unit->inv_waitd_gen++;
194 		unit->inv_waitd_seq = 1;
195 	}
196 	seq = unit->inv_waitd_seq++;
197 	pseq->gen = unit->inv_waitd_gen;
198 	pseq->seq = seq;
199 	if (emit_wait) {
200 		dmar_qi_ensure(unit, 1);
201 		dmar_qi_emit_wait_descr(unit, seq, true, true, false);
202 	}
203 }
204 
205 /*
206  * To avoid missed wakeups, callers must increment the unit's waiters count
207  * before advancing the tail past the wait descriptor.
208  */
209 static void
210 dmar_qi_wait_for_seq(struct dmar_unit *unit, const struct iommu_qi_genseq *gseq,
211     bool nowait)
212 {
213 
214 	DMAR_ASSERT_LOCKED(unit);
215 	KASSERT(unit->inv_seq_waiters > 0, ("%s: no waiters", __func__));
216 	while (!dmar_qi_seq_processed(unit, gseq)) {
217 		if (cold || nowait) {
218 			cpu_spinwait();
219 		} else {
220 			msleep(&unit->inv_seq_waiters, &unit->iommu.lock, 0,
221 			    "dmarse", hz);
222 		}
223 	}
224 	unit->inv_seq_waiters--;
225 }
226 
227 static void
228 dmar_qi_invalidate_emit(struct dmar_domain *domain, iommu_gaddr_t base,
229     iommu_gaddr_t size, struct iommu_qi_genseq *pseq, bool emit_wait)
230 {
231 	struct dmar_unit *unit;
232 	iommu_gaddr_t isize;
233 	int am;
234 
235 	unit = domain->dmar;
236 	DMAR_ASSERT_LOCKED(unit);
237 	for (; size > 0; base += isize, size -= isize) {
238 		am = calc_am(unit, base, size, &isize);
239 		dmar_qi_ensure(unit, 1);
240 		dmar_qi_emit(unit, DMAR_IQ_DESCR_IOTLB_INV |
241 		    DMAR_IQ_DESCR_IOTLB_PAGE | DMAR_IQ_DESCR_IOTLB_DW |
242 		    DMAR_IQ_DESCR_IOTLB_DR |
243 		    DMAR_IQ_DESCR_IOTLB_DID(domain->domain),
244 		    base | am);
245 	}
246 	dmar_qi_emit_wait_seq(unit, pseq, emit_wait);
247 }
248 
249 /*
250  * The caller must not be using the entry's dmamap_link field.
251  */
252 void
253 dmar_qi_invalidate_locked(struct dmar_domain *domain,
254     struct iommu_map_entry *entry, bool emit_wait)
255 {
256 	struct dmar_unit *unit;
257 
258 	unit = domain->dmar;
259 	DMAR_ASSERT_LOCKED(unit);
260 	dmar_qi_invalidate_emit(domain, entry->start, entry->end -
261 	    entry->start, &entry->gseq, emit_wait);
262 
263 	/*
264 	 * To avoid a data race in dmar_qi_task(), the entry's gseq must be
265 	 * initialized before the entry is added to the TLB flush list, and the
266 	 * entry must be added to that list before the tail is advanced.  More
267 	 * precisely, the tail must not be advanced past the wait descriptor
268 	 * that will generate the interrupt that schedules dmar_qi_task() for
269 	 * execution before the entry is added to the list.  While an earlier
270 	 * call to dmar_qi_ensure() might have advanced the tail, it will not
271 	 * advance it past the wait descriptor.
272 	 *
273 	 * See the definition of struct dmar_unit for more information on
274 	 * synchronization.
275 	 */
276 	entry->tlb_flush_next = NULL;
277 	atomic_store_rel_ptr((uintptr_t *)&unit->tlb_flush_tail->tlb_flush_next,
278 	    (uintptr_t)entry);
279 	unit->tlb_flush_tail = entry;
280 
281 	dmar_qi_advance_tail(unit);
282 }
283 
284 void
285 dmar_qi_invalidate_sync(struct dmar_domain *domain, iommu_gaddr_t base,
286     iommu_gaddr_t size, bool cansleep)
287 {
288 	struct dmar_unit *unit;
289 	struct iommu_qi_genseq gseq;
290 
291 	unit = domain->dmar;
292 	DMAR_LOCK(unit);
293 	dmar_qi_invalidate_emit(domain, base, size, &gseq, true);
294 
295 	/*
296 	 * To avoid a missed wakeup in dmar_qi_task(), the unit's waiters count
297 	 * must be incremented before the tail is advanced.
298 	 */
299 	unit->inv_seq_waiters++;
300 
301 	dmar_qi_advance_tail(unit);
302 	dmar_qi_wait_for_seq(unit, &gseq, !cansleep);
303 	DMAR_UNLOCK(unit);
304 }
305 
306 void
307 dmar_qi_invalidate_ctx_glob_locked(struct dmar_unit *unit)
308 {
309 	struct iommu_qi_genseq gseq;
310 
311 	DMAR_ASSERT_LOCKED(unit);
312 	dmar_qi_ensure(unit, 2);
313 	dmar_qi_emit(unit, DMAR_IQ_DESCR_CTX_INV | DMAR_IQ_DESCR_CTX_GLOB, 0);
314 	dmar_qi_emit_wait_seq(unit, &gseq, true);
315 	/* See dmar_qi_invalidate_sync(). */
316 	unit->inv_seq_waiters++;
317 	dmar_qi_advance_tail(unit);
318 	dmar_qi_wait_for_seq(unit, &gseq, false);
319 }
320 
321 void
322 dmar_qi_invalidate_iotlb_glob_locked(struct dmar_unit *unit)
323 {
324 	struct iommu_qi_genseq gseq;
325 
326 	DMAR_ASSERT_LOCKED(unit);
327 	dmar_qi_ensure(unit, 2);
328 	dmar_qi_emit(unit, DMAR_IQ_DESCR_IOTLB_INV | DMAR_IQ_DESCR_IOTLB_GLOB |
329 	    DMAR_IQ_DESCR_IOTLB_DW | DMAR_IQ_DESCR_IOTLB_DR, 0);
330 	dmar_qi_emit_wait_seq(unit, &gseq, true);
331 	/* See dmar_qi_invalidate_sync(). */
332 	unit->inv_seq_waiters++;
333 	dmar_qi_advance_tail(unit);
334 	dmar_qi_wait_for_seq(unit, &gseq, false);
335 }
336 
337 void
338 dmar_qi_invalidate_iec_glob(struct dmar_unit *unit)
339 {
340 	struct iommu_qi_genseq gseq;
341 
342 	DMAR_ASSERT_LOCKED(unit);
343 	dmar_qi_ensure(unit, 2);
344 	dmar_qi_emit(unit, DMAR_IQ_DESCR_IEC_INV, 0);
345 	dmar_qi_emit_wait_seq(unit, &gseq, true);
346 	/* See dmar_qi_invalidate_sync(). */
347 	unit->inv_seq_waiters++;
348 	dmar_qi_advance_tail(unit);
349 	dmar_qi_wait_for_seq(unit, &gseq, false);
350 }
351 
352 void
353 dmar_qi_invalidate_iec(struct dmar_unit *unit, u_int start, u_int cnt)
354 {
355 	struct iommu_qi_genseq gseq;
356 	u_int c, l;
357 
358 	DMAR_ASSERT_LOCKED(unit);
359 	KASSERT(start < unit->irte_cnt && start < start + cnt &&
360 	    start + cnt <= unit->irte_cnt,
361 	    ("inv iec overflow %d %d %d", unit->irte_cnt, start, cnt));
362 	for (; cnt > 0; cnt -= c, start += c) {
363 		l = ffs(start | cnt) - 1;
364 		c = 1 << l;
365 		dmar_qi_ensure(unit, 1);
366 		dmar_qi_emit(unit, DMAR_IQ_DESCR_IEC_INV |
367 		    DMAR_IQ_DESCR_IEC_IDX | DMAR_IQ_DESCR_IEC_IIDX(start) |
368 		    DMAR_IQ_DESCR_IEC_IM(l), 0);
369 	}
370 	dmar_qi_ensure(unit, 1);
371 	dmar_qi_emit_wait_seq(unit, &gseq, true);
372 
373 	/*
374 	 * Since dmar_qi_wait_for_seq() will not sleep, this increment's
375 	 * placement relative to advancing the tail doesn't matter.
376 	 */
377 	unit->inv_seq_waiters++;
378 
379 	dmar_qi_advance_tail(unit);
380 
381 	/*
382 	 * The caller of the function, in particular,
383 	 * dmar_ir_program_irte(), may be called from the context
384 	 * where the sleeping is forbidden (in fact, the
385 	 * intr_table_lock mutex may be held, locked from
386 	 * intr_shuffle_irqs()).  Wait for the invalidation completion
387 	 * using the busy wait.
388 	 *
389 	 * The impact on the interrupt input setup code is small, the
390 	 * expected overhead is comparable with the chipset register
391 	 * read.  It is more harmful for the parallel DMA operations,
392 	 * since we own the dmar unit lock until whole invalidation
393 	 * queue is processed, which includes requests possibly issued
394 	 * before our request.
395 	 */
396 	dmar_qi_wait_for_seq(unit, &gseq, true);
397 }
398 
399 int
400 dmar_qi_intr(void *arg)
401 {
402 	struct dmar_unit *unit;
403 
404 	unit = arg;
405 	KASSERT(unit->qi_enabled, ("dmar%d: QI is not enabled",
406 	    unit->iommu.unit));
407 	taskqueue_enqueue(unit->qi_taskqueue, &unit->qi_task);
408 	return (FILTER_HANDLED);
409 }
410 
411 static void
412 dmar_qi_drain_tlb_flush(struct dmar_unit *unit)
413 {
414 	struct iommu_map_entry *entry, *head;
415 
416 	for (head = unit->tlb_flush_head;; head = entry) {
417 		entry = (struct iommu_map_entry *)
418 		    atomic_load_acq_ptr((uintptr_t *)&head->tlb_flush_next);
419 		if (entry == NULL ||
420 		    !dmar_qi_seq_processed(unit, &entry->gseq))
421 			break;
422 		unit->tlb_flush_head = entry;
423 		iommu_gas_free_entry(head);
424 		if ((entry->flags & IOMMU_MAP_ENTRY_RMRR) != 0)
425 			iommu_gas_free_region(entry);
426 		else
427 			iommu_gas_free_space(entry);
428 	}
429 }
430 
431 static void
432 dmar_qi_task(void *arg, int pending __unused)
433 {
434 	struct dmar_unit *unit;
435 	uint32_t ics;
436 
437 	unit = arg;
438 	dmar_qi_drain_tlb_flush(unit);
439 
440 	/*
441 	 * Request an interrupt on the completion of the next invalidation
442 	 * wait descriptor with the IF field set.
443 	 */
444 	ics = dmar_read4(unit, DMAR_ICS_REG);
445 	if ((ics & DMAR_ICS_IWC) != 0) {
446 		ics = DMAR_ICS_IWC;
447 		dmar_write4(unit, DMAR_ICS_REG, ics);
448 
449 		/*
450 		 * Drain a second time in case the DMAR processes an entry
451 		 * after the first call and before clearing DMAR_ICS_IWC.
452 		 * Otherwise, such entries will linger until a later entry
453 		 * that requests an interrupt is processed.
454 		 */
455 		dmar_qi_drain_tlb_flush(unit);
456 	}
457 
458 	if (unit->inv_seq_waiters > 0) {
459 		/*
460 		 * Acquire the DMAR lock so that wakeup() is called only after
461 		 * the waiter is sleeping.
462 		 */
463 		DMAR_LOCK(unit);
464 		wakeup(&unit->inv_seq_waiters);
465 		DMAR_UNLOCK(unit);
466 	}
467 }
468 
469 int
470 dmar_init_qi(struct dmar_unit *unit)
471 {
472 	uint64_t iqa;
473 	uint32_t ics;
474 	int qi_sz;
475 
476 	if (!DMAR_HAS_QI(unit) || (unit->hw_cap & DMAR_CAP_CM) != 0)
477 		return (0);
478 	unit->qi_enabled = 1;
479 	TUNABLE_INT_FETCH("hw.dmar.qi", &unit->qi_enabled);
480 	if (!unit->qi_enabled)
481 		return (0);
482 
483 	unit->tlb_flush_head = unit->tlb_flush_tail =
484             iommu_gas_alloc_entry(NULL, 0);
485 	TASK_INIT(&unit->qi_task, 0, dmar_qi_task, unit);
486 	unit->qi_taskqueue = taskqueue_create_fast("dmarqf", M_WAITOK,
487 	    taskqueue_thread_enqueue, &unit->qi_taskqueue);
488 	taskqueue_start_threads(&unit->qi_taskqueue, 1, PI_AV,
489 	    "dmar%d qi taskq", unit->iommu.unit);
490 
491 	unit->inv_waitd_gen = 0;
492 	unit->inv_waitd_seq = 1;
493 
494 	qi_sz = DMAR_IQA_QS_DEF;
495 	TUNABLE_INT_FETCH("hw.dmar.qi_size", &qi_sz);
496 	if (qi_sz > DMAR_IQA_QS_MAX)
497 		qi_sz = DMAR_IQA_QS_MAX;
498 	unit->inv_queue_size = (1ULL << qi_sz) * PAGE_SIZE;
499 	/* Reserve one descriptor to prevent wraparound. */
500 	unit->inv_queue_avail = unit->inv_queue_size - DMAR_IQ_DESCR_SZ;
501 
502 	/* The invalidation queue reads by DMARs are always coherent. */
503 	unit->inv_queue = kmem_alloc_contig(unit->inv_queue_size, M_WAITOK |
504 	    M_ZERO, 0, dmar_high, PAGE_SIZE, 0, VM_MEMATTR_DEFAULT);
505 	unit->inv_waitd_seq_hw_phys = pmap_kextract(
506 	    (vm_offset_t)&unit->inv_waitd_seq_hw);
507 
508 	DMAR_LOCK(unit);
509 	dmar_write8(unit, DMAR_IQT_REG, 0);
510 	iqa = pmap_kextract((uintptr_t)unit->inv_queue);
511 	iqa |= qi_sz;
512 	dmar_write8(unit, DMAR_IQA_REG, iqa);
513 	dmar_enable_qi(unit);
514 	ics = dmar_read4(unit, DMAR_ICS_REG);
515 	if ((ics & DMAR_ICS_IWC) != 0) {
516 		ics = DMAR_ICS_IWC;
517 		dmar_write4(unit, DMAR_ICS_REG, ics);
518 	}
519 	dmar_enable_qi_intr(unit);
520 	DMAR_UNLOCK(unit);
521 
522 	return (0);
523 }
524 
525 void
526 dmar_fini_qi(struct dmar_unit *unit)
527 {
528 	struct iommu_qi_genseq gseq;
529 
530 	if (!unit->qi_enabled)
531 		return;
532 	taskqueue_drain(unit->qi_taskqueue, &unit->qi_task);
533 	taskqueue_free(unit->qi_taskqueue);
534 	unit->qi_taskqueue = NULL;
535 
536 	DMAR_LOCK(unit);
537 	/* quisce */
538 	dmar_qi_ensure(unit, 1);
539 	dmar_qi_emit_wait_seq(unit, &gseq, true);
540 	/* See dmar_qi_invalidate_sync_locked(). */
541 	unit->inv_seq_waiters++;
542 	dmar_qi_advance_tail(unit);
543 	dmar_qi_wait_for_seq(unit, &gseq, false);
544 	/* only after the quisce, disable queue */
545 	dmar_disable_qi_intr(unit);
546 	dmar_disable_qi(unit);
547 	KASSERT(unit->inv_seq_waiters == 0,
548 	    ("dmar%d: waiters on disabled queue", unit->iommu.unit));
549 	DMAR_UNLOCK(unit);
550 
551 	kmem_free(unit->inv_queue, unit->inv_queue_size);
552 	unit->inv_queue = NULL;
553 	unit->inv_queue_size = 0;
554 	unit->qi_enabled = 0;
555 }
556 
557 void
558 dmar_enable_qi_intr(struct dmar_unit *unit)
559 {
560 	uint32_t iectl;
561 
562 	DMAR_ASSERT_LOCKED(unit);
563 	KASSERT(DMAR_HAS_QI(unit), ("dmar%d: QI is not supported",
564 	    unit->iommu.unit));
565 	iectl = dmar_read4(unit, DMAR_IECTL_REG);
566 	iectl &= ~DMAR_IECTL_IM;
567 	dmar_write4(unit, DMAR_IECTL_REG, iectl);
568 }
569 
570 void
571 dmar_disable_qi_intr(struct dmar_unit *unit)
572 {
573 	uint32_t iectl;
574 
575 	DMAR_ASSERT_LOCKED(unit);
576 	KASSERT(DMAR_HAS_QI(unit), ("dmar%d: QI is not supported",
577 	    unit->iommu.unit));
578 	iectl = dmar_read4(unit, DMAR_IECTL_REG);
579 	dmar_write4(unit, DMAR_IECTL_REG, iectl | DMAR_IECTL_IM);
580 }
581