2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2013 The FreeBSD Foundation
7 * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
8 * under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/domainset.h>
38 #include <sys/malloc.h>
41 #include <sys/interrupt.h>
42 #include <sys/kernel.h>
46 #include <sys/memdesc.h>
47 #include <sys/mutex.h>
48 #include <sys/sysctl.h>
50 #include <sys/taskqueue.h>
54 #include <dev/pci/pcireg.h>
55 #include <dev/pci/pcivar.h>
57 #include <vm/vm_extern.h>
58 #include <vm/vm_kern.h>
59 #include <vm/vm_object.h>
60 #include <vm/vm_page.h>
61 #include <vm/vm_map.h>
62 #include <machine/atomic.h>
63 #include <machine/bus.h>
64 #include <machine/md_var.h>
65 #include <machine/specialreg.h>
66 #include <x86/include/busdma_impl.h>
67 #include <x86/iommu/intel_reg.h>
68 #include <x86/iommu/busdma_dmar.h>
69 #include <x86/iommu/intel_dmar.h>
72 * busdma_dmar.c, the implementation of the busdma(9) interface using
73 * DMAR units from Intel VT-d.
77 dmar_bus_dma_is_dev_disabled(int domain, int bus, int slot, int func)
82 static const char bounce_str[] = "bounce";
83 static const char dmar_str[] = "dmar";
86 env = kern_getenv("hw.busdma.default");
88 if (strcmp(env, bounce_str) == 0)
90 else if (strcmp(env, dmar_str) == 0)
95 snprintf(str, sizeof(str), "hw.busdma.pci%d.%d.%d.%d",
96 domain, bus, slot, func);
97 env = kern_getenv(str);
99 return (default_bounce != 0);
100 if (strcmp(env, bounce_str) == 0)
102 else if (strcmp(env, dmar_str) == 0)
105 ret = default_bounce != 0;
111 * Given original device, find the requester ID that will be seen by
112 * the DMAR unit and used for page table lookup. PCI bridges may take
113 * ownership of transactions from downstream devices, so it may not be
114 * the same as the BSF of the target device. In those cases, all
115 * devices downstream of the bridge must share a single mapping
116 * domain, and must collectively be assigned to use either DMAR or
120 dmar_get_requester(device_t dev, uint16_t *rid)
122 devclass_t pci_class;
123 device_t l, pci, pcib, pcip, pcibp, requester;
128 pci_class = devclass_find("pci");
131 *rid = pci_get_rid(dev);
134 * Walk the bridge hierarchy from the target device to the
135 * host port to find the translating bridge nearest the DMAR
139 pci = device_get_parent(l);
140 KASSERT(pci != NULL, ("dmar_get_requester(%s): NULL parent "
141 "for %s", device_get_name(dev), device_get_name(l)));
142 KASSERT(device_get_devclass(pci) == pci_class,
143 ("dmar_get_requester(%s): non-pci parent %s for %s",
144 device_get_name(dev), device_get_name(pci),
145 device_get_name(l)));
147 pcib = device_get_parent(pci);
148 KASSERT(pcib != NULL, ("dmar_get_requester(%s): NULL bridge "
149 "for %s", device_get_name(dev), device_get_name(pci)));
152 * The parent of our "bridge" isn't another PCI bus,
153 * so pcib isn't a PCI->PCI bridge but rather a host
154 * port, and the requester ID won't be translated
157 pcip = device_get_parent(pcib);
158 if (device_get_devclass(pcip) != pci_class)
160 pcibp = device_get_parent(pcip);
162 if (pci_find_cap(l, PCIY_EXPRESS, &cap_offset) == 0) {
164 * Do not stop the loop even if the target
165 * device is PCIe, because it is possible (but
166 * unlikely) to have a PCI->PCIe bridge
167 * somewhere in the hierarchy.
172 * Device is not PCIe, it cannot be seen as a
173 * requester by DMAR unit. Check whether the
176 bridge_is_pcie = pci_find_cap(pcib, PCIY_EXPRESS,
181 * Check for a buggy PCIe/PCI bridge that
182 * doesn't report the express capability. If
183 * the bridge above it is express but isn't a
184 * PCI bridge, then we know pcib is actually a
187 if (!bridge_is_pcie && pci_find_cap(pcibp,
188 PCIY_EXPRESS, &cap_offset) == 0) {
189 pcie_flags = pci_read_config(pcibp,
190 cap_offset + PCIER_FLAGS, 2);
191 if ((pcie_flags & PCIEM_FLAGS_TYPE) !=
192 PCIEM_TYPE_PCI_BRIDGE)
193 bridge_is_pcie = true;
196 if (bridge_is_pcie) {
198 * The current device is not PCIe, but
199 * the bridge above it is. This is a
200 * PCIe->PCI bridge. Assume that the
201 * requester ID will be the secondary
202 * bus number with slot and function
205 * XXX: Doesn't handle the case where
206 * the bridge is PCIe->PCI-X, and the
207 * bridge will only take ownership of
208 * requests in some cases. We should
209 * provide context entries with the
210 * same page tables for taken and
211 * non-taken transactions.
213 *rid = PCI_RID(pci_get_bus(l), 0, 0);
217 * Neither the device nor the bridge
218 * above it are PCIe. This is a
219 * conventional PCI->PCI bridge, which
220 * will use the bridge's BSF as the
223 *rid = pci_get_rid(pcib);
232 dmar_instantiate_ctx(struct dmar_unit *dmar, device_t dev, bool rmrr)
235 struct dmar_ctx *ctx;
239 requester = dmar_get_requester(dev, &rid);
242 * If the user requested the IOMMU disabled for the device, we
243 * cannot disable the DMAR, due to possibility of other
244 * devices on the same DMAR still requiring translation.
245 * Instead provide the identity mapping for the device
248 disabled = dmar_bus_dma_is_dev_disabled(pci_get_domain(requester),
249 pci_get_bus(requester), pci_get_slot(requester),
250 pci_get_function(requester));
251 ctx = dmar_get_ctx_for_dev(dmar, requester, rid, disabled, rmrr);
256 * Keep the first reference on context, release the
260 if ((ctx->flags & DMAR_CTX_DISABLED) == 0) {
261 ctx->flags |= DMAR_CTX_DISABLED;
264 dmar_free_ctx_locked(dmar, ctx);
272 dmar_get_dma_tag(device_t dev, device_t child)
274 struct dmar_unit *dmar;
275 struct dmar_ctx *ctx;
278 dmar = dmar_find(child, bootverbose);
279 /* Not in scope of any DMAR ? */
282 if (!dmar->dma_enabled)
284 dmar_quirks_pre_use(dmar);
285 dmar_instantiate_rmrr_ctxs(dmar);
287 ctx = dmar_instantiate_ctx(dmar, child, false);
288 res = ctx == NULL ? NULL : (bus_dma_tag_t)&ctx->ctx_tag;
292 static MALLOC_DEFINE(M_DMAR_DMAMAP, "dmar_dmamap", "Intel DMAR DMA Map");
294 static void dmar_bus_schedule_dmamap(struct dmar_unit *unit,
295 struct bus_dmamap_dmar *map);
298 dmar_bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
299 bus_addr_t boundary, bus_addr_t lowaddr, bus_addr_t highaddr,
300 bus_dma_filter_t *filter, void *filterarg, bus_size_t maxsize,
301 int nsegments, bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
302 void *lockfuncarg, bus_dma_tag_t *dmat)
304 struct bus_dma_tag_dmar *newtag, *oldtag;
308 error = common_bus_dma_tag_create(parent != NULL ?
309 &((struct bus_dma_tag_dmar *)parent)->common : NULL, alignment,
310 boundary, lowaddr, highaddr, filter, filterarg, maxsize,
311 nsegments, maxsegsz, flags, lockfunc, lockfuncarg,
312 sizeof(struct bus_dma_tag_dmar), (void **)&newtag);
316 oldtag = (struct bus_dma_tag_dmar *)parent;
317 newtag->common.impl = &bus_dma_dmar_impl;
318 newtag->ctx = oldtag->ctx;
319 newtag->owner = oldtag->owner;
321 *dmat = (bus_dma_tag_t)newtag;
323 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
324 __func__, newtag, (newtag != NULL ? newtag->common.flags : 0),
330 dmar_bus_dma_tag_set_domain(bus_dma_tag_t dmat)
337 dmar_bus_dma_tag_destroy(bus_dma_tag_t dmat1)
339 struct bus_dma_tag_dmar *dmat, *dmat_copy, *parent;
343 dmat_copy = dmat = (struct bus_dma_tag_dmar *)dmat1;
346 if (dmat->map_count != 0) {
350 while (dmat != NULL) {
351 parent = (struct bus_dma_tag_dmar *)dmat->common.parent;
352 if (atomic_fetchadd_int(&dmat->common.ref_count, -1) ==
354 if (dmat == &dmat->ctx->ctx_tag)
355 dmar_free_ctx(dmat->ctx);
356 free_domain(dmat->segments, M_DMAR_DMAMAP);
357 free(dmat, M_DEVBUF);
364 CTR3(KTR_BUSDMA, "%s tag %p error %d", __func__, dmat_copy, error);
369 dmar_bus_dma_id_mapped(bus_dma_tag_t dmat, vm_paddr_t buf, bus_size_t buflen)
376 dmar_bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp)
378 struct bus_dma_tag_dmar *tag;
379 struct bus_dmamap_dmar *map;
381 tag = (struct bus_dma_tag_dmar *)dmat;
382 map = malloc_domainset(sizeof(*map), M_DMAR_DMAMAP,
383 DOMAINSET_PREF(tag->common.domain), M_NOWAIT | M_ZERO);
388 if (tag->segments == NULL) {
389 tag->segments = malloc_domainset(sizeof(bus_dma_segment_t) *
390 tag->common.nsegments, M_DMAR_DMAMAP,
391 DOMAINSET_PREF(tag->common.domain), M_NOWAIT);
392 if (tag->segments == NULL) {
393 free_domain(map, M_DMAR_DMAMAP);
398 TAILQ_INIT(&map->map_entries);
401 map->cansleep = false;
403 *mapp = (bus_dmamap_t)map;
409 dmar_bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map1)
411 struct bus_dma_tag_dmar *tag;
412 struct bus_dmamap_dmar *map;
413 struct dmar_domain *domain;
415 tag = (struct bus_dma_tag_dmar *)dmat;
416 map = (struct bus_dmamap_dmar *)map1;
418 domain = tag->ctx->domain;
419 DMAR_DOMAIN_LOCK(domain);
420 if (!TAILQ_EMPTY(&map->map_entries)) {
421 DMAR_DOMAIN_UNLOCK(domain);
424 DMAR_DOMAIN_UNLOCK(domain);
425 free_domain(map, M_DMAR_DMAMAP);
433 dmar_bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
436 struct bus_dma_tag_dmar *tag;
437 struct bus_dmamap_dmar *map;
441 error = dmar_bus_dmamap_create(dmat, flags, mapp);
445 mflags = (flags & BUS_DMA_NOWAIT) != 0 ? M_NOWAIT : M_WAITOK;
446 mflags |= (flags & BUS_DMA_ZERO) != 0 ? M_ZERO : 0;
447 attr = (flags & BUS_DMA_NOCACHE) != 0 ? VM_MEMATTR_UNCACHEABLE :
450 tag = (struct bus_dma_tag_dmar *)dmat;
451 map = (struct bus_dmamap_dmar *)*mapp;
453 if (tag->common.maxsize < PAGE_SIZE &&
454 tag->common.alignment <= tag->common.maxsize &&
455 attr == VM_MEMATTR_DEFAULT) {
456 *vaddr = malloc_domainset(tag->common.maxsize, M_DEVBUF,
457 DOMAINSET_PREF(tag->common.domain), mflags);
458 map->flags |= BUS_DMAMAP_DMAR_MALLOC;
460 *vaddr = (void *)kmem_alloc_attr_domainset(
461 DOMAINSET_PREF(tag->common.domain), tag->common.maxsize,
462 mflags, 0ul, BUS_SPACE_MAXADDR, attr);
463 map->flags |= BUS_DMAMAP_DMAR_KMEM_ALLOC;
465 if (*vaddr == NULL) {
466 dmar_bus_dmamap_destroy(dmat, *mapp);
474 dmar_bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map1)
476 struct bus_dma_tag_dmar *tag;
477 struct bus_dmamap_dmar *map;
479 tag = (struct bus_dma_tag_dmar *)dmat;
480 map = (struct bus_dmamap_dmar *)map1;
482 if ((map->flags & BUS_DMAMAP_DMAR_MALLOC) != 0) {
483 free_domain(vaddr, M_DEVBUF);
484 map->flags &= ~BUS_DMAMAP_DMAR_MALLOC;
486 KASSERT((map->flags & BUS_DMAMAP_DMAR_KMEM_ALLOC) != 0,
487 ("dmar_bus_dmamem_free for non alloced map %p", map));
488 kmem_free((vm_offset_t)vaddr, tag->common.maxsize);
489 map->flags &= ~BUS_DMAMAP_DMAR_KMEM_ALLOC;
492 dmar_bus_dmamap_destroy(dmat, map1);
496 dmar_bus_dmamap_load_something1(struct bus_dma_tag_dmar *tag,
497 struct bus_dmamap_dmar *map, vm_page_t *ma, int offset, bus_size_t buflen,
498 int flags, bus_dma_segment_t *segs, int *segp,
499 struct dmar_map_entries_tailq *unroll_list)
501 struct dmar_ctx *ctx;
502 struct dmar_domain *domain;
503 struct dmar_map_entry *entry;
506 int error, idx, gas_flags, seg;
508 KASSERT(offset < DMAR_PAGE_SIZE, ("offset %d", offset));
510 segs = tag->segments;
512 domain = ctx->domain;
518 if (seg >= tag->common.nsegments) {
522 buflen1 = buflen > tag->common.maxsegsz ?
523 tag->common.maxsegsz : buflen;
524 size = round_page(offset + buflen1);
527 * (Too) optimistically allow split if there are more
528 * then one segments left.
530 gas_flags = map->cansleep ? DMAR_GM_CANWAIT : 0;
531 if (seg + 1 < tag->common.nsegments)
532 gas_flags |= DMAR_GM_CANSPLIT;
534 error = dmar_gas_map(domain, &tag->common, size, offset,
535 DMAR_MAP_ENTRY_READ |
536 ((flags & BUS_DMA_NOWRITE) == 0 ? DMAR_MAP_ENTRY_WRITE : 0),
537 gas_flags, ma + idx, &entry);
540 if ((gas_flags & DMAR_GM_CANSPLIT) != 0) {
541 KASSERT(size >= entry->end - entry->start,
542 ("split increased entry size %jx %jx %jx",
543 (uintmax_t)size, (uintmax_t)entry->start,
544 (uintmax_t)entry->end));
545 size = entry->end - entry->start;
549 KASSERT(entry->end - entry->start == size,
550 ("no split allowed %jx %jx %jx",
551 (uintmax_t)size, (uintmax_t)entry->start,
552 (uintmax_t)entry->end));
554 if (offset + buflen1 > size)
555 buflen1 = size - offset;
556 if (buflen1 > tag->common.maxsegsz)
557 buflen1 = tag->common.maxsegsz;
559 KASSERT(((entry->start + offset) & (tag->common.alignment - 1))
561 ("alignment failed: ctx %p start 0x%jx offset %x "
562 "align 0x%jx", ctx, (uintmax_t)entry->start, offset,
563 (uintmax_t)tag->common.alignment));
564 KASSERT(entry->end <= tag->common.lowaddr ||
565 entry->start >= tag->common.highaddr,
566 ("entry placement failed: ctx %p start 0x%jx end 0x%jx "
567 "lowaddr 0x%jx highaddr 0x%jx", ctx,
568 (uintmax_t)entry->start, (uintmax_t)entry->end,
569 (uintmax_t)tag->common.lowaddr,
570 (uintmax_t)tag->common.highaddr));
571 KASSERT(dmar_test_boundary(entry->start + offset, buflen1,
572 tag->common.boundary),
573 ("boundary failed: ctx %p start 0x%jx end 0x%jx "
574 "boundary 0x%jx", ctx, (uintmax_t)entry->start,
575 (uintmax_t)entry->end, (uintmax_t)tag->common.boundary));
576 KASSERT(buflen1 <= tag->common.maxsegsz,
577 ("segment too large: ctx %p start 0x%jx end 0x%jx "
578 "buflen1 0x%jx maxsegsz 0x%jx", ctx,
579 (uintmax_t)entry->start, (uintmax_t)entry->end,
580 (uintmax_t)buflen1, (uintmax_t)tag->common.maxsegsz));
582 DMAR_DOMAIN_LOCK(domain);
583 TAILQ_INSERT_TAIL(&map->map_entries, entry, dmamap_link);
584 entry->flags |= DMAR_MAP_ENTRY_MAP;
585 DMAR_DOMAIN_UNLOCK(domain);
586 TAILQ_INSERT_TAIL(unroll_list, entry, unroll_link);
588 segs[seg].ds_addr = entry->start + offset;
589 segs[seg].ds_len = buflen1;
591 idx += OFF_TO_IDX(trunc_page(offset + buflen1));
593 offset &= DMAR_PAGE_MASK;
602 dmar_bus_dmamap_load_something(struct bus_dma_tag_dmar *tag,
603 struct bus_dmamap_dmar *map, vm_page_t *ma, int offset, bus_size_t buflen,
604 int flags, bus_dma_segment_t *segs, int *segp)
606 struct dmar_ctx *ctx;
607 struct dmar_domain *domain;
608 struct dmar_map_entry *entry, *entry1;
609 struct dmar_map_entries_tailq unroll_list;
613 domain = ctx->domain;
614 atomic_add_long(&ctx->loads, 1);
616 TAILQ_INIT(&unroll_list);
617 error = dmar_bus_dmamap_load_something1(tag, map, ma, offset,
618 buflen, flags, segs, segp, &unroll_list);
621 * The busdma interface does not allow us to report
622 * partial buffer load, so unfortunately we have to
623 * revert all work done.
625 DMAR_DOMAIN_LOCK(domain);
626 TAILQ_FOREACH_SAFE(entry, &unroll_list, unroll_link,
629 * No entries other than what we have created
630 * during the failed run might have been
631 * inserted there in between, since we own ctx
634 TAILQ_REMOVE(&map->map_entries, entry, dmamap_link);
635 TAILQ_REMOVE(&unroll_list, entry, unroll_link);
636 TAILQ_INSERT_TAIL(&domain->unload_entries, entry,
639 DMAR_DOMAIN_UNLOCK(domain);
640 taskqueue_enqueue(domain->dmar->delayed_taskqueue,
641 &domain->unload_task);
644 if (error == ENOMEM && (flags & BUS_DMA_NOWAIT) == 0 &&
647 if (error == EINPROGRESS)
648 dmar_bus_schedule_dmamap(domain->dmar, map);
653 dmar_bus_dmamap_load_ma(bus_dma_tag_t dmat, bus_dmamap_t map1,
654 struct vm_page **ma, bus_size_t tlen, int ma_offs, int flags,
655 bus_dma_segment_t *segs, int *segp)
657 struct bus_dma_tag_dmar *tag;
658 struct bus_dmamap_dmar *map;
660 tag = (struct bus_dma_tag_dmar *)dmat;
661 map = (struct bus_dmamap_dmar *)map1;
662 return (dmar_bus_dmamap_load_something(tag, map, ma, ma_offs, tlen,
667 dmar_bus_dmamap_load_phys(bus_dma_tag_t dmat, bus_dmamap_t map1,
668 vm_paddr_t buf, bus_size_t buflen, int flags, bus_dma_segment_t *segs,
671 struct bus_dma_tag_dmar *tag;
672 struct bus_dmamap_dmar *map;
674 vm_paddr_t pstart, pend, paddr;
675 int error, i, ma_cnt, mflags, offset;
677 tag = (struct bus_dma_tag_dmar *)dmat;
678 map = (struct bus_dmamap_dmar *)map1;
679 pstart = trunc_page(buf);
680 pend = round_page(buf + buflen);
681 offset = buf & PAGE_MASK;
682 ma_cnt = OFF_TO_IDX(pend - pstart);
683 mflags = map->cansleep ? M_WAITOK : M_NOWAIT;
684 ma = malloc(sizeof(vm_page_t) * ma_cnt, M_DEVBUF, mflags);
688 for (i = 0; i < ma_cnt; i++) {
689 paddr = pstart + ptoa(i);
690 ma[i] = PHYS_TO_VM_PAGE(paddr);
691 if (ma[i] == NULL || VM_PAGE_TO_PHYS(ma[i]) != paddr) {
693 * If PHYS_TO_VM_PAGE() returned NULL or the
694 * vm_page was not initialized we'll use a
698 fma = malloc(sizeof(struct vm_page) * ma_cnt,
699 M_DEVBUF, M_ZERO | mflags);
705 vm_page_initfake(&fma[i], pstart + ptoa(i),
710 error = dmar_bus_dmamap_load_something(tag, map, ma, offset, buflen,
718 dmar_bus_dmamap_load_buffer(bus_dma_tag_t dmat, bus_dmamap_t map1, void *buf,
719 bus_size_t buflen, pmap_t pmap, int flags, bus_dma_segment_t *segs,
722 struct bus_dma_tag_dmar *tag;
723 struct bus_dmamap_dmar *map;
725 vm_paddr_t pstart, pend, paddr;
726 int error, i, ma_cnt, mflags, offset;
728 tag = (struct bus_dma_tag_dmar *)dmat;
729 map = (struct bus_dmamap_dmar *)map1;
730 pstart = trunc_page((vm_offset_t)buf);
731 pend = round_page((vm_offset_t)buf + buflen);
732 offset = (vm_offset_t)buf & PAGE_MASK;
733 ma_cnt = OFF_TO_IDX(pend - pstart);
734 mflags = map->cansleep ? M_WAITOK : M_NOWAIT;
735 ma = malloc(sizeof(vm_page_t) * ma_cnt, M_DEVBUF, mflags);
739 for (i = 0; i < ma_cnt; i++, pstart += PAGE_SIZE) {
740 if (pmap == kernel_pmap)
741 paddr = pmap_kextract(pstart);
743 paddr = pmap_extract(pmap, pstart);
744 ma[i] = PHYS_TO_VM_PAGE(paddr);
745 if (ma[i] == NULL || VM_PAGE_TO_PHYS(ma[i]) != paddr) {
747 * If PHYS_TO_VM_PAGE() returned NULL or the
748 * vm_page was not initialized we'll use a
752 fma = malloc(sizeof(struct vm_page) * ma_cnt,
753 M_DEVBUF, M_ZERO | mflags);
759 vm_page_initfake(&fma[i], paddr, VM_MEMATTR_DEFAULT);
763 error = dmar_bus_dmamap_load_something(tag, map, ma, offset, buflen,
771 dmar_bus_dmamap_waitok(bus_dma_tag_t dmat, bus_dmamap_t map1,
772 struct memdesc *mem, bus_dmamap_callback_t *callback, void *callback_arg)
774 struct bus_dmamap_dmar *map;
778 map = (struct bus_dmamap_dmar *)map1;
780 map->tag = (struct bus_dma_tag_dmar *)dmat;
781 map->callback = callback;
782 map->callback_arg = callback_arg;
785 static bus_dma_segment_t *
786 dmar_bus_dmamap_complete(bus_dma_tag_t dmat, bus_dmamap_t map1,
787 bus_dma_segment_t *segs, int nsegs, int error)
789 struct bus_dma_tag_dmar *tag;
790 struct bus_dmamap_dmar *map;
792 tag = (struct bus_dma_tag_dmar *)dmat;
793 map = (struct bus_dmamap_dmar *)map1;
796 KASSERT(map->cansleep,
797 ("map not locked and not sleepable context %p", map));
800 * We are called from the delayed context. Relock the
803 (tag->common.lockfunc)(tag->common.lockfuncarg, BUS_DMA_LOCK);
808 segs = tag->segments;
813 * The limitations of busdma KPI forces the dmar to perform the actual
814 * unload, consisting of the unmapping of the map entries page tables,
815 * from the delayed context on i386, since page table page mapping
816 * might require a sleep to be successfull. The unfortunate
817 * consequence is that the DMA requests can be served some time after
818 * the bus_dmamap_unload() call returned.
820 * On amd64, we assume that sf allocation cannot fail.
823 dmar_bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map1)
825 struct bus_dma_tag_dmar *tag;
826 struct bus_dmamap_dmar *map;
827 struct dmar_ctx *ctx;
828 struct dmar_domain *domain;
829 #if defined(__amd64__)
830 struct dmar_map_entries_tailq entries;
833 tag = (struct bus_dma_tag_dmar *)dmat;
834 map = (struct bus_dmamap_dmar *)map1;
836 domain = ctx->domain;
837 atomic_add_long(&ctx->unloads, 1);
839 #if defined(__i386__)
840 DMAR_DOMAIN_LOCK(domain);
841 TAILQ_CONCAT(&domain->unload_entries, &map->map_entries, dmamap_link);
842 DMAR_DOMAIN_UNLOCK(domain);
843 taskqueue_enqueue(domain->dmar->delayed_taskqueue,
844 &domain->unload_task);
845 #else /* defined(__amd64__) */
846 TAILQ_INIT(&entries);
847 DMAR_DOMAIN_LOCK(domain);
848 TAILQ_CONCAT(&entries, &map->map_entries, dmamap_link);
849 DMAR_DOMAIN_UNLOCK(domain);
850 THREAD_NO_SLEEPING();
851 dmar_domain_unload(domain, &entries, false);
852 THREAD_SLEEPING_OK();
853 KASSERT(TAILQ_EMPTY(&entries), ("lazy dmar_ctx_unload %p", ctx));
858 dmar_bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map,
863 struct bus_dma_impl bus_dma_dmar_impl = {
864 .tag_create = dmar_bus_dma_tag_create,
865 .tag_destroy = dmar_bus_dma_tag_destroy,
866 .tag_set_domain = dmar_bus_dma_tag_set_domain,
867 .id_mapped = dmar_bus_dma_id_mapped,
868 .map_create = dmar_bus_dmamap_create,
869 .map_destroy = dmar_bus_dmamap_destroy,
870 .mem_alloc = dmar_bus_dmamem_alloc,
871 .mem_free = dmar_bus_dmamem_free,
872 .load_phys = dmar_bus_dmamap_load_phys,
873 .load_buffer = dmar_bus_dmamap_load_buffer,
874 .load_ma = dmar_bus_dmamap_load_ma,
875 .map_waitok = dmar_bus_dmamap_waitok,
876 .map_complete = dmar_bus_dmamap_complete,
877 .map_unload = dmar_bus_dmamap_unload,
878 .map_sync = dmar_bus_dmamap_sync,
882 dmar_bus_task_dmamap(void *arg, int pending)
884 struct bus_dma_tag_dmar *tag;
885 struct bus_dmamap_dmar *map;
886 struct dmar_unit *unit;
890 while ((map = TAILQ_FIRST(&unit->delayed_maps)) != NULL) {
891 TAILQ_REMOVE(&unit->delayed_maps, map, delay_link);
894 map->cansleep = true;
896 bus_dmamap_load_mem((bus_dma_tag_t)tag, (bus_dmamap_t)map,
897 &map->mem, map->callback, map->callback_arg,
899 map->cansleep = false;
901 (tag->common.lockfunc)(tag->common.lockfuncarg,
905 map->cansleep = false;
912 dmar_bus_schedule_dmamap(struct dmar_unit *unit, struct bus_dmamap_dmar *map)
917 TAILQ_INSERT_TAIL(&unit->delayed_maps, map, delay_link);
919 taskqueue_enqueue(unit->delayed_taskqueue, &unit->dmamap_load_task);
923 dmar_init_busdma(struct dmar_unit *unit)
926 unit->dma_enabled = 1;
927 TUNABLE_INT_FETCH("hw.dmar.dma", &unit->dma_enabled);
928 TAILQ_INIT(&unit->delayed_maps);
929 TASK_INIT(&unit->dmamap_load_task, 0, dmar_bus_task_dmamap, unit);
930 unit->delayed_taskqueue = taskqueue_create("dmar", M_WAITOK,
931 taskqueue_thread_enqueue, &unit->delayed_taskqueue);
932 taskqueue_start_threads(&unit->delayed_taskqueue, 1, PI_DISK,
933 "dmar%d busdma taskq", unit->unit);
938 dmar_fini_busdma(struct dmar_unit *unit)
941 if (unit->delayed_taskqueue == NULL)
944 taskqueue_drain(unit->delayed_taskqueue, &unit->dmamap_load_task);
945 taskqueue_free(unit->delayed_taskqueue);
946 unit->delayed_taskqueue = NULL;