Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 1 | /* |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 2 | * Copyright (C) 2011-2014 NVIDIA CORPORATION. All rights reserved. |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 3 | * |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
Thierry Reding | 804cb54 | 2015-03-27 11:07:27 +0100 | [diff] [blame] | 9 | #include <linux/bitops.h> |
Thierry Reding | d1313e7 | 2015-01-23 09:49:25 +0100 | [diff] [blame] | 10 | #include <linux/debugfs.h> |
Thierry Reding | bc5e6de | 2013-01-21 11:09:06 +0100 | [diff] [blame] | 11 | #include <linux/err.h> |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 12 | #include <linux/iommu.h> |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 13 | #include <linux/kernel.h> |
Hiroshi Doyu | 0760e8f | 2012-06-25 14:23:55 +0300 | [diff] [blame] | 14 | #include <linux/of.h> |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 15 | #include <linux/of_device.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/slab.h> |
Thierry Reding | 306a7f9 | 2014-07-17 13:17:24 +0200 | [diff] [blame] | 18 | |
| 19 | #include <soc/tegra/ahb.h> |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 20 | #include <soc/tegra/mc.h> |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 21 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 22 | struct tegra_smmu { |
| 23 | void __iomem *regs; |
| 24 | struct device *dev; |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 25 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 26 | struct tegra_mc *mc; |
| 27 | const struct tegra_smmu_soc *soc; |
Stephen Warren | e6bc593 | 2012-09-04 16:36:15 -0600 | [diff] [blame] | 28 | |
Thierry Reding | 804cb54 | 2015-03-27 11:07:27 +0100 | [diff] [blame] | 29 | unsigned long pfn_mask; |
| 30 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 31 | unsigned long *asids; |
| 32 | struct mutex lock; |
Stephen Warren | e6bc593 | 2012-09-04 16:36:15 -0600 | [diff] [blame] | 33 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 34 | struct list_head list; |
Thierry Reding | d1313e7 | 2015-01-23 09:49:25 +0100 | [diff] [blame] | 35 | |
| 36 | struct dentry *debugfs; |
Stephen Warren | e6bc593 | 2012-09-04 16:36:15 -0600 | [diff] [blame] | 37 | }; |
| 38 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 39 | struct tegra_smmu_as { |
Joerg Roedel | d5f1a81 | 2015-03-26 13:43:12 +0100 | [diff] [blame] | 40 | struct iommu_domain domain; |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 41 | struct tegra_smmu *smmu; |
| 42 | unsigned int use_count; |
Russell King | 32924c7 | 2015-07-27 13:29:31 +0100 | [diff] [blame^] | 43 | u32 *count; |
Russell King | 853520f | 2015-07-27 13:29:26 +0100 | [diff] [blame] | 44 | struct page **pts; |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 45 | struct page *pd; |
| 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 | |
Russell King | 32924c7 | 2015-07-27 13:29:31 +0100 | [diff] [blame^] | 268 | as->count = kcalloc(SMMU_NUM_PDE, sizeof(u32), GFP_KERNEL); |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 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) { |
Russell King | 32924c7 | 2015-07-27 13:29:31 +0100 | [diff] [blame^] | 277 | kfree(as->count); |
Russell King | 853520f | 2015-07-27 13:29:26 +0100 | [diff] [blame] | 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 | 471d914 | 2015-03-27 11:07:25 +0100 | [diff] [blame] | 290 | /* setup aperture */ |
Joerg Roedel | 7f65ef0 | 2015-04-02 13:33:19 +0200 | [diff] [blame] | 291 | as->domain.geometry.aperture_start = 0; |
| 292 | as->domain.geometry.aperture_end = 0xffffffff; |
| 293 | as->domain.geometry.force_aperture = true; |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 294 | |
Joerg Roedel | d5f1a81 | 2015-03-26 13:43:12 +0100 | [diff] [blame] | 295 | return &as->domain; |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 296 | } |
| 297 | |
Joerg Roedel | d5f1a81 | 2015-03-26 13:43:12 +0100 | [diff] [blame] | 298 | static void tegra_smmu_domain_free(struct iommu_domain *domain) |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 299 | { |
Joerg Roedel | d5f1a81 | 2015-03-26 13:43:12 +0100 | [diff] [blame] | 300 | struct tegra_smmu_as *as = to_smmu_as(domain); |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 301 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 302 | /* TODO: free page directory and page tables */ |
| 303 | ClearPageReserved(as->pd); |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 304 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 305 | kfree(as); |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 306 | } |
| 307 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 308 | static const struct tegra_smmu_swgroup * |
| 309 | tegra_smmu_find_swgroup(struct tegra_smmu *smmu, unsigned int swgroup) |
Hiroshi Doyu | 39abf8a | 2012-08-02 11:46:40 +0300 | [diff] [blame] | 310 | { |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 311 | const struct tegra_smmu_swgroup *group = NULL; |
| 312 | unsigned int i; |
Hiroshi Doyu | 39abf8a | 2012-08-02 11:46:40 +0300 | [diff] [blame] | 313 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 314 | for (i = 0; i < smmu->soc->num_swgroups; i++) { |
| 315 | if (smmu->soc->swgroups[i].swgroup == swgroup) { |
| 316 | group = &smmu->soc->swgroups[i]; |
Hiroshi Doyu | 39abf8a | 2012-08-02 11:46:40 +0300 | [diff] [blame] | 317 | break; |
Hiroshi Doyu | 39abf8a | 2012-08-02 11:46:40 +0300 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 321 | return group; |
Hiroshi Doyu | 39abf8a | 2012-08-02 11:46:40 +0300 | [diff] [blame] | 322 | } |
| 323 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 324 | static void tegra_smmu_enable(struct tegra_smmu *smmu, unsigned int swgroup, |
| 325 | unsigned int asid) |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 326 | { |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 327 | const struct tegra_smmu_swgroup *group; |
| 328 | unsigned int i; |
| 329 | u32 value; |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 330 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 331 | for (i = 0; i < smmu->soc->num_clients; i++) { |
| 332 | const struct tegra_mc_client *client = &smmu->soc->clients[i]; |
| 333 | |
| 334 | if (client->swgroup != swgroup) |
| 335 | continue; |
| 336 | |
| 337 | value = smmu_readl(smmu, client->smmu.reg); |
| 338 | value |= BIT(client->smmu.bit); |
| 339 | smmu_writel(smmu, value, client->smmu.reg); |
| 340 | } |
| 341 | |
| 342 | group = tegra_smmu_find_swgroup(smmu, swgroup); |
| 343 | if (group) { |
| 344 | value = smmu_readl(smmu, group->reg); |
| 345 | value &= ~SMMU_ASID_MASK; |
| 346 | value |= SMMU_ASID_VALUE(asid); |
| 347 | value |= SMMU_ASID_ENABLE; |
| 348 | smmu_writel(smmu, value, group->reg); |
| 349 | } |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 350 | } |
| 351 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 352 | static void tegra_smmu_disable(struct tegra_smmu *smmu, unsigned int swgroup, |
| 353 | unsigned int asid) |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 354 | { |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 355 | const struct tegra_smmu_swgroup *group; |
| 356 | unsigned int i; |
| 357 | u32 value; |
| 358 | |
| 359 | group = tegra_smmu_find_swgroup(smmu, swgroup); |
| 360 | if (group) { |
| 361 | value = smmu_readl(smmu, group->reg); |
| 362 | value &= ~SMMU_ASID_MASK; |
| 363 | value |= SMMU_ASID_VALUE(asid); |
| 364 | value &= ~SMMU_ASID_ENABLE; |
| 365 | smmu_writel(smmu, value, group->reg); |
| 366 | } |
| 367 | |
| 368 | for (i = 0; i < smmu->soc->num_clients; i++) { |
| 369 | const struct tegra_mc_client *client = &smmu->soc->clients[i]; |
| 370 | |
| 371 | if (client->swgroup != swgroup) |
| 372 | continue; |
| 373 | |
| 374 | value = smmu_readl(smmu, client->smmu.reg); |
| 375 | value &= ~BIT(client->smmu.bit); |
| 376 | smmu_writel(smmu, value, client->smmu.reg); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | static int tegra_smmu_as_prepare(struct tegra_smmu *smmu, |
| 381 | struct tegra_smmu_as *as) |
| 382 | { |
| 383 | u32 value; |
Hiroshi Doyu | 0760e8f | 2012-06-25 14:23:55 +0300 | [diff] [blame] | 384 | int err; |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 385 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 386 | if (as->use_count > 0) { |
| 387 | as->use_count++; |
| 388 | return 0; |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 389 | } |
| 390 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 391 | err = tegra_smmu_alloc_asid(smmu, &as->id); |
| 392 | if (err < 0) |
Hiroshi Doyu | 0547c2f | 2012-06-25 14:23:57 +0300 | [diff] [blame] | 393 | return err; |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 394 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 395 | smmu->soc->ops->flush_dcache(as->pd, 0, SMMU_SIZE_PD); |
| 396 | smmu_flush_ptc(smmu, as->pd, 0); |
| 397 | smmu_flush_tlb_asid(smmu, as->id); |
| 398 | |
| 399 | smmu_writel(smmu, as->id & 0x7f, SMMU_PTB_ASID); |
| 400 | value = SMMU_PTB_DATA_VALUE(as->pd, as->attr); |
| 401 | smmu_writel(smmu, value, SMMU_PTB_DATA); |
| 402 | smmu_flush(smmu); |
| 403 | |
| 404 | as->smmu = smmu; |
| 405 | as->use_count++; |
| 406 | |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | static void tegra_smmu_as_unprepare(struct tegra_smmu *smmu, |
| 411 | struct tegra_smmu_as *as) |
| 412 | { |
| 413 | if (--as->use_count > 0) |
| 414 | return; |
| 415 | |
| 416 | tegra_smmu_free_asid(smmu, as->id); |
| 417 | as->smmu = NULL; |
| 418 | } |
| 419 | |
| 420 | static int tegra_smmu_attach_dev(struct iommu_domain *domain, |
| 421 | struct device *dev) |
| 422 | { |
| 423 | struct tegra_smmu *smmu = dev->archdata.iommu; |
Joerg Roedel | d5f1a81 | 2015-03-26 13:43:12 +0100 | [diff] [blame] | 424 | struct tegra_smmu_as *as = to_smmu_as(domain); |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 425 | struct device_node *np = dev->of_node; |
| 426 | struct of_phandle_args args; |
| 427 | unsigned int index = 0; |
| 428 | int err = 0; |
| 429 | |
| 430 | while (!of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index, |
| 431 | &args)) { |
| 432 | unsigned int swgroup = args.args[0]; |
| 433 | |
| 434 | if (args.np != smmu->dev->of_node) { |
| 435 | of_node_put(args.np); |
| 436 | continue; |
| 437 | } |
| 438 | |
| 439 | of_node_put(args.np); |
| 440 | |
| 441 | err = tegra_smmu_as_prepare(smmu, as); |
| 442 | if (err < 0) |
| 443 | return err; |
| 444 | |
| 445 | tegra_smmu_enable(smmu, swgroup, as->id); |
| 446 | index++; |
| 447 | } |
| 448 | |
| 449 | if (index == 0) |
| 450 | return -ENODEV; |
| 451 | |
| 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | static void tegra_smmu_detach_dev(struct iommu_domain *domain, struct device *dev) |
| 456 | { |
Joerg Roedel | d5f1a81 | 2015-03-26 13:43:12 +0100 | [diff] [blame] | 457 | struct tegra_smmu_as *as = to_smmu_as(domain); |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 458 | struct device_node *np = dev->of_node; |
| 459 | struct tegra_smmu *smmu = as->smmu; |
| 460 | struct of_phandle_args args; |
| 461 | unsigned int index = 0; |
| 462 | |
| 463 | while (!of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index, |
| 464 | &args)) { |
| 465 | unsigned int swgroup = args.args[0]; |
| 466 | |
| 467 | if (args.np != smmu->dev->of_node) { |
| 468 | of_node_put(args.np); |
| 469 | continue; |
| 470 | } |
| 471 | |
| 472 | of_node_put(args.np); |
| 473 | |
| 474 | tegra_smmu_disable(smmu, swgroup, as->id); |
| 475 | tegra_smmu_as_unprepare(smmu, as); |
| 476 | index++; |
| 477 | } |
| 478 | } |
| 479 | |
Russell King | 0b42c7c | 2015-07-27 13:29:21 +0100 | [diff] [blame] | 480 | static u32 *tegra_smmu_pte_offset(struct page *pt_page, unsigned long iova) |
| 481 | { |
| 482 | u32 *pt = page_address(pt_page); |
| 483 | |
| 484 | return pt + iova_pt_index(iova); |
| 485 | } |
| 486 | |
| 487 | static u32 *tegra_smmu_pte_lookup(struct tegra_smmu_as *as, unsigned long iova, |
| 488 | struct page **pagep) |
| 489 | { |
| 490 | unsigned int pd_index = iova_pd_index(iova); |
| 491 | struct page *pt_page; |
Russell King | 0b42c7c | 2015-07-27 13:29:21 +0100 | [diff] [blame] | 492 | |
Russell King | 853520f | 2015-07-27 13:29:26 +0100 | [diff] [blame] | 493 | pt_page = as->pts[pd_index]; |
| 494 | if (!pt_page) |
Russell King | 0b42c7c | 2015-07-27 13:29:21 +0100 | [diff] [blame] | 495 | return NULL; |
| 496 | |
Russell King | 0b42c7c | 2015-07-27 13:29:21 +0100 | [diff] [blame] | 497 | *pagep = pt_page; |
| 498 | |
| 499 | return tegra_smmu_pte_offset(pt_page, iova); |
| 500 | } |
| 501 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 502 | static u32 *as_get_pte(struct tegra_smmu_as *as, dma_addr_t iova, |
| 503 | struct page **pagep) |
| 504 | { |
Russell King | 32924c7 | 2015-07-27 13:29:31 +0100 | [diff] [blame^] | 505 | u32 *pd = page_address(as->pd), *pt; |
Russell King | 34d35f8 | 2015-07-27 13:29:16 +0100 | [diff] [blame] | 506 | unsigned int pde = iova_pd_index(iova); |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 507 | struct tegra_smmu *smmu = as->smmu; |
| 508 | struct page *page; |
| 509 | unsigned int i; |
| 510 | |
Russell King | 853520f | 2015-07-27 13:29:26 +0100 | [diff] [blame] | 511 | if (!as->pts[pde]) { |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 512 | page = alloc_page(GFP_KERNEL | __GFP_DMA); |
| 513 | if (!page) |
| 514 | return NULL; |
| 515 | |
| 516 | pt = page_address(page); |
| 517 | SetPageReserved(page); |
| 518 | |
| 519 | for (i = 0; i < SMMU_NUM_PTE; i++) |
| 520 | pt[i] = 0; |
| 521 | |
Russell King | 853520f | 2015-07-27 13:29:26 +0100 | [diff] [blame] | 522 | as->pts[pde] = page; |
| 523 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 524 | smmu->soc->ops->flush_dcache(page, 0, SMMU_SIZE_PT); |
| 525 | |
| 526 | pd[pde] = SMMU_MK_PDE(page, SMMU_PDE_ATTR | SMMU_PDE_NEXT); |
| 527 | |
| 528 | smmu->soc->ops->flush_dcache(as->pd, pde << 2, 4); |
| 529 | smmu_flush_ptc(smmu, as->pd, pde << 2); |
| 530 | smmu_flush_tlb_section(smmu, as->id, iova); |
| 531 | smmu_flush(smmu); |
| 532 | } else { |
Russell King | 853520f | 2015-07-27 13:29:26 +0100 | [diff] [blame] | 533 | page = as->pts[pde]; |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | *pagep = page; |
| 537 | |
Russell King | 0b42c7c | 2015-07-27 13:29:21 +0100 | [diff] [blame] | 538 | pt = page_address(page); |
| 539 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 540 | /* Keep track of entries in this page table. */ |
Russell King | 0b42c7c | 2015-07-27 13:29:21 +0100 | [diff] [blame] | 541 | if (pt[iova_pt_index(iova)] == 0) |
Russell King | 32924c7 | 2015-07-27 13:29:31 +0100 | [diff] [blame^] | 542 | as->count[pde]++; |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 543 | |
Russell King | 0b42c7c | 2015-07-27 13:29:21 +0100 | [diff] [blame] | 544 | return tegra_smmu_pte_offset(page, iova); |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 545 | } |
| 546 | |
Russell King | b98e34f | 2015-07-27 13:29:05 +0100 | [diff] [blame] | 547 | 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] | 548 | { |
Russell King | b98e34f | 2015-07-27 13:29:05 +0100 | [diff] [blame] | 549 | struct tegra_smmu *smmu = as->smmu; |
Russell King | 34d35f8 | 2015-07-27 13:29:16 +0100 | [diff] [blame] | 550 | unsigned int pde = iova_pd_index(iova); |
Russell King | b98e34f | 2015-07-27 13:29:05 +0100 | [diff] [blame] | 551 | u32 *pd = page_address(as->pd); |
Russell King | 853520f | 2015-07-27 13:29:26 +0100 | [diff] [blame] | 552 | struct page *page = as->pts[pde]; |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 553 | |
| 554 | /* |
| 555 | * When no entries in this page table are used anymore, return the |
| 556 | * memory page to the system. |
| 557 | */ |
Russell King | 32924c7 | 2015-07-27 13:29:31 +0100 | [diff] [blame^] | 558 | if (--as->count[pde] == 0) { |
Russell King | b98e34f | 2015-07-27 13:29:05 +0100 | [diff] [blame] | 559 | unsigned int offset = pde * sizeof(*pd); |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 560 | |
Russell King | b98e34f | 2015-07-27 13:29:05 +0100 | [diff] [blame] | 561 | /* Clear the page directory entry first */ |
| 562 | pd[pde] = 0; |
| 563 | |
| 564 | /* Flush the page directory entry */ |
| 565 | smmu->soc->ops->flush_dcache(as->pd, offset, sizeof(*pd)); |
| 566 | smmu_flush_ptc(smmu, as->pd, offset); |
| 567 | smmu_flush_tlb_section(smmu, as->id, iova); |
| 568 | smmu_flush(smmu); |
| 569 | |
| 570 | /* Finally, free the page */ |
| 571 | ClearPageReserved(page); |
| 572 | __free_page(page); |
Russell King | 853520f | 2015-07-27 13:29:26 +0100 | [diff] [blame] | 573 | as->pts[pde] = NULL; |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 574 | } |
| 575 | } |
| 576 | |
Russell King | 8482ee5e | 2015-07-27 13:29:10 +0100 | [diff] [blame] | 577 | static void tegra_smmu_set_pte(struct tegra_smmu_as *as, unsigned long iova, |
| 578 | u32 *pte, struct page *pte_page, u32 val) |
| 579 | { |
| 580 | struct tegra_smmu *smmu = as->smmu; |
| 581 | unsigned long offset = offset_in_page(pte); |
| 582 | |
| 583 | *pte = val; |
| 584 | |
| 585 | smmu->soc->ops->flush_dcache(pte_page, offset, 4); |
| 586 | smmu_flush_ptc(smmu, pte_page, offset); |
| 587 | smmu_flush_tlb_group(smmu, as->id, iova); |
| 588 | smmu_flush(smmu); |
| 589 | } |
| 590 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 591 | static int tegra_smmu_map(struct iommu_domain *domain, unsigned long iova, |
| 592 | phys_addr_t paddr, size_t size, int prot) |
| 593 | { |
Joerg Roedel | d5f1a81 | 2015-03-26 13:43:12 +0100 | [diff] [blame] | 594 | struct tegra_smmu_as *as = to_smmu_as(domain); |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 595 | struct page *page; |
| 596 | u32 *pte; |
| 597 | |
| 598 | pte = as_get_pte(as, iova, &page); |
| 599 | if (!pte) |
Hiroshi Doyu | 0547c2f | 2012-06-25 14:23:57 +0300 | [diff] [blame] | 600 | return -ENOMEM; |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 601 | |
Russell King | 8482ee5e | 2015-07-27 13:29:10 +0100 | [diff] [blame] | 602 | tegra_smmu_set_pte(as, iova, pte, page, |
| 603 | __phys_to_pfn(paddr) | SMMU_PTE_ATTR); |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 604 | |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 605 | return 0; |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 606 | } |
| 607 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 608 | static size_t tegra_smmu_unmap(struct iommu_domain *domain, unsigned long iova, |
| 609 | size_t size) |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 610 | { |
Joerg Roedel | d5f1a81 | 2015-03-26 13:43:12 +0100 | [diff] [blame] | 611 | struct tegra_smmu_as *as = to_smmu_as(domain); |
Russell King | 0b42c7c | 2015-07-27 13:29:21 +0100 | [diff] [blame] | 612 | struct page *pte_page; |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 613 | u32 *pte; |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 614 | |
Russell King | 0b42c7c | 2015-07-27 13:29:21 +0100 | [diff] [blame] | 615 | pte = tegra_smmu_pte_lookup(as, iova, &pte_page); |
Russell King | b98e34f | 2015-07-27 13:29:05 +0100 | [diff] [blame] | 616 | if (!pte || !*pte) |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 617 | return 0; |
Hiroshi Doyu | 39abf8a | 2012-08-02 11:46:40 +0300 | [diff] [blame] | 618 | |
Russell King | 0b42c7c | 2015-07-27 13:29:21 +0100 | [diff] [blame] | 619 | tegra_smmu_set_pte(as, iova, pte, pte_page, 0); |
Russell King | b98e34f | 2015-07-27 13:29:05 +0100 | [diff] [blame] | 620 | tegra_smmu_pte_put_use(as, iova); |
| 621 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 622 | return size; |
| 623 | } |
| 624 | |
| 625 | static phys_addr_t tegra_smmu_iova_to_phys(struct iommu_domain *domain, |
| 626 | dma_addr_t iova) |
| 627 | { |
Joerg Roedel | d5f1a81 | 2015-03-26 13:43:12 +0100 | [diff] [blame] | 628 | struct tegra_smmu_as *as = to_smmu_as(domain); |
Russell King | 0b42c7c | 2015-07-27 13:29:21 +0100 | [diff] [blame] | 629 | struct page *pte_page; |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 630 | unsigned long pfn; |
| 631 | u32 *pte; |
| 632 | |
Russell King | 0b42c7c | 2015-07-27 13:29:21 +0100 | [diff] [blame] | 633 | pte = tegra_smmu_pte_lookup(as, iova, &pte_page); |
Russell King | 9113785 | 2015-07-27 13:29:00 +0100 | [diff] [blame] | 634 | if (!pte || !*pte) |
| 635 | return 0; |
| 636 | |
Thierry Reding | 804cb54 | 2015-03-27 11:07:27 +0100 | [diff] [blame] | 637 | pfn = *pte & as->smmu->pfn_mask; |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 638 | |
| 639 | return PFN_PHYS(pfn); |
| 640 | } |
| 641 | |
| 642 | static struct tegra_smmu *tegra_smmu_find(struct device_node *np) |
| 643 | { |
| 644 | struct platform_device *pdev; |
| 645 | struct tegra_mc *mc; |
| 646 | |
| 647 | pdev = of_find_device_by_node(np); |
| 648 | if (!pdev) |
| 649 | return NULL; |
| 650 | |
| 651 | mc = platform_get_drvdata(pdev); |
| 652 | if (!mc) |
| 653 | return NULL; |
| 654 | |
| 655 | return mc->smmu; |
| 656 | } |
| 657 | |
| 658 | static int tegra_smmu_add_device(struct device *dev) |
| 659 | { |
| 660 | struct device_node *np = dev->of_node; |
| 661 | struct of_phandle_args args; |
| 662 | unsigned int index = 0; |
| 663 | |
| 664 | while (of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index, |
| 665 | &args) == 0) { |
| 666 | struct tegra_smmu *smmu; |
| 667 | |
| 668 | smmu = tegra_smmu_find(args.np); |
| 669 | if (smmu) { |
| 670 | /* |
| 671 | * Only a single IOMMU master interface is currently |
| 672 | * supported by the Linux kernel, so abort after the |
| 673 | * first match. |
| 674 | */ |
| 675 | dev->archdata.iommu = smmu; |
| 676 | break; |
| 677 | } |
| 678 | |
| 679 | index++; |
| 680 | } |
| 681 | |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 682 | return 0; |
| 683 | } |
| 684 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 685 | static void tegra_smmu_remove_device(struct device *dev) |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 686 | { |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 687 | dev->archdata.iommu = NULL; |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 688 | } |
| 689 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 690 | static const struct iommu_ops tegra_smmu_ops = { |
| 691 | .capable = tegra_smmu_capable, |
Joerg Roedel | d5f1a81 | 2015-03-26 13:43:12 +0100 | [diff] [blame] | 692 | .domain_alloc = tegra_smmu_domain_alloc, |
| 693 | .domain_free = tegra_smmu_domain_free, |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 694 | .attach_dev = tegra_smmu_attach_dev, |
| 695 | .detach_dev = tegra_smmu_detach_dev, |
| 696 | .add_device = tegra_smmu_add_device, |
| 697 | .remove_device = tegra_smmu_remove_device, |
| 698 | .map = tegra_smmu_map, |
| 699 | .unmap = tegra_smmu_unmap, |
| 700 | .map_sg = default_iommu_map_sg, |
| 701 | .iova_to_phys = tegra_smmu_iova_to_phys, |
| 702 | |
| 703 | .pgsize_bitmap = SZ_4K, |
| 704 | }; |
| 705 | |
| 706 | static void tegra_smmu_ahb_enable(void) |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 707 | { |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 708 | static const struct of_device_id ahb_match[] = { |
| 709 | { .compatible = "nvidia,tegra30-ahb", }, |
| 710 | { } |
| 711 | }; |
| 712 | struct device_node *ahb; |
| 713 | |
| 714 | ahb = of_find_matching_node(NULL, ahb_match); |
| 715 | if (ahb) { |
| 716 | tegra_ahb_enable_smmu(ahb); |
| 717 | of_node_put(ahb); |
| 718 | } |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 719 | } |
| 720 | |
Thierry Reding | d1313e7 | 2015-01-23 09:49:25 +0100 | [diff] [blame] | 721 | static int tegra_smmu_swgroups_show(struct seq_file *s, void *data) |
| 722 | { |
| 723 | struct tegra_smmu *smmu = s->private; |
| 724 | unsigned int i; |
| 725 | u32 value; |
| 726 | |
| 727 | seq_printf(s, "swgroup enabled ASID\n"); |
| 728 | seq_printf(s, "------------------------\n"); |
| 729 | |
| 730 | for (i = 0; i < smmu->soc->num_swgroups; i++) { |
| 731 | const struct tegra_smmu_swgroup *group = &smmu->soc->swgroups[i]; |
| 732 | const char *status; |
| 733 | unsigned int asid; |
| 734 | |
| 735 | value = smmu_readl(smmu, group->reg); |
| 736 | |
| 737 | if (value & SMMU_ASID_ENABLE) |
| 738 | status = "yes"; |
| 739 | else |
| 740 | status = "no"; |
| 741 | |
| 742 | asid = value & SMMU_ASID_MASK; |
| 743 | |
| 744 | seq_printf(s, "%-9s %-7s %#04x\n", group->name, status, |
| 745 | asid); |
| 746 | } |
| 747 | |
| 748 | return 0; |
| 749 | } |
| 750 | |
| 751 | static int tegra_smmu_swgroups_open(struct inode *inode, struct file *file) |
| 752 | { |
| 753 | return single_open(file, tegra_smmu_swgroups_show, inode->i_private); |
| 754 | } |
| 755 | |
| 756 | static const struct file_operations tegra_smmu_swgroups_fops = { |
| 757 | .open = tegra_smmu_swgroups_open, |
| 758 | .read = seq_read, |
| 759 | .llseek = seq_lseek, |
| 760 | .release = single_release, |
| 761 | }; |
| 762 | |
| 763 | static int tegra_smmu_clients_show(struct seq_file *s, void *data) |
| 764 | { |
| 765 | struct tegra_smmu *smmu = s->private; |
| 766 | unsigned int i; |
| 767 | u32 value; |
| 768 | |
| 769 | seq_printf(s, "client enabled\n"); |
| 770 | seq_printf(s, "--------------------\n"); |
| 771 | |
| 772 | for (i = 0; i < smmu->soc->num_clients; i++) { |
| 773 | const struct tegra_mc_client *client = &smmu->soc->clients[i]; |
| 774 | const char *status; |
| 775 | |
| 776 | value = smmu_readl(smmu, client->smmu.reg); |
| 777 | |
| 778 | if (value & BIT(client->smmu.bit)) |
| 779 | status = "yes"; |
| 780 | else |
| 781 | status = "no"; |
| 782 | |
| 783 | seq_printf(s, "%-12s %s\n", client->name, status); |
| 784 | } |
| 785 | |
| 786 | return 0; |
| 787 | } |
| 788 | |
| 789 | static int tegra_smmu_clients_open(struct inode *inode, struct file *file) |
| 790 | { |
| 791 | return single_open(file, tegra_smmu_clients_show, inode->i_private); |
| 792 | } |
| 793 | |
| 794 | static const struct file_operations tegra_smmu_clients_fops = { |
| 795 | .open = tegra_smmu_clients_open, |
| 796 | .read = seq_read, |
| 797 | .llseek = seq_lseek, |
| 798 | .release = single_release, |
| 799 | }; |
| 800 | |
| 801 | static void tegra_smmu_debugfs_init(struct tegra_smmu *smmu) |
| 802 | { |
| 803 | smmu->debugfs = debugfs_create_dir("smmu", NULL); |
| 804 | if (!smmu->debugfs) |
| 805 | return; |
| 806 | |
| 807 | debugfs_create_file("swgroups", S_IRUGO, smmu->debugfs, smmu, |
| 808 | &tegra_smmu_swgroups_fops); |
| 809 | debugfs_create_file("clients", S_IRUGO, smmu->debugfs, smmu, |
| 810 | &tegra_smmu_clients_fops); |
| 811 | } |
| 812 | |
| 813 | static void tegra_smmu_debugfs_exit(struct tegra_smmu *smmu) |
| 814 | { |
| 815 | debugfs_remove_recursive(smmu->debugfs); |
| 816 | } |
| 817 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 818 | struct tegra_smmu *tegra_smmu_probe(struct device *dev, |
| 819 | const struct tegra_smmu_soc *soc, |
| 820 | struct tegra_mc *mc) |
| 821 | { |
| 822 | struct tegra_smmu *smmu; |
| 823 | size_t size; |
| 824 | u32 value; |
| 825 | int err; |
Hiroshi DOYU | 7a31f6f | 2011-11-17 07:31:31 +0200 | [diff] [blame] | 826 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 827 | /* This can happen on Tegra20 which doesn't have an SMMU */ |
| 828 | if (!soc) |
| 829 | return NULL; |
| 830 | |
| 831 | smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL); |
| 832 | if (!smmu) |
| 833 | return ERR_PTR(-ENOMEM); |
| 834 | |
| 835 | /* |
| 836 | * This is a bit of a hack. Ideally we'd want to simply return this |
| 837 | * value. However the IOMMU registration process will attempt to add |
| 838 | * all devices to the IOMMU when bus_set_iommu() is called. In order |
| 839 | * not to rely on global variables to track the IOMMU instance, we |
| 840 | * set it here so that it can be looked up from the .add_device() |
| 841 | * callback via the IOMMU device's .drvdata field. |
| 842 | */ |
| 843 | mc->smmu = smmu; |
| 844 | |
| 845 | size = BITS_TO_LONGS(soc->num_asids) * sizeof(long); |
| 846 | |
| 847 | smmu->asids = devm_kzalloc(dev, size, GFP_KERNEL); |
| 848 | if (!smmu->asids) |
| 849 | return ERR_PTR(-ENOMEM); |
| 850 | |
| 851 | mutex_init(&smmu->lock); |
| 852 | |
| 853 | smmu->regs = mc->regs; |
| 854 | smmu->soc = soc; |
| 855 | smmu->dev = dev; |
| 856 | smmu->mc = mc; |
| 857 | |
Thierry Reding | 804cb54 | 2015-03-27 11:07:27 +0100 | [diff] [blame] | 858 | smmu->pfn_mask = BIT_MASK(mc->soc->num_address_bits - PAGE_SHIFT) - 1; |
| 859 | dev_dbg(dev, "address bits: %u, PFN mask: %#lx\n", |
| 860 | mc->soc->num_address_bits, smmu->pfn_mask); |
| 861 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 862 | value = SMMU_PTC_CONFIG_ENABLE | SMMU_PTC_CONFIG_INDEX_MAP(0x3f); |
| 863 | |
| 864 | if (soc->supports_request_limit) |
| 865 | value |= SMMU_PTC_CONFIG_REQ_LIMIT(8); |
| 866 | |
| 867 | smmu_writel(smmu, value, SMMU_PTC_CONFIG); |
| 868 | |
| 869 | value = SMMU_TLB_CONFIG_HIT_UNDER_MISS | |
| 870 | SMMU_TLB_CONFIG_ACTIVE_LINES(0x20); |
| 871 | |
| 872 | if (soc->supports_round_robin_arbitration) |
| 873 | value |= SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION; |
| 874 | |
| 875 | smmu_writel(smmu, value, SMMU_TLB_CONFIG); |
| 876 | |
| 877 | smmu_flush_ptc(smmu, NULL, 0); |
| 878 | smmu_flush_tlb(smmu); |
| 879 | smmu_writel(smmu, SMMU_CONFIG_ENABLE, SMMU_CONFIG); |
| 880 | smmu_flush(smmu); |
| 881 | |
| 882 | tegra_smmu_ahb_enable(); |
| 883 | |
| 884 | err = bus_set_iommu(&platform_bus_type, &tegra_smmu_ops); |
| 885 | if (err < 0) |
| 886 | return ERR_PTR(err); |
| 887 | |
Thierry Reding | d1313e7 | 2015-01-23 09:49:25 +0100 | [diff] [blame] | 888 | if (IS_ENABLED(CONFIG_DEBUG_FS)) |
| 889 | tegra_smmu_debugfs_init(smmu); |
| 890 | |
Thierry Reding | 8918465 | 2014-04-16 09:24:44 +0200 | [diff] [blame] | 891 | return smmu; |
| 892 | } |
Thierry Reding | d1313e7 | 2015-01-23 09:49:25 +0100 | [diff] [blame] | 893 | |
| 894 | void tegra_smmu_remove(struct tegra_smmu *smmu) |
| 895 | { |
| 896 | if (IS_ENABLED(CONFIG_DEBUG_FS)) |
| 897 | tegra_smmu_debugfs_exit(smmu); |
| 898 | } |