summaryrefslogtreecommitdiff
path: root/sys/dev/fdt/fdt_iommu.c
blob: 5e061587754aefec2688f986f1ed3c4a7eb7ef24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* $NetBSD: fdt_iommu.c,v 1.1 2021/09/04 12:34:39 jmcneill Exp $ */

/*-
 * Copyright (c) 2021 Jared McNeill <jmcneill@invisible.ca>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: fdt_iommu.c,v 1.1 2021/09/04 12:34:39 jmcneill Exp $");

#include <sys/param.h>
#include <sys/bus.h>
#include <sys/kmem.h>
#include <sys/queue.h>

#include <libfdt.h>
#include <dev/fdt/fdtvar.h>

struct fdtbus_iommu {
	device_t			iommu_dev;
	int				iommu_phandle;
	const struct fdtbus_iommu_func	*iommu_funcs;
	u_int				iommu_cells;
	LIST_ENTRY(fdtbus_iommu)	iommu_next;
};

static LIST_HEAD(, fdtbus_iommu) fdtbus_iommus =
    LIST_HEAD_INITIALIZER(fdtbus_iommus);

/*
 * fdtbus_get_iommu --
 *
 *	Return the iommu registered with the specified node, or NULL if
 *	not found.
 */
static struct fdtbus_iommu *
fdtbus_get_iommu(int phandle)
{
	struct fdtbus_iommu *iommu;

	LIST_FOREACH(iommu, &fdtbus_iommus, iommu_next) {
		if (iommu->iommu_phandle == phandle) {
			return iommu;
		}
	}

	return NULL;
}

/*
 * fdtbus_register_iommu --
 *
 *	Register an IOMMU on the specified node.
 */
int
fdtbus_register_iommu(device_t dev, int phandle,
    const struct fdtbus_iommu_func *funcs)
{
	struct fdtbus_iommu *iommu;
	u_int cells;

	if (funcs == NULL || funcs->map == NULL) {
		return EINVAL;
	}

	if (of_getprop_uint32(phandle, "#iommu-cells", &cells) != 0) {
		return ENXIO;
	}

	if (fdtbus_get_iommu(phandle) != NULL) {
		return EEXIST;
	}

	iommu = kmem_alloc(sizeof(*iommu), KM_SLEEP);
	iommu->iommu_dev = dev;
	iommu->iommu_phandle = phandle;
	iommu->iommu_funcs = funcs;
	iommu->iommu_cells = cells;

	LIST_INSERT_HEAD(&fdtbus_iommus, iommu, iommu_next);

	return 0;
}

/*
 * fdtbus_iommu_map --
 *
 *	Get a bus dma tag that is translated by any iommus specified by
 *	the device tree. The `index` property refers to the N-th item
 *	in the list of IOMMUs specified in the "iommus" property. If an
 *	IOMMU is not found, the original bus dma tag is returned.
 */
bus_dma_tag_t
fdtbus_iommu_map(int phandle, u_int index, bus_dma_tag_t dmat)
{
	struct fdtbus_iommu *iommu;
	const u_int *p;
	u_int n, cells;
	int len, resid;

	p = fdtbus_get_prop(phandle, "iommus", &len);
	if (p == NULL) {
		return dmat;
	}

	for (n = 0, resid = len; resid > 0; n++) {
		const int iommu_phandle =
		    fdtbus_get_phandle_from_native(be32toh(p[0]));
		if (of_getprop_uint32(iommu_phandle, "#iommu-cells", &cells)) {
			break;
		}
		if (n == index) {
			iommu = fdtbus_get_iommu(iommu_phandle);
			if (iommu == NULL) {
				break;
			}
			return iommu->iommu_funcs->map(iommu->iommu_dev,
			    cells > 0 ? &p[1] : NULL, dmat);
		}
		resid -= (cells + 1) * 4;
		p += cells + 1;
	}

	return dmat;
}

/*
 * fdtbus_iommu_map_pci --
 *
 *	Get a bus dma tag that is translated by any iommus specified by
 *	the device tree for PCI devices. The `rid` param is the requester
 *	ID. Returns a (maybe translated) dma tag.
 */
bus_dma_tag_t
fdtbus_iommu_map_pci(int phandle, uint32_t rid, bus_dma_tag_t dmat)
{
	struct fdtbus_iommu *iommu;
	uint32_t map_mask;
	const u_int *p;
	u_int cells;
	int len;

	len = 0;
	p = fdtbus_get_prop(phandle, "iommu-map", &len);
	KASSERT(p != NULL || len == 0);

	if (of_getprop_uint32(phandle, "iommu-map-mask", &map_mask) == 0) {
		rid &= map_mask;
	}

	while (len >= 4) {
		const u_int rid_base = be32toh(*p++);
		const int iommu_phandle =
		    fdtbus_get_phandle_from_native(be32toh(*p++));
		const u_int iommu_base = be32toh(*p++);
		const u_int length = be32toh(*p++);
		len -= 4;

		if (iommu_phandle <= 0) {
			continue;
		}
		if (of_getprop_uint32(iommu_phandle, "#iommu-cells", &cells)) {
			continue;
		}
		if (cells != 1) {
			/*
			 * The pci-iommu bindings expect iommu references with
			 * exactly one specifier cell.
			 */
			continue;
		}
		iommu = fdtbus_get_iommu(iommu_phandle);
		if (iommu == NULL) {
			continue;
		}

		if (rid >= rid_base && rid < rid_base + length) {
			const uint32_t sid = rid - rid_base + iommu_base;
			return iommu->iommu_funcs->map(iommu->iommu_dev,
			    &sid, dmat);
		}
	}

	return dmat;
}