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