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