blob: 75d7575b81f4170acd27805e62c0abf5c04058d0 [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
Daniel Vetterbe6a0372015-03-18 10:46:04 +01002 * Copyright © 2008-2015 Intel Corporation
Eric Anholt673a3942008-07-30 12:06:12 -07003 *
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 *
26 */
27
David Howells760285e2012-10-02 18:01:07 +010028#include <drm/drmP.h>
David Herrmann0de23972013-07-24 21:07:52 +020029#include <drm/drm_vma_manager.h>
David Howells760285e2012-10-02 18:01:07 +010030#include <drm/i915_drm.h>
Eric Anholt673a3942008-07-30 12:06:12 -070031#include "i915_drv.h"
Chris Wilson57822dc2017-02-22 11:40:48 +000032#include "i915_gem_clflush.h"
Yu Zhangeb822892015-02-10 19:05:49 +080033#include "i915_vgpu.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010034#include "i915_trace.h"
Jesse Barnes652c3932009-08-17 13:31:43 -070035#include "intel_drv.h"
Chris Wilson5d723d72016-08-04 16:32:35 +010036#include "intel_frontbuffer.h"
Peter Antoine0ccdacf2016-04-13 15:03:25 +010037#include "intel_mocs.h"
Chris Wilson6b5e90f2016-11-14 20:41:05 +000038#include <linux/dma-fence-array.h>
Chris Wilsonfe3288b2017-02-12 17:20:01 +000039#include <linux/kthread.h>
Chris Wilsonc13d87e2016-07-20 09:21:15 +010040#include <linux/reservation.h>
Hugh Dickins5949eac2011-06-27 16:18:18 -070041#include <linux/shmem_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Chris Wilson20e49332016-11-22 14:41:21 +000043#include <linux/stop_machine.h>
Eric Anholt673a3942008-07-30 12:06:12 -070044#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080045#include <linux/pci.h>
Daniel Vetter1286ff72012-05-10 15:25:09 +020046#include <linux/dma-buf.h>
Eric Anholt673a3942008-07-30 12:06:12 -070047
Chris Wilsonfbbd37b2016-10-28 13:58:42 +010048static void i915_gem_flush_free_objects(struct drm_i915_private *i915);
Chris Wilson61050802012-04-17 15:31:31 +010049
Chris Wilson2c225692013-08-09 12:26:45 +010050static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
51{
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +053052 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
53 return false;
54
Chris Wilsone59dc172017-02-22 11:40:45 +000055 if (!i915_gem_object_is_coherent(obj))
Chris Wilson2c225692013-08-09 12:26:45 +010056 return true;
57
58 return obj->pin_display;
59}
60
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053061static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +010062insert_mappable_node(struct i915_ggtt *ggtt,
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053063 struct drm_mm_node *node, u32 size)
64{
65 memset(node, 0, sizeof(*node));
Chris Wilson4e64e552017-02-02 21:04:38 +000066 return drm_mm_insert_node_in_range(&ggtt->base.mm, node,
67 size, 0, I915_COLOR_UNEVICTABLE,
68 0, ggtt->mappable_end,
69 DRM_MM_INSERT_LOW);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053070}
71
72static void
73remove_mappable_node(struct drm_mm_node *node)
74{
75 drm_mm_remove_node(node);
76}
77
Chris Wilson73aa8082010-09-30 11:46:12 +010078/* some bookkeeping */
79static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010080 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010081{
Daniel Vetterc20e8352013-07-24 22:40:23 +020082 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010083 dev_priv->mm.object_count++;
84 dev_priv->mm.object_memory += size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020085 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010086}
87
88static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010089 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010090{
Daniel Vetterc20e8352013-07-24 22:40:23 +020091 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010092 dev_priv->mm.object_count--;
93 dev_priv->mm.object_memory -= size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020094 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010095}
96
Chris Wilson21dd3732011-01-26 15:55:56 +000097static int
Daniel Vetter33196de2012-11-14 17:14:05 +010098i915_gem_wait_for_error(struct i915_gpu_error *error)
Chris Wilson30dbf0c2010-09-25 10:19:17 +010099{
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100100 int ret;
101
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100102 might_sleep();
103
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200104 /*
105 * Only wait 10 seconds for the gpu reset to complete to avoid hanging
106 * userspace. If it takes that long something really bad is going on and
107 * we should simply try to bail out and fail as gracefully as possible.
108 */
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100109 ret = wait_event_interruptible_timeout(error->reset_queue,
Chris Wilson8c185ec2017-03-16 17:13:02 +0000110 !i915_reset_backoff(error),
Chris Wilsonb52992c2016-10-28 13:58:24 +0100111 I915_RESET_TIMEOUT);
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200112 if (ret == 0) {
113 DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
114 return -EIO;
115 } else if (ret < 0) {
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100116 return ret;
Chris Wilsond98c52c2016-04-13 17:35:05 +0100117 } else {
118 return 0;
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200119 }
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100120}
121
Chris Wilson54cf91d2010-11-25 18:00:26 +0000122int i915_mutex_lock_interruptible(struct drm_device *dev)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100123{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100124 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100125 int ret;
126
Daniel Vetter33196de2012-11-14 17:14:05 +0100127 ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100128 if (ret)
129 return ret;
130
131 ret = mutex_lock_interruptible(&dev->struct_mutex);
132 if (ret)
133 return ret;
134
Chris Wilson76c1dec2010-09-25 11:22:51 +0100135 return 0;
136}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100137
Eric Anholt673a3942008-07-30 12:06:12 -0700138int
Eric Anholt5a125c32008-10-22 21:40:13 -0700139i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000140 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700141{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300142 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen62106b42016-03-18 10:42:57 +0200143 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300144 struct drm_i915_gem_get_aperture *args = data;
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100145 struct i915_vma *vma;
Chris Wilson6299f992010-11-24 12:23:44 +0000146 size_t pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700147
Chris Wilson6299f992010-11-24 12:23:44 +0000148 pinned = 0;
Chris Wilson73aa8082010-09-30 11:46:12 +0100149 mutex_lock(&dev->struct_mutex);
Chris Wilson1c7f4bc2016-02-26 11:03:19 +0000150 list_for_each_entry(vma, &ggtt->base.active_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100151 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100152 pinned += vma->node.size;
Chris Wilson1c7f4bc2016-02-26 11:03:19 +0000153 list_for_each_entry(vma, &ggtt->base.inactive_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100154 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100155 pinned += vma->node.size;
Chris Wilson73aa8082010-09-30 11:46:12 +0100156 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700157
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300158 args->aper_size = ggtt->base.total;
Akshay Joshi0206e352011-08-16 15:34:10 -0400159 args->aper_available_size = args->aper_size - pinned;
Chris Wilson6299f992010-11-24 12:23:44 +0000160
Eric Anholt5a125c32008-10-22 21:40:13 -0700161 return 0;
162}
163
Chris Wilson03ac84f2016-10-28 13:58:36 +0100164static struct sg_table *
Chris Wilson6a2c4232014-11-04 04:51:40 -0800165i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
Chris Wilson00731152014-05-21 12:42:56 +0100166{
Al Viro93c76a32015-12-04 23:45:44 -0500167 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilsondbb43512016-12-07 13:34:11 +0000168 drm_dma_handle_t *phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800169 struct sg_table *st;
170 struct scatterlist *sg;
Chris Wilsondbb43512016-12-07 13:34:11 +0000171 char *vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800172 int i;
Chris Wilson00731152014-05-21 12:42:56 +0100173
Chris Wilson6a2c4232014-11-04 04:51:40 -0800174 if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
Chris Wilson03ac84f2016-10-28 13:58:36 +0100175 return ERR_PTR(-EINVAL);
Chris Wilson00731152014-05-21 12:42:56 +0100176
Chris Wilsondbb43512016-12-07 13:34:11 +0000177 /* Always aligning to the object size, allows a single allocation
178 * to handle all possible callers, and given typical object sizes,
179 * the alignment of the buddy allocation will naturally match.
180 */
181 phys = drm_pci_alloc(obj->base.dev,
182 obj->base.size,
183 roundup_pow_of_two(obj->base.size));
184 if (!phys)
185 return ERR_PTR(-ENOMEM);
186
187 vaddr = phys->vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800188 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
189 struct page *page;
190 char *src;
191
192 page = shmem_read_mapping_page(mapping, i);
Chris Wilsondbb43512016-12-07 13:34:11 +0000193 if (IS_ERR(page)) {
194 st = ERR_CAST(page);
195 goto err_phys;
196 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800197
198 src = kmap_atomic(page);
199 memcpy(vaddr, src, PAGE_SIZE);
200 drm_clflush_virt_range(vaddr, PAGE_SIZE);
201 kunmap_atomic(src);
202
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300203 put_page(page);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800204 vaddr += PAGE_SIZE;
205 }
206
Chris Wilsonc0336662016-05-06 15:40:21 +0100207 i915_gem_chipset_flush(to_i915(obj->base.dev));
Chris Wilson6a2c4232014-11-04 04:51:40 -0800208
209 st = kmalloc(sizeof(*st), GFP_KERNEL);
Chris Wilsondbb43512016-12-07 13:34:11 +0000210 if (!st) {
211 st = ERR_PTR(-ENOMEM);
212 goto err_phys;
213 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800214
215 if (sg_alloc_table(st, 1, GFP_KERNEL)) {
216 kfree(st);
Chris Wilsondbb43512016-12-07 13:34:11 +0000217 st = ERR_PTR(-ENOMEM);
218 goto err_phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800219 }
220
221 sg = st->sgl;
222 sg->offset = 0;
223 sg->length = obj->base.size;
224
Chris Wilsondbb43512016-12-07 13:34:11 +0000225 sg_dma_address(sg) = phys->busaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800226 sg_dma_len(sg) = obj->base.size;
227
Chris Wilsondbb43512016-12-07 13:34:11 +0000228 obj->phys_handle = phys;
229 return st;
230
231err_phys:
232 drm_pci_free(obj->base.dev, phys);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100233 return st;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800234}
235
236static void
Chris Wilson2b3c8312016-11-11 14:58:09 +0000237__i915_gem_object_release_shmem(struct drm_i915_gem_object *obj,
Chris Wilsone5facdf2016-12-23 14:57:57 +0000238 struct sg_table *pages,
239 bool needs_clflush)
Chris Wilson6a2c4232014-11-04 04:51:40 -0800240{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100241 GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800242
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100243 if (obj->mm.madv == I915_MADV_DONTNEED)
244 obj->mm.dirty = false;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800245
Chris Wilsone5facdf2016-12-23 14:57:57 +0000246 if (needs_clflush &&
247 (obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0 &&
Chris Wilsone59dc172017-02-22 11:40:45 +0000248 !i915_gem_object_is_coherent(obj))
Chris Wilson2b3c8312016-11-11 14:58:09 +0000249 drm_clflush_sg(pages);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100250
251 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
252 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
253}
254
255static void
256i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj,
257 struct sg_table *pages)
258{
Chris Wilsone5facdf2016-12-23 14:57:57 +0000259 __i915_gem_object_release_shmem(obj, pages, false);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100260
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100261 if (obj->mm.dirty) {
Al Viro93c76a32015-12-04 23:45:44 -0500262 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800263 char *vaddr = obj->phys_handle->vaddr;
Chris Wilson00731152014-05-21 12:42:56 +0100264 int i;
265
266 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
Chris Wilson6a2c4232014-11-04 04:51:40 -0800267 struct page *page;
268 char *dst;
Chris Wilson00731152014-05-21 12:42:56 +0100269
Chris Wilson6a2c4232014-11-04 04:51:40 -0800270 page = shmem_read_mapping_page(mapping, i);
271 if (IS_ERR(page))
272 continue;
273
274 dst = kmap_atomic(page);
275 drm_clflush_virt_range(vaddr, PAGE_SIZE);
276 memcpy(dst, vaddr, PAGE_SIZE);
277 kunmap_atomic(dst);
278
279 set_page_dirty(page);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100280 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson00731152014-05-21 12:42:56 +0100281 mark_page_accessed(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300282 put_page(page);
Chris Wilson00731152014-05-21 12:42:56 +0100283 vaddr += PAGE_SIZE;
284 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100285 obj->mm.dirty = false;
Chris Wilson00731152014-05-21 12:42:56 +0100286 }
287
Chris Wilson03ac84f2016-10-28 13:58:36 +0100288 sg_free_table(pages);
289 kfree(pages);
Chris Wilsondbb43512016-12-07 13:34:11 +0000290
291 drm_pci_free(obj->base.dev, obj->phys_handle);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800292}
293
294static void
295i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
296{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100297 i915_gem_object_unpin_pages(obj);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800298}
299
300static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
301 .get_pages = i915_gem_object_get_pages_phys,
302 .put_pages = i915_gem_object_put_pages_phys,
303 .release = i915_gem_object_release_phys,
304};
305
Chris Wilson581ab1f2017-02-15 16:39:00 +0000306static const struct drm_i915_gem_object_ops i915_gem_object_ops;
307
Chris Wilson35a96112016-08-14 18:44:40 +0100308int i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Chris Wilsonaa653a62016-08-04 07:52:27 +0100309{
310 struct i915_vma *vma;
311 LIST_HEAD(still_in_list);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100312 int ret;
Chris Wilsonaa653a62016-08-04 07:52:27 +0100313
Chris Wilson02bef8f2016-08-14 18:44:41 +0100314 lockdep_assert_held(&obj->base.dev->struct_mutex);
315
316 /* Closed vma are removed from the obj->vma_list - but they may
317 * still have an active binding on the object. To remove those we
318 * must wait for all rendering to complete to the object (as unbinding
319 * must anyway), and retire the requests.
Chris Wilsonaa653a62016-08-04 07:52:27 +0100320 */
Chris Wilsone95433c2016-10-28 13:58:27 +0100321 ret = i915_gem_object_wait(obj,
322 I915_WAIT_INTERRUPTIBLE |
323 I915_WAIT_LOCKED |
324 I915_WAIT_ALL,
325 MAX_SCHEDULE_TIMEOUT,
326 NULL);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100327 if (ret)
328 return ret;
329
330 i915_gem_retire_requests(to_i915(obj->base.dev));
331
Chris Wilsonaa653a62016-08-04 07:52:27 +0100332 while ((vma = list_first_entry_or_null(&obj->vma_list,
333 struct i915_vma,
334 obj_link))) {
335 list_move_tail(&vma->obj_link, &still_in_list);
336 ret = i915_vma_unbind(vma);
337 if (ret)
338 break;
339 }
340 list_splice(&still_in_list, &obj->vma_list);
341
342 return ret;
343}
344
Chris Wilsone95433c2016-10-28 13:58:27 +0100345static long
346i915_gem_object_wait_fence(struct dma_fence *fence,
347 unsigned int flags,
348 long timeout,
349 struct intel_rps_client *rps)
350{
351 struct drm_i915_gem_request *rq;
352
353 BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1);
354
355 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
356 return timeout;
357
358 if (!dma_fence_is_i915(fence))
359 return dma_fence_wait_timeout(fence,
360 flags & I915_WAIT_INTERRUPTIBLE,
361 timeout);
362
363 rq = to_request(fence);
364 if (i915_gem_request_completed(rq))
365 goto out;
366
367 /* This client is about to stall waiting for the GPU. In many cases
368 * this is undesirable and limits the throughput of the system, as
369 * many clients cannot continue processing user input/output whilst
370 * blocked. RPS autotuning may take tens of milliseconds to respond
371 * to the GPU load and thus incurs additional latency for the client.
372 * We can circumvent that by promoting the GPU frequency to maximum
373 * before we wait. This makes the GPU throttle up much more quickly
374 * (good for benchmarks and user experience, e.g. window animations),
375 * but at a cost of spending more power processing the workload
376 * (bad for battery). Not all clients even want their results
377 * immediately and for them we should just let the GPU select its own
378 * frequency to maximise efficiency. To prevent a single client from
379 * forcing the clocks too high for the whole system, we only allow
380 * each client to waitboost once in a busy period.
381 */
382 if (rps) {
383 if (INTEL_GEN(rq->i915) >= 6)
384 gen6_rps_boost(rq->i915, rps, rq->emitted_jiffies);
385 else
386 rps = NULL;
387 }
388
389 timeout = i915_wait_request(rq, flags, timeout);
390
391out:
392 if (flags & I915_WAIT_LOCKED && i915_gem_request_completed(rq))
393 i915_gem_request_retire_upto(rq);
394
Chris Wilson754c9fd2017-02-23 07:44:14 +0000395 if (rps && i915_gem_request_global_seqno(rq) == intel_engine_last_submit(rq->engine)) {
Chris Wilsone95433c2016-10-28 13:58:27 +0100396 /* The GPU is now idle and this client has stalled.
397 * Since no other client has submitted a request in the
398 * meantime, assume that this client is the only one
399 * supplying work to the GPU but is unable to keep that
400 * work supplied because it is waiting. Since the GPU is
401 * then never kept fully busy, RPS autoclocking will
402 * keep the clocks relatively low, causing further delays.
403 * Compensate by giving the synchronous client credit for
404 * a waitboost next time.
405 */
406 spin_lock(&rq->i915->rps.client_lock);
407 list_del_init(&rps->link);
408 spin_unlock(&rq->i915->rps.client_lock);
409 }
410
411 return timeout;
412}
413
414static long
415i915_gem_object_wait_reservation(struct reservation_object *resv,
416 unsigned int flags,
417 long timeout,
418 struct intel_rps_client *rps)
419{
Chris Wilsone54ca972017-02-17 15:13:04 +0000420 unsigned int seq = __read_seqcount_begin(&resv->seq);
Chris Wilsone95433c2016-10-28 13:58:27 +0100421 struct dma_fence *excl;
Chris Wilsone54ca972017-02-17 15:13:04 +0000422 bool prune_fences = false;
Chris Wilsone95433c2016-10-28 13:58:27 +0100423
424 if (flags & I915_WAIT_ALL) {
425 struct dma_fence **shared;
426 unsigned int count, i;
427 int ret;
428
429 ret = reservation_object_get_fences_rcu(resv,
430 &excl, &count, &shared);
431 if (ret)
432 return ret;
433
434 for (i = 0; i < count; i++) {
435 timeout = i915_gem_object_wait_fence(shared[i],
436 flags, timeout,
437 rps);
Chris Wilsond892e932017-02-12 21:53:43 +0000438 if (timeout < 0)
Chris Wilsone95433c2016-10-28 13:58:27 +0100439 break;
440
441 dma_fence_put(shared[i]);
442 }
443
444 for (; i < count; i++)
445 dma_fence_put(shared[i]);
446 kfree(shared);
Chris Wilsone54ca972017-02-17 15:13:04 +0000447
448 prune_fences = count && timeout >= 0;
Chris Wilsone95433c2016-10-28 13:58:27 +0100449 } else {
450 excl = reservation_object_get_excl_rcu(resv);
451 }
452
Chris Wilsone54ca972017-02-17 15:13:04 +0000453 if (excl && timeout >= 0) {
Chris Wilsone95433c2016-10-28 13:58:27 +0100454 timeout = i915_gem_object_wait_fence(excl, flags, timeout, rps);
Chris Wilsone54ca972017-02-17 15:13:04 +0000455 prune_fences = timeout >= 0;
456 }
Chris Wilsone95433c2016-10-28 13:58:27 +0100457
458 dma_fence_put(excl);
459
Chris Wilson03d1cac2017-03-08 13:26:28 +0000460 /* Oportunistically prune the fences iff we know they have *all* been
461 * signaled and that the reservation object has not been changed (i.e.
462 * no new fences have been added).
463 */
Chris Wilsone54ca972017-02-17 15:13:04 +0000464 if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) {
Chris Wilson03d1cac2017-03-08 13:26:28 +0000465 if (reservation_object_trylock(resv)) {
466 if (!__read_seqcount_retry(&resv->seq, seq))
467 reservation_object_add_excl_fence(resv, NULL);
468 reservation_object_unlock(resv);
469 }
Chris Wilsone54ca972017-02-17 15:13:04 +0000470 }
471
Chris Wilsone95433c2016-10-28 13:58:27 +0100472 return timeout;
473}
474
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000475static void __fence_set_priority(struct dma_fence *fence, int prio)
476{
477 struct drm_i915_gem_request *rq;
478 struct intel_engine_cs *engine;
479
480 if (!dma_fence_is_i915(fence))
481 return;
482
483 rq = to_request(fence);
484 engine = rq->engine;
485 if (!engine->schedule)
486 return;
487
488 engine->schedule(rq, prio);
489}
490
491static void fence_set_priority(struct dma_fence *fence, int prio)
492{
493 /* Recurse once into a fence-array */
494 if (dma_fence_is_array(fence)) {
495 struct dma_fence_array *array = to_dma_fence_array(fence);
496 int i;
497
498 for (i = 0; i < array->num_fences; i++)
499 __fence_set_priority(array->fences[i], prio);
500 } else {
501 __fence_set_priority(fence, prio);
502 }
503}
504
505int
506i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
507 unsigned int flags,
508 int prio)
509{
510 struct dma_fence *excl;
511
512 if (flags & I915_WAIT_ALL) {
513 struct dma_fence **shared;
514 unsigned int count, i;
515 int ret;
516
517 ret = reservation_object_get_fences_rcu(obj->resv,
518 &excl, &count, &shared);
519 if (ret)
520 return ret;
521
522 for (i = 0; i < count; i++) {
523 fence_set_priority(shared[i], prio);
524 dma_fence_put(shared[i]);
525 }
526
527 kfree(shared);
528 } else {
529 excl = reservation_object_get_excl_rcu(obj->resv);
530 }
531
532 if (excl) {
533 fence_set_priority(excl, prio);
534 dma_fence_put(excl);
535 }
536 return 0;
537}
538
Chris Wilson00e60f22016-08-04 16:32:40 +0100539/**
Chris Wilsone95433c2016-10-28 13:58:27 +0100540 * Waits for rendering to the object to be completed
Chris Wilson00e60f22016-08-04 16:32:40 +0100541 * @obj: i915 gem object
Chris Wilsone95433c2016-10-28 13:58:27 +0100542 * @flags: how to wait (under a lock, for all rendering or just for writes etc)
543 * @timeout: how long to wait
544 * @rps: client (user process) to charge for any waitboosting
Chris Wilson00e60f22016-08-04 16:32:40 +0100545 */
546int
Chris Wilsone95433c2016-10-28 13:58:27 +0100547i915_gem_object_wait(struct drm_i915_gem_object *obj,
548 unsigned int flags,
549 long timeout,
550 struct intel_rps_client *rps)
Chris Wilson00e60f22016-08-04 16:32:40 +0100551{
Chris Wilsone95433c2016-10-28 13:58:27 +0100552 might_sleep();
553#if IS_ENABLED(CONFIG_LOCKDEP)
554 GEM_BUG_ON(debug_locks &&
555 !!lockdep_is_held(&obj->base.dev->struct_mutex) !=
556 !!(flags & I915_WAIT_LOCKED));
557#endif
558 GEM_BUG_ON(timeout < 0);
Chris Wilson00e60f22016-08-04 16:32:40 +0100559
Chris Wilsond07f0e52016-10-28 13:58:44 +0100560 timeout = i915_gem_object_wait_reservation(obj->resv,
561 flags, timeout,
562 rps);
Chris Wilsone95433c2016-10-28 13:58:27 +0100563 return timeout < 0 ? timeout : 0;
Chris Wilson00e60f22016-08-04 16:32:40 +0100564}
565
566static struct intel_rps_client *to_rps_client(struct drm_file *file)
567{
568 struct drm_i915_file_private *fpriv = file->driver_priv;
569
570 return &fpriv->rps;
571}
572
Chris Wilson00731152014-05-21 12:42:56 +0100573int
574i915_gem_object_attach_phys(struct drm_i915_gem_object *obj,
575 int align)
576{
Chris Wilson6a2c4232014-11-04 04:51:40 -0800577 int ret;
Chris Wilson00731152014-05-21 12:42:56 +0100578
Chris Wilsondbb43512016-12-07 13:34:11 +0000579 if (align > obj->base.size)
580 return -EINVAL;
Chris Wilson00731152014-05-21 12:42:56 +0100581
Chris Wilsondbb43512016-12-07 13:34:11 +0000582 if (obj->ops == &i915_gem_phys_ops)
Chris Wilson00731152014-05-21 12:42:56 +0100583 return 0;
Chris Wilson00731152014-05-21 12:42:56 +0100584
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100585 if (obj->mm.madv != I915_MADV_WILLNEED)
Chris Wilson00731152014-05-21 12:42:56 +0100586 return -EFAULT;
587
588 if (obj->base.filp == NULL)
589 return -EINVAL;
590
Chris Wilson4717ca92016-08-04 07:52:28 +0100591 ret = i915_gem_object_unbind(obj);
592 if (ret)
593 return ret;
594
Chris Wilson548625e2016-11-01 12:11:34 +0000595 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100596 if (obj->mm.pages)
597 return -EBUSY;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800598
Chris Wilson581ab1f2017-02-15 16:39:00 +0000599 GEM_BUG_ON(obj->ops != &i915_gem_object_ops);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800600 obj->ops = &i915_gem_phys_ops;
601
Chris Wilson581ab1f2017-02-15 16:39:00 +0000602 ret = i915_gem_object_pin_pages(obj);
603 if (ret)
604 goto err_xfer;
605
606 return 0;
607
608err_xfer:
609 obj->ops = &i915_gem_object_ops;
610 return ret;
Chris Wilson00731152014-05-21 12:42:56 +0100611}
612
613static int
614i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
615 struct drm_i915_gem_pwrite *args,
Chris Wilson03ac84f2016-10-28 13:58:36 +0100616 struct drm_file *file)
Chris Wilson00731152014-05-21 12:42:56 +0100617{
Chris Wilson00731152014-05-21 12:42:56 +0100618 void *vaddr = obj->phys_handle->vaddr + args->offset;
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300619 char __user *user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800620
621 /* We manually control the domain here and pretend that it
622 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
623 */
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -0700624 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000625 if (copy_from_user(vaddr, user_data, args->size))
626 return -EFAULT;
Chris Wilson00731152014-05-21 12:42:56 +0100627
Chris Wilson6a2c4232014-11-04 04:51:40 -0800628 drm_clflush_virt_range(vaddr, args->size);
Chris Wilson10466d22017-01-06 15:22:38 +0000629 i915_gem_chipset_flush(to_i915(obj->base.dev));
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200630
Chris Wilsond59b21e2017-02-22 11:40:49 +0000631 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000632 return 0;
Chris Wilson00731152014-05-21 12:42:56 +0100633}
634
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000635void *i915_gem_object_alloc(struct drm_i915_private *dev_priv)
Chris Wilson42dcedd2012-11-15 11:32:30 +0000636{
Chris Wilsonefab6d82015-04-07 16:20:57 +0100637 return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000638}
639
640void i915_gem_object_free(struct drm_i915_gem_object *obj)
641{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100642 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonefab6d82015-04-07 16:20:57 +0100643 kmem_cache_free(dev_priv->objects, obj);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000644}
645
Dave Airlieff72145b2011-02-07 12:16:14 +1000646static int
647i915_gem_create(struct drm_file *file,
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000648 struct drm_i915_private *dev_priv,
Dave Airlieff72145b2011-02-07 12:16:14 +1000649 uint64_t size,
650 uint32_t *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700651{
Chris Wilson05394f32010-11-08 19:18:58 +0000652 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300653 int ret;
654 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700655
Dave Airlieff72145b2011-02-07 12:16:14 +1000656 size = roundup(size, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200657 if (size == 0)
658 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700659
660 /* Allocate the new object */
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000661 obj = i915_gem_object_create(dev_priv, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100662 if (IS_ERR(obj))
663 return PTR_ERR(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700664
Chris Wilson05394f32010-11-08 19:18:58 +0000665 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100666 /* drop reference from allocate - handle holds it now */
Chris Wilsonf0cd5182016-10-28 13:58:43 +0100667 i915_gem_object_put(obj);
Daniel Vetterd861e332013-07-24 23:25:03 +0200668 if (ret)
669 return ret;
Chris Wilson202f2fe2010-10-14 13:20:40 +0100670
Dave Airlieff72145b2011-02-07 12:16:14 +1000671 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700672 return 0;
673}
674
Dave Airlieff72145b2011-02-07 12:16:14 +1000675int
676i915_gem_dumb_create(struct drm_file *file,
677 struct drm_device *dev,
678 struct drm_mode_create_dumb *args)
679{
680 /* have to work out size/pitch and return them */
Paulo Zanonide45eaf2013-10-18 18:48:24 -0300681 args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000682 args->size = args->pitch * args->height;
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000683 return i915_gem_create(file, to_i915(dev),
Dave Airlieda6b51d2014-12-24 13:11:17 +1000684 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000685}
686
Dave Airlieff72145b2011-02-07 12:16:14 +1000687/**
688 * Creates a new mm object and returns a handle to it.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100689 * @dev: drm device pointer
690 * @data: ioctl data blob
691 * @file: drm file pointer
Dave Airlieff72145b2011-02-07 12:16:14 +1000692 */
693int
694i915_gem_create_ioctl(struct drm_device *dev, void *data,
695 struct drm_file *file)
696{
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000697 struct drm_i915_private *dev_priv = to_i915(dev);
Dave Airlieff72145b2011-02-07 12:16:14 +1000698 struct drm_i915_gem_create *args = data;
Daniel Vetter63ed2cb2012-04-23 16:50:50 +0200699
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000700 i915_gem_flush_free_objects(dev_priv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +0100701
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000702 return i915_gem_create(file, dev_priv,
Dave Airlieda6b51d2014-12-24 13:11:17 +1000703 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000704}
705
Chris Wilsonef749212017-04-12 12:01:10 +0100706static inline enum fb_op_origin
707fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain)
708{
709 return (domain == I915_GEM_DOMAIN_GTT ?
710 obj->frontbuffer_ggtt_origin : ORIGIN_CPU);
711}
712
713static void
714flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains)
715{
716 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
717
718 if (!(obj->base.write_domain & flush_domains))
719 return;
720
721 /* No actual flushing is required for the GTT write domain. Writes
722 * to it "immediately" go to main memory as far as we know, so there's
723 * no chipset flush. It also doesn't land in render cache.
724 *
725 * However, we do have to enforce the order so that all writes through
726 * the GTT land before any writes to the device, such as updates to
727 * the GATT itself.
728 *
729 * We also have to wait a bit for the writes to land from the GTT.
730 * An uncached read (i.e. mmio) seems to be ideal for the round-trip
731 * timing. This issue has only been observed when switching quickly
732 * between GTT writes and CPU reads from inside the kernel on recent hw,
733 * and it appears to only affect discrete GTT blocks (i.e. on LLC
734 * system agents we cannot reproduce this behaviour).
735 */
736 wmb();
737
738 switch (obj->base.write_domain) {
739 case I915_GEM_DOMAIN_GTT:
740 if (INTEL_GEN(dev_priv) >= 6 && !HAS_LLC(dev_priv)) {
741 if (intel_runtime_pm_get_if_in_use(dev_priv)) {
742 spin_lock_irq(&dev_priv->uncore.lock);
743 POSTING_READ_FW(RING_ACTHD(dev_priv->engine[RCS]->mmio_base));
744 spin_unlock_irq(&dev_priv->uncore.lock);
745 intel_runtime_pm_put(dev_priv);
746 }
747 }
748
749 intel_fb_obj_flush(obj,
750 fb_write_origin(obj, I915_GEM_DOMAIN_GTT));
751 break;
752
753 case I915_GEM_DOMAIN_CPU:
754 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
755 break;
756 }
757
758 obj->base.write_domain = 0;
759}
760
Daniel Vetter8c599672011-12-14 13:57:31 +0100761static inline int
Daniel Vetter8461d222011-12-14 13:57:32 +0100762__copy_to_user_swizzled(char __user *cpu_vaddr,
763 const char *gpu_vaddr, int gpu_offset,
764 int length)
765{
766 int ret, cpu_offset = 0;
767
768 while (length > 0) {
769 int cacheline_end = ALIGN(gpu_offset + 1, 64);
770 int this_length = min(cacheline_end - gpu_offset, length);
771 int swizzled_gpu_offset = gpu_offset ^ 64;
772
773 ret = __copy_to_user(cpu_vaddr + cpu_offset,
774 gpu_vaddr + swizzled_gpu_offset,
775 this_length);
776 if (ret)
777 return ret + length;
778
779 cpu_offset += this_length;
780 gpu_offset += this_length;
781 length -= this_length;
782 }
783
784 return 0;
785}
786
787static inline int
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700788__copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
789 const char __user *cpu_vaddr,
Daniel Vetter8c599672011-12-14 13:57:31 +0100790 int length)
791{
792 int ret, cpu_offset = 0;
793
794 while (length > 0) {
795 int cacheline_end = ALIGN(gpu_offset + 1, 64);
796 int this_length = min(cacheline_end - gpu_offset, length);
797 int swizzled_gpu_offset = gpu_offset ^ 64;
798
799 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
800 cpu_vaddr + cpu_offset,
801 this_length);
802 if (ret)
803 return ret + length;
804
805 cpu_offset += this_length;
806 gpu_offset += this_length;
807 length -= this_length;
808 }
809
810 return 0;
811}
812
Brad Volkin4c914c02014-02-18 10:15:45 -0800813/*
814 * Pins the specified object's pages and synchronizes the object with
815 * GPU accesses. Sets needs_clflush to non-zero if the caller should
816 * flush the object from the CPU cache.
817 */
818int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
Chris Wilson43394c72016-08-18 17:16:47 +0100819 unsigned int *needs_clflush)
Brad Volkin4c914c02014-02-18 10:15:45 -0800820{
821 int ret;
822
Chris Wilsone95433c2016-10-28 13:58:27 +0100823 lockdep_assert_held(&obj->base.dev->struct_mutex);
Brad Volkin4c914c02014-02-18 10:15:45 -0800824
Chris Wilsone95433c2016-10-28 13:58:27 +0100825 *needs_clflush = 0;
Chris Wilson43394c72016-08-18 17:16:47 +0100826 if (!i915_gem_object_has_struct_page(obj))
827 return -ENODEV;
Brad Volkin4c914c02014-02-18 10:15:45 -0800828
Chris Wilsone95433c2016-10-28 13:58:27 +0100829 ret = i915_gem_object_wait(obj,
830 I915_WAIT_INTERRUPTIBLE |
831 I915_WAIT_LOCKED,
832 MAX_SCHEDULE_TIMEOUT,
833 NULL);
Chris Wilsonc13d87e2016-07-20 09:21:15 +0100834 if (ret)
835 return ret;
836
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100837 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100838 if (ret)
839 return ret;
840
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000841 if (i915_gem_object_is_coherent(obj) ||
842 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
843 ret = i915_gem_object_set_to_cpu_domain(obj, false);
844 if (ret)
845 goto err_unpin;
846 else
847 goto out;
848 }
849
Chris Wilsonef749212017-04-12 12:01:10 +0100850 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100851
Chris Wilson43394c72016-08-18 17:16:47 +0100852 /* If we're not in the cpu read domain, set ourself into the gtt
853 * read domain and manually flush cachelines (if required). This
854 * optimizes for the case when the gpu will dirty the data
855 * anyway again before the next pread happens.
856 */
857 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000858 *needs_clflush = CLFLUSH_BEFORE;
Brad Volkin4c914c02014-02-18 10:15:45 -0800859
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000860out:
Chris Wilson97649512016-08-18 17:16:50 +0100861 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100862 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100863
864err_unpin:
865 i915_gem_object_unpin_pages(obj);
866 return ret;
Chris Wilson43394c72016-08-18 17:16:47 +0100867}
868
869int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
870 unsigned int *needs_clflush)
871{
872 int ret;
873
Chris Wilsone95433c2016-10-28 13:58:27 +0100874 lockdep_assert_held(&obj->base.dev->struct_mutex);
875
Chris Wilson43394c72016-08-18 17:16:47 +0100876 *needs_clflush = 0;
877 if (!i915_gem_object_has_struct_page(obj))
878 return -ENODEV;
879
Chris Wilsone95433c2016-10-28 13:58:27 +0100880 ret = i915_gem_object_wait(obj,
881 I915_WAIT_INTERRUPTIBLE |
882 I915_WAIT_LOCKED |
883 I915_WAIT_ALL,
884 MAX_SCHEDULE_TIMEOUT,
885 NULL);
Chris Wilson43394c72016-08-18 17:16:47 +0100886 if (ret)
887 return ret;
888
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100889 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100890 if (ret)
891 return ret;
892
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000893 if (i915_gem_object_is_coherent(obj) ||
894 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
895 ret = i915_gem_object_set_to_cpu_domain(obj, true);
896 if (ret)
897 goto err_unpin;
898 else
899 goto out;
900 }
901
Chris Wilsonef749212017-04-12 12:01:10 +0100902 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100903
Chris Wilson43394c72016-08-18 17:16:47 +0100904 /* If we're not in the cpu write domain, set ourself into the
905 * gtt write domain and manually flush cachelines (as required).
906 * This optimizes for the case when the gpu will use the data
907 * right away and we therefore have to clflush anyway.
908 */
909 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000910 *needs_clflush |= CLFLUSH_AFTER;
Chris Wilson43394c72016-08-18 17:16:47 +0100911
912 /* Same trick applies to invalidate partially written cachelines read
913 * before writing.
914 */
915 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000916 *needs_clflush |= CLFLUSH_BEFORE;
Chris Wilson43394c72016-08-18 17:16:47 +0100917
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000918out:
Chris Wilson43394c72016-08-18 17:16:47 +0100919 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100920 obj->mm.dirty = true;
Chris Wilson97649512016-08-18 17:16:50 +0100921 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100922 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100923
924err_unpin:
925 i915_gem_object_unpin_pages(obj);
926 return ret;
Brad Volkin4c914c02014-02-18 10:15:45 -0800927}
928
Daniel Vetter23c18c72012-03-25 19:47:42 +0200929static void
930shmem_clflush_swizzled_range(char *addr, unsigned long length,
931 bool swizzled)
932{
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200933 if (unlikely(swizzled)) {
Daniel Vetter23c18c72012-03-25 19:47:42 +0200934 unsigned long start = (unsigned long) addr;
935 unsigned long end = (unsigned long) addr + length;
936
937 /* For swizzling simply ensure that we always flush both
938 * channels. Lame, but simple and it works. Swizzled
939 * pwrite/pread is far from a hotpath - current userspace
940 * doesn't use it at all. */
941 start = round_down(start, 128);
942 end = round_up(end, 128);
943
944 drm_clflush_virt_range((void *)start, end - start);
945 } else {
946 drm_clflush_virt_range(addr, length);
947 }
948
949}
950
Daniel Vetterd174bd62012-03-25 19:47:40 +0200951/* Only difference to the fast-path function is that this can handle bit17
952 * and uses non-atomic copy and kmap functions. */
953static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100954shmem_pread_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +0200955 char __user *user_data,
956 bool page_do_bit17_swizzling, bool needs_clflush)
957{
958 char *vaddr;
959 int ret;
960
961 vaddr = kmap(page);
962 if (needs_clflush)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100963 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +0200964 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200965
966 if (page_do_bit17_swizzling)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100967 ret = __copy_to_user_swizzled(user_data, vaddr, offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200968 else
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100969 ret = __copy_to_user(user_data, vaddr + offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200970 kunmap(page);
971
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100972 return ret ? - EFAULT : 0;
Daniel Vetterd174bd62012-03-25 19:47:40 +0200973}
974
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100975static int
976shmem_pread(struct page *page, int offset, int length, char __user *user_data,
977 bool page_do_bit17_swizzling, bool needs_clflush)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530978{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100979 int ret;
980
981 ret = -ENODEV;
982 if (!page_do_bit17_swizzling) {
983 char *vaddr = kmap_atomic(page);
984
985 if (needs_clflush)
986 drm_clflush_virt_range(vaddr + offset, length);
987 ret = __copy_to_user_inatomic(user_data, vaddr + offset, length);
988 kunmap_atomic(vaddr);
989 }
990 if (ret == 0)
991 return 0;
992
993 return shmem_pread_slow(page, offset, length, user_data,
994 page_do_bit17_swizzling, needs_clflush);
995}
996
997static int
998i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
999 struct drm_i915_gem_pread *args)
1000{
1001 char __user *user_data;
1002 u64 remain;
1003 unsigned int obj_do_bit17_swizzling;
1004 unsigned int needs_clflush;
1005 unsigned int idx, offset;
1006 int ret;
1007
1008 obj_do_bit17_swizzling = 0;
1009 if (i915_gem_object_needs_bit17_swizzle(obj))
1010 obj_do_bit17_swizzling = BIT(17);
1011
1012 ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex);
1013 if (ret)
1014 return ret;
1015
1016 ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
1017 mutex_unlock(&obj->base.dev->struct_mutex);
1018 if (ret)
1019 return ret;
1020
1021 remain = args->size;
1022 user_data = u64_to_user_ptr(args->data_ptr);
1023 offset = offset_in_page(args->offset);
1024 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1025 struct page *page = i915_gem_object_get_page(obj, idx);
1026 int length;
1027
1028 length = remain;
1029 if (offset + length > PAGE_SIZE)
1030 length = PAGE_SIZE - offset;
1031
1032 ret = shmem_pread(page, offset, length, user_data,
1033 page_to_phys(page) & obj_do_bit17_swizzling,
1034 needs_clflush);
1035 if (ret)
1036 break;
1037
1038 remain -= length;
1039 user_data += length;
1040 offset = 0;
1041 }
1042
1043 i915_gem_obj_finish_shmem_access(obj);
1044 return ret;
1045}
1046
1047static inline bool
1048gtt_user_read(struct io_mapping *mapping,
1049 loff_t base, int offset,
1050 char __user *user_data, int length)
1051{
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301052 void *vaddr;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001053 unsigned long unwritten;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301054
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301055 /* We can use the cpu mem copy function because this is X86. */
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001056 vaddr = (void __force *)io_mapping_map_atomic_wc(mapping, base);
1057 unwritten = __copy_to_user_inatomic(user_data, vaddr + offset, length);
1058 io_mapping_unmap_atomic(vaddr);
1059 if (unwritten) {
1060 vaddr = (void __force *)
1061 io_mapping_map_wc(mapping, base, PAGE_SIZE);
1062 unwritten = copy_to_user(user_data, vaddr + offset, length);
1063 io_mapping_unmap(vaddr);
1064 }
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301065 return unwritten;
1066}
1067
1068static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001069i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
1070 const struct drm_i915_gem_pread *args)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301071{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001072 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1073 struct i915_ggtt *ggtt = &i915->ggtt;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301074 struct drm_mm_node node;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001075 struct i915_vma *vma;
1076 void __user *user_data;
1077 u64 remain, offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301078 int ret;
1079
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001080 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1081 if (ret)
1082 return ret;
1083
1084 intel_runtime_pm_get(i915);
1085 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
1086 PIN_MAPPABLE | PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001087 if (!IS_ERR(vma)) {
1088 node.start = i915_ggtt_offset(vma);
1089 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001090 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001091 if (ret) {
1092 i915_vma_unpin(vma);
1093 vma = ERR_PTR(ret);
1094 }
1095 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001096 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001097 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301098 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001099 goto out_unlock;
1100 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301101 }
1102
1103 ret = i915_gem_object_set_to_gtt_domain(obj, false);
1104 if (ret)
1105 goto out_unpin;
1106
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001107 mutex_unlock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301108
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001109 user_data = u64_to_user_ptr(args->data_ptr);
1110 remain = args->size;
1111 offset = args->offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301112
1113 while (remain > 0) {
1114 /* Operation in this page
1115 *
1116 * page_base = page offset within aperture
1117 * page_offset = offset within page
1118 * page_length = bytes to copy for this page
1119 */
1120 u32 page_base = node.start;
1121 unsigned page_offset = offset_in_page(offset);
1122 unsigned page_length = PAGE_SIZE - page_offset;
1123 page_length = remain < page_length ? remain : page_length;
1124 if (node.allocated) {
1125 wmb();
1126 ggtt->base.insert_page(&ggtt->base,
1127 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001128 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301129 wmb();
1130 } else {
1131 page_base += offset & PAGE_MASK;
1132 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001133
1134 if (gtt_user_read(&ggtt->mappable, page_base, page_offset,
1135 user_data, page_length)) {
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301136 ret = -EFAULT;
1137 break;
1138 }
1139
1140 remain -= page_length;
1141 user_data += page_length;
1142 offset += page_length;
1143 }
1144
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001145 mutex_lock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301146out_unpin:
1147 if (node.allocated) {
1148 wmb();
1149 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001150 node.start, node.size);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301151 remove_mappable_node(&node);
1152 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001153 i915_vma_unpin(vma);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301154 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001155out_unlock:
1156 intel_runtime_pm_put(i915);
1157 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +01001158
Eric Anholteb014592009-03-10 11:44:52 -07001159 return ret;
1160}
1161
Eric Anholt673a3942008-07-30 12:06:12 -07001162/**
1163 * Reads data from the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001164 * @dev: drm device pointer
1165 * @data: ioctl data blob
1166 * @file: drm file pointer
Eric Anholt673a3942008-07-30 12:06:12 -07001167 *
1168 * On error, the contents of *data are undefined.
1169 */
1170int
1171i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001172 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001173{
1174 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001175 struct drm_i915_gem_object *obj;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001176 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001177
Chris Wilson51311d02010-11-17 09:10:42 +00001178 if (args->size == 0)
1179 return 0;
1180
1181 if (!access_ok(VERIFY_WRITE,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001182 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001183 args->size))
1184 return -EFAULT;
1185
Chris Wilson03ac0642016-07-20 13:31:51 +01001186 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001187 if (!obj)
1188 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001189
Chris Wilson7dcd2492010-09-26 20:21:44 +01001190 /* Bounds check source. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001191 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001192 ret = -EINVAL;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001193 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001194 }
1195
Chris Wilsondb53a302011-02-03 11:57:46 +00001196 trace_i915_gem_object_pread(obj, args->offset, args->size);
1197
Chris Wilsone95433c2016-10-28 13:58:27 +01001198 ret = i915_gem_object_wait(obj,
1199 I915_WAIT_INTERRUPTIBLE,
1200 MAX_SCHEDULE_TIMEOUT,
1201 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001202 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001203 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001204
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001205 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001206 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001207 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001208
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001209 ret = i915_gem_shmem_pread(obj, args);
Chris Wilson9c870d02016-10-24 13:42:15 +01001210 if (ret == -EFAULT || ret == -ENODEV)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001211 ret = i915_gem_gtt_pread(obj, args);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301212
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001213 i915_gem_object_unpin_pages(obj);
1214out:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001215 i915_gem_object_put(obj);
Eric Anholteb014592009-03-10 11:44:52 -07001216 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001217}
1218
Keith Packard0839ccb2008-10-30 19:38:48 -07001219/* This is the fast write path which cannot handle
1220 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001221 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001222
Chris Wilsonfe115622016-10-28 13:58:40 +01001223static inline bool
1224ggtt_write(struct io_mapping *mapping,
1225 loff_t base, int offset,
1226 char __user *user_data, int length)
Keith Packard0839ccb2008-10-30 19:38:48 -07001227{
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -07001228 void *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -07001229 unsigned long unwritten;
1230
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -07001231 /* We can use the cpu mem copy function because this is X86. */
Chris Wilsonfe115622016-10-28 13:58:40 +01001232 vaddr = (void __force *)io_mapping_map_atomic_wc(mapping, base);
1233 unwritten = __copy_from_user_inatomic_nocache(vaddr + offset,
Keith Packard0839ccb2008-10-30 19:38:48 -07001234 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001235 io_mapping_unmap_atomic(vaddr);
1236 if (unwritten) {
1237 vaddr = (void __force *)
1238 io_mapping_map_wc(mapping, base, PAGE_SIZE);
1239 unwritten = copy_from_user(vaddr + offset, user_data, length);
1240 io_mapping_unmap(vaddr);
1241 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001242
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001243 return unwritten;
1244}
1245
Eric Anholt3de09aa2009-03-09 09:42:23 -07001246/**
1247 * This is the fast pwrite path, where we copy the data directly from the
1248 * user into the GTT, uncached.
Chris Wilsonfe115622016-10-28 13:58:40 +01001249 * @obj: i915 GEM object
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001250 * @args: pwrite arguments structure
Eric Anholt3de09aa2009-03-09 09:42:23 -07001251 */
Eric Anholt673a3942008-07-30 12:06:12 -07001252static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001253i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
1254 const struct drm_i915_gem_pwrite *args)
Eric Anholt673a3942008-07-30 12:06:12 -07001255{
Chris Wilsonfe115622016-10-28 13:58:40 +01001256 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301257 struct i915_ggtt *ggtt = &i915->ggtt;
1258 struct drm_mm_node node;
Chris Wilsonfe115622016-10-28 13:58:40 +01001259 struct i915_vma *vma;
1260 u64 remain, offset;
1261 void __user *user_data;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301262 int ret;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301263
Chris Wilsonfe115622016-10-28 13:58:40 +01001264 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1265 if (ret)
1266 return ret;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001267
Chris Wilson9c870d02016-10-24 13:42:15 +01001268 intel_runtime_pm_get(i915);
Chris Wilson058d88c2016-08-15 10:49:06 +01001269 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsonde895082016-08-04 16:32:34 +01001270 PIN_MAPPABLE | PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001271 if (!IS_ERR(vma)) {
1272 node.start = i915_ggtt_offset(vma);
1273 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001274 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001275 if (ret) {
1276 i915_vma_unpin(vma);
1277 vma = ERR_PTR(ret);
1278 }
1279 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001280 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001281 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301282 if (ret)
Chris Wilsonfe115622016-10-28 13:58:40 +01001283 goto out_unlock;
1284 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301285 }
Daniel Vetter935aaa62012-03-25 19:47:35 +02001286
1287 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1288 if (ret)
1289 goto out_unpin;
1290
Chris Wilsonfe115622016-10-28 13:58:40 +01001291 mutex_unlock(&i915->drm.struct_mutex);
1292
Chris Wilsonb19482d2016-08-18 17:16:43 +01001293 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Paulo Zanoni063e4e62015-02-13 17:23:45 -02001294
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301295 user_data = u64_to_user_ptr(args->data_ptr);
1296 offset = args->offset;
1297 remain = args->size;
1298 while (remain) {
Eric Anholt673a3942008-07-30 12:06:12 -07001299 /* Operation in this page
1300 *
Keith Packard0839ccb2008-10-30 19:38:48 -07001301 * page_base = page offset within aperture
1302 * page_offset = offset within page
1303 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -07001304 */
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301305 u32 page_base = node.start;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001306 unsigned int page_offset = offset_in_page(offset);
1307 unsigned int page_length = PAGE_SIZE - page_offset;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301308 page_length = remain < page_length ? remain : page_length;
1309 if (node.allocated) {
1310 wmb(); /* flush the write before we modify the GGTT */
1311 ggtt->base.insert_page(&ggtt->base,
1312 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
1313 node.start, I915_CACHE_NONE, 0);
1314 wmb(); /* flush modifications to the GGTT (insert_page) */
1315 } else {
1316 page_base += offset & PAGE_MASK;
1317 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001318 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -07001319 * source page isn't available. Return the error and we'll
1320 * retry in the slow path.
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301321 * If the object is non-shmem backed, we retry again with the
1322 * path that handles page fault.
Keith Packard0839ccb2008-10-30 19:38:48 -07001323 */
Chris Wilsonfe115622016-10-28 13:58:40 +01001324 if (ggtt_write(&ggtt->mappable, page_base, page_offset,
1325 user_data, page_length)) {
1326 ret = -EFAULT;
1327 break;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001328 }
Eric Anholt673a3942008-07-30 12:06:12 -07001329
Keith Packard0839ccb2008-10-30 19:38:48 -07001330 remain -= page_length;
1331 user_data += page_length;
1332 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -07001333 }
Chris Wilsond59b21e2017-02-22 11:40:49 +00001334 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001335
1336 mutex_lock(&i915->drm.struct_mutex);
Daniel Vetter935aaa62012-03-25 19:47:35 +02001337out_unpin:
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301338 if (node.allocated) {
1339 wmb();
1340 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001341 node.start, node.size);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301342 remove_mappable_node(&node);
1343 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001344 i915_vma_unpin(vma);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301345 }
Chris Wilsonfe115622016-10-28 13:58:40 +01001346out_unlock:
Chris Wilson9c870d02016-10-24 13:42:15 +01001347 intel_runtime_pm_put(i915);
Chris Wilsonfe115622016-10-28 13:58:40 +01001348 mutex_unlock(&i915->drm.struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -07001349 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001350}
1351
Eric Anholt673a3942008-07-30 12:06:12 -07001352static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001353shmem_pwrite_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +02001354 char __user *user_data,
1355 bool page_do_bit17_swizzling,
1356 bool needs_clflush_before,
1357 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -07001358{
Daniel Vetterd174bd62012-03-25 19:47:40 +02001359 char *vaddr;
1360 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001361
Daniel Vetterd174bd62012-03-25 19:47:40 +02001362 vaddr = kmap(page);
Daniel Vettere7e58eb2012-03-25 19:47:43 +02001363 if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
Chris Wilsonfe115622016-10-28 13:58:40 +01001364 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001365 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001366 if (page_do_bit17_swizzling)
Chris Wilsonfe115622016-10-28 13:58:40 +01001367 ret = __copy_from_user_swizzled(vaddr, offset, user_data,
1368 length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001369 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001370 ret = __copy_from_user(vaddr + offset, user_data, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001371 if (needs_clflush_after)
Chris Wilsonfe115622016-10-28 13:58:40 +01001372 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001373 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001374 kunmap(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001375
Chris Wilson755d2212012-09-04 21:02:55 +01001376 return ret ? -EFAULT : 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001377}
1378
Chris Wilsonfe115622016-10-28 13:58:40 +01001379/* Per-page copy function for the shmem pwrite fastpath.
1380 * Flushes invalid cachelines before writing to the target if
1381 * needs_clflush_before is set and flushes out any written cachelines after
1382 * writing if needs_clflush is set.
1383 */
Eric Anholt40123c12009-03-09 13:42:30 -07001384static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001385shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
1386 bool page_do_bit17_swizzling,
1387 bool needs_clflush_before,
1388 bool needs_clflush_after)
Eric Anholt40123c12009-03-09 13:42:30 -07001389{
Chris Wilsonfe115622016-10-28 13:58:40 +01001390 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001391
Chris Wilsonfe115622016-10-28 13:58:40 +01001392 ret = -ENODEV;
1393 if (!page_do_bit17_swizzling) {
1394 char *vaddr = kmap_atomic(page);
1395
1396 if (needs_clflush_before)
1397 drm_clflush_virt_range(vaddr + offset, len);
1398 ret = __copy_from_user_inatomic(vaddr + offset, user_data, len);
1399 if (needs_clflush_after)
1400 drm_clflush_virt_range(vaddr + offset, len);
1401
1402 kunmap_atomic(vaddr);
1403 }
1404 if (ret == 0)
1405 return ret;
1406
1407 return shmem_pwrite_slow(page, offset, len, user_data,
1408 page_do_bit17_swizzling,
1409 needs_clflush_before,
1410 needs_clflush_after);
1411}
1412
1413static int
1414i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
1415 const struct drm_i915_gem_pwrite *args)
1416{
1417 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1418 void __user *user_data;
1419 u64 remain;
1420 unsigned int obj_do_bit17_swizzling;
1421 unsigned int partial_cacheline_write;
1422 unsigned int needs_clflush;
1423 unsigned int offset, idx;
1424 int ret;
1425
1426 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
Chris Wilson43394c72016-08-18 17:16:47 +01001427 if (ret)
1428 return ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001429
Chris Wilsonfe115622016-10-28 13:58:40 +01001430 ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
1431 mutex_unlock(&i915->drm.struct_mutex);
1432 if (ret)
1433 return ret;
1434
1435 obj_do_bit17_swizzling = 0;
1436 if (i915_gem_object_needs_bit17_swizzle(obj))
1437 obj_do_bit17_swizzling = BIT(17);
1438
1439 /* If we don't overwrite a cacheline completely we need to be
1440 * careful to have up-to-date data by first clflushing. Don't
1441 * overcomplicate things and flush the entire patch.
1442 */
1443 partial_cacheline_write = 0;
1444 if (needs_clflush & CLFLUSH_BEFORE)
1445 partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
1446
Chris Wilson43394c72016-08-18 17:16:47 +01001447 user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson43394c72016-08-18 17:16:47 +01001448 remain = args->size;
Chris Wilsonfe115622016-10-28 13:58:40 +01001449 offset = offset_in_page(args->offset);
1450 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1451 struct page *page = i915_gem_object_get_page(obj, idx);
1452 int length;
Eric Anholt40123c12009-03-09 13:42:30 -07001453
Chris Wilsonfe115622016-10-28 13:58:40 +01001454 length = remain;
1455 if (offset + length > PAGE_SIZE)
1456 length = PAGE_SIZE - offset;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001457
Chris Wilsonfe115622016-10-28 13:58:40 +01001458 ret = shmem_pwrite(page, offset, length, user_data,
1459 page_to_phys(page) & obj_do_bit17_swizzling,
1460 (offset | length) & partial_cacheline_write,
1461 needs_clflush & CLFLUSH_AFTER);
1462 if (ret)
Chris Wilson9da3da62012-06-01 15:20:22 +01001463 break;
1464
Chris Wilsonfe115622016-10-28 13:58:40 +01001465 remain -= length;
1466 user_data += length;
1467 offset = 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001468 }
1469
Chris Wilsond59b21e2017-02-22 11:40:49 +00001470 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001471 i915_gem_obj_finish_shmem_access(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001472 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001473}
1474
1475/**
1476 * Writes data to the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001477 * @dev: drm device
1478 * @data: ioctl data blob
1479 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001480 *
1481 * On error, the contents of the buffer that were to be modified are undefined.
1482 */
1483int
1484i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001485 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001486{
1487 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001488 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +00001489 int ret;
1490
1491 if (args->size == 0)
1492 return 0;
1493
1494 if (!access_ok(VERIFY_READ,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001495 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001496 args->size))
1497 return -EFAULT;
1498
Chris Wilson03ac0642016-07-20 13:31:51 +01001499 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001500 if (!obj)
1501 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001502
Chris Wilson7dcd2492010-09-26 20:21:44 +01001503 /* Bounds check destination. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001504 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001505 ret = -EINVAL;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001506 goto err;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001507 }
1508
Chris Wilsondb53a302011-02-03 11:57:46 +00001509 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
1510
Chris Wilson7c55e2c2017-03-07 12:03:38 +00001511 ret = -ENODEV;
1512 if (obj->ops->pwrite)
1513 ret = obj->ops->pwrite(obj, args);
1514 if (ret != -ENODEV)
1515 goto err;
1516
Chris Wilsone95433c2016-10-28 13:58:27 +01001517 ret = i915_gem_object_wait(obj,
1518 I915_WAIT_INTERRUPTIBLE |
1519 I915_WAIT_ALL,
1520 MAX_SCHEDULE_TIMEOUT,
1521 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001522 if (ret)
1523 goto err;
1524
Chris Wilsonfe115622016-10-28 13:58:40 +01001525 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001526 if (ret)
Chris Wilsonfe115622016-10-28 13:58:40 +01001527 goto err;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001528
Daniel Vetter935aaa62012-03-25 19:47:35 +02001529 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -07001530 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1531 * it would end up going through the fenced access, and we'll get
1532 * different detiling behavior between reading and writing.
1533 * pread/pwrite currently are reading and writing from the CPU
1534 * perspective, requiring manual detiling by the client.
1535 */
Chris Wilson6eae0052016-06-20 15:05:52 +01001536 if (!i915_gem_object_has_struct_page(obj) ||
Chris Wilson9c870d02016-10-24 13:42:15 +01001537 cpu_write_needs_clflush(obj))
Daniel Vetter935aaa62012-03-25 19:47:35 +02001538 /* Note that the gtt paths might fail with non-page-backed user
1539 * pointers (e.g. gtt mappings when moving data between
Chris Wilson9c870d02016-10-24 13:42:15 +01001540 * textures). Fallback to the shmem path in that case.
1541 */
Chris Wilsonfe115622016-10-28 13:58:40 +01001542 ret = i915_gem_gtt_pwrite_fast(obj, args);
Eric Anholt673a3942008-07-30 12:06:12 -07001543
Chris Wilsond1054ee2016-07-16 18:42:36 +01001544 if (ret == -EFAULT || ret == -ENOSPC) {
Chris Wilson6a2c4232014-11-04 04:51:40 -08001545 if (obj->phys_handle)
1546 ret = i915_gem_phys_pwrite(obj, args, file);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301547 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001548 ret = i915_gem_shmem_pwrite(obj, args);
Chris Wilson6a2c4232014-11-04 04:51:40 -08001549 }
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001550
Chris Wilsonfe115622016-10-28 13:58:40 +01001551 i915_gem_object_unpin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001552err:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001553 i915_gem_object_put(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001554 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001555}
1556
Chris Wilson40e62d52016-10-28 13:58:41 +01001557static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
1558{
1559 struct drm_i915_private *i915;
1560 struct list_head *list;
1561 struct i915_vma *vma;
1562
1563 list_for_each_entry(vma, &obj->vma_list, obj_link) {
1564 if (!i915_vma_is_ggtt(vma))
Chris Wilson28f412e2016-12-23 14:57:55 +00001565 break;
Chris Wilson40e62d52016-10-28 13:58:41 +01001566
1567 if (i915_vma_is_active(vma))
1568 continue;
1569
1570 if (!drm_mm_node_allocated(&vma->node))
1571 continue;
1572
1573 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
1574 }
1575
1576 i915 = to_i915(obj->base.dev);
1577 list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list;
Joonas Lahtinen56cea322016-11-02 12:16:04 +02001578 list_move_tail(&obj->global_link, list);
Chris Wilson40e62d52016-10-28 13:58:41 +01001579}
1580
Eric Anholt673a3942008-07-30 12:06:12 -07001581/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001582 * Called when user space prepares to use an object with the CPU, either
1583 * through the mmap ioctl's mapping or a GTT mapping.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001584 * @dev: drm device
1585 * @data: ioctl data blob
1586 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001587 */
1588int
1589i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001590 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001591{
1592 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001593 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001594 uint32_t read_domains = args->read_domains;
1595 uint32_t write_domain = args->write_domain;
Chris Wilson40e62d52016-10-28 13:58:41 +01001596 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07001597
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001598 /* Only handle setting domains to types used by the CPU. */
Chris Wilsonb8f90962016-08-05 10:14:07 +01001599 if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001600 return -EINVAL;
1601
1602 /* Having something in the write domain implies it's in the read
1603 * domain, and only that read domain. Enforce that in the request.
1604 */
1605 if (write_domain != 0 && read_domains != write_domain)
1606 return -EINVAL;
1607
Chris Wilson03ac0642016-07-20 13:31:51 +01001608 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001609 if (!obj)
1610 return -ENOENT;
Jesse Barnes652c3932009-08-17 13:31:43 -07001611
Chris Wilson3236f572012-08-24 09:35:09 +01001612 /* Try to flush the object off the GPU without holding the lock.
1613 * We will repeat the flush holding the lock in the normal manner
1614 * to catch cases where we are gazumped.
1615 */
Chris Wilson40e62d52016-10-28 13:58:41 +01001616 err = i915_gem_object_wait(obj,
Chris Wilsone95433c2016-10-28 13:58:27 +01001617 I915_WAIT_INTERRUPTIBLE |
1618 (write_domain ? I915_WAIT_ALL : 0),
1619 MAX_SCHEDULE_TIMEOUT,
1620 to_rps_client(file));
Chris Wilson40e62d52016-10-28 13:58:41 +01001621 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001622 goto out;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001623
Chris Wilson40e62d52016-10-28 13:58:41 +01001624 /* Flush and acquire obj->pages so that we are coherent through
1625 * direct access in memory with previous cached writes through
1626 * shmemfs and that our cache domain tracking remains valid.
1627 * For example, if the obj->filp was moved to swap without us
1628 * being notified and releasing the pages, we would mistakenly
1629 * continue to assume that the obj remained out of the CPU cached
1630 * domain.
1631 */
1632 err = i915_gem_object_pin_pages(obj);
1633 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001634 goto out;
Chris Wilson40e62d52016-10-28 13:58:41 +01001635
1636 err = i915_mutex_lock_interruptible(dev);
1637 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001638 goto out_unpin;
Chris Wilson3236f572012-08-24 09:35:09 +01001639
Chris Wilsone22d8e32017-04-12 12:01:11 +01001640 if (read_domains & I915_GEM_DOMAIN_WC)
1641 err = i915_gem_object_set_to_wc_domain(obj, write_domain);
1642 else if (read_domains & I915_GEM_DOMAIN_GTT)
1643 err = i915_gem_object_set_to_gtt_domain(obj, write_domain);
Chris Wilson43566de2015-01-02 16:29:29 +05301644 else
Chris Wilsone22d8e32017-04-12 12:01:11 +01001645 err = i915_gem_object_set_to_cpu_domain(obj, write_domain);
Chris Wilson40e62d52016-10-28 13:58:41 +01001646
1647 /* And bump the LRU for this access */
1648 i915_gem_object_bump_inactive_ggtt(obj);
1649
1650 mutex_unlock(&dev->struct_mutex);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001651
Daniel Vetter031b6982015-06-26 19:35:16 +02001652 if (write_domain != 0)
Chris Wilsonef749212017-04-12 12:01:10 +01001653 intel_fb_obj_invalidate(obj,
1654 fb_write_origin(obj, write_domain));
Daniel Vetter031b6982015-06-26 19:35:16 +02001655
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001656out_unpin:
Chris Wilson40e62d52016-10-28 13:58:41 +01001657 i915_gem_object_unpin_pages(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001658out:
1659 i915_gem_object_put(obj);
Chris Wilson40e62d52016-10-28 13:58:41 +01001660 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07001661}
1662
1663/**
1664 * Called when user space has done writes to this buffer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001665 * @dev: drm device
1666 * @data: ioctl data blob
1667 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001668 */
1669int
1670i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001671 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001672{
1673 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001674 struct drm_i915_gem_object *obj;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001675
Chris Wilson03ac0642016-07-20 13:31:51 +01001676 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonc21724c2016-08-05 10:14:19 +01001677 if (!obj)
1678 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001679
Eric Anholt673a3942008-07-30 12:06:12 -07001680 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001681 i915_gem_object_flush_if_display(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001682 i915_gem_object_put(obj);
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001683
1684 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001685}
1686
1687/**
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001688 * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
1689 * it is mapped to.
1690 * @dev: drm device
1691 * @data: ioctl data blob
1692 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001693 *
1694 * While the mapping holds a reference on the contents of the object, it doesn't
1695 * imply a ref on the object itself.
Daniel Vetter34367382014-10-16 12:28:18 +02001696 *
1697 * IMPORTANT:
1698 *
1699 * DRM driver writers who look a this function as an example for how to do GEM
1700 * mmap support, please don't implement mmap support like here. The modern way
1701 * to implement DRM mmap support is with an mmap offset ioctl (like
1702 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
1703 * That way debug tooling like valgrind will understand what's going on, hiding
1704 * the mmap call in a driver private ioctl will break that. The i915 driver only
1705 * does cpu mmaps this way because we didn't know better.
Eric Anholt673a3942008-07-30 12:06:12 -07001706 */
1707int
1708i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001709 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001710{
1711 struct drm_i915_gem_mmap *args = data;
Chris Wilson03ac0642016-07-20 13:31:51 +01001712 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001713 unsigned long addr;
1714
Akash Goel1816f922015-01-02 16:29:30 +05301715 if (args->flags & ~(I915_MMAP_WC))
1716 return -EINVAL;
1717
Borislav Petkov568a58e2016-03-29 17:42:01 +02001718 if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
Akash Goel1816f922015-01-02 16:29:30 +05301719 return -ENODEV;
1720
Chris Wilson03ac0642016-07-20 13:31:51 +01001721 obj = i915_gem_object_lookup(file, args->handle);
1722 if (!obj)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001723 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001724
Daniel Vetter1286ff72012-05-10 15:25:09 +02001725 /* prime objects have no backing filp to GEM mmap
1726 * pages from.
1727 */
Chris Wilson03ac0642016-07-20 13:31:51 +01001728 if (!obj->base.filp) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001729 i915_gem_object_put(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02001730 return -EINVAL;
1731 }
1732
Chris Wilson03ac0642016-07-20 13:31:51 +01001733 addr = vm_mmap(obj->base.filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001734 PROT_READ | PROT_WRITE, MAP_SHARED,
1735 args->offset);
Akash Goel1816f922015-01-02 16:29:30 +05301736 if (args->flags & I915_MMAP_WC) {
1737 struct mm_struct *mm = current->mm;
1738 struct vm_area_struct *vma;
1739
Michal Hocko80a89a52016-05-23 16:26:11 -07001740 if (down_write_killable(&mm->mmap_sem)) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001741 i915_gem_object_put(obj);
Michal Hocko80a89a52016-05-23 16:26:11 -07001742 return -EINTR;
1743 }
Akash Goel1816f922015-01-02 16:29:30 +05301744 vma = find_vma(mm, addr);
1745 if (vma)
1746 vma->vm_page_prot =
1747 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
1748 else
1749 addr = -ENOMEM;
1750 up_write(&mm->mmap_sem);
Chris Wilsonaeecc962016-06-17 14:46:39 -03001751
1752 /* This may race, but that's ok, it only gets set */
Chris Wilson50349242016-08-18 17:17:04 +01001753 WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
Akash Goel1816f922015-01-02 16:29:30 +05301754 }
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001755 i915_gem_object_put(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001756 if (IS_ERR((void *)addr))
1757 return addr;
1758
1759 args->addr_ptr = (uint64_t) addr;
1760
1761 return 0;
1762}
1763
Chris Wilson03af84f2016-08-18 17:17:01 +01001764static unsigned int tile_row_pages(struct drm_i915_gem_object *obj)
1765{
Chris Wilson6649a0b2017-01-09 16:16:08 +00001766 return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT;
Chris Wilson03af84f2016-08-18 17:17:01 +01001767}
1768
Jesse Barnesde151cf2008-11-12 10:03:55 -08001769/**
Chris Wilson4cc69072016-08-25 19:05:19 +01001770 * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
1771 *
1772 * A history of the GTT mmap interface:
1773 *
1774 * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
1775 * aligned and suitable for fencing, and still fit into the available
1776 * mappable space left by the pinned display objects. A classic problem
1777 * we called the page-fault-of-doom where we would ping-pong between
1778 * two objects that could not fit inside the GTT and so the memcpy
1779 * would page one object in at the expense of the other between every
1780 * single byte.
1781 *
1782 * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
1783 * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
1784 * object is too large for the available space (or simply too large
1785 * for the mappable aperture!), a view is created instead and faulted
1786 * into userspace. (This view is aligned and sized appropriately for
1787 * fenced access.)
1788 *
Chris Wilsone22d8e32017-04-12 12:01:11 +01001789 * 2 - Recognise WC as a separate cache domain so that we can flush the
1790 * delayed writes via GTT before performing direct access via WC.
1791 *
Chris Wilson4cc69072016-08-25 19:05:19 +01001792 * Restrictions:
1793 *
1794 * * snoopable objects cannot be accessed via the GTT. It can cause machine
1795 * hangs on some architectures, corruption on others. An attempt to service
1796 * a GTT page fault from a snoopable object will generate a SIGBUS.
1797 *
1798 * * the object must be able to fit into RAM (physical memory, though no
1799 * limited to the mappable aperture).
1800 *
1801 *
1802 * Caveats:
1803 *
1804 * * a new GTT page fault will synchronize rendering from the GPU and flush
1805 * all data to system memory. Subsequent access will not be synchronized.
1806 *
1807 * * all mappings are revoked on runtime device suspend.
1808 *
1809 * * there are only 8, 16 or 32 fence registers to share between all users
1810 * (older machines require fence register for display and blitter access
1811 * as well). Contention of the fence registers will cause the previous users
1812 * to be unmapped and any new access will generate new page faults.
1813 *
1814 * * running out of memory while servicing a fault may generate a SIGBUS,
1815 * rather than the expected SIGSEGV.
1816 */
1817int i915_gem_mmap_gtt_version(void)
1818{
Chris Wilsone22d8e32017-04-12 12:01:11 +01001819 return 2;
Chris Wilson4cc69072016-08-25 19:05:19 +01001820}
1821
Chris Wilson2d4281b2017-01-10 09:56:32 +00001822static inline struct i915_ggtt_view
1823compute_partial_view(struct drm_i915_gem_object *obj,
Chris Wilson2d4281b2017-01-10 09:56:32 +00001824 pgoff_t page_offset,
1825 unsigned int chunk)
1826{
1827 struct i915_ggtt_view view;
1828
1829 if (i915_gem_object_is_tiled(obj))
1830 chunk = roundup(chunk, tile_row_pages(obj));
1831
Chris Wilson2d4281b2017-01-10 09:56:32 +00001832 view.type = I915_GGTT_VIEW_PARTIAL;
Chris Wilson8bab11932017-01-14 00:28:25 +00001833 view.partial.offset = rounddown(page_offset, chunk);
1834 view.partial.size =
Chris Wilson2d4281b2017-01-10 09:56:32 +00001835 min_t(unsigned int, chunk,
Chris Wilson8bab11932017-01-14 00:28:25 +00001836 (obj->base.size >> PAGE_SHIFT) - view.partial.offset);
Chris Wilson2d4281b2017-01-10 09:56:32 +00001837
1838 /* If the partial covers the entire object, just create a normal VMA. */
1839 if (chunk >= obj->base.size >> PAGE_SHIFT)
1840 view.type = I915_GGTT_VIEW_NORMAL;
1841
1842 return view;
1843}
1844
Chris Wilson4cc69072016-08-25 19:05:19 +01001845/**
Jesse Barnesde151cf2008-11-12 10:03:55 -08001846 * i915_gem_fault - fault a page into the GTT
Geliang Tangd9072a32015-09-15 05:58:44 -07001847 * @vmf: fault info
Jesse Barnesde151cf2008-11-12 10:03:55 -08001848 *
1849 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1850 * from userspace. The fault handler takes care of binding the object to
1851 * the GTT (if needed), allocating and programming a fence register (again,
1852 * only if needed based on whether the old reg is still valid or the object
1853 * is tiled) and inserting a new PTE into the faulting process.
1854 *
1855 * Note that the faulting process may involve evicting existing objects
1856 * from the GTT and/or fence registers to make room. So performance may
1857 * suffer if the GTT working set is large or there are few fence registers
1858 * left.
Chris Wilson4cc69072016-08-25 19:05:19 +01001859 *
1860 * The current feature set supported by i915_gem_fault() and thus GTT mmaps
1861 * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
Jesse Barnesde151cf2008-11-12 10:03:55 -08001862 */
Dave Jiang11bac802017-02-24 14:56:41 -08001863int i915_gem_fault(struct vm_fault *vmf)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001864{
Chris Wilson03af84f2016-08-18 17:17:01 +01001865#define MIN_CHUNK_PAGES ((1 << 20) >> PAGE_SHIFT) /* 1 MiB */
Dave Jiang11bac802017-02-24 14:56:41 -08001866 struct vm_area_struct *area = vmf->vma;
Chris Wilson058d88c2016-08-15 10:49:06 +01001867 struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
Chris Wilson05394f32010-11-08 19:18:58 +00001868 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001869 struct drm_i915_private *dev_priv = to_i915(dev);
1870 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001871 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Chris Wilson058d88c2016-08-15 10:49:06 +01001872 struct i915_vma *vma;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001873 pgoff_t page_offset;
Chris Wilson82118872016-08-18 17:17:05 +01001874 unsigned int flags;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001875 int ret;
Paulo Zanonif65c9162013-11-27 18:20:34 -02001876
Jesse Barnesde151cf2008-11-12 10:03:55 -08001877 /* We don't use vmf->pgoff since that has the fake offset */
Jan Kara1a29d852016-12-14 15:07:01 -08001878 page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001879
Chris Wilsondb53a302011-02-03 11:57:46 +00001880 trace_i915_gem_object_fault(obj, page_offset, true, write);
1881
Chris Wilson6e4930f2014-02-07 18:37:06 -02001882 /* Try to flush the object off the GPU first without holding the lock.
Chris Wilsonb8f90962016-08-05 10:14:07 +01001883 * Upon acquiring the lock, we will perform our sanity checks and then
Chris Wilson6e4930f2014-02-07 18:37:06 -02001884 * repeat the flush holding the lock in the normal manner to catch cases
1885 * where we are gazumped.
1886 */
Chris Wilsone95433c2016-10-28 13:58:27 +01001887 ret = i915_gem_object_wait(obj,
1888 I915_WAIT_INTERRUPTIBLE,
1889 MAX_SCHEDULE_TIMEOUT,
1890 NULL);
Chris Wilson6e4930f2014-02-07 18:37:06 -02001891 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001892 goto err;
1893
Chris Wilson40e62d52016-10-28 13:58:41 +01001894 ret = i915_gem_object_pin_pages(obj);
1895 if (ret)
1896 goto err;
1897
Chris Wilsonb8f90962016-08-05 10:14:07 +01001898 intel_runtime_pm_get(dev_priv);
1899
1900 ret = i915_mutex_lock_interruptible(dev);
1901 if (ret)
1902 goto err_rpm;
Chris Wilson6e4930f2014-02-07 18:37:06 -02001903
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001904 /* Access to snoopable pages through the GTT is incoherent. */
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00001905 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) {
Chris Wilsonddeff6e2014-05-28 16:16:41 +01001906 ret = -EFAULT;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001907 goto err_unlock;
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001908 }
1909
Chris Wilson82118872016-08-18 17:17:05 +01001910 /* If the object is smaller than a couple of partial vma, it is
1911 * not worth only creating a single partial vma - we may as well
1912 * clear enough space for the full object.
1913 */
1914 flags = PIN_MAPPABLE;
1915 if (obj->base.size > 2 * MIN_CHUNK_PAGES << PAGE_SHIFT)
1916 flags |= PIN_NONBLOCK | PIN_NONFAULT;
1917
Chris Wilsona61007a2016-08-18 17:17:02 +01001918 /* Now pin it into the GTT as needed */
Chris Wilson82118872016-08-18 17:17:05 +01001919 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, flags);
Chris Wilsona61007a2016-08-18 17:17:02 +01001920 if (IS_ERR(vma)) {
Chris Wilsona61007a2016-08-18 17:17:02 +01001921 /* Use a partial view if it is bigger than available space */
Chris Wilson2d4281b2017-01-10 09:56:32 +00001922 struct i915_ggtt_view view =
Chris Wilson8201c1f2017-01-10 09:56:33 +00001923 compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES);
Chris Wilsonaa136d92016-08-18 17:17:03 +01001924
Chris Wilson50349242016-08-18 17:17:04 +01001925 /* Userspace is now writing through an untracked VMA, abandon
1926 * all hope that the hardware is able to track future writes.
1927 */
1928 obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
1929
Chris Wilsona61007a2016-08-18 17:17:02 +01001930 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE);
1931 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001932 if (IS_ERR(vma)) {
1933 ret = PTR_ERR(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001934 goto err_unlock;
Chris Wilson058d88c2016-08-15 10:49:06 +01001935 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001936
Chris Wilsonc9839302012-11-20 10:45:17 +00001937 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1938 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001939 goto err_unpin;
Chris Wilsonc9839302012-11-20 10:45:17 +00001940
Chris Wilson49ef5292016-08-18 17:17:00 +01001941 ret = i915_vma_get_fence(vma);
Chris Wilsonc9839302012-11-20 10:45:17 +00001942 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001943 goto err_unpin;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001944
Chris Wilson275f0392016-10-24 13:42:14 +01001945 /* Mark as being mmapped into userspace for later revocation */
Chris Wilson9c870d02016-10-24 13:42:15 +01001946 assert_rpm_wakelock_held(dev_priv);
Chris Wilson275f0392016-10-24 13:42:14 +01001947 if (list_empty(&obj->userfault_link))
1948 list_add(&obj->userfault_link, &dev_priv->mm.userfault_list);
Chris Wilson275f0392016-10-24 13:42:14 +01001949
Chris Wilsonb90b91d2014-06-10 12:14:40 +01001950 /* Finally, remap it using the new GTT offset */
Chris Wilsonc58305a2016-08-19 16:54:28 +01001951 ret = remap_io_mapping(area,
Chris Wilson8bab11932017-01-14 00:28:25 +00001952 area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT),
Chris Wilsonc58305a2016-08-19 16:54:28 +01001953 (ggtt->mappable_base + vma->node.start) >> PAGE_SHIFT,
1954 min_t(u64, vma->size, area->vm_end - area->vm_start),
1955 &ggtt->mappable);
Chris Wilsona61007a2016-08-18 17:17:02 +01001956
Chris Wilsonb8f90962016-08-05 10:14:07 +01001957err_unpin:
Chris Wilson058d88c2016-08-15 10:49:06 +01001958 __i915_vma_unpin(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001959err_unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001960 mutex_unlock(&dev->struct_mutex);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001961err_rpm:
1962 intel_runtime_pm_put(dev_priv);
Chris Wilson40e62d52016-10-28 13:58:41 +01001963 i915_gem_object_unpin_pages(obj);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001964err:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001965 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001966 case -EIO:
Daniel Vetter2232f032014-09-04 09:36:18 +02001967 /*
1968 * We eat errors when the gpu is terminally wedged to avoid
1969 * userspace unduly crashing (gl has no provisions for mmaps to
1970 * fail). But any other -EIO isn't ours (e.g. swap in failure)
1971 * and so needs to be reported.
1972 */
1973 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
Paulo Zanonif65c9162013-11-27 18:20:34 -02001974 ret = VM_FAULT_SIGBUS;
1975 break;
1976 }
Chris Wilson045e7692010-11-07 09:18:22 +00001977 case -EAGAIN:
Daniel Vetter571c6082013-09-12 17:57:28 +02001978 /*
1979 * EAGAIN means the gpu is hung and we'll wait for the error
1980 * handler to reset everything when re-faulting in
1981 * i915_mutex_lock_interruptible.
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001982 */
Chris Wilsonc7150892009-09-23 00:43:56 +01001983 case 0:
1984 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00001985 case -EINTR:
Dmitry Rogozhkine79e0fe2012-10-03 17:15:26 +03001986 case -EBUSY:
1987 /*
1988 * EBUSY is ok: this just means that another thread
1989 * already did the job.
1990 */
Paulo Zanonif65c9162013-11-27 18:20:34 -02001991 ret = VM_FAULT_NOPAGE;
1992 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001993 case -ENOMEM:
Paulo Zanonif65c9162013-11-27 18:20:34 -02001994 ret = VM_FAULT_OOM;
1995 break;
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02001996 case -ENOSPC:
Chris Wilson45d67812014-01-31 11:34:57 +00001997 case -EFAULT:
Paulo Zanonif65c9162013-11-27 18:20:34 -02001998 ret = VM_FAULT_SIGBUS;
1999 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002000 default:
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02002001 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
Paulo Zanonif65c9162013-11-27 18:20:34 -02002002 ret = VM_FAULT_SIGBUS;
2003 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002004 }
Paulo Zanonif65c9162013-11-27 18:20:34 -02002005 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002006}
2007
2008/**
Chris Wilson901782b2009-07-10 08:18:50 +01002009 * i915_gem_release_mmap - remove physical page mappings
2010 * @obj: obj in question
2011 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002012 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01002013 * relinquish ownership of the pages back to the system.
2014 *
2015 * It is vital that we remove the page mapping if we have mapped a tiled
2016 * object through the GTT and then lose the fence register due to
2017 * resource pressure. Similarly if the object has been moved out of the
2018 * aperture, than pages mapped into userspace must be revoked. Removing the
2019 * mapping will then trigger a page fault on the next user access, allowing
2020 * fixup by i915_gem_fault().
2021 */
Eric Anholtd05ca302009-07-10 13:02:26 -07002022void
Chris Wilson05394f32010-11-08 19:18:58 +00002023i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01002024{
Chris Wilson275f0392016-10-24 13:42:14 +01002025 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson275f0392016-10-24 13:42:14 +01002026
Chris Wilson349f2cc2016-04-13 17:35:12 +01002027 /* Serialisation between user GTT access and our code depends upon
2028 * revoking the CPU's PTE whilst the mutex is held. The next user
2029 * pagefault then has to wait until we release the mutex.
Chris Wilson9c870d02016-10-24 13:42:15 +01002030 *
2031 * Note that RPM complicates somewhat by adding an additional
2032 * requirement that operations to the GGTT be made holding the RPM
2033 * wakeref.
Chris Wilson349f2cc2016-04-13 17:35:12 +01002034 */
Chris Wilson275f0392016-10-24 13:42:14 +01002035 lockdep_assert_held(&i915->drm.struct_mutex);
Chris Wilson9c870d02016-10-24 13:42:15 +01002036 intel_runtime_pm_get(i915);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002037
Chris Wilson3594a3e2016-10-24 13:42:16 +01002038 if (list_empty(&obj->userfault_link))
Chris Wilson9c870d02016-10-24 13:42:15 +01002039 goto out;
Chris Wilson901782b2009-07-10 08:18:50 +01002040
Chris Wilson3594a3e2016-10-24 13:42:16 +01002041 list_del_init(&obj->userfault_link);
David Herrmann6796cb12014-01-03 14:24:19 +01002042 drm_vma_node_unmap(&obj->base.vma_node,
2043 obj->base.dev->anon_inode->i_mapping);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002044
2045 /* Ensure that the CPU's PTE are revoked and there are not outstanding
2046 * memory transactions from userspace before we return. The TLB
2047 * flushing implied above by changing the PTE above *should* be
2048 * sufficient, an extra barrier here just provides us with a bit
2049 * of paranoid documentation about our requirement to serialise
2050 * memory writes before touching registers / GSM.
2051 */
2052 wmb();
Chris Wilson9c870d02016-10-24 13:42:15 +01002053
2054out:
2055 intel_runtime_pm_put(i915);
Chris Wilson901782b2009-07-10 08:18:50 +01002056}
2057
Chris Wilson7c108fd2016-10-24 13:42:18 +01002058void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv)
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002059{
Chris Wilson3594a3e2016-10-24 13:42:16 +01002060 struct drm_i915_gem_object *obj, *on;
Chris Wilson7c108fd2016-10-24 13:42:18 +01002061 int i;
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002062
Chris Wilson3594a3e2016-10-24 13:42:16 +01002063 /*
2064 * Only called during RPM suspend. All users of the userfault_list
2065 * must be holding an RPM wakeref to ensure that this can not
2066 * run concurrently with themselves (and use the struct_mutex for
2067 * protection between themselves).
2068 */
2069
2070 list_for_each_entry_safe(obj, on,
2071 &dev_priv->mm.userfault_list, userfault_link) {
Chris Wilson275f0392016-10-24 13:42:14 +01002072 list_del_init(&obj->userfault_link);
Chris Wilson275f0392016-10-24 13:42:14 +01002073 drm_vma_node_unmap(&obj->base.vma_node,
2074 obj->base.dev->anon_inode->i_mapping);
Chris Wilson275f0392016-10-24 13:42:14 +01002075 }
Chris Wilson7c108fd2016-10-24 13:42:18 +01002076
2077 /* The fence will be lost when the device powers down. If any were
2078 * in use by hardware (i.e. they are pinned), we should not be powering
2079 * down! All other fences will be reacquired by the user upon waking.
2080 */
2081 for (i = 0; i < dev_priv->num_fence_regs; i++) {
2082 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
2083
Chris Wilsone0ec3ec2017-02-03 12:57:17 +00002084 /* Ideally we want to assert that the fence register is not
2085 * live at this point (i.e. that no piece of code will be
2086 * trying to write through fence + GTT, as that both violates
2087 * our tracking of activity and associated locking/barriers,
2088 * but also is illegal given that the hw is powered down).
2089 *
2090 * Previously we used reg->pin_count as a "liveness" indicator.
2091 * That is not sufficient, and we need a more fine-grained
2092 * tool if we want to have a sanity check here.
2093 */
Chris Wilson7c108fd2016-10-24 13:42:18 +01002094
2095 if (!reg->vma)
2096 continue;
2097
2098 GEM_BUG_ON(!list_empty(&reg->vma->obj->userfault_link));
2099 reg->dirty = true;
2100 }
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002101}
2102
Chris Wilsond8cb5082012-08-11 15:41:03 +01002103static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
2104{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002105 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002106 int err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002107
Chris Wilsonf3f61842016-08-05 10:14:14 +01002108 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002109 if (likely(!err))
Chris Wilsonf3f61842016-08-05 10:14:14 +01002110 return 0;
Daniel Vetterda494d72012-12-20 15:11:16 +01002111
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002112 /* Attempt to reap some mmap space from dead objects */
2113 do {
2114 err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
2115 if (err)
2116 break;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002117
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002118 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002119 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002120 if (!err)
2121 break;
2122
2123 } while (flush_delayed_work(&dev_priv->gt.retire_work));
Daniel Vetterda494d72012-12-20 15:11:16 +01002124
Chris Wilsonf3f61842016-08-05 10:14:14 +01002125 return err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002126}
2127
2128static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
2129{
Chris Wilsond8cb5082012-08-11 15:41:03 +01002130 drm_gem_free_mmap_offset(&obj->base);
2131}
2132
Dave Airlieda6b51d2014-12-24 13:11:17 +10002133int
Dave Airlieff72145b2011-02-07 12:16:14 +10002134i915_gem_mmap_gtt(struct drm_file *file,
2135 struct drm_device *dev,
Dave Airlieda6b51d2014-12-24 13:11:17 +10002136 uint32_t handle,
Dave Airlieff72145b2011-02-07 12:16:14 +10002137 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002138{
Chris Wilson05394f32010-11-08 19:18:58 +00002139 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002140 int ret;
2141
Chris Wilson03ac0642016-07-20 13:31:51 +01002142 obj = i915_gem_object_lookup(file, handle);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002143 if (!obj)
2144 return -ENOENT;
Chris Wilsonab182822009-09-22 18:46:17 +01002145
Chris Wilsond8cb5082012-08-11 15:41:03 +01002146 ret = i915_gem_object_create_mmap_offset(obj);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002147 if (ret == 0)
2148 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002149
Chris Wilsonf0cd5182016-10-28 13:58:43 +01002150 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002151 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002152}
2153
Dave Airlieff72145b2011-02-07 12:16:14 +10002154/**
2155 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
2156 * @dev: DRM device
2157 * @data: GTT mapping ioctl data
2158 * @file: GEM object info
2159 *
2160 * Simply returns the fake offset to userspace so it can mmap it.
2161 * The mmap call will end up in drm_gem_mmap(), which will set things
2162 * up so we can get faults in the handler above.
2163 *
2164 * The fault handler will take care of binding the object into the GTT
2165 * (since it may have been evicted to make room for something), allocating
2166 * a fence register, and mapping the appropriate aperture address into
2167 * userspace.
2168 */
2169int
2170i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
2171 struct drm_file *file)
2172{
2173 struct drm_i915_gem_mmap_gtt *args = data;
2174
Dave Airlieda6b51d2014-12-24 13:11:17 +10002175 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
Dave Airlieff72145b2011-02-07 12:16:14 +10002176}
2177
Daniel Vetter225067e2012-08-20 10:23:20 +02002178/* Immediately discard the backing storage */
2179static void
2180i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01002181{
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002182 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02002183
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002184 if (obj->base.filp == NULL)
2185 return;
2186
Daniel Vetter225067e2012-08-20 10:23:20 +02002187 /* Our goal here is to return as much of the memory as
2188 * is possible back to the system as we are called from OOM.
2189 * To do this we must instruct the shmfs to drop all of its
2190 * backing pages, *now*.
Chris Wilsone5281cc2010-10-28 13:45:36 +01002191 */
Chris Wilson55372522014-03-25 13:23:06 +00002192 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002193 obj->mm.madv = __I915_MADV_PURGED;
Chris Wilson4e5462e2017-03-07 13:20:31 +00002194 obj->mm.pages = ERR_PTR(-EFAULT);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002195}
Chris Wilsone5281cc2010-10-28 13:45:36 +01002196
Chris Wilson55372522014-03-25 13:23:06 +00002197/* Try to discard unwanted pages */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002198void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
Daniel Vetter225067e2012-08-20 10:23:20 +02002199{
Chris Wilson55372522014-03-25 13:23:06 +00002200 struct address_space *mapping;
2201
Chris Wilson1233e2d2016-10-28 13:58:37 +01002202 lockdep_assert_held(&obj->mm.lock);
2203 GEM_BUG_ON(obj->mm.pages);
2204
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002205 switch (obj->mm.madv) {
Chris Wilson55372522014-03-25 13:23:06 +00002206 case I915_MADV_DONTNEED:
2207 i915_gem_object_truncate(obj);
2208 case __I915_MADV_PURGED:
2209 return;
2210 }
2211
2212 if (obj->base.filp == NULL)
2213 return;
2214
Al Viro93c76a32015-12-04 23:45:44 -05002215 mapping = obj->base.filp->f_mapping,
Chris Wilson55372522014-03-25 13:23:06 +00002216 invalidate_mapping_pages(mapping, 0, (loff_t)-1);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002217}
2218
Chris Wilson5cdf5882010-09-27 15:51:07 +01002219static void
Chris Wilson03ac84f2016-10-28 13:58:36 +01002220i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj,
2221 struct sg_table *pages)
Eric Anholt673a3942008-07-30 12:06:12 -07002222{
Dave Gordon85d12252016-05-20 11:54:06 +01002223 struct sgt_iter sgt_iter;
2224 struct page *page;
Daniel Vetter1286ff72012-05-10 15:25:09 +02002225
Chris Wilsone5facdf2016-12-23 14:57:57 +00002226 __i915_gem_object_release_shmem(obj, pages, true);
Eric Anholt856fa192009-03-19 14:10:50 -07002227
Chris Wilson03ac84f2016-10-28 13:58:36 +01002228 i915_gem_gtt_finish_pages(obj, pages);
Imre Deake2273302015-07-09 12:59:05 +03002229
Daniel Vetter6dacfd22011-09-12 21:30:02 +02002230 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002231 i915_gem_object_save_bit_17_swizzle(obj, pages);
Eric Anholt280b7132009-03-12 16:56:27 -07002232
Chris Wilson03ac84f2016-10-28 13:58:36 +01002233 for_each_sgt_page(page, sgt_iter, pages) {
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002234 if (obj->mm.dirty)
Chris Wilson9da3da62012-06-01 15:20:22 +01002235 set_page_dirty(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002236
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002237 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson9da3da62012-06-01 15:20:22 +01002238 mark_page_accessed(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002239
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002240 put_page(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002241 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002242 obj->mm.dirty = false;
Eric Anholt673a3942008-07-30 12:06:12 -07002243
Chris Wilson03ac84f2016-10-28 13:58:36 +01002244 sg_free_table(pages);
2245 kfree(pages);
Chris Wilson37e680a2012-06-07 15:38:42 +01002246}
2247
Chris Wilson96d77632016-10-28 13:58:33 +01002248static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
2249{
2250 struct radix_tree_iter iter;
2251 void **slot;
2252
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002253 radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
2254 radix_tree_delete(&obj->mm.get_page.radix, iter.index);
Chris Wilson96d77632016-10-28 13:58:33 +01002255}
2256
Chris Wilson548625e2016-11-01 12:11:34 +00002257void __i915_gem_object_put_pages(struct drm_i915_gem_object *obj,
2258 enum i915_mm_subclass subclass)
Chris Wilson37e680a2012-06-07 15:38:42 +01002259{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002260 struct sg_table *pages;
Chris Wilson37e680a2012-06-07 15:38:42 +01002261
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002262 if (i915_gem_object_has_pinned_pages(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002263 return;
Chris Wilsona5570172012-09-04 21:02:54 +01002264
Chris Wilson15717de2016-08-04 07:52:26 +01002265 GEM_BUG_ON(obj->bind_count);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002266 if (!READ_ONCE(obj->mm.pages))
2267 return;
2268
2269 /* May be called by shrinker from within get_pages() (on another bo) */
Chris Wilson548625e2016-11-01 12:11:34 +00002270 mutex_lock_nested(&obj->mm.lock, subclass);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002271 if (unlikely(atomic_read(&obj->mm.pages_pin_count)))
2272 goto unlock;
Ben Widawsky3e123022013-07-31 17:00:04 -07002273
Chris Wilsona2165e32012-12-03 11:49:00 +00002274 /* ->put_pages might need to allocate memory for the bit17 swizzle
2275 * array, hence protect them from being reaped by removing them from gtt
2276 * lists early. */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002277 pages = fetch_and_zero(&obj->mm.pages);
2278 GEM_BUG_ON(!pages);
Chris Wilsona2165e32012-12-03 11:49:00 +00002279
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002280 if (obj->mm.mapping) {
Chris Wilson4b30cb22016-08-18 17:16:42 +01002281 void *ptr;
2282
Chris Wilson0ce81782017-05-17 13:09:59 +01002283 ptr = page_mask_bits(obj->mm.mapping);
Chris Wilson4b30cb22016-08-18 17:16:42 +01002284 if (is_vmalloc_addr(ptr))
2285 vunmap(ptr);
Chris Wilsonfb8621d2016-04-08 12:11:14 +01002286 else
Chris Wilson4b30cb22016-08-18 17:16:42 +01002287 kunmap(kmap_to_page(ptr));
2288
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002289 obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002290 }
2291
Chris Wilson96d77632016-10-28 13:58:33 +01002292 __i915_gem_object_reset_page_iter(obj);
2293
Chris Wilson4e5462e2017-03-07 13:20:31 +00002294 if (!IS_ERR(pages))
2295 obj->ops->put_pages(obj, pages);
2296
Chris Wilson1233e2d2016-10-28 13:58:37 +01002297unlock:
2298 mutex_unlock(&obj->mm.lock);
Chris Wilson6c085a72012-08-20 11:40:46 +02002299}
2300
Chris Wilson935a2f72017-02-13 17:15:13 +00002301static bool i915_sg_trim(struct sg_table *orig_st)
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002302{
2303 struct sg_table new_st;
2304 struct scatterlist *sg, *new_sg;
2305 unsigned int i;
2306
2307 if (orig_st->nents == orig_st->orig_nents)
Chris Wilson935a2f72017-02-13 17:15:13 +00002308 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002309
Chris Wilson8bfc478f2016-12-23 14:57:58 +00002310 if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN))
Chris Wilson935a2f72017-02-13 17:15:13 +00002311 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002312
2313 new_sg = new_st.sgl;
2314 for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
2315 sg_set_page(new_sg, sg_page(sg), sg->length, 0);
2316 /* called before being DMA mapped, no need to copy sg->dma_* */
2317 new_sg = sg_next(new_sg);
2318 }
Chris Wilsonc2dc6cc2016-12-19 12:43:46 +00002319 GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002320
2321 sg_free_table(orig_st);
2322
2323 *orig_st = new_st;
Chris Wilson935a2f72017-02-13 17:15:13 +00002324 return true;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002325}
2326
Chris Wilson03ac84f2016-10-28 13:58:36 +01002327static struct sg_table *
Chris Wilson6c085a72012-08-20 11:40:46 +02002328i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002329{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002330 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsond766ef52016-12-19 12:43:45 +00002331 const unsigned long page_count = obj->base.size / PAGE_SIZE;
2332 unsigned long i;
Eric Anholt673a3942008-07-30 12:06:12 -07002333 struct address_space *mapping;
Chris Wilson9da3da62012-06-01 15:20:22 +01002334 struct sg_table *st;
2335 struct scatterlist *sg;
Dave Gordon85d12252016-05-20 11:54:06 +01002336 struct sgt_iter sgt_iter;
Eric Anholt673a3942008-07-30 12:06:12 -07002337 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002338 unsigned long last_pfn = 0; /* suppress gcc warning */
Chris Wilson4ff340f02016-10-18 13:02:50 +01002339 unsigned int max_segment;
Imre Deake2273302015-07-09 12:59:05 +03002340 int ret;
Chris Wilson6c085a72012-08-20 11:40:46 +02002341 gfp_t gfp;
Eric Anholt673a3942008-07-30 12:06:12 -07002342
Chris Wilson6c085a72012-08-20 11:40:46 +02002343 /* Assert that the object is not currently in any GPU domain. As it
2344 * wasn't in the GTT, there shouldn't be any way it could have been in
2345 * a GPU cache
2346 */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002347 GEM_BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2348 GEM_BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
Chris Wilson6c085a72012-08-20 11:40:46 +02002349
Konrad Rzeszutek Wilk7453c542016-12-20 10:02:02 -05002350 max_segment = swiotlb_max_segment();
Chris Wilson871dfbd2016-10-11 09:20:21 +01002351 if (!max_segment)
Chris Wilson4ff340f02016-10-18 13:02:50 +01002352 max_segment = rounddown(UINT_MAX, PAGE_SIZE);
Chris Wilson871dfbd2016-10-11 09:20:21 +01002353
Chris Wilson9da3da62012-06-01 15:20:22 +01002354 st = kmalloc(sizeof(*st), GFP_KERNEL);
2355 if (st == NULL)
Chris Wilson03ac84f2016-10-28 13:58:36 +01002356 return ERR_PTR(-ENOMEM);
Eric Anholt673a3942008-07-30 12:06:12 -07002357
Chris Wilsond766ef52016-12-19 12:43:45 +00002358rebuild_st:
Chris Wilson9da3da62012-06-01 15:20:22 +01002359 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
Chris Wilson9da3da62012-06-01 15:20:22 +01002360 kfree(st);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002361 return ERR_PTR(-ENOMEM);
Chris Wilson9da3da62012-06-01 15:20:22 +01002362 }
2363
2364 /* Get the list of pages out of our struct file. They'll be pinned
2365 * at this point until we release them.
2366 *
2367 * Fail silently without starting the shrinker
2368 */
Al Viro93c76a32015-12-04 23:45:44 -05002369 mapping = obj->base.filp->f_mapping;
Michal Hockoc62d2552015-11-06 16:28:49 -08002370 gfp = mapping_gfp_constraint(mapping, ~(__GFP_IO | __GFP_RECLAIM));
Mel Gormand0164ad2015-11-06 16:28:21 -08002371 gfp |= __GFP_NORETRY | __GFP_NOWARN;
Imre Deak90797e62013-02-18 19:28:03 +02002372 sg = st->sgl;
2373 st->nents = 0;
2374 for (i = 0; i < page_count; i++) {
Chris Wilson6c085a72012-08-20 11:40:46 +02002375 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
Chris Wilson24f8e002017-03-22 11:05:21 +00002376 if (unlikely(IS_ERR(page))) {
Chris Wilson21ab4e72014-09-09 11:16:08 +01002377 i915_gem_shrink(dev_priv,
2378 page_count,
2379 I915_SHRINK_BOUND |
2380 I915_SHRINK_UNBOUND |
2381 I915_SHRINK_PURGEABLE);
Chris Wilson6c085a72012-08-20 11:40:46 +02002382 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
2383 }
Chris Wilson24f8e002017-03-22 11:05:21 +00002384 if (unlikely(IS_ERR(page))) {
2385 gfp_t reclaim;
2386
Chris Wilson6c085a72012-08-20 11:40:46 +02002387 /* We've tried hard to allocate the memory by reaping
2388 * our own buffer, now let the real VM do its job and
2389 * go down in flames if truly OOM.
Chris Wilson24f8e002017-03-22 11:05:21 +00002390 *
2391 * However, since graphics tend to be disposable,
2392 * defer the oom here by reporting the ENOMEM back
2393 * to userspace.
Chris Wilson6c085a72012-08-20 11:40:46 +02002394 */
Chris Wilsonb268d9f2017-04-05 23:15:14 +01002395 reclaim = mapping_gfp_mask(mapping);
Chris Wilson24f8e002017-03-22 11:05:21 +00002396 reclaim |= __GFP_NORETRY; /* reclaim, but no oom */
2397
Chris Wilson40149f02017-03-22 22:34:47 +00002398 page = shmem_read_mapping_page_gfp(mapping, i, reclaim);
Imre Deake2273302015-07-09 12:59:05 +03002399 if (IS_ERR(page)) {
2400 ret = PTR_ERR(page);
Chris Wilsonb17993b2016-11-14 11:29:30 +00002401 goto err_sg;
Imre Deake2273302015-07-09 12:59:05 +03002402 }
Chris Wilson6c085a72012-08-20 11:40:46 +02002403 }
Chris Wilson871dfbd2016-10-11 09:20:21 +01002404 if (!i ||
2405 sg->length >= max_segment ||
2406 page_to_pfn(page) != last_pfn + 1) {
Imre Deak90797e62013-02-18 19:28:03 +02002407 if (i)
2408 sg = sg_next(sg);
2409 st->nents++;
2410 sg_set_page(sg, page, PAGE_SIZE, 0);
2411 } else {
2412 sg->length += PAGE_SIZE;
2413 }
2414 last_pfn = page_to_pfn(page);
Daniel Vetter3bbbe702013-10-07 17:15:45 -03002415
2416 /* Check that the i965g/gm workaround works. */
2417 WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
Eric Anholt673a3942008-07-30 12:06:12 -07002418 }
Chris Wilson871dfbd2016-10-11 09:20:21 +01002419 if (sg) /* loop terminated early; short sg table */
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002420 sg_mark_end(sg);
Chris Wilson74ce6b62012-10-19 15:51:06 +01002421
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002422 /* Trim unused sg entries to avoid wasting memory. */
2423 i915_sg_trim(st);
2424
Chris Wilson03ac84f2016-10-28 13:58:36 +01002425 ret = i915_gem_gtt_prepare_pages(obj, st);
Chris Wilsond766ef52016-12-19 12:43:45 +00002426 if (ret) {
2427 /* DMA remapping failed? One possible cause is that
2428 * it could not reserve enough large entries, asking
2429 * for PAGE_SIZE chunks instead may be helpful.
2430 */
2431 if (max_segment > PAGE_SIZE) {
2432 for_each_sgt_page(page, sgt_iter, st)
2433 put_page(page);
2434 sg_free_table(st);
2435
2436 max_segment = PAGE_SIZE;
2437 goto rebuild_st;
2438 } else {
2439 dev_warn(&dev_priv->drm.pdev->dev,
2440 "Failed to DMA remap %lu pages\n",
2441 page_count);
2442 goto err_pages;
2443 }
2444 }
Imre Deake2273302015-07-09 12:59:05 +03002445
Eric Anholt673a3942008-07-30 12:06:12 -07002446 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002447 i915_gem_object_do_bit_17_swizzle(obj, st);
Eric Anholt673a3942008-07-30 12:06:12 -07002448
Chris Wilson03ac84f2016-10-28 13:58:36 +01002449 return st;
Eric Anholt673a3942008-07-30 12:06:12 -07002450
Chris Wilsonb17993b2016-11-14 11:29:30 +00002451err_sg:
Imre Deak90797e62013-02-18 19:28:03 +02002452 sg_mark_end(sg);
Chris Wilsonb17993b2016-11-14 11:29:30 +00002453err_pages:
Dave Gordon85d12252016-05-20 11:54:06 +01002454 for_each_sgt_page(page, sgt_iter, st)
2455 put_page(page);
Chris Wilson9da3da62012-06-01 15:20:22 +01002456 sg_free_table(st);
2457 kfree(st);
Chris Wilson0820baf2014-03-25 13:23:03 +00002458
2459 /* shmemfs first checks if there is enough memory to allocate the page
2460 * and reports ENOSPC should there be insufficient, along with the usual
2461 * ENOMEM for a genuine allocation failure.
2462 *
2463 * We use ENOSPC in our driver to mean that we have run out of aperture
2464 * space and so want to translate the error from shmemfs back to our
2465 * usual understanding of ENOMEM.
2466 */
Imre Deake2273302015-07-09 12:59:05 +03002467 if (ret == -ENOSPC)
2468 ret = -ENOMEM;
2469
Chris Wilson03ac84f2016-10-28 13:58:36 +01002470 return ERR_PTR(ret);
2471}
2472
2473void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
2474 struct sg_table *pages)
2475{
Chris Wilson1233e2d2016-10-28 13:58:37 +01002476 lockdep_assert_held(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002477
2478 obj->mm.get_page.sg_pos = pages->sgl;
2479 obj->mm.get_page.sg_idx = 0;
2480
2481 obj->mm.pages = pages;
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002482
2483 if (i915_gem_object_is_tiled(obj) &&
2484 to_i915(obj->base.dev)->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
2485 GEM_BUG_ON(obj->mm.quirked);
2486 __i915_gem_object_pin_pages(obj);
2487 obj->mm.quirked = true;
2488 }
Chris Wilson03ac84f2016-10-28 13:58:36 +01002489}
2490
2491static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
2492{
2493 struct sg_table *pages;
2494
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002495 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2496
Chris Wilson03ac84f2016-10-28 13:58:36 +01002497 if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
2498 DRM_DEBUG("Attempting to obtain a purgeable object\n");
2499 return -EFAULT;
2500 }
2501
2502 pages = obj->ops->get_pages(obj);
2503 if (unlikely(IS_ERR(pages)))
2504 return PTR_ERR(pages);
2505
2506 __i915_gem_object_set_pages(obj, pages);
2507 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002508}
2509
Chris Wilson37e680a2012-06-07 15:38:42 +01002510/* Ensure that the associated pages are gathered from the backing storage
Chris Wilson1233e2d2016-10-28 13:58:37 +01002511 * and pinned into our object. i915_gem_object_pin_pages() may be called
Chris Wilson37e680a2012-06-07 15:38:42 +01002512 * multiple times before they are released by a single call to
Chris Wilson1233e2d2016-10-28 13:58:37 +01002513 * i915_gem_object_unpin_pages() - once the pages are no longer referenced
Chris Wilson37e680a2012-06-07 15:38:42 +01002514 * either as a result of memory pressure (reaping pages under the shrinker)
2515 * or as the object is itself released.
2516 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002517int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
Chris Wilson37e680a2012-06-07 15:38:42 +01002518{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002519 int err;
Chris Wilson37e680a2012-06-07 15:38:42 +01002520
Chris Wilson1233e2d2016-10-28 13:58:37 +01002521 err = mutex_lock_interruptible(&obj->mm.lock);
2522 if (err)
2523 return err;
Chris Wilson4c7d62c2016-10-28 13:58:32 +01002524
Chris Wilson4e5462e2017-03-07 13:20:31 +00002525 if (unlikely(IS_ERR_OR_NULL(obj->mm.pages))) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002526 err = ____i915_gem_object_get_pages(obj);
2527 if (err)
2528 goto unlock;
2529
2530 smp_mb__before_atomic();
Chris Wilson1233e2d2016-10-28 13:58:37 +01002531 }
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002532 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson43e28f02013-01-08 10:53:09 +00002533
Chris Wilson1233e2d2016-10-28 13:58:37 +01002534unlock:
2535 mutex_unlock(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002536 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002537}
2538
Dave Gordondd6034c2016-05-20 11:54:04 +01002539/* The 'mapping' part of i915_gem_object_pin_map() below */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002540static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
2541 enum i915_map_type type)
Dave Gordondd6034c2016-05-20 11:54:04 +01002542{
2543 unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002544 struct sg_table *sgt = obj->mm.pages;
Dave Gordon85d12252016-05-20 11:54:06 +01002545 struct sgt_iter sgt_iter;
2546 struct page *page;
Dave Gordonb338fa42016-05-20 11:54:05 +01002547 struct page *stack_pages[32];
2548 struct page **pages = stack_pages;
Dave Gordondd6034c2016-05-20 11:54:04 +01002549 unsigned long i = 0;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002550 pgprot_t pgprot;
Dave Gordondd6034c2016-05-20 11:54:04 +01002551 void *addr;
2552
2553 /* A single page can always be kmapped */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002554 if (n_pages == 1 && type == I915_MAP_WB)
Dave Gordondd6034c2016-05-20 11:54:04 +01002555 return kmap(sg_page(sgt->sgl));
2556
Dave Gordonb338fa42016-05-20 11:54:05 +01002557 if (n_pages > ARRAY_SIZE(stack_pages)) {
2558 /* Too big for stack -- allocate temporary array instead */
2559 pages = drm_malloc_gfp(n_pages, sizeof(*pages), GFP_TEMPORARY);
2560 if (!pages)
2561 return NULL;
2562 }
Dave Gordondd6034c2016-05-20 11:54:04 +01002563
Dave Gordon85d12252016-05-20 11:54:06 +01002564 for_each_sgt_page(page, sgt_iter, sgt)
2565 pages[i++] = page;
Dave Gordondd6034c2016-05-20 11:54:04 +01002566
2567 /* Check that we have the expected number of pages */
2568 GEM_BUG_ON(i != n_pages);
2569
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002570 switch (type) {
2571 case I915_MAP_WB:
2572 pgprot = PAGE_KERNEL;
2573 break;
2574 case I915_MAP_WC:
2575 pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
2576 break;
2577 }
2578 addr = vmap(pages, n_pages, 0, pgprot);
Dave Gordondd6034c2016-05-20 11:54:04 +01002579
Dave Gordonb338fa42016-05-20 11:54:05 +01002580 if (pages != stack_pages)
2581 drm_free_large(pages);
Dave Gordondd6034c2016-05-20 11:54:04 +01002582
2583 return addr;
2584}
2585
2586/* get, pin, and map the pages of the object into kernel space */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002587void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
2588 enum i915_map_type type)
Chris Wilson0a798eb2016-04-08 12:11:11 +01002589{
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002590 enum i915_map_type has_type;
2591 bool pinned;
2592 void *ptr;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002593 int ret;
2594
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002595 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
Chris Wilson0a798eb2016-04-08 12:11:11 +01002596
Chris Wilson1233e2d2016-10-28 13:58:37 +01002597 ret = mutex_lock_interruptible(&obj->mm.lock);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002598 if (ret)
2599 return ERR_PTR(ret);
2600
Chris Wilson1233e2d2016-10-28 13:58:37 +01002601 pinned = true;
2602 if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
Chris Wilson4e5462e2017-03-07 13:20:31 +00002603 if (unlikely(IS_ERR_OR_NULL(obj->mm.pages))) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002604 ret = ____i915_gem_object_get_pages(obj);
2605 if (ret)
2606 goto err_unlock;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002607
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002608 smp_mb__before_atomic();
2609 }
2610 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002611 pinned = false;
2612 }
2613 GEM_BUG_ON(!obj->mm.pages);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002614
Chris Wilson0ce81782017-05-17 13:09:59 +01002615 ptr = page_unpack_bits(obj->mm.mapping, &has_type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002616 if (ptr && has_type != type) {
2617 if (pinned) {
2618 ret = -EBUSY;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002619 goto err_unpin;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002620 }
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002621
2622 if (is_vmalloc_addr(ptr))
2623 vunmap(ptr);
2624 else
2625 kunmap(kmap_to_page(ptr));
2626
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002627 ptr = obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002628 }
2629
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002630 if (!ptr) {
2631 ptr = i915_gem_object_map(obj, type);
2632 if (!ptr) {
2633 ret = -ENOMEM;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002634 goto err_unpin;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002635 }
2636
Chris Wilson0ce81782017-05-17 13:09:59 +01002637 obj->mm.mapping = page_pack_bits(ptr, type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002638 }
2639
Chris Wilson1233e2d2016-10-28 13:58:37 +01002640out_unlock:
2641 mutex_unlock(&obj->mm.lock);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002642 return ptr;
2643
Chris Wilson1233e2d2016-10-28 13:58:37 +01002644err_unpin:
2645 atomic_dec(&obj->mm.pages_pin_count);
2646err_unlock:
2647 ptr = ERR_PTR(ret);
2648 goto out_unlock;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002649}
2650
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002651static int
2652i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj,
2653 const struct drm_i915_gem_pwrite *arg)
2654{
2655 struct address_space *mapping = obj->base.filp->f_mapping;
2656 char __user *user_data = u64_to_user_ptr(arg->data_ptr);
2657 u64 remain, offset;
2658 unsigned int pg;
2659
2660 /* Before we instantiate/pin the backing store for our use, we
2661 * can prepopulate the shmemfs filp efficiently using a write into
2662 * the pagecache. We avoid the penalty of instantiating all the
2663 * pages, important if the user is just writing to a few and never
2664 * uses the object on the GPU, and using a direct write into shmemfs
2665 * allows it to avoid the cost of retrieving a page (either swapin
2666 * or clearing-before-use) before it is overwritten.
2667 */
2668 if (READ_ONCE(obj->mm.pages))
2669 return -ENODEV;
2670
2671 /* Before the pages are instantiated the object is treated as being
2672 * in the CPU domain. The pages will be clflushed as required before
2673 * use, and we can freely write into the pages directly. If userspace
2674 * races pwrite with any other operation; corruption will ensue -
2675 * that is userspace's prerogative!
2676 */
2677
2678 remain = arg->size;
2679 offset = arg->offset;
2680 pg = offset_in_page(offset);
2681
2682 do {
2683 unsigned int len, unwritten;
2684 struct page *page;
2685 void *data, *vaddr;
2686 int err;
2687
2688 len = PAGE_SIZE - pg;
2689 if (len > remain)
2690 len = remain;
2691
2692 err = pagecache_write_begin(obj->base.filp, mapping,
2693 offset, len, 0,
2694 &page, &data);
2695 if (err < 0)
2696 return err;
2697
2698 vaddr = kmap(page);
2699 unwritten = copy_from_user(vaddr + pg, user_data, len);
2700 kunmap(page);
2701
2702 err = pagecache_write_end(obj->base.filp, mapping,
2703 offset, len, len - unwritten,
2704 page, data);
2705 if (err < 0)
2706 return err;
2707
2708 if (unwritten)
2709 return -EFAULT;
2710
2711 remain -= len;
2712 user_data += len;
2713 offset += len;
2714 pg = 0;
2715 } while (remain);
2716
2717 return 0;
2718}
2719
Chris Wilson60958682016-12-31 11:20:11 +00002720static bool ban_context(const struct i915_gem_context *ctx)
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002721{
Chris Wilson60958682016-12-31 11:20:11 +00002722 return (i915_gem_context_is_bannable(ctx) &&
2723 ctx->ban_score >= CONTEXT_SCORE_BAN_THRESHOLD);
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002724}
2725
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002726static void i915_gem_context_mark_guilty(struct i915_gem_context *ctx)
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002727{
Mika Kuoppalabc1d53c2016-11-16 17:20:34 +02002728 ctx->guilty_count++;
Chris Wilson60958682016-12-31 11:20:11 +00002729 ctx->ban_score += CONTEXT_SCORE_GUILTY;
2730 if (ban_context(ctx))
2731 i915_gem_context_set_banned(ctx);
Mika Kuoppalab083a082016-11-18 15:10:47 +02002732
2733 DRM_DEBUG_DRIVER("context %s marked guilty (score %d) banned? %s\n",
Mika Kuoppalabc1d53c2016-11-16 17:20:34 +02002734 ctx->name, ctx->ban_score,
Chris Wilson60958682016-12-31 11:20:11 +00002735 yesno(i915_gem_context_is_banned(ctx)));
Mika Kuoppalab083a082016-11-18 15:10:47 +02002736
Chris Wilson60958682016-12-31 11:20:11 +00002737 if (!i915_gem_context_is_banned(ctx) || IS_ERR_OR_NULL(ctx->file_priv))
Mika Kuoppalab083a082016-11-18 15:10:47 +02002738 return;
2739
Chris Wilsond9e9da62016-11-22 14:41:18 +00002740 ctx->file_priv->context_bans++;
2741 DRM_DEBUG_DRIVER("client %s has had %d context banned\n",
2742 ctx->name, ctx->file_priv->context_bans);
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002743}
2744
2745static void i915_gem_context_mark_innocent(struct i915_gem_context *ctx)
2746{
Mika Kuoppalabc1d53c2016-11-16 17:20:34 +02002747 ctx->active_count++;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002748}
2749
Chris Wilson8d9fc7f2014-02-25 17:11:23 +02002750struct drm_i915_gem_request *
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002751i915_gem_find_active_request(struct intel_engine_cs *engine)
Chris Wilson9375e442010-09-19 12:21:28 +01002752{
Chris Wilson754c9fd2017-02-23 07:44:14 +00002753 struct drm_i915_gem_request *request, *active = NULL;
2754 unsigned long flags;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002755
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002756 /* We are called by the error capture and reset at a random
2757 * point in time. In particular, note that neither is crucially
2758 * ordered with an interrupt. After a hang, the GPU is dead and we
2759 * assume that no more writes can happen (we waited long enough for
2760 * all writes that were in transaction to be flushed) - adding an
2761 * extra delay for a recent interrupt is pointless. Hence, we do
2762 * not need an engine->irq_seqno_barrier() before the seqno reads.
2763 */
Chris Wilson754c9fd2017-02-23 07:44:14 +00002764 spin_lock_irqsave(&engine->timeline->lock, flags);
Chris Wilson73cb9702016-10-28 13:58:46 +01002765 list_for_each_entry(request, &engine->timeline->requests, link) {
Chris Wilson754c9fd2017-02-23 07:44:14 +00002766 if (__i915_gem_request_completed(request,
2767 request->global_seqno))
Chris Wilson4db080f2013-12-04 11:37:09 +00002768 continue;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002769
Mika Kuoppala36193ac2017-01-17 17:59:02 +02002770 GEM_BUG_ON(request->engine != engine);
Chris Wilsonc00122f32017-02-12 17:19:58 +00002771 GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
2772 &request->fence.flags));
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002773
Chris Wilson754c9fd2017-02-23 07:44:14 +00002774 active = request;
2775 break;
2776 }
2777 spin_unlock_irqrestore(&engine->timeline->lock, flags);
2778
2779 return active;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002780}
2781
Mika Kuoppalabf2f0432017-01-17 17:59:04 +02002782static bool engine_stalled(struct intel_engine_cs *engine)
2783{
2784 if (!engine->hangcheck.stalled)
2785 return false;
2786
2787 /* Check for possible seqno movement after hang declaration */
2788 if (engine->hangcheck.seqno != intel_engine_get_seqno(engine)) {
2789 DRM_DEBUG_DRIVER("%s pardoned\n", engine->name);
2790 return false;
2791 }
2792
2793 return true;
2794}
2795
Chris Wilson0e178ae2017-01-17 17:59:06 +02002796int i915_gem_reset_prepare(struct drm_i915_private *dev_priv)
Chris Wilson4c965542017-01-17 17:59:01 +02002797{
2798 struct intel_engine_cs *engine;
2799 enum intel_engine_id id;
Chris Wilson0e178ae2017-01-17 17:59:06 +02002800 int err = 0;
Chris Wilson4c965542017-01-17 17:59:01 +02002801
2802 /* Ensure irq handler finishes, and not run again. */
Chris Wilson0e178ae2017-01-17 17:59:06 +02002803 for_each_engine(engine, dev_priv, id) {
2804 struct drm_i915_gem_request *request;
2805
Chris Wilsonfe3288b2017-02-12 17:20:01 +00002806 /* Prevent the signaler thread from updating the request
2807 * state (by calling dma_fence_signal) as we are processing
2808 * the reset. The write from the GPU of the seqno is
2809 * asynchronous and the signaler thread may see a different
2810 * value to us and declare the request complete, even though
2811 * the reset routine have picked that request as the active
2812 * (incomplete) request. This conflict is not handled
2813 * gracefully!
2814 */
2815 kthread_park(engine->breadcrumbs.signaler);
2816
Chris Wilson1f7b8472017-02-08 14:30:33 +00002817 /* Prevent request submission to the hardware until we have
2818 * completed the reset in i915_gem_reset_finish(). If a request
2819 * is completed by one engine, it may then queue a request
2820 * to a second via its engine->irq_tasklet *just* as we are
2821 * calling engine->init_hw() and also writing the ELSP.
2822 * Turning off the engine->irq_tasklet until the reset is over
2823 * prevents the race.
2824 */
Chris Wilson4c965542017-01-17 17:59:01 +02002825 tasklet_kill(&engine->irq_tasklet);
Chris Wilson1d309632017-02-12 17:20:00 +00002826 tasklet_disable(&engine->irq_tasklet);
Chris Wilson4c965542017-01-17 17:59:01 +02002827
Chris Wilson8c12d122017-02-10 18:52:14 +00002828 if (engine->irq_seqno_barrier)
2829 engine->irq_seqno_barrier(engine);
2830
Chris Wilson0e178ae2017-01-17 17:59:06 +02002831 if (engine_stalled(engine)) {
2832 request = i915_gem_find_active_request(engine);
2833 if (request && request->fence.error == -EIO)
2834 err = -EIO; /* Previous reset failed! */
2835 }
2836 }
2837
Chris Wilson4c965542017-01-17 17:59:01 +02002838 i915_gem_revoke_fences(dev_priv);
Chris Wilson0e178ae2017-01-17 17:59:06 +02002839
2840 return err;
Chris Wilson4c965542017-01-17 17:59:01 +02002841}
2842
Mika Kuoppala36193ac2017-01-17 17:59:02 +02002843static void skip_request(struct drm_i915_gem_request *request)
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002844{
Chris Wilson821ed7d2016-09-09 14:11:53 +01002845 void *vaddr = request->ring->vaddr;
2846 u32 head;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002847
Chris Wilson821ed7d2016-09-09 14:11:53 +01002848 /* As this request likely depends on state from the lost
2849 * context, clear out all the user operations leaving the
2850 * breadcrumb at the end (so we get the fence notifications).
2851 */
2852 head = request->head;
2853 if (request->postfix < head) {
2854 memset(vaddr + head, 0, request->ring->size - head);
2855 head = 0;
2856 }
2857 memset(vaddr + head, 0, request->postfix - head);
Chris Wilsonc0d5f322017-01-10 17:22:43 +00002858
2859 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson4db080f2013-12-04 11:37:09 +00002860}
2861
Mika Kuoppala36193ac2017-01-17 17:59:02 +02002862static void engine_skip_context(struct drm_i915_gem_request *request)
2863{
2864 struct intel_engine_cs *engine = request->engine;
2865 struct i915_gem_context *hung_ctx = request->ctx;
2866 struct intel_timeline *timeline;
2867 unsigned long flags;
2868
2869 timeline = i915_gem_context_lookup_timeline(hung_ctx, engine);
2870
2871 spin_lock_irqsave(&engine->timeline->lock, flags);
2872 spin_lock(&timeline->lock);
2873
2874 list_for_each_entry_continue(request, &engine->timeline->requests, link)
2875 if (request->ctx == hung_ctx)
2876 skip_request(request);
2877
2878 list_for_each_entry(request, &timeline->requests, link)
2879 skip_request(request);
2880
2881 spin_unlock(&timeline->lock);
2882 spin_unlock_irqrestore(&engine->timeline->lock, flags);
2883}
2884
Mika Kuoppala61da5362017-01-17 17:59:05 +02002885/* Returns true if the request was guilty of hang */
2886static bool i915_gem_reset_request(struct drm_i915_gem_request *request)
2887{
2888 /* Read once and return the resolution */
2889 const bool guilty = engine_stalled(request->engine);
2890
Mika Kuoppala71895a02017-01-17 17:59:07 +02002891 /* The guilty request will get skipped on a hung engine.
2892 *
2893 * Users of client default contexts do not rely on logical
2894 * state preserved between batches so it is safe to execute
2895 * queued requests following the hang. Non default contexts
2896 * rely on preserved state, so skipping a batch loses the
2897 * evolution of the state and it needs to be considered corrupted.
2898 * Executing more queued batches on top of corrupted state is
2899 * risky. But we take the risk by trying to advance through
2900 * the queued requests in order to make the client behaviour
2901 * more predictable around resets, by not throwing away random
2902 * amount of batches it has prepared for execution. Sophisticated
2903 * clients can use gem_reset_stats_ioctl and dma fence status
2904 * (exported via sync_file info ioctl on explicit fences) to observe
2905 * when it loses the context state and should rebuild accordingly.
2906 *
2907 * The context ban, and ultimately the client ban, mechanism are safety
2908 * valves if client submission ends up resulting in nothing more than
2909 * subsequent hangs.
2910 */
2911
Mika Kuoppala61da5362017-01-17 17:59:05 +02002912 if (guilty) {
2913 i915_gem_context_mark_guilty(request->ctx);
2914 skip_request(request);
2915 } else {
2916 i915_gem_context_mark_innocent(request->ctx);
2917 dma_fence_set_error(&request->fence, -EAGAIN);
2918 }
2919
2920 return guilty;
2921}
2922
Chris Wilson821ed7d2016-09-09 14:11:53 +01002923static void i915_gem_reset_engine(struct intel_engine_cs *engine)
Chris Wilson4db080f2013-12-04 11:37:09 +00002924{
Chris Wilsondcff85c2016-08-05 10:14:11 +01002925 struct drm_i915_gem_request *request;
Chris Wilson608c1a52015-09-03 13:01:40 +01002926
Chris Wilson821ed7d2016-09-09 14:11:53 +01002927 request = i915_gem_find_active_request(engine);
Chris Wilsonc0dcb202017-02-07 15:24:37 +00002928 if (request && i915_gem_reset_request(request)) {
2929 DRM_DEBUG_DRIVER("resetting %s to restart from tail of request 0x%x\n",
2930 engine->name, request->global_seqno);
Chris Wilson821ed7d2016-09-09 14:11:53 +01002931
Chris Wilsonc0dcb202017-02-07 15:24:37 +00002932 /* If this context is now banned, skip all pending requests. */
2933 if (i915_gem_context_is_banned(request->ctx))
2934 engine_skip_context(request);
2935 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01002936
2937 /* Setup the CS to resume from the breadcrumb of the hung request */
2938 engine->reset_hw(engine, request);
Chris Wilson821ed7d2016-09-09 14:11:53 +01002939}
2940
Chris Wilsond8027092017-02-08 14:30:32 +00002941void i915_gem_reset(struct drm_i915_private *dev_priv)
Chris Wilson821ed7d2016-09-09 14:11:53 +01002942{
2943 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302944 enum intel_engine_id id;
Chris Wilson821ed7d2016-09-09 14:11:53 +01002945
Chris Wilson4c7d62c2016-10-28 13:58:32 +01002946 lockdep_assert_held(&dev_priv->drm.struct_mutex);
2947
Chris Wilson821ed7d2016-09-09 14:11:53 +01002948 i915_gem_retire_requests(dev_priv);
2949
Chris Wilson2ae55732017-02-12 17:20:02 +00002950 for_each_engine(engine, dev_priv, id) {
2951 struct i915_gem_context *ctx;
2952
Chris Wilson821ed7d2016-09-09 14:11:53 +01002953 i915_gem_reset_engine(engine);
Chris Wilson2ae55732017-02-12 17:20:02 +00002954 ctx = fetch_and_zero(&engine->last_retired_context);
2955 if (ctx)
2956 engine->context_unpin(engine, ctx);
2957 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01002958
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00002959 i915_gem_restore_fences(dev_priv);
Chris Wilsonf2a91d12016-09-21 14:51:06 +01002960
2961 if (dev_priv->gt.awake) {
2962 intel_sanitize_gt_powersave(dev_priv);
2963 intel_enable_gt_powersave(dev_priv);
2964 if (INTEL_GEN(dev_priv) >= 6)
2965 gen6_rps_busy(dev_priv);
2966 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01002967}
2968
Chris Wilsond8027092017-02-08 14:30:32 +00002969void i915_gem_reset_finish(struct drm_i915_private *dev_priv)
2970{
Chris Wilson1f7b8472017-02-08 14:30:33 +00002971 struct intel_engine_cs *engine;
2972 enum intel_engine_id id;
2973
Chris Wilsond8027092017-02-08 14:30:32 +00002974 lockdep_assert_held(&dev_priv->drm.struct_mutex);
Chris Wilson1f7b8472017-02-08 14:30:33 +00002975
Chris Wilsonfe3288b2017-02-12 17:20:01 +00002976 for_each_engine(engine, dev_priv, id) {
Chris Wilson1f7b8472017-02-08 14:30:33 +00002977 tasklet_enable(&engine->irq_tasklet);
Chris Wilsonfe3288b2017-02-12 17:20:01 +00002978 kthread_unpark(engine->breadcrumbs.signaler);
2979 }
Chris Wilsond8027092017-02-08 14:30:32 +00002980}
2981
Chris Wilson821ed7d2016-09-09 14:11:53 +01002982static void nop_submit_request(struct drm_i915_gem_request *request)
2983{
Chris Wilson3cd94422017-01-10 17:22:45 +00002984 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson3dcf93f2016-11-22 14:41:20 +00002985 i915_gem_request_submit(request);
2986 intel_engine_init_global_seqno(request->engine, request->global_seqno);
Chris Wilson821ed7d2016-09-09 14:11:53 +01002987}
2988
Chris Wilson2a20d6f2017-01-10 17:22:46 +00002989static void engine_set_wedged(struct intel_engine_cs *engine)
Chris Wilson821ed7d2016-09-09 14:11:53 +01002990{
Chris Wilson3cd94422017-01-10 17:22:45 +00002991 struct drm_i915_gem_request *request;
2992 unsigned long flags;
2993
Chris Wilson20e49332016-11-22 14:41:21 +00002994 /* We need to be sure that no thread is running the old callback as
2995 * we install the nop handler (otherwise we would submit a request
2996 * to hardware that will never complete). In order to prevent this
2997 * race, we wait until the machine is idle before making the swap
2998 * (using stop_machine()).
2999 */
Chris Wilson821ed7d2016-09-09 14:11:53 +01003000 engine->submit_request = nop_submit_request;
Chris Wilson70c2a242016-09-09 14:11:46 +01003001
Chris Wilson3cd94422017-01-10 17:22:45 +00003002 /* Mark all executing requests as skipped */
3003 spin_lock_irqsave(&engine->timeline->lock, flags);
3004 list_for_each_entry(request, &engine->timeline->requests, link)
3005 dma_fence_set_error(&request->fence, -EIO);
3006 spin_unlock_irqrestore(&engine->timeline->lock, flags);
3007
Chris Wilsonc4b09302016-07-20 09:21:10 +01003008 /* Mark all pending requests as complete so that any concurrent
3009 * (lockless) lookup doesn't try and wait upon the request as we
3010 * reset it.
3011 */
Chris Wilson73cb9702016-10-28 13:58:46 +01003012 intel_engine_init_global_seqno(engine,
Chris Wilsoncb399ea2016-11-01 10:03:16 +00003013 intel_engine_last_submit(engine));
Chris Wilsonc4b09302016-07-20 09:21:10 +01003014
Ben Widawsky1d62bee2014-01-01 10:15:13 -08003015 /*
Oscar Mateodcb4c122014-11-13 10:28:10 +00003016 * Clear the execlists queue up before freeing the requests, as those
3017 * are the ones that keep the context and ringbuffer backing objects
3018 * pinned in place.
3019 */
Oscar Mateodcb4c122014-11-13 10:28:10 +00003020
Tomas Elf7de1691a2015-10-19 16:32:32 +01003021 if (i915.enable_execlists) {
Chris Wilson77f0d0e2017-05-17 13:10:00 +01003022 struct execlist_port *port = engine->execlist_port;
Chris Wilson663f71e2016-11-14 20:41:00 +00003023 unsigned long flags;
Chris Wilson77f0d0e2017-05-17 13:10:00 +01003024 unsigned int n;
Chris Wilson663f71e2016-11-14 20:41:00 +00003025
3026 spin_lock_irqsave(&engine->timeline->lock, flags);
3027
Chris Wilson77f0d0e2017-05-17 13:10:00 +01003028 for (n = 0; n < ARRAY_SIZE(engine->execlist_port); n++)
3029 i915_gem_request_put(port_request(&port[n]));
Chris Wilson70c2a242016-09-09 14:11:46 +01003030 memset(engine->execlist_port, 0, sizeof(engine->execlist_port));
Chris Wilson20311bd2016-11-14 20:41:03 +00003031 engine->execlist_queue = RB_ROOT;
3032 engine->execlist_first = NULL;
Chris Wilson663f71e2016-11-14 20:41:00 +00003033
3034 spin_unlock_irqrestore(&engine->timeline->lock, flags);
Oscar Mateodcb4c122014-11-13 10:28:10 +00003035 }
Eric Anholt673a3942008-07-30 12:06:12 -07003036}
3037
Chris Wilson20e49332016-11-22 14:41:21 +00003038static int __i915_gem_set_wedged_BKL(void *data)
Eric Anholt673a3942008-07-30 12:06:12 -07003039{
Chris Wilson20e49332016-11-22 14:41:21 +00003040 struct drm_i915_private *i915 = data;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003041 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303042 enum intel_engine_id id;
Eric Anholt673a3942008-07-30 12:06:12 -07003043
Chris Wilson20e49332016-11-22 14:41:21 +00003044 for_each_engine(engine, i915, id)
Chris Wilson2a20d6f2017-01-10 17:22:46 +00003045 engine_set_wedged(engine);
Chris Wilson20e49332016-11-22 14:41:21 +00003046
3047 return 0;
3048}
3049
3050void i915_gem_set_wedged(struct drm_i915_private *dev_priv)
3051{
Chris Wilson821ed7d2016-09-09 14:11:53 +01003052 lockdep_assert_held(&dev_priv->drm.struct_mutex);
3053 set_bit(I915_WEDGED, &dev_priv->gpu_error.flags);
Chris Wilson4db080f2013-12-04 11:37:09 +00003054
Chris Wilson2c170af2017-03-30 15:50:36 +01003055 /* Retire completed requests first so the list of inflight/incomplete
3056 * requests is accurate and we don't try and mark successful requests
3057 * as in error during __i915_gem_set_wedged_BKL().
3058 */
3059 i915_gem_retire_requests(dev_priv);
3060
Chris Wilson20e49332016-11-22 14:41:21 +00003061 stop_machine(__i915_gem_set_wedged_BKL, dev_priv, NULL);
Chris Wilsondfaae392010-09-22 10:31:52 +01003062
Chris Wilson20e49332016-11-22 14:41:21 +00003063 i915_gem_context_lost(dev_priv);
Chris Wilson20e49332016-11-22 14:41:21 +00003064
3065 mod_delayed_work(dev_priv->wq, &dev_priv->gt.idle_work, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003066}
3067
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003068bool i915_gem_unset_wedged(struct drm_i915_private *i915)
3069{
3070 struct i915_gem_timeline *tl;
3071 int i;
3072
3073 lockdep_assert_held(&i915->drm.struct_mutex);
3074 if (!test_bit(I915_WEDGED, &i915->gpu_error.flags))
3075 return true;
3076
3077 /* Before unwedging, make sure that all pending operations
3078 * are flushed and errored out - we may have requests waiting upon
3079 * third party fences. We marked all inflight requests as EIO, and
3080 * every execbuf since returned EIO, for consistency we want all
3081 * the currently pending requests to also be marked as EIO, which
3082 * is done inside our nop_submit_request - and so we must wait.
3083 *
3084 * No more can be submitted until we reset the wedged bit.
3085 */
3086 list_for_each_entry(tl, &i915->gt.timelines, link) {
3087 for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
3088 struct drm_i915_gem_request *rq;
3089
3090 rq = i915_gem_active_peek(&tl->engine[i].last_request,
3091 &i915->drm.struct_mutex);
3092 if (!rq)
3093 continue;
3094
3095 /* We can't use our normal waiter as we want to
3096 * avoid recursively trying to handle the current
3097 * reset. The basic dma_fence_default_wait() installs
3098 * a callback for dma_fence_signal(), which is
3099 * triggered by our nop handler (indirectly, the
3100 * callback enables the signaler thread which is
3101 * woken by the nop_submit_request() advancing the seqno
3102 * and when the seqno passes the fence, the signaler
3103 * then signals the fence waking us up).
3104 */
3105 if (dma_fence_default_wait(&rq->fence, true,
3106 MAX_SCHEDULE_TIMEOUT) < 0)
3107 return false;
3108 }
3109 }
3110
3111 /* Undo nop_submit_request. We prevent all new i915 requests from
3112 * being queued (by disallowing execbuf whilst wedged) so having
3113 * waited for all active requests above, we know the system is idle
3114 * and do not have to worry about a thread being inside
3115 * engine->submit_request() as we swap over. So unlike installing
3116 * the nop_submit_request on reset, we can do this from normal
3117 * context and do not require stop_machine().
3118 */
3119 intel_engines_reset_default_submission(i915);
3120
3121 smp_mb__before_atomic(); /* complete takeover before enabling execbuf */
3122 clear_bit(I915_WEDGED, &i915->gpu_error.flags);
3123
3124 return true;
3125}
3126
Daniel Vetter75ef9da2010-08-21 00:25:16 +02003127static void
Eric Anholt673a3942008-07-30 12:06:12 -07003128i915_gem_retire_work_handler(struct work_struct *work)
3129{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003130 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003131 container_of(work, typeof(*dev_priv), gt.retire_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01003132 struct drm_device *dev = &dev_priv->drm;
Eric Anholt673a3942008-07-30 12:06:12 -07003133
Chris Wilson891b48c2010-09-29 12:26:37 +01003134 /* Come back later if the device is busy... */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003135 if (mutex_trylock(&dev->struct_mutex)) {
Chris Wilson67d97da2016-07-04 08:08:31 +01003136 i915_gem_retire_requests(dev_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003137 mutex_unlock(&dev->struct_mutex);
3138 }
Chris Wilson67d97da2016-07-04 08:08:31 +01003139
3140 /* Keep the retire handler running until we are finally idle.
3141 * We do not need to do this test under locking as in the worst-case
3142 * we queue the retire worker once too often.
3143 */
Chris Wilsonc9615612016-07-09 10:12:06 +01003144 if (READ_ONCE(dev_priv->gt.awake)) {
3145 i915_queue_hangcheck(dev_priv);
Chris Wilson67d97da2016-07-04 08:08:31 +01003146 queue_delayed_work(dev_priv->wq,
3147 &dev_priv->gt.retire_work,
Chris Wilsonbcb45082012-10-05 17:02:57 +01003148 round_jiffies_up_relative(HZ));
Chris Wilsonc9615612016-07-09 10:12:06 +01003149 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003150}
Chris Wilson891b48c2010-09-29 12:26:37 +01003151
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003152static void
3153i915_gem_idle_work_handler(struct work_struct *work)
3154{
3155 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003156 container_of(work, typeof(*dev_priv), gt.idle_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01003157 struct drm_device *dev = &dev_priv->drm;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003158 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303159 enum intel_engine_id id;
Chris Wilson67d97da2016-07-04 08:08:31 +01003160 bool rearm_hangcheck;
3161
3162 if (!READ_ONCE(dev_priv->gt.awake))
3163 return;
3164
Imre Deak0cb56702016-11-07 11:20:04 +02003165 /*
3166 * Wait for last execlists context complete, but bail out in case a
3167 * new request is submitted.
3168 */
Chris Wilson8490ae202017-03-30 15:50:37 +01003169 wait_for(intel_engines_are_idle(dev_priv), 10);
Chris Wilson28176ef2016-10-28 13:58:56 +01003170 if (READ_ONCE(dev_priv->gt.active_requests))
Chris Wilson67d97da2016-07-04 08:08:31 +01003171 return;
3172
3173 rearm_hangcheck =
3174 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
3175
3176 if (!mutex_trylock(&dev->struct_mutex)) {
3177 /* Currently busy, come back later */
3178 mod_delayed_work(dev_priv->wq,
3179 &dev_priv->gt.idle_work,
3180 msecs_to_jiffies(50));
3181 goto out_rearm;
3182 }
3183
Imre Deak93c97dc2016-11-07 11:20:03 +02003184 /*
3185 * New request retired after this work handler started, extend active
3186 * period until next instance of the work.
3187 */
3188 if (work_pending(work))
3189 goto out_unlock;
3190
Chris Wilson28176ef2016-10-28 13:58:56 +01003191 if (dev_priv->gt.active_requests)
Chris Wilson67d97da2016-07-04 08:08:31 +01003192 goto out_unlock;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003193
Chris Wilson05425242017-03-03 12:19:47 +00003194 if (wait_for(intel_engines_are_idle(dev_priv), 10))
Imre Deak0cb56702016-11-07 11:20:04 +02003195 DRM_ERROR("Timeout waiting for engines to idle\n");
3196
Chris Wilson67b807a82017-02-27 20:58:50 +00003197 for_each_engine(engine, dev_priv, id) {
3198 intel_engine_disarm_breadcrumbs(engine);
Chris Wilson67d97da2016-07-04 08:08:31 +01003199 i915_gem_batch_pool_fini(&engine->batch_pool);
Chris Wilson67b807a82017-02-27 20:58:50 +00003200 }
Chris Wilson47979482017-05-03 10:39:21 +01003201 i915_gem_timelines_mark_idle(dev_priv);
Zou Nan hai852835f2010-05-21 09:08:56 +08003202
Chris Wilson67d97da2016-07-04 08:08:31 +01003203 GEM_BUG_ON(!dev_priv->gt.awake);
3204 dev_priv->gt.awake = false;
3205 rearm_hangcheck = false;
Daniel Vetter30ecad72015-12-09 09:29:36 +01003206
Chris Wilson67d97da2016-07-04 08:08:31 +01003207 if (INTEL_GEN(dev_priv) >= 6)
3208 gen6_rps_idle(dev_priv);
3209 intel_runtime_pm_put(dev_priv);
3210out_unlock:
3211 mutex_unlock(&dev->struct_mutex);
Chris Wilson35c94182015-04-07 16:20:37 +01003212
Chris Wilson67d97da2016-07-04 08:08:31 +01003213out_rearm:
3214 if (rearm_hangcheck) {
3215 GEM_BUG_ON(!dev_priv->gt.awake);
3216 i915_queue_hangcheck(dev_priv);
Chris Wilson35c94182015-04-07 16:20:37 +01003217 }
Eric Anholt673a3942008-07-30 12:06:12 -07003218}
3219
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003220void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
3221{
3222 struct drm_i915_gem_object *obj = to_intel_bo(gem);
3223 struct drm_i915_file_private *fpriv = file->driver_priv;
3224 struct i915_vma *vma, *vn;
3225
3226 mutex_lock(&obj->base.dev->struct_mutex);
3227 list_for_each_entry_safe(vma, vn, &obj->vma_list, obj_link)
3228 if (vma->vm->file == fpriv)
3229 i915_vma_close(vma);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003230
3231 if (i915_gem_object_is_active(obj) &&
3232 !i915_gem_object_has_active_reference(obj)) {
3233 i915_gem_object_set_active_reference(obj);
3234 i915_gem_object_get(obj);
3235 }
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003236 mutex_unlock(&obj->base.dev->struct_mutex);
3237}
3238
Chris Wilsone95433c2016-10-28 13:58:27 +01003239static unsigned long to_wait_timeout(s64 timeout_ns)
3240{
3241 if (timeout_ns < 0)
3242 return MAX_SCHEDULE_TIMEOUT;
3243
3244 if (timeout_ns == 0)
3245 return 0;
3246
3247 return nsecs_to_jiffies_timeout(timeout_ns);
3248}
3249
Ben Widawsky5816d642012-04-11 11:18:19 -07003250/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003251 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003252 * @dev: drm device pointer
3253 * @data: ioctl data blob
3254 * @file: drm file pointer
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003255 *
3256 * Returns 0 if successful, else an error is returned with the remaining time in
3257 * the timeout parameter.
3258 * -ETIME: object is still busy after timeout
3259 * -ERESTARTSYS: signal interrupted the wait
3260 * -ENONENT: object doesn't exist
3261 * Also possible, but rare:
3262 * -EAGAIN: GPU wedged
3263 * -ENOMEM: damn
3264 * -ENODEV: Internal IRQ fail
3265 * -E?: The add request failed
3266 *
3267 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
3268 * non-zero timeout parameter the wait ioctl will wait for the given number of
3269 * nanoseconds on an object becoming unbusy. Since the wait itself does so
3270 * without holding struct_mutex the object may become re-busied before this
3271 * function completes. A similar but shorter * race condition exists in the busy
3272 * ioctl
3273 */
3274int
3275i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
3276{
3277 struct drm_i915_gem_wait *args = data;
3278 struct drm_i915_gem_object *obj;
Chris Wilsone95433c2016-10-28 13:58:27 +01003279 ktime_t start;
3280 long ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003281
Daniel Vetter11b5d512014-09-29 15:31:26 +02003282 if (args->flags != 0)
3283 return -EINVAL;
3284
Chris Wilson03ac0642016-07-20 13:31:51 +01003285 obj = i915_gem_object_lookup(file, args->bo_handle);
Chris Wilson033d5492016-08-05 10:14:17 +01003286 if (!obj)
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003287 return -ENOENT;
Chris Wilson033d5492016-08-05 10:14:17 +01003288
Chris Wilsone95433c2016-10-28 13:58:27 +01003289 start = ktime_get();
3290
3291 ret = i915_gem_object_wait(obj,
3292 I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL,
3293 to_wait_timeout(args->timeout_ns),
3294 to_rps_client(file));
3295
3296 if (args->timeout_ns > 0) {
3297 args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
3298 if (args->timeout_ns < 0)
3299 args->timeout_ns = 0;
Chris Wilsonc1d20612017-02-16 12:54:41 +00003300
3301 /*
3302 * Apparently ktime isn't accurate enough and occasionally has a
3303 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
3304 * things up to make the test happy. We allow up to 1 jiffy.
3305 *
3306 * This is a regression from the timespec->ktime conversion.
3307 */
3308 if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
3309 args->timeout_ns = 0;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003310 }
3311
Chris Wilsonf0cd5182016-10-28 13:58:43 +01003312 i915_gem_object_put(obj);
John Harrisonff865882014-11-24 18:49:28 +00003313 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003314}
3315
Chris Wilson73cb9702016-10-28 13:58:46 +01003316static int wait_for_timeline(struct i915_gem_timeline *tl, unsigned int flags)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003317{
Chris Wilson73cb9702016-10-28 13:58:46 +01003318 int ret, i;
3319
3320 for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
3321 ret = i915_gem_active_wait(&tl->engine[i].last_request, flags);
3322 if (ret)
3323 return ret;
3324 }
3325
3326 return 0;
3327}
3328
Chris Wilson25112b62017-03-30 15:50:39 +01003329static int wait_for_engine(struct intel_engine_cs *engine, int timeout_ms)
3330{
3331 return wait_for(intel_engine_is_idle(engine), timeout_ms);
3332}
3333
3334static int wait_for_engines(struct drm_i915_private *i915)
3335{
3336 struct intel_engine_cs *engine;
3337 enum intel_engine_id id;
3338
3339 for_each_engine(engine, i915, id) {
3340 if (GEM_WARN_ON(wait_for_engine(engine, 50))) {
3341 i915_gem_set_wedged(i915);
3342 return -EIO;
3343 }
3344
3345 GEM_BUG_ON(intel_engine_get_seqno(engine) !=
3346 intel_engine_last_submit(engine));
3347 }
3348
3349 return 0;
3350}
3351
Chris Wilson73cb9702016-10-28 13:58:46 +01003352int i915_gem_wait_for_idle(struct drm_i915_private *i915, unsigned int flags)
3353{
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003354 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003355
Chris Wilson9caa34a2016-11-11 14:58:08 +00003356 if (flags & I915_WAIT_LOCKED) {
3357 struct i915_gem_timeline *tl;
3358
3359 lockdep_assert_held(&i915->drm.struct_mutex);
3360
3361 list_for_each_entry(tl, &i915->gt.timelines, link) {
3362 ret = wait_for_timeline(tl, flags);
3363 if (ret)
3364 return ret;
3365 }
Chris Wilson72022a72017-03-30 15:50:38 +01003366
3367 i915_gem_retire_requests(i915);
3368 GEM_BUG_ON(i915->gt.active_requests);
Chris Wilson25112b62017-03-30 15:50:39 +01003369
3370 ret = wait_for_engines(i915);
Chris Wilson9caa34a2016-11-11 14:58:08 +00003371 } else {
3372 ret = wait_for_timeline(&i915->gt.global_timeline, flags);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003373 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003374
Chris Wilson25112b62017-03-30 15:50:39 +01003375 return ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003376}
3377
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003378static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj)
3379{
3380 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU && !obj->cache_dirty)
3381 return;
3382
Chris Wilson57822dc2017-02-22 11:40:48 +00003383 i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE);
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003384 obj->base.write_domain = 0;
3385}
3386
3387void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj)
3388{
3389 if (!READ_ONCE(obj->pin_display))
3390 return;
3391
3392 mutex_lock(&obj->base.dev->struct_mutex);
3393 __i915_gem_object_flush_for_display(obj);
3394 mutex_unlock(&obj->base.dev->struct_mutex);
3395}
3396
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003397/**
Chris Wilsone22d8e32017-04-12 12:01:11 +01003398 * Moves a single object to the WC read, and possibly write domain.
3399 * @obj: object to act on
3400 * @write: ask for write access or read only
3401 *
3402 * This function returns when the move is complete, including waiting on
3403 * flushes to occur.
3404 */
3405int
3406i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write)
3407{
3408 int ret;
3409
3410 lockdep_assert_held(&obj->base.dev->struct_mutex);
3411
3412 ret = i915_gem_object_wait(obj,
3413 I915_WAIT_INTERRUPTIBLE |
3414 I915_WAIT_LOCKED |
3415 (write ? I915_WAIT_ALL : 0),
3416 MAX_SCHEDULE_TIMEOUT,
3417 NULL);
3418 if (ret)
3419 return ret;
3420
3421 if (obj->base.write_domain == I915_GEM_DOMAIN_WC)
3422 return 0;
3423
3424 /* Flush and acquire obj->pages so that we are coherent through
3425 * direct access in memory with previous cached writes through
3426 * shmemfs and that our cache domain tracking remains valid.
3427 * For example, if the obj->filp was moved to swap without us
3428 * being notified and releasing the pages, we would mistakenly
3429 * continue to assume that the obj remained out of the CPU cached
3430 * domain.
3431 */
3432 ret = i915_gem_object_pin_pages(obj);
3433 if (ret)
3434 return ret;
3435
3436 flush_write_domain(obj, ~I915_GEM_DOMAIN_WC);
3437
3438 /* Serialise direct access to this object with the barriers for
3439 * coherent writes from the GPU, by effectively invalidating the
3440 * WC domain upon first access.
3441 */
3442 if ((obj->base.read_domains & I915_GEM_DOMAIN_WC) == 0)
3443 mb();
3444
3445 /* It should now be out of any other write domains, and we can update
3446 * the domain values for our changes.
3447 */
3448 GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_WC) != 0);
3449 obj->base.read_domains |= I915_GEM_DOMAIN_WC;
3450 if (write) {
3451 obj->base.read_domains = I915_GEM_DOMAIN_WC;
3452 obj->base.write_domain = I915_GEM_DOMAIN_WC;
3453 obj->mm.dirty = true;
3454 }
3455
3456 i915_gem_object_unpin_pages(obj);
3457 return 0;
3458}
3459
3460/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003461 * Moves a single object to the GTT read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003462 * @obj: object to act on
3463 * @write: ask for write access or read only
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003464 *
3465 * This function returns when the move is complete, including waiting on
3466 * flushes to occur.
3467 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003468int
Chris Wilson20217462010-11-23 15:26:33 +00003469i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003470{
Eric Anholte47c68e2008-11-14 13:35:19 -08003471 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003472
Chris Wilsone95433c2016-10-28 13:58:27 +01003473 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003474
Chris Wilsone95433c2016-10-28 13:58:27 +01003475 ret = i915_gem_object_wait(obj,
3476 I915_WAIT_INTERRUPTIBLE |
3477 I915_WAIT_LOCKED |
3478 (write ? I915_WAIT_ALL : 0),
3479 MAX_SCHEDULE_TIMEOUT,
3480 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00003481 if (ret)
3482 return ret;
3483
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003484 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
3485 return 0;
3486
Chris Wilson43566de2015-01-02 16:29:29 +05303487 /* Flush and acquire obj->pages so that we are coherent through
3488 * direct access in memory with previous cached writes through
3489 * shmemfs and that our cache domain tracking remains valid.
3490 * For example, if the obj->filp was moved to swap without us
3491 * being notified and releasing the pages, we would mistakenly
3492 * continue to assume that the obj remained out of the CPU cached
3493 * domain.
3494 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003495 ret = i915_gem_object_pin_pages(obj);
Chris Wilson43566de2015-01-02 16:29:29 +05303496 if (ret)
3497 return ret;
3498
Chris Wilsonef749212017-04-12 12:01:10 +01003499 flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003500
Chris Wilsond0a57782012-10-09 19:24:37 +01003501 /* Serialise direct access to this object with the barriers for
3502 * coherent writes from the GPU, by effectively invalidating the
3503 * GTT domain upon first access.
3504 */
3505 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
3506 mb();
3507
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003508 /* It should now be out of any other write domains, and we can update
3509 * the domain values for our changes.
3510 */
Chris Wilson40e62d52016-10-28 13:58:41 +01003511 GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
Chris Wilson05394f32010-11-08 19:18:58 +00003512 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003513 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003514 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
3515 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003516 obj->mm.dirty = true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003517 }
3518
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003519 i915_gem_object_unpin_pages(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003520 return 0;
3521}
3522
Chris Wilsonef55f922015-10-09 14:11:27 +01003523/**
3524 * Changes the cache-level of an object across all VMA.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003525 * @obj: object to act on
3526 * @cache_level: new cache level to set for the object
Chris Wilsonef55f922015-10-09 14:11:27 +01003527 *
3528 * After this function returns, the object will be in the new cache-level
3529 * across all GTT and the contents of the backing storage will be coherent,
3530 * with respect to the new cache-level. In order to keep the backing storage
3531 * coherent for all users, we only allow a single cache level to be set
3532 * globally on the object and prevent it from being changed whilst the
3533 * hardware is reading from the object. That is if the object is currently
3534 * on the scanout it will be set to uncached (or equivalent display
3535 * cache coherency) and all non-MOCS GPU access will also be uncached so
3536 * that all direct access to the scanout remains coherent.
3537 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003538int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3539 enum i915_cache_level cache_level)
3540{
Chris Wilsonaa653a62016-08-04 07:52:27 +01003541 struct i915_vma *vma;
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003542 int ret;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003543
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003544 lockdep_assert_held(&obj->base.dev->struct_mutex);
3545
Chris Wilsone4ffd172011-04-04 09:44:39 +01003546 if (obj->cache_level == cache_level)
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003547 return 0;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003548
Chris Wilsonef55f922015-10-09 14:11:27 +01003549 /* Inspect the list of currently bound VMA and unbind any that would
3550 * be invalid given the new cache-level. This is principally to
3551 * catch the issue of the CS prefetch crossing page boundaries and
3552 * reading an invalid PTE on older architectures.
3553 */
Chris Wilsonaa653a62016-08-04 07:52:27 +01003554restart:
3555 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003556 if (!drm_mm_node_allocated(&vma->node))
3557 continue;
3558
Chris Wilson20dfbde2016-08-04 16:32:30 +01003559 if (i915_vma_is_pinned(vma)) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003560 DRM_DEBUG("can not change the cache level of pinned objects\n");
3561 return -EBUSY;
3562 }
3563
Chris Wilsonaa653a62016-08-04 07:52:27 +01003564 if (i915_gem_valid_gtt_space(vma, cache_level))
3565 continue;
3566
3567 ret = i915_vma_unbind(vma);
3568 if (ret)
3569 return ret;
3570
3571 /* As unbinding may affect other elements in the
3572 * obj->vma_list (due to side-effects from retiring
3573 * an active vma), play safe and restart the iterator.
3574 */
3575 goto restart;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003576 }
3577
Chris Wilsonef55f922015-10-09 14:11:27 +01003578 /* We can reuse the existing drm_mm nodes but need to change the
3579 * cache-level on the PTE. We could simply unbind them all and
3580 * rebind with the correct cache-level on next use. However since
3581 * we already have a valid slot, dma mapping, pages etc, we may as
3582 * rewrite the PTE in the belief that doing so tramples upon less
3583 * state and so involves less work.
3584 */
Chris Wilson15717de2016-08-04 07:52:26 +01003585 if (obj->bind_count) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003586 /* Before we change the PTE, the GPU must not be accessing it.
3587 * If we wait upon the object, we know that all the bound
3588 * VMA are no longer active.
3589 */
Chris Wilsone95433c2016-10-28 13:58:27 +01003590 ret = i915_gem_object_wait(obj,
3591 I915_WAIT_INTERRUPTIBLE |
3592 I915_WAIT_LOCKED |
3593 I915_WAIT_ALL,
3594 MAX_SCHEDULE_TIMEOUT,
3595 NULL);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003596 if (ret)
3597 return ret;
3598
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00003599 if (!HAS_LLC(to_i915(obj->base.dev)) &&
3600 cache_level != I915_CACHE_NONE) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003601 /* Access to snoopable pages through the GTT is
3602 * incoherent and on some machines causes a hard
3603 * lockup. Relinquish the CPU mmaping to force
3604 * userspace to refault in the pages and we can
3605 * then double check if the GTT mapping is still
3606 * valid for that pointer access.
3607 */
3608 i915_gem_release_mmap(obj);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003609
Chris Wilsonef55f922015-10-09 14:11:27 +01003610 /* As we no longer need a fence for GTT access,
3611 * we can relinquish it now (and so prevent having
3612 * to steal a fence from someone else on the next
3613 * fence request). Note GPU activity would have
3614 * dropped the fence as all snoopable access is
3615 * supposed to be linear.
3616 */
Chris Wilson49ef5292016-08-18 17:17:00 +01003617 list_for_each_entry(vma, &obj->vma_list, obj_link) {
3618 ret = i915_vma_put_fence(vma);
3619 if (ret)
3620 return ret;
3621 }
Chris Wilsonef55f922015-10-09 14:11:27 +01003622 } else {
3623 /* We either have incoherent backing store and
3624 * so no GTT access or the architecture is fully
3625 * coherent. In such cases, existing GTT mmaps
3626 * ignore the cache bit in the PTE and we can
3627 * rewrite it without confusing the GPU or having
3628 * to force userspace to fault back in its mmaps.
3629 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003630 }
3631
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003632 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003633 if (!drm_mm_node_allocated(&vma->node))
3634 continue;
3635
3636 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
3637 if (ret)
3638 return ret;
3639 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01003640 }
3641
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003642 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU &&
Chris Wilsone59dc172017-02-22 11:40:45 +00003643 i915_gem_object_is_coherent(obj))
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003644 obj->cache_dirty = true;
3645
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003646 list_for_each_entry(vma, &obj->vma_list, obj_link)
Chris Wilson2c225692013-08-09 12:26:45 +01003647 vma->node.color = cache_level;
3648 obj->cache_level = cache_level;
3649
Chris Wilsone4ffd172011-04-04 09:44:39 +01003650 return 0;
3651}
3652
Ben Widawsky199adf42012-09-21 17:01:20 -07003653int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
3654 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003655{
Ben Widawsky199adf42012-09-21 17:01:20 -07003656 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003657 struct drm_i915_gem_object *obj;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003658 int err = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003659
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003660 rcu_read_lock();
3661 obj = i915_gem_object_lookup_rcu(file, args->handle);
3662 if (!obj) {
3663 err = -ENOENT;
3664 goto out;
3665 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01003666
Chris Wilson651d7942013-08-08 14:41:10 +01003667 switch (obj->cache_level) {
3668 case I915_CACHE_LLC:
3669 case I915_CACHE_L3_LLC:
3670 args->caching = I915_CACHING_CACHED;
3671 break;
3672
Chris Wilson4257d3b2013-08-08 14:41:11 +01003673 case I915_CACHE_WT:
3674 args->caching = I915_CACHING_DISPLAY;
3675 break;
3676
Chris Wilson651d7942013-08-08 14:41:10 +01003677 default:
3678 args->caching = I915_CACHING_NONE;
3679 break;
3680 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003681out:
3682 rcu_read_unlock();
3683 return err;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003684}
3685
Ben Widawsky199adf42012-09-21 17:01:20 -07003686int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
3687 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003688{
Chris Wilson9c870d02016-10-24 13:42:15 +01003689 struct drm_i915_private *i915 = to_i915(dev);
Ben Widawsky199adf42012-09-21 17:01:20 -07003690 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003691 struct drm_i915_gem_object *obj;
3692 enum i915_cache_level level;
Chris Wilsond65415d2017-01-19 08:22:10 +00003693 int ret = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003694
Ben Widawsky199adf42012-09-21 17:01:20 -07003695 switch (args->caching) {
3696 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01003697 level = I915_CACHE_NONE;
3698 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07003699 case I915_CACHING_CACHED:
Imre Deake5756c12015-08-14 18:43:30 +03003700 /*
3701 * Due to a HW issue on BXT A stepping, GPU stores via a
3702 * snooped mapping may leave stale data in a corresponding CPU
3703 * cacheline, whereas normally such cachelines would get
3704 * invalidated.
3705 */
Chris Wilson9c870d02016-10-24 13:42:15 +01003706 if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
Imre Deake5756c12015-08-14 18:43:30 +03003707 return -ENODEV;
3708
Chris Wilsone6994ae2012-07-10 10:27:08 +01003709 level = I915_CACHE_LLC;
3710 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003711 case I915_CACHING_DISPLAY:
Chris Wilson9c870d02016-10-24 13:42:15 +01003712 level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003713 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003714 default:
3715 return -EINVAL;
3716 }
3717
Chris Wilsond65415d2017-01-19 08:22:10 +00003718 obj = i915_gem_object_lookup(file, args->handle);
3719 if (!obj)
3720 return -ENOENT;
3721
3722 if (obj->cache_level == level)
3723 goto out;
3724
3725 ret = i915_gem_object_wait(obj,
3726 I915_WAIT_INTERRUPTIBLE,
3727 MAX_SCHEDULE_TIMEOUT,
3728 to_rps_client(file));
3729 if (ret)
3730 goto out;
3731
Ben Widawsky3bc29132012-09-26 16:15:20 -07003732 ret = i915_mutex_lock_interruptible(dev);
3733 if (ret)
Chris Wilsond65415d2017-01-19 08:22:10 +00003734 goto out;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003735
3736 ret = i915_gem_object_set_cache_level(obj, level);
Chris Wilsone6994ae2012-07-10 10:27:08 +01003737 mutex_unlock(&dev->struct_mutex);
Chris Wilsond65415d2017-01-19 08:22:10 +00003738
3739out:
3740 i915_gem_object_put(obj);
Chris Wilsone6994ae2012-07-10 10:27:08 +01003741 return ret;
3742}
3743
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003744/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003745 * Prepare buffer for display plane (scanout, cursors, etc).
3746 * Can be called from an uninterruptible phase (modesetting) and allows
3747 * any flushes to be pipelined (for pageflips).
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003748 */
Chris Wilson058d88c2016-08-15 10:49:06 +01003749struct i915_vma *
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003750i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3751 u32 alignment,
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003752 const struct i915_ggtt_view *view)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003753{
Chris Wilson058d88c2016-08-15 10:49:06 +01003754 struct i915_vma *vma;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003755 int ret;
3756
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003757 lockdep_assert_held(&obj->base.dev->struct_mutex);
3758
Chris Wilsoncc98b412013-08-09 12:25:09 +01003759 /* Mark the pin_display early so that we account for the
3760 * display coherency whilst setting up the cache domains.
3761 */
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003762 obj->pin_display++;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003763
Eric Anholta7ef0642011-03-29 16:59:54 -07003764 /* The display engine is not coherent with the LLC cache on gen6. As
3765 * a result, we make sure that the pinning that is about to occur is
3766 * done with uncached PTEs. This is lowest common denominator for all
3767 * chipsets.
3768 *
3769 * However for gen6+, we could do better by using the GFDT bit instead
3770 * of uncaching, which would allow us to flush all the LLC-cached data
3771 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3772 */
Chris Wilson651d7942013-08-08 14:41:10 +01003773 ret = i915_gem_object_set_cache_level(obj,
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003774 HAS_WT(to_i915(obj->base.dev)) ?
3775 I915_CACHE_WT : I915_CACHE_NONE);
Chris Wilson058d88c2016-08-15 10:49:06 +01003776 if (ret) {
3777 vma = ERR_PTR(ret);
Chris Wilsoncc98b412013-08-09 12:25:09 +01003778 goto err_unpin_display;
Chris Wilson058d88c2016-08-15 10:49:06 +01003779 }
Eric Anholta7ef0642011-03-29 16:59:54 -07003780
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003781 /* As the user may map the buffer once pinned in the display plane
3782 * (e.g. libkms for the bootup splash), we have to ensure that we
Chris Wilson2efb8132016-08-18 17:17:06 +01003783 * always use map_and_fenceable for all scanout buffers. However,
3784 * it may simply be too big to fit into mappable, in which case
3785 * put it anyway and hope that userspace can cope (but always first
3786 * try to preserve the existing ABI).
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003787 */
Chris Wilson2efb8132016-08-18 17:17:06 +01003788 vma = ERR_PTR(-ENOSPC);
Chris Wilson47a8e3f2017-01-14 00:28:27 +00003789 if (!view || view->type == I915_GGTT_VIEW_NORMAL)
Chris Wilson2efb8132016-08-18 17:17:06 +01003790 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
3791 PIN_MAPPABLE | PIN_NONBLOCK);
Chris Wilson767a2222016-11-07 11:01:28 +00003792 if (IS_ERR(vma)) {
3793 struct drm_i915_private *i915 = to_i915(obj->base.dev);
3794 unsigned int flags;
3795
3796 /* Valleyview is definitely limited to scanning out the first
3797 * 512MiB. Lets presume this behaviour was inherited from the
3798 * g4x display engine and that all earlier gen are similarly
3799 * limited. Testing suggests that it is a little more
3800 * complicated than this. For example, Cherryview appears quite
3801 * happy to scanout from anywhere within its global aperture.
3802 */
3803 flags = 0;
3804 if (HAS_GMCH_DISPLAY(i915))
3805 flags = PIN_MAPPABLE;
3806 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags);
3807 }
Chris Wilson058d88c2016-08-15 10:49:06 +01003808 if (IS_ERR(vma))
Chris Wilsoncc98b412013-08-09 12:25:09 +01003809 goto err_unpin_display;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003810
Chris Wilsond8923dc2016-08-18 17:17:07 +01003811 vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
3812
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003813 /* Treat this as an end-of-frame, like intel_user_framebuffer_dirty() */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003814 __i915_gem_object_flush_for_display(obj);
Chris Wilsond59b21e2017-02-22 11:40:49 +00003815 intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01003816
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003817 /* It should now be out of any other write domains, and we can update
3818 * the domain values for our changes.
3819 */
Chris Wilson05394f32010-11-08 19:18:58 +00003820 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003821
Chris Wilson058d88c2016-08-15 10:49:06 +01003822 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003823
3824err_unpin_display:
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003825 obj->pin_display--;
Chris Wilson058d88c2016-08-15 10:49:06 +01003826 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003827}
3828
3829void
Chris Wilson058d88c2016-08-15 10:49:06 +01003830i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
Chris Wilsoncc98b412013-08-09 12:25:09 +01003831{
Chris Wilson49d73912016-11-29 09:50:08 +00003832 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003833
Chris Wilson058d88c2016-08-15 10:49:06 +01003834 if (WARN_ON(vma->obj->pin_display == 0))
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003835 return;
3836
Chris Wilsond8923dc2016-08-18 17:17:07 +01003837 if (--vma->obj->pin_display == 0)
Chris Wilsonf51455d2017-01-10 14:47:34 +00003838 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003839
Chris Wilson383d5822016-08-18 17:17:08 +01003840 /* Bump the LRU to try and avoid premature eviction whilst flipping */
Chris Wilsonbefedbb2017-01-19 19:26:55 +00003841 i915_gem_object_bump_inactive_ggtt(vma->obj);
Chris Wilson383d5822016-08-18 17:17:08 +01003842
Chris Wilson058d88c2016-08-15 10:49:06 +01003843 i915_vma_unpin(vma);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003844}
3845
Eric Anholte47c68e2008-11-14 13:35:19 -08003846/**
3847 * Moves a single object to the CPU read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003848 * @obj: object to act on
3849 * @write: requesting write or read-only access
Eric Anholte47c68e2008-11-14 13:35:19 -08003850 *
3851 * This function returns when the move is complete, including waiting on
3852 * flushes to occur.
3853 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02003854int
Chris Wilson919926a2010-11-12 13:42:53 +00003855i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08003856{
Eric Anholte47c68e2008-11-14 13:35:19 -08003857 int ret;
3858
Chris Wilsone95433c2016-10-28 13:58:27 +01003859 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003860
Chris Wilsone95433c2016-10-28 13:58:27 +01003861 ret = i915_gem_object_wait(obj,
3862 I915_WAIT_INTERRUPTIBLE |
3863 I915_WAIT_LOCKED |
3864 (write ? I915_WAIT_ALL : 0),
3865 MAX_SCHEDULE_TIMEOUT,
3866 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00003867 if (ret)
3868 return ret;
3869
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003870 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
3871 return 0;
3872
Chris Wilsonef749212017-04-12 12:01:10 +01003873 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08003874
Eric Anholte47c68e2008-11-14 13:35:19 -08003875 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00003876 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson57822dc2017-02-22 11:40:48 +00003877 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
Chris Wilson05394f32010-11-08 19:18:58 +00003878 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003879 }
3880
3881 /* It should now be out of any other write domains, and we can update
3882 * the domain values for our changes.
3883 */
Chris Wilson40e62d52016-10-28 13:58:41 +01003884 GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003885
3886 /* If we're writing through the CPU, then the GPU read domains will
3887 * need to be invalidated at next use.
3888 */
3889 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003890 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3891 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003892 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003893
3894 return 0;
3895}
3896
Eric Anholt673a3942008-07-30 12:06:12 -07003897/* Throttle our rendering by waiting until the ring has completed our requests
3898 * emitted over 20 msec ago.
3899 *
Eric Anholtb9624422009-06-03 07:27:35 +00003900 * Note that if we were to use the current jiffies each time around the loop,
3901 * we wouldn't escape the function with any frames outstanding if the time to
3902 * render a frame was over 20ms.
3903 *
Eric Anholt673a3942008-07-30 12:06:12 -07003904 * This should get us reasonable parallelism between CPU and GPU but also
3905 * relatively low latency when blocking on a particular request to finish.
3906 */
3907static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003908i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003909{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003910 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003911 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsond0bc54f2015-05-21 21:01:48 +01003912 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
John Harrison54fb2412014-11-24 18:49:27 +00003913 struct drm_i915_gem_request *request, *target = NULL;
Chris Wilsone95433c2016-10-28 13:58:27 +01003914 long ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003915
Chris Wilsonf4457ae2016-04-13 17:35:08 +01003916 /* ABI: return -EIO if already wedged */
3917 if (i915_terminally_wedged(&dev_priv->gpu_error))
3918 return -EIO;
Chris Wilsone110e8d2011-01-26 15:39:14 +00003919
Chris Wilson1c255952010-09-26 11:03:27 +01003920 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00003921 list_for_each_entry(request, &file_priv->mm.request_list, client_link) {
Eric Anholtb9624422009-06-03 07:27:35 +00003922 if (time_after_eq(request->emitted_jiffies, recent_enough))
3923 break;
3924
Chris Wilsonc8659ef2017-03-02 12:25:25 +00003925 if (target) {
3926 list_del(&target->client_link);
3927 target->file_priv = NULL;
3928 }
John Harrisonfcfa423c2015-05-29 17:44:12 +01003929
John Harrison54fb2412014-11-24 18:49:27 +00003930 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00003931 }
John Harrisonff865882014-11-24 18:49:28 +00003932 if (target)
Chris Wilsone8a261e2016-07-20 13:31:49 +01003933 i915_gem_request_get(target);
Chris Wilson1c255952010-09-26 11:03:27 +01003934 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003935
John Harrison54fb2412014-11-24 18:49:27 +00003936 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003937 return 0;
3938
Chris Wilsone95433c2016-10-28 13:58:27 +01003939 ret = i915_wait_request(target,
3940 I915_WAIT_INTERRUPTIBLE,
3941 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone8a261e2016-07-20 13:31:49 +01003942 i915_gem_request_put(target);
John Harrisonff865882014-11-24 18:49:28 +00003943
Chris Wilsone95433c2016-10-28 13:58:27 +01003944 return ret < 0 ? ret : 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003945}
3946
Chris Wilson058d88c2016-08-15 10:49:06 +01003947struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003948i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
3949 const struct i915_ggtt_view *view,
Chris Wilson91b2db62016-08-04 16:32:23 +01003950 u64 size,
Chris Wilson2ffffd02016-08-04 16:32:22 +01003951 u64 alignment,
3952 u64 flags)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003953{
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003954 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
3955 struct i915_address_space *vm = &dev_priv->ggtt.base;
Chris Wilson59bfa122016-08-04 16:32:31 +01003956 struct i915_vma *vma;
3957 int ret;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003958
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003959 lockdep_assert_held(&obj->base.dev->struct_mutex);
3960
Chris Wilson718659a2017-01-16 15:21:28 +00003961 vma = i915_vma_instance(obj, vm, view);
Chris Wilsone0216b72017-01-19 19:26:57 +00003962 if (unlikely(IS_ERR(vma)))
Chris Wilson058d88c2016-08-15 10:49:06 +01003963 return vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01003964
3965 if (i915_vma_misplaced(vma, size, alignment, flags)) {
3966 if (flags & PIN_NONBLOCK &&
3967 (i915_vma_is_pinned(vma) || i915_vma_is_active(vma)))
Chris Wilson058d88c2016-08-15 10:49:06 +01003968 return ERR_PTR(-ENOSPC);
Chris Wilson59bfa122016-08-04 16:32:31 +01003969
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003970 if (flags & PIN_MAPPABLE) {
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003971 /* If the required space is larger than the available
3972 * aperture, we will not able to find a slot for the
3973 * object and unbinding the object now will be in
3974 * vain. Worse, doing so may cause us to ping-pong
3975 * the object in and out of the Global GTT and
3976 * waste a lot of cycles under the mutex.
3977 */
Chris Wilson944397f2017-01-09 16:16:11 +00003978 if (vma->fence_size > dev_priv->ggtt.mappable_end)
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003979 return ERR_PTR(-E2BIG);
3980
3981 /* If NONBLOCK is set the caller is optimistically
3982 * trying to cache the full object within the mappable
3983 * aperture, and *must* have a fallback in place for
3984 * situations where we cannot bind the object. We
3985 * can be a little more lax here and use the fallback
3986 * more often to avoid costly migrations of ourselves
3987 * and other objects within the aperture.
3988 *
3989 * Half-the-aperture is used as a simple heuristic.
3990 * More interesting would to do search for a free
3991 * block prior to making the commitment to unbind.
3992 * That caters for the self-harm case, and with a
3993 * little more heuristics (e.g. NOFAULT, NOEVICT)
3994 * we could try to minimise harm to others.
3995 */
3996 if (flags & PIN_NONBLOCK &&
Chris Wilson944397f2017-01-09 16:16:11 +00003997 vma->fence_size > dev_priv->ggtt.mappable_end / 2)
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003998 return ERR_PTR(-ENOSPC);
3999 }
4000
Chris Wilson59bfa122016-08-04 16:32:31 +01004001 WARN(i915_vma_is_pinned(vma),
4002 "bo is already pinned in ggtt with incorrect alignment:"
Chris Wilson05a20d02016-08-18 17:16:55 +01004003 " offset=%08x, req.alignment=%llx,"
4004 " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
4005 i915_ggtt_offset(vma), alignment,
Chris Wilson59bfa122016-08-04 16:32:31 +01004006 !!(flags & PIN_MAPPABLE),
Chris Wilson05a20d02016-08-18 17:16:55 +01004007 i915_vma_is_map_and_fenceable(vma));
Chris Wilson59bfa122016-08-04 16:32:31 +01004008 ret = i915_vma_unbind(vma);
4009 if (ret)
Chris Wilson058d88c2016-08-15 10:49:06 +01004010 return ERR_PTR(ret);
Chris Wilson59bfa122016-08-04 16:32:31 +01004011 }
4012
Chris Wilson058d88c2016-08-15 10:49:06 +01004013 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
4014 if (ret)
4015 return ERR_PTR(ret);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004016
Chris Wilson058d88c2016-08-15 10:49:06 +01004017 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07004018}
4019
Chris Wilsonedf6b762016-08-09 09:23:33 +01004020static __always_inline unsigned int __busy_read_flag(unsigned int id)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004021{
4022 /* Note that we could alias engines in the execbuf API, but
4023 * that would be very unwise as it prevents userspace from
4024 * fine control over engine selection. Ahem.
4025 *
4026 * This should be something like EXEC_MAX_ENGINE instead of
4027 * I915_NUM_ENGINES.
4028 */
4029 BUILD_BUG_ON(I915_NUM_ENGINES > 16);
4030 return 0x10000 << id;
4031}
4032
4033static __always_inline unsigned int __busy_write_id(unsigned int id)
4034{
Chris Wilson70cb4722016-08-09 18:08:25 +01004035 /* The uABI guarantees an active writer is also amongst the read
4036 * engines. This would be true if we accessed the activity tracking
4037 * under the lock, but as we perform the lookup of the object and
4038 * its activity locklessly we can not guarantee that the last_write
4039 * being active implies that we have set the same engine flag from
4040 * last_read - hence we always set both read and write busy for
4041 * last_write.
4042 */
4043 return id | __busy_read_flag(id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004044}
4045
Chris Wilsonedf6b762016-08-09 09:23:33 +01004046static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004047__busy_set_if_active(const struct dma_fence *fence,
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004048 unsigned int (*flag)(unsigned int id))
4049{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004050 struct drm_i915_gem_request *rq;
Chris Wilson12555012016-08-16 09:50:40 +01004051
Chris Wilsond07f0e52016-10-28 13:58:44 +01004052 /* We have to check the current hw status of the fence as the uABI
4053 * guarantees forward progress. We could rely on the idle worker
4054 * to eventually flush us, but to minimise latency just ask the
4055 * hardware.
4056 *
4057 * Note we only report on the status of native fences.
4058 */
4059 if (!dma_fence_is_i915(fence))
Chris Wilson12555012016-08-16 09:50:40 +01004060 return 0;
4061
Chris Wilsond07f0e52016-10-28 13:58:44 +01004062 /* opencode to_request() in order to avoid const warnings */
4063 rq = container_of(fence, struct drm_i915_gem_request, fence);
4064 if (i915_gem_request_completed(rq))
4065 return 0;
4066
Chris Wilson1d39f282017-04-11 13:43:06 +01004067 return flag(rq->engine->uabi_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004068}
4069
Chris Wilsonedf6b762016-08-09 09:23:33 +01004070static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004071busy_check_reader(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004072{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004073 return __busy_set_if_active(fence, __busy_read_flag);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004074}
4075
Chris Wilsonedf6b762016-08-09 09:23:33 +01004076static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004077busy_check_writer(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004078{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004079 if (!fence)
4080 return 0;
4081
4082 return __busy_set_if_active(fence, __busy_write_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004083}
4084
Eric Anholt673a3942008-07-30 12:06:12 -07004085int
Eric Anholt673a3942008-07-30 12:06:12 -07004086i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004087 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004088{
4089 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004090 struct drm_i915_gem_object *obj;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004091 struct reservation_object_list *list;
4092 unsigned int seq;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004093 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07004094
Chris Wilsond07f0e52016-10-28 13:58:44 +01004095 err = -ENOENT;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004096 rcu_read_lock();
4097 obj = i915_gem_object_lookup_rcu(file, args->handle);
Chris Wilsond07f0e52016-10-28 13:58:44 +01004098 if (!obj)
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004099 goto out;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004100
4101 /* A discrepancy here is that we do not report the status of
4102 * non-i915 fences, i.e. even though we may report the object as idle,
4103 * a call to set-domain may still stall waiting for foreign rendering.
4104 * This also means that wait-ioctl may report an object as busy,
4105 * where busy-ioctl considers it idle.
4106 *
4107 * We trade the ability to warn of foreign fences to report on which
4108 * i915 engines are active for the object.
4109 *
4110 * Alternatively, we can trade that extra information on read/write
4111 * activity with
4112 * args->busy =
4113 * !reservation_object_test_signaled_rcu(obj->resv, true);
4114 * to report the overall busyness. This is what the wait-ioctl does.
4115 *
4116 */
4117retry:
4118 seq = raw_read_seqcount(&obj->resv->seq);
4119
4120 /* Translate the exclusive fence to the READ *and* WRITE engine */
4121 args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl));
4122
4123 /* Translate shared fences to READ set of engines */
4124 list = rcu_dereference(obj->resv->fence);
4125 if (list) {
4126 unsigned int shared_count = list->shared_count, i;
4127
4128 for (i = 0; i < shared_count; ++i) {
4129 struct dma_fence *fence =
4130 rcu_dereference(list->shared[i]);
4131
4132 args->busy |= busy_check_reader(fence);
4133 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004134 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08004135
Chris Wilsond07f0e52016-10-28 13:58:44 +01004136 if (args->busy && read_seqcount_retry(&obj->resv->seq, seq))
4137 goto retry;
Chris Wilson426960b2016-01-15 16:51:46 +00004138
Chris Wilsond07f0e52016-10-28 13:58:44 +01004139 err = 0;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004140out:
4141 rcu_read_unlock();
4142 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07004143}
4144
4145int
4146i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4147 struct drm_file *file_priv)
4148{
Akshay Joshi0206e352011-08-16 15:34:10 -04004149 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004150}
4151
Chris Wilson3ef94da2009-09-14 16:50:29 +01004152int
4153i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4154 struct drm_file *file_priv)
4155{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004156 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004157 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004158 struct drm_i915_gem_object *obj;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004159 int err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004160
4161 switch (args->madv) {
4162 case I915_MADV_DONTNEED:
4163 case I915_MADV_WILLNEED:
4164 break;
4165 default:
4166 return -EINVAL;
4167 }
4168
Chris Wilson03ac0642016-07-20 13:31:51 +01004169 obj = i915_gem_object_lookup(file_priv, args->handle);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004170 if (!obj)
4171 return -ENOENT;
4172
4173 err = mutex_lock_interruptible(&obj->mm.lock);
4174 if (err)
4175 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004176
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004177 if (obj->mm.pages &&
Chris Wilson3e510a82016-08-05 10:14:23 +01004178 i915_gem_object_is_tiled(obj) &&
Daniel Vetter656bfa32014-11-20 09:26:30 +01004179 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004180 if (obj->mm.madv == I915_MADV_WILLNEED) {
4181 GEM_BUG_ON(!obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004182 __i915_gem_object_unpin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004183 obj->mm.quirked = false;
4184 }
4185 if (args->madv == I915_MADV_WILLNEED) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00004186 GEM_BUG_ON(obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004187 __i915_gem_object_pin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004188 obj->mm.quirked = true;
4189 }
Daniel Vetter656bfa32014-11-20 09:26:30 +01004190 }
4191
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004192 if (obj->mm.madv != __I915_MADV_PURGED)
4193 obj->mm.madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004194
Chris Wilson6c085a72012-08-20 11:40:46 +02004195 /* if the object is no longer attached, discard its backing storage */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004196 if (obj->mm.madv == I915_MADV_DONTNEED && !obj->mm.pages)
Chris Wilson2d7ef392009-09-20 23:13:10 +01004197 i915_gem_object_truncate(obj);
4198
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004199 args->retained = obj->mm.madv != __I915_MADV_PURGED;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004200 mutex_unlock(&obj->mm.lock);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004201
Chris Wilson1233e2d2016-10-28 13:58:37 +01004202out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004203 i915_gem_object_put(obj);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004204 return err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004205}
4206
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004207static void
4208frontbuffer_retire(struct i915_gem_active *active,
4209 struct drm_i915_gem_request *request)
4210{
4211 struct drm_i915_gem_object *obj =
4212 container_of(active, typeof(*obj), frontbuffer_write);
4213
Chris Wilsond59b21e2017-02-22 11:40:49 +00004214 intel_fb_obj_flush(obj, ORIGIN_CS);
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004215}
4216
Chris Wilson37e680a2012-06-07 15:38:42 +01004217void i915_gem_object_init(struct drm_i915_gem_object *obj,
4218 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01004219{
Chris Wilson1233e2d2016-10-28 13:58:37 +01004220 mutex_init(&obj->mm.lock);
4221
Joonas Lahtinen56cea322016-11-02 12:16:04 +02004222 INIT_LIST_HEAD(&obj->global_link);
Chris Wilson275f0392016-10-24 13:42:14 +01004223 INIT_LIST_HEAD(&obj->userfault_link);
Ben Widawskyb25cb2f2013-08-14 11:38:33 +02004224 INIT_LIST_HEAD(&obj->obj_exec_link);
Ben Widawsky2f633152013-07-17 12:19:03 -07004225 INIT_LIST_HEAD(&obj->vma_list);
Chris Wilson8d9d5742015-04-07 16:20:38 +01004226 INIT_LIST_HEAD(&obj->batch_pool_link);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004227
Chris Wilson37e680a2012-06-07 15:38:42 +01004228 obj->ops = ops;
4229
Chris Wilsond07f0e52016-10-28 13:58:44 +01004230 reservation_object_init(&obj->__builtin_resv);
4231 obj->resv = &obj->__builtin_resv;
4232
Chris Wilson50349242016-08-18 17:17:04 +01004233 obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004234 init_request_active(&obj->frontbuffer_write, frontbuffer_retire);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004235
4236 obj->mm.madv = I915_MADV_WILLNEED;
4237 INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
4238 mutex_init(&obj->mm.get_page.lock);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004239
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004240 i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004241}
4242
Chris Wilson37e680a2012-06-07 15:38:42 +01004243static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
Tvrtko Ursulin3599a912016-11-01 14:44:10 +00004244 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
4245 I915_GEM_OBJECT_IS_SHRINKABLE,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004246
Chris Wilson37e680a2012-06-07 15:38:42 +01004247 .get_pages = i915_gem_object_get_pages_gtt,
4248 .put_pages = i915_gem_object_put_pages_gtt,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004249
4250 .pwrite = i915_gem_object_pwrite_gtt,
Chris Wilson37e680a2012-06-07 15:38:42 +01004251};
4252
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004253struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004254i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004255{
Daniel Vetterc397b902010-04-09 19:05:07 +00004256 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004257 struct address_space *mapping;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004258 gfp_t mask;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004259 int ret;
Daniel Vetterc397b902010-04-09 19:05:07 +00004260
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004261 /* There is a prevalence of the assumption that we fit the object's
4262 * page count inside a 32bit _signed_ variable. Let's document this and
4263 * catch if we ever need to fix it. In the meantime, if you do spot
4264 * such a local variable, please consider fixing!
4265 */
Tvrtko Ursulin7a3ee5d2017-03-30 17:31:30 +01004266 if (size >> PAGE_SHIFT > INT_MAX)
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004267 return ERR_PTR(-E2BIG);
4268
4269 if (overflows_type(size, obj->base.size))
4270 return ERR_PTR(-E2BIG);
4271
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00004272 obj = i915_gem_object_alloc(dev_priv);
Daniel Vetterc397b902010-04-09 19:05:07 +00004273 if (obj == NULL)
Chris Wilsonfe3db792016-04-25 13:32:13 +01004274 return ERR_PTR(-ENOMEM);
Daniel Vetterc397b902010-04-09 19:05:07 +00004275
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004276 ret = drm_gem_object_init(&dev_priv->drm, &obj->base, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004277 if (ret)
4278 goto fail;
Daniel Vetterc397b902010-04-09 19:05:07 +00004279
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004280 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
Jani Nikulac0f86832016-12-07 12:13:04 +02004281 if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) {
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004282 /* 965gm cannot relocate objects above 4GiB. */
4283 mask &= ~__GFP_HIGHMEM;
4284 mask |= __GFP_DMA32;
4285 }
4286
Al Viro93c76a32015-12-04 23:45:44 -05004287 mapping = obj->base.filp->f_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004288 mapping_set_gfp_mask(mapping, mask);
Hugh Dickins5949eac2011-06-27 16:18:18 -07004289
Chris Wilson37e680a2012-06-07 15:38:42 +01004290 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004291
Daniel Vetterc397b902010-04-09 19:05:07 +00004292 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4293 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4294
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00004295 if (HAS_LLC(dev_priv)) {
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004296 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004297 * cache) for about a 10% performance improvement
4298 * compared to uncached. Graphics requests other than
4299 * display scanout are coherent with the CPU in
4300 * accessing this cache. This means in this mode we
4301 * don't need to clflush on the CPU side, and on the
4302 * GPU side we only need to flush internal caches to
4303 * get data visible to the CPU.
4304 *
4305 * However, we maintain the display planes as UC, and so
4306 * need to rebind when first used as such.
4307 */
4308 obj->cache_level = I915_CACHE_LLC;
4309 } else
4310 obj->cache_level = I915_CACHE_NONE;
4311
Daniel Vetterd861e332013-07-24 23:25:03 +02004312 trace_i915_gem_object_create(obj);
4313
Chris Wilson05394f32010-11-08 19:18:58 +00004314 return obj;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004315
4316fail:
4317 i915_gem_object_free(obj);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004318 return ERR_PTR(ret);
Daniel Vetterac52bc52010-04-09 19:05:06 +00004319}
4320
Chris Wilson340fbd82014-05-22 09:16:52 +01004321static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4322{
4323 /* If we are the last user of the backing storage (be it shmemfs
4324 * pages or stolen etc), we know that the pages are going to be
4325 * immediately released. In this case, we can then skip copying
4326 * back the contents from the GPU.
4327 */
4328
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004329 if (obj->mm.madv != I915_MADV_WILLNEED)
Chris Wilson340fbd82014-05-22 09:16:52 +01004330 return false;
4331
4332 if (obj->base.filp == NULL)
4333 return true;
4334
4335 /* At first glance, this looks racy, but then again so would be
4336 * userspace racing mmap against close. However, the first external
4337 * reference to the filp can only be obtained through the
4338 * i915_gem_mmap_ioctl() which safeguards us against the user
4339 * acquiring such a reference whilst we are in the middle of
4340 * freeing the object.
4341 */
4342 return atomic_long_read(&obj->base.filp->f_count) == 1;
4343}
4344
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004345static void __i915_gem_free_objects(struct drm_i915_private *i915,
4346 struct llist_node *freed)
Chris Wilsonbe726152010-07-23 23:18:50 +01004347{
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004348 struct drm_i915_gem_object *obj, *on;
Chris Wilsonbe726152010-07-23 23:18:50 +01004349
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004350 mutex_lock(&i915->drm.struct_mutex);
4351 intel_runtime_pm_get(i915);
4352 llist_for_each_entry(obj, freed, freed) {
4353 struct i915_vma *vma, *vn;
Paulo Zanonif65c9162013-11-27 18:20:34 -02004354
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004355 trace_i915_gem_object_destroy(obj);
4356
4357 GEM_BUG_ON(i915_gem_object_is_active(obj));
4358 list_for_each_entry_safe(vma, vn,
4359 &obj->vma_list, obj_link) {
4360 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
4361 GEM_BUG_ON(i915_vma_is_active(vma));
4362 vma->flags &= ~I915_VMA_PIN_MASK;
4363 i915_vma_close(vma);
4364 }
Chris Wilsondb6c2b42016-11-01 11:54:00 +00004365 GEM_BUG_ON(!list_empty(&obj->vma_list));
4366 GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004367
Joonas Lahtinen56cea322016-11-02 12:16:04 +02004368 list_del(&obj->global_link);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004369 }
4370 intel_runtime_pm_put(i915);
4371 mutex_unlock(&i915->drm.struct_mutex);
4372
Chris Wilsonf2be9d62017-04-07 11:25:52 +01004373 cond_resched();
4374
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004375 llist_for_each_entry_safe(obj, on, freed, freed) {
4376 GEM_BUG_ON(obj->bind_count);
4377 GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits));
4378
4379 if (obj->ops->release)
4380 obj->ops->release(obj);
4381
4382 if (WARN_ON(i915_gem_object_has_pinned_pages(obj)))
4383 atomic_set(&obj->mm.pages_pin_count, 0);
Chris Wilson548625e2016-11-01 12:11:34 +00004384 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004385 GEM_BUG_ON(obj->mm.pages);
4386
4387 if (obj->base.import_attach)
4388 drm_prime_gem_destroy(&obj->base, NULL);
4389
Chris Wilsond07f0e52016-10-28 13:58:44 +01004390 reservation_object_fini(&obj->__builtin_resv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004391 drm_gem_object_release(&obj->base);
4392 i915_gem_info_remove_obj(i915, obj->base.size);
4393
4394 kfree(obj->bit_17);
4395 i915_gem_object_free(obj);
4396 }
4397}
4398
4399static void i915_gem_flush_free_objects(struct drm_i915_private *i915)
4400{
4401 struct llist_node *freed;
4402
4403 freed = llist_del_all(&i915->mm.free_list);
4404 if (unlikely(freed))
4405 __i915_gem_free_objects(i915, freed);
4406}
4407
4408static void __i915_gem_free_work(struct work_struct *work)
4409{
4410 struct drm_i915_private *i915 =
4411 container_of(work, struct drm_i915_private, mm.free_work);
4412 struct llist_node *freed;
Chris Wilson26e12f82011-03-20 11:20:19 +00004413
Chris Wilsonb1f788c2016-08-04 07:52:45 +01004414 /* All file-owned VMA should have been released by this point through
4415 * i915_gem_close_object(), or earlier by i915_gem_context_close().
4416 * However, the object may also be bound into the global GTT (e.g.
4417 * older GPUs without per-process support, or for direct access through
4418 * the GTT either for the user or for scanout). Those VMA still need to
4419 * unbound now.
4420 */
Chris Wilson1488fc02012-04-24 15:47:31 +01004421
Chris Wilson5ad08be2017-04-07 11:25:51 +01004422 while ((freed = llist_del_all(&i915->mm.free_list))) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004423 __i915_gem_free_objects(i915, freed);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004424 if (need_resched())
4425 break;
4426 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004427}
4428
4429static void __i915_gem_free_object_rcu(struct rcu_head *head)
4430{
4431 struct drm_i915_gem_object *obj =
4432 container_of(head, typeof(*obj), rcu);
4433 struct drm_i915_private *i915 = to_i915(obj->base.dev);
4434
4435 /* We can't simply use call_rcu() from i915_gem_free_object()
4436 * as we need to block whilst unbinding, and the call_rcu
4437 * task may be called from softirq context. So we take a
4438 * detour through a worker.
4439 */
4440 if (llist_add(&obj->freed, &i915->mm.free_list))
4441 schedule_work(&i915->mm.free_work);
4442}
4443
4444void i915_gem_free_object(struct drm_gem_object *gem_obj)
4445{
4446 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
4447
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004448 if (obj->mm.quirked)
4449 __i915_gem_object_unpin_pages(obj);
4450
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004451 if (discard_backing_storage(obj))
4452 obj->mm.madv = I915_MADV_DONTNEED;
Daniel Vettera071fa02014-06-18 23:28:09 +02004453
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004454 /* Before we free the object, make sure any pure RCU-only
4455 * read-side critical sections are complete, e.g.
4456 * i915_gem_busy_ioctl(). For the corresponding synchronized
4457 * lookup see i915_gem_object_lookup_rcu().
4458 */
4459 call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
Chris Wilsonbe726152010-07-23 23:18:50 +01004460}
4461
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004462void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj)
4463{
4464 lockdep_assert_held(&obj->base.dev->struct_mutex);
4465
4466 GEM_BUG_ON(i915_gem_object_has_active_reference(obj));
4467 if (i915_gem_object_is_active(obj))
4468 i915_gem_object_set_active_reference(obj);
4469 else
4470 i915_gem_object_put(obj);
4471}
4472
Chris Wilson3033aca2016-10-28 13:58:47 +01004473static void assert_kernel_context_is_current(struct drm_i915_private *dev_priv)
4474{
4475 struct intel_engine_cs *engine;
4476 enum intel_engine_id id;
4477
4478 for_each_engine(engine, dev_priv, id)
Chris Wilsonf131e352016-12-29 14:40:37 +00004479 GEM_BUG_ON(engine->last_retired_context &&
4480 !i915_gem_context_is_kernel(engine->last_retired_context));
Chris Wilson3033aca2016-10-28 13:58:47 +01004481}
4482
Chris Wilson24145512017-01-24 11:01:35 +00004483void i915_gem_sanitize(struct drm_i915_private *i915)
4484{
4485 /*
4486 * If we inherit context state from the BIOS or earlier occupants
4487 * of the GPU, the GPU may be in an inconsistent state when we
4488 * try to take over. The only way to remove the earlier state
4489 * is by resetting. However, resetting on earlier gen is tricky as
4490 * it may impact the display and we are uncertain about the stability
Joonas Lahtinenea117b82017-04-28 10:53:38 +03004491 * of the reset, so this could be applied to even earlier gen.
Chris Wilson24145512017-01-24 11:01:35 +00004492 */
Joonas Lahtinenea117b82017-04-28 10:53:38 +03004493 if (INTEL_GEN(i915) >= 5) {
Chris Wilson24145512017-01-24 11:01:35 +00004494 int reset = intel_gpu_reset(i915, ALL_ENGINES);
4495 WARN_ON(reset && reset != -ENODEV);
4496 }
4497}
4498
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004499int i915_gem_suspend(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07004500{
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004501 struct drm_device *dev = &dev_priv->drm;
Chris Wilsondcff85c2016-08-05 10:14:11 +01004502 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004503
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004504 intel_runtime_pm_get(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01004505 intel_suspend_gt_powersave(dev_priv);
4506
Chris Wilson45c5f202013-10-16 11:50:01 +01004507 mutex_lock(&dev->struct_mutex);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004508
4509 /* We have to flush all the executing contexts to main memory so
4510 * that they can saved in the hibernation image. To ensure the last
4511 * context image is coherent, we have to switch away from it. That
4512 * leaves the dev_priv->kernel_context still active when
4513 * we actually suspend, and its image in memory may not match the GPU
4514 * state. Fortunately, the kernel_context is disposable and we do
4515 * not rely on its state.
4516 */
4517 ret = i915_gem_switch_to_kernel_context(dev_priv);
4518 if (ret)
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004519 goto err_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01004520
Chris Wilson22dd3bb2016-09-09 14:11:50 +01004521 ret = i915_gem_wait_for_idle(dev_priv,
4522 I915_WAIT_INTERRUPTIBLE |
4523 I915_WAIT_LOCKED);
Chris Wilsonf7403342013-09-13 23:57:04 +01004524 if (ret)
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004525 goto err_unlock;
Chris Wilsonf7403342013-09-13 23:57:04 +01004526
Chris Wilson3033aca2016-10-28 13:58:47 +01004527 assert_kernel_context_is_current(dev_priv);
Chris Wilsonb2e862d2016-04-28 09:56:41 +01004528 i915_gem_context_lost(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01004529 mutex_unlock(&dev->struct_mutex);
4530
Sagar Arun Kamblefd089232017-04-05 15:51:50 +05304531 intel_guc_suspend(dev_priv);
4532
Chris Wilson737b1502015-01-26 18:03:03 +02004533 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
Chris Wilson67d97da2016-07-04 08:08:31 +01004534 cancel_delayed_work_sync(&dev_priv->gt.retire_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004535
4536 /* As the idle_work is rearming if it detects a race, play safe and
4537 * repeat the flush until it is definitely idle.
4538 */
4539 while (flush_delayed_work(&dev_priv->gt.idle_work))
4540 ;
4541
4542 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson29105cc2010-01-07 10:39:13 +00004543
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004544 /* Assert that we sucessfully flushed all the work and
4545 * reset the GPU back to its idle, low power state.
4546 */
Chris Wilson67d97da2016-07-04 08:08:31 +01004547 WARN_ON(dev_priv->gt.awake);
Chris Wilson05425242017-03-03 12:19:47 +00004548 WARN_ON(!intel_engines_are_idle(dev_priv));
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004549
Imre Deak1c777c52016-10-12 17:46:37 +03004550 /*
4551 * Neither the BIOS, ourselves or any other kernel
4552 * expects the system to be in execlists mode on startup,
4553 * so we need to reset the GPU back to legacy mode. And the only
4554 * known way to disable logical contexts is through a GPU reset.
4555 *
4556 * So in order to leave the system in a known default configuration,
4557 * always reset the GPU upon unload and suspend. Afterwards we then
4558 * clean up the GEM state tracking, flushing off the requests and
4559 * leaving the system in a known idle state.
4560 *
4561 * Note that is of the upmost importance that the GPU is idle and
4562 * all stray writes are flushed *before* we dismantle the backing
4563 * storage for the pinned objects.
4564 *
4565 * However, since we are uncertain that resetting the GPU on older
4566 * machines is a good idea, we don't - just in case it leaves the
4567 * machine in an unusable condition.
4568 */
Chris Wilson24145512017-01-24 11:01:35 +00004569 i915_gem_sanitize(dev_priv);
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004570 goto out_rpm_put;
Imre Deak1c777c52016-10-12 17:46:37 +03004571
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004572err_unlock:
Chris Wilson45c5f202013-10-16 11:50:01 +01004573 mutex_unlock(&dev->struct_mutex);
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004574out_rpm_put:
4575 intel_runtime_pm_put(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01004576 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004577}
4578
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004579void i915_gem_resume(struct drm_i915_private *dev_priv)
Chris Wilson5ab57c72016-07-15 14:56:20 +01004580{
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004581 struct drm_device *dev = &dev_priv->drm;
Chris Wilson5ab57c72016-07-15 14:56:20 +01004582
Imre Deak31ab49a2016-11-07 11:20:05 +02004583 WARN_ON(dev_priv->gt.awake);
4584
Chris Wilson5ab57c72016-07-15 14:56:20 +01004585 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00004586 i915_gem_restore_gtt_mappings(dev_priv);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004587
4588 /* As we didn't flush the kernel context before suspend, we cannot
4589 * guarantee that the context image is complete. So let's just reset
4590 * it and start again.
4591 */
Chris Wilson821ed7d2016-09-09 14:11:53 +01004592 dev_priv->gt.resume(dev_priv);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004593
4594 mutex_unlock(&dev->struct_mutex);
4595}
4596
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004597void i915_gem_init_swizzling(struct drm_i915_private *dev_priv)
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004598{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004599 if (INTEL_GEN(dev_priv) < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004600 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
4601 return;
4602
4603 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
4604 DISP_TILE_SURFACE_SWIZZLING);
4605
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004606 if (IS_GEN5(dev_priv))
Daniel Vetter11782b02012-01-31 16:47:55 +01004607 return;
4608
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004609 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004610 if (IS_GEN6(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004611 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004612 else if (IS_GEN7(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004613 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004614 else if (IS_GEN8(dev_priv))
Ben Widawsky31a53362013-11-02 21:07:04 -07004615 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08004616 else
4617 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004618}
Daniel Vettere21af882012-02-09 20:53:27 +01004619
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004620static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004621{
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004622 I915_WRITE(RING_CTL(base), 0);
4623 I915_WRITE(RING_HEAD(base), 0);
4624 I915_WRITE(RING_TAIL(base), 0);
4625 I915_WRITE(RING_START(base), 0);
4626}
4627
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004628static void init_unused_rings(struct drm_i915_private *dev_priv)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004629{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004630 if (IS_I830(dev_priv)) {
4631 init_unused_ring(dev_priv, PRB1_BASE);
4632 init_unused_ring(dev_priv, SRB0_BASE);
4633 init_unused_ring(dev_priv, SRB1_BASE);
4634 init_unused_ring(dev_priv, SRB2_BASE);
4635 init_unused_ring(dev_priv, SRB3_BASE);
4636 } else if (IS_GEN2(dev_priv)) {
4637 init_unused_ring(dev_priv, SRB0_BASE);
4638 init_unused_ring(dev_priv, SRB1_BASE);
4639 } else if (IS_GEN3(dev_priv)) {
4640 init_unused_ring(dev_priv, PRB1_BASE);
4641 init_unused_ring(dev_priv, PRB2_BASE);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004642 }
4643}
4644
Chris Wilson20a8a742017-02-08 14:30:31 +00004645static int __i915_gem_restart_engines(void *data)
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004646{
Chris Wilson20a8a742017-02-08 14:30:31 +00004647 struct drm_i915_private *i915 = data;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004648 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05304649 enum intel_engine_id id;
Chris Wilson20a8a742017-02-08 14:30:31 +00004650 int err;
4651
4652 for_each_engine(engine, i915, id) {
4653 err = engine->init_hw(engine);
4654 if (err)
4655 return err;
4656 }
4657
4658 return 0;
4659}
4660
4661int i915_gem_init_hw(struct drm_i915_private *dev_priv)
4662{
Chris Wilsond200cda2016-04-28 09:56:44 +01004663 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004664
Chris Wilsonde867c22016-10-25 13:16:02 +01004665 dev_priv->gt.last_init_time = ktime_get();
4666
Chris Wilson5e4f5182015-02-13 14:35:59 +00004667 /* Double layer security blanket, see i915_gem_init() */
4668 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4669
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00004670 if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07004671 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004672
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01004673 if (IS_HASWELL(dev_priv))
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004674 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
Ville Syrjälä0bf21342013-11-29 14:56:12 +02004675 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03004676
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004677 if (HAS_PCH_NOP(dev_priv)) {
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01004678 if (IS_IVYBRIDGE(dev_priv)) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01004679 u32 temp = I915_READ(GEN7_MSG_CTL);
4680 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
4681 I915_WRITE(GEN7_MSG_CTL, temp);
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004682 } else if (INTEL_GEN(dev_priv) >= 7) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01004683 u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
4684 temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
4685 I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
4686 }
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07004687 }
4688
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004689 i915_gem_init_swizzling(dev_priv);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004690
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004691 /*
4692 * At least 830 can leave some of the unused rings
4693 * "active" (ie. head != tail) after resume which
4694 * will prevent c3 entry. Makes sure all unused rings
4695 * are totally idle.
4696 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004697 init_unused_rings(dev_priv);
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004698
Dave Gordoned54c1a2016-01-19 19:02:54 +00004699 BUG_ON(!dev_priv->kernel_context);
John Harrison90638cc2015-05-29 17:43:37 +01004700
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004701 ret = i915_ppgtt_init_hw(dev_priv);
John Harrison4ad2fd82015-06-18 13:11:20 +01004702 if (ret) {
4703 DRM_ERROR("PPGTT enable HW failed %d\n", ret);
4704 goto out;
4705 }
4706
4707 /* Need to do basic initialisation of all rings first: */
Chris Wilson20a8a742017-02-08 14:30:31 +00004708 ret = __i915_gem_restart_engines(dev_priv);
4709 if (ret)
4710 goto out;
Mika Kuoppala99433932013-01-22 14:12:17 +02004711
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004712 intel_mocs_init_l3cc_table(dev_priv);
Peter Antoine0ccdacf2016-04-13 15:03:25 +01004713
Oscar Mateob8991402017-03-28 09:53:47 -07004714 /* We can't enable contexts until all firmware is loaded */
4715 ret = intel_uc_init_hw(dev_priv);
4716 if (ret)
4717 goto out;
Alex Dai33a732f2015-08-12 15:43:36 +01004718
Chris Wilson5e4f5182015-02-13 14:35:59 +00004719out:
4720 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004721 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004722}
4723
Chris Wilson39df9192016-07-20 13:31:57 +01004724bool intel_sanitize_semaphores(struct drm_i915_private *dev_priv, int value)
4725{
4726 if (INTEL_INFO(dev_priv)->gen < 6)
4727 return false;
4728
4729 /* TODO: make semaphores and Execlists play nicely together */
4730 if (i915.enable_execlists)
4731 return false;
4732
4733 if (value >= 0)
4734 return value;
4735
4736#ifdef CONFIG_INTEL_IOMMU
4737 /* Enable semaphores on SNB when IO remapping is off */
4738 if (INTEL_INFO(dev_priv)->gen == 6 && intel_iommu_gfx_mapped)
4739 return false;
4740#endif
4741
4742 return true;
4743}
4744
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004745int i915_gem_init(struct drm_i915_private *dev_priv)
Chris Wilson1070a422012-04-24 15:47:41 +01004746{
Chris Wilson1070a422012-04-24 15:47:41 +01004747 int ret;
4748
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004749 mutex_lock(&dev_priv->drm.struct_mutex);
Jesse Barnesd62b4892013-03-08 10:45:53 -08004750
Chris Wilson94312822017-05-03 10:39:18 +01004751 dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1);
Chris Wilson57822dc2017-02-22 11:40:48 +00004752
Oscar Mateoa83014d2014-07-24 17:04:21 +01004753 if (!i915.enable_execlists) {
Chris Wilson821ed7d2016-09-09 14:11:53 +01004754 dev_priv->gt.resume = intel_legacy_submission_resume;
Chris Wilson7e37f882016-08-02 22:50:21 +01004755 dev_priv->gt.cleanup_engine = intel_engine_cleanup;
Oscar Mateo454afeb2014-07-24 17:04:22 +01004756 } else {
Chris Wilson821ed7d2016-09-09 14:11:53 +01004757 dev_priv->gt.resume = intel_lr_context_resume;
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004758 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
Oscar Mateoa83014d2014-07-24 17:04:21 +01004759 }
4760
Chris Wilson5e4f5182015-02-13 14:35:59 +00004761 /* This is just a security blanket to placate dragons.
4762 * On some systems, we very sporadically observe that the first TLBs
4763 * used by the CS may be stale, despite us poking the TLB reset. If
4764 * we hold the forcewake during initialisation these problems
4765 * just magically go away.
4766 */
4767 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4768
Chris Wilson72778cb2016-05-19 16:17:16 +01004769 i915_gem_init_userptr(dev_priv);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01004770
4771 ret = i915_gem_init_ggtt(dev_priv);
4772 if (ret)
4773 goto out_unlock;
Jesse Barnesd62b4892013-03-08 10:45:53 -08004774
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004775 ret = i915_gem_context_init(dev_priv);
Jani Nikula7bcc3772014-12-05 14:17:42 +02004776 if (ret)
4777 goto out_unlock;
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004778
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004779 ret = intel_engines_init(dev_priv);
Daniel Vetter35a57ff2014-11-20 00:33:07 +01004780 if (ret)
Jani Nikula7bcc3772014-12-05 14:17:42 +02004781 goto out_unlock;
Daniel Vetter53ca26c2012-04-26 23:28:03 +02004782
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004783 ret = i915_gem_init_hw(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01004784 if (ret == -EIO) {
Chris Wilson7e21d642016-07-27 09:07:29 +01004785 /* Allow engine initialisation to fail by marking the GPU as
Chris Wilson60990322014-04-09 09:19:42 +01004786 * wedged. But we only want to do this where the GPU is angry,
4787 * for all other failure, such as an allocation failure, bail.
4788 */
4789 DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
Chris Wilson821ed7d2016-09-09 14:11:53 +01004790 i915_gem_set_wedged(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01004791 ret = 0;
Chris Wilson1070a422012-04-24 15:47:41 +01004792 }
Jani Nikula7bcc3772014-12-05 14:17:42 +02004793
4794out_unlock:
Chris Wilson5e4f5182015-02-13 14:35:59 +00004795 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004796 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson1070a422012-04-24 15:47:41 +01004797
Chris Wilson60990322014-04-09 09:19:42 +01004798 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01004799}
4800
Chris Wilson24145512017-01-24 11:01:35 +00004801void i915_gem_init_mmio(struct drm_i915_private *i915)
4802{
4803 i915_gem_sanitize(i915);
4804}
4805
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004806void
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00004807i915_gem_cleanup_engines(struct drm_i915_private *dev_priv)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004808{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004809 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05304810 enum intel_engine_id id;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004811
Akash Goel3b3f1652016-10-13 22:44:48 +05304812 for_each_engine(engine, dev_priv, id)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004813 dev_priv->gt.cleanup_engine(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004814}
4815
Eric Anholt673a3942008-07-30 12:06:12 -07004816void
Imre Deak40ae4e12016-03-16 14:54:03 +02004817i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
4818{
Chris Wilson49ef5292016-08-18 17:17:00 +01004819 int i;
Imre Deak40ae4e12016-03-16 14:54:03 +02004820
4821 if (INTEL_INFO(dev_priv)->gen >= 7 && !IS_VALLEYVIEW(dev_priv) &&
4822 !IS_CHERRYVIEW(dev_priv))
4823 dev_priv->num_fence_regs = 32;
Jani Nikula73f67aa2016-12-07 22:48:09 +02004824 else if (INTEL_INFO(dev_priv)->gen >= 4 ||
4825 IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
4826 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02004827 dev_priv->num_fence_regs = 16;
4828 else
4829 dev_priv->num_fence_regs = 8;
4830
Chris Wilsonc0336662016-05-06 15:40:21 +01004831 if (intel_vgpu_active(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02004832 dev_priv->num_fence_regs =
4833 I915_READ(vgtif_reg(avail_rs.fence_num));
4834
4835 /* Initialize fence registers to zero */
Chris Wilson49ef5292016-08-18 17:17:00 +01004836 for (i = 0; i < dev_priv->num_fence_regs; i++) {
4837 struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
4838
4839 fence->i915 = dev_priv;
4840 fence->id = i;
4841 list_add_tail(&fence->link, &dev_priv->mm.fence_list);
4842 }
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00004843 i915_gem_restore_fences(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02004844
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00004845 i915_gem_detect_bit_6_swizzle(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02004846}
4847
Chris Wilson73cb9702016-10-28 13:58:46 +01004848int
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00004849i915_gem_load_init(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07004850{
Tvrtko Ursulina9335682016-11-02 15:14:59 +00004851 int err = -ENOMEM;
Chris Wilson42dcedd2012-11-15 11:32:30 +00004852
Tvrtko Ursulina9335682016-11-02 15:14:59 +00004853 dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
4854 if (!dev_priv->objects)
Chris Wilson73cb9702016-10-28 13:58:46 +01004855 goto err_out;
Chris Wilson73cb9702016-10-28 13:58:46 +01004856
Tvrtko Ursulina9335682016-11-02 15:14:59 +00004857 dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
4858 if (!dev_priv->vmas)
Chris Wilson73cb9702016-10-28 13:58:46 +01004859 goto err_objects;
Chris Wilson73cb9702016-10-28 13:58:46 +01004860
Tvrtko Ursulina9335682016-11-02 15:14:59 +00004861 dev_priv->requests = KMEM_CACHE(drm_i915_gem_request,
4862 SLAB_HWCACHE_ALIGN |
4863 SLAB_RECLAIM_ACCOUNT |
4864 SLAB_DESTROY_BY_RCU);
4865 if (!dev_priv->requests)
Chris Wilson73cb9702016-10-28 13:58:46 +01004866 goto err_vmas;
Chris Wilson73cb9702016-10-28 13:58:46 +01004867
Chris Wilson52e54202016-11-14 20:41:02 +00004868 dev_priv->dependencies = KMEM_CACHE(i915_dependency,
4869 SLAB_HWCACHE_ALIGN |
4870 SLAB_RECLAIM_ACCOUNT);
4871 if (!dev_priv->dependencies)
4872 goto err_requests;
4873
Chris Wilson73cb9702016-10-28 13:58:46 +01004874 mutex_lock(&dev_priv->drm.struct_mutex);
4875 INIT_LIST_HEAD(&dev_priv->gt.timelines);
Chris Wilsonbb894852016-11-14 20:40:57 +00004876 err = i915_gem_timeline_init__global(dev_priv);
Chris Wilson73cb9702016-10-28 13:58:46 +01004877 mutex_unlock(&dev_priv->drm.struct_mutex);
4878 if (err)
Chris Wilson52e54202016-11-14 20:41:02 +00004879 goto err_dependencies;
Eric Anholt673a3942008-07-30 12:06:12 -07004880
Ben Widawskya33afea2013-09-17 21:12:45 -07004881 INIT_LIST_HEAD(&dev_priv->context_list);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004882 INIT_WORK(&dev_priv->mm.free_work, __i915_gem_free_work);
4883 init_llist_head(&dev_priv->mm.free_list);
Chris Wilson6c085a72012-08-20 11:40:46 +02004884 INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
4885 INIT_LIST_HEAD(&dev_priv->mm.bound_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004886 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilson275f0392016-10-24 13:42:14 +01004887 INIT_LIST_HEAD(&dev_priv->mm.userfault_list);
Chris Wilson67d97da2016-07-04 08:08:31 +01004888 INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
Eric Anholt673a3942008-07-30 12:06:12 -07004889 i915_gem_retire_work_handler);
Chris Wilson67d97da2016-07-04 08:08:31 +01004890 INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004891 i915_gem_idle_work_handler);
Chris Wilson1f15b762016-07-01 17:23:14 +01004892 init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01004893 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson31169712009-09-14 16:50:28 +01004894
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004895 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01004896
Joonas Lahtinen6f633402016-09-01 14:58:21 +03004897 atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
4898
Chris Wilsonb5add952016-08-04 16:32:36 +01004899 spin_lock_init(&dev_priv->fb_tracking.lock);
Chris Wilson73cb9702016-10-28 13:58:46 +01004900
4901 return 0;
4902
Chris Wilson52e54202016-11-14 20:41:02 +00004903err_dependencies:
4904 kmem_cache_destroy(dev_priv->dependencies);
Chris Wilson73cb9702016-10-28 13:58:46 +01004905err_requests:
4906 kmem_cache_destroy(dev_priv->requests);
4907err_vmas:
4908 kmem_cache_destroy(dev_priv->vmas);
4909err_objects:
4910 kmem_cache_destroy(dev_priv->objects);
4911err_out:
4912 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07004913}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004914
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00004915void i915_gem_load_cleanup(struct drm_i915_private *dev_priv)
Imre Deakd64aa092016-01-19 15:26:29 +02004916{
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00004917 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson7d5d59e2016-11-01 08:48:41 +00004918 WARN_ON(!llist_empty(&dev_priv->mm.free_list));
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00004919 WARN_ON(dev_priv->mm.object_count);
Chris Wilson7d5d59e2016-11-01 08:48:41 +00004920
Matthew Auldea84aa72016-11-17 21:04:11 +00004921 mutex_lock(&dev_priv->drm.struct_mutex);
4922 i915_gem_timeline_fini(&dev_priv->gt.global_timeline);
4923 WARN_ON(!list_empty(&dev_priv->gt.timelines));
4924 mutex_unlock(&dev_priv->drm.struct_mutex);
4925
Chris Wilson52e54202016-11-14 20:41:02 +00004926 kmem_cache_destroy(dev_priv->dependencies);
Imre Deakd64aa092016-01-19 15:26:29 +02004927 kmem_cache_destroy(dev_priv->requests);
4928 kmem_cache_destroy(dev_priv->vmas);
4929 kmem_cache_destroy(dev_priv->objects);
Chris Wilson0eafec62016-08-04 16:32:41 +01004930
4931 /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
4932 rcu_barrier();
Imre Deakd64aa092016-01-19 15:26:29 +02004933}
4934
Chris Wilson6a800ea2016-09-21 14:51:07 +01004935int i915_gem_freeze(struct drm_i915_private *dev_priv)
4936{
Chris Wilsond0aa3012017-04-07 11:25:49 +01004937 /* Discard all purgeable objects, let userspace recover those as
4938 * required after resuming.
4939 */
Chris Wilson6a800ea2016-09-21 14:51:07 +01004940 i915_gem_shrink_all(dev_priv);
Chris Wilson6a800ea2016-09-21 14:51:07 +01004941
Chris Wilson6a800ea2016-09-21 14:51:07 +01004942 return 0;
4943}
4944
Chris Wilson461fb992016-05-14 07:26:33 +01004945int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
4946{
4947 struct drm_i915_gem_object *obj;
Chris Wilson7aab2d52016-09-09 20:02:18 +01004948 struct list_head *phases[] = {
4949 &dev_priv->mm.unbound_list,
4950 &dev_priv->mm.bound_list,
4951 NULL
4952 }, **p;
Chris Wilson461fb992016-05-14 07:26:33 +01004953
4954 /* Called just before we write the hibernation image.
4955 *
4956 * We need to update the domain tracking to reflect that the CPU
4957 * will be accessing all the pages to create and restore from the
4958 * hibernation, and so upon restoration those pages will be in the
4959 * CPU domain.
4960 *
4961 * To make sure the hibernation image contains the latest state,
4962 * we update that state just before writing out the image.
Chris Wilson7aab2d52016-09-09 20:02:18 +01004963 *
4964 * To try and reduce the hibernation image, we manually shrink
Chris Wilsond0aa3012017-04-07 11:25:49 +01004965 * the objects as well, see i915_gem_freeze()
Chris Wilson461fb992016-05-14 07:26:33 +01004966 */
4967
Chris Wilson6a800ea2016-09-21 14:51:07 +01004968 i915_gem_shrink(dev_priv, -1UL, I915_SHRINK_UNBOUND);
Chris Wilson17b93c42017-04-07 11:25:50 +01004969 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson461fb992016-05-14 07:26:33 +01004970
Chris Wilsond0aa3012017-04-07 11:25:49 +01004971 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson7aab2d52016-09-09 20:02:18 +01004972 for (p = phases; *p; p++) {
Joonas Lahtinen56cea322016-11-02 12:16:04 +02004973 list_for_each_entry(obj, *p, global_link) {
Chris Wilson7aab2d52016-09-09 20:02:18 +01004974 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4975 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4976 }
Chris Wilson461fb992016-05-14 07:26:33 +01004977 }
Chris Wilson6a800ea2016-09-21 14:51:07 +01004978 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson461fb992016-05-14 07:26:33 +01004979
4980 return 0;
4981}
4982
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004983void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004984{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004985 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilson15f7bbc2016-07-26 12:01:52 +01004986 struct drm_i915_gem_request *request;
Eric Anholtb9624422009-06-03 07:27:35 +00004987
4988 /* Clean up our request list when the client is going away, so that
4989 * later retire_requests won't dereference our soon-to-be-gone
4990 * file_priv.
4991 */
Chris Wilson1c255952010-09-26 11:03:27 +01004992 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004993 list_for_each_entry(request, &file_priv->mm.request_list, client_link)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004994 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01004995 spin_unlock(&file_priv->mm.lock);
Chris Wilson31169712009-09-14 16:50:28 +01004996
Chris Wilson2e1b8732015-04-27 13:41:22 +01004997 if (!list_empty(&file_priv->rps.link)) {
Chris Wilson8d3afd72015-05-21 21:01:47 +01004998 spin_lock(&to_i915(dev)->rps.client_lock);
Chris Wilson2e1b8732015-04-27 13:41:22 +01004999 list_del(&file_priv->rps.link);
Chris Wilson8d3afd72015-05-21 21:01:47 +01005000 spin_unlock(&to_i915(dev)->rps.client_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01005001 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005002}
5003
5004int i915_gem_open(struct drm_device *dev, struct drm_file *file)
5005{
5006 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08005007 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005008
Chris Wilsonc4c29d72016-11-09 10:45:07 +00005009 DRM_DEBUG("\n");
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005010
5011 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
5012 if (!file_priv)
5013 return -ENOMEM;
5014
5015 file->driver_priv = file_priv;
Dave Gordonf19ec8c2016-07-04 11:34:37 +01005016 file_priv->dev_priv = to_i915(dev);
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02005017 file_priv->file = file;
Chris Wilson2e1b8732015-04-27 13:41:22 +01005018 INIT_LIST_HEAD(&file_priv->rps.link);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005019
5020 spin_lock_init(&file_priv->mm.lock);
5021 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005022
Chris Wilsonc80ff162016-07-27 09:07:27 +01005023 file_priv->bsd_engine = -1;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00005024
Ben Widawskye422b882013-12-06 14:10:58 -08005025 ret = i915_gem_context_open(dev, file);
5026 if (ret)
5027 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005028
Ben Widawskye422b882013-12-06 14:10:58 -08005029 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005030}
5031
Daniel Vetterb680c372014-09-19 18:27:27 +02005032/**
5033 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07005034 * @old: current GEM buffer for the frontbuffer slots
5035 * @new: new GEM buffer for the frontbuffer slots
5036 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02005037 *
5038 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
5039 * from @old and setting them in @new. Both @old and @new can be NULL.
5040 */
Daniel Vettera071fa02014-06-18 23:28:09 +02005041void i915_gem_track_fb(struct drm_i915_gem_object *old,
5042 struct drm_i915_gem_object *new,
5043 unsigned frontbuffer_bits)
5044{
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005045 /* Control of individual bits within the mask are guarded by
5046 * the owning plane->mutex, i.e. we can never see concurrent
5047 * manipulation of individual bits. But since the bitfield as a whole
5048 * is updated using RMW, we need to use atomics in order to update
5049 * the bits.
5050 */
5051 BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
5052 sizeof(atomic_t) * BITS_PER_BYTE);
5053
Daniel Vettera071fa02014-06-18 23:28:09 +02005054 if (old) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005055 WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
5056 atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005057 }
5058
5059 if (new) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005060 WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
5061 atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005062 }
5063}
5064
Dave Gordonea702992015-07-09 19:29:02 +01005065/* Allocate a new GEM object and fill it with the supplied data */
5066struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005067i915_gem_object_create_from_data(struct drm_i915_private *dev_priv,
Dave Gordonea702992015-07-09 19:29:02 +01005068 const void *data, size_t size)
5069{
5070 struct drm_i915_gem_object *obj;
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005071 struct file *file;
5072 size_t offset;
5073 int err;
Dave Gordonea702992015-07-09 19:29:02 +01005074
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005075 obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE));
Chris Wilsonfe3db792016-04-25 13:32:13 +01005076 if (IS_ERR(obj))
Dave Gordonea702992015-07-09 19:29:02 +01005077 return obj;
5078
Chris Wilsonce8ff092017-03-17 19:46:47 +00005079 GEM_BUG_ON(obj->base.write_domain != I915_GEM_DOMAIN_CPU);
Dave Gordonea702992015-07-09 19:29:02 +01005080
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005081 file = obj->base.filp;
5082 offset = 0;
5083 do {
5084 unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
5085 struct page *page;
5086 void *pgdata, *vaddr;
Dave Gordonea702992015-07-09 19:29:02 +01005087
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005088 err = pagecache_write_begin(file, file->f_mapping,
5089 offset, len, 0,
5090 &page, &pgdata);
5091 if (err < 0)
5092 goto fail;
Dave Gordonea702992015-07-09 19:29:02 +01005093
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005094 vaddr = kmap(page);
5095 memcpy(vaddr, data, len);
5096 kunmap(page);
5097
5098 err = pagecache_write_end(file, file->f_mapping,
5099 offset, len, len,
5100 page, pgdata);
5101 if (err < 0)
5102 goto fail;
5103
5104 size -= len;
5105 data += len;
5106 offset += len;
5107 } while (size);
Dave Gordonea702992015-07-09 19:29:02 +01005108
5109 return obj;
5110
5111fail:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01005112 i915_gem_object_put(obj);
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005113 return ERR_PTR(err);
Dave Gordonea702992015-07-09 19:29:02 +01005114}
Chris Wilson96d77632016-10-28 13:58:33 +01005115
5116struct scatterlist *
5117i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
5118 unsigned int n,
5119 unsigned int *offset)
5120{
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005121 struct i915_gem_object_page_iter *iter = &obj->mm.get_page;
Chris Wilson96d77632016-10-28 13:58:33 +01005122 struct scatterlist *sg;
5123 unsigned int idx, count;
5124
5125 might_sleep();
5126 GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005127 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
Chris Wilson96d77632016-10-28 13:58:33 +01005128
5129 /* As we iterate forward through the sg, we record each entry in a
5130 * radixtree for quick repeated (backwards) lookups. If we have seen
5131 * this index previously, we will have an entry for it.
5132 *
5133 * Initial lookup is O(N), but this is amortized to O(1) for
5134 * sequential page access (where each new request is consecutive
5135 * to the previous one). Repeated lookups are O(lg(obj->base.size)),
5136 * i.e. O(1) with a large constant!
5137 */
5138 if (n < READ_ONCE(iter->sg_idx))
5139 goto lookup;
5140
5141 mutex_lock(&iter->lock);
5142
5143 /* We prefer to reuse the last sg so that repeated lookup of this
5144 * (or the subsequent) sg are fast - comparing against the last
5145 * sg is faster than going through the radixtree.
5146 */
5147
5148 sg = iter->sg_pos;
5149 idx = iter->sg_idx;
5150 count = __sg_page_count(sg);
5151
5152 while (idx + count <= n) {
5153 unsigned long exception, i;
5154 int ret;
5155
5156 /* If we cannot allocate and insert this entry, or the
5157 * individual pages from this range, cancel updating the
5158 * sg_idx so that on this lookup we are forced to linearly
5159 * scan onwards, but on future lookups we will try the
5160 * insertion again (in which case we need to be careful of
5161 * the error return reporting that we have already inserted
5162 * this index).
5163 */
5164 ret = radix_tree_insert(&iter->radix, idx, sg);
5165 if (ret && ret != -EEXIST)
5166 goto scan;
5167
5168 exception =
5169 RADIX_TREE_EXCEPTIONAL_ENTRY |
5170 idx << RADIX_TREE_EXCEPTIONAL_SHIFT;
5171 for (i = 1; i < count; i++) {
5172 ret = radix_tree_insert(&iter->radix, idx + i,
5173 (void *)exception);
5174 if (ret && ret != -EEXIST)
5175 goto scan;
5176 }
5177
5178 idx += count;
5179 sg = ____sg_next(sg);
5180 count = __sg_page_count(sg);
5181 }
5182
5183scan:
5184 iter->sg_pos = sg;
5185 iter->sg_idx = idx;
5186
5187 mutex_unlock(&iter->lock);
5188
5189 if (unlikely(n < idx)) /* insertion completed by another thread */
5190 goto lookup;
5191
5192 /* In case we failed to insert the entry into the radixtree, we need
5193 * to look beyond the current sg.
5194 */
5195 while (idx + count <= n) {
5196 idx += count;
5197 sg = ____sg_next(sg);
5198 count = __sg_page_count(sg);
5199 }
5200
5201 *offset = n - idx;
5202 return sg;
5203
5204lookup:
5205 rcu_read_lock();
5206
5207 sg = radix_tree_lookup(&iter->radix, n);
5208 GEM_BUG_ON(!sg);
5209
5210 /* If this index is in the middle of multi-page sg entry,
5211 * the radixtree will contain an exceptional entry that points
5212 * to the start of that range. We will return the pointer to
5213 * the base page and the offset of this page within the
5214 * sg entry's range.
5215 */
5216 *offset = 0;
5217 if (unlikely(radix_tree_exception(sg))) {
5218 unsigned long base =
5219 (unsigned long)sg >> RADIX_TREE_EXCEPTIONAL_SHIFT;
5220
5221 sg = radix_tree_lookup(&iter->radix, base);
5222 GEM_BUG_ON(!sg);
5223
5224 *offset = n - base;
5225 }
5226
5227 rcu_read_unlock();
5228
5229 return sg;
5230}
5231
5232struct page *
5233i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
5234{
5235 struct scatterlist *sg;
5236 unsigned int offset;
5237
5238 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
5239
5240 sg = i915_gem_object_get_sg(obj, n, &offset);
5241 return nth_page(sg_page(sg), offset);
5242}
5243
5244/* Like i915_gem_object_get_page(), but mark the returned page dirty */
5245struct page *
5246i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
5247 unsigned int n)
5248{
5249 struct page *page;
5250
5251 page = i915_gem_object_get_page(obj, n);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005252 if (!obj->mm.dirty)
Chris Wilson96d77632016-10-28 13:58:33 +01005253 set_page_dirty(page);
5254
5255 return page;
5256}
5257
5258dma_addr_t
5259i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
5260 unsigned long n)
5261{
5262 struct scatterlist *sg;
5263 unsigned int offset;
5264
5265 sg = i915_gem_object_get_sg(obj, n, &offset);
5266 return sg_dma_address(sg) + (offset << PAGE_SHIFT);
5267}
Chris Wilson935a2f72017-02-13 17:15:13 +00005268
5269#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
5270#include "selftests/scatterlist.c"
Chris Wilson66d9cb52017-02-13 17:15:17 +00005271#include "selftests/mock_gem_device.c"
Chris Wilson44653982017-02-13 17:15:20 +00005272#include "selftests/huge_gem_object.c"
Chris Wilson8335fd62017-02-13 17:15:28 +00005273#include "selftests/i915_gem_object.c"
Chris Wilson17059452017-02-13 17:15:32 +00005274#include "selftests/i915_gem_coherency.c"
Chris Wilson935a2f72017-02-13 17:15:13 +00005275#endif