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