xref: /freebsd/sys/arm64/iommu/smmu_fdt.c (revision 315ee00f)
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 #include <sys/types.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/bitstring.h>
36 #include <sys/kernel.h>
37 #include <sys/rman.h>
38 #include <sys/sysctl.h>
39 #include <sys/tree.h>
40 #include <sys/taskqueue.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <vm/vm.h>
44 #include <vm/pmap.h>
45 #include <dev/pci/pcireg.h>
46 #include <dev/pci/pcivar.h>
47 #include <dev/iommu/iommu.h>
48 
49 #include <dev/ofw/ofw_bus.h>
50 #include <dev/ofw/ofw_bus_subr.h>
51 
52 #include <dev/iommu/iommu.h>
53 
54 #include <arm64/iommu/iommu.h>
55 
56 #include "smmuvar.h"
57 
58 static struct ofw_compat_data compat_data[] = {
59 	{ "arm,smmu-v3",			1 },
60 	{ NULL,					0 }
61 };
62 
63 static int
64 smmu_fdt_probe(device_t dev)
65 {
66 	if (!ofw_bus_status_okay(dev))
67 		return (ENXIO);
68 
69 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
70 		return (ENXIO);
71 
72 	device_set_desc(dev, "ARM System MMU (SMMU) v3");
73 
74 	return (BUS_PROBE_DEFAULT);
75 }
76 
77 static int
78 smmu_fdt_attach(device_t dev)
79 {
80 	struct smmu_softc *sc;
81 	struct smmu_unit *unit;
82 	struct iommu_unit *iommu;
83 	phandle_t node;
84 	int err;
85 	int rid;
86 
87 	sc = device_get_softc(dev);
88 	sc->dev = dev;
89 
90 	node = ofw_bus_get_node(dev);
91 
92 	rid = 0;
93 	sc->res[0] = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
94 	    RF_ACTIVE);
95 	if (sc->res[0] == NULL) {
96 		device_printf(dev, "Can't allocate memory resource.\n");
97 		err = ENXIO;
98 		goto error;
99 	}
100 
101 	/*
102 	 * Interrupt lines are "eventq", "priq", "cmdq-sync", "gerror".
103 	 */
104 
105 	err = ofw_bus_find_string_index(node, "interrupt-names", "eventq",
106 	    &rid);
107 	if (err != 0) {
108 		device_printf(dev, "Can't get eventq IRQ.\n");
109 		err = ENXIO;
110 		goto error;
111 	}
112 
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");
116 		err = ENXIO;
117 		goto error;
118 	}
119 
120 	/*
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
123 	 * hardware.
124 	 */
125 
126 	err = ofw_bus_find_string_index(node, "interrupt-names", "cmdq-sync",
127 	    &rid);
128 	if (err != 0) {
129 		device_printf(dev, "Can't get cmdq-sync IRQ.\n");
130 		err = ENXIO;
131 		goto error;
132 	}
133 
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");
137 		err = ENXIO;
138 		goto error;
139 	}
140 
141 	err = ofw_bus_find_string_index(node, "interrupt-names", "gerror",
142 	    &rid);
143 	if (err != 0) {
144 		device_printf(dev, "Can't get gerror IRQ.\n");
145 		err = ENXIO;
146 		goto error;
147 	}
148 
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");
152 		err = ENXIO;
153 		goto error;
154 	}
155 
156 	err = smmu_attach(dev);
157 	if (err != 0)
158 		goto error;
159 
160 	unit = &sc->unit;
161 	unit->dev = dev;
162 
163 	iommu = &unit->iommu;
164 	iommu->dev = dev;
165 
166 	LIST_INIT(&unit->domain_list);
167 
168 	/* Use memory start address as an xref. */
169 	sc->xref = OF_xref_from_node(node);
170 
171 	err = iommu_register(iommu);
172 	if (err) {
173 		device_printf(dev, "Failed to register SMMU.\n");
174 		return (ENXIO);
175 	}
176 
177 	OF_device_register_xref(sc->xref, dev);
178 
179 	return (0);
180 
181 error:
182 	if (bootverbose) {
183 		device_printf(dev,
184 		    "Failed to attach. Error %d\n", err);
185 	}
186 
187 	/* Failure so free resources. */
188 	smmu_detach(dev);
189 
190 	return (err);
191 }
192 
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),
197 	DEVMETHOD_END
198 };
199 
200 DEFINE_CLASS_1(smmu, smmu_fdt_driver, smmu_fdt_methods,
201     sizeof(struct smmu_softc), smmu_driver);
202 
203 EARLY_DRIVER_MODULE(smmu, simplebus, smmu_fdt_driver, 0, 0,
204     BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);
205