Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2016 Intel Corporation |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 | * IN THE SOFTWARE. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #ifndef __I915_VMA_H__ |
| 26 | #define __I915_VMA_H__ |
| 27 | |
| 28 | #include <linux/io-mapping.h> |
Chris Wilson | 5c3f8c2 | 2018-07-06 11:39:46 +0100 | [diff] [blame] | 29 | #include <linux/rbtree.h> |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 30 | |
| 31 | #include <drm/drm_mm.h> |
| 32 | |
| 33 | #include "i915_gem_gtt.h" |
| 34 | #include "i915_gem_fence_reg.h" |
| 35 | #include "i915_gem_object.h" |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 36 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 37 | #include "i915_request.h" |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 38 | |
| 39 | enum i915_cache_level; |
| 40 | |
| 41 | /** |
| 42 | * A VMA represents a GEM BO that is bound into an address space. Therefore, a |
| 43 | * VMA's presence cannot be guaranteed before binding, or after unbinding the |
| 44 | * object into/from the address space. |
| 45 | * |
| 46 | * To make things as simple as possible (ie. no refcounting), a VMA's lifetime |
| 47 | * will always be <= an objects lifetime. So object refcounting should cover us. |
| 48 | */ |
| 49 | struct i915_vma { |
| 50 | struct drm_mm_node node; |
| 51 | struct drm_i915_gem_object *obj; |
| 52 | struct i915_address_space *vm; |
Chris Wilson | 93f2cde | 2018-06-07 16:40:46 +0100 | [diff] [blame] | 53 | const struct i915_vma_ops *ops; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 54 | struct drm_i915_fence_reg *fence; |
Chris Wilson | 95ff7c7 | 2017-06-16 15:05:25 +0100 | [diff] [blame] | 55 | struct reservation_object *resv; /** Alias of obj->resv */ |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 56 | struct sg_table *pages; |
| 57 | void __iomem *iomap; |
Chris Wilson | e9e7dc4 | 2018-06-12 13:04:46 +0100 | [diff] [blame] | 58 | void *private; /* owned by creator */ |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 59 | u64 size; |
| 60 | u64 display_alignment; |
Matthew Auld | 7464284 | 2017-10-06 23:18:20 +0100 | [diff] [blame] | 61 | struct i915_page_sizes page_sizes; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 62 | |
Chris Wilson | 944397f | 2017-01-09 16:16:11 +0000 | [diff] [blame] | 63 | u32 fence_size; |
| 64 | u32 fence_alignment; |
| 65 | |
Chris Wilson | 3ffff01 | 2017-08-22 12:05:17 +0100 | [diff] [blame] | 66 | /** |
| 67 | * Count of the number of times this vma has been opened by different |
| 68 | * handles (but same file) for execbuf, i.e. the number of aliases |
| 69 | * that exist in the ctx->handle_vmas LUT for this vma. |
| 70 | */ |
| 71 | unsigned int open_count; |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 72 | unsigned long flags; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 73 | /** |
| 74 | * How many users have pinned this object in GTT space. The following |
| 75 | * users can each hold at most one reference: pwrite/pread, execbuffer |
| 76 | * (objects are not allowed multiple times for the same batchbuffer), |
| 77 | * and the framebuffer code. When switching/pageflipping, the |
| 78 | * framebuffer code has at most two buffers pinned per crtc. |
| 79 | * |
| 80 | * In the worst case this is 1 + 1 + 1 + 2*2 = 7. That would fit into 3 |
| 81 | * bits with absolutely no headroom. So use 4 bits. |
| 82 | */ |
| 83 | #define I915_VMA_PIN_MASK 0xf |
| 84 | #define I915_VMA_PIN_OVERFLOW BIT(5) |
| 85 | |
| 86 | /** Flags and address space this VMA is bound to */ |
| 87 | #define I915_VMA_GLOBAL_BIND BIT(6) |
| 88 | #define I915_VMA_LOCAL_BIND BIT(7) |
| 89 | #define I915_VMA_BIND_MASK (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND | I915_VMA_PIN_OVERFLOW) |
| 90 | |
| 91 | #define I915_VMA_GGTT BIT(8) |
| 92 | #define I915_VMA_CAN_FENCE BIT(9) |
| 93 | #define I915_VMA_CLOSED BIT(10) |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 94 | #define I915_VMA_USERFAULT_BIT 11 |
| 95 | #define I915_VMA_USERFAULT BIT(I915_VMA_USERFAULT_BIT) |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 96 | #define I915_VMA_GGTT_WRITE BIT(12) |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 97 | |
Chris Wilson | 5c3f8c2 | 2018-07-06 11:39:46 +0100 | [diff] [blame] | 98 | unsigned int active_count; |
| 99 | struct rb_root active; |
Chris Wilson | 8b293eb | 2018-07-06 13:31:57 +0100 | [diff] [blame] | 100 | struct i915_gem_active last_active; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 101 | struct i915_gem_active last_fence; |
| 102 | |
| 103 | /** |
| 104 | * Support different GGTT views into the same object. |
| 105 | * This means there can be multiple VMA mappings per object and per VM. |
| 106 | * i915_ggtt_view_type is used to distinguish between those entries. |
| 107 | * The default one of zero (I915_GGTT_VIEW_NORMAL) is default and also |
| 108 | * assumed in GEM functions which take no ggtt view parameter. |
| 109 | */ |
| 110 | struct i915_ggtt_view ggtt_view; |
| 111 | |
| 112 | /** This object's place on the active/inactive lists */ |
| 113 | struct list_head vm_link; |
| 114 | |
| 115 | struct list_head obj_link; /* Link in the object's VMA list */ |
| 116 | struct rb_node obj_node; |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 117 | struct hlist_node obj_hash; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 118 | |
Chris Wilson | 8c45cec | 2017-06-15 09:14:35 +0100 | [diff] [blame] | 119 | /** This vma's place in the execbuf reservation list */ |
| 120 | struct list_head exec_link; |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 121 | struct list_head reloc_link; |
Chris Wilson | 8c45cec | 2017-06-15 09:14:35 +0100 | [diff] [blame] | 122 | |
| 123 | /** This vma's place in the eviction list */ |
| 124 | struct list_head evict_link; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 125 | |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 126 | struct list_head closed_link; |
| 127 | |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 128 | /** |
| 129 | * Used for performing relocations during execbuffer insertion. |
| 130 | */ |
Chris Wilson | c7c6e46 | 2017-08-16 09:52:06 +0100 | [diff] [blame] | 131 | unsigned int *exec_flags; |
Chris Wilson | 95ff7c7 | 2017-06-16 15:05:25 +0100 | [diff] [blame] | 132 | struct hlist_node exec_node; |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 133 | u32 exec_handle; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | struct i915_vma * |
Chris Wilson | 718659a | 2017-01-16 15:21:28 +0000 | [diff] [blame] | 137 | i915_vma_instance(struct drm_i915_gem_object *obj, |
| 138 | struct i915_address_space *vm, |
| 139 | const struct i915_ggtt_view *view); |
| 140 | |
Chris Wilson | 6a2f59e | 2018-07-21 13:50:37 +0100 | [diff] [blame] | 141 | void i915_vma_unpin_and_release(struct i915_vma **p_vma, unsigned int flags); |
| 142 | #define I915_VMA_RELEASE_MAP BIT(0) |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 143 | |
Chris Wilson | 5c3f8c2 | 2018-07-06 11:39:46 +0100 | [diff] [blame] | 144 | static inline bool i915_vma_is_active(struct i915_vma *vma) |
| 145 | { |
| 146 | return vma->active_count; |
| 147 | } |
| 148 | |
| 149 | int __must_check i915_vma_move_to_active(struct i915_vma *vma, |
| 150 | struct i915_request *rq, |
| 151 | unsigned int flags); |
| 152 | |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 153 | static inline bool i915_vma_is_ggtt(const struct i915_vma *vma) |
| 154 | { |
| 155 | return vma->flags & I915_VMA_GGTT; |
| 156 | } |
| 157 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 158 | static inline bool i915_vma_has_ggtt_write(const struct i915_vma *vma) |
| 159 | { |
| 160 | return vma->flags & I915_VMA_GGTT_WRITE; |
| 161 | } |
| 162 | |
| 163 | static inline void i915_vma_set_ggtt_write(struct i915_vma *vma) |
| 164 | { |
| 165 | GEM_BUG_ON(!i915_vma_is_ggtt(vma)); |
| 166 | vma->flags |= I915_VMA_GGTT_WRITE; |
| 167 | } |
| 168 | |
| 169 | static inline void i915_vma_unset_ggtt_write(struct i915_vma *vma) |
| 170 | { |
| 171 | vma->flags &= ~I915_VMA_GGTT_WRITE; |
| 172 | } |
| 173 | |
| 174 | void i915_vma_flush_writes(struct i915_vma *vma); |
| 175 | |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 176 | static inline bool i915_vma_is_map_and_fenceable(const struct i915_vma *vma) |
| 177 | { |
| 178 | return vma->flags & I915_VMA_CAN_FENCE; |
| 179 | } |
| 180 | |
| 181 | static inline bool i915_vma_is_closed(const struct i915_vma *vma) |
| 182 | { |
| 183 | return vma->flags & I915_VMA_CLOSED; |
| 184 | } |
| 185 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 186 | static inline bool i915_vma_set_userfault(struct i915_vma *vma) |
| 187 | { |
| 188 | GEM_BUG_ON(!i915_vma_is_map_and_fenceable(vma)); |
| 189 | return __test_and_set_bit(I915_VMA_USERFAULT_BIT, &vma->flags); |
| 190 | } |
| 191 | |
| 192 | static inline void i915_vma_unset_userfault(struct i915_vma *vma) |
| 193 | { |
| 194 | return __clear_bit(I915_VMA_USERFAULT_BIT, &vma->flags); |
| 195 | } |
| 196 | |
| 197 | static inline bool i915_vma_has_userfault(const struct i915_vma *vma) |
| 198 | { |
| 199 | return test_bit(I915_VMA_USERFAULT_BIT, &vma->flags); |
| 200 | } |
| 201 | |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 202 | static inline u32 i915_ggtt_offset(const struct i915_vma *vma) |
| 203 | { |
| 204 | GEM_BUG_ON(!i915_vma_is_ggtt(vma)); |
| 205 | GEM_BUG_ON(!vma->node.allocated); |
| 206 | GEM_BUG_ON(upper_32_bits(vma->node.start)); |
| 207 | GEM_BUG_ON(upper_32_bits(vma->node.start + vma->node.size - 1)); |
| 208 | return lower_32_bits(vma->node.start); |
| 209 | } |
| 210 | |
Jakub Bartmiński | dd18ced | 2018-07-27 16:11:45 +0200 | [diff] [blame] | 211 | static inline u32 i915_ggtt_pin_bias(struct i915_vma *vma) |
| 212 | { |
| 213 | return i915_vm_to_ggtt(vma->vm)->pin_bias; |
| 214 | } |
| 215 | |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 216 | static inline struct i915_vma *i915_vma_get(struct i915_vma *vma) |
| 217 | { |
| 218 | i915_gem_object_get(vma->obj); |
| 219 | return vma; |
| 220 | } |
| 221 | |
| 222 | static inline void i915_vma_put(struct i915_vma *vma) |
| 223 | { |
| 224 | i915_gem_object_put(vma->obj); |
| 225 | } |
| 226 | |
Tvrtko Ursulin | 2bf0d26 | 2016-12-13 14:37:27 +0000 | [diff] [blame] | 227 | static __always_inline ptrdiff_t ptrdiff(const void *a, const void *b) |
| 228 | { |
| 229 | return a - b; |
| 230 | } |
| 231 | |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 232 | static inline long |
| 233 | i915_vma_compare(struct i915_vma *vma, |
| 234 | struct i915_address_space *vm, |
| 235 | const struct i915_ggtt_view *view) |
| 236 | { |
Tvrtko Ursulin | 2bf0d26 | 2016-12-13 14:37:27 +0000 | [diff] [blame] | 237 | ptrdiff_t cmp; |
| 238 | |
Chris Wilson | 4d1368f | 2016-11-03 20:08:52 +0000 | [diff] [blame] | 239 | GEM_BUG_ON(view && !i915_is_ggtt(vm)); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 240 | |
Tvrtko Ursulin | 2bf0d26 | 2016-12-13 14:37:27 +0000 | [diff] [blame] | 241 | cmp = ptrdiff(vma->vm, vm); |
| 242 | if (cmp) |
| 243 | return cmp; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 244 | |
Chris Wilson | 992e418 | 2017-01-14 00:28:23 +0000 | [diff] [blame] | 245 | BUILD_BUG_ON(I915_GGTT_VIEW_NORMAL != 0); |
| 246 | cmp = vma->ggtt_view.type; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 247 | if (!view) |
Chris Wilson | 992e418 | 2017-01-14 00:28:23 +0000 | [diff] [blame] | 248 | return cmp; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 249 | |
Chris Wilson | 992e418 | 2017-01-14 00:28:23 +0000 | [diff] [blame] | 250 | cmp -= view->type; |
| 251 | if (cmp) |
| 252 | return cmp; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 253 | |
Ville Syrjälä | ed11e41 | 2018-08-28 16:37:23 +0300 | [diff] [blame] | 254 | assert_i915_gem_gtt_types(); |
| 255 | |
Chris Wilson | 992e418 | 2017-01-14 00:28:23 +0000 | [diff] [blame] | 256 | /* ggtt_view.type also encodes its size so that we both distinguish |
| 257 | * different views using it as a "type" and also use a compact (no |
| 258 | * accessing of uninitialised padding bytes) memcmp without storing |
| 259 | * an extra parameter or adding more code. |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 260 | * |
| 261 | * To ensure that the memcmp is valid for all branches of the union, |
| 262 | * even though the code looks like it is just comparing one branch, |
| 263 | * we assert above that all branches have the same address, and that |
| 264 | * each branch has a unique type/size. |
Chris Wilson | 992e418 | 2017-01-14 00:28:23 +0000 | [diff] [blame] | 265 | */ |
| 266 | BUILD_BUG_ON(I915_GGTT_VIEW_NORMAL >= I915_GGTT_VIEW_PARTIAL); |
| 267 | BUILD_BUG_ON(I915_GGTT_VIEW_PARTIAL >= I915_GGTT_VIEW_ROTATED); |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 268 | BUILD_BUG_ON(offsetof(typeof(*view), rotated) != |
| 269 | offsetof(typeof(*view), partial)); |
| 270 | return memcmp(&vma->ggtt_view.partial, &view->partial, view->type); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level, |
| 274 | u32 flags); |
| 275 | bool i915_gem_valid_gtt_space(struct i915_vma *vma, unsigned long cache_level); |
Chris Wilson | 782a3e9 | 2017-02-13 17:15:46 +0000 | [diff] [blame] | 276 | bool i915_vma_misplaced(const struct i915_vma *vma, |
| 277 | u64 size, u64 alignment, u64 flags); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 278 | void __i915_vma_set_map_and_fenceable(struct i915_vma *vma); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 279 | void i915_vma_revoke_mmap(struct i915_vma *vma); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 280 | int __must_check i915_vma_unbind(struct i915_vma *vma); |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 281 | void i915_vma_unlink_ctx(struct i915_vma *vma); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 282 | void i915_vma_close(struct i915_vma *vma); |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 283 | void i915_vma_reopen(struct i915_vma *vma); |
| 284 | void i915_vma_destroy(struct i915_vma *vma); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 285 | |
| 286 | int __i915_vma_do_pin(struct i915_vma *vma, |
| 287 | u64 size, u64 alignment, u64 flags); |
| 288 | static inline int __must_check |
| 289 | i915_vma_pin(struct i915_vma *vma, u64 size, u64 alignment, u64 flags) |
| 290 | { |
| 291 | BUILD_BUG_ON(PIN_MBZ != I915_VMA_PIN_OVERFLOW); |
| 292 | BUILD_BUG_ON(PIN_GLOBAL != I915_VMA_GLOBAL_BIND); |
| 293 | BUILD_BUG_ON(PIN_USER != I915_VMA_LOCAL_BIND); |
| 294 | |
| 295 | /* Pin early to prevent the shrinker/eviction logic from destroying |
| 296 | * our vma as we insert and bind. |
| 297 | */ |
Chris Wilson | 0325701 | 2017-01-11 21:09:26 +0000 | [diff] [blame] | 298 | if (likely(((++vma->flags ^ flags) & I915_VMA_BIND_MASK) == 0)) { |
| 299 | GEM_BUG_ON(!drm_mm_node_allocated(&vma->node)); |
| 300 | GEM_BUG_ON(i915_vma_misplaced(vma, size, alignment, flags)); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 301 | return 0; |
Chris Wilson | 0325701 | 2017-01-11 21:09:26 +0000 | [diff] [blame] | 302 | } |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 303 | |
| 304 | return __i915_vma_do_pin(vma, size, alignment, flags); |
| 305 | } |
| 306 | |
| 307 | static inline int i915_vma_pin_count(const struct i915_vma *vma) |
| 308 | { |
| 309 | return vma->flags & I915_VMA_PIN_MASK; |
| 310 | } |
| 311 | |
| 312 | static inline bool i915_vma_is_pinned(const struct i915_vma *vma) |
| 313 | { |
| 314 | return i915_vma_pin_count(vma); |
| 315 | } |
| 316 | |
| 317 | static inline void __i915_vma_pin(struct i915_vma *vma) |
| 318 | { |
| 319 | vma->flags++; |
| 320 | GEM_BUG_ON(vma->flags & I915_VMA_PIN_OVERFLOW); |
| 321 | } |
| 322 | |
| 323 | static inline void __i915_vma_unpin(struct i915_vma *vma) |
| 324 | { |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 325 | vma->flags--; |
| 326 | } |
| 327 | |
| 328 | static inline void i915_vma_unpin(struct i915_vma *vma) |
| 329 | { |
Chris Wilson | 67fddd9 | 2017-07-21 15:50:34 +0100 | [diff] [blame] | 330 | GEM_BUG_ON(!i915_vma_is_pinned(vma)); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 331 | GEM_BUG_ON(!drm_mm_node_allocated(&vma->node)); |
| 332 | __i915_vma_unpin(vma); |
| 333 | } |
| 334 | |
Chris Wilson | e9e7dc4 | 2018-06-12 13:04:46 +0100 | [diff] [blame] | 335 | static inline bool i915_vma_is_bound(const struct i915_vma *vma, |
| 336 | unsigned int where) |
| 337 | { |
| 338 | return vma->flags & where; |
| 339 | } |
| 340 | |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 341 | /** |
| 342 | * i915_vma_pin_iomap - calls ioremap_wc to map the GGTT VMA via the aperture |
| 343 | * @vma: VMA to iomap |
| 344 | * |
| 345 | * The passed in VMA has to be pinned in the global GTT mappable region. |
| 346 | * An extra pinning of the VMA is acquired for the return iomapping, |
| 347 | * the caller must call i915_vma_unpin_iomap to relinquish the pinning |
| 348 | * after the iomapping is no longer required. |
| 349 | * |
| 350 | * Callers must hold the struct_mutex. |
| 351 | * |
| 352 | * Returns a valid iomapped pointer or ERR_PTR. |
| 353 | */ |
| 354 | void __iomem *i915_vma_pin_iomap(struct i915_vma *vma); |
| 355 | #define IO_ERR_PTR(x) ((void __iomem *)ERR_PTR(x)) |
| 356 | |
| 357 | /** |
| 358 | * i915_vma_unpin_iomap - unpins the mapping returned from i915_vma_iomap |
| 359 | * @vma: VMA to unpin |
| 360 | * |
| 361 | * Unpins the previously iomapped VMA from i915_vma_pin_iomap(). |
| 362 | * |
| 363 | * Callers must hold the struct_mutex. This function is only valid to be |
| 364 | * called on a VMA previously iomapped by the caller with i915_vma_pin_iomap(). |
| 365 | */ |
Chris Wilson | b4563f5 | 2017-10-09 09:43:55 +0100 | [diff] [blame] | 366 | void i915_vma_unpin_iomap(struct i915_vma *vma); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 367 | |
| 368 | static inline struct page *i915_vma_first_page(struct i915_vma *vma) |
| 369 | { |
| 370 | GEM_BUG_ON(!vma->pages); |
| 371 | return sg_page(vma->pages->sgl); |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * i915_vma_pin_fence - pin fencing state |
| 376 | * @vma: vma to pin fencing for |
| 377 | * |
| 378 | * This pins the fencing state (whether tiled or untiled) to make sure the |
| 379 | * vma (and its object) is ready to be used as a scanout target. Fencing |
| 380 | * status must be synchronize first by calling i915_vma_get_fence(): |
| 381 | * |
| 382 | * The resulting fence pin reference must be released again with |
| 383 | * i915_vma_unpin_fence(). |
| 384 | * |
| 385 | * Returns: |
| 386 | * |
| 387 | * True if the vma has a fence, false otherwise. |
| 388 | */ |
Chris Wilson | 3bd4073 | 2017-10-09 09:43:56 +0100 | [diff] [blame] | 389 | int i915_vma_pin_fence(struct i915_vma *vma); |
| 390 | int __must_check i915_vma_put_fence(struct i915_vma *vma); |
| 391 | |
| 392 | static inline void __i915_vma_unpin_fence(struct i915_vma *vma) |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 393 | { |
Chris Wilson | 3bd4073 | 2017-10-09 09:43:56 +0100 | [diff] [blame] | 394 | GEM_BUG_ON(vma->fence->pin_count <= 0); |
| 395 | vma->fence->pin_count--; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | /** |
| 399 | * i915_vma_unpin_fence - unpin fencing state |
| 400 | * @vma: vma to unpin fencing for |
| 401 | * |
| 402 | * This releases the fence pin reference acquired through |
| 403 | * i915_vma_pin_fence. It will handle both objects with and without an |
| 404 | * attached fence correctly, callers do not need to distinguish this. |
| 405 | */ |
| 406 | static inline void |
| 407 | i915_vma_unpin_fence(struct i915_vma *vma) |
| 408 | { |
Chris Wilson | 520ea7c | 2018-06-07 16:40:45 +0100 | [diff] [blame] | 409 | /* lockdep_assert_held(&vma->vm->i915->drm.struct_mutex); */ |
Chris Wilson | 3bd4073 | 2017-10-09 09:43:56 +0100 | [diff] [blame] | 410 | if (vma->fence) |
| 411 | __i915_vma_unpin_fence(vma); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 412 | } |
| 413 | |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 414 | void i915_vma_parked(struct drm_i915_private *i915); |
| 415 | |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 416 | #define for_each_until(cond) if (cond) break; else |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 417 | |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 418 | /** |
| 419 | * for_each_ggtt_vma - Iterate over the GGTT VMA belonging to an object. |
| 420 | * @V: the #i915_vma iterator |
| 421 | * @OBJ: the #drm_i915_gem_object |
| 422 | * |
| 423 | * GGTT VMA are placed at the being of the object's vma_list, see |
| 424 | * vma_create(), so we can stop our walk as soon as we see a ppgtt VMA, |
| 425 | * or the list is empty ofc. |
| 426 | */ |
| 427 | #define for_each_ggtt_vma(V, OBJ) \ |
| 428 | list_for_each_entry(V, &(OBJ)->vma_list, obj_link) \ |
| 429 | for_each_until(!i915_vma_is_ggtt(V)) |
| 430 | |
| 431 | #endif |