blob: b937490fdd5dd0845f5f20c5d4a222fb2d11369b [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
15#include <linux/io.h>
16#include <linux/interrupt.h>
17#include <linux/platform_device.h>
18#include <linux/slab.h>
19#include <linux/pm_runtime.h>
20#include <linux/clk.h>
21#include <linux/err.h>
22#include <linux/mm.h>
23#include <linux/iommu.h>
24#include <linux/errno.h>
25#include <linux/list.h>
26#include <linux/memblock.h>
27#include <linux/export.h>
28
29#include <asm/cacheflush.h>
30#include <asm/pgtable.h>
31
Cho KyongHod09d78f2014-05-12 11:44:58 +053032typedef u32 sysmmu_iova_t;
33typedef u32 sysmmu_pte_t;
34
KyongHo Cho2a965362012-05-12 05:56:09 +090035/* We does not consider super section mapping (16MB) */
36#define SECT_ORDER 20
37#define LPAGE_ORDER 16
38#define SPAGE_ORDER 12
39
40#define SECT_SIZE (1 << SECT_ORDER)
41#define LPAGE_SIZE (1 << LPAGE_ORDER)
42#define SPAGE_SIZE (1 << SPAGE_ORDER)
43
44#define SECT_MASK (~(SECT_SIZE - 1))
45#define LPAGE_MASK (~(LPAGE_SIZE - 1))
46#define SPAGE_MASK (~(SPAGE_SIZE - 1))
47
48#define lv1ent_fault(sent) (((*(sent) & 3) == 0) || ((*(sent) & 3) == 3))
49#define lv1ent_page(sent) ((*(sent) & 3) == 1)
50#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
96#define REG_MMU_CTRL 0x000
97#define REG_MMU_CFG 0x004
98#define REG_MMU_STATUS 0x008
99#define REG_MMU_FLUSH 0x00C
100#define REG_MMU_FLUSH_ENTRY 0x010
101#define REG_PT_BASE_ADDR 0x014
102#define REG_INT_STATUS 0x018
103#define REG_INT_CLEAR 0x01C
104
105#define REG_PAGE_FAULT_ADDR 0x024
106#define REG_AW_FAULT_ADDR 0x028
107#define REG_AR_FAULT_ADDR 0x02C
108#define REG_DEFAULT_SLAVE_ADDR 0x030
109
110#define REG_MMU_VERSION 0x034
111
112#define REG_PB0_SADDR 0x04C
113#define REG_PB0_EADDR 0x050
114#define REG_PB1_SADDR 0x054
115#define REG_PB1_EADDR 0x058
116
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530117#define has_sysmmu(dev) (dev->archdata.iommu != NULL)
118
Cho KyongHo734c3c72014-05-12 11:44:48 +0530119static struct kmem_cache *lv2table_kmem_cache;
120
Cho KyongHod09d78f2014-05-12 11:44:58 +0530121static sysmmu_pte_t *section_entry(sysmmu_pte_t *pgtable, sysmmu_iova_t iova)
KyongHo Cho2a965362012-05-12 05:56:09 +0900122{
123 return pgtable + lv1ent_offset(iova);
124}
125
Cho KyongHod09d78f2014-05-12 11:44:58 +0530126static sysmmu_pte_t *page_entry(sysmmu_pte_t *sent, sysmmu_iova_t iova)
KyongHo Cho2a965362012-05-12 05:56:09 +0900127{
Cho KyongHod09d78f2014-05-12 11:44:58 +0530128 return (sysmmu_pte_t *)phys_to_virt(
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530129 lv2table_base(sent)) + lv2ent_offset(iova);
KyongHo Cho2a965362012-05-12 05:56:09 +0900130}
131
132enum exynos_sysmmu_inttype {
133 SYSMMU_PAGEFAULT,
134 SYSMMU_AR_MULTIHIT,
135 SYSMMU_AW_MULTIHIT,
136 SYSMMU_BUSERROR,
137 SYSMMU_AR_SECURITY,
138 SYSMMU_AR_ACCESS,
139 SYSMMU_AW_SECURITY,
140 SYSMMU_AW_PROTECTION, /* 7 */
141 SYSMMU_FAULT_UNKNOWN,
142 SYSMMU_FAULTS_NUM
143};
144
KyongHo Cho2a965362012-05-12 05:56:09 +0900145static unsigned short fault_reg_offset[SYSMMU_FAULTS_NUM] = {
146 REG_PAGE_FAULT_ADDR,
147 REG_AR_FAULT_ADDR,
148 REG_AW_FAULT_ADDR,
149 REG_DEFAULT_SLAVE_ADDR,
150 REG_AR_FAULT_ADDR,
151 REG_AR_FAULT_ADDR,
152 REG_AW_FAULT_ADDR,
153 REG_AW_FAULT_ADDR
154};
155
156static char *sysmmu_fault_name[SYSMMU_FAULTS_NUM] = {
157 "PAGE FAULT",
158 "AR MULTI-HIT FAULT",
159 "AW MULTI-HIT FAULT",
160 "BUS ERROR",
161 "AR SECURITY PROTECTION FAULT",
162 "AR ACCESS PROTECTION FAULT",
163 "AW SECURITY PROTECTION FAULT",
164 "AW ACCESS PROTECTION FAULT",
165 "UNKNOWN FAULT"
166};
167
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530168/* attached to dev.archdata.iommu of the master device */
169struct exynos_iommu_owner {
170 struct list_head client; /* entry of exynos_iommu_domain.clients */
171 struct device *dev;
172 struct device *sysmmu;
173 struct iommu_domain *domain;
174 void *vmm_data; /* IO virtual memory manager's data */
175 spinlock_t lock; /* Lock to preserve consistency of System MMU */
176};
177
KyongHo Cho2a965362012-05-12 05:56:09 +0900178struct exynos_iommu_domain {
179 struct list_head clients; /* list of sysmmu_drvdata.node */
Cho KyongHod09d78f2014-05-12 11:44:58 +0530180 sysmmu_pte_t *pgtable; /* lv1 page table, 16KB */
KyongHo Cho2a965362012-05-12 05:56:09 +0900181 short *lv2entcnt; /* free lv2 entry counter for each section */
182 spinlock_t lock; /* lock for this structure */
183 spinlock_t pgtablelock; /* lock for modifying page table @ pgtable */
184};
185
186struct sysmmu_drvdata {
KyongHo Cho2a965362012-05-12 05:56:09 +0900187 struct device *sysmmu; /* System MMU's device descriptor */
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530188 struct device *master; /* Owner of system MMU */
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530189 void __iomem *sfrbase;
190 struct clk *clk;
Cho KyongHo70605872014-05-12 11:44:55 +0530191 struct clk *clk_master;
KyongHo Cho2a965362012-05-12 05:56:09 +0900192 int activations;
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530193 spinlock_t lock;
KyongHo Cho2a965362012-05-12 05:56:09 +0900194 struct iommu_domain *domain;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530195 phys_addr_t pgtable;
KyongHo Cho2a965362012-05-12 05:56:09 +0900196};
197
198static bool set_sysmmu_active(struct sysmmu_drvdata *data)
199{
200 /* return true if the System MMU was not active previously
201 and it needs to be initialized */
202 return ++data->activations == 1;
203}
204
205static bool set_sysmmu_inactive(struct sysmmu_drvdata *data)
206{
207 /* return true if the System MMU is needed to be disabled */
208 BUG_ON(data->activations < 1);
209 return --data->activations == 0;
210}
211
212static bool is_sysmmu_active(struct sysmmu_drvdata *data)
213{
214 return data->activations > 0;
215}
216
217static void sysmmu_unblock(void __iomem *sfrbase)
218{
219 __raw_writel(CTRL_ENABLE, sfrbase + REG_MMU_CTRL);
220}
221
222static bool sysmmu_block(void __iomem *sfrbase)
223{
224 int i = 120;
225
226 __raw_writel(CTRL_BLOCK, sfrbase + REG_MMU_CTRL);
227 while ((i > 0) && !(__raw_readl(sfrbase + REG_MMU_STATUS) & 1))
228 --i;
229
230 if (!(__raw_readl(sfrbase + REG_MMU_STATUS) & 1)) {
231 sysmmu_unblock(sfrbase);
232 return false;
233 }
234
235 return true;
236}
237
238static void __sysmmu_tlb_invalidate(void __iomem *sfrbase)
239{
240 __raw_writel(0x1, sfrbase + REG_MMU_FLUSH);
241}
242
243static void __sysmmu_tlb_invalidate_entry(void __iomem *sfrbase,
Cho KyongHod09d78f2014-05-12 11:44:58 +0530244 sysmmu_iova_t iova, unsigned int num_inv)
KyongHo Cho2a965362012-05-12 05:56:09 +0900245{
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530246 unsigned int i;
247 for (i = 0; i < num_inv; i++) {
248 __raw_writel((iova & SPAGE_MASK) | 1,
249 sfrbase + REG_MMU_FLUSH_ENTRY);
250 iova += SPAGE_SIZE;
251 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900252}
253
254static void __sysmmu_set_ptbase(void __iomem *sfrbase,
Cho KyongHod09d78f2014-05-12 11:44:58 +0530255 phys_addr_t pgd)
KyongHo Cho2a965362012-05-12 05:56:09 +0900256{
KyongHo Cho2a965362012-05-12 05:56:09 +0900257 __raw_writel(pgd, sfrbase + REG_PT_BASE_ADDR);
258
259 __sysmmu_tlb_invalidate(sfrbase);
260}
261
Cho KyongHo1fab7fa2014-05-12 11:44:56 +0530262static void show_fault_information(const char *name,
263 enum exynos_sysmmu_inttype itype,
Cho KyongHod09d78f2014-05-12 11:44:58 +0530264 phys_addr_t pgtable_base, sysmmu_iova_t fault_addr)
KyongHo Cho2a965362012-05-12 05:56:09 +0900265{
Cho KyongHod09d78f2014-05-12 11:44:58 +0530266 sysmmu_pte_t *ent;
KyongHo Cho2a965362012-05-12 05:56:09 +0900267
268 if ((itype >= SYSMMU_FAULTS_NUM) || (itype < SYSMMU_PAGEFAULT))
269 itype = SYSMMU_FAULT_UNKNOWN;
270
Cho KyongHod09d78f2014-05-12 11:44:58 +0530271 pr_err("%s occurred at %#x by %s(Page table base: %pa)\n",
Cho KyongHo1fab7fa2014-05-12 11:44:56 +0530272 sysmmu_fault_name[itype], fault_addr, name, &pgtable_base);
KyongHo Cho2a965362012-05-12 05:56:09 +0900273
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530274 ent = section_entry(phys_to_virt(pgtable_base), fault_addr);
Cho KyongHod09d78f2014-05-12 11:44:58 +0530275 pr_err("\tLv1 entry: %#x\n", *ent);
KyongHo Cho2a965362012-05-12 05:56:09 +0900276
277 if (lv1ent_page(ent)) {
278 ent = page_entry(ent, fault_addr);
Cho KyongHod09d78f2014-05-12 11:44:58 +0530279 pr_err("\t Lv2 entry: %#x\n", *ent);
KyongHo Cho2a965362012-05-12 05:56:09 +0900280 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900281}
282
283static irqreturn_t exynos_sysmmu_irq(int irq, void *dev_id)
284{
285 /* SYSMMU is in blocked when interrupt occurred. */
286 struct sysmmu_drvdata *data = dev_id;
KyongHo Cho2a965362012-05-12 05:56:09 +0900287 enum exynos_sysmmu_inttype itype;
Cho KyongHod09d78f2014-05-12 11:44:58 +0530288 sysmmu_iova_t addr = -1;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530289 int ret = -ENOSYS;
KyongHo Cho2a965362012-05-12 05:56:09 +0900290
KyongHo Cho2a965362012-05-12 05:56:09 +0900291 WARN_ON(!is_sysmmu_active(data));
292
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530293 spin_lock(&data->lock);
294
Cho KyongHo70605872014-05-12 11:44:55 +0530295 if (!IS_ERR(data->clk_master))
296 clk_enable(data->clk_master);
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530297
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530298 itype = (enum exynos_sysmmu_inttype)
299 __ffs(__raw_readl(data->sfrbase + REG_INT_STATUS));
300 if (WARN_ON(!((itype >= 0) && (itype < SYSMMU_FAULT_UNKNOWN))))
KyongHo Cho2a965362012-05-12 05:56:09 +0900301 itype = SYSMMU_FAULT_UNKNOWN;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530302 else
303 addr = __raw_readl(data->sfrbase + fault_reg_offset[itype]);
KyongHo Cho2a965362012-05-12 05:56:09 +0900304
Cho KyongHo1fab7fa2014-05-12 11:44:56 +0530305 if (itype == SYSMMU_FAULT_UNKNOWN) {
306 pr_err("%s: Fault is not occurred by System MMU '%s'!\n",
307 __func__, dev_name(data->sysmmu));
308 pr_err("%s: Please check if IRQ is correctly configured.\n",
309 __func__);
310 BUG();
311 } else {
Cho KyongHod09d78f2014-05-12 11:44:58 +0530312 unsigned int base =
Cho KyongHo1fab7fa2014-05-12 11:44:56 +0530313 __raw_readl(data->sfrbase + REG_PT_BASE_ADDR);
314 show_fault_information(dev_name(data->sysmmu),
315 itype, base, addr);
316 if (data->domain)
317 ret = report_iommu_fault(data->domain,
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530318 data->master, addr, itype);
KyongHo Cho2a965362012-05-12 05:56:09 +0900319 }
320
Cho KyongHo1fab7fa2014-05-12 11:44:56 +0530321 /* fault is not recovered by fault handler */
322 BUG_ON(ret != 0);
KyongHo Cho2a965362012-05-12 05:56:09 +0900323
Cho KyongHo1fab7fa2014-05-12 11:44:56 +0530324 __raw_writel(1 << itype, data->sfrbase + REG_INT_CLEAR);
325
326 sysmmu_unblock(data->sfrbase);
KyongHo Cho2a965362012-05-12 05:56:09 +0900327
Cho KyongHo70605872014-05-12 11:44:55 +0530328 if (!IS_ERR(data->clk_master))
329 clk_disable(data->clk_master);
330
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530331 spin_unlock(&data->lock);
KyongHo Cho2a965362012-05-12 05:56:09 +0900332
333 return IRQ_HANDLED;
334}
335
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530336static void __sysmmu_disable_nocount(struct sysmmu_drvdata *data)
KyongHo Cho2a965362012-05-12 05:56:09 +0900337{
Cho KyongHo70605872014-05-12 11:44:55 +0530338 if (!IS_ERR(data->clk_master))
339 clk_enable(data->clk_master);
340
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530341 __raw_writel(CTRL_DISABLE, data->sfrbase + REG_MMU_CTRL);
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530342 __raw_writel(0, data->sfrbase + REG_MMU_CFG);
KyongHo Cho2a965362012-05-12 05:56:09 +0900343
Cho KyongHo46c16d12014-05-12 11:44:54 +0530344 clk_disable(data->clk);
Cho KyongHo70605872014-05-12 11:44:55 +0530345 if (!IS_ERR(data->clk_master))
346 clk_disable(data->clk_master);
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530347}
KyongHo Cho2a965362012-05-12 05:56:09 +0900348
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530349static bool __sysmmu_disable(struct sysmmu_drvdata *data)
350{
351 bool disabled;
352 unsigned long flags;
353
354 spin_lock_irqsave(&data->lock, flags);
355
356 disabled = set_sysmmu_inactive(data);
357
358 if (disabled) {
359 data->pgtable = 0;
360 data->domain = NULL;
361
362 __sysmmu_disable_nocount(data);
363
364 dev_dbg(data->sysmmu, "Disabled\n");
365 } else {
366 dev_dbg(data->sysmmu, "%d times left to disable\n",
367 data->activations);
368 }
369
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530370 spin_unlock_irqrestore(&data->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900371
KyongHo Cho2a965362012-05-12 05:56:09 +0900372 return disabled;
373}
374
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530375static void __sysmmu_init_config(struct sysmmu_drvdata *data)
376{
377 unsigned int cfg = 0;
378
379 __raw_writel(cfg, data->sfrbase + REG_MMU_CFG);
380}
381
382static void __sysmmu_enable_nocount(struct sysmmu_drvdata *data)
383{
384 if (!IS_ERR(data->clk_master))
385 clk_enable(data->clk_master);
386 clk_enable(data->clk);
387
388 __raw_writel(CTRL_BLOCK, data->sfrbase + REG_MMU_CTRL);
389
390 __sysmmu_init_config(data);
391
392 __sysmmu_set_ptbase(data->sfrbase, data->pgtable);
393
394 __raw_writel(CTRL_ENABLE, data->sfrbase + REG_MMU_CTRL);
395
396 if (!IS_ERR(data->clk_master))
397 clk_disable(data->clk_master);
398}
399
400static int __sysmmu_enable(struct sysmmu_drvdata *data,
401 phys_addr_t pgtable, struct iommu_domain *domain)
402{
403 int ret = 0;
404 unsigned long flags;
405
406 spin_lock_irqsave(&data->lock, flags);
407 if (set_sysmmu_active(data)) {
408 data->pgtable = pgtable;
409 data->domain = domain;
410
411 __sysmmu_enable_nocount(data);
412
413 dev_dbg(data->sysmmu, "Enabled\n");
414 } else {
415 ret = (pgtable == data->pgtable) ? 1 : -EBUSY;
416
417 dev_dbg(data->sysmmu, "already enabled\n");
418 }
419
420 if (WARN_ON(ret < 0))
421 set_sysmmu_inactive(data); /* decrement count */
422
423 spin_unlock_irqrestore(&data->lock, flags);
424
425 return ret;
426}
427
KyongHo Cho2a965362012-05-12 05:56:09 +0900428/* __exynos_sysmmu_enable: Enables System MMU
429 *
430 * returns -error if an error occurred and System MMU is not enabled,
431 * 0 if the System MMU has been just enabled and 1 if System MMU was already
432 * enabled before.
433 */
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530434static int __exynos_sysmmu_enable(struct device *dev, phys_addr_t pgtable,
435 struct iommu_domain *domain)
KyongHo Cho2a965362012-05-12 05:56:09 +0900436{
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530437 int ret = 0;
KyongHo Cho2a965362012-05-12 05:56:09 +0900438 unsigned long flags;
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530439 struct exynos_iommu_owner *owner = dev->archdata.iommu;
440 struct sysmmu_drvdata *data;
KyongHo Cho2a965362012-05-12 05:56:09 +0900441
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530442 BUG_ON(!has_sysmmu(dev));
KyongHo Cho2a965362012-05-12 05:56:09 +0900443
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530444 spin_lock_irqsave(&owner->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900445
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530446 data = dev_get_drvdata(owner->sysmmu);
KyongHo Cho2a965362012-05-12 05:56:09 +0900447
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530448 ret = __sysmmu_enable(data, pgtable, domain);
449 if (ret >= 0)
450 data->master = dev;
KyongHo Cho2a965362012-05-12 05:56:09 +0900451
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530452 spin_unlock_irqrestore(&owner->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900453
454 return ret;
455}
456
Cho KyongHod09d78f2014-05-12 11:44:58 +0530457int exynos_sysmmu_enable(struct device *dev, phys_addr_t pgtable)
KyongHo Cho2a965362012-05-12 05:56:09 +0900458{
KyongHo Cho2a965362012-05-12 05:56:09 +0900459 BUG_ON(!memblock_is_memory(pgtable));
460
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530461 return __exynos_sysmmu_enable(dev, pgtable, NULL);
KyongHo Cho2a965362012-05-12 05:56:09 +0900462}
463
Sachin Kamat77e38352013-02-06 13:55:17 +0530464static bool exynos_sysmmu_disable(struct device *dev)
KyongHo Cho2a965362012-05-12 05:56:09 +0900465{
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530466 unsigned long flags;
467 bool disabled = true;
468 struct exynos_iommu_owner *owner = dev->archdata.iommu;
469 struct sysmmu_drvdata *data;
KyongHo Cho2a965362012-05-12 05:56:09 +0900470
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530471 BUG_ON(!has_sysmmu(dev));
472
473 spin_lock_irqsave(&owner->lock, flags);
474
475 data = dev_get_drvdata(owner->sysmmu);
476
477 disabled = __sysmmu_disable(data);
478 if (disabled)
479 data->master = NULL;
480
481 spin_unlock_irqrestore(&owner->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900482
483 return disabled;
484}
485
Cho KyongHod09d78f2014-05-12 11:44:58 +0530486static void sysmmu_tlb_invalidate_entry(struct device *dev, sysmmu_iova_t iova,
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530487 size_t size)
KyongHo Cho2a965362012-05-12 05:56:09 +0900488{
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530489 struct exynos_iommu_owner *owner = dev->archdata.iommu;
KyongHo Cho2a965362012-05-12 05:56:09 +0900490 unsigned long flags;
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530491 struct sysmmu_drvdata *data;
492
493 data = dev_get_drvdata(owner->sysmmu);
KyongHo Cho2a965362012-05-12 05:56:09 +0900494
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530495 spin_lock_irqsave(&data->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900496 if (is_sysmmu_active(data)) {
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530497 unsigned int maj;
498 unsigned int num_inv = 1;
Cho KyongHo70605872014-05-12 11:44:55 +0530499
500 if (!IS_ERR(data->clk_master))
501 clk_enable(data->clk_master);
502
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530503 maj = __raw_readl(data->sfrbase + REG_MMU_VERSION);
504 /*
505 * L2TLB invalidation required
506 * 4KB page: 1 invalidation
507 * 64KB page: 16 invalidation
508 * 1MB page: 64 invalidation
509 * because it is set-associative TLB
510 * with 8-way and 64 sets.
511 * 1MB page can be cached in one of all sets.
512 * 64KB page can be one of 16 consecutive sets.
513 */
514 if ((maj >> 28) == 2) /* major version number */
515 num_inv = min_t(unsigned int, size / PAGE_SIZE, 64);
516
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530517 if (sysmmu_block(data->sfrbase)) {
518 __sysmmu_tlb_invalidate_entry(
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530519 data->sfrbase, iova, num_inv);
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530520 sysmmu_unblock(data->sfrbase);
KyongHo Cho2a965362012-05-12 05:56:09 +0900521 }
Cho KyongHo70605872014-05-12 11:44:55 +0530522 if (!IS_ERR(data->clk_master))
523 clk_disable(data->clk_master);
KyongHo Cho2a965362012-05-12 05:56:09 +0900524 } else {
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530525 dev_dbg(dev, "disabled. Skipping TLB invalidation @ %#x\n",
526 iova);
KyongHo Cho2a965362012-05-12 05:56:09 +0900527 }
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530528 spin_unlock_irqrestore(&data->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900529}
530
531void exynos_sysmmu_tlb_invalidate(struct device *dev)
532{
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530533 struct exynos_iommu_owner *owner = dev->archdata.iommu;
KyongHo Cho2a965362012-05-12 05:56:09 +0900534 unsigned long flags;
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530535 struct sysmmu_drvdata *data;
536
537 data = dev_get_drvdata(owner->sysmmu);
KyongHo Cho2a965362012-05-12 05:56:09 +0900538
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530539 spin_lock_irqsave(&data->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900540 if (is_sysmmu_active(data)) {
Cho KyongHo70605872014-05-12 11:44:55 +0530541 if (!IS_ERR(data->clk_master))
542 clk_enable(data->clk_master);
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530543 if (sysmmu_block(data->sfrbase)) {
544 __sysmmu_tlb_invalidate(data->sfrbase);
545 sysmmu_unblock(data->sfrbase);
KyongHo Cho2a965362012-05-12 05:56:09 +0900546 }
Cho KyongHo70605872014-05-12 11:44:55 +0530547 if (!IS_ERR(data->clk_master))
548 clk_disable(data->clk_master);
KyongHo Cho2a965362012-05-12 05:56:09 +0900549 } else {
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530550 dev_dbg(dev, "disabled. Skipping TLB invalidation\n");
KyongHo Cho2a965362012-05-12 05:56:09 +0900551 }
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530552 spin_unlock_irqrestore(&data->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900553}
554
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530555static int __init exynos_sysmmu_probe(struct platform_device *pdev)
KyongHo Cho2a965362012-05-12 05:56:09 +0900556{
Cho KyongHo46c16d12014-05-12 11:44:54 +0530557 int irq, ret;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530558 struct device *dev = &pdev->dev;
KyongHo Cho2a965362012-05-12 05:56:09 +0900559 struct sysmmu_drvdata *data;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530560 struct resource *res;
KyongHo Cho2a965362012-05-12 05:56:09 +0900561
Cho KyongHo46c16d12014-05-12 11:44:54 +0530562 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
563 if (!data)
564 return -ENOMEM;
KyongHo Cho2a965362012-05-12 05:56:09 +0900565
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530566 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Cho KyongHo46c16d12014-05-12 11:44:54 +0530567 data->sfrbase = devm_ioremap_resource(dev, res);
568 if (IS_ERR(data->sfrbase))
569 return PTR_ERR(data->sfrbase);
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530570
Cho KyongHo46c16d12014-05-12 11:44:54 +0530571 irq = platform_get_irq(pdev, 0);
572 if (irq <= 0) {
Cho KyongHo0bf4e542014-05-12 11:45:00 +0530573 dev_err(dev, "Unable to find IRQ resource\n");
Cho KyongHo46c16d12014-05-12 11:44:54 +0530574 return irq;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530575 }
576
Cho KyongHo46c16d12014-05-12 11:44:54 +0530577 ret = devm_request_irq(dev, irq, exynos_sysmmu_irq, 0,
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530578 dev_name(dev), data);
KyongHo Cho2a965362012-05-12 05:56:09 +0900579 if (ret) {
Cho KyongHo46c16d12014-05-12 11:44:54 +0530580 dev_err(dev, "Unabled to register handler of irq %d\n", irq);
581 return ret;
KyongHo Cho2a965362012-05-12 05:56:09 +0900582 }
583
Cho KyongHo46c16d12014-05-12 11:44:54 +0530584 data->clk = devm_clk_get(dev, "sysmmu");
585 if (IS_ERR(data->clk)) {
586 dev_err(dev, "Failed to get clock!\n");
587 return PTR_ERR(data->clk);
588 } else {
589 ret = clk_prepare(data->clk);
590 if (ret) {
591 dev_err(dev, "Failed to prepare clk\n");
592 return ret;
593 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900594 }
595
Cho KyongHo70605872014-05-12 11:44:55 +0530596 data->clk_master = devm_clk_get(dev, "master");
597 if (!IS_ERR(data->clk_master)) {
598 ret = clk_prepare(data->clk_master);
599 if (ret) {
600 clk_unprepare(data->clk);
601 dev_err(dev, "Failed to prepare master's clk\n");
602 return ret;
603 }
604 }
605
KyongHo Cho2a965362012-05-12 05:56:09 +0900606 data->sysmmu = dev;
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530607 spin_lock_init(&data->lock);
KyongHo Cho2a965362012-05-12 05:56:09 +0900608
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530609 platform_set_drvdata(pdev, data);
610
Cho KyongHof4723ec2014-05-12 11:44:52 +0530611 pm_runtime_enable(dev);
KyongHo Cho2a965362012-05-12 05:56:09 +0900612
KyongHo Cho2a965362012-05-12 05:56:09 +0900613 return 0;
KyongHo Cho2a965362012-05-12 05:56:09 +0900614}
615
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530616static const struct of_device_id sysmmu_of_match[] __initconst = {
617 { .compatible = "samsung,exynos-sysmmu", },
618 { },
619};
620
621static struct platform_driver exynos_sysmmu_driver __refdata = {
622 .probe = exynos_sysmmu_probe,
623 .driver = {
KyongHo Cho2a965362012-05-12 05:56:09 +0900624 .owner = THIS_MODULE,
625 .name = "exynos-sysmmu",
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530626 .of_match_table = sysmmu_of_match,
KyongHo Cho2a965362012-05-12 05:56:09 +0900627 }
628};
629
630static inline void pgtable_flush(void *vastart, void *vaend)
631{
632 dmac_flush_range(vastart, vaend);
633 outer_flush_range(virt_to_phys(vastart),
634 virt_to_phys(vaend));
635}
636
637static int exynos_iommu_domain_init(struct iommu_domain *domain)
638{
639 struct exynos_iommu_domain *priv;
640
641 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
642 if (!priv)
643 return -ENOMEM;
644
Cho KyongHod09d78f2014-05-12 11:44:58 +0530645 priv->pgtable = (sysmmu_pte_t *)__get_free_pages(
KyongHo Cho2a965362012-05-12 05:56:09 +0900646 GFP_KERNEL | __GFP_ZERO, 2);
647 if (!priv->pgtable)
648 goto err_pgtable;
649
650 priv->lv2entcnt = (short *)__get_free_pages(
651 GFP_KERNEL | __GFP_ZERO, 1);
652 if (!priv->lv2entcnt)
653 goto err_counter;
654
655 pgtable_flush(priv->pgtable, priv->pgtable + NUM_LV1ENTRIES);
656
657 spin_lock_init(&priv->lock);
658 spin_lock_init(&priv->pgtablelock);
659 INIT_LIST_HEAD(&priv->clients);
660
Sachin Kamateb516372012-08-01 14:35:17 +0530661 domain->geometry.aperture_start = 0;
662 domain->geometry.aperture_end = ~0UL;
663 domain->geometry.force_aperture = true;
Joerg Roedel3177bb72012-07-11 12:41:10 +0200664
KyongHo Cho2a965362012-05-12 05:56:09 +0900665 domain->priv = priv;
666 return 0;
667
668err_counter:
669 free_pages((unsigned long)priv->pgtable, 2);
670err_pgtable:
671 kfree(priv);
672 return -ENOMEM;
673}
674
675static void exynos_iommu_domain_destroy(struct iommu_domain *domain)
676{
677 struct exynos_iommu_domain *priv = domain->priv;
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530678 struct exynos_iommu_owner *owner;
KyongHo Cho2a965362012-05-12 05:56:09 +0900679 unsigned long flags;
680 int i;
681
682 WARN_ON(!list_empty(&priv->clients));
683
684 spin_lock_irqsave(&priv->lock, flags);
685
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530686 list_for_each_entry(owner, &priv->clients, client) {
687 while (!exynos_sysmmu_disable(owner->dev))
KyongHo Cho2a965362012-05-12 05:56:09 +0900688 ; /* until System MMU is actually disabled */
689 }
690
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530691 while (!list_empty(&priv->clients))
692 list_del_init(priv->clients.next);
693
KyongHo Cho2a965362012-05-12 05:56:09 +0900694 spin_unlock_irqrestore(&priv->lock, flags);
695
696 for (i = 0; i < NUM_LV1ENTRIES; i++)
697 if (lv1ent_page(priv->pgtable + i))
Cho KyongHo734c3c72014-05-12 11:44:48 +0530698 kmem_cache_free(lv2table_kmem_cache,
699 phys_to_virt(lv2table_base(priv->pgtable + i)));
KyongHo Cho2a965362012-05-12 05:56:09 +0900700
701 free_pages((unsigned long)priv->pgtable, 2);
702 free_pages((unsigned long)priv->lv2entcnt, 1);
703 kfree(domain->priv);
704 domain->priv = NULL;
705}
706
707static int exynos_iommu_attach_device(struct iommu_domain *domain,
708 struct device *dev)
709{
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530710 struct exynos_iommu_owner *owner = dev->archdata.iommu;
KyongHo Cho2a965362012-05-12 05:56:09 +0900711 struct exynos_iommu_domain *priv = domain->priv;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530712 phys_addr_t pagetable = virt_to_phys(priv->pgtable);
KyongHo Cho2a965362012-05-12 05:56:09 +0900713 unsigned long flags;
714 int ret;
715
KyongHo Cho2a965362012-05-12 05:56:09 +0900716 spin_lock_irqsave(&priv->lock, flags);
717
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530718 ret = __exynos_sysmmu_enable(dev, pagetable, domain);
KyongHo Cho2a965362012-05-12 05:56:09 +0900719 if (ret == 0) {
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530720 list_add_tail(&owner->client, &priv->clients);
721 owner->domain = domain;
KyongHo Cho2a965362012-05-12 05:56:09 +0900722 }
723
724 spin_unlock_irqrestore(&priv->lock, flags);
725
726 if (ret < 0) {
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530727 dev_err(dev, "%s: Failed to attach IOMMU with pgtable %pa\n",
728 __func__, &pagetable);
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530729 return ret;
KyongHo Cho2a965362012-05-12 05:56:09 +0900730 }
731
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530732 dev_dbg(dev, "%s: Attached IOMMU with pgtable %pa %s\n",
733 __func__, &pagetable, (ret == 0) ? "" : ", again");
734
KyongHo Cho2a965362012-05-12 05:56:09 +0900735 return ret;
736}
737
738static void exynos_iommu_detach_device(struct iommu_domain *domain,
739 struct device *dev)
740{
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530741 struct exynos_iommu_owner *owner;
KyongHo Cho2a965362012-05-12 05:56:09 +0900742 struct exynos_iommu_domain *priv = domain->priv;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530743 phys_addr_t pagetable = virt_to_phys(priv->pgtable);
KyongHo Cho2a965362012-05-12 05:56:09 +0900744 unsigned long flags;
KyongHo Cho2a965362012-05-12 05:56:09 +0900745
746 spin_lock_irqsave(&priv->lock, flags);
747
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530748 list_for_each_entry(owner, &priv->clients, client) {
749 if (owner == dev->archdata.iommu) {
750 if (exynos_sysmmu_disable(dev)) {
751 list_del_init(&owner->client);
752 owner->domain = NULL;
753 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900754 break;
755 }
756 }
757
KyongHo Cho2a965362012-05-12 05:56:09 +0900758 spin_unlock_irqrestore(&priv->lock, flags);
759
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530760 if (owner == dev->archdata.iommu)
761 dev_dbg(dev, "%s: Detached IOMMU with pgtable %pa\n",
762 __func__, &pagetable);
763 else
764 dev_err(dev, "%s: No IOMMU is attached\n", __func__);
KyongHo Cho2a965362012-05-12 05:56:09 +0900765}
766
Cho KyongHod09d78f2014-05-12 11:44:58 +0530767static sysmmu_pte_t *alloc_lv2entry(sysmmu_pte_t *sent, sysmmu_iova_t iova,
KyongHo Cho2a965362012-05-12 05:56:09 +0900768 short *pgcounter)
769{
Cho KyongHo61128f02014-05-12 11:44:47 +0530770 if (lv1ent_section(sent)) {
Cho KyongHod09d78f2014-05-12 11:44:58 +0530771 WARN(1, "Trying mapping on %#08x mapped with 1MiB page", iova);
Cho KyongHo61128f02014-05-12 11:44:47 +0530772 return ERR_PTR(-EADDRINUSE);
773 }
774
KyongHo Cho2a965362012-05-12 05:56:09 +0900775 if (lv1ent_fault(sent)) {
Cho KyongHod09d78f2014-05-12 11:44:58 +0530776 sysmmu_pte_t *pent;
KyongHo Cho2a965362012-05-12 05:56:09 +0900777
Cho KyongHo734c3c72014-05-12 11:44:48 +0530778 pent = kmem_cache_zalloc(lv2table_kmem_cache, GFP_ATOMIC);
Cho KyongHod09d78f2014-05-12 11:44:58 +0530779 BUG_ON((unsigned int)pent & (LV2TABLE_SIZE - 1));
KyongHo Cho2a965362012-05-12 05:56:09 +0900780 if (!pent)
Cho KyongHo61128f02014-05-12 11:44:47 +0530781 return ERR_PTR(-ENOMEM);
KyongHo Cho2a965362012-05-12 05:56:09 +0900782
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530783 *sent = mk_lv1ent_page(virt_to_phys(pent));
KyongHo Cho2a965362012-05-12 05:56:09 +0900784 *pgcounter = NUM_LV2ENTRIES;
785 pgtable_flush(pent, pent + NUM_LV2ENTRIES);
786 pgtable_flush(sent, sent + 1);
787 }
788
789 return page_entry(sent, iova);
790}
791
Cho KyongHod09d78f2014-05-12 11:44:58 +0530792static int lv1set_section(sysmmu_pte_t *sent, sysmmu_iova_t iova,
Cho KyongHo61128f02014-05-12 11:44:47 +0530793 phys_addr_t paddr, short *pgcnt)
KyongHo Cho2a965362012-05-12 05:56:09 +0900794{
Cho KyongHo61128f02014-05-12 11:44:47 +0530795 if (lv1ent_section(sent)) {
Cho KyongHod09d78f2014-05-12 11:44:58 +0530796 WARN(1, "Trying mapping on 1MiB@%#08x that is mapped",
Cho KyongHo61128f02014-05-12 11:44:47 +0530797 iova);
KyongHo Cho2a965362012-05-12 05:56:09 +0900798 return -EADDRINUSE;
Cho KyongHo61128f02014-05-12 11:44:47 +0530799 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900800
801 if (lv1ent_page(sent)) {
Cho KyongHo61128f02014-05-12 11:44:47 +0530802 if (*pgcnt != NUM_LV2ENTRIES) {
Cho KyongHod09d78f2014-05-12 11:44:58 +0530803 WARN(1, "Trying mapping on 1MiB@%#08x that is mapped",
Cho KyongHo61128f02014-05-12 11:44:47 +0530804 iova);
KyongHo Cho2a965362012-05-12 05:56:09 +0900805 return -EADDRINUSE;
Cho KyongHo61128f02014-05-12 11:44:47 +0530806 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900807
Cho KyongHo734c3c72014-05-12 11:44:48 +0530808 kmem_cache_free(lv2table_kmem_cache, page_entry(sent, 0));
KyongHo Cho2a965362012-05-12 05:56:09 +0900809 *pgcnt = 0;
810 }
811
812 *sent = mk_lv1ent_sect(paddr);
813
814 pgtable_flush(sent, sent + 1);
815
816 return 0;
817}
818
Cho KyongHod09d78f2014-05-12 11:44:58 +0530819static int lv2set_page(sysmmu_pte_t *pent, phys_addr_t paddr, size_t size,
KyongHo Cho2a965362012-05-12 05:56:09 +0900820 short *pgcnt)
821{
822 if (size == SPAGE_SIZE) {
Cho KyongHo0bf4e542014-05-12 11:45:00 +0530823 if (WARN_ON(!lv2ent_fault(pent)))
KyongHo Cho2a965362012-05-12 05:56:09 +0900824 return -EADDRINUSE;
825
826 *pent = mk_lv2ent_spage(paddr);
827 pgtable_flush(pent, pent + 1);
828 *pgcnt -= 1;
829 } else { /* size == LPAGE_SIZE */
830 int i;
831 for (i = 0; i < SPAGES_PER_LPAGE; i++, pent++) {
Cho KyongHo0bf4e542014-05-12 11:45:00 +0530832 if (WARN_ON(!lv2ent_fault(pent))) {
Cho KyongHo61128f02014-05-12 11:44:47 +0530833 if (i > 0)
834 memset(pent - i, 0, sizeof(*pent) * i);
KyongHo Cho2a965362012-05-12 05:56:09 +0900835 return -EADDRINUSE;
836 }
837
838 *pent = mk_lv2ent_lpage(paddr);
839 }
840 pgtable_flush(pent - SPAGES_PER_LPAGE, pent);
841 *pgcnt -= SPAGES_PER_LPAGE;
842 }
843
844 return 0;
845}
846
Cho KyongHod09d78f2014-05-12 11:44:58 +0530847static int exynos_iommu_map(struct iommu_domain *domain, unsigned long l_iova,
KyongHo Cho2a965362012-05-12 05:56:09 +0900848 phys_addr_t paddr, size_t size, int prot)
849{
850 struct exynos_iommu_domain *priv = domain->priv;
Cho KyongHod09d78f2014-05-12 11:44:58 +0530851 sysmmu_pte_t *entry;
852 sysmmu_iova_t iova = (sysmmu_iova_t)l_iova;
KyongHo Cho2a965362012-05-12 05:56:09 +0900853 unsigned long flags;
854 int ret = -ENOMEM;
855
856 BUG_ON(priv->pgtable == NULL);
857
858 spin_lock_irqsave(&priv->pgtablelock, flags);
859
860 entry = section_entry(priv->pgtable, iova);
861
862 if (size == SECT_SIZE) {
Cho KyongHo61128f02014-05-12 11:44:47 +0530863 ret = lv1set_section(entry, iova, paddr,
KyongHo Cho2a965362012-05-12 05:56:09 +0900864 &priv->lv2entcnt[lv1ent_offset(iova)]);
865 } else {
Cho KyongHod09d78f2014-05-12 11:44:58 +0530866 sysmmu_pte_t *pent;
KyongHo Cho2a965362012-05-12 05:56:09 +0900867
868 pent = alloc_lv2entry(entry, iova,
869 &priv->lv2entcnt[lv1ent_offset(iova)]);
870
Cho KyongHo61128f02014-05-12 11:44:47 +0530871 if (IS_ERR(pent))
872 ret = PTR_ERR(pent);
KyongHo Cho2a965362012-05-12 05:56:09 +0900873 else
874 ret = lv2set_page(pent, paddr, size,
875 &priv->lv2entcnt[lv1ent_offset(iova)]);
876 }
877
Cho KyongHo61128f02014-05-12 11:44:47 +0530878 if (ret)
Cho KyongHo0bf4e542014-05-12 11:45:00 +0530879 pr_err("%s: Failed(%d) to map %#zx bytes @ %#x\n",
880 __func__, ret, size, iova);
KyongHo Cho2a965362012-05-12 05:56:09 +0900881
882 spin_unlock_irqrestore(&priv->pgtablelock, flags);
883
884 return ret;
885}
886
887static size_t exynos_iommu_unmap(struct iommu_domain *domain,
Cho KyongHod09d78f2014-05-12 11:44:58 +0530888 unsigned long l_iova, size_t size)
KyongHo Cho2a965362012-05-12 05:56:09 +0900889{
890 struct exynos_iommu_domain *priv = domain->priv;
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530891 struct exynos_iommu_owner *owner;
Cho KyongHod09d78f2014-05-12 11:44:58 +0530892 sysmmu_iova_t iova = (sysmmu_iova_t)l_iova;
893 sysmmu_pte_t *ent;
Cho KyongHo61128f02014-05-12 11:44:47 +0530894 size_t err_pgsize;
Cho KyongHod09d78f2014-05-12 11:44:58 +0530895 unsigned long flags;
KyongHo Cho2a965362012-05-12 05:56:09 +0900896
897 BUG_ON(priv->pgtable == NULL);
898
899 spin_lock_irqsave(&priv->pgtablelock, flags);
900
901 ent = section_entry(priv->pgtable, iova);
902
903 if (lv1ent_section(ent)) {
Cho KyongHo0bf4e542014-05-12 11:45:00 +0530904 if (WARN_ON(size < SECT_SIZE)) {
Cho KyongHo61128f02014-05-12 11:44:47 +0530905 err_pgsize = SECT_SIZE;
906 goto err;
907 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900908
909 *ent = 0;
910 pgtable_flush(ent, ent + 1);
911 size = SECT_SIZE;
912 goto done;
913 }
914
915 if (unlikely(lv1ent_fault(ent))) {
916 if (size > SECT_SIZE)
917 size = SECT_SIZE;
918 goto done;
919 }
920
921 /* lv1ent_page(sent) == true here */
922
923 ent = page_entry(ent, iova);
924
925 if (unlikely(lv2ent_fault(ent))) {
926 size = SPAGE_SIZE;
927 goto done;
928 }
929
930 if (lv2ent_small(ent)) {
931 *ent = 0;
932 size = SPAGE_SIZE;
Cho KyongHo6cb47ed2014-05-12 11:44:51 +0530933 pgtable_flush(ent, ent + 1);
KyongHo Cho2a965362012-05-12 05:56:09 +0900934 priv->lv2entcnt[lv1ent_offset(iova)] += 1;
935 goto done;
936 }
937
938 /* lv1ent_large(ent) == true here */
Cho KyongHo0bf4e542014-05-12 11:45:00 +0530939 if (WARN_ON(size < LPAGE_SIZE)) {
Cho KyongHo61128f02014-05-12 11:44:47 +0530940 err_pgsize = LPAGE_SIZE;
941 goto err;
942 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900943
944 memset(ent, 0, sizeof(*ent) * SPAGES_PER_LPAGE);
Cho KyongHo6cb47ed2014-05-12 11:44:51 +0530945 pgtable_flush(ent, ent + SPAGES_PER_LPAGE);
KyongHo Cho2a965362012-05-12 05:56:09 +0900946
947 size = LPAGE_SIZE;
948 priv->lv2entcnt[lv1ent_offset(iova)] += SPAGES_PER_LPAGE;
949done:
950 spin_unlock_irqrestore(&priv->pgtablelock, flags);
951
952 spin_lock_irqsave(&priv->lock, flags);
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530953 list_for_each_entry(owner, &priv->clients, client)
954 sysmmu_tlb_invalidate_entry(owner->dev, iova, size);
KyongHo Cho2a965362012-05-12 05:56:09 +0900955 spin_unlock_irqrestore(&priv->lock, flags);
956
KyongHo Cho2a965362012-05-12 05:56:09 +0900957 return size;
Cho KyongHo61128f02014-05-12 11:44:47 +0530958err:
959 spin_unlock_irqrestore(&priv->pgtablelock, flags);
960
Cho KyongHo0bf4e542014-05-12 11:45:00 +0530961 pr_err("%s: Failed: size(%#zx) @ %#x is smaller than page size %#zx\n",
962 __func__, size, iova, err_pgsize);
Cho KyongHo61128f02014-05-12 11:44:47 +0530963
964 return 0;
KyongHo Cho2a965362012-05-12 05:56:09 +0900965}
966
967static phys_addr_t exynos_iommu_iova_to_phys(struct iommu_domain *domain,
Varun Sethibb5547a2013-03-29 01:23:58 +0530968 dma_addr_t iova)
KyongHo Cho2a965362012-05-12 05:56:09 +0900969{
970 struct exynos_iommu_domain *priv = domain->priv;
Cho KyongHod09d78f2014-05-12 11:44:58 +0530971 sysmmu_pte_t *entry;
KyongHo Cho2a965362012-05-12 05:56:09 +0900972 unsigned long flags;
973 phys_addr_t phys = 0;
974
975 spin_lock_irqsave(&priv->pgtablelock, flags);
976
977 entry = section_entry(priv->pgtable, iova);
978
979 if (lv1ent_section(entry)) {
980 phys = section_phys(entry) + section_offs(iova);
981 } else if (lv1ent_page(entry)) {
982 entry = page_entry(entry, iova);
983
984 if (lv2ent_large(entry))
985 phys = lpage_phys(entry) + lpage_offs(iova);
986 else if (lv2ent_small(entry))
987 phys = spage_phys(entry) + spage_offs(iova);
988 }
989
990 spin_unlock_irqrestore(&priv->pgtablelock, flags);
991
992 return phys;
993}
994
Antonios Motakisbf4a1c92014-05-12 11:44:59 +0530995static int exynos_iommu_add_device(struct device *dev)
996{
997 struct iommu_group *group;
998 int ret;
999
1000 group = iommu_group_get(dev);
1001
1002 if (!group) {
1003 group = iommu_group_alloc();
1004 if (IS_ERR(group)) {
1005 dev_err(dev, "Failed to allocate IOMMU group\n");
1006 return PTR_ERR(group);
1007 }
1008 }
1009
1010 ret = iommu_group_add_device(group, dev);
1011 iommu_group_put(group);
1012
1013 return ret;
1014}
1015
1016static void exynos_iommu_remove_device(struct device *dev)
1017{
1018 iommu_group_remove_device(dev);
1019}
1020
KyongHo Cho2a965362012-05-12 05:56:09 +09001021static struct iommu_ops exynos_iommu_ops = {
1022 .domain_init = &exynos_iommu_domain_init,
1023 .domain_destroy = &exynos_iommu_domain_destroy,
1024 .attach_dev = &exynos_iommu_attach_device,
1025 .detach_dev = &exynos_iommu_detach_device,
1026 .map = &exynos_iommu_map,
1027 .unmap = &exynos_iommu_unmap,
1028 .iova_to_phys = &exynos_iommu_iova_to_phys,
Antonios Motakisbf4a1c92014-05-12 11:44:59 +05301029 .add_device = &exynos_iommu_add_device,
1030 .remove_device = &exynos_iommu_remove_device,
KyongHo Cho2a965362012-05-12 05:56:09 +09001031 .pgsize_bitmap = SECT_SIZE | LPAGE_SIZE | SPAGE_SIZE,
1032};
1033
1034static int __init exynos_iommu_init(void)
1035{
1036 int ret;
1037
Cho KyongHo734c3c72014-05-12 11:44:48 +05301038 lv2table_kmem_cache = kmem_cache_create("exynos-iommu-lv2table",
1039 LV2TABLE_SIZE, LV2TABLE_SIZE, 0, NULL);
1040 if (!lv2table_kmem_cache) {
1041 pr_err("%s: Failed to create kmem cache\n", __func__);
1042 return -ENOMEM;
1043 }
1044
KyongHo Cho2a965362012-05-12 05:56:09 +09001045 ret = platform_driver_register(&exynos_sysmmu_driver);
Cho KyongHo734c3c72014-05-12 11:44:48 +05301046 if (ret) {
1047 pr_err("%s: Failed to register driver\n", __func__);
1048 goto err_reg_driver;
1049 }
KyongHo Cho2a965362012-05-12 05:56:09 +09001050
Cho KyongHo734c3c72014-05-12 11:44:48 +05301051 ret = bus_set_iommu(&platform_bus_type, &exynos_iommu_ops);
1052 if (ret) {
1053 pr_err("%s: Failed to register exynos-iommu driver.\n",
1054 __func__);
1055 goto err_set_iommu;
1056 }
KyongHo Cho2a965362012-05-12 05:56:09 +09001057
Cho KyongHo734c3c72014-05-12 11:44:48 +05301058 return 0;
1059err_set_iommu:
1060 platform_driver_unregister(&exynos_sysmmu_driver);
1061err_reg_driver:
1062 kmem_cache_destroy(lv2table_kmem_cache);
KyongHo Cho2a965362012-05-12 05:56:09 +09001063 return ret;
1064}
1065subsys_initcall(exynos_iommu_init);