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 | |
| 15 | #include <linux/io.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/pm_runtime.h> |
| 20 | #include <linux/clk.h> |
| 21 | #include <linux/err.h> |
| 22 | #include <linux/mm.h> |
| 23 | #include <linux/iommu.h> |
| 24 | #include <linux/errno.h> |
| 25 | #include <linux/list.h> |
| 26 | #include <linux/memblock.h> |
| 27 | #include <linux/export.h> |
| 28 | |
| 29 | #include <asm/cacheflush.h> |
| 30 | #include <asm/pgtable.h> |
| 31 | |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 32 | /* We does not consider super section mapping (16MB) */ |
| 33 | #define SECT_ORDER 20 |
| 34 | #define LPAGE_ORDER 16 |
| 35 | #define SPAGE_ORDER 12 |
| 36 | |
| 37 | #define SECT_SIZE (1 << SECT_ORDER) |
| 38 | #define LPAGE_SIZE (1 << LPAGE_ORDER) |
| 39 | #define SPAGE_SIZE (1 << SPAGE_ORDER) |
| 40 | |
| 41 | #define SECT_MASK (~(SECT_SIZE - 1)) |
| 42 | #define LPAGE_MASK (~(LPAGE_SIZE - 1)) |
| 43 | #define SPAGE_MASK (~(SPAGE_SIZE - 1)) |
| 44 | |
| 45 | #define lv1ent_fault(sent) (((*(sent) & 3) == 0) || ((*(sent) & 3) == 3)) |
| 46 | #define lv1ent_page(sent) ((*(sent) & 3) == 1) |
| 47 | #define lv1ent_section(sent) ((*(sent) & 3) == 2) |
| 48 | |
| 49 | #define lv2ent_fault(pent) ((*(pent) & 3) == 0) |
| 50 | #define lv2ent_small(pent) ((*(pent) & 2) == 2) |
| 51 | #define lv2ent_large(pent) ((*(pent) & 3) == 1) |
| 52 | |
| 53 | #define section_phys(sent) (*(sent) & SECT_MASK) |
| 54 | #define section_offs(iova) ((iova) & 0xFFFFF) |
| 55 | #define lpage_phys(pent) (*(pent) & LPAGE_MASK) |
| 56 | #define lpage_offs(iova) ((iova) & 0xFFFF) |
| 57 | #define spage_phys(pent) (*(pent) & SPAGE_MASK) |
| 58 | #define spage_offs(iova) ((iova) & 0xFFF) |
| 59 | |
| 60 | #define lv1ent_offset(iova) ((iova) >> SECT_ORDER) |
| 61 | #define lv2ent_offset(iova) (((iova) & 0xFF000) >> SPAGE_ORDER) |
| 62 | |
| 63 | #define NUM_LV1ENTRIES 4096 |
| 64 | #define NUM_LV2ENTRIES 256 |
| 65 | |
| 66 | #define LV2TABLE_SIZE (NUM_LV2ENTRIES * sizeof(long)) |
| 67 | |
| 68 | #define SPAGES_PER_LPAGE (LPAGE_SIZE / SPAGE_SIZE) |
| 69 | |
| 70 | #define lv2table_base(sent) (*(sent) & 0xFFFFFC00) |
| 71 | |
| 72 | #define mk_lv1ent_sect(pa) ((pa) | 2) |
| 73 | #define mk_lv1ent_page(pa) ((pa) | 1) |
| 74 | #define mk_lv2ent_lpage(pa) ((pa) | 1) |
| 75 | #define mk_lv2ent_spage(pa) ((pa) | 2) |
| 76 | |
| 77 | #define CTRL_ENABLE 0x5 |
| 78 | #define CTRL_BLOCK 0x7 |
| 79 | #define CTRL_DISABLE 0x0 |
| 80 | |
| 81 | #define REG_MMU_CTRL 0x000 |
| 82 | #define REG_MMU_CFG 0x004 |
| 83 | #define REG_MMU_STATUS 0x008 |
| 84 | #define REG_MMU_FLUSH 0x00C |
| 85 | #define REG_MMU_FLUSH_ENTRY 0x010 |
| 86 | #define REG_PT_BASE_ADDR 0x014 |
| 87 | #define REG_INT_STATUS 0x018 |
| 88 | #define REG_INT_CLEAR 0x01C |
| 89 | |
| 90 | #define REG_PAGE_FAULT_ADDR 0x024 |
| 91 | #define REG_AW_FAULT_ADDR 0x028 |
| 92 | #define REG_AR_FAULT_ADDR 0x02C |
| 93 | #define REG_DEFAULT_SLAVE_ADDR 0x030 |
| 94 | |
| 95 | #define REG_MMU_VERSION 0x034 |
| 96 | |
| 97 | #define REG_PB0_SADDR 0x04C |
| 98 | #define REG_PB0_EADDR 0x050 |
| 99 | #define REG_PB1_SADDR 0x054 |
| 100 | #define REG_PB1_EADDR 0x058 |
| 101 | |
Cho KyongHo | 734c3c7 | 2014-05-12 11:44:48 +0530 | [diff] [blame] | 102 | static struct kmem_cache *lv2table_kmem_cache; |
| 103 | |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 104 | static unsigned long *section_entry(unsigned long *pgtable, unsigned long iova) |
| 105 | { |
| 106 | return pgtable + lv1ent_offset(iova); |
| 107 | } |
| 108 | |
| 109 | static unsigned long *page_entry(unsigned long *sent, unsigned long iova) |
| 110 | { |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 111 | return (unsigned long *)phys_to_virt( |
| 112 | lv2table_base(sent)) + lv2ent_offset(iova); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | enum exynos_sysmmu_inttype { |
| 116 | SYSMMU_PAGEFAULT, |
| 117 | SYSMMU_AR_MULTIHIT, |
| 118 | SYSMMU_AW_MULTIHIT, |
| 119 | SYSMMU_BUSERROR, |
| 120 | SYSMMU_AR_SECURITY, |
| 121 | SYSMMU_AR_ACCESS, |
| 122 | SYSMMU_AW_SECURITY, |
| 123 | SYSMMU_AW_PROTECTION, /* 7 */ |
| 124 | SYSMMU_FAULT_UNKNOWN, |
| 125 | SYSMMU_FAULTS_NUM |
| 126 | }; |
| 127 | |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 128 | static unsigned short fault_reg_offset[SYSMMU_FAULTS_NUM] = { |
| 129 | REG_PAGE_FAULT_ADDR, |
| 130 | REG_AR_FAULT_ADDR, |
| 131 | REG_AW_FAULT_ADDR, |
| 132 | REG_DEFAULT_SLAVE_ADDR, |
| 133 | REG_AR_FAULT_ADDR, |
| 134 | REG_AR_FAULT_ADDR, |
| 135 | REG_AW_FAULT_ADDR, |
| 136 | REG_AW_FAULT_ADDR |
| 137 | }; |
| 138 | |
| 139 | static char *sysmmu_fault_name[SYSMMU_FAULTS_NUM] = { |
| 140 | "PAGE FAULT", |
| 141 | "AR MULTI-HIT FAULT", |
| 142 | "AW MULTI-HIT FAULT", |
| 143 | "BUS ERROR", |
| 144 | "AR SECURITY PROTECTION FAULT", |
| 145 | "AR ACCESS PROTECTION FAULT", |
| 146 | "AW SECURITY PROTECTION FAULT", |
| 147 | "AW ACCESS PROTECTION FAULT", |
| 148 | "UNKNOWN FAULT" |
| 149 | }; |
| 150 | |
| 151 | struct exynos_iommu_domain { |
| 152 | struct list_head clients; /* list of sysmmu_drvdata.node */ |
| 153 | unsigned long *pgtable; /* lv1 page table, 16KB */ |
| 154 | short *lv2entcnt; /* free lv2 entry counter for each section */ |
| 155 | spinlock_t lock; /* lock for this structure */ |
| 156 | spinlock_t pgtablelock; /* lock for modifying page table @ pgtable */ |
| 157 | }; |
| 158 | |
| 159 | struct sysmmu_drvdata { |
| 160 | struct list_head node; /* entry of exynos_iommu_domain.clients */ |
| 161 | struct device *sysmmu; /* System MMU's device descriptor */ |
| 162 | struct device *dev; /* Owner of system MMU */ |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 163 | void __iomem *sfrbase; |
| 164 | struct clk *clk; |
Cho KyongHo | 7060587 | 2014-05-12 11:44:55 +0530 | [diff] [blame] | 165 | struct clk *clk_master; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 166 | int activations; |
| 167 | rwlock_t lock; |
| 168 | struct iommu_domain *domain; |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 169 | phys_addr_t pgtable; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | static bool set_sysmmu_active(struct sysmmu_drvdata *data) |
| 173 | { |
| 174 | /* return true if the System MMU was not active previously |
| 175 | and it needs to be initialized */ |
| 176 | return ++data->activations == 1; |
| 177 | } |
| 178 | |
| 179 | static bool set_sysmmu_inactive(struct sysmmu_drvdata *data) |
| 180 | { |
| 181 | /* return true if the System MMU is needed to be disabled */ |
| 182 | BUG_ON(data->activations < 1); |
| 183 | return --data->activations == 0; |
| 184 | } |
| 185 | |
| 186 | static bool is_sysmmu_active(struct sysmmu_drvdata *data) |
| 187 | { |
| 188 | return data->activations > 0; |
| 189 | } |
| 190 | |
| 191 | static void sysmmu_unblock(void __iomem *sfrbase) |
| 192 | { |
| 193 | __raw_writel(CTRL_ENABLE, sfrbase + REG_MMU_CTRL); |
| 194 | } |
| 195 | |
| 196 | static bool sysmmu_block(void __iomem *sfrbase) |
| 197 | { |
| 198 | int i = 120; |
| 199 | |
| 200 | __raw_writel(CTRL_BLOCK, sfrbase + REG_MMU_CTRL); |
| 201 | while ((i > 0) && !(__raw_readl(sfrbase + REG_MMU_STATUS) & 1)) |
| 202 | --i; |
| 203 | |
| 204 | if (!(__raw_readl(sfrbase + REG_MMU_STATUS) & 1)) { |
| 205 | sysmmu_unblock(sfrbase); |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | return true; |
| 210 | } |
| 211 | |
| 212 | static void __sysmmu_tlb_invalidate(void __iomem *sfrbase) |
| 213 | { |
| 214 | __raw_writel(0x1, sfrbase + REG_MMU_FLUSH); |
| 215 | } |
| 216 | |
| 217 | static void __sysmmu_tlb_invalidate_entry(void __iomem *sfrbase, |
Cho KyongHo | 3ad6b7f | 2014-05-12 11:44:49 +0530 | [diff] [blame] | 218 | unsigned long iova, unsigned int num_inv) |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 219 | { |
Cho KyongHo | 3ad6b7f | 2014-05-12 11:44:49 +0530 | [diff] [blame] | 220 | unsigned int i; |
| 221 | for (i = 0; i < num_inv; i++) { |
| 222 | __raw_writel((iova & SPAGE_MASK) | 1, |
| 223 | sfrbase + REG_MMU_FLUSH_ENTRY); |
| 224 | iova += SPAGE_SIZE; |
| 225 | } |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | static void __sysmmu_set_ptbase(void __iomem *sfrbase, |
| 229 | unsigned long pgd) |
| 230 | { |
| 231 | __raw_writel(0x1, sfrbase + REG_MMU_CFG); /* 16KB LV1, LRU */ |
| 232 | __raw_writel(pgd, sfrbase + REG_PT_BASE_ADDR); |
| 233 | |
| 234 | __sysmmu_tlb_invalidate(sfrbase); |
| 235 | } |
| 236 | |
Cho KyongHo | 1fab7fa | 2014-05-12 11:44:56 +0530 | [diff] [blame^] | 237 | static void show_fault_information(const char *name, |
| 238 | enum exynos_sysmmu_inttype itype, |
| 239 | phys_addr_t pgtable_base, unsigned long fault_addr) |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 240 | { |
| 241 | unsigned long *ent; |
| 242 | |
| 243 | if ((itype >= SYSMMU_FAULTS_NUM) || (itype < SYSMMU_PAGEFAULT)) |
| 244 | itype = SYSMMU_FAULT_UNKNOWN; |
| 245 | |
Cho KyongHo | 1fab7fa | 2014-05-12 11:44:56 +0530 | [diff] [blame^] | 246 | pr_err("%s occurred at %#lx by %s(Page table base: %pa)\n", |
| 247 | sysmmu_fault_name[itype], fault_addr, name, &pgtable_base); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 248 | |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 249 | ent = section_entry(phys_to_virt(pgtable_base), fault_addr); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 250 | pr_err("\tLv1 entry: 0x%lx\n", *ent); |
| 251 | |
| 252 | if (lv1ent_page(ent)) { |
| 253 | ent = page_entry(ent, fault_addr); |
| 254 | pr_err("\t Lv2 entry: 0x%lx\n", *ent); |
| 255 | } |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | static irqreturn_t exynos_sysmmu_irq(int irq, void *dev_id) |
| 259 | { |
| 260 | /* SYSMMU is in blocked when interrupt occurred. */ |
| 261 | struct sysmmu_drvdata *data = dev_id; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 262 | enum exynos_sysmmu_inttype itype; |
| 263 | unsigned long addr = -1; |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 264 | int ret = -ENOSYS; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 265 | |
| 266 | read_lock(&data->lock); |
| 267 | |
| 268 | WARN_ON(!is_sysmmu_active(data)); |
| 269 | |
Cho KyongHo | 7060587 | 2014-05-12 11:44:55 +0530 | [diff] [blame] | 270 | if (!IS_ERR(data->clk_master)) |
| 271 | clk_enable(data->clk_master); |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 272 | itype = (enum exynos_sysmmu_inttype) |
| 273 | __ffs(__raw_readl(data->sfrbase + REG_INT_STATUS)); |
| 274 | if (WARN_ON(!((itype >= 0) && (itype < SYSMMU_FAULT_UNKNOWN)))) |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 275 | itype = SYSMMU_FAULT_UNKNOWN; |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 276 | else |
| 277 | addr = __raw_readl(data->sfrbase + fault_reg_offset[itype]); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 278 | |
Cho KyongHo | 1fab7fa | 2014-05-12 11:44:56 +0530 | [diff] [blame^] | 279 | if (itype == SYSMMU_FAULT_UNKNOWN) { |
| 280 | pr_err("%s: Fault is not occurred by System MMU '%s'!\n", |
| 281 | __func__, dev_name(data->sysmmu)); |
| 282 | pr_err("%s: Please check if IRQ is correctly configured.\n", |
| 283 | __func__); |
| 284 | BUG(); |
| 285 | } else { |
| 286 | unsigned long base = |
| 287 | __raw_readl(data->sfrbase + REG_PT_BASE_ADDR); |
| 288 | show_fault_information(dev_name(data->sysmmu), |
| 289 | itype, base, addr); |
| 290 | if (data->domain) |
| 291 | ret = report_iommu_fault(data->domain, |
| 292 | data->dev, addr, itype); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 293 | } |
| 294 | |
Cho KyongHo | 1fab7fa | 2014-05-12 11:44:56 +0530 | [diff] [blame^] | 295 | /* fault is not recovered by fault handler */ |
| 296 | BUG_ON(ret != 0); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 297 | |
Cho KyongHo | 1fab7fa | 2014-05-12 11:44:56 +0530 | [diff] [blame^] | 298 | __raw_writel(1 << itype, data->sfrbase + REG_INT_CLEAR); |
| 299 | |
| 300 | sysmmu_unblock(data->sfrbase); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 301 | |
Cho KyongHo | 7060587 | 2014-05-12 11:44:55 +0530 | [diff] [blame] | 302 | if (!IS_ERR(data->clk_master)) |
| 303 | clk_disable(data->clk_master); |
| 304 | |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 305 | read_unlock(&data->lock); |
| 306 | |
| 307 | return IRQ_HANDLED; |
| 308 | } |
| 309 | |
| 310 | static bool __exynos_sysmmu_disable(struct sysmmu_drvdata *data) |
| 311 | { |
| 312 | unsigned long flags; |
| 313 | bool disabled = false; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 314 | |
| 315 | write_lock_irqsave(&data->lock, flags); |
| 316 | |
| 317 | if (!set_sysmmu_inactive(data)) |
| 318 | goto finish; |
| 319 | |
Cho KyongHo | 7060587 | 2014-05-12 11:44:55 +0530 | [diff] [blame] | 320 | if (!IS_ERR(data->clk_master)) |
| 321 | clk_enable(data->clk_master); |
| 322 | |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 323 | __raw_writel(CTRL_DISABLE, data->sfrbase + REG_MMU_CTRL); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 324 | |
Cho KyongHo | 46c16d1 | 2014-05-12 11:44:54 +0530 | [diff] [blame] | 325 | clk_disable(data->clk); |
Cho KyongHo | 7060587 | 2014-05-12 11:44:55 +0530 | [diff] [blame] | 326 | if (!IS_ERR(data->clk_master)) |
| 327 | clk_disable(data->clk_master); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 328 | |
| 329 | disabled = true; |
| 330 | data->pgtable = 0; |
| 331 | data->domain = NULL; |
| 332 | finish: |
| 333 | write_unlock_irqrestore(&data->lock, flags); |
| 334 | |
| 335 | if (disabled) |
Cho KyongHo | e5cf63c | 2014-05-12 11:44:53 +0530 | [diff] [blame] | 336 | dev_dbg(data->sysmmu, "Disabled\n"); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 337 | else |
Cho KyongHo | e5cf63c | 2014-05-12 11:44:53 +0530 | [diff] [blame] | 338 | dev_dbg(data->sysmmu, "%d times left to be disabled\n", |
| 339 | data->activations); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 340 | |
| 341 | return disabled; |
| 342 | } |
| 343 | |
| 344 | /* __exynos_sysmmu_enable: Enables System MMU |
| 345 | * |
| 346 | * returns -error if an error occurred and System MMU is not enabled, |
| 347 | * 0 if the System MMU has been just enabled and 1 if System MMU was already |
| 348 | * enabled before. |
| 349 | */ |
| 350 | static int __exynos_sysmmu_enable(struct sysmmu_drvdata *data, |
| 351 | unsigned long pgtable, struct iommu_domain *domain) |
| 352 | { |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 353 | int ret = 0; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 354 | unsigned long flags; |
| 355 | |
| 356 | write_lock_irqsave(&data->lock, flags); |
| 357 | |
| 358 | if (!set_sysmmu_active(data)) { |
| 359 | if (WARN_ON(pgtable != data->pgtable)) { |
| 360 | ret = -EBUSY; |
| 361 | set_sysmmu_inactive(data); |
| 362 | } else { |
| 363 | ret = 1; |
| 364 | } |
| 365 | |
Cho KyongHo | e5cf63c | 2014-05-12 11:44:53 +0530 | [diff] [blame] | 366 | dev_dbg(data->sysmmu, "Already enabled\n"); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 367 | goto finish; |
| 368 | } |
| 369 | |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 370 | data->pgtable = pgtable; |
| 371 | |
Cho KyongHo | 7060587 | 2014-05-12 11:44:55 +0530 | [diff] [blame] | 372 | if (!IS_ERR(data->clk_master)) |
| 373 | clk_enable(data->clk_master); |
| 374 | clk_enable(data->clk); |
| 375 | |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 376 | __sysmmu_set_ptbase(data->sfrbase, pgtable); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 377 | |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 378 | __raw_writel(CTRL_ENABLE, data->sfrbase + REG_MMU_CTRL); |
| 379 | |
Cho KyongHo | 7060587 | 2014-05-12 11:44:55 +0530 | [diff] [blame] | 380 | if (!IS_ERR(data->clk_master)) |
| 381 | clk_disable(data->clk_master); |
| 382 | |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 383 | data->domain = domain; |
| 384 | |
Cho KyongHo | e5cf63c | 2014-05-12 11:44:53 +0530 | [diff] [blame] | 385 | dev_dbg(data->sysmmu, "Enabled\n"); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 386 | finish: |
| 387 | write_unlock_irqrestore(&data->lock, flags); |
| 388 | |
| 389 | return ret; |
| 390 | } |
| 391 | |
| 392 | int exynos_sysmmu_enable(struct device *dev, unsigned long pgtable) |
| 393 | { |
| 394 | struct sysmmu_drvdata *data = dev_get_drvdata(dev->archdata.iommu); |
| 395 | int ret; |
| 396 | |
| 397 | BUG_ON(!memblock_is_memory(pgtable)); |
| 398 | |
| 399 | ret = pm_runtime_get_sync(data->sysmmu); |
| 400 | if (ret < 0) { |
Cho KyongHo | e5cf63c | 2014-05-12 11:44:53 +0530 | [diff] [blame] | 401 | dev_dbg(data->sysmmu, "Failed to enable\n"); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 402 | return ret; |
| 403 | } |
| 404 | |
| 405 | ret = __exynos_sysmmu_enable(data, pgtable, NULL); |
| 406 | if (WARN_ON(ret < 0)) { |
| 407 | pm_runtime_put(data->sysmmu); |
Cho KyongHo | e5cf63c | 2014-05-12 11:44:53 +0530 | [diff] [blame] | 408 | dev_err(data->sysmmu, "Already enabled with page table %#x\n", |
| 409 | data->pgtable); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 410 | } else { |
| 411 | data->dev = dev; |
| 412 | } |
| 413 | |
| 414 | return ret; |
| 415 | } |
| 416 | |
Sachin Kamat | 77e3835 | 2013-02-06 13:55:17 +0530 | [diff] [blame] | 417 | static bool exynos_sysmmu_disable(struct device *dev) |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 418 | { |
| 419 | struct sysmmu_drvdata *data = dev_get_drvdata(dev->archdata.iommu); |
| 420 | bool disabled; |
| 421 | |
| 422 | disabled = __exynos_sysmmu_disable(data); |
| 423 | pm_runtime_put(data->sysmmu); |
| 424 | |
| 425 | return disabled; |
| 426 | } |
| 427 | |
Cho KyongHo | 3ad6b7f | 2014-05-12 11:44:49 +0530 | [diff] [blame] | 428 | static void sysmmu_tlb_invalidate_entry(struct device *dev, unsigned long iova, |
| 429 | size_t size) |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 430 | { |
| 431 | unsigned long flags; |
| 432 | struct sysmmu_drvdata *data = dev_get_drvdata(dev->archdata.iommu); |
| 433 | |
| 434 | read_lock_irqsave(&data->lock, flags); |
| 435 | |
| 436 | if (is_sysmmu_active(data)) { |
Cho KyongHo | 3ad6b7f | 2014-05-12 11:44:49 +0530 | [diff] [blame] | 437 | unsigned int maj; |
| 438 | unsigned int num_inv = 1; |
Cho KyongHo | 7060587 | 2014-05-12 11:44:55 +0530 | [diff] [blame] | 439 | |
| 440 | if (!IS_ERR(data->clk_master)) |
| 441 | clk_enable(data->clk_master); |
| 442 | |
Cho KyongHo | 3ad6b7f | 2014-05-12 11:44:49 +0530 | [diff] [blame] | 443 | maj = __raw_readl(data->sfrbase + REG_MMU_VERSION); |
| 444 | /* |
| 445 | * L2TLB invalidation required |
| 446 | * 4KB page: 1 invalidation |
| 447 | * 64KB page: 16 invalidation |
| 448 | * 1MB page: 64 invalidation |
| 449 | * because it is set-associative TLB |
| 450 | * with 8-way and 64 sets. |
| 451 | * 1MB page can be cached in one of all sets. |
| 452 | * 64KB page can be one of 16 consecutive sets. |
| 453 | */ |
| 454 | if ((maj >> 28) == 2) /* major version number */ |
| 455 | num_inv = min_t(unsigned int, size / PAGE_SIZE, 64); |
| 456 | |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 457 | if (sysmmu_block(data->sfrbase)) { |
| 458 | __sysmmu_tlb_invalidate_entry( |
Cho KyongHo | 3ad6b7f | 2014-05-12 11:44:49 +0530 | [diff] [blame] | 459 | data->sfrbase, iova, num_inv); |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 460 | sysmmu_unblock(data->sfrbase); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 461 | } |
Cho KyongHo | 7060587 | 2014-05-12 11:44:55 +0530 | [diff] [blame] | 462 | if (!IS_ERR(data->clk_master)) |
| 463 | clk_disable(data->clk_master); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 464 | } else { |
Cho KyongHo | e5cf63c | 2014-05-12 11:44:53 +0530 | [diff] [blame] | 465 | dev_dbg(data->sysmmu, "Disabled. Skipping invalidating TLB.\n"); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | read_unlock_irqrestore(&data->lock, flags); |
| 469 | } |
| 470 | |
| 471 | void exynos_sysmmu_tlb_invalidate(struct device *dev) |
| 472 | { |
| 473 | unsigned long flags; |
| 474 | struct sysmmu_drvdata *data = dev_get_drvdata(dev->archdata.iommu); |
| 475 | |
| 476 | read_lock_irqsave(&data->lock, flags); |
| 477 | |
| 478 | if (is_sysmmu_active(data)) { |
Cho KyongHo | 7060587 | 2014-05-12 11:44:55 +0530 | [diff] [blame] | 479 | if (!IS_ERR(data->clk_master)) |
| 480 | clk_enable(data->clk_master); |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 481 | if (sysmmu_block(data->sfrbase)) { |
| 482 | __sysmmu_tlb_invalidate(data->sfrbase); |
| 483 | sysmmu_unblock(data->sfrbase); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 484 | } |
Cho KyongHo | 7060587 | 2014-05-12 11:44:55 +0530 | [diff] [blame] | 485 | if (!IS_ERR(data->clk_master)) |
| 486 | clk_disable(data->clk_master); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 487 | } else { |
Cho KyongHo | e5cf63c | 2014-05-12 11:44:53 +0530 | [diff] [blame] | 488 | dev_dbg(data->sysmmu, "Disabled. Skipping invalidating TLB.\n"); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | read_unlock_irqrestore(&data->lock, flags); |
| 492 | } |
| 493 | |
| 494 | static int exynos_sysmmu_probe(struct platform_device *pdev) |
| 495 | { |
Cho KyongHo | 46c16d1 | 2014-05-12 11:44:54 +0530 | [diff] [blame] | 496 | int irq, ret; |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 497 | struct device *dev = &pdev->dev; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 498 | struct sysmmu_drvdata *data; |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 499 | struct resource *res; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 500 | |
Cho KyongHo | 46c16d1 | 2014-05-12 11:44:54 +0530 | [diff] [blame] | 501 | data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); |
| 502 | if (!data) |
| 503 | return -ENOMEM; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 504 | |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 505 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Cho KyongHo | 46c16d1 | 2014-05-12 11:44:54 +0530 | [diff] [blame] | 506 | data->sfrbase = devm_ioremap_resource(dev, res); |
| 507 | if (IS_ERR(data->sfrbase)) |
| 508 | return PTR_ERR(data->sfrbase); |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 509 | |
Cho KyongHo | 46c16d1 | 2014-05-12 11:44:54 +0530 | [diff] [blame] | 510 | irq = platform_get_irq(pdev, 0); |
| 511 | if (irq <= 0) { |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 512 | dev_dbg(dev, "Unable to find IRQ resource\n"); |
Cho KyongHo | 46c16d1 | 2014-05-12 11:44:54 +0530 | [diff] [blame] | 513 | return irq; |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 514 | } |
| 515 | |
Cho KyongHo | 46c16d1 | 2014-05-12 11:44:54 +0530 | [diff] [blame] | 516 | ret = devm_request_irq(dev, irq, exynos_sysmmu_irq, 0, |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 517 | dev_name(dev), data); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 518 | if (ret) { |
Cho KyongHo | 46c16d1 | 2014-05-12 11:44:54 +0530 | [diff] [blame] | 519 | dev_err(dev, "Unabled to register handler of irq %d\n", irq); |
| 520 | return ret; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 521 | } |
| 522 | |
Cho KyongHo | 46c16d1 | 2014-05-12 11:44:54 +0530 | [diff] [blame] | 523 | data->clk = devm_clk_get(dev, "sysmmu"); |
| 524 | if (IS_ERR(data->clk)) { |
| 525 | dev_err(dev, "Failed to get clock!\n"); |
| 526 | return PTR_ERR(data->clk); |
| 527 | } else { |
| 528 | ret = clk_prepare(data->clk); |
| 529 | if (ret) { |
| 530 | dev_err(dev, "Failed to prepare clk\n"); |
| 531 | return ret; |
| 532 | } |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 533 | } |
| 534 | |
Cho KyongHo | 7060587 | 2014-05-12 11:44:55 +0530 | [diff] [blame] | 535 | data->clk_master = devm_clk_get(dev, "master"); |
| 536 | if (!IS_ERR(data->clk_master)) { |
| 537 | ret = clk_prepare(data->clk_master); |
| 538 | if (ret) { |
| 539 | clk_unprepare(data->clk); |
| 540 | dev_err(dev, "Failed to prepare master's clk\n"); |
| 541 | return ret; |
| 542 | } |
| 543 | } |
| 544 | |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 545 | data->sysmmu = dev; |
| 546 | rwlock_init(&data->lock); |
| 547 | INIT_LIST_HEAD(&data->node); |
| 548 | |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 549 | platform_set_drvdata(pdev, data); |
| 550 | |
Cho KyongHo | f4723ec | 2014-05-12 11:44:52 +0530 | [diff] [blame] | 551 | pm_runtime_enable(dev); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 552 | |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 553 | return 0; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | static struct platform_driver exynos_sysmmu_driver = { |
| 557 | .probe = exynos_sysmmu_probe, |
| 558 | .driver = { |
| 559 | .owner = THIS_MODULE, |
| 560 | .name = "exynos-sysmmu", |
| 561 | } |
| 562 | }; |
| 563 | |
| 564 | static inline void pgtable_flush(void *vastart, void *vaend) |
| 565 | { |
| 566 | dmac_flush_range(vastart, vaend); |
| 567 | outer_flush_range(virt_to_phys(vastart), |
| 568 | virt_to_phys(vaend)); |
| 569 | } |
| 570 | |
| 571 | static int exynos_iommu_domain_init(struct iommu_domain *domain) |
| 572 | { |
| 573 | struct exynos_iommu_domain *priv; |
| 574 | |
| 575 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 576 | if (!priv) |
| 577 | return -ENOMEM; |
| 578 | |
| 579 | priv->pgtable = (unsigned long *)__get_free_pages( |
| 580 | GFP_KERNEL | __GFP_ZERO, 2); |
| 581 | if (!priv->pgtable) |
| 582 | goto err_pgtable; |
| 583 | |
| 584 | priv->lv2entcnt = (short *)__get_free_pages( |
| 585 | GFP_KERNEL | __GFP_ZERO, 1); |
| 586 | if (!priv->lv2entcnt) |
| 587 | goto err_counter; |
| 588 | |
| 589 | pgtable_flush(priv->pgtable, priv->pgtable + NUM_LV1ENTRIES); |
| 590 | |
| 591 | spin_lock_init(&priv->lock); |
| 592 | spin_lock_init(&priv->pgtablelock); |
| 593 | INIT_LIST_HEAD(&priv->clients); |
| 594 | |
Sachin Kamat | eb51637 | 2012-08-01 14:35:17 +0530 | [diff] [blame] | 595 | domain->geometry.aperture_start = 0; |
| 596 | domain->geometry.aperture_end = ~0UL; |
| 597 | domain->geometry.force_aperture = true; |
Joerg Roedel | 3177bb7 | 2012-07-11 12:41:10 +0200 | [diff] [blame] | 598 | |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 599 | domain->priv = priv; |
| 600 | return 0; |
| 601 | |
| 602 | err_counter: |
| 603 | free_pages((unsigned long)priv->pgtable, 2); |
| 604 | err_pgtable: |
| 605 | kfree(priv); |
| 606 | return -ENOMEM; |
| 607 | } |
| 608 | |
| 609 | static void exynos_iommu_domain_destroy(struct iommu_domain *domain) |
| 610 | { |
| 611 | struct exynos_iommu_domain *priv = domain->priv; |
| 612 | struct sysmmu_drvdata *data; |
| 613 | unsigned long flags; |
| 614 | int i; |
| 615 | |
| 616 | WARN_ON(!list_empty(&priv->clients)); |
| 617 | |
| 618 | spin_lock_irqsave(&priv->lock, flags); |
| 619 | |
| 620 | list_for_each_entry(data, &priv->clients, node) { |
| 621 | while (!exynos_sysmmu_disable(data->dev)) |
| 622 | ; /* until System MMU is actually disabled */ |
| 623 | } |
| 624 | |
| 625 | spin_unlock_irqrestore(&priv->lock, flags); |
| 626 | |
| 627 | for (i = 0; i < NUM_LV1ENTRIES; i++) |
| 628 | if (lv1ent_page(priv->pgtable + i)) |
Cho KyongHo | 734c3c7 | 2014-05-12 11:44:48 +0530 | [diff] [blame] | 629 | kmem_cache_free(lv2table_kmem_cache, |
| 630 | phys_to_virt(lv2table_base(priv->pgtable + i))); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 631 | |
| 632 | free_pages((unsigned long)priv->pgtable, 2); |
| 633 | free_pages((unsigned long)priv->lv2entcnt, 1); |
| 634 | kfree(domain->priv); |
| 635 | domain->priv = NULL; |
| 636 | } |
| 637 | |
| 638 | static int exynos_iommu_attach_device(struct iommu_domain *domain, |
| 639 | struct device *dev) |
| 640 | { |
| 641 | struct sysmmu_drvdata *data = dev_get_drvdata(dev->archdata.iommu); |
| 642 | struct exynos_iommu_domain *priv = domain->priv; |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 643 | phys_addr_t pagetable = virt_to_phys(priv->pgtable); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 644 | unsigned long flags; |
| 645 | int ret; |
| 646 | |
| 647 | ret = pm_runtime_get_sync(data->sysmmu); |
| 648 | if (ret < 0) |
| 649 | return ret; |
| 650 | |
| 651 | ret = 0; |
| 652 | |
| 653 | spin_lock_irqsave(&priv->lock, flags); |
| 654 | |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 655 | ret = __exynos_sysmmu_enable(data, pagetable, domain); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 656 | |
| 657 | if (ret == 0) { |
| 658 | /* 'data->node' must not be appeared in priv->clients */ |
| 659 | BUG_ON(!list_empty(&data->node)); |
| 660 | data->dev = dev; |
| 661 | list_add_tail(&data->node, &priv->clients); |
| 662 | } |
| 663 | |
| 664 | spin_unlock_irqrestore(&priv->lock, flags); |
| 665 | |
| 666 | if (ret < 0) { |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 667 | dev_err(dev, "%s: Failed to attach IOMMU with pgtable %pa\n", |
| 668 | __func__, &pagetable); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 669 | pm_runtime_put(data->sysmmu); |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 670 | return ret; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 671 | } |
| 672 | |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 673 | dev_dbg(dev, "%s: Attached IOMMU with pgtable %pa %s\n", |
| 674 | __func__, &pagetable, (ret == 0) ? "" : ", again"); |
| 675 | |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 676 | return ret; |
| 677 | } |
| 678 | |
| 679 | static void exynos_iommu_detach_device(struct iommu_domain *domain, |
| 680 | struct device *dev) |
| 681 | { |
| 682 | struct sysmmu_drvdata *data = dev_get_drvdata(dev->archdata.iommu); |
| 683 | struct exynos_iommu_domain *priv = domain->priv; |
| 684 | struct list_head *pos; |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 685 | phys_addr_t pagetable = virt_to_phys(priv->pgtable); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 686 | unsigned long flags; |
| 687 | bool found = false; |
| 688 | |
| 689 | spin_lock_irqsave(&priv->lock, flags); |
| 690 | |
| 691 | list_for_each(pos, &priv->clients) { |
| 692 | if (list_entry(pos, struct sysmmu_drvdata, node) == data) { |
| 693 | found = true; |
| 694 | break; |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | if (!found) |
| 699 | goto finish; |
| 700 | |
| 701 | if (__exynos_sysmmu_disable(data)) { |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 702 | dev_dbg(dev, "%s: Detached IOMMU with pgtable %pa\n", |
| 703 | __func__, &pagetable); |
Wei Yongjun | f8ffcc9 | 2012-09-06 12:34:09 +0800 | [diff] [blame] | 704 | list_del_init(&data->node); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 705 | |
| 706 | } else { |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 707 | dev_dbg(dev, "%s: Detaching IOMMU with pgtable %pa delayed", |
| 708 | __func__, &pagetable); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | finish: |
| 712 | spin_unlock_irqrestore(&priv->lock, flags); |
| 713 | |
| 714 | if (found) |
| 715 | pm_runtime_put(data->sysmmu); |
| 716 | } |
| 717 | |
| 718 | static unsigned long *alloc_lv2entry(unsigned long *sent, unsigned long iova, |
| 719 | short *pgcounter) |
| 720 | { |
Cho KyongHo | 61128f0 | 2014-05-12 11:44:47 +0530 | [diff] [blame] | 721 | if (lv1ent_section(sent)) { |
| 722 | WARN(1, "Trying mapping on %#08lx mapped with 1MiB page", iova); |
| 723 | return ERR_PTR(-EADDRINUSE); |
| 724 | } |
| 725 | |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 726 | if (lv1ent_fault(sent)) { |
| 727 | unsigned long *pent; |
| 728 | |
Cho KyongHo | 734c3c7 | 2014-05-12 11:44:48 +0530 | [diff] [blame] | 729 | pent = kmem_cache_zalloc(lv2table_kmem_cache, GFP_ATOMIC); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 730 | BUG_ON((unsigned long)pent & (LV2TABLE_SIZE - 1)); |
| 731 | if (!pent) |
Cho KyongHo | 61128f0 | 2014-05-12 11:44:47 +0530 | [diff] [blame] | 732 | return ERR_PTR(-ENOMEM); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 733 | |
Cho KyongHo | 7222e8d | 2014-05-12 11:44:46 +0530 | [diff] [blame] | 734 | *sent = mk_lv1ent_page(virt_to_phys(pent)); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 735 | *pgcounter = NUM_LV2ENTRIES; |
| 736 | pgtable_flush(pent, pent + NUM_LV2ENTRIES); |
| 737 | pgtable_flush(sent, sent + 1); |
| 738 | } |
| 739 | |
| 740 | return page_entry(sent, iova); |
| 741 | } |
| 742 | |
Cho KyongHo | 61128f0 | 2014-05-12 11:44:47 +0530 | [diff] [blame] | 743 | static int lv1set_section(unsigned long *sent, unsigned long iova, |
| 744 | phys_addr_t paddr, short *pgcnt) |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 745 | { |
Cho KyongHo | 61128f0 | 2014-05-12 11:44:47 +0530 | [diff] [blame] | 746 | if (lv1ent_section(sent)) { |
| 747 | WARN(1, "Trying mapping on 1MiB@%#08lx that is mapped", |
| 748 | iova); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 749 | return -EADDRINUSE; |
Cho KyongHo | 61128f0 | 2014-05-12 11:44:47 +0530 | [diff] [blame] | 750 | } |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 751 | |
| 752 | if (lv1ent_page(sent)) { |
Cho KyongHo | 61128f0 | 2014-05-12 11:44:47 +0530 | [diff] [blame] | 753 | if (*pgcnt != NUM_LV2ENTRIES) { |
| 754 | WARN(1, "Trying mapping on 1MiB@%#08lx that is mapped", |
| 755 | iova); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 756 | return -EADDRINUSE; |
Cho KyongHo | 61128f0 | 2014-05-12 11:44:47 +0530 | [diff] [blame] | 757 | } |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 758 | |
Cho KyongHo | 734c3c7 | 2014-05-12 11:44:48 +0530 | [diff] [blame] | 759 | kmem_cache_free(lv2table_kmem_cache, page_entry(sent, 0)); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 760 | *pgcnt = 0; |
| 761 | } |
| 762 | |
| 763 | *sent = mk_lv1ent_sect(paddr); |
| 764 | |
| 765 | pgtable_flush(sent, sent + 1); |
| 766 | |
| 767 | return 0; |
| 768 | } |
| 769 | |
| 770 | static int lv2set_page(unsigned long *pent, phys_addr_t paddr, size_t size, |
| 771 | short *pgcnt) |
| 772 | { |
| 773 | if (size == SPAGE_SIZE) { |
Cho KyongHo | 61128f0 | 2014-05-12 11:44:47 +0530 | [diff] [blame] | 774 | if (!lv2ent_fault(pent)) { |
| 775 | WARN(1, "Trying mapping on 4KiB where mapping exists"); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 776 | return -EADDRINUSE; |
Cho KyongHo | 61128f0 | 2014-05-12 11:44:47 +0530 | [diff] [blame] | 777 | } |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 778 | |
| 779 | *pent = mk_lv2ent_spage(paddr); |
| 780 | pgtable_flush(pent, pent + 1); |
| 781 | *pgcnt -= 1; |
| 782 | } else { /* size == LPAGE_SIZE */ |
| 783 | int i; |
| 784 | for (i = 0; i < SPAGES_PER_LPAGE; i++, pent++) { |
| 785 | if (!lv2ent_fault(pent)) { |
Cho KyongHo | 61128f0 | 2014-05-12 11:44:47 +0530 | [diff] [blame] | 786 | WARN(1, |
| 787 | "Trying mapping on 64KiB where mapping exists"); |
| 788 | if (i > 0) |
| 789 | memset(pent - i, 0, sizeof(*pent) * i); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 790 | return -EADDRINUSE; |
| 791 | } |
| 792 | |
| 793 | *pent = mk_lv2ent_lpage(paddr); |
| 794 | } |
| 795 | pgtable_flush(pent - SPAGES_PER_LPAGE, pent); |
| 796 | *pgcnt -= SPAGES_PER_LPAGE; |
| 797 | } |
| 798 | |
| 799 | return 0; |
| 800 | } |
| 801 | |
| 802 | static int exynos_iommu_map(struct iommu_domain *domain, unsigned long iova, |
| 803 | phys_addr_t paddr, size_t size, int prot) |
| 804 | { |
| 805 | struct exynos_iommu_domain *priv = domain->priv; |
| 806 | unsigned long *entry; |
| 807 | unsigned long flags; |
| 808 | int ret = -ENOMEM; |
| 809 | |
| 810 | BUG_ON(priv->pgtable == NULL); |
| 811 | |
| 812 | spin_lock_irqsave(&priv->pgtablelock, flags); |
| 813 | |
| 814 | entry = section_entry(priv->pgtable, iova); |
| 815 | |
| 816 | if (size == SECT_SIZE) { |
Cho KyongHo | 61128f0 | 2014-05-12 11:44:47 +0530 | [diff] [blame] | 817 | ret = lv1set_section(entry, iova, paddr, |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 818 | &priv->lv2entcnt[lv1ent_offset(iova)]); |
| 819 | } else { |
| 820 | unsigned long *pent; |
| 821 | |
| 822 | pent = alloc_lv2entry(entry, iova, |
| 823 | &priv->lv2entcnt[lv1ent_offset(iova)]); |
| 824 | |
Cho KyongHo | 61128f0 | 2014-05-12 11:44:47 +0530 | [diff] [blame] | 825 | if (IS_ERR(pent)) |
| 826 | ret = PTR_ERR(pent); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 827 | else |
| 828 | ret = lv2set_page(pent, paddr, size, |
| 829 | &priv->lv2entcnt[lv1ent_offset(iova)]); |
| 830 | } |
| 831 | |
Cho KyongHo | 61128f0 | 2014-05-12 11:44:47 +0530 | [diff] [blame] | 832 | if (ret) |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 833 | pr_debug("%s: Failed to map iova 0x%lx/0x%x bytes\n", |
| 834 | __func__, iova, size); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 835 | |
| 836 | spin_unlock_irqrestore(&priv->pgtablelock, flags); |
| 837 | |
| 838 | return ret; |
| 839 | } |
| 840 | |
| 841 | static size_t exynos_iommu_unmap(struct iommu_domain *domain, |
| 842 | unsigned long iova, size_t size) |
| 843 | { |
| 844 | struct exynos_iommu_domain *priv = domain->priv; |
| 845 | struct sysmmu_drvdata *data; |
| 846 | unsigned long flags; |
| 847 | unsigned long *ent; |
Cho KyongHo | 61128f0 | 2014-05-12 11:44:47 +0530 | [diff] [blame] | 848 | size_t err_pgsize; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 849 | |
| 850 | BUG_ON(priv->pgtable == NULL); |
| 851 | |
| 852 | spin_lock_irqsave(&priv->pgtablelock, flags); |
| 853 | |
| 854 | ent = section_entry(priv->pgtable, iova); |
| 855 | |
| 856 | if (lv1ent_section(ent)) { |
Cho KyongHo | 61128f0 | 2014-05-12 11:44:47 +0530 | [diff] [blame] | 857 | if (size < SECT_SIZE) { |
| 858 | err_pgsize = SECT_SIZE; |
| 859 | goto err; |
| 860 | } |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 861 | |
| 862 | *ent = 0; |
| 863 | pgtable_flush(ent, ent + 1); |
| 864 | size = SECT_SIZE; |
| 865 | goto done; |
| 866 | } |
| 867 | |
| 868 | if (unlikely(lv1ent_fault(ent))) { |
| 869 | if (size > SECT_SIZE) |
| 870 | size = SECT_SIZE; |
| 871 | goto done; |
| 872 | } |
| 873 | |
| 874 | /* lv1ent_page(sent) == true here */ |
| 875 | |
| 876 | ent = page_entry(ent, iova); |
| 877 | |
| 878 | if (unlikely(lv2ent_fault(ent))) { |
| 879 | size = SPAGE_SIZE; |
| 880 | goto done; |
| 881 | } |
| 882 | |
| 883 | if (lv2ent_small(ent)) { |
| 884 | *ent = 0; |
| 885 | size = SPAGE_SIZE; |
Cho KyongHo | 6cb47ed | 2014-05-12 11:44:51 +0530 | [diff] [blame] | 886 | pgtable_flush(ent, ent + 1); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 887 | priv->lv2entcnt[lv1ent_offset(iova)] += 1; |
| 888 | goto done; |
| 889 | } |
| 890 | |
| 891 | /* lv1ent_large(ent) == true here */ |
Cho KyongHo | 61128f0 | 2014-05-12 11:44:47 +0530 | [diff] [blame] | 892 | if (size < LPAGE_SIZE) { |
| 893 | err_pgsize = LPAGE_SIZE; |
| 894 | goto err; |
| 895 | } |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 896 | |
| 897 | memset(ent, 0, sizeof(*ent) * SPAGES_PER_LPAGE); |
Cho KyongHo | 6cb47ed | 2014-05-12 11:44:51 +0530 | [diff] [blame] | 898 | pgtable_flush(ent, ent + SPAGES_PER_LPAGE); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 899 | |
| 900 | size = LPAGE_SIZE; |
| 901 | priv->lv2entcnt[lv1ent_offset(iova)] += SPAGES_PER_LPAGE; |
| 902 | done: |
| 903 | spin_unlock_irqrestore(&priv->pgtablelock, flags); |
| 904 | |
| 905 | spin_lock_irqsave(&priv->lock, flags); |
| 906 | list_for_each_entry(data, &priv->clients, node) |
Cho KyongHo | 3ad6b7f | 2014-05-12 11:44:49 +0530 | [diff] [blame] | 907 | sysmmu_tlb_invalidate_entry(data->dev, iova, size); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 908 | spin_unlock_irqrestore(&priv->lock, flags); |
| 909 | |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 910 | return size; |
Cho KyongHo | 61128f0 | 2014-05-12 11:44:47 +0530 | [diff] [blame] | 911 | err: |
| 912 | spin_unlock_irqrestore(&priv->pgtablelock, flags); |
| 913 | |
| 914 | WARN(1, |
| 915 | "%s: Failed due to size(%#x) @ %#08lx is smaller than page size %#x\n", |
| 916 | __func__, size, iova, err_pgsize); |
| 917 | |
| 918 | return 0; |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | static phys_addr_t exynos_iommu_iova_to_phys(struct iommu_domain *domain, |
Varun Sethi | bb5547a | 2013-03-29 01:23:58 +0530 | [diff] [blame] | 922 | dma_addr_t iova) |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 923 | { |
| 924 | struct exynos_iommu_domain *priv = domain->priv; |
| 925 | unsigned long *entry; |
| 926 | unsigned long flags; |
| 927 | phys_addr_t phys = 0; |
| 928 | |
| 929 | spin_lock_irqsave(&priv->pgtablelock, flags); |
| 930 | |
| 931 | entry = section_entry(priv->pgtable, iova); |
| 932 | |
| 933 | if (lv1ent_section(entry)) { |
| 934 | phys = section_phys(entry) + section_offs(iova); |
| 935 | } else if (lv1ent_page(entry)) { |
| 936 | entry = page_entry(entry, iova); |
| 937 | |
| 938 | if (lv2ent_large(entry)) |
| 939 | phys = lpage_phys(entry) + lpage_offs(iova); |
| 940 | else if (lv2ent_small(entry)) |
| 941 | phys = spage_phys(entry) + spage_offs(iova); |
| 942 | } |
| 943 | |
| 944 | spin_unlock_irqrestore(&priv->pgtablelock, flags); |
| 945 | |
| 946 | return phys; |
| 947 | } |
| 948 | |
| 949 | static struct iommu_ops exynos_iommu_ops = { |
| 950 | .domain_init = &exynos_iommu_domain_init, |
| 951 | .domain_destroy = &exynos_iommu_domain_destroy, |
| 952 | .attach_dev = &exynos_iommu_attach_device, |
| 953 | .detach_dev = &exynos_iommu_detach_device, |
| 954 | .map = &exynos_iommu_map, |
| 955 | .unmap = &exynos_iommu_unmap, |
| 956 | .iova_to_phys = &exynos_iommu_iova_to_phys, |
| 957 | .pgsize_bitmap = SECT_SIZE | LPAGE_SIZE | SPAGE_SIZE, |
| 958 | }; |
| 959 | |
| 960 | static int __init exynos_iommu_init(void) |
| 961 | { |
| 962 | int ret; |
| 963 | |
Cho KyongHo | 734c3c7 | 2014-05-12 11:44:48 +0530 | [diff] [blame] | 964 | lv2table_kmem_cache = kmem_cache_create("exynos-iommu-lv2table", |
| 965 | LV2TABLE_SIZE, LV2TABLE_SIZE, 0, NULL); |
| 966 | if (!lv2table_kmem_cache) { |
| 967 | pr_err("%s: Failed to create kmem cache\n", __func__); |
| 968 | return -ENOMEM; |
| 969 | } |
| 970 | |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 971 | ret = platform_driver_register(&exynos_sysmmu_driver); |
Cho KyongHo | 734c3c7 | 2014-05-12 11:44:48 +0530 | [diff] [blame] | 972 | if (ret) { |
| 973 | pr_err("%s: Failed to register driver\n", __func__); |
| 974 | goto err_reg_driver; |
| 975 | } |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 976 | |
Cho KyongHo | 734c3c7 | 2014-05-12 11:44:48 +0530 | [diff] [blame] | 977 | ret = bus_set_iommu(&platform_bus_type, &exynos_iommu_ops); |
| 978 | if (ret) { |
| 979 | pr_err("%s: Failed to register exynos-iommu driver.\n", |
| 980 | __func__); |
| 981 | goto err_set_iommu; |
| 982 | } |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 983 | |
Cho KyongHo | 734c3c7 | 2014-05-12 11:44:48 +0530 | [diff] [blame] | 984 | return 0; |
| 985 | err_set_iommu: |
| 986 | platform_driver_unregister(&exynos_sysmmu_driver); |
| 987 | err_reg_driver: |
| 988 | kmem_cache_destroy(lv2table_kmem_cache); |
KyongHo Cho | 2a96536 | 2012-05-12 05:56:09 +0900 | [diff] [blame] | 989 | return ret; |
| 990 | } |
| 991 | subsys_initcall(exynos_iommu_init); |