blob: 9d9fea2156cb549e1788eb8f835827e788929f71 [file] [log] [blame]
KyongHo Cho2a965362012-05-12 05:56:09 +09001/* linux/drivers/iommu/exynos_iommu.c
2 *
3 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
4 * http://www.samsung.com
5 *
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.
9 */
10
11#ifdef CONFIG_EXYNOS_IOMMU_DEBUG
12#define DEBUG
13#endif
14
KyongHo Cho2a965362012-05-12 05:56:09 +090015#include <linux/clk.h>
16#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>
KyongHo Cho2a965362012-05-12 05:56:09 +090020#include <linux/list.h>
Marek Szyprowski312900c2015-05-19 15:20:30 +020021#include <linux/platform_device.h>
22#include <linux/pm_runtime.h>
23#include <linux/slab.h>
KyongHo Cho2a965362012-05-12 05:56:09 +090024
25#include <asm/cacheflush.h>
26#include <asm/pgtable.h>
27
Cho KyongHod09d78f2014-05-12 11:44:58 +053028typedef u32 sysmmu_iova_t;
29typedef u32 sysmmu_pte_t;
30
Sachin Kamatf171aba2014-08-04 10:06:28 +053031/* We do not consider super section mapping (16MB) */
KyongHo Cho2a965362012-05-12 05:56:09 +090032#define SECT_ORDER 20
33#define LPAGE_ORDER 16
34#define SPAGE_ORDER 12
35
36#define SECT_SIZE (1 << SECT_ORDER)
37#define LPAGE_SIZE (1 << LPAGE_ORDER)
38#define SPAGE_SIZE (1 << SPAGE_ORDER)
39
40#define SECT_MASK (~(SECT_SIZE - 1))
41#define LPAGE_MASK (~(LPAGE_SIZE - 1))
42#define SPAGE_MASK (~(SPAGE_SIZE - 1))
43
Cho KyongHo66a7ed82014-05-12 11:45:04 +053044#define lv1ent_fault(sent) ((*(sent) == ZERO_LV2LINK) || \
45 ((*(sent) & 3) == 0) || ((*(sent) & 3) == 3))
46#define lv1ent_zero(sent) (*(sent) == ZERO_LV2LINK)
47#define lv1ent_page_zero(sent) ((*(sent) & 3) == 1)
48#define lv1ent_page(sent) ((*(sent) != ZERO_LV2LINK) && \
49 ((*(sent) & 3) == 1))
KyongHo Cho2a965362012-05-12 05:56:09 +090050#define lv1ent_section(sent) ((*(sent) & 3) == 2)
51
52#define lv2ent_fault(pent) ((*(pent) & 3) == 0)
53#define lv2ent_small(pent) ((*(pent) & 2) == 2)
54#define lv2ent_large(pent) ((*(pent) & 3) == 1)
55
Cho KyongHod09d78f2014-05-12 11:44:58 +053056static u32 sysmmu_page_offset(sysmmu_iova_t iova, u32 size)
57{
58 return iova & (size - 1);
59}
KyongHo Cho2a965362012-05-12 05:56:09 +090060
Cho KyongHod09d78f2014-05-12 11:44:58 +053061#define section_phys(sent) (*(sent) & SECT_MASK)
62#define section_offs(iova) sysmmu_page_offset((iova), SECT_SIZE)
63#define lpage_phys(pent) (*(pent) & LPAGE_MASK)
64#define lpage_offs(iova) sysmmu_page_offset((iova), LPAGE_SIZE)
65#define spage_phys(pent) (*(pent) & SPAGE_MASK)
66#define spage_offs(iova) sysmmu_page_offset((iova), SPAGE_SIZE)
KyongHo Cho2a965362012-05-12 05:56:09 +090067
68#define NUM_LV1ENTRIES 4096
Cho KyongHod09d78f2014-05-12 11:44:58 +053069#define NUM_LV2ENTRIES (SECT_SIZE / SPAGE_SIZE)
KyongHo Cho2a965362012-05-12 05:56:09 +090070
Cho KyongHod09d78f2014-05-12 11:44:58 +053071static u32 lv1ent_offset(sysmmu_iova_t iova)
72{
73 return iova >> SECT_ORDER;
74}
75
76static u32 lv2ent_offset(sysmmu_iova_t iova)
77{
78 return (iova >> SPAGE_ORDER) & (NUM_LV2ENTRIES - 1);
79}
80
81#define LV2TABLE_SIZE (NUM_LV2ENTRIES * sizeof(sysmmu_pte_t))
KyongHo Cho2a965362012-05-12 05:56:09 +090082
83#define SPAGES_PER_LPAGE (LPAGE_SIZE / SPAGE_SIZE)
84
85#define lv2table_base(sent) (*(sent) & 0xFFFFFC00)
86
87#define mk_lv1ent_sect(pa) ((pa) | 2)
88#define mk_lv1ent_page(pa) ((pa) | 1)
89#define mk_lv2ent_lpage(pa) ((pa) | 1)
90#define mk_lv2ent_spage(pa) ((pa) | 2)
91
92#define CTRL_ENABLE 0x5
93#define CTRL_BLOCK 0x7
94#define CTRL_DISABLE 0x0
95
Cho KyongHoeeb51842014-05-12 11:45:03 +053096#define CFG_LRU 0x1
97#define CFG_QOS(n) ((n & 0xF) << 7)
98#define CFG_MASK 0x0150FFFF /* Selecting bit 0-15, 20, 22 and 24 */
99#define CFG_ACGEN (1 << 24) /* System MMU 3.3 only */
100#define CFG_SYSSEL (1 << 22) /* System MMU 3.2 only */
101#define CFG_FLPDCACHE (1 << 20) /* System MMU 3.2+ only */
102
KyongHo Cho2a965362012-05-12 05:56:09 +0900103#define REG_MMU_CTRL 0x000
104#define REG_MMU_CFG 0x004
105#define REG_MMU_STATUS 0x008
106#define REG_MMU_FLUSH 0x00C
107#define REG_MMU_FLUSH_ENTRY 0x010
108#define REG_PT_BASE_ADDR 0x014
109#define REG_INT_STATUS 0x018
110#define REG_INT_CLEAR 0x01C
111
112#define REG_PAGE_FAULT_ADDR 0x024
113#define REG_AW_FAULT_ADDR 0x028
114#define REG_AR_FAULT_ADDR 0x02C
115#define REG_DEFAULT_SLAVE_ADDR 0x030
116
117#define REG_MMU_VERSION 0x034
118
Cho KyongHoeeb51842014-05-12 11:45:03 +0530119#define MMU_MAJ_VER(val) ((val) >> 7)
120#define MMU_MIN_VER(val) ((val) & 0x7F)
121#define MMU_RAW_VER(reg) (((reg) >> 21) & ((1 << 11) - 1)) /* 11 bits */
122
123#define MAKE_MMU_VER(maj, min) ((((maj) & 0xF) << 7) | ((min) & 0x7F))
124
KyongHo Cho2a965362012-05-12 05:56:09 +0900125#define REG_PB0_SADDR 0x04C
126#define REG_PB0_EADDR 0x050
127#define REG_PB1_SADDR 0x054
128#define REG_PB1_EADDR 0x058
129
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530130#define has_sysmmu(dev) (dev->archdata.iommu != NULL)
131
Cho KyongHo734c3c72014-05-12 11:44:48 +0530132static struct kmem_cache *lv2table_kmem_cache;
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530133static sysmmu_pte_t *zero_lv2_table;
134#define ZERO_LV2LINK mk_lv1ent_page(virt_to_phys(zero_lv2_table))
Cho KyongHo734c3c72014-05-12 11:44:48 +0530135
Cho KyongHod09d78f2014-05-12 11:44:58 +0530136static sysmmu_pte_t *section_entry(sysmmu_pte_t *pgtable, sysmmu_iova_t iova)
KyongHo Cho2a965362012-05-12 05:56:09 +0900137{
138 return pgtable + lv1ent_offset(iova);
139}
140
Cho KyongHod09d78f2014-05-12 11:44:58 +0530141static sysmmu_pte_t *page_entry(sysmmu_pte_t *sent, sysmmu_iova_t iova)
KyongHo Cho2a965362012-05-12 05:56:09 +0900142{
Cho KyongHod09d78f2014-05-12 11:44:58 +0530143 return (sysmmu_pte_t *)phys_to_virt(
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530144 lv2table_base(sent)) + lv2ent_offset(iova);
KyongHo Cho2a965362012-05-12 05:56:09 +0900145}
146
147enum exynos_sysmmu_inttype {
148 SYSMMU_PAGEFAULT,
149 SYSMMU_AR_MULTIHIT,
150 SYSMMU_AW_MULTIHIT,
151 SYSMMU_BUSERROR,
152 SYSMMU_AR_SECURITY,
153 SYSMMU_AR_ACCESS,
154 SYSMMU_AW_SECURITY,
155 SYSMMU_AW_PROTECTION, /* 7 */
156 SYSMMU_FAULT_UNKNOWN,
157 SYSMMU_FAULTS_NUM
158};
159
KyongHo Cho2a965362012-05-12 05:56:09 +0900160static unsigned short fault_reg_offset[SYSMMU_FAULTS_NUM] = {
161 REG_PAGE_FAULT_ADDR,
162 REG_AR_FAULT_ADDR,
163 REG_AW_FAULT_ADDR,
164 REG_DEFAULT_SLAVE_ADDR,
165 REG_AR_FAULT_ADDR,
166 REG_AR_FAULT_ADDR,
167 REG_AW_FAULT_ADDR,
168 REG_AW_FAULT_ADDR
169};
170
171static char *sysmmu_fault_name[SYSMMU_FAULTS_NUM] = {
172 "PAGE FAULT",
173 "AR MULTI-HIT FAULT",
174 "AW MULTI-HIT FAULT",
175 "BUS ERROR",
176 "AR SECURITY PROTECTION FAULT",
177 "AR ACCESS PROTECTION FAULT",
178 "AW SECURITY PROTECTION FAULT",
179 "AW ACCESS PROTECTION FAULT",
180 "UNKNOWN FAULT"
181};
182
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530183/* attached to dev.archdata.iommu of the master device */
184struct exynos_iommu_owner {
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530185 struct device *sysmmu;
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530186};
187
KyongHo Cho2a965362012-05-12 05:56:09 +0900188struct exynos_iommu_domain {
189 struct list_head clients; /* list of sysmmu_drvdata.node */
Cho KyongHod09d78f2014-05-12 11:44:58 +0530190 sysmmu_pte_t *pgtable; /* lv1 page table, 16KB */
KyongHo Cho2a965362012-05-12 05:56:09 +0900191 short *lv2entcnt; /* free lv2 entry counter for each section */
192 spinlock_t lock; /* lock for this structure */
193 spinlock_t pgtablelock; /* lock for modifying page table @ pgtable */
Joerg Roedele1fd1ea2015-03-26 13:43:11 +0100194 struct iommu_domain domain; /* generic domain data structure */
KyongHo Cho2a965362012-05-12 05:56:09 +0900195};
196
197struct sysmmu_drvdata {
KyongHo Cho2a965362012-05-12 05:56:09 +0900198 struct device *sysmmu; /* System MMU's device descriptor */
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530199 struct device *master; /* Owner of system MMU */
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530200 void __iomem *sfrbase;
201 struct clk *clk;
Cho KyongHo70605872014-05-12 11:44:55 +0530202 struct clk *clk_master;
KyongHo Cho2a965362012-05-12 05:56:09 +0900203 int activations;
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530204 spinlock_t lock;
Marek Szyprowskia9133b992015-05-19 15:20:29 +0200205 struct exynos_iommu_domain *domain;
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200206 struct list_head domain_node;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530207 phys_addr_t pgtable;
Marek Szyprowski512bd0c2015-05-19 15:20:24 +0200208 unsigned int version;
KyongHo Cho2a965362012-05-12 05:56:09 +0900209};
210
Joerg Roedele1fd1ea2015-03-26 13:43:11 +0100211static struct exynos_iommu_domain *to_exynos_domain(struct iommu_domain *dom)
212{
213 return container_of(dom, struct exynos_iommu_domain, domain);
214}
215
KyongHo Cho2a965362012-05-12 05:56:09 +0900216static bool set_sysmmu_active(struct sysmmu_drvdata *data)
217{
218 /* return true if the System MMU was not active previously
219 and it needs to be initialized */
220 return ++data->activations == 1;
221}
222
223static bool set_sysmmu_inactive(struct sysmmu_drvdata *data)
224{
225 /* return true if the System MMU is needed to be disabled */
226 BUG_ON(data->activations < 1);
227 return --data->activations == 0;
228}
229
230static bool is_sysmmu_active(struct sysmmu_drvdata *data)
231{
232 return data->activations > 0;
233}
234
235static void sysmmu_unblock(void __iomem *sfrbase)
236{
237 __raw_writel(CTRL_ENABLE, sfrbase + REG_MMU_CTRL);
238}
239
240static bool sysmmu_block(void __iomem *sfrbase)
241{
242 int i = 120;
243
244 __raw_writel(CTRL_BLOCK, sfrbase + REG_MMU_CTRL);
245 while ((i > 0) && !(__raw_readl(sfrbase + REG_MMU_STATUS) & 1))
246 --i;
247
248 if (!(__raw_readl(sfrbase + REG_MMU_STATUS) & 1)) {
249 sysmmu_unblock(sfrbase);
250 return false;
251 }
252
253 return true;
254}
255
256static void __sysmmu_tlb_invalidate(void __iomem *sfrbase)
257{
258 __raw_writel(0x1, sfrbase + REG_MMU_FLUSH);
259}
260
261static void __sysmmu_tlb_invalidate_entry(void __iomem *sfrbase,
Cho KyongHod09d78f2014-05-12 11:44:58 +0530262 sysmmu_iova_t iova, unsigned int num_inv)
KyongHo Cho2a965362012-05-12 05:56:09 +0900263{
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530264 unsigned int i;
Sachin Kamat365409d2014-05-22 09:50:56 +0530265
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530266 for (i = 0; i < num_inv; i++) {
267 __raw_writel((iova & SPAGE_MASK) | 1,
268 sfrbase + REG_MMU_FLUSH_ENTRY);
269 iova += SPAGE_SIZE;
270 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900271}
272
273static void __sysmmu_set_ptbase(void __iomem *sfrbase,
Cho KyongHod09d78f2014-05-12 11:44:58 +0530274 phys_addr_t pgd)
KyongHo Cho2a965362012-05-12 05:56:09 +0900275{
KyongHo Cho2a965362012-05-12 05:56:09 +0900276 __raw_writel(pgd, sfrbase + REG_PT_BASE_ADDR);
277
278 __sysmmu_tlb_invalidate(sfrbase);
279}
280
Cho KyongHo1fab7fa2014-05-12 11:44:56 +0530281static void show_fault_information(const char *name,
282 enum exynos_sysmmu_inttype itype,
Cho KyongHod09d78f2014-05-12 11:44:58 +0530283 phys_addr_t pgtable_base, sysmmu_iova_t fault_addr)
KyongHo Cho2a965362012-05-12 05:56:09 +0900284{
Cho KyongHod09d78f2014-05-12 11:44:58 +0530285 sysmmu_pte_t *ent;
KyongHo Cho2a965362012-05-12 05:56:09 +0900286
287 if ((itype >= SYSMMU_FAULTS_NUM) || (itype < SYSMMU_PAGEFAULT))
288 itype = SYSMMU_FAULT_UNKNOWN;
289
Cho KyongHod09d78f2014-05-12 11:44:58 +0530290 pr_err("%s occurred at %#x by %s(Page table base: %pa)\n",
Cho KyongHo1fab7fa2014-05-12 11:44:56 +0530291 sysmmu_fault_name[itype], fault_addr, name, &pgtable_base);
KyongHo Cho2a965362012-05-12 05:56:09 +0900292
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530293 ent = section_entry(phys_to_virt(pgtable_base), fault_addr);
Cho KyongHod09d78f2014-05-12 11:44:58 +0530294 pr_err("\tLv1 entry: %#x\n", *ent);
KyongHo Cho2a965362012-05-12 05:56:09 +0900295
296 if (lv1ent_page(ent)) {
297 ent = page_entry(ent, fault_addr);
Cho KyongHod09d78f2014-05-12 11:44:58 +0530298 pr_err("\t Lv2 entry: %#x\n", *ent);
KyongHo Cho2a965362012-05-12 05:56:09 +0900299 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900300}
301
302static irqreturn_t exynos_sysmmu_irq(int irq, void *dev_id)
303{
Sachin Kamatf171aba2014-08-04 10:06:28 +0530304 /* SYSMMU is in blocked state when interrupt occurred. */
KyongHo Cho2a965362012-05-12 05:56:09 +0900305 struct sysmmu_drvdata *data = dev_id;
KyongHo Cho2a965362012-05-12 05:56:09 +0900306 enum exynos_sysmmu_inttype itype;
Cho KyongHod09d78f2014-05-12 11:44:58 +0530307 sysmmu_iova_t addr = -1;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530308 int ret = -ENOSYS;
KyongHo Cho2a965362012-05-12 05:56:09 +0900309
KyongHo Cho2a965362012-05-12 05:56:09 +0900310 WARN_ON(!is_sysmmu_active(data));
311
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530312 spin_lock(&data->lock);
313
Cho KyongHo70605872014-05-12 11:44:55 +0530314 if (!IS_ERR(data->clk_master))
315 clk_enable(data->clk_master);
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530316
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530317 itype = (enum exynos_sysmmu_inttype)
318 __ffs(__raw_readl(data->sfrbase + REG_INT_STATUS));
319 if (WARN_ON(!((itype >= 0) && (itype < SYSMMU_FAULT_UNKNOWN))))
KyongHo Cho2a965362012-05-12 05:56:09 +0900320 itype = SYSMMU_FAULT_UNKNOWN;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530321 else
322 addr = __raw_readl(data->sfrbase + fault_reg_offset[itype]);
KyongHo Cho2a965362012-05-12 05:56:09 +0900323
Cho KyongHo1fab7fa2014-05-12 11:44:56 +0530324 if (itype == SYSMMU_FAULT_UNKNOWN) {
325 pr_err("%s: Fault is not occurred by System MMU '%s'!\n",
326 __func__, dev_name(data->sysmmu));
327 pr_err("%s: Please check if IRQ is correctly configured.\n",
328 __func__);
329 BUG();
330 } else {
Cho KyongHod09d78f2014-05-12 11:44:58 +0530331 unsigned int base =
Cho KyongHo1fab7fa2014-05-12 11:44:56 +0530332 __raw_readl(data->sfrbase + REG_PT_BASE_ADDR);
333 show_fault_information(dev_name(data->sysmmu),
334 itype, base, addr);
335 if (data->domain)
Marek Szyprowskia9133b992015-05-19 15:20:29 +0200336 ret = report_iommu_fault(&data->domain->domain,
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530337 data->master, addr, itype);
KyongHo Cho2a965362012-05-12 05:56:09 +0900338 }
339
Cho KyongHo1fab7fa2014-05-12 11:44:56 +0530340 /* fault is not recovered by fault handler */
341 BUG_ON(ret != 0);
KyongHo Cho2a965362012-05-12 05:56:09 +0900342
Cho KyongHo1fab7fa2014-05-12 11:44:56 +0530343 __raw_writel(1 << itype, data->sfrbase + REG_INT_CLEAR);
344
345 sysmmu_unblock(data->sfrbase);
KyongHo Cho2a965362012-05-12 05:56:09 +0900346
Cho KyongHo70605872014-05-12 11:44:55 +0530347 if (!IS_ERR(data->clk_master))
348 clk_disable(data->clk_master);
349
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530350 spin_unlock(&data->lock);
KyongHo Cho2a965362012-05-12 05:56:09 +0900351
352 return IRQ_HANDLED;
353}
354
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530355static void __sysmmu_disable_nocount(struct sysmmu_drvdata *data)
KyongHo Cho2a965362012-05-12 05:56:09 +0900356{
Cho KyongHo70605872014-05-12 11:44:55 +0530357 if (!IS_ERR(data->clk_master))
358 clk_enable(data->clk_master);
359
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530360 __raw_writel(CTRL_DISABLE, data->sfrbase + REG_MMU_CTRL);
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530361 __raw_writel(0, data->sfrbase + REG_MMU_CFG);
KyongHo Cho2a965362012-05-12 05:56:09 +0900362
Cho KyongHo46c16d12014-05-12 11:44:54 +0530363 clk_disable(data->clk);
Cho KyongHo70605872014-05-12 11:44:55 +0530364 if (!IS_ERR(data->clk_master))
365 clk_disable(data->clk_master);
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530366}
KyongHo Cho2a965362012-05-12 05:56:09 +0900367
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530368static bool __sysmmu_disable(struct sysmmu_drvdata *data)
369{
370 bool disabled;
371 unsigned long flags;
372
373 spin_lock_irqsave(&data->lock, flags);
374
375 disabled = set_sysmmu_inactive(data);
376
377 if (disabled) {
378 data->pgtable = 0;
379 data->domain = NULL;
380
381 __sysmmu_disable_nocount(data);
382
383 dev_dbg(data->sysmmu, "Disabled\n");
384 } else {
385 dev_dbg(data->sysmmu, "%d times left to disable\n",
386 data->activations);
387 }
388
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530389 spin_unlock_irqrestore(&data->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900390
KyongHo Cho2a965362012-05-12 05:56:09 +0900391 return disabled;
392}
393
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530394static void __sysmmu_init_config(struct sysmmu_drvdata *data)
395{
Cho KyongHoeeb51842014-05-12 11:45:03 +0530396 unsigned int cfg = CFG_LRU | CFG_QOS(15);
397 unsigned int ver;
398
Marek Szyprowski512bd0c2015-05-19 15:20:24 +0200399 ver = MMU_RAW_VER(__raw_readl(data->sfrbase + REG_MMU_VERSION));
Cho KyongHoeeb51842014-05-12 11:45:03 +0530400 if (MMU_MAJ_VER(ver) == 3) {
401 if (MMU_MIN_VER(ver) >= 2) {
402 cfg |= CFG_FLPDCACHE;
403 if (MMU_MIN_VER(ver) == 3) {
404 cfg |= CFG_ACGEN;
405 cfg &= ~CFG_LRU;
406 } else {
407 cfg |= CFG_SYSSEL;
408 }
409 }
410 }
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530411
412 __raw_writel(cfg, data->sfrbase + REG_MMU_CFG);
Marek Szyprowski512bd0c2015-05-19 15:20:24 +0200413 data->version = ver;
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530414}
415
416static void __sysmmu_enable_nocount(struct sysmmu_drvdata *data)
417{
418 if (!IS_ERR(data->clk_master))
419 clk_enable(data->clk_master);
420 clk_enable(data->clk);
421
422 __raw_writel(CTRL_BLOCK, data->sfrbase + REG_MMU_CTRL);
423
424 __sysmmu_init_config(data);
425
426 __sysmmu_set_ptbase(data->sfrbase, data->pgtable);
427
428 __raw_writel(CTRL_ENABLE, data->sfrbase + REG_MMU_CTRL);
429
430 if (!IS_ERR(data->clk_master))
431 clk_disable(data->clk_master);
432}
433
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200434static int __sysmmu_enable(struct sysmmu_drvdata *data, phys_addr_t pgtable,
Marek Szyprowskia9133b992015-05-19 15:20:29 +0200435 struct exynos_iommu_domain *domain)
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530436{
437 int ret = 0;
438 unsigned long flags;
439
440 spin_lock_irqsave(&data->lock, flags);
441 if (set_sysmmu_active(data)) {
442 data->pgtable = pgtable;
Marek Szyprowskia9133b992015-05-19 15:20:29 +0200443 data->domain = domain;
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530444
445 __sysmmu_enable_nocount(data);
446
447 dev_dbg(data->sysmmu, "Enabled\n");
448 } else {
449 ret = (pgtable == data->pgtable) ? 1 : -EBUSY;
450
451 dev_dbg(data->sysmmu, "already enabled\n");
452 }
453
454 if (WARN_ON(ret < 0))
455 set_sysmmu_inactive(data); /* decrement count */
456
457 spin_unlock_irqrestore(&data->lock, flags);
458
459 return ret;
460}
461
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530462static void __sysmmu_tlb_invalidate_flpdcache(struct sysmmu_drvdata *data,
463 sysmmu_iova_t iova)
464{
Marek Szyprowski512bd0c2015-05-19 15:20:24 +0200465 if (data->version == MAKE_MMU_VER(3, 3))
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530466 __raw_writel(iova | 0x1, data->sfrbase + REG_MMU_FLUSH_ENTRY);
467}
468
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200469static void sysmmu_tlb_invalidate_flpdcache(struct sysmmu_drvdata *data,
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530470 sysmmu_iova_t iova)
471{
472 unsigned long flags;
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530473
474 if (!IS_ERR(data->clk_master))
475 clk_enable(data->clk_master);
476
477 spin_lock_irqsave(&data->lock, flags);
478 if (is_sysmmu_active(data))
479 __sysmmu_tlb_invalidate_flpdcache(data, iova);
480 spin_unlock_irqrestore(&data->lock, flags);
481
482 if (!IS_ERR(data->clk_master))
483 clk_disable(data->clk_master);
484}
485
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200486static void sysmmu_tlb_invalidate_entry(struct sysmmu_drvdata *data,
487 sysmmu_iova_t iova, size_t size)
KyongHo Cho2a965362012-05-12 05:56:09 +0900488{
489 unsigned long flags;
KyongHo Cho2a965362012-05-12 05:56:09 +0900490
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530491 spin_lock_irqsave(&data->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900492 if (is_sysmmu_active(data)) {
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530493 unsigned int num_inv = 1;
Cho KyongHo70605872014-05-12 11:44:55 +0530494
495 if (!IS_ERR(data->clk_master))
496 clk_enable(data->clk_master);
497
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530498 /*
499 * L2TLB invalidation required
500 * 4KB page: 1 invalidation
Sachin Kamatf171aba2014-08-04 10:06:28 +0530501 * 64KB page: 16 invalidations
502 * 1MB page: 64 invalidations
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530503 * because it is set-associative TLB
504 * with 8-way and 64 sets.
505 * 1MB page can be cached in one of all sets.
506 * 64KB page can be one of 16 consecutive sets.
507 */
Marek Szyprowski512bd0c2015-05-19 15:20:24 +0200508 if (MMU_MAJ_VER(data->version) == 2)
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530509 num_inv = min_t(unsigned int, size / PAGE_SIZE, 64);
510
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530511 if (sysmmu_block(data->sfrbase)) {
512 __sysmmu_tlb_invalidate_entry(
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530513 data->sfrbase, iova, num_inv);
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530514 sysmmu_unblock(data->sfrbase);
KyongHo Cho2a965362012-05-12 05:56:09 +0900515 }
Cho KyongHo70605872014-05-12 11:44:55 +0530516 if (!IS_ERR(data->clk_master))
517 clk_disable(data->clk_master);
KyongHo Cho2a965362012-05-12 05:56:09 +0900518 } else {
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200519 dev_dbg(data->master,
520 "disabled. Skipping TLB invalidation @ %#x\n", iova);
KyongHo Cho2a965362012-05-12 05:56:09 +0900521 }
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530522 spin_unlock_irqrestore(&data->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900523}
524
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530525static int __init exynos_sysmmu_probe(struct platform_device *pdev)
KyongHo Cho2a965362012-05-12 05:56:09 +0900526{
Cho KyongHo46c16d12014-05-12 11:44:54 +0530527 int irq, ret;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530528 struct device *dev = &pdev->dev;
KyongHo Cho2a965362012-05-12 05:56:09 +0900529 struct sysmmu_drvdata *data;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530530 struct resource *res;
KyongHo Cho2a965362012-05-12 05:56:09 +0900531
Cho KyongHo46c16d12014-05-12 11:44:54 +0530532 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
533 if (!data)
534 return -ENOMEM;
KyongHo Cho2a965362012-05-12 05:56:09 +0900535
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530536 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Cho KyongHo46c16d12014-05-12 11:44:54 +0530537 data->sfrbase = devm_ioremap_resource(dev, res);
538 if (IS_ERR(data->sfrbase))
539 return PTR_ERR(data->sfrbase);
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530540
Cho KyongHo46c16d12014-05-12 11:44:54 +0530541 irq = platform_get_irq(pdev, 0);
542 if (irq <= 0) {
Cho KyongHo0bf4e542014-05-12 11:45:00 +0530543 dev_err(dev, "Unable to find IRQ resource\n");
Cho KyongHo46c16d12014-05-12 11:44:54 +0530544 return irq;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530545 }
546
Cho KyongHo46c16d12014-05-12 11:44:54 +0530547 ret = devm_request_irq(dev, irq, exynos_sysmmu_irq, 0,
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530548 dev_name(dev), data);
KyongHo Cho2a965362012-05-12 05:56:09 +0900549 if (ret) {
Cho KyongHo46c16d12014-05-12 11:44:54 +0530550 dev_err(dev, "Unabled to register handler of irq %d\n", irq);
551 return ret;
KyongHo Cho2a965362012-05-12 05:56:09 +0900552 }
553
Cho KyongHo46c16d12014-05-12 11:44:54 +0530554 data->clk = devm_clk_get(dev, "sysmmu");
555 if (IS_ERR(data->clk)) {
556 dev_err(dev, "Failed to get clock!\n");
557 return PTR_ERR(data->clk);
558 } else {
559 ret = clk_prepare(data->clk);
560 if (ret) {
561 dev_err(dev, "Failed to prepare clk\n");
562 return ret;
563 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900564 }
565
Cho KyongHo70605872014-05-12 11:44:55 +0530566 data->clk_master = devm_clk_get(dev, "master");
567 if (!IS_ERR(data->clk_master)) {
568 ret = clk_prepare(data->clk_master);
569 if (ret) {
570 clk_unprepare(data->clk);
571 dev_err(dev, "Failed to prepare master's clk\n");
572 return ret;
573 }
574 }
575
KyongHo Cho2a965362012-05-12 05:56:09 +0900576 data->sysmmu = dev;
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530577 spin_lock_init(&data->lock);
KyongHo Cho2a965362012-05-12 05:56:09 +0900578
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530579 platform_set_drvdata(pdev, data);
580
Cho KyongHof4723ec2014-05-12 11:44:52 +0530581 pm_runtime_enable(dev);
KyongHo Cho2a965362012-05-12 05:56:09 +0900582
KyongHo Cho2a965362012-05-12 05:56:09 +0900583 return 0;
KyongHo Cho2a965362012-05-12 05:56:09 +0900584}
585
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530586static const struct of_device_id sysmmu_of_match[] __initconst = {
587 { .compatible = "samsung,exynos-sysmmu", },
588 { },
589};
590
591static struct platform_driver exynos_sysmmu_driver __refdata = {
592 .probe = exynos_sysmmu_probe,
593 .driver = {
KyongHo Cho2a965362012-05-12 05:56:09 +0900594 .name = "exynos-sysmmu",
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530595 .of_match_table = sysmmu_of_match,
KyongHo Cho2a965362012-05-12 05:56:09 +0900596 }
597};
598
599static inline void pgtable_flush(void *vastart, void *vaend)
600{
601 dmac_flush_range(vastart, vaend);
602 outer_flush_range(virt_to_phys(vastart),
603 virt_to_phys(vaend));
604}
605
Joerg Roedele1fd1ea2015-03-26 13:43:11 +0100606static struct iommu_domain *exynos_iommu_domain_alloc(unsigned type)
KyongHo Cho2a965362012-05-12 05:56:09 +0900607{
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200608 struct exynos_iommu_domain *domain;
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530609 int i;
KyongHo Cho2a965362012-05-12 05:56:09 +0900610
Joerg Roedele1fd1ea2015-03-26 13:43:11 +0100611 if (type != IOMMU_DOMAIN_UNMANAGED)
612 return NULL;
KyongHo Cho2a965362012-05-12 05:56:09 +0900613
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200614 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
615 if (!domain)
Joerg Roedele1fd1ea2015-03-26 13:43:11 +0100616 return NULL;
617
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200618 domain->pgtable = (sysmmu_pte_t *)__get_free_pages(GFP_KERNEL, 2);
619 if (!domain->pgtable)
KyongHo Cho2a965362012-05-12 05:56:09 +0900620 goto err_pgtable;
621
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200622 domain->lv2entcnt = (short *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1);
623 if (!domain->lv2entcnt)
KyongHo Cho2a965362012-05-12 05:56:09 +0900624 goto err_counter;
625
Sachin Kamatf171aba2014-08-04 10:06:28 +0530626 /* Workaround for System MMU v3.3 to prevent caching 1MiB mapping */
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530627 for (i = 0; i < NUM_LV1ENTRIES; i += 8) {
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200628 domain->pgtable[i + 0] = ZERO_LV2LINK;
629 domain->pgtable[i + 1] = ZERO_LV2LINK;
630 domain->pgtable[i + 2] = ZERO_LV2LINK;
631 domain->pgtable[i + 3] = ZERO_LV2LINK;
632 domain->pgtable[i + 4] = ZERO_LV2LINK;
633 domain->pgtable[i + 5] = ZERO_LV2LINK;
634 domain->pgtable[i + 6] = ZERO_LV2LINK;
635 domain->pgtable[i + 7] = ZERO_LV2LINK;
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530636 }
637
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200638 pgtable_flush(domain->pgtable, domain->pgtable + NUM_LV1ENTRIES);
KyongHo Cho2a965362012-05-12 05:56:09 +0900639
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200640 spin_lock_init(&domain->lock);
641 spin_lock_init(&domain->pgtablelock);
642 INIT_LIST_HEAD(&domain->clients);
KyongHo Cho2a965362012-05-12 05:56:09 +0900643
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200644 domain->domain.geometry.aperture_start = 0;
645 domain->domain.geometry.aperture_end = ~0UL;
646 domain->domain.geometry.force_aperture = true;
Joerg Roedel3177bb72012-07-11 12:41:10 +0200647
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200648 return &domain->domain;
KyongHo Cho2a965362012-05-12 05:56:09 +0900649
650err_counter:
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200651 free_pages((unsigned long)domain->pgtable, 2);
KyongHo Cho2a965362012-05-12 05:56:09 +0900652err_pgtable:
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200653 kfree(domain);
Joerg Roedele1fd1ea2015-03-26 13:43:11 +0100654 return NULL;
KyongHo Cho2a965362012-05-12 05:56:09 +0900655}
656
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200657static void exynos_iommu_domain_free(struct iommu_domain *iommu_domain)
KyongHo Cho2a965362012-05-12 05:56:09 +0900658{
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200659 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200660 struct sysmmu_drvdata *data, *next;
KyongHo Cho2a965362012-05-12 05:56:09 +0900661 unsigned long flags;
662 int i;
663
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200664 WARN_ON(!list_empty(&domain->clients));
KyongHo Cho2a965362012-05-12 05:56:09 +0900665
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200666 spin_lock_irqsave(&domain->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900667
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200668 list_for_each_entry_safe(data, next, &domain->clients, domain_node) {
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200669 if (__sysmmu_disable(data))
670 data->master = NULL;
671 list_del_init(&data->domain_node);
KyongHo Cho2a965362012-05-12 05:56:09 +0900672 }
673
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200674 spin_unlock_irqrestore(&domain->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900675
676 for (i = 0; i < NUM_LV1ENTRIES; i++)
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200677 if (lv1ent_page(domain->pgtable + i))
Cho KyongHo734c3c72014-05-12 11:44:48 +0530678 kmem_cache_free(lv2table_kmem_cache,
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200679 phys_to_virt(lv2table_base(domain->pgtable + i)));
KyongHo Cho2a965362012-05-12 05:56:09 +0900680
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200681 free_pages((unsigned long)domain->pgtable, 2);
682 free_pages((unsigned long)domain->lv2entcnt, 1);
683 kfree(domain);
KyongHo Cho2a965362012-05-12 05:56:09 +0900684}
685
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200686static int exynos_iommu_attach_device(struct iommu_domain *iommu_domain,
KyongHo Cho2a965362012-05-12 05:56:09 +0900687 struct device *dev)
688{
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530689 struct exynos_iommu_owner *owner = dev->archdata.iommu;
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200690 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200691 struct sysmmu_drvdata *data;
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200692 phys_addr_t pagetable = virt_to_phys(domain->pgtable);
KyongHo Cho2a965362012-05-12 05:56:09 +0900693 unsigned long flags;
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200694 int ret = -ENODEV;
KyongHo Cho2a965362012-05-12 05:56:09 +0900695
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200696 if (!has_sysmmu(dev))
697 return -ENODEV;
KyongHo Cho2a965362012-05-12 05:56:09 +0900698
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200699 data = dev_get_drvdata(owner->sysmmu);
700 if (data) {
Marek Szyprowskia9133b992015-05-19 15:20:29 +0200701 ret = __sysmmu_enable(data, pagetable, domain);
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200702 if (ret >= 0) {
703 data->master = dev;
KyongHo Cho2a965362012-05-12 05:56:09 +0900704
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200705 spin_lock_irqsave(&domain->lock, flags);
706 list_add_tail(&data->domain_node, &domain->clients);
707 spin_unlock_irqrestore(&domain->lock, flags);
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200708 }
709 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900710
711 if (ret < 0) {
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530712 dev_err(dev, "%s: Failed to attach IOMMU with pgtable %pa\n",
713 __func__, &pagetable);
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530714 return ret;
KyongHo Cho2a965362012-05-12 05:56:09 +0900715 }
716
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530717 dev_dbg(dev, "%s: Attached IOMMU with pgtable %pa %s\n",
718 __func__, &pagetable, (ret == 0) ? "" : ", again");
719
KyongHo Cho2a965362012-05-12 05:56:09 +0900720 return ret;
721}
722
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200723static void exynos_iommu_detach_device(struct iommu_domain *iommu_domain,
KyongHo Cho2a965362012-05-12 05:56:09 +0900724 struct device *dev)
725{
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200726 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
727 phys_addr_t pagetable = virt_to_phys(domain->pgtable);
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200728 struct sysmmu_drvdata *data;
KyongHo Cho2a965362012-05-12 05:56:09 +0900729 unsigned long flags;
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200730 bool found = false;
731
732 if (!has_sysmmu(dev))
733 return;
KyongHo Cho2a965362012-05-12 05:56:09 +0900734
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200735 spin_lock_irqsave(&domain->lock, flags);
736 list_for_each_entry(data, &domain->clients, domain_node) {
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200737 if (data->master == dev) {
738 if (__sysmmu_disable(data)) {
739 data->master = NULL;
740 list_del_init(&data->domain_node);
741 }
742 found = true;
KyongHo Cho2a965362012-05-12 05:56:09 +0900743 break;
744 }
745 }
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200746 spin_unlock_irqrestore(&domain->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900747
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200748 if (found)
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530749 dev_dbg(dev, "%s: Detached IOMMU with pgtable %pa\n",
750 __func__, &pagetable);
751 else
752 dev_err(dev, "%s: No IOMMU is attached\n", __func__);
KyongHo Cho2a965362012-05-12 05:56:09 +0900753}
754
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200755static sysmmu_pte_t *alloc_lv2entry(struct exynos_iommu_domain *domain,
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530756 sysmmu_pte_t *sent, sysmmu_iova_t iova, short *pgcounter)
KyongHo Cho2a965362012-05-12 05:56:09 +0900757{
Cho KyongHo61128f02014-05-12 11:44:47 +0530758 if (lv1ent_section(sent)) {
Cho KyongHod09d78f2014-05-12 11:44:58 +0530759 WARN(1, "Trying mapping on %#08x mapped with 1MiB page", iova);
Cho KyongHo61128f02014-05-12 11:44:47 +0530760 return ERR_PTR(-EADDRINUSE);
761 }
762
KyongHo Cho2a965362012-05-12 05:56:09 +0900763 if (lv1ent_fault(sent)) {
Cho KyongHod09d78f2014-05-12 11:44:58 +0530764 sysmmu_pte_t *pent;
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530765 bool need_flush_flpd_cache = lv1ent_zero(sent);
KyongHo Cho2a965362012-05-12 05:56:09 +0900766
Cho KyongHo734c3c72014-05-12 11:44:48 +0530767 pent = kmem_cache_zalloc(lv2table_kmem_cache, GFP_ATOMIC);
Cho KyongHod09d78f2014-05-12 11:44:58 +0530768 BUG_ON((unsigned int)pent & (LV2TABLE_SIZE - 1));
KyongHo Cho2a965362012-05-12 05:56:09 +0900769 if (!pent)
Cho KyongHo61128f02014-05-12 11:44:47 +0530770 return ERR_PTR(-ENOMEM);
KyongHo Cho2a965362012-05-12 05:56:09 +0900771
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530772 *sent = mk_lv1ent_page(virt_to_phys(pent));
Colin Crossdc3814f2015-05-08 17:05:44 -0700773 kmemleak_ignore(pent);
KyongHo Cho2a965362012-05-12 05:56:09 +0900774 *pgcounter = NUM_LV2ENTRIES;
775 pgtable_flush(pent, pent + NUM_LV2ENTRIES);
776 pgtable_flush(sent, sent + 1);
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530777
778 /*
Sachin Kamatf171aba2014-08-04 10:06:28 +0530779 * If pre-fetched SLPD is a faulty SLPD in zero_l2_table,
780 * FLPD cache may cache the address of zero_l2_table. This
781 * function replaces the zero_l2_table with new L2 page table
782 * to write valid mappings.
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530783 * Accessing the valid area may cause page fault since FLPD
Sachin Kamatf171aba2014-08-04 10:06:28 +0530784 * cache may still cache zero_l2_table for the valid area
785 * instead of new L2 page table that has the mapping
786 * information of the valid area.
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530787 * Thus any replacement of zero_l2_table with other valid L2
788 * page table must involve FLPD cache invalidation for System
789 * MMU v3.3.
790 * FLPD cache invalidation is performed with TLB invalidation
791 * by VPN without blocking. It is safe to invalidate TLB without
792 * blocking because the target address of TLB invalidation is
793 * not currently mapped.
794 */
795 if (need_flush_flpd_cache) {
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200796 struct sysmmu_drvdata *data;
Sachin Kamat365409d2014-05-22 09:50:56 +0530797
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200798 spin_lock(&domain->lock);
799 list_for_each_entry(data, &domain->clients, domain_node)
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200800 sysmmu_tlb_invalidate_flpdcache(data, iova);
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200801 spin_unlock(&domain->lock);
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530802 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900803 }
804
805 return page_entry(sent, iova);
806}
807
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200808static int lv1set_section(struct exynos_iommu_domain *domain,
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530809 sysmmu_pte_t *sent, sysmmu_iova_t iova,
Cho KyongHo61128f02014-05-12 11:44:47 +0530810 phys_addr_t paddr, short *pgcnt)
KyongHo Cho2a965362012-05-12 05:56:09 +0900811{
Cho KyongHo61128f02014-05-12 11:44:47 +0530812 if (lv1ent_section(sent)) {
Cho KyongHod09d78f2014-05-12 11:44:58 +0530813 WARN(1, "Trying mapping on 1MiB@%#08x that is mapped",
Cho KyongHo61128f02014-05-12 11:44:47 +0530814 iova);
KyongHo Cho2a965362012-05-12 05:56:09 +0900815 return -EADDRINUSE;
Cho KyongHo61128f02014-05-12 11:44:47 +0530816 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900817
818 if (lv1ent_page(sent)) {
Cho KyongHo61128f02014-05-12 11:44:47 +0530819 if (*pgcnt != NUM_LV2ENTRIES) {
Cho KyongHod09d78f2014-05-12 11:44:58 +0530820 WARN(1, "Trying mapping on 1MiB@%#08x that is mapped",
Cho KyongHo61128f02014-05-12 11:44:47 +0530821 iova);
KyongHo Cho2a965362012-05-12 05:56:09 +0900822 return -EADDRINUSE;
Cho KyongHo61128f02014-05-12 11:44:47 +0530823 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900824
Cho KyongHo734c3c72014-05-12 11:44:48 +0530825 kmem_cache_free(lv2table_kmem_cache, page_entry(sent, 0));
KyongHo Cho2a965362012-05-12 05:56:09 +0900826 *pgcnt = 0;
827 }
828
829 *sent = mk_lv1ent_sect(paddr);
830
831 pgtable_flush(sent, sent + 1);
832
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200833 spin_lock(&domain->lock);
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530834 if (lv1ent_page_zero(sent)) {
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200835 struct sysmmu_drvdata *data;
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530836 /*
837 * Flushing FLPD cache in System MMU v3.3 that may cache a FLPD
838 * entry by speculative prefetch of SLPD which has no mapping.
839 */
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200840 list_for_each_entry(data, &domain->clients, domain_node)
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200841 sysmmu_tlb_invalidate_flpdcache(data, iova);
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530842 }
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200843 spin_unlock(&domain->lock);
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530844
KyongHo Cho2a965362012-05-12 05:56:09 +0900845 return 0;
846}
847
Cho KyongHod09d78f2014-05-12 11:44:58 +0530848static int lv2set_page(sysmmu_pte_t *pent, phys_addr_t paddr, size_t size,
KyongHo Cho2a965362012-05-12 05:56:09 +0900849 short *pgcnt)
850{
851 if (size == SPAGE_SIZE) {
Cho KyongHo0bf4e542014-05-12 11:45:00 +0530852 if (WARN_ON(!lv2ent_fault(pent)))
KyongHo Cho2a965362012-05-12 05:56:09 +0900853 return -EADDRINUSE;
854
855 *pent = mk_lv2ent_spage(paddr);
856 pgtable_flush(pent, pent + 1);
857 *pgcnt -= 1;
858 } else { /* size == LPAGE_SIZE */
859 int i;
Sachin Kamat365409d2014-05-22 09:50:56 +0530860
KyongHo Cho2a965362012-05-12 05:56:09 +0900861 for (i = 0; i < SPAGES_PER_LPAGE; i++, pent++) {
Cho KyongHo0bf4e542014-05-12 11:45:00 +0530862 if (WARN_ON(!lv2ent_fault(pent))) {
Cho KyongHo61128f02014-05-12 11:44:47 +0530863 if (i > 0)
864 memset(pent - i, 0, sizeof(*pent) * i);
KyongHo Cho2a965362012-05-12 05:56:09 +0900865 return -EADDRINUSE;
866 }
867
868 *pent = mk_lv2ent_lpage(paddr);
869 }
870 pgtable_flush(pent - SPAGES_PER_LPAGE, pent);
871 *pgcnt -= SPAGES_PER_LPAGE;
872 }
873
874 return 0;
875}
876
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530877/*
878 * *CAUTION* to the I/O virtual memory managers that support exynos-iommu:
879 *
Sachin Kamatf171aba2014-08-04 10:06:28 +0530880 * System MMU v3.x has advanced logic to improve address translation
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530881 * performance with caching more page table entries by a page table walk.
Sachin Kamatf171aba2014-08-04 10:06:28 +0530882 * However, the logic has a bug that while caching faulty page table entries,
883 * System MMU reports page fault if the cached fault entry is hit even though
884 * the fault entry is updated to a valid entry after the entry is cached.
885 * To prevent caching faulty page table entries which may be updated to valid
886 * entries later, the virtual memory manager should care about the workaround
887 * for the problem. The following describes the workaround.
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530888 *
889 * Any two consecutive I/O virtual address regions must have a hole of 128KiB
Sachin Kamatf171aba2014-08-04 10:06:28 +0530890 * at maximum to prevent misbehavior of System MMU 3.x (workaround for h/w bug).
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530891 *
Sachin Kamatf171aba2014-08-04 10:06:28 +0530892 * Precisely, any start address of I/O virtual region must be aligned with
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530893 * the following sizes for System MMU v3.1 and v3.2.
894 * System MMU v3.1: 128KiB
895 * System MMU v3.2: 256KiB
896 *
897 * Because System MMU v3.3 caches page table entries more aggressively, it needs
Sachin Kamatf171aba2014-08-04 10:06:28 +0530898 * more workarounds.
899 * - Any two consecutive I/O virtual regions must have a hole of size larger
900 * than or equal to 128KiB.
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530901 * - Start address of an I/O virtual region must be aligned by 128KiB.
902 */
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200903static int exynos_iommu_map(struct iommu_domain *iommu_domain,
904 unsigned long l_iova, phys_addr_t paddr, size_t size,
905 int prot)
KyongHo Cho2a965362012-05-12 05:56:09 +0900906{
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200907 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
Cho KyongHod09d78f2014-05-12 11:44:58 +0530908 sysmmu_pte_t *entry;
909 sysmmu_iova_t iova = (sysmmu_iova_t)l_iova;
KyongHo Cho2a965362012-05-12 05:56:09 +0900910 unsigned long flags;
911 int ret = -ENOMEM;
912
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200913 BUG_ON(domain->pgtable == NULL);
KyongHo Cho2a965362012-05-12 05:56:09 +0900914
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200915 spin_lock_irqsave(&domain->pgtablelock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900916
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200917 entry = section_entry(domain->pgtable, iova);
KyongHo Cho2a965362012-05-12 05:56:09 +0900918
919 if (size == SECT_SIZE) {
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200920 ret = lv1set_section(domain, entry, iova, paddr,
921 &domain->lv2entcnt[lv1ent_offset(iova)]);
KyongHo Cho2a965362012-05-12 05:56:09 +0900922 } else {
Cho KyongHod09d78f2014-05-12 11:44:58 +0530923 sysmmu_pte_t *pent;
KyongHo Cho2a965362012-05-12 05:56:09 +0900924
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200925 pent = alloc_lv2entry(domain, entry, iova,
926 &domain->lv2entcnt[lv1ent_offset(iova)]);
KyongHo Cho2a965362012-05-12 05:56:09 +0900927
Cho KyongHo61128f02014-05-12 11:44:47 +0530928 if (IS_ERR(pent))
929 ret = PTR_ERR(pent);
KyongHo Cho2a965362012-05-12 05:56:09 +0900930 else
931 ret = lv2set_page(pent, paddr, size,
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200932 &domain->lv2entcnt[lv1ent_offset(iova)]);
KyongHo Cho2a965362012-05-12 05:56:09 +0900933 }
934
Cho KyongHo61128f02014-05-12 11:44:47 +0530935 if (ret)
Cho KyongHo0bf4e542014-05-12 11:45:00 +0530936 pr_err("%s: Failed(%d) to map %#zx bytes @ %#x\n",
937 __func__, ret, size, iova);
KyongHo Cho2a965362012-05-12 05:56:09 +0900938
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200939 spin_unlock_irqrestore(&domain->pgtablelock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900940
941 return ret;
942}
943
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200944static void exynos_iommu_tlb_invalidate_entry(struct exynos_iommu_domain *domain,
945 sysmmu_iova_t iova, size_t size)
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530946{
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200947 struct sysmmu_drvdata *data;
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530948 unsigned long flags;
949
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200950 spin_lock_irqsave(&domain->lock, flags);
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530951
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200952 list_for_each_entry(data, &domain->clients, domain_node)
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200953 sysmmu_tlb_invalidate_entry(data, iova, size);
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530954
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200955 spin_unlock_irqrestore(&domain->lock, flags);
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530956}
957
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200958static size_t exynos_iommu_unmap(struct iommu_domain *iommu_domain,
959 unsigned long l_iova, size_t size)
KyongHo Cho2a965362012-05-12 05:56:09 +0900960{
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200961 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
Cho KyongHod09d78f2014-05-12 11:44:58 +0530962 sysmmu_iova_t iova = (sysmmu_iova_t)l_iova;
963 sysmmu_pte_t *ent;
Cho KyongHo61128f02014-05-12 11:44:47 +0530964 size_t err_pgsize;
Cho KyongHod09d78f2014-05-12 11:44:58 +0530965 unsigned long flags;
KyongHo Cho2a965362012-05-12 05:56:09 +0900966
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200967 BUG_ON(domain->pgtable == NULL);
KyongHo Cho2a965362012-05-12 05:56:09 +0900968
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200969 spin_lock_irqsave(&domain->pgtablelock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900970
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200971 ent = section_entry(domain->pgtable, iova);
KyongHo Cho2a965362012-05-12 05:56:09 +0900972
973 if (lv1ent_section(ent)) {
Cho KyongHo0bf4e542014-05-12 11:45:00 +0530974 if (WARN_ON(size < SECT_SIZE)) {
Cho KyongHo61128f02014-05-12 11:44:47 +0530975 err_pgsize = SECT_SIZE;
976 goto err;
977 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900978
Sachin Kamatf171aba2014-08-04 10:06:28 +0530979 /* workaround for h/w bug in System MMU v3.3 */
980 *ent = ZERO_LV2LINK;
KyongHo Cho2a965362012-05-12 05:56:09 +0900981 pgtable_flush(ent, ent + 1);
982 size = SECT_SIZE;
983 goto done;
984 }
985
986 if (unlikely(lv1ent_fault(ent))) {
987 if (size > SECT_SIZE)
988 size = SECT_SIZE;
989 goto done;
990 }
991
992 /* lv1ent_page(sent) == true here */
993
994 ent = page_entry(ent, iova);
995
996 if (unlikely(lv2ent_fault(ent))) {
997 size = SPAGE_SIZE;
998 goto done;
999 }
1000
1001 if (lv2ent_small(ent)) {
1002 *ent = 0;
1003 size = SPAGE_SIZE;
Cho KyongHo6cb47ed2014-05-12 11:44:51 +05301004 pgtable_flush(ent, ent + 1);
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001005 domain->lv2entcnt[lv1ent_offset(iova)] += 1;
KyongHo Cho2a965362012-05-12 05:56:09 +09001006 goto done;
1007 }
1008
1009 /* lv1ent_large(ent) == true here */
Cho KyongHo0bf4e542014-05-12 11:45:00 +05301010 if (WARN_ON(size < LPAGE_SIZE)) {
Cho KyongHo61128f02014-05-12 11:44:47 +05301011 err_pgsize = LPAGE_SIZE;
1012 goto err;
1013 }
KyongHo Cho2a965362012-05-12 05:56:09 +09001014
1015 memset(ent, 0, sizeof(*ent) * SPAGES_PER_LPAGE);
Cho KyongHo6cb47ed2014-05-12 11:44:51 +05301016 pgtable_flush(ent, ent + SPAGES_PER_LPAGE);
KyongHo Cho2a965362012-05-12 05:56:09 +09001017
1018 size = LPAGE_SIZE;
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001019 domain->lv2entcnt[lv1ent_offset(iova)] += SPAGES_PER_LPAGE;
KyongHo Cho2a965362012-05-12 05:56:09 +09001020done:
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001021 spin_unlock_irqrestore(&domain->pgtablelock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +09001022
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001023 exynos_iommu_tlb_invalidate_entry(domain, iova, size);
KyongHo Cho2a965362012-05-12 05:56:09 +09001024
KyongHo Cho2a965362012-05-12 05:56:09 +09001025 return size;
Cho KyongHo61128f02014-05-12 11:44:47 +05301026err:
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001027 spin_unlock_irqrestore(&domain->pgtablelock, flags);
Cho KyongHo61128f02014-05-12 11:44:47 +05301028
Cho KyongHo0bf4e542014-05-12 11:45:00 +05301029 pr_err("%s: Failed: size(%#zx) @ %#x is smaller than page size %#zx\n",
1030 __func__, size, iova, err_pgsize);
Cho KyongHo61128f02014-05-12 11:44:47 +05301031
1032 return 0;
KyongHo Cho2a965362012-05-12 05:56:09 +09001033}
1034
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001035static phys_addr_t exynos_iommu_iova_to_phys(struct iommu_domain *iommu_domain,
Varun Sethibb5547a2013-03-29 01:23:58 +05301036 dma_addr_t iova)
KyongHo Cho2a965362012-05-12 05:56:09 +09001037{
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001038 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
Cho KyongHod09d78f2014-05-12 11:44:58 +05301039 sysmmu_pte_t *entry;
KyongHo Cho2a965362012-05-12 05:56:09 +09001040 unsigned long flags;
1041 phys_addr_t phys = 0;
1042
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001043 spin_lock_irqsave(&domain->pgtablelock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +09001044
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001045 entry = section_entry(domain->pgtable, iova);
KyongHo Cho2a965362012-05-12 05:56:09 +09001046
1047 if (lv1ent_section(entry)) {
1048 phys = section_phys(entry) + section_offs(iova);
1049 } else if (lv1ent_page(entry)) {
1050 entry = page_entry(entry, iova);
1051
1052 if (lv2ent_large(entry))
1053 phys = lpage_phys(entry) + lpage_offs(iova);
1054 else if (lv2ent_small(entry))
1055 phys = spage_phys(entry) + spage_offs(iova);
1056 }
1057
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001058 spin_unlock_irqrestore(&domain->pgtablelock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +09001059
1060 return phys;
1061}
1062
Antonios Motakisbf4a1c92014-05-12 11:44:59 +05301063static int exynos_iommu_add_device(struct device *dev)
1064{
1065 struct iommu_group *group;
1066 int ret;
1067
1068 group = iommu_group_get(dev);
1069
1070 if (!group) {
1071 group = iommu_group_alloc();
1072 if (IS_ERR(group)) {
1073 dev_err(dev, "Failed to allocate IOMMU group\n");
1074 return PTR_ERR(group);
1075 }
1076 }
1077
1078 ret = iommu_group_add_device(group, dev);
1079 iommu_group_put(group);
1080
1081 return ret;
1082}
1083
1084static void exynos_iommu_remove_device(struct device *dev)
1085{
1086 iommu_group_remove_device(dev);
1087}
1088
Thierry Redingb22f6432014-06-27 09:03:12 +02001089static const struct iommu_ops exynos_iommu_ops = {
Joerg Roedele1fd1ea2015-03-26 13:43:11 +01001090 .domain_alloc = exynos_iommu_domain_alloc,
1091 .domain_free = exynos_iommu_domain_free,
Bjorn Helgaasba5fa6f2014-05-08 14:49:14 -06001092 .attach_dev = exynos_iommu_attach_device,
1093 .detach_dev = exynos_iommu_detach_device,
1094 .map = exynos_iommu_map,
1095 .unmap = exynos_iommu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -07001096 .map_sg = default_iommu_map_sg,
Bjorn Helgaasba5fa6f2014-05-08 14:49:14 -06001097 .iova_to_phys = exynos_iommu_iova_to_phys,
1098 .add_device = exynos_iommu_add_device,
1099 .remove_device = exynos_iommu_remove_device,
KyongHo Cho2a965362012-05-12 05:56:09 +09001100 .pgsize_bitmap = SECT_SIZE | LPAGE_SIZE | SPAGE_SIZE,
1101};
1102
1103static int __init exynos_iommu_init(void)
1104{
Thierry Redinga7b67cd2015-02-06 11:44:05 +01001105 struct device_node *np;
KyongHo Cho2a965362012-05-12 05:56:09 +09001106 int ret;
1107
Thierry Redinga7b67cd2015-02-06 11:44:05 +01001108 np = of_find_matching_node(NULL, sysmmu_of_match);
1109 if (!np)
1110 return 0;
1111
1112 of_node_put(np);
1113
Cho KyongHo734c3c72014-05-12 11:44:48 +05301114 lv2table_kmem_cache = kmem_cache_create("exynos-iommu-lv2table",
1115 LV2TABLE_SIZE, LV2TABLE_SIZE, 0, NULL);
1116 if (!lv2table_kmem_cache) {
1117 pr_err("%s: Failed to create kmem cache\n", __func__);
1118 return -ENOMEM;
1119 }
1120
KyongHo Cho2a965362012-05-12 05:56:09 +09001121 ret = platform_driver_register(&exynos_sysmmu_driver);
Cho KyongHo734c3c72014-05-12 11:44:48 +05301122 if (ret) {
1123 pr_err("%s: Failed to register driver\n", __func__);
1124 goto err_reg_driver;
1125 }
KyongHo Cho2a965362012-05-12 05:56:09 +09001126
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301127 zero_lv2_table = kmem_cache_zalloc(lv2table_kmem_cache, GFP_KERNEL);
1128 if (zero_lv2_table == NULL) {
1129 pr_err("%s: Failed to allocate zero level2 page table\n",
1130 __func__);
1131 ret = -ENOMEM;
1132 goto err_zero_lv2;
1133 }
1134
Cho KyongHo734c3c72014-05-12 11:44:48 +05301135 ret = bus_set_iommu(&platform_bus_type, &exynos_iommu_ops);
1136 if (ret) {
1137 pr_err("%s: Failed to register exynos-iommu driver.\n",
1138 __func__);
1139 goto err_set_iommu;
1140 }
KyongHo Cho2a965362012-05-12 05:56:09 +09001141
Cho KyongHo734c3c72014-05-12 11:44:48 +05301142 return 0;
1143err_set_iommu:
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301144 kmem_cache_free(lv2table_kmem_cache, zero_lv2_table);
1145err_zero_lv2:
Cho KyongHo734c3c72014-05-12 11:44:48 +05301146 platform_driver_unregister(&exynos_sysmmu_driver);
1147err_reg_driver:
1148 kmem_cache_destroy(lv2table_kmem_cache);
KyongHo Cho2a965362012-05-12 05:56:09 +09001149 return ret;
1150}
1151subsys_initcall(exynos_iommu_init);