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