blob: 49e79f0ea7ddab4f7206c7c8d161153601d74214 [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/**
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002749 * Ensures that an object will eventually get non-busy by flushing any required
2750 * write domains, emitting any outstanding lazy request and retiring and
2751 * completed requests.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01002752 * @obj: object to flush
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002753 */
2754static int
2755i915_gem_object_flush_active(struct drm_i915_gem_object *obj)
2756{
John Harrisona5ac0f92015-05-29 17:44:15 +01002757 int i;
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002758
Chris Wilsonb4716182015-04-27 13:41:17 +01002759 if (!obj->active)
2760 return 0;
John Harrison41c52412014-11-24 18:49:43 +00002761
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00002762 for (i = 0; i < I915_NUM_ENGINES; i++) {
Chris Wilsonb4716182015-04-27 13:41:17 +01002763 struct drm_i915_gem_request *req;
2764
Chris Wilsond72d9082016-08-04 07:52:31 +01002765 req = i915_gem_active_peek(&obj->last_read[i],
2766 &obj->base.dev->struct_mutex);
Chris Wilsonb4716182015-04-27 13:41:17 +01002767 if (req == NULL)
2768 continue;
2769
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002770 if (i915_gem_request_completed(req))
Chris Wilsonb4716182015-04-27 13:41:17 +01002771 i915_gem_object_retire__read(obj, i);
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002772 }
2773
2774 return 0;
2775}
2776
2777/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002778 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01002779 * @dev: drm device pointer
2780 * @data: ioctl data blob
2781 * @file: drm file pointer
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002782 *
2783 * Returns 0 if successful, else an error is returned with the remaining time in
2784 * the timeout parameter.
2785 * -ETIME: object is still busy after timeout
2786 * -ERESTARTSYS: signal interrupted the wait
2787 * -ENONENT: object doesn't exist
2788 * Also possible, but rare:
2789 * -EAGAIN: GPU wedged
2790 * -ENOMEM: damn
2791 * -ENODEV: Internal IRQ fail
2792 * -E?: The add request failed
2793 *
2794 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
2795 * non-zero timeout parameter the wait ioctl will wait for the given number of
2796 * nanoseconds on an object becoming unbusy. Since the wait itself does so
2797 * without holding struct_mutex the object may become re-busied before this
2798 * function completes. A similar but shorter * race condition exists in the busy
2799 * ioctl
2800 */
2801int
2802i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
2803{
2804 struct drm_i915_gem_wait *args = data;
2805 struct drm_i915_gem_object *obj;
Chris Wilson27c01aa2016-08-04 07:52:30 +01002806 struct drm_i915_gem_request *requests[I915_NUM_ENGINES];
Chris Wilsonb4716182015-04-27 13:41:17 +01002807 int i, n = 0;
2808 int ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002809
Daniel Vetter11b5d512014-09-29 15:31:26 +02002810 if (args->flags != 0)
2811 return -EINVAL;
2812
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002813 ret = i915_mutex_lock_interruptible(dev);
2814 if (ret)
2815 return ret;
2816
Chris Wilson03ac0642016-07-20 13:31:51 +01002817 obj = i915_gem_object_lookup(file, args->bo_handle);
2818 if (!obj) {
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002819 mutex_unlock(&dev->struct_mutex);
2820 return -ENOENT;
2821 }
2822
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002823 /* Need to make sure the object gets inactive eventually. */
2824 ret = i915_gem_object_flush_active(obj);
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002825 if (ret)
2826 goto out;
2827
Chris Wilsonb4716182015-04-27 13:41:17 +01002828 if (!obj->active)
John Harrison97b2a6a2014-11-24 18:49:26 +00002829 goto out;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002830
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002831 /* Do this after OLR check to make sure we make forward progress polling
Chris Wilson762e4582015-03-04 18:09:26 +00002832 * on this IOCTL with a timeout == 0 (like busy ioctl)
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002833 */
Chris Wilson762e4582015-03-04 18:09:26 +00002834 if (args->timeout_ns == 0) {
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002835 ret = -ETIME;
2836 goto out;
2837 }
2838
Chris Wilsonf8c417c2016-07-20 13:31:53 +01002839 i915_gem_object_put(obj);
Chris Wilsonb4716182015-04-27 13:41:17 +01002840
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00002841 for (i = 0; i < I915_NUM_ENGINES; i++) {
Chris Wilson27c01aa2016-08-04 07:52:30 +01002842 struct drm_i915_gem_request *req;
Chris Wilsonb4716182015-04-27 13:41:17 +01002843
Chris Wilsond72d9082016-08-04 07:52:31 +01002844 req = i915_gem_active_get(&obj->last_read[i],
2845 &obj->base.dev->struct_mutex);
Chris Wilson27c01aa2016-08-04 07:52:30 +01002846 if (req)
2847 requests[n++] = req;
Chris Wilsonb4716182015-04-27 13:41:17 +01002848 }
2849
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002850 mutex_unlock(&dev->struct_mutex);
2851
Chris Wilsonb4716182015-04-27 13:41:17 +01002852 for (i = 0; i < n; i++) {
2853 if (ret == 0)
Chris Wilson27c01aa2016-08-04 07:52:30 +01002854 ret = __i915_wait_request(requests[i], true,
Chris Wilsonb4716182015-04-27 13:41:17 +01002855 args->timeout_ns > 0 ? &args->timeout_ns : NULL,
Chris Wilsonb6aa0872015-12-02 09:13:46 +00002856 to_rps_client(file));
Chris Wilson27c01aa2016-08-04 07:52:30 +01002857 i915_gem_request_put(requests[i]);
Chris Wilsonb4716182015-04-27 13:41:17 +01002858 }
John Harrisonff865882014-11-24 18:49:28 +00002859 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002860
2861out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01002862 i915_gem_object_put(obj);
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002863 mutex_unlock(&dev->struct_mutex);
2864 return ret;
2865}
2866
Chris Wilsonb4716182015-04-27 13:41:17 +01002867static int
2868__i915_gem_object_sync(struct drm_i915_gem_object *obj,
Chris Wilson8e637172016-08-02 22:50:26 +01002869 struct drm_i915_gem_request *to,
2870 struct drm_i915_gem_request *from)
Chris Wilsonb4716182015-04-27 13:41:17 +01002871{
Chris Wilsonb4716182015-04-27 13:41:17 +01002872 int ret;
2873
Chris Wilson8e637172016-08-02 22:50:26 +01002874 if (to->engine == from->engine)
Chris Wilsonb4716182015-04-27 13:41:17 +01002875 return 0;
2876
Chris Wilson8e637172016-08-02 22:50:26 +01002877 if (i915_gem_request_completed(from))
Chris Wilsonb4716182015-04-27 13:41:17 +01002878 return 0;
2879
Chris Wilson39df9192016-07-20 13:31:57 +01002880 if (!i915.semaphores) {
Chris Wilson8e637172016-08-02 22:50:26 +01002881 ret = __i915_wait_request(from,
2882 from->i915->mm.interruptible,
Chris Wilsona6f766f2015-04-27 13:41:20 +01002883 NULL,
Chris Wilson197be2a2016-07-20 09:21:13 +01002884 NO_WAITBOOST);
Chris Wilsonb4716182015-04-27 13:41:17 +01002885 if (ret)
2886 return ret;
2887
Chris Wilson8e637172016-08-02 22:50:26 +01002888 i915_gem_object_retire_request(obj, from);
Chris Wilsonb4716182015-04-27 13:41:17 +01002889 } else {
Chris Wilson8e637172016-08-02 22:50:26 +01002890 int idx = intel_engine_sync_index(from->engine, to->engine);
Chris Wilsonddf07be2016-08-02 22:50:39 +01002891 if (from->fence.seqno <= from->engine->semaphore.sync_seqno[idx])
Chris Wilsonb4716182015-04-27 13:41:17 +01002892 return 0;
2893
Chris Wilson8e637172016-08-02 22:50:26 +01002894 trace_i915_gem_ring_sync_to(to, from);
Chris Wilsonddf07be2016-08-02 22:50:39 +01002895 ret = to->engine->semaphore.sync_to(to, from);
Chris Wilsonb4716182015-04-27 13:41:17 +01002896 if (ret)
2897 return ret;
2898
Chris Wilsonddf07be2016-08-02 22:50:39 +01002899 from->engine->semaphore.sync_seqno[idx] = from->fence.seqno;
Chris Wilsonb4716182015-04-27 13:41:17 +01002900 }
2901
2902 return 0;
2903}
2904
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002905/**
Ben Widawsky5816d642012-04-11 11:18:19 -07002906 * i915_gem_object_sync - sync an object to a ring.
2907 *
2908 * @obj: object which may be in use on another ring.
Chris Wilson8e637172016-08-02 22:50:26 +01002909 * @to: request we are wishing to use
Ben Widawsky5816d642012-04-11 11:18:19 -07002910 *
2911 * This code is meant to abstract object synchronization with the GPU.
Chris Wilson8e637172016-08-02 22:50:26 +01002912 * Conceptually we serialise writes between engines inside the GPU.
2913 * We only allow one engine to write into a buffer at any time, but
2914 * multiple readers. To ensure each has a coherent view of memory, we must:
Chris Wilsonb4716182015-04-27 13:41:17 +01002915 *
2916 * - If there is an outstanding write request to the object, the new
2917 * request must wait for it to complete (either CPU or in hw, requests
2918 * on the same ring will be naturally ordered).
2919 *
2920 * - If we are a write request (pending_write_domain is set), the new
2921 * request must wait for outstanding read requests to complete.
Ben Widawsky5816d642012-04-11 11:18:19 -07002922 *
2923 * Returns 0 if successful, else propagates up the lower layer error.
2924 */
Ben Widawsky2911a352012-04-05 14:47:36 -07002925int
2926i915_gem_object_sync(struct drm_i915_gem_object *obj,
Chris Wilson8e637172016-08-02 22:50:26 +01002927 struct drm_i915_gem_request *to)
Ben Widawsky2911a352012-04-05 14:47:36 -07002928{
Chris Wilson8cac6f62016-08-04 07:52:32 +01002929 struct i915_gem_active *active;
2930 unsigned long active_mask;
2931 int idx;
Ben Widawsky2911a352012-04-05 14:47:36 -07002932
Chris Wilson8cac6f62016-08-04 07:52:32 +01002933 lockdep_assert_held(&obj->base.dev->struct_mutex);
2934
2935 active_mask = obj->active;
2936 if (!active_mask)
Ben Widawsky2911a352012-04-05 14:47:36 -07002937 return 0;
2938
Chris Wilson8cac6f62016-08-04 07:52:32 +01002939 if (obj->base.pending_write_domain) {
2940 active = obj->last_read;
Chris Wilsonb4716182015-04-27 13:41:17 +01002941 } else {
Chris Wilson8cac6f62016-08-04 07:52:32 +01002942 active_mask = 1;
2943 active = &obj->last_write;
Chris Wilsonb4716182015-04-27 13:41:17 +01002944 }
Chris Wilson8cac6f62016-08-04 07:52:32 +01002945
2946 for_each_active(active_mask, idx) {
2947 struct drm_i915_gem_request *request;
2948 int ret;
2949
2950 request = i915_gem_active_peek(&active[idx],
2951 &obj->base.dev->struct_mutex);
2952 if (!request)
2953 continue;
2954
2955 ret = __i915_gem_object_sync(obj, to, request);
Chris Wilsonb4716182015-04-27 13:41:17 +01002956 if (ret)
2957 return ret;
2958 }
Ben Widawsky2911a352012-04-05 14:47:36 -07002959
Chris Wilsonb4716182015-04-27 13:41:17 +01002960 return 0;
Ben Widawsky2911a352012-04-05 14:47:36 -07002961}
2962
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002963static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
2964{
2965 u32 old_write_domain, old_read_domains;
2966
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002967 /* Force a pagefault for domain tracking on next user access */
2968 i915_gem_release_mmap(obj);
2969
Keith Packardb97c3d92011-06-24 21:02:59 -07002970 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
2971 return;
2972
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002973 old_read_domains = obj->base.read_domains;
2974 old_write_domain = obj->base.write_domain;
2975
2976 obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
2977 obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
2978
2979 trace_i915_gem_object_change_domain(obj,
2980 old_read_domains,
2981 old_write_domain);
2982}
2983
Chris Wilson8ef85612016-04-28 09:56:39 +01002984static void __i915_vma_iounmap(struct i915_vma *vma)
2985{
2986 GEM_BUG_ON(vma->pin_count);
2987
2988 if (vma->iomap == NULL)
2989 return;
2990
2991 io_mapping_unmap(vma->iomap);
2992 vma->iomap = NULL;
2993}
2994
Tvrtko Ursuline9f24d52015-10-05 13:26:36 +01002995static int __i915_vma_unbind(struct i915_vma *vma, bool wait)
Eric Anholt673a3942008-07-30 12:06:12 -07002996{
Ben Widawsky07fe0b12013-07-31 17:00:10 -07002997 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson43e28f02013-01-08 10:53:09 +00002998 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002999
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003000 if (list_empty(&vma->obj_link))
Eric Anholt673a3942008-07-30 12:06:12 -07003001 return 0;
3002
Daniel Vetter0ff501c2013-08-29 19:50:31 +02003003 if (!drm_mm_node_allocated(&vma->node)) {
3004 i915_gem_vma_destroy(vma);
Daniel Vetter0ff501c2013-08-29 19:50:31 +02003005 return 0;
3006 }
Ben Widawsky433544b2013-08-13 18:09:06 -07003007
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08003008 if (vma->pin_count)
Chris Wilson31d8d652012-05-24 19:11:20 +01003009 return -EBUSY;
Eric Anholt673a3942008-07-30 12:06:12 -07003010
Chris Wilson15717de2016-08-04 07:52:26 +01003011 GEM_BUG_ON(obj->bind_count == 0);
3012 GEM_BUG_ON(!obj->pages);
Chris Wilsonc4670ad2012-08-20 10:23:27 +01003013
Tvrtko Ursuline9f24d52015-10-05 13:26:36 +01003014 if (wait) {
3015 ret = i915_gem_object_wait_rendering(obj, false);
3016 if (ret)
3017 return ret;
3018 }
Chris Wilsona8198ee2011-04-13 22:04:09 +01003019
Chris Wilson596c5922016-02-26 11:03:20 +00003020 if (vma->is_ggtt && vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL) {
Daniel Vetter8b1bc9b2014-02-14 14:06:07 +01003021 i915_gem_object_finish_gtt(obj);
Chris Wilsona8198ee2011-04-13 22:04:09 +01003022
Daniel Vetter8b1bc9b2014-02-14 14:06:07 +01003023 /* release the fence reg _after_ flushing */
3024 ret = i915_gem_object_put_fence(obj);
3025 if (ret)
3026 return ret;
Chris Wilson8ef85612016-04-28 09:56:39 +01003027
3028 __i915_vma_iounmap(vma);
Daniel Vetter8b1bc9b2014-02-14 14:06:07 +01003029 }
Daniel Vetter96b47b62009-12-15 17:50:00 +01003030
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003031 trace_i915_vma_unbind(vma);
Chris Wilsondb53a302011-02-03 11:57:46 +00003032
Daniel Vetter777dc5b2015-04-14 17:35:12 +02003033 vma->vm->unbind_vma(vma);
Mika Kuoppala5e562f12015-04-30 11:02:31 +03003034 vma->bound = 0;
Ben Widawsky6f65e292013-12-06 14:10:56 -08003035
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003036 list_del_init(&vma->vm_link);
Chris Wilson596c5922016-02-26 11:03:20 +00003037 if (vma->is_ggtt) {
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003038 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL) {
3039 obj->map_and_fenceable = false;
3040 } else if (vma->ggtt_view.pages) {
3041 sg_free_table(vma->ggtt_view.pages);
3042 kfree(vma->ggtt_view.pages);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003043 }
Chris Wilson016a65a2015-06-11 08:06:08 +01003044 vma->ggtt_view.pages = NULL;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003045 }
Eric Anholt673a3942008-07-30 12:06:12 -07003046
Ben Widawsky2f633152013-07-17 12:19:03 -07003047 drm_mm_remove_node(&vma->node);
3048 i915_gem_vma_destroy(vma);
3049
3050 /* Since the unbound list is global, only move to that list if
Daniel Vetterb93dab62013-08-26 11:23:47 +02003051 * no more VMAs exist. */
Chris Wilson15717de2016-08-04 07:52:26 +01003052 if (--obj->bind_count == 0)
3053 list_move_tail(&obj->global_list,
3054 &to_i915(obj->base.dev)->mm.unbound_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003055
Chris Wilson70903c32013-12-04 09:59:09 +00003056 /* And finally now the object is completely decoupled from this vma,
3057 * we can drop its hold on the backing storage and allow it to be
3058 * reaped by the shrinker.
3059 */
3060 i915_gem_object_unpin_pages(obj);
3061
Chris Wilson88241782011-01-07 17:09:48 +00003062 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00003063}
3064
Tvrtko Ursuline9f24d52015-10-05 13:26:36 +01003065int i915_vma_unbind(struct i915_vma *vma)
3066{
3067 return __i915_vma_unbind(vma, true);
3068}
3069
3070int __i915_vma_unbind_no_wait(struct i915_vma *vma)
3071{
3072 return __i915_vma_unbind(vma, false);
3073}
3074
Chris Wilson6e5a5be2016-06-24 14:55:57 +01003075int i915_gem_wait_for_idle(struct drm_i915_private *dev_priv)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003076{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003077 struct intel_engine_cs *engine;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003078 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003079
Chris Wilson91c8a322016-07-05 10:40:23 +01003080 lockdep_assert_held(&dev_priv->drm.struct_mutex);
Chris Wilson6e5a5be2016-06-24 14:55:57 +01003081
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003082 for_each_engine(engine, dev_priv) {
Chris Wilson62e63002016-06-24 14:55:52 +01003083 if (engine->last_context == NULL)
3084 continue;
3085
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00003086 ret = intel_engine_idle(engine);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003087 if (ret)
3088 return ret;
3089 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003090
Chris Wilsonb4716182015-04-27 13:41:17 +01003091 WARN_ON(i915_verify_lists(dev));
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01003092 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003093}
3094
Chris Wilson4144f9b2014-09-11 08:43:48 +01003095static bool i915_gem_valid_gtt_space(struct i915_vma *vma,
Chris Wilson42d6ab42012-07-26 11:49:32 +01003096 unsigned long cache_level)
3097{
Chris Wilson4144f9b2014-09-11 08:43:48 +01003098 struct drm_mm_node *gtt_space = &vma->node;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003099 struct drm_mm_node *other;
3100
Chris Wilson4144f9b2014-09-11 08:43:48 +01003101 /*
3102 * On some machines we have to be careful when putting differing types
3103 * of snoopable memory together to avoid the prefetcher crossing memory
3104 * domains and dying. During vm initialisation, we decide whether or not
3105 * these constraints apply and set the drm_mm.color_adjust
3106 * appropriately.
Chris Wilson42d6ab42012-07-26 11:49:32 +01003107 */
Chris Wilson4144f9b2014-09-11 08:43:48 +01003108 if (vma->vm->mm.color_adjust == NULL)
Chris Wilson42d6ab42012-07-26 11:49:32 +01003109 return true;
3110
Ben Widawskyc6cfb322013-07-05 14:41:06 -07003111 if (!drm_mm_node_allocated(gtt_space))
Chris Wilson42d6ab42012-07-26 11:49:32 +01003112 return true;
3113
3114 if (list_empty(&gtt_space->node_list))
3115 return true;
3116
3117 other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
3118 if (other->allocated && !other->hole_follows && other->color != cache_level)
3119 return false;
3120
3121 other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
3122 if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
3123 return false;
3124
3125 return true;
3126}
3127
Jesse Barnesde151cf2008-11-12 10:03:55 -08003128/**
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003129 * Finds free space in the GTT aperture and binds the object or a view of it
3130 * there.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003131 * @obj: object to bind
3132 * @vm: address space to bind into
3133 * @ggtt_view: global gtt view if applicable
3134 * @alignment: requested alignment
3135 * @flags: mask of PIN_* flags to use
Eric Anholt673a3942008-07-30 12:06:12 -07003136 */
Daniel Vetter262de142014-02-14 14:01:20 +01003137static struct i915_vma *
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003138i915_gem_object_bind_to_vm(struct drm_i915_gem_object *obj,
3139 struct i915_address_space *vm,
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003140 const struct i915_ggtt_view *ggtt_view,
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003141 unsigned alignment,
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003142 uint64_t flags)
Eric Anholt673a3942008-07-30 12:06:12 -07003143{
Chris Wilson05394f32010-11-08 19:18:58 +00003144 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003145 struct drm_i915_private *dev_priv = to_i915(dev);
3146 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Michel Thierry65bd3422015-07-29 17:23:58 +01003147 u32 fence_alignment, unfenced_alignment;
Michel Thierry101b5062015-10-01 13:33:57 +01003148 u32 search_flag, alloc_flag;
3149 u64 start, end;
Michel Thierry65bd3422015-07-29 17:23:58 +01003150 u64 size, fence_size;
Ben Widawsky2f633152013-07-17 12:19:03 -07003151 struct i915_vma *vma;
Chris Wilson07f73f62009-09-14 16:50:30 +01003152 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003153
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003154 if (i915_is_ggtt(vm)) {
3155 u32 view_size;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003156
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003157 if (WARN_ON(!ggtt_view))
3158 return ERR_PTR(-EINVAL);
3159
3160 view_size = i915_ggtt_view_size(obj, ggtt_view);
3161
3162 fence_size = i915_gem_get_gtt_size(dev,
3163 view_size,
3164 obj->tiling_mode);
3165 fence_alignment = i915_gem_get_gtt_alignment(dev,
3166 view_size,
3167 obj->tiling_mode,
3168 true);
3169 unfenced_alignment = i915_gem_get_gtt_alignment(dev,
3170 view_size,
3171 obj->tiling_mode,
3172 false);
3173 size = flags & PIN_MAPPABLE ? fence_size : view_size;
3174 } else {
3175 fence_size = i915_gem_get_gtt_size(dev,
3176 obj->base.size,
3177 obj->tiling_mode);
3178 fence_alignment = i915_gem_get_gtt_alignment(dev,
3179 obj->base.size,
3180 obj->tiling_mode,
3181 true);
3182 unfenced_alignment =
3183 i915_gem_get_gtt_alignment(dev,
3184 obj->base.size,
3185 obj->tiling_mode,
3186 false);
3187 size = flags & PIN_MAPPABLE ? fence_size : obj->base.size;
3188 }
Chris Wilsona00b10c2010-09-24 21:15:47 +01003189
Michel Thierry101b5062015-10-01 13:33:57 +01003190 start = flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
3191 end = vm->total;
3192 if (flags & PIN_MAPPABLE)
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003193 end = min_t(u64, end, ggtt->mappable_end);
Michel Thierry101b5062015-10-01 13:33:57 +01003194 if (flags & PIN_ZONE_4G)
Michel Thierry48ea1e32016-01-11 11:39:27 +00003195 end = min_t(u64, end, (1ULL << 32) - PAGE_SIZE);
Michel Thierry101b5062015-10-01 13:33:57 +01003196
Eric Anholt673a3942008-07-30 12:06:12 -07003197 if (alignment == 0)
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003198 alignment = flags & PIN_MAPPABLE ? fence_alignment :
Daniel Vetter5e783302010-11-14 22:32:36 +01003199 unfenced_alignment;
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003200 if (flags & PIN_MAPPABLE && alignment & (fence_alignment - 1)) {
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003201 DRM_DEBUG("Invalid object (view type=%u) alignment requested %u\n",
3202 ggtt_view ? ggtt_view->type : 0,
3203 alignment);
Daniel Vetter262de142014-02-14 14:01:20 +01003204 return ERR_PTR(-EINVAL);
Eric Anholt673a3942008-07-30 12:06:12 -07003205 }
3206
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003207 /* If binding the object/GGTT view requires more space than the entire
3208 * aperture has, reject it early before evicting everything in a vain
3209 * attempt to find space.
Chris Wilson654fc602010-05-27 13:18:21 +01003210 */
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003211 if (size > end) {
Michel Thierry65bd3422015-07-29 17:23:58 +01003212 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 +03003213 ggtt_view ? ggtt_view->type : 0,
3214 size,
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003215 flags & PIN_MAPPABLE ? "mappable" : "total",
Chris Wilsond23db882014-05-23 08:48:08 +02003216 end);
Daniel Vetter262de142014-02-14 14:01:20 +01003217 return ERR_PTR(-E2BIG);
Chris Wilson654fc602010-05-27 13:18:21 +01003218 }
3219
Chris Wilson37e680a2012-06-07 15:38:42 +01003220 ret = i915_gem_object_get_pages(obj);
Chris Wilson6c085a72012-08-20 11:40:46 +02003221 if (ret)
Daniel Vetter262de142014-02-14 14:01:20 +01003222 return ERR_PTR(ret);
Chris Wilson6c085a72012-08-20 11:40:46 +02003223
Chris Wilsonfbdda6f2012-11-20 10:45:16 +00003224 i915_gem_object_pin_pages(obj);
3225
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003226 vma = ggtt_view ? i915_gem_obj_lookup_or_create_ggtt_vma(obj, ggtt_view) :
3227 i915_gem_obj_lookup_or_create_vma(obj, vm);
3228
Daniel Vetter262de142014-02-14 14:01:20 +01003229 if (IS_ERR(vma))
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003230 goto err_unpin;
Ben Widawsky2f633152013-07-17 12:19:03 -07003231
Chris Wilson506a8e82015-12-08 11:55:07 +00003232 if (flags & PIN_OFFSET_FIXED) {
3233 uint64_t offset = flags & PIN_OFFSET_MASK;
3234
3235 if (offset & (alignment - 1) || offset + size > end) {
3236 ret = -EINVAL;
3237 goto err_free_vma;
3238 }
3239 vma->node.start = offset;
3240 vma->node.size = size;
3241 vma->node.color = obj->cache_level;
3242 ret = drm_mm_reserve_node(&vm->mm, &vma->node);
3243 if (ret) {
3244 ret = i915_gem_evict_for_vma(vma);
3245 if (ret == 0)
3246 ret = drm_mm_reserve_node(&vm->mm, &vma->node);
3247 }
3248 if (ret)
3249 goto err_free_vma;
Michel Thierry101b5062015-10-01 13:33:57 +01003250 } else {
Chris Wilson506a8e82015-12-08 11:55:07 +00003251 if (flags & PIN_HIGH) {
3252 search_flag = DRM_MM_SEARCH_BELOW;
3253 alloc_flag = DRM_MM_CREATE_TOP;
3254 } else {
3255 search_flag = DRM_MM_SEARCH_DEFAULT;
3256 alloc_flag = DRM_MM_CREATE_DEFAULT;
3257 }
Michel Thierry101b5062015-10-01 13:33:57 +01003258
Ben Widawsky0a9ae0d2013-05-25 12:26:35 -07003259search_free:
Chris Wilson506a8e82015-12-08 11:55:07 +00003260 ret = drm_mm_insert_node_in_range_generic(&vm->mm, &vma->node,
3261 size, alignment,
3262 obj->cache_level,
3263 start, end,
3264 search_flag,
3265 alloc_flag);
3266 if (ret) {
3267 ret = i915_gem_evict_something(dev, vm, size, alignment,
3268 obj->cache_level,
3269 start, end,
3270 flags);
3271 if (ret == 0)
3272 goto search_free;
Chris Wilson97311292009-09-21 00:22:34 +01003273
Chris Wilson506a8e82015-12-08 11:55:07 +00003274 goto err_free_vma;
3275 }
Chris Wilsondc9dd7a2012-12-07 20:37:07 +00003276 }
Chris Wilson4144f9b2014-09-11 08:43:48 +01003277 if (WARN_ON(!i915_gem_valid_gtt_space(vma, obj->cache_level))) {
Ben Widawsky2f633152013-07-17 12:19:03 -07003278 ret = -EINVAL;
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003279 goto err_remove_node;
Eric Anholt673a3942008-07-30 12:06:12 -07003280 }
3281
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003282 trace_i915_vma_bind(vma, flags);
Daniel Vetter08755462015-04-20 09:04:05 -07003283 ret = i915_vma_bind(vma, obj->cache_level, flags);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003284 if (ret)
Imre Deake2273302015-07-09 12:59:05 +03003285 goto err_remove_node;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003286
Ben Widawsky35c20a62013-05-31 11:28:48 -07003287 list_move_tail(&obj->global_list, &dev_priv->mm.bound_list);
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003288 list_add_tail(&vma->vm_link, &vm->inactive_list);
Chris Wilson15717de2016-08-04 07:52:26 +01003289 obj->bind_count++;
Chris Wilsonbf1a1092010-08-07 11:01:20 +01003290
Daniel Vetter262de142014-02-14 14:01:20 +01003291 return vma;
Ben Widawsky2f633152013-07-17 12:19:03 -07003292
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003293err_remove_node:
Dan Carpenter6286ef92013-07-19 08:46:27 +03003294 drm_mm_remove_node(&vma->node);
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003295err_free_vma:
Ben Widawsky2f633152013-07-17 12:19:03 -07003296 i915_gem_vma_destroy(vma);
Daniel Vetter262de142014-02-14 14:01:20 +01003297 vma = ERR_PTR(ret);
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003298err_unpin:
Ben Widawsky2f633152013-07-17 12:19:03 -07003299 i915_gem_object_unpin_pages(obj);
Daniel Vetter262de142014-02-14 14:01:20 +01003300 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07003301}
3302
Chris Wilson000433b2013-08-08 14:41:09 +01003303bool
Chris Wilson2c225692013-08-09 12:26:45 +01003304i915_gem_clflush_object(struct drm_i915_gem_object *obj,
3305 bool force)
Eric Anholt673a3942008-07-30 12:06:12 -07003306{
Eric Anholt673a3942008-07-30 12:06:12 -07003307 /* If we don't have a page list set up, then we're not pinned
3308 * to GPU, and we can ignore the cache flush because it'll happen
3309 * again at bind time.
3310 */
Chris Wilson05394f32010-11-08 19:18:58 +00003311 if (obj->pages == NULL)
Chris Wilson000433b2013-08-08 14:41:09 +01003312 return false;
Eric Anholt673a3942008-07-30 12:06:12 -07003313
Imre Deak769ce462013-02-13 21:56:05 +02003314 /*
3315 * Stolen memory is always coherent with the GPU as it is explicitly
3316 * marked as wc by the system, or the system is cache-coherent.
3317 */
Chris Wilson6a2c4232014-11-04 04:51:40 -08003318 if (obj->stolen || obj->phys_handle)
Chris Wilson000433b2013-08-08 14:41:09 +01003319 return false;
Imre Deak769ce462013-02-13 21:56:05 +02003320
Chris Wilson9c23f7f2011-03-29 16:59:52 -07003321 /* If the GPU is snooping the contents of the CPU cache,
3322 * we do not need to manually clear the CPU cache lines. However,
3323 * the caches are only snooped when the render cache is
3324 * flushed/invalidated. As we always have to emit invalidations
3325 * and flushes when moving into and out of the RENDER domain, correct
3326 * snooping behaviour occurs naturally as the result of our domain
3327 * tracking.
3328 */
Chris Wilson0f719792015-01-13 13:32:52 +00003329 if (!force && cpu_cache_is_coherent(obj->base.dev, obj->cache_level)) {
3330 obj->cache_dirty = true;
Chris Wilson000433b2013-08-08 14:41:09 +01003331 return false;
Chris Wilson0f719792015-01-13 13:32:52 +00003332 }
Chris Wilson9c23f7f2011-03-29 16:59:52 -07003333
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003334 trace_i915_gem_object_clflush(obj);
Chris Wilson9da3da62012-06-01 15:20:22 +01003335 drm_clflush_sg(obj->pages);
Chris Wilson0f719792015-01-13 13:32:52 +00003336 obj->cache_dirty = false;
Chris Wilson000433b2013-08-08 14:41:09 +01003337
3338 return true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003339}
3340
3341/** Flushes the GTT write domain for the object if it's dirty. */
3342static void
Chris Wilson05394f32010-11-08 19:18:58 +00003343i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003344{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003345 uint32_t old_write_domain;
3346
Chris Wilson05394f32010-11-08 19:18:58 +00003347 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
Eric Anholte47c68e2008-11-14 13:35:19 -08003348 return;
3349
Chris Wilson63256ec2011-01-04 18:42:07 +00003350 /* No actual flushing is required for the GTT write domain. Writes
Eric Anholte47c68e2008-11-14 13:35:19 -08003351 * to it immediately go to main memory as far as we know, so there's
3352 * no chipset flush. It also doesn't land in render cache.
Chris Wilson63256ec2011-01-04 18:42:07 +00003353 *
3354 * However, we do have to enforce the order so that all writes through
3355 * the GTT land before any writes to the device, such as updates to
3356 * the GATT itself.
Eric Anholte47c68e2008-11-14 13:35:19 -08003357 */
Chris Wilson63256ec2011-01-04 18:42:07 +00003358 wmb();
3359
Chris Wilson05394f32010-11-08 19:18:58 +00003360 old_write_domain = obj->base.write_domain;
3361 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003362
Rodrigo Vivide152b62015-07-07 16:28:51 -07003363 intel_fb_obj_flush(obj, false, ORIGIN_GTT);
Daniel Vetterf99d7062014-06-19 16:01:59 +02003364
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003365 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00003366 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003367 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08003368}
3369
3370/** Flushes the CPU write domain for the object if it's dirty. */
3371static void
Daniel Vettere62b59e2015-01-21 14:53:48 +01003372i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003373{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003374 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08003375
Chris Wilson05394f32010-11-08 19:18:58 +00003376 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
Eric Anholte47c68e2008-11-14 13:35:19 -08003377 return;
3378
Daniel Vettere62b59e2015-01-21 14:53:48 +01003379 if (i915_gem_clflush_object(obj, obj->pin_display))
Chris Wilsonc0336662016-05-06 15:40:21 +01003380 i915_gem_chipset_flush(to_i915(obj->base.dev));
Chris Wilson000433b2013-08-08 14:41:09 +01003381
Chris Wilson05394f32010-11-08 19:18:58 +00003382 old_write_domain = obj->base.write_domain;
3383 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003384
Rodrigo Vivide152b62015-07-07 16:28:51 -07003385 intel_fb_obj_flush(obj, false, ORIGIN_CPU);
Daniel Vetterf99d7062014-06-19 16:01:59 +02003386
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003387 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00003388 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003389 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08003390}
3391
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003392/**
3393 * Moves a single object to the GTT read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003394 * @obj: object to act on
3395 * @write: ask for write access or read only
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003396 *
3397 * This function returns when the move is complete, including waiting on
3398 * flushes to occur.
3399 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003400int
Chris Wilson20217462010-11-23 15:26:33 +00003401i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003402{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003403 struct drm_device *dev = obj->base.dev;
3404 struct drm_i915_private *dev_priv = to_i915(dev);
3405 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003406 uint32_t old_write_domain, old_read_domains;
Chris Wilson43566de2015-01-02 16:29:29 +05303407 struct i915_vma *vma;
Eric Anholte47c68e2008-11-14 13:35:19 -08003408 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003409
Chris Wilson0201f1e2012-07-20 12:41:01 +01003410 ret = i915_gem_object_wait_rendering(obj, !write);
Chris Wilson88241782011-01-07 17:09:48 +00003411 if (ret)
3412 return ret;
3413
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003414 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
3415 return 0;
3416
Chris Wilson43566de2015-01-02 16:29:29 +05303417 /* Flush and acquire obj->pages so that we are coherent through
3418 * direct access in memory with previous cached writes through
3419 * shmemfs and that our cache domain tracking remains valid.
3420 * For example, if the obj->filp was moved to swap without us
3421 * being notified and releasing the pages, we would mistakenly
3422 * continue to assume that the obj remained out of the CPU cached
3423 * domain.
3424 */
3425 ret = i915_gem_object_get_pages(obj);
3426 if (ret)
3427 return ret;
3428
Daniel Vettere62b59e2015-01-21 14:53:48 +01003429 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003430
Chris Wilsond0a57782012-10-09 19:24:37 +01003431 /* Serialise direct access to this object with the barriers for
3432 * coherent writes from the GPU, by effectively invalidating the
3433 * GTT domain upon first access.
3434 */
3435 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
3436 mb();
3437
Chris Wilson05394f32010-11-08 19:18:58 +00003438 old_write_domain = obj->base.write_domain;
3439 old_read_domains = obj->base.read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003440
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003441 /* It should now be out of any other write domains, and we can update
3442 * the domain values for our changes.
3443 */
Chris Wilson05394f32010-11-08 19:18:58 +00003444 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3445 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003446 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003447 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
3448 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
3449 obj->dirty = 1;
Eric Anholte47c68e2008-11-14 13:35:19 -08003450 }
3451
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003452 trace_i915_gem_object_change_domain(obj,
3453 old_read_domains,
3454 old_write_domain);
3455
Chris Wilson8325a092012-04-24 15:52:35 +01003456 /* And bump the LRU for this access */
Chris Wilson43566de2015-01-02 16:29:29 +05303457 vma = i915_gem_obj_to_ggtt(obj);
3458 if (vma && drm_mm_node_allocated(&vma->node) && !obj->active)
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003459 list_move_tail(&vma->vm_link,
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003460 &ggtt->base.inactive_list);
Chris Wilson8325a092012-04-24 15:52:35 +01003461
Eric Anholte47c68e2008-11-14 13:35:19 -08003462 return 0;
3463}
3464
Chris Wilsonef55f922015-10-09 14:11:27 +01003465/**
3466 * Changes the cache-level of an object across all VMA.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003467 * @obj: object to act on
3468 * @cache_level: new cache level to set for the object
Chris Wilsonef55f922015-10-09 14:11:27 +01003469 *
3470 * After this function returns, the object will be in the new cache-level
3471 * across all GTT and the contents of the backing storage will be coherent,
3472 * with respect to the new cache-level. In order to keep the backing storage
3473 * coherent for all users, we only allow a single cache level to be set
3474 * globally on the object and prevent it from being changed whilst the
3475 * hardware is reading from the object. That is if the object is currently
3476 * on the scanout it will be set to uncached (or equivalent display
3477 * cache coherency) and all non-MOCS GPU access will also be uncached so
3478 * that all direct access to the scanout remains coherent.
3479 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003480int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3481 enum i915_cache_level cache_level)
3482{
Chris Wilsonaa653a62016-08-04 07:52:27 +01003483 struct i915_vma *vma;
Ville Syrjäläed75a552015-08-11 19:47:10 +03003484 int ret = 0;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003485
3486 if (obj->cache_level == cache_level)
Ville Syrjäläed75a552015-08-11 19:47:10 +03003487 goto out;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003488
Chris Wilsonef55f922015-10-09 14:11:27 +01003489 /* Inspect the list of currently bound VMA and unbind any that would
3490 * be invalid given the new cache-level. This is principally to
3491 * catch the issue of the CS prefetch crossing page boundaries and
3492 * reading an invalid PTE on older architectures.
3493 */
Chris Wilsonaa653a62016-08-04 07:52:27 +01003494restart:
3495 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003496 if (!drm_mm_node_allocated(&vma->node))
3497 continue;
3498
3499 if (vma->pin_count) {
3500 DRM_DEBUG("can not change the cache level of pinned objects\n");
3501 return -EBUSY;
3502 }
3503
Chris Wilsonaa653a62016-08-04 07:52:27 +01003504 if (i915_gem_valid_gtt_space(vma, cache_level))
3505 continue;
3506
3507 ret = i915_vma_unbind(vma);
3508 if (ret)
3509 return ret;
3510
3511 /* As unbinding may affect other elements in the
3512 * obj->vma_list (due to side-effects from retiring
3513 * an active vma), play safe and restart the iterator.
3514 */
3515 goto restart;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003516 }
3517
Chris Wilsonef55f922015-10-09 14:11:27 +01003518 /* We can reuse the existing drm_mm nodes but need to change the
3519 * cache-level on the PTE. We could simply unbind them all and
3520 * rebind with the correct cache-level on next use. However since
3521 * we already have a valid slot, dma mapping, pages etc, we may as
3522 * rewrite the PTE in the belief that doing so tramples upon less
3523 * state and so involves less work.
3524 */
Chris Wilson15717de2016-08-04 07:52:26 +01003525 if (obj->bind_count) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003526 /* Before we change the PTE, the GPU must not be accessing it.
3527 * If we wait upon the object, we know that all the bound
3528 * VMA are no longer active.
3529 */
Chris Wilson2e2f3512015-04-27 13:41:14 +01003530 ret = i915_gem_object_wait_rendering(obj, false);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003531 if (ret)
3532 return ret;
3533
Chris Wilsonaa653a62016-08-04 07:52:27 +01003534 if (!HAS_LLC(obj->base.dev) && cache_level != I915_CACHE_NONE) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003535 /* Access to snoopable pages through the GTT is
3536 * incoherent and on some machines causes a hard
3537 * lockup. Relinquish the CPU mmaping to force
3538 * userspace to refault in the pages and we can
3539 * then double check if the GTT mapping is still
3540 * valid for that pointer access.
3541 */
3542 i915_gem_release_mmap(obj);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003543
Chris Wilsonef55f922015-10-09 14:11:27 +01003544 /* As we no longer need a fence for GTT access,
3545 * we can relinquish it now (and so prevent having
3546 * to steal a fence from someone else on the next
3547 * fence request). Note GPU activity would have
3548 * dropped the fence as all snoopable access is
3549 * supposed to be linear.
3550 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003551 ret = i915_gem_object_put_fence(obj);
3552 if (ret)
3553 return ret;
Chris Wilsonef55f922015-10-09 14:11:27 +01003554 } else {
3555 /* We either have incoherent backing store and
3556 * so no GTT access or the architecture is fully
3557 * coherent. In such cases, existing GTT mmaps
3558 * ignore the cache bit in the PTE and we can
3559 * rewrite it without confusing the GPU or having
3560 * to force userspace to fault back in its mmaps.
3561 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003562 }
3563
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003564 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003565 if (!drm_mm_node_allocated(&vma->node))
3566 continue;
3567
3568 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
3569 if (ret)
3570 return ret;
3571 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01003572 }
3573
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003574 list_for_each_entry(vma, &obj->vma_list, obj_link)
Chris Wilson2c225692013-08-09 12:26:45 +01003575 vma->node.color = cache_level;
3576 obj->cache_level = cache_level;
3577
Ville Syrjäläed75a552015-08-11 19:47:10 +03003578out:
Chris Wilsonef55f922015-10-09 14:11:27 +01003579 /* Flush the dirty CPU caches to the backing storage so that the
3580 * object is now coherent at its new cache level (with respect
3581 * to the access domain).
3582 */
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05303583 if (obj->cache_dirty && cpu_write_needs_clflush(obj)) {
Chris Wilson0f719792015-01-13 13:32:52 +00003584 if (i915_gem_clflush_object(obj, true))
Chris Wilsonc0336662016-05-06 15:40:21 +01003585 i915_gem_chipset_flush(to_i915(obj->base.dev));
Chris Wilsone4ffd172011-04-04 09:44:39 +01003586 }
3587
Chris Wilsone4ffd172011-04-04 09:44:39 +01003588 return 0;
3589}
3590
Ben Widawsky199adf42012-09-21 17:01:20 -07003591int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
3592 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003593{
Ben Widawsky199adf42012-09-21 17:01:20 -07003594 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003595 struct drm_i915_gem_object *obj;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003596
Chris Wilson03ac0642016-07-20 13:31:51 +01003597 obj = i915_gem_object_lookup(file, args->handle);
3598 if (!obj)
Chris Wilson432be692015-05-07 12:14:55 +01003599 return -ENOENT;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003600
Chris Wilson651d7942013-08-08 14:41:10 +01003601 switch (obj->cache_level) {
3602 case I915_CACHE_LLC:
3603 case I915_CACHE_L3_LLC:
3604 args->caching = I915_CACHING_CACHED;
3605 break;
3606
Chris Wilson4257d3b2013-08-08 14:41:11 +01003607 case I915_CACHE_WT:
3608 args->caching = I915_CACHING_DISPLAY;
3609 break;
3610
Chris Wilson651d7942013-08-08 14:41:10 +01003611 default:
3612 args->caching = I915_CACHING_NONE;
3613 break;
3614 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01003615
Chris Wilson34911fd2016-07-20 13:31:54 +01003616 i915_gem_object_put_unlocked(obj);
Chris Wilson432be692015-05-07 12:14:55 +01003617 return 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003618}
3619
Ben Widawsky199adf42012-09-21 17:01:20 -07003620int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
3621 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003622{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003623 struct drm_i915_private *dev_priv = to_i915(dev);
Ben Widawsky199adf42012-09-21 17:01:20 -07003624 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003625 struct drm_i915_gem_object *obj;
3626 enum i915_cache_level level;
3627 int ret;
3628
Ben Widawsky199adf42012-09-21 17:01:20 -07003629 switch (args->caching) {
3630 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01003631 level = I915_CACHE_NONE;
3632 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07003633 case I915_CACHING_CACHED:
Imre Deake5756c12015-08-14 18:43:30 +03003634 /*
3635 * Due to a HW issue on BXT A stepping, GPU stores via a
3636 * snooped mapping may leave stale data in a corresponding CPU
3637 * cacheline, whereas normally such cachelines would get
3638 * invalidated.
3639 */
Tvrtko Ursulinca377802016-03-02 12:10:31 +00003640 if (!HAS_LLC(dev) && !HAS_SNOOP(dev))
Imre Deake5756c12015-08-14 18:43:30 +03003641 return -ENODEV;
3642
Chris Wilsone6994ae2012-07-10 10:27:08 +01003643 level = I915_CACHE_LLC;
3644 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003645 case I915_CACHING_DISPLAY:
3646 level = HAS_WT(dev) ? I915_CACHE_WT : I915_CACHE_NONE;
3647 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003648 default:
3649 return -EINVAL;
3650 }
3651
Imre Deakfd0fe6a2015-11-04 21:25:32 +02003652 intel_runtime_pm_get(dev_priv);
3653
Ben Widawsky3bc29132012-09-26 16:15:20 -07003654 ret = i915_mutex_lock_interruptible(dev);
3655 if (ret)
Imre Deakfd0fe6a2015-11-04 21:25:32 +02003656 goto rpm_put;
Ben Widawsky3bc29132012-09-26 16:15:20 -07003657
Chris Wilson03ac0642016-07-20 13:31:51 +01003658 obj = i915_gem_object_lookup(file, args->handle);
3659 if (!obj) {
Chris Wilsone6994ae2012-07-10 10:27:08 +01003660 ret = -ENOENT;
3661 goto unlock;
3662 }
3663
3664 ret = i915_gem_object_set_cache_level(obj, level);
3665
Chris Wilsonf8c417c2016-07-20 13:31:53 +01003666 i915_gem_object_put(obj);
Chris Wilsone6994ae2012-07-10 10:27:08 +01003667unlock:
3668 mutex_unlock(&dev->struct_mutex);
Imre Deakfd0fe6a2015-11-04 21:25:32 +02003669rpm_put:
3670 intel_runtime_pm_put(dev_priv);
3671
Chris Wilsone6994ae2012-07-10 10:27:08 +01003672 return ret;
3673}
3674
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003675/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003676 * Prepare buffer for display plane (scanout, cursors, etc).
3677 * Can be called from an uninterruptible phase (modesetting) and allows
3678 * any flushes to be pipelined (for pageflips).
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003679 */
3680int
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003681i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3682 u32 alignment,
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003683 const struct i915_ggtt_view *view)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003684{
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003685 u32 old_read_domains, old_write_domain;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003686 int ret;
3687
Chris Wilsoncc98b412013-08-09 12:25:09 +01003688 /* Mark the pin_display early so that we account for the
3689 * display coherency whilst setting up the cache domains.
3690 */
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003691 obj->pin_display++;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003692
Eric Anholta7ef0642011-03-29 16:59:54 -07003693 /* The display engine is not coherent with the LLC cache on gen6. As
3694 * a result, we make sure that the pinning that is about to occur is
3695 * done with uncached PTEs. This is lowest common denominator for all
3696 * chipsets.
3697 *
3698 * However for gen6+, we could do better by using the GFDT bit instead
3699 * of uncaching, which would allow us to flush all the LLC-cached data
3700 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3701 */
Chris Wilson651d7942013-08-08 14:41:10 +01003702 ret = i915_gem_object_set_cache_level(obj,
3703 HAS_WT(obj->base.dev) ? I915_CACHE_WT : I915_CACHE_NONE);
Eric Anholta7ef0642011-03-29 16:59:54 -07003704 if (ret)
Chris Wilsoncc98b412013-08-09 12:25:09 +01003705 goto err_unpin_display;
Eric Anholta7ef0642011-03-29 16:59:54 -07003706
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003707 /* As the user may map the buffer once pinned in the display plane
3708 * (e.g. libkms for the bootup splash), we have to ensure that we
3709 * always use map_and_fenceable for all scanout buffers.
3710 */
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003711 ret = i915_gem_object_ggtt_pin(obj, view, alignment,
3712 view->type == I915_GGTT_VIEW_NORMAL ?
3713 PIN_MAPPABLE : 0);
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003714 if (ret)
Chris Wilsoncc98b412013-08-09 12:25:09 +01003715 goto err_unpin_display;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003716
Daniel Vettere62b59e2015-01-21 14:53:48 +01003717 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01003718
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003719 old_write_domain = obj->base.write_domain;
Chris Wilson05394f32010-11-08 19:18:58 +00003720 old_read_domains = obj->base.read_domains;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003721
3722 /* It should now be out of any other write domains, and we can update
3723 * the domain values for our changes.
3724 */
Chris Wilsone5f1d962012-07-20 12:41:00 +01003725 obj->base.write_domain = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00003726 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003727
3728 trace_i915_gem_object_change_domain(obj,
3729 old_read_domains,
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003730 old_write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003731
3732 return 0;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003733
3734err_unpin_display:
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003735 obj->pin_display--;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003736 return ret;
3737}
3738
3739void
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003740i915_gem_object_unpin_from_display_plane(struct drm_i915_gem_object *obj,
3741 const struct i915_ggtt_view *view)
Chris Wilsoncc98b412013-08-09 12:25:09 +01003742{
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003743 if (WARN_ON(obj->pin_display == 0))
3744 return;
3745
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003746 i915_gem_object_ggtt_unpin_view(obj, view);
3747
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003748 obj->pin_display--;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003749}
3750
Eric Anholte47c68e2008-11-14 13:35:19 -08003751/**
3752 * Moves a single object to the CPU read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003753 * @obj: object to act on
3754 * @write: requesting write or read-only access
Eric Anholte47c68e2008-11-14 13:35:19 -08003755 *
3756 * This function returns when the move is complete, including waiting on
3757 * flushes to occur.
3758 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02003759int
Chris Wilson919926a2010-11-12 13:42:53 +00003760i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08003761{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003762 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003763 int ret;
3764
Chris Wilson0201f1e2012-07-20 12:41:01 +01003765 ret = i915_gem_object_wait_rendering(obj, !write);
Chris Wilson88241782011-01-07 17:09:48 +00003766 if (ret)
3767 return ret;
3768
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003769 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
3770 return 0;
3771
Eric Anholte47c68e2008-11-14 13:35:19 -08003772 i915_gem_object_flush_gtt_write_domain(obj);
3773
Chris Wilson05394f32010-11-08 19:18:58 +00003774 old_write_domain = obj->base.write_domain;
3775 old_read_domains = obj->base.read_domains;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003776
Eric Anholte47c68e2008-11-14 13:35:19 -08003777 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00003778 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson2c225692013-08-09 12:26:45 +01003779 i915_gem_clflush_object(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003780
Chris Wilson05394f32010-11-08 19:18:58 +00003781 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003782 }
3783
3784 /* It should now be out of any other write domains, and we can update
3785 * the domain values for our changes.
3786 */
Chris Wilson05394f32010-11-08 19:18:58 +00003787 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003788
3789 /* If we're writing through the CPU, then the GPU read domains will
3790 * need to be invalidated at next use.
3791 */
3792 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003793 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3794 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003795 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003796
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003797 trace_i915_gem_object_change_domain(obj,
3798 old_read_domains,
3799 old_write_domain);
3800
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003801 return 0;
3802}
3803
Eric Anholt673a3942008-07-30 12:06:12 -07003804/* Throttle our rendering by waiting until the ring has completed our requests
3805 * emitted over 20 msec ago.
3806 *
Eric Anholtb9624422009-06-03 07:27:35 +00003807 * Note that if we were to use the current jiffies each time around the loop,
3808 * we wouldn't escape the function with any frames outstanding if the time to
3809 * render a frame was over 20ms.
3810 *
Eric Anholt673a3942008-07-30 12:06:12 -07003811 * This should get us reasonable parallelism between CPU and GPU but also
3812 * relatively low latency when blocking on a particular request to finish.
3813 */
3814static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003815i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003816{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003817 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003818 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsond0bc54f2015-05-21 21:01:48 +01003819 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
John Harrison54fb2412014-11-24 18:49:27 +00003820 struct drm_i915_gem_request *request, *target = NULL;
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003821 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003822
Daniel Vetter308887a2012-11-14 17:14:06 +01003823 ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
3824 if (ret)
3825 return ret;
3826
Chris Wilsonf4457ae2016-04-13 17:35:08 +01003827 /* ABI: return -EIO if already wedged */
3828 if (i915_terminally_wedged(&dev_priv->gpu_error))
3829 return -EIO;
Chris Wilsone110e8d2011-01-26 15:39:14 +00003830
Chris Wilson1c255952010-09-26 11:03:27 +01003831 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003832 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003833 if (time_after_eq(request->emitted_jiffies, recent_enough))
3834 break;
3835
John Harrisonfcfa423c2015-05-29 17:44:12 +01003836 /*
3837 * Note that the request might not have been submitted yet.
3838 * In which case emitted_jiffies will be zero.
3839 */
3840 if (!request->emitted_jiffies)
3841 continue;
3842
John Harrison54fb2412014-11-24 18:49:27 +00003843 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00003844 }
John Harrisonff865882014-11-24 18:49:28 +00003845 if (target)
Chris Wilsone8a261e2016-07-20 13:31:49 +01003846 i915_gem_request_get(target);
Chris Wilson1c255952010-09-26 11:03:27 +01003847 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003848
John Harrison54fb2412014-11-24 18:49:27 +00003849 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003850 return 0;
3851
Chris Wilson299259a2016-04-13 17:35:06 +01003852 ret = __i915_wait_request(target, true, NULL, NULL);
Chris Wilsone8a261e2016-07-20 13:31:49 +01003853 i915_gem_request_put(target);
John Harrisonff865882014-11-24 18:49:28 +00003854
Eric Anholt673a3942008-07-30 12:06:12 -07003855 return ret;
3856}
3857
Chris Wilsond23db882014-05-23 08:48:08 +02003858static bool
3859i915_vma_misplaced(struct i915_vma *vma, uint32_t alignment, uint64_t flags)
3860{
3861 struct drm_i915_gem_object *obj = vma->obj;
3862
3863 if (alignment &&
3864 vma->node.start & (alignment - 1))
3865 return true;
3866
3867 if (flags & PIN_MAPPABLE && !obj->map_and_fenceable)
3868 return true;
3869
3870 if (flags & PIN_OFFSET_BIAS &&
3871 vma->node.start < (flags & PIN_OFFSET_MASK))
3872 return true;
3873
Chris Wilson506a8e82015-12-08 11:55:07 +00003874 if (flags & PIN_OFFSET_FIXED &&
3875 vma->node.start != (flags & PIN_OFFSET_MASK))
3876 return true;
3877
Chris Wilsond23db882014-05-23 08:48:08 +02003878 return false;
3879}
3880
Chris Wilsond0710ab2015-11-20 14:16:39 +00003881void __i915_vma_set_map_and_fenceable(struct i915_vma *vma)
3882{
3883 struct drm_i915_gem_object *obj = vma->obj;
3884 bool mappable, fenceable;
3885 u32 fence_size, fence_alignment;
3886
3887 fence_size = i915_gem_get_gtt_size(obj->base.dev,
3888 obj->base.size,
3889 obj->tiling_mode);
3890 fence_alignment = i915_gem_get_gtt_alignment(obj->base.dev,
3891 obj->base.size,
3892 obj->tiling_mode,
3893 true);
3894
3895 fenceable = (vma->node.size == fence_size &&
3896 (vma->node.start & (fence_alignment - 1)) == 0);
3897
3898 mappable = (vma->node.start + fence_size <=
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003899 to_i915(obj->base.dev)->ggtt.mappable_end);
Chris Wilsond0710ab2015-11-20 14:16:39 +00003900
3901 obj->map_and_fenceable = mappable && fenceable;
3902}
3903
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003904static int
3905i915_gem_object_do_pin(struct drm_i915_gem_object *obj,
3906 struct i915_address_space *vm,
3907 const struct i915_ggtt_view *ggtt_view,
3908 uint32_t alignment,
3909 uint64_t flags)
Eric Anholt673a3942008-07-30 12:06:12 -07003910{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003911 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003912 struct i915_vma *vma;
Chris Wilsonef79e172014-10-31 13:53:52 +00003913 unsigned bound;
Eric Anholt673a3942008-07-30 12:06:12 -07003914 int ret;
3915
Ben Widawsky6e7186a2014-05-06 22:21:36 -07003916 if (WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base))
3917 return -ENODEV;
3918
Daniel Vetterbf3d1492014-02-14 14:01:12 +01003919 if (WARN_ON(flags & (PIN_GLOBAL | PIN_MAPPABLE) && !i915_is_ggtt(vm)))
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003920 return -EINVAL;
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003921
Chris Wilsonc826c442014-10-31 13:53:53 +00003922 if (WARN_ON((flags & (PIN_MAPPABLE | PIN_GLOBAL)) == PIN_MAPPABLE))
3923 return -EINVAL;
3924
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003925 if (WARN_ON(i915_is_ggtt(vm) != !!ggtt_view))
3926 return -EINVAL;
3927
3928 vma = ggtt_view ? i915_gem_obj_to_ggtt_view(obj, ggtt_view) :
3929 i915_gem_obj_to_vma(obj, vm);
3930
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003931 if (vma) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08003932 if (WARN_ON(vma->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT))
3933 return -EBUSY;
3934
Chris Wilsond23db882014-05-23 08:48:08 +02003935 if (i915_vma_misplaced(vma, alignment, flags)) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08003936 WARN(vma->pin_count,
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003937 "bo is already pinned in %s with incorrect alignment:"
Michel Thierry088e0df2015-08-07 17:40:17 +01003938 " offset=%08x %08x, req.alignment=%x, req.map_and_fenceable=%d,"
Daniel Vetter75e9e912010-11-04 17:11:09 +01003939 " obj->map_and_fenceable=%d\n",
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003940 ggtt_view ? "ggtt" : "ppgtt",
Michel Thierry088e0df2015-08-07 17:40:17 +01003941 upper_32_bits(vma->node.start),
3942 lower_32_bits(vma->node.start),
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003943 alignment,
Chris Wilsond23db882014-05-23 08:48:08 +02003944 !!(flags & PIN_MAPPABLE),
Chris Wilson05394f32010-11-08 19:18:58 +00003945 obj->map_and_fenceable);
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003946 ret = i915_vma_unbind(vma);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003947 if (ret)
3948 return ret;
Daniel Vetter8ea99c92014-02-14 14:01:21 +01003949
3950 vma = NULL;
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003951 }
3952 }
3953
Chris Wilsonef79e172014-10-31 13:53:52 +00003954 bound = vma ? vma->bound : 0;
Daniel Vetter8ea99c92014-02-14 14:01:21 +01003955 if (vma == NULL || !drm_mm_node_allocated(&vma->node)) {
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003956 vma = i915_gem_object_bind_to_vm(obj, vm, ggtt_view, alignment,
3957 flags);
Daniel Vetter262de142014-02-14 14:01:20 +01003958 if (IS_ERR(vma))
3959 return PTR_ERR(vma);
Daniel Vetter08755462015-04-20 09:04:05 -07003960 } else {
3961 ret = i915_vma_bind(vma, obj->cache_level, flags);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003962 if (ret)
3963 return ret;
3964 }
Daniel Vetter74898d72012-02-15 23:50:22 +01003965
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003966 if (ggtt_view && ggtt_view->type == I915_GGTT_VIEW_NORMAL &&
3967 (bound ^ vma->bound) & GLOBAL_BIND) {
Chris Wilsond0710ab2015-11-20 14:16:39 +00003968 __i915_vma_set_map_and_fenceable(vma);
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003969 WARN_ON(flags & PIN_MAPPABLE && !obj->map_and_fenceable);
3970 }
Chris Wilsonef79e172014-10-31 13:53:52 +00003971
Daniel Vetter8ea99c92014-02-14 14:01:21 +01003972 vma->pin_count++;
Eric Anholt673a3942008-07-30 12:06:12 -07003973 return 0;
3974}
3975
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003976int
3977i915_gem_object_pin(struct drm_i915_gem_object *obj,
3978 struct i915_address_space *vm,
3979 uint32_t alignment,
3980 uint64_t flags)
3981{
3982 return i915_gem_object_do_pin(obj, vm,
3983 i915_is_ggtt(vm) ? &i915_ggtt_view_normal : NULL,
3984 alignment, flags);
3985}
3986
3987int
3988i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
3989 const struct i915_ggtt_view *view,
3990 uint32_t alignment,
3991 uint64_t flags)
3992{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003993 struct drm_device *dev = obj->base.dev;
3994 struct drm_i915_private *dev_priv = to_i915(dev);
3995 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3996
Matthew Auldade7daa2016-03-24 15:54:20 +00003997 BUG_ON(!view);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003998
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003999 return i915_gem_object_do_pin(obj, &ggtt->base, view,
Tvrtko Ursulin6fafab72015-03-17 15:36:51 +00004000 alignment, flags | PIN_GLOBAL);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004001}
4002
Eric Anholt673a3942008-07-30 12:06:12 -07004003void
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004004i915_gem_object_ggtt_unpin_view(struct drm_i915_gem_object *obj,
4005 const struct i915_ggtt_view *view)
Eric Anholt673a3942008-07-30 12:06:12 -07004006{
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004007 struct i915_vma *vma = i915_gem_obj_to_ggtt_view(obj, view);
Eric Anholt673a3942008-07-30 12:06:12 -07004008
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004009 WARN_ON(vma->pin_count == 0);
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02004010 WARN_ON(!i915_gem_obj_ggtt_bound_view(obj, view));
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004011
Chris Wilson30154652015-04-07 17:28:24 +01004012 --vma->pin_count;
Eric Anholt673a3942008-07-30 12:06:12 -07004013}
4014
4015int
Eric Anholt673a3942008-07-30 12:06:12 -07004016i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004017 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004018{
4019 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004020 struct drm_i915_gem_object *obj;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004021 int ret;
4022
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004023 ret = i915_mutex_lock_interruptible(dev);
4024 if (ret)
4025 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004026
Chris Wilson03ac0642016-07-20 13:31:51 +01004027 obj = i915_gem_object_lookup(file, args->handle);
4028 if (!obj) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004029 ret = -ENOENT;
4030 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004031 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08004032
Chris Wilson0be555b2010-08-04 15:36:30 +01004033 /* Count all active objects as busy, even if they are currently not used
4034 * by the gpu. Users of this interface expect objects to eventually
4035 * become non-busy without any further actions, therefore emit any
4036 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004037 */
Daniel Vetter30dfebf2012-06-01 15:21:23 +02004038 ret = i915_gem_object_flush_active(obj);
Chris Wilsonb4716182015-04-27 13:41:17 +01004039 if (ret)
4040 goto unref;
Daniel Vetter30dfebf2012-06-01 15:21:23 +02004041
Chris Wilson426960b2016-01-15 16:51:46 +00004042 args->busy = 0;
4043 if (obj->active) {
Chris Wilson27c01aa2016-08-04 07:52:30 +01004044 struct drm_i915_gem_request *req;
Chris Wilson426960b2016-01-15 16:51:46 +00004045 int i;
4046
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00004047 for (i = 0; i < I915_NUM_ENGINES; i++) {
Chris Wilsond72d9082016-08-04 07:52:31 +01004048 req = i915_gem_active_peek(&obj->last_read[i],
4049 &obj->base.dev->struct_mutex);
Chris Wilson426960b2016-01-15 16:51:46 +00004050 if (req)
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00004051 args->busy |= 1 << (16 + req->engine->exec_id);
Chris Wilson426960b2016-01-15 16:51:46 +00004052 }
Chris Wilsond72d9082016-08-04 07:52:31 +01004053 req = i915_gem_active_peek(&obj->last_write,
4054 &obj->base.dev->struct_mutex);
Chris Wilson27c01aa2016-08-04 07:52:30 +01004055 if (req)
4056 args->busy |= req->engine->exec_id;
Chris Wilson426960b2016-01-15 16:51:46 +00004057 }
Eric Anholt673a3942008-07-30 12:06:12 -07004058
Chris Wilsonb4716182015-04-27 13:41:17 +01004059unref:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004060 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004061unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004062 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004063 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004064}
4065
4066int
4067i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4068 struct drm_file *file_priv)
4069{
Akshay Joshi0206e352011-08-16 15:34:10 -04004070 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004071}
4072
Chris Wilson3ef94da2009-09-14 16:50:29 +01004073int
4074i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4075 struct drm_file *file_priv)
4076{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004077 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004078 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004079 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004080 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004081
4082 switch (args->madv) {
4083 case I915_MADV_DONTNEED:
4084 case I915_MADV_WILLNEED:
4085 break;
4086 default:
4087 return -EINVAL;
4088 }
4089
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004090 ret = i915_mutex_lock_interruptible(dev);
4091 if (ret)
4092 return ret;
4093
Chris Wilson03ac0642016-07-20 13:31:51 +01004094 obj = i915_gem_object_lookup(file_priv, args->handle);
4095 if (!obj) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004096 ret = -ENOENT;
4097 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004098 }
Chris Wilson3ef94da2009-09-14 16:50:29 +01004099
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004100 if (i915_gem_obj_is_pinned(obj)) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004101 ret = -EINVAL;
4102 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004103 }
4104
Daniel Vetter656bfa32014-11-20 09:26:30 +01004105 if (obj->pages &&
4106 obj->tiling_mode != I915_TILING_NONE &&
4107 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
4108 if (obj->madv == I915_MADV_WILLNEED)
4109 i915_gem_object_unpin_pages(obj);
4110 if (args->madv == I915_MADV_WILLNEED)
4111 i915_gem_object_pin_pages(obj);
4112 }
4113
Chris Wilson05394f32010-11-08 19:18:58 +00004114 if (obj->madv != __I915_MADV_PURGED)
4115 obj->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004116
Chris Wilson6c085a72012-08-20 11:40:46 +02004117 /* if the object is no longer attached, discard its backing storage */
Daniel Vetterbe6a0372015-03-18 10:46:04 +01004118 if (obj->madv == I915_MADV_DONTNEED && obj->pages == NULL)
Chris Wilson2d7ef392009-09-20 23:13:10 +01004119 i915_gem_object_truncate(obj);
4120
Chris Wilson05394f32010-11-08 19:18:58 +00004121 args->retained = obj->madv != __I915_MADV_PURGED;
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004122
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004123out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004124 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004125unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01004126 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004127 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004128}
4129
Chris Wilson37e680a2012-06-07 15:38:42 +01004130void i915_gem_object_init(struct drm_i915_gem_object *obj,
4131 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01004132{
Chris Wilsonb4716182015-04-27 13:41:17 +01004133 int i;
4134
Ben Widawsky35c20a62013-05-31 11:28:48 -07004135 INIT_LIST_HEAD(&obj->global_list);
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00004136 for (i = 0; i < I915_NUM_ENGINES; i++)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004137 INIT_LIST_HEAD(&obj->engine_list[i]);
Ben Widawskyb25cb2f2013-08-14 11:38:33 +02004138 INIT_LIST_HEAD(&obj->obj_exec_link);
Ben Widawsky2f633152013-07-17 12:19:03 -07004139 INIT_LIST_HEAD(&obj->vma_list);
Chris Wilson8d9d5742015-04-07 16:20:38 +01004140 INIT_LIST_HEAD(&obj->batch_pool_link);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004141
Chris Wilson37e680a2012-06-07 15:38:42 +01004142 obj->ops = ops;
4143
Chris Wilson0327d6b2012-08-11 15:41:06 +01004144 obj->fence_reg = I915_FENCE_REG_NONE;
4145 obj->madv = I915_MADV_WILLNEED;
Chris Wilson0327d6b2012-08-11 15:41:06 +01004146
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004147 i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004148}
4149
Chris Wilson37e680a2012-06-07 15:38:42 +01004150static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
Chris Wilsonde472662016-01-22 18:32:31 +00004151 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE,
Chris Wilson37e680a2012-06-07 15:38:42 +01004152 .get_pages = i915_gem_object_get_pages_gtt,
4153 .put_pages = i915_gem_object_put_pages_gtt,
4154};
4155
Dave Gordond37cd8a2016-04-22 19:14:32 +01004156struct drm_i915_gem_object *i915_gem_object_create(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00004157 size_t size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004158{
Daniel Vetterc397b902010-04-09 19:05:07 +00004159 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004160 struct address_space *mapping;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004161 gfp_t mask;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004162 int ret;
Daniel Vetterc397b902010-04-09 19:05:07 +00004163
Chris Wilson42dcedd2012-11-15 11:32:30 +00004164 obj = i915_gem_object_alloc(dev);
Daniel Vetterc397b902010-04-09 19:05:07 +00004165 if (obj == NULL)
Chris Wilsonfe3db792016-04-25 13:32:13 +01004166 return ERR_PTR(-ENOMEM);
Daniel Vetterc397b902010-04-09 19:05:07 +00004167
Chris Wilsonfe3db792016-04-25 13:32:13 +01004168 ret = drm_gem_object_init(dev, &obj->base, size);
4169 if (ret)
4170 goto fail;
Daniel Vetterc397b902010-04-09 19:05:07 +00004171
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004172 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
4173 if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) {
4174 /* 965gm cannot relocate objects above 4GiB. */
4175 mask &= ~__GFP_HIGHMEM;
4176 mask |= __GFP_DMA32;
4177 }
4178
Al Viro496ad9a2013-01-23 17:07:38 -05004179 mapping = file_inode(obj->base.filp)->i_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004180 mapping_set_gfp_mask(mapping, mask);
Hugh Dickins5949eac2011-06-27 16:18:18 -07004181
Chris Wilson37e680a2012-06-07 15:38:42 +01004182 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004183
Daniel Vetterc397b902010-04-09 19:05:07 +00004184 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4185 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4186
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004187 if (HAS_LLC(dev)) {
4188 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004189 * cache) for about a 10% performance improvement
4190 * compared to uncached. Graphics requests other than
4191 * display scanout are coherent with the CPU in
4192 * accessing this cache. This means in this mode we
4193 * don't need to clflush on the CPU side, and on the
4194 * GPU side we only need to flush internal caches to
4195 * get data visible to the CPU.
4196 *
4197 * However, we maintain the display planes as UC, and so
4198 * need to rebind when first used as such.
4199 */
4200 obj->cache_level = I915_CACHE_LLC;
4201 } else
4202 obj->cache_level = I915_CACHE_NONE;
4203
Daniel Vetterd861e332013-07-24 23:25:03 +02004204 trace_i915_gem_object_create(obj);
4205
Chris Wilson05394f32010-11-08 19:18:58 +00004206 return obj;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004207
4208fail:
4209 i915_gem_object_free(obj);
4210
4211 return ERR_PTR(ret);
Daniel Vetterac52bc52010-04-09 19:05:06 +00004212}
4213
Chris Wilson340fbd82014-05-22 09:16:52 +01004214static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4215{
4216 /* If we are the last user of the backing storage (be it shmemfs
4217 * pages or stolen etc), we know that the pages are going to be
4218 * immediately released. In this case, we can then skip copying
4219 * back the contents from the GPU.
4220 */
4221
4222 if (obj->madv != I915_MADV_WILLNEED)
4223 return false;
4224
4225 if (obj->base.filp == NULL)
4226 return true;
4227
4228 /* At first glance, this looks racy, but then again so would be
4229 * userspace racing mmap against close. However, the first external
4230 * reference to the filp can only be obtained through the
4231 * i915_gem_mmap_ioctl() which safeguards us against the user
4232 * acquiring such a reference whilst we are in the middle of
4233 * freeing the object.
4234 */
4235 return atomic_long_read(&obj->base.filp->f_count) == 1;
4236}
4237
Chris Wilson1488fc02012-04-24 15:47:31 +01004238void i915_gem_free_object(struct drm_gem_object *gem_obj)
Chris Wilsonbe726152010-07-23 23:18:50 +01004239{
Chris Wilson1488fc02012-04-24 15:47:31 +01004240 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00004241 struct drm_device *dev = obj->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004242 struct drm_i915_private *dev_priv = to_i915(dev);
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004243 struct i915_vma *vma, *next;
Chris Wilsonbe726152010-07-23 23:18:50 +01004244
Paulo Zanonif65c9162013-11-27 18:20:34 -02004245 intel_runtime_pm_get(dev_priv);
4246
Chris Wilson26e12f82011-03-20 11:20:19 +00004247 trace_i915_gem_object_destroy(obj);
4248
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004249 list_for_each_entry_safe(vma, next, &obj->vma_list, obj_link) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004250 int ret;
4251
4252 vma->pin_count = 0;
Chris Wilsonc13d87e2016-07-20 09:21:15 +01004253 ret = __i915_vma_unbind_no_wait(vma);
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004254 if (WARN_ON(ret == -ERESTARTSYS)) {
4255 bool was_interruptible;
Chris Wilson1488fc02012-04-24 15:47:31 +01004256
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004257 was_interruptible = dev_priv->mm.interruptible;
4258 dev_priv->mm.interruptible = false;
Chris Wilson1488fc02012-04-24 15:47:31 +01004259
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004260 WARN_ON(i915_vma_unbind(vma));
Chris Wilson1488fc02012-04-24 15:47:31 +01004261
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004262 dev_priv->mm.interruptible = was_interruptible;
4263 }
Chris Wilson1488fc02012-04-24 15:47:31 +01004264 }
Chris Wilson15717de2016-08-04 07:52:26 +01004265 GEM_BUG_ON(obj->bind_count);
Chris Wilson1488fc02012-04-24 15:47:31 +01004266
Ben Widawsky1d64ae72013-05-31 14:46:20 -07004267 /* Stolen objects don't hold a ref, but do hold pin count. Fix that up
4268 * before progressing. */
4269 if (obj->stolen)
4270 i915_gem_object_unpin_pages(obj);
4271
Daniel Vettera071fa02014-06-18 23:28:09 +02004272 WARN_ON(obj->frontbuffer_bits);
4273
Daniel Vetter656bfa32014-11-20 09:26:30 +01004274 if (obj->pages && obj->madv == I915_MADV_WILLNEED &&
4275 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES &&
4276 obj->tiling_mode != I915_TILING_NONE)
4277 i915_gem_object_unpin_pages(obj);
4278
Ben Widawsky401c29f2013-05-31 11:28:47 -07004279 if (WARN_ON(obj->pages_pin_count))
4280 obj->pages_pin_count = 0;
Chris Wilson340fbd82014-05-22 09:16:52 +01004281 if (discard_backing_storage(obj))
Chris Wilson55372522014-03-25 13:23:06 +00004282 obj->madv = I915_MADV_DONTNEED;
Chris Wilson37e680a2012-06-07 15:38:42 +01004283 i915_gem_object_put_pages(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01004284
Chris Wilson9da3da62012-06-01 15:20:22 +01004285 BUG_ON(obj->pages);
4286
Chris Wilson2f745ad2012-09-04 21:02:58 +01004287 if (obj->base.import_attach)
4288 drm_prime_gem_destroy(&obj->base, NULL);
Chris Wilsonbe726152010-07-23 23:18:50 +01004289
Chris Wilson5cc9ed42014-05-16 14:22:37 +01004290 if (obj->ops->release)
4291 obj->ops->release(obj);
4292
Chris Wilson05394f32010-11-08 19:18:58 +00004293 drm_gem_object_release(&obj->base);
4294 i915_gem_info_remove_obj(dev_priv, obj->base.size);
Chris Wilsonbe726152010-07-23 23:18:50 +01004295
Chris Wilson05394f32010-11-08 19:18:58 +00004296 kfree(obj->bit_17);
Chris Wilson42dcedd2012-11-15 11:32:30 +00004297 i915_gem_object_free(obj);
Paulo Zanonif65c9162013-11-27 18:20:34 -02004298
4299 intel_runtime_pm_put(dev_priv);
Chris Wilsonbe726152010-07-23 23:18:50 +01004300}
4301
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004302struct i915_vma *i915_gem_obj_to_vma(struct drm_i915_gem_object *obj,
4303 struct i915_address_space *vm)
Ben Widawsky2f633152013-07-17 12:19:03 -07004304{
Daniel Vettere656a6c2013-08-14 14:14:04 +02004305 struct i915_vma *vma;
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004306 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Tvrtko Ursulin1b683722015-11-12 11:59:55 +00004307 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL &&
4308 vma->vm == vm)
Daniel Vettere656a6c2013-08-14 14:14:04 +02004309 return vma;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004310 }
4311 return NULL;
4312}
Daniel Vettere656a6c2013-08-14 14:14:04 +02004313
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004314struct i915_vma *i915_gem_obj_to_ggtt_view(struct drm_i915_gem_object *obj,
4315 const struct i915_ggtt_view *view)
4316{
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004317 struct i915_vma *vma;
4318
Tvrtko Ursulin598b9ec2016-04-21 13:04:44 +01004319 GEM_BUG_ON(!view);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004320
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004321 list_for_each_entry(vma, &obj->vma_list, obj_link)
Tvrtko Ursulin598b9ec2016-04-21 13:04:44 +01004322 if (vma->is_ggtt && i915_ggtt_view_equal(&vma->ggtt_view, view))
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004323 return vma;
Daniel Vettere656a6c2013-08-14 14:14:04 +02004324 return NULL;
4325}
4326
Ben Widawsky2f633152013-07-17 12:19:03 -07004327void i915_gem_vma_destroy(struct i915_vma *vma)
4328{
4329 WARN_ON(vma->node.allocated);
Chris Wilsonaaa056672013-08-20 12:56:40 +01004330
4331 /* Keep the vma as a placeholder in the execbuffer reservation lists */
4332 if (!list_empty(&vma->exec_list))
4333 return;
4334
Chris Wilson596c5922016-02-26 11:03:20 +00004335 if (!vma->is_ggtt)
4336 i915_ppgtt_put(i915_vm_to_ppgtt(vma->vm));
Michel Thierryb9d06dd2014-08-06 15:04:44 +02004337
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004338 list_del(&vma->obj_link);
Daniel Vetterb93dab62013-08-26 11:23:47 +02004339
Chris Wilsone20d2ab2015-04-07 16:20:58 +01004340 kmem_cache_free(to_i915(vma->obj->base.dev)->vmas, vma);
Ben Widawsky2f633152013-07-17 12:19:03 -07004341}
4342
Chris Wilsone3efda42014-04-09 09:19:41 +01004343static void
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004344i915_gem_stop_engines(struct drm_device *dev)
Chris Wilsone3efda42014-04-09 09:19:41 +01004345{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004346 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004347 struct intel_engine_cs *engine;
Chris Wilsone3efda42014-04-09 09:19:41 +01004348
Dave Gordonb4ac5af2016-03-24 11:20:38 +00004349 for_each_engine(engine, dev_priv)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004350 dev_priv->gt.stop_engine(engine);
Chris Wilsone3efda42014-04-09 09:19:41 +01004351}
4352
Jesse Barnes5669fca2009-02-17 15:13:31 -08004353int
Chris Wilson45c5f202013-10-16 11:50:01 +01004354i915_gem_suspend(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07004355{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004356 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson45c5f202013-10-16 11:50:01 +01004357 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004358
Chris Wilson54b4f682016-07-21 21:16:19 +01004359 intel_suspend_gt_powersave(dev_priv);
4360
Chris Wilson45c5f202013-10-16 11:50:01 +01004361 mutex_lock(&dev->struct_mutex);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004362
4363 /* We have to flush all the executing contexts to main memory so
4364 * that they can saved in the hibernation image. To ensure the last
4365 * context image is coherent, we have to switch away from it. That
4366 * leaves the dev_priv->kernel_context still active when
4367 * we actually suspend, and its image in memory may not match the GPU
4368 * state. Fortunately, the kernel_context is disposable and we do
4369 * not rely on its state.
4370 */
4371 ret = i915_gem_switch_to_kernel_context(dev_priv);
4372 if (ret)
4373 goto err;
4374
Chris Wilson6e5a5be2016-06-24 14:55:57 +01004375 ret = i915_gem_wait_for_idle(dev_priv);
Chris Wilsonf7403342013-09-13 23:57:04 +01004376 if (ret)
Chris Wilson45c5f202013-10-16 11:50:01 +01004377 goto err;
Chris Wilsonf7403342013-09-13 23:57:04 +01004378
Chris Wilsonc0336662016-05-06 15:40:21 +01004379 i915_gem_retire_requests(dev_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004380
Chris Wilson5ab57c72016-07-15 14:56:20 +01004381 /* Note that rather than stopping the engines, all we have to do
4382 * is assert that every RING_HEAD == RING_TAIL (all execution complete)
4383 * and similar for all logical context images (to ensure they are
4384 * all ready for hibernation).
4385 */
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004386 i915_gem_stop_engines(dev);
Chris Wilsonb2e862d2016-04-28 09:56:41 +01004387 i915_gem_context_lost(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01004388 mutex_unlock(&dev->struct_mutex);
4389
Chris Wilson737b1502015-01-26 18:03:03 +02004390 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
Chris Wilson67d97da2016-07-04 08:08:31 +01004391 cancel_delayed_work_sync(&dev_priv->gt.retire_work);
4392 flush_delayed_work(&dev_priv->gt.idle_work);
Chris Wilson29105cc2010-01-07 10:39:13 +00004393
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004394 /* Assert that we sucessfully flushed all the work and
4395 * reset the GPU back to its idle, low power state.
4396 */
Chris Wilson67d97da2016-07-04 08:08:31 +01004397 WARN_ON(dev_priv->gt.awake);
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004398
Eric Anholt673a3942008-07-30 12:06:12 -07004399 return 0;
Chris Wilson45c5f202013-10-16 11:50:01 +01004400
4401err:
4402 mutex_unlock(&dev->struct_mutex);
4403 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004404}
4405
Chris Wilson5ab57c72016-07-15 14:56:20 +01004406void i915_gem_resume(struct drm_device *dev)
4407{
4408 struct drm_i915_private *dev_priv = to_i915(dev);
4409
4410 mutex_lock(&dev->struct_mutex);
4411 i915_gem_restore_gtt_mappings(dev);
4412
4413 /* As we didn't flush the kernel context before suspend, we cannot
4414 * guarantee that the context image is complete. So let's just reset
4415 * it and start again.
4416 */
4417 if (i915.enable_execlists)
4418 intel_lr_context_reset(dev_priv, dev_priv->kernel_context);
4419
4420 mutex_unlock(&dev->struct_mutex);
4421}
4422
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004423void i915_gem_init_swizzling(struct drm_device *dev)
4424{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004425 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004426
Daniel Vetter11782b02012-01-31 16:47:55 +01004427 if (INTEL_INFO(dev)->gen < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004428 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
4429 return;
4430
4431 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
4432 DISP_TILE_SURFACE_SWIZZLING);
4433
Daniel Vetter11782b02012-01-31 16:47:55 +01004434 if (IS_GEN5(dev))
4435 return;
4436
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004437 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
4438 if (IS_GEN6(dev))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004439 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Ben Widawsky8782e262012-12-18 10:31:23 -08004440 else if (IS_GEN7(dev))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004441 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Ben Widawsky31a53362013-11-02 21:07:04 -07004442 else if (IS_GEN8(dev))
4443 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08004444 else
4445 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004446}
Daniel Vettere21af882012-02-09 20:53:27 +01004447
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004448static void init_unused_ring(struct drm_device *dev, u32 base)
4449{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004450 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004451
4452 I915_WRITE(RING_CTL(base), 0);
4453 I915_WRITE(RING_HEAD(base), 0);
4454 I915_WRITE(RING_TAIL(base), 0);
4455 I915_WRITE(RING_START(base), 0);
4456}
4457
4458static void init_unused_rings(struct drm_device *dev)
4459{
4460 if (IS_I830(dev)) {
4461 init_unused_ring(dev, PRB1_BASE);
4462 init_unused_ring(dev, SRB0_BASE);
4463 init_unused_ring(dev, SRB1_BASE);
4464 init_unused_ring(dev, SRB2_BASE);
4465 init_unused_ring(dev, SRB3_BASE);
4466 } else if (IS_GEN2(dev)) {
4467 init_unused_ring(dev, SRB0_BASE);
4468 init_unused_ring(dev, SRB1_BASE);
4469 } else if (IS_GEN3(dev)) {
4470 init_unused_ring(dev, PRB1_BASE);
4471 init_unused_ring(dev, PRB2_BASE);
4472 }
4473}
4474
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004475int
4476i915_gem_init_hw(struct drm_device *dev)
4477{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004478 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004479 struct intel_engine_cs *engine;
Chris Wilsond200cda2016-04-28 09:56:44 +01004480 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004481
Chris Wilson5e4f5182015-02-13 14:35:59 +00004482 /* Double layer security blanket, see i915_gem_init() */
4483 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4484
Mika Kuoppala3accaf72016-04-13 17:26:43 +03004485 if (HAS_EDRAM(dev) && INTEL_GEN(dev_priv) < 9)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07004486 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004487
Ville Syrjälä0bf21342013-11-29 14:56:12 +02004488 if (IS_HASWELL(dev))
4489 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev) ?
4490 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03004491
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07004492 if (HAS_PCH_NOP(dev)) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01004493 if (IS_IVYBRIDGE(dev)) {
4494 u32 temp = I915_READ(GEN7_MSG_CTL);
4495 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
4496 I915_WRITE(GEN7_MSG_CTL, temp);
4497 } else if (INTEL_INFO(dev)->gen >= 7) {
4498 u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
4499 temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
4500 I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
4501 }
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07004502 }
4503
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004504 i915_gem_init_swizzling(dev);
4505
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004506 /*
4507 * At least 830 can leave some of the unused rings
4508 * "active" (ie. head != tail) after resume which
4509 * will prevent c3 entry. Makes sure all unused rings
4510 * are totally idle.
4511 */
4512 init_unused_rings(dev);
4513
Dave Gordoned54c1a2016-01-19 19:02:54 +00004514 BUG_ON(!dev_priv->kernel_context);
John Harrison90638cc2015-05-29 17:43:37 +01004515
John Harrison4ad2fd82015-06-18 13:11:20 +01004516 ret = i915_ppgtt_init_hw(dev);
4517 if (ret) {
4518 DRM_ERROR("PPGTT enable HW failed %d\n", ret);
4519 goto out;
4520 }
4521
4522 /* Need to do basic initialisation of all rings first: */
Dave Gordonb4ac5af2016-03-24 11:20:38 +00004523 for_each_engine(engine, dev_priv) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004524 ret = engine->init_hw(engine);
Daniel Vetter35a57ff2014-11-20 00:33:07 +01004525 if (ret)
Chris Wilson5e4f5182015-02-13 14:35:59 +00004526 goto out;
Daniel Vetter35a57ff2014-11-20 00:33:07 +01004527 }
Mika Kuoppala99433932013-01-22 14:12:17 +02004528
Peter Antoine0ccdacf2016-04-13 15:03:25 +01004529 intel_mocs_init_l3cc_table(dev);
4530
Alex Dai33a732f2015-08-12 15:43:36 +01004531 /* We can't enable contexts until all firmware is loaded */
Dave Gordone556f7c2016-06-07 09:14:49 +01004532 ret = intel_guc_setup(dev);
4533 if (ret)
4534 goto out;
Alex Dai33a732f2015-08-12 15:43:36 +01004535
Chris Wilson5e4f5182015-02-13 14:35:59 +00004536out:
4537 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004538 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004539}
4540
Chris Wilson39df9192016-07-20 13:31:57 +01004541bool intel_sanitize_semaphores(struct drm_i915_private *dev_priv, int value)
4542{
4543 if (INTEL_INFO(dev_priv)->gen < 6)
4544 return false;
4545
4546 /* TODO: make semaphores and Execlists play nicely together */
4547 if (i915.enable_execlists)
4548 return false;
4549
4550 if (value >= 0)
4551 return value;
4552
4553#ifdef CONFIG_INTEL_IOMMU
4554 /* Enable semaphores on SNB when IO remapping is off */
4555 if (INTEL_INFO(dev_priv)->gen == 6 && intel_iommu_gfx_mapped)
4556 return false;
4557#endif
4558
4559 return true;
4560}
4561
Chris Wilson1070a422012-04-24 15:47:41 +01004562int i915_gem_init(struct drm_device *dev)
4563{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004564 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson1070a422012-04-24 15:47:41 +01004565 int ret;
4566
Chris Wilson1070a422012-04-24 15:47:41 +01004567 mutex_lock(&dev->struct_mutex);
Jesse Barnesd62b4892013-03-08 10:45:53 -08004568
Oscar Mateoa83014d2014-07-24 17:04:21 +01004569 if (!i915.enable_execlists) {
Chris Wilson7e37f882016-08-02 22:50:21 +01004570 dev_priv->gt.cleanup_engine = intel_engine_cleanup;
4571 dev_priv->gt.stop_engine = intel_engine_stop;
Oscar Mateo454afeb2014-07-24 17:04:22 +01004572 } else {
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004573 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
4574 dev_priv->gt.stop_engine = intel_logical_ring_stop;
Oscar Mateoa83014d2014-07-24 17:04:21 +01004575 }
4576
Chris Wilson5e4f5182015-02-13 14:35:59 +00004577 /* This is just a security blanket to placate dragons.
4578 * On some systems, we very sporadically observe that the first TLBs
4579 * used by the CS may be stale, despite us poking the TLB reset. If
4580 * we hold the forcewake during initialisation these problems
4581 * just magically go away.
4582 */
4583 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4584
Chris Wilson72778cb2016-05-19 16:17:16 +01004585 i915_gem_init_userptr(dev_priv);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01004586
4587 ret = i915_gem_init_ggtt(dev_priv);
4588 if (ret)
4589 goto out_unlock;
Jesse Barnesd62b4892013-03-08 10:45:53 -08004590
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004591 ret = i915_gem_context_init(dev);
Jani Nikula7bcc3772014-12-05 14:17:42 +02004592 if (ret)
4593 goto out_unlock;
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004594
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01004595 ret = intel_engines_init(dev);
Daniel Vetter35a57ff2014-11-20 00:33:07 +01004596 if (ret)
Jani Nikula7bcc3772014-12-05 14:17:42 +02004597 goto out_unlock;
Daniel Vetter53ca26c2012-04-26 23:28:03 +02004598
4599 ret = i915_gem_init_hw(dev);
Chris Wilson60990322014-04-09 09:19:42 +01004600 if (ret == -EIO) {
Chris Wilson7e21d642016-07-27 09:07:29 +01004601 /* Allow engine initialisation to fail by marking the GPU as
Chris Wilson60990322014-04-09 09:19:42 +01004602 * wedged. But we only want to do this where the GPU is angry,
4603 * for all other failure, such as an allocation failure, bail.
4604 */
4605 DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
Peter Zijlstra805de8f42015-04-24 01:12:32 +02004606 atomic_or(I915_WEDGED, &dev_priv->gpu_error.reset_counter);
Chris Wilson60990322014-04-09 09:19:42 +01004607 ret = 0;
Chris Wilson1070a422012-04-24 15:47:41 +01004608 }
Jani Nikula7bcc3772014-12-05 14:17:42 +02004609
4610out_unlock:
Chris Wilson5e4f5182015-02-13 14:35:59 +00004611 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Chris Wilson60990322014-04-09 09:19:42 +01004612 mutex_unlock(&dev->struct_mutex);
Chris Wilson1070a422012-04-24 15:47:41 +01004613
Chris Wilson60990322014-04-09 09:19:42 +01004614 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01004615}
4616
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004617void
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004618i915_gem_cleanup_engines(struct drm_device *dev)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004619{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004620 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004621 struct intel_engine_cs *engine;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004622
Dave Gordonb4ac5af2016-03-24 11:20:38 +00004623 for_each_engine(engine, dev_priv)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004624 dev_priv->gt.cleanup_engine(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004625}
4626
Chris Wilson64193402010-10-24 12:38:05 +01004627static void
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00004628init_engine_lists(struct intel_engine_cs *engine)
Chris Wilson64193402010-10-24 12:38:05 +01004629{
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00004630 INIT_LIST_HEAD(&engine->active_list);
4631 INIT_LIST_HEAD(&engine->request_list);
Chris Wilson64193402010-10-24 12:38:05 +01004632}
4633
Eric Anholt673a3942008-07-30 12:06:12 -07004634void
Imre Deak40ae4e12016-03-16 14:54:03 +02004635i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
4636{
Chris Wilson91c8a322016-07-05 10:40:23 +01004637 struct drm_device *dev = &dev_priv->drm;
Imre Deak40ae4e12016-03-16 14:54:03 +02004638
4639 if (INTEL_INFO(dev_priv)->gen >= 7 && !IS_VALLEYVIEW(dev_priv) &&
4640 !IS_CHERRYVIEW(dev_priv))
4641 dev_priv->num_fence_regs = 32;
4642 else if (INTEL_INFO(dev_priv)->gen >= 4 || IS_I945G(dev_priv) ||
4643 IS_I945GM(dev_priv) || IS_G33(dev_priv))
4644 dev_priv->num_fence_regs = 16;
4645 else
4646 dev_priv->num_fence_regs = 8;
4647
Chris Wilsonc0336662016-05-06 15:40:21 +01004648 if (intel_vgpu_active(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02004649 dev_priv->num_fence_regs =
4650 I915_READ(vgtif_reg(avail_rs.fence_num));
4651
4652 /* Initialize fence registers to zero */
4653 i915_gem_restore_fences(dev);
4654
4655 i915_gem_detect_bit_6_swizzle(dev);
4656}
4657
4658void
Imre Deakd64aa092016-01-19 15:26:29 +02004659i915_gem_load_init(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07004660{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004661 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson42dcedd2012-11-15 11:32:30 +00004662 int i;
4663
Chris Wilsonefab6d82015-04-07 16:20:57 +01004664 dev_priv->objects =
Chris Wilson42dcedd2012-11-15 11:32:30 +00004665 kmem_cache_create("i915_gem_object",
4666 sizeof(struct drm_i915_gem_object), 0,
4667 SLAB_HWCACHE_ALIGN,
4668 NULL);
Chris Wilsone20d2ab2015-04-07 16:20:58 +01004669 dev_priv->vmas =
4670 kmem_cache_create("i915_gem_vma",
4671 sizeof(struct i915_vma), 0,
4672 SLAB_HWCACHE_ALIGN,
4673 NULL);
Chris Wilsonefab6d82015-04-07 16:20:57 +01004674 dev_priv->requests =
4675 kmem_cache_create("i915_gem_request",
4676 sizeof(struct drm_i915_gem_request), 0,
4677 SLAB_HWCACHE_ALIGN,
4678 NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07004679
Ben Widawskya33afea2013-09-17 21:12:45 -07004680 INIT_LIST_HEAD(&dev_priv->context_list);
Chris Wilson6c085a72012-08-20 11:40:46 +02004681 INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
4682 INIT_LIST_HEAD(&dev_priv->mm.bound_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004683 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00004684 for (i = 0; i < I915_NUM_ENGINES; i++)
4685 init_engine_lists(&dev_priv->engine[i]);
Daniel Vetter4b9de732011-10-09 21:52:02 +02004686 for (i = 0; i < I915_MAX_NUM_FENCES; i++)
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004687 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Chris Wilson67d97da2016-07-04 08:08:31 +01004688 INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
Eric Anholt673a3942008-07-30 12:06:12 -07004689 i915_gem_retire_work_handler);
Chris Wilson67d97da2016-07-04 08:08:31 +01004690 INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004691 i915_gem_idle_work_handler);
Chris Wilson1f15b762016-07-01 17:23:14 +01004692 init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01004693 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson31169712009-09-14 16:50:28 +01004694
Chris Wilson72bfa192010-12-19 11:42:05 +00004695 dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
4696
Chris Wilson19b2dbd2013-06-12 10:15:12 +01004697 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Eric Anholt10ed13e2011-05-06 13:53:49 -07004698
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004699 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01004700
Chris Wilsonce453d82011-02-21 14:43:56 +00004701 dev_priv->mm.interruptible = true;
4702
Daniel Vetterf99d7062014-06-19 16:01:59 +02004703 mutex_init(&dev_priv->fb_tracking.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004704}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004705
Imre Deakd64aa092016-01-19 15:26:29 +02004706void i915_gem_load_cleanup(struct drm_device *dev)
4707{
4708 struct drm_i915_private *dev_priv = to_i915(dev);
4709
4710 kmem_cache_destroy(dev_priv->requests);
4711 kmem_cache_destroy(dev_priv->vmas);
4712 kmem_cache_destroy(dev_priv->objects);
4713}
4714
Chris Wilson461fb992016-05-14 07:26:33 +01004715int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
4716{
4717 struct drm_i915_gem_object *obj;
4718
4719 /* Called just before we write the hibernation image.
4720 *
4721 * We need to update the domain tracking to reflect that the CPU
4722 * will be accessing all the pages to create and restore from the
4723 * hibernation, and so upon restoration those pages will be in the
4724 * CPU domain.
4725 *
4726 * To make sure the hibernation image contains the latest state,
4727 * we update that state just before writing out the image.
4728 */
4729
4730 list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list) {
4731 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4732 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4733 }
4734
4735 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
4736 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4737 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4738 }
4739
4740 return 0;
4741}
4742
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004743void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004744{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004745 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilson15f7bbc2016-07-26 12:01:52 +01004746 struct drm_i915_gem_request *request;
Eric Anholtb9624422009-06-03 07:27:35 +00004747
4748 /* Clean up our request list when the client is going away, so that
4749 * later retire_requests won't dereference our soon-to-be-gone
4750 * file_priv.
4751 */
Chris Wilson1c255952010-09-26 11:03:27 +01004752 spin_lock(&file_priv->mm.lock);
Chris Wilson15f7bbc2016-07-26 12:01:52 +01004753 list_for_each_entry(request, &file_priv->mm.request_list, client_list)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004754 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01004755 spin_unlock(&file_priv->mm.lock);
Chris Wilson31169712009-09-14 16:50:28 +01004756
Chris Wilson2e1b8732015-04-27 13:41:22 +01004757 if (!list_empty(&file_priv->rps.link)) {
Chris Wilson8d3afd72015-05-21 21:01:47 +01004758 spin_lock(&to_i915(dev)->rps.client_lock);
Chris Wilson2e1b8732015-04-27 13:41:22 +01004759 list_del(&file_priv->rps.link);
Chris Wilson8d3afd72015-05-21 21:01:47 +01004760 spin_unlock(&to_i915(dev)->rps.client_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01004761 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004762}
4763
4764int i915_gem_open(struct drm_device *dev, struct drm_file *file)
4765{
4766 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08004767 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004768
4769 DRM_DEBUG_DRIVER("\n");
4770
4771 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
4772 if (!file_priv)
4773 return -ENOMEM;
4774
4775 file->driver_priv = file_priv;
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004776 file_priv->dev_priv = to_i915(dev);
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02004777 file_priv->file = file;
Chris Wilson2e1b8732015-04-27 13:41:22 +01004778 INIT_LIST_HEAD(&file_priv->rps.link);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004779
4780 spin_lock_init(&file_priv->mm.lock);
4781 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004782
Chris Wilsonc80ff162016-07-27 09:07:27 +01004783 file_priv->bsd_engine = -1;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00004784
Ben Widawskye422b882013-12-06 14:10:58 -08004785 ret = i915_gem_context_open(dev, file);
4786 if (ret)
4787 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004788
Ben Widawskye422b882013-12-06 14:10:58 -08004789 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004790}
4791
Daniel Vetterb680c372014-09-19 18:27:27 +02004792/**
4793 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07004794 * @old: current GEM buffer for the frontbuffer slots
4795 * @new: new GEM buffer for the frontbuffer slots
4796 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02004797 *
4798 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
4799 * from @old and setting them in @new. Both @old and @new can be NULL.
4800 */
Daniel Vettera071fa02014-06-18 23:28:09 +02004801void i915_gem_track_fb(struct drm_i915_gem_object *old,
4802 struct drm_i915_gem_object *new,
4803 unsigned frontbuffer_bits)
4804{
4805 if (old) {
4806 WARN_ON(!mutex_is_locked(&old->base.dev->struct_mutex));
4807 WARN_ON(!(old->frontbuffer_bits & frontbuffer_bits));
4808 old->frontbuffer_bits &= ~frontbuffer_bits;
4809 }
4810
4811 if (new) {
4812 WARN_ON(!mutex_is_locked(&new->base.dev->struct_mutex));
4813 WARN_ON(new->frontbuffer_bits & frontbuffer_bits);
4814 new->frontbuffer_bits |= frontbuffer_bits;
4815 }
4816}
4817
Ben Widawskya70a3142013-07-31 16:59:56 -07004818/* All the new VM stuff */
Michel Thierry088e0df2015-08-07 17:40:17 +01004819u64 i915_gem_obj_offset(struct drm_i915_gem_object *o,
4820 struct i915_address_space *vm)
Ben Widawskya70a3142013-07-31 16:59:56 -07004821{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004822 struct drm_i915_private *dev_priv = to_i915(o->base.dev);
Ben Widawskya70a3142013-07-31 16:59:56 -07004823 struct i915_vma *vma;
4824
Daniel Vetter896ab1a2014-08-06 15:04:51 +02004825 WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base);
Ben Widawskya70a3142013-07-31 16:59:56 -07004826
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004827 list_for_each_entry(vma, &o->vma_list, obj_link) {
Chris Wilson596c5922016-02-26 11:03:20 +00004828 if (vma->is_ggtt &&
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004829 vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
4830 continue;
4831 if (vma->vm == vm)
Ben Widawskya70a3142013-07-31 16:59:56 -07004832 return vma->node.start;
Ben Widawskya70a3142013-07-31 16:59:56 -07004833 }
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004834
Daniel Vetterf25748ea2014-06-17 22:34:38 +02004835 WARN(1, "%s vma for this object not found.\n",
4836 i915_is_ggtt(vm) ? "global" : "ppgtt");
Ben Widawskya70a3142013-07-31 16:59:56 -07004837 return -1;
4838}
4839
Michel Thierry088e0df2015-08-07 17:40:17 +01004840u64 i915_gem_obj_ggtt_offset_view(struct drm_i915_gem_object *o,
4841 const struct i915_ggtt_view *view)
Ben Widawskya70a3142013-07-31 16:59:56 -07004842{
4843 struct i915_vma *vma;
4844
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004845 list_for_each_entry(vma, &o->vma_list, obj_link)
Tvrtko Ursulin8aac2222016-04-21 13:04:45 +01004846 if (vma->is_ggtt && i915_ggtt_view_equal(&vma->ggtt_view, view))
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004847 return vma->node.start;
4848
Tvrtko Ursulin5678ad72015-03-17 14:45:29 +00004849 WARN(1, "global vma for this object not found. (view=%u)\n", view->type);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004850 return -1;
4851}
4852
4853bool i915_gem_obj_bound(struct drm_i915_gem_object *o,
4854 struct i915_address_space *vm)
4855{
4856 struct i915_vma *vma;
4857
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004858 list_for_each_entry(vma, &o->vma_list, obj_link) {
Chris Wilson596c5922016-02-26 11:03:20 +00004859 if (vma->is_ggtt &&
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004860 vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
4861 continue;
4862 if (vma->vm == vm && drm_mm_node_allocated(&vma->node))
4863 return true;
4864 }
4865
4866 return false;
4867}
4868
4869bool i915_gem_obj_ggtt_bound_view(struct drm_i915_gem_object *o,
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02004870 const struct i915_ggtt_view *view)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004871{
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004872 struct i915_vma *vma;
4873
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004874 list_for_each_entry(vma, &o->vma_list, obj_link)
Tvrtko Ursulinff5ec222016-04-21 13:04:46 +01004875 if (vma->is_ggtt &&
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02004876 i915_ggtt_view_equal(&vma->ggtt_view, view) &&
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00004877 drm_mm_node_allocated(&vma->node))
Ben Widawskya70a3142013-07-31 16:59:56 -07004878 return true;
4879
4880 return false;
4881}
4882
Tvrtko Ursulin8da32722016-04-21 13:04:43 +01004883unsigned long i915_gem_obj_ggtt_size(struct drm_i915_gem_object *o)
Ben Widawskya70a3142013-07-31 16:59:56 -07004884{
Ben Widawskya70a3142013-07-31 16:59:56 -07004885 struct i915_vma *vma;
4886
Tvrtko Ursulin8da32722016-04-21 13:04:43 +01004887 GEM_BUG_ON(list_empty(&o->vma_list));
Ben Widawskya70a3142013-07-31 16:59:56 -07004888
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004889 list_for_each_entry(vma, &o->vma_list, obj_link) {
Chris Wilson596c5922016-02-26 11:03:20 +00004890 if (vma->is_ggtt &&
Tvrtko Ursulin8da32722016-04-21 13:04:43 +01004891 vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
Ben Widawskya70a3142013-07-31 16:59:56 -07004892 return vma->node.size;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004893 }
Tvrtko Ursulin8da32722016-04-21 13:04:43 +01004894
Ben Widawskya70a3142013-07-31 16:59:56 -07004895 return 0;
4896}
4897
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004898bool i915_gem_obj_is_pinned(struct drm_i915_gem_object *obj)
Ben Widawsky5c2abbe2013-09-24 09:57:57 -07004899{
4900 struct i915_vma *vma;
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004901 list_for_each_entry(vma, &obj->vma_list, obj_link)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004902 if (vma->pin_count > 0)
4903 return true;
Joonas Lahtinena6631ae2015-05-06 14:34:58 +03004904
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004905 return false;
Ben Widawsky5c2abbe2013-09-24 09:57:57 -07004906}
Dave Gordonea702992015-07-09 19:29:02 +01004907
Dave Gordon033908a2015-12-10 18:51:23 +00004908/* Like i915_gem_object_get_page(), but mark the returned page dirty */
4909struct page *
4910i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj, int n)
4911{
4912 struct page *page;
4913
4914 /* Only default objects have per-page dirty tracking */
Chris Wilsonb9bcd142016-06-20 15:05:51 +01004915 if (WARN_ON(!i915_gem_object_has_struct_page(obj)))
Dave Gordon033908a2015-12-10 18:51:23 +00004916 return NULL;
4917
4918 page = i915_gem_object_get_page(obj, n);
4919 set_page_dirty(page);
4920 return page;
4921}
4922
Dave Gordonea702992015-07-09 19:29:02 +01004923/* Allocate a new GEM object and fill it with the supplied data */
4924struct drm_i915_gem_object *
4925i915_gem_object_create_from_data(struct drm_device *dev,
4926 const void *data, size_t size)
4927{
4928 struct drm_i915_gem_object *obj;
4929 struct sg_table *sg;
4930 size_t bytes;
4931 int ret;
4932
Dave Gordond37cd8a2016-04-22 19:14:32 +01004933 obj = i915_gem_object_create(dev, round_up(size, PAGE_SIZE));
Chris Wilsonfe3db792016-04-25 13:32:13 +01004934 if (IS_ERR(obj))
Dave Gordonea702992015-07-09 19:29:02 +01004935 return obj;
4936
4937 ret = i915_gem_object_set_to_cpu_domain(obj, true);
4938 if (ret)
4939 goto fail;
4940
4941 ret = i915_gem_object_get_pages(obj);
4942 if (ret)
4943 goto fail;
4944
4945 i915_gem_object_pin_pages(obj);
4946 sg = obj->pages;
4947 bytes = sg_copy_from_buffer(sg->sgl, sg->nents, (void *)data, size);
Dave Gordon9e7d18c2015-12-10 18:51:24 +00004948 obj->dirty = 1; /* Backing store is now out of date */
Dave Gordonea702992015-07-09 19:29:02 +01004949 i915_gem_object_unpin_pages(obj);
4950
4951 if (WARN_ON(bytes != size)) {
4952 DRM_ERROR("Incomplete copy, wrote %zu of %zu", bytes, size);
4953 ret = -EFAULT;
4954 goto fail;
4955 }
4956
4957 return obj;
4958
4959fail:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004960 i915_gem_object_put(obj);
Dave Gordonea702992015-07-09 19:29:02 +01004961 return ERR_PTR(ret);
4962}