1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2022 Ruslan Bukin <br@bsdpad.com> 5 * 6 * This work was supported by Innovate UK project 105694, "Digital Security 7 * by Design (DSbD) Technology Platform Prototype". 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 __FBSDID("$FreeBSD$"); 33 34 #include <sys/types.h> 35 #include <sys/systm.h> 36 #include <sys/bus.h> 37 #include <sys/bitstring.h> 38 #include <sys/kernel.h> 39 #include <sys/rman.h> 40 #include <sys/sysctl.h> 41 #include <sys/tree.h> 42 #include <sys/taskqueue.h> 43 #include <sys/malloc.h> 44 #include <sys/module.h> 45 #include <vm/vm.h> 46 #include <vm/pmap.h> 47 #include <dev/pci/pcireg.h> 48 #include <dev/pci/pcivar.h> 49 #include <dev/iommu/iommu.h> 50 51 #include <dev/ofw/ofw_bus.h> 52 #include <dev/ofw/ofw_bus_subr.h> 53 54 #include <dev/iommu/iommu.h> 55 56 #include <arm64/iommu/iommu.h> 57 58 #include "smmuvar.h" 59 60 static struct ofw_compat_data compat_data[] = { 61 { "arm,smmu-v3", 1 }, 62 { NULL, 0 } 63 }; 64 65 static int 66 smmu_fdt_probe(device_t dev) 67 { 68 if (!ofw_bus_status_okay(dev)) 69 return (ENXIO); 70 71 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 72 return (ENXIO); 73 74 device_set_desc(dev, "ARM System MMU (SMMU) v3"); 75 76 return (BUS_PROBE_DEFAULT); 77 } 78 79 static int 80 smmu_fdt_attach(device_t dev) 81 { 82 struct smmu_softc *sc; 83 struct smmu_unit *unit; 84 struct iommu_unit *iommu; 85 phandle_t node; 86 int err; 87 int rid; 88 89 sc = device_get_softc(dev); 90 sc->dev = dev; 91 92 node = ofw_bus_get_node(dev); 93 94 rid = 0; 95 sc->res[0] = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 96 RF_ACTIVE); 97 if (sc->res[0] == NULL) { 98 device_printf(dev, "Can't allocate memory resource.\n"); 99 err = ENXIO; 100 goto error; 101 } 102 103 /* 104 * Interrupt lines are "eventq", "priq", "cmdq-sync", "gerror". 105 */ 106 107 err = ofw_bus_find_string_index(node, "interrupt-names", "eventq", 108 &rid); 109 if (err != 0) { 110 device_printf(dev, "Can't get eventq IRQ.\n"); 111 err = ENXIO; 112 goto error; 113 } 114 115 sc->res[1] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE); 116 if (sc->res[1] == NULL) { 117 device_printf(dev, "Can't allocate eventq IRQ resource.\n"); 118 err = ENXIO; 119 goto error; 120 } 121 122 /* 123 * sc->res[2] is reserved for priq IRQ. It is optional and not used 124 * by the SMMU driver. This IRQ line may or may not be provided by 125 * hardware. 126 */ 127 128 err = ofw_bus_find_string_index(node, "interrupt-names", "cmdq-sync", 129 &rid); 130 if (err != 0) { 131 device_printf(dev, "Can't get cmdq-sync IRQ.\n"); 132 err = ENXIO; 133 goto error; 134 } 135 136 sc->res[3] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE); 137 if (sc->res[3] == NULL) { 138 device_printf(dev, "Can't allocate cmdq-sync IRQ resource.\n"); 139 err = ENXIO; 140 goto error; 141 } 142 143 err = ofw_bus_find_string_index(node, "interrupt-names", "gerror", 144 &rid); 145 if (err != 0) { 146 device_printf(dev, "Can't get gerror IRQ.\n"); 147 err = ENXIO; 148 goto error; 149 } 150 151 sc->res[4] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE); 152 if (sc->res[4] == NULL) { 153 device_printf(dev, "Can't allocate gerror IRQ resource.\n"); 154 err = ENXIO; 155 goto error; 156 } 157 158 err = smmu_attach(dev); 159 if (err != 0) 160 goto error; 161 162 unit = &sc->unit; 163 unit->dev = dev; 164 165 iommu = &unit->iommu; 166 iommu->dev = dev; 167 168 LIST_INIT(&unit->domain_list); 169 170 /* Use memory start address as an xref. */ 171 sc->xref = bus_get_resource_start(dev, SYS_RES_MEMORY, 0); 172 173 err = iommu_register(iommu); 174 if (err) { 175 device_printf(dev, "Failed to register SMMU.\n"); 176 return (ENXIO); 177 } 178 179 OF_device_register_xref(OF_xref_from_node(node), dev); 180 181 return (0); 182 183 error: 184 if (bootverbose) { 185 device_printf(dev, 186 "Failed to attach. Error %d\n", err); 187 } 188 189 /* Failure so free resources. */ 190 smmu_detach(dev); 191 192 return (err); 193 } 194 195 static device_method_t smmu_fdt_methods[] = { 196 /* Device interface */ 197 DEVMETHOD(device_probe, smmu_fdt_probe), 198 DEVMETHOD(device_attach, smmu_fdt_attach), 199 DEVMETHOD_END 200 }; 201 202 DEFINE_CLASS_1(smmu, smmu_fdt_driver, smmu_fdt_methods, 203 sizeof(struct smmu_softc), smmu_driver); 204 205 static devclass_t smmu_fdt_devclass; 206 207 EARLY_DRIVER_MODULE(smmu, simplebus, smmu_fdt_driver, smmu_fdt_devclass, 208 0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE); 209