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