Kuninori Morimoto | 57d3f11c | 2018-09-07 01:42:15 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 2 | /* |
Paul Gortmaker | 8128ac3 | 2018-12-01 14:19:13 -0500 | [diff] [blame] | 3 | * IOMMU API for Renesas VMSA-compatible IPMMU |
| 4 | * Author: Laurent Pinchart <laurent.pinchart@ideasonboard.com> |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 5 | * |
Yoshihiro Shimoda | 17fe161 | 2020-06-11 20:10:30 +0900 | [diff] [blame] | 6 | * Copyright (C) 2014-2020 Renesas Electronics Corporation |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
Magnus Damm | dbb7069 | 2017-05-17 19:06:38 +0900 | [diff] [blame] | 9 | #include <linux/bitmap.h> |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 10 | #include <linux/delay.h> |
Magnus Damm | 3ae4729 | 2017-05-17 19:07:10 +0900 | [diff] [blame] | 11 | #include <linux/dma-iommu.h> |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 12 | #include <linux/dma-mapping.h> |
| 13 | #include <linux/err.h> |
| 14 | #include <linux/export.h> |
Paul Gortmaker | 8128ac3 | 2018-12-01 14:19:13 -0500 | [diff] [blame] | 15 | #include <linux/init.h> |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/io.h> |
Rob Herring | b77cf11 | 2019-02-05 10:37:31 -0600 | [diff] [blame] | 18 | #include <linux/io-pgtable.h> |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 19 | #include <linux/iommu.h> |
Laurent Pinchart | 275f505 | 2014-03-17 01:02:46 +0100 | [diff] [blame] | 20 | #include <linux/of.h> |
Magnus Damm | 33f3ac9 | 2017-10-16 21:29:25 +0900 | [diff] [blame] | 21 | #include <linux/of_device.h> |
Magnus Damm | cda52fc | 2017-10-16 21:29:57 +0900 | [diff] [blame] | 22 | #include <linux/of_iommu.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> |
Magnus Damm | 58b8e8bf | 2017-10-16 21:30:50 +0900 | [diff] [blame] | 27 | #include <linux/sys_soc.h> |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 28 | |
Magnus Damm | 3ae4729 | 2017-05-17 19:07:10 +0900 | [diff] [blame] | 29 | #if defined(CONFIG_ARM) && !defined(CONFIG_IOMMU_DMA) |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 30 | #include <asm/dma-iommu.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 | |
Geert Uytterhoeven | da38e9e | 2019-05-27 13:52:53 +0200 | [diff] [blame] | 38 | #define IPMMU_CTX_MAX 8U |
| 39 | #define IPMMU_CTX_INVALID -1 |
| 40 | |
| 41 | #define IPMMU_UTLB_MAX 48U |
Magnus Damm | dbb7069 | 2017-05-17 19:06:38 +0900 | [diff] [blame] | 42 | |
Magnus Damm | 33f3ac9 | 2017-10-16 21:29:25 +0900 | [diff] [blame] | 43 | struct ipmmu_features { |
| 44 | bool use_ns_alias_offset; |
Magnus Damm | fd5140e | 2017-10-16 21:29:36 +0900 | [diff] [blame] | 45 | bool has_cache_leaf_nodes; |
Magnus Damm | 5fd1634 | 2017-10-16 21:29:46 +0900 | [diff] [blame] | 46 | unsigned int number_of_contexts; |
Geert Uytterhoeven | b7f3f04 | 2019-05-27 13:52:51 +0200 | [diff] [blame] | 47 | unsigned int num_utlbs; |
Magnus Damm | f5c8589 | 2017-10-16 21:30:28 +0900 | [diff] [blame] | 48 | bool setup_imbuscr; |
Magnus Damm | c295f50 | 2017-10-16 21:30:39 +0900 | [diff] [blame] | 49 | bool twobit_imttbcr_sl0; |
Yoshihiro Shimoda | 2ae8695 | 2018-07-09 11:53:31 +0900 | [diff] [blame] | 50 | bool reserved_context; |
Hai Nguyen Pham | 3623002 | 2019-09-04 14:08:02 +0200 | [diff] [blame] | 51 | bool cache_snoop; |
Yoshihiro Shimoda | 3dc28d9 | 2019-11-06 11:35:48 +0900 | [diff] [blame] | 52 | unsigned int ctx_offset_base; |
| 53 | unsigned int ctx_offset_stride; |
Yoshihiro Shimoda | 1289f7f | 2019-11-06 11:35:50 +0900 | [diff] [blame] | 54 | unsigned int utlb_offset_base; |
Magnus Damm | 33f3ac9 | 2017-10-16 21:29:25 +0900 | [diff] [blame] | 55 | }; |
| 56 | |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 57 | struct ipmmu_vmsa_device { |
| 58 | struct device *dev; |
| 59 | void __iomem *base; |
Magnus Damm | 01da21e | 2017-07-17 22:05:10 +0900 | [diff] [blame] | 60 | struct iommu_device iommu; |
Magnus Damm | fd5140e | 2017-10-16 21:29:36 +0900 | [diff] [blame] | 61 | struct ipmmu_vmsa_device *root; |
Magnus Damm | 33f3ac9 | 2017-10-16 21:29:25 +0900 | [diff] [blame] | 62 | const struct ipmmu_features *features; |
Magnus Damm | 5fd1634 | 2017-10-16 21:29:46 +0900 | [diff] [blame] | 63 | unsigned int num_ctx; |
Magnus Damm | dbb7069 | 2017-05-17 19:06:38 +0900 | [diff] [blame] | 64 | spinlock_t lock; /* Protects ctx and domains[] */ |
| 65 | DECLARE_BITMAP(ctx, IPMMU_CTX_MAX); |
| 66 | struct ipmmu_vmsa_domain *domains[IPMMU_CTX_MAX]; |
Geert Uytterhoeven | da38e9e | 2019-05-27 13:52:53 +0200 | [diff] [blame] | 67 | s8 utlb_ctx[IPMMU_UTLB_MAX]; |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 68 | |
Robin Murphy | b354c73 | 2017-10-13 19:23:40 +0100 | [diff] [blame] | 69 | struct iommu_group *group; |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 70 | struct dma_iommu_mapping *mapping; |
| 71 | }; |
| 72 | |
| 73 | struct ipmmu_vmsa_domain { |
| 74 | struct ipmmu_vmsa_device *mmu; |
Joerg Roedel | 5914c5f | 2015-03-26 13:43:16 +0100 | [diff] [blame] | 75 | struct iommu_domain io_domain; |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 76 | |
Laurent Pinchart | f20ed39 | 2015-01-20 18:30:04 +0200 | [diff] [blame] | 77 | struct io_pgtable_cfg cfg; |
| 78 | struct io_pgtable_ops *iop; |
| 79 | |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 80 | unsigned int context_id; |
Geert Uytterhoeven | 46583e8 | 2018-07-20 18:16:59 +0200 | [diff] [blame] | 81 | struct mutex mutex; /* Protects mappings */ |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 82 | }; |
| 83 | |
Joerg Roedel | 5914c5f | 2015-03-26 13:43:16 +0100 | [diff] [blame] | 84 | static struct ipmmu_vmsa_domain *to_vmsa_domain(struct iommu_domain *dom) |
| 85 | { |
| 86 | return container_of(dom, struct ipmmu_vmsa_domain, io_domain); |
| 87 | } |
| 88 | |
Robin Murphy | e4efe4a | 2017-10-13 19:23:41 +0100 | [diff] [blame] | 89 | static struct ipmmu_vmsa_device *to_ipmmu(struct device *dev) |
Magnus Damm | 0fbc8b0 | 2017-05-17 19:07:20 +0900 | [diff] [blame] | 90 | { |
Joerg Roedel | be568d6 | 2020-03-26 16:08:37 +0100 | [diff] [blame] | 91 | return dev_iommu_priv_get(dev); |
Magnus Damm | 0fbc8b0 | 2017-05-17 19:07:20 +0900 | [diff] [blame] | 92 | } |
| 93 | |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 94 | #define TLB_LOOP_TIMEOUT 100 /* 100us */ |
| 95 | |
| 96 | /* ----------------------------------------------------------------------------- |
| 97 | * Registers Definition |
| 98 | */ |
| 99 | |
Laurent Pinchart | 275f505 | 2014-03-17 01:02:46 +0100 | [diff] [blame] | 100 | #define IM_NS_ALIAS_OFFSET 0x800 |
| 101 | |
Yoshihiro Shimoda | df9828a | 2019-11-06 11:35:46 +0900 | [diff] [blame] | 102 | /* MMU "context" registers */ |
| 103 | #define IMCTR 0x0000 /* R-Car Gen2/3 */ |
| 104 | #define IMCTR_INTEN (1 << 2) /* R-Car Gen2/3 */ |
| 105 | #define IMCTR_FLUSH (1 << 1) /* R-Car Gen2/3 */ |
| 106 | #define IMCTR_MMUEN (1 << 0) /* R-Car Gen2/3 */ |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 107 | |
Yoshihiro Shimoda | df9828a | 2019-11-06 11:35:46 +0900 | [diff] [blame] | 108 | #define IMTTBCR 0x0008 /* R-Car Gen2/3 */ |
| 109 | #define IMTTBCR_EAE (1 << 31) /* R-Car Gen2/3 */ |
Hai Nguyen Pham | 3623002 | 2019-09-04 14:08:02 +0200 | [diff] [blame] | 110 | #define IMTTBCR_SH0_INNER_SHAREABLE (3 << 12) /* R-Car Gen2 only */ |
Hai Nguyen Pham | 3623002 | 2019-09-04 14:08:02 +0200 | [diff] [blame] | 111 | #define IMTTBCR_ORGN0_WB_WA (1 << 10) /* R-Car Gen2 only */ |
Hai Nguyen Pham | 3623002 | 2019-09-04 14:08:02 +0200 | [diff] [blame] | 112 | #define IMTTBCR_IRGN0_WB_WA (1 << 8) /* R-Car Gen2 only */ |
Geert Uytterhoeven | 5ca54fd | 2019-09-04 14:08:01 +0200 | [diff] [blame] | 113 | #define IMTTBCR_SL0_TWOBIT_LVL_1 (2 << 6) /* R-Car Gen3 only */ |
Yoshihiro Shimoda | df9828a | 2019-11-06 11:35:46 +0900 | [diff] [blame] | 114 | #define IMTTBCR_SL0_LVL_1 (1 << 4) /* R-Car Gen2 only */ |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 115 | |
Yoshihiro Shimoda | df9828a | 2019-11-06 11:35:46 +0900 | [diff] [blame] | 116 | #define IMBUSCR 0x000c /* R-Car Gen2 only */ |
| 117 | #define IMBUSCR_DVM (1 << 2) /* R-Car Gen2 only */ |
| 118 | #define IMBUSCR_BUSSEL_MASK (3 << 0) /* R-Car Gen2 only */ |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 119 | |
Yoshihiro Shimoda | df9828a | 2019-11-06 11:35:46 +0900 | [diff] [blame] | 120 | #define IMTTLBR0 0x0010 /* R-Car Gen2/3 */ |
| 121 | #define IMTTUBR0 0x0014 /* R-Car Gen2/3 */ |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 122 | |
Yoshihiro Shimoda | df9828a | 2019-11-06 11:35:46 +0900 | [diff] [blame] | 123 | #define IMSTR 0x0020 /* R-Car Gen2/3 */ |
| 124 | #define IMSTR_MHIT (1 << 4) /* R-Car Gen2/3 */ |
| 125 | #define IMSTR_ABORT (1 << 2) /* R-Car Gen2/3 */ |
| 126 | #define IMSTR_PF (1 << 1) /* R-Car Gen2/3 */ |
| 127 | #define IMSTR_TF (1 << 0) /* R-Car Gen2/3 */ |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 128 | |
Yoshihiro Shimoda | df9828a | 2019-11-06 11:35:46 +0900 | [diff] [blame] | 129 | #define IMMAIR0 0x0028 /* R-Car Gen2/3 */ |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 130 | |
Yoshihiro Shimoda | df9828a | 2019-11-06 11:35:46 +0900 | [diff] [blame] | 131 | #define IMELAR 0x0030 /* R-Car Gen2/3, IMEAR on R-Car Gen2 */ |
| 132 | #define IMEUAR 0x0034 /* R-Car Gen3 only */ |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 133 | |
Yoshihiro Shimoda | df9828a | 2019-11-06 11:35:46 +0900 | [diff] [blame] | 134 | /* uTLB registers */ |
Magnus Damm | ddbbddd | 2018-06-14 12:48:21 +0200 | [diff] [blame] | 135 | #define IMUCTR(n) ((n) < 32 ? IMUCTR0(n) : IMUCTR32(n)) |
Yoshihiro Shimoda | df9828a | 2019-11-06 11:35:46 +0900 | [diff] [blame] | 136 | #define IMUCTR0(n) (0x0300 + ((n) * 16)) /* R-Car Gen2/3 */ |
| 137 | #define IMUCTR32(n) (0x0600 + (((n) - 32) * 16)) /* R-Car Gen3 only */ |
| 138 | #define IMUCTR_TTSEL_MMU(n) ((n) << 4) /* R-Car Gen2/3 */ |
| 139 | #define IMUCTR_FLUSH (1 << 1) /* R-Car Gen2/3 */ |
| 140 | #define IMUCTR_MMUEN (1 << 0) /* R-Car Gen2/3 */ |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 141 | |
Magnus Damm | ddbbddd | 2018-06-14 12:48:21 +0200 | [diff] [blame] | 142 | #define IMUASID(n) ((n) < 32 ? IMUASID0(n) : IMUASID32(n)) |
Yoshihiro Shimoda | df9828a | 2019-11-06 11:35:46 +0900 | [diff] [blame] | 143 | #define IMUASID0(n) (0x0308 + ((n) * 16)) /* R-Car Gen2/3 */ |
| 144 | #define IMUASID32(n) (0x0608 + (((n) - 32) * 16)) /* R-Car Gen3 only */ |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 145 | |
| 146 | /* ----------------------------------------------------------------------------- |
Magnus Damm | fd5140e | 2017-10-16 21:29:36 +0900 | [diff] [blame] | 147 | * Root device handling |
| 148 | */ |
| 149 | |
| 150 | static struct platform_driver ipmmu_driver; |
| 151 | |
| 152 | static bool ipmmu_is_root(struct ipmmu_vmsa_device *mmu) |
| 153 | { |
| 154 | return mmu->root == mmu; |
| 155 | } |
| 156 | |
| 157 | static int __ipmmu_check_device(struct device *dev, void *data) |
| 158 | { |
| 159 | struct ipmmu_vmsa_device *mmu = dev_get_drvdata(dev); |
| 160 | struct ipmmu_vmsa_device **rootp = data; |
| 161 | |
| 162 | if (ipmmu_is_root(mmu)) |
| 163 | *rootp = mmu; |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | static struct ipmmu_vmsa_device *ipmmu_find_root(void) |
| 169 | { |
| 170 | struct ipmmu_vmsa_device *root = NULL; |
| 171 | |
| 172 | return driver_for_each_device(&ipmmu_driver.driver, NULL, &root, |
| 173 | __ipmmu_check_device) == 0 ? root : NULL; |
| 174 | } |
| 175 | |
| 176 | /* ----------------------------------------------------------------------------- |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 177 | * Read/Write Access |
| 178 | */ |
| 179 | |
| 180 | static u32 ipmmu_read(struct ipmmu_vmsa_device *mmu, unsigned int offset) |
| 181 | { |
| 182 | return ioread32(mmu->base + offset); |
| 183 | } |
| 184 | |
| 185 | static void ipmmu_write(struct ipmmu_vmsa_device *mmu, unsigned int offset, |
| 186 | u32 data) |
| 187 | { |
| 188 | iowrite32(data, mmu->base + offset); |
| 189 | } |
| 190 | |
Yoshihiro Shimoda | 16d9454 | 2019-11-06 11:35:47 +0900 | [diff] [blame] | 191 | static unsigned int ipmmu_ctx_reg(struct ipmmu_vmsa_device *mmu, |
| 192 | unsigned int context_id, unsigned int reg) |
| 193 | { |
Yoshihiro Shimoda | 3dc28d9 | 2019-11-06 11:35:48 +0900 | [diff] [blame] | 194 | return mmu->features->ctx_offset_base + |
| 195 | context_id * mmu->features->ctx_offset_stride + reg; |
Yoshihiro Shimoda | 16d9454 | 2019-11-06 11:35:47 +0900 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | static u32 ipmmu_ctx_read(struct ipmmu_vmsa_device *mmu, |
| 199 | unsigned int context_id, unsigned int reg) |
| 200 | { |
| 201 | return ipmmu_read(mmu, ipmmu_ctx_reg(mmu, context_id, reg)); |
| 202 | } |
| 203 | |
| 204 | static void ipmmu_ctx_write(struct ipmmu_vmsa_device *mmu, |
| 205 | unsigned int context_id, unsigned int reg, u32 data) |
| 206 | { |
| 207 | ipmmu_write(mmu, ipmmu_ctx_reg(mmu, context_id, reg), data); |
| 208 | } |
| 209 | |
Magnus Damm | d574893 | 2017-10-16 21:30:18 +0900 | [diff] [blame] | 210 | static u32 ipmmu_ctx_read_root(struct ipmmu_vmsa_domain *domain, |
| 211 | unsigned int reg) |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 212 | { |
Yoshihiro Shimoda | 16d9454 | 2019-11-06 11:35:47 +0900 | [diff] [blame] | 213 | return ipmmu_ctx_read(domain->mmu->root, domain->context_id, reg); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 214 | } |
| 215 | |
Magnus Damm | d574893 | 2017-10-16 21:30:18 +0900 | [diff] [blame] | 216 | static void ipmmu_ctx_write_root(struct ipmmu_vmsa_domain *domain, |
| 217 | unsigned int reg, u32 data) |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 218 | { |
Yoshihiro Shimoda | 16d9454 | 2019-11-06 11:35:47 +0900 | [diff] [blame] | 219 | ipmmu_ctx_write(domain->mmu->root, domain->context_id, reg, data); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 220 | } |
| 221 | |
Magnus Damm | d574893 | 2017-10-16 21:30:18 +0900 | [diff] [blame] | 222 | static void ipmmu_ctx_write_all(struct ipmmu_vmsa_domain *domain, |
| 223 | unsigned int reg, u32 data) |
| 224 | { |
| 225 | if (domain->mmu != domain->mmu->root) |
Yoshihiro Shimoda | 16d9454 | 2019-11-06 11:35:47 +0900 | [diff] [blame] | 226 | ipmmu_ctx_write(domain->mmu, domain->context_id, reg, data); |
Magnus Damm | d574893 | 2017-10-16 21:30:18 +0900 | [diff] [blame] | 227 | |
Yoshihiro Shimoda | 16d9454 | 2019-11-06 11:35:47 +0900 | [diff] [blame] | 228 | ipmmu_ctx_write(domain->mmu->root, domain->context_id, reg, data); |
Magnus Damm | d574893 | 2017-10-16 21:30:18 +0900 | [diff] [blame] | 229 | } |
| 230 | |
Yoshihiro Shimoda | 3667c99 | 2019-11-06 11:35:49 +0900 | [diff] [blame] | 231 | static u32 ipmmu_utlb_reg(struct ipmmu_vmsa_device *mmu, unsigned int reg) |
| 232 | { |
Yoshihiro Shimoda | 1289f7f | 2019-11-06 11:35:50 +0900 | [diff] [blame] | 233 | return mmu->features->utlb_offset_base + reg; |
Yoshihiro Shimoda | 3667c99 | 2019-11-06 11:35:49 +0900 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | static void ipmmu_imuasid_write(struct ipmmu_vmsa_device *mmu, |
| 237 | unsigned int utlb, u32 data) |
| 238 | { |
| 239 | ipmmu_write(mmu, ipmmu_utlb_reg(mmu, IMUASID(utlb)), data); |
| 240 | } |
| 241 | |
| 242 | static void ipmmu_imuctr_write(struct ipmmu_vmsa_device *mmu, |
| 243 | unsigned int utlb, u32 data) |
| 244 | { |
| 245 | ipmmu_write(mmu, ipmmu_utlb_reg(mmu, IMUCTR(utlb)), data); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | /* ----------------------------------------------------------------------------- |
| 249 | * TLB and microTLB Management |
| 250 | */ |
| 251 | |
| 252 | /* Wait for any pending TLB invalidations to complete */ |
| 253 | static void ipmmu_tlb_sync(struct ipmmu_vmsa_domain *domain) |
| 254 | { |
| 255 | unsigned int count = 0; |
| 256 | |
Magnus Damm | d574893 | 2017-10-16 21:30:18 +0900 | [diff] [blame] | 257 | while (ipmmu_ctx_read_root(domain, IMCTR) & IMCTR_FLUSH) { |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 258 | cpu_relax(); |
| 259 | if (++count == TLB_LOOP_TIMEOUT) { |
| 260 | dev_err_ratelimited(domain->mmu->dev, |
| 261 | "TLB sync timed out -- MMU may be deadlocked\n"); |
| 262 | return; |
| 263 | } |
| 264 | udelay(1); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | static void ipmmu_tlb_invalidate(struct ipmmu_vmsa_domain *domain) |
| 269 | { |
| 270 | u32 reg; |
| 271 | |
Magnus Damm | d574893 | 2017-10-16 21:30:18 +0900 | [diff] [blame] | 272 | reg = ipmmu_ctx_read_root(domain, IMCTR); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 273 | reg |= IMCTR_FLUSH; |
Magnus Damm | d574893 | 2017-10-16 21:30:18 +0900 | [diff] [blame] | 274 | ipmmu_ctx_write_all(domain, IMCTR, reg); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 275 | |
| 276 | ipmmu_tlb_sync(domain); |
| 277 | } |
| 278 | |
| 279 | /* |
| 280 | * Enable MMU translation for the microTLB. |
| 281 | */ |
| 282 | static void ipmmu_utlb_enable(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 | /* |
| 288 | * TODO: Reference-count the microTLB as several bus masters can be |
| 289 | * connected to the same microTLB. |
| 290 | */ |
| 291 | |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 292 | /* TODO: What should we set the ASID to ? */ |
Yoshihiro Shimoda | 3667c99 | 2019-11-06 11:35:49 +0900 | [diff] [blame] | 293 | ipmmu_imuasid_write(mmu, utlb, 0); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 294 | /* TODO: Do we need to flush the microTLB ? */ |
Yoshihiro Shimoda | 3667c99 | 2019-11-06 11:35:49 +0900 | [diff] [blame] | 295 | ipmmu_imuctr_write(mmu, utlb, IMUCTR_TTSEL_MMU(domain->context_id) | |
| 296 | IMUCTR_FLUSH | IMUCTR_MMUEN); |
Geert Uytterhoeven | da38e9e | 2019-05-27 13:52:53 +0200 | [diff] [blame] | 297 | mmu->utlb_ctx[utlb] = domain->context_id; |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | /* |
| 301 | * Disable MMU translation for the microTLB. |
| 302 | */ |
| 303 | static void ipmmu_utlb_disable(struct ipmmu_vmsa_domain *domain, |
Laurent Pinchart | 192d204 | 2014-05-15 12:40:42 +0200 | [diff] [blame] | 304 | unsigned int utlb) |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 305 | { |
| 306 | struct ipmmu_vmsa_device *mmu = domain->mmu; |
| 307 | |
Yoshihiro Shimoda | 3667c99 | 2019-11-06 11:35:49 +0900 | [diff] [blame] | 308 | ipmmu_imuctr_write(mmu, utlb, 0); |
Geert Uytterhoeven | da38e9e | 2019-05-27 13:52:53 +0200 | [diff] [blame] | 309 | mmu->utlb_ctx[utlb] = IPMMU_CTX_INVALID; |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 310 | } |
| 311 | |
Laurent Pinchart | f20ed39 | 2015-01-20 18:30:04 +0200 | [diff] [blame] | 312 | static void ipmmu_tlb_flush_all(void *cookie) |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 313 | { |
Laurent Pinchart | f20ed39 | 2015-01-20 18:30:04 +0200 | [diff] [blame] | 314 | struct ipmmu_vmsa_domain *domain = cookie; |
| 315 | |
| 316 | ipmmu_tlb_invalidate(domain); |
| 317 | } |
| 318 | |
Will Deacon | 05aed94 | 2019-07-02 16:44:25 +0100 | [diff] [blame] | 319 | static void ipmmu_tlb_flush(unsigned long iova, size_t size, |
| 320 | size_t granule, void *cookie) |
Laurent Pinchart | f20ed39 | 2015-01-20 18:30:04 +0200 | [diff] [blame] | 321 | { |
Will Deacon | 05aed94 | 2019-07-02 16:44:25 +0100 | [diff] [blame] | 322 | ipmmu_tlb_flush_all(cookie); |
Laurent Pinchart | f20ed39 | 2015-01-20 18:30:04 +0200 | [diff] [blame] | 323 | } |
| 324 | |
Will Deacon | 298f7889 | 2019-07-02 16:43:34 +0100 | [diff] [blame] | 325 | static const struct iommu_flush_ops ipmmu_flush_ops = { |
Laurent Pinchart | f20ed39 | 2015-01-20 18:30:04 +0200 | [diff] [blame] | 326 | .tlb_flush_all = ipmmu_tlb_flush_all, |
Will Deacon | 05aed94 | 2019-07-02 16:44:25 +0100 | [diff] [blame] | 327 | .tlb_flush_walk = ipmmu_tlb_flush, |
Laurent Pinchart | f20ed39 | 2015-01-20 18:30:04 +0200 | [diff] [blame] | 328 | }; |
| 329 | |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 330 | /* ----------------------------------------------------------------------------- |
| 331 | * Domain/Context Management |
| 332 | */ |
| 333 | |
Magnus Damm | dbb7069 | 2017-05-17 19:06:38 +0900 | [diff] [blame] | 334 | static int ipmmu_domain_allocate_context(struct ipmmu_vmsa_device *mmu, |
| 335 | struct ipmmu_vmsa_domain *domain) |
| 336 | { |
| 337 | unsigned long flags; |
| 338 | int ret; |
| 339 | |
| 340 | spin_lock_irqsave(&mmu->lock, flags); |
| 341 | |
Magnus Damm | 5fd1634 | 2017-10-16 21:29:46 +0900 | [diff] [blame] | 342 | ret = find_first_zero_bit(mmu->ctx, mmu->num_ctx); |
| 343 | if (ret != mmu->num_ctx) { |
Magnus Damm | dbb7069 | 2017-05-17 19:06:38 +0900 | [diff] [blame] | 344 | mmu->domains[ret] = domain; |
| 345 | set_bit(ret, mmu->ctx); |
Magnus Damm | 5fd1634 | 2017-10-16 21:29:46 +0900 | [diff] [blame] | 346 | } else |
| 347 | ret = -EBUSY; |
Magnus Damm | dbb7069 | 2017-05-17 19:06:38 +0900 | [diff] [blame] | 348 | |
| 349 | spin_unlock_irqrestore(&mmu->lock, flags); |
| 350 | |
| 351 | return ret; |
| 352 | } |
| 353 | |
Oleksandr Tyshchenko | a175a67 | 2017-08-23 17:31:42 +0300 | [diff] [blame] | 354 | static void ipmmu_domain_free_context(struct ipmmu_vmsa_device *mmu, |
| 355 | unsigned int context_id) |
| 356 | { |
| 357 | unsigned long flags; |
| 358 | |
| 359 | spin_lock_irqsave(&mmu->lock, flags); |
| 360 | |
| 361 | clear_bit(context_id, mmu->ctx); |
| 362 | mmu->domains[context_id] = NULL; |
| 363 | |
| 364 | spin_unlock_irqrestore(&mmu->lock, flags); |
| 365 | } |
| 366 | |
Geert Uytterhoeven | 892db54 | 2019-05-27 13:52:52 +0200 | [diff] [blame] | 367 | static void ipmmu_domain_setup_context(struct ipmmu_vmsa_domain *domain) |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 368 | { |
Geert Uytterhoeven | f64232e | 2015-12-22 20:01:06 +0100 | [diff] [blame] | 369 | u64 ttbr; |
Magnus Damm | c295f50 | 2017-10-16 21:30:39 +0900 | [diff] [blame] | 370 | u32 tmp; |
Oleksandr Tyshchenko | a175a67 | 2017-08-23 17:31:42 +0300 | [diff] [blame] | 371 | |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 372 | /* TTBR0 */ |
Robin Murphy | d1e5f26 | 2019-10-25 19:08:37 +0100 | [diff] [blame] | 373 | ttbr = domain->cfg.arm_lpae_s1_cfg.ttbr; |
Magnus Damm | d574893 | 2017-10-16 21:30:18 +0900 | [diff] [blame] | 374 | ipmmu_ctx_write_root(domain, IMTTLBR0, ttbr); |
| 375 | ipmmu_ctx_write_root(domain, IMTTUBR0, ttbr >> 32); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 376 | |
| 377 | /* |
| 378 | * TTBCR |
Hai Nguyen Pham | 3623002 | 2019-09-04 14:08:02 +0200 | [diff] [blame] | 379 | * We use long descriptors and allocate the whole 32-bit VA space to |
| 380 | * TTBR0. |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 381 | */ |
Magnus Damm | c295f50 | 2017-10-16 21:30:39 +0900 | [diff] [blame] | 382 | if (domain->mmu->features->twobit_imttbcr_sl0) |
| 383 | tmp = IMTTBCR_SL0_TWOBIT_LVL_1; |
| 384 | else |
| 385 | tmp = IMTTBCR_SL0_LVL_1; |
| 386 | |
Hai Nguyen Pham | 3623002 | 2019-09-04 14:08:02 +0200 | [diff] [blame] | 387 | if (domain->mmu->features->cache_snoop) |
| 388 | tmp |= IMTTBCR_SH0_INNER_SHAREABLE | IMTTBCR_ORGN0_WB_WA | |
| 389 | IMTTBCR_IRGN0_WB_WA; |
| 390 | |
| 391 | ipmmu_ctx_write_root(domain, IMTTBCR, IMTTBCR_EAE | tmp); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 392 | |
Laurent Pinchart | f20ed39 | 2015-01-20 18:30:04 +0200 | [diff] [blame] | 393 | /* MAIR0 */ |
Magnus Damm | d574893 | 2017-10-16 21:30:18 +0900 | [diff] [blame] | 394 | ipmmu_ctx_write_root(domain, IMMAIR0, |
Robin Murphy | 205577a | 2019-10-25 19:08:36 +0100 | [diff] [blame] | 395 | domain->cfg.arm_lpae_s1_cfg.mair); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 396 | |
| 397 | /* IMBUSCR */ |
Magnus Damm | f5c8589 | 2017-10-16 21:30:28 +0900 | [diff] [blame] | 398 | if (domain->mmu->features->setup_imbuscr) |
| 399 | ipmmu_ctx_write_root(domain, IMBUSCR, |
| 400 | ipmmu_ctx_read_root(domain, IMBUSCR) & |
| 401 | ~(IMBUSCR_DVM | IMBUSCR_BUSSEL_MASK)); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 402 | |
| 403 | /* |
| 404 | * IMSTR |
| 405 | * Clear all interrupt flags. |
| 406 | */ |
Magnus Damm | d574893 | 2017-10-16 21:30:18 +0900 | [diff] [blame] | 407 | ipmmu_ctx_write_root(domain, IMSTR, ipmmu_ctx_read_root(domain, IMSTR)); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 408 | |
| 409 | /* |
| 410 | * IMCTR |
| 411 | * Enable the MMU and interrupt generation. The long-descriptor |
| 412 | * translation table format doesn't use TEX remapping. Don't enable AF |
| 413 | * software management as we have no use for it. Flush the TLB as |
| 414 | * required when modifying the context registers. |
| 415 | */ |
Magnus Damm | d574893 | 2017-10-16 21:30:18 +0900 | [diff] [blame] | 416 | ipmmu_ctx_write_all(domain, IMCTR, |
| 417 | IMCTR_INTEN | IMCTR_FLUSH | IMCTR_MMUEN); |
Geert Uytterhoeven | 892db54 | 2019-05-27 13:52:52 +0200 | [diff] [blame] | 418 | } |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 419 | |
Geert Uytterhoeven | 892db54 | 2019-05-27 13:52:52 +0200 | [diff] [blame] | 420 | static int ipmmu_domain_init_context(struct ipmmu_vmsa_domain *domain) |
| 421 | { |
| 422 | int ret; |
| 423 | |
| 424 | /* |
| 425 | * Allocate the page table operations. |
| 426 | * |
| 427 | * VMSA states in section B3.6.3 "Control of Secure or Non-secure memory |
| 428 | * access, Long-descriptor format" that the NStable bit being set in a |
| 429 | * table descriptor will result in the NStable and NS bits of all child |
| 430 | * entries being ignored and considered as being set. The IPMMU seems |
| 431 | * not to comply with this, as it generates a secure access page fault |
| 432 | * if any of the NStable and NS bits isn't set when running in |
| 433 | * non-secure mode. |
| 434 | */ |
| 435 | domain->cfg.quirks = IO_PGTABLE_QUIRK_ARM_NS; |
| 436 | domain->cfg.pgsize_bitmap = SZ_1G | SZ_2M | SZ_4K; |
| 437 | domain->cfg.ias = 32; |
| 438 | domain->cfg.oas = 40; |
Will Deacon | 298f7889 | 2019-07-02 16:43:34 +0100 | [diff] [blame] | 439 | domain->cfg.tlb = &ipmmu_flush_ops; |
Geert Uytterhoeven | 892db54 | 2019-05-27 13:52:52 +0200 | [diff] [blame] | 440 | domain->io_domain.geometry.aperture_end = DMA_BIT_MASK(32); |
| 441 | domain->io_domain.geometry.force_aperture = true; |
| 442 | /* |
| 443 | * TODO: Add support for coherent walk through CCI with DVM and remove |
| 444 | * cache handling. For now, delegate it to the io-pgtable code. |
| 445 | */ |
Joerg Roedel | 3430abd | 2019-07-01 14:41:24 +0200 | [diff] [blame] | 446 | domain->cfg.coherent_walk = false; |
Geert Uytterhoeven | 892db54 | 2019-05-27 13:52:52 +0200 | [diff] [blame] | 447 | domain->cfg.iommu_dev = domain->mmu->root->dev; |
| 448 | |
| 449 | /* |
| 450 | * Find an unused context. |
| 451 | */ |
| 452 | ret = ipmmu_domain_allocate_context(domain->mmu->root, domain); |
| 453 | if (ret < 0) |
| 454 | return ret; |
| 455 | |
| 456 | domain->context_id = ret; |
| 457 | |
| 458 | domain->iop = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &domain->cfg, |
| 459 | domain); |
| 460 | if (!domain->iop) { |
| 461 | ipmmu_domain_free_context(domain->mmu->root, |
| 462 | domain->context_id); |
| 463 | return -EINVAL; |
| 464 | } |
| 465 | |
| 466 | ipmmu_domain_setup_context(domain); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | static void ipmmu_domain_destroy_context(struct ipmmu_vmsa_domain *domain) |
| 471 | { |
Geert Uytterhoeven | e5b78f2 | 2018-11-07 14:18:50 +0100 | [diff] [blame] | 472 | if (!domain->mmu) |
| 473 | return; |
| 474 | |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 475 | /* |
| 476 | * Disable the context. Flush the TLB as required when modifying the |
| 477 | * context registers. |
| 478 | * |
| 479 | * TODO: Is TLB flush really needed ? |
| 480 | */ |
Magnus Damm | d574893 | 2017-10-16 21:30:18 +0900 | [diff] [blame] | 481 | ipmmu_ctx_write_all(domain, IMCTR, IMCTR_FLUSH); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 482 | ipmmu_tlb_sync(domain); |
Magnus Damm | fd5140e | 2017-10-16 21:29:36 +0900 | [diff] [blame] | 483 | ipmmu_domain_free_context(domain->mmu->root, domain->context_id); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | /* ----------------------------------------------------------------------------- |
| 487 | * Fault Handling |
| 488 | */ |
| 489 | |
| 490 | static irqreturn_t ipmmu_domain_irq(struct ipmmu_vmsa_domain *domain) |
| 491 | { |
| 492 | const u32 err_mask = IMSTR_MHIT | IMSTR_ABORT | IMSTR_PF | IMSTR_TF; |
| 493 | struct ipmmu_vmsa_device *mmu = domain->mmu; |
Geert Uytterhoeven | 82576aa8 | 2019-05-27 13:52:49 +0200 | [diff] [blame] | 494 | unsigned long iova; |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 495 | u32 status; |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 496 | |
Magnus Damm | d574893 | 2017-10-16 21:30:18 +0900 | [diff] [blame] | 497 | status = ipmmu_ctx_read_root(domain, IMSTR); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 498 | if (!(status & err_mask)) |
| 499 | return IRQ_NONE; |
| 500 | |
Geert Uytterhoeven | 82576aa8 | 2019-05-27 13:52:49 +0200 | [diff] [blame] | 501 | iova = ipmmu_ctx_read_root(domain, IMELAR); |
| 502 | if (IS_ENABLED(CONFIG_64BIT)) |
| 503 | iova |= (u64)ipmmu_ctx_read_root(domain, IMEUAR) << 32; |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 504 | |
| 505 | /* |
| 506 | * Clear the error status flags. Unlike traditional interrupt flag |
| 507 | * registers that must be cleared by writing 1, this status register |
| 508 | * seems to require 0. The error address register must be read before, |
| 509 | * otherwise its value will be 0. |
| 510 | */ |
Magnus Damm | d574893 | 2017-10-16 21:30:18 +0900 | [diff] [blame] | 511 | ipmmu_ctx_write_root(domain, IMSTR, 0); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 512 | |
| 513 | /* Log fatal errors. */ |
| 514 | if (status & IMSTR_MHIT) |
Geert Uytterhoeven | 82576aa8 | 2019-05-27 13:52:49 +0200 | [diff] [blame] | 515 | dev_err_ratelimited(mmu->dev, "Multiple TLB hits @0x%lx\n", |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 516 | iova); |
| 517 | if (status & IMSTR_ABORT) |
Geert Uytterhoeven | 82576aa8 | 2019-05-27 13:52:49 +0200 | [diff] [blame] | 518 | dev_err_ratelimited(mmu->dev, "Page Table Walk Abort @0x%lx\n", |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 519 | iova); |
| 520 | |
| 521 | if (!(status & (IMSTR_PF | IMSTR_TF))) |
| 522 | return IRQ_NONE; |
| 523 | |
| 524 | /* |
| 525 | * Try to handle page faults and translation faults. |
| 526 | * |
| 527 | * TODO: We need to look up the faulty device based on the I/O VA. Use |
| 528 | * the IOMMU device for now. |
| 529 | */ |
Joerg Roedel | 5914c5f | 2015-03-26 13:43:16 +0100 | [diff] [blame] | 530 | if (!report_iommu_fault(&domain->io_domain, mmu->dev, iova, 0)) |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 531 | return IRQ_HANDLED; |
| 532 | |
| 533 | dev_err_ratelimited(mmu->dev, |
Geert Uytterhoeven | 82576aa8 | 2019-05-27 13:52:49 +0200 | [diff] [blame] | 534 | "Unhandled fault: status 0x%08x iova 0x%lx\n", |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 535 | status, iova); |
| 536 | |
| 537 | return IRQ_HANDLED; |
| 538 | } |
| 539 | |
| 540 | static irqreturn_t ipmmu_irq(int irq, void *dev) |
| 541 | { |
| 542 | struct ipmmu_vmsa_device *mmu = dev; |
Magnus Damm | dbb7069 | 2017-05-17 19:06:38 +0900 | [diff] [blame] | 543 | irqreturn_t status = IRQ_NONE; |
| 544 | unsigned int i; |
| 545 | unsigned long flags; |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 546 | |
Magnus Damm | dbb7069 | 2017-05-17 19:06:38 +0900 | [diff] [blame] | 547 | spin_lock_irqsave(&mmu->lock, flags); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 548 | |
Magnus Damm | dbb7069 | 2017-05-17 19:06:38 +0900 | [diff] [blame] | 549 | /* |
| 550 | * Check interrupts for all active contexts. |
| 551 | */ |
Magnus Damm | 5fd1634 | 2017-10-16 21:29:46 +0900 | [diff] [blame] | 552 | for (i = 0; i < mmu->num_ctx; i++) { |
Magnus Damm | dbb7069 | 2017-05-17 19:06:38 +0900 | [diff] [blame] | 553 | if (!mmu->domains[i]) |
| 554 | continue; |
| 555 | if (ipmmu_domain_irq(mmu->domains[i]) == IRQ_HANDLED) |
| 556 | status = IRQ_HANDLED; |
| 557 | } |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 558 | |
Magnus Damm | dbb7069 | 2017-05-17 19:06:38 +0900 | [diff] [blame] | 559 | spin_unlock_irqrestore(&mmu->lock, flags); |
| 560 | |
| 561 | return status; |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | /* ----------------------------------------------------------------------------- |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 565 | * IOMMU Operations |
| 566 | */ |
| 567 | |
Magnus Damm | 8e73bf6 | 2017-05-17 19:06:59 +0900 | [diff] [blame] | 568 | static struct iommu_domain *__ipmmu_domain_alloc(unsigned type) |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 569 | { |
| 570 | struct ipmmu_vmsa_domain *domain; |
| 571 | |
| 572 | domain = kzalloc(sizeof(*domain), GFP_KERNEL); |
| 573 | if (!domain) |
Joerg Roedel | 5914c5f | 2015-03-26 13:43:16 +0100 | [diff] [blame] | 574 | return NULL; |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 575 | |
Geert Uytterhoeven | 46583e8 | 2018-07-20 18:16:59 +0200 | [diff] [blame] | 576 | mutex_init(&domain->mutex); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 577 | |
Joerg Roedel | 5914c5f | 2015-03-26 13:43:16 +0100 | [diff] [blame] | 578 | return &domain->io_domain; |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 579 | } |
| 580 | |
Robin Murphy | 1c7e7c0 | 2017-10-13 19:23:39 +0100 | [diff] [blame] | 581 | static struct iommu_domain *ipmmu_domain_alloc(unsigned type) |
| 582 | { |
| 583 | struct iommu_domain *io_domain = NULL; |
| 584 | |
| 585 | switch (type) { |
| 586 | case IOMMU_DOMAIN_UNMANAGED: |
| 587 | io_domain = __ipmmu_domain_alloc(type); |
| 588 | break; |
| 589 | |
| 590 | case IOMMU_DOMAIN_DMA: |
| 591 | io_domain = __ipmmu_domain_alloc(type); |
| 592 | if (io_domain && iommu_get_dma_cookie(io_domain)) { |
| 593 | kfree(io_domain); |
| 594 | io_domain = NULL; |
| 595 | } |
| 596 | break; |
| 597 | } |
| 598 | |
| 599 | return io_domain; |
| 600 | } |
| 601 | |
Joerg Roedel | 5914c5f | 2015-03-26 13:43:16 +0100 | [diff] [blame] | 602 | static void ipmmu_domain_free(struct iommu_domain *io_domain) |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 603 | { |
Joerg Roedel | 5914c5f | 2015-03-26 13:43:16 +0100 | [diff] [blame] | 604 | struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 605 | |
| 606 | /* |
| 607 | * Free the domain resources. We assume that all devices have already |
| 608 | * been detached. |
| 609 | */ |
Robin Murphy | 1c7e7c0 | 2017-10-13 19:23:39 +0100 | [diff] [blame] | 610 | iommu_put_dma_cookie(io_domain); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 611 | ipmmu_domain_destroy_context(domain); |
Laurent Pinchart | f20ed39 | 2015-01-20 18:30:04 +0200 | [diff] [blame] | 612 | free_io_pgtable_ops(domain->iop); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 613 | kfree(domain); |
| 614 | } |
| 615 | |
| 616 | static int ipmmu_attach_device(struct iommu_domain *io_domain, |
| 617 | struct device *dev) |
| 618 | { |
Joerg Roedel | df90365 | 2018-11-29 14:01:00 +0100 | [diff] [blame] | 619 | struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); |
Robin Murphy | e4efe4a | 2017-10-13 19:23:41 +0100 | [diff] [blame] | 620 | struct ipmmu_vmsa_device *mmu = to_ipmmu(dev); |
Joerg Roedel | 5914c5f | 2015-03-26 13:43:16 +0100 | [diff] [blame] | 621 | struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain); |
Laurent Pinchart | a166d31 | 2014-07-24 01:36:43 +0200 | [diff] [blame] | 622 | unsigned int i; |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 623 | int ret = 0; |
| 624 | |
Robin Murphy | e4efe4a | 2017-10-13 19:23:41 +0100 | [diff] [blame] | 625 | if (!mmu) { |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 626 | dev_err(dev, "Cannot attach to IPMMU\n"); |
| 627 | return -ENXIO; |
| 628 | } |
| 629 | |
Geert Uytterhoeven | 46583e8 | 2018-07-20 18:16:59 +0200 | [diff] [blame] | 630 | mutex_lock(&domain->mutex); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 631 | |
| 632 | if (!domain->mmu) { |
| 633 | /* The domain hasn't been used yet, initialize it. */ |
| 634 | domain->mmu = mmu; |
| 635 | ret = ipmmu_domain_init_context(domain); |
Magnus Damm | 5fd1634 | 2017-10-16 21:29:46 +0900 | [diff] [blame] | 636 | if (ret < 0) { |
| 637 | dev_err(dev, "Unable to initialize IPMMU context\n"); |
| 638 | domain->mmu = NULL; |
| 639 | } else { |
| 640 | dev_info(dev, "Using IPMMU context %u\n", |
| 641 | domain->context_id); |
| 642 | } |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 643 | } else if (domain->mmu != mmu) { |
| 644 | /* |
| 645 | * Something is wrong, we can't attach two devices using |
| 646 | * different IOMMUs to the same domain. |
| 647 | */ |
| 648 | dev_err(dev, "Can't attach IPMMU %s to domain on IPMMU %s\n", |
| 649 | dev_name(mmu->dev), dev_name(domain->mmu->dev)); |
| 650 | ret = -EINVAL; |
Magnus Damm | 3ae4729 | 2017-05-17 19:07:10 +0900 | [diff] [blame] | 651 | } else |
| 652 | dev_info(dev, "Reusing IPMMU context %u\n", domain->context_id); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 653 | |
Geert Uytterhoeven | 46583e8 | 2018-07-20 18:16:59 +0200 | [diff] [blame] | 654 | mutex_unlock(&domain->mutex); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 655 | |
| 656 | if (ret < 0) |
| 657 | return ret; |
| 658 | |
Magnus Damm | 7b2d596 | 2017-07-17 22:05:41 +0900 | [diff] [blame] | 659 | for (i = 0; i < fwspec->num_ids; ++i) |
| 660 | ipmmu_utlb_enable(domain, fwspec->ids[i]); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 661 | |
| 662 | return 0; |
| 663 | } |
| 664 | |
| 665 | static void ipmmu_detach_device(struct iommu_domain *io_domain, |
| 666 | struct device *dev) |
| 667 | { |
Joerg Roedel | df90365 | 2018-11-29 14:01:00 +0100 | [diff] [blame] | 668 | struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); |
Joerg Roedel | 5914c5f | 2015-03-26 13:43:16 +0100 | [diff] [blame] | 669 | struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain); |
Laurent Pinchart | a166d31 | 2014-07-24 01:36:43 +0200 | [diff] [blame] | 670 | unsigned int i; |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 671 | |
Magnus Damm | 7b2d596 | 2017-07-17 22:05:41 +0900 | [diff] [blame] | 672 | for (i = 0; i < fwspec->num_ids; ++i) |
| 673 | ipmmu_utlb_disable(domain, fwspec->ids[i]); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 674 | |
| 675 | /* |
| 676 | * TODO: Optimize by disabling the context when no device is attached. |
| 677 | */ |
| 678 | } |
| 679 | |
| 680 | static int ipmmu_map(struct iommu_domain *io_domain, unsigned long iova, |
Tom Murphy | 781ca2d | 2019-09-08 09:56:38 -0700 | [diff] [blame] | 681 | phys_addr_t paddr, size_t size, int prot, gfp_t gfp) |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 682 | { |
Joerg Roedel | 5914c5f | 2015-03-26 13:43:16 +0100 | [diff] [blame] | 683 | struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 684 | |
| 685 | if (!domain) |
| 686 | return -ENODEV; |
| 687 | |
Baolin Wang | f34ce7a | 2020-06-12 11:39:55 +0800 | [diff] [blame] | 688 | return domain->iop->map(domain->iop, iova, paddr, size, prot, gfp); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | static size_t ipmmu_unmap(struct iommu_domain *io_domain, unsigned long iova, |
Will Deacon | 56f8af5 | 2019-07-02 16:44:06 +0100 | [diff] [blame] | 692 | size_t size, struct iommu_iotlb_gather *gather) |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 693 | { |
Joerg Roedel | 5914c5f | 2015-03-26 13:43:16 +0100 | [diff] [blame] | 694 | struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 695 | |
Will Deacon | a2d3a38 | 2019-07-02 16:44:58 +0100 | [diff] [blame] | 696 | return domain->iop->unmap(domain->iop, iova, size, gather); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 697 | } |
| 698 | |
Will Deacon | 56f8af5 | 2019-07-02 16:44:06 +0100 | [diff] [blame] | 699 | static void ipmmu_flush_iotlb_all(struct iommu_domain *io_domain) |
Robin Murphy | 32b1244 | 2017-09-28 15:55:01 +0100 | [diff] [blame] | 700 | { |
| 701 | struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain); |
| 702 | |
| 703 | if (domain->mmu) |
| 704 | ipmmu_tlb_flush_all(domain); |
| 705 | } |
| 706 | |
Will Deacon | 56f8af5 | 2019-07-02 16:44:06 +0100 | [diff] [blame] | 707 | static void ipmmu_iotlb_sync(struct iommu_domain *io_domain, |
| 708 | struct iommu_iotlb_gather *gather) |
| 709 | { |
| 710 | ipmmu_flush_iotlb_all(io_domain); |
| 711 | } |
| 712 | |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 713 | static phys_addr_t ipmmu_iova_to_phys(struct iommu_domain *io_domain, |
| 714 | dma_addr_t iova) |
| 715 | { |
Joerg Roedel | 5914c5f | 2015-03-26 13:43:16 +0100 | [diff] [blame] | 716 | struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 717 | |
| 718 | /* TODO: Is locking needed ? */ |
| 719 | |
Laurent Pinchart | f20ed39 | 2015-01-20 18:30:04 +0200 | [diff] [blame] | 720 | return domain->iop->iova_to_phys(domain->iop, iova); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 721 | } |
| 722 | |
Magnus Damm | 7b2d596 | 2017-07-17 22:05:41 +0900 | [diff] [blame] | 723 | static int ipmmu_init_platform_device(struct device *dev, |
| 724 | struct of_phandle_args *args) |
Laurent Pinchart | 192d204 | 2014-05-15 12:40:42 +0200 | [diff] [blame] | 725 | { |
Magnus Damm | 7b2d596 | 2017-07-17 22:05:41 +0900 | [diff] [blame] | 726 | struct platform_device *ipmmu_pdev; |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 727 | |
Magnus Damm | 7b2d596 | 2017-07-17 22:05:41 +0900 | [diff] [blame] | 728 | ipmmu_pdev = of_find_device_by_node(args->np); |
| 729 | if (!ipmmu_pdev) |
Laurent Pinchart | bb590c9 | 2015-01-24 23:13:50 +0200 | [diff] [blame] | 730 | return -ENODEV; |
| 731 | |
Joerg Roedel | be568d6 | 2020-03-26 16:08:37 +0100 | [diff] [blame] | 732 | dev_iommu_priv_set(dev, platform_get_drvdata(ipmmu_pdev)); |
Joerg Roedel | df90365 | 2018-11-29 14:01:00 +0100 | [diff] [blame] | 733 | |
Magnus Damm | 383fef5f | 2017-05-17 19:06:48 +0900 | [diff] [blame] | 734 | return 0; |
Magnus Damm | 383fef5f | 2017-05-17 19:06:48 +0900 | [diff] [blame] | 735 | } |
| 736 | |
Yoshihiro Shimoda | 815cdd8 | 2021-01-28 22:02:59 +0900 | [diff] [blame] | 737 | static const struct soc_device_attribute soc_needs_opt_in[] = { |
| 738 | { .family = "R-Car Gen3", }, |
| 739 | { .family = "RZ/G2", }, |
| 740 | { /* sentinel */ } |
| 741 | }; |
| 742 | |
| 743 | static const struct soc_device_attribute soc_denylist[] = { |
Fabrizio Castro | 60fb008 | 2018-08-23 16:33:04 +0100 | [diff] [blame] | 744 | { .soc_id = "r8a774a1", }, |
Yoshihiro Shimoda | 815cdd8 | 2021-01-28 22:02:59 +0900 | [diff] [blame] | 745 | { .soc_id = "r8a7795", .revision = "ES1.*" }, |
| 746 | { .soc_id = "r8a7795", .revision = "ES2.*" }, |
Magnus Damm | 0b8ac14 | 2018-06-14 12:48:22 +0200 | [diff] [blame] | 747 | { .soc_id = "r8a7796", }, |
Magnus Damm | 58b8e8bf | 2017-10-16 21:30:50 +0900 | [diff] [blame] | 748 | { /* sentinel */ } |
| 749 | }; |
| 750 | |
Yoshihiro Shimoda | 815cdd8 | 2021-01-28 22:02:59 +0900 | [diff] [blame] | 751 | static const char * const devices_allowlist[] = { |
Yoshihiro Shimoda | cec0813 | 2021-01-28 22:03:00 +0900 | [diff] [blame] | 752 | "ee100000.mmc", |
| 753 | "ee120000.mmc", |
| 754 | "ee140000.mmc", |
| 755 | "ee160000.mmc" |
Yoshihiro Shimoda | b7ee92c | 2018-11-28 09:23:36 +0000 | [diff] [blame] | 756 | }; |
| 757 | |
Yoshihiro Shimoda | 815cdd8 | 2021-01-28 22:02:59 +0900 | [diff] [blame] | 758 | static bool ipmmu_device_is_allowed(struct device *dev) |
Yoshihiro Shimoda | b7ee92c | 2018-11-28 09:23:36 +0000 | [diff] [blame] | 759 | { |
Yoshihiro Shimoda | 8075964 | 2018-11-28 09:23:36 +0000 | [diff] [blame] | 760 | unsigned int i; |
| 761 | |
Yoshihiro Shimoda | b7ee92c | 2018-11-28 09:23:36 +0000 | [diff] [blame] | 762 | /* |
Yoshihiro Shimoda | 815cdd8 | 2021-01-28 22:02:59 +0900 | [diff] [blame] | 763 | * R-Car Gen3 and RZ/G2 use the allow list to opt-in devices. |
Yoshihiro Shimoda | b7ee92c | 2018-11-28 09:23:36 +0000 | [diff] [blame] | 764 | * For Other SoCs, this returns true anyway. |
| 765 | */ |
Yoshihiro Shimoda | 815cdd8 | 2021-01-28 22:02:59 +0900 | [diff] [blame] | 766 | if (!soc_device_match(soc_needs_opt_in)) |
Yoshihiro Shimoda | b7ee92c | 2018-11-28 09:23:36 +0000 | [diff] [blame] | 767 | return true; |
| 768 | |
Yoshihiro Shimoda | 815cdd8 | 2021-01-28 22:02:59 +0900 | [diff] [blame] | 769 | /* Check whether this SoC can use the IPMMU correctly or not */ |
| 770 | if (soc_device_match(soc_denylist)) |
Yoshihiro Shimoda | b7ee92c | 2018-11-28 09:23:36 +0000 | [diff] [blame] | 771 | return false; |
| 772 | |
Yoshihiro Shimoda | 815cdd8 | 2021-01-28 22:02:59 +0900 | [diff] [blame] | 773 | /* Check whether this device can work with the IPMMU */ |
| 774 | for (i = 0; i < ARRAY_SIZE(devices_allowlist); i++) { |
| 775 | if (!strcmp(dev_name(dev), devices_allowlist[i])) |
Yoshihiro Shimoda | 8075964 | 2018-11-28 09:23:36 +0000 | [diff] [blame] | 776 | return true; |
| 777 | } |
| 778 | |
| 779 | /* Otherwise, do not allow use of IPMMU */ |
Yoshihiro Shimoda | b7ee92c | 2018-11-28 09:23:36 +0000 | [diff] [blame] | 780 | return false; |
| 781 | } |
| 782 | |
Magnus Damm | 49558da | 2017-07-17 22:05:20 +0900 | [diff] [blame] | 783 | static int ipmmu_of_xlate(struct device *dev, |
| 784 | struct of_phandle_args *spec) |
| 785 | { |
Yoshihiro Shimoda | 815cdd8 | 2021-01-28 22:02:59 +0900 | [diff] [blame] | 786 | if (!ipmmu_device_is_allowed(dev)) |
Magnus Damm | 58b8e8bf | 2017-10-16 21:30:50 +0900 | [diff] [blame] | 787 | return -ENODEV; |
| 788 | |
Magnus Damm | 7b2d596 | 2017-07-17 22:05:41 +0900 | [diff] [blame] | 789 | iommu_fwspec_add_ids(dev, spec->args, 1); |
| 790 | |
Magnus Damm | 49558da | 2017-07-17 22:05:20 +0900 | [diff] [blame] | 791 | /* Initialize once - xlate() will call multiple times */ |
Robin Murphy | e4efe4a | 2017-10-13 19:23:41 +0100 | [diff] [blame] | 792 | if (to_ipmmu(dev)) |
Magnus Damm | 49558da | 2017-07-17 22:05:20 +0900 | [diff] [blame] | 793 | return 0; |
| 794 | |
Magnus Damm | 7b2d596 | 2017-07-17 22:05:41 +0900 | [diff] [blame] | 795 | return ipmmu_init_platform_device(dev, spec); |
Magnus Damm | 49558da | 2017-07-17 22:05:20 +0900 | [diff] [blame] | 796 | } |
| 797 | |
Robin Murphy | 49c875f | 2017-10-13 19:23:42 +0100 | [diff] [blame] | 798 | static int ipmmu_init_arm_mapping(struct device *dev) |
Magnus Damm | 383fef5f | 2017-05-17 19:06:48 +0900 | [diff] [blame] | 799 | { |
Robin Murphy | e4efe4a | 2017-10-13 19:23:41 +0100 | [diff] [blame] | 800 | struct ipmmu_vmsa_device *mmu = to_ipmmu(dev); |
Magnus Damm | 383fef5f | 2017-05-17 19:06:48 +0900 | [diff] [blame] | 801 | int ret; |
| 802 | |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 803 | /* |
| 804 | * Create the ARM mapping, used by the ARM DMA mapping core to allocate |
| 805 | * VAs. This will allocate a corresponding IOMMU domain. |
| 806 | * |
| 807 | * TODO: |
| 808 | * - Create one mapping per context (TLB). |
| 809 | * - Make the mapping size configurable ? We currently use a 2GB mapping |
| 810 | * at a 1GB offset to ensure that NULL VAs will fault. |
| 811 | */ |
| 812 | if (!mmu->mapping) { |
| 813 | struct dma_iommu_mapping *mapping; |
| 814 | |
| 815 | mapping = arm_iommu_create_mapping(&platform_bus_type, |
Joerg Roedel | 720b0ce | 2014-05-26 13:07:01 +0200 | [diff] [blame] | 816 | SZ_1G, SZ_2G); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 817 | if (IS_ERR(mapping)) { |
| 818 | dev_err(mmu->dev, "failed to create ARM IOMMU mapping\n"); |
Laurent Pinchart | b8f80bf | 2014-03-14 14:00:56 +0100 | [diff] [blame] | 819 | ret = PTR_ERR(mapping); |
| 820 | goto error; |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | mmu->mapping = mapping; |
| 824 | } |
| 825 | |
| 826 | /* Attach the ARM VA mapping to the device. */ |
| 827 | ret = arm_iommu_attach_device(dev, mmu->mapping); |
| 828 | if (ret < 0) { |
| 829 | dev_err(dev, "Failed to attach device to VA mapping\n"); |
| 830 | goto error; |
| 831 | } |
| 832 | |
| 833 | return 0; |
| 834 | |
| 835 | error: |
Robin Murphy | 49c875f | 2017-10-13 19:23:42 +0100 | [diff] [blame] | 836 | if (mmu->mapping) |
Magnus Damm | 383fef5f | 2017-05-17 19:06:48 +0900 | [diff] [blame] | 837 | arm_iommu_release_mapping(mmu->mapping); |
Laurent Pinchart | a166d31 | 2014-07-24 01:36:43 +0200 | [diff] [blame] | 838 | |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 839 | return ret; |
| 840 | } |
| 841 | |
Joerg Roedel | 6580c8a | 2020-04-29 15:37:05 +0200 | [diff] [blame] | 842 | static struct iommu_device *ipmmu_probe_device(struct device *dev) |
Magnus Damm | 3ae4729 | 2017-05-17 19:07:10 +0900 | [diff] [blame] | 843 | { |
Geert Uytterhoeven | 80eaa9f | 2019-05-27 13:52:48 +0200 | [diff] [blame] | 844 | struct ipmmu_vmsa_device *mmu = to_ipmmu(dev); |
Magnus Damm | 3ae4729 | 2017-05-17 19:07:10 +0900 | [diff] [blame] | 845 | |
Magnus Damm | 0fbc8b0 | 2017-05-17 19:07:20 +0900 | [diff] [blame] | 846 | /* |
| 847 | * Only let through devices that have been verified in xlate() |
Magnus Damm | 0fbc8b0 | 2017-05-17 19:07:20 +0900 | [diff] [blame] | 848 | */ |
Geert Uytterhoeven | 80eaa9f | 2019-05-27 13:52:48 +0200 | [diff] [blame] | 849 | if (!mmu) |
Joerg Roedel | 6580c8a | 2020-04-29 15:37:05 +0200 | [diff] [blame] | 850 | return ERR_PTR(-ENODEV); |
Magnus Damm | 3ae4729 | 2017-05-17 19:07:10 +0900 | [diff] [blame] | 851 | |
Joerg Roedel | 6580c8a | 2020-04-29 15:37:05 +0200 | [diff] [blame] | 852 | return &mmu->iommu; |
Magnus Damm | 3ae4729 | 2017-05-17 19:07:10 +0900 | [diff] [blame] | 853 | } |
| 854 | |
Joerg Roedel | 6580c8a | 2020-04-29 15:37:05 +0200 | [diff] [blame] | 855 | static void ipmmu_probe_finalize(struct device *dev) |
Magnus Damm | 3ae4729 | 2017-05-17 19:07:10 +0900 | [diff] [blame] | 856 | { |
Joerg Roedel | 6580c8a | 2020-04-29 15:37:05 +0200 | [diff] [blame] | 857 | int ret = 0; |
Geert Uytterhoeven | 80eaa9f | 2019-05-27 13:52:48 +0200 | [diff] [blame] | 858 | |
Joerg Roedel | 6580c8a | 2020-04-29 15:37:05 +0200 | [diff] [blame] | 859 | if (IS_ENABLED(CONFIG_ARM) && !IS_ENABLED(CONFIG_IOMMU_DMA)) |
| 860 | ret = ipmmu_init_arm_mapping(dev); |
| 861 | |
| 862 | if (ret) |
| 863 | dev_err(dev, "Can't create IOMMU mapping - DMA-OPS will not work\n"); |
| 864 | } |
| 865 | |
| 866 | static void ipmmu_release_device(struct device *dev) |
| 867 | { |
Robin Murphy | 49c875f | 2017-10-13 19:23:42 +0100 | [diff] [blame] | 868 | arm_iommu_detach_device(dev); |
Magnus Damm | 3ae4729 | 2017-05-17 19:07:10 +0900 | [diff] [blame] | 869 | } |
| 870 | |
Robin Murphy | b354c73 | 2017-10-13 19:23:40 +0100 | [diff] [blame] | 871 | static struct iommu_group *ipmmu_find_group(struct device *dev) |
Magnus Damm | 3ae4729 | 2017-05-17 19:07:10 +0900 | [diff] [blame] | 872 | { |
Robin Murphy | e4efe4a | 2017-10-13 19:23:41 +0100 | [diff] [blame] | 873 | struct ipmmu_vmsa_device *mmu = to_ipmmu(dev); |
Magnus Damm | 3ae4729 | 2017-05-17 19:07:10 +0900 | [diff] [blame] | 874 | struct iommu_group *group; |
Magnus Damm | 3ae4729 | 2017-05-17 19:07:10 +0900 | [diff] [blame] | 875 | |
Robin Murphy | e4efe4a | 2017-10-13 19:23:41 +0100 | [diff] [blame] | 876 | if (mmu->group) |
| 877 | return iommu_group_ref_get(mmu->group); |
Robin Murphy | b354c73 | 2017-10-13 19:23:40 +0100 | [diff] [blame] | 878 | |
| 879 | group = iommu_group_alloc(); |
| 880 | if (!IS_ERR(group)) |
Robin Murphy | e4efe4a | 2017-10-13 19:23:41 +0100 | [diff] [blame] | 881 | mmu->group = group; |
Magnus Damm | 3ae4729 | 2017-05-17 19:07:10 +0900 | [diff] [blame] | 882 | |
| 883 | return group; |
| 884 | } |
| 885 | |
Magnus Damm | 3ae4729 | 2017-05-17 19:07:10 +0900 | [diff] [blame] | 886 | static const struct iommu_ops ipmmu_ops = { |
Robin Murphy | 1c7e7c0 | 2017-10-13 19:23:39 +0100 | [diff] [blame] | 887 | .domain_alloc = ipmmu_domain_alloc, |
| 888 | .domain_free = ipmmu_domain_free, |
Magnus Damm | 3ae4729 | 2017-05-17 19:07:10 +0900 | [diff] [blame] | 889 | .attach_dev = ipmmu_attach_device, |
| 890 | .detach_dev = ipmmu_detach_device, |
| 891 | .map = ipmmu_map, |
| 892 | .unmap = ipmmu_unmap, |
Will Deacon | 56f8af5 | 2019-07-02 16:44:06 +0100 | [diff] [blame] | 893 | .flush_iotlb_all = ipmmu_flush_iotlb_all, |
Robin Murphy | 32b1244 | 2017-09-28 15:55:01 +0100 | [diff] [blame] | 894 | .iotlb_sync = ipmmu_iotlb_sync, |
Magnus Damm | 3ae4729 | 2017-05-17 19:07:10 +0900 | [diff] [blame] | 895 | .iova_to_phys = ipmmu_iova_to_phys, |
Joerg Roedel | 6580c8a | 2020-04-29 15:37:05 +0200 | [diff] [blame] | 896 | .probe_device = ipmmu_probe_device, |
| 897 | .release_device = ipmmu_release_device, |
| 898 | .probe_finalize = ipmmu_probe_finalize, |
Arnd Bergmann | 2ba20b5 | 2020-05-09 00:02:16 +0200 | [diff] [blame] | 899 | .device_group = IS_ENABLED(CONFIG_ARM) && !IS_ENABLED(CONFIG_IOMMU_DMA) |
| 900 | ? generic_device_group : ipmmu_find_group, |
Magnus Damm | 3ae4729 | 2017-05-17 19:07:10 +0900 | [diff] [blame] | 901 | .pgsize_bitmap = SZ_1G | SZ_2M | SZ_4K, |
Magnus Damm | 49558da | 2017-07-17 22:05:20 +0900 | [diff] [blame] | 902 | .of_xlate = ipmmu_of_xlate, |
Magnus Damm | 3ae4729 | 2017-05-17 19:07:10 +0900 | [diff] [blame] | 903 | }; |
| 904 | |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 905 | /* ----------------------------------------------------------------------------- |
| 906 | * Probe/remove and init |
| 907 | */ |
| 908 | |
| 909 | static void ipmmu_device_reset(struct ipmmu_vmsa_device *mmu) |
| 910 | { |
| 911 | unsigned int i; |
| 912 | |
| 913 | /* Disable all contexts. */ |
Magnus Damm | 5fd1634 | 2017-10-16 21:29:46 +0900 | [diff] [blame] | 914 | for (i = 0; i < mmu->num_ctx; ++i) |
Yoshihiro Shimoda | 16d9454 | 2019-11-06 11:35:47 +0900 | [diff] [blame] | 915 | ipmmu_ctx_write(mmu, i, IMCTR, 0); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 916 | } |
| 917 | |
Magnus Damm | 33f3ac9 | 2017-10-16 21:29:25 +0900 | [diff] [blame] | 918 | static const struct ipmmu_features ipmmu_features_default = { |
| 919 | .use_ns_alias_offset = true, |
Magnus Damm | fd5140e | 2017-10-16 21:29:36 +0900 | [diff] [blame] | 920 | .has_cache_leaf_nodes = false, |
Magnus Damm | 5fd1634 | 2017-10-16 21:29:46 +0900 | [diff] [blame] | 921 | .number_of_contexts = 1, /* software only tested with one context */ |
Geert Uytterhoeven | b7f3f04 | 2019-05-27 13:52:51 +0200 | [diff] [blame] | 922 | .num_utlbs = 32, |
Magnus Damm | f5c8589 | 2017-10-16 21:30:28 +0900 | [diff] [blame] | 923 | .setup_imbuscr = true, |
Magnus Damm | c295f50 | 2017-10-16 21:30:39 +0900 | [diff] [blame] | 924 | .twobit_imttbcr_sl0 = false, |
Yoshihiro Shimoda | 2ae8695 | 2018-07-09 11:53:31 +0900 | [diff] [blame] | 925 | .reserved_context = false, |
Hai Nguyen Pham | 3623002 | 2019-09-04 14:08:02 +0200 | [diff] [blame] | 926 | .cache_snoop = true, |
Yoshihiro Shimoda | 3dc28d9 | 2019-11-06 11:35:48 +0900 | [diff] [blame] | 927 | .ctx_offset_base = 0, |
| 928 | .ctx_offset_stride = 0x40, |
Yoshihiro Shimoda | 1289f7f | 2019-11-06 11:35:50 +0900 | [diff] [blame] | 929 | .utlb_offset_base = 0, |
Magnus Damm | 33f3ac9 | 2017-10-16 21:29:25 +0900 | [diff] [blame] | 930 | }; |
| 931 | |
Magnus Damm | 0b8ac14 | 2018-06-14 12:48:22 +0200 | [diff] [blame] | 932 | static const struct ipmmu_features ipmmu_features_rcar_gen3 = { |
Magnus Damm | 58b8e8bf | 2017-10-16 21:30:50 +0900 | [diff] [blame] | 933 | .use_ns_alias_offset = false, |
| 934 | .has_cache_leaf_nodes = true, |
| 935 | .number_of_contexts = 8, |
Geert Uytterhoeven | b7f3f04 | 2019-05-27 13:52:51 +0200 | [diff] [blame] | 936 | .num_utlbs = 48, |
Magnus Damm | 58b8e8bf | 2017-10-16 21:30:50 +0900 | [diff] [blame] | 937 | .setup_imbuscr = false, |
| 938 | .twobit_imttbcr_sl0 = true, |
Yoshihiro Shimoda | 2ae8695 | 2018-07-09 11:53:31 +0900 | [diff] [blame] | 939 | .reserved_context = true, |
Hai Nguyen Pham | 3623002 | 2019-09-04 14:08:02 +0200 | [diff] [blame] | 940 | .cache_snoop = false, |
Yoshihiro Shimoda | 3dc28d9 | 2019-11-06 11:35:48 +0900 | [diff] [blame] | 941 | .ctx_offset_base = 0, |
| 942 | .ctx_offset_stride = 0x40, |
Yoshihiro Shimoda | 1289f7f | 2019-11-06 11:35:50 +0900 | [diff] [blame] | 943 | .utlb_offset_base = 0, |
Magnus Damm | 58b8e8bf | 2017-10-16 21:30:50 +0900 | [diff] [blame] | 944 | }; |
| 945 | |
Magnus Damm | 33f3ac9 | 2017-10-16 21:29:25 +0900 | [diff] [blame] | 946 | static const struct of_device_id ipmmu_of_ids[] = { |
| 947 | { |
| 948 | .compatible = "renesas,ipmmu-vmsa", |
| 949 | .data = &ipmmu_features_default, |
| 950 | }, { |
Fabrizio Castro | 60fb008 | 2018-08-23 16:33:04 +0100 | [diff] [blame] | 951 | .compatible = "renesas,ipmmu-r8a774a1", |
| 952 | .data = &ipmmu_features_rcar_gen3, |
| 953 | }, { |
Biju Das | 757f26a | 2019-09-27 11:53:21 +0100 | [diff] [blame] | 954 | .compatible = "renesas,ipmmu-r8a774b1", |
| 955 | .data = &ipmmu_features_rcar_gen3, |
| 956 | }, { |
Fabrizio Castro | b6d39cd8 | 2018-12-13 20:22:44 +0000 | [diff] [blame] | 957 | .compatible = "renesas,ipmmu-r8a774c0", |
| 958 | .data = &ipmmu_features_rcar_gen3, |
| 959 | }, { |
Marian-Cristian Rotariu | 4b2aa7a | 2020-07-14 11:20:54 +0100 | [diff] [blame] | 960 | .compatible = "renesas,ipmmu-r8a774e1", |
| 961 | .data = &ipmmu_features_rcar_gen3, |
| 962 | }, { |
Magnus Damm | 58b8e8bf | 2017-10-16 21:30:50 +0900 | [diff] [blame] | 963 | .compatible = "renesas,ipmmu-r8a7795", |
Magnus Damm | 0b8ac14 | 2018-06-14 12:48:22 +0200 | [diff] [blame] | 964 | .data = &ipmmu_features_rcar_gen3, |
| 965 | }, { |
| 966 | .compatible = "renesas,ipmmu-r8a7796", |
| 967 | .data = &ipmmu_features_rcar_gen3, |
Magnus Damm | 58b8e8bf | 2017-10-16 21:30:50 +0900 | [diff] [blame] | 968 | }, { |
Yoshihiro Shimoda | 17fe161 | 2020-06-11 20:10:30 +0900 | [diff] [blame] | 969 | .compatible = "renesas,ipmmu-r8a77961", |
| 970 | .data = &ipmmu_features_rcar_gen3, |
| 971 | }, { |
Jacopo Mondi | 98dbffd | 2018-06-14 12:48:25 +0200 | [diff] [blame] | 972 | .compatible = "renesas,ipmmu-r8a77965", |
| 973 | .data = &ipmmu_features_rcar_gen3, |
| 974 | }, { |
Simon Horman | 3701c12 | 2018-06-14 12:48:23 +0200 | [diff] [blame] | 975 | .compatible = "renesas,ipmmu-r8a77970", |
| 976 | .data = &ipmmu_features_rcar_gen3, |
| 977 | }, { |
Hai Nguyen Pham | b0c3291 | 2018-10-17 11:13:22 +0200 | [diff] [blame] | 978 | .compatible = "renesas,ipmmu-r8a77990", |
| 979 | .data = &ipmmu_features_rcar_gen3, |
| 980 | }, { |
Simon Horman | 3701c12 | 2018-06-14 12:48:23 +0200 | [diff] [blame] | 981 | .compatible = "renesas,ipmmu-r8a77995", |
| 982 | .data = &ipmmu_features_rcar_gen3, |
Magnus Damm | 33f3ac9 | 2017-10-16 21:29:25 +0900 | [diff] [blame] | 983 | }, { |
| 984 | /* Terminator */ |
| 985 | }, |
| 986 | }; |
| 987 | |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 988 | static int ipmmu_probe(struct platform_device *pdev) |
| 989 | { |
| 990 | struct ipmmu_vmsa_device *mmu; |
| 991 | struct resource *res; |
| 992 | int irq; |
| 993 | int ret; |
| 994 | |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 995 | mmu = devm_kzalloc(&pdev->dev, sizeof(*mmu), GFP_KERNEL); |
| 996 | if (!mmu) { |
| 997 | dev_err(&pdev->dev, "cannot allocate device data\n"); |
| 998 | return -ENOMEM; |
| 999 | } |
| 1000 | |
| 1001 | mmu->dev = &pdev->dev; |
Magnus Damm | dbb7069 | 2017-05-17 19:06:38 +0900 | [diff] [blame] | 1002 | spin_lock_init(&mmu->lock); |
| 1003 | bitmap_zero(mmu->ctx, IPMMU_CTX_MAX); |
Magnus Damm | 33f3ac9 | 2017-10-16 21:29:25 +0900 | [diff] [blame] | 1004 | mmu->features = of_device_get_match_data(&pdev->dev); |
Geert Uytterhoeven | da38e9e | 2019-05-27 13:52:53 +0200 | [diff] [blame] | 1005 | memset(mmu->utlb_ctx, IPMMU_CTX_INVALID, mmu->features->num_utlbs); |
Magnus Damm | 1c89422 | 2017-10-16 21:30:07 +0900 | [diff] [blame] | 1006 | dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(40)); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 1007 | |
| 1008 | /* Map I/O memory and request IRQ. */ |
| 1009 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1010 | mmu->base = devm_ioremap_resource(&pdev->dev, res); |
| 1011 | if (IS_ERR(mmu->base)) |
| 1012 | return PTR_ERR(mmu->base); |
| 1013 | |
Laurent Pinchart | 275f505 | 2014-03-17 01:02:46 +0100 | [diff] [blame] | 1014 | /* |
| 1015 | * The IPMMU has two register banks, for secure and non-secure modes. |
| 1016 | * The bank mapped at the beginning of the IPMMU address space |
| 1017 | * corresponds to the running mode of the CPU. When running in secure |
| 1018 | * mode the non-secure register bank is also available at an offset. |
| 1019 | * |
| 1020 | * Secure mode operation isn't clearly documented and is thus currently |
| 1021 | * not implemented in the driver. Furthermore, preliminary tests of |
| 1022 | * non-secure operation with the main register bank were not successful. |
| 1023 | * Offset the registers base unconditionally to point to the non-secure |
| 1024 | * alias space for now. |
| 1025 | */ |
Magnus Damm | 33f3ac9 | 2017-10-16 21:29:25 +0900 | [diff] [blame] | 1026 | if (mmu->features->use_ns_alias_offset) |
| 1027 | mmu->base += IM_NS_ALIAS_OFFSET; |
Laurent Pinchart | 275f505 | 2014-03-17 01:02:46 +0100 | [diff] [blame] | 1028 | |
Geert Uytterhoeven | b43e0d8 | 2019-05-27 13:52:50 +0200 | [diff] [blame] | 1029 | mmu->num_ctx = min(IPMMU_CTX_MAX, mmu->features->number_of_contexts); |
Magnus Damm | 5fd1634 | 2017-10-16 21:29:46 +0900 | [diff] [blame] | 1030 | |
Magnus Damm | fd5140e | 2017-10-16 21:29:36 +0900 | [diff] [blame] | 1031 | /* |
| 1032 | * Determine if this IPMMU instance is a root device by checking for |
| 1033 | * the lack of has_cache_leaf_nodes flag or renesas,ipmmu-main property. |
| 1034 | */ |
| 1035 | if (!mmu->features->has_cache_leaf_nodes || |
| 1036 | !of_find_property(pdev->dev.of_node, "renesas,ipmmu-main", NULL)) |
| 1037 | mmu->root = mmu; |
| 1038 | else |
| 1039 | mmu->root = ipmmu_find_root(); |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 1040 | |
Magnus Damm | fd5140e | 2017-10-16 21:29:36 +0900 | [diff] [blame] | 1041 | /* |
| 1042 | * Wait until the root device has been registered for sure. |
| 1043 | */ |
| 1044 | if (!mmu->root) |
| 1045 | return -EPROBE_DEFER; |
| 1046 | |
| 1047 | /* Root devices have mandatory IRQs */ |
| 1048 | if (ipmmu_is_root(mmu)) { |
Geert Uytterhoeven | ec37d4e | 2019-10-01 20:06:22 +0200 | [diff] [blame] | 1049 | irq = platform_get_irq(pdev, 0); |
YueHaibing | 565d454 | 2019-10-23 21:59:41 +0800 | [diff] [blame] | 1050 | if (irq < 0) |
Magnus Damm | fd5140e | 2017-10-16 21:29:36 +0900 | [diff] [blame] | 1051 | return irq; |
Magnus Damm | fd5140e | 2017-10-16 21:29:36 +0900 | [diff] [blame] | 1052 | |
| 1053 | ret = devm_request_irq(&pdev->dev, irq, ipmmu_irq, 0, |
| 1054 | dev_name(&pdev->dev), mmu); |
| 1055 | if (ret < 0) { |
| 1056 | dev_err(&pdev->dev, "failed to request IRQ %d\n", irq); |
| 1057 | return ret; |
| 1058 | } |
| 1059 | |
| 1060 | ipmmu_device_reset(mmu); |
Yoshihiro Shimoda | 2ae8695 | 2018-07-09 11:53:31 +0900 | [diff] [blame] | 1061 | |
| 1062 | if (mmu->features->reserved_context) { |
| 1063 | dev_info(&pdev->dev, "IPMMU context 0 is reserved\n"); |
| 1064 | set_bit(0, mmu->ctx); |
| 1065 | } |
Magnus Damm | fd5140e | 2017-10-16 21:29:36 +0900 | [diff] [blame] | 1066 | } |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 1067 | |
Magnus Damm | cda52fc | 2017-10-16 21:29:57 +0900 | [diff] [blame] | 1068 | /* |
| 1069 | * Register the IPMMU to the IOMMU subsystem in the following cases: |
| 1070 | * - R-Car Gen2 IPMMU (all devices registered) |
| 1071 | * - R-Car Gen3 IPMMU (leaf devices only - skip root IPMMU-MM device) |
| 1072 | */ |
| 1073 | if (!mmu->features->has_cache_leaf_nodes || !ipmmu_is_root(mmu)) { |
| 1074 | ret = iommu_device_sysfs_add(&mmu->iommu, &pdev->dev, NULL, |
| 1075 | dev_name(&pdev->dev)); |
| 1076 | if (ret) |
| 1077 | return ret; |
Magnus Damm | 7af9a5f | 2017-08-21 14:53:35 +0900 | [diff] [blame] | 1078 | |
Robin Murphy | 2d471b2 | 2021-04-01 14:56:26 +0100 | [diff] [blame] | 1079 | ret = iommu_device_register(&mmu->iommu, &ipmmu_ops, &pdev->dev); |
Magnus Damm | cda52fc | 2017-10-16 21:29:57 +0900 | [diff] [blame] | 1080 | if (ret) |
| 1081 | return ret; |
| 1082 | |
| 1083 | #if defined(CONFIG_IOMMU_DMA) |
| 1084 | if (!iommu_present(&platform_bus_type)) |
| 1085 | bus_set_iommu(&platform_bus_type, &ipmmu_ops); |
| 1086 | #endif |
| 1087 | } |
Magnus Damm | 01da21e | 2017-07-17 22:05:10 +0900 | [diff] [blame] | 1088 | |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 1089 | /* |
| 1090 | * We can't create the ARM mapping here as it requires the bus to have |
| 1091 | * an IOMMU, which only happens when bus_set_iommu() is called in |
| 1092 | * ipmmu_init() after the probe function returns. |
| 1093 | */ |
| 1094 | |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 1095 | platform_set_drvdata(pdev, mmu); |
| 1096 | |
| 1097 | return 0; |
| 1098 | } |
| 1099 | |
| 1100 | static int ipmmu_remove(struct platform_device *pdev) |
| 1101 | { |
| 1102 | struct ipmmu_vmsa_device *mmu = platform_get_drvdata(pdev); |
| 1103 | |
Magnus Damm | 7af9a5f | 2017-08-21 14:53:35 +0900 | [diff] [blame] | 1104 | iommu_device_sysfs_remove(&mmu->iommu); |
Magnus Damm | 01da21e | 2017-07-17 22:05:10 +0900 | [diff] [blame] | 1105 | iommu_device_unregister(&mmu->iommu); |
| 1106 | |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 1107 | arm_iommu_release_mapping(mmu->mapping); |
| 1108 | |
| 1109 | ipmmu_device_reset(mmu); |
| 1110 | |
| 1111 | return 0; |
| 1112 | } |
| 1113 | |
Geert Uytterhoeven | da38e9e | 2019-05-27 13:52:53 +0200 | [diff] [blame] | 1114 | #ifdef CONFIG_PM_SLEEP |
| 1115 | static int ipmmu_resume_noirq(struct device *dev) |
| 1116 | { |
| 1117 | struct ipmmu_vmsa_device *mmu = dev_get_drvdata(dev); |
| 1118 | unsigned int i; |
| 1119 | |
| 1120 | /* Reset root MMU and restore contexts */ |
| 1121 | if (ipmmu_is_root(mmu)) { |
| 1122 | ipmmu_device_reset(mmu); |
| 1123 | |
| 1124 | for (i = 0; i < mmu->num_ctx; i++) { |
| 1125 | if (!mmu->domains[i]) |
| 1126 | continue; |
| 1127 | |
| 1128 | ipmmu_domain_setup_context(mmu->domains[i]); |
| 1129 | } |
| 1130 | } |
| 1131 | |
| 1132 | /* Re-enable active micro-TLBs */ |
| 1133 | for (i = 0; i < mmu->features->num_utlbs; i++) { |
| 1134 | if (mmu->utlb_ctx[i] == IPMMU_CTX_INVALID) |
| 1135 | continue; |
| 1136 | |
| 1137 | ipmmu_utlb_enable(mmu->root->domains[mmu->utlb_ctx[i]], i); |
| 1138 | } |
| 1139 | |
| 1140 | return 0; |
| 1141 | } |
| 1142 | |
| 1143 | static const struct dev_pm_ops ipmmu_pm = { |
| 1144 | SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(NULL, ipmmu_resume_noirq) |
| 1145 | }; |
| 1146 | #define DEV_PM_OPS &ipmmu_pm |
| 1147 | #else |
| 1148 | #define DEV_PM_OPS NULL |
| 1149 | #endif /* CONFIG_PM_SLEEP */ |
| 1150 | |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 1151 | static struct platform_driver ipmmu_driver = { |
| 1152 | .driver = { |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 1153 | .name = "ipmmu-vmsa", |
Laurent Pinchart | 275f505 | 2014-03-17 01:02:46 +0100 | [diff] [blame] | 1154 | .of_match_table = of_match_ptr(ipmmu_of_ids), |
Geert Uytterhoeven | da38e9e | 2019-05-27 13:52:53 +0200 | [diff] [blame] | 1155 | .pm = DEV_PM_OPS, |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 1156 | }, |
| 1157 | .probe = ipmmu_probe, |
| 1158 | .remove = ipmmu_remove, |
| 1159 | }; |
| 1160 | |
| 1161 | static int __init ipmmu_init(void) |
| 1162 | { |
Dmitry Osipenko | 5c5c874 | 2018-07-27 00:19:16 +0300 | [diff] [blame] | 1163 | struct device_node *np; |
Magnus Damm | cda52fc | 2017-10-16 21:29:57 +0900 | [diff] [blame] | 1164 | static bool setup_done; |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 1165 | int ret; |
| 1166 | |
Magnus Damm | cda52fc | 2017-10-16 21:29:57 +0900 | [diff] [blame] | 1167 | if (setup_done) |
| 1168 | return 0; |
| 1169 | |
Dmitry Osipenko | 5c5c874 | 2018-07-27 00:19:16 +0300 | [diff] [blame] | 1170 | np = of_find_matching_node(NULL, ipmmu_of_ids); |
| 1171 | if (!np) |
| 1172 | return 0; |
| 1173 | |
| 1174 | of_node_put(np); |
| 1175 | |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 1176 | ret = platform_driver_register(&ipmmu_driver); |
| 1177 | if (ret < 0) |
| 1178 | return ret; |
| 1179 | |
Magnus Damm | cda52fc | 2017-10-16 21:29:57 +0900 | [diff] [blame] | 1180 | #if defined(CONFIG_ARM) && !defined(CONFIG_IOMMU_DMA) |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 1181 | if (!iommu_present(&platform_bus_type)) |
| 1182 | bus_set_iommu(&platform_bus_type, &ipmmu_ops); |
Magnus Damm | cda52fc | 2017-10-16 21:29:57 +0900 | [diff] [blame] | 1183 | #endif |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 1184 | |
Magnus Damm | cda52fc | 2017-10-16 21:29:57 +0900 | [diff] [blame] | 1185 | setup_done = true; |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 1186 | return 0; |
| 1187 | } |
Laurent Pinchart | d25a2a1 | 2014-04-02 12:47:37 +0200 | [diff] [blame] | 1188 | subsys_initcall(ipmmu_init); |