smmuv3.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063
  1. /*
  2. * Copyright (C) 2014-2016 Broadcom Corporation
  3. * Copyright (c) 2017 Red Hat, Inc.
  4. * Written by Prem Mallappa, Eric Auger
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "qemu/osdep.h"
  19. #include "qemu/bitops.h"
  20. #include "hw/irq.h"
  21. #include "hw/sysbus.h"
  22. #include "migration/vmstate.h"
  23. #include "hw/qdev-properties.h"
  24. #include "hw/qdev-core.h"
  25. #include "hw/pci/pci.h"
  26. #include "cpu.h"
  27. #include "trace.h"
  28. #include "qemu/log.h"
  29. #include "qemu/error-report.h"
  30. #include "qapi/error.h"
  31. #include "hw/arm/smmuv3.h"
  32. #include "smmuv3-internal.h"
  33. #include "smmu-internal.h"
  34. #define PTW_RECORD_FAULT(ptw_info, cfg) (((ptw_info).stage == SMMU_STAGE_1 && \
  35. (cfg)->record_faults) || \
  36. ((ptw_info).stage == SMMU_STAGE_2 && \
  37. (cfg)->s2cfg.record_faults))
  38. /**
  39. * smmuv3_trigger_irq - pulse @irq if enabled and update
  40. * GERROR register in case of GERROR interrupt
  41. *
  42. * @irq: irq type
  43. * @gerror_mask: mask of gerrors to toggle (relevant if @irq is GERROR)
  44. */
  45. static void smmuv3_trigger_irq(SMMUv3State *s, SMMUIrq irq,
  46. uint32_t gerror_mask)
  47. {
  48. bool pulse = false;
  49. switch (irq) {
  50. case SMMU_IRQ_EVTQ:
  51. pulse = smmuv3_eventq_irq_enabled(s);
  52. break;
  53. case SMMU_IRQ_PRIQ:
  54. qemu_log_mask(LOG_UNIMP, "PRI not yet supported\n");
  55. break;
  56. case SMMU_IRQ_CMD_SYNC:
  57. pulse = true;
  58. break;
  59. case SMMU_IRQ_GERROR:
  60. {
  61. uint32_t pending = s->gerror ^ s->gerrorn;
  62. uint32_t new_gerrors = ~pending & gerror_mask;
  63. if (!new_gerrors) {
  64. /* only toggle non pending errors */
  65. return;
  66. }
  67. s->gerror ^= new_gerrors;
  68. trace_smmuv3_write_gerror(new_gerrors, s->gerror);
  69. pulse = smmuv3_gerror_irq_enabled(s);
  70. break;
  71. }
  72. }
  73. if (pulse) {
  74. trace_smmuv3_trigger_irq(irq);
  75. qemu_irq_pulse(s->irq[irq]);
  76. }
  77. }
  78. static void smmuv3_write_gerrorn(SMMUv3State *s, uint32_t new_gerrorn)
  79. {
  80. uint32_t pending = s->gerror ^ s->gerrorn;
  81. uint32_t toggled = s->gerrorn ^ new_gerrorn;
  82. if (toggled & ~pending) {
  83. qemu_log_mask(LOG_GUEST_ERROR,
  84. "guest toggles non pending errors = 0x%x\n",
  85. toggled & ~pending);
  86. }
  87. /*
  88. * We do not raise any error in case guest toggles bits corresponding
  89. * to not active IRQs (CONSTRAINED UNPREDICTABLE)
  90. */
  91. s->gerrorn = new_gerrorn;
  92. trace_smmuv3_write_gerrorn(toggled & pending, s->gerrorn);
  93. }
  94. static inline MemTxResult queue_read(SMMUQueue *q, Cmd *cmd)
  95. {
  96. dma_addr_t addr = Q_CONS_ENTRY(q);
  97. MemTxResult ret;
  98. int i;
  99. ret = dma_memory_read(&address_space_memory, addr, cmd, sizeof(Cmd),
  100. MEMTXATTRS_UNSPECIFIED);
  101. if (ret != MEMTX_OK) {
  102. return ret;
  103. }
  104. for (i = 0; i < ARRAY_SIZE(cmd->word); i++) {
  105. le32_to_cpus(&cmd->word[i]);
  106. }
  107. return ret;
  108. }
  109. static MemTxResult queue_write(SMMUQueue *q, Evt *evt_in)
  110. {
  111. dma_addr_t addr = Q_PROD_ENTRY(q);
  112. MemTxResult ret;
  113. Evt evt = *evt_in;
  114. int i;
  115. for (i = 0; i < ARRAY_SIZE(evt.word); i++) {
  116. cpu_to_le32s(&evt.word[i]);
  117. }
  118. ret = dma_memory_write(&address_space_memory, addr, &evt, sizeof(Evt),
  119. MEMTXATTRS_UNSPECIFIED);
  120. if (ret != MEMTX_OK) {
  121. return ret;
  122. }
  123. queue_prod_incr(q);
  124. return MEMTX_OK;
  125. }
  126. static MemTxResult smmuv3_write_eventq(SMMUv3State *s, Evt *evt)
  127. {
  128. SMMUQueue *q = &s->eventq;
  129. MemTxResult r;
  130. if (!smmuv3_eventq_enabled(s)) {
  131. return MEMTX_ERROR;
  132. }
  133. if (smmuv3_q_full(q)) {
  134. return MEMTX_ERROR;
  135. }
  136. r = queue_write(q, evt);
  137. if (r != MEMTX_OK) {
  138. return r;
  139. }
  140. if (!smmuv3_q_empty(q)) {
  141. smmuv3_trigger_irq(s, SMMU_IRQ_EVTQ, 0);
  142. }
  143. return MEMTX_OK;
  144. }
  145. void smmuv3_record_event(SMMUv3State *s, SMMUEventInfo *info)
  146. {
  147. Evt evt = {};
  148. MemTxResult r;
  149. if (!smmuv3_eventq_enabled(s)) {
  150. return;
  151. }
  152. EVT_SET_TYPE(&evt, info->type);
  153. EVT_SET_SID(&evt, info->sid);
  154. switch (info->type) {
  155. case SMMU_EVT_NONE:
  156. return;
  157. case SMMU_EVT_F_UUT:
  158. EVT_SET_SSID(&evt, info->u.f_uut.ssid);
  159. EVT_SET_SSV(&evt, info->u.f_uut.ssv);
  160. EVT_SET_ADDR(&evt, info->u.f_uut.addr);
  161. EVT_SET_RNW(&evt, info->u.f_uut.rnw);
  162. EVT_SET_PNU(&evt, info->u.f_uut.pnu);
  163. EVT_SET_IND(&evt, info->u.f_uut.ind);
  164. break;
  165. case SMMU_EVT_C_BAD_STREAMID:
  166. EVT_SET_SSID(&evt, info->u.c_bad_streamid.ssid);
  167. EVT_SET_SSV(&evt, info->u.c_bad_streamid.ssv);
  168. break;
  169. case SMMU_EVT_F_STE_FETCH:
  170. EVT_SET_SSID(&evt, info->u.f_ste_fetch.ssid);
  171. EVT_SET_SSV(&evt, info->u.f_ste_fetch.ssv);
  172. EVT_SET_ADDR2(&evt, info->u.f_ste_fetch.addr);
  173. break;
  174. case SMMU_EVT_C_BAD_STE:
  175. EVT_SET_SSID(&evt, info->u.c_bad_ste.ssid);
  176. EVT_SET_SSV(&evt, info->u.c_bad_ste.ssv);
  177. break;
  178. case SMMU_EVT_F_STREAM_DISABLED:
  179. break;
  180. case SMMU_EVT_F_TRANS_FORBIDDEN:
  181. EVT_SET_ADDR(&evt, info->u.f_transl_forbidden.addr);
  182. EVT_SET_RNW(&evt, info->u.f_transl_forbidden.rnw);
  183. break;
  184. case SMMU_EVT_C_BAD_SUBSTREAMID:
  185. EVT_SET_SSID(&evt, info->u.c_bad_substream.ssid);
  186. break;
  187. case SMMU_EVT_F_CD_FETCH:
  188. EVT_SET_SSID(&evt, info->u.f_cd_fetch.ssid);
  189. EVT_SET_SSV(&evt, info->u.f_cd_fetch.ssv);
  190. EVT_SET_ADDR(&evt, info->u.f_cd_fetch.addr);
  191. break;
  192. case SMMU_EVT_C_BAD_CD:
  193. EVT_SET_SSID(&evt, info->u.c_bad_cd.ssid);
  194. EVT_SET_SSV(&evt, info->u.c_bad_cd.ssv);
  195. break;
  196. case SMMU_EVT_F_WALK_EABT:
  197. case SMMU_EVT_F_TRANSLATION:
  198. case SMMU_EVT_F_ADDR_SIZE:
  199. case SMMU_EVT_F_ACCESS:
  200. case SMMU_EVT_F_PERMISSION:
  201. EVT_SET_STALL(&evt, info->u.f_walk_eabt.stall);
  202. EVT_SET_STAG(&evt, info->u.f_walk_eabt.stag);
  203. EVT_SET_SSID(&evt, info->u.f_walk_eabt.ssid);
  204. EVT_SET_SSV(&evt, info->u.f_walk_eabt.ssv);
  205. EVT_SET_S2(&evt, info->u.f_walk_eabt.s2);
  206. EVT_SET_ADDR(&evt, info->u.f_walk_eabt.addr);
  207. EVT_SET_RNW(&evt, info->u.f_walk_eabt.rnw);
  208. EVT_SET_PNU(&evt, info->u.f_walk_eabt.pnu);
  209. EVT_SET_IND(&evt, info->u.f_walk_eabt.ind);
  210. EVT_SET_CLASS(&evt, info->u.f_walk_eabt.class);
  211. EVT_SET_ADDR2(&evt, info->u.f_walk_eabt.addr2);
  212. break;
  213. case SMMU_EVT_F_CFG_CONFLICT:
  214. EVT_SET_SSID(&evt, info->u.f_cfg_conflict.ssid);
  215. EVT_SET_SSV(&evt, info->u.f_cfg_conflict.ssv);
  216. break;
  217. /* rest is not implemented */
  218. case SMMU_EVT_F_BAD_ATS_TREQ:
  219. case SMMU_EVT_F_TLB_CONFLICT:
  220. case SMMU_EVT_E_PAGE_REQ:
  221. default:
  222. g_assert_not_reached();
  223. }
  224. trace_smmuv3_record_event(smmu_event_string(info->type), info->sid);
  225. r = smmuv3_write_eventq(s, &evt);
  226. if (r != MEMTX_OK) {
  227. smmuv3_trigger_irq(s, SMMU_IRQ_GERROR, R_GERROR_EVENTQ_ABT_ERR_MASK);
  228. }
  229. info->recorded = true;
  230. }
  231. static void smmuv3_init_regs(SMMUv3State *s)
  232. {
  233. /* Based on sys property, the stages supported in smmu will be advertised.*/
  234. if (s->stage && !strcmp("2", s->stage)) {
  235. s->idr[0] = FIELD_DP32(s->idr[0], IDR0, S2P, 1);
  236. } else if (s->stage && !strcmp("nested", s->stage)) {
  237. s->idr[0] = FIELD_DP32(s->idr[0], IDR0, S1P, 1);
  238. s->idr[0] = FIELD_DP32(s->idr[0], IDR0, S2P, 1);
  239. } else {
  240. s->idr[0] = FIELD_DP32(s->idr[0], IDR0, S1P, 1);
  241. }
  242. s->idr[0] = FIELD_DP32(s->idr[0], IDR0, TTF, 2); /* AArch64 PTW only */
  243. s->idr[0] = FIELD_DP32(s->idr[0], IDR0, COHACC, 1); /* IO coherent */
  244. s->idr[0] = FIELD_DP32(s->idr[0], IDR0, ASID16, 1); /* 16-bit ASID */
  245. s->idr[0] = FIELD_DP32(s->idr[0], IDR0, VMID16, 1); /* 16-bit VMID */
  246. s->idr[0] = FIELD_DP32(s->idr[0], IDR0, TTENDIAN, 2); /* little endian */
  247. s->idr[0] = FIELD_DP32(s->idr[0], IDR0, STALL_MODEL, 1); /* No stall */
  248. /* terminated transaction will always be aborted/error returned */
  249. s->idr[0] = FIELD_DP32(s->idr[0], IDR0, TERM_MODEL, 1);
  250. /* 2-level stream table supported */
  251. s->idr[0] = FIELD_DP32(s->idr[0], IDR0, STLEVEL, 1);
  252. s->idr[1] = FIELD_DP32(s->idr[1], IDR1, SIDSIZE, SMMU_IDR1_SIDSIZE);
  253. s->idr[1] = FIELD_DP32(s->idr[1], IDR1, EVENTQS, SMMU_EVENTQS);
  254. s->idr[1] = FIELD_DP32(s->idr[1], IDR1, CMDQS, SMMU_CMDQS);
  255. s->idr[3] = FIELD_DP32(s->idr[3], IDR3, HAD, 1);
  256. if (FIELD_EX32(s->idr[0], IDR0, S2P)) {
  257. /* XNX is a stage-2-specific feature */
  258. s->idr[3] = FIELD_DP32(s->idr[3], IDR3, XNX, 1);
  259. }
  260. s->idr[3] = FIELD_DP32(s->idr[3], IDR3, RIL, 1);
  261. s->idr[3] = FIELD_DP32(s->idr[3], IDR3, BBML, 2);
  262. s->idr[5] = FIELD_DP32(s->idr[5], IDR5, OAS, SMMU_IDR5_OAS); /* 44 bits */
  263. /* 4K, 16K and 64K granule support */
  264. s->idr[5] = FIELD_DP32(s->idr[5], IDR5, GRAN4K, 1);
  265. s->idr[5] = FIELD_DP32(s->idr[5], IDR5, GRAN16K, 1);
  266. s->idr[5] = FIELD_DP32(s->idr[5], IDR5, GRAN64K, 1);
  267. s->cmdq.base = deposit64(s->cmdq.base, 0, 5, SMMU_CMDQS);
  268. s->cmdq.prod = 0;
  269. s->cmdq.cons = 0;
  270. s->cmdq.entry_size = sizeof(struct Cmd);
  271. s->eventq.base = deposit64(s->eventq.base, 0, 5, SMMU_EVENTQS);
  272. s->eventq.prod = 0;
  273. s->eventq.cons = 0;
  274. s->eventq.entry_size = sizeof(struct Evt);
  275. s->features = 0;
  276. s->sid_split = 0;
  277. s->aidr = 0x1;
  278. s->cr[0] = 0;
  279. s->cr0ack = 0;
  280. s->irq_ctrl = 0;
  281. s->gerror = 0;
  282. s->gerrorn = 0;
  283. s->statusr = 0;
  284. s->gbpa = SMMU_GBPA_RESET_VAL;
  285. }
  286. static int smmu_get_ste(SMMUv3State *s, dma_addr_t addr, STE *buf,
  287. SMMUEventInfo *event)
  288. {
  289. int ret, i;
  290. trace_smmuv3_get_ste(addr);
  291. /* TODO: guarantee 64-bit single-copy atomicity */
  292. ret = dma_memory_read(&address_space_memory, addr, buf, sizeof(*buf),
  293. MEMTXATTRS_UNSPECIFIED);
  294. if (ret != MEMTX_OK) {
  295. qemu_log_mask(LOG_GUEST_ERROR,
  296. "Cannot fetch pte at address=0x%"PRIx64"\n", addr);
  297. event->type = SMMU_EVT_F_STE_FETCH;
  298. event->u.f_ste_fetch.addr = addr;
  299. return -EINVAL;
  300. }
  301. for (i = 0; i < ARRAY_SIZE(buf->word); i++) {
  302. le32_to_cpus(&buf->word[i]);
  303. }
  304. return 0;
  305. }
  306. static SMMUTranslationStatus smmuv3_do_translate(SMMUv3State *s, hwaddr addr,
  307. SMMUTransCfg *cfg,
  308. SMMUEventInfo *event,
  309. IOMMUAccessFlags flag,
  310. SMMUTLBEntry **out_entry,
  311. SMMUTranslationClass class);
  312. /* @ssid > 0 not supported yet */
  313. static int smmu_get_cd(SMMUv3State *s, STE *ste, SMMUTransCfg *cfg,
  314. uint32_t ssid, CD *buf, SMMUEventInfo *event)
  315. {
  316. dma_addr_t addr = STE_CTXPTR(ste);
  317. int ret, i;
  318. SMMUTranslationStatus status;
  319. SMMUTLBEntry *entry;
  320. trace_smmuv3_get_cd(addr);
  321. if (cfg->stage == SMMU_NESTED) {
  322. status = smmuv3_do_translate(s, addr, cfg, event,
  323. IOMMU_RO, &entry, SMMU_CLASS_CD);
  324. /* Same PTW faults are reported but with CLASS = CD. */
  325. if (status != SMMU_TRANS_SUCCESS) {
  326. return -EINVAL;
  327. }
  328. addr = CACHED_ENTRY_TO_ADDR(entry, addr);
  329. }
  330. /* TODO: guarantee 64-bit single-copy atomicity */
  331. ret = dma_memory_read(&address_space_memory, addr, buf, sizeof(*buf),
  332. MEMTXATTRS_UNSPECIFIED);
  333. if (ret != MEMTX_OK) {
  334. qemu_log_mask(LOG_GUEST_ERROR,
  335. "Cannot fetch pte at address=0x%"PRIx64"\n", addr);
  336. event->type = SMMU_EVT_F_CD_FETCH;
  337. event->u.f_cd_fetch.addr = addr;
  338. return -EINVAL;
  339. }
  340. for (i = 0; i < ARRAY_SIZE(buf->word); i++) {
  341. le32_to_cpus(&buf->word[i]);
  342. }
  343. return 0;
  344. }
  345. /*
  346. * Max valid value is 39 when SMMU_IDR3.STT == 0.
  347. * In architectures after SMMUv3.0:
  348. * - If STE.S2TG selects a 4KB or 16KB granule, the minimum valid value for this
  349. * field is MAX(16, 64-IAS)
  350. * - If STE.S2TG selects a 64KB granule, the minimum valid value for this field
  351. * is (64-IAS).
  352. * As we only support AA64, IAS = OAS.
  353. */
  354. static bool s2t0sz_valid(SMMUTransCfg *cfg)
  355. {
  356. if (cfg->s2cfg.tsz > 39) {
  357. return false;
  358. }
  359. if (cfg->s2cfg.granule_sz == 16) {
  360. return (cfg->s2cfg.tsz >= 64 - cfg->s2cfg.eff_ps);
  361. }
  362. return (cfg->s2cfg.tsz >= MAX(64 - cfg->s2cfg.eff_ps, 16));
  363. }
  364. /*
  365. * Return true if s2 page table config is valid.
  366. * This checks with the configured start level, ias_bits and granularity we can
  367. * have a valid page table as described in ARM ARM D8.2 Translation process.
  368. * The idea here is to see for the highest possible number of IPA bits, how
  369. * many concatenated tables we would need, if it is more than 16, then this is
  370. * not possible.
  371. */
  372. static bool s2_pgtable_config_valid(uint8_t sl0, uint8_t t0sz, uint8_t gran)
  373. {
  374. int level = get_start_level(sl0, gran);
  375. uint64_t ipa_bits = 64 - t0sz;
  376. uint64_t max_ipa = (1ULL << ipa_bits) - 1;
  377. int nr_concat = pgd_concat_idx(level, gran, max_ipa) + 1;
  378. return nr_concat <= VMSA_MAX_S2_CONCAT;
  379. }
  380. static int decode_ste_s2_cfg(SMMUv3State *s, SMMUTransCfg *cfg,
  381. STE *ste)
  382. {
  383. uint8_t oas = FIELD_EX32(s->idr[5], IDR5, OAS);
  384. if (STE_S2AA64(ste) == 0x0) {
  385. qemu_log_mask(LOG_UNIMP,
  386. "SMMUv3 AArch32 tables not supported\n");
  387. g_assert_not_reached();
  388. }
  389. switch (STE_S2TG(ste)) {
  390. case 0x0: /* 4KB */
  391. cfg->s2cfg.granule_sz = 12;
  392. break;
  393. case 0x1: /* 64KB */
  394. cfg->s2cfg.granule_sz = 16;
  395. break;
  396. case 0x2: /* 16KB */
  397. cfg->s2cfg.granule_sz = 14;
  398. break;
  399. default:
  400. qemu_log_mask(LOG_GUEST_ERROR,
  401. "SMMUv3 bad STE S2TG: %x\n", STE_S2TG(ste));
  402. goto bad_ste;
  403. }
  404. cfg->s2cfg.vttb = STE_S2TTB(ste);
  405. cfg->s2cfg.sl0 = STE_S2SL0(ste);
  406. /* FEAT_TTST not supported. */
  407. if (cfg->s2cfg.sl0 == 0x3) {
  408. qemu_log_mask(LOG_UNIMP, "SMMUv3 S2SL0 = 0x3 has no meaning!\n");
  409. goto bad_ste;
  410. }
  411. /* For AA64, The effective S2PS size is capped to the OAS. */
  412. cfg->s2cfg.eff_ps = oas2bits(MIN(STE_S2PS(ste), oas));
  413. /*
  414. * For SMMUv3.1 and later, when OAS == IAS == 52, the stage 2 input
  415. * range is further limited to 48 bits unless STE.S2TG indicates a
  416. * 64KB granule.
  417. */
  418. if (cfg->s2cfg.granule_sz != 16) {
  419. cfg->s2cfg.eff_ps = MIN(cfg->s2cfg.eff_ps, 48);
  420. }
  421. /*
  422. * It is ILLEGAL for the address in S2TTB to be outside the range
  423. * described by the effective S2PS value.
  424. */
  425. if (cfg->s2cfg.vttb & ~(MAKE_64BIT_MASK(0, cfg->s2cfg.eff_ps))) {
  426. qemu_log_mask(LOG_GUEST_ERROR,
  427. "SMMUv3 S2TTB too large 0x%" PRIx64
  428. ", effective PS %d bits\n",
  429. cfg->s2cfg.vttb, cfg->s2cfg.eff_ps);
  430. goto bad_ste;
  431. }
  432. cfg->s2cfg.tsz = STE_S2T0SZ(ste);
  433. if (!s2t0sz_valid(cfg)) {
  434. qemu_log_mask(LOG_GUEST_ERROR, "SMMUv3 bad STE S2T0SZ = %d\n",
  435. cfg->s2cfg.tsz);
  436. goto bad_ste;
  437. }
  438. if (!s2_pgtable_config_valid(cfg->s2cfg.sl0, cfg->s2cfg.tsz,
  439. cfg->s2cfg.granule_sz)) {
  440. qemu_log_mask(LOG_GUEST_ERROR,
  441. "SMMUv3 STE stage 2 config not valid!\n");
  442. goto bad_ste;
  443. }
  444. /* Only LE supported(IDR0.TTENDIAN). */
  445. if (STE_S2ENDI(ste)) {
  446. qemu_log_mask(LOG_GUEST_ERROR,
  447. "SMMUv3 STE_S2ENDI only supports LE!\n");
  448. goto bad_ste;
  449. }
  450. cfg->s2cfg.affd = STE_S2AFFD(ste);
  451. cfg->s2cfg.record_faults = STE_S2R(ste);
  452. /* As stall is not supported. */
  453. if (STE_S2S(ste)) {
  454. qemu_log_mask(LOG_UNIMP, "SMMUv3 Stall not implemented!\n");
  455. goto bad_ste;
  456. }
  457. return 0;
  458. bad_ste:
  459. return -EINVAL;
  460. }
  461. static void decode_ste_config(SMMUTransCfg *cfg, uint32_t config)
  462. {
  463. if (STE_CFG_ABORT(config)) {
  464. cfg->aborted = true;
  465. return;
  466. }
  467. if (STE_CFG_BYPASS(config)) {
  468. cfg->bypassed = true;
  469. return;
  470. }
  471. if (STE_CFG_S1_ENABLED(config)) {
  472. cfg->stage = SMMU_STAGE_1;
  473. }
  474. if (STE_CFG_S2_ENABLED(config)) {
  475. cfg->stage |= SMMU_STAGE_2;
  476. }
  477. }
  478. /* Returns < 0 in case of invalid STE, 0 otherwise */
  479. static int decode_ste(SMMUv3State *s, SMMUTransCfg *cfg,
  480. STE *ste, SMMUEventInfo *event)
  481. {
  482. uint32_t config;
  483. uint8_t oas = FIELD_EX32(s->idr[5], IDR5, OAS);
  484. int ret;
  485. if (!STE_VALID(ste)) {
  486. if (!event->inval_ste_allowed) {
  487. qemu_log_mask(LOG_GUEST_ERROR, "invalid STE\n");
  488. }
  489. goto bad_ste;
  490. }
  491. config = STE_CONFIG(ste);
  492. decode_ste_config(cfg, config);
  493. if (cfg->aborted || cfg->bypassed) {
  494. return 0;
  495. }
  496. /*
  497. * If a stage is enabled in SW while not advertised, throw bad ste
  498. * according to user manual(IHI0070E) "5.2 Stream Table Entry".
  499. */
  500. if (!STAGE1_SUPPORTED(s) && STE_CFG_S1_ENABLED(config)) {
  501. qemu_log_mask(LOG_GUEST_ERROR, "SMMUv3 S1 used but not supported.\n");
  502. goto bad_ste;
  503. }
  504. if (!STAGE2_SUPPORTED(s) && STE_CFG_S2_ENABLED(config)) {
  505. qemu_log_mask(LOG_GUEST_ERROR, "SMMUv3 S2 used but not supported.\n");
  506. goto bad_ste;
  507. }
  508. if (STAGE2_SUPPORTED(s)) {
  509. /* VMID is considered even if s2 is disabled. */
  510. cfg->s2cfg.vmid = STE_S2VMID(ste);
  511. } else {
  512. /* Default to -1 */
  513. cfg->s2cfg.vmid = -1;
  514. }
  515. if (STE_CFG_S2_ENABLED(config)) {
  516. /*
  517. * Stage-1 OAS defaults to OAS even if not enabled as it would be used
  518. * in input address check for stage-2.
  519. */
  520. cfg->oas = oas2bits(oas);
  521. ret = decode_ste_s2_cfg(s, cfg, ste);
  522. if (ret) {
  523. goto bad_ste;
  524. }
  525. }
  526. if (STE_S1CDMAX(ste) != 0) {
  527. qemu_log_mask(LOG_UNIMP,
  528. "SMMUv3 does not support multiple context descriptors yet\n");
  529. goto bad_ste;
  530. }
  531. if (STE_S1STALLD(ste)) {
  532. qemu_log_mask(LOG_UNIMP,
  533. "SMMUv3 S1 stalling fault model not allowed yet\n");
  534. goto bad_ste;
  535. }
  536. return 0;
  537. bad_ste:
  538. event->type = SMMU_EVT_C_BAD_STE;
  539. return -EINVAL;
  540. }
  541. /**
  542. * smmu_find_ste - Return the stream table entry associated
  543. * to the sid
  544. *
  545. * @s: smmuv3 handle
  546. * @sid: stream ID
  547. * @ste: returned stream table entry
  548. * @event: handle to an event info
  549. *
  550. * Supports linear and 2-level stream table
  551. * Return 0 on success, -EINVAL otherwise
  552. */
  553. static int smmu_find_ste(SMMUv3State *s, uint32_t sid, STE *ste,
  554. SMMUEventInfo *event)
  555. {
  556. dma_addr_t addr, strtab_base;
  557. uint32_t log2size;
  558. int strtab_size_shift;
  559. int ret;
  560. trace_smmuv3_find_ste(sid, s->features, s->sid_split);
  561. log2size = FIELD_EX32(s->strtab_base_cfg, STRTAB_BASE_CFG, LOG2SIZE);
  562. /*
  563. * Check SID range against both guest-configured and implementation limits
  564. */
  565. if (sid >= (1 << MIN(log2size, SMMU_IDR1_SIDSIZE))) {
  566. event->type = SMMU_EVT_C_BAD_STREAMID;
  567. return -EINVAL;
  568. }
  569. if (s->features & SMMU_FEATURE_2LVL_STE) {
  570. int l1_ste_offset, l2_ste_offset, max_l2_ste, span, i;
  571. dma_addr_t l1ptr, l2ptr;
  572. STEDesc l1std;
  573. /*
  574. * Align strtab base address to table size. For this purpose, assume it
  575. * is not bounded by SMMU_IDR1_SIDSIZE.
  576. */
  577. strtab_size_shift = MAX(5, (int)log2size - s->sid_split - 1 + 3);
  578. strtab_base = s->strtab_base & SMMU_BASE_ADDR_MASK &
  579. ~MAKE_64BIT_MASK(0, strtab_size_shift);
  580. l1_ste_offset = sid >> s->sid_split;
  581. l2_ste_offset = sid & ((1 << s->sid_split) - 1);
  582. l1ptr = (dma_addr_t)(strtab_base + l1_ste_offset * sizeof(l1std));
  583. /* TODO: guarantee 64-bit single-copy atomicity */
  584. ret = dma_memory_read(&address_space_memory, l1ptr, &l1std,
  585. sizeof(l1std), MEMTXATTRS_UNSPECIFIED);
  586. if (ret != MEMTX_OK) {
  587. qemu_log_mask(LOG_GUEST_ERROR,
  588. "Could not read L1PTR at 0X%"PRIx64"\n", l1ptr);
  589. event->type = SMMU_EVT_F_STE_FETCH;
  590. event->u.f_ste_fetch.addr = l1ptr;
  591. return -EINVAL;
  592. }
  593. for (i = 0; i < ARRAY_SIZE(l1std.word); i++) {
  594. le32_to_cpus(&l1std.word[i]);
  595. }
  596. span = L1STD_SPAN(&l1std);
  597. if (!span) {
  598. /* l2ptr is not valid */
  599. if (!event->inval_ste_allowed) {
  600. qemu_log_mask(LOG_GUEST_ERROR,
  601. "invalid sid=%d (L1STD span=0)\n", sid);
  602. }
  603. event->type = SMMU_EVT_C_BAD_STREAMID;
  604. return -EINVAL;
  605. }
  606. max_l2_ste = (1 << span) - 1;
  607. l2ptr = l1std_l2ptr(&l1std);
  608. trace_smmuv3_find_ste_2lvl(s->strtab_base, l1ptr, l1_ste_offset,
  609. l2ptr, l2_ste_offset, max_l2_ste);
  610. if (l2_ste_offset > max_l2_ste) {
  611. qemu_log_mask(LOG_GUEST_ERROR,
  612. "l2_ste_offset=%d > max_l2_ste=%d\n",
  613. l2_ste_offset, max_l2_ste);
  614. event->type = SMMU_EVT_C_BAD_STE;
  615. return -EINVAL;
  616. }
  617. addr = l2ptr + l2_ste_offset * sizeof(*ste);
  618. } else {
  619. strtab_size_shift = log2size + 5;
  620. strtab_base = s->strtab_base & SMMU_BASE_ADDR_MASK &
  621. ~MAKE_64BIT_MASK(0, strtab_size_shift);
  622. addr = strtab_base + sid * sizeof(*ste);
  623. }
  624. if (smmu_get_ste(s, addr, ste, event)) {
  625. return -EINVAL;
  626. }
  627. return 0;
  628. }
  629. static int decode_cd(SMMUv3State *s, SMMUTransCfg *cfg,
  630. CD *cd, SMMUEventInfo *event)
  631. {
  632. int ret = -EINVAL;
  633. int i;
  634. SMMUTranslationStatus status;
  635. SMMUTLBEntry *entry;
  636. uint8_t oas = FIELD_EX32(s->idr[5], IDR5, OAS);
  637. if (!CD_VALID(cd) || !CD_AARCH64(cd)) {
  638. goto bad_cd;
  639. }
  640. if (!CD_A(cd)) {
  641. goto bad_cd; /* SMMU_IDR0.TERM_MODEL == 1 */
  642. }
  643. if (CD_S(cd)) {
  644. goto bad_cd; /* !STE_SECURE && SMMU_IDR0.STALL_MODEL == 1 */
  645. }
  646. if (CD_HA(cd) || CD_HD(cd)) {
  647. goto bad_cd; /* HTTU = 0 */
  648. }
  649. /* we support only those at the moment */
  650. cfg->aa64 = true;
  651. cfg->oas = oas2bits(CD_IPS(cd));
  652. cfg->oas = MIN(oas2bits(oas), cfg->oas);
  653. cfg->tbi = CD_TBI(cd);
  654. cfg->asid = CD_ASID(cd);
  655. cfg->affd = CD_AFFD(cd);
  656. trace_smmuv3_decode_cd(cfg->oas);
  657. /* decode data dependent on TT */
  658. for (i = 0; i <= 1; i++) {
  659. int tg, tsz;
  660. SMMUTransTableInfo *tt = &cfg->tt[i];
  661. cfg->tt[i].disabled = CD_EPD(cd, i);
  662. if (cfg->tt[i].disabled) {
  663. continue;
  664. }
  665. tsz = CD_TSZ(cd, i);
  666. if (tsz < 16 || tsz > 39) {
  667. goto bad_cd;
  668. }
  669. tg = CD_TG(cd, i);
  670. tt->granule_sz = tg2granule(tg, i);
  671. if ((tt->granule_sz != 12 && tt->granule_sz != 14 &&
  672. tt->granule_sz != 16) || CD_ENDI(cd)) {
  673. goto bad_cd;
  674. }
  675. /*
  676. * An address greater than 48 bits in size can only be output from a
  677. * TTD when, in SMMUv3.1 and later, the effective IPS is 52 and a 64KB
  678. * granule is in use for that translation table
  679. */
  680. if (tt->granule_sz != 16) {
  681. cfg->oas = MIN(cfg->oas, 48);
  682. }
  683. tt->tsz = tsz;
  684. tt->ttb = CD_TTB(cd, i);
  685. if (tt->ttb & ~(MAKE_64BIT_MASK(0, cfg->oas))) {
  686. goto bad_cd;
  687. }
  688. /* Translate the TTBx, from IPA to PA if nesting is enabled. */
  689. if (cfg->stage == SMMU_NESTED) {
  690. status = smmuv3_do_translate(s, tt->ttb, cfg, event, IOMMU_RO,
  691. &entry, SMMU_CLASS_TT);
  692. /*
  693. * Same PTW faults are reported but with CLASS = TT.
  694. * If TTBx is larger than the effective stage 1 output addres
  695. * size, it reports C_BAD_CD, which is handled by the above case.
  696. */
  697. if (status != SMMU_TRANS_SUCCESS) {
  698. return -EINVAL;
  699. }
  700. tt->ttb = CACHED_ENTRY_TO_ADDR(entry, tt->ttb);
  701. }
  702. tt->had = CD_HAD(cd, i);
  703. trace_smmuv3_decode_cd_tt(i, tt->tsz, tt->ttb, tt->granule_sz, tt->had);
  704. }
  705. cfg->record_faults = CD_R(cd);
  706. return 0;
  707. bad_cd:
  708. event->type = SMMU_EVT_C_BAD_CD;
  709. return ret;
  710. }
  711. /**
  712. * smmuv3_decode_config - Prepare the translation configuration
  713. * for the @mr iommu region
  714. * @mr: iommu memory region the translation config must be prepared for
  715. * @cfg: output translation configuration which is populated through
  716. * the different configuration decoding steps
  717. * @event: must be zero'ed by the caller
  718. *
  719. * return < 0 in case of config decoding error (@event is filled
  720. * accordingly). Return 0 otherwise.
  721. */
  722. static int smmuv3_decode_config(IOMMUMemoryRegion *mr, SMMUTransCfg *cfg,
  723. SMMUEventInfo *event)
  724. {
  725. SMMUDevice *sdev = container_of(mr, SMMUDevice, iommu);
  726. uint32_t sid = smmu_get_sid(sdev);
  727. SMMUv3State *s = sdev->smmu;
  728. int ret;
  729. STE ste;
  730. CD cd;
  731. /* ASID defaults to -1 (if s1 is not supported). */
  732. cfg->asid = -1;
  733. ret = smmu_find_ste(s, sid, &ste, event);
  734. if (ret) {
  735. return ret;
  736. }
  737. ret = decode_ste(s, cfg, &ste, event);
  738. if (ret) {
  739. return ret;
  740. }
  741. if (cfg->aborted || cfg->bypassed || (cfg->stage == SMMU_STAGE_2)) {
  742. return 0;
  743. }
  744. ret = smmu_get_cd(s, &ste, cfg, 0 /* ssid */, &cd, event);
  745. if (ret) {
  746. return ret;
  747. }
  748. return decode_cd(s, cfg, &cd, event);
  749. }
  750. /**
  751. * smmuv3_get_config - Look up for a cached copy of configuration data for
  752. * @sdev and on cache miss performs a configuration structure decoding from
  753. * guest RAM.
  754. *
  755. * @sdev: SMMUDevice handle
  756. * @event: output event info
  757. *
  758. * The configuration cache contains data resulting from both STE and CD
  759. * decoding under the form of an SMMUTransCfg struct. The hash table is indexed
  760. * by the SMMUDevice handle.
  761. */
  762. static SMMUTransCfg *smmuv3_get_config(SMMUDevice *sdev, SMMUEventInfo *event)
  763. {
  764. SMMUv3State *s = sdev->smmu;
  765. SMMUState *bc = &s->smmu_state;
  766. SMMUTransCfg *cfg;
  767. cfg = g_hash_table_lookup(bc->configs, sdev);
  768. if (cfg) {
  769. sdev->cfg_cache_hits++;
  770. trace_smmuv3_config_cache_hit(smmu_get_sid(sdev),
  771. sdev->cfg_cache_hits, sdev->cfg_cache_misses,
  772. 100 * sdev->cfg_cache_hits /
  773. (sdev->cfg_cache_hits + sdev->cfg_cache_misses));
  774. } else {
  775. sdev->cfg_cache_misses++;
  776. trace_smmuv3_config_cache_miss(smmu_get_sid(sdev),
  777. sdev->cfg_cache_hits, sdev->cfg_cache_misses,
  778. 100 * sdev->cfg_cache_hits /
  779. (sdev->cfg_cache_hits + sdev->cfg_cache_misses));
  780. cfg = g_new0(SMMUTransCfg, 1);
  781. if (!smmuv3_decode_config(&sdev->iommu, cfg, event)) {
  782. g_hash_table_insert(bc->configs, sdev, cfg);
  783. } else {
  784. g_free(cfg);
  785. cfg = NULL;
  786. }
  787. }
  788. return cfg;
  789. }
  790. static void smmuv3_flush_config(SMMUDevice *sdev)
  791. {
  792. SMMUv3State *s = sdev->smmu;
  793. SMMUState *bc = &s->smmu_state;
  794. trace_smmu_config_cache_inv(smmu_get_sid(sdev));
  795. g_hash_table_remove(bc->configs, sdev);
  796. }
  797. /* Do translation with TLB lookup. */
  798. static SMMUTranslationStatus smmuv3_do_translate(SMMUv3State *s, hwaddr addr,
  799. SMMUTransCfg *cfg,
  800. SMMUEventInfo *event,
  801. IOMMUAccessFlags flag,
  802. SMMUTLBEntry **out_entry,
  803. SMMUTranslationClass class)
  804. {
  805. SMMUPTWEventInfo ptw_info = {};
  806. SMMUState *bs = ARM_SMMU(s);
  807. SMMUTLBEntry *cached_entry = NULL;
  808. int asid, stage;
  809. bool desc_s2_translation = class != SMMU_CLASS_IN;
  810. /*
  811. * The function uses the argument class to identify which stage is used:
  812. * - CLASS = IN: Means an input translation, determine the stage from STE.
  813. * - CLASS = CD: Means the addr is an IPA of the CD, and it would be
  814. * translated using the stage-2.
  815. * - CLASS = TT: Means the addr is an IPA of the stage-1 translation table
  816. * and it would be translated using the stage-2.
  817. * For the last 2 cases instead of having intrusive changes in the common
  818. * logic, we modify the cfg to be a stage-2 translation only in case of
  819. * nested, and then restore it after.
  820. */
  821. if (desc_s2_translation) {
  822. asid = cfg->asid;
  823. stage = cfg->stage;
  824. cfg->asid = -1;
  825. cfg->stage = SMMU_STAGE_2;
  826. }
  827. cached_entry = smmu_translate(bs, cfg, addr, flag, &ptw_info);
  828. if (desc_s2_translation) {
  829. cfg->asid = asid;
  830. cfg->stage = stage;
  831. }
  832. if (!cached_entry) {
  833. /* All faults from PTW has S2 field. */
  834. event->u.f_walk_eabt.s2 = (ptw_info.stage == SMMU_STAGE_2);
  835. /*
  836. * Fault class is set as follows based on "class" input to
  837. * the function and to "ptw_info" from "smmu_translate()"
  838. * For stage-1:
  839. * - EABT => CLASS_TT (hardcoded)
  840. * - other events => CLASS_IN (input to function)
  841. * For stage-2 => CLASS_IN (input to function)
  842. * For nested, for all events:
  843. * - CD fetch => CLASS_CD (input to function)
  844. * - walking stage 1 translation table => CLASS_TT (from
  845. * is_ipa_descriptor or input in case of TTBx)
  846. * - s2 translation => CLASS_IN (input to function)
  847. */
  848. class = ptw_info.is_ipa_descriptor ? SMMU_CLASS_TT : class;
  849. switch (ptw_info.type) {
  850. case SMMU_PTW_ERR_WALK_EABT:
  851. event->type = SMMU_EVT_F_WALK_EABT;
  852. event->u.f_walk_eabt.rnw = flag & 0x1;
  853. event->u.f_walk_eabt.class = (ptw_info.stage == SMMU_STAGE_2) ?
  854. class : SMMU_CLASS_TT;
  855. event->u.f_walk_eabt.addr2 = ptw_info.addr;
  856. break;
  857. case SMMU_PTW_ERR_TRANSLATION:
  858. if (PTW_RECORD_FAULT(ptw_info, cfg)) {
  859. event->type = SMMU_EVT_F_TRANSLATION;
  860. event->u.f_translation.addr2 = ptw_info.addr;
  861. event->u.f_translation.class = class;
  862. event->u.f_translation.rnw = flag & 0x1;
  863. }
  864. break;
  865. case SMMU_PTW_ERR_ADDR_SIZE:
  866. if (PTW_RECORD_FAULT(ptw_info, cfg)) {
  867. event->type = SMMU_EVT_F_ADDR_SIZE;
  868. event->u.f_addr_size.addr2 = ptw_info.addr;
  869. event->u.f_addr_size.class = class;
  870. event->u.f_addr_size.rnw = flag & 0x1;
  871. }
  872. break;
  873. case SMMU_PTW_ERR_ACCESS:
  874. if (PTW_RECORD_FAULT(ptw_info, cfg)) {
  875. event->type = SMMU_EVT_F_ACCESS;
  876. event->u.f_access.addr2 = ptw_info.addr;
  877. event->u.f_access.class = class;
  878. event->u.f_access.rnw = flag & 0x1;
  879. }
  880. break;
  881. case SMMU_PTW_ERR_PERMISSION:
  882. if (PTW_RECORD_FAULT(ptw_info, cfg)) {
  883. event->type = SMMU_EVT_F_PERMISSION;
  884. event->u.f_permission.addr2 = ptw_info.addr;
  885. event->u.f_permission.class = class;
  886. event->u.f_permission.rnw = flag & 0x1;
  887. }
  888. break;
  889. default:
  890. g_assert_not_reached();
  891. }
  892. return SMMU_TRANS_ERROR;
  893. }
  894. *out_entry = cached_entry;
  895. return SMMU_TRANS_SUCCESS;
  896. }
  897. /*
  898. * Sets the InputAddr for an SMMU_TRANS_ERROR, as it can't be
  899. * set from all contexts, as smmuv3_get_config() can return
  900. * translation faults in case of nested translation (for CD
  901. * and TTBx). But in that case the iova is not known.
  902. */
  903. static void smmuv3_fixup_event(SMMUEventInfo *event, hwaddr iova)
  904. {
  905. switch (event->type) {
  906. case SMMU_EVT_F_WALK_EABT:
  907. case SMMU_EVT_F_TRANSLATION:
  908. case SMMU_EVT_F_ADDR_SIZE:
  909. case SMMU_EVT_F_ACCESS:
  910. case SMMU_EVT_F_PERMISSION:
  911. event->u.f_walk_eabt.addr = iova;
  912. break;
  913. default:
  914. break;
  915. }
  916. }
  917. /* Entry point to SMMU, does everything. */
  918. static IOMMUTLBEntry smmuv3_translate(IOMMUMemoryRegion *mr, hwaddr addr,
  919. IOMMUAccessFlags flag, int iommu_idx)
  920. {
  921. SMMUDevice *sdev = container_of(mr, SMMUDevice, iommu);
  922. SMMUv3State *s = sdev->smmu;
  923. uint32_t sid = smmu_get_sid(sdev);
  924. SMMUEventInfo event = {.type = SMMU_EVT_NONE,
  925. .sid = sid,
  926. .inval_ste_allowed = false};
  927. SMMUTranslationStatus status;
  928. SMMUTransCfg *cfg = NULL;
  929. IOMMUTLBEntry entry = {
  930. .target_as = &address_space_memory,
  931. .iova = addr,
  932. .translated_addr = addr,
  933. .addr_mask = ~(hwaddr)0,
  934. .perm = IOMMU_NONE,
  935. };
  936. SMMUTLBEntry *cached_entry = NULL;
  937. qemu_mutex_lock(&s->mutex);
  938. if (!smmu_enabled(s)) {
  939. if (FIELD_EX32(s->gbpa, GBPA, ABORT)) {
  940. status = SMMU_TRANS_ABORT;
  941. } else {
  942. status = SMMU_TRANS_DISABLE;
  943. }
  944. goto epilogue;
  945. }
  946. cfg = smmuv3_get_config(sdev, &event);
  947. if (!cfg) {
  948. status = SMMU_TRANS_ERROR;
  949. goto epilogue;
  950. }
  951. if (cfg->aborted) {
  952. status = SMMU_TRANS_ABORT;
  953. goto epilogue;
  954. }
  955. if (cfg->bypassed) {
  956. status = SMMU_TRANS_BYPASS;
  957. goto epilogue;
  958. }
  959. status = smmuv3_do_translate(s, addr, cfg, &event, flag,
  960. &cached_entry, SMMU_CLASS_IN);
  961. epilogue:
  962. qemu_mutex_unlock(&s->mutex);
  963. switch (status) {
  964. case SMMU_TRANS_SUCCESS:
  965. entry.perm = cached_entry->entry.perm;
  966. entry.translated_addr = CACHED_ENTRY_TO_ADDR(cached_entry, addr);
  967. entry.addr_mask = cached_entry->entry.addr_mask;
  968. trace_smmuv3_translate_success(mr->parent_obj.name, sid, addr,
  969. entry.translated_addr, entry.perm,
  970. cfg->stage);
  971. break;
  972. case SMMU_TRANS_DISABLE:
  973. entry.perm = flag;
  974. entry.addr_mask = ~TARGET_PAGE_MASK;
  975. trace_smmuv3_translate_disable(mr->parent_obj.name, sid, addr,
  976. entry.perm);
  977. break;
  978. case SMMU_TRANS_BYPASS:
  979. entry.perm = flag;
  980. entry.addr_mask = ~TARGET_PAGE_MASK;
  981. trace_smmuv3_translate_bypass(mr->parent_obj.name, sid, addr,
  982. entry.perm);
  983. break;
  984. case SMMU_TRANS_ABORT:
  985. /* no event is recorded on abort */
  986. trace_smmuv3_translate_abort(mr->parent_obj.name, sid, addr,
  987. entry.perm);
  988. break;
  989. case SMMU_TRANS_ERROR:
  990. smmuv3_fixup_event(&event, addr);
  991. qemu_log_mask(LOG_GUEST_ERROR,
  992. "%s translation failed for iova=0x%"PRIx64" (%s)\n",
  993. mr->parent_obj.name, addr, smmu_event_string(event.type));
  994. smmuv3_record_event(s, &event);
  995. break;
  996. }
  997. return entry;
  998. }
  999. /**
  1000. * smmuv3_notify_iova - call the notifier @n for a given
  1001. * @asid and @iova tuple.
  1002. *
  1003. * @mr: IOMMU mr region handle
  1004. * @n: notifier to be called
  1005. * @asid: address space ID or negative value if we don't care
  1006. * @vmid: virtual machine ID or negative value if we don't care
  1007. * @iova: iova
  1008. * @tg: translation granule (if communicated through range invalidation)
  1009. * @num_pages: number of @granule sized pages (if tg != 0), otherwise 1
  1010. * @stage: Which stage(1 or 2) is used
  1011. */
  1012. static void smmuv3_notify_iova(IOMMUMemoryRegion *mr,
  1013. IOMMUNotifier *n,
  1014. int asid, int vmid,
  1015. dma_addr_t iova, uint8_t tg,
  1016. uint64_t num_pages, int stage)
  1017. {
  1018. SMMUDevice *sdev = container_of(mr, SMMUDevice, iommu);
  1019. SMMUEventInfo eventinfo = {.inval_ste_allowed = true};
  1020. SMMUTransCfg *cfg = smmuv3_get_config(sdev, &eventinfo);
  1021. IOMMUTLBEvent event;
  1022. uint8_t granule;
  1023. if (!cfg) {
  1024. return;
  1025. }
  1026. /*
  1027. * stage is passed from TLB invalidation commands which can be either
  1028. * stage-1 or stage-2.
  1029. * However, IOMMUTLBEvent only understands IOVA, for stage-1 or stage-2
  1030. * SMMU instances we consider the input address as the IOVA, but when
  1031. * nesting is used, we can't mix stage-1 and stage-2 addresses, so for
  1032. * nesting only stage-1 is considered the IOVA and would be notified.
  1033. */
  1034. if ((stage == SMMU_STAGE_2) && (cfg->stage == SMMU_NESTED))
  1035. return;
  1036. if (!tg) {
  1037. SMMUTransTableInfo *tt;
  1038. if (asid >= 0 && cfg->asid != asid) {
  1039. return;
  1040. }
  1041. if (vmid >= 0 && cfg->s2cfg.vmid != vmid) {
  1042. return;
  1043. }
  1044. if (stage == SMMU_STAGE_1) {
  1045. tt = select_tt(cfg, iova);
  1046. if (!tt) {
  1047. return;
  1048. }
  1049. granule = tt->granule_sz;
  1050. } else {
  1051. granule = cfg->s2cfg.granule_sz;
  1052. }
  1053. } else {
  1054. granule = tg * 2 + 10;
  1055. }
  1056. event.type = IOMMU_NOTIFIER_UNMAP;
  1057. event.entry.target_as = &address_space_memory;
  1058. event.entry.iova = iova;
  1059. event.entry.addr_mask = num_pages * (1 << granule) - 1;
  1060. event.entry.perm = IOMMU_NONE;
  1061. memory_region_notify_iommu_one(n, &event);
  1062. }
  1063. /* invalidate an asid/vmid/iova range tuple in all mr's */
  1064. static void smmuv3_inv_notifiers_iova(SMMUState *s, int asid, int vmid,
  1065. dma_addr_t iova, uint8_t tg,
  1066. uint64_t num_pages, int stage)
  1067. {
  1068. SMMUDevice *sdev;
  1069. QLIST_FOREACH(sdev, &s->devices_with_notifiers, next) {
  1070. IOMMUMemoryRegion *mr = &sdev->iommu;
  1071. IOMMUNotifier *n;
  1072. trace_smmuv3_inv_notifiers_iova(mr->parent_obj.name, asid, vmid,
  1073. iova, tg, num_pages, stage);
  1074. IOMMU_NOTIFIER_FOREACH(n, mr) {
  1075. smmuv3_notify_iova(mr, n, asid, vmid, iova, tg, num_pages, stage);
  1076. }
  1077. }
  1078. }
  1079. static void smmuv3_range_inval(SMMUState *s, Cmd *cmd, SMMUStage stage)
  1080. {
  1081. dma_addr_t end, addr = CMD_ADDR(cmd);
  1082. uint8_t type = CMD_TYPE(cmd);
  1083. int vmid = -1;
  1084. uint8_t scale = CMD_SCALE(cmd);
  1085. uint8_t num = CMD_NUM(cmd);
  1086. uint8_t ttl = CMD_TTL(cmd);
  1087. bool leaf = CMD_LEAF(cmd);
  1088. uint8_t tg = CMD_TG(cmd);
  1089. uint64_t num_pages;
  1090. uint8_t granule;
  1091. int asid = -1;
  1092. SMMUv3State *smmuv3 = ARM_SMMUV3(s);
  1093. /* Only consider VMID if stage-2 is supported. */
  1094. if (STAGE2_SUPPORTED(smmuv3)) {
  1095. vmid = CMD_VMID(cmd);
  1096. }
  1097. if (type == SMMU_CMD_TLBI_NH_VA) {
  1098. asid = CMD_ASID(cmd);
  1099. }
  1100. if (!tg) {
  1101. trace_smmuv3_range_inval(vmid, asid, addr, tg, 1, ttl, leaf, stage);
  1102. smmuv3_inv_notifiers_iova(s, asid, vmid, addr, tg, 1, stage);
  1103. if (stage == SMMU_STAGE_1) {
  1104. smmu_iotlb_inv_iova(s, asid, vmid, addr, tg, 1, ttl);
  1105. } else {
  1106. smmu_iotlb_inv_ipa(s, vmid, addr, tg, 1, ttl);
  1107. }
  1108. return;
  1109. }
  1110. /* RIL in use */
  1111. num_pages = (num + 1) * BIT_ULL(scale);
  1112. granule = tg * 2 + 10;
  1113. /* Split invalidations into ^2 range invalidations */
  1114. end = addr + (num_pages << granule) - 1;
  1115. while (addr != end + 1) {
  1116. uint64_t mask = dma_aligned_pow2_mask(addr, end, 64);
  1117. num_pages = (mask + 1) >> granule;
  1118. trace_smmuv3_range_inval(vmid, asid, addr, tg, num_pages,
  1119. ttl, leaf, stage);
  1120. smmuv3_inv_notifiers_iova(s, asid, vmid, addr, tg, num_pages, stage);
  1121. if (stage == SMMU_STAGE_1) {
  1122. smmu_iotlb_inv_iova(s, asid, vmid, addr, tg, num_pages, ttl);
  1123. } else {
  1124. smmu_iotlb_inv_ipa(s, vmid, addr, tg, num_pages, ttl);
  1125. }
  1126. addr += mask + 1;
  1127. }
  1128. }
  1129. static int smmuv3_cmdq_consume(SMMUv3State *s)
  1130. {
  1131. SMMUState *bs = ARM_SMMU(s);
  1132. SMMUCmdError cmd_error = SMMU_CERROR_NONE;
  1133. SMMUQueue *q = &s->cmdq;
  1134. SMMUCommandType type = 0;
  1135. if (!smmuv3_cmdq_enabled(s)) {
  1136. return 0;
  1137. }
  1138. /*
  1139. * some commands depend on register values, typically CR0. In case those
  1140. * register values change while handling the command, spec says it
  1141. * is UNPREDICTABLE whether the command is interpreted under the new
  1142. * or old value.
  1143. */
  1144. while (!smmuv3_q_empty(q)) {
  1145. uint32_t pending = s->gerror ^ s->gerrorn;
  1146. Cmd cmd;
  1147. trace_smmuv3_cmdq_consume(Q_PROD(q), Q_CONS(q),
  1148. Q_PROD_WRAP(q), Q_CONS_WRAP(q));
  1149. if (FIELD_EX32(pending, GERROR, CMDQ_ERR)) {
  1150. break;
  1151. }
  1152. if (queue_read(q, &cmd) != MEMTX_OK) {
  1153. cmd_error = SMMU_CERROR_ABT;
  1154. break;
  1155. }
  1156. type = CMD_TYPE(&cmd);
  1157. trace_smmuv3_cmdq_opcode(smmu_cmd_string(type));
  1158. qemu_mutex_lock(&s->mutex);
  1159. switch (type) {
  1160. case SMMU_CMD_SYNC:
  1161. if (CMD_SYNC_CS(&cmd) & CMD_SYNC_SIG_IRQ) {
  1162. smmuv3_trigger_irq(s, SMMU_IRQ_CMD_SYNC, 0);
  1163. }
  1164. break;
  1165. case SMMU_CMD_PREFETCH_CONFIG:
  1166. case SMMU_CMD_PREFETCH_ADDR:
  1167. break;
  1168. case SMMU_CMD_CFGI_STE:
  1169. {
  1170. uint32_t sid = CMD_SID(&cmd);
  1171. SMMUDevice *sdev = smmu_find_sdev(bs, sid);
  1172. if (CMD_SSEC(&cmd)) {
  1173. cmd_error = SMMU_CERROR_ILL;
  1174. break;
  1175. }
  1176. if (!sdev) {
  1177. break;
  1178. }
  1179. trace_smmuv3_cmdq_cfgi_ste(sid);
  1180. smmuv3_flush_config(sdev);
  1181. break;
  1182. }
  1183. case SMMU_CMD_CFGI_STE_RANGE: /* same as SMMU_CMD_CFGI_ALL */
  1184. {
  1185. uint32_t sid = CMD_SID(&cmd), mask;
  1186. uint8_t range = CMD_STE_RANGE(&cmd);
  1187. SMMUSIDRange sid_range;
  1188. if (CMD_SSEC(&cmd)) {
  1189. cmd_error = SMMU_CERROR_ILL;
  1190. break;
  1191. }
  1192. mask = (1ULL << (range + 1)) - 1;
  1193. sid_range.start = sid & ~mask;
  1194. sid_range.end = sid_range.start + mask;
  1195. trace_smmuv3_cmdq_cfgi_ste_range(sid_range.start, sid_range.end);
  1196. smmu_configs_inv_sid_range(bs, sid_range);
  1197. break;
  1198. }
  1199. case SMMU_CMD_CFGI_CD:
  1200. case SMMU_CMD_CFGI_CD_ALL:
  1201. {
  1202. uint32_t sid = CMD_SID(&cmd);
  1203. SMMUDevice *sdev = smmu_find_sdev(bs, sid);
  1204. if (CMD_SSEC(&cmd)) {
  1205. cmd_error = SMMU_CERROR_ILL;
  1206. break;
  1207. }
  1208. if (!sdev) {
  1209. break;
  1210. }
  1211. trace_smmuv3_cmdq_cfgi_cd(sid);
  1212. smmuv3_flush_config(sdev);
  1213. break;
  1214. }
  1215. case SMMU_CMD_TLBI_NH_ASID:
  1216. {
  1217. int asid = CMD_ASID(&cmd);
  1218. int vmid = -1;
  1219. if (!STAGE1_SUPPORTED(s)) {
  1220. cmd_error = SMMU_CERROR_ILL;
  1221. break;
  1222. }
  1223. /*
  1224. * VMID is only matched when stage 2 is supported, otherwise set it
  1225. * to -1 as the value used for stage-1 only VMIDs.
  1226. */
  1227. if (STAGE2_SUPPORTED(s)) {
  1228. vmid = CMD_VMID(&cmd);
  1229. }
  1230. trace_smmuv3_cmdq_tlbi_nh_asid(asid);
  1231. smmu_inv_notifiers_all(&s->smmu_state);
  1232. smmu_iotlb_inv_asid_vmid(bs, asid, vmid);
  1233. break;
  1234. }
  1235. case SMMU_CMD_TLBI_NH_ALL:
  1236. {
  1237. int vmid = -1;
  1238. if (!STAGE1_SUPPORTED(s)) {
  1239. cmd_error = SMMU_CERROR_ILL;
  1240. break;
  1241. }
  1242. /*
  1243. * If stage-2 is supported, invalidate for this VMID only, otherwise
  1244. * invalidate the whole thing.
  1245. */
  1246. if (STAGE2_SUPPORTED(s)) {
  1247. vmid = CMD_VMID(&cmd);
  1248. trace_smmuv3_cmdq_tlbi_nh(vmid);
  1249. smmu_iotlb_inv_vmid_s1(bs, vmid);
  1250. break;
  1251. }
  1252. QEMU_FALLTHROUGH;
  1253. }
  1254. case SMMU_CMD_TLBI_NSNH_ALL:
  1255. trace_smmuv3_cmdq_tlbi_nsnh();
  1256. smmu_inv_notifiers_all(&s->smmu_state);
  1257. smmu_iotlb_inv_all(bs);
  1258. break;
  1259. case SMMU_CMD_TLBI_NH_VAA:
  1260. case SMMU_CMD_TLBI_NH_VA:
  1261. if (!STAGE1_SUPPORTED(s)) {
  1262. cmd_error = SMMU_CERROR_ILL;
  1263. break;
  1264. }
  1265. smmuv3_range_inval(bs, &cmd, SMMU_STAGE_1);
  1266. break;
  1267. case SMMU_CMD_TLBI_S12_VMALL:
  1268. {
  1269. int vmid = CMD_VMID(&cmd);
  1270. if (!STAGE2_SUPPORTED(s)) {
  1271. cmd_error = SMMU_CERROR_ILL;
  1272. break;
  1273. }
  1274. trace_smmuv3_cmdq_tlbi_s12_vmid(vmid);
  1275. smmu_inv_notifiers_all(&s->smmu_state);
  1276. smmu_iotlb_inv_vmid(bs, vmid);
  1277. break;
  1278. }
  1279. case SMMU_CMD_TLBI_S2_IPA:
  1280. if (!STAGE2_SUPPORTED(s)) {
  1281. cmd_error = SMMU_CERROR_ILL;
  1282. break;
  1283. }
  1284. /*
  1285. * As currently only either s1 or s2 are supported
  1286. * we can reuse same function for s2.
  1287. */
  1288. smmuv3_range_inval(bs, &cmd, SMMU_STAGE_2);
  1289. break;
  1290. case SMMU_CMD_TLBI_EL3_ALL:
  1291. case SMMU_CMD_TLBI_EL3_VA:
  1292. case SMMU_CMD_TLBI_EL2_ALL:
  1293. case SMMU_CMD_TLBI_EL2_ASID:
  1294. case SMMU_CMD_TLBI_EL2_VA:
  1295. case SMMU_CMD_TLBI_EL2_VAA:
  1296. case SMMU_CMD_ATC_INV:
  1297. case SMMU_CMD_PRI_RESP:
  1298. case SMMU_CMD_RESUME:
  1299. case SMMU_CMD_STALL_TERM:
  1300. trace_smmuv3_unhandled_cmd(type);
  1301. break;
  1302. default:
  1303. cmd_error = SMMU_CERROR_ILL;
  1304. break;
  1305. }
  1306. qemu_mutex_unlock(&s->mutex);
  1307. if (cmd_error) {
  1308. if (cmd_error == SMMU_CERROR_ILL) {
  1309. qemu_log_mask(LOG_GUEST_ERROR,
  1310. "Illegal command type: %d\n", CMD_TYPE(&cmd));
  1311. }
  1312. break;
  1313. }
  1314. /*
  1315. * We only increment the cons index after the completion of
  1316. * the command. We do that because the SYNC returns immediately
  1317. * and does not check the completion of previous commands
  1318. */
  1319. queue_cons_incr(q);
  1320. }
  1321. if (cmd_error) {
  1322. trace_smmuv3_cmdq_consume_error(smmu_cmd_string(type), cmd_error);
  1323. smmu_write_cmdq_err(s, cmd_error);
  1324. smmuv3_trigger_irq(s, SMMU_IRQ_GERROR, R_GERROR_CMDQ_ERR_MASK);
  1325. }
  1326. trace_smmuv3_cmdq_consume_out(Q_PROD(q), Q_CONS(q),
  1327. Q_PROD_WRAP(q), Q_CONS_WRAP(q));
  1328. return 0;
  1329. }
  1330. static MemTxResult smmu_writell(SMMUv3State *s, hwaddr offset,
  1331. uint64_t data, MemTxAttrs attrs)
  1332. {
  1333. switch (offset) {
  1334. case A_GERROR_IRQ_CFG0:
  1335. s->gerror_irq_cfg0 = data;
  1336. return MEMTX_OK;
  1337. case A_STRTAB_BASE:
  1338. s->strtab_base = data;
  1339. return MEMTX_OK;
  1340. case A_CMDQ_BASE:
  1341. s->cmdq.base = data;
  1342. s->cmdq.log2size = extract64(s->cmdq.base, 0, 5);
  1343. if (s->cmdq.log2size > SMMU_CMDQS) {
  1344. s->cmdq.log2size = SMMU_CMDQS;
  1345. }
  1346. return MEMTX_OK;
  1347. case A_EVENTQ_BASE:
  1348. s->eventq.base = data;
  1349. s->eventq.log2size = extract64(s->eventq.base, 0, 5);
  1350. if (s->eventq.log2size > SMMU_EVENTQS) {
  1351. s->eventq.log2size = SMMU_EVENTQS;
  1352. }
  1353. return MEMTX_OK;
  1354. case A_EVENTQ_IRQ_CFG0:
  1355. s->eventq_irq_cfg0 = data;
  1356. return MEMTX_OK;
  1357. default:
  1358. qemu_log_mask(LOG_UNIMP,
  1359. "%s Unexpected 64-bit access to 0x%"PRIx64" (WI)\n",
  1360. __func__, offset);
  1361. return MEMTX_OK;
  1362. }
  1363. }
  1364. static MemTxResult smmu_writel(SMMUv3State *s, hwaddr offset,
  1365. uint64_t data, MemTxAttrs attrs)
  1366. {
  1367. switch (offset) {
  1368. case A_CR0:
  1369. s->cr[0] = data;
  1370. s->cr0ack = data & ~SMMU_CR0_RESERVED;
  1371. /* in case the command queue has been enabled */
  1372. smmuv3_cmdq_consume(s);
  1373. return MEMTX_OK;
  1374. case A_CR1:
  1375. s->cr[1] = data;
  1376. return MEMTX_OK;
  1377. case A_CR2:
  1378. s->cr[2] = data;
  1379. return MEMTX_OK;
  1380. case A_IRQ_CTRL:
  1381. s->irq_ctrl = data;
  1382. return MEMTX_OK;
  1383. case A_GERRORN:
  1384. smmuv3_write_gerrorn(s, data);
  1385. /*
  1386. * By acknowledging the CMDQ_ERR, SW may notify cmds can
  1387. * be processed again
  1388. */
  1389. smmuv3_cmdq_consume(s);
  1390. return MEMTX_OK;
  1391. case A_GERROR_IRQ_CFG0: /* 64b */
  1392. s->gerror_irq_cfg0 = deposit64(s->gerror_irq_cfg0, 0, 32, data);
  1393. return MEMTX_OK;
  1394. case A_GERROR_IRQ_CFG0 + 4:
  1395. s->gerror_irq_cfg0 = deposit64(s->gerror_irq_cfg0, 32, 32, data);
  1396. return MEMTX_OK;
  1397. case A_GERROR_IRQ_CFG1:
  1398. s->gerror_irq_cfg1 = data;
  1399. return MEMTX_OK;
  1400. case A_GERROR_IRQ_CFG2:
  1401. s->gerror_irq_cfg2 = data;
  1402. return MEMTX_OK;
  1403. case A_GBPA:
  1404. /*
  1405. * If UPDATE is not set, the write is ignored. This is the only
  1406. * permitted behavior in SMMUv3.2 and later.
  1407. */
  1408. if (data & R_GBPA_UPDATE_MASK) {
  1409. /* Ignore update bit as write is synchronous. */
  1410. s->gbpa = data & ~R_GBPA_UPDATE_MASK;
  1411. }
  1412. return MEMTX_OK;
  1413. case A_STRTAB_BASE: /* 64b */
  1414. s->strtab_base = deposit64(s->strtab_base, 0, 32, data);
  1415. return MEMTX_OK;
  1416. case A_STRTAB_BASE + 4:
  1417. s->strtab_base = deposit64(s->strtab_base, 32, 32, data);
  1418. return MEMTX_OK;
  1419. case A_STRTAB_BASE_CFG:
  1420. s->strtab_base_cfg = data;
  1421. if (FIELD_EX32(data, STRTAB_BASE_CFG, FMT) == 1) {
  1422. s->sid_split = FIELD_EX32(data, STRTAB_BASE_CFG, SPLIT);
  1423. s->features |= SMMU_FEATURE_2LVL_STE;
  1424. }
  1425. return MEMTX_OK;
  1426. case A_CMDQ_BASE: /* 64b */
  1427. s->cmdq.base = deposit64(s->cmdq.base, 0, 32, data);
  1428. s->cmdq.log2size = extract64(s->cmdq.base, 0, 5);
  1429. if (s->cmdq.log2size > SMMU_CMDQS) {
  1430. s->cmdq.log2size = SMMU_CMDQS;
  1431. }
  1432. return MEMTX_OK;
  1433. case A_CMDQ_BASE + 4: /* 64b */
  1434. s->cmdq.base = deposit64(s->cmdq.base, 32, 32, data);
  1435. return MEMTX_OK;
  1436. case A_CMDQ_PROD:
  1437. s->cmdq.prod = data;
  1438. smmuv3_cmdq_consume(s);
  1439. return MEMTX_OK;
  1440. case A_CMDQ_CONS:
  1441. s->cmdq.cons = data;
  1442. return MEMTX_OK;
  1443. case A_EVENTQ_BASE: /* 64b */
  1444. s->eventq.base = deposit64(s->eventq.base, 0, 32, data);
  1445. s->eventq.log2size = extract64(s->eventq.base, 0, 5);
  1446. if (s->eventq.log2size > SMMU_EVENTQS) {
  1447. s->eventq.log2size = SMMU_EVENTQS;
  1448. }
  1449. return MEMTX_OK;
  1450. case A_EVENTQ_BASE + 4:
  1451. s->eventq.base = deposit64(s->eventq.base, 32, 32, data);
  1452. return MEMTX_OK;
  1453. case A_EVENTQ_PROD:
  1454. s->eventq.prod = data;
  1455. return MEMTX_OK;
  1456. case A_EVENTQ_CONS:
  1457. s->eventq.cons = data;
  1458. return MEMTX_OK;
  1459. case A_EVENTQ_IRQ_CFG0: /* 64b */
  1460. s->eventq_irq_cfg0 = deposit64(s->eventq_irq_cfg0, 0, 32, data);
  1461. return MEMTX_OK;
  1462. case A_EVENTQ_IRQ_CFG0 + 4:
  1463. s->eventq_irq_cfg0 = deposit64(s->eventq_irq_cfg0, 32, 32, data);
  1464. return MEMTX_OK;
  1465. case A_EVENTQ_IRQ_CFG1:
  1466. s->eventq_irq_cfg1 = data;
  1467. return MEMTX_OK;
  1468. case A_EVENTQ_IRQ_CFG2:
  1469. s->eventq_irq_cfg2 = data;
  1470. return MEMTX_OK;
  1471. default:
  1472. qemu_log_mask(LOG_UNIMP,
  1473. "%s Unexpected 32-bit access to 0x%"PRIx64" (WI)\n",
  1474. __func__, offset);
  1475. return MEMTX_OK;
  1476. }
  1477. }
  1478. static MemTxResult smmu_write_mmio(void *opaque, hwaddr offset, uint64_t data,
  1479. unsigned size, MemTxAttrs attrs)
  1480. {
  1481. SMMUState *sys = opaque;
  1482. SMMUv3State *s = ARM_SMMUV3(sys);
  1483. MemTxResult r;
  1484. /* CONSTRAINED UNPREDICTABLE choice to have page0/1 be exact aliases */
  1485. offset &= ~0x10000;
  1486. switch (size) {
  1487. case 8:
  1488. r = smmu_writell(s, offset, data, attrs);
  1489. break;
  1490. case 4:
  1491. r = smmu_writel(s, offset, data, attrs);
  1492. break;
  1493. default:
  1494. r = MEMTX_ERROR;
  1495. break;
  1496. }
  1497. trace_smmuv3_write_mmio(offset, data, size, r);
  1498. return r;
  1499. }
  1500. static MemTxResult smmu_readll(SMMUv3State *s, hwaddr offset,
  1501. uint64_t *data, MemTxAttrs attrs)
  1502. {
  1503. switch (offset) {
  1504. case A_GERROR_IRQ_CFG0:
  1505. *data = s->gerror_irq_cfg0;
  1506. return MEMTX_OK;
  1507. case A_STRTAB_BASE:
  1508. *data = s->strtab_base;
  1509. return MEMTX_OK;
  1510. case A_CMDQ_BASE:
  1511. *data = s->cmdq.base;
  1512. return MEMTX_OK;
  1513. case A_EVENTQ_BASE:
  1514. *data = s->eventq.base;
  1515. return MEMTX_OK;
  1516. default:
  1517. *data = 0;
  1518. qemu_log_mask(LOG_UNIMP,
  1519. "%s Unexpected 64-bit access to 0x%"PRIx64" (RAZ)\n",
  1520. __func__, offset);
  1521. return MEMTX_OK;
  1522. }
  1523. }
  1524. static MemTxResult smmu_readl(SMMUv3State *s, hwaddr offset,
  1525. uint64_t *data, MemTxAttrs attrs)
  1526. {
  1527. switch (offset) {
  1528. case A_IDREGS ... A_IDREGS + 0x2f:
  1529. *data = smmuv3_idreg(offset - A_IDREGS);
  1530. return MEMTX_OK;
  1531. case A_IDR0 ... A_IDR5:
  1532. *data = s->idr[(offset - A_IDR0) / 4];
  1533. return MEMTX_OK;
  1534. case A_IIDR:
  1535. *data = s->iidr;
  1536. return MEMTX_OK;
  1537. case A_AIDR:
  1538. *data = s->aidr;
  1539. return MEMTX_OK;
  1540. case A_CR0:
  1541. *data = s->cr[0];
  1542. return MEMTX_OK;
  1543. case A_CR0ACK:
  1544. *data = s->cr0ack;
  1545. return MEMTX_OK;
  1546. case A_CR1:
  1547. *data = s->cr[1];
  1548. return MEMTX_OK;
  1549. case A_CR2:
  1550. *data = s->cr[2];
  1551. return MEMTX_OK;
  1552. case A_STATUSR:
  1553. *data = s->statusr;
  1554. return MEMTX_OK;
  1555. case A_GBPA:
  1556. *data = s->gbpa;
  1557. return MEMTX_OK;
  1558. case A_IRQ_CTRL:
  1559. case A_IRQ_CTRL_ACK:
  1560. *data = s->irq_ctrl;
  1561. return MEMTX_OK;
  1562. case A_GERROR:
  1563. *data = s->gerror;
  1564. return MEMTX_OK;
  1565. case A_GERRORN:
  1566. *data = s->gerrorn;
  1567. return MEMTX_OK;
  1568. case A_GERROR_IRQ_CFG0: /* 64b */
  1569. *data = extract64(s->gerror_irq_cfg0, 0, 32);
  1570. return MEMTX_OK;
  1571. case A_GERROR_IRQ_CFG0 + 4:
  1572. *data = extract64(s->gerror_irq_cfg0, 32, 32);
  1573. return MEMTX_OK;
  1574. case A_GERROR_IRQ_CFG1:
  1575. *data = s->gerror_irq_cfg1;
  1576. return MEMTX_OK;
  1577. case A_GERROR_IRQ_CFG2:
  1578. *data = s->gerror_irq_cfg2;
  1579. return MEMTX_OK;
  1580. case A_STRTAB_BASE: /* 64b */
  1581. *data = extract64(s->strtab_base, 0, 32);
  1582. return MEMTX_OK;
  1583. case A_STRTAB_BASE + 4: /* 64b */
  1584. *data = extract64(s->strtab_base, 32, 32);
  1585. return MEMTX_OK;
  1586. case A_STRTAB_BASE_CFG:
  1587. *data = s->strtab_base_cfg;
  1588. return MEMTX_OK;
  1589. case A_CMDQ_BASE: /* 64b */
  1590. *data = extract64(s->cmdq.base, 0, 32);
  1591. return MEMTX_OK;
  1592. case A_CMDQ_BASE + 4:
  1593. *data = extract64(s->cmdq.base, 32, 32);
  1594. return MEMTX_OK;
  1595. case A_CMDQ_PROD:
  1596. *data = s->cmdq.prod;
  1597. return MEMTX_OK;
  1598. case A_CMDQ_CONS:
  1599. *data = s->cmdq.cons;
  1600. return MEMTX_OK;
  1601. case A_EVENTQ_BASE: /* 64b */
  1602. *data = extract64(s->eventq.base, 0, 32);
  1603. return MEMTX_OK;
  1604. case A_EVENTQ_BASE + 4: /* 64b */
  1605. *data = extract64(s->eventq.base, 32, 32);
  1606. return MEMTX_OK;
  1607. case A_EVENTQ_PROD:
  1608. *data = s->eventq.prod;
  1609. return MEMTX_OK;
  1610. case A_EVENTQ_CONS:
  1611. *data = s->eventq.cons;
  1612. return MEMTX_OK;
  1613. default:
  1614. *data = 0;
  1615. qemu_log_mask(LOG_UNIMP,
  1616. "%s unhandled 32-bit access at 0x%"PRIx64" (RAZ)\n",
  1617. __func__, offset);
  1618. return MEMTX_OK;
  1619. }
  1620. }
  1621. static MemTxResult smmu_read_mmio(void *opaque, hwaddr offset, uint64_t *data,
  1622. unsigned size, MemTxAttrs attrs)
  1623. {
  1624. SMMUState *sys = opaque;
  1625. SMMUv3State *s = ARM_SMMUV3(sys);
  1626. MemTxResult r;
  1627. /* CONSTRAINED UNPREDICTABLE choice to have page0/1 be exact aliases */
  1628. offset &= ~0x10000;
  1629. switch (size) {
  1630. case 8:
  1631. r = smmu_readll(s, offset, data, attrs);
  1632. break;
  1633. case 4:
  1634. r = smmu_readl(s, offset, data, attrs);
  1635. break;
  1636. default:
  1637. r = MEMTX_ERROR;
  1638. break;
  1639. }
  1640. trace_smmuv3_read_mmio(offset, *data, size, r);
  1641. return r;
  1642. }
  1643. static const MemoryRegionOps smmu_mem_ops = {
  1644. .read_with_attrs = smmu_read_mmio,
  1645. .write_with_attrs = smmu_write_mmio,
  1646. .endianness = DEVICE_LITTLE_ENDIAN,
  1647. .valid = {
  1648. .min_access_size = 4,
  1649. .max_access_size = 8,
  1650. },
  1651. .impl = {
  1652. .min_access_size = 4,
  1653. .max_access_size = 8,
  1654. },
  1655. };
  1656. static void smmu_init_irq(SMMUv3State *s, SysBusDevice *dev)
  1657. {
  1658. int i;
  1659. for (i = 0; i < ARRAY_SIZE(s->irq); i++) {
  1660. sysbus_init_irq(dev, &s->irq[i]);
  1661. }
  1662. }
  1663. /*
  1664. * Make sure the IOMMU is reset in 'exit' phase after
  1665. * all outstanding DMA requests have been quiesced during
  1666. * the 'enter' or 'hold' reset phases
  1667. */
  1668. static void smmu_reset_exit(Object *obj, ResetType type)
  1669. {
  1670. SMMUv3State *s = ARM_SMMUV3(obj);
  1671. SMMUv3Class *c = ARM_SMMUV3_GET_CLASS(s);
  1672. trace_smmu_reset_exit();
  1673. if (c->parent_phases.exit) {
  1674. c->parent_phases.exit(obj, type);
  1675. }
  1676. smmuv3_init_regs(s);
  1677. }
  1678. static void smmu_realize(DeviceState *d, Error **errp)
  1679. {
  1680. SMMUState *sys = ARM_SMMU(d);
  1681. SMMUv3State *s = ARM_SMMUV3(sys);
  1682. SMMUv3Class *c = ARM_SMMUV3_GET_CLASS(s);
  1683. SysBusDevice *dev = SYS_BUS_DEVICE(d);
  1684. Error *local_err = NULL;
  1685. c->parent_realize(d, &local_err);
  1686. if (local_err) {
  1687. error_propagate(errp, local_err);
  1688. return;
  1689. }
  1690. qemu_mutex_init(&s->mutex);
  1691. memory_region_init_io(&sys->iomem, OBJECT(s),
  1692. &smmu_mem_ops, sys, TYPE_ARM_SMMUV3, 0x20000);
  1693. sys->mrtypename = TYPE_SMMUV3_IOMMU_MEMORY_REGION;
  1694. sysbus_init_mmio(dev, &sys->iomem);
  1695. smmu_init_irq(s, dev);
  1696. }
  1697. static const VMStateDescription vmstate_smmuv3_queue = {
  1698. .name = "smmuv3_queue",
  1699. .version_id = 1,
  1700. .minimum_version_id = 1,
  1701. .fields = (const VMStateField[]) {
  1702. VMSTATE_UINT64(base, SMMUQueue),
  1703. VMSTATE_UINT32(prod, SMMUQueue),
  1704. VMSTATE_UINT32(cons, SMMUQueue),
  1705. VMSTATE_UINT8(log2size, SMMUQueue),
  1706. VMSTATE_END_OF_LIST(),
  1707. },
  1708. };
  1709. static bool smmuv3_gbpa_needed(void *opaque)
  1710. {
  1711. SMMUv3State *s = opaque;
  1712. /* Only migrate GBPA if it has different reset value. */
  1713. return s->gbpa != SMMU_GBPA_RESET_VAL;
  1714. }
  1715. static const VMStateDescription vmstate_gbpa = {
  1716. .name = "smmuv3/gbpa",
  1717. .version_id = 1,
  1718. .minimum_version_id = 1,
  1719. .needed = smmuv3_gbpa_needed,
  1720. .fields = (const VMStateField[]) {
  1721. VMSTATE_UINT32(gbpa, SMMUv3State),
  1722. VMSTATE_END_OF_LIST()
  1723. }
  1724. };
  1725. static const VMStateDescription vmstate_smmuv3 = {
  1726. .name = "smmuv3",
  1727. .version_id = 1,
  1728. .minimum_version_id = 1,
  1729. .priority = MIG_PRI_IOMMU,
  1730. .fields = (const VMStateField[]) {
  1731. VMSTATE_UINT32(features, SMMUv3State),
  1732. VMSTATE_UINT8(sid_size, SMMUv3State),
  1733. VMSTATE_UINT8(sid_split, SMMUv3State),
  1734. VMSTATE_UINT32_ARRAY(cr, SMMUv3State, 3),
  1735. VMSTATE_UINT32(cr0ack, SMMUv3State),
  1736. VMSTATE_UINT32(statusr, SMMUv3State),
  1737. VMSTATE_UINT32(irq_ctrl, SMMUv3State),
  1738. VMSTATE_UINT32(gerror, SMMUv3State),
  1739. VMSTATE_UINT32(gerrorn, SMMUv3State),
  1740. VMSTATE_UINT64(gerror_irq_cfg0, SMMUv3State),
  1741. VMSTATE_UINT32(gerror_irq_cfg1, SMMUv3State),
  1742. VMSTATE_UINT32(gerror_irq_cfg2, SMMUv3State),
  1743. VMSTATE_UINT64(strtab_base, SMMUv3State),
  1744. VMSTATE_UINT32(strtab_base_cfg, SMMUv3State),
  1745. VMSTATE_UINT64(eventq_irq_cfg0, SMMUv3State),
  1746. VMSTATE_UINT32(eventq_irq_cfg1, SMMUv3State),
  1747. VMSTATE_UINT32(eventq_irq_cfg2, SMMUv3State),
  1748. VMSTATE_STRUCT(cmdq, SMMUv3State, 0, vmstate_smmuv3_queue, SMMUQueue),
  1749. VMSTATE_STRUCT(eventq, SMMUv3State, 0, vmstate_smmuv3_queue, SMMUQueue),
  1750. VMSTATE_END_OF_LIST(),
  1751. },
  1752. .subsections = (const VMStateDescription * const []) {
  1753. &vmstate_gbpa,
  1754. NULL
  1755. }
  1756. };
  1757. static const Property smmuv3_properties[] = {
  1758. /*
  1759. * Stages of translation advertised.
  1760. * "1": Stage 1
  1761. * "2": Stage 2
  1762. * "nested": Both stage 1 and stage 2
  1763. * Defaults to stage 1
  1764. */
  1765. DEFINE_PROP_STRING("stage", SMMUv3State, stage),
  1766. };
  1767. static void smmuv3_instance_init(Object *obj)
  1768. {
  1769. /* Nothing much to do here as of now */
  1770. }
  1771. static void smmuv3_class_init(ObjectClass *klass, void *data)
  1772. {
  1773. DeviceClass *dc = DEVICE_CLASS(klass);
  1774. ResettableClass *rc = RESETTABLE_CLASS(klass);
  1775. SMMUv3Class *c = ARM_SMMUV3_CLASS(klass);
  1776. dc->vmsd = &vmstate_smmuv3;
  1777. resettable_class_set_parent_phases(rc, NULL, NULL, smmu_reset_exit,
  1778. &c->parent_phases);
  1779. device_class_set_parent_realize(dc, smmu_realize,
  1780. &c->parent_realize);
  1781. device_class_set_props(dc, smmuv3_properties);
  1782. }
  1783. static int smmuv3_notify_flag_changed(IOMMUMemoryRegion *iommu,
  1784. IOMMUNotifierFlag old,
  1785. IOMMUNotifierFlag new,
  1786. Error **errp)
  1787. {
  1788. SMMUDevice *sdev = container_of(iommu, SMMUDevice, iommu);
  1789. SMMUv3State *s3 = sdev->smmu;
  1790. SMMUState *s = &(s3->smmu_state);
  1791. if (new & IOMMU_NOTIFIER_DEVIOTLB_UNMAP) {
  1792. error_setg(errp, "SMMUv3 does not support dev-iotlb yet");
  1793. return -EINVAL;
  1794. }
  1795. if (new & IOMMU_NOTIFIER_MAP) {
  1796. error_setg(errp,
  1797. "device %02x.%02x.%x requires iommu MAP notifier which is "
  1798. "not currently supported", pci_bus_num(sdev->bus),
  1799. PCI_SLOT(sdev->devfn), PCI_FUNC(sdev->devfn));
  1800. return -EINVAL;
  1801. }
  1802. if (old == IOMMU_NOTIFIER_NONE) {
  1803. trace_smmuv3_notify_flag_add(iommu->parent_obj.name);
  1804. QLIST_INSERT_HEAD(&s->devices_with_notifiers, sdev, next);
  1805. } else if (new == IOMMU_NOTIFIER_NONE) {
  1806. trace_smmuv3_notify_flag_del(iommu->parent_obj.name);
  1807. QLIST_REMOVE(sdev, next);
  1808. }
  1809. return 0;
  1810. }
  1811. static void smmuv3_iommu_memory_region_class_init(ObjectClass *klass,
  1812. void *data)
  1813. {
  1814. IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_CLASS(klass);
  1815. imrc->translate = smmuv3_translate;
  1816. imrc->notify_flag_changed = smmuv3_notify_flag_changed;
  1817. }
  1818. static const TypeInfo smmuv3_type_info = {
  1819. .name = TYPE_ARM_SMMUV3,
  1820. .parent = TYPE_ARM_SMMU,
  1821. .instance_size = sizeof(SMMUv3State),
  1822. .instance_init = smmuv3_instance_init,
  1823. .class_size = sizeof(SMMUv3Class),
  1824. .class_init = smmuv3_class_init,
  1825. };
  1826. static const TypeInfo smmuv3_iommu_memory_region_info = {
  1827. .parent = TYPE_IOMMU_MEMORY_REGION,
  1828. .name = TYPE_SMMUV3_IOMMU_MEMORY_REGION,
  1829. .class_init = smmuv3_iommu_memory_region_class_init,
  1830. };
  1831. static void smmuv3_register_types(void)
  1832. {
  1833. type_register_static(&smmuv3_type_info);
  1834. type_register_static(&smmuv3_iommu_memory_region_info);
  1835. }
  1836. type_init(smmuv3_register_types)