blob: 09f69b1d80fc75c79e2689486f2f689144716958 [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
Cho KyongHo66a7ed82014-05-12 11:45:04 +053048#define lv1ent_fault(sent) ((*(sent) == ZERO_LV2LINK) || \
49 ((*(sent) & 3) == 0) || ((*(sent) & 3) == 3))
50#define lv1ent_zero(sent) (*(sent) == ZERO_LV2LINK)
51#define lv1ent_page_zero(sent) ((*(sent) & 3) == 1)
52#define lv1ent_page(sent) ((*(sent) != ZERO_LV2LINK) && \
53 ((*(sent) & 3) == 1))
KyongHo Cho2a965362012-05-12 05:56:09 +090054#define lv1ent_section(sent) ((*(sent) & 3) == 2)
55
56#define lv2ent_fault(pent) ((*(pent) & 3) == 0)
57#define lv2ent_small(pent) ((*(pent) & 2) == 2)
58#define lv2ent_large(pent) ((*(pent) & 3) == 1)
59
Cho KyongHod09d78f2014-05-12 11:44:58 +053060static u32 sysmmu_page_offset(sysmmu_iova_t iova, u32 size)
61{
62 return iova & (size - 1);
63}
KyongHo Cho2a965362012-05-12 05:56:09 +090064
Cho KyongHod09d78f2014-05-12 11:44:58 +053065#define section_phys(sent) (*(sent) & SECT_MASK)
66#define section_offs(iova) sysmmu_page_offset((iova), SECT_SIZE)
67#define lpage_phys(pent) (*(pent) & LPAGE_MASK)
68#define lpage_offs(iova) sysmmu_page_offset((iova), LPAGE_SIZE)
69#define spage_phys(pent) (*(pent) & SPAGE_MASK)
70#define spage_offs(iova) sysmmu_page_offset((iova), SPAGE_SIZE)
KyongHo Cho2a965362012-05-12 05:56:09 +090071
72#define NUM_LV1ENTRIES 4096
Cho KyongHod09d78f2014-05-12 11:44:58 +053073#define NUM_LV2ENTRIES (SECT_SIZE / SPAGE_SIZE)
KyongHo Cho2a965362012-05-12 05:56:09 +090074
Cho KyongHod09d78f2014-05-12 11:44:58 +053075static u32 lv1ent_offset(sysmmu_iova_t iova)
76{
77 return iova >> SECT_ORDER;
78}
79
80static u32 lv2ent_offset(sysmmu_iova_t iova)
81{
82 return (iova >> SPAGE_ORDER) & (NUM_LV2ENTRIES - 1);
83}
84
85#define LV2TABLE_SIZE (NUM_LV2ENTRIES * sizeof(sysmmu_pte_t))
KyongHo Cho2a965362012-05-12 05:56:09 +090086
87#define SPAGES_PER_LPAGE (LPAGE_SIZE / SPAGE_SIZE)
88
89#define lv2table_base(sent) (*(sent) & 0xFFFFFC00)
90
91#define mk_lv1ent_sect(pa) ((pa) | 2)
92#define mk_lv1ent_page(pa) ((pa) | 1)
93#define mk_lv2ent_lpage(pa) ((pa) | 1)
94#define mk_lv2ent_spage(pa) ((pa) | 2)
95
96#define CTRL_ENABLE 0x5
97#define CTRL_BLOCK 0x7
98#define CTRL_DISABLE 0x0
99
Cho KyongHoeeb51842014-05-12 11:45:03 +0530100#define CFG_LRU 0x1
101#define CFG_QOS(n) ((n & 0xF) << 7)
102#define CFG_MASK 0x0150FFFF /* Selecting bit 0-15, 20, 22 and 24 */
103#define CFG_ACGEN (1 << 24) /* System MMU 3.3 only */
104#define CFG_SYSSEL (1 << 22) /* System MMU 3.2 only */
105#define CFG_FLPDCACHE (1 << 20) /* System MMU 3.2+ only */
106
KyongHo Cho2a965362012-05-12 05:56:09 +0900107#define REG_MMU_CTRL 0x000
108#define REG_MMU_CFG 0x004
109#define REG_MMU_STATUS 0x008
110#define REG_MMU_FLUSH 0x00C
111#define REG_MMU_FLUSH_ENTRY 0x010
112#define REG_PT_BASE_ADDR 0x014
113#define REG_INT_STATUS 0x018
114#define REG_INT_CLEAR 0x01C
115
116#define REG_PAGE_FAULT_ADDR 0x024
117#define REG_AW_FAULT_ADDR 0x028
118#define REG_AR_FAULT_ADDR 0x02C
119#define REG_DEFAULT_SLAVE_ADDR 0x030
120
121#define REG_MMU_VERSION 0x034
122
Cho KyongHoeeb51842014-05-12 11:45:03 +0530123#define MMU_MAJ_VER(val) ((val) >> 7)
124#define MMU_MIN_VER(val) ((val) & 0x7F)
125#define MMU_RAW_VER(reg) (((reg) >> 21) & ((1 << 11) - 1)) /* 11 bits */
126
127#define MAKE_MMU_VER(maj, min) ((((maj) & 0xF) << 7) | ((min) & 0x7F))
128
KyongHo Cho2a965362012-05-12 05:56:09 +0900129#define REG_PB0_SADDR 0x04C
130#define REG_PB0_EADDR 0x050
131#define REG_PB1_SADDR 0x054
132#define REG_PB1_EADDR 0x058
133
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530134#define has_sysmmu(dev) (dev->archdata.iommu != NULL)
135
Cho KyongHo734c3c72014-05-12 11:44:48 +0530136static struct kmem_cache *lv2table_kmem_cache;
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530137static sysmmu_pte_t *zero_lv2_table;
138#define ZERO_LV2LINK mk_lv1ent_page(virt_to_phys(zero_lv2_table))
Cho KyongHo734c3c72014-05-12 11:44:48 +0530139
Cho KyongHod09d78f2014-05-12 11:44:58 +0530140static sysmmu_pte_t *section_entry(sysmmu_pte_t *pgtable, sysmmu_iova_t iova)
KyongHo Cho2a965362012-05-12 05:56:09 +0900141{
142 return pgtable + lv1ent_offset(iova);
143}
144
Cho KyongHod09d78f2014-05-12 11:44:58 +0530145static sysmmu_pte_t *page_entry(sysmmu_pte_t *sent, sysmmu_iova_t iova)
KyongHo Cho2a965362012-05-12 05:56:09 +0900146{
Cho KyongHod09d78f2014-05-12 11:44:58 +0530147 return (sysmmu_pte_t *)phys_to_virt(
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530148 lv2table_base(sent)) + lv2ent_offset(iova);
KyongHo Cho2a965362012-05-12 05:56:09 +0900149}
150
151enum exynos_sysmmu_inttype {
152 SYSMMU_PAGEFAULT,
153 SYSMMU_AR_MULTIHIT,
154 SYSMMU_AW_MULTIHIT,
155 SYSMMU_BUSERROR,
156 SYSMMU_AR_SECURITY,
157 SYSMMU_AR_ACCESS,
158 SYSMMU_AW_SECURITY,
159 SYSMMU_AW_PROTECTION, /* 7 */
160 SYSMMU_FAULT_UNKNOWN,
161 SYSMMU_FAULTS_NUM
162};
163
KyongHo Cho2a965362012-05-12 05:56:09 +0900164static unsigned short fault_reg_offset[SYSMMU_FAULTS_NUM] = {
165 REG_PAGE_FAULT_ADDR,
166 REG_AR_FAULT_ADDR,
167 REG_AW_FAULT_ADDR,
168 REG_DEFAULT_SLAVE_ADDR,
169 REG_AR_FAULT_ADDR,
170 REG_AR_FAULT_ADDR,
171 REG_AW_FAULT_ADDR,
172 REG_AW_FAULT_ADDR
173};
174
175static char *sysmmu_fault_name[SYSMMU_FAULTS_NUM] = {
176 "PAGE FAULT",
177 "AR MULTI-HIT FAULT",
178 "AW MULTI-HIT FAULT",
179 "BUS ERROR",
180 "AR SECURITY PROTECTION FAULT",
181 "AR ACCESS PROTECTION FAULT",
182 "AW SECURITY PROTECTION FAULT",
183 "AW ACCESS PROTECTION FAULT",
184 "UNKNOWN FAULT"
185};
186
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530187/* attached to dev.archdata.iommu of the master device */
188struct exynos_iommu_owner {
189 struct list_head client; /* entry of exynos_iommu_domain.clients */
190 struct device *dev;
191 struct device *sysmmu;
192 struct iommu_domain *domain;
193 void *vmm_data; /* IO virtual memory manager's data */
194 spinlock_t lock; /* Lock to preserve consistency of System MMU */
195};
196
KyongHo Cho2a965362012-05-12 05:56:09 +0900197struct exynos_iommu_domain {
198 struct list_head clients; /* list of sysmmu_drvdata.node */
Cho KyongHod09d78f2014-05-12 11:44:58 +0530199 sysmmu_pte_t *pgtable; /* lv1 page table, 16KB */
KyongHo Cho2a965362012-05-12 05:56:09 +0900200 short *lv2entcnt; /* free lv2 entry counter for each section */
201 spinlock_t lock; /* lock for this structure */
202 spinlock_t pgtablelock; /* lock for modifying page table @ pgtable */
203};
204
205struct sysmmu_drvdata {
KyongHo Cho2a965362012-05-12 05:56:09 +0900206 struct device *sysmmu; /* System MMU's device descriptor */
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530207 struct device *master; /* Owner of system MMU */
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530208 void __iomem *sfrbase;
209 struct clk *clk;
Cho KyongHo70605872014-05-12 11:44:55 +0530210 struct clk *clk_master;
KyongHo Cho2a965362012-05-12 05:56:09 +0900211 int activations;
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530212 spinlock_t lock;
KyongHo Cho2a965362012-05-12 05:56:09 +0900213 struct iommu_domain *domain;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530214 phys_addr_t pgtable;
KyongHo Cho2a965362012-05-12 05:56:09 +0900215};
216
217static bool set_sysmmu_active(struct sysmmu_drvdata *data)
218{
219 /* return true if the System MMU was not active previously
220 and it needs to be initialized */
221 return ++data->activations == 1;
222}
223
224static bool set_sysmmu_inactive(struct sysmmu_drvdata *data)
225{
226 /* return true if the System MMU is needed to be disabled */
227 BUG_ON(data->activations < 1);
228 return --data->activations == 0;
229}
230
231static bool is_sysmmu_active(struct sysmmu_drvdata *data)
232{
233 return data->activations > 0;
234}
235
236static void sysmmu_unblock(void __iomem *sfrbase)
237{
238 __raw_writel(CTRL_ENABLE, sfrbase + REG_MMU_CTRL);
239}
240
Cho KyongHoeeb51842014-05-12 11:45:03 +0530241static unsigned int __raw_sysmmu_version(struct sysmmu_drvdata *data)
242{
243 return MMU_RAW_VER(__raw_readl(data->sfrbase + REG_MMU_VERSION));
244}
245
KyongHo Cho2a965362012-05-12 05:56:09 +0900246static bool sysmmu_block(void __iomem *sfrbase)
247{
248 int i = 120;
249
250 __raw_writel(CTRL_BLOCK, sfrbase + REG_MMU_CTRL);
251 while ((i > 0) && !(__raw_readl(sfrbase + REG_MMU_STATUS) & 1))
252 --i;
253
254 if (!(__raw_readl(sfrbase + REG_MMU_STATUS) & 1)) {
255 sysmmu_unblock(sfrbase);
256 return false;
257 }
258
259 return true;
260}
261
262static void __sysmmu_tlb_invalidate(void __iomem *sfrbase)
263{
264 __raw_writel(0x1, sfrbase + REG_MMU_FLUSH);
265}
266
267static void __sysmmu_tlb_invalidate_entry(void __iomem *sfrbase,
Cho KyongHod09d78f2014-05-12 11:44:58 +0530268 sysmmu_iova_t iova, unsigned int num_inv)
KyongHo Cho2a965362012-05-12 05:56:09 +0900269{
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530270 unsigned int i;
271 for (i = 0; i < num_inv; i++) {
272 __raw_writel((iova & SPAGE_MASK) | 1,
273 sfrbase + REG_MMU_FLUSH_ENTRY);
274 iova += SPAGE_SIZE;
275 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900276}
277
278static void __sysmmu_set_ptbase(void __iomem *sfrbase,
Cho KyongHod09d78f2014-05-12 11:44:58 +0530279 phys_addr_t pgd)
KyongHo Cho2a965362012-05-12 05:56:09 +0900280{
KyongHo Cho2a965362012-05-12 05:56:09 +0900281 __raw_writel(pgd, sfrbase + REG_PT_BASE_ADDR);
282
283 __sysmmu_tlb_invalidate(sfrbase);
284}
285
Cho KyongHo1fab7fa2014-05-12 11:44:56 +0530286static void show_fault_information(const char *name,
287 enum exynos_sysmmu_inttype itype,
Cho KyongHod09d78f2014-05-12 11:44:58 +0530288 phys_addr_t pgtable_base, sysmmu_iova_t fault_addr)
KyongHo Cho2a965362012-05-12 05:56:09 +0900289{
Cho KyongHod09d78f2014-05-12 11:44:58 +0530290 sysmmu_pte_t *ent;
KyongHo Cho2a965362012-05-12 05:56:09 +0900291
292 if ((itype >= SYSMMU_FAULTS_NUM) || (itype < SYSMMU_PAGEFAULT))
293 itype = SYSMMU_FAULT_UNKNOWN;
294
Cho KyongHod09d78f2014-05-12 11:44:58 +0530295 pr_err("%s occurred at %#x by %s(Page table base: %pa)\n",
Cho KyongHo1fab7fa2014-05-12 11:44:56 +0530296 sysmmu_fault_name[itype], fault_addr, name, &pgtable_base);
KyongHo Cho2a965362012-05-12 05:56:09 +0900297
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530298 ent = section_entry(phys_to_virt(pgtable_base), fault_addr);
Cho KyongHod09d78f2014-05-12 11:44:58 +0530299 pr_err("\tLv1 entry: %#x\n", *ent);
KyongHo Cho2a965362012-05-12 05:56:09 +0900300
301 if (lv1ent_page(ent)) {
302 ent = page_entry(ent, fault_addr);
Cho KyongHod09d78f2014-05-12 11:44:58 +0530303 pr_err("\t Lv2 entry: %#x\n", *ent);
KyongHo Cho2a965362012-05-12 05:56:09 +0900304 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900305}
306
307static irqreturn_t exynos_sysmmu_irq(int irq, void *dev_id)
308{
309 /* SYSMMU is in blocked when interrupt occurred. */
310 struct sysmmu_drvdata *data = dev_id;
KyongHo Cho2a965362012-05-12 05:56:09 +0900311 enum exynos_sysmmu_inttype itype;
Cho KyongHod09d78f2014-05-12 11:44:58 +0530312 sysmmu_iova_t addr = -1;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530313 int ret = -ENOSYS;
KyongHo Cho2a965362012-05-12 05:56:09 +0900314
KyongHo Cho2a965362012-05-12 05:56:09 +0900315 WARN_ON(!is_sysmmu_active(data));
316
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530317 spin_lock(&data->lock);
318
Cho KyongHo70605872014-05-12 11:44:55 +0530319 if (!IS_ERR(data->clk_master))
320 clk_enable(data->clk_master);
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530321
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530322 itype = (enum exynos_sysmmu_inttype)
323 __ffs(__raw_readl(data->sfrbase + REG_INT_STATUS));
324 if (WARN_ON(!((itype >= 0) && (itype < SYSMMU_FAULT_UNKNOWN))))
KyongHo Cho2a965362012-05-12 05:56:09 +0900325 itype = SYSMMU_FAULT_UNKNOWN;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530326 else
327 addr = __raw_readl(data->sfrbase + fault_reg_offset[itype]);
KyongHo Cho2a965362012-05-12 05:56:09 +0900328
Cho KyongHo1fab7fa2014-05-12 11:44:56 +0530329 if (itype == SYSMMU_FAULT_UNKNOWN) {
330 pr_err("%s: Fault is not occurred by System MMU '%s'!\n",
331 __func__, dev_name(data->sysmmu));
332 pr_err("%s: Please check if IRQ is correctly configured.\n",
333 __func__);
334 BUG();
335 } else {
Cho KyongHod09d78f2014-05-12 11:44:58 +0530336 unsigned int base =
Cho KyongHo1fab7fa2014-05-12 11:44:56 +0530337 __raw_readl(data->sfrbase + REG_PT_BASE_ADDR);
338 show_fault_information(dev_name(data->sysmmu),
339 itype, base, addr);
340 if (data->domain)
341 ret = report_iommu_fault(data->domain,
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530342 data->master, addr, itype);
KyongHo Cho2a965362012-05-12 05:56:09 +0900343 }
344
Cho KyongHo1fab7fa2014-05-12 11:44:56 +0530345 /* fault is not recovered by fault handler */
346 BUG_ON(ret != 0);
KyongHo Cho2a965362012-05-12 05:56:09 +0900347
Cho KyongHo1fab7fa2014-05-12 11:44:56 +0530348 __raw_writel(1 << itype, data->sfrbase + REG_INT_CLEAR);
349
350 sysmmu_unblock(data->sfrbase);
KyongHo Cho2a965362012-05-12 05:56:09 +0900351
Cho KyongHo70605872014-05-12 11:44:55 +0530352 if (!IS_ERR(data->clk_master))
353 clk_disable(data->clk_master);
354
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530355 spin_unlock(&data->lock);
KyongHo Cho2a965362012-05-12 05:56:09 +0900356
357 return IRQ_HANDLED;
358}
359
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530360static void __sysmmu_disable_nocount(struct sysmmu_drvdata *data)
KyongHo Cho2a965362012-05-12 05:56:09 +0900361{
Cho KyongHo70605872014-05-12 11:44:55 +0530362 if (!IS_ERR(data->clk_master))
363 clk_enable(data->clk_master);
364
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530365 __raw_writel(CTRL_DISABLE, data->sfrbase + REG_MMU_CTRL);
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530366 __raw_writel(0, data->sfrbase + REG_MMU_CFG);
KyongHo Cho2a965362012-05-12 05:56:09 +0900367
Cho KyongHo46c16d12014-05-12 11:44:54 +0530368 clk_disable(data->clk);
Cho KyongHo70605872014-05-12 11:44:55 +0530369 if (!IS_ERR(data->clk_master))
370 clk_disable(data->clk_master);
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530371}
KyongHo Cho2a965362012-05-12 05:56:09 +0900372
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530373static bool __sysmmu_disable(struct sysmmu_drvdata *data)
374{
375 bool disabled;
376 unsigned long flags;
377
378 spin_lock_irqsave(&data->lock, flags);
379
380 disabled = set_sysmmu_inactive(data);
381
382 if (disabled) {
383 data->pgtable = 0;
384 data->domain = NULL;
385
386 __sysmmu_disable_nocount(data);
387
388 dev_dbg(data->sysmmu, "Disabled\n");
389 } else {
390 dev_dbg(data->sysmmu, "%d times left to disable\n",
391 data->activations);
392 }
393
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530394 spin_unlock_irqrestore(&data->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900395
KyongHo Cho2a965362012-05-12 05:56:09 +0900396 return disabled;
397}
398
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530399static void __sysmmu_init_config(struct sysmmu_drvdata *data)
400{
Cho KyongHoeeb51842014-05-12 11:45:03 +0530401 unsigned int cfg = CFG_LRU | CFG_QOS(15);
402 unsigned int ver;
403
404 ver = __raw_sysmmu_version(data);
405 if (MMU_MAJ_VER(ver) == 3) {
406 if (MMU_MIN_VER(ver) >= 2) {
407 cfg |= CFG_FLPDCACHE;
408 if (MMU_MIN_VER(ver) == 3) {
409 cfg |= CFG_ACGEN;
410 cfg &= ~CFG_LRU;
411 } else {
412 cfg |= CFG_SYSSEL;
413 }
414 }
415 }
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530416
417 __raw_writel(cfg, data->sfrbase + REG_MMU_CFG);
418}
419
420static void __sysmmu_enable_nocount(struct sysmmu_drvdata *data)
421{
422 if (!IS_ERR(data->clk_master))
423 clk_enable(data->clk_master);
424 clk_enable(data->clk);
425
426 __raw_writel(CTRL_BLOCK, data->sfrbase + REG_MMU_CTRL);
427
428 __sysmmu_init_config(data);
429
430 __sysmmu_set_ptbase(data->sfrbase, data->pgtable);
431
432 __raw_writel(CTRL_ENABLE, data->sfrbase + REG_MMU_CTRL);
433
434 if (!IS_ERR(data->clk_master))
435 clk_disable(data->clk_master);
436}
437
438static int __sysmmu_enable(struct sysmmu_drvdata *data,
439 phys_addr_t pgtable, struct iommu_domain *domain)
440{
441 int ret = 0;
442 unsigned long flags;
443
444 spin_lock_irqsave(&data->lock, flags);
445 if (set_sysmmu_active(data)) {
446 data->pgtable = pgtable;
447 data->domain = domain;
448
449 __sysmmu_enable_nocount(data);
450
451 dev_dbg(data->sysmmu, "Enabled\n");
452 } else {
453 ret = (pgtable == data->pgtable) ? 1 : -EBUSY;
454
455 dev_dbg(data->sysmmu, "already enabled\n");
456 }
457
458 if (WARN_ON(ret < 0))
459 set_sysmmu_inactive(data); /* decrement count */
460
461 spin_unlock_irqrestore(&data->lock, flags);
462
463 return ret;
464}
465
KyongHo Cho2a965362012-05-12 05:56:09 +0900466/* __exynos_sysmmu_enable: Enables System MMU
467 *
468 * returns -error if an error occurred and System MMU is not enabled,
469 * 0 if the System MMU has been just enabled and 1 if System MMU was already
470 * enabled before.
471 */
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530472static int __exynos_sysmmu_enable(struct device *dev, phys_addr_t pgtable,
473 struct iommu_domain *domain)
KyongHo Cho2a965362012-05-12 05:56:09 +0900474{
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530475 int ret = 0;
KyongHo Cho2a965362012-05-12 05:56:09 +0900476 unsigned long flags;
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530477 struct exynos_iommu_owner *owner = dev->archdata.iommu;
478 struct sysmmu_drvdata *data;
KyongHo Cho2a965362012-05-12 05:56:09 +0900479
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530480 BUG_ON(!has_sysmmu(dev));
KyongHo Cho2a965362012-05-12 05:56:09 +0900481
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530482 spin_lock_irqsave(&owner->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900483
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530484 data = dev_get_drvdata(owner->sysmmu);
KyongHo Cho2a965362012-05-12 05:56:09 +0900485
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530486 ret = __sysmmu_enable(data, pgtable, domain);
487 if (ret >= 0)
488 data->master = dev;
KyongHo Cho2a965362012-05-12 05:56:09 +0900489
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530490 spin_unlock_irqrestore(&owner->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900491
492 return ret;
493}
494
Cho KyongHod09d78f2014-05-12 11:44:58 +0530495int exynos_sysmmu_enable(struct device *dev, phys_addr_t pgtable)
KyongHo Cho2a965362012-05-12 05:56:09 +0900496{
KyongHo Cho2a965362012-05-12 05:56:09 +0900497 BUG_ON(!memblock_is_memory(pgtable));
498
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530499 return __exynos_sysmmu_enable(dev, pgtable, NULL);
KyongHo Cho2a965362012-05-12 05:56:09 +0900500}
501
Sachin Kamat77e38352013-02-06 13:55:17 +0530502static bool exynos_sysmmu_disable(struct device *dev)
KyongHo Cho2a965362012-05-12 05:56:09 +0900503{
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530504 unsigned long flags;
505 bool disabled = true;
506 struct exynos_iommu_owner *owner = dev->archdata.iommu;
507 struct sysmmu_drvdata *data;
KyongHo Cho2a965362012-05-12 05:56:09 +0900508
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530509 BUG_ON(!has_sysmmu(dev));
510
511 spin_lock_irqsave(&owner->lock, flags);
512
513 data = dev_get_drvdata(owner->sysmmu);
514
515 disabled = __sysmmu_disable(data);
516 if (disabled)
517 data->master = NULL;
518
519 spin_unlock_irqrestore(&owner->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900520
521 return disabled;
522}
523
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530524static void __sysmmu_tlb_invalidate_flpdcache(struct sysmmu_drvdata *data,
525 sysmmu_iova_t iova)
526{
527 if (__raw_sysmmu_version(data) == MAKE_MMU_VER(3, 3))
528 __raw_writel(iova | 0x1, data->sfrbase + REG_MMU_FLUSH_ENTRY);
529}
530
531static void sysmmu_tlb_invalidate_flpdcache(struct device *dev,
532 sysmmu_iova_t iova)
533{
534 unsigned long flags;
535 struct exynos_iommu_owner *owner = dev->archdata.iommu;
536 struct sysmmu_drvdata *data = dev_get_drvdata(owner->sysmmu);
537
538 if (!IS_ERR(data->clk_master))
539 clk_enable(data->clk_master);
540
541 spin_lock_irqsave(&data->lock, flags);
542 if (is_sysmmu_active(data))
543 __sysmmu_tlb_invalidate_flpdcache(data, iova);
544 spin_unlock_irqrestore(&data->lock, flags);
545
546 if (!IS_ERR(data->clk_master))
547 clk_disable(data->clk_master);
548}
549
Cho KyongHod09d78f2014-05-12 11:44:58 +0530550static void sysmmu_tlb_invalidate_entry(struct device *dev, sysmmu_iova_t iova,
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530551 size_t size)
KyongHo Cho2a965362012-05-12 05:56:09 +0900552{
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530553 struct exynos_iommu_owner *owner = dev->archdata.iommu;
KyongHo Cho2a965362012-05-12 05:56:09 +0900554 unsigned long flags;
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530555 struct sysmmu_drvdata *data;
556
557 data = dev_get_drvdata(owner->sysmmu);
KyongHo Cho2a965362012-05-12 05:56:09 +0900558
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530559 spin_lock_irqsave(&data->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900560 if (is_sysmmu_active(data)) {
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530561 unsigned int num_inv = 1;
Cho KyongHo70605872014-05-12 11:44:55 +0530562
563 if (!IS_ERR(data->clk_master))
564 clk_enable(data->clk_master);
565
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530566 /*
567 * L2TLB invalidation required
568 * 4KB page: 1 invalidation
569 * 64KB page: 16 invalidation
570 * 1MB page: 64 invalidation
571 * because it is set-associative TLB
572 * with 8-way and 64 sets.
573 * 1MB page can be cached in one of all sets.
574 * 64KB page can be one of 16 consecutive sets.
575 */
Cho KyongHoeeb51842014-05-12 11:45:03 +0530576 if (MMU_MAJ_VER(__raw_sysmmu_version(data)) == 2)
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530577 num_inv = min_t(unsigned int, size / PAGE_SIZE, 64);
578
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530579 if (sysmmu_block(data->sfrbase)) {
580 __sysmmu_tlb_invalidate_entry(
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530581 data->sfrbase, iova, num_inv);
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530582 sysmmu_unblock(data->sfrbase);
KyongHo Cho2a965362012-05-12 05:56:09 +0900583 }
Cho KyongHo70605872014-05-12 11:44:55 +0530584 if (!IS_ERR(data->clk_master))
585 clk_disable(data->clk_master);
KyongHo Cho2a965362012-05-12 05:56:09 +0900586 } else {
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530587 dev_dbg(dev, "disabled. Skipping TLB invalidation @ %#x\n",
588 iova);
KyongHo Cho2a965362012-05-12 05:56:09 +0900589 }
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530590 spin_unlock_irqrestore(&data->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900591}
592
593void exynos_sysmmu_tlb_invalidate(struct device *dev)
594{
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530595 struct exynos_iommu_owner *owner = dev->archdata.iommu;
KyongHo Cho2a965362012-05-12 05:56:09 +0900596 unsigned long flags;
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530597 struct sysmmu_drvdata *data;
598
599 data = dev_get_drvdata(owner->sysmmu);
KyongHo Cho2a965362012-05-12 05:56:09 +0900600
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530601 spin_lock_irqsave(&data->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900602 if (is_sysmmu_active(data)) {
Cho KyongHo70605872014-05-12 11:44:55 +0530603 if (!IS_ERR(data->clk_master))
604 clk_enable(data->clk_master);
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530605 if (sysmmu_block(data->sfrbase)) {
606 __sysmmu_tlb_invalidate(data->sfrbase);
607 sysmmu_unblock(data->sfrbase);
KyongHo Cho2a965362012-05-12 05:56:09 +0900608 }
Cho KyongHo70605872014-05-12 11:44:55 +0530609 if (!IS_ERR(data->clk_master))
610 clk_disable(data->clk_master);
KyongHo Cho2a965362012-05-12 05:56:09 +0900611 } else {
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530612 dev_dbg(dev, "disabled. Skipping TLB invalidation\n");
KyongHo Cho2a965362012-05-12 05:56:09 +0900613 }
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530614 spin_unlock_irqrestore(&data->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900615}
616
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530617static int __init exynos_sysmmu_probe(struct platform_device *pdev)
KyongHo Cho2a965362012-05-12 05:56:09 +0900618{
Cho KyongHo46c16d12014-05-12 11:44:54 +0530619 int irq, ret;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530620 struct device *dev = &pdev->dev;
KyongHo Cho2a965362012-05-12 05:56:09 +0900621 struct sysmmu_drvdata *data;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530622 struct resource *res;
KyongHo Cho2a965362012-05-12 05:56:09 +0900623
Cho KyongHo46c16d12014-05-12 11:44:54 +0530624 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
625 if (!data)
626 return -ENOMEM;
KyongHo Cho2a965362012-05-12 05:56:09 +0900627
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530628 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Cho KyongHo46c16d12014-05-12 11:44:54 +0530629 data->sfrbase = devm_ioremap_resource(dev, res);
630 if (IS_ERR(data->sfrbase))
631 return PTR_ERR(data->sfrbase);
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530632
Cho KyongHo46c16d12014-05-12 11:44:54 +0530633 irq = platform_get_irq(pdev, 0);
634 if (irq <= 0) {
Cho KyongHo0bf4e542014-05-12 11:45:00 +0530635 dev_err(dev, "Unable to find IRQ resource\n");
Cho KyongHo46c16d12014-05-12 11:44:54 +0530636 return irq;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530637 }
638
Cho KyongHo46c16d12014-05-12 11:44:54 +0530639 ret = devm_request_irq(dev, irq, exynos_sysmmu_irq, 0,
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530640 dev_name(dev), data);
KyongHo Cho2a965362012-05-12 05:56:09 +0900641 if (ret) {
Cho KyongHo46c16d12014-05-12 11:44:54 +0530642 dev_err(dev, "Unabled to register handler of irq %d\n", irq);
643 return ret;
KyongHo Cho2a965362012-05-12 05:56:09 +0900644 }
645
Cho KyongHo46c16d12014-05-12 11:44:54 +0530646 data->clk = devm_clk_get(dev, "sysmmu");
647 if (IS_ERR(data->clk)) {
648 dev_err(dev, "Failed to get clock!\n");
649 return PTR_ERR(data->clk);
650 } else {
651 ret = clk_prepare(data->clk);
652 if (ret) {
653 dev_err(dev, "Failed to prepare clk\n");
654 return ret;
655 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900656 }
657
Cho KyongHo70605872014-05-12 11:44:55 +0530658 data->clk_master = devm_clk_get(dev, "master");
659 if (!IS_ERR(data->clk_master)) {
660 ret = clk_prepare(data->clk_master);
661 if (ret) {
662 clk_unprepare(data->clk);
663 dev_err(dev, "Failed to prepare master's clk\n");
664 return ret;
665 }
666 }
667
KyongHo Cho2a965362012-05-12 05:56:09 +0900668 data->sysmmu = dev;
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530669 spin_lock_init(&data->lock);
KyongHo Cho2a965362012-05-12 05:56:09 +0900670
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530671 platform_set_drvdata(pdev, data);
672
Cho KyongHof4723ec2014-05-12 11:44:52 +0530673 pm_runtime_enable(dev);
KyongHo Cho2a965362012-05-12 05:56:09 +0900674
KyongHo Cho2a965362012-05-12 05:56:09 +0900675 return 0;
KyongHo Cho2a965362012-05-12 05:56:09 +0900676}
677
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530678static const struct of_device_id sysmmu_of_match[] __initconst = {
679 { .compatible = "samsung,exynos-sysmmu", },
680 { },
681};
682
683static struct platform_driver exynos_sysmmu_driver __refdata = {
684 .probe = exynos_sysmmu_probe,
685 .driver = {
KyongHo Cho2a965362012-05-12 05:56:09 +0900686 .owner = THIS_MODULE,
687 .name = "exynos-sysmmu",
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530688 .of_match_table = sysmmu_of_match,
KyongHo Cho2a965362012-05-12 05:56:09 +0900689 }
690};
691
692static inline void pgtable_flush(void *vastart, void *vaend)
693{
694 dmac_flush_range(vastart, vaend);
695 outer_flush_range(virt_to_phys(vastart),
696 virt_to_phys(vaend));
697}
698
699static int exynos_iommu_domain_init(struct iommu_domain *domain)
700{
701 struct exynos_iommu_domain *priv;
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530702 int i;
KyongHo Cho2a965362012-05-12 05:56:09 +0900703
704 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
705 if (!priv)
706 return -ENOMEM;
707
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530708 priv->pgtable = (sysmmu_pte_t *)__get_free_pages(GFP_KERNEL, 2);
KyongHo Cho2a965362012-05-12 05:56:09 +0900709 if (!priv->pgtable)
710 goto err_pgtable;
711
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530712 priv->lv2entcnt = (short *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1);
KyongHo Cho2a965362012-05-12 05:56:09 +0900713 if (!priv->lv2entcnt)
714 goto err_counter;
715
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530716 /* w/a of System MMU v3.3 to prevent caching 1MiB mapping */
717 for (i = 0; i < NUM_LV1ENTRIES; i += 8) {
718 priv->pgtable[i + 0] = ZERO_LV2LINK;
719 priv->pgtable[i + 1] = ZERO_LV2LINK;
720 priv->pgtable[i + 2] = ZERO_LV2LINK;
721 priv->pgtable[i + 3] = ZERO_LV2LINK;
722 priv->pgtable[i + 4] = ZERO_LV2LINK;
723 priv->pgtable[i + 5] = ZERO_LV2LINK;
724 priv->pgtable[i + 6] = ZERO_LV2LINK;
725 priv->pgtable[i + 7] = ZERO_LV2LINK;
726 }
727
KyongHo Cho2a965362012-05-12 05:56:09 +0900728 pgtable_flush(priv->pgtable, priv->pgtable + NUM_LV1ENTRIES);
729
730 spin_lock_init(&priv->lock);
731 spin_lock_init(&priv->pgtablelock);
732 INIT_LIST_HEAD(&priv->clients);
733
Sachin Kamateb516372012-08-01 14:35:17 +0530734 domain->geometry.aperture_start = 0;
735 domain->geometry.aperture_end = ~0UL;
736 domain->geometry.force_aperture = true;
Joerg Roedel3177bb72012-07-11 12:41:10 +0200737
KyongHo Cho2a965362012-05-12 05:56:09 +0900738 domain->priv = priv;
739 return 0;
740
741err_counter:
742 free_pages((unsigned long)priv->pgtable, 2);
743err_pgtable:
744 kfree(priv);
745 return -ENOMEM;
746}
747
748static void exynos_iommu_domain_destroy(struct iommu_domain *domain)
749{
750 struct exynos_iommu_domain *priv = domain->priv;
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530751 struct exynos_iommu_owner *owner;
KyongHo Cho2a965362012-05-12 05:56:09 +0900752 unsigned long flags;
753 int i;
754
755 WARN_ON(!list_empty(&priv->clients));
756
757 spin_lock_irqsave(&priv->lock, flags);
758
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530759 list_for_each_entry(owner, &priv->clients, client) {
760 while (!exynos_sysmmu_disable(owner->dev))
KyongHo Cho2a965362012-05-12 05:56:09 +0900761 ; /* until System MMU is actually disabled */
762 }
763
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530764 while (!list_empty(&priv->clients))
765 list_del_init(priv->clients.next);
766
KyongHo Cho2a965362012-05-12 05:56:09 +0900767 spin_unlock_irqrestore(&priv->lock, flags);
768
769 for (i = 0; i < NUM_LV1ENTRIES; i++)
770 if (lv1ent_page(priv->pgtable + i))
Cho KyongHo734c3c72014-05-12 11:44:48 +0530771 kmem_cache_free(lv2table_kmem_cache,
772 phys_to_virt(lv2table_base(priv->pgtable + i)));
KyongHo Cho2a965362012-05-12 05:56:09 +0900773
774 free_pages((unsigned long)priv->pgtable, 2);
775 free_pages((unsigned long)priv->lv2entcnt, 1);
776 kfree(domain->priv);
777 domain->priv = NULL;
778}
779
780static int exynos_iommu_attach_device(struct iommu_domain *domain,
781 struct device *dev)
782{
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530783 struct exynos_iommu_owner *owner = dev->archdata.iommu;
KyongHo Cho2a965362012-05-12 05:56:09 +0900784 struct exynos_iommu_domain *priv = domain->priv;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530785 phys_addr_t pagetable = virt_to_phys(priv->pgtable);
KyongHo Cho2a965362012-05-12 05:56:09 +0900786 unsigned long flags;
787 int ret;
788
KyongHo Cho2a965362012-05-12 05:56:09 +0900789 spin_lock_irqsave(&priv->lock, flags);
790
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530791 ret = __exynos_sysmmu_enable(dev, pagetable, domain);
KyongHo Cho2a965362012-05-12 05:56:09 +0900792 if (ret == 0) {
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530793 list_add_tail(&owner->client, &priv->clients);
794 owner->domain = domain;
KyongHo Cho2a965362012-05-12 05:56:09 +0900795 }
796
797 spin_unlock_irqrestore(&priv->lock, flags);
798
799 if (ret < 0) {
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530800 dev_err(dev, "%s: Failed to attach IOMMU with pgtable %pa\n",
801 __func__, &pagetable);
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530802 return ret;
KyongHo Cho2a965362012-05-12 05:56:09 +0900803 }
804
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530805 dev_dbg(dev, "%s: Attached IOMMU with pgtable %pa %s\n",
806 __func__, &pagetable, (ret == 0) ? "" : ", again");
807
KyongHo Cho2a965362012-05-12 05:56:09 +0900808 return ret;
809}
810
811static void exynos_iommu_detach_device(struct iommu_domain *domain,
812 struct device *dev)
813{
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530814 struct exynos_iommu_owner *owner;
KyongHo Cho2a965362012-05-12 05:56:09 +0900815 struct exynos_iommu_domain *priv = domain->priv;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530816 phys_addr_t pagetable = virt_to_phys(priv->pgtable);
KyongHo Cho2a965362012-05-12 05:56:09 +0900817 unsigned long flags;
KyongHo Cho2a965362012-05-12 05:56:09 +0900818
819 spin_lock_irqsave(&priv->lock, flags);
820
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530821 list_for_each_entry(owner, &priv->clients, client) {
822 if (owner == dev->archdata.iommu) {
823 if (exynos_sysmmu_disable(dev)) {
824 list_del_init(&owner->client);
825 owner->domain = NULL;
826 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900827 break;
828 }
829 }
830
KyongHo Cho2a965362012-05-12 05:56:09 +0900831 spin_unlock_irqrestore(&priv->lock, flags);
832
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530833 if (owner == dev->archdata.iommu)
834 dev_dbg(dev, "%s: Detached IOMMU with pgtable %pa\n",
835 __func__, &pagetable);
836 else
837 dev_err(dev, "%s: No IOMMU is attached\n", __func__);
KyongHo Cho2a965362012-05-12 05:56:09 +0900838}
839
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530840static sysmmu_pte_t *alloc_lv2entry(struct exynos_iommu_domain *priv,
841 sysmmu_pte_t *sent, sysmmu_iova_t iova, short *pgcounter)
KyongHo Cho2a965362012-05-12 05:56:09 +0900842{
Cho KyongHo61128f02014-05-12 11:44:47 +0530843 if (lv1ent_section(sent)) {
Cho KyongHod09d78f2014-05-12 11:44:58 +0530844 WARN(1, "Trying mapping on %#08x mapped with 1MiB page", iova);
Cho KyongHo61128f02014-05-12 11:44:47 +0530845 return ERR_PTR(-EADDRINUSE);
846 }
847
KyongHo Cho2a965362012-05-12 05:56:09 +0900848 if (lv1ent_fault(sent)) {
Cho KyongHod09d78f2014-05-12 11:44:58 +0530849 sysmmu_pte_t *pent;
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530850 bool need_flush_flpd_cache = lv1ent_zero(sent);
KyongHo Cho2a965362012-05-12 05:56:09 +0900851
Cho KyongHo734c3c72014-05-12 11:44:48 +0530852 pent = kmem_cache_zalloc(lv2table_kmem_cache, GFP_ATOMIC);
Cho KyongHod09d78f2014-05-12 11:44:58 +0530853 BUG_ON((unsigned int)pent & (LV2TABLE_SIZE - 1));
KyongHo Cho2a965362012-05-12 05:56:09 +0900854 if (!pent)
Cho KyongHo61128f02014-05-12 11:44:47 +0530855 return ERR_PTR(-ENOMEM);
KyongHo Cho2a965362012-05-12 05:56:09 +0900856
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530857 *sent = mk_lv1ent_page(virt_to_phys(pent));
KyongHo Cho2a965362012-05-12 05:56:09 +0900858 *pgcounter = NUM_LV2ENTRIES;
859 pgtable_flush(pent, pent + NUM_LV2ENTRIES);
860 pgtable_flush(sent, sent + 1);
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530861
862 /*
863 * If pretched SLPD is a fault SLPD in zero_l2_table, FLPD cache
864 * may caches the address of zero_l2_table. This function
865 * replaces the zero_l2_table with new L2 page table to write
866 * valid mappings.
867 * Accessing the valid area may cause page fault since FLPD
868 * cache may still caches zero_l2_table for the valid area
869 * instead of new L2 page table that have the mapping
870 * information of the valid area
871 * Thus any replacement of zero_l2_table with other valid L2
872 * page table must involve FLPD cache invalidation for System
873 * MMU v3.3.
874 * FLPD cache invalidation is performed with TLB invalidation
875 * by VPN without blocking. It is safe to invalidate TLB without
876 * blocking because the target address of TLB invalidation is
877 * not currently mapped.
878 */
879 if (need_flush_flpd_cache) {
880 struct exynos_iommu_owner *owner;
881 spin_lock(&priv->lock);
882 list_for_each_entry(owner, &priv->clients, client)
883 sysmmu_tlb_invalidate_flpdcache(
884 owner->dev, iova);
885 spin_unlock(&priv->lock);
886 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900887 }
888
889 return page_entry(sent, iova);
890}
891
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530892static int lv1set_section(struct exynos_iommu_domain *priv,
893 sysmmu_pte_t *sent, sysmmu_iova_t iova,
Cho KyongHo61128f02014-05-12 11:44:47 +0530894 phys_addr_t paddr, short *pgcnt)
KyongHo Cho2a965362012-05-12 05:56:09 +0900895{
Cho KyongHo61128f02014-05-12 11:44:47 +0530896 if (lv1ent_section(sent)) {
Cho KyongHod09d78f2014-05-12 11:44:58 +0530897 WARN(1, "Trying mapping on 1MiB@%#08x that is mapped",
Cho KyongHo61128f02014-05-12 11:44:47 +0530898 iova);
KyongHo Cho2a965362012-05-12 05:56:09 +0900899 return -EADDRINUSE;
Cho KyongHo61128f02014-05-12 11:44:47 +0530900 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900901
902 if (lv1ent_page(sent)) {
Cho KyongHo61128f02014-05-12 11:44:47 +0530903 if (*pgcnt != NUM_LV2ENTRIES) {
Cho KyongHod09d78f2014-05-12 11:44:58 +0530904 WARN(1, "Trying mapping on 1MiB@%#08x that is mapped",
Cho KyongHo61128f02014-05-12 11:44:47 +0530905 iova);
KyongHo Cho2a965362012-05-12 05:56:09 +0900906 return -EADDRINUSE;
Cho KyongHo61128f02014-05-12 11:44:47 +0530907 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900908
Cho KyongHo734c3c72014-05-12 11:44:48 +0530909 kmem_cache_free(lv2table_kmem_cache, page_entry(sent, 0));
KyongHo Cho2a965362012-05-12 05:56:09 +0900910 *pgcnt = 0;
911 }
912
913 *sent = mk_lv1ent_sect(paddr);
914
915 pgtable_flush(sent, sent + 1);
916
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530917 spin_lock(&priv->lock);
918 if (lv1ent_page_zero(sent)) {
919 struct exynos_iommu_owner *owner;
920 /*
921 * Flushing FLPD cache in System MMU v3.3 that may cache a FLPD
922 * entry by speculative prefetch of SLPD which has no mapping.
923 */
924 list_for_each_entry(owner, &priv->clients, client)
925 sysmmu_tlb_invalidate_flpdcache(owner->dev, iova);
926 }
927 spin_unlock(&priv->lock);
928
KyongHo Cho2a965362012-05-12 05:56:09 +0900929 return 0;
930}
931
Cho KyongHod09d78f2014-05-12 11:44:58 +0530932static int lv2set_page(sysmmu_pte_t *pent, phys_addr_t paddr, size_t size,
KyongHo Cho2a965362012-05-12 05:56:09 +0900933 short *pgcnt)
934{
935 if (size == SPAGE_SIZE) {
Cho KyongHo0bf4e542014-05-12 11:45:00 +0530936 if (WARN_ON(!lv2ent_fault(pent)))
KyongHo Cho2a965362012-05-12 05:56:09 +0900937 return -EADDRINUSE;
938
939 *pent = mk_lv2ent_spage(paddr);
940 pgtable_flush(pent, pent + 1);
941 *pgcnt -= 1;
942 } else { /* size == LPAGE_SIZE */
943 int i;
944 for (i = 0; i < SPAGES_PER_LPAGE; i++, pent++) {
Cho KyongHo0bf4e542014-05-12 11:45:00 +0530945 if (WARN_ON(!lv2ent_fault(pent))) {
Cho KyongHo61128f02014-05-12 11:44:47 +0530946 if (i > 0)
947 memset(pent - i, 0, sizeof(*pent) * i);
KyongHo Cho2a965362012-05-12 05:56:09 +0900948 return -EADDRINUSE;
949 }
950
951 *pent = mk_lv2ent_lpage(paddr);
952 }
953 pgtable_flush(pent - SPAGES_PER_LPAGE, pent);
954 *pgcnt -= SPAGES_PER_LPAGE;
955 }
956
957 return 0;
958}
959
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530960/*
961 * *CAUTION* to the I/O virtual memory managers that support exynos-iommu:
962 *
963 * System MMU v3.x have an advanced logic to improve address translation
964 * performance with caching more page table entries by a page table walk.
965 * However, the logic has a bug that caching fault page table entries and System
966 * MMU reports page fault if the cached fault entry is hit even though the fault
967 * entry is updated to a valid entry after the entry is cached.
968 * To prevent caching fault page table entries which may be updated to valid
969 * entries later, the virtual memory manager should care about the w/a about the
970 * problem. The followings describe w/a.
971 *
972 * Any two consecutive I/O virtual address regions must have a hole of 128KiB
973 * in maximum to prevent misbehavior of System MMU 3.x. (w/a of h/w bug)
974 *
975 * Precisely, any start address of I/O virtual region must be aligned by
976 * the following sizes for System MMU v3.1 and v3.2.
977 * System MMU v3.1: 128KiB
978 * System MMU v3.2: 256KiB
979 *
980 * Because System MMU v3.3 caches page table entries more aggressively, it needs
981 * more w/a.
982 * - Any two consecutive I/O virtual regions must be have a hole of larger size
983 * than or equal size to 128KiB.
984 * - Start address of an I/O virtual region must be aligned by 128KiB.
985 */
Cho KyongHod09d78f2014-05-12 11:44:58 +0530986static int exynos_iommu_map(struct iommu_domain *domain, unsigned long l_iova,
KyongHo Cho2a965362012-05-12 05:56:09 +0900987 phys_addr_t paddr, size_t size, int prot)
988{
989 struct exynos_iommu_domain *priv = domain->priv;
Cho KyongHod09d78f2014-05-12 11:44:58 +0530990 sysmmu_pte_t *entry;
991 sysmmu_iova_t iova = (sysmmu_iova_t)l_iova;
KyongHo Cho2a965362012-05-12 05:56:09 +0900992 unsigned long flags;
993 int ret = -ENOMEM;
994
995 BUG_ON(priv->pgtable == NULL);
996
997 spin_lock_irqsave(&priv->pgtablelock, flags);
998
999 entry = section_entry(priv->pgtable, iova);
1000
1001 if (size == SECT_SIZE) {
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301002 ret = lv1set_section(priv, entry, iova, paddr,
KyongHo Cho2a965362012-05-12 05:56:09 +09001003 &priv->lv2entcnt[lv1ent_offset(iova)]);
1004 } else {
Cho KyongHod09d78f2014-05-12 11:44:58 +05301005 sysmmu_pte_t *pent;
KyongHo Cho2a965362012-05-12 05:56:09 +09001006
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301007 pent = alloc_lv2entry(priv, entry, iova,
KyongHo Cho2a965362012-05-12 05:56:09 +09001008 &priv->lv2entcnt[lv1ent_offset(iova)]);
1009
Cho KyongHo61128f02014-05-12 11:44:47 +05301010 if (IS_ERR(pent))
1011 ret = PTR_ERR(pent);
KyongHo Cho2a965362012-05-12 05:56:09 +09001012 else
1013 ret = lv2set_page(pent, paddr, size,
1014 &priv->lv2entcnt[lv1ent_offset(iova)]);
1015 }
1016
Cho KyongHo61128f02014-05-12 11:44:47 +05301017 if (ret)
Cho KyongHo0bf4e542014-05-12 11:45:00 +05301018 pr_err("%s: Failed(%d) to map %#zx bytes @ %#x\n",
1019 __func__, ret, size, iova);
KyongHo Cho2a965362012-05-12 05:56:09 +09001020
1021 spin_unlock_irqrestore(&priv->pgtablelock, flags);
1022
1023 return ret;
1024}
1025
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301026static void exynos_iommu_tlb_invalidate_entry(struct exynos_iommu_domain *priv,
1027 sysmmu_iova_t iova, size_t size)
1028{
1029 struct exynos_iommu_owner *owner;
1030 unsigned long flags;
1031
1032 spin_lock_irqsave(&priv->lock, flags);
1033
1034 list_for_each_entry(owner, &priv->clients, client)
1035 sysmmu_tlb_invalidate_entry(owner->dev, iova, size);
1036
1037 spin_unlock_irqrestore(&priv->lock, flags);
1038}
1039
KyongHo Cho2a965362012-05-12 05:56:09 +09001040static size_t exynos_iommu_unmap(struct iommu_domain *domain,
Cho KyongHod09d78f2014-05-12 11:44:58 +05301041 unsigned long l_iova, size_t size)
KyongHo Cho2a965362012-05-12 05:56:09 +09001042{
1043 struct exynos_iommu_domain *priv = domain->priv;
Cho KyongHod09d78f2014-05-12 11:44:58 +05301044 sysmmu_iova_t iova = (sysmmu_iova_t)l_iova;
1045 sysmmu_pte_t *ent;
Cho KyongHo61128f02014-05-12 11:44:47 +05301046 size_t err_pgsize;
Cho KyongHod09d78f2014-05-12 11:44:58 +05301047 unsigned long flags;
KyongHo Cho2a965362012-05-12 05:56:09 +09001048
1049 BUG_ON(priv->pgtable == NULL);
1050
1051 spin_lock_irqsave(&priv->pgtablelock, flags);
1052
1053 ent = section_entry(priv->pgtable, iova);
1054
1055 if (lv1ent_section(ent)) {
Cho KyongHo0bf4e542014-05-12 11:45:00 +05301056 if (WARN_ON(size < SECT_SIZE)) {
Cho KyongHo61128f02014-05-12 11:44:47 +05301057 err_pgsize = SECT_SIZE;
1058 goto err;
1059 }
KyongHo Cho2a965362012-05-12 05:56:09 +09001060
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301061 *ent = ZERO_LV2LINK; /* w/a for h/w bug in Sysmem MMU v3.3 */
KyongHo Cho2a965362012-05-12 05:56:09 +09001062 pgtable_flush(ent, ent + 1);
1063 size = SECT_SIZE;
1064 goto done;
1065 }
1066
1067 if (unlikely(lv1ent_fault(ent))) {
1068 if (size > SECT_SIZE)
1069 size = SECT_SIZE;
1070 goto done;
1071 }
1072
1073 /* lv1ent_page(sent) == true here */
1074
1075 ent = page_entry(ent, iova);
1076
1077 if (unlikely(lv2ent_fault(ent))) {
1078 size = SPAGE_SIZE;
1079 goto done;
1080 }
1081
1082 if (lv2ent_small(ent)) {
1083 *ent = 0;
1084 size = SPAGE_SIZE;
Cho KyongHo6cb47ed2014-05-12 11:44:51 +05301085 pgtable_flush(ent, ent + 1);
KyongHo Cho2a965362012-05-12 05:56:09 +09001086 priv->lv2entcnt[lv1ent_offset(iova)] += 1;
1087 goto done;
1088 }
1089
1090 /* lv1ent_large(ent) == true here */
Cho KyongHo0bf4e542014-05-12 11:45:00 +05301091 if (WARN_ON(size < LPAGE_SIZE)) {
Cho KyongHo61128f02014-05-12 11:44:47 +05301092 err_pgsize = LPAGE_SIZE;
1093 goto err;
1094 }
KyongHo Cho2a965362012-05-12 05:56:09 +09001095
1096 memset(ent, 0, sizeof(*ent) * SPAGES_PER_LPAGE);
Cho KyongHo6cb47ed2014-05-12 11:44:51 +05301097 pgtable_flush(ent, ent + SPAGES_PER_LPAGE);
KyongHo Cho2a965362012-05-12 05:56:09 +09001098
1099 size = LPAGE_SIZE;
1100 priv->lv2entcnt[lv1ent_offset(iova)] += SPAGES_PER_LPAGE;
1101done:
1102 spin_unlock_irqrestore(&priv->pgtablelock, flags);
1103
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301104 exynos_iommu_tlb_invalidate_entry(priv, iova, size);
KyongHo Cho2a965362012-05-12 05:56:09 +09001105
KyongHo Cho2a965362012-05-12 05:56:09 +09001106 return size;
Cho KyongHo61128f02014-05-12 11:44:47 +05301107err:
1108 spin_unlock_irqrestore(&priv->pgtablelock, flags);
1109
Cho KyongHo0bf4e542014-05-12 11:45:00 +05301110 pr_err("%s: Failed: size(%#zx) @ %#x is smaller than page size %#zx\n",
1111 __func__, size, iova, err_pgsize);
Cho KyongHo61128f02014-05-12 11:44:47 +05301112
1113 return 0;
KyongHo Cho2a965362012-05-12 05:56:09 +09001114}
1115
1116static phys_addr_t exynos_iommu_iova_to_phys(struct iommu_domain *domain,
Varun Sethibb5547a2013-03-29 01:23:58 +05301117 dma_addr_t iova)
KyongHo Cho2a965362012-05-12 05:56:09 +09001118{
1119 struct exynos_iommu_domain *priv = domain->priv;
Cho KyongHod09d78f2014-05-12 11:44:58 +05301120 sysmmu_pte_t *entry;
KyongHo Cho2a965362012-05-12 05:56:09 +09001121 unsigned long flags;
1122 phys_addr_t phys = 0;
1123
1124 spin_lock_irqsave(&priv->pgtablelock, flags);
1125
1126 entry = section_entry(priv->pgtable, iova);
1127
1128 if (lv1ent_section(entry)) {
1129 phys = section_phys(entry) + section_offs(iova);
1130 } else if (lv1ent_page(entry)) {
1131 entry = page_entry(entry, iova);
1132
1133 if (lv2ent_large(entry))
1134 phys = lpage_phys(entry) + lpage_offs(iova);
1135 else if (lv2ent_small(entry))
1136 phys = spage_phys(entry) + spage_offs(iova);
1137 }
1138
1139 spin_unlock_irqrestore(&priv->pgtablelock, flags);
1140
1141 return phys;
1142}
1143
Antonios Motakisbf4a1c92014-05-12 11:44:59 +05301144static int exynos_iommu_add_device(struct device *dev)
1145{
1146 struct iommu_group *group;
1147 int ret;
1148
1149 group = iommu_group_get(dev);
1150
1151 if (!group) {
1152 group = iommu_group_alloc();
1153 if (IS_ERR(group)) {
1154 dev_err(dev, "Failed to allocate IOMMU group\n");
1155 return PTR_ERR(group);
1156 }
1157 }
1158
1159 ret = iommu_group_add_device(group, dev);
1160 iommu_group_put(group);
1161
1162 return ret;
1163}
1164
1165static void exynos_iommu_remove_device(struct device *dev)
1166{
1167 iommu_group_remove_device(dev);
1168}
1169
KyongHo Cho2a965362012-05-12 05:56:09 +09001170static struct iommu_ops exynos_iommu_ops = {
Bjorn Helgaasba5fa6f2014-05-08 14:49:14 -06001171 .domain_init = exynos_iommu_domain_init,
1172 .domain_destroy = exynos_iommu_domain_destroy,
1173 .attach_dev = exynos_iommu_attach_device,
1174 .detach_dev = exynos_iommu_detach_device,
1175 .map = exynos_iommu_map,
1176 .unmap = exynos_iommu_unmap,
1177 .iova_to_phys = exynos_iommu_iova_to_phys,
1178 .add_device = exynos_iommu_add_device,
1179 .remove_device = exynos_iommu_remove_device,
KyongHo Cho2a965362012-05-12 05:56:09 +09001180 .pgsize_bitmap = SECT_SIZE | LPAGE_SIZE | SPAGE_SIZE,
1181};
1182
1183static int __init exynos_iommu_init(void)
1184{
1185 int ret;
1186
Cho KyongHo734c3c72014-05-12 11:44:48 +05301187 lv2table_kmem_cache = kmem_cache_create("exynos-iommu-lv2table",
1188 LV2TABLE_SIZE, LV2TABLE_SIZE, 0, NULL);
1189 if (!lv2table_kmem_cache) {
1190 pr_err("%s: Failed to create kmem cache\n", __func__);
1191 return -ENOMEM;
1192 }
1193
KyongHo Cho2a965362012-05-12 05:56:09 +09001194 ret = platform_driver_register(&exynos_sysmmu_driver);
Cho KyongHo734c3c72014-05-12 11:44:48 +05301195 if (ret) {
1196 pr_err("%s: Failed to register driver\n", __func__);
1197 goto err_reg_driver;
1198 }
KyongHo Cho2a965362012-05-12 05:56:09 +09001199
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301200 zero_lv2_table = kmem_cache_zalloc(lv2table_kmem_cache, GFP_KERNEL);
1201 if (zero_lv2_table == NULL) {
1202 pr_err("%s: Failed to allocate zero level2 page table\n",
1203 __func__);
1204 ret = -ENOMEM;
1205 goto err_zero_lv2;
1206 }
1207
Cho KyongHo734c3c72014-05-12 11:44:48 +05301208 ret = bus_set_iommu(&platform_bus_type, &exynos_iommu_ops);
1209 if (ret) {
1210 pr_err("%s: Failed to register exynos-iommu driver.\n",
1211 __func__);
1212 goto err_set_iommu;
1213 }
KyongHo Cho2a965362012-05-12 05:56:09 +09001214
Cho KyongHo734c3c72014-05-12 11:44:48 +05301215 return 0;
1216err_set_iommu:
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301217 kmem_cache_free(lv2table_kmem_cache, zero_lv2_table);
1218err_zero_lv2:
Cho KyongHo734c3c72014-05-12 11:44:48 +05301219 platform_driver_unregister(&exynos_sysmmu_driver);
1220err_reg_driver:
1221 kmem_cache_destroy(lv2table_kmem_cache);
KyongHo Cho2a965362012-05-12 05:56:09 +09001222 return ret;
1223}
1224subsys_initcall(exynos_iommu_init);