]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/iommu/qcom_iommu.c
Merge tag 'trace-v5.6-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux.git] / drivers / iommu / qcom_iommu.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * IOMMU API for QCOM secure IOMMUs.  Somewhat based on arm-smmu.c
4  *
5  * Copyright (C) 2013 ARM Limited
6  * Copyright (C) 2017 Red Hat
7  */
8
9 #include <linux/atomic.h>
10 #include <linux/bitfield.h>
11 #include <linux/clk.h>
12 #include <linux/delay.h>
13 #include <linux/dma-iommu.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/err.h>
16 #include <linux/interrupt.h>
17 #include <linux/io.h>
18 #include <linux/io-64-nonatomic-hi-lo.h>
19 #include <linux/io-pgtable.h>
20 #include <linux/iommu.h>
21 #include <linux/iopoll.h>
22 #include <linux/kconfig.h>
23 #include <linux/init.h>
24 #include <linux/mutex.h>
25 #include <linux/of.h>
26 #include <linux/of_address.h>
27 #include <linux/of_device.h>
28 #include <linux/of_iommu.h>
29 #include <linux/platform_device.h>
30 #include <linux/pm.h>
31 #include <linux/pm_runtime.h>
32 #include <linux/qcom_scm.h>
33 #include <linux/slab.h>
34 #include <linux/spinlock.h>
35
36 #include "arm-smmu.h"
37
38 #define SMMU_INTR_SEL_NS     0x2000
39
40 struct qcom_iommu_ctx;
41
42 struct qcom_iommu_dev {
43         /* IOMMU core code handle */
44         struct iommu_device      iommu;
45         struct device           *dev;
46         struct clk              *iface_clk;
47         struct clk              *bus_clk;
48         void __iomem            *local_base;
49         u32                      sec_id;
50         u8                       num_ctxs;
51         struct qcom_iommu_ctx   *ctxs[0];   /* indexed by asid-1 */
52 };
53
54 struct qcom_iommu_ctx {
55         struct device           *dev;
56         void __iomem            *base;
57         bool                     secure_init;
58         u8                       asid;      /* asid and ctx bank # are 1:1 */
59         struct iommu_domain     *domain;
60 };
61
62 struct qcom_iommu_domain {
63         struct io_pgtable_ops   *pgtbl_ops;
64         spinlock_t               pgtbl_lock;
65         struct mutex             init_mutex; /* Protects iommu pointer */
66         struct iommu_domain      domain;
67         struct qcom_iommu_dev   *iommu;
68 };
69
70 static struct qcom_iommu_domain *to_qcom_iommu_domain(struct iommu_domain *dom)
71 {
72         return container_of(dom, struct qcom_iommu_domain, domain);
73 }
74
75 static const struct iommu_ops qcom_iommu_ops;
76
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
84 static struct qcom_iommu_ctx * to_ctx(struct iommu_fwspec *fwspec, unsigned asid)
85 {
86         struct qcom_iommu_dev *qcom_iommu = to_iommu(fwspec);
87         if (!qcom_iommu)
88                 return NULL;
89         return qcom_iommu->ctxs[asid - 1];
90 }
91
92 static inline void
93 iommu_writel(struct qcom_iommu_ctx *ctx, unsigned reg, u32 val)
94 {
95         writel_relaxed(val, ctx->base + reg);
96 }
97
98 static inline void
99 iommu_writeq(struct qcom_iommu_ctx *ctx, unsigned reg, u64 val)
100 {
101         writeq_relaxed(val, ctx->base + reg);
102 }
103
104 static inline u32
105 iommu_readl(struct qcom_iommu_ctx *ctx, unsigned reg)
106 {
107         return readl_relaxed(ctx->base + reg);
108 }
109
110 static inline u64
111 iommu_readq(struct qcom_iommu_ctx *ctx, unsigned reg)
112 {
113         return readq_relaxed(ctx->base + reg);
114 }
115
116 static void qcom_iommu_tlb_sync(void *cookie)
117 {
118         struct iommu_fwspec *fwspec = cookie;
119         unsigned i;
120
121         for (i = 0; i < fwspec->num_ids; i++) {
122                 struct qcom_iommu_ctx *ctx = to_ctx(fwspec, fwspec->ids[i]);
123                 unsigned int val, ret;
124
125                 iommu_writel(ctx, ARM_SMMU_CB_TLBSYNC, 0);
126
127                 ret = readl_poll_timeout(ctx->base + ARM_SMMU_CB_TLBSTATUS, val,
128                                          (val & 0x1) == 0, 0, 5000000);
129                 if (ret)
130                         dev_err(ctx->dev, "timeout waiting for TLB SYNC\n");
131         }
132 }
133
134 static void qcom_iommu_tlb_inv_context(void *cookie)
135 {
136         struct iommu_fwspec *fwspec = cookie;
137         unsigned i;
138
139         for (i = 0; i < fwspec->num_ids; i++) {
140                 struct qcom_iommu_ctx *ctx = to_ctx(fwspec, fwspec->ids[i]);
141                 iommu_writel(ctx, ARM_SMMU_CB_S1_TLBIASID, ctx->asid);
142         }
143
144         qcom_iommu_tlb_sync(cookie);
145 }
146
147 static void qcom_iommu_tlb_inv_range_nosync(unsigned long iova, size_t size,
148                                             size_t granule, bool leaf, void *cookie)
149 {
150         struct iommu_fwspec *fwspec = cookie;
151         unsigned i, reg;
152
153         reg = leaf ? ARM_SMMU_CB_S1_TLBIVAL : ARM_SMMU_CB_S1_TLBIVA;
154
155         for (i = 0; i < fwspec->num_ids; i++) {
156                 struct qcom_iommu_ctx *ctx = to_ctx(fwspec, fwspec->ids[i]);
157                 size_t s = size;
158
159                 iova = (iova >> 12) << 12;
160                 iova |= ctx->asid;
161                 do {
162                         iommu_writel(ctx, reg, iova);
163                         iova += granule;
164                 } while (s -= granule);
165         }
166 }
167
168 static void qcom_iommu_tlb_flush_walk(unsigned long iova, size_t size,
169                                       size_t granule, void *cookie)
170 {
171         qcom_iommu_tlb_inv_range_nosync(iova, size, granule, false, cookie);
172         qcom_iommu_tlb_sync(cookie);
173 }
174
175 static void qcom_iommu_tlb_flush_leaf(unsigned long iova, size_t size,
176                                       size_t granule, void *cookie)
177 {
178         qcom_iommu_tlb_inv_range_nosync(iova, size, granule, true, cookie);
179         qcom_iommu_tlb_sync(cookie);
180 }
181
182 static void qcom_iommu_tlb_add_page(struct iommu_iotlb_gather *gather,
183                                     unsigned long iova, size_t granule,
184                                     void *cookie)
185 {
186         qcom_iommu_tlb_inv_range_nosync(iova, granule, granule, true, cookie);
187 }
188
189 static const struct iommu_flush_ops qcom_flush_ops = {
190         .tlb_flush_all  = qcom_iommu_tlb_inv_context,
191         .tlb_flush_walk = qcom_iommu_tlb_flush_walk,
192         .tlb_flush_leaf = qcom_iommu_tlb_flush_leaf,
193         .tlb_add_page   = qcom_iommu_tlb_add_page,
194 };
195
196 static irqreturn_t qcom_iommu_fault(int irq, void *dev)
197 {
198         struct qcom_iommu_ctx *ctx = dev;
199         u32 fsr, fsynr;
200         u64 iova;
201
202         fsr = iommu_readl(ctx, ARM_SMMU_CB_FSR);
203
204         if (!(fsr & ARM_SMMU_FSR_FAULT))
205                 return IRQ_NONE;
206
207         fsynr = iommu_readl(ctx, ARM_SMMU_CB_FSYNR0);
208         iova = iommu_readq(ctx, ARM_SMMU_CB_FAR);
209
210         if (!report_iommu_fault(ctx->domain, ctx->dev, iova, 0)) {
211                 dev_err_ratelimited(ctx->dev,
212                                     "Unhandled context fault: fsr=0x%x, "
213                                     "iova=0x%016llx, fsynr=0x%x, cb=%d\n",
214                                     fsr, iova, fsynr, ctx->asid);
215         }
216
217         iommu_writel(ctx, ARM_SMMU_CB_FSR, fsr);
218         iommu_writel(ctx, ARM_SMMU_CB_RESUME, ARM_SMMU_RESUME_TERMINATE);
219
220         return IRQ_HANDLED;
221 }
222
223 static int qcom_iommu_init_domain(struct iommu_domain *domain,
224                                   struct qcom_iommu_dev *qcom_iommu,
225                                   struct iommu_fwspec *fwspec)
226 {
227         struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
228         struct io_pgtable_ops *pgtbl_ops;
229         struct io_pgtable_cfg pgtbl_cfg;
230         int i, ret = 0;
231         u32 reg;
232
233         mutex_lock(&qcom_domain->init_mutex);
234         if (qcom_domain->iommu)
235                 goto out_unlock;
236
237         pgtbl_cfg = (struct io_pgtable_cfg) {
238                 .pgsize_bitmap  = qcom_iommu_ops.pgsize_bitmap,
239                 .ias            = 32,
240                 .oas            = 40,
241                 .tlb            = &qcom_flush_ops,
242                 .iommu_dev      = qcom_iommu->dev,
243         };
244
245         qcom_domain->iommu = qcom_iommu;
246         pgtbl_ops = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &pgtbl_cfg, fwspec);
247         if (!pgtbl_ops) {
248                 dev_err(qcom_iommu->dev, "failed to allocate pagetable ops\n");
249                 ret = -ENOMEM;
250                 goto out_clear_iommu;
251         }
252
253         /* Update the domain's page sizes to reflect the page table format */
254         domain->pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;
255         domain->geometry.aperture_end = (1ULL << pgtbl_cfg.ias) - 1;
256         domain->geometry.force_aperture = true;
257
258         for (i = 0; i < fwspec->num_ids; i++) {
259                 struct qcom_iommu_ctx *ctx = to_ctx(fwspec, fwspec->ids[i]);
260
261                 if (!ctx->secure_init) {
262                         ret = qcom_scm_restore_sec_cfg(qcom_iommu->sec_id, ctx->asid);
263                         if (ret) {
264                                 dev_err(qcom_iommu->dev, "secure init failed: %d\n", ret);
265                                 goto out_clear_iommu;
266                         }
267                         ctx->secure_init = true;
268                 }
269
270                 /* TTBRs */
271                 iommu_writeq(ctx, ARM_SMMU_CB_TTBR0,
272                                 pgtbl_cfg.arm_lpae_s1_cfg.ttbr |
273                                 FIELD_PREP(ARM_SMMU_TTBRn_ASID, ctx->asid));
274                 iommu_writeq(ctx, ARM_SMMU_CB_TTBR1, 0);
275
276                 /* TCR */
277                 iommu_writel(ctx, ARM_SMMU_CB_TCR2,
278                                 arm_smmu_lpae_tcr2(&pgtbl_cfg));
279                 iommu_writel(ctx, ARM_SMMU_CB_TCR,
280                              arm_smmu_lpae_tcr(&pgtbl_cfg) | ARM_SMMU_TCR_EAE);
281
282                 /* MAIRs (stage-1 only) */
283                 iommu_writel(ctx, ARM_SMMU_CB_S1_MAIR0,
284                                 pgtbl_cfg.arm_lpae_s1_cfg.mair);
285                 iommu_writel(ctx, ARM_SMMU_CB_S1_MAIR1,
286                                 pgtbl_cfg.arm_lpae_s1_cfg.mair >> 32);
287
288                 /* SCTLR */
289                 reg = ARM_SMMU_SCTLR_CFIE | ARM_SMMU_SCTLR_CFRE |
290                       ARM_SMMU_SCTLR_AFE | ARM_SMMU_SCTLR_TRE |
291                       ARM_SMMU_SCTLR_M | ARM_SMMU_SCTLR_S1_ASIDPNE |
292                       ARM_SMMU_SCTLR_CFCFG;
293
294                 if (IS_ENABLED(CONFIG_BIG_ENDIAN))
295                         reg |= ARM_SMMU_SCTLR_E;
296
297                 iommu_writel(ctx, ARM_SMMU_CB_SCTLR, reg);
298
299                 ctx->domain = domain;
300         }
301
302         mutex_unlock(&qcom_domain->init_mutex);
303
304         /* Publish page table ops for map/unmap */
305         qcom_domain->pgtbl_ops = pgtbl_ops;
306
307         return 0;
308
309 out_clear_iommu:
310         qcom_domain->iommu = NULL;
311 out_unlock:
312         mutex_unlock(&qcom_domain->init_mutex);
313         return ret;
314 }
315
316 static struct iommu_domain *qcom_iommu_domain_alloc(unsigned type)
317 {
318         struct qcom_iommu_domain *qcom_domain;
319
320         if (type != IOMMU_DOMAIN_UNMANAGED && type != IOMMU_DOMAIN_DMA)
321                 return NULL;
322         /*
323          * Allocate the domain and initialise some of its data structures.
324          * We can't really do anything meaningful until we've added a
325          * master.
326          */
327         qcom_domain = kzalloc(sizeof(*qcom_domain), GFP_KERNEL);
328         if (!qcom_domain)
329                 return NULL;
330
331         if (type == IOMMU_DOMAIN_DMA &&
332             iommu_get_dma_cookie(&qcom_domain->domain)) {
333                 kfree(qcom_domain);
334                 return NULL;
335         }
336
337         mutex_init(&qcom_domain->init_mutex);
338         spin_lock_init(&qcom_domain->pgtbl_lock);
339
340         return &qcom_domain->domain;
341 }
342
343 static void qcom_iommu_domain_free(struct iommu_domain *domain)
344 {
345         struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
346
347         if (WARN_ON(qcom_domain->iommu))    /* forgot to detach? */
348                 return;
349
350         iommu_put_dma_cookie(domain);
351
352         /* NOTE: unmap can be called after client device is powered off,
353          * for example, with GPUs or anything involving dma-buf.  So we
354          * cannot rely on the device_link.  Make sure the IOMMU is on to
355          * avoid unclocked accesses in the TLB inv path:
356          */
357         pm_runtime_get_sync(qcom_domain->iommu->dev);
358
359         free_io_pgtable_ops(qcom_domain->pgtbl_ops);
360
361         pm_runtime_put_sync(qcom_domain->iommu->dev);
362
363         kfree(qcom_domain);
364 }
365
366 static int qcom_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
367 {
368         struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
369         struct qcom_iommu_dev *qcom_iommu = to_iommu(fwspec);
370         struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
371         int ret;
372
373         if (!qcom_iommu) {
374                 dev_err(dev, "cannot attach to IOMMU, is it on the same bus?\n");
375                 return -ENXIO;
376         }
377
378         /* Ensure that the domain is finalized */
379         pm_runtime_get_sync(qcom_iommu->dev);
380         ret = qcom_iommu_init_domain(domain, qcom_iommu, fwspec);
381         pm_runtime_put_sync(qcom_iommu->dev);
382         if (ret < 0)
383                 return ret;
384
385         /*
386          * Sanity check the domain. We don't support domains across
387          * different IOMMUs.
388          */
389         if (qcom_domain->iommu != qcom_iommu) {
390                 dev_err(dev, "cannot attach to IOMMU %s while already "
391                         "attached to domain on IOMMU %s\n",
392                         dev_name(qcom_domain->iommu->dev),
393                         dev_name(qcom_iommu->dev));
394                 return -EINVAL;
395         }
396
397         return 0;
398 }
399
400 static void qcom_iommu_detach_dev(struct iommu_domain *domain, struct device *dev)
401 {
402         struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
403         struct qcom_iommu_dev *qcom_iommu = to_iommu(fwspec);
404         struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
405         unsigned i;
406
407         if (!qcom_domain->iommu)
408                 return;
409
410         pm_runtime_get_sync(qcom_iommu->dev);
411         for (i = 0; i < fwspec->num_ids; i++) {
412                 struct qcom_iommu_ctx *ctx = to_ctx(fwspec, fwspec->ids[i]);
413
414                 /* Disable the context bank: */
415                 iommu_writel(ctx, ARM_SMMU_CB_SCTLR, 0);
416
417                 ctx->domain = NULL;
418         }
419         pm_runtime_put_sync(qcom_iommu->dev);
420
421         qcom_domain->iommu = NULL;
422 }
423
424 static int qcom_iommu_map(struct iommu_domain *domain, unsigned long iova,
425                           phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
426 {
427         int ret;
428         unsigned long flags;
429         struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
430         struct io_pgtable_ops *ops = qcom_domain->pgtbl_ops;
431
432         if (!ops)
433                 return -ENODEV;
434
435         spin_lock_irqsave(&qcom_domain->pgtbl_lock, flags);
436         ret = ops->map(ops, iova, paddr, size, prot);
437         spin_unlock_irqrestore(&qcom_domain->pgtbl_lock, flags);
438         return ret;
439 }
440
441 static size_t qcom_iommu_unmap(struct iommu_domain *domain, unsigned long iova,
442                                size_t size, struct iommu_iotlb_gather *gather)
443 {
444         size_t ret;
445         unsigned long flags;
446         struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
447         struct io_pgtable_ops *ops = qcom_domain->pgtbl_ops;
448
449         if (!ops)
450                 return 0;
451
452         /* NOTE: unmap can be called after client device is powered off,
453          * for example, with GPUs or anything involving dma-buf.  So we
454          * cannot rely on the device_link.  Make sure the IOMMU is on to
455          * avoid unclocked accesses in the TLB inv path:
456          */
457         pm_runtime_get_sync(qcom_domain->iommu->dev);
458         spin_lock_irqsave(&qcom_domain->pgtbl_lock, flags);
459         ret = ops->unmap(ops, iova, size, gather);
460         spin_unlock_irqrestore(&qcom_domain->pgtbl_lock, flags);
461         pm_runtime_put_sync(qcom_domain->iommu->dev);
462
463         return ret;
464 }
465
466 static void qcom_iommu_flush_iotlb_all(struct iommu_domain *domain)
467 {
468         struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
469         struct io_pgtable *pgtable = container_of(qcom_domain->pgtbl_ops,
470                                                   struct io_pgtable, ops);
471         if (!qcom_domain->pgtbl_ops)
472                 return;
473
474         pm_runtime_get_sync(qcom_domain->iommu->dev);
475         qcom_iommu_tlb_sync(pgtable->cookie);
476         pm_runtime_put_sync(qcom_domain->iommu->dev);
477 }
478
479 static void qcom_iommu_iotlb_sync(struct iommu_domain *domain,
480                                   struct iommu_iotlb_gather *gather)
481 {
482         qcom_iommu_flush_iotlb_all(domain);
483 }
484
485 static phys_addr_t qcom_iommu_iova_to_phys(struct iommu_domain *domain,
486                                            dma_addr_t iova)
487 {
488         phys_addr_t ret;
489         unsigned long flags;
490         struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
491         struct io_pgtable_ops *ops = qcom_domain->pgtbl_ops;
492
493         if (!ops)
494                 return 0;
495
496         spin_lock_irqsave(&qcom_domain->pgtbl_lock, flags);
497         ret = ops->iova_to_phys(ops, iova);
498         spin_unlock_irqrestore(&qcom_domain->pgtbl_lock, flags);
499
500         return ret;
501 }
502
503 static bool qcom_iommu_capable(enum iommu_cap cap)
504 {
505         switch (cap) {
506         case IOMMU_CAP_CACHE_COHERENCY:
507                 /*
508                  * Return true here as the SMMU can always send out coherent
509                  * requests.
510                  */
511                 return true;
512         case IOMMU_CAP_NOEXEC:
513                 return true;
514         default:
515                 return false;
516         }
517 }
518
519 static int qcom_iommu_add_device(struct device *dev)
520 {
521         struct qcom_iommu_dev *qcom_iommu = to_iommu(dev_iommu_fwspec_get(dev));
522         struct iommu_group *group;
523         struct device_link *link;
524
525         if (!qcom_iommu)
526                 return -ENODEV;
527
528         /*
529          * Establish the link between iommu and master, so that the
530          * iommu gets runtime enabled/disabled as per the master's
531          * needs.
532          */
533         link = device_link_add(dev, qcom_iommu->dev, DL_FLAG_PM_RUNTIME);
534         if (!link) {
535                 dev_err(qcom_iommu->dev, "Unable to create device link between %s and %s\n",
536                         dev_name(qcom_iommu->dev), dev_name(dev));
537                 return -ENODEV;
538         }
539
540         group = iommu_group_get_for_dev(dev);
541         if (IS_ERR(group))
542                 return PTR_ERR(group);
543
544         iommu_group_put(group);
545         iommu_device_link(&qcom_iommu->iommu, dev);
546
547         return 0;
548 }
549
550 static void qcom_iommu_remove_device(struct device *dev)
551 {
552         struct qcom_iommu_dev *qcom_iommu = to_iommu(dev_iommu_fwspec_get(dev));
553
554         if (!qcom_iommu)
555                 return;
556
557         iommu_device_unlink(&qcom_iommu->iommu, dev);
558         iommu_group_remove_device(dev);
559         iommu_fwspec_free(dev);
560 }
561
562 static int qcom_iommu_of_xlate(struct device *dev, struct of_phandle_args *args)
563 {
564         struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
565         struct qcom_iommu_dev *qcom_iommu;
566         struct platform_device *iommu_pdev;
567         unsigned asid = args->args[0];
568
569         if (args->args_count != 1) {
570                 dev_err(dev, "incorrect number of iommu params found for %s "
571                         "(found %d, expected 1)\n",
572                         args->np->full_name, args->args_count);
573                 return -EINVAL;
574         }
575
576         iommu_pdev = of_find_device_by_node(args->np);
577         if (WARN_ON(!iommu_pdev))
578                 return -EINVAL;
579
580         qcom_iommu = platform_get_drvdata(iommu_pdev);
581
582         /* make sure the asid specified in dt is valid, so we don't have
583          * to sanity check this elsewhere, since 'asid - 1' is used to
584          * index into qcom_iommu->ctxs:
585          */
586         if (WARN_ON(asid < 1) ||
587             WARN_ON(asid > qcom_iommu->num_ctxs))
588                 return -EINVAL;
589
590         if (!fwspec->iommu_priv) {
591                 fwspec->iommu_priv = qcom_iommu;
592         } else {
593                 /* make sure devices iommus dt node isn't referring to
594                  * multiple different iommu devices.  Multiple context
595                  * banks are ok, but multiple devices are not:
596                  */
597                 if (WARN_ON(qcom_iommu != fwspec->iommu_priv))
598                         return -EINVAL;
599         }
600
601         return iommu_fwspec_add_ids(dev, &asid, 1);
602 }
603
604 static const struct iommu_ops qcom_iommu_ops = {
605         .capable        = qcom_iommu_capable,
606         .domain_alloc   = qcom_iommu_domain_alloc,
607         .domain_free    = qcom_iommu_domain_free,
608         .attach_dev     = qcom_iommu_attach_dev,
609         .detach_dev     = qcom_iommu_detach_dev,
610         .map            = qcom_iommu_map,
611         .unmap          = qcom_iommu_unmap,
612         .flush_iotlb_all = qcom_iommu_flush_iotlb_all,
613         .iotlb_sync     = qcom_iommu_iotlb_sync,
614         .iova_to_phys   = qcom_iommu_iova_to_phys,
615         .add_device     = qcom_iommu_add_device,
616         .remove_device  = qcom_iommu_remove_device,
617         .device_group   = generic_device_group,
618         .of_xlate       = qcom_iommu_of_xlate,
619         .pgsize_bitmap  = SZ_4K | SZ_64K | SZ_1M | SZ_16M,
620 };
621
622 static int qcom_iommu_enable_clocks(struct qcom_iommu_dev *qcom_iommu)
623 {
624         int ret;
625
626         ret = clk_prepare_enable(qcom_iommu->iface_clk);
627         if (ret) {
628                 dev_err(qcom_iommu->dev, "Couldn't enable iface_clk\n");
629                 return ret;
630         }
631
632         ret = clk_prepare_enable(qcom_iommu->bus_clk);
633         if (ret) {
634                 dev_err(qcom_iommu->dev, "Couldn't enable bus_clk\n");
635                 clk_disable_unprepare(qcom_iommu->iface_clk);
636                 return ret;
637         }
638
639         return 0;
640 }
641
642 static void qcom_iommu_disable_clocks(struct qcom_iommu_dev *qcom_iommu)
643 {
644         clk_disable_unprepare(qcom_iommu->bus_clk);
645         clk_disable_unprepare(qcom_iommu->iface_clk);
646 }
647
648 static int qcom_iommu_sec_ptbl_init(struct device *dev)
649 {
650         size_t psize = 0;
651         unsigned int spare = 0;
652         void *cpu_addr;
653         dma_addr_t paddr;
654         unsigned long attrs;
655         static bool allocated = false;
656         int ret;
657
658         if (allocated)
659                 return 0;
660
661         ret = qcom_scm_iommu_secure_ptbl_size(spare, &psize);
662         if (ret) {
663                 dev_err(dev, "failed to get iommu secure pgtable size (%d)\n",
664                         ret);
665                 return ret;
666         }
667
668         dev_info(dev, "iommu sec: pgtable size: %zu\n", psize);
669
670         attrs = DMA_ATTR_NO_KERNEL_MAPPING;
671
672         cpu_addr = dma_alloc_attrs(dev, psize, &paddr, GFP_KERNEL, attrs);
673         if (!cpu_addr) {
674                 dev_err(dev, "failed to allocate %zu bytes for pgtable\n",
675                         psize);
676                 return -ENOMEM;
677         }
678
679         ret = qcom_scm_iommu_secure_ptbl_init(paddr, psize, spare);
680         if (ret) {
681                 dev_err(dev, "failed to init iommu pgtable (%d)\n", ret);
682                 goto free_mem;
683         }
684
685         allocated = true;
686         return 0;
687
688 free_mem:
689         dma_free_attrs(dev, psize, cpu_addr, paddr, attrs);
690         return ret;
691 }
692
693 static int get_asid(const struct device_node *np)
694 {
695         u32 reg;
696
697         /* read the "reg" property directly to get the relative address
698          * of the context bank, and calculate the asid from that:
699          */
700         if (of_property_read_u32_index(np, "reg", 0, &reg))
701                 return -ENODEV;
702
703         return reg / 0x1000;      /* context banks are 0x1000 apart */
704 }
705
706 static int qcom_iommu_ctx_probe(struct platform_device *pdev)
707 {
708         struct qcom_iommu_ctx *ctx;
709         struct device *dev = &pdev->dev;
710         struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(dev->parent);
711         struct resource *res;
712         int ret, irq;
713
714         ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
715         if (!ctx)
716                 return -ENOMEM;
717
718         ctx->dev = dev;
719         platform_set_drvdata(pdev, ctx);
720
721         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
722         ctx->base = devm_ioremap_resource(dev, res);
723         if (IS_ERR(ctx->base))
724                 return PTR_ERR(ctx->base);
725
726         irq = platform_get_irq(pdev, 0);
727         if (irq < 0)
728                 return -ENODEV;
729
730         /* clear IRQs before registering fault handler, just in case the
731          * boot-loader left us a surprise:
732          */
733         iommu_writel(ctx, ARM_SMMU_CB_FSR, iommu_readl(ctx, ARM_SMMU_CB_FSR));
734
735         ret = devm_request_irq(dev, irq,
736                                qcom_iommu_fault,
737                                IRQF_SHARED,
738                                "qcom-iommu-fault",
739                                ctx);
740         if (ret) {
741                 dev_err(dev, "failed to request IRQ %u\n", irq);
742                 return ret;
743         }
744
745         ret = get_asid(dev->of_node);
746         if (ret < 0) {
747                 dev_err(dev, "missing reg property\n");
748                 return ret;
749         }
750
751         ctx->asid = ret;
752
753         dev_dbg(dev, "found asid %u\n", ctx->asid);
754
755         qcom_iommu->ctxs[ctx->asid - 1] = ctx;
756
757         return 0;
758 }
759
760 static int qcom_iommu_ctx_remove(struct platform_device *pdev)
761 {
762         struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(pdev->dev.parent);
763         struct qcom_iommu_ctx *ctx = platform_get_drvdata(pdev);
764
765         platform_set_drvdata(pdev, NULL);
766
767         qcom_iommu->ctxs[ctx->asid - 1] = NULL;
768
769         return 0;
770 }
771
772 static const struct of_device_id ctx_of_match[] = {
773         { .compatible = "qcom,msm-iommu-v1-ns" },
774         { .compatible = "qcom,msm-iommu-v1-sec" },
775         { /* sentinel */ }
776 };
777
778 static struct platform_driver qcom_iommu_ctx_driver = {
779         .driver = {
780                 .name           = "qcom-iommu-ctx",
781                 .of_match_table = of_match_ptr(ctx_of_match),
782         },
783         .probe  = qcom_iommu_ctx_probe,
784         .remove = qcom_iommu_ctx_remove,
785 };
786
787 static bool qcom_iommu_has_secure_context(struct qcom_iommu_dev *qcom_iommu)
788 {
789         struct device_node *child;
790
791         for_each_child_of_node(qcom_iommu->dev->of_node, child)
792                 if (of_device_is_compatible(child, "qcom,msm-iommu-v1-sec"))
793                         return true;
794
795         return false;
796 }
797
798 static int qcom_iommu_device_probe(struct platform_device *pdev)
799 {
800         struct device_node *child;
801         struct qcom_iommu_dev *qcom_iommu;
802         struct device *dev = &pdev->dev;
803         struct resource *res;
804         int ret, max_asid = 0;
805
806         /* find the max asid (which is 1:1 to ctx bank idx), so we know how
807          * many child ctx devices we have:
808          */
809         for_each_child_of_node(dev->of_node, child)
810                 max_asid = max(max_asid, get_asid(child));
811
812         qcom_iommu = devm_kzalloc(dev, struct_size(qcom_iommu, ctxs, max_asid),
813                                   GFP_KERNEL);
814         if (!qcom_iommu)
815                 return -ENOMEM;
816         qcom_iommu->num_ctxs = max_asid;
817         qcom_iommu->dev = dev;
818
819         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
820         if (res)
821                 qcom_iommu->local_base = devm_ioremap_resource(dev, res);
822
823         qcom_iommu->iface_clk = devm_clk_get(dev, "iface");
824         if (IS_ERR(qcom_iommu->iface_clk)) {
825                 dev_err(dev, "failed to get iface clock\n");
826                 return PTR_ERR(qcom_iommu->iface_clk);
827         }
828
829         qcom_iommu->bus_clk = devm_clk_get(dev, "bus");
830         if (IS_ERR(qcom_iommu->bus_clk)) {
831                 dev_err(dev, "failed to get bus clock\n");
832                 return PTR_ERR(qcom_iommu->bus_clk);
833         }
834
835         if (of_property_read_u32(dev->of_node, "qcom,iommu-secure-id",
836                                  &qcom_iommu->sec_id)) {
837                 dev_err(dev, "missing qcom,iommu-secure-id property\n");
838                 return -ENODEV;
839         }
840
841         if (qcom_iommu_has_secure_context(qcom_iommu)) {
842                 ret = qcom_iommu_sec_ptbl_init(dev);
843                 if (ret) {
844                         dev_err(dev, "cannot init secure pg table(%d)\n", ret);
845                         return ret;
846                 }
847         }
848
849         platform_set_drvdata(pdev, qcom_iommu);
850
851         pm_runtime_enable(dev);
852
853         /* register context bank devices, which are child nodes: */
854         ret = devm_of_platform_populate(dev);
855         if (ret) {
856                 dev_err(dev, "Failed to populate iommu contexts\n");
857                 return ret;
858         }
859
860         ret = iommu_device_sysfs_add(&qcom_iommu->iommu, dev, NULL,
861                                      dev_name(dev));
862         if (ret) {
863                 dev_err(dev, "Failed to register iommu in sysfs\n");
864                 return ret;
865         }
866
867         iommu_device_set_ops(&qcom_iommu->iommu, &qcom_iommu_ops);
868         iommu_device_set_fwnode(&qcom_iommu->iommu, dev->fwnode);
869
870         ret = iommu_device_register(&qcom_iommu->iommu);
871         if (ret) {
872                 dev_err(dev, "Failed to register iommu\n");
873                 return ret;
874         }
875
876         bus_set_iommu(&platform_bus_type, &qcom_iommu_ops);
877
878         if (qcom_iommu->local_base) {
879                 pm_runtime_get_sync(dev);
880                 writel_relaxed(0xffffffff, qcom_iommu->local_base + SMMU_INTR_SEL_NS);
881                 pm_runtime_put_sync(dev);
882         }
883
884         return 0;
885 }
886
887 static int qcom_iommu_device_remove(struct platform_device *pdev)
888 {
889         struct qcom_iommu_dev *qcom_iommu = platform_get_drvdata(pdev);
890
891         bus_set_iommu(&platform_bus_type, NULL);
892
893         pm_runtime_force_suspend(&pdev->dev);
894         platform_set_drvdata(pdev, NULL);
895         iommu_device_sysfs_remove(&qcom_iommu->iommu);
896         iommu_device_unregister(&qcom_iommu->iommu);
897
898         return 0;
899 }
900
901 static int __maybe_unused qcom_iommu_resume(struct device *dev)
902 {
903         struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(dev);
904
905         return qcom_iommu_enable_clocks(qcom_iommu);
906 }
907
908 static int __maybe_unused qcom_iommu_suspend(struct device *dev)
909 {
910         struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(dev);
911
912         qcom_iommu_disable_clocks(qcom_iommu);
913
914         return 0;
915 }
916
917 static const struct dev_pm_ops qcom_iommu_pm_ops = {
918         SET_RUNTIME_PM_OPS(qcom_iommu_suspend, qcom_iommu_resume, NULL)
919         SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
920                                 pm_runtime_force_resume)
921 };
922
923 static const struct of_device_id qcom_iommu_of_match[] = {
924         { .compatible = "qcom,msm-iommu-v1" },
925         { /* sentinel */ }
926 };
927
928 static struct platform_driver qcom_iommu_driver = {
929         .driver = {
930                 .name           = "qcom-iommu",
931                 .of_match_table = of_match_ptr(qcom_iommu_of_match),
932                 .pm             = &qcom_iommu_pm_ops,
933         },
934         .probe  = qcom_iommu_device_probe,
935         .remove = qcom_iommu_device_remove,
936 };
937
938 static int __init qcom_iommu_init(void)
939 {
940         int ret;
941
942         ret = platform_driver_register(&qcom_iommu_ctx_driver);
943         if (ret)
944                 return ret;
945
946         ret = platform_driver_register(&qcom_iommu_driver);
947         if (ret)
948                 platform_driver_unregister(&qcom_iommu_ctx_driver);
949
950         return ret;
951 }
952 device_initcall(qcom_iommu_init);