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