blob: 25c2c75f5332efe3e98d7e1c290db71d515364f5 [file] [log] [blame]
Marek Szyprowski740a01e2016-02-18 15:12:58 +01001/*
2 * Copyright (c) 2011,2016 Samsung Electronics Co., Ltd.
KyongHo Cho2a965362012-05-12 05:56:09 +09003 * http://www.samsung.com
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9
10#ifdef CONFIG_EXYNOS_IOMMU_DEBUG
11#define DEBUG
12#endif
13
KyongHo Cho2a965362012-05-12 05:56:09 +090014#include <linux/clk.h>
Marek Szyprowski8ed55c82015-05-19 15:20:36 +020015#include <linux/dma-mapping.h>
KyongHo Cho2a965362012-05-12 05:56:09 +090016#include <linux/err.h>
Marek Szyprowski312900c2015-05-19 15:20:30 +020017#include <linux/io.h>
KyongHo Cho2a965362012-05-12 05:56:09 +090018#include <linux/iommu.h>
Marek Szyprowski312900c2015-05-19 15:20:30 +020019#include <linux/interrupt.h>
KyongHo Cho2a965362012-05-12 05:56:09 +090020#include <linux/list.h>
Marek Szyprowski8ed55c82015-05-19 15:20:36 +020021#include <linux/of.h>
22#include <linux/of_iommu.h>
23#include <linux/of_platform.h>
Marek Szyprowski312900c2015-05-19 15:20:30 +020024#include <linux/platform_device.h>
25#include <linux/pm_runtime.h>
26#include <linux/slab.h>
Marek Szyprowski58c6f6a2016-02-18 15:12:49 +010027#include <linux/dma-iommu.h>
KyongHo Cho2a965362012-05-12 05:56:09 +090028
Cho KyongHod09d78f2014-05-12 11:44:58 +053029typedef u32 sysmmu_iova_t;
30typedef u32 sysmmu_pte_t;
31
Sachin Kamatf171aba2014-08-04 10:06:28 +053032/* We do not consider super section mapping (16MB) */
KyongHo Cho2a965362012-05-12 05:56:09 +090033#define SECT_ORDER 20
34#define LPAGE_ORDER 16
35#define SPAGE_ORDER 12
36
37#define SECT_SIZE (1 << SECT_ORDER)
38#define LPAGE_SIZE (1 << LPAGE_ORDER)
39#define SPAGE_SIZE (1 << SPAGE_ORDER)
40
41#define SECT_MASK (~(SECT_SIZE - 1))
42#define LPAGE_MASK (~(LPAGE_SIZE - 1))
43#define SPAGE_MASK (~(SPAGE_SIZE - 1))
44
Cho KyongHo66a7ed82014-05-12 11:45:04 +053045#define lv1ent_fault(sent) ((*(sent) == ZERO_LV2LINK) || \
46 ((*(sent) & 3) == 0) || ((*(sent) & 3) == 3))
47#define lv1ent_zero(sent) (*(sent) == ZERO_LV2LINK)
48#define lv1ent_page_zero(sent) ((*(sent) & 3) == 1)
49#define lv1ent_page(sent) ((*(sent) != ZERO_LV2LINK) && \
50 ((*(sent) & 3) == 1))
KyongHo Cho2a965362012-05-12 05:56:09 +090051#define lv1ent_section(sent) ((*(sent) & 3) == 2)
52
53#define lv2ent_fault(pent) ((*(pent) & 3) == 0)
54#define lv2ent_small(pent) ((*(pent) & 2) == 2)
55#define lv2ent_large(pent) ((*(pent) & 3) == 1)
56
Marek Szyprowski740a01e2016-02-18 15:12:58 +010057/*
58 * v1.x - v3.x SYSMMU supports 32bit physical and 32bit virtual address spaces
59 * v5.0 introduced support for 36bit physical address space by shifting
60 * all page entry values by 4 bits.
61 * All SYSMMU controllers in the system support the address spaces of the same
62 * size, so PG_ENT_SHIFT can be initialized on first SYSMMU probe to proper
63 * value (0 or 4).
64 */
65static short PG_ENT_SHIFT = -1;
66#define SYSMMU_PG_ENT_SHIFT 0
67#define SYSMMU_V5_PG_ENT_SHIFT 4
KyongHo Cho2a965362012-05-12 05:56:09 +090068
Marek Szyprowski1a0d8da2016-11-03 09:04:45 +010069static const sysmmu_pte_t *LV1_PROT;
70static const sysmmu_pte_t SYSMMU_LV1_PROT[] = {
71 ((0 << 15) | (0 << 10)), /* no access */
72 ((1 << 15) | (1 << 10)), /* IOMMU_READ only */
73 ((0 << 15) | (1 << 10)), /* IOMMU_WRITE not supported, use read/write */
74 ((0 << 15) | (1 << 10)), /* IOMMU_READ | IOMMU_WRITE */
75};
76static const sysmmu_pte_t SYSMMU_V5_LV1_PROT[] = {
77 (0 << 4), /* no access */
78 (1 << 4), /* IOMMU_READ only */
79 (2 << 4), /* IOMMU_WRITE only */
80 (3 << 4), /* IOMMU_READ | IOMMU_WRITE */
81};
82
83static const sysmmu_pte_t *LV2_PROT;
84static const sysmmu_pte_t SYSMMU_LV2_PROT[] = {
85 ((0 << 9) | (0 << 4)), /* no access */
86 ((1 << 9) | (1 << 4)), /* IOMMU_READ only */
87 ((0 << 9) | (1 << 4)), /* IOMMU_WRITE not supported, use read/write */
88 ((0 << 9) | (1 << 4)), /* IOMMU_READ | IOMMU_WRITE */
89};
90static const sysmmu_pte_t SYSMMU_V5_LV2_PROT[] = {
91 (0 << 2), /* no access */
92 (1 << 2), /* IOMMU_READ only */
93 (2 << 2), /* IOMMU_WRITE only */
94 (3 << 2), /* IOMMU_READ | IOMMU_WRITE */
95};
96
97#define SYSMMU_SUPPORTED_PROT_BITS (IOMMU_READ | IOMMU_WRITE)
98
Marek Szyprowski740a01e2016-02-18 15:12:58 +010099#define sect_to_phys(ent) (((phys_addr_t) ent) << PG_ENT_SHIFT)
100#define section_phys(sent) (sect_to_phys(*(sent)) & SECT_MASK)
101#define section_offs(iova) (iova & (SECT_SIZE - 1))
102#define lpage_phys(pent) (sect_to_phys(*(pent)) & LPAGE_MASK)
103#define lpage_offs(iova) (iova & (LPAGE_SIZE - 1))
104#define spage_phys(pent) (sect_to_phys(*(pent)) & SPAGE_MASK)
105#define spage_offs(iova) (iova & (SPAGE_SIZE - 1))
KyongHo Cho2a965362012-05-12 05:56:09 +0900106
107#define NUM_LV1ENTRIES 4096
Cho KyongHod09d78f2014-05-12 11:44:58 +0530108#define NUM_LV2ENTRIES (SECT_SIZE / SPAGE_SIZE)
KyongHo Cho2a965362012-05-12 05:56:09 +0900109
Cho KyongHod09d78f2014-05-12 11:44:58 +0530110static u32 lv1ent_offset(sysmmu_iova_t iova)
111{
112 return iova >> SECT_ORDER;
113}
114
115static u32 lv2ent_offset(sysmmu_iova_t iova)
116{
117 return (iova >> SPAGE_ORDER) & (NUM_LV2ENTRIES - 1);
118}
119
Marek Szyprowski5e3435e2016-02-18 15:12:50 +0100120#define LV1TABLE_SIZE (NUM_LV1ENTRIES * sizeof(sysmmu_pte_t))
Cho KyongHod09d78f2014-05-12 11:44:58 +0530121#define LV2TABLE_SIZE (NUM_LV2ENTRIES * sizeof(sysmmu_pte_t))
KyongHo Cho2a965362012-05-12 05:56:09 +0900122
123#define SPAGES_PER_LPAGE (LPAGE_SIZE / SPAGE_SIZE)
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100124#define lv2table_base(sent) (sect_to_phys(*(sent) & 0xFFFFFFC0))
KyongHo Cho2a965362012-05-12 05:56:09 +0900125
Marek Szyprowski1a0d8da2016-11-03 09:04:45 +0100126#define mk_lv1ent_sect(pa, prot) ((pa >> PG_ENT_SHIFT) | LV1_PROT[prot] | 2)
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100127#define mk_lv1ent_page(pa) ((pa >> PG_ENT_SHIFT) | 1)
Marek Szyprowski1a0d8da2016-11-03 09:04:45 +0100128#define mk_lv2ent_lpage(pa, prot) ((pa >> PG_ENT_SHIFT) | LV2_PROT[prot] | 1)
129#define mk_lv2ent_spage(pa, prot) ((pa >> PG_ENT_SHIFT) | LV2_PROT[prot] | 2)
KyongHo Cho2a965362012-05-12 05:56:09 +0900130
131#define CTRL_ENABLE 0x5
132#define CTRL_BLOCK 0x7
133#define CTRL_DISABLE 0x0
134
Cho KyongHoeeb51842014-05-12 11:45:03 +0530135#define CFG_LRU 0x1
Marek Szyprowski1a0d8da2016-11-03 09:04:45 +0100136#define CFG_EAP (1 << 2)
Cho KyongHoeeb51842014-05-12 11:45:03 +0530137#define CFG_QOS(n) ((n & 0xF) << 7)
Cho KyongHoeeb51842014-05-12 11:45:03 +0530138#define CFG_ACGEN (1 << 24) /* System MMU 3.3 only */
139#define CFG_SYSSEL (1 << 22) /* System MMU 3.2 only */
140#define CFG_FLPDCACHE (1 << 20) /* System MMU 3.2+ only */
141
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100142/* common registers */
KyongHo Cho2a965362012-05-12 05:56:09 +0900143#define REG_MMU_CTRL 0x000
144#define REG_MMU_CFG 0x004
145#define REG_MMU_STATUS 0x008
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100146#define REG_MMU_VERSION 0x034
147
148#define MMU_MAJ_VER(val) ((val) >> 7)
149#define MMU_MIN_VER(val) ((val) & 0x7F)
150#define MMU_RAW_VER(reg) (((reg) >> 21) & ((1 << 11) - 1)) /* 11 bits */
151
152#define MAKE_MMU_VER(maj, min) ((((maj) & 0xF) << 7) | ((min) & 0x7F))
153
154/* v1.x - v3.x registers */
KyongHo Cho2a965362012-05-12 05:56:09 +0900155#define REG_MMU_FLUSH 0x00C
156#define REG_MMU_FLUSH_ENTRY 0x010
157#define REG_PT_BASE_ADDR 0x014
158#define REG_INT_STATUS 0x018
159#define REG_INT_CLEAR 0x01C
160
161#define REG_PAGE_FAULT_ADDR 0x024
162#define REG_AW_FAULT_ADDR 0x028
163#define REG_AR_FAULT_ADDR 0x02C
164#define REG_DEFAULT_SLAVE_ADDR 0x030
165
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100166/* v5.x registers */
167#define REG_V5_PT_BASE_PFN 0x00C
168#define REG_V5_MMU_FLUSH_ALL 0x010
169#define REG_V5_MMU_FLUSH_ENTRY 0x014
Marek Szyprowskid5bf7392017-03-24 10:19:01 +0100170#define REG_V5_MMU_FLUSH_RANGE 0x018
171#define REG_V5_MMU_FLUSH_START 0x020
172#define REG_V5_MMU_FLUSH_END 0x024
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100173#define REG_V5_INT_STATUS 0x060
174#define REG_V5_INT_CLEAR 0x064
175#define REG_V5_FAULT_AR_VA 0x070
176#define REG_V5_FAULT_AW_VA 0x080
KyongHo Cho2a965362012-05-12 05:56:09 +0900177
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530178#define has_sysmmu(dev) (dev->archdata.iommu != NULL)
179
Marek Szyprowski5e3435e2016-02-18 15:12:50 +0100180static struct device *dma_dev;
Cho KyongHo734c3c72014-05-12 11:44:48 +0530181static struct kmem_cache *lv2table_kmem_cache;
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530182static sysmmu_pte_t *zero_lv2_table;
183#define ZERO_LV2LINK mk_lv1ent_page(virt_to_phys(zero_lv2_table))
Cho KyongHo734c3c72014-05-12 11:44:48 +0530184
Cho KyongHod09d78f2014-05-12 11:44:58 +0530185static sysmmu_pte_t *section_entry(sysmmu_pte_t *pgtable, sysmmu_iova_t iova)
KyongHo Cho2a965362012-05-12 05:56:09 +0900186{
187 return pgtable + lv1ent_offset(iova);
188}
189
Cho KyongHod09d78f2014-05-12 11:44:58 +0530190static sysmmu_pte_t *page_entry(sysmmu_pte_t *sent, sysmmu_iova_t iova)
KyongHo Cho2a965362012-05-12 05:56:09 +0900191{
Cho KyongHod09d78f2014-05-12 11:44:58 +0530192 return (sysmmu_pte_t *)phys_to_virt(
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530193 lv2table_base(sent)) + lv2ent_offset(iova);
KyongHo Cho2a965362012-05-12 05:56:09 +0900194}
195
Marek Szyprowskid093fc72016-02-18 15:12:53 +0100196/*
197 * IOMMU fault information register
198 */
199struct sysmmu_fault_info {
200 unsigned int bit; /* bit number in STATUS register */
201 unsigned short addr_reg; /* register to read VA fault address */
202 const char *name; /* human readable fault name */
203 unsigned int type; /* fault type for report_iommu_fault */
KyongHo Cho2a965362012-05-12 05:56:09 +0900204};
205
Marek Szyprowskid093fc72016-02-18 15:12:53 +0100206static const struct sysmmu_fault_info sysmmu_faults[] = {
207 { 0, REG_PAGE_FAULT_ADDR, "PAGE", IOMMU_FAULT_READ },
208 { 1, REG_AR_FAULT_ADDR, "AR MULTI-HIT", IOMMU_FAULT_READ },
209 { 2, REG_AW_FAULT_ADDR, "AW MULTI-HIT", IOMMU_FAULT_WRITE },
210 { 3, REG_DEFAULT_SLAVE_ADDR, "BUS ERROR", IOMMU_FAULT_READ },
211 { 4, REG_AR_FAULT_ADDR, "AR SECURITY PROTECTION", IOMMU_FAULT_READ },
212 { 5, REG_AR_FAULT_ADDR, "AR ACCESS PROTECTION", IOMMU_FAULT_READ },
213 { 6, REG_AW_FAULT_ADDR, "AW SECURITY PROTECTION", IOMMU_FAULT_WRITE },
214 { 7, REG_AW_FAULT_ADDR, "AW ACCESS PROTECTION", IOMMU_FAULT_WRITE },
KyongHo Cho2a965362012-05-12 05:56:09 +0900215};
216
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100217static const struct sysmmu_fault_info sysmmu_v5_faults[] = {
218 { 0, REG_V5_FAULT_AR_VA, "AR PTW", IOMMU_FAULT_READ },
219 { 1, REG_V5_FAULT_AR_VA, "AR PAGE", IOMMU_FAULT_READ },
220 { 2, REG_V5_FAULT_AR_VA, "AR MULTI-HIT", IOMMU_FAULT_READ },
221 { 3, REG_V5_FAULT_AR_VA, "AR ACCESS PROTECTION", IOMMU_FAULT_READ },
222 { 4, REG_V5_FAULT_AR_VA, "AR SECURITY PROTECTION", IOMMU_FAULT_READ },
223 { 16, REG_V5_FAULT_AW_VA, "AW PTW", IOMMU_FAULT_WRITE },
224 { 17, REG_V5_FAULT_AW_VA, "AW PAGE", IOMMU_FAULT_WRITE },
225 { 18, REG_V5_FAULT_AW_VA, "AW MULTI-HIT", IOMMU_FAULT_WRITE },
226 { 19, REG_V5_FAULT_AW_VA, "AW ACCESS PROTECTION", IOMMU_FAULT_WRITE },
227 { 20, REG_V5_FAULT_AW_VA, "AW SECURITY PROTECTION", IOMMU_FAULT_WRITE },
228};
229
Marek Szyprowski2860af32015-05-19 15:20:31 +0200230/*
231 * This structure is attached to dev.archdata.iommu of the master device
232 * on device add, contains a list of SYSMMU controllers defined by device tree,
233 * which are bound to given master device. It is usually referenced by 'owner'
234 * pointer.
235*/
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530236struct exynos_iommu_owner {
Marek Szyprowski1b092052015-05-19 15:20:33 +0200237 struct list_head controllers; /* list of sysmmu_drvdata.owner_node */
Marek Szyprowski5fa61cb2016-02-18 15:13:00 +0100238 struct iommu_domain *domain; /* domain this device is attached */
Marek Szyprowski9b265532016-11-14 11:08:11 +0100239 struct mutex rpm_lock; /* for runtime pm of all sysmmus */
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530240};
241
Marek Szyprowski2860af32015-05-19 15:20:31 +0200242/*
243 * This structure exynos specific generalization of struct iommu_domain.
244 * It contains list of SYSMMU controllers from all master devices, which has
245 * been attached to this domain and page tables of IO address space defined by
246 * it. It is usually referenced by 'domain' pointer.
247 */
KyongHo Cho2a965362012-05-12 05:56:09 +0900248struct exynos_iommu_domain {
Marek Szyprowski2860af32015-05-19 15:20:31 +0200249 struct list_head clients; /* list of sysmmu_drvdata.domain_node */
250 sysmmu_pte_t *pgtable; /* lv1 page table, 16KB */
251 short *lv2entcnt; /* free lv2 entry counter for each section */
252 spinlock_t lock; /* lock for modyfying list of clients */
253 spinlock_t pgtablelock; /* lock for modifying page table @ pgtable */
Joerg Roedele1fd1ea2015-03-26 13:43:11 +0100254 struct iommu_domain domain; /* generic domain data structure */
KyongHo Cho2a965362012-05-12 05:56:09 +0900255};
256
Marek Szyprowski2860af32015-05-19 15:20:31 +0200257/*
258 * This structure hold all data of a single SYSMMU controller, this includes
259 * hw resources like registers and clocks, pointers and list nodes to connect
260 * it to all other structures, internal state and parameters read from device
261 * tree. It is usually referenced by 'data' pointer.
262 */
KyongHo Cho2a965362012-05-12 05:56:09 +0900263struct sysmmu_drvdata {
Marek Szyprowski2860af32015-05-19 15:20:31 +0200264 struct device *sysmmu; /* SYSMMU controller device */
265 struct device *master; /* master device (owner) */
266 void __iomem *sfrbase; /* our registers */
267 struct clk *clk; /* SYSMMU's clock */
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100268 struct clk *aclk; /* SYSMMU's aclk clock */
269 struct clk *pclk; /* SYSMMU's pclk clock */
Marek Szyprowski2860af32015-05-19 15:20:31 +0200270 struct clk *clk_master; /* master's device clock */
Marek Szyprowski2860af32015-05-19 15:20:31 +0200271 spinlock_t lock; /* lock for modyfying state */
Marek Szyprowski47a574f2016-11-14 11:08:08 +0100272 bool active; /* current status */
Marek Szyprowski2860af32015-05-19 15:20:31 +0200273 struct exynos_iommu_domain *domain; /* domain we belong to */
274 struct list_head domain_node; /* node for domain clients list */
Marek Szyprowski1b092052015-05-19 15:20:33 +0200275 struct list_head owner_node; /* node for owner controllers list */
Marek Szyprowski2860af32015-05-19 15:20:31 +0200276 phys_addr_t pgtable; /* assigned page table structure */
277 unsigned int version; /* our version */
Joerg Roedeld2c302b2017-02-03 13:23:42 +0100278
279 struct iommu_device iommu; /* IOMMU core handle */
KyongHo Cho2a965362012-05-12 05:56:09 +0900280};
281
Joerg Roedele1fd1ea2015-03-26 13:43:11 +0100282static struct exynos_iommu_domain *to_exynos_domain(struct iommu_domain *dom)
283{
284 return container_of(dom, struct exynos_iommu_domain, domain);
285}
286
Marek Szyprowski02cdc362016-02-18 15:12:52 +0100287static void sysmmu_unblock(struct sysmmu_drvdata *data)
KyongHo Cho2a965362012-05-12 05:56:09 +0900288{
Marek Szyprowski84bd0422016-02-29 13:42:57 +0100289 writel(CTRL_ENABLE, data->sfrbase + REG_MMU_CTRL);
KyongHo Cho2a965362012-05-12 05:56:09 +0900290}
291
Marek Szyprowski02cdc362016-02-18 15:12:52 +0100292static bool sysmmu_block(struct sysmmu_drvdata *data)
KyongHo Cho2a965362012-05-12 05:56:09 +0900293{
294 int i = 120;
295
Marek Szyprowski84bd0422016-02-29 13:42:57 +0100296 writel(CTRL_BLOCK, data->sfrbase + REG_MMU_CTRL);
297 while ((i > 0) && !(readl(data->sfrbase + REG_MMU_STATUS) & 1))
KyongHo Cho2a965362012-05-12 05:56:09 +0900298 --i;
299
Marek Szyprowski84bd0422016-02-29 13:42:57 +0100300 if (!(readl(data->sfrbase + REG_MMU_STATUS) & 1)) {
Marek Szyprowski02cdc362016-02-18 15:12:52 +0100301 sysmmu_unblock(data);
KyongHo Cho2a965362012-05-12 05:56:09 +0900302 return false;
303 }
304
305 return true;
306}
307
Marek Szyprowski02cdc362016-02-18 15:12:52 +0100308static void __sysmmu_tlb_invalidate(struct sysmmu_drvdata *data)
KyongHo Cho2a965362012-05-12 05:56:09 +0900309{
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100310 if (MMU_MAJ_VER(data->version) < 5)
Marek Szyprowski84bd0422016-02-29 13:42:57 +0100311 writel(0x1, data->sfrbase + REG_MMU_FLUSH);
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100312 else
Marek Szyprowski84bd0422016-02-29 13:42:57 +0100313 writel(0x1, data->sfrbase + REG_V5_MMU_FLUSH_ALL);
KyongHo Cho2a965362012-05-12 05:56:09 +0900314}
315
Marek Szyprowski02cdc362016-02-18 15:12:52 +0100316static void __sysmmu_tlb_invalidate_entry(struct sysmmu_drvdata *data,
Cho KyongHod09d78f2014-05-12 11:44:58 +0530317 sysmmu_iova_t iova, unsigned int num_inv)
KyongHo Cho2a965362012-05-12 05:56:09 +0900318{
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530319 unsigned int i;
Sachin Kamat365409d2014-05-22 09:50:56 +0530320
Marek Szyprowskid5bf7392017-03-24 10:19:01 +0100321 if (MMU_MAJ_VER(data->version) < 5) {
322 for (i = 0; i < num_inv; i++) {
Marek Szyprowski84bd0422016-02-29 13:42:57 +0100323 writel((iova & SPAGE_MASK) | 1,
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100324 data->sfrbase + REG_MMU_FLUSH_ENTRY);
Marek Szyprowskid5bf7392017-03-24 10:19:01 +0100325 iova += SPAGE_SIZE;
326 }
327 } else {
328 if (num_inv == 1) {
Marek Szyprowski84bd0422016-02-29 13:42:57 +0100329 writel((iova & SPAGE_MASK) | 1,
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100330 data->sfrbase + REG_V5_MMU_FLUSH_ENTRY);
Marek Szyprowskid5bf7392017-03-24 10:19:01 +0100331 } else {
332 writel((iova & SPAGE_MASK),
333 data->sfrbase + REG_V5_MMU_FLUSH_START);
334 writel((iova & SPAGE_MASK) + (num_inv - 1) * SPAGE_SIZE,
335 data->sfrbase + REG_V5_MMU_FLUSH_END);
336 writel(1, data->sfrbase + REG_V5_MMU_FLUSH_RANGE);
337 }
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530338 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900339}
340
Marek Szyprowski02cdc362016-02-18 15:12:52 +0100341static void __sysmmu_set_ptbase(struct sysmmu_drvdata *data, phys_addr_t pgd)
KyongHo Cho2a965362012-05-12 05:56:09 +0900342{
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100343 if (MMU_MAJ_VER(data->version) < 5)
Marek Szyprowski84bd0422016-02-29 13:42:57 +0100344 writel(pgd, data->sfrbase + REG_PT_BASE_ADDR);
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100345 else
Marek Szyprowski84bd0422016-02-29 13:42:57 +0100346 writel(pgd >> PAGE_SHIFT,
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100347 data->sfrbase + REG_V5_PT_BASE_PFN);
KyongHo Cho2a965362012-05-12 05:56:09 +0900348
Marek Szyprowski02cdc362016-02-18 15:12:52 +0100349 __sysmmu_tlb_invalidate(data);
KyongHo Cho2a965362012-05-12 05:56:09 +0900350}
351
Marek Szyprowskifecc49d2016-05-23 11:30:09 +0200352static void __sysmmu_enable_clocks(struct sysmmu_drvdata *data)
353{
354 BUG_ON(clk_prepare_enable(data->clk_master));
355 BUG_ON(clk_prepare_enable(data->clk));
356 BUG_ON(clk_prepare_enable(data->pclk));
357 BUG_ON(clk_prepare_enable(data->aclk));
358}
359
360static void __sysmmu_disable_clocks(struct sysmmu_drvdata *data)
361{
362 clk_disable_unprepare(data->aclk);
363 clk_disable_unprepare(data->pclk);
364 clk_disable_unprepare(data->clk);
365 clk_disable_unprepare(data->clk_master);
366}
367
Marek Szyprowski850d3132016-02-18 15:12:56 +0100368static void __sysmmu_get_version(struct sysmmu_drvdata *data)
369{
370 u32 ver;
371
Marek Szyprowskifecc49d2016-05-23 11:30:09 +0200372 __sysmmu_enable_clocks(data);
Marek Szyprowski850d3132016-02-18 15:12:56 +0100373
Marek Szyprowski84bd0422016-02-29 13:42:57 +0100374 ver = readl(data->sfrbase + REG_MMU_VERSION);
Marek Szyprowski850d3132016-02-18 15:12:56 +0100375
376 /* controllers on some SoCs don't report proper version */
377 if (ver == 0x80000001u)
378 data->version = MAKE_MMU_VER(1, 0);
379 else
380 data->version = MMU_RAW_VER(ver);
381
382 dev_dbg(data->sysmmu, "hardware version: %d.%d\n",
383 MMU_MAJ_VER(data->version), MMU_MIN_VER(data->version));
384
Marek Szyprowskifecc49d2016-05-23 11:30:09 +0200385 __sysmmu_disable_clocks(data);
Marek Szyprowski850d3132016-02-18 15:12:56 +0100386}
387
Marek Szyprowskid093fc72016-02-18 15:12:53 +0100388static void show_fault_information(struct sysmmu_drvdata *data,
389 const struct sysmmu_fault_info *finfo,
390 sysmmu_iova_t fault_addr)
KyongHo Cho2a965362012-05-12 05:56:09 +0900391{
Cho KyongHod09d78f2014-05-12 11:44:58 +0530392 sysmmu_pte_t *ent;
KyongHo Cho2a965362012-05-12 05:56:09 +0900393
Marek Szyprowskiec5d2412017-01-09 13:03:53 +0100394 dev_err(data->sysmmu, "%s: %s FAULT occurred at %#x\n",
395 dev_name(data->master), finfo->name, fault_addr);
396 dev_dbg(data->sysmmu, "Page table base: %pa\n", &data->pgtable);
Marek Szyprowskid093fc72016-02-18 15:12:53 +0100397 ent = section_entry(phys_to_virt(data->pgtable), fault_addr);
Marek Szyprowskiec5d2412017-01-09 13:03:53 +0100398 dev_dbg(data->sysmmu, "\tLv1 entry: %#x\n", *ent);
KyongHo Cho2a965362012-05-12 05:56:09 +0900399 if (lv1ent_page(ent)) {
400 ent = page_entry(ent, fault_addr);
Marek Szyprowskiec5d2412017-01-09 13:03:53 +0100401 dev_dbg(data->sysmmu, "\t Lv2 entry: %#x\n", *ent);
KyongHo Cho2a965362012-05-12 05:56:09 +0900402 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900403}
404
405static irqreturn_t exynos_sysmmu_irq(int irq, void *dev_id)
406{
Sachin Kamatf171aba2014-08-04 10:06:28 +0530407 /* SYSMMU is in blocked state when interrupt occurred. */
KyongHo Cho2a965362012-05-12 05:56:09 +0900408 struct sysmmu_drvdata *data = dev_id;
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100409 const struct sysmmu_fault_info *finfo;
410 unsigned int i, n, itype;
Marek Szyprowskid093fc72016-02-18 15:12:53 +0100411 sysmmu_iova_t fault_addr = -1;
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100412 unsigned short reg_status, reg_clear;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530413 int ret = -ENOSYS;
KyongHo Cho2a965362012-05-12 05:56:09 +0900414
Marek Szyprowski47a574f2016-11-14 11:08:08 +0100415 WARN_ON(!data->active);
KyongHo Cho2a965362012-05-12 05:56:09 +0900416
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100417 if (MMU_MAJ_VER(data->version) < 5) {
418 reg_status = REG_INT_STATUS;
419 reg_clear = REG_INT_CLEAR;
420 finfo = sysmmu_faults;
421 n = ARRAY_SIZE(sysmmu_faults);
422 } else {
423 reg_status = REG_V5_INT_STATUS;
424 reg_clear = REG_V5_INT_CLEAR;
425 finfo = sysmmu_v5_faults;
426 n = ARRAY_SIZE(sysmmu_v5_faults);
427 }
428
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530429 spin_lock(&data->lock);
430
Marek Szyprowskib398af22016-02-18 15:12:51 +0100431 clk_enable(data->clk_master);
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530432
Marek Szyprowski84bd0422016-02-29 13:42:57 +0100433 itype = __ffs(readl(data->sfrbase + reg_status));
Marek Szyprowskid093fc72016-02-18 15:12:53 +0100434 for (i = 0; i < n; i++, finfo++)
435 if (finfo->bit == itype)
436 break;
437 /* unknown/unsupported fault */
438 BUG_ON(i == n);
KyongHo Cho2a965362012-05-12 05:56:09 +0900439
Marek Szyprowskid093fc72016-02-18 15:12:53 +0100440 /* print debug message */
Marek Szyprowski84bd0422016-02-29 13:42:57 +0100441 fault_addr = readl(data->sfrbase + finfo->addr_reg);
Marek Szyprowskid093fc72016-02-18 15:12:53 +0100442 show_fault_information(data, finfo, fault_addr);
KyongHo Cho2a965362012-05-12 05:56:09 +0900443
Marek Szyprowskid093fc72016-02-18 15:12:53 +0100444 if (data->domain)
445 ret = report_iommu_fault(&data->domain->domain,
446 data->master, fault_addr, finfo->type);
Cho KyongHo1fab7fa2014-05-12 11:44:56 +0530447 /* fault is not recovered by fault handler */
448 BUG_ON(ret != 0);
KyongHo Cho2a965362012-05-12 05:56:09 +0900449
Marek Szyprowski84bd0422016-02-29 13:42:57 +0100450 writel(1 << itype, data->sfrbase + reg_clear);
Cho KyongHo1fab7fa2014-05-12 11:44:56 +0530451
Marek Szyprowski02cdc362016-02-18 15:12:52 +0100452 sysmmu_unblock(data);
KyongHo Cho2a965362012-05-12 05:56:09 +0900453
Marek Szyprowskib398af22016-02-18 15:12:51 +0100454 clk_disable(data->clk_master);
Cho KyongHo70605872014-05-12 11:44:55 +0530455
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530456 spin_unlock(&data->lock);
KyongHo Cho2a965362012-05-12 05:56:09 +0900457
458 return IRQ_HANDLED;
459}
460
Marek Szyprowski47a574f2016-11-14 11:08:08 +0100461static void __sysmmu_disable(struct sysmmu_drvdata *data)
KyongHo Cho2a965362012-05-12 05:56:09 +0900462{
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530463 unsigned long flags;
464
Marek Szyprowski47a574f2016-11-14 11:08:08 +0100465 clk_enable(data->clk_master);
466
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530467 spin_lock_irqsave(&data->lock, flags);
Marek Szyprowski47a574f2016-11-14 11:08:08 +0100468 writel(CTRL_DISABLE, data->sfrbase + REG_MMU_CTRL);
469 writel(0, data->sfrbase + REG_MMU_CFG);
470 data->active = false;
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530471 spin_unlock_irqrestore(&data->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900472
Marek Szyprowski47a574f2016-11-14 11:08:08 +0100473 __sysmmu_disable_clocks(data);
KyongHo Cho2a965362012-05-12 05:56:09 +0900474}
475
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530476static void __sysmmu_init_config(struct sysmmu_drvdata *data)
477{
Marek Szyprowski83addec2016-02-18 15:12:54 +0100478 unsigned int cfg;
Cho KyongHoeeb51842014-05-12 11:45:03 +0530479
Marek Szyprowski83addec2016-02-18 15:12:54 +0100480 if (data->version <= MAKE_MMU_VER(3, 1))
481 cfg = CFG_LRU | CFG_QOS(15);
482 else if (data->version <= MAKE_MMU_VER(3, 2))
483 cfg = CFG_LRU | CFG_QOS(15) | CFG_FLPDCACHE | CFG_SYSSEL;
484 else
485 cfg = CFG_QOS(15) | CFG_FLPDCACHE | CFG_ACGEN;
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530486
Marek Szyprowski1a0d8da2016-11-03 09:04:45 +0100487 cfg |= CFG_EAP; /* enable access protection bits check */
488
Marek Szyprowski84bd0422016-02-29 13:42:57 +0100489 writel(cfg, data->sfrbase + REG_MMU_CFG);
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530490}
491
Marek Szyprowski47a574f2016-11-14 11:08:08 +0100492static void __sysmmu_enable(struct sysmmu_drvdata *data)
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530493{
Marek Szyprowski47a574f2016-11-14 11:08:08 +0100494 unsigned long flags;
495
Marek Szyprowskifecc49d2016-05-23 11:30:09 +0200496 __sysmmu_enable_clocks(data);
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530497
Marek Szyprowski47a574f2016-11-14 11:08:08 +0100498 spin_lock_irqsave(&data->lock, flags);
Marek Szyprowski84bd0422016-02-29 13:42:57 +0100499 writel(CTRL_BLOCK, data->sfrbase + REG_MMU_CTRL);
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530500 __sysmmu_init_config(data);
Marek Szyprowski02cdc362016-02-18 15:12:52 +0100501 __sysmmu_set_ptbase(data, data->pgtable);
Marek Szyprowski84bd0422016-02-29 13:42:57 +0100502 writel(CTRL_ENABLE, data->sfrbase + REG_MMU_CTRL);
Marek Szyprowski47a574f2016-11-14 11:08:08 +0100503 data->active = true;
504 spin_unlock_irqrestore(&data->lock, flags);
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530505
Marek Szyprowskifecc49d2016-05-23 11:30:09 +0200506 /*
507 * SYSMMU driver keeps master's clock enabled only for the short
508 * time, while accessing the registers. For performing address
509 * translation during DMA transaction it relies on the client
510 * driver to enable it.
511 */
Marek Szyprowskib398af22016-02-18 15:12:51 +0100512 clk_disable(data->clk_master);
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530513}
514
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200515static void sysmmu_tlb_invalidate_flpdcache(struct sysmmu_drvdata *data,
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530516 sysmmu_iova_t iova)
517{
518 unsigned long flags;
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530519
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530520 spin_lock_irqsave(&data->lock, flags);
Marek Szyprowski47a574f2016-11-14 11:08:08 +0100521 if (data->active && data->version >= MAKE_MMU_VER(3, 3)) {
Marek Szyprowski01324ab2016-05-23 11:30:08 +0200522 clk_enable(data->clk_master);
Marek Szyprowski7d2aa6b2017-03-20 10:17:56 +0100523 if (sysmmu_block(data)) {
Marek Szyprowskicd37a292017-03-20 10:17:57 +0100524 if (data->version >= MAKE_MMU_VER(5, 0))
525 __sysmmu_tlb_invalidate(data);
526 else
527 __sysmmu_tlb_invalidate_entry(data, iova, 1);
Marek Szyprowski7d2aa6b2017-03-20 10:17:56 +0100528 sysmmu_unblock(data);
529 }
Marek Szyprowski01324ab2016-05-23 11:30:08 +0200530 clk_disable(data->clk_master);
Marek Szyprowskid631ea92016-02-18 15:12:55 +0100531 }
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530532 spin_unlock_irqrestore(&data->lock, flags);
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530533}
534
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200535static void sysmmu_tlb_invalidate_entry(struct sysmmu_drvdata *data,
536 sysmmu_iova_t iova, size_t size)
KyongHo Cho2a965362012-05-12 05:56:09 +0900537{
538 unsigned long flags;
KyongHo Cho2a965362012-05-12 05:56:09 +0900539
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530540 spin_lock_irqsave(&data->lock, flags);
Marek Szyprowski47a574f2016-11-14 11:08:08 +0100541 if (data->active) {
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530542 unsigned int num_inv = 1;
Cho KyongHo70605872014-05-12 11:44:55 +0530543
Marek Szyprowskib398af22016-02-18 15:12:51 +0100544 clk_enable(data->clk_master);
Cho KyongHo70605872014-05-12 11:44:55 +0530545
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530546 /*
547 * L2TLB invalidation required
548 * 4KB page: 1 invalidation
Sachin Kamatf171aba2014-08-04 10:06:28 +0530549 * 64KB page: 16 invalidations
550 * 1MB page: 64 invalidations
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530551 * because it is set-associative TLB
552 * with 8-way and 64 sets.
553 * 1MB page can be cached in one of all sets.
554 * 64KB page can be one of 16 consecutive sets.
555 */
Marek Szyprowski512bd0c2015-05-19 15:20:24 +0200556 if (MMU_MAJ_VER(data->version) == 2)
Cho KyongHo3ad6b7f2014-05-12 11:44:49 +0530557 num_inv = min_t(unsigned int, size / PAGE_SIZE, 64);
558
Marek Szyprowski02cdc362016-02-18 15:12:52 +0100559 if (sysmmu_block(data)) {
560 __sysmmu_tlb_invalidate_entry(data, iova, num_inv);
561 sysmmu_unblock(data);
KyongHo Cho2a965362012-05-12 05:56:09 +0900562 }
Marek Szyprowskib398af22016-02-18 15:12:51 +0100563 clk_disable(data->clk_master);
KyongHo Cho2a965362012-05-12 05:56:09 +0900564 }
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530565 spin_unlock_irqrestore(&data->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900566}
567
Arvind Yadav0b9a3692017-08-28 17:42:05 +0530568static const struct iommu_ops exynos_iommu_ops;
Marek Szyprowski96f66552016-05-23 13:01:27 +0200569
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530570static int __init exynos_sysmmu_probe(struct platform_device *pdev)
KyongHo Cho2a965362012-05-12 05:56:09 +0900571{
Cho KyongHo46c16d12014-05-12 11:44:54 +0530572 int irq, ret;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530573 struct device *dev = &pdev->dev;
KyongHo Cho2a965362012-05-12 05:56:09 +0900574 struct sysmmu_drvdata *data;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530575 struct resource *res;
KyongHo Cho2a965362012-05-12 05:56:09 +0900576
Cho KyongHo46c16d12014-05-12 11:44:54 +0530577 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
578 if (!data)
579 return -ENOMEM;
KyongHo Cho2a965362012-05-12 05:56:09 +0900580
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530581 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Cho KyongHo46c16d12014-05-12 11:44:54 +0530582 data->sfrbase = devm_ioremap_resource(dev, res);
583 if (IS_ERR(data->sfrbase))
584 return PTR_ERR(data->sfrbase);
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530585
Cho KyongHo46c16d12014-05-12 11:44:54 +0530586 irq = platform_get_irq(pdev, 0);
587 if (irq <= 0) {
Cho KyongHo0bf4e542014-05-12 11:45:00 +0530588 dev_err(dev, "Unable to find IRQ resource\n");
Cho KyongHo46c16d12014-05-12 11:44:54 +0530589 return irq;
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530590 }
591
Cho KyongHo46c16d12014-05-12 11:44:54 +0530592 ret = devm_request_irq(dev, irq, exynos_sysmmu_irq, 0,
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530593 dev_name(dev), data);
KyongHo Cho2a965362012-05-12 05:56:09 +0900594 if (ret) {
Cho KyongHo46c16d12014-05-12 11:44:54 +0530595 dev_err(dev, "Unabled to register handler of irq %d\n", irq);
596 return ret;
KyongHo Cho2a965362012-05-12 05:56:09 +0900597 }
598
Cho KyongHo46c16d12014-05-12 11:44:54 +0530599 data->clk = devm_clk_get(dev, "sysmmu");
Marek Szyprowski0c2b0632016-05-23 11:30:07 +0200600 if (PTR_ERR(data->clk) == -ENOENT)
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100601 data->clk = NULL;
Marek Szyprowski0c2b0632016-05-23 11:30:07 +0200602 else if (IS_ERR(data->clk))
603 return PTR_ERR(data->clk);
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100604
605 data->aclk = devm_clk_get(dev, "aclk");
Marek Szyprowski0c2b0632016-05-23 11:30:07 +0200606 if (PTR_ERR(data->aclk) == -ENOENT)
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100607 data->aclk = NULL;
Marek Szyprowski0c2b0632016-05-23 11:30:07 +0200608 else if (IS_ERR(data->aclk))
609 return PTR_ERR(data->aclk);
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100610
611 data->pclk = devm_clk_get(dev, "pclk");
Marek Szyprowski0c2b0632016-05-23 11:30:07 +0200612 if (PTR_ERR(data->pclk) == -ENOENT)
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100613 data->pclk = NULL;
Marek Szyprowski0c2b0632016-05-23 11:30:07 +0200614 else if (IS_ERR(data->pclk))
615 return PTR_ERR(data->pclk);
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100616
617 if (!data->clk && (!data->aclk || !data->pclk)) {
618 dev_err(dev, "Failed to get device clock(s)!\n");
619 return -ENOSYS;
KyongHo Cho2a965362012-05-12 05:56:09 +0900620 }
621
Cho KyongHo70605872014-05-12 11:44:55 +0530622 data->clk_master = devm_clk_get(dev, "master");
Marek Szyprowski0c2b0632016-05-23 11:30:07 +0200623 if (PTR_ERR(data->clk_master) == -ENOENT)
Marek Szyprowskib398af22016-02-18 15:12:51 +0100624 data->clk_master = NULL;
Marek Szyprowski0c2b0632016-05-23 11:30:07 +0200625 else if (IS_ERR(data->clk_master))
626 return PTR_ERR(data->clk_master);
Cho KyongHo70605872014-05-12 11:44:55 +0530627
KyongHo Cho2a965362012-05-12 05:56:09 +0900628 data->sysmmu = dev;
Cho KyongHo9d4e7a22014-05-12 11:44:57 +0530629 spin_lock_init(&data->lock);
KyongHo Cho2a965362012-05-12 05:56:09 +0900630
Joerg Roedeld2c302b2017-02-03 13:23:42 +0100631 ret = iommu_device_sysfs_add(&data->iommu, &pdev->dev, NULL,
632 dev_name(data->sysmmu));
633 if (ret)
634 return ret;
635
636 iommu_device_set_ops(&data->iommu, &exynos_iommu_ops);
637 iommu_device_set_fwnode(&data->iommu, &dev->of_node->fwnode);
638
639 ret = iommu_device_register(&data->iommu);
640 if (ret)
641 return ret;
642
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530643 platform_set_drvdata(pdev, data);
644
Marek Szyprowski850d3132016-02-18 15:12:56 +0100645 __sysmmu_get_version(data);
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100646 if (PG_ENT_SHIFT < 0) {
Marek Szyprowski1a0d8da2016-11-03 09:04:45 +0100647 if (MMU_MAJ_VER(data->version) < 5) {
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100648 PG_ENT_SHIFT = SYSMMU_PG_ENT_SHIFT;
Marek Szyprowski1a0d8da2016-11-03 09:04:45 +0100649 LV1_PROT = SYSMMU_LV1_PROT;
650 LV2_PROT = SYSMMU_LV2_PROT;
651 } else {
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100652 PG_ENT_SHIFT = SYSMMU_V5_PG_ENT_SHIFT;
Marek Szyprowski1a0d8da2016-11-03 09:04:45 +0100653 LV1_PROT = SYSMMU_V5_LV1_PROT;
654 LV2_PROT = SYSMMU_V5_LV2_PROT;
655 }
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100656 }
657
Marek Szyprowski928055a2017-08-04 12:28:33 +0200658 /*
659 * use the first registered sysmmu device for performing
660 * dma mapping operations on iommu page tables (cpu cache flush)
661 */
662 if (!dma_dev)
663 dma_dev = &pdev->dev;
664
Cho KyongHof4723ec2014-05-12 11:44:52 +0530665 pm_runtime_enable(dev);
KyongHo Cho2a965362012-05-12 05:56:09 +0900666
KyongHo Cho2a965362012-05-12 05:56:09 +0900667 return 0;
KyongHo Cho2a965362012-05-12 05:56:09 +0900668}
669
Marek Szyprowski9b265532016-11-14 11:08:11 +0100670static int __maybe_unused exynos_sysmmu_suspend(struct device *dev)
Marek Szyprowski622015e2015-05-19 15:20:35 +0200671{
672 struct sysmmu_drvdata *data = dev_get_drvdata(dev);
Marek Szyprowski47a574f2016-11-14 11:08:08 +0100673 struct device *master = data->master;
Marek Szyprowski622015e2015-05-19 15:20:35 +0200674
Marek Szyprowski47a574f2016-11-14 11:08:08 +0100675 if (master) {
Marek Szyprowski9b265532016-11-14 11:08:11 +0100676 struct exynos_iommu_owner *owner = master->archdata.iommu;
677
678 mutex_lock(&owner->rpm_lock);
Marek Szyprowski92798b42016-11-14 11:08:09 +0100679 if (data->domain) {
680 dev_dbg(data->sysmmu, "saving state\n");
681 __sysmmu_disable(data);
682 }
Marek Szyprowski9b265532016-11-14 11:08:11 +0100683 mutex_unlock(&owner->rpm_lock);
Marek Szyprowski622015e2015-05-19 15:20:35 +0200684 }
685 return 0;
686}
687
Marek Szyprowski9b265532016-11-14 11:08:11 +0100688static int __maybe_unused exynos_sysmmu_resume(struct device *dev)
Marek Szyprowski622015e2015-05-19 15:20:35 +0200689{
690 struct sysmmu_drvdata *data = dev_get_drvdata(dev);
Marek Szyprowski47a574f2016-11-14 11:08:08 +0100691 struct device *master = data->master;
Marek Szyprowski622015e2015-05-19 15:20:35 +0200692
Marek Szyprowski47a574f2016-11-14 11:08:08 +0100693 if (master) {
Marek Szyprowski9b265532016-11-14 11:08:11 +0100694 struct exynos_iommu_owner *owner = master->archdata.iommu;
695
696 mutex_lock(&owner->rpm_lock);
Marek Szyprowski92798b42016-11-14 11:08:09 +0100697 if (data->domain) {
698 dev_dbg(data->sysmmu, "restoring state\n");
699 __sysmmu_enable(data);
700 }
Marek Szyprowski9b265532016-11-14 11:08:11 +0100701 mutex_unlock(&owner->rpm_lock);
Marek Szyprowski622015e2015-05-19 15:20:35 +0200702 }
703 return 0;
704}
Marek Szyprowski622015e2015-05-19 15:20:35 +0200705
706static const struct dev_pm_ops sysmmu_pm_ops = {
Marek Szyprowski9b265532016-11-14 11:08:11 +0100707 SET_RUNTIME_PM_OPS(exynos_sysmmu_suspend, exynos_sysmmu_resume, NULL)
Marek Szyprowski2f5f44f2016-11-14 11:08:12 +0100708 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
709 pm_runtime_force_resume)
Marek Szyprowski622015e2015-05-19 15:20:35 +0200710};
711
Marek Szyprowski9d25e3c2017-10-09 13:40:23 +0200712static const struct of_device_id sysmmu_of_match[] = {
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530713 { .compatible = "samsung,exynos-sysmmu", },
714 { },
715};
716
717static struct platform_driver exynos_sysmmu_driver __refdata = {
718 .probe = exynos_sysmmu_probe,
719 .driver = {
KyongHo Cho2a965362012-05-12 05:56:09 +0900720 .name = "exynos-sysmmu",
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530721 .of_match_table = sysmmu_of_match,
Marek Szyprowski622015e2015-05-19 15:20:35 +0200722 .pm = &sysmmu_pm_ops,
Marek Szyprowskib54b8742016-05-20 15:48:21 +0200723 .suppress_bind_attrs = true,
KyongHo Cho2a965362012-05-12 05:56:09 +0900724 }
725};
726
Marek Szyprowski5e3435e2016-02-18 15:12:50 +0100727static inline void update_pte(sysmmu_pte_t *ent, sysmmu_pte_t val)
KyongHo Cho2a965362012-05-12 05:56:09 +0900728{
Marek Szyprowski5e3435e2016-02-18 15:12:50 +0100729 dma_sync_single_for_cpu(dma_dev, virt_to_phys(ent), sizeof(*ent),
730 DMA_TO_DEVICE);
Ben Dooks6ae53432016-06-08 19:31:10 +0100731 *ent = cpu_to_le32(val);
Marek Szyprowski5e3435e2016-02-18 15:12:50 +0100732 dma_sync_single_for_device(dma_dev, virt_to_phys(ent), sizeof(*ent),
733 DMA_TO_DEVICE);
KyongHo Cho2a965362012-05-12 05:56:09 +0900734}
735
Joerg Roedele1fd1ea2015-03-26 13:43:11 +0100736static struct iommu_domain *exynos_iommu_domain_alloc(unsigned type)
KyongHo Cho2a965362012-05-12 05:56:09 +0900737{
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200738 struct exynos_iommu_domain *domain;
Marek Szyprowski5e3435e2016-02-18 15:12:50 +0100739 dma_addr_t handle;
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530740 int i;
KyongHo Cho2a965362012-05-12 05:56:09 +0900741
Marek Szyprowski740a01e2016-02-18 15:12:58 +0100742 /* Check if correct PTE offsets are initialized */
743 BUG_ON(PG_ENT_SHIFT < 0 || !dma_dev);
KyongHo Cho2a965362012-05-12 05:56:09 +0900744
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200745 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
746 if (!domain)
Joerg Roedele1fd1ea2015-03-26 13:43:11 +0100747 return NULL;
748
Marek Szyprowski58c6f6a2016-02-18 15:12:49 +0100749 if (type == IOMMU_DOMAIN_DMA) {
750 if (iommu_get_dma_cookie(&domain->domain) != 0)
751 goto err_pgtable;
752 } else if (type != IOMMU_DOMAIN_UNMANAGED) {
753 goto err_pgtable;
754 }
755
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200756 domain->pgtable = (sysmmu_pte_t *)__get_free_pages(GFP_KERNEL, 2);
757 if (!domain->pgtable)
Marek Szyprowski58c6f6a2016-02-18 15:12:49 +0100758 goto err_dma_cookie;
KyongHo Cho2a965362012-05-12 05:56:09 +0900759
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200760 domain->lv2entcnt = (short *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1);
761 if (!domain->lv2entcnt)
KyongHo Cho2a965362012-05-12 05:56:09 +0900762 goto err_counter;
763
Sachin Kamatf171aba2014-08-04 10:06:28 +0530764 /* Workaround for System MMU v3.3 to prevent caching 1MiB mapping */
Marek Szyprowskie7527662017-03-24 10:18:44 +0100765 for (i = 0; i < NUM_LV1ENTRIES; i++)
766 domain->pgtable[i] = ZERO_LV2LINK;
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530767
Marek Szyprowski5e3435e2016-02-18 15:12:50 +0100768 handle = dma_map_single(dma_dev, domain->pgtable, LV1TABLE_SIZE,
769 DMA_TO_DEVICE);
770 /* For mapping page table entries we rely on dma == phys */
771 BUG_ON(handle != virt_to_phys(domain->pgtable));
Marek Szyprowski0d6d3da2017-01-09 13:03:54 +0100772 if (dma_mapping_error(dma_dev, handle))
773 goto err_lv2ent;
KyongHo Cho2a965362012-05-12 05:56:09 +0900774
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200775 spin_lock_init(&domain->lock);
776 spin_lock_init(&domain->pgtablelock);
777 INIT_LIST_HEAD(&domain->clients);
KyongHo Cho2a965362012-05-12 05:56:09 +0900778
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200779 domain->domain.geometry.aperture_start = 0;
780 domain->domain.geometry.aperture_end = ~0UL;
781 domain->domain.geometry.force_aperture = true;
Joerg Roedel3177bb72012-07-11 12:41:10 +0200782
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200783 return &domain->domain;
KyongHo Cho2a965362012-05-12 05:56:09 +0900784
Marek Szyprowski0d6d3da2017-01-09 13:03:54 +0100785err_lv2ent:
786 free_pages((unsigned long)domain->lv2entcnt, 1);
KyongHo Cho2a965362012-05-12 05:56:09 +0900787err_counter:
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200788 free_pages((unsigned long)domain->pgtable, 2);
Marek Szyprowski58c6f6a2016-02-18 15:12:49 +0100789err_dma_cookie:
790 if (type == IOMMU_DOMAIN_DMA)
791 iommu_put_dma_cookie(&domain->domain);
KyongHo Cho2a965362012-05-12 05:56:09 +0900792err_pgtable:
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200793 kfree(domain);
Joerg Roedele1fd1ea2015-03-26 13:43:11 +0100794 return NULL;
KyongHo Cho2a965362012-05-12 05:56:09 +0900795}
796
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200797static void exynos_iommu_domain_free(struct iommu_domain *iommu_domain)
KyongHo Cho2a965362012-05-12 05:56:09 +0900798{
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200799 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200800 struct sysmmu_drvdata *data, *next;
KyongHo Cho2a965362012-05-12 05:56:09 +0900801 unsigned long flags;
802 int i;
803
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200804 WARN_ON(!list_empty(&domain->clients));
KyongHo Cho2a965362012-05-12 05:56:09 +0900805
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200806 spin_lock_irqsave(&domain->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900807
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200808 list_for_each_entry_safe(data, next, &domain->clients, domain_node) {
Marek Szyprowskie1172302016-11-14 11:08:10 +0100809 spin_lock(&data->lock);
Marek Szyprowskib0d4c862016-11-14 11:08:07 +0100810 __sysmmu_disable(data);
Marek Szyprowski47a574f2016-11-14 11:08:08 +0100811 data->pgtable = 0;
812 data->domain = NULL;
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200813 list_del_init(&data->domain_node);
Marek Szyprowskie1172302016-11-14 11:08:10 +0100814 spin_unlock(&data->lock);
KyongHo Cho2a965362012-05-12 05:56:09 +0900815 }
816
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200817 spin_unlock_irqrestore(&domain->lock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +0900818
Marek Szyprowski58c6f6a2016-02-18 15:12:49 +0100819 if (iommu_domain->type == IOMMU_DOMAIN_DMA)
820 iommu_put_dma_cookie(iommu_domain);
821
Marek Szyprowski5e3435e2016-02-18 15:12:50 +0100822 dma_unmap_single(dma_dev, virt_to_phys(domain->pgtable), LV1TABLE_SIZE,
823 DMA_TO_DEVICE);
824
KyongHo Cho2a965362012-05-12 05:56:09 +0900825 for (i = 0; i < NUM_LV1ENTRIES; i++)
Marek Szyprowski5e3435e2016-02-18 15:12:50 +0100826 if (lv1ent_page(domain->pgtable + i)) {
827 phys_addr_t base = lv2table_base(domain->pgtable + i);
828
829 dma_unmap_single(dma_dev, base, LV2TABLE_SIZE,
830 DMA_TO_DEVICE);
Cho KyongHo734c3c72014-05-12 11:44:48 +0530831 kmem_cache_free(lv2table_kmem_cache,
Marek Szyprowski5e3435e2016-02-18 15:12:50 +0100832 phys_to_virt(base));
833 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900834
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200835 free_pages((unsigned long)domain->pgtable, 2);
836 free_pages((unsigned long)domain->lv2entcnt, 1);
837 kfree(domain);
KyongHo Cho2a965362012-05-12 05:56:09 +0900838}
839
Marek Szyprowski5fa61cb2016-02-18 15:13:00 +0100840static void exynos_iommu_detach_device(struct iommu_domain *iommu_domain,
841 struct device *dev)
842{
843 struct exynos_iommu_owner *owner = dev->archdata.iommu;
844 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
845 phys_addr_t pagetable = virt_to_phys(domain->pgtable);
846 struct sysmmu_drvdata *data, *next;
847 unsigned long flags;
Marek Szyprowski5fa61cb2016-02-18 15:13:00 +0100848
849 if (!has_sysmmu(dev) || owner->domain != iommu_domain)
850 return;
851
Marek Szyprowski9b265532016-11-14 11:08:11 +0100852 mutex_lock(&owner->rpm_lock);
853
854 list_for_each_entry(data, &owner->controllers, owner_node) {
855 pm_runtime_get_noresume(data->sysmmu);
856 if (pm_runtime_active(data->sysmmu))
857 __sysmmu_disable(data);
Marek Szyprowskie1172302016-11-14 11:08:10 +0100858 pm_runtime_put(data->sysmmu);
859 }
860
Marek Szyprowski5fa61cb2016-02-18 15:13:00 +0100861 spin_lock_irqsave(&domain->lock, flags);
862 list_for_each_entry_safe(data, next, &domain->clients, domain_node) {
Marek Szyprowskie1172302016-11-14 11:08:10 +0100863 spin_lock(&data->lock);
Marek Szyprowski47a574f2016-11-14 11:08:08 +0100864 data->pgtable = 0;
865 data->domain = NULL;
Marek Szyprowskib0d4c862016-11-14 11:08:07 +0100866 list_del_init(&data->domain_node);
Marek Szyprowskie1172302016-11-14 11:08:10 +0100867 spin_unlock(&data->lock);
Marek Szyprowski5fa61cb2016-02-18 15:13:00 +0100868 }
Marek Szyprowskie1172302016-11-14 11:08:10 +0100869 owner->domain = NULL;
Marek Szyprowski5fa61cb2016-02-18 15:13:00 +0100870 spin_unlock_irqrestore(&domain->lock, flags);
871
Marek Szyprowski9b265532016-11-14 11:08:11 +0100872 mutex_unlock(&owner->rpm_lock);
Marek Szyprowski5fa61cb2016-02-18 15:13:00 +0100873
Marek Szyprowskib0d4c862016-11-14 11:08:07 +0100874 dev_dbg(dev, "%s: Detached IOMMU with pgtable %pa\n", __func__,
875 &pagetable);
Marek Szyprowski5fa61cb2016-02-18 15:13:00 +0100876}
877
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200878static int exynos_iommu_attach_device(struct iommu_domain *iommu_domain,
KyongHo Cho2a965362012-05-12 05:56:09 +0900879 struct device *dev)
880{
Cho KyongHo6b21a5d2014-05-12 11:45:02 +0530881 struct exynos_iommu_owner *owner = dev->archdata.iommu;
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200882 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200883 struct sysmmu_drvdata *data;
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200884 phys_addr_t pagetable = virt_to_phys(domain->pgtable);
KyongHo Cho2a965362012-05-12 05:56:09 +0900885 unsigned long flags;
KyongHo Cho2a965362012-05-12 05:56:09 +0900886
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200887 if (!has_sysmmu(dev))
888 return -ENODEV;
KyongHo Cho2a965362012-05-12 05:56:09 +0900889
Marek Szyprowski5fa61cb2016-02-18 15:13:00 +0100890 if (owner->domain)
891 exynos_iommu_detach_device(owner->domain, dev);
892
Marek Szyprowski9b265532016-11-14 11:08:11 +0100893 mutex_lock(&owner->rpm_lock);
894
Marek Szyprowskie1172302016-11-14 11:08:10 +0100895 spin_lock_irqsave(&domain->lock, flags);
Marek Szyprowski1b092052015-05-19 15:20:33 +0200896 list_for_each_entry(data, &owner->controllers, owner_node) {
Marek Szyprowskie1172302016-11-14 11:08:10 +0100897 spin_lock(&data->lock);
Marek Szyprowski47a574f2016-11-14 11:08:08 +0100898 data->pgtable = pagetable;
899 data->domain = domain;
Marek Szyprowskie1172302016-11-14 11:08:10 +0100900 list_add_tail(&data->domain_node, &domain->clients);
901 spin_unlock(&data->lock);
902 }
903 owner->domain = iommu_domain;
904 spin_unlock_irqrestore(&domain->lock, flags);
905
906 list_for_each_entry(data, &owner->controllers, owner_node) {
Marek Szyprowski9b265532016-11-14 11:08:11 +0100907 pm_runtime_get_noresume(data->sysmmu);
908 if (pm_runtime_active(data->sysmmu))
909 __sysmmu_enable(data);
910 pm_runtime_put(data->sysmmu);
911 }
912
913 mutex_unlock(&owner->rpm_lock);
914
Marek Szyprowskib0d4c862016-11-14 11:08:07 +0100915 dev_dbg(dev, "%s: Attached IOMMU with pgtable %pa\n", __func__,
916 &pagetable);
Cho KyongHo7222e8d2014-05-12 11:44:46 +0530917
Marek Szyprowskib0d4c862016-11-14 11:08:07 +0100918 return 0;
KyongHo Cho2a965362012-05-12 05:56:09 +0900919}
920
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200921static sysmmu_pte_t *alloc_lv2entry(struct exynos_iommu_domain *domain,
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530922 sysmmu_pte_t *sent, sysmmu_iova_t iova, short *pgcounter)
KyongHo Cho2a965362012-05-12 05:56:09 +0900923{
Cho KyongHo61128f02014-05-12 11:44:47 +0530924 if (lv1ent_section(sent)) {
Cho KyongHod09d78f2014-05-12 11:44:58 +0530925 WARN(1, "Trying mapping on %#08x mapped with 1MiB page", iova);
Cho KyongHo61128f02014-05-12 11:44:47 +0530926 return ERR_PTR(-EADDRINUSE);
927 }
928
KyongHo Cho2a965362012-05-12 05:56:09 +0900929 if (lv1ent_fault(sent)) {
Marek Szyprowski0d6d3da2017-01-09 13:03:54 +0100930 dma_addr_t handle;
Cho KyongHod09d78f2014-05-12 11:44:58 +0530931 sysmmu_pte_t *pent;
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530932 bool need_flush_flpd_cache = lv1ent_zero(sent);
KyongHo Cho2a965362012-05-12 05:56:09 +0900933
Cho KyongHo734c3c72014-05-12 11:44:48 +0530934 pent = kmem_cache_zalloc(lv2table_kmem_cache, GFP_ATOMIC);
Arnd Bergmanndbf6c6e2016-02-29 09:45:59 +0100935 BUG_ON((uintptr_t)pent & (LV2TABLE_SIZE - 1));
KyongHo Cho2a965362012-05-12 05:56:09 +0900936 if (!pent)
Cho KyongHo61128f02014-05-12 11:44:47 +0530937 return ERR_PTR(-ENOMEM);
KyongHo Cho2a965362012-05-12 05:56:09 +0900938
Marek Szyprowski5e3435e2016-02-18 15:12:50 +0100939 update_pte(sent, mk_lv1ent_page(virt_to_phys(pent)));
Colin Crossdc3814f2015-05-08 17:05:44 -0700940 kmemleak_ignore(pent);
KyongHo Cho2a965362012-05-12 05:56:09 +0900941 *pgcounter = NUM_LV2ENTRIES;
Marek Szyprowski0d6d3da2017-01-09 13:03:54 +0100942 handle = dma_map_single(dma_dev, pent, LV2TABLE_SIZE,
943 DMA_TO_DEVICE);
944 if (dma_mapping_error(dma_dev, handle)) {
945 kmem_cache_free(lv2table_kmem_cache, pent);
946 return ERR_PTR(-EADDRINUSE);
947 }
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530948
949 /*
Sachin Kamatf171aba2014-08-04 10:06:28 +0530950 * If pre-fetched SLPD is a faulty SLPD in zero_l2_table,
951 * FLPD cache may cache the address of zero_l2_table. This
952 * function replaces the zero_l2_table with new L2 page table
953 * to write valid mappings.
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530954 * Accessing the valid area may cause page fault since FLPD
Sachin Kamatf171aba2014-08-04 10:06:28 +0530955 * cache may still cache zero_l2_table for the valid area
956 * instead of new L2 page table that has the mapping
957 * information of the valid area.
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530958 * Thus any replacement of zero_l2_table with other valid L2
959 * page table must involve FLPD cache invalidation for System
960 * MMU v3.3.
961 * FLPD cache invalidation is performed with TLB invalidation
962 * by VPN without blocking. It is safe to invalidate TLB without
963 * blocking because the target address of TLB invalidation is
964 * not currently mapped.
965 */
966 if (need_flush_flpd_cache) {
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200967 struct sysmmu_drvdata *data;
Sachin Kamat365409d2014-05-22 09:50:56 +0530968
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200969 spin_lock(&domain->lock);
970 list_for_each_entry(data, &domain->clients, domain_node)
Marek Szyprowski469aceb2015-05-19 15:20:27 +0200971 sysmmu_tlb_invalidate_flpdcache(data, iova);
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200972 spin_unlock(&domain->lock);
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530973 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900974 }
975
976 return page_entry(sent, iova);
977}
978
Marek Szyprowskibfa00482015-05-19 15:20:28 +0200979static int lv1set_section(struct exynos_iommu_domain *domain,
Cho KyongHo66a7ed82014-05-12 11:45:04 +0530980 sysmmu_pte_t *sent, sysmmu_iova_t iova,
Marek Szyprowski1a0d8da2016-11-03 09:04:45 +0100981 phys_addr_t paddr, int prot, short *pgcnt)
KyongHo Cho2a965362012-05-12 05:56:09 +0900982{
Cho KyongHo61128f02014-05-12 11:44:47 +0530983 if (lv1ent_section(sent)) {
Cho KyongHod09d78f2014-05-12 11:44:58 +0530984 WARN(1, "Trying mapping on 1MiB@%#08x that is mapped",
Cho KyongHo61128f02014-05-12 11:44:47 +0530985 iova);
KyongHo Cho2a965362012-05-12 05:56:09 +0900986 return -EADDRINUSE;
Cho KyongHo61128f02014-05-12 11:44:47 +0530987 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900988
989 if (lv1ent_page(sent)) {
Cho KyongHo61128f02014-05-12 11:44:47 +0530990 if (*pgcnt != NUM_LV2ENTRIES) {
Cho KyongHod09d78f2014-05-12 11:44:58 +0530991 WARN(1, "Trying mapping on 1MiB@%#08x that is mapped",
Cho KyongHo61128f02014-05-12 11:44:47 +0530992 iova);
KyongHo Cho2a965362012-05-12 05:56:09 +0900993 return -EADDRINUSE;
Cho KyongHo61128f02014-05-12 11:44:47 +0530994 }
KyongHo Cho2a965362012-05-12 05:56:09 +0900995
Cho KyongHo734c3c72014-05-12 11:44:48 +0530996 kmem_cache_free(lv2table_kmem_cache, page_entry(sent, 0));
KyongHo Cho2a965362012-05-12 05:56:09 +0900997 *pgcnt = 0;
998 }
999
Marek Szyprowski1a0d8da2016-11-03 09:04:45 +01001000 update_pte(sent, mk_lv1ent_sect(paddr, prot));
KyongHo Cho2a965362012-05-12 05:56:09 +09001001
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001002 spin_lock(&domain->lock);
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301003 if (lv1ent_page_zero(sent)) {
Marek Szyprowski469aceb2015-05-19 15:20:27 +02001004 struct sysmmu_drvdata *data;
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301005 /*
1006 * Flushing FLPD cache in System MMU v3.3 that may cache a FLPD
1007 * entry by speculative prefetch of SLPD which has no mapping.
1008 */
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001009 list_for_each_entry(data, &domain->clients, domain_node)
Marek Szyprowski469aceb2015-05-19 15:20:27 +02001010 sysmmu_tlb_invalidate_flpdcache(data, iova);
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301011 }
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001012 spin_unlock(&domain->lock);
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301013
KyongHo Cho2a965362012-05-12 05:56:09 +09001014 return 0;
1015}
1016
Cho KyongHod09d78f2014-05-12 11:44:58 +05301017static int lv2set_page(sysmmu_pte_t *pent, phys_addr_t paddr, size_t size,
Marek Szyprowski1a0d8da2016-11-03 09:04:45 +01001018 int prot, short *pgcnt)
KyongHo Cho2a965362012-05-12 05:56:09 +09001019{
1020 if (size == SPAGE_SIZE) {
Cho KyongHo0bf4e542014-05-12 11:45:00 +05301021 if (WARN_ON(!lv2ent_fault(pent)))
KyongHo Cho2a965362012-05-12 05:56:09 +09001022 return -EADDRINUSE;
1023
Marek Szyprowski1a0d8da2016-11-03 09:04:45 +01001024 update_pte(pent, mk_lv2ent_spage(paddr, prot));
KyongHo Cho2a965362012-05-12 05:56:09 +09001025 *pgcnt -= 1;
1026 } else { /* size == LPAGE_SIZE */
1027 int i;
Marek Szyprowski5e3435e2016-02-18 15:12:50 +01001028 dma_addr_t pent_base = virt_to_phys(pent);
Sachin Kamat365409d2014-05-22 09:50:56 +05301029
Marek Szyprowski5e3435e2016-02-18 15:12:50 +01001030 dma_sync_single_for_cpu(dma_dev, pent_base,
1031 sizeof(*pent) * SPAGES_PER_LPAGE,
1032 DMA_TO_DEVICE);
KyongHo Cho2a965362012-05-12 05:56:09 +09001033 for (i = 0; i < SPAGES_PER_LPAGE; i++, pent++) {
Cho KyongHo0bf4e542014-05-12 11:45:00 +05301034 if (WARN_ON(!lv2ent_fault(pent))) {
Cho KyongHo61128f02014-05-12 11:44:47 +05301035 if (i > 0)
1036 memset(pent - i, 0, sizeof(*pent) * i);
KyongHo Cho2a965362012-05-12 05:56:09 +09001037 return -EADDRINUSE;
1038 }
1039
Marek Szyprowski1a0d8da2016-11-03 09:04:45 +01001040 *pent = mk_lv2ent_lpage(paddr, prot);
KyongHo Cho2a965362012-05-12 05:56:09 +09001041 }
Marek Szyprowski5e3435e2016-02-18 15:12:50 +01001042 dma_sync_single_for_device(dma_dev, pent_base,
1043 sizeof(*pent) * SPAGES_PER_LPAGE,
1044 DMA_TO_DEVICE);
KyongHo Cho2a965362012-05-12 05:56:09 +09001045 *pgcnt -= SPAGES_PER_LPAGE;
1046 }
1047
1048 return 0;
1049}
1050
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301051/*
1052 * *CAUTION* to the I/O virtual memory managers that support exynos-iommu:
1053 *
Sachin Kamatf171aba2014-08-04 10:06:28 +05301054 * System MMU v3.x has advanced logic to improve address translation
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301055 * performance with caching more page table entries by a page table walk.
Sachin Kamatf171aba2014-08-04 10:06:28 +05301056 * However, the logic has a bug that while caching faulty page table entries,
1057 * System MMU reports page fault if the cached fault entry is hit even though
1058 * the fault entry is updated to a valid entry after the entry is cached.
1059 * To prevent caching faulty page table entries which may be updated to valid
1060 * entries later, the virtual memory manager should care about the workaround
1061 * for the problem. The following describes the workaround.
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301062 *
1063 * Any two consecutive I/O virtual address regions must have a hole of 128KiB
Sachin Kamatf171aba2014-08-04 10:06:28 +05301064 * at maximum to prevent misbehavior of System MMU 3.x (workaround for h/w bug).
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301065 *
Sachin Kamatf171aba2014-08-04 10:06:28 +05301066 * Precisely, any start address of I/O virtual region must be aligned with
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301067 * the following sizes for System MMU v3.1 and v3.2.
1068 * System MMU v3.1: 128KiB
1069 * System MMU v3.2: 256KiB
1070 *
1071 * Because System MMU v3.3 caches page table entries more aggressively, it needs
Sachin Kamatf171aba2014-08-04 10:06:28 +05301072 * more workarounds.
1073 * - Any two consecutive I/O virtual regions must have a hole of size larger
1074 * than or equal to 128KiB.
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301075 * - Start address of an I/O virtual region must be aligned by 128KiB.
1076 */
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001077static int exynos_iommu_map(struct iommu_domain *iommu_domain,
1078 unsigned long l_iova, phys_addr_t paddr, size_t size,
1079 int prot)
KyongHo Cho2a965362012-05-12 05:56:09 +09001080{
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001081 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
Cho KyongHod09d78f2014-05-12 11:44:58 +05301082 sysmmu_pte_t *entry;
1083 sysmmu_iova_t iova = (sysmmu_iova_t)l_iova;
KyongHo Cho2a965362012-05-12 05:56:09 +09001084 unsigned long flags;
1085 int ret = -ENOMEM;
1086
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001087 BUG_ON(domain->pgtable == NULL);
Marek Szyprowski1a0d8da2016-11-03 09:04:45 +01001088 prot &= SYSMMU_SUPPORTED_PROT_BITS;
KyongHo Cho2a965362012-05-12 05:56:09 +09001089
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001090 spin_lock_irqsave(&domain->pgtablelock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +09001091
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001092 entry = section_entry(domain->pgtable, iova);
KyongHo Cho2a965362012-05-12 05:56:09 +09001093
1094 if (size == SECT_SIZE) {
Marek Szyprowski1a0d8da2016-11-03 09:04:45 +01001095 ret = lv1set_section(domain, entry, iova, paddr, prot,
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001096 &domain->lv2entcnt[lv1ent_offset(iova)]);
KyongHo Cho2a965362012-05-12 05:56:09 +09001097 } else {
Cho KyongHod09d78f2014-05-12 11:44:58 +05301098 sysmmu_pte_t *pent;
KyongHo Cho2a965362012-05-12 05:56:09 +09001099
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001100 pent = alloc_lv2entry(domain, entry, iova,
1101 &domain->lv2entcnt[lv1ent_offset(iova)]);
KyongHo Cho2a965362012-05-12 05:56:09 +09001102
Cho KyongHo61128f02014-05-12 11:44:47 +05301103 if (IS_ERR(pent))
1104 ret = PTR_ERR(pent);
KyongHo Cho2a965362012-05-12 05:56:09 +09001105 else
Marek Szyprowski1a0d8da2016-11-03 09:04:45 +01001106 ret = lv2set_page(pent, paddr, size, prot,
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001107 &domain->lv2entcnt[lv1ent_offset(iova)]);
KyongHo Cho2a965362012-05-12 05:56:09 +09001108 }
1109
Cho KyongHo61128f02014-05-12 11:44:47 +05301110 if (ret)
Cho KyongHo0bf4e542014-05-12 11:45:00 +05301111 pr_err("%s: Failed(%d) to map %#zx bytes @ %#x\n",
1112 __func__, ret, size, iova);
KyongHo Cho2a965362012-05-12 05:56:09 +09001113
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001114 spin_unlock_irqrestore(&domain->pgtablelock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +09001115
1116 return ret;
1117}
1118
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001119static void exynos_iommu_tlb_invalidate_entry(struct exynos_iommu_domain *domain,
1120 sysmmu_iova_t iova, size_t size)
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301121{
Marek Szyprowski469aceb2015-05-19 15:20:27 +02001122 struct sysmmu_drvdata *data;
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301123 unsigned long flags;
1124
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001125 spin_lock_irqsave(&domain->lock, flags);
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301126
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001127 list_for_each_entry(data, &domain->clients, domain_node)
Marek Szyprowski469aceb2015-05-19 15:20:27 +02001128 sysmmu_tlb_invalidate_entry(data, iova, size);
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301129
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001130 spin_unlock_irqrestore(&domain->lock, flags);
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301131}
1132
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001133static size_t exynos_iommu_unmap(struct iommu_domain *iommu_domain,
1134 unsigned long l_iova, size_t size)
KyongHo Cho2a965362012-05-12 05:56:09 +09001135{
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001136 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
Cho KyongHod09d78f2014-05-12 11:44:58 +05301137 sysmmu_iova_t iova = (sysmmu_iova_t)l_iova;
1138 sysmmu_pte_t *ent;
Cho KyongHo61128f02014-05-12 11:44:47 +05301139 size_t err_pgsize;
Cho KyongHod09d78f2014-05-12 11:44:58 +05301140 unsigned long flags;
KyongHo Cho2a965362012-05-12 05:56:09 +09001141
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001142 BUG_ON(domain->pgtable == NULL);
KyongHo Cho2a965362012-05-12 05:56:09 +09001143
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001144 spin_lock_irqsave(&domain->pgtablelock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +09001145
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001146 ent = section_entry(domain->pgtable, iova);
KyongHo Cho2a965362012-05-12 05:56:09 +09001147
1148 if (lv1ent_section(ent)) {
Cho KyongHo0bf4e542014-05-12 11:45:00 +05301149 if (WARN_ON(size < SECT_SIZE)) {
Cho KyongHo61128f02014-05-12 11:44:47 +05301150 err_pgsize = SECT_SIZE;
1151 goto err;
1152 }
KyongHo Cho2a965362012-05-12 05:56:09 +09001153
Sachin Kamatf171aba2014-08-04 10:06:28 +05301154 /* workaround for h/w bug in System MMU v3.3 */
Marek Szyprowski5e3435e2016-02-18 15:12:50 +01001155 update_pte(ent, ZERO_LV2LINK);
KyongHo Cho2a965362012-05-12 05:56:09 +09001156 size = SECT_SIZE;
1157 goto done;
1158 }
1159
1160 if (unlikely(lv1ent_fault(ent))) {
1161 if (size > SECT_SIZE)
1162 size = SECT_SIZE;
1163 goto done;
1164 }
1165
1166 /* lv1ent_page(sent) == true here */
1167
1168 ent = page_entry(ent, iova);
1169
1170 if (unlikely(lv2ent_fault(ent))) {
1171 size = SPAGE_SIZE;
1172 goto done;
1173 }
1174
1175 if (lv2ent_small(ent)) {
Marek Szyprowski5e3435e2016-02-18 15:12:50 +01001176 update_pte(ent, 0);
KyongHo Cho2a965362012-05-12 05:56:09 +09001177 size = SPAGE_SIZE;
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001178 domain->lv2entcnt[lv1ent_offset(iova)] += 1;
KyongHo Cho2a965362012-05-12 05:56:09 +09001179 goto done;
1180 }
1181
1182 /* lv1ent_large(ent) == true here */
Cho KyongHo0bf4e542014-05-12 11:45:00 +05301183 if (WARN_ON(size < LPAGE_SIZE)) {
Cho KyongHo61128f02014-05-12 11:44:47 +05301184 err_pgsize = LPAGE_SIZE;
1185 goto err;
1186 }
KyongHo Cho2a965362012-05-12 05:56:09 +09001187
Marek Szyprowski5e3435e2016-02-18 15:12:50 +01001188 dma_sync_single_for_cpu(dma_dev, virt_to_phys(ent),
1189 sizeof(*ent) * SPAGES_PER_LPAGE,
1190 DMA_TO_DEVICE);
KyongHo Cho2a965362012-05-12 05:56:09 +09001191 memset(ent, 0, sizeof(*ent) * SPAGES_PER_LPAGE);
Marek Szyprowski5e3435e2016-02-18 15:12:50 +01001192 dma_sync_single_for_device(dma_dev, virt_to_phys(ent),
1193 sizeof(*ent) * SPAGES_PER_LPAGE,
1194 DMA_TO_DEVICE);
KyongHo Cho2a965362012-05-12 05:56:09 +09001195 size = LPAGE_SIZE;
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001196 domain->lv2entcnt[lv1ent_offset(iova)] += SPAGES_PER_LPAGE;
KyongHo Cho2a965362012-05-12 05:56:09 +09001197done:
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001198 spin_unlock_irqrestore(&domain->pgtablelock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +09001199
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001200 exynos_iommu_tlb_invalidate_entry(domain, iova, size);
KyongHo Cho2a965362012-05-12 05:56:09 +09001201
KyongHo Cho2a965362012-05-12 05:56:09 +09001202 return size;
Cho KyongHo61128f02014-05-12 11:44:47 +05301203err:
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001204 spin_unlock_irqrestore(&domain->pgtablelock, flags);
Cho KyongHo61128f02014-05-12 11:44:47 +05301205
Cho KyongHo0bf4e542014-05-12 11:45:00 +05301206 pr_err("%s: Failed: size(%#zx) @ %#x is smaller than page size %#zx\n",
1207 __func__, size, iova, err_pgsize);
Cho KyongHo61128f02014-05-12 11:44:47 +05301208
1209 return 0;
KyongHo Cho2a965362012-05-12 05:56:09 +09001210}
1211
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001212static phys_addr_t exynos_iommu_iova_to_phys(struct iommu_domain *iommu_domain,
Varun Sethibb5547a2013-03-29 01:23:58 +05301213 dma_addr_t iova)
KyongHo Cho2a965362012-05-12 05:56:09 +09001214{
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001215 struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
Cho KyongHod09d78f2014-05-12 11:44:58 +05301216 sysmmu_pte_t *entry;
KyongHo Cho2a965362012-05-12 05:56:09 +09001217 unsigned long flags;
1218 phys_addr_t phys = 0;
1219
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001220 spin_lock_irqsave(&domain->pgtablelock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +09001221
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001222 entry = section_entry(domain->pgtable, iova);
KyongHo Cho2a965362012-05-12 05:56:09 +09001223
1224 if (lv1ent_section(entry)) {
1225 phys = section_phys(entry) + section_offs(iova);
1226 } else if (lv1ent_page(entry)) {
1227 entry = page_entry(entry, iova);
1228
1229 if (lv2ent_large(entry))
1230 phys = lpage_phys(entry) + lpage_offs(iova);
1231 else if (lv2ent_small(entry))
1232 phys = spage_phys(entry) + spage_offs(iova);
1233 }
1234
Marek Szyprowskibfa00482015-05-19 15:20:28 +02001235 spin_unlock_irqrestore(&domain->pgtablelock, flags);
KyongHo Cho2a965362012-05-12 05:56:09 +09001236
1237 return phys;
1238}
1239
Marek Szyprowski6c2ae7e2016-02-18 15:12:48 +01001240static struct iommu_group *get_device_iommu_group(struct device *dev)
1241{
1242 struct iommu_group *group;
1243
1244 group = iommu_group_get(dev);
1245 if (!group)
1246 group = iommu_group_alloc();
1247
1248 return group;
1249}
1250
Antonios Motakisbf4a1c92014-05-12 11:44:59 +05301251static int exynos_iommu_add_device(struct device *dev)
1252{
1253 struct iommu_group *group;
Antonios Motakisbf4a1c92014-05-12 11:44:59 +05301254
Marek Szyprowski06801db2015-05-19 15:20:32 +02001255 if (!has_sysmmu(dev))
1256 return -ENODEV;
1257
Marek Szyprowski6c2ae7e2016-02-18 15:12:48 +01001258 group = iommu_group_get_for_dev(dev);
Antonios Motakisbf4a1c92014-05-12 11:44:59 +05301259
Marek Szyprowski6c2ae7e2016-02-18 15:12:48 +01001260 if (IS_ERR(group))
1261 return PTR_ERR(group);
Antonios Motakisbf4a1c92014-05-12 11:44:59 +05301262
Antonios Motakisbf4a1c92014-05-12 11:44:59 +05301263 iommu_group_put(group);
1264
Marek Szyprowski6c2ae7e2016-02-18 15:12:48 +01001265 return 0;
Antonios Motakisbf4a1c92014-05-12 11:44:59 +05301266}
1267
1268static void exynos_iommu_remove_device(struct device *dev)
1269{
Marek Szyprowskifff2fd12017-01-09 13:03:56 +01001270 struct exynos_iommu_owner *owner = dev->archdata.iommu;
1271
Marek Szyprowski06801db2015-05-19 15:20:32 +02001272 if (!has_sysmmu(dev))
1273 return;
1274
Marek Szyprowskifff2fd12017-01-09 13:03:56 +01001275 if (owner->domain) {
1276 struct iommu_group *group = iommu_group_get(dev);
1277
1278 if (group) {
1279 WARN_ON(owner->domain !=
1280 iommu_group_default_domain(group));
1281 exynos_iommu_detach_device(owner->domain, dev);
1282 iommu_group_put(group);
1283 }
1284 }
Antonios Motakisbf4a1c92014-05-12 11:44:59 +05301285 iommu_group_remove_device(dev);
1286}
1287
Marek Szyprowskiaa759fd2015-05-19 15:20:37 +02001288static int exynos_iommu_of_xlate(struct device *dev,
1289 struct of_phandle_args *spec)
1290{
1291 struct exynos_iommu_owner *owner = dev->archdata.iommu;
1292 struct platform_device *sysmmu = of_find_device_by_node(spec->np);
Marek Szyprowski0bd5a0c2017-01-09 13:03:55 +01001293 struct sysmmu_drvdata *data, *entry;
Marek Szyprowskiaa759fd2015-05-19 15:20:37 +02001294
1295 if (!sysmmu)
1296 return -ENODEV;
1297
1298 data = platform_get_drvdata(sysmmu);
1299 if (!data)
1300 return -ENODEV;
1301
1302 if (!owner) {
1303 owner = kzalloc(sizeof(*owner), GFP_KERNEL);
1304 if (!owner)
1305 return -ENOMEM;
1306
1307 INIT_LIST_HEAD(&owner->controllers);
Marek Szyprowski9b265532016-11-14 11:08:11 +01001308 mutex_init(&owner->rpm_lock);
Marek Szyprowskiaa759fd2015-05-19 15:20:37 +02001309 dev->archdata.iommu = owner;
1310 }
1311
Marek Szyprowski0bd5a0c2017-01-09 13:03:55 +01001312 list_for_each_entry(entry, &owner->controllers, owner_node)
1313 if (entry == data)
1314 return 0;
1315
Marek Szyprowskiaa759fd2015-05-19 15:20:37 +02001316 list_add_tail(&data->owner_node, &owner->controllers);
Marek Szyprowski92798b42016-11-14 11:08:09 +01001317 data->master = dev;
Marek Szyprowski2f5f44f2016-11-14 11:08:12 +01001318
1319 /*
1320 * SYSMMU will be runtime activated via device link (dependency) to its
1321 * master device, so there are no direct calls to pm_runtime_get/put
1322 * in this driver.
1323 */
1324 device_link_add(dev, data->sysmmu, DL_FLAG_PM_RUNTIME);
1325
Marek Szyprowskiaa759fd2015-05-19 15:20:37 +02001326 return 0;
1327}
1328
Arvind Yadav0b9a3692017-08-28 17:42:05 +05301329static const struct iommu_ops exynos_iommu_ops = {
Joerg Roedele1fd1ea2015-03-26 13:43:11 +01001330 .domain_alloc = exynos_iommu_domain_alloc,
1331 .domain_free = exynos_iommu_domain_free,
Bjorn Helgaasba5fa6f2014-05-08 14:49:14 -06001332 .attach_dev = exynos_iommu_attach_device,
1333 .detach_dev = exynos_iommu_detach_device,
1334 .map = exynos_iommu_map,
1335 .unmap = exynos_iommu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -07001336 .map_sg = default_iommu_map_sg,
Bjorn Helgaasba5fa6f2014-05-08 14:49:14 -06001337 .iova_to_phys = exynos_iommu_iova_to_phys,
Marek Szyprowski6c2ae7e2016-02-18 15:12:48 +01001338 .device_group = get_device_iommu_group,
Bjorn Helgaasba5fa6f2014-05-08 14:49:14 -06001339 .add_device = exynos_iommu_add_device,
1340 .remove_device = exynos_iommu_remove_device,
KyongHo Cho2a965362012-05-12 05:56:09 +09001341 .pgsize_bitmap = SECT_SIZE | LPAGE_SIZE | SPAGE_SIZE,
Marek Szyprowskiaa759fd2015-05-19 15:20:37 +02001342 .of_xlate = exynos_iommu_of_xlate,
KyongHo Cho2a965362012-05-12 05:56:09 +09001343};
1344
1345static int __init exynos_iommu_init(void)
1346{
1347 int ret;
1348
Cho KyongHo734c3c72014-05-12 11:44:48 +05301349 lv2table_kmem_cache = kmem_cache_create("exynos-iommu-lv2table",
1350 LV2TABLE_SIZE, LV2TABLE_SIZE, 0, NULL);
1351 if (!lv2table_kmem_cache) {
1352 pr_err("%s: Failed to create kmem cache\n", __func__);
1353 return -ENOMEM;
1354 }
1355
KyongHo Cho2a965362012-05-12 05:56:09 +09001356 ret = platform_driver_register(&exynos_sysmmu_driver);
Cho KyongHo734c3c72014-05-12 11:44:48 +05301357 if (ret) {
1358 pr_err("%s: Failed to register driver\n", __func__);
1359 goto err_reg_driver;
1360 }
KyongHo Cho2a965362012-05-12 05:56:09 +09001361
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301362 zero_lv2_table = kmem_cache_zalloc(lv2table_kmem_cache, GFP_KERNEL);
1363 if (zero_lv2_table == NULL) {
1364 pr_err("%s: Failed to allocate zero level2 page table\n",
1365 __func__);
1366 ret = -ENOMEM;
1367 goto err_zero_lv2;
1368 }
1369
Cho KyongHo734c3c72014-05-12 11:44:48 +05301370 ret = bus_set_iommu(&platform_bus_type, &exynos_iommu_ops);
1371 if (ret) {
1372 pr_err("%s: Failed to register exynos-iommu driver.\n",
1373 __func__);
1374 goto err_set_iommu;
1375 }
KyongHo Cho2a965362012-05-12 05:56:09 +09001376
Cho KyongHo734c3c72014-05-12 11:44:48 +05301377 return 0;
1378err_set_iommu:
Cho KyongHo66a7ed82014-05-12 11:45:04 +05301379 kmem_cache_free(lv2table_kmem_cache, zero_lv2_table);
1380err_zero_lv2:
Cho KyongHo734c3c72014-05-12 11:44:48 +05301381 platform_driver_unregister(&exynos_sysmmu_driver);
1382err_reg_driver:
1383 kmem_cache_destroy(lv2table_kmem_cache);
KyongHo Cho2a965362012-05-12 05:56:09 +09001384 return ret;
1385}
Marek Szyprowski928055a2017-08-04 12:28:33 +02001386core_initcall(exynos_iommu_init);
Marek Szyprowski8ed55c82015-05-19 15:20:36 +02001387
Marek Szyprowski928055a2017-08-04 12:28:33 +02001388IOMMU_OF_DECLARE(exynos_iommu_of, "samsung,exynos-sysmmu", NULL);