blob: 8892baaececd0d6d5617820fed1d23353ec06e5b [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 Wilsonb3612372012-08-24 09:35:08 +01001342/**
1343 * Ensures that all rendering to the object has completed and the object is
1344 * safe to unbind from the GTT or access from the CPU.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001345 * @obj: i915 gem object
1346 * @readonly: waiting for read access or write
Chris Wilsonb3612372012-08-24 09:35:08 +01001347 */
Chris Wilson2e2f3512015-04-27 13:41:14 +01001348int
Chris Wilsonb3612372012-08-24 09:35:08 +01001349i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
1350 bool readonly)
1351{
Chris Wilsonc13d87e2016-07-20 09:21:15 +01001352 struct reservation_object *resv;
Chris Wilsonb4716182015-04-27 13:41:17 +01001353 int ret, i;
Chris Wilsonb3612372012-08-24 09:35:08 +01001354
Chris Wilsonb4716182015-04-27 13:41:17 +01001355 if (readonly) {
1356 if (obj->last_write_req != NULL) {
1357 ret = i915_wait_request(obj->last_write_req);
1358 if (ret)
1359 return ret;
Chris Wilsonb3612372012-08-24 09:35:08 +01001360
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001361 i = obj->last_write_req->engine->id;
Chris Wilsonb4716182015-04-27 13:41:17 +01001362 if (obj->last_read_req[i] == obj->last_write_req)
1363 i915_gem_object_retire__read(obj, i);
1364 else
1365 i915_gem_object_retire__write(obj);
1366 }
1367 } else {
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00001368 for (i = 0; i < I915_NUM_ENGINES; i++) {
Chris Wilsonb4716182015-04-27 13:41:17 +01001369 if (obj->last_read_req[i] == NULL)
1370 continue;
1371
1372 ret = i915_wait_request(obj->last_read_req[i]);
1373 if (ret)
1374 return ret;
1375
1376 i915_gem_object_retire__read(obj, i);
1377 }
Chris Wilsond501b1d2016-04-13 17:35:02 +01001378 GEM_BUG_ON(obj->active);
Chris Wilsonb4716182015-04-27 13:41:17 +01001379 }
1380
Chris Wilsonc13d87e2016-07-20 09:21:15 +01001381 resv = i915_gem_object_get_dmabuf_resv(obj);
1382 if (resv) {
1383 long err;
1384
1385 err = reservation_object_wait_timeout_rcu(resv, !readonly, true,
1386 MAX_SCHEDULE_TIMEOUT);
1387 if (err < 0)
1388 return err;
1389 }
1390
Chris Wilsonb4716182015-04-27 13:41:17 +01001391 return 0;
1392}
1393
1394static void
1395i915_gem_object_retire_request(struct drm_i915_gem_object *obj,
1396 struct drm_i915_gem_request *req)
1397{
Chris Wilson7e21d642016-07-27 09:07:29 +01001398 int idx = req->engine->id;
Chris Wilsonb4716182015-04-27 13:41:17 +01001399
Chris Wilson7e21d642016-07-27 09:07:29 +01001400 if (obj->last_read_req[idx] == req)
1401 i915_gem_object_retire__read(obj, idx);
Chris Wilsonb4716182015-04-27 13:41:17 +01001402 else if (obj->last_write_req == req)
1403 i915_gem_object_retire__write(obj);
1404
Chris Wilson0c5eed62016-06-29 15:51:14 +01001405 if (!i915_reset_in_progress(&req->i915->gpu_error))
Chris Wilson05235c52016-07-20 09:21:08 +01001406 i915_gem_request_retire_upto(req);
Chris Wilsonb3612372012-08-24 09:35:08 +01001407}
1408
Chris Wilson3236f572012-08-24 09:35:09 +01001409/* A nonblocking variant of the above wait. This is a highly dangerous routine
1410 * as the object state may change during this call.
1411 */
1412static __must_check int
1413i915_gem_object_wait_rendering__nonblocking(struct drm_i915_gem_object *obj,
Chris Wilson2e1b8732015-04-27 13:41:22 +01001414 struct intel_rps_client *rps,
Chris Wilson3236f572012-08-24 09:35:09 +01001415 bool readonly)
1416{
1417 struct drm_device *dev = obj->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001418 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00001419 struct drm_i915_gem_request *requests[I915_NUM_ENGINES];
Chris Wilsonb4716182015-04-27 13:41:17 +01001420 int ret, i, n = 0;
Chris Wilson3236f572012-08-24 09:35:09 +01001421
1422 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1423 BUG_ON(!dev_priv->mm.interruptible);
1424
Chris Wilsonb4716182015-04-27 13:41:17 +01001425 if (!obj->active)
Chris Wilson3236f572012-08-24 09:35:09 +01001426 return 0;
1427
Chris Wilsonb4716182015-04-27 13:41:17 +01001428 if (readonly) {
1429 struct drm_i915_gem_request *req;
1430
1431 req = obj->last_write_req;
1432 if (req == NULL)
1433 return 0;
1434
Chris Wilsone8a261e2016-07-20 13:31:49 +01001435 requests[n++] = i915_gem_request_get(req);
Chris Wilsonb4716182015-04-27 13:41:17 +01001436 } else {
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00001437 for (i = 0; i < I915_NUM_ENGINES; i++) {
Chris Wilsonb4716182015-04-27 13:41:17 +01001438 struct drm_i915_gem_request *req;
1439
1440 req = obj->last_read_req[i];
1441 if (req == NULL)
1442 continue;
1443
Chris Wilsone8a261e2016-07-20 13:31:49 +01001444 requests[n++] = i915_gem_request_get(req);
Chris Wilsonb4716182015-04-27 13:41:17 +01001445 }
1446 }
1447
1448 mutex_unlock(&dev->struct_mutex);
Chris Wilson299259a2016-04-13 17:35:06 +01001449 ret = 0;
Chris Wilsonb4716182015-04-27 13:41:17 +01001450 for (i = 0; ret == 0 && i < n; i++)
Chris Wilson299259a2016-04-13 17:35:06 +01001451 ret = __i915_wait_request(requests[i], true, NULL, rps);
Chris Wilsonb4716182015-04-27 13:41:17 +01001452 mutex_lock(&dev->struct_mutex);
1453
Chris Wilsonb4716182015-04-27 13:41:17 +01001454 for (i = 0; i < n; i++) {
1455 if (ret == 0)
1456 i915_gem_object_retire_request(obj, requests[i]);
Chris Wilsone8a261e2016-07-20 13:31:49 +01001457 i915_gem_request_put(requests[i]);
Chris Wilsonb4716182015-04-27 13:41:17 +01001458 }
1459
1460 return ret;
Chris Wilson3236f572012-08-24 09:35:09 +01001461}
1462
Chris Wilson2e1b8732015-04-27 13:41:22 +01001463static struct intel_rps_client *to_rps_client(struct drm_file *file)
1464{
1465 struct drm_i915_file_private *fpriv = file->driver_priv;
1466 return &fpriv->rps;
1467}
1468
Chris Wilsonaeecc962016-06-17 14:46:39 -03001469static enum fb_op_origin
1470write_origin(struct drm_i915_gem_object *obj, unsigned domain)
1471{
1472 return domain == I915_GEM_DOMAIN_GTT && !obj->has_wc_mmap ?
1473 ORIGIN_GTT : ORIGIN_CPU;
1474}
1475
Eric Anholt673a3942008-07-30 12:06:12 -07001476/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001477 * Called when user space prepares to use an object with the CPU, either
1478 * through the mmap ioctl's mapping or a GTT mapping.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001479 * @dev: drm device
1480 * @data: ioctl data blob
1481 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001482 */
1483int
1484i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001485 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001486{
1487 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001488 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001489 uint32_t read_domains = args->read_domains;
1490 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001491 int ret;
1492
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001493 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001494 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001495 return -EINVAL;
1496
Chris Wilson21d509e2009-06-06 09:46:02 +01001497 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001498 return -EINVAL;
1499
1500 /* Having something in the write domain implies it's in the read
1501 * domain, and only that read domain. Enforce that in the request.
1502 */
1503 if (write_domain != 0 && read_domains != write_domain)
1504 return -EINVAL;
1505
Chris Wilson76c1dec2010-09-25 11:22:51 +01001506 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001507 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001508 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001509
Chris Wilson03ac0642016-07-20 13:31:51 +01001510 obj = i915_gem_object_lookup(file, args->handle);
1511 if (!obj) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001512 ret = -ENOENT;
1513 goto unlock;
Chris Wilson76c1dec2010-09-25 11:22:51 +01001514 }
Jesse Barnes652c3932009-08-17 13:31:43 -07001515
Chris Wilson3236f572012-08-24 09:35:09 +01001516 /* Try to flush the object off the GPU without holding the lock.
1517 * We will repeat the flush holding the lock in the normal manner
1518 * to catch cases where we are gazumped.
1519 */
Chris Wilson6e4930f2014-02-07 18:37:06 -02001520 ret = i915_gem_object_wait_rendering__nonblocking(obj,
Chris Wilson2e1b8732015-04-27 13:41:22 +01001521 to_rps_client(file),
Chris Wilson6e4930f2014-02-07 18:37:06 -02001522 !write_domain);
Chris Wilson3236f572012-08-24 09:35:09 +01001523 if (ret)
1524 goto unref;
1525
Chris Wilson43566de2015-01-02 16:29:29 +05301526 if (read_domains & I915_GEM_DOMAIN_GTT)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001527 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Chris Wilson43566de2015-01-02 16:29:29 +05301528 else
Eric Anholte47c68e2008-11-14 13:35:19 -08001529 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001530
Daniel Vetter031b6982015-06-26 19:35:16 +02001531 if (write_domain != 0)
Chris Wilsonaeecc962016-06-17 14:46:39 -03001532 intel_fb_obj_invalidate(obj, write_origin(obj, write_domain));
Daniel Vetter031b6982015-06-26 19:35:16 +02001533
Chris Wilson3236f572012-08-24 09:35:09 +01001534unref:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01001535 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001536unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001537 mutex_unlock(&dev->struct_mutex);
1538 return ret;
1539}
1540
1541/**
1542 * Called when user space has done writes to this buffer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001543 * @dev: drm device
1544 * @data: ioctl data blob
1545 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001546 */
1547int
1548i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001549 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001550{
1551 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001552 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001553 int ret = 0;
1554
Chris Wilson76c1dec2010-09-25 11:22:51 +01001555 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001556 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001557 return ret;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001558
Chris Wilson03ac0642016-07-20 13:31:51 +01001559 obj = i915_gem_object_lookup(file, args->handle);
1560 if (!obj) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001561 ret = -ENOENT;
1562 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07001563 }
1564
Eric Anholt673a3942008-07-30 12:06:12 -07001565 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson2c225692013-08-09 12:26:45 +01001566 if (obj->pin_display)
Daniel Vettere62b59e2015-01-21 14:53:48 +01001567 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08001568
Chris Wilsonf8c417c2016-07-20 13:31:53 +01001569 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001570unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001571 mutex_unlock(&dev->struct_mutex);
1572 return ret;
1573}
1574
1575/**
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001576 * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
1577 * it is mapped to.
1578 * @dev: drm device
1579 * @data: ioctl data blob
1580 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001581 *
1582 * While the mapping holds a reference on the contents of the object, it doesn't
1583 * imply a ref on the object itself.
Daniel Vetter34367382014-10-16 12:28:18 +02001584 *
1585 * IMPORTANT:
1586 *
1587 * DRM driver writers who look a this function as an example for how to do GEM
1588 * mmap support, please don't implement mmap support like here. The modern way
1589 * to implement DRM mmap support is with an mmap offset ioctl (like
1590 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
1591 * That way debug tooling like valgrind will understand what's going on, hiding
1592 * the mmap call in a driver private ioctl will break that. The i915 driver only
1593 * does cpu mmaps this way because we didn't know better.
Eric Anholt673a3942008-07-30 12:06:12 -07001594 */
1595int
1596i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001597 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001598{
1599 struct drm_i915_gem_mmap *args = data;
Chris Wilson03ac0642016-07-20 13:31:51 +01001600 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001601 unsigned long addr;
1602
Akash Goel1816f922015-01-02 16:29:30 +05301603 if (args->flags & ~(I915_MMAP_WC))
1604 return -EINVAL;
1605
Borislav Petkov568a58e2016-03-29 17:42:01 +02001606 if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
Akash Goel1816f922015-01-02 16:29:30 +05301607 return -ENODEV;
1608
Chris Wilson03ac0642016-07-20 13:31:51 +01001609 obj = i915_gem_object_lookup(file, args->handle);
1610 if (!obj)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001611 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001612
Daniel Vetter1286ff72012-05-10 15:25:09 +02001613 /* prime objects have no backing filp to GEM mmap
1614 * pages from.
1615 */
Chris Wilson03ac0642016-07-20 13:31:51 +01001616 if (!obj->base.filp) {
Chris Wilson34911fd2016-07-20 13:31:54 +01001617 i915_gem_object_put_unlocked(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02001618 return -EINVAL;
1619 }
1620
Chris Wilson03ac0642016-07-20 13:31:51 +01001621 addr = vm_mmap(obj->base.filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001622 PROT_READ | PROT_WRITE, MAP_SHARED,
1623 args->offset);
Akash Goel1816f922015-01-02 16:29:30 +05301624 if (args->flags & I915_MMAP_WC) {
1625 struct mm_struct *mm = current->mm;
1626 struct vm_area_struct *vma;
1627
Michal Hocko80a89a52016-05-23 16:26:11 -07001628 if (down_write_killable(&mm->mmap_sem)) {
Chris Wilson34911fd2016-07-20 13:31:54 +01001629 i915_gem_object_put_unlocked(obj);
Michal Hocko80a89a52016-05-23 16:26:11 -07001630 return -EINTR;
1631 }
Akash Goel1816f922015-01-02 16:29:30 +05301632 vma = find_vma(mm, addr);
1633 if (vma)
1634 vma->vm_page_prot =
1635 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
1636 else
1637 addr = -ENOMEM;
1638 up_write(&mm->mmap_sem);
Chris Wilsonaeecc962016-06-17 14:46:39 -03001639
1640 /* This may race, but that's ok, it only gets set */
Chris Wilson03ac0642016-07-20 13:31:51 +01001641 WRITE_ONCE(obj->has_wc_mmap, true);
Akash Goel1816f922015-01-02 16:29:30 +05301642 }
Chris Wilson34911fd2016-07-20 13:31:54 +01001643 i915_gem_object_put_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001644 if (IS_ERR((void *)addr))
1645 return addr;
1646
1647 args->addr_ptr = (uint64_t) addr;
1648
1649 return 0;
1650}
1651
Jesse Barnesde151cf2008-11-12 10:03:55 -08001652/**
1653 * i915_gem_fault - fault a page into the GTT
Geliang Tangd9072a32015-09-15 05:58:44 -07001654 * @vma: VMA in question
1655 * @vmf: fault info
Jesse Barnesde151cf2008-11-12 10:03:55 -08001656 *
1657 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1658 * from userspace. The fault handler takes care of binding the object to
1659 * the GTT (if needed), allocating and programming a fence register (again,
1660 * only if needed based on whether the old reg is still valid or the object
1661 * is tiled) and inserting a new PTE into the faulting process.
1662 *
1663 * Note that the faulting process may involve evicting existing objects
1664 * from the GTT and/or fence registers to make room. So performance may
1665 * suffer if the GTT working set is large or there are few fence registers
1666 * left.
1667 */
1668int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1669{
Chris Wilson05394f32010-11-08 19:18:58 +00001670 struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
1671 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001672 struct drm_i915_private *dev_priv = to_i915(dev);
1673 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001674 struct i915_ggtt_view view = i915_ggtt_view_normal;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001675 pgoff_t page_offset;
1676 unsigned long pfn;
1677 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001678 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001679
Paulo Zanonif65c9162013-11-27 18:20:34 -02001680 intel_runtime_pm_get(dev_priv);
1681
Jesse Barnesde151cf2008-11-12 10:03:55 -08001682 /* We don't use vmf->pgoff since that has the fake offset */
1683 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1684 PAGE_SHIFT;
1685
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001686 ret = i915_mutex_lock_interruptible(dev);
1687 if (ret)
1688 goto out;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001689
Chris Wilsondb53a302011-02-03 11:57:46 +00001690 trace_i915_gem_object_fault(obj, page_offset, true, write);
1691
Chris Wilson6e4930f2014-02-07 18:37:06 -02001692 /* Try to flush the object off the GPU first without holding the lock.
1693 * Upon reacquiring the lock, we will perform our sanity checks and then
1694 * repeat the flush holding the lock in the normal manner to catch cases
1695 * where we are gazumped.
1696 */
1697 ret = i915_gem_object_wait_rendering__nonblocking(obj, NULL, !write);
1698 if (ret)
1699 goto unlock;
1700
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001701 /* Access to snoopable pages through the GTT is incoherent. */
1702 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev)) {
Chris Wilsonddeff6e2014-05-28 16:16:41 +01001703 ret = -EFAULT;
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001704 goto unlock;
1705 }
1706
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001707 /* Use a partial view if the object is bigger than the aperture. */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001708 if (obj->base.size >= ggtt->mappable_end &&
Joonas Lahtinene7ded2d2015-05-08 14:37:39 +03001709 obj->tiling_mode == I915_TILING_NONE) {
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001710 static const unsigned int chunk_size = 256; // 1 MiB
Joonas Lahtinene7ded2d2015-05-08 14:37:39 +03001711
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001712 memset(&view, 0, sizeof(view));
1713 view.type = I915_GGTT_VIEW_PARTIAL;
1714 view.params.partial.offset = rounddown(page_offset, chunk_size);
1715 view.params.partial.size =
1716 min_t(unsigned int,
1717 chunk_size,
1718 (vma->vm_end - vma->vm_start)/PAGE_SIZE -
1719 view.params.partial.offset);
1720 }
1721
1722 /* Now pin it into the GTT if needed */
1723 ret = i915_gem_object_ggtt_pin(obj, &view, 0, PIN_MAPPABLE);
Chris Wilsond9e86c02010-11-10 16:40:20 +00001724 if (ret)
1725 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001726
Chris Wilsonc9839302012-11-20 10:45:17 +00001727 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1728 if (ret)
1729 goto unpin;
1730
1731 ret = i915_gem_object_get_fence(obj);
1732 if (ret)
1733 goto unpin;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001734
Chris Wilsonb90b91d2014-06-10 12:14:40 +01001735 /* Finally, remap it using the new GTT offset */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001736 pfn = ggtt->mappable_base +
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001737 i915_gem_obj_ggtt_offset_view(obj, &view);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07001738 pfn >>= PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001739
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001740 if (unlikely(view.type == I915_GGTT_VIEW_PARTIAL)) {
1741 /* Overriding existing pages in partial view does not cause
1742 * us any trouble as TLBs are still valid because the fault
1743 * is due to userspace losing part of the mapping or never
1744 * having accessed it before (at this partials' range).
1745 */
1746 unsigned long base = vma->vm_start +
1747 (view.params.partial.offset << PAGE_SHIFT);
1748 unsigned int i;
Chris Wilsonb90b91d2014-06-10 12:14:40 +01001749
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001750 for (i = 0; i < view.params.partial.size; i++) {
1751 ret = vm_insert_pfn(vma, base + i * PAGE_SIZE, pfn + i);
Chris Wilsonb90b91d2014-06-10 12:14:40 +01001752 if (ret)
1753 break;
1754 }
1755
1756 obj->fault_mappable = true;
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001757 } else {
1758 if (!obj->fault_mappable) {
1759 unsigned long size = min_t(unsigned long,
1760 vma->vm_end - vma->vm_start,
1761 obj->base.size);
1762 int i;
1763
1764 for (i = 0; i < size >> PAGE_SHIFT; i++) {
1765 ret = vm_insert_pfn(vma,
1766 (unsigned long)vma->vm_start + i * PAGE_SIZE,
1767 pfn + i);
1768 if (ret)
1769 break;
1770 }
1771
1772 obj->fault_mappable = true;
1773 } else
1774 ret = vm_insert_pfn(vma,
1775 (unsigned long)vmf->virtual_address,
1776 pfn + page_offset);
1777 }
Chris Wilsonc9839302012-11-20 10:45:17 +00001778unpin:
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001779 i915_gem_object_ggtt_unpin_view(obj, &view);
Chris Wilsonc7150892009-09-23 00:43:56 +01001780unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001781 mutex_unlock(&dev->struct_mutex);
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001782out:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001783 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001784 case -EIO:
Daniel Vetter2232f032014-09-04 09:36:18 +02001785 /*
1786 * We eat errors when the gpu is terminally wedged to avoid
1787 * userspace unduly crashing (gl has no provisions for mmaps to
1788 * fail). But any other -EIO isn't ours (e.g. swap in failure)
1789 * and so needs to be reported.
1790 */
1791 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
Paulo Zanonif65c9162013-11-27 18:20:34 -02001792 ret = VM_FAULT_SIGBUS;
1793 break;
1794 }
Chris Wilson045e7692010-11-07 09:18:22 +00001795 case -EAGAIN:
Daniel Vetter571c6082013-09-12 17:57:28 +02001796 /*
1797 * EAGAIN means the gpu is hung and we'll wait for the error
1798 * handler to reset everything when re-faulting in
1799 * i915_mutex_lock_interruptible.
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001800 */
Chris Wilsonc7150892009-09-23 00:43:56 +01001801 case 0:
1802 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00001803 case -EINTR:
Dmitry Rogozhkine79e0fe2012-10-03 17:15:26 +03001804 case -EBUSY:
1805 /*
1806 * EBUSY is ok: this just means that another thread
1807 * already did the job.
1808 */
Paulo Zanonif65c9162013-11-27 18:20:34 -02001809 ret = VM_FAULT_NOPAGE;
1810 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001811 case -ENOMEM:
Paulo Zanonif65c9162013-11-27 18:20:34 -02001812 ret = VM_FAULT_OOM;
1813 break;
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02001814 case -ENOSPC:
Chris Wilson45d67812014-01-31 11:34:57 +00001815 case -EFAULT:
Paulo Zanonif65c9162013-11-27 18:20:34 -02001816 ret = VM_FAULT_SIGBUS;
1817 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001818 default:
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02001819 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
Paulo Zanonif65c9162013-11-27 18:20:34 -02001820 ret = VM_FAULT_SIGBUS;
1821 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001822 }
Paulo Zanonif65c9162013-11-27 18:20:34 -02001823
1824 intel_runtime_pm_put(dev_priv);
1825 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001826}
1827
1828/**
Chris Wilson901782b2009-07-10 08:18:50 +01001829 * i915_gem_release_mmap - remove physical page mappings
1830 * @obj: obj in question
1831 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001832 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001833 * relinquish ownership of the pages back to the system.
1834 *
1835 * It is vital that we remove the page mapping if we have mapped a tiled
1836 * object through the GTT and then lose the fence register due to
1837 * resource pressure. Similarly if the object has been moved out of the
1838 * aperture, than pages mapped into userspace must be revoked. Removing the
1839 * mapping will then trigger a page fault on the next user access, allowing
1840 * fixup by i915_gem_fault().
1841 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001842void
Chris Wilson05394f32010-11-08 19:18:58 +00001843i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01001844{
Chris Wilson349f2cc2016-04-13 17:35:12 +01001845 /* Serialisation between user GTT access and our code depends upon
1846 * revoking the CPU's PTE whilst the mutex is held. The next user
1847 * pagefault then has to wait until we release the mutex.
1848 */
1849 lockdep_assert_held(&obj->base.dev->struct_mutex);
1850
Chris Wilson6299f992010-11-24 12:23:44 +00001851 if (!obj->fault_mappable)
1852 return;
Chris Wilson901782b2009-07-10 08:18:50 +01001853
David Herrmann6796cb12014-01-03 14:24:19 +01001854 drm_vma_node_unmap(&obj->base.vma_node,
1855 obj->base.dev->anon_inode->i_mapping);
Chris Wilson349f2cc2016-04-13 17:35:12 +01001856
1857 /* Ensure that the CPU's PTE are revoked and there are not outstanding
1858 * memory transactions from userspace before we return. The TLB
1859 * flushing implied above by changing the PTE above *should* be
1860 * sufficient, an extra barrier here just provides us with a bit
1861 * of paranoid documentation about our requirement to serialise
1862 * memory writes before touching registers / GSM.
1863 */
1864 wmb();
1865
Chris Wilson6299f992010-11-24 12:23:44 +00001866 obj->fault_mappable = false;
Chris Wilson901782b2009-07-10 08:18:50 +01001867}
1868
Chris Wilsoneedd10f2014-06-16 08:57:44 +01001869void
1870i915_gem_release_all_mmaps(struct drm_i915_private *dev_priv)
1871{
1872 struct drm_i915_gem_object *obj;
1873
1874 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
1875 i915_gem_release_mmap(obj);
1876}
1877
Imre Deak0fa87792013-01-07 21:47:35 +02001878uint32_t
Chris Wilsone28f8712011-07-18 13:11:49 -07001879i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
Chris Wilson92b88ae2010-11-09 11:47:32 +00001880{
Chris Wilsone28f8712011-07-18 13:11:49 -07001881 uint32_t gtt_size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001882
1883 if (INTEL_INFO(dev)->gen >= 4 ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001884 tiling_mode == I915_TILING_NONE)
1885 return size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001886
1887 /* Previous chips need a power-of-two fence region when tiling */
Tvrtko Ursulin7e22dbb2016-05-10 10:57:06 +01001888 if (IS_GEN3(dev))
Chris Wilsone28f8712011-07-18 13:11:49 -07001889 gtt_size = 1024*1024;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001890 else
Chris Wilsone28f8712011-07-18 13:11:49 -07001891 gtt_size = 512*1024;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001892
Chris Wilsone28f8712011-07-18 13:11:49 -07001893 while (gtt_size < size)
1894 gtt_size <<= 1;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001895
Chris Wilsone28f8712011-07-18 13:11:49 -07001896 return gtt_size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001897}
1898
Jesse Barnesde151cf2008-11-12 10:03:55 -08001899/**
1900 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001901 * @dev: drm device
1902 * @size: object size
1903 * @tiling_mode: tiling mode
1904 * @fenced: is fenced alignemned required or not
Jesse Barnesde151cf2008-11-12 10:03:55 -08001905 *
1906 * Return the required GTT alignment for an object, taking into account
Daniel Vetter5e783302010-11-14 22:32:36 +01001907 * potential fence register mapping.
Jesse Barnesde151cf2008-11-12 10:03:55 -08001908 */
Imre Deakd865110c2013-01-07 21:47:33 +02001909uint32_t
1910i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
1911 int tiling_mode, bool fenced)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001912{
Jesse Barnesde151cf2008-11-12 10:03:55 -08001913 /*
1914 * Minimum alignment is 4k (GTT page size), but might be greater
1915 * if a fence register is needed for the object.
1916 */
Imre Deakd865110c2013-01-07 21:47:33 +02001917 if (INTEL_INFO(dev)->gen >= 4 || (!fenced && IS_G33(dev)) ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001918 tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001919 return 4096;
1920
1921 /*
1922 * Previous chips need to be aligned to the size of the smallest
1923 * fence register that can contain the object.
1924 */
Chris Wilsone28f8712011-07-18 13:11:49 -07001925 return i915_gem_get_gtt_size(dev, size, tiling_mode);
Chris Wilsona00b10c2010-09-24 21:15:47 +01001926}
1927
Chris Wilsond8cb5082012-08-11 15:41:03 +01001928static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
1929{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001930 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsond8cb5082012-08-11 15:41:03 +01001931 int ret;
1932
Daniel Vetterda494d72012-12-20 15:11:16 +01001933 dev_priv->mm.shrinker_no_lock_stealing = true;
1934
Chris Wilsond8cb5082012-08-11 15:41:03 +01001935 ret = drm_gem_create_mmap_offset(&obj->base);
1936 if (ret != -ENOSPC)
Daniel Vetterda494d72012-12-20 15:11:16 +01001937 goto out;
Chris Wilsond8cb5082012-08-11 15:41:03 +01001938
1939 /* Badly fragmented mmap space? The only way we can recover
1940 * space is by destroying unwanted objects. We can't randomly release
1941 * mmap_offsets as userspace expects them to be persistent for the
1942 * lifetime of the objects. The closest we can is to release the
1943 * offsets on purgeable objects by truncating it and marking it purged,
1944 * which prevents userspace from ever using that object again.
1945 */
Chris Wilson21ab4e72014-09-09 11:16:08 +01001946 i915_gem_shrink(dev_priv,
1947 obj->base.size >> PAGE_SHIFT,
1948 I915_SHRINK_BOUND |
1949 I915_SHRINK_UNBOUND |
1950 I915_SHRINK_PURGEABLE);
Chris Wilsond8cb5082012-08-11 15:41:03 +01001951 ret = drm_gem_create_mmap_offset(&obj->base);
1952 if (ret != -ENOSPC)
Daniel Vetterda494d72012-12-20 15:11:16 +01001953 goto out;
Chris Wilsond8cb5082012-08-11 15:41:03 +01001954
1955 i915_gem_shrink_all(dev_priv);
Daniel Vetterda494d72012-12-20 15:11:16 +01001956 ret = drm_gem_create_mmap_offset(&obj->base);
1957out:
1958 dev_priv->mm.shrinker_no_lock_stealing = false;
1959
1960 return ret;
Chris Wilsond8cb5082012-08-11 15:41:03 +01001961}
1962
1963static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
1964{
Chris Wilsond8cb5082012-08-11 15:41:03 +01001965 drm_gem_free_mmap_offset(&obj->base);
1966}
1967
Dave Airlieda6b51d2014-12-24 13:11:17 +10001968int
Dave Airlieff72145b2011-02-07 12:16:14 +10001969i915_gem_mmap_gtt(struct drm_file *file,
1970 struct drm_device *dev,
Dave Airlieda6b51d2014-12-24 13:11:17 +10001971 uint32_t handle,
Dave Airlieff72145b2011-02-07 12:16:14 +10001972 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001973{
Chris Wilson05394f32010-11-08 19:18:58 +00001974 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001975 int ret;
1976
Chris Wilson76c1dec2010-09-25 11:22:51 +01001977 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001978 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001979 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001980
Chris Wilson03ac0642016-07-20 13:31:51 +01001981 obj = i915_gem_object_lookup(file, handle);
1982 if (!obj) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001983 ret = -ENOENT;
1984 goto unlock;
1985 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001986
Chris Wilson05394f32010-11-08 19:18:58 +00001987 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonbd9b6a42014-02-10 09:03:50 +00001988 DRM_DEBUG("Attempting to mmap a purgeable buffer\n");
Chris Wilson8c99e572014-01-31 11:34:58 +00001989 ret = -EFAULT;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001990 goto out;
Chris Wilsonab182822009-09-22 18:46:17 +01001991 }
1992
Chris Wilsond8cb5082012-08-11 15:41:03 +01001993 ret = i915_gem_object_create_mmap_offset(obj);
1994 if (ret)
1995 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001996
David Herrmann0de23972013-07-24 21:07:52 +02001997 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001998
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001999out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01002000 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002001unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002002 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002003 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002004}
2005
Dave Airlieff72145b2011-02-07 12:16:14 +10002006/**
2007 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
2008 * @dev: DRM device
2009 * @data: GTT mapping ioctl data
2010 * @file: GEM object info
2011 *
2012 * Simply returns the fake offset to userspace so it can mmap it.
2013 * The mmap call will end up in drm_gem_mmap(), which will set things
2014 * up so we can get faults in the handler above.
2015 *
2016 * The fault handler will take care of binding the object into the GTT
2017 * (since it may have been evicted to make room for something), allocating
2018 * a fence register, and mapping the appropriate aperture address into
2019 * userspace.
2020 */
2021int
2022i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
2023 struct drm_file *file)
2024{
2025 struct drm_i915_gem_mmap_gtt *args = data;
2026
Dave Airlieda6b51d2014-12-24 13:11:17 +10002027 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
Dave Airlieff72145b2011-02-07 12:16:14 +10002028}
2029
Daniel Vetter225067e2012-08-20 10:23:20 +02002030/* Immediately discard the backing storage */
2031static void
2032i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01002033{
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002034 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02002035
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002036 if (obj->base.filp == NULL)
2037 return;
2038
Daniel Vetter225067e2012-08-20 10:23:20 +02002039 /* Our goal here is to return as much of the memory as
2040 * is possible back to the system as we are called from OOM.
2041 * To do this we must instruct the shmfs to drop all of its
2042 * backing pages, *now*.
Chris Wilsone5281cc2010-10-28 13:45:36 +01002043 */
Chris Wilson55372522014-03-25 13:23:06 +00002044 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
Daniel Vetter225067e2012-08-20 10:23:20 +02002045 obj->madv = __I915_MADV_PURGED;
Chris Wilsone5281cc2010-10-28 13:45:36 +01002046}
Chris Wilsone5281cc2010-10-28 13:45:36 +01002047
Chris Wilson55372522014-03-25 13:23:06 +00002048/* Try to discard unwanted pages */
2049static void
2050i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
Daniel Vetter225067e2012-08-20 10:23:20 +02002051{
Chris Wilson55372522014-03-25 13:23:06 +00002052 struct address_space *mapping;
2053
2054 switch (obj->madv) {
2055 case I915_MADV_DONTNEED:
2056 i915_gem_object_truncate(obj);
2057 case __I915_MADV_PURGED:
2058 return;
2059 }
2060
2061 if (obj->base.filp == NULL)
2062 return;
2063
2064 mapping = file_inode(obj->base.filp)->i_mapping,
2065 invalidate_mapping_pages(mapping, 0, (loff_t)-1);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002066}
2067
Chris Wilson5cdf5882010-09-27 15:51:07 +01002068static void
Chris Wilson05394f32010-11-08 19:18:58 +00002069i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002070{
Dave Gordon85d12252016-05-20 11:54:06 +01002071 struct sgt_iter sgt_iter;
2072 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002073 int ret;
Daniel Vetter1286ff72012-05-10 15:25:09 +02002074
Chris Wilson05394f32010-11-08 19:18:58 +00002075 BUG_ON(obj->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07002076
Chris Wilson6c085a72012-08-20 11:40:46 +02002077 ret = i915_gem_object_set_to_cpu_domain(obj, true);
Chris Wilsonf4457ae2016-04-13 17:35:08 +01002078 if (WARN_ON(ret)) {
Chris Wilson6c085a72012-08-20 11:40:46 +02002079 /* In the event of a disaster, abandon all caches and
2080 * hope for the best.
2081 */
Chris Wilson2c225692013-08-09 12:26:45 +01002082 i915_gem_clflush_object(obj, true);
Chris Wilson6c085a72012-08-20 11:40:46 +02002083 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
2084 }
2085
Imre Deake2273302015-07-09 12:59:05 +03002086 i915_gem_gtt_finish_object(obj);
2087
Daniel Vetter6dacfd22011-09-12 21:30:02 +02002088 if (i915_gem_object_needs_bit17_swizzle(obj))
Eric Anholt280b7132009-03-12 16:56:27 -07002089 i915_gem_object_save_bit_17_swizzle(obj);
2090
Chris Wilson05394f32010-11-08 19:18:58 +00002091 if (obj->madv == I915_MADV_DONTNEED)
2092 obj->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01002093
Dave Gordon85d12252016-05-20 11:54:06 +01002094 for_each_sgt_page(page, sgt_iter, obj->pages) {
Chris Wilson05394f32010-11-08 19:18:58 +00002095 if (obj->dirty)
Chris Wilson9da3da62012-06-01 15:20:22 +01002096 set_page_dirty(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002097
Chris Wilson05394f32010-11-08 19:18:58 +00002098 if (obj->madv == I915_MADV_WILLNEED)
Chris Wilson9da3da62012-06-01 15:20:22 +01002099 mark_page_accessed(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002100
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002101 put_page(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002102 }
Chris Wilson05394f32010-11-08 19:18:58 +00002103 obj->dirty = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002104
Chris Wilson9da3da62012-06-01 15:20:22 +01002105 sg_free_table(obj->pages);
2106 kfree(obj->pages);
Chris Wilson37e680a2012-06-07 15:38:42 +01002107}
2108
Chris Wilsondd624af2013-01-15 12:39:35 +00002109int
Chris Wilson37e680a2012-06-07 15:38:42 +01002110i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
2111{
2112 const struct drm_i915_gem_object_ops *ops = obj->ops;
2113
Chris Wilson2f745ad2012-09-04 21:02:58 +01002114 if (obj->pages == NULL)
Chris Wilson37e680a2012-06-07 15:38:42 +01002115 return 0;
2116
Chris Wilsona5570172012-09-04 21:02:54 +01002117 if (obj->pages_pin_count)
2118 return -EBUSY;
2119
Chris Wilson15717de2016-08-04 07:52:26 +01002120 GEM_BUG_ON(obj->bind_count);
Ben Widawsky3e123022013-07-31 17:00:04 -07002121
Chris Wilsona2165e32012-12-03 11:49:00 +00002122 /* ->put_pages might need to allocate memory for the bit17 swizzle
2123 * array, hence protect them from being reaped by removing them from gtt
2124 * lists early. */
Ben Widawsky35c20a62013-05-31 11:28:48 -07002125 list_del(&obj->global_list);
Chris Wilsona2165e32012-12-03 11:49:00 +00002126
Chris Wilson0a798eb2016-04-08 12:11:11 +01002127 if (obj->mapping) {
Chris Wilsonfb8621d2016-04-08 12:11:14 +01002128 if (is_vmalloc_addr(obj->mapping))
2129 vunmap(obj->mapping);
2130 else
2131 kunmap(kmap_to_page(obj->mapping));
Chris Wilson0a798eb2016-04-08 12:11:11 +01002132 obj->mapping = NULL;
2133 }
2134
Chris Wilson37e680a2012-06-07 15:38:42 +01002135 ops->put_pages(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00002136 obj->pages = NULL;
Chris Wilson6c085a72012-08-20 11:40:46 +02002137
Chris Wilson55372522014-03-25 13:23:06 +00002138 i915_gem_object_invalidate(obj);
Chris Wilson6c085a72012-08-20 11:40:46 +02002139
2140 return 0;
2141}
2142
Chris Wilson37e680a2012-06-07 15:38:42 +01002143static int
Chris Wilson6c085a72012-08-20 11:40:46 +02002144i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002145{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002146 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Eric Anholt673a3942008-07-30 12:06:12 -07002147 int page_count, i;
2148 struct address_space *mapping;
Chris Wilson9da3da62012-06-01 15:20:22 +01002149 struct sg_table *st;
2150 struct scatterlist *sg;
Dave Gordon85d12252016-05-20 11:54:06 +01002151 struct sgt_iter sgt_iter;
Eric Anholt673a3942008-07-30 12:06:12 -07002152 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002153 unsigned long last_pfn = 0; /* suppress gcc warning */
Imre Deake2273302015-07-09 12:59:05 +03002154 int ret;
Chris Wilson6c085a72012-08-20 11:40:46 +02002155 gfp_t gfp;
Eric Anholt673a3942008-07-30 12:06:12 -07002156
Chris Wilson6c085a72012-08-20 11:40:46 +02002157 /* Assert that the object is not currently in any GPU domain. As it
2158 * wasn't in the GTT, there shouldn't be any way it could have been in
2159 * a GPU cache
2160 */
2161 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2162 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
2163
Chris Wilson9da3da62012-06-01 15:20:22 +01002164 st = kmalloc(sizeof(*st), GFP_KERNEL);
2165 if (st == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002166 return -ENOMEM;
2167
Chris Wilson9da3da62012-06-01 15:20:22 +01002168 page_count = obj->base.size / PAGE_SIZE;
2169 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
Chris Wilson9da3da62012-06-01 15:20:22 +01002170 kfree(st);
2171 return -ENOMEM;
2172 }
2173
2174 /* Get the list of pages out of our struct file. They'll be pinned
2175 * at this point until we release them.
2176 *
2177 * Fail silently without starting the shrinker
2178 */
Al Viro496ad9a2013-01-23 17:07:38 -05002179 mapping = file_inode(obj->base.filp)->i_mapping;
Michal Hockoc62d2552015-11-06 16:28:49 -08002180 gfp = mapping_gfp_constraint(mapping, ~(__GFP_IO | __GFP_RECLAIM));
Mel Gormand0164ad2015-11-06 16:28:21 -08002181 gfp |= __GFP_NORETRY | __GFP_NOWARN;
Imre Deak90797e62013-02-18 19:28:03 +02002182 sg = st->sgl;
2183 st->nents = 0;
2184 for (i = 0; i < page_count; i++) {
Chris Wilson6c085a72012-08-20 11:40:46 +02002185 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
2186 if (IS_ERR(page)) {
Chris Wilson21ab4e72014-09-09 11:16:08 +01002187 i915_gem_shrink(dev_priv,
2188 page_count,
2189 I915_SHRINK_BOUND |
2190 I915_SHRINK_UNBOUND |
2191 I915_SHRINK_PURGEABLE);
Chris Wilson6c085a72012-08-20 11:40:46 +02002192 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
2193 }
2194 if (IS_ERR(page)) {
2195 /* We've tried hard to allocate the memory by reaping
2196 * our own buffer, now let the real VM do its job and
2197 * go down in flames if truly OOM.
2198 */
Chris Wilson6c085a72012-08-20 11:40:46 +02002199 i915_gem_shrink_all(dev_priv);
David Herrmannf461d1b2014-05-25 14:34:10 +02002200 page = shmem_read_mapping_page(mapping, i);
Imre Deake2273302015-07-09 12:59:05 +03002201 if (IS_ERR(page)) {
2202 ret = PTR_ERR(page);
Chris Wilson6c085a72012-08-20 11:40:46 +02002203 goto err_pages;
Imre Deake2273302015-07-09 12:59:05 +03002204 }
Chris Wilson6c085a72012-08-20 11:40:46 +02002205 }
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002206#ifdef CONFIG_SWIOTLB
2207 if (swiotlb_nr_tbl()) {
2208 st->nents++;
2209 sg_set_page(sg, page, PAGE_SIZE, 0);
2210 sg = sg_next(sg);
2211 continue;
2212 }
2213#endif
Imre Deak90797e62013-02-18 19:28:03 +02002214 if (!i || page_to_pfn(page) != last_pfn + 1) {
2215 if (i)
2216 sg = sg_next(sg);
2217 st->nents++;
2218 sg_set_page(sg, page, PAGE_SIZE, 0);
2219 } else {
2220 sg->length += PAGE_SIZE;
2221 }
2222 last_pfn = page_to_pfn(page);
Daniel Vetter3bbbe702013-10-07 17:15:45 -03002223
2224 /* Check that the i965g/gm workaround works. */
2225 WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
Eric Anholt673a3942008-07-30 12:06:12 -07002226 }
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002227#ifdef CONFIG_SWIOTLB
2228 if (!swiotlb_nr_tbl())
2229#endif
2230 sg_mark_end(sg);
Chris Wilson74ce6b62012-10-19 15:51:06 +01002231 obj->pages = st;
2232
Imre Deake2273302015-07-09 12:59:05 +03002233 ret = i915_gem_gtt_prepare_object(obj);
2234 if (ret)
2235 goto err_pages;
2236
Eric Anholt673a3942008-07-30 12:06:12 -07002237 if (i915_gem_object_needs_bit17_swizzle(obj))
2238 i915_gem_object_do_bit_17_swizzle(obj);
2239
Daniel Vetter656bfa32014-11-20 09:26:30 +01002240 if (obj->tiling_mode != I915_TILING_NONE &&
2241 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES)
2242 i915_gem_object_pin_pages(obj);
2243
Eric Anholt673a3942008-07-30 12:06:12 -07002244 return 0;
2245
2246err_pages:
Imre Deak90797e62013-02-18 19:28:03 +02002247 sg_mark_end(sg);
Dave Gordon85d12252016-05-20 11:54:06 +01002248 for_each_sgt_page(page, sgt_iter, st)
2249 put_page(page);
Chris Wilson9da3da62012-06-01 15:20:22 +01002250 sg_free_table(st);
2251 kfree(st);
Chris Wilson0820baf2014-03-25 13:23:03 +00002252
2253 /* shmemfs first checks if there is enough memory to allocate the page
2254 * and reports ENOSPC should there be insufficient, along with the usual
2255 * ENOMEM for a genuine allocation failure.
2256 *
2257 * We use ENOSPC in our driver to mean that we have run out of aperture
2258 * space and so want to translate the error from shmemfs back to our
2259 * usual understanding of ENOMEM.
2260 */
Imre Deake2273302015-07-09 12:59:05 +03002261 if (ret == -ENOSPC)
2262 ret = -ENOMEM;
2263
2264 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002265}
2266
Chris Wilson37e680a2012-06-07 15:38:42 +01002267/* Ensure that the associated pages are gathered from the backing storage
2268 * and pinned into our object. i915_gem_object_get_pages() may be called
2269 * multiple times before they are released by a single call to
2270 * i915_gem_object_put_pages() - once the pages are no longer referenced
2271 * either as a result of memory pressure (reaping pages under the shrinker)
2272 * or as the object is itself released.
2273 */
2274int
2275i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
2276{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002277 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson37e680a2012-06-07 15:38:42 +01002278 const struct drm_i915_gem_object_ops *ops = obj->ops;
2279 int ret;
2280
Chris Wilson2f745ad2012-09-04 21:02:58 +01002281 if (obj->pages)
Chris Wilson37e680a2012-06-07 15:38:42 +01002282 return 0;
2283
Chris Wilson43e28f02013-01-08 10:53:09 +00002284 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonbd9b6a42014-02-10 09:03:50 +00002285 DRM_DEBUG("Attempting to obtain a purgeable object\n");
Chris Wilson8c99e572014-01-31 11:34:58 +00002286 return -EFAULT;
Chris Wilson43e28f02013-01-08 10:53:09 +00002287 }
2288
Chris Wilsona5570172012-09-04 21:02:54 +01002289 BUG_ON(obj->pages_pin_count);
2290
Chris Wilson37e680a2012-06-07 15:38:42 +01002291 ret = ops->get_pages(obj);
2292 if (ret)
2293 return ret;
2294
Ben Widawsky35c20a62013-05-31 11:28:48 -07002295 list_add_tail(&obj->global_list, &dev_priv->mm.unbound_list);
Chris Wilsonee286372015-04-07 16:20:25 +01002296
2297 obj->get_page.sg = obj->pages->sgl;
2298 obj->get_page.last = 0;
2299
Chris Wilson37e680a2012-06-07 15:38:42 +01002300 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002301}
2302
Dave Gordondd6034c2016-05-20 11:54:04 +01002303/* The 'mapping' part of i915_gem_object_pin_map() below */
2304static void *i915_gem_object_map(const struct drm_i915_gem_object *obj)
2305{
2306 unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
2307 struct sg_table *sgt = obj->pages;
Dave Gordon85d12252016-05-20 11:54:06 +01002308 struct sgt_iter sgt_iter;
2309 struct page *page;
Dave Gordonb338fa42016-05-20 11:54:05 +01002310 struct page *stack_pages[32];
2311 struct page **pages = stack_pages;
Dave Gordondd6034c2016-05-20 11:54:04 +01002312 unsigned long i = 0;
2313 void *addr;
2314
2315 /* A single page can always be kmapped */
2316 if (n_pages == 1)
2317 return kmap(sg_page(sgt->sgl));
2318
Dave Gordonb338fa42016-05-20 11:54:05 +01002319 if (n_pages > ARRAY_SIZE(stack_pages)) {
2320 /* Too big for stack -- allocate temporary array instead */
2321 pages = drm_malloc_gfp(n_pages, sizeof(*pages), GFP_TEMPORARY);
2322 if (!pages)
2323 return NULL;
2324 }
Dave Gordondd6034c2016-05-20 11:54:04 +01002325
Dave Gordon85d12252016-05-20 11:54:06 +01002326 for_each_sgt_page(page, sgt_iter, sgt)
2327 pages[i++] = page;
Dave Gordondd6034c2016-05-20 11:54:04 +01002328
2329 /* Check that we have the expected number of pages */
2330 GEM_BUG_ON(i != n_pages);
2331
2332 addr = vmap(pages, n_pages, 0, PAGE_KERNEL);
2333
Dave Gordonb338fa42016-05-20 11:54:05 +01002334 if (pages != stack_pages)
2335 drm_free_large(pages);
Dave Gordondd6034c2016-05-20 11:54:04 +01002336
2337 return addr;
2338}
2339
2340/* get, pin, and map the pages of the object into kernel space */
Chris Wilson0a798eb2016-04-08 12:11:11 +01002341void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj)
2342{
2343 int ret;
2344
2345 lockdep_assert_held(&obj->base.dev->struct_mutex);
2346
2347 ret = i915_gem_object_get_pages(obj);
2348 if (ret)
2349 return ERR_PTR(ret);
2350
2351 i915_gem_object_pin_pages(obj);
2352
Dave Gordondd6034c2016-05-20 11:54:04 +01002353 if (!obj->mapping) {
2354 obj->mapping = i915_gem_object_map(obj);
2355 if (!obj->mapping) {
Chris Wilson0a798eb2016-04-08 12:11:11 +01002356 i915_gem_object_unpin_pages(obj);
2357 return ERR_PTR(-ENOMEM);
2358 }
2359 }
2360
2361 return obj->mapping;
2362}
2363
Ben Widawskye2d05a82013-09-24 09:57:58 -07002364void i915_vma_move_to_active(struct i915_vma *vma,
John Harrisonb2af0372015-05-29 17:43:50 +01002365 struct drm_i915_gem_request *req)
Ben Widawskye2d05a82013-09-24 09:57:58 -07002366{
Chris Wilsonb4716182015-04-27 13:41:17 +01002367 struct drm_i915_gem_object *obj = vma->obj;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002368 struct intel_engine_cs *engine;
John Harrisonb2af0372015-05-29 17:43:50 +01002369
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00002370 engine = i915_gem_request_get_engine(req);
Chris Wilsonb4716182015-04-27 13:41:17 +01002371
2372 /* Add a reference if we're newly entering the active list. */
2373 if (obj->active == 0)
Chris Wilson25dc5562016-07-20 13:31:52 +01002374 i915_gem_object_get(obj);
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00002375 obj->active |= intel_engine_flag(engine);
Chris Wilsonb4716182015-04-27 13:41:17 +01002376
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00002377 list_move_tail(&obj->engine_list[engine->id], &engine->active_list);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002378 i915_gem_request_assign(&obj->last_read_req[engine->id], req);
Chris Wilsonb4716182015-04-27 13:41:17 +01002379
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00002380 list_move_tail(&vma->vm_link, &vma->vm->active_list);
Ben Widawskye2d05a82013-09-24 09:57:58 -07002381}
2382
Chris Wilsoncaea7472010-11-12 13:53:37 +00002383static void
Chris Wilsonb4716182015-04-27 13:41:17 +01002384i915_gem_object_retire__write(struct drm_i915_gem_object *obj)
2385{
Chris Wilsond501b1d2016-04-13 17:35:02 +01002386 GEM_BUG_ON(obj->last_write_req == NULL);
2387 GEM_BUG_ON(!(obj->active & intel_engine_flag(obj->last_write_req->engine)));
Chris Wilsonb4716182015-04-27 13:41:17 +01002388
2389 i915_gem_request_assign(&obj->last_write_req, NULL);
Rodrigo Vivide152b62015-07-07 16:28:51 -07002390 intel_fb_obj_flush(obj, true, ORIGIN_CS);
Chris Wilsonb4716182015-04-27 13:41:17 +01002391}
2392
2393static void
Chris Wilson7e21d642016-07-27 09:07:29 +01002394i915_gem_object_retire__read(struct drm_i915_gem_object *obj, int idx)
Chris Wilsoncaea7472010-11-12 13:53:37 +00002395{
Ben Widawskyfeb822c2013-12-06 14:10:51 -08002396 struct i915_vma *vma;
Chris Wilsoncaea7472010-11-12 13:53:37 +00002397
Chris Wilson7e21d642016-07-27 09:07:29 +01002398 GEM_BUG_ON(obj->last_read_req[idx] == NULL);
2399 GEM_BUG_ON(!(obj->active & (1 << idx)));
Chris Wilsonb4716182015-04-27 13:41:17 +01002400
Chris Wilson7e21d642016-07-27 09:07:29 +01002401 list_del_init(&obj->engine_list[idx]);
2402 i915_gem_request_assign(&obj->last_read_req[idx], NULL);
Chris Wilsonb4716182015-04-27 13:41:17 +01002403
Chris Wilson7e21d642016-07-27 09:07:29 +01002404 if (obj->last_write_req && obj->last_write_req->engine->id == idx)
Chris Wilsonb4716182015-04-27 13:41:17 +01002405 i915_gem_object_retire__write(obj);
2406
Chris Wilson7e21d642016-07-27 09:07:29 +01002407 obj->active &= ~(1 << idx);
Chris Wilsonb4716182015-04-27 13:41:17 +01002408 if (obj->active)
2409 return;
Chris Wilson65ce3022012-07-20 12:41:02 +01002410
Chris Wilson6c246952015-07-27 10:26:26 +01002411 /* Bump our place on the bound list to keep it roughly in LRU order
2412 * so that we don't steal from recently used but inactive objects
2413 * (unless we are forced to ofc!)
2414 */
2415 list_move_tail(&obj->global_list,
2416 &to_i915(obj->base.dev)->mm.bound_list);
2417
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00002418 list_for_each_entry(vma, &obj->vma_list, obj_link) {
2419 if (!list_empty(&vma->vm_link))
2420 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
Ben Widawskyfeb822c2013-12-06 14:10:51 -08002421 }
Chris Wilsoncaea7472010-11-12 13:53:37 +00002422
John Harrison97b2a6a2014-11-24 18:49:26 +00002423 i915_gem_request_assign(&obj->last_fenced_req, NULL);
Chris Wilsonf8c417c2016-07-20 13:31:53 +01002424 i915_gem_object_put(obj);
Chris Wilsonc8725f32014-03-17 12:21:55 +00002425}
2426
Chris Wilson7b4d3a12016-07-04 08:08:37 +01002427static bool i915_context_is_banned(const struct i915_gem_context *ctx)
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002428{
Mika Kuoppala44e2c072014-01-30 16:01:15 +02002429 unsigned long elapsed;
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002430
Mika Kuoppala44e2c072014-01-30 16:01:15 +02002431 if (ctx->hang_stats.banned)
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002432 return true;
2433
Chris Wilson7b4d3a12016-07-04 08:08:37 +01002434 elapsed = get_seconds() - ctx->hang_stats.guilty_ts;
Chris Wilson676fa572014-12-24 08:13:39 -08002435 if (ctx->hang_stats.ban_period_seconds &&
2436 elapsed <= ctx->hang_stats.ban_period_seconds) {
Chris Wilson7b4d3a12016-07-04 08:08:37 +01002437 DRM_DEBUG("context hanging too fast, banning!\n");
2438 return true;
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002439 }
2440
2441 return false;
2442}
2443
Chris Wilson7b4d3a12016-07-04 08:08:37 +01002444static void i915_set_reset_status(struct i915_gem_context *ctx,
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002445 const bool guilty)
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002446{
Chris Wilson7b4d3a12016-07-04 08:08:37 +01002447 struct i915_ctx_hang_stats *hs = &ctx->hang_stats;
Mika Kuoppala44e2c072014-01-30 16:01:15 +02002448
2449 if (guilty) {
Chris Wilson7b4d3a12016-07-04 08:08:37 +01002450 hs->banned = i915_context_is_banned(ctx);
Mika Kuoppala44e2c072014-01-30 16:01:15 +02002451 hs->batch_active++;
2452 hs->guilty_ts = get_seconds();
2453 } else {
2454 hs->batch_pending++;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002455 }
2456}
2457
Chris Wilson8d9fc7f2014-02-25 17:11:23 +02002458struct drm_i915_gem_request *
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002459i915_gem_find_active_request(struct intel_engine_cs *engine)
Chris Wilson9375e442010-09-19 12:21:28 +01002460{
Chris Wilson4db080f2013-12-04 11:37:09 +00002461 struct drm_i915_gem_request *request;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002462
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002463 /* We are called by the error capture and reset at a random
2464 * point in time. In particular, note that neither is crucially
2465 * ordered with an interrupt. After a hang, the GPU is dead and we
2466 * assume that no more writes can happen (we waited long enough for
2467 * all writes that were in transaction to be flushed) - adding an
2468 * extra delay for a recent interrupt is pointless. Hence, we do
2469 * not need an engine->irq_seqno_barrier() before the seqno reads.
2470 */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002471 list_for_each_entry(request, &engine->request_list, list) {
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002472 if (i915_gem_request_completed(request))
Chris Wilson4db080f2013-12-04 11:37:09 +00002473 continue;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002474
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002475 return request;
Chris Wilson4db080f2013-12-04 11:37:09 +00002476 }
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002477
2478 return NULL;
2479}
2480
Chris Wilson7b4d3a12016-07-04 08:08:37 +01002481static void i915_gem_reset_engine_status(struct intel_engine_cs *engine)
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002482{
2483 struct drm_i915_gem_request *request;
2484 bool ring_hung;
2485
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002486 request = i915_gem_find_active_request(engine);
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002487 if (request == NULL)
2488 return;
2489
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002490 ring_hung = engine->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002491
Chris Wilson7b4d3a12016-07-04 08:08:37 +01002492 i915_set_reset_status(request->ctx, ring_hung);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002493 list_for_each_entry_continue(request, &engine->request_list, list)
Chris Wilson7b4d3a12016-07-04 08:08:37 +01002494 i915_set_reset_status(request->ctx, false);
Chris Wilson4db080f2013-12-04 11:37:09 +00002495}
2496
Chris Wilson7b4d3a12016-07-04 08:08:37 +01002497static void i915_gem_reset_engine_cleanup(struct intel_engine_cs *engine)
Chris Wilson4db080f2013-12-04 11:37:09 +00002498{
Chris Wilson7e37f882016-08-02 22:50:21 +01002499 struct intel_ring *ring;
Chris Wilson608c1a52015-09-03 13:01:40 +01002500
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002501 while (!list_empty(&engine->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00002502 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07002503
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002504 obj = list_first_entry(&engine->active_list,
Chris Wilson05394f32010-11-08 19:18:58 +00002505 struct drm_i915_gem_object,
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00002506 engine_list[engine->id]);
Eric Anholt673a3942008-07-30 12:06:12 -07002507
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002508 i915_gem_object_retire__read(obj, engine->id);
Eric Anholt673a3942008-07-30 12:06:12 -07002509 }
Ben Widawsky1d62bee2014-01-01 10:15:13 -08002510
Chris Wilsonc4b09302016-07-20 09:21:10 +01002511 /* Mark all pending requests as complete so that any concurrent
2512 * (lockless) lookup doesn't try and wait upon the request as we
2513 * reset it.
2514 */
Chris Wilson7e37f882016-08-02 22:50:21 +01002515 intel_engine_init_seqno(engine, engine->last_submitted_seqno);
Chris Wilsonc4b09302016-07-20 09:21:10 +01002516
Ben Widawsky1d62bee2014-01-01 10:15:13 -08002517 /*
Oscar Mateodcb4c122014-11-13 10:28:10 +00002518 * Clear the execlists queue up before freeing the requests, as those
2519 * are the ones that keep the context and ringbuffer backing objects
2520 * pinned in place.
2521 */
Oscar Mateodcb4c122014-11-13 10:28:10 +00002522
Tomas Elf7de1691a2015-10-19 16:32:32 +01002523 if (i915.enable_execlists) {
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +01002524 /* Ensure irq handler finishes or is cancelled. */
2525 tasklet_kill(&engine->irq_tasklet);
Mika Kuoppala1197b4f2015-01-13 11:32:24 +02002526
Tvrtko Ursuline39d42f2016-04-28 09:56:58 +01002527 intel_execlists_cancel_requests(engine);
Oscar Mateodcb4c122014-11-13 10:28:10 +00002528 }
2529
2530 /*
Ben Widawsky1d62bee2014-01-01 10:15:13 -08002531 * We must free the requests after all the corresponding objects have
2532 * been moved off active lists. Which is the same order as the normal
2533 * retire_requests function does. This is important if object hold
2534 * implicit references on things like e.g. ppgtt address spaces through
2535 * the request.
2536 */
Chris Wilson05235c52016-07-20 09:21:08 +01002537 if (!list_empty(&engine->request_list)) {
Ben Widawsky1d62bee2014-01-01 10:15:13 -08002538 struct drm_i915_gem_request *request;
2539
Chris Wilson05235c52016-07-20 09:21:08 +01002540 request = list_last_entry(&engine->request_list,
2541 struct drm_i915_gem_request,
2542 list);
Ben Widawsky1d62bee2014-01-01 10:15:13 -08002543
Chris Wilson05235c52016-07-20 09:21:08 +01002544 i915_gem_request_retire_upto(request);
Ben Widawsky1d62bee2014-01-01 10:15:13 -08002545 }
Chris Wilson608c1a52015-09-03 13:01:40 +01002546
2547 /* Having flushed all requests from all queues, we know that all
2548 * ringbuffers must now be empty. However, since we do not reclaim
2549 * all space when retiring the request (to prevent HEADs colliding
2550 * with rapid ringbuffer wraparound) the amount of available space
2551 * upon reset is less than when we start. Do one more pass over
2552 * all the ringbuffers to reset last_retired_head.
2553 */
Chris Wilson7e37f882016-08-02 22:50:21 +01002554 list_for_each_entry(ring, &engine->buffers, link) {
2555 ring->last_retired_head = ring->tail;
2556 intel_ring_update_space(ring);
Chris Wilson608c1a52015-09-03 13:01:40 +01002557 }
Chris Wilson2ed53a92016-04-07 07:29:11 +01002558
Chris Wilsonb913b332016-07-13 09:10:31 +01002559 engine->i915->gt.active_engines &= ~intel_engine_flag(engine);
Eric Anholt673a3942008-07-30 12:06:12 -07002560}
2561
Chris Wilson069efc12010-09-30 16:53:18 +01002562void i915_gem_reset(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07002563{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002564 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002565 struct intel_engine_cs *engine;
Eric Anholt673a3942008-07-30 12:06:12 -07002566
Chris Wilson4db080f2013-12-04 11:37:09 +00002567 /*
2568 * Before we free the objects from the requests, we need to inspect
2569 * them for finding the guilty party. As the requests only borrow
2570 * their reference to the objects, the inspection must be done first.
2571 */
Dave Gordonb4ac5af2016-03-24 11:20:38 +00002572 for_each_engine(engine, dev_priv)
Chris Wilson7b4d3a12016-07-04 08:08:37 +01002573 i915_gem_reset_engine_status(engine);
Chris Wilson4db080f2013-12-04 11:37:09 +00002574
Dave Gordonb4ac5af2016-03-24 11:20:38 +00002575 for_each_engine(engine, dev_priv)
Chris Wilson7b4d3a12016-07-04 08:08:37 +01002576 i915_gem_reset_engine_cleanup(engine);
Chris Wilsonb913b332016-07-13 09:10:31 +01002577 mod_delayed_work(dev_priv->wq, &dev_priv->gt.idle_work, 0);
Chris Wilsondfaae392010-09-22 10:31:52 +01002578
Ben Widawskyacce9ff2013-12-06 14:11:03 -08002579 i915_gem_context_reset(dev);
2580
Chris Wilson19b2dbd2013-06-12 10:15:12 +01002581 i915_gem_restore_fences(dev);
Chris Wilsonb4716182015-04-27 13:41:17 +01002582
2583 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07002584}
2585
2586/**
2587 * This function clears the request list as sequence numbers are passed.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01002588 * @engine: engine to retire requests on
Eric Anholt673a3942008-07-30 12:06:12 -07002589 */
Chris Wilson1cf0ba12014-05-05 09:07:33 +01002590void
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002591i915_gem_retire_requests_ring(struct intel_engine_cs *engine)
Eric Anholt673a3942008-07-30 12:06:12 -07002592{
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002593 WARN_ON(i915_verify_lists(engine->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07002594
Chris Wilson832a3aa2015-03-18 18:19:22 +00002595 /* Retire requests first as we use it above for the early return.
2596 * If we retire requests last, we may use a later seqno and so clear
2597 * the requests lists without clearing the active list, leading to
2598 * confusion.
Chris Wilsone9103032014-01-07 11:45:14 +00002599 */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002600 while (!list_empty(&engine->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002601 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07002602
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002603 request = list_first_entry(&engine->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07002604 struct drm_i915_gem_request,
2605 list);
Eric Anholt673a3942008-07-30 12:06:12 -07002606
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002607 if (!i915_gem_request_completed(request))
Eric Anholt673a3942008-07-30 12:06:12 -07002608 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01002609
Chris Wilson05235c52016-07-20 09:21:08 +01002610 i915_gem_request_retire_upto(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01002611 }
2612
Chris Wilson832a3aa2015-03-18 18:19:22 +00002613 /* Move any buffers on the active list that are no longer referenced
2614 * by the ringbuffer to the flushing/inactive lists as appropriate,
2615 * before we free the context associated with the requests.
2616 */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002617 while (!list_empty(&engine->active_list)) {
Chris Wilson832a3aa2015-03-18 18:19:22 +00002618 struct drm_i915_gem_object *obj;
2619
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002620 obj = list_first_entry(&engine->active_list,
2621 struct drm_i915_gem_object,
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00002622 engine_list[engine->id]);
Chris Wilson832a3aa2015-03-18 18:19:22 +00002623
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002624 if (!list_empty(&obj->last_read_req[engine->id]->list))
Chris Wilson832a3aa2015-03-18 18:19:22 +00002625 break;
2626
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002627 i915_gem_object_retire__read(obj, engine->id);
Chris Wilson832a3aa2015-03-18 18:19:22 +00002628 }
2629
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002630 WARN_ON(i915_verify_lists(engine->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07002631}
2632
Chris Wilson67d97da2016-07-04 08:08:31 +01002633void i915_gem_retire_requests(struct drm_i915_private *dev_priv)
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002634{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002635 struct intel_engine_cs *engine;
Chris Wilson67d97da2016-07-04 08:08:31 +01002636
Chris Wilson91c8a322016-07-05 10:40:23 +01002637 lockdep_assert_held(&dev_priv->drm.struct_mutex);
Chris Wilson67d97da2016-07-04 08:08:31 +01002638
2639 if (dev_priv->gt.active_engines == 0)
2640 return;
2641
2642 GEM_BUG_ON(!dev_priv->gt.awake);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002643
Dave Gordonb4ac5af2016-03-24 11:20:38 +00002644 for_each_engine(engine, dev_priv) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002645 i915_gem_retire_requests_ring(engine);
Chris Wilson67d97da2016-07-04 08:08:31 +01002646 if (list_empty(&engine->request_list))
2647 dev_priv->gt.active_engines &= ~intel_engine_flag(engine);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002648 }
2649
Chris Wilson67d97da2016-07-04 08:08:31 +01002650 if (dev_priv->gt.active_engines == 0)
Chris Wilson1b51bce2016-07-04 08:08:32 +01002651 queue_delayed_work(dev_priv->wq,
2652 &dev_priv->gt.idle_work,
2653 msecs_to_jiffies(100));
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002654}
2655
Daniel Vetter75ef9da2010-08-21 00:25:16 +02002656static void
Eric Anholt673a3942008-07-30 12:06:12 -07002657i915_gem_retire_work_handler(struct work_struct *work)
2658{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002659 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01002660 container_of(work, typeof(*dev_priv), gt.retire_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01002661 struct drm_device *dev = &dev_priv->drm;
Eric Anholt673a3942008-07-30 12:06:12 -07002662
Chris Wilson891b48c2010-09-29 12:26:37 +01002663 /* Come back later if the device is busy... */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002664 if (mutex_trylock(&dev->struct_mutex)) {
Chris Wilson67d97da2016-07-04 08:08:31 +01002665 i915_gem_retire_requests(dev_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002666 mutex_unlock(&dev->struct_mutex);
2667 }
Chris Wilson67d97da2016-07-04 08:08:31 +01002668
2669 /* Keep the retire handler running until we are finally idle.
2670 * We do not need to do this test under locking as in the worst-case
2671 * we queue the retire worker once too often.
2672 */
Chris Wilsonc9615612016-07-09 10:12:06 +01002673 if (READ_ONCE(dev_priv->gt.awake)) {
2674 i915_queue_hangcheck(dev_priv);
Chris Wilson67d97da2016-07-04 08:08:31 +01002675 queue_delayed_work(dev_priv->wq,
2676 &dev_priv->gt.retire_work,
Chris Wilsonbcb45082012-10-05 17:02:57 +01002677 round_jiffies_up_relative(HZ));
Chris Wilsonc9615612016-07-09 10:12:06 +01002678 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002679}
Chris Wilson891b48c2010-09-29 12:26:37 +01002680
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002681static void
2682i915_gem_idle_work_handler(struct work_struct *work)
2683{
2684 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01002685 container_of(work, typeof(*dev_priv), gt.idle_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01002686 struct drm_device *dev = &dev_priv->drm;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00002687 struct intel_engine_cs *engine;
Chris Wilson67d97da2016-07-04 08:08:31 +01002688 unsigned int stuck_engines;
2689 bool rearm_hangcheck;
2690
2691 if (!READ_ONCE(dev_priv->gt.awake))
2692 return;
2693
2694 if (READ_ONCE(dev_priv->gt.active_engines))
2695 return;
2696
2697 rearm_hangcheck =
2698 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
2699
2700 if (!mutex_trylock(&dev->struct_mutex)) {
2701 /* Currently busy, come back later */
2702 mod_delayed_work(dev_priv->wq,
2703 &dev_priv->gt.idle_work,
2704 msecs_to_jiffies(50));
2705 goto out_rearm;
2706 }
2707
2708 if (dev_priv->gt.active_engines)
2709 goto out_unlock;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002710
Dave Gordonb4ac5af2016-03-24 11:20:38 +00002711 for_each_engine(engine, dev_priv)
Chris Wilson67d97da2016-07-04 08:08:31 +01002712 i915_gem_batch_pool_fini(&engine->batch_pool);
Zou Nan hai852835f2010-05-21 09:08:56 +08002713
Chris Wilson67d97da2016-07-04 08:08:31 +01002714 GEM_BUG_ON(!dev_priv->gt.awake);
2715 dev_priv->gt.awake = false;
2716 rearm_hangcheck = false;
Daniel Vetter30ecad72015-12-09 09:29:36 +01002717
Chris Wilson2529d572016-07-24 10:10:20 +01002718 /* As we have disabled hangcheck, we need to unstick any waiters still
2719 * hanging around. However, as we may be racing against the interrupt
2720 * handler or the waiters themselves, we skip enabling the fake-irq.
2721 */
Chris Wilson67d97da2016-07-04 08:08:31 +01002722 stuck_engines = intel_kick_waiters(dev_priv);
Chris Wilson2529d572016-07-24 10:10:20 +01002723 if (unlikely(stuck_engines))
2724 DRM_DEBUG_DRIVER("kicked stuck waiters (%x)...missed irq?\n",
2725 stuck_engines);
Chris Wilson35c94182015-04-07 16:20:37 +01002726
Chris Wilson67d97da2016-07-04 08:08:31 +01002727 if (INTEL_GEN(dev_priv) >= 6)
2728 gen6_rps_idle(dev_priv);
2729 intel_runtime_pm_put(dev_priv);
2730out_unlock:
2731 mutex_unlock(&dev->struct_mutex);
Chris Wilson35c94182015-04-07 16:20:37 +01002732
Chris Wilson67d97da2016-07-04 08:08:31 +01002733out_rearm:
2734 if (rearm_hangcheck) {
2735 GEM_BUG_ON(!dev_priv->gt.awake);
2736 i915_queue_hangcheck(dev_priv);
Chris Wilson35c94182015-04-07 16:20:37 +01002737 }
Eric Anholt673a3942008-07-30 12:06:12 -07002738}
2739
Ben Widawsky5816d642012-04-11 11:18:19 -07002740/**
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002741 * Ensures that an object will eventually get non-busy by flushing any required
2742 * write domains, emitting any outstanding lazy request and retiring and
2743 * completed requests.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01002744 * @obj: object to flush
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002745 */
2746static int
2747i915_gem_object_flush_active(struct drm_i915_gem_object *obj)
2748{
John Harrisona5ac0f92015-05-29 17:44:15 +01002749 int i;
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002750
Chris Wilsonb4716182015-04-27 13:41:17 +01002751 if (!obj->active)
2752 return 0;
John Harrison41c52412014-11-24 18:49:43 +00002753
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00002754 for (i = 0; i < I915_NUM_ENGINES; i++) {
Chris Wilsonb4716182015-04-27 13:41:17 +01002755 struct drm_i915_gem_request *req;
2756
2757 req = obj->last_read_req[i];
2758 if (req == NULL)
2759 continue;
2760
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002761 if (i915_gem_request_completed(req))
Chris Wilsonb4716182015-04-27 13:41:17 +01002762 i915_gem_object_retire__read(obj, i);
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002763 }
2764
2765 return 0;
2766}
2767
2768/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002769 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01002770 * @dev: drm device pointer
2771 * @data: ioctl data blob
2772 * @file: drm file pointer
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002773 *
2774 * Returns 0 if successful, else an error is returned with the remaining time in
2775 * the timeout parameter.
2776 * -ETIME: object is still busy after timeout
2777 * -ERESTARTSYS: signal interrupted the wait
2778 * -ENONENT: object doesn't exist
2779 * Also possible, but rare:
2780 * -EAGAIN: GPU wedged
2781 * -ENOMEM: damn
2782 * -ENODEV: Internal IRQ fail
2783 * -E?: The add request failed
2784 *
2785 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
2786 * non-zero timeout parameter the wait ioctl will wait for the given number of
2787 * nanoseconds on an object becoming unbusy. Since the wait itself does so
2788 * without holding struct_mutex the object may become re-busied before this
2789 * function completes. A similar but shorter * race condition exists in the busy
2790 * ioctl
2791 */
2792int
2793i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
2794{
2795 struct drm_i915_gem_wait *args = data;
2796 struct drm_i915_gem_object *obj;
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00002797 struct drm_i915_gem_request *req[I915_NUM_ENGINES];
Chris Wilsonb4716182015-04-27 13:41:17 +01002798 int i, n = 0;
2799 int ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002800
Daniel Vetter11b5d512014-09-29 15:31:26 +02002801 if (args->flags != 0)
2802 return -EINVAL;
2803
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002804 ret = i915_mutex_lock_interruptible(dev);
2805 if (ret)
2806 return ret;
2807
Chris Wilson03ac0642016-07-20 13:31:51 +01002808 obj = i915_gem_object_lookup(file, args->bo_handle);
2809 if (!obj) {
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002810 mutex_unlock(&dev->struct_mutex);
2811 return -ENOENT;
2812 }
2813
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002814 /* Need to make sure the object gets inactive eventually. */
2815 ret = i915_gem_object_flush_active(obj);
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002816 if (ret)
2817 goto out;
2818
Chris Wilsonb4716182015-04-27 13:41:17 +01002819 if (!obj->active)
John Harrison97b2a6a2014-11-24 18:49:26 +00002820 goto out;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002821
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002822 /* Do this after OLR check to make sure we make forward progress polling
Chris Wilson762e4582015-03-04 18:09:26 +00002823 * on this IOCTL with a timeout == 0 (like busy ioctl)
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002824 */
Chris Wilson762e4582015-03-04 18:09:26 +00002825 if (args->timeout_ns == 0) {
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002826 ret = -ETIME;
2827 goto out;
2828 }
2829
Chris Wilsonf8c417c2016-07-20 13:31:53 +01002830 i915_gem_object_put(obj);
Chris Wilsonb4716182015-04-27 13:41:17 +01002831
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00002832 for (i = 0; i < I915_NUM_ENGINES; i++) {
Chris Wilsonb4716182015-04-27 13:41:17 +01002833 if (obj->last_read_req[i] == NULL)
2834 continue;
2835
Chris Wilsone8a261e2016-07-20 13:31:49 +01002836 req[n++] = i915_gem_request_get(obj->last_read_req[i]);
Chris Wilsonb4716182015-04-27 13:41:17 +01002837 }
2838
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002839 mutex_unlock(&dev->struct_mutex);
2840
Chris Wilsonb4716182015-04-27 13:41:17 +01002841 for (i = 0; i < n; i++) {
2842 if (ret == 0)
Chris Wilson299259a2016-04-13 17:35:06 +01002843 ret = __i915_wait_request(req[i], true,
Chris Wilsonb4716182015-04-27 13:41:17 +01002844 args->timeout_ns > 0 ? &args->timeout_ns : NULL,
Chris Wilsonb6aa0872015-12-02 09:13:46 +00002845 to_rps_client(file));
Chris Wilsone8a261e2016-07-20 13:31:49 +01002846 i915_gem_request_put(req[i]);
Chris Wilsonb4716182015-04-27 13:41:17 +01002847 }
John Harrisonff865882014-11-24 18:49:28 +00002848 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002849
2850out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01002851 i915_gem_object_put(obj);
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002852 mutex_unlock(&dev->struct_mutex);
2853 return ret;
2854}
2855
Chris Wilsonb4716182015-04-27 13:41:17 +01002856static int
2857__i915_gem_object_sync(struct drm_i915_gem_object *obj,
Chris Wilson8e637172016-08-02 22:50:26 +01002858 struct drm_i915_gem_request *to,
2859 struct drm_i915_gem_request *from)
Chris Wilsonb4716182015-04-27 13:41:17 +01002860{
Chris Wilsonb4716182015-04-27 13:41:17 +01002861 int ret;
2862
Chris Wilson8e637172016-08-02 22:50:26 +01002863 if (to->engine == from->engine)
Chris Wilsonb4716182015-04-27 13:41:17 +01002864 return 0;
2865
Chris Wilson8e637172016-08-02 22:50:26 +01002866 if (i915_gem_request_completed(from))
Chris Wilsonb4716182015-04-27 13:41:17 +01002867 return 0;
2868
Chris Wilson39df9192016-07-20 13:31:57 +01002869 if (!i915.semaphores) {
Chris Wilson8e637172016-08-02 22:50:26 +01002870 ret = __i915_wait_request(from,
2871 from->i915->mm.interruptible,
Chris Wilsona6f766f2015-04-27 13:41:20 +01002872 NULL,
Chris Wilson197be2a2016-07-20 09:21:13 +01002873 NO_WAITBOOST);
Chris Wilsonb4716182015-04-27 13:41:17 +01002874 if (ret)
2875 return ret;
2876
Chris Wilson8e637172016-08-02 22:50:26 +01002877 i915_gem_object_retire_request(obj, from);
Chris Wilsonb4716182015-04-27 13:41:17 +01002878 } else {
Chris Wilson8e637172016-08-02 22:50:26 +01002879 int idx = intel_engine_sync_index(from->engine, to->engine);
Chris Wilsonddf07be2016-08-02 22:50:39 +01002880 if (from->fence.seqno <= from->engine->semaphore.sync_seqno[idx])
Chris Wilsonb4716182015-04-27 13:41:17 +01002881 return 0;
2882
Chris Wilson8e637172016-08-02 22:50:26 +01002883 trace_i915_gem_ring_sync_to(to, from);
Chris Wilsonddf07be2016-08-02 22:50:39 +01002884 ret = to->engine->semaphore.sync_to(to, from);
Chris Wilsonb4716182015-04-27 13:41:17 +01002885 if (ret)
2886 return ret;
2887
Chris Wilsonddf07be2016-08-02 22:50:39 +01002888 from->engine->semaphore.sync_seqno[idx] = from->fence.seqno;
Chris Wilsonb4716182015-04-27 13:41:17 +01002889 }
2890
2891 return 0;
2892}
2893
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002894/**
Ben Widawsky5816d642012-04-11 11:18:19 -07002895 * i915_gem_object_sync - sync an object to a ring.
2896 *
2897 * @obj: object which may be in use on another ring.
Chris Wilson8e637172016-08-02 22:50:26 +01002898 * @to: request we are wishing to use
Ben Widawsky5816d642012-04-11 11:18:19 -07002899 *
2900 * This code is meant to abstract object synchronization with the GPU.
Chris Wilson8e637172016-08-02 22:50:26 +01002901 * Conceptually we serialise writes between engines inside the GPU.
2902 * We only allow one engine to write into a buffer at any time, but
2903 * multiple readers. To ensure each has a coherent view of memory, we must:
Chris Wilsonb4716182015-04-27 13:41:17 +01002904 *
2905 * - If there is an outstanding write request to the object, the new
2906 * request must wait for it to complete (either CPU or in hw, requests
2907 * on the same ring will be naturally ordered).
2908 *
2909 * - If we are a write request (pending_write_domain is set), the new
2910 * request must wait for outstanding read requests to complete.
Ben Widawsky5816d642012-04-11 11:18:19 -07002911 *
2912 * Returns 0 if successful, else propagates up the lower layer error.
2913 */
Ben Widawsky2911a352012-04-05 14:47:36 -07002914int
2915i915_gem_object_sync(struct drm_i915_gem_object *obj,
Chris Wilson8e637172016-08-02 22:50:26 +01002916 struct drm_i915_gem_request *to)
Ben Widawsky2911a352012-04-05 14:47:36 -07002917{
Chris Wilsonb4716182015-04-27 13:41:17 +01002918 const bool readonly = obj->base.pending_write_domain == 0;
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00002919 struct drm_i915_gem_request *req[I915_NUM_ENGINES];
Chris Wilsonb4716182015-04-27 13:41:17 +01002920 int ret, i, n;
Ben Widawsky2911a352012-04-05 14:47:36 -07002921
Chris Wilsonb4716182015-04-27 13:41:17 +01002922 if (!obj->active)
Ben Widawsky2911a352012-04-05 14:47:36 -07002923 return 0;
2924
Chris Wilsonb4716182015-04-27 13:41:17 +01002925 n = 0;
2926 if (readonly) {
2927 if (obj->last_write_req)
2928 req[n++] = obj->last_write_req;
2929 } else {
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00002930 for (i = 0; i < I915_NUM_ENGINES; i++)
Chris Wilsonb4716182015-04-27 13:41:17 +01002931 if (obj->last_read_req[i])
2932 req[n++] = obj->last_read_req[i];
2933 }
2934 for (i = 0; i < n; i++) {
Chris Wilson8e637172016-08-02 22:50:26 +01002935 ret = __i915_gem_object_sync(obj, to, req[i]);
Chris Wilsonb4716182015-04-27 13:41:17 +01002936 if (ret)
2937 return ret;
2938 }
Ben Widawsky2911a352012-04-05 14:47:36 -07002939
Chris Wilsonb4716182015-04-27 13:41:17 +01002940 return 0;
Ben Widawsky2911a352012-04-05 14:47:36 -07002941}
2942
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002943static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
2944{
2945 u32 old_write_domain, old_read_domains;
2946
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002947 /* Force a pagefault for domain tracking on next user access */
2948 i915_gem_release_mmap(obj);
2949
Keith Packardb97c3d92011-06-24 21:02:59 -07002950 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
2951 return;
2952
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002953 old_read_domains = obj->base.read_domains;
2954 old_write_domain = obj->base.write_domain;
2955
2956 obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
2957 obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
2958
2959 trace_i915_gem_object_change_domain(obj,
2960 old_read_domains,
2961 old_write_domain);
2962}
2963
Chris Wilson8ef85612016-04-28 09:56:39 +01002964static void __i915_vma_iounmap(struct i915_vma *vma)
2965{
2966 GEM_BUG_ON(vma->pin_count);
2967
2968 if (vma->iomap == NULL)
2969 return;
2970
2971 io_mapping_unmap(vma->iomap);
2972 vma->iomap = NULL;
2973}
2974
Tvrtko Ursuline9f24d52015-10-05 13:26:36 +01002975static int __i915_vma_unbind(struct i915_vma *vma, bool wait)
Eric Anholt673a3942008-07-30 12:06:12 -07002976{
Ben Widawsky07fe0b12013-07-31 17:00:10 -07002977 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson43e28f02013-01-08 10:53:09 +00002978 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002979
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00002980 if (list_empty(&vma->obj_link))
Eric Anholt673a3942008-07-30 12:06:12 -07002981 return 0;
2982
Daniel Vetter0ff501c2013-08-29 19:50:31 +02002983 if (!drm_mm_node_allocated(&vma->node)) {
2984 i915_gem_vma_destroy(vma);
Daniel Vetter0ff501c2013-08-29 19:50:31 +02002985 return 0;
2986 }
Ben Widawsky433544b2013-08-13 18:09:06 -07002987
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08002988 if (vma->pin_count)
Chris Wilson31d8d652012-05-24 19:11:20 +01002989 return -EBUSY;
Eric Anholt673a3942008-07-30 12:06:12 -07002990
Chris Wilson15717de2016-08-04 07:52:26 +01002991 GEM_BUG_ON(obj->bind_count == 0);
2992 GEM_BUG_ON(!obj->pages);
Chris Wilsonc4670ad2012-08-20 10:23:27 +01002993
Tvrtko Ursuline9f24d52015-10-05 13:26:36 +01002994 if (wait) {
2995 ret = i915_gem_object_wait_rendering(obj, false);
2996 if (ret)
2997 return ret;
2998 }
Chris Wilsona8198ee2011-04-13 22:04:09 +01002999
Chris Wilson596c5922016-02-26 11:03:20 +00003000 if (vma->is_ggtt && vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL) {
Daniel Vetter8b1bc9b2014-02-14 14:06:07 +01003001 i915_gem_object_finish_gtt(obj);
Chris Wilsona8198ee2011-04-13 22:04:09 +01003002
Daniel Vetter8b1bc9b2014-02-14 14:06:07 +01003003 /* release the fence reg _after_ flushing */
3004 ret = i915_gem_object_put_fence(obj);
3005 if (ret)
3006 return ret;
Chris Wilson8ef85612016-04-28 09:56:39 +01003007
3008 __i915_vma_iounmap(vma);
Daniel Vetter8b1bc9b2014-02-14 14:06:07 +01003009 }
Daniel Vetter96b47b62009-12-15 17:50:00 +01003010
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003011 trace_i915_vma_unbind(vma);
Chris Wilsondb53a302011-02-03 11:57:46 +00003012
Daniel Vetter777dc5b2015-04-14 17:35:12 +02003013 vma->vm->unbind_vma(vma);
Mika Kuoppala5e562f12015-04-30 11:02:31 +03003014 vma->bound = 0;
Ben Widawsky6f65e292013-12-06 14:10:56 -08003015
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003016 list_del_init(&vma->vm_link);
Chris Wilson596c5922016-02-26 11:03:20 +00003017 if (vma->is_ggtt) {
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003018 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL) {
3019 obj->map_and_fenceable = false;
3020 } else if (vma->ggtt_view.pages) {
3021 sg_free_table(vma->ggtt_view.pages);
3022 kfree(vma->ggtt_view.pages);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003023 }
Chris Wilson016a65a2015-06-11 08:06:08 +01003024 vma->ggtt_view.pages = NULL;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003025 }
Eric Anholt673a3942008-07-30 12:06:12 -07003026
Ben Widawsky2f633152013-07-17 12:19:03 -07003027 drm_mm_remove_node(&vma->node);
3028 i915_gem_vma_destroy(vma);
3029
3030 /* Since the unbound list is global, only move to that list if
Daniel Vetterb93dab62013-08-26 11:23:47 +02003031 * no more VMAs exist. */
Chris Wilson15717de2016-08-04 07:52:26 +01003032 if (--obj->bind_count == 0)
3033 list_move_tail(&obj->global_list,
3034 &to_i915(obj->base.dev)->mm.unbound_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003035
Chris Wilson70903c32013-12-04 09:59:09 +00003036 /* And finally now the object is completely decoupled from this vma,
3037 * we can drop its hold on the backing storage and allow it to be
3038 * reaped by the shrinker.
3039 */
3040 i915_gem_object_unpin_pages(obj);
3041
Chris Wilson88241782011-01-07 17:09:48 +00003042 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00003043}
3044
Tvrtko Ursuline9f24d52015-10-05 13:26:36 +01003045int i915_vma_unbind(struct i915_vma *vma)
3046{
3047 return __i915_vma_unbind(vma, true);
3048}
3049
3050int __i915_vma_unbind_no_wait(struct i915_vma *vma)
3051{
3052 return __i915_vma_unbind(vma, false);
3053}
3054
Chris Wilson6e5a5be2016-06-24 14:55:57 +01003055int i915_gem_wait_for_idle(struct drm_i915_private *dev_priv)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003056{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003057 struct intel_engine_cs *engine;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003058 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003059
Chris Wilson91c8a322016-07-05 10:40:23 +01003060 lockdep_assert_held(&dev_priv->drm.struct_mutex);
Chris Wilson6e5a5be2016-06-24 14:55:57 +01003061
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003062 for_each_engine(engine, dev_priv) {
Chris Wilson62e63002016-06-24 14:55:52 +01003063 if (engine->last_context == NULL)
3064 continue;
3065
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00003066 ret = intel_engine_idle(engine);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003067 if (ret)
3068 return ret;
3069 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003070
Chris Wilsonb4716182015-04-27 13:41:17 +01003071 WARN_ON(i915_verify_lists(dev));
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01003072 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003073}
3074
Chris Wilson4144f9b2014-09-11 08:43:48 +01003075static bool i915_gem_valid_gtt_space(struct i915_vma *vma,
Chris Wilson42d6ab42012-07-26 11:49:32 +01003076 unsigned long cache_level)
3077{
Chris Wilson4144f9b2014-09-11 08:43:48 +01003078 struct drm_mm_node *gtt_space = &vma->node;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003079 struct drm_mm_node *other;
3080
Chris Wilson4144f9b2014-09-11 08:43:48 +01003081 /*
3082 * On some machines we have to be careful when putting differing types
3083 * of snoopable memory together to avoid the prefetcher crossing memory
3084 * domains and dying. During vm initialisation, we decide whether or not
3085 * these constraints apply and set the drm_mm.color_adjust
3086 * appropriately.
Chris Wilson42d6ab42012-07-26 11:49:32 +01003087 */
Chris Wilson4144f9b2014-09-11 08:43:48 +01003088 if (vma->vm->mm.color_adjust == NULL)
Chris Wilson42d6ab42012-07-26 11:49:32 +01003089 return true;
3090
Ben Widawskyc6cfb322013-07-05 14:41:06 -07003091 if (!drm_mm_node_allocated(gtt_space))
Chris Wilson42d6ab42012-07-26 11:49:32 +01003092 return true;
3093
3094 if (list_empty(&gtt_space->node_list))
3095 return true;
3096
3097 other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
3098 if (other->allocated && !other->hole_follows && other->color != cache_level)
3099 return false;
3100
3101 other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
3102 if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
3103 return false;
3104
3105 return true;
3106}
3107
Jesse Barnesde151cf2008-11-12 10:03:55 -08003108/**
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003109 * Finds free space in the GTT aperture and binds the object or a view of it
3110 * there.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003111 * @obj: object to bind
3112 * @vm: address space to bind into
3113 * @ggtt_view: global gtt view if applicable
3114 * @alignment: requested alignment
3115 * @flags: mask of PIN_* flags to use
Eric Anholt673a3942008-07-30 12:06:12 -07003116 */
Daniel Vetter262de142014-02-14 14:01:20 +01003117static struct i915_vma *
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003118i915_gem_object_bind_to_vm(struct drm_i915_gem_object *obj,
3119 struct i915_address_space *vm,
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003120 const struct i915_ggtt_view *ggtt_view,
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003121 unsigned alignment,
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003122 uint64_t flags)
Eric Anholt673a3942008-07-30 12:06:12 -07003123{
Chris Wilson05394f32010-11-08 19:18:58 +00003124 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003125 struct drm_i915_private *dev_priv = to_i915(dev);
3126 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Michel Thierry65bd3422015-07-29 17:23:58 +01003127 u32 fence_alignment, unfenced_alignment;
Michel Thierry101b5062015-10-01 13:33:57 +01003128 u32 search_flag, alloc_flag;
3129 u64 start, end;
Michel Thierry65bd3422015-07-29 17:23:58 +01003130 u64 size, fence_size;
Ben Widawsky2f633152013-07-17 12:19:03 -07003131 struct i915_vma *vma;
Chris Wilson07f73f62009-09-14 16:50:30 +01003132 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003133
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003134 if (i915_is_ggtt(vm)) {
3135 u32 view_size;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003136
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003137 if (WARN_ON(!ggtt_view))
3138 return ERR_PTR(-EINVAL);
3139
3140 view_size = i915_ggtt_view_size(obj, ggtt_view);
3141
3142 fence_size = i915_gem_get_gtt_size(dev,
3143 view_size,
3144 obj->tiling_mode);
3145 fence_alignment = i915_gem_get_gtt_alignment(dev,
3146 view_size,
3147 obj->tiling_mode,
3148 true);
3149 unfenced_alignment = i915_gem_get_gtt_alignment(dev,
3150 view_size,
3151 obj->tiling_mode,
3152 false);
3153 size = flags & PIN_MAPPABLE ? fence_size : view_size;
3154 } else {
3155 fence_size = i915_gem_get_gtt_size(dev,
3156 obj->base.size,
3157 obj->tiling_mode);
3158 fence_alignment = i915_gem_get_gtt_alignment(dev,
3159 obj->base.size,
3160 obj->tiling_mode,
3161 true);
3162 unfenced_alignment =
3163 i915_gem_get_gtt_alignment(dev,
3164 obj->base.size,
3165 obj->tiling_mode,
3166 false);
3167 size = flags & PIN_MAPPABLE ? fence_size : obj->base.size;
3168 }
Chris Wilsona00b10c2010-09-24 21:15:47 +01003169
Michel Thierry101b5062015-10-01 13:33:57 +01003170 start = flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
3171 end = vm->total;
3172 if (flags & PIN_MAPPABLE)
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003173 end = min_t(u64, end, ggtt->mappable_end);
Michel Thierry101b5062015-10-01 13:33:57 +01003174 if (flags & PIN_ZONE_4G)
Michel Thierry48ea1e32016-01-11 11:39:27 +00003175 end = min_t(u64, end, (1ULL << 32) - PAGE_SIZE);
Michel Thierry101b5062015-10-01 13:33:57 +01003176
Eric Anholt673a3942008-07-30 12:06:12 -07003177 if (alignment == 0)
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003178 alignment = flags & PIN_MAPPABLE ? fence_alignment :
Daniel Vetter5e783302010-11-14 22:32:36 +01003179 unfenced_alignment;
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003180 if (flags & PIN_MAPPABLE && alignment & (fence_alignment - 1)) {
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003181 DRM_DEBUG("Invalid object (view type=%u) alignment requested %u\n",
3182 ggtt_view ? ggtt_view->type : 0,
3183 alignment);
Daniel Vetter262de142014-02-14 14:01:20 +01003184 return ERR_PTR(-EINVAL);
Eric Anholt673a3942008-07-30 12:06:12 -07003185 }
3186
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003187 /* If binding the object/GGTT view requires more space than the entire
3188 * aperture has, reject it early before evicting everything in a vain
3189 * attempt to find space.
Chris Wilson654fc602010-05-27 13:18:21 +01003190 */
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003191 if (size > end) {
Michel Thierry65bd3422015-07-29 17:23:58 +01003192 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 +03003193 ggtt_view ? ggtt_view->type : 0,
3194 size,
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003195 flags & PIN_MAPPABLE ? "mappable" : "total",
Chris Wilsond23db882014-05-23 08:48:08 +02003196 end);
Daniel Vetter262de142014-02-14 14:01:20 +01003197 return ERR_PTR(-E2BIG);
Chris Wilson654fc602010-05-27 13:18:21 +01003198 }
3199
Chris Wilson37e680a2012-06-07 15:38:42 +01003200 ret = i915_gem_object_get_pages(obj);
Chris Wilson6c085a72012-08-20 11:40:46 +02003201 if (ret)
Daniel Vetter262de142014-02-14 14:01:20 +01003202 return ERR_PTR(ret);
Chris Wilson6c085a72012-08-20 11:40:46 +02003203
Chris Wilsonfbdda6f2012-11-20 10:45:16 +00003204 i915_gem_object_pin_pages(obj);
3205
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003206 vma = ggtt_view ? i915_gem_obj_lookup_or_create_ggtt_vma(obj, ggtt_view) :
3207 i915_gem_obj_lookup_or_create_vma(obj, vm);
3208
Daniel Vetter262de142014-02-14 14:01:20 +01003209 if (IS_ERR(vma))
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003210 goto err_unpin;
Ben Widawsky2f633152013-07-17 12:19:03 -07003211
Chris Wilson506a8e82015-12-08 11:55:07 +00003212 if (flags & PIN_OFFSET_FIXED) {
3213 uint64_t offset = flags & PIN_OFFSET_MASK;
3214
3215 if (offset & (alignment - 1) || offset + size > end) {
3216 ret = -EINVAL;
3217 goto err_free_vma;
3218 }
3219 vma->node.start = offset;
3220 vma->node.size = size;
3221 vma->node.color = obj->cache_level;
3222 ret = drm_mm_reserve_node(&vm->mm, &vma->node);
3223 if (ret) {
3224 ret = i915_gem_evict_for_vma(vma);
3225 if (ret == 0)
3226 ret = drm_mm_reserve_node(&vm->mm, &vma->node);
3227 }
3228 if (ret)
3229 goto err_free_vma;
Michel Thierry101b5062015-10-01 13:33:57 +01003230 } else {
Chris Wilson506a8e82015-12-08 11:55:07 +00003231 if (flags & PIN_HIGH) {
3232 search_flag = DRM_MM_SEARCH_BELOW;
3233 alloc_flag = DRM_MM_CREATE_TOP;
3234 } else {
3235 search_flag = DRM_MM_SEARCH_DEFAULT;
3236 alloc_flag = DRM_MM_CREATE_DEFAULT;
3237 }
Michel Thierry101b5062015-10-01 13:33:57 +01003238
Ben Widawsky0a9ae0d2013-05-25 12:26:35 -07003239search_free:
Chris Wilson506a8e82015-12-08 11:55:07 +00003240 ret = drm_mm_insert_node_in_range_generic(&vm->mm, &vma->node,
3241 size, alignment,
3242 obj->cache_level,
3243 start, end,
3244 search_flag,
3245 alloc_flag);
3246 if (ret) {
3247 ret = i915_gem_evict_something(dev, vm, size, alignment,
3248 obj->cache_level,
3249 start, end,
3250 flags);
3251 if (ret == 0)
3252 goto search_free;
Chris Wilson97311292009-09-21 00:22:34 +01003253
Chris Wilson506a8e82015-12-08 11:55:07 +00003254 goto err_free_vma;
3255 }
Chris Wilsondc9dd7a2012-12-07 20:37:07 +00003256 }
Chris Wilson4144f9b2014-09-11 08:43:48 +01003257 if (WARN_ON(!i915_gem_valid_gtt_space(vma, obj->cache_level))) {
Ben Widawsky2f633152013-07-17 12:19:03 -07003258 ret = -EINVAL;
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003259 goto err_remove_node;
Eric Anholt673a3942008-07-30 12:06:12 -07003260 }
3261
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003262 trace_i915_vma_bind(vma, flags);
Daniel Vetter08755462015-04-20 09:04:05 -07003263 ret = i915_vma_bind(vma, obj->cache_level, flags);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003264 if (ret)
Imre Deake2273302015-07-09 12:59:05 +03003265 goto err_remove_node;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003266
Ben Widawsky35c20a62013-05-31 11:28:48 -07003267 list_move_tail(&obj->global_list, &dev_priv->mm.bound_list);
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003268 list_add_tail(&vma->vm_link, &vm->inactive_list);
Chris Wilson15717de2016-08-04 07:52:26 +01003269 obj->bind_count++;
Chris Wilsonbf1a1092010-08-07 11:01:20 +01003270
Daniel Vetter262de142014-02-14 14:01:20 +01003271 return vma;
Ben Widawsky2f633152013-07-17 12:19:03 -07003272
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003273err_remove_node:
Dan Carpenter6286ef92013-07-19 08:46:27 +03003274 drm_mm_remove_node(&vma->node);
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003275err_free_vma:
Ben Widawsky2f633152013-07-17 12:19:03 -07003276 i915_gem_vma_destroy(vma);
Daniel Vetter262de142014-02-14 14:01:20 +01003277 vma = ERR_PTR(ret);
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003278err_unpin:
Ben Widawsky2f633152013-07-17 12:19:03 -07003279 i915_gem_object_unpin_pages(obj);
Daniel Vetter262de142014-02-14 14:01:20 +01003280 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07003281}
3282
Chris Wilson000433b2013-08-08 14:41:09 +01003283bool
Chris Wilson2c225692013-08-09 12:26:45 +01003284i915_gem_clflush_object(struct drm_i915_gem_object *obj,
3285 bool force)
Eric Anholt673a3942008-07-30 12:06:12 -07003286{
Eric Anholt673a3942008-07-30 12:06:12 -07003287 /* If we don't have a page list set up, then we're not pinned
3288 * to GPU, and we can ignore the cache flush because it'll happen
3289 * again at bind time.
3290 */
Chris Wilson05394f32010-11-08 19:18:58 +00003291 if (obj->pages == NULL)
Chris Wilson000433b2013-08-08 14:41:09 +01003292 return false;
Eric Anholt673a3942008-07-30 12:06:12 -07003293
Imre Deak769ce462013-02-13 21:56:05 +02003294 /*
3295 * Stolen memory is always coherent with the GPU as it is explicitly
3296 * marked as wc by the system, or the system is cache-coherent.
3297 */
Chris Wilson6a2c4232014-11-04 04:51:40 -08003298 if (obj->stolen || obj->phys_handle)
Chris Wilson000433b2013-08-08 14:41:09 +01003299 return false;
Imre Deak769ce462013-02-13 21:56:05 +02003300
Chris Wilson9c23f7f2011-03-29 16:59:52 -07003301 /* If the GPU is snooping the contents of the CPU cache,
3302 * we do not need to manually clear the CPU cache lines. However,
3303 * the caches are only snooped when the render cache is
3304 * flushed/invalidated. As we always have to emit invalidations
3305 * and flushes when moving into and out of the RENDER domain, correct
3306 * snooping behaviour occurs naturally as the result of our domain
3307 * tracking.
3308 */
Chris Wilson0f719792015-01-13 13:32:52 +00003309 if (!force && cpu_cache_is_coherent(obj->base.dev, obj->cache_level)) {
3310 obj->cache_dirty = true;
Chris Wilson000433b2013-08-08 14:41:09 +01003311 return false;
Chris Wilson0f719792015-01-13 13:32:52 +00003312 }
Chris Wilson9c23f7f2011-03-29 16:59:52 -07003313
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003314 trace_i915_gem_object_clflush(obj);
Chris Wilson9da3da62012-06-01 15:20:22 +01003315 drm_clflush_sg(obj->pages);
Chris Wilson0f719792015-01-13 13:32:52 +00003316 obj->cache_dirty = false;
Chris Wilson000433b2013-08-08 14:41:09 +01003317
3318 return true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003319}
3320
3321/** Flushes the GTT write domain for the object if it's dirty. */
3322static void
Chris Wilson05394f32010-11-08 19:18:58 +00003323i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003324{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003325 uint32_t old_write_domain;
3326
Chris Wilson05394f32010-11-08 19:18:58 +00003327 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
Eric Anholte47c68e2008-11-14 13:35:19 -08003328 return;
3329
Chris Wilson63256ec2011-01-04 18:42:07 +00003330 /* No actual flushing is required for the GTT write domain. Writes
Eric Anholte47c68e2008-11-14 13:35:19 -08003331 * to it immediately go to main memory as far as we know, so there's
3332 * no chipset flush. It also doesn't land in render cache.
Chris Wilson63256ec2011-01-04 18:42:07 +00003333 *
3334 * However, we do have to enforce the order so that all writes through
3335 * the GTT land before any writes to the device, such as updates to
3336 * the GATT itself.
Eric Anholte47c68e2008-11-14 13:35:19 -08003337 */
Chris Wilson63256ec2011-01-04 18:42:07 +00003338 wmb();
3339
Chris Wilson05394f32010-11-08 19:18:58 +00003340 old_write_domain = obj->base.write_domain;
3341 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003342
Rodrigo Vivide152b62015-07-07 16:28:51 -07003343 intel_fb_obj_flush(obj, false, ORIGIN_GTT);
Daniel Vetterf99d7062014-06-19 16:01:59 +02003344
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003345 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00003346 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003347 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08003348}
3349
3350/** Flushes the CPU write domain for the object if it's dirty. */
3351static void
Daniel Vettere62b59e2015-01-21 14:53:48 +01003352i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003353{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003354 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08003355
Chris Wilson05394f32010-11-08 19:18:58 +00003356 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
Eric Anholte47c68e2008-11-14 13:35:19 -08003357 return;
3358
Daniel Vettere62b59e2015-01-21 14:53:48 +01003359 if (i915_gem_clflush_object(obj, obj->pin_display))
Chris Wilsonc0336662016-05-06 15:40:21 +01003360 i915_gem_chipset_flush(to_i915(obj->base.dev));
Chris Wilson000433b2013-08-08 14:41:09 +01003361
Chris Wilson05394f32010-11-08 19:18:58 +00003362 old_write_domain = obj->base.write_domain;
3363 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003364
Rodrigo Vivide152b62015-07-07 16:28:51 -07003365 intel_fb_obj_flush(obj, false, ORIGIN_CPU);
Daniel Vetterf99d7062014-06-19 16:01:59 +02003366
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003367 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00003368 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003369 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08003370}
3371
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003372/**
3373 * Moves a single object to the GTT read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003374 * @obj: object to act on
3375 * @write: ask for write access or read only
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003376 *
3377 * This function returns when the move is complete, including waiting on
3378 * flushes to occur.
3379 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003380int
Chris Wilson20217462010-11-23 15:26:33 +00003381i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003382{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003383 struct drm_device *dev = obj->base.dev;
3384 struct drm_i915_private *dev_priv = to_i915(dev);
3385 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003386 uint32_t old_write_domain, old_read_domains;
Chris Wilson43566de2015-01-02 16:29:29 +05303387 struct i915_vma *vma;
Eric Anholte47c68e2008-11-14 13:35:19 -08003388 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003389
Chris Wilson0201f1e2012-07-20 12:41:01 +01003390 ret = i915_gem_object_wait_rendering(obj, !write);
Chris Wilson88241782011-01-07 17:09:48 +00003391 if (ret)
3392 return ret;
3393
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003394 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
3395 return 0;
3396
Chris Wilson43566de2015-01-02 16:29:29 +05303397 /* Flush and acquire obj->pages so that we are coherent through
3398 * direct access in memory with previous cached writes through
3399 * shmemfs and that our cache domain tracking remains valid.
3400 * For example, if the obj->filp was moved to swap without us
3401 * being notified and releasing the pages, we would mistakenly
3402 * continue to assume that the obj remained out of the CPU cached
3403 * domain.
3404 */
3405 ret = i915_gem_object_get_pages(obj);
3406 if (ret)
3407 return ret;
3408
Daniel Vettere62b59e2015-01-21 14:53:48 +01003409 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003410
Chris Wilsond0a57782012-10-09 19:24:37 +01003411 /* Serialise direct access to this object with the barriers for
3412 * coherent writes from the GPU, by effectively invalidating the
3413 * GTT domain upon first access.
3414 */
3415 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
3416 mb();
3417
Chris Wilson05394f32010-11-08 19:18:58 +00003418 old_write_domain = obj->base.write_domain;
3419 old_read_domains = obj->base.read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003420
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003421 /* It should now be out of any other write domains, and we can update
3422 * the domain values for our changes.
3423 */
Chris Wilson05394f32010-11-08 19:18:58 +00003424 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3425 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003426 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003427 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
3428 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
3429 obj->dirty = 1;
Eric Anholte47c68e2008-11-14 13:35:19 -08003430 }
3431
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003432 trace_i915_gem_object_change_domain(obj,
3433 old_read_domains,
3434 old_write_domain);
3435
Chris Wilson8325a092012-04-24 15:52:35 +01003436 /* And bump the LRU for this access */
Chris Wilson43566de2015-01-02 16:29:29 +05303437 vma = i915_gem_obj_to_ggtt(obj);
3438 if (vma && drm_mm_node_allocated(&vma->node) && !obj->active)
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003439 list_move_tail(&vma->vm_link,
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003440 &ggtt->base.inactive_list);
Chris Wilson8325a092012-04-24 15:52:35 +01003441
Eric Anholte47c68e2008-11-14 13:35:19 -08003442 return 0;
3443}
3444
Chris Wilsonef55f922015-10-09 14:11:27 +01003445/**
3446 * Changes the cache-level of an object across all VMA.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003447 * @obj: object to act on
3448 * @cache_level: new cache level to set for the object
Chris Wilsonef55f922015-10-09 14:11:27 +01003449 *
3450 * After this function returns, the object will be in the new cache-level
3451 * across all GTT and the contents of the backing storage will be coherent,
3452 * with respect to the new cache-level. In order to keep the backing storage
3453 * coherent for all users, we only allow a single cache level to be set
3454 * globally on the object and prevent it from being changed whilst the
3455 * hardware is reading from the object. That is if the object is currently
3456 * on the scanout it will be set to uncached (or equivalent display
3457 * cache coherency) and all non-MOCS GPU access will also be uncached so
3458 * that all direct access to the scanout remains coherent.
3459 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003460int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3461 enum i915_cache_level cache_level)
3462{
Chris Wilsonaa653a62016-08-04 07:52:27 +01003463 struct i915_vma *vma;
Ville Syrjäläed75a552015-08-11 19:47:10 +03003464 int ret = 0;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003465
3466 if (obj->cache_level == cache_level)
Ville Syrjäläed75a552015-08-11 19:47:10 +03003467 goto out;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003468
Chris Wilsonef55f922015-10-09 14:11:27 +01003469 /* Inspect the list of currently bound VMA and unbind any that would
3470 * be invalid given the new cache-level. This is principally to
3471 * catch the issue of the CS prefetch crossing page boundaries and
3472 * reading an invalid PTE on older architectures.
3473 */
Chris Wilsonaa653a62016-08-04 07:52:27 +01003474restart:
3475 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003476 if (!drm_mm_node_allocated(&vma->node))
3477 continue;
3478
3479 if (vma->pin_count) {
3480 DRM_DEBUG("can not change the cache level of pinned objects\n");
3481 return -EBUSY;
3482 }
3483
Chris Wilsonaa653a62016-08-04 07:52:27 +01003484 if (i915_gem_valid_gtt_space(vma, cache_level))
3485 continue;
3486
3487 ret = i915_vma_unbind(vma);
3488 if (ret)
3489 return ret;
3490
3491 /* As unbinding may affect other elements in the
3492 * obj->vma_list (due to side-effects from retiring
3493 * an active vma), play safe and restart the iterator.
3494 */
3495 goto restart;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003496 }
3497
Chris Wilsonef55f922015-10-09 14:11:27 +01003498 /* We can reuse the existing drm_mm nodes but need to change the
3499 * cache-level on the PTE. We could simply unbind them all and
3500 * rebind with the correct cache-level on next use. However since
3501 * we already have a valid slot, dma mapping, pages etc, we may as
3502 * rewrite the PTE in the belief that doing so tramples upon less
3503 * state and so involves less work.
3504 */
Chris Wilson15717de2016-08-04 07:52:26 +01003505 if (obj->bind_count) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003506 /* Before we change the PTE, the GPU must not be accessing it.
3507 * If we wait upon the object, we know that all the bound
3508 * VMA are no longer active.
3509 */
Chris Wilson2e2f3512015-04-27 13:41:14 +01003510 ret = i915_gem_object_wait_rendering(obj, false);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003511 if (ret)
3512 return ret;
3513
Chris Wilsonaa653a62016-08-04 07:52:27 +01003514 if (!HAS_LLC(obj->base.dev) && cache_level != I915_CACHE_NONE) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003515 /* Access to snoopable pages through the GTT is
3516 * incoherent and on some machines causes a hard
3517 * lockup. Relinquish the CPU mmaping to force
3518 * userspace to refault in the pages and we can
3519 * then double check if the GTT mapping is still
3520 * valid for that pointer access.
3521 */
3522 i915_gem_release_mmap(obj);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003523
Chris Wilsonef55f922015-10-09 14:11:27 +01003524 /* As we no longer need a fence for GTT access,
3525 * we can relinquish it now (and so prevent having
3526 * to steal a fence from someone else on the next
3527 * fence request). Note GPU activity would have
3528 * dropped the fence as all snoopable access is
3529 * supposed to be linear.
3530 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003531 ret = i915_gem_object_put_fence(obj);
3532 if (ret)
3533 return ret;
Chris Wilsonef55f922015-10-09 14:11:27 +01003534 } else {
3535 /* We either have incoherent backing store and
3536 * so no GTT access or the architecture is fully
3537 * coherent. In such cases, existing GTT mmaps
3538 * ignore the cache bit in the PTE and we can
3539 * rewrite it without confusing the GPU or having
3540 * to force userspace to fault back in its mmaps.
3541 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003542 }
3543
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003544 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003545 if (!drm_mm_node_allocated(&vma->node))
3546 continue;
3547
3548 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
3549 if (ret)
3550 return ret;
3551 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01003552 }
3553
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003554 list_for_each_entry(vma, &obj->vma_list, obj_link)
Chris Wilson2c225692013-08-09 12:26:45 +01003555 vma->node.color = cache_level;
3556 obj->cache_level = cache_level;
3557
Ville Syrjäläed75a552015-08-11 19:47:10 +03003558out:
Chris Wilsonef55f922015-10-09 14:11:27 +01003559 /* Flush the dirty CPU caches to the backing storage so that the
3560 * object is now coherent at its new cache level (with respect
3561 * to the access domain).
3562 */
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05303563 if (obj->cache_dirty && cpu_write_needs_clflush(obj)) {
Chris Wilson0f719792015-01-13 13:32:52 +00003564 if (i915_gem_clflush_object(obj, true))
Chris Wilsonc0336662016-05-06 15:40:21 +01003565 i915_gem_chipset_flush(to_i915(obj->base.dev));
Chris Wilsone4ffd172011-04-04 09:44:39 +01003566 }
3567
Chris Wilsone4ffd172011-04-04 09:44:39 +01003568 return 0;
3569}
3570
Ben Widawsky199adf42012-09-21 17:01:20 -07003571int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
3572 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003573{
Ben Widawsky199adf42012-09-21 17:01:20 -07003574 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003575 struct drm_i915_gem_object *obj;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003576
Chris Wilson03ac0642016-07-20 13:31:51 +01003577 obj = i915_gem_object_lookup(file, args->handle);
3578 if (!obj)
Chris Wilson432be692015-05-07 12:14:55 +01003579 return -ENOENT;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003580
Chris Wilson651d7942013-08-08 14:41:10 +01003581 switch (obj->cache_level) {
3582 case I915_CACHE_LLC:
3583 case I915_CACHE_L3_LLC:
3584 args->caching = I915_CACHING_CACHED;
3585 break;
3586
Chris Wilson4257d3b2013-08-08 14:41:11 +01003587 case I915_CACHE_WT:
3588 args->caching = I915_CACHING_DISPLAY;
3589 break;
3590
Chris Wilson651d7942013-08-08 14:41:10 +01003591 default:
3592 args->caching = I915_CACHING_NONE;
3593 break;
3594 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01003595
Chris Wilson34911fd2016-07-20 13:31:54 +01003596 i915_gem_object_put_unlocked(obj);
Chris Wilson432be692015-05-07 12:14:55 +01003597 return 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003598}
3599
Ben Widawsky199adf42012-09-21 17:01:20 -07003600int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
3601 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003602{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003603 struct drm_i915_private *dev_priv = to_i915(dev);
Ben Widawsky199adf42012-09-21 17:01:20 -07003604 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003605 struct drm_i915_gem_object *obj;
3606 enum i915_cache_level level;
3607 int ret;
3608
Ben Widawsky199adf42012-09-21 17:01:20 -07003609 switch (args->caching) {
3610 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01003611 level = I915_CACHE_NONE;
3612 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07003613 case I915_CACHING_CACHED:
Imre Deake5756c12015-08-14 18:43:30 +03003614 /*
3615 * Due to a HW issue on BXT A stepping, GPU stores via a
3616 * snooped mapping may leave stale data in a corresponding CPU
3617 * cacheline, whereas normally such cachelines would get
3618 * invalidated.
3619 */
Tvrtko Ursulinca377802016-03-02 12:10:31 +00003620 if (!HAS_LLC(dev) && !HAS_SNOOP(dev))
Imre Deake5756c12015-08-14 18:43:30 +03003621 return -ENODEV;
3622
Chris Wilsone6994ae2012-07-10 10:27:08 +01003623 level = I915_CACHE_LLC;
3624 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003625 case I915_CACHING_DISPLAY:
3626 level = HAS_WT(dev) ? I915_CACHE_WT : I915_CACHE_NONE;
3627 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003628 default:
3629 return -EINVAL;
3630 }
3631
Imre Deakfd0fe6a2015-11-04 21:25:32 +02003632 intel_runtime_pm_get(dev_priv);
3633
Ben Widawsky3bc29132012-09-26 16:15:20 -07003634 ret = i915_mutex_lock_interruptible(dev);
3635 if (ret)
Imre Deakfd0fe6a2015-11-04 21:25:32 +02003636 goto rpm_put;
Ben Widawsky3bc29132012-09-26 16:15:20 -07003637
Chris Wilson03ac0642016-07-20 13:31:51 +01003638 obj = i915_gem_object_lookup(file, args->handle);
3639 if (!obj) {
Chris Wilsone6994ae2012-07-10 10:27:08 +01003640 ret = -ENOENT;
3641 goto unlock;
3642 }
3643
3644 ret = i915_gem_object_set_cache_level(obj, level);
3645
Chris Wilsonf8c417c2016-07-20 13:31:53 +01003646 i915_gem_object_put(obj);
Chris Wilsone6994ae2012-07-10 10:27:08 +01003647unlock:
3648 mutex_unlock(&dev->struct_mutex);
Imre Deakfd0fe6a2015-11-04 21:25:32 +02003649rpm_put:
3650 intel_runtime_pm_put(dev_priv);
3651
Chris Wilsone6994ae2012-07-10 10:27:08 +01003652 return ret;
3653}
3654
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003655/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003656 * Prepare buffer for display plane (scanout, cursors, etc).
3657 * Can be called from an uninterruptible phase (modesetting) and allows
3658 * any flushes to be pipelined (for pageflips).
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003659 */
3660int
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003661i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3662 u32 alignment,
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003663 const struct i915_ggtt_view *view)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003664{
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003665 u32 old_read_domains, old_write_domain;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003666 int ret;
3667
Chris Wilsoncc98b412013-08-09 12:25:09 +01003668 /* Mark the pin_display early so that we account for the
3669 * display coherency whilst setting up the cache domains.
3670 */
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003671 obj->pin_display++;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003672
Eric Anholta7ef0642011-03-29 16:59:54 -07003673 /* The display engine is not coherent with the LLC cache on gen6. As
3674 * a result, we make sure that the pinning that is about to occur is
3675 * done with uncached PTEs. This is lowest common denominator for all
3676 * chipsets.
3677 *
3678 * However for gen6+, we could do better by using the GFDT bit instead
3679 * of uncaching, which would allow us to flush all the LLC-cached data
3680 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3681 */
Chris Wilson651d7942013-08-08 14:41:10 +01003682 ret = i915_gem_object_set_cache_level(obj,
3683 HAS_WT(obj->base.dev) ? I915_CACHE_WT : I915_CACHE_NONE);
Eric Anholta7ef0642011-03-29 16:59:54 -07003684 if (ret)
Chris Wilsoncc98b412013-08-09 12:25:09 +01003685 goto err_unpin_display;
Eric Anholta7ef0642011-03-29 16:59:54 -07003686
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003687 /* As the user may map the buffer once pinned in the display plane
3688 * (e.g. libkms for the bootup splash), we have to ensure that we
3689 * always use map_and_fenceable for all scanout buffers.
3690 */
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003691 ret = i915_gem_object_ggtt_pin(obj, view, alignment,
3692 view->type == I915_GGTT_VIEW_NORMAL ?
3693 PIN_MAPPABLE : 0);
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003694 if (ret)
Chris Wilsoncc98b412013-08-09 12:25:09 +01003695 goto err_unpin_display;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003696
Daniel Vettere62b59e2015-01-21 14:53:48 +01003697 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01003698
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003699 old_write_domain = obj->base.write_domain;
Chris Wilson05394f32010-11-08 19:18:58 +00003700 old_read_domains = obj->base.read_domains;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003701
3702 /* It should now be out of any other write domains, and we can update
3703 * the domain values for our changes.
3704 */
Chris Wilsone5f1d962012-07-20 12:41:00 +01003705 obj->base.write_domain = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00003706 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003707
3708 trace_i915_gem_object_change_domain(obj,
3709 old_read_domains,
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003710 old_write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003711
3712 return 0;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003713
3714err_unpin_display:
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003715 obj->pin_display--;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003716 return ret;
3717}
3718
3719void
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003720i915_gem_object_unpin_from_display_plane(struct drm_i915_gem_object *obj,
3721 const struct i915_ggtt_view *view)
Chris Wilsoncc98b412013-08-09 12:25:09 +01003722{
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003723 if (WARN_ON(obj->pin_display == 0))
3724 return;
3725
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003726 i915_gem_object_ggtt_unpin_view(obj, view);
3727
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003728 obj->pin_display--;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003729}
3730
Eric Anholte47c68e2008-11-14 13:35:19 -08003731/**
3732 * Moves a single object to the CPU read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003733 * @obj: object to act on
3734 * @write: requesting write or read-only access
Eric Anholte47c68e2008-11-14 13:35:19 -08003735 *
3736 * This function returns when the move is complete, including waiting on
3737 * flushes to occur.
3738 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02003739int
Chris Wilson919926a2010-11-12 13:42:53 +00003740i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08003741{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003742 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003743 int ret;
3744
Chris Wilson0201f1e2012-07-20 12:41:01 +01003745 ret = i915_gem_object_wait_rendering(obj, !write);
Chris Wilson88241782011-01-07 17:09:48 +00003746 if (ret)
3747 return ret;
3748
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003749 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
3750 return 0;
3751
Eric Anholte47c68e2008-11-14 13:35:19 -08003752 i915_gem_object_flush_gtt_write_domain(obj);
3753
Chris Wilson05394f32010-11-08 19:18:58 +00003754 old_write_domain = obj->base.write_domain;
3755 old_read_domains = obj->base.read_domains;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003756
Eric Anholte47c68e2008-11-14 13:35:19 -08003757 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00003758 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson2c225692013-08-09 12:26:45 +01003759 i915_gem_clflush_object(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003760
Chris Wilson05394f32010-11-08 19:18:58 +00003761 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003762 }
3763
3764 /* It should now be out of any other write domains, and we can update
3765 * the domain values for our changes.
3766 */
Chris Wilson05394f32010-11-08 19:18:58 +00003767 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003768
3769 /* If we're writing through the CPU, then the GPU read domains will
3770 * need to be invalidated at next use.
3771 */
3772 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003773 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3774 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003775 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003776
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003777 trace_i915_gem_object_change_domain(obj,
3778 old_read_domains,
3779 old_write_domain);
3780
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003781 return 0;
3782}
3783
Eric Anholt673a3942008-07-30 12:06:12 -07003784/* Throttle our rendering by waiting until the ring has completed our requests
3785 * emitted over 20 msec ago.
3786 *
Eric Anholtb9624422009-06-03 07:27:35 +00003787 * Note that if we were to use the current jiffies each time around the loop,
3788 * we wouldn't escape the function with any frames outstanding if the time to
3789 * render a frame was over 20ms.
3790 *
Eric Anholt673a3942008-07-30 12:06:12 -07003791 * This should get us reasonable parallelism between CPU and GPU but also
3792 * relatively low latency when blocking on a particular request to finish.
3793 */
3794static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003795i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003796{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003797 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003798 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsond0bc54f2015-05-21 21:01:48 +01003799 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
John Harrison54fb2412014-11-24 18:49:27 +00003800 struct drm_i915_gem_request *request, *target = NULL;
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003801 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003802
Daniel Vetter308887a2012-11-14 17:14:06 +01003803 ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
3804 if (ret)
3805 return ret;
3806
Chris Wilsonf4457ae2016-04-13 17:35:08 +01003807 /* ABI: return -EIO if already wedged */
3808 if (i915_terminally_wedged(&dev_priv->gpu_error))
3809 return -EIO;
Chris Wilsone110e8d2011-01-26 15:39:14 +00003810
Chris Wilson1c255952010-09-26 11:03:27 +01003811 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003812 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003813 if (time_after_eq(request->emitted_jiffies, recent_enough))
3814 break;
3815
John Harrisonfcfa423c2015-05-29 17:44:12 +01003816 /*
3817 * Note that the request might not have been submitted yet.
3818 * In which case emitted_jiffies will be zero.
3819 */
3820 if (!request->emitted_jiffies)
3821 continue;
3822
John Harrison54fb2412014-11-24 18:49:27 +00003823 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00003824 }
John Harrisonff865882014-11-24 18:49:28 +00003825 if (target)
Chris Wilsone8a261e2016-07-20 13:31:49 +01003826 i915_gem_request_get(target);
Chris Wilson1c255952010-09-26 11:03:27 +01003827 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003828
John Harrison54fb2412014-11-24 18:49:27 +00003829 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003830 return 0;
3831
Chris Wilson299259a2016-04-13 17:35:06 +01003832 ret = __i915_wait_request(target, true, NULL, NULL);
Chris Wilsone8a261e2016-07-20 13:31:49 +01003833 i915_gem_request_put(target);
John Harrisonff865882014-11-24 18:49:28 +00003834
Eric Anholt673a3942008-07-30 12:06:12 -07003835 return ret;
3836}
3837
Chris Wilsond23db882014-05-23 08:48:08 +02003838static bool
3839i915_vma_misplaced(struct i915_vma *vma, uint32_t alignment, uint64_t flags)
3840{
3841 struct drm_i915_gem_object *obj = vma->obj;
3842
3843 if (alignment &&
3844 vma->node.start & (alignment - 1))
3845 return true;
3846
3847 if (flags & PIN_MAPPABLE && !obj->map_and_fenceable)
3848 return true;
3849
3850 if (flags & PIN_OFFSET_BIAS &&
3851 vma->node.start < (flags & PIN_OFFSET_MASK))
3852 return true;
3853
Chris Wilson506a8e82015-12-08 11:55:07 +00003854 if (flags & PIN_OFFSET_FIXED &&
3855 vma->node.start != (flags & PIN_OFFSET_MASK))
3856 return true;
3857
Chris Wilsond23db882014-05-23 08:48:08 +02003858 return false;
3859}
3860
Chris Wilsond0710ab2015-11-20 14:16:39 +00003861void __i915_vma_set_map_and_fenceable(struct i915_vma *vma)
3862{
3863 struct drm_i915_gem_object *obj = vma->obj;
3864 bool mappable, fenceable;
3865 u32 fence_size, fence_alignment;
3866
3867 fence_size = i915_gem_get_gtt_size(obj->base.dev,
3868 obj->base.size,
3869 obj->tiling_mode);
3870 fence_alignment = i915_gem_get_gtt_alignment(obj->base.dev,
3871 obj->base.size,
3872 obj->tiling_mode,
3873 true);
3874
3875 fenceable = (vma->node.size == fence_size &&
3876 (vma->node.start & (fence_alignment - 1)) == 0);
3877
3878 mappable = (vma->node.start + fence_size <=
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003879 to_i915(obj->base.dev)->ggtt.mappable_end);
Chris Wilsond0710ab2015-11-20 14:16:39 +00003880
3881 obj->map_and_fenceable = mappable && fenceable;
3882}
3883
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003884static int
3885i915_gem_object_do_pin(struct drm_i915_gem_object *obj,
3886 struct i915_address_space *vm,
3887 const struct i915_ggtt_view *ggtt_view,
3888 uint32_t alignment,
3889 uint64_t flags)
Eric Anholt673a3942008-07-30 12:06:12 -07003890{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003891 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003892 struct i915_vma *vma;
Chris Wilsonef79e172014-10-31 13:53:52 +00003893 unsigned bound;
Eric Anholt673a3942008-07-30 12:06:12 -07003894 int ret;
3895
Ben Widawsky6e7186a2014-05-06 22:21:36 -07003896 if (WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base))
3897 return -ENODEV;
3898
Daniel Vetterbf3d1492014-02-14 14:01:12 +01003899 if (WARN_ON(flags & (PIN_GLOBAL | PIN_MAPPABLE) && !i915_is_ggtt(vm)))
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003900 return -EINVAL;
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003901
Chris Wilsonc826c442014-10-31 13:53:53 +00003902 if (WARN_ON((flags & (PIN_MAPPABLE | PIN_GLOBAL)) == PIN_MAPPABLE))
3903 return -EINVAL;
3904
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003905 if (WARN_ON(i915_is_ggtt(vm) != !!ggtt_view))
3906 return -EINVAL;
3907
3908 vma = ggtt_view ? i915_gem_obj_to_ggtt_view(obj, ggtt_view) :
3909 i915_gem_obj_to_vma(obj, vm);
3910
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003911 if (vma) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08003912 if (WARN_ON(vma->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT))
3913 return -EBUSY;
3914
Chris Wilsond23db882014-05-23 08:48:08 +02003915 if (i915_vma_misplaced(vma, alignment, flags)) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08003916 WARN(vma->pin_count,
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003917 "bo is already pinned in %s with incorrect alignment:"
Michel Thierry088e0df2015-08-07 17:40:17 +01003918 " offset=%08x %08x, req.alignment=%x, req.map_and_fenceable=%d,"
Daniel Vetter75e9e912010-11-04 17:11:09 +01003919 " obj->map_and_fenceable=%d\n",
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003920 ggtt_view ? "ggtt" : "ppgtt",
Michel Thierry088e0df2015-08-07 17:40:17 +01003921 upper_32_bits(vma->node.start),
3922 lower_32_bits(vma->node.start),
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003923 alignment,
Chris Wilsond23db882014-05-23 08:48:08 +02003924 !!(flags & PIN_MAPPABLE),
Chris Wilson05394f32010-11-08 19:18:58 +00003925 obj->map_and_fenceable);
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003926 ret = i915_vma_unbind(vma);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003927 if (ret)
3928 return ret;
Daniel Vetter8ea99c92014-02-14 14:01:21 +01003929
3930 vma = NULL;
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003931 }
3932 }
3933
Chris Wilsonef79e172014-10-31 13:53:52 +00003934 bound = vma ? vma->bound : 0;
Daniel Vetter8ea99c92014-02-14 14:01:21 +01003935 if (vma == NULL || !drm_mm_node_allocated(&vma->node)) {
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003936 vma = i915_gem_object_bind_to_vm(obj, vm, ggtt_view, alignment,
3937 flags);
Daniel Vetter262de142014-02-14 14:01:20 +01003938 if (IS_ERR(vma))
3939 return PTR_ERR(vma);
Daniel Vetter08755462015-04-20 09:04:05 -07003940 } else {
3941 ret = i915_vma_bind(vma, obj->cache_level, flags);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003942 if (ret)
3943 return ret;
3944 }
Daniel Vetter74898d72012-02-15 23:50:22 +01003945
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003946 if (ggtt_view && ggtt_view->type == I915_GGTT_VIEW_NORMAL &&
3947 (bound ^ vma->bound) & GLOBAL_BIND) {
Chris Wilsond0710ab2015-11-20 14:16:39 +00003948 __i915_vma_set_map_and_fenceable(vma);
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003949 WARN_ON(flags & PIN_MAPPABLE && !obj->map_and_fenceable);
3950 }
Chris Wilsonef79e172014-10-31 13:53:52 +00003951
Daniel Vetter8ea99c92014-02-14 14:01:21 +01003952 vma->pin_count++;
Eric Anholt673a3942008-07-30 12:06:12 -07003953 return 0;
3954}
3955
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003956int
3957i915_gem_object_pin(struct drm_i915_gem_object *obj,
3958 struct i915_address_space *vm,
3959 uint32_t alignment,
3960 uint64_t flags)
3961{
3962 return i915_gem_object_do_pin(obj, vm,
3963 i915_is_ggtt(vm) ? &i915_ggtt_view_normal : NULL,
3964 alignment, flags);
3965}
3966
3967int
3968i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
3969 const struct i915_ggtt_view *view,
3970 uint32_t alignment,
3971 uint64_t flags)
3972{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003973 struct drm_device *dev = obj->base.dev;
3974 struct drm_i915_private *dev_priv = to_i915(dev);
3975 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3976
Matthew Auldade7daa2016-03-24 15:54:20 +00003977 BUG_ON(!view);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003978
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003979 return i915_gem_object_do_pin(obj, &ggtt->base, view,
Tvrtko Ursulin6fafab72015-03-17 15:36:51 +00003980 alignment, flags | PIN_GLOBAL);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003981}
3982
Eric Anholt673a3942008-07-30 12:06:12 -07003983void
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003984i915_gem_object_ggtt_unpin_view(struct drm_i915_gem_object *obj,
3985 const struct i915_ggtt_view *view)
Eric Anholt673a3942008-07-30 12:06:12 -07003986{
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003987 struct i915_vma *vma = i915_gem_obj_to_ggtt_view(obj, view);
Eric Anholt673a3942008-07-30 12:06:12 -07003988
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003989 WARN_ON(vma->pin_count == 0);
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02003990 WARN_ON(!i915_gem_obj_ggtt_bound_view(obj, view));
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08003991
Chris Wilson30154652015-04-07 17:28:24 +01003992 --vma->pin_count;
Eric Anholt673a3942008-07-30 12:06:12 -07003993}
3994
3995int
Eric Anholt673a3942008-07-30 12:06:12 -07003996i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003997 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003998{
3999 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004000 struct drm_i915_gem_object *obj;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004001 int ret;
4002
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004003 ret = i915_mutex_lock_interruptible(dev);
4004 if (ret)
4005 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004006
Chris Wilson03ac0642016-07-20 13:31:51 +01004007 obj = i915_gem_object_lookup(file, args->handle);
4008 if (!obj) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004009 ret = -ENOENT;
4010 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004011 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08004012
Chris Wilson0be555b2010-08-04 15:36:30 +01004013 /* Count all active objects as busy, even if they are currently not used
4014 * by the gpu. Users of this interface expect objects to eventually
4015 * become non-busy without any further actions, therefore emit any
4016 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004017 */
Daniel Vetter30dfebf2012-06-01 15:21:23 +02004018 ret = i915_gem_object_flush_active(obj);
Chris Wilsonb4716182015-04-27 13:41:17 +01004019 if (ret)
4020 goto unref;
Daniel Vetter30dfebf2012-06-01 15:21:23 +02004021
Chris Wilson426960b2016-01-15 16:51:46 +00004022 args->busy = 0;
4023 if (obj->active) {
4024 int i;
4025
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00004026 for (i = 0; i < I915_NUM_ENGINES; i++) {
Chris Wilson426960b2016-01-15 16:51:46 +00004027 struct drm_i915_gem_request *req;
4028
4029 req = obj->last_read_req[i];
4030 if (req)
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00004031 args->busy |= 1 << (16 + req->engine->exec_id);
Chris Wilson426960b2016-01-15 16:51:46 +00004032 }
4033 if (obj->last_write_req)
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00004034 args->busy |= obj->last_write_req->engine->exec_id;
Chris Wilson426960b2016-01-15 16:51:46 +00004035 }
Eric Anholt673a3942008-07-30 12:06:12 -07004036
Chris Wilsonb4716182015-04-27 13:41:17 +01004037unref:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004038 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004039unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004040 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004041 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004042}
4043
4044int
4045i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4046 struct drm_file *file_priv)
4047{
Akshay Joshi0206e352011-08-16 15:34:10 -04004048 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004049}
4050
Chris Wilson3ef94da2009-09-14 16:50:29 +01004051int
4052i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4053 struct drm_file *file_priv)
4054{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004055 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004056 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004057 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004058 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004059
4060 switch (args->madv) {
4061 case I915_MADV_DONTNEED:
4062 case I915_MADV_WILLNEED:
4063 break;
4064 default:
4065 return -EINVAL;
4066 }
4067
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004068 ret = i915_mutex_lock_interruptible(dev);
4069 if (ret)
4070 return ret;
4071
Chris Wilson03ac0642016-07-20 13:31:51 +01004072 obj = i915_gem_object_lookup(file_priv, args->handle);
4073 if (!obj) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004074 ret = -ENOENT;
4075 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004076 }
Chris Wilson3ef94da2009-09-14 16:50:29 +01004077
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004078 if (i915_gem_obj_is_pinned(obj)) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004079 ret = -EINVAL;
4080 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004081 }
4082
Daniel Vetter656bfa32014-11-20 09:26:30 +01004083 if (obj->pages &&
4084 obj->tiling_mode != I915_TILING_NONE &&
4085 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
4086 if (obj->madv == I915_MADV_WILLNEED)
4087 i915_gem_object_unpin_pages(obj);
4088 if (args->madv == I915_MADV_WILLNEED)
4089 i915_gem_object_pin_pages(obj);
4090 }
4091
Chris Wilson05394f32010-11-08 19:18:58 +00004092 if (obj->madv != __I915_MADV_PURGED)
4093 obj->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004094
Chris Wilson6c085a72012-08-20 11:40:46 +02004095 /* if the object is no longer attached, discard its backing storage */
Daniel Vetterbe6a0372015-03-18 10:46:04 +01004096 if (obj->madv == I915_MADV_DONTNEED && obj->pages == NULL)
Chris Wilson2d7ef392009-09-20 23:13:10 +01004097 i915_gem_object_truncate(obj);
4098
Chris Wilson05394f32010-11-08 19:18:58 +00004099 args->retained = obj->madv != __I915_MADV_PURGED;
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004100
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004101out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004102 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004103unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01004104 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004105 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004106}
4107
Chris Wilson37e680a2012-06-07 15:38:42 +01004108void i915_gem_object_init(struct drm_i915_gem_object *obj,
4109 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01004110{
Chris Wilsonb4716182015-04-27 13:41:17 +01004111 int i;
4112
Ben Widawsky35c20a62013-05-31 11:28:48 -07004113 INIT_LIST_HEAD(&obj->global_list);
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00004114 for (i = 0; i < I915_NUM_ENGINES; i++)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004115 INIT_LIST_HEAD(&obj->engine_list[i]);
Ben Widawskyb25cb2f2013-08-14 11:38:33 +02004116 INIT_LIST_HEAD(&obj->obj_exec_link);
Ben Widawsky2f633152013-07-17 12:19:03 -07004117 INIT_LIST_HEAD(&obj->vma_list);
Chris Wilson8d9d5742015-04-07 16:20:38 +01004118 INIT_LIST_HEAD(&obj->batch_pool_link);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004119
Chris Wilson37e680a2012-06-07 15:38:42 +01004120 obj->ops = ops;
4121
Chris Wilson0327d6b2012-08-11 15:41:06 +01004122 obj->fence_reg = I915_FENCE_REG_NONE;
4123 obj->madv = I915_MADV_WILLNEED;
Chris Wilson0327d6b2012-08-11 15:41:06 +01004124
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004125 i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004126}
4127
Chris Wilson37e680a2012-06-07 15:38:42 +01004128static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
Chris Wilsonde472662016-01-22 18:32:31 +00004129 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE,
Chris Wilson37e680a2012-06-07 15:38:42 +01004130 .get_pages = i915_gem_object_get_pages_gtt,
4131 .put_pages = i915_gem_object_put_pages_gtt,
4132};
4133
Dave Gordond37cd8a2016-04-22 19:14:32 +01004134struct drm_i915_gem_object *i915_gem_object_create(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00004135 size_t size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004136{
Daniel Vetterc397b902010-04-09 19:05:07 +00004137 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004138 struct address_space *mapping;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004139 gfp_t mask;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004140 int ret;
Daniel Vetterc397b902010-04-09 19:05:07 +00004141
Chris Wilson42dcedd2012-11-15 11:32:30 +00004142 obj = i915_gem_object_alloc(dev);
Daniel Vetterc397b902010-04-09 19:05:07 +00004143 if (obj == NULL)
Chris Wilsonfe3db792016-04-25 13:32:13 +01004144 return ERR_PTR(-ENOMEM);
Daniel Vetterc397b902010-04-09 19:05:07 +00004145
Chris Wilsonfe3db792016-04-25 13:32:13 +01004146 ret = drm_gem_object_init(dev, &obj->base, size);
4147 if (ret)
4148 goto fail;
Daniel Vetterc397b902010-04-09 19:05:07 +00004149
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004150 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
4151 if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) {
4152 /* 965gm cannot relocate objects above 4GiB. */
4153 mask &= ~__GFP_HIGHMEM;
4154 mask |= __GFP_DMA32;
4155 }
4156
Al Viro496ad9a2013-01-23 17:07:38 -05004157 mapping = file_inode(obj->base.filp)->i_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004158 mapping_set_gfp_mask(mapping, mask);
Hugh Dickins5949eac2011-06-27 16:18:18 -07004159
Chris Wilson37e680a2012-06-07 15:38:42 +01004160 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004161
Daniel Vetterc397b902010-04-09 19:05:07 +00004162 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4163 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4164
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004165 if (HAS_LLC(dev)) {
4166 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004167 * cache) for about a 10% performance improvement
4168 * compared to uncached. Graphics requests other than
4169 * display scanout are coherent with the CPU in
4170 * accessing this cache. This means in this mode we
4171 * don't need to clflush on the CPU side, and on the
4172 * GPU side we only need to flush internal caches to
4173 * get data visible to the CPU.
4174 *
4175 * However, we maintain the display planes as UC, and so
4176 * need to rebind when first used as such.
4177 */
4178 obj->cache_level = I915_CACHE_LLC;
4179 } else
4180 obj->cache_level = I915_CACHE_NONE;
4181
Daniel Vetterd861e332013-07-24 23:25:03 +02004182 trace_i915_gem_object_create(obj);
4183
Chris Wilson05394f32010-11-08 19:18:58 +00004184 return obj;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004185
4186fail:
4187 i915_gem_object_free(obj);
4188
4189 return ERR_PTR(ret);
Daniel Vetterac52bc52010-04-09 19:05:06 +00004190}
4191
Chris Wilson340fbd82014-05-22 09:16:52 +01004192static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4193{
4194 /* If we are the last user of the backing storage (be it shmemfs
4195 * pages or stolen etc), we know that the pages are going to be
4196 * immediately released. In this case, we can then skip copying
4197 * back the contents from the GPU.
4198 */
4199
4200 if (obj->madv != I915_MADV_WILLNEED)
4201 return false;
4202
4203 if (obj->base.filp == NULL)
4204 return true;
4205
4206 /* At first glance, this looks racy, but then again so would be
4207 * userspace racing mmap against close. However, the first external
4208 * reference to the filp can only be obtained through the
4209 * i915_gem_mmap_ioctl() which safeguards us against the user
4210 * acquiring such a reference whilst we are in the middle of
4211 * freeing the object.
4212 */
4213 return atomic_long_read(&obj->base.filp->f_count) == 1;
4214}
4215
Chris Wilson1488fc02012-04-24 15:47:31 +01004216void i915_gem_free_object(struct drm_gem_object *gem_obj)
Chris Wilsonbe726152010-07-23 23:18:50 +01004217{
Chris Wilson1488fc02012-04-24 15:47:31 +01004218 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00004219 struct drm_device *dev = obj->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004220 struct drm_i915_private *dev_priv = to_i915(dev);
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004221 struct i915_vma *vma, *next;
Chris Wilsonbe726152010-07-23 23:18:50 +01004222
Paulo Zanonif65c9162013-11-27 18:20:34 -02004223 intel_runtime_pm_get(dev_priv);
4224
Chris Wilson26e12f82011-03-20 11:20:19 +00004225 trace_i915_gem_object_destroy(obj);
4226
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004227 list_for_each_entry_safe(vma, next, &obj->vma_list, obj_link) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004228 int ret;
4229
4230 vma->pin_count = 0;
Chris Wilsonc13d87e2016-07-20 09:21:15 +01004231 ret = __i915_vma_unbind_no_wait(vma);
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004232 if (WARN_ON(ret == -ERESTARTSYS)) {
4233 bool was_interruptible;
Chris Wilson1488fc02012-04-24 15:47:31 +01004234
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004235 was_interruptible = dev_priv->mm.interruptible;
4236 dev_priv->mm.interruptible = false;
Chris Wilson1488fc02012-04-24 15:47:31 +01004237
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004238 WARN_ON(i915_vma_unbind(vma));
Chris Wilson1488fc02012-04-24 15:47:31 +01004239
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004240 dev_priv->mm.interruptible = was_interruptible;
4241 }
Chris Wilson1488fc02012-04-24 15:47:31 +01004242 }
Chris Wilson15717de2016-08-04 07:52:26 +01004243 GEM_BUG_ON(obj->bind_count);
Chris Wilson1488fc02012-04-24 15:47:31 +01004244
Ben Widawsky1d64ae72013-05-31 14:46:20 -07004245 /* Stolen objects don't hold a ref, but do hold pin count. Fix that up
4246 * before progressing. */
4247 if (obj->stolen)
4248 i915_gem_object_unpin_pages(obj);
4249
Daniel Vettera071fa02014-06-18 23:28:09 +02004250 WARN_ON(obj->frontbuffer_bits);
4251
Daniel Vetter656bfa32014-11-20 09:26:30 +01004252 if (obj->pages && obj->madv == I915_MADV_WILLNEED &&
4253 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES &&
4254 obj->tiling_mode != I915_TILING_NONE)
4255 i915_gem_object_unpin_pages(obj);
4256
Ben Widawsky401c29f2013-05-31 11:28:47 -07004257 if (WARN_ON(obj->pages_pin_count))
4258 obj->pages_pin_count = 0;
Chris Wilson340fbd82014-05-22 09:16:52 +01004259 if (discard_backing_storage(obj))
Chris Wilson55372522014-03-25 13:23:06 +00004260 obj->madv = I915_MADV_DONTNEED;
Chris Wilson37e680a2012-06-07 15:38:42 +01004261 i915_gem_object_put_pages(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01004262
Chris Wilson9da3da62012-06-01 15:20:22 +01004263 BUG_ON(obj->pages);
4264
Chris Wilson2f745ad2012-09-04 21:02:58 +01004265 if (obj->base.import_attach)
4266 drm_prime_gem_destroy(&obj->base, NULL);
Chris Wilsonbe726152010-07-23 23:18:50 +01004267
Chris Wilson5cc9ed42014-05-16 14:22:37 +01004268 if (obj->ops->release)
4269 obj->ops->release(obj);
4270
Chris Wilson05394f32010-11-08 19:18:58 +00004271 drm_gem_object_release(&obj->base);
4272 i915_gem_info_remove_obj(dev_priv, obj->base.size);
Chris Wilsonbe726152010-07-23 23:18:50 +01004273
Chris Wilson05394f32010-11-08 19:18:58 +00004274 kfree(obj->bit_17);
Chris Wilson42dcedd2012-11-15 11:32:30 +00004275 i915_gem_object_free(obj);
Paulo Zanonif65c9162013-11-27 18:20:34 -02004276
4277 intel_runtime_pm_put(dev_priv);
Chris Wilsonbe726152010-07-23 23:18:50 +01004278}
4279
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004280struct i915_vma *i915_gem_obj_to_vma(struct drm_i915_gem_object *obj,
4281 struct i915_address_space *vm)
Ben Widawsky2f633152013-07-17 12:19:03 -07004282{
Daniel Vettere656a6c2013-08-14 14:14:04 +02004283 struct i915_vma *vma;
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004284 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Tvrtko Ursulin1b683722015-11-12 11:59:55 +00004285 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL &&
4286 vma->vm == vm)
Daniel Vettere656a6c2013-08-14 14:14:04 +02004287 return vma;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004288 }
4289 return NULL;
4290}
Daniel Vettere656a6c2013-08-14 14:14:04 +02004291
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004292struct i915_vma *i915_gem_obj_to_ggtt_view(struct drm_i915_gem_object *obj,
4293 const struct i915_ggtt_view *view)
4294{
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004295 struct i915_vma *vma;
4296
Tvrtko Ursulin598b9ec2016-04-21 13:04:44 +01004297 GEM_BUG_ON(!view);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004298
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004299 list_for_each_entry(vma, &obj->vma_list, obj_link)
Tvrtko Ursulin598b9ec2016-04-21 13:04:44 +01004300 if (vma->is_ggtt && i915_ggtt_view_equal(&vma->ggtt_view, view))
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004301 return vma;
Daniel Vettere656a6c2013-08-14 14:14:04 +02004302 return NULL;
4303}
4304
Ben Widawsky2f633152013-07-17 12:19:03 -07004305void i915_gem_vma_destroy(struct i915_vma *vma)
4306{
4307 WARN_ON(vma->node.allocated);
Chris Wilsonaaa056672013-08-20 12:56:40 +01004308
4309 /* Keep the vma as a placeholder in the execbuffer reservation lists */
4310 if (!list_empty(&vma->exec_list))
4311 return;
4312
Chris Wilson596c5922016-02-26 11:03:20 +00004313 if (!vma->is_ggtt)
4314 i915_ppgtt_put(i915_vm_to_ppgtt(vma->vm));
Michel Thierryb9d06dd2014-08-06 15:04:44 +02004315
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004316 list_del(&vma->obj_link);
Daniel Vetterb93dab62013-08-26 11:23:47 +02004317
Chris Wilsone20d2ab2015-04-07 16:20:58 +01004318 kmem_cache_free(to_i915(vma->obj->base.dev)->vmas, vma);
Ben Widawsky2f633152013-07-17 12:19:03 -07004319}
4320
Chris Wilsone3efda42014-04-09 09:19:41 +01004321static void
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004322i915_gem_stop_engines(struct drm_device *dev)
Chris Wilsone3efda42014-04-09 09:19:41 +01004323{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004324 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004325 struct intel_engine_cs *engine;
Chris Wilsone3efda42014-04-09 09:19:41 +01004326
Dave Gordonb4ac5af2016-03-24 11:20:38 +00004327 for_each_engine(engine, dev_priv)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004328 dev_priv->gt.stop_engine(engine);
Chris Wilsone3efda42014-04-09 09:19:41 +01004329}
4330
Jesse Barnes5669fca2009-02-17 15:13:31 -08004331int
Chris Wilson45c5f202013-10-16 11:50:01 +01004332i915_gem_suspend(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07004333{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004334 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson45c5f202013-10-16 11:50:01 +01004335 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004336
Chris Wilson54b4f682016-07-21 21:16:19 +01004337 intel_suspend_gt_powersave(dev_priv);
4338
Chris Wilson45c5f202013-10-16 11:50:01 +01004339 mutex_lock(&dev->struct_mutex);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004340
4341 /* We have to flush all the executing contexts to main memory so
4342 * that they can saved in the hibernation image. To ensure the last
4343 * context image is coherent, we have to switch away from it. That
4344 * leaves the dev_priv->kernel_context still active when
4345 * we actually suspend, and its image in memory may not match the GPU
4346 * state. Fortunately, the kernel_context is disposable and we do
4347 * not rely on its state.
4348 */
4349 ret = i915_gem_switch_to_kernel_context(dev_priv);
4350 if (ret)
4351 goto err;
4352
Chris Wilson6e5a5be2016-06-24 14:55:57 +01004353 ret = i915_gem_wait_for_idle(dev_priv);
Chris Wilsonf7403342013-09-13 23:57:04 +01004354 if (ret)
Chris Wilson45c5f202013-10-16 11:50:01 +01004355 goto err;
Chris Wilsonf7403342013-09-13 23:57:04 +01004356
Chris Wilsonc0336662016-05-06 15:40:21 +01004357 i915_gem_retire_requests(dev_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004358
Chris Wilson5ab57c72016-07-15 14:56:20 +01004359 /* Note that rather than stopping the engines, all we have to do
4360 * is assert that every RING_HEAD == RING_TAIL (all execution complete)
4361 * and similar for all logical context images (to ensure they are
4362 * all ready for hibernation).
4363 */
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004364 i915_gem_stop_engines(dev);
Chris Wilsonb2e862d2016-04-28 09:56:41 +01004365 i915_gem_context_lost(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01004366 mutex_unlock(&dev->struct_mutex);
4367
Chris Wilson737b1502015-01-26 18:03:03 +02004368 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
Chris Wilson67d97da2016-07-04 08:08:31 +01004369 cancel_delayed_work_sync(&dev_priv->gt.retire_work);
4370 flush_delayed_work(&dev_priv->gt.idle_work);
Chris Wilson29105cc2010-01-07 10:39:13 +00004371
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004372 /* Assert that we sucessfully flushed all the work and
4373 * reset the GPU back to its idle, low power state.
4374 */
Chris Wilson67d97da2016-07-04 08:08:31 +01004375 WARN_ON(dev_priv->gt.awake);
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004376
Eric Anholt673a3942008-07-30 12:06:12 -07004377 return 0;
Chris Wilson45c5f202013-10-16 11:50:01 +01004378
4379err:
4380 mutex_unlock(&dev->struct_mutex);
4381 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004382}
4383
Chris Wilson5ab57c72016-07-15 14:56:20 +01004384void i915_gem_resume(struct drm_device *dev)
4385{
4386 struct drm_i915_private *dev_priv = to_i915(dev);
4387
4388 mutex_lock(&dev->struct_mutex);
4389 i915_gem_restore_gtt_mappings(dev);
4390
4391 /* As we didn't flush the kernel context before suspend, we cannot
4392 * guarantee that the context image is complete. So let's just reset
4393 * it and start again.
4394 */
4395 if (i915.enable_execlists)
4396 intel_lr_context_reset(dev_priv, dev_priv->kernel_context);
4397
4398 mutex_unlock(&dev->struct_mutex);
4399}
4400
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004401void i915_gem_init_swizzling(struct drm_device *dev)
4402{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004403 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004404
Daniel Vetter11782b02012-01-31 16:47:55 +01004405 if (INTEL_INFO(dev)->gen < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004406 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
4407 return;
4408
4409 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
4410 DISP_TILE_SURFACE_SWIZZLING);
4411
Daniel Vetter11782b02012-01-31 16:47:55 +01004412 if (IS_GEN5(dev))
4413 return;
4414
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004415 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
4416 if (IS_GEN6(dev))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004417 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Ben Widawsky8782e262012-12-18 10:31:23 -08004418 else if (IS_GEN7(dev))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004419 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Ben Widawsky31a53362013-11-02 21:07:04 -07004420 else if (IS_GEN8(dev))
4421 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08004422 else
4423 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004424}
Daniel Vettere21af882012-02-09 20:53:27 +01004425
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004426static void init_unused_ring(struct drm_device *dev, u32 base)
4427{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004428 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004429
4430 I915_WRITE(RING_CTL(base), 0);
4431 I915_WRITE(RING_HEAD(base), 0);
4432 I915_WRITE(RING_TAIL(base), 0);
4433 I915_WRITE(RING_START(base), 0);
4434}
4435
4436static void init_unused_rings(struct drm_device *dev)
4437{
4438 if (IS_I830(dev)) {
4439 init_unused_ring(dev, PRB1_BASE);
4440 init_unused_ring(dev, SRB0_BASE);
4441 init_unused_ring(dev, SRB1_BASE);
4442 init_unused_ring(dev, SRB2_BASE);
4443 init_unused_ring(dev, SRB3_BASE);
4444 } else if (IS_GEN2(dev)) {
4445 init_unused_ring(dev, SRB0_BASE);
4446 init_unused_ring(dev, SRB1_BASE);
4447 } else if (IS_GEN3(dev)) {
4448 init_unused_ring(dev, PRB1_BASE);
4449 init_unused_ring(dev, PRB2_BASE);
4450 }
4451}
4452
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004453int
4454i915_gem_init_hw(struct drm_device *dev)
4455{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004456 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004457 struct intel_engine_cs *engine;
Chris Wilsond200cda2016-04-28 09:56:44 +01004458 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004459
Chris Wilson5e4f5182015-02-13 14:35:59 +00004460 /* Double layer security blanket, see i915_gem_init() */
4461 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4462
Mika Kuoppala3accaf72016-04-13 17:26:43 +03004463 if (HAS_EDRAM(dev) && INTEL_GEN(dev_priv) < 9)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07004464 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004465
Ville Syrjälä0bf21342013-11-29 14:56:12 +02004466 if (IS_HASWELL(dev))
4467 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev) ?
4468 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03004469
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07004470 if (HAS_PCH_NOP(dev)) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01004471 if (IS_IVYBRIDGE(dev)) {
4472 u32 temp = I915_READ(GEN7_MSG_CTL);
4473 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
4474 I915_WRITE(GEN7_MSG_CTL, temp);
4475 } else if (INTEL_INFO(dev)->gen >= 7) {
4476 u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
4477 temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
4478 I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
4479 }
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07004480 }
4481
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004482 i915_gem_init_swizzling(dev);
4483
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004484 /*
4485 * At least 830 can leave some of the unused rings
4486 * "active" (ie. head != tail) after resume which
4487 * will prevent c3 entry. Makes sure all unused rings
4488 * are totally idle.
4489 */
4490 init_unused_rings(dev);
4491
Dave Gordoned54c1a2016-01-19 19:02:54 +00004492 BUG_ON(!dev_priv->kernel_context);
John Harrison90638cc2015-05-29 17:43:37 +01004493
John Harrison4ad2fd82015-06-18 13:11:20 +01004494 ret = i915_ppgtt_init_hw(dev);
4495 if (ret) {
4496 DRM_ERROR("PPGTT enable HW failed %d\n", ret);
4497 goto out;
4498 }
4499
4500 /* Need to do basic initialisation of all rings first: */
Dave Gordonb4ac5af2016-03-24 11:20:38 +00004501 for_each_engine(engine, dev_priv) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004502 ret = engine->init_hw(engine);
Daniel Vetter35a57ff2014-11-20 00:33:07 +01004503 if (ret)
Chris Wilson5e4f5182015-02-13 14:35:59 +00004504 goto out;
Daniel Vetter35a57ff2014-11-20 00:33:07 +01004505 }
Mika Kuoppala99433932013-01-22 14:12:17 +02004506
Peter Antoine0ccdacf2016-04-13 15:03:25 +01004507 intel_mocs_init_l3cc_table(dev);
4508
Alex Dai33a732f2015-08-12 15:43:36 +01004509 /* We can't enable contexts until all firmware is loaded */
Dave Gordone556f7c2016-06-07 09:14:49 +01004510 ret = intel_guc_setup(dev);
4511 if (ret)
4512 goto out;
Alex Dai33a732f2015-08-12 15:43:36 +01004513
Chris Wilson5e4f5182015-02-13 14:35:59 +00004514out:
4515 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004516 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004517}
4518
Chris Wilson39df9192016-07-20 13:31:57 +01004519bool intel_sanitize_semaphores(struct drm_i915_private *dev_priv, int value)
4520{
4521 if (INTEL_INFO(dev_priv)->gen < 6)
4522 return false;
4523
4524 /* TODO: make semaphores and Execlists play nicely together */
4525 if (i915.enable_execlists)
4526 return false;
4527
4528 if (value >= 0)
4529 return value;
4530
4531#ifdef CONFIG_INTEL_IOMMU
4532 /* Enable semaphores on SNB when IO remapping is off */
4533 if (INTEL_INFO(dev_priv)->gen == 6 && intel_iommu_gfx_mapped)
4534 return false;
4535#endif
4536
4537 return true;
4538}
4539
Chris Wilson1070a422012-04-24 15:47:41 +01004540int i915_gem_init(struct drm_device *dev)
4541{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004542 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson1070a422012-04-24 15:47:41 +01004543 int ret;
4544
Chris Wilson1070a422012-04-24 15:47:41 +01004545 mutex_lock(&dev->struct_mutex);
Jesse Barnesd62b4892013-03-08 10:45:53 -08004546
Oscar Mateoa83014d2014-07-24 17:04:21 +01004547 if (!i915.enable_execlists) {
Chris Wilson7e37f882016-08-02 22:50:21 +01004548 dev_priv->gt.cleanup_engine = intel_engine_cleanup;
4549 dev_priv->gt.stop_engine = intel_engine_stop;
Oscar Mateo454afeb2014-07-24 17:04:22 +01004550 } else {
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004551 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
4552 dev_priv->gt.stop_engine = intel_logical_ring_stop;
Oscar Mateoa83014d2014-07-24 17:04:21 +01004553 }
4554
Chris Wilson5e4f5182015-02-13 14:35:59 +00004555 /* This is just a security blanket to placate dragons.
4556 * On some systems, we very sporadically observe that the first TLBs
4557 * used by the CS may be stale, despite us poking the TLB reset. If
4558 * we hold the forcewake during initialisation these problems
4559 * just magically go away.
4560 */
4561 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4562
Chris Wilson72778cb2016-05-19 16:17:16 +01004563 i915_gem_init_userptr(dev_priv);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01004564
4565 ret = i915_gem_init_ggtt(dev_priv);
4566 if (ret)
4567 goto out_unlock;
Jesse Barnesd62b4892013-03-08 10:45:53 -08004568
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004569 ret = i915_gem_context_init(dev);
Jani Nikula7bcc3772014-12-05 14:17:42 +02004570 if (ret)
4571 goto out_unlock;
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004572
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01004573 ret = intel_engines_init(dev);
Daniel Vetter35a57ff2014-11-20 00:33:07 +01004574 if (ret)
Jani Nikula7bcc3772014-12-05 14:17:42 +02004575 goto out_unlock;
Daniel Vetter53ca26c2012-04-26 23:28:03 +02004576
4577 ret = i915_gem_init_hw(dev);
Chris Wilson60990322014-04-09 09:19:42 +01004578 if (ret == -EIO) {
Chris Wilson7e21d642016-07-27 09:07:29 +01004579 /* Allow engine initialisation to fail by marking the GPU as
Chris Wilson60990322014-04-09 09:19:42 +01004580 * wedged. But we only want to do this where the GPU is angry,
4581 * for all other failure, such as an allocation failure, bail.
4582 */
4583 DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
Peter Zijlstra805de8f42015-04-24 01:12:32 +02004584 atomic_or(I915_WEDGED, &dev_priv->gpu_error.reset_counter);
Chris Wilson60990322014-04-09 09:19:42 +01004585 ret = 0;
Chris Wilson1070a422012-04-24 15:47:41 +01004586 }
Jani Nikula7bcc3772014-12-05 14:17:42 +02004587
4588out_unlock:
Chris Wilson5e4f5182015-02-13 14:35:59 +00004589 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Chris Wilson60990322014-04-09 09:19:42 +01004590 mutex_unlock(&dev->struct_mutex);
Chris Wilson1070a422012-04-24 15:47:41 +01004591
Chris Wilson60990322014-04-09 09:19:42 +01004592 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01004593}
4594
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004595void
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004596i915_gem_cleanup_engines(struct drm_device *dev)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004597{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004598 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004599 struct intel_engine_cs *engine;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004600
Dave Gordonb4ac5af2016-03-24 11:20:38 +00004601 for_each_engine(engine, dev_priv)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004602 dev_priv->gt.cleanup_engine(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004603}
4604
Chris Wilson64193402010-10-24 12:38:05 +01004605static void
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00004606init_engine_lists(struct intel_engine_cs *engine)
Chris Wilson64193402010-10-24 12:38:05 +01004607{
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00004608 INIT_LIST_HEAD(&engine->active_list);
4609 INIT_LIST_HEAD(&engine->request_list);
Chris Wilson64193402010-10-24 12:38:05 +01004610}
4611
Eric Anholt673a3942008-07-30 12:06:12 -07004612void
Imre Deak40ae4e12016-03-16 14:54:03 +02004613i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
4614{
Chris Wilson91c8a322016-07-05 10:40:23 +01004615 struct drm_device *dev = &dev_priv->drm;
Imre Deak40ae4e12016-03-16 14:54:03 +02004616
4617 if (INTEL_INFO(dev_priv)->gen >= 7 && !IS_VALLEYVIEW(dev_priv) &&
4618 !IS_CHERRYVIEW(dev_priv))
4619 dev_priv->num_fence_regs = 32;
4620 else if (INTEL_INFO(dev_priv)->gen >= 4 || IS_I945G(dev_priv) ||
4621 IS_I945GM(dev_priv) || IS_G33(dev_priv))
4622 dev_priv->num_fence_regs = 16;
4623 else
4624 dev_priv->num_fence_regs = 8;
4625
Chris Wilsonc0336662016-05-06 15:40:21 +01004626 if (intel_vgpu_active(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02004627 dev_priv->num_fence_regs =
4628 I915_READ(vgtif_reg(avail_rs.fence_num));
4629
4630 /* Initialize fence registers to zero */
4631 i915_gem_restore_fences(dev);
4632
4633 i915_gem_detect_bit_6_swizzle(dev);
4634}
4635
4636void
Imre Deakd64aa092016-01-19 15:26:29 +02004637i915_gem_load_init(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07004638{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004639 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson42dcedd2012-11-15 11:32:30 +00004640 int i;
4641
Chris Wilsonefab6d82015-04-07 16:20:57 +01004642 dev_priv->objects =
Chris Wilson42dcedd2012-11-15 11:32:30 +00004643 kmem_cache_create("i915_gem_object",
4644 sizeof(struct drm_i915_gem_object), 0,
4645 SLAB_HWCACHE_ALIGN,
4646 NULL);
Chris Wilsone20d2ab2015-04-07 16:20:58 +01004647 dev_priv->vmas =
4648 kmem_cache_create("i915_gem_vma",
4649 sizeof(struct i915_vma), 0,
4650 SLAB_HWCACHE_ALIGN,
4651 NULL);
Chris Wilsonefab6d82015-04-07 16:20:57 +01004652 dev_priv->requests =
4653 kmem_cache_create("i915_gem_request",
4654 sizeof(struct drm_i915_gem_request), 0,
4655 SLAB_HWCACHE_ALIGN,
4656 NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07004657
Ben Widawskya33afea2013-09-17 21:12:45 -07004658 INIT_LIST_HEAD(&dev_priv->context_list);
Chris Wilson6c085a72012-08-20 11:40:46 +02004659 INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
4660 INIT_LIST_HEAD(&dev_priv->mm.bound_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004661 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00004662 for (i = 0; i < I915_NUM_ENGINES; i++)
4663 init_engine_lists(&dev_priv->engine[i]);
Daniel Vetter4b9de732011-10-09 21:52:02 +02004664 for (i = 0; i < I915_MAX_NUM_FENCES; i++)
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004665 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Chris Wilson67d97da2016-07-04 08:08:31 +01004666 INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
Eric Anholt673a3942008-07-30 12:06:12 -07004667 i915_gem_retire_work_handler);
Chris Wilson67d97da2016-07-04 08:08:31 +01004668 INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004669 i915_gem_idle_work_handler);
Chris Wilson1f15b762016-07-01 17:23:14 +01004670 init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01004671 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson31169712009-09-14 16:50:28 +01004672
Chris Wilson72bfa192010-12-19 11:42:05 +00004673 dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
4674
Chris Wilson19b2dbd2013-06-12 10:15:12 +01004675 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Eric Anholt10ed13e2011-05-06 13:53:49 -07004676
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004677 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01004678
Chris Wilsonce453d82011-02-21 14:43:56 +00004679 dev_priv->mm.interruptible = true;
4680
Daniel Vetterf99d7062014-06-19 16:01:59 +02004681 mutex_init(&dev_priv->fb_tracking.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004682}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004683
Imre Deakd64aa092016-01-19 15:26:29 +02004684void i915_gem_load_cleanup(struct drm_device *dev)
4685{
4686 struct drm_i915_private *dev_priv = to_i915(dev);
4687
4688 kmem_cache_destroy(dev_priv->requests);
4689 kmem_cache_destroy(dev_priv->vmas);
4690 kmem_cache_destroy(dev_priv->objects);
4691}
4692
Chris Wilson461fb992016-05-14 07:26:33 +01004693int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
4694{
4695 struct drm_i915_gem_object *obj;
4696
4697 /* Called just before we write the hibernation image.
4698 *
4699 * We need to update the domain tracking to reflect that the CPU
4700 * will be accessing all the pages to create and restore from the
4701 * hibernation, and so upon restoration those pages will be in the
4702 * CPU domain.
4703 *
4704 * To make sure the hibernation image contains the latest state,
4705 * we update that state just before writing out the image.
4706 */
4707
4708 list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list) {
4709 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4710 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4711 }
4712
4713 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
4714 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4715 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4716 }
4717
4718 return 0;
4719}
4720
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004721void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004722{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004723 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilson15f7bbc2016-07-26 12:01:52 +01004724 struct drm_i915_gem_request *request;
Eric Anholtb9624422009-06-03 07:27:35 +00004725
4726 /* Clean up our request list when the client is going away, so that
4727 * later retire_requests won't dereference our soon-to-be-gone
4728 * file_priv.
4729 */
Chris Wilson1c255952010-09-26 11:03:27 +01004730 spin_lock(&file_priv->mm.lock);
Chris Wilson15f7bbc2016-07-26 12:01:52 +01004731 list_for_each_entry(request, &file_priv->mm.request_list, client_list)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004732 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01004733 spin_unlock(&file_priv->mm.lock);
Chris Wilson31169712009-09-14 16:50:28 +01004734
Chris Wilson2e1b8732015-04-27 13:41:22 +01004735 if (!list_empty(&file_priv->rps.link)) {
Chris Wilson8d3afd72015-05-21 21:01:47 +01004736 spin_lock(&to_i915(dev)->rps.client_lock);
Chris Wilson2e1b8732015-04-27 13:41:22 +01004737 list_del(&file_priv->rps.link);
Chris Wilson8d3afd72015-05-21 21:01:47 +01004738 spin_unlock(&to_i915(dev)->rps.client_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01004739 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004740}
4741
4742int i915_gem_open(struct drm_device *dev, struct drm_file *file)
4743{
4744 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08004745 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004746
4747 DRM_DEBUG_DRIVER("\n");
4748
4749 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
4750 if (!file_priv)
4751 return -ENOMEM;
4752
4753 file->driver_priv = file_priv;
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004754 file_priv->dev_priv = to_i915(dev);
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02004755 file_priv->file = file;
Chris Wilson2e1b8732015-04-27 13:41:22 +01004756 INIT_LIST_HEAD(&file_priv->rps.link);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004757
4758 spin_lock_init(&file_priv->mm.lock);
4759 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004760
Chris Wilsonc80ff162016-07-27 09:07:27 +01004761 file_priv->bsd_engine = -1;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00004762
Ben Widawskye422b882013-12-06 14:10:58 -08004763 ret = i915_gem_context_open(dev, file);
4764 if (ret)
4765 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004766
Ben Widawskye422b882013-12-06 14:10:58 -08004767 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004768}
4769
Daniel Vetterb680c372014-09-19 18:27:27 +02004770/**
4771 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07004772 * @old: current GEM buffer for the frontbuffer slots
4773 * @new: new GEM buffer for the frontbuffer slots
4774 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02004775 *
4776 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
4777 * from @old and setting them in @new. Both @old and @new can be NULL.
4778 */
Daniel Vettera071fa02014-06-18 23:28:09 +02004779void i915_gem_track_fb(struct drm_i915_gem_object *old,
4780 struct drm_i915_gem_object *new,
4781 unsigned frontbuffer_bits)
4782{
4783 if (old) {
4784 WARN_ON(!mutex_is_locked(&old->base.dev->struct_mutex));
4785 WARN_ON(!(old->frontbuffer_bits & frontbuffer_bits));
4786 old->frontbuffer_bits &= ~frontbuffer_bits;
4787 }
4788
4789 if (new) {
4790 WARN_ON(!mutex_is_locked(&new->base.dev->struct_mutex));
4791 WARN_ON(new->frontbuffer_bits & frontbuffer_bits);
4792 new->frontbuffer_bits |= frontbuffer_bits;
4793 }
4794}
4795
Ben Widawskya70a3142013-07-31 16:59:56 -07004796/* All the new VM stuff */
Michel Thierry088e0df2015-08-07 17:40:17 +01004797u64 i915_gem_obj_offset(struct drm_i915_gem_object *o,
4798 struct i915_address_space *vm)
Ben Widawskya70a3142013-07-31 16:59:56 -07004799{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004800 struct drm_i915_private *dev_priv = to_i915(o->base.dev);
Ben Widawskya70a3142013-07-31 16:59:56 -07004801 struct i915_vma *vma;
4802
Daniel Vetter896ab1a2014-08-06 15:04:51 +02004803 WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base);
Ben Widawskya70a3142013-07-31 16:59:56 -07004804
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004805 list_for_each_entry(vma, &o->vma_list, obj_link) {
Chris Wilson596c5922016-02-26 11:03:20 +00004806 if (vma->is_ggtt &&
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004807 vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
4808 continue;
4809 if (vma->vm == vm)
Ben Widawskya70a3142013-07-31 16:59:56 -07004810 return vma->node.start;
Ben Widawskya70a3142013-07-31 16:59:56 -07004811 }
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004812
Daniel Vetterf25748ea2014-06-17 22:34:38 +02004813 WARN(1, "%s vma for this object not found.\n",
4814 i915_is_ggtt(vm) ? "global" : "ppgtt");
Ben Widawskya70a3142013-07-31 16:59:56 -07004815 return -1;
4816}
4817
Michel Thierry088e0df2015-08-07 17:40:17 +01004818u64 i915_gem_obj_ggtt_offset_view(struct drm_i915_gem_object *o,
4819 const struct i915_ggtt_view *view)
Ben Widawskya70a3142013-07-31 16:59:56 -07004820{
4821 struct i915_vma *vma;
4822
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004823 list_for_each_entry(vma, &o->vma_list, obj_link)
Tvrtko Ursulin8aac2222016-04-21 13:04:45 +01004824 if (vma->is_ggtt && i915_ggtt_view_equal(&vma->ggtt_view, view))
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004825 return vma->node.start;
4826
Tvrtko Ursulin5678ad72015-03-17 14:45:29 +00004827 WARN(1, "global vma for this object not found. (view=%u)\n", view->type);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004828 return -1;
4829}
4830
4831bool i915_gem_obj_bound(struct drm_i915_gem_object *o,
4832 struct i915_address_space *vm)
4833{
4834 struct i915_vma *vma;
4835
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004836 list_for_each_entry(vma, &o->vma_list, obj_link) {
Chris Wilson596c5922016-02-26 11:03:20 +00004837 if (vma->is_ggtt &&
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004838 vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
4839 continue;
4840 if (vma->vm == vm && drm_mm_node_allocated(&vma->node))
4841 return true;
4842 }
4843
4844 return false;
4845}
4846
4847bool i915_gem_obj_ggtt_bound_view(struct drm_i915_gem_object *o,
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02004848 const struct i915_ggtt_view *view)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004849{
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004850 struct i915_vma *vma;
4851
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004852 list_for_each_entry(vma, &o->vma_list, obj_link)
Tvrtko Ursulinff5ec222016-04-21 13:04:46 +01004853 if (vma->is_ggtt &&
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02004854 i915_ggtt_view_equal(&vma->ggtt_view, view) &&
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00004855 drm_mm_node_allocated(&vma->node))
Ben Widawskya70a3142013-07-31 16:59:56 -07004856 return true;
4857
4858 return false;
4859}
4860
Tvrtko Ursulin8da32722016-04-21 13:04:43 +01004861unsigned long i915_gem_obj_ggtt_size(struct drm_i915_gem_object *o)
Ben Widawskya70a3142013-07-31 16:59:56 -07004862{
Ben Widawskya70a3142013-07-31 16:59:56 -07004863 struct i915_vma *vma;
4864
Tvrtko Ursulin8da32722016-04-21 13:04:43 +01004865 GEM_BUG_ON(list_empty(&o->vma_list));
Ben Widawskya70a3142013-07-31 16:59:56 -07004866
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004867 list_for_each_entry(vma, &o->vma_list, obj_link) {
Chris Wilson596c5922016-02-26 11:03:20 +00004868 if (vma->is_ggtt &&
Tvrtko Ursulin8da32722016-04-21 13:04:43 +01004869 vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
Ben Widawskya70a3142013-07-31 16:59:56 -07004870 return vma->node.size;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004871 }
Tvrtko Ursulin8da32722016-04-21 13:04:43 +01004872
Ben Widawskya70a3142013-07-31 16:59:56 -07004873 return 0;
4874}
4875
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004876bool i915_gem_obj_is_pinned(struct drm_i915_gem_object *obj)
Ben Widawsky5c2abbe2013-09-24 09:57:57 -07004877{
4878 struct i915_vma *vma;
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004879 list_for_each_entry(vma, &obj->vma_list, obj_link)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004880 if (vma->pin_count > 0)
4881 return true;
Joonas Lahtinena6631ae2015-05-06 14:34:58 +03004882
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004883 return false;
Ben Widawsky5c2abbe2013-09-24 09:57:57 -07004884}
Dave Gordonea702992015-07-09 19:29:02 +01004885
Dave Gordon033908a2015-12-10 18:51:23 +00004886/* Like i915_gem_object_get_page(), but mark the returned page dirty */
4887struct page *
4888i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj, int n)
4889{
4890 struct page *page;
4891
4892 /* Only default objects have per-page dirty tracking */
Chris Wilsonb9bcd142016-06-20 15:05:51 +01004893 if (WARN_ON(!i915_gem_object_has_struct_page(obj)))
Dave Gordon033908a2015-12-10 18:51:23 +00004894 return NULL;
4895
4896 page = i915_gem_object_get_page(obj, n);
4897 set_page_dirty(page);
4898 return page;
4899}
4900
Dave Gordonea702992015-07-09 19:29:02 +01004901/* Allocate a new GEM object and fill it with the supplied data */
4902struct drm_i915_gem_object *
4903i915_gem_object_create_from_data(struct drm_device *dev,
4904 const void *data, size_t size)
4905{
4906 struct drm_i915_gem_object *obj;
4907 struct sg_table *sg;
4908 size_t bytes;
4909 int ret;
4910
Dave Gordond37cd8a2016-04-22 19:14:32 +01004911 obj = i915_gem_object_create(dev, round_up(size, PAGE_SIZE));
Chris Wilsonfe3db792016-04-25 13:32:13 +01004912 if (IS_ERR(obj))
Dave Gordonea702992015-07-09 19:29:02 +01004913 return obj;
4914
4915 ret = i915_gem_object_set_to_cpu_domain(obj, true);
4916 if (ret)
4917 goto fail;
4918
4919 ret = i915_gem_object_get_pages(obj);
4920 if (ret)
4921 goto fail;
4922
4923 i915_gem_object_pin_pages(obj);
4924 sg = obj->pages;
4925 bytes = sg_copy_from_buffer(sg->sgl, sg->nents, (void *)data, size);
Dave Gordon9e7d18c2015-12-10 18:51:24 +00004926 obj->dirty = 1; /* Backing store is now out of date */
Dave Gordonea702992015-07-09 19:29:02 +01004927 i915_gem_object_unpin_pages(obj);
4928
4929 if (WARN_ON(bytes != size)) {
4930 DRM_ERROR("Incomplete copy, wrote %zu of %zu", bytes, size);
4931 ret = -EFAULT;
4932 goto fail;
4933 }
4934
4935 return obj;
4936
4937fail:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004938 i915_gem_object_put(obj);
Dave Gordonea702992015-07-09 19:29:02 +01004939 return ERR_PTR(ret);
4940}