blob: 8b9b9f0116f46c4246b9d614f86892150e811f94 [file] [log] [blame]
Will Deaconfdb1d7b2014-11-14 17:16:49 +00001#ifndef __IO_PGTABLE_H
2#define __IO_PGTABLE_H
Robin Murphye5fc9752016-01-26 17:13:13 +00003#include <linux/bitops.h>
Will Deaconfdb1d7b2014-11-14 17:16:49 +00004
Mitchel Humpheryscd6ff132015-04-23 16:16:56 -07005#include <linux/scatterlist.h>
6
Will Deaconfdb1d7b2014-11-14 17:16:49 +00007/*
8 * Public API for use by IOMMU drivers
9 */
10enum io_pgtable_fmt {
Will Deacone1d3c0f2014-11-14 17:18:23 +000011 ARM_32_LPAE_S1,
12 ARM_32_LPAE_S2,
13 ARM_64_LPAE_S1,
14 ARM_64_LPAE_S2,
Robin Murphye5fc9752016-01-26 17:13:13 +000015 ARM_V7S,
Will Deaconfdb1d7b2014-11-14 17:16:49 +000016 IO_PGTABLE_NUM_FMTS,
17};
18
19/**
20 * struct iommu_gather_ops - IOMMU callbacks for TLB and page table management.
21 *
22 * @tlb_flush_all: Synchronously invalidate the entire TLB context.
23 * @tlb_add_flush: Queue up a TLB invalidation for a virtual address range.
Robin Murphy87a91b12015-07-29 19:46:09 +010024 * @tlb_sync: Ensure any queued TLB invalidation has taken effect, and
25 * any corresponding page table updates are visible to the
26 * IOMMU.
Will Deaconfdb1d7b2014-11-14 17:16:49 +000027 *
28 * Note that these can all be called in atomic context and must therefore
29 * not block.
30 */
31struct iommu_gather_ops {
32 void (*tlb_flush_all)(void *cookie);
Robin Murphy06c610e2015-12-07 18:18:53 +000033 void (*tlb_add_flush)(unsigned long iova, size_t size, size_t granule,
34 bool leaf, void *cookie);
Will Deaconfdb1d7b2014-11-14 17:16:49 +000035 void (*tlb_sync)(void *cookie);
Will Deaconfdb1d7b2014-11-14 17:16:49 +000036};
37
38/**
39 * struct io_pgtable_cfg - Configuration data for a set of page tables.
40 *
41 * @quirks: A bitmap of hardware quirks that require some special
42 * action by the low-level page table allocator.
43 * @pgsize_bitmap: A bitmap of page sizes supported by this set of page
44 * tables.
45 * @ias: Input address (iova) size, in bits.
46 * @oas: Output address (paddr) size, in bits.
47 * @tlb: TLB management callbacks for this set of tables.
Robin Murphyf8d54962015-07-29 19:46:04 +010048 * @iommu_dev: The device representing the DMA configuration for the
49 * page table walker.
Will Deaconfdb1d7b2014-11-14 17:16:49 +000050 */
51struct io_pgtable_cfg {
Robin Murphy3850db42016-02-12 17:09:46 +000052 /*
53 * IO_PGTABLE_QUIRK_ARM_NS: (ARM formats) Set NS and NSTABLE bits in
54 * stage 1 PTEs, for hardware which insists on validating them
55 * even in non-secure state where they should normally be ignored.
56 *
57 * IO_PGTABLE_QUIRK_NO_PERMS: Ignore the IOMMU_READ, IOMMU_WRITE and
58 * IOMMU_NOEXEC flags and map everything with full access, for
59 * hardware which does not implement the permissions of a given
60 * format, and/or requires some format-specific default value.
61 *
62 * IO_PGTABLE_QUIRK_TLBI_ON_MAP: If the format forbids caching invalid
63 * (unmapped) entries but the hardware might do so anyway, perform
64 * TLB maintenance when mapping as well as when unmapping.
Yong Wu1afe2312016-03-14 06:01:10 +080065 *
66 * IO_PGTABLE_QUIRK_ARM_MTK_4GB: (ARM v7s format) Set bit 9 in all
67 * PTEs, for Mediatek IOMMUs which treat it as a 33rd address bit
68 * when the SoC is in "4GB mode" and they can only access the high
69 * remap of DRAM (0x1_00000000 to 0x1_ffffffff).
Robin Murphy3850db42016-02-12 17:09:46 +000070 */
71 #define IO_PGTABLE_QUIRK_ARM_NS BIT(0)
72 #define IO_PGTABLE_QUIRK_NO_PERMS BIT(1)
73 #define IO_PGTABLE_QUIRK_TLBI_ON_MAP BIT(2)
Yong Wu1afe2312016-03-14 06:01:10 +080074 #define IO_PGTABLE_QUIRK_ARM_MTK_4GB BIT(3)
Robin Murphy3850db42016-02-12 17:09:46 +000075 unsigned long quirks;
Will Deaconfdb1d7b2014-11-14 17:16:49 +000076 unsigned long pgsize_bitmap;
77 unsigned int ias;
78 unsigned int oas;
79 const struct iommu_gather_ops *tlb;
Robin Murphyf8d54962015-07-29 19:46:04 +010080 struct device *iommu_dev;
Will Deaconfdb1d7b2014-11-14 17:16:49 +000081
82 /* Low-level data specific to the table format */
83 union {
Will Deacone1d3c0f2014-11-14 17:18:23 +000084 struct {
85 u64 ttbr[2];
86 u64 tcr;
87 u64 mair[2];
88 } arm_lpae_s1_cfg;
89
90 struct {
91 u64 vttbr;
92 u64 vtcr;
93 } arm_lpae_s2_cfg;
Robin Murphye5fc9752016-01-26 17:13:13 +000094
95 struct {
96 u32 ttbr[2];
97 u32 tcr;
98 u32 nmrr;
99 u32 prrr;
100 } arm_v7s_cfg;
Will Deaconfdb1d7b2014-11-14 17:16:49 +0000101 };
102};
103
104/**
105 * struct io_pgtable_ops - Page table manipulation API for IOMMU drivers.
106 *
107 * @map: Map a physically contiguous memory region.
108 * @unmap: Unmap a physically contiguous memory region.
109 * @iova_to_phys: Translate iova to physical address.
110 *
111 * These functions map directly onto the iommu_ops member functions with
112 * the same names.
113 */
114struct io_pgtable_ops {
115 int (*map)(struct io_pgtable_ops *ops, unsigned long iova,
116 phys_addr_t paddr, size_t size, int prot);
Mitchel Humpheryscd6ff132015-04-23 16:16:56 -0700117 int (*map_sg)(struct io_pgtable_ops *ops, unsigned long iova,
118 struct scatterlist *sg, unsigned int nents, int prot);
Will Deaconfdb1d7b2014-11-14 17:16:49 +0000119 int (*unmap)(struct io_pgtable_ops *ops, unsigned long iova,
120 size_t size);
121 phys_addr_t (*iova_to_phys)(struct io_pgtable_ops *ops,
122 unsigned long iova);
123};
124
125/**
126 * alloc_io_pgtable_ops() - Allocate a page table allocator for use by an IOMMU.
127 *
128 * @fmt: The page table format.
129 * @cfg: The page table configuration. This will be modified to represent
130 * the configuration actually provided by the allocator (e.g. the
131 * pgsize_bitmap may be restricted).
132 * @cookie: An opaque token provided by the IOMMU driver and passed back to
133 * the callback routines in cfg->tlb.
134 */
135struct io_pgtable_ops *alloc_io_pgtable_ops(enum io_pgtable_fmt fmt,
136 struct io_pgtable_cfg *cfg,
137 void *cookie);
138
139/**
140 * free_io_pgtable_ops() - Free an io_pgtable_ops structure. The caller
141 * *must* ensure that the page table is no longer
142 * live, but the TLB can be dirty.
143 *
144 * @ops: The ops returned from alloc_io_pgtable_ops.
145 */
146void free_io_pgtable_ops(struct io_pgtable_ops *ops);
147
148
149/*
150 * Internal structures for page table allocator implementations.
151 */
152
153/**
154 * struct io_pgtable - Internal structure describing a set of page tables.
155 *
156 * @fmt: The page table format.
157 * @cookie: An opaque token provided by the IOMMU driver and passed back to
158 * any callback routines.
Robin Murphy88492a42016-01-26 17:13:15 +0000159 * @tlb_sync_pending: Private flag for optimising out redundant syncs.
Will Deaconfdb1d7b2014-11-14 17:16:49 +0000160 * @cfg: A copy of the page table configuration.
161 * @ops: The page table operations in use for this set of page tables.
162 */
163struct io_pgtable {
164 enum io_pgtable_fmt fmt;
165 void *cookie;
Robin Murphy88492a42016-01-26 17:13:15 +0000166 bool tlb_sync_pending;
Will Deaconfdb1d7b2014-11-14 17:16:49 +0000167 struct io_pgtable_cfg cfg;
168 struct io_pgtable_ops ops;
169};
170
Robin Murphyfdc38962015-12-04 17:53:01 +0000171#define io_pgtable_ops_to_pgtable(x) container_of((x), struct io_pgtable, ops)
172
Robin Murphy507e4c92016-01-26 17:13:14 +0000173static inline void io_pgtable_tlb_flush_all(struct io_pgtable *iop)
174{
175 iop->cfg.tlb->tlb_flush_all(iop->cookie);
Robin Murphy88492a42016-01-26 17:13:15 +0000176 iop->tlb_sync_pending = true;
Robin Murphy507e4c92016-01-26 17:13:14 +0000177}
178
179static inline void io_pgtable_tlb_add_flush(struct io_pgtable *iop,
180 unsigned long iova, size_t size, size_t granule, bool leaf)
181{
182 iop->cfg.tlb->tlb_add_flush(iova, size, granule, leaf, iop->cookie);
Robin Murphy88492a42016-01-26 17:13:15 +0000183 iop->tlb_sync_pending = true;
Robin Murphy507e4c92016-01-26 17:13:14 +0000184}
185
186static inline void io_pgtable_tlb_sync(struct io_pgtable *iop)
187{
Robin Murphy88492a42016-01-26 17:13:15 +0000188 if (iop->tlb_sync_pending) {
189 iop->cfg.tlb->tlb_sync(iop->cookie);
190 iop->tlb_sync_pending = false;
191 }
Robin Murphy507e4c92016-01-26 17:13:14 +0000192}
193
Will Deaconfdb1d7b2014-11-14 17:16:49 +0000194/**
195 * struct io_pgtable_init_fns - Alloc/free a set of page tables for a
196 * particular format.
197 *
198 * @alloc: Allocate a set of page tables described by cfg.
199 * @free: Free the page tables associated with iop.
200 */
201struct io_pgtable_init_fns {
202 struct io_pgtable *(*alloc)(struct io_pgtable_cfg *cfg, void *cookie);
203 void (*free)(struct io_pgtable *iop);
204};
205
Joerg Roedel2e169bb2015-08-13 12:01:10 +0200206extern struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns;
207extern struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns;
208extern struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns;
209extern struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns;
Robin Murphye5fc9752016-01-26 17:13:13 +0000210extern struct io_pgtable_init_fns io_pgtable_arm_v7s_init_fns;
Joerg Roedel2e169bb2015-08-13 12:01:10 +0200211
Will Deaconfdb1d7b2014-11-14 17:16:49 +0000212#endif /* __IO_PGTABLE_H */