blob: c1be65fd0af51a99489e35d424ce2dba0ee67152 [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
KyongHo Cho2a965362012-05-12 05:56:09 +090032/* We does not consider super section mapping (16MB) */
33#define SECT_ORDER 20
34#define LPAGE_ORDER 16
35#define SPAGE_ORDER 12
36
37#define SECT_SIZE (1 << SECT_ORDER)
38#define LPAGE_SIZE (1 << LPAGE_ORDER)
39#define SPAGE_SIZE (1 << SPAGE_ORDER)
40
41#define SECT_MASK (~(SECT_SIZE - 1))
42#define LPAGE_MASK (~(LPAGE_SIZE - 1))
43#define SPAGE_MASK (~(SPAGE_SIZE - 1))
44
45#define lv1ent_fault(sent) (((*(sent) & 3) == 0) || ((*(sent) & 3) == 3))
46#define lv1ent_page(sent) ((*(sent) & 3) == 1)
47#define lv1ent_section(sent) ((*(sent) & 3) == 2)
48
49#define lv2ent_fault(pent) ((*(pent) & 3) == 0)
50#define lv2ent_small(pent) ((*(pent) & 2) == 2)
51#define lv2ent_large(pent) ((*(pent) & 3) == 1)
52
53#define section_phys(sent) (*(sent) & SECT_MASK)
54#define section_offs(iova) ((iova) & 0xFFFFF)
55#define lpage_phys(pent) (*(pent) & LPAGE_MASK)
56#define lpage_offs(iova) ((iova) & 0xFFFF)
57#define spage_phys(pent) (*(pent) & SPAGE_MASK)
58#define spage_offs(iova) ((iova) & 0xFFF)
59
60#define lv1ent_offset(iova) ((iova) >> SECT_ORDER)
61#define lv2ent_offset(iova) (((iova) & 0xFF000) >> SPAGE_ORDER)
62
63#define NUM_LV1ENTRIES 4096
64#define NUM_LV2ENTRIES 256
65
66#define LV2TABLE_SIZE (NUM_LV2ENTRIES * sizeof(long))
67
68#define SPAGES_PER_LPAGE (LPAGE_SIZE / SPAGE_SIZE)
69
70#define lv2table_base(sent) (*(sent) & 0xFFFFFC00)
71
72#define mk_lv1ent_sect(pa) ((pa) | 2)
73#define mk_lv1ent_page(pa) ((pa) | 1)
74#define mk_lv2ent_lpage(pa) ((pa) | 1)
75#define mk_lv2ent_spage(pa) ((pa) | 2)
76
77#define CTRL_ENABLE 0x5
78#define CTRL_BLOCK 0x7
79#define CTRL_DISABLE 0x0
80
81#define REG_MMU_CTRL 0x000
82#define REG_MMU_CFG 0x004
83#define REG_MMU_STATUS 0x008
84#define REG_MMU_FLUSH 0x00C
85#define REG_MMU_FLUSH_ENTRY 0x010
86#define REG_PT_BASE_ADDR 0x014
87#define REG_INT_STATUS 0x018
88#define REG_INT_CLEAR 0x01C
89
90#define REG_PAGE_FAULT_ADDR 0x024
91#define REG_AW_FAULT_ADDR 0x028
92#define REG_AR_FAULT_ADDR 0x02C
93#define REG_DEFAULT_SLAVE_ADDR 0x030
94
95#define REG_MMU_VERSION 0x034
96
97#define REG_PB0_SADDR 0x04C
98#define REG_PB0_EADDR 0x050
99#define REG_PB1_SADDR 0x054
100#define REG_PB1_EADDR 0x058
101
Cho KyongHo734c3c72014-05-12 11:44:48 +0530102static struct kmem_cache *lv2table_kmem_cache;
103
KyongHo Cho2a965362012-05-12 05:56:09 +0900104static unsigned long *section_entry(unsigned long *pgtable, unsigned long iova)
105{
106 return pgtable + lv1ent_offset(iova);
107}
108
109static unsigned long *page_entry(unsigned long *sent, unsigned long iova)
110{
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530111 return (unsigned long *)phys_to_virt(
112 lv2table_base(sent)) + lv2ent_offset(iova);
KyongHo Cho2a965362012-05-12 05:56:09 +0900113}
114
115enum exynos_sysmmu_inttype {
116 SYSMMU_PAGEFAULT,
117 SYSMMU_AR_MULTIHIT,
118 SYSMMU_AW_MULTIHIT,
119 SYSMMU_BUSERROR,
120 SYSMMU_AR_SECURITY,
121 SYSMMU_AR_ACCESS,
122 SYSMMU_AW_SECURITY,
123 SYSMMU_AW_PROTECTION, /* 7 */
124 SYSMMU_FAULT_UNKNOWN,
125 SYSMMU_FAULTS_NUM
126};
127
KyongHo Cho2a965362012-05-12 05:56:09 +0900128static unsigned short fault_reg_offset[SYSMMU_FAULTS_NUM] = {
129 REG_PAGE_FAULT_ADDR,
130 REG_AR_FAULT_ADDR,
131 REG_AW_FAULT_ADDR,
132 REG_DEFAULT_SLAVE_ADDR,
133 REG_AR_FAULT_ADDR,
134 REG_AR_FAULT_ADDR,
135 REG_AW_FAULT_ADDR,
136 REG_AW_FAULT_ADDR
137};
138
139static char *sysmmu_fault_name[SYSMMU_FAULTS_NUM] = {
140 "PAGE FAULT",
141 "AR MULTI-HIT FAULT",
142 "AW MULTI-HIT FAULT",
143 "BUS ERROR",
144 "AR SECURITY PROTECTION FAULT",
145 "AR ACCESS PROTECTION FAULT",
146 "AW SECURITY PROTECTION FAULT",
147 "AW ACCESS PROTECTION FAULT",
148 "UNKNOWN FAULT"
149};
150
151struct exynos_iommu_domain {
152 struct list_head clients; /* list of sysmmu_drvdata.node */
153 unsigned long *pgtable; /* lv1 page table, 16KB */
154 short *lv2entcnt; /* free lv2 entry counter for each section */
155 spinlock_t lock; /* lock for this structure */
156 spinlock_t pgtablelock; /* lock for modifying page table @ pgtable */
157};
158
159struct sysmmu_drvdata {
160 struct list_head node; /* entry of exynos_iommu_domain.clients */
161 struct device *sysmmu; /* System MMU's device descriptor */
162 struct device *dev; /* Owner of system MMU */
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530163 void __iomem *sfrbase;
164 struct clk *clk;
Cho KyongHo70605872014-05-12 11:44:55 +0530165 struct clk *clk_master;
KyongHo Cho2a965362012-05-12 05:56:09 +0900166 int activations;
167 rwlock_t lock;
168 struct iommu_domain *domain;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530169 phys_addr_t pgtable;
KyongHo Cho2a965362012-05-12 05:56:09 +0900170};
171
172static bool set_sysmmu_active(struct sysmmu_drvdata *data)
173{
174 /* return true if the System MMU was not active previously
175 and it needs to be initialized */
176 return ++data->activations == 1;
177}
178
179static bool set_sysmmu_inactive(struct sysmmu_drvdata *data)
180{
181 /* return true if the System MMU is needed to be disabled */
182 BUG_ON(data->activations < 1);
183 return --data->activations == 0;
184}
185
186static bool is_sysmmu_active(struct sysmmu_drvdata *data)
187{
188 return data->activations > 0;
189}
190
191static void sysmmu_unblock(void __iomem *sfrbase)
192{
193 __raw_writel(CTRL_ENABLE, sfrbase + REG_MMU_CTRL);
194}
195
196static bool sysmmu_block(void __iomem *sfrbase)
197{
198 int i = 120;
199
200 __raw_writel(CTRL_BLOCK, sfrbase + REG_MMU_CTRL);
201 while ((i > 0) && !(__raw_readl(sfrbase + REG_MMU_STATUS) & 1))
202 --i;
203
204 if (!(__raw_readl(sfrbase + REG_MMU_STATUS) & 1)) {
205 sysmmu_unblock(sfrbase);
206 return false;
207 }
208
209 return true;
210}
211
212static void __sysmmu_tlb_invalidate(void __iomem *sfrbase)
213{
214 __raw_writel(0x1, sfrbase + REG_MMU_FLUSH);
215}
216
217static void __sysmmu_tlb_invalidate_entry(void __iomem *sfrbase,
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530218 unsigned long iova, unsigned int num_inv)
KyongHo Cho2a965362012-05-12 05:56:09 +0900219{
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530220 unsigned int i;
221 for (i = 0; i < num_inv; i++) {
222 __raw_writel((iova & SPAGE_MASK) | 1,
223 sfrbase + REG_MMU_FLUSH_ENTRY);
224 iova += SPAGE_SIZE;
225 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900226}
227
228static void __sysmmu_set_ptbase(void __iomem *sfrbase,
229 unsigned long pgd)
230{
231 __raw_writel(0x1, sfrbase + REG_MMU_CFG); /* 16KB LV1, LRU */
232 __raw_writel(pgd, sfrbase + REG_PT_BASE_ADDR);
233
234 __sysmmu_tlb_invalidate(sfrbase);
235}
236
Cho KyongHo1fab7fa2014-05-12 11:44:56 +0530237static void show_fault_information(const char *name,
238 enum exynos_sysmmu_inttype itype,
239 phys_addr_t pgtable_base, unsigned long fault_addr)
KyongHo Cho2a965362012-05-12 05:56:09 +0900240{
241 unsigned long *ent;
242
243 if ((itype >= SYSMMU_FAULTS_NUM) || (itype < SYSMMU_PAGEFAULT))
244 itype = SYSMMU_FAULT_UNKNOWN;
245
Cho KyongHo1fab7fa2014-05-12 11:44:56 +0530246 pr_err("%s occurred at %#lx by %s(Page table base: %pa)\n",
247 sysmmu_fault_name[itype], fault_addr, name, &pgtable_base);
KyongHo Cho2a965362012-05-12 05:56:09 +0900248
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530249 ent = section_entry(phys_to_virt(pgtable_base), fault_addr);
KyongHo Cho2a965362012-05-12 05:56:09 +0900250 pr_err("\tLv1 entry: 0x%lx\n", *ent);
251
252 if (lv1ent_page(ent)) {
253 ent = page_entry(ent, fault_addr);
254 pr_err("\t Lv2 entry: 0x%lx\n", *ent);
255 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900256}
257
258static irqreturn_t exynos_sysmmu_irq(int irq, void *dev_id)
259{
260 /* SYSMMU is in blocked when interrupt occurred. */
261 struct sysmmu_drvdata *data = dev_id;
KyongHo Cho2a965362012-05-12 05:56:09 +0900262 enum exynos_sysmmu_inttype itype;
263 unsigned long addr = -1;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530264 int ret = -ENOSYS;
KyongHo Cho2a965362012-05-12 05:56:09 +0900265
266 read_lock(&data->lock);
267
268 WARN_ON(!is_sysmmu_active(data));
269
Cho KyongHo70605872014-05-12 11:44:55 +0530270 if (!IS_ERR(data->clk_master))
271 clk_enable(data->clk_master);
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530272 itype = (enum exynos_sysmmu_inttype)
273 __ffs(__raw_readl(data->sfrbase + REG_INT_STATUS));
274 if (WARN_ON(!((itype >= 0) && (itype < SYSMMU_FAULT_UNKNOWN))))
KyongHo Cho2a965362012-05-12 05:56:09 +0900275 itype = SYSMMU_FAULT_UNKNOWN;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530276 else
277 addr = __raw_readl(data->sfrbase + fault_reg_offset[itype]);
KyongHo Cho2a965362012-05-12 05:56:09 +0900278
Cho KyongHo1fab7fa2014-05-12 11:44:56 +0530279 if (itype == SYSMMU_FAULT_UNKNOWN) {
280 pr_err("%s: Fault is not occurred by System MMU '%s'!\n",
281 __func__, dev_name(data->sysmmu));
282 pr_err("%s: Please check if IRQ is correctly configured.\n",
283 __func__);
284 BUG();
285 } else {
286 unsigned long base =
287 __raw_readl(data->sfrbase + REG_PT_BASE_ADDR);
288 show_fault_information(dev_name(data->sysmmu),
289 itype, base, addr);
290 if (data->domain)
291 ret = report_iommu_fault(data->domain,
292 data->dev, addr, itype);
KyongHo Cho2a965362012-05-12 05:56:09 +0900293 }
294
Cho KyongHo1fab7fa2014-05-12 11:44:56 +0530295 /* fault is not recovered by fault handler */
296 BUG_ON(ret != 0);
KyongHo Cho2a965362012-05-12 05:56:09 +0900297
Cho KyongHo1fab7fa2014-05-12 11:44:56 +0530298 __raw_writel(1 << itype, data->sfrbase + REG_INT_CLEAR);
299
300 sysmmu_unblock(data->sfrbase);
KyongHo Cho2a965362012-05-12 05:56:09 +0900301
Cho KyongHo70605872014-05-12 11:44:55 +0530302 if (!IS_ERR(data->clk_master))
303 clk_disable(data->clk_master);
304
KyongHo Cho2a965362012-05-12 05:56:09 +0900305 read_unlock(&data->lock);
306
307 return IRQ_HANDLED;
308}
309
310static bool __exynos_sysmmu_disable(struct sysmmu_drvdata *data)
311{
312 unsigned long flags;
313 bool disabled = false;
KyongHo Cho2a965362012-05-12 05:56:09 +0900314
315 write_lock_irqsave(&data->lock, flags);
316
317 if (!set_sysmmu_inactive(data))
318 goto finish;
319
Cho KyongHo70605872014-05-12 11:44:55 +0530320 if (!IS_ERR(data->clk_master))
321 clk_enable(data->clk_master);
322
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530323 __raw_writel(CTRL_DISABLE, data->sfrbase + REG_MMU_CTRL);
KyongHo Cho2a965362012-05-12 05:56:09 +0900324
Cho KyongHo46c16d12014-05-12 11:44:54 +0530325 clk_disable(data->clk);
Cho KyongHo70605872014-05-12 11:44:55 +0530326 if (!IS_ERR(data->clk_master))
327 clk_disable(data->clk_master);
KyongHo Cho2a965362012-05-12 05:56:09 +0900328
329 disabled = true;
330 data->pgtable = 0;
331 data->domain = NULL;
332finish:
333 write_unlock_irqrestore(&data->lock, flags);
334
335 if (disabled)
Cho KyongHoe5cf63c2014-05-12 11:44:53 +0530336 dev_dbg(data->sysmmu, "Disabled\n");
KyongHo Cho2a965362012-05-12 05:56:09 +0900337 else
Cho KyongHoe5cf63c2014-05-12 11:44:53 +0530338 dev_dbg(data->sysmmu, "%d times left to be disabled\n",
339 data->activations);
KyongHo Cho2a965362012-05-12 05:56:09 +0900340
341 return disabled;
342}
343
344/* __exynos_sysmmu_enable: Enables System MMU
345 *
346 * returns -error if an error occurred and System MMU is not enabled,
347 * 0 if the System MMU has been just enabled and 1 if System MMU was already
348 * enabled before.
349 */
350static int __exynos_sysmmu_enable(struct sysmmu_drvdata *data,
351 unsigned long pgtable, struct iommu_domain *domain)
352{
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530353 int ret = 0;
KyongHo Cho2a965362012-05-12 05:56:09 +0900354 unsigned long flags;
355
356 write_lock_irqsave(&data->lock, flags);
357
358 if (!set_sysmmu_active(data)) {
359 if (WARN_ON(pgtable != data->pgtable)) {
360 ret = -EBUSY;
361 set_sysmmu_inactive(data);
362 } else {
363 ret = 1;
364 }
365
Cho KyongHoe5cf63c2014-05-12 11:44:53 +0530366 dev_dbg(data->sysmmu, "Already enabled\n");
KyongHo Cho2a965362012-05-12 05:56:09 +0900367 goto finish;
368 }
369
KyongHo Cho2a965362012-05-12 05:56:09 +0900370 data->pgtable = pgtable;
371
Cho KyongHo70605872014-05-12 11:44:55 +0530372 if (!IS_ERR(data->clk_master))
373 clk_enable(data->clk_master);
374 clk_enable(data->clk);
375
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530376 __sysmmu_set_ptbase(data->sfrbase, pgtable);
KyongHo Cho2a965362012-05-12 05:56:09 +0900377
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530378 __raw_writel(CTRL_ENABLE, data->sfrbase + REG_MMU_CTRL);
379
Cho KyongHo70605872014-05-12 11:44:55 +0530380 if (!IS_ERR(data->clk_master))
381 clk_disable(data->clk_master);
382
KyongHo Cho2a965362012-05-12 05:56:09 +0900383 data->domain = domain;
384
Cho KyongHoe5cf63c2014-05-12 11:44:53 +0530385 dev_dbg(data->sysmmu, "Enabled\n");
KyongHo Cho2a965362012-05-12 05:56:09 +0900386finish:
387 write_unlock_irqrestore(&data->lock, flags);
388
389 return ret;
390}
391
392int exynos_sysmmu_enable(struct device *dev, unsigned long pgtable)
393{
394 struct sysmmu_drvdata *data = dev_get_drvdata(dev->archdata.iommu);
395 int ret;
396
397 BUG_ON(!memblock_is_memory(pgtable));
398
399 ret = pm_runtime_get_sync(data->sysmmu);
400 if (ret < 0) {
Cho KyongHoe5cf63c2014-05-12 11:44:53 +0530401 dev_dbg(data->sysmmu, "Failed to enable\n");
KyongHo Cho2a965362012-05-12 05:56:09 +0900402 return ret;
403 }
404
405 ret = __exynos_sysmmu_enable(data, pgtable, NULL);
406 if (WARN_ON(ret < 0)) {
407 pm_runtime_put(data->sysmmu);
Cho KyongHoe5cf63c2014-05-12 11:44:53 +0530408 dev_err(data->sysmmu, "Already enabled with page table %#x\n",
409 data->pgtable);
KyongHo Cho2a965362012-05-12 05:56:09 +0900410 } else {
411 data->dev = dev;
412 }
413
414 return ret;
415}
416
Sachin Kamat77e38352013-02-06 13:55:17 +0530417static bool exynos_sysmmu_disable(struct device *dev)
KyongHo Cho2a965362012-05-12 05:56:09 +0900418{
419 struct sysmmu_drvdata *data = dev_get_drvdata(dev->archdata.iommu);
420 bool disabled;
421
422 disabled = __exynos_sysmmu_disable(data);
423 pm_runtime_put(data->sysmmu);
424
425 return disabled;
426}
427
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530428static void sysmmu_tlb_invalidate_entry(struct device *dev, unsigned long iova,
429 size_t size)
KyongHo Cho2a965362012-05-12 05:56:09 +0900430{
431 unsigned long flags;
432 struct sysmmu_drvdata *data = dev_get_drvdata(dev->archdata.iommu);
433
434 read_lock_irqsave(&data->lock, flags);
435
436 if (is_sysmmu_active(data)) {
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530437 unsigned int maj;
438 unsigned int num_inv = 1;
Cho KyongHo70605872014-05-12 11:44:55 +0530439
440 if (!IS_ERR(data->clk_master))
441 clk_enable(data->clk_master);
442
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530443 maj = __raw_readl(data->sfrbase + REG_MMU_VERSION);
444 /*
445 * L2TLB invalidation required
446 * 4KB page: 1 invalidation
447 * 64KB page: 16 invalidation
448 * 1MB page: 64 invalidation
449 * because it is set-associative TLB
450 * with 8-way and 64 sets.
451 * 1MB page can be cached in one of all sets.
452 * 64KB page can be one of 16 consecutive sets.
453 */
454 if ((maj >> 28) == 2) /* major version number */
455 num_inv = min_t(unsigned int, size / PAGE_SIZE, 64);
456
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530457 if (sysmmu_block(data->sfrbase)) {
458 __sysmmu_tlb_invalidate_entry(
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530459 data->sfrbase, iova, num_inv);
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530460 sysmmu_unblock(data->sfrbase);
KyongHo Cho2a965362012-05-12 05:56:09 +0900461 }
Cho KyongHo70605872014-05-12 11:44:55 +0530462 if (!IS_ERR(data->clk_master))
463 clk_disable(data->clk_master);
KyongHo Cho2a965362012-05-12 05:56:09 +0900464 } else {
Cho KyongHoe5cf63c2014-05-12 11:44:53 +0530465 dev_dbg(data->sysmmu, "Disabled. Skipping invalidating TLB.\n");
KyongHo Cho2a965362012-05-12 05:56:09 +0900466 }
467
468 read_unlock_irqrestore(&data->lock, flags);
469}
470
471void exynos_sysmmu_tlb_invalidate(struct device *dev)
472{
473 unsigned long flags;
474 struct sysmmu_drvdata *data = dev_get_drvdata(dev->archdata.iommu);
475
476 read_lock_irqsave(&data->lock, flags);
477
478 if (is_sysmmu_active(data)) {
Cho KyongHo70605872014-05-12 11:44:55 +0530479 if (!IS_ERR(data->clk_master))
480 clk_enable(data->clk_master);
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530481 if (sysmmu_block(data->sfrbase)) {
482 __sysmmu_tlb_invalidate(data->sfrbase);
483 sysmmu_unblock(data->sfrbase);
KyongHo Cho2a965362012-05-12 05:56:09 +0900484 }
Cho KyongHo70605872014-05-12 11:44:55 +0530485 if (!IS_ERR(data->clk_master))
486 clk_disable(data->clk_master);
KyongHo Cho2a965362012-05-12 05:56:09 +0900487 } else {
Cho KyongHoe5cf63c2014-05-12 11:44:53 +0530488 dev_dbg(data->sysmmu, "Disabled. Skipping invalidating TLB.\n");
KyongHo Cho2a965362012-05-12 05:56:09 +0900489 }
490
491 read_unlock_irqrestore(&data->lock, flags);
492}
493
494static int exynos_sysmmu_probe(struct platform_device *pdev)
495{
Cho KyongHo46c16d12014-05-12 11:44:54 +0530496 int irq, ret;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530497 struct device *dev = &pdev->dev;
KyongHo Cho2a965362012-05-12 05:56:09 +0900498 struct sysmmu_drvdata *data;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530499 struct resource *res;
KyongHo Cho2a965362012-05-12 05:56:09 +0900500
Cho KyongHo46c16d12014-05-12 11:44:54 +0530501 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
502 if (!data)
503 return -ENOMEM;
KyongHo Cho2a965362012-05-12 05:56:09 +0900504
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530505 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Cho KyongHo46c16d12014-05-12 11:44:54 +0530506 data->sfrbase = devm_ioremap_resource(dev, res);
507 if (IS_ERR(data->sfrbase))
508 return PTR_ERR(data->sfrbase);
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530509
Cho KyongHo46c16d12014-05-12 11:44:54 +0530510 irq = platform_get_irq(pdev, 0);
511 if (irq <= 0) {
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530512 dev_dbg(dev, "Unable to find IRQ resource\n");
Cho KyongHo46c16d12014-05-12 11:44:54 +0530513 return irq;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530514 }
515
Cho KyongHo46c16d12014-05-12 11:44:54 +0530516 ret = devm_request_irq(dev, irq, exynos_sysmmu_irq, 0,
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530517 dev_name(dev), data);
KyongHo Cho2a965362012-05-12 05:56:09 +0900518 if (ret) {
Cho KyongHo46c16d12014-05-12 11:44:54 +0530519 dev_err(dev, "Unabled to register handler of irq %d\n", irq);
520 return ret;
KyongHo Cho2a965362012-05-12 05:56:09 +0900521 }
522
Cho KyongHo46c16d12014-05-12 11:44:54 +0530523 data->clk = devm_clk_get(dev, "sysmmu");
524 if (IS_ERR(data->clk)) {
525 dev_err(dev, "Failed to get clock!\n");
526 return PTR_ERR(data->clk);
527 } else {
528 ret = clk_prepare(data->clk);
529 if (ret) {
530 dev_err(dev, "Failed to prepare clk\n");
531 return ret;
532 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900533 }
534
Cho KyongHo70605872014-05-12 11:44:55 +0530535 data->clk_master = devm_clk_get(dev, "master");
536 if (!IS_ERR(data->clk_master)) {
537 ret = clk_prepare(data->clk_master);
538 if (ret) {
539 clk_unprepare(data->clk);
540 dev_err(dev, "Failed to prepare master's clk\n");
541 return ret;
542 }
543 }
544
KyongHo Cho2a965362012-05-12 05:56:09 +0900545 data->sysmmu = dev;
546 rwlock_init(&data->lock);
547 INIT_LIST_HEAD(&data->node);
548
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530549 platform_set_drvdata(pdev, data);
550
Cho KyongHof4723ec2014-05-12 11:44:52 +0530551 pm_runtime_enable(dev);
KyongHo Cho2a965362012-05-12 05:56:09 +0900552
KyongHo Cho2a965362012-05-12 05:56:09 +0900553 return 0;
KyongHo Cho2a965362012-05-12 05:56:09 +0900554}
555
556static struct platform_driver exynos_sysmmu_driver = {
557 .probe = exynos_sysmmu_probe,
558 .driver = {
559 .owner = THIS_MODULE,
560 .name = "exynos-sysmmu",
561 }
562};
563
564static inline void pgtable_flush(void *vastart, void *vaend)
565{
566 dmac_flush_range(vastart, vaend);
567 outer_flush_range(virt_to_phys(vastart),
568 virt_to_phys(vaend));
569}
570
571static int exynos_iommu_domain_init(struct iommu_domain *domain)
572{
573 struct exynos_iommu_domain *priv;
574
575 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
576 if (!priv)
577 return -ENOMEM;
578
579 priv->pgtable = (unsigned long *)__get_free_pages(
580 GFP_KERNEL | __GFP_ZERO, 2);
581 if (!priv->pgtable)
582 goto err_pgtable;
583
584 priv->lv2entcnt = (short *)__get_free_pages(
585 GFP_KERNEL | __GFP_ZERO, 1);
586 if (!priv->lv2entcnt)
587 goto err_counter;
588
589 pgtable_flush(priv->pgtable, priv->pgtable + NUM_LV1ENTRIES);
590
591 spin_lock_init(&priv->lock);
592 spin_lock_init(&priv->pgtablelock);
593 INIT_LIST_HEAD(&priv->clients);
594
Sachin Kamateb516372012-08-01 14:35:17 +0530595 domain->geometry.aperture_start = 0;
596 domain->geometry.aperture_end = ~0UL;
597 domain->geometry.force_aperture = true;
Joerg Roedel3177bb72012-07-11 12:41:10 +0200598
KyongHo Cho2a965362012-05-12 05:56:09 +0900599 domain->priv = priv;
600 return 0;
601
602err_counter:
603 free_pages((unsigned long)priv->pgtable, 2);
604err_pgtable:
605 kfree(priv);
606 return -ENOMEM;
607}
608
609static void exynos_iommu_domain_destroy(struct iommu_domain *domain)
610{
611 struct exynos_iommu_domain *priv = domain->priv;
612 struct sysmmu_drvdata *data;
613 unsigned long flags;
614 int i;
615
616 WARN_ON(!list_empty(&priv->clients));
617
618 spin_lock_irqsave(&priv->lock, flags);
619
620 list_for_each_entry(data, &priv->clients, node) {
621 while (!exynos_sysmmu_disable(data->dev))
622 ; /* until System MMU is actually disabled */
623 }
624
625 spin_unlock_irqrestore(&priv->lock, flags);
626
627 for (i = 0; i < NUM_LV1ENTRIES; i++)
628 if (lv1ent_page(priv->pgtable + i))
Cho KyongHo734c3c72014-05-12 11:44:48 +0530629 kmem_cache_free(lv2table_kmem_cache,
630 phys_to_virt(lv2table_base(priv->pgtable + i)));
KyongHo Cho2a965362012-05-12 05:56:09 +0900631
632 free_pages((unsigned long)priv->pgtable, 2);
633 free_pages((unsigned long)priv->lv2entcnt, 1);
634 kfree(domain->priv);
635 domain->priv = NULL;
636}
637
638static int exynos_iommu_attach_device(struct iommu_domain *domain,
639 struct device *dev)
640{
641 struct sysmmu_drvdata *data = dev_get_drvdata(dev->archdata.iommu);
642 struct exynos_iommu_domain *priv = domain->priv;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530643 phys_addr_t pagetable = virt_to_phys(priv->pgtable);
KyongHo Cho2a965362012-05-12 05:56:09 +0900644 unsigned long flags;
645 int ret;
646
647 ret = pm_runtime_get_sync(data->sysmmu);
648 if (ret < 0)
649 return ret;
650
651 ret = 0;
652
653 spin_lock_irqsave(&priv->lock, flags);
654
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530655 ret = __exynos_sysmmu_enable(data, pagetable, domain);
KyongHo Cho2a965362012-05-12 05:56:09 +0900656
657 if (ret == 0) {
658 /* 'data->node' must not be appeared in priv->clients */
659 BUG_ON(!list_empty(&data->node));
660 data->dev = dev;
661 list_add_tail(&data->node, &priv->clients);
662 }
663
664 spin_unlock_irqrestore(&priv->lock, flags);
665
666 if (ret < 0) {
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530667 dev_err(dev, "%s: Failed to attach IOMMU with pgtable %pa\n",
668 __func__, &pagetable);
KyongHo Cho2a965362012-05-12 05:56:09 +0900669 pm_runtime_put(data->sysmmu);
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530670 return ret;
KyongHo Cho2a965362012-05-12 05:56:09 +0900671 }
672
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530673 dev_dbg(dev, "%s: Attached IOMMU with pgtable %pa %s\n",
674 __func__, &pagetable, (ret == 0) ? "" : ", again");
675
KyongHo Cho2a965362012-05-12 05:56:09 +0900676 return ret;
677}
678
679static void exynos_iommu_detach_device(struct iommu_domain *domain,
680 struct device *dev)
681{
682 struct sysmmu_drvdata *data = dev_get_drvdata(dev->archdata.iommu);
683 struct exynos_iommu_domain *priv = domain->priv;
684 struct list_head *pos;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530685 phys_addr_t pagetable = virt_to_phys(priv->pgtable);
KyongHo Cho2a965362012-05-12 05:56:09 +0900686 unsigned long flags;
687 bool found = false;
688
689 spin_lock_irqsave(&priv->lock, flags);
690
691 list_for_each(pos, &priv->clients) {
692 if (list_entry(pos, struct sysmmu_drvdata, node) == data) {
693 found = true;
694 break;
695 }
696 }
697
698 if (!found)
699 goto finish;
700
701 if (__exynos_sysmmu_disable(data)) {
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530702 dev_dbg(dev, "%s: Detached IOMMU with pgtable %pa\n",
703 __func__, &pagetable);
Wei Yongjunf8ffcc92012-09-06 12:34:09 +0800704 list_del_init(&data->node);
KyongHo Cho2a965362012-05-12 05:56:09 +0900705
706 } else {
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530707 dev_dbg(dev, "%s: Detaching IOMMU with pgtable %pa delayed",
708 __func__, &pagetable);
KyongHo Cho2a965362012-05-12 05:56:09 +0900709 }
710
711finish:
712 spin_unlock_irqrestore(&priv->lock, flags);
713
714 if (found)
715 pm_runtime_put(data->sysmmu);
716}
717
718static unsigned long *alloc_lv2entry(unsigned long *sent, unsigned long iova,
719 short *pgcounter)
720{
Cho KyongHo61128f02014-05-12 11:44:47 +0530721 if (lv1ent_section(sent)) {
722 WARN(1, "Trying mapping on %#08lx mapped with 1MiB page", iova);
723 return ERR_PTR(-EADDRINUSE);
724 }
725
KyongHo Cho2a965362012-05-12 05:56:09 +0900726 if (lv1ent_fault(sent)) {
727 unsigned long *pent;
728
Cho KyongHo734c3c72014-05-12 11:44:48 +0530729 pent = kmem_cache_zalloc(lv2table_kmem_cache, GFP_ATOMIC);
KyongHo Cho2a965362012-05-12 05:56:09 +0900730 BUG_ON((unsigned long)pent & (LV2TABLE_SIZE - 1));
731 if (!pent)
Cho KyongHo61128f02014-05-12 11:44:47 +0530732 return ERR_PTR(-ENOMEM);
KyongHo Cho2a965362012-05-12 05:56:09 +0900733
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530734 *sent = mk_lv1ent_page(virt_to_phys(pent));
KyongHo Cho2a965362012-05-12 05:56:09 +0900735 *pgcounter = NUM_LV2ENTRIES;
736 pgtable_flush(pent, pent + NUM_LV2ENTRIES);
737 pgtable_flush(sent, sent + 1);
738 }
739
740 return page_entry(sent, iova);
741}
742
Cho KyongHo61128f02014-05-12 11:44:47 +0530743static int lv1set_section(unsigned long *sent, unsigned long iova,
744 phys_addr_t paddr, short *pgcnt)
KyongHo Cho2a965362012-05-12 05:56:09 +0900745{
Cho KyongHo61128f02014-05-12 11:44:47 +0530746 if (lv1ent_section(sent)) {
747 WARN(1, "Trying mapping on 1MiB@%#08lx that is mapped",
748 iova);
KyongHo Cho2a965362012-05-12 05:56:09 +0900749 return -EADDRINUSE;
Cho KyongHo61128f02014-05-12 11:44:47 +0530750 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900751
752 if (lv1ent_page(sent)) {
Cho KyongHo61128f02014-05-12 11:44:47 +0530753 if (*pgcnt != NUM_LV2ENTRIES) {
754 WARN(1, "Trying mapping on 1MiB@%#08lx that is mapped",
755 iova);
KyongHo Cho2a965362012-05-12 05:56:09 +0900756 return -EADDRINUSE;
Cho KyongHo61128f02014-05-12 11:44:47 +0530757 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900758
Cho KyongHo734c3c72014-05-12 11:44:48 +0530759 kmem_cache_free(lv2table_kmem_cache, page_entry(sent, 0));
KyongHo Cho2a965362012-05-12 05:56:09 +0900760 *pgcnt = 0;
761 }
762
763 *sent = mk_lv1ent_sect(paddr);
764
765 pgtable_flush(sent, sent + 1);
766
767 return 0;
768}
769
770static int lv2set_page(unsigned long *pent, phys_addr_t paddr, size_t size,
771 short *pgcnt)
772{
773 if (size == SPAGE_SIZE) {
Cho KyongHo61128f02014-05-12 11:44:47 +0530774 if (!lv2ent_fault(pent)) {
775 WARN(1, "Trying mapping on 4KiB where mapping exists");
KyongHo Cho2a965362012-05-12 05:56:09 +0900776 return -EADDRINUSE;
Cho KyongHo61128f02014-05-12 11:44:47 +0530777 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900778
779 *pent = mk_lv2ent_spage(paddr);
780 pgtable_flush(pent, pent + 1);
781 *pgcnt -= 1;
782 } else { /* size == LPAGE_SIZE */
783 int i;
784 for (i = 0; i < SPAGES_PER_LPAGE; i++, pent++) {
785 if (!lv2ent_fault(pent)) {
Cho KyongHo61128f02014-05-12 11:44:47 +0530786 WARN(1,
787 "Trying mapping on 64KiB where mapping exists");
788 if (i > 0)
789 memset(pent - i, 0, sizeof(*pent) * i);
KyongHo Cho2a965362012-05-12 05:56:09 +0900790 return -EADDRINUSE;
791 }
792
793 *pent = mk_lv2ent_lpage(paddr);
794 }
795 pgtable_flush(pent - SPAGES_PER_LPAGE, pent);
796 *pgcnt -= SPAGES_PER_LPAGE;
797 }
798
799 return 0;
800}
801
802static int exynos_iommu_map(struct iommu_domain *domain, unsigned long iova,
803 phys_addr_t paddr, size_t size, int prot)
804{
805 struct exynos_iommu_domain *priv = domain->priv;
806 unsigned long *entry;
807 unsigned long flags;
808 int ret = -ENOMEM;
809
810 BUG_ON(priv->pgtable == NULL);
811
812 spin_lock_irqsave(&priv->pgtablelock, flags);
813
814 entry = section_entry(priv->pgtable, iova);
815
816 if (size == SECT_SIZE) {
Cho KyongHo61128f02014-05-12 11:44:47 +0530817 ret = lv1set_section(entry, iova, paddr,
KyongHo Cho2a965362012-05-12 05:56:09 +0900818 &priv->lv2entcnt[lv1ent_offset(iova)]);
819 } else {
820 unsigned long *pent;
821
822 pent = alloc_lv2entry(entry, iova,
823 &priv->lv2entcnt[lv1ent_offset(iova)]);
824
Cho KyongHo61128f02014-05-12 11:44:47 +0530825 if (IS_ERR(pent))
826 ret = PTR_ERR(pent);
KyongHo Cho2a965362012-05-12 05:56:09 +0900827 else
828 ret = lv2set_page(pent, paddr, size,
829 &priv->lv2entcnt[lv1ent_offset(iova)]);
830 }
831
Cho KyongHo61128f02014-05-12 11:44:47 +0530832 if (ret)
KyongHo Cho2a965362012-05-12 05:56:09 +0900833 pr_debug("%s: Failed to map iova 0x%lx/0x%x bytes\n",
834 __func__, iova, size);
KyongHo Cho2a965362012-05-12 05:56:09 +0900835
836 spin_unlock_irqrestore(&priv->pgtablelock, flags);
837
838 return ret;
839}
840
841static size_t exynos_iommu_unmap(struct iommu_domain *domain,
842 unsigned long iova, size_t size)
843{
844 struct exynos_iommu_domain *priv = domain->priv;
845 struct sysmmu_drvdata *data;
846 unsigned long flags;
847 unsigned long *ent;
Cho KyongHo61128f02014-05-12 11:44:47 +0530848 size_t err_pgsize;
KyongHo Cho2a965362012-05-12 05:56:09 +0900849
850 BUG_ON(priv->pgtable == NULL);
851
852 spin_lock_irqsave(&priv->pgtablelock, flags);
853
854 ent = section_entry(priv->pgtable, iova);
855
856 if (lv1ent_section(ent)) {
Cho KyongHo61128f02014-05-12 11:44:47 +0530857 if (size < SECT_SIZE) {
858 err_pgsize = SECT_SIZE;
859 goto err;
860 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900861
862 *ent = 0;
863 pgtable_flush(ent, ent + 1);
864 size = SECT_SIZE;
865 goto done;
866 }
867
868 if (unlikely(lv1ent_fault(ent))) {
869 if (size > SECT_SIZE)
870 size = SECT_SIZE;
871 goto done;
872 }
873
874 /* lv1ent_page(sent) == true here */
875
876 ent = page_entry(ent, iova);
877
878 if (unlikely(lv2ent_fault(ent))) {
879 size = SPAGE_SIZE;
880 goto done;
881 }
882
883 if (lv2ent_small(ent)) {
884 *ent = 0;
885 size = SPAGE_SIZE;
Cho KyongHo6cb47ed2014-05-12 11:44:51 +0530886 pgtable_flush(ent, ent + 1);
KyongHo Cho2a965362012-05-12 05:56:09 +0900887 priv->lv2entcnt[lv1ent_offset(iova)] += 1;
888 goto done;
889 }
890
891 /* lv1ent_large(ent) == true here */
Cho KyongHo61128f02014-05-12 11:44:47 +0530892 if (size < LPAGE_SIZE) {
893 err_pgsize = LPAGE_SIZE;
894 goto err;
895 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900896
897 memset(ent, 0, sizeof(*ent) * SPAGES_PER_LPAGE);
Cho KyongHo6cb47ed2014-05-12 11:44:51 +0530898 pgtable_flush(ent, ent + SPAGES_PER_LPAGE);
KyongHo Cho2a965362012-05-12 05:56:09 +0900899
900 size = LPAGE_SIZE;
901 priv->lv2entcnt[lv1ent_offset(iova)] += SPAGES_PER_LPAGE;
902done:
903 spin_unlock_irqrestore(&priv->pgtablelock, flags);
904
905 spin_lock_irqsave(&priv->lock, flags);
906 list_for_each_entry(data, &priv->clients, node)
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530907 sysmmu_tlb_invalidate_entry(data->dev, iova, size);
KyongHo Cho2a965362012-05-12 05:56:09 +0900908 spin_unlock_irqrestore(&priv->lock, flags);
909
KyongHo Cho2a965362012-05-12 05:56:09 +0900910 return size;
Cho KyongHo61128f02014-05-12 11:44:47 +0530911err:
912 spin_unlock_irqrestore(&priv->pgtablelock, flags);
913
914 WARN(1,
915 "%s: Failed due to size(%#x) @ %#08lx is smaller than page size %#x\n",
916 __func__, size, iova, err_pgsize);
917
918 return 0;
KyongHo Cho2a965362012-05-12 05:56:09 +0900919}
920
921static phys_addr_t exynos_iommu_iova_to_phys(struct iommu_domain *domain,
Varun Sethibb5547a2013-03-29 01:23:58 +0530922 dma_addr_t iova)
KyongHo Cho2a965362012-05-12 05:56:09 +0900923{
924 struct exynos_iommu_domain *priv = domain->priv;
925 unsigned long *entry;
926 unsigned long flags;
927 phys_addr_t phys = 0;
928
929 spin_lock_irqsave(&priv->pgtablelock, flags);
930
931 entry = section_entry(priv->pgtable, iova);
932
933 if (lv1ent_section(entry)) {
934 phys = section_phys(entry) + section_offs(iova);
935 } else if (lv1ent_page(entry)) {
936 entry = page_entry(entry, iova);
937
938 if (lv2ent_large(entry))
939 phys = lpage_phys(entry) + lpage_offs(iova);
940 else if (lv2ent_small(entry))
941 phys = spage_phys(entry) + spage_offs(iova);
942 }
943
944 spin_unlock_irqrestore(&priv->pgtablelock, flags);
945
946 return phys;
947}
948
949static struct iommu_ops exynos_iommu_ops = {
950 .domain_init = &exynos_iommu_domain_init,
951 .domain_destroy = &exynos_iommu_domain_destroy,
952 .attach_dev = &exynos_iommu_attach_device,
953 .detach_dev = &exynos_iommu_detach_device,
954 .map = &exynos_iommu_map,
955 .unmap = &exynos_iommu_unmap,
956 .iova_to_phys = &exynos_iommu_iova_to_phys,
957 .pgsize_bitmap = SECT_SIZE | LPAGE_SIZE | SPAGE_SIZE,
958};
959
960static int __init exynos_iommu_init(void)
961{
962 int ret;
963
Cho KyongHo734c3c72014-05-12 11:44:48 +0530964 lv2table_kmem_cache = kmem_cache_create("exynos-iommu-lv2table",
965 LV2TABLE_SIZE, LV2TABLE_SIZE, 0, NULL);
966 if (!lv2table_kmem_cache) {
967 pr_err("%s: Failed to create kmem cache\n", __func__);
968 return -ENOMEM;
969 }
970
KyongHo Cho2a965362012-05-12 05:56:09 +0900971 ret = platform_driver_register(&exynos_sysmmu_driver);
Cho KyongHo734c3c72014-05-12 11:44:48 +0530972 if (ret) {
973 pr_err("%s: Failed to register driver\n", __func__);
974 goto err_reg_driver;
975 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900976
Cho KyongHo734c3c72014-05-12 11:44:48 +0530977 ret = bus_set_iommu(&platform_bus_type, &exynos_iommu_ops);
978 if (ret) {
979 pr_err("%s: Failed to register exynos-iommu driver.\n",
980 __func__);
981 goto err_set_iommu;
982 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900983
Cho KyongHo734c3c72014-05-12 11:44:48 +0530984 return 0;
985err_set_iommu:
986 platform_driver_unregister(&exynos_sysmmu_driver);
987err_reg_driver:
988 kmem_cache_destroy(lv2table_kmem_cache);
KyongHo Cho2a965362012-05-12 05:56:09 +0900989 return ret;
990}
991subsys_initcall(exynos_iommu_init);