KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 1 | /* 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 Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 15 | #include <linux/clk.h> |
| 16 | #include <linux/err.h> |
Marek Szyprowski | 312900c | 2015-05-19 15:20:30 +0200 | [diff] [blame^] | 17 | #include <linux/io.h> |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 18 | #include <linux/iommu.h> |
Marek Szyprowski | 312900c | 2015-05-19 15:20:30 +0200 | [diff] [blame^] | 19 | #include <linux/interrupt.h> |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 20 | #include <linux/list.h> |
Marek Szyprowski | 312900c | 2015-05-19 15:20:30 +0200 | [diff] [blame^] | 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/pm_runtime.h> |
| 23 | #include <linux/slab.h> |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 24 | |
| 25 | #include <asm/cacheflush.h> |
| 26 | #include <asm/pgtable.h> |
| 27 | |
Cho KyongHo | d09d78f | 2014-05-12 11:44:58 +0530 | [diff] [blame] | 28 | typedef u32 sysmmu_iova_t; |
| 29 | typedef u32 sysmmu_pte_t; |
| 30 | |
Sachin Kamat | f171aba | 2014-08-04 10:06:28 +0530 | [diff] [blame] | 31 | /* We do not consider super section mapping (16MB) */ |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 32 | #define SECT_ORDER 20 |
| 33 | #define LPAGE_ORDER 16 |
| 34 | #define SPAGE_ORDER 12 |
| 35 | |
| 36 | #define SECT_SIZE (1 << SECT_ORDER) |
| 37 | #define LPAGE_SIZE (1 << LPAGE_ORDER) |
| 38 | #define SPAGE_SIZE (1 << SPAGE_ORDER) |
| 39 | |
| 40 | #define SECT_MASK (~(SECT_SIZE - 1)) |
| 41 | #define LPAGE_MASK (~(LPAGE_SIZE - 1)) |
| 42 | #define SPAGE_MASK (~(SPAGE_SIZE - 1)) |
| 43 | |
Cho KyongHo | 66a7ed8 | 2014-05-12 11:45:04 +0530 | [diff] [blame] | 44 | #define lv1ent_fault(sent) ((*(sent) == ZERO_LV2LINK) || \ |
| 45 | ((*(sent) & 3) == 0) || ((*(sent) & 3) == 3)) |
| 46 | #define lv1ent_zero(sent) (*(sent) == ZERO_LV2LINK) |
| 47 | #define lv1ent_page_zero(sent) ((*(sent) & 3) == 1) |
| 48 | #define lv1ent_page(sent) ((*(sent) != ZERO_LV2LINK) && \ |
| 49 | ((*(sent) & 3) == 1)) |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 50 | #define lv1ent_section(sent) ((*(sent) & 3) == 2) |
| 51 | |
| 52 | #define lv2ent_fault(pent) ((*(pent) & 3) == 0) |
| 53 | #define lv2ent_small(pent) ((*(pent) & 2) == 2) |
| 54 | #define lv2ent_large(pent) ((*(pent) & 3) == 1) |
| 55 | |
Cho KyongHo | d09d78f | 2014-05-12 11:44:58 +0530 | [diff] [blame] | 56 | static u32 sysmmu_page_offset(sysmmu_iova_t iova, u32 size) |
| 57 | { |
| 58 | return iova & (size - 1); |
| 59 | } |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 60 | |
Cho KyongHo | d09d78f | 2014-05-12 11:44:58 +0530 | [diff] [blame] | 61 | #define section_phys(sent) (*(sent) & SECT_MASK) |
| 62 | #define section_offs(iova) sysmmu_page_offset((iova), SECT_SIZE) |
| 63 | #define lpage_phys(pent) (*(pent) & LPAGE_MASK) |
| 64 | #define lpage_offs(iova) sysmmu_page_offset((iova), LPAGE_SIZE) |
| 65 | #define spage_phys(pent) (*(pent) & SPAGE_MASK) |
| 66 | #define spage_offs(iova) sysmmu_page_offset((iova), SPAGE_SIZE) |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 67 | |
| 68 | #define NUM_LV1ENTRIES 4096 |
Cho KyongHo | d09d78f | 2014-05-12 11:44:58 +0530 | [diff] [blame] | 69 | #define NUM_LV2ENTRIES (SECT_SIZE / SPAGE_SIZE) |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 70 | |
Cho KyongHo | d09d78f | 2014-05-12 11:44:58 +0530 | [diff] [blame] | 71 | static u32 lv1ent_offset(sysmmu_iova_t iova) |
| 72 | { |
| 73 | return iova >> SECT_ORDER; |
| 74 | } |
| 75 | |
| 76 | static u32 lv2ent_offset(sysmmu_iova_t iova) |
| 77 | { |
| 78 | return (iova >> SPAGE_ORDER) & (NUM_LV2ENTRIES - 1); |
| 79 | } |
| 80 | |
| 81 | #define LV2TABLE_SIZE (NUM_LV2ENTRIES * sizeof(sysmmu_pte_t)) |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 82 | |
| 83 | #define SPAGES_PER_LPAGE (LPAGE_SIZE / SPAGE_SIZE) |
| 84 | |
| 85 | #define lv2table_base(sent) (*(sent) & 0xFFFFFC00) |
| 86 | |
| 87 | #define mk_lv1ent_sect(pa) ((pa) | 2) |
| 88 | #define mk_lv1ent_page(pa) ((pa) | 1) |
| 89 | #define mk_lv2ent_lpage(pa) ((pa) | 1) |
| 90 | #define mk_lv2ent_spage(pa) ((pa) | 2) |
| 91 | |
| 92 | #define CTRL_ENABLE 0x5 |
| 93 | #define CTRL_BLOCK 0x7 |
| 94 | #define CTRL_DISABLE 0x0 |
| 95 | |
Cho KyongHo | eeb5184 | 2014-05-12 11:45:03 +0530 | [diff] [blame] | 96 | #define CFG_LRU 0x1 |
| 97 | #define CFG_QOS(n) ((n & 0xF) << 7) |
| 98 | #define CFG_MASK 0x0150FFFF /* Selecting bit 0-15, 20, 22 and 24 */ |
| 99 | #define CFG_ACGEN (1 << 24) /* System MMU 3.3 only */ |
| 100 | #define CFG_SYSSEL (1 << 22) /* System MMU 3.2 only */ |
| 101 | #define CFG_FLPDCACHE (1 << 20) /* System MMU 3.2+ only */ |
| 102 | |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 103 | #define REG_MMU_CTRL 0x000 |
| 104 | #define REG_MMU_CFG 0x004 |
| 105 | #define REG_MMU_STATUS 0x008 |
| 106 | #define REG_MMU_FLUSH 0x00C |
| 107 | #define REG_MMU_FLUSH_ENTRY 0x010 |
| 108 | #define REG_PT_BASE_ADDR 0x014 |
| 109 | #define REG_INT_STATUS 0x018 |
| 110 | #define REG_INT_CLEAR 0x01C |
| 111 | |
| 112 | #define REG_PAGE_FAULT_ADDR 0x024 |
| 113 | #define REG_AW_FAULT_ADDR 0x028 |
| 114 | #define REG_AR_FAULT_ADDR 0x02C |
| 115 | #define REG_DEFAULT_SLAVE_ADDR 0x030 |
| 116 | |
| 117 | #define REG_MMU_VERSION 0x034 |
| 118 | |
Cho KyongHo | eeb5184 | 2014-05-12 11:45:03 +0530 | [diff] [blame] | 119 | #define MMU_MAJ_VER(val) ((val) >> 7) |
| 120 | #define MMU_MIN_VER(val) ((val) & 0x7F) |
| 121 | #define MMU_RAW_VER(reg) (((reg) >> 21) & ((1 << 11) - 1)) /* 11 bits */ |
| 122 | |
| 123 | #define MAKE_MMU_VER(maj, min) ((((maj) & 0xF) << 7) | ((min) & 0x7F)) |
| 124 | |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 125 | #define REG_PB0_SADDR 0x04C |
| 126 | #define REG_PB0_EADDR 0x050 |
| 127 | #define REG_PB1_SADDR 0x054 |
| 128 | #define REG_PB1_EADDR 0x058 |
| 129 | |
Cho KyongHo | 6b21a5d | 2014-05-12 11:45:02 +0530 | [diff] [blame] | 130 | #define has_sysmmu(dev) (dev->archdata.iommu != NULL) |
| 131 | |
Cho KyongHo | 734c3c7 | 2014-05-12 11:44:48 +0530 | [diff] [blame] | 132 | static struct kmem_cache *lv2table_kmem_cache; |
Cho KyongHo | 66a7ed8 | 2014-05-12 11:45:04 +0530 | [diff] [blame] | 133 | static sysmmu_pte_t *zero_lv2_table; |
| 134 | #define ZERO_LV2LINK mk_lv1ent_page(virt_to_phys(zero_lv2_table)) |
Cho KyongHo | 734c3c7 | 2014-05-12 11:44:48 +0530 | [diff] [blame] | 135 | |
Cho KyongHo | d09d78f | 2014-05-12 11:44:58 +0530 | [diff] [blame] | 136 | static sysmmu_pte_t *section_entry(sysmmu_pte_t *pgtable, sysmmu_iova_t iova) |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 137 | { |
| 138 | return pgtable + lv1ent_offset(iova); |
| 139 | } |
| 140 | |
Cho KyongHo | d09d78f | 2014-05-12 11:44:58 +0530 | [diff] [blame] | 141 | static sysmmu_pte_t *page_entry(sysmmu_pte_t *sent, sysmmu_iova_t iova) |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 142 | { |
Cho KyongHo | d09d78f | 2014-05-12 11:44:58 +0530 | [diff] [blame] | 143 | return (sysmmu_pte_t *)phys_to_virt( |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 144 | lv2table_base(sent)) + lv2ent_offset(iova); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | enum exynos_sysmmu_inttype { |
| 148 | SYSMMU_PAGEFAULT, |
| 149 | SYSMMU_AR_MULTIHIT, |
| 150 | SYSMMU_AW_MULTIHIT, |
| 151 | SYSMMU_BUSERROR, |
| 152 | SYSMMU_AR_SECURITY, |
| 153 | SYSMMU_AR_ACCESS, |
| 154 | SYSMMU_AW_SECURITY, |
| 155 | SYSMMU_AW_PROTECTION, /* 7 */ |
| 156 | SYSMMU_FAULT_UNKNOWN, |
| 157 | SYSMMU_FAULTS_NUM |
| 158 | }; |
| 159 | |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 160 | static unsigned short fault_reg_offset[SYSMMU_FAULTS_NUM] = { |
| 161 | REG_PAGE_FAULT_ADDR, |
| 162 | REG_AR_FAULT_ADDR, |
| 163 | REG_AW_FAULT_ADDR, |
| 164 | REG_DEFAULT_SLAVE_ADDR, |
| 165 | REG_AR_FAULT_ADDR, |
| 166 | REG_AR_FAULT_ADDR, |
| 167 | REG_AW_FAULT_ADDR, |
| 168 | REG_AW_FAULT_ADDR |
| 169 | }; |
| 170 | |
| 171 | static char *sysmmu_fault_name[SYSMMU_FAULTS_NUM] = { |
| 172 | "PAGE FAULT", |
| 173 | "AR MULTI-HIT FAULT", |
| 174 | "AW MULTI-HIT FAULT", |
| 175 | "BUS ERROR", |
| 176 | "AR SECURITY PROTECTION FAULT", |
| 177 | "AR ACCESS PROTECTION FAULT", |
| 178 | "AW SECURITY PROTECTION FAULT", |
| 179 | "AW ACCESS PROTECTION FAULT", |
| 180 | "UNKNOWN FAULT" |
| 181 | }; |
| 182 | |
Cho KyongHo | 6b21a5d | 2014-05-12 11:45:02 +0530 | [diff] [blame] | 183 | /* attached to dev.archdata.iommu of the master device */ |
| 184 | struct exynos_iommu_owner { |
Cho KyongHo | 6b21a5d | 2014-05-12 11:45:02 +0530 | [diff] [blame] | 185 | struct device *sysmmu; |
Cho KyongHo | 6b21a5d | 2014-05-12 11:45:02 +0530 | [diff] [blame] | 186 | }; |
| 187 | |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 188 | struct exynos_iommu_domain { |
| 189 | struct list_head clients; /* list of sysmmu_drvdata.node */ |
Cho KyongHo | d09d78f | 2014-05-12 11:44:58 +0530 | [diff] [blame] | 190 | sysmmu_pte_t *pgtable; /* lv1 page table, 16KB */ |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 191 | short *lv2entcnt; /* free lv2 entry counter for each section */ |
| 192 | spinlock_t lock; /* lock for this structure */ |
| 193 | spinlock_t pgtablelock; /* lock for modifying page table @ pgtable */ |
Joerg Roedel | e1fd1ea | 2015-03-26 13:43:11 +0100 | [diff] [blame] | 194 | struct iommu_domain domain; /* generic domain data structure */ |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 195 | }; |
| 196 | |
| 197 | struct sysmmu_drvdata { |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 198 | struct device *sysmmu; /* System MMU's device descriptor */ |
Cho KyongHo | 6b21a5d | 2014-05-12 11:45:02 +0530 | [diff] [blame] | 199 | struct device *master; /* Owner of system MMU */ |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 200 | void __iomem *sfrbase; |
| 201 | struct clk *clk; |
Cho KyongHo | 7060587 | 2014-05-12 11:44:55 +0530 | [diff] [blame] | 202 | struct clk *clk_master; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 203 | int activations; |
Cho KyongHo | 9d4e7a2 | 2014-05-12 11:44:57 +0530 | [diff] [blame] | 204 | spinlock_t lock; |
Marek Szyprowski | a9133b99 | 2015-05-19 15:20:29 +0200 | [diff] [blame] | 205 | struct exynos_iommu_domain *domain; |
Marek Szyprowski | 469aceb | 2015-05-19 15:20:27 +0200 | [diff] [blame] | 206 | struct list_head domain_node; |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 207 | phys_addr_t pgtable; |
Marek Szyprowski | 512bd0c | 2015-05-19 15:20:24 +0200 | [diff] [blame] | 208 | unsigned int version; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 209 | }; |
| 210 | |
Joerg Roedel | e1fd1ea | 2015-03-26 13:43:11 +0100 | [diff] [blame] | 211 | static struct exynos_iommu_domain *to_exynos_domain(struct iommu_domain *dom) |
| 212 | { |
| 213 | return container_of(dom, struct exynos_iommu_domain, domain); |
| 214 | } |
| 215 | |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 216 | static bool set_sysmmu_active(struct sysmmu_drvdata *data) |
| 217 | { |
| 218 | /* return true if the System MMU was not active previously |
| 219 | and it needs to be initialized */ |
| 220 | return ++data->activations == 1; |
| 221 | } |
| 222 | |
| 223 | static bool set_sysmmu_inactive(struct sysmmu_drvdata *data) |
| 224 | { |
| 225 | /* return true if the System MMU is needed to be disabled */ |
| 226 | BUG_ON(data->activations < 1); |
| 227 | return --data->activations == 0; |
| 228 | } |
| 229 | |
| 230 | static bool is_sysmmu_active(struct sysmmu_drvdata *data) |
| 231 | { |
| 232 | return data->activations > 0; |
| 233 | } |
| 234 | |
| 235 | static void sysmmu_unblock(void __iomem *sfrbase) |
| 236 | { |
| 237 | __raw_writel(CTRL_ENABLE, sfrbase + REG_MMU_CTRL); |
| 238 | } |
| 239 | |
| 240 | static bool sysmmu_block(void __iomem *sfrbase) |
| 241 | { |
| 242 | int i = 120; |
| 243 | |
| 244 | __raw_writel(CTRL_BLOCK, sfrbase + REG_MMU_CTRL); |
| 245 | while ((i > 0) && !(__raw_readl(sfrbase + REG_MMU_STATUS) & 1)) |
| 246 | --i; |
| 247 | |
| 248 | if (!(__raw_readl(sfrbase + REG_MMU_STATUS) & 1)) { |
| 249 | sysmmu_unblock(sfrbase); |
| 250 | return false; |
| 251 | } |
| 252 | |
| 253 | return true; |
| 254 | } |
| 255 | |
| 256 | static void __sysmmu_tlb_invalidate(void __iomem *sfrbase) |
| 257 | { |
| 258 | __raw_writel(0x1, sfrbase + REG_MMU_FLUSH); |
| 259 | } |
| 260 | |
| 261 | static void __sysmmu_tlb_invalidate_entry(void __iomem *sfrbase, |
Cho KyongHo | d09d78f | 2014-05-12 11:44:58 +0530 | [diff] [blame] | 262 | sysmmu_iova_t iova, unsigned int num_inv) |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 263 | { |
Cho KyongHo | 3ad6b7f | 2014-05-12 11:44:49 +0530 | [diff] [blame] | 264 | unsigned int i; |
Sachin Kamat | 365409d | 2014-05-22 09:50:56 +0530 | [diff] [blame] | 265 | |
Cho KyongHo | 3ad6b7f | 2014-05-12 11:44:49 +0530 | [diff] [blame] | 266 | for (i = 0; i < num_inv; i++) { |
| 267 | __raw_writel((iova & SPAGE_MASK) | 1, |
| 268 | sfrbase + REG_MMU_FLUSH_ENTRY); |
| 269 | iova += SPAGE_SIZE; |
| 270 | } |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | static void __sysmmu_set_ptbase(void __iomem *sfrbase, |
Cho KyongHo | d09d78f | 2014-05-12 11:44:58 +0530 | [diff] [blame] | 274 | phys_addr_t pgd) |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 275 | { |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 276 | __raw_writel(pgd, sfrbase + REG_PT_BASE_ADDR); |
| 277 | |
| 278 | __sysmmu_tlb_invalidate(sfrbase); |
| 279 | } |
| 280 | |
Cho KyongHo | 1fab7fa | 2014-05-12 11:44:56 +0530 | [diff] [blame] | 281 | static void show_fault_information(const char *name, |
| 282 | enum exynos_sysmmu_inttype itype, |
Cho KyongHo | d09d78f | 2014-05-12 11:44:58 +0530 | [diff] [blame] | 283 | phys_addr_t pgtable_base, sysmmu_iova_t fault_addr) |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 284 | { |
Cho KyongHo | d09d78f | 2014-05-12 11:44:58 +0530 | [diff] [blame] | 285 | sysmmu_pte_t *ent; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 286 | |
| 287 | if ((itype >= SYSMMU_FAULTS_NUM) || (itype < SYSMMU_PAGEFAULT)) |
| 288 | itype = SYSMMU_FAULT_UNKNOWN; |
| 289 | |
Cho KyongHo | d09d78f | 2014-05-12 11:44:58 +0530 | [diff] [blame] | 290 | pr_err("%s occurred at %#x by %s(Page table base: %pa)\n", |
Cho KyongHo | 1fab7fa | 2014-05-12 11:44:56 +0530 | [diff] [blame] | 291 | sysmmu_fault_name[itype], fault_addr, name, &pgtable_base); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 292 | |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 293 | ent = section_entry(phys_to_virt(pgtable_base), fault_addr); |
Cho KyongHo | d09d78f | 2014-05-12 11:44:58 +0530 | [diff] [blame] | 294 | pr_err("\tLv1 entry: %#x\n", *ent); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 295 | |
| 296 | if (lv1ent_page(ent)) { |
| 297 | ent = page_entry(ent, fault_addr); |
Cho KyongHo | d09d78f | 2014-05-12 11:44:58 +0530 | [diff] [blame] | 298 | pr_err("\t Lv2 entry: %#x\n", *ent); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 299 | } |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | static irqreturn_t exynos_sysmmu_irq(int irq, void *dev_id) |
| 303 | { |
Sachin Kamat | f171aba | 2014-08-04 10:06:28 +0530 | [diff] [blame] | 304 | /* SYSMMU is in blocked state when interrupt occurred. */ |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 305 | struct sysmmu_drvdata *data = dev_id; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 306 | enum exynos_sysmmu_inttype itype; |
Cho KyongHo | d09d78f | 2014-05-12 11:44:58 +0530 | [diff] [blame] | 307 | sysmmu_iova_t addr = -1; |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 308 | int ret = -ENOSYS; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 309 | |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 310 | WARN_ON(!is_sysmmu_active(data)); |
| 311 | |
Cho KyongHo | 9d4e7a2 | 2014-05-12 11:44:57 +0530 | [diff] [blame] | 312 | spin_lock(&data->lock); |
| 313 | |
Cho KyongHo | 7060587 | 2014-05-12 11:44:55 +0530 | [diff] [blame] | 314 | if (!IS_ERR(data->clk_master)) |
| 315 | clk_enable(data->clk_master); |
Cho KyongHo | 9d4e7a2 | 2014-05-12 11:44:57 +0530 | [diff] [blame] | 316 | |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 317 | itype = (enum exynos_sysmmu_inttype) |
| 318 | __ffs(__raw_readl(data->sfrbase + REG_INT_STATUS)); |
| 319 | if (WARN_ON(!((itype >= 0) && (itype < SYSMMU_FAULT_UNKNOWN)))) |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 320 | itype = SYSMMU_FAULT_UNKNOWN; |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 321 | else |
| 322 | addr = __raw_readl(data->sfrbase + fault_reg_offset[itype]); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 323 | |
Cho KyongHo | 1fab7fa | 2014-05-12 11:44:56 +0530 | [diff] [blame] | 324 | if (itype == SYSMMU_FAULT_UNKNOWN) { |
| 325 | pr_err("%s: Fault is not occurred by System MMU '%s'!\n", |
| 326 | __func__, dev_name(data->sysmmu)); |
| 327 | pr_err("%s: Please check if IRQ is correctly configured.\n", |
| 328 | __func__); |
| 329 | BUG(); |
| 330 | } else { |
Cho KyongHo | d09d78f | 2014-05-12 11:44:58 +0530 | [diff] [blame] | 331 | unsigned int base = |
Cho KyongHo | 1fab7fa | 2014-05-12 11:44:56 +0530 | [diff] [blame] | 332 | __raw_readl(data->sfrbase + REG_PT_BASE_ADDR); |
| 333 | show_fault_information(dev_name(data->sysmmu), |
| 334 | itype, base, addr); |
| 335 | if (data->domain) |
Marek Szyprowski | a9133b99 | 2015-05-19 15:20:29 +0200 | [diff] [blame] | 336 | ret = report_iommu_fault(&data->domain->domain, |
Cho KyongHo | 6b21a5d | 2014-05-12 11:45:02 +0530 | [diff] [blame] | 337 | data->master, addr, itype); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 338 | } |
| 339 | |
Cho KyongHo | 1fab7fa | 2014-05-12 11:44:56 +0530 | [diff] [blame] | 340 | /* fault is not recovered by fault handler */ |
| 341 | BUG_ON(ret != 0); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 342 | |
Cho KyongHo | 1fab7fa | 2014-05-12 11:44:56 +0530 | [diff] [blame] | 343 | __raw_writel(1 << itype, data->sfrbase + REG_INT_CLEAR); |
| 344 | |
| 345 | sysmmu_unblock(data->sfrbase); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 346 | |
Cho KyongHo | 7060587 | 2014-05-12 11:44:55 +0530 | [diff] [blame] | 347 | if (!IS_ERR(data->clk_master)) |
| 348 | clk_disable(data->clk_master); |
| 349 | |
Cho KyongHo | 9d4e7a2 | 2014-05-12 11:44:57 +0530 | [diff] [blame] | 350 | spin_unlock(&data->lock); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 351 | |
| 352 | return IRQ_HANDLED; |
| 353 | } |
| 354 | |
Cho KyongHo | 6b21a5d | 2014-05-12 11:45:02 +0530 | [diff] [blame] | 355 | static void __sysmmu_disable_nocount(struct sysmmu_drvdata *data) |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 356 | { |
Cho KyongHo | 7060587 | 2014-05-12 11:44:55 +0530 | [diff] [blame] | 357 | if (!IS_ERR(data->clk_master)) |
| 358 | clk_enable(data->clk_master); |
| 359 | |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 360 | __raw_writel(CTRL_DISABLE, data->sfrbase + REG_MMU_CTRL); |
Cho KyongHo | 6b21a5d | 2014-05-12 11:45:02 +0530 | [diff] [blame] | 361 | __raw_writel(0, data->sfrbase + REG_MMU_CFG); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 362 | |
Cho KyongHo | 46c16d1 | 2014-05-12 11:44:54 +0530 | [diff] [blame] | 363 | clk_disable(data->clk); |
Cho KyongHo | 7060587 | 2014-05-12 11:44:55 +0530 | [diff] [blame] | 364 | if (!IS_ERR(data->clk_master)) |
| 365 | clk_disable(data->clk_master); |
Cho KyongHo | 6b21a5d | 2014-05-12 11:45:02 +0530 | [diff] [blame] | 366 | } |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 367 | |
Cho KyongHo | 6b21a5d | 2014-05-12 11:45:02 +0530 | [diff] [blame] | 368 | static bool __sysmmu_disable(struct sysmmu_drvdata *data) |
| 369 | { |
| 370 | bool disabled; |
| 371 | unsigned long flags; |
| 372 | |
| 373 | spin_lock_irqsave(&data->lock, flags); |
| 374 | |
| 375 | disabled = set_sysmmu_inactive(data); |
| 376 | |
| 377 | if (disabled) { |
| 378 | data->pgtable = 0; |
| 379 | data->domain = NULL; |
| 380 | |
| 381 | __sysmmu_disable_nocount(data); |
| 382 | |
| 383 | dev_dbg(data->sysmmu, "Disabled\n"); |
| 384 | } else { |
| 385 | dev_dbg(data->sysmmu, "%d times left to disable\n", |
| 386 | data->activations); |
| 387 | } |
| 388 | |
Cho KyongHo | 9d4e7a2 | 2014-05-12 11:44:57 +0530 | [diff] [blame] | 389 | spin_unlock_irqrestore(&data->lock, flags); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 390 | |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 391 | return disabled; |
| 392 | } |
| 393 | |
Cho KyongHo | 6b21a5d | 2014-05-12 11:45:02 +0530 | [diff] [blame] | 394 | static void __sysmmu_init_config(struct sysmmu_drvdata *data) |
| 395 | { |
Cho KyongHo | eeb5184 | 2014-05-12 11:45:03 +0530 | [diff] [blame] | 396 | unsigned int cfg = CFG_LRU | CFG_QOS(15); |
| 397 | unsigned int ver; |
| 398 | |
Marek Szyprowski | 512bd0c | 2015-05-19 15:20:24 +0200 | [diff] [blame] | 399 | ver = MMU_RAW_VER(__raw_readl(data->sfrbase + REG_MMU_VERSION)); |
Cho KyongHo | eeb5184 | 2014-05-12 11:45:03 +0530 | [diff] [blame] | 400 | if (MMU_MAJ_VER(ver) == 3) { |
| 401 | if (MMU_MIN_VER(ver) >= 2) { |
| 402 | cfg |= CFG_FLPDCACHE; |
| 403 | if (MMU_MIN_VER(ver) == 3) { |
| 404 | cfg |= CFG_ACGEN; |
| 405 | cfg &= ~CFG_LRU; |
| 406 | } else { |
| 407 | cfg |= CFG_SYSSEL; |
| 408 | } |
| 409 | } |
| 410 | } |
Cho KyongHo | 6b21a5d | 2014-05-12 11:45:02 +0530 | [diff] [blame] | 411 | |
| 412 | __raw_writel(cfg, data->sfrbase + REG_MMU_CFG); |
Marek Szyprowski | 512bd0c | 2015-05-19 15:20:24 +0200 | [diff] [blame] | 413 | data->version = ver; |
Cho KyongHo | 6b21a5d | 2014-05-12 11:45:02 +0530 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | static void __sysmmu_enable_nocount(struct sysmmu_drvdata *data) |
| 417 | { |
| 418 | if (!IS_ERR(data->clk_master)) |
| 419 | clk_enable(data->clk_master); |
| 420 | clk_enable(data->clk); |
| 421 | |
| 422 | __raw_writel(CTRL_BLOCK, data->sfrbase + REG_MMU_CTRL); |
| 423 | |
| 424 | __sysmmu_init_config(data); |
| 425 | |
| 426 | __sysmmu_set_ptbase(data->sfrbase, data->pgtable); |
| 427 | |
| 428 | __raw_writel(CTRL_ENABLE, data->sfrbase + REG_MMU_CTRL); |
| 429 | |
| 430 | if (!IS_ERR(data->clk_master)) |
| 431 | clk_disable(data->clk_master); |
| 432 | } |
| 433 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 434 | static int __sysmmu_enable(struct sysmmu_drvdata *data, phys_addr_t pgtable, |
Marek Szyprowski | a9133b99 | 2015-05-19 15:20:29 +0200 | [diff] [blame] | 435 | struct exynos_iommu_domain *domain) |
Cho KyongHo | 6b21a5d | 2014-05-12 11:45:02 +0530 | [diff] [blame] | 436 | { |
| 437 | int ret = 0; |
| 438 | unsigned long flags; |
| 439 | |
| 440 | spin_lock_irqsave(&data->lock, flags); |
| 441 | if (set_sysmmu_active(data)) { |
| 442 | data->pgtable = pgtable; |
Marek Szyprowski | a9133b99 | 2015-05-19 15:20:29 +0200 | [diff] [blame] | 443 | data->domain = domain; |
Cho KyongHo | 6b21a5d | 2014-05-12 11:45:02 +0530 | [diff] [blame] | 444 | |
| 445 | __sysmmu_enable_nocount(data); |
| 446 | |
| 447 | dev_dbg(data->sysmmu, "Enabled\n"); |
| 448 | } else { |
| 449 | ret = (pgtable == data->pgtable) ? 1 : -EBUSY; |
| 450 | |
| 451 | dev_dbg(data->sysmmu, "already enabled\n"); |
| 452 | } |
| 453 | |
| 454 | if (WARN_ON(ret < 0)) |
| 455 | set_sysmmu_inactive(data); /* decrement count */ |
| 456 | |
| 457 | spin_unlock_irqrestore(&data->lock, flags); |
| 458 | |
| 459 | return ret; |
| 460 | } |
| 461 | |
Cho KyongHo | 66a7ed8 | 2014-05-12 11:45:04 +0530 | [diff] [blame] | 462 | static void __sysmmu_tlb_invalidate_flpdcache(struct sysmmu_drvdata *data, |
| 463 | sysmmu_iova_t iova) |
| 464 | { |
Marek Szyprowski | 512bd0c | 2015-05-19 15:20:24 +0200 | [diff] [blame] | 465 | if (data->version == MAKE_MMU_VER(3, 3)) |
Cho KyongHo | 66a7ed8 | 2014-05-12 11:45:04 +0530 | [diff] [blame] | 466 | __raw_writel(iova | 0x1, data->sfrbase + REG_MMU_FLUSH_ENTRY); |
| 467 | } |
| 468 | |
Marek Szyprowski | 469aceb | 2015-05-19 15:20:27 +0200 | [diff] [blame] | 469 | static void sysmmu_tlb_invalidate_flpdcache(struct sysmmu_drvdata *data, |
Cho KyongHo | 66a7ed8 | 2014-05-12 11:45:04 +0530 | [diff] [blame] | 470 | sysmmu_iova_t iova) |
| 471 | { |
| 472 | unsigned long flags; |
Cho KyongHo | 66a7ed8 | 2014-05-12 11:45:04 +0530 | [diff] [blame] | 473 | |
| 474 | if (!IS_ERR(data->clk_master)) |
| 475 | clk_enable(data->clk_master); |
| 476 | |
| 477 | spin_lock_irqsave(&data->lock, flags); |
| 478 | if (is_sysmmu_active(data)) |
| 479 | __sysmmu_tlb_invalidate_flpdcache(data, iova); |
| 480 | spin_unlock_irqrestore(&data->lock, flags); |
| 481 | |
| 482 | if (!IS_ERR(data->clk_master)) |
| 483 | clk_disable(data->clk_master); |
| 484 | } |
| 485 | |
Marek Szyprowski | 469aceb | 2015-05-19 15:20:27 +0200 | [diff] [blame] | 486 | static void sysmmu_tlb_invalidate_entry(struct sysmmu_drvdata *data, |
| 487 | sysmmu_iova_t iova, size_t size) |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 488 | { |
| 489 | unsigned long flags; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 490 | |
Cho KyongHo | 9d4e7a2 | 2014-05-12 11:44:57 +0530 | [diff] [blame] | 491 | spin_lock_irqsave(&data->lock, flags); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 492 | if (is_sysmmu_active(data)) { |
Cho KyongHo | 3ad6b7f | 2014-05-12 11:44:49 +0530 | [diff] [blame] | 493 | unsigned int num_inv = 1; |
Cho KyongHo | 7060587 | 2014-05-12 11:44:55 +0530 | [diff] [blame] | 494 | |
| 495 | if (!IS_ERR(data->clk_master)) |
| 496 | clk_enable(data->clk_master); |
| 497 | |
Cho KyongHo | 3ad6b7f | 2014-05-12 11:44:49 +0530 | [diff] [blame] | 498 | /* |
| 499 | * L2TLB invalidation required |
| 500 | * 4KB page: 1 invalidation |
Sachin Kamat | f171aba | 2014-08-04 10:06:28 +0530 | [diff] [blame] | 501 | * 64KB page: 16 invalidations |
| 502 | * 1MB page: 64 invalidations |
Cho KyongHo | 3ad6b7f | 2014-05-12 11:44:49 +0530 | [diff] [blame] | 503 | * because it is set-associative TLB |
| 504 | * with 8-way and 64 sets. |
| 505 | * 1MB page can be cached in one of all sets. |
| 506 | * 64KB page can be one of 16 consecutive sets. |
| 507 | */ |
Marek Szyprowski | 512bd0c | 2015-05-19 15:20:24 +0200 | [diff] [blame] | 508 | if (MMU_MAJ_VER(data->version) == 2) |
Cho KyongHo | 3ad6b7f | 2014-05-12 11:44:49 +0530 | [diff] [blame] | 509 | num_inv = min_t(unsigned int, size / PAGE_SIZE, 64); |
| 510 | |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 511 | if (sysmmu_block(data->sfrbase)) { |
| 512 | __sysmmu_tlb_invalidate_entry( |
Cho KyongHo | 3ad6b7f | 2014-05-12 11:44:49 +0530 | [diff] [blame] | 513 | data->sfrbase, iova, num_inv); |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 514 | sysmmu_unblock(data->sfrbase); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 515 | } |
Cho KyongHo | 7060587 | 2014-05-12 11:44:55 +0530 | [diff] [blame] | 516 | if (!IS_ERR(data->clk_master)) |
| 517 | clk_disable(data->clk_master); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 518 | } else { |
Marek Szyprowski | 469aceb | 2015-05-19 15:20:27 +0200 | [diff] [blame] | 519 | dev_dbg(data->master, |
| 520 | "disabled. Skipping TLB invalidation @ %#x\n", iova); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 521 | } |
Cho KyongHo | 9d4e7a2 | 2014-05-12 11:44:57 +0530 | [diff] [blame] | 522 | spin_unlock_irqrestore(&data->lock, flags); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 523 | } |
| 524 | |
Cho KyongHo | 6b21a5d | 2014-05-12 11:45:02 +0530 | [diff] [blame] | 525 | static int __init exynos_sysmmu_probe(struct platform_device *pdev) |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 526 | { |
Cho KyongHo | 46c16d1 | 2014-05-12 11:44:54 +0530 | [diff] [blame] | 527 | int irq, ret; |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 528 | struct device *dev = &pdev->dev; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 529 | struct sysmmu_drvdata *data; |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 530 | struct resource *res; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 531 | |
Cho KyongHo | 46c16d1 | 2014-05-12 11:44:54 +0530 | [diff] [blame] | 532 | data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); |
| 533 | if (!data) |
| 534 | return -ENOMEM; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 535 | |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 536 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Cho KyongHo | 46c16d1 | 2014-05-12 11:44:54 +0530 | [diff] [blame] | 537 | data->sfrbase = devm_ioremap_resource(dev, res); |
| 538 | if (IS_ERR(data->sfrbase)) |
| 539 | return PTR_ERR(data->sfrbase); |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 540 | |
Cho KyongHo | 46c16d1 | 2014-05-12 11:44:54 +0530 | [diff] [blame] | 541 | irq = platform_get_irq(pdev, 0); |
| 542 | if (irq <= 0) { |
Cho KyongHo | 0bf4e54 | 2014-05-12 11:45:00 +0530 | [diff] [blame] | 543 | dev_err(dev, "Unable to find IRQ resource\n"); |
Cho KyongHo | 46c16d1 | 2014-05-12 11:44:54 +0530 | [diff] [blame] | 544 | return irq; |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 545 | } |
| 546 | |
Cho KyongHo | 46c16d1 | 2014-05-12 11:44:54 +0530 | [diff] [blame] | 547 | ret = devm_request_irq(dev, irq, exynos_sysmmu_irq, 0, |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 548 | dev_name(dev), data); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 549 | if (ret) { |
Cho KyongHo | 46c16d1 | 2014-05-12 11:44:54 +0530 | [diff] [blame] | 550 | dev_err(dev, "Unabled to register handler of irq %d\n", irq); |
| 551 | return ret; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 552 | } |
| 553 | |
Cho KyongHo | 46c16d1 | 2014-05-12 11:44:54 +0530 | [diff] [blame] | 554 | data->clk = devm_clk_get(dev, "sysmmu"); |
| 555 | if (IS_ERR(data->clk)) { |
| 556 | dev_err(dev, "Failed to get clock!\n"); |
| 557 | return PTR_ERR(data->clk); |
| 558 | } else { |
| 559 | ret = clk_prepare(data->clk); |
| 560 | if (ret) { |
| 561 | dev_err(dev, "Failed to prepare clk\n"); |
| 562 | return ret; |
| 563 | } |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 564 | } |
| 565 | |
Cho KyongHo | 7060587 | 2014-05-12 11:44:55 +0530 | [diff] [blame] | 566 | data->clk_master = devm_clk_get(dev, "master"); |
| 567 | if (!IS_ERR(data->clk_master)) { |
| 568 | ret = clk_prepare(data->clk_master); |
| 569 | if (ret) { |
| 570 | clk_unprepare(data->clk); |
| 571 | dev_err(dev, "Failed to prepare master's clk\n"); |
| 572 | return ret; |
| 573 | } |
| 574 | } |
| 575 | |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 576 | data->sysmmu = dev; |
Cho KyongHo | 9d4e7a2 | 2014-05-12 11:44:57 +0530 | [diff] [blame] | 577 | spin_lock_init(&data->lock); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 578 | |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 579 | platform_set_drvdata(pdev, data); |
| 580 | |
Cho KyongHo | f4723ec | 2014-05-12 11:44:52 +0530 | [diff] [blame] | 581 | pm_runtime_enable(dev); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 582 | |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 583 | return 0; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 584 | } |
| 585 | |
Cho KyongHo | 6b21a5d | 2014-05-12 11:45:02 +0530 | [diff] [blame] | 586 | static const struct of_device_id sysmmu_of_match[] __initconst = { |
| 587 | { .compatible = "samsung,exynos-sysmmu", }, |
| 588 | { }, |
| 589 | }; |
| 590 | |
| 591 | static struct platform_driver exynos_sysmmu_driver __refdata = { |
| 592 | .probe = exynos_sysmmu_probe, |
| 593 | .driver = { |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 594 | .name = "exynos-sysmmu", |
Cho KyongHo | 6b21a5d | 2014-05-12 11:45:02 +0530 | [diff] [blame] | 595 | .of_match_table = sysmmu_of_match, |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 596 | } |
| 597 | }; |
| 598 | |
| 599 | static inline void pgtable_flush(void *vastart, void *vaend) |
| 600 | { |
| 601 | dmac_flush_range(vastart, vaend); |
| 602 | outer_flush_range(virt_to_phys(vastart), |
| 603 | virt_to_phys(vaend)); |
| 604 | } |
| 605 | |
Joerg Roedel | e1fd1ea | 2015-03-26 13:43:11 +0100 | [diff] [blame] | 606 | static struct iommu_domain *exynos_iommu_domain_alloc(unsigned type) |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 607 | { |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 608 | struct exynos_iommu_domain *domain; |
Cho KyongHo | 66a7ed8 | 2014-05-12 11:45:04 +0530 | [diff] [blame] | 609 | int i; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 610 | |
Joerg Roedel | e1fd1ea | 2015-03-26 13:43:11 +0100 | [diff] [blame] | 611 | if (type != IOMMU_DOMAIN_UNMANAGED) |
| 612 | return NULL; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 613 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 614 | domain = kzalloc(sizeof(*domain), GFP_KERNEL); |
| 615 | if (!domain) |
Joerg Roedel | e1fd1ea | 2015-03-26 13:43:11 +0100 | [diff] [blame] | 616 | return NULL; |
| 617 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 618 | domain->pgtable = (sysmmu_pte_t *)__get_free_pages(GFP_KERNEL, 2); |
| 619 | if (!domain->pgtable) |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 620 | goto err_pgtable; |
| 621 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 622 | domain->lv2entcnt = (short *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1); |
| 623 | if (!domain->lv2entcnt) |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 624 | goto err_counter; |
| 625 | |
Sachin Kamat | f171aba | 2014-08-04 10:06:28 +0530 | [diff] [blame] | 626 | /* Workaround for System MMU v3.3 to prevent caching 1MiB mapping */ |
Cho KyongHo | 66a7ed8 | 2014-05-12 11:45:04 +0530 | [diff] [blame] | 627 | for (i = 0; i < NUM_LV1ENTRIES; i += 8) { |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 628 | domain->pgtable[i + 0] = ZERO_LV2LINK; |
| 629 | domain->pgtable[i + 1] = ZERO_LV2LINK; |
| 630 | domain->pgtable[i + 2] = ZERO_LV2LINK; |
| 631 | domain->pgtable[i + 3] = ZERO_LV2LINK; |
| 632 | domain->pgtable[i + 4] = ZERO_LV2LINK; |
| 633 | domain->pgtable[i + 5] = ZERO_LV2LINK; |
| 634 | domain->pgtable[i + 6] = ZERO_LV2LINK; |
| 635 | domain->pgtable[i + 7] = ZERO_LV2LINK; |
Cho KyongHo | 66a7ed8 | 2014-05-12 11:45:04 +0530 | [diff] [blame] | 636 | } |
| 637 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 638 | pgtable_flush(domain->pgtable, domain->pgtable + NUM_LV1ENTRIES); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 639 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 640 | spin_lock_init(&domain->lock); |
| 641 | spin_lock_init(&domain->pgtablelock); |
| 642 | INIT_LIST_HEAD(&domain->clients); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 643 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 644 | domain->domain.geometry.aperture_start = 0; |
| 645 | domain->domain.geometry.aperture_end = ~0UL; |
| 646 | domain->domain.geometry.force_aperture = true; |
Joerg Roedel | 3177bb7 | 2012-07-11 12:41:10 +0200 | [diff] [blame] | 647 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 648 | return &domain->domain; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 649 | |
| 650 | err_counter: |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 651 | free_pages((unsigned long)domain->pgtable, 2); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 652 | err_pgtable: |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 653 | kfree(domain); |
Joerg Roedel | e1fd1ea | 2015-03-26 13:43:11 +0100 | [diff] [blame] | 654 | return NULL; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 655 | } |
| 656 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 657 | static void exynos_iommu_domain_free(struct iommu_domain *iommu_domain) |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 658 | { |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 659 | struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain); |
Marek Szyprowski | 469aceb | 2015-05-19 15:20:27 +0200 | [diff] [blame] | 660 | struct sysmmu_drvdata *data, *next; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 661 | unsigned long flags; |
| 662 | int i; |
| 663 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 664 | WARN_ON(!list_empty(&domain->clients)); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 665 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 666 | spin_lock_irqsave(&domain->lock, flags); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 667 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 668 | list_for_each_entry_safe(data, next, &domain->clients, domain_node) { |
Marek Szyprowski | 469aceb | 2015-05-19 15:20:27 +0200 | [diff] [blame] | 669 | if (__sysmmu_disable(data)) |
| 670 | data->master = NULL; |
| 671 | list_del_init(&data->domain_node); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 672 | } |
| 673 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 674 | spin_unlock_irqrestore(&domain->lock, flags); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 675 | |
| 676 | for (i = 0; i < NUM_LV1ENTRIES; i++) |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 677 | if (lv1ent_page(domain->pgtable + i)) |
Cho KyongHo | 734c3c7 | 2014-05-12 11:44:48 +0530 | [diff] [blame] | 678 | kmem_cache_free(lv2table_kmem_cache, |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 679 | phys_to_virt(lv2table_base(domain->pgtable + i))); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 680 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 681 | free_pages((unsigned long)domain->pgtable, 2); |
| 682 | free_pages((unsigned long)domain->lv2entcnt, 1); |
| 683 | kfree(domain); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 684 | } |
| 685 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 686 | static int exynos_iommu_attach_device(struct iommu_domain *iommu_domain, |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 687 | struct device *dev) |
| 688 | { |
Cho KyongHo | 6b21a5d | 2014-05-12 11:45:02 +0530 | [diff] [blame] | 689 | struct exynos_iommu_owner *owner = dev->archdata.iommu; |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 690 | struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain); |
Marek Szyprowski | 469aceb | 2015-05-19 15:20:27 +0200 | [diff] [blame] | 691 | struct sysmmu_drvdata *data; |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 692 | phys_addr_t pagetable = virt_to_phys(domain->pgtable); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 693 | unsigned long flags; |
Marek Szyprowski | 469aceb | 2015-05-19 15:20:27 +0200 | [diff] [blame] | 694 | int ret = -ENODEV; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 695 | |
Marek Szyprowski | 469aceb | 2015-05-19 15:20:27 +0200 | [diff] [blame] | 696 | if (!has_sysmmu(dev)) |
| 697 | return -ENODEV; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 698 | |
Marek Szyprowski | 469aceb | 2015-05-19 15:20:27 +0200 | [diff] [blame] | 699 | data = dev_get_drvdata(owner->sysmmu); |
| 700 | if (data) { |
Marek Szyprowski | a9133b99 | 2015-05-19 15:20:29 +0200 | [diff] [blame] | 701 | ret = __sysmmu_enable(data, pagetable, domain); |
Marek Szyprowski | 469aceb | 2015-05-19 15:20:27 +0200 | [diff] [blame] | 702 | if (ret >= 0) { |
| 703 | data->master = dev; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 704 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 705 | spin_lock_irqsave(&domain->lock, flags); |
| 706 | list_add_tail(&data->domain_node, &domain->clients); |
| 707 | spin_unlock_irqrestore(&domain->lock, flags); |
Marek Szyprowski | 469aceb | 2015-05-19 15:20:27 +0200 | [diff] [blame] | 708 | } |
| 709 | } |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 710 | |
| 711 | if (ret < 0) { |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 712 | dev_err(dev, "%s: Failed to attach IOMMU with pgtable %pa\n", |
| 713 | __func__, &pagetable); |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 714 | return ret; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 715 | } |
| 716 | |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 717 | dev_dbg(dev, "%s: Attached IOMMU with pgtable %pa %s\n", |
| 718 | __func__, &pagetable, (ret == 0) ? "" : ", again"); |
| 719 | |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 720 | return ret; |
| 721 | } |
| 722 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 723 | static void exynos_iommu_detach_device(struct iommu_domain *iommu_domain, |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 724 | struct device *dev) |
| 725 | { |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 726 | struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain); |
| 727 | phys_addr_t pagetable = virt_to_phys(domain->pgtable); |
Marek Szyprowski | 469aceb | 2015-05-19 15:20:27 +0200 | [diff] [blame] | 728 | struct sysmmu_drvdata *data; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 729 | unsigned long flags; |
Marek Szyprowski | 469aceb | 2015-05-19 15:20:27 +0200 | [diff] [blame] | 730 | bool found = false; |
| 731 | |
| 732 | if (!has_sysmmu(dev)) |
| 733 | return; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 734 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 735 | spin_lock_irqsave(&domain->lock, flags); |
| 736 | list_for_each_entry(data, &domain->clients, domain_node) { |
Marek Szyprowski | 469aceb | 2015-05-19 15:20:27 +0200 | [diff] [blame] | 737 | if (data->master == dev) { |
| 738 | if (__sysmmu_disable(data)) { |
| 739 | data->master = NULL; |
| 740 | list_del_init(&data->domain_node); |
| 741 | } |
| 742 | found = true; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 743 | break; |
| 744 | } |
| 745 | } |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 746 | spin_unlock_irqrestore(&domain->lock, flags); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 747 | |
Marek Szyprowski | 469aceb | 2015-05-19 15:20:27 +0200 | [diff] [blame] | 748 | if (found) |
Cho KyongHo | 6b21a5d | 2014-05-12 11:45:02 +0530 | [diff] [blame] | 749 | dev_dbg(dev, "%s: Detached IOMMU with pgtable %pa\n", |
| 750 | __func__, &pagetable); |
| 751 | else |
| 752 | dev_err(dev, "%s: No IOMMU is attached\n", __func__); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 753 | } |
| 754 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 755 | static sysmmu_pte_t *alloc_lv2entry(struct exynos_iommu_domain *domain, |
Cho KyongHo | 66a7ed8 | 2014-05-12 11:45:04 +0530 | [diff] [blame] | 756 | sysmmu_pte_t *sent, sysmmu_iova_t iova, short *pgcounter) |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 757 | { |
Cho KyongHo | 61128f0 | 2014-05-12 11:44:47 +0530 | [diff] [blame] | 758 | if (lv1ent_section(sent)) { |
Cho KyongHo | d09d78f | 2014-05-12 11:44:58 +0530 | [diff] [blame] | 759 | WARN(1, "Trying mapping on %#08x mapped with 1MiB page", iova); |
Cho KyongHo | 61128f0 | 2014-05-12 11:44:47 +0530 | [diff] [blame] | 760 | return ERR_PTR(-EADDRINUSE); |
| 761 | } |
| 762 | |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 763 | if (lv1ent_fault(sent)) { |
Cho KyongHo | d09d78f | 2014-05-12 11:44:58 +0530 | [diff] [blame] | 764 | sysmmu_pte_t *pent; |
Cho KyongHo | 66a7ed8 | 2014-05-12 11:45:04 +0530 | [diff] [blame] | 765 | bool need_flush_flpd_cache = lv1ent_zero(sent); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 766 | |
Cho KyongHo | 734c3c7 | 2014-05-12 11:44:48 +0530 | [diff] [blame] | 767 | pent = kmem_cache_zalloc(lv2table_kmem_cache, GFP_ATOMIC); |
Cho KyongHo | d09d78f | 2014-05-12 11:44:58 +0530 | [diff] [blame] | 768 | BUG_ON((unsigned int)pent & (LV2TABLE_SIZE - 1)); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 769 | if (!pent) |
Cho KyongHo | 61128f0 | 2014-05-12 11:44:47 +0530 | [diff] [blame] | 770 | return ERR_PTR(-ENOMEM); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 771 | |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 772 | *sent = mk_lv1ent_page(virt_to_phys(pent)); |
Colin Cross | dc3814f | 2015-05-08 17:05:44 -0700 | [diff] [blame] | 773 | kmemleak_ignore(pent); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 774 | *pgcounter = NUM_LV2ENTRIES; |
| 775 | pgtable_flush(pent, pent + NUM_LV2ENTRIES); |
| 776 | pgtable_flush(sent, sent + 1); |
Cho KyongHo | 66a7ed8 | 2014-05-12 11:45:04 +0530 | [diff] [blame] | 777 | |
| 778 | /* |
Sachin Kamat | f171aba | 2014-08-04 10:06:28 +0530 | [diff] [blame] | 779 | * If pre-fetched SLPD is a faulty SLPD in zero_l2_table, |
| 780 | * FLPD cache may cache the address of zero_l2_table. This |
| 781 | * function replaces the zero_l2_table with new L2 page table |
| 782 | * to write valid mappings. |
Cho KyongHo | 66a7ed8 | 2014-05-12 11:45:04 +0530 | [diff] [blame] | 783 | * Accessing the valid area may cause page fault since FLPD |
Sachin Kamat | f171aba | 2014-08-04 10:06:28 +0530 | [diff] [blame] | 784 | * cache may still cache zero_l2_table for the valid area |
| 785 | * instead of new L2 page table that has the mapping |
| 786 | * information of the valid area. |
Cho KyongHo | 66a7ed8 | 2014-05-12 11:45:04 +0530 | [diff] [blame] | 787 | * Thus any replacement of zero_l2_table with other valid L2 |
| 788 | * page table must involve FLPD cache invalidation for System |
| 789 | * MMU v3.3. |
| 790 | * FLPD cache invalidation is performed with TLB invalidation |
| 791 | * by VPN without blocking. It is safe to invalidate TLB without |
| 792 | * blocking because the target address of TLB invalidation is |
| 793 | * not currently mapped. |
| 794 | */ |
| 795 | if (need_flush_flpd_cache) { |
Marek Szyprowski | 469aceb | 2015-05-19 15:20:27 +0200 | [diff] [blame] | 796 | struct sysmmu_drvdata *data; |
Sachin Kamat | 365409d | 2014-05-22 09:50:56 +0530 | [diff] [blame] | 797 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 798 | spin_lock(&domain->lock); |
| 799 | list_for_each_entry(data, &domain->clients, domain_node) |
Marek Szyprowski | 469aceb | 2015-05-19 15:20:27 +0200 | [diff] [blame] | 800 | sysmmu_tlb_invalidate_flpdcache(data, iova); |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 801 | spin_unlock(&domain->lock); |
Cho KyongHo | 66a7ed8 | 2014-05-12 11:45:04 +0530 | [diff] [blame] | 802 | } |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | return page_entry(sent, iova); |
| 806 | } |
| 807 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 808 | static int lv1set_section(struct exynos_iommu_domain *domain, |
Cho KyongHo | 66a7ed8 | 2014-05-12 11:45:04 +0530 | [diff] [blame] | 809 | sysmmu_pte_t *sent, sysmmu_iova_t iova, |
Cho KyongHo | 61128f0 | 2014-05-12 11:44:47 +0530 | [diff] [blame] | 810 | phys_addr_t paddr, short *pgcnt) |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 811 | { |
Cho KyongHo | 61128f0 | 2014-05-12 11:44:47 +0530 | [diff] [blame] | 812 | if (lv1ent_section(sent)) { |
Cho KyongHo | d09d78f | 2014-05-12 11:44:58 +0530 | [diff] [blame] | 813 | WARN(1, "Trying mapping on 1MiB@%#08x that is mapped", |
Cho KyongHo | 61128f0 | 2014-05-12 11:44:47 +0530 | [diff] [blame] | 814 | iova); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 815 | return -EADDRINUSE; |
Cho KyongHo | 61128f0 | 2014-05-12 11:44:47 +0530 | [diff] [blame] | 816 | } |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 817 | |
| 818 | if (lv1ent_page(sent)) { |
Cho KyongHo | 61128f0 | 2014-05-12 11:44:47 +0530 | [diff] [blame] | 819 | if (*pgcnt != NUM_LV2ENTRIES) { |
Cho KyongHo | d09d78f | 2014-05-12 11:44:58 +0530 | [diff] [blame] | 820 | WARN(1, "Trying mapping on 1MiB@%#08x that is mapped", |
Cho KyongHo | 61128f0 | 2014-05-12 11:44:47 +0530 | [diff] [blame] | 821 | iova); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 822 | return -EADDRINUSE; |
Cho KyongHo | 61128f0 | 2014-05-12 11:44:47 +0530 | [diff] [blame] | 823 | } |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 824 | |
Cho KyongHo | 734c3c7 | 2014-05-12 11:44:48 +0530 | [diff] [blame] | 825 | kmem_cache_free(lv2table_kmem_cache, page_entry(sent, 0)); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 826 | *pgcnt = 0; |
| 827 | } |
| 828 | |
| 829 | *sent = mk_lv1ent_sect(paddr); |
| 830 | |
| 831 | pgtable_flush(sent, sent + 1); |
| 832 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 833 | spin_lock(&domain->lock); |
Cho KyongHo | 66a7ed8 | 2014-05-12 11:45:04 +0530 | [diff] [blame] | 834 | if (lv1ent_page_zero(sent)) { |
Marek Szyprowski | 469aceb | 2015-05-19 15:20:27 +0200 | [diff] [blame] | 835 | struct sysmmu_drvdata *data; |
Cho KyongHo | 66a7ed8 | 2014-05-12 11:45:04 +0530 | [diff] [blame] | 836 | /* |
| 837 | * Flushing FLPD cache in System MMU v3.3 that may cache a FLPD |
| 838 | * entry by speculative prefetch of SLPD which has no mapping. |
| 839 | */ |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 840 | list_for_each_entry(data, &domain->clients, domain_node) |
Marek Szyprowski | 469aceb | 2015-05-19 15:20:27 +0200 | [diff] [blame] | 841 | sysmmu_tlb_invalidate_flpdcache(data, iova); |
Cho KyongHo | 66a7ed8 | 2014-05-12 11:45:04 +0530 | [diff] [blame] | 842 | } |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 843 | spin_unlock(&domain->lock); |
Cho KyongHo | 66a7ed8 | 2014-05-12 11:45:04 +0530 | [diff] [blame] | 844 | |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 845 | return 0; |
| 846 | } |
| 847 | |
Cho KyongHo | d09d78f | 2014-05-12 11:44:58 +0530 | [diff] [blame] | 848 | static int lv2set_page(sysmmu_pte_t *pent, phys_addr_t paddr, size_t size, |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 849 | short *pgcnt) |
| 850 | { |
| 851 | if (size == SPAGE_SIZE) { |
Cho KyongHo | 0bf4e54 | 2014-05-12 11:45:00 +0530 | [diff] [blame] | 852 | if (WARN_ON(!lv2ent_fault(pent))) |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 853 | return -EADDRINUSE; |
| 854 | |
| 855 | *pent = mk_lv2ent_spage(paddr); |
| 856 | pgtable_flush(pent, pent + 1); |
| 857 | *pgcnt -= 1; |
| 858 | } else { /* size == LPAGE_SIZE */ |
| 859 | int i; |
Sachin Kamat | 365409d | 2014-05-22 09:50:56 +0530 | [diff] [blame] | 860 | |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 861 | for (i = 0; i < SPAGES_PER_LPAGE; i++, pent++) { |
Cho KyongHo | 0bf4e54 | 2014-05-12 11:45:00 +0530 | [diff] [blame] | 862 | if (WARN_ON(!lv2ent_fault(pent))) { |
Cho KyongHo | 61128f0 | 2014-05-12 11:44:47 +0530 | [diff] [blame] | 863 | if (i > 0) |
| 864 | memset(pent - i, 0, sizeof(*pent) * i); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 865 | return -EADDRINUSE; |
| 866 | } |
| 867 | |
| 868 | *pent = mk_lv2ent_lpage(paddr); |
| 869 | } |
| 870 | pgtable_flush(pent - SPAGES_PER_LPAGE, pent); |
| 871 | *pgcnt -= SPAGES_PER_LPAGE; |
| 872 | } |
| 873 | |
| 874 | return 0; |
| 875 | } |
| 876 | |
Cho KyongHo | 66a7ed8 | 2014-05-12 11:45:04 +0530 | [diff] [blame] | 877 | /* |
| 878 | * *CAUTION* to the I/O virtual memory managers that support exynos-iommu: |
| 879 | * |
Sachin Kamat | f171aba | 2014-08-04 10:06:28 +0530 | [diff] [blame] | 880 | * System MMU v3.x has advanced logic to improve address translation |
Cho KyongHo | 66a7ed8 | 2014-05-12 11:45:04 +0530 | [diff] [blame] | 881 | * performance with caching more page table entries by a page table walk. |
Sachin Kamat | f171aba | 2014-08-04 10:06:28 +0530 | [diff] [blame] | 882 | * However, the logic has a bug that while caching faulty page table entries, |
| 883 | * System MMU reports page fault if the cached fault entry is hit even though |
| 884 | * the fault entry is updated to a valid entry after the entry is cached. |
| 885 | * To prevent caching faulty page table entries which may be updated to valid |
| 886 | * entries later, the virtual memory manager should care about the workaround |
| 887 | * for the problem. The following describes the workaround. |
Cho KyongHo | 66a7ed8 | 2014-05-12 11:45:04 +0530 | [diff] [blame] | 888 | * |
| 889 | * Any two consecutive I/O virtual address regions must have a hole of 128KiB |
Sachin Kamat | f171aba | 2014-08-04 10:06:28 +0530 | [diff] [blame] | 890 | * at maximum to prevent misbehavior of System MMU 3.x (workaround for h/w bug). |
Cho KyongHo | 66a7ed8 | 2014-05-12 11:45:04 +0530 | [diff] [blame] | 891 | * |
Sachin Kamat | f171aba | 2014-08-04 10:06:28 +0530 | [diff] [blame] | 892 | * Precisely, any start address of I/O virtual region must be aligned with |
Cho KyongHo | 66a7ed8 | 2014-05-12 11:45:04 +0530 | [diff] [blame] | 893 | * the following sizes for System MMU v3.1 and v3.2. |
| 894 | * System MMU v3.1: 128KiB |
| 895 | * System MMU v3.2: 256KiB |
| 896 | * |
| 897 | * Because System MMU v3.3 caches page table entries more aggressively, it needs |
Sachin Kamat | f171aba | 2014-08-04 10:06:28 +0530 | [diff] [blame] | 898 | * more workarounds. |
| 899 | * - Any two consecutive I/O virtual regions must have a hole of size larger |
| 900 | * than or equal to 128KiB. |
Cho KyongHo | 66a7ed8 | 2014-05-12 11:45:04 +0530 | [diff] [blame] | 901 | * - Start address of an I/O virtual region must be aligned by 128KiB. |
| 902 | */ |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 903 | static int exynos_iommu_map(struct iommu_domain *iommu_domain, |
| 904 | unsigned long l_iova, phys_addr_t paddr, size_t size, |
| 905 | int prot) |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 906 | { |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 907 | struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain); |
Cho KyongHo | d09d78f | 2014-05-12 11:44:58 +0530 | [diff] [blame] | 908 | sysmmu_pte_t *entry; |
| 909 | sysmmu_iova_t iova = (sysmmu_iova_t)l_iova; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 910 | unsigned long flags; |
| 911 | int ret = -ENOMEM; |
| 912 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 913 | BUG_ON(domain->pgtable == NULL); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 914 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 915 | spin_lock_irqsave(&domain->pgtablelock, flags); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 916 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 917 | entry = section_entry(domain->pgtable, iova); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 918 | |
| 919 | if (size == SECT_SIZE) { |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 920 | ret = lv1set_section(domain, entry, iova, paddr, |
| 921 | &domain->lv2entcnt[lv1ent_offset(iova)]); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 922 | } else { |
Cho KyongHo | d09d78f | 2014-05-12 11:44:58 +0530 | [diff] [blame] | 923 | sysmmu_pte_t *pent; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 924 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 925 | pent = alloc_lv2entry(domain, entry, iova, |
| 926 | &domain->lv2entcnt[lv1ent_offset(iova)]); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 927 | |
Cho KyongHo | 61128f0 | 2014-05-12 11:44:47 +0530 | [diff] [blame] | 928 | if (IS_ERR(pent)) |
| 929 | ret = PTR_ERR(pent); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 930 | else |
| 931 | ret = lv2set_page(pent, paddr, size, |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 932 | &domain->lv2entcnt[lv1ent_offset(iova)]); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 933 | } |
| 934 | |
Cho KyongHo | 61128f0 | 2014-05-12 11:44:47 +0530 | [diff] [blame] | 935 | if (ret) |
Cho KyongHo | 0bf4e54 | 2014-05-12 11:45:00 +0530 | [diff] [blame] | 936 | pr_err("%s: Failed(%d) to map %#zx bytes @ %#x\n", |
| 937 | __func__, ret, size, iova); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 938 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 939 | spin_unlock_irqrestore(&domain->pgtablelock, flags); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 940 | |
| 941 | return ret; |
| 942 | } |
| 943 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 944 | static void exynos_iommu_tlb_invalidate_entry(struct exynos_iommu_domain *domain, |
| 945 | sysmmu_iova_t iova, size_t size) |
Cho KyongHo | 66a7ed8 | 2014-05-12 11:45:04 +0530 | [diff] [blame] | 946 | { |
Marek Szyprowski | 469aceb | 2015-05-19 15:20:27 +0200 | [diff] [blame] | 947 | struct sysmmu_drvdata *data; |
Cho KyongHo | 66a7ed8 | 2014-05-12 11:45:04 +0530 | [diff] [blame] | 948 | unsigned long flags; |
| 949 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 950 | spin_lock_irqsave(&domain->lock, flags); |
Cho KyongHo | 66a7ed8 | 2014-05-12 11:45:04 +0530 | [diff] [blame] | 951 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 952 | list_for_each_entry(data, &domain->clients, domain_node) |
Marek Szyprowski | 469aceb | 2015-05-19 15:20:27 +0200 | [diff] [blame] | 953 | sysmmu_tlb_invalidate_entry(data, iova, size); |
Cho KyongHo | 66a7ed8 | 2014-05-12 11:45:04 +0530 | [diff] [blame] | 954 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 955 | spin_unlock_irqrestore(&domain->lock, flags); |
Cho KyongHo | 66a7ed8 | 2014-05-12 11:45:04 +0530 | [diff] [blame] | 956 | } |
| 957 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 958 | static size_t exynos_iommu_unmap(struct iommu_domain *iommu_domain, |
| 959 | unsigned long l_iova, size_t size) |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 960 | { |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 961 | struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain); |
Cho KyongHo | d09d78f | 2014-05-12 11:44:58 +0530 | [diff] [blame] | 962 | sysmmu_iova_t iova = (sysmmu_iova_t)l_iova; |
| 963 | sysmmu_pte_t *ent; |
Cho KyongHo | 61128f0 | 2014-05-12 11:44:47 +0530 | [diff] [blame] | 964 | size_t err_pgsize; |
Cho KyongHo | d09d78f | 2014-05-12 11:44:58 +0530 | [diff] [blame] | 965 | unsigned long flags; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 966 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 967 | BUG_ON(domain->pgtable == NULL); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 968 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 969 | spin_lock_irqsave(&domain->pgtablelock, flags); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 970 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 971 | ent = section_entry(domain->pgtable, iova); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 972 | |
| 973 | if (lv1ent_section(ent)) { |
Cho KyongHo | 0bf4e54 | 2014-05-12 11:45:00 +0530 | [diff] [blame] | 974 | if (WARN_ON(size < SECT_SIZE)) { |
Cho KyongHo | 61128f0 | 2014-05-12 11:44:47 +0530 | [diff] [blame] | 975 | err_pgsize = SECT_SIZE; |
| 976 | goto err; |
| 977 | } |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 978 | |
Sachin Kamat | f171aba | 2014-08-04 10:06:28 +0530 | [diff] [blame] | 979 | /* workaround for h/w bug in System MMU v3.3 */ |
| 980 | *ent = ZERO_LV2LINK; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 981 | pgtable_flush(ent, ent + 1); |
| 982 | size = SECT_SIZE; |
| 983 | goto done; |
| 984 | } |
| 985 | |
| 986 | if (unlikely(lv1ent_fault(ent))) { |
| 987 | if (size > SECT_SIZE) |
| 988 | size = SECT_SIZE; |
| 989 | goto done; |
| 990 | } |
| 991 | |
| 992 | /* lv1ent_page(sent) == true here */ |
| 993 | |
| 994 | ent = page_entry(ent, iova); |
| 995 | |
| 996 | if (unlikely(lv2ent_fault(ent))) { |
| 997 | size = SPAGE_SIZE; |
| 998 | goto done; |
| 999 | } |
| 1000 | |
| 1001 | if (lv2ent_small(ent)) { |
| 1002 | *ent = 0; |
| 1003 | size = SPAGE_SIZE; |
Cho KyongHo | 6cb47ed | 2014-05-12 11:44:51 +0530 | [diff] [blame] | 1004 | pgtable_flush(ent, ent + 1); |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 1005 | domain->lv2entcnt[lv1ent_offset(iova)] += 1; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 1006 | goto done; |
| 1007 | } |
| 1008 | |
| 1009 | /* lv1ent_large(ent) == true here */ |
Cho KyongHo | 0bf4e54 | 2014-05-12 11:45:00 +0530 | [diff] [blame] | 1010 | if (WARN_ON(size < LPAGE_SIZE)) { |
Cho KyongHo | 61128f0 | 2014-05-12 11:44:47 +0530 | [diff] [blame] | 1011 | err_pgsize = LPAGE_SIZE; |
| 1012 | goto err; |
| 1013 | } |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 1014 | |
| 1015 | memset(ent, 0, sizeof(*ent) * SPAGES_PER_LPAGE); |
Cho KyongHo | 6cb47ed | 2014-05-12 11:44:51 +0530 | [diff] [blame] | 1016 | pgtable_flush(ent, ent + SPAGES_PER_LPAGE); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 1017 | |
| 1018 | size = LPAGE_SIZE; |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 1019 | domain->lv2entcnt[lv1ent_offset(iova)] += SPAGES_PER_LPAGE; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 1020 | done: |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 1021 | spin_unlock_irqrestore(&domain->pgtablelock, flags); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 1022 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 1023 | exynos_iommu_tlb_invalidate_entry(domain, iova, size); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 1024 | |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 1025 | return size; |
Cho KyongHo | 61128f0 | 2014-05-12 11:44:47 +0530 | [diff] [blame] | 1026 | err: |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 1027 | spin_unlock_irqrestore(&domain->pgtablelock, flags); |
Cho KyongHo | 61128f0 | 2014-05-12 11:44:47 +0530 | [diff] [blame] | 1028 | |
Cho KyongHo | 0bf4e54 | 2014-05-12 11:45:00 +0530 | [diff] [blame] | 1029 | pr_err("%s: Failed: size(%#zx) @ %#x is smaller than page size %#zx\n", |
| 1030 | __func__, size, iova, err_pgsize); |
Cho KyongHo | 61128f0 | 2014-05-12 11:44:47 +0530 | [diff] [blame] | 1031 | |
| 1032 | return 0; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 1033 | } |
| 1034 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 1035 | static phys_addr_t exynos_iommu_iova_to_phys(struct iommu_domain *iommu_domain, |
Varun Sethi | bb5547a | 2013-03-29 01:23:58 +0530 | [diff] [blame] | 1036 | dma_addr_t iova) |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 1037 | { |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 1038 | struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain); |
Cho KyongHo | d09d78f | 2014-05-12 11:44:58 +0530 | [diff] [blame] | 1039 | sysmmu_pte_t *entry; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 1040 | unsigned long flags; |
| 1041 | phys_addr_t phys = 0; |
| 1042 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 1043 | spin_lock_irqsave(&domain->pgtablelock, flags); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 1044 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 1045 | entry = section_entry(domain->pgtable, iova); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 1046 | |
| 1047 | if (lv1ent_section(entry)) { |
| 1048 | phys = section_phys(entry) + section_offs(iova); |
| 1049 | } else if (lv1ent_page(entry)) { |
| 1050 | entry = page_entry(entry, iova); |
| 1051 | |
| 1052 | if (lv2ent_large(entry)) |
| 1053 | phys = lpage_phys(entry) + lpage_offs(iova); |
| 1054 | else if (lv2ent_small(entry)) |
| 1055 | phys = spage_phys(entry) + spage_offs(iova); |
| 1056 | } |
| 1057 | |
Marek Szyprowski | bfa0048 | 2015-05-19 15:20:28 +0200 | [diff] [blame] | 1058 | spin_unlock_irqrestore(&domain->pgtablelock, flags); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 1059 | |
| 1060 | return phys; |
| 1061 | } |
| 1062 | |
Antonios Motakis | bf4a1c9 | 2014-05-12 11:44:59 +0530 | [diff] [blame] | 1063 | static int exynos_iommu_add_device(struct device *dev) |
| 1064 | { |
| 1065 | struct iommu_group *group; |
| 1066 | int ret; |
| 1067 | |
| 1068 | group = iommu_group_get(dev); |
| 1069 | |
| 1070 | if (!group) { |
| 1071 | group = iommu_group_alloc(); |
| 1072 | if (IS_ERR(group)) { |
| 1073 | dev_err(dev, "Failed to allocate IOMMU group\n"); |
| 1074 | return PTR_ERR(group); |
| 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | ret = iommu_group_add_device(group, dev); |
| 1079 | iommu_group_put(group); |
| 1080 | |
| 1081 | return ret; |
| 1082 | } |
| 1083 | |
| 1084 | static void exynos_iommu_remove_device(struct device *dev) |
| 1085 | { |
| 1086 | iommu_group_remove_device(dev); |
| 1087 | } |
| 1088 | |
Thierry Reding | b22f643 | 2014-06-27 09:03:12 +0200 | [diff] [blame] | 1089 | static const struct iommu_ops exynos_iommu_ops = { |
Joerg Roedel | e1fd1ea | 2015-03-26 13:43:11 +0100 | [diff] [blame] | 1090 | .domain_alloc = exynos_iommu_domain_alloc, |
| 1091 | .domain_free = exynos_iommu_domain_free, |
Bjorn Helgaas | ba5fa6f | 2014-05-08 14:49:14 -0600 | [diff] [blame] | 1092 | .attach_dev = exynos_iommu_attach_device, |
| 1093 | .detach_dev = exynos_iommu_detach_device, |
| 1094 | .map = exynos_iommu_map, |
| 1095 | .unmap = exynos_iommu_unmap, |
Olav Haugan | 315786e | 2014-10-25 09:55:16 -0700 | [diff] [blame] | 1096 | .map_sg = default_iommu_map_sg, |
Bjorn Helgaas | ba5fa6f | 2014-05-08 14:49:14 -0600 | [diff] [blame] | 1097 | .iova_to_phys = exynos_iommu_iova_to_phys, |
| 1098 | .add_device = exynos_iommu_add_device, |
| 1099 | .remove_device = exynos_iommu_remove_device, |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 1100 | .pgsize_bitmap = SECT_SIZE | LPAGE_SIZE | SPAGE_SIZE, |
| 1101 | }; |
| 1102 | |
| 1103 | static int __init exynos_iommu_init(void) |
| 1104 | { |
Thierry Reding | a7b67cd | 2015-02-06 11:44:05 +0100 | [diff] [blame] | 1105 | struct device_node *np; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 1106 | int ret; |
| 1107 | |
Thierry Reding | a7b67cd | 2015-02-06 11:44:05 +0100 | [diff] [blame] | 1108 | np = of_find_matching_node(NULL, sysmmu_of_match); |
| 1109 | if (!np) |
| 1110 | return 0; |
| 1111 | |
| 1112 | of_node_put(np); |
| 1113 | |
Cho KyongHo | 734c3c7 | 2014-05-12 11:44:48 +0530 | [diff] [blame] | 1114 | lv2table_kmem_cache = kmem_cache_create("exynos-iommu-lv2table", |
| 1115 | LV2TABLE_SIZE, LV2TABLE_SIZE, 0, NULL); |
| 1116 | if (!lv2table_kmem_cache) { |
| 1117 | pr_err("%s: Failed to create kmem cache\n", __func__); |
| 1118 | return -ENOMEM; |
| 1119 | } |
| 1120 | |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 1121 | ret = platform_driver_register(&exynos_sysmmu_driver); |
Cho KyongHo | 734c3c7 | 2014-05-12 11:44:48 +0530 | [diff] [blame] | 1122 | if (ret) { |
| 1123 | pr_err("%s: Failed to register driver\n", __func__); |
| 1124 | goto err_reg_driver; |
| 1125 | } |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 1126 | |
Cho KyongHo | 66a7ed8 | 2014-05-12 11:45:04 +0530 | [diff] [blame] | 1127 | zero_lv2_table = kmem_cache_zalloc(lv2table_kmem_cache, GFP_KERNEL); |
| 1128 | if (zero_lv2_table == NULL) { |
| 1129 | pr_err("%s: Failed to allocate zero level2 page table\n", |
| 1130 | __func__); |
| 1131 | ret = -ENOMEM; |
| 1132 | goto err_zero_lv2; |
| 1133 | } |
| 1134 | |
Cho KyongHo | 734c3c7 | 2014-05-12 11:44:48 +0530 | [diff] [blame] | 1135 | ret = bus_set_iommu(&platform_bus_type, &exynos_iommu_ops); |
| 1136 | if (ret) { |
| 1137 | pr_err("%s: Failed to register exynos-iommu driver.\n", |
| 1138 | __func__); |
| 1139 | goto err_set_iommu; |
| 1140 | } |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 1141 | |
Cho KyongHo | 734c3c7 | 2014-05-12 11:44:48 +0530 | [diff] [blame] | 1142 | return 0; |
| 1143 | err_set_iommu: |
Cho KyongHo | 66a7ed8 | 2014-05-12 11:45:04 +0530 | [diff] [blame] | 1144 | kmem_cache_free(lv2table_kmem_cache, zero_lv2_table); |
| 1145 | err_zero_lv2: |
Cho KyongHo | 734c3c7 | 2014-05-12 11:44:48 +0530 | [diff] [blame] | 1146 | platform_driver_unregister(&exynos_sysmmu_driver); |
| 1147 | err_reg_driver: |
| 1148 | kmem_cache_destroy(lv2table_kmem_cache); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 1149 | return ret; |
| 1150 | } |
| 1151 | subsys_initcall(exynos_iommu_init); |