Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2008,2010 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 | * Authors: |
| 24 | * Eric Anholt <eric@anholt.net> |
| 25 | * Chris Wilson <chris@chris-wilson.co.uk> |
| 26 | * |
| 27 | */ |
| 28 | |
Eugeni Dodonov | f45b555 | 2011-12-09 17:16:37 -0800 | [diff] [blame] | 29 | #include <linux/dma_remapping.h> |
Chris Wilson | ad778f8 | 2016-08-04 16:32:42 +0100 | [diff] [blame] | 30 | #include <linux/reservation.h> |
Chris Wilson | fec0445 | 2017-01-27 09:40:08 +0000 | [diff] [blame] | 31 | #include <linux/sync_file.h> |
David Hildenbrand | 32d8206 | 2015-05-11 17:52:12 +0200 | [diff] [blame] | 32 | #include <linux/uaccess.h> |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 33 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 34 | #include <drm/drmP.h> |
| 35 | #include <drm/i915_drm.h> |
Chris Wilson | ad778f8 | 2016-08-04 16:32:42 +0100 | [diff] [blame] | 36 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 37 | #include "i915_drv.h" |
Chris Wilson | 57822dc | 2017-02-22 11:40:48 +0000 | [diff] [blame] | 38 | #include "i915_gem_clflush.h" |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 39 | #include "i915_trace.h" |
| 40 | #include "intel_drv.h" |
Chris Wilson | 5d723d7 | 2016-08-04 16:32:35 +0100 | [diff] [blame] | 41 | #include "intel_frontbuffer.h" |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 42 | |
Chris Wilson | d50415c | 2016-08-18 17:16:52 +0100 | [diff] [blame] | 43 | #define DBG_USE_CPU_RELOC 0 /* -1 force GTT relocs; 1 force CPU relocs */ |
| 44 | |
Chris Wilson | dade2a6 | 2017-06-16 15:05:20 +0100 | [diff] [blame] | 45 | #define __EXEC_OBJECT_HAS_REF BIT(31) |
| 46 | #define __EXEC_OBJECT_HAS_PIN BIT(30) |
| 47 | #define __EXEC_OBJECT_HAS_FENCE BIT(29) |
| 48 | #define __EXEC_OBJECT_NEEDS_MAP BIT(28) |
| 49 | #define __EXEC_OBJECT_NEEDS_BIAS BIT(27) |
| 50 | #define __EXEC_OBJECT_INTERNAL_FLAGS (~0u << 27) /* all of the above */ |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 51 | #define __EXEC_OBJECT_RESERVED (__EXEC_OBJECT_HAS_PIN | __EXEC_OBJECT_HAS_FENCE) |
| 52 | |
| 53 | #define __EXEC_HAS_RELOC BIT(31) |
| 54 | #define __EXEC_VALIDATED BIT(30) |
| 55 | #define UPDATE PIN_OFFSET_FIXED |
Chris Wilson | d23db88 | 2014-05-23 08:48:08 +0200 | [diff] [blame] | 56 | |
| 57 | #define BATCH_OFFSET_BIAS (256*1024) |
Chris Wilson | a415d35 | 2013-11-26 11:23:15 +0000 | [diff] [blame] | 58 | |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 59 | #define __I915_EXEC_ILLEGAL_FLAGS \ |
| 60 | (__I915_EXEC_UNKNOWN_FLAGS | I915_EXEC_CONSTANTS_MASK) |
Chris Wilson | 5b043f4 | 2016-08-02 22:50:38 +0100 | [diff] [blame] | 61 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 62 | /** |
| 63 | * DOC: User command execution |
| 64 | * |
| 65 | * Userspace submits commands to be executed on the GPU as an instruction |
| 66 | * stream within a GEM object we call a batchbuffer. This instructions may |
| 67 | * refer to other GEM objects containing auxiliary state such as kernels, |
| 68 | * samplers, render targets and even secondary batchbuffers. Userspace does |
| 69 | * not know where in the GPU memory these objects reside and so before the |
| 70 | * batchbuffer is passed to the GPU for execution, those addresses in the |
| 71 | * batchbuffer and auxiliary objects are updated. This is known as relocation, |
| 72 | * or patching. To try and avoid having to relocate each object on the next |
| 73 | * execution, userspace is told the location of those objects in this pass, |
| 74 | * but this remains just a hint as the kernel may choose a new location for |
| 75 | * any object in the future. |
| 76 | * |
| 77 | * Processing an execbuf ioctl is conceptually split up into a few phases. |
| 78 | * |
| 79 | * 1. Validation - Ensure all the pointers, handles and flags are valid. |
| 80 | * 2. Reservation - Assign GPU address space for every object |
| 81 | * 3. Relocation - Update any addresses to point to the final locations |
| 82 | * 4. Serialisation - Order the request with respect to its dependencies |
| 83 | * 5. Construction - Construct a request to execute the batchbuffer |
| 84 | * 6. Submission (at some point in the future execution) |
| 85 | * |
| 86 | * Reserving resources for the execbuf is the most complicated phase. We |
| 87 | * neither want to have to migrate the object in the address space, nor do |
| 88 | * we want to have to update any relocations pointing to this object. Ideally, |
| 89 | * we want to leave the object where it is and for all the existing relocations |
| 90 | * to match. If the object is given a new address, or if userspace thinks the |
| 91 | * object is elsewhere, we have to parse all the relocation entries and update |
| 92 | * the addresses. Userspace can set the I915_EXEC_NORELOC flag to hint that |
| 93 | * all the target addresses in all of its objects match the value in the |
| 94 | * relocation entries and that they all match the presumed offsets given by the |
| 95 | * list of execbuffer objects. Using this knowledge, we know that if we haven't |
| 96 | * moved any buffers, all the relocation entries are valid and we can skip |
| 97 | * the update. (If userspace is wrong, the likely outcome is an impromptu GPU |
| 98 | * hang.) The requirement for using I915_EXEC_NO_RELOC are: |
| 99 | * |
| 100 | * The addresses written in the objects must match the corresponding |
| 101 | * reloc.presumed_offset which in turn must match the corresponding |
| 102 | * execobject.offset. |
| 103 | * |
| 104 | * Any render targets written to in the batch must be flagged with |
| 105 | * EXEC_OBJECT_WRITE. |
| 106 | * |
| 107 | * To avoid stalling, execobject.offset should match the current |
| 108 | * address of that object within the active context. |
| 109 | * |
| 110 | * The reservation is done is multiple phases. First we try and keep any |
| 111 | * object already bound in its current location - so as long as meets the |
| 112 | * constraints imposed by the new execbuffer. Any object left unbound after the |
| 113 | * first pass is then fitted into any available idle space. If an object does |
| 114 | * not fit, all objects are removed from the reservation and the process rerun |
| 115 | * after sorting the objects into a priority order (more difficult to fit |
| 116 | * objects are tried first). Failing that, the entire VM is cleared and we try |
| 117 | * to fit the execbuf once last time before concluding that it simply will not |
| 118 | * fit. |
| 119 | * |
| 120 | * A small complication to all of this is that we allow userspace not only to |
| 121 | * specify an alignment and a size for the object in the address space, but |
| 122 | * we also allow userspace to specify the exact offset. This objects are |
| 123 | * simpler to place (the location is known a priori) all we have to do is make |
| 124 | * sure the space is available. |
| 125 | * |
| 126 | * Once all the objects are in place, patching up the buried pointers to point |
| 127 | * to the final locations is a fairly simple job of walking over the relocation |
| 128 | * entry arrays, looking up the right address and rewriting the value into |
| 129 | * the object. Simple! ... The relocation entries are stored in user memory |
| 130 | * and so to access them we have to copy them into a local buffer. That copy |
| 131 | * has to avoid taking any pagefaults as they may lead back to a GEM object |
| 132 | * requiring the struct_mutex (i.e. recursive deadlock). So once again we split |
| 133 | * the relocation into multiple passes. First we try to do everything within an |
| 134 | * atomic context (avoid the pagefaults) which requires that we never wait. If |
| 135 | * we detect that we may wait, or if we need to fault, then we have to fallback |
| 136 | * to a slower path. The slowpath has to drop the mutex. (Can you hear alarm |
| 137 | * bells yet?) Dropping the mutex means that we lose all the state we have |
| 138 | * built up so far for the execbuf and we must reset any global data. However, |
| 139 | * we do leave the objects pinned in their final locations - which is a |
| 140 | * potential issue for concurrent execbufs. Once we have left the mutex, we can |
| 141 | * allocate and copy all the relocation entries into a large array at our |
| 142 | * leisure, reacquire the mutex, reclaim all the objects and other state and |
| 143 | * then proceed to update any incorrect addresses with the objects. |
| 144 | * |
| 145 | * As we process the relocation entries, we maintain a record of whether the |
| 146 | * object is being written to. Using NORELOC, we expect userspace to provide |
| 147 | * this information instead. We also check whether we can skip the relocation |
| 148 | * by comparing the expected value inside the relocation entry with the target's |
| 149 | * final address. If they differ, we have to map the current object and rewrite |
| 150 | * the 4 or 8 byte pointer within. |
| 151 | * |
| 152 | * Serialising an execbuf is quite simple according to the rules of the GEM |
| 153 | * ABI. Execution within each context is ordered by the order of submission. |
| 154 | * Writes to any GEM object are in order of submission and are exclusive. Reads |
| 155 | * from a GEM object are unordered with respect to other reads, but ordered by |
| 156 | * writes. A write submitted after a read cannot occur before the read, and |
| 157 | * similarly any read submitted after a write cannot occur before the write. |
| 158 | * Writes are ordered between engines such that only one write occurs at any |
| 159 | * time (completing any reads beforehand) - using semaphores where available |
| 160 | * and CPU serialisation otherwise. Other GEM access obey the same rules, any |
| 161 | * write (either via mmaps using set-domain, or via pwrite) must flush all GPU |
| 162 | * reads before starting, and any read (either using set-domain or pread) must |
| 163 | * flush all GPU writes before starting. (Note we only employ a barrier before, |
| 164 | * we currently rely on userspace not concurrently starting a new execution |
| 165 | * whilst reading or writing to an object. This may be an advantage or not |
| 166 | * depending on how much you trust userspace not to shoot themselves in the |
| 167 | * foot.) Serialisation may just result in the request being inserted into |
| 168 | * a DAG awaiting its turn, but most simple is to wait on the CPU until |
| 169 | * all dependencies are resolved. |
| 170 | * |
| 171 | * After all of that, is just a matter of closing the request and handing it to |
| 172 | * the hardware (well, leaving it in a queue to be executed). However, we also |
| 173 | * offer the ability for batchbuffers to be run with elevated privileges so |
| 174 | * that they access otherwise hidden registers. (Used to adjust L3 cache etc.) |
| 175 | * Before any batch is given extra privileges we first must check that it |
| 176 | * contains no nefarious instructions, we check that each instruction is from |
| 177 | * our whitelist and all registers are also from an allowed list. We first |
| 178 | * copy the user's batchbuffer to a shadow (so that the user doesn't have |
| 179 | * access to it, either by the CPU or GPU as we scan it) and then parse each |
| 180 | * instruction. If everything is ok, we set a flag telling the hardware to run |
| 181 | * the batchbuffer in trusted mode, otherwise the ioctl is rejected. |
| 182 | */ |
| 183 | |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 184 | struct i915_execbuffer { |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 185 | struct drm_i915_private *i915; /** i915 backpointer */ |
| 186 | struct drm_file *file; /** per-file lookup tables and limits */ |
| 187 | struct drm_i915_gem_execbuffer2 *args; /** ioctl parameters */ |
| 188 | struct drm_i915_gem_exec_object2 *exec; /** ioctl execobj[] */ |
| 189 | |
| 190 | struct intel_engine_cs *engine; /** engine to queue the request to */ |
| 191 | struct i915_gem_context *ctx; /** context for building the request */ |
| 192 | struct i915_address_space *vm; /** GTT and vma for the request */ |
| 193 | |
| 194 | struct drm_i915_gem_request *request; /** our request to build */ |
| 195 | struct i915_vma *batch; /** identity of the batch obj/vma */ |
| 196 | |
| 197 | /** actual size of execobj[] as we may extend it for the cmdparser */ |
| 198 | unsigned int buffer_count; |
| 199 | |
| 200 | /** list of vma not yet bound during reservation phase */ |
| 201 | struct list_head unbound; |
| 202 | |
| 203 | /** list of vma that have execobj.relocation_count */ |
| 204 | struct list_head relocs; |
| 205 | |
| 206 | /** |
| 207 | * Track the most recently used object for relocations, as we |
| 208 | * frequently have to perform multiple relocations within the same |
| 209 | * obj/page |
| 210 | */ |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 211 | struct reloc_cache { |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 212 | struct drm_mm_node node; /** temporary GTT binding */ |
| 213 | unsigned long vaddr; /** Current kmap address */ |
| 214 | unsigned long page; /** Currently mapped page index */ |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 215 | bool use_64bit_reloc : 1; |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 216 | bool has_llc : 1; |
| 217 | bool has_fence : 1; |
| 218 | bool needs_unfenced : 1; |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 219 | } reloc_cache; |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 220 | |
| 221 | u64 invalid_flags; /** Set of execobj.flags that are invalid */ |
| 222 | u32 context_flags; /** Set of execobj.flags to insert from the ctx */ |
| 223 | |
| 224 | u32 batch_start_offset; /** Location within object of batch */ |
| 225 | u32 batch_len; /** Length of batch within object */ |
| 226 | u32 batch_flags; /** Flags composed for emit_bb_start() */ |
| 227 | |
| 228 | /** |
| 229 | * Indicate either the size of the hastable used to resolve |
| 230 | * relocation handles, or if negative that we are using a direct |
| 231 | * index into the execobj[]. |
| 232 | */ |
| 233 | int lut_size; |
| 234 | struct hlist_head *buckets; /** ht for relocation handles */ |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 235 | }; |
| 236 | |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 237 | /* |
| 238 | * As an alternative to creating a hashtable of handle-to-vma for a batch, |
| 239 | * we used the last available reserved field in the execobject[] and stash |
| 240 | * a link from the execobj to its vma. |
| 241 | */ |
| 242 | #define __exec_to_vma(ee) (ee)->rsvd2 |
| 243 | #define exec_to_vma(ee) u64_to_ptr(struct i915_vma, __exec_to_vma(ee)) |
| 244 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 245 | /* |
| 246 | * Used to convert any address to canonical form. |
| 247 | * Starting from gen8, some commands (e.g. STATE_BASE_ADDRESS, |
| 248 | * MI_LOAD_REGISTER_MEM and others, see Broadwell PRM Vol2a) require the |
| 249 | * addresses to be in a canonical form: |
| 250 | * "GraphicsAddress[63:48] are ignored by the HW and assumed to be in correct |
| 251 | * canonical form [63:48] == [47]." |
| 252 | */ |
| 253 | #define GEN8_HIGH_ADDRESS_BIT 47 |
| 254 | static inline u64 gen8_canonical_addr(u64 address) |
| 255 | { |
| 256 | return sign_extend64(address, GEN8_HIGH_ADDRESS_BIT); |
| 257 | } |
| 258 | |
| 259 | static inline u64 gen8_noncanonical_addr(u64 address) |
| 260 | { |
| 261 | return address & GENMASK_ULL(GEN8_HIGH_ADDRESS_BIT, 0); |
| 262 | } |
| 263 | |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 264 | static int eb_create(struct i915_execbuffer *eb) |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 265 | { |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 266 | if (!(eb->args->flags & I915_EXEC_HANDLE_LUT)) { |
| 267 | unsigned int size = 1 + ilog2(eb->buffer_count); |
Chris Wilson | eef90cc | 2013-01-08 10:53:17 +0000 | [diff] [blame] | 268 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 269 | /* |
| 270 | * Without a 1:1 association between relocation handles and |
| 271 | * the execobject[] index, we instead create a hashtable. |
| 272 | * We size it dynamically based on available memory, starting |
| 273 | * first with 1:1 assocative hash and scaling back until |
| 274 | * the allocation succeeds. |
| 275 | * |
| 276 | * Later on we use a positive lut_size to indicate we are |
| 277 | * using this hashtable, and a negative value to indicate a |
| 278 | * direct lookup. |
| 279 | */ |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 280 | do { |
| 281 | eb->buckets = kzalloc(sizeof(struct hlist_head) << size, |
| 282 | GFP_TEMPORARY | |
| 283 | __GFP_NORETRY | |
| 284 | __GFP_NOWARN); |
| 285 | if (eb->buckets) |
| 286 | break; |
| 287 | } while (--size); |
Chris Wilson | eef90cc | 2013-01-08 10:53:17 +0000 | [diff] [blame] | 288 | |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 289 | if (unlikely(!eb->buckets)) { |
| 290 | eb->buckets = kzalloc(sizeof(struct hlist_head), |
| 291 | GFP_TEMPORARY); |
| 292 | if (unlikely(!eb->buckets)) |
| 293 | return -ENOMEM; |
| 294 | } |
| 295 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 296 | eb->lut_size = size; |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 297 | } else { |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 298 | eb->lut_size = -eb->buffer_count; |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 299 | } |
Chris Wilson | eef90cc | 2013-01-08 10:53:17 +0000 | [diff] [blame] | 300 | |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 301 | return 0; |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 304 | static bool |
| 305 | eb_vma_misplaced(const struct drm_i915_gem_exec_object2 *entry, |
| 306 | const struct i915_vma *vma) |
| 307 | { |
| 308 | if (!(entry->flags & __EXEC_OBJECT_HAS_PIN)) |
| 309 | return true; |
| 310 | |
| 311 | if (vma->node.size < entry->pad_to_size) |
| 312 | return true; |
| 313 | |
| 314 | if (entry->alignment && !IS_ALIGNED(vma->node.start, entry->alignment)) |
| 315 | return true; |
| 316 | |
| 317 | if (entry->flags & EXEC_OBJECT_PINNED && |
| 318 | vma->node.start != entry->offset) |
| 319 | return true; |
| 320 | |
| 321 | if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS && |
| 322 | vma->node.start < BATCH_OFFSET_BIAS) |
| 323 | return true; |
| 324 | |
| 325 | if (!(entry->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) && |
| 326 | (vma->node.start + vma->node.size - 1) >> 32) |
| 327 | return true; |
| 328 | |
| 329 | return false; |
| 330 | } |
| 331 | |
| 332 | static inline void |
| 333 | eb_pin_vma(struct i915_execbuffer *eb, |
| 334 | struct drm_i915_gem_exec_object2 *entry, |
| 335 | struct i915_vma *vma) |
| 336 | { |
| 337 | u64 flags; |
| 338 | |
Chris Wilson | 616d9ce | 2017-06-16 15:05:21 +0100 | [diff] [blame] | 339 | if (vma->node.size) |
| 340 | flags = vma->node.start; |
| 341 | else |
| 342 | flags = entry->offset & PIN_OFFSET_MASK; |
| 343 | |
| 344 | flags |= PIN_USER | PIN_NOEVICT | PIN_OFFSET_FIXED; |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 345 | if (unlikely(entry->flags & EXEC_OBJECT_NEEDS_GTT)) |
| 346 | flags |= PIN_GLOBAL; |
Chris Wilson | 616d9ce | 2017-06-16 15:05:21 +0100 | [diff] [blame] | 347 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 348 | if (unlikely(i915_vma_pin(vma, 0, 0, flags))) |
| 349 | return; |
| 350 | |
| 351 | if (unlikely(entry->flags & EXEC_OBJECT_NEEDS_FENCE)) { |
| 352 | if (unlikely(i915_vma_get_fence(vma))) { |
| 353 | i915_vma_unpin(vma); |
| 354 | return; |
| 355 | } |
| 356 | |
| 357 | if (i915_vma_pin_fence(vma)) |
| 358 | entry->flags |= __EXEC_OBJECT_HAS_FENCE; |
| 359 | } |
| 360 | |
| 361 | entry->flags |= __EXEC_OBJECT_HAS_PIN; |
| 362 | } |
| 363 | |
Chris Wilson | d55495b | 2017-06-15 09:14:34 +0100 | [diff] [blame] | 364 | static inline void |
| 365 | __eb_unreserve_vma(struct i915_vma *vma, |
| 366 | const struct drm_i915_gem_exec_object2 *entry) |
| 367 | { |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 368 | GEM_BUG_ON(!(entry->flags & __EXEC_OBJECT_HAS_PIN)); |
| 369 | |
Chris Wilson | d55495b | 2017-06-15 09:14:34 +0100 | [diff] [blame] | 370 | if (unlikely(entry->flags & __EXEC_OBJECT_HAS_FENCE)) |
| 371 | i915_vma_unpin_fence(vma); |
| 372 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 373 | __i915_vma_unpin(vma); |
Chris Wilson | d55495b | 2017-06-15 09:14:34 +0100 | [diff] [blame] | 374 | } |
| 375 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 376 | static inline void |
| 377 | eb_unreserve_vma(struct i915_vma *vma, |
| 378 | struct drm_i915_gem_exec_object2 *entry) |
Chris Wilson | d55495b | 2017-06-15 09:14:34 +0100 | [diff] [blame] | 379 | { |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 380 | if (!(entry->flags & __EXEC_OBJECT_HAS_PIN)) |
| 381 | return; |
Chris Wilson | d55495b | 2017-06-15 09:14:34 +0100 | [diff] [blame] | 382 | |
| 383 | __eb_unreserve_vma(vma, entry); |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 384 | entry->flags &= ~__EXEC_OBJECT_RESERVED; |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | static int |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 388 | eb_validate_vma(struct i915_execbuffer *eb, |
| 389 | struct drm_i915_gem_exec_object2 *entry, |
| 390 | struct i915_vma *vma) |
| 391 | { |
| 392 | if (unlikely(entry->flags & eb->invalid_flags)) |
| 393 | return -EINVAL; |
| 394 | |
| 395 | if (unlikely(entry->alignment && !is_power_of_2(entry->alignment))) |
| 396 | return -EINVAL; |
| 397 | |
| 398 | /* |
| 399 | * Offset can be used as input (EXEC_OBJECT_PINNED), reject |
| 400 | * any non-page-aligned or non-canonical addresses. |
| 401 | */ |
| 402 | if (unlikely(entry->flags & EXEC_OBJECT_PINNED && |
| 403 | entry->offset != gen8_canonical_addr(entry->offset & PAGE_MASK))) |
| 404 | return -EINVAL; |
| 405 | |
| 406 | /* pad_to_size was once a reserved field, so sanitize it */ |
| 407 | if (entry->flags & EXEC_OBJECT_PAD_TO_SIZE) { |
| 408 | if (unlikely(offset_in_page(entry->pad_to_size))) |
| 409 | return -EINVAL; |
| 410 | } else { |
| 411 | entry->pad_to_size = 0; |
| 412 | } |
| 413 | |
| 414 | if (unlikely(vma->exec_entry)) { |
| 415 | DRM_DEBUG("Object [handle %d, index %d] appears more than once in object list\n", |
| 416 | entry->handle, (int)(entry - eb->exec)); |
| 417 | return -EINVAL; |
| 418 | } |
| 419 | |
| 420 | /* |
| 421 | * From drm_mm perspective address space is continuous, |
| 422 | * so from this point we're always using non-canonical |
| 423 | * form internally. |
| 424 | */ |
| 425 | entry->offset = gen8_noncanonical_addr(entry->offset); |
| 426 | |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | static int |
| 431 | eb_add_vma(struct i915_execbuffer *eb, |
| 432 | struct drm_i915_gem_exec_object2 *entry, |
| 433 | struct i915_vma *vma) |
| 434 | { |
| 435 | int err; |
| 436 | |
| 437 | GEM_BUG_ON(i915_vma_is_closed(vma)); |
| 438 | |
| 439 | if (!(eb->args->flags & __EXEC_VALIDATED)) { |
| 440 | err = eb_validate_vma(eb, entry, vma); |
| 441 | if (unlikely(err)) |
| 442 | return err; |
| 443 | } |
| 444 | |
| 445 | if (eb->lut_size >= 0) { |
| 446 | vma->exec_handle = entry->handle; |
| 447 | hlist_add_head(&vma->exec_node, |
| 448 | &eb->buckets[hash_32(entry->handle, |
| 449 | eb->lut_size)]); |
| 450 | } |
| 451 | |
| 452 | if (entry->relocation_count) |
| 453 | list_add_tail(&vma->reloc_link, &eb->relocs); |
| 454 | |
| 455 | if (!eb->reloc_cache.has_fence) { |
| 456 | entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE; |
| 457 | } else { |
| 458 | if ((entry->flags & EXEC_OBJECT_NEEDS_FENCE || |
| 459 | eb->reloc_cache.needs_unfenced) && |
| 460 | i915_gem_object_is_tiled(vma->obj)) |
| 461 | entry->flags |= EXEC_OBJECT_NEEDS_GTT | __EXEC_OBJECT_NEEDS_MAP; |
| 462 | } |
| 463 | |
| 464 | if (!(entry->flags & EXEC_OBJECT_PINNED)) |
| 465 | entry->flags |= eb->context_flags; |
| 466 | |
| 467 | /* |
| 468 | * Stash a pointer from the vma to execobj, so we can query its flags, |
| 469 | * size, alignment etc as provided by the user. Also we stash a pointer |
| 470 | * to the vma inside the execobj so that we can use a direct lookup |
| 471 | * to find the right target VMA when doing relocations. |
| 472 | */ |
| 473 | vma->exec_entry = entry; |
Chris Wilson | dade2a6 | 2017-06-16 15:05:20 +0100 | [diff] [blame] | 474 | __exec_to_vma(entry) = (uintptr_t)vma; |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 475 | |
| 476 | err = 0; |
Chris Wilson | 616d9ce | 2017-06-16 15:05:21 +0100 | [diff] [blame] | 477 | eb_pin_vma(eb, entry, vma); |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 478 | if (eb_vma_misplaced(entry, vma)) { |
| 479 | eb_unreserve_vma(vma, entry); |
| 480 | |
| 481 | list_add_tail(&vma->exec_link, &eb->unbound); |
| 482 | if (drm_mm_node_allocated(&vma->node)) |
| 483 | err = i915_vma_unbind(vma); |
| 484 | } else { |
| 485 | if (entry->offset != vma->node.start) { |
| 486 | entry->offset = vma->node.start | UPDATE; |
| 487 | eb->args->flags |= __EXEC_HAS_RELOC; |
| 488 | } |
| 489 | } |
| 490 | return err; |
| 491 | } |
| 492 | |
| 493 | static inline int use_cpu_reloc(const struct reloc_cache *cache, |
| 494 | const struct drm_i915_gem_object *obj) |
| 495 | { |
| 496 | if (!i915_gem_object_has_struct_page(obj)) |
| 497 | return false; |
| 498 | |
| 499 | if (DBG_USE_CPU_RELOC) |
| 500 | return DBG_USE_CPU_RELOC > 0; |
| 501 | |
| 502 | return (cache->has_llc || |
| 503 | obj->cache_dirty || |
| 504 | obj->cache_level != I915_CACHE_NONE); |
| 505 | } |
| 506 | |
| 507 | static int eb_reserve_vma(const struct i915_execbuffer *eb, |
| 508 | struct i915_vma *vma) |
| 509 | { |
| 510 | struct drm_i915_gem_exec_object2 *entry = vma->exec_entry; |
| 511 | u64 flags; |
| 512 | int err; |
| 513 | |
| 514 | flags = PIN_USER | PIN_NONBLOCK; |
| 515 | if (entry->flags & EXEC_OBJECT_NEEDS_GTT) |
| 516 | flags |= PIN_GLOBAL; |
| 517 | |
| 518 | /* |
| 519 | * Wa32bitGeneralStateOffset & Wa32bitInstructionBaseOffset, |
| 520 | * limit address to the first 4GBs for unflagged objects. |
| 521 | */ |
| 522 | if (!(entry->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS)) |
| 523 | flags |= PIN_ZONE_4G; |
| 524 | |
| 525 | if (entry->flags & __EXEC_OBJECT_NEEDS_MAP) |
| 526 | flags |= PIN_MAPPABLE; |
| 527 | |
| 528 | if (entry->flags & EXEC_OBJECT_PINNED) { |
| 529 | flags |= entry->offset | PIN_OFFSET_FIXED; |
| 530 | flags &= ~PIN_NONBLOCK; /* force overlapping PINNED checks */ |
| 531 | } else if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS) { |
| 532 | flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS; |
| 533 | } |
| 534 | |
| 535 | err = i915_vma_pin(vma, entry->pad_to_size, entry->alignment, flags); |
| 536 | if (err) |
| 537 | return err; |
| 538 | |
| 539 | if (entry->offset != vma->node.start) { |
| 540 | entry->offset = vma->node.start | UPDATE; |
| 541 | eb->args->flags |= __EXEC_HAS_RELOC; |
| 542 | } |
| 543 | |
| 544 | entry->flags |= __EXEC_OBJECT_HAS_PIN; |
| 545 | GEM_BUG_ON(eb_vma_misplaced(entry, vma)); |
| 546 | |
| 547 | if (unlikely(entry->flags & EXEC_OBJECT_NEEDS_FENCE)) { |
| 548 | err = i915_vma_get_fence(vma); |
| 549 | if (unlikely(err)) { |
| 550 | i915_vma_unpin(vma); |
| 551 | return err; |
| 552 | } |
| 553 | |
| 554 | if (i915_vma_pin_fence(vma)) |
| 555 | entry->flags |= __EXEC_OBJECT_HAS_FENCE; |
| 556 | } |
| 557 | |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | static int eb_reserve(struct i915_execbuffer *eb) |
| 562 | { |
| 563 | const unsigned int count = eb->buffer_count; |
| 564 | struct list_head last; |
| 565 | struct i915_vma *vma; |
| 566 | unsigned int i, pass; |
| 567 | int err; |
| 568 | |
| 569 | /* |
| 570 | * Attempt to pin all of the buffers into the GTT. |
| 571 | * This is done in 3 phases: |
| 572 | * |
| 573 | * 1a. Unbind all objects that do not match the GTT constraints for |
| 574 | * the execbuffer (fenceable, mappable, alignment etc). |
| 575 | * 1b. Increment pin count for already bound objects. |
| 576 | * 2. Bind new objects. |
| 577 | * 3. Decrement pin count. |
| 578 | * |
| 579 | * This avoid unnecessary unbinding of later objects in order to make |
| 580 | * room for the earlier objects *unless* we need to defragment. |
| 581 | */ |
| 582 | |
| 583 | pass = 0; |
| 584 | err = 0; |
| 585 | do { |
| 586 | list_for_each_entry(vma, &eb->unbound, exec_link) { |
| 587 | err = eb_reserve_vma(eb, vma); |
| 588 | if (err) |
| 589 | break; |
| 590 | } |
| 591 | if (err != -ENOSPC) |
| 592 | return err; |
| 593 | |
| 594 | /* Resort *all* the objects into priority order */ |
| 595 | INIT_LIST_HEAD(&eb->unbound); |
| 596 | INIT_LIST_HEAD(&last); |
| 597 | for (i = 0; i < count; i++) { |
| 598 | struct drm_i915_gem_exec_object2 *entry = &eb->exec[i]; |
| 599 | |
| 600 | if (entry->flags & EXEC_OBJECT_PINNED && |
| 601 | entry->flags & __EXEC_OBJECT_HAS_PIN) |
| 602 | continue; |
| 603 | |
| 604 | vma = exec_to_vma(entry); |
| 605 | eb_unreserve_vma(vma, entry); |
| 606 | |
| 607 | if (entry->flags & EXEC_OBJECT_PINNED) |
| 608 | list_add(&vma->exec_link, &eb->unbound); |
| 609 | else if (entry->flags & __EXEC_OBJECT_NEEDS_MAP) |
| 610 | list_add_tail(&vma->exec_link, &eb->unbound); |
| 611 | else |
| 612 | list_add_tail(&vma->exec_link, &last); |
| 613 | } |
| 614 | list_splice_tail(&last, &eb->unbound); |
| 615 | |
| 616 | switch (pass++) { |
| 617 | case 0: |
| 618 | break; |
| 619 | |
| 620 | case 1: |
| 621 | /* Too fragmented, unbind everything and retry */ |
| 622 | err = i915_gem_evict_vm(eb->vm); |
| 623 | if (err) |
| 624 | return err; |
| 625 | break; |
| 626 | |
| 627 | default: |
| 628 | return -ENOSPC; |
| 629 | } |
| 630 | } while (1); |
| 631 | } |
| 632 | |
| 633 | static inline struct hlist_head * |
| 634 | ht_head(const struct i915_gem_context_vma_lut *lut, u32 handle) |
| 635 | { |
| 636 | return &lut->ht[hash_32(handle, lut->ht_bits)]; |
| 637 | } |
| 638 | |
| 639 | static inline bool |
| 640 | ht_needs_resize(const struct i915_gem_context_vma_lut *lut) |
| 641 | { |
| 642 | return (4*lut->ht_count > 3*lut->ht_size || |
| 643 | 4*lut->ht_count + 1 < lut->ht_size); |
| 644 | } |
| 645 | |
| 646 | static unsigned int eb_batch_index(const struct i915_execbuffer *eb) |
| 647 | { |
| 648 | return eb->buffer_count - 1; |
| 649 | } |
| 650 | |
| 651 | static int eb_select_context(struct i915_execbuffer *eb) |
| 652 | { |
| 653 | struct i915_gem_context *ctx; |
| 654 | |
| 655 | ctx = i915_gem_context_lookup(eb->file->driver_priv, eb->args->rsvd1); |
| 656 | if (unlikely(IS_ERR(ctx))) |
| 657 | return PTR_ERR(ctx); |
| 658 | |
| 659 | if (unlikely(i915_gem_context_is_banned(ctx))) { |
| 660 | DRM_DEBUG("Context %u tried to submit while banned\n", |
| 661 | ctx->user_handle); |
| 662 | return -EIO; |
| 663 | } |
| 664 | |
| 665 | eb->ctx = i915_gem_context_get(ctx); |
| 666 | eb->vm = ctx->ppgtt ? &ctx->ppgtt->base : &eb->i915->ggtt.base; |
| 667 | |
| 668 | eb->context_flags = 0; |
| 669 | if (ctx->flags & CONTEXT_NO_ZEROMAP) |
| 670 | eb->context_flags |= __EXEC_OBJECT_NEEDS_BIAS; |
| 671 | |
| 672 | return 0; |
| 673 | } |
| 674 | |
| 675 | static int eb_lookup_vmas(struct i915_execbuffer *eb) |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 676 | { |
| 677 | #define INTERMEDIATE BIT(0) |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 678 | const unsigned int count = eb->buffer_count; |
| 679 | struct i915_gem_context_vma_lut *lut = &eb->ctx->vma_lut; |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 680 | struct i915_vma *vma; |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 681 | struct idr *idr; |
| 682 | unsigned int i; |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 683 | int slow_pass = -1; |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 684 | int err; |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 685 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 686 | INIT_LIST_HEAD(&eb->relocs); |
| 687 | INIT_LIST_HEAD(&eb->unbound); |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 688 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 689 | if (unlikely(lut->ht_size & I915_CTX_RESIZE_IN_PROGRESS)) |
| 690 | flush_work(&lut->resize); |
| 691 | GEM_BUG_ON(lut->ht_size & I915_CTX_RESIZE_IN_PROGRESS); |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 692 | |
| 693 | for (i = 0; i < count; i++) { |
| 694 | __exec_to_vma(&eb->exec[i]) = 0; |
| 695 | |
| 696 | hlist_for_each_entry(vma, |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 697 | ht_head(lut, eb->exec[i].handle), |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 698 | ctx_node) { |
| 699 | if (vma->ctx_handle != eb->exec[i].handle) |
| 700 | continue; |
| 701 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 702 | err = eb_add_vma(eb, &eb->exec[i], vma); |
| 703 | if (unlikely(err)) |
| 704 | return err; |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 705 | |
| 706 | goto next_vma; |
| 707 | } |
| 708 | |
| 709 | if (slow_pass < 0) |
| 710 | slow_pass = i; |
| 711 | next_vma: ; |
| 712 | } |
| 713 | |
| 714 | if (slow_pass < 0) |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 715 | goto out; |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 716 | |
| 717 | spin_lock(&eb->file->table_lock); |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 718 | /* |
| 719 | * Grab a reference to the object and release the lock so we can lookup |
| 720 | * or create the VMA without using GFP_ATOMIC |
| 721 | */ |
| 722 | idr = &eb->file->object_idr; |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 723 | for (i = slow_pass; i < count; i++) { |
| 724 | struct drm_i915_gem_object *obj; |
| 725 | |
| 726 | if (__exec_to_vma(&eb->exec[i])) |
| 727 | continue; |
| 728 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 729 | obj = to_intel_bo(idr_find(idr, eb->exec[i].handle)); |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 730 | if (unlikely(!obj)) { |
| 731 | spin_unlock(&eb->file->table_lock); |
| 732 | DRM_DEBUG("Invalid object handle %d at index %d\n", |
| 733 | eb->exec[i].handle, i); |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 734 | err = -ENOENT; |
| 735 | goto err; |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | __exec_to_vma(&eb->exec[i]) = INTERMEDIATE | (uintptr_t)obj; |
| 739 | } |
| 740 | spin_unlock(&eb->file->table_lock); |
| 741 | |
| 742 | for (i = slow_pass; i < count; i++) { |
| 743 | struct drm_i915_gem_object *obj; |
| 744 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 745 | if (!(__exec_to_vma(&eb->exec[i]) & INTERMEDIATE)) |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 746 | continue; |
| 747 | |
| 748 | /* |
| 749 | * NOTE: We can leak any vmas created here when something fails |
| 750 | * later on. But that's no issue since vma_unbind can deal with |
| 751 | * vmas which are not actually bound. And since only |
| 752 | * lookup_or_create exists as an interface to get at the vma |
| 753 | * from the (obj, vm) we don't run the risk of creating |
| 754 | * duplicated vmas for the same vm. |
| 755 | */ |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 756 | obj = u64_to_ptr(typeof(*obj), |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 757 | __exec_to_vma(&eb->exec[i]) & ~INTERMEDIATE); |
| 758 | vma = i915_vma_instance(obj, eb->vm, NULL); |
| 759 | if (unlikely(IS_ERR(vma))) { |
| 760 | DRM_DEBUG("Failed to lookup VMA\n"); |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 761 | err = PTR_ERR(vma); |
| 762 | goto err; |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | /* First come, first served */ |
| 766 | if (!vma->ctx) { |
| 767 | vma->ctx = eb->ctx; |
| 768 | vma->ctx_handle = eb->exec[i].handle; |
| 769 | hlist_add_head(&vma->ctx_node, |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 770 | ht_head(lut, eb->exec[i].handle)); |
| 771 | lut->ht_count++; |
| 772 | lut->ht_size |= I915_CTX_RESIZE_IN_PROGRESS; |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 773 | if (i915_vma_is_ggtt(vma)) { |
| 774 | GEM_BUG_ON(obj->vma_hashed); |
| 775 | obj->vma_hashed = vma; |
| 776 | } |
Chris Wilson | dade2a6 | 2017-06-16 15:05:20 +0100 | [diff] [blame] | 777 | |
| 778 | i915_vma_get(vma); |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 779 | } |
| 780 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 781 | err = eb_add_vma(eb, &eb->exec[i], vma); |
| 782 | if (unlikely(err)) |
| 783 | goto err; |
Chris Wilson | dade2a6 | 2017-06-16 15:05:20 +0100 | [diff] [blame] | 784 | |
| 785 | /* Only after we validated the user didn't use our bits */ |
| 786 | if (vma->ctx != eb->ctx) { |
| 787 | i915_vma_get(vma); |
| 788 | eb->exec[i].flags |= __EXEC_OBJECT_HAS_REF; |
| 789 | } |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 790 | } |
| 791 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 792 | if (lut->ht_size & I915_CTX_RESIZE_IN_PROGRESS) { |
| 793 | if (ht_needs_resize(lut)) |
| 794 | queue_work(system_highpri_wq, &lut->resize); |
| 795 | else |
| 796 | lut->ht_size &= ~I915_CTX_RESIZE_IN_PROGRESS; |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 797 | } |
| 798 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 799 | out: |
| 800 | /* take note of the batch buffer before we might reorder the lists */ |
| 801 | i = eb_batch_index(eb); |
| 802 | eb->batch = exec_to_vma(&eb->exec[i]); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 803 | |
| 804 | /* |
| 805 | * SNA is doing fancy tricks with compressing batch buffers, which leads |
| 806 | * to negative relocation deltas. Usually that works out ok since the |
| 807 | * relocate address is still positive, except when the batch is placed |
| 808 | * very low in the GTT. Ensure this doesn't happen. |
| 809 | * |
| 810 | * Note that actual hangs have only been observed on gen7, but for |
| 811 | * paranoia do it everywhere. |
| 812 | */ |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 813 | if (!(eb->exec[i].flags & EXEC_OBJECT_PINNED)) |
| 814 | eb->exec[i].flags |= __EXEC_OBJECT_NEEDS_BIAS; |
| 815 | if (eb->reloc_cache.has_fence) |
| 816 | eb->exec[i].flags |= EXEC_OBJECT_NEEDS_FENCE; |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 817 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 818 | eb->args->flags |= __EXEC_VALIDATED; |
| 819 | return eb_reserve(eb); |
| 820 | |
| 821 | err: |
| 822 | for (i = slow_pass; i < count; i++) { |
| 823 | if (__exec_to_vma(&eb->exec[i]) & INTERMEDIATE) |
| 824 | __exec_to_vma(&eb->exec[i]) = 0; |
| 825 | } |
| 826 | lut->ht_size &= ~I915_CTX_RESIZE_IN_PROGRESS; |
| 827 | return err; |
| 828 | #undef INTERMEDIATE |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 829 | } |
| 830 | |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 831 | static struct i915_vma * |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 832 | eb_get_vma(const struct i915_execbuffer *eb, unsigned long handle) |
Chris Wilson | 3b96eff | 2013-01-08 10:53:14 +0000 | [diff] [blame] | 833 | { |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 834 | if (eb->lut_size < 0) { |
| 835 | if (handle >= -eb->lut_size) |
Chris Wilson | eef90cc | 2013-01-08 10:53:17 +0000 | [diff] [blame] | 836 | return NULL; |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 837 | return exec_to_vma(&eb->exec[handle]); |
Chris Wilson | eef90cc | 2013-01-08 10:53:17 +0000 | [diff] [blame] | 838 | } else { |
| 839 | struct hlist_head *head; |
Geliang Tang | aa45950 | 2016-01-18 23:54:20 +0800 | [diff] [blame] | 840 | struct i915_vma *vma; |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 841 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 842 | head = &eb->buckets[hash_32(handle, eb->lut_size)]; |
Geliang Tang | aa45950 | 2016-01-18 23:54:20 +0800 | [diff] [blame] | 843 | hlist_for_each_entry(vma, head, exec_node) { |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 844 | if (vma->exec_handle == handle) |
| 845 | return vma; |
Chris Wilson | eef90cc | 2013-01-08 10:53:17 +0000 | [diff] [blame] | 846 | } |
| 847 | return NULL; |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 848 | } |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 849 | } |
| 850 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 851 | static void eb_release_vmas(const struct i915_execbuffer *eb) |
Chris Wilson | a415d35 | 2013-11-26 11:23:15 +0000 | [diff] [blame] | 852 | { |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 853 | const unsigned int count = eb->buffer_count; |
| 854 | unsigned int i; |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 855 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 856 | for (i = 0; i < count; i++) { |
| 857 | struct drm_i915_gem_exec_object2 *entry = &eb->exec[i]; |
| 858 | struct i915_vma *vma = exec_to_vma(entry); |
| 859 | |
| 860 | if (!vma) |
Chris Wilson | d55495b | 2017-06-15 09:14:34 +0100 | [diff] [blame] | 861 | continue; |
Chris Wilson | bcffc3f | 2013-01-08 10:53:15 +0000 | [diff] [blame] | 862 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 863 | GEM_BUG_ON(vma->exec_entry != entry); |
Chris Wilson | 172ae5b | 2016-12-05 14:29:37 +0000 | [diff] [blame] | 864 | vma->exec_entry = NULL; |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 865 | |
Chris Wilson | dade2a6 | 2017-06-16 15:05:20 +0100 | [diff] [blame] | 866 | if (entry->flags & __EXEC_OBJECT_HAS_PIN) |
| 867 | __eb_unreserve_vma(vma, entry); |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 868 | |
Chris Wilson | dade2a6 | 2017-06-16 15:05:20 +0100 | [diff] [blame] | 869 | if (entry->flags & __EXEC_OBJECT_HAS_REF) |
| 870 | i915_vma_put(vma); |
| 871 | |
| 872 | entry->flags &= |
| 873 | ~(__EXEC_OBJECT_RESERVED | __EXEC_OBJECT_HAS_REF); |
Chris Wilson | bcffc3f | 2013-01-08 10:53:15 +0000 | [diff] [blame] | 874 | } |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 875 | } |
Chris Wilson | d55495b | 2017-06-15 09:14:34 +0100 | [diff] [blame] | 876 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 877 | static void eb_reset_vmas(const struct i915_execbuffer *eb) |
| 878 | { |
| 879 | eb_release_vmas(eb); |
| 880 | if (eb->lut_size >= 0) |
| 881 | memset(eb->buckets, 0, |
| 882 | sizeof(struct hlist_head) << eb->lut_size); |
| 883 | } |
Chris Wilson | d55495b | 2017-06-15 09:14:34 +0100 | [diff] [blame] | 884 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 885 | static void eb_destroy(const struct i915_execbuffer *eb) |
| 886 | { |
| 887 | if (eb->lut_size >= 0) |
Chris Wilson | d55495b | 2017-06-15 09:14:34 +0100 | [diff] [blame] | 888 | kfree(eb->buckets); |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 889 | } |
| 890 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 891 | static inline u64 |
Chris Wilson | d50415c | 2016-08-18 17:16:52 +0100 | [diff] [blame] | 892 | relocation_target(const struct drm_i915_gem_relocation_entry *reloc, |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 893 | const struct i915_vma *target) |
Michał Winiarski | 934acce | 2015-12-29 18:24:52 +0100 | [diff] [blame] | 894 | { |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 895 | return gen8_canonical_addr((int)reloc->delta + target->node.start); |
Michał Winiarski | 934acce | 2015-12-29 18:24:52 +0100 | [diff] [blame] | 896 | } |
| 897 | |
Chris Wilson | d50415c | 2016-08-18 17:16:52 +0100 | [diff] [blame] | 898 | static void reloc_cache_init(struct reloc_cache *cache, |
| 899 | struct drm_i915_private *i915) |
Rafael Barbalho | 5032d87 | 2013-08-21 17:10:51 +0100 | [diff] [blame] | 900 | { |
Chris Wilson | 31a3920 | 2016-08-18 17:16:46 +0100 | [diff] [blame] | 901 | cache->page = -1; |
Chris Wilson | d50415c | 2016-08-18 17:16:52 +0100 | [diff] [blame] | 902 | cache->vaddr = 0; |
Joonas Lahtinen | dfc5148 | 2016-11-03 10:39:46 +0200 | [diff] [blame] | 903 | /* Must be a variable in the struct to allow GCC to unroll. */ |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 904 | cache->has_llc = HAS_LLC(i915); |
| 905 | cache->has_fence = INTEL_GEN(i915) < 4; |
| 906 | cache->needs_unfenced = INTEL_INFO(i915)->unfenced_needs_alignment; |
Joonas Lahtinen | dfc5148 | 2016-11-03 10:39:46 +0200 | [diff] [blame] | 907 | cache->use_64bit_reloc = HAS_64BIT_RELOC(i915); |
Chris Wilson | e8cb909 | 2016-08-18 17:16:53 +0100 | [diff] [blame] | 908 | cache->node.allocated = false; |
Chris Wilson | 31a3920 | 2016-08-18 17:16:46 +0100 | [diff] [blame] | 909 | } |
Rafael Barbalho | 5032d87 | 2013-08-21 17:10:51 +0100 | [diff] [blame] | 910 | |
Chris Wilson | d50415c | 2016-08-18 17:16:52 +0100 | [diff] [blame] | 911 | static inline void *unmask_page(unsigned long p) |
| 912 | { |
| 913 | return (void *)(uintptr_t)(p & PAGE_MASK); |
| 914 | } |
Rafael Barbalho | 5032d87 | 2013-08-21 17:10:51 +0100 | [diff] [blame] | 915 | |
Chris Wilson | d50415c | 2016-08-18 17:16:52 +0100 | [diff] [blame] | 916 | static inline unsigned int unmask_flags(unsigned long p) |
| 917 | { |
| 918 | return p & ~PAGE_MASK; |
| 919 | } |
Ben Widawsky | 3c94cee | 2013-11-02 21:07:11 -0700 | [diff] [blame] | 920 | |
Chris Wilson | d50415c | 2016-08-18 17:16:52 +0100 | [diff] [blame] | 921 | #define KMAP 0x4 /* after CLFLUSH_FLAGS */ |
Ben Widawsky | 3c94cee | 2013-11-02 21:07:11 -0700 | [diff] [blame] | 922 | |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 923 | static inline struct i915_ggtt *cache_to_ggtt(struct reloc_cache *cache) |
| 924 | { |
| 925 | struct drm_i915_private *i915 = |
| 926 | container_of(cache, struct i915_execbuffer, reloc_cache)->i915; |
| 927 | return &i915->ggtt; |
| 928 | } |
| 929 | |
| 930 | static void reloc_cache_reset(struct reloc_cache *cache) |
Chris Wilson | 31a3920 | 2016-08-18 17:16:46 +0100 | [diff] [blame] | 931 | { |
Chris Wilson | d50415c | 2016-08-18 17:16:52 +0100 | [diff] [blame] | 932 | void *vaddr; |
| 933 | |
Chris Wilson | 31a3920 | 2016-08-18 17:16:46 +0100 | [diff] [blame] | 934 | if (!cache->vaddr) |
| 935 | return; |
| 936 | |
Chris Wilson | d50415c | 2016-08-18 17:16:52 +0100 | [diff] [blame] | 937 | vaddr = unmask_page(cache->vaddr); |
| 938 | if (cache->vaddr & KMAP) { |
| 939 | if (cache->vaddr & CLFLUSH_AFTER) |
| 940 | mb(); |
Chris Wilson | 31a3920 | 2016-08-18 17:16:46 +0100 | [diff] [blame] | 941 | |
Chris Wilson | d50415c | 2016-08-18 17:16:52 +0100 | [diff] [blame] | 942 | kunmap_atomic(vaddr); |
| 943 | i915_gem_obj_finish_shmem_access((struct drm_i915_gem_object *)cache->node.mm); |
| 944 | } else { |
Chris Wilson | e8cb909 | 2016-08-18 17:16:53 +0100 | [diff] [blame] | 945 | wmb(); |
Chris Wilson | d50415c | 2016-08-18 17:16:52 +0100 | [diff] [blame] | 946 | io_mapping_unmap_atomic((void __iomem *)vaddr); |
Chris Wilson | e8cb909 | 2016-08-18 17:16:53 +0100 | [diff] [blame] | 947 | if (cache->node.allocated) { |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 948 | struct i915_ggtt *ggtt = cache_to_ggtt(cache); |
Chris Wilson | e8cb909 | 2016-08-18 17:16:53 +0100 | [diff] [blame] | 949 | |
| 950 | ggtt->base.clear_range(&ggtt->base, |
| 951 | cache->node.start, |
Michał Winiarski | 4fb84d9 | 2016-10-13 14:02:40 +0200 | [diff] [blame] | 952 | cache->node.size); |
Chris Wilson | e8cb909 | 2016-08-18 17:16:53 +0100 | [diff] [blame] | 953 | drm_mm_remove_node(&cache->node); |
| 954 | } else { |
| 955 | i915_vma_unpin((struct i915_vma *)cache->node.mm); |
Ben Widawsky | 3c94cee | 2013-11-02 21:07:11 -0700 | [diff] [blame] | 956 | } |
Chris Wilson | 31a3920 | 2016-08-18 17:16:46 +0100 | [diff] [blame] | 957 | } |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 958 | |
| 959 | cache->vaddr = 0; |
| 960 | cache->page = -1; |
Chris Wilson | 31a3920 | 2016-08-18 17:16:46 +0100 | [diff] [blame] | 961 | } |
Ben Widawsky | 3c94cee | 2013-11-02 21:07:11 -0700 | [diff] [blame] | 962 | |
Chris Wilson | 31a3920 | 2016-08-18 17:16:46 +0100 | [diff] [blame] | 963 | static void *reloc_kmap(struct drm_i915_gem_object *obj, |
| 964 | struct reloc_cache *cache, |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 965 | unsigned long page) |
Chris Wilson | 31a3920 | 2016-08-18 17:16:46 +0100 | [diff] [blame] | 966 | { |
Chris Wilson | d50415c | 2016-08-18 17:16:52 +0100 | [diff] [blame] | 967 | void *vaddr; |
Chris Wilson | 31a3920 | 2016-08-18 17:16:46 +0100 | [diff] [blame] | 968 | |
Chris Wilson | d50415c | 2016-08-18 17:16:52 +0100 | [diff] [blame] | 969 | if (cache->vaddr) { |
| 970 | kunmap_atomic(unmask_page(cache->vaddr)); |
| 971 | } else { |
| 972 | unsigned int flushes; |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 973 | int err; |
Chris Wilson | 31a3920 | 2016-08-18 17:16:46 +0100 | [diff] [blame] | 974 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 975 | err = i915_gem_obj_prepare_shmem_write(obj, &flushes); |
| 976 | if (err) |
| 977 | return ERR_PTR(err); |
Chris Wilson | d50415c | 2016-08-18 17:16:52 +0100 | [diff] [blame] | 978 | |
| 979 | BUILD_BUG_ON(KMAP & CLFLUSH_FLAGS); |
| 980 | BUILD_BUG_ON((KMAP | CLFLUSH_FLAGS) & PAGE_MASK); |
| 981 | |
| 982 | cache->vaddr = flushes | KMAP; |
| 983 | cache->node.mm = (void *)obj; |
| 984 | if (flushes) |
| 985 | mb(); |
Ben Widawsky | 3c94cee | 2013-11-02 21:07:11 -0700 | [diff] [blame] | 986 | } |
| 987 | |
Chris Wilson | d50415c | 2016-08-18 17:16:52 +0100 | [diff] [blame] | 988 | vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj, page)); |
| 989 | cache->vaddr = unmask_flags(cache->vaddr) | (unsigned long)vaddr; |
Chris Wilson | 31a3920 | 2016-08-18 17:16:46 +0100 | [diff] [blame] | 990 | cache->page = page; |
Chris Wilson | 31a3920 | 2016-08-18 17:16:46 +0100 | [diff] [blame] | 991 | |
Chris Wilson | d50415c | 2016-08-18 17:16:52 +0100 | [diff] [blame] | 992 | return vaddr; |
Chris Wilson | 31a3920 | 2016-08-18 17:16:46 +0100 | [diff] [blame] | 993 | } |
| 994 | |
Chris Wilson | d50415c | 2016-08-18 17:16:52 +0100 | [diff] [blame] | 995 | static void *reloc_iomap(struct drm_i915_gem_object *obj, |
Chris Wilson | 31a3920 | 2016-08-18 17:16:46 +0100 | [diff] [blame] | 996 | struct reloc_cache *cache, |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 997 | unsigned long page) |
Chris Wilson | 31a3920 | 2016-08-18 17:16:46 +0100 | [diff] [blame] | 998 | { |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 999 | struct i915_ggtt *ggtt = cache_to_ggtt(cache); |
Chris Wilson | e8cb909 | 2016-08-18 17:16:53 +0100 | [diff] [blame] | 1000 | unsigned long offset; |
Chris Wilson | d50415c | 2016-08-18 17:16:52 +0100 | [diff] [blame] | 1001 | void *vaddr; |
Chris Wilson | 31a3920 | 2016-08-18 17:16:46 +0100 | [diff] [blame] | 1002 | |
Chris Wilson | d50415c | 2016-08-18 17:16:52 +0100 | [diff] [blame] | 1003 | if (cache->vaddr) { |
Jani Nikula | 615e500 | 2016-10-04 12:54:13 +0300 | [diff] [blame] | 1004 | io_mapping_unmap_atomic((void __force __iomem *) unmask_page(cache->vaddr)); |
Chris Wilson | d50415c | 2016-08-18 17:16:52 +0100 | [diff] [blame] | 1005 | } else { |
| 1006 | struct i915_vma *vma; |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1007 | int err; |
Chris Wilson | 31a3920 | 2016-08-18 17:16:46 +0100 | [diff] [blame] | 1008 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1009 | if (use_cpu_reloc(cache, obj)) |
Chris Wilson | d50415c | 2016-08-18 17:16:52 +0100 | [diff] [blame] | 1010 | return NULL; |
Chris Wilson | 31a3920 | 2016-08-18 17:16:46 +0100 | [diff] [blame] | 1011 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1012 | err = i915_gem_object_set_to_gtt_domain(obj, true); |
| 1013 | if (err) |
| 1014 | return ERR_PTR(err); |
Chris Wilson | 31a3920 | 2016-08-18 17:16:46 +0100 | [diff] [blame] | 1015 | |
Chris Wilson | d50415c | 2016-08-18 17:16:52 +0100 | [diff] [blame] | 1016 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
| 1017 | PIN_MAPPABLE | PIN_NONBLOCK); |
Chris Wilson | e8cb909 | 2016-08-18 17:16:53 +0100 | [diff] [blame] | 1018 | if (IS_ERR(vma)) { |
| 1019 | memset(&cache->node, 0, sizeof(cache->node)); |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1020 | err = drm_mm_insert_node_in_range |
Chris Wilson | e8cb909 | 2016-08-18 17:16:53 +0100 | [diff] [blame] | 1021 | (&ggtt->base.mm, &cache->node, |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame] | 1022 | PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE, |
Chris Wilson | e8cb909 | 2016-08-18 17:16:53 +0100 | [diff] [blame] | 1023 | 0, ggtt->mappable_end, |
Chris Wilson | 4e64e55 | 2017-02-02 21:04:38 +0000 | [diff] [blame] | 1024 | DRM_MM_INSERT_LOW); |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1025 | if (err) /* no inactive aperture space, use cpu reloc */ |
Chris Wilson | c92fa4f | 2016-10-07 07:53:25 +0100 | [diff] [blame] | 1026 | return NULL; |
Chris Wilson | e8cb909 | 2016-08-18 17:16:53 +0100 | [diff] [blame] | 1027 | } else { |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1028 | err = i915_vma_put_fence(vma); |
| 1029 | if (err) { |
Chris Wilson | e8cb909 | 2016-08-18 17:16:53 +0100 | [diff] [blame] | 1030 | i915_vma_unpin(vma); |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1031 | return ERR_PTR(err); |
Chris Wilson | e8cb909 | 2016-08-18 17:16:53 +0100 | [diff] [blame] | 1032 | } |
Rafael Barbalho | 5032d87 | 2013-08-21 17:10:51 +0100 | [diff] [blame] | 1033 | |
Chris Wilson | e8cb909 | 2016-08-18 17:16:53 +0100 | [diff] [blame] | 1034 | cache->node.start = vma->node.start; |
| 1035 | cache->node.mm = (void *)vma; |
Chris Wilson | d50415c | 2016-08-18 17:16:52 +0100 | [diff] [blame] | 1036 | } |
Ben Widawsky | 3c94cee | 2013-11-02 21:07:11 -0700 | [diff] [blame] | 1037 | } |
| 1038 | |
Chris Wilson | e8cb909 | 2016-08-18 17:16:53 +0100 | [diff] [blame] | 1039 | offset = cache->node.start; |
| 1040 | if (cache->node.allocated) { |
Chris Wilson | fc09909 | 2016-10-28 15:27:56 +0100 | [diff] [blame] | 1041 | wmb(); |
Chris Wilson | e8cb909 | 2016-08-18 17:16:53 +0100 | [diff] [blame] | 1042 | ggtt->base.insert_page(&ggtt->base, |
| 1043 | i915_gem_object_get_dma_address(obj, page), |
| 1044 | offset, I915_CACHE_NONE, 0); |
| 1045 | } else { |
| 1046 | offset += page << PAGE_SHIFT; |
| 1047 | } |
| 1048 | |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 1049 | vaddr = (void __force *)io_mapping_map_atomic_wc(&ggtt->mappable, |
| 1050 | offset); |
Chris Wilson | d50415c | 2016-08-18 17:16:52 +0100 | [diff] [blame] | 1051 | cache->page = page; |
| 1052 | cache->vaddr = (unsigned long)vaddr; |
| 1053 | |
| 1054 | return vaddr; |
Rafael Barbalho | 5032d87 | 2013-08-21 17:10:51 +0100 | [diff] [blame] | 1055 | } |
| 1056 | |
Chris Wilson | d50415c | 2016-08-18 17:16:52 +0100 | [diff] [blame] | 1057 | static void *reloc_vaddr(struct drm_i915_gem_object *obj, |
| 1058 | struct reloc_cache *cache, |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1059 | unsigned long page) |
Chris Wilson | edf4427b | 2015-01-14 11:20:56 +0000 | [diff] [blame] | 1060 | { |
Chris Wilson | d50415c | 2016-08-18 17:16:52 +0100 | [diff] [blame] | 1061 | void *vaddr; |
| 1062 | |
| 1063 | if (cache->page == page) { |
| 1064 | vaddr = unmask_page(cache->vaddr); |
| 1065 | } else { |
| 1066 | vaddr = NULL; |
| 1067 | if ((cache->vaddr & KMAP) == 0) |
| 1068 | vaddr = reloc_iomap(obj, cache, page); |
| 1069 | if (!vaddr) |
| 1070 | vaddr = reloc_kmap(obj, cache, page); |
| 1071 | } |
| 1072 | |
| 1073 | return vaddr; |
| 1074 | } |
| 1075 | |
| 1076 | static void clflush_write32(u32 *addr, u32 value, unsigned int flushes) |
| 1077 | { |
| 1078 | if (unlikely(flushes & (CLFLUSH_BEFORE | CLFLUSH_AFTER))) { |
| 1079 | if (flushes & CLFLUSH_BEFORE) { |
| 1080 | clflushopt(addr); |
| 1081 | mb(); |
| 1082 | } |
| 1083 | |
| 1084 | *addr = value; |
| 1085 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1086 | /* |
| 1087 | * Writes to the same cacheline are serialised by the CPU |
Chris Wilson | d50415c | 2016-08-18 17:16:52 +0100 | [diff] [blame] | 1088 | * (including clflush). On the write path, we only require |
| 1089 | * that it hits memory in an orderly fashion and place |
| 1090 | * mb barriers at the start and end of the relocation phase |
| 1091 | * to ensure ordering of clflush wrt to the system. |
| 1092 | */ |
| 1093 | if (flushes & CLFLUSH_AFTER) |
| 1094 | clflushopt(addr); |
| 1095 | } else |
| 1096 | *addr = value; |
Chris Wilson | edf4427b | 2015-01-14 11:20:56 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1099 | static u64 |
| 1100 | relocate_entry(struct i915_vma *vma, |
Chris Wilson | d50415c | 2016-08-18 17:16:52 +0100 | [diff] [blame] | 1101 | const struct drm_i915_gem_relocation_entry *reloc, |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1102 | struct i915_execbuffer *eb, |
| 1103 | const struct i915_vma *target) |
Chris Wilson | edf4427b | 2015-01-14 11:20:56 +0000 | [diff] [blame] | 1104 | { |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1105 | struct drm_i915_gem_object *obj = vma->obj; |
Chris Wilson | d50415c | 2016-08-18 17:16:52 +0100 | [diff] [blame] | 1106 | u64 offset = reloc->offset; |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1107 | u64 target_offset = relocation_target(reloc, target); |
| 1108 | bool wide = eb->reloc_cache.use_64bit_reloc; |
Chris Wilson | d50415c | 2016-08-18 17:16:52 +0100 | [diff] [blame] | 1109 | void *vaddr; |
Chris Wilson | edf4427b | 2015-01-14 11:20:56 +0000 | [diff] [blame] | 1110 | |
Chris Wilson | d50415c | 2016-08-18 17:16:52 +0100 | [diff] [blame] | 1111 | repeat: |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1112 | vaddr = reloc_vaddr(obj, &eb->reloc_cache, offset >> PAGE_SHIFT); |
Chris Wilson | d50415c | 2016-08-18 17:16:52 +0100 | [diff] [blame] | 1113 | if (IS_ERR(vaddr)) |
| 1114 | return PTR_ERR(vaddr); |
Chris Wilson | edf4427b | 2015-01-14 11:20:56 +0000 | [diff] [blame] | 1115 | |
Chris Wilson | d50415c | 2016-08-18 17:16:52 +0100 | [diff] [blame] | 1116 | clflush_write32(vaddr + offset_in_page(offset), |
| 1117 | lower_32_bits(target_offset), |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1118 | eb->reloc_cache.vaddr); |
Chris Wilson | edf4427b | 2015-01-14 11:20:56 +0000 | [diff] [blame] | 1119 | |
Chris Wilson | d50415c | 2016-08-18 17:16:52 +0100 | [diff] [blame] | 1120 | if (wide) { |
| 1121 | offset += sizeof(u32); |
| 1122 | target_offset >>= 32; |
| 1123 | wide = false; |
| 1124 | goto repeat; |
Chris Wilson | edf4427b | 2015-01-14 11:20:56 +0000 | [diff] [blame] | 1125 | } |
Chris Wilson | dabdfe0 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 1126 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1127 | return target->node.start | UPDATE; |
Rafael Barbalho | 5032d87 | 2013-08-21 17:10:51 +0100 | [diff] [blame] | 1128 | } |
| 1129 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1130 | static u64 |
| 1131 | eb_relocate_entry(struct i915_execbuffer *eb, |
| 1132 | struct i915_vma *vma, |
| 1133 | const struct drm_i915_gem_relocation_entry *reloc) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1134 | { |
Chris Wilson | 507d977 | 2017-06-16 15:05:17 +0100 | [diff] [blame] | 1135 | struct i915_vma *target; |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1136 | int err; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1137 | |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 1138 | /* we've already hold a reference to all valid objects */ |
Chris Wilson | 507d977 | 2017-06-16 15:05:17 +0100 | [diff] [blame] | 1139 | target = eb_get_vma(eb, reloc->target_handle); |
| 1140 | if (unlikely(!target)) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1141 | return -ENOENT; |
Eric Anholt | e844b99 | 2012-07-31 15:35:01 -0700 | [diff] [blame] | 1142 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1143 | /* Validate that the target is in a valid r/w GPU domain */ |
Chris Wilson | b8f7ab1 | 2010-12-08 10:43:06 +0000 | [diff] [blame] | 1144 | if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1145 | DRM_DEBUG("reloc with multiple write domains: " |
Chris Wilson | 507d977 | 2017-06-16 15:05:17 +0100 | [diff] [blame] | 1146 | "target %d offset %d " |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1147 | "read %08x write %08x", |
Chris Wilson | 507d977 | 2017-06-16 15:05:17 +0100 | [diff] [blame] | 1148 | reloc->target_handle, |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1149 | (int) reloc->offset, |
| 1150 | reloc->read_domains, |
| 1151 | reloc->write_domain); |
Ben Widawsky | 8b78f0e | 2013-12-26 13:39:50 -0800 | [diff] [blame] | 1152 | return -EINVAL; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1153 | } |
Daniel Vetter | 4ca4a25 | 2011-12-14 13:57:27 +0100 | [diff] [blame] | 1154 | if (unlikely((reloc->write_domain | reloc->read_domains) |
| 1155 | & ~I915_GEM_GPU_DOMAINS)) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1156 | DRM_DEBUG("reloc with read/write non-GPU domains: " |
Chris Wilson | 507d977 | 2017-06-16 15:05:17 +0100 | [diff] [blame] | 1157 | "target %d offset %d " |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1158 | "read %08x write %08x", |
Chris Wilson | 507d977 | 2017-06-16 15:05:17 +0100 | [diff] [blame] | 1159 | reloc->target_handle, |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1160 | (int) reloc->offset, |
| 1161 | reloc->read_domains, |
| 1162 | reloc->write_domain); |
Ben Widawsky | 8b78f0e | 2013-12-26 13:39:50 -0800 | [diff] [blame] | 1163 | return -EINVAL; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1164 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1165 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1166 | if (reloc->write_domain) { |
Chris Wilson | 507d977 | 2017-06-16 15:05:17 +0100 | [diff] [blame] | 1167 | target->exec_entry->flags |= EXEC_OBJECT_WRITE; |
| 1168 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1169 | /* |
| 1170 | * Sandybridge PPGTT errata: We need a global gtt mapping |
| 1171 | * for MI and pipe_control writes because the gpu doesn't |
| 1172 | * properly redirect them through the ppgtt for non_secure |
| 1173 | * batchbuffers. |
| 1174 | */ |
| 1175 | if (reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION && |
| 1176 | IS_GEN6(eb->i915)) { |
| 1177 | err = i915_vma_bind(target, target->obj->cache_level, |
| 1178 | PIN_GLOBAL); |
| 1179 | if (WARN_ONCE(err, |
| 1180 | "Unexpected failure to bind target VMA!")) |
| 1181 | return err; |
| 1182 | } |
Chris Wilson | 507d977 | 2017-06-16 15:05:17 +0100 | [diff] [blame] | 1183 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1184 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1185 | /* |
| 1186 | * If the relocation already has the right value in it, no |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1187 | * more work needs to be done. |
| 1188 | */ |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1189 | if (gen8_canonical_addr(target->node.start) == reloc->presumed_offset) |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 1190 | return 0; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1191 | |
| 1192 | /* Check that the relocation address is valid... */ |
Ben Widawsky | 3c94cee | 2013-11-02 21:07:11 -0700 | [diff] [blame] | 1193 | if (unlikely(reloc->offset > |
Chris Wilson | 507d977 | 2017-06-16 15:05:17 +0100 | [diff] [blame] | 1194 | vma->size - (eb->reloc_cache.use_64bit_reloc ? 8 : 4))) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1195 | DRM_DEBUG("Relocation beyond object bounds: " |
Chris Wilson | 507d977 | 2017-06-16 15:05:17 +0100 | [diff] [blame] | 1196 | "target %d offset %d size %d.\n", |
| 1197 | reloc->target_handle, |
| 1198 | (int)reloc->offset, |
| 1199 | (int)vma->size); |
Ben Widawsky | 8b78f0e | 2013-12-26 13:39:50 -0800 | [diff] [blame] | 1200 | return -EINVAL; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1201 | } |
Chris Wilson | b8f7ab1 | 2010-12-08 10:43:06 +0000 | [diff] [blame] | 1202 | if (unlikely(reloc->offset & 3)) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1203 | DRM_DEBUG("Relocation not 4-byte aligned: " |
Chris Wilson | 507d977 | 2017-06-16 15:05:17 +0100 | [diff] [blame] | 1204 | "target %d offset %d.\n", |
| 1205 | reloc->target_handle, |
| 1206 | (int)reloc->offset); |
Ben Widawsky | 8b78f0e | 2013-12-26 13:39:50 -0800 | [diff] [blame] | 1207 | return -EINVAL; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
Chris Wilson | 071750e | 2017-06-16 15:05:18 +0100 | [diff] [blame] | 1210 | /* |
| 1211 | * If we write into the object, we need to force the synchronisation |
| 1212 | * barrier, either with an asynchronous clflush or if we executed the |
| 1213 | * patching using the GPU (though that should be serialised by the |
| 1214 | * timeline). To be completely sure, and since we are required to |
| 1215 | * do relocations we are already stalling, disable the user's opt |
| 1216 | * of our synchronisation. |
| 1217 | */ |
| 1218 | vma->exec_entry->flags &= ~EXEC_OBJECT_ASYNC; |
| 1219 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1220 | /* and update the user's relocation entry */ |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1221 | return relocate_entry(vma, reloc, eb, target); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1224 | static int eb_relocate_vma(struct i915_execbuffer *eb, struct i915_vma *vma) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1225 | { |
Chris Wilson | 1d83f44 | 2012-03-24 20:12:53 +0000 | [diff] [blame] | 1226 | #define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry)) |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1227 | struct drm_i915_gem_relocation_entry stack[N_RELOC(512)]; |
| 1228 | struct drm_i915_gem_relocation_entry __user *urelocs; |
| 1229 | const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry; |
| 1230 | unsigned int remain; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1231 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1232 | urelocs = u64_to_user_ptr(entry->relocs_ptr); |
Chris Wilson | 1d83f44 | 2012-03-24 20:12:53 +0000 | [diff] [blame] | 1233 | remain = entry->relocation_count; |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1234 | if (unlikely(remain > N_RELOC(ULONG_MAX))) |
| 1235 | return -EINVAL; |
Chris Wilson | ebc0808 | 2016-10-18 13:02:51 +0100 | [diff] [blame] | 1236 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1237 | /* |
| 1238 | * We must check that the entire relocation array is safe |
| 1239 | * to read. However, if the array is not writable the user loses |
| 1240 | * the updated relocation values. |
| 1241 | */ |
| 1242 | if (unlikely(!access_ok(VERIFY_READ, urelocs, remain*sizeof(urelocs)))) |
| 1243 | return -EFAULT; |
Chris Wilson | 1d83f44 | 2012-03-24 20:12:53 +0000 | [diff] [blame] | 1244 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1245 | do { |
| 1246 | struct drm_i915_gem_relocation_entry *r = stack; |
| 1247 | unsigned int count = |
| 1248 | min_t(unsigned int, remain, ARRAY_SIZE(stack)); |
| 1249 | unsigned int copied; |
| 1250 | |
| 1251 | /* |
| 1252 | * This is the fast path and we cannot handle a pagefault |
Chris Wilson | ebc0808 | 2016-10-18 13:02:51 +0100 | [diff] [blame] | 1253 | * whilst holding the struct mutex lest the user pass in the |
| 1254 | * relocations contained within a mmaped bo. For in such a case |
| 1255 | * we, the page fault handler would call i915_gem_fault() and |
| 1256 | * we would try to acquire the struct mutex again. Obviously |
| 1257 | * this is bad and so lockdep complains vehemently. |
| 1258 | */ |
| 1259 | pagefault_disable(); |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1260 | copied = __copy_from_user_inatomic(r, urelocs, count * sizeof(r[0])); |
Chris Wilson | ebc0808 | 2016-10-18 13:02:51 +0100 | [diff] [blame] | 1261 | pagefault_enable(); |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1262 | if (unlikely(copied)) { |
| 1263 | remain = -EFAULT; |
Chris Wilson | 31a3920 | 2016-08-18 17:16:46 +0100 | [diff] [blame] | 1264 | goto out; |
| 1265 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1266 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1267 | remain -= count; |
Chris Wilson | 1d83f44 | 2012-03-24 20:12:53 +0000 | [diff] [blame] | 1268 | do { |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1269 | u64 offset = eb_relocate_entry(eb, vma, r); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1270 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1271 | if (likely(offset == 0)) { |
| 1272 | } else if ((s64)offset < 0) { |
| 1273 | remain = (int)offset; |
Chris Wilson | 31a3920 | 2016-08-18 17:16:46 +0100 | [diff] [blame] | 1274 | goto out; |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1275 | } else { |
| 1276 | /* |
| 1277 | * Note that reporting an error now |
| 1278 | * leaves everything in an inconsistent |
| 1279 | * state as we have *already* changed |
| 1280 | * the relocation value inside the |
| 1281 | * object. As we have not changed the |
| 1282 | * reloc.presumed_offset or will not |
| 1283 | * change the execobject.offset, on the |
| 1284 | * call we may not rewrite the value |
| 1285 | * inside the object, leaving it |
| 1286 | * dangling and causing a GPU hang. Unless |
| 1287 | * userspace dynamically rebuilds the |
| 1288 | * relocations on each execbuf rather than |
| 1289 | * presume a static tree. |
| 1290 | * |
| 1291 | * We did previously check if the relocations |
| 1292 | * were writable (access_ok), an error now |
| 1293 | * would be a strange race with mprotect, |
| 1294 | * having already demonstrated that we |
| 1295 | * can read from this userspace address. |
| 1296 | */ |
| 1297 | offset = gen8_canonical_addr(offset & ~UPDATE); |
| 1298 | __put_user(offset, |
| 1299 | &urelocs[r-stack].presumed_offset); |
Chris Wilson | 1d83f44 | 2012-03-24 20:12:53 +0000 | [diff] [blame] | 1300 | } |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1301 | } while (r++, --count); |
| 1302 | urelocs += ARRAY_SIZE(stack); |
| 1303 | } while (remain); |
Chris Wilson | 31a3920 | 2016-08-18 17:16:46 +0100 | [diff] [blame] | 1304 | out: |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 1305 | reloc_cache_reset(&eb->reloc_cache); |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1306 | return remain; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | static int |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1310 | eb_relocate_vma_slow(struct i915_execbuffer *eb, struct i915_vma *vma) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1311 | { |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 1312 | const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry; |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1313 | struct drm_i915_gem_relocation_entry *relocs = |
| 1314 | u64_to_ptr(typeof(*relocs), entry->relocs_ptr); |
| 1315 | unsigned int i; |
| 1316 | int err; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1317 | |
| 1318 | for (i = 0; i < entry->relocation_count; i++) { |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1319 | u64 offset = eb_relocate_entry(eb, vma, &relocs[i]); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1320 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1321 | if ((s64)offset < 0) { |
| 1322 | err = (int)offset; |
| 1323 | goto err; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1324 | } |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1325 | } |
| 1326 | err = 0; |
Chris Wilson | a415d35 | 2013-11-26 11:23:15 +0000 | [diff] [blame] | 1327 | err: |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1328 | reloc_cache_reset(&eb->reloc_cache); |
| 1329 | return err; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1332 | static int check_relocations(const struct drm_i915_gem_exec_object2 *entry) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1333 | { |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1334 | const char __user *addr, *end; |
| 1335 | unsigned long size; |
| 1336 | char __maybe_unused c; |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 1337 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1338 | size = entry->relocation_count; |
| 1339 | if (size == 0) |
| 1340 | return 0; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1341 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1342 | if (size > N_RELOC(ULONG_MAX)) |
| 1343 | return -EINVAL; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1344 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1345 | addr = u64_to_user_ptr(entry->relocs_ptr); |
| 1346 | size *= sizeof(struct drm_i915_gem_relocation_entry); |
| 1347 | if (!access_ok(VERIFY_READ, addr, size)) |
| 1348 | return -EFAULT; |
| 1349 | |
| 1350 | end = addr + size; |
| 1351 | for (; addr < end; addr += PAGE_SIZE) { |
| 1352 | int err = __get_user(c, addr); |
| 1353 | if (err) |
| 1354 | return err; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1355 | } |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1356 | return __get_user(c, end - 1); |
| 1357 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1358 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1359 | static int eb_copy_relocations(const struct i915_execbuffer *eb) |
| 1360 | { |
| 1361 | const unsigned int count = eb->buffer_count; |
| 1362 | unsigned int i; |
| 1363 | int err; |
| 1364 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1365 | for (i = 0; i < count; i++) { |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1366 | const unsigned int nreloc = eb->exec[i].relocation_count; |
| 1367 | struct drm_i915_gem_relocation_entry __user *urelocs; |
| 1368 | struct drm_i915_gem_relocation_entry *relocs; |
| 1369 | unsigned long size; |
| 1370 | unsigned long copied; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1371 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1372 | if (nreloc == 0) |
| 1373 | continue; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1374 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1375 | err = check_relocations(&eb->exec[i]); |
| 1376 | if (err) |
| 1377 | goto err; |
| 1378 | |
| 1379 | urelocs = u64_to_user_ptr(eb->exec[i].relocs_ptr); |
| 1380 | size = nreloc * sizeof(*relocs); |
| 1381 | |
| 1382 | relocs = kvmalloc_array(size, 1, GFP_TEMPORARY); |
| 1383 | if (!relocs) { |
| 1384 | kvfree(relocs); |
| 1385 | err = -ENOMEM; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1386 | goto err; |
| 1387 | } |
| 1388 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1389 | /* copy_from_user is limited to < 4GiB */ |
| 1390 | copied = 0; |
| 1391 | do { |
| 1392 | unsigned int len = |
| 1393 | min_t(u64, BIT_ULL(31), size - copied); |
| 1394 | |
| 1395 | if (__copy_from_user((char *)relocs + copied, |
| 1396 | (char *)urelocs + copied, |
| 1397 | len)) { |
| 1398 | kvfree(relocs); |
| 1399 | err = -EFAULT; |
| 1400 | goto err; |
| 1401 | } |
| 1402 | |
| 1403 | copied += len; |
| 1404 | } while (copied < size); |
| 1405 | |
| 1406 | /* |
| 1407 | * As we do not update the known relocation offsets after |
Chris Wilson | 262b6d3 | 2013-01-15 16:17:54 +0000 | [diff] [blame] | 1408 | * relocating (due to the complexities in lock handling), |
| 1409 | * we need to mark them as invalid now so that we force the |
| 1410 | * relocation processing next time. Just in case the target |
| 1411 | * object is evicted and then rebound into its old |
| 1412 | * presumed_offset before the next execbuffer - if that |
| 1413 | * happened we would make the mistake of assuming that the |
| 1414 | * relocations were valid. |
| 1415 | */ |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1416 | user_access_begin(); |
| 1417 | for (copied = 0; copied < nreloc; copied++) |
| 1418 | unsafe_put_user(-1, |
| 1419 | &urelocs[copied].presumed_offset, |
| 1420 | end_user); |
| 1421 | end_user: |
| 1422 | user_access_end(); |
Chris Wilson | 262b6d3 | 2013-01-15 16:17:54 +0000 | [diff] [blame] | 1423 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1424 | eb->exec[i].relocs_ptr = (uintptr_t)relocs; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1427 | return 0; |
| 1428 | |
| 1429 | err: |
| 1430 | while (i--) { |
| 1431 | struct drm_i915_gem_relocation_entry *relocs = |
| 1432 | u64_to_ptr(typeof(*relocs), eb->exec[i].relocs_ptr); |
| 1433 | if (eb->exec[i].relocation_count) |
| 1434 | kvfree(relocs); |
| 1435 | } |
| 1436 | return err; |
| 1437 | } |
| 1438 | |
| 1439 | static int eb_prefault_relocations(const struct i915_execbuffer *eb) |
| 1440 | { |
| 1441 | const unsigned int count = eb->buffer_count; |
| 1442 | unsigned int i; |
| 1443 | |
| 1444 | if (unlikely(i915.prefault_disable)) |
| 1445 | return 0; |
| 1446 | |
| 1447 | for (i = 0; i < count; i++) { |
| 1448 | int err; |
| 1449 | |
| 1450 | err = check_relocations(&eb->exec[i]); |
| 1451 | if (err) |
| 1452 | return err; |
| 1453 | } |
| 1454 | |
| 1455 | return 0; |
| 1456 | } |
| 1457 | |
| 1458 | static noinline int eb_relocate_slow(struct i915_execbuffer *eb) |
| 1459 | { |
| 1460 | struct drm_device *dev = &eb->i915->drm; |
| 1461 | bool have_copy = false; |
| 1462 | struct i915_vma *vma; |
| 1463 | int err = 0; |
| 1464 | |
| 1465 | repeat: |
| 1466 | if (signal_pending(current)) { |
| 1467 | err = -ERESTARTSYS; |
| 1468 | goto out; |
| 1469 | } |
| 1470 | |
| 1471 | /* We may process another execbuffer during the unlock... */ |
| 1472 | eb_reset_vmas(eb); |
| 1473 | mutex_unlock(&dev->struct_mutex); |
| 1474 | |
| 1475 | /* |
| 1476 | * We take 3 passes through the slowpatch. |
| 1477 | * |
| 1478 | * 1 - we try to just prefault all the user relocation entries and |
| 1479 | * then attempt to reuse the atomic pagefault disabled fast path again. |
| 1480 | * |
| 1481 | * 2 - we copy the user entries to a local buffer here outside of the |
| 1482 | * local and allow ourselves to wait upon any rendering before |
| 1483 | * relocations |
| 1484 | * |
| 1485 | * 3 - we already have a local copy of the relocation entries, but |
| 1486 | * were interrupted (EAGAIN) whilst waiting for the objects, try again. |
| 1487 | */ |
| 1488 | if (!err) { |
| 1489 | err = eb_prefault_relocations(eb); |
| 1490 | } else if (!have_copy) { |
| 1491 | err = eb_copy_relocations(eb); |
| 1492 | have_copy = err == 0; |
| 1493 | } else { |
| 1494 | cond_resched(); |
| 1495 | err = 0; |
| 1496 | } |
| 1497 | if (err) { |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1498 | mutex_lock(&dev->struct_mutex); |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1499 | goto out; |
| 1500 | } |
| 1501 | |
Chris Wilson | 8a2421b | 2017-06-16 15:05:22 +0100 | [diff] [blame^] | 1502 | /* A frequent cause for EAGAIN are currently unavailable client pages */ |
| 1503 | flush_workqueue(eb->i915->mm.userptr_wq); |
| 1504 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1505 | err = i915_mutex_lock_interruptible(dev); |
| 1506 | if (err) { |
| 1507 | mutex_lock(&dev->struct_mutex); |
| 1508 | goto out; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 1511 | /* reacquire the objects */ |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1512 | err = eb_lookup_vmas(eb); |
| 1513 | if (err) |
Chris Wilson | 3b96eff | 2013-01-08 10:53:14 +0000 | [diff] [blame] | 1514 | goto err; |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 1515 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1516 | list_for_each_entry(vma, &eb->relocs, reloc_link) { |
| 1517 | if (!have_copy) { |
| 1518 | pagefault_disable(); |
| 1519 | err = eb_relocate_vma(eb, vma); |
| 1520 | pagefault_enable(); |
| 1521 | if (err) |
| 1522 | goto repeat; |
| 1523 | } else { |
| 1524 | err = eb_relocate_vma_slow(eb, vma); |
| 1525 | if (err) |
| 1526 | goto err; |
| 1527 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1530 | /* |
| 1531 | * Leave the user relocations as are, this is the painfully slow path, |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1532 | * and we want to avoid the complication of dropping the lock whilst |
| 1533 | * having buffers reserved in the aperture and so causing spurious |
| 1534 | * ENOSPC for random operations. |
| 1535 | */ |
| 1536 | |
| 1537 | err: |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1538 | if (err == -EAGAIN) |
| 1539 | goto repeat; |
| 1540 | |
| 1541 | out: |
| 1542 | if (have_copy) { |
| 1543 | const unsigned int count = eb->buffer_count; |
| 1544 | unsigned int i; |
| 1545 | |
| 1546 | for (i = 0; i < count; i++) { |
| 1547 | const struct drm_i915_gem_exec_object2 *entry = |
| 1548 | &eb->exec[i]; |
| 1549 | struct drm_i915_gem_relocation_entry *relocs; |
| 1550 | |
| 1551 | if (!entry->relocation_count) |
| 1552 | continue; |
| 1553 | |
| 1554 | relocs = u64_to_ptr(typeof(*relocs), entry->relocs_ptr); |
| 1555 | kvfree(relocs); |
| 1556 | } |
| 1557 | } |
| 1558 | |
| 1559 | return err ?: have_copy; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1560 | } |
| 1561 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1562 | static int eb_relocate(struct i915_execbuffer *eb) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1563 | { |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1564 | if (eb_lookup_vmas(eb)) |
| 1565 | goto slow; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1566 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1567 | /* The objects are in their final locations, apply the relocations. */ |
| 1568 | if (eb->args->flags & __EXEC_HAS_RELOC) { |
| 1569 | struct i915_vma *vma; |
| 1570 | |
| 1571 | list_for_each_entry(vma, &eb->relocs, reloc_link) { |
| 1572 | if (eb_relocate_vma(eb, vma)) |
| 1573 | goto slow; |
| 1574 | } |
| 1575 | } |
| 1576 | |
| 1577 | return 0; |
| 1578 | |
| 1579 | slow: |
| 1580 | return eb_relocate_slow(eb); |
| 1581 | } |
| 1582 | |
| 1583 | static void eb_export_fence(struct drm_i915_gem_object *obj, |
| 1584 | struct drm_i915_gem_request *req, |
| 1585 | unsigned int flags) |
| 1586 | { |
| 1587 | struct reservation_object *resv = obj->resv; |
| 1588 | |
| 1589 | /* |
| 1590 | * Ignore errors from failing to allocate the new fence, we can't |
| 1591 | * handle an error right now. Worst case should be missed |
| 1592 | * synchronisation leading to rendering corruption. |
| 1593 | */ |
| 1594 | reservation_object_lock(resv, NULL); |
| 1595 | if (flags & EXEC_OBJECT_WRITE) |
| 1596 | reservation_object_add_excl_fence(resv, &req->fence); |
| 1597 | else if (reservation_object_reserve_shared(resv) == 0) |
| 1598 | reservation_object_add_shared_fence(resv, &req->fence); |
| 1599 | reservation_object_unlock(resv); |
| 1600 | } |
| 1601 | |
| 1602 | static int eb_move_to_gpu(struct i915_execbuffer *eb) |
| 1603 | { |
| 1604 | const unsigned int count = eb->buffer_count; |
| 1605 | unsigned int i; |
| 1606 | int err; |
| 1607 | |
| 1608 | for (i = 0; i < count; i++) { |
| 1609 | const struct drm_i915_gem_exec_object2 *entry = &eb->exec[i]; |
| 1610 | struct i915_vma *vma = exec_to_vma(entry); |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 1611 | struct drm_i915_gem_object *obj = vma->obj; |
Chris Wilson | 03ade51 | 2015-04-27 13:41:18 +0100 | [diff] [blame] | 1612 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1613 | if (entry->flags & EXEC_OBJECT_CAPTURE) { |
Chris Wilson | b0fd47a | 2017-04-15 10:39:02 +0100 | [diff] [blame] | 1614 | struct i915_gem_capture_list *capture; |
| 1615 | |
| 1616 | capture = kmalloc(sizeof(*capture), GFP_KERNEL); |
| 1617 | if (unlikely(!capture)) |
| 1618 | return -ENOMEM; |
| 1619 | |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 1620 | capture->next = eb->request->capture_list; |
Chris Wilson | b0fd47a | 2017-04-15 10:39:02 +0100 | [diff] [blame] | 1621 | capture->vma = vma; |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 1622 | eb->request->capture_list = capture; |
Chris Wilson | b0fd47a | 2017-04-15 10:39:02 +0100 | [diff] [blame] | 1623 | } |
| 1624 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1625 | if (entry->flags & EXEC_OBJECT_ASYNC) |
| 1626 | goto skip_flushes; |
Chris Wilson | 77ae995 | 2017-01-27 09:40:07 +0000 | [diff] [blame] | 1627 | |
Chris Wilson | 7fc92e9 | 2017-06-16 11:54:55 +0100 | [diff] [blame] | 1628 | if (unlikely(obj->cache_dirty && !obj->cache_coherent)) |
Chris Wilson | 57822dc | 2017-02-22 11:40:48 +0000 | [diff] [blame] | 1629 | i915_gem_clflush_object(obj, 0); |
Chris Wilson | 57822dc | 2017-02-22 11:40:48 +0000 | [diff] [blame] | 1630 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1631 | err = i915_gem_request_await_object |
| 1632 | (eb->request, obj, entry->flags & EXEC_OBJECT_WRITE); |
| 1633 | if (err) |
| 1634 | return err; |
| 1635 | |
| 1636 | skip_flushes: |
| 1637 | i915_vma_move_to_active(vma, eb->request, entry->flags); |
| 1638 | __eb_unreserve_vma(vma, entry); |
| 1639 | vma->exec_entry = NULL; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1642 | for (i = 0; i < count; i++) { |
| 1643 | const struct drm_i915_gem_exec_object2 *entry = &eb->exec[i]; |
| 1644 | struct i915_vma *vma = exec_to_vma(entry); |
| 1645 | |
| 1646 | eb_export_fence(vma->obj, eb->request, entry->flags); |
Chris Wilson | dade2a6 | 2017-06-16 15:05:20 +0100 | [diff] [blame] | 1647 | if (unlikely(entry->flags & __EXEC_OBJECT_HAS_REF)) |
| 1648 | i915_vma_put(vma); |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1649 | } |
| 1650 | eb->exec = NULL; |
| 1651 | |
Chris Wilson | dcd7993 | 2016-08-18 17:16:40 +0100 | [diff] [blame] | 1652 | /* Unconditionally flush any chipset caches (for streaming writes). */ |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 1653 | i915_gem_chipset_flush(eb->i915); |
Daniel Vetter | 6ac42f4 | 2012-07-21 12:25:01 +0200 | [diff] [blame] | 1654 | |
Chris Wilson | c7fe7d2 | 2016-08-02 22:50:24 +0100 | [diff] [blame] | 1655 | /* Unconditionally invalidate GPU caches and TLBs. */ |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 1656 | return eb->engine->emit_flush(eb->request, EMIT_INVALIDATE); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1657 | } |
| 1658 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1659 | static bool i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1660 | { |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 1661 | if (exec->flags & __I915_EXEC_ILLEGAL_FLAGS) |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame] | 1662 | return false; |
| 1663 | |
Chris Wilson | 2f5945b | 2015-10-06 11:39:55 +0100 | [diff] [blame] | 1664 | /* Kernel clipping was a DRI1 misfeature */ |
| 1665 | if (exec->num_cliprects || exec->cliprects_ptr) |
| 1666 | return false; |
| 1667 | |
| 1668 | if (exec->DR4 == 0xffffffff) { |
| 1669 | DRM_DEBUG("UXA submitting garbage DR4, fixing up\n"); |
| 1670 | exec->DR4 = 0; |
| 1671 | } |
| 1672 | if (exec->DR1 || exec->DR4) |
| 1673 | return false; |
| 1674 | |
| 1675 | if ((exec->batch_start_offset | exec->batch_len) & 0x7) |
| 1676 | return false; |
| 1677 | |
| 1678 | return true; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1679 | } |
| 1680 | |
Chris Wilson | 5cf3d28 | 2016-08-04 07:52:43 +0100 | [diff] [blame] | 1681 | void i915_vma_move_to_active(struct i915_vma *vma, |
| 1682 | struct drm_i915_gem_request *req, |
| 1683 | unsigned int flags) |
| 1684 | { |
| 1685 | struct drm_i915_gem_object *obj = vma->obj; |
| 1686 | const unsigned int idx = req->engine->id; |
| 1687 | |
Chris Wilson | 81147b0 | 2016-12-18 15:37:18 +0000 | [diff] [blame] | 1688 | lockdep_assert_held(&req->i915->drm.struct_mutex); |
Chris Wilson | 5cf3d28 | 2016-08-04 07:52:43 +0100 | [diff] [blame] | 1689 | GEM_BUG_ON(!drm_mm_node_allocated(&vma->node)); |
| 1690 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1691 | /* |
| 1692 | * Add a reference if we're newly entering the active list. |
Chris Wilson | b0decaf | 2016-08-04 07:52:44 +0100 | [diff] [blame] | 1693 | * The order in which we add operations to the retirement queue is |
| 1694 | * vital here: mark_active adds to the start of the callback list, |
| 1695 | * such that subsequent callbacks are called first. Therefore we |
| 1696 | * add the active reference first and queue for it to be dropped |
| 1697 | * *last*. |
| 1698 | */ |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 1699 | if (!i915_vma_is_active(vma)) |
| 1700 | obj->active_count++; |
| 1701 | i915_vma_set_active(vma, idx); |
| 1702 | i915_gem_active_set(&vma->last_read[idx], req); |
| 1703 | list_move_tail(&vma->vm_link, &vma->vm->active_list); |
Chris Wilson | 5cf3d28 | 2016-08-04 07:52:43 +0100 | [diff] [blame] | 1704 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 1705 | obj->base.write_domain = 0; |
Chris Wilson | 5cf3d28 | 2016-08-04 07:52:43 +0100 | [diff] [blame] | 1706 | if (flags & EXEC_OBJECT_WRITE) { |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 1707 | obj->base.write_domain = I915_GEM_DOMAIN_RENDER; |
| 1708 | |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 1709 | if (intel_fb_obj_invalidate(obj, ORIGIN_CS)) |
| 1710 | i915_gem_active_set(&obj->frontbuffer_write, req); |
Chris Wilson | 5cf3d28 | 2016-08-04 07:52:43 +0100 | [diff] [blame] | 1711 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 1712 | obj->base.read_domains = 0; |
Chris Wilson | 5cf3d28 | 2016-08-04 07:52:43 +0100 | [diff] [blame] | 1713 | } |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 1714 | obj->base.read_domains |= I915_GEM_GPU_DOMAINS; |
Chris Wilson | 5cf3d28 | 2016-08-04 07:52:43 +0100 | [diff] [blame] | 1715 | |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 1716 | if (flags & EXEC_OBJECT_NEEDS_FENCE) |
| 1717 | i915_gem_active_set(&vma->last_fence, req); |
Chris Wilson | 5cf3d28 | 2016-08-04 07:52:43 +0100 | [diff] [blame] | 1718 | } |
| 1719 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1720 | static int i915_reset_gen7_sol_offsets(struct drm_i915_gem_request *req) |
Eric Anholt | ae662d3 | 2012-01-03 09:23:29 -0800 | [diff] [blame] | 1721 | { |
Tvrtko Ursulin | 73dec95 | 2017-02-14 11:32:42 +0000 | [diff] [blame] | 1722 | u32 *cs; |
| 1723 | int i; |
Eric Anholt | ae662d3 | 2012-01-03 09:23:29 -0800 | [diff] [blame] | 1724 | |
Chris Wilson | b5321f3 | 2016-08-02 22:50:18 +0100 | [diff] [blame] | 1725 | if (!IS_GEN7(req->i915) || req->engine->id != RCS) { |
Daniel Vetter | 9d662da | 2014-04-24 08:09:09 +0200 | [diff] [blame] | 1726 | DRM_DEBUG("sol reset is gen7/rcs only\n"); |
| 1727 | return -EINVAL; |
| 1728 | } |
Eric Anholt | ae662d3 | 2012-01-03 09:23:29 -0800 | [diff] [blame] | 1729 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1730 | cs = intel_ring_begin(req, 4 * 2 + 2); |
Tvrtko Ursulin | 73dec95 | 2017-02-14 11:32:42 +0000 | [diff] [blame] | 1731 | if (IS_ERR(cs)) |
| 1732 | return PTR_ERR(cs); |
Eric Anholt | ae662d3 | 2012-01-03 09:23:29 -0800 | [diff] [blame] | 1733 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1734 | *cs++ = MI_LOAD_REGISTER_IMM(4); |
Eric Anholt | ae662d3 | 2012-01-03 09:23:29 -0800 | [diff] [blame] | 1735 | for (i = 0; i < 4; i++) { |
Tvrtko Ursulin | 73dec95 | 2017-02-14 11:32:42 +0000 | [diff] [blame] | 1736 | *cs++ = i915_mmio_reg_offset(GEN7_SO_WRITE_OFFSET(i)); |
| 1737 | *cs++ = 0; |
Eric Anholt | ae662d3 | 2012-01-03 09:23:29 -0800 | [diff] [blame] | 1738 | } |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1739 | *cs++ = MI_NOOP; |
Tvrtko Ursulin | 73dec95 | 2017-02-14 11:32:42 +0000 | [diff] [blame] | 1740 | intel_ring_advance(req, cs); |
Eric Anholt | ae662d3 | 2012-01-03 09:23:29 -0800 | [diff] [blame] | 1741 | |
| 1742 | return 0; |
| 1743 | } |
| 1744 | |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 1745 | static struct i915_vma *eb_parse(struct i915_execbuffer *eb, bool is_master) |
Brad Volkin | 7174537 | 2014-12-11 12:13:12 -0800 | [diff] [blame] | 1746 | { |
Brad Volkin | 7174537 | 2014-12-11 12:13:12 -0800 | [diff] [blame] | 1747 | struct drm_i915_gem_object *shadow_batch_obj; |
Chris Wilson | 17cabf5 | 2015-01-14 11:20:57 +0000 | [diff] [blame] | 1748 | struct i915_vma *vma; |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1749 | int err; |
Brad Volkin | 7174537 | 2014-12-11 12:13:12 -0800 | [diff] [blame] | 1750 | |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 1751 | shadow_batch_obj = i915_gem_batch_pool_get(&eb->engine->batch_pool, |
| 1752 | PAGE_ALIGN(eb->batch_len)); |
Brad Volkin | 7174537 | 2014-12-11 12:13:12 -0800 | [diff] [blame] | 1753 | if (IS_ERR(shadow_batch_obj)) |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 1754 | return ERR_CAST(shadow_batch_obj); |
Brad Volkin | 7174537 | 2014-12-11 12:13:12 -0800 | [diff] [blame] | 1755 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1756 | err = intel_engine_cmd_parser(eb->engine, |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 1757 | eb->batch->obj, |
Chris Wilson | 33a051a | 2016-07-27 09:07:26 +0100 | [diff] [blame] | 1758 | shadow_batch_obj, |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 1759 | eb->batch_start_offset, |
| 1760 | eb->batch_len, |
Chris Wilson | 33a051a | 2016-07-27 09:07:26 +0100 | [diff] [blame] | 1761 | is_master); |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1762 | if (err) { |
| 1763 | if (err == -EACCES) /* unhandled chained batch */ |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1764 | vma = NULL; |
| 1765 | else |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1766 | vma = ERR_PTR(err); |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1767 | goto out; |
| 1768 | } |
Brad Volkin | 7174537 | 2014-12-11 12:13:12 -0800 | [diff] [blame] | 1769 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1770 | vma = i915_gem_object_ggtt_pin(shadow_batch_obj, NULL, 0, 0, 0); |
| 1771 | if (IS_ERR(vma)) |
| 1772 | goto out; |
Chris Wilson | de4e783 | 2015-04-07 16:20:35 +0100 | [diff] [blame] | 1773 | |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 1774 | vma->exec_entry = |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1775 | memset(&eb->exec[eb->buffer_count++], |
| 1776 | 0, sizeof(*vma->exec_entry)); |
Chris Wilson | dade2a6 | 2017-06-16 15:05:20 +0100 | [diff] [blame] | 1777 | vma->exec_entry->flags = __EXEC_OBJECT_HAS_PIN | __EXEC_OBJECT_HAS_REF; |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1778 | __exec_to_vma(vma->exec_entry) = (uintptr_t)i915_vma_get(vma); |
Brad Volkin | 7174537 | 2014-12-11 12:13:12 -0800 | [diff] [blame] | 1779 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1780 | out: |
Chris Wilson | de4e783 | 2015-04-07 16:20:35 +0100 | [diff] [blame] | 1781 | i915_gem_object_unpin_pages(shadow_batch_obj); |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1782 | return vma; |
Brad Volkin | 7174537 | 2014-12-11 12:13:12 -0800 | [diff] [blame] | 1783 | } |
Chris Wilson | 5c6c600 | 2014-09-06 10:28:27 +0100 | [diff] [blame] | 1784 | |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 1785 | static void |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1786 | add_to_client(struct drm_i915_gem_request *req, struct drm_file *file) |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 1787 | { |
| 1788 | req->file_priv = file->driver_priv; |
| 1789 | list_add_tail(&req->client_link, &req->file_priv->mm.request_list); |
| 1790 | } |
| 1791 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1792 | static int eb_submit(struct i915_execbuffer *eb) |
Oscar Mateo | 7838259 | 2014-07-03 16:28:05 +0100 | [diff] [blame] | 1793 | { |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1794 | int err; |
Oscar Mateo | 7838259 | 2014-07-03 16:28:05 +0100 | [diff] [blame] | 1795 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1796 | err = eb_move_to_gpu(eb); |
| 1797 | if (err) |
| 1798 | return err; |
Oscar Mateo | 7838259 | 2014-07-03 16:28:05 +0100 | [diff] [blame] | 1799 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1800 | err = i915_switch_context(eb->request); |
| 1801 | if (err) |
| 1802 | return err; |
Oscar Mateo | 7838259 | 2014-07-03 16:28:05 +0100 | [diff] [blame] | 1803 | |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 1804 | if (eb->args->flags & I915_EXEC_GEN7_SOL_RESET) { |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1805 | err = i915_reset_gen7_sol_offsets(eb->request); |
| 1806 | if (err) |
| 1807 | return err; |
Oscar Mateo | 7838259 | 2014-07-03 16:28:05 +0100 | [diff] [blame] | 1808 | } |
| 1809 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1810 | err = eb->engine->emit_bb_start(eb->request, |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 1811 | eb->batch->node.start + |
| 1812 | eb->batch_start_offset, |
| 1813 | eb->batch_len, |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1814 | eb->batch_flags); |
| 1815 | if (err) |
| 1816 | return err; |
Oscar Mateo | 7838259 | 2014-07-03 16:28:05 +0100 | [diff] [blame] | 1817 | |
Chris Wilson | 2f5945b | 2015-10-06 11:39:55 +0100 | [diff] [blame] | 1818 | return 0; |
Oscar Mateo | 7838259 | 2014-07-03 16:28:05 +0100 | [diff] [blame] | 1819 | } |
| 1820 | |
Zhao Yakui | a8ebba7 | 2014-04-17 10:37:40 +0800 | [diff] [blame] | 1821 | /** |
| 1822 | * Find one BSD ring to dispatch the corresponding BSD command. |
Chris Wilson | c80ff16 | 2016-07-27 09:07:27 +0100 | [diff] [blame] | 1823 | * The engine index is returned. |
Zhao Yakui | a8ebba7 | 2014-04-17 10:37:40 +0800 | [diff] [blame] | 1824 | */ |
Tvrtko Ursulin | de1add3 | 2016-01-15 15:12:50 +0000 | [diff] [blame] | 1825 | static unsigned int |
Chris Wilson | c80ff16 | 2016-07-27 09:07:27 +0100 | [diff] [blame] | 1826 | gen8_dispatch_bsd_engine(struct drm_i915_private *dev_priv, |
| 1827 | struct drm_file *file) |
Zhao Yakui | a8ebba7 | 2014-04-17 10:37:40 +0800 | [diff] [blame] | 1828 | { |
Zhao Yakui | a8ebba7 | 2014-04-17 10:37:40 +0800 | [diff] [blame] | 1829 | struct drm_i915_file_private *file_priv = file->driver_priv; |
| 1830 | |
Tvrtko Ursulin | de1add3 | 2016-01-15 15:12:50 +0000 | [diff] [blame] | 1831 | /* Check whether the file_priv has already selected one ring. */ |
Joonas Lahtinen | 6f63340 | 2016-09-01 14:58:21 +0300 | [diff] [blame] | 1832 | if ((int)file_priv->bsd_engine < 0) |
| 1833 | file_priv->bsd_engine = atomic_fetch_xor(1, |
| 1834 | &dev_priv->mm.bsd_engine_dispatch_index); |
Tvrtko Ursulin | de1add3 | 2016-01-15 15:12:50 +0000 | [diff] [blame] | 1835 | |
Chris Wilson | c80ff16 | 2016-07-27 09:07:27 +0100 | [diff] [blame] | 1836 | return file_priv->bsd_engine; |
Chris Wilson | d23db88 | 2014-05-23 08:48:08 +0200 | [diff] [blame] | 1837 | } |
| 1838 | |
Tvrtko Ursulin | de1add3 | 2016-01-15 15:12:50 +0000 | [diff] [blame] | 1839 | #define I915_USER_RINGS (4) |
| 1840 | |
Tvrtko Ursulin | 117897f | 2016-03-16 11:00:40 +0000 | [diff] [blame] | 1841 | static const enum intel_engine_id user_ring_map[I915_USER_RINGS + 1] = { |
Tvrtko Ursulin | de1add3 | 2016-01-15 15:12:50 +0000 | [diff] [blame] | 1842 | [I915_EXEC_DEFAULT] = RCS, |
| 1843 | [I915_EXEC_RENDER] = RCS, |
| 1844 | [I915_EXEC_BLT] = BCS, |
| 1845 | [I915_EXEC_BSD] = VCS, |
| 1846 | [I915_EXEC_VEBOX] = VECS |
| 1847 | }; |
| 1848 | |
Dave Gordon | f8ca0c0 | 2016-07-20 18:16:07 +0100 | [diff] [blame] | 1849 | static struct intel_engine_cs * |
| 1850 | eb_select_engine(struct drm_i915_private *dev_priv, |
| 1851 | struct drm_file *file, |
| 1852 | struct drm_i915_gem_execbuffer2 *args) |
Tvrtko Ursulin | de1add3 | 2016-01-15 15:12:50 +0000 | [diff] [blame] | 1853 | { |
| 1854 | unsigned int user_ring_id = args->flags & I915_EXEC_RING_MASK; |
Dave Gordon | f8ca0c0 | 2016-07-20 18:16:07 +0100 | [diff] [blame] | 1855 | struct intel_engine_cs *engine; |
Tvrtko Ursulin | de1add3 | 2016-01-15 15:12:50 +0000 | [diff] [blame] | 1856 | |
| 1857 | if (user_ring_id > I915_USER_RINGS) { |
| 1858 | DRM_DEBUG("execbuf with unknown ring: %u\n", user_ring_id); |
Dave Gordon | f8ca0c0 | 2016-07-20 18:16:07 +0100 | [diff] [blame] | 1859 | return NULL; |
Tvrtko Ursulin | de1add3 | 2016-01-15 15:12:50 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
| 1862 | if ((user_ring_id != I915_EXEC_BSD) && |
| 1863 | ((args->flags & I915_EXEC_BSD_MASK) != 0)) { |
| 1864 | DRM_DEBUG("execbuf with non bsd ring but with invalid " |
| 1865 | "bsd dispatch flags: %d\n", (int)(args->flags)); |
Dave Gordon | f8ca0c0 | 2016-07-20 18:16:07 +0100 | [diff] [blame] | 1866 | return NULL; |
Tvrtko Ursulin | de1add3 | 2016-01-15 15:12:50 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
| 1869 | if (user_ring_id == I915_EXEC_BSD && HAS_BSD2(dev_priv)) { |
| 1870 | unsigned int bsd_idx = args->flags & I915_EXEC_BSD_MASK; |
| 1871 | |
| 1872 | if (bsd_idx == I915_EXEC_BSD_DEFAULT) { |
Chris Wilson | c80ff16 | 2016-07-27 09:07:27 +0100 | [diff] [blame] | 1873 | bsd_idx = gen8_dispatch_bsd_engine(dev_priv, file); |
Tvrtko Ursulin | de1add3 | 2016-01-15 15:12:50 +0000 | [diff] [blame] | 1874 | } else if (bsd_idx >= I915_EXEC_BSD_RING1 && |
| 1875 | bsd_idx <= I915_EXEC_BSD_RING2) { |
Tvrtko Ursulin | d9da6aa | 2016-01-27 13:41:09 +0000 | [diff] [blame] | 1876 | bsd_idx >>= I915_EXEC_BSD_SHIFT; |
Tvrtko Ursulin | de1add3 | 2016-01-15 15:12:50 +0000 | [diff] [blame] | 1877 | bsd_idx--; |
| 1878 | } else { |
| 1879 | DRM_DEBUG("execbuf with unknown bsd ring: %u\n", |
| 1880 | bsd_idx); |
Dave Gordon | f8ca0c0 | 2016-07-20 18:16:07 +0100 | [diff] [blame] | 1881 | return NULL; |
Tvrtko Ursulin | de1add3 | 2016-01-15 15:12:50 +0000 | [diff] [blame] | 1882 | } |
| 1883 | |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 1884 | engine = dev_priv->engine[_VCS(bsd_idx)]; |
Tvrtko Ursulin | de1add3 | 2016-01-15 15:12:50 +0000 | [diff] [blame] | 1885 | } else { |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 1886 | engine = dev_priv->engine[user_ring_map[user_ring_id]]; |
Tvrtko Ursulin | de1add3 | 2016-01-15 15:12:50 +0000 | [diff] [blame] | 1887 | } |
| 1888 | |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 1889 | if (!engine) { |
Tvrtko Ursulin | de1add3 | 2016-01-15 15:12:50 +0000 | [diff] [blame] | 1890 | DRM_DEBUG("execbuf with invalid ring: %u\n", user_ring_id); |
Dave Gordon | f8ca0c0 | 2016-07-20 18:16:07 +0100 | [diff] [blame] | 1891 | return NULL; |
Tvrtko Ursulin | de1add3 | 2016-01-15 15:12:50 +0000 | [diff] [blame] | 1892 | } |
| 1893 | |
Dave Gordon | f8ca0c0 | 2016-07-20 18:16:07 +0100 | [diff] [blame] | 1894 | return engine; |
Tvrtko Ursulin | de1add3 | 2016-01-15 15:12:50 +0000 | [diff] [blame] | 1895 | } |
| 1896 | |
Eric Anholt | ae662d3 | 2012-01-03 09:23:29 -0800 | [diff] [blame] | 1897 | static int |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 1898 | i915_gem_do_execbuffer(struct drm_device *dev, |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1899 | struct drm_file *file, |
| 1900 | struct drm_i915_gem_execbuffer2 *args, |
Ben Widawsky | 41bde55 | 2013-12-06 14:11:21 -0800 | [diff] [blame] | 1901 | struct drm_i915_gem_exec_object2 *exec) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1902 | { |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 1903 | struct i915_execbuffer eb; |
Chris Wilson | fec0445 | 2017-01-27 09:40:08 +0000 | [diff] [blame] | 1904 | struct dma_fence *in_fence = NULL; |
| 1905 | struct sync_file *out_fence = NULL; |
| 1906 | int out_fence_fd = -1; |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1907 | int err; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1908 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1909 | BUILD_BUG_ON(__EXEC_OBJECT_INTERNAL_FLAGS & |
| 1910 | ~__EXEC_OBJECT_UNKNOWN_FLAGS); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1911 | |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 1912 | eb.i915 = to_i915(dev); |
| 1913 | eb.file = file; |
| 1914 | eb.args = args; |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1915 | if (!(args->flags & I915_EXEC_NO_RELOC)) |
| 1916 | args->flags |= __EXEC_HAS_RELOC; |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 1917 | eb.exec = exec; |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1918 | eb.ctx = NULL; |
| 1919 | eb.invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS; |
| 1920 | if (USES_FULL_PPGTT(eb.i915)) |
| 1921 | eb.invalid_flags |= EXEC_OBJECT_NEEDS_GTT; |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 1922 | reloc_cache_init(&eb.reloc_cache, eb.i915); |
| 1923 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1924 | eb.buffer_count = args->buffer_count; |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 1925 | eb.batch_start_offset = args->batch_start_offset; |
| 1926 | eb.batch_len = args->batch_len; |
| 1927 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1928 | eb.batch_flags = 0; |
Chris Wilson | d7d4eed | 2012-10-17 12:09:54 +0100 | [diff] [blame] | 1929 | if (args->flags & I915_EXEC_SECURE) { |
Daniel Vetter | b3ac9f2 | 2016-06-21 10:54:20 +0200 | [diff] [blame] | 1930 | if (!drm_is_current_master(file) || !capable(CAP_SYS_ADMIN)) |
Chris Wilson | d7d4eed | 2012-10-17 12:09:54 +0100 | [diff] [blame] | 1931 | return -EPERM; |
| 1932 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1933 | eb.batch_flags |= I915_DISPATCH_SECURE; |
Chris Wilson | d7d4eed | 2012-10-17 12:09:54 +0100 | [diff] [blame] | 1934 | } |
Daniel Vetter | b45305f | 2012-12-17 16:21:27 +0100 | [diff] [blame] | 1935 | if (args->flags & I915_EXEC_IS_PINNED) |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1936 | eb.batch_flags |= I915_DISPATCH_PINNED; |
Chris Wilson | d7d4eed | 2012-10-17 12:09:54 +0100 | [diff] [blame] | 1937 | |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 1938 | eb.engine = eb_select_engine(eb.i915, file, args); |
| 1939 | if (!eb.engine) |
Dave Gordon | f8ca0c0 | 2016-07-20 18:16:07 +0100 | [diff] [blame] | 1940 | return -EINVAL; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1941 | |
Abdiel Janulgue | a9ed33c | 2015-07-01 10:12:23 +0300 | [diff] [blame] | 1942 | if (args->flags & I915_EXEC_RESOURCE_STREAMER) { |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 1943 | if (!HAS_RESOURCE_STREAMER(eb.i915)) { |
Abdiel Janulgue | a9ed33c | 2015-07-01 10:12:23 +0300 | [diff] [blame] | 1944 | DRM_DEBUG("RS is only allowed for Haswell, Gen8 and above\n"); |
| 1945 | return -EINVAL; |
| 1946 | } |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 1947 | if (eb.engine->id != RCS) { |
Abdiel Janulgue | a9ed33c | 2015-07-01 10:12:23 +0300 | [diff] [blame] | 1948 | DRM_DEBUG("RS is not available on %s\n", |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 1949 | eb.engine->name); |
Abdiel Janulgue | a9ed33c | 2015-07-01 10:12:23 +0300 | [diff] [blame] | 1950 | return -EINVAL; |
| 1951 | } |
| 1952 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1953 | eb.batch_flags |= I915_DISPATCH_RS; |
Abdiel Janulgue | a9ed33c | 2015-07-01 10:12:23 +0300 | [diff] [blame] | 1954 | } |
| 1955 | |
Chris Wilson | fec0445 | 2017-01-27 09:40:08 +0000 | [diff] [blame] | 1956 | if (args->flags & I915_EXEC_FENCE_IN) { |
| 1957 | in_fence = sync_file_get_fence(lower_32_bits(args->rsvd2)); |
Daniele Ceraolo Spurio | 4a04e37 | 2017-02-03 14:45:29 -0800 | [diff] [blame] | 1958 | if (!in_fence) |
| 1959 | return -EINVAL; |
Chris Wilson | fec0445 | 2017-01-27 09:40:08 +0000 | [diff] [blame] | 1960 | } |
| 1961 | |
| 1962 | if (args->flags & I915_EXEC_FENCE_OUT) { |
| 1963 | out_fence_fd = get_unused_fd_flags(O_CLOEXEC); |
| 1964 | if (out_fence_fd < 0) { |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1965 | err = out_fence_fd; |
Daniele Ceraolo Spurio | 4a04e37 | 2017-02-03 14:45:29 -0800 | [diff] [blame] | 1966 | goto err_in_fence; |
Chris Wilson | fec0445 | 2017-01-27 09:40:08 +0000 | [diff] [blame] | 1967 | } |
| 1968 | } |
| 1969 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1970 | if (eb_create(&eb)) |
| 1971 | return -ENOMEM; |
| 1972 | |
| 1973 | /* |
| 1974 | * Take a local wakeref for preparing to dispatch the execbuf as |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 1975 | * we expect to access the hardware fairly frequently in the |
| 1976 | * process. Upon first dispatch, we acquire another prolonged |
| 1977 | * wakeref that we hold until the GPU has been idle for at least |
| 1978 | * 100ms. |
| 1979 | */ |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 1980 | intel_runtime_pm_get(eb.i915); |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1981 | err = i915_mutex_lock_interruptible(dev); |
| 1982 | if (err) |
| 1983 | goto err_rpm; |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 1984 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1985 | err = eb_select_context(&eb); |
| 1986 | if (unlikely(err)) |
| 1987 | goto err_unlock; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1988 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 1989 | err = eb_relocate(&eb); |
| 1990 | if (err) |
| 1991 | /* |
| 1992 | * If the user expects the execobject.offset and |
| 1993 | * reloc.presumed_offset to be an exact match, |
| 1994 | * as for using NO_RELOC, then we cannot update |
| 1995 | * the execobject.offset until we have completed |
| 1996 | * relocation. |
| 1997 | */ |
| 1998 | args->flags &= ~__EXEC_HAS_RELOC; |
| 1999 | if (err < 0) |
| 2000 | goto err_vma; |
Ben Widawsky | 41bde55 | 2013-12-06 14:11:21 -0800 | [diff] [blame] | 2001 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2002 | if (unlikely(eb.batch->exec_entry->flags & EXEC_OBJECT_WRITE)) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 2003 | DRM_DEBUG("Attempting to use self-modifying batch buffer\n"); |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2004 | err = -EINVAL; |
| 2005 | goto err_vma; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 2006 | } |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 2007 | if (eb.batch_start_offset > eb.batch->size || |
| 2008 | eb.batch_len > eb.batch->size - eb.batch_start_offset) { |
Chris Wilson | 0b53727 | 2016-08-18 17:17:12 +0100 | [diff] [blame] | 2009 | DRM_DEBUG("Attempting to use out-of-bounds batch\n"); |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2010 | err = -EINVAL; |
| 2011 | goto err_vma; |
Chris Wilson | 0b53727 | 2016-08-18 17:17:12 +0100 | [diff] [blame] | 2012 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 2013 | |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 2014 | if (eb.engine->needs_cmd_parser && eb.batch_len) { |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 2015 | struct i915_vma *vma; |
Rebecca N. Palmer | c7c7372 | 2015-05-08 14:26:50 +0100 | [diff] [blame] | 2016 | |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 2017 | vma = eb_parse(&eb, drm_is_current_master(file)); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 2018 | if (IS_ERR(vma)) { |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2019 | err = PTR_ERR(vma); |
| 2020 | goto err_vma; |
Brad Volkin | 78a4237 | 2014-12-11 12:13:09 -0800 | [diff] [blame] | 2021 | } |
Chris Wilson | 17cabf5 | 2015-01-14 11:20:57 +0000 | [diff] [blame] | 2022 | |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 2023 | if (vma) { |
Rebecca N. Palmer | c7c7372 | 2015-05-08 14:26:50 +0100 | [diff] [blame] | 2024 | /* |
| 2025 | * Batch parsed and accepted: |
| 2026 | * |
| 2027 | * Set the DISPATCH_SECURE bit to remove the NON_SECURE |
| 2028 | * bit from MI_BATCH_BUFFER_START commands issued in |
| 2029 | * the dispatch_execbuffer implementations. We |
| 2030 | * specifically don't want that set on batches the |
| 2031 | * command parser has accepted. |
| 2032 | */ |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2033 | eb.batch_flags |= I915_DISPATCH_SECURE; |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 2034 | eb.batch_start_offset = 0; |
| 2035 | eb.batch = vma; |
Rebecca N. Palmer | c7c7372 | 2015-05-08 14:26:50 +0100 | [diff] [blame] | 2036 | } |
Brad Volkin | 351e3db | 2014-02-18 10:15:46 -0800 | [diff] [blame] | 2037 | } |
| 2038 | |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 2039 | if (eb.batch_len == 0) |
| 2040 | eb.batch_len = eb.batch->size - eb.batch_start_offset; |
Brad Volkin | 78a4237 | 2014-12-11 12:13:09 -0800 | [diff] [blame] | 2041 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2042 | /* |
| 2043 | * snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure |
Chris Wilson | d7d4eed | 2012-10-17 12:09:54 +0100 | [diff] [blame] | 2044 | * batch" bit. Hence we need to pin secure batches into the global gtt. |
Ben Widawsky | 28cf541 | 2013-11-02 21:07:26 -0700 | [diff] [blame] | 2045 | * hsw should have this fixed, but bdw mucks it up again. */ |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2046 | if (eb.batch_flags & I915_DISPATCH_SECURE) { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 2047 | struct i915_vma *vma; |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 2048 | |
Daniel Vetter | da51a1e | 2014-08-11 12:08:58 +0200 | [diff] [blame] | 2049 | /* |
| 2050 | * So on first glance it looks freaky that we pin the batch here |
| 2051 | * outside of the reservation loop. But: |
| 2052 | * - The batch is already pinned into the relevant ppgtt, so we |
| 2053 | * already have the backing storage fully allocated. |
| 2054 | * - No other BO uses the global gtt (well contexts, but meh), |
Yannick Guerrini | fd0753c | 2015-02-28 17:20:41 +0100 | [diff] [blame] | 2055 | * so we don't really have issues with multiple objects not |
Daniel Vetter | da51a1e | 2014-08-11 12:08:58 +0200 | [diff] [blame] | 2056 | * fitting due to fragmentation. |
| 2057 | * So this is actually safe. |
| 2058 | */ |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2059 | vma = i915_gem_object_ggtt_pin(eb.batch->obj, NULL, 0, 0, 0); |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 2060 | if (IS_ERR(vma)) { |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2061 | err = PTR_ERR(vma); |
| 2062 | goto err_vma; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 2063 | } |
Chris Wilson | d7d4eed | 2012-10-17 12:09:54 +0100 | [diff] [blame] | 2064 | |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 2065 | eb.batch = vma; |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 2066 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 2067 | |
John Harrison | 0c8dac8 | 2015-05-29 17:43:25 +0100 | [diff] [blame] | 2068 | /* Allocate a request for this batch buffer nice and early. */ |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 2069 | eb.request = i915_gem_request_alloc(eb.engine, eb.ctx); |
| 2070 | if (IS_ERR(eb.request)) { |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2071 | err = PTR_ERR(eb.request); |
John Harrison | 0c8dac8 | 2015-05-29 17:43:25 +0100 | [diff] [blame] | 2072 | goto err_batch_unpin; |
Dave Gordon | 2682708 | 2016-01-19 19:02:53 +0000 | [diff] [blame] | 2073 | } |
John Harrison | 0c8dac8 | 2015-05-29 17:43:25 +0100 | [diff] [blame] | 2074 | |
Chris Wilson | fec0445 | 2017-01-27 09:40:08 +0000 | [diff] [blame] | 2075 | if (in_fence) { |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2076 | err = i915_gem_request_await_dma_fence(eb.request, in_fence); |
| 2077 | if (err < 0) |
Chris Wilson | fec0445 | 2017-01-27 09:40:08 +0000 | [diff] [blame] | 2078 | goto err_request; |
| 2079 | } |
| 2080 | |
| 2081 | if (out_fence_fd != -1) { |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 2082 | out_fence = sync_file_create(&eb.request->fence); |
Chris Wilson | fec0445 | 2017-01-27 09:40:08 +0000 | [diff] [blame] | 2083 | if (!out_fence) { |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2084 | err = -ENOMEM; |
Chris Wilson | fec0445 | 2017-01-27 09:40:08 +0000 | [diff] [blame] | 2085 | goto err_request; |
| 2086 | } |
| 2087 | } |
| 2088 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2089 | /* |
| 2090 | * Whilst this request exists, batch_obj will be on the |
Chris Wilson | 17f298cf | 2016-08-10 13:41:46 +0100 | [diff] [blame] | 2091 | * active_list, and so will hold the active reference. Only when this |
| 2092 | * request is retired will the the batch_obj be moved onto the |
| 2093 | * inactive_list and lose its active reference. Hence we do not need |
| 2094 | * to explicitly hold another reference here. |
| 2095 | */ |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 2096 | eb.request->batch = eb.batch; |
Chris Wilson | 17f298cf | 2016-08-10 13:41:46 +0100 | [diff] [blame] | 2097 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2098 | trace_i915_gem_request_queue(eb.request, eb.batch_flags); |
| 2099 | err = eb_submit(&eb); |
Chris Wilson | aa9b781 | 2016-04-13 17:35:15 +0100 | [diff] [blame] | 2100 | err_request: |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2101 | __i915_add_request(eb.request, err == 0); |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 2102 | add_to_client(eb.request, file); |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 2103 | |
Chris Wilson | fec0445 | 2017-01-27 09:40:08 +0000 | [diff] [blame] | 2104 | if (out_fence) { |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2105 | if (err == 0) { |
Chris Wilson | fec0445 | 2017-01-27 09:40:08 +0000 | [diff] [blame] | 2106 | fd_install(out_fence_fd, out_fence->file); |
| 2107 | args->rsvd2 &= GENMASK_ULL(0, 31); /* keep in-fence */ |
| 2108 | args->rsvd2 |= (u64)out_fence_fd << 32; |
| 2109 | out_fence_fd = -1; |
| 2110 | } else { |
| 2111 | fput(out_fence->file); |
| 2112 | } |
| 2113 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 2114 | |
John Harrison | 0c8dac8 | 2015-05-29 17:43:25 +0100 | [diff] [blame] | 2115 | err_batch_unpin: |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2116 | if (eb.batch_flags & I915_DISPATCH_SECURE) |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 2117 | i915_vma_unpin(eb.batch); |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2118 | err_vma: |
| 2119 | if (eb.exec) |
| 2120 | eb_release_vmas(&eb); |
| 2121 | i915_gem_context_put(eb.ctx); |
| 2122 | err_unlock: |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 2123 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2124 | err_rpm: |
Chris Wilson | 650bc63 | 2017-06-15 09:14:33 +0100 | [diff] [blame] | 2125 | intel_runtime_pm_put(eb.i915); |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2126 | eb_destroy(&eb); |
Chris Wilson | fec0445 | 2017-01-27 09:40:08 +0000 | [diff] [blame] | 2127 | if (out_fence_fd != -1) |
| 2128 | put_unused_fd(out_fence_fd); |
Daniele Ceraolo Spurio | 4a04e37 | 2017-02-03 14:45:29 -0800 | [diff] [blame] | 2129 | err_in_fence: |
Chris Wilson | fec0445 | 2017-01-27 09:40:08 +0000 | [diff] [blame] | 2130 | dma_fence_put(in_fence); |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2131 | return err; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 2132 | } |
| 2133 | |
| 2134 | /* |
| 2135 | * Legacy execbuffer just creates an exec2 list from the original exec object |
| 2136 | * list array and passes it to the real function. |
| 2137 | */ |
| 2138 | int |
| 2139 | i915_gem_execbuffer(struct drm_device *dev, void *data, |
| 2140 | struct drm_file *file) |
| 2141 | { |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2142 | const size_t sz = sizeof(struct drm_i915_gem_exec_object2); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 2143 | struct drm_i915_gem_execbuffer *args = data; |
| 2144 | struct drm_i915_gem_execbuffer2 exec2; |
| 2145 | struct drm_i915_gem_exec_object *exec_list = NULL; |
| 2146 | struct drm_i915_gem_exec_object2 *exec2_list = NULL; |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2147 | unsigned int i; |
| 2148 | int err; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 2149 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2150 | if (args->buffer_count < 1 || args->buffer_count > SIZE_MAX / sz - 1) { |
| 2151 | DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 2152 | return -EINVAL; |
| 2153 | } |
| 2154 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2155 | exec2.buffers_ptr = args->buffers_ptr; |
| 2156 | exec2.buffer_count = args->buffer_count; |
| 2157 | exec2.batch_start_offset = args->batch_start_offset; |
| 2158 | exec2.batch_len = args->batch_len; |
| 2159 | exec2.DR1 = args->DR1; |
| 2160 | exec2.DR4 = args->DR4; |
| 2161 | exec2.num_cliprects = args->num_cliprects; |
| 2162 | exec2.cliprects_ptr = args->cliprects_ptr; |
| 2163 | exec2.flags = I915_EXEC_RENDER; |
| 2164 | i915_execbuffer2_set_context_id(exec2, 0); |
| 2165 | |
| 2166 | if (!i915_gem_check_execbuffer(&exec2)) |
| 2167 | return -EINVAL; |
| 2168 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 2169 | /* Copy in the exec list from userland */ |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2170 | exec_list = kvmalloc_array(args->buffer_count, sizeof(*exec_list), |
| 2171 | __GFP_NOWARN | GFP_TEMPORARY); |
| 2172 | exec2_list = kvmalloc_array(args->buffer_count + 1, sz, |
| 2173 | __GFP_NOWARN | GFP_TEMPORARY); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 2174 | if (exec_list == NULL || exec2_list == NULL) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 2175 | DRM_DEBUG("Failed to allocate exec list for %d buffers\n", |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 2176 | args->buffer_count); |
Michal Hocko | 2098105 | 2017-05-17 14:23:12 +0200 | [diff] [blame] | 2177 | kvfree(exec_list); |
| 2178 | kvfree(exec2_list); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 2179 | return -ENOMEM; |
| 2180 | } |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2181 | err = copy_from_user(exec_list, |
Gustavo Padovan | 3ed605b | 2016-04-26 12:32:27 -0300 | [diff] [blame] | 2182 | u64_to_user_ptr(args->buffers_ptr), |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 2183 | sizeof(*exec_list) * args->buffer_count); |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2184 | if (err) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 2185 | DRM_DEBUG("copy %d exec entries failed %d\n", |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2186 | args->buffer_count, err); |
Michal Hocko | 2098105 | 2017-05-17 14:23:12 +0200 | [diff] [blame] | 2187 | kvfree(exec_list); |
| 2188 | kvfree(exec2_list); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 2189 | return -EFAULT; |
| 2190 | } |
| 2191 | |
| 2192 | for (i = 0; i < args->buffer_count; i++) { |
| 2193 | exec2_list[i].handle = exec_list[i].handle; |
| 2194 | exec2_list[i].relocation_count = exec_list[i].relocation_count; |
| 2195 | exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr; |
| 2196 | exec2_list[i].alignment = exec_list[i].alignment; |
| 2197 | exec2_list[i].offset = exec_list[i].offset; |
Tvrtko Ursulin | f0836b7 | 2016-11-16 08:55:32 +0000 | [diff] [blame] | 2198 | if (INTEL_GEN(to_i915(dev)) < 4) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 2199 | exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE; |
| 2200 | else |
| 2201 | exec2_list[i].flags = 0; |
| 2202 | } |
| 2203 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2204 | err = i915_gem_do_execbuffer(dev, file, &exec2, exec2_list); |
| 2205 | if (exec2.flags & __EXEC_HAS_RELOC) { |
Chris Wilson | 9aab8bf | 2014-05-23 10:45:52 +0100 | [diff] [blame] | 2206 | struct drm_i915_gem_exec_object __user *user_exec_list = |
Gustavo Padovan | 3ed605b | 2016-04-26 12:32:27 -0300 | [diff] [blame] | 2207 | u64_to_user_ptr(args->buffers_ptr); |
Chris Wilson | 9aab8bf | 2014-05-23 10:45:52 +0100 | [diff] [blame] | 2208 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 2209 | /* Copy the new buffer offsets back to the user's exec list. */ |
Chris Wilson | 9aab8bf | 2014-05-23 10:45:52 +0100 | [diff] [blame] | 2210 | for (i = 0; i < args->buffer_count; i++) { |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2211 | if (!(exec2_list[i].offset & UPDATE)) |
| 2212 | continue; |
| 2213 | |
Michał Winiarski | 934acce | 2015-12-29 18:24:52 +0100 | [diff] [blame] | 2214 | exec2_list[i].offset = |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2215 | gen8_canonical_addr(exec2_list[i].offset & PIN_OFFSET_MASK); |
| 2216 | exec2_list[i].offset &= PIN_OFFSET_MASK; |
| 2217 | if (__copy_to_user(&user_exec_list[i].offset, |
| 2218 | &exec2_list[i].offset, |
| 2219 | sizeof(user_exec_list[i].offset))) |
Chris Wilson | 9aab8bf | 2014-05-23 10:45:52 +0100 | [diff] [blame] | 2220 | break; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 2221 | } |
| 2222 | } |
| 2223 | |
Michal Hocko | 2098105 | 2017-05-17 14:23:12 +0200 | [diff] [blame] | 2224 | kvfree(exec_list); |
| 2225 | kvfree(exec2_list); |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2226 | return err; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 2227 | } |
| 2228 | |
| 2229 | int |
| 2230 | i915_gem_execbuffer2(struct drm_device *dev, void *data, |
| 2231 | struct drm_file *file) |
| 2232 | { |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2233 | const size_t sz = sizeof(struct drm_i915_gem_exec_object2); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 2234 | struct drm_i915_gem_execbuffer2 *args = data; |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2235 | struct drm_i915_gem_exec_object2 *exec2_list; |
| 2236 | int err; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 2237 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2238 | if (args->buffer_count < 1 || args->buffer_count > SIZE_MAX / sz - 1) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 2239 | DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 2240 | return -EINVAL; |
| 2241 | } |
| 2242 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2243 | if (!i915_gem_check_execbuffer(args)) |
| 2244 | return -EINVAL; |
| 2245 | |
| 2246 | /* Allocate an extra slot for use by the command parser */ |
| 2247 | exec2_list = kvmalloc_array(args->buffer_count + 1, sz, |
| 2248 | __GFP_NOWARN | GFP_TEMPORARY); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 2249 | if (exec2_list == NULL) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 2250 | DRM_DEBUG("Failed to allocate exec list for %d buffers\n", |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 2251 | args->buffer_count); |
| 2252 | return -ENOMEM; |
| 2253 | } |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2254 | if (copy_from_user(exec2_list, |
| 2255 | u64_to_user_ptr(args->buffers_ptr), |
| 2256 | sizeof(*exec2_list) * args->buffer_count)) { |
| 2257 | DRM_DEBUG("copy %d exec entries failed\n", args->buffer_count); |
Michal Hocko | 2098105 | 2017-05-17 14:23:12 +0200 | [diff] [blame] | 2258 | kvfree(exec2_list); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 2259 | return -EFAULT; |
| 2260 | } |
| 2261 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2262 | err = i915_gem_do_execbuffer(dev, file, args, exec2_list); |
Chris Wilson | 9aab8bf | 2014-05-23 10:45:52 +0100 | [diff] [blame] | 2263 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2264 | /* |
| 2265 | * Now that we have begun execution of the batchbuffer, we ignore |
| 2266 | * any new error after this point. Also given that we have already |
| 2267 | * updated the associated relocations, we try to write out the current |
| 2268 | * object locations irrespective of any error. |
| 2269 | */ |
| 2270 | if (args->flags & __EXEC_HAS_RELOC) { |
| 2271 | struct drm_i915_gem_exec_object2 __user *user_exec_list = |
| 2272 | u64_to_user_ptr(args->buffers_ptr); |
| 2273 | unsigned int i; |
| 2274 | |
| 2275 | /* Copy the new buffer offsets back to the user's exec list. */ |
| 2276 | user_access_begin(); |
Chris Wilson | 9aab8bf | 2014-05-23 10:45:52 +0100 | [diff] [blame] | 2277 | for (i = 0; i < args->buffer_count; i++) { |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2278 | if (!(exec2_list[i].offset & UPDATE)) |
| 2279 | continue; |
| 2280 | |
Michał Winiarski | 934acce | 2015-12-29 18:24:52 +0100 | [diff] [blame] | 2281 | exec2_list[i].offset = |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2282 | gen8_canonical_addr(exec2_list[i].offset & PIN_OFFSET_MASK); |
| 2283 | unsafe_put_user(exec2_list[i].offset, |
| 2284 | &user_exec_list[i].offset, |
| 2285 | end_user); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 2286 | } |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2287 | end_user: |
| 2288 | user_access_end(); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 2289 | } |
| 2290 | |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2291 | args->flags &= ~__I915_EXEC_UNKNOWN_FLAGS; |
Michal Hocko | 2098105 | 2017-05-17 14:23:12 +0200 | [diff] [blame] | 2292 | kvfree(exec2_list); |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 2293 | return err; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 2294 | } |