blob: 99ed39c89e0b5be59d6bfd672dbc7df7a3f218dd [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
Marek Szyprowski2860af32015-05-19 15:20:31 +0200183/*
184 * This structure is attached to dev.archdata.iommu of the master device
185 * on device add, contains a list of SYSMMU controllers defined by device tree,
186 * which are bound to given master device. It is usually referenced by 'owner'
187 * pointer.
188*/
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530189struct exynos_iommu_owner {
Marek Szyprowski2860af32015-05-19 15:20:31 +0200190 struct device *sysmmu; /* sysmmu controller for given master */
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530191};
192
Marek Szyprowski2860af32015-05-19 15:20:31 +0200193/*
194 * This structure exynos specific generalization of struct iommu_domain.
195 * It contains list of SYSMMU controllers from all master devices, which has
196 * been attached to this domain and page tables of IO address space defined by
197 * it. It is usually referenced by 'domain' pointer.
198 */
KyongHo Cho2a965362012-05-12 05:56:09 +0900199struct exynos_iommu_domain {
Marek Szyprowski2860af32015-05-19 15:20:31 +0200200 struct list_head clients; /* list of sysmmu_drvdata.domain_node */
201 sysmmu_pte_t *pgtable; /* lv1 page table, 16KB */
202 short *lv2entcnt; /* free lv2 entry counter for each section */
203 spinlock_t lock; /* lock for modyfying list of clients */
204 spinlock_t pgtablelock; /* lock for modifying page table @ pgtable */
Joerg Roedele1fd1ea2015-03-26 13:43:11 +0100205 struct iommu_domain domain; /* generic domain data structure */
KyongHo Cho2a965362012-05-12 05:56:09 +0900206};
207
Marek Szyprowski2860af32015-05-19 15:20:31 +0200208/*
209 * This structure hold all data of a single SYSMMU controller, this includes
210 * hw resources like registers and clocks, pointers and list nodes to connect
211 * it to all other structures, internal state and parameters read from device
212 * tree. It is usually referenced by 'data' pointer.
213 */
KyongHo Cho2a965362012-05-12 05:56:09 +0900214struct sysmmu_drvdata {
Marek Szyprowski2860af32015-05-19 15:20:31 +0200215 struct device *sysmmu; /* SYSMMU controller device */
216 struct device *master; /* master device (owner) */
217 void __iomem *sfrbase; /* our registers */
218 struct clk *clk; /* SYSMMU's clock */
219 struct clk *clk_master; /* master's device clock */
220 int activations; /* number of calls to sysmmu_enable */
221 spinlock_t lock; /* lock for modyfying state */
222 struct exynos_iommu_domain *domain; /* domain we belong to */
223 struct list_head domain_node; /* node for domain clients list */
224 phys_addr_t pgtable; /* assigned page table structure */
225 unsigned int version; /* our version */
KyongHo Cho2a965362012-05-12 05:56:09 +0900226};
227
Joerg Roedele1fd1ea2015-03-26 13:43:11 +0100228static struct exynos_iommu_domain *to_exynos_domain(struct iommu_domain *dom)
229{
230 return container_of(dom, struct exynos_iommu_domain, domain);
231}
232
KyongHo Cho2a965362012-05-12 05:56:09 +0900233static bool set_sysmmu_active(struct sysmmu_drvdata *data)
234{
235 /* return true if the System MMU was not active previously
236 and it needs to be initialized */
237 return ++data->activations == 1;
238}
239
240static bool set_sysmmu_inactive(struct sysmmu_drvdata *data)
241{
242 /* return true if the System MMU is needed to be disabled */
243 BUG_ON(data->activations < 1);
244 return --data->activations == 0;
245}
246
247static bool is_sysmmu_active(struct sysmmu_drvdata *data)
248{
249 return data->activations > 0;
250}
251
252static void sysmmu_unblock(void __iomem *sfrbase)
253{
254 __raw_writel(CTRL_ENABLE, sfrbase + REG_MMU_CTRL);
255}
256
257static bool sysmmu_block(void __iomem *sfrbase)
258{
259 int i = 120;
260
261 __raw_writel(CTRL_BLOCK, sfrbase + REG_MMU_CTRL);
262 while ((i > 0) && !(__raw_readl(sfrbase + REG_MMU_STATUS) & 1))
263 --i;
264
265 if (!(__raw_readl(sfrbase + REG_MMU_STATUS) & 1)) {
266 sysmmu_unblock(sfrbase);
267 return false;
268 }
269
270 return true;
271}
272
273static void __sysmmu_tlb_invalidate(void __iomem *sfrbase)
274{
275 __raw_writel(0x1, sfrbase + REG_MMU_FLUSH);
276}
277
278static void __sysmmu_tlb_invalidate_entry(void __iomem *sfrbase,
Cho KyongHod09d78f2014-05-12 11:44:58 +0530279 sysmmu_iova_t iova, unsigned int num_inv)
KyongHo Cho2a965362012-05-12 05:56:09 +0900280{
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530281 unsigned int i;
Sachin Kamat365409d2014-05-22 09:50:56 +0530282
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530283 for (i = 0; i < num_inv; i++) {
284 __raw_writel((iova & SPAGE_MASK) | 1,
285 sfrbase + REG_MMU_FLUSH_ENTRY);
286 iova += SPAGE_SIZE;
287 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900288}
289
290static void __sysmmu_set_ptbase(void __iomem *sfrbase,
Cho KyongHod09d78f2014-05-12 11:44:58 +0530291 phys_addr_t pgd)
KyongHo Cho2a965362012-05-12 05:56:09 +0900292{
KyongHo Cho2a965362012-05-12 05:56:09 +0900293 __raw_writel(pgd, sfrbase + REG_PT_BASE_ADDR);
294
295 __sysmmu_tlb_invalidate(sfrbase);
296}
297
Cho KyongHo1fab7fa2014-05-12 11:44:56 +0530298static void show_fault_information(const char *name,
299 enum exynos_sysmmu_inttype itype,
Cho KyongHod09d78f2014-05-12 11:44:58 +0530300 phys_addr_t pgtable_base, sysmmu_iova_t fault_addr)
KyongHo Cho2a965362012-05-12 05:56:09 +0900301{
Cho KyongHod09d78f2014-05-12 11:44:58 +0530302 sysmmu_pte_t *ent;
KyongHo Cho2a965362012-05-12 05:56:09 +0900303
304 if ((itype >= SYSMMU_FAULTS_NUM) || (itype < SYSMMU_PAGEFAULT))
305 itype = SYSMMU_FAULT_UNKNOWN;
306
Cho KyongHod09d78f2014-05-12 11:44:58 +0530307 pr_err("%s occurred at %#x by %s(Page table base: %pa)\n",
Cho KyongHo1fab7fa2014-05-12 11:44:56 +0530308 sysmmu_fault_name[itype], fault_addr, name, &pgtable_base);
KyongHo Cho2a965362012-05-12 05:56:09 +0900309
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530310 ent = section_entry(phys_to_virt(pgtable_base), fault_addr);
Cho KyongHod09d78f2014-05-12 11:44:58 +0530311 pr_err("\tLv1 entry: %#x\n", *ent);
KyongHo Cho2a965362012-05-12 05:56:09 +0900312
313 if (lv1ent_page(ent)) {
314 ent = page_entry(ent, fault_addr);
Cho KyongHod09d78f2014-05-12 11:44:58 +0530315 pr_err("\t Lv2 entry: %#x\n", *ent);
KyongHo Cho2a965362012-05-12 05:56:09 +0900316 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900317}
318
319static irqreturn_t exynos_sysmmu_irq(int irq, void *dev_id)
320{
Sachin Kamatf171aba2014-08-04 10:06:28 +0530321 /* SYSMMU is in blocked state when interrupt occurred. */
KyongHo Cho2a965362012-05-12 05:56:09 +0900322 struct sysmmu_drvdata *data = dev_id;
KyongHo Cho2a965362012-05-12 05:56:09 +0900323 enum exynos_sysmmu_inttype itype;
Cho KyongHod09d78f2014-05-12 11:44:58 +0530324 sysmmu_iova_t addr = -1;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530325 int ret = -ENOSYS;
KyongHo Cho2a965362012-05-12 05:56:09 +0900326
KyongHo Cho2a965362012-05-12 05:56:09 +0900327 WARN_ON(!is_sysmmu_active(data));
328
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530329 spin_lock(&data->lock);
330
Cho KyongHo70605872014-05-12 11:44:55 +0530331 if (!IS_ERR(data->clk_master))
332 clk_enable(data->clk_master);
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530333
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530334 itype = (enum exynos_sysmmu_inttype)
335 __ffs(__raw_readl(data->sfrbase + REG_INT_STATUS));
336 if (WARN_ON(!((itype >= 0) && (itype < SYSMMU_FAULT_UNKNOWN))))
KyongHo Cho2a965362012-05-12 05:56:09 +0900337 itype = SYSMMU_FAULT_UNKNOWN;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530338 else
339 addr = __raw_readl(data->sfrbase + fault_reg_offset[itype]);
KyongHo Cho2a965362012-05-12 05:56:09 +0900340
Cho KyongHo1fab7fa2014-05-12 11:44:56 +0530341 if (itype == SYSMMU_FAULT_UNKNOWN) {
342 pr_err("%s: Fault is not occurred by System MMU '%s'!\n",
343 __func__, dev_name(data->sysmmu));
344 pr_err("%s: Please check if IRQ is correctly configured.\n",
345 __func__);
346 BUG();
347 } else {
Cho KyongHod09d78f2014-05-12 11:44:58 +0530348 unsigned int base =
Cho KyongHo1fab7fa2014-05-12 11:44:56 +0530349 __raw_readl(data->sfrbase + REG_PT_BASE_ADDR);
350 show_fault_information(dev_name(data->sysmmu),
351 itype, base, addr);
352 if (data->domain)
Marek Szyprowskia9133b992015-05-19 15:20:29 +0200353 ret = report_iommu_fault(&data->domain->domain,
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530354 data->master, addr, itype);
KyongHo Cho2a965362012-05-12 05:56:09 +0900355 }
356
Cho KyongHo1fab7fa2014-05-12 11:44:56 +0530357 /* fault is not recovered by fault handler */
358 BUG_ON(ret != 0);
KyongHo Cho2a965362012-05-12 05:56:09 +0900359
Cho KyongHo1fab7fa2014-05-12 11:44:56 +0530360 __raw_writel(1 << itype, data->sfrbase + REG_INT_CLEAR);
361
362 sysmmu_unblock(data->sfrbase);
KyongHo Cho2a965362012-05-12 05:56:09 +0900363
Cho KyongHo70605872014-05-12 11:44:55 +0530364 if (!IS_ERR(data->clk_master))
365 clk_disable(data->clk_master);
366
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530367 spin_unlock(&data->lock);
KyongHo Cho2a965362012-05-12 05:56:09 +0900368
369 return IRQ_HANDLED;
370}
371
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530372static void __sysmmu_disable_nocount(struct sysmmu_drvdata *data)
KyongHo Cho2a965362012-05-12 05:56:09 +0900373{
Cho KyongHo70605872014-05-12 11:44:55 +0530374 if (!IS_ERR(data->clk_master))
375 clk_enable(data->clk_master);
376
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530377 __raw_writel(CTRL_DISABLE, data->sfrbase + REG_MMU_CTRL);
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530378 __raw_writel(0, data->sfrbase + REG_MMU_CFG);
KyongHo Cho2a965362012-05-12 05:56:09 +0900379
Cho KyongHo46c16d12014-05-12 11:44:54 +0530380 clk_disable(data->clk);
Cho KyongHo70605872014-05-12 11:44:55 +0530381 if (!IS_ERR(data->clk_master))
382 clk_disable(data->clk_master);
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530383}
KyongHo Cho2a965362012-05-12 05:56:09 +0900384
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530385static bool __sysmmu_disable(struct sysmmu_drvdata *data)
386{
387 bool disabled;
388 unsigned long flags;
389
390 spin_lock_irqsave(&data->lock, flags);
391
392 disabled = set_sysmmu_inactive(data);
393
394 if (disabled) {
395 data->pgtable = 0;
396 data->domain = NULL;
397
398 __sysmmu_disable_nocount(data);
399
400 dev_dbg(data->sysmmu, "Disabled\n");
401 } else {
402 dev_dbg(data->sysmmu, "%d times left to disable\n",
403 data->activations);
404 }
405
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530406 spin_unlock_irqrestore(&data->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900407
KyongHo Cho2a965362012-05-12 05:56:09 +0900408 return disabled;
409}
410
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530411static void __sysmmu_init_config(struct sysmmu_drvdata *data)
412{
Cho KyongHoeeb51842014-05-12 11:45:03 +0530413 unsigned int cfg = CFG_LRU | CFG_QOS(15);
414 unsigned int ver;
415
Marek Szyprowski512bd0c2015-05-19 15:20:24 +0200416 ver = MMU_RAW_VER(__raw_readl(data->sfrbase + REG_MMU_VERSION));
Cho KyongHoeeb51842014-05-12 11:45:03 +0530417 if (MMU_MAJ_VER(ver) == 3) {
418 if (MMU_MIN_VER(ver) >= 2) {
419 cfg |= CFG_FLPDCACHE;
420 if (MMU_MIN_VER(ver) == 3) {
421 cfg |= CFG_ACGEN;
422 cfg &= ~CFG_LRU;
423 } else {
424 cfg |= CFG_SYSSEL;
425 }
426 }
427 }
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530428
429 __raw_writel(cfg, data->sfrbase + REG_MMU_CFG);
Marek Szyprowski512bd0c2015-05-19 15:20:24 +0200430 data->version = ver;
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530431}
432
433static void __sysmmu_enable_nocount(struct sysmmu_drvdata *data)
434{
435 if (!IS_ERR(data->clk_master))
436 clk_enable(data->clk_master);
437 clk_enable(data->clk);
438
439 __raw_writel(CTRL_BLOCK, data->sfrbase + REG_MMU_CTRL);
440
441 __sysmmu_init_config(data);
442
443 __sysmmu_set_ptbase(data->sfrbase, data->pgtable);
444
445 __raw_writel(CTRL_ENABLE, data->sfrbase + REG_MMU_CTRL);
446
447 if (!IS_ERR(data->clk_master))
448 clk_disable(data->clk_master);
449}
450
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200451static int __sysmmu_enable(struct sysmmu_drvdata *data, phys_addr_t pgtable,
Marek Szyprowskia9133b992015-05-19 15:20:29 +0200452 struct exynos_iommu_domain *domain)
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530453{
454 int ret = 0;
455 unsigned long flags;
456
457 spin_lock_irqsave(&data->lock, flags);
458 if (set_sysmmu_active(data)) {
459 data->pgtable = pgtable;
Marek Szyprowskia9133b992015-05-19 15:20:29 +0200460 data->domain = domain;
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530461
462 __sysmmu_enable_nocount(data);
463
464 dev_dbg(data->sysmmu, "Enabled\n");
465 } else {
466 ret = (pgtable == data->pgtable) ? 1 : -EBUSY;
467
468 dev_dbg(data->sysmmu, "already enabled\n");
469 }
470
471 if (WARN_ON(ret < 0))
472 set_sysmmu_inactive(data); /* decrement count */
473
474 spin_unlock_irqrestore(&data->lock, flags);
475
476 return ret;
477}
478
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530479static void __sysmmu_tlb_invalidate_flpdcache(struct sysmmu_drvdata *data,
480 sysmmu_iova_t iova)
481{
Marek Szyprowski512bd0c2015-05-19 15:20:24 +0200482 if (data->version == MAKE_MMU_VER(3, 3))
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530483 __raw_writel(iova | 0x1, data->sfrbase + REG_MMU_FLUSH_ENTRY);
484}
485
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200486static void sysmmu_tlb_invalidate_flpdcache(struct sysmmu_drvdata *data,
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530487 sysmmu_iova_t iova)
488{
489 unsigned long flags;
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530490
491 if (!IS_ERR(data->clk_master))
492 clk_enable(data->clk_master);
493
494 spin_lock_irqsave(&data->lock, flags);
495 if (is_sysmmu_active(data))
496 __sysmmu_tlb_invalidate_flpdcache(data, iova);
497 spin_unlock_irqrestore(&data->lock, flags);
498
499 if (!IS_ERR(data->clk_master))
500 clk_disable(data->clk_master);
501}
502
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200503static void sysmmu_tlb_invalidate_entry(struct sysmmu_drvdata *data,
504 sysmmu_iova_t iova, size_t size)
KyongHo Cho2a965362012-05-12 05:56:09 +0900505{
506 unsigned long flags;
KyongHo Cho2a965362012-05-12 05:56:09 +0900507
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530508 spin_lock_irqsave(&data->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900509 if (is_sysmmu_active(data)) {
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530510 unsigned int num_inv = 1;
Cho KyongHo70605872014-05-12 11:44:55 +0530511
512 if (!IS_ERR(data->clk_master))
513 clk_enable(data->clk_master);
514
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530515 /*
516 * L2TLB invalidation required
517 * 4KB page: 1 invalidation
Sachin Kamatf171aba2014-08-04 10:06:28 +0530518 * 64KB page: 16 invalidations
519 * 1MB page: 64 invalidations
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530520 * because it is set-associative TLB
521 * with 8-way and 64 sets.
522 * 1MB page can be cached in one of all sets.
523 * 64KB page can be one of 16 consecutive sets.
524 */
Marek Szyprowski512bd0c2015-05-19 15:20:24 +0200525 if (MMU_MAJ_VER(data->version) == 2)
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530526 num_inv = min_t(unsigned int, size / PAGE_SIZE, 64);
527
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530528 if (sysmmu_block(data->sfrbase)) {
529 __sysmmu_tlb_invalidate_entry(
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530530 data->sfrbase, iova, num_inv);
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530531 sysmmu_unblock(data->sfrbase);
KyongHo Cho2a965362012-05-12 05:56:09 +0900532 }
Cho KyongHo70605872014-05-12 11:44:55 +0530533 if (!IS_ERR(data->clk_master))
534 clk_disable(data->clk_master);
KyongHo Cho2a965362012-05-12 05:56:09 +0900535 } else {
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200536 dev_dbg(data->master,
537 "disabled. Skipping TLB invalidation @ %#x\n", iova);
KyongHo Cho2a965362012-05-12 05:56:09 +0900538 }
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530539 spin_unlock_irqrestore(&data->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900540}
541
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530542static int __init exynos_sysmmu_probe(struct platform_device *pdev)
KyongHo Cho2a965362012-05-12 05:56:09 +0900543{
Cho KyongHo46c16d12014-05-12 11:44:54 +0530544 int irq, ret;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530545 struct device *dev = &pdev->dev;
KyongHo Cho2a965362012-05-12 05:56:09 +0900546 struct sysmmu_drvdata *data;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530547 struct resource *res;
KyongHo Cho2a965362012-05-12 05:56:09 +0900548
Cho KyongHo46c16d12014-05-12 11:44:54 +0530549 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
550 if (!data)
551 return -ENOMEM;
KyongHo Cho2a965362012-05-12 05:56:09 +0900552
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530553 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Cho KyongHo46c16d12014-05-12 11:44:54 +0530554 data->sfrbase = devm_ioremap_resource(dev, res);
555 if (IS_ERR(data->sfrbase))
556 return PTR_ERR(data->sfrbase);
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530557
Cho KyongHo46c16d12014-05-12 11:44:54 +0530558 irq = platform_get_irq(pdev, 0);
559 if (irq <= 0) {
Cho KyongHo0bf4e542014-05-12 11:45:00 +0530560 dev_err(dev, "Unable to find IRQ resource\n");
Cho KyongHo46c16d12014-05-12 11:44:54 +0530561 return irq;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530562 }
563
Cho KyongHo46c16d12014-05-12 11:44:54 +0530564 ret = devm_request_irq(dev, irq, exynos_sysmmu_irq, 0,
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530565 dev_name(dev), data);
KyongHo Cho2a965362012-05-12 05:56:09 +0900566 if (ret) {
Cho KyongHo46c16d12014-05-12 11:44:54 +0530567 dev_err(dev, "Unabled to register handler of irq %d\n", irq);
568 return ret;
KyongHo Cho2a965362012-05-12 05:56:09 +0900569 }
570
Cho KyongHo46c16d12014-05-12 11:44:54 +0530571 data->clk = devm_clk_get(dev, "sysmmu");
572 if (IS_ERR(data->clk)) {
573 dev_err(dev, "Failed to get clock!\n");
574 return PTR_ERR(data->clk);
575 } else {
576 ret = clk_prepare(data->clk);
577 if (ret) {
578 dev_err(dev, "Failed to prepare clk\n");
579 return ret;
580 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900581 }
582
Cho KyongHo70605872014-05-12 11:44:55 +0530583 data->clk_master = devm_clk_get(dev, "master");
584 if (!IS_ERR(data->clk_master)) {
585 ret = clk_prepare(data->clk_master);
586 if (ret) {
587 clk_unprepare(data->clk);
588 dev_err(dev, "Failed to prepare master's clk\n");
589 return ret;
590 }
591 }
592
KyongHo Cho2a965362012-05-12 05:56:09 +0900593 data->sysmmu = dev;
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530594 spin_lock_init(&data->lock);
KyongHo Cho2a965362012-05-12 05:56:09 +0900595
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530596 platform_set_drvdata(pdev, data);
597
Cho KyongHof4723ec2014-05-12 11:44:52 +0530598 pm_runtime_enable(dev);
KyongHo Cho2a965362012-05-12 05:56:09 +0900599
KyongHo Cho2a965362012-05-12 05:56:09 +0900600 return 0;
KyongHo Cho2a965362012-05-12 05:56:09 +0900601}
602
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530603static const struct of_device_id sysmmu_of_match[] __initconst = {
604 { .compatible = "samsung,exynos-sysmmu", },
605 { },
606};
607
608static struct platform_driver exynos_sysmmu_driver __refdata = {
609 .probe = exynos_sysmmu_probe,
610 .driver = {
KyongHo Cho2a965362012-05-12 05:56:09 +0900611 .name = "exynos-sysmmu",
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530612 .of_match_table = sysmmu_of_match,
KyongHo Cho2a965362012-05-12 05:56:09 +0900613 }
614};
615
616static inline void pgtable_flush(void *vastart, void *vaend)
617{
618 dmac_flush_range(vastart, vaend);
619 outer_flush_range(virt_to_phys(vastart),
620 virt_to_phys(vaend));
621}
622
Joerg Roedele1fd1ea2015-03-26 13:43:11 +0100623static struct iommu_domain *exynos_iommu_domain_alloc(unsigned type)
KyongHo Cho2a965362012-05-12 05:56:09 +0900624{
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200625 struct exynos_iommu_domain *domain;
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530626 int i;
KyongHo Cho2a965362012-05-12 05:56:09 +0900627
Joerg Roedele1fd1ea2015-03-26 13:43:11 +0100628 if (type != IOMMU_DOMAIN_UNMANAGED)
629 return NULL;
KyongHo Cho2a965362012-05-12 05:56:09 +0900630
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200631 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
632 if (!domain)
Joerg Roedele1fd1ea2015-03-26 13:43:11 +0100633 return NULL;
634
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200635 domain->pgtable = (sysmmu_pte_t *)__get_free_pages(GFP_KERNEL, 2);
636 if (!domain->pgtable)
KyongHo Cho2a965362012-05-12 05:56:09 +0900637 goto err_pgtable;
638
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200639 domain->lv2entcnt = (short *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1);
640 if (!domain->lv2entcnt)
KyongHo Cho2a965362012-05-12 05:56:09 +0900641 goto err_counter;
642
Sachin Kamatf171aba2014-08-04 10:06:28 +0530643 /* Workaround for System MMU v3.3 to prevent caching 1MiB mapping */
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530644 for (i = 0; i < NUM_LV1ENTRIES; i += 8) {
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200645 domain->pgtable[i + 0] = ZERO_LV2LINK;
646 domain->pgtable[i + 1] = ZERO_LV2LINK;
647 domain->pgtable[i + 2] = ZERO_LV2LINK;
648 domain->pgtable[i + 3] = ZERO_LV2LINK;
649 domain->pgtable[i + 4] = ZERO_LV2LINK;
650 domain->pgtable[i + 5] = ZERO_LV2LINK;
651 domain->pgtable[i + 6] = ZERO_LV2LINK;
652 domain->pgtable[i + 7] = ZERO_LV2LINK;
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530653 }
654
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200655 pgtable_flush(domain->pgtable, domain->pgtable + NUM_LV1ENTRIES);
KyongHo Cho2a965362012-05-12 05:56:09 +0900656
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200657 spin_lock_init(&domain->lock);
658 spin_lock_init(&domain->pgtablelock);
659 INIT_LIST_HEAD(&domain->clients);
KyongHo Cho2a965362012-05-12 05:56:09 +0900660
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200661 domain->domain.geometry.aperture_start = 0;
662 domain->domain.geometry.aperture_end = ~0UL;
663 domain->domain.geometry.force_aperture = true;
Joerg Roedel3177bb72012-07-11 12:41:10 +0200664
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200665 return &domain->domain;
KyongHo Cho2a965362012-05-12 05:56:09 +0900666
667err_counter:
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200668 free_pages((unsigned long)domain->pgtable, 2);
KyongHo Cho2a965362012-05-12 05:56:09 +0900669err_pgtable:
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200670 kfree(domain);
Joerg Roedele1fd1ea2015-03-26 13:43:11 +0100671 return NULL;
KyongHo Cho2a965362012-05-12 05:56:09 +0900672}
673
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200674static void exynos_iommu_domain_free(struct iommu_domain *iommu_domain)
KyongHo Cho2a965362012-05-12 05:56:09 +0900675{
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200676 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200677 struct sysmmu_drvdata *data, *next;
KyongHo Cho2a965362012-05-12 05:56:09 +0900678 unsigned long flags;
679 int i;
680
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200681 WARN_ON(!list_empty(&domain->clients));
KyongHo Cho2a965362012-05-12 05:56:09 +0900682
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200683 spin_lock_irqsave(&domain->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900684
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200685 list_for_each_entry_safe(data, next, &domain->clients, domain_node) {
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200686 if (__sysmmu_disable(data))
687 data->master = NULL;
688 list_del_init(&data->domain_node);
KyongHo Cho2a965362012-05-12 05:56:09 +0900689 }
690
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200691 spin_unlock_irqrestore(&domain->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900692
693 for (i = 0; i < NUM_LV1ENTRIES; i++)
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200694 if (lv1ent_page(domain->pgtable + i))
Cho KyongHo734c3c72014-05-12 11:44:48 +0530695 kmem_cache_free(lv2table_kmem_cache,
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200696 phys_to_virt(lv2table_base(domain->pgtable + i)));
KyongHo Cho2a965362012-05-12 05:56:09 +0900697
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200698 free_pages((unsigned long)domain->pgtable, 2);
699 free_pages((unsigned long)domain->lv2entcnt, 1);
700 kfree(domain);
KyongHo Cho2a965362012-05-12 05:56:09 +0900701}
702
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200703static int exynos_iommu_attach_device(struct iommu_domain *iommu_domain,
KyongHo Cho2a965362012-05-12 05:56:09 +0900704 struct device *dev)
705{
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530706 struct exynos_iommu_owner *owner = dev->archdata.iommu;
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200707 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200708 struct sysmmu_drvdata *data;
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200709 phys_addr_t pagetable = virt_to_phys(domain->pgtable);
KyongHo Cho2a965362012-05-12 05:56:09 +0900710 unsigned long flags;
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200711 int ret = -ENODEV;
KyongHo Cho2a965362012-05-12 05:56:09 +0900712
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200713 if (!has_sysmmu(dev))
714 return -ENODEV;
KyongHo Cho2a965362012-05-12 05:56:09 +0900715
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200716 data = dev_get_drvdata(owner->sysmmu);
717 if (data) {
Marek Szyprowskia9133b992015-05-19 15:20:29 +0200718 ret = __sysmmu_enable(data, pagetable, domain);
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200719 if (ret >= 0) {
720 data->master = dev;
KyongHo Cho2a965362012-05-12 05:56:09 +0900721
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200722 spin_lock_irqsave(&domain->lock, flags);
723 list_add_tail(&data->domain_node, &domain->clients);
724 spin_unlock_irqrestore(&domain->lock, flags);
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200725 }
726 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900727
728 if (ret < 0) {
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530729 dev_err(dev, "%s: Failed to attach IOMMU with pgtable %pa\n",
730 __func__, &pagetable);
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530731 return ret;
KyongHo Cho2a965362012-05-12 05:56:09 +0900732 }
733
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530734 dev_dbg(dev, "%s: Attached IOMMU with pgtable %pa %s\n",
735 __func__, &pagetable, (ret == 0) ? "" : ", again");
736
KyongHo Cho2a965362012-05-12 05:56:09 +0900737 return ret;
738}
739
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200740static void exynos_iommu_detach_device(struct iommu_domain *iommu_domain,
KyongHo Cho2a965362012-05-12 05:56:09 +0900741 struct device *dev)
742{
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200743 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
744 phys_addr_t pagetable = virt_to_phys(domain->pgtable);
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200745 struct sysmmu_drvdata *data;
KyongHo Cho2a965362012-05-12 05:56:09 +0900746 unsigned long flags;
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200747 bool found = false;
748
749 if (!has_sysmmu(dev))
750 return;
KyongHo Cho2a965362012-05-12 05:56:09 +0900751
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200752 spin_lock_irqsave(&domain->lock, flags);
753 list_for_each_entry(data, &domain->clients, domain_node) {
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200754 if (data->master == dev) {
755 if (__sysmmu_disable(data)) {
756 data->master = NULL;
757 list_del_init(&data->domain_node);
758 }
759 found = true;
KyongHo Cho2a965362012-05-12 05:56:09 +0900760 break;
761 }
762 }
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200763 spin_unlock_irqrestore(&domain->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900764
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200765 if (found)
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530766 dev_dbg(dev, "%s: Detached IOMMU with pgtable %pa\n",
767 __func__, &pagetable);
768 else
769 dev_err(dev, "%s: No IOMMU is attached\n", __func__);
KyongHo Cho2a965362012-05-12 05:56:09 +0900770}
771
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200772static sysmmu_pte_t *alloc_lv2entry(struct exynos_iommu_domain *domain,
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530773 sysmmu_pte_t *sent, sysmmu_iova_t iova, short *pgcounter)
KyongHo Cho2a965362012-05-12 05:56:09 +0900774{
Cho KyongHo61128f02014-05-12 11:44:47 +0530775 if (lv1ent_section(sent)) {
Cho KyongHod09d78f2014-05-12 11:44:58 +0530776 WARN(1, "Trying mapping on %#08x mapped with 1MiB page", iova);
Cho KyongHo61128f02014-05-12 11:44:47 +0530777 return ERR_PTR(-EADDRINUSE);
778 }
779
KyongHo Cho2a965362012-05-12 05:56:09 +0900780 if (lv1ent_fault(sent)) {
Cho KyongHod09d78f2014-05-12 11:44:58 +0530781 sysmmu_pte_t *pent;
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530782 bool need_flush_flpd_cache = lv1ent_zero(sent);
KyongHo Cho2a965362012-05-12 05:56:09 +0900783
Cho KyongHo734c3c72014-05-12 11:44:48 +0530784 pent = kmem_cache_zalloc(lv2table_kmem_cache, GFP_ATOMIC);
Cho KyongHod09d78f2014-05-12 11:44:58 +0530785 BUG_ON((unsigned int)pent & (LV2TABLE_SIZE - 1));
KyongHo Cho2a965362012-05-12 05:56:09 +0900786 if (!pent)
Cho KyongHo61128f02014-05-12 11:44:47 +0530787 return ERR_PTR(-ENOMEM);
KyongHo Cho2a965362012-05-12 05:56:09 +0900788
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530789 *sent = mk_lv1ent_page(virt_to_phys(pent));
Colin Crossdc3814f2015-05-08 17:05:44 -0700790 kmemleak_ignore(pent);
KyongHo Cho2a965362012-05-12 05:56:09 +0900791 *pgcounter = NUM_LV2ENTRIES;
792 pgtable_flush(pent, pent + NUM_LV2ENTRIES);
793 pgtable_flush(sent, sent + 1);
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530794
795 /*
Sachin Kamatf171aba2014-08-04 10:06:28 +0530796 * If pre-fetched SLPD is a faulty SLPD in zero_l2_table,
797 * FLPD cache may cache the address of zero_l2_table. This
798 * function replaces the zero_l2_table with new L2 page table
799 * to write valid mappings.
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530800 * Accessing the valid area may cause page fault since FLPD
Sachin Kamatf171aba2014-08-04 10:06:28 +0530801 * cache may still cache zero_l2_table for the valid area
802 * instead of new L2 page table that has the mapping
803 * information of the valid area.
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530804 * Thus any replacement of zero_l2_table with other valid L2
805 * page table must involve FLPD cache invalidation for System
806 * MMU v3.3.
807 * FLPD cache invalidation is performed with TLB invalidation
808 * by VPN without blocking. It is safe to invalidate TLB without
809 * blocking because the target address of TLB invalidation is
810 * not currently mapped.
811 */
812 if (need_flush_flpd_cache) {
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200813 struct sysmmu_drvdata *data;
Sachin Kamat365409d2014-05-22 09:50:56 +0530814
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200815 spin_lock(&domain->lock);
816 list_for_each_entry(data, &domain->clients, domain_node)
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200817 sysmmu_tlb_invalidate_flpdcache(data, iova);
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200818 spin_unlock(&domain->lock);
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530819 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900820 }
821
822 return page_entry(sent, iova);
823}
824
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200825static int lv1set_section(struct exynos_iommu_domain *domain,
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530826 sysmmu_pte_t *sent, sysmmu_iova_t iova,
Cho KyongHo61128f02014-05-12 11:44:47 +0530827 phys_addr_t paddr, short *pgcnt)
KyongHo Cho2a965362012-05-12 05:56:09 +0900828{
Cho KyongHo61128f02014-05-12 11:44:47 +0530829 if (lv1ent_section(sent)) {
Cho KyongHod09d78f2014-05-12 11:44:58 +0530830 WARN(1, "Trying mapping on 1MiB@%#08x that is mapped",
Cho KyongHo61128f02014-05-12 11:44:47 +0530831 iova);
KyongHo Cho2a965362012-05-12 05:56:09 +0900832 return -EADDRINUSE;
Cho KyongHo61128f02014-05-12 11:44:47 +0530833 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900834
835 if (lv1ent_page(sent)) {
Cho KyongHo61128f02014-05-12 11:44:47 +0530836 if (*pgcnt != NUM_LV2ENTRIES) {
Cho KyongHod09d78f2014-05-12 11:44:58 +0530837 WARN(1, "Trying mapping on 1MiB@%#08x that is mapped",
Cho KyongHo61128f02014-05-12 11:44:47 +0530838 iova);
KyongHo Cho2a965362012-05-12 05:56:09 +0900839 return -EADDRINUSE;
Cho KyongHo61128f02014-05-12 11:44:47 +0530840 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900841
Cho KyongHo734c3c72014-05-12 11:44:48 +0530842 kmem_cache_free(lv2table_kmem_cache, page_entry(sent, 0));
KyongHo Cho2a965362012-05-12 05:56:09 +0900843 *pgcnt = 0;
844 }
845
846 *sent = mk_lv1ent_sect(paddr);
847
848 pgtable_flush(sent, sent + 1);
849
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200850 spin_lock(&domain->lock);
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530851 if (lv1ent_page_zero(sent)) {
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200852 struct sysmmu_drvdata *data;
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530853 /*
854 * Flushing FLPD cache in System MMU v3.3 that may cache a FLPD
855 * entry by speculative prefetch of SLPD which has no mapping.
856 */
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200857 list_for_each_entry(data, &domain->clients, domain_node)
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200858 sysmmu_tlb_invalidate_flpdcache(data, iova);
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530859 }
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200860 spin_unlock(&domain->lock);
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530861
KyongHo Cho2a965362012-05-12 05:56:09 +0900862 return 0;
863}
864
Cho KyongHod09d78f2014-05-12 11:44:58 +0530865static int lv2set_page(sysmmu_pte_t *pent, phys_addr_t paddr, size_t size,
KyongHo Cho2a965362012-05-12 05:56:09 +0900866 short *pgcnt)
867{
868 if (size == SPAGE_SIZE) {
Cho KyongHo0bf4e542014-05-12 11:45:00 +0530869 if (WARN_ON(!lv2ent_fault(pent)))
KyongHo Cho2a965362012-05-12 05:56:09 +0900870 return -EADDRINUSE;
871
872 *pent = mk_lv2ent_spage(paddr);
873 pgtable_flush(pent, pent + 1);
874 *pgcnt -= 1;
875 } else { /* size == LPAGE_SIZE */
876 int i;
Sachin Kamat365409d2014-05-22 09:50:56 +0530877
KyongHo Cho2a965362012-05-12 05:56:09 +0900878 for (i = 0; i < SPAGES_PER_LPAGE; i++, pent++) {
Cho KyongHo0bf4e542014-05-12 11:45:00 +0530879 if (WARN_ON(!lv2ent_fault(pent))) {
Cho KyongHo61128f02014-05-12 11:44:47 +0530880 if (i > 0)
881 memset(pent - i, 0, sizeof(*pent) * i);
KyongHo Cho2a965362012-05-12 05:56:09 +0900882 return -EADDRINUSE;
883 }
884
885 *pent = mk_lv2ent_lpage(paddr);
886 }
887 pgtable_flush(pent - SPAGES_PER_LPAGE, pent);
888 *pgcnt -= SPAGES_PER_LPAGE;
889 }
890
891 return 0;
892}
893
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530894/*
895 * *CAUTION* to the I/O virtual memory managers that support exynos-iommu:
896 *
Sachin Kamatf171aba2014-08-04 10:06:28 +0530897 * System MMU v3.x has advanced logic to improve address translation
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530898 * performance with caching more page table entries by a page table walk.
Sachin Kamatf171aba2014-08-04 10:06:28 +0530899 * However, the logic has a bug that while caching faulty page table entries,
900 * System MMU reports page fault if the cached fault entry is hit even though
901 * the fault entry is updated to a valid entry after the entry is cached.
902 * To prevent caching faulty page table entries which may be updated to valid
903 * entries later, the virtual memory manager should care about the workaround
904 * for the problem. The following describes the workaround.
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530905 *
906 * Any two consecutive I/O virtual address regions must have a hole of 128KiB
Sachin Kamatf171aba2014-08-04 10:06:28 +0530907 * at maximum to prevent misbehavior of System MMU 3.x (workaround for h/w bug).
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530908 *
Sachin Kamatf171aba2014-08-04 10:06:28 +0530909 * Precisely, any start address of I/O virtual region must be aligned with
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530910 * the following sizes for System MMU v3.1 and v3.2.
911 * System MMU v3.1: 128KiB
912 * System MMU v3.2: 256KiB
913 *
914 * Because System MMU v3.3 caches page table entries more aggressively, it needs
Sachin Kamatf171aba2014-08-04 10:06:28 +0530915 * more workarounds.
916 * - Any two consecutive I/O virtual regions must have a hole of size larger
917 * than or equal to 128KiB.
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530918 * - Start address of an I/O virtual region must be aligned by 128KiB.
919 */
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200920static int exynos_iommu_map(struct iommu_domain *iommu_domain,
921 unsigned long l_iova, phys_addr_t paddr, size_t size,
922 int prot)
KyongHo Cho2a965362012-05-12 05:56:09 +0900923{
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200924 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
Cho KyongHod09d78f2014-05-12 11:44:58 +0530925 sysmmu_pte_t *entry;
926 sysmmu_iova_t iova = (sysmmu_iova_t)l_iova;
KyongHo Cho2a965362012-05-12 05:56:09 +0900927 unsigned long flags;
928 int ret = -ENOMEM;
929
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200930 BUG_ON(domain->pgtable == NULL);
KyongHo Cho2a965362012-05-12 05:56:09 +0900931
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200932 spin_lock_irqsave(&domain->pgtablelock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900933
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200934 entry = section_entry(domain->pgtable, iova);
KyongHo Cho2a965362012-05-12 05:56:09 +0900935
936 if (size == SECT_SIZE) {
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200937 ret = lv1set_section(domain, entry, iova, paddr,
938 &domain->lv2entcnt[lv1ent_offset(iova)]);
KyongHo Cho2a965362012-05-12 05:56:09 +0900939 } else {
Cho KyongHod09d78f2014-05-12 11:44:58 +0530940 sysmmu_pte_t *pent;
KyongHo Cho2a965362012-05-12 05:56:09 +0900941
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200942 pent = alloc_lv2entry(domain, entry, iova,
943 &domain->lv2entcnt[lv1ent_offset(iova)]);
KyongHo Cho2a965362012-05-12 05:56:09 +0900944
Cho KyongHo61128f02014-05-12 11:44:47 +0530945 if (IS_ERR(pent))
946 ret = PTR_ERR(pent);
KyongHo Cho2a965362012-05-12 05:56:09 +0900947 else
948 ret = lv2set_page(pent, paddr, size,
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200949 &domain->lv2entcnt[lv1ent_offset(iova)]);
KyongHo Cho2a965362012-05-12 05:56:09 +0900950 }
951
Cho KyongHo61128f02014-05-12 11:44:47 +0530952 if (ret)
Cho KyongHo0bf4e542014-05-12 11:45:00 +0530953 pr_err("%s: Failed(%d) to map %#zx bytes @ %#x\n",
954 __func__, ret, size, iova);
KyongHo Cho2a965362012-05-12 05:56:09 +0900955
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200956 spin_unlock_irqrestore(&domain->pgtablelock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900957
958 return ret;
959}
960
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200961static void exynos_iommu_tlb_invalidate_entry(struct exynos_iommu_domain *domain,
962 sysmmu_iova_t iova, size_t size)
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530963{
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200964 struct sysmmu_drvdata *data;
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530965 unsigned long flags;
966
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200967 spin_lock_irqsave(&domain->lock, flags);
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530968
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200969 list_for_each_entry(data, &domain->clients, domain_node)
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200970 sysmmu_tlb_invalidate_entry(data, iova, size);
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530971
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200972 spin_unlock_irqrestore(&domain->lock, flags);
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530973}
974
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200975static size_t exynos_iommu_unmap(struct iommu_domain *iommu_domain,
976 unsigned long l_iova, size_t size)
KyongHo Cho2a965362012-05-12 05:56:09 +0900977{
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200978 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
Cho KyongHod09d78f2014-05-12 11:44:58 +0530979 sysmmu_iova_t iova = (sysmmu_iova_t)l_iova;
980 sysmmu_pte_t *ent;
Cho KyongHo61128f02014-05-12 11:44:47 +0530981 size_t err_pgsize;
Cho KyongHod09d78f2014-05-12 11:44:58 +0530982 unsigned long flags;
KyongHo Cho2a965362012-05-12 05:56:09 +0900983
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200984 BUG_ON(domain->pgtable == NULL);
KyongHo Cho2a965362012-05-12 05:56:09 +0900985
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200986 spin_lock_irqsave(&domain->pgtablelock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900987
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200988 ent = section_entry(domain->pgtable, iova);
KyongHo Cho2a965362012-05-12 05:56:09 +0900989
990 if (lv1ent_section(ent)) {
Cho KyongHo0bf4e542014-05-12 11:45:00 +0530991 if (WARN_ON(size < SECT_SIZE)) {
Cho KyongHo61128f02014-05-12 11:44:47 +0530992 err_pgsize = SECT_SIZE;
993 goto err;
994 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900995
Sachin Kamatf171aba2014-08-04 10:06:28 +0530996 /* workaround for h/w bug in System MMU v3.3 */
997 *ent = ZERO_LV2LINK;
KyongHo Cho2a965362012-05-12 05:56:09 +0900998 pgtable_flush(ent, ent + 1);
999 size = SECT_SIZE;
1000 goto done;
1001 }
1002
1003 if (unlikely(lv1ent_fault(ent))) {
1004 if (size > SECT_SIZE)
1005 size = SECT_SIZE;
1006 goto done;
1007 }
1008
1009 /* lv1ent_page(sent) == true here */
1010
1011 ent = page_entry(ent, iova);
1012
1013 if (unlikely(lv2ent_fault(ent))) {
1014 size = SPAGE_SIZE;
1015 goto done;
1016 }
1017
1018 if (lv2ent_small(ent)) {
1019 *ent = 0;
1020 size = SPAGE_SIZE;
Cho KyongHo6cb47ed2014-05-12 11:44:51 +05301021 pgtable_flush(ent, ent + 1);
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001022 domain->lv2entcnt[lv1ent_offset(iova)] += 1;
KyongHo Cho2a965362012-05-12 05:56:09 +09001023 goto done;
1024 }
1025
1026 /* lv1ent_large(ent) == true here */
Cho KyongHo0bf4e542014-05-12 11:45:00 +05301027 if (WARN_ON(size < LPAGE_SIZE)) {
Cho KyongHo61128f02014-05-12 11:44:47 +05301028 err_pgsize = LPAGE_SIZE;
1029 goto err;
1030 }
KyongHo Cho2a965362012-05-12 05:56:09 +09001031
1032 memset(ent, 0, sizeof(*ent) * SPAGES_PER_LPAGE);
Cho KyongHo6cb47ed2014-05-12 11:44:51 +05301033 pgtable_flush(ent, ent + SPAGES_PER_LPAGE);
KyongHo Cho2a965362012-05-12 05:56:09 +09001034
1035 size = LPAGE_SIZE;
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001036 domain->lv2entcnt[lv1ent_offset(iova)] += SPAGES_PER_LPAGE;
KyongHo Cho2a965362012-05-12 05:56:09 +09001037done:
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001038 spin_unlock_irqrestore(&domain->pgtablelock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +09001039
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001040 exynos_iommu_tlb_invalidate_entry(domain, iova, size);
KyongHo Cho2a965362012-05-12 05:56:09 +09001041
KyongHo Cho2a965362012-05-12 05:56:09 +09001042 return size;
Cho KyongHo61128f02014-05-12 11:44:47 +05301043err:
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001044 spin_unlock_irqrestore(&domain->pgtablelock, flags);
Cho KyongHo61128f02014-05-12 11:44:47 +05301045
Cho KyongHo0bf4e542014-05-12 11:45:00 +05301046 pr_err("%s: Failed: size(%#zx) @ %#x is smaller than page size %#zx\n",
1047 __func__, size, iova, err_pgsize);
Cho KyongHo61128f02014-05-12 11:44:47 +05301048
1049 return 0;
KyongHo Cho2a965362012-05-12 05:56:09 +09001050}
1051
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001052static phys_addr_t exynos_iommu_iova_to_phys(struct iommu_domain *iommu_domain,
Varun Sethibb5547a2013-03-29 01:23:58 +05301053 dma_addr_t iova)
KyongHo Cho2a965362012-05-12 05:56:09 +09001054{
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001055 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
Cho KyongHod09d78f2014-05-12 11:44:58 +05301056 sysmmu_pte_t *entry;
KyongHo Cho2a965362012-05-12 05:56:09 +09001057 unsigned long flags;
1058 phys_addr_t phys = 0;
1059
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001060 spin_lock_irqsave(&domain->pgtablelock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +09001061
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001062 entry = section_entry(domain->pgtable, iova);
KyongHo Cho2a965362012-05-12 05:56:09 +09001063
1064 if (lv1ent_section(entry)) {
1065 phys = section_phys(entry) + section_offs(iova);
1066 } else if (lv1ent_page(entry)) {
1067 entry = page_entry(entry, iova);
1068
1069 if (lv2ent_large(entry))
1070 phys = lpage_phys(entry) + lpage_offs(iova);
1071 else if (lv2ent_small(entry))
1072 phys = spage_phys(entry) + spage_offs(iova);
1073 }
1074
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001075 spin_unlock_irqrestore(&domain->pgtablelock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +09001076
1077 return phys;
1078}
1079
Antonios Motakisbf4a1c92014-05-12 11:44:59 +05301080static int exynos_iommu_add_device(struct device *dev)
1081{
1082 struct iommu_group *group;
1083 int ret;
1084
Marek Szyprowski06801db2015-05-19 15:20:32 +02001085 if (!has_sysmmu(dev))
1086 return -ENODEV;
1087
Antonios Motakisbf4a1c92014-05-12 11:44:59 +05301088 group = iommu_group_get(dev);
1089
1090 if (!group) {
1091 group = iommu_group_alloc();
1092 if (IS_ERR(group)) {
1093 dev_err(dev, "Failed to allocate IOMMU group\n");
1094 return PTR_ERR(group);
1095 }
1096 }
1097
1098 ret = iommu_group_add_device(group, dev);
1099 iommu_group_put(group);
1100
1101 return ret;
1102}
1103
1104static void exynos_iommu_remove_device(struct device *dev)
1105{
Marek Szyprowski06801db2015-05-19 15:20:32 +02001106 if (!has_sysmmu(dev))
1107 return;
1108
Antonios Motakisbf4a1c92014-05-12 11:44:59 +05301109 iommu_group_remove_device(dev);
1110}
1111
Thierry Redingb22f6432014-06-27 09:03:12 +02001112static const struct iommu_ops exynos_iommu_ops = {
Joerg Roedele1fd1ea2015-03-26 13:43:11 +01001113 .domain_alloc = exynos_iommu_domain_alloc,
1114 .domain_free = exynos_iommu_domain_free,
Bjorn Helgaasba5fa6f2014-05-08 14:49:14 -06001115 .attach_dev = exynos_iommu_attach_device,
1116 .detach_dev = exynos_iommu_detach_device,
1117 .map = exynos_iommu_map,
1118 .unmap = exynos_iommu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -07001119 .map_sg = default_iommu_map_sg,
Bjorn Helgaasba5fa6f2014-05-08 14:49:14 -06001120 .iova_to_phys = exynos_iommu_iova_to_phys,
1121 .add_device = exynos_iommu_add_device,
1122 .remove_device = exynos_iommu_remove_device,
KyongHo Cho2a965362012-05-12 05:56:09 +09001123 .pgsize_bitmap = SECT_SIZE | LPAGE_SIZE | SPAGE_SIZE,
1124};
1125
1126static int __init exynos_iommu_init(void)
1127{
Thierry Redinga7b67cd2015-02-06 11:44:05 +01001128 struct device_node *np;
KyongHo Cho2a965362012-05-12 05:56:09 +09001129 int ret;
1130
Thierry Redinga7b67cd2015-02-06 11:44:05 +01001131 np = of_find_matching_node(NULL, sysmmu_of_match);
1132 if (!np)
1133 return 0;
1134
1135 of_node_put(np);
1136
Cho KyongHo734c3c72014-05-12 11:44:48 +05301137 lv2table_kmem_cache = kmem_cache_create("exynos-iommu-lv2table",
1138 LV2TABLE_SIZE, LV2TABLE_SIZE, 0, NULL);
1139 if (!lv2table_kmem_cache) {
1140 pr_err("%s: Failed to create kmem cache\n", __func__);
1141 return -ENOMEM;
1142 }
1143
KyongHo Cho2a965362012-05-12 05:56:09 +09001144 ret = platform_driver_register(&exynos_sysmmu_driver);
Cho KyongHo734c3c72014-05-12 11:44:48 +05301145 if (ret) {
1146 pr_err("%s: Failed to register driver\n", __func__);
1147 goto err_reg_driver;
1148 }
KyongHo Cho2a965362012-05-12 05:56:09 +09001149
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301150 zero_lv2_table = kmem_cache_zalloc(lv2table_kmem_cache, GFP_KERNEL);
1151 if (zero_lv2_table == NULL) {
1152 pr_err("%s: Failed to allocate zero level2 page table\n",
1153 __func__);
1154 ret = -ENOMEM;
1155 goto err_zero_lv2;
1156 }
1157
Cho KyongHo734c3c72014-05-12 11:44:48 +05301158 ret = bus_set_iommu(&platform_bus_type, &exynos_iommu_ops);
1159 if (ret) {
1160 pr_err("%s: Failed to register exynos-iommu driver.\n",
1161 __func__);
1162 goto err_set_iommu;
1163 }
KyongHo Cho2a965362012-05-12 05:56:09 +09001164
Cho KyongHo734c3c72014-05-12 11:44:48 +05301165 return 0;
1166err_set_iommu:
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301167 kmem_cache_free(lv2table_kmem_cache, zero_lv2_table);
1168err_zero_lv2:
Cho KyongHo734c3c72014-05-12 11:44:48 +05301169 platform_driver_unregister(&exynos_sysmmu_driver);
1170err_reg_driver:
1171 kmem_cache_destroy(lv2table_kmem_cache);
KyongHo Cho2a965362012-05-12 05:56:09 +09001172 return ret;
1173}
1174subsys_initcall(exynos_iommu_init);