2
0

smmuv3-internal.h 20 KB

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