blob: 354b0546a191572886e4538f3da48a7ff375c5b7 [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
Daniel Vetterbe6a0372015-03-18 10:46:04 +01002 * Copyright © 2008-2015 Intel Corporation
Eric Anholt673a3942008-07-30 12:06:12 -07003 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
David Howells760285e2012-10-02 18:01:07 +010028#include <drm/drmP.h>
David Herrmann0de23972013-07-24 21:07:52 +020029#include <drm/drm_vma_manager.h>
David Howells760285e2012-10-02 18:01:07 +010030#include <drm/i915_drm.h>
Eric Anholt673a3942008-07-30 12:06:12 -070031#include "i915_drv.h"
Chris Wilson57822dc2017-02-22 11:40:48 +000032#include "i915_gem_clflush.h"
Yu Zhangeb822892015-02-10 19:05:49 +080033#include "i915_vgpu.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010034#include "i915_trace.h"
Jesse Barnes652c3932009-08-17 13:31:43 -070035#include "intel_drv.h"
Chris Wilson5d723d72016-08-04 16:32:35 +010036#include "intel_frontbuffer.h"
Peter Antoine0ccdacf2016-04-13 15:03:25 +010037#include "intel_mocs.h"
Matthew Auld465c4032017-10-06 23:18:14 +010038#include "i915_gemfs.h"
Chris Wilson6b5e90f2016-11-14 20:41:05 +000039#include <linux/dma-fence-array.h>
Chris Wilsonfe3288b2017-02-12 17:20:01 +000040#include <linux/kthread.h>
Chris Wilsonc13d87e2016-07-20 09:21:15 +010041#include <linux/reservation.h>
Hugh Dickins5949eac2011-06-27 16:18:18 -070042#include <linux/shmem_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Chris Wilson20e49332016-11-22 14:41:21 +000044#include <linux/stop_machine.h>
Eric Anholt673a3942008-07-30 12:06:12 -070045#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080046#include <linux/pci.h>
Daniel Vetter1286ff72012-05-10 15:25:09 +020047#include <linux/dma-buf.h>
Eric Anholt673a3942008-07-30 12:06:12 -070048
Chris Wilsonfbbd37b2016-10-28 13:58:42 +010049static void i915_gem_flush_free_objects(struct drm_i915_private *i915);
Chris Wilson61050802012-04-17 15:31:31 +010050
Chris Wilson2c225692013-08-09 12:26:45 +010051static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
52{
Chris Wilsone27ab732017-06-15 13:38:49 +010053 if (obj->cache_dirty)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +053054 return false;
55
Chris Wilsonb8f55be2017-08-11 12:11:16 +010056 if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE))
Chris Wilson2c225692013-08-09 12:26:45 +010057 return true;
58
Chris Wilsonbd3d2252017-10-13 21:26:14 +010059 return obj->pin_global; /* currently in use by HW, keep flushed */
Chris Wilson2c225692013-08-09 12:26:45 +010060}
61
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053062static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +010063insert_mappable_node(struct i915_ggtt *ggtt,
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053064 struct drm_mm_node *node, u32 size)
65{
66 memset(node, 0, sizeof(*node));
Chris Wilson4e64e552017-02-02 21:04:38 +000067 return drm_mm_insert_node_in_range(&ggtt->base.mm, node,
68 size, 0, I915_COLOR_UNEVICTABLE,
69 0, ggtt->mappable_end,
70 DRM_MM_INSERT_LOW);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053071}
72
73static void
74remove_mappable_node(struct drm_mm_node *node)
75{
76 drm_mm_remove_node(node);
77}
78
Chris Wilson73aa8082010-09-30 11:46:12 +010079/* some bookkeeping */
80static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010081 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010082{
Daniel Vetterc20e8352013-07-24 22:40:23 +020083 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010084 dev_priv->mm.object_count++;
85 dev_priv->mm.object_memory += size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020086 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010087}
88
89static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010090 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010091{
Daniel Vetterc20e8352013-07-24 22:40:23 +020092 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010093 dev_priv->mm.object_count--;
94 dev_priv->mm.object_memory -= size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020095 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010096}
97
Chris Wilson21dd3732011-01-26 15:55:56 +000098static int
Daniel Vetter33196de2012-11-14 17:14:05 +010099i915_gem_wait_for_error(struct i915_gpu_error *error)
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100100{
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100101 int ret;
102
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100103 might_sleep();
104
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200105 /*
106 * Only wait 10 seconds for the gpu reset to complete to avoid hanging
107 * userspace. If it takes that long something really bad is going on and
108 * we should simply try to bail out and fail as gracefully as possible.
109 */
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100110 ret = wait_event_interruptible_timeout(error->reset_queue,
Chris Wilson8c185ec2017-03-16 17:13:02 +0000111 !i915_reset_backoff(error),
Chris Wilsonb52992c2016-10-28 13:58:24 +0100112 I915_RESET_TIMEOUT);
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200113 if (ret == 0) {
114 DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
115 return -EIO;
116 } else if (ret < 0) {
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100117 return ret;
Chris Wilsond98c52c2016-04-13 17:35:05 +0100118 } else {
119 return 0;
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200120 }
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100121}
122
Chris Wilson54cf91d2010-11-25 18:00:26 +0000123int i915_mutex_lock_interruptible(struct drm_device *dev)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100124{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100125 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100126 int ret;
127
Daniel Vetter33196de2012-11-14 17:14:05 +0100128 ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100129 if (ret)
130 return ret;
131
132 ret = mutex_lock_interruptible(&dev->struct_mutex);
133 if (ret)
134 return ret;
135
Chris Wilson76c1dec2010-09-25 11:22:51 +0100136 return 0;
137}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100138
Eric Anholt673a3942008-07-30 12:06:12 -0700139int
Eric Anholt5a125c32008-10-22 21:40:13 -0700140i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000141 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700142{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300143 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen62106b42016-03-18 10:42:57 +0200144 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300145 struct drm_i915_gem_get_aperture *args = data;
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100146 struct i915_vma *vma;
Weinan Liff8f7972017-05-31 10:35:52 +0800147 u64 pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700148
Weinan Liff8f7972017-05-31 10:35:52 +0800149 pinned = ggtt->base.reserved;
Chris Wilson73aa8082010-09-30 11:46:12 +0100150 mutex_lock(&dev->struct_mutex);
Chris Wilson1c7f4bc2016-02-26 11:03:19 +0000151 list_for_each_entry(vma, &ggtt->base.active_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100152 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100153 pinned += vma->node.size;
Chris Wilson1c7f4bc2016-02-26 11:03:19 +0000154 list_for_each_entry(vma, &ggtt->base.inactive_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100155 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100156 pinned += vma->node.size;
Chris Wilson73aa8082010-09-30 11:46:12 +0100157 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700158
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300159 args->aper_size = ggtt->base.total;
Akshay Joshi0206e352011-08-16 15:34:10 -0400160 args->aper_available_size = args->aper_size - pinned;
Chris Wilson6299f992010-11-24 12:23:44 +0000161
Eric Anholt5a125c32008-10-22 21:40:13 -0700162 return 0;
163}
164
Matthew Auldb91b09e2017-10-06 23:18:17 +0100165static int i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
Chris Wilson00731152014-05-21 12:42:56 +0100166{
Al Viro93c76a32015-12-04 23:45:44 -0500167 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilsondbb43512016-12-07 13:34:11 +0000168 drm_dma_handle_t *phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800169 struct sg_table *st;
170 struct scatterlist *sg;
Chris Wilsondbb43512016-12-07 13:34:11 +0000171 char *vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800172 int i;
Matthew Auldb91b09e2017-10-06 23:18:17 +0100173 int err;
Chris Wilson00731152014-05-21 12:42:56 +0100174
Chris Wilson6a2c4232014-11-04 04:51:40 -0800175 if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
Matthew Auldb91b09e2017-10-06 23:18:17 +0100176 return -EINVAL;
Chris Wilson00731152014-05-21 12:42:56 +0100177
Chris Wilsondbb43512016-12-07 13:34:11 +0000178 /* Always aligning to the object size, allows a single allocation
179 * to handle all possible callers, and given typical object sizes,
180 * the alignment of the buddy allocation will naturally match.
181 */
182 phys = drm_pci_alloc(obj->base.dev,
Ville Syrjälä750fae22017-09-07 17:32:03 +0300183 roundup_pow_of_two(obj->base.size),
Chris Wilsondbb43512016-12-07 13:34:11 +0000184 roundup_pow_of_two(obj->base.size));
185 if (!phys)
Matthew Auldb91b09e2017-10-06 23:18:17 +0100186 return -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000187
188 vaddr = phys->vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800189 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
190 struct page *page;
191 char *src;
192
193 page = shmem_read_mapping_page(mapping, i);
Chris Wilsondbb43512016-12-07 13:34:11 +0000194 if (IS_ERR(page)) {
Matthew Auldb91b09e2017-10-06 23:18:17 +0100195 err = PTR_ERR(page);
Chris Wilsondbb43512016-12-07 13:34:11 +0000196 goto err_phys;
197 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800198
199 src = kmap_atomic(page);
200 memcpy(vaddr, src, PAGE_SIZE);
201 drm_clflush_virt_range(vaddr, PAGE_SIZE);
202 kunmap_atomic(src);
203
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300204 put_page(page);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800205 vaddr += PAGE_SIZE;
206 }
207
Chris Wilsonc0336662016-05-06 15:40:21 +0100208 i915_gem_chipset_flush(to_i915(obj->base.dev));
Chris Wilson6a2c4232014-11-04 04:51:40 -0800209
210 st = kmalloc(sizeof(*st), GFP_KERNEL);
Chris Wilsondbb43512016-12-07 13:34:11 +0000211 if (!st) {
Matthew Auldb91b09e2017-10-06 23:18:17 +0100212 err = -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000213 goto err_phys;
214 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800215
216 if (sg_alloc_table(st, 1, GFP_KERNEL)) {
217 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100218 err = -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000219 goto err_phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800220 }
221
222 sg = st->sgl;
223 sg->offset = 0;
224 sg->length = obj->base.size;
225
Chris Wilsondbb43512016-12-07 13:34:11 +0000226 sg_dma_address(sg) = phys->busaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800227 sg_dma_len(sg) = obj->base.size;
228
Chris Wilsondbb43512016-12-07 13:34:11 +0000229 obj->phys_handle = phys;
Matthew Auldb91b09e2017-10-06 23:18:17 +0100230
Matthew Aulda5c081662017-10-06 23:18:18 +0100231 __i915_gem_object_set_pages(obj, st, sg->length);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100232
233 return 0;
Chris Wilsondbb43512016-12-07 13:34:11 +0000234
235err_phys:
236 drm_pci_free(obj->base.dev, phys);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100237
238 return err;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800239}
240
Chris Wilsone27ab732017-06-15 13:38:49 +0100241static void __start_cpu_write(struct drm_i915_gem_object *obj)
242{
243 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
244 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
245 if (cpu_write_needs_clflush(obj))
246 obj->cache_dirty = true;
247}
248
Chris Wilson6a2c4232014-11-04 04:51:40 -0800249static void
Chris Wilson2b3c8312016-11-11 14:58:09 +0000250__i915_gem_object_release_shmem(struct drm_i915_gem_object *obj,
Chris Wilsone5facdf2016-12-23 14:57:57 +0000251 struct sg_table *pages,
252 bool needs_clflush)
Chris Wilson6a2c4232014-11-04 04:51:40 -0800253{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100254 GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800255
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100256 if (obj->mm.madv == I915_MADV_DONTNEED)
257 obj->mm.dirty = false;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800258
Chris Wilsone5facdf2016-12-23 14:57:57 +0000259 if (needs_clflush &&
260 (obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0 &&
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100261 !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ))
Chris Wilson2b3c8312016-11-11 14:58:09 +0000262 drm_clflush_sg(pages);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100263
Chris Wilsone27ab732017-06-15 13:38:49 +0100264 __start_cpu_write(obj);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100265}
266
267static void
268i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj,
269 struct sg_table *pages)
270{
Chris Wilsone5facdf2016-12-23 14:57:57 +0000271 __i915_gem_object_release_shmem(obj, pages, false);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100272
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100273 if (obj->mm.dirty) {
Al Viro93c76a32015-12-04 23:45:44 -0500274 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800275 char *vaddr = obj->phys_handle->vaddr;
Chris Wilson00731152014-05-21 12:42:56 +0100276 int i;
277
278 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
Chris Wilson6a2c4232014-11-04 04:51:40 -0800279 struct page *page;
280 char *dst;
Chris Wilson00731152014-05-21 12:42:56 +0100281
Chris Wilson6a2c4232014-11-04 04:51:40 -0800282 page = shmem_read_mapping_page(mapping, i);
283 if (IS_ERR(page))
284 continue;
285
286 dst = kmap_atomic(page);
287 drm_clflush_virt_range(vaddr, PAGE_SIZE);
288 memcpy(dst, vaddr, PAGE_SIZE);
289 kunmap_atomic(dst);
290
291 set_page_dirty(page);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100292 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson00731152014-05-21 12:42:56 +0100293 mark_page_accessed(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300294 put_page(page);
Chris Wilson00731152014-05-21 12:42:56 +0100295 vaddr += PAGE_SIZE;
296 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100297 obj->mm.dirty = false;
Chris Wilson00731152014-05-21 12:42:56 +0100298 }
299
Chris Wilson03ac84f2016-10-28 13:58:36 +0100300 sg_free_table(pages);
301 kfree(pages);
Chris Wilsondbb43512016-12-07 13:34:11 +0000302
303 drm_pci_free(obj->base.dev, obj->phys_handle);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800304}
305
306static void
307i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
308{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100309 i915_gem_object_unpin_pages(obj);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800310}
311
312static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
313 .get_pages = i915_gem_object_get_pages_phys,
314 .put_pages = i915_gem_object_put_pages_phys,
315 .release = i915_gem_object_release_phys,
316};
317
Chris Wilson581ab1f2017-02-15 16:39:00 +0000318static const struct drm_i915_gem_object_ops i915_gem_object_ops;
319
Chris Wilson35a96112016-08-14 18:44:40 +0100320int i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Chris Wilsonaa653a62016-08-04 07:52:27 +0100321{
322 struct i915_vma *vma;
323 LIST_HEAD(still_in_list);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100324 int ret;
Chris Wilsonaa653a62016-08-04 07:52:27 +0100325
Chris Wilson02bef8f2016-08-14 18:44:41 +0100326 lockdep_assert_held(&obj->base.dev->struct_mutex);
327
328 /* Closed vma are removed from the obj->vma_list - but they may
329 * still have an active binding on the object. To remove those we
330 * must wait for all rendering to complete to the object (as unbinding
331 * must anyway), and retire the requests.
Chris Wilsonaa653a62016-08-04 07:52:27 +0100332 */
Chris Wilsone95433c2016-10-28 13:58:27 +0100333 ret = i915_gem_object_wait(obj,
334 I915_WAIT_INTERRUPTIBLE |
335 I915_WAIT_LOCKED |
336 I915_WAIT_ALL,
337 MAX_SCHEDULE_TIMEOUT,
338 NULL);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100339 if (ret)
340 return ret;
341
342 i915_gem_retire_requests(to_i915(obj->base.dev));
343
Chris Wilsonaa653a62016-08-04 07:52:27 +0100344 while ((vma = list_first_entry_or_null(&obj->vma_list,
345 struct i915_vma,
346 obj_link))) {
347 list_move_tail(&vma->obj_link, &still_in_list);
348 ret = i915_vma_unbind(vma);
349 if (ret)
350 break;
351 }
352 list_splice(&still_in_list, &obj->vma_list);
353
354 return ret;
355}
356
Chris Wilsone95433c2016-10-28 13:58:27 +0100357static long
358i915_gem_object_wait_fence(struct dma_fence *fence,
359 unsigned int flags,
360 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100361 struct intel_rps_client *rps_client)
Chris Wilsone95433c2016-10-28 13:58:27 +0100362{
363 struct drm_i915_gem_request *rq;
364
365 BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1);
366
367 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
368 return timeout;
369
370 if (!dma_fence_is_i915(fence))
371 return dma_fence_wait_timeout(fence,
372 flags & I915_WAIT_INTERRUPTIBLE,
373 timeout);
374
375 rq = to_request(fence);
376 if (i915_gem_request_completed(rq))
377 goto out;
378
379 /* This client is about to stall waiting for the GPU. In many cases
380 * this is undesirable and limits the throughput of the system, as
381 * many clients cannot continue processing user input/output whilst
382 * blocked. RPS autotuning may take tens of milliseconds to respond
383 * to the GPU load and thus incurs additional latency for the client.
384 * We can circumvent that by promoting the GPU frequency to maximum
385 * before we wait. This makes the GPU throttle up much more quickly
386 * (good for benchmarks and user experience, e.g. window animations),
387 * but at a cost of spending more power processing the workload
388 * (bad for battery). Not all clients even want their results
389 * immediately and for them we should just let the GPU select its own
390 * frequency to maximise efficiency. To prevent a single client from
391 * forcing the clocks too high for the whole system, we only allow
392 * each client to waitboost once in a busy period.
393 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100394 if (rps_client) {
Chris Wilsone95433c2016-10-28 13:58:27 +0100395 if (INTEL_GEN(rq->i915) >= 6)
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100396 gen6_rps_boost(rq, rps_client);
Chris Wilsone95433c2016-10-28 13:58:27 +0100397 else
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100398 rps_client = NULL;
Chris Wilsone95433c2016-10-28 13:58:27 +0100399 }
400
401 timeout = i915_wait_request(rq, flags, timeout);
402
403out:
404 if (flags & I915_WAIT_LOCKED && i915_gem_request_completed(rq))
405 i915_gem_request_retire_upto(rq);
406
Chris Wilsone95433c2016-10-28 13:58:27 +0100407 return timeout;
408}
409
410static long
411i915_gem_object_wait_reservation(struct reservation_object *resv,
412 unsigned int flags,
413 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100414 struct intel_rps_client *rps_client)
Chris Wilsone95433c2016-10-28 13:58:27 +0100415{
Chris Wilsone54ca972017-02-17 15:13:04 +0000416 unsigned int seq = __read_seqcount_begin(&resv->seq);
Chris Wilsone95433c2016-10-28 13:58:27 +0100417 struct dma_fence *excl;
Chris Wilsone54ca972017-02-17 15:13:04 +0000418 bool prune_fences = false;
Chris Wilsone95433c2016-10-28 13:58:27 +0100419
420 if (flags & I915_WAIT_ALL) {
421 struct dma_fence **shared;
422 unsigned int count, i;
423 int ret;
424
425 ret = reservation_object_get_fences_rcu(resv,
426 &excl, &count, &shared);
427 if (ret)
428 return ret;
429
430 for (i = 0; i < count; i++) {
431 timeout = i915_gem_object_wait_fence(shared[i],
432 flags, timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100433 rps_client);
Chris Wilsond892e932017-02-12 21:53:43 +0000434 if (timeout < 0)
Chris Wilsone95433c2016-10-28 13:58:27 +0100435 break;
436
437 dma_fence_put(shared[i]);
438 }
439
440 for (; i < count; i++)
441 dma_fence_put(shared[i]);
442 kfree(shared);
Chris Wilsone54ca972017-02-17 15:13:04 +0000443
444 prune_fences = count && timeout >= 0;
Chris Wilsone95433c2016-10-28 13:58:27 +0100445 } else {
446 excl = reservation_object_get_excl_rcu(resv);
447 }
448
Chris Wilsone54ca972017-02-17 15:13:04 +0000449 if (excl && timeout >= 0) {
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100450 timeout = i915_gem_object_wait_fence(excl, flags, timeout,
451 rps_client);
Chris Wilsone54ca972017-02-17 15:13:04 +0000452 prune_fences = timeout >= 0;
453 }
Chris Wilsone95433c2016-10-28 13:58:27 +0100454
455 dma_fence_put(excl);
456
Chris Wilson03d1cac2017-03-08 13:26:28 +0000457 /* Oportunistically prune the fences iff we know they have *all* been
458 * signaled and that the reservation object has not been changed (i.e.
459 * no new fences have been added).
460 */
Chris Wilsone54ca972017-02-17 15:13:04 +0000461 if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) {
Chris Wilson03d1cac2017-03-08 13:26:28 +0000462 if (reservation_object_trylock(resv)) {
463 if (!__read_seqcount_retry(&resv->seq, seq))
464 reservation_object_add_excl_fence(resv, NULL);
465 reservation_object_unlock(resv);
466 }
Chris Wilsone54ca972017-02-17 15:13:04 +0000467 }
468
Chris Wilsone95433c2016-10-28 13:58:27 +0100469 return timeout;
470}
471
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000472static void __fence_set_priority(struct dma_fence *fence, int prio)
473{
474 struct drm_i915_gem_request *rq;
475 struct intel_engine_cs *engine;
476
477 if (!dma_fence_is_i915(fence))
478 return;
479
480 rq = to_request(fence);
481 engine = rq->engine;
482 if (!engine->schedule)
483 return;
484
485 engine->schedule(rq, prio);
486}
487
488static void fence_set_priority(struct dma_fence *fence, int prio)
489{
490 /* Recurse once into a fence-array */
491 if (dma_fence_is_array(fence)) {
492 struct dma_fence_array *array = to_dma_fence_array(fence);
493 int i;
494
495 for (i = 0; i < array->num_fences; i++)
496 __fence_set_priority(array->fences[i], prio);
497 } else {
498 __fence_set_priority(fence, prio);
499 }
500}
501
502int
503i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
504 unsigned int flags,
505 int prio)
506{
507 struct dma_fence *excl;
508
509 if (flags & I915_WAIT_ALL) {
510 struct dma_fence **shared;
511 unsigned int count, i;
512 int ret;
513
514 ret = reservation_object_get_fences_rcu(obj->resv,
515 &excl, &count, &shared);
516 if (ret)
517 return ret;
518
519 for (i = 0; i < count; i++) {
520 fence_set_priority(shared[i], prio);
521 dma_fence_put(shared[i]);
522 }
523
524 kfree(shared);
525 } else {
526 excl = reservation_object_get_excl_rcu(obj->resv);
527 }
528
529 if (excl) {
530 fence_set_priority(excl, prio);
531 dma_fence_put(excl);
532 }
533 return 0;
534}
535
Chris Wilson00e60f22016-08-04 16:32:40 +0100536/**
Chris Wilsone95433c2016-10-28 13:58:27 +0100537 * Waits for rendering to the object to be completed
Chris Wilson00e60f22016-08-04 16:32:40 +0100538 * @obj: i915 gem object
Chris Wilsone95433c2016-10-28 13:58:27 +0100539 * @flags: how to wait (under a lock, for all rendering or just for writes etc)
540 * @timeout: how long to wait
Chris Wilsona0a8b1c2017-11-09 14:06:44 +0000541 * @rps_client: client (user process) to charge for any waitboosting
Chris Wilson00e60f22016-08-04 16:32:40 +0100542 */
543int
Chris Wilsone95433c2016-10-28 13:58:27 +0100544i915_gem_object_wait(struct drm_i915_gem_object *obj,
545 unsigned int flags,
546 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100547 struct intel_rps_client *rps_client)
Chris Wilson00e60f22016-08-04 16:32:40 +0100548{
Chris Wilsone95433c2016-10-28 13:58:27 +0100549 might_sleep();
550#if IS_ENABLED(CONFIG_LOCKDEP)
551 GEM_BUG_ON(debug_locks &&
552 !!lockdep_is_held(&obj->base.dev->struct_mutex) !=
553 !!(flags & I915_WAIT_LOCKED));
554#endif
555 GEM_BUG_ON(timeout < 0);
Chris Wilson00e60f22016-08-04 16:32:40 +0100556
Chris Wilsond07f0e52016-10-28 13:58:44 +0100557 timeout = i915_gem_object_wait_reservation(obj->resv,
558 flags, timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100559 rps_client);
Chris Wilsone95433c2016-10-28 13:58:27 +0100560 return timeout < 0 ? timeout : 0;
Chris Wilson00e60f22016-08-04 16:32:40 +0100561}
562
563static struct intel_rps_client *to_rps_client(struct drm_file *file)
564{
565 struct drm_i915_file_private *fpriv = file->driver_priv;
566
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100567 return &fpriv->rps_client;
Chris Wilson00e60f22016-08-04 16:32:40 +0100568}
569
Chris Wilson00731152014-05-21 12:42:56 +0100570static int
571i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
572 struct drm_i915_gem_pwrite *args,
Chris Wilson03ac84f2016-10-28 13:58:36 +0100573 struct drm_file *file)
Chris Wilson00731152014-05-21 12:42:56 +0100574{
Chris Wilson00731152014-05-21 12:42:56 +0100575 void *vaddr = obj->phys_handle->vaddr + args->offset;
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300576 char __user *user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800577
578 /* We manually control the domain here and pretend that it
579 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
580 */
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -0700581 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000582 if (copy_from_user(vaddr, user_data, args->size))
583 return -EFAULT;
Chris Wilson00731152014-05-21 12:42:56 +0100584
Chris Wilson6a2c4232014-11-04 04:51:40 -0800585 drm_clflush_virt_range(vaddr, args->size);
Chris Wilson10466d22017-01-06 15:22:38 +0000586 i915_gem_chipset_flush(to_i915(obj->base.dev));
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200587
Chris Wilsond59b21e2017-02-22 11:40:49 +0000588 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000589 return 0;
Chris Wilson00731152014-05-21 12:42:56 +0100590}
591
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000592void *i915_gem_object_alloc(struct drm_i915_private *dev_priv)
Chris Wilson42dcedd2012-11-15 11:32:30 +0000593{
Chris Wilsonefab6d82015-04-07 16:20:57 +0100594 return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000595}
596
597void i915_gem_object_free(struct drm_i915_gem_object *obj)
598{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100599 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonefab6d82015-04-07 16:20:57 +0100600 kmem_cache_free(dev_priv->objects, obj);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000601}
602
Dave Airlieff72145b2011-02-07 12:16:14 +1000603static int
604i915_gem_create(struct drm_file *file,
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000605 struct drm_i915_private *dev_priv,
Dave Airlieff72145b2011-02-07 12:16:14 +1000606 uint64_t size,
607 uint32_t *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700608{
Chris Wilson05394f32010-11-08 19:18:58 +0000609 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300610 int ret;
611 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700612
Dave Airlieff72145b2011-02-07 12:16:14 +1000613 size = roundup(size, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200614 if (size == 0)
615 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700616
617 /* Allocate the new object */
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000618 obj = i915_gem_object_create(dev_priv, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100619 if (IS_ERR(obj))
620 return PTR_ERR(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700621
Chris Wilson05394f32010-11-08 19:18:58 +0000622 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100623 /* drop reference from allocate - handle holds it now */
Chris Wilsonf0cd5182016-10-28 13:58:43 +0100624 i915_gem_object_put(obj);
Daniel Vetterd861e332013-07-24 23:25:03 +0200625 if (ret)
626 return ret;
Chris Wilson202f2fe2010-10-14 13:20:40 +0100627
Dave Airlieff72145b2011-02-07 12:16:14 +1000628 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700629 return 0;
630}
631
Dave Airlieff72145b2011-02-07 12:16:14 +1000632int
633i915_gem_dumb_create(struct drm_file *file,
634 struct drm_device *dev,
635 struct drm_mode_create_dumb *args)
636{
637 /* have to work out size/pitch and return them */
Paulo Zanonide45eaf2013-10-18 18:48:24 -0300638 args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000639 args->size = args->pitch * args->height;
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000640 return i915_gem_create(file, to_i915(dev),
Dave Airlieda6b51d2014-12-24 13:11:17 +1000641 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000642}
643
Chris Wilsone27ab732017-06-15 13:38:49 +0100644static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj)
645{
646 return !(obj->cache_level == I915_CACHE_NONE ||
647 obj->cache_level == I915_CACHE_WT);
648}
649
Dave Airlieff72145b2011-02-07 12:16:14 +1000650/**
651 * Creates a new mm object and returns a handle to it.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100652 * @dev: drm device pointer
653 * @data: ioctl data blob
654 * @file: drm file pointer
Dave Airlieff72145b2011-02-07 12:16:14 +1000655 */
656int
657i915_gem_create_ioctl(struct drm_device *dev, void *data,
658 struct drm_file *file)
659{
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000660 struct drm_i915_private *dev_priv = to_i915(dev);
Dave Airlieff72145b2011-02-07 12:16:14 +1000661 struct drm_i915_gem_create *args = data;
Daniel Vetter63ed2cb2012-04-23 16:50:50 +0200662
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000663 i915_gem_flush_free_objects(dev_priv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +0100664
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000665 return i915_gem_create(file, dev_priv,
Dave Airlieda6b51d2014-12-24 13:11:17 +1000666 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000667}
668
Chris Wilsonef749212017-04-12 12:01:10 +0100669static inline enum fb_op_origin
670fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain)
671{
672 return (domain == I915_GEM_DOMAIN_GTT ?
673 obj->frontbuffer_ggtt_origin : ORIGIN_CPU);
674}
675
676static void
677flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains)
678{
679 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
680
681 if (!(obj->base.write_domain & flush_domains))
682 return;
683
684 /* No actual flushing is required for the GTT write domain. Writes
685 * to it "immediately" go to main memory as far as we know, so there's
686 * no chipset flush. It also doesn't land in render cache.
687 *
688 * However, we do have to enforce the order so that all writes through
689 * the GTT land before any writes to the device, such as updates to
690 * the GATT itself.
691 *
692 * We also have to wait a bit for the writes to land from the GTT.
693 * An uncached read (i.e. mmio) seems to be ideal for the round-trip
694 * timing. This issue has only been observed when switching quickly
695 * between GTT writes and CPU reads from inside the kernel on recent hw,
696 * and it appears to only affect discrete GTT blocks (i.e. on LLC
697 * system agents we cannot reproduce this behaviour).
698 */
699 wmb();
700
701 switch (obj->base.write_domain) {
702 case I915_GEM_DOMAIN_GTT:
Chris Wilsonc5ba5b22017-09-07 19:45:20 +0100703 if (!HAS_LLC(dev_priv)) {
Chris Wilsonb69a7842017-08-29 20:25:46 +0100704 intel_runtime_pm_get(dev_priv);
705 spin_lock_irq(&dev_priv->uncore.lock);
Chris Wilsonc5ba5b22017-09-07 19:45:20 +0100706 POSTING_READ_FW(RING_HEAD(dev_priv->engine[RCS]->mmio_base));
Chris Wilsonb69a7842017-08-29 20:25:46 +0100707 spin_unlock_irq(&dev_priv->uncore.lock);
708 intel_runtime_pm_put(dev_priv);
Chris Wilsonef749212017-04-12 12:01:10 +0100709 }
710
711 intel_fb_obj_flush(obj,
712 fb_write_origin(obj, I915_GEM_DOMAIN_GTT));
713 break;
714
715 case I915_GEM_DOMAIN_CPU:
716 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
717 break;
Chris Wilsone27ab732017-06-15 13:38:49 +0100718
719 case I915_GEM_DOMAIN_RENDER:
720 if (gpu_write_needs_clflush(obj))
721 obj->cache_dirty = true;
722 break;
Chris Wilsonef749212017-04-12 12:01:10 +0100723 }
724
725 obj->base.write_domain = 0;
726}
727
Daniel Vetter8c599672011-12-14 13:57:31 +0100728static inline int
Daniel Vetter8461d222011-12-14 13:57:32 +0100729__copy_to_user_swizzled(char __user *cpu_vaddr,
730 const char *gpu_vaddr, int gpu_offset,
731 int length)
732{
733 int ret, cpu_offset = 0;
734
735 while (length > 0) {
736 int cacheline_end = ALIGN(gpu_offset + 1, 64);
737 int this_length = min(cacheline_end - gpu_offset, length);
738 int swizzled_gpu_offset = gpu_offset ^ 64;
739
740 ret = __copy_to_user(cpu_vaddr + cpu_offset,
741 gpu_vaddr + swizzled_gpu_offset,
742 this_length);
743 if (ret)
744 return ret + length;
745
746 cpu_offset += this_length;
747 gpu_offset += this_length;
748 length -= this_length;
749 }
750
751 return 0;
752}
753
754static inline int
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700755__copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
756 const char __user *cpu_vaddr,
Daniel Vetter8c599672011-12-14 13:57:31 +0100757 int length)
758{
759 int ret, cpu_offset = 0;
760
761 while (length > 0) {
762 int cacheline_end = ALIGN(gpu_offset + 1, 64);
763 int this_length = min(cacheline_end - gpu_offset, length);
764 int swizzled_gpu_offset = gpu_offset ^ 64;
765
766 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
767 cpu_vaddr + cpu_offset,
768 this_length);
769 if (ret)
770 return ret + length;
771
772 cpu_offset += this_length;
773 gpu_offset += this_length;
774 length -= this_length;
775 }
776
777 return 0;
778}
779
Brad Volkin4c914c02014-02-18 10:15:45 -0800780/*
781 * Pins the specified object's pages and synchronizes the object with
782 * GPU accesses. Sets needs_clflush to non-zero if the caller should
783 * flush the object from the CPU cache.
784 */
785int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
Chris Wilson43394c72016-08-18 17:16:47 +0100786 unsigned int *needs_clflush)
Brad Volkin4c914c02014-02-18 10:15:45 -0800787{
788 int ret;
789
Chris Wilsone95433c2016-10-28 13:58:27 +0100790 lockdep_assert_held(&obj->base.dev->struct_mutex);
Brad Volkin4c914c02014-02-18 10:15:45 -0800791
Chris Wilsone95433c2016-10-28 13:58:27 +0100792 *needs_clflush = 0;
Chris Wilson43394c72016-08-18 17:16:47 +0100793 if (!i915_gem_object_has_struct_page(obj))
794 return -ENODEV;
Brad Volkin4c914c02014-02-18 10:15:45 -0800795
Chris Wilsone95433c2016-10-28 13:58:27 +0100796 ret = i915_gem_object_wait(obj,
797 I915_WAIT_INTERRUPTIBLE |
798 I915_WAIT_LOCKED,
799 MAX_SCHEDULE_TIMEOUT,
800 NULL);
Chris Wilsonc13d87e2016-07-20 09:21:15 +0100801 if (ret)
802 return ret;
803
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100804 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100805 if (ret)
806 return ret;
807
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100808 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ ||
809 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000810 ret = i915_gem_object_set_to_cpu_domain(obj, false);
811 if (ret)
812 goto err_unpin;
813 else
814 goto out;
815 }
816
Chris Wilsonef749212017-04-12 12:01:10 +0100817 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100818
Chris Wilson43394c72016-08-18 17:16:47 +0100819 /* If we're not in the cpu read domain, set ourself into the gtt
820 * read domain and manually flush cachelines (if required). This
821 * optimizes for the case when the gpu will dirty the data
822 * anyway again before the next pread happens.
823 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100824 if (!obj->cache_dirty &&
825 !(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000826 *needs_clflush = CLFLUSH_BEFORE;
Brad Volkin4c914c02014-02-18 10:15:45 -0800827
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000828out:
Chris Wilson97649512016-08-18 17:16:50 +0100829 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100830 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100831
832err_unpin:
833 i915_gem_object_unpin_pages(obj);
834 return ret;
Chris Wilson43394c72016-08-18 17:16:47 +0100835}
836
837int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
838 unsigned int *needs_clflush)
839{
840 int ret;
841
Chris Wilsone95433c2016-10-28 13:58:27 +0100842 lockdep_assert_held(&obj->base.dev->struct_mutex);
843
Chris Wilson43394c72016-08-18 17:16:47 +0100844 *needs_clflush = 0;
845 if (!i915_gem_object_has_struct_page(obj))
846 return -ENODEV;
847
Chris Wilsone95433c2016-10-28 13:58:27 +0100848 ret = i915_gem_object_wait(obj,
849 I915_WAIT_INTERRUPTIBLE |
850 I915_WAIT_LOCKED |
851 I915_WAIT_ALL,
852 MAX_SCHEDULE_TIMEOUT,
853 NULL);
Chris Wilson43394c72016-08-18 17:16:47 +0100854 if (ret)
855 return ret;
856
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100857 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100858 if (ret)
859 return ret;
860
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100861 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE ||
862 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000863 ret = i915_gem_object_set_to_cpu_domain(obj, true);
864 if (ret)
865 goto err_unpin;
866 else
867 goto out;
868 }
869
Chris Wilsonef749212017-04-12 12:01:10 +0100870 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100871
Chris Wilson43394c72016-08-18 17:16:47 +0100872 /* If we're not in the cpu write domain, set ourself into the
873 * gtt write domain and manually flush cachelines (as required).
874 * This optimizes for the case when the gpu will use the data
875 * right away and we therefore have to clflush anyway.
876 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100877 if (!obj->cache_dirty) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000878 *needs_clflush |= CLFLUSH_AFTER;
Chris Wilson43394c72016-08-18 17:16:47 +0100879
Chris Wilsone27ab732017-06-15 13:38:49 +0100880 /*
881 * Same trick applies to invalidate partially written
882 * cachelines read before writing.
883 */
884 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
885 *needs_clflush |= CLFLUSH_BEFORE;
886 }
Chris Wilson43394c72016-08-18 17:16:47 +0100887
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000888out:
Chris Wilson43394c72016-08-18 17:16:47 +0100889 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100890 obj->mm.dirty = true;
Chris Wilson97649512016-08-18 17:16:50 +0100891 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100892 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100893
894err_unpin:
895 i915_gem_object_unpin_pages(obj);
896 return ret;
Brad Volkin4c914c02014-02-18 10:15:45 -0800897}
898
Daniel Vetter23c18c72012-03-25 19:47:42 +0200899static void
900shmem_clflush_swizzled_range(char *addr, unsigned long length,
901 bool swizzled)
902{
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200903 if (unlikely(swizzled)) {
Daniel Vetter23c18c72012-03-25 19:47:42 +0200904 unsigned long start = (unsigned long) addr;
905 unsigned long end = (unsigned long) addr + length;
906
907 /* For swizzling simply ensure that we always flush both
908 * channels. Lame, but simple and it works. Swizzled
909 * pwrite/pread is far from a hotpath - current userspace
910 * doesn't use it at all. */
911 start = round_down(start, 128);
912 end = round_up(end, 128);
913
914 drm_clflush_virt_range((void *)start, end - start);
915 } else {
916 drm_clflush_virt_range(addr, length);
917 }
918
919}
920
Daniel Vetterd174bd62012-03-25 19:47:40 +0200921/* Only difference to the fast-path function is that this can handle bit17
922 * and uses non-atomic copy and kmap functions. */
923static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100924shmem_pread_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +0200925 char __user *user_data,
926 bool page_do_bit17_swizzling, bool needs_clflush)
927{
928 char *vaddr;
929 int ret;
930
931 vaddr = kmap(page);
932 if (needs_clflush)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100933 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +0200934 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200935
936 if (page_do_bit17_swizzling)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100937 ret = __copy_to_user_swizzled(user_data, vaddr, offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200938 else
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100939 ret = __copy_to_user(user_data, vaddr + offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200940 kunmap(page);
941
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100942 return ret ? - EFAULT : 0;
Daniel Vetterd174bd62012-03-25 19:47:40 +0200943}
944
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100945static int
946shmem_pread(struct page *page, int offset, int length, char __user *user_data,
947 bool page_do_bit17_swizzling, bool needs_clflush)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530948{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100949 int ret;
950
951 ret = -ENODEV;
952 if (!page_do_bit17_swizzling) {
953 char *vaddr = kmap_atomic(page);
954
955 if (needs_clflush)
956 drm_clflush_virt_range(vaddr + offset, length);
957 ret = __copy_to_user_inatomic(user_data, vaddr + offset, length);
958 kunmap_atomic(vaddr);
959 }
960 if (ret == 0)
961 return 0;
962
963 return shmem_pread_slow(page, offset, length, user_data,
964 page_do_bit17_swizzling, needs_clflush);
965}
966
967static int
968i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
969 struct drm_i915_gem_pread *args)
970{
971 char __user *user_data;
972 u64 remain;
973 unsigned int obj_do_bit17_swizzling;
974 unsigned int needs_clflush;
975 unsigned int idx, offset;
976 int ret;
977
978 obj_do_bit17_swizzling = 0;
979 if (i915_gem_object_needs_bit17_swizzle(obj))
980 obj_do_bit17_swizzling = BIT(17);
981
982 ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex);
983 if (ret)
984 return ret;
985
986 ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
987 mutex_unlock(&obj->base.dev->struct_mutex);
988 if (ret)
989 return ret;
990
991 remain = args->size;
992 user_data = u64_to_user_ptr(args->data_ptr);
993 offset = offset_in_page(args->offset);
994 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
995 struct page *page = i915_gem_object_get_page(obj, idx);
996 int length;
997
998 length = remain;
999 if (offset + length > PAGE_SIZE)
1000 length = PAGE_SIZE - offset;
1001
1002 ret = shmem_pread(page, offset, length, user_data,
1003 page_to_phys(page) & obj_do_bit17_swizzling,
1004 needs_clflush);
1005 if (ret)
1006 break;
1007
1008 remain -= length;
1009 user_data += length;
1010 offset = 0;
1011 }
1012
1013 i915_gem_obj_finish_shmem_access(obj);
1014 return ret;
1015}
1016
1017static inline bool
1018gtt_user_read(struct io_mapping *mapping,
1019 loff_t base, int offset,
1020 char __user *user_data, int length)
1021{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001022 void __iomem *vaddr;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001023 unsigned long unwritten;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301024
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301025 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001026 vaddr = io_mapping_map_atomic_wc(mapping, base);
1027 unwritten = __copy_to_user_inatomic(user_data,
1028 (void __force *)vaddr + offset,
1029 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001030 io_mapping_unmap_atomic(vaddr);
1031 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001032 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1033 unwritten = copy_to_user(user_data,
1034 (void __force *)vaddr + offset,
1035 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001036 io_mapping_unmap(vaddr);
1037 }
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301038 return unwritten;
1039}
1040
1041static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001042i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
1043 const struct drm_i915_gem_pread *args)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301044{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001045 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1046 struct i915_ggtt *ggtt = &i915->ggtt;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301047 struct drm_mm_node node;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001048 struct i915_vma *vma;
1049 void __user *user_data;
1050 u64 remain, offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301051 int ret;
1052
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001053 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1054 if (ret)
1055 return ret;
1056
1057 intel_runtime_pm_get(i915);
1058 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001059 PIN_MAPPABLE |
1060 PIN_NONFAULT |
1061 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001062 if (!IS_ERR(vma)) {
1063 node.start = i915_ggtt_offset(vma);
1064 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001065 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001066 if (ret) {
1067 i915_vma_unpin(vma);
1068 vma = ERR_PTR(ret);
1069 }
1070 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001071 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001072 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301073 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001074 goto out_unlock;
1075 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301076 }
1077
1078 ret = i915_gem_object_set_to_gtt_domain(obj, false);
1079 if (ret)
1080 goto out_unpin;
1081
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001082 mutex_unlock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301083
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001084 user_data = u64_to_user_ptr(args->data_ptr);
1085 remain = args->size;
1086 offset = args->offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301087
1088 while (remain > 0) {
1089 /* Operation in this page
1090 *
1091 * page_base = page offset within aperture
1092 * page_offset = offset within page
1093 * page_length = bytes to copy for this page
1094 */
1095 u32 page_base = node.start;
1096 unsigned page_offset = offset_in_page(offset);
1097 unsigned page_length = PAGE_SIZE - page_offset;
1098 page_length = remain < page_length ? remain : page_length;
1099 if (node.allocated) {
1100 wmb();
1101 ggtt->base.insert_page(&ggtt->base,
1102 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001103 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301104 wmb();
1105 } else {
1106 page_base += offset & PAGE_MASK;
1107 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001108
1109 if (gtt_user_read(&ggtt->mappable, page_base, page_offset,
1110 user_data, page_length)) {
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301111 ret = -EFAULT;
1112 break;
1113 }
1114
1115 remain -= page_length;
1116 user_data += page_length;
1117 offset += page_length;
1118 }
1119
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001120 mutex_lock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301121out_unpin:
1122 if (node.allocated) {
1123 wmb();
1124 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001125 node.start, node.size);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301126 remove_mappable_node(&node);
1127 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001128 i915_vma_unpin(vma);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301129 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001130out_unlock:
1131 intel_runtime_pm_put(i915);
1132 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +01001133
Eric Anholteb014592009-03-10 11:44:52 -07001134 return ret;
1135}
1136
Eric Anholt673a3942008-07-30 12:06:12 -07001137/**
1138 * Reads data from the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001139 * @dev: drm device pointer
1140 * @data: ioctl data blob
1141 * @file: drm file pointer
Eric Anholt673a3942008-07-30 12:06:12 -07001142 *
1143 * On error, the contents of *data are undefined.
1144 */
1145int
1146i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001147 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001148{
1149 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001150 struct drm_i915_gem_object *obj;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001151 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001152
Chris Wilson51311d02010-11-17 09:10:42 +00001153 if (args->size == 0)
1154 return 0;
1155
1156 if (!access_ok(VERIFY_WRITE,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001157 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001158 args->size))
1159 return -EFAULT;
1160
Chris Wilson03ac0642016-07-20 13:31:51 +01001161 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001162 if (!obj)
1163 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001164
Chris Wilson7dcd2492010-09-26 20:21:44 +01001165 /* Bounds check source. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001166 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001167 ret = -EINVAL;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001168 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001169 }
1170
Chris Wilsondb53a302011-02-03 11:57:46 +00001171 trace_i915_gem_object_pread(obj, args->offset, args->size);
1172
Chris Wilsone95433c2016-10-28 13:58:27 +01001173 ret = i915_gem_object_wait(obj,
1174 I915_WAIT_INTERRUPTIBLE,
1175 MAX_SCHEDULE_TIMEOUT,
1176 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001177 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001178 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001179
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001180 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001181 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001182 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001183
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001184 ret = i915_gem_shmem_pread(obj, args);
Chris Wilson9c870d02016-10-24 13:42:15 +01001185 if (ret == -EFAULT || ret == -ENODEV)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001186 ret = i915_gem_gtt_pread(obj, args);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301187
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001188 i915_gem_object_unpin_pages(obj);
1189out:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001190 i915_gem_object_put(obj);
Eric Anholteb014592009-03-10 11:44:52 -07001191 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001192}
1193
Keith Packard0839ccb2008-10-30 19:38:48 -07001194/* This is the fast write path which cannot handle
1195 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001196 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001197
Chris Wilsonfe115622016-10-28 13:58:40 +01001198static inline bool
1199ggtt_write(struct io_mapping *mapping,
1200 loff_t base, int offset,
1201 char __user *user_data, int length)
Keith Packard0839ccb2008-10-30 19:38:48 -07001202{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001203 void __iomem *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -07001204 unsigned long unwritten;
1205
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -07001206 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001207 vaddr = io_mapping_map_atomic_wc(mapping, base);
1208 unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset,
Keith Packard0839ccb2008-10-30 19:38:48 -07001209 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001210 io_mapping_unmap_atomic(vaddr);
1211 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001212 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1213 unwritten = copy_from_user((void __force *)vaddr + offset,
1214 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001215 io_mapping_unmap(vaddr);
1216 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001217
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001218 return unwritten;
1219}
1220
Eric Anholt3de09aa2009-03-09 09:42:23 -07001221/**
1222 * This is the fast pwrite path, where we copy the data directly from the
1223 * user into the GTT, uncached.
Chris Wilsonfe115622016-10-28 13:58:40 +01001224 * @obj: i915 GEM object
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001225 * @args: pwrite arguments structure
Eric Anholt3de09aa2009-03-09 09:42:23 -07001226 */
Eric Anholt673a3942008-07-30 12:06:12 -07001227static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001228i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
1229 const struct drm_i915_gem_pwrite *args)
Eric Anholt673a3942008-07-30 12:06:12 -07001230{
Chris Wilsonfe115622016-10-28 13:58:40 +01001231 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301232 struct i915_ggtt *ggtt = &i915->ggtt;
1233 struct drm_mm_node node;
Chris Wilsonfe115622016-10-28 13:58:40 +01001234 struct i915_vma *vma;
1235 u64 remain, offset;
1236 void __user *user_data;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301237 int ret;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301238
Chris Wilsonfe115622016-10-28 13:58:40 +01001239 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1240 if (ret)
1241 return ret;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001242
Chris Wilson8bd818152017-10-19 07:37:33 +01001243 if (i915_gem_object_has_struct_page(obj)) {
1244 /*
1245 * Avoid waking the device up if we can fallback, as
1246 * waking/resuming is very slow (worst-case 10-100 ms
1247 * depending on PCI sleeps and our own resume time).
1248 * This easily dwarfs any performance advantage from
1249 * using the cache bypass of indirect GGTT access.
1250 */
1251 if (!intel_runtime_pm_get_if_in_use(i915)) {
1252 ret = -EFAULT;
1253 goto out_unlock;
1254 }
1255 } else {
1256 /* No backing pages, no fallback, we must force GGTT access */
1257 intel_runtime_pm_get(i915);
1258 }
1259
Chris Wilson058d88c2016-08-15 10:49:06 +01001260 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001261 PIN_MAPPABLE |
1262 PIN_NONFAULT |
1263 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001264 if (!IS_ERR(vma)) {
1265 node.start = i915_ggtt_offset(vma);
1266 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001267 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001268 if (ret) {
1269 i915_vma_unpin(vma);
1270 vma = ERR_PTR(ret);
1271 }
1272 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001273 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001274 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301275 if (ret)
Chris Wilson8bd818152017-10-19 07:37:33 +01001276 goto out_rpm;
Chris Wilsonfe115622016-10-28 13:58:40 +01001277 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301278 }
Daniel Vetter935aaa62012-03-25 19:47:35 +02001279
1280 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1281 if (ret)
1282 goto out_unpin;
1283
Chris Wilsonfe115622016-10-28 13:58:40 +01001284 mutex_unlock(&i915->drm.struct_mutex);
1285
Chris Wilsonb19482d2016-08-18 17:16:43 +01001286 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Paulo Zanoni063e4e62015-02-13 17:23:45 -02001287
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301288 user_data = u64_to_user_ptr(args->data_ptr);
1289 offset = args->offset;
1290 remain = args->size;
1291 while (remain) {
Eric Anholt673a3942008-07-30 12:06:12 -07001292 /* Operation in this page
1293 *
Keith Packard0839ccb2008-10-30 19:38:48 -07001294 * page_base = page offset within aperture
1295 * page_offset = offset within page
1296 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -07001297 */
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301298 u32 page_base = node.start;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001299 unsigned int page_offset = offset_in_page(offset);
1300 unsigned int page_length = PAGE_SIZE - page_offset;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301301 page_length = remain < page_length ? remain : page_length;
1302 if (node.allocated) {
1303 wmb(); /* flush the write before we modify the GGTT */
1304 ggtt->base.insert_page(&ggtt->base,
1305 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
1306 node.start, I915_CACHE_NONE, 0);
1307 wmb(); /* flush modifications to the GGTT (insert_page) */
1308 } else {
1309 page_base += offset & PAGE_MASK;
1310 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001311 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -07001312 * source page isn't available. Return the error and we'll
1313 * retry in the slow path.
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301314 * If the object is non-shmem backed, we retry again with the
1315 * path that handles page fault.
Keith Packard0839ccb2008-10-30 19:38:48 -07001316 */
Chris Wilsonfe115622016-10-28 13:58:40 +01001317 if (ggtt_write(&ggtt->mappable, page_base, page_offset,
1318 user_data, page_length)) {
1319 ret = -EFAULT;
1320 break;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001321 }
Eric Anholt673a3942008-07-30 12:06:12 -07001322
Keith Packard0839ccb2008-10-30 19:38:48 -07001323 remain -= page_length;
1324 user_data += page_length;
1325 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -07001326 }
Chris Wilsond59b21e2017-02-22 11:40:49 +00001327 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001328
1329 mutex_lock(&i915->drm.struct_mutex);
Daniel Vetter935aaa62012-03-25 19:47:35 +02001330out_unpin:
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301331 if (node.allocated) {
1332 wmb();
1333 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001334 node.start, node.size);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301335 remove_mappable_node(&node);
1336 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001337 i915_vma_unpin(vma);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301338 }
Chris Wilson8bd818152017-10-19 07:37:33 +01001339out_rpm:
Chris Wilson9c870d02016-10-24 13:42:15 +01001340 intel_runtime_pm_put(i915);
Chris Wilson8bd818152017-10-19 07:37:33 +01001341out_unlock:
Chris Wilsonfe115622016-10-28 13:58:40 +01001342 mutex_unlock(&i915->drm.struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -07001343 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001344}
1345
Eric Anholt673a3942008-07-30 12:06:12 -07001346static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001347shmem_pwrite_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +02001348 char __user *user_data,
1349 bool page_do_bit17_swizzling,
1350 bool needs_clflush_before,
1351 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -07001352{
Daniel Vetterd174bd62012-03-25 19:47:40 +02001353 char *vaddr;
1354 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001355
Daniel Vetterd174bd62012-03-25 19:47:40 +02001356 vaddr = kmap(page);
Daniel Vettere7e58eb2012-03-25 19:47:43 +02001357 if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
Chris Wilsonfe115622016-10-28 13:58:40 +01001358 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001359 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001360 if (page_do_bit17_swizzling)
Chris Wilsonfe115622016-10-28 13:58:40 +01001361 ret = __copy_from_user_swizzled(vaddr, offset, user_data,
1362 length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001363 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001364 ret = __copy_from_user(vaddr + offset, user_data, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001365 if (needs_clflush_after)
Chris Wilsonfe115622016-10-28 13:58:40 +01001366 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001367 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001368 kunmap(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001369
Chris Wilson755d2212012-09-04 21:02:55 +01001370 return ret ? -EFAULT : 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001371}
1372
Chris Wilsonfe115622016-10-28 13:58:40 +01001373/* Per-page copy function for the shmem pwrite fastpath.
1374 * Flushes invalid cachelines before writing to the target if
1375 * needs_clflush_before is set and flushes out any written cachelines after
1376 * writing if needs_clflush is set.
1377 */
Eric Anholt40123c12009-03-09 13:42:30 -07001378static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001379shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
1380 bool page_do_bit17_swizzling,
1381 bool needs_clflush_before,
1382 bool needs_clflush_after)
Eric Anholt40123c12009-03-09 13:42:30 -07001383{
Chris Wilsonfe115622016-10-28 13:58:40 +01001384 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001385
Chris Wilsonfe115622016-10-28 13:58:40 +01001386 ret = -ENODEV;
1387 if (!page_do_bit17_swizzling) {
1388 char *vaddr = kmap_atomic(page);
1389
1390 if (needs_clflush_before)
1391 drm_clflush_virt_range(vaddr + offset, len);
1392 ret = __copy_from_user_inatomic(vaddr + offset, user_data, len);
1393 if (needs_clflush_after)
1394 drm_clflush_virt_range(vaddr + offset, len);
1395
1396 kunmap_atomic(vaddr);
1397 }
1398 if (ret == 0)
1399 return ret;
1400
1401 return shmem_pwrite_slow(page, offset, len, user_data,
1402 page_do_bit17_swizzling,
1403 needs_clflush_before,
1404 needs_clflush_after);
1405}
1406
1407static int
1408i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
1409 const struct drm_i915_gem_pwrite *args)
1410{
1411 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1412 void __user *user_data;
1413 u64 remain;
1414 unsigned int obj_do_bit17_swizzling;
1415 unsigned int partial_cacheline_write;
1416 unsigned int needs_clflush;
1417 unsigned int offset, idx;
1418 int ret;
1419
1420 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
Chris Wilson43394c72016-08-18 17:16:47 +01001421 if (ret)
1422 return ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001423
Chris Wilsonfe115622016-10-28 13:58:40 +01001424 ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
1425 mutex_unlock(&i915->drm.struct_mutex);
1426 if (ret)
1427 return ret;
1428
1429 obj_do_bit17_swizzling = 0;
1430 if (i915_gem_object_needs_bit17_swizzle(obj))
1431 obj_do_bit17_swizzling = BIT(17);
1432
1433 /* If we don't overwrite a cacheline completely we need to be
1434 * careful to have up-to-date data by first clflushing. Don't
1435 * overcomplicate things and flush the entire patch.
1436 */
1437 partial_cacheline_write = 0;
1438 if (needs_clflush & CLFLUSH_BEFORE)
1439 partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
1440
Chris Wilson43394c72016-08-18 17:16:47 +01001441 user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson43394c72016-08-18 17:16:47 +01001442 remain = args->size;
Chris Wilsonfe115622016-10-28 13:58:40 +01001443 offset = offset_in_page(args->offset);
1444 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1445 struct page *page = i915_gem_object_get_page(obj, idx);
1446 int length;
Eric Anholt40123c12009-03-09 13:42:30 -07001447
Chris Wilsonfe115622016-10-28 13:58:40 +01001448 length = remain;
1449 if (offset + length > PAGE_SIZE)
1450 length = PAGE_SIZE - offset;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001451
Chris Wilsonfe115622016-10-28 13:58:40 +01001452 ret = shmem_pwrite(page, offset, length, user_data,
1453 page_to_phys(page) & obj_do_bit17_swizzling,
1454 (offset | length) & partial_cacheline_write,
1455 needs_clflush & CLFLUSH_AFTER);
1456 if (ret)
Chris Wilson9da3da62012-06-01 15:20:22 +01001457 break;
1458
Chris Wilsonfe115622016-10-28 13:58:40 +01001459 remain -= length;
1460 user_data += length;
1461 offset = 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001462 }
1463
Chris Wilsond59b21e2017-02-22 11:40:49 +00001464 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001465 i915_gem_obj_finish_shmem_access(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001466 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001467}
1468
1469/**
1470 * Writes data to the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001471 * @dev: drm device
1472 * @data: ioctl data blob
1473 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001474 *
1475 * On error, the contents of the buffer that were to be modified are undefined.
1476 */
1477int
1478i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001479 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001480{
1481 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001482 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +00001483 int ret;
1484
1485 if (args->size == 0)
1486 return 0;
1487
1488 if (!access_ok(VERIFY_READ,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001489 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001490 args->size))
1491 return -EFAULT;
1492
Chris Wilson03ac0642016-07-20 13:31:51 +01001493 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001494 if (!obj)
1495 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001496
Chris Wilson7dcd2492010-09-26 20:21:44 +01001497 /* Bounds check destination. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001498 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001499 ret = -EINVAL;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001500 goto err;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001501 }
1502
Chris Wilsondb53a302011-02-03 11:57:46 +00001503 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
1504
Chris Wilson7c55e2c2017-03-07 12:03:38 +00001505 ret = -ENODEV;
1506 if (obj->ops->pwrite)
1507 ret = obj->ops->pwrite(obj, args);
1508 if (ret != -ENODEV)
1509 goto err;
1510
Chris Wilsone95433c2016-10-28 13:58:27 +01001511 ret = i915_gem_object_wait(obj,
1512 I915_WAIT_INTERRUPTIBLE |
1513 I915_WAIT_ALL,
1514 MAX_SCHEDULE_TIMEOUT,
1515 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001516 if (ret)
1517 goto err;
1518
Chris Wilsonfe115622016-10-28 13:58:40 +01001519 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001520 if (ret)
Chris Wilsonfe115622016-10-28 13:58:40 +01001521 goto err;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001522
Daniel Vetter935aaa62012-03-25 19:47:35 +02001523 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -07001524 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1525 * it would end up going through the fenced access, and we'll get
1526 * different detiling behavior between reading and writing.
1527 * pread/pwrite currently are reading and writing from the CPU
1528 * perspective, requiring manual detiling by the client.
1529 */
Chris Wilson6eae0052016-06-20 15:05:52 +01001530 if (!i915_gem_object_has_struct_page(obj) ||
Chris Wilson9c870d02016-10-24 13:42:15 +01001531 cpu_write_needs_clflush(obj))
Daniel Vetter935aaa62012-03-25 19:47:35 +02001532 /* Note that the gtt paths might fail with non-page-backed user
1533 * pointers (e.g. gtt mappings when moving data between
Chris Wilson9c870d02016-10-24 13:42:15 +01001534 * textures). Fallback to the shmem path in that case.
1535 */
Chris Wilsonfe115622016-10-28 13:58:40 +01001536 ret = i915_gem_gtt_pwrite_fast(obj, args);
Eric Anholt673a3942008-07-30 12:06:12 -07001537
Chris Wilsond1054ee2016-07-16 18:42:36 +01001538 if (ret == -EFAULT || ret == -ENOSPC) {
Chris Wilson6a2c4232014-11-04 04:51:40 -08001539 if (obj->phys_handle)
1540 ret = i915_gem_phys_pwrite(obj, args, file);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301541 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001542 ret = i915_gem_shmem_pwrite(obj, args);
Chris Wilson6a2c4232014-11-04 04:51:40 -08001543 }
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001544
Chris Wilsonfe115622016-10-28 13:58:40 +01001545 i915_gem_object_unpin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001546err:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001547 i915_gem_object_put(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001548 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001549}
1550
Chris Wilson40e62d52016-10-28 13:58:41 +01001551static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
1552{
1553 struct drm_i915_private *i915;
1554 struct list_head *list;
1555 struct i915_vma *vma;
1556
Chris Wilsonf2123812017-10-16 12:40:37 +01001557 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
1558
Chris Wilson40e62d52016-10-28 13:58:41 +01001559 list_for_each_entry(vma, &obj->vma_list, obj_link) {
1560 if (!i915_vma_is_ggtt(vma))
Chris Wilson28f412e2016-12-23 14:57:55 +00001561 break;
Chris Wilson40e62d52016-10-28 13:58:41 +01001562
1563 if (i915_vma_is_active(vma))
1564 continue;
1565
1566 if (!drm_mm_node_allocated(&vma->node))
1567 continue;
1568
1569 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
1570 }
1571
1572 i915 = to_i915(obj->base.dev);
Chris Wilsonf2123812017-10-16 12:40:37 +01001573 spin_lock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001574 list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list;
Chris Wilsonf2123812017-10-16 12:40:37 +01001575 list_move_tail(&obj->mm.link, list);
1576 spin_unlock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001577}
1578
Eric Anholt673a3942008-07-30 12:06:12 -07001579/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001580 * Called when user space prepares to use an object with the CPU, either
1581 * through the mmap ioctl's mapping or a GTT mapping.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001582 * @dev: drm device
1583 * @data: ioctl data blob
1584 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001585 */
1586int
1587i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001588 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001589{
1590 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001591 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001592 uint32_t read_domains = args->read_domains;
1593 uint32_t write_domain = args->write_domain;
Chris Wilson40e62d52016-10-28 13:58:41 +01001594 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07001595
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001596 /* Only handle setting domains to types used by the CPU. */
Chris Wilsonb8f90962016-08-05 10:14:07 +01001597 if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001598 return -EINVAL;
1599
1600 /* Having something in the write domain implies it's in the read
1601 * domain, and only that read domain. Enforce that in the request.
1602 */
1603 if (write_domain != 0 && read_domains != write_domain)
1604 return -EINVAL;
1605
Chris Wilson03ac0642016-07-20 13:31:51 +01001606 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001607 if (!obj)
1608 return -ENOENT;
Jesse Barnes652c3932009-08-17 13:31:43 -07001609
Chris Wilson3236f572012-08-24 09:35:09 +01001610 /* Try to flush the object off the GPU without holding the lock.
1611 * We will repeat the flush holding the lock in the normal manner
1612 * to catch cases where we are gazumped.
1613 */
Chris Wilson40e62d52016-10-28 13:58:41 +01001614 err = i915_gem_object_wait(obj,
Chris Wilsone95433c2016-10-28 13:58:27 +01001615 I915_WAIT_INTERRUPTIBLE |
1616 (write_domain ? I915_WAIT_ALL : 0),
1617 MAX_SCHEDULE_TIMEOUT,
1618 to_rps_client(file));
Chris Wilson40e62d52016-10-28 13:58:41 +01001619 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001620 goto out;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001621
Tina Zhanga03f3952017-11-14 10:25:13 +00001622 /*
1623 * Proxy objects do not control access to the backing storage, ergo
1624 * they cannot be used as a means to manipulate the cache domain
1625 * tracking for that backing storage. The proxy object is always
1626 * considered to be outside of any cache domain.
1627 */
1628 if (i915_gem_object_is_proxy(obj)) {
1629 err = -ENXIO;
1630 goto out;
1631 }
1632
1633 /*
1634 * Flush and acquire obj->pages so that we are coherent through
Chris Wilson40e62d52016-10-28 13:58:41 +01001635 * direct access in memory with previous cached writes through
1636 * shmemfs and that our cache domain tracking remains valid.
1637 * For example, if the obj->filp was moved to swap without us
1638 * being notified and releasing the pages, we would mistakenly
1639 * continue to assume that the obj remained out of the CPU cached
1640 * domain.
1641 */
1642 err = i915_gem_object_pin_pages(obj);
1643 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001644 goto out;
Chris Wilson40e62d52016-10-28 13:58:41 +01001645
1646 err = i915_mutex_lock_interruptible(dev);
1647 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001648 goto out_unpin;
Chris Wilson3236f572012-08-24 09:35:09 +01001649
Chris Wilsone22d8e32017-04-12 12:01:11 +01001650 if (read_domains & I915_GEM_DOMAIN_WC)
1651 err = i915_gem_object_set_to_wc_domain(obj, write_domain);
1652 else if (read_domains & I915_GEM_DOMAIN_GTT)
1653 err = i915_gem_object_set_to_gtt_domain(obj, write_domain);
Chris Wilson43566de2015-01-02 16:29:29 +05301654 else
Chris Wilsone22d8e32017-04-12 12:01:11 +01001655 err = i915_gem_object_set_to_cpu_domain(obj, write_domain);
Chris Wilson40e62d52016-10-28 13:58:41 +01001656
1657 /* And bump the LRU for this access */
1658 i915_gem_object_bump_inactive_ggtt(obj);
1659
1660 mutex_unlock(&dev->struct_mutex);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001661
Daniel Vetter031b6982015-06-26 19:35:16 +02001662 if (write_domain != 0)
Chris Wilsonef749212017-04-12 12:01:10 +01001663 intel_fb_obj_invalidate(obj,
1664 fb_write_origin(obj, write_domain));
Daniel Vetter031b6982015-06-26 19:35:16 +02001665
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001666out_unpin:
Chris Wilson40e62d52016-10-28 13:58:41 +01001667 i915_gem_object_unpin_pages(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001668out:
1669 i915_gem_object_put(obj);
Chris Wilson40e62d52016-10-28 13:58:41 +01001670 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07001671}
1672
1673/**
1674 * Called when user space has done writes to this buffer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001675 * @dev: drm device
1676 * @data: ioctl data blob
1677 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001678 */
1679int
1680i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001681 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001682{
1683 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001684 struct drm_i915_gem_object *obj;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001685
Chris Wilson03ac0642016-07-20 13:31:51 +01001686 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonc21724c2016-08-05 10:14:19 +01001687 if (!obj)
1688 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001689
Tina Zhanga03f3952017-11-14 10:25:13 +00001690 /*
1691 * Proxy objects are barred from CPU access, so there is no
1692 * need to ban sw_finish as it is a nop.
1693 */
1694
Eric Anholt673a3942008-07-30 12:06:12 -07001695 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001696 i915_gem_object_flush_if_display(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001697 i915_gem_object_put(obj);
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001698
1699 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001700}
1701
1702/**
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001703 * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
1704 * it is mapped to.
1705 * @dev: drm device
1706 * @data: ioctl data blob
1707 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001708 *
1709 * While the mapping holds a reference on the contents of the object, it doesn't
1710 * imply a ref on the object itself.
Daniel Vetter34367382014-10-16 12:28:18 +02001711 *
1712 * IMPORTANT:
1713 *
1714 * DRM driver writers who look a this function as an example for how to do GEM
1715 * mmap support, please don't implement mmap support like here. The modern way
1716 * to implement DRM mmap support is with an mmap offset ioctl (like
1717 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
1718 * That way debug tooling like valgrind will understand what's going on, hiding
1719 * the mmap call in a driver private ioctl will break that. The i915 driver only
1720 * does cpu mmaps this way because we didn't know better.
Eric Anholt673a3942008-07-30 12:06:12 -07001721 */
1722int
1723i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001724 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001725{
1726 struct drm_i915_gem_mmap *args = data;
Chris Wilson03ac0642016-07-20 13:31:51 +01001727 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001728 unsigned long addr;
1729
Akash Goel1816f922015-01-02 16:29:30 +05301730 if (args->flags & ~(I915_MMAP_WC))
1731 return -EINVAL;
1732
Borislav Petkov568a58e2016-03-29 17:42:01 +02001733 if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
Akash Goel1816f922015-01-02 16:29:30 +05301734 return -ENODEV;
1735
Chris Wilson03ac0642016-07-20 13:31:51 +01001736 obj = i915_gem_object_lookup(file, args->handle);
1737 if (!obj)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001738 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001739
Daniel Vetter1286ff72012-05-10 15:25:09 +02001740 /* prime objects have no backing filp to GEM mmap
1741 * pages from.
1742 */
Chris Wilson03ac0642016-07-20 13:31:51 +01001743 if (!obj->base.filp) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001744 i915_gem_object_put(obj);
Tina Zhang274b2462017-11-14 10:25:12 +00001745 return -ENXIO;
Daniel Vetter1286ff72012-05-10 15:25:09 +02001746 }
1747
Chris Wilson03ac0642016-07-20 13:31:51 +01001748 addr = vm_mmap(obj->base.filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001749 PROT_READ | PROT_WRITE, MAP_SHARED,
1750 args->offset);
Akash Goel1816f922015-01-02 16:29:30 +05301751 if (args->flags & I915_MMAP_WC) {
1752 struct mm_struct *mm = current->mm;
1753 struct vm_area_struct *vma;
1754
Michal Hocko80a89a52016-05-23 16:26:11 -07001755 if (down_write_killable(&mm->mmap_sem)) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001756 i915_gem_object_put(obj);
Michal Hocko80a89a52016-05-23 16:26:11 -07001757 return -EINTR;
1758 }
Akash Goel1816f922015-01-02 16:29:30 +05301759 vma = find_vma(mm, addr);
1760 if (vma)
1761 vma->vm_page_prot =
1762 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
1763 else
1764 addr = -ENOMEM;
1765 up_write(&mm->mmap_sem);
Chris Wilsonaeecc962016-06-17 14:46:39 -03001766
1767 /* This may race, but that's ok, it only gets set */
Chris Wilson50349242016-08-18 17:17:04 +01001768 WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
Akash Goel1816f922015-01-02 16:29:30 +05301769 }
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001770 i915_gem_object_put(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001771 if (IS_ERR((void *)addr))
1772 return addr;
1773
1774 args->addr_ptr = (uint64_t) addr;
1775
1776 return 0;
1777}
1778
Chris Wilson03af84f2016-08-18 17:17:01 +01001779static unsigned int tile_row_pages(struct drm_i915_gem_object *obj)
1780{
Chris Wilson6649a0b2017-01-09 16:16:08 +00001781 return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT;
Chris Wilson03af84f2016-08-18 17:17:01 +01001782}
1783
Jesse Barnesde151cf2008-11-12 10:03:55 -08001784/**
Chris Wilson4cc69072016-08-25 19:05:19 +01001785 * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
1786 *
1787 * A history of the GTT mmap interface:
1788 *
1789 * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
1790 * aligned and suitable for fencing, and still fit into the available
1791 * mappable space left by the pinned display objects. A classic problem
1792 * we called the page-fault-of-doom where we would ping-pong between
1793 * two objects that could not fit inside the GTT and so the memcpy
1794 * would page one object in at the expense of the other between every
1795 * single byte.
1796 *
1797 * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
1798 * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
1799 * object is too large for the available space (or simply too large
1800 * for the mappable aperture!), a view is created instead and faulted
1801 * into userspace. (This view is aligned and sized appropriately for
1802 * fenced access.)
1803 *
Chris Wilsone22d8e32017-04-12 12:01:11 +01001804 * 2 - Recognise WC as a separate cache domain so that we can flush the
1805 * delayed writes via GTT before performing direct access via WC.
1806 *
Chris Wilson4cc69072016-08-25 19:05:19 +01001807 * Restrictions:
1808 *
1809 * * snoopable objects cannot be accessed via the GTT. It can cause machine
1810 * hangs on some architectures, corruption on others. An attempt to service
1811 * a GTT page fault from a snoopable object will generate a SIGBUS.
1812 *
1813 * * the object must be able to fit into RAM (physical memory, though no
1814 * limited to the mappable aperture).
1815 *
1816 *
1817 * Caveats:
1818 *
1819 * * a new GTT page fault will synchronize rendering from the GPU and flush
1820 * all data to system memory. Subsequent access will not be synchronized.
1821 *
1822 * * all mappings are revoked on runtime device suspend.
1823 *
1824 * * there are only 8, 16 or 32 fence registers to share between all users
1825 * (older machines require fence register for display and blitter access
1826 * as well). Contention of the fence registers will cause the previous users
1827 * to be unmapped and any new access will generate new page faults.
1828 *
1829 * * running out of memory while servicing a fault may generate a SIGBUS,
1830 * rather than the expected SIGSEGV.
1831 */
1832int i915_gem_mmap_gtt_version(void)
1833{
Chris Wilsone22d8e32017-04-12 12:01:11 +01001834 return 2;
Chris Wilson4cc69072016-08-25 19:05:19 +01001835}
1836
Chris Wilson2d4281b2017-01-10 09:56:32 +00001837static inline struct i915_ggtt_view
1838compute_partial_view(struct drm_i915_gem_object *obj,
Chris Wilson2d4281b2017-01-10 09:56:32 +00001839 pgoff_t page_offset,
1840 unsigned int chunk)
1841{
1842 struct i915_ggtt_view view;
1843
1844 if (i915_gem_object_is_tiled(obj))
1845 chunk = roundup(chunk, tile_row_pages(obj));
1846
Chris Wilson2d4281b2017-01-10 09:56:32 +00001847 view.type = I915_GGTT_VIEW_PARTIAL;
Chris Wilson8bab11932017-01-14 00:28:25 +00001848 view.partial.offset = rounddown(page_offset, chunk);
1849 view.partial.size =
Chris Wilson2d4281b2017-01-10 09:56:32 +00001850 min_t(unsigned int, chunk,
Chris Wilson8bab11932017-01-14 00:28:25 +00001851 (obj->base.size >> PAGE_SHIFT) - view.partial.offset);
Chris Wilson2d4281b2017-01-10 09:56:32 +00001852
1853 /* If the partial covers the entire object, just create a normal VMA. */
1854 if (chunk >= obj->base.size >> PAGE_SHIFT)
1855 view.type = I915_GGTT_VIEW_NORMAL;
1856
1857 return view;
1858}
1859
Chris Wilson4cc69072016-08-25 19:05:19 +01001860/**
Jesse Barnesde151cf2008-11-12 10:03:55 -08001861 * i915_gem_fault - fault a page into the GTT
Geliang Tangd9072a32015-09-15 05:58:44 -07001862 * @vmf: fault info
Jesse Barnesde151cf2008-11-12 10:03:55 -08001863 *
1864 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1865 * from userspace. The fault handler takes care of binding the object to
1866 * the GTT (if needed), allocating and programming a fence register (again,
1867 * only if needed based on whether the old reg is still valid or the object
1868 * is tiled) and inserting a new PTE into the faulting process.
1869 *
1870 * Note that the faulting process may involve evicting existing objects
1871 * from the GTT and/or fence registers to make room. So performance may
1872 * suffer if the GTT working set is large or there are few fence registers
1873 * left.
Chris Wilson4cc69072016-08-25 19:05:19 +01001874 *
1875 * The current feature set supported by i915_gem_fault() and thus GTT mmaps
1876 * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
Jesse Barnesde151cf2008-11-12 10:03:55 -08001877 */
Dave Jiang11bac802017-02-24 14:56:41 -08001878int i915_gem_fault(struct vm_fault *vmf)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001879{
Chris Wilson03af84f2016-08-18 17:17:01 +01001880#define MIN_CHUNK_PAGES ((1 << 20) >> PAGE_SHIFT) /* 1 MiB */
Dave Jiang11bac802017-02-24 14:56:41 -08001881 struct vm_area_struct *area = vmf->vma;
Chris Wilson058d88c2016-08-15 10:49:06 +01001882 struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
Chris Wilson05394f32010-11-08 19:18:58 +00001883 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001884 struct drm_i915_private *dev_priv = to_i915(dev);
1885 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001886 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Chris Wilson058d88c2016-08-15 10:49:06 +01001887 struct i915_vma *vma;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001888 pgoff_t page_offset;
Chris Wilson82118872016-08-18 17:17:05 +01001889 unsigned int flags;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001890 int ret;
Paulo Zanonif65c9162013-11-27 18:20:34 -02001891
Jesse Barnesde151cf2008-11-12 10:03:55 -08001892 /* We don't use vmf->pgoff since that has the fake offset */
Jan Kara1a29d852016-12-14 15:07:01 -08001893 page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001894
Chris Wilsondb53a302011-02-03 11:57:46 +00001895 trace_i915_gem_object_fault(obj, page_offset, true, write);
1896
Chris Wilson6e4930f2014-02-07 18:37:06 -02001897 /* Try to flush the object off the GPU first without holding the lock.
Chris Wilsonb8f90962016-08-05 10:14:07 +01001898 * Upon acquiring the lock, we will perform our sanity checks and then
Chris Wilson6e4930f2014-02-07 18:37:06 -02001899 * repeat the flush holding the lock in the normal manner to catch cases
1900 * where we are gazumped.
1901 */
Chris Wilsone95433c2016-10-28 13:58:27 +01001902 ret = i915_gem_object_wait(obj,
1903 I915_WAIT_INTERRUPTIBLE,
1904 MAX_SCHEDULE_TIMEOUT,
1905 NULL);
Chris Wilson6e4930f2014-02-07 18:37:06 -02001906 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001907 goto err;
1908
Chris Wilson40e62d52016-10-28 13:58:41 +01001909 ret = i915_gem_object_pin_pages(obj);
1910 if (ret)
1911 goto err;
1912
Chris Wilsonb8f90962016-08-05 10:14:07 +01001913 intel_runtime_pm_get(dev_priv);
1914
1915 ret = i915_mutex_lock_interruptible(dev);
1916 if (ret)
1917 goto err_rpm;
Chris Wilson6e4930f2014-02-07 18:37:06 -02001918
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001919 /* Access to snoopable pages through the GTT is incoherent. */
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00001920 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) {
Chris Wilsonddeff6e2014-05-28 16:16:41 +01001921 ret = -EFAULT;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001922 goto err_unlock;
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001923 }
1924
Chris Wilson82118872016-08-18 17:17:05 +01001925 /* If the object is smaller than a couple of partial vma, it is
1926 * not worth only creating a single partial vma - we may as well
1927 * clear enough space for the full object.
1928 */
1929 flags = PIN_MAPPABLE;
1930 if (obj->base.size > 2 * MIN_CHUNK_PAGES << PAGE_SHIFT)
1931 flags |= PIN_NONBLOCK | PIN_NONFAULT;
1932
Chris Wilsona61007a2016-08-18 17:17:02 +01001933 /* Now pin it into the GTT as needed */
Chris Wilson82118872016-08-18 17:17:05 +01001934 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, flags);
Chris Wilsona61007a2016-08-18 17:17:02 +01001935 if (IS_ERR(vma)) {
Chris Wilsona61007a2016-08-18 17:17:02 +01001936 /* Use a partial view if it is bigger than available space */
Chris Wilson2d4281b2017-01-10 09:56:32 +00001937 struct i915_ggtt_view view =
Chris Wilson8201c1f2017-01-10 09:56:33 +00001938 compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES);
Chris Wilsonaa136d92016-08-18 17:17:03 +01001939
Chris Wilson50349242016-08-18 17:17:04 +01001940 /* Userspace is now writing through an untracked VMA, abandon
1941 * all hope that the hardware is able to track future writes.
1942 */
1943 obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
1944
Chris Wilsona61007a2016-08-18 17:17:02 +01001945 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE);
1946 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001947 if (IS_ERR(vma)) {
1948 ret = PTR_ERR(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001949 goto err_unlock;
Chris Wilson058d88c2016-08-15 10:49:06 +01001950 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001951
Chris Wilsonc9839302012-11-20 10:45:17 +00001952 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1953 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001954 goto err_unpin;
Chris Wilsonc9839302012-11-20 10:45:17 +00001955
Chris Wilson3bd40732017-10-09 09:43:56 +01001956 ret = i915_vma_pin_fence(vma);
Chris Wilsonc9839302012-11-20 10:45:17 +00001957 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001958 goto err_unpin;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001959
Chris Wilsonb90b91d2014-06-10 12:14:40 +01001960 /* Finally, remap it using the new GTT offset */
Chris Wilsonc58305a2016-08-19 16:54:28 +01001961 ret = remap_io_mapping(area,
Chris Wilson8bab11932017-01-14 00:28:25 +00001962 area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT),
Chris Wilsonc58305a2016-08-19 16:54:28 +01001963 (ggtt->mappable_base + vma->node.start) >> PAGE_SHIFT,
1964 min_t(u64, vma->size, area->vm_end - area->vm_start),
1965 &ggtt->mappable);
Chris Wilsona65adaf2017-10-09 09:43:57 +01001966 if (ret)
1967 goto err_fence;
Chris Wilsona61007a2016-08-18 17:17:02 +01001968
Chris Wilsona65adaf2017-10-09 09:43:57 +01001969 /* Mark as being mmapped into userspace for later revocation */
1970 assert_rpm_wakelock_held(dev_priv);
1971 if (!i915_vma_set_userfault(vma) && !obj->userfault_count++)
1972 list_add(&obj->userfault_link, &dev_priv->mm.userfault_list);
1973 GEM_BUG_ON(!obj->userfault_count);
1974
1975err_fence:
Chris Wilson3bd40732017-10-09 09:43:56 +01001976 i915_vma_unpin_fence(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001977err_unpin:
Chris Wilson058d88c2016-08-15 10:49:06 +01001978 __i915_vma_unpin(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001979err_unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001980 mutex_unlock(&dev->struct_mutex);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001981err_rpm:
1982 intel_runtime_pm_put(dev_priv);
Chris Wilson40e62d52016-10-28 13:58:41 +01001983 i915_gem_object_unpin_pages(obj);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001984err:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001985 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001986 case -EIO:
Daniel Vetter2232f032014-09-04 09:36:18 +02001987 /*
1988 * We eat errors when the gpu is terminally wedged to avoid
1989 * userspace unduly crashing (gl has no provisions for mmaps to
1990 * fail). But any other -EIO isn't ours (e.g. swap in failure)
1991 * and so needs to be reported.
1992 */
1993 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
Paulo Zanonif65c9162013-11-27 18:20:34 -02001994 ret = VM_FAULT_SIGBUS;
1995 break;
1996 }
Chris Wilson045e7692010-11-07 09:18:22 +00001997 case -EAGAIN:
Daniel Vetter571c6082013-09-12 17:57:28 +02001998 /*
1999 * EAGAIN means the gpu is hung and we'll wait for the error
2000 * handler to reset everything when re-faulting in
2001 * i915_mutex_lock_interruptible.
Chris Wilsond9bc7e92011-02-07 13:09:31 +00002002 */
Chris Wilsonc7150892009-09-23 00:43:56 +01002003 case 0:
2004 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00002005 case -EINTR:
Dmitry Rogozhkine79e0fe2012-10-03 17:15:26 +03002006 case -EBUSY:
2007 /*
2008 * EBUSY is ok: this just means that another thread
2009 * already did the job.
2010 */
Paulo Zanonif65c9162013-11-27 18:20:34 -02002011 ret = VM_FAULT_NOPAGE;
2012 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002013 case -ENOMEM:
Paulo Zanonif65c9162013-11-27 18:20:34 -02002014 ret = VM_FAULT_OOM;
2015 break;
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02002016 case -ENOSPC:
Chris Wilson45d67812014-01-31 11:34:57 +00002017 case -EFAULT:
Paulo Zanonif65c9162013-11-27 18:20:34 -02002018 ret = VM_FAULT_SIGBUS;
2019 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002020 default:
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02002021 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
Paulo Zanonif65c9162013-11-27 18:20:34 -02002022 ret = VM_FAULT_SIGBUS;
2023 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002024 }
Paulo Zanonif65c9162013-11-27 18:20:34 -02002025 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002026}
2027
Chris Wilsona65adaf2017-10-09 09:43:57 +01002028static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj)
2029{
2030 struct i915_vma *vma;
2031
2032 GEM_BUG_ON(!obj->userfault_count);
2033
2034 obj->userfault_count = 0;
2035 list_del(&obj->userfault_link);
2036 drm_vma_node_unmap(&obj->base.vma_node,
2037 obj->base.dev->anon_inode->i_mapping);
2038
2039 list_for_each_entry(vma, &obj->vma_list, obj_link) {
2040 if (!i915_vma_is_ggtt(vma))
2041 break;
2042
2043 i915_vma_unset_userfault(vma);
2044 }
2045}
2046
Jesse Barnesde151cf2008-11-12 10:03:55 -08002047/**
Chris Wilson901782b2009-07-10 08:18:50 +01002048 * i915_gem_release_mmap - remove physical page mappings
2049 * @obj: obj in question
2050 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002051 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01002052 * relinquish ownership of the pages back to the system.
2053 *
2054 * It is vital that we remove the page mapping if we have mapped a tiled
2055 * object through the GTT and then lose the fence register due to
2056 * resource pressure. Similarly if the object has been moved out of the
2057 * aperture, than pages mapped into userspace must be revoked. Removing the
2058 * mapping will then trigger a page fault on the next user access, allowing
2059 * fixup by i915_gem_fault().
2060 */
Eric Anholtd05ca302009-07-10 13:02:26 -07002061void
Chris Wilson05394f32010-11-08 19:18:58 +00002062i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01002063{
Chris Wilson275f0392016-10-24 13:42:14 +01002064 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson275f0392016-10-24 13:42:14 +01002065
Chris Wilson349f2cc2016-04-13 17:35:12 +01002066 /* Serialisation between user GTT access and our code depends upon
2067 * revoking the CPU's PTE whilst the mutex is held. The next user
2068 * pagefault then has to wait until we release the mutex.
Chris Wilson9c870d02016-10-24 13:42:15 +01002069 *
2070 * Note that RPM complicates somewhat by adding an additional
2071 * requirement that operations to the GGTT be made holding the RPM
2072 * wakeref.
Chris Wilson349f2cc2016-04-13 17:35:12 +01002073 */
Chris Wilson275f0392016-10-24 13:42:14 +01002074 lockdep_assert_held(&i915->drm.struct_mutex);
Chris Wilson9c870d02016-10-24 13:42:15 +01002075 intel_runtime_pm_get(i915);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002076
Chris Wilsona65adaf2017-10-09 09:43:57 +01002077 if (!obj->userfault_count)
Chris Wilson9c870d02016-10-24 13:42:15 +01002078 goto out;
Chris Wilson901782b2009-07-10 08:18:50 +01002079
Chris Wilsona65adaf2017-10-09 09:43:57 +01002080 __i915_gem_object_release_mmap(obj);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002081
2082 /* Ensure that the CPU's PTE are revoked and there are not outstanding
2083 * memory transactions from userspace before we return. The TLB
2084 * flushing implied above by changing the PTE above *should* be
2085 * sufficient, an extra barrier here just provides us with a bit
2086 * of paranoid documentation about our requirement to serialise
2087 * memory writes before touching registers / GSM.
2088 */
2089 wmb();
Chris Wilson9c870d02016-10-24 13:42:15 +01002090
2091out:
2092 intel_runtime_pm_put(i915);
Chris Wilson901782b2009-07-10 08:18:50 +01002093}
2094
Chris Wilson7c108fd2016-10-24 13:42:18 +01002095void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv)
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002096{
Chris Wilson3594a3e2016-10-24 13:42:16 +01002097 struct drm_i915_gem_object *obj, *on;
Chris Wilson7c108fd2016-10-24 13:42:18 +01002098 int i;
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002099
Chris Wilson3594a3e2016-10-24 13:42:16 +01002100 /*
2101 * Only called during RPM suspend. All users of the userfault_list
2102 * must be holding an RPM wakeref to ensure that this can not
2103 * run concurrently with themselves (and use the struct_mutex for
2104 * protection between themselves).
2105 */
2106
2107 list_for_each_entry_safe(obj, on,
Chris Wilsona65adaf2017-10-09 09:43:57 +01002108 &dev_priv->mm.userfault_list, userfault_link)
2109 __i915_gem_object_release_mmap(obj);
Chris Wilson7c108fd2016-10-24 13:42:18 +01002110
2111 /* The fence will be lost when the device powers down. If any were
2112 * in use by hardware (i.e. they are pinned), we should not be powering
2113 * down! All other fences will be reacquired by the user upon waking.
2114 */
2115 for (i = 0; i < dev_priv->num_fence_regs; i++) {
2116 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
2117
Chris Wilsone0ec3ec2017-02-03 12:57:17 +00002118 /* Ideally we want to assert that the fence register is not
2119 * live at this point (i.e. that no piece of code will be
2120 * trying to write through fence + GTT, as that both violates
2121 * our tracking of activity and associated locking/barriers,
2122 * but also is illegal given that the hw is powered down).
2123 *
2124 * Previously we used reg->pin_count as a "liveness" indicator.
2125 * That is not sufficient, and we need a more fine-grained
2126 * tool if we want to have a sanity check here.
2127 */
Chris Wilson7c108fd2016-10-24 13:42:18 +01002128
2129 if (!reg->vma)
2130 continue;
2131
Chris Wilsona65adaf2017-10-09 09:43:57 +01002132 GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
Chris Wilson7c108fd2016-10-24 13:42:18 +01002133 reg->dirty = true;
2134 }
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002135}
2136
Chris Wilsond8cb5082012-08-11 15:41:03 +01002137static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
2138{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002139 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002140 int err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002141
Chris Wilsonf3f61842016-08-05 10:14:14 +01002142 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002143 if (likely(!err))
Chris Wilsonf3f61842016-08-05 10:14:14 +01002144 return 0;
Daniel Vetterda494d72012-12-20 15:11:16 +01002145
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002146 /* Attempt to reap some mmap space from dead objects */
2147 do {
2148 err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
2149 if (err)
2150 break;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002151
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002152 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002153 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002154 if (!err)
2155 break;
2156
2157 } while (flush_delayed_work(&dev_priv->gt.retire_work));
Daniel Vetterda494d72012-12-20 15:11:16 +01002158
Chris Wilsonf3f61842016-08-05 10:14:14 +01002159 return err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002160}
2161
2162static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
2163{
Chris Wilsond8cb5082012-08-11 15:41:03 +01002164 drm_gem_free_mmap_offset(&obj->base);
2165}
2166
Dave Airlieda6b51d2014-12-24 13:11:17 +10002167int
Dave Airlieff72145b2011-02-07 12:16:14 +10002168i915_gem_mmap_gtt(struct drm_file *file,
2169 struct drm_device *dev,
Dave Airlieda6b51d2014-12-24 13:11:17 +10002170 uint32_t handle,
Dave Airlieff72145b2011-02-07 12:16:14 +10002171 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002172{
Chris Wilson05394f32010-11-08 19:18:58 +00002173 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002174 int ret;
2175
Chris Wilson03ac0642016-07-20 13:31:51 +01002176 obj = i915_gem_object_lookup(file, handle);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002177 if (!obj)
2178 return -ENOENT;
Chris Wilsonab182822009-09-22 18:46:17 +01002179
Chris Wilsond8cb5082012-08-11 15:41:03 +01002180 ret = i915_gem_object_create_mmap_offset(obj);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002181 if (ret == 0)
2182 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002183
Chris Wilsonf0cd5182016-10-28 13:58:43 +01002184 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002185 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002186}
2187
Dave Airlieff72145b2011-02-07 12:16:14 +10002188/**
2189 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
2190 * @dev: DRM device
2191 * @data: GTT mapping ioctl data
2192 * @file: GEM object info
2193 *
2194 * Simply returns the fake offset to userspace so it can mmap it.
2195 * The mmap call will end up in drm_gem_mmap(), which will set things
2196 * up so we can get faults in the handler above.
2197 *
2198 * The fault handler will take care of binding the object into the GTT
2199 * (since it may have been evicted to make room for something), allocating
2200 * a fence register, and mapping the appropriate aperture address into
2201 * userspace.
2202 */
2203int
2204i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
2205 struct drm_file *file)
2206{
2207 struct drm_i915_gem_mmap_gtt *args = data;
2208
Dave Airlieda6b51d2014-12-24 13:11:17 +10002209 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
Dave Airlieff72145b2011-02-07 12:16:14 +10002210}
2211
Daniel Vetter225067e2012-08-20 10:23:20 +02002212/* Immediately discard the backing storage */
2213static void
2214i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01002215{
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002216 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02002217
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002218 if (obj->base.filp == NULL)
2219 return;
2220
Daniel Vetter225067e2012-08-20 10:23:20 +02002221 /* Our goal here is to return as much of the memory as
2222 * is possible back to the system as we are called from OOM.
2223 * To do this we must instruct the shmfs to drop all of its
2224 * backing pages, *now*.
Chris Wilsone5281cc2010-10-28 13:45:36 +01002225 */
Chris Wilson55372522014-03-25 13:23:06 +00002226 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002227 obj->mm.madv = __I915_MADV_PURGED;
Chris Wilson4e5462e2017-03-07 13:20:31 +00002228 obj->mm.pages = ERR_PTR(-EFAULT);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002229}
Chris Wilsone5281cc2010-10-28 13:45:36 +01002230
Chris Wilson55372522014-03-25 13:23:06 +00002231/* Try to discard unwanted pages */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002232void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
Daniel Vetter225067e2012-08-20 10:23:20 +02002233{
Chris Wilson55372522014-03-25 13:23:06 +00002234 struct address_space *mapping;
2235
Chris Wilson1233e2d2016-10-28 13:58:37 +01002236 lockdep_assert_held(&obj->mm.lock);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002237 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilson1233e2d2016-10-28 13:58:37 +01002238
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002239 switch (obj->mm.madv) {
Chris Wilson55372522014-03-25 13:23:06 +00002240 case I915_MADV_DONTNEED:
2241 i915_gem_object_truncate(obj);
2242 case __I915_MADV_PURGED:
2243 return;
2244 }
2245
2246 if (obj->base.filp == NULL)
2247 return;
2248
Al Viro93c76a32015-12-04 23:45:44 -05002249 mapping = obj->base.filp->f_mapping,
Chris Wilson55372522014-03-25 13:23:06 +00002250 invalidate_mapping_pages(mapping, 0, (loff_t)-1);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002251}
2252
Chris Wilson5cdf5882010-09-27 15:51:07 +01002253static void
Chris Wilson03ac84f2016-10-28 13:58:36 +01002254i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj,
2255 struct sg_table *pages)
Eric Anholt673a3942008-07-30 12:06:12 -07002256{
Dave Gordon85d12252016-05-20 11:54:06 +01002257 struct sgt_iter sgt_iter;
2258 struct page *page;
Daniel Vetter1286ff72012-05-10 15:25:09 +02002259
Chris Wilsone5facdf2016-12-23 14:57:57 +00002260 __i915_gem_object_release_shmem(obj, pages, true);
Eric Anholt856fa192009-03-19 14:10:50 -07002261
Chris Wilson03ac84f2016-10-28 13:58:36 +01002262 i915_gem_gtt_finish_pages(obj, pages);
Imre Deake2273302015-07-09 12:59:05 +03002263
Daniel Vetter6dacfd22011-09-12 21:30:02 +02002264 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002265 i915_gem_object_save_bit_17_swizzle(obj, pages);
Eric Anholt280b7132009-03-12 16:56:27 -07002266
Chris Wilson03ac84f2016-10-28 13:58:36 +01002267 for_each_sgt_page(page, sgt_iter, pages) {
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002268 if (obj->mm.dirty)
Chris Wilson9da3da62012-06-01 15:20:22 +01002269 set_page_dirty(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002270
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002271 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson9da3da62012-06-01 15:20:22 +01002272 mark_page_accessed(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002273
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002274 put_page(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002275 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002276 obj->mm.dirty = false;
Eric Anholt673a3942008-07-30 12:06:12 -07002277
Chris Wilson03ac84f2016-10-28 13:58:36 +01002278 sg_free_table(pages);
2279 kfree(pages);
Chris Wilson37e680a2012-06-07 15:38:42 +01002280}
2281
Chris Wilson96d77632016-10-28 13:58:33 +01002282static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
2283{
2284 struct radix_tree_iter iter;
Ville Syrjäläc23aa712017-09-01 20:12:51 +03002285 void __rcu **slot;
Chris Wilson96d77632016-10-28 13:58:33 +01002286
Chris Wilsonbea6e982017-10-26 14:00:31 +01002287 rcu_read_lock();
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002288 radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
2289 radix_tree_delete(&obj->mm.get_page.radix, iter.index);
Chris Wilsonbea6e982017-10-26 14:00:31 +01002290 rcu_read_unlock();
Chris Wilson96d77632016-10-28 13:58:33 +01002291}
2292
Chris Wilson548625e2016-11-01 12:11:34 +00002293void __i915_gem_object_put_pages(struct drm_i915_gem_object *obj,
2294 enum i915_mm_subclass subclass)
Chris Wilson37e680a2012-06-07 15:38:42 +01002295{
Chris Wilsonf2123812017-10-16 12:40:37 +01002296 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002297 struct sg_table *pages;
Chris Wilson37e680a2012-06-07 15:38:42 +01002298
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002299 if (i915_gem_object_has_pinned_pages(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002300 return;
Chris Wilsona5570172012-09-04 21:02:54 +01002301
Chris Wilson15717de2016-08-04 07:52:26 +01002302 GEM_BUG_ON(obj->bind_count);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002303 if (!i915_gem_object_has_pages(obj))
Chris Wilson1233e2d2016-10-28 13:58:37 +01002304 return;
2305
2306 /* May be called by shrinker from within get_pages() (on another bo) */
Chris Wilson548625e2016-11-01 12:11:34 +00002307 mutex_lock_nested(&obj->mm.lock, subclass);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002308 if (unlikely(atomic_read(&obj->mm.pages_pin_count)))
2309 goto unlock;
Ben Widawsky3e123022013-07-31 17:00:04 -07002310
Chris Wilsona2165e32012-12-03 11:49:00 +00002311 /* ->put_pages might need to allocate memory for the bit17 swizzle
2312 * array, hence protect them from being reaped by removing them from gtt
2313 * lists early. */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002314 pages = fetch_and_zero(&obj->mm.pages);
2315 GEM_BUG_ON(!pages);
Chris Wilsona2165e32012-12-03 11:49:00 +00002316
Chris Wilsonf2123812017-10-16 12:40:37 +01002317 spin_lock(&i915->mm.obj_lock);
2318 list_del(&obj->mm.link);
2319 spin_unlock(&i915->mm.obj_lock);
2320
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002321 if (obj->mm.mapping) {
Chris Wilson4b30cb22016-08-18 17:16:42 +01002322 void *ptr;
2323
Chris Wilson0ce81782017-05-17 13:09:59 +01002324 ptr = page_mask_bits(obj->mm.mapping);
Chris Wilson4b30cb22016-08-18 17:16:42 +01002325 if (is_vmalloc_addr(ptr))
2326 vunmap(ptr);
Chris Wilsonfb8621d2016-04-08 12:11:14 +01002327 else
Chris Wilson4b30cb22016-08-18 17:16:42 +01002328 kunmap(kmap_to_page(ptr));
2329
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002330 obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002331 }
2332
Chris Wilson96d77632016-10-28 13:58:33 +01002333 __i915_gem_object_reset_page_iter(obj);
2334
Chris Wilson4e5462e2017-03-07 13:20:31 +00002335 if (!IS_ERR(pages))
2336 obj->ops->put_pages(obj, pages);
2337
Matthew Aulda5c081662017-10-06 23:18:18 +01002338 obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0;
2339
Chris Wilson1233e2d2016-10-28 13:58:37 +01002340unlock:
2341 mutex_unlock(&obj->mm.lock);
Chris Wilson6c085a72012-08-20 11:40:46 +02002342}
2343
Chris Wilson935a2f72017-02-13 17:15:13 +00002344static bool i915_sg_trim(struct sg_table *orig_st)
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002345{
2346 struct sg_table new_st;
2347 struct scatterlist *sg, *new_sg;
2348 unsigned int i;
2349
2350 if (orig_st->nents == orig_st->orig_nents)
Chris Wilson935a2f72017-02-13 17:15:13 +00002351 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002352
Chris Wilson8bfc478f2016-12-23 14:57:58 +00002353 if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN))
Chris Wilson935a2f72017-02-13 17:15:13 +00002354 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002355
2356 new_sg = new_st.sgl;
2357 for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
2358 sg_set_page(new_sg, sg_page(sg), sg->length, 0);
2359 /* called before being DMA mapped, no need to copy sg->dma_* */
2360 new_sg = sg_next(new_sg);
2361 }
Chris Wilsonc2dc6cc2016-12-19 12:43:46 +00002362 GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002363
2364 sg_free_table(orig_st);
2365
2366 *orig_st = new_st;
Chris Wilson935a2f72017-02-13 17:15:13 +00002367 return true;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002368}
2369
Matthew Auldb91b09e2017-10-06 23:18:17 +01002370static int i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002371{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002372 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsond766ef52016-12-19 12:43:45 +00002373 const unsigned long page_count = obj->base.size / PAGE_SIZE;
2374 unsigned long i;
Eric Anholt673a3942008-07-30 12:06:12 -07002375 struct address_space *mapping;
Chris Wilson9da3da62012-06-01 15:20:22 +01002376 struct sg_table *st;
2377 struct scatterlist *sg;
Dave Gordon85d12252016-05-20 11:54:06 +01002378 struct sgt_iter sgt_iter;
Eric Anholt673a3942008-07-30 12:06:12 -07002379 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002380 unsigned long last_pfn = 0; /* suppress gcc warning */
Tvrtko Ursulin56024522017-08-03 10:14:17 +01002381 unsigned int max_segment = i915_sg_segment_size();
Matthew Auld84e89782017-10-09 12:00:24 +01002382 unsigned int sg_page_sizes;
Chris Wilson4846bf02017-06-09 12:03:46 +01002383 gfp_t noreclaim;
Imre Deake2273302015-07-09 12:59:05 +03002384 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002385
Chris Wilson6c085a72012-08-20 11:40:46 +02002386 /* Assert that the object is not currently in any GPU domain. As it
2387 * wasn't in the GTT, there shouldn't be any way it could have been in
2388 * a GPU cache
2389 */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002390 GEM_BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2391 GEM_BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
Chris Wilson6c085a72012-08-20 11:40:46 +02002392
Chris Wilson9da3da62012-06-01 15:20:22 +01002393 st = kmalloc(sizeof(*st), GFP_KERNEL);
2394 if (st == NULL)
Matthew Auldb91b09e2017-10-06 23:18:17 +01002395 return -ENOMEM;
Eric Anholt673a3942008-07-30 12:06:12 -07002396
Chris Wilsond766ef52016-12-19 12:43:45 +00002397rebuild_st:
Chris Wilson9da3da62012-06-01 15:20:22 +01002398 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
Chris Wilson9da3da62012-06-01 15:20:22 +01002399 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002400 return -ENOMEM;
Chris Wilson9da3da62012-06-01 15:20:22 +01002401 }
2402
2403 /* Get the list of pages out of our struct file. They'll be pinned
2404 * at this point until we release them.
2405 *
2406 * Fail silently without starting the shrinker
2407 */
Al Viro93c76a32015-12-04 23:45:44 -05002408 mapping = obj->base.filp->f_mapping;
Chris Wilson0f6ab552017-06-09 12:03:48 +01002409 noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM);
Chris Wilson4846bf02017-06-09 12:03:46 +01002410 noreclaim |= __GFP_NORETRY | __GFP_NOWARN;
2411
Imre Deak90797e62013-02-18 19:28:03 +02002412 sg = st->sgl;
2413 st->nents = 0;
Matthew Auld84e89782017-10-09 12:00:24 +01002414 sg_page_sizes = 0;
Imre Deak90797e62013-02-18 19:28:03 +02002415 for (i = 0; i < page_count; i++) {
Chris Wilson4846bf02017-06-09 12:03:46 +01002416 const unsigned int shrink[] = {
2417 I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE,
2418 0,
2419 }, *s = shrink;
2420 gfp_t gfp = noreclaim;
2421
2422 do {
Chris Wilson6c085a72012-08-20 11:40:46 +02002423 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
Chris Wilson4846bf02017-06-09 12:03:46 +01002424 if (likely(!IS_ERR(page)))
2425 break;
2426
2427 if (!*s) {
2428 ret = PTR_ERR(page);
2429 goto err_sg;
2430 }
2431
Chris Wilson912d5722017-09-06 16:19:30 -07002432 i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++);
Chris Wilson4846bf02017-06-09 12:03:46 +01002433 cond_resched();
Chris Wilson24f8e002017-03-22 11:05:21 +00002434
Chris Wilson6c085a72012-08-20 11:40:46 +02002435 /* We've tried hard to allocate the memory by reaping
2436 * our own buffer, now let the real VM do its job and
2437 * go down in flames if truly OOM.
Chris Wilson24f8e002017-03-22 11:05:21 +00002438 *
2439 * However, since graphics tend to be disposable,
2440 * defer the oom here by reporting the ENOMEM back
2441 * to userspace.
Chris Wilson6c085a72012-08-20 11:40:46 +02002442 */
Chris Wilson4846bf02017-06-09 12:03:46 +01002443 if (!*s) {
2444 /* reclaim and warn, but no oom */
2445 gfp = mapping_gfp_mask(mapping);
Chris Wilsoneaf41802017-06-09 12:03:47 +01002446
2447 /* Our bo are always dirty and so we require
2448 * kswapd to reclaim our pages (direct reclaim
2449 * does not effectively begin pageout of our
2450 * buffers on its own). However, direct reclaim
2451 * only waits for kswapd when under allocation
2452 * congestion. So as a result __GFP_RECLAIM is
2453 * unreliable and fails to actually reclaim our
2454 * dirty pages -- unless you try over and over
2455 * again with !__GFP_NORETRY. However, we still
2456 * want to fail this allocation rather than
2457 * trigger the out-of-memory killer and for
Michal Hockodbb32952017-07-12 14:36:55 -07002458 * this we want __GFP_RETRY_MAYFAIL.
Chris Wilsoneaf41802017-06-09 12:03:47 +01002459 */
Michal Hockodbb32952017-07-12 14:36:55 -07002460 gfp |= __GFP_RETRY_MAYFAIL;
Imre Deake2273302015-07-09 12:59:05 +03002461 }
Chris Wilson4846bf02017-06-09 12:03:46 +01002462 } while (1);
2463
Chris Wilson871dfbd2016-10-11 09:20:21 +01002464 if (!i ||
2465 sg->length >= max_segment ||
2466 page_to_pfn(page) != last_pfn + 1) {
Matthew Aulda5c081662017-10-06 23:18:18 +01002467 if (i) {
Matthew Auld84e89782017-10-09 12:00:24 +01002468 sg_page_sizes |= sg->length;
Imre Deak90797e62013-02-18 19:28:03 +02002469 sg = sg_next(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002470 }
Imre Deak90797e62013-02-18 19:28:03 +02002471 st->nents++;
2472 sg_set_page(sg, page, PAGE_SIZE, 0);
2473 } else {
2474 sg->length += PAGE_SIZE;
2475 }
2476 last_pfn = page_to_pfn(page);
Daniel Vetter3bbbe702013-10-07 17:15:45 -03002477
2478 /* Check that the i965g/gm workaround works. */
2479 WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
Eric Anholt673a3942008-07-30 12:06:12 -07002480 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002481 if (sg) { /* loop terminated early; short sg table */
Matthew Auld84e89782017-10-09 12:00:24 +01002482 sg_page_sizes |= sg->length;
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002483 sg_mark_end(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002484 }
Chris Wilson74ce6b62012-10-19 15:51:06 +01002485
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002486 /* Trim unused sg entries to avoid wasting memory. */
2487 i915_sg_trim(st);
2488
Chris Wilson03ac84f2016-10-28 13:58:36 +01002489 ret = i915_gem_gtt_prepare_pages(obj, st);
Chris Wilsond766ef52016-12-19 12:43:45 +00002490 if (ret) {
2491 /* DMA remapping failed? One possible cause is that
2492 * it could not reserve enough large entries, asking
2493 * for PAGE_SIZE chunks instead may be helpful.
2494 */
2495 if (max_segment > PAGE_SIZE) {
2496 for_each_sgt_page(page, sgt_iter, st)
2497 put_page(page);
2498 sg_free_table(st);
2499
2500 max_segment = PAGE_SIZE;
2501 goto rebuild_st;
2502 } else {
2503 dev_warn(&dev_priv->drm.pdev->dev,
2504 "Failed to DMA remap %lu pages\n",
2505 page_count);
2506 goto err_pages;
2507 }
2508 }
Imre Deake2273302015-07-09 12:59:05 +03002509
Eric Anholt673a3942008-07-30 12:06:12 -07002510 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002511 i915_gem_object_do_bit_17_swizzle(obj, st);
Eric Anholt673a3942008-07-30 12:06:12 -07002512
Matthew Auld84e89782017-10-09 12:00:24 +01002513 __i915_gem_object_set_pages(obj, st, sg_page_sizes);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002514
2515 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002516
Chris Wilsonb17993b2016-11-14 11:29:30 +00002517err_sg:
Imre Deak90797e62013-02-18 19:28:03 +02002518 sg_mark_end(sg);
Chris Wilsonb17993b2016-11-14 11:29:30 +00002519err_pages:
Dave Gordon85d12252016-05-20 11:54:06 +01002520 for_each_sgt_page(page, sgt_iter, st)
2521 put_page(page);
Chris Wilson9da3da62012-06-01 15:20:22 +01002522 sg_free_table(st);
2523 kfree(st);
Chris Wilson0820baf2014-03-25 13:23:03 +00002524
2525 /* shmemfs first checks if there is enough memory to allocate the page
2526 * and reports ENOSPC should there be insufficient, along with the usual
2527 * ENOMEM for a genuine allocation failure.
2528 *
2529 * We use ENOSPC in our driver to mean that we have run out of aperture
2530 * space and so want to translate the error from shmemfs back to our
2531 * usual understanding of ENOMEM.
2532 */
Imre Deake2273302015-07-09 12:59:05 +03002533 if (ret == -ENOSPC)
2534 ret = -ENOMEM;
2535
Matthew Auldb91b09e2017-10-06 23:18:17 +01002536 return ret;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002537}
2538
2539void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
Matthew Aulda5c081662017-10-06 23:18:18 +01002540 struct sg_table *pages,
Matthew Auld84e89782017-10-09 12:00:24 +01002541 unsigned int sg_page_sizes)
Chris Wilson03ac84f2016-10-28 13:58:36 +01002542{
Matthew Aulda5c081662017-10-06 23:18:18 +01002543 struct drm_i915_private *i915 = to_i915(obj->base.dev);
2544 unsigned long supported = INTEL_INFO(i915)->page_sizes;
2545 int i;
2546
Chris Wilson1233e2d2016-10-28 13:58:37 +01002547 lockdep_assert_held(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002548
2549 obj->mm.get_page.sg_pos = pages->sgl;
2550 obj->mm.get_page.sg_idx = 0;
2551
2552 obj->mm.pages = pages;
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002553
2554 if (i915_gem_object_is_tiled(obj) &&
Chris Wilsonf2123812017-10-16 12:40:37 +01002555 i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002556 GEM_BUG_ON(obj->mm.quirked);
2557 __i915_gem_object_pin_pages(obj);
2558 obj->mm.quirked = true;
2559 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002560
Matthew Auld84e89782017-10-09 12:00:24 +01002561 GEM_BUG_ON(!sg_page_sizes);
2562 obj->mm.page_sizes.phys = sg_page_sizes;
Matthew Aulda5c081662017-10-06 23:18:18 +01002563
2564 /*
Matthew Auld84e89782017-10-09 12:00:24 +01002565 * Calculate the supported page-sizes which fit into the given
2566 * sg_page_sizes. This will give us the page-sizes which we may be able
2567 * to use opportunistically when later inserting into the GTT. For
2568 * example if phys=2G, then in theory we should be able to use 1G, 2M,
2569 * 64K or 4K pages, although in practice this will depend on a number of
2570 * other factors.
Matthew Aulda5c081662017-10-06 23:18:18 +01002571 */
2572 obj->mm.page_sizes.sg = 0;
2573 for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) {
2574 if (obj->mm.page_sizes.phys & ~0u << i)
2575 obj->mm.page_sizes.sg |= BIT(i);
2576 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002577 GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg));
Chris Wilsonf2123812017-10-16 12:40:37 +01002578
2579 spin_lock(&i915->mm.obj_lock);
2580 list_add(&obj->mm.link, &i915->mm.unbound_list);
2581 spin_unlock(&i915->mm.obj_lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002582}
2583
2584static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
2585{
Matthew Auldb91b09e2017-10-06 23:18:17 +01002586 int err;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002587
2588 if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
2589 DRM_DEBUG("Attempting to obtain a purgeable object\n");
2590 return -EFAULT;
2591 }
2592
Matthew Auldb91b09e2017-10-06 23:18:17 +01002593 err = obj->ops->get_pages(obj);
2594 GEM_BUG_ON(!err && IS_ERR_OR_NULL(obj->mm.pages));
Chris Wilson03ac84f2016-10-28 13:58:36 +01002595
Matthew Auldb91b09e2017-10-06 23:18:17 +01002596 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002597}
2598
Chris Wilson37e680a2012-06-07 15:38:42 +01002599/* Ensure that the associated pages are gathered from the backing storage
Chris Wilson1233e2d2016-10-28 13:58:37 +01002600 * and pinned into our object. i915_gem_object_pin_pages() may be called
Chris Wilson37e680a2012-06-07 15:38:42 +01002601 * multiple times before they are released by a single call to
Chris Wilson1233e2d2016-10-28 13:58:37 +01002602 * i915_gem_object_unpin_pages() - once the pages are no longer referenced
Chris Wilson37e680a2012-06-07 15:38:42 +01002603 * either as a result of memory pressure (reaping pages under the shrinker)
2604 * or as the object is itself released.
2605 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002606int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
Chris Wilson37e680a2012-06-07 15:38:42 +01002607{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002608 int err;
Chris Wilson37e680a2012-06-07 15:38:42 +01002609
Chris Wilson1233e2d2016-10-28 13:58:37 +01002610 err = mutex_lock_interruptible(&obj->mm.lock);
2611 if (err)
2612 return err;
Chris Wilson4c7d62c2016-10-28 13:58:32 +01002613
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002614 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002615 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2616
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002617 err = ____i915_gem_object_get_pages(obj);
2618 if (err)
2619 goto unlock;
2620
2621 smp_mb__before_atomic();
Chris Wilson1233e2d2016-10-28 13:58:37 +01002622 }
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002623 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson43e28f02013-01-08 10:53:09 +00002624
Chris Wilson1233e2d2016-10-28 13:58:37 +01002625unlock:
2626 mutex_unlock(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002627 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002628}
2629
Dave Gordondd6034c2016-05-20 11:54:04 +01002630/* The 'mapping' part of i915_gem_object_pin_map() below */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002631static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
2632 enum i915_map_type type)
Dave Gordondd6034c2016-05-20 11:54:04 +01002633{
2634 unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002635 struct sg_table *sgt = obj->mm.pages;
Dave Gordon85d12252016-05-20 11:54:06 +01002636 struct sgt_iter sgt_iter;
2637 struct page *page;
Dave Gordonb338fa42016-05-20 11:54:05 +01002638 struct page *stack_pages[32];
2639 struct page **pages = stack_pages;
Dave Gordondd6034c2016-05-20 11:54:04 +01002640 unsigned long i = 0;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002641 pgprot_t pgprot;
Dave Gordondd6034c2016-05-20 11:54:04 +01002642 void *addr;
2643
2644 /* A single page can always be kmapped */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002645 if (n_pages == 1 && type == I915_MAP_WB)
Dave Gordondd6034c2016-05-20 11:54:04 +01002646 return kmap(sg_page(sgt->sgl));
2647
Dave Gordonb338fa42016-05-20 11:54:05 +01002648 if (n_pages > ARRAY_SIZE(stack_pages)) {
2649 /* Too big for stack -- allocate temporary array instead */
Michal Hocko0ee931c2017-09-13 16:28:29 -07002650 pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL);
Dave Gordonb338fa42016-05-20 11:54:05 +01002651 if (!pages)
2652 return NULL;
2653 }
Dave Gordondd6034c2016-05-20 11:54:04 +01002654
Dave Gordon85d12252016-05-20 11:54:06 +01002655 for_each_sgt_page(page, sgt_iter, sgt)
2656 pages[i++] = page;
Dave Gordondd6034c2016-05-20 11:54:04 +01002657
2658 /* Check that we have the expected number of pages */
2659 GEM_BUG_ON(i != n_pages);
2660
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002661 switch (type) {
Chris Wilsona575c672017-08-28 11:46:31 +01002662 default:
2663 MISSING_CASE(type);
2664 /* fallthrough to use PAGE_KERNEL anyway */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002665 case I915_MAP_WB:
2666 pgprot = PAGE_KERNEL;
2667 break;
2668 case I915_MAP_WC:
2669 pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
2670 break;
2671 }
2672 addr = vmap(pages, n_pages, 0, pgprot);
Dave Gordondd6034c2016-05-20 11:54:04 +01002673
Dave Gordonb338fa42016-05-20 11:54:05 +01002674 if (pages != stack_pages)
Michal Hocko20981052017-05-17 14:23:12 +02002675 kvfree(pages);
Dave Gordondd6034c2016-05-20 11:54:04 +01002676
2677 return addr;
2678}
2679
2680/* get, pin, and map the pages of the object into kernel space */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002681void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
2682 enum i915_map_type type)
Chris Wilson0a798eb2016-04-08 12:11:11 +01002683{
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002684 enum i915_map_type has_type;
2685 bool pinned;
2686 void *ptr;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002687 int ret;
2688
Tina Zhanga03f3952017-11-14 10:25:13 +00002689 if (unlikely(!i915_gem_object_has_struct_page(obj)))
2690 return ERR_PTR(-ENXIO);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002691
Chris Wilson1233e2d2016-10-28 13:58:37 +01002692 ret = mutex_lock_interruptible(&obj->mm.lock);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002693 if (ret)
2694 return ERR_PTR(ret);
2695
Chris Wilsona575c672017-08-28 11:46:31 +01002696 pinned = !(type & I915_MAP_OVERRIDE);
2697 type &= ~I915_MAP_OVERRIDE;
2698
Chris Wilson1233e2d2016-10-28 13:58:37 +01002699 if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002700 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002701 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2702
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002703 ret = ____i915_gem_object_get_pages(obj);
2704 if (ret)
2705 goto err_unlock;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002706
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002707 smp_mb__before_atomic();
2708 }
2709 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002710 pinned = false;
2711 }
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002712 GEM_BUG_ON(!i915_gem_object_has_pages(obj));
Chris Wilson0a798eb2016-04-08 12:11:11 +01002713
Chris Wilson0ce81782017-05-17 13:09:59 +01002714 ptr = page_unpack_bits(obj->mm.mapping, &has_type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002715 if (ptr && has_type != type) {
2716 if (pinned) {
2717 ret = -EBUSY;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002718 goto err_unpin;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002719 }
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002720
2721 if (is_vmalloc_addr(ptr))
2722 vunmap(ptr);
2723 else
2724 kunmap(kmap_to_page(ptr));
2725
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002726 ptr = obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002727 }
2728
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002729 if (!ptr) {
2730 ptr = i915_gem_object_map(obj, type);
2731 if (!ptr) {
2732 ret = -ENOMEM;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002733 goto err_unpin;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002734 }
2735
Chris Wilson0ce81782017-05-17 13:09:59 +01002736 obj->mm.mapping = page_pack_bits(ptr, type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002737 }
2738
Chris Wilson1233e2d2016-10-28 13:58:37 +01002739out_unlock:
2740 mutex_unlock(&obj->mm.lock);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002741 return ptr;
2742
Chris Wilson1233e2d2016-10-28 13:58:37 +01002743err_unpin:
2744 atomic_dec(&obj->mm.pages_pin_count);
2745err_unlock:
2746 ptr = ERR_PTR(ret);
2747 goto out_unlock;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002748}
2749
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002750static int
2751i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj,
2752 const struct drm_i915_gem_pwrite *arg)
2753{
2754 struct address_space *mapping = obj->base.filp->f_mapping;
2755 char __user *user_data = u64_to_user_ptr(arg->data_ptr);
2756 u64 remain, offset;
2757 unsigned int pg;
2758
2759 /* Before we instantiate/pin the backing store for our use, we
2760 * can prepopulate the shmemfs filp efficiently using a write into
2761 * the pagecache. We avoid the penalty of instantiating all the
2762 * pages, important if the user is just writing to a few and never
2763 * uses the object on the GPU, and using a direct write into shmemfs
2764 * allows it to avoid the cost of retrieving a page (either swapin
2765 * or clearing-before-use) before it is overwritten.
2766 */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002767 if (i915_gem_object_has_pages(obj))
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002768 return -ENODEV;
2769
Chris Wilsona6d65e42017-10-16 21:27:32 +01002770 if (obj->mm.madv != I915_MADV_WILLNEED)
2771 return -EFAULT;
2772
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002773 /* Before the pages are instantiated the object is treated as being
2774 * in the CPU domain. The pages will be clflushed as required before
2775 * use, and we can freely write into the pages directly. If userspace
2776 * races pwrite with any other operation; corruption will ensue -
2777 * that is userspace's prerogative!
2778 */
2779
2780 remain = arg->size;
2781 offset = arg->offset;
2782 pg = offset_in_page(offset);
2783
2784 do {
2785 unsigned int len, unwritten;
2786 struct page *page;
2787 void *data, *vaddr;
2788 int err;
2789
2790 len = PAGE_SIZE - pg;
2791 if (len > remain)
2792 len = remain;
2793
2794 err = pagecache_write_begin(obj->base.filp, mapping,
2795 offset, len, 0,
2796 &page, &data);
2797 if (err < 0)
2798 return err;
2799
2800 vaddr = kmap(page);
2801 unwritten = copy_from_user(vaddr + pg, user_data, len);
2802 kunmap(page);
2803
2804 err = pagecache_write_end(obj->base.filp, mapping,
2805 offset, len, len - unwritten,
2806 page, data);
2807 if (err < 0)
2808 return err;
2809
2810 if (unwritten)
2811 return -EFAULT;
2812
2813 remain -= len;
2814 user_data += len;
2815 offset += len;
2816 pg = 0;
2817 } while (remain);
2818
2819 return 0;
2820}
2821
Chris Wilson77b25a92017-07-21 13:32:30 +01002822static bool ban_context(const struct i915_gem_context *ctx,
2823 unsigned int score)
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002824{
Chris Wilson60958682016-12-31 11:20:11 +00002825 return (i915_gem_context_is_bannable(ctx) &&
Chris Wilson77b25a92017-07-21 13:32:30 +01002826 score >= CONTEXT_SCORE_BAN_THRESHOLD);
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002827}
2828
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002829static void i915_gem_context_mark_guilty(struct i915_gem_context *ctx)
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002830{
Chris Wilson77b25a92017-07-21 13:32:30 +01002831 unsigned int score;
2832 bool banned;
Mika Kuoppalab083a082016-11-18 15:10:47 +02002833
Chris Wilson77b25a92017-07-21 13:32:30 +01002834 atomic_inc(&ctx->guilty_count);
2835
2836 score = atomic_add_return(CONTEXT_SCORE_GUILTY, &ctx->ban_score);
2837 banned = ban_context(ctx, score);
Mika Kuoppalab083a082016-11-18 15:10:47 +02002838 DRM_DEBUG_DRIVER("context %s marked guilty (score %d) banned? %s\n",
Chris Wilson77b25a92017-07-21 13:32:30 +01002839 ctx->name, score, yesno(banned));
2840 if (!banned)
Mika Kuoppalab083a082016-11-18 15:10:47 +02002841 return;
2842
Chris Wilson77b25a92017-07-21 13:32:30 +01002843 i915_gem_context_set_banned(ctx);
2844 if (!IS_ERR_OR_NULL(ctx->file_priv)) {
2845 atomic_inc(&ctx->file_priv->context_bans);
2846 DRM_DEBUG_DRIVER("client %s has had %d context banned\n",
2847 ctx->name, atomic_read(&ctx->file_priv->context_bans));
2848 }
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002849}
2850
2851static void i915_gem_context_mark_innocent(struct i915_gem_context *ctx)
2852{
Chris Wilson77b25a92017-07-21 13:32:30 +01002853 atomic_inc(&ctx->active_count);
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002854}
2855
Chris Wilson8d9fc7f2014-02-25 17:11:23 +02002856struct drm_i915_gem_request *
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002857i915_gem_find_active_request(struct intel_engine_cs *engine)
Chris Wilson9375e442010-09-19 12:21:28 +01002858{
Chris Wilson754c9fd2017-02-23 07:44:14 +00002859 struct drm_i915_gem_request *request, *active = NULL;
2860 unsigned long flags;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002861
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002862 /* We are called by the error capture and reset at a random
2863 * point in time. In particular, note that neither is crucially
2864 * ordered with an interrupt. After a hang, the GPU is dead and we
2865 * assume that no more writes can happen (we waited long enough for
2866 * all writes that were in transaction to be flushed) - adding an
2867 * extra delay for a recent interrupt is pointless. Hence, we do
2868 * not need an engine->irq_seqno_barrier() before the seqno reads.
2869 */
Chris Wilson754c9fd2017-02-23 07:44:14 +00002870 spin_lock_irqsave(&engine->timeline->lock, flags);
Chris Wilson73cb9702016-10-28 13:58:46 +01002871 list_for_each_entry(request, &engine->timeline->requests, link) {
Chris Wilson754c9fd2017-02-23 07:44:14 +00002872 if (__i915_gem_request_completed(request,
2873 request->global_seqno))
Chris Wilson4db080f2013-12-04 11:37:09 +00002874 continue;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002875
Mika Kuoppala36193ac2017-01-17 17:59:02 +02002876 GEM_BUG_ON(request->engine != engine);
Chris Wilsonc00122f32017-02-12 17:19:58 +00002877 GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
2878 &request->fence.flags));
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002879
Chris Wilson754c9fd2017-02-23 07:44:14 +00002880 active = request;
2881 break;
2882 }
2883 spin_unlock_irqrestore(&engine->timeline->lock, flags);
2884
2885 return active;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002886}
2887
Mika Kuoppalabf2f0432017-01-17 17:59:04 +02002888static bool engine_stalled(struct intel_engine_cs *engine)
2889{
2890 if (!engine->hangcheck.stalled)
2891 return false;
2892
2893 /* Check for possible seqno movement after hang declaration */
2894 if (engine->hangcheck.seqno != intel_engine_get_seqno(engine)) {
2895 DRM_DEBUG_DRIVER("%s pardoned\n", engine->name);
2896 return false;
2897 }
2898
2899 return true;
2900}
2901
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002902/*
2903 * Ensure irq handler finishes, and not run again.
2904 * Also return the active request so that we only search for it once.
2905 */
2906struct drm_i915_gem_request *
2907i915_gem_reset_prepare_engine(struct intel_engine_cs *engine)
2908{
2909 struct drm_i915_gem_request *request = NULL;
2910
Chris Wilson1749d902017-10-09 12:02:59 +01002911 /*
2912 * During the reset sequence, we must prevent the engine from
2913 * entering RC6. As the context state is undefined until we restart
2914 * the engine, if it does enter RC6 during the reset, the state
2915 * written to the powercontext is undefined and so we may lose
2916 * GPU state upon resume, i.e. fail to restart after a reset.
2917 */
2918 intel_uncore_forcewake_get(engine->i915, FORCEWAKE_ALL);
2919
2920 /*
2921 * Prevent the signaler thread from updating the request
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002922 * state (by calling dma_fence_signal) as we are processing
2923 * the reset. The write from the GPU of the seqno is
2924 * asynchronous and the signaler thread may see a different
2925 * value to us and declare the request complete, even though
2926 * the reset routine have picked that request as the active
2927 * (incomplete) request. This conflict is not handled
2928 * gracefully!
2929 */
2930 kthread_park(engine->breadcrumbs.signaler);
2931
Chris Wilson1749d902017-10-09 12:02:59 +01002932 /*
2933 * Prevent request submission to the hardware until we have
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002934 * completed the reset in i915_gem_reset_finish(). If a request
2935 * is completed by one engine, it may then queue a request
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05302936 * to a second via its execlists->tasklet *just* as we are
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002937 * calling engine->init_hw() and also writing the ELSP.
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05302938 * Turning off the execlists->tasklet until the reset is over
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002939 * prevents the race.
2940 */
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05302941 tasklet_kill(&engine->execlists.tasklet);
2942 tasklet_disable(&engine->execlists.tasklet);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002943
Michał Winiarskic41937f2017-10-26 15:35:58 +02002944 /*
2945 * We're using worker to queue preemption requests from the tasklet in
2946 * GuC submission mode.
2947 * Even though tasklet was disabled, we may still have a worker queued.
2948 * Let's make sure that all workers scheduled before disabling the
2949 * tasklet are completed before continuing with the reset.
2950 */
2951 if (engine->i915->guc.preempt_wq)
2952 flush_workqueue(engine->i915->guc.preempt_wq);
2953
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002954 if (engine->irq_seqno_barrier)
2955 engine->irq_seqno_barrier(engine);
2956
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01002957 request = i915_gem_find_active_request(engine);
2958 if (request && request->fence.error == -EIO)
2959 request = ERR_PTR(-EIO); /* Previous reset failed! */
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002960
2961 return request;
2962}
2963
Chris Wilson0e178ae2017-01-17 17:59:06 +02002964int i915_gem_reset_prepare(struct drm_i915_private *dev_priv)
Chris Wilson4c965542017-01-17 17:59:01 +02002965{
2966 struct intel_engine_cs *engine;
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002967 struct drm_i915_gem_request *request;
Chris Wilson4c965542017-01-17 17:59:01 +02002968 enum intel_engine_id id;
Chris Wilson0e178ae2017-01-17 17:59:06 +02002969 int err = 0;
Chris Wilson4c965542017-01-17 17:59:01 +02002970
Chris Wilson0e178ae2017-01-17 17:59:06 +02002971 for_each_engine(engine, dev_priv, id) {
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002972 request = i915_gem_reset_prepare_engine(engine);
2973 if (IS_ERR(request)) {
2974 err = PTR_ERR(request);
2975 continue;
Chris Wilson0e178ae2017-01-17 17:59:06 +02002976 }
Michel Thierryc64992e2017-06-20 10:57:44 +01002977
2978 engine->hangcheck.active_request = request;
Chris Wilson0e178ae2017-01-17 17:59:06 +02002979 }
2980
Chris Wilson4c965542017-01-17 17:59:01 +02002981 i915_gem_revoke_fences(dev_priv);
Chris Wilson0e178ae2017-01-17 17:59:06 +02002982
2983 return err;
Chris Wilson4c965542017-01-17 17:59:01 +02002984}
2985
Mika Kuoppala36193ac2017-01-17 17:59:02 +02002986static void skip_request(struct drm_i915_gem_request *request)
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002987{
Chris Wilson821ed7d2016-09-09 14:11:53 +01002988 void *vaddr = request->ring->vaddr;
2989 u32 head;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002990
Chris Wilson821ed7d2016-09-09 14:11:53 +01002991 /* As this request likely depends on state from the lost
2992 * context, clear out all the user operations leaving the
2993 * breadcrumb at the end (so we get the fence notifications).
2994 */
2995 head = request->head;
2996 if (request->postfix < head) {
2997 memset(vaddr + head, 0, request->ring->size - head);
2998 head = 0;
2999 }
3000 memset(vaddr + head, 0, request->postfix - head);
Chris Wilsonc0d5f322017-01-10 17:22:43 +00003001
3002 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson4db080f2013-12-04 11:37:09 +00003003}
3004
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003005static void engine_skip_context(struct drm_i915_gem_request *request)
3006{
3007 struct intel_engine_cs *engine = request->engine;
3008 struct i915_gem_context *hung_ctx = request->ctx;
3009 struct intel_timeline *timeline;
3010 unsigned long flags;
3011
3012 timeline = i915_gem_context_lookup_timeline(hung_ctx, engine);
3013
3014 spin_lock_irqsave(&engine->timeline->lock, flags);
3015 spin_lock(&timeline->lock);
3016
3017 list_for_each_entry_continue(request, &engine->timeline->requests, link)
3018 if (request->ctx == hung_ctx)
3019 skip_request(request);
3020
3021 list_for_each_entry(request, &timeline->requests, link)
3022 skip_request(request);
3023
3024 spin_unlock(&timeline->lock);
3025 spin_unlock_irqrestore(&engine->timeline->lock, flags);
3026}
3027
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003028/* Returns the request if it was guilty of the hang */
3029static struct drm_i915_gem_request *
3030i915_gem_reset_request(struct intel_engine_cs *engine,
3031 struct drm_i915_gem_request *request)
Mika Kuoppala61da5362017-01-17 17:59:05 +02003032{
Mika Kuoppala71895a02017-01-17 17:59:07 +02003033 /* The guilty request will get skipped on a hung engine.
3034 *
3035 * Users of client default contexts do not rely on logical
3036 * state preserved between batches so it is safe to execute
3037 * queued requests following the hang. Non default contexts
3038 * rely on preserved state, so skipping a batch loses the
3039 * evolution of the state and it needs to be considered corrupted.
3040 * Executing more queued batches on top of corrupted state is
3041 * risky. But we take the risk by trying to advance through
3042 * the queued requests in order to make the client behaviour
3043 * more predictable around resets, by not throwing away random
3044 * amount of batches it has prepared for execution. Sophisticated
3045 * clients can use gem_reset_stats_ioctl and dma fence status
3046 * (exported via sync_file info ioctl on explicit fences) to observe
3047 * when it loses the context state and should rebuild accordingly.
3048 *
3049 * The context ban, and ultimately the client ban, mechanism are safety
3050 * valves if client submission ends up resulting in nothing more than
3051 * subsequent hangs.
3052 */
3053
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003054 if (engine_stalled(engine)) {
Mika Kuoppala61da5362017-01-17 17:59:05 +02003055 i915_gem_context_mark_guilty(request->ctx);
3056 skip_request(request);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003057
3058 /* If this context is now banned, skip all pending requests. */
3059 if (i915_gem_context_is_banned(request->ctx))
3060 engine_skip_context(request);
Mika Kuoppala61da5362017-01-17 17:59:05 +02003061 } else {
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003062 /*
3063 * Since this is not the hung engine, it may have advanced
3064 * since the hang declaration. Double check by refinding
3065 * the active request at the time of the reset.
3066 */
3067 request = i915_gem_find_active_request(engine);
3068 if (request) {
3069 i915_gem_context_mark_innocent(request->ctx);
3070 dma_fence_set_error(&request->fence, -EAGAIN);
3071
3072 /* Rewind the engine to replay the incomplete rq */
3073 spin_lock_irq(&engine->timeline->lock);
3074 request = list_prev_entry(request, link);
3075 if (&request->link == &engine->timeline->requests)
3076 request = NULL;
3077 spin_unlock_irq(&engine->timeline->lock);
3078 }
Mika Kuoppala61da5362017-01-17 17:59:05 +02003079 }
3080
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003081 return request;
Mika Kuoppala61da5362017-01-17 17:59:05 +02003082}
3083
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003084void i915_gem_reset_engine(struct intel_engine_cs *engine,
3085 struct drm_i915_gem_request *request)
Chris Wilson4db080f2013-12-04 11:37:09 +00003086{
Chris Wilsoned454f22017-07-21 13:32:29 +01003087 engine->irq_posted = 0;
3088
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003089 if (request)
3090 request = i915_gem_reset_request(engine, request);
3091
3092 if (request) {
Chris Wilsonc0dcb202017-02-07 15:24:37 +00003093 DRM_DEBUG_DRIVER("resetting %s to restart from tail of request 0x%x\n",
3094 engine->name, request->global_seqno);
Chris Wilsonc0dcb202017-02-07 15:24:37 +00003095 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003096
3097 /* Setup the CS to resume from the breadcrumb of the hung request */
3098 engine->reset_hw(engine, request);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003099}
3100
Chris Wilsond8027092017-02-08 14:30:32 +00003101void i915_gem_reset(struct drm_i915_private *dev_priv)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003102{
3103 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303104 enum intel_engine_id id;
Chris Wilson821ed7d2016-09-09 14:11:53 +01003105
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003106 lockdep_assert_held(&dev_priv->drm.struct_mutex);
3107
Chris Wilson821ed7d2016-09-09 14:11:53 +01003108 i915_gem_retire_requests(dev_priv);
3109
Chris Wilson2ae55732017-02-12 17:20:02 +00003110 for_each_engine(engine, dev_priv, id) {
3111 struct i915_gem_context *ctx;
3112
Michel Thierryc64992e2017-06-20 10:57:44 +01003113 i915_gem_reset_engine(engine, engine->hangcheck.active_request);
Chris Wilson2ae55732017-02-12 17:20:02 +00003114 ctx = fetch_and_zero(&engine->last_retired_context);
3115 if (ctx)
3116 engine->context_unpin(engine, ctx);
3117 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003118
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00003119 i915_gem_restore_fences(dev_priv);
Chris Wilsonf2a91d12016-09-21 14:51:06 +01003120
3121 if (dev_priv->gt.awake) {
3122 intel_sanitize_gt_powersave(dev_priv);
3123 intel_enable_gt_powersave(dev_priv);
3124 if (INTEL_GEN(dev_priv) >= 6)
3125 gen6_rps_busy(dev_priv);
3126 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003127}
3128
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003129void i915_gem_reset_finish_engine(struct intel_engine_cs *engine)
3130{
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05303131 tasklet_enable(&engine->execlists.tasklet);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003132 kthread_unpark(engine->breadcrumbs.signaler);
Chris Wilson1749d902017-10-09 12:02:59 +01003133
3134 intel_uncore_forcewake_put(engine->i915, FORCEWAKE_ALL);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003135}
3136
Chris Wilsond8027092017-02-08 14:30:32 +00003137void i915_gem_reset_finish(struct drm_i915_private *dev_priv)
3138{
Chris Wilson1f7b8472017-02-08 14:30:33 +00003139 struct intel_engine_cs *engine;
3140 enum intel_engine_id id;
3141
Chris Wilsond8027092017-02-08 14:30:32 +00003142 lockdep_assert_held(&dev_priv->drm.struct_mutex);
Chris Wilson1f7b8472017-02-08 14:30:33 +00003143
Chris Wilsonfe3288b2017-02-12 17:20:01 +00003144 for_each_engine(engine, dev_priv, id) {
Michel Thierryc64992e2017-06-20 10:57:44 +01003145 engine->hangcheck.active_request = NULL;
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003146 i915_gem_reset_finish_engine(engine);
Chris Wilsonfe3288b2017-02-12 17:20:01 +00003147 }
Chris Wilsond8027092017-02-08 14:30:32 +00003148}
3149
Chris Wilson821ed7d2016-09-09 14:11:53 +01003150static void nop_submit_request(struct drm_i915_gem_request *request)
3151{
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003152 dma_fence_set_error(&request->fence, -EIO);
3153
3154 i915_gem_request_submit(request);
3155}
3156
3157static void nop_complete_submit_request(struct drm_i915_gem_request *request)
3158{
Chris Wilson8d550822017-10-06 12:56:17 +01003159 unsigned long flags;
3160
Chris Wilson3cd94422017-01-10 17:22:45 +00003161 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson8d550822017-10-06 12:56:17 +01003162
3163 spin_lock_irqsave(&request->engine->timeline->lock, flags);
3164 __i915_gem_request_submit(request);
Chris Wilson3dcf93f72016-11-22 14:41:20 +00003165 intel_engine_init_global_seqno(request->engine, request->global_seqno);
Chris Wilson8d550822017-10-06 12:56:17 +01003166 spin_unlock_irqrestore(&request->engine->timeline->lock, flags);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003167}
3168
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003169void i915_gem_set_wedged(struct drm_i915_private *i915)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003170{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003171 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303172 enum intel_engine_id id;
Eric Anholt673a3942008-07-30 12:06:12 -07003173
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003174 /*
3175 * First, stop submission to hw, but do not yet complete requests by
3176 * rolling the global seqno forward (since this would complete requests
3177 * for which we haven't set the fence error to EIO yet).
3178 */
Chris Wilson20e49332016-11-22 14:41:21 +00003179 for_each_engine(engine, i915, id)
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003180 engine->submit_request = nop_submit_request;
3181
3182 /*
3183 * Make sure no one is running the old callback before we proceed with
3184 * cancelling requests and resetting the completion tracking. Otherwise
3185 * we might submit a request to the hardware which never completes.
3186 */
3187 synchronize_rcu();
3188
3189 for_each_engine(engine, i915, id) {
3190 /* Mark all executing requests as skipped */
3191 engine->cancel_requests(engine);
3192
3193 /*
3194 * Only once we've force-cancelled all in-flight requests can we
3195 * start to complete all requests.
3196 */
3197 engine->submit_request = nop_complete_submit_request;
3198 }
3199
3200 /*
3201 * Make sure no request can slip through without getting completed by
3202 * either this call here to intel_engine_init_global_seqno, or the one
3203 * in nop_complete_submit_request.
3204 */
3205 synchronize_rcu();
3206
3207 for_each_engine(engine, i915, id) {
3208 unsigned long flags;
3209
3210 /* Mark all pending requests as complete so that any concurrent
3211 * (lockless) lookup doesn't try and wait upon the request as we
3212 * reset it.
3213 */
3214 spin_lock_irqsave(&engine->timeline->lock, flags);
3215 intel_engine_init_global_seqno(engine,
3216 intel_engine_last_submit(engine));
3217 spin_unlock_irqrestore(&engine->timeline->lock, flags);
3218 }
Chris Wilson20e49332016-11-22 14:41:21 +00003219
Chris Wilson3d7adbb2017-07-21 13:32:27 +01003220 set_bit(I915_WEDGED, &i915->gpu_error.flags);
3221 wake_up_all(&i915->gpu_error.reset_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07003222}
3223
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003224bool i915_gem_unset_wedged(struct drm_i915_private *i915)
3225{
3226 struct i915_gem_timeline *tl;
3227 int i;
3228
3229 lockdep_assert_held(&i915->drm.struct_mutex);
3230 if (!test_bit(I915_WEDGED, &i915->gpu_error.flags))
3231 return true;
3232
3233 /* Before unwedging, make sure that all pending operations
3234 * are flushed and errored out - we may have requests waiting upon
3235 * third party fences. We marked all inflight requests as EIO, and
3236 * every execbuf since returned EIO, for consistency we want all
3237 * the currently pending requests to also be marked as EIO, which
3238 * is done inside our nop_submit_request - and so we must wait.
3239 *
3240 * No more can be submitted until we reset the wedged bit.
3241 */
3242 list_for_each_entry(tl, &i915->gt.timelines, link) {
3243 for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
3244 struct drm_i915_gem_request *rq;
3245
3246 rq = i915_gem_active_peek(&tl->engine[i].last_request,
3247 &i915->drm.struct_mutex);
3248 if (!rq)
3249 continue;
3250
3251 /* We can't use our normal waiter as we want to
3252 * avoid recursively trying to handle the current
3253 * reset. The basic dma_fence_default_wait() installs
3254 * a callback for dma_fence_signal(), which is
3255 * triggered by our nop handler (indirectly, the
3256 * callback enables the signaler thread which is
3257 * woken by the nop_submit_request() advancing the seqno
3258 * and when the seqno passes the fence, the signaler
3259 * then signals the fence waking us up).
3260 */
3261 if (dma_fence_default_wait(&rq->fence, true,
3262 MAX_SCHEDULE_TIMEOUT) < 0)
3263 return false;
3264 }
3265 }
3266
3267 /* Undo nop_submit_request. We prevent all new i915 requests from
3268 * being queued (by disallowing execbuf whilst wedged) so having
3269 * waited for all active requests above, we know the system is idle
3270 * and do not have to worry about a thread being inside
3271 * engine->submit_request() as we swap over. So unlike installing
3272 * the nop_submit_request on reset, we can do this from normal
3273 * context and do not require stop_machine().
3274 */
3275 intel_engines_reset_default_submission(i915);
Chris Wilson36703e72017-06-22 11:56:25 +01003276 i915_gem_contexts_lost(i915);
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003277
3278 smp_mb__before_atomic(); /* complete takeover before enabling execbuf */
3279 clear_bit(I915_WEDGED, &i915->gpu_error.flags);
3280
3281 return true;
3282}
3283
Daniel Vetter75ef9da2010-08-21 00:25:16 +02003284static void
Eric Anholt673a3942008-07-30 12:06:12 -07003285i915_gem_retire_work_handler(struct work_struct *work)
3286{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003287 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003288 container_of(work, typeof(*dev_priv), gt.retire_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01003289 struct drm_device *dev = &dev_priv->drm;
Eric Anholt673a3942008-07-30 12:06:12 -07003290
Chris Wilson891b48c2010-09-29 12:26:37 +01003291 /* Come back later if the device is busy... */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003292 if (mutex_trylock(&dev->struct_mutex)) {
Chris Wilson67d97da2016-07-04 08:08:31 +01003293 i915_gem_retire_requests(dev_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003294 mutex_unlock(&dev->struct_mutex);
3295 }
Chris Wilson67d97da2016-07-04 08:08:31 +01003296
3297 /* Keep the retire handler running until we are finally idle.
3298 * We do not need to do this test under locking as in the worst-case
3299 * we queue the retire worker once too often.
3300 */
Chris Wilsonc9615612016-07-09 10:12:06 +01003301 if (READ_ONCE(dev_priv->gt.awake)) {
3302 i915_queue_hangcheck(dev_priv);
Chris Wilson67d97da2016-07-04 08:08:31 +01003303 queue_delayed_work(dev_priv->wq,
3304 &dev_priv->gt.retire_work,
Chris Wilsonbcb45082012-10-05 17:02:57 +01003305 round_jiffies_up_relative(HZ));
Chris Wilsonc9615612016-07-09 10:12:06 +01003306 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003307}
Chris Wilson891b48c2010-09-29 12:26:37 +01003308
Chris Wilson5427f202017-10-23 22:32:34 +01003309static inline bool
3310new_requests_since_last_retire(const struct drm_i915_private *i915)
3311{
3312 return (READ_ONCE(i915->gt.active_requests) ||
3313 work_pending(&i915->gt.idle_work.work));
3314}
3315
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003316static void
3317i915_gem_idle_work_handler(struct work_struct *work)
3318{
3319 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003320 container_of(work, typeof(*dev_priv), gt.idle_work.work);
Chris Wilson67d97da2016-07-04 08:08:31 +01003321 bool rearm_hangcheck;
Chris Wilson5427f202017-10-23 22:32:34 +01003322 ktime_t end;
Chris Wilson67d97da2016-07-04 08:08:31 +01003323
3324 if (!READ_ONCE(dev_priv->gt.awake))
3325 return;
3326
Imre Deak0cb56702016-11-07 11:20:04 +02003327 /*
3328 * Wait for last execlists context complete, but bail out in case a
3329 * new request is submitted.
3330 */
Chris Wilson5427f202017-10-23 22:32:34 +01003331 end = ktime_add_ms(ktime_get(), 200);
3332 do {
3333 if (new_requests_since_last_retire(dev_priv))
3334 return;
3335
3336 if (intel_engines_are_idle(dev_priv))
3337 break;
3338
3339 usleep_range(100, 500);
3340 } while (ktime_before(ktime_get(), end));
Chris Wilson67d97da2016-07-04 08:08:31 +01003341
3342 rearm_hangcheck =
3343 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
3344
Chris Wilson5427f202017-10-23 22:32:34 +01003345 if (!mutex_trylock(&dev_priv->drm.struct_mutex)) {
Chris Wilson67d97da2016-07-04 08:08:31 +01003346 /* Currently busy, come back later */
3347 mod_delayed_work(dev_priv->wq,
3348 &dev_priv->gt.idle_work,
3349 msecs_to_jiffies(50));
3350 goto out_rearm;
3351 }
3352
Imre Deak93c97dc2016-11-07 11:20:03 +02003353 /*
3354 * New request retired after this work handler started, extend active
3355 * period until next instance of the work.
3356 */
Chris Wilson5427f202017-10-23 22:32:34 +01003357 if (new_requests_since_last_retire(dev_priv))
Imre Deak93c97dc2016-11-07 11:20:03 +02003358 goto out_unlock;
3359
Chris Wilson5427f202017-10-23 22:32:34 +01003360 /*
Chris Wilsonff320d62017-10-23 22:32:35 +01003361 * Be paranoid and flush a concurrent interrupt to make sure
3362 * we don't reactivate any irq tasklets after parking.
3363 *
3364 * FIXME: Note that even though we have waited for execlists to be idle,
3365 * there may still be an in-flight interrupt even though the CSB
3366 * is now empty. synchronize_irq() makes sure that a residual interrupt
3367 * is completed before we continue, but it doesn't prevent the HW from
3368 * raising a spurious interrupt later. To complete the shield we should
3369 * coordinate disabling the CS irq with flushing the interrupts.
3370 */
3371 synchronize_irq(dev_priv->drm.irq);
3372
Chris Wilsonaba5e272017-10-25 15:39:41 +01003373 intel_engines_park(dev_priv);
Chris Wilsond02a1d82017-11-27 12:30:54 +00003374 i915_gem_timelines_park(dev_priv);
3375
Tvrtko Ursulinfeff0dc2017-11-21 18:18:46 +00003376 i915_pmu_gt_parked(dev_priv);
Zou Nan haid1b851f2010-05-21 09:08:57 +08003377
Chris Wilson67d97da2016-07-04 08:08:31 +01003378 GEM_BUG_ON(!dev_priv->gt.awake);
3379 dev_priv->gt.awake = false;
3380 rearm_hangcheck = false;
Daniel Vetter30ecad72015-12-09 09:29:36 +01003381
Chris Wilson67d97da2016-07-04 08:08:31 +01003382 if (INTEL_GEN(dev_priv) >= 6)
3383 gen6_rps_idle(dev_priv);
3384 intel_runtime_pm_put(dev_priv);
3385out_unlock:
Chris Wilson5427f202017-10-23 22:32:34 +01003386 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson35c94182015-04-07 16:20:37 +01003387
Chris Wilson67d97da2016-07-04 08:08:31 +01003388out_rearm:
3389 if (rearm_hangcheck) {
3390 GEM_BUG_ON(!dev_priv->gt.awake);
3391 i915_queue_hangcheck(dev_priv);
Chris Wilson35c94182015-04-07 16:20:37 +01003392 }
Eric Anholt673a3942008-07-30 12:06:12 -07003393}
3394
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003395void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
3396{
Chris Wilsond1b48c12017-08-16 09:52:08 +01003397 struct drm_i915_private *i915 = to_i915(gem->dev);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003398 struct drm_i915_gem_object *obj = to_intel_bo(gem);
3399 struct drm_i915_file_private *fpriv = file->driver_priv;
Chris Wilsond1b48c12017-08-16 09:52:08 +01003400 struct i915_lut_handle *lut, *ln;
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003401
Chris Wilsond1b48c12017-08-16 09:52:08 +01003402 mutex_lock(&i915->drm.struct_mutex);
3403
3404 list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) {
3405 struct i915_gem_context *ctx = lut->ctx;
3406 struct i915_vma *vma;
3407
Chris Wilson432295d2017-08-22 12:05:15 +01003408 GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF));
Chris Wilsond1b48c12017-08-16 09:52:08 +01003409 if (ctx->file_priv != fpriv)
3410 continue;
3411
3412 vma = radix_tree_delete(&ctx->handles_vma, lut->handle);
Chris Wilson3ffff012017-08-22 12:05:17 +01003413 GEM_BUG_ON(vma->obj != obj);
3414
3415 /* We allow the process to have multiple handles to the same
3416 * vma, in the same fd namespace, by virtue of flink/open.
3417 */
3418 GEM_BUG_ON(!vma->open_count);
3419 if (!--vma->open_count && !i915_vma_is_ggtt(vma))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003420 i915_vma_close(vma);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003421
Chris Wilsond1b48c12017-08-16 09:52:08 +01003422 list_del(&lut->obj_link);
3423 list_del(&lut->ctx_link);
Chris Wilson4ff4b442017-06-16 15:05:16 +01003424
Chris Wilsond1b48c12017-08-16 09:52:08 +01003425 kmem_cache_free(i915->luts, lut);
3426 __i915_gem_object_release_unless_active(obj);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003427 }
Chris Wilsond1b48c12017-08-16 09:52:08 +01003428
3429 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003430}
3431
Chris Wilsone95433c2016-10-28 13:58:27 +01003432static unsigned long to_wait_timeout(s64 timeout_ns)
3433{
3434 if (timeout_ns < 0)
3435 return MAX_SCHEDULE_TIMEOUT;
3436
3437 if (timeout_ns == 0)
3438 return 0;
3439
3440 return nsecs_to_jiffies_timeout(timeout_ns);
3441}
3442
Ben Widawsky5816d642012-04-11 11:18:19 -07003443/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003444 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003445 * @dev: drm device pointer
3446 * @data: ioctl data blob
3447 * @file: drm file pointer
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003448 *
3449 * Returns 0 if successful, else an error is returned with the remaining time in
3450 * the timeout parameter.
3451 * -ETIME: object is still busy after timeout
3452 * -ERESTARTSYS: signal interrupted the wait
3453 * -ENONENT: object doesn't exist
3454 * Also possible, but rare:
Chris Wilsonb8050142017-08-11 11:57:31 +01003455 * -EAGAIN: incomplete, restart syscall
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003456 * -ENOMEM: damn
3457 * -ENODEV: Internal IRQ fail
3458 * -E?: The add request failed
3459 *
3460 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
3461 * non-zero timeout parameter the wait ioctl will wait for the given number of
3462 * nanoseconds on an object becoming unbusy. Since the wait itself does so
3463 * without holding struct_mutex the object may become re-busied before this
3464 * function completes. A similar but shorter * race condition exists in the busy
3465 * ioctl
3466 */
3467int
3468i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
3469{
3470 struct drm_i915_gem_wait *args = data;
3471 struct drm_i915_gem_object *obj;
Chris Wilsone95433c2016-10-28 13:58:27 +01003472 ktime_t start;
3473 long ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003474
Daniel Vetter11b5d512014-09-29 15:31:26 +02003475 if (args->flags != 0)
3476 return -EINVAL;
3477
Chris Wilson03ac0642016-07-20 13:31:51 +01003478 obj = i915_gem_object_lookup(file, args->bo_handle);
Chris Wilson033d5492016-08-05 10:14:17 +01003479 if (!obj)
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003480 return -ENOENT;
Chris Wilson033d5492016-08-05 10:14:17 +01003481
Chris Wilsone95433c2016-10-28 13:58:27 +01003482 start = ktime_get();
3483
3484 ret = i915_gem_object_wait(obj,
3485 I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL,
3486 to_wait_timeout(args->timeout_ns),
3487 to_rps_client(file));
3488
3489 if (args->timeout_ns > 0) {
3490 args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
3491 if (args->timeout_ns < 0)
3492 args->timeout_ns = 0;
Chris Wilsonc1d20612017-02-16 12:54:41 +00003493
3494 /*
3495 * Apparently ktime isn't accurate enough and occasionally has a
3496 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
3497 * things up to make the test happy. We allow up to 1 jiffy.
3498 *
3499 * This is a regression from the timespec->ktime conversion.
3500 */
3501 if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
3502 args->timeout_ns = 0;
Chris Wilsonb8050142017-08-11 11:57:31 +01003503
3504 /* Asked to wait beyond the jiffie/scheduler precision? */
3505 if (ret == -ETIME && args->timeout_ns)
3506 ret = -EAGAIN;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003507 }
3508
Chris Wilsonf0cd5182016-10-28 13:58:43 +01003509 i915_gem_object_put(obj);
John Harrisonff865882014-11-24 18:49:28 +00003510 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003511}
3512
Chris Wilson73cb9702016-10-28 13:58:46 +01003513static int wait_for_timeline(struct i915_gem_timeline *tl, unsigned int flags)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003514{
Chris Wilson73cb9702016-10-28 13:58:46 +01003515 int ret, i;
3516
3517 for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
3518 ret = i915_gem_active_wait(&tl->engine[i].last_request, flags);
3519 if (ret)
3520 return ret;
3521 }
3522
3523 return 0;
3524}
3525
Chris Wilson25112b62017-03-30 15:50:39 +01003526static int wait_for_engines(struct drm_i915_private *i915)
3527{
Chris Wilsoncad99462017-08-26 12:09:33 +01003528 if (wait_for(intel_engines_are_idle(i915), 50)) {
3529 DRM_ERROR("Failed to idle engines, declaring wedged!\n");
3530 i915_gem_set_wedged(i915);
3531 return -EIO;
Chris Wilson25112b62017-03-30 15:50:39 +01003532 }
3533
3534 return 0;
3535}
3536
Chris Wilson73cb9702016-10-28 13:58:46 +01003537int i915_gem_wait_for_idle(struct drm_i915_private *i915, unsigned int flags)
3538{
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003539 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003540
Chris Wilson863e9fd2017-05-30 13:13:32 +01003541 /* If the device is asleep, we have no requests outstanding */
3542 if (!READ_ONCE(i915->gt.awake))
3543 return 0;
3544
Chris Wilson9caa34a2016-11-11 14:58:08 +00003545 if (flags & I915_WAIT_LOCKED) {
3546 struct i915_gem_timeline *tl;
3547
3548 lockdep_assert_held(&i915->drm.struct_mutex);
3549
3550 list_for_each_entry(tl, &i915->gt.timelines, link) {
3551 ret = wait_for_timeline(tl, flags);
3552 if (ret)
3553 return ret;
3554 }
Chris Wilson72022a72017-03-30 15:50:38 +01003555
3556 i915_gem_retire_requests(i915);
3557 GEM_BUG_ON(i915->gt.active_requests);
Chris Wilson25112b62017-03-30 15:50:39 +01003558
3559 ret = wait_for_engines(i915);
Chris Wilson9caa34a2016-11-11 14:58:08 +00003560 } else {
3561 ret = wait_for_timeline(&i915->gt.global_timeline, flags);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003562 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003563
Chris Wilson25112b62017-03-30 15:50:39 +01003564 return ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003565}
3566
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003567static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj)
3568{
Chris Wilsone27ab732017-06-15 13:38:49 +01003569 /*
3570 * We manually flush the CPU domain so that we can override and
3571 * force the flush for the display, and perform it asyncrhonously.
3572 */
3573 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
3574 if (obj->cache_dirty)
3575 i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE);
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003576 obj->base.write_domain = 0;
3577}
3578
3579void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj)
3580{
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003581 if (!READ_ONCE(obj->pin_global))
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003582 return;
3583
3584 mutex_lock(&obj->base.dev->struct_mutex);
3585 __i915_gem_object_flush_for_display(obj);
3586 mutex_unlock(&obj->base.dev->struct_mutex);
3587}
3588
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003589/**
Chris Wilsone22d8e32017-04-12 12:01:11 +01003590 * Moves a single object to the WC read, and possibly write domain.
3591 * @obj: object to act on
3592 * @write: ask for write access or read only
3593 *
3594 * This function returns when the move is complete, including waiting on
3595 * flushes to occur.
3596 */
3597int
3598i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write)
3599{
3600 int ret;
3601
3602 lockdep_assert_held(&obj->base.dev->struct_mutex);
3603
3604 ret = i915_gem_object_wait(obj,
3605 I915_WAIT_INTERRUPTIBLE |
3606 I915_WAIT_LOCKED |
3607 (write ? I915_WAIT_ALL : 0),
3608 MAX_SCHEDULE_TIMEOUT,
3609 NULL);
3610 if (ret)
3611 return ret;
3612
3613 if (obj->base.write_domain == I915_GEM_DOMAIN_WC)
3614 return 0;
3615
3616 /* Flush and acquire obj->pages so that we are coherent through
3617 * direct access in memory with previous cached writes through
3618 * shmemfs and that our cache domain tracking remains valid.
3619 * For example, if the obj->filp was moved to swap without us
3620 * being notified and releasing the pages, we would mistakenly
3621 * continue to assume that the obj remained out of the CPU cached
3622 * domain.
3623 */
3624 ret = i915_gem_object_pin_pages(obj);
3625 if (ret)
3626 return ret;
3627
3628 flush_write_domain(obj, ~I915_GEM_DOMAIN_WC);
3629
3630 /* Serialise direct access to this object with the barriers for
3631 * coherent writes from the GPU, by effectively invalidating the
3632 * WC domain upon first access.
3633 */
3634 if ((obj->base.read_domains & I915_GEM_DOMAIN_WC) == 0)
3635 mb();
3636
3637 /* It should now be out of any other write domains, and we can update
3638 * the domain values for our changes.
3639 */
3640 GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_WC) != 0);
3641 obj->base.read_domains |= I915_GEM_DOMAIN_WC;
3642 if (write) {
3643 obj->base.read_domains = I915_GEM_DOMAIN_WC;
3644 obj->base.write_domain = I915_GEM_DOMAIN_WC;
3645 obj->mm.dirty = true;
3646 }
3647
3648 i915_gem_object_unpin_pages(obj);
3649 return 0;
3650}
3651
3652/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003653 * Moves a single object to the GTT read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003654 * @obj: object to act on
3655 * @write: ask for write access or read only
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003656 *
3657 * This function returns when the move is complete, including waiting on
3658 * flushes to occur.
3659 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003660int
Chris Wilson20217462010-11-23 15:26:33 +00003661i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003662{
Eric Anholte47c68e2008-11-14 13:35:19 -08003663 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003664
Chris Wilsone95433c2016-10-28 13:58:27 +01003665 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003666
Chris Wilsone95433c2016-10-28 13:58:27 +01003667 ret = i915_gem_object_wait(obj,
3668 I915_WAIT_INTERRUPTIBLE |
3669 I915_WAIT_LOCKED |
3670 (write ? I915_WAIT_ALL : 0),
3671 MAX_SCHEDULE_TIMEOUT,
3672 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00003673 if (ret)
3674 return ret;
3675
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003676 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
3677 return 0;
3678
Chris Wilson43566de2015-01-02 16:29:29 +05303679 /* Flush and acquire obj->pages so that we are coherent through
3680 * direct access in memory with previous cached writes through
3681 * shmemfs and that our cache domain tracking remains valid.
3682 * For example, if the obj->filp was moved to swap without us
3683 * being notified and releasing the pages, we would mistakenly
3684 * continue to assume that the obj remained out of the CPU cached
3685 * domain.
3686 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003687 ret = i915_gem_object_pin_pages(obj);
Chris Wilson43566de2015-01-02 16:29:29 +05303688 if (ret)
3689 return ret;
3690
Chris Wilsonef749212017-04-12 12:01:10 +01003691 flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003692
Chris Wilsond0a57782012-10-09 19:24:37 +01003693 /* Serialise direct access to this object with the barriers for
3694 * coherent writes from the GPU, by effectively invalidating the
3695 * GTT domain upon first access.
3696 */
3697 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
3698 mb();
3699
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003700 /* It should now be out of any other write domains, and we can update
3701 * the domain values for our changes.
3702 */
Chris Wilson40e62d52016-10-28 13:58:41 +01003703 GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
Chris Wilson05394f32010-11-08 19:18:58 +00003704 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003705 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003706 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
3707 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003708 obj->mm.dirty = true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003709 }
3710
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003711 i915_gem_object_unpin_pages(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003712 return 0;
3713}
3714
Chris Wilsonef55f922015-10-09 14:11:27 +01003715/**
3716 * Changes the cache-level of an object across all VMA.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003717 * @obj: object to act on
3718 * @cache_level: new cache level to set for the object
Chris Wilsonef55f922015-10-09 14:11:27 +01003719 *
3720 * After this function returns, the object will be in the new cache-level
3721 * across all GTT and the contents of the backing storage will be coherent,
3722 * with respect to the new cache-level. In order to keep the backing storage
3723 * coherent for all users, we only allow a single cache level to be set
3724 * globally on the object and prevent it from being changed whilst the
3725 * hardware is reading from the object. That is if the object is currently
3726 * on the scanout it will be set to uncached (or equivalent display
3727 * cache coherency) and all non-MOCS GPU access will also be uncached so
3728 * that all direct access to the scanout remains coherent.
3729 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003730int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3731 enum i915_cache_level cache_level)
3732{
Chris Wilsonaa653a62016-08-04 07:52:27 +01003733 struct i915_vma *vma;
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003734 int ret;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003735
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003736 lockdep_assert_held(&obj->base.dev->struct_mutex);
3737
Chris Wilsone4ffd172011-04-04 09:44:39 +01003738 if (obj->cache_level == cache_level)
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003739 return 0;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003740
Chris Wilsonef55f922015-10-09 14:11:27 +01003741 /* Inspect the list of currently bound VMA and unbind any that would
3742 * be invalid given the new cache-level. This is principally to
3743 * catch the issue of the CS prefetch crossing page boundaries and
3744 * reading an invalid PTE on older architectures.
3745 */
Chris Wilsonaa653a62016-08-04 07:52:27 +01003746restart:
3747 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003748 if (!drm_mm_node_allocated(&vma->node))
3749 continue;
3750
Chris Wilson20dfbde2016-08-04 16:32:30 +01003751 if (i915_vma_is_pinned(vma)) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003752 DRM_DEBUG("can not change the cache level of pinned objects\n");
3753 return -EBUSY;
3754 }
3755
Chris Wilsonaa653a62016-08-04 07:52:27 +01003756 if (i915_gem_valid_gtt_space(vma, cache_level))
3757 continue;
3758
3759 ret = i915_vma_unbind(vma);
3760 if (ret)
3761 return ret;
3762
3763 /* As unbinding may affect other elements in the
3764 * obj->vma_list (due to side-effects from retiring
3765 * an active vma), play safe and restart the iterator.
3766 */
3767 goto restart;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003768 }
3769
Chris Wilsonef55f922015-10-09 14:11:27 +01003770 /* We can reuse the existing drm_mm nodes but need to change the
3771 * cache-level on the PTE. We could simply unbind them all and
3772 * rebind with the correct cache-level on next use. However since
3773 * we already have a valid slot, dma mapping, pages etc, we may as
3774 * rewrite the PTE in the belief that doing so tramples upon less
3775 * state and so involves less work.
3776 */
Chris Wilson15717de2016-08-04 07:52:26 +01003777 if (obj->bind_count) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003778 /* Before we change the PTE, the GPU must not be accessing it.
3779 * If we wait upon the object, we know that all the bound
3780 * VMA are no longer active.
3781 */
Chris Wilsone95433c2016-10-28 13:58:27 +01003782 ret = i915_gem_object_wait(obj,
3783 I915_WAIT_INTERRUPTIBLE |
3784 I915_WAIT_LOCKED |
3785 I915_WAIT_ALL,
3786 MAX_SCHEDULE_TIMEOUT,
3787 NULL);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003788 if (ret)
3789 return ret;
3790
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00003791 if (!HAS_LLC(to_i915(obj->base.dev)) &&
3792 cache_level != I915_CACHE_NONE) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003793 /* Access to snoopable pages through the GTT is
3794 * incoherent and on some machines causes a hard
3795 * lockup. Relinquish the CPU mmaping to force
3796 * userspace to refault in the pages and we can
3797 * then double check if the GTT mapping is still
3798 * valid for that pointer access.
3799 */
3800 i915_gem_release_mmap(obj);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003801
Chris Wilsonef55f922015-10-09 14:11:27 +01003802 /* As we no longer need a fence for GTT access,
3803 * we can relinquish it now (and so prevent having
3804 * to steal a fence from someone else on the next
3805 * fence request). Note GPU activity would have
3806 * dropped the fence as all snoopable access is
3807 * supposed to be linear.
3808 */
Chris Wilson49ef5292016-08-18 17:17:00 +01003809 list_for_each_entry(vma, &obj->vma_list, obj_link) {
3810 ret = i915_vma_put_fence(vma);
3811 if (ret)
3812 return ret;
3813 }
Chris Wilsonef55f922015-10-09 14:11:27 +01003814 } else {
3815 /* We either have incoherent backing store and
3816 * so no GTT access or the architecture is fully
3817 * coherent. In such cases, existing GTT mmaps
3818 * ignore the cache bit in the PTE and we can
3819 * rewrite it without confusing the GPU or having
3820 * to force userspace to fault back in its mmaps.
3821 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003822 }
3823
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003824 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003825 if (!drm_mm_node_allocated(&vma->node))
3826 continue;
3827
3828 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
3829 if (ret)
3830 return ret;
3831 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01003832 }
3833
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003834 list_for_each_entry(vma, &obj->vma_list, obj_link)
Chris Wilson2c225692013-08-09 12:26:45 +01003835 vma->node.color = cache_level;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01003836 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01003837 obj->cache_dirty = true; /* Always invalidate stale cachelines */
Chris Wilson2c225692013-08-09 12:26:45 +01003838
Chris Wilsone4ffd172011-04-04 09:44:39 +01003839 return 0;
3840}
3841
Ben Widawsky199adf42012-09-21 17:01:20 -07003842int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
3843 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003844{
Ben Widawsky199adf42012-09-21 17:01:20 -07003845 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003846 struct drm_i915_gem_object *obj;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003847 int err = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003848
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003849 rcu_read_lock();
3850 obj = i915_gem_object_lookup_rcu(file, args->handle);
3851 if (!obj) {
3852 err = -ENOENT;
3853 goto out;
3854 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01003855
Chris Wilson651d7942013-08-08 14:41:10 +01003856 switch (obj->cache_level) {
3857 case I915_CACHE_LLC:
3858 case I915_CACHE_L3_LLC:
3859 args->caching = I915_CACHING_CACHED;
3860 break;
3861
Chris Wilson4257d3b2013-08-08 14:41:11 +01003862 case I915_CACHE_WT:
3863 args->caching = I915_CACHING_DISPLAY;
3864 break;
3865
Chris Wilson651d7942013-08-08 14:41:10 +01003866 default:
3867 args->caching = I915_CACHING_NONE;
3868 break;
3869 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003870out:
3871 rcu_read_unlock();
3872 return err;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003873}
3874
Ben Widawsky199adf42012-09-21 17:01:20 -07003875int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
3876 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003877{
Chris Wilson9c870d02016-10-24 13:42:15 +01003878 struct drm_i915_private *i915 = to_i915(dev);
Ben Widawsky199adf42012-09-21 17:01:20 -07003879 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003880 struct drm_i915_gem_object *obj;
3881 enum i915_cache_level level;
Chris Wilsond65415d2017-01-19 08:22:10 +00003882 int ret = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003883
Ben Widawsky199adf42012-09-21 17:01:20 -07003884 switch (args->caching) {
3885 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01003886 level = I915_CACHE_NONE;
3887 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07003888 case I915_CACHING_CACHED:
Imre Deake5756c12015-08-14 18:43:30 +03003889 /*
3890 * Due to a HW issue on BXT A stepping, GPU stores via a
3891 * snooped mapping may leave stale data in a corresponding CPU
3892 * cacheline, whereas normally such cachelines would get
3893 * invalidated.
3894 */
Chris Wilson9c870d02016-10-24 13:42:15 +01003895 if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
Imre Deake5756c12015-08-14 18:43:30 +03003896 return -ENODEV;
3897
Chris Wilsone6994ae2012-07-10 10:27:08 +01003898 level = I915_CACHE_LLC;
3899 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003900 case I915_CACHING_DISPLAY:
Chris Wilson9c870d02016-10-24 13:42:15 +01003901 level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003902 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003903 default:
3904 return -EINVAL;
3905 }
3906
Chris Wilsond65415d2017-01-19 08:22:10 +00003907 obj = i915_gem_object_lookup(file, args->handle);
3908 if (!obj)
3909 return -ENOENT;
3910
Tina Zhanga03f3952017-11-14 10:25:13 +00003911 /*
3912 * The caching mode of proxy object is handled by its generator, and
3913 * not allowed to be changed by userspace.
3914 */
3915 if (i915_gem_object_is_proxy(obj)) {
3916 ret = -ENXIO;
3917 goto out;
3918 }
3919
Chris Wilsond65415d2017-01-19 08:22:10 +00003920 if (obj->cache_level == level)
3921 goto out;
3922
3923 ret = i915_gem_object_wait(obj,
3924 I915_WAIT_INTERRUPTIBLE,
3925 MAX_SCHEDULE_TIMEOUT,
3926 to_rps_client(file));
3927 if (ret)
3928 goto out;
3929
Ben Widawsky3bc29132012-09-26 16:15:20 -07003930 ret = i915_mutex_lock_interruptible(dev);
3931 if (ret)
Chris Wilsond65415d2017-01-19 08:22:10 +00003932 goto out;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003933
3934 ret = i915_gem_object_set_cache_level(obj, level);
Chris Wilsone6994ae2012-07-10 10:27:08 +01003935 mutex_unlock(&dev->struct_mutex);
Chris Wilsond65415d2017-01-19 08:22:10 +00003936
3937out:
3938 i915_gem_object_put(obj);
Chris Wilsone6994ae2012-07-10 10:27:08 +01003939 return ret;
3940}
3941
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003942/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003943 * Prepare buffer for display plane (scanout, cursors, etc).
3944 * Can be called from an uninterruptible phase (modesetting) and allows
3945 * any flushes to be pipelined (for pageflips).
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003946 */
Chris Wilson058d88c2016-08-15 10:49:06 +01003947struct i915_vma *
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003948i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3949 u32 alignment,
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003950 const struct i915_ggtt_view *view)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003951{
Chris Wilson058d88c2016-08-15 10:49:06 +01003952 struct i915_vma *vma;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003953 int ret;
3954
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003955 lockdep_assert_held(&obj->base.dev->struct_mutex);
3956
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003957 /* Mark the global pin early so that we account for the
Chris Wilsoncc98b412013-08-09 12:25:09 +01003958 * display coherency whilst setting up the cache domains.
3959 */
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003960 obj->pin_global++;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003961
Eric Anholta7ef0642011-03-29 16:59:54 -07003962 /* The display engine is not coherent with the LLC cache on gen6. As
3963 * a result, we make sure that the pinning that is about to occur is
3964 * done with uncached PTEs. This is lowest common denominator for all
3965 * chipsets.
3966 *
3967 * However for gen6+, we could do better by using the GFDT bit instead
3968 * of uncaching, which would allow us to flush all the LLC-cached data
3969 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3970 */
Chris Wilson651d7942013-08-08 14:41:10 +01003971 ret = i915_gem_object_set_cache_level(obj,
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003972 HAS_WT(to_i915(obj->base.dev)) ?
3973 I915_CACHE_WT : I915_CACHE_NONE);
Chris Wilson058d88c2016-08-15 10:49:06 +01003974 if (ret) {
3975 vma = ERR_PTR(ret);
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003976 goto err_unpin_global;
Chris Wilson058d88c2016-08-15 10:49:06 +01003977 }
Eric Anholta7ef0642011-03-29 16:59:54 -07003978
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003979 /* As the user may map the buffer once pinned in the display plane
3980 * (e.g. libkms for the bootup splash), we have to ensure that we
Chris Wilson2efb8132016-08-18 17:17:06 +01003981 * always use map_and_fenceable for all scanout buffers. However,
3982 * it may simply be too big to fit into mappable, in which case
3983 * put it anyway and hope that userspace can cope (but always first
3984 * try to preserve the existing ABI).
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003985 */
Chris Wilson2efb8132016-08-18 17:17:06 +01003986 vma = ERR_PTR(-ENOSPC);
Chris Wilson47a8e3f2017-01-14 00:28:27 +00003987 if (!view || view->type == I915_GGTT_VIEW_NORMAL)
Chris Wilson2efb8132016-08-18 17:17:06 +01003988 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
3989 PIN_MAPPABLE | PIN_NONBLOCK);
Chris Wilson767a2222016-11-07 11:01:28 +00003990 if (IS_ERR(vma)) {
3991 struct drm_i915_private *i915 = to_i915(obj->base.dev);
3992 unsigned int flags;
3993
3994 /* Valleyview is definitely limited to scanning out the first
3995 * 512MiB. Lets presume this behaviour was inherited from the
3996 * g4x display engine and that all earlier gen are similarly
3997 * limited. Testing suggests that it is a little more
3998 * complicated than this. For example, Cherryview appears quite
3999 * happy to scanout from anywhere within its global aperture.
4000 */
4001 flags = 0;
4002 if (HAS_GMCH_DISPLAY(i915))
4003 flags = PIN_MAPPABLE;
4004 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags);
4005 }
Chris Wilson058d88c2016-08-15 10:49:06 +01004006 if (IS_ERR(vma))
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004007 goto err_unpin_global;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004008
Chris Wilsond8923dc2016-08-18 17:17:07 +01004009 vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
4010
Chris Wilsona6a7cc42016-11-18 21:17:46 +00004011 /* Treat this as an end-of-frame, like intel_user_framebuffer_dirty() */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00004012 __i915_gem_object_flush_for_display(obj);
Chris Wilsond59b21e2017-02-22 11:40:49 +00004013 intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01004014
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004015 /* It should now be out of any other write domains, and we can update
4016 * the domain values for our changes.
4017 */
Chris Wilson05394f32010-11-08 19:18:58 +00004018 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004019
Chris Wilson058d88c2016-08-15 10:49:06 +01004020 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004021
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004022err_unpin_global:
4023 obj->pin_global--;
Chris Wilson058d88c2016-08-15 10:49:06 +01004024 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004025}
4026
4027void
Chris Wilson058d88c2016-08-15 10:49:06 +01004028i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
Chris Wilsoncc98b412013-08-09 12:25:09 +01004029{
Chris Wilson49d73912016-11-29 09:50:08 +00004030 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004031
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004032 if (WARN_ON(vma->obj->pin_global == 0))
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01004033 return;
4034
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004035 if (--vma->obj->pin_global == 0)
Chris Wilsonf51455d2017-01-10 14:47:34 +00004036 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004037
Chris Wilson383d5822016-08-18 17:17:08 +01004038 /* Bump the LRU to try and avoid premature eviction whilst flipping */
Chris Wilsonbefedbb2017-01-19 19:26:55 +00004039 i915_gem_object_bump_inactive_ggtt(vma->obj);
Chris Wilson383d5822016-08-18 17:17:08 +01004040
Chris Wilson058d88c2016-08-15 10:49:06 +01004041 i915_vma_unpin(vma);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004042}
4043
Eric Anholte47c68e2008-11-14 13:35:19 -08004044/**
4045 * Moves a single object to the CPU read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01004046 * @obj: object to act on
4047 * @write: requesting write or read-only access
Eric Anholte47c68e2008-11-14 13:35:19 -08004048 *
4049 * This function returns when the move is complete, including waiting on
4050 * flushes to occur.
4051 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02004052int
Chris Wilson919926a2010-11-12 13:42:53 +00004053i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08004054{
Eric Anholte47c68e2008-11-14 13:35:19 -08004055 int ret;
4056
Chris Wilsone95433c2016-10-28 13:58:27 +01004057 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004058
Chris Wilsone95433c2016-10-28 13:58:27 +01004059 ret = i915_gem_object_wait(obj,
4060 I915_WAIT_INTERRUPTIBLE |
4061 I915_WAIT_LOCKED |
4062 (write ? I915_WAIT_ALL : 0),
4063 MAX_SCHEDULE_TIMEOUT,
4064 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00004065 if (ret)
4066 return ret;
4067
Chris Wilsonef749212017-04-12 12:01:10 +01004068 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08004069
Eric Anholte47c68e2008-11-14 13:35:19 -08004070 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00004071 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson57822dc2017-02-22 11:40:48 +00004072 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
Chris Wilson05394f32010-11-08 19:18:58 +00004073 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08004074 }
4075
4076 /* It should now be out of any other write domains, and we can update
4077 * the domain values for our changes.
4078 */
Chris Wilsone27ab732017-06-15 13:38:49 +01004079 GEM_BUG_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08004080
4081 /* If we're writing through the CPU, then the GPU read domains will
4082 * need to be invalidated at next use.
4083 */
Chris Wilsone27ab732017-06-15 13:38:49 +01004084 if (write)
4085 __start_cpu_write(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08004086
4087 return 0;
4088}
4089
Eric Anholt673a3942008-07-30 12:06:12 -07004090/* Throttle our rendering by waiting until the ring has completed our requests
4091 * emitted over 20 msec ago.
4092 *
Eric Anholtb9624422009-06-03 07:27:35 +00004093 * Note that if we were to use the current jiffies each time around the loop,
4094 * we wouldn't escape the function with any frames outstanding if the time to
4095 * render a frame was over 20ms.
4096 *
Eric Anholt673a3942008-07-30 12:06:12 -07004097 * This should get us reasonable parallelism between CPU and GPU but also
4098 * relatively low latency when blocking on a particular request to finish.
4099 */
4100static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004101i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004102{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004103 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004104 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsond0bc54f2015-05-21 21:01:48 +01004105 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
John Harrison54fb2412014-11-24 18:49:27 +00004106 struct drm_i915_gem_request *request, *target = NULL;
Chris Wilsone95433c2016-10-28 13:58:27 +01004107 long ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004108
Chris Wilsonf4457ae2016-04-13 17:35:08 +01004109 /* ABI: return -EIO if already wedged */
4110 if (i915_terminally_wedged(&dev_priv->gpu_error))
4111 return -EIO;
Chris Wilsone110e8d2011-01-26 15:39:14 +00004112
Chris Wilson1c255952010-09-26 11:03:27 +01004113 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004114 list_for_each_entry(request, &file_priv->mm.request_list, client_link) {
Eric Anholtb9624422009-06-03 07:27:35 +00004115 if (time_after_eq(request->emitted_jiffies, recent_enough))
4116 break;
4117
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004118 if (target) {
4119 list_del(&target->client_link);
4120 target->file_priv = NULL;
4121 }
John Harrisonfcfa423c2015-05-29 17:44:12 +01004122
John Harrison54fb2412014-11-24 18:49:27 +00004123 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00004124 }
John Harrisonff865882014-11-24 18:49:28 +00004125 if (target)
Chris Wilsone8a261e2016-07-20 13:31:49 +01004126 i915_gem_request_get(target);
Chris Wilson1c255952010-09-26 11:03:27 +01004127 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004128
John Harrison54fb2412014-11-24 18:49:27 +00004129 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004130 return 0;
4131
Chris Wilsone95433c2016-10-28 13:58:27 +01004132 ret = i915_wait_request(target,
4133 I915_WAIT_INTERRUPTIBLE,
4134 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone8a261e2016-07-20 13:31:49 +01004135 i915_gem_request_put(target);
John Harrisonff865882014-11-24 18:49:28 +00004136
Chris Wilsone95433c2016-10-28 13:58:27 +01004137 return ret < 0 ? ret : 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004138}
4139
Chris Wilson058d88c2016-08-15 10:49:06 +01004140struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004141i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
4142 const struct i915_ggtt_view *view,
Chris Wilson91b2db62016-08-04 16:32:23 +01004143 u64 size,
Chris Wilson2ffffd02016-08-04 16:32:22 +01004144 u64 alignment,
4145 u64 flags)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004146{
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004147 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
4148 struct i915_address_space *vm = &dev_priv->ggtt.base;
Chris Wilson59bfa122016-08-04 16:32:31 +01004149 struct i915_vma *vma;
4150 int ret;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03004151
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004152 lockdep_assert_held(&obj->base.dev->struct_mutex);
4153
Chris Wilson43ae70d92017-10-09 09:44:01 +01004154 if (!view && flags & PIN_MAPPABLE) {
4155 /* If the required space is larger than the available
4156 * aperture, we will not able to find a slot for the
4157 * object and unbinding the object now will be in
4158 * vain. Worse, doing so may cause us to ping-pong
4159 * the object in and out of the Global GTT and
4160 * waste a lot of cycles under the mutex.
4161 */
4162 if (obj->base.size > dev_priv->ggtt.mappable_end)
4163 return ERR_PTR(-E2BIG);
4164
4165 /* If NONBLOCK is set the caller is optimistically
4166 * trying to cache the full object within the mappable
4167 * aperture, and *must* have a fallback in place for
4168 * situations where we cannot bind the object. We
4169 * can be a little more lax here and use the fallback
4170 * more often to avoid costly migrations of ourselves
4171 * and other objects within the aperture.
4172 *
4173 * Half-the-aperture is used as a simple heuristic.
4174 * More interesting would to do search for a free
4175 * block prior to making the commitment to unbind.
4176 * That caters for the self-harm case, and with a
4177 * little more heuristics (e.g. NOFAULT, NOEVICT)
4178 * we could try to minimise harm to others.
4179 */
4180 if (flags & PIN_NONBLOCK &&
4181 obj->base.size > dev_priv->ggtt.mappable_end / 2)
4182 return ERR_PTR(-ENOSPC);
4183 }
4184
Chris Wilson718659a2017-01-16 15:21:28 +00004185 vma = i915_vma_instance(obj, vm, view);
Chris Wilsone0216b72017-01-19 19:26:57 +00004186 if (unlikely(IS_ERR(vma)))
Chris Wilson058d88c2016-08-15 10:49:06 +01004187 return vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01004188
4189 if (i915_vma_misplaced(vma, size, alignment, flags)) {
Chris Wilson43ae70d92017-10-09 09:44:01 +01004190 if (flags & PIN_NONBLOCK) {
4191 if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
4192 return ERR_PTR(-ENOSPC);
Chris Wilson59bfa122016-08-04 16:32:31 +01004193
Chris Wilson43ae70d92017-10-09 09:44:01 +01004194 if (flags & PIN_MAPPABLE &&
Chris Wilson944397f2017-01-09 16:16:11 +00004195 vma->fence_size > dev_priv->ggtt.mappable_end / 2)
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004196 return ERR_PTR(-ENOSPC);
4197 }
4198
Chris Wilson59bfa122016-08-04 16:32:31 +01004199 WARN(i915_vma_is_pinned(vma),
4200 "bo is already pinned in ggtt with incorrect alignment:"
Chris Wilson05a20d02016-08-18 17:16:55 +01004201 " offset=%08x, req.alignment=%llx,"
4202 " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
4203 i915_ggtt_offset(vma), alignment,
Chris Wilson59bfa122016-08-04 16:32:31 +01004204 !!(flags & PIN_MAPPABLE),
Chris Wilson05a20d02016-08-18 17:16:55 +01004205 i915_vma_is_map_and_fenceable(vma));
Chris Wilson59bfa122016-08-04 16:32:31 +01004206 ret = i915_vma_unbind(vma);
4207 if (ret)
Chris Wilson058d88c2016-08-15 10:49:06 +01004208 return ERR_PTR(ret);
Chris Wilson59bfa122016-08-04 16:32:31 +01004209 }
4210
Chris Wilson058d88c2016-08-15 10:49:06 +01004211 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
4212 if (ret)
4213 return ERR_PTR(ret);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004214
Chris Wilson058d88c2016-08-15 10:49:06 +01004215 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07004216}
4217
Chris Wilsonedf6b762016-08-09 09:23:33 +01004218static __always_inline unsigned int __busy_read_flag(unsigned int id)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004219{
4220 /* Note that we could alias engines in the execbuf API, but
4221 * that would be very unwise as it prevents userspace from
4222 * fine control over engine selection. Ahem.
4223 *
4224 * This should be something like EXEC_MAX_ENGINE instead of
4225 * I915_NUM_ENGINES.
4226 */
4227 BUILD_BUG_ON(I915_NUM_ENGINES > 16);
4228 return 0x10000 << id;
4229}
4230
4231static __always_inline unsigned int __busy_write_id(unsigned int id)
4232{
Chris Wilson70cb4722016-08-09 18:08:25 +01004233 /* The uABI guarantees an active writer is also amongst the read
4234 * engines. This would be true if we accessed the activity tracking
4235 * under the lock, but as we perform the lookup of the object and
4236 * its activity locklessly we can not guarantee that the last_write
4237 * being active implies that we have set the same engine flag from
4238 * last_read - hence we always set both read and write busy for
4239 * last_write.
4240 */
4241 return id | __busy_read_flag(id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004242}
4243
Chris Wilsonedf6b762016-08-09 09:23:33 +01004244static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004245__busy_set_if_active(const struct dma_fence *fence,
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004246 unsigned int (*flag)(unsigned int id))
4247{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004248 struct drm_i915_gem_request *rq;
Chris Wilson12555012016-08-16 09:50:40 +01004249
Chris Wilsond07f0e52016-10-28 13:58:44 +01004250 /* We have to check the current hw status of the fence as the uABI
4251 * guarantees forward progress. We could rely on the idle worker
4252 * to eventually flush us, but to minimise latency just ask the
4253 * hardware.
4254 *
4255 * Note we only report on the status of native fences.
4256 */
4257 if (!dma_fence_is_i915(fence))
Chris Wilson12555012016-08-16 09:50:40 +01004258 return 0;
4259
Chris Wilsond07f0e52016-10-28 13:58:44 +01004260 /* opencode to_request() in order to avoid const warnings */
4261 rq = container_of(fence, struct drm_i915_gem_request, fence);
4262 if (i915_gem_request_completed(rq))
4263 return 0;
4264
Chris Wilson1d39f282017-04-11 13:43:06 +01004265 return flag(rq->engine->uabi_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004266}
4267
Chris Wilsonedf6b762016-08-09 09:23:33 +01004268static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004269busy_check_reader(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004270{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004271 return __busy_set_if_active(fence, __busy_read_flag);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004272}
4273
Chris Wilsonedf6b762016-08-09 09:23:33 +01004274static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004275busy_check_writer(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004276{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004277 if (!fence)
4278 return 0;
4279
4280 return __busy_set_if_active(fence, __busy_write_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004281}
4282
Eric Anholt673a3942008-07-30 12:06:12 -07004283int
Eric Anholt673a3942008-07-30 12:06:12 -07004284i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004285 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004286{
4287 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004288 struct drm_i915_gem_object *obj;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004289 struct reservation_object_list *list;
4290 unsigned int seq;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004291 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07004292
Chris Wilsond07f0e52016-10-28 13:58:44 +01004293 err = -ENOENT;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004294 rcu_read_lock();
4295 obj = i915_gem_object_lookup_rcu(file, args->handle);
Chris Wilsond07f0e52016-10-28 13:58:44 +01004296 if (!obj)
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004297 goto out;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004298
4299 /* A discrepancy here is that we do not report the status of
4300 * non-i915 fences, i.e. even though we may report the object as idle,
4301 * a call to set-domain may still stall waiting for foreign rendering.
4302 * This also means that wait-ioctl may report an object as busy,
4303 * where busy-ioctl considers it idle.
4304 *
4305 * We trade the ability to warn of foreign fences to report on which
4306 * i915 engines are active for the object.
4307 *
4308 * Alternatively, we can trade that extra information on read/write
4309 * activity with
4310 * args->busy =
4311 * !reservation_object_test_signaled_rcu(obj->resv, true);
4312 * to report the overall busyness. This is what the wait-ioctl does.
4313 *
4314 */
4315retry:
4316 seq = raw_read_seqcount(&obj->resv->seq);
4317
4318 /* Translate the exclusive fence to the READ *and* WRITE engine */
4319 args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl));
4320
4321 /* Translate shared fences to READ set of engines */
4322 list = rcu_dereference(obj->resv->fence);
4323 if (list) {
4324 unsigned int shared_count = list->shared_count, i;
4325
4326 for (i = 0; i < shared_count; ++i) {
4327 struct dma_fence *fence =
4328 rcu_dereference(list->shared[i]);
4329
4330 args->busy |= busy_check_reader(fence);
4331 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004332 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08004333
Chris Wilsond07f0e52016-10-28 13:58:44 +01004334 if (args->busy && read_seqcount_retry(&obj->resv->seq, seq))
4335 goto retry;
Chris Wilson426960b2016-01-15 16:51:46 +00004336
Chris Wilsond07f0e52016-10-28 13:58:44 +01004337 err = 0;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004338out:
4339 rcu_read_unlock();
4340 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07004341}
4342
4343int
4344i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4345 struct drm_file *file_priv)
4346{
Akshay Joshi0206e352011-08-16 15:34:10 -04004347 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004348}
4349
Chris Wilson3ef94da2009-09-14 16:50:29 +01004350int
4351i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4352 struct drm_file *file_priv)
4353{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004354 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004355 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004356 struct drm_i915_gem_object *obj;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004357 int err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004358
4359 switch (args->madv) {
4360 case I915_MADV_DONTNEED:
4361 case I915_MADV_WILLNEED:
4362 break;
4363 default:
4364 return -EINVAL;
4365 }
4366
Chris Wilson03ac0642016-07-20 13:31:51 +01004367 obj = i915_gem_object_lookup(file_priv, args->handle);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004368 if (!obj)
4369 return -ENOENT;
4370
4371 err = mutex_lock_interruptible(&obj->mm.lock);
4372 if (err)
4373 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004374
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004375 if (i915_gem_object_has_pages(obj) &&
Chris Wilson3e510a82016-08-05 10:14:23 +01004376 i915_gem_object_is_tiled(obj) &&
Daniel Vetter656bfa32014-11-20 09:26:30 +01004377 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004378 if (obj->mm.madv == I915_MADV_WILLNEED) {
4379 GEM_BUG_ON(!obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004380 __i915_gem_object_unpin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004381 obj->mm.quirked = false;
4382 }
4383 if (args->madv == I915_MADV_WILLNEED) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00004384 GEM_BUG_ON(obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004385 __i915_gem_object_pin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004386 obj->mm.quirked = true;
4387 }
Daniel Vetter656bfa32014-11-20 09:26:30 +01004388 }
4389
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004390 if (obj->mm.madv != __I915_MADV_PURGED)
4391 obj->mm.madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004392
Chris Wilson6c085a72012-08-20 11:40:46 +02004393 /* if the object is no longer attached, discard its backing storage */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004394 if (obj->mm.madv == I915_MADV_DONTNEED &&
4395 !i915_gem_object_has_pages(obj))
Chris Wilson2d7ef392009-09-20 23:13:10 +01004396 i915_gem_object_truncate(obj);
4397
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004398 args->retained = obj->mm.madv != __I915_MADV_PURGED;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004399 mutex_unlock(&obj->mm.lock);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004400
Chris Wilson1233e2d2016-10-28 13:58:37 +01004401out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004402 i915_gem_object_put(obj);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004403 return err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004404}
4405
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004406static void
4407frontbuffer_retire(struct i915_gem_active *active,
4408 struct drm_i915_gem_request *request)
4409{
4410 struct drm_i915_gem_object *obj =
4411 container_of(active, typeof(*obj), frontbuffer_write);
4412
Chris Wilsond59b21e2017-02-22 11:40:49 +00004413 intel_fb_obj_flush(obj, ORIGIN_CS);
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004414}
4415
Chris Wilson37e680a2012-06-07 15:38:42 +01004416void i915_gem_object_init(struct drm_i915_gem_object *obj,
4417 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01004418{
Chris Wilson1233e2d2016-10-28 13:58:37 +01004419 mutex_init(&obj->mm.lock);
4420
Ben Widawsky2f633152013-07-17 12:19:03 -07004421 INIT_LIST_HEAD(&obj->vma_list);
Chris Wilsond1b48c12017-08-16 09:52:08 +01004422 INIT_LIST_HEAD(&obj->lut_list);
Chris Wilson8d9d5742015-04-07 16:20:38 +01004423 INIT_LIST_HEAD(&obj->batch_pool_link);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004424
Chris Wilson37e680a2012-06-07 15:38:42 +01004425 obj->ops = ops;
4426
Chris Wilsond07f0e52016-10-28 13:58:44 +01004427 reservation_object_init(&obj->__builtin_resv);
4428 obj->resv = &obj->__builtin_resv;
4429
Chris Wilson50349242016-08-18 17:17:04 +01004430 obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004431 init_request_active(&obj->frontbuffer_write, frontbuffer_retire);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004432
4433 obj->mm.madv = I915_MADV_WILLNEED;
4434 INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
4435 mutex_init(&obj->mm.get_page.lock);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004436
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004437 i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004438}
4439
Chris Wilson37e680a2012-06-07 15:38:42 +01004440static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
Tvrtko Ursulin3599a912016-11-01 14:44:10 +00004441 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
4442 I915_GEM_OBJECT_IS_SHRINKABLE,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004443
Chris Wilson37e680a2012-06-07 15:38:42 +01004444 .get_pages = i915_gem_object_get_pages_gtt,
4445 .put_pages = i915_gem_object_put_pages_gtt,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004446
4447 .pwrite = i915_gem_object_pwrite_gtt,
Chris Wilson37e680a2012-06-07 15:38:42 +01004448};
4449
Matthew Auld465c4032017-10-06 23:18:14 +01004450static int i915_gem_object_create_shmem(struct drm_device *dev,
4451 struct drm_gem_object *obj,
4452 size_t size)
4453{
4454 struct drm_i915_private *i915 = to_i915(dev);
4455 unsigned long flags = VM_NORESERVE;
4456 struct file *filp;
4457
4458 drm_gem_private_object_init(dev, obj, size);
4459
4460 if (i915->mm.gemfs)
4461 filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size,
4462 flags);
4463 else
4464 filp = shmem_file_setup("i915", size, flags);
4465
4466 if (IS_ERR(filp))
4467 return PTR_ERR(filp);
4468
4469 obj->filp = filp;
4470
4471 return 0;
4472}
4473
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004474struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004475i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004476{
Daniel Vetterc397b902010-04-09 19:05:07 +00004477 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004478 struct address_space *mapping;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004479 unsigned int cache_level;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004480 gfp_t mask;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004481 int ret;
Daniel Vetterc397b902010-04-09 19:05:07 +00004482
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004483 /* There is a prevalence of the assumption that we fit the object's
4484 * page count inside a 32bit _signed_ variable. Let's document this and
4485 * catch if we ever need to fix it. In the meantime, if you do spot
4486 * such a local variable, please consider fixing!
4487 */
Tvrtko Ursulin7a3ee5d2017-03-30 17:31:30 +01004488 if (size >> PAGE_SHIFT > INT_MAX)
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004489 return ERR_PTR(-E2BIG);
4490
4491 if (overflows_type(size, obj->base.size))
4492 return ERR_PTR(-E2BIG);
4493
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00004494 obj = i915_gem_object_alloc(dev_priv);
Daniel Vetterc397b902010-04-09 19:05:07 +00004495 if (obj == NULL)
Chris Wilsonfe3db792016-04-25 13:32:13 +01004496 return ERR_PTR(-ENOMEM);
Daniel Vetterc397b902010-04-09 19:05:07 +00004497
Matthew Auld465c4032017-10-06 23:18:14 +01004498 ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004499 if (ret)
4500 goto fail;
Daniel Vetterc397b902010-04-09 19:05:07 +00004501
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004502 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
Jani Nikulac0f86832016-12-07 12:13:04 +02004503 if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) {
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004504 /* 965gm cannot relocate objects above 4GiB. */
4505 mask &= ~__GFP_HIGHMEM;
4506 mask |= __GFP_DMA32;
4507 }
4508
Al Viro93c76a32015-12-04 23:45:44 -05004509 mapping = obj->base.filp->f_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004510 mapping_set_gfp_mask(mapping, mask);
Chris Wilson4846bf02017-06-09 12:03:46 +01004511 GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM));
Hugh Dickins5949eac2011-06-27 16:18:18 -07004512
Chris Wilson37e680a2012-06-07 15:38:42 +01004513 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004514
Daniel Vetterc397b902010-04-09 19:05:07 +00004515 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4516 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4517
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004518 if (HAS_LLC(dev_priv))
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004519 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004520 * cache) for about a 10% performance improvement
4521 * compared to uncached. Graphics requests other than
4522 * display scanout are coherent with the CPU in
4523 * accessing this cache. This means in this mode we
4524 * don't need to clflush on the CPU side, and on the
4525 * GPU side we only need to flush internal caches to
4526 * get data visible to the CPU.
4527 *
4528 * However, we maintain the display planes as UC, and so
4529 * need to rebind when first used as such.
4530 */
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004531 cache_level = I915_CACHE_LLC;
4532 else
4533 cache_level = I915_CACHE_NONE;
Eric Anholta1871112011-03-29 16:59:55 -07004534
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004535 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01004536
Daniel Vetterd861e332013-07-24 23:25:03 +02004537 trace_i915_gem_object_create(obj);
4538
Chris Wilson05394f32010-11-08 19:18:58 +00004539 return obj;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004540
4541fail:
4542 i915_gem_object_free(obj);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004543 return ERR_PTR(ret);
Daniel Vetterac52bc52010-04-09 19:05:06 +00004544}
4545
Chris Wilson340fbd82014-05-22 09:16:52 +01004546static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4547{
4548 /* If we are the last user of the backing storage (be it shmemfs
4549 * pages or stolen etc), we know that the pages are going to be
4550 * immediately released. In this case, we can then skip copying
4551 * back the contents from the GPU.
4552 */
4553
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004554 if (obj->mm.madv != I915_MADV_WILLNEED)
Chris Wilson340fbd82014-05-22 09:16:52 +01004555 return false;
4556
4557 if (obj->base.filp == NULL)
4558 return true;
4559
4560 /* At first glance, this looks racy, but then again so would be
4561 * userspace racing mmap against close. However, the first external
4562 * reference to the filp can only be obtained through the
4563 * i915_gem_mmap_ioctl() which safeguards us against the user
4564 * acquiring such a reference whilst we are in the middle of
4565 * freeing the object.
4566 */
4567 return atomic_long_read(&obj->base.filp->f_count) == 1;
4568}
4569
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004570static void __i915_gem_free_objects(struct drm_i915_private *i915,
4571 struct llist_node *freed)
Chris Wilsonbe726152010-07-23 23:18:50 +01004572{
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004573 struct drm_i915_gem_object *obj, *on;
Chris Wilsonbe726152010-07-23 23:18:50 +01004574
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004575 intel_runtime_pm_get(i915);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004576 llist_for_each_entry_safe(obj, on, freed, freed) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004577 struct i915_vma *vma, *vn;
Paulo Zanonif65c9162013-11-27 18:20:34 -02004578
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004579 trace_i915_gem_object_destroy(obj);
4580
Chris Wilsoncc731f52017-10-13 21:26:21 +01004581 mutex_lock(&i915->drm.struct_mutex);
4582
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004583 GEM_BUG_ON(i915_gem_object_is_active(obj));
4584 list_for_each_entry_safe(vma, vn,
4585 &obj->vma_list, obj_link) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004586 GEM_BUG_ON(i915_vma_is_active(vma));
4587 vma->flags &= ~I915_VMA_PIN_MASK;
4588 i915_vma_close(vma);
4589 }
Chris Wilsondb6c2b42016-11-01 11:54:00 +00004590 GEM_BUG_ON(!list_empty(&obj->vma_list));
4591 GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004592
Chris Wilsonf2123812017-10-16 12:40:37 +01004593 /* This serializes freeing with the shrinker. Since the free
4594 * is delayed, first by RCU then by the workqueue, we want the
4595 * shrinker to be able to free pages of unreferenced objects,
4596 * or else we may oom whilst there are plenty of deferred
4597 * freed objects.
4598 */
4599 if (i915_gem_object_has_pages(obj)) {
4600 spin_lock(&i915->mm.obj_lock);
4601 list_del_init(&obj->mm.link);
4602 spin_unlock(&i915->mm.obj_lock);
4603 }
4604
Chris Wilsoncc731f52017-10-13 21:26:21 +01004605 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004606
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004607 GEM_BUG_ON(obj->bind_count);
Chris Wilsona65adaf2017-10-09 09:43:57 +01004608 GEM_BUG_ON(obj->userfault_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004609 GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits));
Chris Wilson67b48042017-08-22 12:05:16 +01004610 GEM_BUG_ON(!list_empty(&obj->lut_list));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004611
4612 if (obj->ops->release)
4613 obj->ops->release(obj);
4614
4615 if (WARN_ON(i915_gem_object_has_pinned_pages(obj)))
4616 atomic_set(&obj->mm.pages_pin_count, 0);
Chris Wilson548625e2016-11-01 12:11:34 +00004617 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004618 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004619
4620 if (obj->base.import_attach)
4621 drm_prime_gem_destroy(&obj->base, NULL);
4622
Chris Wilsond07f0e52016-10-28 13:58:44 +01004623 reservation_object_fini(&obj->__builtin_resv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004624 drm_gem_object_release(&obj->base);
4625 i915_gem_info_remove_obj(i915, obj->base.size);
4626
4627 kfree(obj->bit_17);
4628 i915_gem_object_free(obj);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004629
4630 if (on)
4631 cond_resched();
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004632 }
Chris Wilsoncc731f52017-10-13 21:26:21 +01004633 intel_runtime_pm_put(i915);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004634}
4635
4636static void i915_gem_flush_free_objects(struct drm_i915_private *i915)
4637{
4638 struct llist_node *freed;
4639
Chris Wilson87701b42017-10-13 21:26:20 +01004640 /* Free the oldest, most stale object to keep the free_list short */
4641 freed = NULL;
4642 if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */
4643 /* Only one consumer of llist_del_first() allowed */
4644 spin_lock(&i915->mm.free_lock);
4645 freed = llist_del_first(&i915->mm.free_list);
4646 spin_unlock(&i915->mm.free_lock);
4647 }
4648 if (unlikely(freed)) {
4649 freed->next = NULL;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004650 __i915_gem_free_objects(i915, freed);
Chris Wilson87701b42017-10-13 21:26:20 +01004651 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004652}
4653
4654static void __i915_gem_free_work(struct work_struct *work)
4655{
4656 struct drm_i915_private *i915 =
4657 container_of(work, struct drm_i915_private, mm.free_work);
4658 struct llist_node *freed;
Chris Wilson26e12f82011-03-20 11:20:19 +00004659
Chris Wilsonb1f788c2016-08-04 07:52:45 +01004660 /* All file-owned VMA should have been released by this point through
4661 * i915_gem_close_object(), or earlier by i915_gem_context_close().
4662 * However, the object may also be bound into the global GTT (e.g.
4663 * older GPUs without per-process support, or for direct access through
4664 * the GTT either for the user or for scanout). Those VMA still need to
4665 * unbound now.
4666 */
Chris Wilson1488fc02012-04-24 15:47:31 +01004667
Chris Wilsonf991c492017-11-06 11:15:08 +00004668 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004669 while ((freed = llist_del_all(&i915->mm.free_list))) {
Chris Wilsonf991c492017-11-06 11:15:08 +00004670 spin_unlock(&i915->mm.free_lock);
4671
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004672 __i915_gem_free_objects(i915, freed);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004673 if (need_resched())
Chris Wilsonf991c492017-11-06 11:15:08 +00004674 return;
4675
4676 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004677 }
Chris Wilsonf991c492017-11-06 11:15:08 +00004678 spin_unlock(&i915->mm.free_lock);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004679}
4680
4681static void __i915_gem_free_object_rcu(struct rcu_head *head)
4682{
4683 struct drm_i915_gem_object *obj =
4684 container_of(head, typeof(*obj), rcu);
4685 struct drm_i915_private *i915 = to_i915(obj->base.dev);
4686
4687 /* We can't simply use call_rcu() from i915_gem_free_object()
4688 * as we need to block whilst unbinding, and the call_rcu
4689 * task may be called from softirq context. So we take a
4690 * detour through a worker.
4691 */
4692 if (llist_add(&obj->freed, &i915->mm.free_list))
4693 schedule_work(&i915->mm.free_work);
4694}
4695
4696void i915_gem_free_object(struct drm_gem_object *gem_obj)
4697{
4698 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
4699
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004700 if (obj->mm.quirked)
4701 __i915_gem_object_unpin_pages(obj);
4702
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004703 if (discard_backing_storage(obj))
4704 obj->mm.madv = I915_MADV_DONTNEED;
Daniel Vettera071fa02014-06-18 23:28:09 +02004705
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004706 /* Before we free the object, make sure any pure RCU-only
4707 * read-side critical sections are complete, e.g.
4708 * i915_gem_busy_ioctl(). For the corresponding synchronized
4709 * lookup see i915_gem_object_lookup_rcu().
4710 */
4711 call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
Chris Wilsonbe726152010-07-23 23:18:50 +01004712}
4713
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004714void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj)
4715{
4716 lockdep_assert_held(&obj->base.dev->struct_mutex);
4717
Chris Wilsond1b48c12017-08-16 09:52:08 +01004718 if (!i915_gem_object_has_active_reference(obj) &&
4719 i915_gem_object_is_active(obj))
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004720 i915_gem_object_set_active_reference(obj);
4721 else
4722 i915_gem_object_put(obj);
4723}
4724
Chris Wilsonae6c4572017-11-10 14:26:28 +00004725static void assert_kernel_context_is_current(struct drm_i915_private *i915)
Chris Wilson3033aca2016-10-28 13:58:47 +01004726{
Chris Wilsonae6c4572017-11-10 14:26:28 +00004727 struct i915_gem_context *kernel_context = i915->kernel_context;
Chris Wilson3033aca2016-10-28 13:58:47 +01004728 struct intel_engine_cs *engine;
4729 enum intel_engine_id id;
4730
Chris Wilsonae6c4572017-11-10 14:26:28 +00004731 for_each_engine(engine, i915, id) {
4732 GEM_BUG_ON(__i915_gem_active_peek(&engine->timeline->last_request));
4733 GEM_BUG_ON(engine->last_retired_context != kernel_context);
4734 }
Chris Wilson3033aca2016-10-28 13:58:47 +01004735}
4736
Chris Wilson24145512017-01-24 11:01:35 +00004737void i915_gem_sanitize(struct drm_i915_private *i915)
4738{
Chris Wilsonf36325f2017-08-26 12:09:34 +01004739 if (i915_terminally_wedged(&i915->gpu_error)) {
4740 mutex_lock(&i915->drm.struct_mutex);
4741 i915_gem_unset_wedged(i915);
4742 mutex_unlock(&i915->drm.struct_mutex);
4743 }
4744
Chris Wilson24145512017-01-24 11:01:35 +00004745 /*
4746 * If we inherit context state from the BIOS or earlier occupants
4747 * of the GPU, the GPU may be in an inconsistent state when we
4748 * try to take over. The only way to remove the earlier state
4749 * is by resetting. However, resetting on earlier gen is tricky as
4750 * it may impact the display and we are uncertain about the stability
Joonas Lahtinenea117b82017-04-28 10:53:38 +03004751 * of the reset, so this could be applied to even earlier gen.
Chris Wilson24145512017-01-24 11:01:35 +00004752 */
Joonas Lahtinenea117b82017-04-28 10:53:38 +03004753 if (INTEL_GEN(i915) >= 5) {
Chris Wilson24145512017-01-24 11:01:35 +00004754 int reset = intel_gpu_reset(i915, ALL_ENGINES);
4755 WARN_ON(reset && reset != -ENODEV);
4756 }
4757}
4758
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004759int i915_gem_suspend(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07004760{
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004761 struct drm_device *dev = &dev_priv->drm;
Chris Wilsondcff85c2016-08-05 10:14:11 +01004762 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004763
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004764 intel_runtime_pm_get(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01004765 intel_suspend_gt_powersave(dev_priv);
4766
Chris Wilson45c5f202013-10-16 11:50:01 +01004767 mutex_lock(&dev->struct_mutex);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004768
4769 /* We have to flush all the executing contexts to main memory so
4770 * that they can saved in the hibernation image. To ensure the last
4771 * context image is coherent, we have to switch away from it. That
4772 * leaves the dev_priv->kernel_context still active when
4773 * we actually suspend, and its image in memory may not match the GPU
4774 * state. Fortunately, the kernel_context is disposable and we do
4775 * not rely on its state.
4776 */
4777 ret = i915_gem_switch_to_kernel_context(dev_priv);
4778 if (ret)
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004779 goto err_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01004780
Chris Wilson22dd3bb2016-09-09 14:11:50 +01004781 ret = i915_gem_wait_for_idle(dev_priv,
4782 I915_WAIT_INTERRUPTIBLE |
4783 I915_WAIT_LOCKED);
Chris Wilsoncad99462017-08-26 12:09:33 +01004784 if (ret && ret != -EIO)
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004785 goto err_unlock;
Chris Wilsonf7403342013-09-13 23:57:04 +01004786
Chris Wilson3033aca2016-10-28 13:58:47 +01004787 assert_kernel_context_is_current(dev_priv);
Chris Wilson829a0af2017-06-20 12:05:45 +01004788 i915_gem_contexts_lost(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01004789 mutex_unlock(&dev->struct_mutex);
4790
Sagar Arun Kamble63987bf2017-04-05 15:51:50 +05304791 intel_guc_suspend(dev_priv);
4792
Chris Wilson737b1502015-01-26 18:03:03 +02004793 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
Chris Wilson67d97da2016-07-04 08:08:31 +01004794 cancel_delayed_work_sync(&dev_priv->gt.retire_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004795
4796 /* As the idle_work is rearming if it detects a race, play safe and
4797 * repeat the flush until it is definitely idle.
4798 */
Chris Wilson7c262402017-10-06 11:40:38 +01004799 drain_delayed_work(&dev_priv->gt.idle_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004800
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004801 /* Assert that we sucessfully flushed all the work and
4802 * reset the GPU back to its idle, low power state.
4803 */
Chris Wilson67d97da2016-07-04 08:08:31 +01004804 WARN_ON(dev_priv->gt.awake);
Chris Wilsonfc692bd2017-08-26 12:09:35 +01004805 if (WARN_ON(!intel_engines_are_idle(dev_priv)))
4806 i915_gem_set_wedged(dev_priv); /* no hope, discard everything */
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004807
Imre Deak1c777c52016-10-12 17:46:37 +03004808 /*
4809 * Neither the BIOS, ourselves or any other kernel
4810 * expects the system to be in execlists mode on startup,
4811 * so we need to reset the GPU back to legacy mode. And the only
4812 * known way to disable logical contexts is through a GPU reset.
4813 *
4814 * So in order to leave the system in a known default configuration,
4815 * always reset the GPU upon unload and suspend. Afterwards we then
4816 * clean up the GEM state tracking, flushing off the requests and
4817 * leaving the system in a known idle state.
4818 *
4819 * Note that is of the upmost importance that the GPU is idle and
4820 * all stray writes are flushed *before* we dismantle the backing
4821 * storage for the pinned objects.
4822 *
4823 * However, since we are uncertain that resetting the GPU on older
4824 * machines is a good idea, we don't - just in case it leaves the
4825 * machine in an unusable condition.
4826 */
Chris Wilson24145512017-01-24 11:01:35 +00004827 i915_gem_sanitize(dev_priv);
Chris Wilsoncad99462017-08-26 12:09:33 +01004828
4829 intel_runtime_pm_put(dev_priv);
4830 return 0;
Imre Deak1c777c52016-10-12 17:46:37 +03004831
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004832err_unlock:
Chris Wilson45c5f202013-10-16 11:50:01 +01004833 mutex_unlock(&dev->struct_mutex);
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004834 intel_runtime_pm_put(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01004835 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004836}
4837
Chris Wilson37cd3302017-11-12 11:27:38 +00004838void i915_gem_resume(struct drm_i915_private *i915)
Chris Wilson5ab57c72016-07-15 14:56:20 +01004839{
Chris Wilson37cd3302017-11-12 11:27:38 +00004840 WARN_ON(i915->gt.awake);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004841
Chris Wilson37cd3302017-11-12 11:27:38 +00004842 mutex_lock(&i915->drm.struct_mutex);
4843 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
Imre Deak31ab49a2016-11-07 11:20:05 +02004844
Chris Wilson37cd3302017-11-12 11:27:38 +00004845 i915_gem_restore_gtt_mappings(i915);
4846 i915_gem_restore_fences(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004847
4848 /* As we didn't flush the kernel context before suspend, we cannot
4849 * guarantee that the context image is complete. So let's just reset
4850 * it and start again.
4851 */
Chris Wilson37cd3302017-11-12 11:27:38 +00004852 i915->gt.resume(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004853
Chris Wilson37cd3302017-11-12 11:27:38 +00004854 if (i915_gem_init_hw(i915))
4855 goto err_wedged;
4856
Chris Wilson7469c622017-11-14 13:03:00 +00004857 intel_guc_resume(i915);
4858
Chris Wilson37cd3302017-11-12 11:27:38 +00004859 /* Always reload a context for powersaving. */
4860 if (i915_gem_switch_to_kernel_context(i915))
4861 goto err_wedged;
4862
4863out_unlock:
4864 intel_uncore_forcewake_put(i915, FORCEWAKE_ALL);
4865 mutex_unlock(&i915->drm.struct_mutex);
4866 return;
4867
4868err_wedged:
4869 DRM_ERROR("failed to re-initialize GPU, declaring wedged!\n");
4870 i915_gem_set_wedged(i915);
4871 goto out_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01004872}
4873
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004874void i915_gem_init_swizzling(struct drm_i915_private *dev_priv)
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004875{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004876 if (INTEL_GEN(dev_priv) < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004877 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
4878 return;
4879
4880 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
4881 DISP_TILE_SURFACE_SWIZZLING);
4882
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004883 if (IS_GEN5(dev_priv))
Daniel Vetter11782b02012-01-31 16:47:55 +01004884 return;
4885
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004886 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004887 if (IS_GEN6(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004888 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004889 else if (IS_GEN7(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004890 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004891 else if (IS_GEN8(dev_priv))
Ben Widawsky31a53362013-11-02 21:07:04 -07004892 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08004893 else
4894 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004895}
Daniel Vettere21af882012-02-09 20:53:27 +01004896
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004897static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004898{
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004899 I915_WRITE(RING_CTL(base), 0);
4900 I915_WRITE(RING_HEAD(base), 0);
4901 I915_WRITE(RING_TAIL(base), 0);
4902 I915_WRITE(RING_START(base), 0);
4903}
4904
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004905static void init_unused_rings(struct drm_i915_private *dev_priv)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004906{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004907 if (IS_I830(dev_priv)) {
4908 init_unused_ring(dev_priv, PRB1_BASE);
4909 init_unused_ring(dev_priv, SRB0_BASE);
4910 init_unused_ring(dev_priv, SRB1_BASE);
4911 init_unused_ring(dev_priv, SRB2_BASE);
4912 init_unused_ring(dev_priv, SRB3_BASE);
4913 } else if (IS_GEN2(dev_priv)) {
4914 init_unused_ring(dev_priv, SRB0_BASE);
4915 init_unused_ring(dev_priv, SRB1_BASE);
4916 } else if (IS_GEN3(dev_priv)) {
4917 init_unused_ring(dev_priv, PRB1_BASE);
4918 init_unused_ring(dev_priv, PRB2_BASE);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004919 }
4920}
4921
Chris Wilson20a8a742017-02-08 14:30:31 +00004922static int __i915_gem_restart_engines(void *data)
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004923{
Chris Wilson20a8a742017-02-08 14:30:31 +00004924 struct drm_i915_private *i915 = data;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004925 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05304926 enum intel_engine_id id;
Chris Wilson20a8a742017-02-08 14:30:31 +00004927 int err;
4928
4929 for_each_engine(engine, i915, id) {
4930 err = engine->init_hw(engine);
4931 if (err)
4932 return err;
4933 }
4934
4935 return 0;
4936}
4937
4938int i915_gem_init_hw(struct drm_i915_private *dev_priv)
4939{
Chris Wilsond200cda2016-04-28 09:56:44 +01004940 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004941
Chris Wilsonde867c22016-10-25 13:16:02 +01004942 dev_priv->gt.last_init_time = ktime_get();
4943
Chris Wilson5e4f5182015-02-13 14:35:59 +00004944 /* Double layer security blanket, see i915_gem_init() */
4945 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4946
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00004947 if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07004948 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004949
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01004950 if (IS_HASWELL(dev_priv))
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004951 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
Ville Syrjälä0bf21342013-11-29 14:56:12 +02004952 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03004953
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004954 if (HAS_PCH_NOP(dev_priv)) {
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01004955 if (IS_IVYBRIDGE(dev_priv)) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01004956 u32 temp = I915_READ(GEN7_MSG_CTL);
4957 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
4958 I915_WRITE(GEN7_MSG_CTL, temp);
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004959 } else if (INTEL_GEN(dev_priv) >= 7) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01004960 u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
4961 temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
4962 I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
4963 }
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07004964 }
4965
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004966 i915_gem_init_swizzling(dev_priv);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004967
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004968 /*
4969 * At least 830 can leave some of the unused rings
4970 * "active" (ie. head != tail) after resume which
4971 * will prevent c3 entry. Makes sure all unused rings
4972 * are totally idle.
4973 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004974 init_unused_rings(dev_priv);
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004975
Dave Gordoned54c1a2016-01-19 19:02:54 +00004976 BUG_ON(!dev_priv->kernel_context);
Chris Wilson6f74b362017-10-15 15:37:25 +01004977 if (i915_terminally_wedged(&dev_priv->gpu_error)) {
4978 ret = -EIO;
4979 goto out;
4980 }
John Harrison90638cc2015-05-29 17:43:37 +01004981
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004982 ret = i915_ppgtt_init_hw(dev_priv);
John Harrison4ad2fd82015-06-18 13:11:20 +01004983 if (ret) {
4984 DRM_ERROR("PPGTT enable HW failed %d\n", ret);
4985 goto out;
4986 }
4987
Michał Winiarski9bdc3572017-10-25 18:25:19 +01004988 /* We can't enable contexts until all firmware is loaded */
4989 ret = intel_uc_init_hw(dev_priv);
4990 if (ret)
4991 goto out;
4992
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004993 intel_mocs_init_l3cc_table(dev_priv);
Peter Antoine0ccdacf2016-04-13 15:03:25 +01004994
Chris Wilson136109c2017-11-02 13:14:30 +00004995 /* Only when the HW is re-initialised, can we replay the requests */
4996 ret = __i915_gem_restart_engines(dev_priv);
Chris Wilson5e4f5182015-02-13 14:35:59 +00004997out:
4998 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004999 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005000}
5001
Chris Wilsond2b4b972017-11-10 14:26:33 +00005002static int __intel_engines_record_defaults(struct drm_i915_private *i915)
5003{
5004 struct i915_gem_context *ctx;
5005 struct intel_engine_cs *engine;
5006 enum intel_engine_id id;
5007 int err;
5008
5009 /*
5010 * As we reset the gpu during very early sanitisation, the current
5011 * register state on the GPU should reflect its defaults values.
5012 * We load a context onto the hw (with restore-inhibit), then switch
5013 * over to a second context to save that default register state. We
5014 * can then prime every new context with that state so they all start
5015 * from the same default HW values.
5016 */
5017
5018 ctx = i915_gem_context_create_kernel(i915, 0);
5019 if (IS_ERR(ctx))
5020 return PTR_ERR(ctx);
5021
5022 for_each_engine(engine, i915, id) {
5023 struct drm_i915_gem_request *rq;
5024
5025 rq = i915_gem_request_alloc(engine, ctx);
5026 if (IS_ERR(rq)) {
5027 err = PTR_ERR(rq);
5028 goto out_ctx;
5029 }
5030
Chris Wilson3fef5cd2017-11-20 10:20:02 +00005031 err = 0;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005032 if (engine->init_context)
5033 err = engine->init_context(rq);
5034
5035 __i915_add_request(rq, true);
5036 if (err)
5037 goto err_active;
5038 }
5039
5040 err = i915_gem_switch_to_kernel_context(i915);
5041 if (err)
5042 goto err_active;
5043
5044 err = i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED);
5045 if (err)
5046 goto err_active;
5047
5048 assert_kernel_context_is_current(i915);
5049
5050 for_each_engine(engine, i915, id) {
5051 struct i915_vma *state;
5052
5053 state = ctx->engine[id].state;
5054 if (!state)
5055 continue;
5056
5057 /*
5058 * As we will hold a reference to the logical state, it will
5059 * not be torn down with the context, and importantly the
5060 * object will hold onto its vma (making it possible for a
5061 * stray GTT write to corrupt our defaults). Unmap the vma
5062 * from the GTT to prevent such accidents and reclaim the
5063 * space.
5064 */
5065 err = i915_vma_unbind(state);
5066 if (err)
5067 goto err_active;
5068
5069 err = i915_gem_object_set_to_cpu_domain(state->obj, false);
5070 if (err)
5071 goto err_active;
5072
5073 engine->default_state = i915_gem_object_get(state->obj);
5074 }
5075
5076 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) {
5077 unsigned int found = intel_engines_has_context_isolation(i915);
5078
5079 /*
5080 * Make sure that classes with multiple engine instances all
5081 * share the same basic configuration.
5082 */
5083 for_each_engine(engine, i915, id) {
5084 unsigned int bit = BIT(engine->uabi_class);
5085 unsigned int expected = engine->default_state ? bit : 0;
5086
5087 if ((found & bit) != expected) {
5088 DRM_ERROR("mismatching default context state for class %d on engine %s\n",
5089 engine->uabi_class, engine->name);
5090 }
5091 }
5092 }
5093
5094out_ctx:
5095 i915_gem_context_set_closed(ctx);
5096 i915_gem_context_put(ctx);
5097 return err;
5098
5099err_active:
5100 /*
5101 * If we have to abandon now, we expect the engines to be idle
5102 * and ready to be torn-down. First try to flush any remaining
5103 * request, ensure we are pointing at the kernel context and
5104 * then remove it.
5105 */
5106 if (WARN_ON(i915_gem_switch_to_kernel_context(i915)))
5107 goto out_ctx;
5108
5109 if (WARN_ON(i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED)))
5110 goto out_ctx;
5111
5112 i915_gem_contexts_lost(i915);
5113 goto out_ctx;
5114}
5115
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005116int i915_gem_init(struct drm_i915_private *dev_priv)
Chris Wilson1070a422012-04-24 15:47:41 +01005117{
Chris Wilson1070a422012-04-24 15:47:41 +01005118 int ret;
5119
Matthew Auldda9fe3f32017-10-06 23:18:31 +01005120 /*
5121 * We need to fallback to 4K pages since gvt gtt handling doesn't
5122 * support huge page entries - we will need to check either hypervisor
5123 * mm can support huge guest page or just do emulation in gvt.
5124 */
5125 if (intel_vgpu_active(dev_priv))
5126 mkwrite_device_info(dev_priv)->page_sizes =
5127 I915_GTT_PAGE_SIZE_4K;
5128
Chris Wilson94312822017-05-03 10:39:18 +01005129 dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1);
Chris Wilson57822dc2017-02-22 11:40:48 +00005130
Chris Wilsonfb5c5512017-11-20 20:55:00 +00005131 if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) {
Chris Wilson821ed7d2016-09-09 14:11:53 +01005132 dev_priv->gt.resume = intel_lr_context_resume;
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005133 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
Chris Wilsonfb5c5512017-11-20 20:55:00 +00005134 } else {
5135 dev_priv->gt.resume = intel_legacy_submission_resume;
5136 dev_priv->gt.cleanup_engine = intel_engine_cleanup;
Oscar Mateoa83014d2014-07-24 17:04:21 +01005137 }
5138
Chris Wilsonee487002017-11-22 17:26:21 +00005139 ret = i915_gem_init_userptr(dev_priv);
5140 if (ret)
5141 return ret;
5142
Chris Wilson5e4f5182015-02-13 14:35:59 +00005143 /* This is just a security blanket to placate dragons.
5144 * On some systems, we very sporadically observe that the first TLBs
5145 * used by the CS may be stale, despite us poking the TLB reset. If
5146 * we hold the forcewake during initialisation these problems
5147 * just magically go away.
5148 */
Chris Wilsonee487002017-11-22 17:26:21 +00005149 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson5e4f5182015-02-13 14:35:59 +00005150 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5151
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01005152 ret = i915_gem_init_ggtt(dev_priv);
5153 if (ret)
5154 goto out_unlock;
Jesse Barnesd62b4892013-03-08 10:45:53 -08005155
Chris Wilson829a0af2017-06-20 12:05:45 +01005156 ret = i915_gem_contexts_init(dev_priv);
Jani Nikula7bcc3772014-12-05 14:17:42 +02005157 if (ret)
5158 goto out_unlock;
Ben Widawsky2fa48d82013-12-06 14:11:04 -08005159
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005160 ret = intel_engines_init(dev_priv);
Daniel Vetter35a57ff2014-11-20 00:33:07 +01005161 if (ret)
Jani Nikula7bcc3772014-12-05 14:17:42 +02005162 goto out_unlock;
Daniel Vetter53ca26c2012-04-26 23:28:03 +02005163
Chris Wilsonf58d13d2017-11-10 14:26:29 +00005164 intel_init_gt_powersave(dev_priv);
5165
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005166 ret = i915_gem_init_hw(dev_priv);
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005167 if (ret)
5168 goto out_unlock;
5169
5170 /*
5171 * Despite its name intel_init_clock_gating applies both display
5172 * clock gating workarounds; GT mmio workarounds and the occasional
5173 * GT power context workaround. Worse, sometimes it includes a context
5174 * register workaround which we need to apply before we record the
5175 * default HW state for all contexts.
5176 *
5177 * FIXME: break up the workarounds and apply them at the right time!
5178 */
5179 intel_init_clock_gating(dev_priv);
5180
Chris Wilsond2b4b972017-11-10 14:26:33 +00005181 ret = __intel_engines_record_defaults(dev_priv);
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005182out_unlock:
Chris Wilson60990322014-04-09 09:19:42 +01005183 if (ret == -EIO) {
Chris Wilson7e21d642016-07-27 09:07:29 +01005184 /* Allow engine initialisation to fail by marking the GPU as
Chris Wilson60990322014-04-09 09:19:42 +01005185 * wedged. But we only want to do this where the GPU is angry,
5186 * for all other failure, such as an allocation failure, bail.
5187 */
Chris Wilson6f74b362017-10-15 15:37:25 +01005188 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
5189 DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
5190 i915_gem_set_wedged(dev_priv);
5191 }
Chris Wilson60990322014-04-09 09:19:42 +01005192 ret = 0;
Chris Wilson1070a422012-04-24 15:47:41 +01005193 }
Chris Wilson5e4f5182015-02-13 14:35:59 +00005194 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005195 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson1070a422012-04-24 15:47:41 +01005196
Chris Wilson60990322014-04-09 09:19:42 +01005197 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01005198}
5199
Chris Wilson24145512017-01-24 11:01:35 +00005200void i915_gem_init_mmio(struct drm_i915_private *i915)
5201{
5202 i915_gem_sanitize(i915);
5203}
5204
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005205void
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005206i915_gem_cleanup_engines(struct drm_i915_private *dev_priv)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005207{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005208 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305209 enum intel_engine_id id;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005210
Akash Goel3b3f1652016-10-13 22:44:48 +05305211 for_each_engine(engine, dev_priv, id)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005212 dev_priv->gt.cleanup_engine(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005213}
5214
Eric Anholt673a3942008-07-30 12:06:12 -07005215void
Imre Deak40ae4e12016-03-16 14:54:03 +02005216i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
5217{
Chris Wilson49ef5292016-08-18 17:17:00 +01005218 int i;
Imre Deak40ae4e12016-03-16 14:54:03 +02005219
5220 if (INTEL_INFO(dev_priv)->gen >= 7 && !IS_VALLEYVIEW(dev_priv) &&
5221 !IS_CHERRYVIEW(dev_priv))
5222 dev_priv->num_fence_regs = 32;
Jani Nikula73f67aa2016-12-07 22:48:09 +02005223 else if (INTEL_INFO(dev_priv)->gen >= 4 ||
5224 IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
5225 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005226 dev_priv->num_fence_regs = 16;
5227 else
5228 dev_priv->num_fence_regs = 8;
5229
Chris Wilsonc0336662016-05-06 15:40:21 +01005230 if (intel_vgpu_active(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005231 dev_priv->num_fence_regs =
5232 I915_READ(vgtif_reg(avail_rs.fence_num));
5233
5234 /* Initialize fence registers to zero */
Chris Wilson49ef5292016-08-18 17:17:00 +01005235 for (i = 0; i < dev_priv->num_fence_regs; i++) {
5236 struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
5237
5238 fence->i915 = dev_priv;
5239 fence->id = i;
5240 list_add_tail(&fence->link, &dev_priv->mm.fence_list);
5241 }
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005242 i915_gem_restore_fences(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005243
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005244 i915_gem_detect_bit_6_swizzle(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005245}
5246
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005247static void i915_gem_init__mm(struct drm_i915_private *i915)
5248{
5249 spin_lock_init(&i915->mm.object_stat_lock);
5250 spin_lock_init(&i915->mm.obj_lock);
5251 spin_lock_init(&i915->mm.free_lock);
5252
5253 init_llist_head(&i915->mm.free_list);
5254
5255 INIT_LIST_HEAD(&i915->mm.unbound_list);
5256 INIT_LIST_HEAD(&i915->mm.bound_list);
5257 INIT_LIST_HEAD(&i915->mm.fence_list);
5258 INIT_LIST_HEAD(&i915->mm.userfault_list);
5259
5260 INIT_WORK(&i915->mm.free_work, __i915_gem_free_work);
5261}
5262
Chris Wilson73cb9702016-10-28 13:58:46 +01005263int
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005264i915_gem_load_init(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07005265{
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005266 int err = -ENOMEM;
Chris Wilson42dcedd2012-11-15 11:32:30 +00005267
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005268 dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
5269 if (!dev_priv->objects)
Chris Wilson73cb9702016-10-28 13:58:46 +01005270 goto err_out;
Chris Wilson73cb9702016-10-28 13:58:46 +01005271
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005272 dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
5273 if (!dev_priv->vmas)
Chris Wilson73cb9702016-10-28 13:58:46 +01005274 goto err_objects;
Chris Wilson73cb9702016-10-28 13:58:46 +01005275
Chris Wilsond1b48c12017-08-16 09:52:08 +01005276 dev_priv->luts = KMEM_CACHE(i915_lut_handle, 0);
5277 if (!dev_priv->luts)
5278 goto err_vmas;
5279
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005280 dev_priv->requests = KMEM_CACHE(drm_i915_gem_request,
5281 SLAB_HWCACHE_ALIGN |
5282 SLAB_RECLAIM_ACCOUNT |
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -08005283 SLAB_TYPESAFE_BY_RCU);
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005284 if (!dev_priv->requests)
Chris Wilsond1b48c12017-08-16 09:52:08 +01005285 goto err_luts;
Chris Wilson73cb9702016-10-28 13:58:46 +01005286
Chris Wilson52e54202016-11-14 20:41:02 +00005287 dev_priv->dependencies = KMEM_CACHE(i915_dependency,
5288 SLAB_HWCACHE_ALIGN |
5289 SLAB_RECLAIM_ACCOUNT);
5290 if (!dev_priv->dependencies)
5291 goto err_requests;
5292
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005293 dev_priv->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN);
5294 if (!dev_priv->priorities)
5295 goto err_dependencies;
5296
Chris Wilson73cb9702016-10-28 13:58:46 +01005297 mutex_lock(&dev_priv->drm.struct_mutex);
5298 INIT_LIST_HEAD(&dev_priv->gt.timelines);
Chris Wilsonbb894852016-11-14 20:40:57 +00005299 err = i915_gem_timeline_init__global(dev_priv);
Chris Wilson73cb9702016-10-28 13:58:46 +01005300 mutex_unlock(&dev_priv->drm.struct_mutex);
5301 if (err)
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005302 goto err_priorities;
Eric Anholt673a3942008-07-30 12:06:12 -07005303
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005304 i915_gem_init__mm(dev_priv);
Chris Wilsonf2123812017-10-16 12:40:37 +01005305
Chris Wilson67d97da2016-07-04 08:08:31 +01005306 INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
Eric Anholt673a3942008-07-30 12:06:12 -07005307 i915_gem_retire_work_handler);
Chris Wilson67d97da2016-07-04 08:08:31 +01005308 INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005309 i915_gem_idle_work_handler);
Chris Wilson1f15b762016-07-01 17:23:14 +01005310 init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01005311 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson31169712009-09-14 16:50:28 +01005312
Joonas Lahtinen6f633402016-09-01 14:58:21 +03005313 atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
5314
Chris Wilsonb5add952016-08-04 16:32:36 +01005315 spin_lock_init(&dev_priv->fb_tracking.lock);
Chris Wilson73cb9702016-10-28 13:58:46 +01005316
Matthew Auld465c4032017-10-06 23:18:14 +01005317 err = i915_gemfs_init(dev_priv);
5318 if (err)
5319 DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err);
5320
Chris Wilson73cb9702016-10-28 13:58:46 +01005321 return 0;
5322
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005323err_priorities:
5324 kmem_cache_destroy(dev_priv->priorities);
Chris Wilson52e54202016-11-14 20:41:02 +00005325err_dependencies:
5326 kmem_cache_destroy(dev_priv->dependencies);
Chris Wilson73cb9702016-10-28 13:58:46 +01005327err_requests:
5328 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005329err_luts:
5330 kmem_cache_destroy(dev_priv->luts);
Chris Wilson73cb9702016-10-28 13:58:46 +01005331err_vmas:
5332 kmem_cache_destroy(dev_priv->vmas);
5333err_objects:
5334 kmem_cache_destroy(dev_priv->objects);
5335err_out:
5336 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07005337}
Dave Airlie71acb5e2008-12-30 20:31:46 +10005338
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005339void i915_gem_load_cleanup(struct drm_i915_private *dev_priv)
Imre Deakd64aa092016-01-19 15:26:29 +02005340{
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005341 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson7d5d59e2016-11-01 08:48:41 +00005342 WARN_ON(!llist_empty(&dev_priv->mm.free_list));
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005343 WARN_ON(dev_priv->mm.object_count);
Chris Wilson7d5d59e2016-11-01 08:48:41 +00005344
Matthew Auldea84aa72016-11-17 21:04:11 +00005345 mutex_lock(&dev_priv->drm.struct_mutex);
5346 i915_gem_timeline_fini(&dev_priv->gt.global_timeline);
5347 WARN_ON(!list_empty(&dev_priv->gt.timelines));
5348 mutex_unlock(&dev_priv->drm.struct_mutex);
5349
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005350 kmem_cache_destroy(dev_priv->priorities);
Chris Wilson52e54202016-11-14 20:41:02 +00005351 kmem_cache_destroy(dev_priv->dependencies);
Imre Deakd64aa092016-01-19 15:26:29 +02005352 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005353 kmem_cache_destroy(dev_priv->luts);
Imre Deakd64aa092016-01-19 15:26:29 +02005354 kmem_cache_destroy(dev_priv->vmas);
5355 kmem_cache_destroy(dev_priv->objects);
Chris Wilson0eafec62016-08-04 16:32:41 +01005356
5357 /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
5358 rcu_barrier();
Matthew Auld465c4032017-10-06 23:18:14 +01005359
5360 i915_gemfs_fini(dev_priv);
Imre Deakd64aa092016-01-19 15:26:29 +02005361}
5362
Chris Wilson6a800ea2016-09-21 14:51:07 +01005363int i915_gem_freeze(struct drm_i915_private *dev_priv)
5364{
Chris Wilsond0aa3012017-04-07 11:25:49 +01005365 /* Discard all purgeable objects, let userspace recover those as
5366 * required after resuming.
5367 */
Chris Wilson6a800ea2016-09-21 14:51:07 +01005368 i915_gem_shrink_all(dev_priv);
Chris Wilson6a800ea2016-09-21 14:51:07 +01005369
Chris Wilson6a800ea2016-09-21 14:51:07 +01005370 return 0;
5371}
5372
Chris Wilson461fb992016-05-14 07:26:33 +01005373int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
5374{
5375 struct drm_i915_gem_object *obj;
Chris Wilson7aab2d52016-09-09 20:02:18 +01005376 struct list_head *phases[] = {
5377 &dev_priv->mm.unbound_list,
5378 &dev_priv->mm.bound_list,
5379 NULL
5380 }, **p;
Chris Wilson461fb992016-05-14 07:26:33 +01005381
5382 /* Called just before we write the hibernation image.
5383 *
5384 * We need to update the domain tracking to reflect that the CPU
5385 * will be accessing all the pages to create and restore from the
5386 * hibernation, and so upon restoration those pages will be in the
5387 * CPU domain.
5388 *
5389 * To make sure the hibernation image contains the latest state,
5390 * we update that state just before writing out the image.
Chris Wilson7aab2d52016-09-09 20:02:18 +01005391 *
5392 * To try and reduce the hibernation image, we manually shrink
Chris Wilsond0aa3012017-04-07 11:25:49 +01005393 * the objects as well, see i915_gem_freeze()
Chris Wilson461fb992016-05-14 07:26:33 +01005394 */
5395
Chris Wilson912d5722017-09-06 16:19:30 -07005396 i915_gem_shrink(dev_priv, -1UL, NULL, I915_SHRINK_UNBOUND);
Chris Wilson17b93c42017-04-07 11:25:50 +01005397 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson461fb992016-05-14 07:26:33 +01005398
Chris Wilsonf2123812017-10-16 12:40:37 +01005399 spin_lock(&dev_priv->mm.obj_lock);
Chris Wilson7aab2d52016-09-09 20:02:18 +01005400 for (p = phases; *p; p++) {
Chris Wilsonf2123812017-10-16 12:40:37 +01005401 list_for_each_entry(obj, *p, mm.link)
Chris Wilsone27ab732017-06-15 13:38:49 +01005402 __start_cpu_write(obj);
Chris Wilson461fb992016-05-14 07:26:33 +01005403 }
Chris Wilsonf2123812017-10-16 12:40:37 +01005404 spin_unlock(&dev_priv->mm.obj_lock);
Chris Wilson461fb992016-05-14 07:26:33 +01005405
5406 return 0;
5407}
5408
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005409void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00005410{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005411 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilson15f7bbc2016-07-26 12:01:52 +01005412 struct drm_i915_gem_request *request;
Eric Anholtb9624422009-06-03 07:27:35 +00005413
5414 /* Clean up our request list when the client is going away, so that
5415 * later retire_requests won't dereference our soon-to-be-gone
5416 * file_priv.
5417 */
Chris Wilson1c255952010-09-26 11:03:27 +01005418 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00005419 list_for_each_entry(request, &file_priv->mm.request_list, client_link)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005420 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01005421 spin_unlock(&file_priv->mm.lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005422}
5423
Chris Wilson829a0af2017-06-20 12:05:45 +01005424int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005425{
5426 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08005427 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005428
Chris Wilsonc4c29d72016-11-09 10:45:07 +00005429 DRM_DEBUG("\n");
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005430
5431 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
5432 if (!file_priv)
5433 return -ENOMEM;
5434
5435 file->driver_priv = file_priv;
Chris Wilson829a0af2017-06-20 12:05:45 +01005436 file_priv->dev_priv = i915;
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02005437 file_priv->file = file;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005438
5439 spin_lock_init(&file_priv->mm.lock);
5440 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005441
Chris Wilsonc80ff162016-07-27 09:07:27 +01005442 file_priv->bsd_engine = -1;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00005443
Chris Wilson829a0af2017-06-20 12:05:45 +01005444 ret = i915_gem_context_open(i915, file);
Ben Widawskye422b882013-12-06 14:10:58 -08005445 if (ret)
5446 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005447
Ben Widawskye422b882013-12-06 14:10:58 -08005448 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005449}
5450
Daniel Vetterb680c372014-09-19 18:27:27 +02005451/**
5452 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07005453 * @old: current GEM buffer for the frontbuffer slots
5454 * @new: new GEM buffer for the frontbuffer slots
5455 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02005456 *
5457 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
5458 * from @old and setting them in @new. Both @old and @new can be NULL.
5459 */
Daniel Vettera071fa02014-06-18 23:28:09 +02005460void i915_gem_track_fb(struct drm_i915_gem_object *old,
5461 struct drm_i915_gem_object *new,
5462 unsigned frontbuffer_bits)
5463{
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005464 /* Control of individual bits within the mask are guarded by
5465 * the owning plane->mutex, i.e. we can never see concurrent
5466 * manipulation of individual bits. But since the bitfield as a whole
5467 * is updated using RMW, we need to use atomics in order to update
5468 * the bits.
5469 */
5470 BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
5471 sizeof(atomic_t) * BITS_PER_BYTE);
5472
Daniel Vettera071fa02014-06-18 23:28:09 +02005473 if (old) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005474 WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
5475 atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005476 }
5477
5478 if (new) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005479 WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
5480 atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005481 }
5482}
5483
Dave Gordonea702992015-07-09 19:29:02 +01005484/* Allocate a new GEM object and fill it with the supplied data */
5485struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005486i915_gem_object_create_from_data(struct drm_i915_private *dev_priv,
Dave Gordonea702992015-07-09 19:29:02 +01005487 const void *data, size_t size)
5488{
5489 struct drm_i915_gem_object *obj;
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005490 struct file *file;
5491 size_t offset;
5492 int err;
Dave Gordonea702992015-07-09 19:29:02 +01005493
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005494 obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE));
Chris Wilsonfe3db792016-04-25 13:32:13 +01005495 if (IS_ERR(obj))
Dave Gordonea702992015-07-09 19:29:02 +01005496 return obj;
5497
Chris Wilsonce8ff092017-03-17 19:46:47 +00005498 GEM_BUG_ON(obj->base.write_domain != I915_GEM_DOMAIN_CPU);
Dave Gordonea702992015-07-09 19:29:02 +01005499
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005500 file = obj->base.filp;
5501 offset = 0;
5502 do {
5503 unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
5504 struct page *page;
5505 void *pgdata, *vaddr;
Dave Gordonea702992015-07-09 19:29:02 +01005506
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005507 err = pagecache_write_begin(file, file->f_mapping,
5508 offset, len, 0,
5509 &page, &pgdata);
5510 if (err < 0)
5511 goto fail;
Dave Gordonea702992015-07-09 19:29:02 +01005512
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005513 vaddr = kmap(page);
5514 memcpy(vaddr, data, len);
5515 kunmap(page);
5516
5517 err = pagecache_write_end(file, file->f_mapping,
5518 offset, len, len,
5519 page, pgdata);
5520 if (err < 0)
5521 goto fail;
5522
5523 size -= len;
5524 data += len;
5525 offset += len;
5526 } while (size);
Dave Gordonea702992015-07-09 19:29:02 +01005527
5528 return obj;
5529
5530fail:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01005531 i915_gem_object_put(obj);
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005532 return ERR_PTR(err);
Dave Gordonea702992015-07-09 19:29:02 +01005533}
Chris Wilson96d77632016-10-28 13:58:33 +01005534
5535struct scatterlist *
5536i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
5537 unsigned int n,
5538 unsigned int *offset)
5539{
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005540 struct i915_gem_object_page_iter *iter = &obj->mm.get_page;
Chris Wilson96d77632016-10-28 13:58:33 +01005541 struct scatterlist *sg;
5542 unsigned int idx, count;
5543
5544 might_sleep();
5545 GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005546 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
Chris Wilson96d77632016-10-28 13:58:33 +01005547
5548 /* As we iterate forward through the sg, we record each entry in a
5549 * radixtree for quick repeated (backwards) lookups. If we have seen
5550 * this index previously, we will have an entry for it.
5551 *
5552 * Initial lookup is O(N), but this is amortized to O(1) for
5553 * sequential page access (where each new request is consecutive
5554 * to the previous one). Repeated lookups are O(lg(obj->base.size)),
5555 * i.e. O(1) with a large constant!
5556 */
5557 if (n < READ_ONCE(iter->sg_idx))
5558 goto lookup;
5559
5560 mutex_lock(&iter->lock);
5561
5562 /* We prefer to reuse the last sg so that repeated lookup of this
5563 * (or the subsequent) sg are fast - comparing against the last
5564 * sg is faster than going through the radixtree.
5565 */
5566
5567 sg = iter->sg_pos;
5568 idx = iter->sg_idx;
5569 count = __sg_page_count(sg);
5570
5571 while (idx + count <= n) {
5572 unsigned long exception, i;
5573 int ret;
5574
5575 /* If we cannot allocate and insert this entry, or the
5576 * individual pages from this range, cancel updating the
5577 * sg_idx so that on this lookup we are forced to linearly
5578 * scan onwards, but on future lookups we will try the
5579 * insertion again (in which case we need to be careful of
5580 * the error return reporting that we have already inserted
5581 * this index).
5582 */
5583 ret = radix_tree_insert(&iter->radix, idx, sg);
5584 if (ret && ret != -EEXIST)
5585 goto scan;
5586
5587 exception =
5588 RADIX_TREE_EXCEPTIONAL_ENTRY |
5589 idx << RADIX_TREE_EXCEPTIONAL_SHIFT;
5590 for (i = 1; i < count; i++) {
5591 ret = radix_tree_insert(&iter->radix, idx + i,
5592 (void *)exception);
5593 if (ret && ret != -EEXIST)
5594 goto scan;
5595 }
5596
5597 idx += count;
5598 sg = ____sg_next(sg);
5599 count = __sg_page_count(sg);
5600 }
5601
5602scan:
5603 iter->sg_pos = sg;
5604 iter->sg_idx = idx;
5605
5606 mutex_unlock(&iter->lock);
5607
5608 if (unlikely(n < idx)) /* insertion completed by another thread */
5609 goto lookup;
5610
5611 /* In case we failed to insert the entry into the radixtree, we need
5612 * to look beyond the current sg.
5613 */
5614 while (idx + count <= n) {
5615 idx += count;
5616 sg = ____sg_next(sg);
5617 count = __sg_page_count(sg);
5618 }
5619
5620 *offset = n - idx;
5621 return sg;
5622
5623lookup:
5624 rcu_read_lock();
5625
5626 sg = radix_tree_lookup(&iter->radix, n);
5627 GEM_BUG_ON(!sg);
5628
5629 /* If this index is in the middle of multi-page sg entry,
5630 * the radixtree will contain an exceptional entry that points
5631 * to the start of that range. We will return the pointer to
5632 * the base page and the offset of this page within the
5633 * sg entry's range.
5634 */
5635 *offset = 0;
5636 if (unlikely(radix_tree_exception(sg))) {
5637 unsigned long base =
5638 (unsigned long)sg >> RADIX_TREE_EXCEPTIONAL_SHIFT;
5639
5640 sg = radix_tree_lookup(&iter->radix, base);
5641 GEM_BUG_ON(!sg);
5642
5643 *offset = n - base;
5644 }
5645
5646 rcu_read_unlock();
5647
5648 return sg;
5649}
5650
5651struct page *
5652i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
5653{
5654 struct scatterlist *sg;
5655 unsigned int offset;
5656
5657 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
5658
5659 sg = i915_gem_object_get_sg(obj, n, &offset);
5660 return nth_page(sg_page(sg), offset);
5661}
5662
5663/* Like i915_gem_object_get_page(), but mark the returned page dirty */
5664struct page *
5665i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
5666 unsigned int n)
5667{
5668 struct page *page;
5669
5670 page = i915_gem_object_get_page(obj, n);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005671 if (!obj->mm.dirty)
Chris Wilson96d77632016-10-28 13:58:33 +01005672 set_page_dirty(page);
5673
5674 return page;
5675}
5676
5677dma_addr_t
5678i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
5679 unsigned long n)
5680{
5681 struct scatterlist *sg;
5682 unsigned int offset;
5683
5684 sg = i915_gem_object_get_sg(obj, n, &offset);
5685 return sg_dma_address(sg) + (offset << PAGE_SHIFT);
5686}
Chris Wilson935a2f72017-02-13 17:15:13 +00005687
Chris Wilson8eeb7902017-07-26 19:16:01 +01005688int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align)
5689{
5690 struct sg_table *pages;
5691 int err;
5692
5693 if (align > obj->base.size)
5694 return -EINVAL;
5695
5696 if (obj->ops == &i915_gem_phys_ops)
5697 return 0;
5698
5699 if (obj->ops != &i915_gem_object_ops)
5700 return -EINVAL;
5701
5702 err = i915_gem_object_unbind(obj);
5703 if (err)
5704 return err;
5705
5706 mutex_lock(&obj->mm.lock);
5707
5708 if (obj->mm.madv != I915_MADV_WILLNEED) {
5709 err = -EFAULT;
5710 goto err_unlock;
5711 }
5712
5713 if (obj->mm.quirked) {
5714 err = -EFAULT;
5715 goto err_unlock;
5716 }
5717
5718 if (obj->mm.mapping) {
5719 err = -EBUSY;
5720 goto err_unlock;
5721 }
5722
Chris Wilsonf2123812017-10-16 12:40:37 +01005723 pages = fetch_and_zero(&obj->mm.pages);
5724 if (pages) {
5725 struct drm_i915_private *i915 = to_i915(obj->base.dev);
5726
5727 __i915_gem_object_reset_page_iter(obj);
5728
5729 spin_lock(&i915->mm.obj_lock);
5730 list_del(&obj->mm.link);
5731 spin_unlock(&i915->mm.obj_lock);
5732 }
5733
Chris Wilson8eeb7902017-07-26 19:16:01 +01005734 obj->ops = &i915_gem_phys_ops;
5735
Chris Wilson8fb6a5d2017-07-26 19:16:02 +01005736 err = ____i915_gem_object_get_pages(obj);
Chris Wilson8eeb7902017-07-26 19:16:01 +01005737 if (err)
5738 goto err_xfer;
5739
5740 /* Perma-pin (until release) the physical set of pages */
5741 __i915_gem_object_pin_pages(obj);
5742
5743 if (!IS_ERR_OR_NULL(pages))
5744 i915_gem_object_ops.put_pages(obj, pages);
5745 mutex_unlock(&obj->mm.lock);
5746 return 0;
5747
5748err_xfer:
5749 obj->ops = &i915_gem_object_ops;
5750 obj->mm.pages = pages;
5751err_unlock:
5752 mutex_unlock(&obj->mm.lock);
5753 return err;
5754}
5755
Chris Wilson935a2f72017-02-13 17:15:13 +00005756#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
5757#include "selftests/scatterlist.c"
Chris Wilson66d9cb52017-02-13 17:15:17 +00005758#include "selftests/mock_gem_device.c"
Chris Wilson44653982017-02-13 17:15:20 +00005759#include "selftests/huge_gem_object.c"
Matthew Auld40498662017-10-06 23:18:29 +01005760#include "selftests/huge_pages.c"
Chris Wilson8335fd62017-02-13 17:15:28 +00005761#include "selftests/i915_gem_object.c"
Chris Wilson17059452017-02-13 17:15:32 +00005762#include "selftests/i915_gem_coherency.c"
Chris Wilson935a2f72017-02-13 17:15:13 +00005763#endif