msm_iommu.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
  3. *
  4. * Author: Stepan Moskovchenko <stepanm@codeaurora.org>
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/kernel.h>
  8. #include <linux/init.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/errno.h>
  11. #include <linux/io.h>
  12. #include <linux/io-pgtable.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/list.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/slab.h>
  17. #include <linux/iommu.h>
  18. #include <linux/clk.h>
  19. #include <linux/err.h>
  20. #include <asm/cacheflush.h>
  21. #include <linux/sizes.h>
  22. #include "msm_iommu_hw-8xxx.h"
  23. #include "msm_iommu.h"
  24. #define MRC(reg, processor, op1, crn, crm, op2) \
  25. __asm__ __volatile__ ( \
  26. " mrc " #processor "," #op1 ", %0," #crn "," #crm "," #op2 "\n" \
  27. : "=r" (reg))
  28. /* bitmap of the page sizes currently supported */
  29. #define MSM_IOMMU_PGSIZES (SZ_4K | SZ_64K | SZ_1M | SZ_16M)
  30. static DEFINE_SPINLOCK(msm_iommu_lock);
  31. static LIST_HEAD(qcom_iommu_devices);
  32. static struct iommu_ops msm_iommu_ops;
  33. struct msm_priv {
  34. struct list_head list_attached;
  35. struct iommu_domain domain;
  36. struct io_pgtable_cfg cfg;
  37. struct io_pgtable_ops *iop;
  38. struct device *dev;
  39. spinlock_t pgtlock; /* pagetable lock */
  40. };
  41. static struct msm_priv *to_msm_priv(struct iommu_domain *dom)
  42. {
  43. return container_of(dom, struct msm_priv, domain);
  44. }
  45. static int __enable_clocks(struct msm_iommu_dev *iommu)
  46. {
  47. int ret;
  48. ret = clk_enable(iommu->pclk);
  49. if (ret)
  50. goto fail;
  51. if (iommu->clk) {
  52. ret = clk_enable(iommu->clk);
  53. if (ret)
  54. clk_disable(iommu->pclk);
  55. }
  56. fail:
  57. return ret;
  58. }
  59. static void __disable_clocks(struct msm_iommu_dev *iommu)
  60. {
  61. if (iommu->clk)
  62. clk_disable(iommu->clk);
  63. clk_disable(iommu->pclk);
  64. }
  65. static void msm_iommu_reset(void __iomem *base, int ncb)
  66. {
  67. int ctx;
  68. SET_RPUE(base, 0);
  69. SET_RPUEIE(base, 0);
  70. SET_ESRRESTORE(base, 0);
  71. SET_TBE(base, 0);
  72. SET_CR(base, 0);
  73. SET_SPDMBE(base, 0);
  74. SET_TESTBUSCR(base, 0);
  75. SET_TLBRSW(base, 0);
  76. SET_GLOBAL_TLBIALL(base, 0);
  77. SET_RPU_ACR(base, 0);
  78. SET_TLBLKCRWE(base, 1);
  79. for (ctx = 0; ctx < ncb; ctx++) {
  80. SET_BPRCOSH(base, ctx, 0);
  81. SET_BPRCISH(base, ctx, 0);
  82. SET_BPRCNSH(base, ctx, 0);
  83. SET_BPSHCFG(base, ctx, 0);
  84. SET_BPMTCFG(base, ctx, 0);
  85. SET_ACTLR(base, ctx, 0);
  86. SET_SCTLR(base, ctx, 0);
  87. SET_FSRRESTORE(base, ctx, 0);
  88. SET_TTBR0(base, ctx, 0);
  89. SET_TTBR1(base, ctx, 0);
  90. SET_TTBCR(base, ctx, 0);
  91. SET_BFBCR(base, ctx, 0);
  92. SET_PAR(base, ctx, 0);
  93. SET_FAR(base, ctx, 0);
  94. SET_CTX_TLBIALL(base, ctx, 0);
  95. SET_TLBFLPTER(base, ctx, 0);
  96. SET_TLBSLPTER(base, ctx, 0);
  97. SET_TLBLKCR(base, ctx, 0);
  98. SET_CONTEXTIDR(base, ctx, 0);
  99. }
  100. }
  101. static void __flush_iotlb(void *cookie)
  102. {
  103. struct msm_priv *priv = cookie;
  104. struct msm_iommu_dev *iommu = NULL;
  105. struct msm_iommu_ctx_dev *master;
  106. int ret = 0;
  107. list_for_each_entry(iommu, &priv->list_attached, dom_node) {
  108. ret = __enable_clocks(iommu);
  109. if (ret)
  110. goto fail;
  111. list_for_each_entry(master, &iommu->ctx_list, list)
  112. SET_CTX_TLBIALL(iommu->base, master->num, 0);
  113. __disable_clocks(iommu);
  114. }
  115. fail:
  116. return;
  117. }
  118. static void __flush_iotlb_range(unsigned long iova, size_t size,
  119. size_t granule, bool leaf, void *cookie)
  120. {
  121. struct msm_priv *priv = cookie;
  122. struct msm_iommu_dev *iommu = NULL;
  123. struct msm_iommu_ctx_dev *master;
  124. int ret = 0;
  125. int temp_size;
  126. list_for_each_entry(iommu, &priv->list_attached, dom_node) {
  127. ret = __enable_clocks(iommu);
  128. if (ret)
  129. goto fail;
  130. list_for_each_entry(master, &iommu->ctx_list, list) {
  131. temp_size = size;
  132. do {
  133. iova &= TLBIVA_VA;
  134. iova |= GET_CONTEXTIDR_ASID(iommu->base,
  135. master->num);
  136. SET_TLBIVA(iommu->base, master->num, iova);
  137. iova += granule;
  138. } while (temp_size -= granule);
  139. }
  140. __disable_clocks(iommu);
  141. }
  142. fail:
  143. return;
  144. }
  145. static void __flush_iotlb_walk(unsigned long iova, size_t size,
  146. size_t granule, void *cookie)
  147. {
  148. __flush_iotlb_range(iova, size, granule, false, cookie);
  149. }
  150. static void __flush_iotlb_page(struct iommu_iotlb_gather *gather,
  151. unsigned long iova, size_t granule, void *cookie)
  152. {
  153. __flush_iotlb_range(iova, granule, granule, true, cookie);
  154. }
  155. static const struct iommu_flush_ops msm_iommu_flush_ops = {
  156. .tlb_flush_all = __flush_iotlb,
  157. .tlb_flush_walk = __flush_iotlb_walk,
  158. .tlb_add_page = __flush_iotlb_page,
  159. };
  160. static int msm_iommu_alloc_ctx(unsigned long *map, int start, int end)
  161. {
  162. int idx;
  163. do {
  164. idx = find_next_zero_bit(map, end, start);
  165. if (idx == end)
  166. return -ENOSPC;
  167. } while (test_and_set_bit(idx, map));
  168. return idx;
  169. }
  170. static void msm_iommu_free_ctx(unsigned long *map, int idx)
  171. {
  172. clear_bit(idx, map);
  173. }
  174. static void config_mids(struct msm_iommu_dev *iommu,
  175. struct msm_iommu_ctx_dev *master)
  176. {
  177. int mid, ctx, i;
  178. for (i = 0; i < master->num_mids; i++) {
  179. mid = master->mids[i];
  180. ctx = master->num;
  181. SET_M2VCBR_N(iommu->base, mid, 0);
  182. SET_CBACR_N(iommu->base, ctx, 0);
  183. /* Set VMID = 0 */
  184. SET_VMID(iommu->base, mid, 0);
  185. /* Set the context number for that MID to this context */
  186. SET_CBNDX(iommu->base, mid, ctx);
  187. /* Set MID associated with this context bank to 0*/
  188. SET_CBVMID(iommu->base, ctx, 0);
  189. /* Set the ASID for TLB tagging for this context */
  190. SET_CONTEXTIDR_ASID(iommu->base, ctx, ctx);
  191. /* Set security bit override to be Non-secure */
  192. SET_NSCFG(iommu->base, mid, 3);
  193. }
  194. }
  195. static void __reset_context(void __iomem *base, int ctx)
  196. {
  197. SET_BPRCOSH(base, ctx, 0);
  198. SET_BPRCISH(base, ctx, 0);
  199. SET_BPRCNSH(base, ctx, 0);
  200. SET_BPSHCFG(base, ctx, 0);
  201. SET_BPMTCFG(base, ctx, 0);
  202. SET_ACTLR(base, ctx, 0);
  203. SET_SCTLR(base, ctx, 0);
  204. SET_FSRRESTORE(base, ctx, 0);
  205. SET_TTBR0(base, ctx, 0);
  206. SET_TTBR1(base, ctx, 0);
  207. SET_TTBCR(base, ctx, 0);
  208. SET_BFBCR(base, ctx, 0);
  209. SET_PAR(base, ctx, 0);
  210. SET_FAR(base, ctx, 0);
  211. SET_CTX_TLBIALL(base, ctx, 0);
  212. SET_TLBFLPTER(base, ctx, 0);
  213. SET_TLBSLPTER(base, ctx, 0);
  214. SET_TLBLKCR(base, ctx, 0);
  215. }
  216. static void __program_context(void __iomem *base, int ctx,
  217. struct msm_priv *priv)
  218. {
  219. __reset_context(base, ctx);
  220. /* Turn on TEX Remap */
  221. SET_TRE(base, ctx, 1);
  222. SET_AFE(base, ctx, 1);
  223. /* Set up HTW mode */
  224. /* TLB miss configuration: perform HTW on miss */
  225. SET_TLBMCFG(base, ctx, 0x3);
  226. /* V2P configuration: HTW for access */
  227. SET_V2PCFG(base, ctx, 0x3);
  228. SET_TTBCR(base, ctx, priv->cfg.arm_v7s_cfg.tcr);
  229. SET_TTBR0(base, ctx, priv->cfg.arm_v7s_cfg.ttbr);
  230. SET_TTBR1(base, ctx, 0);
  231. /* Set prrr and nmrr */
  232. SET_PRRR(base, ctx, priv->cfg.arm_v7s_cfg.prrr);
  233. SET_NMRR(base, ctx, priv->cfg.arm_v7s_cfg.nmrr);
  234. /* Invalidate the TLB for this context */
  235. SET_CTX_TLBIALL(base, ctx, 0);
  236. /* Set interrupt number to "secure" interrupt */
  237. SET_IRPTNDX(base, ctx, 0);
  238. /* Enable context fault interrupt */
  239. SET_CFEIE(base, ctx, 1);
  240. /* Stall access on a context fault and let the handler deal with it */
  241. SET_CFCFG(base, ctx, 1);
  242. /* Redirect all cacheable requests to L2 slave port. */
  243. SET_RCISH(base, ctx, 1);
  244. SET_RCOSH(base, ctx, 1);
  245. SET_RCNSH(base, ctx, 1);
  246. /* Turn on BFB prefetch */
  247. SET_BFBDFE(base, ctx, 1);
  248. /* Enable the MMU */
  249. SET_M(base, ctx, 1);
  250. }
  251. static struct iommu_domain *msm_iommu_domain_alloc_paging(struct device *dev)
  252. {
  253. struct msm_priv *priv;
  254. priv = kzalloc_obj(*priv);
  255. if (!priv)
  256. goto fail_nomem;
  257. INIT_LIST_HEAD(&priv->list_attached);
  258. priv->domain.pgsize_bitmap = MSM_IOMMU_PGSIZES;
  259. priv->domain.geometry.aperture_start = 0;
  260. priv->domain.geometry.aperture_end = (1ULL << 32) - 1;
  261. priv->domain.geometry.force_aperture = true;
  262. return &priv->domain;
  263. fail_nomem:
  264. kfree(priv);
  265. return NULL;
  266. }
  267. static void msm_iommu_domain_free(struct iommu_domain *domain)
  268. {
  269. struct msm_priv *priv;
  270. unsigned long flags;
  271. spin_lock_irqsave(&msm_iommu_lock, flags);
  272. priv = to_msm_priv(domain);
  273. kfree(priv);
  274. spin_unlock_irqrestore(&msm_iommu_lock, flags);
  275. }
  276. static int msm_iommu_domain_config(struct msm_priv *priv)
  277. {
  278. spin_lock_init(&priv->pgtlock);
  279. priv->cfg = (struct io_pgtable_cfg) {
  280. .pgsize_bitmap = priv->domain.pgsize_bitmap,
  281. .ias = 32,
  282. .oas = 32,
  283. .tlb = &msm_iommu_flush_ops,
  284. .iommu_dev = priv->dev,
  285. };
  286. priv->iop = alloc_io_pgtable_ops(ARM_V7S, &priv->cfg, priv);
  287. if (!priv->iop) {
  288. dev_err(priv->dev, "Failed to allocate pgtable\n");
  289. return -EINVAL;
  290. }
  291. return 0;
  292. }
  293. /* Must be called under msm_iommu_lock */
  294. static struct msm_iommu_dev *find_iommu_for_dev(struct device *dev)
  295. {
  296. struct msm_iommu_dev *iommu, *ret = NULL;
  297. struct msm_iommu_ctx_dev *master;
  298. list_for_each_entry(iommu, &qcom_iommu_devices, dev_node) {
  299. master = list_first_entry(&iommu->ctx_list,
  300. struct msm_iommu_ctx_dev,
  301. list);
  302. if (master->of_node == dev->of_node) {
  303. ret = iommu;
  304. break;
  305. }
  306. }
  307. return ret;
  308. }
  309. static struct iommu_device *msm_iommu_probe_device(struct device *dev)
  310. {
  311. struct msm_iommu_dev *iommu;
  312. unsigned long flags;
  313. spin_lock_irqsave(&msm_iommu_lock, flags);
  314. iommu = find_iommu_for_dev(dev);
  315. spin_unlock_irqrestore(&msm_iommu_lock, flags);
  316. if (!iommu)
  317. return ERR_PTR(-ENODEV);
  318. return &iommu->iommu;
  319. }
  320. static int msm_iommu_attach_dev(struct iommu_domain *domain, struct device *dev,
  321. struct iommu_domain *old)
  322. {
  323. int ret = 0;
  324. unsigned long flags;
  325. struct msm_iommu_dev *iommu;
  326. struct msm_priv *priv = to_msm_priv(domain);
  327. struct msm_iommu_ctx_dev *master;
  328. priv->dev = dev;
  329. msm_iommu_domain_config(priv);
  330. spin_lock_irqsave(&msm_iommu_lock, flags);
  331. list_for_each_entry(iommu, &qcom_iommu_devices, dev_node) {
  332. master = list_first_entry(&iommu->ctx_list,
  333. struct msm_iommu_ctx_dev,
  334. list);
  335. if (master->of_node == dev->of_node) {
  336. ret = __enable_clocks(iommu);
  337. if (ret)
  338. goto fail;
  339. list_for_each_entry(master, &iommu->ctx_list, list) {
  340. if (master->num) {
  341. dev_err(dev, "domain already attached");
  342. ret = -EEXIST;
  343. goto fail;
  344. }
  345. master->num =
  346. msm_iommu_alloc_ctx(iommu->context_map,
  347. 0, iommu->ncb);
  348. if (IS_ERR_VALUE(master->num)) {
  349. ret = -ENODEV;
  350. goto fail;
  351. }
  352. config_mids(iommu, master);
  353. __program_context(iommu->base, master->num,
  354. priv);
  355. }
  356. __disable_clocks(iommu);
  357. list_add(&iommu->dom_node, &priv->list_attached);
  358. }
  359. }
  360. fail:
  361. spin_unlock_irqrestore(&msm_iommu_lock, flags);
  362. return ret;
  363. }
  364. static int msm_iommu_identity_attach(struct iommu_domain *identity_domain,
  365. struct device *dev,
  366. struct iommu_domain *old)
  367. {
  368. struct msm_priv *priv;
  369. unsigned long flags;
  370. struct msm_iommu_dev *iommu;
  371. struct msm_iommu_ctx_dev *master;
  372. int ret = 0;
  373. if (old == identity_domain || !old)
  374. return 0;
  375. priv = to_msm_priv(old);
  376. free_io_pgtable_ops(priv->iop);
  377. spin_lock_irqsave(&msm_iommu_lock, flags);
  378. list_for_each_entry(iommu, &priv->list_attached, dom_node) {
  379. ret = __enable_clocks(iommu);
  380. if (ret)
  381. goto fail;
  382. list_for_each_entry(master, &iommu->ctx_list, list) {
  383. msm_iommu_free_ctx(iommu->context_map, master->num);
  384. __reset_context(iommu->base, master->num);
  385. }
  386. __disable_clocks(iommu);
  387. }
  388. fail:
  389. spin_unlock_irqrestore(&msm_iommu_lock, flags);
  390. return ret;
  391. }
  392. static struct iommu_domain_ops msm_iommu_identity_ops = {
  393. .attach_dev = msm_iommu_identity_attach,
  394. };
  395. static struct iommu_domain msm_iommu_identity_domain = {
  396. .type = IOMMU_DOMAIN_IDENTITY,
  397. .ops = &msm_iommu_identity_ops,
  398. };
  399. static int msm_iommu_map(struct iommu_domain *domain, unsigned long iova,
  400. phys_addr_t pa, size_t pgsize, size_t pgcount,
  401. int prot, gfp_t gfp, size_t *mapped)
  402. {
  403. struct msm_priv *priv = to_msm_priv(domain);
  404. unsigned long flags;
  405. int ret;
  406. spin_lock_irqsave(&priv->pgtlock, flags);
  407. ret = priv->iop->map_pages(priv->iop, iova, pa, pgsize, pgcount, prot,
  408. GFP_ATOMIC, mapped);
  409. spin_unlock_irqrestore(&priv->pgtlock, flags);
  410. return ret;
  411. }
  412. static int msm_iommu_sync_map(struct iommu_domain *domain, unsigned long iova,
  413. size_t size)
  414. {
  415. struct msm_priv *priv = to_msm_priv(domain);
  416. __flush_iotlb_range(iova, size, SZ_4K, false, priv);
  417. return 0;
  418. }
  419. static size_t msm_iommu_unmap(struct iommu_domain *domain, unsigned long iova,
  420. size_t pgsize, size_t pgcount,
  421. struct iommu_iotlb_gather *gather)
  422. {
  423. struct msm_priv *priv = to_msm_priv(domain);
  424. unsigned long flags;
  425. size_t ret;
  426. spin_lock_irqsave(&priv->pgtlock, flags);
  427. ret = priv->iop->unmap_pages(priv->iop, iova, pgsize, pgcount, gather);
  428. spin_unlock_irqrestore(&priv->pgtlock, flags);
  429. return ret;
  430. }
  431. static phys_addr_t msm_iommu_iova_to_phys(struct iommu_domain *domain,
  432. dma_addr_t va)
  433. {
  434. struct msm_priv *priv;
  435. struct msm_iommu_dev *iommu;
  436. struct msm_iommu_ctx_dev *master;
  437. unsigned int par;
  438. unsigned long flags;
  439. phys_addr_t ret = 0;
  440. spin_lock_irqsave(&msm_iommu_lock, flags);
  441. priv = to_msm_priv(domain);
  442. iommu = list_first_entry(&priv->list_attached,
  443. struct msm_iommu_dev, dom_node);
  444. if (list_empty(&iommu->ctx_list))
  445. goto fail;
  446. master = list_first_entry(&iommu->ctx_list,
  447. struct msm_iommu_ctx_dev, list);
  448. if (!master)
  449. goto fail;
  450. ret = __enable_clocks(iommu);
  451. if (ret)
  452. goto fail;
  453. /* Invalidate context TLB */
  454. SET_CTX_TLBIALL(iommu->base, master->num, 0);
  455. SET_V2PPR(iommu->base, master->num, va & V2Pxx_VA);
  456. par = GET_PAR(iommu->base, master->num);
  457. /* We are dealing with a supersection */
  458. if (GET_NOFAULT_SS(iommu->base, master->num))
  459. ret = (par & 0xFF000000) | (va & 0x00FFFFFF);
  460. else /* Upper 20 bits from PAR, lower 12 from VA */
  461. ret = (par & 0xFFFFF000) | (va & 0x00000FFF);
  462. if (GET_FAULT(iommu->base, master->num))
  463. ret = 0;
  464. __disable_clocks(iommu);
  465. fail:
  466. spin_unlock_irqrestore(&msm_iommu_lock, flags);
  467. return ret;
  468. }
  469. static void print_ctx_regs(void __iomem *base, int ctx)
  470. {
  471. unsigned int fsr = GET_FSR(base, ctx);
  472. pr_err("FAR = %08x PAR = %08x\n",
  473. GET_FAR(base, ctx), GET_PAR(base, ctx));
  474. pr_err("FSR = %08x [%s%s%s%s%s%s%s%s%s%s]\n", fsr,
  475. (fsr & 0x02) ? "TF " : "",
  476. (fsr & 0x04) ? "AFF " : "",
  477. (fsr & 0x08) ? "APF " : "",
  478. (fsr & 0x10) ? "TLBMF " : "",
  479. (fsr & 0x20) ? "HTWDEEF " : "",
  480. (fsr & 0x40) ? "HTWSEEF " : "",
  481. (fsr & 0x80) ? "MHF " : "",
  482. (fsr & 0x10000) ? "SL " : "",
  483. (fsr & 0x40000000) ? "SS " : "",
  484. (fsr & 0x80000000) ? "MULTI " : "");
  485. pr_err("FSYNR0 = %08x FSYNR1 = %08x\n",
  486. GET_FSYNR0(base, ctx), GET_FSYNR1(base, ctx));
  487. pr_err("TTBR0 = %08x TTBR1 = %08x\n",
  488. GET_TTBR0(base, ctx), GET_TTBR1(base, ctx));
  489. pr_err("SCTLR = %08x ACTLR = %08x\n",
  490. GET_SCTLR(base, ctx), GET_ACTLR(base, ctx));
  491. }
  492. static int insert_iommu_master(struct device *dev,
  493. struct msm_iommu_dev **iommu,
  494. const struct of_phandle_args *spec)
  495. {
  496. struct msm_iommu_ctx_dev *master = dev_iommu_priv_get(dev);
  497. int sid;
  498. if (list_empty(&(*iommu)->ctx_list)) {
  499. master = kzalloc_obj(*master, GFP_ATOMIC);
  500. if (!master) {
  501. dev_err(dev, "Failed to allocate iommu_master\n");
  502. return -ENOMEM;
  503. }
  504. master->of_node = dev->of_node;
  505. list_add(&master->list, &(*iommu)->ctx_list);
  506. dev_iommu_priv_set(dev, master);
  507. }
  508. for (sid = 0; sid < master->num_mids; sid++)
  509. if (master->mids[sid] == spec->args[0]) {
  510. dev_warn(dev, "Stream ID 0x%x repeated; ignoring\n",
  511. sid);
  512. return 0;
  513. }
  514. master->mids[master->num_mids++] = spec->args[0];
  515. return 0;
  516. }
  517. static int qcom_iommu_of_xlate(struct device *dev,
  518. const struct of_phandle_args *spec)
  519. {
  520. struct msm_iommu_dev *iommu = NULL, *iter;
  521. unsigned long flags;
  522. int ret = 0;
  523. spin_lock_irqsave(&msm_iommu_lock, flags);
  524. list_for_each_entry(iter, &qcom_iommu_devices, dev_node) {
  525. if (iter->dev->of_node == spec->np) {
  526. iommu = iter;
  527. break;
  528. }
  529. }
  530. if (!iommu) {
  531. ret = -ENODEV;
  532. goto fail;
  533. }
  534. ret = insert_iommu_master(dev, &iommu, spec);
  535. fail:
  536. spin_unlock_irqrestore(&msm_iommu_lock, flags);
  537. return ret;
  538. }
  539. irqreturn_t msm_iommu_fault_handler(int irq, void *dev_id)
  540. {
  541. struct msm_iommu_dev *iommu = dev_id;
  542. unsigned int fsr;
  543. int i, ret;
  544. spin_lock(&msm_iommu_lock);
  545. if (!iommu) {
  546. pr_err("Invalid device ID in context interrupt handler\n");
  547. goto fail;
  548. }
  549. pr_err("Unexpected IOMMU page fault!\n");
  550. pr_err("base = %08x\n", (unsigned int)iommu->base);
  551. ret = __enable_clocks(iommu);
  552. if (ret)
  553. goto fail;
  554. for (i = 0; i < iommu->ncb; i++) {
  555. fsr = GET_FSR(iommu->base, i);
  556. if (fsr) {
  557. pr_err("Fault occurred in context %d.\n", i);
  558. pr_err("Interesting registers:\n");
  559. print_ctx_regs(iommu->base, i);
  560. SET_FSR(iommu->base, i, 0x4000000F);
  561. }
  562. }
  563. __disable_clocks(iommu);
  564. fail:
  565. spin_unlock(&msm_iommu_lock);
  566. return 0;
  567. }
  568. static struct iommu_ops msm_iommu_ops = {
  569. .identity_domain = &msm_iommu_identity_domain,
  570. .domain_alloc_paging = msm_iommu_domain_alloc_paging,
  571. .probe_device = msm_iommu_probe_device,
  572. .device_group = generic_device_group,
  573. .of_xlate = qcom_iommu_of_xlate,
  574. .default_domain_ops = &(const struct iommu_domain_ops) {
  575. .attach_dev = msm_iommu_attach_dev,
  576. .map_pages = msm_iommu_map,
  577. .unmap_pages = msm_iommu_unmap,
  578. /*
  579. * Nothing is needed here, the barrier to guarantee
  580. * completion of the tlb sync operation is implicitly
  581. * taken care when the iommu client does a writel before
  582. * kick starting the other master.
  583. */
  584. .iotlb_sync = NULL,
  585. .iotlb_sync_map = msm_iommu_sync_map,
  586. .iova_to_phys = msm_iommu_iova_to_phys,
  587. .free = msm_iommu_domain_free,
  588. }
  589. };
  590. static int msm_iommu_probe(struct platform_device *pdev)
  591. {
  592. struct resource *r;
  593. resource_size_t ioaddr;
  594. struct msm_iommu_dev *iommu;
  595. int ret, par, val;
  596. iommu = devm_kzalloc(&pdev->dev, sizeof(*iommu), GFP_KERNEL);
  597. if (!iommu)
  598. return -ENODEV;
  599. iommu->dev = &pdev->dev;
  600. INIT_LIST_HEAD(&iommu->ctx_list);
  601. iommu->pclk = devm_clk_get_prepared(iommu->dev, "smmu_pclk");
  602. if (IS_ERR(iommu->pclk))
  603. return dev_err_probe(iommu->dev, PTR_ERR(iommu->pclk),
  604. "could not get smmu_pclk\n");
  605. iommu->clk = devm_clk_get_prepared(iommu->dev, "iommu_clk");
  606. if (IS_ERR(iommu->clk))
  607. return dev_err_probe(iommu->dev, PTR_ERR(iommu->clk),
  608. "could not get iommu_clk\n");
  609. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  610. iommu->base = devm_ioremap_resource(iommu->dev, r);
  611. if (IS_ERR(iommu->base)) {
  612. ret = dev_err_probe(iommu->dev, PTR_ERR(iommu->base), "could not get iommu base\n");
  613. return ret;
  614. }
  615. ioaddr = r->start;
  616. iommu->irq = platform_get_irq(pdev, 0);
  617. if (iommu->irq < 0)
  618. return -ENODEV;
  619. ret = of_property_read_u32(iommu->dev->of_node, "qcom,ncb", &val);
  620. if (ret) {
  621. dev_err(iommu->dev, "could not get ncb\n");
  622. return ret;
  623. }
  624. iommu->ncb = val;
  625. msm_iommu_reset(iommu->base, iommu->ncb);
  626. SET_M(iommu->base, 0, 1);
  627. SET_PAR(iommu->base, 0, 0);
  628. SET_V2PCFG(iommu->base, 0, 1);
  629. SET_V2PPR(iommu->base, 0, 0);
  630. par = GET_PAR(iommu->base, 0);
  631. SET_V2PCFG(iommu->base, 0, 0);
  632. SET_M(iommu->base, 0, 0);
  633. if (!par) {
  634. pr_err("Invalid PAR value detected\n");
  635. return -ENODEV;
  636. }
  637. ret = devm_request_threaded_irq(iommu->dev, iommu->irq, NULL,
  638. msm_iommu_fault_handler,
  639. IRQF_ONESHOT | IRQF_SHARED,
  640. "msm_iommu_secure_irpt_handler",
  641. iommu);
  642. if (ret) {
  643. pr_err("Request IRQ %d failed with ret=%d\n", iommu->irq, ret);
  644. return ret;
  645. }
  646. list_add(&iommu->dev_node, &qcom_iommu_devices);
  647. ret = iommu_device_sysfs_add(&iommu->iommu, iommu->dev, NULL,
  648. "msm-smmu.%pa", &ioaddr);
  649. if (ret) {
  650. pr_err("Could not add msm-smmu at %pa to sysfs\n", &ioaddr);
  651. return ret;
  652. }
  653. ret = iommu_device_register(&iommu->iommu, &msm_iommu_ops, &pdev->dev);
  654. if (ret) {
  655. pr_err("Could not register msm-smmu at %pa\n", &ioaddr);
  656. return ret;
  657. }
  658. pr_info("device mapped at %p, irq %d with %d ctx banks\n",
  659. iommu->base, iommu->irq, iommu->ncb);
  660. return ret;
  661. }
  662. static const struct of_device_id msm_iommu_dt_match[] = {
  663. { .compatible = "qcom,apq8064-iommu" },
  664. {}
  665. };
  666. static struct platform_driver msm_iommu_driver = {
  667. .driver = {
  668. .name = "msm_iommu",
  669. .of_match_table = msm_iommu_dt_match,
  670. },
  671. .probe = msm_iommu_probe,
  672. };
  673. builtin_platform_driver(msm_iommu_driver);