2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2019 Ruslan Bukin <br@bsdpad.com>
6 * This software was developed by SRI International and the University of
7 * Cambridge Computer Laboratory (Department of Computer Science and
8 * Technology) under DARPA contract HR0011-18-C-0016 ("ECATS"), as part of the
9 * DARPA SSITH research programme.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include "opt_platform.h"
37 #include <sys/param.h>
40 #include <sys/kernel.h>
42 #include <sys/malloc.h>
44 #include <sys/mutex.h>
45 #include <sys/rwlock.h>
47 #include <machine/bus.h>
51 #include <vm/vm_extern.h>
52 #include <vm/vm_page.h>
55 #include <dev/fdt/fdt_common.h>
56 #include <dev/ofw/ofw_bus.h>
57 #include <dev/ofw/ofw_bus_subr.h>
60 #include <dev/xdma/xdma.h>
64 xdma_iommu_remove_entry(xdma_channel_t *xchan, vm_offset_t va)
66 struct xdma_iommu *xio;
70 va &= ~(PAGE_SIZE - 1);
71 pmap_remove(&xio->p, va, va + PAGE_SIZE);
73 XDMA_IOMMU_REMOVE(xio->dev, xio, va);
75 vmem_free(xio->vmem, va, PAGE_SIZE);
79 xdma_iommu_enter(struct xdma_iommu *xio, vm_offset_t va,
80 vm_paddr_t pa, vm_size_t size, vm_prot_t prot)
87 KASSERT((size & PAGE_MASK) == 0,
88 ("%s: device mapping not page-sized", __func__));
90 for (; size > 0; size -= PAGE_SIZE) {
91 m = PHYS_TO_VM_PAGE(pa);
92 pmap_enter(p, va, m, prot, prot | PMAP_ENTER_WIRED, 0);
94 XDMA_IOMMU_ENTER(xio->dev, xio, va, pa);
102 xdma_iommu_add_entry(xdma_channel_t *xchan, vm_offset_t *va,
103 vm_paddr_t pa, vm_size_t size, vm_prot_t prot)
105 struct xdma_iommu *xio;
108 size = roundup2(size, PAGE_SIZE);
111 if (vmem_alloc(xio->vmem, size,
112 M_FIRSTFIT | M_NOWAIT, &addr)) {
113 panic("Could not allocate virtual address.\n");
116 addr |= pa & (PAGE_SIZE - 1);
121 xdma_iommu_enter(xio, addr, pa, size, prot);
125 xdma_iommu_init(struct xdma_iommu *xio)
128 phandle_t mem_node, node;
135 node = ofw_bus_get_node(xio->dev);
136 if (!OF_hasprop(node, "va-region"))
139 if (OF_getencprop(node, "va-region", (void *)&mem_handle,
140 sizeof(mem_handle)) <= 0)
144 xio->vmem = vmem_create("xDMA vmem", 0, 0, PAGE_SIZE,
145 PAGE_SIZE, M_FIRSTFIT | M_WAITOK);
146 if (xio->vmem == NULL)
150 mem_node = OF_node_from_xref(mem_handle);
151 if (xdma_handle_mem_node(xio->vmem, mem_node) != 0) {
152 vmem_destroy(xio->vmem);
157 XDMA_IOMMU_INIT(xio->dev, xio);
163 xdma_iommu_release(struct xdma_iommu *xio)
166 pmap_release(&xio->p);
168 vmem_destroy(xio->vmem);
170 XDMA_IOMMU_RELEASE(xio->dev, xio);