108dbd0f8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2b10f127eSOhad Ben-Cohen /* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
3b10f127eSOhad Ben-Cohen *
4a007dd51SPaul Gortmaker * Author: Stepan Moskovchenko <stepanm@codeaurora.org>
5b10f127eSOhad Ben-Cohen */
6b10f127eSOhad Ben-Cohen
7b10f127eSOhad Ben-Cohen #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8b10f127eSOhad Ben-Cohen #include <linux/kernel.h>
9a007dd51SPaul Gortmaker #include <linux/init.h>
10b10f127eSOhad Ben-Cohen #include <linux/platform_device.h>
11b10f127eSOhad Ben-Cohen #include <linux/errno.h>
12b10f127eSOhad Ben-Cohen #include <linux/io.h>
13b77cf11fSRob Herring #include <linux/io-pgtable.h>
14b10f127eSOhad Ben-Cohen #include <linux/interrupt.h>
15b10f127eSOhad Ben-Cohen #include <linux/list.h>
16b10f127eSOhad Ben-Cohen #include <linux/spinlock.h>
17b10f127eSOhad Ben-Cohen #include <linux/slab.h>
18b10f127eSOhad Ben-Cohen #include <linux/iommu.h>
19b10f127eSOhad Ben-Cohen #include <linux/clk.h>
20f7f125efSSricharan R #include <linux/err.h>
21b10f127eSOhad Ben-Cohen
22b10f127eSOhad Ben-Cohen #include <asm/cacheflush.h>
2387dfb311SMasahiro Yamada #include <linux/sizes.h>
24b10f127eSOhad Ben-Cohen
250b559df5SStephen Boyd #include "msm_iommu_hw-8xxx.h"
260b559df5SStephen Boyd #include "msm_iommu.h"
27b10f127eSOhad Ben-Cohen
28b10f127eSOhad Ben-Cohen #define MRC(reg, processor, op1, crn, crm, op2) \
29b10f127eSOhad Ben-Cohen __asm__ __volatile__ ( \
30b10f127eSOhad Ben-Cohen " mrc " #processor "," #op1 ", %0," #crn "," #crm "," #op2 "\n" \
31b10f127eSOhad Ben-Cohen : "=r" (reg))
32b10f127eSOhad Ben-Cohen
3383427275SOhad Ben-Cohen /* bitmap of the page sizes currently supported */
3483427275SOhad Ben-Cohen #define MSM_IOMMU_PGSIZES (SZ_4K | SZ_64K | SZ_1M | SZ_16M)
3583427275SOhad Ben-Cohen
36c4e0f3b2SSamuel Zou static DEFINE_SPINLOCK(msm_iommu_lock);
37109bd48eSSricharan R static LIST_HEAD(qcom_iommu_devices);
38c9220fbdSSricharan R static struct iommu_ops msm_iommu_ops;
39b10f127eSOhad Ben-Cohen
40b10f127eSOhad Ben-Cohen struct msm_priv {
41b10f127eSOhad Ben-Cohen struct list_head list_attached;
423e116c3cSJoerg Roedel struct iommu_domain domain;
43c9220fbdSSricharan R struct io_pgtable_cfg cfg;
44c9220fbdSSricharan R struct io_pgtable_ops *iop;
45c9220fbdSSricharan R struct device *dev;
46c9220fbdSSricharan R spinlock_t pgtlock; /* pagetable lock */
47b10f127eSOhad Ben-Cohen };
48b10f127eSOhad Ben-Cohen
to_msm_priv(struct iommu_domain * dom)493e116c3cSJoerg Roedel static struct msm_priv *to_msm_priv(struct iommu_domain *dom)
503e116c3cSJoerg Roedel {
513e116c3cSJoerg Roedel return container_of(dom, struct msm_priv, domain);
523e116c3cSJoerg Roedel }
533e116c3cSJoerg Roedel
__enable_clocks(struct msm_iommu_dev * iommu)54109bd48eSSricharan R static int __enable_clocks(struct msm_iommu_dev *iommu)
55b10f127eSOhad Ben-Cohen {
56b10f127eSOhad Ben-Cohen int ret;
57b10f127eSOhad Ben-Cohen
58109bd48eSSricharan R ret = clk_enable(iommu->pclk);
59b10f127eSOhad Ben-Cohen if (ret)
60b10f127eSOhad Ben-Cohen goto fail;
61b10f127eSOhad Ben-Cohen
62109bd48eSSricharan R if (iommu->clk) {
63109bd48eSSricharan R ret = clk_enable(iommu->clk);
64b10f127eSOhad Ben-Cohen if (ret)
65109bd48eSSricharan R clk_disable(iommu->pclk);
66b10f127eSOhad Ben-Cohen }
67b10f127eSOhad Ben-Cohen fail:
68b10f127eSOhad Ben-Cohen return ret;
69b10f127eSOhad Ben-Cohen }
70b10f127eSOhad Ben-Cohen
__disable_clocks(struct msm_iommu_dev * iommu)71109bd48eSSricharan R static void __disable_clocks(struct msm_iommu_dev *iommu)
72b10f127eSOhad Ben-Cohen {
73109bd48eSSricharan R if (iommu->clk)
74109bd48eSSricharan R clk_disable(iommu->clk);
75109bd48eSSricharan R clk_disable(iommu->pclk);
76b10f127eSOhad Ben-Cohen }
77b10f127eSOhad Ben-Cohen
msm_iommu_reset(void __iomem * base,int ncb)78f7f125efSSricharan R static void msm_iommu_reset(void __iomem *base, int ncb)
79f7f125efSSricharan R {
80f7f125efSSricharan R int ctx;
81f7f125efSSricharan R
82f7f125efSSricharan R SET_RPUE(base, 0);
83f7f125efSSricharan R SET_RPUEIE(base, 0);
84f7f125efSSricharan R SET_ESRRESTORE(base, 0);
85f7f125efSSricharan R SET_TBE(base, 0);
86f7f125efSSricharan R SET_CR(base, 0);
87f7f125efSSricharan R SET_SPDMBE(base, 0);
88f7f125efSSricharan R SET_TESTBUSCR(base, 0);
89f7f125efSSricharan R SET_TLBRSW(base, 0);
90f7f125efSSricharan R SET_GLOBAL_TLBIALL(base, 0);
91f7f125efSSricharan R SET_RPU_ACR(base, 0);
92f7f125efSSricharan R SET_TLBLKCRWE(base, 1);
93f7f125efSSricharan R
94f7f125efSSricharan R for (ctx = 0; ctx < ncb; ctx++) {
95f7f125efSSricharan R SET_BPRCOSH(base, ctx, 0);
96f7f125efSSricharan R SET_BPRCISH(base, ctx, 0);
97f7f125efSSricharan R SET_BPRCNSH(base, ctx, 0);
98f7f125efSSricharan R SET_BPSHCFG(base, ctx, 0);
99f7f125efSSricharan R SET_BPMTCFG(base, ctx, 0);
100f7f125efSSricharan R SET_ACTLR(base, ctx, 0);
101f7f125efSSricharan R SET_SCTLR(base, ctx, 0);
102f7f125efSSricharan R SET_FSRRESTORE(base, ctx, 0);
103f7f125efSSricharan R SET_TTBR0(base, ctx, 0);
104f7f125efSSricharan R SET_TTBR1(base, ctx, 0);
105f7f125efSSricharan R SET_TTBCR(base, ctx, 0);
106f7f125efSSricharan R SET_BFBCR(base, ctx, 0);
107f7f125efSSricharan R SET_PAR(base, ctx, 0);
108f7f125efSSricharan R SET_FAR(base, ctx, 0);
109f7f125efSSricharan R SET_CTX_TLBIALL(base, ctx, 0);
110f7f125efSSricharan R SET_TLBFLPTER(base, ctx, 0);
111f7f125efSSricharan R SET_TLBSLPTER(base, ctx, 0);
112f7f125efSSricharan R SET_TLBLKCR(base, ctx, 0);
113f7f125efSSricharan R SET_CONTEXTIDR(base, ctx, 0);
114f7f125efSSricharan R }
115f7f125efSSricharan R }
116f7f125efSSricharan R
__flush_iotlb(void * cookie)117c9220fbdSSricharan R static void __flush_iotlb(void *cookie)
118b10f127eSOhad Ben-Cohen {
119c9220fbdSSricharan R struct msm_priv *priv = cookie;
120109bd48eSSricharan R struct msm_iommu_dev *iommu = NULL;
121109bd48eSSricharan R struct msm_iommu_ctx_dev *master;
122b10f127eSOhad Ben-Cohen int ret = 0;
123109bd48eSSricharan R
124109bd48eSSricharan R list_for_each_entry(iommu, &priv->list_attached, dom_node) {
125109bd48eSSricharan R ret = __enable_clocks(iommu);
126b10f127eSOhad Ben-Cohen if (ret)
127b10f127eSOhad Ben-Cohen goto fail;
128b10f127eSOhad Ben-Cohen
129109bd48eSSricharan R list_for_each_entry(master, &iommu->ctx_list, list)
130109bd48eSSricharan R SET_CTX_TLBIALL(iommu->base, master->num, 0);
131109bd48eSSricharan R
132109bd48eSSricharan R __disable_clocks(iommu);
133b10f127eSOhad Ben-Cohen }
134b10f127eSOhad Ben-Cohen fail:
135c9220fbdSSricharan R return;
136b10f127eSOhad Ben-Cohen }
137b10f127eSOhad Ben-Cohen
__flush_iotlb_range(unsigned long iova,size_t size,size_t granule,bool leaf,void * cookie)138c9220fbdSSricharan R static void __flush_iotlb_range(unsigned long iova, size_t size,
139c9220fbdSSricharan R size_t granule, bool leaf, void *cookie)
140c9220fbdSSricharan R {
141c9220fbdSSricharan R struct msm_priv *priv = cookie;
142c9220fbdSSricharan R struct msm_iommu_dev *iommu = NULL;
143c9220fbdSSricharan R struct msm_iommu_ctx_dev *master;
144c9220fbdSSricharan R int ret = 0;
145c9220fbdSSricharan R int temp_size;
146c9220fbdSSricharan R
147c9220fbdSSricharan R list_for_each_entry(iommu, &priv->list_attached, dom_node) {
148c9220fbdSSricharan R ret = __enable_clocks(iommu);
149c9220fbdSSricharan R if (ret)
150c9220fbdSSricharan R goto fail;
151c9220fbdSSricharan R
152c9220fbdSSricharan R list_for_each_entry(master, &iommu->ctx_list, list) {
153c9220fbdSSricharan R temp_size = size;
154c9220fbdSSricharan R do {
155c9220fbdSSricharan R iova &= TLBIVA_VA;
156c9220fbdSSricharan R iova |= GET_CONTEXTIDR_ASID(iommu->base,
157c9220fbdSSricharan R master->num);
158c9220fbdSSricharan R SET_TLBIVA(iommu->base, master->num, iova);
159c9220fbdSSricharan R iova += granule;
160c9220fbdSSricharan R } while (temp_size -= granule);
161c9220fbdSSricharan R }
162c9220fbdSSricharan R
163c9220fbdSSricharan R __disable_clocks(iommu);
164c9220fbdSSricharan R }
165c9220fbdSSricharan R
166c9220fbdSSricharan R fail:
167c9220fbdSSricharan R return;
168c9220fbdSSricharan R }
169c9220fbdSSricharan R
__flush_iotlb_walk(unsigned long iova,size_t size,size_t granule,void * cookie)17005aed941SWill Deacon static void __flush_iotlb_walk(unsigned long iova, size_t size,
17105aed941SWill Deacon size_t granule, void *cookie)
172c9220fbdSSricharan R {
17305aed941SWill Deacon __flush_iotlb_range(iova, size, granule, false, cookie);
174c9220fbdSSricharan R }
175c9220fbdSSricharan R
__flush_iotlb_page(struct iommu_iotlb_gather * gather,unsigned long iova,size_t granule,void * cookie)1763951c41aSWill Deacon static void __flush_iotlb_page(struct iommu_iotlb_gather *gather,
1773951c41aSWill Deacon unsigned long iova, size_t granule, void *cookie)
178abfd6fe0SWill Deacon {
179abfd6fe0SWill Deacon __flush_iotlb_range(iova, granule, granule, true, cookie);
180abfd6fe0SWill Deacon }
181abfd6fe0SWill Deacon
182298f7889SWill Deacon static const struct iommu_flush_ops msm_iommu_flush_ops = {
183c9220fbdSSricharan R .tlb_flush_all = __flush_iotlb,
18405aed941SWill Deacon .tlb_flush_walk = __flush_iotlb_walk,
185abfd6fe0SWill Deacon .tlb_add_page = __flush_iotlb_page,
186c9220fbdSSricharan R };
187c9220fbdSSricharan R
msm_iommu_alloc_ctx(unsigned long * map,int start,int end)188109bd48eSSricharan R static int msm_iommu_alloc_ctx(unsigned long *map, int start, int end)
189109bd48eSSricharan R {
190109bd48eSSricharan R int idx;
191109bd48eSSricharan R
192109bd48eSSricharan R do {
193109bd48eSSricharan R idx = find_next_zero_bit(map, end, start);
194109bd48eSSricharan R if (idx == end)
195109bd48eSSricharan R return -ENOSPC;
196109bd48eSSricharan R } while (test_and_set_bit(idx, map));
197109bd48eSSricharan R
198109bd48eSSricharan R return idx;
199109bd48eSSricharan R }
200109bd48eSSricharan R
msm_iommu_free_ctx(unsigned long * map,int idx)201109bd48eSSricharan R static void msm_iommu_free_ctx(unsigned long *map, int idx)
202109bd48eSSricharan R {
203109bd48eSSricharan R clear_bit(idx, map);
204109bd48eSSricharan R }
205109bd48eSSricharan R
config_mids(struct msm_iommu_dev * iommu,struct msm_iommu_ctx_dev * master)206109bd48eSSricharan R static void config_mids(struct msm_iommu_dev *iommu,
207109bd48eSSricharan R struct msm_iommu_ctx_dev *master)
208109bd48eSSricharan R {
209109bd48eSSricharan R int mid, ctx, i;
210109bd48eSSricharan R
211109bd48eSSricharan R for (i = 0; i < master->num_mids; i++) {
212109bd48eSSricharan R mid = master->mids[i];
213109bd48eSSricharan R ctx = master->num;
214109bd48eSSricharan R
215109bd48eSSricharan R SET_M2VCBR_N(iommu->base, mid, 0);
216109bd48eSSricharan R SET_CBACR_N(iommu->base, ctx, 0);
217109bd48eSSricharan R
218109bd48eSSricharan R /* Set VMID = 0 */
219109bd48eSSricharan R SET_VMID(iommu->base, mid, 0);
220109bd48eSSricharan R
221109bd48eSSricharan R /* Set the context number for that MID to this context */
222109bd48eSSricharan R SET_CBNDX(iommu->base, mid, ctx);
223109bd48eSSricharan R
224109bd48eSSricharan R /* Set MID associated with this context bank to 0*/
225109bd48eSSricharan R SET_CBVMID(iommu->base, ctx, 0);
226109bd48eSSricharan R
227109bd48eSSricharan R /* Set the ASID for TLB tagging for this context */
228109bd48eSSricharan R SET_CONTEXTIDR_ASID(iommu->base, ctx, ctx);
229109bd48eSSricharan R
230109bd48eSSricharan R /* Set security bit override to be Non-secure */
231109bd48eSSricharan R SET_NSCFG(iommu->base, mid, 3);
232109bd48eSSricharan R }
233109bd48eSSricharan R }
234109bd48eSSricharan R
__reset_context(void __iomem * base,int ctx)235b10f127eSOhad Ben-Cohen static void __reset_context(void __iomem *base, int ctx)
236b10f127eSOhad Ben-Cohen {
237b10f127eSOhad Ben-Cohen SET_BPRCOSH(base, ctx, 0);
238b10f127eSOhad Ben-Cohen SET_BPRCISH(base, ctx, 0);
239b10f127eSOhad Ben-Cohen SET_BPRCNSH(base, ctx, 0);
240b10f127eSOhad Ben-Cohen SET_BPSHCFG(base, ctx, 0);
241b10f127eSOhad Ben-Cohen SET_BPMTCFG(base, ctx, 0);
242b10f127eSOhad Ben-Cohen SET_ACTLR(base, ctx, 0);
243b10f127eSOhad Ben-Cohen SET_SCTLR(base, ctx, 0);
244b10f127eSOhad Ben-Cohen SET_FSRRESTORE(base, ctx, 0);
245b10f127eSOhad Ben-Cohen SET_TTBR0(base, ctx, 0);
246b10f127eSOhad Ben-Cohen SET_TTBR1(base, ctx, 0);
247b10f127eSOhad Ben-Cohen SET_TTBCR(base, ctx, 0);
248b10f127eSOhad Ben-Cohen SET_BFBCR(base, ctx, 0);
249b10f127eSOhad Ben-Cohen SET_PAR(base, ctx, 0);
250b10f127eSOhad Ben-Cohen SET_FAR(base, ctx, 0);
251b10f127eSOhad Ben-Cohen SET_CTX_TLBIALL(base, ctx, 0);
252b10f127eSOhad Ben-Cohen SET_TLBFLPTER(base, ctx, 0);
253b10f127eSOhad Ben-Cohen SET_TLBSLPTER(base, ctx, 0);
254b10f127eSOhad Ben-Cohen SET_TLBLKCR(base, ctx, 0);
255b10f127eSOhad Ben-Cohen }
256b10f127eSOhad Ben-Cohen
__program_context(void __iomem * base,int ctx,struct msm_priv * priv)257c9220fbdSSricharan R static void __program_context(void __iomem *base, int ctx,
258c9220fbdSSricharan R struct msm_priv *priv)
259b10f127eSOhad Ben-Cohen {
260b10f127eSOhad Ben-Cohen __reset_context(base, ctx);
261b10f127eSOhad Ben-Cohen
262c9220fbdSSricharan R /* Turn on TEX Remap */
263c9220fbdSSricharan R SET_TRE(base, ctx, 1);
264c9220fbdSSricharan R SET_AFE(base, ctx, 1);
265c9220fbdSSricharan R
266b10f127eSOhad Ben-Cohen /* Set up HTW mode */
267b10f127eSOhad Ben-Cohen /* TLB miss configuration: perform HTW on miss */
268b10f127eSOhad Ben-Cohen SET_TLBMCFG(base, ctx, 0x3);
269b10f127eSOhad Ben-Cohen
270b10f127eSOhad Ben-Cohen /* V2P configuration: HTW for access */
271b10f127eSOhad Ben-Cohen SET_V2PCFG(base, ctx, 0x3);
272b10f127eSOhad Ben-Cohen
273c9220fbdSSricharan R SET_TTBCR(base, ctx, priv->cfg.arm_v7s_cfg.tcr);
274d1e5f26fSRobin Murphy SET_TTBR0(base, ctx, priv->cfg.arm_v7s_cfg.ttbr);
275d1e5f26fSRobin Murphy SET_TTBR1(base, ctx, 0);
276c9220fbdSSricharan R
277c9220fbdSSricharan R /* Set prrr and nmrr */
278c9220fbdSSricharan R SET_PRRR(base, ctx, priv->cfg.arm_v7s_cfg.prrr);
279c9220fbdSSricharan R SET_NMRR(base, ctx, priv->cfg.arm_v7s_cfg.nmrr);
280b10f127eSOhad Ben-Cohen
281b10f127eSOhad Ben-Cohen /* Invalidate the TLB for this context */
282b10f127eSOhad Ben-Cohen SET_CTX_TLBIALL(base, ctx, 0);
283b10f127eSOhad Ben-Cohen
284b10f127eSOhad Ben-Cohen /* Set interrupt number to "secure" interrupt */
285b10f127eSOhad Ben-Cohen SET_IRPTNDX(base, ctx, 0);
286b10f127eSOhad Ben-Cohen
287b10f127eSOhad Ben-Cohen /* Enable context fault interrupt */
288b10f127eSOhad Ben-Cohen SET_CFEIE(base, ctx, 1);
289b10f127eSOhad Ben-Cohen
290b10f127eSOhad Ben-Cohen /* Stall access on a context fault and let the handler deal with it */
291b10f127eSOhad Ben-Cohen SET_CFCFG(base, ctx, 1);
292b10f127eSOhad Ben-Cohen
293b10f127eSOhad Ben-Cohen /* Redirect all cacheable requests to L2 slave port. */
294b10f127eSOhad Ben-Cohen SET_RCISH(base, ctx, 1);
295b10f127eSOhad Ben-Cohen SET_RCOSH(base, ctx, 1);
296b10f127eSOhad Ben-Cohen SET_RCNSH(base, ctx, 1);
297b10f127eSOhad Ben-Cohen
298b10f127eSOhad Ben-Cohen /* Turn on BFB prefetch */
299b10f127eSOhad Ben-Cohen SET_BFBDFE(base, ctx, 1);
300b10f127eSOhad Ben-Cohen
301b10f127eSOhad Ben-Cohen /* Enable the MMU */
302b10f127eSOhad Ben-Cohen SET_M(base, ctx, 1);
303b10f127eSOhad Ben-Cohen }
304b10f127eSOhad Ben-Cohen
msm_iommu_domain_alloc(unsigned type)3053e116c3cSJoerg Roedel static struct iommu_domain *msm_iommu_domain_alloc(unsigned type)
306b10f127eSOhad Ben-Cohen {
3073e116c3cSJoerg Roedel struct msm_priv *priv;
308b10f127eSOhad Ben-Cohen
3093e116c3cSJoerg Roedel if (type != IOMMU_DOMAIN_UNMANAGED)
3103e116c3cSJoerg Roedel return NULL;
3113e116c3cSJoerg Roedel
3123e116c3cSJoerg Roedel priv = kzalloc(sizeof(*priv), GFP_KERNEL);
313b10f127eSOhad Ben-Cohen if (!priv)
314b10f127eSOhad Ben-Cohen goto fail_nomem;
315b10f127eSOhad Ben-Cohen
316b10f127eSOhad Ben-Cohen INIT_LIST_HEAD(&priv->list_attached);
3174be6a290SJoerg Roedel
3183e116c3cSJoerg Roedel priv->domain.geometry.aperture_start = 0;
3193e116c3cSJoerg Roedel priv->domain.geometry.aperture_end = (1ULL << 32) - 1;
3203e116c3cSJoerg Roedel priv->domain.geometry.force_aperture = true;
3214be6a290SJoerg Roedel
3223e116c3cSJoerg Roedel return &priv->domain;
323b10f127eSOhad Ben-Cohen
324b10f127eSOhad Ben-Cohen fail_nomem:
325b10f127eSOhad Ben-Cohen kfree(priv);
3263e116c3cSJoerg Roedel return NULL;
327b10f127eSOhad Ben-Cohen }
328b10f127eSOhad Ben-Cohen
msm_iommu_domain_free(struct iommu_domain * domain)3293e116c3cSJoerg Roedel static void msm_iommu_domain_free(struct iommu_domain *domain)
330b10f127eSOhad Ben-Cohen {
331b10f127eSOhad Ben-Cohen struct msm_priv *priv;
332b10f127eSOhad Ben-Cohen unsigned long flags;
333b10f127eSOhad Ben-Cohen
334b10f127eSOhad Ben-Cohen spin_lock_irqsave(&msm_iommu_lock, flags);
3353e116c3cSJoerg Roedel priv = to_msm_priv(domain);
336b10f127eSOhad Ben-Cohen kfree(priv);
337b10f127eSOhad Ben-Cohen spin_unlock_irqrestore(&msm_iommu_lock, flags);
338b10f127eSOhad Ben-Cohen }
339b10f127eSOhad Ben-Cohen
msm_iommu_domain_config(struct msm_priv * priv)340c9220fbdSSricharan R static int msm_iommu_domain_config(struct msm_priv *priv)
341c9220fbdSSricharan R {
342c9220fbdSSricharan R spin_lock_init(&priv->pgtlock);
343c9220fbdSSricharan R
344c9220fbdSSricharan R priv->cfg = (struct io_pgtable_cfg) {
345c9220fbdSSricharan R .pgsize_bitmap = msm_iommu_ops.pgsize_bitmap,
346c9220fbdSSricharan R .ias = 32,
347c9220fbdSSricharan R .oas = 32,
348298f7889SWill Deacon .tlb = &msm_iommu_flush_ops,
349c9220fbdSSricharan R .iommu_dev = priv->dev,
350c9220fbdSSricharan R };
351c9220fbdSSricharan R
352c9220fbdSSricharan R priv->iop = alloc_io_pgtable_ops(ARM_V7S, &priv->cfg, priv);
353c9220fbdSSricharan R if (!priv->iop) {
354c9220fbdSSricharan R dev_err(priv->dev, "Failed to allocate pgtable\n");
355c9220fbdSSricharan R return -EINVAL;
356c9220fbdSSricharan R }
357c9220fbdSSricharan R
358c9220fbdSSricharan R msm_iommu_ops.pgsize_bitmap = priv->cfg.pgsize_bitmap;
359c9220fbdSSricharan R
360c9220fbdSSricharan R return 0;
361c9220fbdSSricharan R }
362c9220fbdSSricharan R
36342df43b3SJoerg Roedel /* Must be called under msm_iommu_lock */
find_iommu_for_dev(struct device * dev)36442df43b3SJoerg Roedel static struct msm_iommu_dev *find_iommu_for_dev(struct device *dev)
36542df43b3SJoerg Roedel {
36642df43b3SJoerg Roedel struct msm_iommu_dev *iommu, *ret = NULL;
36742df43b3SJoerg Roedel struct msm_iommu_ctx_dev *master;
36842df43b3SJoerg Roedel
36942df43b3SJoerg Roedel list_for_each_entry(iommu, &qcom_iommu_devices, dev_node) {
37042df43b3SJoerg Roedel master = list_first_entry(&iommu->ctx_list,
37142df43b3SJoerg Roedel struct msm_iommu_ctx_dev,
37242df43b3SJoerg Roedel list);
37342df43b3SJoerg Roedel if (master->of_node == dev->of_node) {
37442df43b3SJoerg Roedel ret = iommu;
37542df43b3SJoerg Roedel break;
37642df43b3SJoerg Roedel }
37742df43b3SJoerg Roedel }
37842df43b3SJoerg Roedel
37942df43b3SJoerg Roedel return ret;
38042df43b3SJoerg Roedel }
38142df43b3SJoerg Roedel
msm_iommu_probe_device(struct device * dev)382dea74f1cSJoerg Roedel static struct iommu_device *msm_iommu_probe_device(struct device *dev)
38342df43b3SJoerg Roedel {
38442df43b3SJoerg Roedel struct msm_iommu_dev *iommu;
38542df43b3SJoerg Roedel unsigned long flags;
38642df43b3SJoerg Roedel
38742df43b3SJoerg Roedel spin_lock_irqsave(&msm_iommu_lock, flags);
38842df43b3SJoerg Roedel iommu = find_iommu_for_dev(dev);
38937952146SNiklas Cassel spin_unlock_irqrestore(&msm_iommu_lock, flags);
39037952146SNiklas Cassel
391dea74f1cSJoerg Roedel if (!iommu)
392dea74f1cSJoerg Roedel return ERR_PTR(-ENODEV);
393ce2eb8f4SRobin Murphy
394dea74f1cSJoerg Roedel return &iommu->iommu;
39542df43b3SJoerg Roedel }
39642df43b3SJoerg Roedel
msm_iommu_attach_dev(struct iommu_domain * domain,struct device * dev)397b10f127eSOhad Ben-Cohen static int msm_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
398b10f127eSOhad Ben-Cohen {
399b10f127eSOhad Ben-Cohen int ret = 0;
400b10f127eSOhad Ben-Cohen unsigned long flags;
401109bd48eSSricharan R struct msm_iommu_dev *iommu;
402109bd48eSSricharan R struct msm_priv *priv = to_msm_priv(domain);
403109bd48eSSricharan R struct msm_iommu_ctx_dev *master;
404b10f127eSOhad Ben-Cohen
405c9220fbdSSricharan R priv->dev = dev;
406c9220fbdSSricharan R msm_iommu_domain_config(priv);
407c9220fbdSSricharan R
408b10f127eSOhad Ben-Cohen spin_lock_irqsave(&msm_iommu_lock, flags);
409109bd48eSSricharan R list_for_each_entry(iommu, &qcom_iommu_devices, dev_node) {
410109bd48eSSricharan R master = list_first_entry(&iommu->ctx_list,
411109bd48eSSricharan R struct msm_iommu_ctx_dev,
412109bd48eSSricharan R list);
413109bd48eSSricharan R if (master->of_node == dev->of_node) {
414109bd48eSSricharan R ret = __enable_clocks(iommu);
415b10f127eSOhad Ben-Cohen if (ret)
416b10f127eSOhad Ben-Cohen goto fail;
417b10f127eSOhad Ben-Cohen
418109bd48eSSricharan R list_for_each_entry(master, &iommu->ctx_list, list) {
419109bd48eSSricharan R if (master->num) {
420109bd48eSSricharan R dev_err(dev, "domain already attached");
421109bd48eSSricharan R ret = -EEXIST;
422109bd48eSSricharan R goto fail;
423109bd48eSSricharan R }
424109bd48eSSricharan R master->num =
425109bd48eSSricharan R msm_iommu_alloc_ctx(iommu->context_map,
426109bd48eSSricharan R 0, iommu->ncb);
427109bd48eSSricharan R if (IS_ERR_VALUE(master->num)) {
428109bd48eSSricharan R ret = -ENODEV;
429109bd48eSSricharan R goto fail;
430109bd48eSSricharan R }
431109bd48eSSricharan R config_mids(iommu, master);
432109bd48eSSricharan R __program_context(iommu->base, master->num,
433c9220fbdSSricharan R priv);
434109bd48eSSricharan R }
435109bd48eSSricharan R __disable_clocks(iommu);
436109bd48eSSricharan R list_add(&iommu->dom_node, &priv->list_attached);
437109bd48eSSricharan R }
438109bd48eSSricharan R }
439b10f127eSOhad Ben-Cohen
440b10f127eSOhad Ben-Cohen fail:
441b10f127eSOhad Ben-Cohen spin_unlock_irqrestore(&msm_iommu_lock, flags);
442109bd48eSSricharan R
443b10f127eSOhad Ben-Cohen return ret;
444b10f127eSOhad Ben-Cohen }
445b10f127eSOhad Ben-Cohen
msm_iommu_set_platform_dma(struct device * dev)446c1fe9119SLu Baolu static void msm_iommu_set_platform_dma(struct device *dev)
447b10f127eSOhad Ben-Cohen {
448c1fe9119SLu Baolu struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
449109bd48eSSricharan R struct msm_priv *priv = to_msm_priv(domain);
450b10f127eSOhad Ben-Cohen unsigned long flags;
451109bd48eSSricharan R struct msm_iommu_dev *iommu;
452109bd48eSSricharan R struct msm_iommu_ctx_dev *master;
453b10f127eSOhad Ben-Cohen int ret;
454b10f127eSOhad Ben-Cohen
455c9220fbdSSricharan R free_io_pgtable_ops(priv->iop);
456b10f127eSOhad Ben-Cohen
457c9220fbdSSricharan R spin_lock_irqsave(&msm_iommu_lock, flags);
458109bd48eSSricharan R list_for_each_entry(iommu, &priv->list_attached, dom_node) {
459109bd48eSSricharan R ret = __enable_clocks(iommu);
460b10f127eSOhad Ben-Cohen if (ret)
461b10f127eSOhad Ben-Cohen goto fail;
462b10f127eSOhad Ben-Cohen
463109bd48eSSricharan R list_for_each_entry(master, &iommu->ctx_list, list) {
464109bd48eSSricharan R msm_iommu_free_ctx(iommu->context_map, master->num);
465109bd48eSSricharan R __reset_context(iommu->base, master->num);
466109bd48eSSricharan R }
467109bd48eSSricharan R __disable_clocks(iommu);
468109bd48eSSricharan R }
469b10f127eSOhad Ben-Cohen fail:
470b10f127eSOhad Ben-Cohen spin_unlock_irqrestore(&msm_iommu_lock, flags);
471b10f127eSOhad Ben-Cohen }
472b10f127eSOhad Ben-Cohen
msm_iommu_map(struct iommu_domain * domain,unsigned long iova,phys_addr_t pa,size_t pgsize,size_t pgcount,int prot,gfp_t gfp,size_t * mapped)473c9220fbdSSricharan R static int msm_iommu_map(struct iommu_domain *domain, unsigned long iova,
4748b35cdcfSRobin Murphy phys_addr_t pa, size_t pgsize, size_t pgcount,
4758b35cdcfSRobin Murphy int prot, gfp_t gfp, size_t *mapped)
476b10f127eSOhad Ben-Cohen {
477c9220fbdSSricharan R struct msm_priv *priv = to_msm_priv(domain);
478b10f127eSOhad Ben-Cohen unsigned long flags;
479c9220fbdSSricharan R int ret;
480b10f127eSOhad Ben-Cohen
481c9220fbdSSricharan R spin_lock_irqsave(&priv->pgtlock, flags);
4828b35cdcfSRobin Murphy ret = priv->iop->map_pages(priv->iop, iova, pa, pgsize, pgcount, prot,
4838b35cdcfSRobin Murphy GFP_ATOMIC, mapped);
484c9220fbdSSricharan R spin_unlock_irqrestore(&priv->pgtlock, flags);
485b10f127eSOhad Ben-Cohen
486b10f127eSOhad Ben-Cohen return ret;
487b10f127eSOhad Ben-Cohen }
488b10f127eSOhad Ben-Cohen
msm_iommu_sync_map(struct iommu_domain * domain,unsigned long iova,size_t size)489c867c78aSRobin Murphy static void msm_iommu_sync_map(struct iommu_domain *domain, unsigned long iova,
490c867c78aSRobin Murphy size_t size)
491c867c78aSRobin Murphy {
492c867c78aSRobin Murphy struct msm_priv *priv = to_msm_priv(domain);
493c867c78aSRobin Murphy
494c867c78aSRobin Murphy __flush_iotlb_range(iova, size, SZ_4K, false, priv);
495c867c78aSRobin Murphy }
496c867c78aSRobin Murphy
msm_iommu_unmap(struct iommu_domain * domain,unsigned long iova,size_t pgsize,size_t pgcount,struct iommu_iotlb_gather * gather)497c9220fbdSSricharan R static size_t msm_iommu_unmap(struct iommu_domain *domain, unsigned long iova,
4988b35cdcfSRobin Murphy size_t pgsize, size_t pgcount,
4998b35cdcfSRobin Murphy struct iommu_iotlb_gather *gather)
500b10f127eSOhad Ben-Cohen {
501c9220fbdSSricharan R struct msm_priv *priv = to_msm_priv(domain);
502b10f127eSOhad Ben-Cohen unsigned long flags;
5038b35cdcfSRobin Murphy size_t ret;
504b10f127eSOhad Ben-Cohen
505c9220fbdSSricharan R spin_lock_irqsave(&priv->pgtlock, flags);
5068b35cdcfSRobin Murphy ret = priv->iop->unmap_pages(priv->iop, iova, pgsize, pgcount, gather);
507c9220fbdSSricharan R spin_unlock_irqrestore(&priv->pgtlock, flags);
508b10f127eSOhad Ben-Cohen
5098b35cdcfSRobin Murphy return ret;
510b10f127eSOhad Ben-Cohen }
511b10f127eSOhad Ben-Cohen
msm_iommu_iova_to_phys(struct iommu_domain * domain,dma_addr_t va)512b10f127eSOhad Ben-Cohen static phys_addr_t msm_iommu_iova_to_phys(struct iommu_domain *domain,
513bb5547acSVarun Sethi dma_addr_t va)
514b10f127eSOhad Ben-Cohen {
515b10f127eSOhad Ben-Cohen struct msm_priv *priv;
516109bd48eSSricharan R struct msm_iommu_dev *iommu;
517109bd48eSSricharan R struct msm_iommu_ctx_dev *master;
518b10f127eSOhad Ben-Cohen unsigned int par;
519b10f127eSOhad Ben-Cohen unsigned long flags;
520b10f127eSOhad Ben-Cohen phys_addr_t ret = 0;
521b10f127eSOhad Ben-Cohen
522b10f127eSOhad Ben-Cohen spin_lock_irqsave(&msm_iommu_lock, flags);
523b10f127eSOhad Ben-Cohen
5243e116c3cSJoerg Roedel priv = to_msm_priv(domain);
525109bd48eSSricharan R iommu = list_first_entry(&priv->list_attached,
526109bd48eSSricharan R struct msm_iommu_dev, dom_node);
527109bd48eSSricharan R
528109bd48eSSricharan R if (list_empty(&iommu->ctx_list))
529b10f127eSOhad Ben-Cohen goto fail;
530b10f127eSOhad Ben-Cohen
531109bd48eSSricharan R master = list_first_entry(&iommu->ctx_list,
532109bd48eSSricharan R struct msm_iommu_ctx_dev, list);
533109bd48eSSricharan R if (!master)
534109bd48eSSricharan R goto fail;
535b10f127eSOhad Ben-Cohen
536109bd48eSSricharan R ret = __enable_clocks(iommu);
537b10f127eSOhad Ben-Cohen if (ret)
538b10f127eSOhad Ben-Cohen goto fail;
539b10f127eSOhad Ben-Cohen
540b10f127eSOhad Ben-Cohen /* Invalidate context TLB */
541109bd48eSSricharan R SET_CTX_TLBIALL(iommu->base, master->num, 0);
542109bd48eSSricharan R SET_V2PPR(iommu->base, master->num, va & V2Pxx_VA);
543b10f127eSOhad Ben-Cohen
544109bd48eSSricharan R par = GET_PAR(iommu->base, master->num);
545b10f127eSOhad Ben-Cohen
546b10f127eSOhad Ben-Cohen /* We are dealing with a supersection */
547109bd48eSSricharan R if (GET_NOFAULT_SS(iommu->base, master->num))
548b10f127eSOhad Ben-Cohen ret = (par & 0xFF000000) | (va & 0x00FFFFFF);
549b10f127eSOhad Ben-Cohen else /* Upper 20 bits from PAR, lower 12 from VA */
550b10f127eSOhad Ben-Cohen ret = (par & 0xFFFFF000) | (va & 0x00000FFF);
551b10f127eSOhad Ben-Cohen
552109bd48eSSricharan R if (GET_FAULT(iommu->base, master->num))
553b10f127eSOhad Ben-Cohen ret = 0;
554b10f127eSOhad Ben-Cohen
555109bd48eSSricharan R __disable_clocks(iommu);
556b10f127eSOhad Ben-Cohen fail:
557b10f127eSOhad Ben-Cohen spin_unlock_irqrestore(&msm_iommu_lock, flags);
558b10f127eSOhad Ben-Cohen return ret;
559b10f127eSOhad Ben-Cohen }
560b10f127eSOhad Ben-Cohen
print_ctx_regs(void __iomem * base,int ctx)561b10f127eSOhad Ben-Cohen static void print_ctx_regs(void __iomem *base, int ctx)
562b10f127eSOhad Ben-Cohen {
563b10f127eSOhad Ben-Cohen unsigned int fsr = GET_FSR(base, ctx);
564b10f127eSOhad Ben-Cohen pr_err("FAR = %08x PAR = %08x\n",
565b10f127eSOhad Ben-Cohen GET_FAR(base, ctx), GET_PAR(base, ctx));
566b10f127eSOhad Ben-Cohen pr_err("FSR = %08x [%s%s%s%s%s%s%s%s%s%s]\n", fsr,
567b10f127eSOhad Ben-Cohen (fsr & 0x02) ? "TF " : "",
568b10f127eSOhad Ben-Cohen (fsr & 0x04) ? "AFF " : "",
569b10f127eSOhad Ben-Cohen (fsr & 0x08) ? "APF " : "",
570b10f127eSOhad Ben-Cohen (fsr & 0x10) ? "TLBMF " : "",
571b10f127eSOhad Ben-Cohen (fsr & 0x20) ? "HTWDEEF " : "",
572b10f127eSOhad Ben-Cohen (fsr & 0x40) ? "HTWSEEF " : "",
573b10f127eSOhad Ben-Cohen (fsr & 0x80) ? "MHF " : "",
574b10f127eSOhad Ben-Cohen (fsr & 0x10000) ? "SL " : "",
575b10f127eSOhad Ben-Cohen (fsr & 0x40000000) ? "SS " : "",
576b10f127eSOhad Ben-Cohen (fsr & 0x80000000) ? "MULTI " : "");
577b10f127eSOhad Ben-Cohen
578b10f127eSOhad Ben-Cohen pr_err("FSYNR0 = %08x FSYNR1 = %08x\n",
579b10f127eSOhad Ben-Cohen GET_FSYNR0(base, ctx), GET_FSYNR1(base, ctx));
580b10f127eSOhad Ben-Cohen pr_err("TTBR0 = %08x TTBR1 = %08x\n",
581b10f127eSOhad Ben-Cohen GET_TTBR0(base, ctx), GET_TTBR1(base, ctx));
582b10f127eSOhad Ben-Cohen pr_err("SCTLR = %08x ACTLR = %08x\n",
583b10f127eSOhad Ben-Cohen GET_SCTLR(base, ctx), GET_ACTLR(base, ctx));
584b10f127eSOhad Ben-Cohen }
585b10f127eSOhad Ben-Cohen
insert_iommu_master(struct device * dev,struct msm_iommu_dev ** iommu,struct of_phandle_args * spec)586bb5bdc5aSXiaoke Wang static int insert_iommu_master(struct device *dev,
587f78ebca8SSricharan R struct msm_iommu_dev **iommu,
588f78ebca8SSricharan R struct of_phandle_args *spec)
589f78ebca8SSricharan R {
5904bbe0c7cSJoerg Roedel struct msm_iommu_ctx_dev *master = dev_iommu_priv_get(dev);
591f78ebca8SSricharan R int sid;
592f78ebca8SSricharan R
593f78ebca8SSricharan R if (list_empty(&(*iommu)->ctx_list)) {
594f78ebca8SSricharan R master = kzalloc(sizeof(*master), GFP_ATOMIC);
595bb5bdc5aSXiaoke Wang if (!master) {
596bb5bdc5aSXiaoke Wang dev_err(dev, "Failed to allocate iommu_master\n");
597bb5bdc5aSXiaoke Wang return -ENOMEM;
598bb5bdc5aSXiaoke Wang }
599f78ebca8SSricharan R master->of_node = dev->of_node;
600f78ebca8SSricharan R list_add(&master->list, &(*iommu)->ctx_list);
6014bbe0c7cSJoerg Roedel dev_iommu_priv_set(dev, master);
602f78ebca8SSricharan R }
603f78ebca8SSricharan R
604f78ebca8SSricharan R for (sid = 0; sid < master->num_mids; sid++)
605f78ebca8SSricharan R if (master->mids[sid] == spec->args[0]) {
606f066b8f7SJustin Stitt dev_warn(dev, "Stream ID 0x%x repeated; ignoring\n",
607f78ebca8SSricharan R sid);
608bb5bdc5aSXiaoke Wang return 0;
609f78ebca8SSricharan R }
610f78ebca8SSricharan R
611f78ebca8SSricharan R master->mids[master->num_mids++] = spec->args[0];
612bb5bdc5aSXiaoke Wang return 0;
613f78ebca8SSricharan R }
614f78ebca8SSricharan R
qcom_iommu_of_xlate(struct device * dev,struct of_phandle_args * spec)615f78ebca8SSricharan R static int qcom_iommu_of_xlate(struct device *dev,
616f78ebca8SSricharan R struct of_phandle_args *spec)
617f78ebca8SSricharan R {
6188b9ad480SXiaomeng Tong struct msm_iommu_dev *iommu = NULL, *iter;
619f78ebca8SSricharan R unsigned long flags;
620f78ebca8SSricharan R int ret = 0;
621f78ebca8SSricharan R
622f78ebca8SSricharan R spin_lock_irqsave(&msm_iommu_lock, flags);
6238b9ad480SXiaomeng Tong list_for_each_entry(iter, &qcom_iommu_devices, dev_node) {
6248b9ad480SXiaomeng Tong if (iter->dev->of_node == spec->np) {
6258b9ad480SXiaomeng Tong iommu = iter;
626f78ebca8SSricharan R break;
6278b9ad480SXiaomeng Tong }
6288b9ad480SXiaomeng Tong }
629f78ebca8SSricharan R
6308b9ad480SXiaomeng Tong if (!iommu) {
631f78ebca8SSricharan R ret = -ENODEV;
632f78ebca8SSricharan R goto fail;
633f78ebca8SSricharan R }
634f78ebca8SSricharan R
635bb5bdc5aSXiaoke Wang ret = insert_iommu_master(dev, &iommu, spec);
636f78ebca8SSricharan R fail:
637f78ebca8SSricharan R spin_unlock_irqrestore(&msm_iommu_lock, flags);
638f78ebca8SSricharan R
639f78ebca8SSricharan R return ret;
640f78ebca8SSricharan R }
641f78ebca8SSricharan R
msm_iommu_fault_handler(int irq,void * dev_id)642b10f127eSOhad Ben-Cohen irqreturn_t msm_iommu_fault_handler(int irq, void *dev_id)
643b10f127eSOhad Ben-Cohen {
644109bd48eSSricharan R struct msm_iommu_dev *iommu = dev_id;
645b10f127eSOhad Ben-Cohen unsigned int fsr;
646b10f127eSOhad Ben-Cohen int i, ret;
647b10f127eSOhad Ben-Cohen
648b10f127eSOhad Ben-Cohen spin_lock(&msm_iommu_lock);
649b10f127eSOhad Ben-Cohen
650109bd48eSSricharan R if (!iommu) {
651b10f127eSOhad Ben-Cohen pr_err("Invalid device ID in context interrupt handler\n");
652b10f127eSOhad Ben-Cohen goto fail;
653b10f127eSOhad Ben-Cohen }
654b10f127eSOhad Ben-Cohen
655b10f127eSOhad Ben-Cohen pr_err("Unexpected IOMMU page fault!\n");
656109bd48eSSricharan R pr_err("base = %08x\n", (unsigned int)iommu->base);
657b10f127eSOhad Ben-Cohen
658109bd48eSSricharan R ret = __enable_clocks(iommu);
659b10f127eSOhad Ben-Cohen if (ret)
660b10f127eSOhad Ben-Cohen goto fail;
661b10f127eSOhad Ben-Cohen
662109bd48eSSricharan R for (i = 0; i < iommu->ncb; i++) {
663109bd48eSSricharan R fsr = GET_FSR(iommu->base, i);
664b10f127eSOhad Ben-Cohen if (fsr) {
665b10f127eSOhad Ben-Cohen pr_err("Fault occurred in context %d.\n", i);
666b10f127eSOhad Ben-Cohen pr_err("Interesting registers:\n");
667109bd48eSSricharan R print_ctx_regs(iommu->base, i);
668109bd48eSSricharan R SET_FSR(iommu->base, i, 0x4000000F);
669b10f127eSOhad Ben-Cohen }
670b10f127eSOhad Ben-Cohen }
671109bd48eSSricharan R __disable_clocks(iommu);
672b10f127eSOhad Ben-Cohen fail:
673b10f127eSOhad Ben-Cohen spin_unlock(&msm_iommu_lock);
674b10f127eSOhad Ben-Cohen return 0;
675b10f127eSOhad Ben-Cohen }
676b10f127eSOhad Ben-Cohen
677f78ebca8SSricharan R static struct iommu_ops msm_iommu_ops = {
6783e116c3cSJoerg Roedel .domain_alloc = msm_iommu_domain_alloc,
6799a630a4bSLu Baolu .probe_device = msm_iommu_probe_device,
6809a630a4bSLu Baolu .device_group = generic_device_group,
681c1fe9119SLu Baolu .set_platform_dma_ops = msm_iommu_set_platform_dma,
6829a630a4bSLu Baolu .pgsize_bitmap = MSM_IOMMU_PGSIZES,
6839a630a4bSLu Baolu .of_xlate = qcom_iommu_of_xlate,
6849a630a4bSLu Baolu .default_domain_ops = &(const struct iommu_domain_ops) {
685b10f127eSOhad Ben-Cohen .attach_dev = msm_iommu_attach_dev,
6868b35cdcfSRobin Murphy .map_pages = msm_iommu_map,
6878b35cdcfSRobin Murphy .unmap_pages = msm_iommu_unmap,
688e953f7f2SWill Deacon /*
689e953f7f2SWill Deacon * Nothing is needed here, the barrier to guarantee
690e953f7f2SWill Deacon * completion of the tlb sync operation is implicitly
691e953f7f2SWill Deacon * taken care when the iommu client does a writel before
692e953f7f2SWill Deacon * kick starting the other master.
693e953f7f2SWill Deacon */
694e953f7f2SWill Deacon .iotlb_sync = NULL,
695c867c78aSRobin Murphy .iotlb_sync_map = msm_iommu_sync_map,
696b10f127eSOhad Ben-Cohen .iova_to_phys = msm_iommu_iova_to_phys,
6979a630a4bSLu Baolu .free = msm_iommu_domain_free,
6989a630a4bSLu Baolu }
699b10f127eSOhad Ben-Cohen };
700b10f127eSOhad Ben-Cohen
msm_iommu_probe(struct platform_device * pdev)701f7f125efSSricharan R static int msm_iommu_probe(struct platform_device *pdev)
702f7f125efSSricharan R {
703f7f125efSSricharan R struct resource *r;
70442df43b3SJoerg Roedel resource_size_t ioaddr;
705f7f125efSSricharan R struct msm_iommu_dev *iommu;
706f7f125efSSricharan R int ret, par, val;
707f7f125efSSricharan R
708f7f125efSSricharan R iommu = devm_kzalloc(&pdev->dev, sizeof(*iommu), GFP_KERNEL);
709f7f125efSSricharan R if (!iommu)
710f7f125efSSricharan R return -ENODEV;
711f7f125efSSricharan R
712f7f125efSSricharan R iommu->dev = &pdev->dev;
713f7f125efSSricharan R INIT_LIST_HEAD(&iommu->ctx_list);
714f7f125efSSricharan R
715f7f125efSSricharan R iommu->pclk = devm_clk_get(iommu->dev, "smmu_pclk");
716a063158bSDavid Heidelberg if (IS_ERR(iommu->pclk))
717a063158bSDavid Heidelberg return dev_err_probe(iommu->dev, PTR_ERR(iommu->pclk),
718a063158bSDavid Heidelberg "could not get smmu_pclk\n");
719f7f125efSSricharan R
720f7f125efSSricharan R ret = clk_prepare(iommu->pclk);
721a063158bSDavid Heidelberg if (ret)
722a063158bSDavid Heidelberg return dev_err_probe(iommu->dev, ret,
723a063158bSDavid Heidelberg "could not prepare smmu_pclk\n");
724f7f125efSSricharan R
725f7f125efSSricharan R iommu->clk = devm_clk_get(iommu->dev, "iommu_clk");
726f7f125efSSricharan R if (IS_ERR(iommu->clk)) {
727f7f125efSSricharan R clk_unprepare(iommu->pclk);
728a063158bSDavid Heidelberg return dev_err_probe(iommu->dev, PTR_ERR(iommu->clk),
729a063158bSDavid Heidelberg "could not get iommu_clk\n");
730f7f125efSSricharan R }
731f7f125efSSricharan R
732f7f125efSSricharan R ret = clk_prepare(iommu->clk);
733f7f125efSSricharan R if (ret) {
734f7f125efSSricharan R clk_unprepare(iommu->pclk);
735a063158bSDavid Heidelberg return dev_err_probe(iommu->dev, ret, "could not prepare iommu_clk\n");
736f7f125efSSricharan R }
737f7f125efSSricharan R
738f7f125efSSricharan R r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
739f7f125efSSricharan R iommu->base = devm_ioremap_resource(iommu->dev, r);
740f7f125efSSricharan R if (IS_ERR(iommu->base)) {
741a063158bSDavid Heidelberg ret = dev_err_probe(iommu->dev, PTR_ERR(iommu->base), "could not get iommu base\n");
742f7f125efSSricharan R goto fail;
743f7f125efSSricharan R }
74442df43b3SJoerg Roedel ioaddr = r->start;
745f7f125efSSricharan R
746f7f125efSSricharan R iommu->irq = platform_get_irq(pdev, 0);
747f7f125efSSricharan R if (iommu->irq < 0) {
748f7f125efSSricharan R ret = -ENODEV;
749f7f125efSSricharan R goto fail;
750f7f125efSSricharan R }
751f7f125efSSricharan R
752f7f125efSSricharan R ret = of_property_read_u32(iommu->dev->of_node, "qcom,ncb", &val);
753f7f125efSSricharan R if (ret) {
754f7f125efSSricharan R dev_err(iommu->dev, "could not get ncb\n");
755f7f125efSSricharan R goto fail;
756f7f125efSSricharan R }
757f7f125efSSricharan R iommu->ncb = val;
758f7f125efSSricharan R
759f7f125efSSricharan R msm_iommu_reset(iommu->base, iommu->ncb);
760f7f125efSSricharan R SET_M(iommu->base, 0, 1);
761f7f125efSSricharan R SET_PAR(iommu->base, 0, 0);
762f7f125efSSricharan R SET_V2PCFG(iommu->base, 0, 1);
763f7f125efSSricharan R SET_V2PPR(iommu->base, 0, 0);
764f7f125efSSricharan R par = GET_PAR(iommu->base, 0);
765f7f125efSSricharan R SET_V2PCFG(iommu->base, 0, 0);
766f7f125efSSricharan R SET_M(iommu->base, 0, 0);
767f7f125efSSricharan R
768f7f125efSSricharan R if (!par) {
769f7f125efSSricharan R pr_err("Invalid PAR value detected\n");
770f7f125efSSricharan R ret = -ENODEV;
771f7f125efSSricharan R goto fail;
772f7f125efSSricharan R }
773f7f125efSSricharan R
774f7f125efSSricharan R ret = devm_request_threaded_irq(iommu->dev, iommu->irq, NULL,
775f7f125efSSricharan R msm_iommu_fault_handler,
776f7f125efSSricharan R IRQF_ONESHOT | IRQF_SHARED,
777f7f125efSSricharan R "msm_iommu_secure_irpt_handler",
778f7f125efSSricharan R iommu);
779f7f125efSSricharan R if (ret) {
780f7f125efSSricharan R pr_err("Request IRQ %d failed with ret=%d\n", iommu->irq, ret);
781f7f125efSSricharan R goto fail;
782f7f125efSSricharan R }
783f7f125efSSricharan R
784f7f125efSSricharan R list_add(&iommu->dev_node, &qcom_iommu_devices);
78542df43b3SJoerg Roedel
78642df43b3SJoerg Roedel ret = iommu_device_sysfs_add(&iommu->iommu, iommu->dev, NULL,
78742df43b3SJoerg Roedel "msm-smmu.%pa", &ioaddr);
78842df43b3SJoerg Roedel if (ret) {
78942df43b3SJoerg Roedel pr_err("Could not add msm-smmu at %pa to sysfs\n", &ioaddr);
79042df43b3SJoerg Roedel goto fail;
79142df43b3SJoerg Roedel }
79242df43b3SJoerg Roedel
7932d471b20SRobin Murphy ret = iommu_device_register(&iommu->iommu, &msm_iommu_ops, &pdev->dev);
79442df43b3SJoerg Roedel if (ret) {
79542df43b3SJoerg Roedel pr_err("Could not register msm-smmu at %pa\n", &ioaddr);
79642df43b3SJoerg Roedel goto fail;
79742df43b3SJoerg Roedel }
79842df43b3SJoerg Roedel
799f7f125efSSricharan R pr_info("device mapped at %p, irq %d with %d ctx banks\n",
800f7f125efSSricharan R iommu->base, iommu->irq, iommu->ncb);
801f7f125efSSricharan R
802f7f125efSSricharan R return ret;
803f7f125efSSricharan R fail:
804f7f125efSSricharan R clk_unprepare(iommu->clk);
805f7f125efSSricharan R clk_unprepare(iommu->pclk);
806f7f125efSSricharan R return ret;
807f7f125efSSricharan R }
808f7f125efSSricharan R
809f7f125efSSricharan R static const struct of_device_id msm_iommu_dt_match[] = {
810f7f125efSSricharan R { .compatible = "qcom,apq8064-iommu" },
811f7f125efSSricharan R {}
812f7f125efSSricharan R };
813f7f125efSSricharan R
msm_iommu_remove(struct platform_device * pdev)814*816a4afcSUwe Kleine-König static void msm_iommu_remove(struct platform_device *pdev)
815f7f125efSSricharan R {
816f7f125efSSricharan R struct msm_iommu_dev *iommu = platform_get_drvdata(pdev);
817f7f125efSSricharan R
818f7f125efSSricharan R clk_unprepare(iommu->clk);
819f7f125efSSricharan R clk_unprepare(iommu->pclk);
820f7f125efSSricharan R }
821f7f125efSSricharan R
822f7f125efSSricharan R static struct platform_driver msm_iommu_driver = {
823f7f125efSSricharan R .driver = {
824f7f125efSSricharan R .name = "msm_iommu",
825f7f125efSSricharan R .of_match_table = msm_iommu_dt_match,
826f7f125efSSricharan R },
827f7f125efSSricharan R .probe = msm_iommu_probe,
828*816a4afcSUwe Kleine-König .remove_new = msm_iommu_remove,
829f7f125efSSricharan R };
8306b813e0eSRobin Murphy builtin_platform_driver(msm_iommu_driver);
831