xref: /freebsd/sys/arm64/iommu/smmu_acpi.c (revision f7c32ed617858bcd22f8d1b03199099d50125721)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2019-2020 Ruslan Bukin <br@bsdpad.com>
5  *
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.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
19  *
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
30  * SUCH DAMAGE.
31  */
32 
33 #include "opt_acpi.h"
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include <sys/types.h>
39 #include <sys/systm.h>
40 #include <sys/bus.h>
41 #include <sys/bitstring.h>
42 #include <sys/kernel.h>
43 #include <sys/tree.h>
44 #include <sys/taskqueue.h>
45 #include <sys/malloc.h>
46 #include <sys/module.h>
47 #include <vm/vm.h>
48 #include <vm/pmap.h>
49 #include <contrib/dev/acpica/include/acpi.h>
50 #include <dev/acpica/acpivar.h>
51 #include <dev/pci/pcireg.h>
52 #include <dev/pci/pcivar.h>
53 #include <dev/iommu/iommu.h>
54 
55 #include <arm64/iommu/iommu.h>
56 
57 #include "smmuvar.h"
58 
59 #define	MEMORY_RESOURCE_SIZE	0x20000
60 #define	MAX_SMMU		8
61 
62 struct smmu_acpi_devinfo {
63 	struct resource_list	di_rl;
64 };
65 
66 struct iort_table_data {
67 	device_t parent;
68 	device_t dev;
69 	ACPI_IORT_SMMU_V3 *smmu[MAX_SMMU];
70 	int count;
71 };
72 
73 static void
74 iort_handler(ACPI_SUBTABLE_HEADER *entry, void *arg)
75 {
76 	struct iort_table_data *iort_data;
77 	ACPI_IORT_NODE *node;
78 	int i;
79 
80 	iort_data = (struct iort_table_data *)arg;
81 	i = iort_data->count;
82 
83 	switch(entry->Type) {
84 	case ACPI_IORT_NODE_SMMU_V3:
85 		if (i == MAX_SMMU) {
86 			printf("SMMUv3 found, but no space available.\n");
87 			break;
88 		}
89 
90 		if (iort_data->smmu[i] != NULL) {
91 			if (bootverbose)
92 				device_printf(iort_data->parent,
93 				    "smmu: Already have an SMMU table");
94 			break;
95 		}
96 		node = (ACPI_IORT_NODE *)entry;
97 		iort_data->smmu[i] = (ACPI_IORT_SMMU_V3 *)node->NodeData;
98 		iort_data->count++;
99 		break;
100 	default:
101 		break;
102 	}
103 }
104 
105 static void
106 smmu_acpi_identify(driver_t *driver, device_t parent)
107 {
108 	struct iort_table_data iort_data;
109 	ACPI_TABLE_IORT *iort;
110 	vm_paddr_t iort_pa;
111 	uintptr_t priv;
112 	device_t dev;
113 	int i;
114 
115 	iort_pa = acpi_find_table(ACPI_SIG_IORT);
116 	if (iort_pa == 0)
117 		return;
118 
119 	iort = acpi_map_table(iort_pa, ACPI_SIG_IORT);
120 	if (iort == NULL) {
121 		device_printf(parent, "smmu: Unable to map the IORT\n");
122 		return;
123 	}
124 
125 	iort_data.parent = parent;
126 	for (i = 0; i < MAX_SMMU; i++)
127 		iort_data.smmu[i] = NULL;
128 	iort_data.count = 0;
129 
130 	acpi_walk_subtables(iort + 1, (char *)iort + iort->Header.Length,
131 	    iort_handler, &iort_data);
132 	if (iort_data.count == 0) {
133 		device_printf(parent, "No SMMU found.\n");
134 		goto out;
135 	}
136 
137 	for (i = 0; i < iort_data.count; i++) {
138 		dev = BUS_ADD_CHILD(parent,
139 		    BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE, "smmu", -1);
140 		if (dev == NULL) {
141 			device_printf(parent, "add smmu child failed\n");
142 			goto out;
143 		}
144 
145 		/* Add the IORT data */
146 		BUS_SET_RESOURCE(parent, dev, SYS_RES_IRQ, 0,
147 		    iort_data.smmu[i]->EventGsiv, 1);
148 		BUS_SET_RESOURCE(parent, dev, SYS_RES_IRQ, 1,
149 		    iort_data.smmu[i]->SyncGsiv, 1);
150 		BUS_SET_RESOURCE(parent, dev, SYS_RES_IRQ, 2,
151 		    iort_data.smmu[i]->GerrGsiv, 1);
152 		BUS_SET_RESOURCE(parent, dev, SYS_RES_MEMORY, 0,
153 		    iort_data.smmu[i]->BaseAddress, MEMORY_RESOURCE_SIZE);
154 
155 		priv = iort_data.smmu[i]->Flags;
156 		priv <<= 32;
157 		priv |= iort_data.smmu[i]->Model;
158 
159 		acpi_set_private(dev, (void *)priv);
160 	}
161 
162 	iort_data.dev = dev;
163 
164 out:
165 	acpi_unmap_table(iort);
166 }
167 
168 static int
169 smmu_acpi_probe(device_t dev)
170 {
171 
172 	switch((uintptr_t)acpi_get_private(dev) & 0xffffffff) {
173 	case ACPI_IORT_SMMU_V3_GENERIC:
174 		/* Generic SMMUv3 */
175 		break;
176 	default:
177 		return (ENXIO);
178 	}
179 
180 	device_set_desc(dev, SMMU_DEVSTR);
181 
182 	return (BUS_PROBE_NOWILDCARD);
183 }
184 
185 static int
186 smmu_acpi_attach(device_t dev)
187 {
188 	struct smmu_softc *sc;
189 	struct smmu_unit *unit;
190 	struct iommu_unit *iommu;
191 	uintptr_t priv;
192 	int err;
193 
194 	sc = device_get_softc(dev);
195 	sc->dev = dev;
196 
197 	priv = (uintptr_t)acpi_get_private(dev);
198 	if ((priv >> 32) & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE)
199 		sc->features |= SMMU_FEATURE_COHERENCY;
200 
201 	if (bootverbose)
202 		device_printf(sc->dev, "%s: features %x\n",
203 		    __func__, sc->features);
204 
205 	err = smmu_attach(dev);
206 	if (err != 0)
207 		goto error;
208 
209 	unit = &sc->unit;
210 	unit->dev = dev;
211 
212 	iommu = &unit->iommu;
213 	iommu->dev = dev;
214 
215 	LIST_INIT(&unit->domain_list);
216 
217 	/* Use memory start address as an xref. */
218 	sc->xref = bus_get_resource_start(dev, SYS_RES_MEMORY, 0);
219 
220 	err = iommu_register(iommu);
221 	if (err) {
222 		device_printf(dev, "Failed to register SMMU.\n");
223 		return (ENXIO);
224 	}
225 
226 	return (0);
227 
228 error:
229 	if (bootverbose) {
230 		device_printf(dev,
231 		    "Failed to attach. Error %d\n", err);
232 	}
233 	/* Failure so free resources. */
234 	smmu_detach(dev);
235 
236 	return (err);
237 }
238 
239 static device_method_t smmu_acpi_methods[] = {
240 	/* Device interface */
241 	DEVMETHOD(device_identify,		smmu_acpi_identify),
242 	DEVMETHOD(device_probe,			smmu_acpi_probe),
243 	DEVMETHOD(device_attach,		smmu_acpi_attach),
244 
245 	/* End */
246 	DEVMETHOD_END
247 };
248 
249 DEFINE_CLASS_1(smmu, smmu_acpi_driver, smmu_acpi_methods,
250     sizeof(struct smmu_softc), smmu_driver);
251 
252 static devclass_t smmu_acpi_devclass;
253 
254 EARLY_DRIVER_MODULE(smmu, acpi, smmu_acpi_driver, smmu_acpi_devclass,
255     0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);
256