blob: 54732ff6b551821a42d70862d510c09c5074daf6 [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
Daniel Vetterbe6a0372015-03-18 10:46:04 +01002 * Copyright © 2008-2015 Intel Corporation
Eric Anholt673a3942008-07-30 12:06:12 -07003 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
David Howells760285e2012-10-02 18:01:07 +010028#include <drm/drmP.h>
David Herrmann0de23972013-07-24 21:07:52 +020029#include <drm/drm_vma_manager.h>
David Howells760285e2012-10-02 18:01:07 +010030#include <drm/i915_drm.h>
Eric Anholt673a3942008-07-30 12:06:12 -070031#include "i915_drv.h"
Chris Wilsonc13d87e2016-07-20 09:21:15 +010032#include "i915_gem_dmabuf.h"
Yu Zhangeb822892015-02-10 19:05:49 +080033#include "i915_vgpu.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010034#include "i915_trace.h"
Jesse Barnes652c3932009-08-17 13:31:43 -070035#include "intel_drv.h"
Peter Antoine0ccdacf2016-04-13 15:03:25 +010036#include "intel_mocs.h"
Chris Wilsonc13d87e2016-07-20 09:21:15 +010037#include <linux/reservation.h>
Hugh Dickins5949eac2011-06-27 16:18:18 -070038#include <linux/shmem_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Eric Anholt673a3942008-07-30 12:06:12 -070040#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080041#include <linux/pci.h>
Daniel Vetter1286ff72012-05-10 15:25:09 +020042#include <linux/dma-buf.h>
Eric Anholt673a3942008-07-30 12:06:12 -070043
Chris Wilson05394f32010-11-08 19:18:58 +000044static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
Daniel Vettere62b59e2015-01-21 14:53:48 +010045static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
Chris Wilsonc8725f32014-03-17 12:21:55 +000046static void
Chris Wilsonb4716182015-04-27 13:41:17 +010047i915_gem_object_retire__write(struct drm_i915_gem_object *obj);
48static void
Chris Wilson7e21d642016-07-27 09:07:29 +010049i915_gem_object_retire__read(struct drm_i915_gem_object *obj, int engine);
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{
54 return HAS_LLC(dev) || level != I915_CACHE_NONE;
55}
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
69insert_mappable_node(struct drm_i915_private *i915,
70 struct drm_mm_node *node, u32 size)
71{
72 memset(node, 0, sizeof(*node));
73 return drm_mm_insert_node_in_range_generic(&i915->ggtt.base.mm, node,
74 size, 0, 0, 0,
75 i915->ggtt.mappable_end,
76 DRM_MM_SEARCH_DEFAULT,
77 DRM_MM_CREATE_DEFAULT);
78}
79
80static void
81remove_mappable_node(struct drm_mm_node *node)
82{
83 drm_mm_remove_node(node);
84}
85
Chris Wilson73aa8082010-09-30 11:46:12 +010086/* some bookkeeping */
87static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
88 size_t size)
89{
Daniel Vetterc20e8352013-07-24 22:40:23 +020090 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010091 dev_priv->mm.object_count++;
92 dev_priv->mm.object_memory += size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020093 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010094}
95
96static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
97 size_t size)
98{
Daniel Vetterc20e8352013-07-24 22:40:23 +020099 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +0100100 dev_priv->mm.object_count--;
101 dev_priv->mm.object_memory -= size;
Daniel Vetterc20e8352013-07-24 22:40:23 +0200102 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +0100103}
104
Chris Wilson21dd3732011-01-26 15:55:56 +0000105static int
Daniel Vetter33196de2012-11-14 17:14:05 +0100106i915_gem_wait_for_error(struct i915_gpu_error *error)
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100107{
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100108 int ret;
109
Chris Wilsond98c52c2016-04-13 17:35:05 +0100110 if (!i915_reset_in_progress(error))
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100111 return 0;
112
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200113 /*
114 * Only wait 10 seconds for the gpu reset to complete to avoid hanging
115 * userspace. If it takes that long something really bad is going on and
116 * we should simply try to bail out and fail as gracefully as possible.
117 */
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100118 ret = wait_event_interruptible_timeout(error->reset_queue,
Chris Wilsond98c52c2016-04-13 17:35:05 +0100119 !i915_reset_in_progress(error),
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100120 10*HZ);
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200121 if (ret == 0) {
122 DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
123 return -EIO;
124 } else if (ret < 0) {
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100125 return ret;
Chris Wilsond98c52c2016-04-13 17:35:05 +0100126 } else {
127 return 0;
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200128 }
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100129}
130
Chris Wilson54cf91d2010-11-25 18:00:26 +0000131int i915_mutex_lock_interruptible(struct drm_device *dev)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100132{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100133 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100134 int ret;
135
Daniel Vetter33196de2012-11-14 17:14:05 +0100136 ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100137 if (ret)
138 return ret;
139
140 ret = mutex_lock_interruptible(&dev->struct_mutex);
141 if (ret)
142 return ret;
143
Chris Wilson23bc5982010-09-29 16:10:57 +0100144 WARN_ON(i915_verify_lists(dev));
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)
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100161 if (vma->pin_count)
162 pinned += vma->node.size;
Chris Wilson1c7f4bc2016-02-26 11:03:19 +0000163 list_for_each_entry(vma, &ggtt->base.inactive_list, vm_link)
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100164 if (vma->pin_count)
165 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 Wilson6a2c4232014-11-04 04:51:40 -0800174static int
175i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
Chris Wilson00731152014-05-21 12:42:56 +0100176{
Chris Wilson6a2c4232014-11-04 04:51:40 -0800177 struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
178 char *vaddr = obj->phys_handle->vaddr;
179 struct sg_table *st;
180 struct scatterlist *sg;
181 int i;
Chris Wilson00731152014-05-21 12:42:56 +0100182
Chris Wilson6a2c4232014-11-04 04:51:40 -0800183 if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
184 return -EINVAL;
Chris Wilson00731152014-05-21 12:42:56 +0100185
Chris Wilson6a2c4232014-11-04 04:51:40 -0800186 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
187 struct page *page;
188 char *src;
189
190 page = shmem_read_mapping_page(mapping, i);
191 if (IS_ERR(page))
192 return PTR_ERR(page);
193
194 src = kmap_atomic(page);
195 memcpy(vaddr, src, PAGE_SIZE);
196 drm_clflush_virt_range(vaddr, PAGE_SIZE);
197 kunmap_atomic(src);
198
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300199 put_page(page);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800200 vaddr += PAGE_SIZE;
201 }
202
Chris Wilsonc0336662016-05-06 15:40:21 +0100203 i915_gem_chipset_flush(to_i915(obj->base.dev));
Chris Wilson6a2c4232014-11-04 04:51:40 -0800204
205 st = kmalloc(sizeof(*st), GFP_KERNEL);
206 if (st == NULL)
207 return -ENOMEM;
208
209 if (sg_alloc_table(st, 1, GFP_KERNEL)) {
210 kfree(st);
211 return -ENOMEM;
212 }
213
214 sg = st->sgl;
215 sg->offset = 0;
216 sg->length = obj->base.size;
217
218 sg_dma_address(sg) = obj->phys_handle->busaddr;
219 sg_dma_len(sg) = obj->base.size;
220
221 obj->pages = st;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800222 return 0;
223}
224
225static void
226i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj)
227{
228 int ret;
229
230 BUG_ON(obj->madv == __I915_MADV_PURGED);
231
232 ret = i915_gem_object_set_to_cpu_domain(obj, true);
Chris Wilsonf4457ae2016-04-13 17:35:08 +0100233 if (WARN_ON(ret)) {
Chris Wilson6a2c4232014-11-04 04:51:40 -0800234 /* In the event of a disaster, abandon all caches and
235 * hope for the best.
236 */
Chris Wilson6a2c4232014-11-04 04:51:40 -0800237 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
238 }
239
240 if (obj->madv == I915_MADV_DONTNEED)
241 obj->dirty = 0;
242
243 if (obj->dirty) {
Chris Wilson00731152014-05-21 12:42:56 +0100244 struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800245 char *vaddr = obj->phys_handle->vaddr;
Chris Wilson00731152014-05-21 12:42:56 +0100246 int i;
247
248 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
Chris Wilson6a2c4232014-11-04 04:51:40 -0800249 struct page *page;
250 char *dst;
Chris Wilson00731152014-05-21 12:42:56 +0100251
Chris Wilson6a2c4232014-11-04 04:51:40 -0800252 page = shmem_read_mapping_page(mapping, i);
253 if (IS_ERR(page))
254 continue;
255
256 dst = kmap_atomic(page);
257 drm_clflush_virt_range(vaddr, PAGE_SIZE);
258 memcpy(dst, vaddr, PAGE_SIZE);
259 kunmap_atomic(dst);
260
261 set_page_dirty(page);
262 if (obj->madv == I915_MADV_WILLNEED)
Chris Wilson00731152014-05-21 12:42:56 +0100263 mark_page_accessed(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300264 put_page(page);
Chris Wilson00731152014-05-21 12:42:56 +0100265 vaddr += PAGE_SIZE;
266 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800267 obj->dirty = 0;
Chris Wilson00731152014-05-21 12:42:56 +0100268 }
269
Chris Wilson6a2c4232014-11-04 04:51:40 -0800270 sg_free_table(obj->pages);
271 kfree(obj->pages);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800272}
273
274static void
275i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
276{
277 drm_pci_free(obj->base.dev, obj->phys_handle);
278}
279
280static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
281 .get_pages = i915_gem_object_get_pages_phys,
282 .put_pages = i915_gem_object_put_pages_phys,
283 .release = i915_gem_object_release_phys,
284};
285
Chris Wilsonaa653a62016-08-04 07:52:27 +0100286int
287i915_gem_object_unbind(struct drm_i915_gem_object *obj)
288{
289 struct i915_vma *vma;
290 LIST_HEAD(still_in_list);
291 int ret;
292
293 /* The vma will only be freed if it is marked as closed, and if we wait
294 * upon rendering to the vma, we may unbind anything in the list.
295 */
296 while ((vma = list_first_entry_or_null(&obj->vma_list,
297 struct i915_vma,
298 obj_link))) {
299 list_move_tail(&vma->obj_link, &still_in_list);
300 ret = i915_vma_unbind(vma);
301 if (ret)
302 break;
303 }
304 list_splice(&still_in_list, &obj->vma_list);
305
306 return ret;
307}
308
Chris Wilson00731152014-05-21 12:42:56 +0100309int
310i915_gem_object_attach_phys(struct drm_i915_gem_object *obj,
311 int align)
312{
313 drm_dma_handle_t *phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800314 int ret;
Chris Wilson00731152014-05-21 12:42:56 +0100315
316 if (obj->phys_handle) {
317 if ((unsigned long)obj->phys_handle->vaddr & (align -1))
318 return -EBUSY;
319
320 return 0;
321 }
322
323 if (obj->madv != I915_MADV_WILLNEED)
324 return -EFAULT;
325
326 if (obj->base.filp == NULL)
327 return -EINVAL;
328
Chris Wilson4717ca92016-08-04 07:52:28 +0100329 ret = i915_gem_object_unbind(obj);
330 if (ret)
331 return ret;
332
333 ret = i915_gem_object_put_pages(obj);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800334 if (ret)
335 return ret;
336
Chris Wilson00731152014-05-21 12:42:56 +0100337 /* create a new object */
338 phys = drm_pci_alloc(obj->base.dev, obj->base.size, align);
339 if (!phys)
340 return -ENOMEM;
341
Chris Wilson00731152014-05-21 12:42:56 +0100342 obj->phys_handle = phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800343 obj->ops = &i915_gem_phys_ops;
344
345 return i915_gem_object_get_pages(obj);
Chris Wilson00731152014-05-21 12:42:56 +0100346}
347
348static int
349i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
350 struct drm_i915_gem_pwrite *args,
351 struct drm_file *file_priv)
352{
353 struct drm_device *dev = obj->base.dev;
354 void *vaddr = obj->phys_handle->vaddr + args->offset;
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300355 char __user *user_data = u64_to_user_ptr(args->data_ptr);
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200356 int ret = 0;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800357
358 /* We manually control the domain here and pretend that it
359 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
360 */
361 ret = i915_gem_object_wait_rendering(obj, false);
362 if (ret)
363 return ret;
Chris Wilson00731152014-05-21 12:42:56 +0100364
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -0700365 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilson00731152014-05-21 12:42:56 +0100366 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
367 unsigned long unwritten;
368
369 /* The physical object once assigned is fixed for the lifetime
370 * of the obj, so we can safely drop the lock and continue
371 * to access vaddr.
372 */
373 mutex_unlock(&dev->struct_mutex);
374 unwritten = copy_from_user(vaddr, user_data, args->size);
375 mutex_lock(&dev->struct_mutex);
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200376 if (unwritten) {
377 ret = -EFAULT;
378 goto out;
379 }
Chris Wilson00731152014-05-21 12:42:56 +0100380 }
381
Chris Wilson6a2c4232014-11-04 04:51:40 -0800382 drm_clflush_virt_range(vaddr, args->size);
Chris Wilsonc0336662016-05-06 15:40:21 +0100383 i915_gem_chipset_flush(to_i915(dev));
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200384
385out:
Rodrigo Vivide152b62015-07-07 16:28:51 -0700386 intel_fb_obj_flush(obj, false, ORIGIN_CPU);
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200387 return ret;
Chris Wilson00731152014-05-21 12:42:56 +0100388}
389
Chris Wilson42dcedd2012-11-15 11:32:30 +0000390void *i915_gem_object_alloc(struct drm_device *dev)
391{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100392 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonefab6d82015-04-07 16:20:57 +0100393 return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000394}
395
396void i915_gem_object_free(struct drm_i915_gem_object *obj)
397{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100398 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonefab6d82015-04-07 16:20:57 +0100399 kmem_cache_free(dev_priv->objects, obj);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000400}
401
Dave Airlieff72145b2011-02-07 12:16:14 +1000402static int
403i915_gem_create(struct drm_file *file,
404 struct drm_device *dev,
405 uint64_t size,
406 uint32_t *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700407{
Chris Wilson05394f32010-11-08 19:18:58 +0000408 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300409 int ret;
410 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700411
Dave Airlieff72145b2011-02-07 12:16:14 +1000412 size = roundup(size, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200413 if (size == 0)
414 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700415
416 /* Allocate the new object */
Dave Gordond37cd8a2016-04-22 19:14:32 +0100417 obj = i915_gem_object_create(dev, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100418 if (IS_ERR(obj))
419 return PTR_ERR(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700420
Chris Wilson05394f32010-11-08 19:18:58 +0000421 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100422 /* drop reference from allocate - handle holds it now */
Chris Wilson34911fd2016-07-20 13:31:54 +0100423 i915_gem_object_put_unlocked(obj);
Daniel Vetterd861e332013-07-24 23:25:03 +0200424 if (ret)
425 return ret;
Chris Wilson202f2fe2010-10-14 13:20:40 +0100426
Dave Airlieff72145b2011-02-07 12:16:14 +1000427 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700428 return 0;
429}
430
Dave Airlieff72145b2011-02-07 12:16:14 +1000431int
432i915_gem_dumb_create(struct drm_file *file,
433 struct drm_device *dev,
434 struct drm_mode_create_dumb *args)
435{
436 /* have to work out size/pitch and return them */
Paulo Zanonide45eaf2013-10-18 18:48:24 -0300437 args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000438 args->size = args->pitch * args->height;
439 return i915_gem_create(file, dev,
Dave Airlieda6b51d2014-12-24 13:11:17 +1000440 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000441}
442
Dave Airlieff72145b2011-02-07 12:16:14 +1000443/**
444 * Creates a new mm object and returns a handle to it.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100445 * @dev: drm device pointer
446 * @data: ioctl data blob
447 * @file: drm file pointer
Dave Airlieff72145b2011-02-07 12:16:14 +1000448 */
449int
450i915_gem_create_ioctl(struct drm_device *dev, void *data,
451 struct drm_file *file)
452{
453 struct drm_i915_gem_create *args = data;
Daniel Vetter63ed2cb2012-04-23 16:50:50 +0200454
Dave Airlieff72145b2011-02-07 12:16:14 +1000455 return i915_gem_create(file, dev,
Dave Airlieda6b51d2014-12-24 13:11:17 +1000456 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000457}
458
Daniel Vetter8c599672011-12-14 13:57:31 +0100459static inline int
Daniel Vetter8461d222011-12-14 13:57:32 +0100460__copy_to_user_swizzled(char __user *cpu_vaddr,
461 const char *gpu_vaddr, int gpu_offset,
462 int length)
463{
464 int ret, cpu_offset = 0;
465
466 while (length > 0) {
467 int cacheline_end = ALIGN(gpu_offset + 1, 64);
468 int this_length = min(cacheline_end - gpu_offset, length);
469 int swizzled_gpu_offset = gpu_offset ^ 64;
470
471 ret = __copy_to_user(cpu_vaddr + cpu_offset,
472 gpu_vaddr + swizzled_gpu_offset,
473 this_length);
474 if (ret)
475 return ret + length;
476
477 cpu_offset += this_length;
478 gpu_offset += this_length;
479 length -= this_length;
480 }
481
482 return 0;
483}
484
485static inline int
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700486__copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
487 const char __user *cpu_vaddr,
Daniel Vetter8c599672011-12-14 13:57:31 +0100488 int length)
489{
490 int ret, cpu_offset = 0;
491
492 while (length > 0) {
493 int cacheline_end = ALIGN(gpu_offset + 1, 64);
494 int this_length = min(cacheline_end - gpu_offset, length);
495 int swizzled_gpu_offset = gpu_offset ^ 64;
496
497 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
498 cpu_vaddr + cpu_offset,
499 this_length);
500 if (ret)
501 return ret + length;
502
503 cpu_offset += this_length;
504 gpu_offset += this_length;
505 length -= this_length;
506 }
507
508 return 0;
509}
510
Brad Volkin4c914c02014-02-18 10:15:45 -0800511/*
512 * Pins the specified object's pages and synchronizes the object with
513 * GPU accesses. Sets needs_clflush to non-zero if the caller should
514 * flush the object from the CPU cache.
515 */
516int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
517 int *needs_clflush)
518{
519 int ret;
520
521 *needs_clflush = 0;
522
Chris Wilsonb9bcd142016-06-20 15:05:51 +0100523 if (WARN_ON(!i915_gem_object_has_struct_page(obj)))
Brad Volkin4c914c02014-02-18 10:15:45 -0800524 return -EINVAL;
525
Chris Wilsonc13d87e2016-07-20 09:21:15 +0100526 ret = i915_gem_object_wait_rendering(obj, true);
527 if (ret)
528 return ret;
529
Brad Volkin4c914c02014-02-18 10:15:45 -0800530 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) {
531 /* If we're not in the cpu read domain, set ourself into the gtt
532 * read domain and manually flush cachelines (if required). This
533 * optimizes for the case when the gpu will dirty the data
534 * anyway again before the next pread happens. */
535 *needs_clflush = !cpu_cache_is_coherent(obj->base.dev,
536 obj->cache_level);
Brad Volkin4c914c02014-02-18 10:15:45 -0800537 }
538
539 ret = i915_gem_object_get_pages(obj);
540 if (ret)
541 return ret;
542
543 i915_gem_object_pin_pages(obj);
544
545 return ret;
546}
547
Daniel Vetterd174bd62012-03-25 19:47:40 +0200548/* Per-page copy function for the shmem pread fastpath.
549 * Flushes invalid cachelines before reading the target if
550 * needs_clflush is set. */
Eric Anholteb014592009-03-10 11:44:52 -0700551static int
Daniel Vetterd174bd62012-03-25 19:47:40 +0200552shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length,
553 char __user *user_data,
554 bool page_do_bit17_swizzling, bool needs_clflush)
555{
556 char *vaddr;
557 int ret;
558
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200559 if (unlikely(page_do_bit17_swizzling))
Daniel Vetterd174bd62012-03-25 19:47:40 +0200560 return -EINVAL;
561
562 vaddr = kmap_atomic(page);
563 if (needs_clflush)
564 drm_clflush_virt_range(vaddr + shmem_page_offset,
565 page_length);
566 ret = __copy_to_user_inatomic(user_data,
567 vaddr + shmem_page_offset,
568 page_length);
569 kunmap_atomic(vaddr);
570
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100571 return ret ? -EFAULT : 0;
Daniel Vetterd174bd62012-03-25 19:47:40 +0200572}
573
Daniel Vetter23c18c72012-03-25 19:47:42 +0200574static void
575shmem_clflush_swizzled_range(char *addr, unsigned long length,
576 bool swizzled)
577{
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200578 if (unlikely(swizzled)) {
Daniel Vetter23c18c72012-03-25 19:47:42 +0200579 unsigned long start = (unsigned long) addr;
580 unsigned long end = (unsigned long) addr + length;
581
582 /* For swizzling simply ensure that we always flush both
583 * channels. Lame, but simple and it works. Swizzled
584 * pwrite/pread is far from a hotpath - current userspace
585 * doesn't use it at all. */
586 start = round_down(start, 128);
587 end = round_up(end, 128);
588
589 drm_clflush_virt_range((void *)start, end - start);
590 } else {
591 drm_clflush_virt_range(addr, length);
592 }
593
594}
595
Daniel Vetterd174bd62012-03-25 19:47:40 +0200596/* Only difference to the fast-path function is that this can handle bit17
597 * and uses non-atomic copy and kmap functions. */
598static int
599shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length,
600 char __user *user_data,
601 bool page_do_bit17_swizzling, bool needs_clflush)
602{
603 char *vaddr;
604 int ret;
605
606 vaddr = kmap(page);
607 if (needs_clflush)
Daniel Vetter23c18c72012-03-25 19:47:42 +0200608 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
609 page_length,
610 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200611
612 if (page_do_bit17_swizzling)
613 ret = __copy_to_user_swizzled(user_data,
614 vaddr, shmem_page_offset,
615 page_length);
616 else
617 ret = __copy_to_user(user_data,
618 vaddr + shmem_page_offset,
619 page_length);
620 kunmap(page);
621
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100622 return ret ? - EFAULT : 0;
Daniel Vetterd174bd62012-03-25 19:47:40 +0200623}
624
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530625static inline unsigned long
626slow_user_access(struct io_mapping *mapping,
627 uint64_t page_base, int page_offset,
628 char __user *user_data,
629 unsigned long length, bool pwrite)
630{
631 void __iomem *ioaddr;
632 void *vaddr;
633 uint64_t unwritten;
634
635 ioaddr = io_mapping_map_wc(mapping, page_base, PAGE_SIZE);
636 /* We can use the cpu mem copy function because this is X86. */
637 vaddr = (void __force *)ioaddr + page_offset;
638 if (pwrite)
639 unwritten = __copy_from_user(vaddr, user_data, length);
640 else
641 unwritten = __copy_to_user(user_data, vaddr, length);
642
643 io_mapping_unmap(ioaddr);
644 return unwritten;
645}
646
647static int
648i915_gem_gtt_pread(struct drm_device *dev,
649 struct drm_i915_gem_object *obj, uint64_t size,
650 uint64_t data_offset, uint64_t data_ptr)
651{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100652 struct drm_i915_private *dev_priv = to_i915(dev);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530653 struct i915_ggtt *ggtt = &dev_priv->ggtt;
654 struct drm_mm_node node;
655 char __user *user_data;
656 uint64_t remain;
657 uint64_t offset;
658 int ret;
659
660 ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_MAPPABLE);
661 if (ret) {
662 ret = insert_mappable_node(dev_priv, &node, PAGE_SIZE);
663 if (ret)
664 goto out;
665
666 ret = i915_gem_object_get_pages(obj);
667 if (ret) {
668 remove_mappable_node(&node);
669 goto out;
670 }
671
672 i915_gem_object_pin_pages(obj);
673 } else {
674 node.start = i915_gem_obj_ggtt_offset(obj);
675 node.allocated = false;
676 ret = i915_gem_object_put_fence(obj);
677 if (ret)
678 goto out_unpin;
679 }
680
681 ret = i915_gem_object_set_to_gtt_domain(obj, false);
682 if (ret)
683 goto out_unpin;
684
685 user_data = u64_to_user_ptr(data_ptr);
686 remain = size;
687 offset = data_offset;
688
689 mutex_unlock(&dev->struct_mutex);
690 if (likely(!i915.prefault_disable)) {
691 ret = fault_in_multipages_writeable(user_data, remain);
692 if (ret) {
693 mutex_lock(&dev->struct_mutex);
694 goto out_unpin;
695 }
696 }
697
698 while (remain > 0) {
699 /* Operation in this page
700 *
701 * page_base = page offset within aperture
702 * page_offset = offset within page
703 * page_length = bytes to copy for this page
704 */
705 u32 page_base = node.start;
706 unsigned page_offset = offset_in_page(offset);
707 unsigned page_length = PAGE_SIZE - page_offset;
708 page_length = remain < page_length ? remain : page_length;
709 if (node.allocated) {
710 wmb();
711 ggtt->base.insert_page(&ggtt->base,
712 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
713 node.start,
714 I915_CACHE_NONE, 0);
715 wmb();
716 } else {
717 page_base += offset & PAGE_MASK;
718 }
719 /* This is a slow read/write as it tries to read from
720 * and write to user memory which may result into page
721 * faults, and so we cannot perform this under struct_mutex.
722 */
723 if (slow_user_access(ggtt->mappable, page_base,
724 page_offset, user_data,
725 page_length, false)) {
726 ret = -EFAULT;
727 break;
728 }
729
730 remain -= page_length;
731 user_data += page_length;
732 offset += page_length;
733 }
734
735 mutex_lock(&dev->struct_mutex);
736 if (ret == 0 && (obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0) {
737 /* The user has modified the object whilst we tried
738 * reading from it, and we now have no idea what domain
739 * the pages should be in. As we have just been touching
740 * them directly, flush everything back to the GTT
741 * domain.
742 */
743 ret = i915_gem_object_set_to_gtt_domain(obj, false);
744 }
745
746out_unpin:
747 if (node.allocated) {
748 wmb();
749 ggtt->base.clear_range(&ggtt->base,
750 node.start, node.size,
751 true);
752 i915_gem_object_unpin_pages(obj);
753 remove_mappable_node(&node);
754 } else {
755 i915_gem_object_ggtt_unpin(obj);
756 }
757out:
758 return ret;
759}
760
Eric Anholteb014592009-03-10 11:44:52 -0700761static int
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200762i915_gem_shmem_pread(struct drm_device *dev,
763 struct drm_i915_gem_object *obj,
764 struct drm_i915_gem_pread *args,
765 struct drm_file *file)
Eric Anholteb014592009-03-10 11:44:52 -0700766{
Daniel Vetter8461d222011-12-14 13:57:32 +0100767 char __user *user_data;
Eric Anholteb014592009-03-10 11:44:52 -0700768 ssize_t remain;
Daniel Vetter8461d222011-12-14 13:57:32 +0100769 loff_t offset;
Ben Widawskyeb2c0c82012-02-15 14:42:43 +0100770 int shmem_page_offset, page_length, ret = 0;
Daniel Vetter8461d222011-12-14 13:57:32 +0100771 int obj_do_bit17_swizzling, page_do_bit17_swizzling;
Daniel Vetter96d79b52012-03-25 19:47:36 +0200772 int prefaulted = 0;
Daniel Vetter84897312012-03-25 19:47:31 +0200773 int needs_clflush = 0;
Imre Deak67d5a502013-02-18 19:28:02 +0200774 struct sg_page_iter sg_iter;
Eric Anholteb014592009-03-10 11:44:52 -0700775
Chris Wilson6eae0052016-06-20 15:05:52 +0100776 if (!i915_gem_object_has_struct_page(obj))
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530777 return -ENODEV;
778
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300779 user_data = u64_to_user_ptr(args->data_ptr);
Eric Anholteb014592009-03-10 11:44:52 -0700780 remain = args->size;
781
Daniel Vetter8461d222011-12-14 13:57:32 +0100782 obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700783
Brad Volkin4c914c02014-02-18 10:15:45 -0800784 ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100785 if (ret)
786 return ret;
787
Eric Anholteb014592009-03-10 11:44:52 -0700788 offset = args->offset;
Daniel Vetter8461d222011-12-14 13:57:32 +0100789
Imre Deak67d5a502013-02-18 19:28:02 +0200790 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
791 offset >> PAGE_SHIFT) {
Imre Deak2db76d72013-03-26 15:14:18 +0200792 struct page *page = sg_page_iter_page(&sg_iter);
Chris Wilson9da3da62012-06-01 15:20:22 +0100793
794 if (remain <= 0)
795 break;
796
Eric Anholteb014592009-03-10 11:44:52 -0700797 /* Operation in this page
798 *
Eric Anholteb014592009-03-10 11:44:52 -0700799 * shmem_page_offset = offset within page in shmem file
Eric Anholteb014592009-03-10 11:44:52 -0700800 * page_length = bytes to copy for this page
801 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100802 shmem_page_offset = offset_in_page(offset);
Eric Anholteb014592009-03-10 11:44:52 -0700803 page_length = remain;
804 if ((shmem_page_offset + page_length) > PAGE_SIZE)
805 page_length = PAGE_SIZE - shmem_page_offset;
Eric Anholteb014592009-03-10 11:44:52 -0700806
Daniel Vetter8461d222011-12-14 13:57:32 +0100807 page_do_bit17_swizzling = obj_do_bit17_swizzling &&
808 (page_to_phys(page) & (1 << 17)) != 0;
809
Daniel Vetterd174bd62012-03-25 19:47:40 +0200810 ret = shmem_pread_fast(page, shmem_page_offset, page_length,
811 user_data, page_do_bit17_swizzling,
812 needs_clflush);
813 if (ret == 0)
814 goto next_page;
Eric Anholteb014592009-03-10 11:44:52 -0700815
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200816 mutex_unlock(&dev->struct_mutex);
817
Jani Nikulad330a952014-01-21 11:24:25 +0200818 if (likely(!i915.prefault_disable) && !prefaulted) {
Daniel Vetterf56f8212012-03-25 19:47:41 +0200819 ret = fault_in_multipages_writeable(user_data, remain);
Daniel Vetter96d79b52012-03-25 19:47:36 +0200820 /* Userspace is tricking us, but we've already clobbered
821 * its pages with the prefault and promised to write the
822 * data up to the first fault. Hence ignore any errors
823 * and just continue. */
824 (void)ret;
825 prefaulted = 1;
826 }
827
Daniel Vetterd174bd62012-03-25 19:47:40 +0200828 ret = shmem_pread_slow(page, shmem_page_offset, page_length,
829 user_data, page_do_bit17_swizzling,
830 needs_clflush);
Eric Anholteb014592009-03-10 11:44:52 -0700831
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200832 mutex_lock(&dev->struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100833
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100834 if (ret)
Daniel Vetter8461d222011-12-14 13:57:32 +0100835 goto out;
Daniel Vetter8461d222011-12-14 13:57:32 +0100836
Chris Wilson17793c92014-03-07 08:30:36 +0000837next_page:
Eric Anholteb014592009-03-10 11:44:52 -0700838 remain -= page_length;
Daniel Vetter8461d222011-12-14 13:57:32 +0100839 user_data += page_length;
Eric Anholteb014592009-03-10 11:44:52 -0700840 offset += page_length;
841 }
842
Chris Wilson4f27b752010-10-14 15:26:45 +0100843out:
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100844 i915_gem_object_unpin_pages(obj);
845
Eric Anholteb014592009-03-10 11:44:52 -0700846 return ret;
847}
848
Eric Anholt673a3942008-07-30 12:06:12 -0700849/**
850 * Reads data from the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100851 * @dev: drm device pointer
852 * @data: ioctl data blob
853 * @file: drm file pointer
Eric Anholt673a3942008-07-30 12:06:12 -0700854 *
855 * On error, the contents of *data are undefined.
856 */
857int
858i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000859 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700860{
861 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000862 struct drm_i915_gem_object *obj;
Chris Wilson35b62a82010-09-26 20:23:38 +0100863 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700864
Chris Wilson51311d02010-11-17 09:10:42 +0000865 if (args->size == 0)
866 return 0;
867
868 if (!access_ok(VERIFY_WRITE,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300869 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +0000870 args->size))
871 return -EFAULT;
872
Chris Wilson4f27b752010-10-14 15:26:45 +0100873 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100874 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100875 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700876
Chris Wilson03ac0642016-07-20 13:31:51 +0100877 obj = i915_gem_object_lookup(file, args->handle);
878 if (!obj) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100879 ret = -ENOENT;
880 goto unlock;
Chris Wilson4f27b752010-10-14 15:26:45 +0100881 }
Eric Anholt673a3942008-07-30 12:06:12 -0700882
Chris Wilson7dcd2492010-09-26 20:21:44 +0100883 /* Bounds check source. */
Chris Wilson05394f32010-11-08 19:18:58 +0000884 if (args->offset > obj->base.size ||
885 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100886 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100887 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100888 }
889
Chris Wilsondb53a302011-02-03 11:57:46 +0000890 trace_i915_gem_object_pread(obj, args->offset, args->size);
891
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200892 ret = i915_gem_shmem_pread(dev, obj, args, file);
Eric Anholt673a3942008-07-30 12:06:12 -0700893
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530894 /* pread for non shmem backed objects */
895 if (ret == -EFAULT || ret == -ENODEV)
896 ret = i915_gem_gtt_pread(dev, obj, args->size,
897 args->offset, args->data_ptr);
898
Chris Wilson35b62a82010-09-26 20:23:38 +0100899out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +0100900 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100901unlock:
Chris Wilson4f27b752010-10-14 15:26:45 +0100902 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700903 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700904}
905
Keith Packard0839ccb2008-10-30 19:38:48 -0700906/* This is the fast write path which cannot handle
907 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700908 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700909
Keith Packard0839ccb2008-10-30 19:38:48 -0700910static inline int
911fast_user_write(struct io_mapping *mapping,
912 loff_t page_base, int page_offset,
913 char __user *user_data,
914 int length)
915{
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700916 void __iomem *vaddr_atomic;
917 void *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700918 unsigned long unwritten;
919
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700920 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700921 /* We can use the cpu mem copy function because this is X86. */
922 vaddr = (void __force*)vaddr_atomic + page_offset;
923 unwritten = __copy_from_user_inatomic_nocache(vaddr,
Keith Packard0839ccb2008-10-30 19:38:48 -0700924 user_data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700925 io_mapping_unmap_atomic(vaddr_atomic);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100926 return unwritten;
Keith Packard0839ccb2008-10-30 19:38:48 -0700927}
928
Eric Anholt3de09aa2009-03-09 09:42:23 -0700929/**
930 * This is the fast pwrite path, where we copy the data directly from the
931 * user into the GTT, uncached.
Daniel Vetter62f90b32016-07-15 21:48:07 +0200932 * @i915: i915 device private data
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100933 * @obj: i915 gem object
934 * @args: pwrite arguments structure
935 * @file: drm file pointer
Eric Anholt3de09aa2009-03-09 09:42:23 -0700936 */
Eric Anholt673a3942008-07-30 12:06:12 -0700937static int
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530938i915_gem_gtt_pwrite_fast(struct drm_i915_private *i915,
Chris Wilson05394f32010-11-08 19:18:58 +0000939 struct drm_i915_gem_object *obj,
Eric Anholt3de09aa2009-03-09 09:42:23 -0700940 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000941 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700942{
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530943 struct i915_ggtt *ggtt = &i915->ggtt;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530944 struct drm_device *dev = obj->base.dev;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530945 struct drm_mm_node node;
946 uint64_t remain, offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700947 char __user *user_data;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530948 int ret;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530949 bool hit_slow_path = false;
950
951 if (obj->tiling_mode != I915_TILING_NONE)
952 return -EFAULT;
Daniel Vetter935aaa62012-03-25 19:47:35 +0200953
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100954 ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_MAPPABLE | PIN_NONBLOCK);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530955 if (ret) {
956 ret = insert_mappable_node(i915, &node, PAGE_SIZE);
957 if (ret)
958 goto out;
959
960 ret = i915_gem_object_get_pages(obj);
961 if (ret) {
962 remove_mappable_node(&node);
963 goto out;
964 }
965
966 i915_gem_object_pin_pages(obj);
967 } else {
968 node.start = i915_gem_obj_ggtt_offset(obj);
969 node.allocated = false;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530970 ret = i915_gem_object_put_fence(obj);
971 if (ret)
972 goto out_unpin;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530973 }
Daniel Vetter935aaa62012-03-25 19:47:35 +0200974
975 ret = i915_gem_object_set_to_gtt_domain(obj, true);
976 if (ret)
977 goto out_unpin;
978
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -0700979 intel_fb_obj_invalidate(obj, ORIGIN_GTT);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530980 obj->dirty = true;
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200981
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530982 user_data = u64_to_user_ptr(args->data_ptr);
983 offset = args->offset;
984 remain = args->size;
985 while (remain) {
Eric Anholt673a3942008-07-30 12:06:12 -0700986 /* Operation in this page
987 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700988 * page_base = page offset within aperture
989 * page_offset = offset within page
990 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700991 */
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530992 u32 page_base = node.start;
993 unsigned page_offset = offset_in_page(offset);
994 unsigned page_length = PAGE_SIZE - page_offset;
995 page_length = remain < page_length ? remain : page_length;
996 if (node.allocated) {
997 wmb(); /* flush the write before we modify the GGTT */
998 ggtt->base.insert_page(&ggtt->base,
999 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
1000 node.start, I915_CACHE_NONE, 0);
1001 wmb(); /* flush modifications to the GGTT (insert_page) */
1002 } else {
1003 page_base += offset & PAGE_MASK;
1004 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001005 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -07001006 * source page isn't available. Return the error and we'll
1007 * retry in the slow path.
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301008 * If the object is non-shmem backed, we retry again with the
1009 * path that handles page fault.
Keith Packard0839ccb2008-10-30 19:38:48 -07001010 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001011 if (fast_user_write(ggtt->mappable, page_base,
Daniel Vetter935aaa62012-03-25 19:47:35 +02001012 page_offset, user_data, page_length)) {
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301013 hit_slow_path = true;
1014 mutex_unlock(&dev->struct_mutex);
1015 if (slow_user_access(ggtt->mappable,
1016 page_base,
1017 page_offset, user_data,
1018 page_length, true)) {
1019 ret = -EFAULT;
1020 mutex_lock(&dev->struct_mutex);
1021 goto out_flush;
1022 }
1023
1024 mutex_lock(&dev->struct_mutex);
Daniel Vetter935aaa62012-03-25 19:47:35 +02001025 }
Eric Anholt673a3942008-07-30 12:06:12 -07001026
Keith Packard0839ccb2008-10-30 19:38:48 -07001027 remain -= page_length;
1028 user_data += page_length;
1029 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -07001030 }
Eric Anholt673a3942008-07-30 12:06:12 -07001031
Paulo Zanoni063e4e62015-02-13 17:23:45 -02001032out_flush:
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301033 if (hit_slow_path) {
1034 if (ret == 0 &&
1035 (obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0) {
1036 /* The user has modified the object whilst we tried
1037 * reading from it, and we now have no idea what domain
1038 * the pages should be in. As we have just been touching
1039 * them directly, flush everything back to the GTT
1040 * domain.
1041 */
1042 ret = i915_gem_object_set_to_gtt_domain(obj, false);
1043 }
1044 }
1045
Rodrigo Vivide152b62015-07-07 16:28:51 -07001046 intel_fb_obj_flush(obj, false, ORIGIN_GTT);
Daniel Vetter935aaa62012-03-25 19:47:35 +02001047out_unpin:
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301048 if (node.allocated) {
1049 wmb();
1050 ggtt->base.clear_range(&ggtt->base,
1051 node.start, node.size,
1052 true);
1053 i915_gem_object_unpin_pages(obj);
1054 remove_mappable_node(&node);
1055 } else {
1056 i915_gem_object_ggtt_unpin(obj);
1057 }
Daniel Vetter935aaa62012-03-25 19:47:35 +02001058out:
Eric Anholt3de09aa2009-03-09 09:42:23 -07001059 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001060}
1061
Daniel Vetterd174bd62012-03-25 19:47:40 +02001062/* Per-page copy function for the shmem pwrite fastpath.
1063 * Flushes invalid cachelines before writing to the target if
1064 * needs_clflush_before is set and flushes out any written cachelines after
1065 * writing if needs_clflush is set. */
Eric Anholt673a3942008-07-30 12:06:12 -07001066static int
Daniel Vetterd174bd62012-03-25 19:47:40 +02001067shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
1068 char __user *user_data,
1069 bool page_do_bit17_swizzling,
1070 bool needs_clflush_before,
1071 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -07001072{
Daniel Vetterd174bd62012-03-25 19:47:40 +02001073 char *vaddr;
Eric Anholt3de09aa2009-03-09 09:42:23 -07001074 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -07001075
Daniel Vettere7e58eb2012-03-25 19:47:43 +02001076 if (unlikely(page_do_bit17_swizzling))
Daniel Vetterd174bd62012-03-25 19:47:40 +02001077 return -EINVAL;
Eric Anholt3de09aa2009-03-09 09:42:23 -07001078
Daniel Vetterd174bd62012-03-25 19:47:40 +02001079 vaddr = kmap_atomic(page);
1080 if (needs_clflush_before)
1081 drm_clflush_virt_range(vaddr + shmem_page_offset,
1082 page_length);
Chris Wilsonc2831a92014-03-07 08:30:37 +00001083 ret = __copy_from_user_inatomic(vaddr + shmem_page_offset,
1084 user_data, page_length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001085 if (needs_clflush_after)
1086 drm_clflush_virt_range(vaddr + shmem_page_offset,
1087 page_length);
1088 kunmap_atomic(vaddr);
Eric Anholt3de09aa2009-03-09 09:42:23 -07001089
Chris Wilson755d2212012-09-04 21:02:55 +01001090 return ret ? -EFAULT : 0;
Eric Anholt3de09aa2009-03-09 09:42:23 -07001091}
1092
Daniel Vetterd174bd62012-03-25 19:47:40 +02001093/* Only difference to the fast-path function is that this can handle bit17
1094 * and uses non-atomic copy and kmap functions. */
Eric Anholt3043c602008-10-02 12:24:47 -07001095static int
Daniel Vetterd174bd62012-03-25 19:47:40 +02001096shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
1097 char __user *user_data,
1098 bool page_do_bit17_swizzling,
1099 bool needs_clflush_before,
1100 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -07001101{
Daniel Vetterd174bd62012-03-25 19:47:40 +02001102 char *vaddr;
1103 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001104
Daniel Vetterd174bd62012-03-25 19:47:40 +02001105 vaddr = kmap(page);
Daniel Vettere7e58eb2012-03-25 19:47:43 +02001106 if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
Daniel Vetter23c18c72012-03-25 19:47:42 +02001107 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
1108 page_length,
1109 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001110 if (page_do_bit17_swizzling)
1111 ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
Chris Wilsone5281cc2010-10-28 13:45:36 +01001112 user_data,
1113 page_length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001114 else
1115 ret = __copy_from_user(vaddr + shmem_page_offset,
1116 user_data,
1117 page_length);
1118 if (needs_clflush_after)
Daniel Vetter23c18c72012-03-25 19:47:42 +02001119 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
1120 page_length,
1121 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001122 kunmap(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001123
Chris Wilson755d2212012-09-04 21:02:55 +01001124 return ret ? -EFAULT : 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001125}
1126
Eric Anholt40123c12009-03-09 13:42:30 -07001127static int
Daniel Vettere244a442012-03-25 19:47:28 +02001128i915_gem_shmem_pwrite(struct drm_device *dev,
1129 struct drm_i915_gem_object *obj,
1130 struct drm_i915_gem_pwrite *args,
1131 struct drm_file *file)
Eric Anholt40123c12009-03-09 13:42:30 -07001132{
Eric Anholt40123c12009-03-09 13:42:30 -07001133 ssize_t remain;
Daniel Vetter8c599672011-12-14 13:57:31 +01001134 loff_t offset;
1135 char __user *user_data;
Ben Widawskyeb2c0c82012-02-15 14:42:43 +01001136 int shmem_page_offset, page_length, ret = 0;
Daniel Vetter8c599672011-12-14 13:57:31 +01001137 int obj_do_bit17_swizzling, page_do_bit17_swizzling;
Daniel Vettere244a442012-03-25 19:47:28 +02001138 int hit_slowpath = 0;
Daniel Vetter58642882012-03-25 19:47:37 +02001139 int needs_clflush_after = 0;
1140 int needs_clflush_before = 0;
Imre Deak67d5a502013-02-18 19:28:02 +02001141 struct sg_page_iter sg_iter;
Eric Anholt40123c12009-03-09 13:42:30 -07001142
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001143 user_data = u64_to_user_ptr(args->data_ptr);
Eric Anholt40123c12009-03-09 13:42:30 -07001144 remain = args->size;
1145
Daniel Vetter8c599672011-12-14 13:57:31 +01001146 obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001147
Chris Wilsonc13d87e2016-07-20 09:21:15 +01001148 ret = i915_gem_object_wait_rendering(obj, false);
1149 if (ret)
1150 return ret;
1151
Daniel Vetter58642882012-03-25 19:47:37 +02001152 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
1153 /* If we're not in the cpu write domain, set ourself into the gtt
1154 * write domain and manually flush cachelines (if required). This
1155 * optimizes for the case when the gpu will use the data
1156 * right away and we therefore have to clflush anyway. */
Chris Wilson2c225692013-08-09 12:26:45 +01001157 needs_clflush_after = cpu_write_needs_clflush(obj);
Daniel Vetter58642882012-03-25 19:47:37 +02001158 }
Chris Wilsonc76ce032013-08-08 14:41:03 +01001159 /* Same trick applies to invalidate partially written cachelines read
1160 * before writing. */
1161 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
1162 needs_clflush_before =
1163 !cpu_cache_is_coherent(dev, obj->cache_level);
Daniel Vetter58642882012-03-25 19:47:37 +02001164
Chris Wilson755d2212012-09-04 21:02:55 +01001165 ret = i915_gem_object_get_pages(obj);
1166 if (ret)
1167 return ret;
1168
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -07001169 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Paulo Zanoni063e4e62015-02-13 17:23:45 -02001170
Chris Wilson755d2212012-09-04 21:02:55 +01001171 i915_gem_object_pin_pages(obj);
1172
Eric Anholt40123c12009-03-09 13:42:30 -07001173 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +00001174 obj->dirty = 1;
Eric Anholt40123c12009-03-09 13:42:30 -07001175
Imre Deak67d5a502013-02-18 19:28:02 +02001176 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
1177 offset >> PAGE_SHIFT) {
Imre Deak2db76d72013-03-26 15:14:18 +02001178 struct page *page = sg_page_iter_page(&sg_iter);
Daniel Vetter58642882012-03-25 19:47:37 +02001179 int partial_cacheline_write;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001180
Chris Wilson9da3da62012-06-01 15:20:22 +01001181 if (remain <= 0)
1182 break;
1183
Eric Anholt40123c12009-03-09 13:42:30 -07001184 /* Operation in this page
1185 *
Eric Anholt40123c12009-03-09 13:42:30 -07001186 * shmem_page_offset = offset within page in shmem file
Eric Anholt40123c12009-03-09 13:42:30 -07001187 * page_length = bytes to copy for this page
1188 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +01001189 shmem_page_offset = offset_in_page(offset);
Eric Anholt40123c12009-03-09 13:42:30 -07001190
1191 page_length = remain;
1192 if ((shmem_page_offset + page_length) > PAGE_SIZE)
1193 page_length = PAGE_SIZE - shmem_page_offset;
Eric Anholt40123c12009-03-09 13:42:30 -07001194
Daniel Vetter58642882012-03-25 19:47:37 +02001195 /* If we don't overwrite a cacheline completely we need to be
1196 * careful to have up-to-date data by first clflushing. Don't
1197 * overcomplicate things and flush the entire patch. */
1198 partial_cacheline_write = needs_clflush_before &&
1199 ((shmem_page_offset | page_length)
1200 & (boot_cpu_data.x86_clflush_size - 1));
1201
Daniel Vetter8c599672011-12-14 13:57:31 +01001202 page_do_bit17_swizzling = obj_do_bit17_swizzling &&
1203 (page_to_phys(page) & (1 << 17)) != 0;
1204
Daniel Vetterd174bd62012-03-25 19:47:40 +02001205 ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
1206 user_data, page_do_bit17_swizzling,
1207 partial_cacheline_write,
1208 needs_clflush_after);
1209 if (ret == 0)
1210 goto next_page;
Eric Anholt40123c12009-03-09 13:42:30 -07001211
Daniel Vettere244a442012-03-25 19:47:28 +02001212 hit_slowpath = 1;
Daniel Vettere244a442012-03-25 19:47:28 +02001213 mutex_unlock(&dev->struct_mutex);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001214 ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
1215 user_data, page_do_bit17_swizzling,
1216 partial_cacheline_write,
1217 needs_clflush_after);
Eric Anholt40123c12009-03-09 13:42:30 -07001218
Daniel Vettere244a442012-03-25 19:47:28 +02001219 mutex_lock(&dev->struct_mutex);
Chris Wilson755d2212012-09-04 21:02:55 +01001220
Chris Wilson755d2212012-09-04 21:02:55 +01001221 if (ret)
Daniel Vetter8c599672011-12-14 13:57:31 +01001222 goto out;
Daniel Vetter8c599672011-12-14 13:57:31 +01001223
Chris Wilson17793c92014-03-07 08:30:36 +00001224next_page:
Eric Anholt40123c12009-03-09 13:42:30 -07001225 remain -= page_length;
Daniel Vetter8c599672011-12-14 13:57:31 +01001226 user_data += page_length;
Eric Anholt40123c12009-03-09 13:42:30 -07001227 offset += page_length;
1228 }
1229
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001230out:
Chris Wilson755d2212012-09-04 21:02:55 +01001231 i915_gem_object_unpin_pages(obj);
1232
Daniel Vettere244a442012-03-25 19:47:28 +02001233 if (hit_slowpath) {
Daniel Vetter8dcf0152012-11-15 16:53:58 +01001234 /*
1235 * Fixup: Flush cpu caches in case we didn't flush the dirty
1236 * cachelines in-line while writing and the object moved
1237 * out of the cpu write domain while we've dropped the lock.
1238 */
1239 if (!needs_clflush_after &&
1240 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
Chris Wilson000433b2013-08-08 14:41:09 +01001241 if (i915_gem_clflush_object(obj, obj->pin_display))
Ville Syrjäläed75a552015-08-11 19:47:10 +03001242 needs_clflush_after = true;
Daniel Vettere244a442012-03-25 19:47:28 +02001243 }
Daniel Vetter8c599672011-12-14 13:57:31 +01001244 }
Eric Anholt40123c12009-03-09 13:42:30 -07001245
Daniel Vetter58642882012-03-25 19:47:37 +02001246 if (needs_clflush_after)
Chris Wilsonc0336662016-05-06 15:40:21 +01001247 i915_gem_chipset_flush(to_i915(dev));
Ville Syrjäläed75a552015-08-11 19:47:10 +03001248 else
1249 obj->cache_dirty = true;
Daniel Vetter58642882012-03-25 19:47:37 +02001250
Rodrigo Vivide152b62015-07-07 16:28:51 -07001251 intel_fb_obj_flush(obj, false, ORIGIN_CPU);
Eric Anholt40123c12009-03-09 13:42:30 -07001252 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001253}
1254
1255/**
1256 * Writes data to the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001257 * @dev: drm device
1258 * @data: ioctl data blob
1259 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001260 *
1261 * On error, the contents of the buffer that were to be modified are undefined.
1262 */
1263int
1264i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001265 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001266{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001267 struct drm_i915_private *dev_priv = to_i915(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07001268 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001269 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +00001270 int ret;
1271
1272 if (args->size == 0)
1273 return 0;
1274
1275 if (!access_ok(VERIFY_READ,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001276 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001277 args->size))
1278 return -EFAULT;
1279
Jani Nikulad330a952014-01-21 11:24:25 +02001280 if (likely(!i915.prefault_disable)) {
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001281 ret = fault_in_multipages_readable(u64_to_user_ptr(args->data_ptr),
Xiong Zhang0b74b502013-07-19 13:51:24 +08001282 args->size);
1283 if (ret)
1284 return -EFAULT;
1285 }
Eric Anholt673a3942008-07-30 12:06:12 -07001286
Imre Deak5d77d9c2014-11-12 16:40:35 +02001287 intel_runtime_pm_get(dev_priv);
1288
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001289 ret = i915_mutex_lock_interruptible(dev);
1290 if (ret)
Imre Deak5d77d9c2014-11-12 16:40:35 +02001291 goto put_rpm;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001292
Chris Wilson03ac0642016-07-20 13:31:51 +01001293 obj = i915_gem_object_lookup(file, args->handle);
1294 if (!obj) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001295 ret = -ENOENT;
1296 goto unlock;
1297 }
Eric Anholt673a3942008-07-30 12:06:12 -07001298
Chris Wilson7dcd2492010-09-26 20:21:44 +01001299 /* Bounds check destination. */
Chris Wilson05394f32010-11-08 19:18:58 +00001300 if (args->offset > obj->base.size ||
1301 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001302 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +01001303 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001304 }
1305
Chris Wilsondb53a302011-02-03 11:57:46 +00001306 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
1307
Daniel Vetter935aaa62012-03-25 19:47:35 +02001308 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -07001309 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1310 * it would end up going through the fenced access, and we'll get
1311 * different detiling behavior between reading and writing.
1312 * pread/pwrite currently are reading and writing from the CPU
1313 * perspective, requiring manual detiling by the client.
1314 */
Chris Wilson6eae0052016-06-20 15:05:52 +01001315 if (!i915_gem_object_has_struct_page(obj) ||
1316 cpu_write_needs_clflush(obj)) {
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301317 ret = i915_gem_gtt_pwrite_fast(dev_priv, obj, args, file);
Daniel Vetter935aaa62012-03-25 19:47:35 +02001318 /* Note that the gtt paths might fail with non-page-backed user
1319 * pointers (e.g. gtt mappings when moving data between
1320 * textures). Fallback to the shmem path in that case. */
Eric Anholt40123c12009-03-09 13:42:30 -07001321 }
Eric Anholt673a3942008-07-30 12:06:12 -07001322
Chris Wilsond1054ee2016-07-16 18:42:36 +01001323 if (ret == -EFAULT || ret == -ENOSPC) {
Chris Wilson6a2c4232014-11-04 04:51:40 -08001324 if (obj->phys_handle)
1325 ret = i915_gem_phys_pwrite(obj, args, file);
Chris Wilson6eae0052016-06-20 15:05:52 +01001326 else if (i915_gem_object_has_struct_page(obj))
Chris Wilson6a2c4232014-11-04 04:51:40 -08001327 ret = i915_gem_shmem_pwrite(dev, obj, args, file);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301328 else
1329 ret = -ENODEV;
Chris Wilson6a2c4232014-11-04 04:51:40 -08001330 }
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001331
Chris Wilson35b62a82010-09-26 20:23:38 +01001332out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01001333 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001334unlock:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001335 mutex_unlock(&dev->struct_mutex);
Imre Deak5d77d9c2014-11-12 16:40:35 +02001336put_rpm:
1337 intel_runtime_pm_put(dev_priv);
1338
Eric Anholt673a3942008-07-30 12:06:12 -07001339 return ret;
1340}
1341
Chris Wilsonb4716182015-04-27 13:41:17 +01001342static void
1343i915_gem_object_retire_request(struct drm_i915_gem_object *obj,
1344 struct drm_i915_gem_request *req)
1345{
Chris Wilson7e21d642016-07-27 09:07:29 +01001346 int idx = req->engine->id;
Chris Wilsonb4716182015-04-27 13:41:17 +01001347
Chris Wilsond72d9082016-08-04 07:52:31 +01001348 if (i915_gem_active_peek(&obj->last_read[idx],
1349 &obj->base.dev->struct_mutex) == req)
Chris Wilson7e21d642016-07-27 09:07:29 +01001350 i915_gem_object_retire__read(obj, idx);
Chris Wilsond72d9082016-08-04 07:52:31 +01001351 else if (i915_gem_active_peek(&obj->last_write,
1352 &obj->base.dev->struct_mutex) == req)
Chris Wilsonb4716182015-04-27 13:41:17 +01001353 i915_gem_object_retire__write(obj);
1354
Chris Wilson0c5eed62016-06-29 15:51:14 +01001355 if (!i915_reset_in_progress(&req->i915->gpu_error))
Chris Wilson05235c52016-07-20 09:21:08 +01001356 i915_gem_request_retire_upto(req);
Chris Wilsonb3612372012-08-24 09:35:08 +01001357}
1358
Chris Wilson8cac6f62016-08-04 07:52:32 +01001359/**
1360 * Ensures that all rendering to the object has completed and the object is
1361 * safe to unbind from the GTT or access from the CPU.
1362 * @obj: i915 gem object
1363 * @readonly: waiting for read access or write
1364 */
1365int
1366i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
1367 bool readonly)
1368{
1369 struct reservation_object *resv;
1370 struct i915_gem_active *active;
1371 unsigned long active_mask;
1372 int idx, ret;
1373
1374 lockdep_assert_held(&obj->base.dev->struct_mutex);
1375
1376 if (!readonly) {
1377 active = obj->last_read;
1378 active_mask = obj->active;
1379 } else {
1380 active_mask = 1;
1381 active = &obj->last_write;
1382 }
1383
1384 for_each_active(active_mask, idx) {
1385 struct drm_i915_gem_request *request;
1386
1387 request = i915_gem_active_peek(&active[idx],
1388 &obj->base.dev->struct_mutex);
1389 if (!request)
1390 continue;
1391
1392 ret = i915_wait_request(request);
1393 if (ret)
1394 return ret;
1395
1396 i915_gem_object_retire_request(obj, request);
1397 }
1398
1399 resv = i915_gem_object_get_dmabuf_resv(obj);
1400 if (resv) {
1401 long err;
1402
1403 err = reservation_object_wait_timeout_rcu(resv, !readonly, true,
1404 MAX_SCHEDULE_TIMEOUT);
1405 if (err < 0)
1406 return err;
1407 }
1408
1409 return 0;
1410}
1411
Chris Wilson3236f572012-08-24 09:35:09 +01001412/* A nonblocking variant of the above wait. This is a highly dangerous routine
1413 * as the object state may change during this call.
1414 */
1415static __must_check int
1416i915_gem_object_wait_rendering__nonblocking(struct drm_i915_gem_object *obj,
Chris Wilson2e1b8732015-04-27 13:41:22 +01001417 struct intel_rps_client *rps,
Chris Wilson3236f572012-08-24 09:35:09 +01001418 bool readonly)
1419{
1420 struct drm_device *dev = obj->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001421 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00001422 struct drm_i915_gem_request *requests[I915_NUM_ENGINES];
Chris Wilson8cac6f62016-08-04 07:52:32 +01001423 struct i915_gem_active *active;
1424 unsigned long active_mask;
Chris Wilsonb4716182015-04-27 13:41:17 +01001425 int ret, i, n = 0;
Chris Wilson3236f572012-08-24 09:35:09 +01001426
1427 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1428 BUG_ON(!dev_priv->mm.interruptible);
1429
Chris Wilson8cac6f62016-08-04 07:52:32 +01001430 active_mask = obj->active;
1431 if (!active_mask)
Chris Wilson3236f572012-08-24 09:35:09 +01001432 return 0;
1433
Chris Wilson8cac6f62016-08-04 07:52:32 +01001434 if (!readonly) {
1435 active = obj->last_read;
1436 } else {
1437 active_mask = 1;
1438 active = &obj->last_write;
1439 }
1440
1441 for_each_active(active_mask, i) {
Chris Wilsonb4716182015-04-27 13:41:17 +01001442 struct drm_i915_gem_request *req;
1443
Chris Wilson8cac6f62016-08-04 07:52:32 +01001444 req = i915_gem_active_get(&active[i],
Chris Wilsond72d9082016-08-04 07:52:31 +01001445 &obj->base.dev->struct_mutex);
Chris Wilson8cac6f62016-08-04 07:52:32 +01001446 if (req)
Chris Wilson27c01aa2016-08-04 07:52:30 +01001447 requests[n++] = req;
Chris Wilsonb4716182015-04-27 13:41:17 +01001448 }
1449
1450 mutex_unlock(&dev->struct_mutex);
Chris Wilson299259a2016-04-13 17:35:06 +01001451 ret = 0;
Chris Wilsonb4716182015-04-27 13:41:17 +01001452 for (i = 0; ret == 0 && i < n; i++)
Chris Wilson299259a2016-04-13 17:35:06 +01001453 ret = __i915_wait_request(requests[i], true, NULL, rps);
Chris Wilsonb4716182015-04-27 13:41:17 +01001454 mutex_lock(&dev->struct_mutex);
1455
Chris Wilsonb4716182015-04-27 13:41:17 +01001456 for (i = 0; i < n; i++) {
1457 if (ret == 0)
1458 i915_gem_object_retire_request(obj, requests[i]);
Chris Wilsone8a261e2016-07-20 13:31:49 +01001459 i915_gem_request_put(requests[i]);
Chris Wilsonb4716182015-04-27 13:41:17 +01001460 }
1461
1462 return ret;
Chris Wilson3236f572012-08-24 09:35:09 +01001463}
1464
Chris Wilson2e1b8732015-04-27 13:41:22 +01001465static struct intel_rps_client *to_rps_client(struct drm_file *file)
1466{
1467 struct drm_i915_file_private *fpriv = file->driver_priv;
1468 return &fpriv->rps;
1469}
1470
Chris Wilsonaeecc962016-06-17 14:46:39 -03001471static enum fb_op_origin
1472write_origin(struct drm_i915_gem_object *obj, unsigned domain)
1473{
1474 return domain == I915_GEM_DOMAIN_GTT && !obj->has_wc_mmap ?
1475 ORIGIN_GTT : ORIGIN_CPU;
1476}
1477
Eric Anholt673a3942008-07-30 12:06:12 -07001478/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001479 * Called when user space prepares to use an object with the CPU, either
1480 * through the mmap ioctl's mapping or a GTT mapping.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001481 * @dev: drm device
1482 * @data: ioctl data blob
1483 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001484 */
1485int
1486i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001487 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001488{
1489 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001490 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001491 uint32_t read_domains = args->read_domains;
1492 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001493 int ret;
1494
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001495 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001496 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001497 return -EINVAL;
1498
Chris Wilson21d509e2009-06-06 09:46:02 +01001499 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001500 return -EINVAL;
1501
1502 /* Having something in the write domain implies it's in the read
1503 * domain, and only that read domain. Enforce that in the request.
1504 */
1505 if (write_domain != 0 && read_domains != write_domain)
1506 return -EINVAL;
1507
Chris Wilson76c1dec2010-09-25 11:22:51 +01001508 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001509 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001510 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001511
Chris Wilson03ac0642016-07-20 13:31:51 +01001512 obj = i915_gem_object_lookup(file, args->handle);
1513 if (!obj) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001514 ret = -ENOENT;
1515 goto unlock;
Chris Wilson76c1dec2010-09-25 11:22:51 +01001516 }
Jesse Barnes652c3932009-08-17 13:31:43 -07001517
Chris Wilson3236f572012-08-24 09:35:09 +01001518 /* Try to flush the object off the GPU without holding the lock.
1519 * We will repeat the flush holding the lock in the normal manner
1520 * to catch cases where we are gazumped.
1521 */
Chris Wilson6e4930f2014-02-07 18:37:06 -02001522 ret = i915_gem_object_wait_rendering__nonblocking(obj,
Chris Wilson2e1b8732015-04-27 13:41:22 +01001523 to_rps_client(file),
Chris Wilson6e4930f2014-02-07 18:37:06 -02001524 !write_domain);
Chris Wilson3236f572012-08-24 09:35:09 +01001525 if (ret)
1526 goto unref;
1527
Chris Wilson43566de2015-01-02 16:29:29 +05301528 if (read_domains & I915_GEM_DOMAIN_GTT)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001529 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Chris Wilson43566de2015-01-02 16:29:29 +05301530 else
Eric Anholte47c68e2008-11-14 13:35:19 -08001531 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001532
Daniel Vetter031b6982015-06-26 19:35:16 +02001533 if (write_domain != 0)
Chris Wilsonaeecc962016-06-17 14:46:39 -03001534 intel_fb_obj_invalidate(obj, write_origin(obj, write_domain));
Daniel Vetter031b6982015-06-26 19:35:16 +02001535
Chris Wilson3236f572012-08-24 09:35:09 +01001536unref:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01001537 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001538unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001539 mutex_unlock(&dev->struct_mutex);
1540 return ret;
1541}
1542
1543/**
1544 * Called when user space has done writes to this buffer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001545 * @dev: drm device
1546 * @data: ioctl data blob
1547 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001548 */
1549int
1550i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001551 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001552{
1553 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001554 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001555 int ret = 0;
1556
Chris Wilson76c1dec2010-09-25 11:22:51 +01001557 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001558 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001559 return ret;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001560
Chris Wilson03ac0642016-07-20 13:31:51 +01001561 obj = i915_gem_object_lookup(file, args->handle);
1562 if (!obj) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001563 ret = -ENOENT;
1564 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07001565 }
1566
Eric Anholt673a3942008-07-30 12:06:12 -07001567 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson2c225692013-08-09 12:26:45 +01001568 if (obj->pin_display)
Daniel Vettere62b59e2015-01-21 14:53:48 +01001569 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08001570
Chris Wilsonf8c417c2016-07-20 13:31:53 +01001571 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001572unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001573 mutex_unlock(&dev->struct_mutex);
1574 return ret;
1575}
1576
1577/**
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001578 * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
1579 * it is mapped to.
1580 * @dev: drm device
1581 * @data: ioctl data blob
1582 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001583 *
1584 * While the mapping holds a reference on the contents of the object, it doesn't
1585 * imply a ref on the object itself.
Daniel Vetter34367382014-10-16 12:28:18 +02001586 *
1587 * IMPORTANT:
1588 *
1589 * DRM driver writers who look a this function as an example for how to do GEM
1590 * mmap support, please don't implement mmap support like here. The modern way
1591 * to implement DRM mmap support is with an mmap offset ioctl (like
1592 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
1593 * That way debug tooling like valgrind will understand what's going on, hiding
1594 * the mmap call in a driver private ioctl will break that. The i915 driver only
1595 * does cpu mmaps this way because we didn't know better.
Eric Anholt673a3942008-07-30 12:06:12 -07001596 */
1597int
1598i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001599 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001600{
1601 struct drm_i915_gem_mmap *args = data;
Chris Wilson03ac0642016-07-20 13:31:51 +01001602 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001603 unsigned long addr;
1604
Akash Goel1816f922015-01-02 16:29:30 +05301605 if (args->flags & ~(I915_MMAP_WC))
1606 return -EINVAL;
1607
Borislav Petkov568a58e2016-03-29 17:42:01 +02001608 if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
Akash Goel1816f922015-01-02 16:29:30 +05301609 return -ENODEV;
1610
Chris Wilson03ac0642016-07-20 13:31:51 +01001611 obj = i915_gem_object_lookup(file, args->handle);
1612 if (!obj)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001613 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001614
Daniel Vetter1286ff72012-05-10 15:25:09 +02001615 /* prime objects have no backing filp to GEM mmap
1616 * pages from.
1617 */
Chris Wilson03ac0642016-07-20 13:31:51 +01001618 if (!obj->base.filp) {
Chris Wilson34911fd2016-07-20 13:31:54 +01001619 i915_gem_object_put_unlocked(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02001620 return -EINVAL;
1621 }
1622
Chris Wilson03ac0642016-07-20 13:31:51 +01001623 addr = vm_mmap(obj->base.filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001624 PROT_READ | PROT_WRITE, MAP_SHARED,
1625 args->offset);
Akash Goel1816f922015-01-02 16:29:30 +05301626 if (args->flags & I915_MMAP_WC) {
1627 struct mm_struct *mm = current->mm;
1628 struct vm_area_struct *vma;
1629
Michal Hocko80a89a52016-05-23 16:26:11 -07001630 if (down_write_killable(&mm->mmap_sem)) {
Chris Wilson34911fd2016-07-20 13:31:54 +01001631 i915_gem_object_put_unlocked(obj);
Michal Hocko80a89a52016-05-23 16:26:11 -07001632 return -EINTR;
1633 }
Akash Goel1816f922015-01-02 16:29:30 +05301634 vma = find_vma(mm, addr);
1635 if (vma)
1636 vma->vm_page_prot =
1637 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
1638 else
1639 addr = -ENOMEM;
1640 up_write(&mm->mmap_sem);
Chris Wilsonaeecc962016-06-17 14:46:39 -03001641
1642 /* This may race, but that's ok, it only gets set */
Chris Wilson03ac0642016-07-20 13:31:51 +01001643 WRITE_ONCE(obj->has_wc_mmap, true);
Akash Goel1816f922015-01-02 16:29:30 +05301644 }
Chris Wilson34911fd2016-07-20 13:31:54 +01001645 i915_gem_object_put_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001646 if (IS_ERR((void *)addr))
1647 return addr;
1648
1649 args->addr_ptr = (uint64_t) addr;
1650
1651 return 0;
1652}
1653
Jesse Barnesde151cf2008-11-12 10:03:55 -08001654/**
1655 * i915_gem_fault - fault a page into the GTT
Geliang Tangd9072a32015-09-15 05:58:44 -07001656 * @vma: VMA in question
1657 * @vmf: fault info
Jesse Barnesde151cf2008-11-12 10:03:55 -08001658 *
1659 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1660 * from userspace. The fault handler takes care of binding the object to
1661 * the GTT (if needed), allocating and programming a fence register (again,
1662 * only if needed based on whether the old reg is still valid or the object
1663 * is tiled) and inserting a new PTE into the faulting process.
1664 *
1665 * Note that the faulting process may involve evicting existing objects
1666 * from the GTT and/or fence registers to make room. So performance may
1667 * suffer if the GTT working set is large or there are few fence registers
1668 * left.
1669 */
1670int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1671{
Chris Wilson05394f32010-11-08 19:18:58 +00001672 struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
1673 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001674 struct drm_i915_private *dev_priv = to_i915(dev);
1675 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001676 struct i915_ggtt_view view = i915_ggtt_view_normal;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001677 pgoff_t page_offset;
1678 unsigned long pfn;
1679 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001680 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001681
Paulo Zanonif65c9162013-11-27 18:20:34 -02001682 intel_runtime_pm_get(dev_priv);
1683
Jesse Barnesde151cf2008-11-12 10:03:55 -08001684 /* We don't use vmf->pgoff since that has the fake offset */
1685 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1686 PAGE_SHIFT;
1687
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001688 ret = i915_mutex_lock_interruptible(dev);
1689 if (ret)
1690 goto out;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001691
Chris Wilsondb53a302011-02-03 11:57:46 +00001692 trace_i915_gem_object_fault(obj, page_offset, true, write);
1693
Chris Wilson6e4930f2014-02-07 18:37:06 -02001694 /* Try to flush the object off the GPU first without holding the lock.
1695 * Upon reacquiring the lock, we will perform our sanity checks and then
1696 * repeat the flush holding the lock in the normal manner to catch cases
1697 * where we are gazumped.
1698 */
1699 ret = i915_gem_object_wait_rendering__nonblocking(obj, NULL, !write);
1700 if (ret)
1701 goto unlock;
1702
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001703 /* Access to snoopable pages through the GTT is incoherent. */
1704 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev)) {
Chris Wilsonddeff6e2014-05-28 16:16:41 +01001705 ret = -EFAULT;
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001706 goto unlock;
1707 }
1708
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001709 /* Use a partial view if the object is bigger than the aperture. */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001710 if (obj->base.size >= ggtt->mappable_end &&
Joonas Lahtinene7ded2d2015-05-08 14:37:39 +03001711 obj->tiling_mode == I915_TILING_NONE) {
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001712 static const unsigned int chunk_size = 256; // 1 MiB
Joonas Lahtinene7ded2d2015-05-08 14:37:39 +03001713
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001714 memset(&view, 0, sizeof(view));
1715 view.type = I915_GGTT_VIEW_PARTIAL;
1716 view.params.partial.offset = rounddown(page_offset, chunk_size);
1717 view.params.partial.size =
1718 min_t(unsigned int,
1719 chunk_size,
1720 (vma->vm_end - vma->vm_start)/PAGE_SIZE -
1721 view.params.partial.offset);
1722 }
1723
1724 /* Now pin it into the GTT if needed */
1725 ret = i915_gem_object_ggtt_pin(obj, &view, 0, PIN_MAPPABLE);
Chris Wilsond9e86c02010-11-10 16:40:20 +00001726 if (ret)
1727 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001728
Chris Wilsonc9839302012-11-20 10:45:17 +00001729 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1730 if (ret)
1731 goto unpin;
1732
1733 ret = i915_gem_object_get_fence(obj);
1734 if (ret)
1735 goto unpin;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001736
Chris Wilsonb90b91d2014-06-10 12:14:40 +01001737 /* Finally, remap it using the new GTT offset */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001738 pfn = ggtt->mappable_base +
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001739 i915_gem_obj_ggtt_offset_view(obj, &view);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07001740 pfn >>= PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001741
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001742 if (unlikely(view.type == I915_GGTT_VIEW_PARTIAL)) {
1743 /* Overriding existing pages in partial view does not cause
1744 * us any trouble as TLBs are still valid because the fault
1745 * is due to userspace losing part of the mapping or never
1746 * having accessed it before (at this partials' range).
1747 */
1748 unsigned long base = vma->vm_start +
1749 (view.params.partial.offset << PAGE_SHIFT);
1750 unsigned int i;
Chris Wilsonb90b91d2014-06-10 12:14:40 +01001751
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001752 for (i = 0; i < view.params.partial.size; i++) {
1753 ret = vm_insert_pfn(vma, base + i * PAGE_SIZE, pfn + i);
Chris Wilsonb90b91d2014-06-10 12:14:40 +01001754 if (ret)
1755 break;
1756 }
1757
1758 obj->fault_mappable = true;
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001759 } else {
1760 if (!obj->fault_mappable) {
1761 unsigned long size = min_t(unsigned long,
1762 vma->vm_end - vma->vm_start,
1763 obj->base.size);
1764 int i;
1765
1766 for (i = 0; i < size >> PAGE_SHIFT; i++) {
1767 ret = vm_insert_pfn(vma,
1768 (unsigned long)vma->vm_start + i * PAGE_SIZE,
1769 pfn + i);
1770 if (ret)
1771 break;
1772 }
1773
1774 obj->fault_mappable = true;
1775 } else
1776 ret = vm_insert_pfn(vma,
1777 (unsigned long)vmf->virtual_address,
1778 pfn + page_offset);
1779 }
Chris Wilsonc9839302012-11-20 10:45:17 +00001780unpin:
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001781 i915_gem_object_ggtt_unpin_view(obj, &view);
Chris Wilsonc7150892009-09-23 00:43:56 +01001782unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001783 mutex_unlock(&dev->struct_mutex);
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001784out:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001785 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001786 case -EIO:
Daniel Vetter2232f032014-09-04 09:36:18 +02001787 /*
1788 * We eat errors when the gpu is terminally wedged to avoid
1789 * userspace unduly crashing (gl has no provisions for mmaps to
1790 * fail). But any other -EIO isn't ours (e.g. swap in failure)
1791 * and so needs to be reported.
1792 */
1793 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
Paulo Zanonif65c9162013-11-27 18:20:34 -02001794 ret = VM_FAULT_SIGBUS;
1795 break;
1796 }
Chris Wilson045e7692010-11-07 09:18:22 +00001797 case -EAGAIN:
Daniel Vetter571c6082013-09-12 17:57:28 +02001798 /*
1799 * EAGAIN means the gpu is hung and we'll wait for the error
1800 * handler to reset everything when re-faulting in
1801 * i915_mutex_lock_interruptible.
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001802 */
Chris Wilsonc7150892009-09-23 00:43:56 +01001803 case 0:
1804 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00001805 case -EINTR:
Dmitry Rogozhkine79e0fe2012-10-03 17:15:26 +03001806 case -EBUSY:
1807 /*
1808 * EBUSY is ok: this just means that another thread
1809 * already did the job.
1810 */
Paulo Zanonif65c9162013-11-27 18:20:34 -02001811 ret = VM_FAULT_NOPAGE;
1812 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001813 case -ENOMEM:
Paulo Zanonif65c9162013-11-27 18:20:34 -02001814 ret = VM_FAULT_OOM;
1815 break;
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02001816 case -ENOSPC:
Chris Wilson45d67812014-01-31 11:34:57 +00001817 case -EFAULT:
Paulo Zanonif65c9162013-11-27 18:20:34 -02001818 ret = VM_FAULT_SIGBUS;
1819 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001820 default:
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02001821 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
Paulo Zanonif65c9162013-11-27 18:20:34 -02001822 ret = VM_FAULT_SIGBUS;
1823 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001824 }
Paulo Zanonif65c9162013-11-27 18:20:34 -02001825
1826 intel_runtime_pm_put(dev_priv);
1827 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001828}
1829
1830/**
Chris Wilson901782b2009-07-10 08:18:50 +01001831 * i915_gem_release_mmap - remove physical page mappings
1832 * @obj: obj in question
1833 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001834 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001835 * relinquish ownership of the pages back to the system.
1836 *
1837 * It is vital that we remove the page mapping if we have mapped a tiled
1838 * object through the GTT and then lose the fence register due to
1839 * resource pressure. Similarly if the object has been moved out of the
1840 * aperture, than pages mapped into userspace must be revoked. Removing the
1841 * mapping will then trigger a page fault on the next user access, allowing
1842 * fixup by i915_gem_fault().
1843 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001844void
Chris Wilson05394f32010-11-08 19:18:58 +00001845i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01001846{
Chris Wilson349f2cc2016-04-13 17:35:12 +01001847 /* Serialisation between user GTT access and our code depends upon
1848 * revoking the CPU's PTE whilst the mutex is held. The next user
1849 * pagefault then has to wait until we release the mutex.
1850 */
1851 lockdep_assert_held(&obj->base.dev->struct_mutex);
1852
Chris Wilson6299f992010-11-24 12:23:44 +00001853 if (!obj->fault_mappable)
1854 return;
Chris Wilson901782b2009-07-10 08:18:50 +01001855
David Herrmann6796cb12014-01-03 14:24:19 +01001856 drm_vma_node_unmap(&obj->base.vma_node,
1857 obj->base.dev->anon_inode->i_mapping);
Chris Wilson349f2cc2016-04-13 17:35:12 +01001858
1859 /* Ensure that the CPU's PTE are revoked and there are not outstanding
1860 * memory transactions from userspace before we return. The TLB
1861 * flushing implied above by changing the PTE above *should* be
1862 * sufficient, an extra barrier here just provides us with a bit
1863 * of paranoid documentation about our requirement to serialise
1864 * memory writes before touching registers / GSM.
1865 */
1866 wmb();
1867
Chris Wilson6299f992010-11-24 12:23:44 +00001868 obj->fault_mappable = false;
Chris Wilson901782b2009-07-10 08:18:50 +01001869}
1870
Chris Wilsoneedd10f2014-06-16 08:57:44 +01001871void
1872i915_gem_release_all_mmaps(struct drm_i915_private *dev_priv)
1873{
1874 struct drm_i915_gem_object *obj;
1875
1876 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
1877 i915_gem_release_mmap(obj);
1878}
1879
Imre Deak0fa87792013-01-07 21:47:35 +02001880uint32_t
Chris Wilsone28f8712011-07-18 13:11:49 -07001881i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
Chris Wilson92b88ae2010-11-09 11:47:32 +00001882{
Chris Wilsone28f8712011-07-18 13:11:49 -07001883 uint32_t gtt_size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001884
1885 if (INTEL_INFO(dev)->gen >= 4 ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001886 tiling_mode == I915_TILING_NONE)
1887 return size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001888
1889 /* Previous chips need a power-of-two fence region when tiling */
Tvrtko Ursulin7e22dbb2016-05-10 10:57:06 +01001890 if (IS_GEN3(dev))
Chris Wilsone28f8712011-07-18 13:11:49 -07001891 gtt_size = 1024*1024;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001892 else
Chris Wilsone28f8712011-07-18 13:11:49 -07001893 gtt_size = 512*1024;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001894
Chris Wilsone28f8712011-07-18 13:11:49 -07001895 while (gtt_size < size)
1896 gtt_size <<= 1;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001897
Chris Wilsone28f8712011-07-18 13:11:49 -07001898 return gtt_size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001899}
1900
Jesse Barnesde151cf2008-11-12 10:03:55 -08001901/**
1902 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001903 * @dev: drm device
1904 * @size: object size
1905 * @tiling_mode: tiling mode
1906 * @fenced: is fenced alignemned required or not
Jesse Barnesde151cf2008-11-12 10:03:55 -08001907 *
1908 * Return the required GTT alignment for an object, taking into account
Daniel Vetter5e783302010-11-14 22:32:36 +01001909 * potential fence register mapping.
Jesse Barnesde151cf2008-11-12 10:03:55 -08001910 */
Imre Deakd865110c2013-01-07 21:47:33 +02001911uint32_t
1912i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
1913 int tiling_mode, bool fenced)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001914{
Jesse Barnesde151cf2008-11-12 10:03:55 -08001915 /*
1916 * Minimum alignment is 4k (GTT page size), but might be greater
1917 * if a fence register is needed for the object.
1918 */
Imre Deakd865110c2013-01-07 21:47:33 +02001919 if (INTEL_INFO(dev)->gen >= 4 || (!fenced && IS_G33(dev)) ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001920 tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001921 return 4096;
1922
1923 /*
1924 * Previous chips need to be aligned to the size of the smallest
1925 * fence register that can contain the object.
1926 */
Chris Wilsone28f8712011-07-18 13:11:49 -07001927 return i915_gem_get_gtt_size(dev, size, tiling_mode);
Chris Wilsona00b10c2010-09-24 21:15:47 +01001928}
1929
Chris Wilsond8cb5082012-08-11 15:41:03 +01001930static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
1931{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001932 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsond8cb5082012-08-11 15:41:03 +01001933 int ret;
1934
Daniel Vetterda494d72012-12-20 15:11:16 +01001935 dev_priv->mm.shrinker_no_lock_stealing = true;
1936
Chris Wilsond8cb5082012-08-11 15:41:03 +01001937 ret = drm_gem_create_mmap_offset(&obj->base);
1938 if (ret != -ENOSPC)
Daniel Vetterda494d72012-12-20 15:11:16 +01001939 goto out;
Chris Wilsond8cb5082012-08-11 15:41:03 +01001940
1941 /* Badly fragmented mmap space? The only way we can recover
1942 * space is by destroying unwanted objects. We can't randomly release
1943 * mmap_offsets as userspace expects them to be persistent for the
1944 * lifetime of the objects. The closest we can is to release the
1945 * offsets on purgeable objects by truncating it and marking it purged,
1946 * which prevents userspace from ever using that object again.
1947 */
Chris Wilson21ab4e72014-09-09 11:16:08 +01001948 i915_gem_shrink(dev_priv,
1949 obj->base.size >> PAGE_SHIFT,
1950 I915_SHRINK_BOUND |
1951 I915_SHRINK_UNBOUND |
1952 I915_SHRINK_PURGEABLE);
Chris Wilsond8cb5082012-08-11 15:41:03 +01001953 ret = drm_gem_create_mmap_offset(&obj->base);
1954 if (ret != -ENOSPC)
Daniel Vetterda494d72012-12-20 15:11:16 +01001955 goto out;
Chris Wilsond8cb5082012-08-11 15:41:03 +01001956
1957 i915_gem_shrink_all(dev_priv);
Daniel Vetterda494d72012-12-20 15:11:16 +01001958 ret = drm_gem_create_mmap_offset(&obj->base);
1959out:
1960 dev_priv->mm.shrinker_no_lock_stealing = false;
1961
1962 return ret;
Chris Wilsond8cb5082012-08-11 15:41:03 +01001963}
1964
1965static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
1966{
Chris Wilsond8cb5082012-08-11 15:41:03 +01001967 drm_gem_free_mmap_offset(&obj->base);
1968}
1969
Dave Airlieda6b51d2014-12-24 13:11:17 +10001970int
Dave Airlieff72145b2011-02-07 12:16:14 +10001971i915_gem_mmap_gtt(struct drm_file *file,
1972 struct drm_device *dev,
Dave Airlieda6b51d2014-12-24 13:11:17 +10001973 uint32_t handle,
Dave Airlieff72145b2011-02-07 12:16:14 +10001974 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001975{
Chris Wilson05394f32010-11-08 19:18:58 +00001976 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001977 int ret;
1978
Chris Wilson76c1dec2010-09-25 11:22:51 +01001979 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001980 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001981 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001982
Chris Wilson03ac0642016-07-20 13:31:51 +01001983 obj = i915_gem_object_lookup(file, handle);
1984 if (!obj) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001985 ret = -ENOENT;
1986 goto unlock;
1987 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001988
Chris Wilson05394f32010-11-08 19:18:58 +00001989 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonbd9b6a42014-02-10 09:03:50 +00001990 DRM_DEBUG("Attempting to mmap a purgeable buffer\n");
Chris Wilson8c99e572014-01-31 11:34:58 +00001991 ret = -EFAULT;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001992 goto out;
Chris Wilsonab182822009-09-22 18:46:17 +01001993 }
1994
Chris Wilsond8cb5082012-08-11 15:41:03 +01001995 ret = i915_gem_object_create_mmap_offset(obj);
1996 if (ret)
1997 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001998
David Herrmann0de23972013-07-24 21:07:52 +02001999 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002000
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002001out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01002002 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002003unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002004 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002005 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002006}
2007
Dave Airlieff72145b2011-02-07 12:16:14 +10002008/**
2009 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
2010 * @dev: DRM device
2011 * @data: GTT mapping ioctl data
2012 * @file: GEM object info
2013 *
2014 * Simply returns the fake offset to userspace so it can mmap it.
2015 * The mmap call will end up in drm_gem_mmap(), which will set things
2016 * up so we can get faults in the handler above.
2017 *
2018 * The fault handler will take care of binding the object into the GTT
2019 * (since it may have been evicted to make room for something), allocating
2020 * a fence register, and mapping the appropriate aperture address into
2021 * userspace.
2022 */
2023int
2024i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
2025 struct drm_file *file)
2026{
2027 struct drm_i915_gem_mmap_gtt *args = data;
2028
Dave Airlieda6b51d2014-12-24 13:11:17 +10002029 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
Dave Airlieff72145b2011-02-07 12:16:14 +10002030}
2031
Daniel Vetter225067e2012-08-20 10:23:20 +02002032/* Immediately discard the backing storage */
2033static void
2034i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01002035{
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002036 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02002037
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002038 if (obj->base.filp == NULL)
2039 return;
2040
Daniel Vetter225067e2012-08-20 10:23:20 +02002041 /* Our goal here is to return as much of the memory as
2042 * is possible back to the system as we are called from OOM.
2043 * To do this we must instruct the shmfs to drop all of its
2044 * backing pages, *now*.
Chris Wilsone5281cc2010-10-28 13:45:36 +01002045 */
Chris Wilson55372522014-03-25 13:23:06 +00002046 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
Daniel Vetter225067e2012-08-20 10:23:20 +02002047 obj->madv = __I915_MADV_PURGED;
Chris Wilsone5281cc2010-10-28 13:45:36 +01002048}
Chris Wilsone5281cc2010-10-28 13:45:36 +01002049
Chris Wilson55372522014-03-25 13:23:06 +00002050/* Try to discard unwanted pages */
2051static void
2052i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
Daniel Vetter225067e2012-08-20 10:23:20 +02002053{
Chris Wilson55372522014-03-25 13:23:06 +00002054 struct address_space *mapping;
2055
2056 switch (obj->madv) {
2057 case I915_MADV_DONTNEED:
2058 i915_gem_object_truncate(obj);
2059 case __I915_MADV_PURGED:
2060 return;
2061 }
2062
2063 if (obj->base.filp == NULL)
2064 return;
2065
2066 mapping = file_inode(obj->base.filp)->i_mapping,
2067 invalidate_mapping_pages(mapping, 0, (loff_t)-1);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002068}
2069
Chris Wilson5cdf5882010-09-27 15:51:07 +01002070static void
Chris Wilson05394f32010-11-08 19:18:58 +00002071i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002072{
Dave Gordon85d12252016-05-20 11:54:06 +01002073 struct sgt_iter sgt_iter;
2074 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002075 int ret;
Daniel Vetter1286ff72012-05-10 15:25:09 +02002076
Chris Wilson05394f32010-11-08 19:18:58 +00002077 BUG_ON(obj->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07002078
Chris Wilson6c085a72012-08-20 11:40:46 +02002079 ret = i915_gem_object_set_to_cpu_domain(obj, true);
Chris Wilsonf4457ae2016-04-13 17:35:08 +01002080 if (WARN_ON(ret)) {
Chris Wilson6c085a72012-08-20 11:40:46 +02002081 /* In the event of a disaster, abandon all caches and
2082 * hope for the best.
2083 */
Chris Wilson2c225692013-08-09 12:26:45 +01002084 i915_gem_clflush_object(obj, true);
Chris Wilson6c085a72012-08-20 11:40:46 +02002085 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
2086 }
2087
Imre Deake2273302015-07-09 12:59:05 +03002088 i915_gem_gtt_finish_object(obj);
2089
Daniel Vetter6dacfd22011-09-12 21:30:02 +02002090 if (i915_gem_object_needs_bit17_swizzle(obj))
Eric Anholt280b7132009-03-12 16:56:27 -07002091 i915_gem_object_save_bit_17_swizzle(obj);
2092
Chris Wilson05394f32010-11-08 19:18:58 +00002093 if (obj->madv == I915_MADV_DONTNEED)
2094 obj->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01002095
Dave Gordon85d12252016-05-20 11:54:06 +01002096 for_each_sgt_page(page, sgt_iter, obj->pages) {
Chris Wilson05394f32010-11-08 19:18:58 +00002097 if (obj->dirty)
Chris Wilson9da3da62012-06-01 15:20:22 +01002098 set_page_dirty(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002099
Chris Wilson05394f32010-11-08 19:18:58 +00002100 if (obj->madv == I915_MADV_WILLNEED)
Chris Wilson9da3da62012-06-01 15:20:22 +01002101 mark_page_accessed(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002102
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002103 put_page(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002104 }
Chris Wilson05394f32010-11-08 19:18:58 +00002105 obj->dirty = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002106
Chris Wilson9da3da62012-06-01 15:20:22 +01002107 sg_free_table(obj->pages);
2108 kfree(obj->pages);
Chris Wilson37e680a2012-06-07 15:38:42 +01002109}
2110
Chris Wilsondd624af2013-01-15 12:39:35 +00002111int
Chris Wilson37e680a2012-06-07 15:38:42 +01002112i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
2113{
2114 const struct drm_i915_gem_object_ops *ops = obj->ops;
2115
Chris Wilson2f745ad2012-09-04 21:02:58 +01002116 if (obj->pages == NULL)
Chris Wilson37e680a2012-06-07 15:38:42 +01002117 return 0;
2118
Chris Wilsona5570172012-09-04 21:02:54 +01002119 if (obj->pages_pin_count)
2120 return -EBUSY;
2121
Chris Wilson15717de2016-08-04 07:52:26 +01002122 GEM_BUG_ON(obj->bind_count);
Ben Widawsky3e123022013-07-31 17:00:04 -07002123
Chris Wilsona2165e32012-12-03 11:49:00 +00002124 /* ->put_pages might need to allocate memory for the bit17 swizzle
2125 * array, hence protect them from being reaped by removing them from gtt
2126 * lists early. */
Ben Widawsky35c20a62013-05-31 11:28:48 -07002127 list_del(&obj->global_list);
Chris Wilsona2165e32012-12-03 11:49:00 +00002128
Chris Wilson0a798eb2016-04-08 12:11:11 +01002129 if (obj->mapping) {
Chris Wilsonfb8621d2016-04-08 12:11:14 +01002130 if (is_vmalloc_addr(obj->mapping))
2131 vunmap(obj->mapping);
2132 else
2133 kunmap(kmap_to_page(obj->mapping));
Chris Wilson0a798eb2016-04-08 12:11:11 +01002134 obj->mapping = NULL;
2135 }
2136
Chris Wilson37e680a2012-06-07 15:38:42 +01002137 ops->put_pages(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00002138 obj->pages = NULL;
Chris Wilson6c085a72012-08-20 11:40:46 +02002139
Chris Wilson55372522014-03-25 13:23:06 +00002140 i915_gem_object_invalidate(obj);
Chris Wilson6c085a72012-08-20 11:40:46 +02002141
2142 return 0;
2143}
2144
Chris Wilson37e680a2012-06-07 15:38:42 +01002145static int
Chris Wilson6c085a72012-08-20 11:40:46 +02002146i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002147{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002148 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Eric Anholt673a3942008-07-30 12:06:12 -07002149 int page_count, i;
2150 struct address_space *mapping;
Chris Wilson9da3da62012-06-01 15:20:22 +01002151 struct sg_table *st;
2152 struct scatterlist *sg;
Dave Gordon85d12252016-05-20 11:54:06 +01002153 struct sgt_iter sgt_iter;
Eric Anholt673a3942008-07-30 12:06:12 -07002154 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002155 unsigned long last_pfn = 0; /* suppress gcc warning */
Imre Deake2273302015-07-09 12:59:05 +03002156 int ret;
Chris Wilson6c085a72012-08-20 11:40:46 +02002157 gfp_t gfp;
Eric Anholt673a3942008-07-30 12:06:12 -07002158
Chris Wilson6c085a72012-08-20 11:40:46 +02002159 /* Assert that the object is not currently in any GPU domain. As it
2160 * wasn't in the GTT, there shouldn't be any way it could have been in
2161 * a GPU cache
2162 */
2163 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2164 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
2165
Chris Wilson9da3da62012-06-01 15:20:22 +01002166 st = kmalloc(sizeof(*st), GFP_KERNEL);
2167 if (st == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002168 return -ENOMEM;
2169
Chris Wilson9da3da62012-06-01 15:20:22 +01002170 page_count = obj->base.size / PAGE_SIZE;
2171 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
Chris Wilson9da3da62012-06-01 15:20:22 +01002172 kfree(st);
2173 return -ENOMEM;
2174 }
2175
2176 /* Get the list of pages out of our struct file. They'll be pinned
2177 * at this point until we release them.
2178 *
2179 * Fail silently without starting the shrinker
2180 */
Al Viro496ad9a2013-01-23 17:07:38 -05002181 mapping = file_inode(obj->base.filp)->i_mapping;
Michal Hockoc62d2552015-11-06 16:28:49 -08002182 gfp = mapping_gfp_constraint(mapping, ~(__GFP_IO | __GFP_RECLAIM));
Mel Gormand0164ad2015-11-06 16:28:21 -08002183 gfp |= __GFP_NORETRY | __GFP_NOWARN;
Imre Deak90797e62013-02-18 19:28:03 +02002184 sg = st->sgl;
2185 st->nents = 0;
2186 for (i = 0; i < page_count; i++) {
Chris Wilson6c085a72012-08-20 11:40:46 +02002187 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
2188 if (IS_ERR(page)) {
Chris Wilson21ab4e72014-09-09 11:16:08 +01002189 i915_gem_shrink(dev_priv,
2190 page_count,
2191 I915_SHRINK_BOUND |
2192 I915_SHRINK_UNBOUND |
2193 I915_SHRINK_PURGEABLE);
Chris Wilson6c085a72012-08-20 11:40:46 +02002194 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
2195 }
2196 if (IS_ERR(page)) {
2197 /* We've tried hard to allocate the memory by reaping
2198 * our own buffer, now let the real VM do its job and
2199 * go down in flames if truly OOM.
2200 */
Chris Wilson6c085a72012-08-20 11:40:46 +02002201 i915_gem_shrink_all(dev_priv);
David Herrmannf461d1b2014-05-25 14:34:10 +02002202 page = shmem_read_mapping_page(mapping, i);
Imre Deake2273302015-07-09 12:59:05 +03002203 if (IS_ERR(page)) {
2204 ret = PTR_ERR(page);
Chris Wilson6c085a72012-08-20 11:40:46 +02002205 goto err_pages;
Imre Deake2273302015-07-09 12:59:05 +03002206 }
Chris Wilson6c085a72012-08-20 11:40:46 +02002207 }
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002208#ifdef CONFIG_SWIOTLB
2209 if (swiotlb_nr_tbl()) {
2210 st->nents++;
2211 sg_set_page(sg, page, PAGE_SIZE, 0);
2212 sg = sg_next(sg);
2213 continue;
2214 }
2215#endif
Imre Deak90797e62013-02-18 19:28:03 +02002216 if (!i || page_to_pfn(page) != last_pfn + 1) {
2217 if (i)
2218 sg = sg_next(sg);
2219 st->nents++;
2220 sg_set_page(sg, page, PAGE_SIZE, 0);
2221 } else {
2222 sg->length += PAGE_SIZE;
2223 }
2224 last_pfn = page_to_pfn(page);
Daniel Vetter3bbbe702013-10-07 17:15:45 -03002225
2226 /* Check that the i965g/gm workaround works. */
2227 WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
Eric Anholt673a3942008-07-30 12:06:12 -07002228 }
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002229#ifdef CONFIG_SWIOTLB
2230 if (!swiotlb_nr_tbl())
2231#endif
2232 sg_mark_end(sg);
Chris Wilson74ce6b62012-10-19 15:51:06 +01002233 obj->pages = st;
2234
Imre Deake2273302015-07-09 12:59:05 +03002235 ret = i915_gem_gtt_prepare_object(obj);
2236 if (ret)
2237 goto err_pages;
2238
Eric Anholt673a3942008-07-30 12:06:12 -07002239 if (i915_gem_object_needs_bit17_swizzle(obj))
2240 i915_gem_object_do_bit_17_swizzle(obj);
2241
Daniel Vetter656bfa32014-11-20 09:26:30 +01002242 if (obj->tiling_mode != I915_TILING_NONE &&
2243 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES)
2244 i915_gem_object_pin_pages(obj);
2245
Eric Anholt673a3942008-07-30 12:06:12 -07002246 return 0;
2247
2248err_pages:
Imre Deak90797e62013-02-18 19:28:03 +02002249 sg_mark_end(sg);
Dave Gordon85d12252016-05-20 11:54:06 +01002250 for_each_sgt_page(page, sgt_iter, st)
2251 put_page(page);
Chris Wilson9da3da62012-06-01 15:20:22 +01002252 sg_free_table(st);
2253 kfree(st);
Chris Wilson0820baf2014-03-25 13:23:03 +00002254
2255 /* shmemfs first checks if there is enough memory to allocate the page
2256 * and reports ENOSPC should there be insufficient, along with the usual
2257 * ENOMEM for a genuine allocation failure.
2258 *
2259 * We use ENOSPC in our driver to mean that we have run out of aperture
2260 * space and so want to translate the error from shmemfs back to our
2261 * usual understanding of ENOMEM.
2262 */
Imre Deake2273302015-07-09 12:59:05 +03002263 if (ret == -ENOSPC)
2264 ret = -ENOMEM;
2265
2266 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002267}
2268
Chris Wilson37e680a2012-06-07 15:38:42 +01002269/* Ensure that the associated pages are gathered from the backing storage
2270 * and pinned into our object. i915_gem_object_get_pages() may be called
2271 * multiple times before they are released by a single call to
2272 * i915_gem_object_put_pages() - once the pages are no longer referenced
2273 * either as a result of memory pressure (reaping pages under the shrinker)
2274 * or as the object is itself released.
2275 */
2276int
2277i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
2278{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002279 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson37e680a2012-06-07 15:38:42 +01002280 const struct drm_i915_gem_object_ops *ops = obj->ops;
2281 int ret;
2282
Chris Wilson2f745ad2012-09-04 21:02:58 +01002283 if (obj->pages)
Chris Wilson37e680a2012-06-07 15:38:42 +01002284 return 0;
2285
Chris Wilson43e28f02013-01-08 10:53:09 +00002286 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonbd9b6a42014-02-10 09:03:50 +00002287 DRM_DEBUG("Attempting to obtain a purgeable object\n");
Chris Wilson8c99e572014-01-31 11:34:58 +00002288 return -EFAULT;
Chris Wilson43e28f02013-01-08 10:53:09 +00002289 }
2290
Chris Wilsona5570172012-09-04 21:02:54 +01002291 BUG_ON(obj->pages_pin_count);
2292
Chris Wilson37e680a2012-06-07 15:38:42 +01002293 ret = ops->get_pages(obj);
2294 if (ret)
2295 return ret;
2296
Ben Widawsky35c20a62013-05-31 11:28:48 -07002297 list_add_tail(&obj->global_list, &dev_priv->mm.unbound_list);
Chris Wilsonee286372015-04-07 16:20:25 +01002298
2299 obj->get_page.sg = obj->pages->sgl;
2300 obj->get_page.last = 0;
2301
Chris Wilson37e680a2012-06-07 15:38:42 +01002302 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002303}
2304
Dave Gordondd6034c2016-05-20 11:54:04 +01002305/* The 'mapping' part of i915_gem_object_pin_map() below */
2306static void *i915_gem_object_map(const struct drm_i915_gem_object *obj)
2307{
2308 unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
2309 struct sg_table *sgt = obj->pages;
Dave Gordon85d12252016-05-20 11:54:06 +01002310 struct sgt_iter sgt_iter;
2311 struct page *page;
Dave Gordonb338fa42016-05-20 11:54:05 +01002312 struct page *stack_pages[32];
2313 struct page **pages = stack_pages;
Dave Gordondd6034c2016-05-20 11:54:04 +01002314 unsigned long i = 0;
2315 void *addr;
2316
2317 /* A single page can always be kmapped */
2318 if (n_pages == 1)
2319 return kmap(sg_page(sgt->sgl));
2320
Dave Gordonb338fa42016-05-20 11:54:05 +01002321 if (n_pages > ARRAY_SIZE(stack_pages)) {
2322 /* Too big for stack -- allocate temporary array instead */
2323 pages = drm_malloc_gfp(n_pages, sizeof(*pages), GFP_TEMPORARY);
2324 if (!pages)
2325 return NULL;
2326 }
Dave Gordondd6034c2016-05-20 11:54:04 +01002327
Dave Gordon85d12252016-05-20 11:54:06 +01002328 for_each_sgt_page(page, sgt_iter, sgt)
2329 pages[i++] = page;
Dave Gordondd6034c2016-05-20 11:54:04 +01002330
2331 /* Check that we have the expected number of pages */
2332 GEM_BUG_ON(i != n_pages);
2333
2334 addr = vmap(pages, n_pages, 0, PAGE_KERNEL);
2335
Dave Gordonb338fa42016-05-20 11:54:05 +01002336 if (pages != stack_pages)
2337 drm_free_large(pages);
Dave Gordondd6034c2016-05-20 11:54:04 +01002338
2339 return addr;
2340}
2341
2342/* get, pin, and map the pages of the object into kernel space */
Chris Wilson0a798eb2016-04-08 12:11:11 +01002343void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj)
2344{
2345 int ret;
2346
2347 lockdep_assert_held(&obj->base.dev->struct_mutex);
2348
2349 ret = i915_gem_object_get_pages(obj);
2350 if (ret)
2351 return ERR_PTR(ret);
2352
2353 i915_gem_object_pin_pages(obj);
2354
Dave Gordondd6034c2016-05-20 11:54:04 +01002355 if (!obj->mapping) {
2356 obj->mapping = i915_gem_object_map(obj);
2357 if (!obj->mapping) {
Chris Wilson0a798eb2016-04-08 12:11:11 +01002358 i915_gem_object_unpin_pages(obj);
2359 return ERR_PTR(-ENOMEM);
2360 }
2361 }
2362
2363 return obj->mapping;
2364}
2365
Ben Widawskye2d05a82013-09-24 09:57:58 -07002366void i915_vma_move_to_active(struct i915_vma *vma,
John Harrisonb2af0372015-05-29 17:43:50 +01002367 struct drm_i915_gem_request *req)
Ben Widawskye2d05a82013-09-24 09:57:58 -07002368{
Chris Wilsonb4716182015-04-27 13:41:17 +01002369 struct drm_i915_gem_object *obj = vma->obj;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002370 struct intel_engine_cs *engine;
John Harrisonb2af0372015-05-29 17:43:50 +01002371
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00002372 engine = i915_gem_request_get_engine(req);
Chris Wilsonb4716182015-04-27 13:41:17 +01002373
2374 /* Add a reference if we're newly entering the active list. */
2375 if (obj->active == 0)
Chris Wilson25dc5562016-07-20 13:31:52 +01002376 i915_gem_object_get(obj);
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00002377 obj->active |= intel_engine_flag(engine);
Chris Wilsonb4716182015-04-27 13:41:17 +01002378
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00002379 list_move_tail(&obj->engine_list[engine->id], &engine->active_list);
Chris Wilson381f3712016-08-04 07:52:29 +01002380 i915_gem_active_set(&obj->last_read[engine->id], req);
Chris Wilsonb4716182015-04-27 13:41:17 +01002381
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00002382 list_move_tail(&vma->vm_link, &vma->vm->active_list);
Ben Widawskye2d05a82013-09-24 09:57:58 -07002383}
2384
Chris Wilsoncaea7472010-11-12 13:53:37 +00002385static void
Chris Wilsonb4716182015-04-27 13:41:17 +01002386i915_gem_object_retire__write(struct drm_i915_gem_object *obj)
2387{
Chris Wilson27c01aa2016-08-04 07:52:30 +01002388 GEM_BUG_ON(!i915_gem_active_isset(&obj->last_write));
Chris Wilsond72d9082016-08-04 07:52:31 +01002389 GEM_BUG_ON(!(obj->active &
2390 intel_engine_flag(i915_gem_active_get_engine(&obj->last_write,
2391 &obj->base.dev->struct_mutex))));
Chris Wilsonb4716182015-04-27 13:41:17 +01002392
Chris Wilson381f3712016-08-04 07:52:29 +01002393 i915_gem_active_set(&obj->last_write, NULL);
Rodrigo Vivide152b62015-07-07 16:28:51 -07002394 intel_fb_obj_flush(obj, true, ORIGIN_CS);
Chris Wilsonb4716182015-04-27 13:41:17 +01002395}
2396
2397static void
Chris Wilson7e21d642016-07-27 09:07:29 +01002398i915_gem_object_retire__read(struct drm_i915_gem_object *obj, int idx)
Chris Wilsoncaea7472010-11-12 13:53:37 +00002399{
Chris Wilson27c01aa2016-08-04 07:52:30 +01002400 struct intel_engine_cs *engine;
Ben Widawskyfeb822c2013-12-06 14:10:51 -08002401 struct i915_vma *vma;
Chris Wilsoncaea7472010-11-12 13:53:37 +00002402
Chris Wilson27c01aa2016-08-04 07:52:30 +01002403 GEM_BUG_ON(!i915_gem_active_isset(&obj->last_read[idx]));
Chris Wilson7e21d642016-07-27 09:07:29 +01002404 GEM_BUG_ON(!(obj->active & (1 << idx)));
Chris Wilsonb4716182015-04-27 13:41:17 +01002405
Chris Wilson7e21d642016-07-27 09:07:29 +01002406 list_del_init(&obj->engine_list[idx]);
Chris Wilson381f3712016-08-04 07:52:29 +01002407 i915_gem_active_set(&obj->last_read[idx], NULL);
Chris Wilsonb4716182015-04-27 13:41:17 +01002408
Chris Wilsond72d9082016-08-04 07:52:31 +01002409 engine = i915_gem_active_get_engine(&obj->last_write,
2410 &obj->base.dev->struct_mutex);
Chris Wilson27c01aa2016-08-04 07:52:30 +01002411 if (engine && engine->id == idx)
Chris Wilsonb4716182015-04-27 13:41:17 +01002412 i915_gem_object_retire__write(obj);
2413
Chris Wilson7e21d642016-07-27 09:07:29 +01002414 obj->active &= ~(1 << idx);
Chris Wilsonb4716182015-04-27 13:41:17 +01002415 if (obj->active)
2416 return;
Chris Wilson65ce3022012-07-20 12:41:02 +01002417
Chris Wilson6c246952015-07-27 10:26:26 +01002418 /* Bump our place on the bound list to keep it roughly in LRU order
2419 * so that we don't steal from recently used but inactive objects
2420 * (unless we are forced to ofc!)
2421 */
2422 list_move_tail(&obj->global_list,
2423 &to_i915(obj->base.dev)->mm.bound_list);
2424
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00002425 list_for_each_entry(vma, &obj->vma_list, obj_link) {
2426 if (!list_empty(&vma->vm_link))
2427 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
Ben Widawskyfeb822c2013-12-06 14:10:51 -08002428 }
Chris Wilsoncaea7472010-11-12 13:53:37 +00002429
Chris Wilson381f3712016-08-04 07:52:29 +01002430 i915_gem_active_set(&obj->last_fence, NULL);
Chris Wilsonf8c417c2016-07-20 13:31:53 +01002431 i915_gem_object_put(obj);
Chris Wilsonc8725f32014-03-17 12:21:55 +00002432}
2433
Chris Wilson7b4d3a12016-07-04 08:08:37 +01002434static bool i915_context_is_banned(const struct i915_gem_context *ctx)
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002435{
Mika Kuoppala44e2c072014-01-30 16:01:15 +02002436 unsigned long elapsed;
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002437
Mika Kuoppala44e2c072014-01-30 16:01:15 +02002438 if (ctx->hang_stats.banned)
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002439 return true;
2440
Chris Wilson7b4d3a12016-07-04 08:08:37 +01002441 elapsed = get_seconds() - ctx->hang_stats.guilty_ts;
Chris Wilson676fa572014-12-24 08:13:39 -08002442 if (ctx->hang_stats.ban_period_seconds &&
2443 elapsed <= ctx->hang_stats.ban_period_seconds) {
Chris Wilson7b4d3a12016-07-04 08:08:37 +01002444 DRM_DEBUG("context hanging too fast, banning!\n");
2445 return true;
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002446 }
2447
2448 return false;
2449}
2450
Chris Wilson7b4d3a12016-07-04 08:08:37 +01002451static void i915_set_reset_status(struct i915_gem_context *ctx,
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002452 const bool guilty)
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002453{
Chris Wilson7b4d3a12016-07-04 08:08:37 +01002454 struct i915_ctx_hang_stats *hs = &ctx->hang_stats;
Mika Kuoppala44e2c072014-01-30 16:01:15 +02002455
2456 if (guilty) {
Chris Wilson7b4d3a12016-07-04 08:08:37 +01002457 hs->banned = i915_context_is_banned(ctx);
Mika Kuoppala44e2c072014-01-30 16:01:15 +02002458 hs->batch_active++;
2459 hs->guilty_ts = get_seconds();
2460 } else {
2461 hs->batch_pending++;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002462 }
2463}
2464
Chris Wilson8d9fc7f2014-02-25 17:11:23 +02002465struct drm_i915_gem_request *
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002466i915_gem_find_active_request(struct intel_engine_cs *engine)
Chris Wilson9375e442010-09-19 12:21:28 +01002467{
Chris Wilson4db080f2013-12-04 11:37:09 +00002468 struct drm_i915_gem_request *request;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002469
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002470 /* We are called by the error capture and reset at a random
2471 * point in time. In particular, note that neither is crucially
2472 * ordered with an interrupt. After a hang, the GPU is dead and we
2473 * assume that no more writes can happen (we waited long enough for
2474 * all writes that were in transaction to be flushed) - adding an
2475 * extra delay for a recent interrupt is pointless. Hence, we do
2476 * not need an engine->irq_seqno_barrier() before the seqno reads.
2477 */
Chris Wilsonefdf7c02016-08-04 07:52:33 +01002478 list_for_each_entry(request, &engine->request_list, link) {
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002479 if (i915_gem_request_completed(request))
Chris Wilson4db080f2013-12-04 11:37:09 +00002480 continue;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002481
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002482 return request;
Chris Wilson4db080f2013-12-04 11:37:09 +00002483 }
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002484
2485 return NULL;
2486}
2487
Chris Wilson7b4d3a12016-07-04 08:08:37 +01002488static void i915_gem_reset_engine_status(struct intel_engine_cs *engine)
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002489{
2490 struct drm_i915_gem_request *request;
2491 bool ring_hung;
2492
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002493 request = i915_gem_find_active_request(engine);
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002494 if (request == NULL)
2495 return;
2496
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002497 ring_hung = engine->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002498
Chris Wilson7b4d3a12016-07-04 08:08:37 +01002499 i915_set_reset_status(request->ctx, ring_hung);
Chris Wilsonefdf7c02016-08-04 07:52:33 +01002500 list_for_each_entry_continue(request, &engine->request_list, link)
Chris Wilson7b4d3a12016-07-04 08:08:37 +01002501 i915_set_reset_status(request->ctx, false);
Chris Wilson4db080f2013-12-04 11:37:09 +00002502}
2503
Chris Wilson7b4d3a12016-07-04 08:08:37 +01002504static void i915_gem_reset_engine_cleanup(struct intel_engine_cs *engine)
Chris Wilson4db080f2013-12-04 11:37:09 +00002505{
Chris Wilson7e37f882016-08-02 22:50:21 +01002506 struct intel_ring *ring;
Chris Wilson608c1a52015-09-03 13:01:40 +01002507
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002508 while (!list_empty(&engine->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00002509 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07002510
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002511 obj = list_first_entry(&engine->active_list,
Chris Wilson05394f32010-11-08 19:18:58 +00002512 struct drm_i915_gem_object,
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00002513 engine_list[engine->id]);
Eric Anholt673a3942008-07-30 12:06:12 -07002514
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002515 i915_gem_object_retire__read(obj, engine->id);
Eric Anholt673a3942008-07-30 12:06:12 -07002516 }
Ben Widawsky1d62bee2014-01-01 10:15:13 -08002517
Chris Wilsonc4b09302016-07-20 09:21:10 +01002518 /* Mark all pending requests as complete so that any concurrent
2519 * (lockless) lookup doesn't try and wait upon the request as we
2520 * reset it.
2521 */
Chris Wilson7e37f882016-08-02 22:50:21 +01002522 intel_engine_init_seqno(engine, engine->last_submitted_seqno);
Chris Wilsonc4b09302016-07-20 09:21:10 +01002523
Ben Widawsky1d62bee2014-01-01 10:15:13 -08002524 /*
Oscar Mateodcb4c122014-11-13 10:28:10 +00002525 * Clear the execlists queue up before freeing the requests, as those
2526 * are the ones that keep the context and ringbuffer backing objects
2527 * pinned in place.
2528 */
Oscar Mateodcb4c122014-11-13 10:28:10 +00002529
Tomas Elf7de1691a2015-10-19 16:32:32 +01002530 if (i915.enable_execlists) {
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +01002531 /* Ensure irq handler finishes or is cancelled. */
2532 tasklet_kill(&engine->irq_tasklet);
Mika Kuoppala1197b4f2015-01-13 11:32:24 +02002533
Tvrtko Ursuline39d42f2016-04-28 09:56:58 +01002534 intel_execlists_cancel_requests(engine);
Oscar Mateodcb4c122014-11-13 10:28:10 +00002535 }
2536
2537 /*
Ben Widawsky1d62bee2014-01-01 10:15:13 -08002538 * We must free the requests after all the corresponding objects have
2539 * been moved off active lists. Which is the same order as the normal
2540 * retire_requests function does. This is important if object hold
2541 * implicit references on things like e.g. ppgtt address spaces through
2542 * the request.
2543 */
Chris Wilson05235c52016-07-20 09:21:08 +01002544 if (!list_empty(&engine->request_list)) {
Ben Widawsky1d62bee2014-01-01 10:15:13 -08002545 struct drm_i915_gem_request *request;
2546
Chris Wilson05235c52016-07-20 09:21:08 +01002547 request = list_last_entry(&engine->request_list,
2548 struct drm_i915_gem_request,
Chris Wilsonefdf7c02016-08-04 07:52:33 +01002549 link);
Ben Widawsky1d62bee2014-01-01 10:15:13 -08002550
Chris Wilson05235c52016-07-20 09:21:08 +01002551 i915_gem_request_retire_upto(request);
Ben Widawsky1d62bee2014-01-01 10:15:13 -08002552 }
Chris Wilson608c1a52015-09-03 13:01:40 +01002553
2554 /* Having flushed all requests from all queues, we know that all
2555 * ringbuffers must now be empty. However, since we do not reclaim
2556 * all space when retiring the request (to prevent HEADs colliding
2557 * with rapid ringbuffer wraparound) the amount of available space
2558 * upon reset is less than when we start. Do one more pass over
2559 * all the ringbuffers to reset last_retired_head.
2560 */
Chris Wilson7e37f882016-08-02 22:50:21 +01002561 list_for_each_entry(ring, &engine->buffers, link) {
2562 ring->last_retired_head = ring->tail;
2563 intel_ring_update_space(ring);
Chris Wilson608c1a52015-09-03 13:01:40 +01002564 }
Chris Wilson2ed53a92016-04-07 07:29:11 +01002565
Chris Wilsonb913b332016-07-13 09:10:31 +01002566 engine->i915->gt.active_engines &= ~intel_engine_flag(engine);
Eric Anholt673a3942008-07-30 12:06:12 -07002567}
2568
Chris Wilson069efc12010-09-30 16:53:18 +01002569void i915_gem_reset(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07002570{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002571 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002572 struct intel_engine_cs *engine;
Eric Anholt673a3942008-07-30 12:06:12 -07002573
Chris Wilson4db080f2013-12-04 11:37:09 +00002574 /*
2575 * Before we free the objects from the requests, we need to inspect
2576 * them for finding the guilty party. As the requests only borrow
2577 * their reference to the objects, the inspection must be done first.
2578 */
Dave Gordonb4ac5af2016-03-24 11:20:38 +00002579 for_each_engine(engine, dev_priv)
Chris Wilson7b4d3a12016-07-04 08:08:37 +01002580 i915_gem_reset_engine_status(engine);
Chris Wilson4db080f2013-12-04 11:37:09 +00002581
Dave Gordonb4ac5af2016-03-24 11:20:38 +00002582 for_each_engine(engine, dev_priv)
Chris Wilson7b4d3a12016-07-04 08:08:37 +01002583 i915_gem_reset_engine_cleanup(engine);
Chris Wilsonb913b332016-07-13 09:10:31 +01002584 mod_delayed_work(dev_priv->wq, &dev_priv->gt.idle_work, 0);
Chris Wilsondfaae392010-09-22 10:31:52 +01002585
Ben Widawskyacce9ff2013-12-06 14:11:03 -08002586 i915_gem_context_reset(dev);
2587
Chris Wilson19b2dbd2013-06-12 10:15:12 +01002588 i915_gem_restore_fences(dev);
Chris Wilsonb4716182015-04-27 13:41:17 +01002589
2590 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07002591}
2592
2593/**
2594 * This function clears the request list as sequence numbers are passed.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01002595 * @engine: engine to retire requests on
Eric Anholt673a3942008-07-30 12:06:12 -07002596 */
Chris Wilson1cf0ba12014-05-05 09:07:33 +01002597void
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002598i915_gem_retire_requests_ring(struct intel_engine_cs *engine)
Eric Anholt673a3942008-07-30 12:06:12 -07002599{
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002600 WARN_ON(i915_verify_lists(engine->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07002601
Chris Wilson832a3aa2015-03-18 18:19:22 +00002602 /* Retire requests first as we use it above for the early return.
2603 * If we retire requests last, we may use a later seqno and so clear
2604 * the requests lists without clearing the active list, leading to
2605 * confusion.
Chris Wilsone9103032014-01-07 11:45:14 +00002606 */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002607 while (!list_empty(&engine->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002608 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07002609
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002610 request = list_first_entry(&engine->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07002611 struct drm_i915_gem_request,
Chris Wilsonefdf7c02016-08-04 07:52:33 +01002612 link);
Eric Anholt673a3942008-07-30 12:06:12 -07002613
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002614 if (!i915_gem_request_completed(request))
Eric Anholt673a3942008-07-30 12:06:12 -07002615 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01002616
Chris Wilson05235c52016-07-20 09:21:08 +01002617 i915_gem_request_retire_upto(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01002618 }
2619
Chris Wilson832a3aa2015-03-18 18:19:22 +00002620 /* Move any buffers on the active list that are no longer referenced
2621 * by the ringbuffer to the flushing/inactive lists as appropriate,
2622 * before we free the context associated with the requests.
2623 */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002624 while (!list_empty(&engine->active_list)) {
Chris Wilson832a3aa2015-03-18 18:19:22 +00002625 struct drm_i915_gem_object *obj;
2626
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002627 obj = list_first_entry(&engine->active_list,
2628 struct drm_i915_gem_object,
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00002629 engine_list[engine->id]);
Chris Wilson832a3aa2015-03-18 18:19:22 +00002630
Chris Wilsond72d9082016-08-04 07:52:31 +01002631 if (!list_empty(&i915_gem_active_peek(&obj->last_read[engine->id],
Chris Wilsonefdf7c02016-08-04 07:52:33 +01002632 &obj->base.dev->struct_mutex)->link))
Chris Wilson832a3aa2015-03-18 18:19:22 +00002633 break;
2634
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002635 i915_gem_object_retire__read(obj, engine->id);
Chris Wilson832a3aa2015-03-18 18:19:22 +00002636 }
2637
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002638 WARN_ON(i915_verify_lists(engine->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07002639}
2640
Chris Wilson67d97da2016-07-04 08:08:31 +01002641void i915_gem_retire_requests(struct drm_i915_private *dev_priv)
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002642{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002643 struct intel_engine_cs *engine;
Chris Wilson67d97da2016-07-04 08:08:31 +01002644
Chris Wilson91c8a322016-07-05 10:40:23 +01002645 lockdep_assert_held(&dev_priv->drm.struct_mutex);
Chris Wilson67d97da2016-07-04 08:08:31 +01002646
2647 if (dev_priv->gt.active_engines == 0)
2648 return;
2649
2650 GEM_BUG_ON(!dev_priv->gt.awake);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002651
Dave Gordonb4ac5af2016-03-24 11:20:38 +00002652 for_each_engine(engine, dev_priv) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002653 i915_gem_retire_requests_ring(engine);
Chris Wilson67d97da2016-07-04 08:08:31 +01002654 if (list_empty(&engine->request_list))
2655 dev_priv->gt.active_engines &= ~intel_engine_flag(engine);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002656 }
2657
Chris Wilson67d97da2016-07-04 08:08:31 +01002658 if (dev_priv->gt.active_engines == 0)
Chris Wilson1b51bce2016-07-04 08:08:32 +01002659 queue_delayed_work(dev_priv->wq,
2660 &dev_priv->gt.idle_work,
2661 msecs_to_jiffies(100));
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002662}
2663
Daniel Vetter75ef9da2010-08-21 00:25:16 +02002664static void
Eric Anholt673a3942008-07-30 12:06:12 -07002665i915_gem_retire_work_handler(struct work_struct *work)
2666{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002667 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01002668 container_of(work, typeof(*dev_priv), gt.retire_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01002669 struct drm_device *dev = &dev_priv->drm;
Eric Anholt673a3942008-07-30 12:06:12 -07002670
Chris Wilson891b48c2010-09-29 12:26:37 +01002671 /* Come back later if the device is busy... */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002672 if (mutex_trylock(&dev->struct_mutex)) {
Chris Wilson67d97da2016-07-04 08:08:31 +01002673 i915_gem_retire_requests(dev_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002674 mutex_unlock(&dev->struct_mutex);
2675 }
Chris Wilson67d97da2016-07-04 08:08:31 +01002676
2677 /* Keep the retire handler running until we are finally idle.
2678 * We do not need to do this test under locking as in the worst-case
2679 * we queue the retire worker once too often.
2680 */
Chris Wilsonc9615612016-07-09 10:12:06 +01002681 if (READ_ONCE(dev_priv->gt.awake)) {
2682 i915_queue_hangcheck(dev_priv);
Chris Wilson67d97da2016-07-04 08:08:31 +01002683 queue_delayed_work(dev_priv->wq,
2684 &dev_priv->gt.retire_work,
Chris Wilsonbcb45082012-10-05 17:02:57 +01002685 round_jiffies_up_relative(HZ));
Chris Wilsonc9615612016-07-09 10:12:06 +01002686 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002687}
Chris Wilson891b48c2010-09-29 12:26:37 +01002688
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002689static void
2690i915_gem_idle_work_handler(struct work_struct *work)
2691{
2692 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01002693 container_of(work, typeof(*dev_priv), gt.idle_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01002694 struct drm_device *dev = &dev_priv->drm;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00002695 struct intel_engine_cs *engine;
Chris Wilson67d97da2016-07-04 08:08:31 +01002696 unsigned int stuck_engines;
2697 bool rearm_hangcheck;
2698
2699 if (!READ_ONCE(dev_priv->gt.awake))
2700 return;
2701
2702 if (READ_ONCE(dev_priv->gt.active_engines))
2703 return;
2704
2705 rearm_hangcheck =
2706 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
2707
2708 if (!mutex_trylock(&dev->struct_mutex)) {
2709 /* Currently busy, come back later */
2710 mod_delayed_work(dev_priv->wq,
2711 &dev_priv->gt.idle_work,
2712 msecs_to_jiffies(50));
2713 goto out_rearm;
2714 }
2715
2716 if (dev_priv->gt.active_engines)
2717 goto out_unlock;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002718
Dave Gordonb4ac5af2016-03-24 11:20:38 +00002719 for_each_engine(engine, dev_priv)
Chris Wilson67d97da2016-07-04 08:08:31 +01002720 i915_gem_batch_pool_fini(&engine->batch_pool);
Zou Nan hai852835f2010-05-21 09:08:56 +08002721
Chris Wilson67d97da2016-07-04 08:08:31 +01002722 GEM_BUG_ON(!dev_priv->gt.awake);
2723 dev_priv->gt.awake = false;
2724 rearm_hangcheck = false;
Daniel Vetter30ecad72015-12-09 09:29:36 +01002725
Chris Wilson2529d572016-07-24 10:10:20 +01002726 /* As we have disabled hangcheck, we need to unstick any waiters still
2727 * hanging around. However, as we may be racing against the interrupt
2728 * handler or the waiters themselves, we skip enabling the fake-irq.
2729 */
Chris Wilson67d97da2016-07-04 08:08:31 +01002730 stuck_engines = intel_kick_waiters(dev_priv);
Chris Wilson2529d572016-07-24 10:10:20 +01002731 if (unlikely(stuck_engines))
2732 DRM_DEBUG_DRIVER("kicked stuck waiters (%x)...missed irq?\n",
2733 stuck_engines);
Chris Wilson35c94182015-04-07 16:20:37 +01002734
Chris Wilson67d97da2016-07-04 08:08:31 +01002735 if (INTEL_GEN(dev_priv) >= 6)
2736 gen6_rps_idle(dev_priv);
2737 intel_runtime_pm_put(dev_priv);
2738out_unlock:
2739 mutex_unlock(&dev->struct_mutex);
Chris Wilson35c94182015-04-07 16:20:37 +01002740
Chris Wilson67d97da2016-07-04 08:08:31 +01002741out_rearm:
2742 if (rearm_hangcheck) {
2743 GEM_BUG_ON(!dev_priv->gt.awake);
2744 i915_queue_hangcheck(dev_priv);
Chris Wilson35c94182015-04-07 16:20:37 +01002745 }
Eric Anholt673a3942008-07-30 12:06:12 -07002746}
2747
Ben Widawsky5816d642012-04-11 11:18:19 -07002748/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002749 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01002750 * @dev: drm device pointer
2751 * @data: ioctl data blob
2752 * @file: drm file pointer
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002753 *
2754 * Returns 0 if successful, else an error is returned with the remaining time in
2755 * the timeout parameter.
2756 * -ETIME: object is still busy after timeout
2757 * -ERESTARTSYS: signal interrupted the wait
2758 * -ENONENT: object doesn't exist
2759 * Also possible, but rare:
2760 * -EAGAIN: GPU wedged
2761 * -ENOMEM: damn
2762 * -ENODEV: Internal IRQ fail
2763 * -E?: The add request failed
2764 *
2765 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
2766 * non-zero timeout parameter the wait ioctl will wait for the given number of
2767 * nanoseconds on an object becoming unbusy. Since the wait itself does so
2768 * without holding struct_mutex the object may become re-busied before this
2769 * function completes. A similar but shorter * race condition exists in the busy
2770 * ioctl
2771 */
2772int
2773i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
2774{
2775 struct drm_i915_gem_wait *args = data;
2776 struct drm_i915_gem_object *obj;
Chris Wilson27c01aa2016-08-04 07:52:30 +01002777 struct drm_i915_gem_request *requests[I915_NUM_ENGINES];
Chris Wilsonb4716182015-04-27 13:41:17 +01002778 int i, n = 0;
2779 int ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002780
Daniel Vetter11b5d512014-09-29 15:31:26 +02002781 if (args->flags != 0)
2782 return -EINVAL;
2783
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002784 ret = i915_mutex_lock_interruptible(dev);
2785 if (ret)
2786 return ret;
2787
Chris Wilson03ac0642016-07-20 13:31:51 +01002788 obj = i915_gem_object_lookup(file, args->bo_handle);
2789 if (!obj) {
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002790 mutex_unlock(&dev->struct_mutex);
2791 return -ENOENT;
2792 }
2793
Chris Wilsonb4716182015-04-27 13:41:17 +01002794 if (!obj->active)
John Harrison97b2a6a2014-11-24 18:49:26 +00002795 goto out;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002796
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00002797 for (i = 0; i < I915_NUM_ENGINES; i++) {
Chris Wilson27c01aa2016-08-04 07:52:30 +01002798 struct drm_i915_gem_request *req;
Chris Wilsonb4716182015-04-27 13:41:17 +01002799
Chris Wilsond72d9082016-08-04 07:52:31 +01002800 req = i915_gem_active_get(&obj->last_read[i],
2801 &obj->base.dev->struct_mutex);
Chris Wilson27c01aa2016-08-04 07:52:30 +01002802 if (req)
2803 requests[n++] = req;
Chris Wilsonb4716182015-04-27 13:41:17 +01002804 }
2805
Chris Wilson21c310f2016-08-04 07:52:34 +01002806out:
2807 i915_gem_object_put(obj);
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002808 mutex_unlock(&dev->struct_mutex);
2809
Chris Wilsonb4716182015-04-27 13:41:17 +01002810 for (i = 0; i < n; i++) {
2811 if (ret == 0)
Chris Wilson27c01aa2016-08-04 07:52:30 +01002812 ret = __i915_wait_request(requests[i], true,
Chris Wilsonb4716182015-04-27 13:41:17 +01002813 args->timeout_ns > 0 ? &args->timeout_ns : NULL,
Chris Wilsonb6aa0872015-12-02 09:13:46 +00002814 to_rps_client(file));
Chris Wilson27c01aa2016-08-04 07:52:30 +01002815 i915_gem_request_put(requests[i]);
Chris Wilsonb4716182015-04-27 13:41:17 +01002816 }
John Harrisonff865882014-11-24 18:49:28 +00002817 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002818}
2819
Chris Wilsonb4716182015-04-27 13:41:17 +01002820static int
2821__i915_gem_object_sync(struct drm_i915_gem_object *obj,
Chris Wilson8e637172016-08-02 22:50:26 +01002822 struct drm_i915_gem_request *to,
2823 struct drm_i915_gem_request *from)
Chris Wilsonb4716182015-04-27 13:41:17 +01002824{
Chris Wilsonb4716182015-04-27 13:41:17 +01002825 int ret;
2826
Chris Wilson8e637172016-08-02 22:50:26 +01002827 if (to->engine == from->engine)
Chris Wilsonb4716182015-04-27 13:41:17 +01002828 return 0;
2829
Chris Wilson8e637172016-08-02 22:50:26 +01002830 if (i915_gem_request_completed(from))
Chris Wilsonb4716182015-04-27 13:41:17 +01002831 return 0;
2832
Chris Wilson39df9192016-07-20 13:31:57 +01002833 if (!i915.semaphores) {
Chris Wilson8e637172016-08-02 22:50:26 +01002834 ret = __i915_wait_request(from,
2835 from->i915->mm.interruptible,
Chris Wilsona6f766f2015-04-27 13:41:20 +01002836 NULL,
Chris Wilson197be2a2016-07-20 09:21:13 +01002837 NO_WAITBOOST);
Chris Wilsonb4716182015-04-27 13:41:17 +01002838 if (ret)
2839 return ret;
2840
Chris Wilson8e637172016-08-02 22:50:26 +01002841 i915_gem_object_retire_request(obj, from);
Chris Wilsonb4716182015-04-27 13:41:17 +01002842 } else {
Chris Wilson8e637172016-08-02 22:50:26 +01002843 int idx = intel_engine_sync_index(from->engine, to->engine);
Chris Wilsonddf07be2016-08-02 22:50:39 +01002844 if (from->fence.seqno <= from->engine->semaphore.sync_seqno[idx])
Chris Wilsonb4716182015-04-27 13:41:17 +01002845 return 0;
2846
Chris Wilson8e637172016-08-02 22:50:26 +01002847 trace_i915_gem_ring_sync_to(to, from);
Chris Wilsonddf07be2016-08-02 22:50:39 +01002848 ret = to->engine->semaphore.sync_to(to, from);
Chris Wilsonb4716182015-04-27 13:41:17 +01002849 if (ret)
2850 return ret;
2851
Chris Wilsonddf07be2016-08-02 22:50:39 +01002852 from->engine->semaphore.sync_seqno[idx] = from->fence.seqno;
Chris Wilsonb4716182015-04-27 13:41:17 +01002853 }
2854
2855 return 0;
2856}
2857
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002858/**
Ben Widawsky5816d642012-04-11 11:18:19 -07002859 * i915_gem_object_sync - sync an object to a ring.
2860 *
2861 * @obj: object which may be in use on another ring.
Chris Wilson8e637172016-08-02 22:50:26 +01002862 * @to: request we are wishing to use
Ben Widawsky5816d642012-04-11 11:18:19 -07002863 *
2864 * This code is meant to abstract object synchronization with the GPU.
Chris Wilson8e637172016-08-02 22:50:26 +01002865 * Conceptually we serialise writes between engines inside the GPU.
2866 * We only allow one engine to write into a buffer at any time, but
2867 * multiple readers. To ensure each has a coherent view of memory, we must:
Chris Wilsonb4716182015-04-27 13:41:17 +01002868 *
2869 * - If there is an outstanding write request to the object, the new
2870 * request must wait for it to complete (either CPU or in hw, requests
2871 * on the same ring will be naturally ordered).
2872 *
2873 * - If we are a write request (pending_write_domain is set), the new
2874 * request must wait for outstanding read requests to complete.
Ben Widawsky5816d642012-04-11 11:18:19 -07002875 *
2876 * Returns 0 if successful, else propagates up the lower layer error.
2877 */
Ben Widawsky2911a352012-04-05 14:47:36 -07002878int
2879i915_gem_object_sync(struct drm_i915_gem_object *obj,
Chris Wilson8e637172016-08-02 22:50:26 +01002880 struct drm_i915_gem_request *to)
Ben Widawsky2911a352012-04-05 14:47:36 -07002881{
Chris Wilson8cac6f62016-08-04 07:52:32 +01002882 struct i915_gem_active *active;
2883 unsigned long active_mask;
2884 int idx;
Ben Widawsky2911a352012-04-05 14:47:36 -07002885
Chris Wilson8cac6f62016-08-04 07:52:32 +01002886 lockdep_assert_held(&obj->base.dev->struct_mutex);
2887
2888 active_mask = obj->active;
2889 if (!active_mask)
Ben Widawsky2911a352012-04-05 14:47:36 -07002890 return 0;
2891
Chris Wilson8cac6f62016-08-04 07:52:32 +01002892 if (obj->base.pending_write_domain) {
2893 active = obj->last_read;
Chris Wilsonb4716182015-04-27 13:41:17 +01002894 } else {
Chris Wilson8cac6f62016-08-04 07:52:32 +01002895 active_mask = 1;
2896 active = &obj->last_write;
Chris Wilsonb4716182015-04-27 13:41:17 +01002897 }
Chris Wilson8cac6f62016-08-04 07:52:32 +01002898
2899 for_each_active(active_mask, idx) {
2900 struct drm_i915_gem_request *request;
2901 int ret;
2902
2903 request = i915_gem_active_peek(&active[idx],
2904 &obj->base.dev->struct_mutex);
2905 if (!request)
2906 continue;
2907
2908 ret = __i915_gem_object_sync(obj, to, request);
Chris Wilsonb4716182015-04-27 13:41:17 +01002909 if (ret)
2910 return ret;
2911 }
Ben Widawsky2911a352012-04-05 14:47:36 -07002912
Chris Wilsonb4716182015-04-27 13:41:17 +01002913 return 0;
Ben Widawsky2911a352012-04-05 14:47:36 -07002914}
2915
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002916static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
2917{
2918 u32 old_write_domain, old_read_domains;
2919
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002920 /* Force a pagefault for domain tracking on next user access */
2921 i915_gem_release_mmap(obj);
2922
Keith Packardb97c3d92011-06-24 21:02:59 -07002923 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
2924 return;
2925
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002926 old_read_domains = obj->base.read_domains;
2927 old_write_domain = obj->base.write_domain;
2928
2929 obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
2930 obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
2931
2932 trace_i915_gem_object_change_domain(obj,
2933 old_read_domains,
2934 old_write_domain);
2935}
2936
Chris Wilson8ef85612016-04-28 09:56:39 +01002937static void __i915_vma_iounmap(struct i915_vma *vma)
2938{
2939 GEM_BUG_ON(vma->pin_count);
2940
2941 if (vma->iomap == NULL)
2942 return;
2943
2944 io_mapping_unmap(vma->iomap);
2945 vma->iomap = NULL;
2946}
2947
Tvrtko Ursuline9f24d52015-10-05 13:26:36 +01002948static int __i915_vma_unbind(struct i915_vma *vma, bool wait)
Eric Anholt673a3942008-07-30 12:06:12 -07002949{
Ben Widawsky07fe0b12013-07-31 17:00:10 -07002950 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson43e28f02013-01-08 10:53:09 +00002951 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002952
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00002953 if (list_empty(&vma->obj_link))
Eric Anholt673a3942008-07-30 12:06:12 -07002954 return 0;
2955
Daniel Vetter0ff501c2013-08-29 19:50:31 +02002956 if (!drm_mm_node_allocated(&vma->node)) {
2957 i915_gem_vma_destroy(vma);
Daniel Vetter0ff501c2013-08-29 19:50:31 +02002958 return 0;
2959 }
Ben Widawsky433544b2013-08-13 18:09:06 -07002960
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08002961 if (vma->pin_count)
Chris Wilson31d8d652012-05-24 19:11:20 +01002962 return -EBUSY;
Eric Anholt673a3942008-07-30 12:06:12 -07002963
Chris Wilson15717de2016-08-04 07:52:26 +01002964 GEM_BUG_ON(obj->bind_count == 0);
2965 GEM_BUG_ON(!obj->pages);
Chris Wilsonc4670ad2012-08-20 10:23:27 +01002966
Tvrtko Ursuline9f24d52015-10-05 13:26:36 +01002967 if (wait) {
2968 ret = i915_gem_object_wait_rendering(obj, false);
2969 if (ret)
2970 return ret;
2971 }
Chris Wilsona8198ee2011-04-13 22:04:09 +01002972
Chris Wilson596c5922016-02-26 11:03:20 +00002973 if (vma->is_ggtt && vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL) {
Daniel Vetter8b1bc9b2014-02-14 14:06:07 +01002974 i915_gem_object_finish_gtt(obj);
Chris Wilsona8198ee2011-04-13 22:04:09 +01002975
Daniel Vetter8b1bc9b2014-02-14 14:06:07 +01002976 /* release the fence reg _after_ flushing */
2977 ret = i915_gem_object_put_fence(obj);
2978 if (ret)
2979 return ret;
Chris Wilson8ef85612016-04-28 09:56:39 +01002980
2981 __i915_vma_iounmap(vma);
Daniel Vetter8b1bc9b2014-02-14 14:06:07 +01002982 }
Daniel Vetter96b47b62009-12-15 17:50:00 +01002983
Ben Widawsky07fe0b12013-07-31 17:00:10 -07002984 trace_i915_vma_unbind(vma);
Chris Wilsondb53a302011-02-03 11:57:46 +00002985
Daniel Vetter777dc5b2015-04-14 17:35:12 +02002986 vma->vm->unbind_vma(vma);
Mika Kuoppala5e562f12015-04-30 11:02:31 +03002987 vma->bound = 0;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002988
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00002989 list_del_init(&vma->vm_link);
Chris Wilson596c5922016-02-26 11:03:20 +00002990 if (vma->is_ggtt) {
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002991 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL) {
2992 obj->map_and_fenceable = false;
2993 } else if (vma->ggtt_view.pages) {
2994 sg_free_table(vma->ggtt_view.pages);
2995 kfree(vma->ggtt_view.pages);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002996 }
Chris Wilson016a65a2015-06-11 08:06:08 +01002997 vma->ggtt_view.pages = NULL;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002998 }
Eric Anholt673a3942008-07-30 12:06:12 -07002999
Ben Widawsky2f633152013-07-17 12:19:03 -07003000 drm_mm_remove_node(&vma->node);
3001 i915_gem_vma_destroy(vma);
3002
3003 /* Since the unbound list is global, only move to that list if
Daniel Vetterb93dab62013-08-26 11:23:47 +02003004 * no more VMAs exist. */
Chris Wilson15717de2016-08-04 07:52:26 +01003005 if (--obj->bind_count == 0)
3006 list_move_tail(&obj->global_list,
3007 &to_i915(obj->base.dev)->mm.unbound_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003008
Chris Wilson70903c32013-12-04 09:59:09 +00003009 /* And finally now the object is completely decoupled from this vma,
3010 * we can drop its hold on the backing storage and allow it to be
3011 * reaped by the shrinker.
3012 */
3013 i915_gem_object_unpin_pages(obj);
3014
Chris Wilson88241782011-01-07 17:09:48 +00003015 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00003016}
3017
Tvrtko Ursuline9f24d52015-10-05 13:26:36 +01003018int i915_vma_unbind(struct i915_vma *vma)
3019{
3020 return __i915_vma_unbind(vma, true);
3021}
3022
3023int __i915_vma_unbind_no_wait(struct i915_vma *vma)
3024{
3025 return __i915_vma_unbind(vma, false);
3026}
3027
Chris Wilson6e5a5be2016-06-24 14:55:57 +01003028int i915_gem_wait_for_idle(struct drm_i915_private *dev_priv)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003029{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003030 struct intel_engine_cs *engine;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003031 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003032
Chris Wilson91c8a322016-07-05 10:40:23 +01003033 lockdep_assert_held(&dev_priv->drm.struct_mutex);
Chris Wilson6e5a5be2016-06-24 14:55:57 +01003034
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003035 for_each_engine(engine, dev_priv) {
Chris Wilson62e63002016-06-24 14:55:52 +01003036 if (engine->last_context == NULL)
3037 continue;
3038
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00003039 ret = intel_engine_idle(engine);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003040 if (ret)
3041 return ret;
3042 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003043
Chris Wilsonb4716182015-04-27 13:41:17 +01003044 WARN_ON(i915_verify_lists(dev));
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01003045 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003046}
3047
Chris Wilson4144f9b2014-09-11 08:43:48 +01003048static bool i915_gem_valid_gtt_space(struct i915_vma *vma,
Chris Wilson42d6ab42012-07-26 11:49:32 +01003049 unsigned long cache_level)
3050{
Chris Wilson4144f9b2014-09-11 08:43:48 +01003051 struct drm_mm_node *gtt_space = &vma->node;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003052 struct drm_mm_node *other;
3053
Chris Wilson4144f9b2014-09-11 08:43:48 +01003054 /*
3055 * On some machines we have to be careful when putting differing types
3056 * of snoopable memory together to avoid the prefetcher crossing memory
3057 * domains and dying. During vm initialisation, we decide whether or not
3058 * these constraints apply and set the drm_mm.color_adjust
3059 * appropriately.
Chris Wilson42d6ab42012-07-26 11:49:32 +01003060 */
Chris Wilson4144f9b2014-09-11 08:43:48 +01003061 if (vma->vm->mm.color_adjust == NULL)
Chris Wilson42d6ab42012-07-26 11:49:32 +01003062 return true;
3063
Ben Widawskyc6cfb322013-07-05 14:41:06 -07003064 if (!drm_mm_node_allocated(gtt_space))
Chris Wilson42d6ab42012-07-26 11:49:32 +01003065 return true;
3066
3067 if (list_empty(&gtt_space->node_list))
3068 return true;
3069
3070 other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
3071 if (other->allocated && !other->hole_follows && other->color != cache_level)
3072 return false;
3073
3074 other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
3075 if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
3076 return false;
3077
3078 return true;
3079}
3080
Jesse Barnesde151cf2008-11-12 10:03:55 -08003081/**
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003082 * Finds free space in the GTT aperture and binds the object or a view of it
3083 * there.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003084 * @obj: object to bind
3085 * @vm: address space to bind into
3086 * @ggtt_view: global gtt view if applicable
3087 * @alignment: requested alignment
3088 * @flags: mask of PIN_* flags to use
Eric Anholt673a3942008-07-30 12:06:12 -07003089 */
Daniel Vetter262de142014-02-14 14:01:20 +01003090static struct i915_vma *
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003091i915_gem_object_bind_to_vm(struct drm_i915_gem_object *obj,
3092 struct i915_address_space *vm,
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003093 const struct i915_ggtt_view *ggtt_view,
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003094 unsigned alignment,
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003095 uint64_t flags)
Eric Anholt673a3942008-07-30 12:06:12 -07003096{
Chris Wilson05394f32010-11-08 19:18:58 +00003097 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003098 struct drm_i915_private *dev_priv = to_i915(dev);
3099 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Michel Thierry65bd3422015-07-29 17:23:58 +01003100 u32 fence_alignment, unfenced_alignment;
Michel Thierry101b5062015-10-01 13:33:57 +01003101 u32 search_flag, alloc_flag;
3102 u64 start, end;
Michel Thierry65bd3422015-07-29 17:23:58 +01003103 u64 size, fence_size;
Ben Widawsky2f633152013-07-17 12:19:03 -07003104 struct i915_vma *vma;
Chris Wilson07f73f62009-09-14 16:50:30 +01003105 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003106
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003107 if (i915_is_ggtt(vm)) {
3108 u32 view_size;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003109
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003110 if (WARN_ON(!ggtt_view))
3111 return ERR_PTR(-EINVAL);
3112
3113 view_size = i915_ggtt_view_size(obj, ggtt_view);
3114
3115 fence_size = i915_gem_get_gtt_size(dev,
3116 view_size,
3117 obj->tiling_mode);
3118 fence_alignment = i915_gem_get_gtt_alignment(dev,
3119 view_size,
3120 obj->tiling_mode,
3121 true);
3122 unfenced_alignment = i915_gem_get_gtt_alignment(dev,
3123 view_size,
3124 obj->tiling_mode,
3125 false);
3126 size = flags & PIN_MAPPABLE ? fence_size : view_size;
3127 } else {
3128 fence_size = i915_gem_get_gtt_size(dev,
3129 obj->base.size,
3130 obj->tiling_mode);
3131 fence_alignment = i915_gem_get_gtt_alignment(dev,
3132 obj->base.size,
3133 obj->tiling_mode,
3134 true);
3135 unfenced_alignment =
3136 i915_gem_get_gtt_alignment(dev,
3137 obj->base.size,
3138 obj->tiling_mode,
3139 false);
3140 size = flags & PIN_MAPPABLE ? fence_size : obj->base.size;
3141 }
Chris Wilsona00b10c2010-09-24 21:15:47 +01003142
Michel Thierry101b5062015-10-01 13:33:57 +01003143 start = flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
3144 end = vm->total;
3145 if (flags & PIN_MAPPABLE)
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003146 end = min_t(u64, end, ggtt->mappable_end);
Michel Thierry101b5062015-10-01 13:33:57 +01003147 if (flags & PIN_ZONE_4G)
Michel Thierry48ea1e32016-01-11 11:39:27 +00003148 end = min_t(u64, end, (1ULL << 32) - PAGE_SIZE);
Michel Thierry101b5062015-10-01 13:33:57 +01003149
Eric Anholt673a3942008-07-30 12:06:12 -07003150 if (alignment == 0)
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003151 alignment = flags & PIN_MAPPABLE ? fence_alignment :
Daniel Vetter5e783302010-11-14 22:32:36 +01003152 unfenced_alignment;
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003153 if (flags & PIN_MAPPABLE && alignment & (fence_alignment - 1)) {
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003154 DRM_DEBUG("Invalid object (view type=%u) alignment requested %u\n",
3155 ggtt_view ? ggtt_view->type : 0,
3156 alignment);
Daniel Vetter262de142014-02-14 14:01:20 +01003157 return ERR_PTR(-EINVAL);
Eric Anholt673a3942008-07-30 12:06:12 -07003158 }
3159
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003160 /* If binding the object/GGTT view requires more space than the entire
3161 * aperture has, reject it early before evicting everything in a vain
3162 * attempt to find space.
Chris Wilson654fc602010-05-27 13:18:21 +01003163 */
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003164 if (size > end) {
Michel Thierry65bd3422015-07-29 17:23:58 +01003165 DRM_DEBUG("Attempting to bind an object (view type=%u) larger than the aperture: size=%llu > %s aperture=%llu\n",
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003166 ggtt_view ? ggtt_view->type : 0,
3167 size,
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003168 flags & PIN_MAPPABLE ? "mappable" : "total",
Chris Wilsond23db882014-05-23 08:48:08 +02003169 end);
Daniel Vetter262de142014-02-14 14:01:20 +01003170 return ERR_PTR(-E2BIG);
Chris Wilson654fc602010-05-27 13:18:21 +01003171 }
3172
Chris Wilson37e680a2012-06-07 15:38:42 +01003173 ret = i915_gem_object_get_pages(obj);
Chris Wilson6c085a72012-08-20 11:40:46 +02003174 if (ret)
Daniel Vetter262de142014-02-14 14:01:20 +01003175 return ERR_PTR(ret);
Chris Wilson6c085a72012-08-20 11:40:46 +02003176
Chris Wilsonfbdda6f2012-11-20 10:45:16 +00003177 i915_gem_object_pin_pages(obj);
3178
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003179 vma = ggtt_view ? i915_gem_obj_lookup_or_create_ggtt_vma(obj, ggtt_view) :
3180 i915_gem_obj_lookup_or_create_vma(obj, vm);
3181
Daniel Vetter262de142014-02-14 14:01:20 +01003182 if (IS_ERR(vma))
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003183 goto err_unpin;
Ben Widawsky2f633152013-07-17 12:19:03 -07003184
Chris Wilson506a8e82015-12-08 11:55:07 +00003185 if (flags & PIN_OFFSET_FIXED) {
3186 uint64_t offset = flags & PIN_OFFSET_MASK;
3187
3188 if (offset & (alignment - 1) || offset + size > end) {
3189 ret = -EINVAL;
3190 goto err_free_vma;
3191 }
3192 vma->node.start = offset;
3193 vma->node.size = size;
3194 vma->node.color = obj->cache_level;
3195 ret = drm_mm_reserve_node(&vm->mm, &vma->node);
3196 if (ret) {
3197 ret = i915_gem_evict_for_vma(vma);
3198 if (ret == 0)
3199 ret = drm_mm_reserve_node(&vm->mm, &vma->node);
3200 }
3201 if (ret)
3202 goto err_free_vma;
Michel Thierry101b5062015-10-01 13:33:57 +01003203 } else {
Chris Wilson506a8e82015-12-08 11:55:07 +00003204 if (flags & PIN_HIGH) {
3205 search_flag = DRM_MM_SEARCH_BELOW;
3206 alloc_flag = DRM_MM_CREATE_TOP;
3207 } else {
3208 search_flag = DRM_MM_SEARCH_DEFAULT;
3209 alloc_flag = DRM_MM_CREATE_DEFAULT;
3210 }
Michel Thierry101b5062015-10-01 13:33:57 +01003211
Ben Widawsky0a9ae0d2013-05-25 12:26:35 -07003212search_free:
Chris Wilson506a8e82015-12-08 11:55:07 +00003213 ret = drm_mm_insert_node_in_range_generic(&vm->mm, &vma->node,
3214 size, alignment,
3215 obj->cache_level,
3216 start, end,
3217 search_flag,
3218 alloc_flag);
3219 if (ret) {
3220 ret = i915_gem_evict_something(dev, vm, size, alignment,
3221 obj->cache_level,
3222 start, end,
3223 flags);
3224 if (ret == 0)
3225 goto search_free;
Chris Wilson97311292009-09-21 00:22:34 +01003226
Chris Wilson506a8e82015-12-08 11:55:07 +00003227 goto err_free_vma;
3228 }
Chris Wilsondc9dd7a2012-12-07 20:37:07 +00003229 }
Chris Wilson4144f9b2014-09-11 08:43:48 +01003230 if (WARN_ON(!i915_gem_valid_gtt_space(vma, obj->cache_level))) {
Ben Widawsky2f633152013-07-17 12:19:03 -07003231 ret = -EINVAL;
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003232 goto err_remove_node;
Eric Anholt673a3942008-07-30 12:06:12 -07003233 }
3234
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003235 trace_i915_vma_bind(vma, flags);
Daniel Vetter08755462015-04-20 09:04:05 -07003236 ret = i915_vma_bind(vma, obj->cache_level, flags);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003237 if (ret)
Imre Deake2273302015-07-09 12:59:05 +03003238 goto err_remove_node;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003239
Ben Widawsky35c20a62013-05-31 11:28:48 -07003240 list_move_tail(&obj->global_list, &dev_priv->mm.bound_list);
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003241 list_add_tail(&vma->vm_link, &vm->inactive_list);
Chris Wilson15717de2016-08-04 07:52:26 +01003242 obj->bind_count++;
Chris Wilsonbf1a1092010-08-07 11:01:20 +01003243
Daniel Vetter262de142014-02-14 14:01:20 +01003244 return vma;
Ben Widawsky2f633152013-07-17 12:19:03 -07003245
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003246err_remove_node:
Dan Carpenter6286ef92013-07-19 08:46:27 +03003247 drm_mm_remove_node(&vma->node);
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003248err_free_vma:
Ben Widawsky2f633152013-07-17 12:19:03 -07003249 i915_gem_vma_destroy(vma);
Daniel Vetter262de142014-02-14 14:01:20 +01003250 vma = ERR_PTR(ret);
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003251err_unpin:
Ben Widawsky2f633152013-07-17 12:19:03 -07003252 i915_gem_object_unpin_pages(obj);
Daniel Vetter262de142014-02-14 14:01:20 +01003253 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07003254}
3255
Chris Wilson000433b2013-08-08 14:41:09 +01003256bool
Chris Wilson2c225692013-08-09 12:26:45 +01003257i915_gem_clflush_object(struct drm_i915_gem_object *obj,
3258 bool force)
Eric Anholt673a3942008-07-30 12:06:12 -07003259{
Eric Anholt673a3942008-07-30 12:06:12 -07003260 /* If we don't have a page list set up, then we're not pinned
3261 * to GPU, and we can ignore the cache flush because it'll happen
3262 * again at bind time.
3263 */
Chris Wilson05394f32010-11-08 19:18:58 +00003264 if (obj->pages == NULL)
Chris Wilson000433b2013-08-08 14:41:09 +01003265 return false;
Eric Anholt673a3942008-07-30 12:06:12 -07003266
Imre Deak769ce462013-02-13 21:56:05 +02003267 /*
3268 * Stolen memory is always coherent with the GPU as it is explicitly
3269 * marked as wc by the system, or the system is cache-coherent.
3270 */
Chris Wilson6a2c4232014-11-04 04:51:40 -08003271 if (obj->stolen || obj->phys_handle)
Chris Wilson000433b2013-08-08 14:41:09 +01003272 return false;
Imre Deak769ce462013-02-13 21:56:05 +02003273
Chris Wilson9c23f7f2011-03-29 16:59:52 -07003274 /* If the GPU is snooping the contents of the CPU cache,
3275 * we do not need to manually clear the CPU cache lines. However,
3276 * the caches are only snooped when the render cache is
3277 * flushed/invalidated. As we always have to emit invalidations
3278 * and flushes when moving into and out of the RENDER domain, correct
3279 * snooping behaviour occurs naturally as the result of our domain
3280 * tracking.
3281 */
Chris Wilson0f719792015-01-13 13:32:52 +00003282 if (!force && cpu_cache_is_coherent(obj->base.dev, obj->cache_level)) {
3283 obj->cache_dirty = true;
Chris Wilson000433b2013-08-08 14:41:09 +01003284 return false;
Chris Wilson0f719792015-01-13 13:32:52 +00003285 }
Chris Wilson9c23f7f2011-03-29 16:59:52 -07003286
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003287 trace_i915_gem_object_clflush(obj);
Chris Wilson9da3da62012-06-01 15:20:22 +01003288 drm_clflush_sg(obj->pages);
Chris Wilson0f719792015-01-13 13:32:52 +00003289 obj->cache_dirty = false;
Chris Wilson000433b2013-08-08 14:41:09 +01003290
3291 return true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003292}
3293
3294/** Flushes the GTT write domain for the object if it's dirty. */
3295static void
Chris Wilson05394f32010-11-08 19:18:58 +00003296i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003297{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003298 uint32_t old_write_domain;
3299
Chris Wilson05394f32010-11-08 19:18:58 +00003300 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
Eric Anholte47c68e2008-11-14 13:35:19 -08003301 return;
3302
Chris Wilson63256ec2011-01-04 18:42:07 +00003303 /* No actual flushing is required for the GTT write domain. Writes
Eric Anholte47c68e2008-11-14 13:35:19 -08003304 * to it immediately go to main memory as far as we know, so there's
3305 * no chipset flush. It also doesn't land in render cache.
Chris Wilson63256ec2011-01-04 18:42:07 +00003306 *
3307 * However, we do have to enforce the order so that all writes through
3308 * the GTT land before any writes to the device, such as updates to
3309 * the GATT itself.
Eric Anholte47c68e2008-11-14 13:35:19 -08003310 */
Chris Wilson63256ec2011-01-04 18:42:07 +00003311 wmb();
3312
Chris Wilson05394f32010-11-08 19:18:58 +00003313 old_write_domain = obj->base.write_domain;
3314 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003315
Rodrigo Vivide152b62015-07-07 16:28:51 -07003316 intel_fb_obj_flush(obj, false, ORIGIN_GTT);
Daniel Vetterf99d7062014-06-19 16:01:59 +02003317
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003318 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00003319 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003320 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08003321}
3322
3323/** Flushes the CPU write domain for the object if it's dirty. */
3324static void
Daniel Vettere62b59e2015-01-21 14:53:48 +01003325i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003326{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003327 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08003328
Chris Wilson05394f32010-11-08 19:18:58 +00003329 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
Eric Anholte47c68e2008-11-14 13:35:19 -08003330 return;
3331
Daniel Vettere62b59e2015-01-21 14:53:48 +01003332 if (i915_gem_clflush_object(obj, obj->pin_display))
Chris Wilsonc0336662016-05-06 15:40:21 +01003333 i915_gem_chipset_flush(to_i915(obj->base.dev));
Chris Wilson000433b2013-08-08 14:41:09 +01003334
Chris Wilson05394f32010-11-08 19:18:58 +00003335 old_write_domain = obj->base.write_domain;
3336 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003337
Rodrigo Vivide152b62015-07-07 16:28:51 -07003338 intel_fb_obj_flush(obj, false, ORIGIN_CPU);
Daniel Vetterf99d7062014-06-19 16:01:59 +02003339
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003340 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00003341 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003342 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08003343}
3344
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003345/**
3346 * Moves a single object to the GTT read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003347 * @obj: object to act on
3348 * @write: ask for write access or read only
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003349 *
3350 * This function returns when the move is complete, including waiting on
3351 * flushes to occur.
3352 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003353int
Chris Wilson20217462010-11-23 15:26:33 +00003354i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003355{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003356 struct drm_device *dev = obj->base.dev;
3357 struct drm_i915_private *dev_priv = to_i915(dev);
3358 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003359 uint32_t old_write_domain, old_read_domains;
Chris Wilson43566de2015-01-02 16:29:29 +05303360 struct i915_vma *vma;
Eric Anholte47c68e2008-11-14 13:35:19 -08003361 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003362
Chris Wilson0201f1e2012-07-20 12:41:01 +01003363 ret = i915_gem_object_wait_rendering(obj, !write);
Chris Wilson88241782011-01-07 17:09:48 +00003364 if (ret)
3365 return ret;
3366
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003367 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
3368 return 0;
3369
Chris Wilson43566de2015-01-02 16:29:29 +05303370 /* Flush and acquire obj->pages so that we are coherent through
3371 * direct access in memory with previous cached writes through
3372 * shmemfs and that our cache domain tracking remains valid.
3373 * For example, if the obj->filp was moved to swap without us
3374 * being notified and releasing the pages, we would mistakenly
3375 * continue to assume that the obj remained out of the CPU cached
3376 * domain.
3377 */
3378 ret = i915_gem_object_get_pages(obj);
3379 if (ret)
3380 return ret;
3381
Daniel Vettere62b59e2015-01-21 14:53:48 +01003382 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003383
Chris Wilsond0a57782012-10-09 19:24:37 +01003384 /* Serialise direct access to this object with the barriers for
3385 * coherent writes from the GPU, by effectively invalidating the
3386 * GTT domain upon first access.
3387 */
3388 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
3389 mb();
3390
Chris Wilson05394f32010-11-08 19:18:58 +00003391 old_write_domain = obj->base.write_domain;
3392 old_read_domains = obj->base.read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003393
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003394 /* It should now be out of any other write domains, and we can update
3395 * the domain values for our changes.
3396 */
Chris Wilson05394f32010-11-08 19:18:58 +00003397 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3398 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003399 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003400 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
3401 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
3402 obj->dirty = 1;
Eric Anholte47c68e2008-11-14 13:35:19 -08003403 }
3404
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003405 trace_i915_gem_object_change_domain(obj,
3406 old_read_domains,
3407 old_write_domain);
3408
Chris Wilson8325a092012-04-24 15:52:35 +01003409 /* And bump the LRU for this access */
Chris Wilson43566de2015-01-02 16:29:29 +05303410 vma = i915_gem_obj_to_ggtt(obj);
3411 if (vma && drm_mm_node_allocated(&vma->node) && !obj->active)
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003412 list_move_tail(&vma->vm_link,
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003413 &ggtt->base.inactive_list);
Chris Wilson8325a092012-04-24 15:52:35 +01003414
Eric Anholte47c68e2008-11-14 13:35:19 -08003415 return 0;
3416}
3417
Chris Wilsonef55f922015-10-09 14:11:27 +01003418/**
3419 * Changes the cache-level of an object across all VMA.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003420 * @obj: object to act on
3421 * @cache_level: new cache level to set for the object
Chris Wilsonef55f922015-10-09 14:11:27 +01003422 *
3423 * After this function returns, the object will be in the new cache-level
3424 * across all GTT and the contents of the backing storage will be coherent,
3425 * with respect to the new cache-level. In order to keep the backing storage
3426 * coherent for all users, we only allow a single cache level to be set
3427 * globally on the object and prevent it from being changed whilst the
3428 * hardware is reading from the object. That is if the object is currently
3429 * on the scanout it will be set to uncached (or equivalent display
3430 * cache coherency) and all non-MOCS GPU access will also be uncached so
3431 * that all direct access to the scanout remains coherent.
3432 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003433int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3434 enum i915_cache_level cache_level)
3435{
Chris Wilsonaa653a62016-08-04 07:52:27 +01003436 struct i915_vma *vma;
Ville Syrjäläed75a552015-08-11 19:47:10 +03003437 int ret = 0;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003438
3439 if (obj->cache_level == cache_level)
Ville Syrjäläed75a552015-08-11 19:47:10 +03003440 goto out;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003441
Chris Wilsonef55f922015-10-09 14:11:27 +01003442 /* Inspect the list of currently bound VMA and unbind any that would
3443 * be invalid given the new cache-level. This is principally to
3444 * catch the issue of the CS prefetch crossing page boundaries and
3445 * reading an invalid PTE on older architectures.
3446 */
Chris Wilsonaa653a62016-08-04 07:52:27 +01003447restart:
3448 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003449 if (!drm_mm_node_allocated(&vma->node))
3450 continue;
3451
3452 if (vma->pin_count) {
3453 DRM_DEBUG("can not change the cache level of pinned objects\n");
3454 return -EBUSY;
3455 }
3456
Chris Wilsonaa653a62016-08-04 07:52:27 +01003457 if (i915_gem_valid_gtt_space(vma, cache_level))
3458 continue;
3459
3460 ret = i915_vma_unbind(vma);
3461 if (ret)
3462 return ret;
3463
3464 /* As unbinding may affect other elements in the
3465 * obj->vma_list (due to side-effects from retiring
3466 * an active vma), play safe and restart the iterator.
3467 */
3468 goto restart;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003469 }
3470
Chris Wilsonef55f922015-10-09 14:11:27 +01003471 /* We can reuse the existing drm_mm nodes but need to change the
3472 * cache-level on the PTE. We could simply unbind them all and
3473 * rebind with the correct cache-level on next use. However since
3474 * we already have a valid slot, dma mapping, pages etc, we may as
3475 * rewrite the PTE in the belief that doing so tramples upon less
3476 * state and so involves less work.
3477 */
Chris Wilson15717de2016-08-04 07:52:26 +01003478 if (obj->bind_count) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003479 /* Before we change the PTE, the GPU must not be accessing it.
3480 * If we wait upon the object, we know that all the bound
3481 * VMA are no longer active.
3482 */
Chris Wilson2e2f3512015-04-27 13:41:14 +01003483 ret = i915_gem_object_wait_rendering(obj, false);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003484 if (ret)
3485 return ret;
3486
Chris Wilsonaa653a62016-08-04 07:52:27 +01003487 if (!HAS_LLC(obj->base.dev) && cache_level != I915_CACHE_NONE) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003488 /* Access to snoopable pages through the GTT is
3489 * incoherent and on some machines causes a hard
3490 * lockup. Relinquish the CPU mmaping to force
3491 * userspace to refault in the pages and we can
3492 * then double check if the GTT mapping is still
3493 * valid for that pointer access.
3494 */
3495 i915_gem_release_mmap(obj);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003496
Chris Wilsonef55f922015-10-09 14:11:27 +01003497 /* As we no longer need a fence for GTT access,
3498 * we can relinquish it now (and so prevent having
3499 * to steal a fence from someone else on the next
3500 * fence request). Note GPU activity would have
3501 * dropped the fence as all snoopable access is
3502 * supposed to be linear.
3503 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003504 ret = i915_gem_object_put_fence(obj);
3505 if (ret)
3506 return ret;
Chris Wilsonef55f922015-10-09 14:11:27 +01003507 } else {
3508 /* We either have incoherent backing store and
3509 * so no GTT access or the architecture is fully
3510 * coherent. In such cases, existing GTT mmaps
3511 * ignore the cache bit in the PTE and we can
3512 * rewrite it without confusing the GPU or having
3513 * to force userspace to fault back in its mmaps.
3514 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003515 }
3516
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003517 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003518 if (!drm_mm_node_allocated(&vma->node))
3519 continue;
3520
3521 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
3522 if (ret)
3523 return ret;
3524 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01003525 }
3526
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003527 list_for_each_entry(vma, &obj->vma_list, obj_link)
Chris Wilson2c225692013-08-09 12:26:45 +01003528 vma->node.color = cache_level;
3529 obj->cache_level = cache_level;
3530
Ville Syrjäläed75a552015-08-11 19:47:10 +03003531out:
Chris Wilsonef55f922015-10-09 14:11:27 +01003532 /* Flush the dirty CPU caches to the backing storage so that the
3533 * object is now coherent at its new cache level (with respect
3534 * to the access domain).
3535 */
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05303536 if (obj->cache_dirty && cpu_write_needs_clflush(obj)) {
Chris Wilson0f719792015-01-13 13:32:52 +00003537 if (i915_gem_clflush_object(obj, true))
Chris Wilsonc0336662016-05-06 15:40:21 +01003538 i915_gem_chipset_flush(to_i915(obj->base.dev));
Chris Wilsone4ffd172011-04-04 09:44:39 +01003539 }
3540
Chris Wilsone4ffd172011-04-04 09:44:39 +01003541 return 0;
3542}
3543
Ben Widawsky199adf42012-09-21 17:01:20 -07003544int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
3545 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003546{
Ben Widawsky199adf42012-09-21 17:01:20 -07003547 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003548 struct drm_i915_gem_object *obj;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003549
Chris Wilson03ac0642016-07-20 13:31:51 +01003550 obj = i915_gem_object_lookup(file, args->handle);
3551 if (!obj)
Chris Wilson432be692015-05-07 12:14:55 +01003552 return -ENOENT;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003553
Chris Wilson651d7942013-08-08 14:41:10 +01003554 switch (obj->cache_level) {
3555 case I915_CACHE_LLC:
3556 case I915_CACHE_L3_LLC:
3557 args->caching = I915_CACHING_CACHED;
3558 break;
3559
Chris Wilson4257d3b2013-08-08 14:41:11 +01003560 case I915_CACHE_WT:
3561 args->caching = I915_CACHING_DISPLAY;
3562 break;
3563
Chris Wilson651d7942013-08-08 14:41:10 +01003564 default:
3565 args->caching = I915_CACHING_NONE;
3566 break;
3567 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01003568
Chris Wilson34911fd2016-07-20 13:31:54 +01003569 i915_gem_object_put_unlocked(obj);
Chris Wilson432be692015-05-07 12:14:55 +01003570 return 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003571}
3572
Ben Widawsky199adf42012-09-21 17:01:20 -07003573int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
3574 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003575{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003576 struct drm_i915_private *dev_priv = to_i915(dev);
Ben Widawsky199adf42012-09-21 17:01:20 -07003577 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003578 struct drm_i915_gem_object *obj;
3579 enum i915_cache_level level;
3580 int ret;
3581
Ben Widawsky199adf42012-09-21 17:01:20 -07003582 switch (args->caching) {
3583 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01003584 level = I915_CACHE_NONE;
3585 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07003586 case I915_CACHING_CACHED:
Imre Deake5756c12015-08-14 18:43:30 +03003587 /*
3588 * Due to a HW issue on BXT A stepping, GPU stores via a
3589 * snooped mapping may leave stale data in a corresponding CPU
3590 * cacheline, whereas normally such cachelines would get
3591 * invalidated.
3592 */
Tvrtko Ursulinca377802016-03-02 12:10:31 +00003593 if (!HAS_LLC(dev) && !HAS_SNOOP(dev))
Imre Deake5756c12015-08-14 18:43:30 +03003594 return -ENODEV;
3595
Chris Wilsone6994ae2012-07-10 10:27:08 +01003596 level = I915_CACHE_LLC;
3597 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003598 case I915_CACHING_DISPLAY:
3599 level = HAS_WT(dev) ? I915_CACHE_WT : I915_CACHE_NONE;
3600 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003601 default:
3602 return -EINVAL;
3603 }
3604
Imre Deakfd0fe6a2015-11-04 21:25:32 +02003605 intel_runtime_pm_get(dev_priv);
3606
Ben Widawsky3bc29132012-09-26 16:15:20 -07003607 ret = i915_mutex_lock_interruptible(dev);
3608 if (ret)
Imre Deakfd0fe6a2015-11-04 21:25:32 +02003609 goto rpm_put;
Ben Widawsky3bc29132012-09-26 16:15:20 -07003610
Chris Wilson03ac0642016-07-20 13:31:51 +01003611 obj = i915_gem_object_lookup(file, args->handle);
3612 if (!obj) {
Chris Wilsone6994ae2012-07-10 10:27:08 +01003613 ret = -ENOENT;
3614 goto unlock;
3615 }
3616
3617 ret = i915_gem_object_set_cache_level(obj, level);
3618
Chris Wilsonf8c417c2016-07-20 13:31:53 +01003619 i915_gem_object_put(obj);
Chris Wilsone6994ae2012-07-10 10:27:08 +01003620unlock:
3621 mutex_unlock(&dev->struct_mutex);
Imre Deakfd0fe6a2015-11-04 21:25:32 +02003622rpm_put:
3623 intel_runtime_pm_put(dev_priv);
3624
Chris Wilsone6994ae2012-07-10 10:27:08 +01003625 return ret;
3626}
3627
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003628/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003629 * Prepare buffer for display plane (scanout, cursors, etc).
3630 * Can be called from an uninterruptible phase (modesetting) and allows
3631 * any flushes to be pipelined (for pageflips).
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003632 */
3633int
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003634i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3635 u32 alignment,
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003636 const struct i915_ggtt_view *view)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003637{
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003638 u32 old_read_domains, old_write_domain;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003639 int ret;
3640
Chris Wilsoncc98b412013-08-09 12:25:09 +01003641 /* Mark the pin_display early so that we account for the
3642 * display coherency whilst setting up the cache domains.
3643 */
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003644 obj->pin_display++;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003645
Eric Anholta7ef0642011-03-29 16:59:54 -07003646 /* The display engine is not coherent with the LLC cache on gen6. As
3647 * a result, we make sure that the pinning that is about to occur is
3648 * done with uncached PTEs. This is lowest common denominator for all
3649 * chipsets.
3650 *
3651 * However for gen6+, we could do better by using the GFDT bit instead
3652 * of uncaching, which would allow us to flush all the LLC-cached data
3653 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3654 */
Chris Wilson651d7942013-08-08 14:41:10 +01003655 ret = i915_gem_object_set_cache_level(obj,
3656 HAS_WT(obj->base.dev) ? I915_CACHE_WT : I915_CACHE_NONE);
Eric Anholta7ef0642011-03-29 16:59:54 -07003657 if (ret)
Chris Wilsoncc98b412013-08-09 12:25:09 +01003658 goto err_unpin_display;
Eric Anholta7ef0642011-03-29 16:59:54 -07003659
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003660 /* As the user may map the buffer once pinned in the display plane
3661 * (e.g. libkms for the bootup splash), we have to ensure that we
3662 * always use map_and_fenceable for all scanout buffers.
3663 */
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003664 ret = i915_gem_object_ggtt_pin(obj, view, alignment,
3665 view->type == I915_GGTT_VIEW_NORMAL ?
3666 PIN_MAPPABLE : 0);
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003667 if (ret)
Chris Wilsoncc98b412013-08-09 12:25:09 +01003668 goto err_unpin_display;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003669
Daniel Vettere62b59e2015-01-21 14:53:48 +01003670 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01003671
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003672 old_write_domain = obj->base.write_domain;
Chris Wilson05394f32010-11-08 19:18:58 +00003673 old_read_domains = obj->base.read_domains;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003674
3675 /* It should now be out of any other write domains, and we can update
3676 * the domain values for our changes.
3677 */
Chris Wilsone5f1d962012-07-20 12:41:00 +01003678 obj->base.write_domain = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00003679 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003680
3681 trace_i915_gem_object_change_domain(obj,
3682 old_read_domains,
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003683 old_write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003684
3685 return 0;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003686
3687err_unpin_display:
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003688 obj->pin_display--;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003689 return ret;
3690}
3691
3692void
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003693i915_gem_object_unpin_from_display_plane(struct drm_i915_gem_object *obj,
3694 const struct i915_ggtt_view *view)
Chris Wilsoncc98b412013-08-09 12:25:09 +01003695{
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003696 if (WARN_ON(obj->pin_display == 0))
3697 return;
3698
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003699 i915_gem_object_ggtt_unpin_view(obj, view);
3700
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003701 obj->pin_display--;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003702}
3703
Eric Anholte47c68e2008-11-14 13:35:19 -08003704/**
3705 * Moves a single object to the CPU read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003706 * @obj: object to act on
3707 * @write: requesting write or read-only access
Eric Anholte47c68e2008-11-14 13:35:19 -08003708 *
3709 * This function returns when the move is complete, including waiting on
3710 * flushes to occur.
3711 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02003712int
Chris Wilson919926a2010-11-12 13:42:53 +00003713i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08003714{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003715 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003716 int ret;
3717
Chris Wilson0201f1e2012-07-20 12:41:01 +01003718 ret = i915_gem_object_wait_rendering(obj, !write);
Chris Wilson88241782011-01-07 17:09:48 +00003719 if (ret)
3720 return ret;
3721
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003722 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
3723 return 0;
3724
Eric Anholte47c68e2008-11-14 13:35:19 -08003725 i915_gem_object_flush_gtt_write_domain(obj);
3726
Chris Wilson05394f32010-11-08 19:18:58 +00003727 old_write_domain = obj->base.write_domain;
3728 old_read_domains = obj->base.read_domains;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003729
Eric Anholte47c68e2008-11-14 13:35:19 -08003730 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00003731 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson2c225692013-08-09 12:26:45 +01003732 i915_gem_clflush_object(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003733
Chris Wilson05394f32010-11-08 19:18:58 +00003734 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003735 }
3736
3737 /* It should now be out of any other write domains, and we can update
3738 * the domain values for our changes.
3739 */
Chris Wilson05394f32010-11-08 19:18:58 +00003740 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003741
3742 /* If we're writing through the CPU, then the GPU read domains will
3743 * need to be invalidated at next use.
3744 */
3745 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003746 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3747 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003748 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003749
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003750 trace_i915_gem_object_change_domain(obj,
3751 old_read_domains,
3752 old_write_domain);
3753
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003754 return 0;
3755}
3756
Eric Anholt673a3942008-07-30 12:06:12 -07003757/* Throttle our rendering by waiting until the ring has completed our requests
3758 * emitted over 20 msec ago.
3759 *
Eric Anholtb9624422009-06-03 07:27:35 +00003760 * Note that if we were to use the current jiffies each time around the loop,
3761 * we wouldn't escape the function with any frames outstanding if the time to
3762 * render a frame was over 20ms.
3763 *
Eric Anholt673a3942008-07-30 12:06:12 -07003764 * This should get us reasonable parallelism between CPU and GPU but also
3765 * relatively low latency when blocking on a particular request to finish.
3766 */
3767static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003768i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003769{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003770 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003771 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsond0bc54f2015-05-21 21:01:48 +01003772 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
John Harrison54fb2412014-11-24 18:49:27 +00003773 struct drm_i915_gem_request *request, *target = NULL;
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003774 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003775
Daniel Vetter308887a2012-11-14 17:14:06 +01003776 ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
3777 if (ret)
3778 return ret;
3779
Chris Wilsonf4457ae2016-04-13 17:35:08 +01003780 /* ABI: return -EIO if already wedged */
3781 if (i915_terminally_wedged(&dev_priv->gpu_error))
3782 return -EIO;
Chris Wilsone110e8d2011-01-26 15:39:14 +00003783
Chris Wilson1c255952010-09-26 11:03:27 +01003784 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003785 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003786 if (time_after_eq(request->emitted_jiffies, recent_enough))
3787 break;
3788
John Harrisonfcfa423c2015-05-29 17:44:12 +01003789 /*
3790 * Note that the request might not have been submitted yet.
3791 * In which case emitted_jiffies will be zero.
3792 */
3793 if (!request->emitted_jiffies)
3794 continue;
3795
John Harrison54fb2412014-11-24 18:49:27 +00003796 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00003797 }
John Harrisonff865882014-11-24 18:49:28 +00003798 if (target)
Chris Wilsone8a261e2016-07-20 13:31:49 +01003799 i915_gem_request_get(target);
Chris Wilson1c255952010-09-26 11:03:27 +01003800 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003801
John Harrison54fb2412014-11-24 18:49:27 +00003802 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003803 return 0;
3804
Chris Wilson299259a2016-04-13 17:35:06 +01003805 ret = __i915_wait_request(target, true, NULL, NULL);
Chris Wilsone8a261e2016-07-20 13:31:49 +01003806 i915_gem_request_put(target);
John Harrisonff865882014-11-24 18:49:28 +00003807
Eric Anholt673a3942008-07-30 12:06:12 -07003808 return ret;
3809}
3810
Chris Wilsond23db882014-05-23 08:48:08 +02003811static bool
3812i915_vma_misplaced(struct i915_vma *vma, uint32_t alignment, uint64_t flags)
3813{
3814 struct drm_i915_gem_object *obj = vma->obj;
3815
3816 if (alignment &&
3817 vma->node.start & (alignment - 1))
3818 return true;
3819
3820 if (flags & PIN_MAPPABLE && !obj->map_and_fenceable)
3821 return true;
3822
3823 if (flags & PIN_OFFSET_BIAS &&
3824 vma->node.start < (flags & PIN_OFFSET_MASK))
3825 return true;
3826
Chris Wilson506a8e82015-12-08 11:55:07 +00003827 if (flags & PIN_OFFSET_FIXED &&
3828 vma->node.start != (flags & PIN_OFFSET_MASK))
3829 return true;
3830
Chris Wilsond23db882014-05-23 08:48:08 +02003831 return false;
3832}
3833
Chris Wilsond0710ab2015-11-20 14:16:39 +00003834void __i915_vma_set_map_and_fenceable(struct i915_vma *vma)
3835{
3836 struct drm_i915_gem_object *obj = vma->obj;
3837 bool mappable, fenceable;
3838 u32 fence_size, fence_alignment;
3839
3840 fence_size = i915_gem_get_gtt_size(obj->base.dev,
3841 obj->base.size,
3842 obj->tiling_mode);
3843 fence_alignment = i915_gem_get_gtt_alignment(obj->base.dev,
3844 obj->base.size,
3845 obj->tiling_mode,
3846 true);
3847
3848 fenceable = (vma->node.size == fence_size &&
3849 (vma->node.start & (fence_alignment - 1)) == 0);
3850
3851 mappable = (vma->node.start + fence_size <=
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003852 to_i915(obj->base.dev)->ggtt.mappable_end);
Chris Wilsond0710ab2015-11-20 14:16:39 +00003853
3854 obj->map_and_fenceable = mappable && fenceable;
3855}
3856
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003857static int
3858i915_gem_object_do_pin(struct drm_i915_gem_object *obj,
3859 struct i915_address_space *vm,
3860 const struct i915_ggtt_view *ggtt_view,
3861 uint32_t alignment,
3862 uint64_t flags)
Eric Anholt673a3942008-07-30 12:06:12 -07003863{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003864 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003865 struct i915_vma *vma;
Chris Wilsonef79e172014-10-31 13:53:52 +00003866 unsigned bound;
Eric Anholt673a3942008-07-30 12:06:12 -07003867 int ret;
3868
Ben Widawsky6e7186a2014-05-06 22:21:36 -07003869 if (WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base))
3870 return -ENODEV;
3871
Daniel Vetterbf3d1492014-02-14 14:01:12 +01003872 if (WARN_ON(flags & (PIN_GLOBAL | PIN_MAPPABLE) && !i915_is_ggtt(vm)))
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003873 return -EINVAL;
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003874
Chris Wilsonc826c442014-10-31 13:53:53 +00003875 if (WARN_ON((flags & (PIN_MAPPABLE | PIN_GLOBAL)) == PIN_MAPPABLE))
3876 return -EINVAL;
3877
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003878 if (WARN_ON(i915_is_ggtt(vm) != !!ggtt_view))
3879 return -EINVAL;
3880
3881 vma = ggtt_view ? i915_gem_obj_to_ggtt_view(obj, ggtt_view) :
3882 i915_gem_obj_to_vma(obj, vm);
3883
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003884 if (vma) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08003885 if (WARN_ON(vma->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT))
3886 return -EBUSY;
3887
Chris Wilsond23db882014-05-23 08:48:08 +02003888 if (i915_vma_misplaced(vma, alignment, flags)) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08003889 WARN(vma->pin_count,
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003890 "bo is already pinned in %s with incorrect alignment:"
Michel Thierry088e0df2015-08-07 17:40:17 +01003891 " offset=%08x %08x, req.alignment=%x, req.map_and_fenceable=%d,"
Daniel Vetter75e9e912010-11-04 17:11:09 +01003892 " obj->map_and_fenceable=%d\n",
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003893 ggtt_view ? "ggtt" : "ppgtt",
Michel Thierry088e0df2015-08-07 17:40:17 +01003894 upper_32_bits(vma->node.start),
3895 lower_32_bits(vma->node.start),
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003896 alignment,
Chris Wilsond23db882014-05-23 08:48:08 +02003897 !!(flags & PIN_MAPPABLE),
Chris Wilson05394f32010-11-08 19:18:58 +00003898 obj->map_and_fenceable);
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003899 ret = i915_vma_unbind(vma);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003900 if (ret)
3901 return ret;
Daniel Vetter8ea99c92014-02-14 14:01:21 +01003902
3903 vma = NULL;
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003904 }
3905 }
3906
Chris Wilsonef79e172014-10-31 13:53:52 +00003907 bound = vma ? vma->bound : 0;
Daniel Vetter8ea99c92014-02-14 14:01:21 +01003908 if (vma == NULL || !drm_mm_node_allocated(&vma->node)) {
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003909 vma = i915_gem_object_bind_to_vm(obj, vm, ggtt_view, alignment,
3910 flags);
Daniel Vetter262de142014-02-14 14:01:20 +01003911 if (IS_ERR(vma))
3912 return PTR_ERR(vma);
Daniel Vetter08755462015-04-20 09:04:05 -07003913 } else {
3914 ret = i915_vma_bind(vma, obj->cache_level, flags);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003915 if (ret)
3916 return ret;
3917 }
Daniel Vetter74898d72012-02-15 23:50:22 +01003918
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003919 if (ggtt_view && ggtt_view->type == I915_GGTT_VIEW_NORMAL &&
3920 (bound ^ vma->bound) & GLOBAL_BIND) {
Chris Wilsond0710ab2015-11-20 14:16:39 +00003921 __i915_vma_set_map_and_fenceable(vma);
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003922 WARN_ON(flags & PIN_MAPPABLE && !obj->map_and_fenceable);
3923 }
Chris Wilsonef79e172014-10-31 13:53:52 +00003924
Daniel Vetter8ea99c92014-02-14 14:01:21 +01003925 vma->pin_count++;
Eric Anholt673a3942008-07-30 12:06:12 -07003926 return 0;
3927}
3928
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003929int
3930i915_gem_object_pin(struct drm_i915_gem_object *obj,
3931 struct i915_address_space *vm,
3932 uint32_t alignment,
3933 uint64_t flags)
3934{
3935 return i915_gem_object_do_pin(obj, vm,
3936 i915_is_ggtt(vm) ? &i915_ggtt_view_normal : NULL,
3937 alignment, flags);
3938}
3939
3940int
3941i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
3942 const struct i915_ggtt_view *view,
3943 uint32_t alignment,
3944 uint64_t flags)
3945{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003946 struct drm_device *dev = obj->base.dev;
3947 struct drm_i915_private *dev_priv = to_i915(dev);
3948 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3949
Matthew Auldade7daa2016-03-24 15:54:20 +00003950 BUG_ON(!view);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003951
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003952 return i915_gem_object_do_pin(obj, &ggtt->base, view,
Tvrtko Ursulin6fafab72015-03-17 15:36:51 +00003953 alignment, flags | PIN_GLOBAL);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003954}
3955
Eric Anholt673a3942008-07-30 12:06:12 -07003956void
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003957i915_gem_object_ggtt_unpin_view(struct drm_i915_gem_object *obj,
3958 const struct i915_ggtt_view *view)
Eric Anholt673a3942008-07-30 12:06:12 -07003959{
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003960 struct i915_vma *vma = i915_gem_obj_to_ggtt_view(obj, view);
Eric Anholt673a3942008-07-30 12:06:12 -07003961
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003962 WARN_ON(vma->pin_count == 0);
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02003963 WARN_ON(!i915_gem_obj_ggtt_bound_view(obj, view));
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08003964
Chris Wilson30154652015-04-07 17:28:24 +01003965 --vma->pin_count;
Eric Anholt673a3942008-07-30 12:06:12 -07003966}
3967
3968int
Eric Anholt673a3942008-07-30 12:06:12 -07003969i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003970 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003971{
3972 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003973 struct drm_i915_gem_object *obj;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003974 int ret;
3975
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003976 ret = i915_mutex_lock_interruptible(dev);
3977 if (ret)
3978 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003979
Chris Wilson03ac0642016-07-20 13:31:51 +01003980 obj = i915_gem_object_lookup(file, args->handle);
3981 if (!obj) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003982 ret = -ENOENT;
3983 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003984 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003985
Chris Wilson0be555b2010-08-04 15:36:30 +01003986 /* Count all active objects as busy, even if they are currently not used
3987 * by the gpu. Users of this interface expect objects to eventually
Chris Wilson21c310f2016-08-04 07:52:34 +01003988 * become non-busy without any further actions.
Eric Anholtc4de0a52008-12-14 19:05:04 -08003989 */
Chris Wilson426960b2016-01-15 16:51:46 +00003990 args->busy = 0;
3991 if (obj->active) {
Chris Wilson27c01aa2016-08-04 07:52:30 +01003992 struct drm_i915_gem_request *req;
Chris Wilson426960b2016-01-15 16:51:46 +00003993 int i;
3994
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00003995 for (i = 0; i < I915_NUM_ENGINES; i++) {
Chris Wilsond72d9082016-08-04 07:52:31 +01003996 req = i915_gem_active_peek(&obj->last_read[i],
3997 &obj->base.dev->struct_mutex);
Chris Wilson426960b2016-01-15 16:51:46 +00003998 if (req)
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00003999 args->busy |= 1 << (16 + req->engine->exec_id);
Chris Wilson426960b2016-01-15 16:51:46 +00004000 }
Chris Wilsond72d9082016-08-04 07:52:31 +01004001 req = i915_gem_active_peek(&obj->last_write,
4002 &obj->base.dev->struct_mutex);
Chris Wilson27c01aa2016-08-04 07:52:30 +01004003 if (req)
4004 args->busy |= req->engine->exec_id;
Chris Wilson426960b2016-01-15 16:51:46 +00004005 }
Eric Anholt673a3942008-07-30 12:06:12 -07004006
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004007 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004008unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004009 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004010 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004011}
4012
4013int
4014i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4015 struct drm_file *file_priv)
4016{
Akshay Joshi0206e352011-08-16 15:34:10 -04004017 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004018}
4019
Chris Wilson3ef94da2009-09-14 16:50:29 +01004020int
4021i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4022 struct drm_file *file_priv)
4023{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004024 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004025 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004026 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004027 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004028
4029 switch (args->madv) {
4030 case I915_MADV_DONTNEED:
4031 case I915_MADV_WILLNEED:
4032 break;
4033 default:
4034 return -EINVAL;
4035 }
4036
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004037 ret = i915_mutex_lock_interruptible(dev);
4038 if (ret)
4039 return ret;
4040
Chris Wilson03ac0642016-07-20 13:31:51 +01004041 obj = i915_gem_object_lookup(file_priv, args->handle);
4042 if (!obj) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004043 ret = -ENOENT;
4044 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004045 }
Chris Wilson3ef94da2009-09-14 16:50:29 +01004046
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004047 if (i915_gem_obj_is_pinned(obj)) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004048 ret = -EINVAL;
4049 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004050 }
4051
Daniel Vetter656bfa32014-11-20 09:26:30 +01004052 if (obj->pages &&
4053 obj->tiling_mode != I915_TILING_NONE &&
4054 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
4055 if (obj->madv == I915_MADV_WILLNEED)
4056 i915_gem_object_unpin_pages(obj);
4057 if (args->madv == I915_MADV_WILLNEED)
4058 i915_gem_object_pin_pages(obj);
4059 }
4060
Chris Wilson05394f32010-11-08 19:18:58 +00004061 if (obj->madv != __I915_MADV_PURGED)
4062 obj->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004063
Chris Wilson6c085a72012-08-20 11:40:46 +02004064 /* if the object is no longer attached, discard its backing storage */
Daniel Vetterbe6a0372015-03-18 10:46:04 +01004065 if (obj->madv == I915_MADV_DONTNEED && obj->pages == NULL)
Chris Wilson2d7ef392009-09-20 23:13:10 +01004066 i915_gem_object_truncate(obj);
4067
Chris Wilson05394f32010-11-08 19:18:58 +00004068 args->retained = obj->madv != __I915_MADV_PURGED;
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004069
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004070out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004071 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004072unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01004073 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004074 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004075}
4076
Chris Wilson37e680a2012-06-07 15:38:42 +01004077void i915_gem_object_init(struct drm_i915_gem_object *obj,
4078 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01004079{
Chris Wilsonb4716182015-04-27 13:41:17 +01004080 int i;
4081
Ben Widawsky35c20a62013-05-31 11:28:48 -07004082 INIT_LIST_HEAD(&obj->global_list);
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00004083 for (i = 0; i < I915_NUM_ENGINES; i++)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004084 INIT_LIST_HEAD(&obj->engine_list[i]);
Ben Widawskyb25cb2f2013-08-14 11:38:33 +02004085 INIT_LIST_HEAD(&obj->obj_exec_link);
Ben Widawsky2f633152013-07-17 12:19:03 -07004086 INIT_LIST_HEAD(&obj->vma_list);
Chris Wilson8d9d5742015-04-07 16:20:38 +01004087 INIT_LIST_HEAD(&obj->batch_pool_link);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004088
Chris Wilson37e680a2012-06-07 15:38:42 +01004089 obj->ops = ops;
4090
Chris Wilson0327d6b2012-08-11 15:41:06 +01004091 obj->fence_reg = I915_FENCE_REG_NONE;
4092 obj->madv = I915_MADV_WILLNEED;
Chris Wilson0327d6b2012-08-11 15:41:06 +01004093
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004094 i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004095}
4096
Chris Wilson37e680a2012-06-07 15:38:42 +01004097static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
Chris Wilsonde472662016-01-22 18:32:31 +00004098 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE,
Chris Wilson37e680a2012-06-07 15:38:42 +01004099 .get_pages = i915_gem_object_get_pages_gtt,
4100 .put_pages = i915_gem_object_put_pages_gtt,
4101};
4102
Dave Gordond37cd8a2016-04-22 19:14:32 +01004103struct drm_i915_gem_object *i915_gem_object_create(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00004104 size_t size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004105{
Daniel Vetterc397b902010-04-09 19:05:07 +00004106 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004107 struct address_space *mapping;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004108 gfp_t mask;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004109 int ret;
Daniel Vetterc397b902010-04-09 19:05:07 +00004110
Chris Wilson42dcedd2012-11-15 11:32:30 +00004111 obj = i915_gem_object_alloc(dev);
Daniel Vetterc397b902010-04-09 19:05:07 +00004112 if (obj == NULL)
Chris Wilsonfe3db792016-04-25 13:32:13 +01004113 return ERR_PTR(-ENOMEM);
Daniel Vetterc397b902010-04-09 19:05:07 +00004114
Chris Wilsonfe3db792016-04-25 13:32:13 +01004115 ret = drm_gem_object_init(dev, &obj->base, size);
4116 if (ret)
4117 goto fail;
Daniel Vetterc397b902010-04-09 19:05:07 +00004118
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004119 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
4120 if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) {
4121 /* 965gm cannot relocate objects above 4GiB. */
4122 mask &= ~__GFP_HIGHMEM;
4123 mask |= __GFP_DMA32;
4124 }
4125
Al Viro496ad9a2013-01-23 17:07:38 -05004126 mapping = file_inode(obj->base.filp)->i_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004127 mapping_set_gfp_mask(mapping, mask);
Hugh Dickins5949eac2011-06-27 16:18:18 -07004128
Chris Wilson37e680a2012-06-07 15:38:42 +01004129 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004130
Daniel Vetterc397b902010-04-09 19:05:07 +00004131 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4132 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4133
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004134 if (HAS_LLC(dev)) {
4135 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004136 * cache) for about a 10% performance improvement
4137 * compared to uncached. Graphics requests other than
4138 * display scanout are coherent with the CPU in
4139 * accessing this cache. This means in this mode we
4140 * don't need to clflush on the CPU side, and on the
4141 * GPU side we only need to flush internal caches to
4142 * get data visible to the CPU.
4143 *
4144 * However, we maintain the display planes as UC, and so
4145 * need to rebind when first used as such.
4146 */
4147 obj->cache_level = I915_CACHE_LLC;
4148 } else
4149 obj->cache_level = I915_CACHE_NONE;
4150
Daniel Vetterd861e332013-07-24 23:25:03 +02004151 trace_i915_gem_object_create(obj);
4152
Chris Wilson05394f32010-11-08 19:18:58 +00004153 return obj;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004154
4155fail:
4156 i915_gem_object_free(obj);
4157
4158 return ERR_PTR(ret);
Daniel Vetterac52bc52010-04-09 19:05:06 +00004159}
4160
Chris Wilson340fbd82014-05-22 09:16:52 +01004161static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4162{
4163 /* If we are the last user of the backing storage (be it shmemfs
4164 * pages or stolen etc), we know that the pages are going to be
4165 * immediately released. In this case, we can then skip copying
4166 * back the contents from the GPU.
4167 */
4168
4169 if (obj->madv != I915_MADV_WILLNEED)
4170 return false;
4171
4172 if (obj->base.filp == NULL)
4173 return true;
4174
4175 /* At first glance, this looks racy, but then again so would be
4176 * userspace racing mmap against close. However, the first external
4177 * reference to the filp can only be obtained through the
4178 * i915_gem_mmap_ioctl() which safeguards us against the user
4179 * acquiring such a reference whilst we are in the middle of
4180 * freeing the object.
4181 */
4182 return atomic_long_read(&obj->base.filp->f_count) == 1;
4183}
4184
Chris Wilson1488fc02012-04-24 15:47:31 +01004185void i915_gem_free_object(struct drm_gem_object *gem_obj)
Chris Wilsonbe726152010-07-23 23:18:50 +01004186{
Chris Wilson1488fc02012-04-24 15:47:31 +01004187 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00004188 struct drm_device *dev = obj->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004189 struct drm_i915_private *dev_priv = to_i915(dev);
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004190 struct i915_vma *vma, *next;
Chris Wilsonbe726152010-07-23 23:18:50 +01004191
Paulo Zanonif65c9162013-11-27 18:20:34 -02004192 intel_runtime_pm_get(dev_priv);
4193
Chris Wilson26e12f82011-03-20 11:20:19 +00004194 trace_i915_gem_object_destroy(obj);
4195
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004196 list_for_each_entry_safe(vma, next, &obj->vma_list, obj_link) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004197 int ret;
4198
4199 vma->pin_count = 0;
Chris Wilsonc13d87e2016-07-20 09:21:15 +01004200 ret = __i915_vma_unbind_no_wait(vma);
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004201 if (WARN_ON(ret == -ERESTARTSYS)) {
4202 bool was_interruptible;
Chris Wilson1488fc02012-04-24 15:47:31 +01004203
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004204 was_interruptible = dev_priv->mm.interruptible;
4205 dev_priv->mm.interruptible = false;
Chris Wilson1488fc02012-04-24 15:47:31 +01004206
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004207 WARN_ON(i915_vma_unbind(vma));
Chris Wilson1488fc02012-04-24 15:47:31 +01004208
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004209 dev_priv->mm.interruptible = was_interruptible;
4210 }
Chris Wilson1488fc02012-04-24 15:47:31 +01004211 }
Chris Wilson15717de2016-08-04 07:52:26 +01004212 GEM_BUG_ON(obj->bind_count);
Chris Wilson1488fc02012-04-24 15:47:31 +01004213
Ben Widawsky1d64ae72013-05-31 14:46:20 -07004214 /* Stolen objects don't hold a ref, but do hold pin count. Fix that up
4215 * before progressing. */
4216 if (obj->stolen)
4217 i915_gem_object_unpin_pages(obj);
4218
Daniel Vettera071fa02014-06-18 23:28:09 +02004219 WARN_ON(obj->frontbuffer_bits);
4220
Daniel Vetter656bfa32014-11-20 09:26:30 +01004221 if (obj->pages && obj->madv == I915_MADV_WILLNEED &&
4222 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES &&
4223 obj->tiling_mode != I915_TILING_NONE)
4224 i915_gem_object_unpin_pages(obj);
4225
Ben Widawsky401c29f2013-05-31 11:28:47 -07004226 if (WARN_ON(obj->pages_pin_count))
4227 obj->pages_pin_count = 0;
Chris Wilson340fbd82014-05-22 09:16:52 +01004228 if (discard_backing_storage(obj))
Chris Wilson55372522014-03-25 13:23:06 +00004229 obj->madv = I915_MADV_DONTNEED;
Chris Wilson37e680a2012-06-07 15:38:42 +01004230 i915_gem_object_put_pages(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01004231
Chris Wilson9da3da62012-06-01 15:20:22 +01004232 BUG_ON(obj->pages);
4233
Chris Wilson2f745ad2012-09-04 21:02:58 +01004234 if (obj->base.import_attach)
4235 drm_prime_gem_destroy(&obj->base, NULL);
Chris Wilsonbe726152010-07-23 23:18:50 +01004236
Chris Wilson5cc9ed42014-05-16 14:22:37 +01004237 if (obj->ops->release)
4238 obj->ops->release(obj);
4239
Chris Wilson05394f32010-11-08 19:18:58 +00004240 drm_gem_object_release(&obj->base);
4241 i915_gem_info_remove_obj(dev_priv, obj->base.size);
Chris Wilsonbe726152010-07-23 23:18:50 +01004242
Chris Wilson05394f32010-11-08 19:18:58 +00004243 kfree(obj->bit_17);
Chris Wilson42dcedd2012-11-15 11:32:30 +00004244 i915_gem_object_free(obj);
Paulo Zanonif65c9162013-11-27 18:20:34 -02004245
4246 intel_runtime_pm_put(dev_priv);
Chris Wilsonbe726152010-07-23 23:18:50 +01004247}
4248
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004249struct i915_vma *i915_gem_obj_to_vma(struct drm_i915_gem_object *obj,
4250 struct i915_address_space *vm)
Ben Widawsky2f633152013-07-17 12:19:03 -07004251{
Daniel Vettere656a6c2013-08-14 14:14:04 +02004252 struct i915_vma *vma;
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004253 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Tvrtko Ursulin1b683722015-11-12 11:59:55 +00004254 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL &&
4255 vma->vm == vm)
Daniel Vettere656a6c2013-08-14 14:14:04 +02004256 return vma;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004257 }
4258 return NULL;
4259}
Daniel Vettere656a6c2013-08-14 14:14:04 +02004260
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004261struct i915_vma *i915_gem_obj_to_ggtt_view(struct drm_i915_gem_object *obj,
4262 const struct i915_ggtt_view *view)
4263{
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004264 struct i915_vma *vma;
4265
Tvrtko Ursulin598b9ec2016-04-21 13:04:44 +01004266 GEM_BUG_ON(!view);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004267
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004268 list_for_each_entry(vma, &obj->vma_list, obj_link)
Tvrtko Ursulin598b9ec2016-04-21 13:04:44 +01004269 if (vma->is_ggtt && i915_ggtt_view_equal(&vma->ggtt_view, view))
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004270 return vma;
Daniel Vettere656a6c2013-08-14 14:14:04 +02004271 return NULL;
4272}
4273
Ben Widawsky2f633152013-07-17 12:19:03 -07004274void i915_gem_vma_destroy(struct i915_vma *vma)
4275{
4276 WARN_ON(vma->node.allocated);
Chris Wilsonaaa056672013-08-20 12:56:40 +01004277
4278 /* Keep the vma as a placeholder in the execbuffer reservation lists */
4279 if (!list_empty(&vma->exec_list))
4280 return;
4281
Chris Wilson596c5922016-02-26 11:03:20 +00004282 if (!vma->is_ggtt)
4283 i915_ppgtt_put(i915_vm_to_ppgtt(vma->vm));
Michel Thierryb9d06dd2014-08-06 15:04:44 +02004284
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004285 list_del(&vma->obj_link);
Daniel Vetterb93dab62013-08-26 11:23:47 +02004286
Chris Wilsone20d2ab2015-04-07 16:20:58 +01004287 kmem_cache_free(to_i915(vma->obj->base.dev)->vmas, vma);
Ben Widawsky2f633152013-07-17 12:19:03 -07004288}
4289
Chris Wilsone3efda42014-04-09 09:19:41 +01004290static void
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004291i915_gem_stop_engines(struct drm_device *dev)
Chris Wilsone3efda42014-04-09 09:19:41 +01004292{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004293 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004294 struct intel_engine_cs *engine;
Chris Wilsone3efda42014-04-09 09:19:41 +01004295
Dave Gordonb4ac5af2016-03-24 11:20:38 +00004296 for_each_engine(engine, dev_priv)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004297 dev_priv->gt.stop_engine(engine);
Chris Wilsone3efda42014-04-09 09:19:41 +01004298}
4299
Jesse Barnes5669fca2009-02-17 15:13:31 -08004300int
Chris Wilson45c5f202013-10-16 11:50:01 +01004301i915_gem_suspend(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07004302{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004303 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson45c5f202013-10-16 11:50:01 +01004304 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004305
Chris Wilson54b4f682016-07-21 21:16:19 +01004306 intel_suspend_gt_powersave(dev_priv);
4307
Chris Wilson45c5f202013-10-16 11:50:01 +01004308 mutex_lock(&dev->struct_mutex);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004309
4310 /* We have to flush all the executing contexts to main memory so
4311 * that they can saved in the hibernation image. To ensure the last
4312 * context image is coherent, we have to switch away from it. That
4313 * leaves the dev_priv->kernel_context still active when
4314 * we actually suspend, and its image in memory may not match the GPU
4315 * state. Fortunately, the kernel_context is disposable and we do
4316 * not rely on its state.
4317 */
4318 ret = i915_gem_switch_to_kernel_context(dev_priv);
4319 if (ret)
4320 goto err;
4321
Chris Wilson6e5a5be2016-06-24 14:55:57 +01004322 ret = i915_gem_wait_for_idle(dev_priv);
Chris Wilsonf7403342013-09-13 23:57:04 +01004323 if (ret)
Chris Wilson45c5f202013-10-16 11:50:01 +01004324 goto err;
Chris Wilsonf7403342013-09-13 23:57:04 +01004325
Chris Wilsonc0336662016-05-06 15:40:21 +01004326 i915_gem_retire_requests(dev_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004327
Chris Wilson5ab57c72016-07-15 14:56:20 +01004328 /* Note that rather than stopping the engines, all we have to do
4329 * is assert that every RING_HEAD == RING_TAIL (all execution complete)
4330 * and similar for all logical context images (to ensure they are
4331 * all ready for hibernation).
4332 */
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004333 i915_gem_stop_engines(dev);
Chris Wilsonb2e862d2016-04-28 09:56:41 +01004334 i915_gem_context_lost(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01004335 mutex_unlock(&dev->struct_mutex);
4336
Chris Wilson737b1502015-01-26 18:03:03 +02004337 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
Chris Wilson67d97da2016-07-04 08:08:31 +01004338 cancel_delayed_work_sync(&dev_priv->gt.retire_work);
4339 flush_delayed_work(&dev_priv->gt.idle_work);
Chris Wilson29105cc2010-01-07 10:39:13 +00004340
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004341 /* Assert that we sucessfully flushed all the work and
4342 * reset the GPU back to its idle, low power state.
4343 */
Chris Wilson67d97da2016-07-04 08:08:31 +01004344 WARN_ON(dev_priv->gt.awake);
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004345
Eric Anholt673a3942008-07-30 12:06:12 -07004346 return 0;
Chris Wilson45c5f202013-10-16 11:50:01 +01004347
4348err:
4349 mutex_unlock(&dev->struct_mutex);
4350 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004351}
4352
Chris Wilson5ab57c72016-07-15 14:56:20 +01004353void i915_gem_resume(struct drm_device *dev)
4354{
4355 struct drm_i915_private *dev_priv = to_i915(dev);
4356
4357 mutex_lock(&dev->struct_mutex);
4358 i915_gem_restore_gtt_mappings(dev);
4359
4360 /* As we didn't flush the kernel context before suspend, we cannot
4361 * guarantee that the context image is complete. So let's just reset
4362 * it and start again.
4363 */
4364 if (i915.enable_execlists)
4365 intel_lr_context_reset(dev_priv, dev_priv->kernel_context);
4366
4367 mutex_unlock(&dev->struct_mutex);
4368}
4369
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004370void i915_gem_init_swizzling(struct drm_device *dev)
4371{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004372 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004373
Daniel Vetter11782b02012-01-31 16:47:55 +01004374 if (INTEL_INFO(dev)->gen < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004375 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
4376 return;
4377
4378 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
4379 DISP_TILE_SURFACE_SWIZZLING);
4380
Daniel Vetter11782b02012-01-31 16:47:55 +01004381 if (IS_GEN5(dev))
4382 return;
4383
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004384 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
4385 if (IS_GEN6(dev))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004386 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Ben Widawsky8782e262012-12-18 10:31:23 -08004387 else if (IS_GEN7(dev))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004388 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Ben Widawsky31a53362013-11-02 21:07:04 -07004389 else if (IS_GEN8(dev))
4390 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08004391 else
4392 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004393}
Daniel Vettere21af882012-02-09 20:53:27 +01004394
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004395static void init_unused_ring(struct drm_device *dev, u32 base)
4396{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004397 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004398
4399 I915_WRITE(RING_CTL(base), 0);
4400 I915_WRITE(RING_HEAD(base), 0);
4401 I915_WRITE(RING_TAIL(base), 0);
4402 I915_WRITE(RING_START(base), 0);
4403}
4404
4405static void init_unused_rings(struct drm_device *dev)
4406{
4407 if (IS_I830(dev)) {
4408 init_unused_ring(dev, PRB1_BASE);
4409 init_unused_ring(dev, SRB0_BASE);
4410 init_unused_ring(dev, SRB1_BASE);
4411 init_unused_ring(dev, SRB2_BASE);
4412 init_unused_ring(dev, SRB3_BASE);
4413 } else if (IS_GEN2(dev)) {
4414 init_unused_ring(dev, SRB0_BASE);
4415 init_unused_ring(dev, SRB1_BASE);
4416 } else if (IS_GEN3(dev)) {
4417 init_unused_ring(dev, PRB1_BASE);
4418 init_unused_ring(dev, PRB2_BASE);
4419 }
4420}
4421
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004422int
4423i915_gem_init_hw(struct drm_device *dev)
4424{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004425 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004426 struct intel_engine_cs *engine;
Chris Wilsond200cda2016-04-28 09:56:44 +01004427 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004428
Chris Wilson5e4f5182015-02-13 14:35:59 +00004429 /* Double layer security blanket, see i915_gem_init() */
4430 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4431
Mika Kuoppala3accaf72016-04-13 17:26:43 +03004432 if (HAS_EDRAM(dev) && INTEL_GEN(dev_priv) < 9)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07004433 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004434
Ville Syrjälä0bf21342013-11-29 14:56:12 +02004435 if (IS_HASWELL(dev))
4436 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev) ?
4437 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03004438
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07004439 if (HAS_PCH_NOP(dev)) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01004440 if (IS_IVYBRIDGE(dev)) {
4441 u32 temp = I915_READ(GEN7_MSG_CTL);
4442 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
4443 I915_WRITE(GEN7_MSG_CTL, temp);
4444 } else if (INTEL_INFO(dev)->gen >= 7) {
4445 u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
4446 temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
4447 I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
4448 }
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07004449 }
4450
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004451 i915_gem_init_swizzling(dev);
4452
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004453 /*
4454 * At least 830 can leave some of the unused rings
4455 * "active" (ie. head != tail) after resume which
4456 * will prevent c3 entry. Makes sure all unused rings
4457 * are totally idle.
4458 */
4459 init_unused_rings(dev);
4460
Dave Gordoned54c1a2016-01-19 19:02:54 +00004461 BUG_ON(!dev_priv->kernel_context);
John Harrison90638cc2015-05-29 17:43:37 +01004462
John Harrison4ad2fd82015-06-18 13:11:20 +01004463 ret = i915_ppgtt_init_hw(dev);
4464 if (ret) {
4465 DRM_ERROR("PPGTT enable HW failed %d\n", ret);
4466 goto out;
4467 }
4468
4469 /* Need to do basic initialisation of all rings first: */
Dave Gordonb4ac5af2016-03-24 11:20:38 +00004470 for_each_engine(engine, dev_priv) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004471 ret = engine->init_hw(engine);
Daniel Vetter35a57ff2014-11-20 00:33:07 +01004472 if (ret)
Chris Wilson5e4f5182015-02-13 14:35:59 +00004473 goto out;
Daniel Vetter35a57ff2014-11-20 00:33:07 +01004474 }
Mika Kuoppala99433932013-01-22 14:12:17 +02004475
Peter Antoine0ccdacf2016-04-13 15:03:25 +01004476 intel_mocs_init_l3cc_table(dev);
4477
Alex Dai33a732f2015-08-12 15:43:36 +01004478 /* We can't enable contexts until all firmware is loaded */
Dave Gordone556f7c2016-06-07 09:14:49 +01004479 ret = intel_guc_setup(dev);
4480 if (ret)
4481 goto out;
Alex Dai33a732f2015-08-12 15:43:36 +01004482
Chris Wilson5e4f5182015-02-13 14:35:59 +00004483out:
4484 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004485 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004486}
4487
Chris Wilson39df9192016-07-20 13:31:57 +01004488bool intel_sanitize_semaphores(struct drm_i915_private *dev_priv, int value)
4489{
4490 if (INTEL_INFO(dev_priv)->gen < 6)
4491 return false;
4492
4493 /* TODO: make semaphores and Execlists play nicely together */
4494 if (i915.enable_execlists)
4495 return false;
4496
4497 if (value >= 0)
4498 return value;
4499
4500#ifdef CONFIG_INTEL_IOMMU
4501 /* Enable semaphores on SNB when IO remapping is off */
4502 if (INTEL_INFO(dev_priv)->gen == 6 && intel_iommu_gfx_mapped)
4503 return false;
4504#endif
4505
4506 return true;
4507}
4508
Chris Wilson1070a422012-04-24 15:47:41 +01004509int i915_gem_init(struct drm_device *dev)
4510{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004511 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson1070a422012-04-24 15:47:41 +01004512 int ret;
4513
Chris Wilson1070a422012-04-24 15:47:41 +01004514 mutex_lock(&dev->struct_mutex);
Jesse Barnesd62b4892013-03-08 10:45:53 -08004515
Oscar Mateoa83014d2014-07-24 17:04:21 +01004516 if (!i915.enable_execlists) {
Chris Wilson7e37f882016-08-02 22:50:21 +01004517 dev_priv->gt.cleanup_engine = intel_engine_cleanup;
4518 dev_priv->gt.stop_engine = intel_engine_stop;
Oscar Mateo454afeb2014-07-24 17:04:22 +01004519 } else {
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004520 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
4521 dev_priv->gt.stop_engine = intel_logical_ring_stop;
Oscar Mateoa83014d2014-07-24 17:04:21 +01004522 }
4523
Chris Wilson5e4f5182015-02-13 14:35:59 +00004524 /* This is just a security blanket to placate dragons.
4525 * On some systems, we very sporadically observe that the first TLBs
4526 * used by the CS may be stale, despite us poking the TLB reset. If
4527 * we hold the forcewake during initialisation these problems
4528 * just magically go away.
4529 */
4530 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4531
Chris Wilson72778cb2016-05-19 16:17:16 +01004532 i915_gem_init_userptr(dev_priv);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01004533
4534 ret = i915_gem_init_ggtt(dev_priv);
4535 if (ret)
4536 goto out_unlock;
Jesse Barnesd62b4892013-03-08 10:45:53 -08004537
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004538 ret = i915_gem_context_init(dev);
Jani Nikula7bcc3772014-12-05 14:17:42 +02004539 if (ret)
4540 goto out_unlock;
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004541
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01004542 ret = intel_engines_init(dev);
Daniel Vetter35a57ff2014-11-20 00:33:07 +01004543 if (ret)
Jani Nikula7bcc3772014-12-05 14:17:42 +02004544 goto out_unlock;
Daniel Vetter53ca26c2012-04-26 23:28:03 +02004545
4546 ret = i915_gem_init_hw(dev);
Chris Wilson60990322014-04-09 09:19:42 +01004547 if (ret == -EIO) {
Chris Wilson7e21d642016-07-27 09:07:29 +01004548 /* Allow engine initialisation to fail by marking the GPU as
Chris Wilson60990322014-04-09 09:19:42 +01004549 * wedged. But we only want to do this where the GPU is angry,
4550 * for all other failure, such as an allocation failure, bail.
4551 */
4552 DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
Peter Zijlstra805de8f42015-04-24 01:12:32 +02004553 atomic_or(I915_WEDGED, &dev_priv->gpu_error.reset_counter);
Chris Wilson60990322014-04-09 09:19:42 +01004554 ret = 0;
Chris Wilson1070a422012-04-24 15:47:41 +01004555 }
Jani Nikula7bcc3772014-12-05 14:17:42 +02004556
4557out_unlock:
Chris Wilson5e4f5182015-02-13 14:35:59 +00004558 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Chris Wilson60990322014-04-09 09:19:42 +01004559 mutex_unlock(&dev->struct_mutex);
Chris Wilson1070a422012-04-24 15:47:41 +01004560
Chris Wilson60990322014-04-09 09:19:42 +01004561 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01004562}
4563
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004564void
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004565i915_gem_cleanup_engines(struct drm_device *dev)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004566{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004567 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004568 struct intel_engine_cs *engine;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004569
Dave Gordonb4ac5af2016-03-24 11:20:38 +00004570 for_each_engine(engine, dev_priv)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004571 dev_priv->gt.cleanup_engine(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004572}
4573
Chris Wilson64193402010-10-24 12:38:05 +01004574static void
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00004575init_engine_lists(struct intel_engine_cs *engine)
Chris Wilson64193402010-10-24 12:38:05 +01004576{
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00004577 INIT_LIST_HEAD(&engine->active_list);
4578 INIT_LIST_HEAD(&engine->request_list);
Chris Wilson64193402010-10-24 12:38:05 +01004579}
4580
Eric Anholt673a3942008-07-30 12:06:12 -07004581void
Imre Deak40ae4e12016-03-16 14:54:03 +02004582i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
4583{
Chris Wilson91c8a322016-07-05 10:40:23 +01004584 struct drm_device *dev = &dev_priv->drm;
Imre Deak40ae4e12016-03-16 14:54:03 +02004585
4586 if (INTEL_INFO(dev_priv)->gen >= 7 && !IS_VALLEYVIEW(dev_priv) &&
4587 !IS_CHERRYVIEW(dev_priv))
4588 dev_priv->num_fence_regs = 32;
4589 else if (INTEL_INFO(dev_priv)->gen >= 4 || IS_I945G(dev_priv) ||
4590 IS_I945GM(dev_priv) || IS_G33(dev_priv))
4591 dev_priv->num_fence_regs = 16;
4592 else
4593 dev_priv->num_fence_regs = 8;
4594
Chris Wilsonc0336662016-05-06 15:40:21 +01004595 if (intel_vgpu_active(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02004596 dev_priv->num_fence_regs =
4597 I915_READ(vgtif_reg(avail_rs.fence_num));
4598
4599 /* Initialize fence registers to zero */
4600 i915_gem_restore_fences(dev);
4601
4602 i915_gem_detect_bit_6_swizzle(dev);
4603}
4604
4605void
Imre Deakd64aa092016-01-19 15:26:29 +02004606i915_gem_load_init(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07004607{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004608 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson42dcedd2012-11-15 11:32:30 +00004609 int i;
4610
Chris Wilsonefab6d82015-04-07 16:20:57 +01004611 dev_priv->objects =
Chris Wilson42dcedd2012-11-15 11:32:30 +00004612 kmem_cache_create("i915_gem_object",
4613 sizeof(struct drm_i915_gem_object), 0,
4614 SLAB_HWCACHE_ALIGN,
4615 NULL);
Chris Wilsone20d2ab2015-04-07 16:20:58 +01004616 dev_priv->vmas =
4617 kmem_cache_create("i915_gem_vma",
4618 sizeof(struct i915_vma), 0,
4619 SLAB_HWCACHE_ALIGN,
4620 NULL);
Chris Wilsonefab6d82015-04-07 16:20:57 +01004621 dev_priv->requests =
4622 kmem_cache_create("i915_gem_request",
4623 sizeof(struct drm_i915_gem_request), 0,
4624 SLAB_HWCACHE_ALIGN,
4625 NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07004626
Ben Widawskya33afea2013-09-17 21:12:45 -07004627 INIT_LIST_HEAD(&dev_priv->context_list);
Chris Wilson6c085a72012-08-20 11:40:46 +02004628 INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
4629 INIT_LIST_HEAD(&dev_priv->mm.bound_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004630 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00004631 for (i = 0; i < I915_NUM_ENGINES; i++)
4632 init_engine_lists(&dev_priv->engine[i]);
Daniel Vetter4b9de732011-10-09 21:52:02 +02004633 for (i = 0; i < I915_MAX_NUM_FENCES; i++)
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004634 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Chris Wilson67d97da2016-07-04 08:08:31 +01004635 INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
Eric Anholt673a3942008-07-30 12:06:12 -07004636 i915_gem_retire_work_handler);
Chris Wilson67d97da2016-07-04 08:08:31 +01004637 INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004638 i915_gem_idle_work_handler);
Chris Wilson1f15b762016-07-01 17:23:14 +01004639 init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01004640 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson31169712009-09-14 16:50:28 +01004641
Chris Wilson72bfa192010-12-19 11:42:05 +00004642 dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
4643
Chris Wilson19b2dbd2013-06-12 10:15:12 +01004644 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Eric Anholt10ed13e2011-05-06 13:53:49 -07004645
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004646 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01004647
Chris Wilsonce453d82011-02-21 14:43:56 +00004648 dev_priv->mm.interruptible = true;
4649
Daniel Vetterf99d7062014-06-19 16:01:59 +02004650 mutex_init(&dev_priv->fb_tracking.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004651}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004652
Imre Deakd64aa092016-01-19 15:26:29 +02004653void i915_gem_load_cleanup(struct drm_device *dev)
4654{
4655 struct drm_i915_private *dev_priv = to_i915(dev);
4656
4657 kmem_cache_destroy(dev_priv->requests);
4658 kmem_cache_destroy(dev_priv->vmas);
4659 kmem_cache_destroy(dev_priv->objects);
4660}
4661
Chris Wilson461fb992016-05-14 07:26:33 +01004662int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
4663{
4664 struct drm_i915_gem_object *obj;
4665
4666 /* Called just before we write the hibernation image.
4667 *
4668 * We need to update the domain tracking to reflect that the CPU
4669 * will be accessing all the pages to create and restore from the
4670 * hibernation, and so upon restoration those pages will be in the
4671 * CPU domain.
4672 *
4673 * To make sure the hibernation image contains the latest state,
4674 * we update that state just before writing out the image.
4675 */
4676
4677 list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list) {
4678 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4679 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4680 }
4681
4682 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
4683 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4684 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4685 }
4686
4687 return 0;
4688}
4689
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004690void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004691{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004692 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilson15f7bbc2016-07-26 12:01:52 +01004693 struct drm_i915_gem_request *request;
Eric Anholtb9624422009-06-03 07:27:35 +00004694
4695 /* Clean up our request list when the client is going away, so that
4696 * later retire_requests won't dereference our soon-to-be-gone
4697 * file_priv.
4698 */
Chris Wilson1c255952010-09-26 11:03:27 +01004699 spin_lock(&file_priv->mm.lock);
Chris Wilson15f7bbc2016-07-26 12:01:52 +01004700 list_for_each_entry(request, &file_priv->mm.request_list, client_list)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004701 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01004702 spin_unlock(&file_priv->mm.lock);
Chris Wilson31169712009-09-14 16:50:28 +01004703
Chris Wilson2e1b8732015-04-27 13:41:22 +01004704 if (!list_empty(&file_priv->rps.link)) {
Chris Wilson8d3afd72015-05-21 21:01:47 +01004705 spin_lock(&to_i915(dev)->rps.client_lock);
Chris Wilson2e1b8732015-04-27 13:41:22 +01004706 list_del(&file_priv->rps.link);
Chris Wilson8d3afd72015-05-21 21:01:47 +01004707 spin_unlock(&to_i915(dev)->rps.client_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01004708 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004709}
4710
4711int i915_gem_open(struct drm_device *dev, struct drm_file *file)
4712{
4713 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08004714 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004715
4716 DRM_DEBUG_DRIVER("\n");
4717
4718 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
4719 if (!file_priv)
4720 return -ENOMEM;
4721
4722 file->driver_priv = file_priv;
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004723 file_priv->dev_priv = to_i915(dev);
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02004724 file_priv->file = file;
Chris Wilson2e1b8732015-04-27 13:41:22 +01004725 INIT_LIST_HEAD(&file_priv->rps.link);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004726
4727 spin_lock_init(&file_priv->mm.lock);
4728 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004729
Chris Wilsonc80ff162016-07-27 09:07:27 +01004730 file_priv->bsd_engine = -1;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00004731
Ben Widawskye422b882013-12-06 14:10:58 -08004732 ret = i915_gem_context_open(dev, file);
4733 if (ret)
4734 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004735
Ben Widawskye422b882013-12-06 14:10:58 -08004736 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004737}
4738
Daniel Vetterb680c372014-09-19 18:27:27 +02004739/**
4740 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07004741 * @old: current GEM buffer for the frontbuffer slots
4742 * @new: new GEM buffer for the frontbuffer slots
4743 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02004744 *
4745 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
4746 * from @old and setting them in @new. Both @old and @new can be NULL.
4747 */
Daniel Vettera071fa02014-06-18 23:28:09 +02004748void i915_gem_track_fb(struct drm_i915_gem_object *old,
4749 struct drm_i915_gem_object *new,
4750 unsigned frontbuffer_bits)
4751{
4752 if (old) {
4753 WARN_ON(!mutex_is_locked(&old->base.dev->struct_mutex));
4754 WARN_ON(!(old->frontbuffer_bits & frontbuffer_bits));
4755 old->frontbuffer_bits &= ~frontbuffer_bits;
4756 }
4757
4758 if (new) {
4759 WARN_ON(!mutex_is_locked(&new->base.dev->struct_mutex));
4760 WARN_ON(new->frontbuffer_bits & frontbuffer_bits);
4761 new->frontbuffer_bits |= frontbuffer_bits;
4762 }
4763}
4764
Ben Widawskya70a3142013-07-31 16:59:56 -07004765/* All the new VM stuff */
Michel Thierry088e0df2015-08-07 17:40:17 +01004766u64 i915_gem_obj_offset(struct drm_i915_gem_object *o,
4767 struct i915_address_space *vm)
Ben Widawskya70a3142013-07-31 16:59:56 -07004768{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004769 struct drm_i915_private *dev_priv = to_i915(o->base.dev);
Ben Widawskya70a3142013-07-31 16:59:56 -07004770 struct i915_vma *vma;
4771
Daniel Vetter896ab1a2014-08-06 15:04:51 +02004772 WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base);
Ben Widawskya70a3142013-07-31 16:59:56 -07004773
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004774 list_for_each_entry(vma, &o->vma_list, obj_link) {
Chris Wilson596c5922016-02-26 11:03:20 +00004775 if (vma->is_ggtt &&
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004776 vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
4777 continue;
4778 if (vma->vm == vm)
Ben Widawskya70a3142013-07-31 16:59:56 -07004779 return vma->node.start;
Ben Widawskya70a3142013-07-31 16:59:56 -07004780 }
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004781
Daniel Vetterf25748ea2014-06-17 22:34:38 +02004782 WARN(1, "%s vma for this object not found.\n",
4783 i915_is_ggtt(vm) ? "global" : "ppgtt");
Ben Widawskya70a3142013-07-31 16:59:56 -07004784 return -1;
4785}
4786
Michel Thierry088e0df2015-08-07 17:40:17 +01004787u64 i915_gem_obj_ggtt_offset_view(struct drm_i915_gem_object *o,
4788 const struct i915_ggtt_view *view)
Ben Widawskya70a3142013-07-31 16:59:56 -07004789{
4790 struct i915_vma *vma;
4791
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004792 list_for_each_entry(vma, &o->vma_list, obj_link)
Tvrtko Ursulin8aac2222016-04-21 13:04:45 +01004793 if (vma->is_ggtt && i915_ggtt_view_equal(&vma->ggtt_view, view))
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004794 return vma->node.start;
4795
Tvrtko Ursulin5678ad72015-03-17 14:45:29 +00004796 WARN(1, "global vma for this object not found. (view=%u)\n", view->type);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004797 return -1;
4798}
4799
4800bool i915_gem_obj_bound(struct drm_i915_gem_object *o,
4801 struct i915_address_space *vm)
4802{
4803 struct i915_vma *vma;
4804
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004805 list_for_each_entry(vma, &o->vma_list, obj_link) {
Chris Wilson596c5922016-02-26 11:03:20 +00004806 if (vma->is_ggtt &&
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004807 vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
4808 continue;
4809 if (vma->vm == vm && drm_mm_node_allocated(&vma->node))
4810 return true;
4811 }
4812
4813 return false;
4814}
4815
4816bool i915_gem_obj_ggtt_bound_view(struct drm_i915_gem_object *o,
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02004817 const struct i915_ggtt_view *view)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004818{
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004819 struct i915_vma *vma;
4820
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004821 list_for_each_entry(vma, &o->vma_list, obj_link)
Tvrtko Ursulinff5ec222016-04-21 13:04:46 +01004822 if (vma->is_ggtt &&
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02004823 i915_ggtt_view_equal(&vma->ggtt_view, view) &&
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00004824 drm_mm_node_allocated(&vma->node))
Ben Widawskya70a3142013-07-31 16:59:56 -07004825 return true;
4826
4827 return false;
4828}
4829
Tvrtko Ursulin8da32722016-04-21 13:04:43 +01004830unsigned long i915_gem_obj_ggtt_size(struct drm_i915_gem_object *o)
Ben Widawskya70a3142013-07-31 16:59:56 -07004831{
Ben Widawskya70a3142013-07-31 16:59:56 -07004832 struct i915_vma *vma;
4833
Tvrtko Ursulin8da32722016-04-21 13:04:43 +01004834 GEM_BUG_ON(list_empty(&o->vma_list));
Ben Widawskya70a3142013-07-31 16:59:56 -07004835
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004836 list_for_each_entry(vma, &o->vma_list, obj_link) {
Chris Wilson596c5922016-02-26 11:03:20 +00004837 if (vma->is_ggtt &&
Tvrtko Ursulin8da32722016-04-21 13:04:43 +01004838 vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
Ben Widawskya70a3142013-07-31 16:59:56 -07004839 return vma->node.size;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004840 }
Tvrtko Ursulin8da32722016-04-21 13:04:43 +01004841
Ben Widawskya70a3142013-07-31 16:59:56 -07004842 return 0;
4843}
4844
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004845bool i915_gem_obj_is_pinned(struct drm_i915_gem_object *obj)
Ben Widawsky5c2abbe2013-09-24 09:57:57 -07004846{
4847 struct i915_vma *vma;
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004848 list_for_each_entry(vma, &obj->vma_list, obj_link)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004849 if (vma->pin_count > 0)
4850 return true;
Joonas Lahtinena6631ae2015-05-06 14:34:58 +03004851
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004852 return false;
Ben Widawsky5c2abbe2013-09-24 09:57:57 -07004853}
Dave Gordonea702992015-07-09 19:29:02 +01004854
Dave Gordon033908a2015-12-10 18:51:23 +00004855/* Like i915_gem_object_get_page(), but mark the returned page dirty */
4856struct page *
4857i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj, int n)
4858{
4859 struct page *page;
4860
4861 /* Only default objects have per-page dirty tracking */
Chris Wilsonb9bcd142016-06-20 15:05:51 +01004862 if (WARN_ON(!i915_gem_object_has_struct_page(obj)))
Dave Gordon033908a2015-12-10 18:51:23 +00004863 return NULL;
4864
4865 page = i915_gem_object_get_page(obj, n);
4866 set_page_dirty(page);
4867 return page;
4868}
4869
Dave Gordonea702992015-07-09 19:29:02 +01004870/* Allocate a new GEM object and fill it with the supplied data */
4871struct drm_i915_gem_object *
4872i915_gem_object_create_from_data(struct drm_device *dev,
4873 const void *data, size_t size)
4874{
4875 struct drm_i915_gem_object *obj;
4876 struct sg_table *sg;
4877 size_t bytes;
4878 int ret;
4879
Dave Gordond37cd8a2016-04-22 19:14:32 +01004880 obj = i915_gem_object_create(dev, round_up(size, PAGE_SIZE));
Chris Wilsonfe3db792016-04-25 13:32:13 +01004881 if (IS_ERR(obj))
Dave Gordonea702992015-07-09 19:29:02 +01004882 return obj;
4883
4884 ret = i915_gem_object_set_to_cpu_domain(obj, true);
4885 if (ret)
4886 goto fail;
4887
4888 ret = i915_gem_object_get_pages(obj);
4889 if (ret)
4890 goto fail;
4891
4892 i915_gem_object_pin_pages(obj);
4893 sg = obj->pages;
4894 bytes = sg_copy_from_buffer(sg->sgl, sg->nents, (void *)data, size);
Dave Gordon9e7d18c2015-12-10 18:51:24 +00004895 obj->dirty = 1; /* Backing store is now out of date */
Dave Gordonea702992015-07-09 19:29:02 +01004896 i915_gem_object_unpin_pages(obj);
4897
4898 if (WARN_ON(bytes != size)) {
4899 DRM_ERROR("Incomplete copy, wrote %zu of %zu", bytes, size);
4900 ret = -EFAULT;
4901 goto fail;
4902 }
4903
4904 return obj;
4905
4906fail:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004907 i915_gem_object_put(obj);
Dave Gordonea702992015-07-09 19:29:02 +01004908 return ERR_PTR(ret);
4909}