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