2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2022 Ruslan Bukin <br@bsdpad.com>
6 * This work was supported by Innovate UK project 105694, "Digital Security
7 * by Design (DSbD) Technology Platform Prototype".
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/cdefs.h>
32 #include <sys/types.h>
33 #include <sys/systm.h>
35 #include <sys/bitstring.h>
36 #include <sys/kernel.h>
38 #include <sys/sysctl.h>
40 #include <sys/taskqueue.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
45 #include <dev/pci/pcireg.h>
46 #include <dev/pci/pcivar.h>
47 #include <dev/iommu/iommu.h>
49 #include <dev/ofw/ofw_bus.h>
50 #include <dev/ofw/ofw_bus_subr.h>
52 #include <dev/iommu/iommu.h>
54 #include <arm64/iommu/iommu.h>
58 static struct ofw_compat_data compat_data[] = {
64 smmu_fdt_probe(device_t dev)
66 if (!ofw_bus_status_okay(dev))
69 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
72 device_set_desc(dev, "ARM System MMU (SMMU) v3");
74 return (BUS_PROBE_DEFAULT);
78 smmu_fdt_attach(device_t dev)
80 struct smmu_softc *sc;
81 struct smmu_unit *unit;
82 struct iommu_unit *iommu;
87 sc = device_get_softc(dev);
90 node = ofw_bus_get_node(dev);
93 sc->res[0] = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
95 if (sc->res[0] == NULL) {
96 device_printf(dev, "Can't allocate memory resource.\n");
102 * Interrupt lines are "eventq", "priq", "cmdq-sync", "gerror".
105 err = ofw_bus_find_string_index(node, "interrupt-names", "eventq",
108 device_printf(dev, "Can't get eventq IRQ.\n");
113 sc->res[1] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
114 if (sc->res[1] == NULL) {
115 device_printf(dev, "Can't allocate eventq IRQ resource.\n");
121 * sc->res[2] is reserved for priq IRQ. It is optional and not used
122 * by the SMMU driver. This IRQ line may or may not be provided by
126 err = ofw_bus_find_string_index(node, "interrupt-names", "cmdq-sync",
129 device_printf(dev, "Can't get cmdq-sync IRQ.\n");
134 sc->res[3] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
135 if (sc->res[3] == NULL) {
136 device_printf(dev, "Can't allocate cmdq-sync IRQ resource.\n");
141 err = ofw_bus_find_string_index(node, "interrupt-names", "gerror",
144 device_printf(dev, "Can't get gerror IRQ.\n");
149 sc->res[4] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
150 if (sc->res[4] == NULL) {
151 device_printf(dev, "Can't allocate gerror IRQ resource.\n");
156 err = smmu_attach(dev);
163 iommu = &unit->iommu;
166 LIST_INIT(&unit->domain_list);
168 /* Use memory start address as an xref. */
169 sc->xref = OF_xref_from_node(node);
171 err = iommu_register(iommu);
173 device_printf(dev, "Failed to register SMMU.\n");
177 OF_device_register_xref(sc->xref, dev);
184 "Failed to attach. Error %d\n", err);
187 /* Failure so free resources. */
193 static device_method_t smmu_fdt_methods[] = {
194 /* Device interface */
195 DEVMETHOD(device_probe, smmu_fdt_probe),
196 DEVMETHOD(device_attach, smmu_fdt_attach),
200 DEFINE_CLASS_1(smmu, smmu_fdt_driver, smmu_fdt_methods,
201 sizeof(struct smmu_softc), smmu_driver);
203 EARLY_DRIVER_MODULE(smmu, simplebus, smmu_fdt_driver, 0, 0,
204 BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);