1 /* linux/drivers/iommu/exynos_iommu.c
3 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
4 * http://www.samsung.com
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #ifdef CONFIG_EXYNOS_IOMMU_DEBUG
15 #include <linux/clk.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/err.h>
19 #include <linux/iommu.h>
20 #include <linux/interrupt.h>
21 #include <linux/list.h>
23 #include <linux/of_iommu.h>
24 #include <linux/of_platform.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/slab.h>
28 #include <linux/dma-iommu.h>
30 typedef u32 sysmmu_iova_t;
31 typedef u32 sysmmu_pte_t;
33 /* We do not consider super section mapping (16MB) */
35 #define LPAGE_ORDER 16
36 #define SPAGE_ORDER 12
38 #define SECT_SIZE (1 << SECT_ORDER)
39 #define LPAGE_SIZE (1 << LPAGE_ORDER)
40 #define SPAGE_SIZE (1 << SPAGE_ORDER)
42 #define SECT_MASK (~(SECT_SIZE - 1))
43 #define LPAGE_MASK (~(LPAGE_SIZE - 1))
44 #define SPAGE_MASK (~(SPAGE_SIZE - 1))
46 #define lv1ent_fault(sent) ((*(sent) == ZERO_LV2LINK) || \
47 ((*(sent) & 3) == 0) || ((*(sent) & 3) == 3))
48 #define lv1ent_zero(sent) (*(sent) == ZERO_LV2LINK)
49 #define lv1ent_page_zero(sent) ((*(sent) & 3) == 1)
50 #define lv1ent_page(sent) ((*(sent) != ZERO_LV2LINK) && \
52 #define lv1ent_section(sent) ((*(sent) & 3) == 2)
54 #define lv2ent_fault(pent) ((*(pent) & 3) == 0)
55 #define lv2ent_small(pent) ((*(pent) & 2) == 2)
56 #define lv2ent_large(pent) ((*(pent) & 3) == 1)
58 static u32 sysmmu_page_offset(sysmmu_iova_t iova, u32 size)
60 return iova & (size - 1);
63 #define section_phys(sent) (*(sent) & SECT_MASK)
64 #define section_offs(iova) sysmmu_page_offset((iova), SECT_SIZE)
65 #define lpage_phys(pent) (*(pent) & LPAGE_MASK)
66 #define lpage_offs(iova) sysmmu_page_offset((iova), LPAGE_SIZE)
67 #define spage_phys(pent) (*(pent) & SPAGE_MASK)
68 #define spage_offs(iova) sysmmu_page_offset((iova), SPAGE_SIZE)
70 #define NUM_LV1ENTRIES 4096
71 #define NUM_LV2ENTRIES (SECT_SIZE / SPAGE_SIZE)
73 static u32 lv1ent_offset(sysmmu_iova_t iova)
75 return iova >> SECT_ORDER;
78 static u32 lv2ent_offset(sysmmu_iova_t iova)
80 return (iova >> SPAGE_ORDER) & (NUM_LV2ENTRIES - 1);
83 #define LV1TABLE_SIZE (NUM_LV1ENTRIES * sizeof(sysmmu_pte_t))
84 #define LV2TABLE_SIZE (NUM_LV2ENTRIES * sizeof(sysmmu_pte_t))
86 #define SPAGES_PER_LPAGE (LPAGE_SIZE / SPAGE_SIZE)
88 #define lv2table_base(sent) (*(sent) & 0xFFFFFC00)
90 #define mk_lv1ent_sect(pa) ((pa) | 2)
91 #define mk_lv1ent_page(pa) ((pa) | 1)
92 #define mk_lv2ent_lpage(pa) ((pa) | 1)
93 #define mk_lv2ent_spage(pa) ((pa) | 2)
95 #define CTRL_ENABLE 0x5
96 #define CTRL_BLOCK 0x7
97 #define CTRL_DISABLE 0x0
100 #define CFG_QOS(n) ((n & 0xF) << 7)
101 #define CFG_MASK 0x0150FFFF /* Selecting bit 0-15, 20, 22 and 24 */
102 #define CFG_ACGEN (1 << 24) /* System MMU 3.3 only */
103 #define CFG_SYSSEL (1 << 22) /* System MMU 3.2 only */
104 #define CFG_FLPDCACHE (1 << 20) /* System MMU 3.2+ only */
106 #define REG_MMU_CTRL 0x000
107 #define REG_MMU_CFG 0x004
108 #define REG_MMU_STATUS 0x008
109 #define REG_MMU_FLUSH 0x00C
110 #define REG_MMU_FLUSH_ENTRY 0x010
111 #define REG_PT_BASE_ADDR 0x014
112 #define REG_INT_STATUS 0x018
113 #define REG_INT_CLEAR 0x01C
115 #define REG_PAGE_FAULT_ADDR 0x024
116 #define REG_AW_FAULT_ADDR 0x028
117 #define REG_AR_FAULT_ADDR 0x02C
118 #define REG_DEFAULT_SLAVE_ADDR 0x030
120 #define REG_MMU_VERSION 0x034
122 #define MMU_MAJ_VER(val) ((val) >> 7)
123 #define MMU_MIN_VER(val) ((val) & 0x7F)
124 #define MMU_RAW_VER(reg) (((reg) >> 21) & ((1 << 11) - 1)) /* 11 bits */
126 #define MAKE_MMU_VER(maj, min) ((((maj) & 0xF) << 7) | ((min) & 0x7F))
128 #define REG_PB0_SADDR 0x04C
129 #define REG_PB0_EADDR 0x050
130 #define REG_PB1_SADDR 0x054
131 #define REG_PB1_EADDR 0x058
133 #define has_sysmmu(dev) (dev->archdata.iommu != NULL)
135 static struct device *dma_dev;
136 static struct kmem_cache *lv2table_kmem_cache;
137 static sysmmu_pte_t *zero_lv2_table;
138 #define ZERO_LV2LINK mk_lv1ent_page(virt_to_phys(zero_lv2_table))
140 static sysmmu_pte_t *section_entry(sysmmu_pte_t *pgtable, sysmmu_iova_t iova)
142 return pgtable + lv1ent_offset(iova);
145 static sysmmu_pte_t *page_entry(sysmmu_pte_t *sent, sysmmu_iova_t iova)
147 return (sysmmu_pte_t *)phys_to_virt(
148 lv2table_base(sent)) + lv2ent_offset(iova);
151 enum exynos_sysmmu_inttype {
159 SYSMMU_AW_PROTECTION, /* 7 */
160 SYSMMU_FAULT_UNKNOWN,
164 static unsigned short fault_reg_offset[SYSMMU_FAULTS_NUM] = {
168 REG_DEFAULT_SLAVE_ADDR,
175 static char *sysmmu_fault_name[SYSMMU_FAULTS_NUM] = {
177 "AR MULTI-HIT FAULT",
178 "AW MULTI-HIT FAULT",
180 "AR SECURITY PROTECTION FAULT",
181 "AR ACCESS PROTECTION FAULT",
182 "AW SECURITY PROTECTION FAULT",
183 "AW ACCESS PROTECTION FAULT",
188 * This structure is attached to dev.archdata.iommu of the master device
189 * on device add, contains a list of SYSMMU controllers defined by device tree,
190 * which are bound to given master device. It is usually referenced by 'owner'
193 struct exynos_iommu_owner {
194 struct list_head controllers; /* list of sysmmu_drvdata.owner_node */
198 * This structure exynos specific generalization of struct iommu_domain.
199 * It contains list of SYSMMU controllers from all master devices, which has
200 * been attached to this domain and page tables of IO address space defined by
201 * it. It is usually referenced by 'domain' pointer.
203 struct exynos_iommu_domain {
204 struct list_head clients; /* list of sysmmu_drvdata.domain_node */
205 sysmmu_pte_t *pgtable; /* lv1 page table, 16KB */
206 short *lv2entcnt; /* free lv2 entry counter for each section */
207 spinlock_t lock; /* lock for modyfying list of clients */
208 spinlock_t pgtablelock; /* lock for modifying page table @ pgtable */
209 struct iommu_domain domain; /* generic domain data structure */
213 * This structure hold all data of a single SYSMMU controller, this includes
214 * hw resources like registers and clocks, pointers and list nodes to connect
215 * it to all other structures, internal state and parameters read from device
216 * tree. It is usually referenced by 'data' pointer.
218 struct sysmmu_drvdata {
219 struct device *sysmmu; /* SYSMMU controller device */
220 struct device *master; /* master device (owner) */
221 void __iomem *sfrbase; /* our registers */
222 struct clk *clk; /* SYSMMU's clock */
223 struct clk *clk_master; /* master's device clock */
224 int activations; /* number of calls to sysmmu_enable */
225 spinlock_t lock; /* lock for modyfying state */
226 struct exynos_iommu_domain *domain; /* domain we belong to */
227 struct list_head domain_node; /* node for domain clients list */
228 struct list_head owner_node; /* node for owner controllers list */
229 phys_addr_t pgtable; /* assigned page table structure */
230 unsigned int version; /* our version */
233 static struct exynos_iommu_domain *to_exynos_domain(struct iommu_domain *dom)
235 return container_of(dom, struct exynos_iommu_domain, domain);
238 static bool set_sysmmu_active(struct sysmmu_drvdata *data)
240 /* return true if the System MMU was not active previously
241 and it needs to be initialized */
242 return ++data->activations == 1;
245 static bool set_sysmmu_inactive(struct sysmmu_drvdata *data)
247 /* return true if the System MMU is needed to be disabled */
248 BUG_ON(data->activations < 1);
249 return --data->activations == 0;
252 static bool is_sysmmu_active(struct sysmmu_drvdata *data)
254 return data->activations > 0;
257 static void sysmmu_unblock(struct sysmmu_drvdata *data)
259 __raw_writel(CTRL_ENABLE, data->sfrbase + REG_MMU_CTRL);
262 static bool sysmmu_block(struct sysmmu_drvdata *data)
266 __raw_writel(CTRL_BLOCK, data->sfrbase + REG_MMU_CTRL);
267 while ((i > 0) && !(__raw_readl(data->sfrbase + REG_MMU_STATUS) & 1))
270 if (!(__raw_readl(data->sfrbase + REG_MMU_STATUS) & 1)) {
271 sysmmu_unblock(data);
278 static void __sysmmu_tlb_invalidate(struct sysmmu_drvdata *data)
280 __raw_writel(0x1, data->sfrbase + REG_MMU_FLUSH);
283 static void __sysmmu_tlb_invalidate_entry(struct sysmmu_drvdata *data,
284 sysmmu_iova_t iova, unsigned int num_inv)
288 for (i = 0; i < num_inv; i++) {
289 __raw_writel((iova & SPAGE_MASK) | 1,
290 data->sfrbase + REG_MMU_FLUSH_ENTRY);
295 static void __sysmmu_set_ptbase(struct sysmmu_drvdata *data, phys_addr_t pgd)
297 __raw_writel(pgd, data->sfrbase + REG_PT_BASE_ADDR);
299 __sysmmu_tlb_invalidate(data);
302 static void show_fault_information(const char *name,
303 enum exynos_sysmmu_inttype itype,
304 phys_addr_t pgtable_base, sysmmu_iova_t fault_addr)
308 if ((itype >= SYSMMU_FAULTS_NUM) || (itype < SYSMMU_PAGEFAULT))
309 itype = SYSMMU_FAULT_UNKNOWN;
311 pr_err("%s occurred at %#x by %s(Page table base: %pa)\n",
312 sysmmu_fault_name[itype], fault_addr, name, &pgtable_base);
314 ent = section_entry(phys_to_virt(pgtable_base), fault_addr);
315 pr_err("\tLv1 entry: %#x\n", *ent);
317 if (lv1ent_page(ent)) {
318 ent = page_entry(ent, fault_addr);
319 pr_err("\t Lv2 entry: %#x\n", *ent);
323 static irqreturn_t exynos_sysmmu_irq(int irq, void *dev_id)
325 /* SYSMMU is in blocked state when interrupt occurred. */
326 struct sysmmu_drvdata *data = dev_id;
327 enum exynos_sysmmu_inttype itype;
328 sysmmu_iova_t addr = -1;
331 WARN_ON(!is_sysmmu_active(data));
333 spin_lock(&data->lock);
335 clk_enable(data->clk_master);
337 itype = (enum exynos_sysmmu_inttype)
338 __ffs(__raw_readl(data->sfrbase + REG_INT_STATUS));
339 if (WARN_ON(!((itype >= 0) && (itype < SYSMMU_FAULT_UNKNOWN))))
340 itype = SYSMMU_FAULT_UNKNOWN;
342 addr = __raw_readl(data->sfrbase + fault_reg_offset[itype]);
344 if (itype == SYSMMU_FAULT_UNKNOWN) {
345 pr_err("%s: Fault is not occurred by System MMU '%s'!\n",
346 __func__, dev_name(data->sysmmu));
347 pr_err("%s: Please check if IRQ is correctly configured.\n",
352 __raw_readl(data->sfrbase + REG_PT_BASE_ADDR);
353 show_fault_information(dev_name(data->sysmmu),
356 ret = report_iommu_fault(&data->domain->domain,
357 data->master, addr, itype);
360 /* fault is not recovered by fault handler */
363 __raw_writel(1 << itype, data->sfrbase + REG_INT_CLEAR);
365 sysmmu_unblock(data);
367 clk_disable(data->clk_master);
369 spin_unlock(&data->lock);
374 static void __sysmmu_disable_nocount(struct sysmmu_drvdata *data)
376 clk_enable(data->clk_master);
378 __raw_writel(CTRL_DISABLE, data->sfrbase + REG_MMU_CTRL);
379 __raw_writel(0, data->sfrbase + REG_MMU_CFG);
381 clk_disable(data->clk);
382 clk_disable(data->clk_master);
385 static bool __sysmmu_disable(struct sysmmu_drvdata *data)
390 spin_lock_irqsave(&data->lock, flags);
392 disabled = set_sysmmu_inactive(data);
398 __sysmmu_disable_nocount(data);
400 dev_dbg(data->sysmmu, "Disabled\n");
402 dev_dbg(data->sysmmu, "%d times left to disable\n",
406 spin_unlock_irqrestore(&data->lock, flags);
411 static void __sysmmu_init_config(struct sysmmu_drvdata *data)
413 unsigned int cfg = CFG_LRU | CFG_QOS(15);
416 ver = MMU_RAW_VER(__raw_readl(data->sfrbase + REG_MMU_VERSION));
417 if (MMU_MAJ_VER(ver) == 3) {
418 if (MMU_MIN_VER(ver) >= 2) {
419 cfg |= CFG_FLPDCACHE;
420 if (MMU_MIN_VER(ver) == 3) {
429 __raw_writel(cfg, data->sfrbase + REG_MMU_CFG);
433 static void __sysmmu_enable_nocount(struct sysmmu_drvdata *data)
435 clk_enable(data->clk_master);
436 clk_enable(data->clk);
438 __raw_writel(CTRL_BLOCK, data->sfrbase + REG_MMU_CTRL);
440 __sysmmu_init_config(data);
442 __sysmmu_set_ptbase(data, data->pgtable);
444 __raw_writel(CTRL_ENABLE, data->sfrbase + REG_MMU_CTRL);
446 clk_disable(data->clk_master);
449 static int __sysmmu_enable(struct sysmmu_drvdata *data, phys_addr_t pgtable,
450 struct exynos_iommu_domain *domain)
455 spin_lock_irqsave(&data->lock, flags);
456 if (set_sysmmu_active(data)) {
457 data->pgtable = pgtable;
458 data->domain = domain;
460 __sysmmu_enable_nocount(data);
462 dev_dbg(data->sysmmu, "Enabled\n");
464 ret = (pgtable == data->pgtable) ? 1 : -EBUSY;
466 dev_dbg(data->sysmmu, "already enabled\n");
469 if (WARN_ON(ret < 0))
470 set_sysmmu_inactive(data); /* decrement count */
472 spin_unlock_irqrestore(&data->lock, flags);
477 static void __sysmmu_tlb_invalidate_flpdcache(struct sysmmu_drvdata *data,
480 if (data->version == MAKE_MMU_VER(3, 3))
481 __raw_writel(iova | 0x1, data->sfrbase + REG_MMU_FLUSH_ENTRY);
484 static void sysmmu_tlb_invalidate_flpdcache(struct sysmmu_drvdata *data,
489 clk_enable(data->clk_master);
491 spin_lock_irqsave(&data->lock, flags);
492 if (is_sysmmu_active(data))
493 __sysmmu_tlb_invalidate_flpdcache(data, iova);
494 spin_unlock_irqrestore(&data->lock, flags);
496 clk_disable(data->clk_master);
499 static void sysmmu_tlb_invalidate_entry(struct sysmmu_drvdata *data,
500 sysmmu_iova_t iova, size_t size)
504 spin_lock_irqsave(&data->lock, flags);
505 if (is_sysmmu_active(data)) {
506 unsigned int num_inv = 1;
508 clk_enable(data->clk_master);
511 * L2TLB invalidation required
512 * 4KB page: 1 invalidation
513 * 64KB page: 16 invalidations
514 * 1MB page: 64 invalidations
515 * because it is set-associative TLB
516 * with 8-way and 64 sets.
517 * 1MB page can be cached in one of all sets.
518 * 64KB page can be one of 16 consecutive sets.
520 if (MMU_MAJ_VER(data->version) == 2)
521 num_inv = min_t(unsigned int, size / PAGE_SIZE, 64);
523 if (sysmmu_block(data)) {
524 __sysmmu_tlb_invalidate_entry(data, iova, num_inv);
525 sysmmu_unblock(data);
527 clk_disable(data->clk_master);
529 dev_dbg(data->master,
530 "disabled. Skipping TLB invalidation @ %#x\n", iova);
532 spin_unlock_irqrestore(&data->lock, flags);
535 static int __init exynos_sysmmu_probe(struct platform_device *pdev)
538 struct device *dev = &pdev->dev;
539 struct sysmmu_drvdata *data;
540 struct resource *res;
542 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
546 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
547 data->sfrbase = devm_ioremap_resource(dev, res);
548 if (IS_ERR(data->sfrbase))
549 return PTR_ERR(data->sfrbase);
551 irq = platform_get_irq(pdev, 0);
553 dev_err(dev, "Unable to find IRQ resource\n");
557 ret = devm_request_irq(dev, irq, exynos_sysmmu_irq, 0,
558 dev_name(dev), data);
560 dev_err(dev, "Unabled to register handler of irq %d\n", irq);
564 data->clk = devm_clk_get(dev, "sysmmu");
565 if (IS_ERR(data->clk)) {
566 dev_err(dev, "Failed to get clock!\n");
567 return PTR_ERR(data->clk);
569 ret = clk_prepare(data->clk);
571 dev_err(dev, "Failed to prepare clk\n");
576 data->clk_master = devm_clk_get(dev, "master");
577 if (!IS_ERR(data->clk_master)) {
578 ret = clk_prepare(data->clk_master);
580 clk_unprepare(data->clk);
581 dev_err(dev, "Failed to prepare master's clk\n");
585 data->clk_master = NULL;
589 spin_lock_init(&data->lock);
591 platform_set_drvdata(pdev, data);
593 pm_runtime_enable(dev);
598 #ifdef CONFIG_PM_SLEEP
599 static int exynos_sysmmu_suspend(struct device *dev)
601 struct sysmmu_drvdata *data = dev_get_drvdata(dev);
603 dev_dbg(dev, "suspend\n");
604 if (is_sysmmu_active(data)) {
605 __sysmmu_disable_nocount(data);
611 static int exynos_sysmmu_resume(struct device *dev)
613 struct sysmmu_drvdata *data = dev_get_drvdata(dev);
615 dev_dbg(dev, "resume\n");
616 if (is_sysmmu_active(data)) {
617 pm_runtime_get_sync(dev);
618 __sysmmu_enable_nocount(data);
624 static const struct dev_pm_ops sysmmu_pm_ops = {
625 SET_LATE_SYSTEM_SLEEP_PM_OPS(exynos_sysmmu_suspend, exynos_sysmmu_resume)
628 static const struct of_device_id sysmmu_of_match[] __initconst = {
629 { .compatible = "samsung,exynos-sysmmu", },
633 static struct platform_driver exynos_sysmmu_driver __refdata = {
634 .probe = exynos_sysmmu_probe,
636 .name = "exynos-sysmmu",
637 .of_match_table = sysmmu_of_match,
638 .pm = &sysmmu_pm_ops,
642 static inline void update_pte(sysmmu_pte_t *ent, sysmmu_pte_t val)
644 dma_sync_single_for_cpu(dma_dev, virt_to_phys(ent), sizeof(*ent),
647 dma_sync_single_for_device(dma_dev, virt_to_phys(ent), sizeof(*ent),
651 static struct iommu_domain *exynos_iommu_domain_alloc(unsigned type)
653 struct exynos_iommu_domain *domain;
658 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
662 if (type == IOMMU_DOMAIN_DMA) {
663 if (iommu_get_dma_cookie(&domain->domain) != 0)
665 } else if (type != IOMMU_DOMAIN_UNMANAGED) {
669 domain->pgtable = (sysmmu_pte_t *)__get_free_pages(GFP_KERNEL, 2);
670 if (!domain->pgtable)
673 domain->lv2entcnt = (short *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1);
674 if (!domain->lv2entcnt)
677 /* Workaround for System MMU v3.3 to prevent caching 1MiB mapping */
678 for (i = 0; i < NUM_LV1ENTRIES; i += 8) {
679 domain->pgtable[i + 0] = ZERO_LV2LINK;
680 domain->pgtable[i + 1] = ZERO_LV2LINK;
681 domain->pgtable[i + 2] = ZERO_LV2LINK;
682 domain->pgtable[i + 3] = ZERO_LV2LINK;
683 domain->pgtable[i + 4] = ZERO_LV2LINK;
684 domain->pgtable[i + 5] = ZERO_LV2LINK;
685 domain->pgtable[i + 6] = ZERO_LV2LINK;
686 domain->pgtable[i + 7] = ZERO_LV2LINK;
689 handle = dma_map_single(dma_dev, domain->pgtable, LV1TABLE_SIZE,
691 /* For mapping page table entries we rely on dma == phys */
692 BUG_ON(handle != virt_to_phys(domain->pgtable));
694 spin_lock_init(&domain->lock);
695 spin_lock_init(&domain->pgtablelock);
696 INIT_LIST_HEAD(&domain->clients);
698 domain->domain.geometry.aperture_start = 0;
699 domain->domain.geometry.aperture_end = ~0UL;
700 domain->domain.geometry.force_aperture = true;
702 return &domain->domain;
705 free_pages((unsigned long)domain->pgtable, 2);
707 if (type == IOMMU_DOMAIN_DMA)
708 iommu_put_dma_cookie(&domain->domain);
714 static void exynos_iommu_domain_free(struct iommu_domain *iommu_domain)
716 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
717 struct sysmmu_drvdata *data, *next;
721 WARN_ON(!list_empty(&domain->clients));
723 spin_lock_irqsave(&domain->lock, flags);
725 list_for_each_entry_safe(data, next, &domain->clients, domain_node) {
726 if (__sysmmu_disable(data))
728 list_del_init(&data->domain_node);
731 spin_unlock_irqrestore(&domain->lock, flags);
733 if (iommu_domain->type == IOMMU_DOMAIN_DMA)
734 iommu_put_dma_cookie(iommu_domain);
736 dma_unmap_single(dma_dev, virt_to_phys(domain->pgtable), LV1TABLE_SIZE,
739 for (i = 0; i < NUM_LV1ENTRIES; i++)
740 if (lv1ent_page(domain->pgtable + i)) {
741 phys_addr_t base = lv2table_base(domain->pgtable + i);
743 dma_unmap_single(dma_dev, base, LV2TABLE_SIZE,
745 kmem_cache_free(lv2table_kmem_cache,
749 free_pages((unsigned long)domain->pgtable, 2);
750 free_pages((unsigned long)domain->lv2entcnt, 1);
754 static int exynos_iommu_attach_device(struct iommu_domain *iommu_domain,
757 struct exynos_iommu_owner *owner = dev->archdata.iommu;
758 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
759 struct sysmmu_drvdata *data;
760 phys_addr_t pagetable = virt_to_phys(domain->pgtable);
764 if (!has_sysmmu(dev))
767 list_for_each_entry(data, &owner->controllers, owner_node) {
768 pm_runtime_get_sync(data->sysmmu);
769 ret = __sysmmu_enable(data, pagetable, domain);
773 spin_lock_irqsave(&domain->lock, flags);
774 list_add_tail(&data->domain_node, &domain->clients);
775 spin_unlock_irqrestore(&domain->lock, flags);
780 dev_err(dev, "%s: Failed to attach IOMMU with pgtable %pa\n",
781 __func__, &pagetable);
785 dev_dbg(dev, "%s: Attached IOMMU with pgtable %pa %s\n",
786 __func__, &pagetable, (ret == 0) ? "" : ", again");
791 static void exynos_iommu_detach_device(struct iommu_domain *iommu_domain,
794 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
795 phys_addr_t pagetable = virt_to_phys(domain->pgtable);
796 struct sysmmu_drvdata *data, *next;
800 if (!has_sysmmu(dev))
803 spin_lock_irqsave(&domain->lock, flags);
804 list_for_each_entry_safe(data, next, &domain->clients, domain_node) {
805 if (data->master == dev) {
806 if (__sysmmu_disable(data)) {
808 list_del_init(&data->domain_node);
810 pm_runtime_put(data->sysmmu);
814 spin_unlock_irqrestore(&domain->lock, flags);
817 dev_dbg(dev, "%s: Detached IOMMU with pgtable %pa\n",
818 __func__, &pagetable);
820 dev_err(dev, "%s: No IOMMU is attached\n", __func__);
823 static sysmmu_pte_t *alloc_lv2entry(struct exynos_iommu_domain *domain,
824 sysmmu_pte_t *sent, sysmmu_iova_t iova, short *pgcounter)
826 if (lv1ent_section(sent)) {
827 WARN(1, "Trying mapping on %#08x mapped with 1MiB page", iova);
828 return ERR_PTR(-EADDRINUSE);
831 if (lv1ent_fault(sent)) {
833 bool need_flush_flpd_cache = lv1ent_zero(sent);
835 pent = kmem_cache_zalloc(lv2table_kmem_cache, GFP_ATOMIC);
836 BUG_ON((unsigned int)pent & (LV2TABLE_SIZE - 1));
838 return ERR_PTR(-ENOMEM);
840 update_pte(sent, mk_lv1ent_page(virt_to_phys(pent)));
841 kmemleak_ignore(pent);
842 *pgcounter = NUM_LV2ENTRIES;
843 dma_map_single(dma_dev, pent, LV2TABLE_SIZE, DMA_TO_DEVICE);
846 * If pre-fetched SLPD is a faulty SLPD in zero_l2_table,
847 * FLPD cache may cache the address of zero_l2_table. This
848 * function replaces the zero_l2_table with new L2 page table
849 * to write valid mappings.
850 * Accessing the valid area may cause page fault since FLPD
851 * cache may still cache zero_l2_table for the valid area
852 * instead of new L2 page table that has the mapping
853 * information of the valid area.
854 * Thus any replacement of zero_l2_table with other valid L2
855 * page table must involve FLPD cache invalidation for System
857 * FLPD cache invalidation is performed with TLB invalidation
858 * by VPN without blocking. It is safe to invalidate TLB without
859 * blocking because the target address of TLB invalidation is
860 * not currently mapped.
862 if (need_flush_flpd_cache) {
863 struct sysmmu_drvdata *data;
865 spin_lock(&domain->lock);
866 list_for_each_entry(data, &domain->clients, domain_node)
867 sysmmu_tlb_invalidate_flpdcache(data, iova);
868 spin_unlock(&domain->lock);
872 return page_entry(sent, iova);
875 static int lv1set_section(struct exynos_iommu_domain *domain,
876 sysmmu_pte_t *sent, sysmmu_iova_t iova,
877 phys_addr_t paddr, short *pgcnt)
879 if (lv1ent_section(sent)) {
880 WARN(1, "Trying mapping on 1MiB@%#08x that is mapped",
885 if (lv1ent_page(sent)) {
886 if (*pgcnt != NUM_LV2ENTRIES) {
887 WARN(1, "Trying mapping on 1MiB@%#08x that is mapped",
892 kmem_cache_free(lv2table_kmem_cache, page_entry(sent, 0));
896 update_pte(sent, mk_lv1ent_sect(paddr));
898 spin_lock(&domain->lock);
899 if (lv1ent_page_zero(sent)) {
900 struct sysmmu_drvdata *data;
902 * Flushing FLPD cache in System MMU v3.3 that may cache a FLPD
903 * entry by speculative prefetch of SLPD which has no mapping.
905 list_for_each_entry(data, &domain->clients, domain_node)
906 sysmmu_tlb_invalidate_flpdcache(data, iova);
908 spin_unlock(&domain->lock);
913 static int lv2set_page(sysmmu_pte_t *pent, phys_addr_t paddr, size_t size,
916 if (size == SPAGE_SIZE) {
917 if (WARN_ON(!lv2ent_fault(pent)))
920 update_pte(pent, mk_lv2ent_spage(paddr));
922 } else { /* size == LPAGE_SIZE */
924 dma_addr_t pent_base = virt_to_phys(pent);
926 dma_sync_single_for_cpu(dma_dev, pent_base,
927 sizeof(*pent) * SPAGES_PER_LPAGE,
929 for (i = 0; i < SPAGES_PER_LPAGE; i++, pent++) {
930 if (WARN_ON(!lv2ent_fault(pent))) {
932 memset(pent - i, 0, sizeof(*pent) * i);
936 *pent = mk_lv2ent_lpage(paddr);
938 dma_sync_single_for_device(dma_dev, pent_base,
939 sizeof(*pent) * SPAGES_PER_LPAGE,
941 *pgcnt -= SPAGES_PER_LPAGE;
948 * *CAUTION* to the I/O virtual memory managers that support exynos-iommu:
950 * System MMU v3.x has advanced logic to improve address translation
951 * performance with caching more page table entries by a page table walk.
952 * However, the logic has a bug that while caching faulty page table entries,
953 * System MMU reports page fault if the cached fault entry is hit even though
954 * the fault entry is updated to a valid entry after the entry is cached.
955 * To prevent caching faulty page table entries which may be updated to valid
956 * entries later, the virtual memory manager should care about the workaround
957 * for the problem. The following describes the workaround.
959 * Any two consecutive I/O virtual address regions must have a hole of 128KiB
960 * at maximum to prevent misbehavior of System MMU 3.x (workaround for h/w bug).
962 * Precisely, any start address of I/O virtual region must be aligned with
963 * the following sizes for System MMU v3.1 and v3.2.
964 * System MMU v3.1: 128KiB
965 * System MMU v3.2: 256KiB
967 * Because System MMU v3.3 caches page table entries more aggressively, it needs
969 * - Any two consecutive I/O virtual regions must have a hole of size larger
970 * than or equal to 128KiB.
971 * - Start address of an I/O virtual region must be aligned by 128KiB.
973 static int exynos_iommu_map(struct iommu_domain *iommu_domain,
974 unsigned long l_iova, phys_addr_t paddr, size_t size,
977 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
979 sysmmu_iova_t iova = (sysmmu_iova_t)l_iova;
983 BUG_ON(domain->pgtable == NULL);
985 spin_lock_irqsave(&domain->pgtablelock, flags);
987 entry = section_entry(domain->pgtable, iova);
989 if (size == SECT_SIZE) {
990 ret = lv1set_section(domain, entry, iova, paddr,
991 &domain->lv2entcnt[lv1ent_offset(iova)]);
995 pent = alloc_lv2entry(domain, entry, iova,
996 &domain->lv2entcnt[lv1ent_offset(iova)]);
1001 ret = lv2set_page(pent, paddr, size,
1002 &domain->lv2entcnt[lv1ent_offset(iova)]);
1006 pr_err("%s: Failed(%d) to map %#zx bytes @ %#x\n",
1007 __func__, ret, size, iova);
1009 spin_unlock_irqrestore(&domain->pgtablelock, flags);
1014 static void exynos_iommu_tlb_invalidate_entry(struct exynos_iommu_domain *domain,
1015 sysmmu_iova_t iova, size_t size)
1017 struct sysmmu_drvdata *data;
1018 unsigned long flags;
1020 spin_lock_irqsave(&domain->lock, flags);
1022 list_for_each_entry(data, &domain->clients, domain_node)
1023 sysmmu_tlb_invalidate_entry(data, iova, size);
1025 spin_unlock_irqrestore(&domain->lock, flags);
1028 static size_t exynos_iommu_unmap(struct iommu_domain *iommu_domain,
1029 unsigned long l_iova, size_t size)
1031 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
1032 sysmmu_iova_t iova = (sysmmu_iova_t)l_iova;
1035 unsigned long flags;
1037 BUG_ON(domain->pgtable == NULL);
1039 spin_lock_irqsave(&domain->pgtablelock, flags);
1041 ent = section_entry(domain->pgtable, iova);
1043 if (lv1ent_section(ent)) {
1044 if (WARN_ON(size < SECT_SIZE)) {
1045 err_pgsize = SECT_SIZE;
1049 /* workaround for h/w bug in System MMU v3.3 */
1050 update_pte(ent, ZERO_LV2LINK);
1055 if (unlikely(lv1ent_fault(ent))) {
1056 if (size > SECT_SIZE)
1061 /* lv1ent_page(sent) == true here */
1063 ent = page_entry(ent, iova);
1065 if (unlikely(lv2ent_fault(ent))) {
1070 if (lv2ent_small(ent)) {
1073 domain->lv2entcnt[lv1ent_offset(iova)] += 1;
1077 /* lv1ent_large(ent) == true here */
1078 if (WARN_ON(size < LPAGE_SIZE)) {
1079 err_pgsize = LPAGE_SIZE;
1083 dma_sync_single_for_cpu(dma_dev, virt_to_phys(ent),
1084 sizeof(*ent) * SPAGES_PER_LPAGE,
1086 memset(ent, 0, sizeof(*ent) * SPAGES_PER_LPAGE);
1087 dma_sync_single_for_device(dma_dev, virt_to_phys(ent),
1088 sizeof(*ent) * SPAGES_PER_LPAGE,
1091 domain->lv2entcnt[lv1ent_offset(iova)] += SPAGES_PER_LPAGE;
1093 spin_unlock_irqrestore(&domain->pgtablelock, flags);
1095 exynos_iommu_tlb_invalidate_entry(domain, iova, size);
1099 spin_unlock_irqrestore(&domain->pgtablelock, flags);
1101 pr_err("%s: Failed: size(%#zx) @ %#x is smaller than page size %#zx\n",
1102 __func__, size, iova, err_pgsize);
1107 static phys_addr_t exynos_iommu_iova_to_phys(struct iommu_domain *iommu_domain,
1110 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
1111 sysmmu_pte_t *entry;
1112 unsigned long flags;
1113 phys_addr_t phys = 0;
1115 spin_lock_irqsave(&domain->pgtablelock, flags);
1117 entry = section_entry(domain->pgtable, iova);
1119 if (lv1ent_section(entry)) {
1120 phys = section_phys(entry) + section_offs(iova);
1121 } else if (lv1ent_page(entry)) {
1122 entry = page_entry(entry, iova);
1124 if (lv2ent_large(entry))
1125 phys = lpage_phys(entry) + lpage_offs(iova);
1126 else if (lv2ent_small(entry))
1127 phys = spage_phys(entry) + spage_offs(iova);
1130 spin_unlock_irqrestore(&domain->pgtablelock, flags);
1135 static struct iommu_group *get_device_iommu_group(struct device *dev)
1137 struct iommu_group *group;
1139 group = iommu_group_get(dev);
1141 group = iommu_group_alloc();
1146 static int exynos_iommu_add_device(struct device *dev)
1148 struct iommu_group *group;
1150 if (!has_sysmmu(dev))
1153 group = iommu_group_get_for_dev(dev);
1156 return PTR_ERR(group);
1158 iommu_group_put(group);
1163 static void exynos_iommu_remove_device(struct device *dev)
1165 if (!has_sysmmu(dev))
1168 iommu_group_remove_device(dev);
1171 static int exynos_iommu_of_xlate(struct device *dev,
1172 struct of_phandle_args *spec)
1174 struct exynos_iommu_owner *owner = dev->archdata.iommu;
1175 struct platform_device *sysmmu = of_find_device_by_node(spec->np);
1176 struct sysmmu_drvdata *data;
1181 data = platform_get_drvdata(sysmmu);
1186 owner = kzalloc(sizeof(*owner), GFP_KERNEL);
1190 INIT_LIST_HEAD(&owner->controllers);
1191 dev->archdata.iommu = owner;
1194 list_add_tail(&data->owner_node, &owner->controllers);
1198 static struct iommu_ops exynos_iommu_ops = {
1199 .domain_alloc = exynos_iommu_domain_alloc,
1200 .domain_free = exynos_iommu_domain_free,
1201 .attach_dev = exynos_iommu_attach_device,
1202 .detach_dev = exynos_iommu_detach_device,
1203 .map = exynos_iommu_map,
1204 .unmap = exynos_iommu_unmap,
1205 .map_sg = default_iommu_map_sg,
1206 .iova_to_phys = exynos_iommu_iova_to_phys,
1207 .device_group = get_device_iommu_group,
1208 .add_device = exynos_iommu_add_device,
1209 .remove_device = exynos_iommu_remove_device,
1210 .pgsize_bitmap = SECT_SIZE | LPAGE_SIZE | SPAGE_SIZE,
1211 .of_xlate = exynos_iommu_of_xlate,
1214 static bool init_done;
1216 static int __init exynos_iommu_init(void)
1220 lv2table_kmem_cache = kmem_cache_create("exynos-iommu-lv2table",
1221 LV2TABLE_SIZE, LV2TABLE_SIZE, 0, NULL);
1222 if (!lv2table_kmem_cache) {
1223 pr_err("%s: Failed to create kmem cache\n", __func__);
1227 ret = platform_driver_register(&exynos_sysmmu_driver);
1229 pr_err("%s: Failed to register driver\n", __func__);
1230 goto err_reg_driver;
1233 zero_lv2_table = kmem_cache_zalloc(lv2table_kmem_cache, GFP_KERNEL);
1234 if (zero_lv2_table == NULL) {
1235 pr_err("%s: Failed to allocate zero level2 page table\n",
1241 ret = bus_set_iommu(&platform_bus_type, &exynos_iommu_ops);
1243 pr_err("%s: Failed to register exynos-iommu driver.\n",
1252 kmem_cache_free(lv2table_kmem_cache, zero_lv2_table);
1254 platform_driver_unregister(&exynos_sysmmu_driver);
1256 kmem_cache_destroy(lv2table_kmem_cache);
1260 static int __init exynos_iommu_of_setup(struct device_node *np)
1262 struct platform_device *pdev;
1265 exynos_iommu_init();
1267 pdev = of_platform_device_create(np, NULL, platform_bus_type.dev_root);
1269 return PTR_ERR(pdev);
1272 * use the first registered sysmmu device for performing
1273 * dma mapping operations on iommu page tables (cpu cache flush)
1276 dma_dev = &pdev->dev;
1278 of_iommu_set_ops(np, &exynos_iommu_ops);
1282 IOMMU_OF_DECLARE(exynos_iommu_of, "samsung,exynos-sysmmu",
1283 exynos_iommu_of_setup);