blob: 15787a177a16044d51ba3a0566bbdc538fd9ead1 [file] [log] [blame]
KyongHo Cho2a965362012-05-12 05:56:09 +09001/* linux/drivers/iommu/exynos_iommu.c
2 *
3 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
4 * http://www.samsung.com
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#ifdef CONFIG_EXYNOS_IOMMU_DEBUG
12#define DEBUG
13#endif
14
KyongHo Cho2a965362012-05-12 05:56:09 +090015#include <linux/clk.h>
Marek Szyprowski8ed55c82015-05-19 15:20:36 +020016#include <linux/dma-mapping.h>
KyongHo Cho2a965362012-05-12 05:56:09 +090017#include <linux/err.h>
Marek Szyprowski312900c2015-05-19 15:20:30 +020018#include <linux/io.h>
KyongHo Cho2a965362012-05-12 05:56:09 +090019#include <linux/iommu.h>
Marek Szyprowski312900c2015-05-19 15:20:30 +020020#include <linux/interrupt.h>
KyongHo Cho2a965362012-05-12 05:56:09 +090021#include <linux/list.h>
Marek Szyprowski8ed55c82015-05-19 15:20:36 +020022#include <linux/of.h>
23#include <linux/of_iommu.h>
24#include <linux/of_platform.h>
Marek Szyprowski312900c2015-05-19 15:20:30 +020025#include <linux/platform_device.h>
26#include <linux/pm_runtime.h>
27#include <linux/slab.h>
Marek Szyprowski58c6f6a2016-02-18 15:12:49 +010028#include <linux/dma-iommu.h>
KyongHo Cho2a965362012-05-12 05:56:09 +090029
Cho KyongHod09d78f2014-05-12 11:44:58 +053030typedef u32 sysmmu_iova_t;
31typedef u32 sysmmu_pte_t;
32
Sachin Kamatf171aba2014-08-04 10:06:28 +053033/* We do not consider super section mapping (16MB) */
KyongHo Cho2a965362012-05-12 05:56:09 +090034#define SECT_ORDER 20
35#define LPAGE_ORDER 16
36#define SPAGE_ORDER 12
37
38#define SECT_SIZE (1 << SECT_ORDER)
39#define LPAGE_SIZE (1 << LPAGE_ORDER)
40#define SPAGE_SIZE (1 << SPAGE_ORDER)
41
42#define SECT_MASK (~(SECT_SIZE - 1))
43#define LPAGE_MASK (~(LPAGE_SIZE - 1))
44#define SPAGE_MASK (~(SPAGE_SIZE - 1))
45
Cho KyongHo66a7ed82014-05-12 11:45:04 +053046#define lv1ent_fault(sent) ((*(sent) == ZERO_LV2LINK) || \
47 ((*(sent) & 3) == 0) || ((*(sent) & 3) == 3))
48#define lv1ent_zero(sent) (*(sent) == ZERO_LV2LINK)
49#define lv1ent_page_zero(sent) ((*(sent) & 3) == 1)
50#define lv1ent_page(sent) ((*(sent) != ZERO_LV2LINK) && \
51 ((*(sent) & 3) == 1))
KyongHo Cho2a965362012-05-12 05:56:09 +090052#define lv1ent_section(sent) ((*(sent) & 3) == 2)
53
54#define lv2ent_fault(pent) ((*(pent) & 3) == 0)
55#define lv2ent_small(pent) ((*(pent) & 2) == 2)
56#define lv2ent_large(pent) ((*(pent) & 3) == 1)
57
Cho KyongHod09d78f2014-05-12 11:44:58 +053058static u32 sysmmu_page_offset(sysmmu_iova_t iova, u32 size)
59{
60 return iova & (size - 1);
61}
KyongHo Cho2a965362012-05-12 05:56:09 +090062
Cho KyongHod09d78f2014-05-12 11:44:58 +053063#define section_phys(sent) (*(sent) & SECT_MASK)
64#define section_offs(iova) sysmmu_page_offset((iova), SECT_SIZE)
65#define lpage_phys(pent) (*(pent) & LPAGE_MASK)
66#define lpage_offs(iova) sysmmu_page_offset((iova), LPAGE_SIZE)
67#define spage_phys(pent) (*(pent) & SPAGE_MASK)
68#define spage_offs(iova) sysmmu_page_offset((iova), SPAGE_SIZE)
KyongHo Cho2a965362012-05-12 05:56:09 +090069
70#define NUM_LV1ENTRIES 4096
Cho KyongHod09d78f2014-05-12 11:44:58 +053071#define NUM_LV2ENTRIES (SECT_SIZE / SPAGE_SIZE)
KyongHo Cho2a965362012-05-12 05:56:09 +090072
Cho KyongHod09d78f2014-05-12 11:44:58 +053073static u32 lv1ent_offset(sysmmu_iova_t iova)
74{
75 return iova >> SECT_ORDER;
76}
77
78static u32 lv2ent_offset(sysmmu_iova_t iova)
79{
80 return (iova >> SPAGE_ORDER) & (NUM_LV2ENTRIES - 1);
81}
82
Marek Szyprowski5e3435e2016-02-18 15:12:50 +010083#define LV1TABLE_SIZE (NUM_LV1ENTRIES * sizeof(sysmmu_pte_t))
Cho KyongHod09d78f2014-05-12 11:44:58 +053084#define LV2TABLE_SIZE (NUM_LV2ENTRIES * sizeof(sysmmu_pte_t))
KyongHo Cho2a965362012-05-12 05:56:09 +090085
86#define SPAGES_PER_LPAGE (LPAGE_SIZE / SPAGE_SIZE)
87
88#define lv2table_base(sent) (*(sent) & 0xFFFFFC00)
89
90#define mk_lv1ent_sect(pa) ((pa) | 2)
91#define mk_lv1ent_page(pa) ((pa) | 1)
92#define mk_lv2ent_lpage(pa) ((pa) | 1)
93#define mk_lv2ent_spage(pa) ((pa) | 2)
94
95#define CTRL_ENABLE 0x5
96#define CTRL_BLOCK 0x7
97#define CTRL_DISABLE 0x0
98
Cho KyongHoeeb51842014-05-12 11:45:03 +053099#define CFG_LRU 0x1
100#define CFG_QOS(n) ((n & 0xF) << 7)
101#define CFG_MASK 0x0150FFFF /* Selecting bit 0-15, 20, 22 and 24 */
102#define CFG_ACGEN (1 << 24) /* System MMU 3.3 only */
103#define CFG_SYSSEL (1 << 22) /* System MMU 3.2 only */
104#define CFG_FLPDCACHE (1 << 20) /* System MMU 3.2+ only */
105
KyongHo Cho2a965362012-05-12 05:56:09 +0900106#define REG_MMU_CTRL 0x000
107#define REG_MMU_CFG 0x004
108#define REG_MMU_STATUS 0x008
109#define REG_MMU_FLUSH 0x00C
110#define REG_MMU_FLUSH_ENTRY 0x010
111#define REG_PT_BASE_ADDR 0x014
112#define REG_INT_STATUS 0x018
113#define REG_INT_CLEAR 0x01C
114
115#define REG_PAGE_FAULT_ADDR 0x024
116#define REG_AW_FAULT_ADDR 0x028
117#define REG_AR_FAULT_ADDR 0x02C
118#define REG_DEFAULT_SLAVE_ADDR 0x030
119
120#define REG_MMU_VERSION 0x034
121
Cho KyongHoeeb51842014-05-12 11:45:03 +0530122#define MMU_MAJ_VER(val) ((val) >> 7)
123#define MMU_MIN_VER(val) ((val) & 0x7F)
124#define MMU_RAW_VER(reg) (((reg) >> 21) & ((1 << 11) - 1)) /* 11 bits */
125
126#define MAKE_MMU_VER(maj, min) ((((maj) & 0xF) << 7) | ((min) & 0x7F))
127
KyongHo Cho2a965362012-05-12 05:56:09 +0900128#define REG_PB0_SADDR 0x04C
129#define REG_PB0_EADDR 0x050
130#define REG_PB1_SADDR 0x054
131#define REG_PB1_EADDR 0x058
132
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530133#define has_sysmmu(dev) (dev->archdata.iommu != NULL)
134
Marek Szyprowski5e3435e2016-02-18 15:12:50 +0100135static struct device *dma_dev;
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
Marek Szyprowskid093fc72016-02-18 15:12:53 +0100151/*
152 * IOMMU fault information register
153 */
154struct sysmmu_fault_info {
155 unsigned int bit; /* bit number in STATUS register */
156 unsigned short addr_reg; /* register to read VA fault address */
157 const char *name; /* human readable fault name */
158 unsigned int type; /* fault type for report_iommu_fault */
KyongHo Cho2a965362012-05-12 05:56:09 +0900159};
160
Marek Szyprowskid093fc72016-02-18 15:12:53 +0100161static const struct sysmmu_fault_info sysmmu_faults[] = {
162 { 0, REG_PAGE_FAULT_ADDR, "PAGE", IOMMU_FAULT_READ },
163 { 1, REG_AR_FAULT_ADDR, "AR MULTI-HIT", IOMMU_FAULT_READ },
164 { 2, REG_AW_FAULT_ADDR, "AW MULTI-HIT", IOMMU_FAULT_WRITE },
165 { 3, REG_DEFAULT_SLAVE_ADDR, "BUS ERROR", IOMMU_FAULT_READ },
166 { 4, REG_AR_FAULT_ADDR, "AR SECURITY PROTECTION", IOMMU_FAULT_READ },
167 { 5, REG_AR_FAULT_ADDR, "AR ACCESS PROTECTION", IOMMU_FAULT_READ },
168 { 6, REG_AW_FAULT_ADDR, "AW SECURITY PROTECTION", IOMMU_FAULT_WRITE },
169 { 7, REG_AW_FAULT_ADDR, "AW ACCESS PROTECTION", IOMMU_FAULT_WRITE },
KyongHo Cho2a965362012-05-12 05:56:09 +0900170};
171
Marek Szyprowski2860af32015-05-19 15:20:31 +0200172/*
173 * This structure is attached to dev.archdata.iommu of the master device
174 * on device add, contains a list of SYSMMU controllers defined by device tree,
175 * which are bound to given master device. It is usually referenced by 'owner'
176 * pointer.
177*/
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530178struct exynos_iommu_owner {
Marek Szyprowski1b092052015-05-19 15:20:33 +0200179 struct list_head controllers; /* list of sysmmu_drvdata.owner_node */
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530180};
181
Marek Szyprowski2860af32015-05-19 15:20:31 +0200182/*
183 * This structure exynos specific generalization of struct iommu_domain.
184 * It contains list of SYSMMU controllers from all master devices, which has
185 * been attached to this domain and page tables of IO address space defined by
186 * it. It is usually referenced by 'domain' pointer.
187 */
KyongHo Cho2a965362012-05-12 05:56:09 +0900188struct exynos_iommu_domain {
Marek Szyprowski2860af32015-05-19 15:20:31 +0200189 struct list_head clients; /* list of sysmmu_drvdata.domain_node */
190 sysmmu_pte_t *pgtable; /* lv1 page table, 16KB */
191 short *lv2entcnt; /* free lv2 entry counter for each section */
192 spinlock_t lock; /* lock for modyfying list of clients */
193 spinlock_t pgtablelock; /* lock for modifying page table @ pgtable */
Joerg Roedele1fd1ea2015-03-26 13:43:11 +0100194 struct iommu_domain domain; /* generic domain data structure */
KyongHo Cho2a965362012-05-12 05:56:09 +0900195};
196
Marek Szyprowski2860af32015-05-19 15:20:31 +0200197/*
198 * This structure hold all data of a single SYSMMU controller, this includes
199 * hw resources like registers and clocks, pointers and list nodes to connect
200 * it to all other structures, internal state and parameters read from device
201 * tree. It is usually referenced by 'data' pointer.
202 */
KyongHo Cho2a965362012-05-12 05:56:09 +0900203struct sysmmu_drvdata {
Marek Szyprowski2860af32015-05-19 15:20:31 +0200204 struct device *sysmmu; /* SYSMMU controller device */
205 struct device *master; /* master device (owner) */
206 void __iomem *sfrbase; /* our registers */
207 struct clk *clk; /* SYSMMU's clock */
208 struct clk *clk_master; /* master's device clock */
209 int activations; /* number of calls to sysmmu_enable */
210 spinlock_t lock; /* lock for modyfying state */
211 struct exynos_iommu_domain *domain; /* domain we belong to */
212 struct list_head domain_node; /* node for domain clients list */
Marek Szyprowski1b092052015-05-19 15:20:33 +0200213 struct list_head owner_node; /* node for owner controllers list */
Marek Szyprowski2860af32015-05-19 15:20:31 +0200214 phys_addr_t pgtable; /* assigned page table structure */
215 unsigned int version; /* our version */
KyongHo Cho2a965362012-05-12 05:56:09 +0900216};
217
Joerg Roedele1fd1ea2015-03-26 13:43:11 +0100218static struct exynos_iommu_domain *to_exynos_domain(struct iommu_domain *dom)
219{
220 return container_of(dom, struct exynos_iommu_domain, domain);
221}
222
KyongHo Cho2a965362012-05-12 05:56:09 +0900223static bool set_sysmmu_active(struct sysmmu_drvdata *data)
224{
225 /* return true if the System MMU was not active previously
226 and it needs to be initialized */
227 return ++data->activations == 1;
228}
229
230static bool set_sysmmu_inactive(struct sysmmu_drvdata *data)
231{
232 /* return true if the System MMU is needed to be disabled */
233 BUG_ON(data->activations < 1);
234 return --data->activations == 0;
235}
236
237static bool is_sysmmu_active(struct sysmmu_drvdata *data)
238{
239 return data->activations > 0;
240}
241
Marek Szyprowski02cdc362016-02-18 15:12:52 +0100242static void sysmmu_unblock(struct sysmmu_drvdata *data)
KyongHo Cho2a965362012-05-12 05:56:09 +0900243{
Marek Szyprowski02cdc362016-02-18 15:12:52 +0100244 __raw_writel(CTRL_ENABLE, data->sfrbase + REG_MMU_CTRL);
KyongHo Cho2a965362012-05-12 05:56:09 +0900245}
246
Marek Szyprowski02cdc362016-02-18 15:12:52 +0100247static bool sysmmu_block(struct sysmmu_drvdata *data)
KyongHo Cho2a965362012-05-12 05:56:09 +0900248{
249 int i = 120;
250
Marek Szyprowski02cdc362016-02-18 15:12:52 +0100251 __raw_writel(CTRL_BLOCK, data->sfrbase + REG_MMU_CTRL);
252 while ((i > 0) && !(__raw_readl(data->sfrbase + REG_MMU_STATUS) & 1))
KyongHo Cho2a965362012-05-12 05:56:09 +0900253 --i;
254
Marek Szyprowski02cdc362016-02-18 15:12:52 +0100255 if (!(__raw_readl(data->sfrbase + REG_MMU_STATUS) & 1)) {
256 sysmmu_unblock(data);
KyongHo Cho2a965362012-05-12 05:56:09 +0900257 return false;
258 }
259
260 return true;
261}
262
Marek Szyprowski02cdc362016-02-18 15:12:52 +0100263static void __sysmmu_tlb_invalidate(struct sysmmu_drvdata *data)
KyongHo Cho2a965362012-05-12 05:56:09 +0900264{
Marek Szyprowski02cdc362016-02-18 15:12:52 +0100265 __raw_writel(0x1, data->sfrbase + REG_MMU_FLUSH);
KyongHo Cho2a965362012-05-12 05:56:09 +0900266}
267
Marek Szyprowski02cdc362016-02-18 15:12:52 +0100268static void __sysmmu_tlb_invalidate_entry(struct sysmmu_drvdata *data,
Cho KyongHod09d78f2014-05-12 11:44:58 +0530269 sysmmu_iova_t iova, unsigned int num_inv)
KyongHo Cho2a965362012-05-12 05:56:09 +0900270{
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530271 unsigned int i;
Sachin Kamat365409d2014-05-22 09:50:56 +0530272
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530273 for (i = 0; i < num_inv; i++) {
274 __raw_writel((iova & SPAGE_MASK) | 1,
Marek Szyprowski02cdc362016-02-18 15:12:52 +0100275 data->sfrbase + REG_MMU_FLUSH_ENTRY);
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530276 iova += SPAGE_SIZE;
277 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900278}
279
Marek Szyprowski02cdc362016-02-18 15:12:52 +0100280static void __sysmmu_set_ptbase(struct sysmmu_drvdata *data, phys_addr_t pgd)
KyongHo Cho2a965362012-05-12 05:56:09 +0900281{
Marek Szyprowski02cdc362016-02-18 15:12:52 +0100282 __raw_writel(pgd, data->sfrbase + REG_PT_BASE_ADDR);
KyongHo Cho2a965362012-05-12 05:56:09 +0900283
Marek Szyprowski02cdc362016-02-18 15:12:52 +0100284 __sysmmu_tlb_invalidate(data);
KyongHo Cho2a965362012-05-12 05:56:09 +0900285}
286
Marek Szyprowskid093fc72016-02-18 15:12:53 +0100287static void show_fault_information(struct sysmmu_drvdata *data,
288 const struct sysmmu_fault_info *finfo,
289 sysmmu_iova_t fault_addr)
KyongHo Cho2a965362012-05-12 05:56:09 +0900290{
Cho KyongHod09d78f2014-05-12 11:44:58 +0530291 sysmmu_pte_t *ent;
KyongHo Cho2a965362012-05-12 05:56:09 +0900292
Marek Szyprowskid093fc72016-02-18 15:12:53 +0100293 dev_err(data->sysmmu, "%s FAULT occurred at %#x (page table base: %pa)\n",
294 finfo->name, fault_addr, &data->pgtable);
295 ent = section_entry(phys_to_virt(data->pgtable), fault_addr);
296 dev_err(data->sysmmu, "\tLv1 entry: %#x\n", *ent);
KyongHo Cho2a965362012-05-12 05:56:09 +0900297 if (lv1ent_page(ent)) {
298 ent = page_entry(ent, fault_addr);
Marek Szyprowskid093fc72016-02-18 15:12:53 +0100299 dev_err(data->sysmmu, "\t Lv2 entry: %#x\n", *ent);
KyongHo Cho2a965362012-05-12 05:56:09 +0900300 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900301}
302
303static irqreturn_t exynos_sysmmu_irq(int irq, void *dev_id)
304{
Sachin Kamatf171aba2014-08-04 10:06:28 +0530305 /* SYSMMU is in blocked state when interrupt occurred. */
KyongHo Cho2a965362012-05-12 05:56:09 +0900306 struct sysmmu_drvdata *data = dev_id;
Marek Szyprowskid093fc72016-02-18 15:12:53 +0100307 const struct sysmmu_fault_info *finfo = sysmmu_faults;
308 int i, n = ARRAY_SIZE(sysmmu_faults);
309 unsigned int itype;
310 sysmmu_iova_t fault_addr = -1;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530311 int ret = -ENOSYS;
KyongHo Cho2a965362012-05-12 05:56:09 +0900312
KyongHo Cho2a965362012-05-12 05:56:09 +0900313 WARN_ON(!is_sysmmu_active(data));
314
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530315 spin_lock(&data->lock);
316
Marek Szyprowskib398af22016-02-18 15:12:51 +0100317 clk_enable(data->clk_master);
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530318
Marek Szyprowskid093fc72016-02-18 15:12:53 +0100319 itype = __ffs(__raw_readl(data->sfrbase + REG_INT_STATUS));
320 for (i = 0; i < n; i++, finfo++)
321 if (finfo->bit == itype)
322 break;
323 /* unknown/unsupported fault */
324 BUG_ON(i == n);
KyongHo Cho2a965362012-05-12 05:56:09 +0900325
Marek Szyprowskid093fc72016-02-18 15:12:53 +0100326 /* print debug message */
327 fault_addr = __raw_readl(data->sfrbase + finfo->addr_reg);
328 show_fault_information(data, finfo, fault_addr);
KyongHo Cho2a965362012-05-12 05:56:09 +0900329
Marek Szyprowskid093fc72016-02-18 15:12:53 +0100330 if (data->domain)
331 ret = report_iommu_fault(&data->domain->domain,
332 data->master, fault_addr, finfo->type);
Cho KyongHo1fab7fa2014-05-12 11:44:56 +0530333 /* fault is not recovered by fault handler */
334 BUG_ON(ret != 0);
KyongHo Cho2a965362012-05-12 05:56:09 +0900335
Cho KyongHo1fab7fa2014-05-12 11:44:56 +0530336 __raw_writel(1 << itype, data->sfrbase + REG_INT_CLEAR);
337
Marek Szyprowski02cdc362016-02-18 15:12:52 +0100338 sysmmu_unblock(data);
KyongHo Cho2a965362012-05-12 05:56:09 +0900339
Marek Szyprowskib398af22016-02-18 15:12:51 +0100340 clk_disable(data->clk_master);
Cho KyongHo70605872014-05-12 11:44:55 +0530341
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530342 spin_unlock(&data->lock);
KyongHo Cho2a965362012-05-12 05:56:09 +0900343
344 return IRQ_HANDLED;
345}
346
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530347static void __sysmmu_disable_nocount(struct sysmmu_drvdata *data)
KyongHo Cho2a965362012-05-12 05:56:09 +0900348{
Marek Szyprowskib398af22016-02-18 15:12:51 +0100349 clk_enable(data->clk_master);
Cho KyongHo70605872014-05-12 11:44:55 +0530350
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530351 __raw_writel(CTRL_DISABLE, data->sfrbase + REG_MMU_CTRL);
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530352 __raw_writel(0, data->sfrbase + REG_MMU_CFG);
KyongHo Cho2a965362012-05-12 05:56:09 +0900353
Cho KyongHo46c16d12014-05-12 11:44:54 +0530354 clk_disable(data->clk);
Marek Szyprowskib398af22016-02-18 15:12:51 +0100355 clk_disable(data->clk_master);
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530356}
KyongHo Cho2a965362012-05-12 05:56:09 +0900357
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530358static bool __sysmmu_disable(struct sysmmu_drvdata *data)
359{
360 bool disabled;
361 unsigned long flags;
362
363 spin_lock_irqsave(&data->lock, flags);
364
365 disabled = set_sysmmu_inactive(data);
366
367 if (disabled) {
368 data->pgtable = 0;
369 data->domain = NULL;
370
371 __sysmmu_disable_nocount(data);
372
373 dev_dbg(data->sysmmu, "Disabled\n");
374 } else {
375 dev_dbg(data->sysmmu, "%d times left to disable\n",
376 data->activations);
377 }
378
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530379 spin_unlock_irqrestore(&data->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900380
KyongHo Cho2a965362012-05-12 05:56:09 +0900381 return disabled;
382}
383
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530384static void __sysmmu_init_config(struct sysmmu_drvdata *data)
385{
Marek Szyprowski83addec2016-02-18 15:12:54 +0100386 unsigned int cfg;
Cho KyongHoeeb51842014-05-12 11:45:03 +0530387
Marek Szyprowski83addec2016-02-18 15:12:54 +0100388 data->version = MMU_RAW_VER(__raw_readl(data->sfrbase + REG_MMU_VERSION));
389 if (data->version <= MAKE_MMU_VER(3, 1))
390 cfg = CFG_LRU | CFG_QOS(15);
391 else if (data->version <= MAKE_MMU_VER(3, 2))
392 cfg = CFG_LRU | CFG_QOS(15) | CFG_FLPDCACHE | CFG_SYSSEL;
393 else
394 cfg = CFG_QOS(15) | CFG_FLPDCACHE | CFG_ACGEN;
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530395
396 __raw_writel(cfg, data->sfrbase + REG_MMU_CFG);
397}
398
399static void __sysmmu_enable_nocount(struct sysmmu_drvdata *data)
400{
Marek Szyprowskib398af22016-02-18 15:12:51 +0100401 clk_enable(data->clk_master);
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530402 clk_enable(data->clk);
403
404 __raw_writel(CTRL_BLOCK, data->sfrbase + REG_MMU_CTRL);
405
406 __sysmmu_init_config(data);
407
Marek Szyprowski02cdc362016-02-18 15:12:52 +0100408 __sysmmu_set_ptbase(data, data->pgtable);
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530409
410 __raw_writel(CTRL_ENABLE, data->sfrbase + REG_MMU_CTRL);
411
Marek Szyprowskib398af22016-02-18 15:12:51 +0100412 clk_disable(data->clk_master);
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530413}
414
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200415static int __sysmmu_enable(struct sysmmu_drvdata *data, phys_addr_t pgtable,
Marek Szyprowskia9133b992015-05-19 15:20:29 +0200416 struct exynos_iommu_domain *domain)
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530417{
418 int ret = 0;
419 unsigned long flags;
420
421 spin_lock_irqsave(&data->lock, flags);
422 if (set_sysmmu_active(data)) {
423 data->pgtable = pgtable;
Marek Szyprowskia9133b992015-05-19 15:20:29 +0200424 data->domain = domain;
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530425
426 __sysmmu_enable_nocount(data);
427
428 dev_dbg(data->sysmmu, "Enabled\n");
429 } else {
430 ret = (pgtable == data->pgtable) ? 1 : -EBUSY;
431
432 dev_dbg(data->sysmmu, "already enabled\n");
433 }
434
435 if (WARN_ON(ret < 0))
436 set_sysmmu_inactive(data); /* decrement count */
437
438 spin_unlock_irqrestore(&data->lock, flags);
439
440 return ret;
441}
442
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530443static void __sysmmu_tlb_invalidate_flpdcache(struct sysmmu_drvdata *data,
444 sysmmu_iova_t iova)
445{
Marek Szyprowski512bd0c2015-05-19 15:20:24 +0200446 if (data->version == MAKE_MMU_VER(3, 3))
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530447 __raw_writel(iova | 0x1, data->sfrbase + REG_MMU_FLUSH_ENTRY);
448}
449
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200450static void sysmmu_tlb_invalidate_flpdcache(struct sysmmu_drvdata *data,
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530451 sysmmu_iova_t iova)
452{
453 unsigned long flags;
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530454
Marek Szyprowskib398af22016-02-18 15:12:51 +0100455 clk_enable(data->clk_master);
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530456
457 spin_lock_irqsave(&data->lock, flags);
458 if (is_sysmmu_active(data))
459 __sysmmu_tlb_invalidate_flpdcache(data, iova);
460 spin_unlock_irqrestore(&data->lock, flags);
461
Marek Szyprowskib398af22016-02-18 15:12:51 +0100462 clk_disable(data->clk_master);
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530463}
464
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200465static void sysmmu_tlb_invalidate_entry(struct sysmmu_drvdata *data,
466 sysmmu_iova_t iova, size_t size)
KyongHo Cho2a965362012-05-12 05:56:09 +0900467{
468 unsigned long flags;
KyongHo Cho2a965362012-05-12 05:56:09 +0900469
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530470 spin_lock_irqsave(&data->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900471 if (is_sysmmu_active(data)) {
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530472 unsigned int num_inv = 1;
Cho KyongHo70605872014-05-12 11:44:55 +0530473
Marek Szyprowskib398af22016-02-18 15:12:51 +0100474 clk_enable(data->clk_master);
Cho KyongHo70605872014-05-12 11:44:55 +0530475
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530476 /*
477 * L2TLB invalidation required
478 * 4KB page: 1 invalidation
Sachin Kamatf171aba2014-08-04 10:06:28 +0530479 * 64KB page: 16 invalidations
480 * 1MB page: 64 invalidations
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530481 * because it is set-associative TLB
482 * with 8-way and 64 sets.
483 * 1MB page can be cached in one of all sets.
484 * 64KB page can be one of 16 consecutive sets.
485 */
Marek Szyprowski512bd0c2015-05-19 15:20:24 +0200486 if (MMU_MAJ_VER(data->version) == 2)
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530487 num_inv = min_t(unsigned int, size / PAGE_SIZE, 64);
488
Marek Szyprowski02cdc362016-02-18 15:12:52 +0100489 if (sysmmu_block(data)) {
490 __sysmmu_tlb_invalidate_entry(data, iova, num_inv);
491 sysmmu_unblock(data);
KyongHo Cho2a965362012-05-12 05:56:09 +0900492 }
Marek Szyprowskib398af22016-02-18 15:12:51 +0100493 clk_disable(data->clk_master);
KyongHo Cho2a965362012-05-12 05:56:09 +0900494 } else {
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200495 dev_dbg(data->master,
496 "disabled. Skipping TLB invalidation @ %#x\n", iova);
KyongHo Cho2a965362012-05-12 05:56:09 +0900497 }
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530498 spin_unlock_irqrestore(&data->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900499}
500
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530501static int __init exynos_sysmmu_probe(struct platform_device *pdev)
KyongHo Cho2a965362012-05-12 05:56:09 +0900502{
Cho KyongHo46c16d12014-05-12 11:44:54 +0530503 int irq, ret;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530504 struct device *dev = &pdev->dev;
KyongHo Cho2a965362012-05-12 05:56:09 +0900505 struct sysmmu_drvdata *data;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530506 struct resource *res;
KyongHo Cho2a965362012-05-12 05:56:09 +0900507
Cho KyongHo46c16d12014-05-12 11:44:54 +0530508 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
509 if (!data)
510 return -ENOMEM;
KyongHo Cho2a965362012-05-12 05:56:09 +0900511
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530512 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Cho KyongHo46c16d12014-05-12 11:44:54 +0530513 data->sfrbase = devm_ioremap_resource(dev, res);
514 if (IS_ERR(data->sfrbase))
515 return PTR_ERR(data->sfrbase);
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530516
Cho KyongHo46c16d12014-05-12 11:44:54 +0530517 irq = platform_get_irq(pdev, 0);
518 if (irq <= 0) {
Cho KyongHo0bf4e542014-05-12 11:45:00 +0530519 dev_err(dev, "Unable to find IRQ resource\n");
Cho KyongHo46c16d12014-05-12 11:44:54 +0530520 return irq;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530521 }
522
Cho KyongHo46c16d12014-05-12 11:44:54 +0530523 ret = devm_request_irq(dev, irq, exynos_sysmmu_irq, 0,
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530524 dev_name(dev), data);
KyongHo Cho2a965362012-05-12 05:56:09 +0900525 if (ret) {
Cho KyongHo46c16d12014-05-12 11:44:54 +0530526 dev_err(dev, "Unabled to register handler of irq %d\n", irq);
527 return ret;
KyongHo Cho2a965362012-05-12 05:56:09 +0900528 }
529
Cho KyongHo46c16d12014-05-12 11:44:54 +0530530 data->clk = devm_clk_get(dev, "sysmmu");
531 if (IS_ERR(data->clk)) {
532 dev_err(dev, "Failed to get clock!\n");
533 return PTR_ERR(data->clk);
534 } else {
535 ret = clk_prepare(data->clk);
536 if (ret) {
537 dev_err(dev, "Failed to prepare clk\n");
538 return ret;
539 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900540 }
541
Cho KyongHo70605872014-05-12 11:44:55 +0530542 data->clk_master = devm_clk_get(dev, "master");
543 if (!IS_ERR(data->clk_master)) {
544 ret = clk_prepare(data->clk_master);
545 if (ret) {
546 clk_unprepare(data->clk);
547 dev_err(dev, "Failed to prepare master's clk\n");
548 return ret;
549 }
Marek Szyprowskib398af22016-02-18 15:12:51 +0100550 } else {
551 data->clk_master = NULL;
Cho KyongHo70605872014-05-12 11:44:55 +0530552 }
553
KyongHo Cho2a965362012-05-12 05:56:09 +0900554 data->sysmmu = dev;
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530555 spin_lock_init(&data->lock);
KyongHo Cho2a965362012-05-12 05:56:09 +0900556
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530557 platform_set_drvdata(pdev, data);
558
Cho KyongHof4723ec2014-05-12 11:44:52 +0530559 pm_runtime_enable(dev);
KyongHo Cho2a965362012-05-12 05:56:09 +0900560
KyongHo Cho2a965362012-05-12 05:56:09 +0900561 return 0;
KyongHo Cho2a965362012-05-12 05:56:09 +0900562}
563
Marek Szyprowski622015e2015-05-19 15:20:35 +0200564#ifdef CONFIG_PM_SLEEP
565static int exynos_sysmmu_suspend(struct device *dev)
566{
567 struct sysmmu_drvdata *data = dev_get_drvdata(dev);
568
569 dev_dbg(dev, "suspend\n");
570 if (is_sysmmu_active(data)) {
571 __sysmmu_disable_nocount(data);
572 pm_runtime_put(dev);
573 }
574 return 0;
575}
576
577static int exynos_sysmmu_resume(struct device *dev)
578{
579 struct sysmmu_drvdata *data = dev_get_drvdata(dev);
580
581 dev_dbg(dev, "resume\n");
582 if (is_sysmmu_active(data)) {
583 pm_runtime_get_sync(dev);
584 __sysmmu_enable_nocount(data);
585 }
586 return 0;
587}
588#endif
589
590static const struct dev_pm_ops sysmmu_pm_ops = {
591 SET_LATE_SYSTEM_SLEEP_PM_OPS(exynos_sysmmu_suspend, exynos_sysmmu_resume)
592};
593
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530594static const struct of_device_id sysmmu_of_match[] __initconst = {
595 { .compatible = "samsung,exynos-sysmmu", },
596 { },
597};
598
599static struct platform_driver exynos_sysmmu_driver __refdata = {
600 .probe = exynos_sysmmu_probe,
601 .driver = {
KyongHo Cho2a965362012-05-12 05:56:09 +0900602 .name = "exynos-sysmmu",
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530603 .of_match_table = sysmmu_of_match,
Marek Szyprowski622015e2015-05-19 15:20:35 +0200604 .pm = &sysmmu_pm_ops,
KyongHo Cho2a965362012-05-12 05:56:09 +0900605 }
606};
607
Marek Szyprowski5e3435e2016-02-18 15:12:50 +0100608static inline void update_pte(sysmmu_pte_t *ent, sysmmu_pte_t val)
KyongHo Cho2a965362012-05-12 05:56:09 +0900609{
Marek Szyprowski5e3435e2016-02-18 15:12:50 +0100610 dma_sync_single_for_cpu(dma_dev, virt_to_phys(ent), sizeof(*ent),
611 DMA_TO_DEVICE);
612 *ent = val;
613 dma_sync_single_for_device(dma_dev, virt_to_phys(ent), sizeof(*ent),
614 DMA_TO_DEVICE);
KyongHo Cho2a965362012-05-12 05:56:09 +0900615}
616
Joerg Roedele1fd1ea2015-03-26 13:43:11 +0100617static struct iommu_domain *exynos_iommu_domain_alloc(unsigned type)
KyongHo Cho2a965362012-05-12 05:56:09 +0900618{
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200619 struct exynos_iommu_domain *domain;
Marek Szyprowski5e3435e2016-02-18 15:12:50 +0100620 dma_addr_t handle;
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530621 int i;
KyongHo Cho2a965362012-05-12 05:56:09 +0900622
KyongHo Cho2a965362012-05-12 05:56:09 +0900623
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200624 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
625 if (!domain)
Joerg Roedele1fd1ea2015-03-26 13:43:11 +0100626 return NULL;
627
Marek Szyprowski58c6f6a2016-02-18 15:12:49 +0100628 if (type == IOMMU_DOMAIN_DMA) {
629 if (iommu_get_dma_cookie(&domain->domain) != 0)
630 goto err_pgtable;
631 } else if (type != IOMMU_DOMAIN_UNMANAGED) {
632 goto err_pgtable;
633 }
634
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200635 domain->pgtable = (sysmmu_pte_t *)__get_free_pages(GFP_KERNEL, 2);
636 if (!domain->pgtable)
Marek Szyprowski58c6f6a2016-02-18 15:12:49 +0100637 goto err_dma_cookie;
KyongHo Cho2a965362012-05-12 05:56:09 +0900638
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200639 domain->lv2entcnt = (short *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1);
640 if (!domain->lv2entcnt)
KyongHo Cho2a965362012-05-12 05:56:09 +0900641 goto err_counter;
642
Sachin Kamatf171aba2014-08-04 10:06:28 +0530643 /* Workaround for System MMU v3.3 to prevent caching 1MiB mapping */
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530644 for (i = 0; i < NUM_LV1ENTRIES; i += 8) {
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200645 domain->pgtable[i + 0] = ZERO_LV2LINK;
646 domain->pgtable[i + 1] = ZERO_LV2LINK;
647 domain->pgtable[i + 2] = ZERO_LV2LINK;
648 domain->pgtable[i + 3] = ZERO_LV2LINK;
649 domain->pgtable[i + 4] = ZERO_LV2LINK;
650 domain->pgtable[i + 5] = ZERO_LV2LINK;
651 domain->pgtable[i + 6] = ZERO_LV2LINK;
652 domain->pgtable[i + 7] = ZERO_LV2LINK;
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530653 }
654
Marek Szyprowski5e3435e2016-02-18 15:12:50 +0100655 handle = dma_map_single(dma_dev, domain->pgtable, LV1TABLE_SIZE,
656 DMA_TO_DEVICE);
657 /* For mapping page table entries we rely on dma == phys */
658 BUG_ON(handle != virt_to_phys(domain->pgtable));
KyongHo Cho2a965362012-05-12 05:56:09 +0900659
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200660 spin_lock_init(&domain->lock);
661 spin_lock_init(&domain->pgtablelock);
662 INIT_LIST_HEAD(&domain->clients);
KyongHo Cho2a965362012-05-12 05:56:09 +0900663
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200664 domain->domain.geometry.aperture_start = 0;
665 domain->domain.geometry.aperture_end = ~0UL;
666 domain->domain.geometry.force_aperture = true;
Joerg Roedel3177bb72012-07-11 12:41:10 +0200667
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200668 return &domain->domain;
KyongHo Cho2a965362012-05-12 05:56:09 +0900669
670err_counter:
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200671 free_pages((unsigned long)domain->pgtable, 2);
Marek Szyprowski58c6f6a2016-02-18 15:12:49 +0100672err_dma_cookie:
673 if (type == IOMMU_DOMAIN_DMA)
674 iommu_put_dma_cookie(&domain->domain);
KyongHo Cho2a965362012-05-12 05:56:09 +0900675err_pgtable:
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200676 kfree(domain);
Joerg Roedele1fd1ea2015-03-26 13:43:11 +0100677 return NULL;
KyongHo Cho2a965362012-05-12 05:56:09 +0900678}
679
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200680static void exynos_iommu_domain_free(struct iommu_domain *iommu_domain)
KyongHo Cho2a965362012-05-12 05:56:09 +0900681{
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200682 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200683 struct sysmmu_drvdata *data, *next;
KyongHo Cho2a965362012-05-12 05:56:09 +0900684 unsigned long flags;
685 int i;
686
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200687 WARN_ON(!list_empty(&domain->clients));
KyongHo Cho2a965362012-05-12 05:56:09 +0900688
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200689 spin_lock_irqsave(&domain->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900690
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200691 list_for_each_entry_safe(data, next, &domain->clients, domain_node) {
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200692 if (__sysmmu_disable(data))
693 data->master = NULL;
694 list_del_init(&data->domain_node);
KyongHo Cho2a965362012-05-12 05:56:09 +0900695 }
696
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200697 spin_unlock_irqrestore(&domain->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900698
Marek Szyprowski58c6f6a2016-02-18 15:12:49 +0100699 if (iommu_domain->type == IOMMU_DOMAIN_DMA)
700 iommu_put_dma_cookie(iommu_domain);
701
Marek Szyprowski5e3435e2016-02-18 15:12:50 +0100702 dma_unmap_single(dma_dev, virt_to_phys(domain->pgtable), LV1TABLE_SIZE,
703 DMA_TO_DEVICE);
704
KyongHo Cho2a965362012-05-12 05:56:09 +0900705 for (i = 0; i < NUM_LV1ENTRIES; i++)
Marek Szyprowski5e3435e2016-02-18 15:12:50 +0100706 if (lv1ent_page(domain->pgtable + i)) {
707 phys_addr_t base = lv2table_base(domain->pgtable + i);
708
709 dma_unmap_single(dma_dev, base, LV2TABLE_SIZE,
710 DMA_TO_DEVICE);
Cho KyongHo734c3c72014-05-12 11:44:48 +0530711 kmem_cache_free(lv2table_kmem_cache,
Marek Szyprowski5e3435e2016-02-18 15:12:50 +0100712 phys_to_virt(base));
713 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900714
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200715 free_pages((unsigned long)domain->pgtable, 2);
716 free_pages((unsigned long)domain->lv2entcnt, 1);
717 kfree(domain);
KyongHo Cho2a965362012-05-12 05:56:09 +0900718}
719
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200720static int exynos_iommu_attach_device(struct iommu_domain *iommu_domain,
KyongHo Cho2a965362012-05-12 05:56:09 +0900721 struct device *dev)
722{
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530723 struct exynos_iommu_owner *owner = dev->archdata.iommu;
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200724 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200725 struct sysmmu_drvdata *data;
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200726 phys_addr_t pagetable = virt_to_phys(domain->pgtable);
KyongHo Cho2a965362012-05-12 05:56:09 +0900727 unsigned long flags;
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200728 int ret = -ENODEV;
KyongHo Cho2a965362012-05-12 05:56:09 +0900729
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200730 if (!has_sysmmu(dev))
731 return -ENODEV;
KyongHo Cho2a965362012-05-12 05:56:09 +0900732
Marek Szyprowski1b092052015-05-19 15:20:33 +0200733 list_for_each_entry(data, &owner->controllers, owner_node) {
Marek Szyprowskice70ca52015-05-19 15:20:34 +0200734 pm_runtime_get_sync(data->sysmmu);
Marek Szyprowskia9133b992015-05-19 15:20:29 +0200735 ret = __sysmmu_enable(data, pagetable, domain);
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200736 if (ret >= 0) {
737 data->master = dev;
KyongHo Cho2a965362012-05-12 05:56:09 +0900738
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200739 spin_lock_irqsave(&domain->lock, flags);
740 list_add_tail(&data->domain_node, &domain->clients);
741 spin_unlock_irqrestore(&domain->lock, flags);
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200742 }
743 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900744
745 if (ret < 0) {
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530746 dev_err(dev, "%s: Failed to attach IOMMU with pgtable %pa\n",
747 __func__, &pagetable);
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530748 return ret;
KyongHo Cho2a965362012-05-12 05:56:09 +0900749 }
750
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530751 dev_dbg(dev, "%s: Attached IOMMU with pgtable %pa %s\n",
752 __func__, &pagetable, (ret == 0) ? "" : ", again");
753
KyongHo Cho2a965362012-05-12 05:56:09 +0900754 return ret;
755}
756
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200757static void exynos_iommu_detach_device(struct iommu_domain *iommu_domain,
KyongHo Cho2a965362012-05-12 05:56:09 +0900758 struct device *dev)
759{
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200760 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
761 phys_addr_t pagetable = virt_to_phys(domain->pgtable);
Marek Szyprowski1b092052015-05-19 15:20:33 +0200762 struct sysmmu_drvdata *data, *next;
KyongHo Cho2a965362012-05-12 05:56:09 +0900763 unsigned long flags;
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200764 bool found = false;
765
766 if (!has_sysmmu(dev))
767 return;
KyongHo Cho2a965362012-05-12 05:56:09 +0900768
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200769 spin_lock_irqsave(&domain->lock, flags);
Marek Szyprowski1b092052015-05-19 15:20:33 +0200770 list_for_each_entry_safe(data, next, &domain->clients, domain_node) {
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200771 if (data->master == dev) {
772 if (__sysmmu_disable(data)) {
773 data->master = NULL;
774 list_del_init(&data->domain_node);
775 }
Marek Szyprowskice70ca52015-05-19 15:20:34 +0200776 pm_runtime_put(data->sysmmu);
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200777 found = true;
KyongHo Cho2a965362012-05-12 05:56:09 +0900778 }
779 }
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200780 spin_unlock_irqrestore(&domain->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900781
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200782 if (found)
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530783 dev_dbg(dev, "%s: Detached IOMMU with pgtable %pa\n",
784 __func__, &pagetable);
785 else
786 dev_err(dev, "%s: No IOMMU is attached\n", __func__);
KyongHo Cho2a965362012-05-12 05:56:09 +0900787}
788
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200789static sysmmu_pte_t *alloc_lv2entry(struct exynos_iommu_domain *domain,
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530790 sysmmu_pte_t *sent, sysmmu_iova_t iova, short *pgcounter)
KyongHo Cho2a965362012-05-12 05:56:09 +0900791{
Cho KyongHo61128f02014-05-12 11:44:47 +0530792 if (lv1ent_section(sent)) {
Cho KyongHod09d78f2014-05-12 11:44:58 +0530793 WARN(1, "Trying mapping on %#08x mapped with 1MiB page", iova);
Cho KyongHo61128f02014-05-12 11:44:47 +0530794 return ERR_PTR(-EADDRINUSE);
795 }
796
KyongHo Cho2a965362012-05-12 05:56:09 +0900797 if (lv1ent_fault(sent)) {
Cho KyongHod09d78f2014-05-12 11:44:58 +0530798 sysmmu_pte_t *pent;
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530799 bool need_flush_flpd_cache = lv1ent_zero(sent);
KyongHo Cho2a965362012-05-12 05:56:09 +0900800
Cho KyongHo734c3c72014-05-12 11:44:48 +0530801 pent = kmem_cache_zalloc(lv2table_kmem_cache, GFP_ATOMIC);
Cho KyongHod09d78f2014-05-12 11:44:58 +0530802 BUG_ON((unsigned int)pent & (LV2TABLE_SIZE - 1));
KyongHo Cho2a965362012-05-12 05:56:09 +0900803 if (!pent)
Cho KyongHo61128f02014-05-12 11:44:47 +0530804 return ERR_PTR(-ENOMEM);
KyongHo Cho2a965362012-05-12 05:56:09 +0900805
Marek Szyprowski5e3435e2016-02-18 15:12:50 +0100806 update_pte(sent, mk_lv1ent_page(virt_to_phys(pent)));
Colin Crossdc3814f2015-05-08 17:05:44 -0700807 kmemleak_ignore(pent);
KyongHo Cho2a965362012-05-12 05:56:09 +0900808 *pgcounter = NUM_LV2ENTRIES;
Marek Szyprowski5e3435e2016-02-18 15:12:50 +0100809 dma_map_single(dma_dev, pent, LV2TABLE_SIZE, DMA_TO_DEVICE);
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530810
811 /*
Sachin Kamatf171aba2014-08-04 10:06:28 +0530812 * If pre-fetched SLPD is a faulty SLPD in zero_l2_table,
813 * FLPD cache may cache the address of zero_l2_table. This
814 * function replaces the zero_l2_table with new L2 page table
815 * to write valid mappings.
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530816 * Accessing the valid area may cause page fault since FLPD
Sachin Kamatf171aba2014-08-04 10:06:28 +0530817 * cache may still cache zero_l2_table for the valid area
818 * instead of new L2 page table that has the mapping
819 * information of the valid area.
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530820 * Thus any replacement of zero_l2_table with other valid L2
821 * page table must involve FLPD cache invalidation for System
822 * MMU v3.3.
823 * FLPD cache invalidation is performed with TLB invalidation
824 * by VPN without blocking. It is safe to invalidate TLB without
825 * blocking because the target address of TLB invalidation is
826 * not currently mapped.
827 */
828 if (need_flush_flpd_cache) {
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200829 struct sysmmu_drvdata *data;
Sachin Kamat365409d2014-05-22 09:50:56 +0530830
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200831 spin_lock(&domain->lock);
832 list_for_each_entry(data, &domain->clients, domain_node)
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200833 sysmmu_tlb_invalidate_flpdcache(data, iova);
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200834 spin_unlock(&domain->lock);
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530835 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900836 }
837
838 return page_entry(sent, iova);
839}
840
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200841static int lv1set_section(struct exynos_iommu_domain *domain,
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530842 sysmmu_pte_t *sent, sysmmu_iova_t iova,
Cho KyongHo61128f02014-05-12 11:44:47 +0530843 phys_addr_t paddr, short *pgcnt)
KyongHo Cho2a965362012-05-12 05:56:09 +0900844{
Cho KyongHo61128f02014-05-12 11:44:47 +0530845 if (lv1ent_section(sent)) {
Cho KyongHod09d78f2014-05-12 11:44:58 +0530846 WARN(1, "Trying mapping on 1MiB@%#08x that is mapped",
Cho KyongHo61128f02014-05-12 11:44:47 +0530847 iova);
KyongHo Cho2a965362012-05-12 05:56:09 +0900848 return -EADDRINUSE;
Cho KyongHo61128f02014-05-12 11:44:47 +0530849 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900850
851 if (lv1ent_page(sent)) {
Cho KyongHo61128f02014-05-12 11:44:47 +0530852 if (*pgcnt != NUM_LV2ENTRIES) {
Cho KyongHod09d78f2014-05-12 11:44:58 +0530853 WARN(1, "Trying mapping on 1MiB@%#08x that is mapped",
Cho KyongHo61128f02014-05-12 11:44:47 +0530854 iova);
KyongHo Cho2a965362012-05-12 05:56:09 +0900855 return -EADDRINUSE;
Cho KyongHo61128f02014-05-12 11:44:47 +0530856 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900857
Cho KyongHo734c3c72014-05-12 11:44:48 +0530858 kmem_cache_free(lv2table_kmem_cache, page_entry(sent, 0));
KyongHo Cho2a965362012-05-12 05:56:09 +0900859 *pgcnt = 0;
860 }
861
Marek Szyprowski5e3435e2016-02-18 15:12:50 +0100862 update_pte(sent, mk_lv1ent_sect(paddr));
KyongHo Cho2a965362012-05-12 05:56:09 +0900863
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200864 spin_lock(&domain->lock);
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530865 if (lv1ent_page_zero(sent)) {
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200866 struct sysmmu_drvdata *data;
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530867 /*
868 * Flushing FLPD cache in System MMU v3.3 that may cache a FLPD
869 * entry by speculative prefetch of SLPD which has no mapping.
870 */
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200871 list_for_each_entry(data, &domain->clients, domain_node)
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200872 sysmmu_tlb_invalidate_flpdcache(data, iova);
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530873 }
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200874 spin_unlock(&domain->lock);
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530875
KyongHo Cho2a965362012-05-12 05:56:09 +0900876 return 0;
877}
878
Cho KyongHod09d78f2014-05-12 11:44:58 +0530879static int lv2set_page(sysmmu_pte_t *pent, phys_addr_t paddr, size_t size,
KyongHo Cho2a965362012-05-12 05:56:09 +0900880 short *pgcnt)
881{
882 if (size == SPAGE_SIZE) {
Cho KyongHo0bf4e542014-05-12 11:45:00 +0530883 if (WARN_ON(!lv2ent_fault(pent)))
KyongHo Cho2a965362012-05-12 05:56:09 +0900884 return -EADDRINUSE;
885
Marek Szyprowski5e3435e2016-02-18 15:12:50 +0100886 update_pte(pent, mk_lv2ent_spage(paddr));
KyongHo Cho2a965362012-05-12 05:56:09 +0900887 *pgcnt -= 1;
888 } else { /* size == LPAGE_SIZE */
889 int i;
Marek Szyprowski5e3435e2016-02-18 15:12:50 +0100890 dma_addr_t pent_base = virt_to_phys(pent);
Sachin Kamat365409d2014-05-22 09:50:56 +0530891
Marek Szyprowski5e3435e2016-02-18 15:12:50 +0100892 dma_sync_single_for_cpu(dma_dev, pent_base,
893 sizeof(*pent) * SPAGES_PER_LPAGE,
894 DMA_TO_DEVICE);
KyongHo Cho2a965362012-05-12 05:56:09 +0900895 for (i = 0; i < SPAGES_PER_LPAGE; i++, pent++) {
Cho KyongHo0bf4e542014-05-12 11:45:00 +0530896 if (WARN_ON(!lv2ent_fault(pent))) {
Cho KyongHo61128f02014-05-12 11:44:47 +0530897 if (i > 0)
898 memset(pent - i, 0, sizeof(*pent) * i);
KyongHo Cho2a965362012-05-12 05:56:09 +0900899 return -EADDRINUSE;
900 }
901
902 *pent = mk_lv2ent_lpage(paddr);
903 }
Marek Szyprowski5e3435e2016-02-18 15:12:50 +0100904 dma_sync_single_for_device(dma_dev, pent_base,
905 sizeof(*pent) * SPAGES_PER_LPAGE,
906 DMA_TO_DEVICE);
KyongHo Cho2a965362012-05-12 05:56:09 +0900907 *pgcnt -= SPAGES_PER_LPAGE;
908 }
909
910 return 0;
911}
912
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530913/*
914 * *CAUTION* to the I/O virtual memory managers that support exynos-iommu:
915 *
Sachin Kamatf171aba2014-08-04 10:06:28 +0530916 * System MMU v3.x has advanced logic to improve address translation
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530917 * performance with caching more page table entries by a page table walk.
Sachin Kamatf171aba2014-08-04 10:06:28 +0530918 * However, the logic has a bug that while caching faulty page table entries,
919 * System MMU reports page fault if the cached fault entry is hit even though
920 * the fault entry is updated to a valid entry after the entry is cached.
921 * To prevent caching faulty page table entries which may be updated to valid
922 * entries later, the virtual memory manager should care about the workaround
923 * for the problem. The following describes the workaround.
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530924 *
925 * Any two consecutive I/O virtual address regions must have a hole of 128KiB
Sachin Kamatf171aba2014-08-04 10:06:28 +0530926 * at maximum to prevent misbehavior of System MMU 3.x (workaround for h/w bug).
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530927 *
Sachin Kamatf171aba2014-08-04 10:06:28 +0530928 * Precisely, any start address of I/O virtual region must be aligned with
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530929 * the following sizes for System MMU v3.1 and v3.2.
930 * System MMU v3.1: 128KiB
931 * System MMU v3.2: 256KiB
932 *
933 * Because System MMU v3.3 caches page table entries more aggressively, it needs
Sachin Kamatf171aba2014-08-04 10:06:28 +0530934 * more workarounds.
935 * - Any two consecutive I/O virtual regions must have a hole of size larger
936 * than or equal to 128KiB.
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530937 * - Start address of an I/O virtual region must be aligned by 128KiB.
938 */
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200939static int exynos_iommu_map(struct iommu_domain *iommu_domain,
940 unsigned long l_iova, phys_addr_t paddr, size_t size,
941 int prot)
KyongHo Cho2a965362012-05-12 05:56:09 +0900942{
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200943 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
Cho KyongHod09d78f2014-05-12 11:44:58 +0530944 sysmmu_pte_t *entry;
945 sysmmu_iova_t iova = (sysmmu_iova_t)l_iova;
KyongHo Cho2a965362012-05-12 05:56:09 +0900946 unsigned long flags;
947 int ret = -ENOMEM;
948
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200949 BUG_ON(domain->pgtable == NULL);
KyongHo Cho2a965362012-05-12 05:56:09 +0900950
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200951 spin_lock_irqsave(&domain->pgtablelock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900952
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200953 entry = section_entry(domain->pgtable, iova);
KyongHo Cho2a965362012-05-12 05:56:09 +0900954
955 if (size == SECT_SIZE) {
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200956 ret = lv1set_section(domain, entry, iova, paddr,
957 &domain->lv2entcnt[lv1ent_offset(iova)]);
KyongHo Cho2a965362012-05-12 05:56:09 +0900958 } else {
Cho KyongHod09d78f2014-05-12 11:44:58 +0530959 sysmmu_pte_t *pent;
KyongHo Cho2a965362012-05-12 05:56:09 +0900960
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200961 pent = alloc_lv2entry(domain, entry, iova,
962 &domain->lv2entcnt[lv1ent_offset(iova)]);
KyongHo Cho2a965362012-05-12 05:56:09 +0900963
Cho KyongHo61128f02014-05-12 11:44:47 +0530964 if (IS_ERR(pent))
965 ret = PTR_ERR(pent);
KyongHo Cho2a965362012-05-12 05:56:09 +0900966 else
967 ret = lv2set_page(pent, paddr, size,
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200968 &domain->lv2entcnt[lv1ent_offset(iova)]);
KyongHo Cho2a965362012-05-12 05:56:09 +0900969 }
970
Cho KyongHo61128f02014-05-12 11:44:47 +0530971 if (ret)
Cho KyongHo0bf4e542014-05-12 11:45:00 +0530972 pr_err("%s: Failed(%d) to map %#zx bytes @ %#x\n",
973 __func__, ret, size, iova);
KyongHo Cho2a965362012-05-12 05:56:09 +0900974
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200975 spin_unlock_irqrestore(&domain->pgtablelock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900976
977 return ret;
978}
979
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200980static void exynos_iommu_tlb_invalidate_entry(struct exynos_iommu_domain *domain,
981 sysmmu_iova_t iova, size_t size)
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530982{
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200983 struct sysmmu_drvdata *data;
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530984 unsigned long flags;
985
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200986 spin_lock_irqsave(&domain->lock, flags);
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530987
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200988 list_for_each_entry(data, &domain->clients, domain_node)
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200989 sysmmu_tlb_invalidate_entry(data, iova, size);
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530990
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200991 spin_unlock_irqrestore(&domain->lock, flags);
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530992}
993
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200994static size_t exynos_iommu_unmap(struct iommu_domain *iommu_domain,
995 unsigned long l_iova, size_t size)
KyongHo Cho2a965362012-05-12 05:56:09 +0900996{
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200997 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
Cho KyongHod09d78f2014-05-12 11:44:58 +0530998 sysmmu_iova_t iova = (sysmmu_iova_t)l_iova;
999 sysmmu_pte_t *ent;
Cho KyongHo61128f02014-05-12 11:44:47 +05301000 size_t err_pgsize;
Cho KyongHod09d78f2014-05-12 11:44:58 +05301001 unsigned long flags;
KyongHo Cho2a965362012-05-12 05:56:09 +09001002
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001003 BUG_ON(domain->pgtable == NULL);
KyongHo Cho2a965362012-05-12 05:56:09 +09001004
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001005 spin_lock_irqsave(&domain->pgtablelock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +09001006
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001007 ent = section_entry(domain->pgtable, iova);
KyongHo Cho2a965362012-05-12 05:56:09 +09001008
1009 if (lv1ent_section(ent)) {
Cho KyongHo0bf4e542014-05-12 11:45:00 +05301010 if (WARN_ON(size < SECT_SIZE)) {
Cho KyongHo61128f02014-05-12 11:44:47 +05301011 err_pgsize = SECT_SIZE;
1012 goto err;
1013 }
KyongHo Cho2a965362012-05-12 05:56:09 +09001014
Sachin Kamatf171aba2014-08-04 10:06:28 +05301015 /* workaround for h/w bug in System MMU v3.3 */
Marek Szyprowski5e3435e2016-02-18 15:12:50 +01001016 update_pte(ent, ZERO_LV2LINK);
KyongHo Cho2a965362012-05-12 05:56:09 +09001017 size = SECT_SIZE;
1018 goto done;
1019 }
1020
1021 if (unlikely(lv1ent_fault(ent))) {
1022 if (size > SECT_SIZE)
1023 size = SECT_SIZE;
1024 goto done;
1025 }
1026
1027 /* lv1ent_page(sent) == true here */
1028
1029 ent = page_entry(ent, iova);
1030
1031 if (unlikely(lv2ent_fault(ent))) {
1032 size = SPAGE_SIZE;
1033 goto done;
1034 }
1035
1036 if (lv2ent_small(ent)) {
Marek Szyprowski5e3435e2016-02-18 15:12:50 +01001037 update_pte(ent, 0);
KyongHo Cho2a965362012-05-12 05:56:09 +09001038 size = SPAGE_SIZE;
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001039 domain->lv2entcnt[lv1ent_offset(iova)] += 1;
KyongHo Cho2a965362012-05-12 05:56:09 +09001040 goto done;
1041 }
1042
1043 /* lv1ent_large(ent) == true here */
Cho KyongHo0bf4e542014-05-12 11:45:00 +05301044 if (WARN_ON(size < LPAGE_SIZE)) {
Cho KyongHo61128f02014-05-12 11:44:47 +05301045 err_pgsize = LPAGE_SIZE;
1046 goto err;
1047 }
KyongHo Cho2a965362012-05-12 05:56:09 +09001048
Marek Szyprowski5e3435e2016-02-18 15:12:50 +01001049 dma_sync_single_for_cpu(dma_dev, virt_to_phys(ent),
1050 sizeof(*ent) * SPAGES_PER_LPAGE,
1051 DMA_TO_DEVICE);
KyongHo Cho2a965362012-05-12 05:56:09 +09001052 memset(ent, 0, sizeof(*ent) * SPAGES_PER_LPAGE);
Marek Szyprowski5e3435e2016-02-18 15:12:50 +01001053 dma_sync_single_for_device(dma_dev, virt_to_phys(ent),
1054 sizeof(*ent) * SPAGES_PER_LPAGE,
1055 DMA_TO_DEVICE);
KyongHo Cho2a965362012-05-12 05:56:09 +09001056 size = LPAGE_SIZE;
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001057 domain->lv2entcnt[lv1ent_offset(iova)] += SPAGES_PER_LPAGE;
KyongHo Cho2a965362012-05-12 05:56:09 +09001058done:
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001059 spin_unlock_irqrestore(&domain->pgtablelock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +09001060
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001061 exynos_iommu_tlb_invalidate_entry(domain, iova, size);
KyongHo Cho2a965362012-05-12 05:56:09 +09001062
KyongHo Cho2a965362012-05-12 05:56:09 +09001063 return size;
Cho KyongHo61128f02014-05-12 11:44:47 +05301064err:
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001065 spin_unlock_irqrestore(&domain->pgtablelock, flags);
Cho KyongHo61128f02014-05-12 11:44:47 +05301066
Cho KyongHo0bf4e542014-05-12 11:45:00 +05301067 pr_err("%s: Failed: size(%#zx) @ %#x is smaller than page size %#zx\n",
1068 __func__, size, iova, err_pgsize);
Cho KyongHo61128f02014-05-12 11:44:47 +05301069
1070 return 0;
KyongHo Cho2a965362012-05-12 05:56:09 +09001071}
1072
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001073static phys_addr_t exynos_iommu_iova_to_phys(struct iommu_domain *iommu_domain,
Varun Sethibb5547a2013-03-29 01:23:58 +05301074 dma_addr_t iova)
KyongHo Cho2a965362012-05-12 05:56:09 +09001075{
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001076 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
Cho KyongHod09d78f2014-05-12 11:44:58 +05301077 sysmmu_pte_t *entry;
KyongHo Cho2a965362012-05-12 05:56:09 +09001078 unsigned long flags;
1079 phys_addr_t phys = 0;
1080
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001081 spin_lock_irqsave(&domain->pgtablelock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +09001082
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001083 entry = section_entry(domain->pgtable, iova);
KyongHo Cho2a965362012-05-12 05:56:09 +09001084
1085 if (lv1ent_section(entry)) {
1086 phys = section_phys(entry) + section_offs(iova);
1087 } else if (lv1ent_page(entry)) {
1088 entry = page_entry(entry, iova);
1089
1090 if (lv2ent_large(entry))
1091 phys = lpage_phys(entry) + lpage_offs(iova);
1092 else if (lv2ent_small(entry))
1093 phys = spage_phys(entry) + spage_offs(iova);
1094 }
1095
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001096 spin_unlock_irqrestore(&domain->pgtablelock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +09001097
1098 return phys;
1099}
1100
Marek Szyprowski6c2ae7e2016-02-18 15:12:48 +01001101static struct iommu_group *get_device_iommu_group(struct device *dev)
1102{
1103 struct iommu_group *group;
1104
1105 group = iommu_group_get(dev);
1106 if (!group)
1107 group = iommu_group_alloc();
1108
1109 return group;
1110}
1111
Antonios Motakisbf4a1c92014-05-12 11:44:59 +05301112static int exynos_iommu_add_device(struct device *dev)
1113{
1114 struct iommu_group *group;
Antonios Motakisbf4a1c92014-05-12 11:44:59 +05301115
Marek Szyprowski06801db2015-05-19 15:20:32 +02001116 if (!has_sysmmu(dev))
1117 return -ENODEV;
1118
Marek Szyprowski6c2ae7e2016-02-18 15:12:48 +01001119 group = iommu_group_get_for_dev(dev);
Antonios Motakisbf4a1c92014-05-12 11:44:59 +05301120
Marek Szyprowski6c2ae7e2016-02-18 15:12:48 +01001121 if (IS_ERR(group))
1122 return PTR_ERR(group);
Antonios Motakisbf4a1c92014-05-12 11:44:59 +05301123
Antonios Motakisbf4a1c92014-05-12 11:44:59 +05301124 iommu_group_put(group);
1125
Marek Szyprowski6c2ae7e2016-02-18 15:12:48 +01001126 return 0;
Antonios Motakisbf4a1c92014-05-12 11:44:59 +05301127}
1128
1129static void exynos_iommu_remove_device(struct device *dev)
1130{
Marek Szyprowski06801db2015-05-19 15:20:32 +02001131 if (!has_sysmmu(dev))
1132 return;
1133
Antonios Motakisbf4a1c92014-05-12 11:44:59 +05301134 iommu_group_remove_device(dev);
1135}
1136
Marek Szyprowskiaa759fd2015-05-19 15:20:37 +02001137static int exynos_iommu_of_xlate(struct device *dev,
1138 struct of_phandle_args *spec)
1139{
1140 struct exynos_iommu_owner *owner = dev->archdata.iommu;
1141 struct platform_device *sysmmu = of_find_device_by_node(spec->np);
1142 struct sysmmu_drvdata *data;
1143
1144 if (!sysmmu)
1145 return -ENODEV;
1146
1147 data = platform_get_drvdata(sysmmu);
1148 if (!data)
1149 return -ENODEV;
1150
1151 if (!owner) {
1152 owner = kzalloc(sizeof(*owner), GFP_KERNEL);
1153 if (!owner)
1154 return -ENOMEM;
1155
1156 INIT_LIST_HEAD(&owner->controllers);
1157 dev->archdata.iommu = owner;
1158 }
1159
1160 list_add_tail(&data->owner_node, &owner->controllers);
1161 return 0;
1162}
1163
Marek Szyprowski8ed55c82015-05-19 15:20:36 +02001164static struct iommu_ops exynos_iommu_ops = {
Joerg Roedele1fd1ea2015-03-26 13:43:11 +01001165 .domain_alloc = exynos_iommu_domain_alloc,
1166 .domain_free = exynos_iommu_domain_free,
Bjorn Helgaasba5fa6f2014-05-08 14:49:14 -06001167 .attach_dev = exynos_iommu_attach_device,
1168 .detach_dev = exynos_iommu_detach_device,
1169 .map = exynos_iommu_map,
1170 .unmap = exynos_iommu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -07001171 .map_sg = default_iommu_map_sg,
Bjorn Helgaasba5fa6f2014-05-08 14:49:14 -06001172 .iova_to_phys = exynos_iommu_iova_to_phys,
Marek Szyprowski6c2ae7e2016-02-18 15:12:48 +01001173 .device_group = get_device_iommu_group,
Bjorn Helgaasba5fa6f2014-05-08 14:49:14 -06001174 .add_device = exynos_iommu_add_device,
1175 .remove_device = exynos_iommu_remove_device,
KyongHo Cho2a965362012-05-12 05:56:09 +09001176 .pgsize_bitmap = SECT_SIZE | LPAGE_SIZE | SPAGE_SIZE,
Marek Szyprowskiaa759fd2015-05-19 15:20:37 +02001177 .of_xlate = exynos_iommu_of_xlate,
KyongHo Cho2a965362012-05-12 05:56:09 +09001178};
1179
Marek Szyprowski8ed55c82015-05-19 15:20:36 +02001180static bool init_done;
1181
KyongHo Cho2a965362012-05-12 05:56:09 +09001182static int __init exynos_iommu_init(void)
1183{
1184 int ret;
1185
Cho KyongHo734c3c72014-05-12 11:44:48 +05301186 lv2table_kmem_cache = kmem_cache_create("exynos-iommu-lv2table",
1187 LV2TABLE_SIZE, LV2TABLE_SIZE, 0, NULL);
1188 if (!lv2table_kmem_cache) {
1189 pr_err("%s: Failed to create kmem cache\n", __func__);
1190 return -ENOMEM;
1191 }
1192
KyongHo Cho2a965362012-05-12 05:56:09 +09001193 ret = platform_driver_register(&exynos_sysmmu_driver);
Cho KyongHo734c3c72014-05-12 11:44:48 +05301194 if (ret) {
1195 pr_err("%s: Failed to register driver\n", __func__);
1196 goto err_reg_driver;
1197 }
KyongHo Cho2a965362012-05-12 05:56:09 +09001198
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301199 zero_lv2_table = kmem_cache_zalloc(lv2table_kmem_cache, GFP_KERNEL);
1200 if (zero_lv2_table == NULL) {
1201 pr_err("%s: Failed to allocate zero level2 page table\n",
1202 __func__);
1203 ret = -ENOMEM;
1204 goto err_zero_lv2;
1205 }
1206
Cho KyongHo734c3c72014-05-12 11:44:48 +05301207 ret = bus_set_iommu(&platform_bus_type, &exynos_iommu_ops);
1208 if (ret) {
1209 pr_err("%s: Failed to register exynos-iommu driver.\n",
1210 __func__);
1211 goto err_set_iommu;
1212 }
KyongHo Cho2a965362012-05-12 05:56:09 +09001213
Marek Szyprowski8ed55c82015-05-19 15:20:36 +02001214 init_done = true;
1215
Cho KyongHo734c3c72014-05-12 11:44:48 +05301216 return 0;
1217err_set_iommu:
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301218 kmem_cache_free(lv2table_kmem_cache, zero_lv2_table);
1219err_zero_lv2:
Cho KyongHo734c3c72014-05-12 11:44:48 +05301220 platform_driver_unregister(&exynos_sysmmu_driver);
1221err_reg_driver:
1222 kmem_cache_destroy(lv2table_kmem_cache);
KyongHo Cho2a965362012-05-12 05:56:09 +09001223 return ret;
1224}
Marek Szyprowski8ed55c82015-05-19 15:20:36 +02001225
1226static int __init exynos_iommu_of_setup(struct device_node *np)
1227{
1228 struct platform_device *pdev;
1229
1230 if (!init_done)
1231 exynos_iommu_init();
1232
1233 pdev = of_platform_device_create(np, NULL, platform_bus_type.dev_root);
1234 if (IS_ERR(pdev))
1235 return PTR_ERR(pdev);
1236
Marek Szyprowski5e3435e2016-02-18 15:12:50 +01001237 /*
1238 * use the first registered sysmmu device for performing
1239 * dma mapping operations on iommu page tables (cpu cache flush)
1240 */
1241 if (!dma_dev)
1242 dma_dev = &pdev->dev;
1243
Marek Szyprowski8ed55c82015-05-19 15:20:36 +02001244 of_iommu_set_ops(np, &exynos_iommu_ops);
1245 return 0;
1246}
1247
1248IOMMU_OF_DECLARE(exynos_iommu_of, "samsung,exynos-sysmmu",
1249 exynos_iommu_of_setup);