blob: 5f7b8c88eb7e6f804c102ec5916e8c316bbecb1d [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
Daniel Vetterbe6a0372015-03-18 10:46:04 +01002 * Copyright © 2008-2015 Intel Corporation
Eric Anholt673a3942008-07-30 12:06:12 -07003 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
David Howells760285e2012-10-02 18:01:07 +010028#include <drm/drmP.h>
David Herrmann0de23972013-07-24 21:07:52 +020029#include <drm/drm_vma_manager.h>
David Howells760285e2012-10-02 18:01:07 +010030#include <drm/i915_drm.h>
Eric Anholt673a3942008-07-30 12:06:12 -070031#include "i915_drv.h"
Yu Zhangeb822892015-02-10 19:05:49 +080032#include "i915_vgpu.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010033#include "i915_trace.h"
Jesse Barnes652c3932009-08-17 13:31:43 -070034#include "intel_drv.h"
Chris Wilson5d723d72016-08-04 16:32:35 +010035#include "intel_frontbuffer.h"
Peter Antoine0ccdacf2016-04-13 15:03:25 +010036#include "intel_mocs.h"
Chris Wilson6b5e90f2016-11-14 20:41:05 +000037#include <linux/dma-fence-array.h>
Chris Wilsonfe3288b2017-02-12 17:20:01 +000038#include <linux/kthread.h>
Chris Wilsonc13d87e2016-07-20 09:21:15 +010039#include <linux/reservation.h>
Hugh Dickins5949eac2011-06-27 16:18:18 -070040#include <linux/shmem_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090041#include <linux/slab.h>
Chris Wilson20e49332016-11-22 14:41:21 +000042#include <linux/stop_machine.h>
Eric Anholt673a3942008-07-30 12:06:12 -070043#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080044#include <linux/pci.h>
Daniel Vetter1286ff72012-05-10 15:25:09 +020045#include <linux/dma-buf.h>
Eric Anholt673a3942008-07-30 12:06:12 -070046
Chris Wilsonfbbd37b2016-10-28 13:58:42 +010047static void i915_gem_flush_free_objects(struct drm_i915_private *i915);
Chris Wilson05394f32010-11-08 19:18:58 +000048static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
Daniel Vettere62b59e2015-01-21 14:53:48 +010049static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
Chris Wilson61050802012-04-17 15:31:31 +010050
Chris Wilsonc76ce032013-08-08 14:41:03 +010051static bool cpu_cache_is_coherent(struct drm_device *dev,
52 enum i915_cache_level level)
53{
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +000054 return HAS_LLC(to_i915(dev)) || level != I915_CACHE_NONE;
Chris Wilsonc76ce032013-08-08 14:41:03 +010055}
56
Chris Wilson2c225692013-08-09 12:26:45 +010057static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
58{
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +053059 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
60 return false;
61
Chris Wilson2c225692013-08-09 12:26:45 +010062 if (!cpu_cache_is_coherent(obj->base.dev, obj->cache_level))
63 return true;
64
65 return obj->pin_display;
66}
67
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053068static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +010069insert_mappable_node(struct i915_ggtt *ggtt,
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053070 struct drm_mm_node *node, u32 size)
71{
72 memset(node, 0, sizeof(*node));
Chris Wilson4e64e552017-02-02 21:04:38 +000073 return drm_mm_insert_node_in_range(&ggtt->base.mm, node,
74 size, 0, I915_COLOR_UNEVICTABLE,
75 0, ggtt->mappable_end,
76 DRM_MM_INSERT_LOW);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053077}
78
79static void
80remove_mappable_node(struct drm_mm_node *node)
81{
82 drm_mm_remove_node(node);
83}
84
Chris Wilson73aa8082010-09-30 11:46:12 +010085/* some bookkeeping */
86static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010087 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010088{
Daniel Vetterc20e8352013-07-24 22:40:23 +020089 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010090 dev_priv->mm.object_count++;
91 dev_priv->mm.object_memory += size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020092 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010093}
94
95static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010096 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010097{
Daniel Vetterc20e8352013-07-24 22:40:23 +020098 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010099 dev_priv->mm.object_count--;
100 dev_priv->mm.object_memory -= size;
Daniel Vetterc20e8352013-07-24 22:40:23 +0200101 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +0100102}
103
Chris Wilson21dd3732011-01-26 15:55:56 +0000104static int
Daniel Vetter33196de2012-11-14 17:14:05 +0100105i915_gem_wait_for_error(struct i915_gpu_error *error)
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100106{
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100107 int ret;
108
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100109 might_sleep();
110
Chris Wilsond98c52c2016-04-13 17:35:05 +0100111 if (!i915_reset_in_progress(error))
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100112 return 0;
113
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200114 /*
115 * Only wait 10 seconds for the gpu reset to complete to avoid hanging
116 * userspace. If it takes that long something really bad is going on and
117 * we should simply try to bail out and fail as gracefully as possible.
118 */
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100119 ret = wait_event_interruptible_timeout(error->reset_queue,
Chris Wilsond98c52c2016-04-13 17:35:05 +0100120 !i915_reset_in_progress(error),
Chris Wilsonb52992c2016-10-28 13:58:24 +0100121 I915_RESET_TIMEOUT);
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200122 if (ret == 0) {
123 DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
124 return -EIO;
125 } else if (ret < 0) {
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100126 return ret;
Chris Wilsond98c52c2016-04-13 17:35:05 +0100127 } else {
128 return 0;
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200129 }
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100130}
131
Chris Wilson54cf91d2010-11-25 18:00:26 +0000132int i915_mutex_lock_interruptible(struct drm_device *dev)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100133{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100134 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100135 int ret;
136
Daniel Vetter33196de2012-11-14 17:14:05 +0100137 ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100138 if (ret)
139 return ret;
140
141 ret = mutex_lock_interruptible(&dev->struct_mutex);
142 if (ret)
143 return ret;
144
Chris Wilson76c1dec2010-09-25 11:22:51 +0100145 return 0;
146}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100147
Eric Anholt673a3942008-07-30 12:06:12 -0700148int
Eric Anholt5a125c32008-10-22 21:40:13 -0700149i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000150 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700151{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300152 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen62106b42016-03-18 10:42:57 +0200153 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300154 struct drm_i915_gem_get_aperture *args = data;
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100155 struct i915_vma *vma;
Chris Wilson6299f992010-11-24 12:23:44 +0000156 size_t pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700157
Chris Wilson6299f992010-11-24 12:23:44 +0000158 pinned = 0;
Chris Wilson73aa8082010-09-30 11:46:12 +0100159 mutex_lock(&dev->struct_mutex);
Chris Wilson1c7f4bc2016-02-26 11:03:19 +0000160 list_for_each_entry(vma, &ggtt->base.active_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100161 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100162 pinned += vma->node.size;
Chris Wilson1c7f4bc2016-02-26 11:03:19 +0000163 list_for_each_entry(vma, &ggtt->base.inactive_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100164 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100165 pinned += vma->node.size;
Chris Wilson73aa8082010-09-30 11:46:12 +0100166 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700167
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300168 args->aper_size = ggtt->base.total;
Akshay Joshi0206e352011-08-16 15:34:10 -0400169 args->aper_available_size = args->aper_size - pinned;
Chris Wilson6299f992010-11-24 12:23:44 +0000170
Eric Anholt5a125c32008-10-22 21:40:13 -0700171 return 0;
172}
173
Chris Wilson03ac84f2016-10-28 13:58:36 +0100174static struct sg_table *
Chris Wilson6a2c4232014-11-04 04:51:40 -0800175i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
Chris Wilson00731152014-05-21 12:42:56 +0100176{
Al Viro93c76a32015-12-04 23:45:44 -0500177 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilsondbb43512016-12-07 13:34:11 +0000178 drm_dma_handle_t *phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800179 struct sg_table *st;
180 struct scatterlist *sg;
Chris Wilsondbb43512016-12-07 13:34:11 +0000181 char *vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800182 int i;
Chris Wilson00731152014-05-21 12:42:56 +0100183
Chris Wilson6a2c4232014-11-04 04:51:40 -0800184 if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
Chris Wilson03ac84f2016-10-28 13:58:36 +0100185 return ERR_PTR(-EINVAL);
Chris Wilson00731152014-05-21 12:42:56 +0100186
Chris Wilsondbb43512016-12-07 13:34:11 +0000187 /* Always aligning to the object size, allows a single allocation
188 * to handle all possible callers, and given typical object sizes,
189 * the alignment of the buddy allocation will naturally match.
190 */
191 phys = drm_pci_alloc(obj->base.dev,
192 obj->base.size,
193 roundup_pow_of_two(obj->base.size));
194 if (!phys)
195 return ERR_PTR(-ENOMEM);
196
197 vaddr = phys->vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800198 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
199 struct page *page;
200 char *src;
201
202 page = shmem_read_mapping_page(mapping, i);
Chris Wilsondbb43512016-12-07 13:34:11 +0000203 if (IS_ERR(page)) {
204 st = ERR_CAST(page);
205 goto err_phys;
206 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800207
208 src = kmap_atomic(page);
209 memcpy(vaddr, src, PAGE_SIZE);
210 drm_clflush_virt_range(vaddr, PAGE_SIZE);
211 kunmap_atomic(src);
212
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300213 put_page(page);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800214 vaddr += PAGE_SIZE;
215 }
216
Chris Wilsonc0336662016-05-06 15:40:21 +0100217 i915_gem_chipset_flush(to_i915(obj->base.dev));
Chris Wilson6a2c4232014-11-04 04:51:40 -0800218
219 st = kmalloc(sizeof(*st), GFP_KERNEL);
Chris Wilsondbb43512016-12-07 13:34:11 +0000220 if (!st) {
221 st = ERR_PTR(-ENOMEM);
222 goto err_phys;
223 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800224
225 if (sg_alloc_table(st, 1, GFP_KERNEL)) {
226 kfree(st);
Chris Wilsondbb43512016-12-07 13:34:11 +0000227 st = ERR_PTR(-ENOMEM);
228 goto err_phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800229 }
230
231 sg = st->sgl;
232 sg->offset = 0;
233 sg->length = obj->base.size;
234
Chris Wilsondbb43512016-12-07 13:34:11 +0000235 sg_dma_address(sg) = phys->busaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800236 sg_dma_len(sg) = obj->base.size;
237
Chris Wilsondbb43512016-12-07 13:34:11 +0000238 obj->phys_handle = phys;
239 return st;
240
241err_phys:
242 drm_pci_free(obj->base.dev, phys);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100243 return st;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800244}
245
246static void
Chris Wilson2b3c8312016-11-11 14:58:09 +0000247__i915_gem_object_release_shmem(struct drm_i915_gem_object *obj,
Chris Wilsone5facdf2016-12-23 14:57:57 +0000248 struct sg_table *pages,
249 bool needs_clflush)
Chris Wilson6a2c4232014-11-04 04:51:40 -0800250{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100251 GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800252
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100253 if (obj->mm.madv == I915_MADV_DONTNEED)
254 obj->mm.dirty = false;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800255
Chris Wilsone5facdf2016-12-23 14:57:57 +0000256 if (needs_clflush &&
257 (obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0 &&
Chris Wilson05c34832016-11-18 21:17:47 +0000258 !cpu_cache_is_coherent(obj->base.dev, obj->cache_level))
Chris Wilson2b3c8312016-11-11 14:58:09 +0000259 drm_clflush_sg(pages);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100260
261 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
262 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
263}
264
265static void
266i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj,
267 struct sg_table *pages)
268{
Chris Wilsone5facdf2016-12-23 14:57:57 +0000269 __i915_gem_object_release_shmem(obj, pages, false);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100270
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100271 if (obj->mm.dirty) {
Al Viro93c76a32015-12-04 23:45:44 -0500272 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800273 char *vaddr = obj->phys_handle->vaddr;
Chris Wilson00731152014-05-21 12:42:56 +0100274 int i;
275
276 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
Chris Wilson6a2c4232014-11-04 04:51:40 -0800277 struct page *page;
278 char *dst;
Chris Wilson00731152014-05-21 12:42:56 +0100279
Chris Wilson6a2c4232014-11-04 04:51:40 -0800280 page = shmem_read_mapping_page(mapping, i);
281 if (IS_ERR(page))
282 continue;
283
284 dst = kmap_atomic(page);
285 drm_clflush_virt_range(vaddr, PAGE_SIZE);
286 memcpy(dst, vaddr, PAGE_SIZE);
287 kunmap_atomic(dst);
288
289 set_page_dirty(page);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100290 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson00731152014-05-21 12:42:56 +0100291 mark_page_accessed(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300292 put_page(page);
Chris Wilson00731152014-05-21 12:42:56 +0100293 vaddr += PAGE_SIZE;
294 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100295 obj->mm.dirty = false;
Chris Wilson00731152014-05-21 12:42:56 +0100296 }
297
Chris Wilson03ac84f2016-10-28 13:58:36 +0100298 sg_free_table(pages);
299 kfree(pages);
Chris Wilsondbb43512016-12-07 13:34:11 +0000300
301 drm_pci_free(obj->base.dev, obj->phys_handle);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800302}
303
304static void
305i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
306{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100307 i915_gem_object_unpin_pages(obj);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800308}
309
310static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
311 .get_pages = i915_gem_object_get_pages_phys,
312 .put_pages = i915_gem_object_put_pages_phys,
313 .release = i915_gem_object_release_phys,
314};
315
Chris Wilson581ab1f2017-02-15 16:39:00 +0000316static const struct drm_i915_gem_object_ops i915_gem_object_ops;
317
Chris Wilson35a96112016-08-14 18:44:40 +0100318int i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Chris Wilsonaa653a62016-08-04 07:52:27 +0100319{
320 struct i915_vma *vma;
321 LIST_HEAD(still_in_list);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100322 int ret;
Chris Wilsonaa653a62016-08-04 07:52:27 +0100323
Chris Wilson02bef8f2016-08-14 18:44:41 +0100324 lockdep_assert_held(&obj->base.dev->struct_mutex);
325
326 /* Closed vma are removed from the obj->vma_list - but they may
327 * still have an active binding on the object. To remove those we
328 * must wait for all rendering to complete to the object (as unbinding
329 * must anyway), and retire the requests.
Chris Wilsonaa653a62016-08-04 07:52:27 +0100330 */
Chris Wilsone95433c2016-10-28 13:58:27 +0100331 ret = i915_gem_object_wait(obj,
332 I915_WAIT_INTERRUPTIBLE |
333 I915_WAIT_LOCKED |
334 I915_WAIT_ALL,
335 MAX_SCHEDULE_TIMEOUT,
336 NULL);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100337 if (ret)
338 return ret;
339
340 i915_gem_retire_requests(to_i915(obj->base.dev));
341
Chris Wilsonaa653a62016-08-04 07:52:27 +0100342 while ((vma = list_first_entry_or_null(&obj->vma_list,
343 struct i915_vma,
344 obj_link))) {
345 list_move_tail(&vma->obj_link, &still_in_list);
346 ret = i915_vma_unbind(vma);
347 if (ret)
348 break;
349 }
350 list_splice(&still_in_list, &obj->vma_list);
351
352 return ret;
353}
354
Chris Wilsone95433c2016-10-28 13:58:27 +0100355static long
356i915_gem_object_wait_fence(struct dma_fence *fence,
357 unsigned int flags,
358 long timeout,
359 struct intel_rps_client *rps)
360{
361 struct drm_i915_gem_request *rq;
362
363 BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1);
364
365 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
366 return timeout;
367
368 if (!dma_fence_is_i915(fence))
369 return dma_fence_wait_timeout(fence,
370 flags & I915_WAIT_INTERRUPTIBLE,
371 timeout);
372
373 rq = to_request(fence);
374 if (i915_gem_request_completed(rq))
375 goto out;
376
377 /* This client is about to stall waiting for the GPU. In many cases
378 * this is undesirable and limits the throughput of the system, as
379 * many clients cannot continue processing user input/output whilst
380 * blocked. RPS autotuning may take tens of milliseconds to respond
381 * to the GPU load and thus incurs additional latency for the client.
382 * We can circumvent that by promoting the GPU frequency to maximum
383 * before we wait. This makes the GPU throttle up much more quickly
384 * (good for benchmarks and user experience, e.g. window animations),
385 * but at a cost of spending more power processing the workload
386 * (bad for battery). Not all clients even want their results
387 * immediately and for them we should just let the GPU select its own
388 * frequency to maximise efficiency. To prevent a single client from
389 * forcing the clocks too high for the whole system, we only allow
390 * each client to waitboost once in a busy period.
391 */
392 if (rps) {
393 if (INTEL_GEN(rq->i915) >= 6)
394 gen6_rps_boost(rq->i915, rps, rq->emitted_jiffies);
395 else
396 rps = NULL;
397 }
398
399 timeout = i915_wait_request(rq, flags, timeout);
400
401out:
402 if (flags & I915_WAIT_LOCKED && i915_gem_request_completed(rq))
403 i915_gem_request_retire_upto(rq);
404
Chris Wilsoncb399ea2016-11-01 10:03:16 +0000405 if (rps && rq->global_seqno == intel_engine_last_submit(rq->engine)) {
Chris Wilsone95433c2016-10-28 13:58:27 +0100406 /* The GPU is now idle and this client has stalled.
407 * Since no other client has submitted a request in the
408 * meantime, assume that this client is the only one
409 * supplying work to the GPU but is unable to keep that
410 * work supplied because it is waiting. Since the GPU is
411 * then never kept fully busy, RPS autoclocking will
412 * keep the clocks relatively low, causing further delays.
413 * Compensate by giving the synchronous client credit for
414 * a waitboost next time.
415 */
416 spin_lock(&rq->i915->rps.client_lock);
417 list_del_init(&rps->link);
418 spin_unlock(&rq->i915->rps.client_lock);
419 }
420
421 return timeout;
422}
423
424static long
425i915_gem_object_wait_reservation(struct reservation_object *resv,
426 unsigned int flags,
427 long timeout,
428 struct intel_rps_client *rps)
429{
430 struct dma_fence *excl;
431
432 if (flags & I915_WAIT_ALL) {
433 struct dma_fence **shared;
434 unsigned int count, i;
435 int ret;
436
437 ret = reservation_object_get_fences_rcu(resv,
438 &excl, &count, &shared);
439 if (ret)
440 return ret;
441
442 for (i = 0; i < count; i++) {
443 timeout = i915_gem_object_wait_fence(shared[i],
444 flags, timeout,
445 rps);
Chris Wilsond892e932017-02-12 21:53:43 +0000446 if (timeout < 0)
Chris Wilsone95433c2016-10-28 13:58:27 +0100447 break;
448
449 dma_fence_put(shared[i]);
450 }
451
452 for (; i < count; i++)
453 dma_fence_put(shared[i]);
454 kfree(shared);
455 } else {
456 excl = reservation_object_get_excl_rcu(resv);
457 }
458
Chris Wilsond892e932017-02-12 21:53:43 +0000459 if (excl && timeout >= 0)
Chris Wilsone95433c2016-10-28 13:58:27 +0100460 timeout = i915_gem_object_wait_fence(excl, flags, timeout, rps);
461
462 dma_fence_put(excl);
463
464 return timeout;
465}
466
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000467static void __fence_set_priority(struct dma_fence *fence, int prio)
468{
469 struct drm_i915_gem_request *rq;
470 struct intel_engine_cs *engine;
471
472 if (!dma_fence_is_i915(fence))
473 return;
474
475 rq = to_request(fence);
476 engine = rq->engine;
477 if (!engine->schedule)
478 return;
479
480 engine->schedule(rq, prio);
481}
482
483static void fence_set_priority(struct dma_fence *fence, int prio)
484{
485 /* Recurse once into a fence-array */
486 if (dma_fence_is_array(fence)) {
487 struct dma_fence_array *array = to_dma_fence_array(fence);
488 int i;
489
490 for (i = 0; i < array->num_fences; i++)
491 __fence_set_priority(array->fences[i], prio);
492 } else {
493 __fence_set_priority(fence, prio);
494 }
495}
496
497int
498i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
499 unsigned int flags,
500 int prio)
501{
502 struct dma_fence *excl;
503
504 if (flags & I915_WAIT_ALL) {
505 struct dma_fence **shared;
506 unsigned int count, i;
507 int ret;
508
509 ret = reservation_object_get_fences_rcu(obj->resv,
510 &excl, &count, &shared);
511 if (ret)
512 return ret;
513
514 for (i = 0; i < count; i++) {
515 fence_set_priority(shared[i], prio);
516 dma_fence_put(shared[i]);
517 }
518
519 kfree(shared);
520 } else {
521 excl = reservation_object_get_excl_rcu(obj->resv);
522 }
523
524 if (excl) {
525 fence_set_priority(excl, prio);
526 dma_fence_put(excl);
527 }
528 return 0;
529}
530
Chris Wilson00e60f22016-08-04 16:32:40 +0100531/**
Chris Wilsone95433c2016-10-28 13:58:27 +0100532 * Waits for rendering to the object to be completed
Chris Wilson00e60f22016-08-04 16:32:40 +0100533 * @obj: i915 gem object
Chris Wilsone95433c2016-10-28 13:58:27 +0100534 * @flags: how to wait (under a lock, for all rendering or just for writes etc)
535 * @timeout: how long to wait
536 * @rps: client (user process) to charge for any waitboosting
Chris Wilson00e60f22016-08-04 16:32:40 +0100537 */
538int
Chris Wilsone95433c2016-10-28 13:58:27 +0100539i915_gem_object_wait(struct drm_i915_gem_object *obj,
540 unsigned int flags,
541 long timeout,
542 struct intel_rps_client *rps)
Chris Wilson00e60f22016-08-04 16:32:40 +0100543{
Chris Wilsone95433c2016-10-28 13:58:27 +0100544 might_sleep();
545#if IS_ENABLED(CONFIG_LOCKDEP)
546 GEM_BUG_ON(debug_locks &&
547 !!lockdep_is_held(&obj->base.dev->struct_mutex) !=
548 !!(flags & I915_WAIT_LOCKED));
549#endif
550 GEM_BUG_ON(timeout < 0);
Chris Wilson00e60f22016-08-04 16:32:40 +0100551
Chris Wilsond07f0e52016-10-28 13:58:44 +0100552 timeout = i915_gem_object_wait_reservation(obj->resv,
553 flags, timeout,
554 rps);
Chris Wilsone95433c2016-10-28 13:58:27 +0100555 return timeout < 0 ? timeout : 0;
Chris Wilson00e60f22016-08-04 16:32:40 +0100556}
557
558static struct intel_rps_client *to_rps_client(struct drm_file *file)
559{
560 struct drm_i915_file_private *fpriv = file->driver_priv;
561
562 return &fpriv->rps;
563}
564
Chris Wilson00731152014-05-21 12:42:56 +0100565int
566i915_gem_object_attach_phys(struct drm_i915_gem_object *obj,
567 int align)
568{
Chris Wilson6a2c4232014-11-04 04:51:40 -0800569 int ret;
Chris Wilson00731152014-05-21 12:42:56 +0100570
Chris Wilsondbb43512016-12-07 13:34:11 +0000571 if (align > obj->base.size)
572 return -EINVAL;
Chris Wilson00731152014-05-21 12:42:56 +0100573
Chris Wilsondbb43512016-12-07 13:34:11 +0000574 if (obj->ops == &i915_gem_phys_ops)
Chris Wilson00731152014-05-21 12:42:56 +0100575 return 0;
Chris Wilson00731152014-05-21 12:42:56 +0100576
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100577 if (obj->mm.madv != I915_MADV_WILLNEED)
Chris Wilson00731152014-05-21 12:42:56 +0100578 return -EFAULT;
579
580 if (obj->base.filp == NULL)
581 return -EINVAL;
582
Chris Wilson4717ca92016-08-04 07:52:28 +0100583 ret = i915_gem_object_unbind(obj);
584 if (ret)
585 return ret;
586
Chris Wilson548625e2016-11-01 12:11:34 +0000587 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100588 if (obj->mm.pages)
589 return -EBUSY;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800590
Chris Wilson581ab1f2017-02-15 16:39:00 +0000591 GEM_BUG_ON(obj->ops != &i915_gem_object_ops);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800592 obj->ops = &i915_gem_phys_ops;
593
Chris Wilson581ab1f2017-02-15 16:39:00 +0000594 ret = i915_gem_object_pin_pages(obj);
595 if (ret)
596 goto err_xfer;
597
598 return 0;
599
600err_xfer:
601 obj->ops = &i915_gem_object_ops;
602 return ret;
Chris Wilson00731152014-05-21 12:42:56 +0100603}
604
605static int
606i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
607 struct drm_i915_gem_pwrite *args,
Chris Wilson03ac84f2016-10-28 13:58:36 +0100608 struct drm_file *file)
Chris Wilson00731152014-05-21 12:42:56 +0100609{
Chris Wilson00731152014-05-21 12:42:56 +0100610 void *vaddr = obj->phys_handle->vaddr + args->offset;
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300611 char __user *user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800612
613 /* We manually control the domain here and pretend that it
614 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
615 */
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -0700616 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000617 if (copy_from_user(vaddr, user_data, args->size))
618 return -EFAULT;
Chris Wilson00731152014-05-21 12:42:56 +0100619
Chris Wilson6a2c4232014-11-04 04:51:40 -0800620 drm_clflush_virt_range(vaddr, args->size);
Chris Wilson10466d22017-01-06 15:22:38 +0000621 i915_gem_chipset_flush(to_i915(obj->base.dev));
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200622
Rodrigo Vivide152b62015-07-07 16:28:51 -0700623 intel_fb_obj_flush(obj, false, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000624 return 0;
Chris Wilson00731152014-05-21 12:42:56 +0100625}
626
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000627void *i915_gem_object_alloc(struct drm_i915_private *dev_priv)
Chris Wilson42dcedd2012-11-15 11:32:30 +0000628{
Chris Wilsonefab6d82015-04-07 16:20:57 +0100629 return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000630}
631
632void i915_gem_object_free(struct drm_i915_gem_object *obj)
633{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100634 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonefab6d82015-04-07 16:20:57 +0100635 kmem_cache_free(dev_priv->objects, obj);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000636}
637
Dave Airlieff72145b2011-02-07 12:16:14 +1000638static int
639i915_gem_create(struct drm_file *file,
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000640 struct drm_i915_private *dev_priv,
Dave Airlieff72145b2011-02-07 12:16:14 +1000641 uint64_t size,
642 uint32_t *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700643{
Chris Wilson05394f32010-11-08 19:18:58 +0000644 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300645 int ret;
646 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700647
Dave Airlieff72145b2011-02-07 12:16:14 +1000648 size = roundup(size, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200649 if (size == 0)
650 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700651
652 /* Allocate the new object */
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000653 obj = i915_gem_object_create(dev_priv, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100654 if (IS_ERR(obj))
655 return PTR_ERR(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700656
Chris Wilson05394f32010-11-08 19:18:58 +0000657 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100658 /* drop reference from allocate - handle holds it now */
Chris Wilsonf0cd5182016-10-28 13:58:43 +0100659 i915_gem_object_put(obj);
Daniel Vetterd861e332013-07-24 23:25:03 +0200660 if (ret)
661 return ret;
Chris Wilson202f2fe2010-10-14 13:20:40 +0100662
Dave Airlieff72145b2011-02-07 12:16:14 +1000663 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700664 return 0;
665}
666
Dave Airlieff72145b2011-02-07 12:16:14 +1000667int
668i915_gem_dumb_create(struct drm_file *file,
669 struct drm_device *dev,
670 struct drm_mode_create_dumb *args)
671{
672 /* have to work out size/pitch and return them */
Paulo Zanonide45eaf2013-10-18 18:48:24 -0300673 args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000674 args->size = args->pitch * args->height;
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000675 return i915_gem_create(file, to_i915(dev),
Dave Airlieda6b51d2014-12-24 13:11:17 +1000676 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000677}
678
Dave Airlieff72145b2011-02-07 12:16:14 +1000679/**
680 * Creates a new mm object and returns a handle to it.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100681 * @dev: drm device pointer
682 * @data: ioctl data blob
683 * @file: drm file pointer
Dave Airlieff72145b2011-02-07 12:16:14 +1000684 */
685int
686i915_gem_create_ioctl(struct drm_device *dev, void *data,
687 struct drm_file *file)
688{
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000689 struct drm_i915_private *dev_priv = to_i915(dev);
Dave Airlieff72145b2011-02-07 12:16:14 +1000690 struct drm_i915_gem_create *args = data;
Daniel Vetter63ed2cb2012-04-23 16:50:50 +0200691
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000692 i915_gem_flush_free_objects(dev_priv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +0100693
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000694 return i915_gem_create(file, dev_priv,
Dave Airlieda6b51d2014-12-24 13:11:17 +1000695 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000696}
697
Daniel Vetter8c599672011-12-14 13:57:31 +0100698static inline int
Daniel Vetter8461d222011-12-14 13:57:32 +0100699__copy_to_user_swizzled(char __user *cpu_vaddr,
700 const char *gpu_vaddr, int gpu_offset,
701 int length)
702{
703 int ret, cpu_offset = 0;
704
705 while (length > 0) {
706 int cacheline_end = ALIGN(gpu_offset + 1, 64);
707 int this_length = min(cacheline_end - gpu_offset, length);
708 int swizzled_gpu_offset = gpu_offset ^ 64;
709
710 ret = __copy_to_user(cpu_vaddr + cpu_offset,
711 gpu_vaddr + swizzled_gpu_offset,
712 this_length);
713 if (ret)
714 return ret + length;
715
716 cpu_offset += this_length;
717 gpu_offset += this_length;
718 length -= this_length;
719 }
720
721 return 0;
722}
723
724static inline int
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700725__copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
726 const char __user *cpu_vaddr,
Daniel Vetter8c599672011-12-14 13:57:31 +0100727 int length)
728{
729 int ret, cpu_offset = 0;
730
731 while (length > 0) {
732 int cacheline_end = ALIGN(gpu_offset + 1, 64);
733 int this_length = min(cacheline_end - gpu_offset, length);
734 int swizzled_gpu_offset = gpu_offset ^ 64;
735
736 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
737 cpu_vaddr + cpu_offset,
738 this_length);
739 if (ret)
740 return ret + length;
741
742 cpu_offset += this_length;
743 gpu_offset += this_length;
744 length -= this_length;
745 }
746
747 return 0;
748}
749
Brad Volkin4c914c02014-02-18 10:15:45 -0800750/*
751 * Pins the specified object's pages and synchronizes the object with
752 * GPU accesses. Sets needs_clflush to non-zero if the caller should
753 * flush the object from the CPU cache.
754 */
755int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
Chris Wilson43394c72016-08-18 17:16:47 +0100756 unsigned int *needs_clflush)
Brad Volkin4c914c02014-02-18 10:15:45 -0800757{
758 int ret;
759
Chris Wilsone95433c2016-10-28 13:58:27 +0100760 lockdep_assert_held(&obj->base.dev->struct_mutex);
Brad Volkin4c914c02014-02-18 10:15:45 -0800761
Chris Wilsone95433c2016-10-28 13:58:27 +0100762 *needs_clflush = 0;
Chris Wilson43394c72016-08-18 17:16:47 +0100763 if (!i915_gem_object_has_struct_page(obj))
764 return -ENODEV;
Brad Volkin4c914c02014-02-18 10:15:45 -0800765
Chris Wilsone95433c2016-10-28 13:58:27 +0100766 ret = i915_gem_object_wait(obj,
767 I915_WAIT_INTERRUPTIBLE |
768 I915_WAIT_LOCKED,
769 MAX_SCHEDULE_TIMEOUT,
770 NULL);
Chris Wilsonc13d87e2016-07-20 09:21:15 +0100771 if (ret)
772 return ret;
773
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100774 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100775 if (ret)
776 return ret;
777
Chris Wilsona314d5c2016-08-18 17:16:48 +0100778 i915_gem_object_flush_gtt_write_domain(obj);
779
Chris Wilson43394c72016-08-18 17:16:47 +0100780 /* If we're not in the cpu read domain, set ourself into the gtt
781 * read domain and manually flush cachelines (if required). This
782 * optimizes for the case when the gpu will dirty the data
783 * anyway again before the next pread happens.
784 */
785 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
Brad Volkin4c914c02014-02-18 10:15:45 -0800786 *needs_clflush = !cpu_cache_is_coherent(obj->base.dev,
787 obj->cache_level);
Brad Volkin4c914c02014-02-18 10:15:45 -0800788
Chris Wilson43394c72016-08-18 17:16:47 +0100789 if (*needs_clflush && !static_cpu_has(X86_FEATURE_CLFLUSH)) {
790 ret = i915_gem_object_set_to_cpu_domain(obj, false);
Chris Wilson97649512016-08-18 17:16:50 +0100791 if (ret)
792 goto err_unpin;
793
Chris Wilson43394c72016-08-18 17:16:47 +0100794 *needs_clflush = 0;
795 }
796
Chris Wilson97649512016-08-18 17:16:50 +0100797 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100798 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100799
800err_unpin:
801 i915_gem_object_unpin_pages(obj);
802 return ret;
Chris Wilson43394c72016-08-18 17:16:47 +0100803}
804
805int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
806 unsigned int *needs_clflush)
807{
808 int ret;
809
Chris Wilsone95433c2016-10-28 13:58:27 +0100810 lockdep_assert_held(&obj->base.dev->struct_mutex);
811
Chris Wilson43394c72016-08-18 17:16:47 +0100812 *needs_clflush = 0;
813 if (!i915_gem_object_has_struct_page(obj))
814 return -ENODEV;
815
Chris Wilsone95433c2016-10-28 13:58:27 +0100816 ret = i915_gem_object_wait(obj,
817 I915_WAIT_INTERRUPTIBLE |
818 I915_WAIT_LOCKED |
819 I915_WAIT_ALL,
820 MAX_SCHEDULE_TIMEOUT,
821 NULL);
Chris Wilson43394c72016-08-18 17:16:47 +0100822 if (ret)
823 return ret;
824
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100825 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100826 if (ret)
827 return ret;
828
Chris Wilsona314d5c2016-08-18 17:16:48 +0100829 i915_gem_object_flush_gtt_write_domain(obj);
830
Chris Wilson43394c72016-08-18 17:16:47 +0100831 /* If we're not in the cpu write domain, set ourself into the
832 * gtt write domain and manually flush cachelines (as required).
833 * This optimizes for the case when the gpu will use the data
834 * right away and we therefore have to clflush anyway.
835 */
836 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
837 *needs_clflush |= cpu_write_needs_clflush(obj) << 1;
838
839 /* Same trick applies to invalidate partially written cachelines read
840 * before writing.
841 */
842 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
843 *needs_clflush |= !cpu_cache_is_coherent(obj->base.dev,
844 obj->cache_level);
845
Chris Wilson43394c72016-08-18 17:16:47 +0100846 if (*needs_clflush && !static_cpu_has(X86_FEATURE_CLFLUSH)) {
847 ret = i915_gem_object_set_to_cpu_domain(obj, true);
Chris Wilson97649512016-08-18 17:16:50 +0100848 if (ret)
849 goto err_unpin;
850
Chris Wilson43394c72016-08-18 17:16:47 +0100851 *needs_clflush = 0;
852 }
853
854 if ((*needs_clflush & CLFLUSH_AFTER) == 0)
855 obj->cache_dirty = true;
856
857 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100858 obj->mm.dirty = true;
Chris Wilson97649512016-08-18 17:16:50 +0100859 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100860 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100861
862err_unpin:
863 i915_gem_object_unpin_pages(obj);
864 return ret;
Brad Volkin4c914c02014-02-18 10:15:45 -0800865}
866
Daniel Vetter23c18c72012-03-25 19:47:42 +0200867static void
868shmem_clflush_swizzled_range(char *addr, unsigned long length,
869 bool swizzled)
870{
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200871 if (unlikely(swizzled)) {
Daniel Vetter23c18c72012-03-25 19:47:42 +0200872 unsigned long start = (unsigned long) addr;
873 unsigned long end = (unsigned long) addr + length;
874
875 /* For swizzling simply ensure that we always flush both
876 * channels. Lame, but simple and it works. Swizzled
877 * pwrite/pread is far from a hotpath - current userspace
878 * doesn't use it at all. */
879 start = round_down(start, 128);
880 end = round_up(end, 128);
881
882 drm_clflush_virt_range((void *)start, end - start);
883 } else {
884 drm_clflush_virt_range(addr, length);
885 }
886
887}
888
Daniel Vetterd174bd62012-03-25 19:47:40 +0200889/* Only difference to the fast-path function is that this can handle bit17
890 * and uses non-atomic copy and kmap functions. */
891static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100892shmem_pread_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +0200893 char __user *user_data,
894 bool page_do_bit17_swizzling, bool needs_clflush)
895{
896 char *vaddr;
897 int ret;
898
899 vaddr = kmap(page);
900 if (needs_clflush)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100901 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +0200902 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200903
904 if (page_do_bit17_swizzling)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100905 ret = __copy_to_user_swizzled(user_data, vaddr, offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200906 else
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100907 ret = __copy_to_user(user_data, vaddr + offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200908 kunmap(page);
909
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100910 return ret ? - EFAULT : 0;
Daniel Vetterd174bd62012-03-25 19:47:40 +0200911}
912
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100913static int
914shmem_pread(struct page *page, int offset, int length, char __user *user_data,
915 bool page_do_bit17_swizzling, bool needs_clflush)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530916{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100917 int ret;
918
919 ret = -ENODEV;
920 if (!page_do_bit17_swizzling) {
921 char *vaddr = kmap_atomic(page);
922
923 if (needs_clflush)
924 drm_clflush_virt_range(vaddr + offset, length);
925 ret = __copy_to_user_inatomic(user_data, vaddr + offset, length);
926 kunmap_atomic(vaddr);
927 }
928 if (ret == 0)
929 return 0;
930
931 return shmem_pread_slow(page, offset, length, user_data,
932 page_do_bit17_swizzling, needs_clflush);
933}
934
935static int
936i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
937 struct drm_i915_gem_pread *args)
938{
939 char __user *user_data;
940 u64 remain;
941 unsigned int obj_do_bit17_swizzling;
942 unsigned int needs_clflush;
943 unsigned int idx, offset;
944 int ret;
945
946 obj_do_bit17_swizzling = 0;
947 if (i915_gem_object_needs_bit17_swizzle(obj))
948 obj_do_bit17_swizzling = BIT(17);
949
950 ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex);
951 if (ret)
952 return ret;
953
954 ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
955 mutex_unlock(&obj->base.dev->struct_mutex);
956 if (ret)
957 return ret;
958
959 remain = args->size;
960 user_data = u64_to_user_ptr(args->data_ptr);
961 offset = offset_in_page(args->offset);
962 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
963 struct page *page = i915_gem_object_get_page(obj, idx);
964 int length;
965
966 length = remain;
967 if (offset + length > PAGE_SIZE)
968 length = PAGE_SIZE - offset;
969
970 ret = shmem_pread(page, offset, length, user_data,
971 page_to_phys(page) & obj_do_bit17_swizzling,
972 needs_clflush);
973 if (ret)
974 break;
975
976 remain -= length;
977 user_data += length;
978 offset = 0;
979 }
980
981 i915_gem_obj_finish_shmem_access(obj);
982 return ret;
983}
984
985static inline bool
986gtt_user_read(struct io_mapping *mapping,
987 loff_t base, int offset,
988 char __user *user_data, int length)
989{
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530990 void *vaddr;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100991 unsigned long unwritten;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530992
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530993 /* We can use the cpu mem copy function because this is X86. */
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100994 vaddr = (void __force *)io_mapping_map_atomic_wc(mapping, base);
995 unwritten = __copy_to_user_inatomic(user_data, vaddr + offset, length);
996 io_mapping_unmap_atomic(vaddr);
997 if (unwritten) {
998 vaddr = (void __force *)
999 io_mapping_map_wc(mapping, base, PAGE_SIZE);
1000 unwritten = copy_to_user(user_data, vaddr + offset, length);
1001 io_mapping_unmap(vaddr);
1002 }
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301003 return unwritten;
1004}
1005
1006static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001007i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
1008 const struct drm_i915_gem_pread *args)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301009{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001010 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1011 struct i915_ggtt *ggtt = &i915->ggtt;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301012 struct drm_mm_node node;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001013 struct i915_vma *vma;
1014 void __user *user_data;
1015 u64 remain, offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301016 int ret;
1017
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001018 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1019 if (ret)
1020 return ret;
1021
1022 intel_runtime_pm_get(i915);
1023 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
1024 PIN_MAPPABLE | PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001025 if (!IS_ERR(vma)) {
1026 node.start = i915_ggtt_offset(vma);
1027 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001028 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001029 if (ret) {
1030 i915_vma_unpin(vma);
1031 vma = ERR_PTR(ret);
1032 }
1033 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001034 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001035 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301036 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001037 goto out_unlock;
1038 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301039 }
1040
1041 ret = i915_gem_object_set_to_gtt_domain(obj, false);
1042 if (ret)
1043 goto out_unpin;
1044
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001045 mutex_unlock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301046
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001047 user_data = u64_to_user_ptr(args->data_ptr);
1048 remain = args->size;
1049 offset = args->offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301050
1051 while (remain > 0) {
1052 /* Operation in this page
1053 *
1054 * page_base = page offset within aperture
1055 * page_offset = offset within page
1056 * page_length = bytes to copy for this page
1057 */
1058 u32 page_base = node.start;
1059 unsigned page_offset = offset_in_page(offset);
1060 unsigned page_length = PAGE_SIZE - page_offset;
1061 page_length = remain < page_length ? remain : page_length;
1062 if (node.allocated) {
1063 wmb();
1064 ggtt->base.insert_page(&ggtt->base,
1065 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001066 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301067 wmb();
1068 } else {
1069 page_base += offset & PAGE_MASK;
1070 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001071
1072 if (gtt_user_read(&ggtt->mappable, page_base, page_offset,
1073 user_data, page_length)) {
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301074 ret = -EFAULT;
1075 break;
1076 }
1077
1078 remain -= page_length;
1079 user_data += page_length;
1080 offset += page_length;
1081 }
1082
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001083 mutex_lock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301084out_unpin:
1085 if (node.allocated) {
1086 wmb();
1087 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001088 node.start, node.size);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301089 remove_mappable_node(&node);
1090 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001091 i915_vma_unpin(vma);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301092 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001093out_unlock:
1094 intel_runtime_pm_put(i915);
1095 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +01001096
Eric Anholteb014592009-03-10 11:44:52 -07001097 return ret;
1098}
1099
Eric Anholt673a3942008-07-30 12:06:12 -07001100/**
1101 * Reads data from the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001102 * @dev: drm device pointer
1103 * @data: ioctl data blob
1104 * @file: drm file pointer
Eric Anholt673a3942008-07-30 12:06:12 -07001105 *
1106 * On error, the contents of *data are undefined.
1107 */
1108int
1109i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001110 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001111{
1112 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001113 struct drm_i915_gem_object *obj;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001114 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001115
Chris Wilson51311d02010-11-17 09:10:42 +00001116 if (args->size == 0)
1117 return 0;
1118
1119 if (!access_ok(VERIFY_WRITE,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001120 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001121 args->size))
1122 return -EFAULT;
1123
Chris Wilson03ac0642016-07-20 13:31:51 +01001124 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001125 if (!obj)
1126 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001127
Chris Wilson7dcd2492010-09-26 20:21:44 +01001128 /* Bounds check source. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001129 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001130 ret = -EINVAL;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001131 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001132 }
1133
Chris Wilsondb53a302011-02-03 11:57:46 +00001134 trace_i915_gem_object_pread(obj, args->offset, args->size);
1135
Chris Wilsone95433c2016-10-28 13:58:27 +01001136 ret = i915_gem_object_wait(obj,
1137 I915_WAIT_INTERRUPTIBLE,
1138 MAX_SCHEDULE_TIMEOUT,
1139 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001140 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001141 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001142
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001143 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001144 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001145 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001146
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001147 ret = i915_gem_shmem_pread(obj, args);
Chris Wilson9c870d02016-10-24 13:42:15 +01001148 if (ret == -EFAULT || ret == -ENODEV)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001149 ret = i915_gem_gtt_pread(obj, args);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301150
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001151 i915_gem_object_unpin_pages(obj);
1152out:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001153 i915_gem_object_put(obj);
Eric Anholteb014592009-03-10 11:44:52 -07001154 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001155}
1156
Keith Packard0839ccb2008-10-30 19:38:48 -07001157/* This is the fast write path which cannot handle
1158 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001159 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001160
Chris Wilsonfe115622016-10-28 13:58:40 +01001161static inline bool
1162ggtt_write(struct io_mapping *mapping,
1163 loff_t base, int offset,
1164 char __user *user_data, int length)
Keith Packard0839ccb2008-10-30 19:38:48 -07001165{
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -07001166 void *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -07001167 unsigned long unwritten;
1168
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -07001169 /* We can use the cpu mem copy function because this is X86. */
Chris Wilsonfe115622016-10-28 13:58:40 +01001170 vaddr = (void __force *)io_mapping_map_atomic_wc(mapping, base);
1171 unwritten = __copy_from_user_inatomic_nocache(vaddr + offset,
Keith Packard0839ccb2008-10-30 19:38:48 -07001172 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001173 io_mapping_unmap_atomic(vaddr);
1174 if (unwritten) {
1175 vaddr = (void __force *)
1176 io_mapping_map_wc(mapping, base, PAGE_SIZE);
1177 unwritten = copy_from_user(vaddr + offset, user_data, length);
1178 io_mapping_unmap(vaddr);
1179 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001180
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001181 return unwritten;
1182}
1183
Eric Anholt3de09aa2009-03-09 09:42:23 -07001184/**
1185 * This is the fast pwrite path, where we copy the data directly from the
1186 * user into the GTT, uncached.
Chris Wilsonfe115622016-10-28 13:58:40 +01001187 * @obj: i915 GEM object
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001188 * @args: pwrite arguments structure
Eric Anholt3de09aa2009-03-09 09:42:23 -07001189 */
Eric Anholt673a3942008-07-30 12:06:12 -07001190static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001191i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
1192 const struct drm_i915_gem_pwrite *args)
Eric Anholt673a3942008-07-30 12:06:12 -07001193{
Chris Wilsonfe115622016-10-28 13:58:40 +01001194 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301195 struct i915_ggtt *ggtt = &i915->ggtt;
1196 struct drm_mm_node node;
Chris Wilsonfe115622016-10-28 13:58:40 +01001197 struct i915_vma *vma;
1198 u64 remain, offset;
1199 void __user *user_data;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301200 int ret;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301201
Chris Wilsonfe115622016-10-28 13:58:40 +01001202 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1203 if (ret)
1204 return ret;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001205
Chris Wilson9c870d02016-10-24 13:42:15 +01001206 intel_runtime_pm_get(i915);
Chris Wilson058d88c2016-08-15 10:49:06 +01001207 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsonde895082016-08-04 16:32:34 +01001208 PIN_MAPPABLE | PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001209 if (!IS_ERR(vma)) {
1210 node.start = i915_ggtt_offset(vma);
1211 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001212 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001213 if (ret) {
1214 i915_vma_unpin(vma);
1215 vma = ERR_PTR(ret);
1216 }
1217 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001218 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001219 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301220 if (ret)
Chris Wilsonfe115622016-10-28 13:58:40 +01001221 goto out_unlock;
1222 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301223 }
Daniel Vetter935aaa62012-03-25 19:47:35 +02001224
1225 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1226 if (ret)
1227 goto out_unpin;
1228
Chris Wilsonfe115622016-10-28 13:58:40 +01001229 mutex_unlock(&i915->drm.struct_mutex);
1230
Chris Wilsonb19482d2016-08-18 17:16:43 +01001231 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Paulo Zanoni063e4e62015-02-13 17:23:45 -02001232
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301233 user_data = u64_to_user_ptr(args->data_ptr);
1234 offset = args->offset;
1235 remain = args->size;
1236 while (remain) {
Eric Anholt673a3942008-07-30 12:06:12 -07001237 /* Operation in this page
1238 *
Keith Packard0839ccb2008-10-30 19:38:48 -07001239 * page_base = page offset within aperture
1240 * page_offset = offset within page
1241 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -07001242 */
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301243 u32 page_base = node.start;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001244 unsigned int page_offset = offset_in_page(offset);
1245 unsigned int page_length = PAGE_SIZE - page_offset;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301246 page_length = remain < page_length ? remain : page_length;
1247 if (node.allocated) {
1248 wmb(); /* flush the write before we modify the GGTT */
1249 ggtt->base.insert_page(&ggtt->base,
1250 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
1251 node.start, I915_CACHE_NONE, 0);
1252 wmb(); /* flush modifications to the GGTT (insert_page) */
1253 } else {
1254 page_base += offset & PAGE_MASK;
1255 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001256 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -07001257 * source page isn't available. Return the error and we'll
1258 * retry in the slow path.
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301259 * If the object is non-shmem backed, we retry again with the
1260 * path that handles page fault.
Keith Packard0839ccb2008-10-30 19:38:48 -07001261 */
Chris Wilsonfe115622016-10-28 13:58:40 +01001262 if (ggtt_write(&ggtt->mappable, page_base, page_offset,
1263 user_data, page_length)) {
1264 ret = -EFAULT;
1265 break;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001266 }
Eric Anholt673a3942008-07-30 12:06:12 -07001267
Keith Packard0839ccb2008-10-30 19:38:48 -07001268 remain -= page_length;
1269 user_data += page_length;
1270 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -07001271 }
Chris Wilsonb19482d2016-08-18 17:16:43 +01001272 intel_fb_obj_flush(obj, false, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001273
1274 mutex_lock(&i915->drm.struct_mutex);
Daniel Vetter935aaa62012-03-25 19:47:35 +02001275out_unpin:
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301276 if (node.allocated) {
1277 wmb();
1278 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001279 node.start, node.size);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301280 remove_mappable_node(&node);
1281 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001282 i915_vma_unpin(vma);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301283 }
Chris Wilsonfe115622016-10-28 13:58:40 +01001284out_unlock:
Chris Wilson9c870d02016-10-24 13:42:15 +01001285 intel_runtime_pm_put(i915);
Chris Wilsonfe115622016-10-28 13:58:40 +01001286 mutex_unlock(&i915->drm.struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -07001287 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001288}
1289
Eric Anholt673a3942008-07-30 12:06:12 -07001290static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001291shmem_pwrite_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +02001292 char __user *user_data,
1293 bool page_do_bit17_swizzling,
1294 bool needs_clflush_before,
1295 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -07001296{
Daniel Vetterd174bd62012-03-25 19:47:40 +02001297 char *vaddr;
1298 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001299
Daniel Vetterd174bd62012-03-25 19:47:40 +02001300 vaddr = kmap(page);
Daniel Vettere7e58eb2012-03-25 19:47:43 +02001301 if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
Chris Wilsonfe115622016-10-28 13:58:40 +01001302 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001303 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001304 if (page_do_bit17_swizzling)
Chris Wilsonfe115622016-10-28 13:58:40 +01001305 ret = __copy_from_user_swizzled(vaddr, offset, user_data,
1306 length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001307 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001308 ret = __copy_from_user(vaddr + offset, user_data, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001309 if (needs_clflush_after)
Chris Wilsonfe115622016-10-28 13:58:40 +01001310 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001311 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001312 kunmap(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001313
Chris Wilson755d2212012-09-04 21:02:55 +01001314 return ret ? -EFAULT : 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001315}
1316
Chris Wilsonfe115622016-10-28 13:58:40 +01001317/* Per-page copy function for the shmem pwrite fastpath.
1318 * Flushes invalid cachelines before writing to the target if
1319 * needs_clflush_before is set and flushes out any written cachelines after
1320 * writing if needs_clflush is set.
1321 */
Eric Anholt40123c12009-03-09 13:42:30 -07001322static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001323shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
1324 bool page_do_bit17_swizzling,
1325 bool needs_clflush_before,
1326 bool needs_clflush_after)
Eric Anholt40123c12009-03-09 13:42:30 -07001327{
Chris Wilsonfe115622016-10-28 13:58:40 +01001328 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001329
Chris Wilsonfe115622016-10-28 13:58:40 +01001330 ret = -ENODEV;
1331 if (!page_do_bit17_swizzling) {
1332 char *vaddr = kmap_atomic(page);
1333
1334 if (needs_clflush_before)
1335 drm_clflush_virt_range(vaddr + offset, len);
1336 ret = __copy_from_user_inatomic(vaddr + offset, user_data, len);
1337 if (needs_clflush_after)
1338 drm_clflush_virt_range(vaddr + offset, len);
1339
1340 kunmap_atomic(vaddr);
1341 }
1342 if (ret == 0)
1343 return ret;
1344
1345 return shmem_pwrite_slow(page, offset, len, user_data,
1346 page_do_bit17_swizzling,
1347 needs_clflush_before,
1348 needs_clflush_after);
1349}
1350
1351static int
1352i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
1353 const struct drm_i915_gem_pwrite *args)
1354{
1355 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1356 void __user *user_data;
1357 u64 remain;
1358 unsigned int obj_do_bit17_swizzling;
1359 unsigned int partial_cacheline_write;
1360 unsigned int needs_clflush;
1361 unsigned int offset, idx;
1362 int ret;
1363
1364 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
Chris Wilson43394c72016-08-18 17:16:47 +01001365 if (ret)
1366 return ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001367
Chris Wilsonfe115622016-10-28 13:58:40 +01001368 ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
1369 mutex_unlock(&i915->drm.struct_mutex);
1370 if (ret)
1371 return ret;
1372
1373 obj_do_bit17_swizzling = 0;
1374 if (i915_gem_object_needs_bit17_swizzle(obj))
1375 obj_do_bit17_swizzling = BIT(17);
1376
1377 /* If we don't overwrite a cacheline completely we need to be
1378 * careful to have up-to-date data by first clflushing. Don't
1379 * overcomplicate things and flush the entire patch.
1380 */
1381 partial_cacheline_write = 0;
1382 if (needs_clflush & CLFLUSH_BEFORE)
1383 partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
1384
Chris Wilson43394c72016-08-18 17:16:47 +01001385 user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson43394c72016-08-18 17:16:47 +01001386 remain = args->size;
Chris Wilsonfe115622016-10-28 13:58:40 +01001387 offset = offset_in_page(args->offset);
1388 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1389 struct page *page = i915_gem_object_get_page(obj, idx);
1390 int length;
Eric Anholt40123c12009-03-09 13:42:30 -07001391
Chris Wilsonfe115622016-10-28 13:58:40 +01001392 length = remain;
1393 if (offset + length > PAGE_SIZE)
1394 length = PAGE_SIZE - offset;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001395
Chris Wilsonfe115622016-10-28 13:58:40 +01001396 ret = shmem_pwrite(page, offset, length, user_data,
1397 page_to_phys(page) & obj_do_bit17_swizzling,
1398 (offset | length) & partial_cacheline_write,
1399 needs_clflush & CLFLUSH_AFTER);
1400 if (ret)
Chris Wilson9da3da62012-06-01 15:20:22 +01001401 break;
1402
Chris Wilsonfe115622016-10-28 13:58:40 +01001403 remain -= length;
1404 user_data += length;
1405 offset = 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001406 }
1407
Rodrigo Vivide152b62015-07-07 16:28:51 -07001408 intel_fb_obj_flush(obj, false, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001409 i915_gem_obj_finish_shmem_access(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001410 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001411}
1412
1413/**
1414 * Writes data to the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001415 * @dev: drm device
1416 * @data: ioctl data blob
1417 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001418 *
1419 * On error, the contents of the buffer that were to be modified are undefined.
1420 */
1421int
1422i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001423 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001424{
1425 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001426 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +00001427 int ret;
1428
1429 if (args->size == 0)
1430 return 0;
1431
1432 if (!access_ok(VERIFY_READ,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001433 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001434 args->size))
1435 return -EFAULT;
1436
Chris Wilson03ac0642016-07-20 13:31:51 +01001437 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001438 if (!obj)
1439 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001440
Chris Wilson7dcd2492010-09-26 20:21:44 +01001441 /* Bounds check destination. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001442 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001443 ret = -EINVAL;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001444 goto err;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001445 }
1446
Chris Wilsondb53a302011-02-03 11:57:46 +00001447 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
1448
Chris Wilsone95433c2016-10-28 13:58:27 +01001449 ret = i915_gem_object_wait(obj,
1450 I915_WAIT_INTERRUPTIBLE |
1451 I915_WAIT_ALL,
1452 MAX_SCHEDULE_TIMEOUT,
1453 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001454 if (ret)
1455 goto err;
1456
Chris Wilsonfe115622016-10-28 13:58:40 +01001457 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001458 if (ret)
Chris Wilsonfe115622016-10-28 13:58:40 +01001459 goto err;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001460
Daniel Vetter935aaa62012-03-25 19:47:35 +02001461 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -07001462 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1463 * it would end up going through the fenced access, and we'll get
1464 * different detiling behavior between reading and writing.
1465 * pread/pwrite currently are reading and writing from the CPU
1466 * perspective, requiring manual detiling by the client.
1467 */
Chris Wilson6eae0052016-06-20 15:05:52 +01001468 if (!i915_gem_object_has_struct_page(obj) ||
Chris Wilson9c870d02016-10-24 13:42:15 +01001469 cpu_write_needs_clflush(obj))
Daniel Vetter935aaa62012-03-25 19:47:35 +02001470 /* Note that the gtt paths might fail with non-page-backed user
1471 * pointers (e.g. gtt mappings when moving data between
Chris Wilson9c870d02016-10-24 13:42:15 +01001472 * textures). Fallback to the shmem path in that case.
1473 */
Chris Wilsonfe115622016-10-28 13:58:40 +01001474 ret = i915_gem_gtt_pwrite_fast(obj, args);
Eric Anholt673a3942008-07-30 12:06:12 -07001475
Chris Wilsond1054ee2016-07-16 18:42:36 +01001476 if (ret == -EFAULT || ret == -ENOSPC) {
Chris Wilson6a2c4232014-11-04 04:51:40 -08001477 if (obj->phys_handle)
1478 ret = i915_gem_phys_pwrite(obj, args, file);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301479 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001480 ret = i915_gem_shmem_pwrite(obj, args);
Chris Wilson6a2c4232014-11-04 04:51:40 -08001481 }
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001482
Chris Wilsonfe115622016-10-28 13:58:40 +01001483 i915_gem_object_unpin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001484err:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001485 i915_gem_object_put(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001486 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001487}
1488
Chris Wilsond243ad82016-08-18 17:16:44 +01001489static inline enum fb_op_origin
Chris Wilsonaeecc962016-06-17 14:46:39 -03001490write_origin(struct drm_i915_gem_object *obj, unsigned domain)
1491{
Chris Wilson50349242016-08-18 17:17:04 +01001492 return (domain == I915_GEM_DOMAIN_GTT ?
1493 obj->frontbuffer_ggtt_origin : ORIGIN_CPU);
Chris Wilsonaeecc962016-06-17 14:46:39 -03001494}
1495
Chris Wilson40e62d52016-10-28 13:58:41 +01001496static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
1497{
1498 struct drm_i915_private *i915;
1499 struct list_head *list;
1500 struct i915_vma *vma;
1501
1502 list_for_each_entry(vma, &obj->vma_list, obj_link) {
1503 if (!i915_vma_is_ggtt(vma))
Chris Wilson28f412e2016-12-23 14:57:55 +00001504 break;
Chris Wilson40e62d52016-10-28 13:58:41 +01001505
1506 if (i915_vma_is_active(vma))
1507 continue;
1508
1509 if (!drm_mm_node_allocated(&vma->node))
1510 continue;
1511
1512 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
1513 }
1514
1515 i915 = to_i915(obj->base.dev);
1516 list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list;
Joonas Lahtinen56cea322016-11-02 12:16:04 +02001517 list_move_tail(&obj->global_link, list);
Chris Wilson40e62d52016-10-28 13:58:41 +01001518}
1519
Eric Anholt673a3942008-07-30 12:06:12 -07001520/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001521 * Called when user space prepares to use an object with the CPU, either
1522 * through the mmap ioctl's mapping or a GTT mapping.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001523 * @dev: drm device
1524 * @data: ioctl data blob
1525 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001526 */
1527int
1528i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001529 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001530{
1531 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001532 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001533 uint32_t read_domains = args->read_domains;
1534 uint32_t write_domain = args->write_domain;
Chris Wilson40e62d52016-10-28 13:58:41 +01001535 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07001536
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001537 /* Only handle setting domains to types used by the CPU. */
Chris Wilsonb8f90962016-08-05 10:14:07 +01001538 if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001539 return -EINVAL;
1540
1541 /* Having something in the write domain implies it's in the read
1542 * domain, and only that read domain. Enforce that in the request.
1543 */
1544 if (write_domain != 0 && read_domains != write_domain)
1545 return -EINVAL;
1546
Chris Wilson03ac0642016-07-20 13:31:51 +01001547 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001548 if (!obj)
1549 return -ENOENT;
Jesse Barnes652c3932009-08-17 13:31:43 -07001550
Chris Wilson3236f572012-08-24 09:35:09 +01001551 /* Try to flush the object off the GPU without holding the lock.
1552 * We will repeat the flush holding the lock in the normal manner
1553 * to catch cases where we are gazumped.
1554 */
Chris Wilson40e62d52016-10-28 13:58:41 +01001555 err = i915_gem_object_wait(obj,
Chris Wilsone95433c2016-10-28 13:58:27 +01001556 I915_WAIT_INTERRUPTIBLE |
1557 (write_domain ? I915_WAIT_ALL : 0),
1558 MAX_SCHEDULE_TIMEOUT,
1559 to_rps_client(file));
Chris Wilson40e62d52016-10-28 13:58:41 +01001560 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001561 goto out;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001562
Chris Wilson40e62d52016-10-28 13:58:41 +01001563 /* Flush and acquire obj->pages so that we are coherent through
1564 * direct access in memory with previous cached writes through
1565 * shmemfs and that our cache domain tracking remains valid.
1566 * For example, if the obj->filp was moved to swap without us
1567 * being notified and releasing the pages, we would mistakenly
1568 * continue to assume that the obj remained out of the CPU cached
1569 * domain.
1570 */
1571 err = i915_gem_object_pin_pages(obj);
1572 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001573 goto out;
Chris Wilson40e62d52016-10-28 13:58:41 +01001574
1575 err = i915_mutex_lock_interruptible(dev);
1576 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001577 goto out_unpin;
Chris Wilson3236f572012-08-24 09:35:09 +01001578
Chris Wilson43566de2015-01-02 16:29:29 +05301579 if (read_domains & I915_GEM_DOMAIN_GTT)
Chris Wilson40e62d52016-10-28 13:58:41 +01001580 err = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Chris Wilson43566de2015-01-02 16:29:29 +05301581 else
Chris Wilson40e62d52016-10-28 13:58:41 +01001582 err = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
1583
1584 /* And bump the LRU for this access */
1585 i915_gem_object_bump_inactive_ggtt(obj);
1586
1587 mutex_unlock(&dev->struct_mutex);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001588
Daniel Vetter031b6982015-06-26 19:35:16 +02001589 if (write_domain != 0)
Chris Wilsonaeecc962016-06-17 14:46:39 -03001590 intel_fb_obj_invalidate(obj, write_origin(obj, write_domain));
Daniel Vetter031b6982015-06-26 19:35:16 +02001591
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001592out_unpin:
Chris Wilson40e62d52016-10-28 13:58:41 +01001593 i915_gem_object_unpin_pages(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001594out:
1595 i915_gem_object_put(obj);
Chris Wilson40e62d52016-10-28 13:58:41 +01001596 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07001597}
1598
1599/**
1600 * Called when user space has done writes to this buffer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001601 * @dev: drm device
1602 * @data: ioctl data blob
1603 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001604 */
1605int
1606i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001607 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001608{
1609 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001610 struct drm_i915_gem_object *obj;
Chris Wilsonc21724c2016-08-05 10:14:19 +01001611 int err = 0;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001612
Chris Wilson03ac0642016-07-20 13:31:51 +01001613 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonc21724c2016-08-05 10:14:19 +01001614 if (!obj)
1615 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001616
Eric Anholt673a3942008-07-30 12:06:12 -07001617 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilsonc21724c2016-08-05 10:14:19 +01001618 if (READ_ONCE(obj->pin_display)) {
1619 err = i915_mutex_lock_interruptible(dev);
1620 if (!err) {
1621 i915_gem_object_flush_cpu_write_domain(obj);
1622 mutex_unlock(&dev->struct_mutex);
1623 }
1624 }
Eric Anholte47c68e2008-11-14 13:35:19 -08001625
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001626 i915_gem_object_put(obj);
Chris Wilsonc21724c2016-08-05 10:14:19 +01001627 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07001628}
1629
1630/**
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001631 * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
1632 * it is mapped to.
1633 * @dev: drm device
1634 * @data: ioctl data blob
1635 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001636 *
1637 * While the mapping holds a reference on the contents of the object, it doesn't
1638 * imply a ref on the object itself.
Daniel Vetter34367382014-10-16 12:28:18 +02001639 *
1640 * IMPORTANT:
1641 *
1642 * DRM driver writers who look a this function as an example for how to do GEM
1643 * mmap support, please don't implement mmap support like here. The modern way
1644 * to implement DRM mmap support is with an mmap offset ioctl (like
1645 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
1646 * That way debug tooling like valgrind will understand what's going on, hiding
1647 * the mmap call in a driver private ioctl will break that. The i915 driver only
1648 * does cpu mmaps this way because we didn't know better.
Eric Anholt673a3942008-07-30 12:06:12 -07001649 */
1650int
1651i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001652 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001653{
1654 struct drm_i915_gem_mmap *args = data;
Chris Wilson03ac0642016-07-20 13:31:51 +01001655 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001656 unsigned long addr;
1657
Akash Goel1816f922015-01-02 16:29:30 +05301658 if (args->flags & ~(I915_MMAP_WC))
1659 return -EINVAL;
1660
Borislav Petkov568a58e2016-03-29 17:42:01 +02001661 if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
Akash Goel1816f922015-01-02 16:29:30 +05301662 return -ENODEV;
1663
Chris Wilson03ac0642016-07-20 13:31:51 +01001664 obj = i915_gem_object_lookup(file, args->handle);
1665 if (!obj)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001666 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001667
Daniel Vetter1286ff72012-05-10 15:25:09 +02001668 /* prime objects have no backing filp to GEM mmap
1669 * pages from.
1670 */
Chris Wilson03ac0642016-07-20 13:31:51 +01001671 if (!obj->base.filp) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001672 i915_gem_object_put(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02001673 return -EINVAL;
1674 }
1675
Chris Wilson03ac0642016-07-20 13:31:51 +01001676 addr = vm_mmap(obj->base.filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001677 PROT_READ | PROT_WRITE, MAP_SHARED,
1678 args->offset);
Akash Goel1816f922015-01-02 16:29:30 +05301679 if (args->flags & I915_MMAP_WC) {
1680 struct mm_struct *mm = current->mm;
1681 struct vm_area_struct *vma;
1682
Michal Hocko80a89a52016-05-23 16:26:11 -07001683 if (down_write_killable(&mm->mmap_sem)) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001684 i915_gem_object_put(obj);
Michal Hocko80a89a52016-05-23 16:26:11 -07001685 return -EINTR;
1686 }
Akash Goel1816f922015-01-02 16:29:30 +05301687 vma = find_vma(mm, addr);
1688 if (vma)
1689 vma->vm_page_prot =
1690 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
1691 else
1692 addr = -ENOMEM;
1693 up_write(&mm->mmap_sem);
Chris Wilsonaeecc962016-06-17 14:46:39 -03001694
1695 /* This may race, but that's ok, it only gets set */
Chris Wilson50349242016-08-18 17:17:04 +01001696 WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
Akash Goel1816f922015-01-02 16:29:30 +05301697 }
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001698 i915_gem_object_put(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001699 if (IS_ERR((void *)addr))
1700 return addr;
1701
1702 args->addr_ptr = (uint64_t) addr;
1703
1704 return 0;
1705}
1706
Chris Wilson03af84f2016-08-18 17:17:01 +01001707static unsigned int tile_row_pages(struct drm_i915_gem_object *obj)
1708{
Chris Wilson6649a0b2017-01-09 16:16:08 +00001709 return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT;
Chris Wilson03af84f2016-08-18 17:17:01 +01001710}
1711
Jesse Barnesde151cf2008-11-12 10:03:55 -08001712/**
Chris Wilson4cc69072016-08-25 19:05:19 +01001713 * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
1714 *
1715 * A history of the GTT mmap interface:
1716 *
1717 * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
1718 * aligned and suitable for fencing, and still fit into the available
1719 * mappable space left by the pinned display objects. A classic problem
1720 * we called the page-fault-of-doom where we would ping-pong between
1721 * two objects that could not fit inside the GTT and so the memcpy
1722 * would page one object in at the expense of the other between every
1723 * single byte.
1724 *
1725 * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
1726 * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
1727 * object is too large for the available space (or simply too large
1728 * for the mappable aperture!), a view is created instead and faulted
1729 * into userspace. (This view is aligned and sized appropriately for
1730 * fenced access.)
1731 *
1732 * Restrictions:
1733 *
1734 * * snoopable objects cannot be accessed via the GTT. It can cause machine
1735 * hangs on some architectures, corruption on others. An attempt to service
1736 * a GTT page fault from a snoopable object will generate a SIGBUS.
1737 *
1738 * * the object must be able to fit into RAM (physical memory, though no
1739 * limited to the mappable aperture).
1740 *
1741 *
1742 * Caveats:
1743 *
1744 * * a new GTT page fault will synchronize rendering from the GPU and flush
1745 * all data to system memory. Subsequent access will not be synchronized.
1746 *
1747 * * all mappings are revoked on runtime device suspend.
1748 *
1749 * * there are only 8, 16 or 32 fence registers to share between all users
1750 * (older machines require fence register for display and blitter access
1751 * as well). Contention of the fence registers will cause the previous users
1752 * to be unmapped and any new access will generate new page faults.
1753 *
1754 * * running out of memory while servicing a fault may generate a SIGBUS,
1755 * rather than the expected SIGSEGV.
1756 */
1757int i915_gem_mmap_gtt_version(void)
1758{
1759 return 1;
1760}
1761
Chris Wilson2d4281b2017-01-10 09:56:32 +00001762static inline struct i915_ggtt_view
1763compute_partial_view(struct drm_i915_gem_object *obj,
Chris Wilson2d4281b2017-01-10 09:56:32 +00001764 pgoff_t page_offset,
1765 unsigned int chunk)
1766{
1767 struct i915_ggtt_view view;
1768
1769 if (i915_gem_object_is_tiled(obj))
1770 chunk = roundup(chunk, tile_row_pages(obj));
1771
Chris Wilson2d4281b2017-01-10 09:56:32 +00001772 view.type = I915_GGTT_VIEW_PARTIAL;
Chris Wilson8bab11932017-01-14 00:28:25 +00001773 view.partial.offset = rounddown(page_offset, chunk);
1774 view.partial.size =
Chris Wilson2d4281b2017-01-10 09:56:32 +00001775 min_t(unsigned int, chunk,
Chris Wilson8bab11932017-01-14 00:28:25 +00001776 (obj->base.size >> PAGE_SHIFT) - view.partial.offset);
Chris Wilson2d4281b2017-01-10 09:56:32 +00001777
1778 /* If the partial covers the entire object, just create a normal VMA. */
1779 if (chunk >= obj->base.size >> PAGE_SHIFT)
1780 view.type = I915_GGTT_VIEW_NORMAL;
1781
1782 return view;
1783}
1784
Chris Wilson4cc69072016-08-25 19:05:19 +01001785/**
Jesse Barnesde151cf2008-11-12 10:03:55 -08001786 * i915_gem_fault - fault a page into the GTT
Chris Wilson058d88c2016-08-15 10:49:06 +01001787 * @area: CPU VMA in question
Geliang Tangd9072a32015-09-15 05:58:44 -07001788 * @vmf: fault info
Jesse Barnesde151cf2008-11-12 10:03:55 -08001789 *
1790 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1791 * from userspace. The fault handler takes care of binding the object to
1792 * the GTT (if needed), allocating and programming a fence register (again,
1793 * only if needed based on whether the old reg is still valid or the object
1794 * is tiled) and inserting a new PTE into the faulting process.
1795 *
1796 * Note that the faulting process may involve evicting existing objects
1797 * from the GTT and/or fence registers to make room. So performance may
1798 * suffer if the GTT working set is large or there are few fence registers
1799 * left.
Chris Wilson4cc69072016-08-25 19:05:19 +01001800 *
1801 * The current feature set supported by i915_gem_fault() and thus GTT mmaps
1802 * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
Jesse Barnesde151cf2008-11-12 10:03:55 -08001803 */
Chris Wilson058d88c2016-08-15 10:49:06 +01001804int i915_gem_fault(struct vm_area_struct *area, struct vm_fault *vmf)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001805{
Chris Wilson03af84f2016-08-18 17:17:01 +01001806#define MIN_CHUNK_PAGES ((1 << 20) >> PAGE_SHIFT) /* 1 MiB */
Chris Wilson058d88c2016-08-15 10:49:06 +01001807 struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
Chris Wilson05394f32010-11-08 19:18:58 +00001808 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001809 struct drm_i915_private *dev_priv = to_i915(dev);
1810 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001811 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Chris Wilson058d88c2016-08-15 10:49:06 +01001812 struct i915_vma *vma;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001813 pgoff_t page_offset;
Chris Wilson82118872016-08-18 17:17:05 +01001814 unsigned int flags;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001815 int ret;
Paulo Zanonif65c9162013-11-27 18:20:34 -02001816
Jesse Barnesde151cf2008-11-12 10:03:55 -08001817 /* We don't use vmf->pgoff since that has the fake offset */
Jan Kara1a29d852016-12-14 15:07:01 -08001818 page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001819
Chris Wilsondb53a302011-02-03 11:57:46 +00001820 trace_i915_gem_object_fault(obj, page_offset, true, write);
1821
Chris Wilson6e4930f2014-02-07 18:37:06 -02001822 /* Try to flush the object off the GPU first without holding the lock.
Chris Wilsonb8f90962016-08-05 10:14:07 +01001823 * Upon acquiring the lock, we will perform our sanity checks and then
Chris Wilson6e4930f2014-02-07 18:37:06 -02001824 * repeat the flush holding the lock in the normal manner to catch cases
1825 * where we are gazumped.
1826 */
Chris Wilsone95433c2016-10-28 13:58:27 +01001827 ret = i915_gem_object_wait(obj,
1828 I915_WAIT_INTERRUPTIBLE,
1829 MAX_SCHEDULE_TIMEOUT,
1830 NULL);
Chris Wilson6e4930f2014-02-07 18:37:06 -02001831 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001832 goto err;
1833
Chris Wilson40e62d52016-10-28 13:58:41 +01001834 ret = i915_gem_object_pin_pages(obj);
1835 if (ret)
1836 goto err;
1837
Chris Wilsonb8f90962016-08-05 10:14:07 +01001838 intel_runtime_pm_get(dev_priv);
1839
1840 ret = i915_mutex_lock_interruptible(dev);
1841 if (ret)
1842 goto err_rpm;
Chris Wilson6e4930f2014-02-07 18:37:06 -02001843
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001844 /* Access to snoopable pages through the GTT is incoherent. */
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00001845 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) {
Chris Wilsonddeff6e2014-05-28 16:16:41 +01001846 ret = -EFAULT;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001847 goto err_unlock;
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001848 }
1849
Chris Wilson82118872016-08-18 17:17:05 +01001850 /* If the object is smaller than a couple of partial vma, it is
1851 * not worth only creating a single partial vma - we may as well
1852 * clear enough space for the full object.
1853 */
1854 flags = PIN_MAPPABLE;
1855 if (obj->base.size > 2 * MIN_CHUNK_PAGES << PAGE_SHIFT)
1856 flags |= PIN_NONBLOCK | PIN_NONFAULT;
1857
Chris Wilsona61007a2016-08-18 17:17:02 +01001858 /* Now pin it into the GTT as needed */
Chris Wilson82118872016-08-18 17:17:05 +01001859 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, flags);
Chris Wilsona61007a2016-08-18 17:17:02 +01001860 if (IS_ERR(vma)) {
Chris Wilsona61007a2016-08-18 17:17:02 +01001861 /* Use a partial view if it is bigger than available space */
Chris Wilson2d4281b2017-01-10 09:56:32 +00001862 struct i915_ggtt_view view =
Chris Wilson8201c1f2017-01-10 09:56:33 +00001863 compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES);
Chris Wilsonaa136d92016-08-18 17:17:03 +01001864
Chris Wilson50349242016-08-18 17:17:04 +01001865 /* Userspace is now writing through an untracked VMA, abandon
1866 * all hope that the hardware is able to track future writes.
1867 */
1868 obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
1869
Chris Wilsona61007a2016-08-18 17:17:02 +01001870 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE);
1871 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001872 if (IS_ERR(vma)) {
1873 ret = PTR_ERR(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001874 goto err_unlock;
Chris Wilson058d88c2016-08-15 10:49:06 +01001875 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001876
Chris Wilsonc9839302012-11-20 10:45:17 +00001877 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1878 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001879 goto err_unpin;
Chris Wilsonc9839302012-11-20 10:45:17 +00001880
Chris Wilson49ef5292016-08-18 17:17:00 +01001881 ret = i915_vma_get_fence(vma);
Chris Wilsonc9839302012-11-20 10:45:17 +00001882 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001883 goto err_unpin;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001884
Chris Wilson275f0392016-10-24 13:42:14 +01001885 /* Mark as being mmapped into userspace for later revocation */
Chris Wilson9c870d02016-10-24 13:42:15 +01001886 assert_rpm_wakelock_held(dev_priv);
Chris Wilson275f0392016-10-24 13:42:14 +01001887 if (list_empty(&obj->userfault_link))
1888 list_add(&obj->userfault_link, &dev_priv->mm.userfault_list);
Chris Wilson275f0392016-10-24 13:42:14 +01001889
Chris Wilsonb90b91d2014-06-10 12:14:40 +01001890 /* Finally, remap it using the new GTT offset */
Chris Wilsonc58305a2016-08-19 16:54:28 +01001891 ret = remap_io_mapping(area,
Chris Wilson8bab11932017-01-14 00:28:25 +00001892 area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT),
Chris Wilsonc58305a2016-08-19 16:54:28 +01001893 (ggtt->mappable_base + vma->node.start) >> PAGE_SHIFT,
1894 min_t(u64, vma->size, area->vm_end - area->vm_start),
1895 &ggtt->mappable);
Chris Wilsona61007a2016-08-18 17:17:02 +01001896
Chris Wilsonb8f90962016-08-05 10:14:07 +01001897err_unpin:
Chris Wilson058d88c2016-08-15 10:49:06 +01001898 __i915_vma_unpin(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001899err_unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001900 mutex_unlock(&dev->struct_mutex);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001901err_rpm:
1902 intel_runtime_pm_put(dev_priv);
Chris Wilson40e62d52016-10-28 13:58:41 +01001903 i915_gem_object_unpin_pages(obj);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001904err:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001905 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001906 case -EIO:
Daniel Vetter2232f032014-09-04 09:36:18 +02001907 /*
1908 * We eat errors when the gpu is terminally wedged to avoid
1909 * userspace unduly crashing (gl has no provisions for mmaps to
1910 * fail). But any other -EIO isn't ours (e.g. swap in failure)
1911 * and so needs to be reported.
1912 */
1913 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
Paulo Zanonif65c9162013-11-27 18:20:34 -02001914 ret = VM_FAULT_SIGBUS;
1915 break;
1916 }
Chris Wilson045e7692010-11-07 09:18:22 +00001917 case -EAGAIN:
Daniel Vetter571c6082013-09-12 17:57:28 +02001918 /*
1919 * EAGAIN means the gpu is hung and we'll wait for the error
1920 * handler to reset everything when re-faulting in
1921 * i915_mutex_lock_interruptible.
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001922 */
Chris Wilsonc7150892009-09-23 00:43:56 +01001923 case 0:
1924 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00001925 case -EINTR:
Dmitry Rogozhkine79e0fe2012-10-03 17:15:26 +03001926 case -EBUSY:
1927 /*
1928 * EBUSY is ok: this just means that another thread
1929 * already did the job.
1930 */
Paulo Zanonif65c9162013-11-27 18:20:34 -02001931 ret = VM_FAULT_NOPAGE;
1932 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001933 case -ENOMEM:
Paulo Zanonif65c9162013-11-27 18:20:34 -02001934 ret = VM_FAULT_OOM;
1935 break;
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02001936 case -ENOSPC:
Chris Wilson45d67812014-01-31 11:34:57 +00001937 case -EFAULT:
Paulo Zanonif65c9162013-11-27 18:20:34 -02001938 ret = VM_FAULT_SIGBUS;
1939 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001940 default:
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02001941 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
Paulo Zanonif65c9162013-11-27 18:20:34 -02001942 ret = VM_FAULT_SIGBUS;
1943 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001944 }
Paulo Zanonif65c9162013-11-27 18:20:34 -02001945 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001946}
1947
1948/**
Chris Wilson901782b2009-07-10 08:18:50 +01001949 * i915_gem_release_mmap - remove physical page mappings
1950 * @obj: obj in question
1951 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001952 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001953 * relinquish ownership of the pages back to the system.
1954 *
1955 * It is vital that we remove the page mapping if we have mapped a tiled
1956 * object through the GTT and then lose the fence register due to
1957 * resource pressure. Similarly if the object has been moved out of the
1958 * aperture, than pages mapped into userspace must be revoked. Removing the
1959 * mapping will then trigger a page fault on the next user access, allowing
1960 * fixup by i915_gem_fault().
1961 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001962void
Chris Wilson05394f32010-11-08 19:18:58 +00001963i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01001964{
Chris Wilson275f0392016-10-24 13:42:14 +01001965 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson275f0392016-10-24 13:42:14 +01001966
Chris Wilson349f2cc2016-04-13 17:35:12 +01001967 /* Serialisation between user GTT access and our code depends upon
1968 * revoking the CPU's PTE whilst the mutex is held. The next user
1969 * pagefault then has to wait until we release the mutex.
Chris Wilson9c870d02016-10-24 13:42:15 +01001970 *
1971 * Note that RPM complicates somewhat by adding an additional
1972 * requirement that operations to the GGTT be made holding the RPM
1973 * wakeref.
Chris Wilson349f2cc2016-04-13 17:35:12 +01001974 */
Chris Wilson275f0392016-10-24 13:42:14 +01001975 lockdep_assert_held(&i915->drm.struct_mutex);
Chris Wilson9c870d02016-10-24 13:42:15 +01001976 intel_runtime_pm_get(i915);
Chris Wilson349f2cc2016-04-13 17:35:12 +01001977
Chris Wilson3594a3e2016-10-24 13:42:16 +01001978 if (list_empty(&obj->userfault_link))
Chris Wilson9c870d02016-10-24 13:42:15 +01001979 goto out;
Chris Wilson901782b2009-07-10 08:18:50 +01001980
Chris Wilson3594a3e2016-10-24 13:42:16 +01001981 list_del_init(&obj->userfault_link);
David Herrmann6796cb12014-01-03 14:24:19 +01001982 drm_vma_node_unmap(&obj->base.vma_node,
1983 obj->base.dev->anon_inode->i_mapping);
Chris Wilson349f2cc2016-04-13 17:35:12 +01001984
1985 /* Ensure that the CPU's PTE are revoked and there are not outstanding
1986 * memory transactions from userspace before we return. The TLB
1987 * flushing implied above by changing the PTE above *should* be
1988 * sufficient, an extra barrier here just provides us with a bit
1989 * of paranoid documentation about our requirement to serialise
1990 * memory writes before touching registers / GSM.
1991 */
1992 wmb();
Chris Wilson9c870d02016-10-24 13:42:15 +01001993
1994out:
1995 intel_runtime_pm_put(i915);
Chris Wilson901782b2009-07-10 08:18:50 +01001996}
1997
Chris Wilson7c108fd2016-10-24 13:42:18 +01001998void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv)
Chris Wilsoneedd10f2014-06-16 08:57:44 +01001999{
Chris Wilson3594a3e2016-10-24 13:42:16 +01002000 struct drm_i915_gem_object *obj, *on;
Chris Wilson7c108fd2016-10-24 13:42:18 +01002001 int i;
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002002
Chris Wilson3594a3e2016-10-24 13:42:16 +01002003 /*
2004 * Only called during RPM suspend. All users of the userfault_list
2005 * must be holding an RPM wakeref to ensure that this can not
2006 * run concurrently with themselves (and use the struct_mutex for
2007 * protection between themselves).
2008 */
2009
2010 list_for_each_entry_safe(obj, on,
2011 &dev_priv->mm.userfault_list, userfault_link) {
Chris Wilson275f0392016-10-24 13:42:14 +01002012 list_del_init(&obj->userfault_link);
Chris Wilson275f0392016-10-24 13:42:14 +01002013 drm_vma_node_unmap(&obj->base.vma_node,
2014 obj->base.dev->anon_inode->i_mapping);
Chris Wilson275f0392016-10-24 13:42:14 +01002015 }
Chris Wilson7c108fd2016-10-24 13:42:18 +01002016
2017 /* The fence will be lost when the device powers down. If any were
2018 * in use by hardware (i.e. they are pinned), we should not be powering
2019 * down! All other fences will be reacquired by the user upon waking.
2020 */
2021 for (i = 0; i < dev_priv->num_fence_regs; i++) {
2022 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
2023
Chris Wilsone0ec3ec2017-02-03 12:57:17 +00002024 /* Ideally we want to assert that the fence register is not
2025 * live at this point (i.e. that no piece of code will be
2026 * trying to write through fence + GTT, as that both violates
2027 * our tracking of activity and associated locking/barriers,
2028 * but also is illegal given that the hw is powered down).
2029 *
2030 * Previously we used reg->pin_count as a "liveness" indicator.
2031 * That is not sufficient, and we need a more fine-grained
2032 * tool if we want to have a sanity check here.
2033 */
Chris Wilson7c108fd2016-10-24 13:42:18 +01002034
2035 if (!reg->vma)
2036 continue;
2037
2038 GEM_BUG_ON(!list_empty(&reg->vma->obj->userfault_link));
2039 reg->dirty = true;
2040 }
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002041}
2042
Chris Wilsond8cb5082012-08-11 15:41:03 +01002043static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
2044{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002045 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002046 int err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002047
Chris Wilsonf3f61842016-08-05 10:14:14 +01002048 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002049 if (likely(!err))
Chris Wilsonf3f61842016-08-05 10:14:14 +01002050 return 0;
Daniel Vetterda494d72012-12-20 15:11:16 +01002051
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002052 /* Attempt to reap some mmap space from dead objects */
2053 do {
2054 err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
2055 if (err)
2056 break;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002057
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002058 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002059 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002060 if (!err)
2061 break;
2062
2063 } while (flush_delayed_work(&dev_priv->gt.retire_work));
Daniel Vetterda494d72012-12-20 15:11:16 +01002064
Chris Wilsonf3f61842016-08-05 10:14:14 +01002065 return err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002066}
2067
2068static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
2069{
Chris Wilsond8cb5082012-08-11 15:41:03 +01002070 drm_gem_free_mmap_offset(&obj->base);
2071}
2072
Dave Airlieda6b51d2014-12-24 13:11:17 +10002073int
Dave Airlieff72145b2011-02-07 12:16:14 +10002074i915_gem_mmap_gtt(struct drm_file *file,
2075 struct drm_device *dev,
Dave Airlieda6b51d2014-12-24 13:11:17 +10002076 uint32_t handle,
Dave Airlieff72145b2011-02-07 12:16:14 +10002077 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002078{
Chris Wilson05394f32010-11-08 19:18:58 +00002079 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002080 int ret;
2081
Chris Wilson03ac0642016-07-20 13:31:51 +01002082 obj = i915_gem_object_lookup(file, handle);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002083 if (!obj)
2084 return -ENOENT;
Chris Wilsonab182822009-09-22 18:46:17 +01002085
Chris Wilsond8cb5082012-08-11 15:41:03 +01002086 ret = i915_gem_object_create_mmap_offset(obj);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002087 if (ret == 0)
2088 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002089
Chris Wilsonf0cd5182016-10-28 13:58:43 +01002090 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002091 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002092}
2093
Dave Airlieff72145b2011-02-07 12:16:14 +10002094/**
2095 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
2096 * @dev: DRM device
2097 * @data: GTT mapping ioctl data
2098 * @file: GEM object info
2099 *
2100 * Simply returns the fake offset to userspace so it can mmap it.
2101 * The mmap call will end up in drm_gem_mmap(), which will set things
2102 * up so we can get faults in the handler above.
2103 *
2104 * The fault handler will take care of binding the object into the GTT
2105 * (since it may have been evicted to make room for something), allocating
2106 * a fence register, and mapping the appropriate aperture address into
2107 * userspace.
2108 */
2109int
2110i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
2111 struct drm_file *file)
2112{
2113 struct drm_i915_gem_mmap_gtt *args = data;
2114
Dave Airlieda6b51d2014-12-24 13:11:17 +10002115 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
Dave Airlieff72145b2011-02-07 12:16:14 +10002116}
2117
Daniel Vetter225067e2012-08-20 10:23:20 +02002118/* Immediately discard the backing storage */
2119static void
2120i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01002121{
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002122 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02002123
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002124 if (obj->base.filp == NULL)
2125 return;
2126
Daniel Vetter225067e2012-08-20 10:23:20 +02002127 /* Our goal here is to return as much of the memory as
2128 * is possible back to the system as we are called from OOM.
2129 * To do this we must instruct the shmfs to drop all of its
2130 * backing pages, *now*.
Chris Wilsone5281cc2010-10-28 13:45:36 +01002131 */
Chris Wilson55372522014-03-25 13:23:06 +00002132 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002133 obj->mm.madv = __I915_MADV_PURGED;
Chris Wilsone5281cc2010-10-28 13:45:36 +01002134}
Chris Wilsone5281cc2010-10-28 13:45:36 +01002135
Chris Wilson55372522014-03-25 13:23:06 +00002136/* Try to discard unwanted pages */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002137void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
Daniel Vetter225067e2012-08-20 10:23:20 +02002138{
Chris Wilson55372522014-03-25 13:23:06 +00002139 struct address_space *mapping;
2140
Chris Wilson1233e2d2016-10-28 13:58:37 +01002141 lockdep_assert_held(&obj->mm.lock);
2142 GEM_BUG_ON(obj->mm.pages);
2143
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002144 switch (obj->mm.madv) {
Chris Wilson55372522014-03-25 13:23:06 +00002145 case I915_MADV_DONTNEED:
2146 i915_gem_object_truncate(obj);
2147 case __I915_MADV_PURGED:
2148 return;
2149 }
2150
2151 if (obj->base.filp == NULL)
2152 return;
2153
Al Viro93c76a32015-12-04 23:45:44 -05002154 mapping = obj->base.filp->f_mapping,
Chris Wilson55372522014-03-25 13:23:06 +00002155 invalidate_mapping_pages(mapping, 0, (loff_t)-1);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002156}
2157
Chris Wilson5cdf5882010-09-27 15:51:07 +01002158static void
Chris Wilson03ac84f2016-10-28 13:58:36 +01002159i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj,
2160 struct sg_table *pages)
Eric Anholt673a3942008-07-30 12:06:12 -07002161{
Dave Gordon85d12252016-05-20 11:54:06 +01002162 struct sgt_iter sgt_iter;
2163 struct page *page;
Daniel Vetter1286ff72012-05-10 15:25:09 +02002164
Chris Wilsone5facdf2016-12-23 14:57:57 +00002165 __i915_gem_object_release_shmem(obj, pages, true);
Eric Anholt856fa192009-03-19 14:10:50 -07002166
Chris Wilson03ac84f2016-10-28 13:58:36 +01002167 i915_gem_gtt_finish_pages(obj, pages);
Imre Deake2273302015-07-09 12:59:05 +03002168
Daniel Vetter6dacfd22011-09-12 21:30:02 +02002169 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002170 i915_gem_object_save_bit_17_swizzle(obj, pages);
Eric Anholt280b7132009-03-12 16:56:27 -07002171
Chris Wilson03ac84f2016-10-28 13:58:36 +01002172 for_each_sgt_page(page, sgt_iter, pages) {
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002173 if (obj->mm.dirty)
Chris Wilson9da3da62012-06-01 15:20:22 +01002174 set_page_dirty(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002175
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002176 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson9da3da62012-06-01 15:20:22 +01002177 mark_page_accessed(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002178
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002179 put_page(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002180 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002181 obj->mm.dirty = false;
Eric Anholt673a3942008-07-30 12:06:12 -07002182
Chris Wilson03ac84f2016-10-28 13:58:36 +01002183 sg_free_table(pages);
2184 kfree(pages);
Chris Wilson37e680a2012-06-07 15:38:42 +01002185}
2186
Chris Wilson96d77632016-10-28 13:58:33 +01002187static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
2188{
2189 struct radix_tree_iter iter;
2190 void **slot;
2191
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002192 radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
2193 radix_tree_delete(&obj->mm.get_page.radix, iter.index);
Chris Wilson96d77632016-10-28 13:58:33 +01002194}
2195
Chris Wilson548625e2016-11-01 12:11:34 +00002196void __i915_gem_object_put_pages(struct drm_i915_gem_object *obj,
2197 enum i915_mm_subclass subclass)
Chris Wilson37e680a2012-06-07 15:38:42 +01002198{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002199 struct sg_table *pages;
Chris Wilson37e680a2012-06-07 15:38:42 +01002200
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002201 if (i915_gem_object_has_pinned_pages(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002202 return;
Chris Wilsona5570172012-09-04 21:02:54 +01002203
Chris Wilson15717de2016-08-04 07:52:26 +01002204 GEM_BUG_ON(obj->bind_count);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002205 if (!READ_ONCE(obj->mm.pages))
2206 return;
2207
2208 /* May be called by shrinker from within get_pages() (on another bo) */
Chris Wilson548625e2016-11-01 12:11:34 +00002209 mutex_lock_nested(&obj->mm.lock, subclass);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002210 if (unlikely(atomic_read(&obj->mm.pages_pin_count)))
2211 goto unlock;
Ben Widawsky3e123022013-07-31 17:00:04 -07002212
Chris Wilsona2165e32012-12-03 11:49:00 +00002213 /* ->put_pages might need to allocate memory for the bit17 swizzle
2214 * array, hence protect them from being reaped by removing them from gtt
2215 * lists early. */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002216 pages = fetch_and_zero(&obj->mm.pages);
2217 GEM_BUG_ON(!pages);
Chris Wilsona2165e32012-12-03 11:49:00 +00002218
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002219 if (obj->mm.mapping) {
Chris Wilson4b30cb22016-08-18 17:16:42 +01002220 void *ptr;
2221
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002222 ptr = ptr_mask_bits(obj->mm.mapping);
Chris Wilson4b30cb22016-08-18 17:16:42 +01002223 if (is_vmalloc_addr(ptr))
2224 vunmap(ptr);
Chris Wilsonfb8621d2016-04-08 12:11:14 +01002225 else
Chris Wilson4b30cb22016-08-18 17:16:42 +01002226 kunmap(kmap_to_page(ptr));
2227
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002228 obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002229 }
2230
Chris Wilson96d77632016-10-28 13:58:33 +01002231 __i915_gem_object_reset_page_iter(obj);
2232
Chris Wilson03ac84f2016-10-28 13:58:36 +01002233 obj->ops->put_pages(obj, pages);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002234unlock:
2235 mutex_unlock(&obj->mm.lock);
Chris Wilson6c085a72012-08-20 11:40:46 +02002236}
2237
Chris Wilson935a2f72017-02-13 17:15:13 +00002238static bool i915_sg_trim(struct sg_table *orig_st)
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002239{
2240 struct sg_table new_st;
2241 struct scatterlist *sg, *new_sg;
2242 unsigned int i;
2243
2244 if (orig_st->nents == orig_st->orig_nents)
Chris Wilson935a2f72017-02-13 17:15:13 +00002245 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002246
Chris Wilson8bfc478f2016-12-23 14:57:58 +00002247 if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN))
Chris Wilson935a2f72017-02-13 17:15:13 +00002248 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002249
2250 new_sg = new_st.sgl;
2251 for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
2252 sg_set_page(new_sg, sg_page(sg), sg->length, 0);
2253 /* called before being DMA mapped, no need to copy sg->dma_* */
2254 new_sg = sg_next(new_sg);
2255 }
Chris Wilsonc2dc6cc2016-12-19 12:43:46 +00002256 GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002257
2258 sg_free_table(orig_st);
2259
2260 *orig_st = new_st;
Chris Wilson935a2f72017-02-13 17:15:13 +00002261 return true;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002262}
2263
Chris Wilson03ac84f2016-10-28 13:58:36 +01002264static struct sg_table *
Chris Wilson6c085a72012-08-20 11:40:46 +02002265i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002266{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002267 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsond766ef52016-12-19 12:43:45 +00002268 const unsigned long page_count = obj->base.size / PAGE_SIZE;
2269 unsigned long i;
Eric Anholt673a3942008-07-30 12:06:12 -07002270 struct address_space *mapping;
Chris Wilson9da3da62012-06-01 15:20:22 +01002271 struct sg_table *st;
2272 struct scatterlist *sg;
Dave Gordon85d12252016-05-20 11:54:06 +01002273 struct sgt_iter sgt_iter;
Eric Anholt673a3942008-07-30 12:06:12 -07002274 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002275 unsigned long last_pfn = 0; /* suppress gcc warning */
Chris Wilson4ff340f02016-10-18 13:02:50 +01002276 unsigned int max_segment;
Imre Deake2273302015-07-09 12:59:05 +03002277 int ret;
Chris Wilson6c085a72012-08-20 11:40:46 +02002278 gfp_t gfp;
Eric Anholt673a3942008-07-30 12:06:12 -07002279
Chris Wilson6c085a72012-08-20 11:40:46 +02002280 /* Assert that the object is not currently in any GPU domain. As it
2281 * wasn't in the GTT, there shouldn't be any way it could have been in
2282 * a GPU cache
2283 */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002284 GEM_BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2285 GEM_BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
Chris Wilson6c085a72012-08-20 11:40:46 +02002286
Konrad Rzeszutek Wilk7453c542016-12-20 10:02:02 -05002287 max_segment = swiotlb_max_segment();
Chris Wilson871dfbd2016-10-11 09:20:21 +01002288 if (!max_segment)
Chris Wilson4ff340f02016-10-18 13:02:50 +01002289 max_segment = rounddown(UINT_MAX, PAGE_SIZE);
Chris Wilson871dfbd2016-10-11 09:20:21 +01002290
Chris Wilson9da3da62012-06-01 15:20:22 +01002291 st = kmalloc(sizeof(*st), GFP_KERNEL);
2292 if (st == NULL)
Chris Wilson03ac84f2016-10-28 13:58:36 +01002293 return ERR_PTR(-ENOMEM);
Eric Anholt673a3942008-07-30 12:06:12 -07002294
Chris Wilsond766ef52016-12-19 12:43:45 +00002295rebuild_st:
Chris Wilson9da3da62012-06-01 15:20:22 +01002296 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
Chris Wilson9da3da62012-06-01 15:20:22 +01002297 kfree(st);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002298 return ERR_PTR(-ENOMEM);
Chris Wilson9da3da62012-06-01 15:20:22 +01002299 }
2300
2301 /* Get the list of pages out of our struct file. They'll be pinned
2302 * at this point until we release them.
2303 *
2304 * Fail silently without starting the shrinker
2305 */
Al Viro93c76a32015-12-04 23:45:44 -05002306 mapping = obj->base.filp->f_mapping;
Michal Hockoc62d2552015-11-06 16:28:49 -08002307 gfp = mapping_gfp_constraint(mapping, ~(__GFP_IO | __GFP_RECLAIM));
Mel Gormand0164ad2015-11-06 16:28:21 -08002308 gfp |= __GFP_NORETRY | __GFP_NOWARN;
Imre Deak90797e62013-02-18 19:28:03 +02002309 sg = st->sgl;
2310 st->nents = 0;
2311 for (i = 0; i < page_count; i++) {
Chris Wilson6c085a72012-08-20 11:40:46 +02002312 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
2313 if (IS_ERR(page)) {
Chris Wilson21ab4e72014-09-09 11:16:08 +01002314 i915_gem_shrink(dev_priv,
2315 page_count,
2316 I915_SHRINK_BOUND |
2317 I915_SHRINK_UNBOUND |
2318 I915_SHRINK_PURGEABLE);
Chris Wilson6c085a72012-08-20 11:40:46 +02002319 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
2320 }
2321 if (IS_ERR(page)) {
2322 /* We've tried hard to allocate the memory by reaping
2323 * our own buffer, now let the real VM do its job and
2324 * go down in flames if truly OOM.
2325 */
David Herrmannf461d1b2014-05-25 14:34:10 +02002326 page = shmem_read_mapping_page(mapping, i);
Imre Deake2273302015-07-09 12:59:05 +03002327 if (IS_ERR(page)) {
2328 ret = PTR_ERR(page);
Chris Wilsonb17993b2016-11-14 11:29:30 +00002329 goto err_sg;
Imre Deake2273302015-07-09 12:59:05 +03002330 }
Chris Wilson6c085a72012-08-20 11:40:46 +02002331 }
Chris Wilson871dfbd2016-10-11 09:20:21 +01002332 if (!i ||
2333 sg->length >= max_segment ||
2334 page_to_pfn(page) != last_pfn + 1) {
Imre Deak90797e62013-02-18 19:28:03 +02002335 if (i)
2336 sg = sg_next(sg);
2337 st->nents++;
2338 sg_set_page(sg, page, PAGE_SIZE, 0);
2339 } else {
2340 sg->length += PAGE_SIZE;
2341 }
2342 last_pfn = page_to_pfn(page);
Daniel Vetter3bbbe702013-10-07 17:15:45 -03002343
2344 /* Check that the i965g/gm workaround works. */
2345 WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
Eric Anholt673a3942008-07-30 12:06:12 -07002346 }
Chris Wilson871dfbd2016-10-11 09:20:21 +01002347 if (sg) /* loop terminated early; short sg table */
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002348 sg_mark_end(sg);
Chris Wilson74ce6b62012-10-19 15:51:06 +01002349
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002350 /* Trim unused sg entries to avoid wasting memory. */
2351 i915_sg_trim(st);
2352
Chris Wilson03ac84f2016-10-28 13:58:36 +01002353 ret = i915_gem_gtt_prepare_pages(obj, st);
Chris Wilsond766ef52016-12-19 12:43:45 +00002354 if (ret) {
2355 /* DMA remapping failed? One possible cause is that
2356 * it could not reserve enough large entries, asking
2357 * for PAGE_SIZE chunks instead may be helpful.
2358 */
2359 if (max_segment > PAGE_SIZE) {
2360 for_each_sgt_page(page, sgt_iter, st)
2361 put_page(page);
2362 sg_free_table(st);
2363
2364 max_segment = PAGE_SIZE;
2365 goto rebuild_st;
2366 } else {
2367 dev_warn(&dev_priv->drm.pdev->dev,
2368 "Failed to DMA remap %lu pages\n",
2369 page_count);
2370 goto err_pages;
2371 }
2372 }
Imre Deake2273302015-07-09 12:59:05 +03002373
Eric Anholt673a3942008-07-30 12:06:12 -07002374 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002375 i915_gem_object_do_bit_17_swizzle(obj, st);
Eric Anholt673a3942008-07-30 12:06:12 -07002376
Chris Wilson03ac84f2016-10-28 13:58:36 +01002377 return st;
Eric Anholt673a3942008-07-30 12:06:12 -07002378
Chris Wilsonb17993b2016-11-14 11:29:30 +00002379err_sg:
Imre Deak90797e62013-02-18 19:28:03 +02002380 sg_mark_end(sg);
Chris Wilsonb17993b2016-11-14 11:29:30 +00002381err_pages:
Dave Gordon85d12252016-05-20 11:54:06 +01002382 for_each_sgt_page(page, sgt_iter, st)
2383 put_page(page);
Chris Wilson9da3da62012-06-01 15:20:22 +01002384 sg_free_table(st);
2385 kfree(st);
Chris Wilson0820baf2014-03-25 13:23:03 +00002386
2387 /* shmemfs first checks if there is enough memory to allocate the page
2388 * and reports ENOSPC should there be insufficient, along with the usual
2389 * ENOMEM for a genuine allocation failure.
2390 *
2391 * We use ENOSPC in our driver to mean that we have run out of aperture
2392 * space and so want to translate the error from shmemfs back to our
2393 * usual understanding of ENOMEM.
2394 */
Imre Deake2273302015-07-09 12:59:05 +03002395 if (ret == -ENOSPC)
2396 ret = -ENOMEM;
2397
Chris Wilson03ac84f2016-10-28 13:58:36 +01002398 return ERR_PTR(ret);
2399}
2400
2401void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
2402 struct sg_table *pages)
2403{
Chris Wilson1233e2d2016-10-28 13:58:37 +01002404 lockdep_assert_held(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002405
2406 obj->mm.get_page.sg_pos = pages->sgl;
2407 obj->mm.get_page.sg_idx = 0;
2408
2409 obj->mm.pages = pages;
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002410
2411 if (i915_gem_object_is_tiled(obj) &&
2412 to_i915(obj->base.dev)->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
2413 GEM_BUG_ON(obj->mm.quirked);
2414 __i915_gem_object_pin_pages(obj);
2415 obj->mm.quirked = true;
2416 }
Chris Wilson03ac84f2016-10-28 13:58:36 +01002417}
2418
2419static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
2420{
2421 struct sg_table *pages;
2422
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002423 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2424
Chris Wilson03ac84f2016-10-28 13:58:36 +01002425 if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
2426 DRM_DEBUG("Attempting to obtain a purgeable object\n");
2427 return -EFAULT;
2428 }
2429
2430 pages = obj->ops->get_pages(obj);
2431 if (unlikely(IS_ERR(pages)))
2432 return PTR_ERR(pages);
2433
2434 __i915_gem_object_set_pages(obj, pages);
2435 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002436}
2437
Chris Wilson37e680a2012-06-07 15:38:42 +01002438/* Ensure that the associated pages are gathered from the backing storage
Chris Wilson1233e2d2016-10-28 13:58:37 +01002439 * and pinned into our object. i915_gem_object_pin_pages() may be called
Chris Wilson37e680a2012-06-07 15:38:42 +01002440 * multiple times before they are released by a single call to
Chris Wilson1233e2d2016-10-28 13:58:37 +01002441 * i915_gem_object_unpin_pages() - once the pages are no longer referenced
Chris Wilson37e680a2012-06-07 15:38:42 +01002442 * either as a result of memory pressure (reaping pages under the shrinker)
2443 * or as the object is itself released.
2444 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002445int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
Chris Wilson37e680a2012-06-07 15:38:42 +01002446{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002447 int err;
Chris Wilson37e680a2012-06-07 15:38:42 +01002448
Chris Wilson1233e2d2016-10-28 13:58:37 +01002449 err = mutex_lock_interruptible(&obj->mm.lock);
2450 if (err)
2451 return err;
Chris Wilson4c7d62c2016-10-28 13:58:32 +01002452
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002453 if (unlikely(!obj->mm.pages)) {
2454 err = ____i915_gem_object_get_pages(obj);
2455 if (err)
2456 goto unlock;
2457
2458 smp_mb__before_atomic();
Chris Wilson1233e2d2016-10-28 13:58:37 +01002459 }
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002460 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson43e28f02013-01-08 10:53:09 +00002461
Chris Wilson1233e2d2016-10-28 13:58:37 +01002462unlock:
2463 mutex_unlock(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002464 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002465}
2466
Dave Gordondd6034c2016-05-20 11:54:04 +01002467/* The 'mapping' part of i915_gem_object_pin_map() below */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002468static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
2469 enum i915_map_type type)
Dave Gordondd6034c2016-05-20 11:54:04 +01002470{
2471 unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002472 struct sg_table *sgt = obj->mm.pages;
Dave Gordon85d12252016-05-20 11:54:06 +01002473 struct sgt_iter sgt_iter;
2474 struct page *page;
Dave Gordonb338fa42016-05-20 11:54:05 +01002475 struct page *stack_pages[32];
2476 struct page **pages = stack_pages;
Dave Gordondd6034c2016-05-20 11:54:04 +01002477 unsigned long i = 0;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002478 pgprot_t pgprot;
Dave Gordondd6034c2016-05-20 11:54:04 +01002479 void *addr;
2480
2481 /* A single page can always be kmapped */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002482 if (n_pages == 1 && type == I915_MAP_WB)
Dave Gordondd6034c2016-05-20 11:54:04 +01002483 return kmap(sg_page(sgt->sgl));
2484
Dave Gordonb338fa42016-05-20 11:54:05 +01002485 if (n_pages > ARRAY_SIZE(stack_pages)) {
2486 /* Too big for stack -- allocate temporary array instead */
2487 pages = drm_malloc_gfp(n_pages, sizeof(*pages), GFP_TEMPORARY);
2488 if (!pages)
2489 return NULL;
2490 }
Dave Gordondd6034c2016-05-20 11:54:04 +01002491
Dave Gordon85d12252016-05-20 11:54:06 +01002492 for_each_sgt_page(page, sgt_iter, sgt)
2493 pages[i++] = page;
Dave Gordondd6034c2016-05-20 11:54:04 +01002494
2495 /* Check that we have the expected number of pages */
2496 GEM_BUG_ON(i != n_pages);
2497
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002498 switch (type) {
2499 case I915_MAP_WB:
2500 pgprot = PAGE_KERNEL;
2501 break;
2502 case I915_MAP_WC:
2503 pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
2504 break;
2505 }
2506 addr = vmap(pages, n_pages, 0, pgprot);
Dave Gordondd6034c2016-05-20 11:54:04 +01002507
Dave Gordonb338fa42016-05-20 11:54:05 +01002508 if (pages != stack_pages)
2509 drm_free_large(pages);
Dave Gordondd6034c2016-05-20 11:54:04 +01002510
2511 return addr;
2512}
2513
2514/* get, pin, and map the pages of the object into kernel space */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002515void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
2516 enum i915_map_type type)
Chris Wilson0a798eb2016-04-08 12:11:11 +01002517{
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002518 enum i915_map_type has_type;
2519 bool pinned;
2520 void *ptr;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002521 int ret;
2522
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002523 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
Chris Wilson0a798eb2016-04-08 12:11:11 +01002524
Chris Wilson1233e2d2016-10-28 13:58:37 +01002525 ret = mutex_lock_interruptible(&obj->mm.lock);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002526 if (ret)
2527 return ERR_PTR(ret);
2528
Chris Wilson1233e2d2016-10-28 13:58:37 +01002529 pinned = true;
2530 if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002531 if (unlikely(!obj->mm.pages)) {
2532 ret = ____i915_gem_object_get_pages(obj);
2533 if (ret)
2534 goto err_unlock;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002535
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002536 smp_mb__before_atomic();
2537 }
2538 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002539 pinned = false;
2540 }
2541 GEM_BUG_ON(!obj->mm.pages);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002542
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002543 ptr = ptr_unpack_bits(obj->mm.mapping, has_type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002544 if (ptr && has_type != type) {
2545 if (pinned) {
2546 ret = -EBUSY;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002547 goto err_unpin;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002548 }
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002549
2550 if (is_vmalloc_addr(ptr))
2551 vunmap(ptr);
2552 else
2553 kunmap(kmap_to_page(ptr));
2554
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002555 ptr = obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002556 }
2557
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002558 if (!ptr) {
2559 ptr = i915_gem_object_map(obj, type);
2560 if (!ptr) {
2561 ret = -ENOMEM;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002562 goto err_unpin;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002563 }
2564
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002565 obj->mm.mapping = ptr_pack_bits(ptr, type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002566 }
2567
Chris Wilson1233e2d2016-10-28 13:58:37 +01002568out_unlock:
2569 mutex_unlock(&obj->mm.lock);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002570 return ptr;
2571
Chris Wilson1233e2d2016-10-28 13:58:37 +01002572err_unpin:
2573 atomic_dec(&obj->mm.pages_pin_count);
2574err_unlock:
2575 ptr = ERR_PTR(ret);
2576 goto out_unlock;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002577}
2578
Chris Wilson60958682016-12-31 11:20:11 +00002579static bool ban_context(const struct i915_gem_context *ctx)
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002580{
Chris Wilson60958682016-12-31 11:20:11 +00002581 return (i915_gem_context_is_bannable(ctx) &&
2582 ctx->ban_score >= CONTEXT_SCORE_BAN_THRESHOLD);
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002583}
2584
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002585static void i915_gem_context_mark_guilty(struct i915_gem_context *ctx)
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002586{
Mika Kuoppalabc1d53c2016-11-16 17:20:34 +02002587 ctx->guilty_count++;
Chris Wilson60958682016-12-31 11:20:11 +00002588 ctx->ban_score += CONTEXT_SCORE_GUILTY;
2589 if (ban_context(ctx))
2590 i915_gem_context_set_banned(ctx);
Mika Kuoppalab083a082016-11-18 15:10:47 +02002591
2592 DRM_DEBUG_DRIVER("context %s marked guilty (score %d) banned? %s\n",
Mika Kuoppalabc1d53c2016-11-16 17:20:34 +02002593 ctx->name, ctx->ban_score,
Chris Wilson60958682016-12-31 11:20:11 +00002594 yesno(i915_gem_context_is_banned(ctx)));
Mika Kuoppalab083a082016-11-18 15:10:47 +02002595
Chris Wilson60958682016-12-31 11:20:11 +00002596 if (!i915_gem_context_is_banned(ctx) || IS_ERR_OR_NULL(ctx->file_priv))
Mika Kuoppalab083a082016-11-18 15:10:47 +02002597 return;
2598
Chris Wilsond9e9da62016-11-22 14:41:18 +00002599 ctx->file_priv->context_bans++;
2600 DRM_DEBUG_DRIVER("client %s has had %d context banned\n",
2601 ctx->name, ctx->file_priv->context_bans);
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002602}
2603
2604static void i915_gem_context_mark_innocent(struct i915_gem_context *ctx)
2605{
Mika Kuoppalabc1d53c2016-11-16 17:20:34 +02002606 ctx->active_count++;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002607}
2608
Chris Wilson8d9fc7f2014-02-25 17:11:23 +02002609struct drm_i915_gem_request *
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002610i915_gem_find_active_request(struct intel_engine_cs *engine)
Chris Wilson9375e442010-09-19 12:21:28 +01002611{
Chris Wilson4db080f2013-12-04 11:37:09 +00002612 struct drm_i915_gem_request *request;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002613
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002614 /* We are called by the error capture and reset at a random
2615 * point in time. In particular, note that neither is crucially
2616 * ordered with an interrupt. After a hang, the GPU is dead and we
2617 * assume that no more writes can happen (we waited long enough for
2618 * all writes that were in transaction to be flushed) - adding an
2619 * extra delay for a recent interrupt is pointless. Hence, we do
2620 * not need an engine->irq_seqno_barrier() before the seqno reads.
2621 */
Chris Wilson73cb9702016-10-28 13:58:46 +01002622 list_for_each_entry(request, &engine->timeline->requests, link) {
Chris Wilson80b204b2016-10-28 13:58:58 +01002623 if (__i915_gem_request_completed(request))
Chris Wilson4db080f2013-12-04 11:37:09 +00002624 continue;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002625
Mika Kuoppala36193ac2017-01-17 17:59:02 +02002626 GEM_BUG_ON(request->engine != engine);
Chris Wilsonc00122f32017-02-12 17:19:58 +00002627 GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
2628 &request->fence.flags));
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002629 return request;
Chris Wilson4db080f2013-12-04 11:37:09 +00002630 }
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002631
2632 return NULL;
2633}
2634
Mika Kuoppalabf2f0432017-01-17 17:59:04 +02002635static bool engine_stalled(struct intel_engine_cs *engine)
2636{
2637 if (!engine->hangcheck.stalled)
2638 return false;
2639
2640 /* Check for possible seqno movement after hang declaration */
2641 if (engine->hangcheck.seqno != intel_engine_get_seqno(engine)) {
2642 DRM_DEBUG_DRIVER("%s pardoned\n", engine->name);
2643 return false;
2644 }
2645
2646 return true;
2647}
2648
Chris Wilson0e178ae2017-01-17 17:59:06 +02002649int i915_gem_reset_prepare(struct drm_i915_private *dev_priv)
Chris Wilson4c965542017-01-17 17:59:01 +02002650{
2651 struct intel_engine_cs *engine;
2652 enum intel_engine_id id;
Chris Wilson0e178ae2017-01-17 17:59:06 +02002653 int err = 0;
Chris Wilson4c965542017-01-17 17:59:01 +02002654
2655 /* Ensure irq handler finishes, and not run again. */
Chris Wilson0e178ae2017-01-17 17:59:06 +02002656 for_each_engine(engine, dev_priv, id) {
2657 struct drm_i915_gem_request *request;
2658
Chris Wilsonfe3288b2017-02-12 17:20:01 +00002659 /* Prevent the signaler thread from updating the request
2660 * state (by calling dma_fence_signal) as we are processing
2661 * the reset. The write from the GPU of the seqno is
2662 * asynchronous and the signaler thread may see a different
2663 * value to us and declare the request complete, even though
2664 * the reset routine have picked that request as the active
2665 * (incomplete) request. This conflict is not handled
2666 * gracefully!
2667 */
2668 kthread_park(engine->breadcrumbs.signaler);
2669
Chris Wilson1f7b8472017-02-08 14:30:33 +00002670 /* Prevent request submission to the hardware until we have
2671 * completed the reset in i915_gem_reset_finish(). If a request
2672 * is completed by one engine, it may then queue a request
2673 * to a second via its engine->irq_tasklet *just* as we are
2674 * calling engine->init_hw() and also writing the ELSP.
2675 * Turning off the engine->irq_tasklet until the reset is over
2676 * prevents the race.
2677 */
Chris Wilson4c965542017-01-17 17:59:01 +02002678 tasklet_kill(&engine->irq_tasklet);
Chris Wilson1d309632017-02-12 17:20:00 +00002679 tasklet_disable(&engine->irq_tasklet);
Chris Wilson4c965542017-01-17 17:59:01 +02002680
Chris Wilson8c12d122017-02-10 18:52:14 +00002681 if (engine->irq_seqno_barrier)
2682 engine->irq_seqno_barrier(engine);
2683
Chris Wilson0e178ae2017-01-17 17:59:06 +02002684 if (engine_stalled(engine)) {
2685 request = i915_gem_find_active_request(engine);
2686 if (request && request->fence.error == -EIO)
2687 err = -EIO; /* Previous reset failed! */
2688 }
2689 }
2690
Chris Wilson4c965542017-01-17 17:59:01 +02002691 i915_gem_revoke_fences(dev_priv);
Chris Wilson0e178ae2017-01-17 17:59:06 +02002692
2693 return err;
Chris Wilson4c965542017-01-17 17:59:01 +02002694}
2695
Mika Kuoppala36193ac2017-01-17 17:59:02 +02002696static void skip_request(struct drm_i915_gem_request *request)
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002697{
Chris Wilson821ed7d2016-09-09 14:11:53 +01002698 void *vaddr = request->ring->vaddr;
2699 u32 head;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002700
Chris Wilson821ed7d2016-09-09 14:11:53 +01002701 /* As this request likely depends on state from the lost
2702 * context, clear out all the user operations leaving the
2703 * breadcrumb at the end (so we get the fence notifications).
2704 */
2705 head = request->head;
2706 if (request->postfix < head) {
2707 memset(vaddr + head, 0, request->ring->size - head);
2708 head = 0;
2709 }
2710 memset(vaddr + head, 0, request->postfix - head);
Chris Wilsonc0d5f322017-01-10 17:22:43 +00002711
2712 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson4db080f2013-12-04 11:37:09 +00002713}
2714
Mika Kuoppala36193ac2017-01-17 17:59:02 +02002715static void engine_skip_context(struct drm_i915_gem_request *request)
2716{
2717 struct intel_engine_cs *engine = request->engine;
2718 struct i915_gem_context *hung_ctx = request->ctx;
2719 struct intel_timeline *timeline;
2720 unsigned long flags;
2721
2722 timeline = i915_gem_context_lookup_timeline(hung_ctx, engine);
2723
2724 spin_lock_irqsave(&engine->timeline->lock, flags);
2725 spin_lock(&timeline->lock);
2726
2727 list_for_each_entry_continue(request, &engine->timeline->requests, link)
2728 if (request->ctx == hung_ctx)
2729 skip_request(request);
2730
2731 list_for_each_entry(request, &timeline->requests, link)
2732 skip_request(request);
2733
2734 spin_unlock(&timeline->lock);
2735 spin_unlock_irqrestore(&engine->timeline->lock, flags);
2736}
2737
Mika Kuoppala61da5362017-01-17 17:59:05 +02002738/* Returns true if the request was guilty of hang */
2739static bool i915_gem_reset_request(struct drm_i915_gem_request *request)
2740{
2741 /* Read once and return the resolution */
2742 const bool guilty = engine_stalled(request->engine);
2743
Mika Kuoppala71895a02017-01-17 17:59:07 +02002744 /* The guilty request will get skipped on a hung engine.
2745 *
2746 * Users of client default contexts do not rely on logical
2747 * state preserved between batches so it is safe to execute
2748 * queued requests following the hang. Non default contexts
2749 * rely on preserved state, so skipping a batch loses the
2750 * evolution of the state and it needs to be considered corrupted.
2751 * Executing more queued batches on top of corrupted state is
2752 * risky. But we take the risk by trying to advance through
2753 * the queued requests in order to make the client behaviour
2754 * more predictable around resets, by not throwing away random
2755 * amount of batches it has prepared for execution. Sophisticated
2756 * clients can use gem_reset_stats_ioctl and dma fence status
2757 * (exported via sync_file info ioctl on explicit fences) to observe
2758 * when it loses the context state and should rebuild accordingly.
2759 *
2760 * The context ban, and ultimately the client ban, mechanism are safety
2761 * valves if client submission ends up resulting in nothing more than
2762 * subsequent hangs.
2763 */
2764
Mika Kuoppala61da5362017-01-17 17:59:05 +02002765 if (guilty) {
2766 i915_gem_context_mark_guilty(request->ctx);
2767 skip_request(request);
2768 } else {
2769 i915_gem_context_mark_innocent(request->ctx);
2770 dma_fence_set_error(&request->fence, -EAGAIN);
2771 }
2772
2773 return guilty;
2774}
2775
Chris Wilson821ed7d2016-09-09 14:11:53 +01002776static void i915_gem_reset_engine(struct intel_engine_cs *engine)
Chris Wilson4db080f2013-12-04 11:37:09 +00002777{
Chris Wilsondcff85c2016-08-05 10:14:11 +01002778 struct drm_i915_gem_request *request;
Chris Wilson608c1a52015-09-03 13:01:40 +01002779
Chris Wilson821ed7d2016-09-09 14:11:53 +01002780 request = i915_gem_find_active_request(engine);
Chris Wilsonc0dcb202017-02-07 15:24:37 +00002781 if (request && i915_gem_reset_request(request)) {
2782 DRM_DEBUG_DRIVER("resetting %s to restart from tail of request 0x%x\n",
2783 engine->name, request->global_seqno);
Chris Wilson821ed7d2016-09-09 14:11:53 +01002784
Chris Wilsonc0dcb202017-02-07 15:24:37 +00002785 /* If this context is now banned, skip all pending requests. */
2786 if (i915_gem_context_is_banned(request->ctx))
2787 engine_skip_context(request);
2788 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01002789
2790 /* Setup the CS to resume from the breadcrumb of the hung request */
2791 engine->reset_hw(engine, request);
Chris Wilson821ed7d2016-09-09 14:11:53 +01002792}
2793
Chris Wilsond8027092017-02-08 14:30:32 +00002794void i915_gem_reset(struct drm_i915_private *dev_priv)
Chris Wilson821ed7d2016-09-09 14:11:53 +01002795{
2796 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302797 enum intel_engine_id id;
Chris Wilson821ed7d2016-09-09 14:11:53 +01002798
Chris Wilson4c7d62c2016-10-28 13:58:32 +01002799 lockdep_assert_held(&dev_priv->drm.struct_mutex);
2800
Chris Wilson821ed7d2016-09-09 14:11:53 +01002801 i915_gem_retire_requests(dev_priv);
2802
Chris Wilson2ae55732017-02-12 17:20:02 +00002803 for_each_engine(engine, dev_priv, id) {
2804 struct i915_gem_context *ctx;
2805
Chris Wilson821ed7d2016-09-09 14:11:53 +01002806 i915_gem_reset_engine(engine);
Chris Wilson2ae55732017-02-12 17:20:02 +00002807 ctx = fetch_and_zero(&engine->last_retired_context);
2808 if (ctx)
2809 engine->context_unpin(engine, ctx);
2810 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01002811
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00002812 i915_gem_restore_fences(dev_priv);
Chris Wilsonf2a91d12016-09-21 14:51:06 +01002813
2814 if (dev_priv->gt.awake) {
2815 intel_sanitize_gt_powersave(dev_priv);
2816 intel_enable_gt_powersave(dev_priv);
2817 if (INTEL_GEN(dev_priv) >= 6)
2818 gen6_rps_busy(dev_priv);
2819 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01002820}
2821
Chris Wilsond8027092017-02-08 14:30:32 +00002822void i915_gem_reset_finish(struct drm_i915_private *dev_priv)
2823{
Chris Wilson1f7b8472017-02-08 14:30:33 +00002824 struct intel_engine_cs *engine;
2825 enum intel_engine_id id;
2826
Chris Wilsond8027092017-02-08 14:30:32 +00002827 lockdep_assert_held(&dev_priv->drm.struct_mutex);
Chris Wilson1f7b8472017-02-08 14:30:33 +00002828
Chris Wilsonfe3288b2017-02-12 17:20:01 +00002829 for_each_engine(engine, dev_priv, id) {
Chris Wilson1f7b8472017-02-08 14:30:33 +00002830 tasklet_enable(&engine->irq_tasklet);
Chris Wilsonfe3288b2017-02-12 17:20:01 +00002831 kthread_unpark(engine->breadcrumbs.signaler);
2832 }
Chris Wilsond8027092017-02-08 14:30:32 +00002833}
2834
Chris Wilson821ed7d2016-09-09 14:11:53 +01002835static void nop_submit_request(struct drm_i915_gem_request *request)
2836{
Chris Wilson3cd94422017-01-10 17:22:45 +00002837 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson3dcf93f2016-11-22 14:41:20 +00002838 i915_gem_request_submit(request);
2839 intel_engine_init_global_seqno(request->engine, request->global_seqno);
Chris Wilson821ed7d2016-09-09 14:11:53 +01002840}
2841
Chris Wilson2a20d6f2017-01-10 17:22:46 +00002842static void engine_set_wedged(struct intel_engine_cs *engine)
Chris Wilson821ed7d2016-09-09 14:11:53 +01002843{
Chris Wilson3cd94422017-01-10 17:22:45 +00002844 struct drm_i915_gem_request *request;
2845 unsigned long flags;
2846
Chris Wilson20e49332016-11-22 14:41:21 +00002847 /* We need to be sure that no thread is running the old callback as
2848 * we install the nop handler (otherwise we would submit a request
2849 * to hardware that will never complete). In order to prevent this
2850 * race, we wait until the machine is idle before making the swap
2851 * (using stop_machine()).
2852 */
Chris Wilson821ed7d2016-09-09 14:11:53 +01002853 engine->submit_request = nop_submit_request;
Chris Wilson70c2a242016-09-09 14:11:46 +01002854
Chris Wilson3cd94422017-01-10 17:22:45 +00002855 /* Mark all executing requests as skipped */
2856 spin_lock_irqsave(&engine->timeline->lock, flags);
2857 list_for_each_entry(request, &engine->timeline->requests, link)
2858 dma_fence_set_error(&request->fence, -EIO);
2859 spin_unlock_irqrestore(&engine->timeline->lock, flags);
2860
Chris Wilsonc4b09302016-07-20 09:21:10 +01002861 /* Mark all pending requests as complete so that any concurrent
2862 * (lockless) lookup doesn't try and wait upon the request as we
2863 * reset it.
2864 */
Chris Wilson73cb9702016-10-28 13:58:46 +01002865 intel_engine_init_global_seqno(engine,
Chris Wilsoncb399ea2016-11-01 10:03:16 +00002866 intel_engine_last_submit(engine));
Chris Wilsonc4b09302016-07-20 09:21:10 +01002867
Ben Widawsky1d62bee2014-01-01 10:15:13 -08002868 /*
Oscar Mateodcb4c122014-11-13 10:28:10 +00002869 * Clear the execlists queue up before freeing the requests, as those
2870 * are the ones that keep the context and ringbuffer backing objects
2871 * pinned in place.
2872 */
Oscar Mateodcb4c122014-11-13 10:28:10 +00002873
Tomas Elf7de1691a2015-10-19 16:32:32 +01002874 if (i915.enable_execlists) {
Chris Wilson663f71e2016-11-14 20:41:00 +00002875 unsigned long flags;
2876
2877 spin_lock_irqsave(&engine->timeline->lock, flags);
2878
Chris Wilson70c2a242016-09-09 14:11:46 +01002879 i915_gem_request_put(engine->execlist_port[0].request);
2880 i915_gem_request_put(engine->execlist_port[1].request);
2881 memset(engine->execlist_port, 0, sizeof(engine->execlist_port));
Chris Wilson20311bd2016-11-14 20:41:03 +00002882 engine->execlist_queue = RB_ROOT;
2883 engine->execlist_first = NULL;
Chris Wilson663f71e2016-11-14 20:41:00 +00002884
2885 spin_unlock_irqrestore(&engine->timeline->lock, flags);
Oscar Mateodcb4c122014-11-13 10:28:10 +00002886 }
Eric Anholt673a3942008-07-30 12:06:12 -07002887}
2888
Chris Wilson20e49332016-11-22 14:41:21 +00002889static int __i915_gem_set_wedged_BKL(void *data)
Eric Anholt673a3942008-07-30 12:06:12 -07002890{
Chris Wilson20e49332016-11-22 14:41:21 +00002891 struct drm_i915_private *i915 = data;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002892 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302893 enum intel_engine_id id;
Eric Anholt673a3942008-07-30 12:06:12 -07002894
Chris Wilson20e49332016-11-22 14:41:21 +00002895 for_each_engine(engine, i915, id)
Chris Wilson2a20d6f2017-01-10 17:22:46 +00002896 engine_set_wedged(engine);
Chris Wilson20e49332016-11-22 14:41:21 +00002897
2898 return 0;
2899}
2900
2901void i915_gem_set_wedged(struct drm_i915_private *dev_priv)
2902{
Chris Wilson821ed7d2016-09-09 14:11:53 +01002903 lockdep_assert_held(&dev_priv->drm.struct_mutex);
2904 set_bit(I915_WEDGED, &dev_priv->gpu_error.flags);
Chris Wilson4db080f2013-12-04 11:37:09 +00002905
Chris Wilson20e49332016-11-22 14:41:21 +00002906 stop_machine(__i915_gem_set_wedged_BKL, dev_priv, NULL);
Chris Wilsondfaae392010-09-22 10:31:52 +01002907
Chris Wilson20e49332016-11-22 14:41:21 +00002908 i915_gem_context_lost(dev_priv);
Chris Wilson821ed7d2016-09-09 14:11:53 +01002909 i915_gem_retire_requests(dev_priv);
Chris Wilson20e49332016-11-22 14:41:21 +00002910
2911 mod_delayed_work(dev_priv->wq, &dev_priv->gt.idle_work, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002912}
2913
Daniel Vetter75ef9da2010-08-21 00:25:16 +02002914static void
Eric Anholt673a3942008-07-30 12:06:12 -07002915i915_gem_retire_work_handler(struct work_struct *work)
2916{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002917 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01002918 container_of(work, typeof(*dev_priv), gt.retire_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01002919 struct drm_device *dev = &dev_priv->drm;
Eric Anholt673a3942008-07-30 12:06:12 -07002920
Chris Wilson891b48c2010-09-29 12:26:37 +01002921 /* Come back later if the device is busy... */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002922 if (mutex_trylock(&dev->struct_mutex)) {
Chris Wilson67d97da2016-07-04 08:08:31 +01002923 i915_gem_retire_requests(dev_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002924 mutex_unlock(&dev->struct_mutex);
2925 }
Chris Wilson67d97da2016-07-04 08:08:31 +01002926
2927 /* Keep the retire handler running until we are finally idle.
2928 * We do not need to do this test under locking as in the worst-case
2929 * we queue the retire worker once too often.
2930 */
Chris Wilsonc9615612016-07-09 10:12:06 +01002931 if (READ_ONCE(dev_priv->gt.awake)) {
2932 i915_queue_hangcheck(dev_priv);
Chris Wilson67d97da2016-07-04 08:08:31 +01002933 queue_delayed_work(dev_priv->wq,
2934 &dev_priv->gt.retire_work,
Chris Wilsonbcb45082012-10-05 17:02:57 +01002935 round_jiffies_up_relative(HZ));
Chris Wilsonc9615612016-07-09 10:12:06 +01002936 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002937}
Chris Wilson891b48c2010-09-29 12:26:37 +01002938
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002939static void
2940i915_gem_idle_work_handler(struct work_struct *work)
2941{
2942 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01002943 container_of(work, typeof(*dev_priv), gt.idle_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01002944 struct drm_device *dev = &dev_priv->drm;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00002945 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302946 enum intel_engine_id id;
Chris Wilson67d97da2016-07-04 08:08:31 +01002947 bool rearm_hangcheck;
2948
2949 if (!READ_ONCE(dev_priv->gt.awake))
2950 return;
2951
Imre Deak0cb56702016-11-07 11:20:04 +02002952 /*
2953 * Wait for last execlists context complete, but bail out in case a
2954 * new request is submitted.
2955 */
2956 wait_for(READ_ONCE(dev_priv->gt.active_requests) ||
2957 intel_execlists_idle(dev_priv), 10);
2958
Chris Wilson28176ef2016-10-28 13:58:56 +01002959 if (READ_ONCE(dev_priv->gt.active_requests))
Chris Wilson67d97da2016-07-04 08:08:31 +01002960 return;
2961
2962 rearm_hangcheck =
2963 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
2964
2965 if (!mutex_trylock(&dev->struct_mutex)) {
2966 /* Currently busy, come back later */
2967 mod_delayed_work(dev_priv->wq,
2968 &dev_priv->gt.idle_work,
2969 msecs_to_jiffies(50));
2970 goto out_rearm;
2971 }
2972
Imre Deak93c97dc2016-11-07 11:20:03 +02002973 /*
2974 * New request retired after this work handler started, extend active
2975 * period until next instance of the work.
2976 */
2977 if (work_pending(work))
2978 goto out_unlock;
2979
Chris Wilson28176ef2016-10-28 13:58:56 +01002980 if (dev_priv->gt.active_requests)
Chris Wilson67d97da2016-07-04 08:08:31 +01002981 goto out_unlock;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002982
Imre Deak0cb56702016-11-07 11:20:04 +02002983 if (wait_for(intel_execlists_idle(dev_priv), 10))
2984 DRM_ERROR("Timeout waiting for engines to idle\n");
2985
Akash Goel3b3f1652016-10-13 22:44:48 +05302986 for_each_engine(engine, dev_priv, id)
Chris Wilson67d97da2016-07-04 08:08:31 +01002987 i915_gem_batch_pool_fini(&engine->batch_pool);
Zou Nan hai852835f2010-05-21 09:08:56 +08002988
Chris Wilson67d97da2016-07-04 08:08:31 +01002989 GEM_BUG_ON(!dev_priv->gt.awake);
2990 dev_priv->gt.awake = false;
2991 rearm_hangcheck = false;
Daniel Vetter30ecad72015-12-09 09:29:36 +01002992
Chris Wilson67d97da2016-07-04 08:08:31 +01002993 if (INTEL_GEN(dev_priv) >= 6)
2994 gen6_rps_idle(dev_priv);
2995 intel_runtime_pm_put(dev_priv);
2996out_unlock:
2997 mutex_unlock(&dev->struct_mutex);
Chris Wilson35c94182015-04-07 16:20:37 +01002998
Chris Wilson67d97da2016-07-04 08:08:31 +01002999out_rearm:
3000 if (rearm_hangcheck) {
3001 GEM_BUG_ON(!dev_priv->gt.awake);
3002 i915_queue_hangcheck(dev_priv);
Chris Wilson35c94182015-04-07 16:20:37 +01003003 }
Eric Anholt673a3942008-07-30 12:06:12 -07003004}
3005
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003006void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
3007{
3008 struct drm_i915_gem_object *obj = to_intel_bo(gem);
3009 struct drm_i915_file_private *fpriv = file->driver_priv;
3010 struct i915_vma *vma, *vn;
3011
3012 mutex_lock(&obj->base.dev->struct_mutex);
3013 list_for_each_entry_safe(vma, vn, &obj->vma_list, obj_link)
3014 if (vma->vm->file == fpriv)
3015 i915_vma_close(vma);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003016
3017 if (i915_gem_object_is_active(obj) &&
3018 !i915_gem_object_has_active_reference(obj)) {
3019 i915_gem_object_set_active_reference(obj);
3020 i915_gem_object_get(obj);
3021 }
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003022 mutex_unlock(&obj->base.dev->struct_mutex);
3023}
3024
Chris Wilsone95433c2016-10-28 13:58:27 +01003025static unsigned long to_wait_timeout(s64 timeout_ns)
3026{
3027 if (timeout_ns < 0)
3028 return MAX_SCHEDULE_TIMEOUT;
3029
3030 if (timeout_ns == 0)
3031 return 0;
3032
3033 return nsecs_to_jiffies_timeout(timeout_ns);
3034}
3035
Ben Widawsky5816d642012-04-11 11:18:19 -07003036/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003037 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003038 * @dev: drm device pointer
3039 * @data: ioctl data blob
3040 * @file: drm file pointer
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003041 *
3042 * Returns 0 if successful, else an error is returned with the remaining time in
3043 * the timeout parameter.
3044 * -ETIME: object is still busy after timeout
3045 * -ERESTARTSYS: signal interrupted the wait
3046 * -ENONENT: object doesn't exist
3047 * Also possible, but rare:
3048 * -EAGAIN: GPU wedged
3049 * -ENOMEM: damn
3050 * -ENODEV: Internal IRQ fail
3051 * -E?: The add request failed
3052 *
3053 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
3054 * non-zero timeout parameter the wait ioctl will wait for the given number of
3055 * nanoseconds on an object becoming unbusy. Since the wait itself does so
3056 * without holding struct_mutex the object may become re-busied before this
3057 * function completes. A similar but shorter * race condition exists in the busy
3058 * ioctl
3059 */
3060int
3061i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
3062{
3063 struct drm_i915_gem_wait *args = data;
3064 struct drm_i915_gem_object *obj;
Chris Wilsone95433c2016-10-28 13:58:27 +01003065 ktime_t start;
3066 long ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003067
Daniel Vetter11b5d512014-09-29 15:31:26 +02003068 if (args->flags != 0)
3069 return -EINVAL;
3070
Chris Wilson03ac0642016-07-20 13:31:51 +01003071 obj = i915_gem_object_lookup(file, args->bo_handle);
Chris Wilson033d5492016-08-05 10:14:17 +01003072 if (!obj)
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003073 return -ENOENT;
Chris Wilson033d5492016-08-05 10:14:17 +01003074
Chris Wilsone95433c2016-10-28 13:58:27 +01003075 start = ktime_get();
3076
3077 ret = i915_gem_object_wait(obj,
3078 I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL,
3079 to_wait_timeout(args->timeout_ns),
3080 to_rps_client(file));
3081
3082 if (args->timeout_ns > 0) {
3083 args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
3084 if (args->timeout_ns < 0)
3085 args->timeout_ns = 0;
Chris Wilsonc1d20612017-02-16 12:54:41 +00003086
3087 /*
3088 * Apparently ktime isn't accurate enough and occasionally has a
3089 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
3090 * things up to make the test happy. We allow up to 1 jiffy.
3091 *
3092 * This is a regression from the timespec->ktime conversion.
3093 */
3094 if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
3095 args->timeout_ns = 0;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003096 }
3097
Chris Wilsonf0cd5182016-10-28 13:58:43 +01003098 i915_gem_object_put(obj);
John Harrisonff865882014-11-24 18:49:28 +00003099 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003100}
3101
Chris Wilson73cb9702016-10-28 13:58:46 +01003102static int wait_for_timeline(struct i915_gem_timeline *tl, unsigned int flags)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003103{
Chris Wilson73cb9702016-10-28 13:58:46 +01003104 int ret, i;
3105
3106 for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
3107 ret = i915_gem_active_wait(&tl->engine[i].last_request, flags);
3108 if (ret)
3109 return ret;
3110 }
3111
3112 return 0;
3113}
3114
3115int i915_gem_wait_for_idle(struct drm_i915_private *i915, unsigned int flags)
3116{
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003117 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003118
Chris Wilson9caa34a2016-11-11 14:58:08 +00003119 if (flags & I915_WAIT_LOCKED) {
3120 struct i915_gem_timeline *tl;
3121
3122 lockdep_assert_held(&i915->drm.struct_mutex);
3123
3124 list_for_each_entry(tl, &i915->gt.timelines, link) {
3125 ret = wait_for_timeline(tl, flags);
3126 if (ret)
3127 return ret;
3128 }
3129 } else {
3130 ret = wait_for_timeline(&i915->gt.global_timeline, flags);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003131 if (ret)
3132 return ret;
3133 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003134
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01003135 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003136}
3137
Chris Wilsond0da48c2016-11-06 12:59:59 +00003138void i915_gem_clflush_object(struct drm_i915_gem_object *obj,
3139 bool force)
Eric Anholt673a3942008-07-30 12:06:12 -07003140{
Eric Anholt673a3942008-07-30 12:06:12 -07003141 /* If we don't have a page list set up, then we're not pinned
3142 * to GPU, and we can ignore the cache flush because it'll happen
3143 * again at bind time.
3144 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003145 if (!obj->mm.pages)
Chris Wilsond0da48c2016-11-06 12:59:59 +00003146 return;
Eric Anholt673a3942008-07-30 12:06:12 -07003147
Imre Deak769ce462013-02-13 21:56:05 +02003148 /*
3149 * Stolen memory is always coherent with the GPU as it is explicitly
3150 * marked as wc by the system, or the system is cache-coherent.
3151 */
Chris Wilson6a2c4232014-11-04 04:51:40 -08003152 if (obj->stolen || obj->phys_handle)
Chris Wilsond0da48c2016-11-06 12:59:59 +00003153 return;
Imre Deak769ce462013-02-13 21:56:05 +02003154
Chris Wilson9c23f7f2011-03-29 16:59:52 -07003155 /* If the GPU is snooping the contents of the CPU cache,
3156 * we do not need to manually clear the CPU cache lines. However,
3157 * the caches are only snooped when the render cache is
3158 * flushed/invalidated. As we always have to emit invalidations
3159 * and flushes when moving into and out of the RENDER domain, correct
3160 * snooping behaviour occurs naturally as the result of our domain
3161 * tracking.
3162 */
Chris Wilson0f719792015-01-13 13:32:52 +00003163 if (!force && cpu_cache_is_coherent(obj->base.dev, obj->cache_level)) {
3164 obj->cache_dirty = true;
Chris Wilsond0da48c2016-11-06 12:59:59 +00003165 return;
Chris Wilson0f719792015-01-13 13:32:52 +00003166 }
Chris Wilson9c23f7f2011-03-29 16:59:52 -07003167
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003168 trace_i915_gem_object_clflush(obj);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003169 drm_clflush_sg(obj->mm.pages);
Chris Wilson0f719792015-01-13 13:32:52 +00003170 obj->cache_dirty = false;
Eric Anholte47c68e2008-11-14 13:35:19 -08003171}
3172
3173/** Flushes the GTT write domain for the object if it's dirty. */
3174static void
Chris Wilson05394f32010-11-08 19:18:58 +00003175i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003176{
Chris Wilson3b5724d2016-08-18 17:16:49 +01003177 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003178
Chris Wilson05394f32010-11-08 19:18:58 +00003179 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
Eric Anholte47c68e2008-11-14 13:35:19 -08003180 return;
3181
Chris Wilson63256ec2011-01-04 18:42:07 +00003182 /* No actual flushing is required for the GTT write domain. Writes
Chris Wilson3b5724d2016-08-18 17:16:49 +01003183 * to it "immediately" go to main memory as far as we know, so there's
Eric Anholte47c68e2008-11-14 13:35:19 -08003184 * no chipset flush. It also doesn't land in render cache.
Chris Wilson63256ec2011-01-04 18:42:07 +00003185 *
3186 * However, we do have to enforce the order so that all writes through
3187 * the GTT land before any writes to the device, such as updates to
3188 * the GATT itself.
Chris Wilson3b5724d2016-08-18 17:16:49 +01003189 *
3190 * We also have to wait a bit for the writes to land from the GTT.
3191 * An uncached read (i.e. mmio) seems to be ideal for the round-trip
3192 * timing. This issue has only been observed when switching quickly
3193 * between GTT writes and CPU reads from inside the kernel on recent hw,
3194 * and it appears to only affect discrete GTT blocks (i.e. on LLC
3195 * system agents we cannot reproduce this behaviour).
Eric Anholte47c68e2008-11-14 13:35:19 -08003196 */
Chris Wilson63256ec2011-01-04 18:42:07 +00003197 wmb();
Chris Wilson3b5724d2016-08-18 17:16:49 +01003198 if (INTEL_GEN(dev_priv) >= 6 && !HAS_LLC(dev_priv))
Akash Goel3b3f1652016-10-13 22:44:48 +05303199 POSTING_READ(RING_ACTHD(dev_priv->engine[RCS]->mmio_base));
Chris Wilson63256ec2011-01-04 18:42:07 +00003200
Chris Wilsond243ad82016-08-18 17:16:44 +01003201 intel_fb_obj_flush(obj, false, write_origin(obj, I915_GEM_DOMAIN_GTT));
Daniel Vetterf99d7062014-06-19 16:01:59 +02003202
Chris Wilsonb0dc4652016-08-18 17:16:51 +01003203 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003204 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00003205 obj->base.read_domains,
Chris Wilsonb0dc4652016-08-18 17:16:51 +01003206 I915_GEM_DOMAIN_GTT);
Eric Anholte47c68e2008-11-14 13:35:19 -08003207}
3208
3209/** Flushes the CPU write domain for the object if it's dirty. */
3210static void
Daniel Vettere62b59e2015-01-21 14:53:48 +01003211i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003212{
Chris Wilson05394f32010-11-08 19:18:58 +00003213 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
Eric Anholte47c68e2008-11-14 13:35:19 -08003214 return;
3215
Chris Wilsond0da48c2016-11-06 12:59:59 +00003216 i915_gem_clflush_object(obj, obj->pin_display);
Rodrigo Vivide152b62015-07-07 16:28:51 -07003217 intel_fb_obj_flush(obj, false, ORIGIN_CPU);
Daniel Vetterf99d7062014-06-19 16:01:59 +02003218
Chris Wilsonb0dc4652016-08-18 17:16:51 +01003219 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003220 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00003221 obj->base.read_domains,
Chris Wilsonb0dc4652016-08-18 17:16:51 +01003222 I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08003223}
3224
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003225/**
3226 * Moves a single object to the GTT read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003227 * @obj: object to act on
3228 * @write: ask for write access or read only
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003229 *
3230 * This function returns when the move is complete, including waiting on
3231 * flushes to occur.
3232 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003233int
Chris Wilson20217462010-11-23 15:26:33 +00003234i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003235{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003236 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003237 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003238
Chris Wilsone95433c2016-10-28 13:58:27 +01003239 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003240
Chris Wilsone95433c2016-10-28 13:58:27 +01003241 ret = i915_gem_object_wait(obj,
3242 I915_WAIT_INTERRUPTIBLE |
3243 I915_WAIT_LOCKED |
3244 (write ? I915_WAIT_ALL : 0),
3245 MAX_SCHEDULE_TIMEOUT,
3246 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00003247 if (ret)
3248 return ret;
3249
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003250 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
3251 return 0;
3252
Chris Wilson43566de2015-01-02 16:29:29 +05303253 /* Flush and acquire obj->pages so that we are coherent through
3254 * direct access in memory with previous cached writes through
3255 * shmemfs and that our cache domain tracking remains valid.
3256 * For example, if the obj->filp was moved to swap without us
3257 * being notified and releasing the pages, we would mistakenly
3258 * continue to assume that the obj remained out of the CPU cached
3259 * domain.
3260 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003261 ret = i915_gem_object_pin_pages(obj);
Chris Wilson43566de2015-01-02 16:29:29 +05303262 if (ret)
3263 return ret;
3264
Daniel Vettere62b59e2015-01-21 14:53:48 +01003265 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003266
Chris Wilsond0a57782012-10-09 19:24:37 +01003267 /* Serialise direct access to this object with the barriers for
3268 * coherent writes from the GPU, by effectively invalidating the
3269 * GTT domain upon first access.
3270 */
3271 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
3272 mb();
3273
Chris Wilson05394f32010-11-08 19:18:58 +00003274 old_write_domain = obj->base.write_domain;
3275 old_read_domains = obj->base.read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003276
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003277 /* It should now be out of any other write domains, and we can update
3278 * the domain values for our changes.
3279 */
Chris Wilson40e62d52016-10-28 13:58:41 +01003280 GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
Chris Wilson05394f32010-11-08 19:18:58 +00003281 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003282 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003283 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
3284 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003285 obj->mm.dirty = true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003286 }
3287
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003288 trace_i915_gem_object_change_domain(obj,
3289 old_read_domains,
3290 old_write_domain);
3291
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003292 i915_gem_object_unpin_pages(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003293 return 0;
3294}
3295
Chris Wilsonef55f922015-10-09 14:11:27 +01003296/**
3297 * Changes the cache-level of an object across all VMA.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003298 * @obj: object to act on
3299 * @cache_level: new cache level to set for the object
Chris Wilsonef55f922015-10-09 14:11:27 +01003300 *
3301 * After this function returns, the object will be in the new cache-level
3302 * across all GTT and the contents of the backing storage will be coherent,
3303 * with respect to the new cache-level. In order to keep the backing storage
3304 * coherent for all users, we only allow a single cache level to be set
3305 * globally on the object and prevent it from being changed whilst the
3306 * hardware is reading from the object. That is if the object is currently
3307 * on the scanout it will be set to uncached (or equivalent display
3308 * cache coherency) and all non-MOCS GPU access will also be uncached so
3309 * that all direct access to the scanout remains coherent.
3310 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003311int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3312 enum i915_cache_level cache_level)
3313{
Chris Wilsonaa653a62016-08-04 07:52:27 +01003314 struct i915_vma *vma;
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003315 int ret;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003316
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003317 lockdep_assert_held(&obj->base.dev->struct_mutex);
3318
Chris Wilsone4ffd172011-04-04 09:44:39 +01003319 if (obj->cache_level == cache_level)
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003320 return 0;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003321
Chris Wilsonef55f922015-10-09 14:11:27 +01003322 /* Inspect the list of currently bound VMA and unbind any that would
3323 * be invalid given the new cache-level. This is principally to
3324 * catch the issue of the CS prefetch crossing page boundaries and
3325 * reading an invalid PTE on older architectures.
3326 */
Chris Wilsonaa653a62016-08-04 07:52:27 +01003327restart:
3328 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003329 if (!drm_mm_node_allocated(&vma->node))
3330 continue;
3331
Chris Wilson20dfbde2016-08-04 16:32:30 +01003332 if (i915_vma_is_pinned(vma)) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003333 DRM_DEBUG("can not change the cache level of pinned objects\n");
3334 return -EBUSY;
3335 }
3336
Chris Wilsonaa653a62016-08-04 07:52:27 +01003337 if (i915_gem_valid_gtt_space(vma, cache_level))
3338 continue;
3339
3340 ret = i915_vma_unbind(vma);
3341 if (ret)
3342 return ret;
3343
3344 /* As unbinding may affect other elements in the
3345 * obj->vma_list (due to side-effects from retiring
3346 * an active vma), play safe and restart the iterator.
3347 */
3348 goto restart;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003349 }
3350
Chris Wilsonef55f922015-10-09 14:11:27 +01003351 /* We can reuse the existing drm_mm nodes but need to change the
3352 * cache-level on the PTE. We could simply unbind them all and
3353 * rebind with the correct cache-level on next use. However since
3354 * we already have a valid slot, dma mapping, pages etc, we may as
3355 * rewrite the PTE in the belief that doing so tramples upon less
3356 * state and so involves less work.
3357 */
Chris Wilson15717de2016-08-04 07:52:26 +01003358 if (obj->bind_count) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003359 /* Before we change the PTE, the GPU must not be accessing it.
3360 * If we wait upon the object, we know that all the bound
3361 * VMA are no longer active.
3362 */
Chris Wilsone95433c2016-10-28 13:58:27 +01003363 ret = i915_gem_object_wait(obj,
3364 I915_WAIT_INTERRUPTIBLE |
3365 I915_WAIT_LOCKED |
3366 I915_WAIT_ALL,
3367 MAX_SCHEDULE_TIMEOUT,
3368 NULL);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003369 if (ret)
3370 return ret;
3371
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00003372 if (!HAS_LLC(to_i915(obj->base.dev)) &&
3373 cache_level != I915_CACHE_NONE) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003374 /* Access to snoopable pages through the GTT is
3375 * incoherent and on some machines causes a hard
3376 * lockup. Relinquish the CPU mmaping to force
3377 * userspace to refault in the pages and we can
3378 * then double check if the GTT mapping is still
3379 * valid for that pointer access.
3380 */
3381 i915_gem_release_mmap(obj);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003382
Chris Wilsonef55f922015-10-09 14:11:27 +01003383 /* As we no longer need a fence for GTT access,
3384 * we can relinquish it now (and so prevent having
3385 * to steal a fence from someone else on the next
3386 * fence request). Note GPU activity would have
3387 * dropped the fence as all snoopable access is
3388 * supposed to be linear.
3389 */
Chris Wilson49ef5292016-08-18 17:17:00 +01003390 list_for_each_entry(vma, &obj->vma_list, obj_link) {
3391 ret = i915_vma_put_fence(vma);
3392 if (ret)
3393 return ret;
3394 }
Chris Wilsonef55f922015-10-09 14:11:27 +01003395 } else {
3396 /* We either have incoherent backing store and
3397 * so no GTT access or the architecture is fully
3398 * coherent. In such cases, existing GTT mmaps
3399 * ignore the cache bit in the PTE and we can
3400 * rewrite it without confusing the GPU or having
3401 * to force userspace to fault back in its mmaps.
3402 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003403 }
3404
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003405 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003406 if (!drm_mm_node_allocated(&vma->node))
3407 continue;
3408
3409 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
3410 if (ret)
3411 return ret;
3412 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01003413 }
3414
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003415 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU &&
3416 cpu_cache_is_coherent(obj->base.dev, obj->cache_level))
3417 obj->cache_dirty = true;
3418
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003419 list_for_each_entry(vma, &obj->vma_list, obj_link)
Chris Wilson2c225692013-08-09 12:26:45 +01003420 vma->node.color = cache_level;
3421 obj->cache_level = cache_level;
3422
Chris Wilsone4ffd172011-04-04 09:44:39 +01003423 return 0;
3424}
3425
Ben Widawsky199adf42012-09-21 17:01:20 -07003426int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
3427 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003428{
Ben Widawsky199adf42012-09-21 17:01:20 -07003429 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003430 struct drm_i915_gem_object *obj;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003431 int err = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003432
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003433 rcu_read_lock();
3434 obj = i915_gem_object_lookup_rcu(file, args->handle);
3435 if (!obj) {
3436 err = -ENOENT;
3437 goto out;
3438 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01003439
Chris Wilson651d7942013-08-08 14:41:10 +01003440 switch (obj->cache_level) {
3441 case I915_CACHE_LLC:
3442 case I915_CACHE_L3_LLC:
3443 args->caching = I915_CACHING_CACHED;
3444 break;
3445
Chris Wilson4257d3b2013-08-08 14:41:11 +01003446 case I915_CACHE_WT:
3447 args->caching = I915_CACHING_DISPLAY;
3448 break;
3449
Chris Wilson651d7942013-08-08 14:41:10 +01003450 default:
3451 args->caching = I915_CACHING_NONE;
3452 break;
3453 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003454out:
3455 rcu_read_unlock();
3456 return err;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003457}
3458
Ben Widawsky199adf42012-09-21 17:01:20 -07003459int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
3460 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003461{
Chris Wilson9c870d02016-10-24 13:42:15 +01003462 struct drm_i915_private *i915 = to_i915(dev);
Ben Widawsky199adf42012-09-21 17:01:20 -07003463 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003464 struct drm_i915_gem_object *obj;
3465 enum i915_cache_level level;
Chris Wilsond65415d2017-01-19 08:22:10 +00003466 int ret = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003467
Ben Widawsky199adf42012-09-21 17:01:20 -07003468 switch (args->caching) {
3469 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01003470 level = I915_CACHE_NONE;
3471 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07003472 case I915_CACHING_CACHED:
Imre Deake5756c12015-08-14 18:43:30 +03003473 /*
3474 * Due to a HW issue on BXT A stepping, GPU stores via a
3475 * snooped mapping may leave stale data in a corresponding CPU
3476 * cacheline, whereas normally such cachelines would get
3477 * invalidated.
3478 */
Chris Wilson9c870d02016-10-24 13:42:15 +01003479 if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
Imre Deake5756c12015-08-14 18:43:30 +03003480 return -ENODEV;
3481
Chris Wilsone6994ae2012-07-10 10:27:08 +01003482 level = I915_CACHE_LLC;
3483 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003484 case I915_CACHING_DISPLAY:
Chris Wilson9c870d02016-10-24 13:42:15 +01003485 level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003486 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003487 default:
3488 return -EINVAL;
3489 }
3490
Chris Wilsond65415d2017-01-19 08:22:10 +00003491 obj = i915_gem_object_lookup(file, args->handle);
3492 if (!obj)
3493 return -ENOENT;
3494
3495 if (obj->cache_level == level)
3496 goto out;
3497
3498 ret = i915_gem_object_wait(obj,
3499 I915_WAIT_INTERRUPTIBLE,
3500 MAX_SCHEDULE_TIMEOUT,
3501 to_rps_client(file));
3502 if (ret)
3503 goto out;
3504
Ben Widawsky3bc29132012-09-26 16:15:20 -07003505 ret = i915_mutex_lock_interruptible(dev);
3506 if (ret)
Chris Wilsond65415d2017-01-19 08:22:10 +00003507 goto out;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003508
3509 ret = i915_gem_object_set_cache_level(obj, level);
Chris Wilsone6994ae2012-07-10 10:27:08 +01003510 mutex_unlock(&dev->struct_mutex);
Chris Wilsond65415d2017-01-19 08:22:10 +00003511
3512out:
3513 i915_gem_object_put(obj);
Chris Wilsone6994ae2012-07-10 10:27:08 +01003514 return ret;
3515}
3516
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003517/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003518 * Prepare buffer for display plane (scanout, cursors, etc).
3519 * Can be called from an uninterruptible phase (modesetting) and allows
3520 * any flushes to be pipelined (for pageflips).
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003521 */
Chris Wilson058d88c2016-08-15 10:49:06 +01003522struct i915_vma *
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003523i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3524 u32 alignment,
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003525 const struct i915_ggtt_view *view)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003526{
Chris Wilson058d88c2016-08-15 10:49:06 +01003527 struct i915_vma *vma;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003528 u32 old_read_domains, old_write_domain;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003529 int ret;
3530
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003531 lockdep_assert_held(&obj->base.dev->struct_mutex);
3532
Chris Wilsoncc98b412013-08-09 12:25:09 +01003533 /* Mark the pin_display early so that we account for the
3534 * display coherency whilst setting up the cache domains.
3535 */
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003536 obj->pin_display++;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003537
Eric Anholta7ef0642011-03-29 16:59:54 -07003538 /* The display engine is not coherent with the LLC cache on gen6. As
3539 * a result, we make sure that the pinning that is about to occur is
3540 * done with uncached PTEs. This is lowest common denominator for all
3541 * chipsets.
3542 *
3543 * However for gen6+, we could do better by using the GFDT bit instead
3544 * of uncaching, which would allow us to flush all the LLC-cached data
3545 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3546 */
Chris Wilson651d7942013-08-08 14:41:10 +01003547 ret = i915_gem_object_set_cache_level(obj,
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003548 HAS_WT(to_i915(obj->base.dev)) ?
3549 I915_CACHE_WT : I915_CACHE_NONE);
Chris Wilson058d88c2016-08-15 10:49:06 +01003550 if (ret) {
3551 vma = ERR_PTR(ret);
Chris Wilsoncc98b412013-08-09 12:25:09 +01003552 goto err_unpin_display;
Chris Wilson058d88c2016-08-15 10:49:06 +01003553 }
Eric Anholta7ef0642011-03-29 16:59:54 -07003554
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003555 /* As the user may map the buffer once pinned in the display plane
3556 * (e.g. libkms for the bootup splash), we have to ensure that we
Chris Wilson2efb8132016-08-18 17:17:06 +01003557 * always use map_and_fenceable for all scanout buffers. However,
3558 * it may simply be too big to fit into mappable, in which case
3559 * put it anyway and hope that userspace can cope (but always first
3560 * try to preserve the existing ABI).
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003561 */
Chris Wilson2efb8132016-08-18 17:17:06 +01003562 vma = ERR_PTR(-ENOSPC);
Chris Wilson47a8e3f2017-01-14 00:28:27 +00003563 if (!view || view->type == I915_GGTT_VIEW_NORMAL)
Chris Wilson2efb8132016-08-18 17:17:06 +01003564 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
3565 PIN_MAPPABLE | PIN_NONBLOCK);
Chris Wilson767a2222016-11-07 11:01:28 +00003566 if (IS_ERR(vma)) {
3567 struct drm_i915_private *i915 = to_i915(obj->base.dev);
3568 unsigned int flags;
3569
3570 /* Valleyview is definitely limited to scanning out the first
3571 * 512MiB. Lets presume this behaviour was inherited from the
3572 * g4x display engine and that all earlier gen are similarly
3573 * limited. Testing suggests that it is a little more
3574 * complicated than this. For example, Cherryview appears quite
3575 * happy to scanout from anywhere within its global aperture.
3576 */
3577 flags = 0;
3578 if (HAS_GMCH_DISPLAY(i915))
3579 flags = PIN_MAPPABLE;
3580 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags);
3581 }
Chris Wilson058d88c2016-08-15 10:49:06 +01003582 if (IS_ERR(vma))
Chris Wilsoncc98b412013-08-09 12:25:09 +01003583 goto err_unpin_display;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003584
Chris Wilsond8923dc2016-08-18 17:17:07 +01003585 vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
3586
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003587 /* Treat this as an end-of-frame, like intel_user_framebuffer_dirty() */
Chris Wilson69aeafe2017-01-09 11:19:32 +00003588 if (obj->cache_dirty || obj->base.write_domain == I915_GEM_DOMAIN_CPU) {
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003589 i915_gem_clflush_object(obj, true);
3590 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
3591 }
Chris Wilsonb118c1e2010-05-27 13:18:14 +01003592
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003593 old_write_domain = obj->base.write_domain;
Chris Wilson05394f32010-11-08 19:18:58 +00003594 old_read_domains = obj->base.read_domains;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003595
3596 /* It should now be out of any other write domains, and we can update
3597 * the domain values for our changes.
3598 */
Chris Wilsone5f1d962012-07-20 12:41:00 +01003599 obj->base.write_domain = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00003600 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003601
3602 trace_i915_gem_object_change_domain(obj,
3603 old_read_domains,
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003604 old_write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003605
Chris Wilson058d88c2016-08-15 10:49:06 +01003606 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003607
3608err_unpin_display:
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003609 obj->pin_display--;
Chris Wilson058d88c2016-08-15 10:49:06 +01003610 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003611}
3612
3613void
Chris Wilson058d88c2016-08-15 10:49:06 +01003614i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
Chris Wilsoncc98b412013-08-09 12:25:09 +01003615{
Chris Wilson49d73912016-11-29 09:50:08 +00003616 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003617
Chris Wilson058d88c2016-08-15 10:49:06 +01003618 if (WARN_ON(vma->obj->pin_display == 0))
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003619 return;
3620
Chris Wilsond8923dc2016-08-18 17:17:07 +01003621 if (--vma->obj->pin_display == 0)
Chris Wilsonf51455d2017-01-10 14:47:34 +00003622 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003623
Chris Wilson383d5822016-08-18 17:17:08 +01003624 /* Bump the LRU to try and avoid premature eviction whilst flipping */
Chris Wilsonbefedbb2017-01-19 19:26:55 +00003625 i915_gem_object_bump_inactive_ggtt(vma->obj);
Chris Wilson383d5822016-08-18 17:17:08 +01003626
Chris Wilson058d88c2016-08-15 10:49:06 +01003627 i915_vma_unpin(vma);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003628}
3629
Eric Anholte47c68e2008-11-14 13:35:19 -08003630/**
3631 * Moves a single object to the CPU read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003632 * @obj: object to act on
3633 * @write: requesting write or read-only access
Eric Anholte47c68e2008-11-14 13:35:19 -08003634 *
3635 * This function returns when the move is complete, including waiting on
3636 * flushes to occur.
3637 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02003638int
Chris Wilson919926a2010-11-12 13:42:53 +00003639i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08003640{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003641 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003642 int ret;
3643
Chris Wilsone95433c2016-10-28 13:58:27 +01003644 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003645
Chris Wilsone95433c2016-10-28 13:58:27 +01003646 ret = i915_gem_object_wait(obj,
3647 I915_WAIT_INTERRUPTIBLE |
3648 I915_WAIT_LOCKED |
3649 (write ? I915_WAIT_ALL : 0),
3650 MAX_SCHEDULE_TIMEOUT,
3651 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00003652 if (ret)
3653 return ret;
3654
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003655 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
3656 return 0;
3657
Eric Anholte47c68e2008-11-14 13:35:19 -08003658 i915_gem_object_flush_gtt_write_domain(obj);
3659
Chris Wilson05394f32010-11-08 19:18:58 +00003660 old_write_domain = obj->base.write_domain;
3661 old_read_domains = obj->base.read_domains;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003662
Eric Anholte47c68e2008-11-14 13:35:19 -08003663 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00003664 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson2c225692013-08-09 12:26:45 +01003665 i915_gem_clflush_object(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003666
Chris Wilson05394f32010-11-08 19:18:58 +00003667 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003668 }
3669
3670 /* It should now be out of any other write domains, and we can update
3671 * the domain values for our changes.
3672 */
Chris Wilson40e62d52016-10-28 13:58:41 +01003673 GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003674
3675 /* If we're writing through the CPU, then the GPU read domains will
3676 * need to be invalidated at next use.
3677 */
3678 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003679 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3680 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003681 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003682
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003683 trace_i915_gem_object_change_domain(obj,
3684 old_read_domains,
3685 old_write_domain);
3686
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003687 return 0;
3688}
3689
Eric Anholt673a3942008-07-30 12:06:12 -07003690/* Throttle our rendering by waiting until the ring has completed our requests
3691 * emitted over 20 msec ago.
3692 *
Eric Anholtb9624422009-06-03 07:27:35 +00003693 * Note that if we were to use the current jiffies each time around the loop,
3694 * we wouldn't escape the function with any frames outstanding if the time to
3695 * render a frame was over 20ms.
3696 *
Eric Anholt673a3942008-07-30 12:06:12 -07003697 * This should get us reasonable parallelism between CPU and GPU but also
3698 * relatively low latency when blocking on a particular request to finish.
3699 */
3700static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003701i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003702{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003703 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003704 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsond0bc54f2015-05-21 21:01:48 +01003705 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
John Harrison54fb2412014-11-24 18:49:27 +00003706 struct drm_i915_gem_request *request, *target = NULL;
Chris Wilsone95433c2016-10-28 13:58:27 +01003707 long ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003708
Chris Wilsonf4457ae2016-04-13 17:35:08 +01003709 /* ABI: return -EIO if already wedged */
3710 if (i915_terminally_wedged(&dev_priv->gpu_error))
3711 return -EIO;
Chris Wilsone110e8d2011-01-26 15:39:14 +00003712
Chris Wilson1c255952010-09-26 11:03:27 +01003713 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003714 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003715 if (time_after_eq(request->emitted_jiffies, recent_enough))
3716 break;
3717
John Harrisonfcfa423c2015-05-29 17:44:12 +01003718 /*
3719 * Note that the request might not have been submitted yet.
3720 * In which case emitted_jiffies will be zero.
3721 */
3722 if (!request->emitted_jiffies)
3723 continue;
3724
John Harrison54fb2412014-11-24 18:49:27 +00003725 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00003726 }
John Harrisonff865882014-11-24 18:49:28 +00003727 if (target)
Chris Wilsone8a261e2016-07-20 13:31:49 +01003728 i915_gem_request_get(target);
Chris Wilson1c255952010-09-26 11:03:27 +01003729 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003730
John Harrison54fb2412014-11-24 18:49:27 +00003731 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003732 return 0;
3733
Chris Wilsone95433c2016-10-28 13:58:27 +01003734 ret = i915_wait_request(target,
3735 I915_WAIT_INTERRUPTIBLE,
3736 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone8a261e2016-07-20 13:31:49 +01003737 i915_gem_request_put(target);
John Harrisonff865882014-11-24 18:49:28 +00003738
Chris Wilsone95433c2016-10-28 13:58:27 +01003739 return ret < 0 ? ret : 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003740}
3741
Chris Wilson058d88c2016-08-15 10:49:06 +01003742struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003743i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
3744 const struct i915_ggtt_view *view,
Chris Wilson91b2db62016-08-04 16:32:23 +01003745 u64 size,
Chris Wilson2ffffd02016-08-04 16:32:22 +01003746 u64 alignment,
3747 u64 flags)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003748{
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003749 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
3750 struct i915_address_space *vm = &dev_priv->ggtt.base;
Chris Wilson59bfa122016-08-04 16:32:31 +01003751 struct i915_vma *vma;
3752 int ret;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003753
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003754 lockdep_assert_held(&obj->base.dev->struct_mutex);
3755
Chris Wilson718659a2017-01-16 15:21:28 +00003756 vma = i915_vma_instance(obj, vm, view);
Chris Wilsone0216b72017-01-19 19:26:57 +00003757 if (unlikely(IS_ERR(vma)))
Chris Wilson058d88c2016-08-15 10:49:06 +01003758 return vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01003759
3760 if (i915_vma_misplaced(vma, size, alignment, flags)) {
3761 if (flags & PIN_NONBLOCK &&
3762 (i915_vma_is_pinned(vma) || i915_vma_is_active(vma)))
Chris Wilson058d88c2016-08-15 10:49:06 +01003763 return ERR_PTR(-ENOSPC);
Chris Wilson59bfa122016-08-04 16:32:31 +01003764
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003765 if (flags & PIN_MAPPABLE) {
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003766 /* If the required space is larger than the available
3767 * aperture, we will not able to find a slot for the
3768 * object and unbinding the object now will be in
3769 * vain. Worse, doing so may cause us to ping-pong
3770 * the object in and out of the Global GTT and
3771 * waste a lot of cycles under the mutex.
3772 */
Chris Wilson944397f2017-01-09 16:16:11 +00003773 if (vma->fence_size > dev_priv->ggtt.mappable_end)
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003774 return ERR_PTR(-E2BIG);
3775
3776 /* If NONBLOCK is set the caller is optimistically
3777 * trying to cache the full object within the mappable
3778 * aperture, and *must* have a fallback in place for
3779 * situations where we cannot bind the object. We
3780 * can be a little more lax here and use the fallback
3781 * more often to avoid costly migrations of ourselves
3782 * and other objects within the aperture.
3783 *
3784 * Half-the-aperture is used as a simple heuristic.
3785 * More interesting would to do search for a free
3786 * block prior to making the commitment to unbind.
3787 * That caters for the self-harm case, and with a
3788 * little more heuristics (e.g. NOFAULT, NOEVICT)
3789 * we could try to minimise harm to others.
3790 */
3791 if (flags & PIN_NONBLOCK &&
Chris Wilson944397f2017-01-09 16:16:11 +00003792 vma->fence_size > dev_priv->ggtt.mappable_end / 2)
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003793 return ERR_PTR(-ENOSPC);
3794 }
3795
Chris Wilson59bfa122016-08-04 16:32:31 +01003796 WARN(i915_vma_is_pinned(vma),
3797 "bo is already pinned in ggtt with incorrect alignment:"
Chris Wilson05a20d02016-08-18 17:16:55 +01003798 " offset=%08x, req.alignment=%llx,"
3799 " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
3800 i915_ggtt_offset(vma), alignment,
Chris Wilson59bfa122016-08-04 16:32:31 +01003801 !!(flags & PIN_MAPPABLE),
Chris Wilson05a20d02016-08-18 17:16:55 +01003802 i915_vma_is_map_and_fenceable(vma));
Chris Wilson59bfa122016-08-04 16:32:31 +01003803 ret = i915_vma_unbind(vma);
3804 if (ret)
Chris Wilson058d88c2016-08-15 10:49:06 +01003805 return ERR_PTR(ret);
Chris Wilson59bfa122016-08-04 16:32:31 +01003806 }
3807
Chris Wilson058d88c2016-08-15 10:49:06 +01003808 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
3809 if (ret)
3810 return ERR_PTR(ret);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003811
Chris Wilson058d88c2016-08-15 10:49:06 +01003812 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07003813}
3814
Chris Wilsonedf6b762016-08-09 09:23:33 +01003815static __always_inline unsigned int __busy_read_flag(unsigned int id)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003816{
3817 /* Note that we could alias engines in the execbuf API, but
3818 * that would be very unwise as it prevents userspace from
3819 * fine control over engine selection. Ahem.
3820 *
3821 * This should be something like EXEC_MAX_ENGINE instead of
3822 * I915_NUM_ENGINES.
3823 */
3824 BUILD_BUG_ON(I915_NUM_ENGINES > 16);
3825 return 0x10000 << id;
3826}
3827
3828static __always_inline unsigned int __busy_write_id(unsigned int id)
3829{
Chris Wilson70cb4722016-08-09 18:08:25 +01003830 /* The uABI guarantees an active writer is also amongst the read
3831 * engines. This would be true if we accessed the activity tracking
3832 * under the lock, but as we perform the lookup of the object and
3833 * its activity locklessly we can not guarantee that the last_write
3834 * being active implies that we have set the same engine flag from
3835 * last_read - hence we always set both read and write busy for
3836 * last_write.
3837 */
3838 return id | __busy_read_flag(id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003839}
3840
Chris Wilsonedf6b762016-08-09 09:23:33 +01003841static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01003842__busy_set_if_active(const struct dma_fence *fence,
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003843 unsigned int (*flag)(unsigned int id))
3844{
Chris Wilsond07f0e52016-10-28 13:58:44 +01003845 struct drm_i915_gem_request *rq;
Chris Wilson12555012016-08-16 09:50:40 +01003846
Chris Wilsond07f0e52016-10-28 13:58:44 +01003847 /* We have to check the current hw status of the fence as the uABI
3848 * guarantees forward progress. We could rely on the idle worker
3849 * to eventually flush us, but to minimise latency just ask the
3850 * hardware.
3851 *
3852 * Note we only report on the status of native fences.
3853 */
3854 if (!dma_fence_is_i915(fence))
Chris Wilson12555012016-08-16 09:50:40 +01003855 return 0;
3856
Chris Wilsond07f0e52016-10-28 13:58:44 +01003857 /* opencode to_request() in order to avoid const warnings */
3858 rq = container_of(fence, struct drm_i915_gem_request, fence);
3859 if (i915_gem_request_completed(rq))
3860 return 0;
3861
3862 return flag(rq->engine->exec_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003863}
3864
Chris Wilsonedf6b762016-08-09 09:23:33 +01003865static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01003866busy_check_reader(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003867{
Chris Wilsond07f0e52016-10-28 13:58:44 +01003868 return __busy_set_if_active(fence, __busy_read_flag);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003869}
3870
Chris Wilsonedf6b762016-08-09 09:23:33 +01003871static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01003872busy_check_writer(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003873{
Chris Wilsond07f0e52016-10-28 13:58:44 +01003874 if (!fence)
3875 return 0;
3876
3877 return __busy_set_if_active(fence, __busy_write_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003878}
3879
Eric Anholt673a3942008-07-30 12:06:12 -07003880int
Eric Anholt673a3942008-07-30 12:06:12 -07003881i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003882 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003883{
3884 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003885 struct drm_i915_gem_object *obj;
Chris Wilsond07f0e52016-10-28 13:58:44 +01003886 struct reservation_object_list *list;
3887 unsigned int seq;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003888 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07003889
Chris Wilsond07f0e52016-10-28 13:58:44 +01003890 err = -ENOENT;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003891 rcu_read_lock();
3892 obj = i915_gem_object_lookup_rcu(file, args->handle);
Chris Wilsond07f0e52016-10-28 13:58:44 +01003893 if (!obj)
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003894 goto out;
Chris Wilsond07f0e52016-10-28 13:58:44 +01003895
3896 /* A discrepancy here is that we do not report the status of
3897 * non-i915 fences, i.e. even though we may report the object as idle,
3898 * a call to set-domain may still stall waiting for foreign rendering.
3899 * This also means that wait-ioctl may report an object as busy,
3900 * where busy-ioctl considers it idle.
3901 *
3902 * We trade the ability to warn of foreign fences to report on which
3903 * i915 engines are active for the object.
3904 *
3905 * Alternatively, we can trade that extra information on read/write
3906 * activity with
3907 * args->busy =
3908 * !reservation_object_test_signaled_rcu(obj->resv, true);
3909 * to report the overall busyness. This is what the wait-ioctl does.
3910 *
3911 */
3912retry:
3913 seq = raw_read_seqcount(&obj->resv->seq);
3914
3915 /* Translate the exclusive fence to the READ *and* WRITE engine */
3916 args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl));
3917
3918 /* Translate shared fences to READ set of engines */
3919 list = rcu_dereference(obj->resv->fence);
3920 if (list) {
3921 unsigned int shared_count = list->shared_count, i;
3922
3923 for (i = 0; i < shared_count; ++i) {
3924 struct dma_fence *fence =
3925 rcu_dereference(list->shared[i]);
3926
3927 args->busy |= busy_check_reader(fence);
3928 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003929 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003930
Chris Wilsond07f0e52016-10-28 13:58:44 +01003931 if (args->busy && read_seqcount_retry(&obj->resv->seq, seq))
3932 goto retry;
Chris Wilson426960b2016-01-15 16:51:46 +00003933
Chris Wilsond07f0e52016-10-28 13:58:44 +01003934 err = 0;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003935out:
3936 rcu_read_unlock();
3937 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07003938}
3939
3940int
3941i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3942 struct drm_file *file_priv)
3943{
Akshay Joshi0206e352011-08-16 15:34:10 -04003944 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07003945}
3946
Chris Wilson3ef94da2009-09-14 16:50:29 +01003947int
3948i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
3949 struct drm_file *file_priv)
3950{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003951 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson3ef94da2009-09-14 16:50:29 +01003952 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003953 struct drm_i915_gem_object *obj;
Chris Wilson1233e2d2016-10-28 13:58:37 +01003954 int err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003955
3956 switch (args->madv) {
3957 case I915_MADV_DONTNEED:
3958 case I915_MADV_WILLNEED:
3959 break;
3960 default:
3961 return -EINVAL;
3962 }
3963
Chris Wilson03ac0642016-07-20 13:31:51 +01003964 obj = i915_gem_object_lookup(file_priv, args->handle);
Chris Wilson1233e2d2016-10-28 13:58:37 +01003965 if (!obj)
3966 return -ENOENT;
3967
3968 err = mutex_lock_interruptible(&obj->mm.lock);
3969 if (err)
3970 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003971
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003972 if (obj->mm.pages &&
Chris Wilson3e510a82016-08-05 10:14:23 +01003973 i915_gem_object_is_tiled(obj) &&
Daniel Vetter656bfa32014-11-20 09:26:30 +01003974 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilsonbc0629a2016-11-01 10:03:17 +00003975 if (obj->mm.madv == I915_MADV_WILLNEED) {
3976 GEM_BUG_ON(!obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003977 __i915_gem_object_unpin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00003978 obj->mm.quirked = false;
3979 }
3980 if (args->madv == I915_MADV_WILLNEED) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00003981 GEM_BUG_ON(obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003982 __i915_gem_object_pin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00003983 obj->mm.quirked = true;
3984 }
Daniel Vetter656bfa32014-11-20 09:26:30 +01003985 }
3986
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003987 if (obj->mm.madv != __I915_MADV_PURGED)
3988 obj->mm.madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003989
Chris Wilson6c085a72012-08-20 11:40:46 +02003990 /* if the object is no longer attached, discard its backing storage */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003991 if (obj->mm.madv == I915_MADV_DONTNEED && !obj->mm.pages)
Chris Wilson2d7ef392009-09-20 23:13:10 +01003992 i915_gem_object_truncate(obj);
3993
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003994 args->retained = obj->mm.madv != __I915_MADV_PURGED;
Chris Wilson1233e2d2016-10-28 13:58:37 +01003995 mutex_unlock(&obj->mm.lock);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003996
Chris Wilson1233e2d2016-10-28 13:58:37 +01003997out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01003998 i915_gem_object_put(obj);
Chris Wilson1233e2d2016-10-28 13:58:37 +01003999 return err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004000}
4001
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004002static void
4003frontbuffer_retire(struct i915_gem_active *active,
4004 struct drm_i915_gem_request *request)
4005{
4006 struct drm_i915_gem_object *obj =
4007 container_of(active, typeof(*obj), frontbuffer_write);
4008
4009 intel_fb_obj_flush(obj, true, ORIGIN_CS);
4010}
4011
Chris Wilson37e680a2012-06-07 15:38:42 +01004012void i915_gem_object_init(struct drm_i915_gem_object *obj,
4013 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01004014{
Chris Wilson1233e2d2016-10-28 13:58:37 +01004015 mutex_init(&obj->mm.lock);
4016
Joonas Lahtinen56cea322016-11-02 12:16:04 +02004017 INIT_LIST_HEAD(&obj->global_link);
Chris Wilson275f0392016-10-24 13:42:14 +01004018 INIT_LIST_HEAD(&obj->userfault_link);
Ben Widawskyb25cb2f2013-08-14 11:38:33 +02004019 INIT_LIST_HEAD(&obj->obj_exec_link);
Ben Widawsky2f633152013-07-17 12:19:03 -07004020 INIT_LIST_HEAD(&obj->vma_list);
Chris Wilson8d9d5742015-04-07 16:20:38 +01004021 INIT_LIST_HEAD(&obj->batch_pool_link);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004022
Chris Wilson37e680a2012-06-07 15:38:42 +01004023 obj->ops = ops;
4024
Chris Wilsond07f0e52016-10-28 13:58:44 +01004025 reservation_object_init(&obj->__builtin_resv);
4026 obj->resv = &obj->__builtin_resv;
4027
Chris Wilson50349242016-08-18 17:17:04 +01004028 obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004029 init_request_active(&obj->frontbuffer_write, frontbuffer_retire);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004030
4031 obj->mm.madv = I915_MADV_WILLNEED;
4032 INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
4033 mutex_init(&obj->mm.get_page.lock);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004034
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004035 i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004036}
4037
Chris Wilson37e680a2012-06-07 15:38:42 +01004038static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
Tvrtko Ursulin3599a912016-11-01 14:44:10 +00004039 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
4040 I915_GEM_OBJECT_IS_SHRINKABLE,
Chris Wilson37e680a2012-06-07 15:38:42 +01004041 .get_pages = i915_gem_object_get_pages_gtt,
4042 .put_pages = i915_gem_object_put_pages_gtt,
4043};
4044
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004045struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004046i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004047{
Daniel Vetterc397b902010-04-09 19:05:07 +00004048 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004049 struct address_space *mapping;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004050 gfp_t mask;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004051 int ret;
Daniel Vetterc397b902010-04-09 19:05:07 +00004052
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004053 /* There is a prevalence of the assumption that we fit the object's
4054 * page count inside a 32bit _signed_ variable. Let's document this and
4055 * catch if we ever need to fix it. In the meantime, if you do spot
4056 * such a local variable, please consider fixing!
4057 */
4058 if (WARN_ON(size >> PAGE_SHIFT > INT_MAX))
4059 return ERR_PTR(-E2BIG);
4060
4061 if (overflows_type(size, obj->base.size))
4062 return ERR_PTR(-E2BIG);
4063
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00004064 obj = i915_gem_object_alloc(dev_priv);
Daniel Vetterc397b902010-04-09 19:05:07 +00004065 if (obj == NULL)
Chris Wilsonfe3db792016-04-25 13:32:13 +01004066 return ERR_PTR(-ENOMEM);
Daniel Vetterc397b902010-04-09 19:05:07 +00004067
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004068 ret = drm_gem_object_init(&dev_priv->drm, &obj->base, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004069 if (ret)
4070 goto fail;
Daniel Vetterc397b902010-04-09 19:05:07 +00004071
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004072 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
Jani Nikulac0f86832016-12-07 12:13:04 +02004073 if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) {
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004074 /* 965gm cannot relocate objects above 4GiB. */
4075 mask &= ~__GFP_HIGHMEM;
4076 mask |= __GFP_DMA32;
4077 }
4078
Al Viro93c76a32015-12-04 23:45:44 -05004079 mapping = obj->base.filp->f_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004080 mapping_set_gfp_mask(mapping, mask);
Hugh Dickins5949eac2011-06-27 16:18:18 -07004081
Chris Wilson37e680a2012-06-07 15:38:42 +01004082 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004083
Daniel Vetterc397b902010-04-09 19:05:07 +00004084 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4085 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4086
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00004087 if (HAS_LLC(dev_priv)) {
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004088 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004089 * cache) for about a 10% performance improvement
4090 * compared to uncached. Graphics requests other than
4091 * display scanout are coherent with the CPU in
4092 * accessing this cache. This means in this mode we
4093 * don't need to clflush on the CPU side, and on the
4094 * GPU side we only need to flush internal caches to
4095 * get data visible to the CPU.
4096 *
4097 * However, we maintain the display planes as UC, and so
4098 * need to rebind when first used as such.
4099 */
4100 obj->cache_level = I915_CACHE_LLC;
4101 } else
4102 obj->cache_level = I915_CACHE_NONE;
4103
Daniel Vetterd861e332013-07-24 23:25:03 +02004104 trace_i915_gem_object_create(obj);
4105
Chris Wilson05394f32010-11-08 19:18:58 +00004106 return obj;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004107
4108fail:
4109 i915_gem_object_free(obj);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004110 return ERR_PTR(ret);
Daniel Vetterac52bc52010-04-09 19:05:06 +00004111}
4112
Chris Wilson340fbd82014-05-22 09:16:52 +01004113static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4114{
4115 /* If we are the last user of the backing storage (be it shmemfs
4116 * pages or stolen etc), we know that the pages are going to be
4117 * immediately released. In this case, we can then skip copying
4118 * back the contents from the GPU.
4119 */
4120
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004121 if (obj->mm.madv != I915_MADV_WILLNEED)
Chris Wilson340fbd82014-05-22 09:16:52 +01004122 return false;
4123
4124 if (obj->base.filp == NULL)
4125 return true;
4126
4127 /* At first glance, this looks racy, but then again so would be
4128 * userspace racing mmap against close. However, the first external
4129 * reference to the filp can only be obtained through the
4130 * i915_gem_mmap_ioctl() which safeguards us against the user
4131 * acquiring such a reference whilst we are in the middle of
4132 * freeing the object.
4133 */
4134 return atomic_long_read(&obj->base.filp->f_count) == 1;
4135}
4136
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004137static void __i915_gem_free_objects(struct drm_i915_private *i915,
4138 struct llist_node *freed)
Chris Wilsonbe726152010-07-23 23:18:50 +01004139{
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004140 struct drm_i915_gem_object *obj, *on;
Chris Wilsonbe726152010-07-23 23:18:50 +01004141
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004142 mutex_lock(&i915->drm.struct_mutex);
4143 intel_runtime_pm_get(i915);
4144 llist_for_each_entry(obj, freed, freed) {
4145 struct i915_vma *vma, *vn;
Paulo Zanonif65c9162013-11-27 18:20:34 -02004146
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004147 trace_i915_gem_object_destroy(obj);
4148
4149 GEM_BUG_ON(i915_gem_object_is_active(obj));
4150 list_for_each_entry_safe(vma, vn,
4151 &obj->vma_list, obj_link) {
4152 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
4153 GEM_BUG_ON(i915_vma_is_active(vma));
4154 vma->flags &= ~I915_VMA_PIN_MASK;
4155 i915_vma_close(vma);
4156 }
Chris Wilsondb6c2b42016-11-01 11:54:00 +00004157 GEM_BUG_ON(!list_empty(&obj->vma_list));
4158 GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004159
Joonas Lahtinen56cea322016-11-02 12:16:04 +02004160 list_del(&obj->global_link);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004161 }
4162 intel_runtime_pm_put(i915);
4163 mutex_unlock(&i915->drm.struct_mutex);
4164
4165 llist_for_each_entry_safe(obj, on, freed, freed) {
4166 GEM_BUG_ON(obj->bind_count);
4167 GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits));
4168
4169 if (obj->ops->release)
4170 obj->ops->release(obj);
4171
4172 if (WARN_ON(i915_gem_object_has_pinned_pages(obj)))
4173 atomic_set(&obj->mm.pages_pin_count, 0);
Chris Wilson548625e2016-11-01 12:11:34 +00004174 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004175 GEM_BUG_ON(obj->mm.pages);
4176
4177 if (obj->base.import_attach)
4178 drm_prime_gem_destroy(&obj->base, NULL);
4179
Chris Wilsond07f0e52016-10-28 13:58:44 +01004180 reservation_object_fini(&obj->__builtin_resv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004181 drm_gem_object_release(&obj->base);
4182 i915_gem_info_remove_obj(i915, obj->base.size);
4183
4184 kfree(obj->bit_17);
4185 i915_gem_object_free(obj);
4186 }
4187}
4188
4189static void i915_gem_flush_free_objects(struct drm_i915_private *i915)
4190{
4191 struct llist_node *freed;
4192
4193 freed = llist_del_all(&i915->mm.free_list);
4194 if (unlikely(freed))
4195 __i915_gem_free_objects(i915, freed);
4196}
4197
4198static void __i915_gem_free_work(struct work_struct *work)
4199{
4200 struct drm_i915_private *i915 =
4201 container_of(work, struct drm_i915_private, mm.free_work);
4202 struct llist_node *freed;
Chris Wilson26e12f82011-03-20 11:20:19 +00004203
Chris Wilsonb1f788c2016-08-04 07:52:45 +01004204 /* All file-owned VMA should have been released by this point through
4205 * i915_gem_close_object(), or earlier by i915_gem_context_close().
4206 * However, the object may also be bound into the global GTT (e.g.
4207 * older GPUs without per-process support, or for direct access through
4208 * the GTT either for the user or for scanout). Those VMA still need to
4209 * unbound now.
4210 */
Chris Wilson1488fc02012-04-24 15:47:31 +01004211
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004212 while ((freed = llist_del_all(&i915->mm.free_list)))
4213 __i915_gem_free_objects(i915, freed);
4214}
4215
4216static void __i915_gem_free_object_rcu(struct rcu_head *head)
4217{
4218 struct drm_i915_gem_object *obj =
4219 container_of(head, typeof(*obj), rcu);
4220 struct drm_i915_private *i915 = to_i915(obj->base.dev);
4221
4222 /* We can't simply use call_rcu() from i915_gem_free_object()
4223 * as we need to block whilst unbinding, and the call_rcu
4224 * task may be called from softirq context. So we take a
4225 * detour through a worker.
4226 */
4227 if (llist_add(&obj->freed, &i915->mm.free_list))
4228 schedule_work(&i915->mm.free_work);
4229}
4230
4231void i915_gem_free_object(struct drm_gem_object *gem_obj)
4232{
4233 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
4234
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004235 if (obj->mm.quirked)
4236 __i915_gem_object_unpin_pages(obj);
4237
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004238 if (discard_backing_storage(obj))
4239 obj->mm.madv = I915_MADV_DONTNEED;
Daniel Vettera071fa02014-06-18 23:28:09 +02004240
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004241 /* Before we free the object, make sure any pure RCU-only
4242 * read-side critical sections are complete, e.g.
4243 * i915_gem_busy_ioctl(). For the corresponding synchronized
4244 * lookup see i915_gem_object_lookup_rcu().
4245 */
4246 call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
Chris Wilsonbe726152010-07-23 23:18:50 +01004247}
4248
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004249void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj)
4250{
4251 lockdep_assert_held(&obj->base.dev->struct_mutex);
4252
4253 GEM_BUG_ON(i915_gem_object_has_active_reference(obj));
4254 if (i915_gem_object_is_active(obj))
4255 i915_gem_object_set_active_reference(obj);
4256 else
4257 i915_gem_object_put(obj);
4258}
4259
Chris Wilson3033aca2016-10-28 13:58:47 +01004260static void assert_kernel_context_is_current(struct drm_i915_private *dev_priv)
4261{
4262 struct intel_engine_cs *engine;
4263 enum intel_engine_id id;
4264
4265 for_each_engine(engine, dev_priv, id)
Chris Wilsonf131e352016-12-29 14:40:37 +00004266 GEM_BUG_ON(engine->last_retired_context &&
4267 !i915_gem_context_is_kernel(engine->last_retired_context));
Chris Wilson3033aca2016-10-28 13:58:47 +01004268}
4269
Chris Wilson24145512017-01-24 11:01:35 +00004270void i915_gem_sanitize(struct drm_i915_private *i915)
4271{
4272 /*
4273 * If we inherit context state from the BIOS or earlier occupants
4274 * of the GPU, the GPU may be in an inconsistent state when we
4275 * try to take over. The only way to remove the earlier state
4276 * is by resetting. However, resetting on earlier gen is tricky as
4277 * it may impact the display and we are uncertain about the stability
4278 * of the reset, so we only reset recent machines with logical
4279 * context support (that must be reset to remove any stray contexts).
4280 */
4281 if (HAS_HW_CONTEXTS(i915)) {
4282 int reset = intel_gpu_reset(i915, ALL_ENGINES);
4283 WARN_ON(reset && reset != -ENODEV);
4284 }
4285}
4286
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004287int i915_gem_suspend(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07004288{
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004289 struct drm_device *dev = &dev_priv->drm;
Chris Wilsondcff85c2016-08-05 10:14:11 +01004290 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004291
Chris Wilson54b4f682016-07-21 21:16:19 +01004292 intel_suspend_gt_powersave(dev_priv);
4293
Chris Wilson45c5f202013-10-16 11:50:01 +01004294 mutex_lock(&dev->struct_mutex);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004295
4296 /* We have to flush all the executing contexts to main memory so
4297 * that they can saved in the hibernation image. To ensure the last
4298 * context image is coherent, we have to switch away from it. That
4299 * leaves the dev_priv->kernel_context still active when
4300 * we actually suspend, and its image in memory may not match the GPU
4301 * state. Fortunately, the kernel_context is disposable and we do
4302 * not rely on its state.
4303 */
4304 ret = i915_gem_switch_to_kernel_context(dev_priv);
4305 if (ret)
4306 goto err;
4307
Chris Wilson22dd3bb2016-09-09 14:11:50 +01004308 ret = i915_gem_wait_for_idle(dev_priv,
4309 I915_WAIT_INTERRUPTIBLE |
4310 I915_WAIT_LOCKED);
Chris Wilsonf7403342013-09-13 23:57:04 +01004311 if (ret)
Chris Wilson45c5f202013-10-16 11:50:01 +01004312 goto err;
Chris Wilsonf7403342013-09-13 23:57:04 +01004313
Chris Wilsonc0336662016-05-06 15:40:21 +01004314 i915_gem_retire_requests(dev_priv);
Chris Wilson28176ef2016-10-28 13:58:56 +01004315 GEM_BUG_ON(dev_priv->gt.active_requests);
Eric Anholt673a3942008-07-30 12:06:12 -07004316
Chris Wilson3033aca2016-10-28 13:58:47 +01004317 assert_kernel_context_is_current(dev_priv);
Chris Wilsonb2e862d2016-04-28 09:56:41 +01004318 i915_gem_context_lost(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01004319 mutex_unlock(&dev->struct_mutex);
4320
Chris Wilson737b1502015-01-26 18:03:03 +02004321 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
Chris Wilson67d97da2016-07-04 08:08:31 +01004322 cancel_delayed_work_sync(&dev_priv->gt.retire_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004323
4324 /* As the idle_work is rearming if it detects a race, play safe and
4325 * repeat the flush until it is definitely idle.
4326 */
4327 while (flush_delayed_work(&dev_priv->gt.idle_work))
4328 ;
4329
4330 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson29105cc2010-01-07 10:39:13 +00004331
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004332 /* Assert that we sucessfully flushed all the work and
4333 * reset the GPU back to its idle, low power state.
4334 */
Chris Wilson67d97da2016-07-04 08:08:31 +01004335 WARN_ON(dev_priv->gt.awake);
Imre Deak31ab49a2016-11-07 11:20:05 +02004336 WARN_ON(!intel_execlists_idle(dev_priv));
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004337
Imre Deak1c777c52016-10-12 17:46:37 +03004338 /*
4339 * Neither the BIOS, ourselves or any other kernel
4340 * expects the system to be in execlists mode on startup,
4341 * so we need to reset the GPU back to legacy mode. And the only
4342 * known way to disable logical contexts is through a GPU reset.
4343 *
4344 * So in order to leave the system in a known default configuration,
4345 * always reset the GPU upon unload and suspend. Afterwards we then
4346 * clean up the GEM state tracking, flushing off the requests and
4347 * leaving the system in a known idle state.
4348 *
4349 * Note that is of the upmost importance that the GPU is idle and
4350 * all stray writes are flushed *before* we dismantle the backing
4351 * storage for the pinned objects.
4352 *
4353 * However, since we are uncertain that resetting the GPU on older
4354 * machines is a good idea, we don't - just in case it leaves the
4355 * machine in an unusable condition.
4356 */
Chris Wilson24145512017-01-24 11:01:35 +00004357 i915_gem_sanitize(dev_priv);
Imre Deak1c777c52016-10-12 17:46:37 +03004358
Eric Anholt673a3942008-07-30 12:06:12 -07004359 return 0;
Chris Wilson45c5f202013-10-16 11:50:01 +01004360
4361err:
4362 mutex_unlock(&dev->struct_mutex);
4363 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004364}
4365
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004366void i915_gem_resume(struct drm_i915_private *dev_priv)
Chris Wilson5ab57c72016-07-15 14:56:20 +01004367{
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004368 struct drm_device *dev = &dev_priv->drm;
Chris Wilson5ab57c72016-07-15 14:56:20 +01004369
Imre Deak31ab49a2016-11-07 11:20:05 +02004370 WARN_ON(dev_priv->gt.awake);
4371
Chris Wilson5ab57c72016-07-15 14:56:20 +01004372 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00004373 i915_gem_restore_gtt_mappings(dev_priv);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004374
4375 /* As we didn't flush the kernel context before suspend, we cannot
4376 * guarantee that the context image is complete. So let's just reset
4377 * it and start again.
4378 */
Chris Wilson821ed7d2016-09-09 14:11:53 +01004379 dev_priv->gt.resume(dev_priv);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004380
4381 mutex_unlock(&dev->struct_mutex);
4382}
4383
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004384void i915_gem_init_swizzling(struct drm_i915_private *dev_priv)
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004385{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004386 if (INTEL_GEN(dev_priv) < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004387 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
4388 return;
4389
4390 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
4391 DISP_TILE_SURFACE_SWIZZLING);
4392
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004393 if (IS_GEN5(dev_priv))
Daniel Vetter11782b02012-01-31 16:47:55 +01004394 return;
4395
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004396 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004397 if (IS_GEN6(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004398 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004399 else if (IS_GEN7(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004400 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004401 else if (IS_GEN8(dev_priv))
Ben Widawsky31a53362013-11-02 21:07:04 -07004402 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08004403 else
4404 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004405}
Daniel Vettere21af882012-02-09 20:53:27 +01004406
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004407static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004408{
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004409 I915_WRITE(RING_CTL(base), 0);
4410 I915_WRITE(RING_HEAD(base), 0);
4411 I915_WRITE(RING_TAIL(base), 0);
4412 I915_WRITE(RING_START(base), 0);
4413}
4414
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004415static void init_unused_rings(struct drm_i915_private *dev_priv)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004416{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004417 if (IS_I830(dev_priv)) {
4418 init_unused_ring(dev_priv, PRB1_BASE);
4419 init_unused_ring(dev_priv, SRB0_BASE);
4420 init_unused_ring(dev_priv, SRB1_BASE);
4421 init_unused_ring(dev_priv, SRB2_BASE);
4422 init_unused_ring(dev_priv, SRB3_BASE);
4423 } else if (IS_GEN2(dev_priv)) {
4424 init_unused_ring(dev_priv, SRB0_BASE);
4425 init_unused_ring(dev_priv, SRB1_BASE);
4426 } else if (IS_GEN3(dev_priv)) {
4427 init_unused_ring(dev_priv, PRB1_BASE);
4428 init_unused_ring(dev_priv, PRB2_BASE);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004429 }
4430}
4431
Chris Wilson20a8a742017-02-08 14:30:31 +00004432static int __i915_gem_restart_engines(void *data)
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004433{
Chris Wilson20a8a742017-02-08 14:30:31 +00004434 struct drm_i915_private *i915 = data;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004435 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05304436 enum intel_engine_id id;
Chris Wilson20a8a742017-02-08 14:30:31 +00004437 int err;
4438
4439 for_each_engine(engine, i915, id) {
4440 err = engine->init_hw(engine);
4441 if (err)
4442 return err;
4443 }
4444
4445 return 0;
4446}
4447
4448int i915_gem_init_hw(struct drm_i915_private *dev_priv)
4449{
Chris Wilsond200cda2016-04-28 09:56:44 +01004450 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004451
Chris Wilsonde867c22016-10-25 13:16:02 +01004452 dev_priv->gt.last_init_time = ktime_get();
4453
Chris Wilson5e4f5182015-02-13 14:35:59 +00004454 /* Double layer security blanket, see i915_gem_init() */
4455 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4456
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00004457 if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07004458 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004459
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01004460 if (IS_HASWELL(dev_priv))
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004461 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
Ville Syrjälä0bf21342013-11-29 14:56:12 +02004462 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03004463
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004464 if (HAS_PCH_NOP(dev_priv)) {
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01004465 if (IS_IVYBRIDGE(dev_priv)) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01004466 u32 temp = I915_READ(GEN7_MSG_CTL);
4467 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
4468 I915_WRITE(GEN7_MSG_CTL, temp);
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004469 } else if (INTEL_GEN(dev_priv) >= 7) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01004470 u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
4471 temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
4472 I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
4473 }
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07004474 }
4475
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004476 i915_gem_init_swizzling(dev_priv);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004477
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004478 /*
4479 * At least 830 can leave some of the unused rings
4480 * "active" (ie. head != tail) after resume which
4481 * will prevent c3 entry. Makes sure all unused rings
4482 * are totally idle.
4483 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004484 init_unused_rings(dev_priv);
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004485
Dave Gordoned54c1a2016-01-19 19:02:54 +00004486 BUG_ON(!dev_priv->kernel_context);
John Harrison90638cc2015-05-29 17:43:37 +01004487
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004488 ret = i915_ppgtt_init_hw(dev_priv);
John Harrison4ad2fd82015-06-18 13:11:20 +01004489 if (ret) {
4490 DRM_ERROR("PPGTT enable HW failed %d\n", ret);
4491 goto out;
4492 }
4493
4494 /* Need to do basic initialisation of all rings first: */
Chris Wilson20a8a742017-02-08 14:30:31 +00004495 ret = __i915_gem_restart_engines(dev_priv);
4496 if (ret)
4497 goto out;
Mika Kuoppala99433932013-01-22 14:12:17 +02004498
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004499 intel_mocs_init_l3cc_table(dev_priv);
Peter Antoine0ccdacf2016-04-13 15:03:25 +01004500
Alex Dai33a732f2015-08-12 15:43:36 +01004501 /* We can't enable contexts until all firmware is loaded */
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004502 ret = intel_guc_setup(dev_priv);
Dave Gordone556f7c2016-06-07 09:14:49 +01004503 if (ret)
4504 goto out;
Alex Dai33a732f2015-08-12 15:43:36 +01004505
Chris Wilson5e4f5182015-02-13 14:35:59 +00004506out:
4507 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004508 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004509}
4510
Chris Wilson39df9192016-07-20 13:31:57 +01004511bool intel_sanitize_semaphores(struct drm_i915_private *dev_priv, int value)
4512{
4513 if (INTEL_INFO(dev_priv)->gen < 6)
4514 return false;
4515
4516 /* TODO: make semaphores and Execlists play nicely together */
4517 if (i915.enable_execlists)
4518 return false;
4519
4520 if (value >= 0)
4521 return value;
4522
4523#ifdef CONFIG_INTEL_IOMMU
4524 /* Enable semaphores on SNB when IO remapping is off */
4525 if (INTEL_INFO(dev_priv)->gen == 6 && intel_iommu_gfx_mapped)
4526 return false;
4527#endif
4528
4529 return true;
4530}
4531
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004532int i915_gem_init(struct drm_i915_private *dev_priv)
Chris Wilson1070a422012-04-24 15:47:41 +01004533{
Chris Wilson1070a422012-04-24 15:47:41 +01004534 int ret;
4535
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004536 mutex_lock(&dev_priv->drm.struct_mutex);
Jesse Barnesd62b4892013-03-08 10:45:53 -08004537
Oscar Mateoa83014d2014-07-24 17:04:21 +01004538 if (!i915.enable_execlists) {
Chris Wilson821ed7d2016-09-09 14:11:53 +01004539 dev_priv->gt.resume = intel_legacy_submission_resume;
Chris Wilson7e37f882016-08-02 22:50:21 +01004540 dev_priv->gt.cleanup_engine = intel_engine_cleanup;
Oscar Mateo454afeb2014-07-24 17:04:22 +01004541 } else {
Chris Wilson821ed7d2016-09-09 14:11:53 +01004542 dev_priv->gt.resume = intel_lr_context_resume;
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004543 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
Oscar Mateoa83014d2014-07-24 17:04:21 +01004544 }
4545
Chris Wilson5e4f5182015-02-13 14:35:59 +00004546 /* This is just a security blanket to placate dragons.
4547 * On some systems, we very sporadically observe that the first TLBs
4548 * used by the CS may be stale, despite us poking the TLB reset. If
4549 * we hold the forcewake during initialisation these problems
4550 * just magically go away.
4551 */
4552 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4553
Chris Wilson72778cb2016-05-19 16:17:16 +01004554 i915_gem_init_userptr(dev_priv);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01004555
4556 ret = i915_gem_init_ggtt(dev_priv);
4557 if (ret)
4558 goto out_unlock;
Jesse Barnesd62b4892013-03-08 10:45:53 -08004559
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004560 ret = i915_gem_context_init(dev_priv);
Jani Nikula7bcc3772014-12-05 14:17:42 +02004561 if (ret)
4562 goto out_unlock;
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004563
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004564 ret = intel_engines_init(dev_priv);
Daniel Vetter35a57ff2014-11-20 00:33:07 +01004565 if (ret)
Jani Nikula7bcc3772014-12-05 14:17:42 +02004566 goto out_unlock;
Daniel Vetter53ca26c2012-04-26 23:28:03 +02004567
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004568 ret = i915_gem_init_hw(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01004569 if (ret == -EIO) {
Chris Wilson7e21d642016-07-27 09:07:29 +01004570 /* Allow engine initialisation to fail by marking the GPU as
Chris Wilson60990322014-04-09 09:19:42 +01004571 * wedged. But we only want to do this where the GPU is angry,
4572 * for all other failure, such as an allocation failure, bail.
4573 */
4574 DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
Chris Wilson821ed7d2016-09-09 14:11:53 +01004575 i915_gem_set_wedged(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01004576 ret = 0;
Chris Wilson1070a422012-04-24 15:47:41 +01004577 }
Jani Nikula7bcc3772014-12-05 14:17:42 +02004578
4579out_unlock:
Chris Wilson5e4f5182015-02-13 14:35:59 +00004580 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004581 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson1070a422012-04-24 15:47:41 +01004582
Chris Wilson60990322014-04-09 09:19:42 +01004583 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01004584}
4585
Chris Wilson24145512017-01-24 11:01:35 +00004586void i915_gem_init_mmio(struct drm_i915_private *i915)
4587{
4588 i915_gem_sanitize(i915);
4589}
4590
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004591void
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00004592i915_gem_cleanup_engines(struct drm_i915_private *dev_priv)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004593{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004594 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05304595 enum intel_engine_id id;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004596
Akash Goel3b3f1652016-10-13 22:44:48 +05304597 for_each_engine(engine, dev_priv, id)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004598 dev_priv->gt.cleanup_engine(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004599}
4600
Eric Anholt673a3942008-07-30 12:06:12 -07004601void
Imre Deak40ae4e12016-03-16 14:54:03 +02004602i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
4603{
Chris Wilson49ef5292016-08-18 17:17:00 +01004604 int i;
Imre Deak40ae4e12016-03-16 14:54:03 +02004605
4606 if (INTEL_INFO(dev_priv)->gen >= 7 && !IS_VALLEYVIEW(dev_priv) &&
4607 !IS_CHERRYVIEW(dev_priv))
4608 dev_priv->num_fence_regs = 32;
Jani Nikula73f67aa2016-12-07 22:48:09 +02004609 else if (INTEL_INFO(dev_priv)->gen >= 4 ||
4610 IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
4611 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02004612 dev_priv->num_fence_regs = 16;
4613 else
4614 dev_priv->num_fence_regs = 8;
4615
Chris Wilsonc0336662016-05-06 15:40:21 +01004616 if (intel_vgpu_active(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02004617 dev_priv->num_fence_regs =
4618 I915_READ(vgtif_reg(avail_rs.fence_num));
4619
4620 /* Initialize fence registers to zero */
Chris Wilson49ef5292016-08-18 17:17:00 +01004621 for (i = 0; i < dev_priv->num_fence_regs; i++) {
4622 struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
4623
4624 fence->i915 = dev_priv;
4625 fence->id = i;
4626 list_add_tail(&fence->link, &dev_priv->mm.fence_list);
4627 }
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00004628 i915_gem_restore_fences(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02004629
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00004630 i915_gem_detect_bit_6_swizzle(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02004631}
4632
Chris Wilson73cb9702016-10-28 13:58:46 +01004633int
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00004634i915_gem_load_init(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07004635{
Tvrtko Ursulina9335682016-11-02 15:14:59 +00004636 int err = -ENOMEM;
Chris Wilson42dcedd2012-11-15 11:32:30 +00004637
Tvrtko Ursulina9335682016-11-02 15:14:59 +00004638 dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
4639 if (!dev_priv->objects)
Chris Wilson73cb9702016-10-28 13:58:46 +01004640 goto err_out;
Chris Wilson73cb9702016-10-28 13:58:46 +01004641
Tvrtko Ursulina9335682016-11-02 15:14:59 +00004642 dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
4643 if (!dev_priv->vmas)
Chris Wilson73cb9702016-10-28 13:58:46 +01004644 goto err_objects;
Chris Wilson73cb9702016-10-28 13:58:46 +01004645
Tvrtko Ursulina9335682016-11-02 15:14:59 +00004646 dev_priv->requests = KMEM_CACHE(drm_i915_gem_request,
4647 SLAB_HWCACHE_ALIGN |
4648 SLAB_RECLAIM_ACCOUNT |
4649 SLAB_DESTROY_BY_RCU);
4650 if (!dev_priv->requests)
Chris Wilson73cb9702016-10-28 13:58:46 +01004651 goto err_vmas;
Chris Wilson73cb9702016-10-28 13:58:46 +01004652
Chris Wilson52e54202016-11-14 20:41:02 +00004653 dev_priv->dependencies = KMEM_CACHE(i915_dependency,
4654 SLAB_HWCACHE_ALIGN |
4655 SLAB_RECLAIM_ACCOUNT);
4656 if (!dev_priv->dependencies)
4657 goto err_requests;
4658
Chris Wilson73cb9702016-10-28 13:58:46 +01004659 mutex_lock(&dev_priv->drm.struct_mutex);
4660 INIT_LIST_HEAD(&dev_priv->gt.timelines);
Chris Wilsonbb894852016-11-14 20:40:57 +00004661 err = i915_gem_timeline_init__global(dev_priv);
Chris Wilson73cb9702016-10-28 13:58:46 +01004662 mutex_unlock(&dev_priv->drm.struct_mutex);
4663 if (err)
Chris Wilson52e54202016-11-14 20:41:02 +00004664 goto err_dependencies;
Eric Anholt673a3942008-07-30 12:06:12 -07004665
Ben Widawskya33afea2013-09-17 21:12:45 -07004666 INIT_LIST_HEAD(&dev_priv->context_list);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004667 INIT_WORK(&dev_priv->mm.free_work, __i915_gem_free_work);
4668 init_llist_head(&dev_priv->mm.free_list);
Chris Wilson6c085a72012-08-20 11:40:46 +02004669 INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
4670 INIT_LIST_HEAD(&dev_priv->mm.bound_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004671 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilson275f0392016-10-24 13:42:14 +01004672 INIT_LIST_HEAD(&dev_priv->mm.userfault_list);
Chris Wilson67d97da2016-07-04 08:08:31 +01004673 INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
Eric Anholt673a3942008-07-30 12:06:12 -07004674 i915_gem_retire_work_handler);
Chris Wilson67d97da2016-07-04 08:08:31 +01004675 INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004676 i915_gem_idle_work_handler);
Chris Wilson1f15b762016-07-01 17:23:14 +01004677 init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01004678 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson31169712009-09-14 16:50:28 +01004679
Chris Wilson72bfa192010-12-19 11:42:05 +00004680 dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
4681
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004682 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01004683
Chris Wilsonce453d82011-02-21 14:43:56 +00004684 dev_priv->mm.interruptible = true;
4685
Joonas Lahtinen6f633402016-09-01 14:58:21 +03004686 atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
4687
Chris Wilsonb5add952016-08-04 16:32:36 +01004688 spin_lock_init(&dev_priv->fb_tracking.lock);
Chris Wilson73cb9702016-10-28 13:58:46 +01004689
4690 return 0;
4691
Chris Wilson52e54202016-11-14 20:41:02 +00004692err_dependencies:
4693 kmem_cache_destroy(dev_priv->dependencies);
Chris Wilson73cb9702016-10-28 13:58:46 +01004694err_requests:
4695 kmem_cache_destroy(dev_priv->requests);
4696err_vmas:
4697 kmem_cache_destroy(dev_priv->vmas);
4698err_objects:
4699 kmem_cache_destroy(dev_priv->objects);
4700err_out:
4701 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07004702}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004703
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00004704void i915_gem_load_cleanup(struct drm_i915_private *dev_priv)
Imre Deakd64aa092016-01-19 15:26:29 +02004705{
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00004706 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson7d5d59e2016-11-01 08:48:41 +00004707 WARN_ON(!llist_empty(&dev_priv->mm.free_list));
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00004708 WARN_ON(dev_priv->mm.object_count);
Chris Wilson7d5d59e2016-11-01 08:48:41 +00004709
Matthew Auldea84aa72016-11-17 21:04:11 +00004710 mutex_lock(&dev_priv->drm.struct_mutex);
4711 i915_gem_timeline_fini(&dev_priv->gt.global_timeline);
4712 WARN_ON(!list_empty(&dev_priv->gt.timelines));
4713 mutex_unlock(&dev_priv->drm.struct_mutex);
4714
Chris Wilson52e54202016-11-14 20:41:02 +00004715 kmem_cache_destroy(dev_priv->dependencies);
Imre Deakd64aa092016-01-19 15:26:29 +02004716 kmem_cache_destroy(dev_priv->requests);
4717 kmem_cache_destroy(dev_priv->vmas);
4718 kmem_cache_destroy(dev_priv->objects);
Chris Wilson0eafec62016-08-04 16:32:41 +01004719
4720 /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
4721 rcu_barrier();
Imre Deakd64aa092016-01-19 15:26:29 +02004722}
4723
Chris Wilson6a800ea2016-09-21 14:51:07 +01004724int i915_gem_freeze(struct drm_i915_private *dev_priv)
4725{
Chris Wilson6a800ea2016-09-21 14:51:07 +01004726 mutex_lock(&dev_priv->drm.struct_mutex);
4727 i915_gem_shrink_all(dev_priv);
4728 mutex_unlock(&dev_priv->drm.struct_mutex);
4729
Chris Wilson6a800ea2016-09-21 14:51:07 +01004730 return 0;
4731}
4732
Chris Wilson461fb992016-05-14 07:26:33 +01004733int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
4734{
4735 struct drm_i915_gem_object *obj;
Chris Wilson7aab2d52016-09-09 20:02:18 +01004736 struct list_head *phases[] = {
4737 &dev_priv->mm.unbound_list,
4738 &dev_priv->mm.bound_list,
4739 NULL
4740 }, **p;
Chris Wilson461fb992016-05-14 07:26:33 +01004741
4742 /* Called just before we write the hibernation image.
4743 *
4744 * We need to update the domain tracking to reflect that the CPU
4745 * will be accessing all the pages to create and restore from the
4746 * hibernation, and so upon restoration those pages will be in the
4747 * CPU domain.
4748 *
4749 * To make sure the hibernation image contains the latest state,
4750 * we update that state just before writing out the image.
Chris Wilson7aab2d52016-09-09 20:02:18 +01004751 *
4752 * To try and reduce the hibernation image, we manually shrink
4753 * the objects as well.
Chris Wilson461fb992016-05-14 07:26:33 +01004754 */
4755
Chris Wilson6a800ea2016-09-21 14:51:07 +01004756 mutex_lock(&dev_priv->drm.struct_mutex);
4757 i915_gem_shrink(dev_priv, -1UL, I915_SHRINK_UNBOUND);
Chris Wilson461fb992016-05-14 07:26:33 +01004758
Chris Wilson7aab2d52016-09-09 20:02:18 +01004759 for (p = phases; *p; p++) {
Joonas Lahtinen56cea322016-11-02 12:16:04 +02004760 list_for_each_entry(obj, *p, global_link) {
Chris Wilson7aab2d52016-09-09 20:02:18 +01004761 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4762 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4763 }
Chris Wilson461fb992016-05-14 07:26:33 +01004764 }
Chris Wilson6a800ea2016-09-21 14:51:07 +01004765 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson461fb992016-05-14 07:26:33 +01004766
4767 return 0;
4768}
4769
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004770void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004771{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004772 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilson15f7bbc2016-07-26 12:01:52 +01004773 struct drm_i915_gem_request *request;
Eric Anholtb9624422009-06-03 07:27:35 +00004774
4775 /* Clean up our request list when the client is going away, so that
4776 * later retire_requests won't dereference our soon-to-be-gone
4777 * file_priv.
4778 */
Chris Wilson1c255952010-09-26 11:03:27 +01004779 spin_lock(&file_priv->mm.lock);
Chris Wilson15f7bbc2016-07-26 12:01:52 +01004780 list_for_each_entry(request, &file_priv->mm.request_list, client_list)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004781 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01004782 spin_unlock(&file_priv->mm.lock);
Chris Wilson31169712009-09-14 16:50:28 +01004783
Chris Wilson2e1b8732015-04-27 13:41:22 +01004784 if (!list_empty(&file_priv->rps.link)) {
Chris Wilson8d3afd72015-05-21 21:01:47 +01004785 spin_lock(&to_i915(dev)->rps.client_lock);
Chris Wilson2e1b8732015-04-27 13:41:22 +01004786 list_del(&file_priv->rps.link);
Chris Wilson8d3afd72015-05-21 21:01:47 +01004787 spin_unlock(&to_i915(dev)->rps.client_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01004788 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004789}
4790
4791int i915_gem_open(struct drm_device *dev, struct drm_file *file)
4792{
4793 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08004794 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004795
Chris Wilsonc4c29d72016-11-09 10:45:07 +00004796 DRM_DEBUG("\n");
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004797
4798 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
4799 if (!file_priv)
4800 return -ENOMEM;
4801
4802 file->driver_priv = file_priv;
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004803 file_priv->dev_priv = to_i915(dev);
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02004804 file_priv->file = file;
Chris Wilson2e1b8732015-04-27 13:41:22 +01004805 INIT_LIST_HEAD(&file_priv->rps.link);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004806
4807 spin_lock_init(&file_priv->mm.lock);
4808 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004809
Chris Wilsonc80ff162016-07-27 09:07:27 +01004810 file_priv->bsd_engine = -1;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00004811
Ben Widawskye422b882013-12-06 14:10:58 -08004812 ret = i915_gem_context_open(dev, file);
4813 if (ret)
4814 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004815
Ben Widawskye422b882013-12-06 14:10:58 -08004816 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004817}
4818
Daniel Vetterb680c372014-09-19 18:27:27 +02004819/**
4820 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07004821 * @old: current GEM buffer for the frontbuffer slots
4822 * @new: new GEM buffer for the frontbuffer slots
4823 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02004824 *
4825 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
4826 * from @old and setting them in @new. Both @old and @new can be NULL.
4827 */
Daniel Vettera071fa02014-06-18 23:28:09 +02004828void i915_gem_track_fb(struct drm_i915_gem_object *old,
4829 struct drm_i915_gem_object *new,
4830 unsigned frontbuffer_bits)
4831{
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01004832 /* Control of individual bits within the mask are guarded by
4833 * the owning plane->mutex, i.e. we can never see concurrent
4834 * manipulation of individual bits. But since the bitfield as a whole
4835 * is updated using RMW, we need to use atomics in order to update
4836 * the bits.
4837 */
4838 BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
4839 sizeof(atomic_t) * BITS_PER_BYTE);
4840
Daniel Vettera071fa02014-06-18 23:28:09 +02004841 if (old) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01004842 WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
4843 atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02004844 }
4845
4846 if (new) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01004847 WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
4848 atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02004849 }
4850}
4851
Dave Gordonea702992015-07-09 19:29:02 +01004852/* Allocate a new GEM object and fill it with the supplied data */
4853struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004854i915_gem_object_create_from_data(struct drm_i915_private *dev_priv,
Dave Gordonea702992015-07-09 19:29:02 +01004855 const void *data, size_t size)
4856{
4857 struct drm_i915_gem_object *obj;
4858 struct sg_table *sg;
4859 size_t bytes;
4860 int ret;
4861
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004862 obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE));
Chris Wilsonfe3db792016-04-25 13:32:13 +01004863 if (IS_ERR(obj))
Dave Gordonea702992015-07-09 19:29:02 +01004864 return obj;
4865
4866 ret = i915_gem_object_set_to_cpu_domain(obj, true);
4867 if (ret)
4868 goto fail;
4869
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004870 ret = i915_gem_object_pin_pages(obj);
Dave Gordonea702992015-07-09 19:29:02 +01004871 if (ret)
4872 goto fail;
4873
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004874 sg = obj->mm.pages;
Dave Gordonea702992015-07-09 19:29:02 +01004875 bytes = sg_copy_from_buffer(sg->sgl, sg->nents, (void *)data, size);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004876 obj->mm.dirty = true; /* Backing store is now out of date */
Dave Gordonea702992015-07-09 19:29:02 +01004877 i915_gem_object_unpin_pages(obj);
4878
4879 if (WARN_ON(bytes != size)) {
4880 DRM_ERROR("Incomplete copy, wrote %zu of %zu", bytes, size);
4881 ret = -EFAULT;
4882 goto fail;
4883 }
4884
4885 return obj;
4886
4887fail:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004888 i915_gem_object_put(obj);
Dave Gordonea702992015-07-09 19:29:02 +01004889 return ERR_PTR(ret);
4890}
Chris Wilson96d77632016-10-28 13:58:33 +01004891
4892struct scatterlist *
4893i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
4894 unsigned int n,
4895 unsigned int *offset)
4896{
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004897 struct i915_gem_object_page_iter *iter = &obj->mm.get_page;
Chris Wilson96d77632016-10-28 13:58:33 +01004898 struct scatterlist *sg;
4899 unsigned int idx, count;
4900
4901 might_sleep();
4902 GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004903 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
Chris Wilson96d77632016-10-28 13:58:33 +01004904
4905 /* As we iterate forward through the sg, we record each entry in a
4906 * radixtree for quick repeated (backwards) lookups. If we have seen
4907 * this index previously, we will have an entry for it.
4908 *
4909 * Initial lookup is O(N), but this is amortized to O(1) for
4910 * sequential page access (where each new request is consecutive
4911 * to the previous one). Repeated lookups are O(lg(obj->base.size)),
4912 * i.e. O(1) with a large constant!
4913 */
4914 if (n < READ_ONCE(iter->sg_idx))
4915 goto lookup;
4916
4917 mutex_lock(&iter->lock);
4918
4919 /* We prefer to reuse the last sg so that repeated lookup of this
4920 * (or the subsequent) sg are fast - comparing against the last
4921 * sg is faster than going through the radixtree.
4922 */
4923
4924 sg = iter->sg_pos;
4925 idx = iter->sg_idx;
4926 count = __sg_page_count(sg);
4927
4928 while (idx + count <= n) {
4929 unsigned long exception, i;
4930 int ret;
4931
4932 /* If we cannot allocate and insert this entry, or the
4933 * individual pages from this range, cancel updating the
4934 * sg_idx so that on this lookup we are forced to linearly
4935 * scan onwards, but on future lookups we will try the
4936 * insertion again (in which case we need to be careful of
4937 * the error return reporting that we have already inserted
4938 * this index).
4939 */
4940 ret = radix_tree_insert(&iter->radix, idx, sg);
4941 if (ret && ret != -EEXIST)
4942 goto scan;
4943
4944 exception =
4945 RADIX_TREE_EXCEPTIONAL_ENTRY |
4946 idx << RADIX_TREE_EXCEPTIONAL_SHIFT;
4947 for (i = 1; i < count; i++) {
4948 ret = radix_tree_insert(&iter->radix, idx + i,
4949 (void *)exception);
4950 if (ret && ret != -EEXIST)
4951 goto scan;
4952 }
4953
4954 idx += count;
4955 sg = ____sg_next(sg);
4956 count = __sg_page_count(sg);
4957 }
4958
4959scan:
4960 iter->sg_pos = sg;
4961 iter->sg_idx = idx;
4962
4963 mutex_unlock(&iter->lock);
4964
4965 if (unlikely(n < idx)) /* insertion completed by another thread */
4966 goto lookup;
4967
4968 /* In case we failed to insert the entry into the radixtree, we need
4969 * to look beyond the current sg.
4970 */
4971 while (idx + count <= n) {
4972 idx += count;
4973 sg = ____sg_next(sg);
4974 count = __sg_page_count(sg);
4975 }
4976
4977 *offset = n - idx;
4978 return sg;
4979
4980lookup:
4981 rcu_read_lock();
4982
4983 sg = radix_tree_lookup(&iter->radix, n);
4984 GEM_BUG_ON(!sg);
4985
4986 /* If this index is in the middle of multi-page sg entry,
4987 * the radixtree will contain an exceptional entry that points
4988 * to the start of that range. We will return the pointer to
4989 * the base page and the offset of this page within the
4990 * sg entry's range.
4991 */
4992 *offset = 0;
4993 if (unlikely(radix_tree_exception(sg))) {
4994 unsigned long base =
4995 (unsigned long)sg >> RADIX_TREE_EXCEPTIONAL_SHIFT;
4996
4997 sg = radix_tree_lookup(&iter->radix, base);
4998 GEM_BUG_ON(!sg);
4999
5000 *offset = n - base;
5001 }
5002
5003 rcu_read_unlock();
5004
5005 return sg;
5006}
5007
5008struct page *
5009i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
5010{
5011 struct scatterlist *sg;
5012 unsigned int offset;
5013
5014 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
5015
5016 sg = i915_gem_object_get_sg(obj, n, &offset);
5017 return nth_page(sg_page(sg), offset);
5018}
5019
5020/* Like i915_gem_object_get_page(), but mark the returned page dirty */
5021struct page *
5022i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
5023 unsigned int n)
5024{
5025 struct page *page;
5026
5027 page = i915_gem_object_get_page(obj, n);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005028 if (!obj->mm.dirty)
Chris Wilson96d77632016-10-28 13:58:33 +01005029 set_page_dirty(page);
5030
5031 return page;
5032}
5033
5034dma_addr_t
5035i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
5036 unsigned long n)
5037{
5038 struct scatterlist *sg;
5039 unsigned int offset;
5040
5041 sg = i915_gem_object_get_sg(obj, n, &offset);
5042 return sg_dma_address(sg) + (offset << PAGE_SHIFT);
5043}
Chris Wilson935a2f72017-02-13 17:15:13 +00005044
5045#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
5046#include "selftests/scatterlist.c"
Chris Wilson66d9cb52017-02-13 17:15:17 +00005047#include "selftests/mock_gem_device.c"
Chris Wilson44653982017-02-13 17:15:20 +00005048#include "selftests/huge_gem_object.c"
Chris Wilson8335fd62017-02-13 17:15:28 +00005049#include "selftests/i915_gem_object.c"
Chris Wilson17059452017-02-13 17:15:32 +00005050#include "selftests/i915_gem_coherency.c"
Chris Wilson935a2f72017-02-13 17:15:13 +00005051#endif