2
0

smmu-common.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  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. * Author: Prem Mallappa <pmallapp@broadcom.com>
  16. *
  17. */
  18. #include "qemu/osdep.h"
  19. #include "trace.h"
  20. #include "exec/target_page.h"
  21. #include "hw/core/cpu.h"
  22. #include "hw/qdev-properties.h"
  23. #include "qapi/error.h"
  24. #include "qemu/jhash.h"
  25. #include "qemu/module.h"
  26. #include "qemu/error-report.h"
  27. #include "hw/arm/smmu-common.h"
  28. #include "smmu-internal.h"
  29. /* IOTLB Management */
  30. static guint smmu_iotlb_key_hash(gconstpointer v)
  31. {
  32. SMMUIOTLBKey *key = (SMMUIOTLBKey *)v;
  33. uint32_t a, b, c;
  34. /* Jenkins hash */
  35. a = b = c = JHASH_INITVAL + sizeof(*key);
  36. a += key->asid + key->vmid + key->level + key->tg;
  37. b += extract64(key->iova, 0, 32);
  38. c += extract64(key->iova, 32, 32);
  39. __jhash_mix(a, b, c);
  40. __jhash_final(a, b, c);
  41. return c;
  42. }
  43. static gboolean smmu_iotlb_key_equal(gconstpointer v1, gconstpointer v2)
  44. {
  45. SMMUIOTLBKey *k1 = (SMMUIOTLBKey *)v1, *k2 = (SMMUIOTLBKey *)v2;
  46. return (k1->asid == k2->asid) && (k1->iova == k2->iova) &&
  47. (k1->level == k2->level) && (k1->tg == k2->tg) &&
  48. (k1->vmid == k2->vmid);
  49. }
  50. SMMUIOTLBKey smmu_get_iotlb_key(int asid, int vmid, uint64_t iova,
  51. uint8_t tg, uint8_t level)
  52. {
  53. SMMUIOTLBKey key = {.asid = asid, .vmid = vmid, .iova = iova,
  54. .tg = tg, .level = level};
  55. return key;
  56. }
  57. static SMMUTLBEntry *smmu_iotlb_lookup_all_levels(SMMUState *bs,
  58. SMMUTransCfg *cfg,
  59. SMMUTransTableInfo *tt,
  60. hwaddr iova)
  61. {
  62. uint8_t tg = (tt->granule_sz - 10) / 2;
  63. uint8_t inputsize = 64 - tt->tsz;
  64. uint8_t stride = tt->granule_sz - 3;
  65. uint8_t level = 4 - (inputsize - 4) / stride;
  66. SMMUTLBEntry *entry = NULL;
  67. while (level <= 3) {
  68. uint64_t subpage_size = 1ULL << level_shift(level, tt->granule_sz);
  69. uint64_t mask = subpage_size - 1;
  70. SMMUIOTLBKey key;
  71. key = smmu_get_iotlb_key(cfg->asid, cfg->s2cfg.vmid,
  72. iova & ~mask, tg, level);
  73. entry = g_hash_table_lookup(bs->iotlb, &key);
  74. if (entry) {
  75. break;
  76. }
  77. level++;
  78. }
  79. return entry;
  80. }
  81. /**
  82. * smmu_iotlb_lookup - Look up for a TLB entry.
  83. * @bs: SMMU state which includes the TLB instance
  84. * @cfg: Configuration of the translation
  85. * @tt: Translation table info (granule and tsz)
  86. * @iova: IOVA address to lookup
  87. *
  88. * returns a valid entry on success, otherwise NULL.
  89. * In case of nested translation, tt can be updated to include
  90. * the granule of the found entry as it might different from
  91. * the IOVA granule.
  92. */
  93. SMMUTLBEntry *smmu_iotlb_lookup(SMMUState *bs, SMMUTransCfg *cfg,
  94. SMMUTransTableInfo *tt, hwaddr iova)
  95. {
  96. SMMUTLBEntry *entry = NULL;
  97. entry = smmu_iotlb_lookup_all_levels(bs, cfg, tt, iova);
  98. /*
  99. * For nested translation also try the s2 granule, as the TLB will insert
  100. * it if the size of s2 tlb entry was smaller.
  101. */
  102. if (!entry && (cfg->stage == SMMU_NESTED) &&
  103. (cfg->s2cfg.granule_sz != tt->granule_sz)) {
  104. tt->granule_sz = cfg->s2cfg.granule_sz;
  105. entry = smmu_iotlb_lookup_all_levels(bs, cfg, tt, iova);
  106. }
  107. if (entry) {
  108. cfg->iotlb_hits++;
  109. trace_smmu_iotlb_lookup_hit(cfg->asid, cfg->s2cfg.vmid, iova,
  110. cfg->iotlb_hits, cfg->iotlb_misses,
  111. 100 * cfg->iotlb_hits /
  112. (cfg->iotlb_hits + cfg->iotlb_misses));
  113. } else {
  114. cfg->iotlb_misses++;
  115. trace_smmu_iotlb_lookup_miss(cfg->asid, cfg->s2cfg.vmid, iova,
  116. cfg->iotlb_hits, cfg->iotlb_misses,
  117. 100 * cfg->iotlb_hits /
  118. (cfg->iotlb_hits + cfg->iotlb_misses));
  119. }
  120. return entry;
  121. }
  122. void smmu_iotlb_insert(SMMUState *bs, SMMUTransCfg *cfg, SMMUTLBEntry *new)
  123. {
  124. SMMUIOTLBKey *key = g_new0(SMMUIOTLBKey, 1);
  125. uint8_t tg = (new->granule - 10) / 2;
  126. if (g_hash_table_size(bs->iotlb) >= SMMU_IOTLB_MAX_SIZE) {
  127. smmu_iotlb_inv_all(bs);
  128. }
  129. *key = smmu_get_iotlb_key(cfg->asid, cfg->s2cfg.vmid, new->entry.iova,
  130. tg, new->level);
  131. trace_smmu_iotlb_insert(cfg->asid, cfg->s2cfg.vmid, new->entry.iova,
  132. tg, new->level);
  133. g_hash_table_insert(bs->iotlb, key, new);
  134. }
  135. void smmu_iotlb_inv_all(SMMUState *s)
  136. {
  137. trace_smmu_iotlb_inv_all();
  138. g_hash_table_remove_all(s->iotlb);
  139. }
  140. static gboolean smmu_hash_remove_by_asid_vmid(gpointer key, gpointer value,
  141. gpointer user_data)
  142. {
  143. SMMUIOTLBPageInvInfo *info = (SMMUIOTLBPageInvInfo *)user_data;
  144. SMMUIOTLBKey *iotlb_key = (SMMUIOTLBKey *)key;
  145. return (SMMU_IOTLB_ASID(*iotlb_key) == info->asid) &&
  146. (SMMU_IOTLB_VMID(*iotlb_key) == info->vmid);
  147. }
  148. static gboolean smmu_hash_remove_by_vmid(gpointer key, gpointer value,
  149. gpointer user_data)
  150. {
  151. int vmid = *(int *)user_data;
  152. SMMUIOTLBKey *iotlb_key = (SMMUIOTLBKey *)key;
  153. return SMMU_IOTLB_VMID(*iotlb_key) == vmid;
  154. }
  155. static gboolean smmu_hash_remove_by_vmid_s1(gpointer key, gpointer value,
  156. gpointer user_data)
  157. {
  158. int vmid = *(int *)user_data;
  159. SMMUIOTLBKey *iotlb_key = (SMMUIOTLBKey *)key;
  160. return (SMMU_IOTLB_VMID(*iotlb_key) == vmid) &&
  161. (SMMU_IOTLB_ASID(*iotlb_key) >= 0);
  162. }
  163. static gboolean smmu_hash_remove_by_asid_vmid_iova(gpointer key, gpointer value,
  164. gpointer user_data)
  165. {
  166. SMMUTLBEntry *iter = (SMMUTLBEntry *)value;
  167. IOMMUTLBEntry *entry = &iter->entry;
  168. SMMUIOTLBPageInvInfo *info = (SMMUIOTLBPageInvInfo *)user_data;
  169. SMMUIOTLBKey iotlb_key = *(SMMUIOTLBKey *)key;
  170. if (info->asid >= 0 && info->asid != SMMU_IOTLB_ASID(iotlb_key)) {
  171. return false;
  172. }
  173. if (info->vmid >= 0 && info->vmid != SMMU_IOTLB_VMID(iotlb_key)) {
  174. return false;
  175. }
  176. return ((info->iova & ~entry->addr_mask) == entry->iova) ||
  177. ((entry->iova & ~info->mask) == info->iova);
  178. }
  179. static gboolean smmu_hash_remove_by_vmid_ipa(gpointer key, gpointer value,
  180. gpointer user_data)
  181. {
  182. SMMUTLBEntry *iter = (SMMUTLBEntry *)value;
  183. IOMMUTLBEntry *entry = &iter->entry;
  184. SMMUIOTLBPageInvInfo *info = (SMMUIOTLBPageInvInfo *)user_data;
  185. SMMUIOTLBKey iotlb_key = *(SMMUIOTLBKey *)key;
  186. if (SMMU_IOTLB_ASID(iotlb_key) >= 0) {
  187. /* This is a stage-1 address. */
  188. return false;
  189. }
  190. if (info->vmid != SMMU_IOTLB_VMID(iotlb_key)) {
  191. return false;
  192. }
  193. return ((info->iova & ~entry->addr_mask) == entry->iova) ||
  194. ((entry->iova & ~info->mask) == info->iova);
  195. }
  196. static gboolean
  197. smmu_hash_remove_by_sid_range(gpointer key, gpointer value, gpointer user_data)
  198. {
  199. SMMUDevice *sdev = (SMMUDevice *)key;
  200. uint32_t sid = smmu_get_sid(sdev);
  201. SMMUSIDRange *sid_range = (SMMUSIDRange *)user_data;
  202. if (sid < sid_range->start || sid > sid_range->end) {
  203. return false;
  204. }
  205. trace_smmu_config_cache_inv(sid);
  206. return true;
  207. }
  208. void smmu_configs_inv_sid_range(SMMUState *s, SMMUSIDRange sid_range)
  209. {
  210. trace_smmu_configs_inv_sid_range(sid_range.start, sid_range.end);
  211. g_hash_table_foreach_remove(s->configs, smmu_hash_remove_by_sid_range,
  212. &sid_range);
  213. }
  214. void smmu_iotlb_inv_iova(SMMUState *s, int asid, int vmid, dma_addr_t iova,
  215. uint8_t tg, uint64_t num_pages, uint8_t ttl)
  216. {
  217. /* if tg is not set we use 4KB range invalidation */
  218. uint8_t granule = tg ? tg * 2 + 10 : 12;
  219. if (ttl && (num_pages == 1) && (asid >= 0)) {
  220. SMMUIOTLBKey key = smmu_get_iotlb_key(asid, vmid, iova, tg, ttl);
  221. if (g_hash_table_remove(s->iotlb, &key)) {
  222. return;
  223. }
  224. /*
  225. * if the entry is not found, let's see if it does not
  226. * belong to a larger IOTLB entry
  227. */
  228. }
  229. SMMUIOTLBPageInvInfo info = {
  230. .asid = asid, .iova = iova,
  231. .vmid = vmid,
  232. .mask = (num_pages * 1 << granule) - 1};
  233. g_hash_table_foreach_remove(s->iotlb,
  234. smmu_hash_remove_by_asid_vmid_iova,
  235. &info);
  236. }
  237. /*
  238. * Similar to smmu_iotlb_inv_iova(), but for Stage-2, ASID is always -1,
  239. * in Stage-1 invalidation ASID = -1, means don't care.
  240. */
  241. void smmu_iotlb_inv_ipa(SMMUState *s, int vmid, dma_addr_t ipa, uint8_t tg,
  242. uint64_t num_pages, uint8_t ttl)
  243. {
  244. uint8_t granule = tg ? tg * 2 + 10 : 12;
  245. int asid = -1;
  246. if (ttl && (num_pages == 1)) {
  247. SMMUIOTLBKey key = smmu_get_iotlb_key(asid, vmid, ipa, tg, ttl);
  248. if (g_hash_table_remove(s->iotlb, &key)) {
  249. return;
  250. }
  251. }
  252. SMMUIOTLBPageInvInfo info = {
  253. .iova = ipa,
  254. .vmid = vmid,
  255. .mask = (num_pages << granule) - 1};
  256. g_hash_table_foreach_remove(s->iotlb,
  257. smmu_hash_remove_by_vmid_ipa,
  258. &info);
  259. }
  260. void smmu_iotlb_inv_asid_vmid(SMMUState *s, int asid, int vmid)
  261. {
  262. SMMUIOTLBPageInvInfo info = {
  263. .asid = asid,
  264. .vmid = vmid,
  265. };
  266. trace_smmu_iotlb_inv_asid_vmid(asid, vmid);
  267. g_hash_table_foreach_remove(s->iotlb, smmu_hash_remove_by_asid_vmid, &info);
  268. }
  269. void smmu_iotlb_inv_vmid(SMMUState *s, int vmid)
  270. {
  271. trace_smmu_iotlb_inv_vmid(vmid);
  272. g_hash_table_foreach_remove(s->iotlb, smmu_hash_remove_by_vmid, &vmid);
  273. }
  274. inline void smmu_iotlb_inv_vmid_s1(SMMUState *s, int vmid)
  275. {
  276. trace_smmu_iotlb_inv_vmid_s1(vmid);
  277. g_hash_table_foreach_remove(s->iotlb, smmu_hash_remove_by_vmid_s1, &vmid);
  278. }
  279. /* VMSAv8-64 Translation */
  280. /**
  281. * get_pte - Get the content of a page table entry located at
  282. * @base_addr[@index]
  283. */
  284. static int get_pte(dma_addr_t baseaddr, uint32_t index, uint64_t *pte,
  285. SMMUPTWEventInfo *info)
  286. {
  287. int ret;
  288. dma_addr_t addr = baseaddr + index * sizeof(*pte);
  289. /* TODO: guarantee 64-bit single-copy atomicity */
  290. ret = ldq_le_dma(&address_space_memory, addr, pte, MEMTXATTRS_UNSPECIFIED);
  291. if (ret != MEMTX_OK) {
  292. info->type = SMMU_PTW_ERR_WALK_EABT;
  293. info->addr = addr;
  294. return -EINVAL;
  295. }
  296. trace_smmu_get_pte(baseaddr, index, addr, *pte);
  297. return 0;
  298. }
  299. /* VMSAv8-64 Translation Table Format Descriptor Decoding */
  300. /**
  301. * get_page_pte_address - returns the L3 descriptor output address,
  302. * ie. the page frame
  303. * ARM ARM spec: Figure D4-17 VMSAv8-64 level 3 descriptor format
  304. */
  305. static inline hwaddr get_page_pte_address(uint64_t pte, int granule_sz)
  306. {
  307. return PTE_ADDRESS(pte, granule_sz);
  308. }
  309. /**
  310. * get_table_pte_address - return table descriptor output address,
  311. * ie. address of next level table
  312. * ARM ARM Figure D4-16 VMSAv8-64 level0, level1, and level 2 descriptor formats
  313. */
  314. static inline hwaddr get_table_pte_address(uint64_t pte, int granule_sz)
  315. {
  316. return PTE_ADDRESS(pte, granule_sz);
  317. }
  318. /**
  319. * get_block_pte_address - return block descriptor output address and block size
  320. * ARM ARM Figure D4-16 VMSAv8-64 level0, level1, and level 2 descriptor formats
  321. */
  322. static inline hwaddr get_block_pte_address(uint64_t pte, int level,
  323. int granule_sz, uint64_t *bsz)
  324. {
  325. int n = level_shift(level, granule_sz);
  326. *bsz = 1ULL << n;
  327. return PTE_ADDRESS(pte, n);
  328. }
  329. SMMUTransTableInfo *select_tt(SMMUTransCfg *cfg, dma_addr_t iova)
  330. {
  331. bool tbi = extract64(iova, 55, 1) ? TBI1(cfg->tbi) : TBI0(cfg->tbi);
  332. uint8_t tbi_byte = tbi * 8;
  333. if (cfg->tt[0].tsz &&
  334. !extract64(iova, 64 - cfg->tt[0].tsz, cfg->tt[0].tsz - tbi_byte)) {
  335. /* there is a ttbr0 region and we are in it (high bits all zero) */
  336. return &cfg->tt[0];
  337. } else if (cfg->tt[1].tsz &&
  338. sextract64(iova, 64 - cfg->tt[1].tsz, cfg->tt[1].tsz - tbi_byte) == -1) {
  339. /* there is a ttbr1 region and we are in it (high bits all one) */
  340. return &cfg->tt[1];
  341. } else if (!cfg->tt[0].tsz) {
  342. /* ttbr0 region is "everything not in the ttbr1 region" */
  343. return &cfg->tt[0];
  344. } else if (!cfg->tt[1].tsz) {
  345. /* ttbr1 region is "everything not in the ttbr0 region" */
  346. return &cfg->tt[1];
  347. }
  348. /* in the gap between the two regions, this is a Translation fault */
  349. return NULL;
  350. }
  351. /* Translate stage-1 table address using stage-2 page table. */
  352. static inline int translate_table_addr_ipa(SMMUState *bs,
  353. dma_addr_t *table_addr,
  354. SMMUTransCfg *cfg,
  355. SMMUPTWEventInfo *info)
  356. {
  357. dma_addr_t addr = *table_addr;
  358. SMMUTLBEntry *cached_entry;
  359. int asid;
  360. /*
  361. * The translation table walks performed from TTB0 or TTB1 are always
  362. * performed in IPA space if stage 2 translations are enabled.
  363. */
  364. asid = cfg->asid;
  365. cfg->stage = SMMU_STAGE_2;
  366. cfg->asid = -1;
  367. cached_entry = smmu_translate(bs, cfg, addr, IOMMU_RO, info);
  368. cfg->asid = asid;
  369. cfg->stage = SMMU_NESTED;
  370. if (cached_entry) {
  371. *table_addr = CACHED_ENTRY_TO_ADDR(cached_entry, addr);
  372. return 0;
  373. }
  374. info->stage = SMMU_STAGE_2;
  375. info->addr = addr;
  376. info->is_ipa_descriptor = true;
  377. return -EINVAL;
  378. }
  379. /**
  380. * smmu_ptw_64_s1 - VMSAv8-64 Walk of the page tables for a given IOVA
  381. * @bs: smmu state which includes TLB instance
  382. * @cfg: translation config
  383. * @iova: iova to translate
  384. * @perm: access type
  385. * @tlbe: SMMUTLBEntry (out)
  386. * @info: handle to an error info
  387. *
  388. * Return 0 on success, < 0 on error. In case of error, @info is filled
  389. * and tlbe->perm is set to IOMMU_NONE.
  390. * Upon success, @tlbe is filled with translated_addr and entry
  391. * permission rights.
  392. */
  393. static int smmu_ptw_64_s1(SMMUState *bs, SMMUTransCfg *cfg,
  394. dma_addr_t iova, IOMMUAccessFlags perm,
  395. SMMUTLBEntry *tlbe, SMMUPTWEventInfo *info)
  396. {
  397. dma_addr_t baseaddr, indexmask;
  398. SMMUStage stage = cfg->stage;
  399. SMMUTransTableInfo *tt = select_tt(cfg, iova);
  400. uint8_t level, granule_sz, inputsize, stride;
  401. if (!tt || tt->disabled) {
  402. info->type = SMMU_PTW_ERR_TRANSLATION;
  403. goto error;
  404. }
  405. granule_sz = tt->granule_sz;
  406. stride = VMSA_STRIDE(granule_sz);
  407. inputsize = 64 - tt->tsz;
  408. level = 4 - (inputsize - 4) / stride;
  409. indexmask = VMSA_IDXMSK(inputsize, stride, level);
  410. baseaddr = extract64(tt->ttb, 0, cfg->oas);
  411. baseaddr &= ~indexmask;
  412. while (level < VMSA_LEVELS) {
  413. uint64_t subpage_size = 1ULL << level_shift(level, granule_sz);
  414. uint64_t mask = subpage_size - 1;
  415. uint32_t offset = iova_level_offset(iova, inputsize, level, granule_sz);
  416. uint64_t pte, gpa;
  417. dma_addr_t pte_addr = baseaddr + offset * sizeof(pte);
  418. uint8_t ap;
  419. if (get_pte(baseaddr, offset, &pte, info)) {
  420. goto error;
  421. }
  422. trace_smmu_ptw_level(stage, level, iova, subpage_size,
  423. baseaddr, offset, pte);
  424. if (is_invalid_pte(pte) || is_reserved_pte(pte, level)) {
  425. trace_smmu_ptw_invalid_pte(stage, level, baseaddr,
  426. pte_addr, offset, pte);
  427. break;
  428. }
  429. if (is_table_pte(pte, level)) {
  430. ap = PTE_APTABLE(pte);
  431. if (is_permission_fault(ap, perm) && !tt->had) {
  432. info->type = SMMU_PTW_ERR_PERMISSION;
  433. goto error;
  434. }
  435. baseaddr = get_table_pte_address(pte, granule_sz);
  436. if (cfg->stage == SMMU_NESTED) {
  437. if (translate_table_addr_ipa(bs, &baseaddr, cfg, info)) {
  438. goto error;
  439. }
  440. }
  441. level++;
  442. continue;
  443. } else if (is_page_pte(pte, level)) {
  444. gpa = get_page_pte_address(pte, granule_sz);
  445. trace_smmu_ptw_page_pte(stage, level, iova,
  446. baseaddr, pte_addr, pte, gpa);
  447. } else {
  448. uint64_t block_size;
  449. gpa = get_block_pte_address(pte, level, granule_sz,
  450. &block_size);
  451. trace_smmu_ptw_block_pte(stage, level, baseaddr,
  452. pte_addr, pte, iova, gpa,
  453. block_size >> 20);
  454. }
  455. /*
  456. * QEMU does not currently implement HTTU, so if AFFD and PTE.AF
  457. * are 0 we take an Access flag fault. (5.4. Context Descriptor)
  458. * An Access flag fault takes priority over a Permission fault.
  459. */
  460. if (!PTE_AF(pte) && !cfg->affd) {
  461. info->type = SMMU_PTW_ERR_ACCESS;
  462. goto error;
  463. }
  464. ap = PTE_AP(pte);
  465. if (is_permission_fault(ap, perm)) {
  466. info->type = SMMU_PTW_ERR_PERMISSION;
  467. goto error;
  468. }
  469. /*
  470. * The address output from the translation causes a stage 1 Address
  471. * Size fault if it exceeds the range of the effective IPA size for
  472. * the given CD.
  473. */
  474. if (gpa >= (1ULL << cfg->oas)) {
  475. info->type = SMMU_PTW_ERR_ADDR_SIZE;
  476. goto error;
  477. }
  478. tlbe->entry.translated_addr = gpa;
  479. tlbe->entry.iova = iova & ~mask;
  480. tlbe->entry.addr_mask = mask;
  481. tlbe->parent_perm = PTE_AP_TO_PERM(ap);
  482. tlbe->entry.perm = tlbe->parent_perm;
  483. tlbe->level = level;
  484. tlbe->granule = granule_sz;
  485. return 0;
  486. }
  487. info->type = SMMU_PTW_ERR_TRANSLATION;
  488. error:
  489. info->stage = SMMU_STAGE_1;
  490. tlbe->entry.perm = IOMMU_NONE;
  491. return -EINVAL;
  492. }
  493. /**
  494. * smmu_ptw_64_s2 - VMSAv8-64 Walk of the page tables for a given ipa
  495. * for stage-2.
  496. * @cfg: translation config
  497. * @ipa: ipa to translate
  498. * @perm: access type
  499. * @tlbe: SMMUTLBEntry (out)
  500. * @info: handle to an error info
  501. *
  502. * Return 0 on success, < 0 on error. In case of error, @info is filled
  503. * and tlbe->perm is set to IOMMU_NONE.
  504. * Upon success, @tlbe is filled with translated_addr and entry
  505. * permission rights.
  506. */
  507. static int smmu_ptw_64_s2(SMMUTransCfg *cfg,
  508. dma_addr_t ipa, IOMMUAccessFlags perm,
  509. SMMUTLBEntry *tlbe, SMMUPTWEventInfo *info)
  510. {
  511. const SMMUStage stage = SMMU_STAGE_2;
  512. int granule_sz = cfg->s2cfg.granule_sz;
  513. /* ARM DDI0487I.a: Table D8-7. */
  514. int inputsize = 64 - cfg->s2cfg.tsz;
  515. int level = get_start_level(cfg->s2cfg.sl0, granule_sz);
  516. int stride = VMSA_STRIDE(granule_sz);
  517. int idx = pgd_concat_idx(level, granule_sz, ipa);
  518. /*
  519. * Get the ttb from concatenated structure.
  520. * The offset is the idx * size of each ttb(number of ptes * (sizeof(pte))
  521. */
  522. uint64_t baseaddr = extract64(cfg->s2cfg.vttb, 0, cfg->s2cfg.eff_ps) +
  523. (1 << stride) * idx * sizeof(uint64_t);
  524. dma_addr_t indexmask = VMSA_IDXMSK(inputsize, stride, level);
  525. baseaddr &= ~indexmask;
  526. /*
  527. * On input, a stage 2 Translation fault occurs if the IPA is outside the
  528. * range configured by the relevant S2T0SZ field of the STE.
  529. */
  530. if (ipa >= (1ULL << inputsize)) {
  531. info->type = SMMU_PTW_ERR_TRANSLATION;
  532. goto error_ipa;
  533. }
  534. while (level < VMSA_LEVELS) {
  535. uint64_t subpage_size = 1ULL << level_shift(level, granule_sz);
  536. uint64_t mask = subpage_size - 1;
  537. uint32_t offset = iova_level_offset(ipa, inputsize, level, granule_sz);
  538. uint64_t pte, gpa;
  539. dma_addr_t pte_addr = baseaddr + offset * sizeof(pte);
  540. uint8_t s2ap;
  541. if (get_pte(baseaddr, offset, &pte, info)) {
  542. goto error;
  543. }
  544. trace_smmu_ptw_level(stage, level, ipa, subpage_size,
  545. baseaddr, offset, pte);
  546. if (is_invalid_pte(pte) || is_reserved_pte(pte, level)) {
  547. trace_smmu_ptw_invalid_pte(stage, level, baseaddr,
  548. pte_addr, offset, pte);
  549. break;
  550. }
  551. if (is_table_pte(pte, level)) {
  552. baseaddr = get_table_pte_address(pte, granule_sz);
  553. level++;
  554. continue;
  555. } else if (is_page_pte(pte, level)) {
  556. gpa = get_page_pte_address(pte, granule_sz);
  557. trace_smmu_ptw_page_pte(stage, level, ipa,
  558. baseaddr, pte_addr, pte, gpa);
  559. } else {
  560. uint64_t block_size;
  561. gpa = get_block_pte_address(pte, level, granule_sz,
  562. &block_size);
  563. trace_smmu_ptw_block_pte(stage, level, baseaddr,
  564. pte_addr, pte, ipa, gpa,
  565. block_size >> 20);
  566. }
  567. /*
  568. * If S2AFFD and PTE.AF are 0 => fault. (5.2. Stream Table Entry)
  569. * An Access fault takes priority over a Permission fault.
  570. */
  571. if (!PTE_AF(pte) && !cfg->s2cfg.affd) {
  572. info->type = SMMU_PTW_ERR_ACCESS;
  573. goto error_ipa;
  574. }
  575. s2ap = PTE_AP(pte);
  576. if (is_permission_fault_s2(s2ap, perm)) {
  577. info->type = SMMU_PTW_ERR_PERMISSION;
  578. goto error_ipa;
  579. }
  580. /*
  581. * The address output from the translation causes a stage 2 Address
  582. * Size fault if it exceeds the effective PA output range.
  583. */
  584. if (gpa >= (1ULL << cfg->s2cfg.eff_ps)) {
  585. info->type = SMMU_PTW_ERR_ADDR_SIZE;
  586. goto error_ipa;
  587. }
  588. tlbe->entry.translated_addr = gpa;
  589. tlbe->entry.iova = ipa & ~mask;
  590. tlbe->entry.addr_mask = mask;
  591. tlbe->parent_perm = s2ap;
  592. tlbe->entry.perm = tlbe->parent_perm;
  593. tlbe->level = level;
  594. tlbe->granule = granule_sz;
  595. return 0;
  596. }
  597. info->type = SMMU_PTW_ERR_TRANSLATION;
  598. error_ipa:
  599. info->addr = ipa;
  600. error:
  601. info->stage = SMMU_STAGE_2;
  602. tlbe->entry.perm = IOMMU_NONE;
  603. return -EINVAL;
  604. }
  605. /*
  606. * combine S1 and S2 TLB entries into a single entry.
  607. * As a result the S1 entry is overridden with combined data.
  608. */
  609. static void combine_tlb(SMMUTLBEntry *tlbe, SMMUTLBEntry *tlbe_s2,
  610. dma_addr_t iova, SMMUTransCfg *cfg)
  611. {
  612. if (tlbe_s2->entry.addr_mask < tlbe->entry.addr_mask) {
  613. tlbe->entry.addr_mask = tlbe_s2->entry.addr_mask;
  614. tlbe->granule = tlbe_s2->granule;
  615. tlbe->level = tlbe_s2->level;
  616. }
  617. tlbe->entry.translated_addr = CACHED_ENTRY_TO_ADDR(tlbe_s2,
  618. tlbe->entry.translated_addr);
  619. tlbe->entry.iova = iova & ~tlbe->entry.addr_mask;
  620. /* parent_perm has s2 perm while perm keeps s1 perm. */
  621. tlbe->parent_perm = tlbe_s2->entry.perm;
  622. return;
  623. }
  624. /**
  625. * smmu_ptw - Walk the page tables for an IOVA, according to @cfg
  626. *
  627. * @bs: smmu state which includes TLB instance
  628. * @cfg: translation configuration
  629. * @iova: iova to translate
  630. * @perm: tentative access type
  631. * @tlbe: returned entry
  632. * @info: ptw event handle
  633. *
  634. * return 0 on success
  635. */
  636. int smmu_ptw(SMMUState *bs, SMMUTransCfg *cfg, dma_addr_t iova,
  637. IOMMUAccessFlags perm, SMMUTLBEntry *tlbe, SMMUPTWEventInfo *info)
  638. {
  639. int ret;
  640. SMMUTLBEntry tlbe_s2;
  641. dma_addr_t ipa;
  642. if (cfg->stage == SMMU_STAGE_1) {
  643. return smmu_ptw_64_s1(bs, cfg, iova, perm, tlbe, info);
  644. } else if (cfg->stage == SMMU_STAGE_2) {
  645. /*
  646. * If bypassing stage 1(or unimplemented), the input address is passed
  647. * directly to stage 2 as IPA. If the input address of a transaction
  648. * exceeds the size of the IAS, a stage 1 Address Size fault occurs.
  649. * For AA64, IAS = OAS according to (IHI 0070.E.a) "3.4 Address sizes"
  650. */
  651. if (iova >= (1ULL << cfg->oas)) {
  652. info->type = SMMU_PTW_ERR_ADDR_SIZE;
  653. info->stage = SMMU_STAGE_1;
  654. tlbe->entry.perm = IOMMU_NONE;
  655. return -EINVAL;
  656. }
  657. return smmu_ptw_64_s2(cfg, iova, perm, tlbe, info);
  658. }
  659. /* SMMU_NESTED. */
  660. ret = smmu_ptw_64_s1(bs, cfg, iova, perm, tlbe, info);
  661. if (ret) {
  662. return ret;
  663. }
  664. ipa = CACHED_ENTRY_TO_ADDR(tlbe, iova);
  665. ret = smmu_ptw_64_s2(cfg, ipa, perm, &tlbe_s2, info);
  666. if (ret) {
  667. return ret;
  668. }
  669. combine_tlb(tlbe, &tlbe_s2, iova, cfg);
  670. return 0;
  671. }
  672. SMMUTLBEntry *smmu_translate(SMMUState *bs, SMMUTransCfg *cfg, dma_addr_t addr,
  673. IOMMUAccessFlags flag, SMMUPTWEventInfo *info)
  674. {
  675. SMMUTLBEntry *cached_entry = NULL;
  676. SMMUTransTableInfo *tt;
  677. int status;
  678. /*
  679. * Combined attributes used for TLB lookup, holds the attributes for
  680. * the input stage.
  681. */
  682. SMMUTransTableInfo tt_combined;
  683. if (cfg->stage == SMMU_STAGE_2) {
  684. /* Stage2. */
  685. tt_combined.granule_sz = cfg->s2cfg.granule_sz;
  686. tt_combined.tsz = cfg->s2cfg.tsz;
  687. } else {
  688. /* Select stage1 translation table. */
  689. tt = select_tt(cfg, addr);
  690. if (!tt) {
  691. info->type = SMMU_PTW_ERR_TRANSLATION;
  692. info->stage = SMMU_STAGE_1;
  693. return NULL;
  694. }
  695. tt_combined.granule_sz = tt->granule_sz;
  696. tt_combined.tsz = tt->tsz;
  697. }
  698. cached_entry = smmu_iotlb_lookup(bs, cfg, &tt_combined, addr);
  699. if (cached_entry) {
  700. if ((flag & IOMMU_WO) && !(cached_entry->entry.perm &
  701. cached_entry->parent_perm & IOMMU_WO)) {
  702. info->type = SMMU_PTW_ERR_PERMISSION;
  703. info->stage = !(cached_entry->entry.perm & IOMMU_WO) ?
  704. SMMU_STAGE_1 :
  705. SMMU_STAGE_2;
  706. return NULL;
  707. }
  708. return cached_entry;
  709. }
  710. cached_entry = g_new0(SMMUTLBEntry, 1);
  711. status = smmu_ptw(bs, cfg, addr, flag, cached_entry, info);
  712. if (status) {
  713. g_free(cached_entry);
  714. return NULL;
  715. }
  716. smmu_iotlb_insert(bs, cfg, cached_entry);
  717. return cached_entry;
  718. }
  719. /**
  720. * The bus number is used for lookup when SID based invalidation occurs.
  721. * In that case we lazily populate the SMMUPciBus array from the bus hash
  722. * table. At the time the SMMUPciBus is created (smmu_find_add_as), the bus
  723. * numbers may not be always initialized yet.
  724. */
  725. SMMUPciBus *smmu_find_smmu_pcibus(SMMUState *s, uint8_t bus_num)
  726. {
  727. SMMUPciBus *smmu_pci_bus = s->smmu_pcibus_by_bus_num[bus_num];
  728. GHashTableIter iter;
  729. if (smmu_pci_bus) {
  730. return smmu_pci_bus;
  731. }
  732. g_hash_table_iter_init(&iter, s->smmu_pcibus_by_busptr);
  733. while (g_hash_table_iter_next(&iter, NULL, (void **)&smmu_pci_bus)) {
  734. if (pci_bus_num(smmu_pci_bus->bus) == bus_num) {
  735. s->smmu_pcibus_by_bus_num[bus_num] = smmu_pci_bus;
  736. return smmu_pci_bus;
  737. }
  738. }
  739. return NULL;
  740. }
  741. static AddressSpace *smmu_find_add_as(PCIBus *bus, void *opaque, int devfn)
  742. {
  743. SMMUState *s = opaque;
  744. SMMUPciBus *sbus = g_hash_table_lookup(s->smmu_pcibus_by_busptr, bus);
  745. SMMUDevice *sdev;
  746. static unsigned int index;
  747. if (!sbus) {
  748. sbus = g_malloc0(sizeof(SMMUPciBus) +
  749. sizeof(SMMUDevice *) * SMMU_PCI_DEVFN_MAX);
  750. sbus->bus = bus;
  751. g_hash_table_insert(s->smmu_pcibus_by_busptr, bus, sbus);
  752. }
  753. sdev = sbus->pbdev[devfn];
  754. if (!sdev) {
  755. char *name = g_strdup_printf("%s-%d-%d", s->mrtypename, devfn, index++);
  756. sdev = sbus->pbdev[devfn] = g_new0(SMMUDevice, 1);
  757. sdev->smmu = s;
  758. sdev->bus = bus;
  759. sdev->devfn = devfn;
  760. memory_region_init_iommu(&sdev->iommu, sizeof(sdev->iommu),
  761. s->mrtypename,
  762. OBJECT(s), name, UINT64_MAX);
  763. address_space_init(&sdev->as,
  764. MEMORY_REGION(&sdev->iommu), name);
  765. trace_smmu_add_mr(name);
  766. g_free(name);
  767. }
  768. return &sdev->as;
  769. }
  770. static const PCIIOMMUOps smmu_ops = {
  771. .get_address_space = smmu_find_add_as,
  772. };
  773. SMMUDevice *smmu_find_sdev(SMMUState *s, uint32_t sid)
  774. {
  775. uint8_t bus_n, devfn;
  776. SMMUPciBus *smmu_bus;
  777. bus_n = PCI_BUS_NUM(sid);
  778. smmu_bus = smmu_find_smmu_pcibus(s, bus_n);
  779. if (smmu_bus) {
  780. devfn = SMMU_PCI_DEVFN(sid);
  781. return smmu_bus->pbdev[devfn];
  782. }
  783. return NULL;
  784. }
  785. /* Unmap all notifiers attached to @mr */
  786. static void smmu_inv_notifiers_mr(IOMMUMemoryRegion *mr)
  787. {
  788. IOMMUNotifier *n;
  789. trace_smmu_inv_notifiers_mr(mr->parent_obj.name);
  790. IOMMU_NOTIFIER_FOREACH(n, mr) {
  791. memory_region_unmap_iommu_notifier_range(n);
  792. }
  793. }
  794. /* Unmap all notifiers of all mr's */
  795. void smmu_inv_notifiers_all(SMMUState *s)
  796. {
  797. SMMUDevice *sdev;
  798. QLIST_FOREACH(sdev, &s->devices_with_notifiers, next) {
  799. smmu_inv_notifiers_mr(&sdev->iommu);
  800. }
  801. }
  802. static void smmu_base_realize(DeviceState *dev, Error **errp)
  803. {
  804. SMMUState *s = ARM_SMMU(dev);
  805. SMMUBaseClass *sbc = ARM_SMMU_GET_CLASS(dev);
  806. Error *local_err = NULL;
  807. sbc->parent_realize(dev, &local_err);
  808. if (local_err) {
  809. error_propagate(errp, local_err);
  810. return;
  811. }
  812. s->configs = g_hash_table_new_full(NULL, NULL, NULL, g_free);
  813. s->iotlb = g_hash_table_new_full(smmu_iotlb_key_hash, smmu_iotlb_key_equal,
  814. g_free, g_free);
  815. s->smmu_pcibus_by_busptr = g_hash_table_new(NULL, NULL);
  816. if (s->primary_bus) {
  817. pci_setup_iommu(s->primary_bus, &smmu_ops, s);
  818. } else {
  819. error_setg(errp, "SMMU is not attached to any PCI bus!");
  820. }
  821. }
  822. /*
  823. * Make sure the IOMMU is reset in 'exit' phase after
  824. * all outstanding DMA requests have been quiesced during
  825. * the 'enter' or 'hold' reset phases
  826. */
  827. static void smmu_base_reset_exit(Object *obj, ResetType type)
  828. {
  829. SMMUState *s = ARM_SMMU(obj);
  830. memset(s->smmu_pcibus_by_bus_num, 0, sizeof(s->smmu_pcibus_by_bus_num));
  831. g_hash_table_remove_all(s->configs);
  832. g_hash_table_remove_all(s->iotlb);
  833. }
  834. static const Property smmu_dev_properties[] = {
  835. DEFINE_PROP_UINT8("bus_num", SMMUState, bus_num, 0),
  836. DEFINE_PROP_LINK("primary-bus", SMMUState, primary_bus,
  837. TYPE_PCI_BUS, PCIBus *),
  838. };
  839. static void smmu_base_class_init(ObjectClass *klass, void *data)
  840. {
  841. DeviceClass *dc = DEVICE_CLASS(klass);
  842. ResettableClass *rc = RESETTABLE_CLASS(klass);
  843. SMMUBaseClass *sbc = ARM_SMMU_CLASS(klass);
  844. device_class_set_props(dc, smmu_dev_properties);
  845. device_class_set_parent_realize(dc, smmu_base_realize,
  846. &sbc->parent_realize);
  847. rc->phases.exit = smmu_base_reset_exit;
  848. }
  849. static const TypeInfo smmu_base_info = {
  850. .name = TYPE_ARM_SMMU,
  851. .parent = TYPE_SYS_BUS_DEVICE,
  852. .instance_size = sizeof(SMMUState),
  853. .class_data = NULL,
  854. .class_size = sizeof(SMMUBaseClass),
  855. .class_init = smmu_base_class_init,
  856. .abstract = true,
  857. };
  858. static void smmu_base_register_types(void)
  859. {
  860. type_register_static(&smmu_base_info);
  861. }
  862. type_init(smmu_base_register_types)