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