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