msm_iommu.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2013 Red Hat
  4. * Author: Rob Clark <robdclark@gmail.com>
  5. */
  6. #include <linux/adreno-smmu-priv.h>
  7. #include <linux/io-pgtable.h>
  8. #include <linux/kmemleak.h>
  9. #include "msm_drv.h"
  10. #include "msm_gpu_trace.h"
  11. #include "msm_mmu.h"
  12. struct msm_iommu {
  13. struct msm_mmu base;
  14. struct iommu_domain *domain;
  15. struct mutex init_lock; /* protects pagetables counter and prr_page */
  16. int pagetables;
  17. struct page *prr_page;
  18. struct kmem_cache *pt_cache;
  19. };
  20. #define to_msm_iommu(x) container_of(x, struct msm_iommu, base)
  21. struct msm_iommu_pagetable {
  22. struct msm_mmu base;
  23. struct msm_mmu *parent;
  24. struct io_pgtable_ops *pgtbl_ops;
  25. const struct iommu_flush_ops *tlb;
  26. struct device *iommu_dev;
  27. unsigned long pgsize_bitmap; /* Bitmap of page sizes in use */
  28. phys_addr_t ttbr;
  29. u32 asid;
  30. /** @root_page_table: Stores the root page table pointer. */
  31. void *root_page_table;
  32. };
  33. static struct msm_iommu_pagetable *to_pagetable(struct msm_mmu *mmu)
  34. {
  35. return container_of(mmu, struct msm_iommu_pagetable, base);
  36. }
  37. /* based on iommu_pgsize() in iommu.c: */
  38. static size_t calc_pgsize(struct msm_iommu_pagetable *pagetable,
  39. unsigned long iova, phys_addr_t paddr,
  40. size_t size, size_t *count)
  41. {
  42. unsigned int pgsize_idx, pgsize_idx_next;
  43. unsigned long pgsizes;
  44. size_t offset, pgsize, pgsize_next;
  45. unsigned long addr_merge = paddr | iova;
  46. /* Page sizes supported by the hardware and small enough for @size */
  47. pgsizes = pagetable->pgsize_bitmap & GENMASK(__fls(size), 0);
  48. /* Constrain the page sizes further based on the maximum alignment */
  49. if (likely(addr_merge))
  50. pgsizes &= GENMASK(__ffs(addr_merge), 0);
  51. /* Make sure we have at least one suitable page size */
  52. BUG_ON(!pgsizes);
  53. /* Pick the biggest page size remaining */
  54. pgsize_idx = __fls(pgsizes);
  55. pgsize = BIT(pgsize_idx);
  56. if (!count)
  57. return pgsize;
  58. /* Find the next biggest support page size, if it exists */
  59. pgsizes = pagetable->pgsize_bitmap & ~GENMASK(pgsize_idx, 0);
  60. if (!pgsizes)
  61. goto out_set_count;
  62. pgsize_idx_next = __ffs(pgsizes);
  63. pgsize_next = BIT(pgsize_idx_next);
  64. /*
  65. * There's no point trying a bigger page size unless the virtual
  66. * and physical addresses are similarly offset within the larger page.
  67. */
  68. if ((iova ^ paddr) & (pgsize_next - 1))
  69. goto out_set_count;
  70. /* Calculate the offset to the next page size alignment boundary */
  71. offset = pgsize_next - (addr_merge & (pgsize_next - 1));
  72. /*
  73. * If size is big enough to accommodate the larger page, reduce
  74. * the number of smaller pages.
  75. */
  76. if (offset + pgsize_next <= size)
  77. size = offset;
  78. out_set_count:
  79. *count = size >> pgsize_idx;
  80. return pgsize;
  81. }
  82. static int msm_iommu_pagetable_unmap(struct msm_mmu *mmu, u64 iova,
  83. size_t size)
  84. {
  85. struct msm_iommu_pagetable *pagetable = to_pagetable(mmu);
  86. struct io_pgtable_ops *ops = pagetable->pgtbl_ops;
  87. int ret = 0;
  88. while (size) {
  89. size_t pgsize, count;
  90. ssize_t unmapped;
  91. pgsize = calc_pgsize(pagetable, iova, iova, size, &count);
  92. unmapped = ops->unmap_pages(ops, iova, pgsize, count, NULL);
  93. if (unmapped <= 0) {
  94. ret = -EINVAL;
  95. /*
  96. * Continue attempting to unamp the remained of the
  97. * range, so we don't end up with some dangling
  98. * mapped pages
  99. */
  100. unmapped = PAGE_SIZE;
  101. }
  102. iova += unmapped;
  103. size -= unmapped;
  104. }
  105. iommu_flush_iotlb_all(to_msm_iommu(pagetable->parent)->domain);
  106. return ret;
  107. }
  108. static int msm_iommu_pagetable_map_prr(struct msm_mmu *mmu, u64 iova, size_t len, int prot)
  109. {
  110. struct msm_iommu_pagetable *pagetable = to_pagetable(mmu);
  111. struct io_pgtable_ops *ops = pagetable->pgtbl_ops;
  112. struct msm_iommu *iommu = to_msm_iommu(pagetable->parent);
  113. phys_addr_t phys = page_to_phys(iommu->prr_page);
  114. u64 addr = iova;
  115. while (len) {
  116. size_t mapped = 0;
  117. size_t size = PAGE_SIZE;
  118. int ret;
  119. ret = ops->map_pages(ops, addr, phys, size, 1, prot, GFP_KERNEL, &mapped);
  120. /* map_pages could fail after mapping some of the pages,
  121. * so update the counters before error handling.
  122. */
  123. addr += mapped;
  124. len -= mapped;
  125. if (ret) {
  126. msm_iommu_pagetable_unmap(mmu, iova, addr - iova);
  127. return -EINVAL;
  128. }
  129. }
  130. return 0;
  131. }
  132. static int msm_iommu_pagetable_map(struct msm_mmu *mmu, u64 iova,
  133. struct sg_table *sgt, size_t off, size_t len,
  134. int prot)
  135. {
  136. struct msm_iommu_pagetable *pagetable = to_pagetable(mmu);
  137. struct io_pgtable_ops *ops = pagetable->pgtbl_ops;
  138. struct scatterlist *sg;
  139. u64 addr = iova;
  140. unsigned int i;
  141. if (!sgt)
  142. return msm_iommu_pagetable_map_prr(mmu, iova, len, prot);
  143. for_each_sgtable_sg(sgt, sg, i) {
  144. size_t size = sg->length;
  145. phys_addr_t phys = sg_phys(sg);
  146. if (!len)
  147. break;
  148. if (size <= off) {
  149. off -= size;
  150. continue;
  151. }
  152. phys += off;
  153. size -= off;
  154. size = min_t(size_t, size, len);
  155. off = 0;
  156. while (size) {
  157. size_t pgsize, count, mapped = 0;
  158. int ret;
  159. pgsize = calc_pgsize(pagetable, addr, phys, size, &count);
  160. ret = ops->map_pages(ops, addr, phys, pgsize, count,
  161. prot, GFP_KERNEL, &mapped);
  162. /* map_pages could fail after mapping some of the pages,
  163. * so update the counters before error handling.
  164. */
  165. phys += mapped;
  166. addr += mapped;
  167. size -= mapped;
  168. len -= mapped;
  169. if (ret) {
  170. msm_iommu_pagetable_unmap(mmu, iova, addr - iova);
  171. return -EINVAL;
  172. }
  173. }
  174. }
  175. return 0;
  176. }
  177. static void msm_iommu_pagetable_destroy(struct msm_mmu *mmu)
  178. {
  179. struct msm_iommu_pagetable *pagetable = to_pagetable(mmu);
  180. struct msm_iommu *iommu = to_msm_iommu(pagetable->parent);
  181. struct adreno_smmu_priv *adreno_smmu =
  182. dev_get_drvdata(pagetable->parent->dev);
  183. /*
  184. * If this is the last attached pagetable for the parent,
  185. * disable TTBR0 in the arm-smmu driver
  186. */
  187. mutex_lock(&iommu->init_lock);
  188. if (--iommu->pagetables == 0) {
  189. adreno_smmu->set_ttbr0_cfg(adreno_smmu->cookie, NULL);
  190. if (adreno_smmu->set_prr_bit) {
  191. adreno_smmu->set_prr_bit(adreno_smmu->cookie, false);
  192. __free_page(iommu->prr_page);
  193. iommu->prr_page = NULL;
  194. }
  195. }
  196. mutex_unlock(&iommu->init_lock);
  197. free_io_pgtable_ops(pagetable->pgtbl_ops);
  198. kfree(pagetable);
  199. }
  200. int msm_iommu_pagetable_params(struct msm_mmu *mmu,
  201. phys_addr_t *ttbr, int *asid)
  202. {
  203. struct msm_iommu_pagetable *pagetable;
  204. if (mmu->type != MSM_MMU_IOMMU_PAGETABLE)
  205. return -EINVAL;
  206. pagetable = to_pagetable(mmu);
  207. if (ttbr)
  208. *ttbr = pagetable->ttbr;
  209. if (asid)
  210. *asid = pagetable->asid;
  211. return 0;
  212. }
  213. struct iommu_domain_geometry *msm_iommu_get_geometry(struct msm_mmu *mmu)
  214. {
  215. struct msm_iommu *iommu = to_msm_iommu(mmu);
  216. return &iommu->domain->geometry;
  217. }
  218. int
  219. msm_iommu_pagetable_walk(struct msm_mmu *mmu, unsigned long iova, uint64_t ptes[4])
  220. {
  221. struct msm_iommu_pagetable *pagetable;
  222. struct arm_lpae_io_pgtable_walk_data wd = {};
  223. if (mmu->type != MSM_MMU_IOMMU_PAGETABLE)
  224. return -EINVAL;
  225. pagetable = to_pagetable(mmu);
  226. if (!pagetable->pgtbl_ops->pgtable_walk)
  227. return -EINVAL;
  228. pagetable->pgtbl_ops->pgtable_walk(pagetable->pgtbl_ops, iova, &wd);
  229. for (int i = 0; i < ARRAY_SIZE(wd.ptes); i++)
  230. ptes[i] = wd.ptes[i];
  231. return 0;
  232. }
  233. static void
  234. msm_iommu_pagetable_prealloc_count(struct msm_mmu *mmu, struct msm_mmu_prealloc *p,
  235. uint64_t iova, size_t len)
  236. {
  237. u64 pt_count;
  238. /*
  239. * L1, L2 and L3 page tables.
  240. *
  241. * We could optimize L3 allocation by iterating over the sgt and merging
  242. * 2M contiguous blocks, but it's simpler to over-provision and return
  243. * the pages if they're not used.
  244. *
  245. * The first level descriptor (v8 / v7-lpae page table format) encodes
  246. * 30 bits of address. The second level encodes 29. For the 3rd it is
  247. * 39.
  248. *
  249. * https://developer.arm.com/documentation/ddi0406/c/System-Level-Architecture/Virtual-Memory-System-Architecture--VMSA-/Long-descriptor-translation-table-format/Long-descriptor-translation-table-format-descriptors?lang=en#BEIHEFFB
  250. */
  251. pt_count = ((ALIGN(iova + len, 1ull << 39) - ALIGN_DOWN(iova, 1ull << 39)) >> 39) +
  252. ((ALIGN(iova + len, 1ull << 30) - ALIGN_DOWN(iova, 1ull << 30)) >> 30) +
  253. ((ALIGN(iova + len, 1ull << 21) - ALIGN_DOWN(iova, 1ull << 21)) >> 21);
  254. p->count += pt_count;
  255. }
  256. static struct kmem_cache *
  257. get_pt_cache(struct msm_mmu *mmu)
  258. {
  259. struct msm_iommu_pagetable *pagetable = to_pagetable(mmu);
  260. return to_msm_iommu(pagetable->parent)->pt_cache;
  261. }
  262. static int
  263. msm_iommu_pagetable_prealloc_allocate(struct msm_mmu *mmu, struct msm_mmu_prealloc *p)
  264. {
  265. struct kmem_cache *pt_cache = get_pt_cache(mmu);
  266. int ret;
  267. p->pages = kvmalloc_objs(*p->pages, p->count);
  268. if (!p->pages)
  269. return -ENOMEM;
  270. ret = kmem_cache_alloc_bulk(pt_cache, GFP_KERNEL, p->count, p->pages);
  271. if (ret != p->count) {
  272. kfree(p->pages);
  273. p->pages = NULL;
  274. p->count = ret;
  275. return -ENOMEM;
  276. }
  277. return 0;
  278. }
  279. static void
  280. msm_iommu_pagetable_prealloc_cleanup(struct msm_mmu *mmu, struct msm_mmu_prealloc *p)
  281. {
  282. struct kmem_cache *pt_cache = get_pt_cache(mmu);
  283. uint32_t remaining_pt_count = p->count - p->ptr;
  284. if (!p->pages)
  285. return;
  286. if (p->count > 0)
  287. trace_msm_mmu_prealloc_cleanup(p->count, remaining_pt_count);
  288. kmem_cache_free_bulk(pt_cache, remaining_pt_count, &p->pages[p->ptr]);
  289. kvfree(p->pages);
  290. }
  291. /**
  292. * msm_iommu_pagetable_alloc_pt() - Custom page table allocator
  293. * @cookie: Cookie passed at page table allocation time.
  294. * @size: Size of the page table. This size should be fixed,
  295. * and determined at creation time based on the granule size.
  296. * @gfp: GFP flags.
  297. *
  298. * We want a custom allocator so we can use a cache for page table
  299. * allocations and amortize the cost of the over-reservation that's
  300. * done to allow asynchronous VM operations.
  301. *
  302. * Return: non-NULL on success, NULL if the allocation failed for any
  303. * reason.
  304. */
  305. static void *
  306. msm_iommu_pagetable_alloc_pt(void *cookie, size_t size, gfp_t gfp)
  307. {
  308. struct msm_iommu_pagetable *pagetable = cookie;
  309. struct msm_mmu_prealloc *p = pagetable->base.prealloc;
  310. void *page;
  311. /* Allocation of the root page table happening during init. */
  312. if (unlikely(!pagetable->root_page_table)) {
  313. struct page *p;
  314. p = alloc_pages_node(dev_to_node(pagetable->iommu_dev),
  315. gfp | __GFP_ZERO, get_order(size));
  316. page = p ? page_address(p) : NULL;
  317. pagetable->root_page_table = page;
  318. return page;
  319. }
  320. if (WARN_ON(!p) || WARN_ON(p->ptr >= p->count))
  321. return NULL;
  322. page = p->pages[p->ptr++];
  323. memset(page, 0, size);
  324. /*
  325. * Page table entries don't use virtual addresses, which trips out
  326. * kmemleak. kmemleak_alloc_phys() might work, but physical addresses
  327. * are mixed with other fields, and I fear kmemleak won't detect that
  328. * either.
  329. *
  330. * Let's just ignore memory passed to the page-table driver for now.
  331. */
  332. kmemleak_ignore(page);
  333. return page;
  334. }
  335. /**
  336. * msm_iommu_pagetable_free_pt() - Custom page table free function
  337. * @cookie: Cookie passed at page table allocation time.
  338. * @data: Page table to free.
  339. * @size: Size of the page table. This size should be fixed,
  340. * and determined at creation time based on the granule size.
  341. */
  342. static void
  343. msm_iommu_pagetable_free_pt(void *cookie, void *data, size_t size)
  344. {
  345. struct msm_iommu_pagetable *pagetable = cookie;
  346. if (unlikely(pagetable->root_page_table == data)) {
  347. free_pages((unsigned long)data, get_order(size));
  348. pagetable->root_page_table = NULL;
  349. return;
  350. }
  351. kmem_cache_free(get_pt_cache(&pagetable->base), data);
  352. }
  353. static const struct msm_mmu_funcs pagetable_funcs = {
  354. .prealloc_count = msm_iommu_pagetable_prealloc_count,
  355. .prealloc_allocate = msm_iommu_pagetable_prealloc_allocate,
  356. .prealloc_cleanup = msm_iommu_pagetable_prealloc_cleanup,
  357. .map = msm_iommu_pagetable_map,
  358. .unmap = msm_iommu_pagetable_unmap,
  359. .destroy = msm_iommu_pagetable_destroy,
  360. };
  361. static void msm_iommu_tlb_flush_all(void *cookie)
  362. {
  363. struct msm_iommu_pagetable *pagetable = cookie;
  364. struct adreno_smmu_priv *adreno_smmu;
  365. if (!pm_runtime_get_if_in_use(pagetable->iommu_dev))
  366. return;
  367. adreno_smmu = dev_get_drvdata(pagetable->parent->dev);
  368. pagetable->tlb->tlb_flush_all((void *)adreno_smmu->cookie);
  369. pm_runtime_put_autosuspend(pagetable->iommu_dev);
  370. }
  371. static void msm_iommu_tlb_flush_walk(unsigned long iova, size_t size,
  372. size_t granule, void *cookie)
  373. {
  374. struct msm_iommu_pagetable *pagetable = cookie;
  375. struct adreno_smmu_priv *adreno_smmu;
  376. if (!pm_runtime_get_if_in_use(pagetable->iommu_dev))
  377. return;
  378. adreno_smmu = dev_get_drvdata(pagetable->parent->dev);
  379. pagetable->tlb->tlb_flush_walk(iova, size, granule, (void *)adreno_smmu->cookie);
  380. pm_runtime_put_autosuspend(pagetable->iommu_dev);
  381. }
  382. static void msm_iommu_tlb_add_page(struct iommu_iotlb_gather *gather,
  383. unsigned long iova, size_t granule, void *cookie)
  384. {
  385. }
  386. static const struct iommu_flush_ops tlb_ops = {
  387. .tlb_flush_all = msm_iommu_tlb_flush_all,
  388. .tlb_flush_walk = msm_iommu_tlb_flush_walk,
  389. .tlb_add_page = msm_iommu_tlb_add_page,
  390. };
  391. static int msm_gpu_fault_handler(struct iommu_domain *domain, struct device *dev,
  392. unsigned long iova, int flags, void *arg);
  393. static size_t get_tblsz(const struct io_pgtable_cfg *cfg)
  394. {
  395. int pg_shift, bits_per_level;
  396. pg_shift = __ffs(cfg->pgsize_bitmap);
  397. /* arm_lpae_iopte is u64: */
  398. bits_per_level = pg_shift - ilog2(sizeof(u64));
  399. return sizeof(u64) << bits_per_level;
  400. }
  401. struct msm_mmu *msm_iommu_pagetable_create(struct msm_mmu *parent, bool kernel_managed)
  402. {
  403. struct adreno_smmu_priv *adreno_smmu = dev_get_drvdata(parent->dev);
  404. struct msm_iommu *iommu = to_msm_iommu(parent);
  405. struct msm_iommu_pagetable *pagetable;
  406. const struct io_pgtable_cfg *ttbr1_cfg = NULL;
  407. struct io_pgtable_cfg ttbr0_cfg;
  408. int ret;
  409. /* Get the pagetable configuration from the domain */
  410. if (adreno_smmu->cookie)
  411. ttbr1_cfg = adreno_smmu->get_ttbr1_cfg(adreno_smmu->cookie);
  412. /*
  413. * If you hit this WARN_ONCE() you are probably missing an entry in
  414. * qcom_smmu_impl_of_match[] in arm-smmu-qcom.c
  415. */
  416. if (WARN_ONCE(!ttbr1_cfg, "No per-process page tables"))
  417. return ERR_PTR(-ENODEV);
  418. pagetable = kzalloc_obj(*pagetable);
  419. if (!pagetable)
  420. return ERR_PTR(-ENOMEM);
  421. msm_mmu_init(&pagetable->base, parent->dev, &pagetable_funcs,
  422. MSM_MMU_IOMMU_PAGETABLE);
  423. /* Clone the TTBR1 cfg as starting point for TTBR0 cfg: */
  424. ttbr0_cfg = *ttbr1_cfg;
  425. /* The incoming cfg will have the TTBR1 quirk enabled */
  426. ttbr0_cfg.quirks &= ~IO_PGTABLE_QUIRK_ARM_TTBR1;
  427. ttbr0_cfg.tlb = &tlb_ops;
  428. if (!kernel_managed) {
  429. ttbr0_cfg.quirks |= IO_PGTABLE_QUIRK_NO_WARN;
  430. /*
  431. * With userspace managed VM (aka VM_BIND), we need to pre-
  432. * allocate pages ahead of time for map/unmap operations,
  433. * handing them to io-pgtable via custom alloc/free ops as
  434. * needed:
  435. */
  436. ttbr0_cfg.alloc = msm_iommu_pagetable_alloc_pt;
  437. ttbr0_cfg.free = msm_iommu_pagetable_free_pt;
  438. /*
  439. * Restrict to single page granules. Otherwise we may run
  440. * into a situation where userspace wants to unmap/remap
  441. * only a part of a larger block mapping, which is not
  442. * possible without unmapping the entire block. Which in
  443. * turn could cause faults if the GPU is accessing other
  444. * parts of the block mapping.
  445. *
  446. * Note that prior to commit 33729a5fc0ca ("iommu/io-pgtable-arm:
  447. * Remove split on unmap behavior)" this was handled in
  448. * io-pgtable-arm. But this apparently does not work
  449. * correctly on SMMUv3.
  450. */
  451. WARN_ON(!(ttbr0_cfg.pgsize_bitmap & PAGE_SIZE));
  452. ttbr0_cfg.pgsize_bitmap = PAGE_SIZE;
  453. }
  454. pagetable->iommu_dev = ttbr1_cfg->iommu_dev;
  455. pagetable->pgtbl_ops = alloc_io_pgtable_ops(ARM_64_LPAE_S1,
  456. &ttbr0_cfg, pagetable);
  457. if (!pagetable->pgtbl_ops) {
  458. kfree(pagetable);
  459. return ERR_PTR(-ENOMEM);
  460. }
  461. /*
  462. * If this is the first pagetable that we've allocated, send it back to
  463. * the arm-smmu driver as a trigger to set up TTBR0
  464. */
  465. mutex_lock(&iommu->init_lock);
  466. if (iommu->pagetables++ == 0) {
  467. ret = adreno_smmu->set_ttbr0_cfg(adreno_smmu->cookie, &ttbr0_cfg);
  468. if (ret) {
  469. iommu->pagetables--;
  470. mutex_unlock(&iommu->init_lock);
  471. free_io_pgtable_ops(pagetable->pgtbl_ops);
  472. kfree(pagetable);
  473. return ERR_PTR(ret);
  474. }
  475. BUG_ON(iommu->prr_page);
  476. if (adreno_smmu->set_prr_bit) {
  477. /*
  478. * We need a zero'd page for two reasons:
  479. *
  480. * 1) Reserve a known physical address to use when
  481. * mapping NULL / sparsely resident regions
  482. * 2) Read back zero
  483. *
  484. * It appears the hw drops writes to the PRR region
  485. * on the floor, but reads actually return whatever
  486. * is in the PRR page.
  487. */
  488. iommu->prr_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  489. adreno_smmu->set_prr_addr(adreno_smmu->cookie,
  490. page_to_phys(iommu->prr_page));
  491. adreno_smmu->set_prr_bit(adreno_smmu->cookie, true);
  492. }
  493. }
  494. mutex_unlock(&iommu->init_lock);
  495. /* Needed later for TLB flush */
  496. pagetable->parent = parent;
  497. pagetable->tlb = ttbr1_cfg->tlb;
  498. pagetable->pgsize_bitmap = ttbr0_cfg.pgsize_bitmap;
  499. pagetable->ttbr = ttbr0_cfg.arm_lpae_s1_cfg.ttbr;
  500. /*
  501. * TODO we would like each set of page tables to have a unique ASID
  502. * to optimize TLB invalidation. But iommu_flush_iotlb_all() will
  503. * end up flushing the ASID used for TTBR1 pagetables, which is not
  504. * what we want. So for now just use the same ASID as TTBR1.
  505. */
  506. pagetable->asid = 0;
  507. return &pagetable->base;
  508. }
  509. static int msm_gpu_fault_handler(struct iommu_domain *domain, struct device *dev,
  510. unsigned long iova, int flags, void *arg)
  511. {
  512. struct msm_iommu *iommu = arg;
  513. struct adreno_smmu_priv *adreno_smmu = dev_get_drvdata(iommu->base.dev);
  514. struct adreno_smmu_fault_info info, *ptr = NULL;
  515. if (adreno_smmu->get_fault_info) {
  516. adreno_smmu->get_fault_info(adreno_smmu->cookie, &info);
  517. ptr = &info;
  518. }
  519. if (iommu->base.handler)
  520. return iommu->base.handler(iommu->base.arg, iova, flags, ptr);
  521. pr_warn_ratelimited("*** fault: iova=%16lx, flags=%d\n", iova, flags);
  522. return 0;
  523. }
  524. static int msm_disp_fault_handler(struct iommu_domain *domain, struct device *dev,
  525. unsigned long iova, int flags, void *arg)
  526. {
  527. struct msm_iommu *iommu = arg;
  528. if (iommu->base.handler)
  529. return iommu->base.handler(iommu->base.arg, iova, flags, NULL);
  530. return -ENOSYS;
  531. }
  532. static void msm_iommu_set_stall(struct msm_mmu *mmu, bool enable)
  533. {
  534. struct adreno_smmu_priv *adreno_smmu = dev_get_drvdata(mmu->dev);
  535. if (adreno_smmu->set_stall)
  536. adreno_smmu->set_stall(adreno_smmu->cookie, enable);
  537. }
  538. static void msm_iommu_detach(struct msm_mmu *mmu)
  539. {
  540. struct msm_iommu *iommu = to_msm_iommu(mmu);
  541. iommu_detach_device(iommu->domain, mmu->dev);
  542. }
  543. static int msm_iommu_map(struct msm_mmu *mmu, uint64_t iova,
  544. struct sg_table *sgt, size_t off, size_t len,
  545. int prot)
  546. {
  547. struct msm_iommu *iommu = to_msm_iommu(mmu);
  548. size_t ret;
  549. WARN_ON(off != 0);
  550. /* The arm-smmu driver expects the addresses to be sign extended */
  551. if (iova & BIT_ULL(48))
  552. iova |= GENMASK_ULL(63, 49);
  553. ret = iommu_map_sgtable(iommu->domain, iova, sgt, prot);
  554. WARN_ON(!ret);
  555. return (ret == len) ? 0 : -EINVAL;
  556. }
  557. static int msm_iommu_unmap(struct msm_mmu *mmu, uint64_t iova, size_t len)
  558. {
  559. struct msm_iommu *iommu = to_msm_iommu(mmu);
  560. if (iova & BIT_ULL(48))
  561. iova |= GENMASK_ULL(63, 49);
  562. iommu_unmap(iommu->domain, iova, len);
  563. return 0;
  564. }
  565. static void msm_iommu_destroy(struct msm_mmu *mmu)
  566. {
  567. struct msm_iommu *iommu = to_msm_iommu(mmu);
  568. iommu_domain_free(iommu->domain);
  569. kmem_cache_destroy(iommu->pt_cache);
  570. kfree(iommu);
  571. }
  572. static const struct msm_mmu_funcs funcs = {
  573. .detach = msm_iommu_detach,
  574. .map = msm_iommu_map,
  575. .unmap = msm_iommu_unmap,
  576. .destroy = msm_iommu_destroy,
  577. .set_stall = msm_iommu_set_stall,
  578. };
  579. struct msm_mmu *msm_iommu_new(struct device *dev, unsigned long quirks)
  580. {
  581. struct iommu_domain *domain;
  582. struct msm_iommu *iommu;
  583. int ret;
  584. if (!device_iommu_mapped(dev))
  585. return ERR_PTR(-ENODEV);
  586. domain = iommu_paging_domain_alloc(dev);
  587. if (IS_ERR(domain))
  588. return ERR_CAST(domain);
  589. iommu_set_pgtable_quirks(domain, quirks);
  590. iommu = kzalloc_obj(*iommu);
  591. if (!iommu) {
  592. iommu_domain_free(domain);
  593. return ERR_PTR(-ENOMEM);
  594. }
  595. iommu->domain = domain;
  596. msm_mmu_init(&iommu->base, dev, &funcs, MSM_MMU_IOMMU);
  597. mutex_init(&iommu->init_lock);
  598. ret = iommu_attach_device(iommu->domain, dev);
  599. if (ret) {
  600. iommu_domain_free(domain);
  601. kfree(iommu);
  602. return ERR_PTR(ret);
  603. }
  604. return &iommu->base;
  605. }
  606. struct msm_mmu *msm_iommu_disp_new(struct device *dev, unsigned long quirks)
  607. {
  608. struct msm_iommu *iommu;
  609. struct msm_mmu *mmu;
  610. mmu = msm_iommu_new(dev, quirks);
  611. if (IS_ERR(mmu))
  612. return mmu;
  613. iommu = to_msm_iommu(mmu);
  614. iommu_set_fault_handler(iommu->domain, msm_disp_fault_handler, iommu);
  615. return mmu;
  616. }
  617. struct msm_mmu *msm_iommu_gpu_new(struct device *dev, struct msm_gpu *gpu, unsigned long quirks)
  618. {
  619. struct adreno_smmu_priv *adreno_smmu = dev_get_drvdata(dev);
  620. struct msm_iommu *iommu;
  621. struct msm_mmu *mmu;
  622. mmu = msm_iommu_new(dev, quirks);
  623. if (IS_ERR(mmu))
  624. return mmu;
  625. iommu = to_msm_iommu(mmu);
  626. if (adreno_smmu->cookie) {
  627. const struct io_pgtable_cfg *cfg =
  628. adreno_smmu->get_ttbr1_cfg(adreno_smmu->cookie);
  629. size_t tblsz = get_tblsz(cfg);
  630. iommu->pt_cache =
  631. kmem_cache_create("msm-mmu-pt", tblsz, tblsz, 0, NULL);
  632. }
  633. iommu_set_fault_handler(iommu->domain, msm_gpu_fault_handler, iommu);
  634. /* Enable stall on iommu fault: */
  635. if (adreno_smmu->set_stall)
  636. adreno_smmu->set_stall(adreno_smmu->cookie, true);
  637. return mmu;
  638. }