1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2013 The FreeBSD Foundation
5 * All rights reserved.
6 *
7 * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
8 * under sponsorship from the FreeBSD Foundation.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD: stable/12/sys/x86/iommu/intel_utils.c 373067 2023-05-06 01:19:05Z jah $");
34
35 #include <sys/param.h>
36 #include <sys/bus.h>
37 #include <sys/kernel.h>
38 #include <sys/lock.h>
39 #include <sys/malloc.h>
40 #include <sys/memdesc.h>
41 #include <sys/mutex.h>
42 #include <sys/proc.h>
43 #include <sys/queue.h>
44 #include <sys/rman.h>
45 #include <sys/rwlock.h>
46 #include <sys/sched.h>
47 #include <sys/sf_buf.h>
48 #include <sys/sysctl.h>
49 #include <sys/systm.h>
50 #include <sys/taskqueue.h>
51 #include <sys/time.h>
52 #include <sys/tree.h>
53 #include <sys/vmem.h>
54 #include <dev/pci/pcivar.h>
55 #include <vm/vm.h>
56 #include <vm/vm_extern.h>
57 #include <vm/vm_kern.h>
58 #include <vm/vm_object.h>
59 #include <vm/vm_page.h>
60 #include <vm/vm_map.h>
61 #include <vm/vm_pageout.h>
62 #include <machine/bus.h>
63 #include <machine/cpu.h>
64 #include <machine/intr_machdep.h>
65 #include <x86/include/apicvar.h>
66 #include <x86/include/busdma_impl.h>
67 #include <x86/iommu/intel_reg.h>
68 #include <x86/iommu/busdma_dmar.h>
69 #include <dev/pci/pcireg.h>
70 #include <x86/iommu/intel_dmar.h>
71
72 u_int
dmar_nd2mask(u_int nd)73 dmar_nd2mask(u_int nd)
74 {
75 static const u_int masks[] = {
76 0x000f, /* nd == 0 */
77 0x002f, /* nd == 1 */
78 0x00ff, /* nd == 2 */
79 0x02ff, /* nd == 3 */
80 0x0fff, /* nd == 4 */
81 0x2fff, /* nd == 5 */
82 0xffff, /* nd == 6 */
83 0x0000, /* nd == 7 reserved */
84 };
85
86 KASSERT(nd <= 6, ("number of domains %d", nd));
87 return (masks[nd]);
88 }
89
90 static const struct sagaw_bits_tag {
91 int agaw;
92 int cap;
93 int awlvl;
94 int pglvl;
95 } sagaw_bits[] = {
96 {.agaw = 30, .cap = DMAR_CAP_SAGAW_2LVL, .awlvl = DMAR_CTX2_AW_2LVL,
97 .pglvl = 2},
98 {.agaw = 39, .cap = DMAR_CAP_SAGAW_3LVL, .awlvl = DMAR_CTX2_AW_3LVL,
99 .pglvl = 3},
100 {.agaw = 48, .cap = DMAR_CAP_SAGAW_4LVL, .awlvl = DMAR_CTX2_AW_4LVL,
101 .pglvl = 4},
102 {.agaw = 57, .cap = DMAR_CAP_SAGAW_5LVL, .awlvl = DMAR_CTX2_AW_5LVL,
103 .pglvl = 5}
104 /*
105 * 6-level paging (DMAR_CAP_SAGAW_6LVL) is not supported on any
106 * current VT-d hardware and its SAGAW field value is listed as
107 * reserved in the VT-d spec. If support is added in the future,
108 * this structure and the logic in dmar_maxaddr2mgaw() will need
109 * to change to avoid attempted comparison against 1ULL << 64.
110 */
111 };
112
113 bool
dmar_pglvl_supported(struct dmar_unit * unit,int pglvl)114 dmar_pglvl_supported(struct dmar_unit *unit, int pglvl)
115 {
116 int i;
117
118 for (i = 0; i < nitems(sagaw_bits); i++) {
119 if (sagaw_bits[i].pglvl != pglvl)
120 continue;
121 if ((DMAR_CAP_SAGAW(unit->hw_cap) & sagaw_bits[i].cap) != 0)
122 return (true);
123 }
124 return (false);
125 }
126
127 int
domain_set_agaw(struct dmar_domain * domain,int mgaw)128 domain_set_agaw(struct dmar_domain *domain, int mgaw)
129 {
130 int sagaw, i;
131
132 domain->mgaw = mgaw;
133 sagaw = DMAR_CAP_SAGAW(domain->dmar->hw_cap);
134 for (i = 0; i < nitems(sagaw_bits); i++) {
135 if (sagaw_bits[i].agaw >= mgaw) {
136 domain->agaw = sagaw_bits[i].agaw;
137 domain->pglvl = sagaw_bits[i].pglvl;
138 domain->awlvl = sagaw_bits[i].awlvl;
139 return (0);
140 }
141 }
142 device_printf(domain->dmar->dev,
143 "context request mgaw %d: no agaw found, sagaw %x\n",
144 mgaw, sagaw);
145 return (EINVAL);
146 }
147
148 /*
149 * Find a best fit mgaw for the given maxaddr:
150 * - if allow_less is false, must find sagaw which maps all requested
151 * addresses (used by identity mappings);
152 * - if allow_less is true, and no supported sagaw can map all requested
153 * address space, accept the biggest sagaw, whatever is it.
154 */
155 int
dmar_maxaddr2mgaw(struct dmar_unit * unit,dmar_gaddr_t maxaddr,bool allow_less)156 dmar_maxaddr2mgaw(struct dmar_unit *unit, dmar_gaddr_t maxaddr, bool allow_less)
157 {
158 int i;
159
160 for (i = 0; i < nitems(sagaw_bits); i++) {
161 if ((1ULL << sagaw_bits[i].agaw) >= maxaddr &&
162 (DMAR_CAP_SAGAW(unit->hw_cap) & sagaw_bits[i].cap) != 0)
163 break;
164 }
165 if (allow_less && i == nitems(sagaw_bits)) {
166 do {
167 i--;
168 } while ((DMAR_CAP_SAGAW(unit->hw_cap) & sagaw_bits[i].cap)
169 == 0);
170 }
171 if (i < nitems(sagaw_bits))
172 return (sagaw_bits[i].agaw);
173 KASSERT(0, ("no mgaw for maxaddr %jx allow_less %d",
174 (uintmax_t) maxaddr, allow_less));
175 return (-1);
176 }
177
178 /*
179 * Calculate the total amount of page table pages needed to map the
180 * whole bus address space on the context with the selected agaw.
181 */
182 vm_pindex_t
pglvl_max_pages(int pglvl)183 pglvl_max_pages(int pglvl)
184 {
185 vm_pindex_t res;
186 int i;
187
188 for (res = 0, i = pglvl; i > 0; i--) {
189 res *= DMAR_NPTEPG;
190 res++;
191 }
192 return (res);
193 }
194
195 /*
196 * Return true if the page table level lvl supports the superpage for
197 * the context ctx.
198 */
199 int
domain_is_sp_lvl(struct dmar_domain * domain,int lvl)200 domain_is_sp_lvl(struct dmar_domain *domain, int lvl)
201 {
202 int alvl, cap_sps;
203 static const int sagaw_sp[] = {
204 DMAR_CAP_SPS_2M,
205 DMAR_CAP_SPS_1G,
206 DMAR_CAP_SPS_512G,
207 DMAR_CAP_SPS_1T
208 };
209
210 alvl = domain->pglvl - lvl - 1;
211 cap_sps = DMAR_CAP_SPS(domain->dmar->hw_cap);
212 return (alvl < nitems(sagaw_sp) && (sagaw_sp[alvl] & cap_sps) != 0);
213 }
214
215 dmar_gaddr_t
pglvl_page_size(int total_pglvl,int lvl)216 pglvl_page_size(int total_pglvl, int lvl)
217 {
218 int rlvl;
219 static const dmar_gaddr_t pg_sz[] = {
220 (dmar_gaddr_t)DMAR_PAGE_SIZE,
221 (dmar_gaddr_t)DMAR_PAGE_SIZE << DMAR_NPTEPGSHIFT,
222 (dmar_gaddr_t)DMAR_PAGE_SIZE << (2 * DMAR_NPTEPGSHIFT),
223 (dmar_gaddr_t)DMAR_PAGE_SIZE << (3 * DMAR_NPTEPGSHIFT),
224 (dmar_gaddr_t)DMAR_PAGE_SIZE << (4 * DMAR_NPTEPGSHIFT),
225 (dmar_gaddr_t)DMAR_PAGE_SIZE << (5 * DMAR_NPTEPGSHIFT)
226 };
227
228 KASSERT(lvl >= 0 && lvl < total_pglvl,
229 ("total %d lvl %d", total_pglvl, lvl));
230 rlvl = total_pglvl - lvl - 1;
231 KASSERT(rlvl < nitems(pg_sz), ("sizeof pg_sz lvl %d", lvl));
232 return (pg_sz[rlvl]);
233 }
234
235 dmar_gaddr_t
domain_page_size(struct dmar_domain * domain,int lvl)236 domain_page_size(struct dmar_domain *domain, int lvl)
237 {
238
239 return (pglvl_page_size(domain->pglvl, lvl));
240 }
241
242 int
calc_am(struct dmar_unit * unit,dmar_gaddr_t base,dmar_gaddr_t size,dmar_gaddr_t * isizep)243 calc_am(struct dmar_unit *unit, dmar_gaddr_t base, dmar_gaddr_t size,
244 dmar_gaddr_t *isizep)
245 {
246 dmar_gaddr_t isize;
247 int am;
248
249 for (am = DMAR_CAP_MAMV(unit->hw_cap);; am--) {
250 isize = 1ULL << (am + DMAR_PAGE_SHIFT);
251 if ((base & (isize - 1)) == 0 && size >= isize)
252 break;
253 if (am == 0)
254 break;
255 }
256 *isizep = isize;
257 return (am);
258 }
259
260 dmar_haddr_t dmar_high;
261 int haw;
262 int dmar_tbl_pagecnt;
263
264 vm_page_t
dmar_pgalloc(vm_object_t obj,vm_pindex_t idx,int flags)265 dmar_pgalloc(vm_object_t obj, vm_pindex_t idx, int flags)
266 {
267 vm_page_t m;
268 int zeroed, aflags;
269
270 zeroed = (flags & DMAR_PGF_ZERO) != 0 ? VM_ALLOC_ZERO : 0;
271 aflags = zeroed | VM_ALLOC_NOBUSY | VM_ALLOC_SYSTEM | VM_ALLOC_NODUMP |
272 ((flags & DMAR_PGF_WAITOK) != 0 ? VM_ALLOC_WAITFAIL :
273 VM_ALLOC_NOWAIT);
274 for (;;) {
275 if ((flags & DMAR_PGF_OBJL) == 0)
276 VM_OBJECT_WLOCK(obj);
277 m = vm_page_lookup(obj, idx);
278 if ((flags & DMAR_PGF_NOALLOC) != 0 || m != NULL) {
279 if ((flags & DMAR_PGF_OBJL) == 0)
280 VM_OBJECT_WUNLOCK(obj);
281 break;
282 }
283 m = vm_page_alloc_contig(obj, idx, aflags, 1, 0,
284 dmar_high, PAGE_SIZE, 0, VM_MEMATTR_DEFAULT);
285 if ((flags & DMAR_PGF_OBJL) == 0)
286 VM_OBJECT_WUNLOCK(obj);
287 if (m != NULL) {
288 if (zeroed && (m->flags & PG_ZERO) == 0)
289 pmap_zero_page(m);
290 atomic_add_int(&dmar_tbl_pagecnt, 1);
291 break;
292 }
293 if ((flags & DMAR_PGF_WAITOK) == 0)
294 break;
295 }
296 return (m);
297 }
298
299 void
dmar_pgfree(vm_object_t obj,vm_pindex_t idx,int flags)300 dmar_pgfree(vm_object_t obj, vm_pindex_t idx, int flags)
301 {
302 vm_page_t m;
303
304 if ((flags & DMAR_PGF_OBJL) == 0)
305 VM_OBJECT_WLOCK(obj);
306 m = vm_page_lookup(obj, idx);
307 if (m != NULL) {
308 vm_page_free(m);
309 atomic_subtract_int(&dmar_tbl_pagecnt, 1);
310 }
311 if ((flags & DMAR_PGF_OBJL) == 0)
312 VM_OBJECT_WUNLOCK(obj);
313 }
314
315 void *
dmar_map_pgtbl(vm_object_t obj,vm_pindex_t idx,int flags,struct sf_buf ** sf)316 dmar_map_pgtbl(vm_object_t obj, vm_pindex_t idx, int flags,
317 struct sf_buf **sf)
318 {
319 vm_page_t m;
320 bool allocated;
321
322 if ((flags & DMAR_PGF_OBJL) == 0)
323 VM_OBJECT_WLOCK(obj);
324 m = vm_page_lookup(obj, idx);
325 if (m == NULL && (flags & DMAR_PGF_ALLOC) != 0) {
326 m = dmar_pgalloc(obj, idx, flags | DMAR_PGF_OBJL);
327 allocated = true;
328 } else
329 allocated = false;
330 if (m == NULL) {
331 if ((flags & DMAR_PGF_OBJL) == 0)
332 VM_OBJECT_WUNLOCK(obj);
333 return (NULL);
334 }
335 /* Sleepable allocations cannot fail. */
336 if ((flags & DMAR_PGF_WAITOK) != 0)
337 VM_OBJECT_WUNLOCK(obj);
338 sched_pin();
339 *sf = sf_buf_alloc(m, SFB_CPUPRIVATE | ((flags & DMAR_PGF_WAITOK)
340 == 0 ? SFB_NOWAIT : 0));
341 if (*sf == NULL) {
342 sched_unpin();
343 if (allocated) {
344 VM_OBJECT_ASSERT_WLOCKED(obj);
345 dmar_pgfree(obj, m->pindex, flags | DMAR_PGF_OBJL);
346 }
347 if ((flags & DMAR_PGF_OBJL) == 0)
348 VM_OBJECT_WUNLOCK(obj);
349 return (NULL);
350 }
351 if ((flags & (DMAR_PGF_WAITOK | DMAR_PGF_OBJL)) ==
352 (DMAR_PGF_WAITOK | DMAR_PGF_OBJL))
353 VM_OBJECT_WLOCK(obj);
354 else if ((flags & (DMAR_PGF_WAITOK | DMAR_PGF_OBJL)) == 0)
355 VM_OBJECT_WUNLOCK(obj);
356 return ((void *)sf_buf_kva(*sf));
357 }
358
359 void
dmar_unmap_pgtbl(struct sf_buf * sf)360 dmar_unmap_pgtbl(struct sf_buf *sf)
361 {
362
363 sf_buf_free(sf);
364 sched_unpin();
365 }
366
367 static void
dmar_flush_transl_to_ram(struct dmar_unit * unit,void * dst,size_t sz)368 dmar_flush_transl_to_ram(struct dmar_unit *unit, void *dst, size_t sz)
369 {
370
371 if (DMAR_IS_COHERENT(unit))
372 return;
373 /*
374 * If DMAR does not snoop paging structures accesses, flush
375 * CPU cache to memory.
376 */
377 pmap_force_invalidate_cache_range((uintptr_t)dst, (uintptr_t)dst + sz);
378 }
379
380 void
dmar_flush_pte_to_ram(struct dmar_unit * unit,dmar_pte_t * dst)381 dmar_flush_pte_to_ram(struct dmar_unit *unit, dmar_pte_t *dst)
382 {
383
384 dmar_flush_transl_to_ram(unit, dst, sizeof(*dst));
385 }
386
387 void
dmar_flush_ctx_to_ram(struct dmar_unit * unit,dmar_ctx_entry_t * dst)388 dmar_flush_ctx_to_ram(struct dmar_unit *unit, dmar_ctx_entry_t *dst)
389 {
390
391 dmar_flush_transl_to_ram(unit, dst, sizeof(*dst));
392 }
393
394 void
dmar_flush_root_to_ram(struct dmar_unit * unit,dmar_root_entry_t * dst)395 dmar_flush_root_to_ram(struct dmar_unit *unit, dmar_root_entry_t *dst)
396 {
397
398 dmar_flush_transl_to_ram(unit, dst, sizeof(*dst));
399 }
400
401 /*
402 * Load the root entry pointer into the hardware, busily waiting for
403 * the completion.
404 */
405 int
dmar_load_root_entry_ptr(struct dmar_unit * unit)406 dmar_load_root_entry_ptr(struct dmar_unit *unit)
407 {
408 vm_page_t root_entry;
409 int error;
410
411 /*
412 * Access to the GCMD register must be serialized while the
413 * command is submitted.
414 */
415 DMAR_ASSERT_LOCKED(unit);
416
417 VM_OBJECT_RLOCK(unit->ctx_obj);
418 root_entry = vm_page_lookup(unit->ctx_obj, 0);
419 VM_OBJECT_RUNLOCK(unit->ctx_obj);
420 dmar_write8(unit, DMAR_RTADDR_REG, VM_PAGE_TO_PHYS(root_entry));
421 dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd | DMAR_GCMD_SRTP);
422 DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_RTPS)
423 != 0));
424 return (error);
425 }
426
427 /*
428 * Globally invalidate the context entries cache, busily waiting for
429 * the completion.
430 */
431 int
dmar_inv_ctx_glob(struct dmar_unit * unit)432 dmar_inv_ctx_glob(struct dmar_unit *unit)
433 {
434 int error;
435
436 /*
437 * Access to the CCMD register must be serialized while the
438 * command is submitted.
439 */
440 DMAR_ASSERT_LOCKED(unit);
441 KASSERT(!unit->qi_enabled, ("QI enabled"));
442
443 /*
444 * The DMAR_CCMD_ICC bit in the upper dword should be written
445 * after the low dword write is completed. Amd64
446 * dmar_write8() does not have this issue, i386 dmar_write8()
447 * writes the upper dword last.
448 */
449 dmar_write8(unit, DMAR_CCMD_REG, DMAR_CCMD_ICC | DMAR_CCMD_CIRG_GLOB);
450 DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_CCMD_REG + 4) & DMAR_CCMD_ICC32)
451 == 0));
452 return (error);
453 }
454
455 /*
456 * Globally invalidate the IOTLB, busily waiting for the completion.
457 */
458 int
dmar_inv_iotlb_glob(struct dmar_unit * unit)459 dmar_inv_iotlb_glob(struct dmar_unit *unit)
460 {
461 int error, reg;
462
463 DMAR_ASSERT_LOCKED(unit);
464 KASSERT(!unit->qi_enabled, ("QI enabled"));
465
466 reg = 16 * DMAR_ECAP_IRO(unit->hw_ecap);
467 /* See a comment about DMAR_CCMD_ICC in dmar_inv_ctx_glob. */
468 dmar_write8(unit, reg + DMAR_IOTLB_REG_OFF, DMAR_IOTLB_IVT |
469 DMAR_IOTLB_IIRG_GLB | DMAR_IOTLB_DR | DMAR_IOTLB_DW);
470 DMAR_WAIT_UNTIL(((dmar_read4(unit, reg + DMAR_IOTLB_REG_OFF + 4) &
471 DMAR_IOTLB_IVT32) == 0));
472 return (error);
473 }
474
475 /*
476 * Flush the chipset write buffers. See 11.1 "Write Buffer Flushing"
477 * in the architecture specification.
478 */
479 int
dmar_flush_write_bufs(struct dmar_unit * unit)480 dmar_flush_write_bufs(struct dmar_unit *unit)
481 {
482 int error;
483
484 DMAR_ASSERT_LOCKED(unit);
485
486 /*
487 * DMAR_GCMD_WBF is only valid when CAP_RWBF is reported.
488 */
489 KASSERT((unit->hw_cap & DMAR_CAP_RWBF) != 0,
490 ("dmar%d: no RWBF", unit->unit));
491
492 dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd | DMAR_GCMD_WBF);
493 DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_WBFS)
494 != 0));
495 return (error);
496 }
497
498 int
dmar_enable_translation(struct dmar_unit * unit)499 dmar_enable_translation(struct dmar_unit *unit)
500 {
501 int error;
502
503 DMAR_ASSERT_LOCKED(unit);
504 unit->hw_gcmd |= DMAR_GCMD_TE;
505 dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd);
506 DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_TES)
507 != 0));
508 return (error);
509 }
510
511 int
dmar_disable_translation(struct dmar_unit * unit)512 dmar_disable_translation(struct dmar_unit *unit)
513 {
514 int error;
515
516 DMAR_ASSERT_LOCKED(unit);
517 unit->hw_gcmd &= ~DMAR_GCMD_TE;
518 dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd);
519 DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_TES)
520 == 0));
521 return (error);
522 }
523
524 int
dmar_load_irt_ptr(struct dmar_unit * unit)525 dmar_load_irt_ptr(struct dmar_unit *unit)
526 {
527 uint64_t irta, s;
528 int error;
529
530 DMAR_ASSERT_LOCKED(unit);
531 irta = unit->irt_phys;
532 if (DMAR_X2APIC(unit))
533 irta |= DMAR_IRTA_EIME;
534 s = fls(unit->irte_cnt) - 2;
535 KASSERT(unit->irte_cnt >= 2 && s <= DMAR_IRTA_S_MASK &&
536 powerof2(unit->irte_cnt),
537 ("IRTA_REG_S overflow %x", unit->irte_cnt));
538 irta |= s;
539 dmar_write8(unit, DMAR_IRTA_REG, irta);
540 dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd | DMAR_GCMD_SIRTP);
541 DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_IRTPS)
542 != 0));
543 return (error);
544 }
545
546 int
dmar_enable_ir(struct dmar_unit * unit)547 dmar_enable_ir(struct dmar_unit *unit)
548 {
549 int error;
550
551 DMAR_ASSERT_LOCKED(unit);
552 unit->hw_gcmd |= DMAR_GCMD_IRE;
553 unit->hw_gcmd &= ~DMAR_GCMD_CFI;
554 dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd);
555 DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_IRES)
556 != 0));
557 return (error);
558 }
559
560 int
dmar_disable_ir(struct dmar_unit * unit)561 dmar_disable_ir(struct dmar_unit *unit)
562 {
563 int error;
564
565 DMAR_ASSERT_LOCKED(unit);
566 unit->hw_gcmd &= ~DMAR_GCMD_IRE;
567 dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd);
568 DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_IRES)
569 == 0));
570 return (error);
571 }
572
573 #define BARRIER_F \
574 u_int f_done, f_inproc, f_wakeup; \
575 \
576 f_done = 1 << (barrier_id * 3); \
577 f_inproc = 1 << (barrier_id * 3 + 1); \
578 f_wakeup = 1 << (barrier_id * 3 + 2)
579
580 bool
dmar_barrier_enter(struct dmar_unit * dmar,u_int barrier_id)581 dmar_barrier_enter(struct dmar_unit *dmar, u_int barrier_id)
582 {
583 BARRIER_F;
584
585 DMAR_LOCK(dmar);
586 if ((dmar->barrier_flags & f_done) != 0) {
587 DMAR_UNLOCK(dmar);
588 return (false);
589 }
590
591 if ((dmar->barrier_flags & f_inproc) != 0) {
592 while ((dmar->barrier_flags & f_inproc) != 0) {
593 dmar->barrier_flags |= f_wakeup;
594 msleep(&dmar->barrier_flags, &dmar->lock, 0,
595 "dmarb", 0);
596 }
597 KASSERT((dmar->barrier_flags & f_done) != 0,
598 ("dmar%d barrier %d missing done", dmar->unit, barrier_id));
599 DMAR_UNLOCK(dmar);
600 return (false);
601 }
602
603 dmar->barrier_flags |= f_inproc;
604 DMAR_UNLOCK(dmar);
605 return (true);
606 }
607
608 void
dmar_barrier_exit(struct dmar_unit * dmar,u_int barrier_id)609 dmar_barrier_exit(struct dmar_unit *dmar, u_int barrier_id)
610 {
611 BARRIER_F;
612
613 DMAR_ASSERT_LOCKED(dmar);
614 KASSERT((dmar->barrier_flags & (f_done | f_inproc)) == f_inproc,
615 ("dmar%d barrier %d missed entry", dmar->unit, barrier_id));
616 dmar->barrier_flags |= f_done;
617 if ((dmar->barrier_flags & f_wakeup) != 0)
618 wakeup(&dmar->barrier_flags);
619 dmar->barrier_flags &= ~(f_inproc | f_wakeup);
620 DMAR_UNLOCK(dmar);
621 }
622
623 int dmar_batch_coalesce = 100;
624 struct timespec dmar_hw_timeout = {
625 .tv_sec = 0,
626 .tv_nsec = 1000000
627 };
628
629 static const uint64_t d = 1000000000;
630
631 void
dmar_update_timeout(uint64_t newval)632 dmar_update_timeout(uint64_t newval)
633 {
634
635 /* XXXKIB not atomic */
636 dmar_hw_timeout.tv_sec = newval / d;
637 dmar_hw_timeout.tv_nsec = newval % d;
638 }
639
640 uint64_t
dmar_get_timeout(void)641 dmar_get_timeout(void)
642 {
643
644 return ((uint64_t)dmar_hw_timeout.tv_sec * d +
645 dmar_hw_timeout.tv_nsec);
646 }
647
648 static int
dmar_timeout_sysctl(SYSCTL_HANDLER_ARGS)649 dmar_timeout_sysctl(SYSCTL_HANDLER_ARGS)
650 {
651 uint64_t val;
652 int error;
653
654 val = dmar_get_timeout();
655 error = sysctl_handle_long(oidp, &val, 0, req);
656 if (error != 0 || req->newptr == NULL)
657 return (error);
658 dmar_update_timeout(val);
659 return (error);
660 }
661
662 static SYSCTL_NODE(_hw, OID_AUTO, dmar, CTLFLAG_RD, NULL, "");
663 SYSCTL_INT(_hw_dmar, OID_AUTO, tbl_pagecnt, CTLFLAG_RD,
664 &dmar_tbl_pagecnt, 0,
665 "Count of pages used for DMAR pagetables");
666 SYSCTL_INT(_hw_dmar, OID_AUTO, batch_coalesce, CTLFLAG_RWTUN,
667 &dmar_batch_coalesce, 0,
668 "Number of qi batches between interrupt");
669 SYSCTL_PROC(_hw_dmar, OID_AUTO, timeout,
670 CTLTYPE_U64 | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 0,
671 dmar_timeout_sysctl, "QU",
672 "Timeout for command wait, in nanoseconds");
673 #ifdef INVARIANTS
674 int dmar_check_free;
675 SYSCTL_INT(_hw_dmar, OID_AUTO, check_free, CTLFLAG_RWTUN,
676 &dmar_check_free, 0,
677 "Check the GPA RBtree for free_down and free_after validity");
678 #endif
679
680