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