2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2019-2020 Ruslan Bukin <br@bsdpad.com>
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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
36 * An incoming transaction from a peripheral device has an address, size,
37 * attributes and StreamID.
39 * In case of PCI-based devices, StreamID is a PCI rid.
41 * The StreamID is used to select a Stream Table Entry (STE) in a Stream table,
42 * which contains per-device configuration.
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
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.
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
56 * This driver enables S1 stage with standard AArch64 page tables.
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.
61 * An arm64 SoC could have more than one SMMU instance.
62 * ACPI IORT table describes which SMMU unit is assigned for a particular
67 * Register interface and Memory-based circular buffer queues are used
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.
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.)
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.
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).
90 #include "opt_platform.h"
93 #include <sys/cdefs.h>
94 __FBSDID("$FreeBSD$");
96 #include <sys/param.h>
97 #include <sys/bitstring.h>
99 #include <sys/kernel.h>
100 #include <sys/malloc.h>
101 #include <sys/rman.h>
102 #include <sys/lock.h>
103 #include <sys/tree.h>
104 #include <sys/taskqueue.h>
106 #include <vm/vm_page.h>
108 #include <contrib/dev/acpica/include/acpi.h>
109 #include <dev/acpica/acpivar.h>
111 #include <dev/pci/pcireg.h>
112 #include <dev/pci/pcivar.h>
113 #include <dev/iommu/iommu.h>
116 #include "iommu_if.h"
121 #define STRTAB_L1_SZ_SHIFT 20
122 #define STRTAB_SPLIT 8
124 #define STRTAB_L1_DESC_L2PTR_M (0x3fffffffffff << 6)
125 #define STRTAB_L1_DESC_DWORDS 1
127 #define STRTAB_STE_DWORDS 8
129 #define CMDQ_ENTRY_DWORDS 2
130 #define EVTQ_ENTRY_DWORDS 4
131 #define PRIQ_ENTRY_DWORDS 2
135 #define Q_WRP(q, p) ((p) & (1 << (q)->size_log2))
136 #define Q_IDX(q, p) ((p) & ((1 << (q)->size_log2) - 1))
137 #define Q_OVF(p) ((p) & (1 << 31)) /* Event queue overflowed */
139 #define SMMU_Q_ALIGN (64 * 1024)
141 static struct resource_spec smmu_spec[] = {
142 { SYS_RES_MEMORY, 0, RF_ACTIVE },
143 { SYS_RES_IRQ, 0, RF_ACTIVE },
144 { SYS_RES_IRQ, 1, RF_ACTIVE },
145 { SYS_RES_IRQ, 2, RF_ACTIVE },
149 MALLOC_DEFINE(M_SMMU, "SMMU", SMMU_DEVSTR);
151 #define dprintf(fmt, ...)
159 static struct smmu_event events[] = {
161 "Unsupported Upstream Transaction."},
162 { 0x02, "C_BAD_STREAMID",
163 "Transaction StreamID out of range."},
164 { 0x03, "F_STE_FETCH",
165 "Fetch of STE caused external abort."},
167 "Used STE invalid."},
168 { 0x05, "F_BAD_ATS_TREQ",
169 "Address Translation Request disallowed for a StreamID "
170 "and a PCIe ATS Translation Request received."},
171 { 0x06, "F_STREAM_DISABLED",
172 "The STE of a transaction marks non-substream transactions "
174 { 0x07, "F_TRANSL_FORBIDDEN",
175 "An incoming PCIe transaction is marked Translated but "
176 "SMMU bypass is disallowed for this StreamID."},
177 { 0x08, "C_BAD_SUBSTREAMID",
178 "Incoming SubstreamID present, but configuration is invalid."},
179 { 0x09, "F_CD_FETCH",
180 "Fetch of CD caused external abort."},
182 "Fetched CD invalid."},
183 { 0x0b, "F_WALK_EABT",
184 "An external abort occurred fetching (or updating) "
185 "a translation table descriptor."},
186 { 0x10, "F_TRANSLATION",
187 "Translation fault."},
188 { 0x11, "F_ADDR_SIZE",
189 "Address Size fault."},
191 "Access flag fault due to AF == 0 in a page or block TTD."},
192 { 0x13, "F_PERMISSION",
193 "Permission fault occurred on page access."},
194 { 0x20, "F_TLB_CONFLICT",
195 "A TLB conflict occurred because of the transaction."},
196 { 0x21, "F_CFG_CONFLICT",
197 "A configuration cache conflict occurred due to "
199 { 0x24, "E_PAGE_REQUEST",
200 "Speculative page request hint."},
201 { 0x25, "F_VMS_FETCH",
202 "Fetch of VMS caused external abort."},
207 smmu_q_has_space(struct smmu_queue *q)
211 * See 6.3.27 SMMU_CMDQ_PROD
213 * There is space in the queue for additional commands if:
214 * SMMU_CMDQ_CONS.RD != SMMU_CMDQ_PROD.WR ||
215 * SMMU_CMDQ_CONS.RD_WRAP == SMMU_CMDQ_PROD.WR_WRAP
218 if (Q_IDX(q, q->lc.cons) != Q_IDX(q, q->lc.prod) ||
219 Q_WRP(q, q->lc.cons) == Q_WRP(q, q->lc.prod))
226 smmu_q_empty(struct smmu_queue *q)
229 if (Q_IDX(q, q->lc.cons) == Q_IDX(q, q->lc.prod) &&
230 Q_WRP(q, q->lc.cons) == Q_WRP(q, q->lc.prod))
237 smmu_q_consumed(struct smmu_queue *q, uint32_t prod)
240 if ((Q_WRP(q, q->lc.cons) == Q_WRP(q, prod)) &&
241 (Q_IDX(q, q->lc.cons) >= Q_IDX(q, prod)))
244 if ((Q_WRP(q, q->lc.cons) != Q_WRP(q, prod)) &&
245 (Q_IDX(q, q->lc.cons) <= Q_IDX(q, prod)))
252 smmu_q_inc_cons(struct smmu_queue *q)
257 cons = (Q_WRP(q, q->lc.cons) | Q_IDX(q, q->lc.cons)) + 1;
258 val = (Q_OVF(q->lc.cons) | Q_WRP(q, cons) | Q_IDX(q, cons));
264 smmu_q_inc_prod(struct smmu_queue *q)
269 prod = (Q_WRP(q, q->lc.prod) | Q_IDX(q, q->lc.prod)) + 1;
270 val = (Q_OVF(q->lc.prod) | Q_WRP(q, prod) | Q_IDX(q, prod));
276 smmu_write_ack(struct smmu_softc *sc, uint32_t reg,
277 uint32_t reg_ack, uint32_t val)
284 bus_write_4(sc->res[0], reg, val);
287 v = bus_read_4(sc->res[0], reg_ack);
293 device_printf(sc->dev, "Failed to write reg.\n");
304 KASSERT(x > 0 && powerof2(x), ("%s: invalid arg %ld", __func__, x));
306 return (flsl(x) - 1);
310 smmu_init_queue(struct smmu_softc *sc, struct smmu_queue *q,
311 uint32_t prod_off, uint32_t cons_off, uint32_t dwords)
315 sz = (1 << q->size_log2) * dwords * 8;
317 /* Set up the command circular buffer */
318 q->vaddr = contigmalloc(sz, M_SMMU,
319 M_WAITOK | M_ZERO, 0, (1ul << 48) - 1, SMMU_Q_ALIGN, 0);
320 if (q->vaddr == NULL) {
321 device_printf(sc->dev, "failed to allocate %d bytes\n", sz);
325 q->prod_off = prod_off;
326 q->cons_off = cons_off;
327 q->paddr = vtophys(q->vaddr);
329 q->base = CMDQ_BASE_RA | EVENTQ_BASE_WA | PRIQ_BASE_WA;
330 q->base |= q->paddr & Q_BASE_ADDR_M;
331 q->base |= q->size_log2 << Q_LOG2SIZE_S;
337 smmu_init_queues(struct smmu_softc *sc)
342 err = smmu_init_queue(sc, &sc->cmdq,
343 SMMU_CMDQ_PROD, SMMU_CMDQ_CONS, CMDQ_ENTRY_DWORDS);
348 err = smmu_init_queue(sc, &sc->evtq,
349 SMMU_EVENTQ_PROD, SMMU_EVENTQ_CONS, EVTQ_ENTRY_DWORDS);
353 if (!(sc->features & SMMU_FEATURE_PRI))
357 err = smmu_init_queue(sc, &sc->priq,
358 SMMU_PRIQ_PROD, SMMU_PRIQ_CONS, PRIQ_ENTRY_DWORDS);
366 * Dump 2LVL or linear STE.
369 smmu_dump_ste(struct smmu_softc *sc, int sid)
371 struct smmu_strtab *strtab;
372 struct l1_desc *l1_desc;
376 strtab = &sc->strtab;
378 if (sc->features & SMMU_FEATURE_2_LVL_STREAM_TABLE) {
379 i = sid >> STRTAB_SPLIT;
380 l1 = (void *)((uint64_t)strtab->vaddr +
381 STRTAB_L1_DESC_DWORDS * 8 * i);
382 device_printf(sc->dev, "L1 ste == %lx\n", l1[0]);
384 l1_desc = &strtab->l1[i];
386 if (ste == NULL) /* L2 is not initialized */
389 ste = (void *)((uint64_t)strtab->vaddr +
390 sid * (STRTAB_STE_DWORDS << 3));
393 /* Dump L2 or linear STE. */
394 for (i = 0; i < STRTAB_STE_DWORDS; i++)
395 device_printf(sc->dev, "ste[%d] == %lx\n", i, ste[i]);
399 smmu_dump_cd(struct smmu_softc *sc, struct smmu_cd *cd)
404 device_printf(sc->dev, "%s\n", __func__);
407 for (i = 0; i < CD_DWORDS; i++)
408 device_printf(sc->dev, "cd[%d] == %lx\n", i, vaddr[i]);
412 smmu_evtq_dequeue(struct smmu_softc *sc, uint32_t *evt)
414 struct smmu_queue *evtq;
419 evtq->lc.val = bus_read_8(sc->res[0], evtq->prod_off);
420 entry_addr = (void *)((uint64_t)evtq->vaddr +
421 evtq->lc.cons * EVTQ_ENTRY_DWORDS * 8);
422 memcpy(evt, entry_addr, EVTQ_ENTRY_DWORDS * 8);
423 evtq->lc.cons = smmu_q_inc_cons(evtq);
424 bus_write_4(sc->res[0], evtq->cons_off, evtq->lc.cons);
428 smmu_print_event(struct smmu_softc *sc, uint32_t *evt)
430 struct smmu_event *ev;
431 uintptr_t input_addr;
440 event_id = evt[0] & 0xff;
441 for (i = 0; events[i].ident != 0; i++) {
442 if (events[i].ident == event_id) {
451 input_addr |= evt[4];
453 if (smmu_quirks_check(dev, sid, event_id, input_addr)) {
454 /* The event is known. Don't print anything. */
459 device_printf(sc->dev,
460 "Event %s (%s) received.\n", ev->str, ev->msg);
462 device_printf(sc->dev, "Event 0x%x received\n", event_id);
464 device_printf(sc->dev, "SID %x, Input Address: %jx\n",
467 for (i = 0; i < 8; i++)
468 device_printf(sc->dev, "evt[%d] %x\n", i, evt[i]);
470 smmu_dump_ste(sc, sid);
474 make_cmd(struct smmu_softc *sc, uint64_t *cmd,
475 struct smmu_cmdq_entry *entry)
478 memset(cmd, 0, CMDQ_ENTRY_DWORDS * 8);
479 cmd[0] = entry->opcode << CMD_QUEUE_OPCODE_S;
481 switch (entry->opcode) {
483 cmd[0] |= (uint64_t)entry->tlbi.asid << TLBI_0_ASID_S;
484 cmd[1] = entry->tlbi.addr & TLBI_1_ADDR_M;
485 if (entry->tlbi.leaf) {
487 * Leaf flag means that only cached entries
488 * for the last level of translation table walk
489 * are required to be invalidated.
491 cmd[1] |= TLBI_1_LEAF;
494 case CMD_TLBI_NH_ASID:
495 cmd[0] |= (uint64_t)entry->tlbi.asid << TLBI_0_ASID_S;
497 case CMD_TLBI_NSNH_ALL:
498 case CMD_TLBI_NH_ALL:
499 case CMD_TLBI_EL2_ALL:
502 cmd[0] |= ((uint64_t)entry->cfgi.ssid << CFGI_0_SSID_S);
505 cmd[0] |= ((uint64_t)entry->cfgi.sid << CFGI_0_STE_SID_S);
506 cmd[1] |= ((uint64_t)entry->cfgi.leaf << CFGI_1_LEAF_S);
508 case CMD_CFGI_STE_RANGE:
509 cmd[1] = (31 << CFGI_1_STE_RANGE_S);
512 cmd[0] |= SYNC_0_MSH_IS | SYNC_0_MSIATTR_OIWB;
513 if (entry->sync.msiaddr) {
514 cmd[0] |= SYNC_0_CS_SIG_IRQ;
515 cmd[1] |= (entry->sync.msiaddr & SYNC_1_MSIADDRESS_M);
517 cmd[0] |= SYNC_0_CS_SIG_SEV;
519 case CMD_PREFETCH_CONFIG:
520 cmd[0] |= ((uint64_t)entry->prefetch.sid << PREFETCH_0_SID_S);
526 smmu_cmdq_enqueue_cmd(struct smmu_softc *sc, struct smmu_cmdq_entry *entry)
528 uint64_t cmd[CMDQ_ENTRY_DWORDS];
529 struct smmu_queue *cmdq;
534 make_cmd(sc, cmd, entry);
538 /* Ensure that a space is available. */
540 cmdq->lc.cons = bus_read_4(sc->res[0], cmdq->cons_off);
541 } while (smmu_q_has_space(cmdq) == 0);
543 /* Write the command to the current prod entry. */
544 entry_addr = (void *)((uint64_t)cmdq->vaddr +
545 Q_IDX(cmdq, cmdq->lc.prod) * CMDQ_ENTRY_DWORDS * 8);
546 memcpy(entry_addr, cmd, CMDQ_ENTRY_DWORDS * 8);
548 /* Increment prod index. */
549 cmdq->lc.prod = smmu_q_inc_prod(cmdq);
550 bus_write_4(sc->res[0], cmdq->prod_off, cmdq->lc.prod);
556 smmu_poll_until_consumed(struct smmu_softc *sc, struct smmu_queue *q)
560 q->lc.val = bus_read_8(sc->res[0], q->prod_off);
568 smmu_sync(struct smmu_softc *sc)
570 struct smmu_cmdq_entry cmd;
571 struct smmu_queue *q;
579 /* Enqueue sync command. */
580 cmd.opcode = CMD_SYNC;
581 cmd.sync.msiaddr = q->paddr + Q_IDX(q, prod) * CMDQ_ENTRY_DWORDS * 8;
582 smmu_cmdq_enqueue_cmd(sc, &cmd);
584 /* Wait for the sync completion. */
585 base = (void *)((uint64_t)q->vaddr +
586 Q_IDX(q, prod) * CMDQ_ENTRY_DWORDS * 8);
589 * It takes around 200 loops (6 instructions each)
590 * on Neoverse N1 to complete the sync.
596 /* MSI write completed. */
603 device_printf(sc->dev, "Failed to sync\n");
609 smmu_sync_cd(struct smmu_softc *sc, int sid, int ssid, bool leaf)
611 struct smmu_cmdq_entry cmd;
613 cmd.opcode = CMD_CFGI_CD;
615 cmd.cfgi.ssid = ssid;
616 cmd.cfgi.leaf = leaf;
617 smmu_cmdq_enqueue_cmd(sc, &cmd);
623 smmu_invalidate_all_sid(struct smmu_softc *sc)
625 struct smmu_cmdq_entry cmd;
627 /* Invalidate cached config */
628 cmd.opcode = CMD_CFGI_STE_RANGE;
629 smmu_cmdq_enqueue_cmd(sc, &cmd);
634 smmu_tlbi_all(struct smmu_softc *sc)
636 struct smmu_cmdq_entry cmd;
638 /* Invalidate entire TLB */
639 cmd.opcode = CMD_TLBI_NSNH_ALL;
640 smmu_cmdq_enqueue_cmd(sc, &cmd);
645 smmu_tlbi_asid(struct smmu_softc *sc, uint16_t asid)
647 struct smmu_cmdq_entry cmd;
649 /* Invalidate TLB for an ASID. */
650 cmd.opcode = CMD_TLBI_NH_ASID;
651 cmd.tlbi.asid = asid;
652 smmu_cmdq_enqueue_cmd(sc, &cmd);
657 smmu_tlbi_va(struct smmu_softc *sc, vm_offset_t va, uint16_t asid)
659 struct smmu_cmdq_entry cmd;
661 /* Invalidate specific range */
662 cmd.opcode = CMD_TLBI_NH_VA;
663 cmd.tlbi.asid = asid;
665 cmd.tlbi.leaf = true; /* We change only L3. */
667 smmu_cmdq_enqueue_cmd(sc, &cmd);
671 smmu_invalidate_sid(struct smmu_softc *sc, uint32_t sid)
673 struct smmu_cmdq_entry cmd;
675 /* Invalidate cached config */
676 cmd.opcode = CMD_CFGI_STE;
678 smmu_cmdq_enqueue_cmd(sc, &cmd);
683 smmu_prefetch_sid(struct smmu_softc *sc, uint32_t sid)
685 struct smmu_cmdq_entry cmd;
687 cmd.opcode = CMD_PREFETCH_CONFIG;
688 cmd.prefetch.sid = sid;
689 smmu_cmdq_enqueue_cmd(sc, &cmd);
694 * Init STE in bypass mode. Traffic is not translated for the sid.
697 smmu_init_ste_bypass(struct smmu_softc *sc, uint32_t sid, uint64_t *ste)
701 val = STE0_VALID | STE0_CONFIG_BYPASS;
703 ste[1] = STE1_SHCFG_INCOMING | STE1_EATS_FULLATS;
711 smmu_invalidate_sid(sc, sid);
714 smmu_invalidate_sid(sc, sid);
716 smmu_prefetch_sid(sc, sid);
720 * Enable Stage1 (S1) translation for the sid.
723 smmu_init_ste_s1(struct smmu_softc *sc, struct smmu_cd *cd,
724 uint32_t sid, uint64_t *ste)
731 ste[1] = STE1_EATS_FULLATS |
743 if (sc->features & SMMU_FEATURE_STALL &&
744 ((sc->features & SMMU_FEATURE_STALL_FORCE) == 0))
745 ste[1] |= STE1_S1STALLD;
748 val |= (cd->paddr & STE0_S1CONTEXTPTR_M);
749 val |= STE0_CONFIG_S1_TRANS;
751 smmu_invalidate_sid(sc, sid);
753 /* The STE[0] has to be written in a single blast, last of all. */
757 smmu_invalidate_sid(sc, sid);
758 smmu_sync_cd(sc, sid, 0, true);
759 smmu_invalidate_sid(sc, sid);
761 /* The sid will be used soon most likely. */
762 smmu_prefetch_sid(sc, sid);
768 smmu_init_ste(struct smmu_softc *sc, struct smmu_cd *cd, int sid, bool bypass)
770 struct smmu_strtab *strtab;
771 struct l1_desc *l1_desc;
774 strtab = &sc->strtab;
776 if (sc->features & SMMU_FEATURE_2_LVL_STREAM_TABLE) {
777 l1_desc = &strtab->l1[sid >> STRTAB_SPLIT];
779 addr += (sid & ((1 << STRTAB_SPLIT) - 1)) * STRTAB_STE_DWORDS;
781 addr = (void *)((uint64_t)strtab->vaddr +
782 STRTAB_STE_DWORDS * 8 * sid);
786 smmu_init_ste_bypass(sc, sid, addr);
788 smmu_init_ste_s1(sc, cd, sid, addr);
796 smmu_init_cd(struct smmu_softc *sc, struct smmu_domain *domain)
805 size = 1 * (CD_DWORDS << 3);
808 cd = domain->cd = malloc(sizeof(struct smmu_cd),
809 M_SMMU, M_WAITOK | M_ZERO);
811 cd->vaddr = contigmalloc(size, M_SMMU,
812 M_WAITOK | M_ZERO, /* flags */
814 (1ul << 40) - 1, /* high */
815 size, /* alignment */
817 if (cd->vaddr == NULL) {
818 device_printf(sc->dev, "Failed to allocate CD\n");
823 cd->paddr = vtophys(cd->vaddr);
832 val |= (uint64_t)domain->asid << CD0_ASID_S;
834 val |= CD0_EPD1; /* Disable TT1 */
835 val |= ((64 - sc->ias) << CD0_T0SZ_S);
836 val |= CD0_IPS_48BITS;
838 paddr = p->pm_l0_paddr & CD1_TTB0_M;
839 KASSERT(paddr == p->pm_l0_paddr, ("bad allocation 1"));
843 ptr[3] = MAIR_ATTR(MAIR_DEVICE_nGnRnE, VM_MEMATTR_DEVICE) |
844 MAIR_ATTR(MAIR_NORMAL_NC, VM_MEMATTR_UNCACHEABLE) |
845 MAIR_ATTR(MAIR_NORMAL_WB, VM_MEMATTR_WRITE_BACK) |
846 MAIR_ATTR(MAIR_NORMAL_WT, VM_MEMATTR_WRITE_THROUGH);
848 /* Install the CD. */
855 smmu_init_strtab_linear(struct smmu_softc *sc)
857 struct smmu_strtab *strtab;
862 strtab = &sc->strtab;
863 strtab->num_l1_entries = (1 << sc->sid_bits);
865 size = strtab->num_l1_entries * (STRTAB_STE_DWORDS << 3);
868 device_printf(sc->dev,
869 "%s: linear strtab size %d, num_l1_entries %d\n",
870 __func__, size, strtab->num_l1_entries);
872 strtab->vaddr = contigmalloc(size, M_SMMU,
873 M_WAITOK | M_ZERO, /* flags */
875 (1ul << 48) - 1, /* high */
876 size, /* alignment */
878 if (strtab->vaddr == NULL) {
879 device_printf(sc->dev, "failed to allocate strtab\n");
883 reg = STRTAB_BASE_CFG_FMT_LINEAR;
884 reg |= sc->sid_bits << STRTAB_BASE_CFG_LOG2SIZE_S;
885 strtab->base_cfg = (uint32_t)reg;
887 base = vtophys(strtab->vaddr);
889 reg = base & STRTAB_BASE_ADDR_M;
890 KASSERT(reg == base, ("bad allocation 2"));
891 reg |= STRTAB_BASE_RA;
898 smmu_init_strtab_2lvl(struct smmu_softc *sc)
900 struct smmu_strtab *strtab;
908 strtab = &sc->strtab;
910 size = STRTAB_L1_SZ_SHIFT - (ilog2(STRTAB_L1_DESC_DWORDS) + 3);
911 size = min(size, sc->sid_bits - STRTAB_SPLIT);
912 strtab->num_l1_entries = (1 << size);
913 size += STRTAB_SPLIT;
915 l1size = strtab->num_l1_entries * (STRTAB_L1_DESC_DWORDS << 3);
918 device_printf(sc->dev,
919 "%s: size %d, l1 entries %d, l1size %d\n",
920 __func__, size, strtab->num_l1_entries, l1size);
922 strtab->vaddr = contigmalloc(l1size, M_SMMU,
923 M_WAITOK | M_ZERO, /* flags */
925 (1ul << 48) - 1, /* high */
926 l1size, /* alignment */
928 if (strtab->vaddr == NULL) {
929 device_printf(sc->dev, "Failed to allocate 2lvl strtab.\n");
933 sz = strtab->num_l1_entries * sizeof(struct l1_desc);
935 strtab->l1 = malloc(sz, M_SMMU, M_WAITOK | M_ZERO);
936 if (strtab->l1 == NULL) {
937 contigfree(strtab->vaddr, l1size, M_SMMU);
941 reg = STRTAB_BASE_CFG_FMT_2LVL;
942 reg |= size << STRTAB_BASE_CFG_LOG2SIZE_S;
943 reg |= STRTAB_SPLIT << STRTAB_BASE_CFG_SPLIT_S;
944 strtab->base_cfg = (uint32_t)reg;
946 base = vtophys(strtab->vaddr);
948 reg_base = base & STRTAB_BASE_ADDR_M;
949 KASSERT(reg_base == base, ("bad allocation 3"));
950 reg_base |= STRTAB_BASE_RA;
951 strtab->base = reg_base;
957 smmu_init_strtab(struct smmu_softc *sc)
961 if (sc->features & SMMU_FEATURE_2_LVL_STREAM_TABLE)
962 error = smmu_init_strtab_2lvl(sc);
964 error = smmu_init_strtab_linear(sc);
970 smmu_init_l1_entry(struct smmu_softc *sc, int sid)
972 struct smmu_strtab *strtab;
973 struct l1_desc *l1_desc;
979 strtab = &sc->strtab;
980 l1_desc = &strtab->l1[sid >> STRTAB_SPLIT];
982 size = 1 << (STRTAB_SPLIT + ilog2(STRTAB_STE_DWORDS) + 3);
984 l1_desc->span = STRTAB_SPLIT + 1;
985 l1_desc->size = size;
986 l1_desc->va = contigmalloc(size, M_SMMU,
987 M_WAITOK | M_ZERO, /* flags */
989 (1ul << 48) - 1, /* high */
990 size, /* alignment */
992 if (l1_desc->va == NULL) {
993 device_printf(sc->dev, "failed to allocate l2 entry\n");
997 l1_desc->pa = vtophys(l1_desc->va);
999 i = sid >> STRTAB_SPLIT;
1000 addr = (void *)((uint64_t)strtab->vaddr +
1001 STRTAB_L1_DESC_DWORDS * 8 * i);
1003 /* Install the L1 entry. */
1004 val = l1_desc->pa & STRTAB_L1_DESC_L2PTR_M;
1005 KASSERT(val == l1_desc->pa, ("bad allocation 4"));
1006 val |= l1_desc->span;
1013 smmu_deinit_l1_entry(struct smmu_softc *sc, int sid)
1015 struct smmu_strtab *strtab;
1016 struct l1_desc *l1_desc;
1020 strtab = &sc->strtab;
1022 i = sid >> STRTAB_SPLIT;
1023 addr = (void *)((uint64_t)strtab->vaddr +
1024 STRTAB_L1_DESC_DWORDS * 8 * i);
1027 if (sc->features & SMMU_FEATURE_2_LVL_STREAM_TABLE) {
1028 l1_desc = &strtab->l1[sid >> STRTAB_SPLIT];
1029 contigfree(l1_desc->va, l1_desc->size, M_SMMU);
1034 smmu_disable(struct smmu_softc *sc)
1040 reg = bus_read_4(sc->res[0], SMMU_CR0);
1042 error = smmu_write_ack(sc, SMMU_CR0, SMMU_CR0ACK, reg);
1044 device_printf(sc->dev, "Could not disable SMMU.\n");
1050 smmu_event_intr(void *arg)
1052 uint32_t evt[EVTQ_ENTRY_DWORDS * 2];
1053 struct smmu_softc *sc;
1058 smmu_evtq_dequeue(sc, evt);
1059 smmu_print_event(sc, evt);
1060 } while (!smmu_q_empty(&sc->evtq));
1062 return (FILTER_HANDLED);
1066 smmu_sync_intr(void *arg)
1068 struct smmu_softc *sc;
1072 device_printf(sc->dev, "%s\n", __func__);
1074 return (FILTER_HANDLED);
1078 smmu_gerr_intr(void *arg)
1080 struct smmu_softc *sc;
1084 device_printf(sc->dev, "SMMU Global Error\n");
1086 return (FILTER_HANDLED);
1090 smmu_enable_interrupts(struct smmu_softc *sc)
1096 bus_write_8(sc->res[0], SMMU_GERROR_IRQ_CFG0, 0);
1097 bus_write_4(sc->res[0], SMMU_GERROR_IRQ_CFG1, 0);
1098 bus_write_4(sc->res[0], SMMU_GERROR_IRQ_CFG2, 0);
1100 bus_write_8(sc->res[0], SMMU_EVENTQ_IRQ_CFG0, 0);
1101 bus_write_4(sc->res[0], SMMU_EVENTQ_IRQ_CFG1, 0);
1102 bus_write_4(sc->res[0], SMMU_EVENTQ_IRQ_CFG2, 0);
1104 if (sc->features & CR0_PRIQEN) {
1105 bus_write_8(sc->res[0], SMMU_PRIQ_IRQ_CFG0, 0);
1106 bus_write_4(sc->res[0], SMMU_PRIQ_IRQ_CFG1, 0);
1107 bus_write_4(sc->res[0], SMMU_PRIQ_IRQ_CFG2, 0);
1110 /* Disable any interrupts. */
1111 error = smmu_write_ack(sc, SMMU_IRQ_CTRL, SMMU_IRQ_CTRLACK, 0);
1113 device_printf(sc->dev, "Could not disable interrupts.\n");
1117 /* Enable interrupts. */
1118 reg = IRQ_CTRL_EVENTQ_IRQEN | IRQ_CTRL_GERROR_IRQEN;
1119 if (sc->features & SMMU_FEATURE_PRI)
1120 reg |= IRQ_CTRL_PRIQ_IRQEN;
1122 error = smmu_write_ack(sc, SMMU_IRQ_CTRL, SMMU_IRQ_CTRLACK, reg);
1124 device_printf(sc->dev, "Could not enable interrupts.\n");
1133 smmu_configure_intr(struct smmu_softc *sc, struct resource *res)
1135 struct intr_map_data_acpi *ad;
1136 struct intr_map_data *data;
1138 data = rman_get_virtual(res);
1139 KASSERT(data != NULL, ("data is NULL"));
1141 if (data->type == INTR_MAP_DATA_ACPI) {
1142 ad = (struct intr_map_data_acpi *)data;
1143 ad->trig = INTR_TRIGGER_EDGE;
1144 ad->pol = INTR_POLARITY_HIGH;
1150 smmu_setup_interrupts(struct smmu_softc *sc)
1159 * Configure SMMU interrupts as EDGE triggered manually
1160 * as ACPI tables carries no information for that.
1162 smmu_configure_intr(sc, sc->res[1]);
1163 smmu_configure_intr(sc, sc->res[2]);
1164 smmu_configure_intr(sc, sc->res[3]);
1167 error = bus_setup_intr(dev, sc->res[1], INTR_TYPE_MISC,
1168 smmu_event_intr, NULL, sc, &sc->intr_cookie[0]);
1170 device_printf(dev, "Couldn't setup Event interrupt handler\n");
1174 error = bus_setup_intr(dev, sc->res[3], INTR_TYPE_MISC,
1175 smmu_gerr_intr, NULL, sc, &sc->intr_cookie[2]);
1177 device_printf(dev, "Couldn't setup Gerr interrupt handler\n");
1185 smmu_reset(struct smmu_softc *sc)
1187 struct smmu_cmdq_entry cmd;
1188 struct smmu_strtab *strtab;
1192 reg = bus_read_4(sc->res[0], SMMU_CR0);
1194 if (reg & CR0_SMMUEN)
1195 device_printf(sc->dev,
1196 "%s: Warning: SMMU is enabled\n", __func__);
1198 error = smmu_disable(sc);
1200 device_printf(sc->dev,
1201 "%s: Could not disable SMMU.\n", __func__);
1203 if (smmu_enable_interrupts(sc) != 0) {
1204 device_printf(sc->dev, "Could not enable interrupts.\n");
1208 reg = CR1_TABLE_SH_IS |
1214 bus_write_4(sc->res[0], SMMU_CR1, reg);
1216 reg = CR2_PTM | CR2_RECINVSID | CR2_E2H;
1217 bus_write_4(sc->res[0], SMMU_CR2, reg);
1220 strtab = &sc->strtab;
1221 bus_write_8(sc->res[0], SMMU_STRTAB_BASE, strtab->base);
1222 bus_write_4(sc->res[0], SMMU_STRTAB_BASE_CFG, strtab->base_cfg);
1224 /* Command queue. */
1225 bus_write_8(sc->res[0], SMMU_CMDQ_BASE, sc->cmdq.base);
1226 bus_write_4(sc->res[0], SMMU_CMDQ_PROD, sc->cmdq.lc.prod);
1227 bus_write_4(sc->res[0], SMMU_CMDQ_CONS, sc->cmdq.lc.cons);
1230 error = smmu_write_ack(sc, SMMU_CR0, SMMU_CR0ACK, reg);
1232 device_printf(sc->dev, "Could not enable command queue\n");
1236 /* Invalidate cached configuration. */
1237 smmu_invalidate_all_sid(sc);
1239 if (sc->features & SMMU_FEATURE_HYP) {
1240 cmd.opcode = CMD_TLBI_EL2_ALL;
1241 smmu_cmdq_enqueue_cmd(sc, &cmd);
1244 /* Invalidate TLB. */
1248 bus_write_8(sc->res[0], SMMU_EVENTQ_BASE, sc->evtq.base);
1249 bus_write_4(sc->res[0], SMMU_EVENTQ_PROD, sc->evtq.lc.prod);
1250 bus_write_4(sc->res[0], SMMU_EVENTQ_CONS, sc->evtq.lc.cons);
1252 reg |= CR0_EVENTQEN;
1253 error = smmu_write_ack(sc, SMMU_CR0, SMMU_CR0ACK, reg);
1255 device_printf(sc->dev, "Could not enable event queue\n");
1259 if (sc->features & SMMU_FEATURE_PRI) {
1261 bus_write_8(sc->res[0], SMMU_PRIQ_BASE, sc->priq.base);
1262 bus_write_4(sc->res[0], SMMU_PRIQ_PROD, sc->priq.lc.prod);
1263 bus_write_4(sc->res[0], SMMU_PRIQ_CONS, sc->priq.lc.cons);
1266 error = smmu_write_ack(sc, SMMU_CR0, SMMU_CR0ACK, reg);
1268 device_printf(sc->dev, "Could not enable PRI queue\n");
1273 if (sc->features & SMMU_FEATURE_ATS) {
1275 error = smmu_write_ack(sc, SMMU_CR0, SMMU_CR0ACK, reg);
1277 device_printf(sc->dev, "Could not enable ATS check.\n");
1283 error = smmu_write_ack(sc, SMMU_CR0, SMMU_CR0ACK, reg);
1285 device_printf(sc->dev, "Could not enable SMMU.\n");
1293 smmu_check_features(struct smmu_softc *sc)
1300 reg = bus_read_4(sc->res[0], SMMU_IDR0);
1302 if (reg & IDR0_ST_LVL_2) {
1304 device_printf(sc->dev,
1305 "2-level stream table supported.\n");
1306 sc->features |= SMMU_FEATURE_2_LVL_STREAM_TABLE;
1309 if (reg & IDR0_CD2L) {
1311 device_printf(sc->dev,
1312 "2-level CD table supported.\n");
1313 sc->features |= SMMU_FEATURE_2_LVL_CD;
1316 switch (reg & IDR0_TTENDIAN_M) {
1317 case IDR0_TTENDIAN_MIXED:
1319 device_printf(sc->dev, "Mixed endianess supported.\n");
1320 sc->features |= SMMU_FEATURE_TT_LE;
1321 sc->features |= SMMU_FEATURE_TT_BE;
1323 case IDR0_TTENDIAN_LITTLE:
1325 device_printf(sc->dev,
1326 "Little endian supported only.\n");
1327 sc->features |= SMMU_FEATURE_TT_LE;
1329 case IDR0_TTENDIAN_BIG:
1331 device_printf(sc->dev, "Big endian supported only.\n");
1332 sc->features |= SMMU_FEATURE_TT_BE;
1335 device_printf(sc->dev, "Unsupported endianness.\n");
1340 sc->features |= SMMU_FEATURE_SEV;
1342 if (reg & IDR0_MSI) {
1344 device_printf(sc->dev, "MSI feature present.\n");
1345 sc->features |= SMMU_FEATURE_MSI;
1348 if (reg & IDR0_HYP) {
1350 device_printf(sc->dev, "HYP feature present.\n");
1351 sc->features |= SMMU_FEATURE_HYP;
1355 sc->features |= SMMU_FEATURE_ATS;
1358 sc->features |= SMMU_FEATURE_PRI;
1360 switch (reg & IDR0_STALL_MODEL_M) {
1361 case IDR0_STALL_MODEL_FORCE:
1362 /* Stall is forced. */
1363 sc->features |= SMMU_FEATURE_STALL_FORCE;
1365 case IDR0_STALL_MODEL_STALL:
1366 sc->features |= SMMU_FEATURE_STALL;
1370 /* Grab translation stages supported. */
1371 if (reg & IDR0_S1P) {
1373 device_printf(sc->dev,
1374 "Stage 1 translation supported.\n");
1375 sc->features |= SMMU_FEATURE_S1P;
1377 if (reg & IDR0_S2P) {
1379 device_printf(sc->dev,
1380 "Stage 2 translation supported.\n");
1381 sc->features |= SMMU_FEATURE_S2P;
1384 switch (reg & IDR0_TTF_M) {
1390 device_printf(sc->dev, "No AArch64 table format support.\n");
1394 if (reg & IDR0_ASID16)
1400 device_printf(sc->dev, "ASID bits %d\n", sc->asid_bits);
1402 if (reg & IDR0_VMID16)
1407 reg = bus_read_4(sc->res[0], SMMU_IDR1);
1409 if (reg & (IDR1_TABLES_PRESET | IDR1_QUEUES_PRESET | IDR1_REL)) {
1410 device_printf(sc->dev,
1411 "Embedded implementations not supported by this driver.\n");
1415 val = (reg & IDR1_CMDQS_M) >> IDR1_CMDQS_S;
1416 sc->cmdq.size_log2 = val;
1418 device_printf(sc->dev, "CMD queue bits %d\n", val);
1420 val = (reg & IDR1_EVENTQS_M) >> IDR1_EVENTQS_S;
1421 sc->evtq.size_log2 = val;
1423 device_printf(sc->dev, "EVENT queue bits %d\n", val);
1425 if (sc->features & SMMU_FEATURE_PRI) {
1426 val = (reg & IDR1_PRIQS_M) >> IDR1_PRIQS_S;
1427 sc->priq.size_log2 = val;
1429 device_printf(sc->dev, "PRI queue bits %d\n", val);
1432 sc->ssid_bits = (reg & IDR1_SSIDSIZE_M) >> IDR1_SSIDSIZE_S;
1433 sc->sid_bits = (reg & IDR1_SIDSIZE_M) >> IDR1_SIDSIZE_S;
1435 if (sc->sid_bits <= STRTAB_SPLIT)
1436 sc->features &= ~SMMU_FEATURE_2_LVL_STREAM_TABLE;
1439 device_printf(sc->dev, "SSID bits %d\n", sc->ssid_bits);
1440 device_printf(sc->dev, "SID bits %d\n", sc->sid_bits);
1444 reg = bus_read_4(sc->res[0], SMMU_IDR3);
1446 sc->features |= SMMU_FEATURE_RANGE_INV;
1449 reg = bus_read_4(sc->res[0], SMMU_IDR5);
1451 switch (reg & IDR5_OAS_M) {
1476 if (reg & IDR5_GRAN64K)
1477 sc->pgsizes |= 64 * 1024;
1478 if (reg & IDR5_GRAN16K)
1479 sc->pgsizes |= 16 * 1024;
1480 if (reg & IDR5_GRAN4K)
1481 sc->pgsizes |= 4 * 1024;
1483 if ((reg & IDR5_VAX_M) == IDR5_VAX_52)
1484 sc->features |= SMMU_FEATURE_VAX;
1490 smmu_init_asids(struct smmu_softc *sc)
1493 sc->asid_set_size = (1 << sc->asid_bits);
1494 sc->asid_set = bit_alloc(sc->asid_set_size, M_SMMU, M_WAITOK);
1495 mtx_init(&sc->asid_set_mutex, "asid set", NULL, MTX_SPIN);
1499 smmu_asid_alloc(struct smmu_softc *sc, int *new_asid)
1502 mtx_lock_spin(&sc->asid_set_mutex);
1503 bit_ffc(sc->asid_set, sc->asid_set_size, new_asid);
1504 if (*new_asid == -1) {
1505 mtx_unlock_spin(&sc->asid_set_mutex);
1508 bit_set(sc->asid_set, *new_asid);
1509 mtx_unlock_spin(&sc->asid_set_mutex);
1515 smmu_asid_free(struct smmu_softc *sc, int asid)
1518 mtx_lock_spin(&sc->asid_set_mutex);
1519 bit_clear(sc->asid_set, asid);
1520 mtx_unlock_spin(&sc->asid_set_mutex);
1527 smmu_attach(device_t dev)
1529 struct smmu_softc *sc;
1532 sc = device_get_softc(dev);
1535 mtx_init(&sc->sc_mtx, device_get_nameunit(sc->dev), "smmu", MTX_DEF);
1537 error = bus_alloc_resources(dev, smmu_spec, sc->res);
1539 device_printf(dev, "Couldn't allocate resources.\n");
1543 error = smmu_setup_interrupts(sc);
1545 bus_release_resources(dev, smmu_spec, sc->res);
1549 error = smmu_check_features(sc);
1551 device_printf(dev, "Some features are required "
1552 "but not supported by hardware.\n");
1556 smmu_init_asids(sc);
1558 error = smmu_init_queues(sc);
1560 device_printf(dev, "Couldn't allocate queues.\n");
1564 error = smmu_init_strtab(sc);
1566 device_printf(dev, "Couldn't allocate strtab.\n");
1570 error = smmu_reset(sc);
1572 device_printf(dev, "Couldn't reset SMMU.\n");
1580 smmu_detach(device_t dev)
1582 struct smmu_softc *sc;
1584 sc = device_get_softc(dev);
1586 bus_release_resources(dev, smmu_spec, sc->res);
1592 smmu_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
1594 struct smmu_softc *sc;
1596 sc = device_get_softc(dev);
1598 device_printf(sc->dev, "%s\n", __func__);
1604 smmu_unmap(device_t dev, struct iommu_domain *iodom,
1605 vm_offset_t va, bus_size_t size)
1607 struct smmu_domain *domain;
1608 struct smmu_softc *sc;
1612 sc = device_get_softc(dev);
1614 domain = (struct smmu_domain *)iodom;
1618 dprintf("%s: %lx, %ld, domain %d\n", __func__, va, size, domain->asid);
1620 for (i = 0; i < size; i += PAGE_SIZE) {
1621 if (pmap_sremove(&domain->p, va) == 0) {
1622 /* pmap entry removed, invalidate TLB. */
1623 smmu_tlbi_va(sc, va, domain->asid);
1637 smmu_map(device_t dev, struct iommu_domain *iodom,
1638 vm_offset_t va, vm_page_t *ma, vm_size_t size,
1641 struct smmu_domain *domain;
1642 struct smmu_softc *sc;
1647 sc = device_get_softc(dev);
1649 domain = (struct smmu_domain *)iodom;
1651 dprintf("%s: %lx -> %lx, %ld, domain %d\n", __func__, va, pa, size,
1654 for (i = 0; size > 0; size -= PAGE_SIZE) {
1655 pa = VM_PAGE_TO_PHYS(ma[i++]);
1656 error = pmap_senter(&domain->p, va, pa, prot, 0);
1659 smmu_tlbi_va(sc, va, domain->asid);
1668 static struct iommu_domain *
1669 smmu_domain_alloc(device_t dev, struct iommu_unit *iommu)
1671 struct smmu_domain *domain;
1672 struct smmu_unit *unit;
1673 struct smmu_softc *sc;
1677 sc = device_get_softc(dev);
1679 unit = (struct smmu_unit *)iommu;
1681 domain = malloc(sizeof(*domain), M_SMMU, M_WAITOK | M_ZERO);
1683 error = smmu_asid_alloc(sc, &new_asid);
1685 free(domain, M_SMMU);
1686 device_printf(sc->dev,
1687 "Could not allocate ASID for a new domain.\n");
1691 domain->asid = (uint16_t)new_asid;
1693 pmap_pinit(&domain->p);
1694 PMAP_LOCK_INIT(&domain->p);
1696 error = smmu_init_cd(sc, domain);
1698 free(domain, M_SMMU);
1699 device_printf(sc->dev, "Could not initialize CD\n");
1703 smmu_tlbi_asid(sc, domain->asid);
1705 LIST_INIT(&domain->ctx_list);
1708 LIST_INSERT_HEAD(&unit->domain_list, domain, next);
1709 IOMMU_UNLOCK(iommu);
1711 return (&domain->iodom);
1715 smmu_domain_free(device_t dev, struct iommu_domain *iodom)
1717 struct smmu_domain *domain;
1718 struct smmu_softc *sc;
1721 sc = device_get_softc(dev);
1723 domain = (struct smmu_domain *)iodom;
1725 LIST_REMOVE(domain, next);
1729 pmap_sremove_pages(&domain->p);
1730 pmap_release(&domain->p);
1732 smmu_tlbi_asid(sc, domain->asid);
1733 smmu_asid_free(sc, domain->asid);
1735 contigfree(cd->vaddr, cd->size, M_SMMU);
1738 free(domain, M_SMMU);
1742 smmu_set_buswide(device_t dev, struct smmu_domain *domain,
1743 struct smmu_ctx *ctx)
1745 struct smmu_softc *sc;
1748 sc = device_get_softc(dev);
1750 for (i = 0; i < PCI_SLOTMAX; i++)
1751 smmu_init_ste(sc, domain->cd, (ctx->sid | i), ctx->bypass);
1756 static struct iommu_ctx *
1757 smmu_ctx_alloc(device_t dev, struct iommu_domain *iodom, device_t child,
1760 struct smmu_domain *domain;
1761 struct smmu_softc *sc;
1762 struct smmu_ctx *ctx;
1768 sc = device_get_softc(dev);
1769 domain = (struct smmu_domain *)iodom;
1771 seg = pci_get_domain(child);
1772 rid = pci_get_rid(child);
1773 err = acpi_iort_map_pci_smmuv3(seg, rid, &xref, &sid);
1777 if (sc->features & SMMU_FEATURE_2_LVL_STREAM_TABLE) {
1778 err = smmu_init_l1_entry(sc, sid);
1783 ctx = malloc(sizeof(struct smmu_ctx), M_SMMU, M_WAITOK | M_ZERO);
1784 ctx->vendor = pci_get_vendor(child);
1785 ctx->device = pci_get_device(child);
1788 ctx->domain = domain;
1799 smmu_init_ste(sc, domain->cd, ctx->sid, ctx->bypass);
1801 if (iommu_is_buswide_ctx(iodom->iommu, pci_get_bus(ctx->dev)))
1802 smmu_set_buswide(dev, domain, ctx);
1804 IOMMU_DOMAIN_LOCK(iodom);
1805 LIST_INSERT_HEAD(&domain->ctx_list, ctx, next);
1806 IOMMU_DOMAIN_UNLOCK(iodom);
1808 return (&ctx->ioctx);
1812 smmu_ctx_free(device_t dev, struct iommu_ctx *ioctx)
1814 struct smmu_softc *sc;
1815 struct smmu_ctx *ctx;
1817 IOMMU_ASSERT_LOCKED(ioctx->domain->iommu);
1819 sc = device_get_softc(dev);
1820 ctx = (struct smmu_ctx *)ioctx;
1822 smmu_deinit_l1_entry(sc, ctx->sid);
1824 LIST_REMOVE(ctx, next);
1830 smmu_ctx_lookup_by_sid(device_t dev, u_int sid)
1832 struct smmu_softc *sc;
1833 struct smmu_domain *domain;
1834 struct smmu_unit *unit;
1835 struct smmu_ctx *ctx;
1837 sc = device_get_softc(dev);
1841 LIST_FOREACH(domain, &unit->domain_list, next) {
1842 LIST_FOREACH(ctx, &domain->ctx_list, next) {
1843 if (ctx->sid == sid)
1851 static struct iommu_ctx *
1852 smmu_ctx_lookup(device_t dev, device_t child)
1854 struct iommu_unit *iommu;
1855 struct smmu_softc *sc;
1856 struct smmu_domain *domain;
1857 struct smmu_unit *unit;
1858 struct smmu_ctx *ctx;
1860 sc = device_get_softc(dev);
1863 iommu = &unit->iommu;
1865 IOMMU_ASSERT_LOCKED(iommu);
1867 LIST_FOREACH(domain, &unit->domain_list, next) {
1868 IOMMU_DOMAIN_LOCK(&domain->iodom);
1869 LIST_FOREACH(ctx, &domain->ctx_list, next) {
1870 if (ctx->dev == child) {
1871 IOMMU_DOMAIN_UNLOCK(&domain->iodom);
1872 return (&ctx->ioctx);
1875 IOMMU_DOMAIN_UNLOCK(&domain->iodom);
1882 smmu_find(device_t dev, device_t child)
1884 struct smmu_softc *sc;
1890 sc = device_get_softc(dev);
1892 rid = pci_get_rid(child);
1893 seg = pci_get_domain(child);
1896 * Find an xref of an IOMMU controller that serves traffic for dev.
1899 error = acpi_iort_map_pci_smmuv3(seg, rid, &xref, &sid);
1901 /* Could not find reference to an SMMU device. */
1905 /* TODO: add FDT support. */
1909 /* Check if xref is ours. */
1910 if (xref != sc->xref)
1916 static device_method_t smmu_methods[] = {
1917 /* Device interface */
1918 DEVMETHOD(device_detach, smmu_detach),
1920 /* SMMU interface */
1921 DEVMETHOD(iommu_find, smmu_find),
1922 DEVMETHOD(iommu_map, smmu_map),
1923 DEVMETHOD(iommu_unmap, smmu_unmap),
1924 DEVMETHOD(iommu_domain_alloc, smmu_domain_alloc),
1925 DEVMETHOD(iommu_domain_free, smmu_domain_free),
1926 DEVMETHOD(iommu_ctx_alloc, smmu_ctx_alloc),
1927 DEVMETHOD(iommu_ctx_free, smmu_ctx_free),
1928 DEVMETHOD(iommu_ctx_lookup, smmu_ctx_lookup),
1931 DEVMETHOD(bus_read_ivar, smmu_read_ivar),
1937 DEFINE_CLASS_0(smmu, smmu_driver, smmu_methods, sizeof(struct smmu_softc));