0001
0002 #ifndef __IO_PGTABLE_H
0003 #define __IO_PGTABLE_H
0004
0005 #include <linux/bitops.h>
0006 #include <linux/iommu.h>
0007
0008
0009
0010
0011 enum io_pgtable_fmt {
0012 ARM_32_LPAE_S1,
0013 ARM_32_LPAE_S2,
0014 ARM_64_LPAE_S1,
0015 ARM_64_LPAE_S2,
0016 ARM_V7S,
0017 ARM_MALI_LPAE,
0018 AMD_IOMMU_V1,
0019 APPLE_DART,
0020 IO_PGTABLE_NUM_FMTS,
0021 };
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039 struct iommu_flush_ops {
0040 void (*tlb_flush_all)(void *cookie);
0041 void (*tlb_flush_walk)(unsigned long iova, size_t size, size_t granule,
0042 void *cookie);
0043 void (*tlb_add_page)(struct iommu_iotlb_gather *gather,
0044 unsigned long iova, size_t granule, void *cookie);
0045 };
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062 struct io_pgtable_cfg {
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087 #define IO_PGTABLE_QUIRK_ARM_NS BIT(0)
0088 #define IO_PGTABLE_QUIRK_NO_PERMS BIT(1)
0089 #define IO_PGTABLE_QUIRK_ARM_MTK_EXT BIT(3)
0090 #define IO_PGTABLE_QUIRK_ARM_MTK_TTBR_EXT BIT(4)
0091 #define IO_PGTABLE_QUIRK_ARM_TTBR1 BIT(5)
0092 #define IO_PGTABLE_QUIRK_ARM_OUTER_WBWA BIT(6)
0093 unsigned long quirks;
0094 unsigned long pgsize_bitmap;
0095 unsigned int ias;
0096 unsigned int oas;
0097 bool coherent_walk;
0098 const struct iommu_flush_ops *tlb;
0099 struct device *iommu_dev;
0100
0101
0102 union {
0103 struct {
0104 u64 ttbr;
0105 struct {
0106 u32 ips:3;
0107 u32 tg:2;
0108 u32 sh:2;
0109 u32 orgn:2;
0110 u32 irgn:2;
0111 u32 tsz:6;
0112 } tcr;
0113 u64 mair;
0114 } arm_lpae_s1_cfg;
0115
0116 struct {
0117 u64 vttbr;
0118 struct {
0119 u32 ps:3;
0120 u32 tg:2;
0121 u32 sh:2;
0122 u32 orgn:2;
0123 u32 irgn:2;
0124 u32 sl:2;
0125 u32 tsz:6;
0126 } vtcr;
0127 } arm_lpae_s2_cfg;
0128
0129 struct {
0130 u32 ttbr;
0131 u32 tcr;
0132 u32 nmrr;
0133 u32 prrr;
0134 } arm_v7s_cfg;
0135
0136 struct {
0137 u64 transtab;
0138 u64 memattr;
0139 } arm_mali_lpae_cfg;
0140
0141 struct {
0142 u64 ttbr[4];
0143 u32 n_ttbrs;
0144 } apple_dart_cfg;
0145 };
0146 };
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160 struct io_pgtable_ops {
0161 int (*map)(struct io_pgtable_ops *ops, unsigned long iova,
0162 phys_addr_t paddr, size_t size, int prot, gfp_t gfp);
0163 int (*map_pages)(struct io_pgtable_ops *ops, unsigned long iova,
0164 phys_addr_t paddr, size_t pgsize, size_t pgcount,
0165 int prot, gfp_t gfp, size_t *mapped);
0166 size_t (*unmap)(struct io_pgtable_ops *ops, unsigned long iova,
0167 size_t size, struct iommu_iotlb_gather *gather);
0168 size_t (*unmap_pages)(struct io_pgtable_ops *ops, unsigned long iova,
0169 size_t pgsize, size_t pgcount,
0170 struct iommu_iotlb_gather *gather);
0171 phys_addr_t (*iova_to_phys)(struct io_pgtable_ops *ops,
0172 unsigned long iova);
0173 };
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185 struct io_pgtable_ops *alloc_io_pgtable_ops(enum io_pgtable_fmt fmt,
0186 struct io_pgtable_cfg *cfg,
0187 void *cookie);
0188
0189
0190
0191
0192
0193
0194
0195
0196 void free_io_pgtable_ops(struct io_pgtable_ops *ops);
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212 struct io_pgtable {
0213 enum io_pgtable_fmt fmt;
0214 void *cookie;
0215 struct io_pgtable_cfg cfg;
0216 struct io_pgtable_ops ops;
0217 };
0218
0219 #define io_pgtable_ops_to_pgtable(x) container_of((x), struct io_pgtable, ops)
0220
0221 static inline void io_pgtable_tlb_flush_all(struct io_pgtable *iop)
0222 {
0223 if (iop->cfg.tlb && iop->cfg.tlb->tlb_flush_all)
0224 iop->cfg.tlb->tlb_flush_all(iop->cookie);
0225 }
0226
0227 static inline void
0228 io_pgtable_tlb_flush_walk(struct io_pgtable *iop, unsigned long iova,
0229 size_t size, size_t granule)
0230 {
0231 if (iop->cfg.tlb && iop->cfg.tlb->tlb_flush_walk)
0232 iop->cfg.tlb->tlb_flush_walk(iova, size, granule, iop->cookie);
0233 }
0234
0235 static inline void
0236 io_pgtable_tlb_add_page(struct io_pgtable *iop,
0237 struct iommu_iotlb_gather * gather, unsigned long iova,
0238 size_t granule)
0239 {
0240 if (iop->cfg.tlb && iop->cfg.tlb->tlb_add_page)
0241 iop->cfg.tlb->tlb_add_page(gather, iova, granule, iop->cookie);
0242 }
0243
0244
0245
0246
0247
0248
0249
0250
0251 struct io_pgtable_init_fns {
0252 struct io_pgtable *(*alloc)(struct io_pgtable_cfg *cfg, void *cookie);
0253 void (*free)(struct io_pgtable *iop);
0254 };
0255
0256 extern struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns;
0257 extern struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns;
0258 extern struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns;
0259 extern struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns;
0260 extern struct io_pgtable_init_fns io_pgtable_arm_v7s_init_fns;
0261 extern struct io_pgtable_init_fns io_pgtable_arm_mali_lpae_init_fns;
0262 extern struct io_pgtable_init_fns io_pgtable_amd_iommu_v1_init_fns;
0263 extern struct io_pgtable_init_fns io_pgtable_apple_dart_init_fns;
0264
0265 #endif