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