blob: 05c6bc099d62524ba94700215d5c93571bf1876b [file] [log] [blame]
Marek Szyprowski740a01e2016-02-18 15:12:58 +01001/*
2 * Copyright (c) 2011,2016 Samsung Electronics Co., Ltd.
KyongHo Cho2a965362012-05-12 05:56:09 +09003 * http://www.samsung.com
4 *
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.
8 */
9
10#ifdef CONFIG_EXYNOS_IOMMU_DEBUG
11#define DEBUG
12#endif
13
KyongHo Cho2a965362012-05-12 05:56:09 +090014#include <linux/clk.h>
Marek Szyprowski8ed55c82015-05-19 15:20:36 +020015#include <linux/dma-mapping.h>
KyongHo Cho2a965362012-05-12 05:56:09 +090016#include <linux/err.h>
Marek Szyprowski312900c2015-05-19 15:20:30 +020017#include <linux/io.h>
KyongHo Cho2a965362012-05-12 05:56:09 +090018#include <linux/iommu.h>
Marek Szyprowski312900c2015-05-19 15:20:30 +020019#include <linux/interrupt.h>
Randy Dunlap514c6032018-04-05 16:25:34 -070020#include <linux/kmemleak.h>
KyongHo Cho2a965362012-05-12 05:56:09 +090021#include <linux/list.h>
Marek Szyprowski8ed55c82015-05-19 15:20:36 +020022#include <linux/of.h>
23#include <linux/of_iommu.h>
24#include <linux/of_platform.h>
Marek Szyprowski312900c2015-05-19 15:20:30 +020025#include <linux/platform_device.h>
26#include <linux/pm_runtime.h>
27#include <linux/slab.h>
Marek Szyprowski58c6f6a2016-02-18 15:12:49 +010028#include <linux/dma-iommu.h>
KyongHo Cho2a965362012-05-12 05:56:09 +090029
Cho KyongHod09d78f2014-05-12 11:44:58 +053030typedef u32 sysmmu_iova_t;
31typedef u32 sysmmu_pte_t;
32
Sachin Kamatf171aba2014-08-04 10:06:28 +053033/* We do not consider super section mapping (16MB) */
KyongHo Cho2a965362012-05-12 05:56:09 +090034#define SECT_ORDER 20
35#define LPAGE_ORDER 16
36#define SPAGE_ORDER 12
37
38#define SECT_SIZE (1 << SECT_ORDER)
39#define LPAGE_SIZE (1 << LPAGE_ORDER)
40#define SPAGE_SIZE (1 << SPAGE_ORDER)
41
42#define SECT_MASK (~(SECT_SIZE - 1))
43#define LPAGE_MASK (~(LPAGE_SIZE - 1))
44#define SPAGE_MASK (~(SPAGE_SIZE - 1))
45
Cho KyongHo66a7ed82014-05-12 11:45:04 +053046#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) && \
51 ((*(sent) & 3) == 1))
KyongHo Cho2a965362012-05-12 05:56:09 +090052#define lv1ent_section(sent) ((*(sent) & 3) == 2)
53
54#define lv2ent_fault(pent) ((*(pent) & 3) == 0)
55#define lv2ent_small(pent) ((*(pent) & 2) == 2)
56#define lv2ent_large(pent) ((*(pent) & 3) == 1)
57
Marek Szyprowski740a01e2016-02-18 15:12:58 +010058/*
59 * v1.x - v3.x SYSMMU supports 32bit physical and 32bit virtual address spaces
60 * v5.0 introduced support for 36bit physical address space by shifting
61 * all page entry values by 4 bits.
62 * All SYSMMU controllers in the system support the address spaces of the same
63 * size, so PG_ENT_SHIFT can be initialized on first SYSMMU probe to proper
64 * value (0 or 4).
65 */
66static short PG_ENT_SHIFT = -1;
67#define SYSMMU_PG_ENT_SHIFT 0
68#define SYSMMU_V5_PG_ENT_SHIFT 4
KyongHo Cho2a965362012-05-12 05:56:09 +090069
Marek Szyprowski1a0d8da2016-11-03 09:04:45 +010070static const sysmmu_pte_t *LV1_PROT;
71static const sysmmu_pte_t SYSMMU_LV1_PROT[] = {
72 ((0 << 15) | (0 << 10)), /* no access */
73 ((1 << 15) | (1 << 10)), /* IOMMU_READ only */
74 ((0 << 15) | (1 << 10)), /* IOMMU_WRITE not supported, use read/write */
75 ((0 << 15) | (1 << 10)), /* IOMMU_READ | IOMMU_WRITE */
76};
77static const sysmmu_pte_t SYSMMU_V5_LV1_PROT[] = {
78 (0 << 4), /* no access */
79 (1 << 4), /* IOMMU_READ only */
80 (2 << 4), /* IOMMU_WRITE only */
81 (3 << 4), /* IOMMU_READ | IOMMU_WRITE */
82};
83
84static const sysmmu_pte_t *LV2_PROT;
85static const sysmmu_pte_t SYSMMU_LV2_PROT[] = {
86 ((0 << 9) | (0 << 4)), /* no access */
87 ((1 << 9) | (1 << 4)), /* IOMMU_READ only */
88 ((0 << 9) | (1 << 4)), /* IOMMU_WRITE not supported, use read/write */
89 ((0 << 9) | (1 << 4)), /* IOMMU_READ | IOMMU_WRITE */
90};
91static const sysmmu_pte_t SYSMMU_V5_LV2_PROT[] = {
92 (0 << 2), /* no access */
93 (1 << 2), /* IOMMU_READ only */
94 (2 << 2), /* IOMMU_WRITE only */
95 (3 << 2), /* IOMMU_READ | IOMMU_WRITE */
96};
97
98#define SYSMMU_SUPPORTED_PROT_BITS (IOMMU_READ | IOMMU_WRITE)
99
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100100#define sect_to_phys(ent) (((phys_addr_t) ent) << PG_ENT_SHIFT)
101#define section_phys(sent) (sect_to_phys(*(sent)) & SECT_MASK)
102#define section_offs(iova) (iova & (SECT_SIZE - 1))
103#define lpage_phys(pent) (sect_to_phys(*(pent)) & LPAGE_MASK)
104#define lpage_offs(iova) (iova & (LPAGE_SIZE - 1))
105#define spage_phys(pent) (sect_to_phys(*(pent)) & SPAGE_MASK)
106#define spage_offs(iova) (iova & (SPAGE_SIZE - 1))
KyongHo Cho2a965362012-05-12 05:56:09 +0900107
108#define NUM_LV1ENTRIES 4096
Cho KyongHod09d78f2014-05-12 11:44:58 +0530109#define NUM_LV2ENTRIES (SECT_SIZE / SPAGE_SIZE)
KyongHo Cho2a965362012-05-12 05:56:09 +0900110
Cho KyongHod09d78f2014-05-12 11:44:58 +0530111static u32 lv1ent_offset(sysmmu_iova_t iova)
112{
113 return iova >> SECT_ORDER;
114}
115
116static u32 lv2ent_offset(sysmmu_iova_t iova)
117{
118 return (iova >> SPAGE_ORDER) & (NUM_LV2ENTRIES - 1);
119}
120
Marek Szyprowski5e3435e2016-02-18 15:12:50 +0100121#define LV1TABLE_SIZE (NUM_LV1ENTRIES * sizeof(sysmmu_pte_t))
Cho KyongHod09d78f2014-05-12 11:44:58 +0530122#define LV2TABLE_SIZE (NUM_LV2ENTRIES * sizeof(sysmmu_pte_t))
KyongHo Cho2a965362012-05-12 05:56:09 +0900123
124#define SPAGES_PER_LPAGE (LPAGE_SIZE / SPAGE_SIZE)
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100125#define lv2table_base(sent) (sect_to_phys(*(sent) & 0xFFFFFFC0))
KyongHo Cho2a965362012-05-12 05:56:09 +0900126
Marek Szyprowski1a0d8da2016-11-03 09:04:45 +0100127#define mk_lv1ent_sect(pa, prot) ((pa >> PG_ENT_SHIFT) | LV1_PROT[prot] | 2)
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100128#define mk_lv1ent_page(pa) ((pa >> PG_ENT_SHIFT) | 1)
Marek Szyprowski1a0d8da2016-11-03 09:04:45 +0100129#define mk_lv2ent_lpage(pa, prot) ((pa >> PG_ENT_SHIFT) | LV2_PROT[prot] | 1)
130#define mk_lv2ent_spage(pa, prot) ((pa >> PG_ENT_SHIFT) | LV2_PROT[prot] | 2)
KyongHo Cho2a965362012-05-12 05:56:09 +0900131
132#define CTRL_ENABLE 0x5
133#define CTRL_BLOCK 0x7
134#define CTRL_DISABLE 0x0
135
Cho KyongHoeeb51842014-05-12 11:45:03 +0530136#define CFG_LRU 0x1
Marek Szyprowski1a0d8da2016-11-03 09:04:45 +0100137#define CFG_EAP (1 << 2)
Cho KyongHoeeb51842014-05-12 11:45:03 +0530138#define CFG_QOS(n) ((n & 0xF) << 7)
Cho KyongHoeeb51842014-05-12 11:45:03 +0530139#define CFG_ACGEN (1 << 24) /* System MMU 3.3 only */
140#define CFG_SYSSEL (1 << 22) /* System MMU 3.2 only */
141#define CFG_FLPDCACHE (1 << 20) /* System MMU 3.2+ only */
142
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100143/* common registers */
KyongHo Cho2a965362012-05-12 05:56:09 +0900144#define REG_MMU_CTRL 0x000
145#define REG_MMU_CFG 0x004
146#define REG_MMU_STATUS 0x008
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100147#define REG_MMU_VERSION 0x034
148
149#define MMU_MAJ_VER(val) ((val) >> 7)
150#define MMU_MIN_VER(val) ((val) & 0x7F)
151#define MMU_RAW_VER(reg) (((reg) >> 21) & ((1 << 11) - 1)) /* 11 bits */
152
153#define MAKE_MMU_VER(maj, min) ((((maj) & 0xF) << 7) | ((min) & 0x7F))
154
155/* v1.x - v3.x registers */
KyongHo Cho2a965362012-05-12 05:56:09 +0900156#define REG_MMU_FLUSH 0x00C
157#define REG_MMU_FLUSH_ENTRY 0x010
158#define REG_PT_BASE_ADDR 0x014
159#define REG_INT_STATUS 0x018
160#define REG_INT_CLEAR 0x01C
161
162#define REG_PAGE_FAULT_ADDR 0x024
163#define REG_AW_FAULT_ADDR 0x028
164#define REG_AR_FAULT_ADDR 0x02C
165#define REG_DEFAULT_SLAVE_ADDR 0x030
166
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100167/* v5.x registers */
168#define REG_V5_PT_BASE_PFN 0x00C
169#define REG_V5_MMU_FLUSH_ALL 0x010
170#define REG_V5_MMU_FLUSH_ENTRY 0x014
Marek Szyprowskid5bf7392017-03-24 10:19:01 +0100171#define REG_V5_MMU_FLUSH_RANGE 0x018
172#define REG_V5_MMU_FLUSH_START 0x020
173#define REG_V5_MMU_FLUSH_END 0x024
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100174#define REG_V5_INT_STATUS 0x060
175#define REG_V5_INT_CLEAR 0x064
176#define REG_V5_FAULT_AR_VA 0x070
177#define REG_V5_FAULT_AW_VA 0x080
KyongHo Cho2a965362012-05-12 05:56:09 +0900178
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530179#define has_sysmmu(dev) (dev->archdata.iommu != NULL)
180
Marek Szyprowski5e3435e2016-02-18 15:12:50 +0100181static struct device *dma_dev;
Cho KyongHo734c3c72014-05-12 11:44:48 +0530182static struct kmem_cache *lv2table_kmem_cache;
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530183static sysmmu_pte_t *zero_lv2_table;
184#define ZERO_LV2LINK mk_lv1ent_page(virt_to_phys(zero_lv2_table))
Cho KyongHo734c3c72014-05-12 11:44:48 +0530185
Cho KyongHod09d78f2014-05-12 11:44:58 +0530186static sysmmu_pte_t *section_entry(sysmmu_pte_t *pgtable, sysmmu_iova_t iova)
KyongHo Cho2a965362012-05-12 05:56:09 +0900187{
188 return pgtable + lv1ent_offset(iova);
189}
190
Cho KyongHod09d78f2014-05-12 11:44:58 +0530191static sysmmu_pte_t *page_entry(sysmmu_pte_t *sent, sysmmu_iova_t iova)
KyongHo Cho2a965362012-05-12 05:56:09 +0900192{
Cho KyongHod09d78f2014-05-12 11:44:58 +0530193 return (sysmmu_pte_t *)phys_to_virt(
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530194 lv2table_base(sent)) + lv2ent_offset(iova);
KyongHo Cho2a965362012-05-12 05:56:09 +0900195}
196
Marek Szyprowskid093fc72016-02-18 15:12:53 +0100197/*
198 * IOMMU fault information register
199 */
200struct sysmmu_fault_info {
201 unsigned int bit; /* bit number in STATUS register */
202 unsigned short addr_reg; /* register to read VA fault address */
203 const char *name; /* human readable fault name */
204 unsigned int type; /* fault type for report_iommu_fault */
KyongHo Cho2a965362012-05-12 05:56:09 +0900205};
206
Marek Szyprowskid093fc72016-02-18 15:12:53 +0100207static const struct sysmmu_fault_info sysmmu_faults[] = {
208 { 0, REG_PAGE_FAULT_ADDR, "PAGE", IOMMU_FAULT_READ },
209 { 1, REG_AR_FAULT_ADDR, "AR MULTI-HIT", IOMMU_FAULT_READ },
210 { 2, REG_AW_FAULT_ADDR, "AW MULTI-HIT", IOMMU_FAULT_WRITE },
211 { 3, REG_DEFAULT_SLAVE_ADDR, "BUS ERROR", IOMMU_FAULT_READ },
212 { 4, REG_AR_FAULT_ADDR, "AR SECURITY PROTECTION", IOMMU_FAULT_READ },
213 { 5, REG_AR_FAULT_ADDR, "AR ACCESS PROTECTION", IOMMU_FAULT_READ },
214 { 6, REG_AW_FAULT_ADDR, "AW SECURITY PROTECTION", IOMMU_FAULT_WRITE },
215 { 7, REG_AW_FAULT_ADDR, "AW ACCESS PROTECTION", IOMMU_FAULT_WRITE },
KyongHo Cho2a965362012-05-12 05:56:09 +0900216};
217
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100218static const struct sysmmu_fault_info sysmmu_v5_faults[] = {
219 { 0, REG_V5_FAULT_AR_VA, "AR PTW", IOMMU_FAULT_READ },
220 { 1, REG_V5_FAULT_AR_VA, "AR PAGE", IOMMU_FAULT_READ },
221 { 2, REG_V5_FAULT_AR_VA, "AR MULTI-HIT", IOMMU_FAULT_READ },
222 { 3, REG_V5_FAULT_AR_VA, "AR ACCESS PROTECTION", IOMMU_FAULT_READ },
223 { 4, REG_V5_FAULT_AR_VA, "AR SECURITY PROTECTION", IOMMU_FAULT_READ },
224 { 16, REG_V5_FAULT_AW_VA, "AW PTW", IOMMU_FAULT_WRITE },
225 { 17, REG_V5_FAULT_AW_VA, "AW PAGE", IOMMU_FAULT_WRITE },
226 { 18, REG_V5_FAULT_AW_VA, "AW MULTI-HIT", IOMMU_FAULT_WRITE },
227 { 19, REG_V5_FAULT_AW_VA, "AW ACCESS PROTECTION", IOMMU_FAULT_WRITE },
228 { 20, REG_V5_FAULT_AW_VA, "AW SECURITY PROTECTION", IOMMU_FAULT_WRITE },
229};
230
Marek Szyprowski2860af32015-05-19 15:20:31 +0200231/*
232 * This structure is attached to dev.archdata.iommu of the master device
233 * on device add, contains a list of SYSMMU controllers defined by device tree,
234 * which are bound to given master device. It is usually referenced by 'owner'
235 * pointer.
236*/
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530237struct exynos_iommu_owner {
Marek Szyprowski1b092052015-05-19 15:20:33 +0200238 struct list_head controllers; /* list of sysmmu_drvdata.owner_node */
Marek Szyprowski5fa61cb2016-02-18 15:13:00 +0100239 struct iommu_domain *domain; /* domain this device is attached */
Marek Szyprowski9b265532016-11-14 11:08:11 +0100240 struct mutex rpm_lock; /* for runtime pm of all sysmmus */
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530241};
242
Marek Szyprowski2860af32015-05-19 15:20:31 +0200243/*
244 * This structure exynos specific generalization of struct iommu_domain.
245 * It contains list of SYSMMU controllers from all master devices, which has
246 * been attached to this domain and page tables of IO address space defined by
247 * it. It is usually referenced by 'domain' pointer.
248 */
KyongHo Cho2a965362012-05-12 05:56:09 +0900249struct exynos_iommu_domain {
Marek Szyprowski2860af32015-05-19 15:20:31 +0200250 struct list_head clients; /* list of sysmmu_drvdata.domain_node */
251 sysmmu_pte_t *pgtable; /* lv1 page table, 16KB */
252 short *lv2entcnt; /* free lv2 entry counter for each section */
253 spinlock_t lock; /* lock for modyfying list of clients */
254 spinlock_t pgtablelock; /* lock for modifying page table @ pgtable */
Joerg Roedele1fd1ea2015-03-26 13:43:11 +0100255 struct iommu_domain domain; /* generic domain data structure */
KyongHo Cho2a965362012-05-12 05:56:09 +0900256};
257
Marek Szyprowski2860af32015-05-19 15:20:31 +0200258/*
259 * This structure hold all data of a single SYSMMU controller, this includes
260 * hw resources like registers and clocks, pointers and list nodes to connect
261 * it to all other structures, internal state and parameters read from device
262 * tree. It is usually referenced by 'data' pointer.
263 */
KyongHo Cho2a965362012-05-12 05:56:09 +0900264struct sysmmu_drvdata {
Marek Szyprowski2860af32015-05-19 15:20:31 +0200265 struct device *sysmmu; /* SYSMMU controller device */
266 struct device *master; /* master device (owner) */
Marek Szyprowski7a974b22017-09-15 13:05:08 +0200267 struct device_link *link; /* runtime PM link to master */
Marek Szyprowski2860af32015-05-19 15:20:31 +0200268 void __iomem *sfrbase; /* our registers */
269 struct clk *clk; /* SYSMMU's clock */
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100270 struct clk *aclk; /* SYSMMU's aclk clock */
271 struct clk *pclk; /* SYSMMU's pclk clock */
Marek Szyprowski2860af32015-05-19 15:20:31 +0200272 struct clk *clk_master; /* master's device clock */
Marek Szyprowski2860af32015-05-19 15:20:31 +0200273 spinlock_t lock; /* lock for modyfying state */
Marek Szyprowski47a574f2016-11-14 11:08:08 +0100274 bool active; /* current status */
Marek Szyprowski2860af32015-05-19 15:20:31 +0200275 struct exynos_iommu_domain *domain; /* domain we belong to */
276 struct list_head domain_node; /* node for domain clients list */
Marek Szyprowski1b092052015-05-19 15:20:33 +0200277 struct list_head owner_node; /* node for owner controllers list */
Marek Szyprowski2860af32015-05-19 15:20:31 +0200278 phys_addr_t pgtable; /* assigned page table structure */
279 unsigned int version; /* our version */
Joerg Roedeld2c302b2017-02-03 13:23:42 +0100280
281 struct iommu_device iommu; /* IOMMU core handle */
KyongHo Cho2a965362012-05-12 05:56:09 +0900282};
283
Joerg Roedele1fd1ea2015-03-26 13:43:11 +0100284static struct exynos_iommu_domain *to_exynos_domain(struct iommu_domain *dom)
285{
286 return container_of(dom, struct exynos_iommu_domain, domain);
287}
288
Marek Szyprowski02cdc362016-02-18 15:12:52 +0100289static void sysmmu_unblock(struct sysmmu_drvdata *data)
KyongHo Cho2a965362012-05-12 05:56:09 +0900290{
Marek Szyprowski84bd0422016-02-29 13:42:57 +0100291 writel(CTRL_ENABLE, data->sfrbase + REG_MMU_CTRL);
KyongHo Cho2a965362012-05-12 05:56:09 +0900292}
293
Marek Szyprowski02cdc362016-02-18 15:12:52 +0100294static bool sysmmu_block(struct sysmmu_drvdata *data)
KyongHo Cho2a965362012-05-12 05:56:09 +0900295{
296 int i = 120;
297
Marek Szyprowski84bd0422016-02-29 13:42:57 +0100298 writel(CTRL_BLOCK, data->sfrbase + REG_MMU_CTRL);
299 while ((i > 0) && !(readl(data->sfrbase + REG_MMU_STATUS) & 1))
KyongHo Cho2a965362012-05-12 05:56:09 +0900300 --i;
301
Marek Szyprowski84bd0422016-02-29 13:42:57 +0100302 if (!(readl(data->sfrbase + REG_MMU_STATUS) & 1)) {
Marek Szyprowski02cdc362016-02-18 15:12:52 +0100303 sysmmu_unblock(data);
KyongHo Cho2a965362012-05-12 05:56:09 +0900304 return false;
305 }
306
307 return true;
308}
309
Marek Szyprowski02cdc362016-02-18 15:12:52 +0100310static void __sysmmu_tlb_invalidate(struct sysmmu_drvdata *data)
KyongHo Cho2a965362012-05-12 05:56:09 +0900311{
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100312 if (MMU_MAJ_VER(data->version) < 5)
Marek Szyprowski84bd0422016-02-29 13:42:57 +0100313 writel(0x1, data->sfrbase + REG_MMU_FLUSH);
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100314 else
Marek Szyprowski84bd0422016-02-29 13:42:57 +0100315 writel(0x1, data->sfrbase + REG_V5_MMU_FLUSH_ALL);
KyongHo Cho2a965362012-05-12 05:56:09 +0900316}
317
Marek Szyprowski02cdc362016-02-18 15:12:52 +0100318static void __sysmmu_tlb_invalidate_entry(struct sysmmu_drvdata *data,
Cho KyongHod09d78f2014-05-12 11:44:58 +0530319 sysmmu_iova_t iova, unsigned int num_inv)
KyongHo Cho2a965362012-05-12 05:56:09 +0900320{
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530321 unsigned int i;
Sachin Kamat365409d2014-05-22 09:50:56 +0530322
Marek Szyprowskid5bf7392017-03-24 10:19:01 +0100323 if (MMU_MAJ_VER(data->version) < 5) {
324 for (i = 0; i < num_inv; i++) {
Marek Szyprowski84bd0422016-02-29 13:42:57 +0100325 writel((iova & SPAGE_MASK) | 1,
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100326 data->sfrbase + REG_MMU_FLUSH_ENTRY);
Marek Szyprowskid5bf7392017-03-24 10:19:01 +0100327 iova += SPAGE_SIZE;
328 }
329 } else {
330 if (num_inv == 1) {
Marek Szyprowski84bd0422016-02-29 13:42:57 +0100331 writel((iova & SPAGE_MASK) | 1,
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100332 data->sfrbase + REG_V5_MMU_FLUSH_ENTRY);
Marek Szyprowskid5bf7392017-03-24 10:19:01 +0100333 } else {
334 writel((iova & SPAGE_MASK),
335 data->sfrbase + REG_V5_MMU_FLUSH_START);
336 writel((iova & SPAGE_MASK) + (num_inv - 1) * SPAGE_SIZE,
337 data->sfrbase + REG_V5_MMU_FLUSH_END);
338 writel(1, data->sfrbase + REG_V5_MMU_FLUSH_RANGE);
339 }
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530340 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900341}
342
Marek Szyprowski02cdc362016-02-18 15:12:52 +0100343static void __sysmmu_set_ptbase(struct sysmmu_drvdata *data, phys_addr_t pgd)
KyongHo Cho2a965362012-05-12 05:56:09 +0900344{
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100345 if (MMU_MAJ_VER(data->version) < 5)
Marek Szyprowski84bd0422016-02-29 13:42:57 +0100346 writel(pgd, data->sfrbase + REG_PT_BASE_ADDR);
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100347 else
Marek Szyprowski84bd0422016-02-29 13:42:57 +0100348 writel(pgd >> PAGE_SHIFT,
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100349 data->sfrbase + REG_V5_PT_BASE_PFN);
KyongHo Cho2a965362012-05-12 05:56:09 +0900350
Marek Szyprowski02cdc362016-02-18 15:12:52 +0100351 __sysmmu_tlb_invalidate(data);
KyongHo Cho2a965362012-05-12 05:56:09 +0900352}
353
Marek Szyprowskifecc49d2016-05-23 11:30:09 +0200354static void __sysmmu_enable_clocks(struct sysmmu_drvdata *data)
355{
356 BUG_ON(clk_prepare_enable(data->clk_master));
357 BUG_ON(clk_prepare_enable(data->clk));
358 BUG_ON(clk_prepare_enable(data->pclk));
359 BUG_ON(clk_prepare_enable(data->aclk));
360}
361
362static void __sysmmu_disable_clocks(struct sysmmu_drvdata *data)
363{
364 clk_disable_unprepare(data->aclk);
365 clk_disable_unprepare(data->pclk);
366 clk_disable_unprepare(data->clk);
367 clk_disable_unprepare(data->clk_master);
368}
369
Marek Szyprowski850d3132016-02-18 15:12:56 +0100370static void __sysmmu_get_version(struct sysmmu_drvdata *data)
371{
372 u32 ver;
373
Marek Szyprowskifecc49d2016-05-23 11:30:09 +0200374 __sysmmu_enable_clocks(data);
Marek Szyprowski850d3132016-02-18 15:12:56 +0100375
Marek Szyprowski84bd0422016-02-29 13:42:57 +0100376 ver = readl(data->sfrbase + REG_MMU_VERSION);
Marek Szyprowski850d3132016-02-18 15:12:56 +0100377
378 /* controllers on some SoCs don't report proper version */
379 if (ver == 0x80000001u)
380 data->version = MAKE_MMU_VER(1, 0);
381 else
382 data->version = MMU_RAW_VER(ver);
383
384 dev_dbg(data->sysmmu, "hardware version: %d.%d\n",
385 MMU_MAJ_VER(data->version), MMU_MIN_VER(data->version));
386
Marek Szyprowskifecc49d2016-05-23 11:30:09 +0200387 __sysmmu_disable_clocks(data);
Marek Szyprowski850d3132016-02-18 15:12:56 +0100388}
389
Marek Szyprowskid093fc72016-02-18 15:12:53 +0100390static void show_fault_information(struct sysmmu_drvdata *data,
391 const struct sysmmu_fault_info *finfo,
392 sysmmu_iova_t fault_addr)
KyongHo Cho2a965362012-05-12 05:56:09 +0900393{
Cho KyongHod09d78f2014-05-12 11:44:58 +0530394 sysmmu_pte_t *ent;
KyongHo Cho2a965362012-05-12 05:56:09 +0900395
Marek Szyprowskiec5d2412017-01-09 13:03:53 +0100396 dev_err(data->sysmmu, "%s: %s FAULT occurred at %#x\n",
397 dev_name(data->master), finfo->name, fault_addr);
398 dev_dbg(data->sysmmu, "Page table base: %pa\n", &data->pgtable);
Marek Szyprowskid093fc72016-02-18 15:12:53 +0100399 ent = section_entry(phys_to_virt(data->pgtable), fault_addr);
Marek Szyprowskiec5d2412017-01-09 13:03:53 +0100400 dev_dbg(data->sysmmu, "\tLv1 entry: %#x\n", *ent);
KyongHo Cho2a965362012-05-12 05:56:09 +0900401 if (lv1ent_page(ent)) {
402 ent = page_entry(ent, fault_addr);
Marek Szyprowskiec5d2412017-01-09 13:03:53 +0100403 dev_dbg(data->sysmmu, "\t Lv2 entry: %#x\n", *ent);
KyongHo Cho2a965362012-05-12 05:56:09 +0900404 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900405}
406
407static irqreturn_t exynos_sysmmu_irq(int irq, void *dev_id)
408{
Sachin Kamatf171aba2014-08-04 10:06:28 +0530409 /* SYSMMU is in blocked state when interrupt occurred. */
KyongHo Cho2a965362012-05-12 05:56:09 +0900410 struct sysmmu_drvdata *data = dev_id;
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100411 const struct sysmmu_fault_info *finfo;
412 unsigned int i, n, itype;
Marek Szyprowskid093fc72016-02-18 15:12:53 +0100413 sysmmu_iova_t fault_addr = -1;
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100414 unsigned short reg_status, reg_clear;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530415 int ret = -ENOSYS;
KyongHo Cho2a965362012-05-12 05:56:09 +0900416
Marek Szyprowski47a574f2016-11-14 11:08:08 +0100417 WARN_ON(!data->active);
KyongHo Cho2a965362012-05-12 05:56:09 +0900418
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100419 if (MMU_MAJ_VER(data->version) < 5) {
420 reg_status = REG_INT_STATUS;
421 reg_clear = REG_INT_CLEAR;
422 finfo = sysmmu_faults;
423 n = ARRAY_SIZE(sysmmu_faults);
424 } else {
425 reg_status = REG_V5_INT_STATUS;
426 reg_clear = REG_V5_INT_CLEAR;
427 finfo = sysmmu_v5_faults;
428 n = ARRAY_SIZE(sysmmu_v5_faults);
429 }
430
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530431 spin_lock(&data->lock);
432
Marek Szyprowskib398af22016-02-18 15:12:51 +0100433 clk_enable(data->clk_master);
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530434
Marek Szyprowski84bd0422016-02-29 13:42:57 +0100435 itype = __ffs(readl(data->sfrbase + reg_status));
Marek Szyprowskid093fc72016-02-18 15:12:53 +0100436 for (i = 0; i < n; i++, finfo++)
437 if (finfo->bit == itype)
438 break;
439 /* unknown/unsupported fault */
440 BUG_ON(i == n);
KyongHo Cho2a965362012-05-12 05:56:09 +0900441
Marek Szyprowskid093fc72016-02-18 15:12:53 +0100442 /* print debug message */
Marek Szyprowski84bd0422016-02-29 13:42:57 +0100443 fault_addr = readl(data->sfrbase + finfo->addr_reg);
Marek Szyprowskid093fc72016-02-18 15:12:53 +0100444 show_fault_information(data, finfo, fault_addr);
KyongHo Cho2a965362012-05-12 05:56:09 +0900445
Marek Szyprowskid093fc72016-02-18 15:12:53 +0100446 if (data->domain)
447 ret = report_iommu_fault(&data->domain->domain,
448 data->master, fault_addr, finfo->type);
Cho KyongHo1fab7fa2014-05-12 11:44:56 +0530449 /* fault is not recovered by fault handler */
450 BUG_ON(ret != 0);
KyongHo Cho2a965362012-05-12 05:56:09 +0900451
Marek Szyprowski84bd0422016-02-29 13:42:57 +0100452 writel(1 << itype, data->sfrbase + reg_clear);
Cho KyongHo1fab7fa2014-05-12 11:44:56 +0530453
Marek Szyprowski02cdc362016-02-18 15:12:52 +0100454 sysmmu_unblock(data);
KyongHo Cho2a965362012-05-12 05:56:09 +0900455
Marek Szyprowskib398af22016-02-18 15:12:51 +0100456 clk_disable(data->clk_master);
Cho KyongHo70605872014-05-12 11:44:55 +0530457
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530458 spin_unlock(&data->lock);
KyongHo Cho2a965362012-05-12 05:56:09 +0900459
460 return IRQ_HANDLED;
461}
462
Marek Szyprowski47a574f2016-11-14 11:08:08 +0100463static void __sysmmu_disable(struct sysmmu_drvdata *data)
KyongHo Cho2a965362012-05-12 05:56:09 +0900464{
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530465 unsigned long flags;
466
Marek Szyprowski47a574f2016-11-14 11:08:08 +0100467 clk_enable(data->clk_master);
468
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530469 spin_lock_irqsave(&data->lock, flags);
Marek Szyprowski47a574f2016-11-14 11:08:08 +0100470 writel(CTRL_DISABLE, data->sfrbase + REG_MMU_CTRL);
471 writel(0, data->sfrbase + REG_MMU_CFG);
472 data->active = false;
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530473 spin_unlock_irqrestore(&data->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900474
Marek Szyprowski47a574f2016-11-14 11:08:08 +0100475 __sysmmu_disable_clocks(data);
KyongHo Cho2a965362012-05-12 05:56:09 +0900476}
477
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530478static void __sysmmu_init_config(struct sysmmu_drvdata *data)
479{
Marek Szyprowski83addec2016-02-18 15:12:54 +0100480 unsigned int cfg;
Cho KyongHoeeb51842014-05-12 11:45:03 +0530481
Marek Szyprowski83addec2016-02-18 15:12:54 +0100482 if (data->version <= MAKE_MMU_VER(3, 1))
483 cfg = CFG_LRU | CFG_QOS(15);
484 else if (data->version <= MAKE_MMU_VER(3, 2))
485 cfg = CFG_LRU | CFG_QOS(15) | CFG_FLPDCACHE | CFG_SYSSEL;
486 else
487 cfg = CFG_QOS(15) | CFG_FLPDCACHE | CFG_ACGEN;
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530488
Marek Szyprowski1a0d8da2016-11-03 09:04:45 +0100489 cfg |= CFG_EAP; /* enable access protection bits check */
490
Marek Szyprowski84bd0422016-02-29 13:42:57 +0100491 writel(cfg, data->sfrbase + REG_MMU_CFG);
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530492}
493
Marek Szyprowski47a574f2016-11-14 11:08:08 +0100494static void __sysmmu_enable(struct sysmmu_drvdata *data)
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530495{
Marek Szyprowski47a574f2016-11-14 11:08:08 +0100496 unsigned long flags;
497
Marek Szyprowskifecc49d2016-05-23 11:30:09 +0200498 __sysmmu_enable_clocks(data);
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530499
Marek Szyprowski47a574f2016-11-14 11:08:08 +0100500 spin_lock_irqsave(&data->lock, flags);
Marek Szyprowski84bd0422016-02-29 13:42:57 +0100501 writel(CTRL_BLOCK, data->sfrbase + REG_MMU_CTRL);
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530502 __sysmmu_init_config(data);
Marek Szyprowski02cdc362016-02-18 15:12:52 +0100503 __sysmmu_set_ptbase(data, data->pgtable);
Marek Szyprowski84bd0422016-02-29 13:42:57 +0100504 writel(CTRL_ENABLE, data->sfrbase + REG_MMU_CTRL);
Marek Szyprowski47a574f2016-11-14 11:08:08 +0100505 data->active = true;
506 spin_unlock_irqrestore(&data->lock, flags);
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530507
Marek Szyprowskifecc49d2016-05-23 11:30:09 +0200508 /*
509 * SYSMMU driver keeps master's clock enabled only for the short
510 * time, while accessing the registers. For performing address
511 * translation during DMA transaction it relies on the client
512 * driver to enable it.
513 */
Marek Szyprowskib398af22016-02-18 15:12:51 +0100514 clk_disable(data->clk_master);
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530515}
516
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200517static void sysmmu_tlb_invalidate_flpdcache(struct sysmmu_drvdata *data,
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530518 sysmmu_iova_t iova)
519{
520 unsigned long flags;
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530521
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530522 spin_lock_irqsave(&data->lock, flags);
Marek Szyprowski47a574f2016-11-14 11:08:08 +0100523 if (data->active && data->version >= MAKE_MMU_VER(3, 3)) {
Marek Szyprowski01324ab2016-05-23 11:30:08 +0200524 clk_enable(data->clk_master);
Marek Szyprowski7d2aa6b2017-03-20 10:17:56 +0100525 if (sysmmu_block(data)) {
Marek Szyprowskicd37a292017-03-20 10:17:57 +0100526 if (data->version >= MAKE_MMU_VER(5, 0))
527 __sysmmu_tlb_invalidate(data);
528 else
529 __sysmmu_tlb_invalidate_entry(data, iova, 1);
Marek Szyprowski7d2aa6b2017-03-20 10:17:56 +0100530 sysmmu_unblock(data);
531 }
Marek Szyprowski01324ab2016-05-23 11:30:08 +0200532 clk_disable(data->clk_master);
Marek Szyprowskid631ea92016-02-18 15:12:55 +0100533 }
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530534 spin_unlock_irqrestore(&data->lock, flags);
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530535}
536
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200537static void sysmmu_tlb_invalidate_entry(struct sysmmu_drvdata *data,
538 sysmmu_iova_t iova, size_t size)
KyongHo Cho2a965362012-05-12 05:56:09 +0900539{
540 unsigned long flags;
KyongHo Cho2a965362012-05-12 05:56:09 +0900541
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530542 spin_lock_irqsave(&data->lock, flags);
Marek Szyprowski47a574f2016-11-14 11:08:08 +0100543 if (data->active) {
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530544 unsigned int num_inv = 1;
Cho KyongHo70605872014-05-12 11:44:55 +0530545
Marek Szyprowskib398af22016-02-18 15:12:51 +0100546 clk_enable(data->clk_master);
Cho KyongHo70605872014-05-12 11:44:55 +0530547
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530548 /*
549 * L2TLB invalidation required
550 * 4KB page: 1 invalidation
Sachin Kamatf171aba2014-08-04 10:06:28 +0530551 * 64KB page: 16 invalidations
552 * 1MB page: 64 invalidations
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530553 * because it is set-associative TLB
554 * with 8-way and 64 sets.
555 * 1MB page can be cached in one of all sets.
556 * 64KB page can be one of 16 consecutive sets.
557 */
Marek Szyprowski512bd0c2015-05-19 15:20:24 +0200558 if (MMU_MAJ_VER(data->version) == 2)
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530559 num_inv = min_t(unsigned int, size / PAGE_SIZE, 64);
560
Marek Szyprowski02cdc362016-02-18 15:12:52 +0100561 if (sysmmu_block(data)) {
562 __sysmmu_tlb_invalidate_entry(data, iova, num_inv);
563 sysmmu_unblock(data);
KyongHo Cho2a965362012-05-12 05:56:09 +0900564 }
Marek Szyprowskib398af22016-02-18 15:12:51 +0100565 clk_disable(data->clk_master);
KyongHo Cho2a965362012-05-12 05:56:09 +0900566 }
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530567 spin_unlock_irqrestore(&data->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900568}
569
Arvind Yadav0b9a3692017-08-28 17:42:05 +0530570static const struct iommu_ops exynos_iommu_ops;
Marek Szyprowski96f66552016-05-23 13:01:27 +0200571
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530572static int __init exynos_sysmmu_probe(struct platform_device *pdev)
KyongHo Cho2a965362012-05-12 05:56:09 +0900573{
Cho KyongHo46c16d12014-05-12 11:44:54 +0530574 int irq, ret;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530575 struct device *dev = &pdev->dev;
KyongHo Cho2a965362012-05-12 05:56:09 +0900576 struct sysmmu_drvdata *data;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530577 struct resource *res;
KyongHo Cho2a965362012-05-12 05:56:09 +0900578
Cho KyongHo46c16d12014-05-12 11:44:54 +0530579 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
580 if (!data)
581 return -ENOMEM;
KyongHo Cho2a965362012-05-12 05:56:09 +0900582
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530583 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Cho KyongHo46c16d12014-05-12 11:44:54 +0530584 data->sfrbase = devm_ioremap_resource(dev, res);
585 if (IS_ERR(data->sfrbase))
586 return PTR_ERR(data->sfrbase);
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530587
Cho KyongHo46c16d12014-05-12 11:44:54 +0530588 irq = platform_get_irq(pdev, 0);
589 if (irq <= 0) {
Cho KyongHo0bf4e542014-05-12 11:45:00 +0530590 dev_err(dev, "Unable to find IRQ resource\n");
Cho KyongHo46c16d12014-05-12 11:44:54 +0530591 return irq;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530592 }
593
Cho KyongHo46c16d12014-05-12 11:44:54 +0530594 ret = devm_request_irq(dev, irq, exynos_sysmmu_irq, 0,
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530595 dev_name(dev), data);
KyongHo Cho2a965362012-05-12 05:56:09 +0900596 if (ret) {
Cho KyongHo46c16d12014-05-12 11:44:54 +0530597 dev_err(dev, "Unabled to register handler of irq %d\n", irq);
598 return ret;
KyongHo Cho2a965362012-05-12 05:56:09 +0900599 }
600
Cho KyongHo46c16d12014-05-12 11:44:54 +0530601 data->clk = devm_clk_get(dev, "sysmmu");
Marek Szyprowski0c2b0632016-05-23 11:30:07 +0200602 if (PTR_ERR(data->clk) == -ENOENT)
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100603 data->clk = NULL;
Marek Szyprowski0c2b0632016-05-23 11:30:07 +0200604 else if (IS_ERR(data->clk))
605 return PTR_ERR(data->clk);
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100606
607 data->aclk = devm_clk_get(dev, "aclk");
Marek Szyprowski0c2b0632016-05-23 11:30:07 +0200608 if (PTR_ERR(data->aclk) == -ENOENT)
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100609 data->aclk = NULL;
Marek Szyprowski0c2b0632016-05-23 11:30:07 +0200610 else if (IS_ERR(data->aclk))
611 return PTR_ERR(data->aclk);
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100612
613 data->pclk = devm_clk_get(dev, "pclk");
Marek Szyprowski0c2b0632016-05-23 11:30:07 +0200614 if (PTR_ERR(data->pclk) == -ENOENT)
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100615 data->pclk = NULL;
Marek Szyprowski0c2b0632016-05-23 11:30:07 +0200616 else if (IS_ERR(data->pclk))
617 return PTR_ERR(data->pclk);
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100618
619 if (!data->clk && (!data->aclk || !data->pclk)) {
620 dev_err(dev, "Failed to get device clock(s)!\n");
621 return -ENOSYS;
KyongHo Cho2a965362012-05-12 05:56:09 +0900622 }
623
Cho KyongHo70605872014-05-12 11:44:55 +0530624 data->clk_master = devm_clk_get(dev, "master");
Marek Szyprowski0c2b0632016-05-23 11:30:07 +0200625 if (PTR_ERR(data->clk_master) == -ENOENT)
Marek Szyprowskib398af22016-02-18 15:12:51 +0100626 data->clk_master = NULL;
Marek Szyprowski0c2b0632016-05-23 11:30:07 +0200627 else if (IS_ERR(data->clk_master))
628 return PTR_ERR(data->clk_master);
Cho KyongHo70605872014-05-12 11:44:55 +0530629
KyongHo Cho2a965362012-05-12 05:56:09 +0900630 data->sysmmu = dev;
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530631 spin_lock_init(&data->lock);
KyongHo Cho2a965362012-05-12 05:56:09 +0900632
Joerg Roedeld2c302b2017-02-03 13:23:42 +0100633 ret = iommu_device_sysfs_add(&data->iommu, &pdev->dev, NULL,
634 dev_name(data->sysmmu));
635 if (ret)
636 return ret;
637
638 iommu_device_set_ops(&data->iommu, &exynos_iommu_ops);
639 iommu_device_set_fwnode(&data->iommu, &dev->of_node->fwnode);
640
641 ret = iommu_device_register(&data->iommu);
642 if (ret)
643 return ret;
644
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530645 platform_set_drvdata(pdev, data);
646
Marek Szyprowski850d3132016-02-18 15:12:56 +0100647 __sysmmu_get_version(data);
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100648 if (PG_ENT_SHIFT < 0) {
Marek Szyprowski1a0d8da2016-11-03 09:04:45 +0100649 if (MMU_MAJ_VER(data->version) < 5) {
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100650 PG_ENT_SHIFT = SYSMMU_PG_ENT_SHIFT;
Marek Szyprowski1a0d8da2016-11-03 09:04:45 +0100651 LV1_PROT = SYSMMU_LV1_PROT;
652 LV2_PROT = SYSMMU_LV2_PROT;
653 } else {
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100654 PG_ENT_SHIFT = SYSMMU_V5_PG_ENT_SHIFT;
Marek Szyprowski1a0d8da2016-11-03 09:04:45 +0100655 LV1_PROT = SYSMMU_V5_LV1_PROT;
656 LV2_PROT = SYSMMU_V5_LV2_PROT;
657 }
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100658 }
659
Marek Szyprowski928055a2017-08-04 12:28:33 +0200660 /*
661 * use the first registered sysmmu device for performing
662 * dma mapping operations on iommu page tables (cpu cache flush)
663 */
664 if (!dma_dev)
665 dma_dev = &pdev->dev;
666
Cho KyongHof4723ec2014-05-12 11:44:52 +0530667 pm_runtime_enable(dev);
KyongHo Cho2a965362012-05-12 05:56:09 +0900668
KyongHo Cho2a965362012-05-12 05:56:09 +0900669 return 0;
KyongHo Cho2a965362012-05-12 05:56:09 +0900670}
671
Marek Szyprowski9b265532016-11-14 11:08:11 +0100672static int __maybe_unused exynos_sysmmu_suspend(struct device *dev)
Marek Szyprowski622015e2015-05-19 15:20:35 +0200673{
674 struct sysmmu_drvdata *data = dev_get_drvdata(dev);
Marek Szyprowski47a574f2016-11-14 11:08:08 +0100675 struct device *master = data->master;
Marek Szyprowski622015e2015-05-19 15:20:35 +0200676
Marek Szyprowski47a574f2016-11-14 11:08:08 +0100677 if (master) {
Marek Szyprowski9b265532016-11-14 11:08:11 +0100678 struct exynos_iommu_owner *owner = master->archdata.iommu;
679
680 mutex_lock(&owner->rpm_lock);
Marek Szyprowski92798b42016-11-14 11:08:09 +0100681 if (data->domain) {
682 dev_dbg(data->sysmmu, "saving state\n");
683 __sysmmu_disable(data);
684 }
Marek Szyprowski9b265532016-11-14 11:08:11 +0100685 mutex_unlock(&owner->rpm_lock);
Marek Szyprowski622015e2015-05-19 15:20:35 +0200686 }
687 return 0;
688}
689
Marek Szyprowski9b265532016-11-14 11:08:11 +0100690static int __maybe_unused exynos_sysmmu_resume(struct device *dev)
Marek Szyprowski622015e2015-05-19 15:20:35 +0200691{
692 struct sysmmu_drvdata *data = dev_get_drvdata(dev);
Marek Szyprowski47a574f2016-11-14 11:08:08 +0100693 struct device *master = data->master;
Marek Szyprowski622015e2015-05-19 15:20:35 +0200694
Marek Szyprowski47a574f2016-11-14 11:08:08 +0100695 if (master) {
Marek Szyprowski9b265532016-11-14 11:08:11 +0100696 struct exynos_iommu_owner *owner = master->archdata.iommu;
697
698 mutex_lock(&owner->rpm_lock);
Marek Szyprowski92798b42016-11-14 11:08:09 +0100699 if (data->domain) {
700 dev_dbg(data->sysmmu, "restoring state\n");
701 __sysmmu_enable(data);
702 }
Marek Szyprowski9b265532016-11-14 11:08:11 +0100703 mutex_unlock(&owner->rpm_lock);
Marek Szyprowski622015e2015-05-19 15:20:35 +0200704 }
705 return 0;
706}
Marek Szyprowski622015e2015-05-19 15:20:35 +0200707
708static const struct dev_pm_ops sysmmu_pm_ops = {
Marek Szyprowski9b265532016-11-14 11:08:11 +0100709 SET_RUNTIME_PM_OPS(exynos_sysmmu_suspend, exynos_sysmmu_resume, NULL)
Marek Szyprowski2f5f44f2016-11-14 11:08:12 +0100710 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
711 pm_runtime_force_resume)
Marek Szyprowski622015e2015-05-19 15:20:35 +0200712};
713
Marek Szyprowski9d25e3c2017-10-09 13:40:23 +0200714static const struct of_device_id sysmmu_of_match[] = {
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530715 { .compatible = "samsung,exynos-sysmmu", },
716 { },
717};
718
719static struct platform_driver exynos_sysmmu_driver __refdata = {
720 .probe = exynos_sysmmu_probe,
721 .driver = {
KyongHo Cho2a965362012-05-12 05:56:09 +0900722 .name = "exynos-sysmmu",
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530723 .of_match_table = sysmmu_of_match,
Marek Szyprowski622015e2015-05-19 15:20:35 +0200724 .pm = &sysmmu_pm_ops,
Marek Szyprowskib54b8742016-05-20 15:48:21 +0200725 .suppress_bind_attrs = true,
KyongHo Cho2a965362012-05-12 05:56:09 +0900726 }
727};
728
Marek Szyprowski5e3435e2016-02-18 15:12:50 +0100729static inline void update_pte(sysmmu_pte_t *ent, sysmmu_pte_t val)
KyongHo Cho2a965362012-05-12 05:56:09 +0900730{
Marek Szyprowski5e3435e2016-02-18 15:12:50 +0100731 dma_sync_single_for_cpu(dma_dev, virt_to_phys(ent), sizeof(*ent),
732 DMA_TO_DEVICE);
Ben Dooks6ae53432016-06-08 19:31:10 +0100733 *ent = cpu_to_le32(val);
Marek Szyprowski5e3435e2016-02-18 15:12:50 +0100734 dma_sync_single_for_device(dma_dev, virt_to_phys(ent), sizeof(*ent),
735 DMA_TO_DEVICE);
KyongHo Cho2a965362012-05-12 05:56:09 +0900736}
737
Joerg Roedele1fd1ea2015-03-26 13:43:11 +0100738static struct iommu_domain *exynos_iommu_domain_alloc(unsigned type)
KyongHo Cho2a965362012-05-12 05:56:09 +0900739{
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200740 struct exynos_iommu_domain *domain;
Marek Szyprowski5e3435e2016-02-18 15:12:50 +0100741 dma_addr_t handle;
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530742 int i;
KyongHo Cho2a965362012-05-12 05:56:09 +0900743
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100744 /* Check if correct PTE offsets are initialized */
745 BUG_ON(PG_ENT_SHIFT < 0 || !dma_dev);
KyongHo Cho2a965362012-05-12 05:56:09 +0900746
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200747 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
748 if (!domain)
Joerg Roedele1fd1ea2015-03-26 13:43:11 +0100749 return NULL;
750
Marek Szyprowski58c6f6a2016-02-18 15:12:49 +0100751 if (type == IOMMU_DOMAIN_DMA) {
752 if (iommu_get_dma_cookie(&domain->domain) != 0)
753 goto err_pgtable;
754 } else if (type != IOMMU_DOMAIN_UNMANAGED) {
755 goto err_pgtable;
756 }
757
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200758 domain->pgtable = (sysmmu_pte_t *)__get_free_pages(GFP_KERNEL, 2);
759 if (!domain->pgtable)
Marek Szyprowski58c6f6a2016-02-18 15:12:49 +0100760 goto err_dma_cookie;
KyongHo Cho2a965362012-05-12 05:56:09 +0900761
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200762 domain->lv2entcnt = (short *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1);
763 if (!domain->lv2entcnt)
KyongHo Cho2a965362012-05-12 05:56:09 +0900764 goto err_counter;
765
Sachin Kamatf171aba2014-08-04 10:06:28 +0530766 /* Workaround for System MMU v3.3 to prevent caching 1MiB mapping */
Marek Szyprowskie7527662017-03-24 10:18:44 +0100767 for (i = 0; i < NUM_LV1ENTRIES; i++)
768 domain->pgtable[i] = ZERO_LV2LINK;
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530769
Marek Szyprowski5e3435e2016-02-18 15:12:50 +0100770 handle = dma_map_single(dma_dev, domain->pgtable, LV1TABLE_SIZE,
771 DMA_TO_DEVICE);
772 /* For mapping page table entries we rely on dma == phys */
773 BUG_ON(handle != virt_to_phys(domain->pgtable));
Marek Szyprowski0d6d3da2017-01-09 13:03:54 +0100774 if (dma_mapping_error(dma_dev, handle))
775 goto err_lv2ent;
KyongHo Cho2a965362012-05-12 05:56:09 +0900776
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200777 spin_lock_init(&domain->lock);
778 spin_lock_init(&domain->pgtablelock);
779 INIT_LIST_HEAD(&domain->clients);
KyongHo Cho2a965362012-05-12 05:56:09 +0900780
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200781 domain->domain.geometry.aperture_start = 0;
782 domain->domain.geometry.aperture_end = ~0UL;
783 domain->domain.geometry.force_aperture = true;
Joerg Roedel3177bb72012-07-11 12:41:10 +0200784
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200785 return &domain->domain;
KyongHo Cho2a965362012-05-12 05:56:09 +0900786
Marek Szyprowski0d6d3da2017-01-09 13:03:54 +0100787err_lv2ent:
788 free_pages((unsigned long)domain->lv2entcnt, 1);
KyongHo Cho2a965362012-05-12 05:56:09 +0900789err_counter:
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200790 free_pages((unsigned long)domain->pgtable, 2);
Marek Szyprowski58c6f6a2016-02-18 15:12:49 +0100791err_dma_cookie:
792 if (type == IOMMU_DOMAIN_DMA)
793 iommu_put_dma_cookie(&domain->domain);
KyongHo Cho2a965362012-05-12 05:56:09 +0900794err_pgtable:
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200795 kfree(domain);
Joerg Roedele1fd1ea2015-03-26 13:43:11 +0100796 return NULL;
KyongHo Cho2a965362012-05-12 05:56:09 +0900797}
798
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200799static void exynos_iommu_domain_free(struct iommu_domain *iommu_domain)
KyongHo Cho2a965362012-05-12 05:56:09 +0900800{
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200801 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200802 struct sysmmu_drvdata *data, *next;
KyongHo Cho2a965362012-05-12 05:56:09 +0900803 unsigned long flags;
804 int i;
805
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200806 WARN_ON(!list_empty(&domain->clients));
KyongHo Cho2a965362012-05-12 05:56:09 +0900807
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200808 spin_lock_irqsave(&domain->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900809
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200810 list_for_each_entry_safe(data, next, &domain->clients, domain_node) {
Marek Szyprowskie1172302016-11-14 11:08:10 +0100811 spin_lock(&data->lock);
Marek Szyprowskib0d4c862016-11-14 11:08:07 +0100812 __sysmmu_disable(data);
Marek Szyprowski47a574f2016-11-14 11:08:08 +0100813 data->pgtable = 0;
814 data->domain = NULL;
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200815 list_del_init(&data->domain_node);
Marek Szyprowskie1172302016-11-14 11:08:10 +0100816 spin_unlock(&data->lock);
KyongHo Cho2a965362012-05-12 05:56:09 +0900817 }
818
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200819 spin_unlock_irqrestore(&domain->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900820
Marek Szyprowski58c6f6a2016-02-18 15:12:49 +0100821 if (iommu_domain->type == IOMMU_DOMAIN_DMA)
822 iommu_put_dma_cookie(iommu_domain);
823
Marek Szyprowski5e3435e2016-02-18 15:12:50 +0100824 dma_unmap_single(dma_dev, virt_to_phys(domain->pgtable), LV1TABLE_SIZE,
825 DMA_TO_DEVICE);
826
KyongHo Cho2a965362012-05-12 05:56:09 +0900827 for (i = 0; i < NUM_LV1ENTRIES; i++)
Marek Szyprowski5e3435e2016-02-18 15:12:50 +0100828 if (lv1ent_page(domain->pgtable + i)) {
829 phys_addr_t base = lv2table_base(domain->pgtable + i);
830
831 dma_unmap_single(dma_dev, base, LV2TABLE_SIZE,
832 DMA_TO_DEVICE);
Cho KyongHo734c3c72014-05-12 11:44:48 +0530833 kmem_cache_free(lv2table_kmem_cache,
Marek Szyprowski5e3435e2016-02-18 15:12:50 +0100834 phys_to_virt(base));
835 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900836
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200837 free_pages((unsigned long)domain->pgtable, 2);
838 free_pages((unsigned long)domain->lv2entcnt, 1);
839 kfree(domain);
KyongHo Cho2a965362012-05-12 05:56:09 +0900840}
841
Marek Szyprowski5fa61cb2016-02-18 15:13:00 +0100842static void exynos_iommu_detach_device(struct iommu_domain *iommu_domain,
843 struct device *dev)
844{
845 struct exynos_iommu_owner *owner = dev->archdata.iommu;
846 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
847 phys_addr_t pagetable = virt_to_phys(domain->pgtable);
848 struct sysmmu_drvdata *data, *next;
849 unsigned long flags;
Marek Szyprowski5fa61cb2016-02-18 15:13:00 +0100850
851 if (!has_sysmmu(dev) || owner->domain != iommu_domain)
852 return;
853
Marek Szyprowski9b265532016-11-14 11:08:11 +0100854 mutex_lock(&owner->rpm_lock);
855
856 list_for_each_entry(data, &owner->controllers, owner_node) {
857 pm_runtime_get_noresume(data->sysmmu);
858 if (pm_runtime_active(data->sysmmu))
859 __sysmmu_disable(data);
Marek Szyprowskie1172302016-11-14 11:08:10 +0100860 pm_runtime_put(data->sysmmu);
861 }
862
Marek Szyprowski5fa61cb2016-02-18 15:13:00 +0100863 spin_lock_irqsave(&domain->lock, flags);
864 list_for_each_entry_safe(data, next, &domain->clients, domain_node) {
Marek Szyprowskie1172302016-11-14 11:08:10 +0100865 spin_lock(&data->lock);
Marek Szyprowski47a574f2016-11-14 11:08:08 +0100866 data->pgtable = 0;
867 data->domain = NULL;
Marek Szyprowskib0d4c862016-11-14 11:08:07 +0100868 list_del_init(&data->domain_node);
Marek Szyprowskie1172302016-11-14 11:08:10 +0100869 spin_unlock(&data->lock);
Marek Szyprowski5fa61cb2016-02-18 15:13:00 +0100870 }
Marek Szyprowskie1172302016-11-14 11:08:10 +0100871 owner->domain = NULL;
Marek Szyprowski5fa61cb2016-02-18 15:13:00 +0100872 spin_unlock_irqrestore(&domain->lock, flags);
873
Marek Szyprowski9b265532016-11-14 11:08:11 +0100874 mutex_unlock(&owner->rpm_lock);
Marek Szyprowski5fa61cb2016-02-18 15:13:00 +0100875
Marek Szyprowskib0d4c862016-11-14 11:08:07 +0100876 dev_dbg(dev, "%s: Detached IOMMU with pgtable %pa\n", __func__,
877 &pagetable);
Marek Szyprowski5fa61cb2016-02-18 15:13:00 +0100878}
879
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200880static int exynos_iommu_attach_device(struct iommu_domain *iommu_domain,
KyongHo Cho2a965362012-05-12 05:56:09 +0900881 struct device *dev)
882{
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530883 struct exynos_iommu_owner *owner = dev->archdata.iommu;
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200884 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200885 struct sysmmu_drvdata *data;
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200886 phys_addr_t pagetable = virt_to_phys(domain->pgtable);
KyongHo Cho2a965362012-05-12 05:56:09 +0900887 unsigned long flags;
KyongHo Cho2a965362012-05-12 05:56:09 +0900888
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200889 if (!has_sysmmu(dev))
890 return -ENODEV;
KyongHo Cho2a965362012-05-12 05:56:09 +0900891
Marek Szyprowski5fa61cb2016-02-18 15:13:00 +0100892 if (owner->domain)
893 exynos_iommu_detach_device(owner->domain, dev);
894
Marek Szyprowski9b265532016-11-14 11:08:11 +0100895 mutex_lock(&owner->rpm_lock);
896
Marek Szyprowskie1172302016-11-14 11:08:10 +0100897 spin_lock_irqsave(&domain->lock, flags);
Marek Szyprowski1b092052015-05-19 15:20:33 +0200898 list_for_each_entry(data, &owner->controllers, owner_node) {
Marek Szyprowskie1172302016-11-14 11:08:10 +0100899 spin_lock(&data->lock);
Marek Szyprowski47a574f2016-11-14 11:08:08 +0100900 data->pgtable = pagetable;
901 data->domain = domain;
Marek Szyprowskie1172302016-11-14 11:08:10 +0100902 list_add_tail(&data->domain_node, &domain->clients);
903 spin_unlock(&data->lock);
904 }
905 owner->domain = iommu_domain;
906 spin_unlock_irqrestore(&domain->lock, flags);
907
908 list_for_each_entry(data, &owner->controllers, owner_node) {
Marek Szyprowski9b265532016-11-14 11:08:11 +0100909 pm_runtime_get_noresume(data->sysmmu);
910 if (pm_runtime_active(data->sysmmu))
911 __sysmmu_enable(data);
912 pm_runtime_put(data->sysmmu);
913 }
914
915 mutex_unlock(&owner->rpm_lock);
916
Marek Szyprowskib0d4c862016-11-14 11:08:07 +0100917 dev_dbg(dev, "%s: Attached IOMMU with pgtable %pa\n", __func__,
918 &pagetable);
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530919
Marek Szyprowskib0d4c862016-11-14 11:08:07 +0100920 return 0;
KyongHo Cho2a965362012-05-12 05:56:09 +0900921}
922
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200923static sysmmu_pte_t *alloc_lv2entry(struct exynos_iommu_domain *domain,
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530924 sysmmu_pte_t *sent, sysmmu_iova_t iova, short *pgcounter)
KyongHo Cho2a965362012-05-12 05:56:09 +0900925{
Cho KyongHo61128f02014-05-12 11:44:47 +0530926 if (lv1ent_section(sent)) {
Cho KyongHod09d78f2014-05-12 11:44:58 +0530927 WARN(1, "Trying mapping on %#08x mapped with 1MiB page", iova);
Cho KyongHo61128f02014-05-12 11:44:47 +0530928 return ERR_PTR(-EADDRINUSE);
929 }
930
KyongHo Cho2a965362012-05-12 05:56:09 +0900931 if (lv1ent_fault(sent)) {
Marek Szyprowski0d6d3da2017-01-09 13:03:54 +0100932 dma_addr_t handle;
Cho KyongHod09d78f2014-05-12 11:44:58 +0530933 sysmmu_pte_t *pent;
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530934 bool need_flush_flpd_cache = lv1ent_zero(sent);
KyongHo Cho2a965362012-05-12 05:56:09 +0900935
Cho KyongHo734c3c72014-05-12 11:44:48 +0530936 pent = kmem_cache_zalloc(lv2table_kmem_cache, GFP_ATOMIC);
Arnd Bergmanndbf6c6e2016-02-29 09:45:59 +0100937 BUG_ON((uintptr_t)pent & (LV2TABLE_SIZE - 1));
KyongHo Cho2a965362012-05-12 05:56:09 +0900938 if (!pent)
Cho KyongHo61128f02014-05-12 11:44:47 +0530939 return ERR_PTR(-ENOMEM);
KyongHo Cho2a965362012-05-12 05:56:09 +0900940
Marek Szyprowski5e3435e2016-02-18 15:12:50 +0100941 update_pte(sent, mk_lv1ent_page(virt_to_phys(pent)));
Colin Crossdc3814f2015-05-08 17:05:44 -0700942 kmemleak_ignore(pent);
KyongHo Cho2a965362012-05-12 05:56:09 +0900943 *pgcounter = NUM_LV2ENTRIES;
Marek Szyprowski0d6d3da2017-01-09 13:03:54 +0100944 handle = dma_map_single(dma_dev, pent, LV2TABLE_SIZE,
945 DMA_TO_DEVICE);
946 if (dma_mapping_error(dma_dev, handle)) {
947 kmem_cache_free(lv2table_kmem_cache, pent);
948 return ERR_PTR(-EADDRINUSE);
949 }
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530950
951 /*
Sachin Kamatf171aba2014-08-04 10:06:28 +0530952 * If pre-fetched SLPD is a faulty SLPD in zero_l2_table,
953 * FLPD cache may cache the address of zero_l2_table. This
954 * function replaces the zero_l2_table with new L2 page table
955 * to write valid mappings.
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530956 * Accessing the valid area may cause page fault since FLPD
Sachin Kamatf171aba2014-08-04 10:06:28 +0530957 * cache may still cache zero_l2_table for the valid area
958 * instead of new L2 page table that has the mapping
959 * information of the valid area.
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530960 * Thus any replacement of zero_l2_table with other valid L2
961 * page table must involve FLPD cache invalidation for System
962 * MMU v3.3.
963 * FLPD cache invalidation is performed with TLB invalidation
964 * by VPN without blocking. It is safe to invalidate TLB without
965 * blocking because the target address of TLB invalidation is
966 * not currently mapped.
967 */
968 if (need_flush_flpd_cache) {
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200969 struct sysmmu_drvdata *data;
Sachin Kamat365409d2014-05-22 09:50:56 +0530970
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200971 spin_lock(&domain->lock);
972 list_for_each_entry(data, &domain->clients, domain_node)
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200973 sysmmu_tlb_invalidate_flpdcache(data, iova);
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200974 spin_unlock(&domain->lock);
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530975 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900976 }
977
978 return page_entry(sent, iova);
979}
980
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200981static int lv1set_section(struct exynos_iommu_domain *domain,
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530982 sysmmu_pte_t *sent, sysmmu_iova_t iova,
Marek Szyprowski1a0d8da2016-11-03 09:04:45 +0100983 phys_addr_t paddr, int prot, short *pgcnt)
KyongHo Cho2a965362012-05-12 05:56:09 +0900984{
Cho KyongHo61128f02014-05-12 11:44:47 +0530985 if (lv1ent_section(sent)) {
Cho KyongHod09d78f2014-05-12 11:44:58 +0530986 WARN(1, "Trying mapping on 1MiB@%#08x that is mapped",
Cho KyongHo61128f02014-05-12 11:44:47 +0530987 iova);
KyongHo Cho2a965362012-05-12 05:56:09 +0900988 return -EADDRINUSE;
Cho KyongHo61128f02014-05-12 11:44:47 +0530989 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900990
991 if (lv1ent_page(sent)) {
Cho KyongHo61128f02014-05-12 11:44:47 +0530992 if (*pgcnt != NUM_LV2ENTRIES) {
Cho KyongHod09d78f2014-05-12 11:44:58 +0530993 WARN(1, "Trying mapping on 1MiB@%#08x that is mapped",
Cho KyongHo61128f02014-05-12 11:44:47 +0530994 iova);
KyongHo Cho2a965362012-05-12 05:56:09 +0900995 return -EADDRINUSE;
Cho KyongHo61128f02014-05-12 11:44:47 +0530996 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900997
Cho KyongHo734c3c72014-05-12 11:44:48 +0530998 kmem_cache_free(lv2table_kmem_cache, page_entry(sent, 0));
KyongHo Cho2a965362012-05-12 05:56:09 +0900999 *pgcnt = 0;
1000 }
1001
Marek Szyprowski1a0d8da2016-11-03 09:04:45 +01001002 update_pte(sent, mk_lv1ent_sect(paddr, prot));
KyongHo Cho2a965362012-05-12 05:56:09 +09001003
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001004 spin_lock(&domain->lock);
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301005 if (lv1ent_page_zero(sent)) {
Marek Szyprowski469aceb2015-05-19 15:20:27 +02001006 struct sysmmu_drvdata *data;
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301007 /*
1008 * Flushing FLPD cache in System MMU v3.3 that may cache a FLPD
1009 * entry by speculative prefetch of SLPD which has no mapping.
1010 */
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001011 list_for_each_entry(data, &domain->clients, domain_node)
Marek Szyprowski469aceb2015-05-19 15:20:27 +02001012 sysmmu_tlb_invalidate_flpdcache(data, iova);
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301013 }
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001014 spin_unlock(&domain->lock);
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301015
KyongHo Cho2a965362012-05-12 05:56:09 +09001016 return 0;
1017}
1018
Cho KyongHod09d78f2014-05-12 11:44:58 +05301019static int lv2set_page(sysmmu_pte_t *pent, phys_addr_t paddr, size_t size,
Marek Szyprowski1a0d8da2016-11-03 09:04:45 +01001020 int prot, short *pgcnt)
KyongHo Cho2a965362012-05-12 05:56:09 +09001021{
1022 if (size == SPAGE_SIZE) {
Cho KyongHo0bf4e542014-05-12 11:45:00 +05301023 if (WARN_ON(!lv2ent_fault(pent)))
KyongHo Cho2a965362012-05-12 05:56:09 +09001024 return -EADDRINUSE;
1025
Marek Szyprowski1a0d8da2016-11-03 09:04:45 +01001026 update_pte(pent, mk_lv2ent_spage(paddr, prot));
KyongHo Cho2a965362012-05-12 05:56:09 +09001027 *pgcnt -= 1;
1028 } else { /* size == LPAGE_SIZE */
1029 int i;
Marek Szyprowski5e3435e2016-02-18 15:12:50 +01001030 dma_addr_t pent_base = virt_to_phys(pent);
Sachin Kamat365409d2014-05-22 09:50:56 +05301031
Marek Szyprowski5e3435e2016-02-18 15:12:50 +01001032 dma_sync_single_for_cpu(dma_dev, pent_base,
1033 sizeof(*pent) * SPAGES_PER_LPAGE,
1034 DMA_TO_DEVICE);
KyongHo Cho2a965362012-05-12 05:56:09 +09001035 for (i = 0; i < SPAGES_PER_LPAGE; i++, pent++) {
Cho KyongHo0bf4e542014-05-12 11:45:00 +05301036 if (WARN_ON(!lv2ent_fault(pent))) {
Cho KyongHo61128f02014-05-12 11:44:47 +05301037 if (i > 0)
1038 memset(pent - i, 0, sizeof(*pent) * i);
KyongHo Cho2a965362012-05-12 05:56:09 +09001039 return -EADDRINUSE;
1040 }
1041
Marek Szyprowski1a0d8da2016-11-03 09:04:45 +01001042 *pent = mk_lv2ent_lpage(paddr, prot);
KyongHo Cho2a965362012-05-12 05:56:09 +09001043 }
Marek Szyprowski5e3435e2016-02-18 15:12:50 +01001044 dma_sync_single_for_device(dma_dev, pent_base,
1045 sizeof(*pent) * SPAGES_PER_LPAGE,
1046 DMA_TO_DEVICE);
KyongHo Cho2a965362012-05-12 05:56:09 +09001047 *pgcnt -= SPAGES_PER_LPAGE;
1048 }
1049
1050 return 0;
1051}
1052
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301053/*
1054 * *CAUTION* to the I/O virtual memory managers that support exynos-iommu:
1055 *
Sachin Kamatf171aba2014-08-04 10:06:28 +05301056 * System MMU v3.x has advanced logic to improve address translation
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301057 * performance with caching more page table entries by a page table walk.
Sachin Kamatf171aba2014-08-04 10:06:28 +05301058 * However, the logic has a bug that while caching faulty page table entries,
1059 * System MMU reports page fault if the cached fault entry is hit even though
1060 * the fault entry is updated to a valid entry after the entry is cached.
1061 * To prevent caching faulty page table entries which may be updated to valid
1062 * entries later, the virtual memory manager should care about the workaround
1063 * for the problem. The following describes the workaround.
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301064 *
1065 * Any two consecutive I/O virtual address regions must have a hole of 128KiB
Sachin Kamatf171aba2014-08-04 10:06:28 +05301066 * at maximum to prevent misbehavior of System MMU 3.x (workaround for h/w bug).
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301067 *
Sachin Kamatf171aba2014-08-04 10:06:28 +05301068 * Precisely, any start address of I/O virtual region must be aligned with
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301069 * the following sizes for System MMU v3.1 and v3.2.
1070 * System MMU v3.1: 128KiB
1071 * System MMU v3.2: 256KiB
1072 *
1073 * Because System MMU v3.3 caches page table entries more aggressively, it needs
Sachin Kamatf171aba2014-08-04 10:06:28 +05301074 * more workarounds.
1075 * - Any two consecutive I/O virtual regions must have a hole of size larger
1076 * than or equal to 128KiB.
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301077 * - Start address of an I/O virtual region must be aligned by 128KiB.
1078 */
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001079static int exynos_iommu_map(struct iommu_domain *iommu_domain,
1080 unsigned long l_iova, phys_addr_t paddr, size_t size,
1081 int prot)
KyongHo Cho2a965362012-05-12 05:56:09 +09001082{
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001083 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
Cho KyongHod09d78f2014-05-12 11:44:58 +05301084 sysmmu_pte_t *entry;
1085 sysmmu_iova_t iova = (sysmmu_iova_t)l_iova;
KyongHo Cho2a965362012-05-12 05:56:09 +09001086 unsigned long flags;
1087 int ret = -ENOMEM;
1088
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001089 BUG_ON(domain->pgtable == NULL);
Marek Szyprowski1a0d8da2016-11-03 09:04:45 +01001090 prot &= SYSMMU_SUPPORTED_PROT_BITS;
KyongHo Cho2a965362012-05-12 05:56:09 +09001091
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001092 spin_lock_irqsave(&domain->pgtablelock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +09001093
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001094 entry = section_entry(domain->pgtable, iova);
KyongHo Cho2a965362012-05-12 05:56:09 +09001095
1096 if (size == SECT_SIZE) {
Marek Szyprowski1a0d8da2016-11-03 09:04:45 +01001097 ret = lv1set_section(domain, entry, iova, paddr, prot,
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001098 &domain->lv2entcnt[lv1ent_offset(iova)]);
KyongHo Cho2a965362012-05-12 05:56:09 +09001099 } else {
Cho KyongHod09d78f2014-05-12 11:44:58 +05301100 sysmmu_pte_t *pent;
KyongHo Cho2a965362012-05-12 05:56:09 +09001101
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001102 pent = alloc_lv2entry(domain, entry, iova,
1103 &domain->lv2entcnt[lv1ent_offset(iova)]);
KyongHo Cho2a965362012-05-12 05:56:09 +09001104
Cho KyongHo61128f02014-05-12 11:44:47 +05301105 if (IS_ERR(pent))
1106 ret = PTR_ERR(pent);
KyongHo Cho2a965362012-05-12 05:56:09 +09001107 else
Marek Szyprowski1a0d8da2016-11-03 09:04:45 +01001108 ret = lv2set_page(pent, paddr, size, prot,
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001109 &domain->lv2entcnt[lv1ent_offset(iova)]);
KyongHo Cho2a965362012-05-12 05:56:09 +09001110 }
1111
Cho KyongHo61128f02014-05-12 11:44:47 +05301112 if (ret)
Cho KyongHo0bf4e542014-05-12 11:45:00 +05301113 pr_err("%s: Failed(%d) to map %#zx bytes @ %#x\n",
1114 __func__, ret, size, iova);
KyongHo Cho2a965362012-05-12 05:56:09 +09001115
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001116 spin_unlock_irqrestore(&domain->pgtablelock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +09001117
1118 return ret;
1119}
1120
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001121static void exynos_iommu_tlb_invalidate_entry(struct exynos_iommu_domain *domain,
1122 sysmmu_iova_t iova, size_t size)
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301123{
Marek Szyprowski469aceb2015-05-19 15:20:27 +02001124 struct sysmmu_drvdata *data;
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301125 unsigned long flags;
1126
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001127 spin_lock_irqsave(&domain->lock, flags);
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301128
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001129 list_for_each_entry(data, &domain->clients, domain_node)
Marek Szyprowski469aceb2015-05-19 15:20:27 +02001130 sysmmu_tlb_invalidate_entry(data, iova, size);
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301131
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001132 spin_unlock_irqrestore(&domain->lock, flags);
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301133}
1134
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001135static size_t exynos_iommu_unmap(struct iommu_domain *iommu_domain,
1136 unsigned long l_iova, size_t size)
KyongHo Cho2a965362012-05-12 05:56:09 +09001137{
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001138 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
Cho KyongHod09d78f2014-05-12 11:44:58 +05301139 sysmmu_iova_t iova = (sysmmu_iova_t)l_iova;
1140 sysmmu_pte_t *ent;
Cho KyongHo61128f02014-05-12 11:44:47 +05301141 size_t err_pgsize;
Cho KyongHod09d78f2014-05-12 11:44:58 +05301142 unsigned long flags;
KyongHo Cho2a965362012-05-12 05:56:09 +09001143
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001144 BUG_ON(domain->pgtable == NULL);
KyongHo Cho2a965362012-05-12 05:56:09 +09001145
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001146 spin_lock_irqsave(&domain->pgtablelock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +09001147
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001148 ent = section_entry(domain->pgtable, iova);
KyongHo Cho2a965362012-05-12 05:56:09 +09001149
1150 if (lv1ent_section(ent)) {
Cho KyongHo0bf4e542014-05-12 11:45:00 +05301151 if (WARN_ON(size < SECT_SIZE)) {
Cho KyongHo61128f02014-05-12 11:44:47 +05301152 err_pgsize = SECT_SIZE;
1153 goto err;
1154 }
KyongHo Cho2a965362012-05-12 05:56:09 +09001155
Sachin Kamatf171aba2014-08-04 10:06:28 +05301156 /* workaround for h/w bug in System MMU v3.3 */
Marek Szyprowski5e3435e2016-02-18 15:12:50 +01001157 update_pte(ent, ZERO_LV2LINK);
KyongHo Cho2a965362012-05-12 05:56:09 +09001158 size = SECT_SIZE;
1159 goto done;
1160 }
1161
1162 if (unlikely(lv1ent_fault(ent))) {
1163 if (size > SECT_SIZE)
1164 size = SECT_SIZE;
1165 goto done;
1166 }
1167
1168 /* lv1ent_page(sent) == true here */
1169
1170 ent = page_entry(ent, iova);
1171
1172 if (unlikely(lv2ent_fault(ent))) {
1173 size = SPAGE_SIZE;
1174 goto done;
1175 }
1176
1177 if (lv2ent_small(ent)) {
Marek Szyprowski5e3435e2016-02-18 15:12:50 +01001178 update_pte(ent, 0);
KyongHo Cho2a965362012-05-12 05:56:09 +09001179 size = SPAGE_SIZE;
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001180 domain->lv2entcnt[lv1ent_offset(iova)] += 1;
KyongHo Cho2a965362012-05-12 05:56:09 +09001181 goto done;
1182 }
1183
1184 /* lv1ent_large(ent) == true here */
Cho KyongHo0bf4e542014-05-12 11:45:00 +05301185 if (WARN_ON(size < LPAGE_SIZE)) {
Cho KyongHo61128f02014-05-12 11:44:47 +05301186 err_pgsize = LPAGE_SIZE;
1187 goto err;
1188 }
KyongHo Cho2a965362012-05-12 05:56:09 +09001189
Marek Szyprowski5e3435e2016-02-18 15:12:50 +01001190 dma_sync_single_for_cpu(dma_dev, virt_to_phys(ent),
1191 sizeof(*ent) * SPAGES_PER_LPAGE,
1192 DMA_TO_DEVICE);
KyongHo Cho2a965362012-05-12 05:56:09 +09001193 memset(ent, 0, sizeof(*ent) * SPAGES_PER_LPAGE);
Marek Szyprowski5e3435e2016-02-18 15:12:50 +01001194 dma_sync_single_for_device(dma_dev, virt_to_phys(ent),
1195 sizeof(*ent) * SPAGES_PER_LPAGE,
1196 DMA_TO_DEVICE);
KyongHo Cho2a965362012-05-12 05:56:09 +09001197 size = LPAGE_SIZE;
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001198 domain->lv2entcnt[lv1ent_offset(iova)] += SPAGES_PER_LPAGE;
KyongHo Cho2a965362012-05-12 05:56:09 +09001199done:
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001200 spin_unlock_irqrestore(&domain->pgtablelock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +09001201
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001202 exynos_iommu_tlb_invalidate_entry(domain, iova, size);
KyongHo Cho2a965362012-05-12 05:56:09 +09001203
KyongHo Cho2a965362012-05-12 05:56:09 +09001204 return size;
Cho KyongHo61128f02014-05-12 11:44:47 +05301205err:
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001206 spin_unlock_irqrestore(&domain->pgtablelock, flags);
Cho KyongHo61128f02014-05-12 11:44:47 +05301207
Cho KyongHo0bf4e542014-05-12 11:45:00 +05301208 pr_err("%s: Failed: size(%#zx) @ %#x is smaller than page size %#zx\n",
1209 __func__, size, iova, err_pgsize);
Cho KyongHo61128f02014-05-12 11:44:47 +05301210
1211 return 0;
KyongHo Cho2a965362012-05-12 05:56:09 +09001212}
1213
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001214static phys_addr_t exynos_iommu_iova_to_phys(struct iommu_domain *iommu_domain,
Varun Sethibb5547a2013-03-29 01:23:58 +05301215 dma_addr_t iova)
KyongHo Cho2a965362012-05-12 05:56:09 +09001216{
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001217 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
Cho KyongHod09d78f2014-05-12 11:44:58 +05301218 sysmmu_pte_t *entry;
KyongHo Cho2a965362012-05-12 05:56:09 +09001219 unsigned long flags;
1220 phys_addr_t phys = 0;
1221
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001222 spin_lock_irqsave(&domain->pgtablelock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +09001223
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001224 entry = section_entry(domain->pgtable, iova);
KyongHo Cho2a965362012-05-12 05:56:09 +09001225
1226 if (lv1ent_section(entry)) {
1227 phys = section_phys(entry) + section_offs(iova);
1228 } else if (lv1ent_page(entry)) {
1229 entry = page_entry(entry, iova);
1230
1231 if (lv2ent_large(entry))
1232 phys = lpage_phys(entry) + lpage_offs(iova);
1233 else if (lv2ent_small(entry))
1234 phys = spage_phys(entry) + spage_offs(iova);
1235 }
1236
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001237 spin_unlock_irqrestore(&domain->pgtablelock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +09001238
1239 return phys;
1240}
1241
Antonios Motakisbf4a1c92014-05-12 11:44:59 +05301242static int exynos_iommu_add_device(struct device *dev)
1243{
Marek Szyprowski7a974b22017-09-15 13:05:08 +02001244 struct exynos_iommu_owner *owner = dev->archdata.iommu;
1245 struct sysmmu_drvdata *data;
Antonios Motakisbf4a1c92014-05-12 11:44:59 +05301246 struct iommu_group *group;
Antonios Motakisbf4a1c92014-05-12 11:44:59 +05301247
Marek Szyprowski06801db2015-05-19 15:20:32 +02001248 if (!has_sysmmu(dev))
1249 return -ENODEV;
1250
Marek Szyprowski6c2ae7e2016-02-18 15:12:48 +01001251 group = iommu_group_get_for_dev(dev);
Antonios Motakisbf4a1c92014-05-12 11:44:59 +05301252
Marek Szyprowski6c2ae7e2016-02-18 15:12:48 +01001253 if (IS_ERR(group))
1254 return PTR_ERR(group);
Antonios Motakisbf4a1c92014-05-12 11:44:59 +05301255
Marek Szyprowski7a974b22017-09-15 13:05:08 +02001256 list_for_each_entry(data, &owner->controllers, owner_node) {
1257 /*
1258 * SYSMMU will be runtime activated via device link
1259 * (dependency) to its master device, so there are no
1260 * direct calls to pm_runtime_get/put in this driver.
1261 */
1262 data->link = device_link_add(dev, data->sysmmu,
Rafael J. Wysockiea4f6402019-02-01 01:54:21 +01001263 DL_FLAG_STATELESS |
Marek Szyprowski7a974b22017-09-15 13:05:08 +02001264 DL_FLAG_PM_RUNTIME);
1265 }
Antonios Motakisbf4a1c92014-05-12 11:44:59 +05301266 iommu_group_put(group);
1267
Marek Szyprowski6c2ae7e2016-02-18 15:12:48 +01001268 return 0;
Antonios Motakisbf4a1c92014-05-12 11:44:59 +05301269}
1270
1271static void exynos_iommu_remove_device(struct device *dev)
1272{
Marek Szyprowskifff2fd12017-01-09 13:03:56 +01001273 struct exynos_iommu_owner *owner = dev->archdata.iommu;
Marek Szyprowski7a974b22017-09-15 13:05:08 +02001274 struct sysmmu_drvdata *data;
Marek Szyprowskifff2fd12017-01-09 13:03:56 +01001275
Marek Szyprowski06801db2015-05-19 15:20:32 +02001276 if (!has_sysmmu(dev))
1277 return;
1278
Marek Szyprowskifff2fd12017-01-09 13:03:56 +01001279 if (owner->domain) {
1280 struct iommu_group *group = iommu_group_get(dev);
1281
1282 if (group) {
1283 WARN_ON(owner->domain !=
1284 iommu_group_default_domain(group));
1285 exynos_iommu_detach_device(owner->domain, dev);
1286 iommu_group_put(group);
1287 }
1288 }
Antonios Motakisbf4a1c92014-05-12 11:44:59 +05301289 iommu_group_remove_device(dev);
Marek Szyprowski7a974b22017-09-15 13:05:08 +02001290
1291 list_for_each_entry(data, &owner->controllers, owner_node)
1292 device_link_del(data->link);
Antonios Motakisbf4a1c92014-05-12 11:44:59 +05301293}
1294
Marek Szyprowskiaa759fd2015-05-19 15:20:37 +02001295static int exynos_iommu_of_xlate(struct device *dev,
1296 struct of_phandle_args *spec)
1297{
1298 struct exynos_iommu_owner *owner = dev->archdata.iommu;
1299 struct platform_device *sysmmu = of_find_device_by_node(spec->np);
Marek Szyprowski0bd5a0c2017-01-09 13:03:55 +01001300 struct sysmmu_drvdata *data, *entry;
Marek Szyprowskiaa759fd2015-05-19 15:20:37 +02001301
1302 if (!sysmmu)
1303 return -ENODEV;
1304
1305 data = platform_get_drvdata(sysmmu);
1306 if (!data)
1307 return -ENODEV;
1308
1309 if (!owner) {
1310 owner = kzalloc(sizeof(*owner), GFP_KERNEL);
1311 if (!owner)
1312 return -ENOMEM;
1313
1314 INIT_LIST_HEAD(&owner->controllers);
Marek Szyprowski9b265532016-11-14 11:08:11 +01001315 mutex_init(&owner->rpm_lock);
Marek Szyprowskiaa759fd2015-05-19 15:20:37 +02001316 dev->archdata.iommu = owner;
1317 }
1318
Marek Szyprowski0bd5a0c2017-01-09 13:03:55 +01001319 list_for_each_entry(entry, &owner->controllers, owner_node)
1320 if (entry == data)
1321 return 0;
1322
Marek Szyprowskiaa759fd2015-05-19 15:20:37 +02001323 list_add_tail(&data->owner_node, &owner->controllers);
Marek Szyprowski92798b42016-11-14 11:08:09 +01001324 data->master = dev;
Marek Szyprowski2f5f44f2016-11-14 11:08:12 +01001325
Marek Szyprowskiaa759fd2015-05-19 15:20:37 +02001326 return 0;
1327}
1328
Arvind Yadav0b9a3692017-08-28 17:42:05 +05301329static const struct iommu_ops exynos_iommu_ops = {
Joerg Roedele1fd1ea2015-03-26 13:43:11 +01001330 .domain_alloc = exynos_iommu_domain_alloc,
1331 .domain_free = exynos_iommu_domain_free,
Bjorn Helgaasba5fa6f2014-05-08 14:49:14 -06001332 .attach_dev = exynos_iommu_attach_device,
1333 .detach_dev = exynos_iommu_detach_device,
1334 .map = exynos_iommu_map,
1335 .unmap = exynos_iommu_unmap,
1336 .iova_to_phys = exynos_iommu_iova_to_phys,
Robin Murphy6d7cf022018-01-24 14:22:09 +00001337 .device_group = generic_device_group,
Bjorn Helgaasba5fa6f2014-05-08 14:49:14 -06001338 .add_device = exynos_iommu_add_device,
1339 .remove_device = exynos_iommu_remove_device,
KyongHo Cho2a965362012-05-12 05:56:09 +09001340 .pgsize_bitmap = SECT_SIZE | LPAGE_SIZE | SPAGE_SIZE,
Marek Szyprowskiaa759fd2015-05-19 15:20:37 +02001341 .of_xlate = exynos_iommu_of_xlate,
KyongHo Cho2a965362012-05-12 05:56:09 +09001342};
1343
1344static int __init exynos_iommu_init(void)
1345{
Robin Murphydc98b842018-01-09 15:34:07 +00001346 struct device_node *np;
KyongHo Cho2a965362012-05-12 05:56:09 +09001347 int ret;
1348
Robin Murphydc98b842018-01-09 15:34:07 +00001349 np = of_find_matching_node(NULL, sysmmu_of_match);
1350 if (!np)
1351 return 0;
1352
1353 of_node_put(np);
1354
Cho KyongHo734c3c72014-05-12 11:44:48 +05301355 lv2table_kmem_cache = kmem_cache_create("exynos-iommu-lv2table",
1356 LV2TABLE_SIZE, LV2TABLE_SIZE, 0, NULL);
1357 if (!lv2table_kmem_cache) {
1358 pr_err("%s: Failed to create kmem cache\n", __func__);
1359 return -ENOMEM;
1360 }
1361
KyongHo Cho2a965362012-05-12 05:56:09 +09001362 ret = platform_driver_register(&exynos_sysmmu_driver);
Cho KyongHo734c3c72014-05-12 11:44:48 +05301363 if (ret) {
1364 pr_err("%s: Failed to register driver\n", __func__);
1365 goto err_reg_driver;
1366 }
KyongHo Cho2a965362012-05-12 05:56:09 +09001367
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301368 zero_lv2_table = kmem_cache_zalloc(lv2table_kmem_cache, GFP_KERNEL);
1369 if (zero_lv2_table == NULL) {
1370 pr_err("%s: Failed to allocate zero level2 page table\n",
1371 __func__);
1372 ret = -ENOMEM;
1373 goto err_zero_lv2;
1374 }
1375
Cho KyongHo734c3c72014-05-12 11:44:48 +05301376 ret = bus_set_iommu(&platform_bus_type, &exynos_iommu_ops);
1377 if (ret) {
1378 pr_err("%s: Failed to register exynos-iommu driver.\n",
1379 __func__);
1380 goto err_set_iommu;
1381 }
KyongHo Cho2a965362012-05-12 05:56:09 +09001382
Cho KyongHo734c3c72014-05-12 11:44:48 +05301383 return 0;
1384err_set_iommu:
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301385 kmem_cache_free(lv2table_kmem_cache, zero_lv2_table);
1386err_zero_lv2:
Cho KyongHo734c3c72014-05-12 11:44:48 +05301387 platform_driver_unregister(&exynos_sysmmu_driver);
1388err_reg_driver:
1389 kmem_cache_destroy(lv2table_kmem_cache);
KyongHo Cho2a965362012-05-12 05:56:09 +09001390 return ret;
1391}
Marek Szyprowski928055a2017-08-04 12:28:33 +02001392core_initcall(exynos_iommu_init);