2 * Copyright (c) 2011,2016 Samsung Electronics Co., Ltd.
3 * http://www.samsung.com
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
10 #ifdef CONFIG_EXYNOS_IOMMU_DEBUG
14 #include <linux/clk.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/err.h>
18 #include <linux/iommu.h>
19 #include <linux/interrupt.h>
20 #include <linux/list.h>
22 #include <linux/of_iommu.h>
23 #include <linux/of_platform.h>
24 #include <linux/platform_device.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/slab.h>
27 #include <linux/dma-iommu.h>
29 typedef u32 sysmmu_iova_t;
30 typedef u32 sysmmu_pte_t;
32 /* We do not consider super section mapping (16MB) */
34 #define LPAGE_ORDER 16
35 #define SPAGE_ORDER 12
37 #define SECT_SIZE (1 << SECT_ORDER)
38 #define LPAGE_SIZE (1 << LPAGE_ORDER)
39 #define SPAGE_SIZE (1 << SPAGE_ORDER)
41 #define SECT_MASK (~(SECT_SIZE - 1))
42 #define LPAGE_MASK (~(LPAGE_SIZE - 1))
43 #define SPAGE_MASK (~(SPAGE_SIZE - 1))
45 #define lv1ent_fault(sent) ((*(sent) == ZERO_LV2LINK) || \
46 ((*(sent) & 3) == 0) || ((*(sent) & 3) == 3))
47 #define lv1ent_zero(sent) (*(sent) == ZERO_LV2LINK)
48 #define lv1ent_page_zero(sent) ((*(sent) & 3) == 1)
49 #define lv1ent_page(sent) ((*(sent) != ZERO_LV2LINK) && \
51 #define lv1ent_section(sent) ((*(sent) & 3) == 2)
53 #define lv2ent_fault(pent) ((*(pent) & 3) == 0)
54 #define lv2ent_small(pent) ((*(pent) & 2) == 2)
55 #define lv2ent_large(pent) ((*(pent) & 3) == 1)
58 * v1.x - v3.x SYSMMU supports 32bit physical and 32bit virtual address spaces
59 * v5.0 introduced support for 36bit physical address space by shifting
60 * all page entry values by 4 bits.
61 * All SYSMMU controllers in the system support the address spaces of the same
62 * size, so PG_ENT_SHIFT can be initialized on first SYSMMU probe to proper
65 static short PG_ENT_SHIFT = -1;
66 #define SYSMMU_PG_ENT_SHIFT 0
67 #define SYSMMU_V5_PG_ENT_SHIFT 4
69 #define sect_to_phys(ent) (((phys_addr_t) ent) << PG_ENT_SHIFT)
70 #define section_phys(sent) (sect_to_phys(*(sent)) & SECT_MASK)
71 #define section_offs(iova) (iova & (SECT_SIZE - 1))
72 #define lpage_phys(pent) (sect_to_phys(*(pent)) & LPAGE_MASK)
73 #define lpage_offs(iova) (iova & (LPAGE_SIZE - 1))
74 #define spage_phys(pent) (sect_to_phys(*(pent)) & SPAGE_MASK)
75 #define spage_offs(iova) (iova & (SPAGE_SIZE - 1))
77 #define NUM_LV1ENTRIES 4096
78 #define NUM_LV2ENTRIES (SECT_SIZE / SPAGE_SIZE)
80 static u32 lv1ent_offset(sysmmu_iova_t iova)
82 return iova >> SECT_ORDER;
85 static u32 lv2ent_offset(sysmmu_iova_t iova)
87 return (iova >> SPAGE_ORDER) & (NUM_LV2ENTRIES - 1);
90 #define LV1TABLE_SIZE (NUM_LV1ENTRIES * sizeof(sysmmu_pte_t))
91 #define LV2TABLE_SIZE (NUM_LV2ENTRIES * sizeof(sysmmu_pte_t))
93 #define SPAGES_PER_LPAGE (LPAGE_SIZE / SPAGE_SIZE)
94 #define lv2table_base(sent) (sect_to_phys(*(sent) & 0xFFFFFFC0))
96 #define mk_lv1ent_sect(pa) ((pa >> PG_ENT_SHIFT) | 2)
97 #define mk_lv1ent_page(pa) ((pa >> PG_ENT_SHIFT) | 1)
98 #define mk_lv2ent_lpage(pa) ((pa >> PG_ENT_SHIFT) | 1)
99 #define mk_lv2ent_spage(pa) ((pa >> PG_ENT_SHIFT) | 2)
101 #define CTRL_ENABLE 0x5
102 #define CTRL_BLOCK 0x7
103 #define CTRL_DISABLE 0x0
106 #define CFG_QOS(n) ((n & 0xF) << 7)
107 #define CFG_ACGEN (1 << 24) /* System MMU 3.3 only */
108 #define CFG_SYSSEL (1 << 22) /* System MMU 3.2 only */
109 #define CFG_FLPDCACHE (1 << 20) /* System MMU 3.2+ only */
111 /* common registers */
112 #define REG_MMU_CTRL 0x000
113 #define REG_MMU_CFG 0x004
114 #define REG_MMU_STATUS 0x008
115 #define REG_MMU_VERSION 0x034
117 #define MMU_MAJ_VER(val) ((val) >> 7)
118 #define MMU_MIN_VER(val) ((val) & 0x7F)
119 #define MMU_RAW_VER(reg) (((reg) >> 21) & ((1 << 11) - 1)) /* 11 bits */
121 #define MAKE_MMU_VER(maj, min) ((((maj) & 0xF) << 7) | ((min) & 0x7F))
123 /* v1.x - v3.x registers */
124 #define REG_MMU_FLUSH 0x00C
125 #define REG_MMU_FLUSH_ENTRY 0x010
126 #define REG_PT_BASE_ADDR 0x014
127 #define REG_INT_STATUS 0x018
128 #define REG_INT_CLEAR 0x01C
130 #define REG_PAGE_FAULT_ADDR 0x024
131 #define REG_AW_FAULT_ADDR 0x028
132 #define REG_AR_FAULT_ADDR 0x02C
133 #define REG_DEFAULT_SLAVE_ADDR 0x030
136 #define REG_V5_PT_BASE_PFN 0x00C
137 #define REG_V5_MMU_FLUSH_ALL 0x010
138 #define REG_V5_MMU_FLUSH_ENTRY 0x014
139 #define REG_V5_INT_STATUS 0x060
140 #define REG_V5_INT_CLEAR 0x064
141 #define REG_V5_FAULT_AR_VA 0x070
142 #define REG_V5_FAULT_AW_VA 0x080
144 #define has_sysmmu(dev) (dev->archdata.iommu != NULL)
146 static struct device *dma_dev;
147 static struct kmem_cache *lv2table_kmem_cache;
148 static sysmmu_pte_t *zero_lv2_table;
149 #define ZERO_LV2LINK mk_lv1ent_page(virt_to_phys(zero_lv2_table))
151 static sysmmu_pte_t *section_entry(sysmmu_pte_t *pgtable, sysmmu_iova_t iova)
153 return pgtable + lv1ent_offset(iova);
156 static sysmmu_pte_t *page_entry(sysmmu_pte_t *sent, sysmmu_iova_t iova)
158 return (sysmmu_pte_t *)phys_to_virt(
159 lv2table_base(sent)) + lv2ent_offset(iova);
163 * IOMMU fault information register
165 struct sysmmu_fault_info {
166 unsigned int bit; /* bit number in STATUS register */
167 unsigned short addr_reg; /* register to read VA fault address */
168 const char *name; /* human readable fault name */
169 unsigned int type; /* fault type for report_iommu_fault */
172 static const struct sysmmu_fault_info sysmmu_faults[] = {
173 { 0, REG_PAGE_FAULT_ADDR, "PAGE", IOMMU_FAULT_READ },
174 { 1, REG_AR_FAULT_ADDR, "AR MULTI-HIT", IOMMU_FAULT_READ },
175 { 2, REG_AW_FAULT_ADDR, "AW MULTI-HIT", IOMMU_FAULT_WRITE },
176 { 3, REG_DEFAULT_SLAVE_ADDR, "BUS ERROR", IOMMU_FAULT_READ },
177 { 4, REG_AR_FAULT_ADDR, "AR SECURITY PROTECTION", IOMMU_FAULT_READ },
178 { 5, REG_AR_FAULT_ADDR, "AR ACCESS PROTECTION", IOMMU_FAULT_READ },
179 { 6, REG_AW_FAULT_ADDR, "AW SECURITY PROTECTION", IOMMU_FAULT_WRITE },
180 { 7, REG_AW_FAULT_ADDR, "AW ACCESS PROTECTION", IOMMU_FAULT_WRITE },
183 static const struct sysmmu_fault_info sysmmu_v5_faults[] = {
184 { 0, REG_V5_FAULT_AR_VA, "AR PTW", IOMMU_FAULT_READ },
185 { 1, REG_V5_FAULT_AR_VA, "AR PAGE", IOMMU_FAULT_READ },
186 { 2, REG_V5_FAULT_AR_VA, "AR MULTI-HIT", IOMMU_FAULT_READ },
187 { 3, REG_V5_FAULT_AR_VA, "AR ACCESS PROTECTION", IOMMU_FAULT_READ },
188 { 4, REG_V5_FAULT_AR_VA, "AR SECURITY PROTECTION", IOMMU_FAULT_READ },
189 { 16, REG_V5_FAULT_AW_VA, "AW PTW", IOMMU_FAULT_WRITE },
190 { 17, REG_V5_FAULT_AW_VA, "AW PAGE", IOMMU_FAULT_WRITE },
191 { 18, REG_V5_FAULT_AW_VA, "AW MULTI-HIT", IOMMU_FAULT_WRITE },
192 { 19, REG_V5_FAULT_AW_VA, "AW ACCESS PROTECTION", IOMMU_FAULT_WRITE },
193 { 20, REG_V5_FAULT_AW_VA, "AW SECURITY PROTECTION", IOMMU_FAULT_WRITE },
197 * This structure is attached to dev.archdata.iommu of the master device
198 * on device add, contains a list of SYSMMU controllers defined by device tree,
199 * which are bound to given master device. It is usually referenced by 'owner'
202 struct exynos_iommu_owner {
203 struct list_head controllers; /* list of sysmmu_drvdata.owner_node */
204 struct iommu_domain *domain; /* domain this device is attached */
208 * This structure exynos specific generalization of struct iommu_domain.
209 * It contains list of SYSMMU controllers from all master devices, which has
210 * been attached to this domain and page tables of IO address space defined by
211 * it. It is usually referenced by 'domain' pointer.
213 struct exynos_iommu_domain {
214 struct list_head clients; /* list of sysmmu_drvdata.domain_node */
215 sysmmu_pte_t *pgtable; /* lv1 page table, 16KB */
216 short *lv2entcnt; /* free lv2 entry counter for each section */
217 spinlock_t lock; /* lock for modyfying list of clients */
218 spinlock_t pgtablelock; /* lock for modifying page table @ pgtable */
219 struct iommu_domain domain; /* generic domain data structure */
223 * This structure hold all data of a single SYSMMU controller, this includes
224 * hw resources like registers and clocks, pointers and list nodes to connect
225 * it to all other structures, internal state and parameters read from device
226 * tree. It is usually referenced by 'data' pointer.
228 struct sysmmu_drvdata {
229 struct device *sysmmu; /* SYSMMU controller device */
230 struct device *master; /* master device (owner) */
231 void __iomem *sfrbase; /* our registers */
232 struct clk *clk; /* SYSMMU's clock */
233 struct clk *aclk; /* SYSMMU's aclk clock */
234 struct clk *pclk; /* SYSMMU's pclk clock */
235 struct clk *clk_master; /* master's device clock */
236 int activations; /* number of calls to sysmmu_enable */
237 spinlock_t lock; /* lock for modyfying state */
238 struct exynos_iommu_domain *domain; /* domain we belong to */
239 struct list_head domain_node; /* node for domain clients list */
240 struct list_head owner_node; /* node for owner controllers list */
241 phys_addr_t pgtable; /* assigned page table structure */
242 unsigned int version; /* our version */
245 static struct exynos_iommu_domain *to_exynos_domain(struct iommu_domain *dom)
247 return container_of(dom, struct exynos_iommu_domain, domain);
250 static bool set_sysmmu_active(struct sysmmu_drvdata *data)
252 /* return true if the System MMU was not active previously
253 and it needs to be initialized */
254 return ++data->activations == 1;
257 static bool set_sysmmu_inactive(struct sysmmu_drvdata *data)
259 /* return true if the System MMU is needed to be disabled */
260 BUG_ON(data->activations < 1);
261 return --data->activations == 0;
264 static bool is_sysmmu_active(struct sysmmu_drvdata *data)
266 return data->activations > 0;
269 static void sysmmu_unblock(struct sysmmu_drvdata *data)
271 writel(CTRL_ENABLE, data->sfrbase + REG_MMU_CTRL);
274 static bool sysmmu_block(struct sysmmu_drvdata *data)
278 writel(CTRL_BLOCK, data->sfrbase + REG_MMU_CTRL);
279 while ((i > 0) && !(readl(data->sfrbase + REG_MMU_STATUS) & 1))
282 if (!(readl(data->sfrbase + REG_MMU_STATUS) & 1)) {
283 sysmmu_unblock(data);
290 static void __sysmmu_tlb_invalidate(struct sysmmu_drvdata *data)
292 if (MMU_MAJ_VER(data->version) < 5)
293 writel(0x1, data->sfrbase + REG_MMU_FLUSH);
295 writel(0x1, data->sfrbase + REG_V5_MMU_FLUSH_ALL);
298 static void __sysmmu_tlb_invalidate_entry(struct sysmmu_drvdata *data,
299 sysmmu_iova_t iova, unsigned int num_inv)
303 for (i = 0; i < num_inv; i++) {
304 if (MMU_MAJ_VER(data->version) < 5)
305 writel((iova & SPAGE_MASK) | 1,
306 data->sfrbase + REG_MMU_FLUSH_ENTRY);
308 writel((iova & SPAGE_MASK) | 1,
309 data->sfrbase + REG_V5_MMU_FLUSH_ENTRY);
314 static void __sysmmu_set_ptbase(struct sysmmu_drvdata *data, phys_addr_t pgd)
316 if (MMU_MAJ_VER(data->version) < 5)
317 writel(pgd, data->sfrbase + REG_PT_BASE_ADDR);
319 writel(pgd >> PAGE_SHIFT,
320 data->sfrbase + REG_V5_PT_BASE_PFN);
322 __sysmmu_tlb_invalidate(data);
325 static void __sysmmu_enable_clocks(struct sysmmu_drvdata *data)
327 BUG_ON(clk_prepare_enable(data->clk_master));
328 BUG_ON(clk_prepare_enable(data->clk));
329 BUG_ON(clk_prepare_enable(data->pclk));
330 BUG_ON(clk_prepare_enable(data->aclk));
333 static void __sysmmu_disable_clocks(struct sysmmu_drvdata *data)
335 clk_disable_unprepare(data->aclk);
336 clk_disable_unprepare(data->pclk);
337 clk_disable_unprepare(data->clk);
338 clk_disable_unprepare(data->clk_master);
341 static void __sysmmu_get_version(struct sysmmu_drvdata *data)
345 __sysmmu_enable_clocks(data);
347 ver = readl(data->sfrbase + REG_MMU_VERSION);
349 /* controllers on some SoCs don't report proper version */
350 if (ver == 0x80000001u)
351 data->version = MAKE_MMU_VER(1, 0);
353 data->version = MMU_RAW_VER(ver);
355 dev_dbg(data->sysmmu, "hardware version: %d.%d\n",
356 MMU_MAJ_VER(data->version), MMU_MIN_VER(data->version));
358 __sysmmu_disable_clocks(data);
361 static void show_fault_information(struct sysmmu_drvdata *data,
362 const struct sysmmu_fault_info *finfo,
363 sysmmu_iova_t fault_addr)
367 dev_err(data->sysmmu, "%s FAULT occurred at %#x (page table base: %pa)\n",
368 finfo->name, fault_addr, &data->pgtable);
369 ent = section_entry(phys_to_virt(data->pgtable), fault_addr);
370 dev_err(data->sysmmu, "\tLv1 entry: %#x\n", *ent);
371 if (lv1ent_page(ent)) {
372 ent = page_entry(ent, fault_addr);
373 dev_err(data->sysmmu, "\t Lv2 entry: %#x\n", *ent);
377 static irqreturn_t exynos_sysmmu_irq(int irq, void *dev_id)
379 /* SYSMMU is in blocked state when interrupt occurred. */
380 struct sysmmu_drvdata *data = dev_id;
381 const struct sysmmu_fault_info *finfo;
382 unsigned int i, n, itype;
383 sysmmu_iova_t fault_addr = -1;
384 unsigned short reg_status, reg_clear;
387 WARN_ON(!is_sysmmu_active(data));
389 if (MMU_MAJ_VER(data->version) < 5) {
390 reg_status = REG_INT_STATUS;
391 reg_clear = REG_INT_CLEAR;
392 finfo = sysmmu_faults;
393 n = ARRAY_SIZE(sysmmu_faults);
395 reg_status = REG_V5_INT_STATUS;
396 reg_clear = REG_V5_INT_CLEAR;
397 finfo = sysmmu_v5_faults;
398 n = ARRAY_SIZE(sysmmu_v5_faults);
401 spin_lock(&data->lock);
403 clk_enable(data->clk_master);
405 itype = __ffs(readl(data->sfrbase + reg_status));
406 for (i = 0; i < n; i++, finfo++)
407 if (finfo->bit == itype)
409 /* unknown/unsupported fault */
412 /* print debug message */
413 fault_addr = readl(data->sfrbase + finfo->addr_reg);
414 show_fault_information(data, finfo, fault_addr);
417 ret = report_iommu_fault(&data->domain->domain,
418 data->master, fault_addr, finfo->type);
419 /* fault is not recovered by fault handler */
422 writel(1 << itype, data->sfrbase + reg_clear);
424 sysmmu_unblock(data);
426 clk_disable(data->clk_master);
428 spin_unlock(&data->lock);
433 static void __sysmmu_disable_nocount(struct sysmmu_drvdata *data)
435 clk_enable(data->clk_master);
437 writel(CTRL_DISABLE, data->sfrbase + REG_MMU_CTRL);
438 writel(0, data->sfrbase + REG_MMU_CFG);
440 __sysmmu_disable_clocks(data);
443 static bool __sysmmu_disable(struct sysmmu_drvdata *data)
448 spin_lock_irqsave(&data->lock, flags);
450 disabled = set_sysmmu_inactive(data);
456 __sysmmu_disable_nocount(data);
458 dev_dbg(data->sysmmu, "Disabled\n");
460 dev_dbg(data->sysmmu, "%d times left to disable\n",
464 spin_unlock_irqrestore(&data->lock, flags);
469 static void __sysmmu_init_config(struct sysmmu_drvdata *data)
473 if (data->version <= MAKE_MMU_VER(3, 1))
474 cfg = CFG_LRU | CFG_QOS(15);
475 else if (data->version <= MAKE_MMU_VER(3, 2))
476 cfg = CFG_LRU | CFG_QOS(15) | CFG_FLPDCACHE | CFG_SYSSEL;
478 cfg = CFG_QOS(15) | CFG_FLPDCACHE | CFG_ACGEN;
480 writel(cfg, data->sfrbase + REG_MMU_CFG);
483 static void __sysmmu_enable_nocount(struct sysmmu_drvdata *data)
485 __sysmmu_enable_clocks(data);
487 writel(CTRL_BLOCK, data->sfrbase + REG_MMU_CTRL);
489 __sysmmu_init_config(data);
491 __sysmmu_set_ptbase(data, data->pgtable);
493 writel(CTRL_ENABLE, data->sfrbase + REG_MMU_CTRL);
496 * SYSMMU driver keeps master's clock enabled only for the short
497 * time, while accessing the registers. For performing address
498 * translation during DMA transaction it relies on the client
499 * driver to enable it.
501 clk_disable(data->clk_master);
504 static int __sysmmu_enable(struct sysmmu_drvdata *data, phys_addr_t pgtable,
505 struct exynos_iommu_domain *domain)
510 spin_lock_irqsave(&data->lock, flags);
511 if (set_sysmmu_active(data)) {
512 data->pgtable = pgtable;
513 data->domain = domain;
515 __sysmmu_enable_nocount(data);
517 dev_dbg(data->sysmmu, "Enabled\n");
519 ret = (pgtable == data->pgtable) ? 1 : -EBUSY;
521 dev_dbg(data->sysmmu, "already enabled\n");
524 if (WARN_ON(ret < 0))
525 set_sysmmu_inactive(data); /* decrement count */
527 spin_unlock_irqrestore(&data->lock, flags);
532 static void sysmmu_tlb_invalidate_flpdcache(struct sysmmu_drvdata *data,
538 spin_lock_irqsave(&data->lock, flags);
539 if (is_sysmmu_active(data) && data->version >= MAKE_MMU_VER(3, 3)) {
540 clk_enable(data->clk_master);
541 __sysmmu_tlb_invalidate_entry(data, iova, 1);
542 clk_disable(data->clk_master);
544 spin_unlock_irqrestore(&data->lock, flags);
548 static void sysmmu_tlb_invalidate_entry(struct sysmmu_drvdata *data,
549 sysmmu_iova_t iova, size_t size)
553 spin_lock_irqsave(&data->lock, flags);
554 if (is_sysmmu_active(data)) {
555 unsigned int num_inv = 1;
557 clk_enable(data->clk_master);
560 * L2TLB invalidation required
561 * 4KB page: 1 invalidation
562 * 64KB page: 16 invalidations
563 * 1MB page: 64 invalidations
564 * because it is set-associative TLB
565 * with 8-way and 64 sets.
566 * 1MB page can be cached in one of all sets.
567 * 64KB page can be one of 16 consecutive sets.
569 if (MMU_MAJ_VER(data->version) == 2)
570 num_inv = min_t(unsigned int, size / PAGE_SIZE, 64);
572 if (sysmmu_block(data)) {
573 __sysmmu_tlb_invalidate_entry(data, iova, num_inv);
574 sysmmu_unblock(data);
576 clk_disable(data->clk_master);
578 dev_dbg(data->master,
579 "disabled. Skipping TLB invalidation @ %#x\n", iova);
581 spin_unlock_irqrestore(&data->lock, flags);
584 static int __init exynos_sysmmu_probe(struct platform_device *pdev)
587 struct device *dev = &pdev->dev;
588 struct sysmmu_drvdata *data;
589 struct resource *res;
591 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
595 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
596 data->sfrbase = devm_ioremap_resource(dev, res);
597 if (IS_ERR(data->sfrbase))
598 return PTR_ERR(data->sfrbase);
600 irq = platform_get_irq(pdev, 0);
602 dev_err(dev, "Unable to find IRQ resource\n");
606 ret = devm_request_irq(dev, irq, exynos_sysmmu_irq, 0,
607 dev_name(dev), data);
609 dev_err(dev, "Unabled to register handler of irq %d\n", irq);
613 data->clk = devm_clk_get(dev, "sysmmu");
614 if (PTR_ERR(data->clk) == -ENOENT)
616 else if (IS_ERR(data->clk))
617 return PTR_ERR(data->clk);
619 data->aclk = devm_clk_get(dev, "aclk");
620 if (PTR_ERR(data->aclk) == -ENOENT)
622 else if (IS_ERR(data->aclk))
623 return PTR_ERR(data->aclk);
625 data->pclk = devm_clk_get(dev, "pclk");
626 if (PTR_ERR(data->pclk) == -ENOENT)
628 else if (IS_ERR(data->pclk))
629 return PTR_ERR(data->pclk);
631 if (!data->clk && (!data->aclk || !data->pclk)) {
632 dev_err(dev, "Failed to get device clock(s)!\n");
636 data->clk_master = devm_clk_get(dev, "master");
637 if (PTR_ERR(data->clk_master) == -ENOENT)
638 data->clk_master = NULL;
639 else if (IS_ERR(data->clk_master))
640 return PTR_ERR(data->clk_master);
643 spin_lock_init(&data->lock);
645 platform_set_drvdata(pdev, data);
647 __sysmmu_get_version(data);
648 if (PG_ENT_SHIFT < 0) {
649 if (MMU_MAJ_VER(data->version) < 5)
650 PG_ENT_SHIFT = SYSMMU_PG_ENT_SHIFT;
652 PG_ENT_SHIFT = SYSMMU_V5_PG_ENT_SHIFT;
655 pm_runtime_enable(dev);
660 #ifdef CONFIG_PM_SLEEP
661 static int exynos_sysmmu_suspend(struct device *dev)
663 struct sysmmu_drvdata *data = dev_get_drvdata(dev);
665 dev_dbg(dev, "suspend\n");
666 if (is_sysmmu_active(data)) {
667 __sysmmu_disable_nocount(data);
673 static int exynos_sysmmu_resume(struct device *dev)
675 struct sysmmu_drvdata *data = dev_get_drvdata(dev);
677 dev_dbg(dev, "resume\n");
678 if (is_sysmmu_active(data)) {
679 pm_runtime_get_sync(dev);
680 __sysmmu_enable_nocount(data);
686 static const struct dev_pm_ops sysmmu_pm_ops = {
687 SET_LATE_SYSTEM_SLEEP_PM_OPS(exynos_sysmmu_suspend, exynos_sysmmu_resume)
690 static const struct of_device_id sysmmu_of_match[] __initconst = {
691 { .compatible = "samsung,exynos-sysmmu", },
695 static struct platform_driver exynos_sysmmu_driver __refdata = {
696 .probe = exynos_sysmmu_probe,
698 .name = "exynos-sysmmu",
699 .of_match_table = sysmmu_of_match,
700 .pm = &sysmmu_pm_ops,
701 .suppress_bind_attrs = true,
705 static inline void update_pte(sysmmu_pte_t *ent, sysmmu_pte_t val)
707 dma_sync_single_for_cpu(dma_dev, virt_to_phys(ent), sizeof(*ent),
710 dma_sync_single_for_device(dma_dev, virt_to_phys(ent), sizeof(*ent),
714 static struct iommu_domain *exynos_iommu_domain_alloc(unsigned type)
716 struct exynos_iommu_domain *domain;
720 /* Check if correct PTE offsets are initialized */
721 BUG_ON(PG_ENT_SHIFT < 0 || !dma_dev);
723 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
727 if (type == IOMMU_DOMAIN_DMA) {
728 if (iommu_get_dma_cookie(&domain->domain) != 0)
730 } else if (type != IOMMU_DOMAIN_UNMANAGED) {
734 domain->pgtable = (sysmmu_pte_t *)__get_free_pages(GFP_KERNEL, 2);
735 if (!domain->pgtable)
738 domain->lv2entcnt = (short *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1);
739 if (!domain->lv2entcnt)
742 /* Workaround for System MMU v3.3 to prevent caching 1MiB mapping */
743 for (i = 0; i < NUM_LV1ENTRIES; i += 8) {
744 domain->pgtable[i + 0] = ZERO_LV2LINK;
745 domain->pgtable[i + 1] = ZERO_LV2LINK;
746 domain->pgtable[i + 2] = ZERO_LV2LINK;
747 domain->pgtable[i + 3] = ZERO_LV2LINK;
748 domain->pgtable[i + 4] = ZERO_LV2LINK;
749 domain->pgtable[i + 5] = ZERO_LV2LINK;
750 domain->pgtable[i + 6] = ZERO_LV2LINK;
751 domain->pgtable[i + 7] = ZERO_LV2LINK;
754 handle = dma_map_single(dma_dev, domain->pgtable, LV1TABLE_SIZE,
756 /* For mapping page table entries we rely on dma == phys */
757 BUG_ON(handle != virt_to_phys(domain->pgtable));
759 spin_lock_init(&domain->lock);
760 spin_lock_init(&domain->pgtablelock);
761 INIT_LIST_HEAD(&domain->clients);
763 domain->domain.geometry.aperture_start = 0;
764 domain->domain.geometry.aperture_end = ~0UL;
765 domain->domain.geometry.force_aperture = true;
767 return &domain->domain;
770 free_pages((unsigned long)domain->pgtable, 2);
772 if (type == IOMMU_DOMAIN_DMA)
773 iommu_put_dma_cookie(&domain->domain);
779 static void exynos_iommu_domain_free(struct iommu_domain *iommu_domain)
781 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
782 struct sysmmu_drvdata *data, *next;
786 WARN_ON(!list_empty(&domain->clients));
788 spin_lock_irqsave(&domain->lock, flags);
790 list_for_each_entry_safe(data, next, &domain->clients, domain_node) {
791 if (__sysmmu_disable(data))
793 list_del_init(&data->domain_node);
796 spin_unlock_irqrestore(&domain->lock, flags);
798 if (iommu_domain->type == IOMMU_DOMAIN_DMA)
799 iommu_put_dma_cookie(iommu_domain);
801 dma_unmap_single(dma_dev, virt_to_phys(domain->pgtable), LV1TABLE_SIZE,
804 for (i = 0; i < NUM_LV1ENTRIES; i++)
805 if (lv1ent_page(domain->pgtable + i)) {
806 phys_addr_t base = lv2table_base(domain->pgtable + i);
808 dma_unmap_single(dma_dev, base, LV2TABLE_SIZE,
810 kmem_cache_free(lv2table_kmem_cache,
814 free_pages((unsigned long)domain->pgtable, 2);
815 free_pages((unsigned long)domain->lv2entcnt, 1);
819 static void exynos_iommu_detach_device(struct iommu_domain *iommu_domain,
822 struct exynos_iommu_owner *owner = dev->archdata.iommu;
823 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
824 phys_addr_t pagetable = virt_to_phys(domain->pgtable);
825 struct sysmmu_drvdata *data, *next;
829 if (!has_sysmmu(dev) || owner->domain != iommu_domain)
832 spin_lock_irqsave(&domain->lock, flags);
833 list_for_each_entry_safe(data, next, &domain->clients, domain_node) {
834 if (data->master == dev) {
835 if (__sysmmu_disable(data)) {
837 list_del_init(&data->domain_node);
839 pm_runtime_put(data->sysmmu);
843 spin_unlock_irqrestore(&domain->lock, flags);
845 owner->domain = NULL;
848 dev_dbg(dev, "%s: Detached IOMMU with pgtable %pa\n",
849 __func__, &pagetable);
851 dev_err(dev, "%s: No IOMMU is attached\n", __func__);
854 static int exynos_iommu_attach_device(struct iommu_domain *iommu_domain,
857 struct exynos_iommu_owner *owner = dev->archdata.iommu;
858 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
859 struct sysmmu_drvdata *data;
860 phys_addr_t pagetable = virt_to_phys(domain->pgtable);
864 if (!has_sysmmu(dev))
868 exynos_iommu_detach_device(owner->domain, dev);
870 list_for_each_entry(data, &owner->controllers, owner_node) {
871 pm_runtime_get_sync(data->sysmmu);
872 ret = __sysmmu_enable(data, pagetable, domain);
876 spin_lock_irqsave(&domain->lock, flags);
877 list_add_tail(&data->domain_node, &domain->clients);
878 spin_unlock_irqrestore(&domain->lock, flags);
883 dev_err(dev, "%s: Failed to attach IOMMU with pgtable %pa\n",
884 __func__, &pagetable);
888 owner->domain = iommu_domain;
889 dev_dbg(dev, "%s: Attached IOMMU with pgtable %pa %s\n",
890 __func__, &pagetable, (ret == 0) ? "" : ", again");
895 static sysmmu_pte_t *alloc_lv2entry(struct exynos_iommu_domain *domain,
896 sysmmu_pte_t *sent, sysmmu_iova_t iova, short *pgcounter)
898 if (lv1ent_section(sent)) {
899 WARN(1, "Trying mapping on %#08x mapped with 1MiB page", iova);
900 return ERR_PTR(-EADDRINUSE);
903 if (lv1ent_fault(sent)) {
905 bool need_flush_flpd_cache = lv1ent_zero(sent);
907 pent = kmem_cache_zalloc(lv2table_kmem_cache, GFP_ATOMIC);
908 BUG_ON((uintptr_t)pent & (LV2TABLE_SIZE - 1));
910 return ERR_PTR(-ENOMEM);
912 update_pte(sent, mk_lv1ent_page(virt_to_phys(pent)));
913 kmemleak_ignore(pent);
914 *pgcounter = NUM_LV2ENTRIES;
915 dma_map_single(dma_dev, pent, LV2TABLE_SIZE, DMA_TO_DEVICE);
918 * If pre-fetched SLPD is a faulty SLPD in zero_l2_table,
919 * FLPD cache may cache the address of zero_l2_table. This
920 * function replaces the zero_l2_table with new L2 page table
921 * to write valid mappings.
922 * Accessing the valid area may cause page fault since FLPD
923 * cache may still cache zero_l2_table for the valid area
924 * instead of new L2 page table that has the mapping
925 * information of the valid area.
926 * Thus any replacement of zero_l2_table with other valid L2
927 * page table must involve FLPD cache invalidation for System
929 * FLPD cache invalidation is performed with TLB invalidation
930 * by VPN without blocking. It is safe to invalidate TLB without
931 * blocking because the target address of TLB invalidation is
932 * not currently mapped.
934 if (need_flush_flpd_cache) {
935 struct sysmmu_drvdata *data;
937 spin_lock(&domain->lock);
938 list_for_each_entry(data, &domain->clients, domain_node)
939 sysmmu_tlb_invalidate_flpdcache(data, iova);
940 spin_unlock(&domain->lock);
944 return page_entry(sent, iova);
947 static int lv1set_section(struct exynos_iommu_domain *domain,
948 sysmmu_pte_t *sent, sysmmu_iova_t iova,
949 phys_addr_t paddr, short *pgcnt)
951 if (lv1ent_section(sent)) {
952 WARN(1, "Trying mapping on 1MiB@%#08x that is mapped",
957 if (lv1ent_page(sent)) {
958 if (*pgcnt != NUM_LV2ENTRIES) {
959 WARN(1, "Trying mapping on 1MiB@%#08x that is mapped",
964 kmem_cache_free(lv2table_kmem_cache, page_entry(sent, 0));
968 update_pte(sent, mk_lv1ent_sect(paddr));
970 spin_lock(&domain->lock);
971 if (lv1ent_page_zero(sent)) {
972 struct sysmmu_drvdata *data;
974 * Flushing FLPD cache in System MMU v3.3 that may cache a FLPD
975 * entry by speculative prefetch of SLPD which has no mapping.
977 list_for_each_entry(data, &domain->clients, domain_node)
978 sysmmu_tlb_invalidate_flpdcache(data, iova);
980 spin_unlock(&domain->lock);
985 static int lv2set_page(sysmmu_pte_t *pent, phys_addr_t paddr, size_t size,
988 if (size == SPAGE_SIZE) {
989 if (WARN_ON(!lv2ent_fault(pent)))
992 update_pte(pent, mk_lv2ent_spage(paddr));
994 } else { /* size == LPAGE_SIZE */
996 dma_addr_t pent_base = virt_to_phys(pent);
998 dma_sync_single_for_cpu(dma_dev, pent_base,
999 sizeof(*pent) * SPAGES_PER_LPAGE,
1001 for (i = 0; i < SPAGES_PER_LPAGE; i++, pent++) {
1002 if (WARN_ON(!lv2ent_fault(pent))) {
1004 memset(pent - i, 0, sizeof(*pent) * i);
1008 *pent = mk_lv2ent_lpage(paddr);
1010 dma_sync_single_for_device(dma_dev, pent_base,
1011 sizeof(*pent) * SPAGES_PER_LPAGE,
1013 *pgcnt -= SPAGES_PER_LPAGE;
1020 * *CAUTION* to the I/O virtual memory managers that support exynos-iommu:
1022 * System MMU v3.x has advanced logic to improve address translation
1023 * performance with caching more page table entries by a page table walk.
1024 * However, the logic has a bug that while caching faulty page table entries,
1025 * System MMU reports page fault if the cached fault entry is hit even though
1026 * the fault entry is updated to a valid entry after the entry is cached.
1027 * To prevent caching faulty page table entries which may be updated to valid
1028 * entries later, the virtual memory manager should care about the workaround
1029 * for the problem. The following describes the workaround.
1031 * Any two consecutive I/O virtual address regions must have a hole of 128KiB
1032 * at maximum to prevent misbehavior of System MMU 3.x (workaround for h/w bug).
1034 * Precisely, any start address of I/O virtual region must be aligned with
1035 * the following sizes for System MMU v3.1 and v3.2.
1036 * System MMU v3.1: 128KiB
1037 * System MMU v3.2: 256KiB
1039 * Because System MMU v3.3 caches page table entries more aggressively, it needs
1041 * - Any two consecutive I/O virtual regions must have a hole of size larger
1042 * than or equal to 128KiB.
1043 * - Start address of an I/O virtual region must be aligned by 128KiB.
1045 static int exynos_iommu_map(struct iommu_domain *iommu_domain,
1046 unsigned long l_iova, phys_addr_t paddr, size_t size,
1049 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
1050 sysmmu_pte_t *entry;
1051 sysmmu_iova_t iova = (sysmmu_iova_t)l_iova;
1052 unsigned long flags;
1055 BUG_ON(domain->pgtable == NULL);
1057 spin_lock_irqsave(&domain->pgtablelock, flags);
1059 entry = section_entry(domain->pgtable, iova);
1061 if (size == SECT_SIZE) {
1062 ret = lv1set_section(domain, entry, iova, paddr,
1063 &domain->lv2entcnt[lv1ent_offset(iova)]);
1067 pent = alloc_lv2entry(domain, entry, iova,
1068 &domain->lv2entcnt[lv1ent_offset(iova)]);
1071 ret = PTR_ERR(pent);
1073 ret = lv2set_page(pent, paddr, size,
1074 &domain->lv2entcnt[lv1ent_offset(iova)]);
1078 pr_err("%s: Failed(%d) to map %#zx bytes @ %#x\n",
1079 __func__, ret, size, iova);
1081 spin_unlock_irqrestore(&domain->pgtablelock, flags);
1086 static void exynos_iommu_tlb_invalidate_entry(struct exynos_iommu_domain *domain,
1087 sysmmu_iova_t iova, size_t size)
1089 struct sysmmu_drvdata *data;
1090 unsigned long flags;
1092 spin_lock_irqsave(&domain->lock, flags);
1094 list_for_each_entry(data, &domain->clients, domain_node)
1095 sysmmu_tlb_invalidate_entry(data, iova, size);
1097 spin_unlock_irqrestore(&domain->lock, flags);
1100 static size_t exynos_iommu_unmap(struct iommu_domain *iommu_domain,
1101 unsigned long l_iova, size_t size)
1103 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
1104 sysmmu_iova_t iova = (sysmmu_iova_t)l_iova;
1107 unsigned long flags;
1109 BUG_ON(domain->pgtable == NULL);
1111 spin_lock_irqsave(&domain->pgtablelock, flags);
1113 ent = section_entry(domain->pgtable, iova);
1115 if (lv1ent_section(ent)) {
1116 if (WARN_ON(size < SECT_SIZE)) {
1117 err_pgsize = SECT_SIZE;
1121 /* workaround for h/w bug in System MMU v3.3 */
1122 update_pte(ent, ZERO_LV2LINK);
1127 if (unlikely(lv1ent_fault(ent))) {
1128 if (size > SECT_SIZE)
1133 /* lv1ent_page(sent) == true here */
1135 ent = page_entry(ent, iova);
1137 if (unlikely(lv2ent_fault(ent))) {
1142 if (lv2ent_small(ent)) {
1145 domain->lv2entcnt[lv1ent_offset(iova)] += 1;
1149 /* lv1ent_large(ent) == true here */
1150 if (WARN_ON(size < LPAGE_SIZE)) {
1151 err_pgsize = LPAGE_SIZE;
1155 dma_sync_single_for_cpu(dma_dev, virt_to_phys(ent),
1156 sizeof(*ent) * SPAGES_PER_LPAGE,
1158 memset(ent, 0, sizeof(*ent) * SPAGES_PER_LPAGE);
1159 dma_sync_single_for_device(dma_dev, virt_to_phys(ent),
1160 sizeof(*ent) * SPAGES_PER_LPAGE,
1163 domain->lv2entcnt[lv1ent_offset(iova)] += SPAGES_PER_LPAGE;
1165 spin_unlock_irqrestore(&domain->pgtablelock, flags);
1167 exynos_iommu_tlb_invalidate_entry(domain, iova, size);
1171 spin_unlock_irqrestore(&domain->pgtablelock, flags);
1173 pr_err("%s: Failed: size(%#zx) @ %#x is smaller than page size %#zx\n",
1174 __func__, size, iova, err_pgsize);
1179 static phys_addr_t exynos_iommu_iova_to_phys(struct iommu_domain *iommu_domain,
1182 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
1183 sysmmu_pte_t *entry;
1184 unsigned long flags;
1185 phys_addr_t phys = 0;
1187 spin_lock_irqsave(&domain->pgtablelock, flags);
1189 entry = section_entry(domain->pgtable, iova);
1191 if (lv1ent_section(entry)) {
1192 phys = section_phys(entry) + section_offs(iova);
1193 } else if (lv1ent_page(entry)) {
1194 entry = page_entry(entry, iova);
1196 if (lv2ent_large(entry))
1197 phys = lpage_phys(entry) + lpage_offs(iova);
1198 else if (lv2ent_small(entry))
1199 phys = spage_phys(entry) + spage_offs(iova);
1202 spin_unlock_irqrestore(&domain->pgtablelock, flags);
1207 static struct iommu_group *get_device_iommu_group(struct device *dev)
1209 struct iommu_group *group;
1211 group = iommu_group_get(dev);
1213 group = iommu_group_alloc();
1218 static int exynos_iommu_add_device(struct device *dev)
1220 struct iommu_group *group;
1222 if (!has_sysmmu(dev))
1225 group = iommu_group_get_for_dev(dev);
1228 return PTR_ERR(group);
1230 iommu_group_put(group);
1235 static void exynos_iommu_remove_device(struct device *dev)
1237 if (!has_sysmmu(dev))
1240 iommu_group_remove_device(dev);
1243 static int exynos_iommu_of_xlate(struct device *dev,
1244 struct of_phandle_args *spec)
1246 struct exynos_iommu_owner *owner = dev->archdata.iommu;
1247 struct platform_device *sysmmu = of_find_device_by_node(spec->np);
1248 struct sysmmu_drvdata *data;
1253 data = platform_get_drvdata(sysmmu);
1258 owner = kzalloc(sizeof(*owner), GFP_KERNEL);
1262 INIT_LIST_HEAD(&owner->controllers);
1263 dev->archdata.iommu = owner;
1266 list_add_tail(&data->owner_node, &owner->controllers);
1270 static struct iommu_ops exynos_iommu_ops = {
1271 .domain_alloc = exynos_iommu_domain_alloc,
1272 .domain_free = exynos_iommu_domain_free,
1273 .attach_dev = exynos_iommu_attach_device,
1274 .detach_dev = exynos_iommu_detach_device,
1275 .map = exynos_iommu_map,
1276 .unmap = exynos_iommu_unmap,
1277 .map_sg = default_iommu_map_sg,
1278 .iova_to_phys = exynos_iommu_iova_to_phys,
1279 .device_group = get_device_iommu_group,
1280 .add_device = exynos_iommu_add_device,
1281 .remove_device = exynos_iommu_remove_device,
1282 .pgsize_bitmap = SECT_SIZE | LPAGE_SIZE | SPAGE_SIZE,
1283 .of_xlate = exynos_iommu_of_xlate,
1286 static bool init_done;
1288 static int __init exynos_iommu_init(void)
1292 lv2table_kmem_cache = kmem_cache_create("exynos-iommu-lv2table",
1293 LV2TABLE_SIZE, LV2TABLE_SIZE, 0, NULL);
1294 if (!lv2table_kmem_cache) {
1295 pr_err("%s: Failed to create kmem cache\n", __func__);
1299 ret = platform_driver_register(&exynos_sysmmu_driver);
1301 pr_err("%s: Failed to register driver\n", __func__);
1302 goto err_reg_driver;
1305 zero_lv2_table = kmem_cache_zalloc(lv2table_kmem_cache, GFP_KERNEL);
1306 if (zero_lv2_table == NULL) {
1307 pr_err("%s: Failed to allocate zero level2 page table\n",
1313 ret = bus_set_iommu(&platform_bus_type, &exynos_iommu_ops);
1315 pr_err("%s: Failed to register exynos-iommu driver.\n",
1324 kmem_cache_free(lv2table_kmem_cache, zero_lv2_table);
1326 platform_driver_unregister(&exynos_sysmmu_driver);
1328 kmem_cache_destroy(lv2table_kmem_cache);
1332 static int __init exynos_iommu_of_setup(struct device_node *np)
1334 struct platform_device *pdev;
1337 exynos_iommu_init();
1339 pdev = of_platform_device_create(np, NULL, platform_bus_type.dev_root);
1341 return PTR_ERR(pdev);
1344 * use the first registered sysmmu device for performing
1345 * dma mapping operations on iommu page tables (cpu cache flush)
1348 dma_dev = &pdev->dev;
1350 of_iommu_set_ops(np, &exynos_iommu_ops);
1354 IOMMU_OF_DECLARE(exynos_iommu_of, "samsung,exynos-sysmmu",
1355 exynos_iommu_of_setup);