qcom_iommu.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. /*
  2. * IOMMU API for QCOM secure IOMMUs. Somewhat based on arm-smmu.c
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. * Copyright (C) 2013 ARM Limited
  17. * Copyright (C) 2017 Red Hat
  18. */
  19. #include <linux/atomic.h>
  20. #include <linux/clk.h>
  21. #include <linux/delay.h>
  22. #include <linux/dma-iommu.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/err.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/io.h>
  27. #include <linux/io-64-nonatomic-hi-lo.h>
  28. #include <linux/iommu.h>
  29. #include <linux/iopoll.h>
  30. #include <linux/kconfig.h>
  31. #include <linux/module.h>
  32. #include <linux/mutex.h>
  33. #include <linux/of.h>
  34. #include <linux/of_address.h>
  35. #include <linux/of_device.h>
  36. #include <linux/of_iommu.h>
  37. #include <linux/platform_device.h>
  38. #include <linux/pm.h>
  39. #include <linux/pm_runtime.h>
  40. #include <linux/qcom_scm.h>
  41. #include <linux/slab.h>
  42. #include <linux/spinlock.h>
  43. #include "io-pgtable.h"
  44. #include "arm-smmu-regs.h"
  45. #define SMMU_INTR_SEL_NS 0x2000
  46. struct qcom_iommu_ctx;
  47. struct qcom_iommu_dev {
  48. /* IOMMU core code handle */
  49. struct iommu_device iommu;
  50. struct device *dev;
  51. struct clk *iface_clk;
  52. struct clk *bus_clk;
  53. void __iomem *local_base;
  54. u32 sec_id;
  55. u8 num_ctxs;
  56. struct qcom_iommu_ctx *ctxs[0]; /* indexed by asid-1 */
  57. };
  58. struct qcom_iommu_ctx {
  59. struct device *dev;
  60. void __iomem *base;
  61. bool secure_init;
  62. u8 asid; /* asid and ctx bank # are 1:1 */
  63. };
  64. struct qcom_iommu_domain {
  65. struct io_pgtable_ops *pgtbl_ops;
  66. spinlock_t pgtbl_lock;
  67. struct mutex init_mutex; /* Protects iommu pointer */
  68. struct iommu_domain domain;
  69. struct qcom_iommu_dev *iommu;
  70. };
  71. static struct qcom_iommu_domain *to_qcom_iommu_domain(struct iommu_domain *dom)
  72. {
  73. return container_of(dom, struct qcom_iommu_domain, domain);
  74. }
  75. static const struct iommu_ops qcom_iommu_ops;
  76. static struct qcom_iommu_dev * to_iommu(struct iommu_fwspec *fwspec)
  77. {
  78. if (!fwspec || fwspec->ops != &qcom_iommu_ops)
  79. return NULL;
  80. return fwspec->iommu_priv;
  81. }
  82. static struct qcom_iommu_ctx * to_ctx(struct iommu_fwspec *fwspec, unsigned asid)
  83. {
  84. struct qcom_iommu_dev *qcom_iommu = to_iommu(fwspec);
  85. if (!qcom_iommu)
  86. return NULL;
  87. return qcom_iommu->ctxs[asid - 1];
  88. }
  89. static inline void
  90. iommu_writel(struct qcom_iommu_ctx *ctx, unsigned reg, u32 val)
  91. {
  92. writel_relaxed(val, ctx->base + reg);
  93. }
  94. static inline void
  95. iommu_writeq(struct qcom_iommu_ctx *ctx, unsigned reg, u64 val)
  96. {
  97. writeq_relaxed(val, ctx->base + reg);
  98. }
  99. static inline u32
  100. iommu_readl(struct qcom_iommu_ctx *ctx, unsigned reg)
  101. {
  102. return readl_relaxed(ctx->base + reg);
  103. }
  104. static inline u64
  105. iommu_readq(struct qcom_iommu_ctx *ctx, unsigned reg)
  106. {
  107. return readq_relaxed(ctx->base + reg);
  108. }
  109. static void qcom_iommu_tlb_sync(void *cookie)
  110. {
  111. struct iommu_fwspec *fwspec = cookie;
  112. unsigned i;
  113. for (i = 0; i < fwspec->num_ids; i++) {
  114. struct qcom_iommu_ctx *ctx = to_ctx(fwspec, fwspec->ids[i]);
  115. unsigned int val, ret;
  116. iommu_writel(ctx, ARM_SMMU_CB_TLBSYNC, 0);
  117. ret = readl_poll_timeout(ctx->base + ARM_SMMU_CB_TLBSTATUS, val,
  118. (val & 0x1) == 0, 0, 5000000);
  119. if (ret)
  120. dev_err(ctx->dev, "timeout waiting for TLB SYNC\n");
  121. }
  122. }
  123. static void qcom_iommu_tlb_inv_context(void *cookie)
  124. {
  125. struct iommu_fwspec *fwspec = cookie;
  126. unsigned i;
  127. for (i = 0; i < fwspec->num_ids; i++) {
  128. struct qcom_iommu_ctx *ctx = to_ctx(fwspec, fwspec->ids[i]);
  129. iommu_writel(ctx, ARM_SMMU_CB_S1_TLBIASID, ctx->asid);
  130. }
  131. qcom_iommu_tlb_sync(cookie);
  132. }
  133. static void qcom_iommu_tlb_inv_range_nosync(unsigned long iova, size_t size,
  134. size_t granule, bool leaf, void *cookie)
  135. {
  136. struct iommu_fwspec *fwspec = cookie;
  137. unsigned i, reg;
  138. reg = leaf ? ARM_SMMU_CB_S1_TLBIVAL : ARM_SMMU_CB_S1_TLBIVA;
  139. for (i = 0; i < fwspec->num_ids; i++) {
  140. struct qcom_iommu_ctx *ctx = to_ctx(fwspec, fwspec->ids[i]);
  141. size_t s = size;
  142. iova &= ~12UL;
  143. iova |= ctx->asid;
  144. do {
  145. iommu_writel(ctx, reg, iova);
  146. iova += granule;
  147. } while (s -= granule);
  148. }
  149. }
  150. static const struct iommu_gather_ops qcom_gather_ops = {
  151. .tlb_flush_all = qcom_iommu_tlb_inv_context,
  152. .tlb_add_flush = qcom_iommu_tlb_inv_range_nosync,
  153. .tlb_sync = qcom_iommu_tlb_sync,
  154. };
  155. static irqreturn_t qcom_iommu_fault(int irq, void *dev)
  156. {
  157. struct qcom_iommu_ctx *ctx = dev;
  158. u32 fsr, fsynr;
  159. u64 iova;
  160. fsr = iommu_readl(ctx, ARM_SMMU_CB_FSR);
  161. if (!(fsr & FSR_FAULT))
  162. return IRQ_NONE;
  163. fsynr = iommu_readl(ctx, ARM_SMMU_CB_FSYNR0);
  164. iova = iommu_readq(ctx, ARM_SMMU_CB_FAR);
  165. dev_err_ratelimited(ctx->dev,
  166. "Unhandled context fault: fsr=0x%x, "
  167. "iova=0x%016llx, fsynr=0x%x, cb=%d\n",
  168. fsr, iova, fsynr, ctx->asid);
  169. iommu_writel(ctx, ARM_SMMU_CB_FSR, fsr);
  170. return IRQ_HANDLED;
  171. }
  172. static int qcom_iommu_init_domain(struct iommu_domain *domain,
  173. struct qcom_iommu_dev *qcom_iommu,
  174. struct iommu_fwspec *fwspec)
  175. {
  176. struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
  177. struct io_pgtable_ops *pgtbl_ops;
  178. struct io_pgtable_cfg pgtbl_cfg;
  179. int i, ret = 0;
  180. u32 reg;
  181. mutex_lock(&qcom_domain->init_mutex);
  182. if (qcom_domain->iommu)
  183. goto out_unlock;
  184. pgtbl_cfg = (struct io_pgtable_cfg) {
  185. .pgsize_bitmap = qcom_iommu_ops.pgsize_bitmap,
  186. .ias = 32,
  187. .oas = 40,
  188. .tlb = &qcom_gather_ops,
  189. .iommu_dev = qcom_iommu->dev,
  190. };
  191. qcom_domain->iommu = qcom_iommu;
  192. pgtbl_ops = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &pgtbl_cfg, fwspec);
  193. if (!pgtbl_ops) {
  194. dev_err(qcom_iommu->dev, "failed to allocate pagetable ops\n");
  195. ret = -ENOMEM;
  196. goto out_clear_iommu;
  197. }
  198. /* Update the domain's page sizes to reflect the page table format */
  199. domain->pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;
  200. domain->geometry.aperture_end = (1ULL << pgtbl_cfg.ias) - 1;
  201. domain->geometry.force_aperture = true;
  202. for (i = 0; i < fwspec->num_ids; i++) {
  203. struct qcom_iommu_ctx *ctx = to_ctx(fwspec, fwspec->ids[i]);
  204. if (!ctx->secure_init) {
  205. ret = qcom_scm_restore_sec_cfg(qcom_iommu->sec_id, ctx->asid);
  206. if (ret) {
  207. dev_err(qcom_iommu->dev, "secure init failed: %d\n", ret);
  208. goto out_clear_iommu;
  209. }
  210. ctx->secure_init = true;
  211. }
  212. /* TTBRs */
  213. iommu_writeq(ctx, ARM_SMMU_CB_TTBR0,
  214. pgtbl_cfg.arm_lpae_s1_cfg.ttbr[0] |
  215. ((u64)ctx->asid << TTBRn_ASID_SHIFT));
  216. iommu_writeq(ctx, ARM_SMMU_CB_TTBR1,
  217. pgtbl_cfg.arm_lpae_s1_cfg.ttbr[1] |
  218. ((u64)ctx->asid << TTBRn_ASID_SHIFT));
  219. /* TTBCR */
  220. iommu_writel(ctx, ARM_SMMU_CB_TTBCR2,
  221. (pgtbl_cfg.arm_lpae_s1_cfg.tcr >> 32) |
  222. TTBCR2_SEP_UPSTREAM);
  223. iommu_writel(ctx, ARM_SMMU_CB_TTBCR,
  224. pgtbl_cfg.arm_lpae_s1_cfg.tcr);
  225. /* MAIRs (stage-1 only) */
  226. iommu_writel(ctx, ARM_SMMU_CB_S1_MAIR0,
  227. pgtbl_cfg.arm_lpae_s1_cfg.mair[0]);
  228. iommu_writel(ctx, ARM_SMMU_CB_S1_MAIR1,
  229. pgtbl_cfg.arm_lpae_s1_cfg.mair[1]);
  230. /* SCTLR */
  231. reg = SCTLR_CFIE | SCTLR_CFRE | SCTLR_AFE | SCTLR_TRE |
  232. SCTLR_M | SCTLR_S1_ASIDPNE;
  233. if (IS_ENABLED(CONFIG_BIG_ENDIAN))
  234. reg |= SCTLR_E;
  235. iommu_writel(ctx, ARM_SMMU_CB_SCTLR, reg);
  236. }
  237. mutex_unlock(&qcom_domain->init_mutex);
  238. /* Publish page table ops for map/unmap */
  239. qcom_domain->pgtbl_ops = pgtbl_ops;
  240. return 0;
  241. out_clear_iommu:
  242. qcom_domain->iommu = NULL;
  243. out_unlock:
  244. mutex_unlock(&qcom_domain->init_mutex);
  245. return ret;
  246. }
  247. static struct iommu_domain *qcom_iommu_domain_alloc(unsigned type)
  248. {
  249. struct qcom_iommu_domain *qcom_domain;
  250. if (type != IOMMU_DOMAIN_UNMANAGED && type != IOMMU_DOMAIN_DMA)
  251. return NULL;
  252. /*
  253. * Allocate the domain and initialise some of its data structures.
  254. * We can't really do anything meaningful until we've added a
  255. * master.
  256. */
  257. qcom_domain = kzalloc(sizeof(*qcom_domain), GFP_KERNEL);
  258. if (!qcom_domain)
  259. return NULL;
  260. if (type == IOMMU_DOMAIN_DMA &&
  261. iommu_get_dma_cookie(&qcom_domain->domain)) {
  262. kfree(qcom_domain);
  263. return NULL;
  264. }
  265. mutex_init(&qcom_domain->init_mutex);
  266. spin_lock_init(&qcom_domain->pgtbl_lock);
  267. return &qcom_domain->domain;
  268. }
  269. static void qcom_iommu_domain_free(struct iommu_domain *domain)
  270. {
  271. struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
  272. if (WARN_ON(qcom_domain->iommu)) /* forgot to detach? */
  273. return;
  274. iommu_put_dma_cookie(domain);
  275. /* NOTE: unmap can be called after client device is powered off,
  276. * for example, with GPUs or anything involving dma-buf. So we
  277. * cannot rely on the device_link. Make sure the IOMMU is on to
  278. * avoid unclocked accesses in the TLB inv path:
  279. */
  280. pm_runtime_get_sync(qcom_domain->iommu->dev);
  281. free_io_pgtable_ops(qcom_domain->pgtbl_ops);
  282. pm_runtime_put_sync(qcom_domain->iommu->dev);
  283. kfree(qcom_domain);
  284. }
  285. static int qcom_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
  286. {
  287. struct qcom_iommu_dev *qcom_iommu = to_iommu(dev->iommu_fwspec);
  288. struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
  289. int ret;
  290. if (!qcom_iommu) {
  291. dev_err(dev, "cannot attach to IOMMU, is it on the same bus?\n");
  292. return -ENXIO;
  293. }
  294. /* Ensure that the domain is finalized */
  295. pm_runtime_get_sync(qcom_iommu->dev);
  296. ret = qcom_iommu_init_domain(domain, qcom_iommu, dev->iommu_fwspec);
  297. pm_runtime_put_sync(qcom_iommu->dev);
  298. if (ret < 0)
  299. return ret;
  300. /*
  301. * Sanity check the domain. We don't support domains across
  302. * different IOMMUs.
  303. */
  304. if (qcom_domain->iommu != qcom_iommu) {
  305. dev_err(dev, "cannot attach to IOMMU %s while already "
  306. "attached to domain on IOMMU %s\n",
  307. dev_name(qcom_domain->iommu->dev),
  308. dev_name(qcom_iommu->dev));
  309. return -EINVAL;
  310. }
  311. return 0;
  312. }
  313. static void qcom_iommu_detach_dev(struct iommu_domain *domain, struct device *dev)
  314. {
  315. struct iommu_fwspec *fwspec = dev->iommu_fwspec;
  316. struct qcom_iommu_dev *qcom_iommu = to_iommu(fwspec);
  317. struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
  318. unsigned i;
  319. if (!qcom_domain->iommu)
  320. return;
  321. pm_runtime_get_sync(qcom_iommu->dev);
  322. for (i = 0; i < fwspec->num_ids; i++) {
  323. struct qcom_iommu_ctx *ctx = to_ctx(fwspec, fwspec->ids[i]);
  324. /* Disable the context bank: */
  325. iommu_writel(ctx, ARM_SMMU_CB_SCTLR, 0);
  326. }
  327. pm_runtime_put_sync(qcom_iommu->dev);
  328. qcom_domain->iommu = NULL;
  329. }
  330. static int qcom_iommu_map(struct iommu_domain *domain, unsigned long iova,
  331. phys_addr_t paddr, size_t size, int prot)
  332. {
  333. int ret;
  334. unsigned long flags;
  335. struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
  336. struct io_pgtable_ops *ops = qcom_domain->pgtbl_ops;
  337. if (!ops)
  338. return -ENODEV;
  339. spin_lock_irqsave(&qcom_domain->pgtbl_lock, flags);
  340. ret = ops->map(ops, iova, paddr, size, prot);
  341. spin_unlock_irqrestore(&qcom_domain->pgtbl_lock, flags);
  342. return ret;
  343. }
  344. static size_t qcom_iommu_unmap(struct iommu_domain *domain, unsigned long iova,
  345. size_t size)
  346. {
  347. size_t ret;
  348. unsigned long flags;
  349. struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
  350. struct io_pgtable_ops *ops = qcom_domain->pgtbl_ops;
  351. if (!ops)
  352. return 0;
  353. /* NOTE: unmap can be called after client device is powered off,
  354. * for example, with GPUs or anything involving dma-buf. So we
  355. * cannot rely on the device_link. Make sure the IOMMU is on to
  356. * avoid unclocked accesses in the TLB inv path:
  357. */
  358. pm_runtime_get_sync(qcom_domain->iommu->dev);
  359. spin_lock_irqsave(&qcom_domain->pgtbl_lock, flags);
  360. ret = ops->unmap(ops, iova, size);
  361. spin_unlock_irqrestore(&qcom_domain->pgtbl_lock, flags);
  362. pm_runtime_put_sync(qcom_domain->iommu->dev);
  363. return ret;
  364. }
  365. static phys_addr_t qcom_iommu_iova_to_phys(struct iommu_domain *domain,
  366. dma_addr_t iova)
  367. {
  368. phys_addr_t ret;
  369. unsigned long flags;
  370. struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
  371. struct io_pgtable_ops *ops = qcom_domain->pgtbl_ops;
  372. if (!ops)
  373. return 0;
  374. spin_lock_irqsave(&qcom_domain->pgtbl_lock, flags);
  375. ret = ops->iova_to_phys(ops, iova);
  376. spin_unlock_irqrestore(&qcom_domain->pgtbl_lock, flags);
  377. return ret;
  378. }
  379. static bool qcom_iommu_capable(enum iommu_cap cap)
  380. {
  381. switch (cap) {
  382. case IOMMU_CAP_CACHE_COHERENCY:
  383. /*
  384. * Return true here as the SMMU can always send out coherent
  385. * requests.
  386. */
  387. return true;
  388. case IOMMU_CAP_NOEXEC:
  389. return true;
  390. default:
  391. return false;
  392. }
  393. }
  394. static int qcom_iommu_add_device(struct device *dev)
  395. {
  396. struct qcom_iommu_dev *qcom_iommu = to_iommu(dev->iommu_fwspec);
  397. struct iommu_group *group;
  398. struct device_link *link;
  399. if (!qcom_iommu)
  400. return -ENODEV;
  401. /*
  402. * Establish the link between iommu and master, so that the
  403. * iommu gets runtime enabled/disabled as per the master's
  404. * needs.
  405. */
  406. link = device_link_add(dev, qcom_iommu->dev, DL_FLAG_PM_RUNTIME);
  407. if (!link) {
  408. dev_err(qcom_iommu->dev, "Unable to create device link between %s and %s\n",
  409. dev_name(qcom_iommu->dev), dev_name(dev));
  410. return -ENODEV;
  411. }
  412. group = iommu_group_get_for_dev(dev);
  413. if (IS_ERR_OR_NULL(group))
  414. return PTR_ERR_OR_ZERO(group);
  415. iommu_group_put(group);
  416. iommu_device_link(&qcom_iommu->iommu, dev);
  417. return 0;
  418. }
  419. static void qcom_iommu_remove_device(struct device *dev)
  420. {
  421. struct qcom_iommu_dev *qcom_iommu = to_iommu(dev->iommu_fwspec);
  422. if (!qcom_iommu)
  423. return;
  424. iommu_device_unlink(&qcom_iommu->iommu, dev);
  425. iommu_group_remove_device(dev);
  426. iommu_fwspec_free(dev);
  427. }
  428. static int qcom_iommu_of_xlate(struct device *dev, struct of_phandle_args *args)
  429. {
  430. struct qcom_iommu_dev *qcom_iommu;
  431. struct platform_device *iommu_pdev;
  432. unsigned asid = args->args[0];
  433. if (args->args_count != 1) {
  434. dev_err(dev, "incorrect number of iommu params found for %s "
  435. "(found %d, expected 1)\n",
  436. args->np->full_name, args->args_count);
  437. return -EINVAL;
  438. }
  439. iommu_pdev = of_find_device_by_node(args->np);
  440. if (WARN_ON(!iommu_pdev))
  441. return -EINVAL;
  442. qcom_iommu = platform_get_drvdata(iommu_pdev);
  443. /* make sure the asid specified in dt is valid, so we don't have
  444. * to sanity check this elsewhere, since 'asid - 1' is used to
  445. * index into qcom_iommu->ctxs:
  446. */
  447. if (WARN_ON(asid < 1) ||
  448. WARN_ON(asid > qcom_iommu->num_ctxs))
  449. return -EINVAL;
  450. if (!dev->iommu_fwspec->iommu_priv) {
  451. dev->iommu_fwspec->iommu_priv = qcom_iommu;
  452. } else {
  453. /* make sure devices iommus dt node isn't referring to
  454. * multiple different iommu devices. Multiple context
  455. * banks are ok, but multiple devices are not:
  456. */
  457. if (WARN_ON(qcom_iommu != dev->iommu_fwspec->iommu_priv))
  458. return -EINVAL;
  459. }
  460. return iommu_fwspec_add_ids(dev, &asid, 1);
  461. }
  462. static const struct iommu_ops qcom_iommu_ops = {
  463. .capable = qcom_iommu_capable,
  464. .domain_alloc = qcom_iommu_domain_alloc,
  465. .domain_free = qcom_iommu_domain_free,
  466. .attach_dev = qcom_iommu_attach_dev,
  467. .detach_dev = qcom_iommu_detach_dev,
  468. .map = qcom_iommu_map,
  469. .unmap = qcom_iommu_unmap,
  470. .map_sg = default_iommu_map_sg,
  471. .iova_to_phys = qcom_iommu_iova_to_phys,
  472. .add_device = qcom_iommu_add_device,
  473. .remove_device = qcom_iommu_remove_device,
  474. .device_group = generic_device_group,
  475. .of_xlate = qcom_iommu_of_xlate,
  476. .pgsize_bitmap = SZ_4K | SZ_64K | SZ_1M | SZ_16M,
  477. };
  478. static int qcom_iommu_enable_clocks(struct qcom_iommu_dev *qcom_iommu)
  479. {
  480. int ret;
  481. ret = clk_prepare_enable(qcom_iommu->iface_clk);
  482. if (ret) {
  483. dev_err(qcom_iommu->dev, "Couldn't enable iface_clk\n");
  484. return ret;
  485. }
  486. ret = clk_prepare_enable(qcom_iommu->bus_clk);
  487. if (ret) {
  488. dev_err(qcom_iommu->dev, "Couldn't enable bus_clk\n");
  489. clk_disable_unprepare(qcom_iommu->iface_clk);
  490. return ret;
  491. }
  492. return 0;
  493. }
  494. static void qcom_iommu_disable_clocks(struct qcom_iommu_dev *qcom_iommu)
  495. {
  496. clk_disable_unprepare(qcom_iommu->bus_clk);
  497. clk_disable_unprepare(qcom_iommu->iface_clk);
  498. }
  499. static int qcom_iommu_sec_ptbl_init(struct device *dev)
  500. {
  501. size_t psize = 0;
  502. unsigned int spare = 0;
  503. void *cpu_addr;
  504. dma_addr_t paddr;
  505. unsigned long attrs;
  506. static bool allocated = false;
  507. int ret;
  508. if (allocated)
  509. return 0;
  510. ret = qcom_scm_iommu_secure_ptbl_size(spare, &psize);
  511. if (ret) {
  512. dev_err(dev, "failed to get iommu secure pgtable size (%d)\n",
  513. ret);
  514. return ret;
  515. }
  516. dev_info(dev, "iommu sec: pgtable size: %zu\n", psize);
  517. attrs = DMA_ATTR_NO_KERNEL_MAPPING;
  518. cpu_addr = dma_alloc_attrs(dev, psize, &paddr, GFP_KERNEL, attrs);
  519. if (!cpu_addr) {
  520. dev_err(dev, "failed to allocate %zu bytes for pgtable\n",
  521. psize);
  522. return -ENOMEM;
  523. }
  524. ret = qcom_scm_iommu_secure_ptbl_init(paddr, psize, spare);
  525. if (ret) {
  526. dev_err(dev, "failed to init iommu pgtable (%d)\n", ret);
  527. goto free_mem;
  528. }
  529. allocated = true;
  530. return 0;
  531. free_mem:
  532. dma_free_attrs(dev, psize, cpu_addr, paddr, attrs);
  533. return ret;
  534. }
  535. static int get_asid(const struct device_node *np)
  536. {
  537. u32 reg;
  538. /* read the "reg" property directly to get the relative address
  539. * of the context bank, and calculate the asid from that:
  540. */
  541. if (of_property_read_u32_index(np, "reg", 0, &reg))
  542. return -ENODEV;
  543. return reg / 0x1000; /* context banks are 0x1000 apart */
  544. }
  545. static int qcom_iommu_ctx_probe(struct platform_device *pdev)
  546. {
  547. struct qcom_iommu_ctx *ctx;
  548. struct device *dev = &pdev->dev;
  549. struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(dev->parent);
  550. struct resource *res;
  551. int ret, irq;
  552. ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
  553. if (!ctx)
  554. return -ENOMEM;
  555. ctx->dev = dev;
  556. platform_set_drvdata(pdev, ctx);
  557. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  558. ctx->base = devm_ioremap_resource(dev, res);
  559. if (IS_ERR(ctx->base))
  560. return PTR_ERR(ctx->base);
  561. irq = platform_get_irq(pdev, 0);
  562. if (irq < 0) {
  563. dev_err(dev, "failed to get irq\n");
  564. return -ENODEV;
  565. }
  566. /* clear IRQs before registering fault handler, just in case the
  567. * boot-loader left us a surprise:
  568. */
  569. iommu_writel(ctx, ARM_SMMU_CB_FSR, iommu_readl(ctx, ARM_SMMU_CB_FSR));
  570. ret = devm_request_irq(dev, irq,
  571. qcom_iommu_fault,
  572. IRQF_SHARED,
  573. "qcom-iommu-fault",
  574. ctx);
  575. if (ret) {
  576. dev_err(dev, "failed to request IRQ %u\n", irq);
  577. return ret;
  578. }
  579. ret = get_asid(dev->of_node);
  580. if (ret < 0) {
  581. dev_err(dev, "missing reg property\n");
  582. return ret;
  583. }
  584. ctx->asid = ret;
  585. dev_dbg(dev, "found asid %u\n", ctx->asid);
  586. qcom_iommu->ctxs[ctx->asid - 1] = ctx;
  587. return 0;
  588. }
  589. static int qcom_iommu_ctx_remove(struct platform_device *pdev)
  590. {
  591. struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(pdev->dev.parent);
  592. struct qcom_iommu_ctx *ctx = platform_get_drvdata(pdev);
  593. platform_set_drvdata(pdev, NULL);
  594. qcom_iommu->ctxs[ctx->asid - 1] = NULL;
  595. return 0;
  596. }
  597. static const struct of_device_id ctx_of_match[] = {
  598. { .compatible = "qcom,msm-iommu-v1-ns" },
  599. { .compatible = "qcom,msm-iommu-v1-sec" },
  600. { /* sentinel */ }
  601. };
  602. static struct platform_driver qcom_iommu_ctx_driver = {
  603. .driver = {
  604. .name = "qcom-iommu-ctx",
  605. .of_match_table = of_match_ptr(ctx_of_match),
  606. },
  607. .probe = qcom_iommu_ctx_probe,
  608. .remove = qcom_iommu_ctx_remove,
  609. };
  610. static bool qcom_iommu_has_secure_context(struct qcom_iommu_dev *qcom_iommu)
  611. {
  612. struct device_node *child;
  613. for_each_child_of_node(qcom_iommu->dev->of_node, child)
  614. if (of_device_is_compatible(child, "qcom,msm-iommu-v1-sec"))
  615. return true;
  616. return false;
  617. }
  618. static int qcom_iommu_device_probe(struct platform_device *pdev)
  619. {
  620. struct device_node *child;
  621. struct qcom_iommu_dev *qcom_iommu;
  622. struct device *dev = &pdev->dev;
  623. struct resource *res;
  624. int ret, sz, max_asid = 0;
  625. /* find the max asid (which is 1:1 to ctx bank idx), so we know how
  626. * many child ctx devices we have:
  627. */
  628. for_each_child_of_node(dev->of_node, child)
  629. max_asid = max(max_asid, get_asid(child));
  630. sz = sizeof(*qcom_iommu) + (max_asid * sizeof(qcom_iommu->ctxs[0]));
  631. qcom_iommu = devm_kzalloc(dev, sz, GFP_KERNEL);
  632. if (!qcom_iommu)
  633. return -ENOMEM;
  634. qcom_iommu->num_ctxs = max_asid;
  635. qcom_iommu->dev = dev;
  636. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  637. if (res)
  638. qcom_iommu->local_base = devm_ioremap_resource(dev, res);
  639. qcom_iommu->iface_clk = devm_clk_get(dev, "iface");
  640. if (IS_ERR(qcom_iommu->iface_clk)) {
  641. dev_err(dev, "failed to get iface clock\n");
  642. return PTR_ERR(qcom_iommu->iface_clk);
  643. }
  644. qcom_iommu->bus_clk = devm_clk_get(dev, "bus");
  645. if (IS_ERR(qcom_iommu->bus_clk)) {
  646. dev_err(dev, "failed to get bus clock\n");
  647. return PTR_ERR(qcom_iommu->bus_clk);
  648. }
  649. if (of_property_read_u32(dev->of_node, "qcom,iommu-secure-id",
  650. &qcom_iommu->sec_id)) {
  651. dev_err(dev, "missing qcom,iommu-secure-id property\n");
  652. return -ENODEV;
  653. }
  654. if (qcom_iommu_has_secure_context(qcom_iommu)) {
  655. ret = qcom_iommu_sec_ptbl_init(dev);
  656. if (ret) {
  657. dev_err(dev, "cannot init secure pg table(%d)\n", ret);
  658. return ret;
  659. }
  660. }
  661. platform_set_drvdata(pdev, qcom_iommu);
  662. pm_runtime_enable(dev);
  663. /* register context bank devices, which are child nodes: */
  664. ret = devm_of_platform_populate(dev);
  665. if (ret) {
  666. dev_err(dev, "Failed to populate iommu contexts\n");
  667. return ret;
  668. }
  669. ret = iommu_device_sysfs_add(&qcom_iommu->iommu, dev, NULL,
  670. dev_name(dev));
  671. if (ret) {
  672. dev_err(dev, "Failed to register iommu in sysfs\n");
  673. return ret;
  674. }
  675. iommu_device_set_ops(&qcom_iommu->iommu, &qcom_iommu_ops);
  676. iommu_device_set_fwnode(&qcom_iommu->iommu, dev->fwnode);
  677. ret = iommu_device_register(&qcom_iommu->iommu);
  678. if (ret) {
  679. dev_err(dev, "Failed to register iommu\n");
  680. return ret;
  681. }
  682. bus_set_iommu(&platform_bus_type, &qcom_iommu_ops);
  683. if (qcom_iommu->local_base) {
  684. pm_runtime_get_sync(dev);
  685. writel_relaxed(0xffffffff, qcom_iommu->local_base + SMMU_INTR_SEL_NS);
  686. pm_runtime_put_sync(dev);
  687. }
  688. return 0;
  689. }
  690. static int qcom_iommu_device_remove(struct platform_device *pdev)
  691. {
  692. struct qcom_iommu_dev *qcom_iommu = platform_get_drvdata(pdev);
  693. bus_set_iommu(&platform_bus_type, NULL);
  694. pm_runtime_force_suspend(&pdev->dev);
  695. platform_set_drvdata(pdev, NULL);
  696. iommu_device_sysfs_remove(&qcom_iommu->iommu);
  697. iommu_device_unregister(&qcom_iommu->iommu);
  698. return 0;
  699. }
  700. static int __maybe_unused qcom_iommu_resume(struct device *dev)
  701. {
  702. struct platform_device *pdev = to_platform_device(dev);
  703. struct qcom_iommu_dev *qcom_iommu = platform_get_drvdata(pdev);
  704. return qcom_iommu_enable_clocks(qcom_iommu);
  705. }
  706. static int __maybe_unused qcom_iommu_suspend(struct device *dev)
  707. {
  708. struct platform_device *pdev = to_platform_device(dev);
  709. struct qcom_iommu_dev *qcom_iommu = platform_get_drvdata(pdev);
  710. qcom_iommu_disable_clocks(qcom_iommu);
  711. return 0;
  712. }
  713. static const struct dev_pm_ops qcom_iommu_pm_ops = {
  714. SET_RUNTIME_PM_OPS(qcom_iommu_suspend, qcom_iommu_resume, NULL)
  715. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  716. pm_runtime_force_resume)
  717. };
  718. static const struct of_device_id qcom_iommu_of_match[] = {
  719. { .compatible = "qcom,msm-iommu-v1" },
  720. { /* sentinel */ }
  721. };
  722. MODULE_DEVICE_TABLE(of, qcom_iommu_of_match);
  723. static struct platform_driver qcom_iommu_driver = {
  724. .driver = {
  725. .name = "qcom-iommu",
  726. .of_match_table = of_match_ptr(qcom_iommu_of_match),
  727. .pm = &qcom_iommu_pm_ops,
  728. },
  729. .probe = qcom_iommu_device_probe,
  730. .remove = qcom_iommu_device_remove,
  731. };
  732. static int __init qcom_iommu_init(void)
  733. {
  734. int ret;
  735. ret = platform_driver_register(&qcom_iommu_ctx_driver);
  736. if (ret)
  737. return ret;
  738. ret = platform_driver_register(&qcom_iommu_driver);
  739. if (ret)
  740. platform_driver_unregister(&qcom_iommu_ctx_driver);
  741. return ret;
  742. }
  743. static void __exit qcom_iommu_exit(void)
  744. {
  745. platform_driver_unregister(&qcom_iommu_driver);
  746. platform_driver_unregister(&qcom_iommu_ctx_driver);
  747. }
  748. module_init(qcom_iommu_init);
  749. module_exit(qcom_iommu_exit);
  750. IOMMU_OF_DECLARE(qcom_iommu_dev, "qcom,msm-iommu-v1", NULL);
  751. MODULE_DESCRIPTION("IOMMU API for QCOM IOMMU v1 implementations");
  752. MODULE_LICENSE("GPL v2");