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