Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 1 | /* |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 2 | * Copyright (C) 2011-2014 NVIDIA CORPORATION. All rights reserved. |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 3 | * |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
Thierry Reding | 804cb54 | 2015-03-27 11:07:27 +0100 | [diff] [blame] | 9 | #include <linux/bitops.h> |
Thierry Reding | d1313e7 | 2015-01-23 09:49:25 +0100 | [diff] [blame] | 10 | #include <linux/debugfs.h> |
Thierry Reding | bc5e6de | 2013-01-21 11:09:06 +0100 | [diff] [blame] | 11 | #include <linux/err.h> |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 12 | #include <linux/iommu.h> |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 13 | #include <linux/kernel.h> |
Hiroshi Doyu | 0760e8f | 2012-06-25 14:23:55 +0300 | [diff] [blame] | 14 | #include <linux/of.h> |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 15 | #include <linux/of_device.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/slab.h> |
Thierry Reding | 306a7f9 | 2014-07-17 13:17:24 +0200 | [diff] [blame] | 18 | |
| 19 | #include <soc/tegra/ahb.h> |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 20 | #include <soc/tegra/mc.h> |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 21 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 22 | struct tegra_smmu { |
| 23 | void __iomem *regs; |
| 24 | struct device *dev; |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 25 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 26 | struct tegra_mc *mc; |
| 27 | const struct tegra_smmu_soc *soc; |
Stephen Warren | e6bc593 | 2012-09-04 16:36:15 -0600 | [diff] [blame] | 28 | |
Thierry Reding | 804cb54 | 2015-03-27 11:07:27 +0100 | [diff] [blame] | 29 | unsigned long pfn_mask; |
| 30 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 31 | unsigned long *asids; |
| 32 | struct mutex lock; |
Stephen Warren | e6bc593 | 2012-09-04 16:36:15 -0600 | [diff] [blame] | 33 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 34 | struct list_head list; |
Thierry Reding | d1313e7 | 2015-01-23 09:49:25 +0100 | [diff] [blame] | 35 | |
| 36 | struct dentry *debugfs; |
Stephen Warren | e6bc593 | 2012-09-04 16:36:15 -0600 | [diff] [blame] | 37 | }; |
| 38 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 39 | struct tegra_smmu_as { |
Joerg Roedel | d5f1a81 | 2015-03-26 13:43:12 +0100 | [diff] [blame] | 40 | struct iommu_domain domain; |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 41 | struct tegra_smmu *smmu; |
| 42 | unsigned int use_count; |
Russell King | 32924c7 | 2015-07-27 13:29:31 +0100 | [diff] [blame] | 43 | u32 *count; |
Russell King | 853520f | 2015-07-27 13:29:26 +0100 | [diff] [blame] | 44 | struct page **pts; |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 45 | struct page *pd; |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 46 | dma_addr_t pd_dma; |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 47 | unsigned id; |
| 48 | u32 attr; |
Hiroshi Doyu | 39abf8a | 2012-08-02 11:46:40 +0300 | [diff] [blame] | 49 | }; |
| 50 | |
Joerg Roedel | d5f1a81 | 2015-03-26 13:43:12 +0100 | [diff] [blame] | 51 | static struct tegra_smmu_as *to_smmu_as(struct iommu_domain *dom) |
| 52 | { |
| 53 | return container_of(dom, struct tegra_smmu_as, domain); |
| 54 | } |
| 55 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 56 | static inline void smmu_writel(struct tegra_smmu *smmu, u32 value, |
| 57 | unsigned long offset) |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 58 | { |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 59 | writel(value, smmu->regs + offset); |
Joerg Roedel | fe1229b | 2013-02-04 20:40:58 +0100 | [diff] [blame] | 60 | } |
| 61 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 62 | static inline u32 smmu_readl(struct tegra_smmu *smmu, unsigned long offset) |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 63 | { |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 64 | return readl(smmu->regs + offset); |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 65 | } |
| 66 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 67 | #define SMMU_CONFIG 0x010 |
| 68 | #define SMMU_CONFIG_ENABLE (1 << 0) |
| 69 | |
| 70 | #define SMMU_TLB_CONFIG 0x14 |
| 71 | #define SMMU_TLB_CONFIG_HIT_UNDER_MISS (1 << 29) |
| 72 | #define SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION (1 << 28) |
| 73 | #define SMMU_TLB_CONFIG_ACTIVE_LINES(x) ((x) & 0x3f) |
| 74 | |
| 75 | #define SMMU_PTC_CONFIG 0x18 |
| 76 | #define SMMU_PTC_CONFIG_ENABLE (1 << 29) |
| 77 | #define SMMU_PTC_CONFIG_REQ_LIMIT(x) (((x) & 0x0f) << 24) |
| 78 | #define SMMU_PTC_CONFIG_INDEX_MAP(x) ((x) & 0x3f) |
| 79 | |
| 80 | #define SMMU_PTB_ASID 0x01c |
| 81 | #define SMMU_PTB_ASID_VALUE(x) ((x) & 0x7f) |
| 82 | |
| 83 | #define SMMU_PTB_DATA 0x020 |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 84 | #define SMMU_PTB_DATA_VALUE(dma, attr) ((dma) >> 12 | (attr)) |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 85 | |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 86 | #define SMMU_MK_PDE(dma, attr) ((dma) >> SMMU_PTE_SHIFT | (attr)) |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 87 | |
| 88 | #define SMMU_TLB_FLUSH 0x030 |
| 89 | #define SMMU_TLB_FLUSH_VA_MATCH_ALL (0 << 0) |
| 90 | #define SMMU_TLB_FLUSH_VA_MATCH_SECTION (2 << 0) |
| 91 | #define SMMU_TLB_FLUSH_VA_MATCH_GROUP (3 << 0) |
| 92 | #define SMMU_TLB_FLUSH_ASID(x) (((x) & 0x7f) << 24) |
| 93 | #define SMMU_TLB_FLUSH_VA_SECTION(addr) ((((addr) & 0xffc00000) >> 12) | \ |
| 94 | SMMU_TLB_FLUSH_VA_MATCH_SECTION) |
| 95 | #define SMMU_TLB_FLUSH_VA_GROUP(addr) ((((addr) & 0xffffc000) >> 12) | \ |
| 96 | SMMU_TLB_FLUSH_VA_MATCH_GROUP) |
| 97 | #define SMMU_TLB_FLUSH_ASID_MATCH (1 << 31) |
| 98 | |
| 99 | #define SMMU_PTC_FLUSH 0x034 |
| 100 | #define SMMU_PTC_FLUSH_TYPE_ALL (0 << 0) |
| 101 | #define SMMU_PTC_FLUSH_TYPE_ADR (1 << 0) |
| 102 | |
| 103 | #define SMMU_PTC_FLUSH_HI 0x9b8 |
| 104 | #define SMMU_PTC_FLUSH_HI_MASK 0x3 |
| 105 | |
| 106 | /* per-SWGROUP SMMU_*_ASID register */ |
| 107 | #define SMMU_ASID_ENABLE (1 << 31) |
| 108 | #define SMMU_ASID_MASK 0x7f |
| 109 | #define SMMU_ASID_VALUE(x) ((x) & SMMU_ASID_MASK) |
| 110 | |
| 111 | /* page table definitions */ |
| 112 | #define SMMU_NUM_PDE 1024 |
| 113 | #define SMMU_NUM_PTE 1024 |
| 114 | |
| 115 | #define SMMU_SIZE_PD (SMMU_NUM_PDE * 4) |
| 116 | #define SMMU_SIZE_PT (SMMU_NUM_PTE * 4) |
| 117 | |
| 118 | #define SMMU_PDE_SHIFT 22 |
| 119 | #define SMMU_PTE_SHIFT 12 |
| 120 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 121 | #define SMMU_PD_READABLE (1 << 31) |
| 122 | #define SMMU_PD_WRITABLE (1 << 30) |
| 123 | #define SMMU_PD_NONSECURE (1 << 29) |
| 124 | |
| 125 | #define SMMU_PDE_READABLE (1 << 31) |
| 126 | #define SMMU_PDE_WRITABLE (1 << 30) |
| 127 | #define SMMU_PDE_NONSECURE (1 << 29) |
| 128 | #define SMMU_PDE_NEXT (1 << 28) |
| 129 | |
| 130 | #define SMMU_PTE_READABLE (1 << 31) |
| 131 | #define SMMU_PTE_WRITABLE (1 << 30) |
| 132 | #define SMMU_PTE_NONSECURE (1 << 29) |
| 133 | |
| 134 | #define SMMU_PDE_ATTR (SMMU_PDE_READABLE | SMMU_PDE_WRITABLE | \ |
| 135 | SMMU_PDE_NONSECURE) |
| 136 | #define SMMU_PTE_ATTR (SMMU_PTE_READABLE | SMMU_PTE_WRITABLE | \ |
| 137 | SMMU_PTE_NONSECURE) |
| 138 | |
Russell King | 34d35f8 | 2015-07-27 13:29:16 +0100 | [diff] [blame] | 139 | static unsigned int iova_pd_index(unsigned long iova) |
| 140 | { |
| 141 | return (iova >> SMMU_PDE_SHIFT) & (SMMU_NUM_PDE - 1); |
| 142 | } |
| 143 | |
| 144 | static unsigned int iova_pt_index(unsigned long iova) |
| 145 | { |
| 146 | return (iova >> SMMU_PTE_SHIFT) & (SMMU_NUM_PTE - 1); |
| 147 | } |
| 148 | |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 149 | static bool smmu_dma_addr_valid(struct tegra_smmu *smmu, dma_addr_t addr) |
Russell King | 4b3c7d1 | 2015-07-27 13:29:36 +0100 | [diff] [blame] | 150 | { |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 151 | addr >>= 12; |
| 152 | return (addr & smmu->pfn_mask) == addr; |
| 153 | } |
Russell King | 4b3c7d1 | 2015-07-27 13:29:36 +0100 | [diff] [blame] | 154 | |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 155 | static dma_addr_t smmu_pde_to_dma(u32 pde) |
| 156 | { |
| 157 | return pde << 12; |
Russell King | 4b3c7d1 | 2015-07-27 13:29:36 +0100 | [diff] [blame] | 158 | } |
| 159 | |
Russell King | b8fe038 | 2015-07-27 13:29:41 +0100 | [diff] [blame] | 160 | static void smmu_flush_ptc_all(struct tegra_smmu *smmu) |
| 161 | { |
| 162 | smmu_writel(smmu, SMMU_PTC_FLUSH_TYPE_ALL, SMMU_PTC_FLUSH); |
| 163 | } |
| 164 | |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 165 | static inline void smmu_flush_ptc(struct tegra_smmu *smmu, dma_addr_t dma, |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 166 | unsigned long offset) |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 167 | { |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 168 | u32 value; |
Hiroshi Doyu | a6870e9 | 2013-01-31 10:14:10 +0200 | [diff] [blame] | 169 | |
Russell King | b8fe038 | 2015-07-27 13:29:41 +0100 | [diff] [blame] | 170 | offset &= ~(smmu->mc->soc->atom_size - 1); |
Hiroshi Doyu | a6870e9 | 2013-01-31 10:14:10 +0200 | [diff] [blame] | 171 | |
Russell King | b8fe038 | 2015-07-27 13:29:41 +0100 | [diff] [blame] | 172 | if (smmu->mc->soc->num_address_bits > 32) { |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 173 | #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT |
| 174 | value = (dma >> 32) & SMMU_PTC_FLUSH_HI_MASK; |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 175 | #else |
Russell King | b8fe038 | 2015-07-27 13:29:41 +0100 | [diff] [blame] | 176 | value = 0; |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 177 | #endif |
Russell King | b8fe038 | 2015-07-27 13:29:41 +0100 | [diff] [blame] | 178 | smmu_writel(smmu, value, SMMU_PTC_FLUSH_HI); |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 179 | } |
Hiroshi DOYU | 9e971a0 | 2012-07-02 14:26:38 +0300 | [diff] [blame] | 180 | |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 181 | value = (dma + offset) | SMMU_PTC_FLUSH_TYPE_ADR; |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 182 | smmu_writel(smmu, value, SMMU_PTC_FLUSH); |
| 183 | } |
| 184 | |
| 185 | static inline void smmu_flush_tlb(struct tegra_smmu *smmu) |
| 186 | { |
| 187 | smmu_writel(smmu, SMMU_TLB_FLUSH_VA_MATCH_ALL, SMMU_TLB_FLUSH); |
| 188 | } |
| 189 | |
| 190 | static inline void smmu_flush_tlb_asid(struct tegra_smmu *smmu, |
| 191 | unsigned long asid) |
| 192 | { |
| 193 | u32 value; |
| 194 | |
| 195 | value = SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_ASID(asid) | |
| 196 | SMMU_TLB_FLUSH_VA_MATCH_ALL; |
| 197 | smmu_writel(smmu, value, SMMU_TLB_FLUSH); |
| 198 | } |
| 199 | |
| 200 | static inline void smmu_flush_tlb_section(struct tegra_smmu *smmu, |
| 201 | unsigned long asid, |
| 202 | unsigned long iova) |
| 203 | { |
| 204 | u32 value; |
| 205 | |
| 206 | value = SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_ASID(asid) | |
| 207 | SMMU_TLB_FLUSH_VA_SECTION(iova); |
| 208 | smmu_writel(smmu, value, SMMU_TLB_FLUSH); |
| 209 | } |
| 210 | |
| 211 | static inline void smmu_flush_tlb_group(struct tegra_smmu *smmu, |
| 212 | unsigned long asid, |
| 213 | unsigned long iova) |
| 214 | { |
| 215 | u32 value; |
| 216 | |
| 217 | value = SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_ASID(asid) | |
| 218 | SMMU_TLB_FLUSH_VA_GROUP(iova); |
| 219 | smmu_writel(smmu, value, SMMU_TLB_FLUSH); |
| 220 | } |
| 221 | |
| 222 | static inline void smmu_flush(struct tegra_smmu *smmu) |
| 223 | { |
| 224 | smmu_readl(smmu, SMMU_CONFIG); |
| 225 | } |
| 226 | |
| 227 | static int tegra_smmu_alloc_asid(struct tegra_smmu *smmu, unsigned int *idp) |
| 228 | { |
| 229 | unsigned long id; |
| 230 | |
| 231 | mutex_lock(&smmu->lock); |
| 232 | |
| 233 | id = find_first_zero_bit(smmu->asids, smmu->soc->num_asids); |
| 234 | if (id >= smmu->soc->num_asids) { |
| 235 | mutex_unlock(&smmu->lock); |
| 236 | return -ENOSPC; |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 237 | } |
Hiroshi DOYU | 9e971a0 | 2012-07-02 14:26:38 +0300 | [diff] [blame] | 238 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 239 | set_bit(id, smmu->asids); |
| 240 | *idp = id; |
Hiroshi DOYU | 9e971a0 | 2012-07-02 14:26:38 +0300 | [diff] [blame] | 241 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 242 | mutex_unlock(&smmu->lock); |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 243 | return 0; |
| 244 | } |
| 245 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 246 | static void tegra_smmu_free_asid(struct tegra_smmu *smmu, unsigned int id) |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 247 | { |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 248 | mutex_lock(&smmu->lock); |
| 249 | clear_bit(id, smmu->asids); |
| 250 | mutex_unlock(&smmu->lock); |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 251 | } |
| 252 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 253 | static bool tegra_smmu_capable(enum iommu_cap cap) |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 254 | { |
Joerg Roedel | 7c2aa64 | 2014-09-05 10:51:37 +0200 | [diff] [blame] | 255 | return false; |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 256 | } |
| 257 | |
Joerg Roedel | d5f1a81 | 2015-03-26 13:43:12 +0100 | [diff] [blame] | 258 | static struct iommu_domain *tegra_smmu_domain_alloc(unsigned type) |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 259 | { |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 260 | struct tegra_smmu_as *as; |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 261 | |
Joerg Roedel | d5f1a81 | 2015-03-26 13:43:12 +0100 | [diff] [blame] | 262 | if (type != IOMMU_DOMAIN_UNMANAGED) |
| 263 | return NULL; |
| 264 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 265 | as = kzalloc(sizeof(*as), GFP_KERNEL); |
| 266 | if (!as) |
Joerg Roedel | d5f1a81 | 2015-03-26 13:43:12 +0100 | [diff] [blame] | 267 | return NULL; |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 268 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 269 | as->attr = SMMU_PD_READABLE | SMMU_PD_WRITABLE | SMMU_PD_NONSECURE; |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 270 | |
Russell King | 707917c | 2015-07-27 13:30:02 +0100 | [diff] [blame^] | 271 | as->pd = alloc_page(GFP_KERNEL | __GFP_DMA | __GFP_ZERO); |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 272 | if (!as->pd) { |
| 273 | kfree(as); |
Joerg Roedel | d5f1a81 | 2015-03-26 13:43:12 +0100 | [diff] [blame] | 274 | return NULL; |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 275 | } |
| 276 | |
Russell King | 32924c7 | 2015-07-27 13:29:31 +0100 | [diff] [blame] | 277 | as->count = kcalloc(SMMU_NUM_PDE, sizeof(u32), GFP_KERNEL); |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 278 | if (!as->count) { |
| 279 | __free_page(as->pd); |
| 280 | kfree(as); |
Joerg Roedel | d5f1a81 | 2015-03-26 13:43:12 +0100 | [diff] [blame] | 281 | return NULL; |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 282 | } |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 283 | |
Russell King | 853520f | 2015-07-27 13:29:26 +0100 | [diff] [blame] | 284 | as->pts = kcalloc(SMMU_NUM_PDE, sizeof(*as->pts), GFP_KERNEL); |
| 285 | if (!as->pts) { |
Russell King | 32924c7 | 2015-07-27 13:29:31 +0100 | [diff] [blame] | 286 | kfree(as->count); |
Russell King | 853520f | 2015-07-27 13:29:26 +0100 | [diff] [blame] | 287 | __free_page(as->pd); |
| 288 | kfree(as); |
| 289 | return NULL; |
| 290 | } |
| 291 | |
Thierry Reding | 471d914 | 2015-03-27 11:07:25 +0100 | [diff] [blame] | 292 | /* setup aperture */ |
Joerg Roedel | 7f65ef0 | 2015-04-02 13:33:19 +0200 | [diff] [blame] | 293 | as->domain.geometry.aperture_start = 0; |
| 294 | as->domain.geometry.aperture_end = 0xffffffff; |
| 295 | as->domain.geometry.force_aperture = true; |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 296 | |
Joerg Roedel | d5f1a81 | 2015-03-26 13:43:12 +0100 | [diff] [blame] | 297 | return &as->domain; |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 298 | } |
| 299 | |
Joerg Roedel | d5f1a81 | 2015-03-26 13:43:12 +0100 | [diff] [blame] | 300 | static void tegra_smmu_domain_free(struct iommu_domain *domain) |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 301 | { |
Joerg Roedel | d5f1a81 | 2015-03-26 13:43:12 +0100 | [diff] [blame] | 302 | struct tegra_smmu_as *as = to_smmu_as(domain); |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 303 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 304 | /* TODO: free page directory and page tables */ |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 305 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 306 | kfree(as); |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 307 | } |
| 308 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 309 | static const struct tegra_smmu_swgroup * |
| 310 | tegra_smmu_find_swgroup(struct tegra_smmu *smmu, unsigned int swgroup) |
Hiroshi Doyu | 39abf8a | 2012-08-02 11:46:40 +0300 | [diff] [blame] | 311 | { |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 312 | const struct tegra_smmu_swgroup *group = NULL; |
| 313 | unsigned int i; |
Hiroshi Doyu | 39abf8a | 2012-08-02 11:46:40 +0300 | [diff] [blame] | 314 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 315 | for (i = 0; i < smmu->soc->num_swgroups; i++) { |
| 316 | if (smmu->soc->swgroups[i].swgroup == swgroup) { |
| 317 | group = &smmu->soc->swgroups[i]; |
Hiroshi Doyu | 39abf8a | 2012-08-02 11:46:40 +0300 | [diff] [blame] | 318 | break; |
Hiroshi Doyu | 39abf8a | 2012-08-02 11:46:40 +0300 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 322 | return group; |
Hiroshi Doyu | 39abf8a | 2012-08-02 11:46:40 +0300 | [diff] [blame] | 323 | } |
| 324 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 325 | static void tegra_smmu_enable(struct tegra_smmu *smmu, unsigned int swgroup, |
| 326 | unsigned int asid) |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 327 | { |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 328 | const struct tegra_smmu_swgroup *group; |
| 329 | unsigned int i; |
| 330 | u32 value; |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 331 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 332 | for (i = 0; i < smmu->soc->num_clients; i++) { |
| 333 | const struct tegra_mc_client *client = &smmu->soc->clients[i]; |
| 334 | |
| 335 | if (client->swgroup != swgroup) |
| 336 | continue; |
| 337 | |
| 338 | value = smmu_readl(smmu, client->smmu.reg); |
| 339 | value |= BIT(client->smmu.bit); |
| 340 | smmu_writel(smmu, value, client->smmu.reg); |
| 341 | } |
| 342 | |
| 343 | group = tegra_smmu_find_swgroup(smmu, swgroup); |
| 344 | if (group) { |
| 345 | value = smmu_readl(smmu, group->reg); |
| 346 | value &= ~SMMU_ASID_MASK; |
| 347 | value |= SMMU_ASID_VALUE(asid); |
| 348 | value |= SMMU_ASID_ENABLE; |
| 349 | smmu_writel(smmu, value, group->reg); |
| 350 | } |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 351 | } |
| 352 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 353 | static void tegra_smmu_disable(struct tegra_smmu *smmu, unsigned int swgroup, |
| 354 | unsigned int asid) |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 355 | { |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 356 | const struct tegra_smmu_swgroup *group; |
| 357 | unsigned int i; |
| 358 | u32 value; |
| 359 | |
| 360 | group = tegra_smmu_find_swgroup(smmu, swgroup); |
| 361 | if (group) { |
| 362 | value = smmu_readl(smmu, group->reg); |
| 363 | value &= ~SMMU_ASID_MASK; |
| 364 | value |= SMMU_ASID_VALUE(asid); |
| 365 | value &= ~SMMU_ASID_ENABLE; |
| 366 | smmu_writel(smmu, value, group->reg); |
| 367 | } |
| 368 | |
| 369 | for (i = 0; i < smmu->soc->num_clients; i++) { |
| 370 | const struct tegra_mc_client *client = &smmu->soc->clients[i]; |
| 371 | |
| 372 | if (client->swgroup != swgroup) |
| 373 | continue; |
| 374 | |
| 375 | value = smmu_readl(smmu, client->smmu.reg); |
| 376 | value &= ~BIT(client->smmu.bit); |
| 377 | smmu_writel(smmu, value, client->smmu.reg); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | static int tegra_smmu_as_prepare(struct tegra_smmu *smmu, |
| 382 | struct tegra_smmu_as *as) |
| 383 | { |
| 384 | u32 value; |
Hiroshi Doyu | 0760e8f | 2012-06-25 14:23:55 +0300 | [diff] [blame] | 385 | int err; |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 386 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 387 | if (as->use_count > 0) { |
| 388 | as->use_count++; |
| 389 | return 0; |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 390 | } |
| 391 | |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 392 | as->pd_dma = dma_map_page(smmu->dev, as->pd, 0, SMMU_SIZE_PD, |
| 393 | DMA_TO_DEVICE); |
| 394 | if (dma_mapping_error(smmu->dev, as->pd_dma)) |
| 395 | return -ENOMEM; |
| 396 | |
| 397 | /* We can't handle 64-bit DMA addresses */ |
| 398 | if (!smmu_dma_addr_valid(smmu, as->pd_dma)) { |
| 399 | err = -ENOMEM; |
| 400 | goto err_unmap; |
| 401 | } |
| 402 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 403 | err = tegra_smmu_alloc_asid(smmu, &as->id); |
| 404 | if (err < 0) |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 405 | goto err_unmap; |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 406 | |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 407 | smmu_flush_ptc(smmu, as->pd_dma, 0); |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 408 | smmu_flush_tlb_asid(smmu, as->id); |
| 409 | |
| 410 | smmu_writel(smmu, as->id & 0x7f, SMMU_PTB_ASID); |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 411 | value = SMMU_PTB_DATA_VALUE(as->pd_dma, as->attr); |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 412 | smmu_writel(smmu, value, SMMU_PTB_DATA); |
| 413 | smmu_flush(smmu); |
| 414 | |
| 415 | as->smmu = smmu; |
| 416 | as->use_count++; |
| 417 | |
| 418 | return 0; |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 419 | |
| 420 | err_unmap: |
| 421 | dma_unmap_page(smmu->dev, as->pd_dma, SMMU_SIZE_PD, DMA_TO_DEVICE); |
| 422 | return err; |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | static void tegra_smmu_as_unprepare(struct tegra_smmu *smmu, |
| 426 | struct tegra_smmu_as *as) |
| 427 | { |
| 428 | if (--as->use_count > 0) |
| 429 | return; |
| 430 | |
| 431 | tegra_smmu_free_asid(smmu, as->id); |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 432 | |
| 433 | dma_unmap_page(smmu->dev, as->pd_dma, SMMU_SIZE_PD, DMA_TO_DEVICE); |
| 434 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 435 | as->smmu = NULL; |
| 436 | } |
| 437 | |
| 438 | static int tegra_smmu_attach_dev(struct iommu_domain *domain, |
| 439 | struct device *dev) |
| 440 | { |
| 441 | struct tegra_smmu *smmu = dev->archdata.iommu; |
Joerg Roedel | d5f1a81 | 2015-03-26 13:43:12 +0100 | [diff] [blame] | 442 | struct tegra_smmu_as *as = to_smmu_as(domain); |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 443 | struct device_node *np = dev->of_node; |
| 444 | struct of_phandle_args args; |
| 445 | unsigned int index = 0; |
| 446 | int err = 0; |
| 447 | |
| 448 | while (!of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index, |
| 449 | &args)) { |
| 450 | unsigned int swgroup = args.args[0]; |
| 451 | |
| 452 | if (args.np != smmu->dev->of_node) { |
| 453 | of_node_put(args.np); |
| 454 | continue; |
| 455 | } |
| 456 | |
| 457 | of_node_put(args.np); |
| 458 | |
| 459 | err = tegra_smmu_as_prepare(smmu, as); |
| 460 | if (err < 0) |
| 461 | return err; |
| 462 | |
| 463 | tegra_smmu_enable(smmu, swgroup, as->id); |
| 464 | index++; |
| 465 | } |
| 466 | |
| 467 | if (index == 0) |
| 468 | return -ENODEV; |
| 469 | |
| 470 | return 0; |
| 471 | } |
| 472 | |
| 473 | static void tegra_smmu_detach_dev(struct iommu_domain *domain, struct device *dev) |
| 474 | { |
Joerg Roedel | d5f1a81 | 2015-03-26 13:43:12 +0100 | [diff] [blame] | 475 | struct tegra_smmu_as *as = to_smmu_as(domain); |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 476 | struct device_node *np = dev->of_node; |
| 477 | struct tegra_smmu *smmu = as->smmu; |
| 478 | struct of_phandle_args args; |
| 479 | unsigned int index = 0; |
| 480 | |
| 481 | while (!of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index, |
| 482 | &args)) { |
| 483 | unsigned int swgroup = args.args[0]; |
| 484 | |
| 485 | if (args.np != smmu->dev->of_node) { |
| 486 | of_node_put(args.np); |
| 487 | continue; |
| 488 | } |
| 489 | |
| 490 | of_node_put(args.np); |
| 491 | |
| 492 | tegra_smmu_disable(smmu, swgroup, as->id); |
| 493 | tegra_smmu_as_unprepare(smmu, as); |
| 494 | index++; |
| 495 | } |
| 496 | } |
| 497 | |
Russell King | 0b42c7c | 2015-07-27 13:29:21 +0100 | [diff] [blame] | 498 | static u32 *tegra_smmu_pte_offset(struct page *pt_page, unsigned long iova) |
| 499 | { |
| 500 | u32 *pt = page_address(pt_page); |
| 501 | |
| 502 | return pt + iova_pt_index(iova); |
| 503 | } |
| 504 | |
| 505 | static u32 *tegra_smmu_pte_lookup(struct tegra_smmu_as *as, unsigned long iova, |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 506 | dma_addr_t *dmap) |
Russell King | 0b42c7c | 2015-07-27 13:29:21 +0100 | [diff] [blame] | 507 | { |
| 508 | unsigned int pd_index = iova_pd_index(iova); |
| 509 | struct page *pt_page; |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 510 | u32 *pd; |
Russell King | 0b42c7c | 2015-07-27 13:29:21 +0100 | [diff] [blame] | 511 | |
Russell King | 853520f | 2015-07-27 13:29:26 +0100 | [diff] [blame] | 512 | pt_page = as->pts[pd_index]; |
| 513 | if (!pt_page) |
Russell King | 0b42c7c | 2015-07-27 13:29:21 +0100 | [diff] [blame] | 514 | return NULL; |
| 515 | |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 516 | pd = page_address(as->pd); |
| 517 | *dmap = smmu_pde_to_dma(pd[pd_index]); |
Russell King | 0b42c7c | 2015-07-27 13:29:21 +0100 | [diff] [blame] | 518 | |
| 519 | return tegra_smmu_pte_offset(pt_page, iova); |
| 520 | } |
| 521 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 522 | static u32 *as_get_pte(struct tegra_smmu_as *as, dma_addr_t iova, |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 523 | dma_addr_t *dmap) |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 524 | { |
Russell King | 32924c7 | 2015-07-27 13:29:31 +0100 | [diff] [blame] | 525 | u32 *pd = page_address(as->pd), *pt; |
Russell King | 34d35f8 | 2015-07-27 13:29:16 +0100 | [diff] [blame] | 526 | unsigned int pde = iova_pd_index(iova); |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 527 | struct tegra_smmu *smmu = as->smmu; |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 528 | |
Russell King | 853520f | 2015-07-27 13:29:26 +0100 | [diff] [blame] | 529 | if (!as->pts[pde]) { |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 530 | struct page *page; |
| 531 | dma_addr_t dma; |
| 532 | |
Russell King | 707917c | 2015-07-27 13:30:02 +0100 | [diff] [blame^] | 533 | page = alloc_page(GFP_KERNEL | __GFP_DMA | __GFP_ZERO); |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 534 | if (!page) |
| 535 | return NULL; |
| 536 | |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 537 | dma = dma_map_page(smmu->dev, page, 0, SMMU_SIZE_PT, |
| 538 | DMA_TO_DEVICE); |
| 539 | if (dma_mapping_error(smmu->dev, dma)) { |
| 540 | __free_page(page); |
| 541 | return NULL; |
| 542 | } |
| 543 | |
| 544 | if (!smmu_dma_addr_valid(smmu, dma)) { |
| 545 | dma_unmap_page(smmu->dev, dma, SMMU_SIZE_PT, |
| 546 | DMA_TO_DEVICE); |
| 547 | __free_page(page); |
| 548 | return NULL; |
| 549 | } |
| 550 | |
Russell King | 853520f | 2015-07-27 13:29:26 +0100 | [diff] [blame] | 551 | as->pts[pde] = page; |
| 552 | |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 553 | pd[pde] = SMMU_MK_PDE(dma, SMMU_PDE_ATTR | SMMU_PDE_NEXT); |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 554 | |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 555 | dma_sync_single_range_for_device(smmu->dev, as->pd_dma, |
| 556 | pde << 2, 4, DMA_TO_DEVICE); |
| 557 | smmu_flush_ptc(smmu, as->pd_dma, pde << 2); |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 558 | smmu_flush_tlb_section(smmu, as->id, iova); |
| 559 | smmu_flush(smmu); |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 560 | |
| 561 | *dmap = dma; |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 562 | } else { |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 563 | *dmap = smmu_pde_to_dma(pd[pde]); |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 564 | } |
| 565 | |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 566 | pt = tegra_smmu_pte_offset(as->pts[pde], iova); |
Russell King | 0b42c7c | 2015-07-27 13:29:21 +0100 | [diff] [blame] | 567 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 568 | /* Keep track of entries in this page table. */ |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 569 | if (*pt == 0) |
Russell King | 32924c7 | 2015-07-27 13:29:31 +0100 | [diff] [blame] | 570 | as->count[pde]++; |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 571 | |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 572 | return pt; |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 573 | } |
| 574 | |
Russell King | b98e34f | 2015-07-27 13:29:05 +0100 | [diff] [blame] | 575 | static void tegra_smmu_pte_put_use(struct tegra_smmu_as *as, unsigned long iova) |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 576 | { |
Russell King | b98e34f | 2015-07-27 13:29:05 +0100 | [diff] [blame] | 577 | struct tegra_smmu *smmu = as->smmu; |
Russell King | 34d35f8 | 2015-07-27 13:29:16 +0100 | [diff] [blame] | 578 | unsigned int pde = iova_pd_index(iova); |
Russell King | b98e34f | 2015-07-27 13:29:05 +0100 | [diff] [blame] | 579 | u32 *pd = page_address(as->pd); |
Russell King | 853520f | 2015-07-27 13:29:26 +0100 | [diff] [blame] | 580 | struct page *page = as->pts[pde]; |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 581 | |
| 582 | /* |
| 583 | * When no entries in this page table are used anymore, return the |
| 584 | * memory page to the system. |
| 585 | */ |
Russell King | 32924c7 | 2015-07-27 13:29:31 +0100 | [diff] [blame] | 586 | if (--as->count[pde] == 0) { |
Russell King | b98e34f | 2015-07-27 13:29:05 +0100 | [diff] [blame] | 587 | unsigned int offset = pde * sizeof(*pd); |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 588 | dma_addr_t pte_dma = smmu_pde_to_dma(pd[pde]); |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 589 | |
Russell King | b98e34f | 2015-07-27 13:29:05 +0100 | [diff] [blame] | 590 | /* Clear the page directory entry first */ |
| 591 | pd[pde] = 0; |
| 592 | |
| 593 | /* Flush the page directory entry */ |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 594 | dma_sync_single_range_for_device(smmu->dev, as->pd_dma, offset, |
| 595 | sizeof(*pd), DMA_TO_DEVICE); |
| 596 | smmu_flush_ptc(smmu, as->pd_dma, offset); |
Russell King | b98e34f | 2015-07-27 13:29:05 +0100 | [diff] [blame] | 597 | smmu_flush_tlb_section(smmu, as->id, iova); |
| 598 | smmu_flush(smmu); |
| 599 | |
| 600 | /* Finally, free the page */ |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 601 | dma_unmap_page(smmu->dev, pte_dma, SMMU_SIZE_PT, DMA_TO_DEVICE); |
Russell King | b98e34f | 2015-07-27 13:29:05 +0100 | [diff] [blame] | 602 | __free_page(page); |
Russell King | 853520f | 2015-07-27 13:29:26 +0100 | [diff] [blame] | 603 | as->pts[pde] = NULL; |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | |
Russell King | 8482ee5 | 2015-07-27 13:29:10 +0100 | [diff] [blame] | 607 | static void tegra_smmu_set_pte(struct tegra_smmu_as *as, unsigned long iova, |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 608 | u32 *pte, dma_addr_t pte_dma, u32 val) |
Russell King | 8482ee5 | 2015-07-27 13:29:10 +0100 | [diff] [blame] | 609 | { |
| 610 | struct tegra_smmu *smmu = as->smmu; |
| 611 | unsigned long offset = offset_in_page(pte); |
| 612 | |
| 613 | *pte = val; |
| 614 | |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 615 | dma_sync_single_range_for_device(smmu->dev, pte_dma, offset, |
| 616 | 4, DMA_TO_DEVICE); |
| 617 | smmu_flush_ptc(smmu, pte_dma, offset); |
Russell King | 8482ee5 | 2015-07-27 13:29:10 +0100 | [diff] [blame] | 618 | smmu_flush_tlb_group(smmu, as->id, iova); |
| 619 | smmu_flush(smmu); |
| 620 | } |
| 621 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 622 | static int tegra_smmu_map(struct iommu_domain *domain, unsigned long iova, |
| 623 | phys_addr_t paddr, size_t size, int prot) |
| 624 | { |
Joerg Roedel | d5f1a81 | 2015-03-26 13:43:12 +0100 | [diff] [blame] | 625 | struct tegra_smmu_as *as = to_smmu_as(domain); |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 626 | dma_addr_t pte_dma; |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 627 | u32 *pte; |
| 628 | |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 629 | pte = as_get_pte(as, iova, &pte_dma); |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 630 | if (!pte) |
Hiroshi Doyu | 0547c2f | 2012-06-25 14:23:57 +0300 | [diff] [blame] | 631 | return -ENOMEM; |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 632 | |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 633 | tegra_smmu_set_pte(as, iova, pte, pte_dma, |
Russell King | 8482ee5 | 2015-07-27 13:29:10 +0100 | [diff] [blame] | 634 | __phys_to_pfn(paddr) | SMMU_PTE_ATTR); |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 635 | |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 636 | return 0; |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 637 | } |
| 638 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 639 | static size_t tegra_smmu_unmap(struct iommu_domain *domain, unsigned long iova, |
| 640 | size_t size) |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 641 | { |
Joerg Roedel | d5f1a81 | 2015-03-26 13:43:12 +0100 | [diff] [blame] | 642 | struct tegra_smmu_as *as = to_smmu_as(domain); |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 643 | dma_addr_t pte_dma; |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 644 | u32 *pte; |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 645 | |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 646 | pte = tegra_smmu_pte_lookup(as, iova, &pte_dma); |
Russell King | b98e34f | 2015-07-27 13:29:05 +0100 | [diff] [blame] | 647 | if (!pte || !*pte) |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 648 | return 0; |
Hiroshi Doyu | 39abf8a | 2012-08-02 11:46:40 +0300 | [diff] [blame] | 649 | |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 650 | tegra_smmu_set_pte(as, iova, pte, pte_dma, 0); |
Russell King | b98e34f | 2015-07-27 13:29:05 +0100 | [diff] [blame] | 651 | tegra_smmu_pte_put_use(as, iova); |
| 652 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 653 | return size; |
| 654 | } |
| 655 | |
| 656 | static phys_addr_t tegra_smmu_iova_to_phys(struct iommu_domain *domain, |
| 657 | dma_addr_t iova) |
| 658 | { |
Joerg Roedel | d5f1a81 | 2015-03-26 13:43:12 +0100 | [diff] [blame] | 659 | struct tegra_smmu_as *as = to_smmu_as(domain); |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 660 | unsigned long pfn; |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 661 | dma_addr_t pte_dma; |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 662 | u32 *pte; |
| 663 | |
Russell King | e3c9719 | 2015-07-27 13:29:52 +0100 | [diff] [blame] | 664 | pte = tegra_smmu_pte_lookup(as, iova, &pte_dma); |
Russell King | 9113785 | 2015-07-27 13:29:00 +0100 | [diff] [blame] | 665 | if (!pte || !*pte) |
| 666 | return 0; |
| 667 | |
Thierry Reding | 804cb54 | 2015-03-27 11:07:27 +0100 | [diff] [blame] | 668 | pfn = *pte & as->smmu->pfn_mask; |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 669 | |
| 670 | return PFN_PHYS(pfn); |
| 671 | } |
| 672 | |
| 673 | static struct tegra_smmu *tegra_smmu_find(struct device_node *np) |
| 674 | { |
| 675 | struct platform_device *pdev; |
| 676 | struct tegra_mc *mc; |
| 677 | |
| 678 | pdev = of_find_device_by_node(np); |
| 679 | if (!pdev) |
| 680 | return NULL; |
| 681 | |
| 682 | mc = platform_get_drvdata(pdev); |
| 683 | if (!mc) |
| 684 | return NULL; |
| 685 | |
| 686 | return mc->smmu; |
| 687 | } |
| 688 | |
| 689 | static int tegra_smmu_add_device(struct device *dev) |
| 690 | { |
| 691 | struct device_node *np = dev->of_node; |
| 692 | struct of_phandle_args args; |
| 693 | unsigned int index = 0; |
| 694 | |
| 695 | while (of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index, |
| 696 | &args) == 0) { |
| 697 | struct tegra_smmu *smmu; |
| 698 | |
| 699 | smmu = tegra_smmu_find(args.np); |
| 700 | if (smmu) { |
| 701 | /* |
| 702 | * Only a single IOMMU master interface is currently |
| 703 | * supported by the Linux kernel, so abort after the |
| 704 | * first match. |
| 705 | */ |
| 706 | dev->archdata.iommu = smmu; |
| 707 | break; |
| 708 | } |
| 709 | |
| 710 | index++; |
| 711 | } |
| 712 | |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 713 | return 0; |
| 714 | } |
| 715 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 716 | static void tegra_smmu_remove_device(struct device *dev) |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 717 | { |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 718 | dev->archdata.iommu = NULL; |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 719 | } |
| 720 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 721 | static const struct iommu_ops tegra_smmu_ops = { |
| 722 | .capable = tegra_smmu_capable, |
Joerg Roedel | d5f1a81 | 2015-03-26 13:43:12 +0100 | [diff] [blame] | 723 | .domain_alloc = tegra_smmu_domain_alloc, |
| 724 | .domain_free = tegra_smmu_domain_free, |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 725 | .attach_dev = tegra_smmu_attach_dev, |
| 726 | .detach_dev = tegra_smmu_detach_dev, |
| 727 | .add_device = tegra_smmu_add_device, |
| 728 | .remove_device = tegra_smmu_remove_device, |
| 729 | .map = tegra_smmu_map, |
| 730 | .unmap = tegra_smmu_unmap, |
| 731 | .map_sg = default_iommu_map_sg, |
| 732 | .iova_to_phys = tegra_smmu_iova_to_phys, |
| 733 | |
| 734 | .pgsize_bitmap = SZ_4K, |
| 735 | }; |
| 736 | |
| 737 | static void tegra_smmu_ahb_enable(void) |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 738 | { |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 739 | static const struct of_device_id ahb_match[] = { |
| 740 | { .compatible = "nvidia,tegra30-ahb", }, |
| 741 | { } |
| 742 | }; |
| 743 | struct device_node *ahb; |
| 744 | |
| 745 | ahb = of_find_matching_node(NULL, ahb_match); |
| 746 | if (ahb) { |
| 747 | tegra_ahb_enable_smmu(ahb); |
| 748 | of_node_put(ahb); |
| 749 | } |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 750 | } |
| 751 | |
Thierry Reding | d1313e7 | 2015-01-23 09:49:25 +0100 | [diff] [blame] | 752 | static int tegra_smmu_swgroups_show(struct seq_file *s, void *data) |
| 753 | { |
| 754 | struct tegra_smmu *smmu = s->private; |
| 755 | unsigned int i; |
| 756 | u32 value; |
| 757 | |
| 758 | seq_printf(s, "swgroup enabled ASID\n"); |
| 759 | seq_printf(s, "------------------------\n"); |
| 760 | |
| 761 | for (i = 0; i < smmu->soc->num_swgroups; i++) { |
| 762 | const struct tegra_smmu_swgroup *group = &smmu->soc->swgroups[i]; |
| 763 | const char *status; |
| 764 | unsigned int asid; |
| 765 | |
| 766 | value = smmu_readl(smmu, group->reg); |
| 767 | |
| 768 | if (value & SMMU_ASID_ENABLE) |
| 769 | status = "yes"; |
| 770 | else |
| 771 | status = "no"; |
| 772 | |
| 773 | asid = value & SMMU_ASID_MASK; |
| 774 | |
| 775 | seq_printf(s, "%-9s %-7s %#04x\n", group->name, status, |
| 776 | asid); |
| 777 | } |
| 778 | |
| 779 | return 0; |
| 780 | } |
| 781 | |
| 782 | static int tegra_smmu_swgroups_open(struct inode *inode, struct file *file) |
| 783 | { |
| 784 | return single_open(file, tegra_smmu_swgroups_show, inode->i_private); |
| 785 | } |
| 786 | |
| 787 | static const struct file_operations tegra_smmu_swgroups_fops = { |
| 788 | .open = tegra_smmu_swgroups_open, |
| 789 | .read = seq_read, |
| 790 | .llseek = seq_lseek, |
| 791 | .release = single_release, |
| 792 | }; |
| 793 | |
| 794 | static int tegra_smmu_clients_show(struct seq_file *s, void *data) |
| 795 | { |
| 796 | struct tegra_smmu *smmu = s->private; |
| 797 | unsigned int i; |
| 798 | u32 value; |
| 799 | |
| 800 | seq_printf(s, "client enabled\n"); |
| 801 | seq_printf(s, "--------------------\n"); |
| 802 | |
| 803 | for (i = 0; i < smmu->soc->num_clients; i++) { |
| 804 | const struct tegra_mc_client *client = &smmu->soc->clients[i]; |
| 805 | const char *status; |
| 806 | |
| 807 | value = smmu_readl(smmu, client->smmu.reg); |
| 808 | |
| 809 | if (value & BIT(client->smmu.bit)) |
| 810 | status = "yes"; |
| 811 | else |
| 812 | status = "no"; |
| 813 | |
| 814 | seq_printf(s, "%-12s %s\n", client->name, status); |
| 815 | } |
| 816 | |
| 817 | return 0; |
| 818 | } |
| 819 | |
| 820 | static int tegra_smmu_clients_open(struct inode *inode, struct file *file) |
| 821 | { |
| 822 | return single_open(file, tegra_smmu_clients_show, inode->i_private); |
| 823 | } |
| 824 | |
| 825 | static const struct file_operations tegra_smmu_clients_fops = { |
| 826 | .open = tegra_smmu_clients_open, |
| 827 | .read = seq_read, |
| 828 | .llseek = seq_lseek, |
| 829 | .release = single_release, |
| 830 | }; |
| 831 | |
| 832 | static void tegra_smmu_debugfs_init(struct tegra_smmu *smmu) |
| 833 | { |
| 834 | smmu->debugfs = debugfs_create_dir("smmu", NULL); |
| 835 | if (!smmu->debugfs) |
| 836 | return; |
| 837 | |
| 838 | debugfs_create_file("swgroups", S_IRUGO, smmu->debugfs, smmu, |
| 839 | &tegra_smmu_swgroups_fops); |
| 840 | debugfs_create_file("clients", S_IRUGO, smmu->debugfs, smmu, |
| 841 | &tegra_smmu_clients_fops); |
| 842 | } |
| 843 | |
| 844 | static void tegra_smmu_debugfs_exit(struct tegra_smmu *smmu) |
| 845 | { |
| 846 | debugfs_remove_recursive(smmu->debugfs); |
| 847 | } |
| 848 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 849 | struct tegra_smmu *tegra_smmu_probe(struct device *dev, |
| 850 | const struct tegra_smmu_soc *soc, |
| 851 | struct tegra_mc *mc) |
| 852 | { |
| 853 | struct tegra_smmu *smmu; |
| 854 | size_t size; |
| 855 | u32 value; |
| 856 | int err; |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 857 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 858 | /* This can happen on Tegra20 which doesn't have an SMMU */ |
| 859 | if (!soc) |
| 860 | return NULL; |
| 861 | |
| 862 | smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL); |
| 863 | if (!smmu) |
| 864 | return ERR_PTR(-ENOMEM); |
| 865 | |
| 866 | /* |
| 867 | * This is a bit of a hack. Ideally we'd want to simply return this |
| 868 | * value. However the IOMMU registration process will attempt to add |
| 869 | * all devices to the IOMMU when bus_set_iommu() is called. In order |
| 870 | * not to rely on global variables to track the IOMMU instance, we |
| 871 | * set it here so that it can be looked up from the .add_device() |
| 872 | * callback via the IOMMU device's .drvdata field. |
| 873 | */ |
| 874 | mc->smmu = smmu; |
| 875 | |
| 876 | size = BITS_TO_LONGS(soc->num_asids) * sizeof(long); |
| 877 | |
| 878 | smmu->asids = devm_kzalloc(dev, size, GFP_KERNEL); |
| 879 | if (!smmu->asids) |
| 880 | return ERR_PTR(-ENOMEM); |
| 881 | |
| 882 | mutex_init(&smmu->lock); |
| 883 | |
| 884 | smmu->regs = mc->regs; |
| 885 | smmu->soc = soc; |
| 886 | smmu->dev = dev; |
| 887 | smmu->mc = mc; |
| 888 | |
Thierry Reding | 804cb54 | 2015-03-27 11:07:27 +0100 | [diff] [blame] | 889 | smmu->pfn_mask = BIT_MASK(mc->soc->num_address_bits - PAGE_SHIFT) - 1; |
| 890 | dev_dbg(dev, "address bits: %u, PFN mask: %#lx\n", |
| 891 | mc->soc->num_address_bits, smmu->pfn_mask); |
| 892 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 893 | value = SMMU_PTC_CONFIG_ENABLE | SMMU_PTC_CONFIG_INDEX_MAP(0x3f); |
| 894 | |
| 895 | if (soc->supports_request_limit) |
| 896 | value |= SMMU_PTC_CONFIG_REQ_LIMIT(8); |
| 897 | |
| 898 | smmu_writel(smmu, value, SMMU_PTC_CONFIG); |
| 899 | |
| 900 | value = SMMU_TLB_CONFIG_HIT_UNDER_MISS | |
| 901 | SMMU_TLB_CONFIG_ACTIVE_LINES(0x20); |
| 902 | |
| 903 | if (soc->supports_round_robin_arbitration) |
| 904 | value |= SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION; |
| 905 | |
| 906 | smmu_writel(smmu, value, SMMU_TLB_CONFIG); |
| 907 | |
Russell King | b8fe038 | 2015-07-27 13:29:41 +0100 | [diff] [blame] | 908 | smmu_flush_ptc_all(smmu); |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 909 | smmu_flush_tlb(smmu); |
| 910 | smmu_writel(smmu, SMMU_CONFIG_ENABLE, SMMU_CONFIG); |
| 911 | smmu_flush(smmu); |
| 912 | |
| 913 | tegra_smmu_ahb_enable(); |
| 914 | |
| 915 | err = bus_set_iommu(&platform_bus_type, &tegra_smmu_ops); |
| 916 | if (err < 0) |
| 917 | return ERR_PTR(err); |
| 918 | |
Thierry Reding | d1313e7 | 2015-01-23 09:49:25 +0100 | [diff] [blame] | 919 | if (IS_ENABLED(CONFIG_DEBUG_FS)) |
| 920 | tegra_smmu_debugfs_init(smmu); |
| 921 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 922 | return smmu; |
| 923 | } |
Thierry Reding | d1313e7 | 2015-01-23 09:49:25 +0100 | [diff] [blame] | 924 | |
| 925 | void tegra_smmu_remove(struct tegra_smmu *smmu) |
| 926 | { |
| 927 | if (IS_ENABLED(CONFIG_DEBUG_FS)) |
| 928 | tegra_smmu_debugfs_exit(smmu); |
| 929 | } |