xref: /freebsd/sys/arm64/iommu/smmu.c (revision 8165650389ba2d0a68cea6902ac3750055cad9da)
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 /*
34  * Hardware overview.
35  *
36  * An incoming transaction from a peripheral device has an address, size,
37  * attributes and StreamID.
38  *
39  * In case of PCI-based devices, StreamID is a PCI rid.
40  *
41  * The StreamID is used to select a Stream Table Entry (STE) in a Stream table,
42  * which contains per-device configuration.
43  *
44  * Stream table is a linear or 2-level walk table (this driver supports both).
45  * Note that a linear table could occupy 1GB or more of memory depending on
46  * sid_bits value.
47  *
48  * STE is used to locate a Context Descriptor, which is a struct in memory
49  * that describes stages of translation, translation table type, pointer to
50  * level 0 of page tables, ASID, etc.
51  *
52  * Hardware supports two stages of translation: Stage1 (S1) and Stage2 (S2):
53  *  o S1 is used for the host machine traffic translation
54  *  o S2 is for a hypervisor
55  *
56  * This driver enables S1 stage with standard AArch64 page tables.
57  *
58  * Note that SMMU does not share TLB with a main CPU.
59  * Command queue is used by this driver to Invalidate SMMU TLB, STE cache.
60  *
61  * An arm64 SoC could have more than one SMMU instance.
62  * ACPI IORT table describes which SMMU unit is assigned for a particular
63  * peripheral device.
64  *
65  * Queues.
66  *
67  * Register interface and Memory-based circular buffer queues are used
68  * to interface SMMU.
69  *
70  * These are a Command queue for commands to send to the SMMU and an Event
71  * queue for event/fault reports from the SMMU. Optionally PRI queue is
72  * designed for PCIe page requests reception.
73  *
74  * Note that not every hardware supports PRI services. For instance they were
75  * not found in Neoverse N1 SDP machine.
76  * (This drivers does not implement PRI queue.)
77  *
78  * All SMMU queues are arranged as circular buffers in memory. They are used
79  * in a producer-consumer fashion so that an output queue contains data
80  * produced by the SMMU and consumed by software.
81  * An input queue contains data produced by software, consumed by the SMMU.
82  *
83  * Interrupts.
84  *
85  * Interrupts are not required by this driver for normal operation.
86  * The standard wired interrupt is only triggered when an event comes from
87  * the SMMU, which is only in a case of errors (e.g. translation fault).
88  */
89 
90 #include "opt_platform.h"
91 #include "opt_acpi.h"
92 
93 #include <sys/param.h>
94 #include <sys/bitstring.h>
95 #include <sys/bus.h>
96 #include <sys/kernel.h>
97 #include <sys/malloc.h>
98 #include <sys/mutex.h>
99 #include <sys/rman.h>
100 #include <sys/lock.h>
101 #include <sys/sysctl.h>
102 #include <sys/tree.h>
103 #include <sys/taskqueue.h>
104 #include <vm/vm.h>
105 #include <vm/vm_page.h>
106 #ifdef DEV_ACPI
107 #include <contrib/dev/acpica/include/acpi.h>
108 #include <dev/acpica/acpivar.h>
109 #endif
110 #include <dev/pci/pcireg.h>
111 #include <dev/pci/pcivar.h>
112 #include <dev/iommu/iommu.h>
113 #include <arm64/iommu/iommu_pmap.h>
114 
115 #include <machine/bus.h>
116 
117 #ifdef FDT
118 #include <dev/fdt/fdt_common.h>
119 #include <dev/ofw/ofw_bus.h>
120 #include <dev/ofw/ofw_bus_subr.h>
121 #endif
122 
123 #include "iommu.h"
124 #include "iommu_if.h"
125 
126 #include "smmureg.h"
127 #include "smmuvar.h"
128 
129 #define	STRTAB_L1_SZ_SHIFT	20
130 #define	STRTAB_SPLIT		8
131 
132 #define	STRTAB_L1_DESC_L2PTR_M	(0x3fffffffffff << 6)
133 #define	STRTAB_L1_DESC_DWORDS	1
134 
135 #define	STRTAB_STE_DWORDS	8
136 
137 #define	CMDQ_ENTRY_DWORDS	2
138 #define	EVTQ_ENTRY_DWORDS	4
139 #define	PRIQ_ENTRY_DWORDS	2
140 
141 #define	CD_DWORDS		8
142 
143 #define	Q_WRP(q, p)		((p) & (1 << (q)->size_log2))
144 #define	Q_IDX(q, p)		((p) & ((1 << (q)->size_log2) - 1))
145 #define	Q_OVF(p)		((p) & (1 << 31)) /* Event queue overflowed */
146 
147 #define	SMMU_Q_ALIGN		(64 * 1024)
148 
149 #define		MAXADDR_48BIT	0xFFFFFFFFFFFFUL
150 #define		MAXADDR_52BIT	0xFFFFFFFFFFFFFUL
151 
152 static struct resource_spec smmu_spec[] = {
153 	{ SYS_RES_MEMORY, 0, RF_ACTIVE },
154 	{ SYS_RES_IRQ, 0, RF_ACTIVE },
155 	{ SYS_RES_IRQ, 1, RF_ACTIVE | RF_OPTIONAL },
156 	{ SYS_RES_IRQ, 2, RF_ACTIVE },
157 	{ SYS_RES_IRQ, 3, RF_ACTIVE },
158 	RESOURCE_SPEC_END
159 };
160 
161 MALLOC_DEFINE(M_SMMU, "SMMU", SMMU_DEVSTR);
162 
163 #define	dprintf(fmt, ...)
164 
165 struct smmu_event {
166 	int ident;
167 	char *str;
168 	char *msg;
169 };
170 
171 static struct smmu_event events[] = {
172 	{ 0x01, "F_UUT",
173 		"Unsupported Upstream Transaction."},
174 	{ 0x02, "C_BAD_STREAMID",
175 		"Transaction StreamID out of range."},
176 	{ 0x03, "F_STE_FETCH",
177 		"Fetch of STE caused external abort."},
178 	{ 0x04, "C_BAD_STE",
179 		"Used STE invalid."},
180 	{ 0x05, "F_BAD_ATS_TREQ",
181 		"Address Translation Request disallowed for a StreamID "
182 		"and a PCIe ATS Translation Request received."},
183 	{ 0x06, "F_STREAM_DISABLED",
184 		"The STE of a transaction marks non-substream transactions "
185 		"disabled."},
186 	{ 0x07, "F_TRANSL_FORBIDDEN",
187 		"An incoming PCIe transaction is marked Translated but "
188 		"SMMU bypass is disallowed for this StreamID."},
189 	{ 0x08, "C_BAD_SUBSTREAMID",
190 		"Incoming SubstreamID present, but configuration is invalid."},
191 	{ 0x09, "F_CD_FETCH",
192 		"Fetch of CD caused external abort."},
193 	{ 0x0a, "C_BAD_CD",
194 		"Fetched CD invalid."},
195 	{ 0x0b, "F_WALK_EABT",
196 		"An external abort occurred fetching (or updating) "
197 		"a translation table descriptor."},
198 	{ 0x10, "F_TRANSLATION",
199 		"Translation fault."},
200 	{ 0x11, "F_ADDR_SIZE",
201 		"Address Size fault."},
202 	{ 0x12, "F_ACCESS",
203 		"Access flag fault due to AF == 0 in a page or block TTD."},
204 	{ 0x13, "F_PERMISSION",
205 		"Permission fault occurred on page access."},
206 	{ 0x20, "F_TLB_CONFLICT",
207 		"A TLB conflict occurred because of the transaction."},
208 	{ 0x21, "F_CFG_CONFLICT",
209 		"A configuration cache conflict occurred due to "
210 		"the transaction."},
211 	{ 0x24, "E_PAGE_REQUEST",
212 		"Speculative page request hint."},
213 	{ 0x25, "F_VMS_FETCH",
214 		"Fetch of VMS caused external abort."},
215 	{ 0, NULL, NULL },
216 };
217 
218 static int
219 smmu_q_has_space(struct smmu_queue *q)
220 {
221 
222 	/*
223 	 * See 6.3.27 SMMU_CMDQ_PROD
224 	 *
225 	 * There is space in the queue for additional commands if:
226 	 *  SMMU_CMDQ_CONS.RD != SMMU_CMDQ_PROD.WR ||
227 	 *  SMMU_CMDQ_CONS.RD_WRAP == SMMU_CMDQ_PROD.WR_WRAP
228 	 */
229 
230 	if (Q_IDX(q, q->lc.cons) != Q_IDX(q, q->lc.prod) ||
231 	    Q_WRP(q, q->lc.cons) == Q_WRP(q, q->lc.prod))
232 		return (1);
233 
234 	return (0);
235 }
236 
237 static int
238 smmu_q_empty(struct smmu_queue *q)
239 {
240 
241 	if (Q_IDX(q, q->lc.cons) == Q_IDX(q, q->lc.prod) &&
242 	    Q_WRP(q, q->lc.cons) == Q_WRP(q, q->lc.prod))
243 		return (1);
244 
245 	return (0);
246 }
247 
248 static int __unused
249 smmu_q_consumed(struct smmu_queue *q, uint32_t prod)
250 {
251 
252 	if ((Q_WRP(q, q->lc.cons) == Q_WRP(q, prod)) &&
253 	    (Q_IDX(q, q->lc.cons) >= Q_IDX(q, prod)))
254 		return (1);
255 
256 	if ((Q_WRP(q, q->lc.cons) != Q_WRP(q, prod)) &&
257 	    (Q_IDX(q, q->lc.cons) <= Q_IDX(q, prod)))
258 		return (1);
259 
260 	return (0);
261 }
262 
263 static uint32_t
264 smmu_q_inc_cons(struct smmu_queue *q)
265 {
266 	uint32_t cons;
267 	uint32_t val;
268 
269 	cons = (Q_WRP(q, q->lc.cons) | Q_IDX(q, q->lc.cons)) + 1;
270 	val = (Q_OVF(q->lc.cons) | Q_WRP(q, cons) | Q_IDX(q, cons));
271 
272 	return (val);
273 }
274 
275 static uint32_t
276 smmu_q_inc_prod(struct smmu_queue *q)
277 {
278 	uint32_t prod;
279 	uint32_t val;
280 
281 	prod = (Q_WRP(q, q->lc.prod) | Q_IDX(q, q->lc.prod)) + 1;
282 	val = (Q_OVF(q->lc.prod) | Q_WRP(q, prod) | Q_IDX(q, prod));
283 
284 	return (val);
285 }
286 
287 static int
288 smmu_write_ack(struct smmu_softc *sc, uint32_t reg,
289     uint32_t reg_ack, uint32_t val)
290 {
291 	uint32_t v;
292 	int timeout;
293 
294 	timeout = 100000;
295 
296 	bus_write_4(sc->res[0], reg, val);
297 
298 	do {
299 		v = bus_read_4(sc->res[0], reg_ack);
300 		if (v == val)
301 			break;
302 	} while (timeout--);
303 
304 	if (timeout <= 0) {
305 		device_printf(sc->dev, "Failed to write reg.\n");
306 		return (-1);
307 	}
308 
309 	return (0);
310 }
311 
312 static int
313 smmu_init_queue(struct smmu_softc *sc, struct smmu_queue *q,
314     uint32_t prod_off, uint32_t cons_off, uint32_t dwords)
315 {
316 	int sz;
317 
318 	sz = (1 << q->size_log2) * dwords * 8;
319 
320 	/* Set up the command circular buffer */
321 	q->vaddr = contigmalloc(sz, M_SMMU,
322 	    M_WAITOK | M_ZERO, 0, (1ul << 48) - 1, SMMU_Q_ALIGN, 0);
323 	if (q->vaddr == NULL) {
324 		device_printf(sc->dev, "failed to allocate %d bytes\n", sz);
325 		return (-1);
326 	}
327 
328 	q->prod_off = prod_off;
329 	q->cons_off = cons_off;
330 	q->paddr = vtophys(q->vaddr);
331 
332 	q->base = CMDQ_BASE_RA | EVENTQ_BASE_WA | PRIQ_BASE_WA;
333 	q->base |= q->paddr & Q_BASE_ADDR_M;
334 	q->base |= q->size_log2 << Q_LOG2SIZE_S;
335 
336 	return (0);
337 }
338 
339 static int
340 smmu_init_queues(struct smmu_softc *sc)
341 {
342 	int err;
343 
344 	/* Command queue. */
345 	err = smmu_init_queue(sc, &sc->cmdq,
346 	    SMMU_CMDQ_PROD, SMMU_CMDQ_CONS, CMDQ_ENTRY_DWORDS);
347 	if (err)
348 		return (ENXIO);
349 
350 	/* Event queue. */
351 	err = smmu_init_queue(sc, &sc->evtq,
352 	    SMMU_EVENTQ_PROD, SMMU_EVENTQ_CONS, EVTQ_ENTRY_DWORDS);
353 	if (err)
354 		return (ENXIO);
355 
356 	if (!(sc->features & SMMU_FEATURE_PRI))
357 		return (0);
358 
359 	/* PRI queue. */
360 	err = smmu_init_queue(sc, &sc->priq,
361 	    SMMU_PRIQ_PROD, SMMU_PRIQ_CONS, PRIQ_ENTRY_DWORDS);
362 	if (err)
363 		return (ENXIO);
364 
365 	return (0);
366 }
367 
368 /*
369  * Dump 2LVL or linear STE.
370  */
371 static void
372 smmu_dump_ste(struct smmu_softc *sc, int sid)
373 {
374 	struct smmu_strtab *strtab;
375 	struct l1_desc *l1_desc;
376 	uint64_t *ste, *l1;
377 	int i;
378 
379 	strtab = &sc->strtab;
380 
381 	if (sc->features & SMMU_FEATURE_2_LVL_STREAM_TABLE) {
382 		i = sid >> STRTAB_SPLIT;
383 		l1 = (void *)((uint64_t)strtab->vaddr +
384 		    STRTAB_L1_DESC_DWORDS * 8 * i);
385 		device_printf(sc->dev, "L1 ste == %lx\n", l1[0]);
386 
387 		l1_desc = &strtab->l1[i];
388 		ste = l1_desc->va;
389 		if (ste == NULL) /* L2 is not initialized */
390 			return;
391 	} else {
392 		ste = (void *)((uint64_t)strtab->vaddr +
393 		    sid * (STRTAB_STE_DWORDS << 3));
394 	}
395 
396 	/* Dump L2 or linear STE. */
397 	for (i = 0; i < STRTAB_STE_DWORDS; i++)
398 		device_printf(sc->dev, "ste[%d] == %lx\n", i, ste[i]);
399 }
400 
401 static void __unused
402 smmu_dump_cd(struct smmu_softc *sc, struct smmu_cd *cd)
403 {
404 	uint64_t *vaddr;
405 	int i;
406 
407 	device_printf(sc->dev, "%s\n", __func__);
408 
409 	vaddr = cd->vaddr;
410 	for (i = 0; i < CD_DWORDS; i++)
411 		device_printf(sc->dev, "cd[%d] == %lx\n", i, vaddr[i]);
412 }
413 
414 static void
415 smmu_evtq_dequeue(struct smmu_softc *sc, uint32_t *evt)
416 {
417 	struct smmu_queue *evtq;
418 	void *entry_addr;
419 
420 	evtq = &sc->evtq;
421 
422 	evtq->lc.val = bus_read_8(sc->res[0], evtq->prod_off);
423 	entry_addr = (void *)((uint64_t)evtq->vaddr +
424 	    evtq->lc.cons * EVTQ_ENTRY_DWORDS * 8);
425 	memcpy(evt, entry_addr, EVTQ_ENTRY_DWORDS * 8);
426 	evtq->lc.cons = smmu_q_inc_cons(evtq);
427 	bus_write_4(sc->res[0], evtq->cons_off, evtq->lc.cons);
428 }
429 
430 static void
431 smmu_print_event(struct smmu_softc *sc, uint32_t *evt)
432 {
433 	struct smmu_event *ev;
434 	uintptr_t input_addr;
435 	uint8_t event_id;
436 	device_t dev;
437 	int sid;
438 	int i;
439 
440 	dev = sc->dev;
441 
442 	ev = NULL;
443 	event_id = evt[0] & 0xff;
444 	for (i = 0; events[i].ident != 0; i++) {
445 		if (events[i].ident == event_id) {
446 			ev = &events[i];
447 			break;
448 		}
449 	}
450 
451 	sid = evt[1];
452 	input_addr = evt[5];
453 	input_addr <<= 32;
454 	input_addr |= evt[4];
455 
456 	if (smmu_quirks_check(dev, sid, event_id, input_addr)) {
457 		/* The event is known. Don't print anything. */
458 		return;
459 	}
460 
461 	if (ev) {
462 		device_printf(sc->dev,
463 		    "Event %s (%s) received.\n", ev->str, ev->msg);
464 	} else
465 		device_printf(sc->dev, "Event 0x%x received\n", event_id);
466 
467 	device_printf(sc->dev, "SID %x, Input Address: %jx\n",
468 	    sid, input_addr);
469 
470 	for (i = 0; i < 8; i++)
471 		device_printf(sc->dev, "evt[%d] %x\n", i, evt[i]);
472 
473 	smmu_dump_ste(sc, sid);
474 }
475 
476 static void
477 make_cmd(struct smmu_softc *sc, uint64_t *cmd,
478     struct smmu_cmdq_entry *entry)
479 {
480 
481 	memset(cmd, 0, CMDQ_ENTRY_DWORDS * 8);
482 	cmd[0] = entry->opcode << CMD_QUEUE_OPCODE_S;
483 
484 	switch (entry->opcode) {
485 	case CMD_TLBI_NH_VA:
486 		cmd[0] |= (uint64_t)entry->tlbi.asid << TLBI_0_ASID_S;
487 		cmd[1] = entry->tlbi.addr & TLBI_1_ADDR_M;
488 		if (entry->tlbi.leaf) {
489 			/*
490 			 * Leaf flag means that only cached entries
491 			 * for the last level of translation table walk
492 			 * are required to be invalidated.
493 			 */
494 			cmd[1] |= TLBI_1_LEAF;
495 		}
496 		break;
497 	case CMD_TLBI_NH_ASID:
498 		cmd[0] |= (uint64_t)entry->tlbi.asid << TLBI_0_ASID_S;
499 		break;
500 	case CMD_TLBI_NSNH_ALL:
501 	case CMD_TLBI_NH_ALL:
502 	case CMD_TLBI_EL2_ALL:
503 		break;
504 	case CMD_CFGI_CD:
505 		cmd[0] |= ((uint64_t)entry->cfgi.ssid << CFGI_0_SSID_S);
506 		/* FALLTROUGH */
507 	case CMD_CFGI_STE:
508 		cmd[0] |= ((uint64_t)entry->cfgi.sid << CFGI_0_STE_SID_S);
509 		cmd[1] |= ((uint64_t)entry->cfgi.leaf << CFGI_1_LEAF_S);
510 		break;
511 	case CMD_CFGI_STE_RANGE:
512 		cmd[1] = (31 << CFGI_1_STE_RANGE_S);
513 		break;
514 	case CMD_SYNC:
515 		cmd[0] |= SYNC_0_MSH_IS | SYNC_0_MSIATTR_OIWB;
516 		if (entry->sync.msiaddr) {
517 			cmd[0] |= SYNC_0_CS_SIG_IRQ;
518 			cmd[1] |= (entry->sync.msiaddr & SYNC_1_MSIADDRESS_M);
519 		} else
520 			cmd[0] |= SYNC_0_CS_SIG_SEV;
521 		break;
522 	case CMD_PREFETCH_CONFIG:
523 		cmd[0] |= ((uint64_t)entry->prefetch.sid << PREFETCH_0_SID_S);
524 		break;
525 	};
526 }
527 
528 static void
529 smmu_cmdq_enqueue_cmd(struct smmu_softc *sc, struct smmu_cmdq_entry *entry)
530 {
531 	uint64_t cmd[CMDQ_ENTRY_DWORDS];
532 	struct smmu_queue *cmdq;
533 	void *entry_addr;
534 
535 	cmdq = &sc->cmdq;
536 
537 	make_cmd(sc, cmd, entry);
538 
539 	SMMU_LOCK(sc);
540 
541 	/* Ensure that a space is available. */
542 	do {
543 		cmdq->lc.cons = bus_read_4(sc->res[0], cmdq->cons_off);
544 	} while (smmu_q_has_space(cmdq) == 0);
545 
546 	/* Write the command to the current prod entry. */
547 	entry_addr = (void *)((uint64_t)cmdq->vaddr +
548 	    Q_IDX(cmdq, cmdq->lc.prod) * CMDQ_ENTRY_DWORDS * 8);
549 	memcpy(entry_addr, cmd, CMDQ_ENTRY_DWORDS * 8);
550 
551 	/* Increment prod index. */
552 	cmdq->lc.prod = smmu_q_inc_prod(cmdq);
553 	bus_write_4(sc->res[0], cmdq->prod_off, cmdq->lc.prod);
554 
555 	SMMU_UNLOCK(sc);
556 }
557 
558 static void __unused
559 smmu_poll_until_consumed(struct smmu_softc *sc, struct smmu_queue *q)
560 {
561 
562 	while (1) {
563 		q->lc.val = bus_read_8(sc->res[0], q->prod_off);
564 		if (smmu_q_empty(q))
565 			break;
566 		cpu_spinwait();
567 	}
568 }
569 
570 static int
571 smmu_sync(struct smmu_softc *sc)
572 {
573 	struct smmu_cmdq_entry cmd;
574 	struct smmu_queue *q;
575 	uint32_t *base;
576 	int timeout;
577 	int prod;
578 
579 	q = &sc->cmdq;
580 	prod = q->lc.prod;
581 
582 	/* Enqueue sync command. */
583 	cmd.opcode = CMD_SYNC;
584 	cmd.sync.msiaddr = q->paddr + Q_IDX(q, prod) * CMDQ_ENTRY_DWORDS * 8;
585 	smmu_cmdq_enqueue_cmd(sc, &cmd);
586 
587 	/* Wait for the sync completion. */
588 	base = (void *)((uint64_t)q->vaddr +
589 	    Q_IDX(q, prod) * CMDQ_ENTRY_DWORDS * 8);
590 
591 	/*
592 	 * It takes around 200 loops (6 instructions each)
593 	 * on Neoverse N1 to complete the sync.
594 	 */
595 	timeout = 10000;
596 
597 	do {
598 		if (*base == 0) {
599 			/* MSI write completed. */
600 			break;
601 		}
602 		cpu_spinwait();
603 	} while (timeout--);
604 
605 	if (timeout < 0)
606 		device_printf(sc->dev, "Failed to sync\n");
607 
608 	return (0);
609 }
610 
611 static int
612 smmu_sync_cd(struct smmu_softc *sc, int sid, int ssid, bool leaf)
613 {
614 	struct smmu_cmdq_entry cmd;
615 
616 	cmd.opcode = CMD_CFGI_CD;
617 	cmd.cfgi.sid = sid;
618 	cmd.cfgi.ssid = ssid;
619 	cmd.cfgi.leaf = leaf;
620 	smmu_cmdq_enqueue_cmd(sc, &cmd);
621 
622 	return (0);
623 }
624 
625 static void
626 smmu_invalidate_all_sid(struct smmu_softc *sc)
627 {
628 	struct smmu_cmdq_entry cmd;
629 
630 	/* Invalidate cached config */
631 	cmd.opcode = CMD_CFGI_STE_RANGE;
632 	smmu_cmdq_enqueue_cmd(sc, &cmd);
633 	smmu_sync(sc);
634 }
635 
636 static void
637 smmu_tlbi_all(struct smmu_softc *sc)
638 {
639 	struct smmu_cmdq_entry cmd;
640 
641 	/* Invalidate entire TLB */
642 	cmd.opcode = CMD_TLBI_NSNH_ALL;
643 	smmu_cmdq_enqueue_cmd(sc, &cmd);
644 	smmu_sync(sc);
645 }
646 
647 static void
648 smmu_tlbi_asid(struct smmu_softc *sc, uint16_t asid)
649 {
650 	struct smmu_cmdq_entry cmd;
651 
652 	/* Invalidate TLB for an ASID. */
653 	cmd.opcode = CMD_TLBI_NH_ASID;
654 	cmd.tlbi.asid = asid;
655 	smmu_cmdq_enqueue_cmd(sc, &cmd);
656 	smmu_sync(sc);
657 }
658 
659 static void
660 smmu_tlbi_va(struct smmu_softc *sc, vm_offset_t va, uint16_t asid)
661 {
662 	struct smmu_cmdq_entry cmd;
663 
664 	/* Invalidate specific range */
665 	cmd.opcode = CMD_TLBI_NH_VA;
666 	cmd.tlbi.asid = asid;
667 	cmd.tlbi.vmid = 0;
668 	cmd.tlbi.leaf = true; /* We change only L3. */
669 	cmd.tlbi.addr = va;
670 	smmu_cmdq_enqueue_cmd(sc, &cmd);
671 }
672 
673 static void
674 smmu_invalidate_sid(struct smmu_softc *sc, uint32_t sid)
675 {
676 	struct smmu_cmdq_entry cmd;
677 
678 	/* Invalidate cached config */
679 	cmd.opcode = CMD_CFGI_STE;
680 	cmd.cfgi.sid = sid;
681 	smmu_cmdq_enqueue_cmd(sc, &cmd);
682 	smmu_sync(sc);
683 }
684 
685 static void
686 smmu_prefetch_sid(struct smmu_softc *sc, uint32_t sid)
687 {
688 	struct smmu_cmdq_entry cmd;
689 
690 	cmd.opcode = CMD_PREFETCH_CONFIG;
691 	cmd.prefetch.sid = sid;
692 	smmu_cmdq_enqueue_cmd(sc, &cmd);
693 	smmu_sync(sc);
694 }
695 
696 /*
697  * Init STE in bypass mode. Traffic is not translated for the sid.
698  */
699 static void
700 smmu_init_ste_bypass(struct smmu_softc *sc, uint32_t sid, uint64_t *ste)
701 {
702 	uint64_t val;
703 
704 	val = STE0_VALID | STE0_CONFIG_BYPASS;
705 
706 	ste[1] = STE1_SHCFG_INCOMING | STE1_EATS_FULLATS;
707 	ste[2] = 0;
708 	ste[3] = 0;
709 	ste[4] = 0;
710 	ste[5] = 0;
711 	ste[6] = 0;
712 	ste[7] = 0;
713 
714 	smmu_invalidate_sid(sc, sid);
715 	ste[0] = val;
716 	dsb(sy);
717 	smmu_invalidate_sid(sc, sid);
718 
719 	smmu_prefetch_sid(sc, sid);
720 }
721 
722 /*
723  * Enable Stage1 (S1) translation for the sid.
724  */
725 static int
726 smmu_init_ste_s1(struct smmu_softc *sc, struct smmu_cd *cd,
727     uint32_t sid, uint64_t *ste)
728 {
729 	uint64_t val;
730 
731 	val = STE0_VALID;
732 
733 	/* S1 */
734 	ste[1] = STE1_EATS_FULLATS	|
735 		 STE1_S1CSH_IS		|
736 		 STE1_S1CIR_WBRA	|
737 		 STE1_S1COR_WBRA	|
738 		 STE1_STRW_NS_EL1;
739 	ste[2] = 0;
740 	ste[3] = 0;
741 	ste[4] = 0;
742 	ste[5] = 0;
743 	ste[6] = 0;
744 	ste[7] = 0;
745 
746 	if (sc->features & SMMU_FEATURE_STALL &&
747 	    ((sc->features & SMMU_FEATURE_STALL_FORCE) == 0))
748 		ste[1] |= STE1_S1STALLD;
749 
750 	/* Configure STE */
751 	val |= (cd->paddr & STE0_S1CONTEXTPTR_M);
752 	val |= STE0_CONFIG_S1_TRANS;
753 
754 	smmu_invalidate_sid(sc, sid);
755 
756 	/* The STE[0] has to be written in a single blast, last of all. */
757 	ste[0] = val;
758 	dsb(sy);
759 
760 	smmu_invalidate_sid(sc, sid);
761 	smmu_sync_cd(sc, sid, 0, true);
762 	smmu_invalidate_sid(sc, sid);
763 
764 	/* The sid will be used soon most likely. */
765 	smmu_prefetch_sid(sc, sid);
766 
767 	return (0);
768 }
769 
770 static uint64_t *
771 smmu_get_ste_addr(struct smmu_softc *sc, int sid)
772 {
773 	struct smmu_strtab *strtab;
774 	struct l1_desc *l1_desc;
775 	uint64_t *addr;
776 
777 	strtab = &sc->strtab;
778 
779 	if (sc->features & SMMU_FEATURE_2_LVL_STREAM_TABLE) {
780 		l1_desc = &strtab->l1[sid >> STRTAB_SPLIT];
781 		addr = l1_desc->va;
782 		addr += (sid & ((1 << STRTAB_SPLIT) - 1)) * STRTAB_STE_DWORDS;
783 	} else {
784 		addr = (void *)((uint64_t)strtab->vaddr +
785 		    STRTAB_STE_DWORDS * 8 * sid);
786 	};
787 
788 	return (addr);
789 }
790 
791 static int
792 smmu_init_ste(struct smmu_softc *sc, struct smmu_cd *cd, int sid, bool bypass)
793 {
794 	uint64_t *addr;
795 
796 	addr = smmu_get_ste_addr(sc, sid);
797 
798 	if (bypass)
799 		smmu_init_ste_bypass(sc, sid, addr);
800 	else
801 		smmu_init_ste_s1(sc, cd, sid, addr);
802 
803 	smmu_sync(sc);
804 
805 	return (0);
806 }
807 
808 static void
809 smmu_deinit_ste(struct smmu_softc *sc, int sid)
810 {
811 	uint64_t *ste;
812 
813 	ste = smmu_get_ste_addr(sc, sid);
814 	ste[0] = 0;
815 
816 	smmu_invalidate_sid(sc, sid);
817 	smmu_sync_cd(sc, sid, 0, true);
818 	smmu_invalidate_sid(sc, sid);
819 
820 	smmu_sync(sc);
821 }
822 
823 static int
824 smmu_init_cd(struct smmu_softc *sc, struct smmu_domain *domain)
825 {
826 	vm_paddr_t paddr;
827 	uint64_t *ptr;
828 	uint64_t val;
829 	vm_size_t size;
830 	struct smmu_cd *cd;
831 	struct smmu_pmap *p;
832 
833 	size = 1 * (CD_DWORDS << 3);
834 
835 	p = &domain->p;
836 	cd = domain->cd = malloc(sizeof(struct smmu_cd),
837 	    M_SMMU, M_WAITOK | M_ZERO);
838 
839 	cd->vaddr = contigmalloc(size, M_SMMU,
840 	    M_WAITOK | M_ZERO,	/* flags */
841 	    0,			/* low */
842 	    (1ul << 40) - 1,	/* high */
843 	    size,		/* alignment */
844 	    0);			/* boundary */
845 	if (cd->vaddr == NULL) {
846 		device_printf(sc->dev, "Failed to allocate CD\n");
847 		return (ENXIO);
848 	}
849 
850 	cd->paddr = vtophys(cd->vaddr);
851 
852 	ptr = cd->vaddr;
853 
854 	val = CD0_VALID;
855 	val |= CD0_AA64;
856 	val |= CD0_R;
857 	val |= CD0_A;
858 	val |= CD0_ASET;
859 	val |= (uint64_t)domain->asid << CD0_ASID_S;
860 	val |= CD0_TG0_4KB;
861 	val |= CD0_EPD1; /* Disable TT1 */
862 	val |= ((64 - sc->ias) << CD0_T0SZ_S);
863 	val |= CD0_IPS_48BITS;
864 
865 	paddr = p->sp_l0_paddr & CD1_TTB0_M;
866 	KASSERT(paddr == p->sp_l0_paddr, ("bad allocation 1"));
867 
868 	ptr[1] = paddr;
869 	ptr[2] = 0;
870 	ptr[3] = MAIR_ATTR(MAIR_DEVICE_nGnRnE, VM_MEMATTR_DEVICE)	|
871 		 MAIR_ATTR(MAIR_NORMAL_NC, VM_MEMATTR_UNCACHEABLE)	|
872 		 MAIR_ATTR(MAIR_NORMAL_WB, VM_MEMATTR_WRITE_BACK)	|
873 		 MAIR_ATTR(MAIR_NORMAL_WT, VM_MEMATTR_WRITE_THROUGH);
874 
875 	/* Install the CD. */
876 	ptr[0] = val;
877 
878 	return (0);
879 }
880 
881 static int
882 smmu_init_strtab_linear(struct smmu_softc *sc)
883 {
884 	struct smmu_strtab *strtab;
885 	vm_paddr_t base;
886 	uint32_t size;
887 	uint64_t reg;
888 
889 	strtab = &sc->strtab;
890 	strtab->num_l1_entries = (1 << sc->sid_bits);
891 
892 	size = strtab->num_l1_entries * (STRTAB_STE_DWORDS << 3);
893 
894 	if (bootverbose)
895 		device_printf(sc->dev,
896 		    "%s: linear strtab size %d, num_l1_entries %d\n",
897 		    __func__, size, strtab->num_l1_entries);
898 
899 	strtab->vaddr = contigmalloc(size, M_SMMU,
900 	    M_WAITOK | M_ZERO,	/* flags */
901 	    0,			/* low */
902 	    (1ul << 48) - 1,	/* high */
903 	    size,		/* alignment */
904 	    0);			/* boundary */
905 	if (strtab->vaddr == NULL) {
906 		device_printf(sc->dev, "failed to allocate strtab\n");
907 		return (ENXIO);
908 	}
909 
910 	reg = STRTAB_BASE_CFG_FMT_LINEAR;
911 	reg |= sc->sid_bits << STRTAB_BASE_CFG_LOG2SIZE_S;
912 	strtab->base_cfg = (uint32_t)reg;
913 
914 	base = vtophys(strtab->vaddr);
915 
916 	reg = base & STRTAB_BASE_ADDR_M;
917 	KASSERT(reg == base, ("bad allocation 2"));
918 	reg |= STRTAB_BASE_RA;
919 	strtab->base = reg;
920 
921 	return (0);
922 }
923 
924 static int
925 smmu_init_strtab_2lvl(struct smmu_softc *sc)
926 {
927 	struct smmu_strtab *strtab;
928 	vm_paddr_t base;
929 	uint64_t reg_base;
930 	uint32_t l1size;
931 	uint32_t size;
932 	uint32_t reg;
933 	int sz;
934 
935 	strtab = &sc->strtab;
936 
937 	size = STRTAB_L1_SZ_SHIFT - (ilog2(STRTAB_L1_DESC_DWORDS) + 3);
938 	size = min(size, sc->sid_bits - STRTAB_SPLIT);
939 	strtab->num_l1_entries = (1 << size);
940 	size += STRTAB_SPLIT;
941 
942 	l1size = strtab->num_l1_entries * (STRTAB_L1_DESC_DWORDS << 3);
943 
944 	if (bootverbose)
945 		device_printf(sc->dev,
946 		    "%s: size %d, l1 entries %d, l1size %d\n",
947 		    __func__, size, strtab->num_l1_entries, l1size);
948 
949 	strtab->vaddr = contigmalloc(l1size, M_SMMU,
950 	    M_WAITOK | M_ZERO,	/* flags */
951 	    0,			/* low */
952 	    (1ul << 48) - 1,	/* high */
953 	    l1size,		/* alignment */
954 	    0);			/* boundary */
955 	if (strtab->vaddr == NULL) {
956 		device_printf(sc->dev, "Failed to allocate 2lvl strtab.\n");
957 		return (ENOMEM);
958 	}
959 
960 	sz = strtab->num_l1_entries * sizeof(struct l1_desc);
961 
962 	strtab->l1 = malloc(sz, M_SMMU, M_WAITOK | M_ZERO);
963 	if (strtab->l1 == NULL) {
964 		free(strtab->vaddr, M_SMMU);
965 		return (ENOMEM);
966 	}
967 
968 	reg = STRTAB_BASE_CFG_FMT_2LVL;
969 	reg |= size << STRTAB_BASE_CFG_LOG2SIZE_S;
970 	reg |= STRTAB_SPLIT << STRTAB_BASE_CFG_SPLIT_S;
971 	strtab->base_cfg = (uint32_t)reg;
972 
973 	base = vtophys(strtab->vaddr);
974 
975 	reg_base = base & STRTAB_BASE_ADDR_M;
976 	KASSERT(reg_base == base, ("bad allocation 3"));
977 	reg_base |= STRTAB_BASE_RA;
978 	strtab->base = reg_base;
979 
980 	return (0);
981 }
982 
983 static int
984 smmu_init_strtab(struct smmu_softc *sc)
985 {
986 	int error;
987 
988 	if (sc->features & SMMU_FEATURE_2_LVL_STREAM_TABLE)
989 		error = smmu_init_strtab_2lvl(sc);
990 	else
991 		error = smmu_init_strtab_linear(sc);
992 
993 	return (error);
994 }
995 
996 static int
997 smmu_init_l1_entry(struct smmu_softc *sc, int sid)
998 {
999 	struct smmu_strtab *strtab;
1000 	struct l1_desc *l1_desc;
1001 	uint64_t *addr;
1002 	uint64_t val;
1003 	size_t size;
1004 	int i;
1005 
1006 	strtab = &sc->strtab;
1007 	l1_desc = &strtab->l1[sid >> STRTAB_SPLIT];
1008 	if (l1_desc->va) {
1009 		/* Already allocated. */
1010 		return (0);
1011 	}
1012 
1013 	size = 1 << (STRTAB_SPLIT + ilog2(STRTAB_STE_DWORDS) + 3);
1014 
1015 	l1_desc->span = STRTAB_SPLIT + 1;
1016 	l1_desc->va = contigmalloc(size, M_SMMU,
1017 	    M_WAITOK | M_ZERO,	/* flags */
1018 	    0,			/* low */
1019 	    (1ul << 48) - 1,	/* high */
1020 	    size,		/* alignment */
1021 	    0);			/* boundary */
1022 	if (l1_desc->va == NULL) {
1023 		device_printf(sc->dev, "failed to allocate l2 entry\n");
1024 		return (ENXIO);
1025 	}
1026 
1027 	l1_desc->pa = vtophys(l1_desc->va);
1028 
1029 	i = sid >> STRTAB_SPLIT;
1030 	addr = (void *)((uint64_t)strtab->vaddr +
1031 	    STRTAB_L1_DESC_DWORDS * 8 * i);
1032 
1033 	/* Install the L1 entry. */
1034 	val = l1_desc->pa & STRTAB_L1_DESC_L2PTR_M;
1035 	KASSERT(val == l1_desc->pa, ("bad allocation 4"));
1036 	val |= l1_desc->span;
1037 	*addr = val;
1038 
1039 	return (0);
1040 }
1041 
1042 static void __unused
1043 smmu_deinit_l1_entry(struct smmu_softc *sc, int sid)
1044 {
1045 	struct smmu_strtab *strtab;
1046 	struct l1_desc *l1_desc;
1047 	uint64_t *addr;
1048 	int i;
1049 
1050 	strtab = &sc->strtab;
1051 
1052 	i = sid >> STRTAB_SPLIT;
1053 	addr = (void *)((uint64_t)strtab->vaddr +
1054 	    STRTAB_L1_DESC_DWORDS * 8 * i);
1055 	*addr = 0;
1056 
1057 	l1_desc = &strtab->l1[sid >> STRTAB_SPLIT];
1058 	free(l1_desc->va, M_SMMU);
1059 }
1060 
1061 static int
1062 smmu_disable(struct smmu_softc *sc)
1063 {
1064 	uint32_t reg;
1065 	int error;
1066 
1067 	/* Disable SMMU */
1068 	reg = bus_read_4(sc->res[0], SMMU_CR0);
1069 	reg &= ~CR0_SMMUEN;
1070 	error = smmu_write_ack(sc, SMMU_CR0, SMMU_CR0ACK, reg);
1071 	if (error)
1072 		device_printf(sc->dev, "Could not disable SMMU.\n");
1073 
1074 	return (0);
1075 }
1076 
1077 static int
1078 smmu_event_intr(void *arg)
1079 {
1080 	uint32_t evt[EVTQ_ENTRY_DWORDS * 2];
1081 	struct smmu_softc *sc;
1082 
1083 	sc = arg;
1084 
1085 	do {
1086 		smmu_evtq_dequeue(sc, evt);
1087 		smmu_print_event(sc, evt);
1088 	} while (!smmu_q_empty(&sc->evtq));
1089 
1090 	return (FILTER_HANDLED);
1091 }
1092 
1093 static int __unused
1094 smmu_sync_intr(void *arg)
1095 {
1096 	struct smmu_softc *sc;
1097 
1098 	sc = arg;
1099 
1100 	device_printf(sc->dev, "%s\n", __func__);
1101 
1102 	return (FILTER_HANDLED);
1103 }
1104 
1105 static int
1106 smmu_gerr_intr(void *arg)
1107 {
1108 	struct smmu_softc *sc;
1109 
1110 	sc = arg;
1111 
1112 	device_printf(sc->dev, "SMMU Global Error\n");
1113 
1114 	return (FILTER_HANDLED);
1115 }
1116 
1117 static int
1118 smmu_enable_interrupts(struct smmu_softc *sc)
1119 {
1120 	uint32_t reg;
1121 	int error;
1122 
1123 	/* Disable MSI. */
1124 	bus_write_8(sc->res[0], SMMU_GERROR_IRQ_CFG0, 0);
1125 	bus_write_4(sc->res[0], SMMU_GERROR_IRQ_CFG1, 0);
1126 	bus_write_4(sc->res[0], SMMU_GERROR_IRQ_CFG2, 0);
1127 
1128 	bus_write_8(sc->res[0], SMMU_EVENTQ_IRQ_CFG0, 0);
1129 	bus_write_4(sc->res[0], SMMU_EVENTQ_IRQ_CFG1, 0);
1130 	bus_write_4(sc->res[0], SMMU_EVENTQ_IRQ_CFG2, 0);
1131 
1132 	if (sc->features & CR0_PRIQEN) {
1133 		bus_write_8(sc->res[0], SMMU_PRIQ_IRQ_CFG0, 0);
1134 		bus_write_4(sc->res[0], SMMU_PRIQ_IRQ_CFG1, 0);
1135 		bus_write_4(sc->res[0], SMMU_PRIQ_IRQ_CFG2, 0);
1136 	}
1137 
1138 	/* Disable any interrupts. */
1139 	error = smmu_write_ack(sc, SMMU_IRQ_CTRL, SMMU_IRQ_CTRLACK, 0);
1140 	if (error) {
1141 		device_printf(sc->dev, "Could not disable interrupts.\n");
1142 		return (ENXIO);
1143 	}
1144 
1145 	/* Enable interrupts. */
1146 	reg = IRQ_CTRL_EVENTQ_IRQEN | IRQ_CTRL_GERROR_IRQEN;
1147 	if (sc->features & SMMU_FEATURE_PRI)
1148 		reg |= IRQ_CTRL_PRIQ_IRQEN;
1149 
1150 	error = smmu_write_ack(sc, SMMU_IRQ_CTRL, SMMU_IRQ_CTRLACK, reg);
1151 	if (error) {
1152 		device_printf(sc->dev, "Could not enable interrupts.\n");
1153 		return (ENXIO);
1154 	}
1155 
1156 	return (0);
1157 }
1158 
1159 #ifdef DEV_ACPI
1160 static void
1161 smmu_configure_intr(struct smmu_softc *sc, struct resource *res)
1162 {
1163 	struct intr_map_data_acpi *ad;
1164 	struct intr_map_data *data;
1165 
1166 	data = rman_get_virtual(res);
1167 	KASSERT(data != NULL, ("data is NULL"));
1168 
1169 	if (data->type == INTR_MAP_DATA_ACPI) {
1170 		ad = (struct intr_map_data_acpi *)data;
1171 		ad->trig = INTR_TRIGGER_EDGE;
1172 		ad->pol = INTR_POLARITY_HIGH;
1173 	}
1174 }
1175 #endif
1176 
1177 static int
1178 smmu_setup_interrupts(struct smmu_softc *sc)
1179 {
1180 	device_t dev;
1181 	int error;
1182 
1183 	dev = sc->dev;
1184 
1185 #ifdef DEV_ACPI
1186 	/*
1187 	 * Configure SMMU interrupts as EDGE triggered manually
1188 	 * as ACPI tables carries no information for that.
1189 	 */
1190 	smmu_configure_intr(sc, sc->res[1]);
1191 	/* PRIQ is not in use. */
1192 	smmu_configure_intr(sc, sc->res[3]);
1193 	smmu_configure_intr(sc, sc->res[4]);
1194 #endif
1195 
1196 	error = bus_setup_intr(dev, sc->res[1], INTR_TYPE_MISC,
1197 	    smmu_event_intr, NULL, sc, &sc->intr_cookie[0]);
1198 	if (error) {
1199 		device_printf(dev, "Couldn't setup Event interrupt handler\n");
1200 		return (ENXIO);
1201 	}
1202 
1203 	error = bus_setup_intr(dev, sc->res[4], INTR_TYPE_MISC,
1204 	    smmu_gerr_intr, NULL, sc, &sc->intr_cookie[2]);
1205 	if (error) {
1206 		device_printf(dev, "Couldn't setup Gerr interrupt handler\n");
1207 		return (ENXIO);
1208 	}
1209 
1210 	return (0);
1211 }
1212 
1213 static int
1214 smmu_reset(struct smmu_softc *sc)
1215 {
1216 	struct smmu_cmdq_entry cmd;
1217 	struct smmu_strtab *strtab;
1218 	int error;
1219 	int reg;
1220 
1221 	reg = bus_read_4(sc->res[0], SMMU_CR0);
1222 
1223 	if (reg & CR0_SMMUEN)
1224 		device_printf(sc->dev,
1225 		    "%s: Warning: SMMU is enabled\n", __func__);
1226 
1227 	error = smmu_disable(sc);
1228 	if (error)
1229 		device_printf(sc->dev,
1230 		    "%s: Could not disable SMMU.\n", __func__);
1231 
1232 	if (smmu_enable_interrupts(sc) != 0) {
1233 		device_printf(sc->dev, "Could not enable interrupts.\n");
1234 		return (ENXIO);
1235 	}
1236 
1237 	reg = CR1_TABLE_SH_IS	|
1238 	      CR1_TABLE_OC_WBC	|
1239 	      CR1_TABLE_IC_WBC	|
1240 	      CR1_QUEUE_SH_IS	|
1241 	      CR1_QUEUE_OC_WBC	|
1242 	      CR1_QUEUE_IC_WBC;
1243 	bus_write_4(sc->res[0], SMMU_CR1, reg);
1244 
1245 	reg = CR2_PTM | CR2_RECINVSID | CR2_E2H;
1246 	bus_write_4(sc->res[0], SMMU_CR2, reg);
1247 
1248 	/* Stream table. */
1249 	strtab = &sc->strtab;
1250 	bus_write_8(sc->res[0], SMMU_STRTAB_BASE, strtab->base);
1251 	bus_write_4(sc->res[0], SMMU_STRTAB_BASE_CFG, strtab->base_cfg);
1252 
1253 	/* Command queue. */
1254 	bus_write_8(sc->res[0], SMMU_CMDQ_BASE, sc->cmdq.base);
1255 	bus_write_4(sc->res[0], SMMU_CMDQ_PROD, sc->cmdq.lc.prod);
1256 	bus_write_4(sc->res[0], SMMU_CMDQ_CONS, sc->cmdq.lc.cons);
1257 
1258 	reg = CR0_CMDQEN;
1259 	error = smmu_write_ack(sc, SMMU_CR0, SMMU_CR0ACK, reg);
1260 	if (error) {
1261 		device_printf(sc->dev, "Could not enable command queue\n");
1262 		return (ENXIO);
1263 	}
1264 
1265 	/* Invalidate cached configuration. */
1266 	smmu_invalidate_all_sid(sc);
1267 
1268 	if (sc->features & SMMU_FEATURE_HYP) {
1269 		cmd.opcode = CMD_TLBI_EL2_ALL;
1270 		smmu_cmdq_enqueue_cmd(sc, &cmd);
1271 	};
1272 
1273 	/* Invalidate TLB. */
1274 	smmu_tlbi_all(sc);
1275 
1276 	/* Event queue */
1277 	bus_write_8(sc->res[0], SMMU_EVENTQ_BASE, sc->evtq.base);
1278 	bus_write_4(sc->res[0], SMMU_EVENTQ_PROD, sc->evtq.lc.prod);
1279 	bus_write_4(sc->res[0], SMMU_EVENTQ_CONS, sc->evtq.lc.cons);
1280 
1281 	reg |= CR0_EVENTQEN;
1282 	error = smmu_write_ack(sc, SMMU_CR0, SMMU_CR0ACK, reg);
1283 	if (error) {
1284 		device_printf(sc->dev, "Could not enable event queue\n");
1285 		return (ENXIO);
1286 	}
1287 
1288 	if (sc->features & SMMU_FEATURE_PRI) {
1289 		/* PRI queue */
1290 		bus_write_8(sc->res[0], SMMU_PRIQ_BASE, sc->priq.base);
1291 		bus_write_4(sc->res[0], SMMU_PRIQ_PROD, sc->priq.lc.prod);
1292 		bus_write_4(sc->res[0], SMMU_PRIQ_CONS, sc->priq.lc.cons);
1293 
1294 		reg |= CR0_PRIQEN;
1295 		error = smmu_write_ack(sc, SMMU_CR0, SMMU_CR0ACK, reg);
1296 		if (error) {
1297 			device_printf(sc->dev, "Could not enable PRI queue\n");
1298 			return (ENXIO);
1299 		}
1300 	}
1301 
1302 	if (sc->features & SMMU_FEATURE_ATS) {
1303 		reg |= CR0_ATSCHK;
1304 		error = smmu_write_ack(sc, SMMU_CR0, SMMU_CR0ACK, reg);
1305 		if (error) {
1306 			device_printf(sc->dev, "Could not enable ATS check.\n");
1307 			return (ENXIO);
1308 		}
1309 	}
1310 
1311 	reg |= CR0_SMMUEN;
1312 	error = smmu_write_ack(sc, SMMU_CR0, SMMU_CR0ACK, reg);
1313 	if (error) {
1314 		device_printf(sc->dev, "Could not enable SMMU.\n");
1315 		return (ENXIO);
1316 	}
1317 
1318 	return (0);
1319 }
1320 
1321 static int
1322 smmu_check_features(struct smmu_softc *sc)
1323 {
1324 	uint32_t reg;
1325 	uint32_t val;
1326 
1327 	sc->features = 0;
1328 
1329 	reg = bus_read_4(sc->res[0], SMMU_IDR0);
1330 
1331 	if (reg & IDR0_ST_LVL_2) {
1332 		if (bootverbose)
1333 			device_printf(sc->dev,
1334 			    "2-level stream table supported.\n");
1335 		sc->features |= SMMU_FEATURE_2_LVL_STREAM_TABLE;
1336 	}
1337 
1338 	if (reg & IDR0_CD2L) {
1339 		if (bootverbose)
1340 			device_printf(sc->dev,
1341 			    "2-level CD table supported.\n");
1342 		sc->features |= SMMU_FEATURE_2_LVL_CD;
1343 	}
1344 
1345 	switch (reg & IDR0_TTENDIAN_M) {
1346 	case IDR0_TTENDIAN_MIXED:
1347 		if (bootverbose)
1348 			device_printf(sc->dev, "Mixed endianness supported.\n");
1349 		sc->features |= SMMU_FEATURE_TT_LE;
1350 		sc->features |= SMMU_FEATURE_TT_BE;
1351 		break;
1352 	case IDR0_TTENDIAN_LITTLE:
1353 		if (bootverbose)
1354 			device_printf(sc->dev,
1355 			    "Little endian supported only.\n");
1356 		sc->features |= SMMU_FEATURE_TT_LE;
1357 		break;
1358 	case IDR0_TTENDIAN_BIG:
1359 		if (bootverbose)
1360 			device_printf(sc->dev, "Big endian supported only.\n");
1361 		sc->features |= SMMU_FEATURE_TT_BE;
1362 		break;
1363 	default:
1364 		device_printf(sc->dev, "Unsupported endianness.\n");
1365 		return (ENXIO);
1366 	}
1367 
1368 	if (reg & IDR0_SEV)
1369 		sc->features |= SMMU_FEATURE_SEV;
1370 
1371 	if (reg & IDR0_MSI) {
1372 		if (bootverbose)
1373 			device_printf(sc->dev, "MSI feature present.\n");
1374 		sc->features |= SMMU_FEATURE_MSI;
1375 	}
1376 
1377 	if (reg & IDR0_HYP) {
1378 		if (bootverbose)
1379 			device_printf(sc->dev, "HYP feature present.\n");
1380 		sc->features |= SMMU_FEATURE_HYP;
1381 	}
1382 
1383 	if (reg & IDR0_ATS)
1384 		sc->features |= SMMU_FEATURE_ATS;
1385 
1386 	if (reg & IDR0_PRI)
1387 		sc->features |= SMMU_FEATURE_PRI;
1388 
1389 	switch (reg & IDR0_STALL_MODEL_M) {
1390 	case IDR0_STALL_MODEL_FORCE:
1391 		/* Stall is forced. */
1392 		sc->features |= SMMU_FEATURE_STALL_FORCE;
1393 		/* FALLTHROUGH */
1394 	case IDR0_STALL_MODEL_STALL:
1395 		sc->features |= SMMU_FEATURE_STALL;
1396 		break;
1397 	}
1398 
1399 	/* Grab translation stages supported. */
1400 	if (reg & IDR0_S1P) {
1401 		if (bootverbose)
1402 			device_printf(sc->dev,
1403 			    "Stage 1 translation supported.\n");
1404 		sc->features |= SMMU_FEATURE_S1P;
1405 	}
1406 	if (reg & IDR0_S2P) {
1407 		if (bootverbose)
1408 			device_printf(sc->dev,
1409 			    "Stage 2 translation supported.\n");
1410 		sc->features |= SMMU_FEATURE_S2P;
1411 	}
1412 
1413 	switch (reg & IDR0_TTF_M) {
1414 	case IDR0_TTF_ALL:
1415 	case IDR0_TTF_AA64:
1416 		sc->ias = 40;
1417 		break;
1418 	default:
1419 		device_printf(sc->dev, "No AArch64 table format support.\n");
1420 		return (ENXIO);
1421 	}
1422 
1423 	if (reg & IDR0_ASID16)
1424 		sc->asid_bits = 16;
1425 	else
1426 		sc->asid_bits = 8;
1427 
1428 	if (bootverbose)
1429 		device_printf(sc->dev, "ASID bits %d\n", sc->asid_bits);
1430 
1431 	if (reg & IDR0_VMID16)
1432 		sc->vmid_bits = 16;
1433 	else
1434 		sc->vmid_bits = 8;
1435 
1436 	reg = bus_read_4(sc->res[0], SMMU_IDR1);
1437 
1438 	if (reg & (IDR1_TABLES_PRESET | IDR1_QUEUES_PRESET | IDR1_REL)) {
1439 		device_printf(sc->dev,
1440 		    "Embedded implementations not supported by this driver.\n");
1441 		return (ENXIO);
1442 	}
1443 
1444 	val = (reg & IDR1_CMDQS_M) >> IDR1_CMDQS_S;
1445 	sc->cmdq.size_log2 = val;
1446 	if (bootverbose)
1447 		device_printf(sc->dev, "CMD queue bits %d\n", val);
1448 
1449 	val = (reg & IDR1_EVENTQS_M) >> IDR1_EVENTQS_S;
1450 	sc->evtq.size_log2 = val;
1451 	if (bootverbose)
1452 		device_printf(sc->dev, "EVENT queue bits %d\n", val);
1453 
1454 	if (sc->features & SMMU_FEATURE_PRI) {
1455 		val = (reg & IDR1_PRIQS_M) >> IDR1_PRIQS_S;
1456 		sc->priq.size_log2 = val;
1457 		if (bootverbose)
1458 			device_printf(sc->dev, "PRI queue bits %d\n", val);
1459 	}
1460 
1461 	sc->ssid_bits = (reg & IDR1_SSIDSIZE_M) >> IDR1_SSIDSIZE_S;
1462 	sc->sid_bits = (reg & IDR1_SIDSIZE_M) >> IDR1_SIDSIZE_S;
1463 
1464 	if (sc->sid_bits <= STRTAB_SPLIT)
1465 		sc->features &= ~SMMU_FEATURE_2_LVL_STREAM_TABLE;
1466 
1467 	if (bootverbose) {
1468 		device_printf(sc->dev, "SSID bits %d\n", sc->ssid_bits);
1469 		device_printf(sc->dev, "SID bits %d\n", sc->sid_bits);
1470 	}
1471 
1472 	/* IDR3 */
1473 	reg = bus_read_4(sc->res[0], SMMU_IDR3);
1474 	if (reg & IDR3_RIL)
1475 		sc->features |= SMMU_FEATURE_RANGE_INV;
1476 
1477 	/* IDR5 */
1478 	reg = bus_read_4(sc->res[0], SMMU_IDR5);
1479 
1480 	switch (reg & IDR5_OAS_M) {
1481 	case IDR5_OAS_32:
1482 		sc->oas = 32;
1483 		break;
1484 	case IDR5_OAS_36:
1485 		sc->oas = 36;
1486 		break;
1487 	case IDR5_OAS_40:
1488 		sc->oas = 40;
1489 		break;
1490 	case IDR5_OAS_42:
1491 		sc->oas = 42;
1492 		break;
1493 	case IDR5_OAS_44:
1494 		sc->oas = 44;
1495 		break;
1496 	case IDR5_OAS_48:
1497 		sc->oas = 48;
1498 		break;
1499 	case IDR5_OAS_52:
1500 		sc->oas = 52;
1501 		break;
1502 	}
1503 
1504 	sc->pgsizes = 0;
1505 	if (reg & IDR5_GRAN64K)
1506 		sc->pgsizes |= 64 * 1024;
1507 	if (reg & IDR5_GRAN16K)
1508 		sc->pgsizes |= 16 * 1024;
1509 	if (reg & IDR5_GRAN4K)
1510 		sc->pgsizes |= 4 * 1024;
1511 
1512 	if ((reg & IDR5_VAX_M) == IDR5_VAX_52)
1513 		sc->features |= SMMU_FEATURE_VAX;
1514 
1515 	return (0);
1516 }
1517 
1518 static void
1519 smmu_init_asids(struct smmu_softc *sc)
1520 {
1521 
1522 	sc->asid_set_size = (1 << sc->asid_bits);
1523 	sc->asid_set = bit_alloc(sc->asid_set_size, M_SMMU, M_WAITOK);
1524 	mtx_init(&sc->asid_set_mutex, "asid set", NULL, MTX_SPIN);
1525 }
1526 
1527 static int
1528 smmu_asid_alloc(struct smmu_softc *sc, int *new_asid)
1529 {
1530 
1531 	mtx_lock_spin(&sc->asid_set_mutex);
1532 	bit_ffc(sc->asid_set, sc->asid_set_size, new_asid);
1533 	if (*new_asid == -1) {
1534 		mtx_unlock_spin(&sc->asid_set_mutex);
1535 		return (ENOMEM);
1536 	}
1537 	bit_set(sc->asid_set, *new_asid);
1538 	mtx_unlock_spin(&sc->asid_set_mutex);
1539 
1540 	return (0);
1541 }
1542 
1543 static void
1544 smmu_asid_free(struct smmu_softc *sc, int asid)
1545 {
1546 
1547 	mtx_lock_spin(&sc->asid_set_mutex);
1548 	bit_clear(sc->asid_set, asid);
1549 	mtx_unlock_spin(&sc->asid_set_mutex);
1550 }
1551 
1552 /*
1553  * Device interface.
1554  */
1555 int
1556 smmu_attach(device_t dev)
1557 {
1558 	struct smmu_softc *sc;
1559 	int error;
1560 
1561 	sc = device_get_softc(dev);
1562 	sc->dev = dev;
1563 
1564 	mtx_init(&sc->sc_mtx, device_get_nameunit(sc->dev), "smmu", MTX_DEF);
1565 
1566 	error = smmu_setup_interrupts(sc);
1567 	if (error) {
1568 		bus_release_resources(dev, smmu_spec, sc->res);
1569 		return (ENXIO);
1570 	}
1571 
1572 	error = smmu_check_features(sc);
1573 	if (error) {
1574 		device_printf(dev, "Some features are required "
1575 		    "but not supported by hardware.\n");
1576 		return (ENXIO);
1577 	}
1578 
1579 	smmu_init_asids(sc);
1580 
1581 	error = smmu_init_queues(sc);
1582 	if (error) {
1583 		device_printf(dev, "Couldn't allocate queues.\n");
1584 		return (ENXIO);
1585 	}
1586 
1587 	error = smmu_init_strtab(sc);
1588 	if (error) {
1589 		device_printf(dev, "Couldn't allocate strtab.\n");
1590 		return (ENXIO);
1591 	}
1592 
1593 	error = smmu_reset(sc);
1594 	if (error) {
1595 		device_printf(dev, "Couldn't reset SMMU.\n");
1596 		return (ENXIO);
1597 	}
1598 
1599 	return (0);
1600 }
1601 
1602 int
1603 smmu_detach(device_t dev)
1604 {
1605 	struct smmu_softc *sc;
1606 
1607 	sc = device_get_softc(dev);
1608 
1609 	bus_release_resources(dev, smmu_spec, sc->res);
1610 
1611 	return (0);
1612 }
1613 
1614 static int
1615 smmu_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
1616 {
1617 	struct smmu_softc *sc;
1618 
1619 	sc = device_get_softc(dev);
1620 
1621 	device_printf(sc->dev, "%s\n", __func__);
1622 
1623 	return (ENOENT);
1624 }
1625 
1626 static int
1627 smmu_unmap(device_t dev, struct iommu_domain *iodom,
1628     vm_offset_t va, bus_size_t size)
1629 {
1630 	struct smmu_domain *domain;
1631 	struct smmu_softc *sc;
1632 	int err;
1633 	int i;
1634 
1635 	sc = device_get_softc(dev);
1636 
1637 	domain = (struct smmu_domain *)iodom;
1638 
1639 	err = 0;
1640 
1641 	dprintf("%s: %lx, %ld, domain %d\n", __func__, va, size, domain->asid);
1642 
1643 	for (i = 0; i < size; i += PAGE_SIZE) {
1644 		if (smmu_pmap_remove(&domain->p, va) == 0) {
1645 			/* pmap entry removed, invalidate TLB. */
1646 			smmu_tlbi_va(sc, va, domain->asid);
1647 		} else {
1648 			err = ENOENT;
1649 			break;
1650 		}
1651 		va += PAGE_SIZE;
1652 	}
1653 
1654 	smmu_sync(sc);
1655 
1656 	return (err);
1657 }
1658 
1659 static int
1660 smmu_map(device_t dev, struct iommu_domain *iodom,
1661     vm_offset_t va, vm_page_t *ma, vm_size_t size,
1662     vm_prot_t prot)
1663 {
1664 	struct smmu_domain *domain;
1665 	struct smmu_softc *sc;
1666 	vm_paddr_t pa;
1667 	int error;
1668 	int i;
1669 
1670 	sc = device_get_softc(dev);
1671 
1672 	domain = (struct smmu_domain *)iodom;
1673 
1674 	dprintf("%s: %lx -> %lx, %ld, domain %d\n", __func__, va, pa, size,
1675 	    domain->asid);
1676 
1677 	for (i = 0; size > 0; size -= PAGE_SIZE) {
1678 		pa = VM_PAGE_TO_PHYS(ma[i++]);
1679 		error = smmu_pmap_enter(&domain->p, va, pa, prot, 0);
1680 		if (error)
1681 			return (error);
1682 		smmu_tlbi_va(sc, va, domain->asid);
1683 		va += PAGE_SIZE;
1684 	}
1685 
1686 	smmu_sync(sc);
1687 
1688 	return (0);
1689 }
1690 
1691 static struct iommu_domain *
1692 smmu_domain_alloc(device_t dev, struct iommu_unit *iommu)
1693 {
1694 	struct iommu_domain *iodom;
1695 	struct smmu_domain *domain;
1696 	struct smmu_unit *unit;
1697 	struct smmu_softc *sc;
1698 	int error;
1699 	int new_asid;
1700 
1701 	sc = device_get_softc(dev);
1702 
1703 	unit = (struct smmu_unit *)iommu;
1704 
1705 	domain = malloc(sizeof(*domain), M_SMMU, M_WAITOK | M_ZERO);
1706 
1707 	error = smmu_asid_alloc(sc, &new_asid);
1708 	if (error) {
1709 		free(domain, M_SMMU);
1710 		device_printf(sc->dev,
1711 		    "Could not allocate ASID for a new domain.\n");
1712 		return (NULL);
1713 	}
1714 
1715 	domain->asid = (uint16_t)new_asid;
1716 
1717 	smmu_pmap_pinit(&domain->p);
1718 
1719 	error = smmu_init_cd(sc, domain);
1720 	if (error) {
1721 		free(domain, M_SMMU);
1722 		device_printf(sc->dev, "Could not initialize CD\n");
1723 		return (NULL);
1724 	}
1725 
1726 	smmu_tlbi_asid(sc, domain->asid);
1727 
1728 	LIST_INIT(&domain->ctx_list);
1729 
1730 	IOMMU_LOCK(iommu);
1731 	LIST_INSERT_HEAD(&unit->domain_list, domain, next);
1732 	IOMMU_UNLOCK(iommu);
1733 
1734 	iodom = &domain->iodom;
1735 
1736 	/*
1737 	 * Use 48-bit address space regardless of VAX bit
1738 	 * as we need 64k IOMMU_PAGE_SIZE for 52-bit space.
1739 	 */
1740 	iodom->end = MAXADDR_48BIT;
1741 
1742 	return (iodom);
1743 }
1744 
1745 static void
1746 smmu_domain_free(device_t dev, struct iommu_domain *iodom)
1747 {
1748 	struct smmu_domain *domain;
1749 	struct smmu_softc *sc;
1750 	struct smmu_cd *cd;
1751 
1752 	sc = device_get_softc(dev);
1753 
1754 	domain = (struct smmu_domain *)iodom;
1755 
1756 	LIST_REMOVE(domain, next);
1757 
1758 	cd = domain->cd;
1759 
1760 	smmu_pmap_remove_pages(&domain->p);
1761 	smmu_pmap_release(&domain->p);
1762 
1763 	smmu_tlbi_asid(sc, domain->asid);
1764 	smmu_asid_free(sc, domain->asid);
1765 
1766 	free(cd->vaddr, M_SMMU);
1767 	free(cd, M_SMMU);
1768 
1769 	free(domain, M_SMMU);
1770 }
1771 
1772 static int
1773 smmu_set_buswide(device_t dev, struct smmu_domain *domain,
1774     struct smmu_ctx *ctx)
1775 {
1776 	struct smmu_softc *sc;
1777 	int i;
1778 
1779 	sc = device_get_softc(dev);
1780 
1781 	for (i = 0; i < PCI_SLOTMAX; i++)
1782 		smmu_init_ste(sc, domain->cd, (ctx->sid | i), ctx->bypass);
1783 
1784 	return (0);
1785 }
1786 
1787 static int
1788 smmu_pci_get_sid(device_t child, u_int *xref0, u_int *sid0)
1789 {
1790 	struct pci_id_ofw_iommu pi;
1791 	int err;
1792 
1793 	err = pci_get_id(child, PCI_ID_OFW_IOMMU, (uintptr_t *)&pi);
1794 	if (err == 0) {
1795 		if (sid0)
1796 			*sid0 = pi.id;
1797 		if (xref0)
1798 			*xref0 = pi.xref;
1799 	}
1800 
1801 	return (err);
1802 }
1803 
1804 static struct iommu_ctx *
1805 smmu_ctx_alloc(device_t dev, struct iommu_domain *iodom, device_t child,
1806     bool disabled)
1807 {
1808 	struct smmu_domain *domain;
1809 	struct smmu_ctx *ctx;
1810 
1811 	domain = (struct smmu_domain *)iodom;
1812 
1813 	ctx = malloc(sizeof(struct smmu_ctx), M_SMMU, M_WAITOK | M_ZERO);
1814 	ctx->dev = child;
1815 	ctx->domain = domain;
1816 	if (disabled)
1817 		ctx->bypass = true;
1818 
1819 	IOMMU_DOMAIN_LOCK(iodom);
1820 	LIST_INSERT_HEAD(&domain->ctx_list, ctx, next);
1821 	IOMMU_DOMAIN_UNLOCK(iodom);
1822 
1823 	return (&ctx->ioctx);
1824 }
1825 
1826 static int
1827 smmu_ctx_init(device_t dev, struct iommu_ctx *ioctx)
1828 {
1829 	struct smmu_domain *domain;
1830 	struct iommu_domain *iodom;
1831 	struct smmu_softc *sc;
1832 	struct smmu_ctx *ctx;
1833 	devclass_t pci_class;
1834 	u_int sid;
1835 	int err;
1836 
1837 	ctx = (struct smmu_ctx *)ioctx;
1838 
1839 	sc = device_get_softc(dev);
1840 
1841 	domain = ctx->domain;
1842 	iodom = (struct iommu_domain *)domain;
1843 
1844 	pci_class = devclass_find("pci");
1845 	if (device_get_devclass(device_get_parent(ctx->dev)) == pci_class) {
1846 		err = smmu_pci_get_sid(ctx->dev, NULL, &sid);
1847 		if (err)
1848 			return (err);
1849 
1850 		ioctx->rid = pci_get_rid(dev);
1851 		ctx->sid = sid;
1852 		ctx->vendor = pci_get_vendor(ctx->dev);
1853 		ctx->device = pci_get_device(ctx->dev);
1854 	}
1855 
1856 	if (sc->features & SMMU_FEATURE_2_LVL_STREAM_TABLE) {
1857 		err = smmu_init_l1_entry(sc, ctx->sid);
1858 		if (err)
1859 			return (err);
1860 	}
1861 
1862 	/*
1863 	 * Neoverse N1 SDP:
1864 	 * 0x800 xhci
1865 	 * 0x700 re
1866 	 * 0x600 sata
1867 	 */
1868 
1869 	smmu_init_ste(sc, domain->cd, ctx->sid, ctx->bypass);
1870 
1871 	if (device_get_devclass(device_get_parent(ctx->dev)) == pci_class)
1872 		if (iommu_is_buswide_ctx(iodom->iommu, pci_get_bus(ctx->dev)))
1873 			smmu_set_buswide(dev, domain, ctx);
1874 
1875 	return (0);
1876 }
1877 
1878 static void
1879 smmu_ctx_free(device_t dev, struct iommu_ctx *ioctx)
1880 {
1881 	struct smmu_softc *sc;
1882 	struct smmu_ctx *ctx;
1883 
1884 	IOMMU_ASSERT_LOCKED(ioctx->domain->iommu);
1885 
1886 	sc = device_get_softc(dev);
1887 	ctx = (struct smmu_ctx *)ioctx;
1888 
1889 	smmu_deinit_ste(sc, ctx->sid);
1890 
1891 	LIST_REMOVE(ctx, next);
1892 
1893 	free(ctx, M_SMMU);
1894 }
1895 
1896 struct smmu_ctx *
1897 smmu_ctx_lookup_by_sid(device_t dev, u_int sid)
1898 {
1899 	struct smmu_softc *sc;
1900 	struct smmu_domain *domain;
1901 	struct smmu_unit *unit;
1902 	struct smmu_ctx *ctx;
1903 
1904 	sc = device_get_softc(dev);
1905 
1906 	unit = &sc->unit;
1907 
1908 	LIST_FOREACH(domain, &unit->domain_list, next) {
1909 		LIST_FOREACH(ctx, &domain->ctx_list, next) {
1910 			if (ctx->sid == sid)
1911 				return (ctx);
1912 		}
1913 	}
1914 
1915 	return (NULL);
1916 }
1917 
1918 static struct iommu_ctx *
1919 smmu_ctx_lookup(device_t dev, device_t child)
1920 {
1921 	struct iommu_unit *iommu __diagused;
1922 	struct smmu_softc *sc;
1923 	struct smmu_domain *domain;
1924 	struct smmu_unit *unit;
1925 	struct smmu_ctx *ctx;
1926 
1927 	sc = device_get_softc(dev);
1928 
1929 	unit = &sc->unit;
1930 	iommu = &unit->iommu;
1931 
1932 	IOMMU_ASSERT_LOCKED(iommu);
1933 
1934 	LIST_FOREACH(domain, &unit->domain_list, next) {
1935 		IOMMU_DOMAIN_LOCK(&domain->iodom);
1936 		LIST_FOREACH(ctx, &domain->ctx_list, next) {
1937 			if (ctx->dev == child) {
1938 				IOMMU_DOMAIN_UNLOCK(&domain->iodom);
1939 				return (&ctx->ioctx);
1940 			}
1941 		}
1942 		IOMMU_DOMAIN_UNLOCK(&domain->iodom);
1943 	}
1944 
1945 	return (NULL);
1946 }
1947 
1948 static int
1949 smmu_find(device_t dev, device_t child)
1950 {
1951 	struct smmu_softc *sc;
1952 	u_int xref;
1953 	int err;
1954 
1955 	sc = device_get_softc(dev);
1956 
1957 	err = smmu_pci_get_sid(child, &xref, NULL);
1958 	if (err)
1959 		return (ENOENT);
1960 
1961 	/* Check if xref is ours. */
1962 	if (xref != sc->xref)
1963 		return (EFAULT);
1964 
1965 	return (0);
1966 }
1967 
1968 #ifdef FDT
1969 static int
1970 smmu_ofw_md_data(device_t dev, struct iommu_ctx *ioctx, pcell_t *cells,
1971     int ncells)
1972 {
1973 	struct smmu_ctx *ctx;
1974 
1975 	ctx = (struct smmu_ctx *)ioctx;
1976 
1977 	if (ncells != 1)
1978 		return (-1);
1979 
1980 	ctx->sid = cells[0];
1981 
1982 	return (0);
1983 }
1984 #endif
1985 
1986 static device_method_t smmu_methods[] = {
1987 	/* Device interface */
1988 	DEVMETHOD(device_detach,	smmu_detach),
1989 
1990 	/* SMMU interface */
1991 	DEVMETHOD(iommu_find,		smmu_find),
1992 	DEVMETHOD(iommu_map,		smmu_map),
1993 	DEVMETHOD(iommu_unmap,		smmu_unmap),
1994 	DEVMETHOD(iommu_domain_alloc,	smmu_domain_alloc),
1995 	DEVMETHOD(iommu_domain_free,	smmu_domain_free),
1996 	DEVMETHOD(iommu_ctx_alloc,	smmu_ctx_alloc),
1997 	DEVMETHOD(iommu_ctx_init,	smmu_ctx_init),
1998 	DEVMETHOD(iommu_ctx_free,	smmu_ctx_free),
1999 	DEVMETHOD(iommu_ctx_lookup,	smmu_ctx_lookup),
2000 #ifdef FDT
2001 	DEVMETHOD(iommu_ofw_md_data,	smmu_ofw_md_data),
2002 #endif
2003 
2004 	/* Bus interface */
2005 	DEVMETHOD(bus_read_ivar,	smmu_read_ivar),
2006 
2007 	/* End */
2008 	DEVMETHOD_END
2009 };
2010 
2011 DEFINE_CLASS_0(smmu, smmu_driver, smmu_methods, sizeof(struct smmu_softc));
2012