Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 1 | /* |
| 2 | * IPMMU VMSA |
| 3 | * |
| 4 | * Copyright (C) 2014 Renesas Electronics Corporation |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | */ |
| 10 | |
Magnus Damm | dbb7069 | 2017-05-17 19:06:38 +0900 | [diff] [blame] | 11 | #include <linux/bitmap.h> |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 12 | #include <linux/delay.h> |
Magnus Damm | 3ae4729 | 2017-05-17 19:07:10 +0900 | [diff] [blame] | 13 | #include <linux/dma-iommu.h> |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 14 | #include <linux/dma-mapping.h> |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/export.h> |
| 17 | #include <linux/interrupt.h> |
| 18 | #include <linux/io.h> |
| 19 | #include <linux/iommu.h> |
| 20 | #include <linux/module.h> |
Laurent Pinchart | 275f505 | 2014-03-17 01:02:46 +0100 | [diff] [blame] | 21 | #include <linux/of.h> |
Magnus Damm | 33f3ac9 | 2017-10-16 21:29:25 +0900 | [diff] [blame^] | 22 | #include <linux/of_device.h> |
Magnus Damm | 7b2d596 | 2017-07-17 22:05:41 +0900 | [diff] [blame] | 23 | #include <linux/of_platform.h> |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 24 | #include <linux/platform_device.h> |
| 25 | #include <linux/sizes.h> |
| 26 | #include <linux/slab.h> |
| 27 | |
Magnus Damm | 3ae4729 | 2017-05-17 19:07:10 +0900 | [diff] [blame] | 28 | #if defined(CONFIG_ARM) && !defined(CONFIG_IOMMU_DMA) |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 29 | #include <asm/dma-iommu.h> |
| 30 | #include <asm/pgalloc.h> |
Robin Murphy | 49c875f | 2017-10-13 19:23:42 +0100 | [diff] [blame] | 31 | #else |
| 32 | #define arm_iommu_create_mapping(...) NULL |
| 33 | #define arm_iommu_attach_device(...) -ENODEV |
| 34 | #define arm_iommu_release_mapping(...) do {} while (0) |
| 35 | #define arm_iommu_detach_device(...) do {} while (0) |
Magnus Damm | 3ae4729 | 2017-05-17 19:07:10 +0900 | [diff] [blame] | 36 | #endif |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 37 | |
Laurent Pinchart | f20ed39 | 2015-01-20 18:30:04 +0200 | [diff] [blame] | 38 | #include "io-pgtable.h" |
| 39 | |
Magnus Damm | dbb7069 | 2017-05-17 19:06:38 +0900 | [diff] [blame] | 40 | #define IPMMU_CTX_MAX 1 |
| 41 | |
Magnus Damm | 33f3ac9 | 2017-10-16 21:29:25 +0900 | [diff] [blame^] | 42 | struct ipmmu_features { |
| 43 | bool use_ns_alias_offset; |
| 44 | }; |
| 45 | |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 46 | struct ipmmu_vmsa_device { |
| 47 | struct device *dev; |
| 48 | void __iomem *base; |
Magnus Damm | 01da21e | 2017-07-17 22:05:10 +0900 | [diff] [blame] | 49 | struct iommu_device iommu; |
Magnus Damm | 33f3ac9 | 2017-10-16 21:29:25 +0900 | [diff] [blame^] | 50 | const struct ipmmu_features *features; |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 51 | unsigned int num_utlbs; |
Magnus Damm | dbb7069 | 2017-05-17 19:06:38 +0900 | [diff] [blame] | 52 | spinlock_t lock; /* Protects ctx and domains[] */ |
| 53 | DECLARE_BITMAP(ctx, IPMMU_CTX_MAX); |
| 54 | struct ipmmu_vmsa_domain *domains[IPMMU_CTX_MAX]; |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 55 | |
Robin Murphy | b354c73 | 2017-10-13 19:23:40 +0100 | [diff] [blame] | 56 | struct iommu_group *group; |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 57 | struct dma_iommu_mapping *mapping; |
| 58 | }; |
| 59 | |
| 60 | struct ipmmu_vmsa_domain { |
| 61 | struct ipmmu_vmsa_device *mmu; |
Joerg Roedel | 5914c5f | 2015-03-26 13:43:16 +0100 | [diff] [blame] | 62 | struct iommu_domain io_domain; |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 63 | |
Laurent Pinchart | f20ed39 | 2015-01-20 18:30:04 +0200 | [diff] [blame] | 64 | struct io_pgtable_cfg cfg; |
| 65 | struct io_pgtable_ops *iop; |
| 66 | |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 67 | unsigned int context_id; |
| 68 | spinlock_t lock; /* Protects mappings */ |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 69 | }; |
| 70 | |
Joerg Roedel | 5914c5f | 2015-03-26 13:43:16 +0100 | [diff] [blame] | 71 | static struct ipmmu_vmsa_domain *to_vmsa_domain(struct iommu_domain *dom) |
| 72 | { |
| 73 | return container_of(dom, struct ipmmu_vmsa_domain, io_domain); |
| 74 | } |
| 75 | |
Robin Murphy | e4efe4a | 2017-10-13 19:23:41 +0100 | [diff] [blame] | 76 | static struct ipmmu_vmsa_device *to_ipmmu(struct device *dev) |
Magnus Damm | 0fbc8b0 | 2017-05-17 19:07:20 +0900 | [diff] [blame] | 77 | { |
Robin Murphy | 3c49ed3 | 2017-07-17 22:05:31 +0900 | [diff] [blame] | 78 | return dev->iommu_fwspec ? dev->iommu_fwspec->iommu_priv : NULL; |
Magnus Damm | 0fbc8b0 | 2017-05-17 19:07:20 +0900 | [diff] [blame] | 79 | } |
| 80 | |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 81 | #define TLB_LOOP_TIMEOUT 100 /* 100us */ |
| 82 | |
| 83 | /* ----------------------------------------------------------------------------- |
| 84 | * Registers Definition |
| 85 | */ |
| 86 | |
Laurent Pinchart | 275f505 | 2014-03-17 01:02:46 +0100 | [diff] [blame] | 87 | #define IM_NS_ALIAS_OFFSET 0x800 |
| 88 | |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 89 | #define IM_CTX_SIZE 0x40 |
| 90 | |
| 91 | #define IMCTR 0x0000 |
| 92 | #define IMCTR_TRE (1 << 17) |
| 93 | #define IMCTR_AFE (1 << 16) |
| 94 | #define IMCTR_RTSEL_MASK (3 << 4) |
| 95 | #define IMCTR_RTSEL_SHIFT 4 |
| 96 | #define IMCTR_TREN (1 << 3) |
| 97 | #define IMCTR_INTEN (1 << 2) |
| 98 | #define IMCTR_FLUSH (1 << 1) |
| 99 | #define IMCTR_MMUEN (1 << 0) |
| 100 | |
| 101 | #define IMCAAR 0x0004 |
| 102 | |
| 103 | #define IMTTBCR 0x0008 |
| 104 | #define IMTTBCR_EAE (1 << 31) |
| 105 | #define IMTTBCR_PMB (1 << 30) |
| 106 | #define IMTTBCR_SH1_NON_SHAREABLE (0 << 28) |
| 107 | #define IMTTBCR_SH1_OUTER_SHAREABLE (2 << 28) |
| 108 | #define IMTTBCR_SH1_INNER_SHAREABLE (3 << 28) |
| 109 | #define IMTTBCR_SH1_MASK (3 << 28) |
| 110 | #define IMTTBCR_ORGN1_NC (0 << 26) |
| 111 | #define IMTTBCR_ORGN1_WB_WA (1 << 26) |
| 112 | #define IMTTBCR_ORGN1_WT (2 << 26) |
| 113 | #define IMTTBCR_ORGN1_WB (3 << 26) |
| 114 | #define IMTTBCR_ORGN1_MASK (3 << 26) |
| 115 | #define IMTTBCR_IRGN1_NC (0 << 24) |
| 116 | #define IMTTBCR_IRGN1_WB_WA (1 << 24) |
| 117 | #define IMTTBCR_IRGN1_WT (2 << 24) |
| 118 | #define IMTTBCR_IRGN1_WB (3 << 24) |
| 119 | #define IMTTBCR_IRGN1_MASK (3 << 24) |
| 120 | #define IMTTBCR_TSZ1_MASK (7 << 16) |
| 121 | #define IMTTBCR_TSZ1_SHIFT 16 |
| 122 | #define IMTTBCR_SH0_NON_SHAREABLE (0 << 12) |
| 123 | #define IMTTBCR_SH0_OUTER_SHAREABLE (2 << 12) |
| 124 | #define IMTTBCR_SH0_INNER_SHAREABLE (3 << 12) |
| 125 | #define IMTTBCR_SH0_MASK (3 << 12) |
| 126 | #define IMTTBCR_ORGN0_NC (0 << 10) |
| 127 | #define IMTTBCR_ORGN0_WB_WA (1 << 10) |
| 128 | #define IMTTBCR_ORGN0_WT (2 << 10) |
| 129 | #define IMTTBCR_ORGN0_WB (3 << 10) |
| 130 | #define IMTTBCR_ORGN0_MASK (3 << 10) |
| 131 | #define IMTTBCR_IRGN0_NC (0 << 8) |
| 132 | #define IMTTBCR_IRGN0_WB_WA (1 << 8) |
| 133 | #define IMTTBCR_IRGN0_WT (2 << 8) |
| 134 | #define IMTTBCR_IRGN0_WB (3 << 8) |
| 135 | #define IMTTBCR_IRGN0_MASK (3 << 8) |
| 136 | #define IMTTBCR_SL0_LVL_2 (0 << 4) |
| 137 | #define IMTTBCR_SL0_LVL_1 (1 << 4) |
| 138 | #define IMTTBCR_TSZ0_MASK (7 << 0) |
| 139 | #define IMTTBCR_TSZ0_SHIFT O |
| 140 | |
| 141 | #define IMBUSCR 0x000c |
| 142 | #define IMBUSCR_DVM (1 << 2) |
| 143 | #define IMBUSCR_BUSSEL_SYS (0 << 0) |
| 144 | #define IMBUSCR_BUSSEL_CCI (1 << 0) |
| 145 | #define IMBUSCR_BUSSEL_IMCAAR (2 << 0) |
| 146 | #define IMBUSCR_BUSSEL_CCI_IMCAAR (3 << 0) |
| 147 | #define IMBUSCR_BUSSEL_MASK (3 << 0) |
| 148 | |
| 149 | #define IMTTLBR0 0x0010 |
| 150 | #define IMTTUBR0 0x0014 |
| 151 | #define IMTTLBR1 0x0018 |
| 152 | #define IMTTUBR1 0x001c |
| 153 | |
| 154 | #define IMSTR 0x0020 |
| 155 | #define IMSTR_ERRLVL_MASK (3 << 12) |
| 156 | #define IMSTR_ERRLVL_SHIFT 12 |
| 157 | #define IMSTR_ERRCODE_TLB_FORMAT (1 << 8) |
| 158 | #define IMSTR_ERRCODE_ACCESS_PERM (4 << 8) |
| 159 | #define IMSTR_ERRCODE_SECURE_ACCESS (5 << 8) |
| 160 | #define IMSTR_ERRCODE_MASK (7 << 8) |
| 161 | #define IMSTR_MHIT (1 << 4) |
| 162 | #define IMSTR_ABORT (1 << 2) |
| 163 | #define IMSTR_PF (1 << 1) |
| 164 | #define IMSTR_TF (1 << 0) |
| 165 | |
| 166 | #define IMMAIR0 0x0028 |
| 167 | #define IMMAIR1 0x002c |
| 168 | #define IMMAIR_ATTR_MASK 0xff |
| 169 | #define IMMAIR_ATTR_DEVICE 0x04 |
| 170 | #define IMMAIR_ATTR_NC 0x44 |
| 171 | #define IMMAIR_ATTR_WBRWA 0xff |
| 172 | #define IMMAIR_ATTR_SHIFT(n) ((n) << 3) |
| 173 | #define IMMAIR_ATTR_IDX_NC 0 |
| 174 | #define IMMAIR_ATTR_IDX_WBRWA 1 |
| 175 | #define IMMAIR_ATTR_IDX_DEV 2 |
| 176 | |
| 177 | #define IMEAR 0x0030 |
| 178 | |
| 179 | #define IMPCTR 0x0200 |
| 180 | #define IMPSTR 0x0208 |
| 181 | #define IMPEAR 0x020c |
| 182 | #define IMPMBA(n) (0x0280 + ((n) * 4)) |
| 183 | #define IMPMBD(n) (0x02c0 + ((n) * 4)) |
| 184 | |
| 185 | #define IMUCTR(n) (0x0300 + ((n) * 16)) |
| 186 | #define IMUCTR_FIXADDEN (1 << 31) |
| 187 | #define IMUCTR_FIXADD_MASK (0xff << 16) |
| 188 | #define IMUCTR_FIXADD_SHIFT 16 |
| 189 | #define IMUCTR_TTSEL_MMU(n) ((n) << 4) |
| 190 | #define IMUCTR_TTSEL_PMB (8 << 4) |
| 191 | #define IMUCTR_TTSEL_MASK (15 << 4) |
| 192 | #define IMUCTR_FLUSH (1 << 1) |
| 193 | #define IMUCTR_MMUEN (1 << 0) |
| 194 | |
| 195 | #define IMUASID(n) (0x0308 + ((n) * 16)) |
| 196 | #define IMUASID_ASID8_MASK (0xff << 8) |
| 197 | #define IMUASID_ASID8_SHIFT 8 |
| 198 | #define IMUASID_ASID0_MASK (0xff << 0) |
| 199 | #define IMUASID_ASID0_SHIFT 0 |
| 200 | |
| 201 | /* ----------------------------------------------------------------------------- |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 202 | * Read/Write Access |
| 203 | */ |
| 204 | |
| 205 | static u32 ipmmu_read(struct ipmmu_vmsa_device *mmu, unsigned int offset) |
| 206 | { |
| 207 | return ioread32(mmu->base + offset); |
| 208 | } |
| 209 | |
| 210 | static void ipmmu_write(struct ipmmu_vmsa_device *mmu, unsigned int offset, |
| 211 | u32 data) |
| 212 | { |
| 213 | iowrite32(data, mmu->base + offset); |
| 214 | } |
| 215 | |
| 216 | static u32 ipmmu_ctx_read(struct ipmmu_vmsa_domain *domain, unsigned int reg) |
| 217 | { |
| 218 | return ipmmu_read(domain->mmu, domain->context_id * IM_CTX_SIZE + reg); |
| 219 | } |
| 220 | |
| 221 | static void ipmmu_ctx_write(struct ipmmu_vmsa_domain *domain, unsigned int reg, |
| 222 | u32 data) |
| 223 | { |
| 224 | ipmmu_write(domain->mmu, domain->context_id * IM_CTX_SIZE + reg, data); |
| 225 | } |
| 226 | |
| 227 | /* ----------------------------------------------------------------------------- |
| 228 | * TLB and microTLB Management |
| 229 | */ |
| 230 | |
| 231 | /* Wait for any pending TLB invalidations to complete */ |
| 232 | static void ipmmu_tlb_sync(struct ipmmu_vmsa_domain *domain) |
| 233 | { |
| 234 | unsigned int count = 0; |
| 235 | |
| 236 | while (ipmmu_ctx_read(domain, IMCTR) & IMCTR_FLUSH) { |
| 237 | cpu_relax(); |
| 238 | if (++count == TLB_LOOP_TIMEOUT) { |
| 239 | dev_err_ratelimited(domain->mmu->dev, |
| 240 | "TLB sync timed out -- MMU may be deadlocked\n"); |
| 241 | return; |
| 242 | } |
| 243 | udelay(1); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | static void ipmmu_tlb_invalidate(struct ipmmu_vmsa_domain *domain) |
| 248 | { |
| 249 | u32 reg; |
| 250 | |
| 251 | reg = ipmmu_ctx_read(domain, IMCTR); |
| 252 | reg |= IMCTR_FLUSH; |
| 253 | ipmmu_ctx_write(domain, IMCTR, reg); |
| 254 | |
| 255 | ipmmu_tlb_sync(domain); |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | * Enable MMU translation for the microTLB. |
| 260 | */ |
| 261 | static void ipmmu_utlb_enable(struct ipmmu_vmsa_domain *domain, |
Laurent Pinchart | 192d204 | 2014-05-15 12:40:42 +0200 | [diff] [blame] | 262 | unsigned int utlb) |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 263 | { |
| 264 | struct ipmmu_vmsa_device *mmu = domain->mmu; |
| 265 | |
Laurent Pinchart | 192d204 | 2014-05-15 12:40:42 +0200 | [diff] [blame] | 266 | /* |
| 267 | * TODO: Reference-count the microTLB as several bus masters can be |
| 268 | * connected to the same microTLB. |
| 269 | */ |
| 270 | |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 271 | /* TODO: What should we set the ASID to ? */ |
Laurent Pinchart | 192d204 | 2014-05-15 12:40:42 +0200 | [diff] [blame] | 272 | ipmmu_write(mmu, IMUASID(utlb), 0); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 273 | /* TODO: Do we need to flush the microTLB ? */ |
Laurent Pinchart | 192d204 | 2014-05-15 12:40:42 +0200 | [diff] [blame] | 274 | ipmmu_write(mmu, IMUCTR(utlb), |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 275 | IMUCTR_TTSEL_MMU(domain->context_id) | IMUCTR_FLUSH | |
| 276 | IMUCTR_MMUEN); |
| 277 | } |
| 278 | |
| 279 | /* |
| 280 | * Disable MMU translation for the microTLB. |
| 281 | */ |
| 282 | static void ipmmu_utlb_disable(struct ipmmu_vmsa_domain *domain, |
Laurent Pinchart | 192d204 | 2014-05-15 12:40:42 +0200 | [diff] [blame] | 283 | unsigned int utlb) |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 284 | { |
| 285 | struct ipmmu_vmsa_device *mmu = domain->mmu; |
| 286 | |
Laurent Pinchart | 192d204 | 2014-05-15 12:40:42 +0200 | [diff] [blame] | 287 | ipmmu_write(mmu, IMUCTR(utlb), 0); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 288 | } |
| 289 | |
Laurent Pinchart | f20ed39 | 2015-01-20 18:30:04 +0200 | [diff] [blame] | 290 | static void ipmmu_tlb_flush_all(void *cookie) |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 291 | { |
Laurent Pinchart | f20ed39 | 2015-01-20 18:30:04 +0200 | [diff] [blame] | 292 | struct ipmmu_vmsa_domain *domain = cookie; |
| 293 | |
| 294 | ipmmu_tlb_invalidate(domain); |
| 295 | } |
| 296 | |
Robin Murphy | 06c610e | 2015-12-07 18:18:53 +0000 | [diff] [blame] | 297 | static void ipmmu_tlb_add_flush(unsigned long iova, size_t size, |
| 298 | size_t granule, bool leaf, void *cookie) |
Laurent Pinchart | f20ed39 | 2015-01-20 18:30:04 +0200 | [diff] [blame] | 299 | { |
| 300 | /* The hardware doesn't support selective TLB flush. */ |
| 301 | } |
| 302 | |
Bhumika Goyal | 8da4af9 | 2017-08-28 23:47:27 +0530 | [diff] [blame] | 303 | static const struct iommu_gather_ops ipmmu_gather_ops = { |
Laurent Pinchart | f20ed39 | 2015-01-20 18:30:04 +0200 | [diff] [blame] | 304 | .tlb_flush_all = ipmmu_tlb_flush_all, |
| 305 | .tlb_add_flush = ipmmu_tlb_add_flush, |
| 306 | .tlb_sync = ipmmu_tlb_flush_all, |
Laurent Pinchart | f20ed39 | 2015-01-20 18:30:04 +0200 | [diff] [blame] | 307 | }; |
| 308 | |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 309 | /* ----------------------------------------------------------------------------- |
| 310 | * Domain/Context Management |
| 311 | */ |
| 312 | |
Magnus Damm | dbb7069 | 2017-05-17 19:06:38 +0900 | [diff] [blame] | 313 | static int ipmmu_domain_allocate_context(struct ipmmu_vmsa_device *mmu, |
| 314 | struct ipmmu_vmsa_domain *domain) |
| 315 | { |
| 316 | unsigned long flags; |
| 317 | int ret; |
| 318 | |
| 319 | spin_lock_irqsave(&mmu->lock, flags); |
| 320 | |
| 321 | ret = find_first_zero_bit(mmu->ctx, IPMMU_CTX_MAX); |
| 322 | if (ret != IPMMU_CTX_MAX) { |
| 323 | mmu->domains[ret] = domain; |
| 324 | set_bit(ret, mmu->ctx); |
| 325 | } |
| 326 | |
| 327 | spin_unlock_irqrestore(&mmu->lock, flags); |
| 328 | |
| 329 | return ret; |
| 330 | } |
| 331 | |
Oleksandr Tyshchenko | a175a67 | 2017-08-23 17:31:42 +0300 | [diff] [blame] | 332 | static void ipmmu_domain_free_context(struct ipmmu_vmsa_device *mmu, |
| 333 | unsigned int context_id) |
| 334 | { |
| 335 | unsigned long flags; |
| 336 | |
| 337 | spin_lock_irqsave(&mmu->lock, flags); |
| 338 | |
| 339 | clear_bit(context_id, mmu->ctx); |
| 340 | mmu->domains[context_id] = NULL; |
| 341 | |
| 342 | spin_unlock_irqrestore(&mmu->lock, flags); |
| 343 | } |
| 344 | |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 345 | static int ipmmu_domain_init_context(struct ipmmu_vmsa_domain *domain) |
| 346 | { |
Geert Uytterhoeven | f64232e | 2015-12-22 20:01:06 +0100 | [diff] [blame] | 347 | u64 ttbr; |
Magnus Damm | dbb7069 | 2017-05-17 19:06:38 +0900 | [diff] [blame] | 348 | int ret; |
Laurent Pinchart | f20ed39 | 2015-01-20 18:30:04 +0200 | [diff] [blame] | 349 | |
| 350 | /* |
| 351 | * Allocate the page table operations. |
| 352 | * |
| 353 | * VMSA states in section B3.6.3 "Control of Secure or Non-secure memory |
| 354 | * access, Long-descriptor format" that the NStable bit being set in a |
| 355 | * table descriptor will result in the NStable and NS bits of all child |
| 356 | * entries being ignored and considered as being set. The IPMMU seems |
| 357 | * not to comply with this, as it generates a secure access page fault |
| 358 | * if any of the NStable and NS bits isn't set when running in |
| 359 | * non-secure mode. |
| 360 | */ |
| 361 | domain->cfg.quirks = IO_PGTABLE_QUIRK_ARM_NS; |
Magnus Damm | 26b6aec | 2017-05-17 19:07:41 +0900 | [diff] [blame] | 362 | domain->cfg.pgsize_bitmap = SZ_1G | SZ_2M | SZ_4K; |
Laurent Pinchart | f20ed39 | 2015-01-20 18:30:04 +0200 | [diff] [blame] | 363 | domain->cfg.ias = 32; |
| 364 | domain->cfg.oas = 40; |
| 365 | domain->cfg.tlb = &ipmmu_gather_ops; |
Geert Uytterhoeven | 3b6bb5b | 2017-01-31 12:17:07 +0100 | [diff] [blame] | 366 | domain->io_domain.geometry.aperture_end = DMA_BIT_MASK(32); |
| 367 | domain->io_domain.geometry.force_aperture = true; |
Robin Murphy | ff2ed96 | 2015-07-29 19:46:08 +0100 | [diff] [blame] | 368 | /* |
| 369 | * TODO: Add support for coherent walk through CCI with DVM and remove |
| 370 | * cache handling. For now, delegate it to the io-pgtable code. |
| 371 | */ |
| 372 | domain->cfg.iommu_dev = domain->mmu->dev; |
Laurent Pinchart | f20ed39 | 2015-01-20 18:30:04 +0200 | [diff] [blame] | 373 | |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 374 | /* |
Magnus Damm | dbb7069 | 2017-05-17 19:06:38 +0900 | [diff] [blame] | 375 | * Find an unused context. |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 376 | */ |
Magnus Damm | dbb7069 | 2017-05-17 19:06:38 +0900 | [diff] [blame] | 377 | ret = ipmmu_domain_allocate_context(domain->mmu, domain); |
Oleksandr Tyshchenko | a175a67 | 2017-08-23 17:31:42 +0300 | [diff] [blame] | 378 | if (ret == IPMMU_CTX_MAX) |
Magnus Damm | dbb7069 | 2017-05-17 19:06:38 +0900 | [diff] [blame] | 379 | return -EBUSY; |
Magnus Damm | dbb7069 | 2017-05-17 19:06:38 +0900 | [diff] [blame] | 380 | |
| 381 | domain->context_id = ret; |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 382 | |
Oleksandr Tyshchenko | a175a67 | 2017-08-23 17:31:42 +0300 | [diff] [blame] | 383 | domain->iop = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &domain->cfg, |
| 384 | domain); |
| 385 | if (!domain->iop) { |
| 386 | ipmmu_domain_free_context(domain->mmu, domain->context_id); |
| 387 | return -EINVAL; |
| 388 | } |
| 389 | |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 390 | /* TTBR0 */ |
Laurent Pinchart | f20ed39 | 2015-01-20 18:30:04 +0200 | [diff] [blame] | 391 | ttbr = domain->cfg.arm_lpae_s1_cfg.ttbr[0]; |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 392 | ipmmu_ctx_write(domain, IMTTLBR0, ttbr); |
| 393 | ipmmu_ctx_write(domain, IMTTUBR0, ttbr >> 32); |
| 394 | |
| 395 | /* |
| 396 | * TTBCR |
| 397 | * We use long descriptors with inner-shareable WBWA tables and allocate |
| 398 | * the whole 32-bit VA space to TTBR0. |
| 399 | */ |
| 400 | ipmmu_ctx_write(domain, IMTTBCR, IMTTBCR_EAE | |
| 401 | IMTTBCR_SH0_INNER_SHAREABLE | IMTTBCR_ORGN0_WB_WA | |
| 402 | IMTTBCR_IRGN0_WB_WA | IMTTBCR_SL0_LVL_1); |
| 403 | |
Laurent Pinchart | f20ed39 | 2015-01-20 18:30:04 +0200 | [diff] [blame] | 404 | /* MAIR0 */ |
| 405 | ipmmu_ctx_write(domain, IMMAIR0, domain->cfg.arm_lpae_s1_cfg.mair[0]); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 406 | |
| 407 | /* IMBUSCR */ |
| 408 | ipmmu_ctx_write(domain, IMBUSCR, |
| 409 | ipmmu_ctx_read(domain, IMBUSCR) & |
| 410 | ~(IMBUSCR_DVM | IMBUSCR_BUSSEL_MASK)); |
| 411 | |
| 412 | /* |
| 413 | * IMSTR |
| 414 | * Clear all interrupt flags. |
| 415 | */ |
| 416 | ipmmu_ctx_write(domain, IMSTR, ipmmu_ctx_read(domain, IMSTR)); |
| 417 | |
| 418 | /* |
| 419 | * IMCTR |
| 420 | * Enable the MMU and interrupt generation. The long-descriptor |
| 421 | * translation table format doesn't use TEX remapping. Don't enable AF |
| 422 | * software management as we have no use for it. Flush the TLB as |
| 423 | * required when modifying the context registers. |
| 424 | */ |
| 425 | ipmmu_ctx_write(domain, IMCTR, IMCTR_INTEN | IMCTR_FLUSH | IMCTR_MMUEN); |
| 426 | |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | static void ipmmu_domain_destroy_context(struct ipmmu_vmsa_domain *domain) |
| 431 | { |
| 432 | /* |
| 433 | * Disable the context. Flush the TLB as required when modifying the |
| 434 | * context registers. |
| 435 | * |
| 436 | * TODO: Is TLB flush really needed ? |
| 437 | */ |
| 438 | ipmmu_ctx_write(domain, IMCTR, IMCTR_FLUSH); |
| 439 | ipmmu_tlb_sync(domain); |
Magnus Damm | dbb7069 | 2017-05-17 19:06:38 +0900 | [diff] [blame] | 440 | ipmmu_domain_free_context(domain->mmu, domain->context_id); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | /* ----------------------------------------------------------------------------- |
| 444 | * Fault Handling |
| 445 | */ |
| 446 | |
| 447 | static irqreturn_t ipmmu_domain_irq(struct ipmmu_vmsa_domain *domain) |
| 448 | { |
| 449 | const u32 err_mask = IMSTR_MHIT | IMSTR_ABORT | IMSTR_PF | IMSTR_TF; |
| 450 | struct ipmmu_vmsa_device *mmu = domain->mmu; |
| 451 | u32 status; |
| 452 | u32 iova; |
| 453 | |
| 454 | status = ipmmu_ctx_read(domain, IMSTR); |
| 455 | if (!(status & err_mask)) |
| 456 | return IRQ_NONE; |
| 457 | |
| 458 | iova = ipmmu_ctx_read(domain, IMEAR); |
| 459 | |
| 460 | /* |
| 461 | * Clear the error status flags. Unlike traditional interrupt flag |
| 462 | * registers that must be cleared by writing 1, this status register |
| 463 | * seems to require 0. The error address register must be read before, |
| 464 | * otherwise its value will be 0. |
| 465 | */ |
| 466 | ipmmu_ctx_write(domain, IMSTR, 0); |
| 467 | |
| 468 | /* Log fatal errors. */ |
| 469 | if (status & IMSTR_MHIT) |
| 470 | dev_err_ratelimited(mmu->dev, "Multiple TLB hits @0x%08x\n", |
| 471 | iova); |
| 472 | if (status & IMSTR_ABORT) |
| 473 | dev_err_ratelimited(mmu->dev, "Page Table Walk Abort @0x%08x\n", |
| 474 | iova); |
| 475 | |
| 476 | if (!(status & (IMSTR_PF | IMSTR_TF))) |
| 477 | return IRQ_NONE; |
| 478 | |
| 479 | /* |
| 480 | * Try to handle page faults and translation faults. |
| 481 | * |
| 482 | * TODO: We need to look up the faulty device based on the I/O VA. Use |
| 483 | * the IOMMU device for now. |
| 484 | */ |
Joerg Roedel | 5914c5f | 2015-03-26 13:43:16 +0100 | [diff] [blame] | 485 | if (!report_iommu_fault(&domain->io_domain, mmu->dev, iova, 0)) |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 486 | return IRQ_HANDLED; |
| 487 | |
| 488 | dev_err_ratelimited(mmu->dev, |
| 489 | "Unhandled fault: status 0x%08x iova 0x%08x\n", |
| 490 | status, iova); |
| 491 | |
| 492 | return IRQ_HANDLED; |
| 493 | } |
| 494 | |
| 495 | static irqreturn_t ipmmu_irq(int irq, void *dev) |
| 496 | { |
| 497 | struct ipmmu_vmsa_device *mmu = dev; |
Magnus Damm | dbb7069 | 2017-05-17 19:06:38 +0900 | [diff] [blame] | 498 | irqreturn_t status = IRQ_NONE; |
| 499 | unsigned int i; |
| 500 | unsigned long flags; |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 501 | |
Magnus Damm | dbb7069 | 2017-05-17 19:06:38 +0900 | [diff] [blame] | 502 | spin_lock_irqsave(&mmu->lock, flags); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 503 | |
Magnus Damm | dbb7069 | 2017-05-17 19:06:38 +0900 | [diff] [blame] | 504 | /* |
| 505 | * Check interrupts for all active contexts. |
| 506 | */ |
| 507 | for (i = 0; i < IPMMU_CTX_MAX; i++) { |
| 508 | if (!mmu->domains[i]) |
| 509 | continue; |
| 510 | if (ipmmu_domain_irq(mmu->domains[i]) == IRQ_HANDLED) |
| 511 | status = IRQ_HANDLED; |
| 512 | } |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 513 | |
Magnus Damm | dbb7069 | 2017-05-17 19:06:38 +0900 | [diff] [blame] | 514 | spin_unlock_irqrestore(&mmu->lock, flags); |
| 515 | |
| 516 | return status; |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | /* ----------------------------------------------------------------------------- |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 520 | * IOMMU Operations |
| 521 | */ |
| 522 | |
Magnus Damm | 8e73bf6 | 2017-05-17 19:06:59 +0900 | [diff] [blame] | 523 | static struct iommu_domain *__ipmmu_domain_alloc(unsigned type) |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 524 | { |
| 525 | struct ipmmu_vmsa_domain *domain; |
| 526 | |
| 527 | domain = kzalloc(sizeof(*domain), GFP_KERNEL); |
| 528 | if (!domain) |
Joerg Roedel | 5914c5f | 2015-03-26 13:43:16 +0100 | [diff] [blame] | 529 | return NULL; |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 530 | |
| 531 | spin_lock_init(&domain->lock); |
| 532 | |
Joerg Roedel | 5914c5f | 2015-03-26 13:43:16 +0100 | [diff] [blame] | 533 | return &domain->io_domain; |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 534 | } |
| 535 | |
Robin Murphy | 1c7e7c0 | 2017-10-13 19:23:39 +0100 | [diff] [blame] | 536 | static struct iommu_domain *ipmmu_domain_alloc(unsigned type) |
| 537 | { |
| 538 | struct iommu_domain *io_domain = NULL; |
| 539 | |
| 540 | switch (type) { |
| 541 | case IOMMU_DOMAIN_UNMANAGED: |
| 542 | io_domain = __ipmmu_domain_alloc(type); |
| 543 | break; |
| 544 | |
| 545 | case IOMMU_DOMAIN_DMA: |
| 546 | io_domain = __ipmmu_domain_alloc(type); |
| 547 | if (io_domain && iommu_get_dma_cookie(io_domain)) { |
| 548 | kfree(io_domain); |
| 549 | io_domain = NULL; |
| 550 | } |
| 551 | break; |
| 552 | } |
| 553 | |
| 554 | return io_domain; |
| 555 | } |
| 556 | |
Joerg Roedel | 5914c5f | 2015-03-26 13:43:16 +0100 | [diff] [blame] | 557 | static void ipmmu_domain_free(struct iommu_domain *io_domain) |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 558 | { |
Joerg Roedel | 5914c5f | 2015-03-26 13:43:16 +0100 | [diff] [blame] | 559 | struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 560 | |
| 561 | /* |
| 562 | * Free the domain resources. We assume that all devices have already |
| 563 | * been detached. |
| 564 | */ |
Robin Murphy | 1c7e7c0 | 2017-10-13 19:23:39 +0100 | [diff] [blame] | 565 | iommu_put_dma_cookie(io_domain); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 566 | ipmmu_domain_destroy_context(domain); |
Laurent Pinchart | f20ed39 | 2015-01-20 18:30:04 +0200 | [diff] [blame] | 567 | free_io_pgtable_ops(domain->iop); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 568 | kfree(domain); |
| 569 | } |
| 570 | |
| 571 | static int ipmmu_attach_device(struct iommu_domain *io_domain, |
| 572 | struct device *dev) |
| 573 | { |
Magnus Damm | 7b2d596 | 2017-07-17 22:05:41 +0900 | [diff] [blame] | 574 | struct iommu_fwspec *fwspec = dev->iommu_fwspec; |
Robin Murphy | e4efe4a | 2017-10-13 19:23:41 +0100 | [diff] [blame] | 575 | struct ipmmu_vmsa_device *mmu = to_ipmmu(dev); |
Joerg Roedel | 5914c5f | 2015-03-26 13:43:16 +0100 | [diff] [blame] | 576 | struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 577 | unsigned long flags; |
Laurent Pinchart | a166d31 | 2014-07-24 01:36:43 +0200 | [diff] [blame] | 578 | unsigned int i; |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 579 | int ret = 0; |
| 580 | |
Robin Murphy | e4efe4a | 2017-10-13 19:23:41 +0100 | [diff] [blame] | 581 | if (!mmu) { |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 582 | dev_err(dev, "Cannot attach to IPMMU\n"); |
| 583 | return -ENXIO; |
| 584 | } |
| 585 | |
| 586 | spin_lock_irqsave(&domain->lock, flags); |
| 587 | |
| 588 | if (!domain->mmu) { |
| 589 | /* The domain hasn't been used yet, initialize it. */ |
| 590 | domain->mmu = mmu; |
| 591 | ret = ipmmu_domain_init_context(domain); |
| 592 | } else if (domain->mmu != mmu) { |
| 593 | /* |
| 594 | * Something is wrong, we can't attach two devices using |
| 595 | * different IOMMUs to the same domain. |
| 596 | */ |
| 597 | dev_err(dev, "Can't attach IPMMU %s to domain on IPMMU %s\n", |
| 598 | dev_name(mmu->dev), dev_name(domain->mmu->dev)); |
| 599 | ret = -EINVAL; |
Magnus Damm | 3ae4729 | 2017-05-17 19:07:10 +0900 | [diff] [blame] | 600 | } else |
| 601 | dev_info(dev, "Reusing IPMMU context %u\n", domain->context_id); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 602 | |
| 603 | spin_unlock_irqrestore(&domain->lock, flags); |
| 604 | |
| 605 | if (ret < 0) |
| 606 | return ret; |
| 607 | |
Magnus Damm | 7b2d596 | 2017-07-17 22:05:41 +0900 | [diff] [blame] | 608 | for (i = 0; i < fwspec->num_ids; ++i) |
| 609 | ipmmu_utlb_enable(domain, fwspec->ids[i]); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 610 | |
| 611 | return 0; |
| 612 | } |
| 613 | |
| 614 | static void ipmmu_detach_device(struct iommu_domain *io_domain, |
| 615 | struct device *dev) |
| 616 | { |
Magnus Damm | 7b2d596 | 2017-07-17 22:05:41 +0900 | [diff] [blame] | 617 | struct iommu_fwspec *fwspec = dev->iommu_fwspec; |
Joerg Roedel | 5914c5f | 2015-03-26 13:43:16 +0100 | [diff] [blame] | 618 | struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain); |
Laurent Pinchart | a166d31 | 2014-07-24 01:36:43 +0200 | [diff] [blame] | 619 | unsigned int i; |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 620 | |
Magnus Damm | 7b2d596 | 2017-07-17 22:05:41 +0900 | [diff] [blame] | 621 | for (i = 0; i < fwspec->num_ids; ++i) |
| 622 | ipmmu_utlb_disable(domain, fwspec->ids[i]); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 623 | |
| 624 | /* |
| 625 | * TODO: Optimize by disabling the context when no device is attached. |
| 626 | */ |
| 627 | } |
| 628 | |
| 629 | static int ipmmu_map(struct iommu_domain *io_domain, unsigned long iova, |
| 630 | phys_addr_t paddr, size_t size, int prot) |
| 631 | { |
Joerg Roedel | 5914c5f | 2015-03-26 13:43:16 +0100 | [diff] [blame] | 632 | struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 633 | |
| 634 | if (!domain) |
| 635 | return -ENODEV; |
| 636 | |
Laurent Pinchart | f20ed39 | 2015-01-20 18:30:04 +0200 | [diff] [blame] | 637 | return domain->iop->map(domain->iop, iova, paddr, size, prot); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | static size_t ipmmu_unmap(struct iommu_domain *io_domain, unsigned long iova, |
| 641 | size_t size) |
| 642 | { |
Joerg Roedel | 5914c5f | 2015-03-26 13:43:16 +0100 | [diff] [blame] | 643 | struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 644 | |
Laurent Pinchart | f20ed39 | 2015-01-20 18:30:04 +0200 | [diff] [blame] | 645 | return domain->iop->unmap(domain->iop, iova, size); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 646 | } |
| 647 | |
Robin Murphy | 32b1244 | 2017-09-28 15:55:01 +0100 | [diff] [blame] | 648 | static void ipmmu_iotlb_sync(struct iommu_domain *io_domain) |
| 649 | { |
| 650 | struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain); |
| 651 | |
| 652 | if (domain->mmu) |
| 653 | ipmmu_tlb_flush_all(domain); |
| 654 | } |
| 655 | |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 656 | static phys_addr_t ipmmu_iova_to_phys(struct iommu_domain *io_domain, |
| 657 | dma_addr_t iova) |
| 658 | { |
Joerg Roedel | 5914c5f | 2015-03-26 13:43:16 +0100 | [diff] [blame] | 659 | struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 660 | |
| 661 | /* TODO: Is locking needed ? */ |
| 662 | |
Laurent Pinchart | f20ed39 | 2015-01-20 18:30:04 +0200 | [diff] [blame] | 663 | return domain->iop->iova_to_phys(domain->iop, iova); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 664 | } |
| 665 | |
Magnus Damm | 7b2d596 | 2017-07-17 22:05:41 +0900 | [diff] [blame] | 666 | static int ipmmu_init_platform_device(struct device *dev, |
| 667 | struct of_phandle_args *args) |
Laurent Pinchart | 192d204 | 2014-05-15 12:40:42 +0200 | [diff] [blame] | 668 | { |
Magnus Damm | 7b2d596 | 2017-07-17 22:05:41 +0900 | [diff] [blame] | 669 | struct platform_device *ipmmu_pdev; |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 670 | |
Magnus Damm | 7b2d596 | 2017-07-17 22:05:41 +0900 | [diff] [blame] | 671 | ipmmu_pdev = of_find_device_by_node(args->np); |
| 672 | if (!ipmmu_pdev) |
Laurent Pinchart | bb590c9 | 2015-01-24 23:13:50 +0200 | [diff] [blame] | 673 | return -ENODEV; |
| 674 | |
Robin Murphy | e4efe4a | 2017-10-13 19:23:41 +0100 | [diff] [blame] | 675 | dev->iommu_fwspec->iommu_priv = platform_get_drvdata(ipmmu_pdev); |
Magnus Damm | 383fef5f | 2017-05-17 19:06:48 +0900 | [diff] [blame] | 676 | return 0; |
Magnus Damm | 383fef5f | 2017-05-17 19:06:48 +0900 | [diff] [blame] | 677 | } |
| 678 | |
Magnus Damm | 49558da | 2017-07-17 22:05:20 +0900 | [diff] [blame] | 679 | static int ipmmu_of_xlate(struct device *dev, |
| 680 | struct of_phandle_args *spec) |
| 681 | { |
Magnus Damm | 7b2d596 | 2017-07-17 22:05:41 +0900 | [diff] [blame] | 682 | iommu_fwspec_add_ids(dev, spec->args, 1); |
| 683 | |
Magnus Damm | 49558da | 2017-07-17 22:05:20 +0900 | [diff] [blame] | 684 | /* Initialize once - xlate() will call multiple times */ |
Robin Murphy | e4efe4a | 2017-10-13 19:23:41 +0100 | [diff] [blame] | 685 | if (to_ipmmu(dev)) |
Magnus Damm | 49558da | 2017-07-17 22:05:20 +0900 | [diff] [blame] | 686 | return 0; |
| 687 | |
Magnus Damm | 7b2d596 | 2017-07-17 22:05:41 +0900 | [diff] [blame] | 688 | return ipmmu_init_platform_device(dev, spec); |
Magnus Damm | 49558da | 2017-07-17 22:05:20 +0900 | [diff] [blame] | 689 | } |
| 690 | |
Robin Murphy | 49c875f | 2017-10-13 19:23:42 +0100 | [diff] [blame] | 691 | static int ipmmu_init_arm_mapping(struct device *dev) |
Magnus Damm | 383fef5f | 2017-05-17 19:06:48 +0900 | [diff] [blame] | 692 | { |
Robin Murphy | e4efe4a | 2017-10-13 19:23:41 +0100 | [diff] [blame] | 693 | struct ipmmu_vmsa_device *mmu = to_ipmmu(dev); |
Magnus Damm | 383fef5f | 2017-05-17 19:06:48 +0900 | [diff] [blame] | 694 | struct iommu_group *group; |
| 695 | int ret; |
| 696 | |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 697 | /* Create a device group and add the device to it. */ |
| 698 | group = iommu_group_alloc(); |
| 699 | if (IS_ERR(group)) { |
| 700 | dev_err(dev, "Failed to allocate IOMMU group\n"); |
Robin Murphy | 49c875f | 2017-10-13 19:23:42 +0100 | [diff] [blame] | 701 | return PTR_ERR(group); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | ret = iommu_group_add_device(group, dev); |
| 705 | iommu_group_put(group); |
| 706 | |
| 707 | if (ret < 0) { |
| 708 | dev_err(dev, "Failed to add device to IPMMU group\n"); |
Robin Murphy | 49c875f | 2017-10-13 19:23:42 +0100 | [diff] [blame] | 709 | return ret; |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 710 | } |
| 711 | |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 712 | /* |
| 713 | * Create the ARM mapping, used by the ARM DMA mapping core to allocate |
| 714 | * VAs. This will allocate a corresponding IOMMU domain. |
| 715 | * |
| 716 | * TODO: |
| 717 | * - Create one mapping per context (TLB). |
| 718 | * - Make the mapping size configurable ? We currently use a 2GB mapping |
| 719 | * at a 1GB offset to ensure that NULL VAs will fault. |
| 720 | */ |
| 721 | if (!mmu->mapping) { |
| 722 | struct dma_iommu_mapping *mapping; |
| 723 | |
| 724 | mapping = arm_iommu_create_mapping(&platform_bus_type, |
Joerg Roedel | 720b0ce | 2014-05-26 13:07:01 +0200 | [diff] [blame] | 725 | SZ_1G, SZ_2G); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 726 | if (IS_ERR(mapping)) { |
| 727 | dev_err(mmu->dev, "failed to create ARM IOMMU mapping\n"); |
Laurent Pinchart | b8f80bf | 2014-03-14 14:00:56 +0100 | [diff] [blame] | 728 | ret = PTR_ERR(mapping); |
| 729 | goto error; |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | mmu->mapping = mapping; |
| 733 | } |
| 734 | |
| 735 | /* Attach the ARM VA mapping to the device. */ |
| 736 | ret = arm_iommu_attach_device(dev, mmu->mapping); |
| 737 | if (ret < 0) { |
| 738 | dev_err(dev, "Failed to attach device to VA mapping\n"); |
| 739 | goto error; |
| 740 | } |
| 741 | |
| 742 | return 0; |
| 743 | |
| 744 | error: |
Robin Murphy | 49c875f | 2017-10-13 19:23:42 +0100 | [diff] [blame] | 745 | iommu_group_remove_device(dev); |
| 746 | if (mmu->mapping) |
Magnus Damm | 383fef5f | 2017-05-17 19:06:48 +0900 | [diff] [blame] | 747 | arm_iommu_release_mapping(mmu->mapping); |
Laurent Pinchart | a166d31 | 2014-07-24 01:36:43 +0200 | [diff] [blame] | 748 | |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 749 | return ret; |
| 750 | } |
| 751 | |
Robin Murphy | 49c875f | 2017-10-13 19:23:42 +0100 | [diff] [blame] | 752 | static int ipmmu_add_device(struct device *dev) |
Magnus Damm | 3ae4729 | 2017-05-17 19:07:10 +0900 | [diff] [blame] | 753 | { |
Magnus Damm | 3ae4729 | 2017-05-17 19:07:10 +0900 | [diff] [blame] | 754 | struct iommu_group *group; |
| 755 | |
Magnus Damm | 0fbc8b0 | 2017-05-17 19:07:20 +0900 | [diff] [blame] | 756 | /* |
| 757 | * Only let through devices that have been verified in xlate() |
Magnus Damm | 0fbc8b0 | 2017-05-17 19:07:20 +0900 | [diff] [blame] | 758 | */ |
Robin Murphy | e4efe4a | 2017-10-13 19:23:41 +0100 | [diff] [blame] | 759 | if (!to_ipmmu(dev)) |
Magnus Damm | 3ae4729 | 2017-05-17 19:07:10 +0900 | [diff] [blame] | 760 | return -ENODEV; |
| 761 | |
Robin Murphy | 49c875f | 2017-10-13 19:23:42 +0100 | [diff] [blame] | 762 | if (IS_ENABLED(CONFIG_ARM) && !IS_ENABLED(CONFIG_IOMMU_DMA)) |
| 763 | return ipmmu_init_arm_mapping(dev); |
| 764 | |
Magnus Damm | 3ae4729 | 2017-05-17 19:07:10 +0900 | [diff] [blame] | 765 | group = iommu_group_get_for_dev(dev); |
| 766 | if (IS_ERR(group)) |
| 767 | return PTR_ERR(group); |
| 768 | |
Robin Murphy | 49c875f | 2017-10-13 19:23:42 +0100 | [diff] [blame] | 769 | iommu_group_put(group); |
Magnus Damm | 3ae4729 | 2017-05-17 19:07:10 +0900 | [diff] [blame] | 770 | return 0; |
| 771 | } |
| 772 | |
Robin Murphy | 49c875f | 2017-10-13 19:23:42 +0100 | [diff] [blame] | 773 | static void ipmmu_remove_device(struct device *dev) |
Magnus Damm | 3ae4729 | 2017-05-17 19:07:10 +0900 | [diff] [blame] | 774 | { |
Robin Murphy | 49c875f | 2017-10-13 19:23:42 +0100 | [diff] [blame] | 775 | arm_iommu_detach_device(dev); |
Magnus Damm | 3ae4729 | 2017-05-17 19:07:10 +0900 | [diff] [blame] | 776 | iommu_group_remove_device(dev); |
| 777 | } |
| 778 | |
Robin Murphy | b354c73 | 2017-10-13 19:23:40 +0100 | [diff] [blame] | 779 | static struct iommu_group *ipmmu_find_group(struct device *dev) |
Magnus Damm | 3ae4729 | 2017-05-17 19:07:10 +0900 | [diff] [blame] | 780 | { |
Robin Murphy | e4efe4a | 2017-10-13 19:23:41 +0100 | [diff] [blame] | 781 | struct ipmmu_vmsa_device *mmu = to_ipmmu(dev); |
Magnus Damm | 3ae4729 | 2017-05-17 19:07:10 +0900 | [diff] [blame] | 782 | struct iommu_group *group; |
Magnus Damm | 3ae4729 | 2017-05-17 19:07:10 +0900 | [diff] [blame] | 783 | |
Robin Murphy | e4efe4a | 2017-10-13 19:23:41 +0100 | [diff] [blame] | 784 | if (mmu->group) |
| 785 | return iommu_group_ref_get(mmu->group); |
Robin Murphy | b354c73 | 2017-10-13 19:23:40 +0100 | [diff] [blame] | 786 | |
| 787 | group = iommu_group_alloc(); |
| 788 | if (!IS_ERR(group)) |
Robin Murphy | e4efe4a | 2017-10-13 19:23:41 +0100 | [diff] [blame] | 789 | mmu->group = group; |
Magnus Damm | 3ae4729 | 2017-05-17 19:07:10 +0900 | [diff] [blame] | 790 | |
| 791 | return group; |
| 792 | } |
| 793 | |
Magnus Damm | 3ae4729 | 2017-05-17 19:07:10 +0900 | [diff] [blame] | 794 | static const struct iommu_ops ipmmu_ops = { |
Robin Murphy | 1c7e7c0 | 2017-10-13 19:23:39 +0100 | [diff] [blame] | 795 | .domain_alloc = ipmmu_domain_alloc, |
| 796 | .domain_free = ipmmu_domain_free, |
Magnus Damm | 3ae4729 | 2017-05-17 19:07:10 +0900 | [diff] [blame] | 797 | .attach_dev = ipmmu_attach_device, |
| 798 | .detach_dev = ipmmu_detach_device, |
| 799 | .map = ipmmu_map, |
| 800 | .unmap = ipmmu_unmap, |
Robin Murphy | 32b1244 | 2017-09-28 15:55:01 +0100 | [diff] [blame] | 801 | .flush_iotlb_all = ipmmu_iotlb_sync, |
| 802 | .iotlb_sync = ipmmu_iotlb_sync, |
Magnus Damm | 3ae4729 | 2017-05-17 19:07:10 +0900 | [diff] [blame] | 803 | .map_sg = default_iommu_map_sg, |
| 804 | .iova_to_phys = ipmmu_iova_to_phys, |
Robin Murphy | 49c875f | 2017-10-13 19:23:42 +0100 | [diff] [blame] | 805 | .add_device = ipmmu_add_device, |
| 806 | .remove_device = ipmmu_remove_device, |
Robin Murphy | b354c73 | 2017-10-13 19:23:40 +0100 | [diff] [blame] | 807 | .device_group = ipmmu_find_group, |
Magnus Damm | 3ae4729 | 2017-05-17 19:07:10 +0900 | [diff] [blame] | 808 | .pgsize_bitmap = SZ_1G | SZ_2M | SZ_4K, |
Magnus Damm | 49558da | 2017-07-17 22:05:20 +0900 | [diff] [blame] | 809 | .of_xlate = ipmmu_of_xlate, |
Magnus Damm | 3ae4729 | 2017-05-17 19:07:10 +0900 | [diff] [blame] | 810 | }; |
| 811 | |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 812 | /* ----------------------------------------------------------------------------- |
| 813 | * Probe/remove and init |
| 814 | */ |
| 815 | |
| 816 | static void ipmmu_device_reset(struct ipmmu_vmsa_device *mmu) |
| 817 | { |
| 818 | unsigned int i; |
| 819 | |
| 820 | /* Disable all contexts. */ |
| 821 | for (i = 0; i < 4; ++i) |
| 822 | ipmmu_write(mmu, i * IM_CTX_SIZE + IMCTR, 0); |
| 823 | } |
| 824 | |
Magnus Damm | 33f3ac9 | 2017-10-16 21:29:25 +0900 | [diff] [blame^] | 825 | static const struct ipmmu_features ipmmu_features_default = { |
| 826 | .use_ns_alias_offset = true, |
| 827 | }; |
| 828 | |
| 829 | static const struct of_device_id ipmmu_of_ids[] = { |
| 830 | { |
| 831 | .compatible = "renesas,ipmmu-vmsa", |
| 832 | .data = &ipmmu_features_default, |
| 833 | }, { |
| 834 | /* Terminator */ |
| 835 | }, |
| 836 | }; |
| 837 | |
| 838 | MODULE_DEVICE_TABLE(of, ipmmu_of_ids); |
| 839 | |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 840 | static int ipmmu_probe(struct platform_device *pdev) |
| 841 | { |
| 842 | struct ipmmu_vmsa_device *mmu; |
| 843 | struct resource *res; |
| 844 | int irq; |
| 845 | int ret; |
| 846 | |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 847 | mmu = devm_kzalloc(&pdev->dev, sizeof(*mmu), GFP_KERNEL); |
| 848 | if (!mmu) { |
| 849 | dev_err(&pdev->dev, "cannot allocate device data\n"); |
| 850 | return -ENOMEM; |
| 851 | } |
| 852 | |
| 853 | mmu->dev = &pdev->dev; |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 854 | mmu->num_utlbs = 32; |
Magnus Damm | dbb7069 | 2017-05-17 19:06:38 +0900 | [diff] [blame] | 855 | spin_lock_init(&mmu->lock); |
| 856 | bitmap_zero(mmu->ctx, IPMMU_CTX_MAX); |
Magnus Damm | 33f3ac9 | 2017-10-16 21:29:25 +0900 | [diff] [blame^] | 857 | mmu->features = of_device_get_match_data(&pdev->dev); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 858 | |
| 859 | /* Map I/O memory and request IRQ. */ |
| 860 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 861 | mmu->base = devm_ioremap_resource(&pdev->dev, res); |
| 862 | if (IS_ERR(mmu->base)) |
| 863 | return PTR_ERR(mmu->base); |
| 864 | |
Laurent Pinchart | 275f505 | 2014-03-17 01:02:46 +0100 | [diff] [blame] | 865 | /* |
| 866 | * The IPMMU has two register banks, for secure and non-secure modes. |
| 867 | * The bank mapped at the beginning of the IPMMU address space |
| 868 | * corresponds to the running mode of the CPU. When running in secure |
| 869 | * mode the non-secure register bank is also available at an offset. |
| 870 | * |
| 871 | * Secure mode operation isn't clearly documented and is thus currently |
| 872 | * not implemented in the driver. Furthermore, preliminary tests of |
| 873 | * non-secure operation with the main register bank were not successful. |
| 874 | * Offset the registers base unconditionally to point to the non-secure |
| 875 | * alias space for now. |
| 876 | */ |
Magnus Damm | 33f3ac9 | 2017-10-16 21:29:25 +0900 | [diff] [blame^] | 877 | if (mmu->features->use_ns_alias_offset) |
| 878 | mmu->base += IM_NS_ALIAS_OFFSET; |
Laurent Pinchart | 275f505 | 2014-03-17 01:02:46 +0100 | [diff] [blame] | 879 | |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 880 | irq = platform_get_irq(pdev, 0); |
| 881 | if (irq < 0) { |
| 882 | dev_err(&pdev->dev, "no IRQ found\n"); |
| 883 | return irq; |
| 884 | } |
| 885 | |
| 886 | ret = devm_request_irq(&pdev->dev, irq, ipmmu_irq, 0, |
| 887 | dev_name(&pdev->dev), mmu); |
| 888 | if (ret < 0) { |
| 889 | dev_err(&pdev->dev, "failed to request IRQ %d\n", irq); |
Axel Lin | e222d6a | 2014-11-01 11:45:32 +0800 | [diff] [blame] | 890 | return ret; |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | ipmmu_device_reset(mmu); |
| 894 | |
Magnus Damm | 7af9a5f | 2017-08-21 14:53:35 +0900 | [diff] [blame] | 895 | ret = iommu_device_sysfs_add(&mmu->iommu, &pdev->dev, NULL, |
| 896 | dev_name(&pdev->dev)); |
| 897 | if (ret) |
| 898 | return ret; |
| 899 | |
Magnus Damm | 01da21e | 2017-07-17 22:05:10 +0900 | [diff] [blame] | 900 | iommu_device_set_ops(&mmu->iommu, &ipmmu_ops); |
| 901 | iommu_device_set_fwnode(&mmu->iommu, &pdev->dev.of_node->fwnode); |
| 902 | |
| 903 | ret = iommu_device_register(&mmu->iommu); |
| 904 | if (ret) |
| 905 | return ret; |
| 906 | |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 907 | /* |
| 908 | * We can't create the ARM mapping here as it requires the bus to have |
| 909 | * an IOMMU, which only happens when bus_set_iommu() is called in |
| 910 | * ipmmu_init() after the probe function returns. |
| 911 | */ |
| 912 | |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 913 | platform_set_drvdata(pdev, mmu); |
| 914 | |
| 915 | return 0; |
| 916 | } |
| 917 | |
| 918 | static int ipmmu_remove(struct platform_device *pdev) |
| 919 | { |
| 920 | struct ipmmu_vmsa_device *mmu = platform_get_drvdata(pdev); |
| 921 | |
Magnus Damm | 7af9a5f | 2017-08-21 14:53:35 +0900 | [diff] [blame] | 922 | iommu_device_sysfs_remove(&mmu->iommu); |
Magnus Damm | 01da21e | 2017-07-17 22:05:10 +0900 | [diff] [blame] | 923 | iommu_device_unregister(&mmu->iommu); |
| 924 | |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 925 | arm_iommu_release_mapping(mmu->mapping); |
| 926 | |
| 927 | ipmmu_device_reset(mmu); |
| 928 | |
| 929 | return 0; |
| 930 | } |
| 931 | |
| 932 | static struct platform_driver ipmmu_driver = { |
| 933 | .driver = { |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 934 | .name = "ipmmu-vmsa", |
Laurent Pinchart | 275f505 | 2014-03-17 01:02:46 +0100 | [diff] [blame] | 935 | .of_match_table = of_match_ptr(ipmmu_of_ids), |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 936 | }, |
| 937 | .probe = ipmmu_probe, |
| 938 | .remove = ipmmu_remove, |
| 939 | }; |
| 940 | |
| 941 | static int __init ipmmu_init(void) |
| 942 | { |
| 943 | int ret; |
| 944 | |
| 945 | ret = platform_driver_register(&ipmmu_driver); |
| 946 | if (ret < 0) |
| 947 | return ret; |
| 948 | |
| 949 | if (!iommu_present(&platform_bus_type)) |
| 950 | bus_set_iommu(&platform_bus_type, &ipmmu_ops); |
| 951 | |
| 952 | return 0; |
| 953 | } |
| 954 | |
| 955 | static void __exit ipmmu_exit(void) |
| 956 | { |
| 957 | return platform_driver_unregister(&ipmmu_driver); |
| 958 | } |
| 959 | |
| 960 | subsys_initcall(ipmmu_init); |
| 961 | module_exit(ipmmu_exit); |
| 962 | |
| 963 | MODULE_DESCRIPTION("IOMMU API for Renesas VMSA-compatible IPMMU"); |
| 964 | MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>"); |
| 965 | MODULE_LICENSE("GPL v2"); |