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