Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/riscv/iommu/iommu_frontend.c
288965 views
1
/*-
2
* SPDX-License-Identifier: BSD-2-Clause
3
*
4
* Copyright (c) 2026 Ruslan Bukin <[email protected]>
5
*
6
* Redistribution and use in source and binary forms, with or without
7
* modification, are permitted provided that the following conditions
8
* are met:
9
* 1. Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
* 2. Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in the
13
* documentation and/or other materials provided with the distribution.
14
*
15
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25
* SUCH DAMAGE.
26
*/
27
28
#include "opt_platform.h"
29
30
#include <sys/param.h>
31
#include <sys/bus.h>
32
#include <sys/kernel.h>
33
#include <sys/malloc.h>
34
#include <sys/memdesc.h>
35
#include <sys/tree.h>
36
#include <sys/taskqueue.h>
37
#include <sys/lock.h>
38
#include <sys/mutex.h>
39
#include <sys/sx.h>
40
#include <sys/sysctl.h>
41
#include <vm/vm.h>
42
43
#include <dev/pci/pcireg.h>
44
#include <dev/pci/pcivar.h>
45
#include <machine/bus.h>
46
#include <dev/iommu/busdma_iommu.h>
47
#include <machine/vmparam.h>
48
49
#ifdef FDT
50
#include <dev/fdt/fdt_common.h>
51
#include <dev/ofw/ofw_bus.h>
52
#include <dev/ofw/ofw_bus_subr.h>
53
#endif
54
55
#include "iommu_frontend.h"
56
#include "iommu_if.h"
57
58
static MALLOC_DEFINE(M_IOMMU, "IOMMU", "IOMMU framework");
59
60
#define IOMMU_LIST_LOCK() sx_xlock(&iommu_sx)
61
#define IOMMU_LIST_UNLOCK() sx_xunlock(&iommu_sx)
62
#define IOMMU_LIST_ASSERT_LOCKED() sx_assert(&iommu_sx, SA_XLOCKED)
63
64
#define dprintf(fmt, ...)
65
66
static struct sx iommu_sx;
67
68
struct iommu_entry {
69
struct iommu_unit *iommu;
70
LIST_ENTRY(iommu_entry) next;
71
};
72
static LIST_HEAD(, iommu_entry) iommu_list = LIST_HEAD_INITIALIZER(iommu_list);
73
74
static int
75
iommu_domain_unmap_buf(struct iommu_domain *iodom,
76
struct iommu_map_entry *entry, int flags)
77
{
78
struct iommu_unit *iommu;
79
int error;
80
81
iommu = iodom->iommu;
82
error = IOMMU_UNMAP(iommu->dev, iodom, entry->start, entry->end -
83
entry->start);
84
return (error);
85
}
86
87
static int
88
iommu_domain_map_buf(struct iommu_domain *iodom, struct iommu_map_entry *entry,
89
vm_page_t *ma, uint64_t eflags, int flags)
90
{
91
struct iommu_unit *iommu;
92
vm_prot_t prot;
93
vm_offset_t va;
94
int error;
95
96
dprintf("%s: base %lx, size %lx\n", __func__, base, size);
97
98
prot = 0;
99
if (eflags & IOMMU_MAP_ENTRY_READ)
100
prot |= VM_PROT_READ;
101
if (eflags & IOMMU_MAP_ENTRY_WRITE)
102
prot |= VM_PROT_WRITE;
103
104
va = entry->start;
105
iommu = iodom->iommu;
106
error = IOMMU_MAP(iommu->dev, iodom, va, ma, entry->end -
107
entry->start, prot);
108
return (error);
109
}
110
111
static const struct iommu_domain_map_ops domain_map_ops = {
112
.map = iommu_domain_map_buf,
113
.unmap = iommu_domain_unmap_buf,
114
};
115
116
static struct iommu_domain *
117
iommu_domain_alloc(struct iommu_unit *iommu)
118
{
119
struct iommu_domain *iodom;
120
121
iodom = IOMMU_DOMAIN_ALLOC(iommu->dev, iommu);
122
if (iodom == NULL)
123
return (NULL);
124
125
KASSERT(iodom->end != 0, ("domain end is not set"));
126
127
iommu_domain_init(iommu, iodom, &domain_map_ops);
128
iodom->iommu = iommu;
129
iommu_gas_init_domain(iodom);
130
131
return (iodom);
132
}
133
134
static int
135
iommu_domain_free(struct iommu_domain *iodom)
136
{
137
struct iommu_unit *iommu;
138
139
iommu = iodom->iommu;
140
141
IOMMU_LOCK(iommu);
142
143
if ((iodom->flags & IOMMU_DOMAIN_GAS_INITED) != 0) {
144
IOMMU_DOMAIN_LOCK(iodom);
145
iommu_gas_fini_domain(iodom);
146
IOMMU_DOMAIN_UNLOCK(iodom);
147
}
148
149
iommu_domain_fini(iodom);
150
151
IOMMU_DOMAIN_FREE(iommu->dev, iodom);
152
IOMMU_UNLOCK(iommu);
153
154
return (0);
155
}
156
157
static void
158
iommu_tag_init(struct iommu_domain *iodom, struct bus_dma_tag_iommu *t)
159
{
160
bus_addr_t maxaddr;
161
162
maxaddr = MIN(iodom->end, BUS_SPACE_MAXADDR);
163
164
t->common.impl = &bus_dma_iommu_impl;
165
t->common.alignment = 1;
166
t->common.boundary = 0;
167
t->common.lowaddr = maxaddr;
168
t->common.highaddr = maxaddr;
169
t->common.maxsize = maxaddr;
170
t->common.nsegments = BUS_SPACE_UNRESTRICTED;
171
t->common.maxsegsz = maxaddr;
172
}
173
174
static struct iommu_ctx *
175
iommu_ctx_alloc(device_t requester, struct iommu_domain *iodom, bool disabled)
176
{
177
struct iommu_unit *iommu;
178
struct iommu_ctx *ioctx;
179
180
iommu = iodom->iommu;
181
182
ioctx = IOMMU_CTX_ALLOC(iommu->dev, iodom, requester, disabled);
183
if (ioctx == NULL)
184
return (NULL);
185
186
ioctx->domain = iodom;
187
188
return (ioctx);
189
}
190
191
static int
192
iommu_ctx_init(device_t requester, struct iommu_ctx *ioctx)
193
{
194
struct bus_dma_tag_iommu *tag;
195
struct iommu_domain *iodom;
196
struct iommu_unit *iommu;
197
int error;
198
199
iodom = ioctx->domain;
200
iommu = iodom->iommu;
201
202
error = IOMMU_CTX_INIT(iommu->dev, ioctx);
203
if (error)
204
return (error);
205
206
tag = ioctx->tag = malloc(sizeof(struct bus_dma_tag_iommu),
207
M_DEVBUF, M_WAITOK | M_ZERO);
208
tag->owner = requester;
209
tag->ctx = ioctx;
210
tag->ctx->domain = iodom;
211
212
iommu_tag_init(iodom, tag);
213
214
return (error);
215
}
216
217
static struct iommu_unit *
218
iommu_lookup(device_t dev)
219
{
220
struct iommu_entry *entry;
221
struct iommu_unit *iommu;
222
223
IOMMU_LIST_LOCK();
224
LIST_FOREACH(entry, &iommu_list, next) {
225
iommu = entry->iommu;
226
if (iommu->dev == dev) {
227
IOMMU_LIST_UNLOCK();
228
return (iommu);
229
}
230
}
231
IOMMU_LIST_UNLOCK();
232
233
return (NULL);
234
}
235
236
#ifdef FDT
237
struct iommu_ctx *
238
iommu_get_ctx_ofw(device_t dev, int channel)
239
{
240
struct iommu_domain *iodom;
241
struct iommu_unit *iommu;
242
struct iommu_ctx *ioctx;
243
phandle_t node, parent;
244
device_t iommu_dev;
245
pcell_t *cells;
246
int niommus;
247
int ncells;
248
int error;
249
250
node = ofw_bus_get_node(dev);
251
if (node <= 0) {
252
device_printf(dev,
253
"%s called on not ofw based device.\n", __func__);
254
return (NULL);
255
}
256
257
error = ofw_bus_parse_xref_list_get_length(node,
258
"iommus", "#iommu-cells", &niommus);
259
if (error) {
260
device_printf(dev, "%s can't get iommu list.\n", __func__);
261
return (NULL);
262
}
263
264
if (niommus == 0) {
265
device_printf(dev, "%s iommu list is empty.\n", __func__);
266
return (NULL);
267
}
268
269
error = ofw_bus_parse_xref_list_alloc(node, "iommus", "#iommu-cells",
270
channel, &parent, &ncells, &cells);
271
if (error != 0) {
272
device_printf(dev, "%s can't get iommu device xref.\n",
273
__func__);
274
return (NULL);
275
}
276
277
iommu_dev = OF_device_from_xref(parent);
278
if (iommu_dev == NULL) {
279
device_printf(dev, "%s can't get iommu device.\n", __func__);
280
return (NULL);
281
}
282
283
iommu = iommu_lookup(iommu_dev);
284
if (iommu == NULL) {
285
device_printf(dev, "%s can't lookup iommu.\n", __func__);
286
return (NULL);
287
}
288
289
/*
290
* In our current configuration we have a domain per each ctx,
291
* so allocate a domain first.
292
*/
293
iodom = iommu_domain_alloc(iommu);
294
if (iodom == NULL) {
295
device_printf(dev, "%s can't allocate domain.\n", __func__);
296
return (NULL);
297
}
298
299
ioctx = iommu_ctx_alloc(dev, iodom, false);
300
if (ioctx == NULL) {
301
iommu_domain_free(iodom);
302
return (NULL);
303
}
304
305
ioctx->domain = iodom;
306
307
error = IOMMU_OFW_MD_DATA(iommu->dev, ioctx, cells, ncells);
308
if (error) {
309
device_printf(dev, "%s can't set MD data\n", __func__);
310
return (NULL);
311
}
312
313
error = iommu_ctx_init(dev, ioctx);
314
if (error) {
315
IOMMU_CTX_FREE(iommu->dev, ioctx);
316
iommu_domain_free(iodom);
317
return (NULL);
318
}
319
320
return (ioctx);
321
}
322
#endif
323
324
struct iommu_ctx *
325
iommu_get_ctx(struct iommu_unit *iommu, device_t requester,
326
uint16_t rid, bool disabled, bool rmrr)
327
{
328
struct iommu_domain *iodom;
329
struct iommu_ctx *ioctx;
330
int error;
331
332
IOMMU_LOCK(iommu);
333
ioctx = IOMMU_CTX_LOOKUP(iommu->dev, requester);
334
if (ioctx) {
335
IOMMU_UNLOCK(iommu);
336
return (ioctx);
337
}
338
IOMMU_UNLOCK(iommu);
339
340
/*
341
* In our current configuration we have a domain per each ctx.
342
* So allocate a domain first.
343
*/
344
iodom = iommu_domain_alloc(iommu);
345
if (iodom == NULL)
346
return (NULL);
347
348
ioctx = iommu_ctx_alloc(requester, iodom, disabled);
349
if (ioctx == NULL) {
350
iommu_domain_free(iodom);
351
return (NULL);
352
}
353
354
error = iommu_ctx_init(requester, ioctx);
355
if (error) {
356
IOMMU_CTX_FREE(iommu->dev, ioctx);
357
iommu_domain_free(iodom);
358
return (NULL);
359
}
360
361
return (ioctx);
362
}
363
364
void
365
iommu_free_ctx_locked(struct iommu_unit *iommu, struct iommu_ctx *ioctx)
366
{
367
struct iommu_domain *domain;
368
bool released;
369
int error;
370
371
IOMMU_ASSERT_LOCKED(iommu);
372
373
domain = ioctx->domain;
374
375
released = IOMMU_CTX_FREE(iommu->dev, ioctx);
376
IOMMU_UNLOCK(iommu);
377
378
if (released) {
379
/* Since we have a domain per each ctx, remove it too. */
380
error = iommu_domain_free(domain);
381
if (error)
382
device_printf(iommu->dev, "Could not free a domain\n");
383
}
384
}
385
386
static void
387
iommu_domain_free_entry(struct iommu_map_entry *entry, bool free)
388
{
389
iommu_gas_free_space(entry);
390
391
if (free)
392
iommu_gas_free_entry(entry);
393
else
394
entry->flags = 0;
395
}
396
397
void
398
iommu_domain_unload(struct iommu_domain *iodom,
399
struct iommu_map_entries_tailq *entries, bool cansleep)
400
{
401
struct iommu_map_entry *entry, *entry1;
402
int error __diagused;
403
404
TAILQ_FOREACH_SAFE(entry, entries, dmamap_link, entry1) {
405
KASSERT((entry->flags & IOMMU_MAP_ENTRY_MAP) != 0,
406
("not mapped entry %p %p", iodom, entry));
407
error = iodom->ops->unmap(iodom, entry,
408
cansleep ? IOMMU_PGF_WAITOK : 0);
409
KASSERT(error == 0, ("unmap %p error %d", iodom, error));
410
TAILQ_REMOVE(entries, entry, dmamap_link);
411
iommu_domain_free_entry(entry, true);
412
}
413
414
if (TAILQ_EMPTY(entries))
415
return;
416
417
panic("entries map is not empty");
418
}
419
420
int
421
iommu_register(struct iommu_unit *iommu)
422
{
423
struct iommu_entry *entry;
424
425
mtx_init(&iommu->lock, "IOMMU", NULL, MTX_DEF);
426
427
entry = malloc(sizeof(struct iommu_entry), M_IOMMU, M_WAITOK | M_ZERO);
428
entry->iommu = iommu;
429
430
IOMMU_LIST_LOCK();
431
LIST_INSERT_HEAD(&iommu_list, entry, next);
432
IOMMU_LIST_UNLOCK();
433
434
sysctl_ctx_init(&iommu->sysctl_ctx);
435
iommu_init_busdma(iommu);
436
437
return (0);
438
}
439
440
int
441
iommu_unregister(struct iommu_unit *iommu)
442
{
443
struct iommu_entry *entry, *tmp;
444
445
IOMMU_LIST_LOCK();
446
LIST_FOREACH_SAFE(entry, &iommu_list, next, tmp) {
447
if (entry->iommu == iommu) {
448
LIST_REMOVE(entry, next);
449
free(entry, M_IOMMU);
450
}
451
}
452
IOMMU_LIST_UNLOCK();
453
454
iommu_fini_busdma(iommu);
455
sysctl_ctx_free(&iommu->sysctl_ctx);
456
457
mtx_destroy(&iommu->lock);
458
459
return (0);
460
}
461
462
struct iommu_unit *
463
iommu_find(device_t dev, bool verbose)
464
{
465
struct iommu_entry *entry;
466
struct iommu_unit *iommu;
467
int error;
468
469
IOMMU_LIST_LOCK();
470
LIST_FOREACH(entry, &iommu_list, next) {
471
iommu = entry->iommu;
472
error = IOMMU_FIND(iommu->dev, dev);
473
if (error == 0) {
474
IOMMU_LIST_UNLOCK();
475
return (entry->iommu);
476
}
477
}
478
IOMMU_LIST_UNLOCK();
479
480
return (NULL);
481
}
482
483
void
484
iommu_unit_pre_instantiate_ctx(struct iommu_unit *unit)
485
{
486
}
487
488
void
489
iommu_domain_unload_entry(struct iommu_map_entry *entry, bool free,
490
bool cansleep __unused)
491
{
492
493
dprintf("%s\n", __func__);
494
495
iommu_domain_free_entry(entry, free);
496
}
497
498
static void
499
iommu_init(void)
500
{
501
502
sx_init(&iommu_sx, "IOMMU list");
503
}
504
505
SYSINIT(iommu, SI_SUB_DRIVERS, SI_ORDER_FIRST, iommu_init, NULL);
506
507