1263b2ba5SJacek Lawrynowicz // SPDX-License-Identifier: GPL-2.0-only
2263b2ba5SJacek Lawrynowicz /*
3*b7ed87ffSMaciej Falkowski * Copyright (C) 2020-2024 Intel Corporation
4263b2ba5SJacek Lawrynowicz */
5263b2ba5SJacek Lawrynowicz
6263b2ba5SJacek Lawrynowicz #include <linux/circ_buf.h>
7263b2ba5SJacek Lawrynowicz #include <linux/highmem.h>
8263b2ba5SJacek Lawrynowicz
9263b2ba5SJacek Lawrynowicz #include "ivpu_drv.h"
10929acfb9SWachowski, Karol #include "ivpu_hw.h"
11263b2ba5SJacek Lawrynowicz #include "ivpu_hw_reg_io.h"
12263b2ba5SJacek Lawrynowicz #include "ivpu_mmu.h"
13263b2ba5SJacek Lawrynowicz #include "ivpu_mmu_context.h"
14852be13fSJacek Lawrynowicz #include "ivpu_pm.h"
15263b2ba5SJacek Lawrynowicz
16beaf3ebfSJacek Lawrynowicz #define IVPU_MMU_REG_IDR0 0x00200000u
17beaf3ebfSJacek Lawrynowicz #define IVPU_MMU_REG_IDR1 0x00200004u
18beaf3ebfSJacek Lawrynowicz #define IVPU_MMU_REG_IDR3 0x0020000cu
19beaf3ebfSJacek Lawrynowicz #define IVPU_MMU_REG_IDR5 0x00200014u
20beaf3ebfSJacek Lawrynowicz #define IVPU_MMU_REG_CR0 0x00200020u
21beaf3ebfSJacek Lawrynowicz #define IVPU_MMU_REG_CR0ACK 0x00200024u
2274ce0f38SKrystian Pradzynski #define IVPU_MMU_REG_CR0ACK_VAL_MASK GENMASK(31, 0)
23beaf3ebfSJacek Lawrynowicz #define IVPU_MMU_REG_CR1 0x00200028u
24beaf3ebfSJacek Lawrynowicz #define IVPU_MMU_REG_CR2 0x0020002cu
25beaf3ebfSJacek Lawrynowicz #define IVPU_MMU_REG_IRQ_CTRL 0x00200050u
26beaf3ebfSJacek Lawrynowicz #define IVPU_MMU_REG_IRQ_CTRLACK 0x00200054u
2774ce0f38SKrystian Pradzynski #define IVPU_MMU_REG_IRQ_CTRLACK_VAL_MASK GENMASK(31, 0)
28beaf3ebfSJacek Lawrynowicz
29beaf3ebfSJacek Lawrynowicz #define IVPU_MMU_REG_GERROR 0x00200060u
30beaf3ebfSJacek Lawrynowicz #define IVPU_MMU_REG_GERROR_CMDQ_MASK BIT_MASK(0)
31beaf3ebfSJacek Lawrynowicz #define IVPU_MMU_REG_GERROR_EVTQ_ABT_MASK BIT_MASK(2)
32beaf3ebfSJacek Lawrynowicz #define IVPU_MMU_REG_GERROR_PRIQ_ABT_MASK BIT_MASK(3)
33beaf3ebfSJacek Lawrynowicz #define IVPU_MMU_REG_GERROR_MSI_CMDQ_ABT_MASK BIT_MASK(4)
34beaf3ebfSJacek Lawrynowicz #define IVPU_MMU_REG_GERROR_MSI_EVTQ_ABT_MASK BIT_MASK(5)
35beaf3ebfSJacek Lawrynowicz #define IVPU_MMU_REG_GERROR_MSI_PRIQ_ABT_MASK BIT_MASK(6)
36beaf3ebfSJacek Lawrynowicz #define IVPU_MMU_REG_GERROR_MSI_ABT_MASK BIT_MASK(7)
37beaf3ebfSJacek Lawrynowicz
38beaf3ebfSJacek Lawrynowicz #define IVPU_MMU_REG_GERRORN 0x00200064u
39beaf3ebfSJacek Lawrynowicz
40beaf3ebfSJacek Lawrynowicz #define IVPU_MMU_REG_STRTAB_BASE 0x00200080u
41beaf3ebfSJacek Lawrynowicz #define IVPU_MMU_REG_STRTAB_BASE_CFG 0x00200088u
42beaf3ebfSJacek Lawrynowicz #define IVPU_MMU_REG_CMDQ_BASE 0x00200090u
43beaf3ebfSJacek Lawrynowicz #define IVPU_MMU_REG_CMDQ_PROD 0x00200098u
44beaf3ebfSJacek Lawrynowicz #define IVPU_MMU_REG_CMDQ_CONS 0x0020009cu
4574ce0f38SKrystian Pradzynski #define IVPU_MMU_REG_CMDQ_CONS_VAL_MASK GENMASK(23, 0)
4674ce0f38SKrystian Pradzynski #define IVPU_MMU_REG_CMDQ_CONS_ERR_MASK GENMASK(30, 24)
47beaf3ebfSJacek Lawrynowicz #define IVPU_MMU_REG_EVTQ_BASE 0x002000a0u
48beaf3ebfSJacek Lawrynowicz #define IVPU_MMU_REG_EVTQ_PROD 0x002000a8u
49beaf3ebfSJacek Lawrynowicz #define IVPU_MMU_REG_EVTQ_CONS 0x002000acu
50beaf3ebfSJacek Lawrynowicz #define IVPU_MMU_REG_EVTQ_PROD_SEC (0x002000a8u + SZ_64K)
51beaf3ebfSJacek Lawrynowicz #define IVPU_MMU_REG_EVTQ_CONS_SEC (0x002000acu + SZ_64K)
52beaf3ebfSJacek Lawrynowicz
53263b2ba5SJacek Lawrynowicz #define IVPU_MMU_IDR0_REF 0x080f3e0f
54263b2ba5SJacek Lawrynowicz #define IVPU_MMU_IDR0_REF_SIMICS 0x080f3e1f
55263b2ba5SJacek Lawrynowicz #define IVPU_MMU_IDR1_REF 0x0e739d18
56263b2ba5SJacek Lawrynowicz #define IVPU_MMU_IDR3_REF 0x0000003c
57263b2ba5SJacek Lawrynowicz #define IVPU_MMU_IDR5_REF 0x00040070
58263b2ba5SJacek Lawrynowicz #define IVPU_MMU_IDR5_REF_SIMICS 0x00000075
59263b2ba5SJacek Lawrynowicz #define IVPU_MMU_IDR5_REF_FPGA 0x00800075
60263b2ba5SJacek Lawrynowicz
61263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CDTAB_ENT_SIZE 64
62263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CDTAB_ENT_COUNT_LOG2 8 /* 256 entries */
63263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CDTAB_ENT_COUNT ((u32)1 << IVPU_MMU_CDTAB_ENT_COUNT_LOG2)
64263b2ba5SJacek Lawrynowicz
65263b2ba5SJacek Lawrynowicz #define IVPU_MMU_STREAM_ID0 0
66263b2ba5SJacek Lawrynowicz #define IVPU_MMU_STREAM_ID3 3
67263b2ba5SJacek Lawrynowicz
68263b2ba5SJacek Lawrynowicz #define IVPU_MMU_STRTAB_ENT_SIZE 64
69263b2ba5SJacek Lawrynowicz #define IVPU_MMU_STRTAB_ENT_COUNT 4
70263b2ba5SJacek Lawrynowicz #define IVPU_MMU_STRTAB_CFG_LOG2SIZE 2
71263b2ba5SJacek Lawrynowicz #define IVPU_MMU_STRTAB_CFG IVPU_MMU_STRTAB_CFG_LOG2SIZE
72263b2ba5SJacek Lawrynowicz
73263b2ba5SJacek Lawrynowicz #define IVPU_MMU_Q_COUNT_LOG2 4 /* 16 entries */
74263b2ba5SJacek Lawrynowicz #define IVPU_MMU_Q_COUNT ((u32)1 << IVPU_MMU_Q_COUNT_LOG2)
75b039f1c4SWachowski, Karol #define IVPU_MMU_Q_WRAP_MASK GENMASK(IVPU_MMU_Q_COUNT_LOG2, 0)
76263b2ba5SJacek Lawrynowicz #define IVPU_MMU_Q_IDX_MASK (IVPU_MMU_Q_COUNT - 1)
77263b2ba5SJacek Lawrynowicz #define IVPU_MMU_Q_IDX(val) ((val) & IVPU_MMU_Q_IDX_MASK)
78b039f1c4SWachowski, Karol #define IVPU_MMU_Q_WRP(val) ((val) & IVPU_MMU_Q_COUNT)
79263b2ba5SJacek Lawrynowicz
80263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CMDQ_CMD_SIZE 16
81263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CMDQ_SIZE (IVPU_MMU_Q_COUNT * IVPU_MMU_CMDQ_CMD_SIZE)
82263b2ba5SJacek Lawrynowicz
83263b2ba5SJacek Lawrynowicz #define IVPU_MMU_EVTQ_CMD_SIZE 32
84263b2ba5SJacek Lawrynowicz #define IVPU_MMU_EVTQ_SIZE (IVPU_MMU_Q_COUNT * IVPU_MMU_EVTQ_CMD_SIZE)
85263b2ba5SJacek Lawrynowicz
86263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CMD_OPCODE GENMASK(7, 0)
87263b2ba5SJacek Lawrynowicz
88263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CMD_SYNC_0_CS GENMASK(13, 12)
89263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CMD_SYNC_0_MSH GENMASK(23, 22)
90263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CMD_SYNC_0_MSI_ATTR GENMASK(27, 24)
91263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CMD_SYNC_0_MSI_ATTR GENMASK(27, 24)
92263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CMD_SYNC_0_MSI_DATA GENMASK(63, 32)
93263b2ba5SJacek Lawrynowicz
94263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CMD_CFGI_0_SSEC BIT(10)
95263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CMD_CFGI_0_SSV BIT(11)
96263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CMD_CFGI_0_SSID GENMASK(31, 12)
97263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CMD_CFGI_0_SID GENMASK(63, 32)
98263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CMD_CFGI_1_RANGE GENMASK(4, 0)
99263b2ba5SJacek Lawrynowicz
100263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CMD_TLBI_0_ASID GENMASK(63, 48)
101263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CMD_TLBI_0_VMID GENMASK(47, 32)
102263b2ba5SJacek Lawrynowicz
103263b2ba5SJacek Lawrynowicz #define CMD_PREFETCH_CFG 0x1
104263b2ba5SJacek Lawrynowicz #define CMD_CFGI_STE 0x3
105263b2ba5SJacek Lawrynowicz #define CMD_CFGI_ALL 0x4
106263b2ba5SJacek Lawrynowicz #define CMD_CFGI_CD 0x5
107263b2ba5SJacek Lawrynowicz #define CMD_CFGI_CD_ALL 0x6
108263b2ba5SJacek Lawrynowicz #define CMD_TLBI_NH_ASID 0x11
109263b2ba5SJacek Lawrynowicz #define CMD_TLBI_EL2_ALL 0x20
110263b2ba5SJacek Lawrynowicz #define CMD_TLBI_NSNH_ALL 0x30
111263b2ba5SJacek Lawrynowicz #define CMD_SYNC 0x46
112263b2ba5SJacek Lawrynowicz
113263b2ba5SJacek Lawrynowicz #define IVPU_MMU_EVT_F_UUT 0x01
114263b2ba5SJacek Lawrynowicz #define IVPU_MMU_EVT_C_BAD_STREAMID 0x02
115263b2ba5SJacek Lawrynowicz #define IVPU_MMU_EVT_F_STE_FETCH 0x03
116263b2ba5SJacek Lawrynowicz #define IVPU_MMU_EVT_C_BAD_STE 0x04
117263b2ba5SJacek Lawrynowicz #define IVPU_MMU_EVT_F_BAD_ATS_TREQ 0x05
118263b2ba5SJacek Lawrynowicz #define IVPU_MMU_EVT_F_STREAM_DISABLED 0x06
119263b2ba5SJacek Lawrynowicz #define IVPU_MMU_EVT_F_TRANSL_FORBIDDEN 0x07
120263b2ba5SJacek Lawrynowicz #define IVPU_MMU_EVT_C_BAD_SUBSTREAMID 0x08
121263b2ba5SJacek Lawrynowicz #define IVPU_MMU_EVT_F_CD_FETCH 0x09
122263b2ba5SJacek Lawrynowicz #define IVPU_MMU_EVT_C_BAD_CD 0x0a
123263b2ba5SJacek Lawrynowicz #define IVPU_MMU_EVT_F_WALK_EABT 0x0b
124263b2ba5SJacek Lawrynowicz #define IVPU_MMU_EVT_F_TRANSLATION 0x10
125263b2ba5SJacek Lawrynowicz #define IVPU_MMU_EVT_F_ADDR_SIZE 0x11
126263b2ba5SJacek Lawrynowicz #define IVPU_MMU_EVT_F_ACCESS 0x12
127263b2ba5SJacek Lawrynowicz #define IVPU_MMU_EVT_F_PERMISSION 0x13
128263b2ba5SJacek Lawrynowicz #define IVPU_MMU_EVT_F_TLB_CONFLICT 0x20
129263b2ba5SJacek Lawrynowicz #define IVPU_MMU_EVT_F_CFG_CONFLICT 0x21
130263b2ba5SJacek Lawrynowicz #define IVPU_MMU_EVT_E_PAGE_REQUEST 0x24
131263b2ba5SJacek Lawrynowicz #define IVPU_MMU_EVT_F_VMS_FETCH 0x25
132263b2ba5SJacek Lawrynowicz
133263b2ba5SJacek Lawrynowicz #define IVPU_MMU_EVT_OP_MASK GENMASK_ULL(7, 0)
134263b2ba5SJacek Lawrynowicz #define IVPU_MMU_EVT_SSID_MASK GENMASK_ULL(31, 12)
135263b2ba5SJacek Lawrynowicz
136263b2ba5SJacek Lawrynowicz #define IVPU_MMU_Q_BASE_RWA BIT(62)
137263b2ba5SJacek Lawrynowicz #define IVPU_MMU_Q_BASE_ADDR_MASK GENMASK_ULL(51, 5)
138263b2ba5SJacek Lawrynowicz #define IVPU_MMU_STRTAB_BASE_RA BIT(62)
139263b2ba5SJacek Lawrynowicz #define IVPU_MMU_STRTAB_BASE_ADDR_MASK GENMASK_ULL(51, 6)
140263b2ba5SJacek Lawrynowicz
141263b2ba5SJacek Lawrynowicz #define IVPU_MMU_IRQ_EVTQ_EN BIT(2)
142263b2ba5SJacek Lawrynowicz #define IVPU_MMU_IRQ_GERROR_EN BIT(0)
143263b2ba5SJacek Lawrynowicz
144263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CR0_ATSCHK BIT(4)
145263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CR0_CMDQEN BIT(3)
146263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CR0_EVTQEN BIT(2)
147263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CR0_PRIQEN BIT(1)
148263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CR0_SMMUEN BIT(0)
149263b2ba5SJacek Lawrynowicz
150263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CR1_TABLE_SH GENMASK(11, 10)
151263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CR1_TABLE_OC GENMASK(9, 8)
152263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CR1_TABLE_IC GENMASK(7, 6)
153263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CR1_QUEUE_SH GENMASK(5, 4)
154263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CR1_QUEUE_OC GENMASK(3, 2)
155263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CR1_QUEUE_IC GENMASK(1, 0)
156263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CACHE_NC 0
157263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CACHE_WB 1
158263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CACHE_WT 2
159263b2ba5SJacek Lawrynowicz #define IVPU_MMU_SH_NSH 0
160263b2ba5SJacek Lawrynowicz #define IVPU_MMU_SH_OSH 2
161263b2ba5SJacek Lawrynowicz #define IVPU_MMU_SH_ISH 3
162263b2ba5SJacek Lawrynowicz
163263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CMDQ_OP GENMASK_ULL(7, 0)
164263b2ba5SJacek Lawrynowicz
165263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CD_0_TCR_T0SZ GENMASK_ULL(5, 0)
166263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CD_0_TCR_TG0 GENMASK_ULL(7, 6)
167263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CD_0_TCR_IRGN0 GENMASK_ULL(9, 8)
168263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CD_0_TCR_ORGN0 GENMASK_ULL(11, 10)
169263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CD_0_TCR_SH0 GENMASK_ULL(13, 12)
170263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CD_0_TCR_EPD0 BIT_ULL(14)
171263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CD_0_TCR_EPD1 BIT_ULL(30)
172263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CD_0_ENDI BIT(15)
173263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CD_0_V BIT(31)
174263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CD_0_TCR_IPS GENMASK_ULL(34, 32)
175263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CD_0_TCR_TBI0 BIT_ULL(38)
176263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CD_0_AA64 BIT(41)
177263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CD_0_S BIT(44)
178263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CD_0_R BIT(45)
179263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CD_0_A BIT(46)
180263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CD_0_ASET BIT(47)
181263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CD_0_ASID GENMASK_ULL(63, 48)
182263b2ba5SJacek Lawrynowicz
183a2fd4a6fSKarol Wachowski #define IVPU_MMU_T0SZ_48BIT 16
184a2fd4a6fSKarol Wachowski #define IVPU_MMU_T0SZ_38BIT 26
185a2fd4a6fSKarol Wachowski
186a2fd4a6fSKarol Wachowski #define IVPU_MMU_IPS_48BIT 5
187a2fd4a6fSKarol Wachowski #define IVPU_MMU_IPS_44BIT 4
188a2fd4a6fSKarol Wachowski #define IVPU_MMU_IPS_42BIT 3
189a2fd4a6fSKarol Wachowski #define IVPU_MMU_IPS_40BIT 2
190a2fd4a6fSKarol Wachowski #define IVPU_MMU_IPS_36BIT 1
191a2fd4a6fSKarol Wachowski #define IVPU_MMU_IPS_32BIT 0
192a2fd4a6fSKarol Wachowski
193263b2ba5SJacek Lawrynowicz #define IVPU_MMU_CD_1_TTB0_MASK GENMASK_ULL(51, 4)
194263b2ba5SJacek Lawrynowicz
195263b2ba5SJacek Lawrynowicz #define IVPU_MMU_STE_0_S1CDMAX GENMASK_ULL(63, 59)
196263b2ba5SJacek Lawrynowicz #define IVPU_MMU_STE_0_S1FMT GENMASK_ULL(5, 4)
197263b2ba5SJacek Lawrynowicz #define IVPU_MMU_STE_0_S1FMT_LINEAR 0
198263b2ba5SJacek Lawrynowicz #define IVPU_MMU_STE_DWORDS 8
199263b2ba5SJacek Lawrynowicz #define IVPU_MMU_STE_0_CFG_S1_TRANS 5
200263b2ba5SJacek Lawrynowicz #define IVPU_MMU_STE_0_CFG GENMASK_ULL(3, 1)
201263b2ba5SJacek Lawrynowicz #define IVPU_MMU_STE_0_S1CTXPTR_MASK GENMASK_ULL(51, 6)
202263b2ba5SJacek Lawrynowicz #define IVPU_MMU_STE_0_V BIT(0)
203263b2ba5SJacek Lawrynowicz
204263b2ba5SJacek Lawrynowicz #define IVPU_MMU_STE_1_STRW_NSEL1 0ul
205263b2ba5SJacek Lawrynowicz #define IVPU_MMU_STE_1_CONT GENMASK_ULL(16, 13)
206263b2ba5SJacek Lawrynowicz #define IVPU_MMU_STE_1_STRW GENMASK_ULL(31, 30)
207263b2ba5SJacek Lawrynowicz #define IVPU_MMU_STE_1_PRIVCFG GENMASK_ULL(49, 48)
208263b2ba5SJacek Lawrynowicz #define IVPU_MMU_STE_1_PRIVCFG_UNPRIV 2ul
209263b2ba5SJacek Lawrynowicz #define IVPU_MMU_STE_1_INSTCFG GENMASK_ULL(51, 50)
210263b2ba5SJacek Lawrynowicz #define IVPU_MMU_STE_1_INSTCFG_DATA 2ul
211263b2ba5SJacek Lawrynowicz #define IVPU_MMU_STE_1_MEV BIT(19)
212263b2ba5SJacek Lawrynowicz #define IVPU_MMU_STE_1_S1STALLD BIT(27)
213263b2ba5SJacek Lawrynowicz #define IVPU_MMU_STE_1_S1C_CACHE_NC 0ul
214263b2ba5SJacek Lawrynowicz #define IVPU_MMU_STE_1_S1C_CACHE_WBRA 1ul
215263b2ba5SJacek Lawrynowicz #define IVPU_MMU_STE_1_S1C_CACHE_WT 2ul
216263b2ba5SJacek Lawrynowicz #define IVPU_MMU_STE_1_S1C_CACHE_WB 3ul
217263b2ba5SJacek Lawrynowicz #define IVPU_MMU_STE_1_S1CIR GENMASK_ULL(3, 2)
218263b2ba5SJacek Lawrynowicz #define IVPU_MMU_STE_1_S1COR GENMASK_ULL(5, 4)
219263b2ba5SJacek Lawrynowicz #define IVPU_MMU_STE_1_S1CSH GENMASK_ULL(7, 6)
220263b2ba5SJacek Lawrynowicz #define IVPU_MMU_STE_1_S1DSS GENMASK_ULL(1, 0)
221263b2ba5SJacek Lawrynowicz #define IVPU_MMU_STE_1_S1DSS_TERMINATE 0x0
222263b2ba5SJacek Lawrynowicz
223263b2ba5SJacek Lawrynowicz #define IVPU_MMU_REG_TIMEOUT_US (10 * USEC_PER_MSEC)
224263b2ba5SJacek Lawrynowicz #define IVPU_MMU_QUEUE_TIMEOUT_US (100 * USEC_PER_MSEC)
225263b2ba5SJacek Lawrynowicz
226beaf3ebfSJacek Lawrynowicz #define IVPU_MMU_GERROR_ERR_MASK ((REG_FLD(IVPU_MMU_REG_GERROR, CMDQ)) | \
227beaf3ebfSJacek Lawrynowicz (REG_FLD(IVPU_MMU_REG_GERROR, EVTQ_ABT)) | \
228beaf3ebfSJacek Lawrynowicz (REG_FLD(IVPU_MMU_REG_GERROR, PRIQ_ABT)) | \
229beaf3ebfSJacek Lawrynowicz (REG_FLD(IVPU_MMU_REG_GERROR, MSI_CMDQ_ABT)) | \
230beaf3ebfSJacek Lawrynowicz (REG_FLD(IVPU_MMU_REG_GERROR, MSI_EVTQ_ABT)) | \
231beaf3ebfSJacek Lawrynowicz (REG_FLD(IVPU_MMU_REG_GERROR, MSI_PRIQ_ABT)) | \
232beaf3ebfSJacek Lawrynowicz (REG_FLD(IVPU_MMU_REG_GERROR, MSI_ABT)))
233263b2ba5SJacek Lawrynowicz
234e013aa9aSKarol Wachowski #define IVPU_MMU_CERROR_NONE 0x0
235e013aa9aSKarol Wachowski #define IVPU_MMU_CERROR_ILL 0x1
236e013aa9aSKarol Wachowski #define IVPU_MMU_CERROR_ABT 0x2
237e013aa9aSKarol Wachowski #define IVPU_MMU_CERROR_ATC_INV_SYNC 0x3
238e013aa9aSKarol Wachowski
ivpu_mmu_event_to_str(u32 cmd)239e013aa9aSKarol Wachowski static const char *ivpu_mmu_event_to_str(u32 cmd)
240263b2ba5SJacek Lawrynowicz {
241263b2ba5SJacek Lawrynowicz switch (cmd) {
242263b2ba5SJacek Lawrynowicz case IVPU_MMU_EVT_F_UUT:
243263b2ba5SJacek Lawrynowicz return "Unsupported Upstream Transaction";
244263b2ba5SJacek Lawrynowicz case IVPU_MMU_EVT_C_BAD_STREAMID:
245263b2ba5SJacek Lawrynowicz return "Transaction StreamID out of range";
246263b2ba5SJacek Lawrynowicz case IVPU_MMU_EVT_F_STE_FETCH:
247263b2ba5SJacek Lawrynowicz return "Fetch of STE caused external abort";
248263b2ba5SJacek Lawrynowicz case IVPU_MMU_EVT_C_BAD_STE:
249263b2ba5SJacek Lawrynowicz return "Used STE invalid";
250263b2ba5SJacek Lawrynowicz case IVPU_MMU_EVT_F_BAD_ATS_TREQ:
251263b2ba5SJacek Lawrynowicz return "Address Request disallowed for a StreamID";
252263b2ba5SJacek Lawrynowicz case IVPU_MMU_EVT_F_STREAM_DISABLED:
253263b2ba5SJacek Lawrynowicz return "Transaction marks non-substream disabled";
254263b2ba5SJacek Lawrynowicz case IVPU_MMU_EVT_F_TRANSL_FORBIDDEN:
255263b2ba5SJacek Lawrynowicz return "MMU bypass is disallowed for this StreamID";
256263b2ba5SJacek Lawrynowicz case IVPU_MMU_EVT_C_BAD_SUBSTREAMID:
257263b2ba5SJacek Lawrynowicz return "Invalid StreamID";
258263b2ba5SJacek Lawrynowicz case IVPU_MMU_EVT_F_CD_FETCH:
259263b2ba5SJacek Lawrynowicz return "Fetch of CD caused external abort";
260263b2ba5SJacek Lawrynowicz case IVPU_MMU_EVT_C_BAD_CD:
261263b2ba5SJacek Lawrynowicz return "Fetched CD invalid";
262263b2ba5SJacek Lawrynowicz case IVPU_MMU_EVT_F_WALK_EABT:
263263b2ba5SJacek Lawrynowicz return " An external abort occurred fetching a TLB";
264263b2ba5SJacek Lawrynowicz case IVPU_MMU_EVT_F_TRANSLATION:
265263b2ba5SJacek Lawrynowicz return "Translation fault";
266263b2ba5SJacek Lawrynowicz case IVPU_MMU_EVT_F_ADDR_SIZE:
267263b2ba5SJacek Lawrynowicz return " Output address caused address size fault";
268263b2ba5SJacek Lawrynowicz case IVPU_MMU_EVT_F_ACCESS:
269263b2ba5SJacek Lawrynowicz return "Access flag fault";
270263b2ba5SJacek Lawrynowicz case IVPU_MMU_EVT_F_PERMISSION:
271263b2ba5SJacek Lawrynowicz return "Permission fault occurred on page access";
272263b2ba5SJacek Lawrynowicz case IVPU_MMU_EVT_F_TLB_CONFLICT:
273263b2ba5SJacek Lawrynowicz return "A TLB conflict";
274263b2ba5SJacek Lawrynowicz case IVPU_MMU_EVT_F_CFG_CONFLICT:
275263b2ba5SJacek Lawrynowicz return "A configuration cache conflict";
276263b2ba5SJacek Lawrynowicz case IVPU_MMU_EVT_E_PAGE_REQUEST:
277263b2ba5SJacek Lawrynowicz return "Page request hint from a client device";
278263b2ba5SJacek Lawrynowicz case IVPU_MMU_EVT_F_VMS_FETCH:
279263b2ba5SJacek Lawrynowicz return "Fetch of VMS caused external abort";
280263b2ba5SJacek Lawrynowicz default:
2813556f922SWachowski, Karol return "Unknown event";
282263b2ba5SJacek Lawrynowicz }
283263b2ba5SJacek Lawrynowicz }
284263b2ba5SJacek Lawrynowicz
ivpu_mmu_cmdq_err_to_str(u32 err)285e013aa9aSKarol Wachowski static const char *ivpu_mmu_cmdq_err_to_str(u32 err)
286e013aa9aSKarol Wachowski {
287e013aa9aSKarol Wachowski switch (err) {
288e013aa9aSKarol Wachowski case IVPU_MMU_CERROR_NONE:
2893556f922SWachowski, Karol return "No error";
290e013aa9aSKarol Wachowski case IVPU_MMU_CERROR_ILL:
291e013aa9aSKarol Wachowski return "Illegal command";
292e013aa9aSKarol Wachowski case IVPU_MMU_CERROR_ABT:
2933556f922SWachowski, Karol return "External abort on command queue read";
294e013aa9aSKarol Wachowski case IVPU_MMU_CERROR_ATC_INV_SYNC:
295e013aa9aSKarol Wachowski return "Sync failed to complete ATS invalidation";
296e013aa9aSKarol Wachowski default:
2973556f922SWachowski, Karol return "Unknown error";
298e013aa9aSKarol Wachowski }
299e013aa9aSKarol Wachowski }
300e013aa9aSKarol Wachowski
ivpu_mmu_config_check(struct ivpu_device * vdev)301263b2ba5SJacek Lawrynowicz static void ivpu_mmu_config_check(struct ivpu_device *vdev)
302263b2ba5SJacek Lawrynowicz {
303263b2ba5SJacek Lawrynowicz u32 val_ref;
304263b2ba5SJacek Lawrynowicz u32 val;
305263b2ba5SJacek Lawrynowicz
306263b2ba5SJacek Lawrynowicz if (ivpu_is_simics(vdev))
307263b2ba5SJacek Lawrynowicz val_ref = IVPU_MMU_IDR0_REF_SIMICS;
308263b2ba5SJacek Lawrynowicz else
309263b2ba5SJacek Lawrynowicz val_ref = IVPU_MMU_IDR0_REF;
310263b2ba5SJacek Lawrynowicz
311beaf3ebfSJacek Lawrynowicz val = REGV_RD32(IVPU_MMU_REG_IDR0);
312263b2ba5SJacek Lawrynowicz if (val != val_ref)
313263b2ba5SJacek Lawrynowicz ivpu_dbg(vdev, MMU, "IDR0 0x%x != IDR0_REF 0x%x\n", val, val_ref);
314263b2ba5SJacek Lawrynowicz
315beaf3ebfSJacek Lawrynowicz val = REGV_RD32(IVPU_MMU_REG_IDR1);
316263b2ba5SJacek Lawrynowicz if (val != IVPU_MMU_IDR1_REF)
317263b2ba5SJacek Lawrynowicz ivpu_dbg(vdev, MMU, "IDR1 0x%x != IDR1_REF 0x%x\n", val, IVPU_MMU_IDR1_REF);
318263b2ba5SJacek Lawrynowicz
319beaf3ebfSJacek Lawrynowicz val = REGV_RD32(IVPU_MMU_REG_IDR3);
320263b2ba5SJacek Lawrynowicz if (val != IVPU_MMU_IDR3_REF)
321263b2ba5SJacek Lawrynowicz ivpu_dbg(vdev, MMU, "IDR3 0x%x != IDR3_REF 0x%x\n", val, IVPU_MMU_IDR3_REF);
322263b2ba5SJacek Lawrynowicz
323263b2ba5SJacek Lawrynowicz if (ivpu_is_simics(vdev))
324263b2ba5SJacek Lawrynowicz val_ref = IVPU_MMU_IDR5_REF_SIMICS;
325263b2ba5SJacek Lawrynowicz else if (ivpu_is_fpga(vdev))
326263b2ba5SJacek Lawrynowicz val_ref = IVPU_MMU_IDR5_REF_FPGA;
327263b2ba5SJacek Lawrynowicz else
328263b2ba5SJacek Lawrynowicz val_ref = IVPU_MMU_IDR5_REF;
329263b2ba5SJacek Lawrynowicz
330beaf3ebfSJacek Lawrynowicz val = REGV_RD32(IVPU_MMU_REG_IDR5);
331263b2ba5SJacek Lawrynowicz if (val != val_ref)
332263b2ba5SJacek Lawrynowicz ivpu_dbg(vdev, MMU, "IDR5 0x%x != IDR5_REF 0x%x\n", val, val_ref);
333263b2ba5SJacek Lawrynowicz }
334263b2ba5SJacek Lawrynowicz
ivpu_mmu_cdtab_alloc(struct ivpu_device * vdev)335263b2ba5SJacek Lawrynowicz static int ivpu_mmu_cdtab_alloc(struct ivpu_device *vdev)
336263b2ba5SJacek Lawrynowicz {
337263b2ba5SJacek Lawrynowicz struct ivpu_mmu_info *mmu = vdev->mmu;
338263b2ba5SJacek Lawrynowicz struct ivpu_mmu_cdtab *cdtab = &mmu->cdtab;
339263b2ba5SJacek Lawrynowicz size_t size = IVPU_MMU_CDTAB_ENT_COUNT * IVPU_MMU_CDTAB_ENT_SIZE;
340263b2ba5SJacek Lawrynowicz
341263b2ba5SJacek Lawrynowicz cdtab->base = dmam_alloc_coherent(vdev->drm.dev, size, &cdtab->dma, GFP_KERNEL);
342263b2ba5SJacek Lawrynowicz if (!cdtab->base)
343263b2ba5SJacek Lawrynowicz return -ENOMEM;
344263b2ba5SJacek Lawrynowicz
345263b2ba5SJacek Lawrynowicz ivpu_dbg(vdev, MMU, "CDTAB alloc: dma=%pad size=%zu\n", &cdtab->dma, size);
346263b2ba5SJacek Lawrynowicz
347263b2ba5SJacek Lawrynowicz return 0;
348263b2ba5SJacek Lawrynowicz }
349263b2ba5SJacek Lawrynowicz
ivpu_mmu_strtab_alloc(struct ivpu_device * vdev)350263b2ba5SJacek Lawrynowicz static int ivpu_mmu_strtab_alloc(struct ivpu_device *vdev)
351263b2ba5SJacek Lawrynowicz {
352263b2ba5SJacek Lawrynowicz struct ivpu_mmu_info *mmu = vdev->mmu;
353263b2ba5SJacek Lawrynowicz struct ivpu_mmu_strtab *strtab = &mmu->strtab;
354263b2ba5SJacek Lawrynowicz size_t size = IVPU_MMU_STRTAB_ENT_COUNT * IVPU_MMU_STRTAB_ENT_SIZE;
355263b2ba5SJacek Lawrynowicz
356263b2ba5SJacek Lawrynowicz strtab->base = dmam_alloc_coherent(vdev->drm.dev, size, &strtab->dma, GFP_KERNEL);
357263b2ba5SJacek Lawrynowicz if (!strtab->base)
358263b2ba5SJacek Lawrynowicz return -ENOMEM;
359263b2ba5SJacek Lawrynowicz
360263b2ba5SJacek Lawrynowicz strtab->base_cfg = IVPU_MMU_STRTAB_CFG;
361263b2ba5SJacek Lawrynowicz strtab->dma_q = IVPU_MMU_STRTAB_BASE_RA;
362263b2ba5SJacek Lawrynowicz strtab->dma_q |= strtab->dma & IVPU_MMU_STRTAB_BASE_ADDR_MASK;
363263b2ba5SJacek Lawrynowicz
364263b2ba5SJacek Lawrynowicz ivpu_dbg(vdev, MMU, "STRTAB alloc: dma=%pad dma_q=%pad size=%zu\n",
365263b2ba5SJacek Lawrynowicz &strtab->dma, &strtab->dma_q, size);
366263b2ba5SJacek Lawrynowicz
367263b2ba5SJacek Lawrynowicz return 0;
368263b2ba5SJacek Lawrynowicz }
369263b2ba5SJacek Lawrynowicz
ivpu_mmu_cmdq_alloc(struct ivpu_device * vdev)370263b2ba5SJacek Lawrynowicz static int ivpu_mmu_cmdq_alloc(struct ivpu_device *vdev)
371263b2ba5SJacek Lawrynowicz {
372263b2ba5SJacek Lawrynowicz struct ivpu_mmu_info *mmu = vdev->mmu;
373263b2ba5SJacek Lawrynowicz struct ivpu_mmu_queue *q = &mmu->cmdq;
374263b2ba5SJacek Lawrynowicz
375263b2ba5SJacek Lawrynowicz q->base = dmam_alloc_coherent(vdev->drm.dev, IVPU_MMU_CMDQ_SIZE, &q->dma, GFP_KERNEL);
376263b2ba5SJacek Lawrynowicz if (!q->base)
377263b2ba5SJacek Lawrynowicz return -ENOMEM;
378263b2ba5SJacek Lawrynowicz
379263b2ba5SJacek Lawrynowicz q->dma_q = IVPU_MMU_Q_BASE_RWA;
380263b2ba5SJacek Lawrynowicz q->dma_q |= q->dma & IVPU_MMU_Q_BASE_ADDR_MASK;
381263b2ba5SJacek Lawrynowicz q->dma_q |= IVPU_MMU_Q_COUNT_LOG2;
382263b2ba5SJacek Lawrynowicz
383263b2ba5SJacek Lawrynowicz ivpu_dbg(vdev, MMU, "CMDQ alloc: dma=%pad dma_q=%pad size=%u\n",
384263b2ba5SJacek Lawrynowicz &q->dma, &q->dma_q, IVPU_MMU_CMDQ_SIZE);
385263b2ba5SJacek Lawrynowicz
386263b2ba5SJacek Lawrynowicz return 0;
387263b2ba5SJacek Lawrynowicz }
388263b2ba5SJacek Lawrynowicz
ivpu_mmu_evtq_alloc(struct ivpu_device * vdev)389263b2ba5SJacek Lawrynowicz static int ivpu_mmu_evtq_alloc(struct ivpu_device *vdev)
390263b2ba5SJacek Lawrynowicz {
391263b2ba5SJacek Lawrynowicz struct ivpu_mmu_info *mmu = vdev->mmu;
392263b2ba5SJacek Lawrynowicz struct ivpu_mmu_queue *q = &mmu->evtq;
393263b2ba5SJacek Lawrynowicz
394263b2ba5SJacek Lawrynowicz q->base = dmam_alloc_coherent(vdev->drm.dev, IVPU_MMU_EVTQ_SIZE, &q->dma, GFP_KERNEL);
395263b2ba5SJacek Lawrynowicz if (!q->base)
396263b2ba5SJacek Lawrynowicz return -ENOMEM;
397263b2ba5SJacek Lawrynowicz
398263b2ba5SJacek Lawrynowicz q->dma_q = IVPU_MMU_Q_BASE_RWA;
399263b2ba5SJacek Lawrynowicz q->dma_q |= q->dma & IVPU_MMU_Q_BASE_ADDR_MASK;
400263b2ba5SJacek Lawrynowicz q->dma_q |= IVPU_MMU_Q_COUNT_LOG2;
401263b2ba5SJacek Lawrynowicz
402263b2ba5SJacek Lawrynowicz ivpu_dbg(vdev, MMU, "EVTQ alloc: dma=%pad dma_q=%pad size=%u\n",
403263b2ba5SJacek Lawrynowicz &q->dma, &q->dma_q, IVPU_MMU_EVTQ_SIZE);
404263b2ba5SJacek Lawrynowicz
405263b2ba5SJacek Lawrynowicz return 0;
406263b2ba5SJacek Lawrynowicz }
407263b2ba5SJacek Lawrynowicz
ivpu_mmu_structs_alloc(struct ivpu_device * vdev)408263b2ba5SJacek Lawrynowicz static int ivpu_mmu_structs_alloc(struct ivpu_device *vdev)
409263b2ba5SJacek Lawrynowicz {
410263b2ba5SJacek Lawrynowicz int ret;
411263b2ba5SJacek Lawrynowicz
412263b2ba5SJacek Lawrynowicz ret = ivpu_mmu_cdtab_alloc(vdev);
413263b2ba5SJacek Lawrynowicz if (ret) {
414263b2ba5SJacek Lawrynowicz ivpu_err(vdev, "Failed to allocate cdtab: %d\n", ret);
415263b2ba5SJacek Lawrynowicz return ret;
416263b2ba5SJacek Lawrynowicz }
417263b2ba5SJacek Lawrynowicz
418263b2ba5SJacek Lawrynowicz ret = ivpu_mmu_strtab_alloc(vdev);
419263b2ba5SJacek Lawrynowicz if (ret) {
420263b2ba5SJacek Lawrynowicz ivpu_err(vdev, "Failed to allocate strtab: %d\n", ret);
421263b2ba5SJacek Lawrynowicz return ret;
422263b2ba5SJacek Lawrynowicz }
423263b2ba5SJacek Lawrynowicz
424263b2ba5SJacek Lawrynowicz ret = ivpu_mmu_cmdq_alloc(vdev);
425263b2ba5SJacek Lawrynowicz if (ret) {
426263b2ba5SJacek Lawrynowicz ivpu_err(vdev, "Failed to allocate cmdq: %d\n", ret);
427263b2ba5SJacek Lawrynowicz return ret;
428263b2ba5SJacek Lawrynowicz }
429263b2ba5SJacek Lawrynowicz
430263b2ba5SJacek Lawrynowicz ret = ivpu_mmu_evtq_alloc(vdev);
431263b2ba5SJacek Lawrynowicz if (ret)
432263b2ba5SJacek Lawrynowicz ivpu_err(vdev, "Failed to allocate evtq: %d\n", ret);
433263b2ba5SJacek Lawrynowicz
434263b2ba5SJacek Lawrynowicz return ret;
435263b2ba5SJacek Lawrynowicz }
436263b2ba5SJacek Lawrynowicz
ivpu_mmu_reg_write_cr0(struct ivpu_device * vdev,u32 val)43774ce0f38SKrystian Pradzynski static int ivpu_mmu_reg_write_cr0(struct ivpu_device *vdev, u32 val)
438263b2ba5SJacek Lawrynowicz {
43974ce0f38SKrystian Pradzynski REGV_WR32(IVPU_MMU_REG_CR0, val);
440263b2ba5SJacek Lawrynowicz
44174ce0f38SKrystian Pradzynski return REGV_POLL_FLD(IVPU_MMU_REG_CR0ACK, VAL, val, IVPU_MMU_REG_TIMEOUT_US);
44274ce0f38SKrystian Pradzynski }
443263b2ba5SJacek Lawrynowicz
ivpu_mmu_reg_write_irq_ctrl(struct ivpu_device * vdev,u32 val)44474ce0f38SKrystian Pradzynski static int ivpu_mmu_reg_write_irq_ctrl(struct ivpu_device *vdev, u32 val)
44574ce0f38SKrystian Pradzynski {
44674ce0f38SKrystian Pradzynski REGV_WR32(IVPU_MMU_REG_IRQ_CTRL, val);
447263b2ba5SJacek Lawrynowicz
44874ce0f38SKrystian Pradzynski return REGV_POLL_FLD(IVPU_MMU_REG_IRQ_CTRLACK, VAL, val, IVPU_MMU_REG_TIMEOUT_US);
449263b2ba5SJacek Lawrynowicz }
450263b2ba5SJacek Lawrynowicz
ivpu_mmu_irqs_setup(struct ivpu_device * vdev)451263b2ba5SJacek Lawrynowicz static int ivpu_mmu_irqs_setup(struct ivpu_device *vdev)
452263b2ba5SJacek Lawrynowicz {
453263b2ba5SJacek Lawrynowicz u32 irq_ctrl = IVPU_MMU_IRQ_EVTQ_EN | IVPU_MMU_IRQ_GERROR_EN;
454263b2ba5SJacek Lawrynowicz int ret;
455263b2ba5SJacek Lawrynowicz
45674ce0f38SKrystian Pradzynski ret = ivpu_mmu_reg_write_irq_ctrl(vdev, 0);
457263b2ba5SJacek Lawrynowicz if (ret)
458263b2ba5SJacek Lawrynowicz return ret;
459263b2ba5SJacek Lawrynowicz
46074ce0f38SKrystian Pradzynski return ivpu_mmu_reg_write_irq_ctrl(vdev, irq_ctrl);
461263b2ba5SJacek Lawrynowicz }
462263b2ba5SJacek Lawrynowicz
ivpu_mmu_cmdq_wait_for_cons(struct ivpu_device * vdev)463263b2ba5SJacek Lawrynowicz static int ivpu_mmu_cmdq_wait_for_cons(struct ivpu_device *vdev)
464263b2ba5SJacek Lawrynowicz {
465263b2ba5SJacek Lawrynowicz struct ivpu_mmu_queue *cmdq = &vdev->mmu->cmdq;
46674ce0f38SKrystian Pradzynski int ret;
467263b2ba5SJacek Lawrynowicz
46874ce0f38SKrystian Pradzynski ret = REGV_POLL_FLD(IVPU_MMU_REG_CMDQ_CONS, VAL, cmdq->prod,
469263b2ba5SJacek Lawrynowicz IVPU_MMU_QUEUE_TIMEOUT_US);
47074ce0f38SKrystian Pradzynski if (ret)
47174ce0f38SKrystian Pradzynski return ret;
47274ce0f38SKrystian Pradzynski
47374ce0f38SKrystian Pradzynski cmdq->cons = cmdq->prod;
47474ce0f38SKrystian Pradzynski
47574ce0f38SKrystian Pradzynski return 0;
476263b2ba5SJacek Lawrynowicz }
477263b2ba5SJacek Lawrynowicz
ivpu_mmu_queue_is_full(struct ivpu_mmu_queue * q)478b039f1c4SWachowski, Karol static bool ivpu_mmu_queue_is_full(struct ivpu_mmu_queue *q)
479b039f1c4SWachowski, Karol {
480b039f1c4SWachowski, Karol return ((IVPU_MMU_Q_IDX(q->prod) == IVPU_MMU_Q_IDX(q->cons)) &&
481b039f1c4SWachowski, Karol (IVPU_MMU_Q_WRP(q->prod) != IVPU_MMU_Q_WRP(q->cons)));
482b039f1c4SWachowski, Karol }
483b039f1c4SWachowski, Karol
ivpu_mmu_queue_is_empty(struct ivpu_mmu_queue * q)484b039f1c4SWachowski, Karol static bool ivpu_mmu_queue_is_empty(struct ivpu_mmu_queue *q)
485b039f1c4SWachowski, Karol {
486b039f1c4SWachowski, Karol return ((IVPU_MMU_Q_IDX(q->prod) == IVPU_MMU_Q_IDX(q->cons)) &&
487b039f1c4SWachowski, Karol (IVPU_MMU_Q_WRP(q->prod) == IVPU_MMU_Q_WRP(q->cons)));
488b039f1c4SWachowski, Karol }
489b039f1c4SWachowski, Karol
ivpu_mmu_cmdq_cmd_write(struct ivpu_device * vdev,const char * name,u64 data0,u64 data1)490263b2ba5SJacek Lawrynowicz static int ivpu_mmu_cmdq_cmd_write(struct ivpu_device *vdev, const char *name, u64 data0, u64 data1)
491263b2ba5SJacek Lawrynowicz {
492b039f1c4SWachowski, Karol struct ivpu_mmu_queue *cmdq = &vdev->mmu->cmdq;
493b039f1c4SWachowski, Karol u64 *queue_buffer = cmdq->base;
494b039f1c4SWachowski, Karol int idx = IVPU_MMU_Q_IDX(cmdq->prod) * (IVPU_MMU_CMDQ_CMD_SIZE / sizeof(*queue_buffer));
495263b2ba5SJacek Lawrynowicz
496b039f1c4SWachowski, Karol if (ivpu_mmu_queue_is_full(cmdq)) {
497263b2ba5SJacek Lawrynowicz ivpu_err(vdev, "Failed to write MMU CMD %s\n", name);
498263b2ba5SJacek Lawrynowicz return -EBUSY;
499263b2ba5SJacek Lawrynowicz }
500263b2ba5SJacek Lawrynowicz
501263b2ba5SJacek Lawrynowicz queue_buffer[idx] = data0;
502263b2ba5SJacek Lawrynowicz queue_buffer[idx + 1] = data1;
503b039f1c4SWachowski, Karol cmdq->prod = (cmdq->prod + 1) & IVPU_MMU_Q_WRAP_MASK;
504263b2ba5SJacek Lawrynowicz
505263b2ba5SJacek Lawrynowicz ivpu_dbg(vdev, MMU, "CMD write: %s data: 0x%llx 0x%llx\n", name, data0, data1);
506263b2ba5SJacek Lawrynowicz
507263b2ba5SJacek Lawrynowicz return 0;
508263b2ba5SJacek Lawrynowicz }
509263b2ba5SJacek Lawrynowicz
ivpu_mmu_cmdq_sync(struct ivpu_device * vdev)510263b2ba5SJacek Lawrynowicz static int ivpu_mmu_cmdq_sync(struct ivpu_device *vdev)
511263b2ba5SJacek Lawrynowicz {
512263b2ba5SJacek Lawrynowicz struct ivpu_mmu_queue *q = &vdev->mmu->cmdq;
513263b2ba5SJacek Lawrynowicz u64 val;
514263b2ba5SJacek Lawrynowicz int ret;
515263b2ba5SJacek Lawrynowicz
5160c287c27SJacek Lawrynowicz val = FIELD_PREP(IVPU_MMU_CMD_OPCODE, CMD_SYNC);
517263b2ba5SJacek Lawrynowicz
518263b2ba5SJacek Lawrynowicz ret = ivpu_mmu_cmdq_cmd_write(vdev, "SYNC", val, 0);
519263b2ba5SJacek Lawrynowicz if (ret)
520263b2ba5SJacek Lawrynowicz return ret;
521263b2ba5SJacek Lawrynowicz
5222c3801b1SWachowski, Karol if (!ivpu_is_force_snoop_enabled(vdev))
523263b2ba5SJacek Lawrynowicz clflush_cache_range(q->base, IVPU_MMU_CMDQ_SIZE);
524beaf3ebfSJacek Lawrynowicz REGV_WR32(IVPU_MMU_REG_CMDQ_PROD, q->prod);
525263b2ba5SJacek Lawrynowicz
526263b2ba5SJacek Lawrynowicz ret = ivpu_mmu_cmdq_wait_for_cons(vdev);
527e013aa9aSKarol Wachowski if (ret) {
528e013aa9aSKarol Wachowski u32 err;
529e013aa9aSKarol Wachowski
530e013aa9aSKarol Wachowski val = REGV_RD32(IVPU_MMU_REG_CMDQ_CONS);
531e013aa9aSKarol Wachowski err = REG_GET_FLD(IVPU_MMU_REG_CMDQ_CONS, ERR, val);
532e013aa9aSKarol Wachowski
533e013aa9aSKarol Wachowski ivpu_err(vdev, "Timed out waiting for MMU consumer: %d, error: %s\n", ret,
534e013aa9aSKarol Wachowski ivpu_mmu_cmdq_err_to_str(err));
535929acfb9SWachowski, Karol ivpu_hw_diagnose_failure(vdev);
536e013aa9aSKarol Wachowski }
537263b2ba5SJacek Lawrynowicz
538263b2ba5SJacek Lawrynowicz return ret;
539263b2ba5SJacek Lawrynowicz }
540263b2ba5SJacek Lawrynowicz
ivpu_mmu_cmdq_write_cfgi_all(struct ivpu_device * vdev)541263b2ba5SJacek Lawrynowicz static int ivpu_mmu_cmdq_write_cfgi_all(struct ivpu_device *vdev)
542263b2ba5SJacek Lawrynowicz {
543263b2ba5SJacek Lawrynowicz u64 data0 = FIELD_PREP(IVPU_MMU_CMD_OPCODE, CMD_CFGI_ALL);
544263b2ba5SJacek Lawrynowicz u64 data1 = FIELD_PREP(IVPU_MMU_CMD_CFGI_1_RANGE, 0x1f);
545263b2ba5SJacek Lawrynowicz
546263b2ba5SJacek Lawrynowicz return ivpu_mmu_cmdq_cmd_write(vdev, "CFGI_ALL", data0, data1);
547263b2ba5SJacek Lawrynowicz }
548263b2ba5SJacek Lawrynowicz
ivpu_mmu_cmdq_write_tlbi_nh_asid(struct ivpu_device * vdev,u16 ssid)549263b2ba5SJacek Lawrynowicz static int ivpu_mmu_cmdq_write_tlbi_nh_asid(struct ivpu_device *vdev, u16 ssid)
550263b2ba5SJacek Lawrynowicz {
551263b2ba5SJacek Lawrynowicz u64 val = FIELD_PREP(IVPU_MMU_CMD_OPCODE, CMD_TLBI_NH_ASID) |
552263b2ba5SJacek Lawrynowicz FIELD_PREP(IVPU_MMU_CMD_TLBI_0_ASID, ssid);
553263b2ba5SJacek Lawrynowicz
554263b2ba5SJacek Lawrynowicz return ivpu_mmu_cmdq_cmd_write(vdev, "TLBI_NH_ASID", val, 0);
555263b2ba5SJacek Lawrynowicz }
556263b2ba5SJacek Lawrynowicz
ivpu_mmu_cmdq_write_tlbi_nsnh_all(struct ivpu_device * vdev)557263b2ba5SJacek Lawrynowicz static int ivpu_mmu_cmdq_write_tlbi_nsnh_all(struct ivpu_device *vdev)
558263b2ba5SJacek Lawrynowicz {
559263b2ba5SJacek Lawrynowicz u64 val = FIELD_PREP(IVPU_MMU_CMD_OPCODE, CMD_TLBI_NSNH_ALL);
560263b2ba5SJacek Lawrynowicz
561263b2ba5SJacek Lawrynowicz return ivpu_mmu_cmdq_cmd_write(vdev, "TLBI_NSNH_ALL", val, 0);
562263b2ba5SJacek Lawrynowicz }
563263b2ba5SJacek Lawrynowicz
ivpu_mmu_reset(struct ivpu_device * vdev)564263b2ba5SJacek Lawrynowicz static int ivpu_mmu_reset(struct ivpu_device *vdev)
565263b2ba5SJacek Lawrynowicz {
566263b2ba5SJacek Lawrynowicz struct ivpu_mmu_info *mmu = vdev->mmu;
567263b2ba5SJacek Lawrynowicz u32 val;
568263b2ba5SJacek Lawrynowicz int ret;
569263b2ba5SJacek Lawrynowicz
570263b2ba5SJacek Lawrynowicz memset(mmu->cmdq.base, 0, IVPU_MMU_CMDQ_SIZE);
5712c3801b1SWachowski, Karol if (!ivpu_is_force_snoop_enabled(vdev))
572263b2ba5SJacek Lawrynowicz clflush_cache_range(mmu->cmdq.base, IVPU_MMU_CMDQ_SIZE);
573263b2ba5SJacek Lawrynowicz mmu->cmdq.prod = 0;
574263b2ba5SJacek Lawrynowicz mmu->cmdq.cons = 0;
575263b2ba5SJacek Lawrynowicz
576263b2ba5SJacek Lawrynowicz memset(mmu->evtq.base, 0, IVPU_MMU_EVTQ_SIZE);
577263b2ba5SJacek Lawrynowicz mmu->evtq.prod = 0;
578263b2ba5SJacek Lawrynowicz mmu->evtq.cons = 0;
579263b2ba5SJacek Lawrynowicz
58074ce0f38SKrystian Pradzynski ret = ivpu_mmu_reg_write_cr0(vdev, 0);
581263b2ba5SJacek Lawrynowicz if (ret)
582263b2ba5SJacek Lawrynowicz return ret;
583263b2ba5SJacek Lawrynowicz
584263b2ba5SJacek Lawrynowicz val = FIELD_PREP(IVPU_MMU_CR1_TABLE_SH, IVPU_MMU_SH_ISH) |
585263b2ba5SJacek Lawrynowicz FIELD_PREP(IVPU_MMU_CR1_TABLE_OC, IVPU_MMU_CACHE_WB) |
586263b2ba5SJacek Lawrynowicz FIELD_PREP(IVPU_MMU_CR1_TABLE_IC, IVPU_MMU_CACHE_WB) |
587263b2ba5SJacek Lawrynowicz FIELD_PREP(IVPU_MMU_CR1_QUEUE_SH, IVPU_MMU_SH_ISH) |
588263b2ba5SJacek Lawrynowicz FIELD_PREP(IVPU_MMU_CR1_QUEUE_OC, IVPU_MMU_CACHE_WB) |
589263b2ba5SJacek Lawrynowicz FIELD_PREP(IVPU_MMU_CR1_QUEUE_IC, IVPU_MMU_CACHE_WB);
590beaf3ebfSJacek Lawrynowicz REGV_WR32(IVPU_MMU_REG_CR1, val);
591263b2ba5SJacek Lawrynowicz
592beaf3ebfSJacek Lawrynowicz REGV_WR64(IVPU_MMU_REG_STRTAB_BASE, mmu->strtab.dma_q);
593beaf3ebfSJacek Lawrynowicz REGV_WR32(IVPU_MMU_REG_STRTAB_BASE_CFG, mmu->strtab.base_cfg);
594263b2ba5SJacek Lawrynowicz
595beaf3ebfSJacek Lawrynowicz REGV_WR64(IVPU_MMU_REG_CMDQ_BASE, mmu->cmdq.dma_q);
596beaf3ebfSJacek Lawrynowicz REGV_WR32(IVPU_MMU_REG_CMDQ_PROD, 0);
597beaf3ebfSJacek Lawrynowicz REGV_WR32(IVPU_MMU_REG_CMDQ_CONS, 0);
598263b2ba5SJacek Lawrynowicz
599263b2ba5SJacek Lawrynowicz val = IVPU_MMU_CR0_CMDQEN;
60074ce0f38SKrystian Pradzynski ret = ivpu_mmu_reg_write_cr0(vdev, val);
601263b2ba5SJacek Lawrynowicz if (ret)
602263b2ba5SJacek Lawrynowicz return ret;
603263b2ba5SJacek Lawrynowicz
604263b2ba5SJacek Lawrynowicz ret = ivpu_mmu_cmdq_write_cfgi_all(vdev);
605263b2ba5SJacek Lawrynowicz if (ret)
606263b2ba5SJacek Lawrynowicz return ret;
607263b2ba5SJacek Lawrynowicz
608263b2ba5SJacek Lawrynowicz ret = ivpu_mmu_cmdq_write_tlbi_nsnh_all(vdev);
609263b2ba5SJacek Lawrynowicz if (ret)
610263b2ba5SJacek Lawrynowicz return ret;
611263b2ba5SJacek Lawrynowicz
612263b2ba5SJacek Lawrynowicz ret = ivpu_mmu_cmdq_sync(vdev);
613263b2ba5SJacek Lawrynowicz if (ret)
614263b2ba5SJacek Lawrynowicz return ret;
615263b2ba5SJacek Lawrynowicz
616beaf3ebfSJacek Lawrynowicz REGV_WR64(IVPU_MMU_REG_EVTQ_BASE, mmu->evtq.dma_q);
617beaf3ebfSJacek Lawrynowicz REGV_WR32(IVPU_MMU_REG_EVTQ_PROD_SEC, 0);
618beaf3ebfSJacek Lawrynowicz REGV_WR32(IVPU_MMU_REG_EVTQ_CONS_SEC, 0);
619263b2ba5SJacek Lawrynowicz
620263b2ba5SJacek Lawrynowicz val |= IVPU_MMU_CR0_EVTQEN;
62174ce0f38SKrystian Pradzynski ret = ivpu_mmu_reg_write_cr0(vdev, val);
622263b2ba5SJacek Lawrynowicz if (ret)
623263b2ba5SJacek Lawrynowicz return ret;
624263b2ba5SJacek Lawrynowicz
625263b2ba5SJacek Lawrynowicz val |= IVPU_MMU_CR0_ATSCHK;
62674ce0f38SKrystian Pradzynski ret = ivpu_mmu_reg_write_cr0(vdev, val);
627263b2ba5SJacek Lawrynowicz if (ret)
628263b2ba5SJacek Lawrynowicz return ret;
629263b2ba5SJacek Lawrynowicz
630263b2ba5SJacek Lawrynowicz ret = ivpu_mmu_irqs_setup(vdev);
631263b2ba5SJacek Lawrynowicz if (ret)
632263b2ba5SJacek Lawrynowicz return ret;
633263b2ba5SJacek Lawrynowicz
634263b2ba5SJacek Lawrynowicz val |= IVPU_MMU_CR0_SMMUEN;
63574ce0f38SKrystian Pradzynski return ivpu_mmu_reg_write_cr0(vdev, val);
636263b2ba5SJacek Lawrynowicz }
637263b2ba5SJacek Lawrynowicz
ivpu_mmu_strtab_link_cd(struct ivpu_device * vdev,u32 sid)638263b2ba5SJacek Lawrynowicz static void ivpu_mmu_strtab_link_cd(struct ivpu_device *vdev, u32 sid)
639263b2ba5SJacek Lawrynowicz {
640263b2ba5SJacek Lawrynowicz struct ivpu_mmu_info *mmu = vdev->mmu;
641263b2ba5SJacek Lawrynowicz struct ivpu_mmu_strtab *strtab = &mmu->strtab;
642263b2ba5SJacek Lawrynowicz struct ivpu_mmu_cdtab *cdtab = &mmu->cdtab;
643263b2ba5SJacek Lawrynowicz u64 *entry = strtab->base + (sid * IVPU_MMU_STRTAB_ENT_SIZE);
644263b2ba5SJacek Lawrynowicz u64 str[2];
645263b2ba5SJacek Lawrynowicz
646263b2ba5SJacek Lawrynowicz str[0] = FIELD_PREP(IVPU_MMU_STE_0_CFG, IVPU_MMU_STE_0_CFG_S1_TRANS) |
647263b2ba5SJacek Lawrynowicz FIELD_PREP(IVPU_MMU_STE_0_S1CDMAX, IVPU_MMU_CDTAB_ENT_COUNT_LOG2) |
648263b2ba5SJacek Lawrynowicz FIELD_PREP(IVPU_MMU_STE_0_S1FMT, IVPU_MMU_STE_0_S1FMT_LINEAR) |
649263b2ba5SJacek Lawrynowicz IVPU_MMU_STE_0_V |
650263b2ba5SJacek Lawrynowicz (cdtab->dma & IVPU_MMU_STE_0_S1CTXPTR_MASK);
651263b2ba5SJacek Lawrynowicz
652263b2ba5SJacek Lawrynowicz str[1] = FIELD_PREP(IVPU_MMU_STE_1_S1DSS, IVPU_MMU_STE_1_S1DSS_TERMINATE) |
653263b2ba5SJacek Lawrynowicz FIELD_PREP(IVPU_MMU_STE_1_S1CIR, IVPU_MMU_STE_1_S1C_CACHE_NC) |
654263b2ba5SJacek Lawrynowicz FIELD_PREP(IVPU_MMU_STE_1_S1COR, IVPU_MMU_STE_1_S1C_CACHE_NC) |
655263b2ba5SJacek Lawrynowicz FIELD_PREP(IVPU_MMU_STE_1_S1CSH, IVPU_MMU_SH_NSH) |
656263b2ba5SJacek Lawrynowicz FIELD_PREP(IVPU_MMU_STE_1_PRIVCFG, IVPU_MMU_STE_1_PRIVCFG_UNPRIV) |
657263b2ba5SJacek Lawrynowicz FIELD_PREP(IVPU_MMU_STE_1_INSTCFG, IVPU_MMU_STE_1_INSTCFG_DATA) |
658263b2ba5SJacek Lawrynowicz FIELD_PREP(IVPU_MMU_STE_1_STRW, IVPU_MMU_STE_1_STRW_NSEL1) |
659263b2ba5SJacek Lawrynowicz FIELD_PREP(IVPU_MMU_STE_1_CONT, IVPU_MMU_STRTAB_CFG_LOG2SIZE) |
660263b2ba5SJacek Lawrynowicz IVPU_MMU_STE_1_MEV |
661263b2ba5SJacek Lawrynowicz IVPU_MMU_STE_1_S1STALLD;
662263b2ba5SJacek Lawrynowicz
663263b2ba5SJacek Lawrynowicz WRITE_ONCE(entry[1], str[1]);
664263b2ba5SJacek Lawrynowicz WRITE_ONCE(entry[0], str[0]);
665263b2ba5SJacek Lawrynowicz
6662c3801b1SWachowski, Karol if (!ivpu_is_force_snoop_enabled(vdev))
667263b2ba5SJacek Lawrynowicz clflush_cache_range(entry, IVPU_MMU_STRTAB_ENT_SIZE);
668263b2ba5SJacek Lawrynowicz
669263b2ba5SJacek Lawrynowicz ivpu_dbg(vdev, MMU, "STRTAB write entry (SSID=%u): 0x%llx, 0x%llx\n", sid, str[0], str[1]);
670263b2ba5SJacek Lawrynowicz }
671263b2ba5SJacek Lawrynowicz
ivpu_mmu_strtab_init(struct ivpu_device * vdev)672263b2ba5SJacek Lawrynowicz static int ivpu_mmu_strtab_init(struct ivpu_device *vdev)
673263b2ba5SJacek Lawrynowicz {
674263b2ba5SJacek Lawrynowicz ivpu_mmu_strtab_link_cd(vdev, IVPU_MMU_STREAM_ID0);
675263b2ba5SJacek Lawrynowicz ivpu_mmu_strtab_link_cd(vdev, IVPU_MMU_STREAM_ID3);
676263b2ba5SJacek Lawrynowicz
677263b2ba5SJacek Lawrynowicz return 0;
678263b2ba5SJacek Lawrynowicz }
679263b2ba5SJacek Lawrynowicz
ivpu_mmu_invalidate_tlb(struct ivpu_device * vdev,u16 ssid)680263b2ba5SJacek Lawrynowicz int ivpu_mmu_invalidate_tlb(struct ivpu_device *vdev, u16 ssid)
681263b2ba5SJacek Lawrynowicz {
682263b2ba5SJacek Lawrynowicz struct ivpu_mmu_info *mmu = vdev->mmu;
683b563e479SStanislaw Gruszka int ret = 0;
684263b2ba5SJacek Lawrynowicz
685b563e479SStanislaw Gruszka mutex_lock(&mmu->lock);
686b563e479SStanislaw Gruszka if (!mmu->on)
687263b2ba5SJacek Lawrynowicz goto unlock;
688263b2ba5SJacek Lawrynowicz
689263b2ba5SJacek Lawrynowicz ret = ivpu_mmu_cmdq_write_tlbi_nh_asid(vdev, ssid);
690263b2ba5SJacek Lawrynowicz if (ret)
691263b2ba5SJacek Lawrynowicz goto unlock;
692263b2ba5SJacek Lawrynowicz
693263b2ba5SJacek Lawrynowicz ret = ivpu_mmu_cmdq_sync(vdev);
694263b2ba5SJacek Lawrynowicz unlock:
695263b2ba5SJacek Lawrynowicz mutex_unlock(&mmu->lock);
696263b2ba5SJacek Lawrynowicz return ret;
697263b2ba5SJacek Lawrynowicz }
698263b2ba5SJacek Lawrynowicz
ivpu_mmu_cd_add(struct ivpu_device * vdev,u32 ssid,u64 cd_dma)699263b2ba5SJacek Lawrynowicz static int ivpu_mmu_cd_add(struct ivpu_device *vdev, u32 ssid, u64 cd_dma)
700263b2ba5SJacek Lawrynowicz {
701263b2ba5SJacek Lawrynowicz struct ivpu_mmu_info *mmu = vdev->mmu;
702263b2ba5SJacek Lawrynowicz struct ivpu_mmu_cdtab *cdtab = &mmu->cdtab;
703263b2ba5SJacek Lawrynowicz u64 *entry;
704263b2ba5SJacek Lawrynowicz u64 cd[4];
705b563e479SStanislaw Gruszka int ret = 0;
706263b2ba5SJacek Lawrynowicz
707263b2ba5SJacek Lawrynowicz if (ssid > IVPU_MMU_CDTAB_ENT_COUNT)
708263b2ba5SJacek Lawrynowicz return -EINVAL;
709263b2ba5SJacek Lawrynowicz
710263b2ba5SJacek Lawrynowicz entry = cdtab->base + (ssid * IVPU_MMU_CDTAB_ENT_SIZE);
711263b2ba5SJacek Lawrynowicz
712263b2ba5SJacek Lawrynowicz if (cd_dma != 0) {
713a2fd4a6fSKarol Wachowski cd[0] = FIELD_PREP(IVPU_MMU_CD_0_TCR_T0SZ, IVPU_MMU_T0SZ_48BIT) |
714263b2ba5SJacek Lawrynowicz FIELD_PREP(IVPU_MMU_CD_0_TCR_TG0, 0) |
715263b2ba5SJacek Lawrynowicz FIELD_PREP(IVPU_MMU_CD_0_TCR_IRGN0, 0) |
716263b2ba5SJacek Lawrynowicz FIELD_PREP(IVPU_MMU_CD_0_TCR_ORGN0, 0) |
717263b2ba5SJacek Lawrynowicz FIELD_PREP(IVPU_MMU_CD_0_TCR_SH0, 0) |
718a2fd4a6fSKarol Wachowski FIELD_PREP(IVPU_MMU_CD_0_TCR_IPS, IVPU_MMU_IPS_48BIT) |
719263b2ba5SJacek Lawrynowicz FIELD_PREP(IVPU_MMU_CD_0_ASID, ssid) |
720263b2ba5SJacek Lawrynowicz IVPU_MMU_CD_0_TCR_EPD1 |
721263b2ba5SJacek Lawrynowicz IVPU_MMU_CD_0_AA64 |
722263b2ba5SJacek Lawrynowicz IVPU_MMU_CD_0_R |
723263b2ba5SJacek Lawrynowicz IVPU_MMU_CD_0_ASET |
724263b2ba5SJacek Lawrynowicz IVPU_MMU_CD_0_V;
725263b2ba5SJacek Lawrynowicz cd[1] = cd_dma & IVPU_MMU_CD_1_TTB0_MASK;
726263b2ba5SJacek Lawrynowicz cd[2] = 0;
727263b2ba5SJacek Lawrynowicz cd[3] = 0x0000000000007444;
728263b2ba5SJacek Lawrynowicz
729263b2ba5SJacek Lawrynowicz /* For global context generate memory fault on VPU */
730263b2ba5SJacek Lawrynowicz if (ssid == IVPU_GLOBAL_CONTEXT_MMU_SSID)
731263b2ba5SJacek Lawrynowicz cd[0] |= IVPU_MMU_CD_0_A;
732263b2ba5SJacek Lawrynowicz } else {
733263b2ba5SJacek Lawrynowicz memset(cd, 0, sizeof(cd));
734263b2ba5SJacek Lawrynowicz }
735263b2ba5SJacek Lawrynowicz
736263b2ba5SJacek Lawrynowicz WRITE_ONCE(entry[1], cd[1]);
737263b2ba5SJacek Lawrynowicz WRITE_ONCE(entry[2], cd[2]);
738263b2ba5SJacek Lawrynowicz WRITE_ONCE(entry[3], cd[3]);
739263b2ba5SJacek Lawrynowicz WRITE_ONCE(entry[0], cd[0]);
740263b2ba5SJacek Lawrynowicz
7412c3801b1SWachowski, Karol if (!ivpu_is_force_snoop_enabled(vdev))
742263b2ba5SJacek Lawrynowicz clflush_cache_range(entry, IVPU_MMU_CDTAB_ENT_SIZE);
743263b2ba5SJacek Lawrynowicz
744263b2ba5SJacek Lawrynowicz ivpu_dbg(vdev, MMU, "CDTAB %s entry (SSID=%u, dma=%pad): 0x%llx, 0x%llx, 0x%llx, 0x%llx\n",
745263b2ba5SJacek Lawrynowicz cd_dma ? "write" : "clear", ssid, &cd_dma, cd[0], cd[1], cd[2], cd[3]);
746263b2ba5SJacek Lawrynowicz
747b563e479SStanislaw Gruszka mutex_lock(&mmu->lock);
748b563e479SStanislaw Gruszka if (!mmu->on)
749263b2ba5SJacek Lawrynowicz goto unlock;
750263b2ba5SJacek Lawrynowicz
751263b2ba5SJacek Lawrynowicz ret = ivpu_mmu_cmdq_write_cfgi_all(vdev);
752263b2ba5SJacek Lawrynowicz if (ret)
753263b2ba5SJacek Lawrynowicz goto unlock;
754263b2ba5SJacek Lawrynowicz
755263b2ba5SJacek Lawrynowicz ret = ivpu_mmu_cmdq_sync(vdev);
756263b2ba5SJacek Lawrynowicz unlock:
757263b2ba5SJacek Lawrynowicz mutex_unlock(&mmu->lock);
758263b2ba5SJacek Lawrynowicz return ret;
759263b2ba5SJacek Lawrynowicz }
760263b2ba5SJacek Lawrynowicz
ivpu_mmu_cd_add_gbl(struct ivpu_device * vdev)761263b2ba5SJacek Lawrynowicz static int ivpu_mmu_cd_add_gbl(struct ivpu_device *vdev)
762263b2ba5SJacek Lawrynowicz {
763263b2ba5SJacek Lawrynowicz int ret;
764263b2ba5SJacek Lawrynowicz
765263b2ba5SJacek Lawrynowicz ret = ivpu_mmu_cd_add(vdev, 0, vdev->gctx.pgtable.pgd_dma);
766263b2ba5SJacek Lawrynowicz if (ret)
767263b2ba5SJacek Lawrynowicz ivpu_err(vdev, "Failed to add global CD entry: %d\n", ret);
768263b2ba5SJacek Lawrynowicz
769263b2ba5SJacek Lawrynowicz return ret;
770263b2ba5SJacek Lawrynowicz }
771263b2ba5SJacek Lawrynowicz
ivpu_mmu_cd_add_user(struct ivpu_device * vdev,u32 ssid,dma_addr_t cd_dma)772263b2ba5SJacek Lawrynowicz static int ivpu_mmu_cd_add_user(struct ivpu_device *vdev, u32 ssid, dma_addr_t cd_dma)
773263b2ba5SJacek Lawrynowicz {
774263b2ba5SJacek Lawrynowicz int ret;
775263b2ba5SJacek Lawrynowicz
776263b2ba5SJacek Lawrynowicz if (ssid == 0) {
777263b2ba5SJacek Lawrynowicz ivpu_err(vdev, "Invalid SSID: %u\n", ssid);
778263b2ba5SJacek Lawrynowicz return -EINVAL;
779263b2ba5SJacek Lawrynowicz }
780263b2ba5SJacek Lawrynowicz
781263b2ba5SJacek Lawrynowicz ret = ivpu_mmu_cd_add(vdev, ssid, cd_dma);
782263b2ba5SJacek Lawrynowicz if (ret)
783263b2ba5SJacek Lawrynowicz ivpu_err(vdev, "Failed to add CD entry SSID=%u: %d\n", ssid, ret);
784263b2ba5SJacek Lawrynowicz
785263b2ba5SJacek Lawrynowicz return ret;
786263b2ba5SJacek Lawrynowicz }
787263b2ba5SJacek Lawrynowicz
ivpu_mmu_init(struct ivpu_device * vdev)788263b2ba5SJacek Lawrynowicz int ivpu_mmu_init(struct ivpu_device *vdev)
789263b2ba5SJacek Lawrynowicz {
790263b2ba5SJacek Lawrynowicz struct ivpu_mmu_info *mmu = vdev->mmu;
791263b2ba5SJacek Lawrynowicz int ret;
792263b2ba5SJacek Lawrynowicz
793263b2ba5SJacek Lawrynowicz ivpu_dbg(vdev, MMU, "Init..\n");
794263b2ba5SJacek Lawrynowicz
795263b2ba5SJacek Lawrynowicz ivpu_mmu_config_check(vdev);
796263b2ba5SJacek Lawrynowicz
79748aea7f2SJacek Lawrynowicz ret = drmm_mutex_init(&vdev->drm, &mmu->lock);
79848aea7f2SJacek Lawrynowicz if (ret)
79948aea7f2SJacek Lawrynowicz return ret;
80048aea7f2SJacek Lawrynowicz
801263b2ba5SJacek Lawrynowicz ret = ivpu_mmu_structs_alloc(vdev);
802263b2ba5SJacek Lawrynowicz if (ret)
803263b2ba5SJacek Lawrynowicz return ret;
804263b2ba5SJacek Lawrynowicz
805263b2ba5SJacek Lawrynowicz ret = ivpu_mmu_strtab_init(vdev);
806263b2ba5SJacek Lawrynowicz if (ret) {
807263b2ba5SJacek Lawrynowicz ivpu_err(vdev, "Failed to initialize strtab: %d\n", ret);
808263b2ba5SJacek Lawrynowicz return ret;
809263b2ba5SJacek Lawrynowicz }
810263b2ba5SJacek Lawrynowicz
811263b2ba5SJacek Lawrynowicz ret = ivpu_mmu_cd_add_gbl(vdev);
812263b2ba5SJacek Lawrynowicz if (ret) {
813263b2ba5SJacek Lawrynowicz ivpu_err(vdev, "Failed to initialize strtab: %d\n", ret);
814263b2ba5SJacek Lawrynowicz return ret;
815263b2ba5SJacek Lawrynowicz }
816263b2ba5SJacek Lawrynowicz
817263b2ba5SJacek Lawrynowicz ret = ivpu_mmu_enable(vdev);
818263b2ba5SJacek Lawrynowicz if (ret) {
819263b2ba5SJacek Lawrynowicz ivpu_err(vdev, "Failed to resume MMU: %d\n", ret);
820263b2ba5SJacek Lawrynowicz return ret;
821263b2ba5SJacek Lawrynowicz }
822263b2ba5SJacek Lawrynowicz
823263b2ba5SJacek Lawrynowicz ivpu_dbg(vdev, MMU, "Init done\n");
824263b2ba5SJacek Lawrynowicz
825263b2ba5SJacek Lawrynowicz return 0;
826263b2ba5SJacek Lawrynowicz }
827263b2ba5SJacek Lawrynowicz
ivpu_mmu_enable(struct ivpu_device * vdev)828263b2ba5SJacek Lawrynowicz int ivpu_mmu_enable(struct ivpu_device *vdev)
829263b2ba5SJacek Lawrynowicz {
830263b2ba5SJacek Lawrynowicz struct ivpu_mmu_info *mmu = vdev->mmu;
831263b2ba5SJacek Lawrynowicz int ret;
832263b2ba5SJacek Lawrynowicz
833263b2ba5SJacek Lawrynowicz mutex_lock(&mmu->lock);
834263b2ba5SJacek Lawrynowicz
835263b2ba5SJacek Lawrynowicz mmu->on = true;
836263b2ba5SJacek Lawrynowicz
837263b2ba5SJacek Lawrynowicz ret = ivpu_mmu_reset(vdev);
838263b2ba5SJacek Lawrynowicz if (ret) {
839263b2ba5SJacek Lawrynowicz ivpu_err(vdev, "Failed to reset MMU: %d\n", ret);
840263b2ba5SJacek Lawrynowicz goto err;
841263b2ba5SJacek Lawrynowicz }
842263b2ba5SJacek Lawrynowicz
843263b2ba5SJacek Lawrynowicz ret = ivpu_mmu_cmdq_write_cfgi_all(vdev);
844263b2ba5SJacek Lawrynowicz if (ret)
845263b2ba5SJacek Lawrynowicz goto err;
846263b2ba5SJacek Lawrynowicz
847263b2ba5SJacek Lawrynowicz ret = ivpu_mmu_cmdq_write_tlbi_nsnh_all(vdev);
848263b2ba5SJacek Lawrynowicz if (ret)
849263b2ba5SJacek Lawrynowicz goto err;
850263b2ba5SJacek Lawrynowicz
851263b2ba5SJacek Lawrynowicz ret = ivpu_mmu_cmdq_sync(vdev);
852263b2ba5SJacek Lawrynowicz if (ret)
853263b2ba5SJacek Lawrynowicz goto err;
854263b2ba5SJacek Lawrynowicz
855263b2ba5SJacek Lawrynowicz mutex_unlock(&mmu->lock);
856263b2ba5SJacek Lawrynowicz
857263b2ba5SJacek Lawrynowicz return 0;
858263b2ba5SJacek Lawrynowicz err:
859263b2ba5SJacek Lawrynowicz mmu->on = false;
860263b2ba5SJacek Lawrynowicz mutex_unlock(&mmu->lock);
861263b2ba5SJacek Lawrynowicz return ret;
862263b2ba5SJacek Lawrynowicz }
863263b2ba5SJacek Lawrynowicz
ivpu_mmu_disable(struct ivpu_device * vdev)864263b2ba5SJacek Lawrynowicz void ivpu_mmu_disable(struct ivpu_device *vdev)
865263b2ba5SJacek Lawrynowicz {
866263b2ba5SJacek Lawrynowicz struct ivpu_mmu_info *mmu = vdev->mmu;
867263b2ba5SJacek Lawrynowicz
868263b2ba5SJacek Lawrynowicz mutex_lock(&mmu->lock);
869263b2ba5SJacek Lawrynowicz mmu->on = false;
870263b2ba5SJacek Lawrynowicz mutex_unlock(&mmu->lock);
871263b2ba5SJacek Lawrynowicz }
872263b2ba5SJacek Lawrynowicz
ivpu_mmu_dump_event(struct ivpu_device * vdev,u32 * event)873263b2ba5SJacek Lawrynowicz static void ivpu_mmu_dump_event(struct ivpu_device *vdev, u32 *event)
874263b2ba5SJacek Lawrynowicz {
875263b2ba5SJacek Lawrynowicz u32 ssid = FIELD_GET(IVPU_MMU_EVT_SSID_MASK, event[0]);
876263b2ba5SJacek Lawrynowicz u32 op = FIELD_GET(IVPU_MMU_EVT_OP_MASK, event[0]);
877263b2ba5SJacek Lawrynowicz u64 fetch_addr = ((u64)event[7]) << 32 | event[6];
878263b2ba5SJacek Lawrynowicz u64 in_addr = ((u64)event[5]) << 32 | event[4];
879263b2ba5SJacek Lawrynowicz u32 sid = event[1];
880263b2ba5SJacek Lawrynowicz
881*b7ed87ffSMaciej Falkowski ivpu_err_ratelimited(vdev, "MMU EVTQ: 0x%x (%s) SSID: %d SID: %d, e[2] %08x, e[3] %08x, in addr: 0x%llx, fetch addr: 0x%llx\n",
882*b7ed87ffSMaciej Falkowski op, ivpu_mmu_event_to_str(op), ssid, sid,
883*b7ed87ffSMaciej Falkowski event[2], event[3], in_addr, fetch_addr);
884263b2ba5SJacek Lawrynowicz }
885263b2ba5SJacek Lawrynowicz
ivpu_mmu_get_event(struct ivpu_device * vdev)886263b2ba5SJacek Lawrynowicz static u32 *ivpu_mmu_get_event(struct ivpu_device *vdev)
887263b2ba5SJacek Lawrynowicz {
888263b2ba5SJacek Lawrynowicz struct ivpu_mmu_queue *evtq = &vdev->mmu->evtq;
889263b2ba5SJacek Lawrynowicz u32 idx = IVPU_MMU_Q_IDX(evtq->cons);
890263b2ba5SJacek Lawrynowicz u32 *evt = evtq->base + (idx * IVPU_MMU_EVTQ_CMD_SIZE);
891263b2ba5SJacek Lawrynowicz
892beaf3ebfSJacek Lawrynowicz evtq->prod = REGV_RD32(IVPU_MMU_REG_EVTQ_PROD_SEC);
893b039f1c4SWachowski, Karol if (ivpu_mmu_queue_is_empty(evtq))
894263b2ba5SJacek Lawrynowicz return NULL;
895263b2ba5SJacek Lawrynowicz
896263b2ba5SJacek Lawrynowicz evtq->cons = (evtq->cons + 1) & IVPU_MMU_Q_WRAP_MASK;
897263b2ba5SJacek Lawrynowicz return evt;
898263b2ba5SJacek Lawrynowicz }
899263b2ba5SJacek Lawrynowicz
ivpu_mmu_irq_evtq_handler(struct ivpu_device * vdev)900263b2ba5SJacek Lawrynowicz void ivpu_mmu_irq_evtq_handler(struct ivpu_device *vdev)
901263b2ba5SJacek Lawrynowicz {
902263b2ba5SJacek Lawrynowicz u32 *event;
903263b2ba5SJacek Lawrynowicz u32 ssid;
904263b2ba5SJacek Lawrynowicz
905263b2ba5SJacek Lawrynowicz ivpu_dbg(vdev, IRQ, "MMU event queue\n");
906263b2ba5SJacek Lawrynowicz
907263b2ba5SJacek Lawrynowicz while ((event = ivpu_mmu_get_event(vdev)) != NULL) {
908263b2ba5SJacek Lawrynowicz ivpu_mmu_dump_event(vdev, event);
909263b2ba5SJacek Lawrynowicz
910263b2ba5SJacek Lawrynowicz ssid = FIELD_GET(IVPU_MMU_EVT_SSID_MASK, event[0]);
91127d19268SJacek Lawrynowicz if (ssid == IVPU_GLOBAL_CONTEXT_MMU_SSID) {
91227d19268SJacek Lawrynowicz ivpu_pm_trigger_recovery(vdev, "MMU event");
91327d19268SJacek Lawrynowicz return;
914263b2ba5SJacek Lawrynowicz }
915852be13fSJacek Lawrynowicz
91627d19268SJacek Lawrynowicz ivpu_mmu_user_context_mark_invalid(vdev, ssid);
917b039f1c4SWachowski, Karol REGV_WR32(IVPU_MMU_REG_EVTQ_CONS_SEC, vdev->mmu->evtq.cons);
91827d19268SJacek Lawrynowicz }
919*b7ed87ffSMaciej Falkowski
920*b7ed87ffSMaciej Falkowski if (!kfifo_put(&vdev->hw->irq.fifo, IVPU_HW_IRQ_SRC_MMU_EVTQ))
921*b7ed87ffSMaciej Falkowski ivpu_err_ratelimited(vdev, "IRQ FIFO full\n");
922263b2ba5SJacek Lawrynowicz }
923263b2ba5SJacek Lawrynowicz
ivpu_mmu_evtq_dump(struct ivpu_device * vdev)92430cf36bbSWachowski, Karol void ivpu_mmu_evtq_dump(struct ivpu_device *vdev)
92530cf36bbSWachowski, Karol {
92630cf36bbSWachowski, Karol u32 *event;
92730cf36bbSWachowski, Karol
92830cf36bbSWachowski, Karol while ((event = ivpu_mmu_get_event(vdev)) != NULL)
92930cf36bbSWachowski, Karol ivpu_mmu_dump_event(vdev, event);
93030cf36bbSWachowski, Karol }
93130cf36bbSWachowski, Karol
ivpu_mmu_irq_gerr_handler(struct ivpu_device * vdev)932263b2ba5SJacek Lawrynowicz void ivpu_mmu_irq_gerr_handler(struct ivpu_device *vdev)
933263b2ba5SJacek Lawrynowicz {
934263b2ba5SJacek Lawrynowicz u32 gerror_val, gerrorn_val, active;
935263b2ba5SJacek Lawrynowicz
936263b2ba5SJacek Lawrynowicz ivpu_dbg(vdev, IRQ, "MMU error\n");
937263b2ba5SJacek Lawrynowicz
938beaf3ebfSJacek Lawrynowicz gerror_val = REGV_RD32(IVPU_MMU_REG_GERROR);
939beaf3ebfSJacek Lawrynowicz gerrorn_val = REGV_RD32(IVPU_MMU_REG_GERRORN);
940263b2ba5SJacek Lawrynowicz
941263b2ba5SJacek Lawrynowicz active = gerror_val ^ gerrorn_val;
942263b2ba5SJacek Lawrynowicz if (!(active & IVPU_MMU_GERROR_ERR_MASK))
943263b2ba5SJacek Lawrynowicz return;
944263b2ba5SJacek Lawrynowicz
945beaf3ebfSJacek Lawrynowicz if (REG_TEST_FLD(IVPU_MMU_REG_GERROR, MSI_ABT, active))
946263b2ba5SJacek Lawrynowicz ivpu_warn_ratelimited(vdev, "MMU MSI ABT write aborted\n");
947263b2ba5SJacek Lawrynowicz
948beaf3ebfSJacek Lawrynowicz if (REG_TEST_FLD(IVPU_MMU_REG_GERROR, MSI_PRIQ_ABT, active))
949263b2ba5SJacek Lawrynowicz ivpu_warn_ratelimited(vdev, "MMU PRIQ MSI ABT write aborted\n");
950263b2ba5SJacek Lawrynowicz
951beaf3ebfSJacek Lawrynowicz if (REG_TEST_FLD(IVPU_MMU_REG_GERROR, MSI_EVTQ_ABT, active))
952263b2ba5SJacek Lawrynowicz ivpu_warn_ratelimited(vdev, "MMU EVTQ MSI ABT write aborted\n");
953263b2ba5SJacek Lawrynowicz
954beaf3ebfSJacek Lawrynowicz if (REG_TEST_FLD(IVPU_MMU_REG_GERROR, MSI_CMDQ_ABT, active))
955263b2ba5SJacek Lawrynowicz ivpu_warn_ratelimited(vdev, "MMU CMDQ MSI ABT write aborted\n");
956263b2ba5SJacek Lawrynowicz
957beaf3ebfSJacek Lawrynowicz if (REG_TEST_FLD(IVPU_MMU_REG_GERROR, PRIQ_ABT, active))
958263b2ba5SJacek Lawrynowicz ivpu_err_ratelimited(vdev, "MMU PRIQ write aborted\n");
959263b2ba5SJacek Lawrynowicz
960beaf3ebfSJacek Lawrynowicz if (REG_TEST_FLD(IVPU_MMU_REG_GERROR, EVTQ_ABT, active))
961263b2ba5SJacek Lawrynowicz ivpu_err_ratelimited(vdev, "MMU EVTQ write aborted\n");
962263b2ba5SJacek Lawrynowicz
963beaf3ebfSJacek Lawrynowicz if (REG_TEST_FLD(IVPU_MMU_REG_GERROR, CMDQ, active))
964263b2ba5SJacek Lawrynowicz ivpu_err_ratelimited(vdev, "MMU CMDQ write aborted\n");
965263b2ba5SJacek Lawrynowicz
966beaf3ebfSJacek Lawrynowicz REGV_WR32(IVPU_MMU_REG_GERRORN, gerror_val);
967263b2ba5SJacek Lawrynowicz }
968263b2ba5SJacek Lawrynowicz
ivpu_mmu_set_pgtable(struct ivpu_device * vdev,int ssid,struct ivpu_mmu_pgtable * pgtable)969263b2ba5SJacek Lawrynowicz int ivpu_mmu_set_pgtable(struct ivpu_device *vdev, int ssid, struct ivpu_mmu_pgtable *pgtable)
970263b2ba5SJacek Lawrynowicz {
971263b2ba5SJacek Lawrynowicz return ivpu_mmu_cd_add_user(vdev, ssid, pgtable->pgd_dma);
972263b2ba5SJacek Lawrynowicz }
973263b2ba5SJacek Lawrynowicz
ivpu_mmu_clear_pgtable(struct ivpu_device * vdev,int ssid)974263b2ba5SJacek Lawrynowicz void ivpu_mmu_clear_pgtable(struct ivpu_device *vdev, int ssid)
975263b2ba5SJacek Lawrynowicz {
976263b2ba5SJacek Lawrynowicz ivpu_mmu_cd_add_user(vdev, ssid, 0); /* 0 will clear CD entry */
977263b2ba5SJacek Lawrynowicz }
978