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