smmuv3-internal.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. /*
  2. * ARM SMMUv3 support - Internal API
  3. *
  4. * Copyright (C) 2014-2016 Broadcom Corporation
  5. * Copyright (c) 2017 Red Hat, Inc.
  6. * Written by Prem Mallappa, Eric Auger
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #ifndef HW_ARM_SMMUV3_INTERNAL_H
  21. #define HW_ARM_SMMUV3_INTERNAL_H
  22. #include "hw/registerfields.h"
  23. #include "hw/arm/smmu-common.h"
  24. typedef enum SMMUTranslationStatus {
  25. SMMU_TRANS_DISABLE,
  26. SMMU_TRANS_ABORT,
  27. SMMU_TRANS_BYPASS,
  28. SMMU_TRANS_ERROR,
  29. SMMU_TRANS_SUCCESS,
  30. } SMMUTranslationStatus;
  31. typedef enum SMMUTranslationClass {
  32. SMMU_CLASS_CD,
  33. SMMU_CLASS_TT,
  34. SMMU_CLASS_IN,
  35. } SMMUTranslationClass;
  36. /* MMIO Registers */
  37. REG32(IDR0, 0x0)
  38. FIELD(IDR0, S2P, 0 , 1)
  39. FIELD(IDR0, S1P, 1 , 1)
  40. FIELD(IDR0, TTF, 2 , 2)
  41. FIELD(IDR0, COHACC, 4 , 1)
  42. FIELD(IDR0, BTM, 5 , 1)
  43. FIELD(IDR0, HTTU, 6 , 2)
  44. FIELD(IDR0, DORMHINT, 8 , 1)
  45. FIELD(IDR0, HYP, 9 , 1)
  46. FIELD(IDR0, ATS, 10, 1)
  47. FIELD(IDR0, NS1ATS, 11, 1)
  48. FIELD(IDR0, ASID16, 12, 1)
  49. FIELD(IDR0, MSI, 13, 1)
  50. FIELD(IDR0, SEV, 14, 1)
  51. FIELD(IDR0, ATOS, 15, 1)
  52. FIELD(IDR0, PRI, 16, 1)
  53. FIELD(IDR0, VMW, 17, 1)
  54. FIELD(IDR0, VMID16, 18, 1)
  55. FIELD(IDR0, CD2L, 19, 1)
  56. FIELD(IDR0, VATOS, 20, 1)
  57. FIELD(IDR0, TTENDIAN, 21, 2)
  58. FIELD(IDR0, ATSRECERR, 23, 1)
  59. FIELD(IDR0, STALL_MODEL, 24, 2)
  60. FIELD(IDR0, TERM_MODEL, 26, 1)
  61. FIELD(IDR0, STLEVEL, 27, 2)
  62. FIELD(IDR0, RME_IMPL, 30, 1)
  63. REG32(IDR1, 0x4)
  64. FIELD(IDR1, SIDSIZE, 0 , 6)
  65. FIELD(IDR1, SSIDSIZE, 6 , 5)
  66. FIELD(IDR1, PRIQS, 11, 5)
  67. FIELD(IDR1, EVENTQS, 16, 5)
  68. FIELD(IDR1, CMDQS, 21, 5)
  69. FIELD(IDR1, ATTR_PERMS_OVR, 26, 1)
  70. FIELD(IDR1, ATTR_TYPES_OVR, 27, 1)
  71. FIELD(IDR1, REL, 28, 1)
  72. FIELD(IDR1, QUEUES_PRESET, 29, 1)
  73. FIELD(IDR1, TABLES_PRESET, 30, 1)
  74. FIELD(IDR1, ECMDQ, 31, 1)
  75. #define SMMU_IDR1_SIDSIZE 16
  76. #define SMMU_CMDQS 19
  77. #define SMMU_EVENTQS 19
  78. REG32(IDR2, 0x8)
  79. FIELD(IDR2, BA_VATOS, 0, 10)
  80. REG32(IDR3, 0xc)
  81. FIELD(IDR3, HAD, 2, 1);
  82. FIELD(IDR3, PBHA, 3, 1);
  83. FIELD(IDR3, XNX, 4, 1);
  84. FIELD(IDR3, PPS, 5, 1);
  85. FIELD(IDR3, MPAM, 7, 1);
  86. FIELD(IDR3, FWB, 8, 1);
  87. FIELD(IDR3, STT, 9, 1);
  88. FIELD(IDR3, RIL, 10, 1);
  89. FIELD(IDR3, BBML, 11, 2);
  90. FIELD(IDR3, E0PD, 13, 1);
  91. FIELD(IDR3, PTWNNC, 14, 1);
  92. FIELD(IDR3, DPT, 15, 1);
  93. REG32(IDR4, 0x10)
  94. REG32(IDR5, 0x14)
  95. FIELD(IDR5, OAS, 0, 3);
  96. FIELD(IDR5, GRAN4K, 4, 1);
  97. FIELD(IDR5, GRAN16K, 5, 1);
  98. FIELD(IDR5, GRAN64K, 6, 1);
  99. FIELD(IDR5, VAX, 10, 2);
  100. FIELD(IDR5, STALL_MAX, 16, 16);
  101. #define SMMU_IDR5_OAS 4
  102. REG32(IIDR, 0x18)
  103. REG32(AIDR, 0x1c)
  104. REG32(CR0, 0x20)
  105. FIELD(CR0, SMMU_ENABLE, 0, 1)
  106. FIELD(CR0, EVENTQEN, 2, 1)
  107. FIELD(CR0, CMDQEN, 3, 1)
  108. #define SMMU_CR0_RESERVED 0xFFFFFC20
  109. REG32(CR0ACK, 0x24)
  110. REG32(CR1, 0x28)
  111. REG32(CR2, 0x2c)
  112. REG32(STATUSR, 0x40)
  113. REG32(GBPA, 0x44)
  114. FIELD(GBPA, ABORT, 20, 1)
  115. FIELD(GBPA, UPDATE, 31, 1)
  116. /* Use incoming. */
  117. #define SMMU_GBPA_RESET_VAL 0x1000
  118. REG32(IRQ_CTRL, 0x50)
  119. FIELD(IRQ_CTRL, GERROR_IRQEN, 0, 1)
  120. FIELD(IRQ_CTRL, PRI_IRQEN, 1, 1)
  121. FIELD(IRQ_CTRL, EVENTQ_IRQEN, 2, 1)
  122. REG32(IRQ_CTRL_ACK, 0x54)
  123. REG32(GERROR, 0x60)
  124. FIELD(GERROR, CMDQ_ERR, 0, 1)
  125. FIELD(GERROR, EVENTQ_ABT_ERR, 2, 1)
  126. FIELD(GERROR, PRIQ_ABT_ERR, 3, 1)
  127. FIELD(GERROR, MSI_CMDQ_ABT_ERR, 4, 1)
  128. FIELD(GERROR, MSI_EVENTQ_ABT_ERR, 5, 1)
  129. FIELD(GERROR, MSI_PRIQ_ABT_ERR, 6, 1)
  130. FIELD(GERROR, MSI_GERROR_ABT_ERR, 7, 1)
  131. FIELD(GERROR, MSI_SFM_ERR, 8, 1)
  132. REG32(GERRORN, 0x64)
  133. #define A_GERROR_IRQ_CFG0 0x68 /* 64b */
  134. REG32(GERROR_IRQ_CFG1, 0x70)
  135. REG32(GERROR_IRQ_CFG2, 0x74)
  136. #define A_STRTAB_BASE 0x80 /* 64b */
  137. #define SMMU_BASE_ADDR_MASK 0xfffffffffffc0
  138. REG32(STRTAB_BASE_CFG, 0x88)
  139. FIELD(STRTAB_BASE_CFG, FMT, 16, 2)
  140. FIELD(STRTAB_BASE_CFG, SPLIT, 6 , 5)
  141. FIELD(STRTAB_BASE_CFG, LOG2SIZE, 0 , 6)
  142. #define A_CMDQ_BASE 0x90 /* 64b */
  143. REG32(CMDQ_PROD, 0x98)
  144. REG32(CMDQ_CONS, 0x9c)
  145. FIELD(CMDQ_CONS, ERR, 24, 7)
  146. #define A_EVENTQ_BASE 0xa0 /* 64b */
  147. REG32(EVENTQ_PROD, 0xa8)
  148. REG32(EVENTQ_CONS, 0xac)
  149. #define A_EVENTQ_IRQ_CFG0 0xb0 /* 64b */
  150. REG32(EVENTQ_IRQ_CFG1, 0xb8)
  151. REG32(EVENTQ_IRQ_CFG2, 0xbc)
  152. #define A_IDREGS 0xfd0
  153. static inline int smmu_enabled(SMMUv3State *s)
  154. {
  155. return FIELD_EX32(s->cr[0], CR0, SMMU_ENABLE);
  156. }
  157. /* Command Queue Entry */
  158. typedef struct Cmd {
  159. uint32_t word[4];
  160. } Cmd;
  161. /* Event Queue Entry */
  162. typedef struct Evt {
  163. uint32_t word[8];
  164. } Evt;
  165. static inline uint32_t smmuv3_idreg(int regoffset)
  166. {
  167. /*
  168. * Return the value of the Primecell/Corelink ID registers at the
  169. * specified offset from the first ID register.
  170. * These value indicate an ARM implementation of MMU600 p1
  171. */
  172. static const uint8_t smmuv3_ids[] = {
  173. 0x04, 0, 0, 0, 0x84, 0xB4, 0xF0, 0x10, 0x0D, 0xF0, 0x05, 0xB1
  174. };
  175. return smmuv3_ids[regoffset / 4];
  176. }
  177. static inline bool smmuv3_eventq_irq_enabled(SMMUv3State *s)
  178. {
  179. return FIELD_EX32(s->irq_ctrl, IRQ_CTRL, EVENTQ_IRQEN);
  180. }
  181. static inline bool smmuv3_gerror_irq_enabled(SMMUv3State *s)
  182. {
  183. return FIELD_EX32(s->irq_ctrl, IRQ_CTRL, GERROR_IRQEN);
  184. }
  185. /* Queue Handling */
  186. #define Q_BASE(q) ((q)->base & SMMU_BASE_ADDR_MASK)
  187. #define WRAP_MASK(q) (1 << (q)->log2size)
  188. #define INDEX_MASK(q) (((1 << (q)->log2size)) - 1)
  189. #define WRAP_INDEX_MASK(q) ((1 << ((q)->log2size + 1)) - 1)
  190. #define Q_CONS(q) ((q)->cons & INDEX_MASK(q))
  191. #define Q_PROD(q) ((q)->prod & INDEX_MASK(q))
  192. #define Q_CONS_ENTRY(q) (Q_BASE(q) + (q)->entry_size * Q_CONS(q))
  193. #define Q_PROD_ENTRY(q) (Q_BASE(q) + (q)->entry_size * Q_PROD(q))
  194. #define Q_CONS_WRAP(q) (((q)->cons & WRAP_MASK(q)) >> (q)->log2size)
  195. #define Q_PROD_WRAP(q) (((q)->prod & WRAP_MASK(q)) >> (q)->log2size)
  196. static inline bool smmuv3_q_full(SMMUQueue *q)
  197. {
  198. return ((q->cons ^ q->prod) & WRAP_INDEX_MASK(q)) == WRAP_MASK(q);
  199. }
  200. static inline bool smmuv3_q_empty(SMMUQueue *q)
  201. {
  202. return (q->cons & WRAP_INDEX_MASK(q)) == (q->prod & WRAP_INDEX_MASK(q));
  203. }
  204. static inline void queue_prod_incr(SMMUQueue *q)
  205. {
  206. q->prod = (q->prod + 1) & WRAP_INDEX_MASK(q);
  207. }
  208. static inline void queue_cons_incr(SMMUQueue *q)
  209. {
  210. /*
  211. * We have to use deposit for the CONS registers to preserve
  212. * the ERR field in the high bits.
  213. */
  214. q->cons = deposit32(q->cons, 0, q->log2size + 1, q->cons + 1);
  215. }
  216. static inline bool smmuv3_cmdq_enabled(SMMUv3State *s)
  217. {
  218. return FIELD_EX32(s->cr[0], CR0, CMDQEN);
  219. }
  220. static inline bool smmuv3_eventq_enabled(SMMUv3State *s)
  221. {
  222. return FIELD_EX32(s->cr[0], CR0, EVENTQEN);
  223. }
  224. static inline void smmu_write_cmdq_err(SMMUv3State *s, uint32_t err_type)
  225. {
  226. s->cmdq.cons = FIELD_DP32(s->cmdq.cons, CMDQ_CONS, ERR, err_type);
  227. }
  228. /* Commands */
  229. typedef enum SMMUCommandType {
  230. SMMU_CMD_NONE = 0x00,
  231. SMMU_CMD_PREFETCH_CONFIG ,
  232. SMMU_CMD_PREFETCH_ADDR,
  233. SMMU_CMD_CFGI_STE,
  234. SMMU_CMD_CFGI_STE_RANGE,
  235. SMMU_CMD_CFGI_CD,
  236. SMMU_CMD_CFGI_CD_ALL,
  237. SMMU_CMD_CFGI_ALL,
  238. SMMU_CMD_TLBI_NH_ALL = 0x10,
  239. SMMU_CMD_TLBI_NH_ASID,
  240. SMMU_CMD_TLBI_NH_VA,
  241. SMMU_CMD_TLBI_NH_VAA,
  242. SMMU_CMD_TLBI_EL3_ALL = 0x18,
  243. SMMU_CMD_TLBI_EL3_VA = 0x1a,
  244. SMMU_CMD_TLBI_EL2_ALL = 0x20,
  245. SMMU_CMD_TLBI_EL2_ASID,
  246. SMMU_CMD_TLBI_EL2_VA,
  247. SMMU_CMD_TLBI_EL2_VAA,
  248. SMMU_CMD_TLBI_S12_VMALL = 0x28,
  249. SMMU_CMD_TLBI_S2_IPA = 0x2a,
  250. SMMU_CMD_TLBI_NSNH_ALL = 0x30,
  251. SMMU_CMD_ATC_INV = 0x40,
  252. SMMU_CMD_PRI_RESP,
  253. SMMU_CMD_RESUME = 0x44,
  254. SMMU_CMD_STALL_TERM,
  255. SMMU_CMD_SYNC,
  256. } SMMUCommandType;
  257. static const char *cmd_stringify[] = {
  258. [SMMU_CMD_PREFETCH_CONFIG] = "SMMU_CMD_PREFETCH_CONFIG",
  259. [SMMU_CMD_PREFETCH_ADDR] = "SMMU_CMD_PREFETCH_ADDR",
  260. [SMMU_CMD_CFGI_STE] = "SMMU_CMD_CFGI_STE",
  261. [SMMU_CMD_CFGI_STE_RANGE] = "SMMU_CMD_CFGI_STE_RANGE",
  262. [SMMU_CMD_CFGI_CD] = "SMMU_CMD_CFGI_CD",
  263. [SMMU_CMD_CFGI_CD_ALL] = "SMMU_CMD_CFGI_CD_ALL",
  264. [SMMU_CMD_CFGI_ALL] = "SMMU_CMD_CFGI_ALL",
  265. [SMMU_CMD_TLBI_NH_ALL] = "SMMU_CMD_TLBI_NH_ALL",
  266. [SMMU_CMD_TLBI_NH_ASID] = "SMMU_CMD_TLBI_NH_ASID",
  267. [SMMU_CMD_TLBI_NH_VA] = "SMMU_CMD_TLBI_NH_VA",
  268. [SMMU_CMD_TLBI_NH_VAA] = "SMMU_CMD_TLBI_NH_VAA",
  269. [SMMU_CMD_TLBI_EL3_ALL] = "SMMU_CMD_TLBI_EL3_ALL",
  270. [SMMU_CMD_TLBI_EL3_VA] = "SMMU_CMD_TLBI_EL3_VA",
  271. [SMMU_CMD_TLBI_EL2_ALL] = "SMMU_CMD_TLBI_EL2_ALL",
  272. [SMMU_CMD_TLBI_EL2_ASID] = "SMMU_CMD_TLBI_EL2_ASID",
  273. [SMMU_CMD_TLBI_EL2_VA] = "SMMU_CMD_TLBI_EL2_VA",
  274. [SMMU_CMD_TLBI_EL2_VAA] = "SMMU_CMD_TLBI_EL2_VAA",
  275. [SMMU_CMD_TLBI_S12_VMALL] = "SMMU_CMD_TLBI_S12_VMALL",
  276. [SMMU_CMD_TLBI_S2_IPA] = "SMMU_CMD_TLBI_S2_IPA",
  277. [SMMU_CMD_TLBI_NSNH_ALL] = "SMMU_CMD_TLBI_NSNH_ALL",
  278. [SMMU_CMD_ATC_INV] = "SMMU_CMD_ATC_INV",
  279. [SMMU_CMD_PRI_RESP] = "SMMU_CMD_PRI_RESP",
  280. [SMMU_CMD_RESUME] = "SMMU_CMD_RESUME",
  281. [SMMU_CMD_STALL_TERM] = "SMMU_CMD_STALL_TERM",
  282. [SMMU_CMD_SYNC] = "SMMU_CMD_SYNC",
  283. };
  284. static inline const char *smmu_cmd_string(SMMUCommandType type)
  285. {
  286. if (type > SMMU_CMD_NONE && type < ARRAY_SIZE(cmd_stringify)) {
  287. return cmd_stringify[type] ? cmd_stringify[type] : "UNKNOWN";
  288. } else {
  289. return "INVALID";
  290. }
  291. }
  292. /* CMDQ fields */
  293. typedef enum {
  294. SMMU_CERROR_NONE = 0,
  295. SMMU_CERROR_ILL,
  296. SMMU_CERROR_ABT,
  297. SMMU_CERROR_ATC_INV_SYNC,
  298. } SMMUCmdError;
  299. enum { /* Command completion notification */
  300. CMD_SYNC_SIG_NONE,
  301. CMD_SYNC_SIG_IRQ,
  302. CMD_SYNC_SIG_SEV,
  303. };
  304. #define CMD_TYPE(x) extract32((x)->word[0], 0 , 8)
  305. #define CMD_NUM(x) extract32((x)->word[0], 12 , 5)
  306. #define CMD_SCALE(x) extract32((x)->word[0], 20 , 5)
  307. #define CMD_SSEC(x) extract32((x)->word[0], 10, 1)
  308. #define CMD_SSV(x) extract32((x)->word[0], 11, 1)
  309. #define CMD_RESUME_AC(x) extract32((x)->word[0], 12, 1)
  310. #define CMD_RESUME_AB(x) extract32((x)->word[0], 13, 1)
  311. #define CMD_SYNC_CS(x) extract32((x)->word[0], 12, 2)
  312. #define CMD_SSID(x) extract32((x)->word[0], 12, 20)
  313. #define CMD_SID(x) ((x)->word[1])
  314. #define CMD_VMID(x) extract32((x)->word[1], 0 , 16)
  315. #define CMD_ASID(x) extract32((x)->word[1], 16, 16)
  316. #define CMD_RESUME_STAG(x) extract32((x)->word[2], 0 , 16)
  317. #define CMD_RESP(x) extract32((x)->word[2], 11, 2)
  318. #define CMD_LEAF(x) extract32((x)->word[2], 0 , 1)
  319. #define CMD_TTL(x) extract32((x)->word[2], 8 , 2)
  320. #define CMD_TG(x) extract32((x)->word[2], 10, 2)
  321. #define CMD_STE_RANGE(x) extract32((x)->word[2], 0 , 5)
  322. #define CMD_ADDR(x) \
  323. (((uint64_t)((x)->word[3]) << 32) | \
  324. ((extract64((x)->word[2], 12, 20)) << 12))
  325. #define SMMU_FEATURE_2LVL_STE (1 << 0)
  326. /* Events */
  327. typedef enum SMMUEventType {
  328. SMMU_EVT_NONE = 0x00,
  329. SMMU_EVT_F_UUT ,
  330. SMMU_EVT_C_BAD_STREAMID ,
  331. SMMU_EVT_F_STE_FETCH ,
  332. SMMU_EVT_C_BAD_STE ,
  333. SMMU_EVT_F_BAD_ATS_TREQ ,
  334. SMMU_EVT_F_STREAM_DISABLED ,
  335. SMMU_EVT_F_TRANS_FORBIDDEN ,
  336. SMMU_EVT_C_BAD_SUBSTREAMID ,
  337. SMMU_EVT_F_CD_FETCH ,
  338. SMMU_EVT_C_BAD_CD ,
  339. SMMU_EVT_F_WALK_EABT ,
  340. SMMU_EVT_F_TRANSLATION = 0x10,
  341. SMMU_EVT_F_ADDR_SIZE ,
  342. SMMU_EVT_F_ACCESS ,
  343. SMMU_EVT_F_PERMISSION ,
  344. SMMU_EVT_F_TLB_CONFLICT = 0x20,
  345. SMMU_EVT_F_CFG_CONFLICT ,
  346. SMMU_EVT_E_PAGE_REQ = 0x24,
  347. } SMMUEventType;
  348. static const char *event_stringify[] = {
  349. [SMMU_EVT_NONE] = "no recorded event",
  350. [SMMU_EVT_F_UUT] = "SMMU_EVT_F_UUT",
  351. [SMMU_EVT_C_BAD_STREAMID] = "SMMU_EVT_C_BAD_STREAMID",
  352. [SMMU_EVT_F_STE_FETCH] = "SMMU_EVT_F_STE_FETCH",
  353. [SMMU_EVT_C_BAD_STE] = "SMMU_EVT_C_BAD_STE",
  354. [SMMU_EVT_F_BAD_ATS_TREQ] = "SMMU_EVT_F_BAD_ATS_TREQ",
  355. [SMMU_EVT_F_STREAM_DISABLED] = "SMMU_EVT_F_STREAM_DISABLED",
  356. [SMMU_EVT_F_TRANS_FORBIDDEN] = "SMMU_EVT_F_TRANS_FORBIDDEN",
  357. [SMMU_EVT_C_BAD_SUBSTREAMID] = "SMMU_EVT_C_BAD_SUBSTREAMID",
  358. [SMMU_EVT_F_CD_FETCH] = "SMMU_EVT_F_CD_FETCH",
  359. [SMMU_EVT_C_BAD_CD] = "SMMU_EVT_C_BAD_CD",
  360. [SMMU_EVT_F_WALK_EABT] = "SMMU_EVT_F_WALK_EABT",
  361. [SMMU_EVT_F_TRANSLATION] = "SMMU_EVT_F_TRANSLATION",
  362. [SMMU_EVT_F_ADDR_SIZE] = "SMMU_EVT_F_ADDR_SIZE",
  363. [SMMU_EVT_F_ACCESS] = "SMMU_EVT_F_ACCESS",
  364. [SMMU_EVT_F_PERMISSION] = "SMMU_EVT_F_PERMISSION",
  365. [SMMU_EVT_F_TLB_CONFLICT] = "SMMU_EVT_F_TLB_CONFLICT",
  366. [SMMU_EVT_F_CFG_CONFLICT] = "SMMU_EVT_F_CFG_CONFLICT",
  367. [SMMU_EVT_E_PAGE_REQ] = "SMMU_EVT_E_PAGE_REQ",
  368. };
  369. static inline const char *smmu_event_string(SMMUEventType type)
  370. {
  371. if (type < ARRAY_SIZE(event_stringify)) {
  372. return event_stringify[type] ? event_stringify[type] : "UNKNOWN";
  373. } else {
  374. return "INVALID";
  375. }
  376. }
  377. /* Encode an event record */
  378. typedef struct SMMUEventInfo {
  379. SMMUEventType type;
  380. uint32_t sid;
  381. bool recorded;
  382. bool inval_ste_allowed;
  383. union {
  384. struct {
  385. uint32_t ssid;
  386. bool ssv;
  387. dma_addr_t addr;
  388. bool rnw;
  389. bool pnu;
  390. bool ind;
  391. } f_uut;
  392. struct SSIDInfo {
  393. uint32_t ssid;
  394. bool ssv;
  395. } c_bad_streamid;
  396. struct SSIDAddrInfo {
  397. uint32_t ssid;
  398. bool ssv;
  399. dma_addr_t addr;
  400. } f_ste_fetch;
  401. struct SSIDInfo c_bad_ste;
  402. struct {
  403. dma_addr_t addr;
  404. bool rnw;
  405. } f_transl_forbidden;
  406. struct {
  407. uint32_t ssid;
  408. } c_bad_substream;
  409. struct SSIDAddrInfo f_cd_fetch;
  410. struct SSIDInfo c_bad_cd;
  411. struct FullInfo {
  412. bool stall;
  413. uint16_t stag;
  414. uint32_t ssid;
  415. bool ssv;
  416. bool s2;
  417. dma_addr_t addr;
  418. bool rnw;
  419. bool pnu;
  420. bool ind;
  421. uint8_t class;
  422. dma_addr_t addr2;
  423. } f_walk_eabt;
  424. struct FullInfo f_translation;
  425. struct FullInfo f_addr_size;
  426. struct FullInfo f_access;
  427. struct FullInfo f_permission;
  428. struct SSIDInfo f_cfg_conflict;
  429. /**
  430. * not supported yet:
  431. * F_BAD_ATS_TREQ
  432. * F_BAD_ATS_TREQ
  433. * F_TLB_CONFLICT
  434. * E_PAGE_REQUEST
  435. * IMPDEF_EVENTn
  436. */
  437. } u;
  438. } SMMUEventInfo;
  439. /* EVTQ fields */
  440. #define EVT_Q_OVERFLOW (1 << 31)
  441. #define EVT_SET_TYPE(x, v) ((x)->word[0] = deposit32((x)->word[0], 0 , 8 , v))
  442. #define EVT_SET_SSV(x, v) ((x)->word[0] = deposit32((x)->word[0], 11, 1 , v))
  443. #define EVT_SET_SSID(x, v) ((x)->word[0] = deposit32((x)->word[0], 12, 20, v))
  444. #define EVT_SET_SID(x, v) ((x)->word[1] = v)
  445. #define EVT_SET_STAG(x, v) ((x)->word[2] = deposit32((x)->word[2], 0 , 16, v))
  446. #define EVT_SET_STALL(x, v) ((x)->word[2] = deposit32((x)->word[2], 31, 1 , v))
  447. #define EVT_SET_PNU(x, v) ((x)->word[3] = deposit32((x)->word[3], 1 , 1 , v))
  448. #define EVT_SET_IND(x, v) ((x)->word[3] = deposit32((x)->word[3], 2 , 1 , v))
  449. #define EVT_SET_RNW(x, v) ((x)->word[3] = deposit32((x)->word[3], 3 , 1 , v))
  450. #define EVT_SET_S2(x, v) ((x)->word[3] = deposit32((x)->word[3], 7 , 1 , v))
  451. #define EVT_SET_CLASS(x, v) ((x)->word[3] = deposit32((x)->word[3], 8 , 2 , v))
  452. #define EVT_SET_ADDR(x, addr) \
  453. do { \
  454. (x)->word[5] = (uint32_t)(addr >> 32); \
  455. (x)->word[4] = (uint32_t)(addr & 0xffffffff); \
  456. } while (0)
  457. #define EVT_SET_ADDR2(x, addr) \
  458. do { \
  459. (x)->word[7] = (uint32_t)(addr >> 32); \
  460. (x)->word[6] = (uint32_t)(addr & 0xffffffff); \
  461. } while (0)
  462. void smmuv3_record_event(SMMUv3State *s, SMMUEventInfo *event);
  463. /* Configuration Data */
  464. /* STE Level 1 Descriptor */
  465. typedef struct STEDesc {
  466. uint32_t word[2];
  467. } STEDesc;
  468. /* CD Level 1 Descriptor */
  469. typedef struct CDDesc {
  470. uint32_t word[2];
  471. } CDDesc;
  472. /* Stream Table Entry(STE) */
  473. typedef struct STE {
  474. uint32_t word[16];
  475. } STE;
  476. /* Context Descriptor(CD) */
  477. typedef struct CD {
  478. uint32_t word[16];
  479. } CD;
  480. /* STE fields */
  481. #define STE_VALID(x) extract32((x)->word[0], 0, 1)
  482. #define STE_CONFIG(x) extract32((x)->word[0], 1, 3)
  483. #define STE_CFG_S1_ENABLED(config) (config & 0x1)
  484. #define STE_CFG_S2_ENABLED(config) (config & 0x2)
  485. #define STE_CFG_ABORT(config) (!(config & 0x4))
  486. #define STE_CFG_BYPASS(config) (config == 0x4)
  487. #define STE_S1FMT(x) extract32((x)->word[0], 4 , 2)
  488. #define STE_S1CDMAX(x) extract32((x)->word[1], 27, 5)
  489. #define STE_S1STALLD(x) extract32((x)->word[2], 27, 1)
  490. #define STE_EATS(x) extract32((x)->word[2], 28, 2)
  491. #define STE_STRW(x) extract32((x)->word[2], 30, 2)
  492. #define STE_S2VMID(x) extract32((x)->word[4], 0 , 16)
  493. #define STE_S2T0SZ(x) extract32((x)->word[5], 0 , 6)
  494. #define STE_S2SL0(x) extract32((x)->word[5], 6 , 2)
  495. #define STE_S2TG(x) extract32((x)->word[5], 14, 2)
  496. #define STE_S2PS(x) extract32((x)->word[5], 16, 3)
  497. #define STE_S2AA64(x) extract32((x)->word[5], 19, 1)
  498. #define STE_S2ENDI(x) extract32((x)->word[5], 20, 1)
  499. #define STE_S2AFFD(x) extract32((x)->word[5], 21, 1)
  500. #define STE_S2HD(x) extract32((x)->word[5], 23, 1)
  501. #define STE_S2HA(x) extract32((x)->word[5], 24, 1)
  502. #define STE_S2S(x) extract32((x)->word[5], 25, 1)
  503. #define STE_S2R(x) extract32((x)->word[5], 26, 1)
  504. #define STE_CTXPTR(x) \
  505. ((extract64((x)->word[1], 0, 16) << 32) | \
  506. ((x)->word[0] & 0xffffffc0))
  507. #define STE_S2TTB(x) \
  508. ((extract64((x)->word[7], 0, 16) << 32) | \
  509. ((x)->word[6] & 0xfffffff0))
  510. static inline int oas2bits(int oas_field)
  511. {
  512. switch (oas_field) {
  513. case 0:
  514. return 32;
  515. case 1:
  516. return 36;
  517. case 2:
  518. return 40;
  519. case 3:
  520. return 42;
  521. case 4:
  522. return 44;
  523. case 5:
  524. return 48;
  525. }
  526. g_assert_not_reached();
  527. }
  528. /* CD fields */
  529. #define CD_VALID(x) extract32((x)->word[0], 31, 1)
  530. #define CD_ASID(x) extract32((x)->word[1], 16, 16)
  531. #define CD_TTB(x, sel) \
  532. ((extract64((x)->word[(sel) * 2 + 3], 0, 19) << 32) | \
  533. ((x)->word[(sel) * 2 + 2] & ~0xfULL))
  534. #define CD_HAD(x, sel) extract32((x)->word[(sel) * 2 + 2], 1, 1)
  535. #define CD_TSZ(x, sel) extract32((x)->word[0], (16 * (sel)) + 0, 6)
  536. #define CD_TG(x, sel) extract32((x)->word[0], (16 * (sel)) + 6, 2)
  537. #define CD_EPD(x, sel) extract32((x)->word[0], (16 * (sel)) + 14, 1)
  538. #define CD_ENDI(x) extract32((x)->word[0], 15, 1)
  539. #define CD_IPS(x) extract32((x)->word[1], 0 , 3)
  540. #define CD_AFFD(x) extract32((x)->word[1], 3 , 1)
  541. #define CD_TBI(x) extract32((x)->word[1], 6 , 2)
  542. #define CD_HD(x) extract32((x)->word[1], 10 , 1)
  543. #define CD_HA(x) extract32((x)->word[1], 11 , 1)
  544. #define CD_S(x) extract32((x)->word[1], 12, 1)
  545. #define CD_R(x) extract32((x)->word[1], 13, 1)
  546. #define CD_A(x) extract32((x)->word[1], 14, 1)
  547. #define CD_AARCH64(x) extract32((x)->word[1], 9 , 1)
  548. /**
  549. * tg2granule - Decodes the CD translation granule size field according
  550. * to the ttbr in use
  551. * @bits: TG0/1 fields
  552. * @ttbr: ttbr index in use
  553. */
  554. static inline int tg2granule(int bits, int ttbr)
  555. {
  556. switch (bits) {
  557. case 0:
  558. return ttbr ? 0 : 12;
  559. case 1:
  560. return ttbr ? 14 : 16;
  561. case 2:
  562. return ttbr ? 12 : 14;
  563. case 3:
  564. return ttbr ? 16 : 0;
  565. default:
  566. return 0;
  567. }
  568. }
  569. static inline uint64_t l1std_l2ptr(STEDesc *desc)
  570. {
  571. uint64_t hi, lo;
  572. hi = desc->word[1];
  573. lo = desc->word[0] & ~0x1fULL;
  574. return hi << 32 | lo;
  575. }
  576. #define L1STD_SPAN(stm) (extract32((stm)->word[0], 0, 5))
  577. #endif