Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2006, Intel Corporation. |
| 3 | * |
| 4 | * This file is released under the GPLv2. |
| 5 | * |
mark gross | 98bcef5 | 2008-02-23 15:23:35 -0800 | [diff] [blame] | 6 | * Copyright (C) 2006-2008 Intel Corporation |
| 7 | * Author: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com> |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 8 | * |
| 9 | */ |
| 10 | |
| 11 | #ifndef _IOVA_H_ |
| 12 | #define _IOVA_H_ |
| 13 | |
| 14 | #include <linux/types.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/rbtree.h> |
| 17 | #include <linux/dma-mapping.h> |
| 18 | |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 19 | /* iova structure */ |
| 20 | struct iova { |
| 21 | struct rb_node node; |
Omer Peleg | 9257b4a | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 22 | unsigned long pfn_hi; /* Highest allocated pfn */ |
| 23 | unsigned long pfn_lo; /* Lowest allocated pfn */ |
| 24 | }; |
| 25 | |
| 26 | struct iova_magazine; |
| 27 | struct iova_cpu_rcache; |
| 28 | |
| 29 | #define IOVA_RANGE_CACHE_MAX_SIZE 6 /* log of max cached IOVA range size (in pages) */ |
| 30 | #define MAX_GLOBAL_MAGS 32 /* magazines per bin */ |
| 31 | |
| 32 | struct iova_rcache { |
| 33 | spinlock_t lock; |
| 34 | unsigned long depot_size; |
| 35 | struct iova_magazine *depot[MAX_GLOBAL_MAGS]; |
| 36 | struct iova_cpu_rcache __percpu *cpu_rcaches; |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
Joerg Roedel | 42f87e7 | 2017-08-10 14:44:28 +0200 | [diff] [blame] | 39 | struct iova_domain; |
| 40 | |
| 41 | /* Call-Back from IOVA code into IOMMU drivers */ |
| 42 | typedef void (* iova_flush_cb)(struct iova_domain *domain); |
| 43 | |
| 44 | /* Destructor for per-entry data */ |
| 45 | typedef void (* iova_entry_dtor)(unsigned long data); |
| 46 | |
| 47 | /* Number of entries per Flush Queue */ |
| 48 | #define IOVA_FQ_SIZE 256 |
| 49 | |
| 50 | /* Flush Queue entry for defered flushing */ |
| 51 | struct iova_fq_entry { |
| 52 | unsigned long iova_pfn; |
| 53 | unsigned long pages; |
| 54 | unsigned long data; |
| 55 | }; |
| 56 | |
| 57 | /* Per-CPU Flush Queue structure */ |
| 58 | struct iova_fq { |
| 59 | struct iova_fq_entry entries[IOVA_FQ_SIZE]; |
| 60 | unsigned head, tail; |
| 61 | }; |
| 62 | |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 63 | /* holds all the iova translations for a domain */ |
| 64 | struct iova_domain { |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 65 | spinlock_t iova_rbtree_lock; /* Lock to protect update of rbtree */ |
| 66 | struct rb_root rbroot; /* iova domain rbtree root */ |
| 67 | struct rb_node *cached32_node; /* Save last alloced node */ |
Robin Murphy | 0fb5fe8 | 2015-01-12 17:51:16 +0000 | [diff] [blame] | 68 | unsigned long granule; /* pfn granularity for this domain */ |
Robin Murphy | 1b72250 | 2015-01-12 17:51:15 +0000 | [diff] [blame] | 69 | unsigned long start_pfn; /* Lower limit for this domain */ |
David Miller | f661197 | 2008-02-06 01:36:23 -0800 | [diff] [blame] | 70 | unsigned long dma_32bit_pfn; |
Omer Peleg | 9257b4a | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 71 | struct iova_rcache rcaches[IOVA_RANGE_CACHE_MAX_SIZE]; /* IOVA range caches */ |
Joerg Roedel | 42f87e7 | 2017-08-10 14:44:28 +0200 | [diff] [blame] | 72 | |
| 73 | iova_flush_cb flush_cb; /* Call-Back function to flush IOMMU |
| 74 | TLBs */ |
| 75 | |
| 76 | iova_entry_dtor entry_dtor; /* IOMMU driver specific destructor for |
| 77 | iova entry */ |
| 78 | |
| 79 | struct iova_fq __percpu *fq; /* Flush Queue */ |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
Jiang Liu | a156ef9 | 2014-07-11 14:19:36 +0800 | [diff] [blame] | 82 | static inline unsigned long iova_size(struct iova *iova) |
| 83 | { |
| 84 | return iova->pfn_hi - iova->pfn_lo + 1; |
| 85 | } |
| 86 | |
Robin Murphy | 0fb5fe8 | 2015-01-12 17:51:16 +0000 | [diff] [blame] | 87 | static inline unsigned long iova_shift(struct iova_domain *iovad) |
| 88 | { |
| 89 | return __ffs(iovad->granule); |
| 90 | } |
| 91 | |
| 92 | static inline unsigned long iova_mask(struct iova_domain *iovad) |
| 93 | { |
| 94 | return iovad->granule - 1; |
| 95 | } |
| 96 | |
| 97 | static inline size_t iova_offset(struct iova_domain *iovad, dma_addr_t iova) |
| 98 | { |
| 99 | return iova & iova_mask(iovad); |
| 100 | } |
| 101 | |
| 102 | static inline size_t iova_align(struct iova_domain *iovad, size_t size) |
| 103 | { |
| 104 | return ALIGN(size, iovad->granule); |
| 105 | } |
| 106 | |
| 107 | static inline dma_addr_t iova_dma_addr(struct iova_domain *iovad, struct iova *iova) |
| 108 | { |
| 109 | return (dma_addr_t)iova->pfn_lo << iova_shift(iovad); |
| 110 | } |
| 111 | |
| 112 | static inline unsigned long iova_pfn(struct iova_domain *iovad, dma_addr_t iova) |
| 113 | { |
| 114 | return iova >> iova_shift(iovad); |
| 115 | } |
| 116 | |
Joerg Roedel | b4d8c7a | 2017-03-23 00:06:17 +0100 | [diff] [blame] | 117 | #if IS_ENABLED(CONFIG_IOMMU_IOVA) |
Sakari Ailus | ae1ff3d | 2015-07-13 14:31:28 +0300 | [diff] [blame] | 118 | int iova_cache_get(void); |
| 119 | void iova_cache_put(void); |
Robin Murphy | 85b4545 | 2015-01-12 17:51:14 +0000 | [diff] [blame] | 120 | |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 121 | struct iova *alloc_iova_mem(void); |
| 122 | void free_iova_mem(struct iova *iova); |
| 123 | void free_iova(struct iova_domain *iovad, unsigned long pfn); |
| 124 | void __free_iova(struct iova_domain *iovad, struct iova *iova); |
| 125 | struct iova *alloc_iova(struct iova_domain *iovad, unsigned long size, |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 126 | unsigned long limit_pfn, |
| 127 | bool size_aligned); |
Omer Peleg | 9257b4a | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 128 | void free_iova_fast(struct iova_domain *iovad, unsigned long pfn, |
| 129 | unsigned long size); |
Joerg Roedel | 1928210 | 2017-08-10 15:49:44 +0200 | [diff] [blame^] | 130 | void queue_iova(struct iova_domain *iovad, |
| 131 | unsigned long pfn, unsigned long pages, |
| 132 | unsigned long data); |
Omer Peleg | 9257b4a | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 133 | unsigned long alloc_iova_fast(struct iova_domain *iovad, unsigned long size, |
| 134 | unsigned long limit_pfn); |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 135 | struct iova *reserve_iova(struct iova_domain *iovad, unsigned long pfn_lo, |
| 136 | unsigned long pfn_hi); |
| 137 | void copy_reserved_iova(struct iova_domain *from, struct iova_domain *to); |
Robin Murphy | 0fb5fe8 | 2015-01-12 17:51:16 +0000 | [diff] [blame] | 138 | void init_iova_domain(struct iova_domain *iovad, unsigned long granule, |
| 139 | unsigned long start_pfn, unsigned long pfn_32bit); |
Joerg Roedel | 42f87e7 | 2017-08-10 14:44:28 +0200 | [diff] [blame] | 140 | int init_iova_flush_queue(struct iova_domain *iovad, |
| 141 | iova_flush_cb flush_cb, iova_entry_dtor entry_dtor); |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 142 | struct iova *find_iova(struct iova_domain *iovad, unsigned long pfn); |
| 143 | void put_iova_domain(struct iova_domain *iovad); |
Jiang Liu | 75f0556 | 2014-02-19 14:07:37 +0800 | [diff] [blame] | 144 | struct iova *split_and_remove_iova(struct iova_domain *iovad, |
| 145 | struct iova *iova, unsigned long pfn_lo, unsigned long pfn_hi); |
Omer Peleg | 9257b4a | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 146 | void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad); |
Thierry Reding | 21aff52 | 2017-03-20 20:11:28 +0100 | [diff] [blame] | 147 | #else |
| 148 | static inline int iova_cache_get(void) |
| 149 | { |
| 150 | return -ENOTSUPP; |
| 151 | } |
| 152 | |
| 153 | static inline void iova_cache_put(void) |
| 154 | { |
| 155 | } |
| 156 | |
| 157 | static inline struct iova *alloc_iova_mem(void) |
| 158 | { |
| 159 | return NULL; |
| 160 | } |
| 161 | |
| 162 | static inline void free_iova_mem(struct iova *iova) |
| 163 | { |
| 164 | } |
| 165 | |
| 166 | static inline void free_iova(struct iova_domain *iovad, unsigned long pfn) |
| 167 | { |
| 168 | } |
| 169 | |
| 170 | static inline void __free_iova(struct iova_domain *iovad, struct iova *iova) |
| 171 | { |
| 172 | } |
| 173 | |
| 174 | static inline struct iova *alloc_iova(struct iova_domain *iovad, |
| 175 | unsigned long size, |
| 176 | unsigned long limit_pfn, |
| 177 | bool size_aligned) |
| 178 | { |
| 179 | return NULL; |
| 180 | } |
| 181 | |
| 182 | static inline void free_iova_fast(struct iova_domain *iovad, |
| 183 | unsigned long pfn, |
| 184 | unsigned long size) |
| 185 | { |
| 186 | } |
| 187 | |
Joerg Roedel | 1928210 | 2017-08-10 15:49:44 +0200 | [diff] [blame^] | 188 | static inline void queue_iova(struct iova_domain *iovad, |
| 189 | unsigned long pfn, unsigned long pages, |
| 190 | unsigned long data) |
| 191 | { |
| 192 | } |
| 193 | |
Thierry Reding | 21aff52 | 2017-03-20 20:11:28 +0100 | [diff] [blame] | 194 | static inline unsigned long alloc_iova_fast(struct iova_domain *iovad, |
| 195 | unsigned long size, |
| 196 | unsigned long limit_pfn) |
| 197 | { |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | static inline struct iova *reserve_iova(struct iova_domain *iovad, |
| 202 | unsigned long pfn_lo, |
| 203 | unsigned long pfn_hi) |
| 204 | { |
| 205 | return NULL; |
| 206 | } |
| 207 | |
| 208 | static inline void copy_reserved_iova(struct iova_domain *from, |
| 209 | struct iova_domain *to) |
| 210 | { |
| 211 | } |
| 212 | |
| 213 | static inline void init_iova_domain(struct iova_domain *iovad, |
| 214 | unsigned long granule, |
| 215 | unsigned long start_pfn, |
| 216 | unsigned long pfn_32bit) |
| 217 | { |
| 218 | } |
| 219 | |
Joerg Roedel | 42f87e7 | 2017-08-10 14:44:28 +0200 | [diff] [blame] | 220 | static inline int init_iova_flush_queue(struct iova_domain *iovad, |
| 221 | iova_flush_cb flush_cb, |
| 222 | iova_entry_dtor entry_dtor) |
| 223 | { |
| 224 | return -ENODEV; |
| 225 | } |
| 226 | |
Thierry Reding | 21aff52 | 2017-03-20 20:11:28 +0100 | [diff] [blame] | 227 | static inline struct iova *find_iova(struct iova_domain *iovad, |
| 228 | unsigned long pfn) |
| 229 | { |
| 230 | return NULL; |
| 231 | } |
| 232 | |
| 233 | static inline void put_iova_domain(struct iova_domain *iovad) |
| 234 | { |
| 235 | } |
| 236 | |
| 237 | static inline struct iova *split_and_remove_iova(struct iova_domain *iovad, |
| 238 | struct iova *iova, |
| 239 | unsigned long pfn_lo, |
| 240 | unsigned long pfn_hi) |
| 241 | { |
| 242 | return NULL; |
| 243 | } |
| 244 | |
| 245 | static inline void free_cpu_cached_iovas(unsigned int cpu, |
| 246 | struct iova_domain *iovad) |
| 247 | { |
| 248 | } |
| 249 | #endif |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 250 | |
| 251 | #endif |