qcom_iommu.c 24 KB

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