blob: d93032875f2865d5e890a04e54f321fd68aa64da [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"
Yu Zhangeb822892015-02-10 19:05:49 +080032#include "i915_vgpu.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010033#include "i915_trace.h"
Jesse Barnes652c3932009-08-17 13:31:43 -070034#include "intel_drv.h"
Chris Wilson5d723d72016-08-04 16:32:35 +010035#include "intel_frontbuffer.h"
Peter Antoine0ccdacf2016-04-13 15:03:25 +010036#include "intel_mocs.h"
Chris Wilson6b5e90f2016-11-14 20:41:05 +000037#include <linux/dma-fence-array.h>
Chris Wilsonfe3288b2017-02-12 17:20:01 +000038#include <linux/kthread.h>
Chris Wilsonc13d87e2016-07-20 09:21:15 +010039#include <linux/reservation.h>
Hugh Dickins5949eac2011-06-27 16:18:18 -070040#include <linux/shmem_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090041#include <linux/slab.h>
Chris Wilson20e49332016-11-22 14:41:21 +000042#include <linux/stop_machine.h>
Eric Anholt673a3942008-07-30 12:06:12 -070043#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080044#include <linux/pci.h>
Daniel Vetter1286ff72012-05-10 15:25:09 +020045#include <linux/dma-buf.h>
Eric Anholt673a3942008-07-30 12:06:12 -070046
Chris Wilsonfbbd37b2016-10-28 13:58:42 +010047static void i915_gem_flush_free_objects(struct drm_i915_private *i915);
Chris Wilson05394f32010-11-08 19:18:58 +000048static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
Daniel Vettere62b59e2015-01-21 14:53:48 +010049static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
Chris Wilson61050802012-04-17 15:31:31 +010050
Chris Wilsonc76ce032013-08-08 14:41:03 +010051static bool cpu_cache_is_coherent(struct drm_device *dev,
52 enum i915_cache_level level)
53{
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +000054 return HAS_LLC(to_i915(dev)) || level != I915_CACHE_NONE;
Chris Wilsonc76ce032013-08-08 14:41:03 +010055}
56
Chris Wilson2c225692013-08-09 12:26:45 +010057static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
58{
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +053059 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
60 return false;
61
Chris Wilson2c225692013-08-09 12:26:45 +010062 if (!cpu_cache_is_coherent(obj->base.dev, obj->cache_level))
63 return true;
64
65 return obj->pin_display;
66}
67
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053068static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +010069insert_mappable_node(struct i915_ggtt *ggtt,
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053070 struct drm_mm_node *node, u32 size)
71{
72 memset(node, 0, sizeof(*node));
Chris Wilson4e64e552017-02-02 21:04:38 +000073 return drm_mm_insert_node_in_range(&ggtt->base.mm, node,
74 size, 0, I915_COLOR_UNEVICTABLE,
75 0, ggtt->mappable_end,
76 DRM_MM_INSERT_LOW);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053077}
78
79static void
80remove_mappable_node(struct drm_mm_node *node)
81{
82 drm_mm_remove_node(node);
83}
84
Chris Wilson73aa8082010-09-30 11:46:12 +010085/* some bookkeeping */
86static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010087 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010088{
Daniel Vetterc20e8352013-07-24 22:40:23 +020089 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010090 dev_priv->mm.object_count++;
91 dev_priv->mm.object_memory += size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020092 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010093}
94
95static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010096 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010097{
Daniel Vetterc20e8352013-07-24 22:40:23 +020098 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010099 dev_priv->mm.object_count--;
100 dev_priv->mm.object_memory -= size;
Daniel Vetterc20e8352013-07-24 22:40:23 +0200101 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +0100102}
103
Chris Wilson21dd3732011-01-26 15:55:56 +0000104static int
Daniel Vetter33196de2012-11-14 17:14:05 +0100105i915_gem_wait_for_error(struct i915_gpu_error *error)
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100106{
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100107 int ret;
108
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100109 might_sleep();
110
Chris Wilsond98c52c2016-04-13 17:35:05 +0100111 if (!i915_reset_in_progress(error))
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100112 return 0;
113
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200114 /*
115 * Only wait 10 seconds for the gpu reset to complete to avoid hanging
116 * userspace. If it takes that long something really bad is going on and
117 * we should simply try to bail out and fail as gracefully as possible.
118 */
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100119 ret = wait_event_interruptible_timeout(error->reset_queue,
Chris Wilsond98c52c2016-04-13 17:35:05 +0100120 !i915_reset_in_progress(error),
Chris Wilsonb52992c2016-10-28 13:58:24 +0100121 I915_RESET_TIMEOUT);
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200122 if (ret == 0) {
123 DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
124 return -EIO;
125 } else if (ret < 0) {
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100126 return ret;
Chris Wilsond98c52c2016-04-13 17:35:05 +0100127 } else {
128 return 0;
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200129 }
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100130}
131
Chris Wilson54cf91d2010-11-25 18:00:26 +0000132int i915_mutex_lock_interruptible(struct drm_device *dev)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100133{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100134 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100135 int ret;
136
Daniel Vetter33196de2012-11-14 17:14:05 +0100137 ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100138 if (ret)
139 return ret;
140
141 ret = mutex_lock_interruptible(&dev->struct_mutex);
142 if (ret)
143 return ret;
144
Chris Wilson76c1dec2010-09-25 11:22:51 +0100145 return 0;
146}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100147
Eric Anholt673a3942008-07-30 12:06:12 -0700148int
Eric Anholt5a125c32008-10-22 21:40:13 -0700149i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000150 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700151{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300152 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen62106b42016-03-18 10:42:57 +0200153 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300154 struct drm_i915_gem_get_aperture *args = data;
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100155 struct i915_vma *vma;
Chris Wilson6299f992010-11-24 12:23:44 +0000156 size_t pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700157
Chris Wilson6299f992010-11-24 12:23:44 +0000158 pinned = 0;
Chris Wilson73aa8082010-09-30 11:46:12 +0100159 mutex_lock(&dev->struct_mutex);
Chris Wilson1c7f4bc2016-02-26 11:03:19 +0000160 list_for_each_entry(vma, &ggtt->base.active_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100161 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100162 pinned += vma->node.size;
Chris Wilson1c7f4bc2016-02-26 11:03:19 +0000163 list_for_each_entry(vma, &ggtt->base.inactive_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100164 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100165 pinned += vma->node.size;
Chris Wilson73aa8082010-09-30 11:46:12 +0100166 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700167
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300168 args->aper_size = ggtt->base.total;
Akshay Joshi0206e352011-08-16 15:34:10 -0400169 args->aper_available_size = args->aper_size - pinned;
Chris Wilson6299f992010-11-24 12:23:44 +0000170
Eric Anholt5a125c32008-10-22 21:40:13 -0700171 return 0;
172}
173
Chris Wilson03ac84f2016-10-28 13:58:36 +0100174static struct sg_table *
Chris Wilson6a2c4232014-11-04 04:51:40 -0800175i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
Chris Wilson00731152014-05-21 12:42:56 +0100176{
Al Viro93c76a32015-12-04 23:45:44 -0500177 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilsondbb43512016-12-07 13:34:11 +0000178 drm_dma_handle_t *phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800179 struct sg_table *st;
180 struct scatterlist *sg;
Chris Wilsondbb43512016-12-07 13:34:11 +0000181 char *vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800182 int i;
Chris Wilson00731152014-05-21 12:42:56 +0100183
Chris Wilson6a2c4232014-11-04 04:51:40 -0800184 if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
Chris Wilson03ac84f2016-10-28 13:58:36 +0100185 return ERR_PTR(-EINVAL);
Chris Wilson00731152014-05-21 12:42:56 +0100186
Chris Wilsondbb43512016-12-07 13:34:11 +0000187 /* Always aligning to the object size, allows a single allocation
188 * to handle all possible callers, and given typical object sizes,
189 * the alignment of the buddy allocation will naturally match.
190 */
191 phys = drm_pci_alloc(obj->base.dev,
192 obj->base.size,
193 roundup_pow_of_two(obj->base.size));
194 if (!phys)
195 return ERR_PTR(-ENOMEM);
196
197 vaddr = phys->vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800198 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
199 struct page *page;
200 char *src;
201
202 page = shmem_read_mapping_page(mapping, i);
Chris Wilsondbb43512016-12-07 13:34:11 +0000203 if (IS_ERR(page)) {
204 st = ERR_CAST(page);
205 goto err_phys;
206 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800207
208 src = kmap_atomic(page);
209 memcpy(vaddr, src, PAGE_SIZE);
210 drm_clflush_virt_range(vaddr, PAGE_SIZE);
211 kunmap_atomic(src);
212
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300213 put_page(page);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800214 vaddr += PAGE_SIZE;
215 }
216
Chris Wilsonc0336662016-05-06 15:40:21 +0100217 i915_gem_chipset_flush(to_i915(obj->base.dev));
Chris Wilson6a2c4232014-11-04 04:51:40 -0800218
219 st = kmalloc(sizeof(*st), GFP_KERNEL);
Chris Wilsondbb43512016-12-07 13:34:11 +0000220 if (!st) {
221 st = ERR_PTR(-ENOMEM);
222 goto err_phys;
223 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800224
225 if (sg_alloc_table(st, 1, GFP_KERNEL)) {
226 kfree(st);
Chris Wilsondbb43512016-12-07 13:34:11 +0000227 st = ERR_PTR(-ENOMEM);
228 goto err_phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800229 }
230
231 sg = st->sgl;
232 sg->offset = 0;
233 sg->length = obj->base.size;
234
Chris Wilsondbb43512016-12-07 13:34:11 +0000235 sg_dma_address(sg) = phys->busaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800236 sg_dma_len(sg) = obj->base.size;
237
Chris Wilsondbb43512016-12-07 13:34:11 +0000238 obj->phys_handle = phys;
239 return st;
240
241err_phys:
242 drm_pci_free(obj->base.dev, phys);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100243 return st;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800244}
245
246static void
Chris Wilson2b3c8312016-11-11 14:58:09 +0000247__i915_gem_object_release_shmem(struct drm_i915_gem_object *obj,
Chris Wilsone5facdf2016-12-23 14:57:57 +0000248 struct sg_table *pages,
249 bool needs_clflush)
Chris Wilson6a2c4232014-11-04 04:51:40 -0800250{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100251 GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800252
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100253 if (obj->mm.madv == I915_MADV_DONTNEED)
254 obj->mm.dirty = false;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800255
Chris Wilsone5facdf2016-12-23 14:57:57 +0000256 if (needs_clflush &&
257 (obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0 &&
Chris Wilson05c34832016-11-18 21:17:47 +0000258 !cpu_cache_is_coherent(obj->base.dev, obj->cache_level))
Chris Wilson2b3c8312016-11-11 14:58:09 +0000259 drm_clflush_sg(pages);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100260
261 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
262 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
263}
264
265static void
266i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj,
267 struct sg_table *pages)
268{
Chris Wilsone5facdf2016-12-23 14:57:57 +0000269 __i915_gem_object_release_shmem(obj, pages, false);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100270
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100271 if (obj->mm.dirty) {
Al Viro93c76a32015-12-04 23:45:44 -0500272 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800273 char *vaddr = obj->phys_handle->vaddr;
Chris Wilson00731152014-05-21 12:42:56 +0100274 int i;
275
276 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
Chris Wilson6a2c4232014-11-04 04:51:40 -0800277 struct page *page;
278 char *dst;
Chris Wilson00731152014-05-21 12:42:56 +0100279
Chris Wilson6a2c4232014-11-04 04:51:40 -0800280 page = shmem_read_mapping_page(mapping, i);
281 if (IS_ERR(page))
282 continue;
283
284 dst = kmap_atomic(page);
285 drm_clflush_virt_range(vaddr, PAGE_SIZE);
286 memcpy(dst, vaddr, PAGE_SIZE);
287 kunmap_atomic(dst);
288
289 set_page_dirty(page);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100290 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson00731152014-05-21 12:42:56 +0100291 mark_page_accessed(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300292 put_page(page);
Chris Wilson00731152014-05-21 12:42:56 +0100293 vaddr += PAGE_SIZE;
294 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100295 obj->mm.dirty = false;
Chris Wilson00731152014-05-21 12:42:56 +0100296 }
297
Chris Wilson03ac84f2016-10-28 13:58:36 +0100298 sg_free_table(pages);
299 kfree(pages);
Chris Wilsondbb43512016-12-07 13:34:11 +0000300
301 drm_pci_free(obj->base.dev, obj->phys_handle);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800302}
303
304static void
305i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
306{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100307 i915_gem_object_unpin_pages(obj);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800308}
309
310static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
311 .get_pages = i915_gem_object_get_pages_phys,
312 .put_pages = i915_gem_object_put_pages_phys,
313 .release = i915_gem_object_release_phys,
314};
315
Chris Wilson581ab1f2017-02-15 16:39:00 +0000316static const struct drm_i915_gem_object_ops i915_gem_object_ops;
317
Chris Wilson35a96112016-08-14 18:44:40 +0100318int i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Chris Wilsonaa653a62016-08-04 07:52:27 +0100319{
320 struct i915_vma *vma;
321 LIST_HEAD(still_in_list);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100322 int ret;
Chris Wilsonaa653a62016-08-04 07:52:27 +0100323
Chris Wilson02bef8f2016-08-14 18:44:41 +0100324 lockdep_assert_held(&obj->base.dev->struct_mutex);
325
326 /* Closed vma are removed from the obj->vma_list - but they may
327 * still have an active binding on the object. To remove those we
328 * must wait for all rendering to complete to the object (as unbinding
329 * must anyway), and retire the requests.
Chris Wilsonaa653a62016-08-04 07:52:27 +0100330 */
Chris Wilsone95433c2016-10-28 13:58:27 +0100331 ret = i915_gem_object_wait(obj,
332 I915_WAIT_INTERRUPTIBLE |
333 I915_WAIT_LOCKED |
334 I915_WAIT_ALL,
335 MAX_SCHEDULE_TIMEOUT,
336 NULL);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100337 if (ret)
338 return ret;
339
340 i915_gem_retire_requests(to_i915(obj->base.dev));
341
Chris Wilsonaa653a62016-08-04 07:52:27 +0100342 while ((vma = list_first_entry_or_null(&obj->vma_list,
343 struct i915_vma,
344 obj_link))) {
345 list_move_tail(&vma->obj_link, &still_in_list);
346 ret = i915_vma_unbind(vma);
347 if (ret)
348 break;
349 }
350 list_splice(&still_in_list, &obj->vma_list);
351
352 return ret;
353}
354
Chris Wilsone95433c2016-10-28 13:58:27 +0100355static long
356i915_gem_object_wait_fence(struct dma_fence *fence,
357 unsigned int flags,
358 long timeout,
359 struct intel_rps_client *rps)
360{
361 struct drm_i915_gem_request *rq;
362
363 BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1);
364
365 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
366 return timeout;
367
368 if (!dma_fence_is_i915(fence))
369 return dma_fence_wait_timeout(fence,
370 flags & I915_WAIT_INTERRUPTIBLE,
371 timeout);
372
373 rq = to_request(fence);
374 if (i915_gem_request_completed(rq))
375 goto out;
376
377 /* This client is about to stall waiting for the GPU. In many cases
378 * this is undesirable and limits the throughput of the system, as
379 * many clients cannot continue processing user input/output whilst
380 * blocked. RPS autotuning may take tens of milliseconds to respond
381 * to the GPU load and thus incurs additional latency for the client.
382 * We can circumvent that by promoting the GPU frequency to maximum
383 * before we wait. This makes the GPU throttle up much more quickly
384 * (good for benchmarks and user experience, e.g. window animations),
385 * but at a cost of spending more power processing the workload
386 * (bad for battery). Not all clients even want their results
387 * immediately and for them we should just let the GPU select its own
388 * frequency to maximise efficiency. To prevent a single client from
389 * forcing the clocks too high for the whole system, we only allow
390 * each client to waitboost once in a busy period.
391 */
392 if (rps) {
393 if (INTEL_GEN(rq->i915) >= 6)
394 gen6_rps_boost(rq->i915, rps, rq->emitted_jiffies);
395 else
396 rps = NULL;
397 }
398
399 timeout = i915_wait_request(rq, flags, timeout);
400
401out:
402 if (flags & I915_WAIT_LOCKED && i915_gem_request_completed(rq))
403 i915_gem_request_retire_upto(rq);
404
Chris Wilsoncb399ea2016-11-01 10:03:16 +0000405 if (rps && rq->global_seqno == intel_engine_last_submit(rq->engine)) {
Chris Wilsone95433c2016-10-28 13:58:27 +0100406 /* The GPU is now idle and this client has stalled.
407 * Since no other client has submitted a request in the
408 * meantime, assume that this client is the only one
409 * supplying work to the GPU but is unable to keep that
410 * work supplied because it is waiting. Since the GPU is
411 * then never kept fully busy, RPS autoclocking will
412 * keep the clocks relatively low, causing further delays.
413 * Compensate by giving the synchronous client credit for
414 * a waitboost next time.
415 */
416 spin_lock(&rq->i915->rps.client_lock);
417 list_del_init(&rps->link);
418 spin_unlock(&rq->i915->rps.client_lock);
419 }
420
421 return timeout;
422}
423
424static long
425i915_gem_object_wait_reservation(struct reservation_object *resv,
426 unsigned int flags,
427 long timeout,
428 struct intel_rps_client *rps)
429{
Chris Wilsone54ca972017-02-17 15:13:04 +0000430 unsigned int seq = __read_seqcount_begin(&resv->seq);
Chris Wilsone95433c2016-10-28 13:58:27 +0100431 struct dma_fence *excl;
Chris Wilsone54ca972017-02-17 15:13:04 +0000432 bool prune_fences = false;
Chris Wilsone95433c2016-10-28 13:58:27 +0100433
434 if (flags & I915_WAIT_ALL) {
435 struct dma_fence **shared;
436 unsigned int count, i;
437 int ret;
438
439 ret = reservation_object_get_fences_rcu(resv,
440 &excl, &count, &shared);
441 if (ret)
442 return ret;
443
444 for (i = 0; i < count; i++) {
445 timeout = i915_gem_object_wait_fence(shared[i],
446 flags, timeout,
447 rps);
Chris Wilsond892e932017-02-12 21:53:43 +0000448 if (timeout < 0)
Chris Wilsone95433c2016-10-28 13:58:27 +0100449 break;
450
451 dma_fence_put(shared[i]);
452 }
453
454 for (; i < count; i++)
455 dma_fence_put(shared[i]);
456 kfree(shared);
Chris Wilsone54ca972017-02-17 15:13:04 +0000457
458 prune_fences = count && timeout >= 0;
Chris Wilsone95433c2016-10-28 13:58:27 +0100459 } else {
460 excl = reservation_object_get_excl_rcu(resv);
461 }
462
Chris Wilsone54ca972017-02-17 15:13:04 +0000463 if (excl && timeout >= 0) {
Chris Wilsone95433c2016-10-28 13:58:27 +0100464 timeout = i915_gem_object_wait_fence(excl, flags, timeout, rps);
Chris Wilsone54ca972017-02-17 15:13:04 +0000465 prune_fences = timeout >= 0;
466 }
Chris Wilsone95433c2016-10-28 13:58:27 +0100467
468 dma_fence_put(excl);
469
Chris Wilsone54ca972017-02-17 15:13:04 +0000470 if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) {
471 reservation_object_lock(resv, NULL);
472 if (!__read_seqcount_retry(&resv->seq, seq))
473 reservation_object_add_excl_fence(resv, NULL);
474 reservation_object_unlock(resv);
475 }
476
Chris Wilsone95433c2016-10-28 13:58:27 +0100477 return timeout;
478}
479
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000480static void __fence_set_priority(struct dma_fence *fence, int prio)
481{
482 struct drm_i915_gem_request *rq;
483 struct intel_engine_cs *engine;
484
485 if (!dma_fence_is_i915(fence))
486 return;
487
488 rq = to_request(fence);
489 engine = rq->engine;
490 if (!engine->schedule)
491 return;
492
493 engine->schedule(rq, prio);
494}
495
496static void fence_set_priority(struct dma_fence *fence, int prio)
497{
498 /* Recurse once into a fence-array */
499 if (dma_fence_is_array(fence)) {
500 struct dma_fence_array *array = to_dma_fence_array(fence);
501 int i;
502
503 for (i = 0; i < array->num_fences; i++)
504 __fence_set_priority(array->fences[i], prio);
505 } else {
506 __fence_set_priority(fence, prio);
507 }
508}
509
510int
511i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
512 unsigned int flags,
513 int prio)
514{
515 struct dma_fence *excl;
516
517 if (flags & I915_WAIT_ALL) {
518 struct dma_fence **shared;
519 unsigned int count, i;
520 int ret;
521
522 ret = reservation_object_get_fences_rcu(obj->resv,
523 &excl, &count, &shared);
524 if (ret)
525 return ret;
526
527 for (i = 0; i < count; i++) {
528 fence_set_priority(shared[i], prio);
529 dma_fence_put(shared[i]);
530 }
531
532 kfree(shared);
533 } else {
534 excl = reservation_object_get_excl_rcu(obj->resv);
535 }
536
537 if (excl) {
538 fence_set_priority(excl, prio);
539 dma_fence_put(excl);
540 }
541 return 0;
542}
543
Chris Wilson00e60f22016-08-04 16:32:40 +0100544/**
Chris Wilsone95433c2016-10-28 13:58:27 +0100545 * Waits for rendering to the object to be completed
Chris Wilson00e60f22016-08-04 16:32:40 +0100546 * @obj: i915 gem object
Chris Wilsone95433c2016-10-28 13:58:27 +0100547 * @flags: how to wait (under a lock, for all rendering or just for writes etc)
548 * @timeout: how long to wait
549 * @rps: client (user process) to charge for any waitboosting
Chris Wilson00e60f22016-08-04 16:32:40 +0100550 */
551int
Chris Wilsone95433c2016-10-28 13:58:27 +0100552i915_gem_object_wait(struct drm_i915_gem_object *obj,
553 unsigned int flags,
554 long timeout,
555 struct intel_rps_client *rps)
Chris Wilson00e60f22016-08-04 16:32:40 +0100556{
Chris Wilsone95433c2016-10-28 13:58:27 +0100557 might_sleep();
558#if IS_ENABLED(CONFIG_LOCKDEP)
559 GEM_BUG_ON(debug_locks &&
560 !!lockdep_is_held(&obj->base.dev->struct_mutex) !=
561 !!(flags & I915_WAIT_LOCKED));
562#endif
563 GEM_BUG_ON(timeout < 0);
Chris Wilson00e60f22016-08-04 16:32:40 +0100564
Chris Wilsond07f0e52016-10-28 13:58:44 +0100565 timeout = i915_gem_object_wait_reservation(obj->resv,
566 flags, timeout,
567 rps);
Chris Wilsone95433c2016-10-28 13:58:27 +0100568 return timeout < 0 ? timeout : 0;
Chris Wilson00e60f22016-08-04 16:32:40 +0100569}
570
571static struct intel_rps_client *to_rps_client(struct drm_file *file)
572{
573 struct drm_i915_file_private *fpriv = file->driver_priv;
574
575 return &fpriv->rps;
576}
577
Chris Wilson00731152014-05-21 12:42:56 +0100578int
579i915_gem_object_attach_phys(struct drm_i915_gem_object *obj,
580 int align)
581{
Chris Wilson6a2c4232014-11-04 04:51:40 -0800582 int ret;
Chris Wilson00731152014-05-21 12:42:56 +0100583
Chris Wilsondbb43512016-12-07 13:34:11 +0000584 if (align > obj->base.size)
585 return -EINVAL;
Chris Wilson00731152014-05-21 12:42:56 +0100586
Chris Wilsondbb43512016-12-07 13:34:11 +0000587 if (obj->ops == &i915_gem_phys_ops)
Chris Wilson00731152014-05-21 12:42:56 +0100588 return 0;
Chris Wilson00731152014-05-21 12:42:56 +0100589
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100590 if (obj->mm.madv != I915_MADV_WILLNEED)
Chris Wilson00731152014-05-21 12:42:56 +0100591 return -EFAULT;
592
593 if (obj->base.filp == NULL)
594 return -EINVAL;
595
Chris Wilson4717ca92016-08-04 07:52:28 +0100596 ret = i915_gem_object_unbind(obj);
597 if (ret)
598 return ret;
599
Chris Wilson548625e2016-11-01 12:11:34 +0000600 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100601 if (obj->mm.pages)
602 return -EBUSY;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800603
Chris Wilson581ab1f2017-02-15 16:39:00 +0000604 GEM_BUG_ON(obj->ops != &i915_gem_object_ops);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800605 obj->ops = &i915_gem_phys_ops;
606
Chris Wilson581ab1f2017-02-15 16:39:00 +0000607 ret = i915_gem_object_pin_pages(obj);
608 if (ret)
609 goto err_xfer;
610
611 return 0;
612
613err_xfer:
614 obj->ops = &i915_gem_object_ops;
615 return ret;
Chris Wilson00731152014-05-21 12:42:56 +0100616}
617
618static int
619i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
620 struct drm_i915_gem_pwrite *args,
Chris Wilson03ac84f2016-10-28 13:58:36 +0100621 struct drm_file *file)
Chris Wilson00731152014-05-21 12:42:56 +0100622{
Chris Wilson00731152014-05-21 12:42:56 +0100623 void *vaddr = obj->phys_handle->vaddr + args->offset;
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300624 char __user *user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800625
626 /* We manually control the domain here and pretend that it
627 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
628 */
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -0700629 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000630 if (copy_from_user(vaddr, user_data, args->size))
631 return -EFAULT;
Chris Wilson00731152014-05-21 12:42:56 +0100632
Chris Wilson6a2c4232014-11-04 04:51:40 -0800633 drm_clflush_virt_range(vaddr, args->size);
Chris Wilson10466d22017-01-06 15:22:38 +0000634 i915_gem_chipset_flush(to_i915(obj->base.dev));
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200635
Rodrigo Vivide152b62015-07-07 16:28:51 -0700636 intel_fb_obj_flush(obj, false, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000637 return 0;
Chris Wilson00731152014-05-21 12:42:56 +0100638}
639
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000640void *i915_gem_object_alloc(struct drm_i915_private *dev_priv)
Chris Wilson42dcedd2012-11-15 11:32:30 +0000641{
Chris Wilsonefab6d82015-04-07 16:20:57 +0100642 return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000643}
644
645void i915_gem_object_free(struct drm_i915_gem_object *obj)
646{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100647 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonefab6d82015-04-07 16:20:57 +0100648 kmem_cache_free(dev_priv->objects, obj);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000649}
650
Dave Airlieff72145b2011-02-07 12:16:14 +1000651static int
652i915_gem_create(struct drm_file *file,
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000653 struct drm_i915_private *dev_priv,
Dave Airlieff72145b2011-02-07 12:16:14 +1000654 uint64_t size,
655 uint32_t *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700656{
Chris Wilson05394f32010-11-08 19:18:58 +0000657 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300658 int ret;
659 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700660
Dave Airlieff72145b2011-02-07 12:16:14 +1000661 size = roundup(size, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200662 if (size == 0)
663 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700664
665 /* Allocate the new object */
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000666 obj = i915_gem_object_create(dev_priv, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100667 if (IS_ERR(obj))
668 return PTR_ERR(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700669
Chris Wilson05394f32010-11-08 19:18:58 +0000670 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100671 /* drop reference from allocate - handle holds it now */
Chris Wilsonf0cd5182016-10-28 13:58:43 +0100672 i915_gem_object_put(obj);
Daniel Vetterd861e332013-07-24 23:25:03 +0200673 if (ret)
674 return ret;
Chris Wilson202f2fe2010-10-14 13:20:40 +0100675
Dave Airlieff72145b2011-02-07 12:16:14 +1000676 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700677 return 0;
678}
679
Dave Airlieff72145b2011-02-07 12:16:14 +1000680int
681i915_gem_dumb_create(struct drm_file *file,
682 struct drm_device *dev,
683 struct drm_mode_create_dumb *args)
684{
685 /* have to work out size/pitch and return them */
Paulo Zanonide45eaf2013-10-18 18:48:24 -0300686 args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000687 args->size = args->pitch * args->height;
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000688 return i915_gem_create(file, to_i915(dev),
Dave Airlieda6b51d2014-12-24 13:11:17 +1000689 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000690}
691
Dave Airlieff72145b2011-02-07 12:16:14 +1000692/**
693 * Creates a new mm object and returns a handle to it.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100694 * @dev: drm device pointer
695 * @data: ioctl data blob
696 * @file: drm file pointer
Dave Airlieff72145b2011-02-07 12:16:14 +1000697 */
698int
699i915_gem_create_ioctl(struct drm_device *dev, void *data,
700 struct drm_file *file)
701{
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000702 struct drm_i915_private *dev_priv = to_i915(dev);
Dave Airlieff72145b2011-02-07 12:16:14 +1000703 struct drm_i915_gem_create *args = data;
Daniel Vetter63ed2cb2012-04-23 16:50:50 +0200704
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000705 i915_gem_flush_free_objects(dev_priv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +0100706
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000707 return i915_gem_create(file, dev_priv,
Dave Airlieda6b51d2014-12-24 13:11:17 +1000708 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000709}
710
Daniel Vetter8c599672011-12-14 13:57:31 +0100711static inline int
Daniel Vetter8461d222011-12-14 13:57:32 +0100712__copy_to_user_swizzled(char __user *cpu_vaddr,
713 const char *gpu_vaddr, int gpu_offset,
714 int length)
715{
716 int ret, cpu_offset = 0;
717
718 while (length > 0) {
719 int cacheline_end = ALIGN(gpu_offset + 1, 64);
720 int this_length = min(cacheline_end - gpu_offset, length);
721 int swizzled_gpu_offset = gpu_offset ^ 64;
722
723 ret = __copy_to_user(cpu_vaddr + cpu_offset,
724 gpu_vaddr + swizzled_gpu_offset,
725 this_length);
726 if (ret)
727 return ret + length;
728
729 cpu_offset += this_length;
730 gpu_offset += this_length;
731 length -= this_length;
732 }
733
734 return 0;
735}
736
737static inline int
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700738__copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
739 const char __user *cpu_vaddr,
Daniel Vetter8c599672011-12-14 13:57:31 +0100740 int length)
741{
742 int ret, cpu_offset = 0;
743
744 while (length > 0) {
745 int cacheline_end = ALIGN(gpu_offset + 1, 64);
746 int this_length = min(cacheline_end - gpu_offset, length);
747 int swizzled_gpu_offset = gpu_offset ^ 64;
748
749 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
750 cpu_vaddr + cpu_offset,
751 this_length);
752 if (ret)
753 return ret + length;
754
755 cpu_offset += this_length;
756 gpu_offset += this_length;
757 length -= this_length;
758 }
759
760 return 0;
761}
762
Brad Volkin4c914c02014-02-18 10:15:45 -0800763/*
764 * Pins the specified object's pages and synchronizes the object with
765 * GPU accesses. Sets needs_clflush to non-zero if the caller should
766 * flush the object from the CPU cache.
767 */
768int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
Chris Wilson43394c72016-08-18 17:16:47 +0100769 unsigned int *needs_clflush)
Brad Volkin4c914c02014-02-18 10:15:45 -0800770{
771 int ret;
772
Chris Wilsone95433c2016-10-28 13:58:27 +0100773 lockdep_assert_held(&obj->base.dev->struct_mutex);
Brad Volkin4c914c02014-02-18 10:15:45 -0800774
Chris Wilsone95433c2016-10-28 13:58:27 +0100775 *needs_clflush = 0;
Chris Wilson43394c72016-08-18 17:16:47 +0100776 if (!i915_gem_object_has_struct_page(obj))
777 return -ENODEV;
Brad Volkin4c914c02014-02-18 10:15:45 -0800778
Chris Wilsone95433c2016-10-28 13:58:27 +0100779 ret = i915_gem_object_wait(obj,
780 I915_WAIT_INTERRUPTIBLE |
781 I915_WAIT_LOCKED,
782 MAX_SCHEDULE_TIMEOUT,
783 NULL);
Chris Wilsonc13d87e2016-07-20 09:21:15 +0100784 if (ret)
785 return ret;
786
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100787 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100788 if (ret)
789 return ret;
790
Chris Wilsona314d5c2016-08-18 17:16:48 +0100791 i915_gem_object_flush_gtt_write_domain(obj);
792
Chris Wilson43394c72016-08-18 17:16:47 +0100793 /* If we're not in the cpu read domain, set ourself into the gtt
794 * read domain and manually flush cachelines (if required). This
795 * optimizes for the case when the gpu will dirty the data
796 * anyway again before the next pread happens.
797 */
798 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
Brad Volkin4c914c02014-02-18 10:15:45 -0800799 *needs_clflush = !cpu_cache_is_coherent(obj->base.dev,
800 obj->cache_level);
Brad Volkin4c914c02014-02-18 10:15:45 -0800801
Chris Wilson43394c72016-08-18 17:16:47 +0100802 if (*needs_clflush && !static_cpu_has(X86_FEATURE_CLFLUSH)) {
803 ret = i915_gem_object_set_to_cpu_domain(obj, false);
Chris Wilson97649512016-08-18 17:16:50 +0100804 if (ret)
805 goto err_unpin;
806
Chris Wilson43394c72016-08-18 17:16:47 +0100807 *needs_clflush = 0;
808 }
809
Chris Wilson97649512016-08-18 17:16:50 +0100810 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100811 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100812
813err_unpin:
814 i915_gem_object_unpin_pages(obj);
815 return ret;
Chris Wilson43394c72016-08-18 17:16:47 +0100816}
817
818int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
819 unsigned int *needs_clflush)
820{
821 int ret;
822
Chris Wilsone95433c2016-10-28 13:58:27 +0100823 lockdep_assert_held(&obj->base.dev->struct_mutex);
824
Chris Wilson43394c72016-08-18 17:16:47 +0100825 *needs_clflush = 0;
826 if (!i915_gem_object_has_struct_page(obj))
827 return -ENODEV;
828
Chris Wilsone95433c2016-10-28 13:58:27 +0100829 ret = i915_gem_object_wait(obj,
830 I915_WAIT_INTERRUPTIBLE |
831 I915_WAIT_LOCKED |
832 I915_WAIT_ALL,
833 MAX_SCHEDULE_TIMEOUT,
834 NULL);
Chris Wilson43394c72016-08-18 17:16:47 +0100835 if (ret)
836 return ret;
837
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100838 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100839 if (ret)
840 return ret;
841
Chris Wilsona314d5c2016-08-18 17:16:48 +0100842 i915_gem_object_flush_gtt_write_domain(obj);
843
Chris Wilson43394c72016-08-18 17:16:47 +0100844 /* If we're not in the cpu write domain, set ourself into the
845 * gtt write domain and manually flush cachelines (as required).
846 * This optimizes for the case when the gpu will use the data
847 * right away and we therefore have to clflush anyway.
848 */
849 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
850 *needs_clflush |= cpu_write_needs_clflush(obj) << 1;
851
852 /* Same trick applies to invalidate partially written cachelines read
853 * before writing.
854 */
855 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
856 *needs_clflush |= !cpu_cache_is_coherent(obj->base.dev,
857 obj->cache_level);
858
Chris Wilson43394c72016-08-18 17:16:47 +0100859 if (*needs_clflush && !static_cpu_has(X86_FEATURE_CLFLUSH)) {
860 ret = i915_gem_object_set_to_cpu_domain(obj, true);
Chris Wilson97649512016-08-18 17:16:50 +0100861 if (ret)
862 goto err_unpin;
863
Chris Wilson43394c72016-08-18 17:16:47 +0100864 *needs_clflush = 0;
865 }
866
867 if ((*needs_clflush & CLFLUSH_AFTER) == 0)
868 obj->cache_dirty = true;
869
870 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100871 obj->mm.dirty = true;
Chris Wilson97649512016-08-18 17:16:50 +0100872 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100873 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100874
875err_unpin:
876 i915_gem_object_unpin_pages(obj);
877 return ret;
Brad Volkin4c914c02014-02-18 10:15:45 -0800878}
879
Daniel Vetter23c18c72012-03-25 19:47:42 +0200880static void
881shmem_clflush_swizzled_range(char *addr, unsigned long length,
882 bool swizzled)
883{
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200884 if (unlikely(swizzled)) {
Daniel Vetter23c18c72012-03-25 19:47:42 +0200885 unsigned long start = (unsigned long) addr;
886 unsigned long end = (unsigned long) addr + length;
887
888 /* For swizzling simply ensure that we always flush both
889 * channels. Lame, but simple and it works. Swizzled
890 * pwrite/pread is far from a hotpath - current userspace
891 * doesn't use it at all. */
892 start = round_down(start, 128);
893 end = round_up(end, 128);
894
895 drm_clflush_virt_range((void *)start, end - start);
896 } else {
897 drm_clflush_virt_range(addr, length);
898 }
899
900}
901
Daniel Vetterd174bd62012-03-25 19:47:40 +0200902/* Only difference to the fast-path function is that this can handle bit17
903 * and uses non-atomic copy and kmap functions. */
904static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100905shmem_pread_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +0200906 char __user *user_data,
907 bool page_do_bit17_swizzling, bool needs_clflush)
908{
909 char *vaddr;
910 int ret;
911
912 vaddr = kmap(page);
913 if (needs_clflush)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100914 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +0200915 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200916
917 if (page_do_bit17_swizzling)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100918 ret = __copy_to_user_swizzled(user_data, vaddr, offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200919 else
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100920 ret = __copy_to_user(user_data, vaddr + offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200921 kunmap(page);
922
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100923 return ret ? - EFAULT : 0;
Daniel Vetterd174bd62012-03-25 19:47:40 +0200924}
925
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100926static int
927shmem_pread(struct page *page, int offset, int length, char __user *user_data,
928 bool page_do_bit17_swizzling, bool needs_clflush)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530929{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100930 int ret;
931
932 ret = -ENODEV;
933 if (!page_do_bit17_swizzling) {
934 char *vaddr = kmap_atomic(page);
935
936 if (needs_clflush)
937 drm_clflush_virt_range(vaddr + offset, length);
938 ret = __copy_to_user_inatomic(user_data, vaddr + offset, length);
939 kunmap_atomic(vaddr);
940 }
941 if (ret == 0)
942 return 0;
943
944 return shmem_pread_slow(page, offset, length, user_data,
945 page_do_bit17_swizzling, needs_clflush);
946}
947
948static int
949i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
950 struct drm_i915_gem_pread *args)
951{
952 char __user *user_data;
953 u64 remain;
954 unsigned int obj_do_bit17_swizzling;
955 unsigned int needs_clflush;
956 unsigned int idx, offset;
957 int ret;
958
959 obj_do_bit17_swizzling = 0;
960 if (i915_gem_object_needs_bit17_swizzle(obj))
961 obj_do_bit17_swizzling = BIT(17);
962
963 ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex);
964 if (ret)
965 return ret;
966
967 ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
968 mutex_unlock(&obj->base.dev->struct_mutex);
969 if (ret)
970 return ret;
971
972 remain = args->size;
973 user_data = u64_to_user_ptr(args->data_ptr);
974 offset = offset_in_page(args->offset);
975 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
976 struct page *page = i915_gem_object_get_page(obj, idx);
977 int length;
978
979 length = remain;
980 if (offset + length > PAGE_SIZE)
981 length = PAGE_SIZE - offset;
982
983 ret = shmem_pread(page, offset, length, user_data,
984 page_to_phys(page) & obj_do_bit17_swizzling,
985 needs_clflush);
986 if (ret)
987 break;
988
989 remain -= length;
990 user_data += length;
991 offset = 0;
992 }
993
994 i915_gem_obj_finish_shmem_access(obj);
995 return ret;
996}
997
998static inline bool
999gtt_user_read(struct io_mapping *mapping,
1000 loff_t base, int offset,
1001 char __user *user_data, int length)
1002{
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301003 void *vaddr;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001004 unsigned long unwritten;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301005
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301006 /* We can use the cpu mem copy function because this is X86. */
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001007 vaddr = (void __force *)io_mapping_map_atomic_wc(mapping, base);
1008 unwritten = __copy_to_user_inatomic(user_data, vaddr + offset, length);
1009 io_mapping_unmap_atomic(vaddr);
1010 if (unwritten) {
1011 vaddr = (void __force *)
1012 io_mapping_map_wc(mapping, base, PAGE_SIZE);
1013 unwritten = copy_to_user(user_data, vaddr + offset, length);
1014 io_mapping_unmap(vaddr);
1015 }
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301016 return unwritten;
1017}
1018
1019static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001020i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
1021 const struct drm_i915_gem_pread *args)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301022{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001023 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1024 struct i915_ggtt *ggtt = &i915->ggtt;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301025 struct drm_mm_node node;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001026 struct i915_vma *vma;
1027 void __user *user_data;
1028 u64 remain, offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301029 int ret;
1030
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001031 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1032 if (ret)
1033 return ret;
1034
1035 intel_runtime_pm_get(i915);
1036 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
1037 PIN_MAPPABLE | PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001038 if (!IS_ERR(vma)) {
1039 node.start = i915_ggtt_offset(vma);
1040 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001041 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001042 if (ret) {
1043 i915_vma_unpin(vma);
1044 vma = ERR_PTR(ret);
1045 }
1046 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001047 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001048 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301049 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001050 goto out_unlock;
1051 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301052 }
1053
1054 ret = i915_gem_object_set_to_gtt_domain(obj, false);
1055 if (ret)
1056 goto out_unpin;
1057
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001058 mutex_unlock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301059
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001060 user_data = u64_to_user_ptr(args->data_ptr);
1061 remain = args->size;
1062 offset = args->offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301063
1064 while (remain > 0) {
1065 /* Operation in this page
1066 *
1067 * page_base = page offset within aperture
1068 * page_offset = offset within page
1069 * page_length = bytes to copy for this page
1070 */
1071 u32 page_base = node.start;
1072 unsigned page_offset = offset_in_page(offset);
1073 unsigned page_length = PAGE_SIZE - page_offset;
1074 page_length = remain < page_length ? remain : page_length;
1075 if (node.allocated) {
1076 wmb();
1077 ggtt->base.insert_page(&ggtt->base,
1078 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001079 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301080 wmb();
1081 } else {
1082 page_base += offset & PAGE_MASK;
1083 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001084
1085 if (gtt_user_read(&ggtt->mappable, page_base, page_offset,
1086 user_data, page_length)) {
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301087 ret = -EFAULT;
1088 break;
1089 }
1090
1091 remain -= page_length;
1092 user_data += page_length;
1093 offset += page_length;
1094 }
1095
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001096 mutex_lock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301097out_unpin:
1098 if (node.allocated) {
1099 wmb();
1100 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001101 node.start, node.size);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301102 remove_mappable_node(&node);
1103 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001104 i915_vma_unpin(vma);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301105 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001106out_unlock:
1107 intel_runtime_pm_put(i915);
1108 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +01001109
Eric Anholteb014592009-03-10 11:44:52 -07001110 return ret;
1111}
1112
Eric Anholt673a3942008-07-30 12:06:12 -07001113/**
1114 * Reads data from the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001115 * @dev: drm device pointer
1116 * @data: ioctl data blob
1117 * @file: drm file pointer
Eric Anholt673a3942008-07-30 12:06:12 -07001118 *
1119 * On error, the contents of *data are undefined.
1120 */
1121int
1122i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001123 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001124{
1125 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001126 struct drm_i915_gem_object *obj;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001127 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001128
Chris Wilson51311d02010-11-17 09:10:42 +00001129 if (args->size == 0)
1130 return 0;
1131
1132 if (!access_ok(VERIFY_WRITE,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001133 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001134 args->size))
1135 return -EFAULT;
1136
Chris Wilson03ac0642016-07-20 13:31:51 +01001137 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001138 if (!obj)
1139 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001140
Chris Wilson7dcd2492010-09-26 20:21:44 +01001141 /* Bounds check source. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001142 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001143 ret = -EINVAL;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001144 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001145 }
1146
Chris Wilsondb53a302011-02-03 11:57:46 +00001147 trace_i915_gem_object_pread(obj, args->offset, args->size);
1148
Chris Wilsone95433c2016-10-28 13:58:27 +01001149 ret = i915_gem_object_wait(obj,
1150 I915_WAIT_INTERRUPTIBLE,
1151 MAX_SCHEDULE_TIMEOUT,
1152 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001153 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001154 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001155
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001156 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001157 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001158 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001159
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001160 ret = i915_gem_shmem_pread(obj, args);
Chris Wilson9c870d02016-10-24 13:42:15 +01001161 if (ret == -EFAULT || ret == -ENODEV)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001162 ret = i915_gem_gtt_pread(obj, args);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301163
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001164 i915_gem_object_unpin_pages(obj);
1165out:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001166 i915_gem_object_put(obj);
Eric Anholteb014592009-03-10 11:44:52 -07001167 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001168}
1169
Keith Packard0839ccb2008-10-30 19:38:48 -07001170/* This is the fast write path which cannot handle
1171 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001172 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001173
Chris Wilsonfe115622016-10-28 13:58:40 +01001174static inline bool
1175ggtt_write(struct io_mapping *mapping,
1176 loff_t base, int offset,
1177 char __user *user_data, int length)
Keith Packard0839ccb2008-10-30 19:38:48 -07001178{
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -07001179 void *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -07001180 unsigned long unwritten;
1181
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -07001182 /* We can use the cpu mem copy function because this is X86. */
Chris Wilsonfe115622016-10-28 13:58:40 +01001183 vaddr = (void __force *)io_mapping_map_atomic_wc(mapping, base);
1184 unwritten = __copy_from_user_inatomic_nocache(vaddr + offset,
Keith Packard0839ccb2008-10-30 19:38:48 -07001185 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001186 io_mapping_unmap_atomic(vaddr);
1187 if (unwritten) {
1188 vaddr = (void __force *)
1189 io_mapping_map_wc(mapping, base, PAGE_SIZE);
1190 unwritten = copy_from_user(vaddr + offset, user_data, length);
1191 io_mapping_unmap(vaddr);
1192 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001193
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001194 return unwritten;
1195}
1196
Eric Anholt3de09aa2009-03-09 09:42:23 -07001197/**
1198 * This is the fast pwrite path, where we copy the data directly from the
1199 * user into the GTT, uncached.
Chris Wilsonfe115622016-10-28 13:58:40 +01001200 * @obj: i915 GEM object
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001201 * @args: pwrite arguments structure
Eric Anholt3de09aa2009-03-09 09:42:23 -07001202 */
Eric Anholt673a3942008-07-30 12:06:12 -07001203static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001204i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
1205 const struct drm_i915_gem_pwrite *args)
Eric Anholt673a3942008-07-30 12:06:12 -07001206{
Chris Wilsonfe115622016-10-28 13:58:40 +01001207 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301208 struct i915_ggtt *ggtt = &i915->ggtt;
1209 struct drm_mm_node node;
Chris Wilsonfe115622016-10-28 13:58:40 +01001210 struct i915_vma *vma;
1211 u64 remain, offset;
1212 void __user *user_data;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301213 int ret;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301214
Chris Wilsonfe115622016-10-28 13:58:40 +01001215 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1216 if (ret)
1217 return ret;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001218
Chris Wilson9c870d02016-10-24 13:42:15 +01001219 intel_runtime_pm_get(i915);
Chris Wilson058d88c2016-08-15 10:49:06 +01001220 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsonde895082016-08-04 16:32:34 +01001221 PIN_MAPPABLE | PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001222 if (!IS_ERR(vma)) {
1223 node.start = i915_ggtt_offset(vma);
1224 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001225 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001226 if (ret) {
1227 i915_vma_unpin(vma);
1228 vma = ERR_PTR(ret);
1229 }
1230 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001231 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001232 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301233 if (ret)
Chris Wilsonfe115622016-10-28 13:58:40 +01001234 goto out_unlock;
1235 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301236 }
Daniel Vetter935aaa62012-03-25 19:47:35 +02001237
1238 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1239 if (ret)
1240 goto out_unpin;
1241
Chris Wilsonfe115622016-10-28 13:58:40 +01001242 mutex_unlock(&i915->drm.struct_mutex);
1243
Chris Wilsonb19482d2016-08-18 17:16:43 +01001244 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Paulo Zanoni063e4e62015-02-13 17:23:45 -02001245
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301246 user_data = u64_to_user_ptr(args->data_ptr);
1247 offset = args->offset;
1248 remain = args->size;
1249 while (remain) {
Eric Anholt673a3942008-07-30 12:06:12 -07001250 /* Operation in this page
1251 *
Keith Packard0839ccb2008-10-30 19:38:48 -07001252 * page_base = page offset within aperture
1253 * page_offset = offset within page
1254 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -07001255 */
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301256 u32 page_base = node.start;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001257 unsigned int page_offset = offset_in_page(offset);
1258 unsigned int page_length = PAGE_SIZE - page_offset;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301259 page_length = remain < page_length ? remain : page_length;
1260 if (node.allocated) {
1261 wmb(); /* flush the write before we modify the GGTT */
1262 ggtt->base.insert_page(&ggtt->base,
1263 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
1264 node.start, I915_CACHE_NONE, 0);
1265 wmb(); /* flush modifications to the GGTT (insert_page) */
1266 } else {
1267 page_base += offset & PAGE_MASK;
1268 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001269 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -07001270 * source page isn't available. Return the error and we'll
1271 * retry in the slow path.
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301272 * If the object is non-shmem backed, we retry again with the
1273 * path that handles page fault.
Keith Packard0839ccb2008-10-30 19:38:48 -07001274 */
Chris Wilsonfe115622016-10-28 13:58:40 +01001275 if (ggtt_write(&ggtt->mappable, page_base, page_offset,
1276 user_data, page_length)) {
1277 ret = -EFAULT;
1278 break;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001279 }
Eric Anholt673a3942008-07-30 12:06:12 -07001280
Keith Packard0839ccb2008-10-30 19:38:48 -07001281 remain -= page_length;
1282 user_data += page_length;
1283 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -07001284 }
Chris Wilsonb19482d2016-08-18 17:16:43 +01001285 intel_fb_obj_flush(obj, false, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001286
1287 mutex_lock(&i915->drm.struct_mutex);
Daniel Vetter935aaa62012-03-25 19:47:35 +02001288out_unpin:
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301289 if (node.allocated) {
1290 wmb();
1291 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001292 node.start, node.size);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301293 remove_mappable_node(&node);
1294 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001295 i915_vma_unpin(vma);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301296 }
Chris Wilsonfe115622016-10-28 13:58:40 +01001297out_unlock:
Chris Wilson9c870d02016-10-24 13:42:15 +01001298 intel_runtime_pm_put(i915);
Chris Wilsonfe115622016-10-28 13:58:40 +01001299 mutex_unlock(&i915->drm.struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -07001300 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001301}
1302
Eric Anholt673a3942008-07-30 12:06:12 -07001303static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001304shmem_pwrite_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +02001305 char __user *user_data,
1306 bool page_do_bit17_swizzling,
1307 bool needs_clflush_before,
1308 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -07001309{
Daniel Vetterd174bd62012-03-25 19:47:40 +02001310 char *vaddr;
1311 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001312
Daniel Vetterd174bd62012-03-25 19:47:40 +02001313 vaddr = kmap(page);
Daniel Vettere7e58eb2012-03-25 19:47:43 +02001314 if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
Chris Wilsonfe115622016-10-28 13:58:40 +01001315 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001316 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001317 if (page_do_bit17_swizzling)
Chris Wilsonfe115622016-10-28 13:58:40 +01001318 ret = __copy_from_user_swizzled(vaddr, offset, user_data,
1319 length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001320 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001321 ret = __copy_from_user(vaddr + offset, user_data, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001322 if (needs_clflush_after)
Chris Wilsonfe115622016-10-28 13:58:40 +01001323 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001324 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001325 kunmap(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001326
Chris Wilson755d2212012-09-04 21:02:55 +01001327 return ret ? -EFAULT : 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001328}
1329
Chris Wilsonfe115622016-10-28 13:58:40 +01001330/* Per-page copy function for the shmem pwrite fastpath.
1331 * Flushes invalid cachelines before writing to the target if
1332 * needs_clflush_before is set and flushes out any written cachelines after
1333 * writing if needs_clflush is set.
1334 */
Eric Anholt40123c12009-03-09 13:42:30 -07001335static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001336shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
1337 bool page_do_bit17_swizzling,
1338 bool needs_clflush_before,
1339 bool needs_clflush_after)
Eric Anholt40123c12009-03-09 13:42:30 -07001340{
Chris Wilsonfe115622016-10-28 13:58:40 +01001341 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001342
Chris Wilsonfe115622016-10-28 13:58:40 +01001343 ret = -ENODEV;
1344 if (!page_do_bit17_swizzling) {
1345 char *vaddr = kmap_atomic(page);
1346
1347 if (needs_clflush_before)
1348 drm_clflush_virt_range(vaddr + offset, len);
1349 ret = __copy_from_user_inatomic(vaddr + offset, user_data, len);
1350 if (needs_clflush_after)
1351 drm_clflush_virt_range(vaddr + offset, len);
1352
1353 kunmap_atomic(vaddr);
1354 }
1355 if (ret == 0)
1356 return ret;
1357
1358 return shmem_pwrite_slow(page, offset, len, user_data,
1359 page_do_bit17_swizzling,
1360 needs_clflush_before,
1361 needs_clflush_after);
1362}
1363
1364static int
1365i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
1366 const struct drm_i915_gem_pwrite *args)
1367{
1368 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1369 void __user *user_data;
1370 u64 remain;
1371 unsigned int obj_do_bit17_swizzling;
1372 unsigned int partial_cacheline_write;
1373 unsigned int needs_clflush;
1374 unsigned int offset, idx;
1375 int ret;
1376
1377 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
Chris Wilson43394c72016-08-18 17:16:47 +01001378 if (ret)
1379 return ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001380
Chris Wilsonfe115622016-10-28 13:58:40 +01001381 ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
1382 mutex_unlock(&i915->drm.struct_mutex);
1383 if (ret)
1384 return ret;
1385
1386 obj_do_bit17_swizzling = 0;
1387 if (i915_gem_object_needs_bit17_swizzle(obj))
1388 obj_do_bit17_swizzling = BIT(17);
1389
1390 /* If we don't overwrite a cacheline completely we need to be
1391 * careful to have up-to-date data by first clflushing. Don't
1392 * overcomplicate things and flush the entire patch.
1393 */
1394 partial_cacheline_write = 0;
1395 if (needs_clflush & CLFLUSH_BEFORE)
1396 partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
1397
Chris Wilson43394c72016-08-18 17:16:47 +01001398 user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson43394c72016-08-18 17:16:47 +01001399 remain = args->size;
Chris Wilsonfe115622016-10-28 13:58:40 +01001400 offset = offset_in_page(args->offset);
1401 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1402 struct page *page = i915_gem_object_get_page(obj, idx);
1403 int length;
Eric Anholt40123c12009-03-09 13:42:30 -07001404
Chris Wilsonfe115622016-10-28 13:58:40 +01001405 length = remain;
1406 if (offset + length > PAGE_SIZE)
1407 length = PAGE_SIZE - offset;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001408
Chris Wilsonfe115622016-10-28 13:58:40 +01001409 ret = shmem_pwrite(page, offset, length, user_data,
1410 page_to_phys(page) & obj_do_bit17_swizzling,
1411 (offset | length) & partial_cacheline_write,
1412 needs_clflush & CLFLUSH_AFTER);
1413 if (ret)
Chris Wilson9da3da62012-06-01 15:20:22 +01001414 break;
1415
Chris Wilsonfe115622016-10-28 13:58:40 +01001416 remain -= length;
1417 user_data += length;
1418 offset = 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001419 }
1420
Rodrigo Vivide152b62015-07-07 16:28:51 -07001421 intel_fb_obj_flush(obj, false, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001422 i915_gem_obj_finish_shmem_access(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001423 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001424}
1425
1426/**
1427 * Writes data to the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001428 * @dev: drm device
1429 * @data: ioctl data blob
1430 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001431 *
1432 * On error, the contents of the buffer that were to be modified are undefined.
1433 */
1434int
1435i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001436 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001437{
1438 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001439 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +00001440 int ret;
1441
1442 if (args->size == 0)
1443 return 0;
1444
1445 if (!access_ok(VERIFY_READ,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001446 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001447 args->size))
1448 return -EFAULT;
1449
Chris Wilson03ac0642016-07-20 13:31:51 +01001450 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001451 if (!obj)
1452 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001453
Chris Wilson7dcd2492010-09-26 20:21:44 +01001454 /* Bounds check destination. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001455 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001456 ret = -EINVAL;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001457 goto err;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001458 }
1459
Chris Wilsondb53a302011-02-03 11:57:46 +00001460 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
1461
Chris Wilsone95433c2016-10-28 13:58:27 +01001462 ret = i915_gem_object_wait(obj,
1463 I915_WAIT_INTERRUPTIBLE |
1464 I915_WAIT_ALL,
1465 MAX_SCHEDULE_TIMEOUT,
1466 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001467 if (ret)
1468 goto err;
1469
Chris Wilsonfe115622016-10-28 13:58:40 +01001470 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001471 if (ret)
Chris Wilsonfe115622016-10-28 13:58:40 +01001472 goto err;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001473
Daniel Vetter935aaa62012-03-25 19:47:35 +02001474 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -07001475 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1476 * it would end up going through the fenced access, and we'll get
1477 * different detiling behavior between reading and writing.
1478 * pread/pwrite currently are reading and writing from the CPU
1479 * perspective, requiring manual detiling by the client.
1480 */
Chris Wilson6eae0052016-06-20 15:05:52 +01001481 if (!i915_gem_object_has_struct_page(obj) ||
Chris Wilson9c870d02016-10-24 13:42:15 +01001482 cpu_write_needs_clflush(obj))
Daniel Vetter935aaa62012-03-25 19:47:35 +02001483 /* Note that the gtt paths might fail with non-page-backed user
1484 * pointers (e.g. gtt mappings when moving data between
Chris Wilson9c870d02016-10-24 13:42:15 +01001485 * textures). Fallback to the shmem path in that case.
1486 */
Chris Wilsonfe115622016-10-28 13:58:40 +01001487 ret = i915_gem_gtt_pwrite_fast(obj, args);
Eric Anholt673a3942008-07-30 12:06:12 -07001488
Chris Wilsond1054ee2016-07-16 18:42:36 +01001489 if (ret == -EFAULT || ret == -ENOSPC) {
Chris Wilson6a2c4232014-11-04 04:51:40 -08001490 if (obj->phys_handle)
1491 ret = i915_gem_phys_pwrite(obj, args, file);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301492 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001493 ret = i915_gem_shmem_pwrite(obj, args);
Chris Wilson6a2c4232014-11-04 04:51:40 -08001494 }
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001495
Chris Wilsonfe115622016-10-28 13:58:40 +01001496 i915_gem_object_unpin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001497err:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001498 i915_gem_object_put(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001499 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001500}
1501
Chris Wilsond243ad82016-08-18 17:16:44 +01001502static inline enum fb_op_origin
Chris Wilsonaeecc962016-06-17 14:46:39 -03001503write_origin(struct drm_i915_gem_object *obj, unsigned domain)
1504{
Chris Wilson50349242016-08-18 17:17:04 +01001505 return (domain == I915_GEM_DOMAIN_GTT ?
1506 obj->frontbuffer_ggtt_origin : ORIGIN_CPU);
Chris Wilsonaeecc962016-06-17 14:46:39 -03001507}
1508
Chris Wilson40e62d52016-10-28 13:58:41 +01001509static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
1510{
1511 struct drm_i915_private *i915;
1512 struct list_head *list;
1513 struct i915_vma *vma;
1514
1515 list_for_each_entry(vma, &obj->vma_list, obj_link) {
1516 if (!i915_vma_is_ggtt(vma))
Chris Wilson28f412e2016-12-23 14:57:55 +00001517 break;
Chris Wilson40e62d52016-10-28 13:58:41 +01001518
1519 if (i915_vma_is_active(vma))
1520 continue;
1521
1522 if (!drm_mm_node_allocated(&vma->node))
1523 continue;
1524
1525 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
1526 }
1527
1528 i915 = to_i915(obj->base.dev);
1529 list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list;
Joonas Lahtinen56cea322016-11-02 12:16:04 +02001530 list_move_tail(&obj->global_link, list);
Chris Wilson40e62d52016-10-28 13:58:41 +01001531}
1532
Eric Anholt673a3942008-07-30 12:06:12 -07001533/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001534 * Called when user space prepares to use an object with the CPU, either
1535 * through the mmap ioctl's mapping or a GTT mapping.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001536 * @dev: drm device
1537 * @data: ioctl data blob
1538 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001539 */
1540int
1541i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001542 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001543{
1544 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001545 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001546 uint32_t read_domains = args->read_domains;
1547 uint32_t write_domain = args->write_domain;
Chris Wilson40e62d52016-10-28 13:58:41 +01001548 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07001549
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001550 /* Only handle setting domains to types used by the CPU. */
Chris Wilsonb8f90962016-08-05 10:14:07 +01001551 if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001552 return -EINVAL;
1553
1554 /* Having something in the write domain implies it's in the read
1555 * domain, and only that read domain. Enforce that in the request.
1556 */
1557 if (write_domain != 0 && read_domains != write_domain)
1558 return -EINVAL;
1559
Chris Wilson03ac0642016-07-20 13:31:51 +01001560 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001561 if (!obj)
1562 return -ENOENT;
Jesse Barnes652c3932009-08-17 13:31:43 -07001563
Chris Wilson3236f572012-08-24 09:35:09 +01001564 /* Try to flush the object off the GPU without holding the lock.
1565 * We will repeat the flush holding the lock in the normal manner
1566 * to catch cases where we are gazumped.
1567 */
Chris Wilson40e62d52016-10-28 13:58:41 +01001568 err = i915_gem_object_wait(obj,
Chris Wilsone95433c2016-10-28 13:58:27 +01001569 I915_WAIT_INTERRUPTIBLE |
1570 (write_domain ? I915_WAIT_ALL : 0),
1571 MAX_SCHEDULE_TIMEOUT,
1572 to_rps_client(file));
Chris Wilson40e62d52016-10-28 13:58:41 +01001573 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001574 goto out;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001575
Chris Wilson40e62d52016-10-28 13:58:41 +01001576 /* Flush and acquire obj->pages so that we are coherent through
1577 * direct access in memory with previous cached writes through
1578 * shmemfs and that our cache domain tracking remains valid.
1579 * For example, if the obj->filp was moved to swap without us
1580 * being notified and releasing the pages, we would mistakenly
1581 * continue to assume that the obj remained out of the CPU cached
1582 * domain.
1583 */
1584 err = i915_gem_object_pin_pages(obj);
1585 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001586 goto out;
Chris Wilson40e62d52016-10-28 13:58:41 +01001587
1588 err = i915_mutex_lock_interruptible(dev);
1589 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001590 goto out_unpin;
Chris Wilson3236f572012-08-24 09:35:09 +01001591
Chris Wilson43566de2015-01-02 16:29:29 +05301592 if (read_domains & I915_GEM_DOMAIN_GTT)
Chris Wilson40e62d52016-10-28 13:58:41 +01001593 err = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Chris Wilson43566de2015-01-02 16:29:29 +05301594 else
Chris Wilson40e62d52016-10-28 13:58:41 +01001595 err = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
1596
1597 /* And bump the LRU for this access */
1598 i915_gem_object_bump_inactive_ggtt(obj);
1599
1600 mutex_unlock(&dev->struct_mutex);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001601
Daniel Vetter031b6982015-06-26 19:35:16 +02001602 if (write_domain != 0)
Chris Wilsonaeecc962016-06-17 14:46:39 -03001603 intel_fb_obj_invalidate(obj, write_origin(obj, write_domain));
Daniel Vetter031b6982015-06-26 19:35:16 +02001604
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001605out_unpin:
Chris Wilson40e62d52016-10-28 13:58:41 +01001606 i915_gem_object_unpin_pages(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001607out:
1608 i915_gem_object_put(obj);
Chris Wilson40e62d52016-10-28 13:58:41 +01001609 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07001610}
1611
1612/**
1613 * Called when user space has done writes to this buffer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001614 * @dev: drm device
1615 * @data: ioctl data blob
1616 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001617 */
1618int
1619i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001620 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001621{
1622 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001623 struct drm_i915_gem_object *obj;
Chris Wilsonc21724c2016-08-05 10:14:19 +01001624 int err = 0;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001625
Chris Wilson03ac0642016-07-20 13:31:51 +01001626 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonc21724c2016-08-05 10:14:19 +01001627 if (!obj)
1628 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001629
Eric Anholt673a3942008-07-30 12:06:12 -07001630 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilsonc21724c2016-08-05 10:14:19 +01001631 if (READ_ONCE(obj->pin_display)) {
1632 err = i915_mutex_lock_interruptible(dev);
1633 if (!err) {
1634 i915_gem_object_flush_cpu_write_domain(obj);
1635 mutex_unlock(&dev->struct_mutex);
1636 }
1637 }
Eric Anholte47c68e2008-11-14 13:35:19 -08001638
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001639 i915_gem_object_put(obj);
Chris Wilsonc21724c2016-08-05 10:14:19 +01001640 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07001641}
1642
1643/**
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001644 * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
1645 * it is mapped to.
1646 * @dev: drm device
1647 * @data: ioctl data blob
1648 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001649 *
1650 * While the mapping holds a reference on the contents of the object, it doesn't
1651 * imply a ref on the object itself.
Daniel Vetter34367382014-10-16 12:28:18 +02001652 *
1653 * IMPORTANT:
1654 *
1655 * DRM driver writers who look a this function as an example for how to do GEM
1656 * mmap support, please don't implement mmap support like here. The modern way
1657 * to implement DRM mmap support is with an mmap offset ioctl (like
1658 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
1659 * That way debug tooling like valgrind will understand what's going on, hiding
1660 * the mmap call in a driver private ioctl will break that. The i915 driver only
1661 * does cpu mmaps this way because we didn't know better.
Eric Anholt673a3942008-07-30 12:06:12 -07001662 */
1663int
1664i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001665 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001666{
1667 struct drm_i915_gem_mmap *args = data;
Chris Wilson03ac0642016-07-20 13:31:51 +01001668 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001669 unsigned long addr;
1670
Akash Goel1816f922015-01-02 16:29:30 +05301671 if (args->flags & ~(I915_MMAP_WC))
1672 return -EINVAL;
1673
Borislav Petkov568a58e2016-03-29 17:42:01 +02001674 if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
Akash Goel1816f922015-01-02 16:29:30 +05301675 return -ENODEV;
1676
Chris Wilson03ac0642016-07-20 13:31:51 +01001677 obj = i915_gem_object_lookup(file, args->handle);
1678 if (!obj)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001679 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001680
Daniel Vetter1286ff72012-05-10 15:25:09 +02001681 /* prime objects have no backing filp to GEM mmap
1682 * pages from.
1683 */
Chris Wilson03ac0642016-07-20 13:31:51 +01001684 if (!obj->base.filp) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001685 i915_gem_object_put(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02001686 return -EINVAL;
1687 }
1688
Chris Wilson03ac0642016-07-20 13:31:51 +01001689 addr = vm_mmap(obj->base.filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001690 PROT_READ | PROT_WRITE, MAP_SHARED,
1691 args->offset);
Akash Goel1816f922015-01-02 16:29:30 +05301692 if (args->flags & I915_MMAP_WC) {
1693 struct mm_struct *mm = current->mm;
1694 struct vm_area_struct *vma;
1695
Michal Hocko80a89a52016-05-23 16:26:11 -07001696 if (down_write_killable(&mm->mmap_sem)) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001697 i915_gem_object_put(obj);
Michal Hocko80a89a52016-05-23 16:26:11 -07001698 return -EINTR;
1699 }
Akash Goel1816f922015-01-02 16:29:30 +05301700 vma = find_vma(mm, addr);
1701 if (vma)
1702 vma->vm_page_prot =
1703 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
1704 else
1705 addr = -ENOMEM;
1706 up_write(&mm->mmap_sem);
Chris Wilsonaeecc962016-06-17 14:46:39 -03001707
1708 /* This may race, but that's ok, it only gets set */
Chris Wilson50349242016-08-18 17:17:04 +01001709 WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
Akash Goel1816f922015-01-02 16:29:30 +05301710 }
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001711 i915_gem_object_put(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001712 if (IS_ERR((void *)addr))
1713 return addr;
1714
1715 args->addr_ptr = (uint64_t) addr;
1716
1717 return 0;
1718}
1719
Chris Wilson03af84f2016-08-18 17:17:01 +01001720static unsigned int tile_row_pages(struct drm_i915_gem_object *obj)
1721{
Chris Wilson6649a0b2017-01-09 16:16:08 +00001722 return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT;
Chris Wilson03af84f2016-08-18 17:17:01 +01001723}
1724
Jesse Barnesde151cf2008-11-12 10:03:55 -08001725/**
Chris Wilson4cc69072016-08-25 19:05:19 +01001726 * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
1727 *
1728 * A history of the GTT mmap interface:
1729 *
1730 * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
1731 * aligned and suitable for fencing, and still fit into the available
1732 * mappable space left by the pinned display objects. A classic problem
1733 * we called the page-fault-of-doom where we would ping-pong between
1734 * two objects that could not fit inside the GTT and so the memcpy
1735 * would page one object in at the expense of the other between every
1736 * single byte.
1737 *
1738 * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
1739 * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
1740 * object is too large for the available space (or simply too large
1741 * for the mappable aperture!), a view is created instead and faulted
1742 * into userspace. (This view is aligned and sized appropriately for
1743 * fenced access.)
1744 *
1745 * Restrictions:
1746 *
1747 * * snoopable objects cannot be accessed via the GTT. It can cause machine
1748 * hangs on some architectures, corruption on others. An attempt to service
1749 * a GTT page fault from a snoopable object will generate a SIGBUS.
1750 *
1751 * * the object must be able to fit into RAM (physical memory, though no
1752 * limited to the mappable aperture).
1753 *
1754 *
1755 * Caveats:
1756 *
1757 * * a new GTT page fault will synchronize rendering from the GPU and flush
1758 * all data to system memory. Subsequent access will not be synchronized.
1759 *
1760 * * all mappings are revoked on runtime device suspend.
1761 *
1762 * * there are only 8, 16 or 32 fence registers to share between all users
1763 * (older machines require fence register for display and blitter access
1764 * as well). Contention of the fence registers will cause the previous users
1765 * to be unmapped and any new access will generate new page faults.
1766 *
1767 * * running out of memory while servicing a fault may generate a SIGBUS,
1768 * rather than the expected SIGSEGV.
1769 */
1770int i915_gem_mmap_gtt_version(void)
1771{
1772 return 1;
1773}
1774
Chris Wilson2d4281b2017-01-10 09:56:32 +00001775static inline struct i915_ggtt_view
1776compute_partial_view(struct drm_i915_gem_object *obj,
Chris Wilson2d4281b2017-01-10 09:56:32 +00001777 pgoff_t page_offset,
1778 unsigned int chunk)
1779{
1780 struct i915_ggtt_view view;
1781
1782 if (i915_gem_object_is_tiled(obj))
1783 chunk = roundup(chunk, tile_row_pages(obj));
1784
Chris Wilson2d4281b2017-01-10 09:56:32 +00001785 view.type = I915_GGTT_VIEW_PARTIAL;
Chris Wilson8bab11932017-01-14 00:28:25 +00001786 view.partial.offset = rounddown(page_offset, chunk);
1787 view.partial.size =
Chris Wilson2d4281b2017-01-10 09:56:32 +00001788 min_t(unsigned int, chunk,
Chris Wilson8bab11932017-01-14 00:28:25 +00001789 (obj->base.size >> PAGE_SHIFT) - view.partial.offset);
Chris Wilson2d4281b2017-01-10 09:56:32 +00001790
1791 /* If the partial covers the entire object, just create a normal VMA. */
1792 if (chunk >= obj->base.size >> PAGE_SHIFT)
1793 view.type = I915_GGTT_VIEW_NORMAL;
1794
1795 return view;
1796}
1797
Chris Wilson4cc69072016-08-25 19:05:19 +01001798/**
Jesse Barnesde151cf2008-11-12 10:03:55 -08001799 * i915_gem_fault - fault a page into the GTT
Chris Wilson058d88c2016-08-15 10:49:06 +01001800 * @area: CPU VMA in question
Geliang Tangd9072a32015-09-15 05:58:44 -07001801 * @vmf: fault info
Jesse Barnesde151cf2008-11-12 10:03:55 -08001802 *
1803 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1804 * from userspace. The fault handler takes care of binding the object to
1805 * the GTT (if needed), allocating and programming a fence register (again,
1806 * only if needed based on whether the old reg is still valid or the object
1807 * is tiled) and inserting a new PTE into the faulting process.
1808 *
1809 * Note that the faulting process may involve evicting existing objects
1810 * from the GTT and/or fence registers to make room. So performance may
1811 * suffer if the GTT working set is large or there are few fence registers
1812 * left.
Chris Wilson4cc69072016-08-25 19:05:19 +01001813 *
1814 * The current feature set supported by i915_gem_fault() and thus GTT mmaps
1815 * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
Jesse Barnesde151cf2008-11-12 10:03:55 -08001816 */
Chris Wilson058d88c2016-08-15 10:49:06 +01001817int i915_gem_fault(struct vm_area_struct *area, struct vm_fault *vmf)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001818{
Chris Wilson03af84f2016-08-18 17:17:01 +01001819#define MIN_CHUNK_PAGES ((1 << 20) >> PAGE_SHIFT) /* 1 MiB */
Chris Wilson058d88c2016-08-15 10:49:06 +01001820 struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
Chris Wilson05394f32010-11-08 19:18:58 +00001821 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001822 struct drm_i915_private *dev_priv = to_i915(dev);
1823 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001824 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Chris Wilson058d88c2016-08-15 10:49:06 +01001825 struct i915_vma *vma;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001826 pgoff_t page_offset;
Chris Wilson82118872016-08-18 17:17:05 +01001827 unsigned int flags;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001828 int ret;
Paulo Zanonif65c9162013-11-27 18:20:34 -02001829
Jesse Barnesde151cf2008-11-12 10:03:55 -08001830 /* We don't use vmf->pgoff since that has the fake offset */
Jan Kara1a29d852016-12-14 15:07:01 -08001831 page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001832
Chris Wilsondb53a302011-02-03 11:57:46 +00001833 trace_i915_gem_object_fault(obj, page_offset, true, write);
1834
Chris Wilson6e4930f2014-02-07 18:37:06 -02001835 /* Try to flush the object off the GPU first without holding the lock.
Chris Wilsonb8f90962016-08-05 10:14:07 +01001836 * Upon acquiring the lock, we will perform our sanity checks and then
Chris Wilson6e4930f2014-02-07 18:37:06 -02001837 * repeat the flush holding the lock in the normal manner to catch cases
1838 * where we are gazumped.
1839 */
Chris Wilsone95433c2016-10-28 13:58:27 +01001840 ret = i915_gem_object_wait(obj,
1841 I915_WAIT_INTERRUPTIBLE,
1842 MAX_SCHEDULE_TIMEOUT,
1843 NULL);
Chris Wilson6e4930f2014-02-07 18:37:06 -02001844 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001845 goto err;
1846
Chris Wilson40e62d52016-10-28 13:58:41 +01001847 ret = i915_gem_object_pin_pages(obj);
1848 if (ret)
1849 goto err;
1850
Chris Wilsonb8f90962016-08-05 10:14:07 +01001851 intel_runtime_pm_get(dev_priv);
1852
1853 ret = i915_mutex_lock_interruptible(dev);
1854 if (ret)
1855 goto err_rpm;
Chris Wilson6e4930f2014-02-07 18:37:06 -02001856
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001857 /* Access to snoopable pages through the GTT is incoherent. */
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00001858 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) {
Chris Wilsonddeff6e2014-05-28 16:16:41 +01001859 ret = -EFAULT;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001860 goto err_unlock;
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001861 }
1862
Chris Wilson82118872016-08-18 17:17:05 +01001863 /* If the object is smaller than a couple of partial vma, it is
1864 * not worth only creating a single partial vma - we may as well
1865 * clear enough space for the full object.
1866 */
1867 flags = PIN_MAPPABLE;
1868 if (obj->base.size > 2 * MIN_CHUNK_PAGES << PAGE_SHIFT)
1869 flags |= PIN_NONBLOCK | PIN_NONFAULT;
1870
Chris Wilsona61007a2016-08-18 17:17:02 +01001871 /* Now pin it into the GTT as needed */
Chris Wilson82118872016-08-18 17:17:05 +01001872 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, flags);
Chris Wilsona61007a2016-08-18 17:17:02 +01001873 if (IS_ERR(vma)) {
Chris Wilsona61007a2016-08-18 17:17:02 +01001874 /* Use a partial view if it is bigger than available space */
Chris Wilson2d4281b2017-01-10 09:56:32 +00001875 struct i915_ggtt_view view =
Chris Wilson8201c1f2017-01-10 09:56:33 +00001876 compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES);
Chris Wilsonaa136d92016-08-18 17:17:03 +01001877
Chris Wilson50349242016-08-18 17:17:04 +01001878 /* Userspace is now writing through an untracked VMA, abandon
1879 * all hope that the hardware is able to track future writes.
1880 */
1881 obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
1882
Chris Wilsona61007a2016-08-18 17:17:02 +01001883 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE);
1884 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001885 if (IS_ERR(vma)) {
1886 ret = PTR_ERR(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001887 goto err_unlock;
Chris Wilson058d88c2016-08-15 10:49:06 +01001888 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001889
Chris Wilsonc9839302012-11-20 10:45:17 +00001890 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1891 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001892 goto err_unpin;
Chris Wilsonc9839302012-11-20 10:45:17 +00001893
Chris Wilson49ef5292016-08-18 17:17:00 +01001894 ret = i915_vma_get_fence(vma);
Chris Wilsonc9839302012-11-20 10:45:17 +00001895 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001896 goto err_unpin;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001897
Chris Wilson275f0392016-10-24 13:42:14 +01001898 /* Mark as being mmapped into userspace for later revocation */
Chris Wilson9c870d02016-10-24 13:42:15 +01001899 assert_rpm_wakelock_held(dev_priv);
Chris Wilson275f0392016-10-24 13:42:14 +01001900 if (list_empty(&obj->userfault_link))
1901 list_add(&obj->userfault_link, &dev_priv->mm.userfault_list);
Chris Wilson275f0392016-10-24 13:42:14 +01001902
Chris Wilsonb90b91d2014-06-10 12:14:40 +01001903 /* Finally, remap it using the new GTT offset */
Chris Wilsonc58305a2016-08-19 16:54:28 +01001904 ret = remap_io_mapping(area,
Chris Wilson8bab11932017-01-14 00:28:25 +00001905 area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT),
Chris Wilsonc58305a2016-08-19 16:54:28 +01001906 (ggtt->mappable_base + vma->node.start) >> PAGE_SHIFT,
1907 min_t(u64, vma->size, area->vm_end - area->vm_start),
1908 &ggtt->mappable);
Chris Wilsona61007a2016-08-18 17:17:02 +01001909
Chris Wilsonb8f90962016-08-05 10:14:07 +01001910err_unpin:
Chris Wilson058d88c2016-08-15 10:49:06 +01001911 __i915_vma_unpin(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001912err_unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001913 mutex_unlock(&dev->struct_mutex);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001914err_rpm:
1915 intel_runtime_pm_put(dev_priv);
Chris Wilson40e62d52016-10-28 13:58:41 +01001916 i915_gem_object_unpin_pages(obj);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001917err:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001918 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001919 case -EIO:
Daniel Vetter2232f032014-09-04 09:36:18 +02001920 /*
1921 * We eat errors when the gpu is terminally wedged to avoid
1922 * userspace unduly crashing (gl has no provisions for mmaps to
1923 * fail). But any other -EIO isn't ours (e.g. swap in failure)
1924 * and so needs to be reported.
1925 */
1926 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
Paulo Zanonif65c9162013-11-27 18:20:34 -02001927 ret = VM_FAULT_SIGBUS;
1928 break;
1929 }
Chris Wilson045e7692010-11-07 09:18:22 +00001930 case -EAGAIN:
Daniel Vetter571c6082013-09-12 17:57:28 +02001931 /*
1932 * EAGAIN means the gpu is hung and we'll wait for the error
1933 * handler to reset everything when re-faulting in
1934 * i915_mutex_lock_interruptible.
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001935 */
Chris Wilsonc7150892009-09-23 00:43:56 +01001936 case 0:
1937 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00001938 case -EINTR:
Dmitry Rogozhkine79e0fe2012-10-03 17:15:26 +03001939 case -EBUSY:
1940 /*
1941 * EBUSY is ok: this just means that another thread
1942 * already did the job.
1943 */
Paulo Zanonif65c9162013-11-27 18:20:34 -02001944 ret = VM_FAULT_NOPAGE;
1945 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001946 case -ENOMEM:
Paulo Zanonif65c9162013-11-27 18:20:34 -02001947 ret = VM_FAULT_OOM;
1948 break;
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02001949 case -ENOSPC:
Chris Wilson45d67812014-01-31 11:34:57 +00001950 case -EFAULT:
Paulo Zanonif65c9162013-11-27 18:20:34 -02001951 ret = VM_FAULT_SIGBUS;
1952 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001953 default:
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02001954 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
Paulo Zanonif65c9162013-11-27 18:20:34 -02001955 ret = VM_FAULT_SIGBUS;
1956 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001957 }
Paulo Zanonif65c9162013-11-27 18:20:34 -02001958 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001959}
1960
1961/**
Chris Wilson901782b2009-07-10 08:18:50 +01001962 * i915_gem_release_mmap - remove physical page mappings
1963 * @obj: obj in question
1964 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001965 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001966 * relinquish ownership of the pages back to the system.
1967 *
1968 * It is vital that we remove the page mapping if we have mapped a tiled
1969 * object through the GTT and then lose the fence register due to
1970 * resource pressure. Similarly if the object has been moved out of the
1971 * aperture, than pages mapped into userspace must be revoked. Removing the
1972 * mapping will then trigger a page fault on the next user access, allowing
1973 * fixup by i915_gem_fault().
1974 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001975void
Chris Wilson05394f32010-11-08 19:18:58 +00001976i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01001977{
Chris Wilson275f0392016-10-24 13:42:14 +01001978 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson275f0392016-10-24 13:42:14 +01001979
Chris Wilson349f2cc2016-04-13 17:35:12 +01001980 /* Serialisation between user GTT access and our code depends upon
1981 * revoking the CPU's PTE whilst the mutex is held. The next user
1982 * pagefault then has to wait until we release the mutex.
Chris Wilson9c870d02016-10-24 13:42:15 +01001983 *
1984 * Note that RPM complicates somewhat by adding an additional
1985 * requirement that operations to the GGTT be made holding the RPM
1986 * wakeref.
Chris Wilson349f2cc2016-04-13 17:35:12 +01001987 */
Chris Wilson275f0392016-10-24 13:42:14 +01001988 lockdep_assert_held(&i915->drm.struct_mutex);
Chris Wilson9c870d02016-10-24 13:42:15 +01001989 intel_runtime_pm_get(i915);
Chris Wilson349f2cc2016-04-13 17:35:12 +01001990
Chris Wilson3594a3e2016-10-24 13:42:16 +01001991 if (list_empty(&obj->userfault_link))
Chris Wilson9c870d02016-10-24 13:42:15 +01001992 goto out;
Chris Wilson901782b2009-07-10 08:18:50 +01001993
Chris Wilson3594a3e2016-10-24 13:42:16 +01001994 list_del_init(&obj->userfault_link);
David Herrmann6796cb12014-01-03 14:24:19 +01001995 drm_vma_node_unmap(&obj->base.vma_node,
1996 obj->base.dev->anon_inode->i_mapping);
Chris Wilson349f2cc2016-04-13 17:35:12 +01001997
1998 /* Ensure that the CPU's PTE are revoked and there are not outstanding
1999 * memory transactions from userspace before we return. The TLB
2000 * flushing implied above by changing the PTE above *should* be
2001 * sufficient, an extra barrier here just provides us with a bit
2002 * of paranoid documentation about our requirement to serialise
2003 * memory writes before touching registers / GSM.
2004 */
2005 wmb();
Chris Wilson9c870d02016-10-24 13:42:15 +01002006
2007out:
2008 intel_runtime_pm_put(i915);
Chris Wilson901782b2009-07-10 08:18:50 +01002009}
2010
Chris Wilson7c108fd2016-10-24 13:42:18 +01002011void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv)
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002012{
Chris Wilson3594a3e2016-10-24 13:42:16 +01002013 struct drm_i915_gem_object *obj, *on;
Chris Wilson7c108fd2016-10-24 13:42:18 +01002014 int i;
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002015
Chris Wilson3594a3e2016-10-24 13:42:16 +01002016 /*
2017 * Only called during RPM suspend. All users of the userfault_list
2018 * must be holding an RPM wakeref to ensure that this can not
2019 * run concurrently with themselves (and use the struct_mutex for
2020 * protection between themselves).
2021 */
2022
2023 list_for_each_entry_safe(obj, on,
2024 &dev_priv->mm.userfault_list, userfault_link) {
Chris Wilson275f0392016-10-24 13:42:14 +01002025 list_del_init(&obj->userfault_link);
Chris Wilson275f0392016-10-24 13:42:14 +01002026 drm_vma_node_unmap(&obj->base.vma_node,
2027 obj->base.dev->anon_inode->i_mapping);
Chris Wilson275f0392016-10-24 13:42:14 +01002028 }
Chris Wilson7c108fd2016-10-24 13:42:18 +01002029
2030 /* The fence will be lost when the device powers down. If any were
2031 * in use by hardware (i.e. they are pinned), we should not be powering
2032 * down! All other fences will be reacquired by the user upon waking.
2033 */
2034 for (i = 0; i < dev_priv->num_fence_regs; i++) {
2035 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
2036
Chris Wilsone0ec3ec2017-02-03 12:57:17 +00002037 /* Ideally we want to assert that the fence register is not
2038 * live at this point (i.e. that no piece of code will be
2039 * trying to write through fence + GTT, as that both violates
2040 * our tracking of activity and associated locking/barriers,
2041 * but also is illegal given that the hw is powered down).
2042 *
2043 * Previously we used reg->pin_count as a "liveness" indicator.
2044 * That is not sufficient, and we need a more fine-grained
2045 * tool if we want to have a sanity check here.
2046 */
Chris Wilson7c108fd2016-10-24 13:42:18 +01002047
2048 if (!reg->vma)
2049 continue;
2050
2051 GEM_BUG_ON(!list_empty(&reg->vma->obj->userfault_link));
2052 reg->dirty = true;
2053 }
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002054}
2055
Chris Wilsond8cb5082012-08-11 15:41:03 +01002056static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
2057{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002058 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002059 int err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002060
Chris Wilsonf3f61842016-08-05 10:14:14 +01002061 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002062 if (likely(!err))
Chris Wilsonf3f61842016-08-05 10:14:14 +01002063 return 0;
Daniel Vetterda494d72012-12-20 15:11:16 +01002064
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002065 /* Attempt to reap some mmap space from dead objects */
2066 do {
2067 err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
2068 if (err)
2069 break;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002070
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002071 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002072 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002073 if (!err)
2074 break;
2075
2076 } while (flush_delayed_work(&dev_priv->gt.retire_work));
Daniel Vetterda494d72012-12-20 15:11:16 +01002077
Chris Wilsonf3f61842016-08-05 10:14:14 +01002078 return err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002079}
2080
2081static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
2082{
Chris Wilsond8cb5082012-08-11 15:41:03 +01002083 drm_gem_free_mmap_offset(&obj->base);
2084}
2085
Dave Airlieda6b51d2014-12-24 13:11:17 +10002086int
Dave Airlieff72145b2011-02-07 12:16:14 +10002087i915_gem_mmap_gtt(struct drm_file *file,
2088 struct drm_device *dev,
Dave Airlieda6b51d2014-12-24 13:11:17 +10002089 uint32_t handle,
Dave Airlieff72145b2011-02-07 12:16:14 +10002090 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002091{
Chris Wilson05394f32010-11-08 19:18:58 +00002092 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002093 int ret;
2094
Chris Wilson03ac0642016-07-20 13:31:51 +01002095 obj = i915_gem_object_lookup(file, handle);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002096 if (!obj)
2097 return -ENOENT;
Chris Wilsonab182822009-09-22 18:46:17 +01002098
Chris Wilsond8cb5082012-08-11 15:41:03 +01002099 ret = i915_gem_object_create_mmap_offset(obj);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002100 if (ret == 0)
2101 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002102
Chris Wilsonf0cd5182016-10-28 13:58:43 +01002103 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002104 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002105}
2106
Dave Airlieff72145b2011-02-07 12:16:14 +10002107/**
2108 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
2109 * @dev: DRM device
2110 * @data: GTT mapping ioctl data
2111 * @file: GEM object info
2112 *
2113 * Simply returns the fake offset to userspace so it can mmap it.
2114 * The mmap call will end up in drm_gem_mmap(), which will set things
2115 * up so we can get faults in the handler above.
2116 *
2117 * The fault handler will take care of binding the object into the GTT
2118 * (since it may have been evicted to make room for something), allocating
2119 * a fence register, and mapping the appropriate aperture address into
2120 * userspace.
2121 */
2122int
2123i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
2124 struct drm_file *file)
2125{
2126 struct drm_i915_gem_mmap_gtt *args = data;
2127
Dave Airlieda6b51d2014-12-24 13:11:17 +10002128 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
Dave Airlieff72145b2011-02-07 12:16:14 +10002129}
2130
Daniel Vetter225067e2012-08-20 10:23:20 +02002131/* Immediately discard the backing storage */
2132static void
2133i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01002134{
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002135 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02002136
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002137 if (obj->base.filp == NULL)
2138 return;
2139
Daniel Vetter225067e2012-08-20 10:23:20 +02002140 /* Our goal here is to return as much of the memory as
2141 * is possible back to the system as we are called from OOM.
2142 * To do this we must instruct the shmfs to drop all of its
2143 * backing pages, *now*.
Chris Wilsone5281cc2010-10-28 13:45:36 +01002144 */
Chris Wilson55372522014-03-25 13:23:06 +00002145 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002146 obj->mm.madv = __I915_MADV_PURGED;
Chris Wilsone5281cc2010-10-28 13:45:36 +01002147}
Chris Wilsone5281cc2010-10-28 13:45:36 +01002148
Chris Wilson55372522014-03-25 13:23:06 +00002149/* Try to discard unwanted pages */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002150void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
Daniel Vetter225067e2012-08-20 10:23:20 +02002151{
Chris Wilson55372522014-03-25 13:23:06 +00002152 struct address_space *mapping;
2153
Chris Wilson1233e2d2016-10-28 13:58:37 +01002154 lockdep_assert_held(&obj->mm.lock);
2155 GEM_BUG_ON(obj->mm.pages);
2156
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002157 switch (obj->mm.madv) {
Chris Wilson55372522014-03-25 13:23:06 +00002158 case I915_MADV_DONTNEED:
2159 i915_gem_object_truncate(obj);
2160 case __I915_MADV_PURGED:
2161 return;
2162 }
2163
2164 if (obj->base.filp == NULL)
2165 return;
2166
Al Viro93c76a32015-12-04 23:45:44 -05002167 mapping = obj->base.filp->f_mapping,
Chris Wilson55372522014-03-25 13:23:06 +00002168 invalidate_mapping_pages(mapping, 0, (loff_t)-1);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002169}
2170
Chris Wilson5cdf5882010-09-27 15:51:07 +01002171static void
Chris Wilson03ac84f2016-10-28 13:58:36 +01002172i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj,
2173 struct sg_table *pages)
Eric Anholt673a3942008-07-30 12:06:12 -07002174{
Dave Gordon85d12252016-05-20 11:54:06 +01002175 struct sgt_iter sgt_iter;
2176 struct page *page;
Daniel Vetter1286ff72012-05-10 15:25:09 +02002177
Chris Wilsone5facdf2016-12-23 14:57:57 +00002178 __i915_gem_object_release_shmem(obj, pages, true);
Eric Anholt856fa192009-03-19 14:10:50 -07002179
Chris Wilson03ac84f2016-10-28 13:58:36 +01002180 i915_gem_gtt_finish_pages(obj, pages);
Imre Deake2273302015-07-09 12:59:05 +03002181
Daniel Vetter6dacfd22011-09-12 21:30:02 +02002182 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002183 i915_gem_object_save_bit_17_swizzle(obj, pages);
Eric Anholt280b7132009-03-12 16:56:27 -07002184
Chris Wilson03ac84f2016-10-28 13:58:36 +01002185 for_each_sgt_page(page, sgt_iter, pages) {
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002186 if (obj->mm.dirty)
Chris Wilson9da3da62012-06-01 15:20:22 +01002187 set_page_dirty(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002188
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002189 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson9da3da62012-06-01 15:20:22 +01002190 mark_page_accessed(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002191
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002192 put_page(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002193 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002194 obj->mm.dirty = false;
Eric Anholt673a3942008-07-30 12:06:12 -07002195
Chris Wilson03ac84f2016-10-28 13:58:36 +01002196 sg_free_table(pages);
2197 kfree(pages);
Chris Wilson37e680a2012-06-07 15:38:42 +01002198}
2199
Chris Wilson96d77632016-10-28 13:58:33 +01002200static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
2201{
2202 struct radix_tree_iter iter;
2203 void **slot;
2204
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002205 radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
2206 radix_tree_delete(&obj->mm.get_page.radix, iter.index);
Chris Wilson96d77632016-10-28 13:58:33 +01002207}
2208
Chris Wilson548625e2016-11-01 12:11:34 +00002209void __i915_gem_object_put_pages(struct drm_i915_gem_object *obj,
2210 enum i915_mm_subclass subclass)
Chris Wilson37e680a2012-06-07 15:38:42 +01002211{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002212 struct sg_table *pages;
Chris Wilson37e680a2012-06-07 15:38:42 +01002213
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002214 if (i915_gem_object_has_pinned_pages(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002215 return;
Chris Wilsona5570172012-09-04 21:02:54 +01002216
Chris Wilson15717de2016-08-04 07:52:26 +01002217 GEM_BUG_ON(obj->bind_count);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002218 if (!READ_ONCE(obj->mm.pages))
2219 return;
2220
2221 /* May be called by shrinker from within get_pages() (on another bo) */
Chris Wilson548625e2016-11-01 12:11:34 +00002222 mutex_lock_nested(&obj->mm.lock, subclass);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002223 if (unlikely(atomic_read(&obj->mm.pages_pin_count)))
2224 goto unlock;
Ben Widawsky3e123022013-07-31 17:00:04 -07002225
Chris Wilsona2165e32012-12-03 11:49:00 +00002226 /* ->put_pages might need to allocate memory for the bit17 swizzle
2227 * array, hence protect them from being reaped by removing them from gtt
2228 * lists early. */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002229 pages = fetch_and_zero(&obj->mm.pages);
2230 GEM_BUG_ON(!pages);
Chris Wilsona2165e32012-12-03 11:49:00 +00002231
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002232 if (obj->mm.mapping) {
Chris Wilson4b30cb22016-08-18 17:16:42 +01002233 void *ptr;
2234
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002235 ptr = ptr_mask_bits(obj->mm.mapping);
Chris Wilson4b30cb22016-08-18 17:16:42 +01002236 if (is_vmalloc_addr(ptr))
2237 vunmap(ptr);
Chris Wilsonfb8621d2016-04-08 12:11:14 +01002238 else
Chris Wilson4b30cb22016-08-18 17:16:42 +01002239 kunmap(kmap_to_page(ptr));
2240
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002241 obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002242 }
2243
Chris Wilson96d77632016-10-28 13:58:33 +01002244 __i915_gem_object_reset_page_iter(obj);
2245
Chris Wilson03ac84f2016-10-28 13:58:36 +01002246 obj->ops->put_pages(obj, pages);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002247unlock:
2248 mutex_unlock(&obj->mm.lock);
Chris Wilson6c085a72012-08-20 11:40:46 +02002249}
2250
Chris Wilson935a2f72017-02-13 17:15:13 +00002251static bool i915_sg_trim(struct sg_table *orig_st)
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002252{
2253 struct sg_table new_st;
2254 struct scatterlist *sg, *new_sg;
2255 unsigned int i;
2256
2257 if (orig_st->nents == orig_st->orig_nents)
Chris Wilson935a2f72017-02-13 17:15:13 +00002258 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002259
Chris Wilson8bfc478f2016-12-23 14:57:58 +00002260 if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN))
Chris Wilson935a2f72017-02-13 17:15:13 +00002261 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002262
2263 new_sg = new_st.sgl;
2264 for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
2265 sg_set_page(new_sg, sg_page(sg), sg->length, 0);
2266 /* called before being DMA mapped, no need to copy sg->dma_* */
2267 new_sg = sg_next(new_sg);
2268 }
Chris Wilsonc2dc6cc2016-12-19 12:43:46 +00002269 GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002270
2271 sg_free_table(orig_st);
2272
2273 *orig_st = new_st;
Chris Wilson935a2f72017-02-13 17:15:13 +00002274 return true;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002275}
2276
Chris Wilson03ac84f2016-10-28 13:58:36 +01002277static struct sg_table *
Chris Wilson6c085a72012-08-20 11:40:46 +02002278i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002279{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002280 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsond766ef52016-12-19 12:43:45 +00002281 const unsigned long page_count = obj->base.size / PAGE_SIZE;
2282 unsigned long i;
Eric Anholt673a3942008-07-30 12:06:12 -07002283 struct address_space *mapping;
Chris Wilson9da3da62012-06-01 15:20:22 +01002284 struct sg_table *st;
2285 struct scatterlist *sg;
Dave Gordon85d12252016-05-20 11:54:06 +01002286 struct sgt_iter sgt_iter;
Eric Anholt673a3942008-07-30 12:06:12 -07002287 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002288 unsigned long last_pfn = 0; /* suppress gcc warning */
Chris Wilson4ff340f02016-10-18 13:02:50 +01002289 unsigned int max_segment;
Imre Deake2273302015-07-09 12:59:05 +03002290 int ret;
Chris Wilson6c085a72012-08-20 11:40:46 +02002291 gfp_t gfp;
Eric Anholt673a3942008-07-30 12:06:12 -07002292
Chris Wilson6c085a72012-08-20 11:40:46 +02002293 /* Assert that the object is not currently in any GPU domain. As it
2294 * wasn't in the GTT, there shouldn't be any way it could have been in
2295 * a GPU cache
2296 */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002297 GEM_BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2298 GEM_BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
Chris Wilson6c085a72012-08-20 11:40:46 +02002299
Konrad Rzeszutek Wilk7453c542016-12-20 10:02:02 -05002300 max_segment = swiotlb_max_segment();
Chris Wilson871dfbd2016-10-11 09:20:21 +01002301 if (!max_segment)
Chris Wilson4ff340f02016-10-18 13:02:50 +01002302 max_segment = rounddown(UINT_MAX, PAGE_SIZE);
Chris Wilson871dfbd2016-10-11 09:20:21 +01002303
Chris Wilson9da3da62012-06-01 15:20:22 +01002304 st = kmalloc(sizeof(*st), GFP_KERNEL);
2305 if (st == NULL)
Chris Wilson03ac84f2016-10-28 13:58:36 +01002306 return ERR_PTR(-ENOMEM);
Eric Anholt673a3942008-07-30 12:06:12 -07002307
Chris Wilsond766ef52016-12-19 12:43:45 +00002308rebuild_st:
Chris Wilson9da3da62012-06-01 15:20:22 +01002309 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
Chris Wilson9da3da62012-06-01 15:20:22 +01002310 kfree(st);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002311 return ERR_PTR(-ENOMEM);
Chris Wilson9da3da62012-06-01 15:20:22 +01002312 }
2313
2314 /* Get the list of pages out of our struct file. They'll be pinned
2315 * at this point until we release them.
2316 *
2317 * Fail silently without starting the shrinker
2318 */
Al Viro93c76a32015-12-04 23:45:44 -05002319 mapping = obj->base.filp->f_mapping;
Michal Hockoc62d2552015-11-06 16:28:49 -08002320 gfp = mapping_gfp_constraint(mapping, ~(__GFP_IO | __GFP_RECLAIM));
Mel Gormand0164ad2015-11-06 16:28:21 -08002321 gfp |= __GFP_NORETRY | __GFP_NOWARN;
Imre Deak90797e62013-02-18 19:28:03 +02002322 sg = st->sgl;
2323 st->nents = 0;
2324 for (i = 0; i < page_count; i++) {
Chris Wilson6c085a72012-08-20 11:40:46 +02002325 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
2326 if (IS_ERR(page)) {
Chris Wilson21ab4e72014-09-09 11:16:08 +01002327 i915_gem_shrink(dev_priv,
2328 page_count,
2329 I915_SHRINK_BOUND |
2330 I915_SHRINK_UNBOUND |
2331 I915_SHRINK_PURGEABLE);
Chris Wilson6c085a72012-08-20 11:40:46 +02002332 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
2333 }
2334 if (IS_ERR(page)) {
2335 /* We've tried hard to allocate the memory by reaping
2336 * our own buffer, now let the real VM do its job and
2337 * go down in flames if truly OOM.
2338 */
David Herrmannf461d1be22014-05-25 14:34:10 +02002339 page = shmem_read_mapping_page(mapping, i);
Imre Deake2273302015-07-09 12:59:05 +03002340 if (IS_ERR(page)) {
2341 ret = PTR_ERR(page);
Chris Wilsonb17993b2016-11-14 11:29:30 +00002342 goto err_sg;
Imre Deake2273302015-07-09 12:59:05 +03002343 }
Chris Wilson6c085a72012-08-20 11:40:46 +02002344 }
Chris Wilson871dfbd2016-10-11 09:20:21 +01002345 if (!i ||
2346 sg->length >= max_segment ||
2347 page_to_pfn(page) != last_pfn + 1) {
Imre Deak90797e62013-02-18 19:28:03 +02002348 if (i)
2349 sg = sg_next(sg);
2350 st->nents++;
2351 sg_set_page(sg, page, PAGE_SIZE, 0);
2352 } else {
2353 sg->length += PAGE_SIZE;
2354 }
2355 last_pfn = page_to_pfn(page);
Daniel Vetter3bbbe702013-10-07 17:15:45 -03002356
2357 /* Check that the i965g/gm workaround works. */
2358 WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
Eric Anholt673a3942008-07-30 12:06:12 -07002359 }
Chris Wilson871dfbd2016-10-11 09:20:21 +01002360 if (sg) /* loop terminated early; short sg table */
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002361 sg_mark_end(sg);
Chris Wilson74ce6b62012-10-19 15:51:06 +01002362
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002363 /* Trim unused sg entries to avoid wasting memory. */
2364 i915_sg_trim(st);
2365
Chris Wilson03ac84f2016-10-28 13:58:36 +01002366 ret = i915_gem_gtt_prepare_pages(obj, st);
Chris Wilsond766ef52016-12-19 12:43:45 +00002367 if (ret) {
2368 /* DMA remapping failed? One possible cause is that
2369 * it could not reserve enough large entries, asking
2370 * for PAGE_SIZE chunks instead may be helpful.
2371 */
2372 if (max_segment > PAGE_SIZE) {
2373 for_each_sgt_page(page, sgt_iter, st)
2374 put_page(page);
2375 sg_free_table(st);
2376
2377 max_segment = PAGE_SIZE;
2378 goto rebuild_st;
2379 } else {
2380 dev_warn(&dev_priv->drm.pdev->dev,
2381 "Failed to DMA remap %lu pages\n",
2382 page_count);
2383 goto err_pages;
2384 }
2385 }
Imre Deake2273302015-07-09 12:59:05 +03002386
Eric Anholt673a3942008-07-30 12:06:12 -07002387 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002388 i915_gem_object_do_bit_17_swizzle(obj, st);
Eric Anholt673a3942008-07-30 12:06:12 -07002389
Chris Wilson03ac84f2016-10-28 13:58:36 +01002390 return st;
Eric Anholt673a3942008-07-30 12:06:12 -07002391
Chris Wilsonb17993b2016-11-14 11:29:30 +00002392err_sg:
Imre Deak90797e62013-02-18 19:28:03 +02002393 sg_mark_end(sg);
Chris Wilsonb17993b2016-11-14 11:29:30 +00002394err_pages:
Dave Gordon85d12252016-05-20 11:54:06 +01002395 for_each_sgt_page(page, sgt_iter, st)
2396 put_page(page);
Chris Wilson9da3da62012-06-01 15:20:22 +01002397 sg_free_table(st);
2398 kfree(st);
Chris Wilson0820baf2014-03-25 13:23:03 +00002399
2400 /* shmemfs first checks if there is enough memory to allocate the page
2401 * and reports ENOSPC should there be insufficient, along with the usual
2402 * ENOMEM for a genuine allocation failure.
2403 *
2404 * We use ENOSPC in our driver to mean that we have run out of aperture
2405 * space and so want to translate the error from shmemfs back to our
2406 * usual understanding of ENOMEM.
2407 */
Imre Deake2273302015-07-09 12:59:05 +03002408 if (ret == -ENOSPC)
2409 ret = -ENOMEM;
2410
Chris Wilson03ac84f2016-10-28 13:58:36 +01002411 return ERR_PTR(ret);
2412}
2413
2414void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
2415 struct sg_table *pages)
2416{
Chris Wilson1233e2d2016-10-28 13:58:37 +01002417 lockdep_assert_held(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002418
2419 obj->mm.get_page.sg_pos = pages->sgl;
2420 obj->mm.get_page.sg_idx = 0;
2421
2422 obj->mm.pages = pages;
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002423
2424 if (i915_gem_object_is_tiled(obj) &&
2425 to_i915(obj->base.dev)->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
2426 GEM_BUG_ON(obj->mm.quirked);
2427 __i915_gem_object_pin_pages(obj);
2428 obj->mm.quirked = true;
2429 }
Chris Wilson03ac84f2016-10-28 13:58:36 +01002430}
2431
2432static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
2433{
2434 struct sg_table *pages;
2435
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002436 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2437
Chris Wilson03ac84f2016-10-28 13:58:36 +01002438 if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
2439 DRM_DEBUG("Attempting to obtain a purgeable object\n");
2440 return -EFAULT;
2441 }
2442
2443 pages = obj->ops->get_pages(obj);
2444 if (unlikely(IS_ERR(pages)))
2445 return PTR_ERR(pages);
2446
2447 __i915_gem_object_set_pages(obj, pages);
2448 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002449}
2450
Chris Wilson37e680a2012-06-07 15:38:42 +01002451/* Ensure that the associated pages are gathered from the backing storage
Chris Wilson1233e2d2016-10-28 13:58:37 +01002452 * and pinned into our object. i915_gem_object_pin_pages() may be called
Chris Wilson37e680a2012-06-07 15:38:42 +01002453 * multiple times before they are released by a single call to
Chris Wilson1233e2d2016-10-28 13:58:37 +01002454 * i915_gem_object_unpin_pages() - once the pages are no longer referenced
Chris Wilson37e680a2012-06-07 15:38:42 +01002455 * either as a result of memory pressure (reaping pages under the shrinker)
2456 * or as the object is itself released.
2457 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002458int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
Chris Wilson37e680a2012-06-07 15:38:42 +01002459{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002460 int err;
Chris Wilson37e680a2012-06-07 15:38:42 +01002461
Chris Wilson1233e2d2016-10-28 13:58:37 +01002462 err = mutex_lock_interruptible(&obj->mm.lock);
2463 if (err)
2464 return err;
Chris Wilson4c7d62c2016-10-28 13:58:32 +01002465
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002466 if (unlikely(!obj->mm.pages)) {
2467 err = ____i915_gem_object_get_pages(obj);
2468 if (err)
2469 goto unlock;
2470
2471 smp_mb__before_atomic();
Chris Wilson1233e2d2016-10-28 13:58:37 +01002472 }
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002473 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson43e28f02013-01-08 10:53:09 +00002474
Chris Wilson1233e2d2016-10-28 13:58:37 +01002475unlock:
2476 mutex_unlock(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002477 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002478}
2479
Dave Gordondd6034c2016-05-20 11:54:04 +01002480/* The 'mapping' part of i915_gem_object_pin_map() below */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002481static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
2482 enum i915_map_type type)
Dave Gordondd6034c2016-05-20 11:54:04 +01002483{
2484 unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002485 struct sg_table *sgt = obj->mm.pages;
Dave Gordon85d12252016-05-20 11:54:06 +01002486 struct sgt_iter sgt_iter;
2487 struct page *page;
Dave Gordonb338fa42016-05-20 11:54:05 +01002488 struct page *stack_pages[32];
2489 struct page **pages = stack_pages;
Dave Gordondd6034c2016-05-20 11:54:04 +01002490 unsigned long i = 0;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002491 pgprot_t pgprot;
Dave Gordondd6034c2016-05-20 11:54:04 +01002492 void *addr;
2493
2494 /* A single page can always be kmapped */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002495 if (n_pages == 1 && type == I915_MAP_WB)
Dave Gordondd6034c2016-05-20 11:54:04 +01002496 return kmap(sg_page(sgt->sgl));
2497
Dave Gordonb338fa42016-05-20 11:54:05 +01002498 if (n_pages > ARRAY_SIZE(stack_pages)) {
2499 /* Too big for stack -- allocate temporary array instead */
2500 pages = drm_malloc_gfp(n_pages, sizeof(*pages), GFP_TEMPORARY);
2501 if (!pages)
2502 return NULL;
2503 }
Dave Gordondd6034c2016-05-20 11:54:04 +01002504
Dave Gordon85d12252016-05-20 11:54:06 +01002505 for_each_sgt_page(page, sgt_iter, sgt)
2506 pages[i++] = page;
Dave Gordondd6034c2016-05-20 11:54:04 +01002507
2508 /* Check that we have the expected number of pages */
2509 GEM_BUG_ON(i != n_pages);
2510
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002511 switch (type) {
2512 case I915_MAP_WB:
2513 pgprot = PAGE_KERNEL;
2514 break;
2515 case I915_MAP_WC:
2516 pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
2517 break;
2518 }
2519 addr = vmap(pages, n_pages, 0, pgprot);
Dave Gordondd6034c2016-05-20 11:54:04 +01002520
Dave Gordonb338fa42016-05-20 11:54:05 +01002521 if (pages != stack_pages)
2522 drm_free_large(pages);
Dave Gordondd6034c2016-05-20 11:54:04 +01002523
2524 return addr;
2525}
2526
2527/* get, pin, and map the pages of the object into kernel space */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002528void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
2529 enum i915_map_type type)
Chris Wilson0a798eb2016-04-08 12:11:11 +01002530{
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002531 enum i915_map_type has_type;
2532 bool pinned;
2533 void *ptr;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002534 int ret;
2535
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002536 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
Chris Wilson0a798eb2016-04-08 12:11:11 +01002537
Chris Wilson1233e2d2016-10-28 13:58:37 +01002538 ret = mutex_lock_interruptible(&obj->mm.lock);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002539 if (ret)
2540 return ERR_PTR(ret);
2541
Chris Wilson1233e2d2016-10-28 13:58:37 +01002542 pinned = true;
2543 if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002544 if (unlikely(!obj->mm.pages)) {
2545 ret = ____i915_gem_object_get_pages(obj);
2546 if (ret)
2547 goto err_unlock;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002548
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002549 smp_mb__before_atomic();
2550 }
2551 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002552 pinned = false;
2553 }
2554 GEM_BUG_ON(!obj->mm.pages);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002555
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002556 ptr = ptr_unpack_bits(obj->mm.mapping, has_type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002557 if (ptr && has_type != type) {
2558 if (pinned) {
2559 ret = -EBUSY;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002560 goto err_unpin;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002561 }
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002562
2563 if (is_vmalloc_addr(ptr))
2564 vunmap(ptr);
2565 else
2566 kunmap(kmap_to_page(ptr));
2567
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002568 ptr = obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002569 }
2570
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002571 if (!ptr) {
2572 ptr = i915_gem_object_map(obj, type);
2573 if (!ptr) {
2574 ret = -ENOMEM;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002575 goto err_unpin;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002576 }
2577
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002578 obj->mm.mapping = ptr_pack_bits(ptr, type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002579 }
2580
Chris Wilson1233e2d2016-10-28 13:58:37 +01002581out_unlock:
2582 mutex_unlock(&obj->mm.lock);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002583 return ptr;
2584
Chris Wilson1233e2d2016-10-28 13:58:37 +01002585err_unpin:
2586 atomic_dec(&obj->mm.pages_pin_count);
2587err_unlock:
2588 ptr = ERR_PTR(ret);
2589 goto out_unlock;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002590}
2591
Chris Wilson60958682016-12-31 11:20:11 +00002592static bool ban_context(const struct i915_gem_context *ctx)
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002593{
Chris Wilson60958682016-12-31 11:20:11 +00002594 return (i915_gem_context_is_bannable(ctx) &&
2595 ctx->ban_score >= CONTEXT_SCORE_BAN_THRESHOLD);
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002596}
2597
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002598static void i915_gem_context_mark_guilty(struct i915_gem_context *ctx)
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002599{
Mika Kuoppalabc1d53c2016-11-16 17:20:34 +02002600 ctx->guilty_count++;
Chris Wilson60958682016-12-31 11:20:11 +00002601 ctx->ban_score += CONTEXT_SCORE_GUILTY;
2602 if (ban_context(ctx))
2603 i915_gem_context_set_banned(ctx);
Mika Kuoppalab083a082016-11-18 15:10:47 +02002604
2605 DRM_DEBUG_DRIVER("context %s marked guilty (score %d) banned? %s\n",
Mika Kuoppalabc1d53c2016-11-16 17:20:34 +02002606 ctx->name, ctx->ban_score,
Chris Wilson60958682016-12-31 11:20:11 +00002607 yesno(i915_gem_context_is_banned(ctx)));
Mika Kuoppalab083a082016-11-18 15:10:47 +02002608
Chris Wilson60958682016-12-31 11:20:11 +00002609 if (!i915_gem_context_is_banned(ctx) || IS_ERR_OR_NULL(ctx->file_priv))
Mika Kuoppalab083a082016-11-18 15:10:47 +02002610 return;
2611
Chris Wilsond9e9da62016-11-22 14:41:18 +00002612 ctx->file_priv->context_bans++;
2613 DRM_DEBUG_DRIVER("client %s has had %d context banned\n",
2614 ctx->name, ctx->file_priv->context_bans);
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002615}
2616
2617static void i915_gem_context_mark_innocent(struct i915_gem_context *ctx)
2618{
Mika Kuoppalabc1d53c2016-11-16 17:20:34 +02002619 ctx->active_count++;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002620}
2621
Chris Wilson8d9fc7f2014-02-25 17:11:23 +02002622struct drm_i915_gem_request *
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002623i915_gem_find_active_request(struct intel_engine_cs *engine)
Chris Wilson9375e442010-09-19 12:21:28 +01002624{
Chris Wilson4db080f2013-12-04 11:37:09 +00002625 struct drm_i915_gem_request *request;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002626
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002627 /* We are called by the error capture and reset at a random
2628 * point in time. In particular, note that neither is crucially
2629 * ordered with an interrupt. After a hang, the GPU is dead and we
2630 * assume that no more writes can happen (we waited long enough for
2631 * all writes that were in transaction to be flushed) - adding an
2632 * extra delay for a recent interrupt is pointless. Hence, we do
2633 * not need an engine->irq_seqno_barrier() before the seqno reads.
2634 */
Chris Wilson73cb9702016-10-28 13:58:46 +01002635 list_for_each_entry(request, &engine->timeline->requests, link) {
Chris Wilson80b204b2016-10-28 13:58:58 +01002636 if (__i915_gem_request_completed(request))
Chris Wilson4db080f2013-12-04 11:37:09 +00002637 continue;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002638
Mika Kuoppala36193ac2017-01-17 17:59:02 +02002639 GEM_BUG_ON(request->engine != engine);
Chris Wilsonc00122f32017-02-12 17:19:58 +00002640 GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
2641 &request->fence.flags));
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002642 return request;
Chris Wilson4db080f2013-12-04 11:37:09 +00002643 }
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002644
2645 return NULL;
2646}
2647
Mika Kuoppalabf2f0432017-01-17 17:59:04 +02002648static bool engine_stalled(struct intel_engine_cs *engine)
2649{
2650 if (!engine->hangcheck.stalled)
2651 return false;
2652
2653 /* Check for possible seqno movement after hang declaration */
2654 if (engine->hangcheck.seqno != intel_engine_get_seqno(engine)) {
2655 DRM_DEBUG_DRIVER("%s pardoned\n", engine->name);
2656 return false;
2657 }
2658
2659 return true;
2660}
2661
Chris Wilson0e178ae2017-01-17 17:59:06 +02002662int i915_gem_reset_prepare(struct drm_i915_private *dev_priv)
Chris Wilson4c965542017-01-17 17:59:01 +02002663{
2664 struct intel_engine_cs *engine;
2665 enum intel_engine_id id;
Chris Wilson0e178ae2017-01-17 17:59:06 +02002666 int err = 0;
Chris Wilson4c965542017-01-17 17:59:01 +02002667
2668 /* Ensure irq handler finishes, and not run again. */
Chris Wilson0e178ae2017-01-17 17:59:06 +02002669 for_each_engine(engine, dev_priv, id) {
2670 struct drm_i915_gem_request *request;
2671
Chris Wilsonfe3288b2017-02-12 17:20:01 +00002672 /* Prevent the signaler thread from updating the request
2673 * state (by calling dma_fence_signal) as we are processing
2674 * the reset. The write from the GPU of the seqno is
2675 * asynchronous and the signaler thread may see a different
2676 * value to us and declare the request complete, even though
2677 * the reset routine have picked that request as the active
2678 * (incomplete) request. This conflict is not handled
2679 * gracefully!
2680 */
2681 kthread_park(engine->breadcrumbs.signaler);
2682
Chris Wilson1f7b8472017-02-08 14:30:33 +00002683 /* Prevent request submission to the hardware until we have
2684 * completed the reset in i915_gem_reset_finish(). If a request
2685 * is completed by one engine, it may then queue a request
2686 * to a second via its engine->irq_tasklet *just* as we are
2687 * calling engine->init_hw() and also writing the ELSP.
2688 * Turning off the engine->irq_tasklet until the reset is over
2689 * prevents the race.
2690 */
Chris Wilson4c965542017-01-17 17:59:01 +02002691 tasklet_kill(&engine->irq_tasklet);
Chris Wilson1d309632017-02-12 17:20:00 +00002692 tasklet_disable(&engine->irq_tasklet);
Chris Wilson4c965542017-01-17 17:59:01 +02002693
Chris Wilson8c12d122017-02-10 18:52:14 +00002694 if (engine->irq_seqno_barrier)
2695 engine->irq_seqno_barrier(engine);
2696
Chris Wilson0e178ae2017-01-17 17:59:06 +02002697 if (engine_stalled(engine)) {
2698 request = i915_gem_find_active_request(engine);
2699 if (request && request->fence.error == -EIO)
2700 err = -EIO; /* Previous reset failed! */
2701 }
2702 }
2703
Chris Wilson4c965542017-01-17 17:59:01 +02002704 i915_gem_revoke_fences(dev_priv);
Chris Wilson0e178ae2017-01-17 17:59:06 +02002705
2706 return err;
Chris Wilson4c965542017-01-17 17:59:01 +02002707}
2708
Mika Kuoppala36193ac2017-01-17 17:59:02 +02002709static void skip_request(struct drm_i915_gem_request *request)
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002710{
Chris Wilson821ed7d2016-09-09 14:11:53 +01002711 void *vaddr = request->ring->vaddr;
2712 u32 head;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002713
Chris Wilson821ed7d2016-09-09 14:11:53 +01002714 /* As this request likely depends on state from the lost
2715 * context, clear out all the user operations leaving the
2716 * breadcrumb at the end (so we get the fence notifications).
2717 */
2718 head = request->head;
2719 if (request->postfix < head) {
2720 memset(vaddr + head, 0, request->ring->size - head);
2721 head = 0;
2722 }
2723 memset(vaddr + head, 0, request->postfix - head);
Chris Wilsonc0d5f322017-01-10 17:22:43 +00002724
2725 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson4db080f2013-12-04 11:37:09 +00002726}
2727
Mika Kuoppala36193ac2017-01-17 17:59:02 +02002728static void engine_skip_context(struct drm_i915_gem_request *request)
2729{
2730 struct intel_engine_cs *engine = request->engine;
2731 struct i915_gem_context *hung_ctx = request->ctx;
2732 struct intel_timeline *timeline;
2733 unsigned long flags;
2734
2735 timeline = i915_gem_context_lookup_timeline(hung_ctx, engine);
2736
2737 spin_lock_irqsave(&engine->timeline->lock, flags);
2738 spin_lock(&timeline->lock);
2739
2740 list_for_each_entry_continue(request, &engine->timeline->requests, link)
2741 if (request->ctx == hung_ctx)
2742 skip_request(request);
2743
2744 list_for_each_entry(request, &timeline->requests, link)
2745 skip_request(request);
2746
2747 spin_unlock(&timeline->lock);
2748 spin_unlock_irqrestore(&engine->timeline->lock, flags);
2749}
2750
Mika Kuoppala61da5362017-01-17 17:59:05 +02002751/* Returns true if the request was guilty of hang */
2752static bool i915_gem_reset_request(struct drm_i915_gem_request *request)
2753{
2754 /* Read once and return the resolution */
2755 const bool guilty = engine_stalled(request->engine);
2756
Mika Kuoppala71895a02017-01-17 17:59:07 +02002757 /* The guilty request will get skipped on a hung engine.
2758 *
2759 * Users of client default contexts do not rely on logical
2760 * state preserved between batches so it is safe to execute
2761 * queued requests following the hang. Non default contexts
2762 * rely on preserved state, so skipping a batch loses the
2763 * evolution of the state and it needs to be considered corrupted.
2764 * Executing more queued batches on top of corrupted state is
2765 * risky. But we take the risk by trying to advance through
2766 * the queued requests in order to make the client behaviour
2767 * more predictable around resets, by not throwing away random
2768 * amount of batches it has prepared for execution. Sophisticated
2769 * clients can use gem_reset_stats_ioctl and dma fence status
2770 * (exported via sync_file info ioctl on explicit fences) to observe
2771 * when it loses the context state and should rebuild accordingly.
2772 *
2773 * The context ban, and ultimately the client ban, mechanism are safety
2774 * valves if client submission ends up resulting in nothing more than
2775 * subsequent hangs.
2776 */
2777
Mika Kuoppala61da5362017-01-17 17:59:05 +02002778 if (guilty) {
2779 i915_gem_context_mark_guilty(request->ctx);
2780 skip_request(request);
2781 } else {
2782 i915_gem_context_mark_innocent(request->ctx);
2783 dma_fence_set_error(&request->fence, -EAGAIN);
2784 }
2785
2786 return guilty;
2787}
2788
Chris Wilson821ed7d2016-09-09 14:11:53 +01002789static void i915_gem_reset_engine(struct intel_engine_cs *engine)
Chris Wilson4db080f2013-12-04 11:37:09 +00002790{
Chris Wilsondcff85c2016-08-05 10:14:11 +01002791 struct drm_i915_gem_request *request;
Chris Wilson608c1a52015-09-03 13:01:40 +01002792
Chris Wilson821ed7d2016-09-09 14:11:53 +01002793 request = i915_gem_find_active_request(engine);
Chris Wilsonc0dcb202017-02-07 15:24:37 +00002794 if (request && i915_gem_reset_request(request)) {
2795 DRM_DEBUG_DRIVER("resetting %s to restart from tail of request 0x%x\n",
2796 engine->name, request->global_seqno);
Chris Wilson821ed7d2016-09-09 14:11:53 +01002797
Chris Wilsonc0dcb202017-02-07 15:24:37 +00002798 /* If this context is now banned, skip all pending requests. */
2799 if (i915_gem_context_is_banned(request->ctx))
2800 engine_skip_context(request);
2801 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01002802
2803 /* Setup the CS to resume from the breadcrumb of the hung request */
2804 engine->reset_hw(engine, request);
Chris Wilson821ed7d2016-09-09 14:11:53 +01002805}
2806
Chris Wilsond8027092017-02-08 14:30:32 +00002807void i915_gem_reset(struct drm_i915_private *dev_priv)
Chris Wilson821ed7d2016-09-09 14:11:53 +01002808{
2809 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302810 enum intel_engine_id id;
Chris Wilson821ed7d2016-09-09 14:11:53 +01002811
Chris Wilson4c7d62c2016-10-28 13:58:32 +01002812 lockdep_assert_held(&dev_priv->drm.struct_mutex);
2813
Chris Wilson821ed7d2016-09-09 14:11:53 +01002814 i915_gem_retire_requests(dev_priv);
2815
Chris Wilson2ae55732017-02-12 17:20:02 +00002816 for_each_engine(engine, dev_priv, id) {
2817 struct i915_gem_context *ctx;
2818
Chris Wilson821ed7d2016-09-09 14:11:53 +01002819 i915_gem_reset_engine(engine);
Chris Wilson2ae55732017-02-12 17:20:02 +00002820 ctx = fetch_and_zero(&engine->last_retired_context);
2821 if (ctx)
2822 engine->context_unpin(engine, ctx);
2823 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01002824
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00002825 i915_gem_restore_fences(dev_priv);
Chris Wilsonf2a91d12016-09-21 14:51:06 +01002826
2827 if (dev_priv->gt.awake) {
2828 intel_sanitize_gt_powersave(dev_priv);
2829 intel_enable_gt_powersave(dev_priv);
2830 if (INTEL_GEN(dev_priv) >= 6)
2831 gen6_rps_busy(dev_priv);
2832 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01002833}
2834
Chris Wilsond8027092017-02-08 14:30:32 +00002835void i915_gem_reset_finish(struct drm_i915_private *dev_priv)
2836{
Chris Wilson1f7b8472017-02-08 14:30:33 +00002837 struct intel_engine_cs *engine;
2838 enum intel_engine_id id;
2839
Chris Wilsond8027092017-02-08 14:30:32 +00002840 lockdep_assert_held(&dev_priv->drm.struct_mutex);
Chris Wilson1f7b8472017-02-08 14:30:33 +00002841
Chris Wilsonfe3288b2017-02-12 17:20:01 +00002842 for_each_engine(engine, dev_priv, id) {
Chris Wilson1f7b8472017-02-08 14:30:33 +00002843 tasklet_enable(&engine->irq_tasklet);
Chris Wilsonfe3288b2017-02-12 17:20:01 +00002844 kthread_unpark(engine->breadcrumbs.signaler);
2845 }
Chris Wilsond8027092017-02-08 14:30:32 +00002846}
2847
Chris Wilson821ed7d2016-09-09 14:11:53 +01002848static void nop_submit_request(struct drm_i915_gem_request *request)
2849{
Chris Wilson3cd94422017-01-10 17:22:45 +00002850 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson3dcf93f72016-11-22 14:41:20 +00002851 i915_gem_request_submit(request);
2852 intel_engine_init_global_seqno(request->engine, request->global_seqno);
Chris Wilson821ed7d2016-09-09 14:11:53 +01002853}
2854
Chris Wilson2a20d6f2017-01-10 17:22:46 +00002855static void engine_set_wedged(struct intel_engine_cs *engine)
Chris Wilson821ed7d2016-09-09 14:11:53 +01002856{
Chris Wilson3cd94422017-01-10 17:22:45 +00002857 struct drm_i915_gem_request *request;
2858 unsigned long flags;
2859
Chris Wilson20e49332016-11-22 14:41:21 +00002860 /* We need to be sure that no thread is running the old callback as
2861 * we install the nop handler (otherwise we would submit a request
2862 * to hardware that will never complete). In order to prevent this
2863 * race, we wait until the machine is idle before making the swap
2864 * (using stop_machine()).
2865 */
Chris Wilson821ed7d2016-09-09 14:11:53 +01002866 engine->submit_request = nop_submit_request;
Chris Wilson70c2a242016-09-09 14:11:46 +01002867
Chris Wilson3cd94422017-01-10 17:22:45 +00002868 /* Mark all executing requests as skipped */
2869 spin_lock_irqsave(&engine->timeline->lock, flags);
2870 list_for_each_entry(request, &engine->timeline->requests, link)
2871 dma_fence_set_error(&request->fence, -EIO);
2872 spin_unlock_irqrestore(&engine->timeline->lock, flags);
2873
Chris Wilsonc4b09302016-07-20 09:21:10 +01002874 /* Mark all pending requests as complete so that any concurrent
2875 * (lockless) lookup doesn't try and wait upon the request as we
2876 * reset it.
2877 */
Chris Wilson73cb9702016-10-28 13:58:46 +01002878 intel_engine_init_global_seqno(engine,
Chris Wilsoncb399ea2016-11-01 10:03:16 +00002879 intel_engine_last_submit(engine));
Chris Wilsonc4b09302016-07-20 09:21:10 +01002880
Ben Widawsky1d62bee2014-01-01 10:15:13 -08002881 /*
Oscar Mateodcb4c122014-11-13 10:28:10 +00002882 * Clear the execlists queue up before freeing the requests, as those
2883 * are the ones that keep the context and ringbuffer backing objects
2884 * pinned in place.
2885 */
Oscar Mateodcb4c122014-11-13 10:28:10 +00002886
Tomas Elf7de1691a2015-10-19 16:32:32 +01002887 if (i915.enable_execlists) {
Chris Wilson663f71e2016-11-14 20:41:00 +00002888 unsigned long flags;
2889
2890 spin_lock_irqsave(&engine->timeline->lock, flags);
2891
Chris Wilson70c2a242016-09-09 14:11:46 +01002892 i915_gem_request_put(engine->execlist_port[0].request);
2893 i915_gem_request_put(engine->execlist_port[1].request);
2894 memset(engine->execlist_port, 0, sizeof(engine->execlist_port));
Chris Wilson20311bd2016-11-14 20:41:03 +00002895 engine->execlist_queue = RB_ROOT;
2896 engine->execlist_first = NULL;
Chris Wilson663f71e2016-11-14 20:41:00 +00002897
2898 spin_unlock_irqrestore(&engine->timeline->lock, flags);
Oscar Mateodcb4c122014-11-13 10:28:10 +00002899 }
Eric Anholt673a3942008-07-30 12:06:12 -07002900}
2901
Chris Wilson20e49332016-11-22 14:41:21 +00002902static int __i915_gem_set_wedged_BKL(void *data)
Eric Anholt673a3942008-07-30 12:06:12 -07002903{
Chris Wilson20e49332016-11-22 14:41:21 +00002904 struct drm_i915_private *i915 = data;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002905 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302906 enum intel_engine_id id;
Eric Anholt673a3942008-07-30 12:06:12 -07002907
Chris Wilson20e49332016-11-22 14:41:21 +00002908 for_each_engine(engine, i915, id)
Chris Wilson2a20d6f2017-01-10 17:22:46 +00002909 engine_set_wedged(engine);
Chris Wilson20e49332016-11-22 14:41:21 +00002910
2911 return 0;
2912}
2913
2914void i915_gem_set_wedged(struct drm_i915_private *dev_priv)
2915{
Chris Wilson821ed7d2016-09-09 14:11:53 +01002916 lockdep_assert_held(&dev_priv->drm.struct_mutex);
2917 set_bit(I915_WEDGED, &dev_priv->gpu_error.flags);
Chris Wilson4db080f2013-12-04 11:37:09 +00002918
Chris Wilson20e49332016-11-22 14:41:21 +00002919 stop_machine(__i915_gem_set_wedged_BKL, dev_priv, NULL);
Chris Wilsondfaae392010-09-22 10:31:52 +01002920
Chris Wilson20e49332016-11-22 14:41:21 +00002921 i915_gem_context_lost(dev_priv);
Chris Wilson821ed7d2016-09-09 14:11:53 +01002922 i915_gem_retire_requests(dev_priv);
Chris Wilson20e49332016-11-22 14:41:21 +00002923
2924 mod_delayed_work(dev_priv->wq, &dev_priv->gt.idle_work, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002925}
2926
Daniel Vetter75ef9da2010-08-21 00:25:16 +02002927static void
Eric Anholt673a3942008-07-30 12:06:12 -07002928i915_gem_retire_work_handler(struct work_struct *work)
2929{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002930 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01002931 container_of(work, typeof(*dev_priv), gt.retire_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01002932 struct drm_device *dev = &dev_priv->drm;
Eric Anholt673a3942008-07-30 12:06:12 -07002933
Chris Wilson891b48c2010-09-29 12:26:37 +01002934 /* Come back later if the device is busy... */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002935 if (mutex_trylock(&dev->struct_mutex)) {
Chris Wilson67d97da2016-07-04 08:08:31 +01002936 i915_gem_retire_requests(dev_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002937 mutex_unlock(&dev->struct_mutex);
2938 }
Chris Wilson67d97da2016-07-04 08:08:31 +01002939
2940 /* Keep the retire handler running until we are finally idle.
2941 * We do not need to do this test under locking as in the worst-case
2942 * we queue the retire worker once too often.
2943 */
Chris Wilsonc9615612016-07-09 10:12:06 +01002944 if (READ_ONCE(dev_priv->gt.awake)) {
2945 i915_queue_hangcheck(dev_priv);
Chris Wilson67d97da2016-07-04 08:08:31 +01002946 queue_delayed_work(dev_priv->wq,
2947 &dev_priv->gt.retire_work,
Chris Wilsonbcb45082012-10-05 17:02:57 +01002948 round_jiffies_up_relative(HZ));
Chris Wilsonc9615612016-07-09 10:12:06 +01002949 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002950}
Chris Wilson891b48c2010-09-29 12:26:37 +01002951
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002952static void
2953i915_gem_idle_work_handler(struct work_struct *work)
2954{
2955 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01002956 container_of(work, typeof(*dev_priv), gt.idle_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01002957 struct drm_device *dev = &dev_priv->drm;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00002958 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302959 enum intel_engine_id id;
Chris Wilson67d97da2016-07-04 08:08:31 +01002960 bool rearm_hangcheck;
2961
2962 if (!READ_ONCE(dev_priv->gt.awake))
2963 return;
2964
Imre Deak0cb56702016-11-07 11:20:04 +02002965 /*
2966 * Wait for last execlists context complete, but bail out in case a
2967 * new request is submitted.
2968 */
2969 wait_for(READ_ONCE(dev_priv->gt.active_requests) ||
2970 intel_execlists_idle(dev_priv), 10);
2971
Chris Wilson28176ef2016-10-28 13:58:56 +01002972 if (READ_ONCE(dev_priv->gt.active_requests))
Chris Wilson67d97da2016-07-04 08:08:31 +01002973 return;
2974
2975 rearm_hangcheck =
2976 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
2977
2978 if (!mutex_trylock(&dev->struct_mutex)) {
2979 /* Currently busy, come back later */
2980 mod_delayed_work(dev_priv->wq,
2981 &dev_priv->gt.idle_work,
2982 msecs_to_jiffies(50));
2983 goto out_rearm;
2984 }
2985
Imre Deak93c97dc2016-11-07 11:20:03 +02002986 /*
2987 * New request retired after this work handler started, extend active
2988 * period until next instance of the work.
2989 */
2990 if (work_pending(work))
2991 goto out_unlock;
2992
Chris Wilson28176ef2016-10-28 13:58:56 +01002993 if (dev_priv->gt.active_requests)
Chris Wilson67d97da2016-07-04 08:08:31 +01002994 goto out_unlock;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002995
Imre Deak0cb56702016-11-07 11:20:04 +02002996 if (wait_for(intel_execlists_idle(dev_priv), 10))
2997 DRM_ERROR("Timeout waiting for engines to idle\n");
2998
Akash Goel3b3f1652016-10-13 22:44:48 +05302999 for_each_engine(engine, dev_priv, id)
Chris Wilson67d97da2016-07-04 08:08:31 +01003000 i915_gem_batch_pool_fini(&engine->batch_pool);
Zou Nan hai852835f2010-05-21 09:08:56 +08003001
Chris Wilson67d97da2016-07-04 08:08:31 +01003002 GEM_BUG_ON(!dev_priv->gt.awake);
3003 dev_priv->gt.awake = false;
3004 rearm_hangcheck = false;
Daniel Vetter30ecad72015-12-09 09:29:36 +01003005
Chris Wilson67d97da2016-07-04 08:08:31 +01003006 if (INTEL_GEN(dev_priv) >= 6)
3007 gen6_rps_idle(dev_priv);
3008 intel_runtime_pm_put(dev_priv);
3009out_unlock:
3010 mutex_unlock(&dev->struct_mutex);
Chris Wilson35c94182015-04-07 16:20:37 +01003011
Chris Wilson67d97da2016-07-04 08:08:31 +01003012out_rearm:
3013 if (rearm_hangcheck) {
3014 GEM_BUG_ON(!dev_priv->gt.awake);
3015 i915_queue_hangcheck(dev_priv);
Chris Wilson35c94182015-04-07 16:20:37 +01003016 }
Eric Anholt673a3942008-07-30 12:06:12 -07003017}
3018
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003019void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
3020{
3021 struct drm_i915_gem_object *obj = to_intel_bo(gem);
3022 struct drm_i915_file_private *fpriv = file->driver_priv;
3023 struct i915_vma *vma, *vn;
3024
3025 mutex_lock(&obj->base.dev->struct_mutex);
3026 list_for_each_entry_safe(vma, vn, &obj->vma_list, obj_link)
3027 if (vma->vm->file == fpriv)
3028 i915_vma_close(vma);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003029
3030 if (i915_gem_object_is_active(obj) &&
3031 !i915_gem_object_has_active_reference(obj)) {
3032 i915_gem_object_set_active_reference(obj);
3033 i915_gem_object_get(obj);
3034 }
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003035 mutex_unlock(&obj->base.dev->struct_mutex);
3036}
3037
Chris Wilsone95433c2016-10-28 13:58:27 +01003038static unsigned long to_wait_timeout(s64 timeout_ns)
3039{
3040 if (timeout_ns < 0)
3041 return MAX_SCHEDULE_TIMEOUT;
3042
3043 if (timeout_ns == 0)
3044 return 0;
3045
3046 return nsecs_to_jiffies_timeout(timeout_ns);
3047}
3048
Ben Widawsky5816d642012-04-11 11:18:19 -07003049/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003050 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003051 * @dev: drm device pointer
3052 * @data: ioctl data blob
3053 * @file: drm file pointer
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003054 *
3055 * Returns 0 if successful, else an error is returned with the remaining time in
3056 * the timeout parameter.
3057 * -ETIME: object is still busy after timeout
3058 * -ERESTARTSYS: signal interrupted the wait
3059 * -ENONENT: object doesn't exist
3060 * Also possible, but rare:
3061 * -EAGAIN: GPU wedged
3062 * -ENOMEM: damn
3063 * -ENODEV: Internal IRQ fail
3064 * -E?: The add request failed
3065 *
3066 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
3067 * non-zero timeout parameter the wait ioctl will wait for the given number of
3068 * nanoseconds on an object becoming unbusy. Since the wait itself does so
3069 * without holding struct_mutex the object may become re-busied before this
3070 * function completes. A similar but shorter * race condition exists in the busy
3071 * ioctl
3072 */
3073int
3074i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
3075{
3076 struct drm_i915_gem_wait *args = data;
3077 struct drm_i915_gem_object *obj;
Chris Wilsone95433c2016-10-28 13:58:27 +01003078 ktime_t start;
3079 long ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003080
Daniel Vetter11b5d512014-09-29 15:31:26 +02003081 if (args->flags != 0)
3082 return -EINVAL;
3083
Chris Wilson03ac0642016-07-20 13:31:51 +01003084 obj = i915_gem_object_lookup(file, args->bo_handle);
Chris Wilson033d5492016-08-05 10:14:17 +01003085 if (!obj)
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003086 return -ENOENT;
Chris Wilson033d5492016-08-05 10:14:17 +01003087
Chris Wilsone95433c2016-10-28 13:58:27 +01003088 start = ktime_get();
3089
3090 ret = i915_gem_object_wait(obj,
3091 I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL,
3092 to_wait_timeout(args->timeout_ns),
3093 to_rps_client(file));
3094
3095 if (args->timeout_ns > 0) {
3096 args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
3097 if (args->timeout_ns < 0)
3098 args->timeout_ns = 0;
Chris Wilsonc1d20612017-02-16 12:54:41 +00003099
3100 /*
3101 * Apparently ktime isn't accurate enough and occasionally has a
3102 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
3103 * things up to make the test happy. We allow up to 1 jiffy.
3104 *
3105 * This is a regression from the timespec->ktime conversion.
3106 */
3107 if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
3108 args->timeout_ns = 0;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003109 }
3110
Chris Wilsonf0cd5182016-10-28 13:58:43 +01003111 i915_gem_object_put(obj);
John Harrisonff865882014-11-24 18:49:28 +00003112 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003113}
3114
Chris Wilson73cb9702016-10-28 13:58:46 +01003115static int wait_for_timeline(struct i915_gem_timeline *tl, unsigned int flags)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003116{
Chris Wilson73cb9702016-10-28 13:58:46 +01003117 int ret, i;
3118
3119 for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
3120 ret = i915_gem_active_wait(&tl->engine[i].last_request, flags);
3121 if (ret)
3122 return ret;
3123 }
3124
3125 return 0;
3126}
3127
3128int i915_gem_wait_for_idle(struct drm_i915_private *i915, unsigned int flags)
3129{
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003130 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003131
Chris Wilson9caa34a2016-11-11 14:58:08 +00003132 if (flags & I915_WAIT_LOCKED) {
3133 struct i915_gem_timeline *tl;
3134
3135 lockdep_assert_held(&i915->drm.struct_mutex);
3136
3137 list_for_each_entry(tl, &i915->gt.timelines, link) {
3138 ret = wait_for_timeline(tl, flags);
3139 if (ret)
3140 return ret;
3141 }
3142 } else {
3143 ret = wait_for_timeline(&i915->gt.global_timeline, flags);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003144 if (ret)
3145 return ret;
3146 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003147
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01003148 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003149}
3150
Chris Wilsond0da48c2016-11-06 12:59:59 +00003151void i915_gem_clflush_object(struct drm_i915_gem_object *obj,
3152 bool force)
Eric Anholt673a3942008-07-30 12:06:12 -07003153{
Eric Anholt673a3942008-07-30 12:06:12 -07003154 /* If we don't have a page list set up, then we're not pinned
3155 * to GPU, and we can ignore the cache flush because it'll happen
3156 * again at bind time.
3157 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003158 if (!obj->mm.pages)
Chris Wilsond0da48c2016-11-06 12:59:59 +00003159 return;
Eric Anholt673a3942008-07-30 12:06:12 -07003160
Imre Deak769ce462013-02-13 21:56:05 +02003161 /*
3162 * Stolen memory is always coherent with the GPU as it is explicitly
3163 * marked as wc by the system, or the system is cache-coherent.
3164 */
Chris Wilson6a2c4232014-11-04 04:51:40 -08003165 if (obj->stolen || obj->phys_handle)
Chris Wilsond0da48c2016-11-06 12:59:59 +00003166 return;
Imre Deak769ce462013-02-13 21:56:05 +02003167
Chris Wilson9c23f7f2011-03-29 16:59:52 -07003168 /* If the GPU is snooping the contents of the CPU cache,
3169 * we do not need to manually clear the CPU cache lines. However,
3170 * the caches are only snooped when the render cache is
3171 * flushed/invalidated. As we always have to emit invalidations
3172 * and flushes when moving into and out of the RENDER domain, correct
3173 * snooping behaviour occurs naturally as the result of our domain
3174 * tracking.
3175 */
Chris Wilson0f719792015-01-13 13:32:52 +00003176 if (!force && cpu_cache_is_coherent(obj->base.dev, obj->cache_level)) {
3177 obj->cache_dirty = true;
Chris Wilsond0da48c2016-11-06 12:59:59 +00003178 return;
Chris Wilson0f719792015-01-13 13:32:52 +00003179 }
Chris Wilson9c23f7f2011-03-29 16:59:52 -07003180
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003181 trace_i915_gem_object_clflush(obj);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003182 drm_clflush_sg(obj->mm.pages);
Chris Wilson0f719792015-01-13 13:32:52 +00003183 obj->cache_dirty = false;
Eric Anholte47c68e2008-11-14 13:35:19 -08003184}
3185
3186/** Flushes the GTT write domain for the object if it's dirty. */
3187static void
Chris Wilson05394f32010-11-08 19:18:58 +00003188i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003189{
Chris Wilson3b5724d2016-08-18 17:16:49 +01003190 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003191
Chris Wilson05394f32010-11-08 19:18:58 +00003192 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
Eric Anholte47c68e2008-11-14 13:35:19 -08003193 return;
3194
Chris Wilson63256ec2011-01-04 18:42:07 +00003195 /* No actual flushing is required for the GTT write domain. Writes
Chris Wilson3b5724d2016-08-18 17:16:49 +01003196 * to it "immediately" go to main memory as far as we know, so there's
Eric Anholte47c68e2008-11-14 13:35:19 -08003197 * no chipset flush. It also doesn't land in render cache.
Chris Wilson63256ec2011-01-04 18:42:07 +00003198 *
3199 * However, we do have to enforce the order so that all writes through
3200 * the GTT land before any writes to the device, such as updates to
3201 * the GATT itself.
Chris Wilson3b5724d2016-08-18 17:16:49 +01003202 *
3203 * We also have to wait a bit for the writes to land from the GTT.
3204 * An uncached read (i.e. mmio) seems to be ideal for the round-trip
3205 * timing. This issue has only been observed when switching quickly
3206 * between GTT writes and CPU reads from inside the kernel on recent hw,
3207 * and it appears to only affect discrete GTT blocks (i.e. on LLC
3208 * system agents we cannot reproduce this behaviour).
Eric Anholte47c68e2008-11-14 13:35:19 -08003209 */
Chris Wilson63256ec2011-01-04 18:42:07 +00003210 wmb();
Chris Wilson3b5724d2016-08-18 17:16:49 +01003211 if (INTEL_GEN(dev_priv) >= 6 && !HAS_LLC(dev_priv))
Akash Goel3b3f1652016-10-13 22:44:48 +05303212 POSTING_READ(RING_ACTHD(dev_priv->engine[RCS]->mmio_base));
Chris Wilson63256ec2011-01-04 18:42:07 +00003213
Chris Wilsond243ad82016-08-18 17:16:44 +01003214 intel_fb_obj_flush(obj, false, write_origin(obj, I915_GEM_DOMAIN_GTT));
Daniel Vetterf99d7062014-06-19 16:01:59 +02003215
Chris Wilsonb0dc4652016-08-18 17:16:51 +01003216 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003217 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00003218 obj->base.read_domains,
Chris Wilsonb0dc4652016-08-18 17:16:51 +01003219 I915_GEM_DOMAIN_GTT);
Eric Anholte47c68e2008-11-14 13:35:19 -08003220}
3221
3222/** Flushes the CPU write domain for the object if it's dirty. */
3223static void
Daniel Vettere62b59e2015-01-21 14:53:48 +01003224i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003225{
Chris Wilson05394f32010-11-08 19:18:58 +00003226 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
Eric Anholte47c68e2008-11-14 13:35:19 -08003227 return;
3228
Chris Wilsond0da48c2016-11-06 12:59:59 +00003229 i915_gem_clflush_object(obj, obj->pin_display);
Rodrigo Vivide152b62015-07-07 16:28:51 -07003230 intel_fb_obj_flush(obj, false, ORIGIN_CPU);
Daniel Vetterf99d7062014-06-19 16:01:59 +02003231
Chris Wilsonb0dc4652016-08-18 17:16:51 +01003232 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003233 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00003234 obj->base.read_domains,
Chris Wilsonb0dc4652016-08-18 17:16:51 +01003235 I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08003236}
3237
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003238/**
3239 * Moves a single object to the GTT read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003240 * @obj: object to act on
3241 * @write: ask for write access or read only
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003242 *
3243 * This function returns when the move is complete, including waiting on
3244 * flushes to occur.
3245 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003246int
Chris Wilson20217462010-11-23 15:26:33 +00003247i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003248{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003249 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003250 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003251
Chris Wilsone95433c2016-10-28 13:58:27 +01003252 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003253
Chris Wilsone95433c2016-10-28 13:58:27 +01003254 ret = i915_gem_object_wait(obj,
3255 I915_WAIT_INTERRUPTIBLE |
3256 I915_WAIT_LOCKED |
3257 (write ? I915_WAIT_ALL : 0),
3258 MAX_SCHEDULE_TIMEOUT,
3259 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00003260 if (ret)
3261 return ret;
3262
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003263 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
3264 return 0;
3265
Chris Wilson43566de2015-01-02 16:29:29 +05303266 /* Flush and acquire obj->pages so that we are coherent through
3267 * direct access in memory with previous cached writes through
3268 * shmemfs and that our cache domain tracking remains valid.
3269 * For example, if the obj->filp was moved to swap without us
3270 * being notified and releasing the pages, we would mistakenly
3271 * continue to assume that the obj remained out of the CPU cached
3272 * domain.
3273 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003274 ret = i915_gem_object_pin_pages(obj);
Chris Wilson43566de2015-01-02 16:29:29 +05303275 if (ret)
3276 return ret;
3277
Daniel Vettere62b59e2015-01-21 14:53:48 +01003278 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003279
Chris Wilsond0a57782012-10-09 19:24:37 +01003280 /* Serialise direct access to this object with the barriers for
3281 * coherent writes from the GPU, by effectively invalidating the
3282 * GTT domain upon first access.
3283 */
3284 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
3285 mb();
3286
Chris Wilson05394f32010-11-08 19:18:58 +00003287 old_write_domain = obj->base.write_domain;
3288 old_read_domains = obj->base.read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003289
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003290 /* It should now be out of any other write domains, and we can update
3291 * the domain values for our changes.
3292 */
Chris Wilson40e62d52016-10-28 13:58:41 +01003293 GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
Chris Wilson05394f32010-11-08 19:18:58 +00003294 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003295 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003296 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
3297 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003298 obj->mm.dirty = true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003299 }
3300
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003301 trace_i915_gem_object_change_domain(obj,
3302 old_read_domains,
3303 old_write_domain);
3304
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003305 i915_gem_object_unpin_pages(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003306 return 0;
3307}
3308
Chris Wilsonef55f922015-10-09 14:11:27 +01003309/**
3310 * Changes the cache-level of an object across all VMA.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003311 * @obj: object to act on
3312 * @cache_level: new cache level to set for the object
Chris Wilsonef55f922015-10-09 14:11:27 +01003313 *
3314 * After this function returns, the object will be in the new cache-level
3315 * across all GTT and the contents of the backing storage will be coherent,
3316 * with respect to the new cache-level. In order to keep the backing storage
3317 * coherent for all users, we only allow a single cache level to be set
3318 * globally on the object and prevent it from being changed whilst the
3319 * hardware is reading from the object. That is if the object is currently
3320 * on the scanout it will be set to uncached (or equivalent display
3321 * cache coherency) and all non-MOCS GPU access will also be uncached so
3322 * that all direct access to the scanout remains coherent.
3323 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003324int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3325 enum i915_cache_level cache_level)
3326{
Chris Wilsonaa653a62016-08-04 07:52:27 +01003327 struct i915_vma *vma;
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003328 int ret;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003329
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003330 lockdep_assert_held(&obj->base.dev->struct_mutex);
3331
Chris Wilsone4ffd172011-04-04 09:44:39 +01003332 if (obj->cache_level == cache_level)
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003333 return 0;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003334
Chris Wilsonef55f922015-10-09 14:11:27 +01003335 /* Inspect the list of currently bound VMA and unbind any that would
3336 * be invalid given the new cache-level. This is principally to
3337 * catch the issue of the CS prefetch crossing page boundaries and
3338 * reading an invalid PTE on older architectures.
3339 */
Chris Wilsonaa653a62016-08-04 07:52:27 +01003340restart:
3341 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003342 if (!drm_mm_node_allocated(&vma->node))
3343 continue;
3344
Chris Wilson20dfbde2016-08-04 16:32:30 +01003345 if (i915_vma_is_pinned(vma)) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003346 DRM_DEBUG("can not change the cache level of pinned objects\n");
3347 return -EBUSY;
3348 }
3349
Chris Wilsonaa653a62016-08-04 07:52:27 +01003350 if (i915_gem_valid_gtt_space(vma, cache_level))
3351 continue;
3352
3353 ret = i915_vma_unbind(vma);
3354 if (ret)
3355 return ret;
3356
3357 /* As unbinding may affect other elements in the
3358 * obj->vma_list (due to side-effects from retiring
3359 * an active vma), play safe and restart the iterator.
3360 */
3361 goto restart;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003362 }
3363
Chris Wilsonef55f922015-10-09 14:11:27 +01003364 /* We can reuse the existing drm_mm nodes but need to change the
3365 * cache-level on the PTE. We could simply unbind them all and
3366 * rebind with the correct cache-level on next use. However since
3367 * we already have a valid slot, dma mapping, pages etc, we may as
3368 * rewrite the PTE in the belief that doing so tramples upon less
3369 * state and so involves less work.
3370 */
Chris Wilson15717de2016-08-04 07:52:26 +01003371 if (obj->bind_count) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003372 /* Before we change the PTE, the GPU must not be accessing it.
3373 * If we wait upon the object, we know that all the bound
3374 * VMA are no longer active.
3375 */
Chris Wilsone95433c2016-10-28 13:58:27 +01003376 ret = i915_gem_object_wait(obj,
3377 I915_WAIT_INTERRUPTIBLE |
3378 I915_WAIT_LOCKED |
3379 I915_WAIT_ALL,
3380 MAX_SCHEDULE_TIMEOUT,
3381 NULL);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003382 if (ret)
3383 return ret;
3384
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00003385 if (!HAS_LLC(to_i915(obj->base.dev)) &&
3386 cache_level != I915_CACHE_NONE) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003387 /* Access to snoopable pages through the GTT is
3388 * incoherent and on some machines causes a hard
3389 * lockup. Relinquish the CPU mmaping to force
3390 * userspace to refault in the pages and we can
3391 * then double check if the GTT mapping is still
3392 * valid for that pointer access.
3393 */
3394 i915_gem_release_mmap(obj);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003395
Chris Wilsonef55f922015-10-09 14:11:27 +01003396 /* As we no longer need a fence for GTT access,
3397 * we can relinquish it now (and so prevent having
3398 * to steal a fence from someone else on the next
3399 * fence request). Note GPU activity would have
3400 * dropped the fence as all snoopable access is
3401 * supposed to be linear.
3402 */
Chris Wilson49ef5292016-08-18 17:17:00 +01003403 list_for_each_entry(vma, &obj->vma_list, obj_link) {
3404 ret = i915_vma_put_fence(vma);
3405 if (ret)
3406 return ret;
3407 }
Chris Wilsonef55f922015-10-09 14:11:27 +01003408 } else {
3409 /* We either have incoherent backing store and
3410 * so no GTT access or the architecture is fully
3411 * coherent. In such cases, existing GTT mmaps
3412 * ignore the cache bit in the PTE and we can
3413 * rewrite it without confusing the GPU or having
3414 * to force userspace to fault back in its mmaps.
3415 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003416 }
3417
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003418 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003419 if (!drm_mm_node_allocated(&vma->node))
3420 continue;
3421
3422 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
3423 if (ret)
3424 return ret;
3425 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01003426 }
3427
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003428 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU &&
3429 cpu_cache_is_coherent(obj->base.dev, obj->cache_level))
3430 obj->cache_dirty = true;
3431
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003432 list_for_each_entry(vma, &obj->vma_list, obj_link)
Chris Wilson2c225692013-08-09 12:26:45 +01003433 vma->node.color = cache_level;
3434 obj->cache_level = cache_level;
3435
Chris Wilsone4ffd172011-04-04 09:44:39 +01003436 return 0;
3437}
3438
Ben Widawsky199adf42012-09-21 17:01:20 -07003439int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
3440 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003441{
Ben Widawsky199adf42012-09-21 17:01:20 -07003442 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003443 struct drm_i915_gem_object *obj;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003444 int err = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003445
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003446 rcu_read_lock();
3447 obj = i915_gem_object_lookup_rcu(file, args->handle);
3448 if (!obj) {
3449 err = -ENOENT;
3450 goto out;
3451 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01003452
Chris Wilson651d7942013-08-08 14:41:10 +01003453 switch (obj->cache_level) {
3454 case I915_CACHE_LLC:
3455 case I915_CACHE_L3_LLC:
3456 args->caching = I915_CACHING_CACHED;
3457 break;
3458
Chris Wilson4257d3b2013-08-08 14:41:11 +01003459 case I915_CACHE_WT:
3460 args->caching = I915_CACHING_DISPLAY;
3461 break;
3462
Chris Wilson651d7942013-08-08 14:41:10 +01003463 default:
3464 args->caching = I915_CACHING_NONE;
3465 break;
3466 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003467out:
3468 rcu_read_unlock();
3469 return err;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003470}
3471
Ben Widawsky199adf42012-09-21 17:01:20 -07003472int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
3473 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003474{
Chris Wilson9c870d02016-10-24 13:42:15 +01003475 struct drm_i915_private *i915 = to_i915(dev);
Ben Widawsky199adf42012-09-21 17:01:20 -07003476 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003477 struct drm_i915_gem_object *obj;
3478 enum i915_cache_level level;
Chris Wilsond65415d2017-01-19 08:22:10 +00003479 int ret = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003480
Ben Widawsky199adf42012-09-21 17:01:20 -07003481 switch (args->caching) {
3482 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01003483 level = I915_CACHE_NONE;
3484 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07003485 case I915_CACHING_CACHED:
Imre Deake5756c12015-08-14 18:43:30 +03003486 /*
3487 * Due to a HW issue on BXT A stepping, GPU stores via a
3488 * snooped mapping may leave stale data in a corresponding CPU
3489 * cacheline, whereas normally such cachelines would get
3490 * invalidated.
3491 */
Chris Wilson9c870d02016-10-24 13:42:15 +01003492 if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
Imre Deake5756c12015-08-14 18:43:30 +03003493 return -ENODEV;
3494
Chris Wilsone6994ae2012-07-10 10:27:08 +01003495 level = I915_CACHE_LLC;
3496 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003497 case I915_CACHING_DISPLAY:
Chris Wilson9c870d02016-10-24 13:42:15 +01003498 level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003499 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003500 default:
3501 return -EINVAL;
3502 }
3503
Chris Wilsond65415d2017-01-19 08:22:10 +00003504 obj = i915_gem_object_lookup(file, args->handle);
3505 if (!obj)
3506 return -ENOENT;
3507
3508 if (obj->cache_level == level)
3509 goto out;
3510
3511 ret = i915_gem_object_wait(obj,
3512 I915_WAIT_INTERRUPTIBLE,
3513 MAX_SCHEDULE_TIMEOUT,
3514 to_rps_client(file));
3515 if (ret)
3516 goto out;
3517
Ben Widawsky3bc29132012-09-26 16:15:20 -07003518 ret = i915_mutex_lock_interruptible(dev);
3519 if (ret)
Chris Wilsond65415d2017-01-19 08:22:10 +00003520 goto out;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003521
3522 ret = i915_gem_object_set_cache_level(obj, level);
Chris Wilsone6994ae2012-07-10 10:27:08 +01003523 mutex_unlock(&dev->struct_mutex);
Chris Wilsond65415d2017-01-19 08:22:10 +00003524
3525out:
3526 i915_gem_object_put(obj);
Chris Wilsone6994ae2012-07-10 10:27:08 +01003527 return ret;
3528}
3529
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003530/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003531 * Prepare buffer for display plane (scanout, cursors, etc).
3532 * Can be called from an uninterruptible phase (modesetting) and allows
3533 * any flushes to be pipelined (for pageflips).
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003534 */
Chris Wilson058d88c2016-08-15 10:49:06 +01003535struct i915_vma *
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003536i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3537 u32 alignment,
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003538 const struct i915_ggtt_view *view)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003539{
Chris Wilson058d88c2016-08-15 10:49:06 +01003540 struct i915_vma *vma;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003541 u32 old_read_domains, old_write_domain;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003542 int ret;
3543
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003544 lockdep_assert_held(&obj->base.dev->struct_mutex);
3545
Chris Wilsoncc98b412013-08-09 12:25:09 +01003546 /* Mark the pin_display early so that we account for the
3547 * display coherency whilst setting up the cache domains.
3548 */
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003549 obj->pin_display++;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003550
Eric Anholta7ef0642011-03-29 16:59:54 -07003551 /* The display engine is not coherent with the LLC cache on gen6. As
3552 * a result, we make sure that the pinning that is about to occur is
3553 * done with uncached PTEs. This is lowest common denominator for all
3554 * chipsets.
3555 *
3556 * However for gen6+, we could do better by using the GFDT bit instead
3557 * of uncaching, which would allow us to flush all the LLC-cached data
3558 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3559 */
Chris Wilson651d7942013-08-08 14:41:10 +01003560 ret = i915_gem_object_set_cache_level(obj,
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003561 HAS_WT(to_i915(obj->base.dev)) ?
3562 I915_CACHE_WT : I915_CACHE_NONE);
Chris Wilson058d88c2016-08-15 10:49:06 +01003563 if (ret) {
3564 vma = ERR_PTR(ret);
Chris Wilsoncc98b412013-08-09 12:25:09 +01003565 goto err_unpin_display;
Chris Wilson058d88c2016-08-15 10:49:06 +01003566 }
Eric Anholta7ef0642011-03-29 16:59:54 -07003567
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003568 /* As the user may map the buffer once pinned in the display plane
3569 * (e.g. libkms for the bootup splash), we have to ensure that we
Chris Wilson2efb8132016-08-18 17:17:06 +01003570 * always use map_and_fenceable for all scanout buffers. However,
3571 * it may simply be too big to fit into mappable, in which case
3572 * put it anyway and hope that userspace can cope (but always first
3573 * try to preserve the existing ABI).
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003574 */
Chris Wilson2efb8132016-08-18 17:17:06 +01003575 vma = ERR_PTR(-ENOSPC);
Chris Wilson47a8e3f2017-01-14 00:28:27 +00003576 if (!view || view->type == I915_GGTT_VIEW_NORMAL)
Chris Wilson2efb8132016-08-18 17:17:06 +01003577 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
3578 PIN_MAPPABLE | PIN_NONBLOCK);
Chris Wilson767a2222016-11-07 11:01:28 +00003579 if (IS_ERR(vma)) {
3580 struct drm_i915_private *i915 = to_i915(obj->base.dev);
3581 unsigned int flags;
3582
3583 /* Valleyview is definitely limited to scanning out the first
3584 * 512MiB. Lets presume this behaviour was inherited from the
3585 * g4x display engine and that all earlier gen are similarly
3586 * limited. Testing suggests that it is a little more
3587 * complicated than this. For example, Cherryview appears quite
3588 * happy to scanout from anywhere within its global aperture.
3589 */
3590 flags = 0;
3591 if (HAS_GMCH_DISPLAY(i915))
3592 flags = PIN_MAPPABLE;
3593 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags);
3594 }
Chris Wilson058d88c2016-08-15 10:49:06 +01003595 if (IS_ERR(vma))
Chris Wilsoncc98b412013-08-09 12:25:09 +01003596 goto err_unpin_display;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003597
Chris Wilsond8923dc2016-08-18 17:17:07 +01003598 vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
3599
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003600 /* Treat this as an end-of-frame, like intel_user_framebuffer_dirty() */
Chris Wilson69aeafe2017-01-09 11:19:32 +00003601 if (obj->cache_dirty || obj->base.write_domain == I915_GEM_DOMAIN_CPU) {
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003602 i915_gem_clflush_object(obj, true);
3603 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
3604 }
Chris Wilsonb118c1e2010-05-27 13:18:14 +01003605
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003606 old_write_domain = obj->base.write_domain;
Chris Wilson05394f32010-11-08 19:18:58 +00003607 old_read_domains = obj->base.read_domains;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003608
3609 /* It should now be out of any other write domains, and we can update
3610 * the domain values for our changes.
3611 */
Chris Wilsone5f1d962012-07-20 12:41:00 +01003612 obj->base.write_domain = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00003613 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003614
3615 trace_i915_gem_object_change_domain(obj,
3616 old_read_domains,
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003617 old_write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003618
Chris Wilson058d88c2016-08-15 10:49:06 +01003619 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003620
3621err_unpin_display:
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003622 obj->pin_display--;
Chris Wilson058d88c2016-08-15 10:49:06 +01003623 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003624}
3625
3626void
Chris Wilson058d88c2016-08-15 10:49:06 +01003627i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
Chris Wilsoncc98b412013-08-09 12:25:09 +01003628{
Chris Wilson49d73912016-11-29 09:50:08 +00003629 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003630
Chris Wilson058d88c2016-08-15 10:49:06 +01003631 if (WARN_ON(vma->obj->pin_display == 0))
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003632 return;
3633
Chris Wilsond8923dc2016-08-18 17:17:07 +01003634 if (--vma->obj->pin_display == 0)
Chris Wilsonf51455d2017-01-10 14:47:34 +00003635 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003636
Chris Wilson383d5822016-08-18 17:17:08 +01003637 /* Bump the LRU to try and avoid premature eviction whilst flipping */
Chris Wilsonbefedbb2017-01-19 19:26:55 +00003638 i915_gem_object_bump_inactive_ggtt(vma->obj);
Chris Wilson383d5822016-08-18 17:17:08 +01003639
Chris Wilson058d88c2016-08-15 10:49:06 +01003640 i915_vma_unpin(vma);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003641}
3642
Eric Anholte47c68e2008-11-14 13:35:19 -08003643/**
3644 * Moves a single object to the CPU read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003645 * @obj: object to act on
3646 * @write: requesting write or read-only access
Eric Anholte47c68e2008-11-14 13:35:19 -08003647 *
3648 * This function returns when the move is complete, including waiting on
3649 * flushes to occur.
3650 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02003651int
Chris Wilson919926a2010-11-12 13:42:53 +00003652i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08003653{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003654 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003655 int ret;
3656
Chris Wilsone95433c2016-10-28 13:58:27 +01003657 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003658
Chris Wilsone95433c2016-10-28 13:58:27 +01003659 ret = i915_gem_object_wait(obj,
3660 I915_WAIT_INTERRUPTIBLE |
3661 I915_WAIT_LOCKED |
3662 (write ? I915_WAIT_ALL : 0),
3663 MAX_SCHEDULE_TIMEOUT,
3664 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00003665 if (ret)
3666 return ret;
3667
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003668 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
3669 return 0;
3670
Eric Anholte47c68e2008-11-14 13:35:19 -08003671 i915_gem_object_flush_gtt_write_domain(obj);
3672
Chris Wilson05394f32010-11-08 19:18:58 +00003673 old_write_domain = obj->base.write_domain;
3674 old_read_domains = obj->base.read_domains;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003675
Eric Anholte47c68e2008-11-14 13:35:19 -08003676 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00003677 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson2c225692013-08-09 12:26:45 +01003678 i915_gem_clflush_object(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003679
Chris Wilson05394f32010-11-08 19:18:58 +00003680 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003681 }
3682
3683 /* It should now be out of any other write domains, and we can update
3684 * the domain values for our changes.
3685 */
Chris Wilson40e62d52016-10-28 13:58:41 +01003686 GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003687
3688 /* If we're writing through the CPU, then the GPU read domains will
3689 * need to be invalidated at next use.
3690 */
3691 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003692 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3693 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003694 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003695
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003696 trace_i915_gem_object_change_domain(obj,
3697 old_read_domains,
3698 old_write_domain);
3699
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003700 return 0;
3701}
3702
Eric Anholt673a3942008-07-30 12:06:12 -07003703/* Throttle our rendering by waiting until the ring has completed our requests
3704 * emitted over 20 msec ago.
3705 *
Eric Anholtb9624422009-06-03 07:27:35 +00003706 * Note that if we were to use the current jiffies each time around the loop,
3707 * we wouldn't escape the function with any frames outstanding if the time to
3708 * render a frame was over 20ms.
3709 *
Eric Anholt673a3942008-07-30 12:06:12 -07003710 * This should get us reasonable parallelism between CPU and GPU but also
3711 * relatively low latency when blocking on a particular request to finish.
3712 */
3713static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003714i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003715{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003716 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003717 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsond0bc54f2015-05-21 21:01:48 +01003718 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
John Harrison54fb2412014-11-24 18:49:27 +00003719 struct drm_i915_gem_request *request, *target = NULL;
Chris Wilsone95433c2016-10-28 13:58:27 +01003720 long ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003721
Chris Wilsonf4457ae2016-04-13 17:35:08 +01003722 /* ABI: return -EIO if already wedged */
3723 if (i915_terminally_wedged(&dev_priv->gpu_error))
3724 return -EIO;
Chris Wilsone110e8d2011-01-26 15:39:14 +00003725
Chris Wilson1c255952010-09-26 11:03:27 +01003726 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003727 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003728 if (time_after_eq(request->emitted_jiffies, recent_enough))
3729 break;
3730
John Harrisonfcfa423c2015-05-29 17:44:12 +01003731 /*
3732 * Note that the request might not have been submitted yet.
3733 * In which case emitted_jiffies will be zero.
3734 */
3735 if (!request->emitted_jiffies)
3736 continue;
3737
John Harrison54fb2412014-11-24 18:49:27 +00003738 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00003739 }
John Harrisonff865882014-11-24 18:49:28 +00003740 if (target)
Chris Wilsone8a261e2016-07-20 13:31:49 +01003741 i915_gem_request_get(target);
Chris Wilson1c255952010-09-26 11:03:27 +01003742 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003743
John Harrison54fb2412014-11-24 18:49:27 +00003744 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003745 return 0;
3746
Chris Wilsone95433c2016-10-28 13:58:27 +01003747 ret = i915_wait_request(target,
3748 I915_WAIT_INTERRUPTIBLE,
3749 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone8a261e2016-07-20 13:31:49 +01003750 i915_gem_request_put(target);
John Harrisonff865882014-11-24 18:49:28 +00003751
Chris Wilsone95433c2016-10-28 13:58:27 +01003752 return ret < 0 ? ret : 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003753}
3754
Chris Wilson058d88c2016-08-15 10:49:06 +01003755struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003756i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
3757 const struct i915_ggtt_view *view,
Chris Wilson91b2db62016-08-04 16:32:23 +01003758 u64 size,
Chris Wilson2ffffd02016-08-04 16:32:22 +01003759 u64 alignment,
3760 u64 flags)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003761{
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003762 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
3763 struct i915_address_space *vm = &dev_priv->ggtt.base;
Chris Wilson59bfa122016-08-04 16:32:31 +01003764 struct i915_vma *vma;
3765 int ret;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003766
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003767 lockdep_assert_held(&obj->base.dev->struct_mutex);
3768
Chris Wilson718659a2017-01-16 15:21:28 +00003769 vma = i915_vma_instance(obj, vm, view);
Chris Wilsone0216b72017-01-19 19:26:57 +00003770 if (unlikely(IS_ERR(vma)))
Chris Wilson058d88c2016-08-15 10:49:06 +01003771 return vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01003772
3773 if (i915_vma_misplaced(vma, size, alignment, flags)) {
3774 if (flags & PIN_NONBLOCK &&
3775 (i915_vma_is_pinned(vma) || i915_vma_is_active(vma)))
Chris Wilson058d88c2016-08-15 10:49:06 +01003776 return ERR_PTR(-ENOSPC);
Chris Wilson59bfa122016-08-04 16:32:31 +01003777
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003778 if (flags & PIN_MAPPABLE) {
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003779 /* If the required space is larger than the available
3780 * aperture, we will not able to find a slot for the
3781 * object and unbinding the object now will be in
3782 * vain. Worse, doing so may cause us to ping-pong
3783 * the object in and out of the Global GTT and
3784 * waste a lot of cycles under the mutex.
3785 */
Chris Wilson944397f2017-01-09 16:16:11 +00003786 if (vma->fence_size > dev_priv->ggtt.mappable_end)
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003787 return ERR_PTR(-E2BIG);
3788
3789 /* If NONBLOCK is set the caller is optimistically
3790 * trying to cache the full object within the mappable
3791 * aperture, and *must* have a fallback in place for
3792 * situations where we cannot bind the object. We
3793 * can be a little more lax here and use the fallback
3794 * more often to avoid costly migrations of ourselves
3795 * and other objects within the aperture.
3796 *
3797 * Half-the-aperture is used as a simple heuristic.
3798 * More interesting would to do search for a free
3799 * block prior to making the commitment to unbind.
3800 * That caters for the self-harm case, and with a
3801 * little more heuristics (e.g. NOFAULT, NOEVICT)
3802 * we could try to minimise harm to others.
3803 */
3804 if (flags & PIN_NONBLOCK &&
Chris Wilson944397f2017-01-09 16:16:11 +00003805 vma->fence_size > dev_priv->ggtt.mappable_end / 2)
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003806 return ERR_PTR(-ENOSPC);
3807 }
3808
Chris Wilson59bfa122016-08-04 16:32:31 +01003809 WARN(i915_vma_is_pinned(vma),
3810 "bo is already pinned in ggtt with incorrect alignment:"
Chris Wilson05a20d02016-08-18 17:16:55 +01003811 " offset=%08x, req.alignment=%llx,"
3812 " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
3813 i915_ggtt_offset(vma), alignment,
Chris Wilson59bfa122016-08-04 16:32:31 +01003814 !!(flags & PIN_MAPPABLE),
Chris Wilson05a20d02016-08-18 17:16:55 +01003815 i915_vma_is_map_and_fenceable(vma));
Chris Wilson59bfa122016-08-04 16:32:31 +01003816 ret = i915_vma_unbind(vma);
3817 if (ret)
Chris Wilson058d88c2016-08-15 10:49:06 +01003818 return ERR_PTR(ret);
Chris Wilson59bfa122016-08-04 16:32:31 +01003819 }
3820
Chris Wilson058d88c2016-08-15 10:49:06 +01003821 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
3822 if (ret)
3823 return ERR_PTR(ret);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003824
Chris Wilson058d88c2016-08-15 10:49:06 +01003825 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07003826}
3827
Chris Wilsonedf6b762016-08-09 09:23:33 +01003828static __always_inline unsigned int __busy_read_flag(unsigned int id)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003829{
3830 /* Note that we could alias engines in the execbuf API, but
3831 * that would be very unwise as it prevents userspace from
3832 * fine control over engine selection. Ahem.
3833 *
3834 * This should be something like EXEC_MAX_ENGINE instead of
3835 * I915_NUM_ENGINES.
3836 */
3837 BUILD_BUG_ON(I915_NUM_ENGINES > 16);
3838 return 0x10000 << id;
3839}
3840
3841static __always_inline unsigned int __busy_write_id(unsigned int id)
3842{
Chris Wilson70cb4722016-08-09 18:08:25 +01003843 /* The uABI guarantees an active writer is also amongst the read
3844 * engines. This would be true if we accessed the activity tracking
3845 * under the lock, but as we perform the lookup of the object and
3846 * its activity locklessly we can not guarantee that the last_write
3847 * being active implies that we have set the same engine flag from
3848 * last_read - hence we always set both read and write busy for
3849 * last_write.
3850 */
3851 return id | __busy_read_flag(id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003852}
3853
Chris Wilsonedf6b762016-08-09 09:23:33 +01003854static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01003855__busy_set_if_active(const struct dma_fence *fence,
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003856 unsigned int (*flag)(unsigned int id))
3857{
Chris Wilsond07f0e52016-10-28 13:58:44 +01003858 struct drm_i915_gem_request *rq;
Chris Wilson12555012016-08-16 09:50:40 +01003859
Chris Wilsond07f0e52016-10-28 13:58:44 +01003860 /* We have to check the current hw status of the fence as the uABI
3861 * guarantees forward progress. We could rely on the idle worker
3862 * to eventually flush us, but to minimise latency just ask the
3863 * hardware.
3864 *
3865 * Note we only report on the status of native fences.
3866 */
3867 if (!dma_fence_is_i915(fence))
Chris Wilson12555012016-08-16 09:50:40 +01003868 return 0;
3869
Chris Wilsond07f0e52016-10-28 13:58:44 +01003870 /* opencode to_request() in order to avoid const warnings */
3871 rq = container_of(fence, struct drm_i915_gem_request, fence);
3872 if (i915_gem_request_completed(rq))
3873 return 0;
3874
3875 return flag(rq->engine->exec_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003876}
3877
Chris Wilsonedf6b762016-08-09 09:23:33 +01003878static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01003879busy_check_reader(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003880{
Chris Wilsond07f0e52016-10-28 13:58:44 +01003881 return __busy_set_if_active(fence, __busy_read_flag);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003882}
3883
Chris Wilsonedf6b762016-08-09 09:23:33 +01003884static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01003885busy_check_writer(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003886{
Chris Wilsond07f0e52016-10-28 13:58:44 +01003887 if (!fence)
3888 return 0;
3889
3890 return __busy_set_if_active(fence, __busy_write_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003891}
3892
Eric Anholt673a3942008-07-30 12:06:12 -07003893int
Eric Anholt673a3942008-07-30 12:06:12 -07003894i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003895 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003896{
3897 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003898 struct drm_i915_gem_object *obj;
Chris Wilsond07f0e52016-10-28 13:58:44 +01003899 struct reservation_object_list *list;
3900 unsigned int seq;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003901 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07003902
Chris Wilsond07f0e52016-10-28 13:58:44 +01003903 err = -ENOENT;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003904 rcu_read_lock();
3905 obj = i915_gem_object_lookup_rcu(file, args->handle);
Chris Wilsond07f0e52016-10-28 13:58:44 +01003906 if (!obj)
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003907 goto out;
Chris Wilsond07f0e52016-10-28 13:58:44 +01003908
3909 /* A discrepancy here is that we do not report the status of
3910 * non-i915 fences, i.e. even though we may report the object as idle,
3911 * a call to set-domain may still stall waiting for foreign rendering.
3912 * This also means that wait-ioctl may report an object as busy,
3913 * where busy-ioctl considers it idle.
3914 *
3915 * We trade the ability to warn of foreign fences to report on which
3916 * i915 engines are active for the object.
3917 *
3918 * Alternatively, we can trade that extra information on read/write
3919 * activity with
3920 * args->busy =
3921 * !reservation_object_test_signaled_rcu(obj->resv, true);
3922 * to report the overall busyness. This is what the wait-ioctl does.
3923 *
3924 */
3925retry:
3926 seq = raw_read_seqcount(&obj->resv->seq);
3927
3928 /* Translate the exclusive fence to the READ *and* WRITE engine */
3929 args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl));
3930
3931 /* Translate shared fences to READ set of engines */
3932 list = rcu_dereference(obj->resv->fence);
3933 if (list) {
3934 unsigned int shared_count = list->shared_count, i;
3935
3936 for (i = 0; i < shared_count; ++i) {
3937 struct dma_fence *fence =
3938 rcu_dereference(list->shared[i]);
3939
3940 args->busy |= busy_check_reader(fence);
3941 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003942 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003943
Chris Wilsond07f0e52016-10-28 13:58:44 +01003944 if (args->busy && read_seqcount_retry(&obj->resv->seq, seq))
3945 goto retry;
Chris Wilson426960b2016-01-15 16:51:46 +00003946
Chris Wilsond07f0e52016-10-28 13:58:44 +01003947 err = 0;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003948out:
3949 rcu_read_unlock();
3950 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07003951}
3952
3953int
3954i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3955 struct drm_file *file_priv)
3956{
Akshay Joshi0206e352011-08-16 15:34:10 -04003957 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07003958}
3959
Chris Wilson3ef94da2009-09-14 16:50:29 +01003960int
3961i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
3962 struct drm_file *file_priv)
3963{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003964 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson3ef94da2009-09-14 16:50:29 +01003965 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003966 struct drm_i915_gem_object *obj;
Chris Wilson1233e2d2016-10-28 13:58:37 +01003967 int err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003968
3969 switch (args->madv) {
3970 case I915_MADV_DONTNEED:
3971 case I915_MADV_WILLNEED:
3972 break;
3973 default:
3974 return -EINVAL;
3975 }
3976
Chris Wilson03ac0642016-07-20 13:31:51 +01003977 obj = i915_gem_object_lookup(file_priv, args->handle);
Chris Wilson1233e2d2016-10-28 13:58:37 +01003978 if (!obj)
3979 return -ENOENT;
3980
3981 err = mutex_lock_interruptible(&obj->mm.lock);
3982 if (err)
3983 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003984
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003985 if (obj->mm.pages &&
Chris Wilson3e510a82016-08-05 10:14:23 +01003986 i915_gem_object_is_tiled(obj) &&
Daniel Vetter656bfa32014-11-20 09:26:30 +01003987 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilsonbc0629a2016-11-01 10:03:17 +00003988 if (obj->mm.madv == I915_MADV_WILLNEED) {
3989 GEM_BUG_ON(!obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003990 __i915_gem_object_unpin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00003991 obj->mm.quirked = false;
3992 }
3993 if (args->madv == I915_MADV_WILLNEED) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00003994 GEM_BUG_ON(obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003995 __i915_gem_object_pin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00003996 obj->mm.quirked = true;
3997 }
Daniel Vetter656bfa32014-11-20 09:26:30 +01003998 }
3999
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004000 if (obj->mm.madv != __I915_MADV_PURGED)
4001 obj->mm.madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004002
Chris Wilson6c085a72012-08-20 11:40:46 +02004003 /* if the object is no longer attached, discard its backing storage */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004004 if (obj->mm.madv == I915_MADV_DONTNEED && !obj->mm.pages)
Chris Wilson2d7ef392009-09-20 23:13:10 +01004005 i915_gem_object_truncate(obj);
4006
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004007 args->retained = obj->mm.madv != __I915_MADV_PURGED;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004008 mutex_unlock(&obj->mm.lock);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004009
Chris Wilson1233e2d2016-10-28 13:58:37 +01004010out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004011 i915_gem_object_put(obj);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004012 return err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004013}
4014
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004015static void
4016frontbuffer_retire(struct i915_gem_active *active,
4017 struct drm_i915_gem_request *request)
4018{
4019 struct drm_i915_gem_object *obj =
4020 container_of(active, typeof(*obj), frontbuffer_write);
4021
4022 intel_fb_obj_flush(obj, true, ORIGIN_CS);
4023}
4024
Chris Wilson37e680a2012-06-07 15:38:42 +01004025void i915_gem_object_init(struct drm_i915_gem_object *obj,
4026 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01004027{
Chris Wilson1233e2d2016-10-28 13:58:37 +01004028 mutex_init(&obj->mm.lock);
4029
Joonas Lahtinen56cea322016-11-02 12:16:04 +02004030 INIT_LIST_HEAD(&obj->global_link);
Chris Wilson275f0392016-10-24 13:42:14 +01004031 INIT_LIST_HEAD(&obj->userfault_link);
Ben Widawskyb25cb2f2013-08-14 11:38:33 +02004032 INIT_LIST_HEAD(&obj->obj_exec_link);
Ben Widawsky2f633152013-07-17 12:19:03 -07004033 INIT_LIST_HEAD(&obj->vma_list);
Chris Wilson8d9d5742015-04-07 16:20:38 +01004034 INIT_LIST_HEAD(&obj->batch_pool_link);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004035
Chris Wilson37e680a2012-06-07 15:38:42 +01004036 obj->ops = ops;
4037
Chris Wilsond07f0e52016-10-28 13:58:44 +01004038 reservation_object_init(&obj->__builtin_resv);
4039 obj->resv = &obj->__builtin_resv;
4040
Chris Wilson50349242016-08-18 17:17:04 +01004041 obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004042 init_request_active(&obj->frontbuffer_write, frontbuffer_retire);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004043
4044 obj->mm.madv = I915_MADV_WILLNEED;
4045 INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
4046 mutex_init(&obj->mm.get_page.lock);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004047
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004048 i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004049}
4050
Chris Wilson37e680a2012-06-07 15:38:42 +01004051static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
Tvrtko Ursulin3599a912016-11-01 14:44:10 +00004052 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
4053 I915_GEM_OBJECT_IS_SHRINKABLE,
Chris Wilson37e680a2012-06-07 15:38:42 +01004054 .get_pages = i915_gem_object_get_pages_gtt,
4055 .put_pages = i915_gem_object_put_pages_gtt,
4056};
4057
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004058struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004059i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004060{
Daniel Vetterc397b902010-04-09 19:05:07 +00004061 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004062 struct address_space *mapping;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004063 gfp_t mask;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004064 int ret;
Daniel Vetterc397b902010-04-09 19:05:07 +00004065
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004066 /* There is a prevalence of the assumption that we fit the object's
4067 * page count inside a 32bit _signed_ variable. Let's document this and
4068 * catch if we ever need to fix it. In the meantime, if you do spot
4069 * such a local variable, please consider fixing!
4070 */
4071 if (WARN_ON(size >> PAGE_SHIFT > INT_MAX))
4072 return ERR_PTR(-E2BIG);
4073
4074 if (overflows_type(size, obj->base.size))
4075 return ERR_PTR(-E2BIG);
4076
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00004077 obj = i915_gem_object_alloc(dev_priv);
Daniel Vetterc397b902010-04-09 19:05:07 +00004078 if (obj == NULL)
Chris Wilsonfe3db792016-04-25 13:32:13 +01004079 return ERR_PTR(-ENOMEM);
Daniel Vetterc397b902010-04-09 19:05:07 +00004080
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004081 ret = drm_gem_object_init(&dev_priv->drm, &obj->base, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004082 if (ret)
4083 goto fail;
Daniel Vetterc397b902010-04-09 19:05:07 +00004084
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004085 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
Jani Nikulac0f86832016-12-07 12:13:04 +02004086 if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) {
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004087 /* 965gm cannot relocate objects above 4GiB. */
4088 mask &= ~__GFP_HIGHMEM;
4089 mask |= __GFP_DMA32;
4090 }
4091
Al Viro93c76a32015-12-04 23:45:44 -05004092 mapping = obj->base.filp->f_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004093 mapping_set_gfp_mask(mapping, mask);
Hugh Dickins5949eac2011-06-27 16:18:18 -07004094
Chris Wilson37e680a2012-06-07 15:38:42 +01004095 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004096
Daniel Vetterc397b902010-04-09 19:05:07 +00004097 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4098 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4099
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00004100 if (HAS_LLC(dev_priv)) {
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004101 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004102 * cache) for about a 10% performance improvement
4103 * compared to uncached. Graphics requests other than
4104 * display scanout are coherent with the CPU in
4105 * accessing this cache. This means in this mode we
4106 * don't need to clflush on the CPU side, and on the
4107 * GPU side we only need to flush internal caches to
4108 * get data visible to the CPU.
4109 *
4110 * However, we maintain the display planes as UC, and so
4111 * need to rebind when first used as such.
4112 */
4113 obj->cache_level = I915_CACHE_LLC;
4114 } else
4115 obj->cache_level = I915_CACHE_NONE;
4116
Daniel Vetterd861e332013-07-24 23:25:03 +02004117 trace_i915_gem_object_create(obj);
4118
Chris Wilson05394f32010-11-08 19:18:58 +00004119 return obj;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004120
4121fail:
4122 i915_gem_object_free(obj);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004123 return ERR_PTR(ret);
Daniel Vetterac52bc52010-04-09 19:05:06 +00004124}
4125
Chris Wilson340fbd82014-05-22 09:16:52 +01004126static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4127{
4128 /* If we are the last user of the backing storage (be it shmemfs
4129 * pages or stolen etc), we know that the pages are going to be
4130 * immediately released. In this case, we can then skip copying
4131 * back the contents from the GPU.
4132 */
4133
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004134 if (obj->mm.madv != I915_MADV_WILLNEED)
Chris Wilson340fbd82014-05-22 09:16:52 +01004135 return false;
4136
4137 if (obj->base.filp == NULL)
4138 return true;
4139
4140 /* At first glance, this looks racy, but then again so would be
4141 * userspace racing mmap against close. However, the first external
4142 * reference to the filp can only be obtained through the
4143 * i915_gem_mmap_ioctl() which safeguards us against the user
4144 * acquiring such a reference whilst we are in the middle of
4145 * freeing the object.
4146 */
4147 return atomic_long_read(&obj->base.filp->f_count) == 1;
4148}
4149
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004150static void __i915_gem_free_objects(struct drm_i915_private *i915,
4151 struct llist_node *freed)
Chris Wilsonbe726152010-07-23 23:18:50 +01004152{
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004153 struct drm_i915_gem_object *obj, *on;
Chris Wilsonbe726152010-07-23 23:18:50 +01004154
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004155 mutex_lock(&i915->drm.struct_mutex);
4156 intel_runtime_pm_get(i915);
4157 llist_for_each_entry(obj, freed, freed) {
4158 struct i915_vma *vma, *vn;
Paulo Zanonif65c9162013-11-27 18:20:34 -02004159
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004160 trace_i915_gem_object_destroy(obj);
4161
4162 GEM_BUG_ON(i915_gem_object_is_active(obj));
4163 list_for_each_entry_safe(vma, vn,
4164 &obj->vma_list, obj_link) {
4165 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
4166 GEM_BUG_ON(i915_vma_is_active(vma));
4167 vma->flags &= ~I915_VMA_PIN_MASK;
4168 i915_vma_close(vma);
4169 }
Chris Wilsondb6c2b42016-11-01 11:54:00 +00004170 GEM_BUG_ON(!list_empty(&obj->vma_list));
4171 GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004172
Joonas Lahtinen56cea322016-11-02 12:16:04 +02004173 list_del(&obj->global_link);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004174 }
4175 intel_runtime_pm_put(i915);
4176 mutex_unlock(&i915->drm.struct_mutex);
4177
4178 llist_for_each_entry_safe(obj, on, freed, freed) {
4179 GEM_BUG_ON(obj->bind_count);
4180 GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits));
4181
4182 if (obj->ops->release)
4183 obj->ops->release(obj);
4184
4185 if (WARN_ON(i915_gem_object_has_pinned_pages(obj)))
4186 atomic_set(&obj->mm.pages_pin_count, 0);
Chris Wilson548625e2016-11-01 12:11:34 +00004187 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004188 GEM_BUG_ON(obj->mm.pages);
4189
4190 if (obj->base.import_attach)
4191 drm_prime_gem_destroy(&obj->base, NULL);
4192
Chris Wilsond07f0e52016-10-28 13:58:44 +01004193 reservation_object_fini(&obj->__builtin_resv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004194 drm_gem_object_release(&obj->base);
4195 i915_gem_info_remove_obj(i915, obj->base.size);
4196
4197 kfree(obj->bit_17);
4198 i915_gem_object_free(obj);
4199 }
4200}
4201
4202static void i915_gem_flush_free_objects(struct drm_i915_private *i915)
4203{
4204 struct llist_node *freed;
4205
4206 freed = llist_del_all(&i915->mm.free_list);
4207 if (unlikely(freed))
4208 __i915_gem_free_objects(i915, freed);
4209}
4210
4211static void __i915_gem_free_work(struct work_struct *work)
4212{
4213 struct drm_i915_private *i915 =
4214 container_of(work, struct drm_i915_private, mm.free_work);
4215 struct llist_node *freed;
Chris Wilson26e12f82011-03-20 11:20:19 +00004216
Chris Wilsonb1f788c2016-08-04 07:52:45 +01004217 /* All file-owned VMA should have been released by this point through
4218 * i915_gem_close_object(), or earlier by i915_gem_context_close().
4219 * However, the object may also be bound into the global GTT (e.g.
4220 * older GPUs without per-process support, or for direct access through
4221 * the GTT either for the user or for scanout). Those VMA still need to
4222 * unbound now.
4223 */
Chris Wilson1488fc02012-04-24 15:47:31 +01004224
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004225 while ((freed = llist_del_all(&i915->mm.free_list)))
4226 __i915_gem_free_objects(i915, freed);
4227}
4228
4229static void __i915_gem_free_object_rcu(struct rcu_head *head)
4230{
4231 struct drm_i915_gem_object *obj =
4232 container_of(head, typeof(*obj), rcu);
4233 struct drm_i915_private *i915 = to_i915(obj->base.dev);
4234
4235 /* We can't simply use call_rcu() from i915_gem_free_object()
4236 * as we need to block whilst unbinding, and the call_rcu
4237 * task may be called from softirq context. So we take a
4238 * detour through a worker.
4239 */
4240 if (llist_add(&obj->freed, &i915->mm.free_list))
4241 schedule_work(&i915->mm.free_work);
4242}
4243
4244void i915_gem_free_object(struct drm_gem_object *gem_obj)
4245{
4246 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
4247
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004248 if (obj->mm.quirked)
4249 __i915_gem_object_unpin_pages(obj);
4250
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004251 if (discard_backing_storage(obj))
4252 obj->mm.madv = I915_MADV_DONTNEED;
Daniel Vettera071fa02014-06-18 23:28:09 +02004253
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004254 /* Before we free the object, make sure any pure RCU-only
4255 * read-side critical sections are complete, e.g.
4256 * i915_gem_busy_ioctl(). For the corresponding synchronized
4257 * lookup see i915_gem_object_lookup_rcu().
4258 */
4259 call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
Chris Wilsonbe726152010-07-23 23:18:50 +01004260}
4261
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004262void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj)
4263{
4264 lockdep_assert_held(&obj->base.dev->struct_mutex);
4265
4266 GEM_BUG_ON(i915_gem_object_has_active_reference(obj));
4267 if (i915_gem_object_is_active(obj))
4268 i915_gem_object_set_active_reference(obj);
4269 else
4270 i915_gem_object_put(obj);
4271}
4272
Chris Wilson3033aca2016-10-28 13:58:47 +01004273static void assert_kernel_context_is_current(struct drm_i915_private *dev_priv)
4274{
4275 struct intel_engine_cs *engine;
4276 enum intel_engine_id id;
4277
4278 for_each_engine(engine, dev_priv, id)
Chris Wilsonf131e352016-12-29 14:40:37 +00004279 GEM_BUG_ON(engine->last_retired_context &&
4280 !i915_gem_context_is_kernel(engine->last_retired_context));
Chris Wilson3033aca2016-10-28 13:58:47 +01004281}
4282
Chris Wilson24145512017-01-24 11:01:35 +00004283void i915_gem_sanitize(struct drm_i915_private *i915)
4284{
4285 /*
4286 * If we inherit context state from the BIOS or earlier occupants
4287 * of the GPU, the GPU may be in an inconsistent state when we
4288 * try to take over. The only way to remove the earlier state
4289 * is by resetting. However, resetting on earlier gen is tricky as
4290 * it may impact the display and we are uncertain about the stability
4291 * of the reset, so we only reset recent machines with logical
4292 * context support (that must be reset to remove any stray contexts).
4293 */
4294 if (HAS_HW_CONTEXTS(i915)) {
4295 int reset = intel_gpu_reset(i915, ALL_ENGINES);
4296 WARN_ON(reset && reset != -ENODEV);
4297 }
4298}
4299
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004300int i915_gem_suspend(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07004301{
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004302 struct drm_device *dev = &dev_priv->drm;
Chris Wilsondcff85c2016-08-05 10:14:11 +01004303 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004304
Chris Wilson54b4f682016-07-21 21:16:19 +01004305 intel_suspend_gt_powersave(dev_priv);
4306
Chris Wilson45c5f202013-10-16 11:50:01 +01004307 mutex_lock(&dev->struct_mutex);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004308
4309 /* We have to flush all the executing contexts to main memory so
4310 * that they can saved in the hibernation image. To ensure the last
4311 * context image is coherent, we have to switch away from it. That
4312 * leaves the dev_priv->kernel_context still active when
4313 * we actually suspend, and its image in memory may not match the GPU
4314 * state. Fortunately, the kernel_context is disposable and we do
4315 * not rely on its state.
4316 */
4317 ret = i915_gem_switch_to_kernel_context(dev_priv);
4318 if (ret)
4319 goto err;
4320
Chris Wilson22dd3bb2016-09-09 14:11:50 +01004321 ret = i915_gem_wait_for_idle(dev_priv,
4322 I915_WAIT_INTERRUPTIBLE |
4323 I915_WAIT_LOCKED);
Chris Wilsonf7403342013-09-13 23:57:04 +01004324 if (ret)
Chris Wilson45c5f202013-10-16 11:50:01 +01004325 goto err;
Chris Wilsonf7403342013-09-13 23:57:04 +01004326
Chris Wilsonc0336662016-05-06 15:40:21 +01004327 i915_gem_retire_requests(dev_priv);
Chris Wilson28176ef2016-10-28 13:58:56 +01004328 GEM_BUG_ON(dev_priv->gt.active_requests);
Eric Anholt673a3942008-07-30 12:06:12 -07004329
Chris Wilson3033aca2016-10-28 13:58:47 +01004330 assert_kernel_context_is_current(dev_priv);
Chris Wilsonb2e862d2016-04-28 09:56:41 +01004331 i915_gem_context_lost(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01004332 mutex_unlock(&dev->struct_mutex);
4333
Chris Wilson737b1502015-01-26 18:03:03 +02004334 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
Chris Wilson67d97da2016-07-04 08:08:31 +01004335 cancel_delayed_work_sync(&dev_priv->gt.retire_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004336
4337 /* As the idle_work is rearming if it detects a race, play safe and
4338 * repeat the flush until it is definitely idle.
4339 */
4340 while (flush_delayed_work(&dev_priv->gt.idle_work))
4341 ;
4342
4343 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson29105cc2010-01-07 10:39:13 +00004344
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004345 /* Assert that we sucessfully flushed all the work and
4346 * reset the GPU back to its idle, low power state.
4347 */
Chris Wilson67d97da2016-07-04 08:08:31 +01004348 WARN_ON(dev_priv->gt.awake);
Imre Deak31ab49a2016-11-07 11:20:05 +02004349 WARN_ON(!intel_execlists_idle(dev_priv));
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004350
Imre Deak1c777c52016-10-12 17:46:37 +03004351 /*
4352 * Neither the BIOS, ourselves or any other kernel
4353 * expects the system to be in execlists mode on startup,
4354 * so we need to reset the GPU back to legacy mode. And the only
4355 * known way to disable logical contexts is through a GPU reset.
4356 *
4357 * So in order to leave the system in a known default configuration,
4358 * always reset the GPU upon unload and suspend. Afterwards we then
4359 * clean up the GEM state tracking, flushing off the requests and
4360 * leaving the system in a known idle state.
4361 *
4362 * Note that is of the upmost importance that the GPU is idle and
4363 * all stray writes are flushed *before* we dismantle the backing
4364 * storage for the pinned objects.
4365 *
4366 * However, since we are uncertain that resetting the GPU on older
4367 * machines is a good idea, we don't - just in case it leaves the
4368 * machine in an unusable condition.
4369 */
Chris Wilson24145512017-01-24 11:01:35 +00004370 i915_gem_sanitize(dev_priv);
Imre Deak1c777c52016-10-12 17:46:37 +03004371
Eric Anholt673a3942008-07-30 12:06:12 -07004372 return 0;
Chris Wilson45c5f202013-10-16 11:50:01 +01004373
4374err:
4375 mutex_unlock(&dev->struct_mutex);
4376 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004377}
4378
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004379void i915_gem_resume(struct drm_i915_private *dev_priv)
Chris Wilson5ab57c72016-07-15 14:56:20 +01004380{
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004381 struct drm_device *dev = &dev_priv->drm;
Chris Wilson5ab57c72016-07-15 14:56:20 +01004382
Imre Deak31ab49a2016-11-07 11:20:05 +02004383 WARN_ON(dev_priv->gt.awake);
4384
Chris Wilson5ab57c72016-07-15 14:56:20 +01004385 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00004386 i915_gem_restore_gtt_mappings(dev_priv);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004387
4388 /* As we didn't flush the kernel context before suspend, we cannot
4389 * guarantee that the context image is complete. So let's just reset
4390 * it and start again.
4391 */
Chris Wilson821ed7d2016-09-09 14:11:53 +01004392 dev_priv->gt.resume(dev_priv);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004393
4394 mutex_unlock(&dev->struct_mutex);
4395}
4396
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004397void i915_gem_init_swizzling(struct drm_i915_private *dev_priv)
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004398{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004399 if (INTEL_GEN(dev_priv) < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004400 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
4401 return;
4402
4403 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
4404 DISP_TILE_SURFACE_SWIZZLING);
4405
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004406 if (IS_GEN5(dev_priv))
Daniel Vetter11782b02012-01-31 16:47:55 +01004407 return;
4408
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004409 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004410 if (IS_GEN6(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004411 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004412 else if (IS_GEN7(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004413 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004414 else if (IS_GEN8(dev_priv))
Ben Widawsky31a53362013-11-02 21:07:04 -07004415 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08004416 else
4417 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004418}
Daniel Vettere21af882012-02-09 20:53:27 +01004419
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004420static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004421{
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004422 I915_WRITE(RING_CTL(base), 0);
4423 I915_WRITE(RING_HEAD(base), 0);
4424 I915_WRITE(RING_TAIL(base), 0);
4425 I915_WRITE(RING_START(base), 0);
4426}
4427
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004428static void init_unused_rings(struct drm_i915_private *dev_priv)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004429{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004430 if (IS_I830(dev_priv)) {
4431 init_unused_ring(dev_priv, PRB1_BASE);
4432 init_unused_ring(dev_priv, SRB0_BASE);
4433 init_unused_ring(dev_priv, SRB1_BASE);
4434 init_unused_ring(dev_priv, SRB2_BASE);
4435 init_unused_ring(dev_priv, SRB3_BASE);
4436 } else if (IS_GEN2(dev_priv)) {
4437 init_unused_ring(dev_priv, SRB0_BASE);
4438 init_unused_ring(dev_priv, SRB1_BASE);
4439 } else if (IS_GEN3(dev_priv)) {
4440 init_unused_ring(dev_priv, PRB1_BASE);
4441 init_unused_ring(dev_priv, PRB2_BASE);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004442 }
4443}
4444
Chris Wilson20a8a742017-02-08 14:30:31 +00004445static int __i915_gem_restart_engines(void *data)
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004446{
Chris Wilson20a8a742017-02-08 14:30:31 +00004447 struct drm_i915_private *i915 = data;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004448 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05304449 enum intel_engine_id id;
Chris Wilson20a8a742017-02-08 14:30:31 +00004450 int err;
4451
4452 for_each_engine(engine, i915, id) {
4453 err = engine->init_hw(engine);
4454 if (err)
4455 return err;
4456 }
4457
4458 return 0;
4459}
4460
4461int i915_gem_init_hw(struct drm_i915_private *dev_priv)
4462{
Chris Wilsond200cda2016-04-28 09:56:44 +01004463 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004464
Chris Wilsonde867c22016-10-25 13:16:02 +01004465 dev_priv->gt.last_init_time = ktime_get();
4466
Chris Wilson5e4f5182015-02-13 14:35:59 +00004467 /* Double layer security blanket, see i915_gem_init() */
4468 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4469
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00004470 if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07004471 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004472
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01004473 if (IS_HASWELL(dev_priv))
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004474 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
Ville Syrjälä0bf21342013-11-29 14:56:12 +02004475 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03004476
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004477 if (HAS_PCH_NOP(dev_priv)) {
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01004478 if (IS_IVYBRIDGE(dev_priv)) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01004479 u32 temp = I915_READ(GEN7_MSG_CTL);
4480 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
4481 I915_WRITE(GEN7_MSG_CTL, temp);
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004482 } else if (INTEL_GEN(dev_priv) >= 7) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01004483 u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
4484 temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
4485 I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
4486 }
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07004487 }
4488
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004489 i915_gem_init_swizzling(dev_priv);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004490
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004491 /*
4492 * At least 830 can leave some of the unused rings
4493 * "active" (ie. head != tail) after resume which
4494 * will prevent c3 entry. Makes sure all unused rings
4495 * are totally idle.
4496 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004497 init_unused_rings(dev_priv);
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004498
Dave Gordoned54c1a2016-01-19 19:02:54 +00004499 BUG_ON(!dev_priv->kernel_context);
John Harrison90638cc2015-05-29 17:43:37 +01004500
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004501 ret = i915_ppgtt_init_hw(dev_priv);
John Harrison4ad2fd82015-06-18 13:11:20 +01004502 if (ret) {
4503 DRM_ERROR("PPGTT enable HW failed %d\n", ret);
4504 goto out;
4505 }
4506
4507 /* Need to do basic initialisation of all rings first: */
Chris Wilson20a8a742017-02-08 14:30:31 +00004508 ret = __i915_gem_restart_engines(dev_priv);
4509 if (ret)
4510 goto out;
Mika Kuoppala99433932013-01-22 14:12:17 +02004511
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004512 intel_mocs_init_l3cc_table(dev_priv);
Peter Antoine0ccdacf2016-04-13 15:03:25 +01004513
Alex Dai33a732f2015-08-12 15:43:36 +01004514 /* We can't enable contexts until all firmware is loaded */
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004515 ret = intel_guc_setup(dev_priv);
Dave Gordone556f7c2016-06-07 09:14:49 +01004516 if (ret)
4517 goto out;
Alex Dai33a732f2015-08-12 15:43:36 +01004518
Chris Wilson5e4f5182015-02-13 14:35:59 +00004519out:
4520 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004521 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004522}
4523
Chris Wilson39df9192016-07-20 13:31:57 +01004524bool intel_sanitize_semaphores(struct drm_i915_private *dev_priv, int value)
4525{
4526 if (INTEL_INFO(dev_priv)->gen < 6)
4527 return false;
4528
4529 /* TODO: make semaphores and Execlists play nicely together */
4530 if (i915.enable_execlists)
4531 return false;
4532
4533 if (value >= 0)
4534 return value;
4535
4536#ifdef CONFIG_INTEL_IOMMU
4537 /* Enable semaphores on SNB when IO remapping is off */
4538 if (INTEL_INFO(dev_priv)->gen == 6 && intel_iommu_gfx_mapped)
4539 return false;
4540#endif
4541
4542 return true;
4543}
4544
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004545int i915_gem_init(struct drm_i915_private *dev_priv)
Chris Wilson1070a422012-04-24 15:47:41 +01004546{
Chris Wilson1070a422012-04-24 15:47:41 +01004547 int ret;
4548
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004549 mutex_lock(&dev_priv->drm.struct_mutex);
Jesse Barnesd62b4892013-03-08 10:45:53 -08004550
Oscar Mateoa83014d2014-07-24 17:04:21 +01004551 if (!i915.enable_execlists) {
Chris Wilson821ed7d2016-09-09 14:11:53 +01004552 dev_priv->gt.resume = intel_legacy_submission_resume;
Chris Wilson7e37f882016-08-02 22:50:21 +01004553 dev_priv->gt.cleanup_engine = intel_engine_cleanup;
Oscar Mateo454afeb2014-07-24 17:04:22 +01004554 } else {
Chris Wilson821ed7d2016-09-09 14:11:53 +01004555 dev_priv->gt.resume = intel_lr_context_resume;
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004556 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
Oscar Mateoa83014d2014-07-24 17:04:21 +01004557 }
4558
Chris Wilson5e4f5182015-02-13 14:35:59 +00004559 /* This is just a security blanket to placate dragons.
4560 * On some systems, we very sporadically observe that the first TLBs
4561 * used by the CS may be stale, despite us poking the TLB reset. If
4562 * we hold the forcewake during initialisation these problems
4563 * just magically go away.
4564 */
4565 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4566
Chris Wilson72778cb2016-05-19 16:17:16 +01004567 i915_gem_init_userptr(dev_priv);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01004568
4569 ret = i915_gem_init_ggtt(dev_priv);
4570 if (ret)
4571 goto out_unlock;
Jesse Barnesd62b4892013-03-08 10:45:53 -08004572
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004573 ret = i915_gem_context_init(dev_priv);
Jani Nikula7bcc3772014-12-05 14:17:42 +02004574 if (ret)
4575 goto out_unlock;
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004576
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004577 ret = intel_engines_init(dev_priv);
Daniel Vetter35a57ff2014-11-20 00:33:07 +01004578 if (ret)
Jani Nikula7bcc3772014-12-05 14:17:42 +02004579 goto out_unlock;
Daniel Vetter53ca26c2012-04-26 23:28:03 +02004580
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004581 ret = i915_gem_init_hw(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01004582 if (ret == -EIO) {
Chris Wilson7e21d642016-07-27 09:07:29 +01004583 /* Allow engine initialisation to fail by marking the GPU as
Chris Wilson60990322014-04-09 09:19:42 +01004584 * wedged. But we only want to do this where the GPU is angry,
4585 * for all other failure, such as an allocation failure, bail.
4586 */
4587 DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
Chris Wilson821ed7d2016-09-09 14:11:53 +01004588 i915_gem_set_wedged(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01004589 ret = 0;
Chris Wilson1070a422012-04-24 15:47:41 +01004590 }
Jani Nikula7bcc3772014-12-05 14:17:42 +02004591
4592out_unlock:
Chris Wilson5e4f5182015-02-13 14:35:59 +00004593 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004594 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson1070a422012-04-24 15:47:41 +01004595
Chris Wilson60990322014-04-09 09:19:42 +01004596 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01004597}
4598
Chris Wilson24145512017-01-24 11:01:35 +00004599void i915_gem_init_mmio(struct drm_i915_private *i915)
4600{
4601 i915_gem_sanitize(i915);
4602}
4603
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004604void
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00004605i915_gem_cleanup_engines(struct drm_i915_private *dev_priv)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004606{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004607 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05304608 enum intel_engine_id id;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004609
Akash Goel3b3f1652016-10-13 22:44:48 +05304610 for_each_engine(engine, dev_priv, id)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004611 dev_priv->gt.cleanup_engine(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004612}
4613
Eric Anholt673a3942008-07-30 12:06:12 -07004614void
Imre Deak40ae4e12016-03-16 14:54:03 +02004615i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
4616{
Chris Wilson49ef5292016-08-18 17:17:00 +01004617 int i;
Imre Deak40ae4e12016-03-16 14:54:03 +02004618
4619 if (INTEL_INFO(dev_priv)->gen >= 7 && !IS_VALLEYVIEW(dev_priv) &&
4620 !IS_CHERRYVIEW(dev_priv))
4621 dev_priv->num_fence_regs = 32;
Jani Nikula73f67aa2016-12-07 22:48:09 +02004622 else if (INTEL_INFO(dev_priv)->gen >= 4 ||
4623 IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
4624 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02004625 dev_priv->num_fence_regs = 16;
4626 else
4627 dev_priv->num_fence_regs = 8;
4628
Chris Wilsonc0336662016-05-06 15:40:21 +01004629 if (intel_vgpu_active(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02004630 dev_priv->num_fence_regs =
4631 I915_READ(vgtif_reg(avail_rs.fence_num));
4632
4633 /* Initialize fence registers to zero */
Chris Wilson49ef5292016-08-18 17:17:00 +01004634 for (i = 0; i < dev_priv->num_fence_regs; i++) {
4635 struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
4636
4637 fence->i915 = dev_priv;
4638 fence->id = i;
4639 list_add_tail(&fence->link, &dev_priv->mm.fence_list);
4640 }
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00004641 i915_gem_restore_fences(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02004642
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00004643 i915_gem_detect_bit_6_swizzle(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02004644}
4645
Chris Wilson73cb9702016-10-28 13:58:46 +01004646int
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00004647i915_gem_load_init(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07004648{
Tvrtko Ursulina9335682016-11-02 15:14:59 +00004649 int err = -ENOMEM;
Chris Wilson42dcedd2012-11-15 11:32:30 +00004650
Tvrtko Ursulina9335682016-11-02 15:14:59 +00004651 dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
4652 if (!dev_priv->objects)
Chris Wilson73cb9702016-10-28 13:58:46 +01004653 goto err_out;
Chris Wilson73cb9702016-10-28 13:58:46 +01004654
Tvrtko Ursulina9335682016-11-02 15:14:59 +00004655 dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
4656 if (!dev_priv->vmas)
Chris Wilson73cb9702016-10-28 13:58:46 +01004657 goto err_objects;
Chris Wilson73cb9702016-10-28 13:58:46 +01004658
Tvrtko Ursulina9335682016-11-02 15:14:59 +00004659 dev_priv->requests = KMEM_CACHE(drm_i915_gem_request,
4660 SLAB_HWCACHE_ALIGN |
4661 SLAB_RECLAIM_ACCOUNT |
4662 SLAB_DESTROY_BY_RCU);
4663 if (!dev_priv->requests)
Chris Wilson73cb9702016-10-28 13:58:46 +01004664 goto err_vmas;
Chris Wilson73cb9702016-10-28 13:58:46 +01004665
Chris Wilson52e54202016-11-14 20:41:02 +00004666 dev_priv->dependencies = KMEM_CACHE(i915_dependency,
4667 SLAB_HWCACHE_ALIGN |
4668 SLAB_RECLAIM_ACCOUNT);
4669 if (!dev_priv->dependencies)
4670 goto err_requests;
4671
Chris Wilson73cb9702016-10-28 13:58:46 +01004672 mutex_lock(&dev_priv->drm.struct_mutex);
4673 INIT_LIST_HEAD(&dev_priv->gt.timelines);
Chris Wilsonbb894852016-11-14 20:40:57 +00004674 err = i915_gem_timeline_init__global(dev_priv);
Chris Wilson73cb9702016-10-28 13:58:46 +01004675 mutex_unlock(&dev_priv->drm.struct_mutex);
4676 if (err)
Chris Wilson52e54202016-11-14 20:41:02 +00004677 goto err_dependencies;
Eric Anholt673a3942008-07-30 12:06:12 -07004678
Ben Widawskya33afea2013-09-17 21:12:45 -07004679 INIT_LIST_HEAD(&dev_priv->context_list);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004680 INIT_WORK(&dev_priv->mm.free_work, __i915_gem_free_work);
4681 init_llist_head(&dev_priv->mm.free_list);
Chris Wilson6c085a72012-08-20 11:40:46 +02004682 INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
4683 INIT_LIST_HEAD(&dev_priv->mm.bound_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004684 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilson275f0392016-10-24 13:42:14 +01004685 INIT_LIST_HEAD(&dev_priv->mm.userfault_list);
Chris Wilson67d97da2016-07-04 08:08:31 +01004686 INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
Eric Anholt673a3942008-07-30 12:06:12 -07004687 i915_gem_retire_work_handler);
Chris Wilson67d97da2016-07-04 08:08:31 +01004688 INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004689 i915_gem_idle_work_handler);
Chris Wilson1f15b762016-07-01 17:23:14 +01004690 init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01004691 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson31169712009-09-14 16:50:28 +01004692
Chris Wilson72bfa192010-12-19 11:42:05 +00004693 dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
4694
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004695 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01004696
Chris Wilsonce453d82011-02-21 14:43:56 +00004697 dev_priv->mm.interruptible = true;
4698
Joonas Lahtinen6f633402016-09-01 14:58:21 +03004699 atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
4700
Chris Wilsonb5add952016-08-04 16:32:36 +01004701 spin_lock_init(&dev_priv->fb_tracking.lock);
Chris Wilson73cb9702016-10-28 13:58:46 +01004702
4703 return 0;
4704
Chris Wilson52e54202016-11-14 20:41:02 +00004705err_dependencies:
4706 kmem_cache_destroy(dev_priv->dependencies);
Chris Wilson73cb9702016-10-28 13:58:46 +01004707err_requests:
4708 kmem_cache_destroy(dev_priv->requests);
4709err_vmas:
4710 kmem_cache_destroy(dev_priv->vmas);
4711err_objects:
4712 kmem_cache_destroy(dev_priv->objects);
4713err_out:
4714 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07004715}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004716
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00004717void i915_gem_load_cleanup(struct drm_i915_private *dev_priv)
Imre Deakd64aa092016-01-19 15:26:29 +02004718{
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00004719 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson7d5d59e2016-11-01 08:48:41 +00004720 WARN_ON(!llist_empty(&dev_priv->mm.free_list));
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00004721 WARN_ON(dev_priv->mm.object_count);
Chris Wilson7d5d59e2016-11-01 08:48:41 +00004722
Matthew Auldea84aa72016-11-17 21:04:11 +00004723 mutex_lock(&dev_priv->drm.struct_mutex);
4724 i915_gem_timeline_fini(&dev_priv->gt.global_timeline);
4725 WARN_ON(!list_empty(&dev_priv->gt.timelines));
4726 mutex_unlock(&dev_priv->drm.struct_mutex);
4727
Chris Wilson52e54202016-11-14 20:41:02 +00004728 kmem_cache_destroy(dev_priv->dependencies);
Imre Deakd64aa092016-01-19 15:26:29 +02004729 kmem_cache_destroy(dev_priv->requests);
4730 kmem_cache_destroy(dev_priv->vmas);
4731 kmem_cache_destroy(dev_priv->objects);
Chris Wilson0eafec62016-08-04 16:32:41 +01004732
4733 /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
4734 rcu_barrier();
Imre Deakd64aa092016-01-19 15:26:29 +02004735}
4736
Chris Wilson6a800ea2016-09-21 14:51:07 +01004737int i915_gem_freeze(struct drm_i915_private *dev_priv)
4738{
Chris Wilson6a800ea2016-09-21 14:51:07 +01004739 mutex_lock(&dev_priv->drm.struct_mutex);
4740 i915_gem_shrink_all(dev_priv);
4741 mutex_unlock(&dev_priv->drm.struct_mutex);
4742
Chris Wilson6a800ea2016-09-21 14:51:07 +01004743 return 0;
4744}
4745
Chris Wilson461fb992016-05-14 07:26:33 +01004746int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
4747{
4748 struct drm_i915_gem_object *obj;
Chris Wilson7aab2d52016-09-09 20:02:18 +01004749 struct list_head *phases[] = {
4750 &dev_priv->mm.unbound_list,
4751 &dev_priv->mm.bound_list,
4752 NULL
4753 }, **p;
Chris Wilson461fb992016-05-14 07:26:33 +01004754
4755 /* Called just before we write the hibernation image.
4756 *
4757 * We need to update the domain tracking to reflect that the CPU
4758 * will be accessing all the pages to create and restore from the
4759 * hibernation, and so upon restoration those pages will be in the
4760 * CPU domain.
4761 *
4762 * To make sure the hibernation image contains the latest state,
4763 * we update that state just before writing out the image.
Chris Wilson7aab2d52016-09-09 20:02:18 +01004764 *
4765 * To try and reduce the hibernation image, we manually shrink
4766 * the objects as well.
Chris Wilson461fb992016-05-14 07:26:33 +01004767 */
4768
Chris Wilson6a800ea2016-09-21 14:51:07 +01004769 mutex_lock(&dev_priv->drm.struct_mutex);
4770 i915_gem_shrink(dev_priv, -1UL, I915_SHRINK_UNBOUND);
Chris Wilson461fb992016-05-14 07:26:33 +01004771
Chris Wilson7aab2d52016-09-09 20:02:18 +01004772 for (p = phases; *p; p++) {
Joonas Lahtinen56cea322016-11-02 12:16:04 +02004773 list_for_each_entry(obj, *p, global_link) {
Chris Wilson7aab2d52016-09-09 20:02:18 +01004774 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4775 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4776 }
Chris Wilson461fb992016-05-14 07:26:33 +01004777 }
Chris Wilson6a800ea2016-09-21 14:51:07 +01004778 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson461fb992016-05-14 07:26:33 +01004779
4780 return 0;
4781}
4782
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004783void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004784{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004785 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilson15f7bbc2016-07-26 12:01:52 +01004786 struct drm_i915_gem_request *request;
Eric Anholtb9624422009-06-03 07:27:35 +00004787
4788 /* Clean up our request list when the client is going away, so that
4789 * later retire_requests won't dereference our soon-to-be-gone
4790 * file_priv.
4791 */
Chris Wilson1c255952010-09-26 11:03:27 +01004792 spin_lock(&file_priv->mm.lock);
Chris Wilson15f7bbc2016-07-26 12:01:52 +01004793 list_for_each_entry(request, &file_priv->mm.request_list, client_list)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004794 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01004795 spin_unlock(&file_priv->mm.lock);
Chris Wilson31169712009-09-14 16:50:28 +01004796
Chris Wilson2e1b8732015-04-27 13:41:22 +01004797 if (!list_empty(&file_priv->rps.link)) {
Chris Wilson8d3afd72015-05-21 21:01:47 +01004798 spin_lock(&to_i915(dev)->rps.client_lock);
Chris Wilson2e1b8732015-04-27 13:41:22 +01004799 list_del(&file_priv->rps.link);
Chris Wilson8d3afd72015-05-21 21:01:47 +01004800 spin_unlock(&to_i915(dev)->rps.client_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01004801 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004802}
4803
4804int i915_gem_open(struct drm_device *dev, struct drm_file *file)
4805{
4806 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08004807 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004808
Chris Wilsonc4c29d72016-11-09 10:45:07 +00004809 DRM_DEBUG("\n");
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004810
4811 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
4812 if (!file_priv)
4813 return -ENOMEM;
4814
4815 file->driver_priv = file_priv;
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004816 file_priv->dev_priv = to_i915(dev);
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02004817 file_priv->file = file;
Chris Wilson2e1b8732015-04-27 13:41:22 +01004818 INIT_LIST_HEAD(&file_priv->rps.link);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004819
4820 spin_lock_init(&file_priv->mm.lock);
4821 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004822
Chris Wilsonc80ff162016-07-27 09:07:27 +01004823 file_priv->bsd_engine = -1;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00004824
Ben Widawskye422b882013-12-06 14:10:58 -08004825 ret = i915_gem_context_open(dev, file);
4826 if (ret)
4827 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004828
Ben Widawskye422b882013-12-06 14:10:58 -08004829 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004830}
4831
Daniel Vetterb680c372014-09-19 18:27:27 +02004832/**
4833 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07004834 * @old: current GEM buffer for the frontbuffer slots
4835 * @new: new GEM buffer for the frontbuffer slots
4836 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02004837 *
4838 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
4839 * from @old and setting them in @new. Both @old and @new can be NULL.
4840 */
Daniel Vettera071fa02014-06-18 23:28:09 +02004841void i915_gem_track_fb(struct drm_i915_gem_object *old,
4842 struct drm_i915_gem_object *new,
4843 unsigned frontbuffer_bits)
4844{
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01004845 /* Control of individual bits within the mask are guarded by
4846 * the owning plane->mutex, i.e. we can never see concurrent
4847 * manipulation of individual bits. But since the bitfield as a whole
4848 * is updated using RMW, we need to use atomics in order to update
4849 * the bits.
4850 */
4851 BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
4852 sizeof(atomic_t) * BITS_PER_BYTE);
4853
Daniel Vettera071fa02014-06-18 23:28:09 +02004854 if (old) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01004855 WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
4856 atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02004857 }
4858
4859 if (new) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01004860 WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
4861 atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02004862 }
4863}
4864
Dave Gordonea702992015-07-09 19:29:02 +01004865/* Allocate a new GEM object and fill it with the supplied data */
4866struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004867i915_gem_object_create_from_data(struct drm_i915_private *dev_priv,
Dave Gordonea702992015-07-09 19:29:02 +01004868 const void *data, size_t size)
4869{
4870 struct drm_i915_gem_object *obj;
4871 struct sg_table *sg;
4872 size_t bytes;
4873 int ret;
4874
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004875 obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE));
Chris Wilsonfe3db792016-04-25 13:32:13 +01004876 if (IS_ERR(obj))
Dave Gordonea702992015-07-09 19:29:02 +01004877 return obj;
4878
4879 ret = i915_gem_object_set_to_cpu_domain(obj, true);
4880 if (ret)
4881 goto fail;
4882
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004883 ret = i915_gem_object_pin_pages(obj);
Dave Gordonea702992015-07-09 19:29:02 +01004884 if (ret)
4885 goto fail;
4886
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004887 sg = obj->mm.pages;
Dave Gordonea702992015-07-09 19:29:02 +01004888 bytes = sg_copy_from_buffer(sg->sgl, sg->nents, (void *)data, size);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004889 obj->mm.dirty = true; /* Backing store is now out of date */
Dave Gordonea702992015-07-09 19:29:02 +01004890 i915_gem_object_unpin_pages(obj);
4891
4892 if (WARN_ON(bytes != size)) {
4893 DRM_ERROR("Incomplete copy, wrote %zu of %zu", bytes, size);
4894 ret = -EFAULT;
4895 goto fail;
4896 }
4897
4898 return obj;
4899
4900fail:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004901 i915_gem_object_put(obj);
Dave Gordonea702992015-07-09 19:29:02 +01004902 return ERR_PTR(ret);
4903}
Chris Wilson96d77632016-10-28 13:58:33 +01004904
4905struct scatterlist *
4906i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
4907 unsigned int n,
4908 unsigned int *offset)
4909{
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004910 struct i915_gem_object_page_iter *iter = &obj->mm.get_page;
Chris Wilson96d77632016-10-28 13:58:33 +01004911 struct scatterlist *sg;
4912 unsigned int idx, count;
4913
4914 might_sleep();
4915 GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004916 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
Chris Wilson96d77632016-10-28 13:58:33 +01004917
4918 /* As we iterate forward through the sg, we record each entry in a
4919 * radixtree for quick repeated (backwards) lookups. If we have seen
4920 * this index previously, we will have an entry for it.
4921 *
4922 * Initial lookup is O(N), but this is amortized to O(1) for
4923 * sequential page access (where each new request is consecutive
4924 * to the previous one). Repeated lookups are O(lg(obj->base.size)),
4925 * i.e. O(1) with a large constant!
4926 */
4927 if (n < READ_ONCE(iter->sg_idx))
4928 goto lookup;
4929
4930 mutex_lock(&iter->lock);
4931
4932 /* We prefer to reuse the last sg so that repeated lookup of this
4933 * (or the subsequent) sg are fast - comparing against the last
4934 * sg is faster than going through the radixtree.
4935 */
4936
4937 sg = iter->sg_pos;
4938 idx = iter->sg_idx;
4939 count = __sg_page_count(sg);
4940
4941 while (idx + count <= n) {
4942 unsigned long exception, i;
4943 int ret;
4944
4945 /* If we cannot allocate and insert this entry, or the
4946 * individual pages from this range, cancel updating the
4947 * sg_idx so that on this lookup we are forced to linearly
4948 * scan onwards, but on future lookups we will try the
4949 * insertion again (in which case we need to be careful of
4950 * the error return reporting that we have already inserted
4951 * this index).
4952 */
4953 ret = radix_tree_insert(&iter->radix, idx, sg);
4954 if (ret && ret != -EEXIST)
4955 goto scan;
4956
4957 exception =
4958 RADIX_TREE_EXCEPTIONAL_ENTRY |
4959 idx << RADIX_TREE_EXCEPTIONAL_SHIFT;
4960 for (i = 1; i < count; i++) {
4961 ret = radix_tree_insert(&iter->radix, idx + i,
4962 (void *)exception);
4963 if (ret && ret != -EEXIST)
4964 goto scan;
4965 }
4966
4967 idx += count;
4968 sg = ____sg_next(sg);
4969 count = __sg_page_count(sg);
4970 }
4971
4972scan:
4973 iter->sg_pos = sg;
4974 iter->sg_idx = idx;
4975
4976 mutex_unlock(&iter->lock);
4977
4978 if (unlikely(n < idx)) /* insertion completed by another thread */
4979 goto lookup;
4980
4981 /* In case we failed to insert the entry into the radixtree, we need
4982 * to look beyond the current sg.
4983 */
4984 while (idx + count <= n) {
4985 idx += count;
4986 sg = ____sg_next(sg);
4987 count = __sg_page_count(sg);
4988 }
4989
4990 *offset = n - idx;
4991 return sg;
4992
4993lookup:
4994 rcu_read_lock();
4995
4996 sg = radix_tree_lookup(&iter->radix, n);
4997 GEM_BUG_ON(!sg);
4998
4999 /* If this index is in the middle of multi-page sg entry,
5000 * the radixtree will contain an exceptional entry that points
5001 * to the start of that range. We will return the pointer to
5002 * the base page and the offset of this page within the
5003 * sg entry's range.
5004 */
5005 *offset = 0;
5006 if (unlikely(radix_tree_exception(sg))) {
5007 unsigned long base =
5008 (unsigned long)sg >> RADIX_TREE_EXCEPTIONAL_SHIFT;
5009
5010 sg = radix_tree_lookup(&iter->radix, base);
5011 GEM_BUG_ON(!sg);
5012
5013 *offset = n - base;
5014 }
5015
5016 rcu_read_unlock();
5017
5018 return sg;
5019}
5020
5021struct page *
5022i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
5023{
5024 struct scatterlist *sg;
5025 unsigned int offset;
5026
5027 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
5028
5029 sg = i915_gem_object_get_sg(obj, n, &offset);
5030 return nth_page(sg_page(sg), offset);
5031}
5032
5033/* Like i915_gem_object_get_page(), but mark the returned page dirty */
5034struct page *
5035i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
5036 unsigned int n)
5037{
5038 struct page *page;
5039
5040 page = i915_gem_object_get_page(obj, n);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005041 if (!obj->mm.dirty)
Chris Wilson96d77632016-10-28 13:58:33 +01005042 set_page_dirty(page);
5043
5044 return page;
5045}
5046
5047dma_addr_t
5048i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
5049 unsigned long n)
5050{
5051 struct scatterlist *sg;
5052 unsigned int offset;
5053
5054 sg = i915_gem_object_get_sg(obj, n, &offset);
5055 return sg_dma_address(sg) + (offset << PAGE_SHIFT);
5056}
Chris Wilson935a2f72017-02-13 17:15:13 +00005057
5058#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
5059#include "selftests/scatterlist.c"
Chris Wilson66d9cb52017-02-13 17:15:17 +00005060#include "selftests/mock_gem_device.c"
Chris Wilson44653982017-02-13 17:15:20 +00005061#include "selftests/huge_gem_object.c"
Chris Wilson8335fd62017-02-13 17:15:28 +00005062#include "selftests/i915_gem_object.c"
Chris Wilson17059452017-02-13 17:15:32 +00005063#include "selftests/i915_gem_coherency.c"
Chris Wilson935a2f72017-02-13 17:15:13 +00005064#endif